1 /* Copyright (C) 1999, 2000, 2001 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 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. */
29 #include <sys/fcntl.h>
32 #include <sys/types.h>
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. */
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
)
66 fputs (flag_descr
[flag
& FLAG_TYPE_MASK
], stdout
);
69 fputs (_("unknown"), stdout
);
72 switch (flag
& FLAG_REQUIRED_MASK
)
75 case FLAG_SPARC_LIB64
:
76 fputs (",64bit", stdout
);
78 #if defined __ia64__ || defined __i386__
80 fputs (",IA-64", stdout
);
82 #if defined __s390__ || defined __s390x__
84 fputs(",64bit", stdout
);
89 printf (",%d", flag
& FLAG_REQUIRED_MASK
);
93 printf (", hwcap: 0x%" PRIx64
, hwcap
);
96 static const char *const abi_tag_os
[] =
101 [3] = N_("Unknown OS")
103 unsigned int os
= osversion
>> 24;
105 printf (_(", OS ABI: %s %d.%d.%d"),
106 _(abi_tag_os
[os
> 3 ? 3 : os
]),
107 (osversion
>> 16) & 0xff,
108 (osversion
>> 8) & 0xff,
111 printf (") => %s\n", key
);
115 /* Print the whole cache file, if a file contains the new cache format
116 hidden in the old one, print the contents of the new format. */
118 print_cache (const char *cache_name
)
124 struct cache_file
*cache
;
125 struct cache_file_new
*cache_new
= NULL
;
126 const char *cache_data
;
129 fd
= open (cache_name
, O_RDONLY
);
131 error (EXIT_FAILURE
, errno
, _("Can't open cache file %s\n"), cache_name
);
133 if (fstat64 (fd
, &st
) < 0
134 /* No need to map the file if it is empty. */
141 cache
= mmap (0, st
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
142 if (cache
== MAP_FAILED
)
143 error (EXIT_FAILURE
, errno
, _("mmap of cache file failed.\n"));
144 cache_size
= st
.st_size
;
146 if (cache_size
< sizeof (struct cache_file
))
147 error (EXIT_FAILURE
, 0, _("File is not a cache file.\n"));
149 if (memcmp (cache
->magic
, CACHEMAGIC
, sizeof CACHEMAGIC
- 1))
151 /* This can only be the new format without the old one. */
152 cache_new
= (struct cache_file_new
*) cache
;
154 if (memcmp (cache_new
->magic
, CACHEMAGIC_NEW
, sizeof CACHEMAGIC_NEW
- 1)
155 || memcmp (cache_new
->version
, CACHE_VERSION
,
156 sizeof CACHE_VERSION
- 1))
157 error (EXIT_FAILURE
, 0, _("File is not a cache file.\n"));
159 /* This is where the strings start. */
160 cache_data
= (const char *) cache_new
;
164 size_t offset
= ALIGN_CACHE (sizeof (struct cache_file
)
166 * sizeof (struct file_entry
)));
167 /* This is where the strings start. */
168 cache_data
= (const char *) &cache
->libs
[cache
->nlibs
];
170 /* Check for a new cache embedded in the old format. */
172 (offset
+ sizeof (struct cache_file_new
)))
175 cache_new
= (struct cache_file_new
*) ((void *)cache
+ offset
);
177 if (memcmp (cache_new
->magic
, CACHEMAGIC_NEW
,
178 sizeof CACHEMAGIC_NEW
- 1) == 0
179 && memcmp (cache_new
->version
, CACHE_VERSION
,
180 sizeof CACHE_VERSION
- 1) == 0)
182 cache_data
= (const char *) cache_new
;
190 printf (_("%d libs found in cache `%s'\n"), cache
->nlibs
, cache_name
);
192 /* Print everything. */
193 for (i
= 0; i
< cache
->nlibs
; i
++)
194 print_entry (cache_data
+ cache
->libs
[i
].key
,
195 cache
->libs
[i
].flags
, 0, 0,
196 cache_data
+ cache
->libs
[i
].value
);
198 else if (format
== 1)
200 printf (_("%d libs found in cache `%s'\n"),
201 cache_new
->nlibs
, cache_name
);
203 /* Print everything. */
204 for (i
= 0; i
< cache_new
->nlibs
; i
++)
205 print_entry (cache_data
+ cache_new
->libs
[i
].key
,
206 cache_new
->libs
[i
].flags
,
207 cache_new
->libs
[i
].osversion
,
208 cache_new
->libs
[i
].hwcap
,
209 cache_data
+ cache_new
->libs
[i
].value
);
212 munmap (cache
, cache_size
);
216 /* Initialize cache data structures. */
226 int compare (const struct cache_entry
*e1
, const struct cache_entry
*e2
)
230 /* We need to swap entries here to get the correct sort order. */
231 res
= _dl_cache_libcmp (e2
->lib
, e1
->lib
);
234 if (e1
->flags
< e2
->flags
)
236 else if (e1
->flags
> e2
->flags
)
238 /* Sort by most specific hwcap. */
239 else if (e2
->bits_hwcap
> e1
->bits_hwcap
)
241 else if (e2
->bits_hwcap
< e1
->bits_hwcap
)
243 else if (e2
->hwcap
> e1
->hwcap
)
245 else if (e2
->hwcap
< e1
->hwcap
)
247 if (e2
->osversion
> e1
->osversion
)
249 if (e2
->osversion
< e1
->osversion
)
255 /* Save the contents of the cache. */
257 save_cache (const char *cache_name
)
259 struct cache_entry
*entry
;
260 int fd
, idx_old
, idx_new
;
261 size_t total_strlen
, len
;
262 char *strings
, *str
, *temp_name
;
263 struct cache_file
*file_entries
= NULL
;
264 struct cache_file_new
*file_entries_new
= NULL
;
265 size_t file_entries_size
= 0;
266 size_t file_entries_new_size
= 0;
267 unsigned int str_offset
;
268 /* Number of cache entries. */
269 int cache_entry_count
= 0;
270 /* Number of normal cache entries. */
271 int cache_entry_old_count
= 0;
272 /* Pad for alignment of cache_file_new. */
275 /* The cache entries are sorted already, save them in this order. */
277 /* Count the length of all strings. */
278 /* The old format doesn't contain hwcap entries and doesn't contain
279 libraries in subdirectories with hwcaps entries. Count therefore
280 also all entries with hwcap == 0. */
282 for (entry
= entries
; entry
!= NULL
; entry
= entry
->next
)
284 /* Account the final NULs. */
285 total_strlen
+= strlen (entry
->lib
) + strlen (entry
->path
) + 2;
287 if (entry
->hwcap
== 0)
288 ++cache_entry_old_count
;
291 /* Create the on disk cache structure. */
292 /* First an array for all strings. */
293 strings
= (char *)xmalloc (total_strlen
);
297 /* And the list of all entries in the old format. */
298 file_entries_size
= sizeof (struct cache_file
)
299 + cache_entry_old_count
* sizeof (struct file_entry
);
300 file_entries
= (struct cache_file
*) xmalloc (file_entries_size
);
302 /* Fill in the header. */
303 memset (file_entries
, 0, sizeof (struct cache_file
));
304 memcpy (file_entries
->magic
, CACHEMAGIC
, sizeof CACHEMAGIC
- 1);
306 file_entries
->nlibs
= cache_entry_old_count
;
311 /* And the list of all entries in the new format. */
312 file_entries_new_size
= sizeof (struct cache_file_new
)
313 + cache_entry_count
* sizeof (struct file_entry_new
);
315 (struct cache_file_new
*) xmalloc (file_entries_new_size
);
317 /* Fill in the header. */
318 memset (file_entries_new
, 0, sizeof (struct cache_file_new
));
319 memcpy (file_entries_new
->magic
, CACHEMAGIC_NEW
,
320 sizeof CACHEMAGIC_NEW
- 1);
321 memcpy (file_entries_new
->version
, CACHE_VERSION
,
322 sizeof CACHE_VERSION
- 1);
324 file_entries_new
->nlibs
= cache_entry_count
;
325 file_entries_new
->len_strings
= total_strlen
;
328 pad
= ALIGN_CACHE (file_entries_size
) - file_entries_size
;
330 /* If we have both formats, we hide the new format in the strings
331 table, we have to adjust all string indices for this so that
332 old libc5/glibc 2 dynamic linkers just ignore them. */
334 str_offset
= file_entries_new_size
;
339 for (idx_old
= 0, idx_new
= 0, entry
= entries
; entry
!= NULL
;
340 entry
= entry
->next
, ++idx_new
)
342 /* First the library. */
345 file_entries
->libs
[idx_old
].flags
= entry
->flags
;
346 /* XXX: Actually we can optimize here and remove duplicates. */
347 file_entries
->libs
[idx_old
].key
= str_offset
+ pad
;
351 /* We could subtract file_entries_new_size from str_offset -
352 not doing so makes the code easier, the string table
353 always begins at the beginning of the the new cache
355 file_entries_new
->libs
[idx_new
].flags
= entry
->flags
;
356 file_entries_new
->libs
[idx_new
].osversion
= entry
->osversion
;
357 file_entries_new
->libs
[idx_new
].hwcap
= entry
->hwcap
;
358 file_entries_new
->libs
[idx_new
].key
= str_offset
;
360 len
= strlen (entry
->lib
);
361 str
= stpcpy (str
, entry
->lib
);
362 /* Account the final NUL. */
364 str_offset
+= len
+ 1;
367 file_entries
->libs
[idx_old
].value
= str_offset
+ pad
;
369 file_entries_new
->libs
[idx_new
].value
= str_offset
;
370 len
= strlen (entry
->path
);
371 str
= stpcpy (str
, entry
->path
);
372 /* Account the final NUL. */
374 str_offset
+= len
+ 1;
375 /* Ignore entries with hwcap for old format. */
376 if (entry
->hwcap
== 0)
380 /* Write out the cache. */
382 /* Write cache first to a temporary file and rename it later. */
383 temp_name
= xmalloc (strlen (cache_name
) + 2);
384 sprintf (temp_name
, "%s~", cache_name
);
385 /* First remove an old copy if it exists. */
386 if (unlink (temp_name
) && errno
!= ENOENT
)
387 error (EXIT_FAILURE
, errno
, _("Can't remove old temporary cache file %s"),
391 fd
= open (temp_name
, O_CREAT
|O_WRONLY
|O_TRUNC
|O_NOFOLLOW
,
392 S_IROTH
|S_IRGRP
|S_IRUSR
|S_IWUSR
);
394 error (EXIT_FAILURE
, errno
, _("Can't create temporary cache file %s"),
397 /* Write contents. */
400 if (write (fd
, file_entries
, file_entries_size
)
401 != (ssize_t
)file_entries_size
)
402 error (EXIT_FAILURE
, errno
, _("Writing of cache data failed"));
410 if (write (fd
, zero
, pad
) != (ssize_t
)pad
)
411 error (EXIT_FAILURE
, errno
, _("Writing of cache data failed"));
413 if (write (fd
, file_entries_new
, file_entries_new_size
)
414 != (ssize_t
)file_entries_new_size
)
415 error (EXIT_FAILURE
, errno
, _("Writing of cache data failed"));
418 if (write (fd
, strings
, total_strlen
) != (ssize_t
)total_strlen
)
419 error (EXIT_FAILURE
, errno
, _("Writing of cache data failed."));
423 /* Make sure user can always read cache file */
424 if (chmod (temp_name
, S_IROTH
|S_IRGRP
|S_IRUSR
|S_IWUSR
))
425 error (EXIT_FAILURE
, errno
,
426 _("Changing access rights of %s to %#o failed"), temp_name
,
427 S_IROTH
|S_IRGRP
|S_IRUSR
|S_IWUSR
);
429 /* Move temporary to its final location. */
430 if (rename (temp_name
, cache_name
))
431 error (EXIT_FAILURE
, errno
, _("Renaming of %s to %s failed"), temp_name
,
434 /* Free all allocated memory. */
443 entries
= entries
->next
;
449 /* Add one library to the cache. */
451 add_to_cache (const char *path
, const char *lib
, int flags
,
452 unsigned int osversion
, uint64_t hwcap
)
454 struct cache_entry
*new_entry
, *ptr
, *prev
;
458 new_entry
= (struct cache_entry
*) xmalloc (sizeof (struct cache_entry
));
460 len
= strlen (lib
) + strlen (path
) + 2;
462 full_path
= (char *) xmalloc (len
);
463 snprintf (full_path
, len
, "%s/%s", path
, lib
);
465 new_entry
->lib
= xstrdup (lib
);
466 new_entry
->path
= full_path
;
467 new_entry
->flags
= flags
;
468 new_entry
->osversion
= osversion
;
469 new_entry
->hwcap
= hwcap
;
470 new_entry
->bits_hwcap
= 0;
472 /* Count the number of bits set in the masked value. */
473 for (i
= 0; (~((1ULL << i
) - 1) & hwcap
) != 0; ++i
)
474 if ((hwcap
& (1ULL << i
)) != 0)
475 ++new_entry
->bits_hwcap
;
478 /* Keep the list sorted - search for right place to insert. */
483 if (compare (ptr
, new_entry
) > 0)
488 /* Is this the first entry? */
491 new_entry
->next
= entries
;
496 new_entry
->next
= prev
->next
;
497 prev
->next
= new_entry
;