446cf2
commit b44ac4f4c7a8bbe5eaa2701aa9452eaf2c96e1dd
446cf2
Author: Florian Weimer <fweimer@redhat.com>
446cf2
Date:   Fri Dec 4 09:13:43 2020 +0100
446cf2
446cf2
    elf: Process glibc-hwcaps subdirectories in ldconfig
446cf2
    
446cf2
    Libraries from these subdirectories are added to the cache
446cf2
    with a special hwcap bit DL_CACHE_HWCAP_EXTENSION, so that
446cf2
    they are ignored by older dynamic loaders.
446cf2
    
446cf2
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
446cf2
446cf2
diff --git a/elf/cache.c b/elf/cache.c
446cf2
index f773cacacf26db1c..dde3d7fefa4105f9 100644
446cf2
--- a/elf/cache.c
446cf2
+++ b/elf/cache.c
446cf2
@@ -40,6 +40,105 @@
446cf2
 /* Used to store library names, paths, and other strings.  */
446cf2
 static struct stringtable strings;
446cf2
 
446cf2
+/* Keeping track of "glibc-hwcaps" subdirectories.  During cache
446cf2
+   construction, a linear search by name is performed to deduplicate
446cf2
+   entries.  */
446cf2
+struct glibc_hwcaps_subdirectory
446cf2
+{
446cf2
+  struct glibc_hwcaps_subdirectory *next;
446cf2
+
446cf2
+  /* Interned string with the subdirectory name.  */
446cf2
+  struct stringtable_entry *name;
446cf2
+
446cf2
+  /* Array index in the cache_extension_tag_glibc_hwcaps section in
446cf2
+     the stored cached file.  This is computed after all the
446cf2
+     subdirectories have been processed, so that subdirectory names in
446cf2
+     the extension section can be sorted.  */
446cf2
+  uint32_t section_index;
446cf2
+
446cf2
+  /* True if the subdirectory is actually used for anything.  */
446cf2
+  bool used;
446cf2
+};
446cf2
+
446cf2
+const char *
446cf2
+glibc_hwcaps_subdirectory_name (const struct glibc_hwcaps_subdirectory *dir)
446cf2
+{
446cf2
+  return dir->name->string;
446cf2
+}
446cf2
+
446cf2
+/* Linked list of known hwcaps subdirecty names.  */
446cf2
+static struct glibc_hwcaps_subdirectory *hwcaps;
446cf2
+
446cf2
+struct glibc_hwcaps_subdirectory *
446cf2
+new_glibc_hwcaps_subdirectory (const char *name)
446cf2
+{
446cf2
+  struct stringtable_entry *name_interned = stringtable_add (&strings, name);
446cf2
+  for (struct glibc_hwcaps_subdirectory *p = hwcaps; p != NULL; p = p->next)
446cf2
+    if (p->name == name_interned)
446cf2
+      return p;
446cf2
+  struct glibc_hwcaps_subdirectory *p = xmalloc (sizeof (*p));
446cf2
+  p->next = hwcaps;
446cf2
+  p->name = name_interned;
446cf2
+  p->section_index = 0;
446cf2
+  p->used = false;
446cf2
+  hwcaps = p;
446cf2
+  return p;
446cf2
+}
446cf2
+
446cf2
+/* Helper for sorting struct glibc_hwcaps_subdirectory elements by
446cf2
+   name.  */
446cf2
+static int
446cf2
+assign_glibc_hwcaps_indices_compare (const void *l, const void *r)
446cf2
+{
446cf2
+  const struct glibc_hwcaps_subdirectory *left
446cf2
+    = *(struct glibc_hwcaps_subdirectory **)l;
446cf2
+  const struct glibc_hwcaps_subdirectory *right
446cf2
+    = *(struct glibc_hwcaps_subdirectory **)r;
446cf2
+  return strcmp (glibc_hwcaps_subdirectory_name (left),
446cf2
+		 glibc_hwcaps_subdirectory_name (right));
446cf2
+}
446cf2
+
446cf2
+/* Count the number of hwcaps subdirectories which are actually
446cf2
+   used.  */
446cf2
+static size_t
446cf2
+glibc_hwcaps_count (void)
446cf2
+{
446cf2
+  size_t count = 0;
446cf2
+  for (struct glibc_hwcaps_subdirectory *p = hwcaps; p != NULL; p = p->next)
446cf2
+    if (p->used)
446cf2
+      ++count;
446cf2
+  return count;
446cf2
+}
446cf2
+
446cf2
+/* Compute the section_index fields for all   */
446cf2
+static void
446cf2
+assign_glibc_hwcaps_indices (void)
446cf2
+{
446cf2
+  /* Convert the linked list into an array, so that we can use qsort.
446cf2
+     Only copy the subdirectories which are actually used.  */
446cf2
+  size_t count = glibc_hwcaps_count ();
446cf2
+  struct glibc_hwcaps_subdirectory **array
446cf2
+    = xmalloc (sizeof (*array) * count);
446cf2
+  {
446cf2
+    size_t i = 0;
446cf2
+    for (struct glibc_hwcaps_subdirectory *p = hwcaps; p != NULL; p = p->next)
446cf2
+      if (p->used)
446cf2
+	{
446cf2
+	  array[i] = p;
446cf2
+	  ++i;
446cf2
+	}
446cf2
+    assert (i == count);
446cf2
+  }
446cf2
+
446cf2
+  qsort (array, count, sizeof (*array), assign_glibc_hwcaps_indices_compare);
446cf2
+
446cf2
+  /* Assign the array indices.  */
446cf2
+  for (size_t i = 0; i < count; ++i)
446cf2
+    array[i]->section_index = i;
446cf2
+
446cf2
+  free (array);
446cf2
+}
446cf2
+
446cf2
 struct cache_entry
