unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Florian Weimer <fweimer@redhat.com>
To: libc-alpha@sourceware.org
Subject: [PATCH v3 18/32] elf: Move link map fields used by dependency sorting to writable part
Date: Thu, 07 Dec 2023 11:32:10 +0100	[thread overview]
Message-ID: <3d5c8f150d9112ce00b183638d93ae52694b3b58.1701944612.git.fweimer@redhat.com> (raw)
In-Reply-To: <cover.1701944612.git.fweimer@redhat.com>

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     | 33 +++++++++++++++++----------------
 elf/dl-fini.c      |  2 +-
 elf/dl-sort-maps.c | 14 +++++++-------
 include/link.h     | 12 +++++++-----
 4 files changed, 32 insertions(+), 29 deletions(-)

diff --git a/elf/dl-close.c b/elf/dl-close.c
index f242dcee9e..8f9d57df39 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;
     }
@@ -157,10 +157,10 @@ _dl_close_worker (struct link_map_private *map, bool force)
      The map variable is NULL after a retry.  */
   if (map != NULL)
     {
-      maps[map->l_idx] = maps[0];
-      maps[map->l_idx]->l_idx = map->l_idx;
+      maps[map->l_rw->l_idx] = maps[0];
+      maps[map->l_rw->l_idx]->l_rw->l_idx = map->l_rw->l_idx;
       maps[0] = map;
-      maps[0]->l_idx = 0;
+      maps[0]->l_rw->l_idx = 0;
     }
 
   /* Keep track of the lowest index link map we have covered already.  */
@@ -187,7 +187,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)
@@ -197,9 +197,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)
 		    {
@@ -208,8 +209,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;
 		    }
 		}
 
@@ -222,15 +223,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;
 		  }
 	      }
 	  }
@@ -321,7 +322,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;
@@ -368,7 +369,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.  */
@@ -413,7 +414,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 e3a547e4da..ae8e7bb528 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 c752bc2cb7..2632337e29 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
@@ -319,9 +324,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
-- 
2.43.0



  parent reply	other threads:[~2023-12-07 10:33 UTC|newest]

