b1dca6
commit de1a9197af7f67a89f929dcadb8ceca8c3846b1c
b1dca6
Author: Florian Weimer <fweimer@redhat.com>
b1dca6
Date:   Fri Oct 30 11:57:59 2020 +0100
b1dca6
b1dca6
    elf: Unify old and new format cache handling code in ld.so
b1dca6
    
b1dca6
    struct file_entry_new starts with the fields of struct file_entry,
b1dca6
    so the code can be shared if the size computation is made dynamic.
b1dca6
    
b1dca6
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
b1dca6
b1dca6
diff --git a/elf/dl-cache.c b/elf/dl-cache.c
b1dca6
index ef37ca18fa9fb6e0..366a051dfcd26132 100644
b1dca6
--- a/elf/dl-cache.c
b1dca6
+++ b/elf/dl-cache.c
b1dca6
@@ -35,103 +35,141 @@ static struct cache_file *cache;
b1dca6
 static struct cache_file_new *cache_new;
b1dca6
 static size_t cachesize;
b1dca6
 
b1dca6
-/* 1 if cache_data + PTR points into the cache.  */
b1dca6
-#define _dl_cache_verify_ptr(ptr) (ptr < cache_data_size)
b1dca6
-
b1dca6
-#define SEARCH_CACHE(cache) \
b1dca6
-/* We use binary search since the table is sorted in the cache file.	      \
b1dca6
-   The first matching entry in the table is returned.			      \
b1dca6
-   It is important to use the same algorithm as used while generating	      \
b1dca6
-   the cache file.  */							      \
b1dca6
-do									      \
b1dca6
-  {									      \
b1dca6
-    left = 0;								      \
b1dca6
-    right = cache->nlibs - 1;						      \
b1dca6
-									      \
b1dca6
-    while (left <= right)						      \
b1dca6
-      {									      \
b1dca6
-	__typeof__ (cache->libs[0].key) key;				      \
b1dca6
-									      \
b1dca6
-	middle = (left + right) / 2;					      \
b1dca6
-									      \
b1dca6
-	key = cache->libs[middle].key;					      \
b1dca6
-									      \
b1dca6
-	/* Make sure string table indices are not bogus before using	      \
b1dca6
-	   them.  */							      \
b1dca6
-	if (! _dl_cache_verify_ptr (key))				      \
b1dca6
-	  {								      \
b1dca6
-	    cmpres = 1;							      \
b1dca6
-	    break;							      \
b1dca6
-	  }								      \
b1dca6
-									      \
b1dca6
-	/* Actually compare the entry with the key.  */			      \
b1dca6
-	cmpres = _dl_cache_libcmp (name, cache_data + key);		      \
b1dca6
-	if (__glibc_unlikely (cmpres == 0))				      \
b1dca6
-	  {								      \
b1dca6
-	    /* Found it.  LEFT now marks the last entry for which we	      \
b1dca6
-	       know the name is correct.  */				      \
b1dca6
-	    left = middle;						      \
b1dca6
-									      \
b1dca6
-	    /* There might be entries with this name before the one we	      \
b1dca6
-	       found.  So we have to find the beginning.  */		      \
b1dca6
-	    while (middle > 0)						      \
b1dca6
-	      {								      \
b1dca6
-		__typeof__ (cache->libs[0].key) key;			      \
b1dca6
-									      \
b1dca6
-		key = cache->libs[middle - 1].key;			      \
b1dca6
-		/* Make sure string table indices are not bogus before	      \
b1dca6
-		   using them.  */					      \
b1dca6
-		if (! _dl_cache_verify_ptr (key)			      \
b1dca6
-		    /* Actually compare the entry.  */			      \
b1dca6
-		    || _dl_cache_libcmp (name, cache_data + key) != 0)	      \
b1dca6
-		  break;						      \
b1dca6
-		--middle;						      \
b1dca6
-	      }								      \
b1dca6
-									      \
b1dca6
-	    do								      \
b1dca6
-	      {								      \
b1dca6
-		int flags;						      \
b1dca6
-		__typeof__ (cache->libs[0]) *lib = &cache->libs[middle];      \
b1dca6
-									      \
b1dca6
-		/* Only perform the name test if necessary.  */		      \
b1dca6
-		if (middle > left					      \
b1dca6
-		    /* We haven't seen this string so far.  Test whether the  \
b1dca6
-		       index is ok and whether the name matches.  Otherwise   \
b1dca6
-		       we are done.  */					      \
b1dca6
-		    && (! _dl_cache_verify_ptr (lib->key)		      \
b1dca6
-			|| (_dl_cache_libcmp (name, cache_data + lib->key)    \
b1dca6
-			    != 0)))					      \
b1dca6
-		  break;						      \
b1dca6
-									      \
b1dca6
-		flags = lib->flags;					      \
b1dca6
-		if (_dl_cache_check_flags (flags)			      \
b1dca6
-		    && _dl_cache_verify_ptr (lib->value))		      \
b1dca6
-		  {							      \
b1dca6
-		    if (best == NULL || flags == GLRO(dl_correct_cache_id))   \
b1dca6
-		      {							      \
b1dca6
-			HWCAP_CHECK;					      \
b1dca6
-			best = cache_data + lib->value;			      \
b1dca6
-									      \
b1dca6
-			if (flags == GLRO(dl_correct_cache_id))		      \
b1dca6
-			  /* We've found an exact match for the shared	      \
b1dca6
-			     object and no general `ELF' release.  Stop	      \
b1dca6
-			     searching.  */				      \
b1dca6
-			  break;					      \
b1dca6
-		      }							      \
b1dca6
-		  }							      \
b1dca6
-	      }								      \
b1dca6
-	    while (++middle <= right);					      \
b1dca6
-	    break;							      \
b1dca6
-	}								      \
b1dca6
-									      \
b1dca6
-	if (cmpres < 0)							      \
b1dca6
-	  left = middle + 1;						      \
b1dca6
-	else								      \
b1dca6
-	  right = middle - 1;						      \
b1dca6
-      }									      \
b1dca6
-  }									      \
b1dca6
-while (0)
b1dca6
+/* True if PTR is a valid string table index.  */
b1dca6
+static inline bool
b1dca6
+_dl_cache_verify_ptr (uint32_t ptr, size_t string_table_size)
b1dca6
+{
b1dca6
+  return ptr < string_table_size;
b1dca6
+}
b1dca6
+
b1dca6
+/* Compute the address of the element INDEX of the array at LIBS.
b1dca6
+   Conceptually, this is &LIBS[INDEX], but use ENTRY_SIZE for the size
b1dca6
+   of *LIBS.  */
b1dca6
+static inline const struct file_entry *
b1dca6
+_dl_cache_file_entry (const struct file_entry *libs, size_t entry_size,
b1dca6
+		      size_t index)
b1dca6
+{
b1dca6
+  return (const void *) libs + index * entry_size;
b1dca6
+}
b1dca6
+
b1dca6
+/* We use binary search since the table is sorted in the cache file.
b1dca6
+   The first matching entry in the table is returned.  It is important
b1dca6
+   to use the same algorithm as used while generating the cache file.
b1dca6
+   STRING_TABLE_SIZE indicates the maximum offset in STRING_TABLE at
b1dca6
+   which data is mapped; it is not exact.  */
b1dca6
+static const char *
b1dca6
+search_cache (const char *string_table, uint32_t string_table_size,
b1dca6
+	      struct file_entry *libs, uint32_t nlibs, uint32_t entry_size,
b1dca6
+	      const char *name)
b1dca6
+{
b1dca6
+  /* Used by the HWCAP check in the struct file_entry_new case.  */
b1dca6
+  uint64_t platform = _dl_string_platform (GLRO (dl_platform));
b1dca6
+  if (platform != (uint64_t) -1)
b1dca6
+    platform = 1ULL << platform;
b1dca6
+  uint64_t hwcap_mask = GET_HWCAP_MASK ();
b1dca6
+#define _DL_HWCAP_TLS_MASK (1LL << 63)
b1dca6
+  uint64_t hwcap_exclude = ~((GLRO (dl_hwcap) & hwcap_mask)
b1dca6
+			     | _DL_HWCAP_PLATFORM | _DL_HWCAP_TLS_MASK);
b1dca6
+
b1dca6
+  int left = 0;
b1dca6
+  int right = nlibs - 1;
b1dca6
+  const char *best = NULL;
b1dca6
+
b1dca6
+  while (left <= right)
b1dca6
+    {
b1dca6
+      int middle = (left + right) / 2;
b1dca6
+      uint32_t key = _dl_cache_file_entry (libs, entry_size, middle)->key;
b1dca6
+
b1dca6
+      /* Make sure string table indices are not bogus before using
b1dca6
+	 them.  */
b1dca6
+      if (!_dl_cache_verify_ptr (key, string_table_size))
b1dca6
+	return NULL;
b1dca6
+
b1dca6
+      /* Actually compare the entry with the key.  */
b1dca6
+      int cmpres = _dl_cache_libcmp (name, string_table + key);
b1dca6
+      if (__glibc_unlikely (cmpres == 0))
b1dca6
+	{
b1dca6
+	  /* Found it.  LEFT now marks the last entry for which we
b1dca6
+	     know the name is correct.  */
b1dca6
+	  left = middle;
b1dca6
+
b1dca6
+	  /* There might be entries with this name before the one we
b1dca6
+	     found.  So we have to find the beginning.  */
b1dca6
+	  while (middle > 0)
b1dca6
+	    {
b1dca6
+	      key = _dl_cache_file_entry (libs, entry_size, middle - 1)->key;
b1dca6
+	      /* Make sure string table indices are not bogus before
b1dca6
+		 using them.  */
b1dca6
+	      if (!_dl_cache_verify_ptr (key, string_table_size)
b1dca6
+		  /* Actually compare the entry.  */
b1dca6
+		  || _dl_cache_libcmp (name, string_table + key) != 0)
b1dca6
+		break;
b1dca6
+	      --middle;
b1dca6
+	    }
b1dca6
+
b1dca6
+	  do
b1dca6
+	    {
b1dca6
+	      int flags;
b1dca6
+	      const struct file_entry *lib
b1dca6
+		= _dl_cache_file_entry (libs, entry_size, middle);
b1dca6
+
b1dca6
+	      /* Only perform the name test if necessary.  */
b1dca6
+	      if (middle > left
b1dca6
+		  /* We haven't seen this string so far.  Test whether the
b1dca6
+		     index is ok and whether the name matches.  Otherwise
b1dca6
+		     we are done.  */
b1dca6
+		  && (! _dl_cache_verify_ptr (lib->key, string_table_size)
b1dca6
+		      || (_dl_cache_libcmp (name, string_table + lib->key)
b1dca6
+			  != 0)))
b1dca6
+		break;
b1dca6
+
b1dca6
+	      flags = lib->flags;
b1dca6
+	      if (_dl_cache_check_flags (flags)
b1dca6
+		  && _dl_cache_verify_ptr (lib->value, string_table_size))
b1dca6
+		{
b1dca6
+		  if (best == NULL || flags == GLRO (dl_correct_cache_id))
b1dca6
+		    {
b1dca6
+		      if (entry_size >= sizeof (struct file_entry_new))
b1dca6
+			{
b1dca6
+			  /* The entry is large enough to include
b1dca6
+			     HWCAP data.  Check it.  */
b1dca6
+			  struct file_entry_new *libnew
b1dca6
+			    = (struct file_entry_new *) lib;
b1dca6
+
b1dca6
+			  if (libnew->hwcap & hwcap_exclude)
b1dca6
+			    continue;
b1dca6
+			  if (GLRO (dl_osversion)
b1dca6
+			      && libnew->osversion > GLRO (dl_osversion))
b1dca6
+			    continue;
b1dca6
+			  if (_DL_PLATFORMS_COUNT
b1dca6
+			      && (libnew->hwcap & _DL_HWCAP_PLATFORM) != 0
b1dca6
+			      && ((libnew->hwcap & _DL_HWCAP_PLATFORM)
b1dca6
+				  != platform))
b1dca6
+			    continue;
b1dca6
+			}
b1dca6
+
b1dca6
+		      best = string_table + lib->value;
b1dca6
+
b1dca6
+		      if (flags == GLRO (dl_correct_cache_id))
b1dca6
+			/* We've found an exact match for the shared
b1dca6
+			   object and no general `ELF' release.  Stop
b1dca6
+			   searching.  */
b1dca6
+			break;
b1dca6
+		    }
b1dca6
+		}
b1dca6
+	    }
b1dca6
+	  while (++middle <= right);
b1dca6
+	  break;
b1dca6
+	}
b1dca6
 
b1dca6
+      if (cmpres < 0)
b1dca6
+	left = middle + 1;
b1dca6
+      else
b1dca6
+	right = middle - 1;
b1dca6
+    }
b1dca6
+
b1dca6
+  return best;
b1dca6
+}
b1dca6
 
