unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella via Libc-alpha <libc-alpha@sourceware.org>
To: libc-alpha@sourceware.org, Fangrui Song <maskray@google.com>,
	"H . J . Lu" <hjl.tools@gmail.com>
Subject: [PATCH] i386: Avoid avoid rely on linker optimization to avoid relocation
Date: Thu, 17 Nov 2022 09:43:17 -0300	[thread overview]
Message-ID: <20221117124317.2816607-1-adhemerval.zanella@linaro.org> (raw)

lld does not implement all the linker optimization to avoid the GOT
relocation as done by binutils (bfd/elf32-i386.c:elf_i386_convert_load_reloc).
The current 'movl main@GOT(%ebx), %eax' will then create a GOT
relocation when building with lld, which make static-pie status to
not being able to start the provided main function.

The change uses a __wrap_main local symbol, which in turn calls main
(similar as used by aarch64 and s390x).

Checked on i686-linux-gnu with binutils and lld.
---
 sysdeps/i386/start.S | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/sysdeps/i386/start.S b/sysdeps/i386/start.S
index 4ec04bdfd7..d593c4de00 100644
--- a/sysdeps/i386/start.S
+++ b/sysdeps/i386/start.S
@@ -98,11 +98,10 @@ ENTRY (_start)
 	pushl main@GOT(%ebx)
 # else
 	/* Avoid relocation in static PIE since _start is called before
-	   it is relocated.  Don't use "leal main@GOTOFF(%ebx), %eax"
-	   since main may be in a shared object.  Linker will convert
-	   "movl main@GOT(%ebx), %eax" to "leal main@GOTOFF(%ebx), %eax"
+	   it is relocated.  This also avoid rely on linker optimization to
+	   transform 'movl main@GOT(%ebx), %eax' to 'leal main@GOTOFF(%ebx)'
 	   if main is defined locally.  */
-	movl main@GOT(%ebx), %eax
+	leal __wrap_main@GOTOFF(%ebx), %eax
 	pushl %eax
 # endif
 
@@ -130,6 +129,12 @@ ENTRY (_start)
 1:	movl	(%esp), %ebx
 	ret
 #endif
+
+#if defined PIC && !defined SHARED
+__wrap_main:
+	_CET_ENDBR
+	jmp	main
+#endif
 END (_start)
 
 /* To fulfill the System V/i386 ABI we need this symbol.  Yuck, it's so
-- 
2.34.1


             reply	other threads:[~2022-11-17 12:43 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-17 12:43 Adhemerval Zanella via Libc-alpha [this message]
2022-11-17 16:07 ` [PATCH] i386: Avoid avoid rely on linker optimization to avoid relocation H.J. Lu via Libc-alpha
2022-11-17 16:41   ` Adhemerval Zanella Netto via Libc-alpha
2022-11-17 16:52     ` H.J. Lu via Libc-alpha
2022-11-17 17:28       ` Adhemerval Zanella Netto via Libc-alpha
2022-11-17 18:01         ` H.J. Lu via Libc-alpha
2022-11-21 14:47 ` Andreas Schwab via Libc-alpha

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

  List information: https://www.gnu.org/software/libc/involved.html

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

  git send-email \
    --in-reply-to=20221117124317.2816607-1-adhemerval.zanella@linaro.org \
    --to=libc-alpha@sourceware.org \
    --cc=adhemerval.zanella@linaro.org \
    --cc=hjl.tools@gmail.com \
    --cc=maskray@google.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).