From patchwork Tue Jul 4 20:03:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1803373 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=server2.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=XKlDheka; dkim-atps=neutral Received: from server2.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 4QwYhq3bnLz20Pg for ; Wed, 5 Jul 2023 06:06:35 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7B890386C5AE for ; Tue, 4 Jul 2023 20:06:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7B890386C5AE DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1688501193; bh=BepnI9T5+tCBCCTGVNUWgbjE3PJ+ivh6kAyB6S5K8zM=; 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=XKlDhekaEdMIRfVg1aVT/c+Bn2VrUJqSddYKaM6nmtKXb9ItYo4VLLD7TQOoPaNek CFcDTKfz71iJogrXCaN5tF/G9RWg9Jvzhl+ZHyDOZaKl9sf2jBORsGftqt6+3nxVrD e8eKhpg+1N26RgWKF1Yz91qN9DFxJSoba24EcFVM= 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 0B45B3875440 for ; Tue, 4 Jul 2023 20:03:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0B45B3875440 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-423-ns_Oao85O5-TK67EGv4Zzg-1; Tue, 04 Jul 2023 16:03:43 -0400 X-MC-Unique: ns_Oao85O5-TK67EGv4Zzg-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7882980123E for ; Tue, 4 Jul 2023 20:03:43 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.19]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9F01C15230A6 for ; Tue, 4 Jul 2023 20:03:42 +0000 (UTC) To: libc-alpha@sourceware.org Subject: [PATCH 19/33] elf: Move link map fields used by dependency sorting to writable part In-Reply-To: Message-ID: <223ff5913a09884c48dc916dbe664735b4ef0eae.1688499219.git.fweimer@redhat.com> References: X-From-Line: 223ff5913a09884c48dc916dbe664735b4ef0eae Mon Sep 17 00:00:00 2001 Date: Tue, 04 Jul 2023 22:03:40 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.5 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_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.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" Currently, ld.so re-runs dependency sorting during process shutdown in _dl_fini, instead of simply using the reverse initialization order. This means that the l_idx and l_visited fields are written to. There is no way to report errors during shutdown. If these fields are always writable, this avoids the need to make link maps writable during _dl_fini, avoiding the error reporting issue. This commit can be reverted once we stop re-sorting dependencies in _dl_fini. --- elf/dl-close.c | 27 ++++++++++++++------------- elf/dl-fini.c | 2 +- elf/dl-sort-maps.c | 14 +++++++------- include/link.h | 12 +++++++----- 4 files changed, 29 insertions(+), 26 deletions(-) diff --git a/elf/dl-close.c b/elf/dl-close.c index 4001475056..58a69f9b42 100644 --- a/elf/dl-close.c +++ b/elf/dl-close.c @@ -147,7 +147,7 @@ _dl_close_worker (struct link_map_private *map, bool force) { l->l_map_used = 0; l->l_map_done = 0; - l->l_idx = idx; + l->l_rw->l_idx = idx; maps[idx] = l; ++idx; } @@ -177,7 +177,7 @@ _dl_close_worker (struct link_map_private *map, bool force) l->l_map_used = 1; l->l_map_done = 1; /* Signal the object is still needed. */ - l->l_idx = IDX_STILL_USED; + l->l_rw->l_idx = IDX_STILL_USED; /* Mark all dependencies as used. */ if (l->l_initfini != NULL) @@ -187,9 +187,10 @@ _dl_close_worker (struct link_map_private *map, bool force) struct link_map_private **lp = &l->l_initfini[1]; while (*lp != NULL) { - if ((*lp)->l_idx != IDX_STILL_USED) + if ((*lp)->l_rw->l_idx != IDX_STILL_USED) { - assert ((*lp)->l_idx >= 0 && (*lp)->l_idx < nloaded); + assert ((*lp)->l_rw->l_idx >= 0 + && (*lp)->l_rw->l_idx < nloaded); if (!(*lp)->l_map_used) { @@ -198,8 +199,8 @@ _dl_close_worker (struct link_map_private *map, bool force) already processed it, then we need to go back and process again from that point forward to ensure we keep all of its dependencies also. */ - if ((*lp)->l_idx - 1 < done_index) - done_index = (*lp)->l_idx - 1; + if ((*lp)->l_rw->l_idx - 1 < done_index) + done_index = (*lp)->l_rw->l_idx - 1; } } @@ -212,15 +213,15 @@ _dl_close_worker (struct link_map_private *map, bool force) { struct link_map_private *jmap = l->l_rw->l_reldeps->list[j]; - if (jmap->l_idx != IDX_STILL_USED) + if (jmap->l_rw->l_idx != IDX_STILL_USED) { - assert (jmap->l_idx >= 0 && jmap->l_idx < nloaded); + assert (jmap->l_rw->l_idx >= 0 && jmap->l_rw->l_idx < nloaded); if (!jmap->l_map_used) { jmap->l_map_used = 1; - if (jmap->l_idx - 1 < done_index) - done_index = jmap->l_idx - 1; + if (jmap->l_rw->l_idx - 1 < done_index) + done_index = jmap->l_rw->l_idx - 1; } } } @@ -310,7 +311,7 @@ _dl_close_worker (struct link_map_private *map, bool force) ((char *) imap->l_scope[cnt] - offsetof (struct link_map_private, l_searchlist)); assert (tmap->l_ns == nsid); - if (tmap->l_idx == IDX_STILL_USED) + if (tmap->l_rw->l_idx == IDX_STILL_USED) ++remain; else removed_any = true; @@ -357,7 +358,7 @@ _dl_close_worker (struct link_map_private *map, bool force) ((char *) imap->l_scope[cnt] - offsetof (struct link_map_private, l_searchlist))); - if (tmap->l_idx != IDX_STILL_USED) + if (tmap->l_rw->l_idx != IDX_STILL_USED) { /* Remove the scope. Or replace with own map's scope. */ @@ -402,7 +403,7 @@ _dl_close_worker (struct link_map_private *map, bool force) /* The loader is gone, so mark the object as not having one. Note: l_idx != IDX_STILL_USED -> object will be removed. */ if (imap->l_loader != NULL - && imap->l_loader->l_idx != IDX_STILL_USED) + && imap->l_loader->l_rw->l_idx != IDX_STILL_USED) imap->l_loader = NULL; /* Remember where the first dynamically loaded object is. */ diff --git a/elf/dl-fini.c b/elf/dl-fini.c index 5c78159fee..2abd63cb08 100644 --- a/elf/dl-fini.c +++ b/elf/dl-fini.c @@ -77,7 +77,7 @@ _dl_fini (void) assert (i < nloaded); maps[i] = l; - l->l_idx = i; + l->l_rw->l_idx = i; ++i; /* Bump l_direct_opencount of all objects so that they diff --git a/elf/dl-sort-maps.c b/elf/dl-sort-maps.c index bc0801d8a5..f2aa2213d7 100644 --- a/elf/dl-sort-maps.c +++ b/elf/dl-sort-maps.c @@ -51,7 +51,7 @@ _dl_sort_maps_original (struct link_map_private **maps, unsigned int nmaps, { /* Do not handle ld.so in secondary namespaces and objects which are not removed. */ - if (thisp != thisp->l_real || thisp->l_idx == -1) + if (thisp != thisp->l_real || thisp->l_rw->l_idx == -1) goto skip; } @@ -138,17 +138,17 @@ dfs_traversal (struct link_map_private ***rpo, struct link_map_private *map, { /* _dl_map_object_deps ignores l_faked objects when calculating the number of maps before calling _dl_sort_maps, ignore them as well. */ - if (map->l_visited || map->l_faked) + if (map->l_rw->l_visited || map->l_faked) return; - map->l_visited = 1; + map->l_rw->l_visited = 1; if (map->l_initfini) { for (int i = 0; map->l_initfini[i] != NULL; i++) { struct link_map_private *dep = map->l_initfini[i]; - if (dep->l_visited == 0 + if (dep->l_rw->l_visited == 0 && dep->l_main_map == 0) dfs_traversal (rpo, dep, do_reldeps); } @@ -163,7 +163,7 @@ dfs_traversal (struct link_map_private ***rpo, struct link_map_private *map, for (int m = map->l_rw->l_reldeps->act - 1; m >= 0; m--) { struct link_map_private *dep = map->l_rw->l_reldeps->list[m]; - if (dep->l_visited == 0 + if (dep->l_rw->l_visited == 0 && dep->l_main_map == 0) dfs_traversal (rpo, dep, do_reldeps); } @@ -182,7 +182,7 @@ _dl_sort_maps_dfs (struct link_map_private **maps, unsigned int nmaps, { struct link_map_private *first_map = maps[0]; for (int i = nmaps - 1; i >= 0; i--) - maps[i]->l_visited = 0; + maps[i]->l_rw->l_visited = 0; /* We apply DFS traversal for each of maps[i] until the whole total order is found and we're at the start of the Reverse-Postorder (RPO) sequence, @@ -245,7 +245,7 @@ _dl_sort_maps_dfs (struct link_map_private **maps, unsigned int nmaps, if (do_reldeps) { for (int i = nmaps - 1; i >= 0; i--) - rpo[i]->l_visited = 0; + rpo[i]->l_rw->l_visited = 0; struct link_map_private **maps_head = &maps[nmaps]; for (int i = nmaps - 1; i >= 0; i--) diff --git a/include/link.h b/include/link.h index 181cd2e444..a53c652941 100644 --- a/include/link.h +++ b/include/link.h @@ -132,6 +132,13 @@ struct link_map_rw ignored. */ bool l_nodelete_active; bool l_nodelete_pending; + + /* Used for dependency sorting in dlclose/_dl_fini. These need to + be writable all the time because there is no way to report an + error in _dl_fini. These flags can be moved into struct + link_map_private once _dl_fini no longer re-sorts link maps. */ + bool l_visited; + int l_idx; }; /* Structure describing a loaded shared object. The `l_next' and `l_prev' @@ -231,8 +238,6 @@ struct link_map_private unsigned int l_global:1; /* Nonzero if object in _dl_global_scope. */ unsigned int l_reserved:2; /* Reserved for internal use. */ unsigned int l_main_map:1; /* Nonzero for the map of the main program. */ - unsigned int l_visited:1; /* Used internally for map dependency - graph traversal. */ unsigned int l_map_used:1; /* These two bits are used during traversal */ unsigned int l_map_done:1; /* of maps in _dl_close_worker. */ unsigned int l_phdr_allocated:1; /* Nonzero if the data structure pointed @@ -321,9 +326,6 @@ struct link_map_private ElfW(Word) l_flags_1; ElfW(Word) l_flags; - /* Temporarily used in `dl_close'. */ - int l_idx; - struct link_map_machine l_mach; struct