From patchwork Thu Jul 8 22:10:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fangrui Song X-Patchwork-Id: 44275 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1F5B2393A03C for ; Thu, 8 Jul 2021 22:11:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1F5B2393A03C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1625782269; bh=z7NxIRjjKsxdIhAuf27SmyTMYN3/6f16UHRgDPNkqaM=; h=Date:Subject:To:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=MUMYcWEG49vqqnrUuUPASwmLjf940HsnQRsZYfrmUaazb0mWvczJluURP4ROixwY7 7eotOgN6qgn1uZh+2JxbMLAsbzz19ilAY/AgHENMfadCWJEkKMLFMiOAQP7l3W6adv CUsKe5YIKVfgI1ejqINClJyD6bDSvwSoP44HYX6M= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qv1-xf49.google.com (mail-qv1-xf49.google.com [IPv6:2607:f8b0:4864:20::f49]) by sourceware.org (Postfix) with ESMTPS id 53AA93857C69 for ; Thu, 8 Jul 2021 22:10:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 53AA93857C69 Received: by mail-qv1-xf49.google.com with SMTP id q2-20020ad45ca20000b02902b1554c2318so5014028qvh.11 for ; Thu, 08 Jul 2021 15:10:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=z7NxIRjjKsxdIhAuf27SmyTMYN3/6f16UHRgDPNkqaM=; b=roxXXPdoVjI3jO9C9WgzB2Nk4TfaaaSCN45oz1GJUWKvEiMT5+GQAytkk69/JaT3Uz sDOoUBSPP1kWVSJziUJbHJg5IonsMv8CZN7phRTByB9Wx3B4vV8F4T4x4wTC9fj1J46w qFL3EXz17PrsjgdqshS3jd+SlRBrSAtssUcghybshm2oOfHOIsaM6K+IM5ebee5LUWjX ChSJaoc+VmmFyjG0HhOZH9yqVv5A3UV47LKV6Z9dOa3bYXR80yAK7W4YiRwXkkEGpMXn so44+S+oMZasyJzduDcCUmfdWp47OjGgoTllJP4vSHJPZF3rApeSriJNcJ89xUBOoj+w YkIQ== X-Gm-Message-State: AOAM530InxIXBEpvU0AHRYlnnsSIa8RoiENzMebN3dQYKH5CHyfXTuVz QSQAkxe/9H7dm3DxPiRuHIyaOiVtopAEaefByGDxMB3Ox2rZ7NCMFa9ObB0flo5HkAVYOEXHE9e YQv2rf66RMuJjPLGjn0Zyb0qMrdqBTW4nunSHchkttAUCmXPLbAaSc84dz1NqtGtAdWis X-Google-Smtp-Source: ABdhPJwTkplA0LlU+58NZ6tFrCiDxiSAo2h/wH+ThtEQfF5JQKO1tQB0rBVgbh6UG4zcVe9vhgw/AvQxsWyg X-Received: from maskray1.svl.corp.google.com ([2620:15c:2ce:200:6813:cff9:d2ed:7124]) (user=maskray job=sendgmr) by 2002:a05:6214:13d3:: with SMTP id cg19mr32529302qvb.50.1625782246782; Thu, 08 Jul 2021 15:10:46 -0700 (PDT) Date: Thu, 8 Jul 2021 15:10:32 -0700 Message-Id: <20210708221032.955550-1-maskray@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH] csu: Skip ARCH_SETUP_IREL if _dl_relocate_static_pie applied IRELATIVE relocations [BZ #27164] To: libc-alpha@sourceware.org X-Spam-Status: No, score=-19.8 required=5.0 tests=BAYES_00, DKIMWL_WL_MED, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Fangrui Song via Libc-alpha From: Fangrui Song Reply-To: Fangrui Song Cc: Fangrui Song Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" From: Siva Chandra Reddy For a static pie, _dl_relocate_static_pie applies IRELATIVE relocations so ARCH_SETUP_IREL should not apply relocations again. The code currently relies on ld -pie not defining __rela_iplt_start/__rela_iplt_end (they end up as 0 as unresolved undefined weak symbols). However, 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 re-apply the relocations in the range of [__rela_iplt_start, __rela_iplt_end), causing a segfault. Change _dl_relocate_static_pie to return an int, indicating whether the relocations have been applied. This makes the intention clearer and makes glibc buildable with LLD>=9.0 if we allow LLD at configure time. In addition, this enables a future simplification to GNU ld: we can drop a linker script difference between -no-pie and -pie. Co-authored-by: Fangrui Song --- csu/libc-start.c | 8 +++++--- csu/static-reloc.c | 3 ++- elf/dl-reloc-static-pie.c | 4 +++- sysdeps/generic/ldsodefs.h | 7 ++++--- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/csu/libc-start.c b/csu/libc-start.c index 5b5913e7bf..32a69c58a2 100644 --- a/csu/libc-start.c +++ b/csu/libc-start.c @@ -296,10 +296,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 relocs_applied = _dl_relocate_static_pie (); /* Perform IREL{,A} relocations. */ - ARCH_SETUP_IREL (); + if (!relocs_applied) + ARCH_SETUP_IREL (); /* The stack guard goes into the TCB, so initialize it early. */ ARCH_SETUP_TLS (); @@ -307,7 +308,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 (!relocs_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 176394de4d..a3996808f3 100644 --- a/sysdeps/generic/ldsodefs.h +++ b/sysdeps/generic/ldsodefs.h @@ -1200,14 +1200,15 @@ void __tls_init_tp (void) attribute_hidden; void __libc_setup_tls (void); # if ENABLE_STATIC_PIE -/* Relocate static executable with PIE. */ -extern void _dl_relocate_static_pie (void) attribute_hidden; +/* Relocate static executable with PIE. Returns 1 if relocations have + been applied. */ +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