446cf2
 {
446cf2
   struct stringtable_entry *lib; /* Library name.  */
446cf2
@@ -48,6 +147,10 @@ struct cache_entry
446cf2
   unsigned int osversion;	/* Required OS version.  */
446cf2
   uint64_t hwcap;		/* Important hardware capabilities.  */
446cf2
   int bits_hwcap;		/* Number of bits set in hwcap.  */
446cf2
+
446cf2
+  /* glibc-hwcaps subdirectory.  If not NULL, hwcap must be zero.  */
446cf2
+  struct glibc_hwcaps_subdirectory *hwcaps;
446cf2
+
446cf2
   struct cache_entry *next;	/* Next entry in list.  */
446cf2
 };
446cf2
 
446cf2
@@ -60,7 +163,7 @@ static const char *flag_descr[] =
446cf2
 /* Print a single entry.  */
446cf2
 static void
446cf2
 print_entry (const char *lib, int flag, unsigned int osversion,
446cf2
-	     uint64_t hwcap, const char *key)
446cf2
+	     uint64_t hwcap, const char *hwcap_string, const char *key)
446cf2
 {
446cf2
   printf ("\t%s (", lib);
446cf2
   switch (flag & FLAG_TYPE_MASK)
446cf2
@@ -132,7 +235,9 @@ print_entry (const char *lib, int flag, unsigned int osversion,
446cf2
       printf (",%d", flag & FLAG_REQUIRED_MASK);
446cf2
       break;
446cf2
     }
446cf2
-  if (hwcap != 0)
446cf2
+  if (hwcap_string != NULL)
446cf2
+    printf (", hwcap: \"%s\"", hwcap_string);
446cf2
+  else if (hwcap != 0)
446cf2
     printf (", hwcap: %#.16" PRIx64, hwcap);
446cf2
   if (osversion != 0)
446cf2
     {
446cf2
@@ -158,6 +263,29 @@ print_entry (const char *lib, int flag, unsigned int osversion,
446cf2
   printf (") => %s\n", key);
446cf2
 }
446cf2
 
446cf2
+/* Returns the string with the name of the glibcs-hwcaps subdirectory
446cf2
+   associated with ENTRY->hwcap.  file_base must be the base address
446cf2
+   for string table indices.  */
446cf2
+static const char *
446cf2
+glibc_hwcaps_string (struct cache_extension_all_loaded *ext,
446cf2
+		     const void *file_base, size_t file_size,
446cf2
+		     struct file_entry_new *entry)
446cf2
+{
446cf2
+  const uint32_t *hwcaps_array
446cf2
+    = ext->sections[cache_extension_tag_glibc_hwcaps].base;
446cf2
+  if (dl_cache_hwcap_extension (entry) && hwcaps_array != NULL)
446cf2
+    {
446cf2
+      uint32_t index = (uint32_t) entry->hwcap;
446cf2
+      if (index < ext->sections[cache_extension_tag_glibc_hwcaps].size / 4)
446cf2
+	{
446cf2
+	  uint32_t string_table_index = hwcaps_array[index];
446cf2
+	  if (string_table_index < file_size)
446cf2
+	    return file_base + string_table_index;
446cf2
+	}
446cf2
+    }
446cf2
+  return NULL;
446cf2
+}
446cf2
+
446cf2
 /* Print an error and exit if the new-file cache is internally
446cf2
    inconsistent.  */
446cf2
 static void
446cf2
@@ -167,9 +295,7 @@ check_new_cache (struct cache_file_new *cache)
446cf2
     error (EXIT_FAILURE, 0, _("Cache file has wrong endianness.\n"));
446cf2
 }
446cf2
 
446cf2
-/* Print the extension information at the cache at start address
446cf2
-   FILE_BASE, of length FILE_SIZE bytes.  The new-format cache header
446cf2
-   is at CACHE, and the file name for diagnostics is CACHE_NAME.  */
446cf2
+/* Print the extension information in *EXT.  */
446cf2
 static void
446cf2
 print_extensions (struct cache_extension_all_loaded *ext)
446cf2
 {
446cf2
@@ -266,7 +392,7 @@ print_cache (const char *cache_name)
446cf2
       /* Print everything.  */
446cf2
       for (unsigned int i = 0; i < cache->nlibs; i++)
446cf2
 	print_entry (cache_data + cache->libs[i].key,
446cf2
-		     cache->libs[i].flags, 0, 0,
446cf2
+		     cache->libs[i].flags, 0, 0, NULL,
446cf2
 		     cache_data + cache->libs[i].value);
446cf2
     }
446cf2
   else if (format == 1)
446cf2
@@ -281,11 +407,16 @@ print_cache (const char *cache_name)
446cf2
 
446cf2
       /* Print everything.  */
446cf2
       for (unsigned int i = 0; i < cache_new->nlibs; i++)
446cf2
-	print_entry (cache_data + cache_new->libs[i].key,
446cf2
-		     cache_new->libs[i].flags,
446cf2
-		     cache_new->libs[i].osversion,
446cf2
-		     cache_new->libs[i].hwcap,
446cf2
-		     cache_data + cache_new->libs[i].value);
446cf2
+	{
446cf2
+	  const char *hwcaps_string
446cf2
+	    = glibc_hwcaps_string (&ext, cache, cache_size,
446cf2
+				   &cache_new->libs[i]);
446cf2
+	  print_entry (cache_data + cache_new->libs[i].key,
446cf2
+		       cache_new->libs[i].flags,
446cf2
+		       cache_new->libs[i].osversion,
446cf2
+		       cache_new->libs[i].hwcap, hwcaps_string,
446cf2
+		       cache_data + cache_new->libs[i].value);
446cf2
+	}
446cf2
       print_extensions (&ext;;
446cf2
     }
446cf2
   /* Cleanup.  */
446cf2
@@ -311,8 +442,23 @@ compare (const struct cache_entry *e1, const struct cache_entry *e2)
446cf2
 	return 1;
446cf2
       else if (e1->flags > e2->flags)
446cf2
 	return -1;
446cf2
+      /* Keep the glibc-hwcaps extension entries before the regular
446cf2
+	 entries, and sort them by their names.  search_cache in
446cf2
+	 dl-cache.c stops searching once the first non-extension entry
446cf2
+	 is found, so the extension entries need to come first.  */
446cf2
+      else if (e1->hwcaps != NULL && e2->hwcaps == NULL)
446cf2
+	return -1;
446cf2
+      else if (e1->hwcaps == NULL && e2->hwcaps != NULL)
446cf2
+	return 1;
446cf2
+      else if (e1->hwcaps != NULL && e2->hwcaps != NULL)
446cf2
+	{
446cf2
+	  res = strcmp (glibc_hwcaps_subdirectory_name (e1->hwcaps),
446cf2
+			glibc_hwcaps_subdirectory_name (e2->hwcaps));
446cf2
+	  if (res != 0)
446cf2
+	    return res;
446cf2
+	}
446cf2
       /* Sort by most specific hwcap.  */
446cf2
-      else if (e2->bits_hwcap > e1->bits_hwcap)
446cf2
+      if (e2->bits_hwcap > e1->bits_hwcap)
446cf2
 	return 1;
446cf2
       else if (e2->bits_hwcap < e1->bits_hwcap)
446cf2
 	return -1;
446cf2
@@ -337,30 +483,65 @@ enum
446cf2
 			      * sizeof (struct cache_extension_section)))
