* [RFC] tools/nolibc: x86_64: Make it compile with -pie
@ 2023-08-20 18:19 Vitaly Chikunov
2023-08-20 19:13 ` Thomas Weißschuh
0 siblings, 1 reply; 3+ messages in thread
From: Vitaly Chikunov @ 2023-08-20 18:19 UTC (permalink / raw)
To: Willy Tarreau, linux-kernel, linux-kselftest
Use RIP-relative addressing when setting `environ` and `_auxv` in
startup code.
Some toolchains have `-pie` enabled by default. On them or when -pie is
specified manually gcc produces error like this:
ld: /tmp/cci0uPcR.o: relocation R_X86_64_32S against symbol `environ' can not be used when making a PIE object; recompile with -fPIE
ld: failed to set dynamic section sizes: bad value
This is because asm() startup code accesses there pointers with absolute
addressing.
This may inspire others to fix the problem for other architectures too.
Signed-off-by: Vitaly Chikunov <vt@altlinux.org>
---
tools/include/nolibc/arch-x86_64.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/tools/include/nolibc/arch-x86_64.h b/tools/include/nolibc/arch-x86_64.h
index 6fc4d8392742..a6be44b333ce 100644
--- a/tools/include/nolibc/arch-x86_64.h
+++ b/tools/include/nolibc/arch-x86_64.h
@@ -199,14 +199,14 @@ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_pr
"pop %rdi\n" /* argc (first arg, %rdi) */
"mov %rsp, %rsi\n" /* argv[] (second arg, %rsi) */
"lea 8(%rsi,%rdi,8),%rdx\n" /* then a NULL then envp (third arg, %rdx) */
- "mov %rdx, environ\n" /* save environ */
+ "mov %rdx, environ(%rip)\n" /* save environ */
"xor %ebp, %ebp\n" /* zero the stack frame */
"mov %rdx, %rax\n" /* search for auxv (follows NULL after last env) */
"0:\n"
"add $8, %rax\n" /* search for auxv using rax, it follows the */
"cmp -8(%rax), %rbp\n" /* ... NULL after last env (rbp is zero here) */
"jnz 0b\n"
- "mov %rax, _auxv\n" /* save it into _auxv */
+ "mov %rax, _auxv(%rip)\n" /* save it into _auxv */
"and $-16, %rsp\n" /* x86 ABI : esp must be 16-byte aligned before call */
"call main\n" /* main() returns the status code, we'll exit with it. */
"mov %eax, %edi\n" /* retrieve exit code (32 bit) */
--
2.33.8
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [RFC] tools/nolibc: x86_64: Make it compile with -pie
2023-08-20 18:19 [RFC] tools/nolibc: x86_64: Make it compile with -pie Vitaly Chikunov
@ 2023-08-20 19:13 ` Thomas Weißschuh
2023-08-20 19:28 ` Vitaly Chikunov
0 siblings, 1 reply; 3+ messages in thread
From: Thomas Weißschuh @ 2023-08-20 19:13 UTC (permalink / raw)
To: Vitaly Chikunov; +Cc: Willy Tarreau, linux-kernel, linux-kselftest
Hi Vitaly,
On 2023-08-20 21:19:00+0300, Vitaly Chikunov wrote:
> Use RIP-relative addressing when setting `environ` and `_auxv` in
> startup code.
>
> Some toolchains have `-pie` enabled by default. On them or when -pie is
> specified manually gcc produces error like this:
>
> ld: /tmp/cci0uPcR.o: relocation R_X86_64_32S against symbol `environ' can not be used when making a PIE object; recompile with -fPIE
> ld: failed to set dynamic section sizes: bad value
>
> This is because asm() startup code accesses there pointers with absolute
> addressing.
>
> This may inspire others to fix the problem for other architectures too.
>
> Signed-off-by: Vitaly Chikunov <vt@altlinux.org>
> ---
> tools/include/nolibc/arch-x86_64.h | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/tools/include/nolibc/arch-x86_64.h b/tools/include/nolibc/arch-x86_64.h
> index 6fc4d8392742..a6be44b333ce 100644
> --- a/tools/include/nolibc/arch-x86_64.h
> +++ b/tools/include/nolibc/arch-x86_64.h
> @@ -199,14 +199,14 @@ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_pr
> "pop %rdi\n" /* argc (first arg, %rdi) */
> "mov %rsp, %rsi\n" /* argv[] (second arg, %rsi) */
> "lea 8(%rsi,%rdi,8),%rdx\n" /* then a NULL then envp (third arg, %rdx) */
> - "mov %rdx, environ\n" /* save environ */
> + "mov %rdx, environ(%rip)\n" /* save environ */
> "xor %ebp, %ebp\n" /* zero the stack frame */
> "mov %rdx, %rax\n" /* search for auxv (follows NULL after last env) */
> "0:\n"
> "add $8, %rax\n" /* search for auxv using rax, it follows the */
> "cmp -8(%rax), %rbp\n" /* ... NULL after last env (rbp is zero here) */
> "jnz 0b\n"
> - "mov %rax, _auxv\n" /* save it into _auxv */
> + "mov %rax, _auxv(%rip)\n" /* save it into _auxv */
> "and $-16, %rsp\n" /* x86 ABI : esp must be 16-byte aligned before call */
> "call main\n" /* main() returns the status code, we'll exit with it. */
> "mov %eax, %edi\n" /* retrieve exit code (32 bit) */
> --
> 2.33.8
>
nolibc recently switched to startup code that is mostly implemented in C
instead of assembly.
See the current linux-next tree.
That should make this change unnecessary I guess.
Thomas
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [RFC] tools/nolibc: x86_64: Make it compile with -pie
2023-08-20 19:13 ` Thomas Weißschuh
@ 2023-08-20 19:28 ` Vitaly Chikunov
0 siblings, 0 replies; 3+ messages in thread
From: Vitaly Chikunov @ 2023-08-20 19:28 UTC (permalink / raw)
To: Thomas Weißschuh; +Cc: Willy Tarreau, linux-kernel, linux-kselftest
Thomas,
On Sun, Aug 20, 2023 at 09:13:51PM +0200, Thomas Weißschuh wrote:
> On 2023-08-20 21:19:00+0300, Vitaly Chikunov wrote:
> > Use RIP-relative addressing when setting `environ` and `_auxv` in
> > startup code.
> >
> > Some toolchains have `-pie` enabled by default. On them or when -pie is
> > specified manually gcc produces error like this:
> >
> > ld: /tmp/cci0uPcR.o: relocation R_X86_64_32S against symbol `environ' can not be used when making a PIE object; recompile with -fPIE
> > ld: failed to set dynamic section sizes: bad value
> >
> > This is because asm() startup code accesses there pointers with absolute
> > addressing.
> >
> > This may inspire others to fix the problem for other architectures too.
> >
> > Signed-off-by: Vitaly Chikunov <vt@altlinux.org>
> > ---
> > tools/include/nolibc/arch-x86_64.h | 4 ++--
> > 1 file changed, 2 insertions(+), 2 deletions(-)
> >
> > diff --git a/tools/include/nolibc/arch-x86_64.h b/tools/include/nolibc/arch-x86_64.h
> > index 6fc4d8392742..a6be44b333ce 100644
> > --- a/tools/include/nolibc/arch-x86_64.h
> > +++ b/tools/include/nolibc/arch-x86_64.h
> > @@ -199,14 +199,14 @@ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_pr
> > "pop %rdi\n" /* argc (first arg, %rdi) */
> > "mov %rsp, %rsi\n" /* argv[] (second arg, %rsi) */
> > "lea 8(%rsi,%rdi,8),%rdx\n" /* then a NULL then envp (third arg, %rdx) */
> > - "mov %rdx, environ\n" /* save environ */
> > + "mov %rdx, environ(%rip)\n" /* save environ */
> > "xor %ebp, %ebp\n" /* zero the stack frame */
> > "mov %rdx, %rax\n" /* search for auxv (follows NULL after last env) */
> > "0:\n"
> > "add $8, %rax\n" /* search for auxv using rax, it follows the */
> > "cmp -8(%rax), %rbp\n" /* ... NULL after last env (rbp is zero here) */
> > "jnz 0b\n"
> > - "mov %rax, _auxv\n" /* save it into _auxv */
> > + "mov %rax, _auxv(%rip)\n" /* save it into _auxv */
> > "and $-16, %rsp\n" /* x86 ABI : esp must be 16-byte aligned before call */
> > "call main\n" /* main() returns the status code, we'll exit with it. */
> > "mov %eax, %edi\n" /* retrieve exit code (32 bit) */
> > --
> > 2.33.8
> >
>
> nolibc recently switched to startup code that is mostly implemented in C
> instead of assembly.
> See the current linux-next tree.
>
> That should make this change unnecessary I guess.
Yes seems so! Zhangjin Wu done a fine job.
Thanks,
>
>
> Thomas
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-08-20 19:32 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-08-20 18:19 [RFC] tools/nolibc: x86_64: Make it compile with -pie Vitaly Chikunov
2023-08-20 19:13 ` Thomas Weißschuh
2023-08-20 19:28 ` Vitaly Chikunov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox