]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/generic/dl-cache.c
1 /* Support for reading /etc/ld.so.cache files written by Linux ldconfig.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
25 #include <dl-procinfo.h>
27 #include <stdio-common/_itoa.h>
29 extern const char *_dl_platform
;
31 #ifndef _DL_PLATFORMS_COUNT
32 # define _DL_PLATFORMS_COUNT 0
35 /* This is the starting address and the size of the mmap()ed file. */
36 static struct cache_file
*cache
;
37 static struct cache_file_new
*cache_new
;
38 static size_t cachesize
;
40 /* 1 if cache_data + PTR points into the cache. */
41 #define _dl_cache_verify_ptr(ptr) (ptr < cache_data_size)
43 /* This is the cache ID we expect. Normally it is 3 for glibc linked
45 int _dl_correct_cache_id
= _DL_CACHE_DEFAULT_ID
;
47 #define SEARCH_CACHE(cache) \
48 /* We use binary search since the table is sorted in the cache file. \
49 The first matching entry in the table is returned. \
50 It is important to use the same algorithm as used while generating \
55 right = cache->nlibs - 1; \
56 middle = (left + right) / 2; \
59 while (left <= right) \
61 /* Make sure string table indices are not bogus before using \
63 if (! _dl_cache_verify_ptr (cache->libs[middle].key)) \
69 /* Actually compare the entry with the key. */ \
70 cmpres = _dl_cache_libcmp (name, \
71 cache_data + cache->libs[middle].key); \
81 middle = (left + right) / 2; \
86 /* LEFT now marks the last entry for which we know the name is \
90 /* There might be entries with this name before the one we \
91 found. So we have to find the beginning. */ \
93 /* Make sure string table indices are not bogus before \
95 && _dl_cache_verify_ptr (cache->libs[middle - 1].key) \
96 /* Actually compare the entry. */ \
97 && (_dl_cache_libcmp (name, \
99 + cache->libs[middle - 1].key) \
107 /* Only perform the name test if necessary. */ \
109 /* We haven't seen this string so far. Test whether the \
110 index is ok and whether the name matches. Otherwise \
112 && (! _dl_cache_verify_ptr (cache->libs[middle].key) \
113 || (_dl_cache_libcmp (name, \
115 + cache->libs[middle].key) \
119 flags = cache->libs[middle].flags; \
120 if (_dl_cache_check_flags (flags) \
121 && _dl_cache_verify_ptr (cache->libs[middle].value)) \
123 if (best == NULL || flags == _dl_correct_cache_id) \
126 best = cache_data + cache->libs[middle].value; \
128 if (flags == _dl_correct_cache_id) \
129 /* We've found an exact match for the shared \
130 object and no general `ELF' release. Stop \
136 while (++middle <= right); \
143 /* Look up NAME in ld.so.cache and return the file name stored there,
144 or null if none is found. */
148 _dl_load_cache_lookup (const char *name
)
150 int left
, right
, middle
;
152 const char *cache_data
;
153 uint32_t cache_data_size
;
156 /* Print a message if the loading of libs is traced. */
158 _dl_debug_message (1, " search cache=", LD_SO_CACHE
, "\n", NULL
);
162 /* Read the contents of the file. */
163 void *file
= _dl_sysdep_read_whole_file (LD_SO_CACHE
, &cachesize
,
166 /* We can handle three different cache file formats here:
167 - the old libc5/glibc2.0/2.1 format
168 - the old format with the new format in it
169 - only the new format
170 The following checks if the cache contains any of these formats. */
171 if (file
!= NULL
&& cachesize
> sizeof *cache
172 && memcmp (file
, CACHEMAGIC
, sizeof CACHEMAGIC
- 1) == 0)
178 /* Check for new version. */
179 offset
= ALIGN_CACHE (sizeof (struct cache_file
)
180 + cache
->nlibs
* sizeof (struct file_entry
));
182 cache_new
= (struct cache_file_new
*) ((void *) cache
+ offset
);
183 if (cachesize
< (offset
+ sizeof (struct cache_file_new
))
184 || memcmp (cache_new
->magic
, CACHEMAGIC_VERSION_NEW
,
185 sizeof CACHEMAGIC_VERSION_NEW
- 1) != 0)
186 cache_new
= (void *) -1;
188 else if (file
!= NULL
&& cachesize
> sizeof *cache_new
189 && memcmp (file
, CACHEMAGIC_VERSION_NEW
,
190 sizeof CACHEMAGIC_VERSION_NEW
- 1) == 0)
198 __munmap (file
, cachesize
);
202 assert (cache
!= NULL
);
205 if (cache
== (void *) -1)
206 /* Previously looked for the cache file and didn't find it. */
211 if (cache_new
!= (void *) -1)
213 /* This file ends in static libraries where we don't have a hwcap. */
214 unsigned long int *hwcap
;
216 weak_extern (_dl_hwcap
);
218 /* This is where the strings start. */
219 cache_data
= (const char *) cache_new
;
221 /* Now we can compute how large the string table is. */
222 cache_data_size
= (const char *) cache
+ cachesize
- cache_data
;
225 platform
= _dl_string_platform (_dl_platform
);
227 platform
= 1ULL << platform
;
229 /* Only accept hwcap if it's for the right platform. */
230 #define HWCAP_CHECK \
231 if (_DL_PLATFORMS_COUNT && platform != -1 \
232 && (cache_new->libs[middle].hwcap & _DL_HWCAP_PLATFORM) != 0 \
233 && (cache_new->libs[middle].hwcap & _DL_HWCAP_PLATFORM) != platform) \
236 && ((cache_new->libs[middle].hwcap & *hwcap & ~_DL_HWCAP_PLATFORM) \
239 SEARCH_CACHE (cache_new
);
243 /* This is where the strings start. */
244 cache_data
= (const char *) &cache
->libs
[cache
->nlibs
];
246 /* Now we can compute how large the string table is. */
247 cache_data_size
= (const char *) cache
+ cachesize
- cache_data
;
250 #define HWCAP_CHECK do {} while (0)
251 SEARCH_CACHE (cache
);
254 /* Print our result if wanted. */
255 if (_dl_debug_libs
&& best
!= NULL
)
256 _dl_debug_message (1, " trying file=", best
, "\n", NULL
);
262 /* If the system does not support MAP_COPY we cannot leave the file open
263 all the time since this would create problems when the file is replaced.
264 Therefore we provide this function to close the file and open it again
267 _dl_unload_cache (void)
269 if (cache
!= NULL
&& cache
!= (struct cache_file
*) -1)
271 __munmap (cache
, cachesize
);