From patchwork Fri Oct 15 17:41:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 46290 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 0FFB63857804 for ; Fri, 15 Oct 2021 17:41:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0FFB63857804 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1634319713; bh=wKFfzgBIzzoCHrSH/eGlBimjXLhu63YFygeEMWjDTb4=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=Dz66vkDBh38OKvLyJGUl6r8bmLp/Q7n13ITWwGsxsyAk6s13N90wZr8xjQw0898ej MkKzhuucqAqX+2gLK4rYqtVq/XaETBDR3umTS72WkrFZoo7I5hZ/8TCflXD2SzNqyb MT7h16VTfQfNU960PyYQi4BlmI859PNl156jmwrI= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ua1-x936.google.com (mail-ua1-x936.google.com [IPv6:2607:f8b0:4864:20::936]) by sourceware.org (Postfix) with ESMTPS id F0CF23858C60 for ; Fri, 15 Oct 2021 17:41:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org F0CF23858C60 Received: by mail-ua1-x936.google.com with SMTP id f3so19531883uap.6 for ; Fri, 15 Oct 2021 10:41:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=wKFfzgBIzzoCHrSH/eGlBimjXLhu63YFygeEMWjDTb4=; b=gHCPdWGjH3slgWFlY0QeIpiDXLCCsVejLkR+638iRy+R2EySx8raCCJlRduBy03l4H kKWTHVYWZJONONyERcXk7kJfsJvrMu9s45stYwMeawXVw+INiYPCx/kx+FwFjRji0ap5 vMOvVvNwRoS5LXWRC+6kvzXtqTj79Z3uFUZX4sDsBN1cPc6H4Ou7EB++OPAw7G28ct0w vzT/UmCPeMh+83jUfWs/wmLP1dt3tSfmLuAk6GSgl3NBoSh63fKfU078/jiGZxQNSZzt Q+6wftQ3p7neQ5X87/rlPs0atS2jn+4qskGydRR1JvbAw6l2JLxOvSl6i3hMB560KSkH LjwQ== X-Gm-Message-State: AOAM530Dpp++zIWnchfA2Ldu4VgLwuskOL5cHqKSsNzXoQ+c9z277BS0 FtDiOAqz4LkKwHslHseasuxGcaIeMdB2hg== X-Google-Smtp-Source: ABdhPJxjfRJWdGAceD1+zoxLqLwqx01/1voeNjIiAl/8cth4bIKD2zZstKaPo9HARiMY50BNAaT46g== X-Received: by 2002:a67:d114:: with SMTP id u20mr15721979vsi.3.1634319687343; Fri, 15 Oct 2021 10:41:27 -0700 (PDT) Received: from birita.. ([2804:431:c7ca:c6c7:f05e:9652:ab99:7fa2]) by smtp.gmail.com with ESMTPSA id t3sm4267688vka.35.2021.10.15.10.41.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Oct 2021 10:41:27 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH] elf: Fix elf_get_dynamic_info() for bootstrap Date: Fri, 15 Oct 2021 14:41:23 -0300 Message-Id: <20211015174123.2305597-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, URIBL_BLACK 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: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Cc: Szabolcs Nagy , Manuel Lauss Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" THe d6d89608ac8c broke powerpc for --enable-bind-now because it turned out that different than patch assumption rtld elf_get_dynamic_info() does require to handle RTLD_BOOTSTRAP to avoid DT_FLAGS and DT_RUNPATH (more specially the GLRO usage which is not reallocate yet). This patch fixes by passing two arguments to elf_get_dynamic_info() to inform that by rtld (bootstrap) or static pie initialization (static_pie_bootstrap). I think using explicit argument is way more clear and burried C preprocessor, and compiler should remove the dead code. I checked on x86_64 and i686 with default options, --enable-bind-now, and --enable-bind-now and --enable--static-pie. I also check on aarch64, armhf, powerpc64, and powerpc with default and --enable-bind-now. --- elf/dl-load.c | 2 +- elf/dl-reloc-static-pie.c | 3 +- elf/get-dynamic-info.h | 95 +++++++++++++++++++++++---------------- elf/rtld.c | 4 +- elf/setup-vdso.h | 2 +- 5 files changed, 61 insertions(+), 45 deletions(-) diff --git a/elf/dl-load.c b/elf/dl-load.c index 18d3e8fe64..a1f1682188 100644 --- a/elf/dl-load.c +++ b/elf/dl-load.c @@ -1298,7 +1298,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, else l->l_ld = (ElfW(Dyn) *) ((ElfW(Addr)) l->l_ld + l->l_addr); - elf_get_dynamic_info (l); + elf_get_dynamic_info (l, false, false); /* Make sure we are not dlopen'ing an object that has the DF_1_NOOPEN flag set, or a PIE object. */ diff --git a/elf/dl-reloc-static-pie.c b/elf/dl-reloc-static-pie.c index 40a031f2e9..5b85df8a2e 100644 --- a/elf/dl-reloc-static-pie.c +++ b/elf/dl-reloc-static-pie.c @@ -25,7 +25,6 @@ #include -#define STATIC_PIE_BOOTSTRAP #define RESOLVE_MAP(map, scope, sym, version, flags) map #include "dynamic-link.h" #include "get-dynamic-info.h" @@ -52,7 +51,7 @@ _dl_relocate_static_pie (void) break; } - elf_get_dynamic_info (main_map); + elf_get_dynamic_info (main_map, false, true); # ifdef ELF_MACHINE_BEFORE_RTLD_RELOC ELF_MACHINE_BEFORE_RTLD_RELOC (main_map, main_map->l_info); diff --git a/elf/get-dynamic-info.h b/elf/get-dynamic-info.h index 1ac0663d1f..f63e07dc6d 100644 --- a/elf/get-dynamic-info.h +++ b/elf/get-dynamic-info.h @@ -26,7 +26,8 @@ #include static inline void __attribute__ ((unused, always_inline)) -elf_get_dynamic_info (struct link_map *l) +elf_get_dynamic_info (struct link_map *l, bool bootstrap, + bool static_pie_bootstrap) { #if __ELF_NATIVE_CLASS == 32 typedef Elf32_Word d_tag_utype; @@ -35,7 +36,7 @@ elf_get_dynamic_info (struct link_map *l) #endif #ifndef STATIC_PIE_BOOTSTRAP - if (l->l_ld == NULL) + if (!bootstrap && l->l_ld == NULL) return; #endif @@ -112,47 +113,63 @@ elf_get_dynamic_info (struct link_map *l) if (info[DT_REL] != NULL) assert (info[DT_RELENT]->d_un.d_val == sizeof (ElfW(Rel))); #endif -#ifdef STATIC_PIE_BOOTSTRAP - assert (info[DT_RUNPATH] == NULL); - assert (info[DT_RPATH] == NULL); -#endif - if (info[DT_FLAGS] != NULL) + if (bootstrap || static_pie_bootstrap) { - /* Flags are used. Translate to the old form where available. - Since these l_info entries are only tested for NULL pointers it - is ok if they point to the DT_FLAGS entry. */ - l->l_flags = info[DT_FLAGS]->d_un.d_val; - - if (l->l_flags & DF_SYMBOLIC) - info[DT_SYMBOLIC] = info[DT_FLAGS]; - if (l->l_flags & DF_TEXTREL) - info[DT_TEXTREL] = info[DT_FLAGS]; - if (l->l_flags & DF_BIND_NOW) - info[DT_BIND_NOW] = info[DT_FLAGS]; + assert (info[DT_RUNPATH] == NULL); + assert (info[DT_RPATH] == NULL); } - if (info[VERSYMIDX (DT_FLAGS_1)] != NULL) + if (bootstrap) { - l->l_flags_1 = info[VERSYMIDX (DT_FLAGS_1)]->d_un.d_val; - if (l->l_flags_1 & DF_1_NODELETE) - l->l_nodelete_pending = true; - - /* Only DT_1_SUPPORTED_MASK bits are supported, and we would like - to assert this, but we can't. Users have been setting - unsupported DF_1_* flags for a long time and glibc has ignored - them. Therefore to avoid breaking existing applications the - best we can do is add a warning during debugging with the - intent of notifying the user of the problem. */ - if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0) - && l->l_flags_1 & ~DT_1_SUPPORTED_MASK) - _dl_debug_printf ("\nWARNING: Unsupported flag value(s) of 0x%x in DT_FLAGS_1.\n", - l->l_flags_1 & ~DT_1_SUPPORTED_MASK); - - if (l->l_flags_1 & DF_1_NOW) - info[DT_BIND_NOW] = info[VERSYMIDX (DT_FLAGS_1)]; + /* Only the bind now flags are allowed. */ + assert (info[VERSYMIDX (DT_FLAGS_1)] == NULL + || (info[VERSYMIDX (DT_FLAGS_1)]->d_un.d_val & ~DF_1_NOW) == 0); + /* Flags must not be set for ld.so. */ + assert (info[DT_FLAGS] == NULL + || (info[DT_FLAGS]->d_un.d_val & ~DF_BIND_NOW) == 0); } - if (info[DT_RUNPATH] != NULL) - /* If both RUNPATH and RPATH are given, the latter is ignored. */ - info[DT_RPATH] = NULL; + else + { + if (info[DT_FLAGS] != NULL) + { + /* Flags are used. Translate to the old form where available. + Since these l_info entries are only tested for NULL pointers it + is ok if they point to the DT_FLAGS entry. */ + l->l_flags = info[DT_FLAGS]->d_un.d_val; + + if (l->l_flags & DF_SYMBOLIC) + info[DT_SYMBOLIC] = info[DT_FLAGS]; + if (l->l_flags & DF_TEXTREL) + info[DT_TEXTREL] = info[DT_FLAGS]; + if (l->l_flags & DF_BIND_NOW) + info[DT_BIND_NOW] = info[DT_FLAGS]; + } + + if (info[VERSYMIDX (DT_FLAGS_1)] != NULL) + { + l->l_flags_1 = info[VERSYMIDX (DT_FLAGS_1)]->d_un.d_val; + if (l->l_flags_1 & DF_1_NODELETE) + l->l_nodelete_pending = true; + + /* Only DT_1_SUPPORTED_MASK bits are supported, and we would like + to assert this, but we can't. Users have been setting + unsupported DF_1_* flags for a long time and glibc has ignored + them. Therefore to avoid breaking existing applications the + best we can do is add a warning during debugging with the + intent of notifying the user of the problem. */ + if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0) + && l->l_flags_1 & ~DT_1_SUPPORTED_MASK) + _dl_debug_printf ("\nWARNING: Unsupported flag value(s) of 0x%x " + "in DT_FLAGS_1.\n", + l->l_flags_1 & ~DT_1_SUPPORTED_MASK); + + if (l->l_flags_1 & DF_1_NOW) + info[DT_BIND_NOW] = info[VERSYMIDX (DT_FLAGS_1)]; + } + + if (info[DT_RUNPATH] != NULL) + /* If both RUNPATH and RPATH are given, the latter is ignored. */ + info[DT_RPATH] = NULL; + } } #endif diff --git a/elf/rtld.c b/elf/rtld.c index 6cfb7cf672..c66a1d00bc 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -547,7 +547,7 @@ _dl_start (void *arg) /* Read our own dynamic section and fill in the info array. */ bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic (); - elf_get_dynamic_info (&bootstrap_map); + elf_get_dynamic_info (&bootstrap_map, true, false); #if NO_TLS_OFFSET != 0 bootstrap_map.l_tls_offset = NO_TLS_OFFSET; @@ -1615,7 +1615,7 @@ dl_main (const ElfW(Phdr) *phdr, if (! rtld_is_main) { /* Extract the contents of the dynamic section for easy access. */ - elf_get_dynamic_info (main_map); + elf_get_dynamic_info (main_map, false, false); /* If the main map is libc.so, update the base namespace to refer to this map. If libc.so is loaded later, this happens diff --git a/elf/setup-vdso.h b/elf/setup-vdso.h index f44748bc98..3f20578046 100644 --- a/elf/setup-vdso.h +++ b/elf/setup-vdso.h @@ -64,7 +64,7 @@ setup_vdso (struct link_map *main_map __attribute__ ((unused)), l->l_map_end += l->l_addr; l->l_text_end += l->l_addr; l->l_ld = (void *) ((ElfW(Addr)) l->l_ld + l->l_addr); - elf_get_dynamic_info (l); + elf_get_dynamic_info (l, false, false); _dl_setup_hash (l); l->l_relocated = 1;