From patchwork Thu Dec 7 10:32:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 81648 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 39E8F3845BC7 for ; Thu, 7 Dec 2023 10:33:57 +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.133.124]) by sourceware.org (Postfix) with ESMTPS id 75202385DC1A for ; Thu, 7 Dec 2023 10:32:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 75202385DC1A 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 75202385DC1A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701945142; cv=none; b=c+Ynan8vG7inN8lroXIHNkBYE8WGFDa5hwIt4AJkj1npZAM4Ifgd0wO5+TFO2Hyl/D9bi3oqtpcpoTahpKLO5yv+BTUq43nwtOOPCAAw2YTNudTF52pp6QK+fCE5M6vLCuRH6638oQqjM+aKm48AoqZq+B/SQMj26CwtRx2acU0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701945142; c=relaxed/simple; bh=1ob2EZ5vHs2bdAsPf4horizEXBjUA4C7m0+LIc8UTKA=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=tWH30yDXpnnKaR+MeaLc5tTeYuFDX/uaajk7lL1ZeZw1tRQPrMM2W6mgTFh2IE8Wt+iJhmWbK7hsN8sufb3aOrmq6MlkRQuO4YkiwGUgsCRZewfk72Ibi0CQdPYpCVAflJ0BTTmduNM054YAgluCwhR7Ze11AxRIivDOGpQxQwk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701945140; 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=dxBjposVACtVSX0ac+bXhxQHAGh23q6sssTS3uQq15M=; b=CxYETBb440wbOSXKnslKgILdQ7NaY3LZIxtQeiKTEPaA3tDVSNN4W5xqYVuk3KGpHX9LEj lfNs7sfQwckfTIqJ9ASuZRTZ06zjydsTwOdwD5Yg8EX737ZyhoNwtn/YzwQhU07zQT1Cx5 H7Ja2vmxC9ObHD5ROOoAPiFzomiS2ws= 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-106-zZ-TIjXFMEm4ziRiiSskFQ-1; Thu, 07 Dec 2023 05:32:18 -0500 X-MC-Unique: zZ-TIjXFMEm4ziRiiSskFQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (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 10F6883BA85 for ; Thu, 7 Dec 2023 10:32:18 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.131]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5B04A8CD0 for ; Thu, 7 Dec 2023 10:32:17 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v3 20/32] elf: Add l_soname accessor function for DT_SONAME values In-Reply-To: Message-ID: References: X-From-Line: d667511ff2c9f24a98c01132cfcb671133d4889e Mon Sep 17 00:00:00 2001 Date: Thu, 07 Dec 2023 11:32:15 +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.5 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, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE, URIBL_BLACK 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 It's not necessary to introduce temporaries because the compiler is able to evaluate l_soname just once in constracts like: l_soname (l) != NULL && strcmp (l_soname (l), LIBC_SO) != 0 --- elf/dl-load.c | 20 +++++------------ elf/dl-open.c | 4 +--- elf/rtld.c | 44 ++++++++++++++++---------------------- elf/setup-vdso.h | 15 +++++++------ elf/sprof.c | 5 +---- sysdeps/generic/ldsodefs.h | 12 +++++++++++ 6 files changed, 46 insertions(+), 54 deletions(-) diff --git a/elf/dl-load.c b/elf/dl-load.c index d45ae48354..9e64992c36 100644 --- a/elf/dl-load.c +++ b/elf/dl-load.c @@ -1414,8 +1414,7 @@ cannot enable executable stack as shared object requires"); loading. Add it right away. */ if (__glibc_unlikely (GLRO(dl_profile) != NULL) && l->l_info[DT_SONAME] != NULL) - add_name_to_object (l, ((const char *) D_PTR (l, l_info[DT_STRTAB]) - + l->l_info[DT_SONAME]->d_un.d_val)); + add_name_to_object (l, l_soname (l)); #else /* Audit modules only exist when linking is dynamic so ORIGNAME cannot be non-NULL. */ @@ -1425,9 +1424,7 @@ cannot enable executable stack as shared object requires"); /* If we have newly loaded libc.so, update the namespace description. */ if (GL(dl_ns)[nsid].libc_map == NULL - && l->l_info[DT_SONAME] != NULL - && strcmp (((const char *) D_PTR (l, l_info[DT_STRTAB]) - + l->l_info[DT_SONAME]->d_un.d_val), LIBC_SO) == 0) + && l_soname (l) != NULL && strcmp (l_soname(l), LIBC_SO) == 0) GL(dl_ns)[nsid].libc_map = l; /* _dl_close can only eventually undo the module ID assignment (via @@ -1920,19 +1917,12 @@ _dl_lookup_map (Lmid_t nsid, const char *name) continue; if (!_dl_name_match_p (name, l)) { - const char *soname; - - if (__glibc_likely (l->l_soname_added) - || l->l_info[DT_SONAME] == NULL) - continue; - - soname = ((const char *) D_PTR (l, l_info[DT_STRTAB]) - + l->l_info[DT_SONAME]->d_un.d_val); - if (strcmp (name, soname) != 0) + if (__glibc_likely (l->l_soname_added) || l_soname (l) == NULL + || strcmp (name, l_soname (l)) != 0) continue; /* We have a match on a new name -- cache it. */ - add_name_to_object (l, soname); + add_name_to_object (l, l_soname (l)); l->l_soname_added = 1; } diff --git a/elf/dl-open.c b/elf/dl-open.c index 306cdcc6ac..d270672c1f 100644 --- a/elf/dl-open.c +++ b/elf/dl-open.c @@ -650,9 +650,7 @@ dl_open_worker_begin (void *a) Perform partial initialization in this case. This must come after the symbol versioning initialization in _dl_check_map_versions. */ - if (map->l_info[DT_SONAME] != NULL - && strcmp (((const char *) D_PTR (map, l_info[DT_STRTAB]) - + map->l_info[DT_SONAME]->d_un.d_val), LD_SO) == 0) + if (l_soname (map) != NULL && strcmp (l_soname (map), LD_SO) == 0) __rtld_static_init (map); #endif } diff --git a/elf/rtld.c b/elf/rtld.c index b2f0b478bb..c92e99927b 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -1075,13 +1075,9 @@ static void rtld_chain_load (struct link_map_private *main_map, char *argv0) { /* The dynamic loader run against itself. */ - const char *rtld_soname - = ((const char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB]) - + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_val); - if (main_map->l_info[DT_SONAME] != NULL - && strcmp (rtld_soname, - ((const char *) D_PTR (main_map, l_info[DT_STRTAB]) - + main_map->l_info[DT_SONAME]->d_un.d_val)) == 0) + const char *rtld_soname = l_soname (&GL(dl_rtld_map)); + if (l_soname (main_map) != NULL + && strcmp (rtld_soname, l_soname (main_map)) == 0) _dl_fatal_printf ("%s: loader cannot load itself\n", rtld_soname); /* With DT_NEEDED dependencies, the executable is dynamically @@ -1648,20 +1644,20 @@ dl_main (const ElfW(Phdr) *phdr, /* If the current libname is different from the SONAME, add the latter as well. */ - if (GL(dl_rtld_map).l_info[DT_SONAME] != NULL - && strcmp (GL(dl_rtld_map).l_libname->name, - (const char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB]) - + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_val) != 0) - { - static struct libname_list newname; - newname.name = ((char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB]) - + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_ptr); - newname.next = NULL; - newname.dont_free = 1; - - assert (GL(dl_rtld_map).l_libname->next == NULL); - GL(dl_rtld_map).l_libname->next = &newname; - } + { + const char *soname = l_soname (&GL(dl_rtld_map)); + if (soname != NULL + && strcmp (GL(dl_rtld_map).l_libname->name, soname) != 0) + { + static struct libname_list newname; + newname.name = soname; + newname.next = NULL; + newname.dont_free = 1; + + assert (GL(dl_rtld_map).l_libname->next == NULL); + GL(dl_rtld_map).l_libname->next = &newname; + } + } /* The ld.so must be relocated since otherwise loading audit modules will fail since they reuse the very same ld.so. */ assert (GL(dl_rtld_map).l_relocated); @@ -1674,10 +1670,8 @@ dl_main (const ElfW(Phdr) *phdr, /* If the main map is libc.so, update the base namespace to refer to this map. If libc.so is loaded later, this happens in _dl_map_object_from_fd. */ - if (main_map->l_info[DT_SONAME] != NULL - && (strcmp (((const char *) D_PTR (main_map, l_info[DT_STRTAB]) - + main_map->l_info[DT_SONAME]->d_un.d_val), LIBC_SO) - == 0)) + if (l_soname (main_map) != NULL + && strcmp (l_soname (main_map), LIBC_SO) == 0) GL(dl_ns)[LM_ID_BASE].libc_map = main_map; /* Set up our cache of pointers into the hash table. */ diff --git a/elf/setup-vdso.h b/elf/setup-vdso.h index 5e9d2eb820..8aaa44ca26 100644 --- a/elf/setup-vdso.h +++ b/elf/setup-vdso.h @@ -77,13 +77,14 @@ setup_vdso (struct link_map_private *main_map __attribute__ ((unused)), /* Now that we have the info handy, use the DSO image's soname so this object can be looked up by name. */ - if (l->l_info[DT_SONAME] != NULL) - { - char *dsoname = ((char *) D_PTR (l, l_info[DT_STRTAB]) - + l->l_info[DT_SONAME]->d_un.d_val); - l->l_libname->name = dsoname; - l->l_public.l_name = dsoname; - } + { + const char *dsoname = l_soname (l); + if (dsoname != NULL) + { + l->l_libname->name = dsoname; + l->l_public.l_name = (char *) dsoname; + } + } /* Add the vDSO to the object list. */ _dl_add_to_namespace_list (l, LM_ID_BASE); diff --git a/elf/sprof.c b/elf/sprof.c index 155da1bd03..81f51a7632 100644 --- a/elf/sprof.c +++ b/elf/sprof.c @@ -530,10 +530,7 @@ load_shobj (const char *name) printf ("string table: %p\n", result->dynstrtab); /* Determine the soname. */ - if (map->l_info[DT_SONAME] == NULL) - result->soname = NULL; - else - result->soname = result->dynstrtab + map->l_info[DT_SONAME]->d_un.d_val; + result->soname = l_soname (map); if (do_test && result->soname != NULL) printf ("soname: %s\n", result->soname); diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h index 01f99a57b7..9f2022d43e 100644 --- a/sysdeps/generic/ldsodefs.h +++ b/sysdeps/generic/ldsodefs.h @@ -88,6 +88,18 @@ dl_relocate_ld (const struct link_map_private *l) #define D_PTR(map, i) \ ((map)->i->d_un.d_ptr + (dl_relocate_ld (map) ? 0 : (map)->l_public.l_addr)) +/* Returns the soname string if the link map has a DT_SONAME tag, or + NULL if it does not. */ +static inline const char * +l_soname (const struct link_map_private *l) +{ + if (l->l_info[DT_SONAME] == NULL) + return NULL; + else + return ((const char *) D_PTR (l, l_info[DT_STRTAB]) + + l->l_info[DT_SONAME]->d_un.d_val); +} + /* Result of the lookup functions and how to retrieve the base address. */ typedef struct link_map_private *lookup_t; #define LOOKUP_VALUE(map) map