* sysdeps/unix/sysv/linux/ia64/pt-initfini.c: Fix unterminated
[glibc.git] / elf / cache.c
blob67874b3c9e27523de4b7efdf151dc14370a199db
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 case FLAG_SPARC_LIB64:
75 fputs (",64bit", stdout);
76 break;
77 case FLAG_IA64_LIB64:
78 fputs (",IA-64", stdout);
79 break;
80 case FLAG_X8664_LIB64:
81 fputs (",x86-64", stdout);
82 break;
83 case FLAG_S390_LIB64:
84 fputs(",64bit", stdout);
85 break;
86 case FLAG_POWERPC_LIB64:
87 fputs(",64bit", stdout);
88 break;
89 case 0:
90 break;
91 default:
92 printf (",%d", flag & FLAG_REQUIRED_MASK);
93 break;
95 if (hwcap != 0)
96 printf (", hwcap: 0x%" PRIx64, hwcap);
97 if (osversion != 0)
99 static const char *const abi_tag_os[] =
101 [0] = "Linux",
102 [1] = "Hurd",
103 [2] = "Solaris",
104 [3] = "FreeBSD",
105 [4] = N_("Unknown OS")
107 #define MAXTAG (sizeof abi_tag_os / sizeof abi_tag_os[0] - 1)
108 unsigned int os = osversion >> 24;
110 printf (_(", OS ABI: %s %d.%d.%d"),
111 _(abi_tag_os[os > MAXTAG ? MAXTAG : os]),
112 (osversion >> 16) & 0xff,
113 (osversion >> 8) & 0xff,
114 osversion & 0xff);
116 printf (") => %s\n", key);
120 /* Print the whole cache file, if a file contains the new cache format
121 hidden in the old one, print the contents of the new format. */
122 void
123 print_cache (const char *cache_name)
125 size_t cache_size;
126 struct stat64 st;
127 int fd;
128 unsigned int i;
129 struct cache_file *cache;
130 struct cache_file_new *cache_new = NULL;
131 const char *cache_data;
132 int format = 0;
134 fd = open (cache_name, O_RDONLY);
135 if (fd < 0)
136 error (EXIT_FAILURE, errno, _("Can't open cache file %s\n"), cache_name);
138 if (fstat64 (fd, &st) < 0
139 /* No need to map the file if it is empty. */
140 || st.st_size == 0)
142 close (fd);
143 return;
146 cache = mmap (0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
147 if (cache == MAP_FAILED)
148 error (EXIT_FAILURE, errno, _("mmap of cache file failed.\n"));
149 cache_size = st.st_size;
151 if (cache_size < sizeof (struct cache_file))
152 error (EXIT_FAILURE, 0, _("File is not a cache file.\n"));
154 if (memcmp (cache->magic, CACHEMAGIC, sizeof CACHEMAGIC - 1))
156 /* This can only be the new format without the old one. */
157 cache_new = (struct cache_file_new *) cache;
159 if (memcmp (cache_new->magic, CACHEMAGIC_NEW, sizeof CACHEMAGIC_NEW - 1)
160 || memcmp (cache_new->version, CACHE_VERSION,
161 sizeof CACHE_VERSION - 1))
162 error (EXIT_FAILURE, 0, _("File is not a cache file.\n"));
163 format = 1;
164 /* This is where the strings start. */
165 cache_data = (const char *) cache_new;
167 else
169 size_t offset = ALIGN_CACHE (sizeof (struct cache_file)
170 + (cache->nlibs
171 * sizeof (struct file_entry)));
172 /* This is where the strings start. */
173 cache_data = (const char *) &cache->libs[cache->nlibs];
175 /* Check for a new cache embedded in the old format. */
176 if (cache_size >
177 (offset + sizeof (struct cache_file_new)))
180 cache_new = (struct cache_file_new *) ((void *)cache + offset);
182 if (memcmp (cache_new->magic, CACHEMAGIC_NEW,
183 sizeof CACHEMAGIC_NEW - 1) == 0
184 && memcmp (cache_new->version, CACHE_VERSION,
185 sizeof CACHE_VERSION - 1) == 0)
187 cache_data = (const char *) cache_new;
188 format = 1;
193 if (format == 0)
195 printf (_("%d libs found in cache `%s'\n"), cache->nlibs, cache_name);
197 /* Print everything. */
198 for (i = 0; i < cache->nlibs; i++)
199 print_entry (cache_data + cache->libs[i].key,
200 cache->libs[i].flags, 0, 0,
201 cache_data + cache->libs[i].value);
203 else if (format == 1)
205 printf (_("%d libs found in cache `%s'\n"),
206 cache_new->nlibs, cache_name);
208 /* Print everything. */
209 for (i = 0; i < cache_new->nlibs; i++)
210 print_entry (cache_data + cache_new->libs[i].key,
211 cache_new->libs[i].flags,
212 cache_new->libs[i].osversion,
213 cache_new->libs[i].hwcap,
214 cache_data + cache_new->libs[i].value);
216 /* Cleanup. */
217 munmap (cache, cache_size);
218 close (fd);
221 /* Initialize cache data structures. */
222 void
223 init_cache (void)
225 entries = NULL;
230 static
231 int compare (const struct cache_entry *e1, const struct cache_entry *e2)
233 int res;
235 /* We need to swap entries here to get the correct sort order. */
236 res = _dl_cache_libcmp (e2->lib, e1->lib);
237 if (res == 0)
239 if (e1->flags < e2->flags)
240 return 1;
241 else if (e1->flags > e2->flags)
242 return -1;
243 /* Sort by most specific hwcap. */
244 else if (e2->bits_hwcap > e1->bits_hwcap)
245 return 1;
246 else if (e2->bits_hwcap < e1->bits_hwcap)
247 return -1;
248 else if (e2->hwcap > e1->hwcap)
249 return 1;
250 else if (e2->hwcap < e1->hwcap)
251 return -1;
252 if (e2->osversion > e1->osversion)
253 return 1;
254 if (e2->osversion < e1->osversion)
255 return -1;
257 return res;
260 /* Save the contents of the cache. */
261 void
262 save_cache (const char *cache_name)
264 struct cache_entry *entry;
265 int fd, idx_old, idx_new;
266 size_t total_strlen, len;
267 char *strings, *str, *temp_name;
268 struct cache_file *file_entries = NULL;
269 struct cache_file_new *file_entries_new = NULL;
270 size_t file_entries_size = 0;
271 size_t file_entries_new_size = 0;
272 unsigned int str_offset;
273 /* Number of cache entries. */
274 int cache_entry_count = 0;
275 /* Number of normal cache entries. */
276 int cache_entry_old_count = 0;
277 /* Pad for alignment of cache_file_new. */
278 size_t pad;
280 /* The cache entries are sorted already, save them in this order. */
282 /* Count the length of all strings. */
283 /* The old format doesn't contain hwcap entries and doesn't contain
284 libraries in subdirectories with hwcaps entries. Count therefore
285 also all entries with hwcap == 0. */
286 total_strlen = 0;
287 for (entry = entries; entry != NULL; entry = entry->next)
289 /* Account the final NULs. */
290 total_strlen += strlen (entry->lib) + strlen (entry->path) + 2;
291 ++cache_entry_count;
292 if (entry->hwcap == 0)
293 ++cache_entry_old_count;
296 /* Create the on disk cache structure. */
297 /* First an array for all strings. */
298 strings = (char *)xmalloc (total_strlen);
300 if (opt_format != 2)
302 /* And the list of all entries in the old format. */
303 file_entries_size = sizeof (struct cache_file)
304 + cache_entry_old_count * sizeof (struct file_entry);
305 file_entries = (struct cache_file *) xmalloc (file_entries_size);
307 /* Fill in the header. */
308 memset (file_entries, 0, sizeof (struct cache_file));
309 memcpy (file_entries->magic, CACHEMAGIC, sizeof CACHEMAGIC - 1);
311 file_entries->nlibs = cache_entry_old_count;
314 if (opt_format != 0)
316 /* And the list of all entries in the new format. */
317 file_entries_new_size = sizeof (struct cache_file_new)
318 + cache_entry_count * sizeof (struct file_entry_new);
319 file_entries_new =
320 (struct cache_file_new *) xmalloc (file_entries_new_size);
322 /* Fill in the header. */
323 memset (file_entries_new, 0, sizeof (struct cache_file_new));
324 memcpy (file_entries_new->magic, CACHEMAGIC_NEW,
325 sizeof CACHEMAGIC_NEW - 1);
326 memcpy (file_entries_new->version, CACHE_VERSION,
327 sizeof CACHE_VERSION - 1);
329 file_entries_new->nlibs = cache_entry_count;
330 file_entries_new->len_strings = total_strlen;
333 pad = ALIGN_CACHE (file_entries_size) - file_entries_size;
335 /* If we have both formats, we hide the new format in the strings
336 table, we have to adjust all string indices for this so that
337 old libc5/glibc 2 dynamic linkers just ignore them. */
338 if (opt_format != 0)
339 str_offset = file_entries_new_size;
340 else
341 str_offset = 0;
343 str = strings;
344 for (idx_old = 0, idx_new = 0, entry = entries; entry != NULL;
345 entry = entry->next, ++idx_new)
347 /* First the library. */
348 if (opt_format != 2)
350 file_entries->libs[idx_old].flags = entry->flags;
351 /* XXX: Actually we can optimize here and remove duplicates. */
352 file_entries->libs[idx_old].key = str_offset + pad;
354 if (opt_format != 0)
356 /* We could subtract file_entries_new_size from str_offset -
357 not doing so makes the code easier, the string table
358 always begins at the beginning of the the new cache
359 struct. */
360 file_entries_new->libs[idx_new].flags = entry->flags;
361 file_entries_new->libs[idx_new].osversion = entry->osversion;
362 file_entries_new->libs[idx_new].hwcap = entry->hwcap;
363 file_entries_new->libs[idx_new].key = str_offset;
365 len = strlen (entry->lib);
366 str = stpcpy (str, entry->lib);
367 /* Account the final NUL. */
368 ++str;
369 str_offset += len + 1;
370 /* Then the path. */
371 if (opt_format != 2)
372 file_entries->libs[idx_old].value = str_offset + pad;
373 if (opt_format != 0)
374 file_entries_new->libs[idx_new].value = str_offset;
375 len = strlen (entry->path);
376 str = stpcpy (str, entry->path);
377 /* Account the final NUL. */
378 ++str;
379 str_offset += len + 1;
380 /* Ignore entries with hwcap for old format. */
381 if (entry->hwcap == 0)
382 ++idx_old;
385 /* Write out the cache. */
387 /* Write cache first to a temporary file and rename it later. */
388 temp_name = xmalloc (strlen (cache_name) + 2);
389 sprintf (temp_name, "%s~", cache_name);
390 /* First remove an old copy if it exists. */
391 if (unlink (temp_name) && errno != ENOENT)
392 error (EXIT_FAILURE, errno, _("Can't remove old temporary cache file %s"),
393 temp_name);
395 /* Create file. */
396 fd = open (temp_name, O_CREAT|O_WRONLY|O_TRUNC|O_NOFOLLOW,
397 S_IROTH|S_IRGRP|S_IRUSR|S_IWUSR);
398 if (fd < 0)
399 error (EXIT_FAILURE, errno, _("Can't create temporary cache file %s"),
400 temp_name);
402 /* Write contents. */
403 if (opt_format != 2)
405 if (write (fd, file_entries, file_entries_size)
406 != (ssize_t)file_entries_size)
407 error (EXIT_FAILURE, errno, _("Writing of cache data failed"));
409 if (opt_format != 0)
411 /* Align cache. */
412 if (opt_format != 2)
414 char zero[pad];
415 if (write (fd, zero, pad) != (ssize_t)pad)
416 error (EXIT_FAILURE, errno, _("Writing of cache data failed"));
418 if (write (fd, file_entries_new, file_entries_new_size)
419 != (ssize_t)file_entries_new_size)
420 error (EXIT_FAILURE, errno, _("Writing of cache data failed"));
423 if (write (fd, strings, total_strlen) != (ssize_t)total_strlen)
424 error (EXIT_FAILURE, errno, _("Writing of cache data failed."));
426 close (fd);
428 /* Make sure user can always read cache file */
429 if (chmod (temp_name, S_IROTH|S_IRGRP|S_IRUSR|S_IWUSR))
430 error (EXIT_FAILURE, errno,
431 _("Changing access rights of %s to %#o failed"), temp_name,
432 S_IROTH|S_IRGRP|S_IRUSR|S_IWUSR);
434 /* Move temporary to its final location. */
435 if (rename (temp_name, cache_name))
436 error (EXIT_FAILURE, errno, _("Renaming of %s to %s failed"), temp_name,
437 cache_name);
439 /* Free all allocated memory. */
440 free (file_entries);
441 free (strings);
443 while (entries)
445 entry = entries;
446 free (entry->path);
447 free (entry->lib);
448 entries = entries->next;
449 free (entry);
454 /* Add one library to the cache. */
455 void
456 add_to_cache (const char *path, const char *lib, int flags,
457 unsigned int osversion, uint64_t hwcap)
459 struct cache_entry *new_entry, *ptr, *prev;
460 char *full_path;
461 int len, i;
463 new_entry = (struct cache_entry *) xmalloc (sizeof (struct cache_entry));
465 len = strlen (lib) + strlen (path) + 2;
467 full_path = (char *) xmalloc (len);
468 snprintf (full_path, len, "%s/%s", path, lib);
470 new_entry->lib = xstrdup (lib);
471 new_entry->path = full_path;
472 new_entry->flags = flags;
473 new_entry->osversion = osversion;
474 new_entry->hwcap = hwcap;
475 new_entry->bits_hwcap = 0;
477 /* Count the number of bits set in the masked value. */
478 for (i = 0; (~((1ULL << i) - 1) & hwcap) != 0; ++i)
479 if ((hwcap & (1ULL << i)) != 0)
480 ++new_entry->bits_hwcap;
483 /* Keep the list sorted - search for right place to insert. */
484 ptr = entries;
485 prev = entries;
486 while (ptr != NULL)
488 if (compare (ptr, new_entry) > 0)
489 break;
490 prev = ptr;
491 ptr = ptr->next;
493 /* Is this the first entry? */
494 if (ptr == entries)
496 new_entry->next = entries;
497 entries = new_entry;
499 else
501 new_entry->next = prev->next;
502 prev->next = new_entry;