446cf2
   };
446cf2
 
446cf2
-/* Write the cache extensions to FD.  The extension directory is
446cf2
-   assumed to be located at CACHE_EXTENSION_OFFSET.  */
446cf2
+/* Write the cache extensions to FD.  The string table is shifted by
446cf2
+   STRING_TABLE_OFFSET.  The extension directory is assumed to be
446cf2
+   located at CACHE_EXTENSION_OFFSET.  assign_glibc_hwcaps_indices
446cf2
+   must have been called.  */
446cf2
 static void
446cf2
-write_extensions (int fd, uint32_t cache_extension_offset)
446cf2
+write_extensions (int fd, uint32_t str_offset,
446cf2
+		  uint32_t cache_extension_offset)
446cf2
 {
446cf2
   assert ((cache_extension_offset % 4) == 0);
446cf2
 
446cf2
+  /* The length and contents of the glibc-hwcaps section.  */
446cf2
+  uint32_t hwcaps_count = glibc_hwcaps_count ();
446cf2
+  uint32_t hwcaps_offset = cache_extension_offset + cache_extension_size;
446cf2
+  uint32_t hwcaps_size = hwcaps_count * sizeof (uint32_t);
446cf2
+  uint32_t *hwcaps_array = xmalloc (hwcaps_size);
446cf2
+  for (struct glibc_hwcaps_subdirectory *p = hwcaps; p != NULL; p = p->next)
446cf2
+    if (p->used)
446cf2
+      hwcaps_array[p->section_index] = str_offset + p->name->offset;
446cf2
+
446cf2
+  /* This is the offset of the generator string.  */
446cf2
+  uint32_t generator_offset = hwcaps_offset;
446cf2
+  if (hwcaps_count == 0)
446cf2
+    /* There is no section for the hwcaps subdirectories.  */
446cf2
+    generator_offset -= sizeof (struct cache_extension_section);
446cf2
+  else
446cf2
+    /* The string table indices for the hwcaps subdirectories shift
446cf2
+       the generator string backwards.  */
446cf2
+    generator_offset += hwcaps_size;
446cf2
+
446cf2
   struct cache_extension *ext = xmalloc (cache_extension_size);
446cf2
   ext->magic = cache_extension_magic;
446cf2
-  ext->count = cache_extension_count;
446cf2
 
446cf2
-  for (int i = 0; i < cache_extension_count; ++i)
446cf2
-    {
446cf2
-      ext->sections[i].tag = i;
446cf2
-      ext->sections[i].flags = 0;
446cf2
-    }
446cf2
+  /* Extension index current being filled.  */
446cf2
+  size_t xid = 0;
446cf2
 
446cf2
   const char *generator
446cf2
     = "ldconfig " PKGVERSION RELEASE " release version " VERSION;
446cf2
-  ext->sections[cache_extension_tag_generator].offset
446cf2
-    = cache_extension_offset + cache_extension_size;
446cf2
-  ext->sections[cache_extension_tag_generator].size = strlen (generator);
446cf2
+  ext->sections[xid].tag = cache_extension_tag_generator;
446cf2
+  ext->sections[xid].flags = 0;
446cf2
+  ext->sections[xid].offset = generator_offset;
446cf2
+  ext->sections[xid].size = strlen (generator);
446cf2
+
446cf2
+  if (hwcaps_count > 0)
446cf2
+    {
446cf2
+      ++xid;
446cf2
+      ext->sections[xid].tag = cache_extension_tag_glibc_hwcaps;
446cf2
+      ext->sections[xid].flags = 0;
446cf2
+      ext->sections[xid].offset = hwcaps_offset;
446cf2
+      ext->sections[xid].size = hwcaps_size;
446cf2
+    }
446cf2
+
446cf2
+  ++xid;
446cf2
+  ext->count = xid;
446cf2
+  assert (xid <= cache_extension_count);
446cf2
 
446cf2
-  if (write (fd, ext, cache_extension_size) != cache_extension_size
446cf2
+  size_t ext_size = (offsetof (struct cache_extension, sections)
446cf2
+		     + xid * sizeof (struct cache_extension_section));
446cf2
+  if (write (fd, ext, ext_size) != ext_size
446cf2
+      || write (fd, hwcaps_array, hwcaps_size) != hwcaps_size
446cf2
       || write (fd, generator, strlen (generator)) != strlen (generator))
446cf2
     error (EXIT_FAILURE, errno, _("Writing of cache extension data failed"));
446cf2
 
446cf2
@@ -373,6 +554,8 @@ save_cache (const char *cache_name)
446cf2
 {
446cf2
   /* The cache entries are sorted already, save them in this order. */
446cf2
 
446cf2
+  assign_glibc_hwcaps_indices ();
446cf2
+
446cf2
   struct cache_entry *entry;
446cf2
   /* Number of cache entries.  */
446cf2
   int cache_entry_count = 0;
446cf2
@@ -474,7 +657,11 @@ save_cache (const char *cache_name)
446cf2
 	     struct.  */
446cf2
 	  file_entries_new->libs[idx_new].flags = entry->flags;
446cf2
 	  file_entries_new->libs[idx_new].osversion = entry->osversion;
446cf2
-	  file_entries_new->libs[idx_new].hwcap = entry->hwcap;
446cf2
+	  if (entry->hwcaps == NULL)
446cf2
+	    file_entries_new->libs[idx_new].hwcap = entry->hwcap;
446cf2
+	  else
446cf2
+	    file_entries_new->libs[idx_new].hwcap
446cf2
+	      = DL_CACHE_HWCAP_EXTENSION | entry->hwcaps->section_index;
446cf2
 	  file_entries_new->libs[idx_new].key
446cf2
 	    = str_offset + entry->lib->offset;
446cf2
 	  file_entries_new->libs[idx_new].value
446cf2
@@ -554,7 +741,7 @@ save_cache (const char *cache_name)
446cf2
       /* Align file position to 4.  */
446cf2
       off64_t old_offset = lseek64 (fd, extension_offset, SEEK_SET);
446cf2
       assert ((unsigned long long int) (extension_offset - old_offset) < 4);
446cf2
-      write_extensions (fd, extension_offset);
446cf2
+      write_extensions (fd, str_offset, extension_offset);
446cf2
     }
446cf2
 
446cf2
   /* Make sure user can always read cache file */
446cf2
@@ -588,27 +775,35 @@ save_cache (const char *cache_name)
446cf2
 
446cf2
 /* Add one library to the cache.  */
446cf2
 void
446cf2
-add_to_cache (const char *path, const char *lib, int flags,
446cf2
-	      unsigned int osversion, uint64_t hwcap)
446cf2
+add_to_cache (const char *path, const char *filename, const char *soname,
446cf2
+	      int flags, unsigned int osversion, uint64_t hwcap,
446cf2
+	      struct glibc_hwcaps_subdirectory *hwcaps)
446cf2
 {
446cf2
   struct cache_entry *new_entry = xmalloc (sizeof (*new_entry));
446cf2
 
446cf2
   struct stringtable_entry *path_interned;
446cf2
   {
446cf2
     char *p;
446cf2
-    if (asprintf (&p, "%s/%s", path, lib) < 0)
446cf2
+    if (asprintf (&p, "%s/%s", path, filename) < 0)
446cf2
       error (EXIT_FAILURE, errno, _("Could not create library path"));
446cf2
     path_interned = stringtable_add (&strings, p);
446cf2
     free (p);
446cf2
   }
446cf2
 
446cf2
-  new_entry->lib = stringtable_add (&strings, lib);
446cf2
+  new_entry->lib = stringtable_add (&strings, soname);
446cf2
   new_entry->path = path_interned;
446cf2
   new_entry->flags = flags;
446cf2
   new_entry->osversion = osversion;
446cf2
   new_entry->hwcap = hwcap;
446cf2
+  new_entry->hwcaps = hwcaps;
446cf2
   new_entry->bits_hwcap = 0;
446cf2
 
446cf2
+  if (hwcaps != NULL)
446cf2
+    {
446cf2
+      assert (hwcap == 0);
446cf2
+      hwcaps->used = true;
446cf2
+    }
446cf2
+
446cf2
   /* Count the number of bits set in the masked value.  */
446cf2
   for (size_t i = 0;
446cf2
        (~((1ULL << i) - 1) & hwcap) != 0 && i < 8 * sizeof (hwcap); ++i)
446cf2
diff --git a/elf/ldconfig.c b/elf/ldconfig.c
446cf2
index 0fa5aef83f9cd86c..8c66d7e5426d8cc4 100644
446cf2
--- a/elf/ldconfig.c
446cf2
+++ b/elf/ldconfig.c
446cf2
@@ -16,6 +16,7 @@
446cf2
    along with this program; if not, see <http://www.gnu.org/licenses/>.  */
446cf2
 
446cf2
 #define PROCINFO_CLASS static
446cf2
+#include <assert.h>
446cf2
 #include <alloca.h>
446cf2
 #include <argp.h>
446cf2
 #include <dirent.h>
446cf2
@@ -41,6 +42,7 @@
446cf2
 
446cf2
 #include <ldconfig.h>
446cf2
 #include <dl-cache.h>
446cf2
+#include <dl-hwcaps.h>
446cf2
 
446cf2
 #include <dl-procinfo.h>
446cf2
 
446cf2
@@ -85,6 +87,10 @@ struct dir_entry
446cf2
   dev_t dev;
446cf2
   const char *from_file;
446cf2
   int from_line;
446cf2
+
446cf2
+  /* Non-NULL for subdirectories under a glibc-hwcaps subdirectory.  */
446cf2
+  struct glibc_hwcaps_subdirectory *hwcaps;
446cf2
+
446cf2
   struct dir_entry *next;
446cf2
 };
446cf2
 
446cf2
@@ -338,17 +344,20 @@ new_sub_entry (const struct dir_entry *entry, const char *path,
446cf2
   new_entry->from_line = entry->from_line;
446cf2
   new_entry->path = xstrdup (path);
446cf2
   new_entry->flag = entry->flag;
446cf2
+  new_entry->hwcaps = NULL;
446cf2
   new_entry->next = NULL;
446cf2
   new_entry->ino = st->st_ino;
446cf2
   new_entry->dev = st->st_dev;
446cf2
   return new_entry;
446cf2
 }
446cf2
 
446cf2
-/* Add a single directory entry.  */
446cf2
-static void
446cf2
+/* Add a single directory entry.  Return true if the directory is
446cf2
+   actually added (because it is not a duplicate).  */
446cf2
+static bool
446cf2
 add_single_dir (struct dir_entry *entry, int verbose)
446cf2
 {
446cf2
   struct dir_entry *ptr, *prev;
446cf2
+  bool added = true;
446cf2
 
446cf2
   ptr = dir_entries;
446cf2
   prev = ptr;
446cf2
@@ -368,6 +377,7 @@ add_single_dir (struct dir_entry *entry, int verbose)
446cf2
 	  ptr->flag = entry->flag;
446cf2
 	  free (entry->path);
446cf2
 	  free (entry);
446cf2
+	  added = false;
446cf2
 	  break;
446cf2
 	}
446cf2
       prev = ptr;
446cf2
@@ -378,6 +388,73 @@ add_single_dir (struct dir_entry *entry, int verbose)
446cf2
     dir_entries = entry;
446cf2
   else if (ptr == NULL)
446cf2
     prev->next = entry;
446cf2
+  return added;
446cf2
+}
446cf2
+
446cf2
+/* Check if PATH contains a "glibc-hwcaps" subdirectory.  If so, queue
446cf2
+   its subdirectories for glibc-hwcaps processing.  */
446cf2
+static void
446cf2
+add_glibc_hwcaps_subdirectories (struct dir_entry *entry, const char *path)
446cf2
+{
446cf2
+  /* glibc-hwcaps subdirectories do not nest.  */
446cf2
+  assert (entry->hwcaps == NULL);
446cf2
+
446cf2
+  char *glibc_hwcaps;
446cf2
+  if (asprintf (&glibc_hwcaps, "%s/" GLIBC_HWCAPS_SUBDIRECTORY, path) < 0)
446cf2
+    error (EXIT_FAILURE, errno, _("Could not form glibc-hwcaps path"));
446cf2
+
446cf2
+  DIR *dir = opendir (glibc_hwcaps);
446cf2
+  if (dir != NULL)
446cf2
+    {
446cf2
+      while (true)
446cf2
+	{
446cf2
+	  errno = 0;
446cf2
+	  struct dirent64 *e = readdir64 (dir);
446cf2
+	  if (e == NULL)
446cf2
+	    {
446cf2
+	      if (errno == 0)
446cf2
+		break;
446cf2
+	      else
446cf2
+		error (EXIT_FAILURE, errno, _("Listing directory %s"), path);
446cf2
+	    }
446cf2
+
446cf2
+	  /* Ignore hidden subdirectories, including "." and "..", and
446cf2
+	     regular files.  File names containing a ':' cannot be
446cf2
+	     looked up by the dynamic loader, so skip those as
446cf2
+	     well.  */
446cf2
+	  if (e->d_name[0] == '.' || e->d_type == DT_REG
446cf2
+	      || strchr (e->d_name, ':') != NULL)
446cf2
+	    continue;
446cf2
+
446cf2
+	  /* See if this entry eventually resolves to a directory.  */
446cf2
+	  struct stat64 st;
446cf2
+	  if (fstatat64 (dirfd (dir), e->d_name, &st, 0) < 0)
446cf2
+	    /* Ignore unreadable entries.  */
446cf2
+	    continue;
446cf2
+
446cf2
+	  if (S_ISDIR (st.st_mode))
446cf2
+	    {
446cf2
+	      /* This is a directory, so it needs to be scanned for
446cf2
+		 libraries, associated with the hwcaps implied by the
446cf2
+		 subdirectory name.  */
446cf2
+	      char *new_path;
446cf2
+	      if (asprintf (&new_path, "%s/" GLIBC_HWCAPS_SUBDIRECTORY "/%s",
446cf2
+			    /* Use non-canonicalized path here.  */
446cf2
+			    entry->path, e->d_name) < 0)
446cf2
+		error (EXIT_FAILURE, errno,
446cf2
+		       _("Could not form glibc-hwcaps path"));
446cf2
+	      struct dir_entry *new_entry = new_sub_entry (entry, new_path,
446cf2
+							   &st);
446cf2
+	      free (new_path);
446cf2
+	      new_entry->hwcaps = new_glibc_hwcaps_subdirectory (e->d_name);
446cf2
+	      add_single_dir (new_entry, 0);
446cf2
+	    }
446cf2
+	}
446cf2
+
446cf2
+      closedir (dir);
446cf2
+    }
446cf2
+
446cf2
+  free (glibc_hwcaps);
446cf2
 }
446cf2
 
446cf2
 /* Add one directory to the list of directories to process.  */
446cf2
@@ -386,6 +463,7 @@ add_dir_1 (const char *line, const char *from_file, int from_line)
446cf2
 {
446cf2
   unsigned int i;
446cf2
   struct dir_entry *entry = xmalloc (sizeof (struct dir_entry));
446cf2
+  entry->hwcaps = NULL;
446cf2
   entry->next = NULL;
446cf2
 
446cf2
   entry->from_file = strdup (from_file);
446cf2
@@ -443,7 +521,9 @@ add_dir_1 (const char *line, const char *from_file, int from_line)
446cf2
       entry->ino = stat_buf.st_ino;
446cf2
       entry->dev = stat_buf.st_dev;
446cf2
 
446cf2
-      add_single_dir (entry, 1);
446cf2
+      if (add_single_dir (entry, 1))
446cf2
+	/* Add glibc-hwcaps subdirectories if present.  */
446cf2
+	add_glibc_hwcaps_subdirectories (entry, path);
446cf2
     }
446cf2
 
446cf2
   if (opt_chroot)
446cf2
@@ -695,15 +775,27 @@ struct dlib_entry
446cf2
 static void
446cf2
 search_dir (const struct dir_entry *entry)
446cf2
 {
446cf2
-  uint64_t hwcap = path_hwcap (entry->path);
446cf2
-  if (opt_verbose)
446cf2
+  uint64_t hwcap;
446cf2
+  if (entry->hwcaps == NULL)
446cf2
     {
446cf2
-      if (hwcap != 0)
446cf2
-	printf ("%s: (hwcap: %#.16" PRIx64 ")", entry->path, hwcap);
446cf2
-      else
446cf2
-	printf ("%s:", entry->path);
446cf2
-      printf (_(" (from %s:%d)\n"), entry->from_file, entry->from_line);
446cf2
+      hwcap = path_hwcap (entry->path);
446cf2
+      if (opt_verbose)
446cf2
+	{
446cf2
+	  if (hwcap != 0)
446cf2
+	    printf ("%s: (hwcap: %#.16" PRIx64 ")", entry->path, hwcap);
446cf2
+	  else
446cf2
+	    printf ("%s:", entry->path);
446cf2
+	}
446cf2
     }
446cf2
+  else
446cf2
+    {
446cf2
+      hwcap = 0;
446cf2
+      if (opt_verbose)
446cf2
+	printf ("%s: (hwcap: \"%s\")", entry->path,
446cf2
+		glibc_hwcaps_subdirectory_name (entry->hwcaps));
446cf2
+    }
446cf2
+  if (opt_verbose)
446cf2
+    printf (_(" (from %s:%d)\n"), entry->from_file, entry->from_line);
446cf2
 
446cf2
   char *dir_name;
446cf2
   char *real_file_name;
446cf2
@@ -745,13 +837,15 @@ search_dir (const struct dir_entry *entry)
446cf2
 	  && direntry->d_type != DT_DIR)
446cf2
 	continue;
446cf2
       /* Does this file look like a shared library or is it a hwcap
446cf2
-	 subdirectory?  The dynamic linker is also considered as
446cf2
+	 subdirectory (if not already processing a glibc-hwcaps
446cf2
+	 subdirectory)?  The dynamic linker is also considered as
446cf2
 	 shared library.  */
446cf2
       if (((strncmp (direntry->d_name, "lib", 3) != 0
446cf2
 	    && strncmp (direntry->d_name, "ld-", 3) != 0)
446cf2
 	   || strstr (direntry->d_name, ".so") == NULL)
446cf2
 	  && (direntry->d_type == DT_REG
446cf2
-	      || !is_hwcap_platform (direntry->d_name)))
446cf2
+	      || (entry->hwcaps == NULL
446cf2
+		  && !is_hwcap_platform (direntry->d_name))))
446cf2
 	continue;
