b1dca6
commit 600d9e0c87940da9b0fdeff492bf888df852d40c
b1dca6
Author: Florian Weimer <fweimer@redhat.com>
b1dca6
Date:   Fri Dec 4 09:13:43 2020 +0100
b1dca6
b1dca6
    elf: Add glibc-hwcaps subdirectory support to ld.so cache processing
b1dca6
    
b1dca6
    This recognizes the DL_CACHE_HWCAP_EXTENSION flag in cache entries,
b1dca6
    and picks the supported cache entry with the highest priority.
b1dca6
    
b1dca6
    The elf/tst-glibc-hwcaps-prepend-cache test documents a non-desired
b1dca6
    aspect of the current cache implementation: If the cache selects a DSO
b1dca6
    that does not exist on disk, _dl_map_object falls back to open_path,
b1dca6
    which may or may not find an alternative implementation.  This is an
b1dca6
    existing limitation that also applies to the legacy hwcaps processing
b1dca6
    for ld.so.cache.
b1dca6
    
b1dca6
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
b1dca6
b1dca6
diff --git a/elf/Makefile b/elf/Makefile
b1dca6
index a3e802a9a99b759c..f67b231c0f8e3aff 100644
b1dca6
--- a/elf/Makefile
b1dca6
+++ b/elf/Makefile
b1dca6
@@ -162,6 +162,12 @@ tst-tls1-static-non-pie-no-pie = yes
b1dca6
 tests-container = \
b1dca6
 			  tst-ldconfig-bad-aux-cache
b1dca6
 
b1dca6
+ifeq (no,$(build-hardcoded-path-in-tests))
b1dca6
+# This is an ld.so.cache test, and RPATH/RUNPATH in the executable
b1dca6
+# interferes with its test objectives.
b1dca6
+tests-container += tst-glibc-hwcaps-prepend-cache
b1dca6
+endif
b1dca6
+
b1dca6
 tests := tst-tls9 tst-leaks1 \
b1dca6
 	tst-array1 tst-array2 tst-array3 tst-array4 tst-array5 \
b1dca6
 	tst-auxv tst-stringtable
b1dca6
@@ -1784,6 +1790,14 @@ $(objpfx)tst-glibc-hwcaps-prepend.out: \
b1dca6
 	  $< > $@; \
b1dca6
 	$(evaluate-test)
b1dca6
 
b1dca6
+# Like tst-glibc-hwcaps-prepend, but uses a container and loads the
b1dca6
+# library via ld.so.cache.  Test setup is contained in the test
b1dca6
+# itself.
b1dca6
+$(objpfx)tst-glibc-hwcaps-prepend-cache: $(libdl)
b1dca6
+$(objpfx)tst-glibc-hwcaps-prepend-cache.out: \
b1dca6
+  $(objpfx)tst-glibc-hwcaps-prepend-cache $(objpfx)libmarkermod1-1.so \
b1dca6
+  $(objpfx)libmarkermod1-2.so $(objpfx)libmarkermod1-3.so
b1dca6
+
b1dca6
 # tst-glibc-hwcaps-mask checks that --glibc-hwcaps-mask can be used to
b1dca6
 # suppress all auto-detected subdirectories.
b1dca6
 $(objpfx)tst-glibc-hwcaps-mask: $(objpfx)libmarkermod1-1.so
b1dca6
@@ -1795,3 +1809,7 @@ $(objpfx)tst-glibc-hwcaps-mask.out: \
b1dca6
 	  --glibc-hwcaps-mask does-not-exist \
b1dca6
 	  $< > $@; \
b1dca6
 	$(evaluate-test)
b1dca6
+
b1dca6
+# Generic dependency for sysdeps implementation of
b1dca6
+# tst-glibc-hwcaps-cache.
b1dca6
+$(objpfx)tst-glibc-hwcaps-cache.out: $(objpfx)tst-glibc-hwcaps
b1dca6
diff --git a/elf/dl-cache.c b/elf/dl-cache.c
b1dca6
index de063faa8b2c88ae..e75afdaee23226e6 100644
b1dca6
--- a/elf/dl-cache.c
b1dca6
+++ b/elf/dl-cache.c
b1dca6
@@ -35,6 +35,144 @@ static struct cache_file *cache;
b1dca6
 static struct cache_file_new *cache_new;
b1dca6
 static size_t cachesize;
b1dca6
 
