xref: /freebsd/libexec/rtld-elf/libmap.c (revision 4402996dea6d9df64ac8c395f31b5d90d364207b)
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"
147227105fSMatthew N. Dodd #include "libmap.h"
153467e8b8SMatthew N. Dodd 
1629ade362SMatthew N. Dodd #ifndef _PATH_LIBMAP_CONF
1729ade362SMatthew N. Dodd #define	_PATH_LIBMAP_CONF	"/etc/libmap.conf"
1829ade362SMatthew N. Dodd #endif
1929ade362SMatthew N. Dodd 
2029ade362SMatthew N. Dodd TAILQ_HEAD(lm_list, lm);
2129ade362SMatthew N. Dodd struct lm {
2229ade362SMatthew N. Dodd 	char *f;
2329ade362SMatthew N. Dodd 	char *t;
2429ade362SMatthew N. Dodd 
2529ade362SMatthew N. Dodd 	TAILQ_ENTRY(lm)	lm_link;
2629ade362SMatthew N. Dodd };
2729ade362SMatthew N. Dodd 
2829ade362SMatthew N. Dodd TAILQ_HEAD(lmp_list, lmp) lmp_head = TAILQ_HEAD_INITIALIZER(lmp_head);
2929ade362SMatthew N. Dodd struct lmp {
3029ade362SMatthew N. Dodd 	char *p;
3129ade362SMatthew N. Dodd 	struct lm_list lml;
3229ade362SMatthew N. Dodd 	TAILQ_ENTRY(lmp) lmp_link;
3329ade362SMatthew N. Dodd };
3429ade362SMatthew N. Dodd 
351340fc10SMatthew N. Dodd static void		lm_add		(const char *, const char *, const char *);
3629ade362SMatthew N. Dodd static void		lm_free		(struct lm_list *);
3729ade362SMatthew N. Dodd static char *		lml_find	(struct lm_list *, const char *);
3829ade362SMatthew N. Dodd static struct lm_list *	lmp_find	(const char *);
3929ade362SMatthew N. Dodd static struct lm_list *	lmp_init	(char *);
4029ade362SMatthew N. Dodd 
41623b6bd2SMatthew N. Dodd #define	iseol(c)	(((c) == '#') || ((c) == '\0') || \
42623b6bd2SMatthew N. Dodd 			 ((c) == '\n') || ((c) == '\r'))
43623b6bd2SMatthew N. Dodd 
444402996dSMatthew N. Dodd int
4529ade362SMatthew N. Dodd lm_init (void)
4629ade362SMatthew N. Dodd {
4729ade362SMatthew N. Dodd 	FILE	*fp;
4829ade362SMatthew N. Dodd 	char	*cp;
491340fc10SMatthew N. Dodd 	char	*f, *t, *p, *c;
5029ade362SMatthew N. Dodd 	char	prog[MAXPATHLEN];
5129ade362SMatthew N. Dodd 	char	line[MAXPATHLEN + 2];
5229ade362SMatthew N. Dodd 
531aac1ed6SMatthew N. Dodd 	dbg("%s()", __func__);
541aac1ed6SMatthew N. Dodd 
5529ade362SMatthew N. Dodd 	TAILQ_INIT(&lmp_head);
5629ade362SMatthew N. Dodd 
5729ade362SMatthew N. Dodd 	if ((fp = fopen(_PATH_LIBMAP_CONF, "r")) == NULL)
584402996dSMatthew N. Dodd 		return (1);
5929ade362SMatthew N. Dodd 
6029ade362SMatthew N. Dodd 	p = NULL;
6129ade362SMatthew N. Dodd 	while ((cp = fgets(line, MAXPATHLEN + 1, fp)) != NULL) {
621340fc10SMatthew N. Dodd 		t = f = c = NULL;
63623b6bd2SMatthew N. Dodd 
6429ade362SMatthew N. Dodd 		/* Skip over leading space */
65623b6bd2SMatthew N. Dodd 		while (isspace(*cp)) cp++;
66623b6bd2SMatthew N. Dodd 
6729ade362SMatthew N. Dodd 		/* Found a comment or EOL */
68486089f0SAlexander Kabaev 		if (iseol(*cp)) continue;
69623b6bd2SMatthew N. Dodd 
70623b6bd2SMatthew N. Dodd 		/* Found a constraint selector */
7129ade362SMatthew N. Dodd 		if (*cp == '[') {
7229ade362SMatthew N. Dodd 			cp++;
73623b6bd2SMatthew N. Dodd 
7429ade362SMatthew N. Dodd 			/* Skip leading space */
75623b6bd2SMatthew N. Dodd 			while (isspace(*cp)) cp++;
76623b6bd2SMatthew N. Dodd 
7729ade362SMatthew N. Dodd 			/* Found comment, EOL or end of selector */
78486089f0SAlexander Kabaev 			if  (iseol(*cp) || *cp == ']')
79486089f0SAlexander Kabaev 				continue;
80623b6bd2SMatthew N. Dodd 
811340fc10SMatthew N. Dodd 			c = cp++;
8229ade362SMatthew N. Dodd 			/* Skip to end of word */
83486089f0SAlexander Kabaev 			while (!isspace(*cp) && !iseol(*cp) && *cp != ']')
84486089f0SAlexander Kabaev 				cp++;
85623b6bd2SMatthew N. Dodd 
86623b6bd2SMatthew N. Dodd 			/* Skip and zero out trailing space */
87623b6bd2SMatthew N. Dodd 			while (isspace(*cp)) *cp++ = '\0';
88623b6bd2SMatthew N. Dodd 
89623b6bd2SMatthew N. Dodd 			/* Check if there is a closing brace */
90486089f0SAlexander Kabaev 			if (*cp != ']') continue;
91623b6bd2SMatthew N. Dodd 
92623b6bd2SMatthew N. Dodd 			/* Terminate string if there was no trailing space */
9329ade362SMatthew N. Dodd 			*cp++ = '\0';
94623b6bd2SMatthew N. Dodd 
95623b6bd2SMatthew N. Dodd 			/*
96623b6bd2SMatthew N. Dodd 			 * There should be nothing except whitespace or comment
976d5d786fSAlexander Kabaev 			  from this point to the end of the line.
98623b6bd2SMatthew N. Dodd 			 */
991340fc10SMatthew N. Dodd 			while(isspace(*cp)) *cp++;
100486089f0SAlexander Kabaev 			if (!iseol(*cp)) continue;
101623b6bd2SMatthew N. Dodd 
1021340fc10SMatthew N. Dodd 			strcpy(prog, c);
10329ade362SMatthew N. Dodd 			p = prog;
104486089f0SAlexander Kabaev 			continue;
105623b6bd2SMatthew N. Dodd 		}
106623b6bd2SMatthew N. Dodd 
107623b6bd2SMatthew N. Dodd 		/* Parse the 'from' candidate. */
108486089f0SAlexander Kabaev 		f = cp++;
109623b6bd2SMatthew N. Dodd 		while (!isspace(*cp) && !iseol(*cp)) cp++;
11029ade362SMatthew N. Dodd 
111623b6bd2SMatthew N. Dodd 		/* Skip and zero out the trailing whitespace */
112623b6bd2SMatthew N. Dodd 		while (isspace(*cp)) *cp++ = '\0';
113623b6bd2SMatthew N. Dodd 
114623b6bd2SMatthew N. Dodd 		/* Found a comment or EOL */
115486089f0SAlexander Kabaev 		if (iseol(*cp)) continue;
116623b6bd2SMatthew N. Dodd 
117623b6bd2SMatthew N. Dodd 		/* Parse 'to' mapping */
118486089f0SAlexander Kabaev 		t = cp++;
119623b6bd2SMatthew N. Dodd 		while (!isspace(*cp) && !iseol(*cp)) cp++;
120623b6bd2SMatthew N. Dodd 
121486089f0SAlexander Kabaev 		/* Skip and zero out the trailing whitespace */
122486089f0SAlexander Kabaev 		while (isspace(*cp)) *cp++ = '\0';
123486089f0SAlexander Kabaev 
124486089f0SAlexander Kabaev 		/* Should be no extra tokens at this point */
125486089f0SAlexander Kabaev 		if (!iseol(*cp)) continue;
126486089f0SAlexander Kabaev 
127486089f0SAlexander Kabaev 		*cp = '\0';
1281340fc10SMatthew N. Dodd 		lm_add(p, f, t);
12929ade362SMatthew N. Dodd 	}
130486089f0SAlexander Kabaev 	fclose(fp);
1314402996dSMatthew N. Dodd 	return (0);
13229ade362SMatthew N. Dodd }
13329ade362SMatthew N. Dodd 
13429ade362SMatthew N. Dodd static void
13529ade362SMatthew N. Dodd lm_free (struct lm_list *lml)
13629ade362SMatthew N. Dodd {
13729ade362SMatthew N. Dodd 	struct lm *lm;
13829ade362SMatthew N. Dodd 
1391aac1ed6SMatthew N. Dodd 	dbg("%s(%p)", __func__, lml);
1401aac1ed6SMatthew N. Dodd 
14129ade362SMatthew N. Dodd 	while (!TAILQ_EMPTY(lml)) {
14229ade362SMatthew N. Dodd 		lm = TAILQ_FIRST(lml);
14329ade362SMatthew N. Dodd 		TAILQ_REMOVE(lml, lm, lm_link);
14429ade362SMatthew N. Dodd 		free(lm->f);
14529ade362SMatthew N. Dodd 		free(lm->t);
14629ade362SMatthew N. Dodd 		free(lm);
14729ade362SMatthew N. Dodd 	}
14829ade362SMatthew N. Dodd 	return;
14929ade362SMatthew N. Dodd }
15029ade362SMatthew N. Dodd 
15129ade362SMatthew N. Dodd void
15229ade362SMatthew N. Dodd lm_fini (void)
15329ade362SMatthew N. Dodd {
15429ade362SMatthew N. Dodd 	struct lmp *lmp;
15529ade362SMatthew N. Dodd 
1561aac1ed6SMatthew N. Dodd 	dbg("%s()", __func__);
1571aac1ed6SMatthew N. Dodd 
15829ade362SMatthew N. Dodd 	while (!TAILQ_EMPTY(&lmp_head)) {
15929ade362SMatthew N. Dodd 		lmp = TAILQ_FIRST(&lmp_head);
16029ade362SMatthew N. Dodd 		TAILQ_REMOVE(&lmp_head, lmp, lmp_link);
16129ade362SMatthew N. Dodd 		free(lmp->p);
16229ade362SMatthew N. Dodd 		lm_free(&lmp->lml);
16329ade362SMatthew N. Dodd 		free(lmp);
16429ade362SMatthew N. Dodd 	}
16529ade362SMatthew N. Dodd 	return;
16629ade362SMatthew N. Dodd }
16729ade362SMatthew N. Dodd 
16829ade362SMatthew N. Dodd static void
1691340fc10SMatthew N. Dodd lm_add (const char *p, const char *f, const char *t)
17029ade362SMatthew N. Dodd {
17129ade362SMatthew N. Dodd 	struct lm_list *lml;
17229ade362SMatthew N. Dodd 	struct lm *lm;
17329ade362SMatthew N. Dodd 
17429ade362SMatthew N. Dodd 	if (p == NULL)
17529ade362SMatthew N. Dodd 		p = "$DEFAULT$";
17629ade362SMatthew N. Dodd 
1771aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\", \"%s\", \"%s\")", __func__, p, f, t);
1781aac1ed6SMatthew N. Dodd 
17929ade362SMatthew N. Dodd 	if ((lml = lmp_find(p)) == NULL)
1803467e8b8SMatthew N. Dodd 		lml = lmp_init(xstrdup(p));
18129ade362SMatthew N. Dodd 
1823467e8b8SMatthew N. Dodd 	lm = xmalloc(sizeof(struct lm));
1831340fc10SMatthew N. Dodd 	lm->f = xstrdup(f);
1841340fc10SMatthew N. Dodd 	lm->t = xstrdup(t);
18529ade362SMatthew N. Dodd 	TAILQ_INSERT_HEAD(lml, lm, lm_link);
18629ade362SMatthew N. Dodd }
18729ade362SMatthew N. Dodd 
18829ade362SMatthew N. Dodd char *
18929ade362SMatthew N. Dodd lm_find (const char *p, const char *f)
19029ade362SMatthew N. Dodd {
19129ade362SMatthew N. Dodd 	struct lm_list *lml;
19229ade362SMatthew N. Dodd 	char *t;
19329ade362SMatthew N. Dodd 
1941aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\", \"%s\")", __func__, p, f);
1951aac1ed6SMatthew N. Dodd 
19629ade362SMatthew N. Dodd 	if (p != NULL && (lml = lmp_find(p)) != NULL) {
19729ade362SMatthew N. Dodd 		t = lml_find(lml, f);
1983467e8b8SMatthew N. Dodd 		if (t != NULL) {
1993467e8b8SMatthew N. Dodd 			/*
2003467e8b8SMatthew N. Dodd 			 * Add a global mapping if we have
2013467e8b8SMatthew N. Dodd 			 * a successful constrained match.
2023467e8b8SMatthew N. Dodd 			 */
2031340fc10SMatthew N. Dodd 			lm_add(NULL, f, t);
20429ade362SMatthew N. Dodd 			return (t);
20529ade362SMatthew N. Dodd 		}
2063467e8b8SMatthew N. Dodd 	}
20729ade362SMatthew N. Dodd 	lml = lmp_find("$DEFAULT$");
20829ade362SMatthew N. Dodd 	if (lml != NULL)
20929ade362SMatthew N. Dodd 		return (lml_find(lml, f));
21029ade362SMatthew N. Dodd 	else
21129ade362SMatthew N. Dodd 		return (NULL);
21229ade362SMatthew N. Dodd }
21329ade362SMatthew N. Dodd 
21429ade362SMatthew N. Dodd static char *
21529ade362SMatthew N. Dodd lml_find (struct lm_list *lmh, const char *f)
21629ade362SMatthew N. Dodd {
21729ade362SMatthew N. Dodd 	struct lm *lm;
21829ade362SMatthew N. Dodd 
2191aac1ed6SMatthew N. Dodd 	dbg("%s(%p, \"%s\")", __func__, lmh, f);
2201aac1ed6SMatthew N. Dodd 
22129ade362SMatthew N. Dodd 	TAILQ_FOREACH(lm, lmh, lm_link)
22229ade362SMatthew N. Dodd 		if ((strncmp(f, lm->f, strlen(lm->f)) == 0) &&
22329ade362SMatthew N. Dodd 		    (strlen(f) == strlen(lm->f)))
22429ade362SMatthew N. Dodd 			return (lm->t);
22529ade362SMatthew N. Dodd 	return NULL;
22629ade362SMatthew N. Dodd }
22729ade362SMatthew N. Dodd 
22829ade362SMatthew N. Dodd static struct lm_list *
22929ade362SMatthew N. Dodd lmp_find (const char *n)
23029ade362SMatthew N. Dodd {
23129ade362SMatthew N. Dodd 	struct lmp *lmp;
23229ade362SMatthew N. Dodd 
2331aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\")", __func__, n);
2341aac1ed6SMatthew N. Dodd 
23529ade362SMatthew N. Dodd 	TAILQ_FOREACH(lmp, &lmp_head, lmp_link)
23629ade362SMatthew N. Dodd 		if ((strncmp(n, lmp->p, strlen(lmp->p)) == 0) &&
23729ade362SMatthew N. Dodd 		    (strlen(n) == strlen(lmp->p)))
23829ade362SMatthew N. Dodd 			return (&lmp->lml);
23929ade362SMatthew N. Dodd 	return (NULL);
24029ade362SMatthew N. Dodd }
24129ade362SMatthew N. Dodd 
24229ade362SMatthew N. Dodd static struct lm_list *
24329ade362SMatthew N. Dodd lmp_init (char *n)
24429ade362SMatthew N. Dodd {
24529ade362SMatthew N. Dodd 	struct lmp *lmp;
24629ade362SMatthew N. Dodd 
2471aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\")", __func__, n);
2481aac1ed6SMatthew N. Dodd 
2493467e8b8SMatthew N. Dodd 	lmp = xmalloc(sizeof(struct lmp));
25029ade362SMatthew N. Dodd 	lmp->p = n;
25129ade362SMatthew N. Dodd 	TAILQ_INIT(&lmp->lml);
25229ade362SMatthew N. Dodd 	TAILQ_INSERT_HEAD(&lmp_head, lmp, lmp_link);
25329ade362SMatthew N. Dodd 
25429ade362SMatthew N. Dodd 	return (&lmp->lml);
25529ade362SMatthew N. Dodd }
256