1 /* Copyright (C) 1999-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published
6 by the Free Software Foundation; version 2 of the License, or
7 (at your option) any later version.
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, see <https://www.gnu.org/licenses/>. */
29 #include <sys/fcntl.h>
31 #include <sys/param.h>
33 #include <sys/types.h>
38 #include <stringtable.h>
40 /* Used to store library names, paths, and other strings. */
41 static struct stringtable strings
;
43 /* Keeping track of "glibc-hwcaps" subdirectories. During cache
44 construction, a linear search by name is performed to deduplicate
46 struct glibc_hwcaps_subdirectory
48 struct glibc_hwcaps_subdirectory
*next
;
50 /* Interned string with the subdirectory name. */
51 struct stringtable_entry
*name
;
53 /* Array index in the cache_extension_tag_glibc_hwcaps section in
54 the stored cached file. This is computed after all the
55 subdirectories have been processed, so that subdirectory names in
56 the extension section can be sorted. */
57 uint32_t section_index
;
59 /* True if the subdirectory is actually used for anything. */
64 glibc_hwcaps_subdirectory_name (const struct glibc_hwcaps_subdirectory
*dir
)
66 return dir
->name
->string
;
69 /* Linked list of known hwcaps subdirecty names. */
70 static struct glibc_hwcaps_subdirectory
*hwcaps
;
72 struct glibc_hwcaps_subdirectory
*
73 new_glibc_hwcaps_subdirectory (const char *name
)
75 struct stringtable_entry
*name_interned
= stringtable_add (&strings
, name
);
76 for (struct glibc_hwcaps_subdirectory
*p
= hwcaps
; p
!= NULL
; p
= p
->next
)
77 if (p
->name
== name_interned
)
79 struct glibc_hwcaps_subdirectory
*p
= xmalloc (sizeof (*p
));
81 p
->name
= name_interned
;
88 /* Helper for sorting struct glibc_hwcaps_subdirectory elements by
91 assign_glibc_hwcaps_indices_compare (const void *l
, const void *r
)
93 const struct glibc_hwcaps_subdirectory
*left
94 = *(struct glibc_hwcaps_subdirectory
**)l
;
95 const struct glibc_hwcaps_subdirectory
*right
96 = *(struct glibc_hwcaps_subdirectory
**)r
;
97 return strcmp (glibc_hwcaps_subdirectory_name (left
),
98 glibc_hwcaps_subdirectory_name (right
));
101 /* Count the number of hwcaps subdirectories which are actually
104 glibc_hwcaps_count (void)
107 for (struct glibc_hwcaps_subdirectory
*p
= hwcaps
; p
!= NULL
; p
= p
->next
)
113 /* Compute the section_index fields for all */
115 assign_glibc_hwcaps_indices (void)
117 /* Convert the linked list into an array, so that we can use qsort.
118 Only copy the subdirectories which are actually used. */
119 size_t count
= glibc_hwcaps_count ();
120 struct glibc_hwcaps_subdirectory
**array
121 = xmalloc (sizeof (*array
) * count
);
124 for (struct glibc_hwcaps_subdirectory
*p
= hwcaps
; p
!= NULL
; p
= p
->next
)
133 qsort (array
, count
, sizeof (*array
), assign_glibc_hwcaps_indices_compare
);
135 /* Assign the array indices. */
136 for (size_t i
= 0; i
< count
; ++i
)
137 array
[i
]->section_index
= i
;
144 struct stringtable_entry
*lib
; /* Library name. */
145 struct stringtable_entry
*path
; /* Path to find library. */
146 int flags
; /* Flags to indicate kind of library. */
147 unsigned int isa_level
; /* Required ISA level. */
148 uint64_t hwcap
; /* Important hardware capabilities. */
149 int bits_hwcap
; /* Number of bits set in hwcap. */
151 /* glibc-hwcaps subdirectory. If not NULL, hwcap must be zero. */
152 struct glibc_hwcaps_subdirectory
*hwcaps
;
154 struct cache_entry
*next
; /* Next entry in list. */
157 /* List of all cache entries. */
158 static struct cache_entry
*entries
;
160 /* libc4, ELF and libc5 are unsupported. */
161 static const char *flag_descr
[] =
162 { "libc4", "ELF", "libc5", "libc6"};
164 /* Print a single entry. */
166 print_entry (const char *lib
, int flag
, uint64_t hwcap
,
167 const char *hwcap_string
, const char *key
)
169 printf ("\t%s (", lib
);
170 switch (flag
& FLAG_TYPE_MASK
)
173 fputs (flag_descr
[flag
& FLAG_TYPE_MASK
], stdout
);
176 fputs (_("unknown or unsupported flag"), stdout
);
179 switch (flag
& FLAG_REQUIRED_MASK
)
181 case FLAG_SPARC_LIB64
:
182 fputs (",64bit", stdout
);
184 case FLAG_IA64_LIB64
:
185 fputs (",IA-64", stdout
);
187 case FLAG_X8664_LIB64
:
188 fputs (",x86-64", stdout
);
190 case FLAG_S390_LIB64
:
191 fputs (",64bit", stdout
);
193 case FLAG_POWERPC_LIB64
:
194 fputs (",64bit", stdout
);
196 case FLAG_MIPS64_LIBN32
:
197 fputs (",N32", stdout
);
199 case FLAG_MIPS64_LIBN64
:
200 fputs (",64bit", stdout
);
202 case FLAG_X8664_LIBX32
:
203 fputs (",x32", stdout
);
206 fputs (",hard-float", stdout
);
208 case FLAG_AARCH64_LIB64
:
209 fputs (",AArch64", stdout
);
211 /* Uses the ARM soft-float ABI. */
213 fputs (",soft-float", stdout
);
215 case FLAG_MIPS_LIB32_NAN2008
:
216 fputs (",nan2008", stdout
);
218 case FLAG_MIPS64_LIBN32_NAN2008
:
219 fputs (",N32,nan2008", stdout
);
221 case FLAG_MIPS64_LIBN64_NAN2008
:
222 fputs (",64bit,nan2008", stdout
);
224 case FLAG_RISCV_FLOAT_ABI_SOFT
:
225 fputs (",soft-float", stdout
);
227 case FLAG_RISCV_FLOAT_ABI_DOUBLE
:
228 fputs (",double-float", stdout
);
233 printf (",%d", flag
& FLAG_REQUIRED_MASK
);
236 if (hwcap_string
!= NULL
)
237 printf (", hwcap: \"%s\"", hwcap_string
);
239 printf (", hwcap: %#.16" PRIx64
, hwcap
);
240 printf (") => %s\n", key
);
243 /* Returns the string with the name of the glibcs-hwcaps subdirectory
244 associated with ENTRY->hwcap. file_base must be the base address
245 for string table indices. */
247 glibc_hwcaps_string (struct cache_extension_all_loaded
*ext
,
248 const void *file_base
, size_t file_size
,
249 struct file_entry_new
*entry
)
251 const uint32_t *hwcaps_array
252 = ext
->sections
[cache_extension_tag_glibc_hwcaps
].base
;
253 if (dl_cache_hwcap_extension (entry
) && hwcaps_array
!= NULL
)
255 uint32_t index
= (uint32_t) entry
->hwcap
;
256 if (index
< ext
->sections
[cache_extension_tag_glibc_hwcaps
].size
/ 4)
258 uint32_t string_table_index
= hwcaps_array
[index
];
259 if (string_table_index
< file_size
)
260 return file_base
+ string_table_index
;
266 /* Print an error and exit if the new-file cache is internally
269 check_new_cache (struct cache_file_new
*cache
)
271 if (! cache_file_new_matches_endian (cache
))
272 error (EXIT_FAILURE
, 0, _("Cache file has wrong endianness.\n"));
275 /* Print the extension information in *EXT. */
277 print_extensions (struct cache_extension_all_loaded
*ext
)
279 if (ext
->sections
[cache_extension_tag_generator
].base
!= NULL
)
281 fputs (_("Cache generated by: "), stdout
);
282 fwrite (ext
->sections
[cache_extension_tag_generator
].base
, 1,
283 ext
->sections
[cache_extension_tag_generator
].size
, stdout
);
288 /* Print the whole cache file, if a file contains the new cache format
289 hidden in the old one, print the contents of the new format. */
291 print_cache (const char *cache_name
)
293 int fd
= open (cache_name
, O_RDONLY
);
295 error (EXIT_FAILURE
, errno
, _("Can't open cache file %s\n"), cache_name
);
298 if (fstat (fd
, &st
) < 0
299 /* No need to map the file if it is empty. */
306 struct cache_file
*cache
307 = mmap (NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
308 if (cache
== MAP_FAILED
)
309 error (EXIT_FAILURE
, errno
, _("mmap of cache file failed.\n"));
311 size_t cache_size
= st
.st_size
;
312 if (cache_size
< sizeof (struct cache_file
))
313 error (EXIT_FAILURE
, 0, _("File is not a cache file.\n"));
315 struct cache_file_new
*cache_new
= NULL
;
316 const char *cache_data
;
319 if (memcmp (cache
->magic
, CACHEMAGIC
, sizeof CACHEMAGIC
- 1))
321 /* This can only be the new format without the old one. */
322 cache_new
= (struct cache_file_new
*) cache
;
324 if (memcmp (cache_new
->magic
, CACHEMAGIC_NEW
, sizeof CACHEMAGIC_NEW
- 1)
325 || memcmp (cache_new
->version
, CACHE_VERSION
,
326 sizeof CACHE_VERSION
- 1))
327 error (EXIT_FAILURE
, 0, _("File is not a cache file.\n"));
328 check_new_cache (cache_new
);
330 /* This is where the strings start. */
331 cache_data
= (const char *) cache_new
;
335 /* Check for corruption, avoiding overflow. */
336 if ((cache_size
- sizeof (struct cache_file
)) / sizeof (struct file_entry
)
338 error (EXIT_FAILURE
, 0, _("File is not a cache file.\n"));
340 size_t offset
= ALIGN_CACHE (sizeof (struct cache_file
)
342 * sizeof (struct file_entry
)));
343 /* This is where the strings start. */
344 cache_data
= (const char *) &cache
->libs
[cache
->nlibs
];
346 /* Check for a new cache embedded in the old format. */
348 > (offset
+ sizeof (struct cache_file_new
)))
351 cache_new
= (struct cache_file_new
*) ((void *)cache
+ offset
);
353 if (memcmp (cache_new
->magic
, CACHEMAGIC_NEW
,
354 sizeof CACHEMAGIC_NEW
- 1) == 0
355 && memcmp (cache_new
->version
, CACHE_VERSION
,
356 sizeof CACHE_VERSION
- 1) == 0)
358 check_new_cache (cache_new
);
359 cache_data
= (const char *) cache_new
;
367 printf (_("%d libs found in cache `%s'\n"), cache
->nlibs
, cache_name
);
369 /* Print everything. */
370 for (unsigned int i
= 0; i
< cache
->nlibs
; i
++)
371 print_entry (cache_data
+ cache
->libs
[i
].key
,
372 cache
->libs
[i
].flags
, 0, NULL
,
373 cache_data
+ cache
->libs
[i
].value
);
375 else if (format
== 1)
377 struct cache_extension_all_loaded ext
;
378 if (!cache_extension_load (cache_new
, cache
, cache_size
, &ext
))
379 error (EXIT_FAILURE
, 0,
380 _("Malformed extension data in cache file %s\n"), cache_name
);
382 printf (_("%d libs found in cache `%s'\n"),
383 cache_new
->nlibs
, cache_name
);
385 /* Print everything. */
386 for (unsigned int i
= 0; i
< cache_new
->nlibs
; i
++)
388 const char *hwcaps_string
389 = glibc_hwcaps_string (&ext
, cache
, cache_size
,
390 &cache_new
->libs
[i
]);
391 print_entry (cache_data
+ cache_new
->libs
[i
].key
,
392 cache_new
->libs
[i
].flags
,
393 cache_new
->libs
[i
].hwcap
, hwcaps_string
,
394 cache_data
+ cache_new
->libs
[i
].value
);
396 print_extensions (&ext
);
399 munmap (cache
, cache_size
);
403 /* Initialize cache data structures. */
411 compare (const struct cache_entry
*e1
, const struct cache_entry
*e2
)
413 /* We need to swap entries here to get the correct sort order. */
414 int res
= _dl_cache_libcmp (e2
->lib
->string
, e1
->lib
->string
);
417 if (e1
->flags
< e2
->flags
)
419 else if (e1
->flags
> e2
->flags
)
421 /* Keep the glibc-hwcaps extension entries before the regular
422 entries, and sort them by their names. search_cache in
423 dl-cache.c stops searching once the first non-extension entry
424 is found, so the extension entries need to come first. */
425 else if (e1
->hwcaps
!= NULL
&& e2
->hwcaps
== NULL
)
427 else if (e1
->hwcaps
== NULL
&& e2
->hwcaps
!= NULL
)
429 else if (e1
->hwcaps
!= NULL
&& e2
->hwcaps
!= NULL
)
431 res
= strcmp (glibc_hwcaps_subdirectory_name (e1
->hwcaps
),
432 glibc_hwcaps_subdirectory_name (e2
->hwcaps
));
436 /* Sort by most specific hwcap. */
437 if (e2
->bits_hwcap
> e1
->bits_hwcap
)
439 else if (e2
->bits_hwcap
< e1
->bits_hwcap
)
441 else if (e2
->hwcap
> e1
->hwcap
)
443 else if (e2
->hwcap
< e1
->hwcap
)
449 /* Size of the cache extension directory. All tags are assumed to be
453 cache_extension_size
= (offsetof (struct cache_extension
, sections
)
454 + (cache_extension_count
455 * sizeof (struct cache_extension_section
)))
458 /* Write the cache extensions to FD. The string table is shifted by
459 STRING_TABLE_OFFSET. The extension directory is assumed to be
460 located at CACHE_EXTENSION_OFFSET. assign_glibc_hwcaps_indices
461 must have been called. */
463 write_extensions (int fd
, uint32_t str_offset
,
464 uint32_t cache_extension_offset
)
466 assert ((cache_extension_offset
% 4) == 0);
468 /* The length and contents of the glibc-hwcaps section. */
469 uint32_t hwcaps_count
= glibc_hwcaps_count ();
470 uint32_t hwcaps_offset
= cache_extension_offset
+ cache_extension_size
;
471 uint32_t hwcaps_size
= hwcaps_count
* sizeof (uint32_t);
472 uint32_t *hwcaps_array
= xmalloc (hwcaps_size
);
473 for (struct glibc_hwcaps_subdirectory
*p
= hwcaps
; p
!= NULL
; p
= p
->next
)
475 hwcaps_array
[p
->section_index
] = str_offset
+ p
->name
->offset
;
477 /* This is the offset of the generator string. */
478 uint32_t generator_offset
= hwcaps_offset
;
479 if (hwcaps_count
== 0)
480 /* There is no section for the hwcaps subdirectories. */
481 generator_offset
-= sizeof (struct cache_extension_section
);
483 /* The string table indices for the hwcaps subdirectories shift
484 the generator string backwards. */
485 generator_offset
+= hwcaps_size
;
487 struct cache_extension
*ext
= xmalloc (cache_extension_size
);
488 ext
->magic
= cache_extension_magic
;
490 /* Extension index current being filled. */
493 const char *generator
494 = "ldconfig " PKGVERSION RELEASE
" release version " VERSION
;
495 ext
->sections
[xid
].tag
= cache_extension_tag_generator
;
496 ext
->sections
[xid
].flags
= 0;
497 ext
->sections
[xid
].offset
= generator_offset
;
498 ext
->sections
[xid
].size
= strlen (generator
);
500 if (hwcaps_count
> 0)
503 ext
->sections
[xid
].tag
= cache_extension_tag_glibc_hwcaps
;
504 ext
->sections
[xid
].flags
= 0;
505 ext
->sections
[xid
].offset
= hwcaps_offset
;
506 ext
->sections
[xid
].size
= hwcaps_size
;
511 assert (xid
<= cache_extension_count
);
513 size_t ext_size
= (offsetof (struct cache_extension
, sections
)
514 + xid
* sizeof (struct cache_extension_section
));
515 if (write (fd
, ext
, ext_size
) != ext_size
516 || write (fd
, hwcaps_array
, hwcaps_size
) != hwcaps_size
517 || write (fd
, generator
, strlen (generator
)) != strlen (generator
))
518 error (EXIT_FAILURE
, errno
, _("Writing of cache extension data failed"));
524 /* Compute the hwcap value from ENTRY. */
525 static inline uint64_t
526 compute_hwcap_value (struct cache_entry
*entry
)
528 if (entry
->isa_level
> DL_CACHE_HWCAP_ISA_LEVEL_MASK
)
529 error (EXIT_FAILURE
, 0, _("%s: ISA level is too high (%d > %d)"),
530 entry
->path
->string
, entry
->isa_level
,
531 DL_CACHE_HWCAP_ISA_LEVEL_MASK
);
532 return (DL_CACHE_HWCAP_EXTENSION
533 | (((uint64_t) entry
->isa_level
) << 32)
534 | entry
->hwcaps
->section_index
);
537 /* Save the contents of the cache. */
539 save_cache (const char *cache_name
)
541 /* The cache entries are sorted already, save them in this order. */
543 assign_glibc_hwcaps_indices ();
545 struct cache_entry
*entry
;
546 /* Number of cache entries. */
547 int cache_entry_count
= 0;
548 /* The old format doesn't contain hwcap entries and doesn't contain
549 libraries in subdirectories with hwcaps entries. Count therefore
550 also all entries with hwcap == 0. */
551 int cache_entry_old_count
= 0;
553 for (entry
= entries
; entry
!= NULL
; entry
= entry
->next
)
556 if (entry
->hwcap
== 0)
557 ++cache_entry_old_count
;
560 struct stringtable_finalized strings_finalized
;
561 stringtable_finalize (&strings
, &strings_finalized
);
563 /* Create the on disk cache structure. */
564 struct cache_file
*file_entries
= NULL
;
565 size_t file_entries_size
= 0;
567 if (opt_format
!= opt_format_new
)
569 /* struct cache_file_new is 64-bit aligned on some arches while
570 only 32-bit aligned on other arches. Duplicate last old
571 cache entry so that new cache in ld.so.cache can be used by
573 if (opt_format
!= opt_format_old
)
574 cache_entry_old_count
= (cache_entry_old_count
+ 1) & ~1;
576 /* And the list of all entries in the old format. */
577 file_entries_size
= sizeof (struct cache_file
)
578 + cache_entry_old_count
* sizeof (struct file_entry
);
579 file_entries
= xmalloc (file_entries_size
);
581 /* Fill in the header. */
582 memset (file_entries
, '\0', sizeof (struct cache_file
));
583 memcpy (file_entries
->magic
, CACHEMAGIC
, sizeof CACHEMAGIC
- 1);
585 file_entries
->nlibs
= cache_entry_old_count
;
588 struct cache_file_new
*file_entries_new
= NULL
;
589 size_t file_entries_new_size
= 0;
591 if (opt_format
!= opt_format_old
)
593 /* And the list of all entries in the new format. */
594 file_entries_new_size
= sizeof (struct cache_file_new
)
595 + cache_entry_count
* sizeof (struct file_entry_new
);
596 file_entries_new
= xmalloc (file_entries_new_size
);
598 /* Fill in the header. */
599 memset (file_entries_new
, '\0', sizeof (struct cache_file_new
));
600 memcpy (file_entries_new
->magic
, CACHEMAGIC_NEW
,
601 sizeof CACHEMAGIC_NEW
- 1);
602 memcpy (file_entries_new
->version
, CACHE_VERSION
,
603 sizeof CACHE_VERSION
- 1);
605 file_entries_new
->nlibs
= cache_entry_count
;
606 file_entries_new
->len_strings
= strings_finalized
.size
;
607 file_entries_new
->flags
= cache_file_new_flags_endian_current
;
610 /* Pad for alignment of cache_file_new. */
611 size_t pad
= ALIGN_CACHE (file_entries_size
) - file_entries_size
;
613 /* If we have both formats, we hide the new format in the strings
614 table, we have to adjust all string indices for this so that
615 old libc5/glibc 2 dynamic linkers just ignore them. */
616 unsigned int str_offset
;
617 if (opt_format
!= opt_format_old
)
618 str_offset
= file_entries_new_size
;
622 /* An array for all strings. */
626 for (idx_old
= 0, idx_new
= 0, entry
= entries
; entry
!= NULL
;
627 entry
= entry
->next
, ++idx_new
)
629 if (opt_format
!= opt_format_new
&& entry
->hwcap
== 0)
631 file_entries
->libs
[idx_old
].flags
= entry
->flags
;
632 /* XXX: Actually we can optimize here and remove duplicates. */
633 file_entries
->libs
[idx_old
].key
= str_offset
+ pad
;
634 file_entries
->libs
[idx_new
].key
= str_offset
+ entry
->lib
->offset
;
635 file_entries
->libs
[idx_new
].value
636 = str_offset
+ entry
->path
->offset
;
638 if (opt_format
!= opt_format_old
)
640 /* We could subtract file_entries_new_size from str_offset -
641 not doing so makes the code easier, the string table
642 always begins at the beginning of the new cache
644 file_entries_new
->libs
[idx_new
].flags
= entry
->flags
;
645 file_entries_new
->libs
[idx_new
].osversion_unused
= 0;
646 if (entry
->hwcaps
== NULL
)
647 file_entries_new
->libs
[idx_new
].hwcap
= entry
->hwcap
;
649 file_entries_new
->libs
[idx_new
].hwcap
650 = compute_hwcap_value (entry
);
651 file_entries_new
->libs
[idx_new
].key
652 = str_offset
+ entry
->lib
->offset
;
653 file_entries_new
->libs
[idx_new
].value
654 = str_offset
+ entry
->path
->offset
;
657 /* Ignore entries with hwcap for old format. */
658 if (entry
->hwcap
== 0)
662 /* Duplicate last old cache entry if needed. */
663 if (opt_format
!= opt_format_new
664 && idx_old
< cache_entry_old_count
)
665 file_entries
->libs
[idx_old
] = file_entries
->libs
[idx_old
- 1];
667 /* Compute the location of the extension directory. This
668 implementation puts the directory after the string table. The
669 size computation matches the write calls below. The extension
670 directory does not exist with format 0, so the value does not
672 uint32_t extension_offset
= 0;
673 if (opt_format
!= opt_format_new
)
674 extension_offset
+= file_entries_size
;
675 if (opt_format
!= opt_format_old
)
677 if (opt_format
!= opt_format_new
)
678 extension_offset
+= pad
;
679 extension_offset
+= file_entries_new_size
;
681 extension_offset
+= strings_finalized
.size
;
682 extension_offset
= roundup (extension_offset
, 4); /* Provide alignment. */
683 if (opt_format
!= opt_format_old
)
684 file_entries_new
->extension_offset
= extension_offset
;
686 /* Write out the cache. */
688 /* Write cache first to a temporary file and rename it later. */
689 char *temp_name
= xmalloc (strlen (cache_name
) + 2);
690 sprintf (temp_name
, "%s~", cache_name
);
693 int fd
= open (temp_name
, O_CREAT
|O_WRONLY
|O_TRUNC
|O_NOFOLLOW
,
696 error (EXIT_FAILURE
, errno
, _("Can't create temporary cache file %s"),
699 /* Write contents. */
700 if (opt_format
!= opt_format_new
)
702 if (write (fd
, file_entries
, file_entries_size
)
703 != (ssize_t
) file_entries_size
)
704 error (EXIT_FAILURE
, errno
, _("Writing of cache data failed"));
706 if (opt_format
!= opt_format_old
)
709 if (opt_format
!= opt_format_new
)
712 memset (zero
, '\0', pad
);
713 if (write (fd
, zero
, pad
) != (ssize_t
) pad
)
714 error (EXIT_FAILURE
, errno
, _("Writing of cache data failed"));
716 if (write (fd
, file_entries_new
, file_entries_new_size
)
717 != (ssize_t
) file_entries_new_size
)
718 error (EXIT_FAILURE
, errno
, _("Writing of cache data failed"));
721 if (write (fd
, strings_finalized
.strings
, strings_finalized
.size
)
722 != (ssize_t
) strings_finalized
.size
)
723 error (EXIT_FAILURE
, errno
, _("Writing of cache data failed"));
725 if (opt_format
!= opt_format_old
)
727 /* Align file position to 4. */
728 __attribute__ ((unused
)) off64_t old_offset
729 = lseek64 (fd
, extension_offset
, SEEK_SET
);
730 assert ((unsigned long long int) (extension_offset
- old_offset
) < 4);
731 write_extensions (fd
, str_offset
, extension_offset
);
734 /* Make sure user can always read cache file */
735 if (chmod (temp_name
, S_IROTH
|S_IRGRP
|S_IRUSR
|S_IWUSR
))
736 error (EXIT_FAILURE
, errno
,
737 _("Changing access rights of %s to %#o failed"), temp_name
,
738 S_IROTH
|S_IRGRP
|S_IRUSR
|S_IWUSR
);
740 /* Make sure that data is written to disk. */
741 if (fsync (fd
) != 0 || close (fd
) != 0)
742 error (EXIT_FAILURE
, errno
, _("Writing of cache data failed"));
744 /* Move temporary to its final location. */
745 if (rename (temp_name
, cache_name
))
746 error (EXIT_FAILURE
, errno
, _("Renaming of %s to %s failed"), temp_name
,
749 /* Free all allocated memory. */
750 free (file_entries_new
);
752 free (strings_finalized
.strings
);
758 entries
= entries
->next
;
764 /* Add one library to the cache. */
766 add_to_cache (const char *path
, const char *filename
, const char *soname
,
767 int flags
, unsigned int isa_level
, uint64_t hwcap
,
768 struct glibc_hwcaps_subdirectory
*hwcaps
)
770 struct cache_entry
*new_entry
= xmalloc (sizeof (*new_entry
));
772 struct stringtable_entry
*path_interned
;
775 if (asprintf (&p
, "%s/%s", path
, filename
) < 0)
776 error (EXIT_FAILURE
, errno
, _("Could not create library path"));
777 path_interned
= stringtable_add (&strings
, p
);
781 new_entry
->lib
= stringtable_add (&strings
, soname
);
782 new_entry
->path
= path_interned
;
783 new_entry
->flags
= flags
;
784 new_entry
->isa_level
= isa_level
;
785 new_entry
->hwcap
= hwcap
;
786 new_entry
->hwcaps
= hwcaps
;
787 new_entry
->bits_hwcap
= 0;
795 /* Count the number of bits set in the masked value. */
797 (~((1ULL << i
) - 1) & hwcap
) != 0 && i
< 8 * sizeof (hwcap
); ++i
)
798 if ((hwcap
& (1ULL << i
)) != 0)
799 ++new_entry
->bits_hwcap
;
802 /* Keep the list sorted - search for right place to insert. */
803 struct cache_entry
*ptr
= entries
;
804 struct cache_entry
*prev
= entries
;
807 if (compare (ptr
, new_entry
) > 0)
812 /* Is this the first entry? */
815 new_entry
->next
= entries
;
820 new_entry
->next
= prev
->next
;
821 prev
->next
= new_entry
;
826 /* Auxiliary cache. */
828 struct aux_cache_entry_id
836 struct aux_cache_entry
838 struct aux_cache_entry_id id
;
840 unsigned int isa_level
;
843 struct aux_cache_entry
*next
;
846 #define AUX_CACHEMAGIC "glibc-ld.so.auxcache-1.0"
848 struct aux_cache_file_entry
850 struct aux_cache_entry_id id
; /* Unique id of entry. */
851 int32_t flags
; /* This is 1 for an ELF library. */
852 uint32_t soname
; /* String table indice. */
853 uint32_t isa_level
; /* Required ISA level. */
856 /* ldconfig maintains an auxiliary cache file that allows
857 only reading those libraries that have changed since the last iteration.
858 For this for each library some information is cached in the auxiliary
860 struct aux_cache_file
862 char magic
[sizeof AUX_CACHEMAGIC
- 1];
863 uint32_t nlibs
; /* Number of entries. */
864 uint32_t len_strings
; /* Size of string table. */
865 struct aux_cache_file_entry libs
[0]; /* Entries describing libraries. */
866 /* After this the string table of size len_strings is found. */
869 static const unsigned int primes
[] =
871 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071, 262139,
872 524287, 1048573, 2097143, 4194301, 8388593, 16777213, 33554393,
873 67108859, 134217689, 268435399, 536870909, 1073741789, 2147483647
876 static size_t aux_hash_size
;
877 static struct aux_cache_entry
**aux_hash
;
879 /* Simplistic hash function for aux_cache_entry_id. */
881 aux_cache_entry_id_hash (struct aux_cache_entry_id
*id
)
883 uint64_t ret
= ((id
->ino
* 11 + id
->ctime
) * 11 + id
->size
) * 11 + id
->dev
;
884 return ret
^ (ret
>> 32);
887 static size_t nextprime (size_t x
)
889 for (unsigned int i
= 0; i
< sizeof (primes
) / sizeof (primes
[0]); ++i
)
896 init_aux_cache (void)
898 aux_hash_size
= primes
[3];
899 aux_hash
= xcalloc (aux_hash_size
, sizeof (struct aux_cache_entry
*));
903 search_aux_cache (struct stat
*stat_buf
, int *flags
, unsigned int *isa_level
,
906 struct aux_cache_entry_id id
;
907 id
.ino
= (uint64_t) stat_buf
->st_ino
;
908 id
.ctime
= (uint64_t) stat_buf
->st_ctime
;
909 id
.size
= (uint64_t) stat_buf
->st_size
;
910 id
.dev
= (uint64_t) stat_buf
->st_dev
;
912 unsigned int hash
= aux_cache_entry_id_hash (&id
);
913 struct aux_cache_entry
*entry
;
914 for (entry
= aux_hash
[hash
% aux_hash_size
]; entry
; entry
= entry
->next
)
915 if (id
.ino
== entry
->id
.ino
916 && id
.ctime
== entry
->id
.ctime
917 && id
.size
== entry
->id
.size
918 && id
.dev
== entry
->id
.dev
)
920 *flags
= entry
->flags
;
921 *isa_level
= entry
->isa_level
;
922 if (entry
->soname
!= NULL
)
923 *soname
= xstrdup (entry
->soname
);
934 insert_to_aux_cache (struct aux_cache_entry_id
*id
, int flags
,
935 unsigned int isa_level
, const char *soname
, int used
)
937 size_t hash
= aux_cache_entry_id_hash (id
) % aux_hash_size
;
938 struct aux_cache_entry
*entry
;
939 for (entry
= aux_hash
[hash
]; entry
; entry
= entry
->next
)
940 if (id
->ino
== entry
->id
.ino
941 && id
->ctime
== entry
->id
.ctime
942 && id
->size
== entry
->id
.size
943 && id
->dev
== entry
->id
.dev
)
946 size_t len
= soname
? strlen (soname
) + 1 : 0;
947 entry
= xmalloc (sizeof (struct aux_cache_entry
) + len
);
949 entry
->flags
= flags
;
950 entry
->isa_level
= isa_level
;
953 entry
->soname
= memcpy ((char *) (entry
+ 1), soname
, len
);
955 entry
->soname
= NULL
;
956 entry
->next
= aux_hash
[hash
];
957 aux_hash
[hash
] = entry
;
961 add_to_aux_cache (struct stat
*stat_buf
, int flags
, unsigned int isa_level
,
964 struct aux_cache_entry_id id
;
965 id
.ino
= (uint64_t) stat_buf
->st_ino
;
966 id
.ctime
= (uint64_t) stat_buf
->st_ctime
;
967 id
.size
= (uint64_t) stat_buf
->st_size
;
968 id
.dev
= (uint64_t) stat_buf
->st_dev
;
969 insert_to_aux_cache (&id
, flags
, isa_level
, soname
, 1);
972 /* Load auxiliary cache to search for unchanged entries. */
974 load_aux_cache (const char *aux_cache_name
)
976 int fd
= open (aux_cache_name
, O_RDONLY
);
984 if (fstat (fd
, &st
) < 0 || st
.st_size
< sizeof (struct aux_cache_file
))
991 size_t aux_cache_size
= st
.st_size
;
992 struct aux_cache_file
*aux_cache
993 = mmap (NULL
, aux_cache_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
994 if (aux_cache
== MAP_FAILED
995 || aux_cache_size
< sizeof (struct aux_cache_file
)
996 || memcmp (aux_cache
->magic
, AUX_CACHEMAGIC
, sizeof AUX_CACHEMAGIC
- 1)
997 || aux_cache_size
!= (sizeof (struct aux_cache_file
)
998 + aux_cache
->nlibs
* sizeof (struct aux_cache_file_entry
)
999 + aux_cache
->len_strings
))
1001 if (aux_cache
!= MAP_FAILED
)
1002 munmap (aux_cache
, aux_cache_size
);
1009 aux_hash_size
= nextprime (aux_cache
->nlibs
);
1010 aux_hash
= xcalloc (aux_hash_size
, sizeof (struct aux_cache_entry
*));
1012 const char *aux_cache_data
1013 = (const char *) &aux_cache
->libs
[aux_cache
->nlibs
];
1014 for (unsigned int i
= 0; i
< aux_cache
->nlibs
; ++i
)
1015 insert_to_aux_cache (&aux_cache
->libs
[i
].id
,
1016 aux_cache
->libs
[i
].flags
,
1017 aux_cache
->libs
[i
].isa_level
,
1018 aux_cache
->libs
[i
].soname
== 0
1019 ? NULL
: aux_cache_data
+ aux_cache
->libs
[i
].soname
,
1022 munmap (aux_cache
, aux_cache_size
);
1026 /* Save the contents of the auxiliary cache. */
1028 save_aux_cache (const char *aux_cache_name
)
1030 /* Count the length of all sonames. We start with empty string. */
1031 size_t total_strlen
= 1;
1032 /* Number of cache entries. */
1033 int cache_entry_count
= 0;
1035 for (size_t i
= 0; i
< aux_hash_size
; ++i
)
1036 for (struct aux_cache_entry
*entry
= aux_hash
[i
];
1037 entry
!= NULL
; entry
= entry
->next
)
1040 ++cache_entry_count
;
1041 if (entry
->soname
!= NULL
)
1042 total_strlen
+= strlen (entry
->soname
) + 1;
1045 /* Auxiliary cache. */
1046 size_t file_entries_size
1047 = sizeof (struct aux_cache_file
)
1048 + cache_entry_count
* sizeof (struct aux_cache_file_entry
);
1049 struct aux_cache_file
*file_entries
1050 = xmalloc (file_entries_size
+ total_strlen
);
1052 /* Fill in the header of the auxiliary cache. */
1053 memset (file_entries
, '\0', sizeof (struct aux_cache_file
));
1054 memcpy (file_entries
->magic
, AUX_CACHEMAGIC
, sizeof AUX_CACHEMAGIC
- 1);
1056 file_entries
->nlibs
= cache_entry_count
;
1057 file_entries
->len_strings
= total_strlen
;
1059 /* Initial String offset for auxiliary cache is always after the
1060 special empty string. */
1061 unsigned int str_offset
= 1;
1063 /* An array for all strings. */
1064 char *str
= (char *) file_entries
+ file_entries_size
;
1068 for (size_t i
= 0; i
< aux_hash_size
; ++i
)
1069 for (struct aux_cache_entry
*entry
= aux_hash
[i
];
1070 entry
!= NULL
; entry
= entry
->next
)
1073 file_entries
->libs
[idx
].id
= entry
->id
;
1074 file_entries
->libs
[idx
].flags
= entry
->flags
;
1075 if (entry
->soname
== NULL
)
1076 file_entries
->libs
[idx
].soname
= 0;
1079 file_entries
->libs
[idx
].soname
= str_offset
;
1081 size_t len
= strlen (entry
->soname
) + 1;
1082 str
= mempcpy (str
, entry
->soname
, len
);
1085 file_entries
->libs
[idx
++].isa_level
= entry
->isa_level
;
1088 /* Write out auxiliary cache file. */
1089 /* Write auxiliary cache first to a temporary file and rename it later. */
1091 char *temp_name
= xmalloc (strlen (aux_cache_name
) + 2);
1092 sprintf (temp_name
, "%s~", aux_cache_name
);
1094 /* Check that directory exists and create if needed. */
1095 char *dir
= strdupa (aux_cache_name
);
1096 dir
= dirname (dir
);
1099 if (stat (dir
, &st
) < 0)
1101 if (mkdir (dir
, 0700) < 0)
1106 int fd
= open (temp_name
, O_CREAT
|O_WRONLY
|O_TRUNC
|O_NOFOLLOW
,
1111 bool fail
= ((write (fd
, file_entries
, file_entries_size
+ total_strlen
)
1112 != (ssize_t
) (file_entries_size
+ total_strlen
))
1113 || fdatasync (fd
) != 0);
1115 fail
|= close (fd
) != 0;
1123 /* Move temporary to its final location. */
1124 if (rename (temp_name
, aux_cache_name
))
1128 /* Free allocated memory. */
1130 free (file_entries
);