b1dca6
+#ifdef SHARED
b1dca6
+/* This is used to cache the priorities of glibc-hwcaps
b1dca6
+   subdirectories.  The elements of _dl_cache_priorities correspond to
b1dca6
+   the strings in the cache_extension_tag_glibc_hwcaps section.  */
b1dca6
+static uint32_t *glibc_hwcaps_priorities;
b1dca6
+static uint32_t glibc_hwcaps_priorities_length;
b1dca6
+static uint32_t glibc_hwcaps_priorities_allocated;
b1dca6
+
b1dca6
+/* True if the full malloc was used to allocated the array.  */
b1dca6
+static bool glibc_hwcaps_priorities_malloced;
b1dca6
+
b1dca6
+/* Deallocate the glibc_hwcaps_priorities array.  */
b1dca6
+static void
b1dca6
+glibc_hwcaps_priorities_free (void)
b1dca6
+{
b1dca6
+  /* When the minimal malloc is in use, free does not do anything,
b1dca6
+     so it does not make sense to call it.  */
b1dca6
+  if (glibc_hwcaps_priorities_malloced)
b1dca6
+    free (glibc_hwcaps_priorities);
b1dca6
+  glibc_hwcaps_priorities = NULL;
b1dca6
+  glibc_hwcaps_priorities_allocated = 0;
b1dca6
+}
b1dca6
+
b1dca6
+/* Ordered comparison of a hwcaps string from the cache on the left
b1dca6
+   (identified by its string table index) and a _dl_hwcaps_priorities
b1dca6
+   element on the right.  */
b1dca6
+static int
b1dca6
+glibc_hwcaps_compare (uint32_t left_index, struct dl_hwcaps_priority *right)
b1dca6
+{
b1dca6
+  const char *left_name = (const char *) cache + left_index;
b1dca6
+  uint32_t left_name_length = strlen (left_name);
b1dca6
+  uint32_t to_compare;
b1dca6
+  if (left_name_length < right->name_length)
b1dca6
+    to_compare = left_name_length;
b1dca6
+  else
b1dca6
+    to_compare = right->name_length;
b1dca6
+  int cmp = memcmp (left_name, right->name, to_compare);
b1dca6
+  if (cmp != 0)
b1dca6
+    return cmp;
b1dca6
+  if (left_name_length < right->name_length)
b1dca6
+    return -1;
b1dca6
+  else if (left_name_length > right->name_length)
b1dca6
+    return 1;
b1dca6
+  else
b1dca6
+    return 0;
b1dca6
+}
b1dca6
+
b1dca6
+/* Initialize the glibc_hwcaps_priorities array and its length,
b1dca6
+   glibc_hwcaps_priorities_length.  */
b1dca6
+static void
b1dca6
+glibc_hwcaps_priorities_init (void)
b1dca6
+{
b1dca6
+  struct cache_extension_all_loaded ext;
b1dca6
+  if (!cache_extension_load (cache_new, cache, cachesize, &ext))
b1dca6
+    return;
b1dca6
+
b1dca6
+  uint32_t length = (ext.sections[cache_extension_tag_glibc_hwcaps].size
b1dca6
+		     / sizeof (uint32_t));
b1dca6
+  if (length > glibc_hwcaps_priorities_allocated)
b1dca6
+    {
b1dca6
+      glibc_hwcaps_priorities_free ();
b1dca6
+
b1dca6
+      uint32_t *new_allocation = malloc (length * sizeof (uint32_t));
b1dca6
+      if (new_allocation == NULL)
b1dca6
+	/* This effectively disables hwcaps on memory allocation
b1dca6
+	   errors.  */
b1dca6
+	return;
b1dca6
+
b1dca6
+      glibc_hwcaps_priorities = new_allocation;
b1dca6
+      glibc_hwcaps_priorities_allocated = length;
b1dca6
+      glibc_hwcaps_priorities_malloced = __rtld_malloc_is_complete ();
b1dca6
+    }
b1dca6
+
b1dca6
+  /* Compute the priorities for the subdirectories by merging the
b1dca6
+     array in the cache with the dl_hwcaps_priorities array.  */
b1dca6
+  const uint32_t *left = ext.sections[cache_extension_tag_glibc_hwcaps].base;
b1dca6
+  const uint32_t *left_end = left + length;
b1dca6
+  struct dl_hwcaps_priority *right = _dl_hwcaps_priorities;
b1dca6
+  struct dl_hwcaps_priority *right_end = right + _dl_hwcaps_priorities_length;
b1dca6
+  uint32_t *result = glibc_hwcaps_priorities;
b1dca6
+
b1dca6
+  while (left < left_end && right < right_end)
b1dca6
+    {
b1dca6
+      if (*left < cachesize)
b1dca6
+	{
b1dca6
+	  int cmp = glibc_hwcaps_compare (*left, right);
b1dca6
+	  if (cmp == 0)
b1dca6
+	    {
b1dca6
+	      *result = right->priority;
b1dca6
+	      ++result;
b1dca6
+	      ++left;
b1dca6
+	      ++right;
b1dca6
+	    }
b1dca6
+	  else if (cmp < 0)
b1dca6
+	    {
b1dca6
+	      *result = 0;
b1dca6
+	      ++result;
b1dca6
+	      ++left;
b1dca6
+	    }
b1dca6
+	  else
b1dca6
+	    ++right;
b1dca6
+	}
b1dca6
+      else
b1dca6
+	{
b1dca6
+	  *result = 0;
b1dca6
+	  ++result;
b1dca6
+	}
b1dca6
+    }
b1dca6
+  while (left < left_end)
b1dca6
+    {
b1dca6
+      *result = 0;
b1dca6
+      ++result;
b1dca6
+      ++left;
b1dca6
+    }
b1dca6
+
b1dca6
+  glibc_hwcaps_priorities_length = length;
b1dca6
+}
b1dca6
+
b1dca6
+/* Return the priority of the cache_extension_tag_glibc_hwcaps section
b1dca6
+   entry at INDEX.  Zero means do not use.  Otherwise, lower values
b1dca6
+   indicate greater preference.  */
b1dca6
+static uint32_t
b1dca6
+glibc_hwcaps_priority (uint32_t index)
b1dca6
+{
b1dca6
+  /* This does not need to repeated initialization attempts because
b1dca6
+     this function is only called if there is glibc-hwcaps data in the
b1dca6
+     cache, so the first call initializes the glibc_hwcaps_priorities
b1dca6
+     array.  */
b1dca6
+  if (glibc_hwcaps_priorities_length == 0)
b1dca6
+    glibc_hwcaps_priorities_init ();
b1dca6
+
b1dca6
+  if (index < glibc_hwcaps_priorities_length)
b1dca6
+    return glibc_hwcaps_priorities[index];
b1dca6
+  else
b1dca6
+    return 0;
b1dca6
+}
b1dca6
+#endif /* SHARED */
b1dca6
+
b1dca6
 /* True if PTR is a valid string table index.  */
