]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/generic/dl-cache.c
ea4239aef5133a20710fd6710858733414b6f42b
[thirdparty/glibc.git] / 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.
4
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.
9
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.
14
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. */
19
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>
26
27 #include <stdio-common/_itoa.h>
28
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,
32 size_t *filesize_ptr,
33 int mmap_prot);
34
35 extern const char *_dl_platform;
36
37 #ifndef _DL_PLATFORMS_COUNT
38 # define _DL_PLATFORMS_COUNT 0
39 #endif
40
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;
45
46 /* 1 if cache_data + PTR points into the cache. */
47 #define _dl_cache_verify_ptr(ptr) (ptr < cache_data_size)
48
49 /* This is the cache ID we expect. Normally it is 3 for glibc linked
50 binaries. */
51 int _dl_correct_cache_id = _DL_CACHE_DEFAULT_ID;
52
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 \
57 the cache file. */ \
58 do \
59 { \
60 left = 0; \
61 right = cache->nlibs - 1; \
62 middle = (left + right) / 2; \
63 cmpres = 1; \
64 \
65 while (left <= right) \
66 { \
67 /* Make sure string table indices are not bogus before using \
68 them. */ \
69 if (! _dl_cache_verify_ptr (cache->libs[middle].key)) \
70 { \
71 cmpres = 1; \
72 break; \
73 } \
74 \
75 /* Actually compare the entry with the key. */ \
76 cmpres = _dl_cache_libcmp (name, \
77 cache_data + cache->libs[middle].key); \
78 if (cmpres == 0) \
79 /* Found it. */ \
80 break; \
81 \
82 if (cmpres < 0) \
83 left = middle + 1; \
84 else \
85 right = middle - 1; \
86 \
87 middle = (left + right) / 2; \
88 } \
89 \
90 if (cmpres == 0) \
91 { \
92 /* LEFT now marks the last entry for which we know the name is \
93 correct. */ \
94 left = middle; \
95 \
96 /* There might be entries with this name before the one we \
97 found. So we have to find the beginning. */ \
98 while (middle > 0 \
99 /* Make sure string table indices are not bogus before \
100 using them. */ \
101 && _dl_cache_verify_ptr (cache->libs[middle - 1].key) \
102 /* Actually compare the entry. */ \
103 && (_dl_cache_libcmp (name, \
104 cache_data \
105 + cache->libs[middle - 1].key) \
106 == 0)) \
107 --middle; \
108 \
109 do \
110 { \
111 int flags; \
112 \
113 /* Only perform the name test if necessary. */ \
114 if (middle > left \
115 /* We haven't seen this string so far. Test whether the \
116 index is ok and whether the name matches. Otherwise \
117 we are done. */ \
118 && (! _dl_cache_verify_ptr (cache->libs[middle].key) \
119 || (_dl_cache_libcmp (name, \
120 cache_data \
121 + cache->libs[middle].key) \
122 != 0))) \
123 break; \
124 \
125 flags = cache->libs[middle].flags; \
126 if (_dl_cache_check_flags (flags) \
127 && _dl_cache_verify_ptr (cache->libs[middle].value)) \
128 { \
129 if (best == NULL || flags == _dl_correct_cache_id) \
130 { \
131 HWCAP_CHECK; \
132 best = cache_data + cache->libs[middle].value; \
133 \
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 \
137 searching. */ \
138 break; \
139 } \
140 } \
141 } \
142 while (++middle <= right); \
143 } \
144 } \
145 while (0)
146
147
148
149 /* Look up NAME in ld.so.cache and return the file name stored there,
150 or null if none is found. */
151
152 const char *
153 _dl_load_cache_lookup (const char *name)
154 {
155 int left, right, middle;
156 int cmpres;
157 const char *cache_data;
158 uint32_t cache_data_size;
159 const char *best;
160
161 /* Print a message if the loading of libs is traced. */
162 if (_dl_debug_libs)
163 _dl_debug_message (1, " search cache=", LD_SO_CACHE, "\n", NULL);
164
165 if (cache == NULL)
166 {
167 /* Read the contents of the file. */
168 void *file = _dl_sysdep_read_whole_file (LD_SO_CACHE, &cachesize,
169 PROT_READ);
170
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)
178 {
179 size_t offset;
180 /* Looks ok. */
181 cache = file;
182
183 /* Check for new version. */
184 offset = ALIGN_CACHE (sizeof (struct cache_file)
185 + cache->nlibs * sizeof (struct file_entry));
186
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;
192 }
193 else if (file != NULL && cachesize > sizeof *cache_new
194 && memcmp (file, CACHEMAGIC_VERSION_NEW,
195 sizeof CACHEMAGIC_VERSION_NEW - 1) == 0)
196 {
197 cache_new = file;
198 cache = file;
199 }
200 else
201 {
202 if (file != NULL)
203 __munmap (file, cachesize);
204 cache = (void *) -1;
205 }
206
207 assert (cache != NULL);
208 }
209
210 if (cache == (void *) -1)
211 /* Previously looked for the cache file and didn't find it. */
212 return NULL;
213
214 best = NULL;
215
216 if (cache_new != (void *) -1)
217 {
218 /* This file ends in static libraries where we don't have a hwcap. */
219 unsigned long int *hwcap;
220 uint64_t platform;
221 weak_extern (_dl_hwcap);
222
223 /* This is where the strings start. */
224 cache_data = (const char *) cache_new;
225
226 /* Now we can compute how large the string table is. */
227 cache_data_size = (const char *) cache + cachesize - cache_data;
228
229 hwcap = &_dl_hwcap;
230 platform = _dl_string_platform (_dl_platform);
231 if (platform != -1)
232 platform = 1ULL << platform;
233
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) \
239 continue; \
240 if (hwcap \
241 && ((cache_new->libs[middle].hwcap & *hwcap & ~_DL_HWCAP_PLATFORM) \
242 > *hwcap)) \
243 continue
244 SEARCH_CACHE (cache_new);
245 }
246 else
247 {
248 /* This is where the strings start. */
249 cache_data = (const char *) &cache->libs[cache->nlibs];
250
251 /* Now we can compute how large the string table is. */
252 cache_data_size = (const char *) cache + cachesize - cache_data;
253
254 #undef HWCAP_CHECK
255 #define HWCAP_CHECK do {} while (0)
256 SEARCH_CACHE (cache);
257 }
258
259 /* Print our result if wanted. */
260 if (_dl_debug_libs && best != NULL)
261 _dl_debug_message (1, " trying file=", best, "\n", NULL);
262
263 return best;
264 }
265
266 #ifndef MAP_COPY
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
270 once needed. */
271 void
272 _dl_unload_cache (void)
273 {
274 if (cache != NULL && cache != (struct cache_file *) -1)
275 {
276 __munmap (cache, cachesize);
277 cache = NULL;
278 }
279 }
280 #endif