xref: /dragonfly/lib/libc/gen/nlist.c (revision c69bf40f)
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	$FreeBSD: src/lib/libc/gen/nlist.c,v 1.12.2.1 2001/07/11 23:59:09 obrien Exp $
30  *
31  * @(#)nlist.c	8.1 (Berkeley) 6/4/93
32  */
33 
34 #include "namespace.h"
35 #include <sys/param.h>
36 #include <sys/mman.h>
37 #include <sys/stat.h>
38 #include <sys/file.h>
39 
40 #include <errno.h>
41 #include <a.out.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include "un-namespace.h"
46 
47 #define _NLIST_DO_AOUT
48 #define _NLIST_DO_ELF
49 
50 #ifdef _NLIST_DO_ELF
51 #include <machine/elf.h>
52 #include <elf-hints.h>
53 #endif
54 
55 int	__fdnlist(int, struct nlist *);
56 int	__aout_fdnlist(int, struct nlist *);
57 int	__elf_fdnlist(int, struct nlist *);
58 
59 int
60 nlist(const char *name, struct nlist *list)
61 {
62 	int fd, n;
63 
64 	fd = _open(name, O_RDONLY | O_CLOEXEC, 0);
65 	if (fd < 0)
66 		return (-1);
67 	n = __fdnlist(fd, list);
68 	_close(fd);
69 	return (n);
70 }
71 
72 static struct nlist_handlers {
73 	int	(*fn)(int fd, struct nlist *list);
74 } nlist_fn[] = {
75 #ifdef _NLIST_DO_AOUT
76 	{ __aout_fdnlist },
77 #endif
78 #ifdef _NLIST_DO_ELF
79 	{ __elf_fdnlist },
80 #endif
81 };
82 
83 int
84 __fdnlist(int fd, struct nlist *list)
85 {
86 	int n = -1;
87 	size_t i;
88 
89 	for (i = 0; i < NELEM(nlist_fn); i++) {
90 		n = (nlist_fn[i].fn)(fd, list);
91 		if (n != -1)
92 			break;
93 	}
94 	return (n);
95 }
96 
97 #define	ISLAST(p)	(p->n_un.n_name == 0 || p->n_un.n_name[0] == 0)
98 
99 #ifdef _NLIST_DO_AOUT
100 int
101 __aout_fdnlist(int fd, struct nlist *list)
102 {
103 	struct nlist *p, *symtab;
104 	caddr_t strtab, a_out_mmap;
105 	off_t stroff, symoff;
106 	u_long symsize;
107 	int nent;
108 	struct exec * exec;
109 	struct stat st;
110 
111 	/* check that file is at least as large as struct exec! */
112 	if ((_fstat(fd, &st) < 0) || (st.st_size < sizeof(struct exec)))
113 		return (-1);
114 
115 	/* Check for files too large to mmap. */
116 	if (st.st_size > SIZE_T_MAX) {
117 		errno = EFBIG;
118 		return (-1);
119 	}
120 
121 	/*
122 	 * Map the whole a.out file into our address space.
123 	 * We then find the string table withing this area.
124 	 * We do not just mmap the string table, as it probably
125 	 * does not start at a page boundary - we save ourselves a
126 	 * lot of nastiness by mmapping the whole file.
127 	 *
128 	 * This gives us an easy way to randomly access all the strings,
129 	 * without making the memory allocation permanent as with
130 	 * malloc/free (i.e., munmap will return it to the system).
131 	 */
132 	a_out_mmap = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t)0);
133 	if (a_out_mmap == MAP_FAILED)
134 		return (-1);
135 
136 	exec = (struct exec *)a_out_mmap;
137 	if (N_BADMAG(*exec)) {
138 		munmap(a_out_mmap, (size_t)st.st_size);
139 		return (-1);
140 	}
141 
142 	symoff = N_SYMOFF(*exec);
143 	symsize = exec->a_syms;
144 	stroff = symoff + symsize;
145 
146 	/* find the string table in our mmapped area */
147 	strtab = a_out_mmap + stroff;
148 	symtab = (struct nlist *)(a_out_mmap + symoff);
149 
150 	/*
151 	 * clean out any left-over information for all valid entries.
152 	 * Type and value defined to be 0 if not found; historical
153 	 * versions cleared other and desc as well.  Also figure out
154 	 * the largest string length so don't read any more of the
155 	 * string table than we have to.
156 	 *
157 	 * XXX clearing anything other than n_type and n_value violates
158 	 * the semantics given in the man page.
159 	 */
160 	nent = 0;
161 	for (p = list; !ISLAST(p); ++p) {
162 		p->n_type = 0;
163 		p->n_other = 0;
164 		p->n_desc = 0;
165 		p->n_value = 0;
166 		++nent;
167 	}
168 
169 	while (symsize > 0) {
170 		int soff;
171 
172 		symsize-= sizeof(struct nlist);
173 		soff = symtab->n_un.n_strx;
174 
175 
176 		if (soff != 0 && (symtab->n_type & N_STAB) == 0)
177 			for (p = list; !ISLAST(p); p++)
178 				if (!strcmp(&strtab[soff], p->n_un.n_name)) {
179 					p->n_value = symtab->n_value;
180 					p->n_type = symtab->n_type;
181 					p->n_desc = symtab->n_desc;
182 					p->n_other = symtab->n_other;
183 					if (--nent <= 0)
184 						break;
185 				}
186 		symtab++;
187 	}
188 	munmap(a_out_mmap, (size_t)st.st_size);
189 	return (nent);
190 }
191 #endif
192 
193 #ifdef _NLIST_DO_ELF
194 static void	elf_sym_to_nlist(struct nlist *, Elf_Sym *, Elf_Shdr *, int);
195 
196 /*
197  * __elf_is_okay__ - Determine if ehdr really
198  * is ELF and valid for the target platform.
199  *
200  * WARNING:  This is NOT a ELF ABI function and
201  * as such it's use should be restricted.
202  */
203 static int
204 __elf_is_okay__(Elf_Ehdr *ehdr)
205 {
206 	int retval = 0;
207 	/*
208 	 * We need to check magic, class size, endianess,
209 	 * and version before we look at the rest of the
210 	 * Elf_Ehdr structure.  These few elements are
211 	 * represented in a machine independant fashion.
212 	 */
213 	if (IS_ELF(*ehdr) &&
214 	    ehdr->e_ident[EI_CLASS] == ELF_TARG_CLASS &&
215 	    ehdr->e_ident[EI_DATA] == ELF_TARG_DATA &&
216 	    ehdr->e_ident[EI_VERSION] == ELF_TARG_VER) {
217 
218 		/* Now check the machine dependant header */
219 		if (ehdr->e_machine == ELF_TARG_MACH &&
220 		    ehdr->e_version == ELF_TARG_VER)
221 			retval = 1;
222 	}
223 	return retval;
224 }
225 
226 int
227 __elf_fdnlist(int fd, struct nlist *list)
228 {
229 	struct nlist *p;
230 	Elf_Off symoff = 0, symstroff = 0;
231 	Elf_Word symsize = 0, symstrsize = 0;
232 	Elf_Sword cc, i;
233 	int nent = -1;
234 	int errsave;
235 	Elf_Sym sbuf[1024];
236 	Elf_Sym *s;
237 	Elf_Ehdr ehdr;
238 	char *strtab = NULL;
239 	Elf_Shdr *shdr = NULL;
240 	Elf_Word shdr_size;
241 	void *base;
242 	struct stat st;
243 
244 	/* Make sure obj is OK */
245 	if (lseek(fd, (off_t)0, SEEK_SET) == -1 ||
246 	    _read(fd, &ehdr, sizeof(Elf_Ehdr)) != sizeof(Elf_Ehdr) ||
247 	    !__elf_is_okay__(&ehdr) ||
248 	    _fstat(fd, &st) < 0)
249 		return (-1);
250 
251 	/* calculate section header table size */
252 	shdr_size = ehdr.e_shentsize * ehdr.e_shnum;
253 
254 #ifndef __x86_64__
255 	/* Make sure it's not too big to mmap */
256 	if (shdr_size > SIZE_T_MAX) {
257 		errno = EFBIG;
258 		return (-1);
259 	}
260 #endif
261 
262 	/* mmap section header table */
263 	base = mmap(NULL, (size_t)shdr_size, PROT_READ, 0, fd,
264 	    (off_t)ehdr.e_shoff);
265 	if (base == MAP_FAILED)
266 		return (-1);
267 	shdr = (Elf_Shdr *)base;
268 
269 	/*
270 	 * Find the symbol table entry and it's corresponding
271 	 * string table entry.	Version 1.1 of the ABI states
272 	 * that there is only one symbol table but that this
273 	 * could change in the future.
274 	 */
275 	for (i = 0; i < ehdr.e_shnum; i++) {
276 		if (shdr[i].sh_type == SHT_SYMTAB) {
277 			symoff = shdr[i].sh_offset;
278 			symsize = shdr[i].sh_size;
279 			symstroff = shdr[shdr[i].sh_link].sh_offset;
280 			symstrsize = shdr[shdr[i].sh_link].sh_size;
281 			break;
282 		}
283 	}
284 
285 #ifndef __x86_64__
286 	/* Check for files too large to mmap. */
287 	if (symstrsize > SIZE_T_MAX) {
288 		errno = EFBIG;
289 		goto done;
290 	}
291 #endif
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