xref: /freebsd/lib/libproc/proc_sym.c (revision 3cbb4cc200f8a0ad7ed08233425ea54524a21f1c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2016-2017 Mark Johnston <markj@FreeBSD.org>
5  * Copyright (c) 2010 The FreeBSD Foundation
6  * Copyright (c) 2008 John Birrell (jb@freebsd.org)
7  * All rights reserved.
8  *
9  * Portions of this software were developed by Rui Paulo under sponsorship
10  * from the FreeBSD Foundation.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <sys/types.h>
38 #ifndef NO_CTF
39 #include <sys/ctf.h>
40 #include <sys/ctf_api.h>
41 #endif
42 #include <sys/user.h>
43 
44 #include <assert.h>
45 #include <err.h>
46 #include <fcntl.h>
47 #include <libgen.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52 #ifndef NO_CTF
53 #include <libctf.h>
54 #endif
55 #include <libutil.h>
56 
57 #include "crc32.h"
58 #include "_libproc.h"
59 
60 #define	PATH_DEBUG_DIR	"/usr/lib/debug"
61 
62 #ifdef NO_CTF
63 typedef struct ctf_file ctf_file_t;
64 #endif
65 
66 #ifndef NO_CXA_DEMANGLE
67 extern char *__cxa_demangle(const char *, char *, size_t *, int *);
68 #endif /* NO_CXA_DEMANGLE */
69 
70 static int
71 crc32_file(int fd, uint32_t *crc)
72 {
73 	uint8_t buf[PAGE_SIZE], *p;
74 	size_t n;
75 
76 	*crc = ~0;
77 	while ((n = read(fd, buf, sizeof(buf))) > 0) {
78 		p = &buf[0];
79 		while (n-- > 0)
80 			*crc = crc32_tab[(*crc ^ *p++) & 0xff] ^ (*crc >> 8);
81 	}
82 	*crc = ~*crc;
83 	return (n);
84 }
85 
86 static void
87 demangle(const char *symbol, char *buf, size_t len)
88 {
89 #ifndef NO_CXA_DEMANGLE
90 	char *dembuf;
91 
92 	if (symbol[0] == '_' && symbol[1] == 'Z' && symbol[2]) {
93 		dembuf = __cxa_demangle(symbol, NULL, NULL, NULL);
94 		if (!dembuf)
95 			goto fail;
96 		strlcpy(buf, dembuf, len);
97 		free(dembuf);
98 		return;
99 	}
100 fail:
101 #endif /* NO_CXA_DEMANGLE */
102 	strlcpy(buf, symbol, len);
103 }
104 
105 struct symsort_thunk {
106 	Elf *e;
107 	struct symtab *symtab;
108 };
109 
110 static int
111 symvalcmp(void *_thunk, const void *a1, const void *a2)
112 {
113 	GElf_Sym sym1, sym2;
114 	struct symsort_thunk *thunk;
115 	const char *s1, *s2;
116 	u_int i1, i2;
117 	int bind1, bind2;
118 
119 	i1 = *(const u_int *)a1;
120 	i2 = *(const u_int *)a2;
121 	thunk = _thunk;
122 
123 	(void)gelf_getsym(thunk->symtab->data, i1, &sym1);
124 	(void)gelf_getsym(thunk->symtab->data, i2, &sym2);
125 
126 	if (sym1.st_value != sym2.st_value)
127 		return (sym1.st_value < sym2.st_value ? -1 : 1);
128 
129 	/* Prefer non-local symbols. */
130 	bind1 = GELF_ST_BIND(sym1.st_info);
131 	bind2 = GELF_ST_BIND(sym2.st_info);
132 	if (bind1 != bind2) {
133 		if (bind1 == STB_LOCAL && bind2 != STB_LOCAL)
134 			return (-1);
135 		if (bind1 != STB_LOCAL && bind2 == STB_LOCAL)
136 			return (1);
137 	}
138 
139 	s1 = elf_strptr(thunk->e, thunk->symtab->stridx, sym1.st_name);
140 	s2 = elf_strptr(thunk->e, thunk->symtab->stridx, sym2.st_name);
141 	if (s1 != NULL && s2 != NULL) {
142 		/* Prefer symbols without a leading '$'. */
143 		if (*s1 == '$')
144 			return (-1);
145 		if (*s2 == '$')
146 			return (1);
147 
148 		/* Prefer symbols with fewer leading underscores. */
149 		for (; *s1 == '_' && *s2 == '_'; s1++, s2++)
150 			;
151 		if (*s1 == '_')
152 			return (-1);
153 		if (*s2 == '_')
154 			return (1);
155 	}
156 
157 	return (0);
158 }
159 
160 static int
161 load_symtab(Elf *e, struct symtab *symtab, u_long sh_type)
162 {
163 	GElf_Ehdr ehdr;
164 	GElf_Shdr shdr;
165 	struct symsort_thunk thunk;
166 	Elf_Scn *scn;
167 	u_int nsyms;
168 
169 	if (gelf_getehdr(e, &ehdr) == NULL)
170 		return (-1);
171 
172 	scn = NULL;
173 	while ((scn = elf_nextscn(e, scn)) != NULL) {
174 		(void)gelf_getshdr(scn, &shdr);
175 		if (shdr.sh_type == sh_type)
176 			break;
177 	}
178 	if (scn == NULL)
179 		return (-1);
180 
181 	nsyms = shdr.sh_size / shdr.sh_entsize;
182 	if (nsyms > (1 << 20))
183 		return (-1);
184 
185 	if ((symtab->data = elf_getdata(scn, NULL)) == NULL)
186 		return (-1);
187 
188 	symtab->index = calloc(nsyms, sizeof(u_int));
189 	if (symtab->index == NULL)
190 		return (-1);
191 	for (u_int i = 0; i < nsyms; i++)
192 		symtab->index[i] = i;
193 	symtab->nsyms = nsyms;
194 	symtab->stridx = shdr.sh_link;
195 
196 	thunk.e = e;
197 	thunk.symtab = symtab;
198 	qsort_r(symtab->index, nsyms, sizeof(u_int), &thunk, symvalcmp);
199 
200 	return (0);
201 }
202 
203 static void
204 load_symtabs(struct file_info *file)
205 {
206 
207 	file->symtab.nsyms = file->dynsymtab.nsyms = 0;
208 	(void)load_symtab(file->elf, &file->symtab, SHT_SYMTAB);
209 	(void)load_symtab(file->elf, &file->dynsymtab, SHT_DYNSYM);
210 }
211 
212 static int
213 open_debug_file(char *path, const char *debugfile, uint32_t crc)
214 {
215 	size_t n;
216 	uint32_t compcrc;
217 	int fd;
218 
219 	fd = -1;
220 	if ((n = strlcat(path, "/", PATH_MAX)) >= PATH_MAX)
221 		return (fd);
222 	if (strlcat(path, debugfile, PATH_MAX) >= PATH_MAX)
223 		goto out;
224 	if ((fd = open(path, O_RDONLY | O_CLOEXEC)) < 0)
225 		goto out;
226 	if (crc32_file(fd, &compcrc) != 0 || crc != compcrc) {
227 		DPRINTFX("ERROR: CRC32 mismatch for %s", path);
228 		(void)close(fd);
229 		fd = -1;
230 	}
231 out:
232 	path[n] = '\0';
233 	return (fd);
234 }
235 
236 /*
237  * Obtain an ELF descriptor for the specified mapped object. If a GNU debuglink
238  * section is present, a descriptor for the corresponding debug file is
239  * returned.
240  */
241 static int
242 open_object(struct map_info *mapping)
243 {
244 	char path[PATH_MAX];
245 	GElf_Shdr shdr;
246 	Elf *e, *e2;
247 	Elf_Data *data;
248 	Elf_Scn *scn;
249 	struct file_info *file;
250 	prmap_t *map;
251 	const char *debugfile, *scnname;
252 	size_t ndx;
253 	uint32_t crc;
254 	int fd, fd2;
255 
256 	if (mapping->map.pr_mapname[0] == '\0')
257 		return (-1); /* anonymous object */
258 	if (mapping->file->elf != NULL)
259 		return (0); /* already loaded */
260 
261 	file = mapping->file;
262 	map = &mapping->map;
263 	if ((fd = open(map->pr_mapname, O_RDONLY | O_CLOEXEC)) < 0) {
264 		DPRINTF("ERROR: open %s failed", map->pr_mapname);
265 		return (-1);
266 	}
267 	if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
268 		DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
269 		goto err;
270 	}
271 	if (gelf_getehdr(e, &file->ehdr) != &file->ehdr) {
272 		DPRINTFX("ERROR: elf_getehdr() failed: %s", elf_errmsg(-1));
273 		goto err;
274 	}
275 
276 	scn = NULL;
277 	while ((scn = elf_nextscn(e, scn)) != NULL) {
278 		if (gelf_getshdr(scn, &shdr) != &shdr) {
279 			DPRINTFX("ERROR: gelf_getshdr failed: %s",
280 			    elf_errmsg(-1));
281 			goto err;
282 		}
283 		if (shdr.sh_type != SHT_PROGBITS)
284 			continue;
285 		if (elf_getshdrstrndx(e, &ndx) != 0) {
286 			DPRINTFX("ERROR: elf_getshdrstrndx failed: %s",
287 			    elf_errmsg(-1));
288 			goto err;
289 		}
290 		if ((scnname = elf_strptr(e, ndx, shdr.sh_name)) == NULL)
291 			continue;
292 
293 		if (strcmp(scnname, ".gnu_debuglink") == 0)
294 			break;
295 	}
296 	if (scn == NULL)
297 		goto internal;
298 
299 	if ((data = elf_getdata(scn, NULL)) == NULL) {
300 		DPRINTFX("ERROR: elf_getdata failed: %s", elf_errmsg(-1));
301 		goto err;
302 	}
303 
304 	/*
305 	 * The data contains a null-terminated file name followed by a 4-byte
306 	 * CRC.
307 	 */
308 	if (data->d_size < sizeof(crc) + 1) {
309 		DPRINTFX("ERROR: debuglink section is too small (%zd bytes)",
310 		    (ssize_t)data->d_size);
311 		goto internal;
312 	}
313 	if (strnlen(data->d_buf, data->d_size) >= data->d_size - sizeof(crc)) {
314 		DPRINTFX("ERROR: no null-terminator in gnu_debuglink section");
315 		goto internal;
316 	}
317 
318 	debugfile = data->d_buf;
319 	memcpy(&crc, (char *)data->d_buf + data->d_size - sizeof(crc),
320 	    sizeof(crc));
321 
322 	/*
323 	 * Search for the debug file using the algorithm described in the gdb
324 	 * documentation:
325 	 * - look in the directory containing the object,
326 	 * - look in the subdirectory ".debug" of the directory containing the
327 	 *   object,
328 	 * - look in the global debug directories (currently /usr/lib/debug).
329 	 */
330 	(void)strlcpy(path, map->pr_mapname, sizeof(path));
331 	(void)dirname(path);
332 
333 	if ((fd2 = open_debug_file(path, debugfile, crc)) >= 0)
334 		goto external;
335 
336 	if (strlcat(path, "/.debug", sizeof(path)) < sizeof(path) &&
337 	    (fd2 = open_debug_file(path, debugfile, crc)) >= 0)
338 		goto external;
339 
340 	(void)snprintf(path, sizeof(path), PATH_DEBUG_DIR);
341 	if (strlcat(path, map->pr_mapname, sizeof(path)) < sizeof(path)) {
342 		(void)dirname(path);
343 		if ((fd2 = open_debug_file(path, debugfile, crc)) >= 0)
344 			goto external;
345 	}
346 
347 internal:
348 	/* We didn't find a debug file, just return the object's descriptor. */
349 	file->elf = e;
350 	file->fd = fd;
351 	load_symtabs(file);
352 	return (0);
353 
354 external:
355 	if ((e2 = elf_begin(fd2, ELF_C_READ, NULL)) == NULL) {
356 		DPRINTFX("ERROR: elf_begin failed: %s", elf_errmsg(-1));
357 		(void)close(fd2);
358 		goto err;
359 	}
360 	(void)elf_end(e);
361 	(void)close(fd);
362 	file->elf = e2;
363 	file->fd = fd2;
364 	load_symtabs(file);
365 	return (0);
366 
367 err:
368 	if (e != NULL)
369 		(void)elf_end(e);
370 	(void)close(fd);
371 	return (-1);
372 }
373 
374 char *
375 proc_objname(struct proc_handle *p, uintptr_t addr, char *objname,
376     size_t objnamesz)
377 {
378 	prmap_t *map;
379 	size_t i;
380 
381 	if (p->nmappings == 0)
382 		if (proc_rdagent(p) == NULL)
383 			return (NULL);
384 	for (i = 0; i < p->nmappings; i++) {
385 		map = &p->mappings[i].map;
386 		if (addr >= map->pr_vaddr &&
387 		    addr < map->pr_vaddr + map->pr_size) {
388 			strlcpy(objname, map->pr_mapname, objnamesz);
389 			return (objname);
390 		}
391 	}
392 	return (NULL);
393 }
394 
395 int
396 proc_iter_objs(struct proc_handle *p, proc_map_f *func, void *cd)
397 {
398 	char last[MAXPATHLEN], path[MAXPATHLEN], *base;
399 	prmap_t *map;
400 	size_t i;
401 	int error;
402 
403 	if (p->nmappings == 0)
404 		if (proc_rdagent(p) == NULL)
405 			return (-1);
406 
407 	error = 0;
408 	memset(last, 0, sizeof(last));
409 	for (i = 0; i < p->nmappings; i++) {
410 		map = &p->mappings[i].map;
411 		strlcpy(path, map->pr_mapname, sizeof(path));
412 		base = basename(path);
413 		/*
414 		 * We shouldn't call the callback twice with the same object.
415 		 * To do that we are assuming the fact that if there are
416 		 * repeated object names (i.e. different mappings for the
417 		 * same object) they occur next to each other.
418 		 */
419 		if (strcmp(base, last) == 0)
420 			continue;
421 		if ((error = (*func)(cd, map, base)) != 0)
422 			break;
423 		strlcpy(last, path, sizeof(last));
424 	}
425 	return (error);
426 }
427 
428 static struct map_info *
429 _proc_addr2map(struct proc_handle *p, uintptr_t addr)
430 {
431 	struct map_info *mapping;
432 	size_t i;
433 
434 	if (p->nmappings == 0)
435 		if (proc_rdagent(p) == NULL)
436 			return (NULL);
437 	for (i = 0; i < p->nmappings; i++) {
438 		mapping = &p->mappings[i];
439 		if (addr >= mapping->map.pr_vaddr &&
440 		    addr < mapping->map.pr_vaddr + mapping->map.pr_size)
441 			return (mapping);
442 	}
443 	return (NULL);
444 }
445 
446 prmap_t *
447 proc_addr2map(struct proc_handle *p, uintptr_t addr)
448 {
449 
450 	return (&_proc_addr2map(p, addr)->map);
451 }
452 
453 /*
454  * Look up the symbol at addr using a binary search, returning a copy of the
455  * symbol and its name.
456  */
457 static int
458 lookup_symbol_by_addr(Elf *e, struct symtab *symtab, uintptr_t addr,
459     const char **namep, GElf_Sym *symp)
460 {
461 	GElf_Sym sym;
462 	Elf_Data *data;
463 	const char *s;
464 	u_int i, min, max, mid;
465 
466 	if (symtab->nsyms == 0)
467 		return (ENOENT);
468 
469 	data = symtab->data;
470 	min = 0;
471 	max = symtab->nsyms - 1;
472 
473 	while (min <= max) {
474 		mid = (max + min) / 2;
475 		(void)gelf_getsym(data, symtab->index[mid], &sym);
476 		if (addr >= sym.st_value && addr < sym.st_value + sym.st_size)
477 			break;
478 
479 		if (addr < sym.st_value)
480 			max = mid - 1;
481 		else
482 			min = mid + 1;
483 	}
484 	if (min > max)
485 		return (ENOENT);
486 
487 	/*
488 	 * Advance until we find the matching symbol with largest index.
489 	 */
490 	for (i = mid; i < symtab->nsyms; i++) {
491 		(void)gelf_getsym(data, symtab->index[i], &sym);
492 		if (addr < sym.st_value || addr >= sym.st_value + sym.st_size)
493 			break;
494 	}
495 	(void)gelf_getsym(data, symtab->index[i - 1], symp);
496 	s = elf_strptr(e, symtab->stridx, symp->st_name);
497 	if (s != NULL && namep != NULL)
498 		*namep = s;
499 	return (0);
500 }
501 
502 int
503 proc_addr2sym(struct proc_handle *p, uintptr_t addr, char *name,
504     size_t namesz, GElf_Sym *symcopy)
505 {
506 	struct file_info *file;
507 	struct map_info *mapping;
508 	const char *s;
509 	uintptr_t off;
510 	int error;
511 
512 	if ((mapping = _proc_addr2map(p, addr)) == NULL) {
513 		DPRINTFX("ERROR: proc_addr2map failed to resolve 0x%jx", (uintmax_t)addr);
514 		return (-1);
515 	}
516 	if (open_object(mapping) != 0) {
517 		DPRINTFX("ERROR: failed to open object %s",
518 		    mapping->map.pr_mapname);
519 		return (-1);
520 	}
521 
522 	file = mapping->file;
523 	off = file->ehdr.e_type == ET_DYN ?
524 	    mapping->map.pr_vaddr - mapping->map.pr_offset : 0;
525 	if (addr < off)
526 		return (ENOENT);
527 	addr -= off;
528 
529 	error = lookup_symbol_by_addr(file->elf, &file->dynsymtab, addr, &s,
530 	    symcopy);
531 	if (error == ENOENT)
532 		error = lookup_symbol_by_addr(file->elf, &file->symtab, addr,
533 		    &s, symcopy);
534 	if (error == 0) {
535 		symcopy->st_value += off;
536 		demangle(s, name, namesz);
537 	}
538 	return (error);
539 }
540 
541 static struct map_info *
542 _proc_name2map(struct proc_handle *p, const char *name)
543 {
544 	char path[MAXPATHLEN], *base;
545 	struct map_info *mapping;
546 	size_t i, len;
547 
548 	if ((len = strlen(name)) == 0)
549 		return (NULL);
550 	if (p->nmappings == 0)
551 		if (proc_rdagent(p) == NULL)
552 			return (NULL);
553 	for (i = 0; i < p->nmappings; i++) {
554 		mapping = &p->mappings[i];
555 		(void)strlcpy(path, mapping->map.pr_mapname, sizeof(path));
556 		base = basename(path);
557 		if (strcmp(base, name) == 0)
558 			return (mapping);
559 	}
560 	/* If we didn't find a match, try matching prefixes of the basename. */
561 	for (i = 0; i < p->nmappings; i++) {
562 		mapping = &p->mappings[i];
563 		strlcpy(path, mapping->map.pr_mapname, sizeof(path));
564 		base = basename(path);
565 		if (strncmp(base, name, len) == 0)
566 			return (mapping);
567 	}
568 	if (strcmp(name, "a.out") == 0)
569 		return (_proc_addr2map(p,
570 		    p->mappings[p->exec_map].map.pr_vaddr));
571 	return (NULL);
572 }
573 
574 prmap_t *
575 proc_name2map(struct proc_handle *p, const char *name)
576 {
577 
578 	return (&_proc_name2map(p, name)->map);
579 }
580 
581 /*
582  * Look up the symbol with the given name and return a copy of it.
583  */
584 static int
585 lookup_symbol_by_name(Elf *elf, struct symtab *symtab, const char *symbol,
586     GElf_Sym *symcopy, prsyminfo_t *si)
587 {
588 	GElf_Sym sym;
589 	Elf_Data *data;
590 	char *s;
591 	int i;
592 
593 	if (symtab->nsyms == 0)
594 		return (ENOENT);
595 	data = symtab->data;
596 	for (i = 0; gelf_getsym(data, i, &sym) != NULL; i++) {
597 		s = elf_strptr(elf, symtab->stridx, sym.st_name);
598 		if (s != NULL && strcmp(s, symbol) == 0) {
599 			memcpy(symcopy, &sym, sizeof(*symcopy));
600 			if (si != NULL)
601 				si->prs_id = i;
602 			return (0);
603 		}
604 	}
605 	return (ENOENT);
606 }
607 
608 int
609 proc_name2sym(struct proc_handle *p, const char *object, const char *symbol,
610     GElf_Sym *symcopy, prsyminfo_t *si)
611 {
612 	struct file_info *file;
613 	struct map_info *mapping;
614 	uintptr_t off;
615 	int error;
616 
617 	if ((mapping = _proc_name2map(p, object)) == NULL) {
618 		DPRINTFX("ERROR: proc_name2map failed to resolve %s", object);
619 		return (-1);
620 	}
621 	if (open_object(mapping) != 0) {
622 		DPRINTFX("ERROR: failed to open object %s",
623 		    mapping->map.pr_mapname);
624 		return (-1);
625 	}
626 
627 	file = mapping->file;
628 	off = file->ehdr.e_type == ET_DYN ?
629 	    mapping->map.pr_vaddr - mapping->map.pr_offset : 0;
630 
631 	error = lookup_symbol_by_name(file->elf, &file->dynsymtab, symbol,
632 	    symcopy, si);
633 	if (error == ENOENT)
634 		error = lookup_symbol_by_name(file->elf, &file->symtab, symbol,
635 		    symcopy, si);
636 	if (error == 0)
637 		symcopy->st_value += off;
638 	return (error);
639 }
640 
641 ctf_file_t *
642 proc_name2ctf(struct proc_handle *p, const char *name)
643 {
644 #ifndef NO_CTF
645 	ctf_file_t *ctf;
646 	prmap_t *map;
647 	int error;
648 
649 	if ((map = proc_name2map(p, name)) == NULL)
650 		return (NULL);
651 
652 	ctf = ctf_open(map->pr_mapname, &error);
653 	return (ctf);
654 #else
655 	(void)p;
656 	(void)name;
657 	return (NULL);
658 #endif
659 }
660 
661 int
662 proc_iter_symbyaddr(struct proc_handle *p, const char *object, int which,
663     int mask, proc_sym_f *func, void *cd)
664 {
665 	GElf_Sym sym;
666 	struct file_info *file;
667 	struct map_info *mapping;
668 	struct symtab *symtab;
669 	const char *s;
670 	int error, i;
671 
672 	if ((mapping = _proc_name2map(p, object)) == NULL) {
673 		DPRINTFX("ERROR: proc_name2map failed to resolve %s", object);
674 		return (-1);
675 	}
676 	if (open_object(mapping) != 0) {
677 		DPRINTFX("ERROR: failed to open object %s",
678 		    mapping->map.pr_mapname);
679 		return (-1);
680 	}
681 
682 	file = mapping->file;
683 	symtab = which == PR_SYMTAB ? &file->symtab : &file->dynsymtab;
684 	if (symtab->nsyms == 0)
685 		return (-1);
686 
687 	error = 0;
688 	for (i = 0; gelf_getsym(symtab->data, i, &sym) != NULL; i++) {
689 		if (GELF_ST_BIND(sym.st_info) == STB_LOCAL &&
690 		    (mask & BIND_LOCAL) == 0)
691 			continue;
692 		if (GELF_ST_BIND(sym.st_info) == STB_GLOBAL &&
693 		    (mask & BIND_GLOBAL) == 0)
694 			continue;
695 		if (GELF_ST_BIND(sym.st_info) == STB_WEAK &&
696 		    (mask & BIND_WEAK) == 0)
697 			continue;
698 		if (GELF_ST_TYPE(sym.st_info) == STT_NOTYPE &&
699 		    (mask & TYPE_NOTYPE) == 0)
700 			continue;
701 		if (GELF_ST_TYPE(sym.st_info) == STT_OBJECT &&
702 		    (mask & TYPE_OBJECT) == 0)
703 			continue;
704 		if (GELF_ST_TYPE(sym.st_info) == STT_FUNC &&
705 		    (mask & TYPE_FUNC) == 0)
706 			continue;
707 		if (GELF_ST_TYPE(sym.st_info) == STT_SECTION &&
708 		    (mask & TYPE_SECTION) == 0)
709 			continue;
710 		if (GELF_ST_TYPE(sym.st_info) == STT_FILE &&
711 		    (mask & TYPE_FILE) == 0)
712 			continue;
713 		s = elf_strptr(file->elf, symtab->stridx, sym.st_name);
714 		if (file->ehdr.e_type == ET_DYN)
715 			sym.st_value += mapping->map.pr_vaddr;
716 		if ((error = (*func)(cd, &sym, s)) != 0)
717 			break;
718 	}
719 	return (error);
720 }
721