1 /* Support for reading /etc/ld.so.cache files written by Linux ldconfig.
2 Copyright (C) 1996-2022 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>
31 #include <not-errno.h>
33 #ifndef _DL_PLATFORMS_COUNT
34 # define _DL_PLATFORMS_COUNT 0
37 /* This is the starting address and the size of the mmap()ed file. */
38 static struct cache_file
*cache
;
39 static struct cache_file_new
*cache_new
;
40 static size_t cachesize
;
43 /* This is used to cache the priorities of glibc-hwcaps
44 subdirectories. The elements of _dl_cache_priorities correspond to
45 the strings in the cache_extension_tag_glibc_hwcaps section. */
46 static uint32_t *glibc_hwcaps_priorities
;
47 static uint32_t glibc_hwcaps_priorities_length
;
48 static uint32_t glibc_hwcaps_priorities_allocated
;
50 /* True if the full malloc was used to allocated the array. */
51 static bool glibc_hwcaps_priorities_malloced
;
53 /* Deallocate the glibc_hwcaps_priorities array. */
55 glibc_hwcaps_priorities_free (void)
57 /* When the minimal malloc is in use, free does not do anything,
58 so it does not make sense to call it. */
59 if (glibc_hwcaps_priorities_malloced
)
60 free (glibc_hwcaps_priorities
);
61 glibc_hwcaps_priorities
= NULL
;
62 glibc_hwcaps_priorities_allocated
= 0;
65 /* Ordered comparison of a hwcaps string from the cache on the left
66 (identified by its string table index) and a _dl_hwcaps_priorities
67 element on the right. */
69 glibc_hwcaps_compare (uint32_t left_index
, struct dl_hwcaps_priority
*right
)
71 const char *left_name
= (const char *) cache
+ left_index
;
72 uint32_t left_name_length
= strlen (left_name
);
74 if (left_name_length
< right
->name_length
)
75 to_compare
= left_name_length
;
77 to_compare
= right
->name_length
;
78 int cmp
= memcmp (left_name
, right
->name
, to_compare
);
81 if (left_name_length
< right
->name_length
)
83 else if (left_name_length
> right
->name_length
)
89 /* Initialize the glibc_hwcaps_priorities array and its length,
90 glibc_hwcaps_priorities_length. */
92 glibc_hwcaps_priorities_init (void)
94 struct cache_extension_all_loaded ext
;
95 if (!cache_extension_load (cache_new
, cache
, cachesize
, &ext
))
98 uint32_t length
= (ext
.sections
[cache_extension_tag_glibc_hwcaps
].size
100 if (length
> glibc_hwcaps_priorities_allocated
)
102 glibc_hwcaps_priorities_free ();
104 uint32_t *new_allocation
= malloc (length
* sizeof (uint32_t));
105 if (new_allocation
== NULL
)
106 /* This effectively disables hwcaps on memory allocation
110 glibc_hwcaps_priorities
= new_allocation
;
111 glibc_hwcaps_priorities_allocated
= length
;
112 glibc_hwcaps_priorities_malloced
= __rtld_malloc_is_complete ();
115 /* Compute the priorities for the subdirectories by merging the
116 array in the cache with the dl_hwcaps_priorities array. */
117 const uint32_t *left
= ext
.sections
[cache_extension_tag_glibc_hwcaps
].base
;
118 const uint32_t *left_end
= left
+ length
;
119 struct dl_hwcaps_priority
*right
= _dl_hwcaps_priorities
;
120 struct dl_hwcaps_priority
*right_end
= right
+ _dl_hwcaps_priorities_length
;
121 uint32_t *result
= glibc_hwcaps_priorities
;
123 while (left
< left_end
&& right
< right_end
)
125 if (*left
< cachesize
)
127 int cmp
= glibc_hwcaps_compare (*left
, right
);
130 *result
= right
->priority
;
150 while (left
< left_end
)
157 glibc_hwcaps_priorities_length
= length
;
160 /* Return the priority of the cache_extension_tag_glibc_hwcaps section
161 entry at INDEX. Zero means do not use. Otherwise, lower values
162 indicate greater preference. */
164 glibc_hwcaps_priority (uint32_t index
)
166 /* This does not need to repeated initialization attempts because
167 this function is only called if there is glibc-hwcaps data in the
168 cache, so the first call initializes the glibc_hwcaps_priorities
170 if (glibc_hwcaps_priorities_length
== 0)
171 glibc_hwcaps_priorities_init ();
173 if (index
< glibc_hwcaps_priorities_length
)
174 return glibc_hwcaps_priorities
[index
];
180 /* True if PTR is a valid string table index. */
182 _dl_cache_verify_ptr (uint32_t ptr
, size_t string_table_size
)
184 return ptr
< string_table_size
;
187 /* Compute the address of the element INDEX of the array at LIBS.
188 Conceptually, this is &LIBS[INDEX], but use ENTRY_SIZE for the size
190 static inline const struct file_entry
*
191 _dl_cache_file_entry (const struct file_entry
*libs
, size_t entry_size
,
194 return (const void *) libs
+ index
* entry_size
;
197 /* We use binary search since the table is sorted in the cache file.
198 The first matching entry in the table is returned. It is important
199 to use the same algorithm as used while generating the cache file.
200 STRING_TABLE_SIZE indicates the maximum offset in STRING_TABLE at
201 which data is mapped; it is not exact. */
203 search_cache (const char *string_table
, uint32_t string_table_size
,
204 struct file_entry
*libs
, uint32_t nlibs
, uint32_t entry_size
,
207 /* Used by the HWCAP check in the struct file_entry_new case. */
208 uint64_t platform
= _dl_string_platform (GLRO (dl_platform
));
209 if (platform
!= (uint64_t) -1)
210 platform
= 1ULL << platform
;
211 uint64_t hwcap_mask
= GET_HWCAP_MASK ();
212 #define _DL_HWCAP_TLS_MASK (1LL << 63)
213 uint64_t hwcap_exclude
= ~((GLRO (dl_hwcap
) & hwcap_mask
)
214 | _DL_HWCAP_PLATFORM
| _DL_HWCAP_TLS_MASK
);
217 int right
= nlibs
- 1;
218 const char *best
= NULL
;
220 uint32_t best_priority
= 0;
222 int disable_hwcap
= 0;
223 #ifdef NEED_LD_SO_NOHWCAP
224 if (__access_noerrno ("/etc/ld.so.nohwcap", F_OK
) == 0)
228 while (left
<= right
)
230 int middle
= (left
+ right
) / 2;
231 uint32_t key
= _dl_cache_file_entry (libs
, entry_size
, middle
)->key
;
233 /* Make sure string table indices are not bogus before using
235 if (!_dl_cache_verify_ptr (key
, string_table_size
))
238 /* Actually compare the entry with the key. */
239 int cmpres
= _dl_cache_libcmp (name
, string_table
+ key
);
240 if (__glibc_unlikely (cmpres
== 0))
242 /* Found it. LEFT now marks the last entry for which we
243 know the name is correct. */
246 /* There might be entries with this name before the one we
247 found. So we have to find the beginning. */
250 key
= _dl_cache_file_entry (libs
, entry_size
, middle
- 1)->key
;
251 /* Make sure string table indices are not bogus before
253 if (!_dl_cache_verify_ptr (key
, string_table_size
)
254 /* Actually compare the entry. */
255 || _dl_cache_libcmp (name
, string_table
+ key
) != 0)
263 const struct file_entry
*lib
264 = _dl_cache_file_entry (libs
, entry_size
, middle
);
266 /* Only perform the name test if necessary. */
268 /* We haven't seen this string so far. Test whether the
269 index is ok and whether the name matches. Otherwise
271 && (! _dl_cache_verify_ptr (lib
->key
, string_table_size
)
272 || (_dl_cache_libcmp (name
, string_table
+ lib
->key
)
277 if (_dl_cache_check_flags (flags
)
278 && _dl_cache_verify_ptr (lib
->value
, string_table_size
))
280 /* Named/extension hwcaps get slightly different
281 treatment: We keep searching for a better
283 bool named_hwcap
= false;
285 if (entry_size
>= sizeof (struct file_entry_new
))
287 /* The entry is large enough to include
288 HWCAP data. Check it. */
289 struct file_entry_new
*libnew
290 = (struct file_entry_new
*) lib
;
293 named_hwcap
= dl_cache_hwcap_extension (libnew
);
295 && !dl_cache_hwcap_isa_level_compatible (libnew
))
299 /* The entries with named/extension hwcaps have
300 been exhausted (they are listed before all
301 other entries). Return the best match
302 encountered so far if there is one. */
303 if (!named_hwcap
&& best
!= NULL
)
306 if ((libnew
->hwcap
& hwcap_exclude
) && !named_hwcap
)
308 if (disable_hwcap
&& libnew
->hwcap
!= 0)
310 if (_DL_PLATFORMS_COUNT
311 && (libnew
->hwcap
& _DL_HWCAP_PLATFORM
) != 0
312 && ((libnew
->hwcap
& _DL_HWCAP_PLATFORM
)
317 /* For named hwcaps, determine the priority and
318 see if beats what has been found so far. */
321 uint32_t entry_priority
322 = glibc_hwcaps_priority (libnew
->hwcap
);
323 if (entry_priority
== 0)
324 /* Not usable at all. Skip. */
326 else if (best
== NULL
327 || entry_priority
< best_priority
)
328 /* This entry is of higher priority
329 than the previous one, or it is the
331 best_priority
= entry_priority
;
333 /* An entry has already been found,
334 but it is a better match. */
340 best
= string_table
+ lib
->value
;
342 if (!named_hwcap
&& flags
== _DL_CACHE_DEFAULT_ID
)
343 /* With named hwcaps, we need to keep searching to
344 see if we find a better match. A better match
345 is also possible if the flags of the current
346 entry do not match the expected cache flags.
347 But if the flags match, no better entry will be
352 while (++middle
<= right
);
366 _dl_cache_libcmp (const char *p1
, const char *p2
)
370 if (*p1
>= '0' && *p1
<= '9')
372 if (*p2
>= '0' && *p2
<= '9')
374 /* Must compare this numerically. */
380 while (*p1
>= '0' && *p1
<= '9')
381 val1
= val1
* 10 + *p1
++ - '0';
382 while (*p2
>= '0' && *p2
<= '9')
383 val2
= val2
* 10 + *p2
++ - '0';
390 else if (*p2
>= '0' && *p2
<= '9')
404 /* Look up NAME in ld.so.cache and return the file name stored there, or null
405 if none is found. The cache is loaded if it was not already. If loading
406 the cache previously failed there will be no more attempts to load it.
407 The caller is responsible for freeing the returned string. The ld.so.cache
408 may be unmapped at any time by a completing recursive dlopen and
409 this function must take care that it does not return references to
410 any data in the mapping. */
412 _dl_load_cache_lookup (const char *name
)
414 /* Print a message if the loading of libs is traced. */
415 if (__glibc_unlikely (GLRO(dl_debug_mask
) & DL_DEBUG_LIBS
))
416 _dl_debug_printf (" search cache=%s\n", LD_SO_CACHE
);
420 /* Read the contents of the file. */
421 void *file
= _dl_sysdep_read_whole_file (LD_SO_CACHE
, &cachesize
,
424 /* We can handle three different cache file formats here:
425 - only the new format
426 - the old libc5/glibc2.0/2.1 format
427 - the old format with the new format in it
428 The following checks if the cache contains any of these formats. */
429 if (file
!= MAP_FAILED
&& cachesize
> sizeof *cache_new
430 && memcmp (file
, CACHEMAGIC_VERSION_NEW
,
431 sizeof CACHEMAGIC_VERSION_NEW
- 1) == 0
432 /* Check for corruption, avoiding overflow. */
433 && ((cachesize
- sizeof *cache_new
) / sizeof (struct file_entry_new
)
434 >= ((struct cache_file_new
*) file
)->nlibs
))
436 if (! cache_file_new_matches_endian (file
))
438 __munmap (file
, cachesize
);
444 else if (file
!= MAP_FAILED
&& cachesize
> sizeof *cache
445 && memcmp (file
, CACHEMAGIC
, sizeof CACHEMAGIC
- 1) == 0
446 /* Check for corruption, avoiding overflow. */
447 && ((cachesize
- sizeof *cache
) / sizeof (struct file_entry
)
448 >= ((struct cache_file
*) file
)->nlibs
))
454 /* Check for new version. */
455 offset
= ALIGN_CACHE (sizeof (struct cache_file
)
456 + cache
->nlibs
* sizeof (struct file_entry
));
458 cache_new
= (struct cache_file_new
*) ((void *) cache
+ offset
);
459 if (cachesize
< (offset
+ sizeof (struct cache_file_new
))
460 || memcmp (cache_new
->magic
, CACHEMAGIC_VERSION_NEW
,
461 sizeof CACHEMAGIC_VERSION_NEW
- 1) != 0)
462 cache_new
= (void *) -1;
465 if (! cache_file_new_matches_endian (cache_new
))
467 /* The old-format part of the cache is bogus as well
468 if the endianness does not match. (But it is
469 unclear how the new header can be located if the
470 endianess does not match.) */
472 cache_new
= (void *) -1;
473 __munmap (file
, cachesize
);
479 if (file
!= MAP_FAILED
)
480 __munmap (file
, cachesize
);
484 assert (cache
!= NULL
);
487 if (cache
== (void *) -1)
488 /* Previously looked for the cache file and didn't find it. */
492 if (cache_new
!= (void *) -1)
494 const char *string_table
= (const char *) cache_new
;
495 best
= search_cache (string_table
, cachesize
,
496 &cache_new
->libs
[0].entry
, cache_new
->nlibs
,
497 sizeof (cache_new
->libs
[0]), name
);
501 const char *string_table
= (const char *) &cache
->libs
[cache
->nlibs
];
502 uint32_t string_table_size
503 = (const char *) cache
+ cachesize
- string_table
;
504 best
= search_cache (string_table
, string_table_size
,
505 &cache
->libs
[0], cache
->nlibs
,
506 sizeof (cache
->libs
[0]), name
);
509 /* Print our result if wanted. */
510 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_LIBS
, 0)
512 _dl_debug_printf (" trying file=%s\n", best
);
517 /* The double copy is *required* since malloc may be interposed
518 and call dlopen itself whose completion would unmap the data
519 we are accessing. Therefore we must make the copy of the
520 mapping data without using malloc. */
522 size_t best_len
= strlen (best
) + 1;
523 temp
= alloca (best_len
);
524 memcpy (temp
, best
, best_len
);
525 return __strdup (temp
);
529 /* If the system does not support MAP_COPY we cannot leave the file open
530 all the time since this would create problems when the file is replaced.
531 Therefore we provide this function to close the file and open it again
534 _dl_unload_cache (void)
536 if (cache
!= NULL
&& cache
!= (struct cache_file
*) -1)
538 __munmap (cache
, cachesize
);
542 /* This marks the glibc_hwcaps_priorities array as out-of-date. */
543 glibc_hwcaps_priorities_length
= 0;