446cf2
commit dad90d528259b669342757c37dedefa8577e2636
446cf2
Author: Florian Weimer <fweimer@redhat.com>
446cf2
Date:   Fri Dec 4 09:13:43 2020 +0100
446cf2
446cf2
    elf: Add glibc-hwcaps support for LD_LIBRARY_PATH
446cf2
    
446cf2
    This hacks non-power-set processing into _dl_important_hwcaps.
446cf2
    Once the legacy hwcaps handling goes away, the subdirectory
446cf2
    handling needs to be reworked, but it is premature to do this
446cf2
    while both approaches are still supported.
446cf2
    
446cf2
    ld.so supports two new arguments, --glibc-hwcaps-prepend and
446cf2
    --glibc-hwcaps-mask.  Each accepts a colon-separated list of
446cf2
    glibc-hwcaps subdirectory names.  The prepend option adds additional
446cf2
    subdirectories that are searched first, in the specified order.  The
446cf2
    mask option restricts the automatically selected subdirectories to
446cf2
    those listed in the option argument.  For example, on systems where
446cf2
    /usr/lib64 is on the library search path,
446cf2
    --glibc-hwcaps-prepend=valgrind:debug causes the dynamic loader to
446cf2
    search the directories /usr/lib64/glibc-hwcaps/valgrind and
446cf2
    /usr/lib64/glibc-hwcaps/debug just before /usr/lib64 is searched.
446cf2
    
446cf2
    Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
446cf2
446cf2
Conflicts:
446cf2
	elf/Makefile
446cf2
	  (Test backport differences.)
446cf2
446cf2
diff --git a/elf/Makefile b/elf/Makefile
446cf2
index bc96b8fd65e376cc..f795617780b393ec 100644
446cf2
--- a/elf/Makefile
446cf2
+++ b/elf/Makefile
446cf2
@@ -59,7 +59,8 @@ elide-routines.os = $(all-dl-routines) dl-support enbl-secure dl-origin \
446cf2
 # ld.so uses those routines, plus some special stuff for being the program
446cf2
 # interpreter and operating independent of libc.
446cf2
 rtld-routines	= rtld $(all-dl-routines) dl-sysdep dl-environ dl-minimal \
446cf2
-  dl-error-minimal dl-conflict dl-hwcaps dl-usage
446cf2
+  dl-error-minimal dl-conflict dl-hwcaps dl-hwcaps_split dl-hwcaps-subdirs \
446cf2
+  dl-usage
446cf2
 all-rtld-routines = $(rtld-routines) $(sysdep-rtld-routines)
446cf2
 
446cf2
 CFLAGS-dl-runtime.c += -fexceptions -fasynchronous-unwind-tables
446cf2
@@ -199,13 +200,14 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \
446cf2
 	 tst-filterobj tst-filterobj-dlopen tst-auxobj tst-auxobj-dlopen \
446cf2
 	 tst-audit14 tst-audit15 tst-audit16 \
446cf2
 	 tst-tls-ie tst-tls-ie-dlmopen \
446cf2
-	 argv0test
446cf2
+	 argv0test \
446cf2
+	 tst-glibc-hwcaps tst-glibc-hwcaps-prepend tst-glibc-hwcaps-mask
446cf2
 #	 reldep9
446cf2
 tests-internal += loadtest unload unload2 circleload1 \
446cf2
 	 neededtest neededtest2 neededtest3 neededtest4 \
446cf2
 	 tst-tls3 tst-tls6 tst-tls7 tst-tls8 tst-dlmopen2 \
446cf2
 	 tst-ptrguard1 tst-stackguard1 tst-libc_dlvsym \
446cf2
-	 tst-create_format1 tst-tls-surplus
446cf2
+	 tst-create_format1 tst-tls-surplus tst-dl-hwcaps_split
446cf2
 tests-container += tst-pldd
446cf2
 ifeq ($(build-hardcoded-path-in-tests),yes)
446cf2
 tests += tst-dlopen-aout
446cf2
@@ -318,7 +320,10 @@ modules-names = testobj1 testobj2 testobj3 testobj4 testobj5 testobj6 \
446cf2
 		tst-auditlogmod-1 tst-auditlogmod-2 tst-auditlogmod-3 \
446cf2
 		tst-tls-ie-mod0 tst-tls-ie-mod1 tst-tls-ie-mod2 \
446cf2
 		tst-tls-ie-mod3 tst-tls-ie-mod4 tst-tls-ie-mod5 \
446cf2
-		tst-tls-ie-mod6
446cf2
+		tst-tls-ie-mod6 libmarkermod1-1 libmarkermod1-2 libmarkermod1-3 \
446cf2
+		libmarkermod2-1 libmarkermod2-2 \
446cf2
+		libmarkermod3-1 libmarkermod3-2 libmarkermod3-3 \
446cf2
+		libmarkermod4-1 libmarkermod4-2 libmarkermod4-3 libmarkermod4-4 \
446cf2
 
446cf2
 # Most modules build with _ISOMAC defined, but those filtered out
446cf2
 # depend on internal headers.
446cf2
@@ -1732,3 +1737,60 @@ $(objpfx)argv0test.out: tst-rtld-argv0.sh $(objpfx)ld.so \
446cf2
             '$(test-wrapper-env)' '$(run_program_env)' \
446cf2
             '$(rpath-link)' 'test-argv0' > $@; \
446cf2
     $(evaluate-test)
