2 * ldconfig - update shared library symlinks
4 * usage: ldconfig [-DvqnNX] [-f conf] [-C cache] [-r root] dir ...
5 * ldconfig -l [-Dv] lib ...
7 * -D: debug mode, don't update links
8 * -v: verbose mode, print things as we go
9 * -q: quiet mode, don't print warnings
10 * -n: don't process standard directories
11 * -N: don't update the library cache
12 * -X: don't update the library links
13 * -l: library mode, manually link libraries
14 * -p: print the current library cache
15 * -f conf: use conf instead of /etc/ld.so.conf
16 * -C cache: use cache instead of /etc/ld.so.cache
17 * -r root: first, do a chroot to the indicated directory
18 * dir ...: directories to process
19 * lib ...: libraries to link
21 * Copyright 1994-2000 David Engel and Mitch D'Souza
23 * This program may be used for any purpose as long as this
24 * copyright notice is kept.
42 #define BUFFER_SIZE 4096
46 unsigned long a_info; /* Use macros N_MAGIC, etc for access */
47 unsigned a_text; /* length of text, in bytes */
48 unsigned a_data; /* length of data, in bytes */
49 unsigned a_bss; /* length of uninitialized data area for file, in bytes */
50 unsigned a_syms; /* length of symbol table data in file, in bytes */
51 unsigned a_entry; /* start address */
52 unsigned a_trsize; /* length of relocation info for text, in bytes */
53 unsigned a_drsize; /* length of relocation info for data, in bytes */
56 #if !defined (N_MAGIC)
57 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
59 /* Code indicating object file or impure executable. */
61 /* Code indicating pure executable. */
63 /* Code indicating demand-paged executable. */
65 /* This indicates a demand-paged executable with the header in the text.
66 The first page is unmapped to help trap NULL pointer references */
68 /* Code indicating core file. */
71 char *___strtok = NULL;
76 #define PATH_MAX _POSIX_PATH_MAX
81 #define N_MAGIC(exec) ((exec).a_magic & 0xffff)
85 #define EXIT_FATAL 128
88 int debug = 0; /* debug mode */
89 int verbose = 0; /* verbose mode */
90 int libmode = 0; /* library mode */
91 int nolinks = 0; /* don't update links */
92 int nocache = 0; /* don't build cache */
93 void cache_print(void);
94 void cache_write(void);
95 void cache_dolib(const char *dir, const char *so, int libtype);
96 #ifdef __LDSO_CACHE_SUPPORT__
97 char *conffile = LDSO_CONF; /* default conf file */
98 char *cachefile = LDSO_CACHE; /* default cache file */
107 struct needed_tab needed_tab[] = {
108 { "libc.so.0", LIB_ELF_LIBC0 },
109 { "libm.so.0", LIB_ELF_LIBC0 },
110 { "libdl.so.0", LIB_ELF_LIBC0 },
111 { "libc.so.5", LIB_ELF_LIBC5 },
112 { "libm.so.5", LIB_ELF_LIBC5 },
113 { "libdl.so.1", LIB_ELF_LIBC5 },
114 { "libc.so.6", LIB_ELF_LIBC6 },
115 { "libm.so.6", LIB_ELF_LIBC6 },
116 { "libdl.so.2", LIB_ELF_LIBC6 },
121 /* These two are used internally -- you shouldn't need to use them */
122 static void verror_msg(const char *s, va_list p)
125 fprintf(stderr, "%s: ", prog);
126 vfprintf(stderr, s, p);
129 static void warnx(const char *s, ...)
136 fprintf(stderr, "\n");
139 static void err(int errnum, const char *s, ...)
146 fprintf(stderr, "\n");
150 static void vperror_msg(const char *s, va_list p)
159 fprintf(stderr, "%s%s\n", s, strerror(err));
162 static void warn(const char *s, ...)
171 static void *xmalloc(size_t size)
174 if ((ptr = malloc(size)) == NULL)
175 err(EXIT_FATAL,"out of memory");
179 static char *xstrdup(const char *str)
182 if ((ptr = strdup(str)) == NULL)
183 err(EXIT_FATAL,"out of memory");
188 #undef __ELF_NATIVE_CLASS
190 #define readsonameXX readsoname32
191 #define __ELF_NATIVE_CLASS 32
192 #include "readsoname2.c"
194 #undef __ELF_NATIVE_CLASS
196 #define readsonameXX readsoname64
197 #define __ELF_NATIVE_CLASS 64
198 #include "readsoname2.c"
200 char *readsoname(char *name, FILE *infile, int expected_type,
201 int *type, int elfclass)
205 if (elfclass == ELFCLASS32)
206 res = readsoname32(name, infile, expected_type, type);
209 res = readsoname64(name, infile, expected_type, type);
220 /* If shared library, return a malloced copy of the soname and set the
221 type, else return NULL.
223 expected_type should be either LIB_ANY or one of the following:-
229 If the lib is ELF and we can not deduce the type the type will
230 be set based on expected_type.
232 If the expected, actual/deduced types missmatch we display a warning
233 and use the actual/deduced type.
235 char *is_shlib(const char *dir, const char *name, int *type,
236 int *islink, int expected_type)
244 char buff[BUFFER_SIZE];
246 /* see if name is of the form *.so* */
247 if (name[strlen(name)-1] != '~' && (cp = strstr(name, ".so")))
249 /* find the start of the Vminor part, if any */
250 if (cp[3] == '.' && (cp2 = strchr(cp + 4, '.')))
253 cp = cp + strlen(cp);
255 /* construct the full path name */
256 sprintf(buff, "%s%s%s", dir, (*dir && strcmp(dir, "/")) ?
259 /* first, make sure it's a regular file */
260 if (lstat(buff, &statbuf))
261 warn("skipping %s", buff);
262 else if (!S_ISREG(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode))
263 warnx("%s is not a regular file or symlink, skipping", buff);
266 /* is it a regular file or a symlink */
267 *islink = S_ISLNK(statbuf.st_mode);
269 /* then try opening it */
270 if (!(file = fopen(buff, "rb")))
271 warn("skipping %s", buff);
274 /* now make sure it's a shared library */
275 if (fread(&exec, sizeof exec, 1, file) < 1)
276 warnx("can't read header from %s, skipping", buff);
277 else if (N_MAGIC(exec) != ZMAGIC && N_MAGIC(exec) != QMAGIC)
279 elf_hdr = (ElfW(Ehdr) *) &exec;
280 if (elf_hdr->e_ident[0] != 0x7f ||
281 strncmp(&elf_hdr->e_ident[1], "ELF",3) != 0)
283 /* silently ignore linker scripts */
284 if (strncmp((char *)&exec, "/* GNU ld", 9) != 0)
285 warnx("%s is not a shared library, skipping", buff);
289 /* always call readsoname to update type */
290 if(expected_type == LIB_DLL) {
291 warnx("%s is not an a.out library, its ELF!\n", buff);
292 expected_type=LIB_ANY;
295 good = readsoname(buff, file, expected_type, type,
296 elf_hdr->e_ident[EI_CLASS]);
297 if (good == NULL || *islink)
301 good = xstrdup(name);
305 /* if the soname does not match the filename,
306 issue a warning, but only in debug mode. */
307 int len = strlen(good);
308 if (debug && (strncmp(good, name, len) != 0 ||
309 (name[len] != '\0' && name[len] != '.')))
310 warnx("%s has inconsistent soname (%s)", buff, good);
317 good = xstrdup(name);
320 good = xmalloc(cp - name + 1);
321 strncpy(good, name, cp - name);
322 good[cp - name] = '\0';
324 if(expected_type != LIB_ANY && expected_type != LIB_DLL)
326 warnx("%s is not an ELF library, its an a.out DLL!", buff);
327 expected_type=LIB_ANY;
340 /* update the symlink to new library */
341 void link_shlib(const char *dir, const char *file, const char *so)
344 char libname[BUFFER_SIZE];
345 char linkname[BUFFER_SIZE];
347 struct stat linkstat;
349 /* construct the full path names */
350 sprintf(libname, "%s/%s", dir, file);
351 sprintf(linkname, "%s/%s", dir, so);
353 /* see if a link already exists */
354 if (!stat(linkname, &linkstat))
356 /* now see if it's the one we want */
357 if (stat(libname, &libstat))
358 warn("can't stat %s", libname);
359 else if (libstat.st_dev == linkstat.st_dev &&
360 libstat.st_ino == linkstat.st_ino)
364 /* then update the link, if required */
365 if (change > 0 && !nolinks)
367 if (!lstat(linkname, &linkstat))
369 if (!S_ISLNK(linkstat.st_mode))
371 warnx("%s is not a symlink", linkname);
374 else if (remove(linkname))
376 warn("can't unlink %s", linkname);
382 if (symlink(file, linkname))
384 warn("can't link %s to %s", linkname, file);
390 /* some people like to know what we're doing */
392 printf("\t%s => %s%s\n", so, file,
393 change < 0 ? " (SKIPPED)" :
394 (change > 0 ? " (changed)" : ""));
399 /* figure out which library is greater */
400 int libcmp(char *p1, char *p2)
404 if (isdigit(*p1) && isdigit(*p2))
406 /* must compare this numerically */
408 v1 = strtoul(p1, &p1, 10);
409 v2 = strtoul(p2, &p2, 10);
413 else if (isdigit(*p1) && !isdigit(*p2))
415 else if (!isdigit(*p1) && isdigit(*p2))
428 char *so; /* soname of a library */
429 char *name; /* name of a library */
430 int libtype; /* type of a library */
431 int islink; /* is it a symlink */
432 struct lib *next; /* next library in list */
435 /* update all shared library links in a directory */
436 void scan_dir(const char *rawname)
441 char *so, *path, *path_n;
442 struct lib *lp, *libs = NULL;
443 int i, libtype, islink, expected_type = LIB_ANY;
445 /* We need a writable copy of this string */
446 path = strdup(rawname);
448 err(EXIT_FATAL, "Out of memory!\n");
450 /* Eliminate all double //s */
452 while((path_n=strstr(path_n, "//"))) {
454 memmove(path_n, path_n+1, i-1);
455 *(path_n + i - 1)='\0';
461 /* Check for an embedded expected type */
462 t=strrchr(name, '=');
465 *t++ = '\0'; /* Skip = char */
466 if(strcasecmp(t, "libc4") == 0)
468 expected_type = LIB_DLL;
472 if(strcasecmp(t, "libc5") == 0)
474 expected_type = LIB_ELF_LIBC5;
478 if(strcasecmp(t, "libc6") == 0)
480 expected_type = LIB_ELF_LIBC6;
484 if(strcasecmp(t, "libc0") == 0)
486 expected_type = LIB_ELF_LIBC0;
490 warnx("Unknown type field '%s' for dir '%s' - ignored\n", t, name);
491 expected_type = LIB_ANY;
499 /* let 'em know what's going on */
501 printf("%s:\n", name);
503 /* if we can't open it, we can't do anything */
504 if ((dir = opendir(name)) == NULL)
506 warn("skipping %s", name);
511 /* yes, we have to look at every single file */
512 while ((ent = readdir(dir)) != NULL)
514 /* if it's not a shared library, don't bother */
515 if ((so = is_shlib(name, ent->d_name, &libtype, &islink, expected_type)) == NULL)
518 /* have we already seen one with the same so name? */
519 for (lp = libs; lp; lp = lp->next)
521 if (strcmp(so, lp->so) == 0)
523 /* we have, which one do we want to use? */
524 if ((!islink && lp->islink) ||
525 (islink == lp->islink &&
526 libcmp(ent->d_name, lp->name) > 0))
528 /* let's use the new one */
530 lp->name = xstrdup(ent->d_name);
531 lp->libtype = libtype;
538 /* congratulations, you're the first one we've seen */
541 lp = xmalloc(sizeof *lp);
542 lp->so = xstrdup(so);
543 lp->name = xstrdup(ent->d_name);
544 lp->libtype = libtype;
553 /* don't need this any more */
556 /* now we have all the latest libs, update the links */
557 for (lp = libs; lp; lp = lp->next)
560 link_shlib(name, lp->name, lp->so);
562 cache_dolib(name, lp->so, lp->libtype);
565 /* always try to clean up after ourselves */
579 #ifndef __LDSO_CACHE_SUPPORT__
580 void cache_print(void)
582 printf("Library cache disabled\n");
584 void cache_dolib(const char *dir, const char *so, int libtype)
588 void cache_write(void)
593 /* return the list of system-specific directories */
594 char *get_extpath(void)
596 char *res = NULL, *cp;
600 if ((file = fopen(conffile, "r")) != NULL)
602 fstat(fileno(file), &stat);
603 res = xmalloc(stat.st_size + 1);
604 fread(res, 1, stat.st_size, file);
606 res[stat.st_size] = '\0';
608 /* convert comments fo spaces */
609 for (cp = res; *cp; /*nada*/) {
613 while (*cp && *cp != '\n');
623 typedef struct liblist
630 struct liblist *next;
633 static header_t magic = { LDSO_CACHE_MAGIC, LDSO_CACHE_VER, 0 };
634 static liblist_t *lib_head = NULL;
636 static int liblistcomp(liblist_t *x, liblist_t *y)
640 if ((res = libcmp(x->soname, y->soname)) == 0)
642 res = libcmp(strrchr(x->libname, '/') + 1,
643 strrchr(y->libname, '/') + 1);
649 void cache_dolib(const char *dir, const char *so, int libtype)
651 char fullpath[PATH_MAX];
652 liblist_t *new_lib, *cur_lib;
655 sprintf(fullpath, "%s/%s", dir, so);
656 new_lib = xmalloc(sizeof (liblist_t));
657 new_lib->flags = libtype;
658 new_lib->soname = xstrdup(so);
659 new_lib->libname = xstrdup(fullpath);
661 if (lib_head == NULL || liblistcomp(new_lib, lib_head) > 0)
663 new_lib->next = lib_head;
668 for (cur_lib = lib_head; cur_lib->next != NULL &&
669 liblistcomp(new_lib, cur_lib->next) <= 0;
670 cur_lib = cur_lib->next)
672 new_lib->next = cur_lib->next;
673 cur_lib->next = new_lib;
677 void cache_write(void)
681 char tempfile[BUFFER_SIZE];
687 sprintf(tempfile, "%s~", cachefile);
689 if (unlink(tempfile) && errno != ENOENT)
690 err(EXIT_FATAL,"can't unlink %s (%s)", tempfile, strerror(errno));
692 if ((cachefd = creat(tempfile, 0644)) < 0)
693 err(EXIT_FATAL,"can't create %s (%s)", tempfile, strerror(errno));
695 if (write(cachefd, &magic, sizeof (header_t)) != sizeof (header_t))
696 err(EXIT_FATAL,"can't write %s (%s)", tempfile, strerror(errno));
698 for (cur_lib = lib_head; cur_lib != NULL; cur_lib = cur_lib->next)
700 cur_lib->sooffset = stroffset;
701 stroffset += strlen(cur_lib->soname) + 1;
702 cur_lib->liboffset = stroffset;
703 stroffset += strlen(cur_lib->libname) + 1;
704 if (write(cachefd, cur_lib, sizeof (libentry_t)) !=
706 err(EXIT_FATAL,"can't write %s (%s)", tempfile, strerror(errno));
709 for (cur_lib = lib_head; cur_lib != NULL; cur_lib = cur_lib->next)
711 if (write(cachefd, cur_lib->soname, strlen(cur_lib->soname) + 1)
712 != strlen(cur_lib->soname) + 1)
713 err(EXIT_FATAL,"can't write %s (%s)", tempfile, strerror(errno));
714 if (write(cachefd, cur_lib->libname, strlen(cur_lib->libname) + 1)
715 != strlen(cur_lib->libname) + 1)
716 err(EXIT_FATAL,"can't write %s (%s)", tempfile, strerror(errno));
720 err(EXIT_FATAL,"can't close %s (%s)", tempfile, strerror(errno));
722 if (chmod(tempfile, 0644))
723 err(EXIT_FATAL,"can't chmod %s (%s)", tempfile, strerror(errno));
725 if (rename(tempfile, cachefile))
726 err(EXIT_FATAL,"can't rename %s (%s)", tempfile, strerror(errno));
729 void cache_print(void)
738 if (stat(cachefile, &st) || (fd = open(cachefile, O_RDONLY))<0)
739 err(EXIT_FATAL,"can't read %s (%s)", cachefile, strerror(errno));
740 if ((c = mmap(0,st.st_size, PROT_READ, MAP_SHARED ,fd, 0)) == (caddr_t)-1)
741 err(EXIT_FATAL,"can't map %s (%s)", cachefile, strerror(errno));
744 if (memcmp(((header_t *)c)->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN))
745 err(EXIT_FATAL,"%s cache corrupt", cachefile);
747 if (memcmp(((header_t *)c)->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN))
748 err(EXIT_FATAL,"wrong cache version - expected %s", LDSO_CACHE_VER);
750 header = (header_t *)c;
751 libent = (libentry_t *)(c + sizeof (header_t));
752 strs = (char *)&libent[header->nlibs];
754 printf("%d libs found in cache `%s' (version %s)\n",
755 header->nlibs, cachefile, LDSO_CACHE_VER);
757 for (fd = 0; fd < header->nlibs; fd++)
759 printf("\t%s ", strs + libent[fd].sooffset);
760 switch (libent[fd].flags & ~LIB_ELF64)
766 printf("(ELF%s)", libent[fd].flags & LIB_ELF64 ? "/64" : "");
769 printf("(libc0%s)",libent[fd].flags & LIB_ELF64 ? "/64" : "");
773 printf("(libc%d%s)", (libent[fd].flags & ~LIB_ELF64) + 3,
774 libent[fd].flags & LIB_ELF64 ? "/64" : "");
780 printf(" => %s\n", strs + libent[fd].liboffset);
783 munmap (c,st.st_size);
790 #ifdef __LDSO_CACHE_SUPPORT__
791 "ldconfig - updates symlinks and cache for shared libraries\n\n"
792 "Usage: ldconfig [-DvqnNX] [-f conf] [-C cache] [-r root] dir ...\n"
793 " ldconfig -l [-Dv] lib ...\n"
794 " ldconfig -p\n\nOptions:\n"
796 "ldconfig - updates symlinks for shared libraries\n\n"
797 "Usage: ldconfig [-DvqnX] [-r root] dir ...\n"
798 " ldconfig -l [-Dv] lib ...\n\nOptions:\n"
800 "\t-D:\t\tdebug mode, don't update links\n"
801 "\t-v:\t\tverbose mode, print things as we go\n"
802 "\t-q:\t\tquiet mode, don't print warnings\n"
803 "\t-n:\t\tdon't process standard directories\n"
804 "\t-N:\t\tdon't update the library cache\n"
805 "\t-X:\t\tdon't update the library links\n"
806 "\t-l:\t\tlibrary mode, manually link libraries\n"
807 "\t-p:\t\tprint the current library cache\n"
808 #ifdef __LDSO_CACHE_SUPPORT__
809 "\t-f conf :\tuse conf instead of %s\n"
810 "\t-C cache:\tuse cache instead of %s\n"
812 "\t-r root :\tfirst, do a chroot to the indicated directory\n"
813 "\tdir ... :\tdirectories to process\n"
814 #ifdef __LDSO_CACHE_SUPPORT__
815 "\tlib ... :\tlibraries to link\n\n",
816 LDSO_CONF, LDSO_CACHE
818 "\tlib ... :\tlibraries to link\n\n"
824 #define DIR_SEP ":, \t\n"
825 int main(int argc, char **argv)
831 char *chroot_dir = NULL;
833 #ifdef __LDSO_CACHE_SUPPORT__
840 while ((c = getopt(argc, argv, "DvqnNXlpf:C:r:")) != EOF)
844 debug = 1; /* debug mode */
850 verbose = 1; /* verbose mode */
854 verbose = -1; /* quiet mode */
857 nodefault = 1; /* no default dirs */
861 nocache = 1; /* don't build cache */
864 nolinks = 1; /* don't update links */
867 libmode = 1; /* library mode */
870 printcache = 1; /* print cache */
873 #ifdef __LDSO_CACHE_SUPPORT__
874 conffile = optarg; /* alternate conf file */
878 #ifdef __LDSO_CACHE_SUPPORT__
879 cachefile = optarg; /* alternate cache file */
889 /* THE REST OF THESE ARE UNDOCUMENTED AND MAY BE REMOVED
890 IN FUTURE VERSIONS. */
893 if (chroot_dir && *chroot_dir) {
894 if (chroot(chroot_dir) < 0)
895 err(EXIT_FATAL,"couldn't chroot to %s (%s)", chroot_dir, strerror(errno));
897 err(EXIT_FATAL,"couldn't chdir to / (%s)", strerror(errno));
900 /* allow me to introduce myself, hi, my name is ... */
902 printf("%s: uClibc version\n", argv[0]);
906 /* print the cache -- don't you trust me? */
912 /* so you want to do things manually, eh? */
914 /* ok, if you're so smart, which libraries do we link? */
915 for (i = optind; i < argc; i++)
917 /* split into directory and file parts */
918 if (!(cp = strrchr(argv[i], '/')))
920 dir = "."; /* no dir, only a filename */
926 dir = "/"; /* file in root directory */
929 *cp++ = '\0'; /* neither of the above */
932 /* we'd better do a little bit of checking */
933 if ((so = is_shlib(dir, cp, &libtype, &islink, LIB_ANY)) == NULL)
934 err(EXIT_FATAL,"%s%s%s is not a shared library", dir,
935 (*dir && strcmp(dir, "/")) ? "/" : "", cp);
937 /* so far, so good, maybe he knows what he's doing */
938 link_shlib(dir, cp, so);
943 /* the lazy bum want's us to do all the work for him */
945 /* don't cache dirs on the command line */
946 int nocache_save = nocache;
949 /* OK, which directories should we do? */
950 for (i = optind; i < argc; i++)
953 /* restore the desired caching state */
954 nocache = nocache_save;
956 /* look ma, no defaults */
959 scan_dir(UCLIBC_RUNTIME_PREFIX "lib");
960 scan_dir(UCLIBC_RUNTIME_PREFIX "usr/lib");
961 #ifndef __LDSO_CACHE_SUPPORT__
962 scan_dir(UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib");
964 /* I guess the defaults aren't good enough */
965 if ((extpath = get_extpath()))
967 for (cp = strtok(extpath, DIR_SEP); cp; cp = strtok(NULL, DIR_SEP)) {
968 /* strip trailing slashes */
969 int len = strlen(cp);
971 while (cp[--len] == '/' && len)
973 /* we do the redundancy check only if cache usage is enabled */
974 if (strcmp(UCLIBC_RUNTIME_PREFIX "lib", cp) == 0 ||
975 strcmp(UCLIBC_RUNTIME_PREFIX "usr/lib", cp) == 0) {
977 warnx("Remove `%s' from `%s'\n", cp, LDSO_CONF);