Replace FSF snail mail address with URLs.
[glibc.git] / elf / ldconfig.c
blobaa97213cc2c05725ffe6bfee17b6605c4f2c249a
1 /* Copyright (C) 1999-2011, 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, _("Can't open configuration file %s"), canon);
1065 if (canon != filename)
1066 free ((char *) canon);
1067 return;
1070 /* No threads use this stream. */
1071 __fsetlocking (file, FSETLOCKING_BYCALLER);
1073 if (canon != filename)
1074 free ((char *) canon);
1076 lineno = 0;
1079 ssize_t n = getline (&line, &len, file);
1080 if (n < 0)
1081 break;
1083 ++lineno;
1084 if (line[n - 1] == '\n')
1085 line[n - 1] = '\0';
1087 /* Because the file format does not know any form of quoting we
1088 can search forward for the next '#' character and if found
1089 make it terminating the line. */
1090 *strchrnul (line, '#') = '\0';
1092 /* Remove leading whitespace. NUL is no whitespace character. */
1093 char *cp = line;
1094 while (isspace (*cp))
1095 ++cp;
1097 /* If the line is blank it is ignored. */
1098 if (cp[0] == '\0')
1099 continue;
1101 if (!strncmp (cp, "include", 7) && isblank (cp[7]))
1103 char *dir;
1104 cp += 8;
1105 while ((dir = strsep (&cp, " \t")) != NULL)
1106 if (dir[0] != '\0')
1107 parse_conf_include (filename, lineno, do_chroot, dir);
1109 else if (!strncasecmp (cp, "hwcap", 5) && isblank (cp[5]))
1111 cp += 6;
1112 char *p, *name = NULL;
1113 unsigned long int n = strtoul (cp, &cp, 0);
1114 if (cp != NULL && isblank (*cp))
1115 while ((p = strsep (&cp, " \t")) != NULL)
1116 if (p[0] != '\0')
1118 if (name == NULL)
1119 name = p;
1120 else
1122 name = NULL;
1123 break;
1126 if (name == NULL)
1128 error (EXIT_FAILURE, 0, _("%s:%u: bad syntax in hwcap line"),
1129 filename, lineno);
1130 break;
1132 if (n >= (64 - _DL_FIRST_EXTRA))
1133 error (EXIT_FAILURE, 0,
1134 _("%s:%u: hwcap index %lu above maximum %u"),
1135 filename, lineno, n, 64 - _DL_FIRST_EXTRA - 1);
1136 if (hwcap_extra[n] == NULL)
1138 for (unsigned long int h = 0; h < (64 - _DL_FIRST_EXTRA); ++h)
1139 if (hwcap_extra[h] != NULL && !strcmp (name, hwcap_extra[h]))
1140 error (EXIT_FAILURE, 0,
1141 _("%s:%u: hwcap index %lu already defined as %s"),
1142 filename, lineno, h, name);
1143 hwcap_extra[n] = xstrdup (name);
1145 else
1147 if (strcmp (name, hwcap_extra[n]))
1148 error (EXIT_FAILURE, 0,
1149 _("%s:%u: hwcap index %lu already defined as %s"),
1150 filename, lineno, n, hwcap_extra[n]);
1151 if (opt_verbose)
1152 error (0, 0, _("%s:%u: duplicate hwcap %lu %s"),
1153 filename, lineno, n, name);
1156 else
1157 add_dir (cp);
1159 while (!feof_unlocked (file));
1161 /* Free buffer and close file. */
1162 free (line);
1163 fclose (file);
1166 /* Handle one word in an `include' line, a glob pattern of additional
1167 config files to read. */
1168 static void
1169 parse_conf_include (const char *config_file, unsigned int lineno,
1170 bool do_chroot, const char *pattern)
1172 if (opt_chroot && pattern[0] != '/')
1173 error (EXIT_FAILURE, 0,
1174 _("need absolute file name for configuration file when using -r"));
1176 char *copy = NULL;
1177 if (pattern[0] != '/' && strchr (config_file, '/') != NULL)
1179 if (asprintf (&copy, "%s/%s", dirname (strdupa (config_file)),
1180 pattern) < 0)
1181 error (EXIT_FAILURE, 0, _("memory exhausted"));
1182 pattern = copy;
1185 glob64_t gl;
1186 int result;
1187 if (do_chroot && opt_chroot)
1189 char *canon = chroot_canon (opt_chroot, pattern);
1190 if (canon == NULL)
1191 return;
1192 result = glob64 (canon, 0, NULL, &gl);
1193 free (canon);
1195 else
1196 result = glob64 (pattern, 0, NULL, &gl);
1198 switch (result)
1200 case 0:
1201 for (size_t i = 0; i < gl.gl_pathc; ++i)
1202 parse_conf (gl.gl_pathv[i], false);
1203 globfree64 (&gl);
1204 break;
1206 case GLOB_NOMATCH:
1207 break;
1209 case GLOB_NOSPACE:
1210 errno = ENOMEM;
1211 case GLOB_ABORTED:
1212 if (opt_verbose)
1213 error (0, errno, _("%s:%u: cannot read directory %s"),
1214 config_file, lineno, pattern);
1215 break;
1217 default:
1218 abort ();
1219 break;
1222 free (copy);
1225 /* Honour LD_HWCAP_MASK. */
1226 static void
1227 set_hwcap (void)
1229 char *mask = getenv ("LD_HWCAP_MASK");
1231 if (mask)
1232 hwcap_mask = strtoul (mask, NULL, 0);
1237 main (int argc, char **argv)
1239 /* Set locale via LC_ALL. */
1240 setlocale (LC_ALL, "");
1242 /* Set the text message domain. */
1243 textdomain (_libc_intl_domainname);
1245 /* Parse and process arguments. */
1246 int remaining;
1247 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
1249 /* Remaining arguments are additional directories if opt_manual_link
1250 is not set. */
1251 if (remaining != argc && !opt_manual_link)
1253 int i;
1254 for (i = remaining; i < argc; ++i)
1255 if (opt_build_cache && argv[i][0] != '/')
1256 error (EXIT_FAILURE, 0,
1257 _("relative path `%s' used to build cache"),
1258 argv[i]);
1259 else
1260 add_dir (argv[i]);
1263 hwcap_extra[63 - _DL_FIRST_EXTRA] = "tls";
1265 set_hwcap ();
1267 if (opt_chroot)
1269 /* Normalize the path a bit, we might need it for printing later. */
1270 char *endp = rawmemchr (opt_chroot, '\0');
1271 while (endp > opt_chroot && endp[-1] == '/')
1272 --endp;
1273 *endp = '\0';
1274 if (endp == opt_chroot)
1275 opt_chroot = NULL;
1277 if (opt_chroot)
1279 /* It is faster to use chroot if we can. */
1280 if (!chroot (opt_chroot))
1282 if (chdir ("/"))
1283 error (EXIT_FAILURE, errno, _("Can't chdir to /"));
1284 opt_chroot = NULL;
1289 if (cache_file == NULL)
1291 cache_file = alloca (strlen (LD_SO_CACHE) + 1);
1292 strcpy (cache_file, LD_SO_CACHE);
1295 if (config_file == NULL)
1296 config_file = LD_SO_CONF;
1298 if (opt_print_cache)
1300 if (opt_chroot)
1302 char *p = chroot_canon (opt_chroot, cache_file);
1303 if (p == NULL)
1304 error (EXIT_FAILURE, errno, _("Can't open cache file %s\n"),
1305 cache_file);
1306 cache_file = p;
1308 print_cache (cache_file);
1309 if (opt_chroot)
1310 free (cache_file);
1311 exit (0);
1314 if (opt_chroot)
1316 /* Canonicalize the directory name of cache_file, not cache_file,
1317 because we'll rename a temporary cache file to it. */
1318 char *p = strrchr (cache_file, '/');
1319 char *canon = chroot_canon (opt_chroot,
1320 p ? (*p = '\0', cache_file) : "/");
1322 if (canon == NULL)
1323 error (EXIT_FAILURE, errno,
1324 _("Can't open cache file directory %s\n"),
1325 p ? cache_file : "/");
1327 if (p)
1328 ++p;
1329 else
1330 p = cache_file;
1332 cache_file = alloca (strlen (canon) + strlen (p) + 2);
1333 sprintf (cache_file, "%s/%s", canon, p);
1334 free (canon);
1337 if (opt_manual_link)
1339 /* Link all given libraries manually. */
1340 int i;
1342 for (i = remaining; i < argc; ++i)
1343 manual_link (argv[i]);
1345 exit (0);
1349 if (opt_build_cache)
1350 init_cache ();
1352 if (!opt_only_cline)
1354 parse_conf (config_file, true);
1356 /* Always add the standard search paths. */
1357 add_system_dir (SLIBDIR);
1358 if (strcmp (SLIBDIR, LIBDIR))
1359 add_system_dir (LIBDIR);
1362 const char *aux_cache_file = _PATH_LDCONFIG_AUX_CACHE;
1363 if (opt_chroot)
1364 aux_cache_file = chroot_canon (opt_chroot, aux_cache_file);
1366 if (! opt_ignore_aux_cache && aux_cache_file)
1367 load_aux_cache (aux_cache_file);
1368 else
1369 init_aux_cache ();
1371 search_dirs ();
1373 if (opt_build_cache)
1375 save_cache (cache_file);
1376 if (aux_cache_file)
1377 save_aux_cache (aux_cache_file);
1380 return 0;