sod.c revision 1.19
1/*	$OpenBSD: sod.c,v 1.19 2003/10/26 23:23:12 drahn Exp $	*/
2
3/*
4 * Copyright (c) 1993 Paul Kranenburg
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by Paul Kranenburg.
18 * 4. The name of the author may not be used to endorse or promote products
19 *    derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include <sys/types.h>
35#include <sys/syslimits.h>
36#include <stdio.h>
37#include <fcntl.h>
38#include <nlist.h>
39#include <link.h>
40#include <limits.h>
41#include <machine/exec.h>
42#include <sys/mman.h>
43#include <string.h>
44#include <stdlib.h>
45#include <unistd.h>
46#include <syscall.h>
47
48#include "archdep.h"
49#include "util.h"
50#include "sod.h"
51
52int _dl_hinthash(char *cp, int vmajor, int vminor);
53void _dl_maphints(void);
54
55/*
56 * Populate sod struct for dlopen's call to map_object
57 */
58void
59_dl_build_sod(const char *name, struct sod *sodp)
60{
61	unsigned int	tuplet;
62	int		major, minor;
63	char		*realname, *tok, *etok, *cp;
64
65	/* default is an absolute or relative path */
66	sodp->sod_name = (long)_dl_strdup(name);    /* strtok is destructive */
67	sodp->sod_library = 0;
68	sodp->sod_major = sodp->sod_minor = 0;
69
70	/* does it look like /^lib/ ? */
71	if (_dl_strncmp((char *)sodp->sod_name, "lib", 3) != 0)
72		return;
73
74	/* is this a filename? */
75	if (_dl_strchr((char *)sodp->sod_name, '/'))
76		return;
77
78	/* skip over 'lib' */
79	cp = (char *)sodp->sod_name + 3;
80
81	/* dot guardian */
82	if ((_dl_strchr(cp, '.') == NULL) || (*(cp+_dl_strlen(cp)-1) == '.'))
83		return;
84
85	/* default */
86	major = minor = -1;
87
88	realname = NULL;
89	/* loop through name - parse skipping name */
90	for (tuplet = 0; (tok = strsep(&cp, ".")) != NULL; tuplet++) {
91		switch (tuplet) {
92		case 0:
93			/* removed 'lib' and extensions from name */
94			realname = tok;
95			break;
96		case 1:
97			/* 'so' extension */
98			if (_dl_strcmp(tok, "so") != 0) {
99				/*
100				 * filenames such as libX-A.B.so.X.Y
101				 * screw this up, decrement tuplet
102				 * and try to continue.
103				 */
104				 tuplet--;
105			}
106			break;
107		case 2:
108			/* major version extension */
109			major = _dl_strtol(tok, &etok, 10);
110			if (*tok == '\0' || *etok != '\0')
111				goto backout;
112			break;
113		case 3:
114			/* minor version extension */
115			minor = _dl_strtol(tok, &etok, 10);
116			if (*tok == '\0' || *etok != '\0')
117				goto backout;
118			break;
119		/* if we get here, it must be weird */
120		default:
121			goto backout;
122		}
123	}
124	if (realname == NULL)
125		goto backout;
126	cp = (char *)sodp->sod_name;
127	sodp->sod_name = (long)_dl_strdup(realname);
128	_dl_free(cp);
129	sodp->sod_library = 1;
130	sodp->sod_major = major;
131	sodp->sod_minor = minor;
132	return;
133
134backout:
135	_dl_free((char *)sodp->sod_name);
136	sodp->sod_name = (long)_dl_strdup(name);
137}
138
139static struct hints_header	*hheader = NULL;
140static struct hints_bucket	*hbuckets;
141static char			*hstrtab;
142char				*_dl_hint_search_path = NULL;
143
144#define HINTS_VALID (hheader != NULL && hheader != (struct hints_header *)-1)
145
146void
147_dl_maphints(void)
148{
149	struct stat	sb;
150	caddr_t		addr = MAP_FAILED;
151	long		hsize = 0;
152	int		hfd;
153
154	if ((hfd = _dl_open(_PATH_LD_HINTS, O_RDONLY)) < 0)
155		goto bad_hints;
156
157	if (_dl_fstat(hfd, &sb) != 0 || !S_ISREG(sb.st_mode) ||
158	    sb.st_size < sizeof(struct hints_header) || sb.st_size > LONG_MAX)
159		goto bad_hints;
160
161	hsize = (long)sb.st_size;
162	addr = (void *)_dl_mmap(0, hsize, PROT_READ, MAP_PRIVATE, hfd, 0);
163	if (addr == MAP_FAILED)
164		goto bad_hints;
165
166	hheader = (struct hints_header *)addr;
167	if (HH_BADMAG(*hheader) || hheader->hh_ehints > hsize)
168		goto bad_hints;
169
170	if (hheader->hh_version != LD_HINTS_VERSION_1 &&
171	    hheader->hh_version != LD_HINTS_VERSION_2)
172		goto bad_hints;
173
174	hbuckets = (struct hints_bucket *)(addr + hheader->hh_hashtab);
175	hstrtab = (char *)(addr + hheader->hh_strtab);
176	if (hheader->hh_version >= LD_HINTS_VERSION_2)
177		_dl_hint_search_path = hstrtab + hheader->hh_dirlist;
178
179	/* close the file descriptor, leaving the hints mapped */
180	_dl_close(hfd);
181
182	return;
183
184bad_hints:
185	if (addr != MAP_FAILED)
186		_dl_munmap(addr, hsize);
187	if (hfd != -1)
188		_dl_close(hfd);
189	hheader = (struct hints_header *)-1;
190}
191
192char *
193_dl_findhint(char *name, int major, int minor, char *prefered_path)
194{
195	struct hints_bucket	*bp;
196
197	/*
198	 * If not mapped, and we have not tried before, try to map the
199	 * hints, if previous attempts failed hheader is -1 and we
200	 * do not wish to retry it.
201	 */
202	if (hheader == NULL)
203		_dl_maphints();
204
205	/* if it failed to map, return failure */
206	if (!(HINTS_VALID))
207		return NULL;
208
209	bp = hbuckets + (_dl_hinthash(name, major, minor) % hheader->hh_nbucket);
210
211	while (1) {
212		/* Sanity check */
213		if (bp->hi_namex >= hheader->hh_strtab_sz) {
214			_dl_printf("Bad name index: %#x\n", bp->hi_namex);
215			_dl_exit(7);
216			break;
217		}
218		if (bp->hi_pathx >= hheader->hh_strtab_sz) {
219			_dl_printf("Bad path index: %#x\n", bp->hi_pathx);
220			_dl_exit(7);
221			break;
222		}
223
224		if (_dl_strcmp(name, hstrtab + bp->hi_namex) == 0) {
225			/* It's `name', check version numbers */
226			if (bp->hi_major == major &&
227			    (bp->hi_ndewey < 2 || bp->hi_minor >= minor)) {
228				if (prefered_path == NULL ||
229				    _dl_strncmp(prefered_path,
230				    hstrtab + bp->hi_pathx,
231				    _dl_strlen(prefered_path)) == 0)
232					return hstrtab + bp->hi_pathx;
233			}
234		}
235
236		if (bp->hi_next == -1)
237			break;
238
239		/* Move on to next in bucket */
240		bp = &hbuckets[bp->hi_next];
241	}
242
243	/* No hints available for name */
244	return NULL;
245}
246
247int
248_dl_hinthash(char *cp, int vmajor, int vminor)
249{
250	int	k = 0;
251
252	while (*cp)
253		k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
254
255	k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff;
256	if (hheader->hh_version == LD_HINTS_VERSION_1)
257		k = (((k << 1) + (k >> 14)) ^ (vminor*167)) & 0x3fff;
258
259	return k;
260}
261