b1dca6
 int
b1dca6
 _dl_cache_libcmp (const char *p1, const char *p2)
b1dca6
@@ -182,12 +220,6 @@ _dl_cache_libcmp (const char *p1, const char *p2)
b1dca6
 char *
b1dca6
 _dl_load_cache_lookup (const char *name)
b1dca6
 {
b1dca6
-  int left, right, middle;
b1dca6
-  int cmpres;
b1dca6
-  const char *cache_data;
b1dca6
-  uint32_t cache_data_size;
b1dca6
-  const char *best;
b1dca6
-
b1dca6
   /* Print a message if the loading of libs is traced.  */
b1dca6
   if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS))
b1dca6
     _dl_debug_printf (" search cache=%s\n", LD_SO_CACHE);
b1dca6
@@ -247,51 +279,22 @@ _dl_load_cache_lookup (const char *name)
b1dca6
     /* Previously looked for the cache file and didn't find it.  */
b1dca6
     return NULL;
b1dca6
 
b1dca6
-  best = NULL;
b1dca6
-
b1dca6
+  const char *best;
b1dca6
   if (cache_new != (void *) -1)
b1dca6
     {
b1dca6
-      uint64_t platform;
b1dca6
-
b1dca6
-      /* This is where the strings start.  */
b1dca6
-      cache_data = (const char *) cache_new;
b1dca6
-
b1dca6
-      /* Now we can compute how large the string table is.  */
b1dca6
-      cache_data_size = (const char *) cache + cachesize - cache_data;
b1dca6
-
b1dca6
-      platform = _dl_string_platform (GLRO(dl_platform));
b1dca6
-      if (platform != (uint64_t) -1)
b1dca6
-	platform = 1ULL << platform;
b1dca6
-
b1dca6
-      uint64_t hwcap_mask = GET_HWCAP_MASK();
b1dca6
-
b1dca6
-#define _DL_HWCAP_TLS_MASK (1LL << 63)
b1dca6
-      uint64_t hwcap_exclude = ~((GLRO(dl_hwcap) & hwcap_mask)
b1dca6
-				 | _DL_HWCAP_PLATFORM | _DL_HWCAP_TLS_MASK);
b1dca6
-
b1dca6
-      /* Only accept hwcap if it's for the right platform.  */
b1dca6
-#define HWCAP_CHECK \
b1dca6
-      if (lib->hwcap & hwcap_exclude)					      \
b1dca6
-	continue;							      \
b1dca6
-      if (GLRO(dl_osversion) && lib->osversion > GLRO(dl_osversion))	      \
b1dca6
-	continue;							      \
b1dca6
-      if (_DL_PLATFORMS_COUNT						      \
b1dca6
-	  && (lib->hwcap & _DL_HWCAP_PLATFORM) != 0			      \
b1dca6
-	  && (lib->hwcap & _DL_HWCAP_PLATFORM) != platform)		      \
b1dca6
-	continue
b1dca6
-      SEARCH_CACHE (cache_new);
b1dca6
+      const char *string_table = (const char *) cache_new;
b1dca6
+      best = search_cache (string_table, cachesize,
b1dca6
+			   &cache_new->libs[0].entry, cache_new->nlibs,
b1dca6
+			   sizeof (cache_new->libs[0]), name);
b1dca6
     }
