From patchwork Thu Dec 7 10:32:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 81647 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 808903845BFA for ; Thu, 7 Dec 2023 10:33:50 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 60EA23858288 for ; Thu, 7 Dec 2023 10:32:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 60EA23858288 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 60EA23858288 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701945145; cv=none; b=j4hG+8jx7I4M1mAVk0imfOfCZofwc5K3nV8NMgv67YSbPUHZjYTQGOQDRZn5RLM+2SL/CArJOi6YD4QSA11Qmw0hAQW2MqyvZ5gpcLPN9LC2CChf8bPZEQw3mfKpF8h5rkBFuJJtMrmHGczJKThRGk4HtmVtrq6cIbBhf2QYp8k= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701945145; c=relaxed/simple; bh=9hVMj/2cH9+yTZSFxk+eEy7MCMJOPzVMlVGM7b+FKu0=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=cFmU1c2OwnQh8I773PiMvSUAZHHp6BocXce+Z4gwvHOqyobklshKXhx0LgXYR3Q9+/OBSSinJURm6jiaPSsxkKn81nzUn+4RHHkCi4dlsTnZ9d9ubRVi4lDIlVv09aPTGzGfx6TLQ+RuHxwQ/VkW7hDPwekahDj26E4JRpezkaA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701945143; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=+wSiPsVHScVp84EQKVSvXxpdzgwEc8BKQhEZ3jPnXpc=; b=W0ERw/dfrAhFSg13Wez/zoXQtTROuivIw+aXDCzNhPQG6M6JzrR5netGUHZnPo/AuzuZNn B9Jbp4TMxd/R44G3b7XtA+3NoROgIdIQj2b2fn4gO82vPZtfjzv3YfWjMeePQ02i8h1rL2 QRzmnsJNui6lBa361kf8b4/wQwQse1U= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-663-1OqNCA4qN0as8G5wXX_Whw-1; Thu, 07 Dec 2023 05:32:21 -0500 X-MC-Unique: 1OqNCA4qN0as8G5wXX_Whw-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C868A83BA85 for ; Thu, 7 Dec 2023 10:32:20 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.131]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 42A25492BC6 for ; Thu, 7 Dec 2023 10:32:20 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v3 21/32] elf: _dl_rtld_map should not exist in static builds In-Reply-To: Message-ID: <6515a7c0cae19a148a3eb745aeb9f309e77d73fe.1701944612.git.fweimer@redhat.com> References: X-From-Line: 6515a7c0cae19a148a3eb745aeb9f309e77d73fe Mon Sep 17 00:00:00 2001 Date: Thu, 07 Dec 2023 11:32:18 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.3 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-9.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_STOCKGEN, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.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 We have separate objects for SHARED and !SHARED, so there is no need to make the rtld link map equality check somehow work in static builds. Instead, hide the _dl_rtld_map reference in the new is_rtld_link_map function, and simply define that to return false for !SHARED because the static linked loader does not have a link map. --- elf/do-rel.h | 11 +---------- elf/dynamic-link.h | 2 +- sysdeps/arm/dl-machine.h | 11 +---------- sysdeps/generic/ldsodefs.h | 14 ++++++++++++++ sysdeps/mips/dl-machine.h | 16 +++------------- sysdeps/powerpc/powerpc64/dl-machine.h | 4 ++-- sysdeps/sh/dl-machine.h | 7 ++----- 7 files changed, 24 insertions(+), 41 deletions(-) diff --git a/elf/do-rel.h b/elf/do-rel.h index 8083cb4162..78b5ddfe87 100644 --- a/elf/do-rel.h +++ b/elf/do-rel.h @@ -102,16 +102,7 @@ elf_dynamic_do_Rel (struct link_map_private *map, struct r_scope_elem *scope[], else #endif { - /* This is defined in rtld.c, but nowhere in the static libc.a; make - the reference weak so static programs can still link. This - declaration cannot be done when compiling rtld.c (i.e. #ifdef - RTLD_BOOTSTRAP) because rtld.c contains the common defn for - _dl_rtld_map, which is incompatible with a weak decl in the same - file. */ -# ifndef SHARED - weak_extern (GL(dl_rtld_map)); -# endif - if (map != &GL(dl_rtld_map)) /* Already done in rtld itself. */ + if (!is_rtld_link_map (map)) /* Already done in rtld itself. */ # if !defined DO_RELA || defined ELF_MACHINE_REL_RELATIVE /* Rela platforms get the offset from r_addend and this must be copied in the relocation address. Therefore we can skip diff --git a/elf/dynamic-link.h b/elf/dynamic-link.h index 2f72240b6a..8f4d096182 100644 --- a/elf/dynamic-link.h +++ b/elf/dynamic-link.h @@ -190,7 +190,7 @@ elf_machine_lazy_rel (struct link_map_private *map, do { \ int edr_lazy = elf_machine_runtime_setup ((map), (scope), (lazy), \ (consider_profile)); \ - if (((map) != &GL(dl_rtld_map) || DO_RTLD_BOOTSTRAP)) \ + if ((!is_rtld_link_map (map) || DO_RTLD_BOOTSTRAP)) \ ELF_DYNAMIC_DO_RELR (map); \ ELF_DYNAMIC_DO_REL ((map), (scope), edr_lazy, skip_ifunc); \ ELF_DYNAMIC_DO_RELA ((map), (scope), edr_lazy, skip_ifunc); \ diff --git a/sysdeps/arm/dl-machine.h b/sysdeps/arm/dl-machine.h index b138596252..99bf9656b2 100644 --- a/sysdeps/arm/dl-machine.h +++ b/sysdeps/arm/dl-machine.h @@ -356,16 +356,7 @@ elf_machine_rel (struct link_map_private *map, struct r_scope_elem *scope[], Elf32_Addr x; } __attribute__ ((packed, may_alias)); # ifndef RTLD_BOOTSTRAP - /* This is defined in rtld.c, but nowhere in the static - libc.a; make the reference weak so static programs can - still link. This declaration cannot be done when - compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP) because - rtld.c contains the common defn for _dl_rtld_map, which - is incompatible with a weak decl in the same file. */ -# ifndef SHARED - weak_extern (_dl_rtld_map); -# endif - if (map == &GL(dl_rtld_map)) + if (is_rtld_link_map (map)) /* Undo the relocation done here during bootstrapping. Now we will relocate it anew, possibly using a binding found in the user program or a loaded library diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h index 9f2022d43e..c5d49cae62 100644 --- a/sysdeps/generic/ldsodefs.h +++ b/sysdeps/generic/ldsodefs.h @@ -406,8 +406,10 @@ struct rtld_global /* List of search directories. */ EXTERN struct r_search_path_elem *_dl_all_dirs; +#ifdef SHARED /* Structure describing the dynamic linker itself. */ EXTERN struct link_map_private _dl_rtld_map; +#endif #if !PTHREAD_IN_LIBC && defined SHARED \ && defined __rtld_lock_default_lock_recursive @@ -1326,6 +1328,18 @@ dl_init_static_tls (struct link_map_private *map) void __rtld_static_init (struct link_map_private *map) attribute_hidden; #endif +/* Returns true if MAP is the dynamic loader itself. Statically + linked binaries do not have a dynamic loader, so return false. */ +static inline bool +is_rtld_link_map (const struct link_map_private *map) +{ +#ifdef SHARED + return map == &GL (dl_rtld_map); +#else + return false; +#endif +} + /* Return true if the ld.so copy in this namespace is actually active and working. If false, the dl_open/dlfcn hooks have to be used to call into the outer dynamic linker (which happens after static diff --git a/sysdeps/mips/dl-machine.h b/sysdeps/mips/dl-machine.h index b7b1705f65..2eb01ca7cb 100644 --- a/sysdeps/mips/dl-machine.h +++ b/sysdeps/mips/dl-machine.h @@ -436,16 +436,6 @@ elf_machine_reloc (struct link_map_private *map, struct r_scope_elem *scope[], const unsigned long int r_type = ELFW(R_TYPE) (r_info); ElfW(Addr) *addr_field = (ElfW(Addr) *) reloc_addr; -#if !defined RTLD_BOOTSTRAP && !defined SHARED - /* This is defined in rtld.c, but nowhere in the static libc.a; - make the reference weak so static programs can still link. This - declaration cannot be done when compiling rtld.c (i.e. #ifdef - RTLD_BOOTSTRAP) because rtld.c contains the common defn for - _dl_rtld_map, which is incompatible with a weak decl in the same - file. */ - weak_extern (GL(dl_rtld_map)); -#endif - switch (r_type) { #if !defined (RTLD_BOOTSTRAP) @@ -534,7 +524,7 @@ elf_machine_reloc (struct link_map_private *map, struct r_scope_elem *scope[], though it's not ABI compliant. Some day we should bite the bullet and stop doing this. */ #ifndef RTLD_BOOTSTRAP - if (map != &GL(dl_rtld_map)) + if (!is_rtld_link_map (map)) #endif reloc_value += SYMBOL_ADDRESS (map, sym, true); } @@ -553,7 +543,7 @@ elf_machine_reloc (struct link_map_private *map, struct r_scope_elem *scope[], } else #ifndef RTLD_BOOTSTRAP - if (map != &GL(dl_rtld_map)) + if (!is_rtld_link_map (map)) #endif reloc_value += map->l_public.l_addr; @@ -752,7 +742,7 @@ elf_machine_got_rel (struct link_map_private *map, n = map->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val; /* The dynamic linker's local got entries have already been relocated. */ - if (map != &GL(dl_rtld_map)) + if (!is_rtld_link_map (map)) { /* got[0] is reserved. got[1] is also reserved for the dynamic object generated by gnu ld. Skip these reserved entries from relocation. */ diff --git a/sysdeps/powerpc/powerpc64/dl-machine.h b/sysdeps/powerpc/powerpc64/dl-machine.h index 6104d6ae9c..80754546b4 100644 --- a/sysdeps/powerpc/powerpc64/dl-machine.h +++ b/sysdeps/powerpc/powerpc64/dl-machine.h @@ -519,7 +519,7 @@ elf_machine_fixup_plt (struct link_map_private *map, lookup_t sym_map, if (finaladdr != 0 && map != sym_map && !sym_map->l_relocated #if !defined RTLD_BOOTSTRAP && defined SHARED /* Bootstrap map doesn't have l_relocated set for it. */ - && sym_map != &GL(dl_rtld_map) + && !is_rtld_link_map (sym_map) #endif ) offset = sym_map->l_public.l_addr; @@ -645,7 +645,7 @@ resolve_ifunc (Elf64_Addr value, if (map != sym_map # if !defined RTLD_BOOTSTRAP && defined SHARED /* Bootstrap map doesn't have l_relocated set for it. */ - && sym_map != &GL(dl_rtld_map) + && !is_rtld_link_map (sym_map) # endif && !sym_map->l_relocated) { diff --git a/sysdeps/sh/dl-machine.h b/sysdeps/sh/dl-machine.h index 786cd00cab..f76254cca8 100644 --- a/sysdeps/sh/dl-machine.h +++ b/sysdeps/sh/dl-machine.h @@ -285,7 +285,7 @@ elf_machine_rela (struct link_map_private *map, struct r_scope_elem *scope[], if (__glibc_unlikely (r_type == R_SH_RELATIVE)) { #ifndef RTLD_BOOTSTRAP - if (map != &GL(dl_rtld_map)) /* Already done in rtld itself. */ + if (!is_rtld_link_map (map)) /* Already done in rtld itself. */ #endif { if (reloc->r_addend) @@ -388,10 +388,7 @@ elf_machine_rela (struct link_map_private *map, struct r_scope_elem *scope[], compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the common defn for _dl_rtld_map, which is incompatible with a weak decl in the same file. */ -# ifndef SHARED - weak_extern (_dl_rtld_map); -# endif - if (map == &GL(dl_rtld_map)) + if (is_rtld_link_map (map)) /* Undo the relocation done here during bootstrapping. Now we will relocate it anew, possibly using a binding found in the user program or a loaded library