From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7814) id CBC0C389366A; Mon, 1 Feb 2021 17:23:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CBC0C389366A Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Fangrui Song To: glibc-cvs@sourceware.org Subject: [glibc/maskray/lld] csu: Skip ARCH_SETUP_IREL if _dl_relocate_static_pie applied IRELATIVE relocations X-Act-Checkin: glibc X-Git-Author: Siva Chandra Reddy X-Git-Refname: refs/heads/maskray/lld X-Git-Oldrev: f6cdc9ce47364120f5ab77d60a5fc38ee3ba72e2 X-Git-Newrev: 0d59a02ab21b9770805a70783511b3d46b577af1 Message-Id: <20210201172316.CBC0C389366A@sourceware.org> Date: Mon, 1 Feb 2021 17:23:16 +0000 (GMT) X-BeenThere: glibc-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Glibc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 01 Feb 2021 17:23:16 -0000 https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=0d59a02ab21b9770805a70783511b3d46b577af1 commit 0d59a02ab21b9770805a70783511b3d46b577af1 Author: Siva Chandra Reddy Date: Fri Jan 29 10:42:50 2021 -0800 csu: Skip ARCH_SETUP_IREL if _dl_relocate_static_pie applied IRELATIVE relocations For static pie, _dl_relocate_static_pie applies IRELATIVE relocations so ARCH_SETUP_IREL should not apply relocations again. LLD defines __rela_iplt_start/__rela_iplt_end regardless of -no-pie or -pie, so in an LLD linked static pie, ARCH_SETUP_IREL would otherwise apply the relocations in the range of [__rela_iplt_start, __rela_iplt_end). Co-authored-by: Fangrui Song Diff: --- csu/libc-start.c | 8 +++++--- csu/static-reloc.c | 3 ++- elf/dl-reloc-static-pie.c | 4 +++- sysdeps/generic/ldsodefs.h | 4 ++-- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/csu/libc-start.c b/csu/libc-start.c index feb0d7ce11..44d9d875d2 100644 --- a/csu/libc-start.c +++ b/csu/libc-start.c @@ -200,10 +200,11 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL), /* Do static pie self relocation after tunables and cpu features are setup for ifunc resolvers. Before this point relocations must be avoided. */ - _dl_relocate_static_pie (); + int irel_applied = _dl_relocate_static_pie (); /* Perform IREL{,A} relocations. */ - ARCH_SETUP_IREL (); + if (!irel_applied) + ARCH_SETUP_IREL (); /* The stack guard goes into the TCB, so initialize it early. */ ARCH_SETUP_TLS (); @@ -211,7 +212,8 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL), /* In some architectures, IREL{,A} relocations happen after TLS setup in order to let IFUNC resolvers benefit from TCB information, e.g. powerpc's hwcap and platform fields available in the TCB. */ - ARCH_APPLY_IREL (); + if (!irel_applied) + ARCH_APPLY_IREL (); /* Set up the stack checker's canary. */ uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard (_dl_random); diff --git a/csu/static-reloc.c b/csu/static-reloc.c index 972c524f28..9046d9f6a3 100644 --- a/csu/static-reloc.c +++ b/csu/static-reloc.c @@ -19,8 +19,9 @@ #if ENABLE_STATIC_PIE #include -void +int _dl_relocate_static_pie (void) { + return 0; } #endif diff --git a/elf/dl-reloc-static-pie.c b/elf/dl-reloc-static-pie.c index d5bd2f31e9..b707ef4bf1 100644 --- a/elf/dl-reloc-static-pie.c +++ b/elf/dl-reloc-static-pie.c @@ -25,7 +25,7 @@ /* Relocate static executable with PIE. */ -void +int _dl_relocate_static_pie (void) { struct link_map *main_map = _dl_get_dl_main_map (); @@ -66,5 +66,7 @@ _dl_relocate_static_pie (void) with the run-time address of the r_debug structure */ main_map->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r; # endif + + return 1; } #endif diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h index aab7245e93..a0b1e3a34e 100644 --- a/sysdeps/generic/ldsodefs.h +++ b/sysdeps/generic/ldsodefs.h @@ -1148,13 +1148,13 @@ void __libc_setup_tls (void); # if ENABLE_STATIC_PIE /* Relocate static executable with PIE. */ -extern void _dl_relocate_static_pie (void) attribute_hidden; +extern int _dl_relocate_static_pie (void) attribute_hidden; /* Get a pointer to _dl_main_map. */ extern struct link_map * _dl_get_dl_main_map (void) __attribute__ ((visibility ("hidden"))); # else -# define _dl_relocate_static_pie() +# define _dl_relocate_static_pie() 0 # endif #endif