b1dca6
   else
b1dca6
     {
b1dca6
-      /* This is where the strings start.  */
b1dca6
-      cache_data = (const char *) &cache->libs[cache->nlibs];
b1dca6
-
b1dca6
-      /* Now we can compute how large the string table is.  */
b1dca6
-      cache_data_size = (const char *) cache + cachesize - cache_data;
b1dca6
-
b1dca6
-#undef HWCAP_CHECK
b1dca6
-#define HWCAP_CHECK do {} while (0)
b1dca6
-      SEARCH_CACHE (cache);
b1dca6
+      const char *string_table = (const char *) &cache->libs[cache->nlibs];
b1dca6
+      uint32_t string_table_size
b1dca6
+	= (const char *) cache + cachesize - string_table;
b1dca6
+      best = search_cache (string_table, string_table_size,
b1dca6
+			   &cache->libs[0], cache->nlibs,
b1dca6
+			   sizeof (cache->libs[0]), name);
b1dca6
     }
b1dca6
 
b1dca6
   /* Print our result if wanted.  */
b1dca6
diff --git a/sysdeps/generic/dl-cache.h b/sysdeps/generic/dl-cache.h
b1dca6
index cf43f1cf3b441bc7..3c5730dfe42c7c88 100644
b1dca6
--- a/sysdeps/generic/dl-cache.h
b1dca6
+++ b/sysdeps/generic/dl-cache.h
b1dca6
@@ -59,8 +59,8 @@
b1dca6
 */
