From patchwork Wed Mar 18 19:34:15 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 131924 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 85F014C31806 for ; Wed, 18 Mar 2026 19:39:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 85F014C31806 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=b+cwVwM3 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-dl1-x122b.google.com (mail-dl1-x122b.google.com [IPv6:2607:f8b0:4864:20::122b]) by sourceware.org (Postfix) with ESMTPS id BEB6F4C3181B for ; Wed, 18 Mar 2026 19:35:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BEB6F4C3181B Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org BEB6F4C3181B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::122b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1773862511; cv=none; b=euBP69230PhUH3Y7afPjKdkskPq/MFz1PNb3Ey0chrIqk7uWBfIDIHntw/en5mF2TdAanwoq/yQqs9NOC6YwJgtAvBQbrSBtnBNZkj+cl1dVvx4hwLkmbH7t0RC3w/f3yfZ4yT/hYf939ZUVUNI/jhODepgbDZUecD5OIYQPLUI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1773862511; c=relaxed/simple; bh=VGDoQ1L9FmrhaLO0mQ9+B4huwmJm2X+3cdZhNcMWsyE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=sZcxasHM/nqzLRjygPFjZypyfsHIxxqY7pdBOnnmH506UqHZ340BPvGdvb9j+GMnVV9/FSnqJTLLyW+kNd7Bap7TcSspZrT3CUB+Hb389C8MBjMCVlIMgq5BJXmTjskW/F0/fhYzsdqb1WPl3HV9LTysC+ct7eTbowD7YQeAWeM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BEB6F4C3181B Received: by mail-dl1-x122b.google.com with SMTP id a92af1059eb24-126ea4b77adso286291c88.1 for ; Wed, 18 Mar 2026 12:35:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1773862510; x=1774467310; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=fCGnXi5bNDJkNd6r9ndXX+wrMhl5VJ+V6iZerRFwyqI=; b=b+cwVwM3ZGri4zG3j0Z6SrHil/hgd2JIab8mEw+UEvet3YEPBkL38PTcHN0DOH0Nhj TDgOcVRoXhz5/qtunIco8bIJRVpKRvIEF4s8FHVerdVsZhYQQ6HLj7RRcPahAZxkTABA wn84a34yN3yleQWVLuMV0Zp1HMx76stBlV0UvQc2M7ZEpwBYQDjrfGdno3l9jI90Pbe6 /9EzCR8Pm0zRoddU+pQVXKzuWdT4h2kVB/vg8gsrrmR0e1RDGMPu+Vmv9rWqkVCtJZEY 5kYB7mMHNcYeZsYIfxVkKGAB2mA+3p9cA2aEGiTIMDdozM6vkOqkU2CocbovTHfDMx8q MtcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773862510; x=1774467310; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to;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a2 with SMTP id a92af1059eb24-129a7160dfamr2336061c88.29.1773862510138; Wed, 18 Mar 2026 12:35:10 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:f5a4:2159:38c8:c951:d029]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-129b4147522sm4238937c88.15.2026.03.18.12.35.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Mar 2026 12:35:09 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Florian Weimer , Samuel Thibault Subject: [PATCH v8 4/6] elf: Allow RPATH/RUNPATH for static-pie (BZ 33326) Date: Wed, 18 Mar 2026 16:34:15 -0300 Message-ID: <20260318193454.2466865-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260318193454.2466865-1-adhemerval.zanella@linaro.org> References: <20260318193454.2466865-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org The initial static-pie support (commit 9d7a3741c9e59eba87fb) reused ld.so ELF parsing logic, even though RPATH/RUNPATH should not appear in the static-pie bootstrap. With static PIE, RPATH/RUNPATH on the loader typically indicates a toolchain misconfiguration. However, for static PIE, the presence of RPATH/RUNPATH has no impact because these binaries do not use dynamic linking at runtime. Static binaries do not support rpath because they lack dynamic sections, and adding static-pie support affects only the dlopen function. If static dlopen support is removed, this change has no effect. This change also simplifies elf_get_dynamic_info and removes a difference between dynamic and static binaries. Tested on aarch64-linux-gnu and x86_64-linux-gnu. Reviewed-by: Florian Weimer --- elf/Makefile | 11 ++++++++ elf/dl-load.c | 2 +- elf/dl-reloc-static-pie.c | 2 +- elf/get-dynamic-info.h | 5 ++-- elf/rtld.c | 4 +-- elf/setup-vdso.h | 2 +- elf/tst-pie-rpath-mod.c | 19 ++++++++++++++ elf/tst-pie-rpath-static.c | 52 ++++++++++++++++++++++++++++++++++++++ 8 files changed, 89 insertions(+), 8 deletions(-) create mode 100644 elf/tst-pie-rpath-mod.c create mode 100644 elf/tst-pie-rpath-static.c diff --git a/elf/Makefile b/elf/Makefile index 29d771ef7a..986ef6b388 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -1201,7 +1201,11 @@ tests += \ # tests tests-static += \ tst-pie-address-static \ + tst-pie-rpath-static \ # tests-static +modules-names += \ + tst-pie-rpath-mod \ + # modules-names ifeq (yes,$(aligned-65536)) tests += tst-pie-bss-static tests-static += tst-pie-bss-static @@ -3569,3 +3573,10 @@ $(objpfx)tst-assert-startup-static.out: $(objpfx)tst-assert-startup-static grep -q 'Fatal glibc error: tst-assert-startup-static' $@ \ && grep -q '^status: 134$$' $@; \ $(evaluate-test) + +LDFLAGS-tst-pie-rpath-static += -Wl,-rpath,\$$ORIGIN/tst-pie-rpath-static-subdir +CFLAGS-tst-pie-rpath-static.c += -DPFX=\"$(objpfx)\" +ifeq (no,$(build-hardcoded-path-in-tests)) +LDFLAGS-tst-pie-rpath-mod.so += -Wl,-rpath,$(rpath-link) +endif +$(objpfx)tst-pie-rpath-static.out: $(objpfx)tst-pie-rpath-mod.so diff --git a/elf/dl-load.c b/elf/dl-load.c index 7355eef8e7..c5ea40fa4a 100644 --- a/elf/dl-load.c +++ b/elf/dl-load.c @@ -1269,7 +1269,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, if (l->l_ld != NULL) l->l_ld = (ElfW(Dyn) *) ((ElfW(Addr)) l->l_ld + l->l_addr); - elf_get_dynamic_info (l, false, false); + elf_get_dynamic_info (l, 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 bdff2b5ee2..22acd9bb4f 100644 --- a/elf/dl-reloc-static-pie.c +++ b/elf/dl-reloc-static-pie.c @@ -69,7 +69,7 @@ _dl_relocate_static_pie (void) /* Read our own dynamic section and fill in the info array. */ main_map->l_ld = ((void *) l_addr + elf_machine_dynamic ()); - elf_get_dynamic_info (main_map, false, true); + elf_get_dynamic_info (main_map, false); # 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 46faa34acf..9591d00d59 100644 --- a/elf/get-dynamic-info.h +++ b/elf/get-dynamic-info.h @@ -26,8 +26,7 @@ #include static inline void __attribute__ ((unused, always_inline)) -elf_get_dynamic_info (struct link_map *l, bool bootstrap, - bool static_pie_bootstrap) +elf_get_dynamic_info (struct link_map *l, bool bootstrap) { #if __ELF_NATIVE_CLASS == 32 typedef Elf32_Word d_tag_utype; @@ -128,7 +127,7 @@ elf_get_dynamic_info (struct link_map *l, bool bootstrap, #endif if (info[DT_RELR] != NULL) assert (info[DT_RELRENT]->d_un.d_val == sizeof (ElfW(Relr))); - if (bootstrap || static_pie_bootstrap) + if (bootstrap) { assert (info[DT_RUNPATH] == NULL); assert (info[DT_RPATH] == NULL); diff --git a/elf/rtld.c b/elf/rtld.c index e1c2105ba6..783db6eb5c 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 (); bootstrap_map.l_ld_readonly = DL_RO_DYN_SECTION; - elf_get_dynamic_info (&bootstrap_map, true, false); + elf_get_dynamic_info (&bootstrap_map, true); #if NO_TLS_OFFSET != 0 bootstrap_map.l_tls_offset = NO_TLS_OFFSET; @@ -1706,7 +1706,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, false, false); + elf_get_dynamic_info (main_map, 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 0dba7072d5..58b7712046 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_addr = l->l_map_start - l->l_addr; l->l_map_end += l->l_addr; l->l_ld = (void *) ((ElfW(Addr)) l->l_ld + l->l_addr); - elf_get_dynamic_info (l, false, false); + elf_get_dynamic_info (l, false); _dl_setup_hash (l); l->l_relocated = 1; diff --git a/elf/tst-pie-rpath-mod.c b/elf/tst-pie-rpath-mod.c new file mode 100644 index 0000000000..719eb41e09 --- /dev/null +++ b/elf/tst-pie-rpath-mod.c @@ -0,0 +1,19 @@ +/* Check if RPATH/RUNPATH is allowed for static-pie. + Copyright (C) 2026 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +int foo (void) { return 42; } diff --git a/elf/tst-pie-rpath-static.c b/elf/tst-pie-rpath-static.c new file mode 100644 index 0000000000..02fb05d2e6 --- /dev/null +++ b/elf/tst-pie-rpath-static.c @@ -0,0 +1,52 @@ +/* Check if RPATH/RUNPATH is allowed for static-pie. + Copyright (C) 2026 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include + +#define LIBNAME "tst-pie-rpath-mod.so" +#define TESTSUBDIR PFX "tst-pie-rpath-static-subdir" +#define LIBPATH TESTSUBDIR "/" LIBNAME + +static void +do_prepare (int argc, char **argv) +{ + xmkdir (TESTSUBDIR, 0777); + add_temp_file (TESTSUBDIR); + + support_copy_file (PFX "/" LIBNAME, LIBPATH); + add_temp_file (LIBPATH); +} +#define PREPARE do_prepare + +static int +do_test (void) +{ + void *h = xdlopen (LIBNAME, RTLD_NOW); + int (*foo)(void) = xdlsym (h, "foo"); + TEST_COMPARE (foo (), 42); + xdlclose (h); + + return 0; +} + +#include