b1dca6
 static inline bool
b1dca6
 _dl_cache_verify_ptr (uint32_t ptr, size_t string_table_size)
b1dca6
@@ -74,6 +212,9 @@ search_cache (const char *string_table, uint32_t string_table_size,
b1dca6
   int left = 0;
b1dca6
   int right = nlibs - 1;
b1dca6
   const char *best = NULL;
b1dca6
+#ifdef SHARED
b1dca6
+  uint32_t best_priority = 0;
b1dca6
+#endif
b1dca6
 
b1dca6
   while (left <= right)
b1dca6
     {
b1dca6
@@ -129,6 +270,11 @@ search_cache (const char *string_table, uint32_t string_table_size,
b1dca6
 		{
b1dca6
 		  if (best == NULL || flags == GLRO (dl_correct_cache_id))
b1dca6
 		    {
b1dca6
+		      /* Named/extension hwcaps get slightly different
b1dca6
+			 treatment: We keep searching for a better
b1dca6
+			 match.  */
b1dca6
+		      bool named_hwcap = false;
b1dca6
+
b1dca6
 		      if (entry_size >= sizeof (struct file_entry_new))
b1dca6
 			{
b1dca6
 			  /* The entry is large enough to include
b1dca6
@@ -136,7 +282,18 @@ search_cache (const char *string_table, uint32_t string_table_size,
b1dca6
 			  struct file_entry_new *libnew
b1dca6
 			    = (struct file_entry_new *) lib;
b1dca6
 
b1dca6
-			  if (libnew->hwcap & hwcap_exclude)
b1dca6
+#ifdef SHARED
b1dca6
+			  named_hwcap = dl_cache_hwcap_extension (libnew);
b1dca6
+#endif
b1dca6
+
b1dca6
+			  /* The entries with named/extension hwcaps
b1dca6
+			     have been exhausted.  Return the best
b1dca6
+			     match encountered so far if there is
b1dca6
+			     one.  */
b1dca6
+			  if (!named_hwcap && best != NULL)
b1dca6
+			    break;
b1dca6
+
b1dca6
+			  if ((libnew->hwcap & hwcap_exclude) && !named_hwcap)
b1dca6
 			    continue;
b1dca6
 			  if (GLRO (dl_osversion)
b1dca6
 			      && libnew->osversion > GLRO (dl_osversion))
b1dca6
@@ -146,14 +303,41 @@ search_cache (const char *string_table, uint32_t string_table_size,
b1dca6
 			      && ((libnew->hwcap & _DL_HWCAP_PLATFORM)
b1dca6
 				  != platform))
b1dca6
 			    continue;
b1dca6
+
b1dca6
+#ifdef SHARED
b1dca6
+			  /* For named hwcaps, determine the priority
b1dca6
+			     and see if beats what has been found so
b1dca6
+			     far.  */
b1dca6
+			  if (named_hwcap)
b1dca6
+			    {
b1dca6
+			      uint32_t entry_priority
b1dca6
+				= glibc_hwcaps_priority (libnew->hwcap);
b1dca6
+			      if (entry_priority == 0)
b1dca6
+				/* Not usable at all.  Skip.  */
b1dca6
+				continue;
b1dca6
+			      else if (best == NULL
b1dca6
+				       || entry_priority < best_priority)
b1dca6
+				/* This entry is of higher priority
b1dca6
+				   than the previous one, or it is the
b1dca6
+				   first entry.  */
b1dca6
+				best_priority = entry_priority;
b1dca6
+			      else
b1dca6
+				/* An entry has already been found,
b1dca6
+				   but it is a better match.  */
b1dca6
+				continue;
b1dca6
+			    }
b1dca6
+#endif /* SHARED */
b1dca6
 			}
b1dca6
 
b1dca6
 		      best = string_table + lib->value;
b1dca6
 
b1dca6
-		      if (flags == GLRO (dl_correct_cache_id))
b1dca6
+		      if (flags == GLRO (dl_correct_cache_id)
b1dca6
+			  && !named_hwcap)
b1dca6
 			/* We've found an exact match for the shared
b1dca6
 			   object and no general `ELF' release.  Stop
b1dca6
-			   searching.  */
b1dca6
+			   searching, but not if a named (extension)
b1dca6
+			   hwcap is used.  In this case, an entry with
b1dca6
+			   a higher priority may come up later.  */
b1dca6
 			break;
b1dca6
 		    }
b1dca6
 		}
b1dca6
@@ -346,5 +530,9 @@ _dl_unload_cache (void)
b1dca6
       __munmap (cache, cachesize);
b1dca6
       cache = NULL;
b1dca6
     }
b1dca6
+#ifdef SHARED
b1dca6
+  /* This marks the glibc_hwcaps_priorities array as out-of-date.  */
b1dca6
+  glibc_hwcaps_priorities_length = 0;
b1dca6
+#endif
b1dca6
 }
b1dca6
 #endif
b1dca6
diff --git a/elf/dl-hwcaps.c b/elf/dl-hwcaps.c
b1dca6
index e57d0d2d41741021..098173a84c43c1fd 100644
b1dca6
--- a/elf/dl-hwcaps.c
b1dca6
+++ b/elf/dl-hwcaps.c
b1dca6
@@ -89,6 +89,81 @@ copy_hwcaps (struct copy_hwcaps *target, const char *hwcaps,
b1dca6
     }
b1dca6
 }
