xref: /dragonfly/lib/libc/gen/nlist.c (revision 19fe1c42)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	$FreeBSD: src/lib/libc/gen/nlist.c,v 1.12.2.1 2001/07/11 23:59:09 obrien Exp $
34  *	$DragonFly: src/lib/libc/gen/nlist.c,v 1.6 2005/04/26 08:21:34 joerg Exp $
35  *
36  * @(#)nlist.c	8.1 (Berkeley) 6/4/93
37  */
38 
39 #include "namespace.h"
40 #include <sys/param.h>
41 #include <sys/mman.h>
42 #include <sys/stat.h>
43 #include <sys/file.h>
44 
45 #include <errno.h>
46 #include <a.out.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include "un-namespace.h"
51 
52 #define _NLIST_DO_AOUT
53 #define _NLIST_DO_ELF
54 
55 #ifdef _NLIST_DO_ELF
56 #include <machine/elf.h>
57 #include <elf-hints.h>
58 #endif
59 
60 int	__fdnlist(int, struct nlist *);
61 int	__aout_fdnlist(int, struct nlist *);
62 int	__elf_fdnlist(int, struct nlist *);
63 
64 int
65 nlist(const char *name, struct nlist *list)
66 {
67 	int fd, n;
68 
69 	fd = _open(name, O_RDONLY, 0);
70 	if (fd < 0)
71 		return (-1);
72 	n = __fdnlist(fd, list);
73 	_close(fd);
74 	return (n);
75 }
76 
77 static struct nlist_handlers {
78 	int	(*fn)(int fd, struct nlist *list);
79 } nlist_fn[] = {
80 #ifdef _NLIST_DO_AOUT
81 	{ __aout_fdnlist },
82 #endif
83 #ifdef _NLIST_DO_ELF
84 	{ __elf_fdnlist },
85 #endif
86 };
87 
88 int
89 __fdnlist(int fd, struct nlist *list)
90 {
91 	int n = -1;
92 	size_t i;
93 
94 	for (i = 0; i < sizeof(nlist_fn) / sizeof(nlist_fn[0]); i++) {
95 		n = (nlist_fn[i].fn)(fd, list);
96 		if (n != -1)
97 			break;
98 	}
99 	return (n);
100 }
101 
102 #define	ISLAST(p)	(p->n_un.n_name == 0 || p->n_un.n_name[0] == 0)
103 
104 #ifdef _NLIST_DO_AOUT
105 int
106 __aout_fdnlist(int fd, struct nlist *list)
107 {
108 	struct nlist *p, *symtab;
109 	caddr_t strtab, a_out_mmap;
110 	off_t stroff, symoff;
111 	u_long symsize;
112 	int nent;
113 	struct exec * exec;
114 	struct stat st;
115 
116 	/* check that file is at least as large as struct exec! */
117 	if ((_fstat(fd, &st) < 0) || (st.st_size < sizeof(struct exec)))
118 		return (-1);
119 
120 	/* Check for files too large to mmap. */
121 	if (st.st_size > SIZE_T_MAX) {
122 		errno = EFBIG;
123 		return (-1);
124 	}
125 
126 	/*
127 	 * Map the whole a.out file into our address space.
128 	 * We then find the string table withing this area.
129 	 * We do not just mmap the string table, as it probably
130 	 * does not start at a page boundary - we save ourselves a
131 	 * lot of nastiness by mmapping the whole file.
132 	 *
133 	 * This gives us an easy way to randomly access all the strings,
134 	 * without making the memory allocation permanent as with
135 	 * malloc/free (i.e., munmap will return it to the system).
136 	 */
137 	a_out_mmap = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t)0);
138 	if (a_out_mmap == MAP_FAILED)
139 		return (-1);
140 
141 	exec = (struct exec *)a_out_mmap;
142 	if (N_BADMAG(*exec)) {
143 		munmap(a_out_mmap, (size_t)st.st_size);
144 		return (-1);
145 	}
146 
147 	symoff = N_SYMOFF(*exec);
148 	symsize = exec->a_syms;
149 	stroff = symoff + symsize;
150 
151 	/* find the string table in our mmapped area */
152 	strtab = a_out_mmap + stroff;
153 	symtab = (struct nlist *)(a_out_mmap + symoff);
154 
155 	/*
156 	 * clean out any left-over information for all valid entries.
157 	 * Type and value defined to be 0 if not found; historical
158 	 * versions cleared other and desc as well.  Also figure out
159 	 * the largest string length so don't read any more of the
160 	 * string table than we have to.
161 	 *
162 	 * XXX clearing anything other than n_type and n_value violates
163 	 * the semantics given in the man page.
164 	 */
165 	nent = 0;
166 	for (p = list; !ISLAST(p); ++p) {
167 		p->n_type = 0;
168 		p->n_other = 0;
169 		p->n_desc = 0;
170 		p->n_value = 0;
171 		++nent;
172 	}
173 
174 	while (symsize > 0) {
175 		int soff;
176 
177 		symsize-= sizeof(struct nlist);
178 		soff = symtab->n_un.n_strx;
179 
180 
181 		if (soff != 0 && (symtab->n_type & N_STAB) == 0)
182 			for (p = list; !ISLAST(p); p++)
183 				if (!strcmp(&strtab[soff], p->n_un.n_name)) {
184 					p->n_value = symtab->n_value;
185 					p->n_type = symtab->n_type;
186 					p->n_desc = symtab->n_desc;
187 					p->n_other = symtab->n_other;
188 					if (--nent <= 0)
189 						break;
190 				}
191 		symtab++;
192 	}
193 	munmap(a_out_mmap, (size_t)st.st_size);
194 	return (nent);
195 }
196 #endif
197 
198 #ifdef _NLIST_DO_ELF
199 static void	elf_sym_to_nlist(struct nlist *, Elf_Sym *, Elf_Shdr *, int);
200 
201 /*
202  * __elf_is_okay__ - Determine if ehdr really
203  * is ELF and valid for the target platform.
204  *
205  * WARNING:  This is NOT a ELF ABI function and
206  * as such it's use should be restricted.
207  */
208 static int
209 __elf_is_okay__(Elf_Ehdr *ehdr)
210 {
211 	int retval = 0;
212 	/*
213 	 * We need to check magic, class size, endianess,
214 	 * and version before we look at the rest of the
215 	 * Elf_Ehdr structure.  These few elements are
216 	 * represented in a machine independant fashion.
217 	 */
218 	if (IS_ELF(*ehdr) &&
219 	    ehdr->e_ident[EI_CLASS] == ELF_TARG_CLASS &&
220 	    ehdr->e_ident[EI_DATA] == ELF_TARG_DATA &&
221 	    ehdr->e_ident[EI_VERSION] == ELF_TARG_VER) {
222 
223 		/* Now check the machine dependant header */
224 		if (ehdr->e_machine == ELF_TARG_MACH &&
225 		    ehdr->e_version == ELF_TARG_VER)
226 			retval = 1;
227 	}
228 	return retval;
229 }
230 
231 int
232 __elf_fdnlist(int fd, struct nlist *list)
233 {
234 	struct nlist *p;
235 	Elf_Off symoff = 0, symstroff = 0;
236 	Elf_Word symsize = 0, symstrsize = 0;
237 	Elf_Sword cc, i;
238 	int nent = -1;
239 	int errsave;
240 	Elf_Sym sbuf[1024];
241 	Elf_Sym *s;
242 	Elf_Ehdr ehdr;
243 	char *strtab = NULL;
244 	Elf_Shdr *shdr = NULL;
245 	Elf_Word shdr_size;
246 	void *base;
247 	struct stat st;
248 
249 	/* Make sure obj is OK */
250 	if (lseek(fd, (off_t)0, SEEK_SET) == -1 ||
251 	    _read(fd, &ehdr, sizeof(Elf_Ehdr)) != sizeof(Elf_Ehdr) ||
252 	    !__elf_is_okay__(&ehdr) ||
253 	    _fstat(fd, &st) < 0)
254 		return (-1);
255 
256 	/* calculate section header table size */
257 	shdr_size = ehdr.e_shentsize * ehdr.e_shnum;
258 
259 	/* Make sure it's not too big to mmap */
260 	if (shdr_size > SIZE_T_MAX) {
261 		errno = EFBIG;
262 		return (-1);
263 	}
264 
265 	/* mmap section header table */
266 	base = mmap(NULL, (size_t)shdr_size, PROT_READ, 0, fd,
267 	    (off_t)ehdr.e_shoff);
268 	if (base == MAP_FAILED)
269 		return (-1);
270 	shdr = (Elf_Shdr *)base;
271 
272 	/*
273 	 * Find the symbol table entry and it's corresponding
274 	 * string table entry.	Version 1.1 of the ABI states
275 	 * that there is only one symbol table but that this
276 	 * could change in the future.
277 	 */
278 	for (i = 0; i < ehdr.e_shnum; i++) {
279 		if (shdr[i].sh_type == SHT_SYMTAB) {
280 			symoff = shdr[i].sh_offset;
281 			symsize = shdr[i].sh_size;
282 			symstroff = shdr[shdr[i].sh_link].sh_offset;
283 			symstrsize = shdr[shdr[i].sh_link].sh_size;
284 			break;
285 		}
286 	}
287 
288 	/* Check for files too large to mmap. */
289 	if (symstrsize > SIZE_T_MAX) {
290 		errno = EFBIG;
291 		goto done;
292 	}
293 	/*
294 	 * Map string table into our address space.  This gives us
295 	 * an easy way to randomly access all the strings, without
296 	 * making the memory allocation permanent as with malloc/free
297 	 * (i.e., munmap will return it to the system).
298 	 */
299 	base = mmap(NULL, (size_t)symstrsize, PROT_READ, 0, fd,
300 	    (off_t)symstroff);
301 	if (base == MAP_FAILED)
302 		goto done;
303 	strtab = (char *)base;
304 
305 	/*
306 	 * clean out any left-over information for all valid entries.
307 	 * Type and value defined to be 0 if not found; historical
308 	 * versions cleared other and desc as well.  Also figure out
309 	 * the largest string length so don't read any more of the
310 	 * string table than we have to.
311 	 *
312 	 * XXX clearing anything other than n_type and n_value violates
313 	 * the semantics given in the man page.
314 	 */
315 	nent = 0;
316 	for (p = list; !ISLAST(p); ++p) {
317 		p->n_type = 0;
318 		p->n_other = 0;
319 		p->n_desc = 0;
320 		p->n_value = 0;
321 		++nent;
322 	}
323 
324 	/* Don't process any further if object is stripped. */
325 	if (symoff == 0)
326 		goto done;
327 
328 	if (lseek(fd, (off_t) symoff, SEEK_SET) == -1) {
329 		nent = -1;
330 		goto done;
331 	}
332 
333 	while (symsize > 0 && nent > 0) {
334 		cc = MIN(symsize, sizeof(sbuf));
335 		if (_read(fd, sbuf, cc) != cc)
336 			break;
337 		symsize -= cc;
338 		for (s = sbuf; cc > 0 && nent > 0; ++s, cc -= sizeof(*s)) {
339 			char *name;
340 			struct nlist *p_local;
341 
342 			name = strtab + s->st_name;
343 			if (name[0] == '\0')
344 				continue;
345 			for (p_local = list; !ISLAST(p_local); p_local++) {
346 				if ((p_local->n_un.n_name[0] == '_' &&
347 				    strcmp(name, p_local->n_un.n_name+1) == 0)
348 				    || strcmp(name, p_local->n_un.n_name) == 0) {
349 					elf_sym_to_nlist(p_local, s, shdr,
350 					    ehdr.e_shnum);
351 					if (--nent <= 0)
352 						break;
353 				}
354 			}
355 		}
356 	}
357 done:
358 	errsave = errno;
359 	if (strtab != NULL)
360 		munmap(strtab, symstrsize);
361 	if (shdr != NULL)
362 		munmap(shdr, shdr_size);
363 	errno = errsave;
364 	return (nent);
365 }
366 
367 /*
368  * Convert an Elf_Sym into an nlist structure.  This fills in only the
369  * n_value and n_type members.
370  */
371 static void
372 elf_sym_to_nlist(struct nlist *nl, Elf_Sym *s, Elf_Shdr *shdr, int shnum)
373 {
374 	nl->n_value = s->st_value;
375 
376 	switch (s->st_shndx) {
377 	case SHN_UNDEF:
378 	case SHN_COMMON:
379 		nl->n_type = N_UNDF;
380 		break;
381 	case SHN_ABS:
382 		nl->n_type = ELF_ST_TYPE(s->st_info) == STT_FILE ?
383 		    N_FN : N_ABS;
384 		break;
385 	default:
386 		if (s->st_shndx >= shnum)
387 			nl->n_type = N_UNDF;
388 		else {
389 			Elf_Shdr *sh = shdr + s->st_shndx;
390 
391 			nl->n_type = sh->sh_type == SHT_PROGBITS ?
392 			    (sh->sh_flags & SHF_WRITE ? N_DATA : N_TEXT) :
393 			    (sh->sh_type == SHT_NOBITS ? N_BSS : N_UNDF);
394 		}
395 		break;
396 	}
397 
398 	if (ELF_ST_BIND(s->st_info) == STB_GLOBAL ||
399 	    ELF_ST_BIND(s->st_info) == STB_WEAK)
400 		nl->n_type |= N_EXT;
401 }
402 #endif /* _NLIST_DO_ELF */
403