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
30 #include <stdio_ext.h>
34 #include <sys/fcntl.h>
37 #include <sys/types.h>
44 #include <dl-procinfo.h>
46 #ifdef _DL_FIRST_PLATFORM
47 # define _DL_FIRST_EXTRA (_DL_FIRST_PLATFORM + _DL_PLATFORMS_COUNT)
49 # define _DL_FIRST_EXTRA _DL_HWCAP_COUNT
53 # define LD_SO_CONF SYSCONFDIR "/ld.so.conf"
56 /* Get libc version number. */
59 #define PACKAGE _libc_intl_domainname
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. */
81 struct dir_entry
*next
;
84 /* The list is unsorted, contains no duplicates. Entries are added at
86 static struct dir_entry
*dir_entries
;
88 /* Flags for different options. */
90 static int opt_print_cache
;
95 /* Format to support. */
96 /* 0: only libc5/glibc2; 1: both; 2: only glibc 2.2. */
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
*)
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
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
))
179 hwcap_idx
= _dl_string_platform (name
);
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
]))
191 /* Get hwcap (including platform) encoding of path. */
193 path_hwcap (const char *path
)
195 char *str
= xstrdup (path
);
206 /* Search pathname from the end and check for hwcap strings. */
209 ptr
= strrchr (str
, '/');
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
]))
231 /* Search the next part of the path. */
239 /* Handle program arguments. */
241 parse_opt (int key
, char *arg
, struct argp_state
*state
)
247 /* Ignore auxiliary cache since we use non-standard cache. */
248 opt_ignore_aux_cache
= 1;
254 opt_ignore_aux_cache
= 1;
279 if (strcmp (arg
, "old") == 0)
281 else if (strcmp (arg
, "compat") == 0)
283 else if (strcmp (arg
, "new") == 0)
287 return ARGP_ERR_UNKNOWN
;
293 /* Print bug-reporting information in the help message. */
295 more_help (int key
, const char *text
, void *input
)
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"));
307 return (char *) text
;
310 /* Print the version information. */
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\
320 fprintf (stream
, gettext ("Written by %s.\n"),
324 /* Add a single directory entry. */
326 add_single_dir (struct dir_entry
*entry
, int verbose
)
328 struct dir_entry
*ptr
, *prev
;
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
;
348 /* Is this the first entry? */
349 if (ptr
== NULL
&& dir_entries
== NULL
)
351 else if (ptr
== NULL
)
355 /* Add one directory to the list of directories to process. */
357 add_dir (const char *line
)
360 struct dir_entry
*entry
= xmalloc (sizeof (struct dir_entry
));
363 /* Search for an '=' sign. */
364 entry
->path
= xstrdup (line
);
365 char *equal_sign
= strchr (entry
->path
, '=');
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
;
377 if (entry
->flag
== FLAG_ANY
)
378 error (0, 0, _("%s is not a known library type"), equal_sign
);
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';
398 char *path
= entry
->path
;
400 path
= chroot_canon (opt_chroot
, path
);
402 struct stat64 stat_buf
;
403 if (path
== NULL
|| stat64 (path
, &stat_buf
))
406 error (0, errno
, _("Can't stat %s"), entry
->path
);
412 entry
->ino
= stat_buf
.st_ino
;
413 entry
->dev
= stat_buf
.st_dev
;
415 add_single_dir (entry
, 1);
424 chroot_stat (const char *real_path
, const char *path
, struct stat64
*st
)
430 return stat64 (real_path
, st
);
432 ret
= lstat64 (real_path
, st
);
433 if (ret
|| !S_ISLNK (st
->st_mode
))
436 canon_path
= chroot_canon (opt_chroot
, path
);
437 if (canon_path
== NULL
)
440 ret
= stat64 (canon_path
, st
);
445 /* Create a symbolic link from soname to libname in directory path. */
447 create_links (const char *real_path
, const char *path
, const char *libname
,
450 char *full_libname
, *full_soname
;
451 char *real_full_libname
, *real_full_soname
;
452 struct stat64 stat_lib
, stat_so
, lstat_so
;
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
);
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
);
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
);
483 if (stat_lib
.st_dev
== stat_so
.st_dev
484 && stat_lib
.st_ino
== stat_so
.st_ino
)
485 /* Link is already correct. */
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
);
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. */
501 printf ("\t%s -> %s", soname
, libname
);
503 if (do_link
&& opt_link
)
505 /* Remove old link. */
507 if (unlink (real_full_soname
))
509 error (0, 0, _("Can't unlink %s"), full_soname
);
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
);
521 fputs (_(" (changed)\n"), stdout
);
523 fputs (_(" (SKIPPED)\n"), stdout
);
526 else if (opt_verbose
)
527 fputs ("\n", stdout
);
530 /* Manually link the given library. */
532 manual_link (char *library
)
539 struct stat64 stat_buf
;
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
, '/');
551 /* Successfully split names. Check if path is just "/" to avoid
555 libname
= library
+ 1;
556 path
= xrealloc (path
, 2);
567 /* There's no path, construct one. */
569 path
= xrealloc (path
, 2);
575 real_path
= chroot_canon (opt_chroot
, path
);
576 if (real_path
== NULL
)
578 error (0, errno
, _("Can't find %s"), path
);
582 real_library
= alloca (strlen (real_path
) + strlen (libname
) + 2);
583 sprintf (real_library
, "%s/%s", real_path
, libname
);
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
);
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."),
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"),
616 soname
= implicit_soname (libname
, flag
);
617 create_links (real_path
, path
, libname
, soname
);
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
647 /* Information for one library. */
654 unsigned int osversion
;
655 struct dlib_entry
*next
;
660 search_dir (const struct dir_entry
*entry
)
662 uint64_t hwcap
= path_hwcap (entry
->path
);
666 printf ("%s: (hwcap: %#.16" PRIx64
")\n", entry
->path
, hwcap
);
668 printf ("%s:\n", entry
->path
);
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
);
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
);
684 dir_name
= entry
->path
;
685 real_file_name_len
= 0;
686 real_file_name
= file_name
;
690 if (dir_name
== NULL
|| (dir
= opendir (dir_name
)) == NULL
)
693 error (0, errno
, _("Can't open directory %s"), entry
->path
);
694 if (opt_chroot
&& dir_name
)
699 struct dirent64
*direntry
;
700 struct dlib_entry
*dlibs
= NULL
;
701 while ((direntry
= readdir64 (dir
)) != NULL
)
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
)
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
715 if (((strncmp (direntry
->d_name
, "lib", 3) != 0
716 && strncmp (direntry
->d_name
, "ld-", 3) != 0)
717 || strstr (direntry
->d_name
, ".so") == NULL
)
719 #ifdef _DIRENT_HAVE_D_TYPE
720 direntry
->d_type
== DT_REG
||
722 !is_hwcap_platform (direntry
->d_name
)))
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,
733 if (len
>= sizeof (".#prelink#.XXXXXX") - 1
734 && memcmp (direntry
->d_name
+ len
- sizeof (".#prelink#.XXXXXX")
735 + 1, ".#prelink#.", sizeof (".#prelink#.") - 1) == 0)
738 len
+= strlen (entry
->path
) + 2;
739 if (len
> file_name_len
)
742 file_name
= alloca (file_name_len
);
744 real_file_name
= file_name
;
746 sprintf (file_name
, "%s/%s", entry
->path
, direntry
->d_name
);
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
);
765 if (__builtin_expect (lstat64 (real_file_name
, &lstat_buf
), 0))
767 error (0, errno
, _("Cannot lstat %s"), file_name
);
771 struct stat64 stat_buf
;
773 int is_link
= S_ISLNK (lstat_buf
.st_mode
);
776 /* In case of symlink, we check if the symlink refers to
778 char *target_name
= real_file_name
;
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
);
789 if (__builtin_expect (stat64 (target_name
, &stat_buf
), 0))
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
);
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
;
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. */
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
);
832 new_entry
->ino
= lstat_buf
.st_ino
;
833 new_entry
->dev
= lstat_buf
.st_dev
;
834 add_single_dir (new_entry
, 0);
837 else if (!S_ISREG (lstat_buf
.st_mode
) && !is_link
)
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
);
852 real_name
= real_file_name
;
854 #ifdef _DIRENT_HAVE_D_TYPE
855 /* Call lstat64 if not done yet. */
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
);
865 /* First search whether the auxiliary cache contains this
866 library already and it's not changed. */
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
)
878 else if (opt_build_cache
)
879 add_to_aux_cache (&lstat_buf
, flag
, osversion
, soname
);
883 soname
= implicit_soname (direntry
->d_name
, flag
);
885 /* A link may just point to itself. */
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)
902 if (real_name
!= real_file_name
)
908 soname
= xstrdup (direntry
->d_name
);
912 && (entry
->flag
== FLAG_ELF_LIBC5
913 || entry
->flag
== FLAG_ELF_LIBC6
))
916 /* Some sanity checks to print warnings. */
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
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
)
953 dlib_ptr
->flag
= flag
;
955 error (0, 0, _("libraries %s and %s in directory %s have same soname but different type."),
956 dlib_ptr
->name
, direntry
->d_name
,
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. */
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
;
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
,
997 add_to_cache (entry
->path
, dlib_ptr
->soname
, dlib_ptr
->flag
,
998 dlib_ptr
->osversion
, hwcap
);
1001 /* Free all resources. */
1005 free (dlib_ptr
->soname
);
1006 free (dlib_ptr
->name
);
1007 dlibs
= dlibs
->next
;
1011 if (opt_chroot
&& dir_name
)
1015 /* Search through all libraries. */
1019 struct dir_entry
*entry
;
1021 for (entry
= dir_entries
; entry
!= NULL
; entry
= entry
->next
)
1024 /* Free all allocated memory. */
1027 entry
= dir_entries
;
1028 dir_entries
= dir_entries
->next
;
1035 static void parse_conf_include (const char *config_file
, unsigned int lineno
,
1036 bool do_chroot
, const char *pattern
);
1038 /* Parse configuration file. */
1040 parse_conf (const char *filename
, bool do_chroot
)
1046 unsigned int lineno
;
1048 if (do_chroot
&& opt_chroot
)
1050 canon
= chroot_canon (opt_chroot
, filename
);
1052 file
= fopen (canon
, "r");
1059 file
= fopen (filename
, "r");
1064 error (0, errno
, _("\
1065 Warning: ignoring configuration file that cannot be opened: %s"),
1067 if (canon
!= filename
)
1068 free ((char *) canon
);
1072 /* No threads use this stream. */
1073 __fsetlocking (file
, FSETLOCKING_BYCALLER
);
1075 if (canon
!= filename
)
1076 free ((char *) canon
);
1081 ssize_t n
= getline (&line
, &len
, file
);
1086 if (line
[n
- 1] == '\n')
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. */
1096 while (isspace (*cp
))
1099 /* If the line is blank it is ignored. */
1103 if (!strncmp (cp
, "include", 7) && isblank (cp
[7]))
1107 while ((dir
= strsep (&cp
, " \t")) != NULL
)
1109 parse_conf_include (filename
, lineno
, do_chroot
, dir
);
1111 else if (!strncasecmp (cp
, "hwcap", 5) && isblank (cp
[5]))
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
)
1130 error (EXIT_FAILURE
, 0, _("%s:%u: bad syntax in hwcap line"),
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
);
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
]);
1154 error (0, 0, _("%s:%u: duplicate hwcap %lu %s"),
1155 filename
, lineno
, n
, name
);
1161 while (!feof_unlocked (file
));
1163 /* Free buffer and close file. */
1168 /* Handle one word in an `include' line, a glob pattern of additional
1169 config files to read. */
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"));
1179 if (pattern
[0] != '/' && strchr (config_file
, '/') != NULL
)
1181 if (asprintf (©
, "%s/%s", dirname (strdupa (config_file
)),
1183 error (EXIT_FAILURE
, 0, _("memory exhausted"));
1189 if (do_chroot
&& opt_chroot
)
1191 char *canon
= chroot_canon (opt_chroot
, pattern
);
1194 result
= glob64 (canon
, 0, NULL
, &gl
);
1198 result
= glob64 (pattern
, 0, NULL
, &gl
);
1203 for (size_t i
= 0; i
< gl
.gl_pathc
; ++i
)
1204 parse_conf (gl
.gl_pathv
[i
], false);
1215 error (0, errno
, _("%s:%u: cannot read directory %s"),
1216 config_file
, lineno
, pattern
);
1227 /* Honour LD_HWCAP_MASK. */
1231 char *mask
= getenv ("LD_HWCAP_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. */
1249 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
1251 /* Remaining arguments are additional directories if opt_manual_link
1253 if (remaining
!= argc
&& !opt_manual_link
)
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"),
1265 hwcap_extra
[63 - _DL_FIRST_EXTRA
] = "tls";
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] == '/')
1276 if (endp
== opt_chroot
)
1281 /* It is faster to use chroot if we can. */
1282 if (!chroot (opt_chroot
))
1285 error (EXIT_FAILURE
, errno
, _("Can't chdir to /"));
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
)
1304 char *p
= chroot_canon (opt_chroot
, cache_file
);
1306 error (EXIT_FAILURE
, errno
, _("Can't open cache file %s\n"),
1310 print_cache (cache_file
);
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
) : "/");
1325 error (EXIT_FAILURE
, errno
,
1326 _("Can't open cache file directory %s\n"),
1327 p
? cache_file
: "/");
1334 cache_file
= alloca (strlen (canon
) + strlen (p
) + 2);
1335 sprintf (cache_file
, "%s/%s", canon
, p
);
1339 if (opt_manual_link
)
1341 /* Link all given libraries manually. */
1344 for (i
= remaining
; i
< argc
; ++i
)
1345 manual_link (argv
[i
]);
1351 if (opt_build_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
;
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
);
1375 if (opt_build_cache
)
1377 save_cache (cache_file
);
1379 save_aux_cache (aux_cache_file
);