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