From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS3215 2.6.0.0/16 X-Spam-Status: No, score=-4.2 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 3EF411F8C6 for ; Thu, 8 Jul 2021 23:28:33 +0000 (UTC) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 73D303AAA09E for ; Thu, 8 Jul 2021 23:28:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 73D303AAA09E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1625786912; bh=RNw2lugoEDRbhmT9nbwv9genPObv4SQmUeHKLoQP0+Y=; h=References:In-Reply-To:Date:Subject:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=wx0tbKZTqeg2GfpIkjyERJdLXf96AQVL1C3AS+sBPXUOqgtHWSoJ7yFw9tGR/RP/t botRLx5dXgYcRkBloY6/M7aPMQCvtWxPScUm9qQrf/0ry7fwkFskgE7gAtcL01xj47 XQJxwgYsYJqQAWx1xipNy2TGqjiksUjlHpWZtJPk= Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by sourceware.org (Postfix) with ESMTPS id A284A3858018 for ; Thu, 8 Jul 2021 23:28:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A284A3858018 Received: by mail-pf1-x429.google.com with SMTP id j199so7038617pfd.7 for ; Thu, 08 Jul 2021 16:28:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=RNw2lugoEDRbhmT9nbwv9genPObv4SQmUeHKLoQP0+Y=; b=fPfxVgEt7XcNW5TRp/4cH1FK0JIOeTbXWkYRcL22yGHMg70yUkc5Wx9tVtNYw5ECGZ DNugj6qHSuP1cjiCbysK349E+UHGdQUHums+qX9Guaaq8DDqpdVO+lrxwX04/+VGbdKC iTnB19nct3FvBZWxHpuaQSi0vOzd+7PrKPHlPs6Z3jC3ittaz71eGFWAezJgbavo9NhZ 4iyGMlPJTcCp0kT+vrVmiWSsizvUsSrvsn9KiIOBft4kSfcXZCSHLJh4B/QoNtiHxbx5 crKch6fuo/FEhi3kCSWIoqsJBL125iblfhFiRNTd9kyNrPuVho1vXUXEZuM2wNLnIWQT vhPQ== X-Gm-Message-State: AOAM530378sJZa9GJTn7qQT+kQZ0U4mWE6K5cA3/Vvwvb9SCUkyp1fQY 3NkyGz9aP1P34Wm9ccqnG0YsG8KUmBIbHsVGdhQ= X-Google-Smtp-Source: ABdhPJwZ6b6mEFuqfVBSfNO2GxpMiSApFOuw8nxZzAx/42LNVlNl2nL4I5eP/1GXoaiMvQ2lAckQTaG/ld9Zz9sLJ1Q= X-Received: by 2002:a05:6a00:10c5:b029:327:1c58:8e0d with SMTP id d5-20020a056a0010c5b02903271c588e0dmr7747360pfu.57.1625786890799; Thu, 08 Jul 2021 16:28:10 -0700 (PDT) MIME-Version: 1.0 References: <20210708221032.955550-1-maskray@google.com> In-Reply-To: <20210708221032.955550-1-maskray@google.com> Date: Thu, 8 Jul 2021 16:27:35 -0700 Message-ID: Subject: Re: [PATCH] csu: Skip ARCH_SETUP_IREL if _dl_relocate_static_pie applied IRELATIVE relocations [BZ #27164] To: Fangrui Song Content-Type: text/plain; charset="UTF-8" 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: , From: "H.J. Lu via Libc-alpha" Reply-To: "H.J. Lu" Cc: GNU C Library Errors-To: libc-alpha-bounces+e=80x24.org@sourceware.org Sender: "Libc-alpha" On Thu, Jul 8, 2021 at 3:10 PM Fangrui Song wrote: > > 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 This is a bug in LLD. The fix is simple. Please fix it. > 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 > > -- > 2.32.0.93.g670b81a890-goog > -- H.J.