]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/generic/dl-cache.c
ea4239aef5133a20710fd6710858733414b6f42b
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 /* System-dependent function to read a file's whole contents
30 in the most convenient manner available. */
31 extern void *_dl_sysdep_read_whole_file (const char *filename
,
35 extern const char *_dl_platform
;
37 #ifndef _DL_PLATFORMS_COUNT
38 # define _DL_PLATFORMS_COUNT 0
41 /* This is the starting address and the size of the mmap()ed file. */
42 static struct cache_file
*cache
;
43 static struct cache_file_new
*cache_new
;
44 static size_t cachesize
;
46 /* 1 if cache_data + PTR points into the cache. */
47 #define _dl_cache_verify_ptr(ptr) (ptr < cache_data_size)
49 /* This is the cache ID we expect. Normally it is 3 for glibc linked
51 int _dl_correct_cache_id
= _DL_CACHE_DEFAULT_ID
;
53 #define SEARCH_CACHE(cache) \
54 /* We use binary search since the table is sorted in the cache file. \
55 The first matching entry in the table is returned. \
56 It is important to use the same algorithm as used while generating \
61 right = cache->nlibs - 1; \
62 middle = (left + right) / 2; \
65 while (left <= right) \
67 /* Make sure string table indices are not bogus before using \
69 if (! _dl_cache_verify_ptr (cache->libs[middle].key)) \
75 /* Actually compare the entry with the key. */ \
76 cmpres = _dl_cache_libcmp (name, \
77 cache_data + cache->libs[middle].key); \
87 middle = (left + right) / 2; \
92 /* LEFT now marks the last entry for which we know the name is \
96 /* There might be entries with this name before the one we \
97 found. So we have to find the beginning. */ \
99 /* Make sure string table indices are not bogus before \
101 && _dl_cache_verify_ptr (cache->libs[middle - 1].key) \
102 /* Actually compare the entry. */ \
103 && (_dl_cache_libcmp (name, \
105 + cache->libs[middle - 1].key) \
113 /* Only perform the name test if necessary. */ \
115 /* We haven't seen this string so far. Test whether the \
116 index is ok and whether the name matches. Otherwise \
118 && (! _dl_cache_verify_ptr (cache->libs[middle].key) \
119 || (_dl_cache_libcmp (name, \
121 + cache->libs[middle].key) \
125 flags = cache->libs[middle].flags; \
126 if (_dl_cache_check_flags (flags) \
127 && _dl_cache_verify_ptr (cache->libs[middle].value)) \
129 if (best == NULL || flags == _dl_correct_cache_id) \
132 best = cache_data + cache->libs[middle].value; \
134 if (flags == _dl_correct_cache_id) \
135 /* We've found an exact match for the shared \
136 object and no general `ELF' release. Stop \
142 while (++middle <= right); \
149 /* Look up NAME in ld.so.cache and return the file name stored there,
150 or null if none is found. */
153 _dl_load_cache_lookup (const char *name
)
155 int left
, right
, middle
;
157 const char *cache_data
;
158 uint32_t cache_data_size
;
161 /* Print a message if the loading of libs is traced. */
163 _dl_debug_message (1, " search cache=", LD_SO_CACHE
, "\n", NULL
);
167 /* Read the contents of the file. */
168 void *file
= _dl_sysdep_read_whole_file (LD_SO_CACHE
, &cachesize
,
171 /* We can handle three different cache file formats here:
172 - the old libc5/glibc2.0/2.1 format
173 - the old format with the new format in it
174 - only the new format
175 The following checks if the cache contains any of these formats. */
176 if (file
!= NULL
&& cachesize
> sizeof *cache
177 && memcmp (file
, CACHEMAGIC
, sizeof CACHEMAGIC
- 1) == 0)
183 /* Check for new version. */
184 offset
= ALIGN_CACHE (sizeof (struct cache_file
)
185 + cache
->nlibs
* sizeof (struct file_entry
));
187 cache_new
= (struct cache_file_new
*) ((void *) cache
+ offset
);
188 if (cachesize
< (offset
+ sizeof (struct cache_file_new
))
189 || memcmp (cache_new
->magic
, CACHEMAGIC_VERSION_NEW
,
190 sizeof CACHEMAGIC_VERSION_NEW
- 1) != 0)
191 cache_new
= (void *) -1;
193 else if (file
!= NULL
&& cachesize
> sizeof *cache_new
194 && memcmp (file
, CACHEMAGIC_VERSION_NEW
,
195 sizeof CACHEMAGIC_VERSION_NEW
- 1) == 0)
203 __munmap (file
, cachesize
);
207 assert (cache
!= NULL
);
210 if (cache
== (void *) -1)
211 /* Previously looked for the cache file and didn't find it. */
216 if (cache_new
!= (void *) -1)
218 /* This file ends in static libraries where we don't have a hwcap. */
219 unsigned long int *hwcap
;
221 weak_extern (_dl_hwcap
);
223 /* This is where the strings start. */
224 cache_data
= (const char *) cache_new
;
226 /* Now we can compute how large the string table is. */
227 cache_data_size
= (const char *) cache
+ cachesize
- cache_data
;
230 platform
= _dl_string_platform (_dl_platform
);
232 platform
= 1ULL << platform
;
234 /* Only accept hwcap if it's for the right platform. */
235 #define HWCAP_CHECK \
236 if (_DL_PLATFORMS_COUNT && platform != -1 \
237 && (cache_new->libs[middle].hwcap & _DL_HWCAP_PLATFORM) != 0 \
238 && (cache_new->libs[middle].hwcap & _DL_HWCAP_PLATFORM) != platform) \
241 && ((cache_new->libs[middle].hwcap & *hwcap & ~_DL_HWCAP_PLATFORM) \
244 SEARCH_CACHE (cache_new
);
248 /* This is where the strings start. */
249 cache_data
= (const char *) &cache
->libs
[cache
->nlibs
];
251 /* Now we can compute how large the string table is. */
252 cache_data_size
= (const char *) cache
+ cachesize
- cache_data
;
255 #define HWCAP_CHECK do {} while (0)
256 SEARCH_CACHE (cache
);
259 /* Print our result if wanted. */
260 if (_dl_debug_libs
&& best
!= NULL
)
261 _dl_debug_message (1, " trying file=", best
, "\n", NULL
);
267 /* If the system does not support MAP_COPY we cannot leave the file open
268 all the time since this would create problems when the file is replaced.
269 Therefore we provide this function to close the file and open it again
272 _dl_unload_cache (void)
274 if (cache
!= NULL
&& cache
!= (struct cache_file
*) -1)
276 __munmap (cache
, cachesize
);