1 /**	$MirOS: src/lib/libc/gen/fnlist.c,v 1.10 2006/06/02 02:29:48 tg Exp $ */
2 /*	$OpenBSD: nlist.c,v 1.51 2005/08/08 08:05:34 espie Exp $ */
3 /*
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/param.h>
33 #include <sys/mman.h>
34 #include <sys/stat.h>
35 
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <a.out.h>		/* pulls in nlist.h */
43 
44 __RCSID("$MirOS: src/lib/libc/gen/fnlist.c,v 1.10 2006/06/02 02:29:48 tg Exp $");
45 
46 #ifdef _NLIST_DO_ELF
47 #include <elf_abi.h>
48 #include <olf_abi.h>
49 #endif
50 
51 int	__fnlist(FILE *, struct nlist *);
52 int	__aout_fnlist(FILE *, struct nlist *);
53 int	__elf_fnlist(FILE *, struct nlist *);
54 #ifdef _NLIST_DO_ELF
55 int	__elf_is_okay__(Elf_Ehdr *ehdr);
56 #endif
57 
58 #define	ISLAST(p)	(p->n_un.n_name == 0 || p->n_un.n_name[0] == 0)
59 
60 #ifdef correct_fpseek_not_a_speed_hack
61 static ssize_t
__fpread(FILE * f,void * buf,size_t nbytes,off_t offset)62 __fpread(FILE *f, void *buf, size_t nbytes, off_t offset)
63 {
64 	off_t oldofs;
65 	ssize_t rv;
66 
67 	if ((oldofs = ftello(f)) == -1)
68 		return (-1);
69 	if ((oldofs != offset) && (fseeko(f, offset, SEEK_SET)))
70 		return (-1);
71 	if ((rv = fread(buf, 1, nbytes, f)) == 0) {
72 		if (ferror(f))
73 			return (-1);
74 	}
75 	if (fseeko(f, oldofs, SEEK_SET))
76 		return (-1);
77 
78 	return (rv);
79 }
80 #else
81 static ssize_t
__fpread(FILE * f,void * buf,size_t nbytes,off_t offset)82 __fpread(FILE *f, void *buf, size_t nbytes, off_t offset)
83 {
84 	ssize_t rv;
85 
86 	if ((ftello(f) != offset) && (fseeko(f, offset, SEEK_SET)))
87 		return (-1);
88 	if ((rv = fread(buf, 1, nbytes, f)) == 0) {
89 		if (ferror(f))
90 			return (-1);
91 	}
92 
93 	return (rv);
94 }
95 #endif
96 
97 #define fpread __fpread
98 
99 #ifdef _NLIST_DO_AOUT
100 int
__aout_fnlist(FILE * f,struct nlist * list)101 __aout_fnlist(FILE *f, struct nlist *list)
102 {
103 	struct nlist *p, *s;
104 	char *strtab;
105 	off_t symoff, stroff;
106 	u_long symsize;
107 	int nent, cc;
108 	int strsize;
109 	struct nlist nbuf[1024];
110 	struct exec exec;
111 
112 	if (fpread(f, &exec, sizeof(exec), (off_t)0) != sizeof(exec) ||
113 	    N_BADMAG(exec) || exec.a_syms == (u_int32_t)NULL)
114 		return (-1);
115 
116 	stroff = N_STROFF(exec);
117 	symoff = N_SYMOFF(exec);
118 	symsize = exec.a_syms;
119 
120 	/* Read in the size of the string table. */
121 	if (fpread(f, (void *)&strsize, sizeof(strsize), stroff) !=
122 	    sizeof(strsize))
123 		return (-1);
124 	else
125 		stroff += sizeof(strsize);
126 
127 	/*
128 	 * Read in the string table.  We don't try mmap, that will fail
129 	 * for zlib streams so use on malloc.  Since OpenBSD's malloc(3)
130 	 * returns memory to the system on free this does not cause bloat.
131 	 */
132 	strsize -= sizeof(strsize);
133 	if ((strtab = (char *)malloc(strsize)) == NULL)
134 		return (-1);
135 	errno = EIO;
136 	if (fpread(f, strtab, strsize, stroff) != strsize) {
137 		nent = -1;
138 		goto aout_done;
139 	}
140 
141 	/*
142 	 * clean out any left-over information for all valid entries.
143 	 * Type and value defined to be 0 if not found; historical
144 	 * versions cleared other and desc as well.  Also figure out
145 	 * the largest string length so don't read any more of the
146 	 * string table than we have to.
147 	 *
148 	 * XXX clearing anything other than n_type and n_value violates
149 	 * the semantics given in the man page.
150 	 */
151 	nent = 0;
152 	for (p = list; !ISLAST(p); ++p) {
153 		p->n_type = 0;
154 		p->n_other = 0;
155 		p->n_desc = 0;
156 		p->n_value = 0;
157 		++nent;
158 	}
159 
160 	while (symsize > 0) {
161 		cc = MIN(symsize, sizeof(nbuf));
162 		if (fpread(f, nbuf, cc, symoff) != cc)
163 			break;
164 		symsize -= cc;
165 		symoff += cc;
166 		for (s = nbuf; cc > 0; ++s, cc -= sizeof(*s)) {
167 			char *sname = strtab + s->n_un.n_strx - sizeof(int);
168 
169 			if (s->n_un.n_strx == 0 || (s->n_type & N_STAB) != 0)
170 				continue;
171 			for (p = list; !ISLAST(p); p++) {
172 				char *pname = p->n_un.n_name;
173 
174 				if (*sname != '_' && *pname == '_')
175 					pname++;
176 				if (!strcmp(sname, pname)) {
177 					p->n_value = s->n_value;
178 					p->n_type = s->n_type;
179 					p->n_desc = s->n_desc;
180 					p->n_other = s->n_other;
181 					if (--nent <= 0)
182 						break;
183 				}
184 			}
185 		}
186 	}
187 aout_done:
188 	free(strtab);
189 	return (nent);
190 }
191 #endif /* _NLIST_DO_AOUT */
192 
193 #ifdef _NLIST_DO_ELF
194 int
__elf_fnlist(FILE * f,struct nlist * list)195 __elf_fnlist(FILE *f, struct nlist *list)
196 {
197 	struct nlist *p;
198 	caddr_t strtab;
199 	Elf_Off symoff = 0, symstroff = 0;
200 	Elf_Word symsize = 0, symstrsize = 0;
201 	Elf_Sword nent, cc, i;
202 	Elf_Sym sbuf[1024];
203 	Elf_Sym *s;
204 	Elf_Ehdr ehdr;
205 	Elf_Shdr *shdr = NULL;
206 	Elf_Word shdr_size;
207 	struct stat st;
208 
209 	/* Make sure obj is OK */
210 	if (fpread(f, &ehdr, sizeof(Elf_Ehdr), (off_t)0) != sizeof(Elf_Ehdr) ||
211 	    !__elf_is_okay__(&ehdr) || fstat(fileno(f), &st) < 0)
212 		return (-1);
213 
214 	/* calculate section header table size */
215 	shdr_size = ehdr.e_shentsize * ehdr.e_shnum;
216 
217 	if ((shdr = malloc(shdr_size)) == NULL)
218 		return (-1);
219 
220 	if ((off_t)fpread(f, shdr, shdr_size, ehdr.e_shoff) != shdr_size) {
221 		free(shdr);
222 		return (-1);
223 	}
224 
225 	/*
226 	 * Find the symbol table entry and it's corresponding
227 	 * string table entry.	Version 1.1 of the ABI states
228 	 * that there is only one symbol table but that this
229 	 * could change in the future.
230 	 */
231 	for (i = 0; i < ehdr.e_shnum; i++) {
232 		if (shdr[i].sh_type == SHT_SYMTAB) {
233 			symoff = shdr[i].sh_offset;
234 			symsize = shdr[i].sh_size;
235 			symstroff = shdr[shdr[i].sh_link].sh_offset;
236 			symstrsize = shdr[shdr[i].sh_link].sh_size;
237 			break;
238 		}
239 	}
240 
241 	/* Flush the section header table */
242 	free(shdr);
243 
244 	/*
245 	 * Read string table into our address space.
246 	 */
247 	if ((strtab = malloc(symstrsize)) == NULL)
248 		return (-1);
249 	if ((off_t)fpread(f, strtab, symstrsize, symstroff) != symstrsize) {
250 		free(strtab);
251 		return (-1);
252 	}
253 	/*
254 	 * clean out any left-over information for all valid entries.
255 	 * Type and value defined to be 0 if not found; historical
256 	 * versions cleared other and desc as well.  Also figure out
257 	 * the largest string length so don't read any more of the
258 	 * string table than we have to.
259 	 *
260 	 * XXX clearing anything other than n_type and n_value violates
261 	 * the semantics given in the man page.
262 	 */
263 	nent = 0;
264 	for (p = list; !ISLAST(p); ++p) {
265 		p->n_type = 0;
266 		p->n_other = 0;
267 		p->n_desc = 0;
268 		p->n_value = 0;
269 		++nent;
270 	}
271 
272 	/* Don't process any further if object is stripped. */
273 	/* ELFism - dunno if stripped by looking at header */
274 	if (symoff == 0)
275 		goto elf_done;
276 
277 	while (symsize > 0) {
278 		cc = MIN(symsize, sizeof(sbuf));
279 		if (fpread(f, sbuf, cc, symoff) != cc)
280 			break;
281 		symsize -= cc;
282 		symoff += cc;
283 		for (s = sbuf; cc > 0; ++s, cc -= sizeof(*s)) {
284 			int soff = s->st_name;
285 
286 			if (soff == 0)
287 				continue;
288 			for (p = list; !ISLAST(p); p++) {
289 				char *sym;
290 
291 				/*
292 				 * First we check for the symbol as it was
293 				 * provided by the user. If that fails
294 				 * and the first char is an '_', skip over
295 				 * the '_' and try again.
296 				 * XXX - What do we do when the user really
297 				 *       wants '_foo' and the are symbols
298 				 *       for both 'foo' and '_foo' in the
299 				 *	 table and 'foo' is first?
300 				 */
301 				sym = p->n_un.n_name;
302 				if (strcmp(&strtab[soff], sym) != 0 &&
303 				    (sym[0] != '_' ||
304 				     strcmp(&strtab[soff], sym + 1) != 0))
305 					continue;
306 
307 				p->n_value = s->st_value;
308 
309 				/* XXX - type conversion */
310 				/*	 is pretty rude. */
311 				switch(ELF_ST_TYPE(s->st_info)) {
312 				case STT_NOTYPE:
313 					switch (s->st_shndx) {
314 					case SHN_UNDEF:
315 						p->n_type = N_UNDF;
316 						break;
317 					case SHN_ABS:
318 						p->n_type = N_ABS;
319 						break;
320 					case SHN_COMMON:
321 						p->n_type = N_COMM;
322 						break;
323 					default:
324 						p->n_type = N_COMM | N_EXT;
325 						break;
326 					}
327 					break;
328 				case STT_OBJECT:
329 					p->n_type = N_DATA;
330 					break;
331 				case STT_FUNC:
332 					p->n_type = N_TEXT;
333 					break;
334 				case STT_FILE:
335 					p->n_type = N_FN;
336 					break;
337 				}
338 				if (ELF_ST_BIND(s->st_info) ==
339 				    STB_LOCAL)
340 					p->n_type = N_EXT;
341 				p->n_desc = 0;
342 				p->n_other = 0;
343 				if (--nent <= 0)
344 					break;
345 			}
346 		}
347 	}
348 elf_done:
349 	free(strtab);
350 	return (nent);
351 }
352 #endif /* _NLIST_DO_ELF */
353 
354 
355 static struct nlist_handlers {
356 	int	(*fn)(FILE *f, struct nlist *list);
357 } nlist_fn[] = {
358 #ifdef _NLIST_DO_AOUT
359 	{ __aout_fnlist },
360 #endif
361 #ifdef _NLIST_DO_ELF
362 	{ __elf_fnlist },
363 #endif
364 };
365 
366 int
__fnlist(FILE * f,struct nlist * list)367 __fnlist(FILE *f, struct nlist *list)
368 {
369 	int n = -1;
370 	size_t i;
371 
372 	for (i = 0; i < sizeof(nlist_fn)/sizeof(nlist_fn[0]); i++) {
373 		n = (nlist_fn[i].fn)(f, list);
374 		if (n != -1)
375 			break;
376 	}
377 	return (n);
378 }
379