b1dca6
 struct file_entry
b1dca6
 {
b1dca6
-  int flags;		/* This is 1 for an ELF library.  */
b1dca6
-  unsigned int key, value; /* String table indices.  */
b1dca6
+  int32_t flags;		/* This is 1 for an ELF library.  */
b1dca6
+  uint32_t key, value;		/* String table indices.  */
b1dca6
 };
b1dca6
 
b1dca6
 struct cache_file
b1dca6
@@ -77,8 +77,17 @@ struct cache_file
b1dca6
 
b1dca6
 struct file_entry_new
b1dca6
 {
b1dca6
-  int32_t flags;		/* This is 1 for an ELF library.  */
b1dca6
-  uint32_t key, value;		/* String table indices.  */
b1dca6
+  union
b1dca6
+  {
b1dca6
+    /* Fields shared with struct file_entry.  */
b1dca6
+    struct file_entry entry;
b1dca6
+    /* Also expose these fields directly.  */
b1dca6
+    struct
b1dca6
+    {
b1dca6
+      int32_t flags;		/* This is 1 for an ELF library.  */
b1dca6
+      uint32_t key, value;	/* String table indices.  */
b1dca6
+    };
b1dca6
+  };
b1dca6
   uint32_t osversion;		/* Required OS version.	 */
b1dca6
   uint64_t hwcap;		/* Hwcap entry.	 */
b1dca6
 };