From patchwork Tue Sep 6 10:27:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1674676 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=r7GeiEvR; dkim-atps=neutral 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 ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4MMM7M1zsjz1yh5 for ; Tue, 6 Sep 2022 20:29:03 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 453FD385116E for ; Tue, 6 Sep 2022 10:29:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 453FD385116E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1662460141; bh=VxYP2pSaazYZHY097Rcgb0a9uBcuiqYHWNszs+j4QxY=; h=To:Subject:In-Reply-To:References:Date:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=r7GeiEvRuNU3AzJixAdrcyiGxCZ5R30figtw586BV0lHjrTSb13ClqKLlSC5AcM4Z lLTC+ywyw5swOmlqJnuHNuwL/mwZq73JPAgRTF2gH+yd4cfjkKM87URzQesrAL5SWh wXrDeNqc6ZbeD1nhvRyZOhPDdR0M7Hq4s/AvmpDk= 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 0BAD13853825 for ; Tue, 6 Sep 2022 10:27:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0BAD13853825 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-282-SWEiQCBxOWWG7WpgE1fWDA-1; Tue, 06 Sep 2022 06:27:32 -0400 X-MC-Unique: SWEiQCBxOWWG7WpgE1fWDA-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 41325101A54E for ; Tue, 6 Sep 2022 10:27:32 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.109]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 820024C816 for ; Tue, 6 Sep 2022 10:27:31 +0000 (UTC) To: libc-alpha@sourceware.org Subject: [PATCH v2 2/3] elf: Introduce _dl_debug_change_state In-Reply-To: References: X-From-Line: e596eeb8df983d8259550e33180d66b937aa8dad Mon Sep 17 00:00:00 2001 Message-Id: Date: Tue, 06 Sep 2022 12:27:29 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.7 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, 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.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Florian Weimer via Libc-alpha From: Florian Weimer Reply-To: Florian Weimer Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" It combines updating r_state with the debugger notification. The change to _dl_open introduces an additional debugger notification for dlmopen, but debuggers are expected to ignore it. --- elf/dl-close.c | 6 ++---- elf/dl-debug.c | 7 +++++++ elf/dl-load.c | 6 ++---- elf/dl-open.c | 5 ++--- elf/rtld.c | 6 ++---- sysdeps/generic/ldsodefs.h | 14 ++++++++++++-- 6 files changed, 27 insertions(+), 17 deletions(-) diff --git a/elf/dl-close.c b/elf/dl-close.c index bcd6e206e9..c300999e5b 100644 --- a/elf/dl-close.c +++ b/elf/dl-close.c @@ -460,8 +460,7 @@ _dl_close_worker (struct link_map *map, bool force) /* Notify the debugger we are about to remove some loaded objects. */ struct r_debug *r = _dl_debug_update (nsid); - r->r_state = RT_DELETE; - _dl_debug_state (); + _dl_debug_change_state (r, RT_DELETE); LIBC_PROBE (unmap_start, 2, nsid, r); if (unload_global) @@ -765,8 +764,7 @@ _dl_close_worker (struct link_map *map, bool force) while (GL(dl_ns)[GL(dl_nns) - 1]._ns_loaded == NULL); /* Notify the debugger those objects are finalized and gone. */ - r->r_state = RT_CONSISTENT; - _dl_debug_state (); + _dl_debug_change_state (r, RT_CONSISTENT); LIBC_PROBE (unmap_complete, 2, nsid, r); /* Recheck if we need to retry, release the lock. */ diff --git a/elf/dl-debug.c b/elf/dl-debug.c index 66f9ad375d..47961236cf 100644 --- a/elf/dl-debug.c +++ b/elf/dl-debug.c @@ -67,6 +67,13 @@ _dl_debug_update (Lmid_t ns) return &r->base; } +void +_dl_debug_change_state (struct r_debug *r, int state) +{ + atomic_store_release (&r->r_state, state); + _dl_debug_state (); +} + /* Initialize _r_debug_extended for the namespace NS. LDBASE is the run-time load address of the dynamic linker, to be put in _r_debug_extended.r_ldbase. Return the address of _r_debug. */ diff --git a/elf/dl-load.c b/elf/dl-load.c index 1ad0868dad..d23ad557fc 100644 --- a/elf/dl-load.c +++ b/elf/dl-load.c @@ -987,8 +987,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, if (make_consistent && r != NULL) { - r->r_state = RT_CONSISTENT; - _dl_debug_state (); + _dl_debug_change_state (r, RT_CONSISTENT); LIBC_PROBE (map_failed, 2, nsid, r); } @@ -1507,8 +1506,7 @@ cannot enable executable stack as shared object requires"); /* Notify the debugger we have added some objects. We need to call _dl_debug_initialize in a static program in case dynamic linking has not been used before. */ - r->r_state = RT_ADD; - _dl_debug_state (); + _dl_debug_change_state (r, RT_ADD); LIBC_PROBE (map_start, 2, nsid, r); make_consistent = true; } diff --git a/elf/dl-open.c b/elf/dl-open.c index 46e8066fd8..b67c2e099f 100644 --- a/elf/dl-open.c +++ b/elf/dl-open.c @@ -617,8 +617,7 @@ dl_open_worker_begin (void *a) /* Notify the debugger all new objects are now ready to go. */ struct r_debug *r = _dl_debug_update (args->nsid); - r->r_state = RT_CONSISTENT; - _dl_debug_state (); + _dl_debug_change_state (r, RT_CONSISTENT); LIBC_PROBE (map_complete, 3, args->nsid, r, new); _dl_open_check (new); @@ -853,7 +852,7 @@ no more namespaces available for dlmopen()")); memset (&GL(dl_ns)[nsid], 0, sizeof (GL(dl_ns)[nsid])); __rtld_lock_initialize (GL(dl_ns)[nsid]._ns_unique_sym_table.lock); - _dl_debug_update (nsid)->r_state = RT_CONSISTENT; + _dl_debug_change_state (_dl_debug_update (nsid), RT_CONSISTENT); } /* Never allow loading a DSO in a namespace which is empty. Such direct placements is only causing problems. Also don't allow diff --git a/elf/rtld.c b/elf/rtld.c index 3e771a93d8..ad67d7e4c5 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -1835,8 +1835,7 @@ dl_main (const ElfW(Phdr) *phdr, elf_setup_debug_entry (main_map, r); /* We start adding objects. */ - r->r_state = RT_ADD; - _dl_debug_state (); + _dl_debug_change_state (r, RT_ADD); LIBC_PROBE (init_start, 2, LM_ID_BASE, r); /* Auditing checkpoint: we are ready to signal that the initial map @@ -2409,8 +2408,7 @@ dl_main (const ElfW(Phdr) *phdr, /* Notify the debugger all new objects are now ready to go. We must re-get the address since by now the variable might be in another object. */ r = _dl_debug_update (LM_ID_BASE); - r->r_state = RT_CONSISTENT; - _dl_debug_state (); + _dl_debug_change_state (r, RT_CONSISTENT); LIBC_PROBE (init_complete, 2, LM_ID_BASE, r); #if defined USE_LDCONFIG && !defined MAP_COPY diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h index b088f6b1ae..31fb19c871 100644 --- a/sysdeps/generic/ldsodefs.h +++ b/sysdeps/generic/ldsodefs.h @@ -1060,8 +1060,14 @@ extern void _dl_debug_state (void); rtld_hidden_proto (_dl_debug_state) /* Initialize `struct r_debug_extended' for the namespace NS. LDBASE - is the run-time load address of the dynamic linker, to be put in the - `r_ldbase' member. Return the address of the structure. */ + is the run-time load address of the dynamic linker, to be put in + the `r_ldbase' member. + + This function returns the address of the r_debug structure for the + namespace. This is not merely a convenience or optimization, but + it is necessary for the LIBC_PROBE Systemtap/debugger probes to + work reliably: direct variable access can create probes that tools + cannot consume. */ extern struct r_debug *_dl_debug_initialize (ElfW(Addr) ldbase, Lmid_t ns) attribute_hidden; @@ -1069,6 +1075,10 @@ extern struct r_debug *_dl_debug_initialize (ElfW(Addr) ldbase, Lmid_t ns) of the namespace NS. */ extern struct r_debug *_dl_debug_update (Lmid_t ns) attribute_hidden; +/* Updates R->r_state to STATE and notifies the debugger by calling + _dl_debug_state. */ +void _dl_debug_change_state (struct r_debug *r, int state) attribute_hidden; + /* Initialize the basic data structure for the search paths. SOURCE is either "LD_LIBRARY_PATH" or "--library-path". GLIBC_HWCAPS_PREPEND adds additional glibc-hwcaps subdirectories to