diff options
author | mdodd <mdodd@FreeBSD.org> | 2003-04-07 16:21:26 +0000 |
---|---|---|
committer | mdodd <mdodd@FreeBSD.org> | 2003-04-07 16:21:26 +0000 |
commit | 5ce5797f854a3c9afeef6e3aa4c3c251b63c3d24 (patch) | |
tree | 36bdb2349a03bc43a769c15fd3ddd1063c1bf86b /libexec | |
parent | eff9d3d98df90c348751cebe8fd1aeaa8714e244 (diff) | |
download | FreeBSD-src-5ce5797f854a3c9afeef6e3aa4c3c251b63c3d24.zip FreeBSD-src-5ce5797f854a3c9afeef6e3aa4c3c251b63c3d24.tar.gz |
Dynamic object dependency mapping: libmap.
This is an optional feature, disabled by default.
This will be useful to people testing the various POSIX threading
libraries under -CURRENT but can easily serve other needs.
Diffstat (limited to 'libexec')
-rw-r--r-- | libexec/rtld-elf/Makefile | 9 | ||||
-rw-r--r-- | libexec/rtld-elf/libmap.c | 199 | ||||
-rw-r--r-- | libexec/rtld-elf/libmap.h | 7 | ||||
-rw-r--r-- | libexec/rtld-elf/rtld.1 | 2 | ||||
-rw-r--r-- | libexec/rtld-elf/rtld.c | 26 |
5 files changed, 238 insertions, 5 deletions
diff --git a/libexec/rtld-elf/Makefile b/libexec/rtld-elf/Makefile index cf40688..ff03080 100644 --- a/libexec/rtld-elf/Makefile +++ b/libexec/rtld-elf/Makefile @@ -10,6 +10,15 @@ INSTALLFLAGS= -fschg -C -b MLINKS= rtld.1 ld-elf.so.1.1 \ rtld.1 ld.so.1 +# +# To enable the libmap.conf functionality please +# add 'WITH_LIBMAP=yes' to /etc/make.conf, recompile +# and reinstall rtld-elf. +.ifdef WITH_LIBMAP +CFLAGS+= -DWITH_LIBMAP +SRCS+= libmap.c +.endif + .if exists(${.CURDIR}/${MACHINE_ARCH}/Makefile.inc) .include "${.CURDIR}/${MACHINE_ARCH}/Makefile.inc" .endif diff --git a/libexec/rtld-elf/libmap.c b/libexec/rtld-elf/libmap.c new file mode 100644 index 0000000..d4654b4 --- /dev/null +++ b/libexec/rtld-elf/libmap.c @@ -0,0 +1,199 @@ +/* + * $FreeBSD$ + */ + +#include <stdio.h> +#include <ctype.h> +#include <string.h> +#include <stdlib.h> +#include <sys/queue.h> +#include <sys/param.h> + +#ifndef _PATH_LIBMAP_CONF +#define _PATH_LIBMAP_CONF "/etc/libmap.conf" +#endif + +TAILQ_HEAD(lm_list, lm); +struct lm { + char *f; + char *t; + + TAILQ_ENTRY(lm) lm_link; +}; + +TAILQ_HEAD(lmp_list, lmp) lmp_head = TAILQ_HEAD_INITIALIZER(lmp_head); +struct lmp { + char *p; + struct lm_list lml; + TAILQ_ENTRY(lmp) lmp_link; +}; + +static void lm_add (char *, char *, char *); +static void lm_free (struct lm_list *); +static char * lml_find (struct lm_list *, const char *); +static struct lm_list * lmp_find (const char *); +static struct lm_list * lmp_init (char *); + +void +lm_init (void) +{ + FILE *fp; + char *cp; + char *f, *t, *p; + char prog[MAXPATHLEN]; + char line[MAXPATHLEN + 2]; + + TAILQ_INIT(&lmp_head); + + if ((fp = fopen(_PATH_LIBMAP_CONF, "r")) == NULL) + return; + + p = NULL; + while ((cp = fgets(line, MAXPATHLEN + 1, fp)) != NULL) { + t = f = NULL; + /* Skip over leading space */ + while (!isalpha(*cp) && + *cp != '#' && *cp != '\0' && *cp != '[') cp++; + /* Found a comment or EOL */ + if (*cp == '#' || *cp == '\0') + continue; + /* Found a costraint selector */ + if (*cp == '[') { + cp++; + /* Skip leading space */ + while (isspace(*cp) && + *cp != '#' && *cp != '\0' && *cp != ']') cp++; + /* Found comment, EOL or end of selector */ + if (*cp == '#' || *cp == '\0' || *cp == ']') + continue; + p = cp; + /* Skip to end of word */ + while (!isspace(*cp) && + *cp != '#' && *cp != '\0' && *cp != ']') cp++; + *cp++ = '\0'; + bzero(prog, MAXPATHLEN); + strncpy(prog, p, strlen(p)); + p = prog; + continue; + } + f = cp; + while (!isspace(*cp) && *cp != '#' && *cp != '\0') cp++; + *cp++ = '\0'; + while (isspace(*cp) && *cp != '#' && *cp != '\0') cp++; + t = cp; + while (!isspace(*cp) && *cp != '#' && *cp != '\0') cp++; + *cp++ = '\0'; + + lm_add(p, strdup(f), strdup(t)); + bzero(line, sizeof(line)); + } + (void)fclose(fp); + return; +} + +static void +lm_free (struct lm_list *lml) +{ + struct lm *lm; + + while (!TAILQ_EMPTY(lml)) { + lm = TAILQ_FIRST(lml); + TAILQ_REMOVE(lml, lm, lm_link); + free(lm->f); + free(lm->t); + free(lm); + } + return; +} + +void +lm_fini (void) +{ + struct lmp *lmp; + + while (!TAILQ_EMPTY(&lmp_head)) { + lmp = TAILQ_FIRST(&lmp_head); + TAILQ_REMOVE(&lmp_head, lmp, lmp_link); + free(lmp->p); + lm_free(&lmp->lml); + free(lmp); + } + return; +} + +static void +lm_add (char *p, char *f, char *t) +{ + struct lm_list *lml; + struct lm *lm; + + if (p == NULL) + p = "$DEFAULT$"; + +#if 0 + printf("%s(\"%s\", \"%s\", \"%s\")\n", __func__, p, f, t); +#endif + + if ((lml = lmp_find(p)) == NULL) + lml = lmp_init(strdup(p)); + + lm = malloc(sizeof(struct lm)); + lm->f = f; + lm->t = t; + TAILQ_INSERT_HEAD(lml, lm, lm_link); +} + +char * +lm_find (const char *p, const char *f) +{ + struct lm_list *lml; + char *t; + + if (p != NULL && (lml = lmp_find(p)) != NULL) { + t = lml_find(lml, f); + if (t != NULL) + return (t); + } + lml = lmp_find("$DEFAULT$"); + if (lml != NULL) + return (lml_find(lml, f)); + else + return (NULL); +} + +static char * +lml_find (struct lm_list *lmh, const char *f) +{ + struct lm *lm; + + TAILQ_FOREACH(lm, lmh, lm_link) + if ((strncmp(f, lm->f, strlen(lm->f)) == 0) && + (strlen(f) == strlen(lm->f))) + return (lm->t); + return NULL; +} + +static struct lm_list * +lmp_find (const char *n) +{ + struct lmp *lmp; + + TAILQ_FOREACH(lmp, &lmp_head, lmp_link) + if ((strncmp(n, lmp->p, strlen(lmp->p)) == 0) && + (strlen(n) == strlen(lmp->p))) + return (&lmp->lml); + return (NULL); +} + +static struct lm_list * +lmp_init (char *n) +{ + struct lmp *lmp; + + lmp = malloc(sizeof(struct lmp)); + lmp->p = n; + TAILQ_INIT(&lmp->lml); + TAILQ_INSERT_HEAD(&lmp_head, lmp, lmp_link); + + return (&lmp->lml); +} diff --git a/libexec/rtld-elf/libmap.h b/libexec/rtld-elf/libmap.h new file mode 100644 index 0000000..8228ef8 --- /dev/null +++ b/libexec/rtld-elf/libmap.h @@ -0,0 +1,7 @@ +/* + * $FreeBSD$ + */ + +void lm_init (void); +void lm_fini (void); +char * lm_find (const char *, const char *); diff --git a/libexec/rtld-elf/rtld.1 b/libexec/rtld-elf/rtld.1 index 472c572..3916922 100644 --- a/libexec/rtld-elf/rtld.1 +++ b/libexec/rtld-elf/rtld.1 @@ -157,6 +157,8 @@ are recognized and have their usual meaning. .Sh FILES .Bl -tag -width indent .It Pa /var/run/ld-elf.so.hints +.It Pa /etc/libmap.conf +The libmap configuration file. .El .Sh SEE ALSO .Xr ld 1 , diff --git a/libexec/rtld-elf/rtld.c b/libexec/rtld-elf/rtld.c index 2016ce0..2bd6584 100644 --- a/libexec/rtld-elf/rtld.c +++ b/libexec/rtld-elf/rtld.c @@ -52,6 +52,9 @@ #include "debug.h" #include "rtld.h" +#ifdef WITH_LIBMAP +#include "libmap.h" +#endif #define END_SYM "_end" #define PATH_RTLD "/usr/libexec/ld-elf.so.1" @@ -366,6 +369,10 @@ _rtld(Elf_Addr *sp, func_ptr_type *exit_proc, Obj_Entry **objp) sym_zero.st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE); sym_zero.st_shndx = SHN_UNDEF; +#ifdef WITH_LIBMAP + lm_init(); +#endif + dbg("loading LD_PRELOAD libraries"); if (load_preload_objects() == -1) die(); @@ -802,19 +809,25 @@ elf_hash(const char *name) * /usr/lib */ static char * -find_library(const char *name, const Obj_Entry *refobj) +find_library(const char *xname, const Obj_Entry *refobj) { char *pathname; + char *name; - if (strchr(name, '/') != NULL) { /* Hard coded pathname */ - if (name[0] != '/' && !trust) { + if (strchr(xname, '/') != NULL) { /* Hard coded pathname */ + if (xname[0] != '/' && !trust) { _rtld_error("Absolute pathname required for shared object \"%s\"", - name); + xname); return NULL; } - return xstrdup(name); + return xstrdup(xname); } +#ifdef WITH_LIBMAP + if ((name = lm_find(refobj->path, xname)) == NULL) +#endif + name = (char *)xname; + dbg(" Searching for \"%s\"", name); if ((pathname = search_library_path(name, ld_library_path)) != NULL || @@ -1468,6 +1481,9 @@ rtld_exit(void) obj->refcount = 0; objlist_call_fini(&list_fini); /* No need to remove the items from the list, since we are exiting. */ +#ifdef WITH_LIBMAP + lm_fini(); +#endif } static void * |