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