1 /* Support for reading /etc/ld.so.cache files written by Linux ldconfig.
2 Copyright (C) 1996-2017 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 <http://www.gnu.org/licenses/>. */
24 #include <dl-procinfo.h>
27 #include <dl-hwcaps.h>
29 #ifndef _DL_PLATFORMS_COUNT
30 # define _DL_PLATFORMS_COUNT 0
33 /* This is the starting address and the size of the mmap()ed file. */
34 static struct cache_file
*cache
;
35 static struct cache_file_new
*cache_new
;
36 static size_t cachesize
;
38 /* 1 if cache_data + PTR points into the cache. */
39 #define _dl_cache_verify_ptr(ptr) (ptr < cache_data_size)
41 #define SEARCH_CACHE(cache) \
42 /* We use binary search since the table is sorted in the cache file. \
43 The first matching entry in the table is returned. \
44 It is important to use the same algorithm as used while generating \
49 right = cache->nlibs - 1; \
51 while (left <= right) \
53 __typeof__ (cache->libs[0].key) key; \
55 middle = (left + right) / 2; \
57 key = cache->libs[middle].key; \
59 /* Make sure string table indices are not bogus before using \
61 if (! _dl_cache_verify_ptr (key)) \
67 /* Actually compare the entry with the key. */ \
68 cmpres = _dl_cache_libcmp (name, cache_data + key); \
69 if (__glibc_unlikely (cmpres == 0)) \
71 /* Found it. LEFT now marks the last entry for which we \
72 know the name is correct. */ \
75 /* There might be entries with this name before the one we \
76 found. So we have to find the beginning. */ \
79 __typeof__ (cache->libs[0].key) key; \
81 key = cache->libs[middle - 1].key; \
82 /* Make sure string table indices are not bogus before \
84 if (! _dl_cache_verify_ptr (key) \
85 /* Actually compare the entry. */ \
86 || _dl_cache_libcmp (name, cache_data + key) != 0) \
94 __typeof__ (cache->libs[0]) *lib = &cache->libs[middle]; \
96 /* Only perform the name test if necessary. */ \
98 /* We haven't seen this string so far. Test whether the \
99 index is ok and whether the name matches. Otherwise \
101 && (! _dl_cache_verify_ptr (lib->key) \
102 || (_dl_cache_libcmp (name, cache_data + lib->key) \
106 flags = lib->flags; \
107 if (_dl_cache_check_flags (flags) \
108 && _dl_cache_verify_ptr (lib->value)) \
110 if (best == NULL || flags == GLRO(dl_correct_cache_id)) \
113 best = cache_data + lib->value; \
115 if (flags == GLRO(dl_correct_cache_id)) \
116 /* We've found an exact match for the shared \
117 object and no general `ELF' release. Stop \
123 while (++middle <= right); \
130 right = middle - 1; \
138 _dl_cache_libcmp (const char *p1
, const char *p2
)
142 if (*p1
>= '0' && *p1
<= '9')
144 if (*p2
>= '0' && *p2
<= '9')
146 /* Must compare this numerically. */
152 while (*p1
>= '0' && *p1
<= '9')
153 val1
= val1
* 10 + *p1
++ - '0';
154 while (*p2
>= '0' && *p2
<= '9')
155 val2
= val2
* 10 + *p2
++ - '0';
162 else if (*p2
>= '0' && *p2
<= '9')
176 /* Look up NAME in ld.so.cache and return the file name stored there, or null
177 if none is found. The cache is loaded if it was not already. If loading
178 the cache previously failed there will be no more attempts to load it.
179 The caller is responsible for freeing the returned string. The ld.so.cache
180 may be unmapped at any time by a completing recursive dlopen and
181 this function must take care that it does not return references to
182 any data in the mapping. */
185 _dl_load_cache_lookup (const char *name
)
187 int left
, right
, middle
;
189 const char *cache_data
;
190 uint32_t cache_data_size
;
193 /* Print a message if the loading of libs is traced. */
194 if (__glibc_unlikely (GLRO(dl_debug_mask
) & DL_DEBUG_LIBS
))
195 _dl_debug_printf (" search cache=%s\n", LD_SO_CACHE
);
199 /* Read the contents of the file. */
200 void *file
= _dl_sysdep_read_whole_file (LD_SO_CACHE
, &cachesize
,
203 /* We can handle three different cache file formats here:
204 - the old libc5/glibc2.0/2.1 format
205 - the old format with the new format in it
206 - only the new format
207 The following checks if the cache contains any of these formats. */
208 if (file
!= MAP_FAILED
&& cachesize
> sizeof *cache
209 && memcmp (file
, CACHEMAGIC
, sizeof CACHEMAGIC
- 1) == 0)
215 /* Check for new version. */
216 offset
= ALIGN_CACHE (sizeof (struct cache_file
)
217 + cache
->nlibs
* sizeof (struct file_entry
));
219 cache_new
= (struct cache_file_new
*) ((void *) cache
+ offset
);
220 if (cachesize
< (offset
+ sizeof (struct cache_file_new
))
221 || memcmp (cache_new
->magic
, CACHEMAGIC_VERSION_NEW
,
222 sizeof CACHEMAGIC_VERSION_NEW
- 1) != 0)
223 cache_new
= (void *) -1;
225 else if (file
!= MAP_FAILED
&& cachesize
> sizeof *cache_new
226 && memcmp (file
, CACHEMAGIC_VERSION_NEW
,
227 sizeof CACHEMAGIC_VERSION_NEW
- 1) == 0)
234 if (file
!= MAP_FAILED
)
235 __munmap (file
, cachesize
);
239 assert (cache
!= NULL
);
242 if (cache
== (void *) -1)
243 /* Previously looked for the cache file and didn't find it. */
248 if (cache_new
!= (void *) -1)
252 /* This is where the strings start. */
253 cache_data
= (const char *) cache_new
;
255 /* Now we can compute how large the string table is. */
256 cache_data_size
= (const char *) cache
+ cachesize
- cache_data
;
258 platform
= _dl_string_platform (GLRO(dl_platform
));
259 if (platform
!= (uint64_t) -1)
260 platform
= 1ULL << platform
;
262 uint64_t hwcap_mask
= GET_HWCAP_MASK();
264 #define _DL_HWCAP_TLS_MASK (1LL << 63)
265 uint64_t hwcap_exclude
= ~((GLRO(dl_hwcap
) & hwcap_mask
)
266 | _DL_HWCAP_PLATFORM
| _DL_HWCAP_TLS_MASK
);
268 /* Only accept hwcap if it's for the right platform. */
269 #define HWCAP_CHECK \
270 if (lib->hwcap & hwcap_exclude) \
272 if (GLRO(dl_osversion) && lib->osversion > GLRO(dl_osversion)) \
274 if (_DL_PLATFORMS_COUNT \
275 && (lib->hwcap & _DL_HWCAP_PLATFORM) != 0 \
276 && (lib->hwcap & _DL_HWCAP_PLATFORM) != platform) \
278 SEARCH_CACHE (cache_new
);
282 /* This is where the strings start. */
283 cache_data
= (const char *) &cache
->libs
[cache
->nlibs
];
285 /* Now we can compute how large the string table is. */
286 cache_data_size
= (const char *) cache
+ cachesize
- cache_data
;
289 #define HWCAP_CHECK do {} while (0)
290 SEARCH_CACHE (cache
);
293 /* Print our result if wanted. */
294 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_LIBS
, 0)
296 _dl_debug_printf (" trying file=%s\n", best
);
301 /* The double copy is *required* since malloc may be interposed
302 and call dlopen itself whose completion would unmap the data
303 we are accessing. Therefore we must make the copy of the
304 mapping data without using malloc. */
306 temp
= alloca (strlen (best
) + 1);
308 return __strdup (temp
);
312 /* If the system does not support MAP_COPY we cannot leave the file open
313 all the time since this would create problems when the file is replaced.
314 Therefore we provide this function to close the file and open it again
317 _dl_unload_cache (void)
319 if (cache
!= NULL
&& cache
!= (struct cache_file
*) -1)
321 __munmap (cache
, cachesize
);