All of lore.kernel.org
 help / color / mirror / Atom feed
From: Vitaly Chikunov <vt@altlinux.org>
To: Willy Tarreau <w@1wt.eu>,
	linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org
Subject: [RFC] tools/nolibc: x86_64: Make it compile with -pie
Date: Sun, 20 Aug 2023 21:19:00 +0300	[thread overview]
Message-ID: <20230820181900.3786107-1-vt@altlinux.org> (raw)

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


             reply	other threads:[~2023-08-20 18:31 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-20 18:19 Vitaly Chikunov [this message]
2023-08-20 19:13 ` [RFC] tools/nolibc: x86_64: Make it compile with -pie Thomas Weißschuh
2023-08-20 19:28   ` Vitaly Chikunov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230820181900.3786107-1-vt@altlinux.org \
    --to=vt@altlinux.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=w@1wt.eu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.