21 TAILQ_HEAD(lm_list, lm);
25 TAILQ_ENTRY(lm) lm_link;
28 static TAILQ_HEAD(lmp_list, lmp) lmp_head = TAILQ_HEAD_INITIALIZER(lmp_head);
31 enum { T_EXACT=0, T_BASENAME, T_DIRECTORY } type;
33 TAILQ_ENTRY(lmp) lmp_link;
36 static TAILQ_HEAD(lmc_list, lmc) lmc_head = TAILQ_HEAD_INITIALIZER(lmc_head);
41 TAILQ_ENTRY(lmc) next;
46 static void lmc_parse(char *, size_t);
47 static void lmc_parse_file(const char *);
48 static void lmc_parse_dir(const char *);
49 static void lm_add(const char *, const char *, const char *);
50 static void lm_free(struct lm_list *);
51 static char *lml_find(struct lm_list *, const char *);
52 static struct lm_list *lmp_find(const char *);
53 static struct lm_list *lmp_init(char *);
54 static const char *quickbasename(const char *);
56 #define iseol(c) (((c) == '#') || ((c) == '\0') || \
57 ((c) == '\n') || ((c) == '\r'))
60 * Do not use ctype.h macros, which rely on working TLS. It is
61 * too early to have thread-local variables functional.
63 #define rtld_isspace(c) ((c) == ' ' || (c) == '\t')
66 lm_init(char *libmap_override)
70 dbg("lm_init(\"%s\")", libmap_override);
71 TAILQ_INIT(&lmp_head);
73 lmc_parse_file(ld_path_libmap_conf);
75 if (libmap_override) {
77 * Do some character replacement to make $LDLIBMAP look
78 * like a text file, then parse it.
80 libmap_override = xstrdup(libmap_override);
81 for (p = libmap_override; *p; p++) {
91 lmc_parse(libmap_override, p - libmap_override);
92 free(libmap_override);
95 return (lm_count == 0);
99 lmc_parse_file(const char *path)
107 TAILQ_FOREACH(p, &lmc_head, next) {
108 if (strcmp(p->path, path) == 0)
112 fd = open(path, O_RDONLY | O_CLOEXEC);
114 dbg("lm_parse_file: open(\"%s\") failed, %s", path,
115 rtld_strerror(errno));
118 if (fstat(fd, &st) == -1) {
120 dbg("lm_parse_file: fstat(\"%s\") failed, %s", path,
121 rtld_strerror(errno));
125 TAILQ_FOREACH(p, &lmc_head, next) {
126 if (p->dev == st.st_dev && p->ino == st.st_ino) {
132 lm_map = xmalloc(st.st_size);
133 retval = read(fd, lm_map, st.st_size);
134 if (retval != st.st_size) {
137 dbg("lm_parse_file: read(\"%s\") failed, %s", path,
138 rtld_strerror(errno));
142 p = xmalloc(sizeof(struct lmc));
143 p->path = xstrdup(path);
146 TAILQ_INSERT_HEAD(&lmc_head, p, next);
147 lmc_parse(lm_map, st.st_size);
152 lmc_parse_dir(const char *idir)
157 char conffile[MAXPATHLEN];
160 TAILQ_FOREACH(p, &lmc_head, next) {
161 if (strcmp(p->path, idir) == 0)
168 p = xmalloc(sizeof(struct lmc));
169 p->path = xstrdup(idir);
172 TAILQ_INSERT_HEAD(&lmc_head, p, next);
174 while ((dp = readdir(d)) != NULL) {
177 if (dp->d_type != DT_REG)
179 ext = strrchr(dp->d_name, '.');
182 if (strcmp(ext, ".conf") != 0)
184 if (strlcpy(conffile, idir, MAXPATHLEN) >= MAXPATHLEN)
185 continue; /* too long */
186 if (strlcat(conffile, "/", MAXPATHLEN) >= MAXPATHLEN)
187 continue; /* too long */
188 if (strlcat(conffile, dp->d_name, MAXPATHLEN) >= MAXPATHLEN)
189 continue; /* too long */
190 lmc_parse_file(conffile);
196 lmc_parse(char *lm_p, size_t lm_len)
198 char *cp, *f, *t, *c, *p;
199 char prog[MAXPATHLEN];
200 /* allow includedir + full length path */
201 char line[MAXPATHLEN + 13];
206 while (cnt < lm_len) {
208 while (cnt < lm_len && lm_p[cnt] != '\n' &&
209 i < sizeof(line) - 1) {
215 while (cnt < lm_len && lm_p[cnt] != '\n')
223 /* Skip over leading space */
224 while (rtld_isspace(*cp))
227 /* Found a comment or EOL */
231 /* Found a constraint selector */
235 /* Skip leading space */
236 while (rtld_isspace(*cp))
239 /* Found comment, EOL or end of selector */
240 if (iseol(*cp) || *cp == ']')
244 /* Skip to end of word */
245 while (!rtld_isspace(*cp) && !iseol(*cp) && *cp != ']')
248 /* Skip and zero out trailing space */
249 while (rtld_isspace(*cp))
252 /* Check if there is a closing brace */
256 /* Terminate string if there was no trailing space */
260 * There should be nothing except whitespace or comment
261 from this point to the end of the line.
263 while (rtld_isspace(*cp))
268 if (strlcpy(prog, c, sizeof prog) >= sizeof prog)
274 /* Parse the 'from' candidate. */
276 while (!rtld_isspace(*cp) && !iseol(*cp))
279 /* Skip and zero out the trailing whitespace */
280 while (rtld_isspace(*cp))
283 /* Found a comment or EOL */
287 /* Parse 'to' mapping */
289 while (!rtld_isspace(*cp) && !iseol(*cp))
292 /* Skip and zero out the trailing whitespace */
293 while (rtld_isspace(*cp))
296 /* Should be no extra tokens at this point */
301 if (strcmp(f, "includedir") == 0)
303 else if (strcmp(f, "include") == 0)
311 lm_free(struct lm_list *lml)
315 dbg("%s(%p)", __func__, lml);
317 while (!TAILQ_EMPTY(lml)) {
318 lm = TAILQ_FIRST(lml);
319 TAILQ_REMOVE(lml, lm, lm_link);
332 dbg("%s()", __func__);
334 while (!TAILQ_EMPTY(&lmc_head)) {
335 p = TAILQ_FIRST(&lmc_head);
336 TAILQ_REMOVE(&lmc_head, p, next);
341 while (!TAILQ_EMPTY(&lmp_head)) {
342 lmp = TAILQ_FIRST(&lmp_head);
343 TAILQ_REMOVE(&lmp_head, lmp, lmp_link);
351 lm_add(const char *p, const char *f, const char *t)
360 dbg("%s(\"%s\", \"%s\", \"%s\")", __func__, p, f, t);
362 if ((lml = lmp_find(p)) == NULL)
363 lml = lmp_init(xstrdup(p));
365 t1 = lml_find(lml, f);
366 if (t1 == NULL || strcmp(t1, t) != 0) {
367 lm = xmalloc(sizeof(struct lm));
370 TAILQ_INSERT_HEAD(lml, lm, lm_link);
376 lm_find(const char *p, const char *f)
381 dbg("%s(\"%s\", \"%s\")", __func__, p, f);
383 if (p != NULL && (lml = lmp_find(p)) != NULL) {
384 t = lml_find(lml, f);
387 * Add a global mapping if we have
388 * a successful constrained match.
394 lml = lmp_find("$DEFAULT$");
396 return (lml_find(lml, f));
401 * Given a libmap translation list and a library name, return the
402 * replacement library, or NULL.
405 lm_findn(const char *p, const char *f, const size_t n)
407 char pathbuf[64], *s, *t;
409 if (n < sizeof(pathbuf) - 1)
422 lml_find(struct lm_list *lmh, const char *f)
426 dbg("%s(%p, \"%s\")", __func__, lmh, f);
428 TAILQ_FOREACH(lm, lmh, lm_link) {
429 if (strcmp(f, lm->f) == 0)
436 * Given an executable name, return a pointer to the translation list or
437 * NULL if no matches.
439 static struct lm_list *
440 lmp_find(const char *n)
444 dbg("%s(\"%s\")", __func__, n);
446 TAILQ_FOREACH(lmp, &lmp_head, lmp_link) {
447 if ((lmp->type == T_EXACT && strcmp(n, lmp->p) == 0) ||
448 (lmp->type == T_DIRECTORY && strncmp(n, lmp->p,
449 strlen(lmp->p)) == 0) ||
450 (lmp->type == T_BASENAME && strcmp(quickbasename(n),
457 static struct lm_list *
462 dbg("%s(\"%s\")", __func__, n);
464 lmp = xmalloc(sizeof(struct lmp));
466 if (n[strlen(n) - 1] == '/')
467 lmp->type = T_DIRECTORY;
468 else if (strchr(n,'/') == NULL)
469 lmp->type = T_BASENAME;
472 TAILQ_INIT(&lmp->lml);
473 TAILQ_INSERT_HEAD(&lmp_head, lmp, lmp_link);
479 * libc basename is overkill. Return a pointer to the character after
480 * the last /, or the original string if there are no slashes.
483 quickbasename(const char *path)
487 for (p = path; *path != '\0'; path++) {