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