diff options
Diffstat (limited to 'sbin')
-rw-r--r-- | sbin/ldconfig/Makefile | 4 | ||||
-rw-r--r-- | sbin/ldconfig/elfhints.c | 280 | ||||
-rw-r--r-- | sbin/ldconfig/ldconfig.8 | 72 | ||||
-rw-r--r-- | sbin/ldconfig/ldconfig.c | 101 | ||||
-rw-r--r-- | sbin/ldconfig/ldconfig.h | 39 |
5 files changed, 465 insertions, 31 deletions
diff --git a/sbin/ldconfig/Makefile b/sbin/ldconfig/Makefile index 840fd7f..f902e92 100644 --- a/sbin/ldconfig/Makefile +++ b/sbin/ldconfig/Makefile @@ -1,7 +1,7 @@ -# $Id: Makefile,v 1.12 1998/06/01 13:58:19 peter Exp $ +# $Id: Makefile,v 1.13 1998/06/12 10:43:18 peter Exp $ PROG= ldconfig -SRCS= ldconfig.c shlib.c support.c +SRCS= elfhints.c ldconfig.c shlib.c support.c LDDIR?= ${.CURDIR}/../../libexec/rtld-aout CFLAGS+=-I${LDDIR} -DFREEBSD_AOUT MAN8= ldconfig.8 diff --git a/sbin/ldconfig/elfhints.c b/sbin/ldconfig/elfhints.c new file mode 100644 index 0000000..f61d849 --- /dev/null +++ b/sbin/ldconfig/elfhints.c @@ -0,0 +1,280 @@ +/*- + * Copyright (c) 1998 John D. Polstra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $Id$ + */ + +#include <sys/param.h> +#include <sys/mman.h> +#include <sys/stat.h> + +#include <ctype.h> +#include <dirent.h> +#include <elf.h> +#include <err.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "ldconfig.h" + +#define MAXDIRS 1024 /* Maximum directories in path */ +#define MAXFILESIZE (16*1024) /* Maximum hints file size */ + +static void add_dir(const char *, const char *); +static void read_dirs_from_file(const char *, const char *); +static void read_elf_hints(const char *, int); +static void write_elf_hints(const char *); + +static const char *dirs[MAXDIRS]; +static int ndirs; + +static void +add_dir(const char *hintsfile, const char *name) +{ + int i; + + for (i = 0; i < ndirs; i++) + if (strcmp(dirs[i], name) == 0) + return; + if (ndirs >= MAXDIRS) + errx(1, "\"%s\": Too many directories in path", hintsfile); + dirs[ndirs++] = name; +} + +void +list_elf_hints(const char *hintsfile) +{ + int i; + int nlibs; + + read_elf_hints(hintsfile, 1); + printf("%s:\n", hintsfile); + printf("\tsearch directories:"); + for (i = 0; i < ndirs; i++) + printf("%c%s", i == 0 ? ' ' : ':', dirs[i]); + printf("\n"); + + nlibs = 0; + for (i = 0; i < ndirs; i++) { + DIR *dirp; + struct dirent *dp; + + if ((dirp = opendir(dirs[i])) == NULL) + continue; + while ((dp = readdir(dirp)) != NULL) { + int len; + int namelen; + const char *name; + const char *vers; + + /* Name can't be shorter than "libx.so.0" */ + if ((len = strlen(dp->d_name)) < 9 || + strncmp(dp->d_name, "lib", 3) != 0) + continue; + name = dp->d_name + 3; + vers = dp->d_name + len; + while (vers > dp->d_name && isdigit(*(vers-1))) + vers--; + if (vers == dp->d_name + len) + continue; + if (vers < dp->d_name + 4 || + strncmp(vers - 4, ".so.", 4) != 0) + continue; + + /* We have a valid shared library name. */ + namelen = (vers - 4) - name; + printf("\t%d:-l%.*s.%s => %s/%s\n", nlibs, + namelen, name, vers, dirs[i], dp->d_name); + nlibs++; + } + closedir(dirp); + } +} + +static void +read_dirs_from_file(const char *hintsfile, const char *listfile) +{ + FILE *fp; + char buf[MAXPATHLEN]; + int linenum; + + if ((fp = fopen(listfile, "r")) == NULL) + err(1, "%s", listfile); + + linenum = 0; + while (fgets(buf, sizeof buf, fp) != NULL) { + char *cp, *sp; + + linenum++; + cp = buf; + /* Skip leading white space. */ + while (isspace(*cp)) + cp++; + if (*cp == '#' || *cp == '\0') + continue; + sp = cp; + /* Advance over the directory name. */ + while (!isspace(*cp) && *cp != '\0') + cp++; + /* Terminate the string and skip trailing white space. */ + if (*cp != '\0') { + *cp++ = '\0'; + while (isspace(*cp)) + cp++; + } + /* Now we had better be at the end of the line. */ + if (*cp != '\0') + warnx("%s:%d: trailing characters ignored", + listfile, linenum); + + if ((sp = strdup(sp)) == NULL) + errx(1, "Out of memory"); + add_dir(hintsfile, sp); + } + + fclose(fp); +} + +static void +read_elf_hints(const char *hintsfile, int must_exist) +{ + int fd; + struct stat s; + void *mapbase; + struct elfhints_hdr *hdr; + char *strtab; + char *dirlist; + char *p; + + if ((fd = open(hintsfile, O_RDONLY)) == -1) { + if (errno == ENOENT && !must_exist) + return; + err(1, "Cannot open \"%s\"", hintsfile); + } + if (fstat(fd, &s) == -1) + err(1, "Cannot stat \"%s\"", hintsfile); + if (s.st_size > MAXFILESIZE) + errx(1, "\"%s\" is unreasonably large", hintsfile); + /* + * We use a read-write, private mapping so that we can null-terminate + * some strings in it without affecting the underlying file. + */ + mapbase = mmap(NULL, s.st_size, PROT_READ|PROT_WRITE, + MAP_PRIVATE, fd, 0); + if (mapbase == MAP_FAILED) + err(1, "Cannot mmap \"%s\"", hintsfile); + close(fd); + + hdr = (struct elfhints_hdr *)mapbase; + if (hdr->magic != ELFHINTS_MAGIC) + errx(1, "\"%s\": invalid file format", hintsfile); + if (hdr->version != 1) + errx(1, "\"%s\": unrecognized file version (%d)", hintsfile, + hdr->version); + + strtab = (char *)mapbase + hdr->strtab; + dirlist = strtab + hdr->dirlist; + + if (*dirlist != '\0') + while ((p = strsep(&dirlist, ":")) != NULL) + add_dir(hintsfile, p); +} + +void +update_elf_hints(const char *hintsfile, int argc, char **argv, int merge) +{ + int i; + + if (merge) + read_elf_hints(hintsfile, 0); + for (i = 0; i < argc; i++) { + struct stat s; + + if (stat(argv[i], &s) == -1) + err(1, "%s", argv[i]); + if (S_ISREG(s.st_mode)) + read_dirs_from_file(hintsfile, argv[i]); + else + add_dir(hintsfile, argv[i]); + } + write_elf_hints(hintsfile); +} + +static void +write_elf_hints(const char *hintsfile) +{ + struct elfhints_hdr hdr; + char *tempname; + int fd; + FILE *fp; + int i; + + if (asprintf(&tempname, "%s.XXXXXX", hintsfile) == -1) + errx(1, "Out of memory"); + if ((fd = mkstemp(tempname)) == -1) + err(1, "mkstemp(%s)", tempname); + if (fchmod(fd, 0444) == -1) + err(1, "fchmod(%s)", tempname); + if ((fp = fdopen(fd, "wb")) == NULL) + err(1, "fdopen(%s)", tempname); + + hdr.magic = ELFHINTS_MAGIC; + hdr.version = 1; + hdr.strtab = sizeof hdr; + hdr.strsize = 0; + hdr.dirlist = 0; + memset(hdr.spare, 0, sizeof hdr.spare); + + /* Count up the size of the string table. */ + if (ndirs > 0) { + hdr.strsize += strlen(dirs[0]); + for (i = 1; i < ndirs; i++) + hdr.strsize += 1 + strlen(dirs[i]); + } + hdr.dirlistlen = hdr.strsize; + hdr.strsize++; /* For the null terminator */ + + /* Write the header. */ + if (fwrite(&hdr, 1, sizeof hdr, fp) != sizeof hdr) + err(1, "%s: write error", tempname); + /* Write the strings. */ + if (ndirs > 0) { + if (fputs(dirs[0], fp) == EOF) + err(1, "%s: write error", tempname); + for (i = 1; i < ndirs; i++) + if (fprintf(fp, ":%s", dirs[i]) < 0) + err(1, "%s: write error", tempname); + } + if (putc('\0', fp) == EOF || fclose(fp) == EOF) + err(1, "%s: write error", tempname); + + if (rename(tempname, hintsfile) == -1) + err(1, "rename %s to %s", tempname, hintsfile); + free(tempname); +} diff --git a/sbin/ldconfig/ldconfig.8 b/sbin/ldconfig/ldconfig.8 index 1f79ce8..9c4fd95 100644 --- a/sbin/ldconfig/ldconfig.8 +++ b/sbin/ldconfig/ldconfig.8 @@ -27,7 +27,7 @@ .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. .\" -.\" $Id: ldconfig.8,v 1.14 1997/08/22 04:42:12 peter Exp $ +.\" $Id: ldconfig.8,v 1.15 1998/01/01 02:31:47 alex Exp $ .\" .Dd October 3, 1993 .Dt LDCONFIG 8 @@ -37,6 +37,7 @@ .Nd configure the shared library cache .Sh SYNOPSIS .Nm ldconfig +.Op Fl aout | Fl elf .Op Fl Rmrsv .Op Fl f Ar hints_file .Op Ar directory | file Ar ... @@ -44,25 +45,21 @@ .Nm is used to prepare a set of .Dq hints -for use by the run-time linker -.Xr ld.so 1 +for use by the dynamic linker to facilitate quick lookup of shared libraries available in multiple directories. It scans a set of built-in system directories and any .Ar directories -specified on the command line (in the given order) looking for shared -libraries and stores the results in the file -.Pa /var/run/ld.so.hints -to forestall the overhead that would otherwise result from the -directory search operations -.Xr ld.so 1 -would have to perform to load the required shared libraries. +specified on the command line (in the given order) looking for +shared libraries and stores the results in a system file to forestall +the overhead that would otherwise result from the directory search +operations the dynamic linker would have to perform to load the +required shared libraries. .Pp Files named on the command line are expected to contain directories to scan for shared libraries. Each directory's pathname must start on a new line. Blank lines and lines starting with the comment character .Ql \&# -are ignored. A standard name for this file is -.Xr /etc/ld.so.conf. +are ignored. .Pp The shared libraries so found will be automatically available for loading if needed by the program being prepared for execution. This obviates the need @@ -77,7 +74,7 @@ directories where shared libraries might be found. is a .Sq \: separated list of directory paths which are searched by -.Xr ld.so 1 +the dynamic linker when it needs to load a shared library. It can be viewed as the run-time equivalent of the .Fl L @@ -90,13 +87,16 @@ is typically run as part of the boot sequence. The following options recognized by .Nm ldconfig: .Bl -tag -width indent +.It Fl aout +Generate the hints for a.out format shared libraries. +.It Fl elf +Generate the hints for ELF format shared libraries. .It Fl R Rescan the previously configured directories. This opens the previous hints file and fetches the directory list from the header. Any additional pathnames on the command line are also processed. .It Fl f Ar hints_file -Read and/or update the specified hints file, instead of -.Pa /var/run/ld.so.hints . +Read and/or update the specified hints file, instead of the standard file. This option is provided primarily for testing. .It Fl m Instead of replacing the contents of the hints file @@ -121,7 +121,7 @@ Special care must be taken when loading shared libraries into the address space of .Ev set-user-Id programs. Whenever such a program is run, -.Nm ld.so +the dynamic linker will only load shared libraries from the hints file. In particular, the .Ev LD_LIBRARY_PATH @@ -131,12 +131,44 @@ specify the trusted collection of directories from which shared objects can be safely loaded. It is presumed that the set of directories specified to .Nm ldconfig are under control of the system's administrator. +.Sh ENVIRONMENT +.Bl -tag -width OBJFORMATxxx -compact +.It Ev OBJFORMAT +Overrides +.Pa /etc/objformat +(see below) to determine whether +.Fl aout +or +.Fl elf +is the default. If set, its value should be either +.Ql aout +or +.Ql elf . +.El .Sh FILES -.Bl -tag -width /var/run/ld.so.hintsxxx -compact +.Bl -tag -width /var/run/ld-elf.so.hintsxxx -compact .It Pa /var/run/ld.so.hints -Default -.Dq hints -file. +Standard hints file for the a.out dynamic linker. +.It Pa /var/run/ld-elf.so.hints +Standard hints file for the ELF dynamic linker. +.It Pa /etc/ld.so.conf +Conventional configuration file containing directory names for +invocations with +.Fl aout . +.It Pa /etc/ld-elf.so.conf +Conventional configuration file containing directory names for +invocations with +.Fl elf . +.It Pa /etc/objformat +Determines whether +.Fl aout +or +.Fl elf +is the default. If present, it must consist of a single line +containing either +.Ql OBJFORMAT=aout +or +.Ql OBJFORMAT=elf . .Sh SEE ALSO .Xr ld 1 , .Xr link 5 diff --git a/sbin/ldconfig/ldconfig.c b/sbin/ldconfig/ldconfig.c index 197cf78..64d4001 100644 --- a/sbin/ldconfig/ldconfig.c +++ b/sbin/ldconfig/ldconfig.c @@ -30,7 +30,7 @@ #ifndef lint static const char rcsid[] = - "$Id: ldconfig.c,v 1.23 1998/07/06 07:02:26 charnier Exp $"; + "$Id: ldconfig.c,v 1.24 1998/08/02 16:06:33 bde Exp $"; #endif /* not lint */ #include <sys/param.h> @@ -40,6 +40,7 @@ static const char rcsid[] = #include <a.out.h> #include <ctype.h> #include <dirent.h> +#include <elf.h> #include <err.h> #include <errno.h> #include <fcntl.h> @@ -49,6 +50,7 @@ static const char rcsid[] = #include <string.h> #include <unistd.h> +#include "ldconfig.h" #include "shlib.h" #include "support.h" @@ -56,17 +58,29 @@ static const char rcsid[] = /* test */ #undef _PATH_LD_HINTS #define _PATH_LD_HINTS "./ld.so.hints" +#undef _PATH_ELF_HINTS +#define _PATH_ELF_HINTS "./ld-elf.so.hints" #endif #undef major #undef minor +enum obj_format { Unknown, Aout, Elf }; + +#ifndef DEFAULT_FORMAT +#ifdef __ELF__ +#define DEFAULT_FORMAT Elf +#else +#define DEFAULT_FORMAT Aout +#endif +#endif + static int verbose; static int nostd; static int justread; static int merge; static int rescan; -static char *hints_file = _PATH_LD_HINTS; +static char *hints_file; struct shlib_list { /* Internal list of shared libraries found */ @@ -82,13 +96,14 @@ struct shlib_list { static struct shlib_list *shlib_head = NULL, **shlib_tail = &shlib_head; static char *dir_list; -static void enter __P((char *, char *, char *, int *, int)); -static int dodir __P((char *, int)); -int dofile __P((char *, int)); -static int buildhints __P((void)); -static int readhints __P((void)); -static void listhints __P((void)); -static void usage __P((void)); +static int buildhints __P((void)); +static int dodir __P((char *, int)); +int dofile __P((char *, int)); +static void enter __P((char *, char *, char *, int *, int)); +static enum obj_format getobjfmt __P((int *, char **)); +static void listhints __P((void)); +static int readhints __P((void)); +static void usage __P((void)); int main(argc, argv) @@ -97,7 +112,10 @@ char *argv[]; { int i, c; int rval = 0; + enum obj_format fmt; + fmt = getobjfmt(&argc, argv); + hints_file = fmt == Aout ? _PATH_LD_HINTS : _PATH_ELF_HINTS; while ((c = getopt(argc, argv, "Rf:mrsv")) != -1) { switch (c) { case 'R': @@ -124,6 +142,15 @@ char *argv[]; } } + if (fmt == Elf) { + if (justread) + list_elf_hints(hints_file); + else + update_elf_hints(hints_file, argc - optind, + argv + optind, merge || rescan); + return 0; + } + dir_list = strdup(""); if (justread || merge || rescan) { @@ -179,6 +206,62 @@ char *argv[]; return rval; } +static enum obj_format +getobjfmt(argcp, argv) + int *argcp; + char **argv; +{ + enum obj_format fmt; + char **src, **dst; + const char *env; + FILE *fp; + + fmt = Unknown; + + /* Scan for "-aout" or "-elf" arguments, deleting them as we go. */ + for (dst = src = argv + 1; *src != NULL; src++) { + if (strcmp(*src, "-aout") == 0) + fmt = Aout; + else if (strcmp(*src, "-elf") == 0) + fmt = Elf; + else + *dst++ = *src; + } + *dst = NULL; + *argcp -= src - dst; + if (fmt != Unknown) + return fmt; + + /* Check the OBJFORMAT environment variable. */ + if ((env = getenv("OBJFORMAT")) != NULL) { + if (strcmp(env, "aout") == 0) + return Aout; + else if (strcmp(env, "elf") == 0) + return Elf; + } + + /* Take a look at "/etc/objformat". */ + if ((fp = fopen("/etc/objformat", "r")) != NULL) { + char buf[1024]; + + while (fgets(buf, sizeof buf, fp) != NULL) { + if (strcmp(buf, "OBJFORMAT=aout\n") == 0) + fmt = Aout; + else if (strcmp(buf, "OBJFORMAT=elf\n") == 0) + fmt = Elf; + else + warnx("Unrecognized line in /etc/objformat: %s", + buf); + } + fclose(fp); + } + if (fmt != Unknown) + return fmt; + + /* As a last resort, use the compiled in default. */ + return DEFAULT_FORMAT; +} + static void usage() { diff --git a/sbin/ldconfig/ldconfig.h b/sbin/ldconfig/ldconfig.h new file mode 100644 index 0000000..54c0d86 --- /dev/null +++ b/sbin/ldconfig/ldconfig.h @@ -0,0 +1,39 @@ +/*- + * Copyright (c) 1998 John D. Polstra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $Id$ + */ + +#ifndef LDCONFIG_H +#define LDCONFIG_H 1 + +#include <sys/cdefs.h> + +__BEGIN_DECLS +void list_elf_hints __P((const char *)); +void update_elf_hints __P((const char *, int, char **, int)); +__END_DECLS + +#endif |