From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1791) id 2829E3853D65; Mon, 21 Nov 2022 14:36:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2829E3853D65 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1669041376; bh=6U+a9UDbB91I/buytRURWYUI32ZM18l2Iqio1fn7gHI=; h=From:To:Subject:Date:From; b=H3dXw9QG/ot0Hd7aCeon5muAzaha60bkWkjMuo8fnxbbSdiHE7g5N3NVioY+0J9a5 e001d8kF3720kvzHhaw2vO0zALIjfhamzR9RTsZi9gG/RsDOdHIu+Pp5lt+jb7RKCM W4chhdc3vD+ZrUn6OV3xj5joxG4F+wSxcdkj1AyQ= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Adhemerval Zanella To: glibc-cvs@sourceware.org Subject: [glibc] i386: Avoid rely on linker optimization to avoid relocation X-Act-Checkin: glibc X-Git-Author: Adhemerval Zanella Netto X-Git-Refname: refs/heads/master X-Git-Oldrev: eb4181e9f4a512de37dad4ba623c921671584dea X-Git-Newrev: 59aa41585f668b70b86a8e2617057da08f3291cc Message-Id: <20221121143616.2829E3853D65@sourceware.org> Date: Mon, 21 Nov 2022 14:36:16 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=59aa41585f668b70b86a8e2617057da08f3291cc commit 59aa41585f668b70b86a8e2617057da08f3291cc Author: Adhemerval Zanella Netto Date: Thu Nov 17 15:13:08 2022 -0300 i386: Avoid rely on linker optimization to avoid relocation 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. Reviewed-by: Fangrui Song Diff: --- 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..23e4f2b012 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@PLT +#endif END (_start) /* To fulfill the System V/i386 ABI we need this symbol. Yuck, it's so