446cf2
+
446cf2
+# A list containing the name of the most likely searched subdirectory
446cf2
+# of the glibc-hwcaps directory, for each supported architecture (in
446cf2
+# other words, the oldest hardware level recognized by the
446cf2
+# glibc-hwcaps mechanism for this architecture).  Used to obtain test
446cf2
+# coverage for some glibc-hwcaps tests for the widest possible range
446cf2
+# of systems.
446cf2
+glibc-hwcaps-first-subdirs-for-tests =
446cf2
+
446cf2
+# The test modules are parameterized by preprocessor macros.
446cf2
+LDFLAGS-libmarkermod1-1.so += -Wl,-soname,libmarkermod1.so
446cf2
+LDFLAGS-libmarkermod2-1.so += -Wl,-soname,libmarkermod2.so
446cf2
+LDFLAGS-libmarkermod3-1.so += -Wl,-soname,libmarkermod3.so
446cf2
+LDFLAGS-libmarkermod4-1.so += -Wl,-soname,libmarkermod4.so
446cf2
+$(objpfx)libmarkermod%.os : markermodMARKER-VALUE.c
446cf2
+	$(compile-command.c) \
446cf2
+	  -DMARKER=marker$(firstword $(subst -, ,$*)) \
446cf2
+	  -DVALUE=$(lastword $(subst -, ,$*))
446cf2
+$(objpfx)libmarkermod1.so: $(objpfx)libmarkermod1-1.so
446cf2
+	cp $< $@
446cf2
+$(objpfx)libmarkermod2.so: $(objpfx)libmarkermod2-1.so
446cf2
+	cp $< $@
446cf2
+$(objpfx)libmarkermod3.so: $(objpfx)libmarkermod3-1.so
446cf2
+	cp $< $@
446cf2
+$(objpfx)libmarkermod4.so: $(objpfx)libmarkermod4-1.so
446cf2
+	cp $< $@
446cf2
+
446cf2
+# tst-glibc-hwcaps-prepend checks that --glibc-hwcaps-prepend is
446cf2
+# preferred over auto-detected subdirectories.
446cf2
+$(objpfx)tst-glibc-hwcaps-prepend: $(objpfx)libmarkermod1-1.so
446cf2
+$(objpfx)glibc-hwcaps/prepend-markermod1/libmarkermod1.so: \
446cf2
+  $(objpfx)libmarkermod1-2.so
446cf2
+	$(make-target-directory)
446cf2
+	cp $< $@
446cf2
+$(objpfx)glibc-hwcaps/%/libmarkermod1.so: $(objpfx)libmarkermod1-3.so
446cf2
+	$(make-target-directory)
446cf2
+	cp $< $@
446cf2
+$(objpfx)tst-glibc-hwcaps-prepend.out: \
446cf2
+  $(objpfx)tst-glibc-hwcaps-prepend $(objpfx)libmarkermod1.so \
446cf2
+  $(patsubst %,$(objpfx)glibc-hwcaps/%/libmarkermod1.so,prepend-markermod1 \
446cf2
+    $(glibc-hwcaps-first-subdirs-for-tests))
446cf2
+	$(test-wrapper) $(rtld-prefix) \
446cf2
+	  --glibc-hwcaps-prepend prepend-markermod1 \
446cf2
+	  $< > $@; \
446cf2
+	$(evaluate-test)
446cf2
+
446cf2
+# tst-glibc-hwcaps-mask checks that --glibc-hwcaps-mask can be used to
446cf2
+# suppress all auto-detected subdirectories.
446cf2
+$(objpfx)tst-glibc-hwcaps-mask: $(objpfx)libmarkermod1-1.so
446cf2
+$(objpfx)tst-glibc-hwcaps-mask.out: \
446cf2
+  $(objpfx)tst-glibc-hwcaps-mask $(objpfx)libmarkermod1.so \
446cf2
+  $(patsubst %,$(objpfx)glibc-hwcaps/%/libmarkermod1.so,\
446cf2
+    $(glibc-hwcaps-first-subdirs-for-tests))
446cf2
+	$(test-wrapper) $(rtld-prefix) \
446cf2
+	  --glibc-hwcaps-mask does-not-exist \
446cf2
+	  $< > $@; \
446cf2
+	$(evaluate-test)
446cf2
diff --git a/elf/dl-hwcaps-subdirs.c b/elf/dl-hwcaps-subdirs.c
446cf2
new file mode 100644
446cf2
index 0000000000000000..60c6d59731ee3188
446cf2
--- /dev/null
446cf2
+++ b/elf/dl-hwcaps-subdirs.c
446cf2
@@ -0,0 +1,29 @@
446cf2
+/* Architecture-specific glibc-hwcaps subdirectories.  Generic version.
446cf2
+   Copyright (C) 2020 Free Software Foundation, Inc.
446cf2
+   This file is part of the GNU C Library.
446cf2
+
446cf2
+   The GNU C Library is free software; you can redistribute it and/or
446cf2
+   modify it under the terms of the GNU Lesser General Public
446cf2
+   License as published by the Free Software Foundation; either
446cf2
+   version 2.1 of the License, or (at your option) any later version.
446cf2
+
446cf2
+   The GNU C Library is distributed in the hope that it will be useful,
446cf2
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
446cf2
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
446cf2
+   Lesser General Public License for more details.
446cf2
+
446cf2
+   You should have received a copy of the GNU Lesser General Public
446cf2
+   License along with the GNU C Library; if not, see
446cf2
+   <https://www.gnu.org/licenses/>.  */
446cf2
+
446cf2
+#include <dl-hwcaps.h>
446cf2
+
446cf2
+/* In the generic version, there are no subdirectories defined.  */
446cf2
+
446cf2
+const char _dl_hwcaps_subdirs[] = "";
446cf2
+
446cf2
+uint32_t
446cf2
+_dl_hwcaps_subdirs_active (void)
446cf2
+{
446cf2
+  return 0;
446cf2
+}
446cf2
diff --git a/elf/dl-hwcaps.c b/elf/dl-hwcaps.c
446cf2
index 82ee89c36a1eb4ab..e57d0d2d41741021 100644
446cf2
--- a/elf/dl-hwcaps.c
446cf2
+++ b/elf/dl-hwcaps.c
446cf2
@@ -26,20 +26,97 @@
446cf2
 #include <dl-procinfo.h>
446cf2
 #include <dl-hwcaps.h>
446cf2
 
446cf2
+/* This is the result of counting the substrings in a colon-separated
446cf2
+   hwcaps string.  */
446cf2
+struct hwcaps_counts
446cf2
+{
446cf2
+  /* Number of substrings.  */
446cf2
+  size_t count;
446cf2
+
446cf2
+  /* Sum of the individual substring lengths (without separators or
446cf2
+     null terminators).  */
446cf2
+  size_t total_length;
446cf2
+
446cf2
+  /* Maximum length of an individual substring.  */
446cf2
+  size_t maximum_length;
446cf2
+};
446cf2
+
446cf2
+/* Update *COUNTS according to the contents of HWCAPS.  Skip over
446cf2
+   entries whose bit is not set in MASK.  */
446cf2
+static void
446cf2
+update_hwcaps_counts (struct hwcaps_counts *counts, const char *hwcaps,
446cf2
+		      uint32_t bitmask, const char *mask)
446cf2
+{
446cf2
+  struct dl_hwcaps_split_masked sp;
446cf2
+  _dl_hwcaps_split_masked_init (&sp, hwcaps, bitmask, mask);
446cf2
+  while (_dl_hwcaps_split_masked (&sp))
446cf2
+    {
446cf2
+      ++counts->count;
446cf2
+      counts->total_length += sp.split.length;
446cf2
+      if (sp.split.length > counts->maximum_length)
446cf2
+	counts->maximum_length = sp.split.length;
446cf2
+    }
446cf2
+}
446cf2
+
446cf2
+/* State for copy_hwcaps.  Must be initialized to point to
446cf2
+   the storage areas for the array and the strings themselves.  */
446cf2
+struct copy_hwcaps
446cf2
+{
446cf2
+  struct r_strlenpair *next_pair;
446cf2
+  char *next_string;
446cf2
+};
446cf2
+
446cf2
+/* Copy HWCAPS into the string pairs and strings, advancing *TARGET.
446cf2
+   Skip over entries whose bit is not set in MASK.  */
446cf2
+static void
446cf2
+copy_hwcaps (struct copy_hwcaps *target, const char *hwcaps,
446cf2
+	     uint32_t bitmask, const char *mask)
446cf2
+{
446cf2
+  struct dl_hwcaps_split_masked sp;
446cf2
+  _dl_hwcaps_split_masked_init (&sp, hwcaps, bitmask, mask);
446cf2
+  while (_dl_hwcaps_split_masked (&sp))
446cf2
+    {
446cf2
+      target->next_pair->str = target->next_string;
446cf2
+      char *slash = __mempcpy (__mempcpy (target->next_string,
446cf2
+					  GLIBC_HWCAPS_PREFIX,
446cf2
+					  strlen (GLIBC_HWCAPS_PREFIX)),
446cf2
+			       sp.split.segment, sp.split.length);
446cf2
+      *slash = '/';
446cf2
+      target->next_pair->len
446cf2
+	= strlen (GLIBC_HWCAPS_PREFIX) + sp.split.length + 1;
446cf2
+      ++target->next_pair;
446cf2
+      target->next_string = slash + 1;
446cf2
+    }
446cf2
+}
446cf2
+
446cf2
 /* Return an array of useful/necessary hardware capability names.  */
446cf2
 const struct r_strlenpair *
