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