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. */
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
,
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
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 \
52 right = cache->nlibs - 1; \
53 middle = (left + right) / 2; \
56 while (left <= right) \
58 /* Make sure string table indices are not bogus before using \
60 if (! _dl_cache_verify_ptr (cache->libs[middle].key)) \
66 /* Actually compare the entry with the key. */ \
67 cmpres = _dl_cache_libcmp (name, \
68 cache_data + cache->libs[middle].key); \
78 middle = (left + right) / 2; \
83 /* LEFT now marks the last entry for which we know the name is \
87 /* There might be entries with this name before the one we \
88 found. So we have to find the beginning. */ \
90 /* Make sure string table indices are not bogus before \
92 && _dl_cache_verify_ptr (cache->libs[middle - 1].key) \
93 /* Actually compare the entry. */ \
94 && (_dl_cache_libcmp (name, \
96 + cache->libs[middle - 1].key) \
104 /* Only perform the name test if necessary. */ \
106 /* We haven't seen this string so far. Test whether the \
107 index is ok and whether the name matches. Otherwise \
109 && (! _dl_cache_verify_ptr (cache->libs[middle].key) \
110 || (_dl_cache_libcmp (name, \
112 + cache->libs[middle].key) \
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) \
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 \
133 while (++middle <= right); \
140 /* Look up NAME in ld.so.cache and return the file name stored there,
141 or null if none is found. */
144 _dl_load_cache_lookup (const char *name
)
146 int left
, right
, middle
;
148 const char *cache_data
;
151 /* Print a message if the loading of libs is traced. */
153 _dl_debug_message (1, " search cache=", LD_SO_CACHE
, "\n", NULL
);
157 /* Read the contents of the file. */
158 void *file
= _dl_sysdep_read_whole_file (LD_SO_CACHE
, &cachesize
,
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))
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;
197 __munmap (file
, cachesize
);
203 if (cache
== (void *) -1)
204 /* Previously looked for the cache file and didn't find it. */
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
;
220 #define HWCAP_CHECK \
221 if (hwcap && (cache_new->libs[middle].hwcap & *hwcap) > _dl_hwcap) \
223 SEARCH_CACHE (cache_new
);
227 /* This is where the strings start. */
228 cache_data
= (const char *) &cache
->libs
[cache
->nlibs
];
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
);
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
247 _dl_unload_cache (void)
249 if (cache
!= NULL
&& cache
!= (struct cache_file
*) -1)
251 __munmap (cache
, cachesize
);