xref: /freebsd/lib/libc/gen/nlist.c (revision 0de89efe5c443f213c7ea28773ef2dc6cf3af2ed)
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 
51 #ifdef _NLIST_DO_ELF
52 #include <elf.h>
53 #endif
54 
55 int __fdnlist		__P((int, struct nlist *));
56 int __aout_fdnlist	__P((int, struct nlist *));
57 int __elf_fdnlist	__P((int, struct nlist *));
58 
59 int
60 nlist(name, list)
61 	const char *name;
62 	struct nlist *list;
63 {
64 	int fd, n;
65 
66 	fd = open(name, O_RDONLY, 0);
67 	if (fd < 0)
68 		return (-1);
69 	n = __fdnlist(fd, list);
70 	(void)close(fd);
71 	return (n);
72 }
73 
74 static struct nlist_handlers {
75 	int	(*fn) __P((int fd, struct nlist *list));
76 } nlist_fn[] = {
77 #ifdef _NLIST_DO_AOUT
78 	{ __aout_fdnlist },
79 #endif
80 #ifdef _NLIST_DO_ELF
81 	{ __elf_fdnlist },
82 #endif
83 };
84 
85 int
86 __fdnlist(fd, list)
87 	register int fd;
88 	register struct nlist *list;
89 {
90 	int n = -1, i;
91 
92 	for (i = 0; i < sizeof(nlist_fn) / sizeof(nlist_fn[0]); i++) {
93 		n = (nlist_fn[i].fn)(fd, list);
94 		if (n != -1)
95 			break;
96 	}
97 	return (n);
98 }
99 
100 #define	ISLAST(p)	(p->n_un.n_name == 0 || p->n_un.n_name[0] == 0)
101 
102 #ifdef _NLIST_DO_AOUT
103 int
104 __aout_fdnlist(fd, list)
105 	register int fd;
106 	register struct nlist *list;
107 {
108 	register struct nlist *p, *symtab;
109 	register caddr_t strtab, a_out_mmap;
110 	register off_t stroff, symoff;
111 	register u_long symsize;
112 	register 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 		register 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 /*
200  * __elf_is_okay__ - Determine if ehdr really
201  * is ELF and valid for the target platform.
202  *
203  * WARNING:  This is NOT a ELF ABI function and
204  * as such it's use should be restricted.
205  */
206 int
207 __elf_is_okay__(ehdr)
208 	register Elf32_Ehdr *ehdr;
209 {
210 	register int retval = 0;
211 	/*
212 	 * We need to check magic, class size, endianess,
213 	 * and version before we look at the rest of the
214 	 * Elf32_Ehdr structure.  These few elements are
215 	 * represented in a machine independant fashion.
216 	 */
217 	if (IS_ELF(*ehdr) &&
218 	    ehdr->e_ident[EI_CLASS] == ELF_TARG_CLASS &&
219 	    ehdr->e_ident[EI_DATA] == ELF_TARG_DATA &&
220 	    ehdr->e_ident[EI_VERSION] == ELF_TARG_VER) {
221 
222 		/* Now check the machine dependant header */
223 		if (ehdr->e_machine == ELF_TARG_MACH &&
224 		    ehdr->e_version == ELF_TARG_VER)
225 			retval = 1;
226 	}
227 	return retval;
228 }
229 
230 int
231 __elf_fdnlist(fd, list)
232 	register int fd;
233 	register struct nlist *list;
234 {
235 	register struct nlist *p;
236 	register caddr_t strtab;
237 	register Elf32_Off symoff = 0, symstroff = 0;
238 	register Elf32_Word symsize = 0, symstrsize = 0;
239 	register Elf32_Sword nent, cc, i;
240 	Elf32_Sym sbuf[1024];
241 	Elf32_Sym *s;
242 	Elf32_Ehdr ehdr;
243 	Elf32_Shdr *shdr = NULL;
244 	Elf32_Word shdr_size;
245 	struct stat st;
246 
247 	/* Make sure obj is OK */
248 	if (lseek(fd, (off_t)0, SEEK_SET) == -1 ||
249 	    read(fd, &ehdr, sizeof(Elf32_Ehdr)) != sizeof(Elf32_Ehdr) ||
250 	    !__elf_is_okay__(&ehdr) ||
251 	    fstat(fd, &st) < 0)
252 		return (-1);
253 
254 	/* calculate section header table size */
255 	shdr_size = ehdr.e_shentsize * ehdr.e_shnum;
256 
257 	/* Make sure it's not too big to mmap */
258 	if (shdr_size > SIZE_T_MAX) {
259 		errno = EFBIG;
260 		return (-1);
261 	}
262 
263 	/* mmap section header table */
264 	shdr = (Elf32_Shdr *)mmap(NULL, (size_t)shdr_size,
265 				  PROT_READ, 0, fd, (off_t) ehdr.e_shoff);
266 	if (shdr == (Elf32_Shdr *)-1)
267 		return (-1);
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 	/* Flush the section header table */
286 	munmap((caddr_t)shdr, shdr_size);
287 
288 	/* Check for files too large to mmap. */
289 	if (symstrsize > SIZE_T_MAX) {
290 		errno = EFBIG;
291 		return (-1);
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 	strtab = mmap(NULL, (size_t)symstrsize, PROT_READ, 0, fd,
300 		      (off_t) symstroff);
301 	if (strtab == (char *)-1)
302 		return (-1);
303 
304 	/*
305 	 * clean out any left-over information for all valid entries.
306 	 * Type and value defined to be 0 if not found; historical
307 	 * versions cleared other and desc as well.  Also figure out
308 	 * the largest string length so don't read any more of the
309 	 * string table than we have to.
310 	 *
311 	 * XXX clearing anything other than n_type and n_value violates
312 	 * the semantics given in the man page.
313 	 */
314 	nent = 0;
315 	for (p = list; !ISLAST(p); ++p) {
316 		p->n_type = 0;
317 		p->n_other = 0;
318 		p->n_desc = 0;
319 		p->n_value = 0;
320 		++nent;
321 	}
322 
323 	/* Don't process any further if object is stripped. */
324 	/* ELFism - dunno if stripped by looking at header */
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) {
334 		cc = MIN(symsize, sizeof(sbuf));
335 		if (read(fd, sbuf, cc) != cc)
336 			break;
337 		symsize -= cc;
338 		for (s = sbuf; cc > 0; ++s, cc -= sizeof(*s)) {
339 			register int soff = s->st_name;
340 
341 			if (soff == 0)
342 				continue;
343 			for (p = list; !ISLAST(p); p++) {
344 				if ((p->n_un.n_name[0] == '_' &&
345 				     !strcmp(&strtab[soff], p->n_un.n_name+1))
346 				    || !strcmp(&strtab[soff], p->n_un.n_name)) {
347 					p->n_value = s->st_value;
348 
349 					/* XXX - type conversion */
350 					/*	 is pretty rude. */
351 					switch(ELF32_ST_TYPE(s->st_info)) {
352 						case STT_NOTYPE:
353 							p->n_type = N_UNDF;
354 							break;
355 						case STT_OBJECT:
356 							p->n_type = N_DATA;
357 							break;
358 						case STT_FUNC:
359 							p->n_type = N_TEXT;
360 							break;
361 						case STT_FILE:
362 							p->n_type = N_FN;
363 							break;
364 					}
365 					if (ELF32_ST_BIND(s->st_info) ==
366 					    STB_LOCAL)
367 						p->n_type = N_EXT;
368 					p->n_desc = 0;
369 					p->n_other = 0;
370 					if (--nent <= 0)
371 						break;
372 				}
373 			}
374 		}
375 	}
376   done:
377 	munmap(strtab, symstrsize);
378 
379 	return (nent);
380 }
381 #endif /* _NLIST_DO_ELF */
382