Thread overview: 77+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-07 10:30 [PATCH v3 00/32] RELRO linkmaps Florian Weimer
2023-12-07 10:30 ` [PATCH v3 01/32] support: Add <support/memprobe.h> for protection flags probing Florian Weimer
2024-02-22 22:39   ` Joseph Myers
2023-12-07 10:30 ` [PATCH v3 02/32] misc: Enable internal use of memory protection keys Florian Weimer
2024-02-22  1:06   ` Joseph Myers
2023-12-07 10:31 ` [PATCH v3 03/32] elf: Remove _dl_sysdep_open_object hook function Florian Weimer
2024-01-31 13:10   ` Joseph Myers
2024-03-11 17:19     ` Florian Weimer
2024-03-11 17:33       ` Joseph Myers
2024-03-11 17:46         ` Florian Weimer
2024-03-11 18:02           ` Joseph Myers
2024-03-11 18:16             ` Florian Weimer
2023-12-07 10:31 ` [PATCH v3 04/32] elf: Eliminate second loop in find_version in dl-version.c Florian Weimer
2024-02-19 22:17   ` Joseph Myers
2023-12-07 10:31 ` [PATCH v3 05/32] elf: In rtld_setup_main_map, assume ld.so has a DYNAMIC segment Florian Weimer
2024-02-19 22:18   ` Joseph Myers
2023-12-07 10:31 ` [PATCH v3 06/32] elf: Remove version assert in check_match in elf/dl-lookup.c Florian Weimer
2024-03-04 23:22   ` Joseph Myers
2023-12-07 10:31 ` [PATCH v3 07/32] elf: Disambiguate some failures in _dl_load_cache_lookup Florian Weimer
2024-02-19 23:07   ` Joseph Myers
2023-12-07 10:31 ` [PATCH v3 08/32] elf: Eliminate alloca in open_verify Florian Weimer
2024-02-19 23:26   ` Joseph Myers
2023-12-07 10:31 ` [PATCH v3 09/32] Do not export <alloc_buffer.h> functions from libc Florian Weimer
2024-02-21 17:13   ` Joseph Myers
2023-12-07 10:31 ` [PATCH v3 10/32] elf: Make <alloc_buffer.h> usable in ld.so Florian Weimer
2024-02-21 17:19   ` Joseph Myers
2023-12-07 10:31 ` [PATCH v3 11/32] elf: Merge the three implementations of _dl_dst_substitute Florian Weimer
2024-02-28 17:52   ` Joseph Myers
2023-12-07 10:31 ` [PATCH v3 12/32] elf: Move __rtld_malloc_init_stubs call into _dl_start_final Florian Weimer
2024-02-22 22:30   ` Joseph Myers
2024-02-22 23:06   ` Andreas Schwab
2023-12-07 10:31 ` [PATCH v3 13/32] elf: Merge __dl_libc_freemem into __rtld_libc_freeres Florian Weimer
2024-02-22 23:23   ` Joseph Myers
2023-12-07 10:31 ` [PATCH v3 14/32] elf: Use struct link_map_private for the internal link map Florian Weimer
2024-02-22 23:36   ` Joseph Myers
2023-12-07 10:32 ` [PATCH v3 15/32] elf: Remove run-time-writable fields from struct link_map_private Florian Weimer
2024-02-23  0:09   ` Joseph Myers
2023-12-07 10:32 ` [PATCH v3 16/32] elf: Move l_tls_offset into read-write part of link map Florian Weimer
2024-02-26 21:57   ` Joseph Myers
2023-12-07 10:32 ` [PATCH v3 17/32] elf: Allocate auditor state after read-write " Florian Weimer
2024-02-26 22:01   ` Joseph Myers
2023-12-07 10:32 ` Florian Weimer [this message]
2024-02-27 17:51   ` [PATCH v3 18/32] elf: Move link map fields used by dependency sorting to writable part Joseph Myers
2023-12-07 10:32 ` [PATCH v3 19/32] elf: Split _dl_lookup_map, _dl_map_new_object from _dl_map_object Florian Weimer
2024-02-27 17:56   ` Joseph Myers
2023-12-07 10:32 ` [PATCH v3 20/32] elf: Add l_soname accessor function for DT_SONAME values Florian Weimer
2024-02-27 22:14   ` Joseph Myers
2023-12-07 10:32 ` [PATCH v3 21/32] elf: _dl_rtld_map should not exist in static builds Florian Weimer
2024-02-28 12:38   ` Joseph Myers
2023-12-07 10:32 ` [PATCH v3 22/32] elf: Introduce GLPM accessor for the protected memory area Florian Weimer
2024-02-28 12:44   ` Joseph Myers
2023-12-07 10:32 ` [PATCH v3 23/32] elf: Bootstrap allocation for future protected memory allocator Florian Weimer
2024-02-28 15:04   ` Joseph Myers
2023-12-07 10:32 ` [PATCH v3 24/32] elf: Implement a basic " Florian Weimer
2024-02-28 18:46   ` Joseph Myers
2023-12-07 10:32 ` [PATCH v3 25/32] elf: Move most of the _dl_find_object data to the protected heap Florian Weimer
2024-02-28 19:06   ` Joseph Myers
2023-12-07 10:32 ` [PATCH v3 26/32] elf: Switch to a region-based protected memory allocator Florian Weimer
2024-03-05 23:36   ` Joseph Myers
2023-12-07 10:32 ` [PATCH v3 27/32] elf: Determine the caller link map in _dl_open Florian Weimer
2024-02-28 19:23   ` Joseph Myers
2023-12-07 10:32 ` [PATCH v3 28/32] elf: Add fast path to dlopen for fully-opened maps Florian Weimer
2024-02-28 19:26   ` Joseph Myers
2023-12-07 10:32 ` [PATCH v3 29/32] elf: Use _dl_find_object instead of _dl_find_dso_for_object in dlopen Florian Weimer
2024-02-28 19:27   ` Joseph Myers
2023-12-07 10:32 ` [PATCH v3 30/32] elf: Put critical _dl_find_object pointers into protected memory area Florian Weimer
2024-03-04 21:39   ` Joseph Myers
2023-12-07 10:32 ` [PATCH v3 31/32] elf: Add hash tables to speed up DT_NEEDED, dlopen lookups Florian Weimer
2024-03-06  0:04   ` Joseph Myers
2023-12-07 10:33 ` [PATCH v3 32/32] elf: Use memory protection keys for the protected memory allocator Florian Weimer
2024-03-06  0:11   ` Joseph Myers
2023-12-07 10:53 ` [PATCH v3 00/32] RELRO linkmaps Andreas Schwab
2023-12-07 10:56   ` Florian Weimer
2023-12-07 11:34     ` Andreas Schwab
2024-03-01 14:45     ` Adhemerval Zanella Netto
2024-03-11 17:24       ` Florian Weimer
2024-03-12 12:51         ` Adhemerval Zanella Netto

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/libc/involved.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3d5c8f150d9112ce00b183638d93ae52694b3b58.1701944612.git.fweimer@redhat.com \
    --to=fweimer@redhat.com \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).