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