21 TAILQ_HEAD(lm_list, lm);
25 TAILQ_ENTRY(lm) lm_link;
28 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(char *);
48 static void lmc_parse_dir(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(char *path)
106 TAILQ_FOREACH(p, &lmc_head, next) {
107 if (strcmp(p->path, path) == 0)
111 fd = open(path, O_RDONLY | O_CLOEXEC);
113 dbg("lm_parse_file: open(\"%s\") failed, %s", path,
114 rtld_strerror(errno));
117 if (fstat(fd, &st) == -1) {
119 dbg("lm_parse_file: fstat(\"%s\") failed, %s", path,
120 rtld_strerror(errno));
124 TAILQ_FOREACH(p, &lmc_head, next) {
125 if (p->dev == st.st_dev && p->ino == st.st_ino) {
131 lm_map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
132 if (lm_map == (const char *)MAP_FAILED) {
134 dbg("lm_parse_file: mmap(\"%s\") failed, %s", path,
135 rtld_strerror(errno));
139 p = xmalloc(sizeof(struct lmc));
140 p->path = xstrdup(path);
143 TAILQ_INSERT_HEAD(&lmc_head, p, next);
144 lmc_parse(lm_map, st.st_size);
145 munmap(lm_map, st.st_size);
149 lmc_parse_dir(char *idir)
154 char conffile[MAXPATHLEN];
157 TAILQ_FOREACH(p, &lmc_head, next) {
158 if (strcmp(p->path, idir) == 0)
165 p = xmalloc(sizeof(struct lmc));
166 p->path = xstrdup(idir);
169 TAILQ_INSERT_HEAD(&lmc_head, p, next);
171 while ((dp = readdir(d)) != NULL) {
174 if (dp->d_type != DT_REG)
176 ext = strrchr(dp->d_name, '.');
179 if (strcmp(ext, ".conf") != 0)
181 if (strlcpy(conffile, idir, MAXPATHLEN) >= MAXPATHLEN)
182 continue; /* too long */
183 if (strlcat(conffile, "/", MAXPATHLEN) >= MAXPATHLEN)
184 continue; /* too long */
185 if (strlcat(conffile, dp->d_name, MAXPATHLEN) >= MAXPATHLEN)
186 continue; /* too long */
187 lmc_parse_file(conffile);
193 lmc_parse(char *lm_p, size_t lm_len)
195 char *cp, *f, *t, *c, *p;
196 char prog[MAXPATHLEN];
197 /* allow includedir + full length path */
198 char line[MAXPATHLEN + 13];
204 while (cnt < lm_len) {
206 while (cnt < lm_len && lm_p[cnt] != '\n' &&
207 i < sizeof(line) - 1) {
213 while (cnt < lm_len && lm_p[cnt] != '\n')
221 /* Skip over leading space */
222 while (rtld_isspace(*cp))
225 /* Found a comment or EOL */
229 /* Found a constraint selector */
233 /* Skip leading space */
234 while (rtld_isspace(*cp))
237 /* Found comment, EOL or end of selector */
238 if (iseol(*cp) || *cp == ']')
242 /* Skip to end of word */
243 while (!rtld_isspace(*cp) && !iseol(*cp) && *cp != ']')
246 /* Skip and zero out trailing space */
247 while (rtld_isspace(*cp))
250 /* Check if there is a closing brace */
254 /* Terminate string if there was no trailing space */
258 * There should be nothing except whitespace or comment
259 from this point to the end of the line.
261 while (rtld_isspace(*cp))
266 if (strlcpy(prog, c, sizeof prog) >= sizeof prog)
272 /* Parse the 'from' candidate. */
274 while (!rtld_isspace(*cp) && !iseol(*cp))
277 /* Skip and zero out the trailing whitespace */
278 while (rtld_isspace(*cp))
281 /* Found a comment or EOL */
285 /* Parse 'to' mapping */
287 while (!rtld_isspace(*cp) && !iseol(*cp))
290 /* Skip and zero out the trailing whitespace */
291 while (rtld_isspace(*cp))
294 /* Should be no extra tokens at this point */
299 if (strcmp(f, "includedir") == 0)
301 else if (strcmp(f, "include") == 0)
309 lm_free(struct lm_list *lml)
313 dbg("%s(%p)", __func__, lml);
315 while (!TAILQ_EMPTY(lml)) {
316 lm = TAILQ_FIRST(lml);
317 TAILQ_REMOVE(lml, lm, lm_link);
330 dbg("%s()", __func__);
332 while (!TAILQ_EMPTY(&lmc_head)) {
333 p = TAILQ_FIRST(&lmc_head);
334 TAILQ_REMOVE(&lmc_head, p, next);
339 while (!TAILQ_EMPTY(&lmp_head)) {
340 lmp = TAILQ_FIRST(&lmp_head);
341 TAILQ_REMOVE(&lmp_head, lmp, lmp_link);
349 lm_add(const char *p, const char *f, const char *t)
358 dbg("%s(\"%s\", \"%s\", \"%s\")", __func__, p, f, t);
360 if ((lml = lmp_find(p)) == NULL)
361 lml = lmp_init(xstrdup(p));
363 t1 = lml_find(lml, f);
364 if (t1 == NULL || strcmp(t1, t) != 0) {
365 lm = xmalloc(sizeof(struct lm));
368 TAILQ_INSERT_HEAD(lml, lm, lm_link);
374 lm_find(const char *p, const char *f)
379 dbg("%s(\"%s\", \"%s\")", __func__, p, f);
381 if (p != NULL && (lml = lmp_find(p)) != NULL) {
382 t = lml_find(lml, f);
385 * Add a global mapping if we have
386 * a successful constrained match.
392 lml = lmp_find("$DEFAULT$");
394 return (lml_find(lml, f));
399 * Given a libmap translation list and a library name, return the
400 * replacement library, or NULL.
403 lm_findn(const char *p, const char *f, const int n)
405 char pathbuf[64], *s, *t;
407 if (n < sizeof(pathbuf) - 1)
420 lml_find(struct lm_list *lmh, const char *f)
424 dbg("%s(%p, \"%s\")", __func__, lmh, f);
426 TAILQ_FOREACH(lm, lmh, lm_link) {
427 if (strcmp(f, lm->f) == 0)
434 * Given an executable name, return a pointer to the translation list or
435 * NULL if no matches.
437 static struct lm_list *
438 lmp_find(const char *n)
442 dbg("%s(\"%s\")", __func__, n);
444 TAILQ_FOREACH(lmp, &lmp_head, lmp_link) {
445 if ((lmp->type == T_EXACT && strcmp(n, lmp->p) == 0) ||
446 (lmp->type == T_DIRECTORY && strncmp(n, lmp->p,
447 strlen(lmp->p)) == 0) ||
448 (lmp->type == T_BASENAME && strcmp(quickbasename(n),
455 static struct lm_list *
460 dbg("%s(\"%s\")", __func__, n);
462 lmp = xmalloc(sizeof(struct lmp));
464 if (n[strlen(n) - 1] == '/')
465 lmp->type = T_DIRECTORY;
466 else if (strchr(n,'/') == NULL)
467 lmp->type = T_BASENAME;
470 TAILQ_INIT(&lmp->lml);
471 TAILQ_INSERT_HEAD(&lmp_head, lmp, lmp_link);
477 * libc basename is overkill. Return a pointer to the character after
478 * the last /, or the original string if there are no slashes.
481 quickbasename(const char *path)
485 for (p = path; *path != '\0'; path++) {