1 /* Support for reading /etc/ld.so.cache files written by Linux ldconfig.
2 Copyright (C) 1996-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
24 #include <dl-procinfo.h>
27 #include <dl-hwcaps.h>
28 #include <dl-isa-level.h>
30 #ifndef _DL_PLATFORMS_COUNT
31 # define _DL_PLATFORMS_COUNT 0
34 /* This is the starting address and the size of the mmap()ed file. */
35 static struct cache_file
*cache
;
36 static struct cache_file_new
*cache_new
;
37 static size_t cachesize
;
40 /* This is used to cache the priorities of glibc-hwcaps
41 subdirectories. The elements of _dl_cache_priorities correspond to
42 the strings in the cache_extension_tag_glibc_hwcaps section. */
43 static uint32_t *glibc_hwcaps_priorities
;
44 static uint32_t glibc_hwcaps_priorities_length
;
45 static uint32_t glibc_hwcaps_priorities_allocated
;
47 /* True if the full malloc was used to allocated the array. */
48 static bool glibc_hwcaps_priorities_malloced
;
50 /* Deallocate the glibc_hwcaps_priorities array. */
52 glibc_hwcaps_priorities_free (void)
54 /* When the minimal malloc is in use, free does not do anything,
55 so it does not make sense to call it. */
56 if (glibc_hwcaps_priorities_malloced
)
57 free (glibc_hwcaps_priorities
);
58 glibc_hwcaps_priorities
= NULL
;
59 glibc_hwcaps_priorities_allocated
= 0;
62 /* Ordered comparison of a hwcaps string from the cache on the left
63 (identified by its string table index) and a _dl_hwcaps_priorities
64 element on the right. */
66 glibc_hwcaps_compare (uint32_t left_index
, struct dl_hwcaps_priority
*right
)
68 const char *left_name
= (const char *) cache
+ left_index
;
69 uint32_t left_name_length
= strlen (left_name
);
71 if (left_name_length
< right
->name_length
)
72 to_compare
= left_name_length
;
74 to_compare
= right
->name_length
;
75 int cmp
= memcmp (left_name
, right
->name
, to_compare
);
78 if (left_name_length
< right
->name_length
)
80 else if (left_name_length
> right
->name_length
)
86 /* Initialize the glibc_hwcaps_priorities array and its length,
87 glibc_hwcaps_priorities_length. */
89 glibc_hwcaps_priorities_init (void)
91 struct cache_extension_all_loaded ext
;
92 if (!cache_extension_load (cache_new
, cache
, cachesize
, &ext
))
95 uint32_t length
= (ext
.sections
[cache_extension_tag_glibc_hwcaps
].size
97 if (length
> glibc_hwcaps_priorities_allocated
)
99 glibc_hwcaps_priorities_free ();
101 uint32_t *new_allocation
= malloc (length
* sizeof (uint32_t));
102 if (new_allocation
== NULL
)
103 /* This effectively disables hwcaps on memory allocation
107 glibc_hwcaps_priorities
= new_allocation
;
108 glibc_hwcaps_priorities_allocated
= length
;
109 glibc_hwcaps_priorities_malloced
= __rtld_malloc_is_complete ();
112 /* Compute the priorities for the subdirectories by merging the
113 array in the cache with the dl_hwcaps_priorities array. */
114 const uint32_t *left
= ext
.sections
[cache_extension_tag_glibc_hwcaps
].base
;
115 const uint32_t *left_end
= left
+ length
;
116 struct dl_hwcaps_priority
*right
= _dl_hwcaps_priorities
;
117 struct dl_hwcaps_priority
*right_end
= right
+ _dl_hwcaps_priorities_length
;
118 uint32_t *result
= glibc_hwcaps_priorities
;
120 while (left
< left_end
&& right
< right_end
)
122 if (*left
< cachesize
)
124 int cmp
= glibc_hwcaps_compare (*left
, right
);
127 *result
= right
->priority
;
147 while (left
< left_end
)
154 glibc_hwcaps_priorities_length
= length
;
157 /* Return the priority of the cache_extension_tag_glibc_hwcaps section
158 entry at INDEX. Zero means do not use. Otherwise, lower values
159 indicate greater preference. */
161 glibc_hwcaps_priority (uint32_t index
)
163 /* This does not need to repeated initialization attempts because
164 this function is only called if there is glibc-hwcaps data in the
165 cache, so the first call initializes the glibc_hwcaps_priorities
167 if (glibc_hwcaps_priorities_length
== 0)
168 glibc_hwcaps_priorities_init ();
170 if (index
< glibc_hwcaps_priorities_length
)
171 return glibc_hwcaps_priorities
[index
];
177 /* True if PTR is a valid string table index. */
179 _dl_cache_verify_ptr (uint32_t ptr
, size_t string_table_size
)
181 return ptr
< string_table_size
;
184 /* Compute the address of the element INDEX of the array at LIBS.
185 Conceptually, this is &LIBS[INDEX], but use ENTRY_SIZE for the size
187 static inline const struct file_entry
*
188 _dl_cache_file_entry (const struct file_entry
*libs
, size_t entry_size
,
191 return (const void *) libs
+ index
* entry_size
;
194 /* We use binary search since the table is sorted in the cache file.
195 The first matching entry in the table is returned. It is important
196 to use the same algorithm as used while generating the cache file.
197 STRING_TABLE_SIZE indicates the maximum offset in STRING_TABLE at
198 which data is mapped; it is not exact. */
200 search_cache (const char *string_table
, uint32_t string_table_size
,
201 struct file_entry
*libs
, uint32_t nlibs
, uint32_t entry_size
,
204 /* Used by the HWCAP check in the struct file_entry_new case. */
205 uint64_t platform
= _dl_string_platform (GLRO (dl_platform
));
206 if (platform
!= (uint64_t) -1)
207 platform
= 1ULL << platform
;
208 uint64_t hwcap_mask
= GET_HWCAP_MASK ();
209 #define _DL_HWCAP_TLS_MASK (1LL << 63)
210 uint64_t hwcap_exclude
= ~((GLRO (dl_hwcap
) & hwcap_mask
)
211 | _DL_HWCAP_PLATFORM
| _DL_HWCAP_TLS_MASK
);
214 int right
= nlibs
- 1;
215 const char *best
= NULL
;
217 uint32_t best_priority
= 0;
220 while (left
<= right
)
222 int middle
= (left
+ right
) / 2;
223 uint32_t key
= _dl_cache_file_entry (libs
, entry_size
, middle
)->key
;
225 /* Make sure string table indices are not bogus before using
227 if (!_dl_cache_verify_ptr (key
, string_table_size
))
230 /* Actually compare the entry with the key. */
231 int cmpres
= _dl_cache_libcmp (name
, string_table
+ key
);
232 if (__glibc_unlikely (cmpres
== 0))
234 /* Found it. LEFT now marks the last entry for which we
235 know the name is correct. */
238 /* There might be entries with this name before the one we
239 found. So we have to find the beginning. */
242 key
= _dl_cache_file_entry (libs
, entry_size
, middle
- 1)->key
;
243 /* Make sure string table indices are not bogus before
245 if (!_dl_cache_verify_ptr (key
, string_table_size
)
246 /* Actually compare the entry. */
247 || _dl_cache_libcmp (name
, string_table
+ key
) != 0)
255 const struct file_entry
*lib
256 = _dl_cache_file_entry (libs
, entry_size
, middle
);
258 /* Only perform the name test if necessary. */
260 /* We haven't seen this string so far. Test whether the
261 index is ok and whether the name matches. Otherwise
263 && (! _dl_cache_verify_ptr (lib
->key
, string_table_size
)
264 || (_dl_cache_libcmp (name
, string_table
+ lib
->key
)
269 if (_dl_cache_check_flags (flags
)
270 && _dl_cache_verify_ptr (lib
->value
, string_table_size
))
272 /* Named/extension hwcaps get slightly different
273 treatment: We keep searching for a better
275 bool named_hwcap
= false;
277 if (entry_size
>= sizeof (struct file_entry_new
))
279 /* The entry is large enough to include
280 HWCAP data. Check it. */
281 struct file_entry_new
*libnew
282 = (struct file_entry_new
*) lib
;
285 named_hwcap
= dl_cache_hwcap_extension (libnew
);
287 && !dl_cache_hwcap_isa_level_compatible (libnew
))
291 /* The entries with named/extension hwcaps have
292 been exhausted (they are listed before all
293 other entries). Return the best match
294 encountered so far if there is one. */
295 if (!named_hwcap
&& best
!= NULL
)
298 if ((libnew
->hwcap
& hwcap_exclude
) && !named_hwcap
)
300 if (GLRO (dl_osversion
)
301 && libnew
->osversion
> GLRO (dl_osversion
))
303 if (_DL_PLATFORMS_COUNT
304 && (libnew
->hwcap
& _DL_HWCAP_PLATFORM
) != 0
305 && ((libnew
->hwcap
& _DL_HWCAP_PLATFORM
)
310 /* For named hwcaps, determine the priority and
311 see if beats what has been found so far. */
314 uint32_t entry_priority
315 = glibc_hwcaps_priority (libnew
->hwcap
);
316 if (entry_priority
== 0)
317 /* Not usable at all. Skip. */
319 else if (best
== NULL
320 || entry_priority
< best_priority
)
321 /* This entry is of higher priority
322 than the previous one, or it is the
324 best_priority
= entry_priority
;
326 /* An entry has already been found,
327 but it is a better match. */
333 best
= string_table
+ lib
->value
;
335 if (!named_hwcap
&& flags
== _DL_CACHE_DEFAULT_ID
)
336 /* With named hwcaps, we need to keep searching to
337 see if we find a better match. A better match
338 is also possible if the flags of the current
339 entry do not match the expected cache flags.
340 But if the flags match, no better entry will be
345 while (++middle
<= right
);
359 _dl_cache_libcmp (const char *p1
, const char *p2
)
363 if (*p1
>= '0' && *p1
<= '9')
365 if (*p2
>= '0' && *p2
<= '9')
367 /* Must compare this numerically. */
373 while (*p1
>= '0' && *p1
<= '9')
374 val1
= val1
* 10 + *p1
++ - '0';
375 while (*p2
>= '0' && *p2
<= '9')
376 val2
= val2
* 10 + *p2
++ - '0';
383 else if (*p2
>= '0' && *p2
<= '9')
397 /* Look up NAME in ld.so.cache and return the file name stored there, or null
398 if none is found. The cache is loaded if it was not already. If loading
399 the cache previously failed there will be no more attempts to load it.
400 The caller is responsible for freeing the returned string. The ld.so.cache
401 may be unmapped at any time by a completing recursive dlopen and
402 this function must take care that it does not return references to
403 any data in the mapping. */
405 _dl_load_cache_lookup (const char *name
)
407 /* Print a message if the loading of libs is traced. */
408 if (__glibc_unlikely (GLRO(dl_debug_mask
) & DL_DEBUG_LIBS
))
409 _dl_debug_printf (" search cache=%s\n", LD_SO_CACHE
);
413 /* Read the contents of the file. */
414 void *file
= _dl_sysdep_read_whole_file (LD_SO_CACHE
, &cachesize
,
417 /* We can handle three different cache file formats here:
418 - only the new format
419 - the old libc5/glibc2.0/2.1 format
420 - the old format with the new format in it
421 The following checks if the cache contains any of these formats. */
422 if (file
!= MAP_FAILED
&& cachesize
> sizeof *cache_new
423 && memcmp (file
, CACHEMAGIC_VERSION_NEW
,
424 sizeof CACHEMAGIC_VERSION_NEW
- 1) == 0
425 /* Check for corruption, avoiding overflow. */
426 && ((cachesize
- sizeof *cache_new
) / sizeof (struct file_entry_new
)
427 >= ((struct cache_file_new
*) file
)->nlibs
))
429 if (! cache_file_new_matches_endian (file
))
431 __munmap (file
, cachesize
);
437 else if (file
!= MAP_FAILED
&& cachesize
> sizeof *cache
438 && memcmp (file
, CACHEMAGIC
, sizeof CACHEMAGIC
- 1) == 0
439 /* Check for corruption, avoiding overflow. */
440 && ((cachesize
- sizeof *cache
) / sizeof (struct file_entry
)
441 >= ((struct cache_file
*) file
)->nlibs
))
447 /* Check for new version. */
448 offset
= ALIGN_CACHE (sizeof (struct cache_file
)
449 + cache
->nlibs
* sizeof (struct file_entry
));
451 cache_new
= (struct cache_file_new
*) ((void *) cache
+ offset
);
452 if (cachesize
< (offset
+ sizeof (struct cache_file_new
))
453 || memcmp (cache_new
->magic
, CACHEMAGIC_VERSION_NEW
,
454 sizeof CACHEMAGIC_VERSION_NEW
- 1) != 0)
455 cache_new
= (void *) -1;
458 if (! cache_file_new_matches_endian (cache_new
))
460 /* The old-format part of the cache is bogus as well
461 if the endianness does not match. (But it is
462 unclear how the new header can be located if the
463 endianess does not match.) */
465 cache_new
= (void *) -1;
466 __munmap (file
, cachesize
);
472 if (file
!= MAP_FAILED
)
473 __munmap (file
, cachesize
);
477 assert (cache
!= NULL
);
480 if (cache
== (void *) -1)
481 /* Previously looked for the cache file and didn't find it. */
485 if (cache_new
!= (void *) -1)
487 const char *string_table
= (const char *) cache_new
;
488 best
= search_cache (string_table
, cachesize
,
489 &cache_new
->libs
[0].entry
, cache_new
->nlibs
,
490 sizeof (cache_new
->libs
[0]), name
);
494 const char *string_table
= (const char *) &cache
->libs
[cache
->nlibs
];
495 uint32_t string_table_size
496 = (const char *) cache
+ cachesize
- string_table
;
497 best
= search_cache (string_table
, string_table_size
,
498 &cache
->libs
[0], cache
->nlibs
,
499 sizeof (cache
->libs
[0]), name
);
502 /* Print our result if wanted. */
503 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_LIBS
, 0)
505 _dl_debug_printf (" trying file=%s\n", best
);
510 /* The double copy is *required* since malloc may be interposed
511 and call dlopen itself whose completion would unmap the data
512 we are accessing. Therefore we must make the copy of the
513 mapping data without using malloc. */
515 temp
= alloca (strlen (best
) + 1);
517 return __strdup (temp
);
521 /* If the system does not support MAP_COPY we cannot leave the file open
522 all the time since this would create problems when the file is replaced.
523 Therefore we provide this function to close the file and open it again
526 _dl_unload_cache (void)
528 if (cache
!= NULL
&& cache
!= (struct cache_file
*) -1)
530 __munmap (cache
, cachesize
);
534 /* This marks the glibc_hwcaps_priorities array as out-of-date. */
535 glibc_hwcaps_priorities_length
= 0;