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