Update.
[glibc.git] / sysdeps / generic / dl-cache.c
blob85db10e06cb3d62ad078d00ff108b30cab8e56a6
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. */
20 #include <unistd.h>
21 #include <ldsodefs.h>
22 #include <sys/mman.h>
23 #include <dl-cache.h>
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);
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;
37 /* 1 if cache_data + PTR points into the cache. */
38 #define _dl_cache_verify_ptr(ptr) (ptr < cachesize - sizeof *cache)
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;
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; \
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 } \
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; \
73 if (cmpres < 0) \
74 left = middle + 1; \
75 else \
76 right = middle - 1; \
78 middle = (left + right) / 2; \
79 } \
81 if (cmpres == 0) \
82 { \
83 /* LEFT now marks the last entry for which we know the name is \
84 correct. */ \
85 left = middle; \
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; \
100 do \
102 int flags; \
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; \
116 flags = cache->libs[middle].flags; \
117 if (_dl_cache_check_flags (flags) \
118 && _dl_cache_verify_ptr (cache->libs[middle].value)) \
120 if (best == NULL || flags == _dl_correct_cache_id) \
122 HWCAP_CHECK; \
123 best = cache_data + cache->libs[middle].value; \
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; \
133 while (++middle <= right); \
136 while (0)
140 /* Look up NAME in ld.so.cache and return the file name stored there,
141 or null if none is found. */
143 const char *
144 _dl_load_cache_lookup (const char *name)
146 int left, right, middle;
147 int cmpres;
148 const char *cache_data;
149 const char *best;
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);
155 if (cache == NULL)
157 /* Read the contents of the file. */
158 void *file = _dl_sysdep_read_whole_file (LD_SO_CACHE, &cachesize,
159 PROT_READ);
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))
169 size_t offset;
170 /* Looks ok. */
171 cache = file;
173 /* Check for new version. */
174 offset = ALIGN_CACHE (sizeof (struct cache_file)
175 + cache->nlibs * sizeof (struct file_entry));
177 cache_new = (struct cache_file_new *) ((void *)cache + offset);
178 if (cachesize < (offset + sizeof (struct cache_file_new))
179 || memcmp (cache_new->magic, CACHEMAGIC_NEW,
180 sizeof CACHEMAGIC_NEW - 1)
181 || memcmp (cache_new->version, CACHE_VERSION,
182 sizeof CACHE_VERSION - 1))
183 cache_new = (void *) -1;
185 else if (file && cachesize > sizeof *cache_new)
187 cache_new = (struct cache_file_new *) file;
188 if (memcmp (cache_new->magic, CACHEMAGIC_NEW,
189 sizeof CACHEMAGIC_NEW - 1)
190 || memcmp (cache_new->version, CACHE_VERSION,
191 sizeof CACHE_VERSION - 1))
192 cache_new = (void *) -1;
194 else
196 if (file)
197 __munmap (file, cachesize);
198 cache = (void *) -1;
199 return NULL;
203 if (cache == (void *) -1)
204 /* Previously looked for the cache file and didn't find it. */
205 return NULL;
207 best = NULL;
209 if (cache_new != (void *) -1)
211 /* This file ends in static libraries where we don't have a hwcap. */
212 unsigned long int *hwcap;
213 weak_extern (_dl_hwcap);
215 /* This is where the strings start. */
216 cache_data = (const char *) cache_new;
218 hwcap = &_dl_hwcap;
220 #define HWCAP_CHECK \
221 if (hwcap && (cache_new->libs[middle].hwcap & *hwcap) > _dl_hwcap) \
222 continue
223 SEARCH_CACHE (cache_new);
225 else
227 /* This is where the strings start. */
228 cache_data = (const char *) &cache->libs[cache->nlibs];
229 #undef HWCAP_CHECK
230 #define HWCAP_CHECK do {} while (0)
231 SEARCH_CACHE (cache);
234 /* Print our result if wanted. */
235 if (_dl_debug_libs && best != NULL)
236 _dl_debug_message (1, " trying file=", best, "\n", NULL);
238 return best;
241 #ifndef MAP_COPY
242 /* If the system does not support MAP_COPY we cannot leave the file open
243 all the time since this would create problems when the file is replaced.
244 Therefore we provide this function to close the file and open it again
245 once needed. */
246 void
247 _dl_unload_cache (void)
249 if (cache != NULL && cache != (struct cache_file *) -1)
251 __munmap (cache, cachesize);
252 cache = NULL;
255 #endif