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