446cf2
-_dl_important_hwcaps (size_t *sz, size_t *max_capstrlen)
446cf2
+_dl_important_hwcaps (const char *glibc_hwcaps_prepend,
446cf2
+		      const char *glibc_hwcaps_mask,
446cf2
+		      size_t *sz, size_t *max_capstrlen)
446cf2
 {
446cf2
   uint64_t hwcap_mask = GET_HWCAP_MASK();
446cf2
   /* Determine how many important bits are set.  */
446cf2
   uint64_t masked = GLRO(dl_hwcap) & hwcap_mask;
446cf2
   size_t cnt = GLRO (dl_platform) != NULL;
446cf2
   size_t n, m;
446cf2
-  size_t total;
446cf2
   struct r_strlenpair *result;
446cf2
   struct r_strlenpair *rp;
446cf2
   char *cp;
446cf2
 
446cf2
+  /* glibc-hwcaps subdirectories.  These are exempted from the power
446cf2
+     set construction below.  */
446cf2
+  uint32_t hwcaps_subdirs_active = _dl_hwcaps_subdirs_active ();
446cf2
+  struct hwcaps_counts hwcaps_counts =  { 0, };
446cf2
+  update_hwcaps_counts (&hwcaps_counts, glibc_hwcaps_prepend, -1, NULL);
446cf2
+  update_hwcaps_counts (&hwcaps_counts, _dl_hwcaps_subdirs,
446cf2
+			hwcaps_subdirs_active, glibc_hwcaps_mask);
446cf2
+
446cf2
+  /* Each hwcaps subdirectory has a GLIBC_HWCAPS_PREFIX string prefix
446cf2
+     and a "/" suffix once stored in the result.  */
446cf2
+  size_t total = (hwcaps_counts.count * (strlen (GLIBC_HWCAPS_PREFIX) + 1)
446cf2
+		  + hwcaps_counts.total_length);
446cf2
+
446cf2
   /* Count the number of bits set in the masked value.  */
446cf2
   for (n = 0; (~((1ULL << n) - 1) & masked) != 0; ++n)
446cf2
     if ((masked & (1ULL << n)) != 0)
446cf2
@@ -74,10 +151,10 @@ _dl_important_hwcaps (size_t *sz, size_t *max_capstrlen)
446cf2
 
446cf2
   /* Determine the total size of all strings together.  */
446cf2
   if (cnt == 1)
446cf2
-    total = temp[0].len + 1;
446cf2
+    total += temp[0].len + 1;
446cf2
   else
446cf2
     {
446cf2
-      total = temp[0].len + temp[cnt - 1].len + 2;
446cf2
+      total += temp[0].len + temp[cnt - 1].len + 2;
446cf2
       if (cnt > 2)
446cf2
 	{
446cf2
 	  total <<= 1;
446cf2
@@ -94,26 +171,48 @@ _dl_important_hwcaps (size_t *sz, size_t *max_capstrlen)
446cf2
 	}
446cf2
     }
446cf2
 
446cf2
-  /* The result structure: we use a very compressed way to store the
446cf2
-     various combinations of capability names.  */
446cf2
-  *sz = 1 << cnt;
446cf2
-  result = (struct r_strlenpair *) malloc (*sz * sizeof (*result) + total);
446cf2
-  if (result == NULL)
446cf2
+  *sz = hwcaps_counts.count + (1 << cnt);
446cf2
+
446cf2
+  /* This is the overall result, including both glibc-hwcaps
446cf2
+     subdirectories and the legacy hwcaps subdirectories using the
446cf2
+     power set construction.  */
446cf2
+  struct r_strlenpair *overall_result
446cf2
+    = malloc (*sz * sizeof (*result) + total);
446cf2
+  if (overall_result == NULL)
446cf2
     _dl_signal_error (ENOMEM, NULL, NULL,
446cf2
 		      N_("cannot create capability list"));
446cf2
 
446cf2
+  /* Fill in the glibc-hwcaps subdirectories.  */
446cf2
+  {
446cf2
+    struct copy_hwcaps target;
446cf2
+    target.next_pair = overall_result;
446cf2
+    target.next_string = (char *) (overall_result + *sz);
446cf2
+    copy_hwcaps (&target, glibc_hwcaps_prepend, -1, NULL);
446cf2
+    copy_hwcaps (&target, _dl_hwcaps_subdirs,
446cf2
+		 hwcaps_subdirs_active, glibc_hwcaps_mask);
446cf2
+    /* Set up the write target for the power set construction.  */
446cf2
+    result = target.next_pair;
446cf2
+    cp = target.next_string;
446cf2
+  }
446cf2
+
446cf2
+
446cf2
+  /* Power set construction begins here.  We use a very compressed way
446cf2
+     to store the various combinations of capability names.  */
446cf2
+
446cf2
   if (cnt == 1)
446cf2
     {
446cf2
-      result[0].str = (char *) (result + *sz);
446cf2
+      result[0].str = cp;
446cf2
       result[0].len = temp[0].len + 1;
446cf2
-      result[1].str = (char *) (result + *sz);
446cf2
+      result[1].str = cp;
446cf2
       result[1].len = 0;
446cf2
-      cp = __mempcpy ((char *) (result + *sz), temp[0].str, temp[0].len);
446cf2
+      cp = __mempcpy (cp, temp[0].str, temp[0].len);
446cf2
       *cp = '/';
446cf2
-      *sz = 2;
446cf2
-      *max_capstrlen = result[0].len;
446cf2
+      if (result[0].len > hwcaps_counts.maximum_length)
446cf2
+	*max_capstrlen = result[0].len;
446cf2
+      else
446cf2
+	*max_capstrlen = hwcaps_counts.maximum_length;
446cf2
 
446cf2
-      return result;
446cf2
+      return overall_result;
446cf2
     }
446cf2
 
446cf2
   /* Fill in the information.  This follows the following scheme
446cf2
@@ -124,7 +223,7 @@ _dl_important_hwcaps (size_t *sz, size_t *max_capstrlen)
446cf2
 	      #3: 0, 3			1001
446cf2
      This allows the representation of all possible combinations of
446cf2
      capability names in the string.  First generate the strings.  */
446cf2
-  result[1].str = result[0].str = cp = (char *) (result + *sz);
446cf2
+  result[1].str = result[0].str = cp;
446cf2
 #define add(idx) \
446cf2
       cp = __mempcpy (__mempcpy (cp, temp[idx].str, temp[idx].len), "/", 1);
446cf2
   if (cnt == 2)
446cf2
@@ -191,7 +290,10 @@ _dl_important_hwcaps (size_t *sz, size_t *max_capstrlen)
446cf2
   while (--n != 0);
446cf2
 
446cf2
   /* The maximum string length.  */
446cf2
-  *max_capstrlen = result[0].len;
446cf2
+  if (result[0].len > hwcaps_counts.maximum_length)
446cf2
+    *max_capstrlen = result[0].len;
446cf2
+  else
446cf2
+    *max_capstrlen = hwcaps_counts.maximum_length;
446cf2
 
446cf2
-  return result;
446cf2
+  return overall_result;
446cf2
 }
446cf2
diff --git a/elf/dl-hwcaps.h b/elf/dl-hwcaps.h
446cf2
index d69ee11dc27bb5e5..3fcfbceb1a8fc1c8 100644
446cf2
--- a/elf/dl-hwcaps.h
446cf2
+++ b/elf/dl-hwcaps.h
446cf2
@@ -16,6 +16,11 @@
446cf2
    License along with the GNU C Library; if not, see
446cf2
    <http://www.gnu.org/licenses/>.  */
446cf2
 
446cf2
+#ifndef _DL_HWCAPS_H
446cf2
+#define _DL_HWCAPS_H
446cf2
+
446cf2
+#include <stdint.h>
446cf2
+
446cf2
 #include <elf/dl-tunables.h>
446cf2
 
446cf2
 #if HAVE_TUNABLES
446cf2
@@ -28,3 +33,103 @@
446cf2
 #  define GET_HWCAP_MASK() (0)
446cf2
 # endif
