xref: /freebsd/lib/libc/gen/nlist.c (revision f9218d3d4fd34f082473b3a021c6d4d109fb47cf)
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 
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)nlist.c	8.1 (Berkeley) 6/4/93";
36 #endif /* LIBC_SCCS and not lint */
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include "namespace.h"
41 #include <sys/param.h>
42 #include <sys/mman.h>
43 #include <sys/stat.h>
44 #include <sys/file.h>
45 #include <arpa/inet.h>
46 
47 #include <errno.h>
48 #include <a.out.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include "un-namespace.h"
53 
54 #define _NLIST_DO_AOUT
55 #define _NLIST_DO_ELF
56 
57 #ifdef _NLIST_DO_ELF
58 #include <machine/elf.h>
59 #include <elf-hints.h>
60 #endif
61 
62 int __fdnlist(int, struct nlist *);
63 int __aout_fdnlist(int, struct nlist *);
64 int __elf_fdnlist(int, struct nlist *);
65 
66 int
67 nlist(name, list)
68 	const char *name;
69 	struct nlist *list;
70 {
71 	int fd, n;
72 
73 	fd = _open(name, O_RDONLY, 0);
74 	if (fd < 0)
75 		return (-1);
76 	n = __fdnlist(fd, list);
77 	(void)_close(fd);
78 	return (n);
79 }
80 
81 static struct nlist_handlers {
82 	int	(*fn)(int fd, struct nlist *list);
83 } nlist_fn[] = {
84 #ifdef _NLIST_DO_AOUT
85 	{ __aout_fdnlist },
86 #endif
87 #ifdef _NLIST_DO_ELF
88 	{ __elf_fdnlist },
89 #endif
90 };
91 
92 int
93 __fdnlist(fd, list)
94 	int fd;
95 	struct nlist *list;
96 {
97 	int n = -1, i;
98 
99 	for (i = 0; i < sizeof(nlist_fn) / sizeof(nlist_fn[0]); i++) {
100 		n = (nlist_fn[i].fn)(fd, list);
101 		if (n != -1)
102 			break;
103 	}
104 	return (n);
105 }
106 
107 #define	ISLAST(p)	(p->n_un.n_name == 0 || p->n_un.n_name[0] == 0)
108 
109 #ifdef _NLIST_DO_AOUT
110 int
111 __aout_fdnlist(fd, list)
112 	int fd;
113 	struct nlist *list;
114 {
115 	struct nlist *p, *symtab;
116 	caddr_t strtab, a_out_mmap;
117 	off_t stroff, symoff;
118 	u_long symsize;
119 	int nent;
120 	struct exec * exec;
121 	struct stat st;
122 
123 	/* check that file is at least as large as struct exec! */
124 	if ((_fstat(fd, &st) < 0) || (st.st_size < sizeof(struct exec)))
125 		return (-1);
126 
127 	/* Check for files too large to mmap. */
128 	if (st.st_size > SIZE_T_MAX) {
129 		errno = EFBIG;
130 		return (-1);
131 	}
132 
133 	/*
134 	 * Map the whole a.out file into our address space.
135 	 * We then find the string table withing this area.
136 	 * We do not just mmap the string table, as it probably
137 	 * does not start at a page boundary - we save ourselves a
138 	 * lot of nastiness by mmapping the whole file.
139 	 *
140 	 * This gives us an easy way to randomly access all the strings,
141 	 * without making the memory allocation permanent as with
142 	 * malloc/free (i.e., munmap will return it to the system).
143 	 */
144 	a_out_mmap = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t)0);
145 	if (a_out_mmap == MAP_FAILED)
146 		return (-1);
147 
148 	exec = (struct exec *)a_out_mmap;
149 	if (N_BADMAG(*exec)) {
150 		munmap(a_out_mmap, (size_t)st.st_size);
151 		return (-1);
152 	}
153 
154 	symoff = N_SYMOFF(*exec);
155 	symsize = exec->a_syms;
156 	stroff = symoff + symsize;
157 
158 	/* find the string table in our mmapped area */
159 	strtab = a_out_mmap + stroff;
160 	symtab = (struct nlist *)(a_out_mmap + symoff);
161 
162 	/*
163 	 * clean out any left-over information for all valid entries.
164 	 * Type and value defined to be 0 if not found; historical
165 	 * versions cleared other and desc as well.  Also figure out
166 	 * the largest string length so don't read any more of the
167 	 * string table than we have to.
168 	 *
169 	 * XXX clearing anything other than n_type and n_value violates
170 	 * the semantics given in the man page.
171 	 */
172 	nent = 0;
173 	for (p = list; !ISLAST(p); ++p) {
174 		p->n_type = 0;
175 		p->n_other = 0;
176 		p->n_desc = 0;
177 		p->n_value = 0;
178 		++nent;
179 	}
180 
181 	while (symsize > 0) {
182 		int soff;
183 
184 		symsize-= sizeof(struct nlist);
185 		soff = symtab->n_un.n_strx;
186 
187 
188 		if (soff != 0 && (symtab->n_type & N_STAB) == 0)
189 			for (p = list; !ISLAST(p); p++)
190 				if (!strcmp(&strtab[soff], p->n_un.n_name)) {
191 					p->n_value = symtab->n_value;
192 					p->n_type = symtab->n_type;
193 					p->n_desc = symtab->n_desc;
194 					p->n_other = symtab->n_other;
195 					if (--nent <= 0)
196 						break;
197 				}
198 		symtab++;
199 	}
200 	munmap(a_out_mmap, (size_t)st.st_size);
201 	return (nent);
202 }
203 #endif
204 
205 #ifdef _NLIST_DO_ELF
206 static void elf_sym_to_nlist(struct nlist *, Elf_Sym *, Elf_Shdr *, int);
207 
208 /*
209  * __elf_is_okay__ - Determine if ehdr really
210  * is ELF and valid for the target platform.
211  *
212  * WARNING:  This is NOT an ELF ABI function and
213  * as such its use should be restricted.
214  */
215 int
216 __elf_is_okay__(ehdr)
217 	Elf_Ehdr *ehdr;
218 {
219 	int retval = 0;
220 	/*
221 	 * We need to check magic, class size, endianess,
222 	 * and version before we look at the rest of the
223 	 * Elf_Ehdr structure.  These few elements are
224 	 * represented in a machine independant fashion.
225 	 */
226 	if (IS_ELF(*ehdr) &&
227 	    ehdr->e_ident[EI_CLASS] == ELF_TARG_CLASS &&
228 	    ehdr->e_ident[EI_DATA] == ELF_TARG_DATA &&
229 	    ehdr->e_ident[EI_VERSION] == ELF_TARG_VER) {
230 
231 		/* Now check the machine dependant header */
232 		if (ehdr->e_machine == ELF_TARG_MACH &&
233 		    ehdr->e_version == ELF_TARG_VER)
234 			retval = 1;
235 	}
236 	return retval;
237 }
238 
239 int
240 __elf_fdnlist(fd, list)
241 	int fd;
242 	struct nlist *list;
243 {
244 	struct nlist *p;
245 	Elf_Off symoff = 0, symstroff = 0;
246 	Elf_Word symsize = 0, symstrsize = 0;
247 	Elf_Sword cc, i;
248 	int nent = -1;
249 	int errsave;
250 	Elf_Sym sbuf[1024];
251 	Elf_Sym *s;
252 	Elf_Ehdr ehdr;
253 	char *strtab = NULL;
254 	Elf_Shdr *shdr = NULL;
255 	Elf_Word shdr_size;
256 	void *base;
257 	struct stat st;
258 
259 	/* Make sure obj is OK */
260 	if (lseek(fd, (off_t)0, SEEK_SET) == -1 ||
261 	    _read(fd, &ehdr, sizeof(Elf_Ehdr)) != sizeof(Elf_Ehdr) ||
262 	    !__elf_is_okay__(&ehdr) ||
263 	    _fstat(fd, &st) < 0)
264 		return (-1);
265 
266 	/* calculate section header table size */
267 	shdr_size = ehdr.e_shentsize * ehdr.e_shnum;
268 
269 	/* Make sure it's not too big to mmap */
270 	if (shdr_size > SIZE_T_MAX) {
271 		errno = EFBIG;
272 		return (-1);
273 	}
274 
275 	/* mmap section header table */
276 	base = mmap(NULL, (size_t)shdr_size, PROT_READ, 0, fd,
277 	    (off_t)ehdr.e_shoff);
278 	if (base == MAP_FAILED)
279 		return (-1);
280 	shdr = (Elf_Shdr *)base;
281 
282 	/*
283 	 * Find the symbol table entry and it's corresponding
284 	 * string table entry.	Version 1.1 of the ABI states
285 	 * that there is only one symbol table but that this
286 	 * could change in the future.
287 	 */
288 	for (i = 0; i < ehdr.e_shnum; i++) {
289 		if (shdr[i].sh_type == SHT_SYMTAB) {
290 			symoff = shdr[i].sh_offset;
291 			symsize = shdr[i].sh_size;
292 			symstroff = shdr[shdr[i].sh_link].sh_offset;
293 			symstrsize = shdr[shdr[i].sh_link].sh_size;
294 			break;
295 		}
296 	}
297 
298 	/* Check for files too large to mmap. */
299 	if (symstrsize > SIZE_T_MAX) {
300 		errno = EFBIG;
301 		goto done;
302 	}
303 	/*
304 	 * Map string table into our address space.  This gives us
305 	 * an easy way to randomly access all the strings, without
306 	 * making the memory allocation permanent as with malloc/free
307 	 * (i.e., munmap will return it to the system).
308 	 */
309 	base = mmap(NULL, (size_t)symstrsize, PROT_READ, 0, fd,
310 	    (off_t)symstroff);
311 	if (base == MAP_FAILED)
312 		goto done;
313 	strtab = (char *)base;
314 
315 	/*
316 	 * clean out any left-over information for all valid entries.
317 	 * Type and value defined to be 0 if not found; historical
318 	 * versions cleared other and desc as well.  Also figure out
319 	 * the largest string length so don't read any more of the
320 	 * string table than we have to.
321 	 *
322 	 * XXX clearing anything other than n_type and n_value violates
323 	 * the semantics given in the man page.
324 	 */
325 	nent = 0;
326 	for (p = list; !ISLAST(p); ++p) {
327 		p->n_type = 0;
328 		p->n_other = 0;
329 		p->n_desc = 0;
330 		p->n_value = 0;
331 		++nent;
332 	}
333 
334 	/* Don't process any further if object is stripped. */
335 	if (symoff == 0)
336 		goto done;
337 
338 	if (lseek(fd, (off_t) symoff, SEEK_SET) == -1) {
339 		nent = -1;
340 		goto done;
341 	}
342 
343 	while (symsize > 0 && nent > 0) {
344 		cc = MIN(symsize, sizeof(sbuf));
345 		if (_read(fd, sbuf, cc) != cc)
346 			break;
347 		symsize -= cc;
348 		for (s = sbuf; cc > 0 && nent > 0; ++s, cc -= sizeof(*s)) {
349 			char *name;
350 			struct nlist *p;
351 
352 			name = strtab + s->st_name;
353 			if (name[0] == '\0')
354 				continue;
355 			for (p = list; !ISLAST(p); p++) {
356 				if ((p->n_un.n_name[0] == '_' &&
357 				    strcmp(name, p->n_un.n_name+1) == 0)
358 				    || strcmp(name, p->n_un.n_name) == 0) {
359 					elf_sym_to_nlist(p, s, shdr,
360 					    ehdr.e_shnum);
361 					if (--nent <= 0)
362 						break;
363 				}
364 			}
365 		}
366 	}
367   done:
368 	errsave = errno;
369 	if (strtab != NULL)
370 		munmap(strtab, symstrsize);
371 	if (shdr != NULL)
372 		munmap(shdr, shdr_size);
373 	errno = errsave;
374 	return (nent);
375 }
376 
377 /*
378  * Convert an Elf_Sym into an nlist structure.  This fills in only the
379  * n_value and n_type members.
380  */
381 static void
382 elf_sym_to_nlist(nl, s, shdr, shnum)
383 	struct nlist *nl;
384 	Elf_Sym *s;
385 	Elf_Shdr *shdr;
386 	int shnum;
387 {
388 	nl->n_value = s->st_value;
389 
390 	switch (s->st_shndx) {
391 	case SHN_UNDEF:
392 	case SHN_COMMON:
393 		nl->n_type = N_UNDF;
394 		break;
395 	case SHN_ABS:
396 		nl->n_type = ELF_ST_TYPE(s->st_info) == STT_FILE ?
397 		    N_FN : N_ABS;
398 		break;
399 	default:
400 		if (s->st_shndx >= shnum)
401 			nl->n_type = N_UNDF;
402 		else {
403 			Elf_Shdr *sh = shdr + s->st_shndx;
404 
405 			nl->n_type = sh->sh_type == SHT_PROGBITS ?
406 			    (sh->sh_flags & SHF_WRITE ? N_DATA : N_TEXT) :
407 			    (sh->sh_type == SHT_NOBITS ? N_BSS : N_UNDF);
408 		}
409 		break;
410 	}
411 
412 	if (ELF_ST_BIND(s->st_info) == STB_GLOBAL ||
413 	    ELF_ST_BIND(s->st_info) == STB_WEAK)
414 		nl->n_type |= N_EXT;
415 }
416 #endif /* _NLIST_DO_ELF */
417