Include <sys/types.h> instead of <stddef.h> since ssize_t is needed as well. Replace...
[glibc.git] / sysdeps / generic / dl-cache.c
blob4d66a238e3c66efffe47f42334c0c24ccde706cc
1 /* Support for reading /etc/ld.so.cache files written by Linux ldconfig.
2 Copyright (C) 1996,1997,1998,1999,2000,2001,2002 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <assert.h>
21 #include <unistd.h>
22 #include <ldsodefs.h>
23 #include <sys/mman.h>
24 #include <dl-cache.h>
25 #include <dl-procinfo.h>
27 #include <stdio-common/_itoa.h>
29 #ifndef _DL_PLATFORMS_COUNT
30 # define _DL_PLATFORMS_COUNT 0
31 #endif
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 \
45 the cache file. */ \
46 do \
47 { \
48 left = 0; \
49 right = cache->nlibs - 1; \
51 while (left <= right) \
52 { \
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 \
60 them. */ \
61 if (! _dl_cache_verify_ptr (key)) \
62 { \
63 cmpres = 1; \
64 break; \
65 } \
67 /* Actually compare the entry with the key. */ \
68 cmpres = _dl_cache_libcmp (name, cache_data + key); \
69 if (__builtin_expect (cmpres == 0, 0)) \
70 { \
71 /* Found it. LEFT now marks the last entry for which we \
72 know the name is correct. */ \
73 left = middle; \
75 /* There might be entries with this name before the one we \
76 found. So we have to find the beginning. */ \
77 while (middle > 0) \
78 { \
79 __typeof__ (cache->libs[0].key) key; \
81 key = cache->libs[middle - 1].key; \
82 /* Make sure string table indices are not bogus before \
83 using them. */ \
84 if (! _dl_cache_verify_ptr (key) \
85 /* Actually compare the entry. */ \
86 || _dl_cache_libcmp (name, cache_data + key) != 0) \
87 break; \
88 --middle; \
89 } \
91 do \
92 { \
93 int flags; \
94 __typeof__ (cache->libs[0]) *lib = &cache->libs[middle]; \
96 /* Only perform the name test if necessary. */ \
97 if (middle > left \
98 /* We haven't seen this string so far. Test whether the \
99 index is ok and whether the name matches. Otherwise \
100 we are done. */ \
101 && (! _dl_cache_verify_ptr (lib->key) \
102 || (_dl_cache_libcmp (name, cache_data + lib->key) \
103 != 0))) \
104 break; \
106 flags = lib->flags; \
107 if (_dl_cache_check_flags (flags) \
108 && _dl_cache_verify_ptr (lib->value)) \
110 if (best == NULL || flags == GL(dl_correct_cache_id)) \
112 HWCAP_CHECK; \
113 best = cache_data + lib->value; \
115 if (flags == GL(dl_correct_cache_id)) \
116 /* We've found an exact match for the shared \
117 object and no general `ELF' release. Stop \
118 searching. */ \
119 break; \
123 while (++middle <= right); \
124 break; \
127 if (cmpres < 0) \
128 left = middle + 1; \
129 else \
130 right = middle - 1; \
133 while (0)
137 /* Look up NAME in ld.so.cache and return the file name stored there,
138 or null if none is found. */
140 const char *
141 internal_function
142 _dl_load_cache_lookup (const char *name)
144 int left, right, middle;
145 int cmpres;
146 const char *cache_data;
147 uint32_t cache_data_size;
148 const char *best;
150 /* Print a message if the loading of libs is traced. */
151 if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_LIBS, 0))
152 INTUSE(_dl_debug_printf) (" search cache=%s\n", LD_SO_CACHE);
154 if (cache == NULL)
156 /* Read the contents of the file. */
157 void *file = _dl_sysdep_read_whole_file (LD_SO_CACHE, &cachesize,
158 PROT_READ);
160 /* We can handle three different cache file formats here:
161 - the old libc5/glibc2.0/2.1 format
162 - the old format with the new format in it
163 - only the new format
164 The following checks if the cache contains any of these formats. */
165 if (file != MAP_FAILED && cachesize > sizeof *cache
166 && memcmp (file, CACHEMAGIC, sizeof CACHEMAGIC - 1) == 0)
168 size_t offset;
169 /* Looks ok. */
170 cache = file;
172 /* Check for new version. */
173 offset = ALIGN_CACHE (sizeof (struct cache_file)
174 + cache->nlibs * sizeof (struct file_entry));
176 cache_new = (struct cache_file_new *) ((void *) cache + offset);
177 if (cachesize < (offset + sizeof (struct cache_file_new))
178 || memcmp (cache_new->magic, CACHEMAGIC_VERSION_NEW,
179 sizeof CACHEMAGIC_VERSION_NEW - 1) != 0)
180 cache_new = (void *) -1;
182 else if (file != MAP_FAILED && cachesize > sizeof *cache_new
183 && memcmp (file, CACHEMAGIC_VERSION_NEW,
184 sizeof CACHEMAGIC_VERSION_NEW - 1) == 0)
186 cache_new = file;
187 cache = file;
189 else
191 if (file != MAP_FAILED)
192 __munmap (file, cachesize);
193 cache = (void *) -1;
196 assert (cache != NULL);
199 if (cache == (void *) -1)
200 /* Previously looked for the cache file and didn't find it. */
201 return NULL;
203 best = NULL;
205 if (cache_new != (void *) -1)
207 /* This file ends in static libraries where we don't have a hwcap. */
208 unsigned long int *hwcap;
209 uint64_t platform;
210 #ifndef SHARED
211 weak_extern (_dl_hwcap);
212 #endif
214 /* This is where the strings start. */
215 cache_data = (const char *) cache_new;
217 /* Now we can compute how large the string table is. */
218 cache_data_size = (const char *) cache + cachesize - cache_data;
220 hwcap = &GL(dl_hwcap);
221 platform = _dl_string_platform (GL(dl_platform));
222 if (platform != -1)
223 platform = 1ULL << platform;
225 /* Only accept hwcap if it's for the right platform. */
226 #define HWCAP_CHECK \
227 if (GL(dl_osversion) \
228 && cache_new->libs[middle].osversion > GL(dl_osversion)) \
229 continue; \
230 if (_DL_PLATFORMS_COUNT && platform != -1 \
231 && (lib->hwcap & _DL_HWCAP_PLATFORM) != 0 \
232 && (lib->hwcap & _DL_HWCAP_PLATFORM) != platform) \
233 continue; \
234 if (hwcap \
235 && ((lib->hwcap & *hwcap & ~_DL_HWCAP_PLATFORM) > *hwcap)) \
236 continue
237 SEARCH_CACHE (cache_new);
239 else
241 /* This is where the strings start. */
242 cache_data = (const char *) &cache->libs[cache->nlibs];
244 /* Now we can compute how large the string table is. */
245 cache_data_size = (const char *) cache + cachesize - cache_data;
247 #undef HWCAP_CHECK
248 #define HWCAP_CHECK do {} while (0)
249 SEARCH_CACHE (cache);
252 /* Print our result if wanted. */
253 if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_LIBS, 0) && best != NULL)
254 INTUSE(_dl_debug_printf) (" trying file=%s\n", best);
256 return best;
259 #ifndef MAP_COPY
260 /* If the system does not support MAP_COPY we cannot leave the file open
261 all the time since this would create problems when the file is replaced.
262 Therefore we provide this function to close the file and open it again
263 once needed. */
264 void
265 _dl_unload_cache (void)
267 if (cache != NULL && cache != (struct cache_file *) -1)
269 __munmap (cache, cachesize);
270 cache = NULL;
273 INTDEF (_dl_unload_cache)
274 #endif