446cf2
 #endif
446cf2
+
446cf2
+#define GLIBC_HWCAPS_SUBDIRECTORY "glibc-hwcaps"
446cf2
+#define GLIBC_HWCAPS_PREFIX GLIBC_HWCAPS_SUBDIRECTORY "/"
446cf2
+
446cf2
+/* Used by _dl_hwcaps_split below, to split strings at ':'
446cf2
+   separators.  */
446cf2
+struct dl_hwcaps_split
446cf2
+{
446cf2
+  const char *segment;          /* Start of the current segment.  */
446cf2
+  size_t length;                /* Number of bytes until ':' or NUL.  */
446cf2
+};
446cf2
+
446cf2
+/* Prepare *S to parse SUBJECT, for future _dl_hwcaps_split calls.  If
446cf2
+   SUBJECT is NULL, it is treated as the empty string.  */
446cf2
+static inline void
446cf2
+_dl_hwcaps_split_init (struct dl_hwcaps_split *s, const char *subject)
446cf2
+{
446cf2
+  s->segment = subject;
446cf2
+  /* The initial call to _dl_hwcaps_split will not skip anything.  */
446cf2
+  s->length = 0;
446cf2
+}
446cf2
+
446cf2
+/* Extract the next non-empty string segment, up to ':' or the null
446cf2
+   terminator.  Return true if one more segment was found, or false if
446cf2
+   the end of the string was reached.  On success, S->segment is the
446cf2
+   start of the segment found, and S->length is its length.
446cf2
+   (Typically, S->segment[S->length] is not null.)  */
446cf2
+_Bool _dl_hwcaps_split (struct dl_hwcaps_split *s) attribute_hidden;
446cf2
+
446cf2
+/* Similar to dl_hwcaps_split, but with bit-based and name-based
446cf2
+   masking.  */
446cf2
+struct dl_hwcaps_split_masked
446cf2
+{
446cf2
+  struct dl_hwcaps_split split;
446cf2
+
446cf2
+  /* For used by the iterator implementation.  */
446cf2
+  const char *mask;
446cf2
+  uint32_t bitmask;
446cf2
+};
446cf2
+
446cf2
+/* Prepare *S for iteration with _dl_hwcaps_split_masked.  Only HWCAP
446cf2
+   names in SUBJECT whose bit is set in BITMASK and whose name is in
446cf2
+   MASK will be returned.  SUBJECT must not contain empty HWCAP names.
446cf2
+   If MASK is NULL, no name-based masking is applied.  Likewise for
446cf2
+   BITMASK if BITMASK is -1 (infinite number of bits).  */
446cf2
+static inline void
446cf2
+_dl_hwcaps_split_masked_init (struct dl_hwcaps_split_masked *s,
446cf2
+                              const char *subject,
446cf2
+                              uint32_t bitmask, const char *mask)
446cf2
+{
446cf2
+  _dl_hwcaps_split_init (&s->split, subject);
446cf2
+  s->bitmask = bitmask;
446cf2
+  s->mask = mask;
446cf2
+}
446cf2
+
446cf2
+/* Like _dl_hwcaps_split, but apply masking.  */
446cf2
+_Bool _dl_hwcaps_split_masked (struct dl_hwcaps_split_masked *s)
446cf2
+  attribute_hidden;
446cf2
+
446cf2
+/* Returns true if the colon-separated HWCAP list HWCAPS contains the
446cf2
+   capability NAME (with length NAME_LENGTH).  If HWCAPS is NULL, the
446cf2
+   function returns true.  */
446cf2
+_Bool _dl_hwcaps_contains (const char *hwcaps, const char *name,
446cf2
+                           size_t name_length) attribute_hidden;
446cf2
+
446cf2
+/* Colon-separated string of glibc-hwcaps subdirectories, without the
446cf2
+   "glibc-hwcaps/" prefix.  The most preferred subdirectory needs to
446cf2
+   be listed first.  Up to 32 subdirectories are supported, limited by
446cf2
+   the width of the uint32_t mask.  */
446cf2
+extern const char _dl_hwcaps_subdirs[] attribute_hidden;
446cf2
+
446cf2
+/* Returns a bitmap of active subdirectories in _dl_hwcaps_subdirs.
446cf2
+   Bit 0 (the LSB) corresponds to the first substring in
446cf2
+   _dl_hwcaps_subdirs, bit 1 to the second substring, and so on.
446cf2
+   There is no direct correspondence between HWCAP bitmasks and this
446cf2
+   bitmask.  */
446cf2
+uint32_t _dl_hwcaps_subdirs_active (void) attribute_hidden;
446cf2
+
446cf2
+/* Returns a bitmask that marks the last ACTIVE subdirectories in a
446cf2
+   _dl_hwcaps_subdirs_active string (containing SUBDIRS directories in
446cf2
+   total) as active.  Intended for use in _dl_hwcaps_subdirs_active
446cf2
+   implementations (if a contiguous tail of the list in
446cf2
+   _dl_hwcaps_subdirs is selected).  */
446cf2
+static inline uint32_t
446cf2
+_dl_hwcaps_subdirs_build_bitmask (int subdirs, int active)
446cf2
+{
446cf2
+  /* Leading subdirectories that are not active.  */
446cf2
+  int inactive = subdirs - active;
446cf2
+  if (inactive == 32)
446cf2
+    return 0;
446cf2
+
446cf2
+  uint32_t mask;
446cf2
+  if (subdirs != 32)
446cf2
+    mask = (1U << subdirs) - 1;
446cf2
+  else
446cf2
+    mask = -1;
446cf2
+  return mask ^ ((1U << inactive) - 1);
446cf2
+}
446cf2
+
446cf2
+#endif /* _DL_HWCAPS_H */
446cf2
diff --git a/elf/dl-hwcaps_split.c b/elf/dl-hwcaps_split.c
446cf2
new file mode 100644
446cf2
index 0000000000000000..95225e9f409ca229
446cf2
--- /dev/null
446cf2
+++ b/elf/dl-hwcaps_split.c
446cf2
@@ -0,0 +1,77 @@
446cf2
+/* Hardware capability support for run-time dynamic loader.  String splitting.
446cf2
+   Copyright (C) 2020 Free Software Foundation, Inc.
446cf2
+   This file is part of the GNU C Library.
446cf2
+
446cf2
+   The GNU C Library is free software; you can redistribute it and/or
446cf2
+   modify it under the terms of the GNU Lesser General Public
446cf2
+   License as published by the Free Software Foundation; either
446cf2
+   version 2.1 of the License, or (at your option) any later version.
446cf2
+
446cf2
+   The GNU C Library is distributed in the hope that it will be useful,
446cf2
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
446cf2
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
446cf2
+   Lesser General Public License for more details.
446cf2
+
446cf2
+   You should have received a copy of the GNU Lesser General Public
446cf2
+   License along with the GNU C Library; if not, see
446cf2
+   <https://www.gnu.org/licenses/>.  */
446cf2
+
446cf2
+#include <dl-hwcaps.h>
446cf2
+#include <stdbool.h>
446cf2
+#include <string.h>
446cf2
+
446cf2
+_Bool
446cf2
+_dl_hwcaps_split (struct dl_hwcaps_split *s)
446cf2
+{
446cf2
+  if (s->segment == NULL)
446cf2
+    return false;
446cf2
+
446cf2
+  /* Skip over the previous segment.   */
446cf2
+  s->segment += s->length;
446cf2
+
446cf2
+  /* Consume delimiters.  This also avoids returning an empty
446cf2
+     segment.  */
446cf2
+  while (*s->segment == ':')
446cf2
+    ++s->segment;
446cf2
+  if (*s->segment == '\0')
446cf2
+    return false;
446cf2
+
446cf2
+  /* This could use strchrnul, but we would have to link the function
446cf2
+     into ld.so for that.  */
446cf2
+  const char *colon = strchr (s->segment, ':');
446cf2
+  if (colon == NULL)
446cf2
+    s->length = strlen (s->segment);
446cf2
+  else
446cf2
+    s->length = colon - s->segment;
446cf2
+  return true;
446cf2
+}
446cf2
+
446cf2
+_Bool
446cf2
+_dl_hwcaps_split_masked (struct dl_hwcaps_split_masked *s)
446cf2
+{
446cf2
+  while (true)
446cf2
+    {
446cf2
+      if (!_dl_hwcaps_split (&s->split))
446cf2
+        return false;
446cf2
+      bool active = s->bitmask & 1;
446cf2
+      s->bitmask >>= 1;
446cf2
+      if (active && _dl_hwcaps_contains (s->mask,
446cf2
+                                         s->split.segment, s->split.length))
446cf2
+        return true;
446cf2
+    }
446cf2
+}
446cf2
+
446cf2
+_Bool
446cf2
+_dl_hwcaps_contains (const char *hwcaps, const char *name, size_t name_length)
446cf2
+{
446cf2
+  if (hwcaps == NULL)
446cf2
+    return true;
446cf2
+
446cf2
+  struct dl_hwcaps_split split;
446cf2
+  _dl_hwcaps_split_init (&split, hwcaps);
446cf2
+  while (_dl_hwcaps_split (&split))
446cf2
+    if (split.length == name_length
446cf2
+        && memcmp (split.segment, name, name_length) == 0)
446cf2
+      return true;
446cf2
+  return false;
446cf2
+}
446cf2
diff --git a/elf/dl-load.c b/elf/dl-load.c
446cf2
index d2be21ea7d1545fe..fee08d7816714178 100644
446cf2
--- a/elf/dl-load.c
446cf2
+++ b/elf/dl-load.c
446cf2
@@ -682,7 +682,9 @@ cache_rpath (struct link_map *l,
446cf2
 
446cf2
 
446cf2
 void
446cf2
-_dl_init_paths (const char *llp, const char *source)
446cf2
+_dl_init_paths (const char *llp, const char *source,
446cf2
+		const char *glibc_hwcaps_prepend,
446cf2
+		const char *glibc_hwcaps_mask)
446cf2
 {
446cf2
   size_t idx;
446cf2
   const char *strp;
446cf2
@@ -697,7 +699,8 @@ _dl_init_paths (const char *llp, const char *source)
446cf2
 
446cf2
 #ifdef SHARED
446cf2
   /* Get the capabilities.  */
446cf2
-  capstr = _dl_important_hwcaps (&ncapstr, &max_capstrlen);
446cf2
+  capstr = _dl_important_hwcaps (glibc_hwcaps_prepend, glibc_hwcaps_mask,
446cf2
+				 &ncapstr, &max_capstrlen);
446cf2
 #endif
446cf2
 
446cf2
   /* First set up the rest of the default search directory entries.  */
446cf2
diff --git a/elf/dl-main.h b/elf/dl-main.h
446cf2
index b51256d3b48230b0..566713a0d10cfdb7 100644
446cf2
--- a/elf/dl-main.h
446cf2
+++ b/elf/dl-main.h
446cf2
@@ -84,6 +84,14 @@ struct dl_main_state
446cf2
   /* The preload list passed as a command argument.  */
446cf2
   const char *preloadarg;
446cf2
 
446cf2
+  /* Additional glibc-hwcaps subdirectories to search first.
446cf2
+     Colon-separated list.  */
446cf2
+  const char *glibc_hwcaps_prepend;
446cf2
+
446cf2
+  /* Mask for the internal glibc-hwcaps subdirectories.
446cf2
+     Colon-separated list.  */
446cf2
+  const char *glibc_hwcaps_mask;
446cf2
+
446cf2
   enum rtld_mode mode;
446cf2
 
446cf2
   /* True if any of the debugging options is enabled.  */
446cf2
@@ -98,7 +106,8 @@ struct dl_main_state
446cf2
 static inline void
446cf2
 call_init_paths (const struct dl_main_state *state)
446cf2
 {
446cf2
-  _dl_init_paths (state->library_path, state->library_path_source);
446cf2
+  _dl_init_paths (state->library_path, state->library_path_source,
446cf2
+                  state->glibc_hwcaps_prepend, state->glibc_hwcaps_mask);
446cf2
 }
446cf2
 
446cf2
 /* Print ld.so usage information and exit.  */
446cf2
diff --git a/elf/dl-support.c b/elf/dl-support.c
446cf2
index fb9672367f8d6abd..34be8e5babfb6af3 100644
446cf2
--- a/elf/dl-support.c
446cf2
+++ b/elf/dl-support.c
446cf2
@@ -315,7 +315,10 @@ _dl_non_dynamic_init (void)
446cf2
 
446cf2
   /* Initialize the data structures for the search paths for shared
446cf2
      objects.  */
446cf2
-  _dl_init_paths (getenv ("LD_LIBRARY_PATH"), "LD_LIBRARY_PATH");
446cf2
+  _dl_init_paths (getenv ("LD_LIBRARY_PATH"), "LD_LIBRARY_PATH",
446cf2
+		  /* No glibc-hwcaps selection support in statically
446cf2
+		     linked binaries.  */
446cf2
+		  NULL, NULL);
446cf2
 
446cf2
   /* Remember the last search directory added at startup.  */
446cf2
   _dl_init_all_dirs = GL(dl_all_dirs);
446cf2
diff --git a/elf/dl-usage.c b/elf/dl-usage.c
446cf2
index 796ad38b43c2211b..e22a9c39427187d1 100644
446cf2
--- a/elf/dl-usage.c
446cf2
+++ b/elf/dl-usage.c
446cf2
@@ -83,7 +83,7 @@ print_search_path_for_help (struct dl_main_state *state)
446cf2
 {
446cf2
   if (__rtld_search_dirs.dirs == NULL)
446cf2
     /* The run-time search paths have not yet been initialized.  */
446cf2
-    _dl_init_paths (state->library_path, state->library_path_source);
446cf2
+    call_init_paths (state);
446cf2
 
446cf2
   _dl_printf ("\nShared library search path:\n");
446cf2
 
446cf2
@@ -132,6 +132,67 @@ print_hwcap_1_finish (bool *first)
446cf2
     _dl_printf (")\n");
446cf2
 }
446cf2
 
446cf2
+/* Print the header for print_hwcaps_subdirectories.  */
446cf2
+static void
446cf2
+print_hwcaps_subdirectories_header (bool *nothing_printed)
446cf2
+{
446cf2
+  if (*nothing_printed)
446cf2
+    {
446cf2
+      _dl_printf ("\n\
446cf2
+Subdirectories of glibc-hwcaps directories, in priority order:\n");
446cf2
+      *nothing_printed = false;
446cf2
+    }
446cf2
+}
446cf2
+
446cf2
+/* Print the HWCAP name itself, indented.  */
446cf2
+static void
446cf2
+print_hwcaps_subdirectories_name (const struct dl_hwcaps_split *split)
446cf2
+{
446cf2
+  _dl_write (STDOUT_FILENO, "  ", 2);
446cf2
+  _dl_write (STDOUT_FILENO, split->segment, split->length);
446cf2
+}
446cf2
+
446cf2
+/* Print the list of recognized glibc-hwcaps subdirectories.  */
446cf2
+static void
446cf2
+print_hwcaps_subdirectories (const struct dl_main_state *state)
446cf2
+{
446cf2
+  bool nothing_printed = true;
446cf2
+  struct dl_hwcaps_split split;
446cf2
+
446cf2
+  /* The prepended glibc-hwcaps subdirectories.  */
446cf2
+  _dl_hwcaps_split_init (&split, state->glibc_hwcaps_prepend);
446cf2
+  while (_dl_hwcaps_split (&split))
446cf2
+    {
446cf2
+      print_hwcaps_subdirectories_header (&nothing_printed);
446cf2
+      print_hwcaps_subdirectories_name (&split);
446cf2
+      bool first = true;
446cf2
+      print_hwcap_1 (&first, true, "searched");
446cf2
+      print_hwcap_1_finish (&first);
446cf2
+    }
446cf2
+
446cf2
+  /* The built-in glibc-hwcaps subdirectories.  Do the filtering
446cf2
+     manually, so that more precise diagnostics are possible.  */
446cf2
+  uint32_t mask = _dl_hwcaps_subdirs_active ();
446cf2
+  _dl_hwcaps_split_init (&split, _dl_hwcaps_subdirs);
446cf2
+  while (_dl_hwcaps_split (&split))
446cf2
+    {
446cf2
+      print_hwcaps_subdirectories_header (&nothing_printed);
446cf2
+      print_hwcaps_subdirectories_name (&split);
446cf2
+      bool first = true;
446cf2
+      print_hwcap_1 (&first, mask & 1, "supported");
446cf2
+      bool listed = _dl_hwcaps_contains (state->glibc_hwcaps_mask,
446cf2
+                                         split.segment, split.length);
446cf2
+      print_hwcap_1 (&first, !listed, "masked");
446cf2
+      print_hwcap_1 (&first, (mask & 1) && listed, "searched");
446cf2
+      print_hwcap_1_finish (&first);
446cf2
+      mask >>= 1;
446cf2
+    }
446cf2
+
446cf2
+  if (nothing_printed)
446cf2
+    _dl_printf ("\n\
446cf2
+No subdirectories of glibc-hwcaps directories are searched.\n");
446cf2
+}
446cf2
+
446cf2
 /* Write a list of hwcap subdirectories to standard output.  See
446cf2
  _dl_important_hwcaps in dl-hwcaps.c.  */
446cf2
 static void
446cf2
@@ -186,6 +247,10 @@ setting environment variables (which would be inherited by subprocesses).\n\
446cf2
   --inhibit-cache       Do not use " LD_SO_CACHE "\n\
446cf2
   --library-path PATH   use given PATH instead of content of the environment\n\
446cf2
                         variable LD_LIBRARY_PATH\n\
446cf2
+  --glibc-hwcaps-prepend LIST\n\
446cf2
+                        search glibc-hwcaps subdirectories in LIST\n\
446cf2
+  --glibc-hwcaps-mask LIST\n\
446cf2
+                        only search built-in subdirectories if in LIST\n\
446cf2
   --inhibit-rpath LIST  ignore RUNPATH and RPATH information in object names\n\
446cf2
                         in LIST\n\
446cf2
   --audit LIST          use objects named in LIST as auditors\n\
446cf2
@@ -198,6 +263,7 @@ This program interpreter self-identifies as: " RTLD "\n\
446cf2
 ",
446cf2
               argv0);
446cf2
   print_search_path_for_help (state);
446cf2
+  print_hwcaps_subdirectories (state);
446cf2
   print_legacy_hwcap_directories ();
446cf2
   _exit (EXIT_SUCCESS);
446cf2
 }
446cf2
diff --git a/elf/markermodMARKER-VALUE.c b/elf/markermodMARKER-VALUE.c
446cf2
new file mode 100644
446cf2
index 0000000000000000..99bdcf71a4e219c6
446cf2
--- /dev/null
446cf2
+++ b/elf/markermodMARKER-VALUE.c
446cf2
@@ -0,0 +1,29 @@
446cf2
+/* Source file template for building shared objects with marker functions.
446cf2
+   Copyright (C) 2020 Free Software Foundation, Inc.
446cf2
+   This file is part of the GNU C Library.
446cf2
+
446cf2
+   The GNU C Library is free software; you can redistribute it and/or
446cf2
+   modify it under the terms of the GNU Lesser General Public
446cf2
+   License as published by the Free Software Foundation; either
446cf2
+   version 2.1 of the License, or (at your option) any later version.
446cf2
+
446cf2
+   The GNU C Library is distributed in the hope that it will be useful,
446cf2
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
446cf2
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
446cf2
+   Lesser General Public License for more details.
446cf2
+
446cf2
+   You should have received a copy of the GNU Lesser General Public
446cf2
+   License along with the GNU C Library; if not, see
446cf2
+   <https://www.gnu.org/licenses/>.  */
446cf2
+
446cf2
+/* MARKER and VALUE must be set on the compiler command line.  */
446cf2
+
446cf2
+#ifndef MARKER
446cf2
+# error MARKER not defined
446cf2
+#endif
446cf2
+
446cf2
+int
446cf2
+MARKER (void)
446cf2
+{
446cf2
+  return VALUE;
446cf2
+}
446cf2
diff --git a/elf/rtld.c b/elf/rtld.c
446cf2
index da1eef108508b95f..fde5a6a4a485207e 100644
446cf2
--- a/elf/rtld.c
446cf2
+++ b/elf/rtld.c
446cf2
@@ -287,6 +287,8 @@ dl_main_state_init (struct dl_main_state *state)
446cf2
   state->library_path_source = NULL;
446cf2
   state->preloadlist = NULL;
446cf2
   state->preloadarg = NULL;
446cf2
+  state->glibc_hwcaps_prepend = NULL;
446cf2
+  state->glibc_hwcaps_mask = NULL;
446cf2
   state->mode = rtld_mode_normal;
446cf2
   state->any_debug = false;
446cf2
   state->version_info = false;
446cf2
@@ -1238,6 +1240,22 @@ dl_main (const ElfW(Phdr) *phdr,
446cf2
 	  {
446cf2
 	    argv0 = _dl_argv[2];
446cf2
 
446cf2
+	    _dl_skip_args += 2;
446cf2
+	    _dl_argc -= 2;
446cf2
+	    _dl_argv += 2;
446cf2
+	  }
446cf2
+	else if (strcmp (_dl_argv[1], "--glibc-hwcaps-prepend") == 0
446cf2
+		 && _dl_argc > 2)
446cf2
+	  {
446cf2
+	    state.glibc_hwcaps_prepend = _dl_argv[2];
446cf2
+	    _dl_skip_args += 2;
446cf2
+	    _dl_argc -= 2;
446cf2
+	    _dl_argv += 2;
446cf2
+	  }
446cf2
+	else if (strcmp (_dl_argv[1], "--glibc-hwcaps-mask") == 0
446cf2
+		 && _dl_argc > 2)
446cf2
+	  {
446cf2
+	    state.glibc_hwcaps_mask = _dl_argv[2];
446cf2
 	    _dl_skip_args += 2;
446cf2
 	    _dl_argc -= 2;
446cf2
 	    _dl_argv += 2;
446cf2
diff --git a/elf/tst-dl-hwcaps_split.c b/elf/tst-dl-hwcaps_split.c
446cf2
new file mode 100644
446cf2
index 0000000000000000..364159427074bd1c
446cf2
--- /dev/null
446cf2
+++ b/elf/tst-dl-hwcaps_split.c
446cf2
@@ -0,0 +1,148 @@
446cf2
+/* Unit tests for dl-hwcaps.c.
446cf2
+   Copyright (C) 2020 Free Software Foundation, Inc.
446cf2
+   This file is part of the GNU C Library.
446cf2
+
446cf2
+   The GNU C Library is free software; you can redistribute it and/or
446cf2
+   modify it under the terms of the GNU Lesser General Public
446cf2
+   License as published by the Free Software Foundation; either
446cf2
+   version 2.1 of the License, or (at your option) any later version.
446cf2
+
446cf2
+   The GNU C Library is distributed in the hope that it will be useful,
446cf2
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
446cf2
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
446cf2
+   Lesser General Public License for more details.
446cf2
+
446cf2
+   You should have received a copy of the GNU Lesser General Public
446cf2
+   License along with the GNU C Library; if not, see
446cf2
+   <https://www.gnu.org/licenses/>.  */
446cf2
+
446cf2
+#include <array_length.h>
446cf2
+#include <dl-hwcaps.h>
446cf2
+#include <string.h>
446cf2
+#include <support/check.h>
446cf2
+
446cf2
+static void
446cf2
+check_split_masked (const char *input, int32_t bitmask, const char *mask,
446cf2
+                    const char *expected[], size_t expected_length)
446cf2
+{
446cf2
+  struct dl_hwcaps_split_masked split;
446cf2
+  _dl_hwcaps_split_masked_init (&split, input, bitmask, mask);
446cf2
+  size_t index = 0;
446cf2
+  while (_dl_hwcaps_split_masked (&split))
446cf2
+    {
446cf2
+      TEST_VERIFY_EXIT (index < expected_length);
446cf2
+      TEST_COMPARE_BLOB (expected[index], strlen (expected[index]),
446cf2
+                         split.split.segment, split.split.length);
446cf2
+      ++index;
446cf2
+    }
446cf2
+  TEST_COMPARE (index, expected_length);
446cf2
+}
446cf2
+
446cf2
+static void
446cf2
+check_split (const char *input,
446cf2
+             const char *expected[], size_t expected_length)
446cf2
+{
446cf2
+  struct dl_hwcaps_split split;
446cf2
+  _dl_hwcaps_split_init (&split, input);
446cf2
+  size_t index = 0;
446cf2
+  while (_dl_hwcaps_split (&split))
446cf2
+    {
446cf2
+      TEST_VERIFY_EXIT (index < expected_length);
446cf2
+      TEST_COMPARE_BLOB (expected[index], strlen (expected[index]),
446cf2
+                         split.segment, split.length);
446cf2
+      ++index;
446cf2
+    }
446cf2
+  TEST_COMPARE (index, expected_length);
446cf2
+
446cf2
+  /* Reuse the test cases with masking that does not actually remove
446cf2
+     anything.  */
446cf2
+  check_split_masked (input, -1, NULL, expected, expected_length);
446cf2
+  check_split_masked (input, -1, input, expected, expected_length);
446cf2
+}
446cf2
+
446cf2
+static int
446cf2
+do_test (void)
446cf2
+{
446cf2
+  /* Splitting tests, without masking.  */
446cf2
+  check_split (NULL, NULL, 0);
446cf2
+  check_split ("", NULL, 0);
446cf2
+  check_split (":", NULL, 0);
446cf2
+  check_split ("::", NULL, 0);
446cf2
+
446cf2
+  {
446cf2
+    const char *expected[] = { "first" };
446cf2
+    check_split ("first", expected, array_length (expected));
446cf2
+    check_split (":first", expected, array_length (expected));
446cf2
+    check_split ("first:", expected, array_length (expected));
446cf2
+    check_split (":first:", expected, array_length (expected));
446cf2
+  }
446cf2
+
446cf2
+  {
446cf2
+    const char *expected[] = { "first", "second" };
446cf2
+    check_split ("first:second", expected, array_length (expected));
446cf2
+    check_split ("first::second", expected, array_length (expected));
446cf2
+    check_split (":first:second", expected, array_length (expected));
446cf2
+    check_split ("first:second:", expected, array_length (expected));
446cf2
+    check_split (":first:second:", expected, array_length (expected));
446cf2
+  }
446cf2
+
446cf2
+  /* Splitting tests with masking.  */
446cf2
+  {
446cf2
+    const char *expected[] = { "first" };
446cf2
+    check_split_masked ("first", 3, "first:second",
446cf2
+                        expected, array_length (expected));
446cf2
+    check_split_masked ("first:second", 3, "first:",
446cf2
+                        expected, array_length (expected));
446cf2
+    check_split_masked ("first:second", 1, NULL,
446cf2
+                        expected, array_length (expected));
446cf2
+  }
446cf2
+  {
446cf2
+    const char *expected[] = { "second" };
446cf2
+    check_split_masked ("first:second", 3, "second",
446cf2
+                        expected, array_length (expected));
446cf2
+    check_split_masked ("first:second:third", -1, "second:",
446cf2
+                        expected, array_length (expected));
446cf2
+    check_split_masked ("first:second", 2, NULL,
446cf2
+                        expected, array_length (expected));
446cf2
+    check_split_masked ("first:second:third", 2, "first:second",
446cf2
+                        expected, array_length (expected));
446cf2
+  }
446cf2
+
446cf2
+  /* Tests for _dl_hwcaps_contains.  */
446cf2
+  TEST_VERIFY (_dl_hwcaps_contains (NULL, "first", strlen ("first")));
446cf2
+  TEST_VERIFY (_dl_hwcaps_contains (NULL, "", 0));
446cf2
+  TEST_VERIFY (! _dl_hwcaps_contains ("", "first", strlen ("first")));
446cf2
+  TEST_VERIFY (! _dl_hwcaps_contains ("firs", "first", strlen ("first")));
446cf2
+  TEST_VERIFY (_dl_hwcaps_contains ("firs", "first", strlen ("first") - 1));
446cf2
+  for (int i = 0; i < strlen ("first"); ++i)
446cf2
+    TEST_VERIFY (! _dl_hwcaps_contains ("first", "first", i));
446cf2
+  TEST_VERIFY (_dl_hwcaps_contains ("first", "first", strlen ("first")));
446cf2
+  TEST_VERIFY (_dl_hwcaps_contains ("first:", "first", strlen ("first")));
446cf2
+  TEST_VERIFY (_dl_hwcaps_contains ("first:second",
446cf2
+                                    "first", strlen ("first")));
446cf2
+  TEST_VERIFY (_dl_hwcaps_contains (":first:second", "first",
446cf2
+                                    strlen ("first")));
446cf2
+  TEST_VERIFY (_dl_hwcaps_contains ("first:second", "second",
446cf2
+                                    strlen ("second")));
446cf2
+  TEST_VERIFY (_dl_hwcaps_contains ("first:second:", "second",
446cf2
+                                    strlen ("second")));
446cf2
+  TEST_VERIFY (_dl_hwcaps_contains ("first::second:", "second",
446cf2
+                                    strlen ("second")));
446cf2
+  TEST_VERIFY (_dl_hwcaps_contains ("first:second::", "second",
446cf2
+                                    strlen ("second")));
446cf2
+  for (int i = 0; i < strlen ("second"); ++i)
446cf2
+    {
446cf2
+      TEST_VERIFY (!_dl_hwcaps_contains ("first:second", "second", i));
446cf2
+      TEST_VERIFY (!_dl_hwcaps_contains ("first:second:", "second", i));
446cf2
+      TEST_VERIFY (!_dl_hwcaps_contains ("first:second::", "second", i));
446cf2
+      TEST_VERIFY (!_dl_hwcaps_contains ("first::second", "second", i));
446cf2
+    }
446cf2
+
446cf2
+  return 0;
446cf2
+}
446cf2
+
446cf2
+#include <support/test-driver.c>
446cf2
+
446cf2
+/* Rebuild the sources here because the object file is built for
446cf2
+   inclusion into the dynamic loader.  */
446cf2
+#include "dl-hwcaps_split.c"
446cf2
diff --git a/elf/tst-glibc-hwcaps-mask.c b/elf/tst-glibc-hwcaps-mask.c
446cf2
new file mode 100644
446cf2
index 0000000000000000..27b09b358caf7853
446cf2
--- /dev/null
446cf2
+++ b/elf/tst-glibc-hwcaps-mask.c
446cf2
@@ -0,0 +1,31 @@
446cf2
+/* Test that --glibc-hwcaps-mask works.
446cf2
+   Copyright (C) 2020 Free Software Foundation, Inc.
446cf2
+   This file is part of the GNU C Library.
446cf2
+
446cf2
+   The GNU C Library is free software; you can redistribute it and/or
446cf2
+   modify it under the terms of the GNU Lesser General Public
446cf2
+   License as published by the Free Software Foundation; either
446cf2
+   version 2.1 of the License, or (at your option) any later version.
446cf2
+
446cf2
+   The GNU C Library is distributed in the hope that it will be useful,
446cf2
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
446cf2
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
446cf2
+   Lesser General Public License for more details.
446cf2
+
446cf2
+   You should have received a copy of the GNU Lesser General Public
446cf2
+   License along with the GNU C Library; if not, see
446cf2
+   <https://www.gnu.org/licenses/>.  */
446cf2
+
446cf2
+#include <support/check.h>
446cf2
+
446cf2
+extern int marker1 (void);
446cf2
+
446cf2
+static int
446cf2
+do_test (void)
446cf2
+{
446cf2
+  /* The marker1 function in elf/markermod1.so returns 1.  */
446cf2
+  TEST_COMPARE (marker1 (), 1);
446cf2
+  return 0;
446cf2
+}
446cf2
+
446cf2
+#include <support/test-driver.c>
446cf2
diff --git a/elf/tst-glibc-hwcaps-prepend.c b/elf/tst-glibc-hwcaps-prepend.c
446cf2
new file mode 100644
446cf2
index 0000000000000000..57d7319f1484ca4b
446cf2
--- /dev/null
446cf2
+++ b/elf/tst-glibc-hwcaps-prepend.c
446cf2
@@ -0,0 +1,32 @@
446cf2
+/* Test that --glibc-hwcaps-prepend works.
446cf2
+   Copyright (C) 2020 Free Software Foundation, Inc.
446cf2
+   This file is part of the GNU C Library.
446cf2
+
446cf2
+   The GNU C Library is free software; you can redistribute it and/or
446cf2
+   modify it under the terms of the GNU Lesser General Public
446cf2
+   License as published by the Free Software Foundation; either
446cf2
+   version 2.1 of the License, or (at your option) any later version.
446cf2
+
446cf2
+   The GNU C Library is distributed in the hope that it will be useful,
446cf2
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
446cf2
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
446cf2
+   Lesser General Public License for more details.
446cf2
+
446cf2
+   You should have received a copy of the GNU Lesser General Public
446cf2
+   License along with the GNU C Library; if not, see
446cf2
+   <https://www.gnu.org/licenses/>.  */
446cf2
+
446cf2
+#include <support/check.h>
446cf2
+
446cf2
+extern int marker1 (void);
446cf2
+
446cf2
+static int
446cf2
+do_test (void)
446cf2
+{
446cf2
+  /* The marker1 function in
446cf2
+     glibc-hwcaps/prepend-markermod1/markermod1.so returns 2.  */
446cf2
+  TEST_COMPARE (marker1 (), 2);
446cf2
+  return 0;
446cf2
+}
446cf2
+
446cf2
+#include <support/test-driver.c>
446cf2
diff --git a/elf/tst-glibc-hwcaps.c b/elf/tst-glibc-hwcaps.c
446cf2
new file mode 100644
446cf2
index 0000000000000000..28f47cf8914a1f2a
446cf2
--- /dev/null
446cf2
+++ b/elf/tst-glibc-hwcaps.c
446cf2
@@ -0,0 +1,28 @@
446cf2
+/* Stub test for glibc-hwcaps.
446cf2
+   Copyright (C) 2020 Free Software Foundation, Inc.
446cf2
+   This file is part of the GNU C Library.
446cf2
+
446cf2
+   The GNU C Library is free software; you can redistribute it and/or
446cf2
+   modify it under the terms of the GNU Lesser General Public
446cf2
+   License as published by the Free Software Foundation; either
446cf2
+   version 2.1 of the License, or (at your option) any later version.
446cf2
+
446cf2
+   The GNU C Library is distributed in the hope that it will be useful,
446cf2
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
446cf2
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
446cf2
+   Lesser General Public License for more details.
446cf2
+
446cf2
+   You should have received a copy of the GNU Lesser General Public
446cf2
+   License along with the GNU C Library; if not, see
446cf2
+   <https://www.gnu.org/licenses/>.  */
446cf2
+
446cf2
+#include <stdio.h>
446cf2
+
446cf2
+static int
446cf2
+do_test (void)
446cf2
+{
446cf2
+  puts ("info: generic tst-glibc-hwcaps (tests nothing)");
446cf2
+  return 0;
446cf2
+}
446cf2
+
446cf2
+#include <support/test-driver.c>
446cf2
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
446cf2
index 2c9fdeb286bdaadf..77923499d3de4366 100644
446cf2
--- a/sysdeps/generic/ldsodefs.h
446cf2
+++ b/sysdeps/generic/ldsodefs.h
446cf2
@@ -1045,8 +1045,13 @@ extern struct r_debug *_dl_debug_initialize (ElfW(Addr) ldbase, Lmid_t ns)
446cf2
      attribute_hidden;
446cf2
 
446cf2
 /* Initialize the basic data structure for the search paths.  SOURCE
446cf2
-   is either "LD_LIBRARY_PATH" or "--library-path".  */
446cf2
-extern void _dl_init_paths (const char *library_path, const char *source)
446cf2
+   is either "LD_LIBRARY_PATH" or "--library-path".
446cf2
+   GLIBC_HWCAPS_PREPEND adds additional glibc-hwcaps subdirectories to
446cf2
+   search.  GLIBC_HWCAPS_MASK is used to filter the built-in
446cf2
+   subdirectories if not NULL.  */
446cf2
+extern void _dl_init_paths (const char *library_path, const char *source,
446cf2
+			    const char *glibc_hwcaps_prepend,
446cf2
+			    const char *glibc_hwcaps_mask)
446cf2
   attribute_hidden;
446cf2
 
446cf2
 /* Gather the information needed to install the profiling tables and start
446cf2
@@ -1070,9 +1075,14 @@ extern void _dl_show_auxv (void) attribute_hidden;
446cf2
 extern char *_dl_next_ld_env_entry (char ***position) attribute_hidden;
446cf2
 
446cf2
 /* Return an array with the names of the important hardware
446cf2
-   capabilities.  The length of the array is written to *SZ, and the
446cf2
-   maximum of all strings length is written to *MAX_CAPSTRLEN.  */
446cf2
-const struct r_strlenpair *_dl_important_hwcaps (size_t *sz,
446cf2
+   capabilities.  PREPEND is a colon-separated list of glibc-hwcaps
446cf2
+   directories to search first.  MASK is a colon-separated list used
446cf2
+   to filter the built-in glibc-hwcaps subdirectories.  The length of
446cf2
+   the array is written to *SZ, and the maximum of all strings length
446cf2
+   is written to *MAX_CAPSTRLEN.  */
446cf2
+const struct r_strlenpair *_dl_important_hwcaps (const char *prepend,
446cf2
+						 const char *mask,
446cf2
+						 size_t *sz,
446cf2
 						 size_t *max_capstrlen)
446cf2
   attribute_hidden;
446cf2