Updated to fedora-glibc-20060905T0633
[glibc.git] / elf / cache.c
blob6730fb36eb5103d62b7d3ba92746a83897b99f8c
1 /* Copyright (C) 1999-2003,2005,2006 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Andreas Jaeger <aj@suse.de>, 1999.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 #include <errno.h>
19 #include <error.h>
20 #include <dirent.h>
21 #include <inttypes.h>
22 #include <libintl.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/fcntl.h>
28 #include <sys/mman.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
32 #include <ldconfig.h>
33 #include <dl-cache.h>
35 struct cache_entry
37 char *lib; /* Library name. */
38 char *path; /* Path to find library. */
39 int flags; /* Flags to indicate kind of library. */
40 unsigned int osversion; /* Required OS version. */
41 uint64_t hwcap; /* Important hardware capabilities. */
42 int bits_hwcap; /* Number of bits set in hwcap. */
43 struct cache_entry *next; /* Next entry in list. */
46 /* List of all cache entries. */
47 static struct cache_entry *entries;
49 static const char *flag_descr[] =
50 { "libc4", "ELF", "libc5", "libc6"};
52 /* Print a single entry. */
53 static void
54 print_entry (const char *lib, int flag, unsigned int osversion,
55 uint64_t hwcap, const char *key)
57 printf ("\t%s (", lib);
58 switch (flag & FLAG_TYPE_MASK)
60 case FLAG_LIBC4:
61 case FLAG_ELF:
62 case FLAG_ELF_LIBC5:
63 case FLAG_ELF_LIBC6:
64 fputs (flag_descr[flag & FLAG_TYPE_MASK], stdout);
65 break;
66 default:
67 fputs (_("unknown"), stdout);
68 break;
70 switch (flag & FLAG_REQUIRED_MASK)
72 case FLAG_SPARC_LIB64:
73 fputs (",64bit", stdout);
74 break;
75 case FLAG_IA64_LIB64:
76 fputs (",IA-64", stdout);
77 break;
78 case FLAG_X8664_LIB64:
79 fputs (",x86-64", stdout);
80 break;
81 case FLAG_S390_LIB64:
82 fputs(",64bit", stdout);
83 break;
84 case FLAG_POWERPC_LIB64:
85 fputs(",64bit", stdout);
86 break;
87 case FLAG_MIPS64_LIBN32:
88 fputs(",N32", stdout);
89 break;
90 case FLAG_MIPS64_LIBN64:
91 fputs(",64bit", stdout);
92 case 0:
93 break;
94 default:
95 printf (",%d", flag & FLAG_REQUIRED_MASK);
96 break;
98 if (hwcap != 0)
99 printf (", hwcap: %#.16" PRIx64, hwcap);
100 if (osversion != 0)
102 static const char *const abi_tag_os[] =
104 [0] = "Linux",
105 [1] = "Hurd",
106 [2] = "Solaris",
107 [3] = "FreeBSD",
108 [4] = "kNetBSD",
109 [5] = "Syllable",
110 [6] = N_("Unknown OS")
112 #define MAXTAG (sizeof abi_tag_os / sizeof abi_tag_os[0] - 1)
113 unsigned int os = osversion >> 24;
115 printf (_(", OS ABI: %s %d.%d.%d"),
116 _(abi_tag_os[os > MAXTAG ? MAXTAG : os]),
117 (osversion >> 16) & 0xff,
118 (osversion >> 8) & 0xff,
119 osversion & 0xff);
121 printf (") => %s\n", key);
125 /* Print the whole cache file, if a file contains the new cache format
126 hidden in the old one, print the contents of the new format. */
127 void
128 print_cache (const char *cache_name)
130 size_t cache_size;
131 struct stat64 st;
132 int fd;
133 unsigned int i;
134 struct cache_file *cache;
135 struct cache_file_new *cache_new = NULL;
136 const char *cache_data;
137 int format = 0;
139 fd = open (cache_name, O_RDONLY);
140 if (fd < 0)
141 error (EXIT_FAILURE, errno, _("Can't open cache file %s\n"), cache_name);
143 if (fstat64 (fd, &st) < 0
144 /* No need to map the file if it is empty. */
145 || st.st_size == 0)
147 close (fd);
148 return;
151 cache = mmap (0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
152 if (cache == MAP_FAILED)
153 error (EXIT_FAILURE, errno, _("mmap of cache file failed.\n"));
154 cache_size = st.st_size;
156 if (cache_size < sizeof (struct cache_file))
157 error (EXIT_FAILURE, 0, _("File is not a cache file.\n"));
159 if (memcmp (cache->magic, CACHEMAGIC, sizeof CACHEMAGIC - 1))
161 /* This can only be the new format without the old one. */
162 cache_new = (struct cache_file_new *) cache;
164 if (memcmp (cache_new->magic, CACHEMAGIC_NEW, sizeof CACHEMAGIC_NEW - 1)
165 || memcmp (cache_new->version, CACHE_VERSION,
166 sizeof CACHE_VERSION - 1))
167 error (EXIT_FAILURE, 0, _("File is not a cache file.\n"));
168 format = 1;
169 /* This is where the strings start. */
170 cache_data = (const char *) cache_new;
172 else
174 size_t offset = ALIGN_CACHE (sizeof (struct cache_file)
175 + (cache->nlibs
176 * sizeof (struct file_entry)));
177 /* This is where the strings start. */
178 cache_data = (const char *) &cache->libs[cache->nlibs];
180 /* Check for a new cache embedded in the old format. */
181 if (cache_size >
182 (offset + sizeof (struct cache_file_new)))
185 cache_new = (struct cache_file_new *) ((void *)cache + offset);
187 if (memcmp (cache_new->magic, CACHEMAGIC_NEW,
188 sizeof CACHEMAGIC_NEW - 1) == 0
189 && memcmp (cache_new->version, CACHE_VERSION,
190 sizeof CACHE_VERSION - 1) == 0)
192 cache_data = (const char *) cache_new;
193 format = 1;
198 if (format == 0)
200 printf (_("%d libs found in cache `%s'\n"), cache->nlibs, cache_name);
202 /* Print everything. */
203 for (i = 0; i < cache->nlibs; i++)
204 print_entry (cache_data + cache->libs[i].key,
205 cache->libs[i].flags, 0, 0,
206 cache_data + cache->libs[i].value);
208 else if (format == 1)
210 printf (_("%d libs found in cache `%s'\n"),
211 cache_new->nlibs, cache_name);
213 /* Print everything. */
214 for (i = 0; i < cache_new->nlibs; i++)
215 print_entry (cache_data + cache_new->libs[i].key,
216 cache_new->libs[i].flags,
217 cache_new->libs[i].osversion,
218 cache_new->libs[i].hwcap,
219 cache_data + cache_new->libs[i].value);
221 /* Cleanup. */
222 munmap (cache, cache_size);
223 close (fd);
226 /* Initialize cache data structures. */
227 void
228 init_cache (void)
230 entries = NULL;
235 static
236 int compare (const struct cache_entry *e1, const struct cache_entry *e2)
238 int res;
240 /* We need to swap entries here to get the correct sort order. */
241 res = _dl_cache_libcmp (e2->lib, e1->lib);
242 if (res == 0)
244 if (e1->flags < e2->flags)
245 return 1;
246 else if (e1->flags > e2->flags)
247 return -1;
248 /* Sort by most specific hwcap. */
249 else if (e2->bits_hwcap > e1->bits_hwcap)
250 return 1;
251 else if (e2->bits_hwcap < e1->bits_hwcap)
252 return -1;
253 else if (e2->hwcap > e1->hwcap)
254 return 1;
255 else if (e2->hwcap < e1->hwcap)
256 return -1;
257 if (e2->osversion > e1->osversion)
258 return 1;
259 if (e2->osversion < e1->osversion)
260 return -1;
262 return res;
265 /* Save the contents of the cache. */
266 void
267 save_cache (const char *cache_name)
269 struct cache_entry *entry;
270 int fd, idx_old, idx_new;
271 size_t total_strlen, len;
272 char *strings, *str, *temp_name;
273 struct cache_file *file_entries = NULL;
274 struct cache_file_new *file_entries_new = NULL;
275 size_t file_entries_size = 0;
276 size_t file_entries_new_size = 0;
277 unsigned int str_offset;
278 /* Number of cache entries. */
279 int cache_entry_count = 0;
280 /* Number of normal cache entries. */
281 int cache_entry_old_count = 0;
282 /* Pad for alignment of cache_file_new. */
283 size_t pad;
285 /* The cache entries are sorted already, save them in this order. */
287 /* Count the length of all strings. */
288 /* The old format doesn't contain hwcap entries and doesn't contain
289 libraries in subdirectories with hwcaps entries. Count therefore
290 also all entries with hwcap == 0. */
291 total_strlen = 0;
292 for (entry = entries; entry != NULL; entry = entry->next)
294 /* Account the final NULs. */
295 total_strlen += strlen (entry->lib) + strlen (entry->path) + 2;
296 ++cache_entry_count;
297 if (entry->hwcap == 0)
298 ++cache_entry_old_count;
301 /* Create the on disk cache structure. */
302 /* First an array for all strings. */
303 strings = (char *)xmalloc (total_strlen);
305 if (opt_format != 2)
307 /* struct cache_file_new is 64-bit aligned on some arches while
308 only 32-bit aligned on other arches. Duplicate last old
309 cache entry so that new cache in ld.so.cache can be used by
310 both. */
311 if (opt_format != 0)
312 cache_entry_old_count = (cache_entry_old_count + 1) & ~1;
314 /* And the list of all entries in the old format. */
315 file_entries_size = sizeof (struct cache_file)
316 + cache_entry_old_count * sizeof (struct file_entry);
317 file_entries = (struct cache_file *) xmalloc (file_entries_size);
319 /* Fill in the header. */
320 memset (file_entries, 0, sizeof (struct cache_file));
321 memcpy (file_entries->magic, CACHEMAGIC, sizeof CACHEMAGIC - 1);
323 file_entries->nlibs = cache_entry_old_count;
326 if (opt_format != 0)
328 /* And the list of all entries in the new format. */
329 file_entries_new_size = sizeof (struct cache_file_new)
330 + cache_entry_count * sizeof (struct file_entry_new);
331 file_entries_new =
332 (struct cache_file_new *) xmalloc (file_entries_new_size);
334 /* Fill in the header. */
335 memset (file_entries_new, 0, sizeof (struct cache_file_new));
336 memcpy (file_entries_new->magic, CACHEMAGIC_NEW,
337 sizeof CACHEMAGIC_NEW - 1);
338 memcpy (file_entries_new->version, CACHE_VERSION,
339 sizeof CACHE_VERSION - 1);
341 file_entries_new->nlibs = cache_entry_count;
342 file_entries_new->len_strings = total_strlen;
345 pad = ALIGN_CACHE (file_entries_size) - file_entries_size;
347 /* If we have both formats, we hide the new format in the strings
348 table, we have to adjust all string indices for this so that
349 old libc5/glibc 2 dynamic linkers just ignore them. */
350 if (opt_format != 0)
351 str_offset = file_entries_new_size;
352 else
353 str_offset = 0;
355 str = strings;
356 for (idx_old = 0, idx_new = 0, entry = entries; entry != NULL;
357 entry = entry->next, ++idx_new)
359 /* First the library. */
360 if (opt_format != 2 && entry->hwcap == 0)
362 file_entries->libs[idx_old].flags = entry->flags;
363 /* XXX: Actually we can optimize here and remove duplicates. */
364 file_entries->libs[idx_old].key = str_offset + pad;
366 if (opt_format != 0)
368 /* We could subtract file_entries_new_size from str_offset -
369 not doing so makes the code easier, the string table
370 always begins at the beginning of the the new cache
371 struct. */
372 file_entries_new->libs[idx_new].flags = entry->flags;
373 file_entries_new->libs[idx_new].osversion = entry->osversion;
374 file_entries_new->libs[idx_new].hwcap = entry->hwcap;
375 file_entries_new->libs[idx_new].key = str_offset;
377 len = strlen (entry->lib);
378 str = stpcpy (str, entry->lib);
379 /* Account the final NUL. */
380 ++str;
381 str_offset += len + 1;
382 /* Then the path. */
383 if (opt_format != 2 && entry->hwcap == 0)
384 file_entries->libs[idx_old].value = str_offset + pad;
385 if (opt_format != 0)
386 file_entries_new->libs[idx_new].value = str_offset;
387 len = strlen (entry->path);
388 str = stpcpy (str, entry->path);
389 /* Account the final NUL. */
390 ++str;
391 str_offset += len + 1;
392 /* Ignore entries with hwcap for old format. */
393 if (entry->hwcap == 0)
394 ++idx_old;
397 /* Duplicate last old cache entry if needed. */
398 if (opt_format != 2
399 && idx_old < cache_entry_old_count)
400 file_entries->libs[idx_old] = file_entries->libs[idx_old - 1];
402 /* Write out the cache. */
404 /* Write cache first to a temporary file and rename it later. */
405 temp_name = xmalloc (strlen (cache_name) + 2);
406 sprintf (temp_name, "%s~", cache_name);
407 /* First remove an old copy if it exists. */
408 if (unlink (temp_name) && errno != ENOENT)
409 error (EXIT_FAILURE, errno, _("Can't remove old temporary cache file %s"),
410 temp_name);
412 /* Create file. */
413 fd = open (temp_name, O_CREAT|O_WRONLY|O_TRUNC|O_NOFOLLOW,
414 S_IROTH|S_IRGRP|S_IRUSR|S_IWUSR);
415 if (fd < 0)
416 error (EXIT_FAILURE, errno, _("Can't create temporary cache file %s"),
417 temp_name);
419 /* Write contents. */
420 if (opt_format != 2)
422 if (write (fd, file_entries, file_entries_size)
423 != (ssize_t) file_entries_size)
424 error (EXIT_FAILURE, errno, _("Writing of cache data failed"));
426 if (opt_format != 0)
428 /* Align cache. */
429 if (opt_format != 2)
431 char zero[pad];
432 memset (zero, '\0', pad);
433 if (write (fd, zero, pad) != (ssize_t) pad)
434 error (EXIT_FAILURE, errno, _("Writing of cache data failed"));
436 if (write (fd, file_entries_new, file_entries_new_size)
437 != (ssize_t) file_entries_new_size)
438 error (EXIT_FAILURE, errno, _("Writing of cache data failed"));
441 if (write (fd, strings, total_strlen) != (ssize_t) total_strlen)
442 error (EXIT_FAILURE, errno, _("Writing of cache data failed."));
444 close (fd);
446 /* Make sure user can always read cache file */
447 if (chmod (temp_name, S_IROTH|S_IRGRP|S_IRUSR|S_IWUSR))
448 error (EXIT_FAILURE, errno,
449 _("Changing access rights of %s to %#o failed"), temp_name,
450 S_IROTH|S_IRGRP|S_IRUSR|S_IWUSR);
452 /* Move temporary to its final location. */
453 if (rename (temp_name, cache_name))
454 error (EXIT_FAILURE, errno, _("Renaming of %s to %s failed"), temp_name,
455 cache_name);
457 /* Free all allocated memory. */
458 free (file_entries_new);
459 free (file_entries);
460 free (strings);
462 while (entries)
464 entry = entries;
465 free (entry->path);
466 free (entry->lib);
467 entries = entries->next;
468 free (entry);
473 /* Add one library to the cache. */
474 void
475 add_to_cache (const char *path, const char *lib, int flags,
476 unsigned int osversion, uint64_t hwcap)
478 struct cache_entry *new_entry, *ptr, *prev;
479 char *full_path;
480 size_t len, i;
482 new_entry = (struct cache_entry *) xmalloc (sizeof (struct cache_entry));
484 len = strlen (lib) + strlen (path) + 2;
486 full_path = (char *) xmalloc (len);
487 snprintf (full_path, len, "%s/%s", path, lib);
489 new_entry->lib = xstrdup (lib);
490 new_entry->path = full_path;
491 new_entry->flags = flags;
492 new_entry->osversion = osversion;
493 new_entry->hwcap = hwcap;
494 new_entry->bits_hwcap = 0;
496 /* Count the number of bits set in the masked value. */
497 for (i = 0; (~((1ULL << i) - 1) & hwcap) != 0 && i < 8 * sizeof (hwcap); ++i)
498 if ((hwcap & (1ULL << i)) != 0)
499 ++new_entry->bits_hwcap;
502 /* Keep the list sorted - search for right place to insert. */
503 ptr = entries;
504 prev = entries;
505 while (ptr != NULL)
507 if (compare (ptr, new_entry) > 0)
508 break;
509 prev = ptr;
510 ptr = ptr->next;
512 /* Is this the first entry? */
513 if (ptr == entries)
515 new_entry->next = entries;
516 entries = new_entry;
518 else
520 new_entry->next = prev->next;
521 prev->next = new_entry;