Update.
[glibc.git] / elf / cache.c
blob1977ce1dd44ec2cf6e9cc0ff7af68f501e503910
1 /* Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Andreas Jaeger <aj@suse.de>, 1999.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <error.h>
22 #include <dirent.h>
23 #include <inttypes.h>
24 #include <libintl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/fcntl.h>
30 #include <sys/mman.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
34 #include "ldconfig.h"
35 #include "dl-cache.h"
37 struct cache_entry
39 char *lib; /* Library name. */
40 char *path; /* Path to find library. */
41 int flags; /* Flags to indicate kind of library. */
42 unsigned int osversion; /* Required OS version. */
43 uint64_t hwcap; /* Important hardware capabilities. */
44 int bits_hwcap; /* Number of bits set in hwcap. */
45 struct cache_entry *next; /* Next entry in list. */
48 /* List of all cache entries. */
49 static struct cache_entry *entries;
51 static const char *flag_descr[] =
52 { "libc4", "ELF", "libc5", "libc6"};
54 /* Print a single entry. */
55 static void
56 print_entry (const char *lib, int flag, unsigned int osversion,
57 uint64_t hwcap, const char *key)
59 printf ("\t%s (", lib);
60 switch (flag & FLAG_TYPE_MASK)
62 case FLAG_LIBC4:
63 case FLAG_ELF:
64 case FLAG_ELF_LIBC5:
65 case FLAG_ELF_LIBC6:
66 fputs (flag_descr[flag & FLAG_TYPE_MASK], stdout);
67 break;
68 default:
69 fputs (_("unknown"), stdout);
70 break;
72 switch (flag & FLAG_REQUIRED_MASK)
74 #ifdef __sparc__
75 case FLAG_SPARC_LIB64:
76 fputs (",64bit", stdout);
77 #endif
78 #if defined __ia64__ || defined __i386__
79 case FLAG_IA64_LIB64:
80 fputs (",IA-64", stdout);
81 break;
82 #endif
83 #if defined __x86_64__ || defined __i386__
84 case FLAG_X8664_LIB64:
85 fputs (",x86-64", stdout);
86 break;
87 #endif
88 #if defined __s390__ || defined __s390x__
89 case FLAG_S390_LIB64:
90 fputs(",64bit", stdout);
91 #endif
92 case 0:
93 break;
94 default:
95 printf (",%d", flag & FLAG_REQUIRED_MASK);
96 break;
98 if (hwcap != 0)
99 printf (", hwcap: 0x%" 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] = N_("Unknown OS")
109 unsigned int os = osversion >> 24;
111 printf (_(", OS ABI: %s %d.%d.%d"),
112 _(abi_tag_os[os > 3 ? 3 : os]),
113 (osversion >> 16) & 0xff,
114 (osversion >> 8) & 0xff,
115 osversion & 0xff);
117 printf (") => %s\n", key);
121 /* Print the whole cache file, if a file contains the new cache format
122 hidden in the old one, print the contents of the new format. */
123 void
124 print_cache (const char *cache_name)
126 size_t cache_size;
127 struct stat64 st;
128 int fd;
129 unsigned int i;
130 struct cache_file *cache;
131 struct cache_file_new *cache_new = NULL;
132 const char *cache_data;
133 int format = 0;
135 fd = open (cache_name, O_RDONLY);
136 if (fd < 0)
137 error (EXIT_FAILURE, errno, _("Can't open cache file %s\n"), cache_name);
139 if (fstat64 (fd, &st) < 0
140 /* No need to map the file if it is empty. */
141 || st.st_size == 0)
143 close (fd);
144 return;
147 cache = mmap (0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
148 if (cache == MAP_FAILED)
149 error (EXIT_FAILURE, errno, _("mmap of cache file failed.\n"));
150 cache_size = st.st_size;
152 if (cache_size < sizeof (struct cache_file))
153 error (EXIT_FAILURE, 0, _("File is not a cache file.\n"));
155 if (memcmp (cache->magic, CACHEMAGIC, sizeof CACHEMAGIC - 1))
157 /* This can only be the new format without the old one. */
158 cache_new = (struct cache_file_new *) cache;
160 if (memcmp (cache_new->magic, CACHEMAGIC_NEW, sizeof CACHEMAGIC_NEW - 1)
161 || memcmp (cache_new->version, CACHE_VERSION,
162 sizeof CACHE_VERSION - 1))
163 error (EXIT_FAILURE, 0, _("File is not a cache file.\n"));
164 format = 1;
165 /* This is where the strings start. */
166 cache_data = (const char *) cache_new;
168 else
170 size_t offset = ALIGN_CACHE (sizeof (struct cache_file)
171 + (cache->nlibs
172 * sizeof (struct file_entry)));
173 /* This is where the strings start. */
174 cache_data = (const char *) &cache->libs[cache->nlibs];
176 /* Check for a new cache embedded in the old format. */
177 if (cache_size >
178 (offset + sizeof (struct cache_file_new)))
181 cache_new = (struct cache_file_new *) ((void *)cache + offset);
183 if (memcmp (cache_new->magic, CACHEMAGIC_NEW,
184 sizeof CACHEMAGIC_NEW - 1) == 0
185 && memcmp (cache_new->version, CACHE_VERSION,
186 sizeof CACHE_VERSION - 1) == 0)
188 cache_data = (const char *) cache_new;
189 format = 1;
194 if (format == 0)
196 printf (_("%d libs found in cache `%s'\n"), cache->nlibs, cache_name);
198 /* Print everything. */
199 for (i = 0; i < cache->nlibs; i++)
200 print_entry (cache_data + cache->libs[i].key,
201 cache->libs[i].flags, 0, 0,
202 cache_data + cache->libs[i].value);
204 else if (format == 1)
206 printf (_("%d libs found in cache `%s'\n"),
207 cache_new->nlibs, cache_name);
209 /* Print everything. */
210 for (i = 0; i < cache_new->nlibs; i++)
211 print_entry (cache_data + cache_new->libs[i].key,
212 cache_new->libs[i].flags,
213 cache_new->libs[i].osversion,
214 cache_new->libs[i].hwcap,
215 cache_data + cache_new->libs[i].value);
217 /* Cleanup. */
218 munmap (cache, cache_size);
219 close (fd);
222 /* Initialize cache data structures. */
223 void
224 init_cache (void)
226 entries = NULL;
231 static
232 int compare (const struct cache_entry *e1, const struct cache_entry *e2)
234 int res;
236 /* We need to swap entries here to get the correct sort order. */
237 res = _dl_cache_libcmp (e2->lib, e1->lib);
238 if (res == 0)
240 if (e1->flags < e2->flags)
241 return 1;
242 else if (e1->flags > e2->flags)
243 return -1;
244 /* Sort by most specific hwcap. */
245 else if (e2->bits_hwcap > e1->bits_hwcap)
246 return 1;
247 else if (e2->bits_hwcap < e1->bits_hwcap)
248 return -1;
249 else if (e2->hwcap > e1->hwcap)
250 return 1;
251 else if (e2->hwcap < e1->hwcap)
252 return -1;
253 if (e2->osversion > e1->osversion)
254 return 1;
255 if (e2->osversion < e1->osversion)
256 return -1;
258 return res;
261 /* Save the contents of the cache. */
262 void
263 save_cache (const char *cache_name)
265 struct cache_entry *entry;
266 int fd, idx_old, idx_new;
267 size_t total_strlen, len;
268 char *strings, *str, *temp_name;
269 struct cache_file *file_entries = NULL;
270 struct cache_file_new *file_entries_new = NULL;
271 size_t file_entries_size = 0;
272 size_t file_entries_new_size = 0;
273 unsigned int str_offset;
274 /* Number of cache entries. */
275 int cache_entry_count = 0;
276 /* Number of normal cache entries. */
277 int cache_entry_old_count = 0;
278 /* Pad for alignment of cache_file_new. */
279 size_t pad;
281 /* The cache entries are sorted already, save them in this order. */
283 /* Count the length of all strings. */
284 /* The old format doesn't contain hwcap entries and doesn't contain
285 libraries in subdirectories with hwcaps entries. Count therefore
286 also all entries with hwcap == 0. */
287 total_strlen = 0;
288 for (entry = entries; entry != NULL; entry = entry->next)
290 /* Account the final NULs. */
291 total_strlen += strlen (entry->lib) + strlen (entry->path) + 2;
292 ++cache_entry_count;
293 if (entry->hwcap == 0)
294 ++cache_entry_old_count;
297 /* Create the on disk cache structure. */
298 /* First an array for all strings. */
299 strings = (char *)xmalloc (total_strlen);
301 if (opt_format != 2)
303 /* And the list of all entries in the old format. */
304 file_entries_size = sizeof (struct cache_file)
305 + cache_entry_old_count * sizeof (struct file_entry);
306 file_entries = (struct cache_file *) xmalloc (file_entries_size);
308 /* Fill in the header. */
309 memset (file_entries, 0, sizeof (struct cache_file));
310 memcpy (file_entries->magic, CACHEMAGIC, sizeof CACHEMAGIC - 1);
312 file_entries->nlibs = cache_entry_old_count;
315 if (opt_format != 0)
317 /* And the list of all entries in the new format. */
318 file_entries_new_size = sizeof (struct cache_file_new)
319 + cache_entry_count * sizeof (struct file_entry_new);
320 file_entries_new =
321 (struct cache_file_new *) xmalloc (file_entries_new_size);
323 /* Fill in the header. */
324 memset (file_entries_new, 0, sizeof (struct cache_file_new));
325 memcpy (file_entries_new->magic, CACHEMAGIC_NEW,
326 sizeof CACHEMAGIC_NEW - 1);
327 memcpy (file_entries_new->version, CACHE_VERSION,
328 sizeof CACHE_VERSION - 1);
330 file_entries_new->nlibs = cache_entry_count;
331 file_entries_new->len_strings = total_strlen;
334 pad = ALIGN_CACHE (file_entries_size) - file_entries_size;
336 /* If we have both formats, we hide the new format in the strings
337 table, we have to adjust all string indices for this so that
338 old libc5/glibc 2 dynamic linkers just ignore them. */
339 if (opt_format != 0)
340 str_offset = file_entries_new_size;
341 else
342 str_offset = 0;
344 str = strings;
345 for (idx_old = 0, idx_new = 0, entry = entries; entry != NULL;
346 entry = entry->next, ++idx_new)
348 /* First the library. */
349 if (opt_format != 2)
351 file_entries->libs[idx_old].flags = entry->flags;
352 /* XXX: Actually we can optimize here and remove duplicates. */
353 file_entries->libs[idx_old].key = str_offset + pad;
355 if (opt_format != 0)
357 /* We could subtract file_entries_new_size from str_offset -
358 not doing so makes the code easier, the string table
359 always begins at the beginning of the the new cache
360 struct. */
361 file_entries_new->libs[idx_new].flags = entry->flags;
362 file_entries_new->libs[idx_new].osversion = entry->osversion;
363 file_entries_new->libs[idx_new].hwcap = entry->hwcap;
364 file_entries_new->libs[idx_new].key = str_offset;
366 len = strlen (entry->lib);
367 str = stpcpy (str, entry->lib);
368 /* Account the final NUL. */
369 ++str;
370 str_offset += len + 1;
371 /* Then the path. */
372 if (opt_format != 2)
373 file_entries->libs[idx_old].value = str_offset + pad;
374 if (opt_format != 0)
375 file_entries_new->libs[idx_new].value = str_offset;
376 len = strlen (entry->path);
377 str = stpcpy (str, entry->path);
378 /* Account the final NUL. */
379 ++str;
380 str_offset += len + 1;
381 /* Ignore entries with hwcap for old format. */
382 if (entry->hwcap == 0)
383 ++idx_old;
386 /* Write out the cache. */
388 /* Write cache first to a temporary file and rename it later. */
389 temp_name = xmalloc (strlen (cache_name) + 2);
390 sprintf (temp_name, "%s~", cache_name);
391 /* First remove an old copy if it exists. */
392 if (unlink (temp_name) && errno != ENOENT)
393 error (EXIT_FAILURE, errno, _("Can't remove old temporary cache file %s"),
394 temp_name);
396 /* Create file. */
397 fd = open (temp_name, O_CREAT|O_WRONLY|O_TRUNC|O_NOFOLLOW,
398 S_IROTH|S_IRGRP|S_IRUSR|S_IWUSR);
399 if (fd < 0)
400 error (EXIT_FAILURE, errno, _("Can't create temporary cache file %s"),
401 temp_name);
403 /* Write contents. */
404 if (opt_format != 2)
406 if (write (fd, file_entries, file_entries_size)
407 != (ssize_t)file_entries_size)
408 error (EXIT_FAILURE, errno, _("Writing of cache data failed"));
410 if (opt_format != 0)
412 /* Align cache. */
413 if (opt_format != 2)
415 char zero[pad];
416 if (write (fd, zero, pad) != (ssize_t)pad)
417 error (EXIT_FAILURE, errno, _("Writing of cache data failed"));
419 if (write (fd, file_entries_new, file_entries_new_size)
420 != (ssize_t)file_entries_new_size)
421 error (EXIT_FAILURE, errno, _("Writing of cache data failed"));
424 if (write (fd, strings, total_strlen) != (ssize_t)total_strlen)
425 error (EXIT_FAILURE, errno, _("Writing of cache data failed."));
427 close (fd);
429 /* Make sure user can always read cache file */
430 if (chmod (temp_name, S_IROTH|S_IRGRP|S_IRUSR|S_IWUSR))
431 error (EXIT_FAILURE, errno,
432 _("Changing access rights of %s to %#o failed"), temp_name,
433 S_IROTH|S_IRGRP|S_IRUSR|S_IWUSR);
435 /* Move temporary to its final location. */
436 if (rename (temp_name, cache_name))
437 error (EXIT_FAILURE, errno, _("Renaming of %s to %s failed"), temp_name,
438 cache_name);
440 /* Free all allocated memory. */
441 free (file_entries);
442 free (strings);
444 while (entries)
446 entry = entries;
447 free (entry->path);
448 free (entry->lib);
449 entries = entries->next;
450 free (entry);
455 /* Add one library to the cache. */
456 void
457 add_to_cache (const char *path, const char *lib, int flags,
458 unsigned int osversion, uint64_t hwcap)
460 struct cache_entry *new_entry, *ptr, *prev;
461 char *full_path;
462 int len, i;
464 new_entry = (struct cache_entry *) xmalloc (sizeof (struct cache_entry));
466 len = strlen (lib) + strlen (path) + 2;
468 full_path = (char *) xmalloc (len);
469 snprintf (full_path, len, "%s/%s", path, lib);
471 new_entry->lib = xstrdup (lib);
472 new_entry->path = full_path;
473 new_entry->flags = flags;
474 new_entry->osversion = osversion;
475 new_entry->hwcap = hwcap;
476 new_entry->bits_hwcap = 0;
478 /* Count the number of bits set in the masked value. */
479 for (i = 0; (~((1ULL << i) - 1) & hwcap) != 0; ++i)
480 if ((hwcap & (1ULL << i)) != 0)
481 ++new_entry->bits_hwcap;
484 /* Keep the list sorted - search for right place to insert. */
485 ptr = entries;
486 prev = entries;
487 while (ptr != NULL)
489 if (compare (ptr, new_entry) > 0)
490 break;
491 prev = ptr;
492 ptr = ptr->next;
494 /* Is this the first entry? */
495 if (ptr == entries)
497 new_entry->next = entries;
498 entries = new_entry;
500 else
502 new_entry->next = prev->next;
503 prev->next = new_entry;