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) {
120 dbg("lm_parse_file: fstat(\"%s\") failed, %s", path,
121 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);
137 if (retval != st.st_size) {
139 dbg("lm_parse_file: read(\"%s\") failed, %s", path,
140 rtld_strerror(saved_errno));
142 dbg("lm_parse_file: short read(\"%s\"), %zd vs %jd",
143 path, retval, (uintmax_t)st.st_size);
148 p = xmalloc(sizeof(struct lmc));
149 p->path = xstrdup(path);
152 TAILQ_INSERT_HEAD(&lmc_head, p, next);
153 lmc_parse(lm_map, st.st_size);
158 lmc_parse_dir(const char *idir)
163 char conffile[MAXPATHLEN];
166 TAILQ_FOREACH(p, &lmc_head, next) {
167 if (strcmp(p->path, idir) == 0)
174 p = xmalloc(sizeof(struct lmc));
175 p->path = xstrdup(idir);
178 TAILQ_INSERT_HEAD(&lmc_head, p, next);
180 while ((dp = readdir(d)) != NULL) {
183 if (dp->d_type != DT_REG)
185 ext = strrchr(dp->d_name, '.');
188 if (strcmp(ext, ".conf") != 0)
190 if (strlcpy(conffile, idir, MAXPATHLEN) >= MAXPATHLEN)
191 continue; /* too long */
192 if (strlcat(conffile, "/", MAXPATHLEN) >= MAXPATHLEN)
193 continue; /* too long */
194 if (strlcat(conffile, dp->d_name, MAXPATHLEN) >= MAXPATHLEN)
195 continue; /* too long */
196 lmc_parse_file(conffile);
202 lmc_parse(char *lm_p, size_t lm_len)
204 char *cp, *f, *t, *c, *p;
205 char prog[MAXPATHLEN];
206 /* allow includedir + full length path */
207 char line[MAXPATHLEN + 13];
212 while (cnt < lm_len) {
214 while (cnt < lm_len && lm_p[cnt] != '\n' &&
215 i < sizeof(line) - 1) {
221 while (cnt < lm_len && lm_p[cnt] != '\n')
229 /* Skip over leading space */
230 while (rtld_isspace(*cp))
233 /* Found a comment or EOL */
237 /* Found a constraint selector */
241 /* Skip leading space */
242 while (rtld_isspace(*cp))
245 /* Found comment, EOL or end of selector */
246 if (iseol(*cp) || *cp == ']')
250 /* Skip to end of word */
251 while (!rtld_isspace(*cp) && !iseol(*cp) && *cp != ']')
254 /* Skip and zero out trailing space */
255 while (rtld_isspace(*cp))
258 /* Check if there is a closing brace */
262 /* Terminate string if there was no trailing space */
266 * There should be nothing except whitespace or comment
267 from this point to the end of the line.
269 while (rtld_isspace(*cp))
274 if (strlcpy(prog, c, sizeof prog) >= sizeof prog)
280 /* Parse the 'from' candidate. */
282 while (!rtld_isspace(*cp) && !iseol(*cp))
285 /* Skip and zero out the trailing whitespace */
286 while (rtld_isspace(*cp))
289 /* Found a comment or EOL */
293 /* Parse 'to' mapping */
295 while (!rtld_isspace(*cp) && !iseol(*cp))
298 /* Skip and zero out the trailing whitespace */
299 while (rtld_isspace(*cp))
302 /* Should be no extra tokens at this point */
307 if (strcmp(f, "includedir") == 0)
309 else if (strcmp(f, "include") == 0)
317 lm_free(struct lm_list *lml)
321 dbg("%s(%p)", __func__, lml);
323 while (!TAILQ_EMPTY(lml)) {
324 lm = TAILQ_FIRST(lml);
325 TAILQ_REMOVE(lml, lm, lm_link);
338 dbg("%s()", __func__);
340 while (!TAILQ_EMPTY(&lmc_head)) {
341 p = TAILQ_FIRST(&lmc_head);
342 TAILQ_REMOVE(&lmc_head, p, next);
347 while (!TAILQ_EMPTY(&lmp_head)) {
348 lmp = TAILQ_FIRST(&lmp_head);
349 TAILQ_REMOVE(&lmp_head, lmp, lmp_link);
357 lm_add(const char *p, const char *f, const char *t)
366 dbg("%s(\"%s\", \"%s\", \"%s\")", __func__, p, f, t);
368 if ((lml = lmp_find(p)) == NULL)
369 lml = lmp_init(xstrdup(p));
371 t1 = lml_find(lml, f);
372 if (t1 == NULL || strcmp(t1, t) != 0) {
373 lm = xmalloc(sizeof(struct lm));
376 TAILQ_INSERT_HEAD(lml, lm, lm_link);
382 lm_find(const char *p, const char *f)
387 dbg("%s(\"%s\", \"%s\")", __func__, p, f);
389 if (p != NULL && (lml = lmp_find(p)) != NULL) {
390 t = lml_find(lml, f);
393 * Add a global mapping if we have
394 * a successful constrained match.
400 lml = lmp_find("$DEFAULT$");
402 return (lml_find(lml, f));
407 * Given a libmap translation list and a library name, return the
408 * replacement library, or NULL.
411 lm_findn(const char *p, const char *f, const size_t n)
413 char pathbuf[64], *s, *t;
415 if (n < sizeof(pathbuf) - 1)
428 lml_find(struct lm_list *lmh, const char *f)
432 dbg("%s(%p, \"%s\")", __func__, lmh, f);
434 TAILQ_FOREACH(lm, lmh, lm_link) {
435 if (strcmp(f, lm->f) == 0)
442 * Given an executable name, return a pointer to the translation list or
443 * NULL if no matches.
445 static struct lm_list *
446 lmp_find(const char *n)
450 dbg("%s(\"%s\")", __func__, n);
452 TAILQ_FOREACH(lmp, &lmp_head, lmp_link) {
453 if ((lmp->type == T_EXACT && strcmp(n, lmp->p) == 0) ||
454 (lmp->type == T_DIRECTORY && strncmp(n, lmp->p,
455 strlen(lmp->p)) == 0) ||
456 (lmp->type == T_BASENAME && strcmp(quickbasename(n),
463 static struct lm_list *
468 dbg("%s(\"%s\")", __func__, n);
470 lmp = xmalloc(sizeof(struct lmp));
472 if (n[strlen(n) - 1] == '/')
473 lmp->type = T_DIRECTORY;
474 else if (strchr(n,'/') == NULL)
475 lmp->type = T_BASENAME;
478 TAILQ_INIT(&lmp->lml);
479 TAILQ_INSERT_HEAD(&lmp_head, lmp, lmp_link);
485 * libc basename is overkill. Return a pointer to the character after
486 * the last /, or the original string if there are no slashes.
489 quickbasename(const char *path)
493 for (p = path; *path != '\0'; path++) {