446cf2
 
446cf2
       size_t len = strlen (direntry->d_name);
446cf2
@@ -799,7 +893,7 @@ search_dir (const struct dir_entry *entry)
446cf2
 	  }
446cf2
 
446cf2
       struct stat64 stat_buf;
446cf2
-      int is_dir;
446cf2
+      bool is_dir;
446cf2
       int is_link = S_ISLNK (lstat_buf.st_mode);
446cf2
       if (is_link)
446cf2
 	{
446cf2
@@ -837,7 +931,10 @@ search_dir (const struct dir_entry *entry)
446cf2
       else
446cf2
 	is_dir = S_ISDIR (lstat_buf.st_mode);
446cf2
 
446cf2
-      if (is_dir && is_hwcap_platform (direntry->d_name))
446cf2
+      /* No descending into subdirectories if this directory is a
446cf2
+	 glibc-hwcaps subdirectory (which are not recursive).  */
446cf2
+      if (entry->hwcaps == NULL
446cf2
+	  && is_dir && is_hwcap_platform (direntry->d_name))
446cf2
 	{
446cf2
 	  if (!is_link
446cf2
 	      && direntry->d_type != DT_UNKNOWN
446cf2
@@ -1028,13 +1125,31 @@ search_dir (const struct dir_entry *entry)
446cf2
   struct dlib_entry *dlib_ptr;
446cf2
   for (dlib_ptr = dlibs; dlib_ptr != NULL; dlib_ptr = dlib_ptr->next)
446cf2
     {
446cf2
-      /* Don't create links to links.  */
446cf2
-      if (dlib_ptr->is_link == 0)
446cf2
-	create_links (dir_name, entry->path, dlib_ptr->name,
446cf2
-		      dlib_ptr->soname);
446cf2
+      /* The cached file name is the soname for non-glibc-hwcaps
446cf2
+	 subdirectories (relying on symbolic links; this helps with
446cf2
+	 library updates that change the file name), and the actual
446cf2
+	 file for glibc-hwcaps subdirectories.  */
446cf2
+      const char *filename;
446cf2
+      if (entry->hwcaps == NULL)
446cf2
+	{
446cf2
+	  /* Don't create links to links.  */
446cf2
+	  if (dlib_ptr->is_link == 0)
446cf2
+	    create_links (dir_name, entry->path, dlib_ptr->name,
446cf2
+			  dlib_ptr->soname);
446cf2
+	  filename = dlib_ptr->soname;
446cf2
+	}
446cf2
+      else
446cf2
+	{
446cf2
+	  /* Do not create links in glibc-hwcaps subdirectories, but
446cf2
+	     still log the cache addition.  */
446cf2
+	  if (opt_verbose)
446cf2
+	    printf ("\t%s -> %s\n", dlib_ptr->soname, dlib_ptr->name);
446cf2
+	  filename = dlib_ptr->name;
446cf2
+	}
446cf2
       if (opt_build_cache)
446cf2
-	add_to_cache (entry->path, dlib_ptr->soname, dlib_ptr->flag,
446cf2
-		      dlib_ptr->osversion, hwcap);
446cf2
+	add_to_cache (entry->path, filename, dlib_ptr->soname,
446cf2
+		      dlib_ptr->flag, dlib_ptr->osversion,
446cf2
+		      hwcap, entry->hwcaps);
446cf2
     }
446cf2
 
446cf2
   /* Free all resources.  */
446cf2
diff --git a/sysdeps/generic/dl-cache.h b/sysdeps/generic/dl-cache.h
446cf2
index 259e843724531630..6adbe3c79a32a4ec 100644
446cf2
--- a/sysdeps/generic/dl-cache.h
446cf2
+++ b/sysdeps/generic/dl-cache.h
446cf2
@@ -99,6 +99,23 @@ struct file_entry_new
446cf2
   uint64_t hwcap;		/* Hwcap entry.	 */
446cf2
 };
446cf2
 
446cf2
+/* This bit in the hwcap field of struct file_entry_new indicates that
446cf2
+   the lower 32 bits contain an index into the
446cf2
+   cache_extension_tag_glibc_hwcaps section.  Older glibc versions do
446cf2
+   not know about this HWCAP bit, so they will ignore these
446cf2
+   entries.  */
446cf2
+#define DL_CACHE_HWCAP_EXTENSION (1ULL << 62)
446cf2
+
446cf2
+/* Return true if the ENTRY->hwcap value indicates that
446cf2
+   DL_CACHE_HWCAP_EXTENSION is used.  */
446cf2
+static inline bool
446cf2
+dl_cache_hwcap_extension (struct file_entry_new *entry)
446cf2
+{
446cf2
+  /* If DL_CACHE_HWCAP_EXTENSION is set, but other bits as well, this
446cf2
+     is a different kind of extension.  */
446cf2
+  return (entry->hwcap >> 32) == (DL_CACHE_HWCAP_EXTENSION >> 32);
446cf2
+}
446cf2
+
446cf2
 /* See flags member of struct cache_file_new below.  */
446cf2
 enum
446cf2
   {
446cf2
@@ -182,6 +199,17 @@ enum cache_extension_tag
446cf2
       cache file.  */
446cf2
    cache_extension_tag_generator,
446cf2
 
446cf2
+   /* glibc-hwcaps subdirectory information.  An array of uint32_t
446cf2
+      values, which are indices into the string table.  The strings
446cf2
+      are sorted lexicographically (according to strcmp).  The extra
446cf2
+      level of indirection (instead of using string table indices
446cf2
+      directly) allows the dynamic loader to compute the preference
446cf2
+      order of the hwcaps names more efficiently.
446cf2
+
446cf2
+      For this section, 4-byte alignment is required, and the section
446cf2
+      size must be a multiple of 4.  */
446cf2
+   cache_extension_tag_glibc_hwcaps,
446cf2
+
446cf2
    /* Total number of known cache extension tags.  */
446cf2
    cache_extension_count
446cf2
   };
446cf2
@@ -236,6 +264,27 @@ struct cache_extension_all_loaded
446cf2
   struct cache_extension_loaded sections[cache_extension_count];
446cf2
 };
446cf2
 
446cf2
+/* Performs basic data validation based on section tag, and removes
446cf2
+   the sections which are invalid.  */
446cf2
+static void
446cf2
+cache_extension_verify (struct cache_extension_all_loaded *loaded)
446cf2
+{
446cf2
+  {
446cf2
+    /* Section must not be empty, it must be aligned at 4 bytes, and
446cf2
+       the size must be a multiple of 4.  */
446cf2
+    struct cache_extension_loaded *hwcaps
446cf2
+      = &loaded->sections[cache_extension_tag_glibc_hwcaps];
446cf2
+    if (hwcaps->size == 0
446cf2
+	|| ((uintptr_t) hwcaps->base % 4) != 0
446cf2
+	|| (hwcaps->size % 4) != 0)
446cf2
+      {
446cf2
+	hwcaps->base = NULL;
446cf2
+	hwcaps->size = 0;
446cf2
+	hwcaps->flags = 0;
446cf2
+      }
446cf2
+  }
446cf2
+}
446cf2
+
446cf2
 static bool __attribute__ ((unused))
446cf2
 cache_extension_load (const struct cache_file_new *cache,
446cf2
 		      const void *file_base, size_t file_size,
446cf2
@@ -282,6 +331,7 @@ cache_extension_load (const struct cache_file_new *cache,
446cf2
       loaded->sections[tag].size = ext->sections[i].size;
446cf2
       loaded->sections[tag].flags = ext->sections[i].flags;
446cf2
     }
446cf2
+  cache_extension_verify (loaded);
446cf2
   return true;
446cf2
 }
446cf2
 
446cf2
diff --git a/sysdeps/generic/ldconfig.h b/sysdeps/generic/ldconfig.h
446cf2
index b15b142511829436..a8d22f143f867a3e 100644
446cf2
--- a/sysdeps/generic/ldconfig.h
446cf2
+++ b/sysdeps/generic/ldconfig.h
446cf2
@@ -57,8 +57,22 @@ extern void init_cache (void);
446cf2
 
446cf2
 extern void save_cache (const char *cache_name);
446cf2
 
446cf2
-extern void add_to_cache (const char *path, const char *lib, int flags,
446cf2
-			  unsigned int osversion, uint64_t hwcap);
446cf2
+struct glibc_hwcaps_subdirectory;
446cf2
+
446cf2
+/* Return a struct describing the subdirectory for NAME.  Reuse an
446cf2
+   existing struct if it exists.  */
446cf2
+struct glibc_hwcaps_subdirectory *new_glibc_hwcaps_subdirectory
446cf2
+  (const char *name);
446cf2
+
446cf2
+/* Returns the name that was specified when
446cf2
+   add_glibc_hwcaps_subdirectory was called.  */
446cf2
+const char *glibc_hwcaps_subdirectory_name
446cf2
+  (const struct glibc_hwcaps_subdirectory *);
446cf2
+
446cf2
+extern void add_to_cache (const char *path, const char *filename,
446cf2
+			  const char *soname,
446cf2
+			  int flags, unsigned int osversion, uint64_t hwcap,
446cf2
+			  struct glibc_hwcaps_subdirectory *);
446cf2
 
446cf2
 extern void init_aux_cache (void);
446cf2