universally apply our cflags (no vsx, no altivec..)
[glibc.git] / elf / dl-cache.c
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.
4
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.
9
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.
14
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/>. */
18
19 #include <assert.h>
20 #include <unistd.h>
21 #include <ldsodefs.h>
22 #include <sys/mman.h>
23 #include <dl-cache.h>
24 #include <dl-procinfo.h>
25 #include <stdint.h>
26 #include <_itoa.h>
27 #include <dl-hwcaps.h>
28 #include <dl-isa-level.h>
29 #include <fcntl.h>
30 #include <sysdep.h>
31 #include <not-errno.h>
32
33 #ifndef _DL_PLATFORMS_COUNT
34 # define _DL_PLATFORMS_COUNT 0
35 #endif
36
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;
41
42 #ifdef SHARED
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;
49
50 /* True if the full malloc was used to allocated the array. */
51 static bool glibc_hwcaps_priorities_malloced;
52
53 /* Deallocate the glibc_hwcaps_priorities array. */
54 static void
55 glibc_hwcaps_priorities_free (void)
56 {
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;
63 }
64
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. */
68 static int
69 glibc_hwcaps_compare (uint32_t left_index, struct dl_hwcaps_priority *right)
70 {
71 const char *left_name = (const char *) cache + left_index;
72 uint32_t left_name_length = strlen (left_name);
73 uint32_t to_compare;
74 if (left_name_length < right->name_length)
75 to_compare = left_name_length;
76 else
77 to_compare = right->name_length;
78 int cmp = memcmp (left_name, right->name, to_compare);
79 if (cmp != 0)
80 return cmp;
81 if (left_name_length < right->name_length)
82 return -1;
83 else if (left_name_length > right->name_length)
84 return 1;
85 else
86 return 0;
87 }
88
89 /* Initialize the glibc_hwcaps_priorities array and its length,
90 glibc_hwcaps_priorities_length. */
91 static void
92 glibc_hwcaps_priorities_init (void)
93 {
94 struct cache_extension_all_loaded ext;
95 if (!cache_extension_load (cache_new, cache, cachesize, &ext))
96 return;
97
98 uint32_t length = (ext.sections[cache_extension_tag_glibc_hwcaps].size
99 / sizeof (uint32_t));
100 if (length > glibc_hwcaps_priorities_allocated)
101 {
102 glibc_hwcaps_priorities_free ();
103
104 uint32_t *new_allocation = malloc (length * sizeof (uint32_t));
105 if (new_allocation == NULL)
106 /* This effectively disables hwcaps on memory allocation
107 errors. */
108 return;
109
110 glibc_hwcaps_priorities = new_allocation;
111 glibc_hwcaps_priorities_allocated = length;
112 glibc_hwcaps_priorities_malloced = __rtld_malloc_is_complete ();
113 }
114
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;
122
123 while (left < left_end && right < right_end)
124 {
125 if (*left < cachesize)
126 {
127 int cmp = glibc_hwcaps_compare (*left, right);
128 if (cmp == 0)
129 {
130 *result = right->priority;
131 ++result;
132 ++left;
133 ++right;
134 }
135 else if (cmp < 0)
136 {
137 *result = 0;
138 ++result;
139 ++left;
140 }
141 else
142 ++right;
143 }
144 else
145 {
146 *result = 0;
147 ++result;
148 }
149 }
150 while (left < left_end)
151 {
152 *result = 0;
153 ++result;
154 ++left;
155 }
156
157 glibc_hwcaps_priorities_length = length;
158 }
159
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. */
163 static uint32_t
164 glibc_hwcaps_priority (uint32_t index)
165 {
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
169 array. */
170 if (glibc_hwcaps_priorities_length == 0)
171 glibc_hwcaps_priorities_init ();
172
173 if (index < glibc_hwcaps_priorities_length)
174 return glibc_hwcaps_priorities[index];
175 else
176 return 0;
177 }
178 #endif /* SHARED */
179
180 /* True if PTR is a valid string table index. */
181 static inline bool
182 _dl_cache_verify_ptr (uint32_t ptr, size_t string_table_size)
183 {
184 return ptr < string_table_size;
185 }
186
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
189 of *LIBS. */
190 static inline const struct file_entry *
191 _dl_cache_file_entry (const struct file_entry *libs, size_t entry_size,
192 size_t index)
193 {
194 return (const void *) libs + index * entry_size;
195 }
196
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. */
202 static const char *
203 search_cache (const char *string_table, uint32_t string_table_size,
204 struct file_entry *libs, uint32_t nlibs, uint32_t entry_size,
205 const char *name)
206 {
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);
215
216 int left = 0;
217 int right = nlibs - 1;
218 const char *best = NULL;
219 #ifdef SHARED
220 uint32_t best_priority = 0;
221 #endif
222 int disable_hwcap = 0;
223 #ifdef NEED_LD_SO_NOHWCAP
224 if (__access_noerrno ("/etc/ld.so.nohwcap", F_OK) == 0)
225 disable_hwcap = 1;
226 #endif
227
228 while (left <= right)
229 {
230 int middle = (left + right) / 2;
231 uint32_t key = _dl_cache_file_entry (libs, entry_size, middle)->key;
232
233 /* Make sure string table indices are not bogus before using
234 them. */
235 if (!_dl_cache_verify_ptr (key, string_table_size))
236 return NULL;
237
238 /* Actually compare the entry with the key. */
239 int cmpres = _dl_cache_libcmp (name, string_table + key);
240 if (__glibc_unlikely (cmpres == 0))
241 {
242 /* Found it. LEFT now marks the last entry for which we
243 know the name is correct. */
244 left = middle;
245
246 /* There might be entries with this name before the one we
247 found. So we have to find the beginning. */
248 while (middle > 0)
249 {
250 key = _dl_cache_file_entry (libs, entry_size, middle - 1)->key;
251 /* Make sure string table indices are not bogus before
252 using them. */
253 if (!_dl_cache_verify_ptr (key, string_table_size)
254 /* Actually compare the entry. */
255 || _dl_cache_libcmp (name, string_table + key) != 0)
256 break;
257 --middle;
258 }
259
260 do
261 {
262 int flags;
263 const struct file_entry *lib
264 = _dl_cache_file_entry (libs, entry_size, middle);
265
266 /* Only perform the name test if necessary. */
267 if (middle > left
268 /* We haven't seen this string so far. Test whether the
269 index is ok and whether the name matches. Otherwise
270 we are done. */
271 && (! _dl_cache_verify_ptr (lib->key, string_table_size)
272 || (_dl_cache_libcmp (name, string_table + lib->key)
273 != 0)))
274 break;
275
276 flags = lib->flags;
277 if (_dl_cache_check_flags (flags)
278 && _dl_cache_verify_ptr (lib->value, string_table_size))
279 {
280 /* Named/extension hwcaps get slightly different
281 treatment: We keep searching for a better
282 match. */
283 bool named_hwcap = false;
284
285 if (entry_size >= sizeof (struct file_entry_new))
286 {
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;
291
292 #ifdef SHARED
293 named_hwcap = dl_cache_hwcap_extension (libnew);
294 if (named_hwcap
295 && !dl_cache_hwcap_isa_level_compatible (libnew))
296 continue;
297 #endif
298
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)
304 break;
305
306 if ((libnew->hwcap & hwcap_exclude) && !named_hwcap)
307 continue;
308 if (disable_hwcap && libnew->hwcap != 0)
309 continue;
310 if (_DL_PLATFORMS_COUNT
311 && (libnew->hwcap & _DL_HWCAP_PLATFORM) != 0
312 && ((libnew->hwcap & _DL_HWCAP_PLATFORM)
313 != platform))
314 continue;
315
316 #ifdef SHARED
317 /* For named hwcaps, determine the priority and
318 see if beats what has been found so far. */
319 if (named_hwcap)
320 {
321 uint32_t entry_priority
322 = glibc_hwcaps_priority (libnew->hwcap);
323 if (entry_priority == 0)
324 /* Not usable at all. Skip. */
325 continue;
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
330 first entry. */
331 best_priority = entry_priority;
332 else
333 /* An entry has already been found,
334 but it is a better match. */
335 continue;
336 }
337 #endif /* SHARED */
338 }
339
340 best = string_table + lib->value;
341
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
348 found. */
349 break;
350 }
351 }
352 while (++middle <= right);
353 break;
354 }
355
356 if (cmpres < 0)
357 left = middle + 1;
358 else
359 right = middle - 1;
360 }
361
362 return best;
363 }
364
365 int
366 _dl_cache_libcmp (const char *p1, const char *p2)
367 {
368 while (*p1 != '\0')
369 {
370 if (*p1 >= '0' && *p1 <= '9')
371 {
372 if (*p2 >= '0' && *p2 <= '9')
373 {
374 /* Must compare this numerically. */
375 int val1;
376 int val2;
377
378 val1 = *p1++ - '0';
379 val2 = *p2++ - '0';
380 while (*p1 >= '0' && *p1 <= '9')
381 val1 = val1 * 10 + *p1++ - '0';
382 while (*p2 >= '0' && *p2 <= '9')
383 val2 = val2 * 10 + *p2++ - '0';
384 if (val1 != val2)
385 return val1 - val2;
386 }
387 else
388 return 1;
389 }
390 else if (*p2 >= '0' && *p2 <= '9')
391 return -1;
392 else if (*p1 != *p2)
393 return *p1 - *p2;
394 else
395 {
396 ++p1;
397 ++p2;
398 }
399 }
400 return *p1 - *p2;
401 }
402
403
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. */
411 char *
412 _dl_load_cache_lookup (const char *name)
413 {
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);
417
418 if (cache == NULL)
419 {
420 /* Read the contents of the file. */
421 void *file = _dl_sysdep_read_whole_file (LD_SO_CACHE, &cachesize,
422 PROT_READ);
423
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))
435 {
436 if (! cache_file_new_matches_endian (file))
437 {
438 __munmap (file, cachesize);
439 file = (void *) -1;
440 }
441 cache_new = file;
442 cache = file;
443 }
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))
449 {
450 size_t offset;
451 /* Looks ok. */
452 cache = file;
453
454 /* Check for new version. */
455 offset = ALIGN_CACHE (sizeof (struct cache_file)
456 + cache->nlibs * sizeof (struct file_entry));
457
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;
463 else
464 {
465 if (! cache_file_new_matches_endian (cache_new))
466 {
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.) */
471 cache = (void *) -1;
472 cache_new = (void *) -1;
473 __munmap (file, cachesize);
474 }
475 }
476 }
477 else
478 {
479 if (file != MAP_FAILED)
480 __munmap (file, cachesize);
481 cache = (void *) -1;
482 }
483
484 assert (cache != NULL);
485 }
486
487 if (cache == (void *) -1)
488 /* Previously looked for the cache file and didn't find it. */
489 return NULL;
490
491 const char *best;
492 if (cache_new != (void *) -1)
493 {
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);
498 }
499 else
500 {
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);
507 }
508
509 /* Print our result if wanted. */
510 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_LIBS, 0)
511 && best != NULL)
512 _dl_debug_printf (" trying file=%s\n", best);
513
514 if (best == NULL)
515 return NULL;
516
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. */
521 char *temp;
522 size_t best_len = strlen (best) + 1;
523 temp = alloca (best_len);
524 memcpy (temp, best, best_len);
525 return __strdup (temp);
526 }
527
528 #ifndef MAP_COPY
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
532 once needed. */
533 void
534 _dl_unload_cache (void)
535 {
536 if (cache != NULL && cache != (struct cache_file *) -1)
537 {
538 __munmap (cache, cachesize);
539 cache = NULL;
540 }
541 #ifdef SHARED
542 /* This marks the glibc_hwcaps_priorities array as out-of-date. */
543 glibc_hwcaps_priorities_length = 0;
544 #endif
545 }
546 #endif