b1dca6
 
b1dca6
+struct dl_hwcaps_priority *_dl_hwcaps_priorities;
b1dca6
+uint32_t _dl_hwcaps_priorities_length;
b1dca6
+
b1dca6
+/* Allocate _dl_hwcaps_priorities and fill it with data.  */
b1dca6
+static void
b1dca6
+compute_priorities (size_t total_count, const char *prepend,
b1dca6
+		    uint32_t bitmask, const char *mask)
b1dca6
+{
b1dca6
+  _dl_hwcaps_priorities = malloc (total_count
b1dca6
+				  * sizeof (*_dl_hwcaps_priorities));
b1dca6
+  if (_dl_hwcaps_priorities == NULL)
b1dca6
+    _dl_signal_error (ENOMEM, NULL, NULL,
b1dca6
+		      N_("cannot create HWCAP priorities"));
b1dca6
+  _dl_hwcaps_priorities_length = total_count;
b1dca6
+
b1dca6
+  /* First the prepended subdirectories.  */
b1dca6
+  size_t i = 0;
b1dca6
+  {
b1dca6
+    struct dl_hwcaps_split sp;
b1dca6
+    _dl_hwcaps_split_init (&sp, prepend);
b1dca6
+    while (_dl_hwcaps_split (&sp))
b1dca6
+      {
b1dca6
+	_dl_hwcaps_priorities[i].name = sp.segment;
b1dca6
+	_dl_hwcaps_priorities[i].name_length = sp.length;
b1dca6
+	_dl_hwcaps_priorities[i].priority = i + 1;
b1dca6
+	++i;
b1dca6
+      }
b1dca6
+  }
b1dca6
+
b1dca6
+  /* Then the built-in subdirectories that are actually active.  */
b1dca6
+  {
b1dca6
+    struct dl_hwcaps_split_masked sp;
b1dca6
+    _dl_hwcaps_split_masked_init (&sp, _dl_hwcaps_subdirs, bitmask, mask);
b1dca6
+    while (_dl_hwcaps_split_masked (&sp))
b1dca6
+      {
b1dca6
+	_dl_hwcaps_priorities[i].name = sp.split.segment;
b1dca6
+	_dl_hwcaps_priorities[i].name_length = sp.split.length;
b1dca6
+	_dl_hwcaps_priorities[i].priority = i + 1;
b1dca6
+	++i;
b1dca6
+      }
b1dca6
+  }
b1dca6
+  assert (i == total_count);
b1dca6
+}
b1dca6
+
b1dca6
+/* Sort the _dl_hwcaps_priorities array by name.  */
b1dca6
+static void
b1dca6
+sort_priorities_by_name (void)
b1dca6
+{
b1dca6
+  /* Insertion sort.  There is no need to link qsort into the dynamic
b1dca6
+     loader for such a short array.  */
b1dca6
+  for (size_t i = 1; i < _dl_hwcaps_priorities_length; ++i)
b1dca6
+    for (size_t j = i; j > 0; --j)
b1dca6
+      {
b1dca6
+	struct dl_hwcaps_priority *previous = _dl_hwcaps_priorities + j - 1;
b1dca6
+	struct dl_hwcaps_priority *current = _dl_hwcaps_priorities + j;
b1dca6
+
b1dca6
+	/* Bail out if current is greater or equal to the previous
b1dca6
+	   value.  */
b1dca6
+	uint32_t to_compare;
b1dca6
+	if (current->name_length < previous->name_length)
b1dca6
+	  to_compare = current->name_length;
b1dca6
+	else
b1dca6
+	  to_compare = previous->name_length;
b1dca6
+	int cmp = memcmp (current->name, previous->name, to_compare);
b1dca6
+	if (cmp >= 0
b1dca6
+	    || (cmp == 0 && current->name_length >= previous->name_length))
b1dca6
+	  break;
b1dca6
+
b1dca6
+	/* Swap *previous and *current.  */
b1dca6
+	struct dl_hwcaps_priority tmp = *previous;
b1dca6
+	*previous = *current;
b1dca6
+	*current = tmp;
b1dca6
+      }
b1dca6
+}
b1dca6
+
b1dca6
 /* Return an array of useful/necessary hardware capability names.  */
