]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/generic/dl-cache.c
28fab0b19ed56c16909b76529f9015545eda49cb
[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 <unistd.h>
21 #include <ldsodefs.h>
22 #include <sys/mman.h>
23 #include <dl-cache.h>
24
25
26 /* System-dependent function to read a file's whole contents
27 in the most convenient manner available. */
28 extern void *_dl_sysdep_read_whole_file (const char *filename,
29 size_t *filesize_ptr,
30 int mmap_prot);
31
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;
36
37 /* 1 if cache_data + PTR points into the cache. */
38 #define _dl_cache_verify_ptr(ptr) (ptr < cachesize - sizeof *cache)
39
40 /* This is the cache ID we expect. Normally it is 3 for glibc linked
41 binaries. */
42 int _dl_correct_cache_id = _DL_CACHE_DEFAULT_ID;
43
44 #define SEARCH_CACHE(cache) \
45 /* We use binary search since the table is sorted in the cache file. \
46 The first matching entry in the table is returned. \
47 It is important to use the same algorithm as used while generating \
48 the cache file. */ \
49 do \
50 { \
51 left = 0; \
52 right = cache->nlibs - 1; \
53 middle = (left + right) / 2; \
54 cmpres = 1; \
55 \
56 while (left <= right) \
57 { \
58 /* Make sure string table indices are not bogus before using \
59 them. */ \
60 if (! _dl_cache_verify_ptr (cache->libs[middle].key)) \
61 { \
62 cmpres = 1; \
63 break; \
64 } \
65 \
66 /* Actually compare the entry with the key. */ \
67 cmpres = _dl_cache_libcmp (name, \
68 cache_data + cache->libs[middle].key); \
69 if (cmpres == 0) \
70 /* Found it. */ \
71 break; \
72 \
73 if (cmpres < 0) \
74 left = middle + 1; \
75 else \
76 right = middle - 1; \
77 \
78 middle = (left + right) / 2; \
79 } \
80 \
81 if (cmpres == 0) \
82 { \
83 /* LEFT now marks the last entry for which we know the name is \
84 correct. */ \
85 left = middle; \
86 \
87 /* There might be entries with this name before the one we \
88 found. So we have to find the beginning. */ \
89 while (middle > 0 \
90 /* Make sure string table indices are not bogus before \
91 using them. */ \
92 && _dl_cache_verify_ptr (cache->libs[middle - 1].key) \
93 /* Actually compare the entry. */ \
94 && (_dl_cache_libcmp (name, \
95 cache_data \
96 + cache->libs[middle - 1].key) \
97 == 0)) \
98 --middle; \
99 \
100 do \
101 { \
102 int flags; \
103 \
104 /* Only perform the name test if necessary. */ \
105 if (middle > left \
106 /* We haven't seen this string so far. Test whether the \
107 index is ok and whether the name matches. Otherwise \
108 we are done. */ \
109 && (! _dl_cache_verify_ptr (cache->libs[middle].key) \
110 || (_dl_cache_libcmp (name, \
111 cache_data \
112 + cache->libs[middle].key) \
113 != 0))) \
114 break; \
115 \
116 flags = cache->libs[middle].flags; \
117 if (_dl_cache_check_flags (flags) \
118 && _dl_cache_verify_ptr (cache->libs[middle].value)) \
119 { \
120 if (best == NULL || flags == _dl_correct_cache_id) \
121 { \
122 HWCAP_CHECK; \
123 best = cache_data + cache->libs[middle].value; \
124 \
125 if (flags == _dl_correct_cache_id) \
126 /* We've found an exact match for the shared \
127 object and no general `ELF' release. Stop \
128 searching. */ \
129 break; \
130 } \
131 } \
132 } \
133 while (++middle <= right); \
134 } \
135 } \
136 while (0)
137
138
139
140 /* Look up NAME in ld.so.cache and return the file name stored there,
141 or null if none is found. */
142
143 const char *
144 _dl_load_cache_lookup (const char *name)
145 {
146 int left, right, middle;
147 int cmpres;
148 const char *cache_data;
149 const char *best;
150
151 /* Print a message if the loading of libs is traced. */
152 if (_dl_debug_libs)
153 _dl_debug_message (1, " search cache=", LD_SO_CACHE, "\n", NULL);
154
155 if (cache == NULL)
156 {
157 /* Read the contents of the file. */
158 void *file = _dl_sysdep_read_whole_file (LD_SO_CACHE, &cachesize,
159 PROT_READ);
160
161 /* We can handle three different cache file formats here:
162 - the old libc5/glibc2.0/2.1 format
163 - the old format with the new format in it
164 - only the new format
165 The following checks if the cache contains any of these formats. */
166 if (file && cachesize > sizeof *cache &&
167 !memcmp (file, CACHEMAGIC, sizeof CACHEMAGIC - 1))
168 {
169 /* Looks ok. */
170 cache = file;
171
172 /* Check for new version. */
173 cache_new = (struct cache_file_new *) &cache->libs[cache->nlibs];
174 if (cachesize <
175 (sizeof (struct cache_file) + cache->nlibs * sizeof (struct file_entry)
176 + sizeof (struct cache_file_new))
177 || memcmp (cache_new->magic, CACHEMAGIC_NEW,
178 sizeof CACHEMAGIC_NEW - 1)
179 || memcmp (cache_new->version, CACHE_VERSION,
180 sizeof CACHE_VERSION - 1))
181 cache_new = (void *) -1;
182 }
183 else if (file && cachesize > sizeof *cache_new)
184 {
185 cache_new = (struct cache_file_new *) file;
186 if (memcmp (cache_new->magic, CACHEMAGIC_NEW,
187 sizeof CACHEMAGIC_NEW - 1)
188 || memcmp (cache_new->version, CACHE_VERSION,
189 sizeof CACHE_VERSION - 1))
190 cache_new = (void *) -1;
191 }
192 else
193 {
194 if (file)
195 __munmap (file, cachesize);
196 cache = (void *) -1;
197 return NULL;
198 }
199 }
200
201 if (cache == (void *) -1)
202 /* Previously looked for the cache file and didn't find it. */
203 return NULL;
204
205 /* This is where the strings start. */
206 cache_data = (const char *) &cache->libs[cache->nlibs];
207
208 best = NULL;
209
210 if (cache_new != (void *) -1)
211 {
212 /* This file ends in static libraries where we don't have a hwcap. */
213 unsigned long int *hwcap;
214 weak_extern (_dl_hwcap);
215
216 hwcap = &_dl_hwcap;
217
218 #define HWCAP_CHECK \
219 if (hwcap && (cache_new->libs[middle].hwcap & *hwcap) > _dl_hwcap) \
220 continue
221 SEARCH_CACHE (cache_new);
222 }
223 else
224 #undef HWCAP_CHECK
225 #define HWCAP_CHECK do {} while (0)
226 SEARCH_CACHE (cache);
227
228 /* Print our result if wanted. */
229 if (_dl_debug_libs && best != NULL)
230 _dl_debug_message (1, " trying file=", best, "\n", NULL);
231
232 return best;
233 }
234
235 #ifndef MAP_COPY
236 /* If the system does not support MAP_COPY we cannot leave the file open
237 all the time since this would create problems when the file is replaced.
238 Therefore we provide this function to close the file and open it again
239 once needed. */
240 void
241 _dl_unload_cache (void)
242 {
243 if (cache != NULL && cache != (struct cache_file *) -1)
244 {
245 __munmap (cache, cachesize);
246 cache = NULL;
247 }
248 }
249 #endif