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>
28 #ifndef _DL_PLATFORMS_COUNT
29 # define _DL_PLATFORMS_COUNT 0
32 /* This is the starting address and the size of the mmap()ed file. */
33 static struct cache_file
*cache
;
34 static struct cache_file_new
*cache_new
;
35 static size_t cachesize
;
37 /* 1 if cache_data + PTR points into the cache. */
38 #define _dl_cache_verify_ptr(ptr) (ptr < cache_data_size)
40 #define SEARCH_CACHE(cache) \
41 /* We use binary search since the table is sorted in the cache file. \
42 The first matching entry in the table is returned. \
43 It is important to use the same algorithm as used while generating \
48 right = cache->nlibs - 1; \
50 while (left <= right) \
52 __typeof__ (cache->libs[0].key) key; \
54 middle = (left + right) / 2; \
56 key = cache->libs[middle].key; \
58 /* Make sure string table indices are not bogus before using \
60 if (! _dl_cache_verify_ptr (key)) \
66 /* Actually compare the entry with the key. */ \
67 cmpres = _dl_cache_libcmp (name, cache_data + key); \
68 if (__glibc_unlikely (cmpres == 0)) \
70 /* Found it. LEFT now marks the last entry for which we \
71 know the name is correct. */ \
74 /* There might be entries with this name before the one we \
75 found. So we have to find the beginning. */ \
78 __typeof__ (cache->libs[0].key) key; \
80 key = cache->libs[middle - 1].key; \
81 /* Make sure string table indices are not bogus before \
83 if (! _dl_cache_verify_ptr (key) \
84 /* Actually compare the entry. */ \
85 || _dl_cache_libcmp (name, cache_data + key) != 0) \
93 __typeof__ (cache->libs[0]) *lib = &cache->libs[middle]; \
95 /* Only perform the name test if necessary. */ \
97 /* We haven't seen this string so far. Test whether the \
98 index is ok and whether the name matches. Otherwise \
100 && (! _dl_cache_verify_ptr (lib->key) \
101 || (_dl_cache_libcmp (name, cache_data + lib->key) \
105 flags = lib->flags; \
106 if (_dl_cache_check_flags (flags) \
107 && _dl_cache_verify_ptr (lib->value)) \
109 if (best == NULL || flags == GLRO(dl_correct_cache_id)) \
112 best = cache_data + lib->value; \
114 if (flags == GLRO(dl_correct_cache_id)) \
115 /* We've found an exact match for the shared \
116 object and no general `ELF' release. Stop \
122 while (++middle <= right); \
129 right = middle - 1; \
137 _dl_cache_libcmp (const char *p1
, const char *p2
)
141 if (*p1
>= '0' && *p1
<= '9')
143 if (*p2
>= '0' && *p2
<= '9')
145 /* Must compare this numerically. */
151 while (*p1
>= '0' && *p1
<= '9')
152 val1
= val1
* 10 + *p1
++ - '0';
153 while (*p2
>= '0' && *p2
<= '9')
154 val2
= val2
* 10 + *p2
++ - '0';
161 else if (*p2
>= '0' && *p2
<= '9')
175 /* Look up NAME in ld.so.cache and return the file name stored there, or null
176 if none is found. The cache is loaded if it was not already. If loading
177 the cache previously failed there will be no more attempts to load it.
178 The caller is responsible for freeing the returned string. The ld.so.cache
179 may be unmapped at any time by a completing recursive dlopen and
180 this function must take care that it does not return references to
181 any data in the mapping. */
184 _dl_load_cache_lookup (const char *name
)
186 int left
, right
, middle
;
188 const char *cache_data
;
189 uint32_t cache_data_size
;
192 /* Print a message if the loading of libs is traced. */
193 if (__glibc_unlikely (GLRO(dl_debug_mask
) & DL_DEBUG_LIBS
))
194 _dl_debug_printf (" search cache=%s\n", LD_SO_CACHE
);
198 /* Read the contents of the file. */
199 void *file
= _dl_sysdep_read_whole_file (LD_SO_CACHE
, &cachesize
,
202 /* We can handle three different cache file formats here:
203 - the old libc5/glibc2.0/2.1 format
204 - the old format with the new format in it
205 - only the new format
206 The following checks if the cache contains any of these formats. */
207 if (file
!= MAP_FAILED
&& cachesize
> sizeof *cache
208 && memcmp (file
, CACHEMAGIC
, sizeof CACHEMAGIC
- 1) == 0)
214 /* Check for new version. */
215 offset
= ALIGN_CACHE (sizeof (struct cache_file
)
216 + cache
->nlibs
* sizeof (struct file_entry
));
218 cache_new
= (struct cache_file_new
*) ((void *) cache
+ offset
);
219 if (cachesize
< (offset
+ sizeof (struct cache_file_new
))
220 || memcmp (cache_new
->magic
, CACHEMAGIC_VERSION_NEW
,
221 sizeof CACHEMAGIC_VERSION_NEW
- 1) != 0)
222 cache_new
= (void *) -1;
224 else if (file
!= MAP_FAILED
&& cachesize
> sizeof *cache_new
225 && memcmp (file
, CACHEMAGIC_VERSION_NEW
,
226 sizeof CACHEMAGIC_VERSION_NEW
- 1) == 0)
233 if (file
!= MAP_FAILED
)
234 __munmap (file
, cachesize
);
238 assert (cache
!= NULL
);
241 if (cache
== (void *) -1)
242 /* Previously looked for the cache file and didn't find it. */
247 if (cache_new
!= (void *) -1)
251 /* This is where the strings start. */
252 cache_data
= (const char *) cache_new
;
254 /* Now we can compute how large the string table is. */
255 cache_data_size
= (const char *) cache
+ cachesize
- cache_data
;
257 platform
= _dl_string_platform (GLRO(dl_platform
));
258 if (platform
!= (uint64_t) -1)
259 platform
= 1ULL << platform
;
261 #define _DL_HWCAP_TLS_MASK (1LL << 63)
262 uint64_t hwcap_exclude
= ~((GLRO(dl_hwcap
) & GLRO(dl_hwcap_mask
))
263 | _DL_HWCAP_PLATFORM
| _DL_HWCAP_TLS_MASK
);
265 /* Only accept hwcap if it's for the right platform. */
266 #define HWCAP_CHECK \
267 if (lib->hwcap & hwcap_exclude) \
269 if (GLRO(dl_osversion) && lib->osversion > GLRO(dl_osversion)) \
271 if (_DL_PLATFORMS_COUNT \
272 && (lib->hwcap & _DL_HWCAP_PLATFORM) != 0 \
273 && (lib->hwcap & _DL_HWCAP_PLATFORM) != platform) \
275 SEARCH_CACHE (cache_new
);
279 /* This is where the strings start. */
280 cache_data
= (const char *) &cache
->libs
[cache
->nlibs
];
282 /* Now we can compute how large the string table is. */
283 cache_data_size
= (const char *) cache
+ cachesize
- cache_data
;
286 #define HWCAP_CHECK do {} while (0)
287 SEARCH_CACHE (cache
);
290 /* Print our result if wanted. */
291 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_LIBS
, 0)
293 _dl_debug_printf (" trying file=%s\n", best
);
298 /* The double copy is *required* since malloc may be interposed
299 and call dlopen itself whose completion would unmap the data
300 we are accessing. Therefore we must make the copy of the
301 mapping data without using malloc. */
303 temp
= alloca (strlen (best
) + 1);
305 return __strdup (temp
);
309 /* If the system does not support MAP_COPY we cannot leave the file open
310 all the time since this would create problems when the file is replaced.
311 Therefore we provide this function to close the file and open it again
314 _dl_unload_cache (void)
316 if (cache
!= NULL
&& cache
!= (struct cache_file
*) -1)
318 __munmap (cache
, cachesize
);