xref: /freebsd/libexec/rtld-elf/libmap.c (revision 1aac1ed634249394326ffc23b5c2a47a13118cea)
129ade362SMatthew N. Dodd /*
229ade362SMatthew N. Dodd  * $FreeBSD$
329ade362SMatthew N. Dodd  */
429ade362SMatthew N. Dodd 
529ade362SMatthew N. Dodd #include <stdio.h>
629ade362SMatthew N. Dodd #include <ctype.h>
729ade362SMatthew N. Dodd #include <string.h>
829ade362SMatthew N. Dodd #include <stdlib.h>
929ade362SMatthew N. Dodd #include <sys/queue.h>
1029ade362SMatthew N. Dodd #include <sys/param.h>
1129ade362SMatthew N. Dodd 
123467e8b8SMatthew N. Dodd #include "debug.h"
133467e8b8SMatthew N. Dodd #include "rtld.h"
143467e8b8SMatthew N. Dodd 
1529ade362SMatthew N. Dodd #ifndef _PATH_LIBMAP_CONF
1629ade362SMatthew N. Dodd #define	_PATH_LIBMAP_CONF	"/etc/libmap.conf"
1729ade362SMatthew N. Dodd #endif
1829ade362SMatthew N. Dodd 
1929ade362SMatthew N. Dodd TAILQ_HEAD(lm_list, lm);
2029ade362SMatthew N. Dodd struct lm {
2129ade362SMatthew N. Dodd 	char *f;
2229ade362SMatthew N. Dodd 	char *t;
2329ade362SMatthew N. Dodd 
2429ade362SMatthew N. Dodd 	TAILQ_ENTRY(lm)	lm_link;
2529ade362SMatthew N. Dodd };
2629ade362SMatthew N. Dodd 
2729ade362SMatthew N. Dodd TAILQ_HEAD(lmp_list, lmp) lmp_head = TAILQ_HEAD_INITIALIZER(lmp_head);
2829ade362SMatthew N. Dodd struct lmp {
2929ade362SMatthew N. Dodd 	char *p;
3029ade362SMatthew N. Dodd 	struct lm_list lml;
3129ade362SMatthew N. Dodd 	TAILQ_ENTRY(lmp) lmp_link;
3229ade362SMatthew N. Dodd };
3329ade362SMatthew N. Dodd 
341340fc10SMatthew N. Dodd static void		lm_add		(const char *, const char *, const char *);
3529ade362SMatthew N. Dodd static void		lm_free		(struct lm_list *);
3629ade362SMatthew N. Dodd static char *		lml_find	(struct lm_list *, const char *);
3729ade362SMatthew N. Dodd static struct lm_list *	lmp_find	(const char *);
3829ade362SMatthew N. Dodd static struct lm_list *	lmp_init	(char *);
3929ade362SMatthew N. Dodd 
40623b6bd2SMatthew N. Dodd #define	iseol(c)	(((c) == '#') || ((c) == '\0') || \
41623b6bd2SMatthew N. Dodd 			 ((c) == '\n') || ((c) == '\r'))
42623b6bd2SMatthew N. Dodd 
4329ade362SMatthew N. Dodd void
4429ade362SMatthew N. Dodd lm_init (void)
4529ade362SMatthew N. Dodd {
4629ade362SMatthew N. Dodd 	FILE	*fp;
4729ade362SMatthew N. Dodd 	char	*cp;
481340fc10SMatthew N. Dodd 	char	*f, *t, *p, *c;
4929ade362SMatthew N. Dodd 	char	prog[MAXPATHLEN];
5029ade362SMatthew N. Dodd 	char	line[MAXPATHLEN + 2];
5129ade362SMatthew N. Dodd 
521aac1ed6SMatthew N. Dodd 	dbg("%s()", __func__);
531aac1ed6SMatthew N. Dodd 
5429ade362SMatthew N. Dodd 	TAILQ_INIT(&lmp_head);
5529ade362SMatthew N. Dodd 
5629ade362SMatthew N. Dodd 	if ((fp = fopen(_PATH_LIBMAP_CONF, "r")) == NULL)
5729ade362SMatthew N. Dodd 		return;
5829ade362SMatthew N. Dodd 
5929ade362SMatthew N. Dodd 	p = NULL;
6029ade362SMatthew N. Dodd 	while ((cp = fgets(line, MAXPATHLEN + 1, fp)) != NULL) {
611340fc10SMatthew N. Dodd 		t = f = c = NULL;
62623b6bd2SMatthew N. Dodd 
6329ade362SMatthew N. Dodd 		/* Skip over leading space */
64623b6bd2SMatthew N. Dodd 		while (isspace(*cp)) cp++;
65623b6bd2SMatthew N. Dodd 
6629ade362SMatthew N. Dodd 		/* Found a comment or EOL */
67486089f0SAlexander Kabaev 		if (iseol(*cp)) continue;
68623b6bd2SMatthew N. Dodd 
69623b6bd2SMatthew N. Dodd 		/* Found a constraint selector */
7029ade362SMatthew N. Dodd 		if (*cp == '[') {
7129ade362SMatthew N. Dodd 			cp++;
72623b6bd2SMatthew N. Dodd 
7329ade362SMatthew N. Dodd 			/* Skip leading space */
74623b6bd2SMatthew N. Dodd 			while (isspace(*cp)) cp++;
75623b6bd2SMatthew N. Dodd 
7629ade362SMatthew N. Dodd 			/* Found comment, EOL or end of selector */
77486089f0SAlexander Kabaev 			if  (iseol(*cp) || *cp == ']')
78486089f0SAlexander Kabaev 				continue;
79623b6bd2SMatthew N. Dodd 
801340fc10SMatthew N. Dodd 			c = cp++;
8129ade362SMatthew N. Dodd 			/* Skip to end of word */
82486089f0SAlexander Kabaev 			while (!isspace(*cp) && !iseol(*cp) && *cp != ']')
83486089f0SAlexander Kabaev 				cp++;
84623b6bd2SMatthew N. Dodd 
85623b6bd2SMatthew N. Dodd 			/* Skip and zero out trailing space */
86623b6bd2SMatthew N. Dodd 			while (isspace(*cp)) *cp++ = '\0';
87623b6bd2SMatthew N. Dodd 
88623b6bd2SMatthew N. Dodd 			/* Check if there is a closing brace */
89486089f0SAlexander Kabaev 			if (*cp != ']') continue;
90623b6bd2SMatthew N. Dodd 
91623b6bd2SMatthew N. Dodd 			/* Terminate string if there was no trailing space */
9229ade362SMatthew N. Dodd 			*cp++ = '\0';
93623b6bd2SMatthew N. Dodd 
94623b6bd2SMatthew N. Dodd 			/*
95623b6bd2SMatthew N. Dodd 			 * There should be nothing except whitespace or comment
966d5d786fSAlexander Kabaev 			  from this point to the end of the line.
97623b6bd2SMatthew N. Dodd 			 */
981340fc10SMatthew N. Dodd 			while(isspace(*cp)) *cp++;
99486089f0SAlexander Kabaev 			if (!iseol(*cp)) continue;
100623b6bd2SMatthew N. Dodd 
1011340fc10SMatthew N. Dodd 			strcpy(prog, c);
10229ade362SMatthew N. Dodd 			p = prog;
103486089f0SAlexander Kabaev 			continue;
104623b6bd2SMatthew N. Dodd 		}
105623b6bd2SMatthew N. Dodd 
106623b6bd2SMatthew N. Dodd 		/* Parse the 'from' candidate. */
107486089f0SAlexander Kabaev 		f = cp++;
108623b6bd2SMatthew N. Dodd 		while (!isspace(*cp) && !iseol(*cp)) cp++;
10929ade362SMatthew N. Dodd 
110623b6bd2SMatthew N. Dodd 		/* Skip and zero out the trailing whitespace */
111623b6bd2SMatthew N. Dodd 		while (isspace(*cp)) *cp++ = '\0';
112623b6bd2SMatthew N. Dodd 
113623b6bd2SMatthew N. Dodd 		/* Found a comment or EOL */
114486089f0SAlexander Kabaev 		if (iseol(*cp)) continue;
115623b6bd2SMatthew N. Dodd 
116623b6bd2SMatthew N. Dodd 		/* Parse 'to' mapping */
117486089f0SAlexander Kabaev 		t = cp++;
118623b6bd2SMatthew N. Dodd 		while (!isspace(*cp) && !iseol(*cp)) cp++;
119623b6bd2SMatthew N. Dodd 
120486089f0SAlexander Kabaev 		/* Skip and zero out the trailing whitespace */
121486089f0SAlexander Kabaev 		while (isspace(*cp)) *cp++ = '\0';
122486089f0SAlexander Kabaev 
123486089f0SAlexander Kabaev 		/* Should be no extra tokens at this point */
124486089f0SAlexander Kabaev 		if (!iseol(*cp)) continue;
125486089f0SAlexander Kabaev 
126486089f0SAlexander Kabaev 		*cp = '\0';
1271340fc10SMatthew N. Dodd 		lm_add(p, f, t);
12829ade362SMatthew N. Dodd 	}
129486089f0SAlexander Kabaev 	fclose(fp);
13029ade362SMatthew N. Dodd 	return;
13129ade362SMatthew N. Dodd }
13229ade362SMatthew N. Dodd 
13329ade362SMatthew N. Dodd static void
13429ade362SMatthew N. Dodd lm_free (struct lm_list *lml)
13529ade362SMatthew N. Dodd {
13629ade362SMatthew N. Dodd 	struct lm *lm;
13729ade362SMatthew N. Dodd 
1381aac1ed6SMatthew N. Dodd 	dbg("%s(%p)", __func__, lml);
1391aac1ed6SMatthew N. Dodd 
14029ade362SMatthew N. Dodd 	while (!TAILQ_EMPTY(lml)) {
14129ade362SMatthew N. Dodd 		lm = TAILQ_FIRST(lml);
14229ade362SMatthew N. Dodd 		TAILQ_REMOVE(lml, lm, lm_link);
14329ade362SMatthew N. Dodd 		free(lm->f);
14429ade362SMatthew N. Dodd 		free(lm->t);
14529ade362SMatthew N. Dodd 		free(lm);
14629ade362SMatthew N. Dodd 	}
14729ade362SMatthew N. Dodd 	return;
14829ade362SMatthew N. Dodd }
14929ade362SMatthew N. Dodd 
15029ade362SMatthew N. Dodd void
15129ade362SMatthew N. Dodd lm_fini (void)
15229ade362SMatthew N. Dodd {
15329ade362SMatthew N. Dodd 	struct lmp *lmp;
15429ade362SMatthew N. Dodd 
1551aac1ed6SMatthew N. Dodd 	dbg("%s()", __func__);
1561aac1ed6SMatthew N. Dodd 
15729ade362SMatthew N. Dodd 	while (!TAILQ_EMPTY(&lmp_head)) {
15829ade362SMatthew N. Dodd 		lmp = TAILQ_FIRST(&lmp_head);
15929ade362SMatthew N. Dodd 		TAILQ_REMOVE(&lmp_head, lmp, lmp_link);
16029ade362SMatthew N. Dodd 		free(lmp->p);
16129ade362SMatthew N. Dodd 		lm_free(&lmp->lml);
16229ade362SMatthew N. Dodd 		free(lmp);
16329ade362SMatthew N. Dodd 	}
16429ade362SMatthew N. Dodd 	return;
16529ade362SMatthew N. Dodd }
16629ade362SMatthew N. Dodd 
16729ade362SMatthew N. Dodd static void
1681340fc10SMatthew N. Dodd lm_add (const char *p, const char *f, const char *t)
16929ade362SMatthew N. Dodd {
17029ade362SMatthew N. Dodd 	struct lm_list *lml;
17129ade362SMatthew N. Dodd 	struct lm *lm;
17229ade362SMatthew N. Dodd 
17329ade362SMatthew N. Dodd 	if (p == NULL)
17429ade362SMatthew N. Dodd 		p = "$DEFAULT$";
17529ade362SMatthew N. Dodd 
1761aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\", \"%s\", \"%s\")", __func__, p, f, t);
1771aac1ed6SMatthew N. Dodd 
17829ade362SMatthew N. Dodd 	if ((lml = lmp_find(p)) == NULL)
1793467e8b8SMatthew N. Dodd 		lml = lmp_init(xstrdup(p));
18029ade362SMatthew N. Dodd 
1813467e8b8SMatthew N. Dodd 	lm = xmalloc(sizeof(struct lm));
1821340fc10SMatthew N. Dodd 	lm->f = xstrdup(f);
1831340fc10SMatthew N. Dodd 	lm->t = xstrdup(t);
18429ade362SMatthew N. Dodd 	TAILQ_INSERT_HEAD(lml, lm, lm_link);
18529ade362SMatthew N. Dodd }
18629ade362SMatthew N. Dodd 
18729ade362SMatthew N. Dodd char *
18829ade362SMatthew N. Dodd lm_find (const char *p, const char *f)
18929ade362SMatthew N. Dodd {
19029ade362SMatthew N. Dodd 	struct lm_list *lml;
19129ade362SMatthew N. Dodd 	char *t;
19229ade362SMatthew N. Dodd 
1931aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\", \"%s\")", __func__, p, f);
1941aac1ed6SMatthew N. Dodd 
19529ade362SMatthew N. Dodd 	if (p != NULL && (lml = lmp_find(p)) != NULL) {
19629ade362SMatthew N. Dodd 		t = lml_find(lml, f);
1973467e8b8SMatthew N. Dodd 		if (t != NULL) {
1983467e8b8SMatthew N. Dodd 			/*
1993467e8b8SMatthew N. Dodd 			 * Add a global mapping if we have
2003467e8b8SMatthew N. Dodd 			 * a successful constrained match.
2013467e8b8SMatthew N. Dodd 			 */
2021340fc10SMatthew N. Dodd 			lm_add(NULL, f, t);
20329ade362SMatthew N. Dodd 			return (t);
20429ade362SMatthew N. Dodd 		}
2053467e8b8SMatthew N. Dodd 	}
20629ade362SMatthew N. Dodd 	lml = lmp_find("$DEFAULT$");
20729ade362SMatthew N. Dodd 	if (lml != NULL)
20829ade362SMatthew N. Dodd 		return (lml_find(lml, f));
20929ade362SMatthew N. Dodd 	else
21029ade362SMatthew N. Dodd 		return (NULL);
21129ade362SMatthew N. Dodd }
21229ade362SMatthew N. Dodd 
21329ade362SMatthew N. Dodd static char *
21429ade362SMatthew N. Dodd lml_find (struct lm_list *lmh, const char *f)
21529ade362SMatthew N. Dodd {
21629ade362SMatthew N. Dodd 	struct lm *lm;
21729ade362SMatthew N. Dodd 
2181aac1ed6SMatthew N. Dodd 	dbg("%s(%p, \"%s\")", __func__, lmh, f);
2191aac1ed6SMatthew N. Dodd 
22029ade362SMatthew N. Dodd 	TAILQ_FOREACH(lm, lmh, lm_link)
22129ade362SMatthew N. Dodd 		if ((strncmp(f, lm->f, strlen(lm->f)) == 0) &&
22229ade362SMatthew N. Dodd 		    (strlen(f) == strlen(lm->f)))
22329ade362SMatthew N. Dodd 			return (lm->t);
22429ade362SMatthew N. Dodd 	return NULL;
22529ade362SMatthew N. Dodd }
22629ade362SMatthew N. Dodd 
22729ade362SMatthew N. Dodd static struct lm_list *
22829ade362SMatthew N. Dodd lmp_find (const char *n)
22929ade362SMatthew N. Dodd {
23029ade362SMatthew N. Dodd 	struct lmp *lmp;
23129ade362SMatthew N. Dodd 
2321aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\")", __func__, n);
2331aac1ed6SMatthew N. Dodd 
23429ade362SMatthew N. Dodd 	TAILQ_FOREACH(lmp, &lmp_head, lmp_link)
23529ade362SMatthew N. Dodd 		if ((strncmp(n, lmp->p, strlen(lmp->p)) == 0) &&
23629ade362SMatthew N. Dodd 		    (strlen(n) == strlen(lmp->p)))
23729ade362SMatthew N. Dodd 			return (&lmp->lml);
23829ade362SMatthew N. Dodd 	return (NULL);
23929ade362SMatthew N. Dodd }
24029ade362SMatthew N. Dodd 
24129ade362SMatthew N. Dodd static struct lm_list *
24229ade362SMatthew N. Dodd lmp_init (char *n)
24329ade362SMatthew N. Dodd {
24429ade362SMatthew N. Dodd 	struct lmp *lmp;
24529ade362SMatthew N. Dodd 
2461aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\")", __func__, n);
2471aac1ed6SMatthew N. Dodd 
2483467e8b8SMatthew N. Dodd 	lmp = xmalloc(sizeof(struct lmp));
24929ade362SMatthew N. Dodd 	lmp->p = n;
25029ade362SMatthew N. Dodd 	TAILQ_INIT(&lmp->lml);
25129ade362SMatthew N. Dodd 	TAILQ_INSERT_HEAD(&lmp_head, lmp, lmp_link);
25229ade362SMatthew N. Dodd 
25329ade362SMatthew N. Dodd 	return (&lmp->lml);
25429ade362SMatthew N. Dodd }
255