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