Fix clog, clog10 spurious underflow exceptions (bug 14337).
[glibc.git] / elf / ldconfig.c
blob8d6e77f8ec5036567bd90f45d2dc667cec8eeef2
1 /* Copyright (C) 1999-2012 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Andreas Jaeger <aj@suse.de>, 1999.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; version 2 of the License, or
8 (at your option) any later version.
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <http://www.gnu.org/licenses/>. */
18 #define PROCINFO_CLASS static
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 <sys/fcntl.h>
35 #include <sys/mman.h>
36 #include <sys/stat.h>
37 #include <sys/types.h>
38 #include <glob.h>
39 #include <libgen.h>
41 #include <ldconfig.h>
42 #include <dl-cache.h>
44 #include <dl-procinfo.h>
46 #ifdef _DL_FIRST_PLATFORM
47 # define _DL_FIRST_EXTRA (_DL_FIRST_PLATFORM + _DL_PLATFORMS_COUNT)
48 #else
49 # define _DL_FIRST_EXTRA _DL_HWCAP_COUNT
50 #endif
52 #ifndef LD_SO_CONF
53 # define LD_SO_CONF SYSCONFDIR "/ld.so.conf"
54 #endif
56 /* Get libc version number. */
57 #include <version.h>
59 #define PACKAGE _libc_intl_domainname
61 static const struct
63 const char *name;
64 int flag;
65 } lib_types[] =
67 {"libc4", FLAG_LIBC4},
68 {"libc5", FLAG_ELF_LIBC5},
69 {"libc6", FLAG_ELF_LIBC6},
70 {"glibc2", FLAG_ELF_LIBC6}
74 /* List of directories to handle. */
75 struct dir_entry
77 char *path;
78 int flag;
79 ino64_t ino;
80 dev_t dev;
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 /* 0: only libc5/glibc2; 1: both; 2: only glibc 2.2. */
97 int opt_format = 1;
99 /* Build cache. */
100 static int opt_build_cache = 1;
102 /* Generate 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 /* Configuration-defined capabilities defined in kernel vDSOs. */
127 static const char *hwcap_extra[64 - _DL_FIRST_EXTRA];
129 /* Name and version of program. */
130 static void print_version (FILE *stream, struct argp_state *state);
131 void (*argp_program_version_hook) (FILE *, struct argp_state *)
132 = print_version;
134 /* Function to print some extra text in the help message. */
135 static char *more_help (int key, const char *text, void *input);
137 /* Definitions of arguments for argp functions. */
138 static const struct argp_option options[] =
140 { "print-cache", 'p', NULL, 0, N_("Print cache"), 0},
141 { "verbose", 'v', NULL, 0, N_("Generate verbose messages"), 0},
142 { NULL, 'N', NULL, 0, N_("Don't build cache"), 0},
143 { NULL, 'X', NULL, 0, N_("Don't generate links"), 0},
144 { NULL, 'r', N_("ROOT"), 0, N_("Change to and use ROOT as root directory"), 0},
145 { NULL, 'C', N_("CACHE"), 0, N_("Use CACHE as cache file"), 0},
146 { NULL, 'f', N_("CONF"), 0, N_("Use CONF as configuration file"), 0},
147 { NULL, 'n', NULL, 0, N_("Only process directories specified on the command line. Don't build cache."), 0},
148 { NULL, 'l', NULL, 0, N_("Manually link individual libraries."), 0},
149 { "format", 'c', N_("FORMAT"), 0, N_("Format to use: new, old or compat (default)"), 0},
150 { "ignore-aux-cache", 'i', NULL, 0, N_("Ignore auxiliary cache file"), 0},
151 { NULL, 0, NULL, 0, NULL, 0 }
154 #define PROCINFO_CLASS static
155 #include <dl-procinfo.c>
157 /* Short description of program. */
158 static const char doc[] = N_("Configure Dynamic Linker Run Time Bindings.");
160 /* Prototype for option handler. */
161 static error_t parse_opt (int key, char *arg, struct argp_state *state);
163 /* Data structure to communicate with argp functions. */
164 static struct argp argp =
166 options, parse_opt, NULL, doc, NULL, more_help, NULL
169 /* Check if string corresponds to an important hardware capability or
170 a platform. */
171 static int
172 is_hwcap_platform (const char *name)
174 int hwcap_idx = _dl_string_hwcap (name);
176 if (hwcap_idx != -1 && ((1 << hwcap_idx) & hwcap_mask))
177 return 1;
179 hwcap_idx = _dl_string_platform (name);
180 if (hwcap_idx != -1)
181 return 1;
183 for (hwcap_idx = _DL_FIRST_EXTRA; hwcap_idx < 64; ++hwcap_idx)
184 if (hwcap_extra[hwcap_idx - _DL_FIRST_EXTRA] != NULL
185 && !strcmp (name, hwcap_extra[hwcap_idx - _DL_FIRST_EXTRA]))
186 return 1;
188 return 0;
191 /* Get hwcap (including platform) encoding of path. */
192 static uint64_t
193 path_hwcap (const char *path)
195 char *str = xstrdup (path);
196 char *ptr;
197 uint64_t hwcap = 0;
198 uint64_t h;
200 size_t len;
202 len = strlen (str);
203 if (str[len] == '/')
204 str[len] = '\0';
206 /* Search pathname from the end and check for hwcap strings. */
207 for (;;)
209 ptr = strrchr (str, '/');
211 if (ptr == NULL)
212 break;
214 h = _dl_string_hwcap (ptr + 1);
216 if (h == (uint64_t) -1)
218 h = _dl_string_platform (ptr + 1);
219 if (h == (uint64_t) -1)
221 for (h = _DL_FIRST_EXTRA; h < 64; ++h)
222 if (hwcap_extra[h - _DL_FIRST_EXTRA] != NULL
223 && !strcmp (ptr + 1, hwcap_extra[h - _DL_FIRST_EXTRA]))
224 break;
225 if (h == 64)
226 break;
229 hwcap += 1ULL << h;
231 /* Search the next part of the path. */
232 *ptr = '\0';
235 free (str);
236 return hwcap;
239 /* Handle program arguments. */
240 static error_t
241 parse_opt (int key, char *arg, struct argp_state *state)
243 switch (key)
245 case 'C':
246 cache_file = arg;
247 /* Ignore auxiliary cache since we use non-standard cache. */
248 opt_ignore_aux_cache = 1;
249 break;
250 case 'f':
251 config_file = arg;
252 break;
253 case 'i':
254 opt_ignore_aux_cache = 1;
255 break;
256 case 'l':
257 opt_manual_link = 1;
258 break;
259 case 'N':
260 opt_build_cache = 0;
261 break;
262 case 'n':
263 opt_build_cache = 0;
264 opt_only_cline = 1;
265 break;
266 case 'p':
267 opt_print_cache = 1;
268 break;
269 case 'r':
270 opt_chroot = arg;
271 break;
272 case 'v':
273 opt_verbose = 1;
274 break;
275 case 'X':
276 opt_link = 0;
277 break;
278 case 'c':
279 if (strcmp (arg, "old") == 0)
280 opt_format = 0;
281 else if (strcmp (arg, "compat") == 0)
282 opt_format = 1;
283 else if (strcmp (arg, "new") == 0)
284 opt_format = 2;
285 break;
286 default:
287 return ARGP_ERR_UNKNOWN;
290 return 0;
293 /* Print bug-reporting information in the help message. */
294 static char *
295 more_help (int key, const char *text, void *input)
297 switch (key)
299 case ARGP_KEY_HELP_EXTRA:
300 /* We print some extra information. */
301 return strdup (gettext ("\
302 For bug reporting instructions, please see:\n\
303 <http://www.gnu.org/software/libc/bugs.html>.\n"));
304 default:
305 break;
307 return (char *) text;
310 /* Print the version information. */
311 static void
312 print_version (FILE *stream, struct argp_state *state)
314 fprintf (stream, "ldconfig (GNU %s) %s\n", PACKAGE, VERSION);
315 fprintf (stream, gettext ("\
316 Copyright (C) %s Free Software Foundation, Inc.\n\
317 This is free software; see the source for copying conditions. There is NO\n\
318 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
319 "), "2012");
320 fprintf (stream, gettext ("Written by %s.\n"),
321 "Andreas Jaeger");
324 /* Add a single directory entry. */
325 static void
326 add_single_dir (struct dir_entry *entry, int verbose)
328 struct dir_entry *ptr, *prev;
330 ptr = dir_entries;
331 prev = ptr;
332 while (ptr != NULL)
334 /* Check for duplicates. */
335 if (ptr->ino == entry->ino && ptr->dev == entry->dev)
337 if (opt_verbose && verbose)
338 error (0, 0, _("Path `%s' given more than once"), entry->path);
339 /* Use the newer information. */
340 ptr->flag = entry->flag;
341 free (entry->path);
342 free (entry);
343 break;
345 prev = ptr;
346 ptr = ptr->next;
348 /* Is this the first entry? */
349 if (ptr == NULL && dir_entries == NULL)
350 dir_entries = entry;
351 else if (ptr == NULL)
352 prev->next = entry;
355 /* Add one directory to the list of directories to process. */
356 static void
357 add_dir (const char *line)
359 unsigned int i;
360 struct dir_entry *entry = xmalloc (sizeof (struct dir_entry));
361 entry->next = NULL;
363 /* Search for an '=' sign. */
364 entry->path = xstrdup (line);
365 char *equal_sign = strchr (entry->path, '=');
366 if (equal_sign)
368 *equal_sign = '\0';
369 ++equal_sign;
370 entry->flag = FLAG_ANY;
371 for (i = 0; i < sizeof (lib_types) / sizeof (lib_types[0]); ++i)
372 if (strcmp (equal_sign, lib_types[i].name) == 0)
374 entry->flag = lib_types[i].flag;
375 break;
377 if (entry->flag == FLAG_ANY)
378 error (0, 0, _("%s is not a known library type"), equal_sign);
380 else
382 entry->flag = FLAG_ANY;
385 /* Canonify path: for now only remove leading and trailing
386 whitespace and the trailing slashes. */
387 i = strlen (entry->path);
389 while (i > 0 && isspace (entry->path[i - 1]))
390 entry->path[--i] = '\0';
392 while (i > 0 && entry->path[i - 1] == '/')
393 entry->path[--i] = '\0';
395 if (i == 0)
396 return;
398 char *path = entry->path;
399 if (opt_chroot)
400 path = chroot_canon (opt_chroot, path);
402 struct stat64 stat_buf;
403 if (path == NULL || stat64 (path, &stat_buf))
405 if (opt_verbose)
406 error (0, errno, _("Can't stat %s"), entry->path);
407 free (entry->path);
408 free (entry);
410 else
412 entry->ino = stat_buf.st_ino;
413 entry->dev = stat_buf.st_dev;
415 add_single_dir (entry, 1);
418 if (opt_chroot)
419 free (path);
423 static int
424 chroot_stat (const char *real_path, const char *path, struct stat64 *st)
426 int ret;
427 char *canon_path;
429 if (!opt_chroot)
430 return stat64 (real_path, st);
432 ret = lstat64 (real_path, st);
433 if (ret || !S_ISLNK (st->st_mode))
434 return ret;
436 canon_path = chroot_canon (opt_chroot, path);
437 if (canon_path == NULL)
438 return -1;
440 ret = stat64 (canon_path, st);
441 free (canon_path);
442 return ret;
445 /* Create a symbolic link from soname to libname in directory path. */
446 static void
447 create_links (const char *real_path, const char *path, const char *libname,
448 const char *soname)
450 char *full_libname, *full_soname;
451 char *real_full_libname, *real_full_soname;
452 struct stat64 stat_lib, stat_so, lstat_so;
453 int do_link = 1;
454 int do_remove = 1;
455 /* XXX: The logics in this function should be simplified. */
457 /* Get complete path. */
458 full_libname = alloca (strlen (path) + strlen (libname) + 2);
459 full_soname = alloca (strlen (path) + strlen (soname) + 2);
460 sprintf (full_libname, "%s/%s", path, libname);
461 sprintf (full_soname, "%s/%s", path, soname);
462 if (opt_chroot)
464 real_full_libname = alloca (strlen (real_path) + strlen (libname) + 2);
465 real_full_soname = alloca (strlen (real_path) + strlen (soname) + 2);
466 sprintf (real_full_libname, "%s/%s", real_path, libname);
467 sprintf (real_full_soname, "%s/%s", real_path, soname);
469 else
471 real_full_libname = full_libname;
472 real_full_soname = full_soname;
475 /* Does soname already exist and point to the right library? */
476 if (chroot_stat (real_full_soname, full_soname, &stat_so) == 0)
478 if (chroot_stat (real_full_libname, full_libname, &stat_lib))
480 error (0, 0, _("Can't stat %s\n"), full_libname);
481 return;
483 if (stat_lib.st_dev == stat_so.st_dev
484 && stat_lib.st_ino == stat_so.st_ino)
485 /* Link is already correct. */
486 do_link = 0;
487 else if (lstat64 (full_soname, &lstat_so) == 0
488 && !S_ISLNK (lstat_so.st_mode))
490 error (0, 0, _("%s is not a symbolic link\n"), full_soname);
491 do_link = 0;
492 do_remove = 0;
495 else if (lstat64 (real_full_soname, &lstat_so) != 0
496 || !S_ISLNK (lstat_so.st_mode))
497 /* Unless it is a stale symlink, there is no need to remove. */
498 do_remove = 0;
500 if (opt_verbose)
501 printf ("\t%s -> %s", soname, libname);
503 if (do_link && opt_link)
505 /* Remove old link. */
506 if (do_remove)
507 if (unlink (real_full_soname))
509 error (0, 0, _("Can't unlink %s"), full_soname);
510 do_link = 0;
512 /* Create symbolic link. */
513 if (do_link && symlink (libname, real_full_soname))
515 error (0, 0, _("Can't link %s to %s"), full_soname, libname);
516 do_link = 0;
518 if (opt_verbose)
520 if (do_link)
521 fputs (_(" (changed)\n"), stdout);
522 else
523 fputs (_(" (SKIPPED)\n"), stdout);
526 else if (opt_verbose)
527 fputs ("\n", stdout);
530 /* Manually link the given library. */
531 static void
532 manual_link (char *library)
534 char *path;
535 char *real_path;
536 char *real_library;
537 char *libname;
538 char *soname;
539 struct stat64 stat_buf;
540 int flag;
541 unsigned int osversion;
543 /* Prepare arguments for create_links call. Split library name in
544 directory and filename first. Since path is allocated, we've got
545 to be careful to free at the end. */
546 path = xstrdup (library);
547 libname = strrchr (path, '/');
549 if (libname)
551 /* Successfully split names. Check if path is just "/" to avoid
552 an empty path. */
553 if (libname == path)
555 libname = library + 1;
556 path = xrealloc (path, 2);
557 strcpy (path, "/");
559 else
561 *libname = '\0';
562 ++libname;
565 else
567 /* There's no path, construct one. */
568 libname = library;
569 path = xrealloc (path, 2);
570 strcpy (path, ".");
573 if (opt_chroot)
575 real_path = chroot_canon (opt_chroot, path);
576 if (real_path == NULL)
578 error (0, errno, _("Can't find %s"), path);
579 free (path);
580 return;
582 real_library = alloca (strlen (real_path) + strlen (libname) + 2);
583 sprintf (real_library, "%s/%s", real_path, libname);
585 else
587 real_path = path;
588 real_library = library;
591 /* Do some sanity checks first. */
592 if (lstat64 (real_library, &stat_buf))
594 error (0, errno, _("Cannot lstat %s"), library);
595 free (path);
596 return;
598 /* We don't want links here! */
599 else if (!S_ISREG (stat_buf.st_mode))
601 error (0, 0, _("Ignored file %s since it is not a regular file."),
602 library);
603 free (path);
604 return;
607 if (process_file (real_library, library, libname, &flag, &osversion,
608 &soname, 0, &stat_buf))
610 error (0, 0, _("No link created since soname could not be found for %s"),
611 library);
612 free (path);
613 return;
615 if (soname == NULL)
616 soname = implicit_soname (libname, flag);
617 create_links (real_path, path, libname, soname);
618 free (soname);
619 free (path);
623 /* Read a whole directory and search for libraries.
624 The purpose is two-fold:
625 - search for libraries which will be added to the cache
626 - create symbolic links to the soname for each library
628 This has to be done separatly for each directory.
630 To keep track of which libraries to add to the cache and which
631 links to create, we save a list of all libraries.
633 The algorithm is basically:
634 for all libraries in the directory do
635 get soname of library
636 if soname is already in list
637 if new library is newer, replace entry
638 otherwise ignore this library
639 otherwise add library to list
641 For example, if the two libraries libxy.so.1.1 and libxy.so.1.2
642 exist and both have the same soname, e.g. libxy.so, a symbolic link
643 is created from libxy.so.1.2 (the newer one) to libxy.so.
644 libxy.so.1.2 and libxy.so are added to the cache - but not
645 libxy.so.1.1. */
647 /* Information for one library. */
648 struct dlib_entry
650 char *name;
651 char *soname;
652 int flag;
653 int is_link;
654 unsigned int osversion;
655 struct dlib_entry *next;
659 static void
660 search_dir (const struct dir_entry *entry)
662 uint64_t hwcap = path_hwcap (entry->path);
663 if (opt_verbose)
665 if (hwcap != 0)
666 printf ("%s: (hwcap: %#.16" PRIx64 ")\n", entry->path, hwcap);
667 else
668 printf ("%s:\n", entry->path);
671 char *dir_name;
672 char *real_file_name;
673 size_t real_file_name_len;
674 size_t file_name_len = PATH_MAX;
675 char *file_name = alloca (file_name_len);
676 if (opt_chroot)
678 dir_name = chroot_canon (opt_chroot, entry->path);
679 real_file_name_len = PATH_MAX;
680 real_file_name = alloca (real_file_name_len);
682 else
684 dir_name = entry->path;
685 real_file_name_len = 0;
686 real_file_name = file_name;
689 DIR *dir;
690 if (dir_name == NULL || (dir = opendir (dir_name)) == NULL)
692 if (opt_verbose)
693 error (0, errno, _("Can't open directory %s"), entry->path);
694 if (opt_chroot && dir_name)
695 free (dir_name);
696 return;
699 struct dirent64 *direntry;
700 struct dlib_entry *dlibs = NULL;
701 while ((direntry = readdir64 (dir)) != NULL)
703 int flag;
704 #ifdef _DIRENT_HAVE_D_TYPE
705 /* We only look at links and regular files. */
706 if (direntry->d_type != DT_UNKNOWN
707 && direntry->d_type != DT_LNK
708 && direntry->d_type != DT_REG
709 && direntry->d_type != DT_DIR)
710 continue;
711 #endif /* _DIRENT_HAVE_D_TYPE */
712 /* Does this file look like a shared library or is it a hwcap
713 subdirectory? The dynamic linker is also considered as
714 shared library. */
715 if (((strncmp (direntry->d_name, "lib", 3) != 0
716 && strncmp (direntry->d_name, "ld-", 3) != 0)
717 || strstr (direntry->d_name, ".so") == NULL)
718 && (
719 #ifdef _DIRENT_HAVE_D_TYPE
720 direntry->d_type == DT_REG ||
721 #endif
722 !is_hwcap_platform (direntry->d_name)))
723 continue;
725 size_t len = strlen (direntry->d_name);
726 /* Skip temporary files created by the prelink program. Files with
727 names like these are never really DSOs we want to look at. */
728 if (len >= sizeof (".#prelink#") - 1)
730 if (strcmp (direntry->d_name + len - sizeof (".#prelink#") + 1,
731 ".#prelink#") == 0)
732 continue;
733 if (len >= sizeof (".#prelink#.XXXXXX") - 1
734 && memcmp (direntry->d_name + len - sizeof (".#prelink#.XXXXXX")
735 + 1, ".#prelink#.", sizeof (".#prelink#.") - 1) == 0)
736 continue;
738 len += strlen (entry->path) + 2;
739 if (len > file_name_len)
741 file_name_len = len;
742 file_name = alloca (file_name_len);
743 if (!opt_chroot)
744 real_file_name = file_name;
746 sprintf (file_name, "%s/%s", entry->path, direntry->d_name);
747 if (opt_chroot)
749 len = strlen (dir_name) + strlen (direntry->d_name) + 2;
750 if (len > real_file_name_len)
752 real_file_name_len = len;
753 real_file_name = alloca (real_file_name_len);
755 sprintf (real_file_name, "%s/%s", dir_name, direntry->d_name);
758 struct stat64 lstat_buf;
759 #ifdef _DIRENT_HAVE_D_TYPE
760 /* We optimize and try to do the lstat call only if needed. */
761 if (direntry->d_type != DT_UNKNOWN)
762 lstat_buf.st_mode = DTTOIF (direntry->d_type);
763 else
764 #endif
765 if (__builtin_expect (lstat64 (real_file_name, &lstat_buf), 0))
767 error (0, errno, _("Cannot lstat %s"), file_name);
768 continue;
771 struct stat64 stat_buf;
772 int is_dir;
773 int is_link = S_ISLNK (lstat_buf.st_mode);
774 if (is_link)
776 /* In case of symlink, we check if the symlink refers to
777 a directory. */
778 char *target_name = real_file_name;
779 if (opt_chroot)
781 target_name = chroot_canon (opt_chroot, file_name);
782 if (target_name == NULL)
784 if (strstr (file_name, ".so") == NULL)
785 error (0, 0, _("Input file %s not found.\n"), file_name);
786 continue;
789 if (__builtin_expect (stat64 (target_name, &stat_buf), 0))
791 if (opt_verbose)
792 error (0, errno, _("Cannot stat %s"), file_name);
794 /* Remove stale symlinks. */
795 if (strstr (direntry->d_name, ".so."))
796 unlink (real_file_name);
797 continue;
799 is_dir = S_ISDIR (stat_buf.st_mode);
801 /* lstat_buf is later stored, update contents. */
802 lstat_buf.st_dev = stat_buf.st_dev;
803 lstat_buf.st_ino = stat_buf.st_ino;
804 lstat_buf.st_size = stat_buf.st_size;
805 lstat_buf.st_ctime = stat_buf.st_ctime;
807 else
808 is_dir = S_ISDIR (lstat_buf.st_mode);
810 if (is_dir && is_hwcap_platform (direntry->d_name))
812 /* Handle subdirectory later. */
813 struct dir_entry *new_entry;
815 new_entry = xmalloc (sizeof (struct dir_entry));
816 new_entry->path = xstrdup (file_name);
817 new_entry->flag = entry->flag;
818 new_entry->next = NULL;
819 #ifdef _DIRENT_HAVE_D_TYPE
820 /* We have filled in lstat only #ifndef
821 _DIRENT_HAVE_D_TYPE. Fill it in if needed. */
822 if (!is_link
823 && direntry->d_type != DT_UNKNOWN
824 && __builtin_expect (lstat64 (real_file_name, &lstat_buf), 0))
826 error (0, errno, _("Cannot lstat %s"), file_name);
827 free (new_entry->path);
828 free (new_entry);
829 continue;
831 #endif
832 new_entry->ino = lstat_buf.st_ino;
833 new_entry->dev = lstat_buf.st_dev;
834 add_single_dir (new_entry, 0);
835 continue;
837 else if (!S_ISREG (lstat_buf.st_mode) && !is_link)
838 continue;
840 char *real_name;
841 if (opt_chroot && is_link)
843 real_name = chroot_canon (opt_chroot, file_name);
844 if (real_name == NULL)
846 if (strstr (file_name, ".so") == NULL)
847 error (0, 0, _("Input file %s not found.\n"), file_name);
848 continue;
851 else
852 real_name = real_file_name;
854 #ifdef _DIRENT_HAVE_D_TYPE
855 /* Call lstat64 if not done yet. */
856 if (!is_link
857 && direntry->d_type != DT_UNKNOWN
858 && __builtin_expect (lstat64 (real_file_name, &lstat_buf), 0))
860 error (0, errno, _("Cannot lstat %s"), file_name);
861 continue;
863 #endif
865 /* First search whether the auxiliary cache contains this
866 library already and it's not changed. */
867 char *soname;
868 unsigned int osversion;
869 if (!search_aux_cache (&lstat_buf, &flag, &osversion, &soname))
871 if (process_file (real_name, file_name, direntry->d_name, &flag,
872 &osversion, &soname, is_link, &lstat_buf))
874 if (real_name != real_file_name)
875 free (real_name);
876 continue;
878 else if (opt_build_cache)
879 add_to_aux_cache (&lstat_buf, flag, osversion, soname);
882 if (soname == NULL)
883 soname = implicit_soname (direntry->d_name, flag);
885 /* A link may just point to itself. */
886 if (is_link)
888 /* If the path the link points to isn't its soname and it is not
889 .so symlink for ld(1) only, we treat it as a normal file. */
890 const char *real_base_name = basename (real_file_name);
892 if (strcmp (real_base_name, soname) != 0)
894 len = strlen (real_base_name);
895 if (len < strlen (".so")
896 || strcmp (real_base_name + len - strlen (".so"), ".so") != 0
897 || strncmp (real_base_name, soname, len) != 0)
898 is_link = 0;
902 if (real_name != real_file_name)
903 free (real_name);
905 if (is_link)
907 free (soname);
908 soname = xstrdup (direntry->d_name);
911 if (flag == FLAG_ELF
912 && (entry->flag == FLAG_ELF_LIBC5
913 || entry->flag == FLAG_ELF_LIBC6))
914 flag = entry->flag;
916 /* Some sanity checks to print warnings. */
917 if (opt_verbose)
919 if (flag == FLAG_ELF_LIBC5 && entry->flag != FLAG_ELF_LIBC5
920 && entry->flag != FLAG_ANY)
921 error (0, 0, _("libc5 library %s in wrong directory"), file_name);
922 if (flag == FLAG_ELF_LIBC6 && entry->flag != FLAG_ELF_LIBC6
923 && entry->flag != FLAG_ANY)
924 error (0, 0, _("libc6 library %s in wrong directory"), file_name);
925 if (flag == FLAG_LIBC4 && entry->flag != FLAG_LIBC4
926 && entry->flag != FLAG_ANY)
927 error (0, 0, _("libc4 library %s in wrong directory"), file_name);
930 /* Add library to list. */
931 struct dlib_entry *dlib_ptr;
932 for (dlib_ptr = dlibs; dlib_ptr != NULL; dlib_ptr = dlib_ptr->next)
934 /* Is soname already in list? */
935 if (strcmp (dlib_ptr->soname, soname) == 0)
937 /* Prefer a file to a link, otherwise check which one
938 is newer. */
939 if ((!is_link && dlib_ptr->is_link)
940 || (is_link == dlib_ptr->is_link
941 && _dl_cache_libcmp (dlib_ptr->name, direntry->d_name) < 0))
943 /* It's newer - add it. */
944 /* Flag should be the same - sanity check. */
945 if (dlib_ptr->flag != flag)
947 if (dlib_ptr->flag == FLAG_ELF
948 && (flag == FLAG_ELF_LIBC5 || flag == FLAG_ELF_LIBC6))
949 dlib_ptr->flag = flag;
950 else if ((dlib_ptr->flag == FLAG_ELF_LIBC5
951 || dlib_ptr->flag == FLAG_ELF_LIBC6)
952 && flag == FLAG_ELF)
953 dlib_ptr->flag = flag;
954 else
955 error (0, 0, _("libraries %s and %s in directory %s have same soname but different type."),
956 dlib_ptr->name, direntry->d_name,
957 entry->path);
959 free (dlib_ptr->name);
960 dlib_ptr->name = xstrdup (direntry->d_name);
961 dlib_ptr->is_link = is_link;
962 dlib_ptr->osversion = osversion;
964 /* Don't add this library, abort loop. */
965 /* Also free soname, since it's dynamically allocated. */
966 free (soname);
967 break;
970 /* Add the library if it's not already in. */
971 if (dlib_ptr == NULL)
973 dlib_ptr = (struct dlib_entry *)xmalloc (sizeof (struct dlib_entry));
974 dlib_ptr->name = xstrdup (direntry->d_name);
975 dlib_ptr->soname = soname;
976 dlib_ptr->flag = flag;
977 dlib_ptr->is_link = is_link;
978 dlib_ptr->osversion = osversion;
979 /* Add at head of list. */
980 dlib_ptr->next = dlibs;
981 dlibs = dlib_ptr;
985 closedir (dir);
987 /* Now dlibs contains a list of all libs - add those to the cache
988 and created all symbolic links. */
989 struct dlib_entry *dlib_ptr;
990 for (dlib_ptr = dlibs; dlib_ptr != NULL; dlib_ptr = dlib_ptr->next)
992 /* Don't create links to links. */
993 if (dlib_ptr->is_link == 0)
994 create_links (dir_name, entry->path, dlib_ptr->name,
995 dlib_ptr->soname);
996 if (opt_build_cache)
997 add_to_cache (entry->path, dlib_ptr->soname, dlib_ptr->flag,
998 dlib_ptr->osversion, hwcap);
1001 /* Free all resources. */
1002 while (dlibs)
1004 dlib_ptr = dlibs;
1005 free (dlib_ptr->soname);
1006 free (dlib_ptr->name);
1007 dlibs = dlibs->next;
1008 free (dlib_ptr);
1011 if (opt_chroot && dir_name)
1012 free (dir_name);
1015 /* Search through all libraries. */
1016 static void
1017 search_dirs (void)
1019 struct dir_entry *entry;
1021 for (entry = dir_entries; entry != NULL; entry = entry->next)
1022 search_dir (entry);
1024 /* Free all allocated memory. */
1025 while (dir_entries)
1027 entry = dir_entries;
1028 dir_entries = dir_entries->next;
1029 free (entry->path);
1030 free (entry);
1035 static void parse_conf_include (const char *config_file, unsigned int lineno,
1036 bool do_chroot, const char *pattern);
1038 /* Parse configuration file. */
1039 static void
1040 parse_conf (const char *filename, bool do_chroot)
1042 FILE *file = NULL;
1043 char *line = NULL;
1044 const char *canon;
1045 size_t len = 0;
1046 unsigned int lineno;
1048 if (do_chroot && opt_chroot)
1050 canon = chroot_canon (opt_chroot, filename);
1051 if (canon)
1052 file = fopen (canon, "r");
1053 else
1054 canon = filename;
1056 else
1058 canon = filename;
1059 file = fopen (filename, "r");
1062 if (file == NULL)
1064 error (0, errno, _("\
1065 Warning: ignoring configuration file that cannot be opened: %s"),
1066 canon);
1067 if (canon != filename)
1068 free ((char *) canon);
1069 return;
1072 /* No threads use this stream. */
1073 __fsetlocking (file, FSETLOCKING_BYCALLER);
1075 if (canon != filename)
1076 free ((char *) canon);
1078 lineno = 0;
1081 ssize_t n = getline (&line, &len, file);
1082 if (n < 0)
1083 break;
1085 ++lineno;
1086 if (line[n - 1] == '\n')
1087 line[n - 1] = '\0';
1089 /* Because the file format does not know any form of quoting we
1090 can search forward for the next '#' character and if found
1091 make it terminating the line. */
1092 *strchrnul (line, '#') = '\0';
1094 /* Remove leading whitespace. NUL is no whitespace character. */
1095 char *cp = line;
1096 while (isspace (*cp))
1097 ++cp;
1099 /* If the line is blank it is ignored. */
1100 if (cp[0] == '\0')
1101 continue;
1103 if (!strncmp (cp, "include", 7) && isblank (cp[7]))
1105 char *dir;
1106 cp += 8;
1107 while ((dir = strsep (&cp, " \t")) != NULL)
1108 if (dir[0] != '\0')
1109 parse_conf_include (filename, lineno, do_chroot, dir);
1111 else if (!strncasecmp (cp, "hwcap", 5) && isblank (cp[5]))
1113 cp += 6;
1114 char *p, *name = NULL;
1115 unsigned long int n = strtoul (cp, &cp, 0);
1116 if (cp != NULL && isblank (*cp))
1117 while ((p = strsep (&cp, " \t")) != NULL)
1118 if (p[0] != '\0')
1120 if (name == NULL)
1121 name = p;
1122 else
1124 name = NULL;
1125 break;
1128 if (name == NULL)
1130 error (EXIT_FAILURE, 0, _("%s:%u: bad syntax in hwcap line"),
1131 filename, lineno);
1132 break;
1134 if (n >= (64 - _DL_FIRST_EXTRA))
1135 error (EXIT_FAILURE, 0,
1136 _("%s:%u: hwcap index %lu above maximum %u"),
1137 filename, lineno, n, 64 - _DL_FIRST_EXTRA - 1);
1138 if (hwcap_extra[n] == NULL)
1140 for (unsigned long int h = 0; h < (64 - _DL_FIRST_EXTRA); ++h)
1141 if (hwcap_extra[h] != NULL && !strcmp (name, hwcap_extra[h]))
1142 error (EXIT_FAILURE, 0,
1143 _("%s:%u: hwcap index %lu already defined as %s"),
1144 filename, lineno, h, name);
1145 hwcap_extra[n] = xstrdup (name);
1147 else
1149 if (strcmp (name, hwcap_extra[n]))
1150 error (EXIT_FAILURE, 0,
1151 _("%s:%u: hwcap index %lu already defined as %s"),
1152 filename, lineno, n, hwcap_extra[n]);
1153 if (opt_verbose)
1154 error (0, 0, _("%s:%u: duplicate hwcap %lu %s"),
1155 filename, lineno, n, name);
1158 else
1159 add_dir (cp);
1161 while (!feof_unlocked (file));
1163 /* Free buffer and close file. */
1164 free (line);
1165 fclose (file);
1168 /* Handle one word in an `include' line, a glob pattern of additional
1169 config files to read. */
1170 static void
1171 parse_conf_include (const char *config_file, unsigned int lineno,
1172 bool do_chroot, const char *pattern)
1174 if (opt_chroot && pattern[0] != '/')
1175 error (EXIT_FAILURE, 0,
1176 _("need absolute file name for configuration file when using -r"));
1178 char *copy = NULL;
1179 if (pattern[0] != '/' && strchr (config_file, '/') != NULL)
1181 if (asprintf (&copy, "%s/%s", dirname (strdupa (config_file)),
1182 pattern) < 0)
1183 error (EXIT_FAILURE, 0, _("memory exhausted"));
1184 pattern = copy;
1187 glob64_t gl;
1188 int result;
1189 if (do_chroot && opt_chroot)
1191 char *canon = chroot_canon (opt_chroot, pattern);
1192 if (canon == NULL)
1193 return;
1194 result = glob64 (canon, 0, NULL, &gl);
1195 free (canon);
1197 else
1198 result = glob64 (pattern, 0, NULL, &gl);
1200 switch (result)
1202 case 0:
1203 for (size_t i = 0; i < gl.gl_pathc; ++i)
1204 parse_conf (gl.gl_pathv[i], false);
1205 globfree64 (&gl);
1206 break;
1208 case GLOB_NOMATCH:
1209 break;
1211 case GLOB_NOSPACE:
1212 errno = ENOMEM;
1213 case GLOB_ABORTED:
1214 if (opt_verbose)
1215 error (0, errno, _("%s:%u: cannot read directory %s"),
1216 config_file, lineno, pattern);
1217 break;
1219 default:
1220 abort ();
1221 break;
1224 free (copy);
1227 /* Honour LD_HWCAP_MASK. */
1228 static void
1229 set_hwcap (void)
1231 char *mask = getenv ("LD_HWCAP_MASK");
1233 if (mask)
1234 hwcap_mask = strtoul (mask, NULL, 0);
1239 main (int argc, char **argv)
1241 /* Set locale via LC_ALL. */
1242 setlocale (LC_ALL, "");
1244 /* Set the text message domain. */
1245 textdomain (_libc_intl_domainname);
1247 /* Parse and process arguments. */
1248 int remaining;
1249 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
1251 /* Remaining arguments are additional directories if opt_manual_link
1252 is not set. */
1253 if (remaining != argc && !opt_manual_link)
1255 int i;
1256 for (i = remaining; i < argc; ++i)
1257 if (opt_build_cache && argv[i][0] != '/')
1258 error (EXIT_FAILURE, 0,
1259 _("relative path `%s' used to build cache"),
1260 argv[i]);
1261 else
1262 add_dir (argv[i]);
1265 hwcap_extra[63 - _DL_FIRST_EXTRA] = "tls";
1267 set_hwcap ();
1269 if (opt_chroot)
1271 /* Normalize the path a bit, we might need it for printing later. */
1272 char *endp = rawmemchr (opt_chroot, '\0');
1273 while (endp > opt_chroot && endp[-1] == '/')
1274 --endp;
1275 *endp = '\0';
1276 if (endp == opt_chroot)
1277 opt_chroot = NULL;
1279 if (opt_chroot)
1281 /* It is faster to use chroot if we can. */
1282 if (!chroot (opt_chroot))
1284 if (chdir ("/"))
1285 error (EXIT_FAILURE, errno, _("Can't chdir to /"));
1286 opt_chroot = NULL;
1291 if (cache_file == NULL)
1293 cache_file = alloca (strlen (LD_SO_CACHE) + 1);
1294 strcpy (cache_file, LD_SO_CACHE);
1297 if (config_file == NULL)
1298 config_file = LD_SO_CONF;
1300 if (opt_print_cache)
1302 if (opt_chroot)
1304 char *p = chroot_canon (opt_chroot, cache_file);
1305 if (p == NULL)
1306 error (EXIT_FAILURE, errno, _("Can't open cache file %s\n"),
1307 cache_file);
1308 cache_file = p;
1310 print_cache (cache_file);
1311 if (opt_chroot)
1312 free (cache_file);
1313 exit (0);
1316 if (opt_chroot)
1318 /* Canonicalize the directory name of cache_file, not cache_file,
1319 because we'll rename a temporary cache file to it. */
1320 char *p = strrchr (cache_file, '/');
1321 char *canon = chroot_canon (opt_chroot,
1322 p ? (*p = '\0', cache_file) : "/");
1324 if (canon == NULL)
1325 error (EXIT_FAILURE, errno,
1326 _("Can't open cache file directory %s\n"),
1327 p ? cache_file : "/");
1329 if (p)
1330 ++p;
1331 else
1332 p = cache_file;
1334 cache_file = alloca (strlen (canon) + strlen (p) + 2);
1335 sprintf (cache_file, "%s/%s", canon, p);
1336 free (canon);
1339 if (opt_manual_link)
1341 /* Link all given libraries manually. */
1342 int i;
1344 for (i = remaining; i < argc; ++i)
1345 manual_link (argv[i]);
1347 exit (0);
1351 if (opt_build_cache)
1352 init_cache ();
1354 if (!opt_only_cline)
1356 parse_conf (config_file, true);
1358 /* Always add the standard search paths. */
1359 add_system_dir (SLIBDIR);
1360 if (strcmp (SLIBDIR, LIBDIR))
1361 add_system_dir (LIBDIR);
1364 const char *aux_cache_file = _PATH_LDCONFIG_AUX_CACHE;
1365 if (opt_chroot)
1366 aux_cache_file = chroot_canon (opt_chroot, aux_cache_file);
1368 if (! opt_ignore_aux_cache && aux_cache_file)
1369 load_aux_cache (aux_cache_file);
1370 else
1371 init_aux_cache ();
1373 search_dirs ();
1375 if (opt_build_cache)
1377 save_cache (cache_file);
1378 if (aux_cache_file)
1379 save_aux_cache (aux_cache_file);
1382 return 0;