b1dca6
 const struct r_strlenpair *
b1dca6
 _dl_important_hwcaps (const char *glibc_hwcaps_prepend,
b1dca6
@@ -111,6 +186,9 @@ _dl_important_hwcaps (const char *glibc_hwcaps_prepend,
b1dca6
   update_hwcaps_counts (&hwcaps_counts, glibc_hwcaps_prepend, -1, NULL);
b1dca6
   update_hwcaps_counts (&hwcaps_counts, _dl_hwcaps_subdirs,
b1dca6
 			hwcaps_subdirs_active, glibc_hwcaps_mask);
b1dca6
+  compute_priorities (hwcaps_counts.count, glibc_hwcaps_prepend,
b1dca6
+		      hwcaps_subdirs_active, glibc_hwcaps_mask);
b1dca6
+  sort_priorities_by_name ();
b1dca6
 
b1dca6
   /* Each hwcaps subdirectory has a GLIBC_HWCAPS_PREFIX string prefix
b1dca6
      and a "/" suffix once stored in the result.  */
b1dca6
diff --git a/elf/dl-hwcaps.h b/elf/dl-hwcaps.h
b1dca6
index 3fcfbceb1a8fc1c8..769ecab3f886c6c4 100644
b1dca6
--- a/elf/dl-hwcaps.h
b1dca6
+++ b/elf/dl-hwcaps.h
b1dca6
@@ -132,4 +132,23 @@ _dl_hwcaps_subdirs_build_bitmask (int subdirs, int active)
b1dca6
   return mask ^ ((1U << inactive) - 1);
b1dca6
 }
b1dca6
 
b1dca6
+/* Pre-computed glibc-hwcaps subdirectory priorities.  Used in
b1dca6
+   dl-cache.c to quickly find the proprieties for the stored HWCAP
b1dca6
+   names.  */
b1dca6
+struct dl_hwcaps_priority
b1dca6
+{
b1dca6
+  /* The name consists of name_length bytes at name (not necessarily
b1dca6
+     null-terminated).  */
b1dca6
+  const char *name;
b1dca6
+  uint32_t name_length;
b1dca6
+
b1dca6
+  /* Priority of this name.  A positive number.  */
b1dca6
+  uint32_t priority;
b1dca6
+};
b1dca6
+
b1dca6
+/* Pre-computed hwcaps priorities.  Set up by
b1dca6
+   _dl_important_hwcaps.  */
b1dca6
+extern struct dl_hwcaps_priority *_dl_hwcaps_priorities attribute_hidden;
b1dca6
+extern uint32_t _dl_hwcaps_priorities_length attribute_hidden;
b1dca6
+
b1dca6
 #endif /* _DL_HWCAPS_H */
b1dca6
diff --git a/elf/tst-glibc-hwcaps-cache.c b/elf/tst-glibc-hwcaps-cache.c
b1dca6
new file mode 100644
b1dca6
index 0000000000000000..4bad56afc03451fc
b1dca6
--- /dev/null
b1dca6
+++ b/elf/tst-glibc-hwcaps-cache.c
b1dca6
@@ -0,0 +1,45 @@
b1dca6
+/* Wrapper to invoke tst-glibc-hwcaps in a container, to test ld.so.cache.
b1dca6
+   Copyright (C) 2020 Free Software Foundation, Inc.
b1dca6
+   This file is part of the GNU C Library.
b1dca6
+
b1dca6
+   The GNU C Library is free software; you can redistribute it and/or
b1dca6
+   modify it under the terms of the GNU Lesser General Public
b1dca6
+   License as published by the Free Software Foundation; either
b1dca6
+   version 2.1 of the License, or (at your option) any later version.
b1dca6
+
b1dca6
+   The GNU C Library is distributed in the hope that it will be useful,
b1dca6
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
b1dca6
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
b1dca6
+   Lesser General Public License for more details.
b1dca6
+
b1dca6
+   You should have received a copy of the GNU Lesser General Public
b1dca6
+   License along with the GNU C Library; if not, see
b1dca6
+   <https://www.gnu.org/licenses/>.  */
b1dca6
+
b1dca6
+/* This program is just a wrapper that runs ldconfig followed by
b1dca6
+   tst-glibc-hwcaps.  The actual test is provided via an
b1dca6
+   implementation in a sysdeps subdirectory.  */
b1dca6
+
b1dca6
+#include <stdio.h>
b1dca6
+#include <stdlib.h>
b1dca6
+#include <support/support.h>
b1dca6
+#include <unistd.h>
b1dca6
+
b1dca6
+int
b1dca6
+main (int argc, char **argv)
b1dca6
+{
b1dca6
+  /* Run ldconfig to populate the cache.  */
b1dca6
+  {
b1dca6
+    char *command = xasprintf ("%s/ldconfig", support_install_rootsbindir);
b1dca6
+    if (system (command) != 0)
b1dca6
+      return 1;
b1dca6
+    free (command);
b1dca6
+  }
b1dca6
+
b1dca6
+  /* Reuse tst-glibc-hwcaps.  Since this code is running in a
b1dca6
+     container, we can launch it directly.  */
b1dca6
+  char *path = xasprintf ("%s/elf/tst-glibc-hwcaps", support_objdir_root);
b1dca6
+  execv (path, argv);
b1dca6
+  printf ("error: execv of %s failed: %m\n", path);
b1dca6
+  return 1;
b1dca6
+}
b1dca6
diff --git a/elf/tst-glibc-hwcaps-cache.root/etc/ld.so.conf b/elf/tst-glibc-hwcaps-cache.root/etc/ld.so.conf
b1dca6
new file mode 100644
b1dca6
index 0000000000000000..e1e74dbda2bf3dfa
b1dca6
--- /dev/null
b1dca6
+++ b/elf/tst-glibc-hwcaps-cache.root/etc/ld.so.conf
b1dca6
@@ -0,0 +1,2 @@
b1dca6
+# This file was created to suppress a warning from ldconfig:
b1dca6
+# /sbin/ldconfig: Warning: ignoring configuration file that cannot be opened: /etc/ld.so.conf: No such file or directory
b1dca6
diff --git a/elf/tst-glibc-hwcaps-cache.root/postclean.req b/elf/tst-glibc-hwcaps-cache.root/postclean.req
b1dca6
new file mode 100644
b1dca6
index 0000000000000000..e69de29bb2d1d643
b1dca6
diff --git a/elf/tst-glibc-hwcaps-cache.script b/elf/tst-glibc-hwcaps-cache.script
b1dca6
new file mode 100644
b1dca6
index 0000000000000000..6356d152089cdd9a
b1dca6
--- /dev/null
b1dca6
+++ b/elf/tst-glibc-hwcaps-cache.script
b1dca6
@@ -0,0 +1,6 @@
b1dca6
+# test-container does not support scripts in sysdeps directories, so
b1dca6
+# collect everything in one file.
b1dca6
+
b1dca6
+cp $B/elf/libmarkermod2-1.so $L/libmarkermod2.so
b1dca6
+cp $B/elf/libmarkermod3-1.so $L/libmarkermod3.so
b1dca6
+cp $B/elf/libmarkermod4-1.so $L/libmarkermod4.so
b1dca6
diff --git a/elf/tst-glibc-hwcaps-prepend-cache.c b/elf/tst-glibc-hwcaps-prepend-cache.c
b1dca6
new file mode 100644
b1dca6
index 0000000000000000..40509cebe2b5ba27
b1dca6
--- /dev/null
b1dca6
+++ b/elf/tst-glibc-hwcaps-prepend-cache.c
b1dca6
@@ -0,0 +1,149 @@
b1dca6
+/* Test that --glibc-hwcaps-prepend works, using dlopen and /etc/ld.so.cache.
b1dca6
+   Copyright (C) 2020 Free Software Foundation, Inc.
b1dca6
+   This file is part of the GNU C Library.
b1dca6
+
b1dca6
+   The GNU C Library is free software; you can redistribute it and/or
b1dca6
+   modify it under the terms of the GNU Lesser General Public
b1dca6
+   License as published by the Free Software Foundation; either
b1dca6
+   version 2.1 of the License, or (at your option) any later version.
b1dca6
+
b1dca6
+   The GNU C Library is distributed in the hope that it will be useful,
b1dca6
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
b1dca6
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
b1dca6
+   Lesser General Public License for more details.
b1dca6
+
b1dca6
+   You should have received a copy of the GNU Lesser General Public
b1dca6
+   License along with the GNU C Library; if not, see
b1dca6
+   <https://www.gnu.org/licenses/>.  */
b1dca6
+
b1dca6
+#include <dlfcn.h>
b1dca6
+#include <stddef.h>
b1dca6
+#include <stdio.h>
b1dca6
+#include <stdlib.h>
b1dca6
+#include <string.h>
b1dca6
+#include <support/check.h>
b1dca6
+#include <support/support.h>
b1dca6
+#include <support/xdlfcn.h>
b1dca6
+#include <support/xunistd.h>
b1dca6
+
b1dca6
+/* Invoke /sbin/ldconfig with some error checking.  */
b1dca6
+static void
b1dca6
+run_ldconfig (void)
b1dca6
+{
b1dca6
+  char *command = xasprintf ("%s/ldconfig", support_install_rootsbindir);
b1dca6
+  TEST_COMPARE (system (command), 0);
b1dca6
+  free (command);
b1dca6
+}
b1dca6
+
b1dca6
+/* The library under test.  */
b1dca6
+#define SONAME "libmarkermod1.so"
b1dca6
+
b1dca6
+static int
b1dca6
+do_test (void)
b1dca6
+{
b1dca6
+  if (dlopen (SONAME, RTLD_NOW) != NULL)
b1dca6
+    FAIL_EXIT1 (SONAME " is already on the search path");
b1dca6
+
b1dca6
+  /* Install the default implementation of libmarkermod1.so.  */
b1dca6
+  xmkdirp ("/etc", 0777);
b1dca6
+  support_write_file_string ("/etc/ld.so.conf", "/glibc-test/lib\n");
b1dca6
+  xmkdirp ("/glibc-test/lib/glibc-hwcaps/prepend2", 0777);
b1dca6
+  xmkdirp ("/glibc-test/lib/glibc-hwcaps/prepend3", 0777);
b1dca6
+  {
b1dca6
+    char *src = xasprintf ("%s/elf/libmarkermod1-1.so", support_objdir_root);
b1dca6
+    support_copy_file (src, "/glibc-test/lib/" SONAME);
b1dca6
+    free (src);
b1dca6
+  }
b1dca6
+  run_ldconfig ();
b1dca6
+  {
b1dca6
+    /* The default implementation can now be loaded.  */
b1dca6
+    void *handle = xdlopen (SONAME, RTLD_NOW);
b1dca6
+    int (*marker1) (void) = xdlsym (handle, "marker1");
b1dca6
+    TEST_COMPARE (marker1 (), 1);
b1dca6
+    xdlclose (handle);
b1dca6
+  }
b1dca6
+
b1dca6
+  /* Add the first override to the directory that is searched last.  */
b1dca6
+  {
b1dca6
+    char *src = xasprintf ("%s/elf/libmarkermod1-2.so", support_objdir_root);
b1dca6
+    support_copy_file (src, "/glibc-test/lib/glibc-hwcaps/prepend2/"
b1dca6
+                       SONAME);
b1dca6
+    free (src);
b1dca6
+  }
b1dca6
+  {
b1dca6
+    /* This is still the first implementation.  The cache has not been
b1dca6
+       updated.  */
b1dca6
+    void *handle = xdlopen (SONAME, RTLD_NOW);
b1dca6
+    int (*marker1) (void) = xdlsym (handle, "marker1");
b1dca6
+    TEST_COMPARE (marker1 (), 1);
b1dca6
+    xdlclose (handle);
b1dca6
+  }
b1dca6
+  run_ldconfig ();
b1dca6
+  {
b1dca6
+    /* After running ldconfig, it is the second implementation.  */
b1dca6
+    void *handle = xdlopen (SONAME, RTLD_NOW);
b1dca6
+    int (*marker1) (void) = xdlsym (handle, "marker1");
b1dca6
+    TEST_COMPARE (marker1 (), 2);
b1dca6
+    xdlclose (handle);
b1dca6
+  }
b1dca6
+
b1dca6
+  /* Add the second override to the directory that is searched first.  */
b1dca6
+  {
b1dca6
+    char *src = xasprintf ("%s/elf/libmarkermod1-3.so", support_objdir_root);
b1dca6
+    support_copy_file (src, "/glibc-test/lib/glibc-hwcaps/prepend3/"
b1dca6
+                       SONAME);
b1dca6
+    free (src);
b1dca6
+  }
b1dca6
+  {
b1dca6
+    /* This is still the second implementation.  */
b1dca6
+    void *handle = xdlopen (SONAME, RTLD_NOW);
b1dca6
+    int (*marker1) (void) = xdlsym (handle, "marker1");
b1dca6
+    TEST_COMPARE (marker1 (), 2);
b1dca6
+    xdlclose (handle);
b1dca6
+  }
b1dca6
+  run_ldconfig ();
b1dca6
+  {
b1dca6
+    /* After running ldconfig, it is the third implementation.  */
b1dca6
+    void *handle = xdlopen (SONAME, RTLD_NOW);
b1dca6
+    int (*marker1) (void) = xdlsym (handle, "marker1");
b1dca6
+    TEST_COMPARE (marker1 (), 3);
b1dca6
+    xdlclose (handle);
b1dca6
+  }
b1dca6
+
b1dca6
+  /* Remove the second override again, without running ldconfig.
b1dca6
+     Ideally, this would revert to implementation 2.  However, in the
b1dca6
+     current implementation, the cache returns exactly one file name
b1dca6
+     which does not exist after unlinking, so the dlopen fails.  */
b1dca6
+  xunlink ("/glibc-test/lib/glibc-hwcaps/prepend3/" SONAME);
b1dca6
+  TEST_VERIFY (dlopen (SONAME, RTLD_NOW) == NULL);
b1dca6
+  run_ldconfig ();
b1dca6
+  {
b1dca6
+    /* After running ldconfig, the second implementation is available
b1dca6
+       once more.  */
b1dca6
+    void *handle = xdlopen (SONAME, RTLD_NOW);
b1dca6
+    int (*marker1) (void) = xdlsym (handle, "marker1");
b1dca6
+    TEST_COMPARE (marker1 (), 2);
b1dca6
+    xdlclose (handle);
b1dca6
+  }
b1dca6
+
b1dca6
+  return 0;
b1dca6
+}
b1dca6
+
b1dca6
+static void
b1dca6
+prepare (int argc, char **argv)
b1dca6
+{
b1dca6
+  const char *no_restart = "no-restart";
b1dca6
+  if (argc == 2 && strcmp (argv[1], no_restart) == 0)
b1dca6
+    return;
b1dca6
+  /* Re-execute the test with an explicit loader invocation.  */
b1dca6
+  execl (support_objdir_elf_ldso,
b1dca6
+         support_objdir_elf_ldso,
b1dca6
+         "--glibc-hwcaps-prepend", "prepend3:prepend2",
b1dca6
+         argv[0], no_restart,
b1dca6
+         NULL);
b1dca6
+  printf ("error: execv of %s failed: %m\n", argv[0]);
b1dca6
+  _exit (1);
b1dca6
+}
b1dca6
+
b1dca6
+#define PREPARE prepare
b1dca6
+#include <support/test-driver.c>
b1dca6
diff --git a/elf/tst-glibc-hwcaps-prepend-cache.root/postclean.req b/elf/tst-glibc-hwcaps-prepend-cache.root/postclean.req
b1dca6
new file mode 100644
b1dca6
index 0000000000000000..e69de29bb2d1d643