20 #include "rtld_libc.h"
22 TAILQ_HEAD(lm_list, lm);
26 TAILQ_ENTRY(lm) lm_link;
29 static TAILQ_HEAD(lmp_list, lmp) lmp_head = TAILQ_HEAD_INITIALIZER(lmp_head);
32 enum { T_EXACT=0, T_BASENAME, T_DIRECTORY } type;
34 TAILQ_ENTRY(lmp) lmp_link;
37 static TAILQ_HEAD(lmc_list, lmc) lmc_head = TAILQ_HEAD_INITIALIZER(lmc_head);
42 TAILQ_ENTRY(lmc) next;
47 static void lmc_parse(char *, size_t);
48 static void lmc_parse_file(const char *);
49 static void lmc_parse_dir(const char *);
50 static void lm_add(const char *, const char *, const char *);
51 static void lm_free(struct lm_list *);
52 static char *lml_find(struct lm_list *, const char *);
53 static struct lm_list *lmp_find(const char *);
54 static struct lm_list *lmp_init(char *);
55 static const char *quickbasename(const char *);
57 #define iseol(c) (((c) == '#') || ((c) == '\0') || \
58 ((c) == '\n') || ((c) == '\r'))
61 * Do not use ctype.h macros, which rely on working TLS. It is
62 * too early to have thread-local variables functional.
64 #define rtld_isspace(c) ((c) == ' ' || (c) == '\t')
67 lm_init(char *libmap_override)
71 dbg("lm_init(\"%s\")", libmap_override);
72 TAILQ_INIT(&lmp_head);
74 lmc_parse_file(ld_path_libmap_conf);
76 if (libmap_override) {
78 * Do some character replacement to make $LDLIBMAP look
79 * like a text file, then parse it.
81 libmap_override = xstrdup(libmap_override);
82 for (p = libmap_override; *p; p++) {
92 lmc_parse(libmap_override, p - libmap_override);
93 free(libmap_override);
96 return (lm_count == 0);
100 lmc_parse_file(const char *path)
108 TAILQ_FOREACH(p, &lmc_head, next) {
109 if (strcmp(p->path, path) == 0)
113 fd = open(path, O_RDONLY | O_CLOEXEC);
115 dbg("lm_parse_file: open(\"%s\") failed, %s", path,
116 rtld_strerror(errno));
119 if (fstat(fd, &st) == -1) {
121 dbg("lm_parse_file: fstat(\"%s\") failed, %s", path,
122 rtld_strerror(errno));
126 TAILQ_FOREACH(p, &lmc_head, next) {
127 if (p->dev == st.st_dev && p->ino == st.st_ino) {
133 lm_map = xmalloc(st.st_size);
134 retval = read(fd, lm_map, st.st_size);
135 if (retval != st.st_size) {
138 dbg("lm_parse_file: read(\"%s\") failed, %s", path,
139 rtld_strerror(errno));
143 p = xmalloc(sizeof(struct lmc));
144 p->path = xstrdup(path);
147 TAILQ_INSERT_HEAD(&lmc_head, p, next);
148 lmc_parse(lm_map, st.st_size);
153 lmc_parse_dir(const char *idir)
158 char conffile[MAXPATHLEN];
161 TAILQ_FOREACH(p, &lmc_head, next) {
162 if (strcmp(p->path, idir) == 0)
169 p = xmalloc(sizeof(struct lmc));
170 p->path = xstrdup(idir);
173 TAILQ_INSERT_HEAD(&lmc_head, p, next);
175 while ((dp = readdir(d)) != NULL) {
178 if (dp->d_type != DT_REG)
180 ext = strrchr(dp->d_name, '.');
183 if (strcmp(ext, ".conf") != 0)
185 if (strlcpy(conffile, idir, MAXPATHLEN) >= MAXPATHLEN)
186 continue; /* too long */
187 if (strlcat(conffile, "/", MAXPATHLEN) >= MAXPATHLEN)
188 continue; /* too long */
189 if (strlcat(conffile, dp->d_name, MAXPATHLEN) >= MAXPATHLEN)
190 continue; /* too long */
191 lmc_parse_file(conffile);
197 lmc_parse(char *lm_p, size_t lm_len)
199 char *cp, *f, *t, *c, *p;
200 char prog[MAXPATHLEN];
201 /* allow includedir + full length path */
202 char line[MAXPATHLEN + 13];
207 while (cnt < lm_len) {
209 while (cnt < lm_len && lm_p[cnt] != '\n' &&
210 i < sizeof(line) - 1) {
216 while (cnt < lm_len && lm_p[cnt] != '\n')
224 /* Skip over leading space */
225 while (rtld_isspace(*cp))
228 /* Found a comment or EOL */
232 /* Found a constraint selector */
236 /* Skip leading space */
237 while (rtld_isspace(*cp))
240 /* Found comment, EOL or end of selector */
241 if (iseol(*cp) || *cp == ']')
245 /* Skip to end of word */
246 while (!rtld_isspace(*cp) && !iseol(*cp) && *cp != ']')
249 /* Skip and zero out trailing space */
250 while (rtld_isspace(*cp))
253 /* Check if there is a closing brace */
257 /* Terminate string if there was no trailing space */
261 * There should be nothing except whitespace or comment
262 from this point to the end of the line.
264 while (rtld_isspace(*cp))
269 if (strlcpy(prog, c, sizeof prog) >= sizeof prog)
275 /* Parse the 'from' candidate. */
277 while (!rtld_isspace(*cp) && !iseol(*cp))
280 /* Skip and zero out the trailing whitespace */
281 while (rtld_isspace(*cp))
284 /* Found a comment or EOL */
288 /* Parse 'to' mapping */
290 while (!rtld_isspace(*cp) && !iseol(*cp))
293 /* Skip and zero out the trailing whitespace */
294 while (rtld_isspace(*cp))
297 /* Should be no extra tokens at this point */
302 if (strcmp(f, "includedir") == 0)
304 else if (strcmp(f, "include") == 0)
312 lm_free(struct lm_list *lml)
316 dbg("%s(%p)", __func__, lml);
318 while (!TAILQ_EMPTY(lml)) {
319 lm = TAILQ_FIRST(lml);
320 TAILQ_REMOVE(lml, lm, lm_link);
333 dbg("%s()", __func__);
335 while (!TAILQ_EMPTY(&lmc_head)) {
336 p = TAILQ_FIRST(&lmc_head);
337 TAILQ_REMOVE(&lmc_head, p, next);
342 while (!TAILQ_EMPTY(&lmp_head)) {
343 lmp = TAILQ_FIRST(&lmp_head);
344 TAILQ_REMOVE(&lmp_head, lmp, lmp_link);
352 lm_add(const char *p, const char *f, const char *t)
361 dbg("%s(\"%s\", \"%s\", \"%s\")", __func__, p, f, t);
363 if ((lml = lmp_find(p)) == NULL)
364 lml = lmp_init(xstrdup(p));
366 t1 = lml_find(lml, f);
367 if (t1 == NULL || strcmp(t1, t) != 0) {
368 lm = xmalloc(sizeof(struct lm));
371 TAILQ_INSERT_HEAD(lml, lm, lm_link);
377 lm_find(const char *p, const char *f)
382 dbg("%s(\"%s\", \"%s\")", __func__, p, f);
384 if (p != NULL && (lml = lmp_find(p)) != NULL) {
385 t = lml_find(lml, f);
388 * Add a global mapping if we have
389 * a successful constrained match.
395 lml = lmp_find("$DEFAULT$");
397 return (lml_find(lml, f));
402 * Given a libmap translation list and a library name, return the
403 * replacement library, or NULL.
406 lm_findn(const char *p, const char *f, const size_t n)
408 char pathbuf[64], *s, *t;
410 if (n < sizeof(pathbuf) - 1)
423 lml_find(struct lm_list *lmh, const char *f)
427 dbg("%s(%p, \"%s\")", __func__, lmh, f);
429 TAILQ_FOREACH(lm, lmh, lm_link) {
430 if (strcmp(f, lm->f) == 0)
437 * Given an executable name, return a pointer to the translation list or
438 * NULL if no matches.
440 static struct lm_list *
441 lmp_find(const char *n)
445 dbg("%s(\"%s\")", __func__, n);
447 TAILQ_FOREACH(lmp, &lmp_head, lmp_link) {
448 if ((lmp->type == T_EXACT && strcmp(n, lmp->p) == 0) ||
449 (lmp->type == T_DIRECTORY && strncmp(n, lmp->p,
450 strlen(lmp->p)) == 0) ||
451 (lmp->type == T_BASENAME && strcmp(quickbasename(n),
458 static struct lm_list *
463 dbg("%s(\"%s\")", __func__, n);
465 lmp = xmalloc(sizeof(struct lmp));
467 if (n[strlen(n) - 1] == '/')
468 lmp->type = T_DIRECTORY;
469 else if (strchr(n,'/') == NULL)
470 lmp->type = T_BASENAME;
473 TAILQ_INIT(&lmp->lml);
474 TAILQ_INSERT_HEAD(&lmp_head, lmp, lmp_link);
480 * libc basename is overkill. Return a pointer to the character after
481 * the last /, or the original string if there are no slashes.
484 quickbasename(const char *path)
488 for (p = path; *path != '\0'; path++) {