Assume only FLAG_ELF_LIBC6 suport
[glibc.git] / elf / ldconfig.c
blob6f37f38f3533cbd45b1fc2cf65f03b5b01a7bbd3
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/>. */
17 #define PROCINFO_CLASS static
18 #include <assert.h>
19 #include <alloca.h>
20 #include <argp.h>
21 #include <dirent.h>
22 #include <elf.h>
23 #include <error.h>
24 #include <errno.h>
25 #include <inttypes.h>
26 #include <libintl.h>
27 #include <locale.h>
28 #include <stdbool.h>
29 #include <stdio.h>
30 #include <stdio_ext.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <stdint.h>
35 #include <sys/fcntl.h>
36 #include <sys/mman.h>
37 #include <sys/stat.h>
38 #include <sys/types.h>
39 #include <glob.h>
40 #include <libgen.h>
42 #include <ldconfig.h>
43 #include <dl-cache.h>
44 #include <dl-hwcaps.h>
45 #include <dl-is_dso.h>
47 #include <dl-procinfo.h>
49 /* This subpath in search path entries is always supported and
50 included in the cache for backwards compatibility. */
51 #define TLS_SUBPATH "tls"
53 /* The MSB of the hwcap field is set for objects in TLS_SUBPATH
54 directories. There is always TLS support in glibc, so the dynamic
55 loader does not check the bit directly. But more hwcap bits make a
56 an object more preferred, so the bit still has meaning. */
57 #define TLS_HWCAP_BIT 63
59 #ifndef LD_SO_CONF
60 # define LD_SO_CONF SYSCONFDIR "/ld.so.conf"
61 #endif
63 /* Get libc version number. */
64 #include <version.h>
66 #define PACKAGE _libc_intl_domainname
68 /* List of directories to handle. */
69 struct dir_entry
71 char *path;
72 int flag;
73 ino64_t ino;
74 dev_t dev;
75 const char *from_file;
76 int from_line;
78 /* Non-NULL for subdirectories under a glibc-hwcaps subdirectory. */
79 struct glibc_hwcaps_subdirectory *hwcaps;
81 struct dir_entry *next;
84 /* The list is unsorted, contains no duplicates. Entries are added at
85 the end. */
86 static struct dir_entry *dir_entries;
88 /* Flags for different options. */
89 /* Print Cache. */
90 static int opt_print_cache;
92 /* Be verbose. */
93 int opt_verbose;
95 /* Format to support. */
96 enum opt_format opt_format = opt_format_new;
98 /* Build cache. */
99 static int opt_build_cache = 1;
101 /* Enable symbolic link processing. If set, create or update symbolic
102 links, and remove stale symbolic links. */
103 static int opt_link = 1;
105 /* Only process directories specified on the command line. */
106 static int opt_only_cline;
108 /* Path to root for chroot. */
109 static char *opt_chroot;
111 /* Manually link given shared libraries. */
112 static int opt_manual_link;
114 /* Should we ignore an old auxiliary cache file? */
115 static int opt_ignore_aux_cache;
117 /* Cache file to use. */
118 static char *cache_file;
120 /* Configuration file. */
121 static const char *config_file;
123 /* Mask to use for important hardware capabilities. */
124 static unsigned long int hwcap_mask = HWCAP_IMPORTANT;
126 /* Name and version of program. */
127 static void print_version (FILE *stream, struct argp_state *state);
128 void (*argp_program_version_hook) (FILE *, struct argp_state *)
129 = print_version;
131 /* Function to print some extra text in the help message. */
132 static char *more_help (int key, const char *text, void *input);
134 /* Definitions of arguments for argp functions. */
135 static const struct argp_option options[] =
137 { "print-cache", 'p', NULL, 0, N_("Print cache"), 0},
138 { "verbose", 'v', NULL, 0, N_("Generate verbose messages"), 0},
139 { NULL, 'N', NULL, 0, N_("Don't build cache"), 0},
140 { NULL, 'X', NULL, 0, N_("Don't update symbolic links"), 0},
141 { NULL, 'r', N_("ROOT"), 0, N_("Change to and use ROOT as root directory"), 0},
142 { NULL, 'C', N_("CACHE"), 0, N_("Use CACHE as cache file"), 0},
143 { NULL, 'f', N_("CONF"), 0, N_("Use CONF as configuration file"), 0},
144 { NULL, 'n', NULL, 0, N_("Only process directories specified on the command line. Don't build cache."), 0},
145 { NULL, 'l', NULL, 0, N_("Manually link individual libraries."), 0},
146 { "format", 'c', N_("FORMAT"), 0, N_("Format to use: new (default), old, or compat"), 0},
147 { "ignore-aux-cache", 'i', NULL, 0, N_("Ignore auxiliary cache file"), 0},
148 { NULL, 0, NULL, 0, NULL, 0 }
151 #define PROCINFO_CLASS static
152 #include <dl-procinfo.c>
154 /* Short description of program. */
155 static const char doc[] = N_("Configure Dynamic Linker Run Time Bindings.");
157 /* Prototype for option handler. */
158 static error_t parse_opt (int key, char *arg, struct argp_state *state);
160 /* Data structure to communicate with argp functions. */
161 static struct argp argp =
163 options, parse_opt, NULL, doc, NULL, more_help, NULL
166 /* Check if string corresponds to an important hardware capability or
167 a platform. */
168 static int
169 is_hwcap_platform (const char *name)
171 int hwcap_idx = _dl_string_hwcap (name);
173 /* Is this a normal hwcap for the machine like "fpu?" */
174 if (hwcap_idx != -1 && ((1 << hwcap_idx) & hwcap_mask))
175 return 1;
177 /* Is this a platform pseudo-hwcap like "i686?" */
178 hwcap_idx = _dl_string_platform (name);
179 if (hwcap_idx != -1)
180 return 1;
182 /* Backwards-compatibility for the "tls" subdirectory. */
183 if (strcmp (name, TLS_SUBPATH) == 0)
184 return 1;
186 return 0;
189 /* Get hwcap (including platform) encoding of path. */
190 static uint64_t
191 path_hwcap (const char *path)
193 char *str = xstrdup (path);
194 char *ptr;
195 uint64_t hwcap = 0;
196 uint64_t h;
198 size_t len;
200 len = strlen (str);
201 if (str[len] == '/')
202 str[len] = '\0';
204 /* Search pathname from the end and check for hwcap strings. */
205 for (;;)
207 ptr = strrchr (str, '/');
209 if (ptr == NULL)
210 break;
212 h = _dl_string_hwcap (ptr + 1);
214 if (h == (uint64_t) -1)
216 h = _dl_string_platform (ptr + 1);
217 if (h == (uint64_t) -1)
219 if (strcmp (ptr + 1, TLS_SUBPATH) == 0)
220 h = TLS_HWCAP_BIT;
221 else
222 break;
225 hwcap += 1ULL << h;
227 /* Search the next part of the path. */
228 *ptr = '\0';
231 free (str);
232 return hwcap;
235 /* Handle program arguments. */
236 static error_t
237 parse_opt (int key, char *arg, struct argp_state *state)
239 switch (key)
241 case 'C':
242 cache_file = arg;
243 /* Ignore auxiliary cache since we use non-standard cache. */
244 opt_ignore_aux_cache = 1;
245 break;
246 case 'f':
247 config_file = arg;
248 break;
249 case 'i':
250 opt_ignore_aux_cache = 1;
251 break;
252 case 'l':
253 opt_manual_link = 1;
254 break;
255 case 'N':
256 opt_build_cache = 0;
257 break;
258 case 'n':
259 opt_build_cache = 0;
260 opt_only_cline = 1;
261 break;
262 case 'p':
263 opt_print_cache = 1;
264 break;
265 case 'r':
266 opt_chroot = arg;
267 break;
268 case 'v':
269 opt_verbose = 1;
270 break;
271 case 'X':
272 opt_link = 0;
273 break;
274 case 'c':
275 if (strcmp (arg, "old") == 0)
276 opt_format = opt_format_old;
277 else if (strcmp (arg, "compat") == 0)
278 opt_format = opt_format_compat;
279 else if (strcmp (arg, "new") == 0)
280 opt_format = opt_format_new;
281 break;
282 default:
283 return ARGP_ERR_UNKNOWN;
286 return 0;
289 /* Print bug-reporting information in the help message. */
290 static char *
291 more_help (int key, const char *text, void *input)
293 char *tp = NULL;
294 switch (key)
296 case ARGP_KEY_HELP_EXTRA:
297 /* We print some extra information. */
298 if (asprintf (&tp, gettext ("\
299 For bug reporting instructions, please see:\n\
300 %s.\n"), REPORT_BUGS_TO) < 0)
301 return NULL;
302 return tp;
303 default:
304 break;
306 return (char *) text;
309 /* Print the version information. */
310 static void
311 print_version (FILE *stream, struct argp_state *state)
313 fprintf (stream, "ldconfig %s%s\n", PKGVERSION, VERSION);
314 fprintf (stream, gettext ("\
315 Copyright (C) %s Free Software Foundation, Inc.\n\
316 This is free software; see the source for copying conditions. There is NO\n\
317 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
318 "), "2022");
319 fprintf (stream, gettext ("Written by %s.\n"),
320 "Andreas Jaeger");
323 /* Allocate a new subdirectory with full path PATH under ENTRY, using
324 inode data from *ST. */
325 static struct dir_entry *
326 new_sub_entry (const struct dir_entry *entry, const char *path,
327 const struct stat *st)
329 struct dir_entry *new_entry = xmalloc (sizeof (struct dir_entry));
330 new_entry->from_file = entry->from_file;
331 new_entry->from_line = entry->from_line;
332 new_entry->path = xstrdup (path);
333 new_entry->flag = entry->flag;
334 new_entry->hwcaps = NULL;
335 new_entry->next = NULL;
336 new_entry->ino = st->st_ino;
337 new_entry->dev = st->st_dev;
338 return new_entry;
341 /* Add a single directory entry. Return true if the directory is
342 actually added (because it is not a duplicate). */
343 static bool
344 add_single_dir (struct dir_entry *entry, int verbose)
346 struct dir_entry *ptr, *prev;
347 bool added = true;
349 ptr = dir_entries;
350 prev = ptr;
351 while (ptr != NULL)
353 /* Check for duplicates. */
354 if (ptr->ino == entry->ino && ptr->dev == entry->dev)
356 if (opt_verbose && verbose)
358 error (0, 0, _("Path `%s' given more than once"), entry->path);
359 fprintf (stderr, _("(from %s:%d and %s:%d)\n"),
360 entry->from_file, entry->from_line,
361 ptr->from_file, ptr->from_line);
363 /* Use the newer information. */
364 ptr->flag = entry->flag;
365 free (entry->path);
366 free (entry);
367 added = false;
368 break;
370 prev = ptr;
371 ptr = ptr->next;
373 /* Is this the first entry? */
374 if (ptr == NULL && dir_entries == NULL)
375 dir_entries = entry;
376 else if (ptr == NULL)
377 prev->next = entry;
378 return added;
381 /* Check if PATH contains a "glibc-hwcaps" subdirectory. If so, queue
382 its subdirectories for glibc-hwcaps processing. */
383 static void
384 add_glibc_hwcaps_subdirectories (struct dir_entry *entry, const char *path)
386 /* glibc-hwcaps subdirectories do not nest. */
387 assert (entry->hwcaps == NULL);
389 char *glibc_hwcaps;
390 if (asprintf (&glibc_hwcaps, "%s/" GLIBC_HWCAPS_SUBDIRECTORY, path) < 0)
391 error (EXIT_FAILURE, errno, _("Could not form glibc-hwcaps path"));
393 DIR *dir = opendir (glibc_hwcaps);
394 if (dir != NULL)
396 while (true)
398 errno = 0;
399 struct dirent64 *e = readdir64 (dir);
400 if (e == NULL)
402 if (errno == 0)
403 break;
404 else
405 error (EXIT_FAILURE, errno, _("Listing directory %s"), path);
408 /* Ignore hidden subdirectories, including "." and "..", and
409 regular files. File names containing a ':' cannot be
410 looked up by the dynamic loader, so skip those as
411 well. */
412 if (e->d_name[0] == '.' || e->d_type == DT_REG
413 || strchr (e->d_name, ':') != NULL)
414 continue;
416 /* See if this entry eventually resolves to a directory. */
417 struct stat st;
418 if (fstatat (dirfd (dir), e->d_name, &st, 0) < 0)
419 /* Ignore unreadable entries. */
420 continue;
422 if (S_ISDIR (st.st_mode))
424 /* This is a directory, so it needs to be scanned for
425 libraries, associated with the hwcaps implied by the
426 subdirectory name. */
427 char *new_path;
428 if (asprintf (&new_path, "%s/" GLIBC_HWCAPS_SUBDIRECTORY "/%s",
429 /* Use non-canonicalized path here. */
430 entry->path, e->d_name) < 0)
431 error (EXIT_FAILURE, errno,
432 _("Could not form glibc-hwcaps path"));
433 struct dir_entry *new_entry = new_sub_entry (entry, new_path,
434 &st);
435 free (new_path);
436 new_entry->hwcaps = new_glibc_hwcaps_subdirectory (e->d_name);
437 add_single_dir (new_entry, 0);
441 closedir (dir);
444 free (glibc_hwcaps);
447 /* Add one directory to the list of directories to process. */
448 static void
449 add_dir_1 (const char *line, const char *from_file, int from_line)
451 unsigned int i;
452 struct dir_entry *entry = xmalloc (sizeof (struct dir_entry));
453 entry->hwcaps = NULL;
454 entry->next = NULL;
456 entry->from_file = strdup (from_file);
457 entry->from_line = from_line;
459 entry->path = xstrdup (line);
460 entry->flag = FLAG_ELF_LIBC6;
462 /* Canonify path: for now only remove leading and trailing
463 whitespace and the trailing slashes. */
464 i = strlen (entry->path);
466 while (i > 0 && isspace (entry->path[i - 1]))
467 entry->path[--i] = '\0';
469 while (i > 0 && entry->path[i - 1] == '/')
470 entry->path[--i] = '\0';
472 if (i == 0)
474 free (entry->path);
475 free (entry);
476 return;
479 char *path = entry->path;
480 if (opt_chroot != NULL)
481 path = chroot_canon (opt_chroot, path);
483 struct stat stat_buf;
484 if (path == NULL || stat (path, &stat_buf))
486 if (opt_verbose)
487 error (0, errno, _("Can't stat %s"), entry->path);
488 free (entry->path);
489 free (entry);
491 else
493 entry->ino = stat_buf.st_ino;
494 entry->dev = stat_buf.st_dev;
496 if (add_single_dir (entry, 1))
497 /* Add glibc-hwcaps subdirectories if present. */
498 add_glibc_hwcaps_subdirectories (entry, path);
501 if (opt_chroot != NULL)
502 free (path);
505 static void
506 add_dir (const char *line)
508 add_dir_1 (line, "<builtin>", 0);
511 static int
512 chroot_stat (const char *real_path, const char *path, struct stat *st)
514 int ret;
515 char *canon_path;
517 if (!opt_chroot)
518 return stat (real_path, st);
520 ret = lstat (real_path, st);
521 if (ret || !S_ISLNK (st->st_mode))
522 return ret;
524 canon_path = chroot_canon (opt_chroot, path);
525 if (canon_path == NULL)
526 return -1;
528 ret = stat (canon_path, st);
529 free (canon_path);
530 return ret;
533 /* Create a symbolic link from soname to libname in directory path. */
534 static void
535 create_links (const char *real_path, const char *path, const char *libname,
536 const char *soname)
538 char *full_libname, *full_soname;
539 char *real_full_libname, *real_full_soname;
540 struct stat stat_lib, stat_so, lstat_so;
541 int do_link = 1;
542 int do_remove = 1;
543 /* XXX: The logics in this function should be simplified. */
545 /* Get complete path. */
546 full_libname = alloca (strlen (path) + strlen (libname) + 2);
547 full_soname = alloca (strlen (path) + strlen (soname) + 2);
548 sprintf (full_libname, "%s/%s", path, libname);
549 sprintf (full_soname, "%s/%s", path, soname);
550 if (opt_chroot != NULL)
552 real_full_libname = alloca (strlen (real_path) + strlen (libname) + 2);
553 real_full_soname = alloca (strlen (real_path) + strlen (soname) + 2);
554 sprintf (real_full_libname, "%s/%s", real_path, libname);
555 sprintf (real_full_soname, "%s/%s", real_path, soname);
557 else
559 real_full_libname = full_libname;
560 real_full_soname = full_soname;
563 /* Does soname already exist and point to the right library? */
564 if (chroot_stat (real_full_soname, full_soname, &stat_so) == 0)
566 if (chroot_stat (real_full_libname, full_libname, &stat_lib))
568 error (0, 0, _("Can't stat %s\n"), full_libname);
569 return;
571 if (stat_lib.st_dev == stat_so.st_dev
572 && stat_lib.st_ino == stat_so.st_ino)
573 /* Link is already correct. */
574 do_link = 0;
575 else if (lstat (full_soname, &lstat_so) == 0
576 && !S_ISLNK (lstat_so.st_mode))
578 error (0, 0, _("%s is not a symbolic link\n"), full_soname);
579 do_link = 0;
580 do_remove = 0;
583 else if (lstat (real_full_soname, &lstat_so) != 0
584 || !S_ISLNK (lstat_so.st_mode))
585 /* Unless it is a stale symlink, there is no need to remove. */
586 do_remove = 0;
588 if (opt_verbose)
589 printf ("\t%s -> %s", soname, libname);
591 if (do_link && opt_link)
593 /* Remove old link. */
594 if (do_remove)
595 if (unlink (real_full_soname))
597 error (0, 0, _("Can't unlink %s"), full_soname);
598 do_link = 0;
600 /* Create symbolic link. */
601 if (do_link && symlink (libname, real_full_soname))
603 error (0, 0, _("Can't link %s to %s"), full_soname, libname);
604 do_link = 0;
606 if (opt_verbose)
608 if (do_link)
609 fputs (_(" (changed)\n"), stdout);
610 else
611 fputs (_(" (SKIPPED)\n"), stdout);
614 else if (opt_verbose)
615 fputs ("\n", stdout);
618 /* Manually link the given library. */
619 static void
620 manual_link (char *library)
622 char *path;
623 char *real_path;
624 char *real_library;
625 char *libname;
626 char *soname;
627 struct stat stat_buf;
628 int flag;
629 unsigned int isa_level;
631 /* Prepare arguments for create_links call. Split library name in
632 directory and filename first. Since path is allocated, we've got
633 to be careful to free at the end. */
634 path = xstrdup (library);
635 libname = strrchr (path, '/');
637 if (libname)
639 /* Successfully split names. Check if path is just "/" to avoid
640 an empty path. */
641 if (libname == path)
643 libname = library + 1;
644 path = xrealloc (path, 2);
645 strcpy (path, "/");
647 else
649 *libname = '\0';
650 ++libname;
653 else
655 /* There's no path, construct one. */
656 libname = library;
657 path = xrealloc (path, 2);
658 strcpy (path, ".");
661 if (opt_chroot != NULL)
663 real_path = chroot_canon (opt_chroot, path);
664 if (real_path == NULL)
666 error (0, errno, _("Can't find %s"), path);
667 free (path);
668 return;
670 real_library = alloca (strlen (real_path) + strlen (libname) + 2);
671 sprintf (real_library, "%s/%s", real_path, libname);
673 else
675 real_path = path;
676 real_library = library;
679 /* Do some sanity checks first. */
680 if (lstat (real_library, &stat_buf))
682 error (0, errno, _("Cannot lstat %s"), library);
683 goto out;
685 /* We don't want links here! */
686 else if (!S_ISREG (stat_buf.st_mode))
688 error (0, 0, _("Ignored file %s since it is not a regular file."),
689 library);
690 goto out;
693 if (process_file (real_library, library, libname, &flag, &isa_level, &soname,
694 0, &stat_buf))
696 error (0, 0, _("No link created since soname could not be found for %s"),
697 library);
698 goto out;
700 if (soname == NULL)
701 soname = implicit_soname (libname, flag);
702 create_links (real_path, path, libname, soname);
703 free (soname);
704 out:
705 if (path != real_path)
706 free (real_path);
707 free (path);
711 /* Read a whole directory and search for libraries.
712 The purpose is two-fold:
713 - search for libraries which will be added to the cache
714 - create symbolic links to the soname for each library
716 This has to be done separatly for each directory.
718 To keep track of which libraries to add to the cache and which
719 links to create, we save a list of all libraries.
721 The algorithm is basically:
722 for all libraries in the directory do
723 get soname of library
724 if soname is already in list
725 if new library is newer, replace entry
726 otherwise ignore this library
727 otherwise add library to list
729 For example, if the two libraries libxy.so.1.1 and libxy.so.1.2
730 exist and both have the same soname, e.g. libxy.so, a symbolic link
731 is created from libxy.so.1.2 (the newer one) to libxy.so.
732 libxy.so.1.2 and libxy.so are added to the cache - but not
733 libxy.so.1.1. */
735 /* Information for one library. */
736 struct dlib_entry
738 char *name;
739 char *soname;
740 int flag;
741 int is_link;
742 unsigned int isa_level;
743 struct dlib_entry *next;
747 static void
748 search_dir (const struct dir_entry *entry)
750 uint64_t hwcap;
751 if (entry->hwcaps == NULL)
753 hwcap = path_hwcap (entry->path);
754 if (opt_verbose)
756 if (hwcap != 0)
757 printf ("%s: (hwcap: %#.16" PRIx64 ")", entry->path, hwcap);
758 else
759 printf ("%s:", entry->path);
762 else
764 hwcap = 0;
765 if (opt_verbose)
766 printf ("%s: (hwcap: \"%s\")", entry->path,
767 glibc_hwcaps_subdirectory_name (entry->hwcaps));
769 if (opt_verbose)
770 printf (_(" (from %s:%d)\n"), entry->from_file, entry->from_line);
772 char *dir_name;
773 char *real_file_name;
774 size_t real_file_name_len;
775 size_t file_name_len = PATH_MAX;
776 char *file_name = alloca (file_name_len);
777 if (opt_chroot != NULL)
779 dir_name = chroot_canon (opt_chroot, entry->path);
780 real_file_name_len = PATH_MAX;
781 real_file_name = alloca (real_file_name_len);
783 else
785 dir_name = entry->path;
786 real_file_name_len = 0;
787 real_file_name = file_name;
790 DIR *dir;
791 if (dir_name == NULL || (dir = opendir (dir_name)) == NULL)
793 if (opt_verbose)
794 error (0, errno, _("Can't open directory %s"), entry->path);
795 if (opt_chroot != NULL && dir_name != NULL)
796 free (dir_name);
797 return;
800 struct dirent64 *direntry;
801 struct dlib_entry *dlibs = NULL;
802 while ((direntry = readdir64 (dir)) != NULL)
804 int flag;
805 /* We only look at links and regular files. */
806 if (direntry->d_type != DT_UNKNOWN
807 && direntry->d_type != DT_LNK
808 && direntry->d_type != DT_REG
809 && direntry->d_type != DT_DIR)
810 continue;
811 /* Does this file look like a shared library or is it a hwcap
812 subdirectory (if not already processing a glibc-hwcaps
813 subdirectory)? The dynamic linker is also considered as
814 shared library. */
815 if (!_dl_is_dso (direntry->d_name)
816 && (direntry->d_type == DT_REG
817 || (entry->hwcaps == NULL
818 && !is_hwcap_platform (direntry->d_name))))
819 continue;
821 size_t len = strlen (direntry->d_name);
822 /* Skip temporary files created by the prelink program. Files with
823 names like these are never really DSOs we want to look at. */
824 if (len >= sizeof (".#prelink#") - 1)
826 if (strcmp (direntry->d_name + len - sizeof (".#prelink#") + 1,
827 ".#prelink#") == 0)
828 continue;
829 if (len >= sizeof (".#prelink#.XXXXXX") - 1
830 && memcmp (direntry->d_name + len - sizeof (".#prelink#.XXXXXX")
831 + 1, ".#prelink#.", sizeof (".#prelink#.") - 1) == 0)
832 continue;
834 len += strlen (entry->path) + 2;
835 if (len > file_name_len)
837 file_name_len = len;
838 file_name = alloca (file_name_len);
839 if (!opt_chroot)
840 real_file_name = file_name;
842 sprintf (file_name, "%s/%s", entry->path, direntry->d_name);
843 if (opt_chroot != NULL)
845 len = strlen (dir_name) + strlen (direntry->d_name) + 2;
846 if (len > real_file_name_len)
848 real_file_name_len = len;
849 real_file_name = alloca (real_file_name_len);
851 sprintf (real_file_name, "%s/%s", dir_name, direntry->d_name);
854 struct stat lstat_buf;
855 /* We optimize and try to do the lstat call only if needed. */
856 if (direntry->d_type != DT_UNKNOWN)
857 lstat_buf.st_mode = DTTOIF (direntry->d_type);
858 else
859 if (__glibc_unlikely (lstat (real_file_name, &lstat_buf)))
861 error (0, errno, _("Cannot lstat %s"), file_name);
862 continue;
865 struct stat stat_buf;
866 bool is_dir;
867 int is_link = S_ISLNK (lstat_buf.st_mode);
868 if (is_link)
870 /* In case of symlink, we check if the symlink refers to
871 a directory. */
872 char *target_name = real_file_name;
873 if (opt_chroot != NULL)
875 target_name = chroot_canon (opt_chroot, file_name);
876 if (target_name == NULL)
878 if (strstr (file_name, ".so") == NULL)
879 error (0, 0, _("Input file %s not found.\n"), file_name);
880 continue;
883 if (__glibc_unlikely (stat (target_name, &stat_buf)))
885 if (opt_verbose)
886 error (0, errno, _("Cannot stat %s"), file_name);
888 /* Remove stale symlinks. */
889 if (opt_link && strstr (direntry->d_name, ".so."))
890 unlink (real_file_name);
892 if (opt_chroot != NULL)
893 free (target_name);
895 continue;
898 if (opt_chroot != NULL)
899 free (target_name);
901 is_dir = S_ISDIR (stat_buf.st_mode);
903 /* lstat_buf is later stored, update contents. */
904 lstat_buf.st_dev = stat_buf.st_dev;
905 lstat_buf.st_ino = stat_buf.st_ino;
906 lstat_buf.st_size = stat_buf.st_size;
907 lstat_buf.st_ctime = stat_buf.st_ctime;
909 else
910 is_dir = S_ISDIR (lstat_buf.st_mode);
912 /* No descending into subdirectories if this directory is a
913 glibc-hwcaps subdirectory (which are not recursive). */
914 if (entry->hwcaps == NULL
915 && is_dir && is_hwcap_platform (direntry->d_name))
917 if (!is_link
918 && direntry->d_type != DT_UNKNOWN
919 && __builtin_expect (lstat (real_file_name, &lstat_buf), 0))
921 error (0, errno, _("Cannot lstat %s"), file_name);
922 continue;
925 /* Handle subdirectory later. */
926 struct dir_entry *new_entry = new_sub_entry (entry, file_name,
927 &lstat_buf);
928 add_single_dir (new_entry, 0);
929 continue;
931 else if (!S_ISREG (lstat_buf.st_mode) && !is_link)
932 continue;
934 char *real_name;
935 if (opt_chroot != NULL && is_link)
937 real_name = chroot_canon (opt_chroot, file_name);
938 if (real_name == NULL)
940 if (strstr (file_name, ".so") == NULL)
941 error (0, 0, _("Input file %s not found.\n"), file_name);
942 continue;
945 else
946 real_name = real_file_name;
948 /* Call lstat if not done yet. */
949 if (!is_link
950 && direntry->d_type != DT_UNKNOWN
951 && __builtin_expect (lstat (real_file_name, &lstat_buf), 0))
953 error (0, errno, _("Cannot lstat %s"), file_name);
954 continue;
957 /* First search whether the auxiliary cache contains this
958 library already and it's not changed. */
959 char *soname;
960 unsigned int isa_level;
961 if (!search_aux_cache (&lstat_buf, &flag, &isa_level, &soname))
963 if (process_file (real_name, file_name, direntry->d_name, &flag,
964 &isa_level, &soname, is_link, &lstat_buf))
966 if (real_name != real_file_name)
967 free (real_name);
968 continue;
970 else if (opt_build_cache)
971 add_to_aux_cache (&lstat_buf, flag, isa_level, soname);
974 if (soname == NULL)
975 soname = implicit_soname (direntry->d_name, flag);
977 /* A link may just point to itself. */
978 if (is_link)
980 /* If the path the link points to isn't its soname or it is not
981 the .so symlink for ld(1), we treat it as a normal file.
983 You should always do this:
985 libfoo.so -> SONAME -> Arbitrary package-chosen name.
987 e.g. libfoo.so -> libfoo.so.1 -> libfooimp.so.9.99.
988 Given a SONAME of libfoo.so.1.
990 You should *never* do this:
992 libfoo.so -> libfooimp.so.9.99
994 If you do, and your SONAME is libfoo.so.1, then libfoo.so
995 fails to point at the SONAME. In that case ldconfig may consider
996 libfoo.so as another implementation of SONAME and will create
997 symlinks against it causing problems when you try to upgrade
998 or downgrade. The problems will arise because ldconfig will,
999 depending on directory ordering, creat symlinks against libfoo.so
1000 e.g. libfoo.so.1.2 -> libfoo.so, but when libfoo.so is removed
1001 (typically by the removal of a development pacakge not required
1002 for the runtime) it will break the libfoo.so.1.2 symlink and the
1003 application will fail to start. */
1004 const char *real_base_name = basename (real_file_name);
1006 if (strcmp (real_base_name, soname) != 0)
1008 len = strlen (real_base_name);
1009 if (len < strlen (".so")
1010 || strcmp (real_base_name + len - strlen (".so"), ".so") != 0
1011 || strncmp (real_base_name, soname, len) != 0)
1012 is_link = 0;
1016 if (real_name != real_file_name)
1017 free (real_name);
1019 if (is_link)
1021 free (soname);
1022 soname = xstrdup (direntry->d_name);
1025 /* Some sanity checks to print warnings. */
1026 if (opt_verbose)
1028 if (flag == FLAG_ELF_LIBC6 && entry->flag != FLAG_ELF_LIBC6)
1029 error (0, 0, _("libc6 library %s in wrong directory"), file_name);
1032 /* Add library to list. */
1033 struct dlib_entry *dlib_ptr;
1034 for (dlib_ptr = dlibs; dlib_ptr != NULL; dlib_ptr = dlib_ptr->next)
1036 /* Is soname already in list? */
1037 if (strcmp (dlib_ptr->soname, soname) == 0)
1039 /* Prefer a file to a link, otherwise check which one
1040 is newer. */
1041 if ((!is_link && dlib_ptr->is_link)
1042 || (is_link == dlib_ptr->is_link
1043 && _dl_cache_libcmp (dlib_ptr->name, direntry->d_name) < 0))
1045 /* It's newer - add it. */
1046 /* Flag should be the same - sanity check. */
1047 if (dlib_ptr->flag != flag)
1048 error (0, 0, _("libraries %s and %s in directory %s have same soname but different type."),
1049 dlib_ptr->name, direntry->d_name, entry->path);
1050 free (dlib_ptr->name);
1051 dlib_ptr->name = xstrdup (direntry->d_name);
1052 dlib_ptr->is_link = is_link;
1053 dlib_ptr->isa_level = isa_level;
1055 /* Don't add this library, abort loop. */
1056 /* Also free soname, since it's dynamically allocated. */
1057 free (soname);
1058 break;
1061 /* Add the library if it's not already in. */
1062 if (dlib_ptr == NULL)
1064 dlib_ptr = (struct dlib_entry *)xmalloc (sizeof (struct dlib_entry));
1065 dlib_ptr->name = xstrdup (direntry->d_name);
1066 dlib_ptr->soname = soname;
1067 dlib_ptr->flag = flag;
1068 dlib_ptr->is_link = is_link;
1069 dlib_ptr->isa_level = isa_level;
1070 /* Add at head of list. */
1071 dlib_ptr->next = dlibs;
1072 dlibs = dlib_ptr;
1076 closedir (dir);
1078 /* Now dlibs contains a list of all libs - add those to the cache
1079 and created all symbolic links. */
1080 struct dlib_entry *dlib_ptr;
1081 for (dlib_ptr = dlibs; dlib_ptr != NULL; dlib_ptr = dlib_ptr->next)
1083 /* The cached file name is the soname for non-glibc-hwcaps
1084 subdirectories (relying on symbolic links; this helps with
1085 library updates that change the file name), and the actual
1086 file for glibc-hwcaps subdirectories. */
1087 const char *filename;
1088 if (entry->hwcaps == NULL)
1090 /* Don't create links to links. */
1091 if (dlib_ptr->is_link == 0)
1092 create_links (dir_name, entry->path, dlib_ptr->name,
1093 dlib_ptr->soname);
1094 filename = dlib_ptr->soname;
1096 else
1098 /* Do not create links in glibc-hwcaps subdirectories, but
1099 still log the cache addition. */
1100 if (opt_verbose)
1101 printf ("\t%s -> %s\n", dlib_ptr->soname, dlib_ptr->name);
1102 filename = dlib_ptr->name;
1104 if (opt_build_cache)
1105 add_to_cache (entry->path, filename, dlib_ptr->soname,
1106 dlib_ptr->flag, dlib_ptr->isa_level, hwcap,
1107 entry->hwcaps);
1110 /* Free all resources. */
1111 while (dlibs)
1113 dlib_ptr = dlibs;
1114 free (dlib_ptr->soname);
1115 free (dlib_ptr->name);
1116 dlibs = dlibs->next;
1117 free (dlib_ptr);
1120 if (opt_chroot != NULL && dir_name != NULL)
1121 free (dir_name);
1124 /* Search through all libraries. */
1125 static void
1126 search_dirs (void)
1128 struct dir_entry *entry;
1130 for (entry = dir_entries; entry != NULL; entry = entry->next)
1131 search_dir (entry);
1133 /* Free all allocated memory. */
1134 while (dir_entries)
1136 entry = dir_entries;
1137 dir_entries = dir_entries->next;
1138 free (entry->path);
1139 free (entry);
1144 static void parse_conf_include (const char *config_file, unsigned int lineno,
1145 bool do_chroot, const char *pattern);
1147 /* Parse configuration file. */
1148 static void
1149 parse_conf (const char *filename, bool do_chroot)
1151 FILE *file = NULL;
1152 char *line = NULL;
1153 const char *canon;
1154 size_t len = 0;
1155 unsigned int lineno;
1157 if (do_chroot && opt_chroot)
1159 canon = chroot_canon (opt_chroot, filename);
1160 if (canon)
1161 file = fopen (canon, "r");
1162 else
1163 canon = filename;
1165 else
1167 canon = filename;
1168 file = fopen (filename, "r");
1171 if (file == NULL)
1173 if (errno != ENOENT)
1174 error (0, errno, _("\
1175 Warning: ignoring configuration file that cannot be opened: %s"),
1176 canon);
1177 if (canon != filename)
1178 free ((char *) canon);
1179 return;
1182 /* No threads use this stream. */
1183 __fsetlocking (file, FSETLOCKING_BYCALLER);
1185 if (canon != filename)
1186 free ((char *) canon);
1188 lineno = 0;
1191 ssize_t n = getline (&line, &len, file);
1192 if (n < 0)
1193 break;
1195 ++lineno;
1196 if (line[n - 1] == '\n')
1197 line[n - 1] = '\0';
1199 /* Because the file format does not know any form of quoting we
1200 can search forward for the next '#' character and if found
1201 make it terminating the line. */
1202 *strchrnul (line, '#') = '\0';
1204 /* Remove leading whitespace. NUL is no whitespace character. */
1205 char *cp = line;
1206 while (isspace (*cp))
1207 ++cp;
1209 /* If the line is blank it is ignored. */
1210 if (cp[0] == '\0')
1211 continue;
1213 if (!strncmp (cp, "include", 7) && isblank (cp[7]))
1215 char *dir;
1216 cp += 8;
1217 while ((dir = strsep (&cp, " \t")) != NULL)
1218 if (dir[0] != '\0')
1219 parse_conf_include (filename, lineno, do_chroot, dir);
1221 else if (!strncasecmp (cp, "hwcap", 5) && isblank (cp[5]))
1222 error (0, 0, _("%s:%u: hwcap directive ignored"), filename, lineno);
1223 else
1224 add_dir_1 (cp, filename, lineno);
1226 while (!feof_unlocked (file));
1228 /* Free buffer and close file. */
1229 free (line);
1230 fclose (file);
1233 /* Handle one word in an `include' line, a glob pattern of additional
1234 config files to read. */
1235 static void
1236 parse_conf_include (const char *config_file, unsigned int lineno,
1237 bool do_chroot, const char *pattern)
1239 if (opt_chroot != NULL && pattern[0] != '/')
1240 error (EXIT_FAILURE, 0,
1241 _("need absolute file name for configuration file when using -r"));
1243 char *copy = NULL;
1244 if (pattern[0] != '/' && strchr (config_file, '/') != NULL)
1246 if (asprintf (&copy, "%s/%s", dirname (strdupa (config_file)),
1247 pattern) < 0)
1248 error (EXIT_FAILURE, 0, _("memory exhausted"));
1249 pattern = copy;
1252 glob64_t gl;
1253 int result;
1254 if (do_chroot && opt_chroot)
1256 char *canon = chroot_canon (opt_chroot, pattern);
1257 if (canon == NULL)
1258 return;
1259 result = glob64 (canon, 0, NULL, &gl);
1260 free (canon);
1262 else
1263 result = glob64 (pattern, 0, NULL, &gl);
1265 switch (result)
1267 case 0:
1268 for (size_t i = 0; i < gl.gl_pathc; ++i)
1269 parse_conf (gl.gl_pathv[i], false);
1270 globfree64 (&gl);
1271 break;
1273 case GLOB_NOMATCH:
1274 break;
1276 case GLOB_NOSPACE:
1277 errno = ENOMEM;
1278 /* Fall through. */
1279 case GLOB_ABORTED:
1280 if (opt_verbose)
1281 error (0, errno, _("%s:%u: cannot read directory %s"),
1282 config_file, lineno, pattern);
1283 break;
1285 default:
1286 abort ();
1287 break;
1290 free (copy);
1293 /* Honour LD_HWCAP_MASK. */
1294 static void
1295 set_hwcap (void)
1297 char *mask = getenv ("LD_HWCAP_MASK");
1299 if (mask)
1300 hwcap_mask = strtoul (mask, NULL, 0);
1305 main (int argc, char **argv)
1307 /* Set locale via LC_ALL. */
1308 setlocale (LC_ALL, "");
1310 /* But keep the C collation. That way `include' directives using
1311 globbing patterns are processed in a locale-independent order. */
1312 setlocale (LC_COLLATE, "C");
1314 /* Set the text message domain. */
1315 textdomain (_libc_intl_domainname);
1317 /* Parse and process arguments. */
1318 int remaining;
1319 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
1321 /* Remaining arguments are additional directories if opt_manual_link
1322 is not set. */
1323 if (remaining != argc && !opt_manual_link)
1325 int i;
1326 for (i = remaining; i < argc; ++i)
1327 if (opt_build_cache && argv[i][0] != '/')
1328 error (EXIT_FAILURE, 0,
1329 _("relative path `%s' used to build cache"),
1330 argv[i]);
1331 else
1332 add_dir_1 (argv[i], "<cmdline>", 0);
1335 set_hwcap ();
1337 if (opt_chroot != NULL)
1339 /* Normalize the path a bit, we might need it for printing later. */
1340 char *endp = rawmemchr (opt_chroot, '\0');
1341 while (endp > opt_chroot && endp[-1] == '/')
1342 --endp;
1343 *endp = '\0';
1344 if (endp == opt_chroot)
1345 opt_chroot = NULL;
1347 if (opt_chroot != NULL)
1349 /* It is faster to use chroot if we can. */
1350 if (!chroot (opt_chroot))
1352 if (chdir ("/"))
1353 error (EXIT_FAILURE, errno, _("Can't chdir to /"));
1354 opt_chroot = NULL;
1359 if (cache_file == NULL)
1361 cache_file = alloca (strlen (LD_SO_CACHE) + 1);
1362 strcpy (cache_file, LD_SO_CACHE);
1365 if (config_file == NULL)
1366 config_file = LD_SO_CONF;
1368 if (opt_print_cache)
1370 if (opt_chroot != NULL)
1372 char *p = chroot_canon (opt_chroot, cache_file);
1373 if (p == NULL)
1374 error (EXIT_FAILURE, errno, _("Can't open cache file %s\n"),
1375 cache_file);
1376 cache_file = p;
1378 print_cache (cache_file);
1379 if (opt_chroot != NULL)
1380 free (cache_file);
1381 exit (0);
1384 if (opt_chroot != NULL)
1386 /* Canonicalize the directory name of cache_file, not cache_file,
1387 because we'll rename a temporary cache file to it. */
1388 char *p = strrchr (cache_file, '/');
1389 char *canon = chroot_canon (opt_chroot,
1390 p ? (*p = '\0', cache_file) : "/");
1392 if (canon == NULL)
1393 error (EXIT_FAILURE, errno,
1394 _("Can't open cache file directory %s\n"),
1395 p ? cache_file : "/");
1397 if (p)
1398 ++p;
1399 else
1400 p = cache_file;
1402 cache_file = alloca (strlen (canon) + strlen (p) + 2);
1403 sprintf (cache_file, "%s/%s", canon, p);
1404 free (canon);
1407 if (opt_manual_link)
1409 /* Link all given libraries manually. */
1410 int i;
1412 for (i = remaining; i < argc; ++i)
1413 manual_link (argv[i]);
1415 exit (0);
1419 if (opt_build_cache)
1420 init_cache ();
1422 if (!opt_only_cline)
1424 parse_conf (config_file, true);
1426 /* Always add the standard search paths. */
1427 add_system_dir (SLIBDIR);
1428 if (strcmp (SLIBDIR, LIBDIR))
1429 add_system_dir (LIBDIR);
1432 const char *aux_cache_file = _PATH_LDCONFIG_AUX_CACHE;
1433 if (opt_chroot != NULL)
1434 aux_cache_file = chroot_canon (opt_chroot, aux_cache_file);
1436 if (! opt_ignore_aux_cache && aux_cache_file)
1437 load_aux_cache (aux_cache_file);
1438 else
1439 init_aux_cache ();
1441 search_dirs ();
1443 if (opt_build_cache)
1445 save_cache (cache_file);
1446 if (aux_cache_file)
1447 save_aux_cache (aux_cache_file);
1450 return 0;