xref: /freebsd/libexec/rtld-elf/libmap.c (revision c905e45dc0e7ad2da0748371ba8921ca48134a01)
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 
20c905e45dSPeter Wemm #ifdef COMPAT_32BIT
21c905e45dSPeter Wemm #undef _PATH_LIBMAP_CONF
22c905e45dSPeter Wemm #define	_PATH_LIBMAP_CONF	"/etc/libmap32.conf"
23c905e45dSPeter Wemm #endif
24c905e45dSPeter Wemm 
2529ade362SMatthew N. Dodd TAILQ_HEAD(lm_list, lm);
2629ade362SMatthew N. Dodd struct lm {
2729ade362SMatthew N. Dodd 	char *f;
2829ade362SMatthew N. Dodd 	char *t;
2929ade362SMatthew N. Dodd 
3029ade362SMatthew N. Dodd 	TAILQ_ENTRY(lm)	lm_link;
3129ade362SMatthew N. Dodd };
3229ade362SMatthew N. Dodd 
3329ade362SMatthew N. Dodd TAILQ_HEAD(lmp_list, lmp) lmp_head = TAILQ_HEAD_INITIALIZER(lmp_head);
3429ade362SMatthew N. Dodd struct lmp {
3529ade362SMatthew N. Dodd 	char *p;
3629ade362SMatthew N. Dodd 	struct lm_list lml;
3729ade362SMatthew N. Dodd 	TAILQ_ENTRY(lmp) lmp_link;
3829ade362SMatthew N. Dodd };
3929ade362SMatthew N. Dodd 
401340fc10SMatthew N. Dodd static void		lm_add		(const char *, const char *, const char *);
4129ade362SMatthew N. Dodd static void		lm_free		(struct lm_list *);
4229ade362SMatthew N. Dodd static char *		lml_find	(struct lm_list *, const char *);
4329ade362SMatthew N. Dodd static struct lm_list *	lmp_find	(const char *);
4429ade362SMatthew N. Dodd static struct lm_list *	lmp_init	(char *);
4529ade362SMatthew N. Dodd 
46623b6bd2SMatthew N. Dodd #define	iseol(c)	(((c) == '#') || ((c) == '\0') || \
47623b6bd2SMatthew N. Dodd 			 ((c) == '\n') || ((c) == '\r'))
48623b6bd2SMatthew N. Dodd 
494402996dSMatthew N. Dodd int
5029ade362SMatthew N. Dodd lm_init (void)
5129ade362SMatthew N. Dodd {
5229ade362SMatthew N. Dodd 	FILE	*fp;
5329ade362SMatthew N. Dodd 	char	*cp;
541340fc10SMatthew N. Dodd 	char	*f, *t, *p, *c;
5529ade362SMatthew N. Dodd 	char	prog[MAXPATHLEN];
5629ade362SMatthew N. Dodd 	char	line[MAXPATHLEN + 2];
5729ade362SMatthew N. Dodd 
581aac1ed6SMatthew N. Dodd 	dbg("%s()", __func__);
591aac1ed6SMatthew N. Dodd 
6029ade362SMatthew N. Dodd 	TAILQ_INIT(&lmp_head);
6129ade362SMatthew N. Dodd 
6229ade362SMatthew N. Dodd 	if ((fp = fopen(_PATH_LIBMAP_CONF, "r")) == NULL)
634402996dSMatthew N. Dodd 		return (1);
6429ade362SMatthew N. Dodd 
6529ade362SMatthew N. Dodd 	p = NULL;
6629ade362SMatthew N. Dodd 	while ((cp = fgets(line, MAXPATHLEN + 1, fp)) != NULL) {
671340fc10SMatthew N. Dodd 		t = f = c = NULL;
68623b6bd2SMatthew N. Dodd 
6929ade362SMatthew N. Dodd 		/* Skip over leading space */
70623b6bd2SMatthew N. Dodd 		while (isspace(*cp)) cp++;
71623b6bd2SMatthew N. Dodd 
7229ade362SMatthew N. Dodd 		/* Found a comment or EOL */
73486089f0SAlexander Kabaev 		if (iseol(*cp)) continue;
74623b6bd2SMatthew N. Dodd 
75623b6bd2SMatthew N. Dodd 		/* Found a constraint selector */
7629ade362SMatthew N. Dodd 		if (*cp == '[') {
7729ade362SMatthew N. Dodd 			cp++;
78623b6bd2SMatthew N. Dodd 
7929ade362SMatthew N. Dodd 			/* Skip leading space */
80623b6bd2SMatthew N. Dodd 			while (isspace(*cp)) cp++;
81623b6bd2SMatthew N. Dodd 
8229ade362SMatthew N. Dodd 			/* Found comment, EOL or end of selector */
83486089f0SAlexander Kabaev 			if  (iseol(*cp) || *cp == ']')
84486089f0SAlexander Kabaev 				continue;
85623b6bd2SMatthew N. Dodd 
861340fc10SMatthew N. Dodd 			c = cp++;
8729ade362SMatthew N. Dodd 			/* Skip to end of word */
88486089f0SAlexander Kabaev 			while (!isspace(*cp) && !iseol(*cp) && *cp != ']')
89486089f0SAlexander Kabaev 				cp++;
90623b6bd2SMatthew N. Dodd 
91623b6bd2SMatthew N. Dodd 			/* Skip and zero out trailing space */
92623b6bd2SMatthew N. Dodd 			while (isspace(*cp)) *cp++ = '\0';
93623b6bd2SMatthew N. Dodd 
94623b6bd2SMatthew N. Dodd 			/* Check if there is a closing brace */
95486089f0SAlexander Kabaev 			if (*cp != ']') continue;
96623b6bd2SMatthew N. Dodd 
97623b6bd2SMatthew N. Dodd 			/* Terminate string if there was no trailing space */
9829ade362SMatthew N. Dodd 			*cp++ = '\0';
99623b6bd2SMatthew N. Dodd 
100623b6bd2SMatthew N. Dodd 			/*
101623b6bd2SMatthew N. Dodd 			 * There should be nothing except whitespace or comment
1026d5d786fSAlexander Kabaev 			  from this point to the end of the line.
103623b6bd2SMatthew N. Dodd 			 */
1046e918a4dSMax Khon 			while(isspace(*cp)) cp++;
105486089f0SAlexander Kabaev 			if (!iseol(*cp)) continue;
106623b6bd2SMatthew N. Dodd 
1071340fc10SMatthew N. Dodd 			strcpy(prog, c);
10829ade362SMatthew N. Dodd 			p = prog;
109486089f0SAlexander Kabaev 			continue;
110623b6bd2SMatthew N. Dodd 		}
111623b6bd2SMatthew N. Dodd 
112623b6bd2SMatthew N. Dodd 		/* Parse the 'from' candidate. */
113486089f0SAlexander Kabaev 		f = cp++;
114623b6bd2SMatthew N. Dodd 		while (!isspace(*cp) && !iseol(*cp)) cp++;
11529ade362SMatthew N. Dodd 
116623b6bd2SMatthew N. Dodd 		/* Skip and zero out the trailing whitespace */
117623b6bd2SMatthew N. Dodd 		while (isspace(*cp)) *cp++ = '\0';
118623b6bd2SMatthew N. Dodd 
119623b6bd2SMatthew N. Dodd 		/* Found a comment or EOL */
120486089f0SAlexander Kabaev 		if (iseol(*cp)) continue;
121623b6bd2SMatthew N. Dodd 
122623b6bd2SMatthew N. Dodd 		/* Parse 'to' mapping */
123486089f0SAlexander Kabaev 		t = cp++;
124623b6bd2SMatthew N. Dodd 		while (!isspace(*cp) && !iseol(*cp)) cp++;
125623b6bd2SMatthew N. Dodd 
126486089f0SAlexander Kabaev 		/* Skip and zero out the trailing whitespace */
127486089f0SAlexander Kabaev 		while (isspace(*cp)) *cp++ = '\0';
128486089f0SAlexander Kabaev 
129486089f0SAlexander Kabaev 		/* Should be no extra tokens at this point */
130486089f0SAlexander Kabaev 		if (!iseol(*cp)) continue;
131486089f0SAlexander Kabaev 
132486089f0SAlexander Kabaev 		*cp = '\0';
1331340fc10SMatthew N. Dodd 		lm_add(p, f, t);
13429ade362SMatthew N. Dodd 	}
135486089f0SAlexander Kabaev 	fclose(fp);
1364402996dSMatthew N. Dodd 	return (0);
13729ade362SMatthew N. Dodd }
13829ade362SMatthew N. Dodd 
13929ade362SMatthew N. Dodd static void
14029ade362SMatthew N. Dodd lm_free (struct lm_list *lml)
14129ade362SMatthew N. Dodd {
14229ade362SMatthew N. Dodd 	struct lm *lm;
14329ade362SMatthew N. Dodd 
1441aac1ed6SMatthew N. Dodd 	dbg("%s(%p)", __func__, lml);
1451aac1ed6SMatthew N. Dodd 
14629ade362SMatthew N. Dodd 	while (!TAILQ_EMPTY(lml)) {
14729ade362SMatthew N. Dodd 		lm = TAILQ_FIRST(lml);
14829ade362SMatthew N. Dodd 		TAILQ_REMOVE(lml, lm, lm_link);
14929ade362SMatthew N. Dodd 		free(lm->f);
15029ade362SMatthew N. Dodd 		free(lm->t);
15129ade362SMatthew N. Dodd 		free(lm);
15229ade362SMatthew N. Dodd 	}
15329ade362SMatthew N. Dodd 	return;
15429ade362SMatthew N. Dodd }
15529ade362SMatthew N. Dodd 
15629ade362SMatthew N. Dodd void
15729ade362SMatthew N. Dodd lm_fini (void)
15829ade362SMatthew N. Dodd {
15929ade362SMatthew N. Dodd 	struct lmp *lmp;
16029ade362SMatthew N. Dodd 
1611aac1ed6SMatthew N. Dodd 	dbg("%s()", __func__);
1621aac1ed6SMatthew N. Dodd 
16329ade362SMatthew N. Dodd 	while (!TAILQ_EMPTY(&lmp_head)) {
16429ade362SMatthew N. Dodd 		lmp = TAILQ_FIRST(&lmp_head);
16529ade362SMatthew N. Dodd 		TAILQ_REMOVE(&lmp_head, lmp, lmp_link);
16629ade362SMatthew N. Dodd 		free(lmp->p);
16729ade362SMatthew N. Dodd 		lm_free(&lmp->lml);
16829ade362SMatthew N. Dodd 		free(lmp);
16929ade362SMatthew N. Dodd 	}
17029ade362SMatthew N. Dodd 	return;
17129ade362SMatthew N. Dodd }
17229ade362SMatthew N. Dodd 
17329ade362SMatthew N. Dodd static void
1741340fc10SMatthew N. Dodd lm_add (const char *p, const char *f, const char *t)
17529ade362SMatthew N. Dodd {
17629ade362SMatthew N. Dodd 	struct lm_list *lml;
17729ade362SMatthew N. Dodd 	struct lm *lm;
17829ade362SMatthew N. Dodd 
17929ade362SMatthew N. Dodd 	if (p == NULL)
18029ade362SMatthew N. Dodd 		p = "$DEFAULT$";
18129ade362SMatthew N. Dodd 
1821aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\", \"%s\", \"%s\")", __func__, p, f, t);
1831aac1ed6SMatthew N. Dodd 
18429ade362SMatthew N. Dodd 	if ((lml = lmp_find(p)) == NULL)
1853467e8b8SMatthew N. Dodd 		lml = lmp_init(xstrdup(p));
18629ade362SMatthew N. Dodd 
1873467e8b8SMatthew N. Dodd 	lm = xmalloc(sizeof(struct lm));
1881340fc10SMatthew N. Dodd 	lm->f = xstrdup(f);
1891340fc10SMatthew N. Dodd 	lm->t = xstrdup(t);
19029ade362SMatthew N. Dodd 	TAILQ_INSERT_HEAD(lml, lm, lm_link);
19129ade362SMatthew N. Dodd }
19229ade362SMatthew N. Dodd 
19329ade362SMatthew N. Dodd char *
19429ade362SMatthew N. Dodd lm_find (const char *p, const char *f)
19529ade362SMatthew N. Dodd {
19629ade362SMatthew N. Dodd 	struct lm_list *lml;
19729ade362SMatthew N. Dodd 	char *t;
19829ade362SMatthew N. Dodd 
1991aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\", \"%s\")", __func__, p, f);
2001aac1ed6SMatthew N. Dodd 
20129ade362SMatthew N. Dodd 	if (p != NULL && (lml = lmp_find(p)) != NULL) {
20229ade362SMatthew N. Dodd 		t = lml_find(lml, f);
2033467e8b8SMatthew N. Dodd 		if (t != NULL) {
2043467e8b8SMatthew N. Dodd 			/*
2053467e8b8SMatthew N. Dodd 			 * Add a global mapping if we have
2063467e8b8SMatthew N. Dodd 			 * a successful constrained match.
2073467e8b8SMatthew N. Dodd 			 */
2081340fc10SMatthew N. Dodd 			lm_add(NULL, f, t);
20929ade362SMatthew N. Dodd 			return (t);
21029ade362SMatthew N. Dodd 		}
2113467e8b8SMatthew N. Dodd 	}
21229ade362SMatthew N. Dodd 	lml = lmp_find("$DEFAULT$");
21329ade362SMatthew N. Dodd 	if (lml != NULL)
21429ade362SMatthew N. Dodd 		return (lml_find(lml, f));
21529ade362SMatthew N. Dodd 	else
21629ade362SMatthew N. Dodd 		return (NULL);
21729ade362SMatthew N. Dodd }
21829ade362SMatthew N. Dodd 
219c905e45dSPeter Wemm #ifdef COMPAT_32BIT
220c905e45dSPeter Wemm char *
221c905e45dSPeter Wemm lm_findn (const char *p, const char *f, const int n)
222c905e45dSPeter Wemm {
223c905e45dSPeter Wemm 	char pathbuf[64], *s, *t;
224c905e45dSPeter Wemm 
225c905e45dSPeter Wemm 	if (n < sizeof(pathbuf) - 1) {
226c905e45dSPeter Wemm 		memcpy(pathbuf, f, n);
227c905e45dSPeter Wemm 		pathbuf[n] = '\0';
228c905e45dSPeter Wemm 		s = pathbuf;
229c905e45dSPeter Wemm 	} else {
230c905e45dSPeter Wemm 		s = xmalloc(n + 1);
231c905e45dSPeter Wemm 		strcpy(s, f);
232c905e45dSPeter Wemm 	}
233c905e45dSPeter Wemm 	t = lm_find(p, s);
234c905e45dSPeter Wemm 	if (s != pathbuf)
235c905e45dSPeter Wemm 		free(s);
236c905e45dSPeter Wemm 	return (t);
237c905e45dSPeter Wemm }
238c905e45dSPeter Wemm #endif
239c905e45dSPeter Wemm 
24029ade362SMatthew N. Dodd static char *
24129ade362SMatthew N. Dodd lml_find (struct lm_list *lmh, const char *f)
24229ade362SMatthew N. Dodd {
24329ade362SMatthew N. Dodd 	struct lm *lm;
24429ade362SMatthew N. Dodd 
2451aac1ed6SMatthew N. Dodd 	dbg("%s(%p, \"%s\")", __func__, lmh, f);
2461aac1ed6SMatthew N. Dodd 
24729ade362SMatthew N. Dodd 	TAILQ_FOREACH(lm, lmh, lm_link)
248c905e45dSPeter Wemm 		if (strcmp(f, lm->f) == 0)
24929ade362SMatthew N. Dodd 			return (lm->t);
25029ade362SMatthew N. Dodd 	return NULL;
25129ade362SMatthew N. Dodd }
25229ade362SMatthew N. Dodd 
25329ade362SMatthew N. Dodd static struct lm_list *
25429ade362SMatthew N. Dodd lmp_find (const char *n)
25529ade362SMatthew N. Dodd {
25629ade362SMatthew N. Dodd 	struct lmp *lmp;
25729ade362SMatthew N. Dodd 
2581aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\")", __func__, n);
2591aac1ed6SMatthew N. Dodd 
26029ade362SMatthew N. Dodd 	TAILQ_FOREACH(lmp, &lmp_head, lmp_link)
261c905e45dSPeter Wemm 		if (strcmp(n, lmp->p) == 0)
26229ade362SMatthew N. Dodd 			return (&lmp->lml);
26329ade362SMatthew N. Dodd 	return (NULL);
26429ade362SMatthew N. Dodd }
26529ade362SMatthew N. Dodd 
26629ade362SMatthew N. Dodd static struct lm_list *
26729ade362SMatthew N. Dodd lmp_init (char *n)
26829ade362SMatthew N. Dodd {
26929ade362SMatthew N. Dodd 	struct lmp *lmp;
27029ade362SMatthew N. Dodd 
2711aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\")", __func__, n);
2721aac1ed6SMatthew N. Dodd 
2733467e8b8SMatthew N. Dodd 	lmp = xmalloc(sizeof(struct lmp));
27429ade362SMatthew N. Dodd 	lmp->p = n;
27529ade362SMatthew N. Dodd 	TAILQ_INIT(&lmp->lml);
27629ade362SMatthew N. Dodd 	TAILQ_INSERT_HEAD(&lmp_head, lmp, lmp_link);
27729ade362SMatthew N. Dodd 
27829ade362SMatthew N. Dodd 	return (&lmp->lml);
27929ade362SMatthew N. Dodd }
280