1 /*-
2 * Copyright (c) 2007 Hyogeol Lee <hyogeollee@gmail.com>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include <sys/queue.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <ar.h>
31 #include <assert.h>
32 #include <ctype.h>
33 #include <dwarf.h>
34 #include <err.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <gelf.h>
38 #include <getopt.h>
39 #include <inttypes.h>
40 #include <libdwarf.h>
41 #include <libelftc.h>
42 #include <stdbool.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <strings.h>
47 #include <unistd.h>
48
49 #include "_elftc.h"
50
51 ELFTC_VCSID("$Id: nm.c 3179 2015-03-31 19:38:56Z emaste $");
52
53 /* symbol information list */
54 STAILQ_HEAD(sym_head, sym_entry);
55
56 struct sym_entry {
57 char *name;
58 GElf_Sym *sym;
59 STAILQ_ENTRY(sym_entry) sym_entries;
60 };
61
62 typedef int (*fn_sort)(const void *, const void *);
63 typedef void (*fn_elem_print)(char, const char *, const GElf_Sym *, const char *);
64 typedef void (*fn_sym_print)(const GElf_Sym *);
65 typedef int (*fn_filter)(char, const GElf_Sym *, const char *);
66
67 /* output filter list */
68 static SLIST_HEAD(filter_head, filter_entry) nm_out_filter =
69 SLIST_HEAD_INITIALIZER(nm_out_filter);
70
71 struct filter_entry {
72 fn_filter fn;
73 SLIST_ENTRY(filter_entry) filter_entries;
74 };
75
76 struct sym_print_data {
77 struct sym_head *headp;
78 size_t sh_num, list_num;
79 const char *t_table, **s_table, *filename, *objname;
80 };
81
82 struct nm_prog_info {
83 const char *name;
84 const char *def_filename;
85 };
86
87 /* List for line number information. */
88 struct line_info_entry {
89 uint64_t addr; /* address */
90 uint64_t line; /* line number */
91 char *file; /* file name with path */
92 SLIST_ENTRY(line_info_entry) entries;
93 };
94 SLIST_HEAD(line_info_head, line_info_entry);
95
96 /* List for function line number information. */
97 struct func_info_entry {
98 char *name; /* function name */
99 char *file; /* file name with path */
100 uint64_t lowpc; /* low address */
101 uint64_t highpc; /* high address */
102 uint64_t line; /* line number */
103 SLIST_ENTRY(func_info_entry) entries;
104 };
105 SLIST_HEAD(func_info_head, func_info_entry);
106
107 /* List for variable line number information. */
108 struct var_info_entry {
109 char *name; /* variable name */
110 char *file; /* file name with path */
111 uint64_t addr; /* address */
112 uint64_t line; /* line number */
113 SLIST_ENTRY(var_info_entry) entries;
114 };
115 SLIST_HEAD(var_info_head, var_info_entry);
116
117 /* output numric type */
118 enum radix {
119 RADIX_OCT,
120 RADIX_HEX,
121 RADIX_DEC
122 };
123
124 /* output symbol type, PRINT_SYM_DYN for dynamic symbol only */
125 enum print_symbol {
126 PRINT_SYM_SYM,
127 PRINT_SYM_DYN
128 };
129
130 /* output name type */
131 enum print_name {
132 PRINT_NAME_NONE,
133 PRINT_NAME_FULL,
134 PRINT_NAME_MULTI
135 };
136
137 struct nm_prog_options {
138 enum print_symbol print_symbol;
139 enum print_name print_name;
140 enum radix t;
141 int demangle_type;
142 bool print_debug;
143 bool print_armap;
144 int print_size;
145 bool debug_line;
146 int def_only;
147 bool undef_only;
148 int sort_size;
149 bool sort_reverse;
150 int no_demangle;
151
152 /*
153 * function pointer to sort symbol list.
154 * possible function - cmp_name, cmp_none, cmp_size, cmp_value
155 */
156 fn_sort sort_fn;
157
158 /*
159 * function pointer to print symbol elem.
160 * possible function - sym_elem_print_all
161 * sym_elem_print_all_portable
162 * sym_elem_print_all_sysv
163 */
164 fn_elem_print elem_print_fn;
165
166 fn_sym_print value_print_fn;
167 fn_sym_print size_print_fn;
168 };
169
170 #define CHECK_SYM_PRINT_DATA(p) (p->headp == NULL || p->sh_num == 0 || \
171 p->t_table == NULL || p->s_table == NULL || p->filename == NULL)
172 #define IS_SYM_TYPE(t) ((t) == '?' || isalpha((t)) != 0)
173 #define IS_UNDEF_SYM_TYPE(t) ((t) == 'U' || (t) == 'v' || (t) == 'w')
174 #define UNUSED(p) ((void)p)
175
176 static int cmp_name(const void *, const void *);
177 static int cmp_none(const void *, const void *);
178 static int cmp_size(const void *, const void *);
179 static int cmp_value(const void *, const void *);
180 static void filter_dest(void);
181 static int filter_insert(fn_filter);
182 static void get_opt(int, char **);
183 static int get_sym(Elf *, struct sym_head *, int, size_t, size_t,
184 const char *, const char **, int);
185 static const char * get_sym_name(Elf *, const GElf_Sym *, size_t,
186 const char **, int);
187 static char get_sym_type(const GElf_Sym *, const char *);
188 static void global_dest(void);
189 static void global_init(void);
190 static bool is_sec_data(GElf_Shdr *);
191 static bool is_sec_debug(const char *);
192 static bool is_sec_nobits(GElf_Shdr *);
193 static bool is_sec_readonly(GElf_Shdr *);
194 static bool is_sec_text(GElf_Shdr *);
195 static void print_ar_index(int, Elf *);
196 static void print_header(const char *, const char *);
197 static void print_version(void);
198 static int read_elf(Elf *, const char *, Elf_Kind);
199 static int read_object(const char *);
200 static int read_files(int, char **);
201 static void set_opt_value_print_fn(enum radix);
202 static int sym_elem_def(char, const GElf_Sym *, const char *);
203 static int sym_elem_global(char, const GElf_Sym *, const char *);
204 static int sym_elem_global_static(char, const GElf_Sym *,
205 const char *);
206 static int sym_elem_nondebug(char, const GElf_Sym *, const char *);
207 static int sym_elem_nonzero_size(char, const GElf_Sym *,
208 const char *);
209 static void sym_elem_print_all(char, const char *,
210 const GElf_Sym *, const char *);
211 static void sym_elem_print_all_portable(char, const char *,
212 const GElf_Sym *, const char *);
213 static void sym_elem_print_all_sysv(char, const char *,
214 const GElf_Sym *, const char *);
215 static int sym_elem_undef(char, const GElf_Sym *, const char *);
216 static void sym_list_dest(struct sym_head *);
217 static int sym_list_insert(struct sym_head *, const char *,
218 const GElf_Sym *);
219 static void sym_list_print(struct sym_print_data *,
220 struct func_info_head *, struct var_info_head *,
221 struct line_info_head *);
222 static void sym_list_print_each(struct sym_entry *,
223 struct sym_print_data *, struct func_info_head *,
224 struct var_info_head *, struct line_info_head *);
225 static struct sym_entry *sym_list_sort(struct sym_print_data *);
226 static void sym_size_oct_print(const GElf_Sym *);
227 static void sym_size_hex_print(const GElf_Sym *);
228 static void sym_size_dec_print(const GElf_Sym *);
229 static void sym_value_oct_print(const GElf_Sym *);
230 static void sym_value_hex_print(const GElf_Sym *);
231 static void sym_value_dec_print(const GElf_Sym *);
232 static void usage(int);
233
234 static struct nm_prog_info nm_info;
235 static struct nm_prog_options nm_opts;
236 static int nm_elfclass;
237
238 /*
239 * Point to current sym_print_data to use portable qsort function.
240 * (e.g. There is no qsort_r function in NetBSD.)
241 *
242 * Using in sym_list_sort.
243 */
244 static struct sym_print_data *nm_print_data;
245
246 static const struct option nm_longopts[] = {
247 { "debug-syms", no_argument, NULL, 'a' },
248 { "defined-only", no_argument, &nm_opts.def_only, 1},
249 { "demangle", optional_argument, NULL, 'C' },
250 { "dynamic", no_argument, NULL, 'D' },
251 { "extern-only", no_argument, NULL, 'g' },
252 { "format", required_argument, NULL, 'F' },
253 { "help", no_argument, NULL, 'h' },
254 { "line-numbers", no_argument, NULL, 'l' },
255 { "no-demangle", no_argument, &nm_opts.no_demangle,
256 1},
257 { "no-sort", no_argument, NULL, 'p' },
258 { "numeric-sort", no_argument, NULL, 'v' },
259 { "print-armap", no_argument, NULL, 's' },
260 { "print-file-name", no_argument, NULL, 'A' },
261 { "print-size", no_argument, NULL, 'S' },
262 { "radix", required_argument, NULL, 't' },
263 { "reverse-sort", no_argument, NULL, 'r' },
264 { "size-sort", no_argument, &nm_opts.sort_size, 1},
265 { "undefined-only", no_argument, NULL, 'u' },
266 { "version", no_argument, NULL, 'V' },
267 { NULL, 0, NULL, 0 }
268 };
269
270 #if defined(ELFTC_NEED_BYTEORDER_EXTENSIONS)
271 static __inline uint32_t
be32dec(const void * pp)272 be32dec(const void *pp)
273 {
274 unsigned char const *p = (unsigned char const *)pp;
275
276 return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
277 }
278
279 static __inline uint32_t
le32dec(const void * pp)280 le32dec(const void *pp)
281 {
282 unsigned char const *p = (unsigned char const *)pp;
283
284 return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
285 }
286
287 static __inline uint64_t
be64dec(const void * pp)288 be64dec(const void *pp)
289 {
290 unsigned char const *p = (unsigned char const *)pp;
291
292 return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
293 }
294
295 static __inline uint64_t
le64dec(const void * pp)296 le64dec(const void *pp)
297 {
298 unsigned char const *p = (unsigned char const *)pp;
299
300 return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
301 }
302 #endif
303
304 static int
cmp_name(const void * l,const void * r)305 cmp_name(const void *l, const void *r)
306 {
307
308 assert(l != NULL);
309 assert(r != NULL);
310 assert(((const struct sym_entry *)l)->name != NULL);
311 assert(((const struct sym_entry *)r)->name != NULL);
312
313 return (strcmp(((const struct sym_entry *)l)->name,
314 ((const struct sym_entry *)r)->name));
315 }
316
317 static int
cmp_none(const void * l,const void * r)318 cmp_none(const void *l, const void *r)
319 {
320
321 UNUSED(l);
322 UNUSED(r);
323
324 return (0);
325 }
326
327 /* Size comparison. If l and r have same size, compare their name. */
328 static int
cmp_size(const void * lp,const void * rp)329 cmp_size(const void *lp, const void *rp)
330 {
331 const struct sym_entry *l, *r;
332
333 l = lp;
334 r = rp;
335
336 assert(l != NULL);
337 assert(l->name != NULL);
338 assert(l->sym != NULL);
339 assert(r != NULL);
340 assert(r->name != NULL);
341 assert(r->sym != NULL);
342
343 if (l->sym->st_size == r->sym->st_size)
344 return (strcmp(l->name, r->name));
345
346 return (l->sym->st_size - r->sym->st_size);
347 }
348
349 /* Value comparison. Undefined symbols come first. */
350 static int
cmp_value(const void * lp,const void * rp)351 cmp_value(const void *lp, const void *rp)
352 {
353 const struct sym_entry *l, *r;
354 const char *ttable;
355 int l_is_undef, r_is_undef;
356
357 l = lp;
358 r = rp;
359
360 assert(nm_print_data != NULL);
361 ttable = nm_print_data->t_table;
362
363 assert(l != NULL);
364 assert(l->name != NULL);
365 assert(l->sym != NULL);
366 assert(r != NULL);
367 assert(r->name != NULL);
368 assert(r->sym != NULL);
369 assert(ttable != NULL);
370
371 l_is_undef = IS_UNDEF_SYM_TYPE(get_sym_type(l->sym, ttable)) ? 1 : 0;
372 r_is_undef = IS_UNDEF_SYM_TYPE(get_sym_type(r->sym, ttable)) ? 1 : 0;
373
374 assert(l_is_undef + r_is_undef >= 0);
375 assert(l_is_undef + r_is_undef <= 2);
376
377 switch (l_is_undef + r_is_undef) {
378 case 0:
379 /* Both defined */
380 if (l->sym->st_value == r->sym->st_value)
381 return (strcmp(l->name, r->name));
382 return (l->sym->st_value > r->sym->st_value ? 1 : -1);
383 case 1:
384 /* One undefined */
385 return (l_is_undef == 0 ? 1 : -1);
386 case 2:
387 /* Both undefined */
388 return (strcmp(l->name, r->name));
389 }
390 /* NOTREACHED */
391
392 return (l->sym->st_value - r->sym->st_value);
393 }
394
395 static void
filter_dest(void)396 filter_dest(void)
397 {
398 struct filter_entry *e;
399
400 while (!SLIST_EMPTY(&nm_out_filter)) {
401 e = SLIST_FIRST(&nm_out_filter);
402 SLIST_REMOVE_HEAD(&nm_out_filter, filter_entries);
403 free(e);
404 }
405 }
406
407 static int
filter_insert(fn_filter filter_fn)408 filter_insert(fn_filter filter_fn)
409 {
410 struct filter_entry *e;
411
412 assert(filter_fn != NULL);
413
414 if ((e = malloc(sizeof(struct filter_entry))) == NULL) {
415 warn("malloc");
416 return (0);
417 }
418 e->fn = filter_fn;
419 SLIST_INSERT_HEAD(&nm_out_filter, e, filter_entries);
420
421 return (1);
422 }
423
424 static int
parse_demangle_option(const char * opt)425 parse_demangle_option(const char *opt)
426 {
427
428 if (opt == NULL)
429 return (ELFTC_DEM_UNKNOWN);
430 else if (!strncasecmp(opt, "gnu-v2", 6))
431 return (ELFTC_DEM_GNU2);
432 else if (!strncasecmp(opt, "gnu-v3", 6))
433 return (ELFTC_DEM_GNU3);
434 else if (!strncasecmp(opt, "arm", 3))
435 return (ELFTC_DEM_ARM);
436 else
437 errx(EXIT_FAILURE, "unknown demangling style '%s'", opt);
438
439 /* NOTREACHED */
440 return (0);
441 }
442
443 static void
get_opt(int argc,char ** argv)444 get_opt(int argc, char **argv)
445 {
446 int ch;
447 bool is_posix, oflag;
448
449 if (argc <= 0 || argv == NULL)
450 return;
451
452 oflag = is_posix = false;
453 nm_opts.t = RADIX_HEX;
454 while ((ch = getopt_long(argc, argv, "ABCDF:PSVaefghlnoprst:uvx",
455 nm_longopts, NULL)) != -1) {
456 switch (ch) {
457 case 'A':
458 nm_opts.print_name = PRINT_NAME_FULL;
459 break;
460 case 'B':
461 nm_opts.elem_print_fn = &sym_elem_print_all;
462 break;
463 case 'C':
464 nm_opts.demangle_type = parse_demangle_option(optarg);
465 break;
466 case 'D':
467 nm_opts.print_symbol = PRINT_SYM_DYN;
468 break;
469 case 'F':
470 /* sysv, bsd, posix */
471 switch (optarg[0]) {
472 case 'B':
473 case 'b':
474 nm_opts.elem_print_fn = &sym_elem_print_all;
475 break;
476 case 'P':
477 case 'p':
478 is_posix = true;
479 nm_opts.elem_print_fn =
480 &sym_elem_print_all_portable;
481 break;
482 case 'S':
483 case 's':
484 nm_opts.elem_print_fn =
485 &sym_elem_print_all_sysv;
486 break;
487 default:
488 warnx("%s: Invalid format", optarg);
489 usage(1);
490 }
491
492 break;
493 case 'P':
494 is_posix = true;
495 nm_opts.elem_print_fn = &sym_elem_print_all_portable;
496 break;
497 case 'S':
498 nm_opts.print_size = 1;
499 break;
500 case 'V':
501 print_version();
502 /* NOTREACHED */
503 case 'a':
504 nm_opts.print_debug = true;
505 break;
506 case 'e':
507 filter_insert(sym_elem_global_static);
508 break;
509 case 'f':
510 break;
511 case 'g':
512 filter_insert(sym_elem_global);
513 break;
514 case 'h':
515 usage(0);
516 break;
517 case 'l':
518 nm_opts.debug_line = true;
519 break;
520 case 'n':
521 case 'v':
522 nm_opts.sort_fn = &cmp_value;
523 break;
524 case 'o':
525 oflag = true;
526 break;
527 case 'p':
528 nm_opts.sort_fn = &cmp_none;
529 break;
530 case 'r':
531 nm_opts.sort_reverse = true;
532 break;
533 case 's':
534 nm_opts.print_armap = true;
535 break;
536 case 't':
537 /* t require always argument to getopt_long */
538 switch (optarg[0]) {
539 case 'd':
540 nm_opts.t = RADIX_DEC;
541 break;
542 case 'o':
543 nm_opts.t = RADIX_OCT;
544 break;
545 case 'x':
546 nm_opts.t = RADIX_HEX;
547 break;
548 default:
549 warnx("%s: Invalid radix", optarg);
550 usage(1);
551 }
552 break;
553 case 'u':
554 filter_insert(sym_elem_undef);
555 nm_opts.undef_only = true;
556 break;
557 /* case 'v': see case 'n' above. */
558 case 'x':
559 nm_opts.t = RADIX_HEX;
560 break;
561 case 0:
562 if (nm_opts.sort_size != 0) {
563 nm_opts.sort_fn = &cmp_size;
564 filter_insert(sym_elem_def);
565 filter_insert(sym_elem_nonzero_size);
566 }
567 if (nm_opts.def_only != 0)
568 filter_insert(sym_elem_def);
569 if (nm_opts.no_demangle != 0)
570 nm_opts.demangle_type = -1;
571 break;
572 default :
573 usage(1);
574 }
575 }
576
577 /*
578 * In POSIX mode, the '-o' option controls the output radix.
579 * In non-POSIX mode, the option is a synonym for the '-A' and
580 * '--print-file-name' options.
581 */
582 if (oflag) {
583 if (is_posix)
584 nm_opts.t = RADIX_OCT;
585 else
586 nm_opts.print_name = PRINT_NAME_FULL;
587 }
588
589 assert(nm_opts.sort_fn != NULL && "nm_opts.sort_fn is null");
590 assert(nm_opts.elem_print_fn != NULL &&
591 "nm_opts.elem_print_fn is null");
592 assert(nm_opts.value_print_fn != NULL &&
593 "nm_opts.value_print_fn is null");
594
595 set_opt_value_print_fn(nm_opts.t);
596
597 if (nm_opts.undef_only == true) {
598 if (nm_opts.sort_fn == &cmp_size)
599 errx(EXIT_FAILURE,
600 "--size-sort with -u is meaningless");
601 if (nm_opts.def_only != 0)
602 errx(EXIT_FAILURE,
603 "-u with --defined-only is meaningless");
604 }
605 if (nm_opts.print_debug == false)
606 filter_insert(sym_elem_nondebug);
607 if (nm_opts.sort_reverse == true && nm_opts.sort_fn == cmp_none)
608 nm_opts.sort_reverse = false;
609 }
610
611 /*
612 * Get symbol information from elf.
613 */
614 static int
get_sym(Elf * elf,struct sym_head * headp,int shnum,size_t dynndx,size_t strndx,const char * type_table,const char ** sec_table,int sec_table_size)615 get_sym(Elf *elf, struct sym_head *headp, int shnum, size_t dynndx,
616 size_t strndx, const char *type_table, const char **sec_table,
617 int sec_table_size)
618 {
619 Elf_Scn *scn;
620 Elf_Data *data;
621 GElf_Shdr shdr;
622 GElf_Sym sym;
623 struct filter_entry *fep;
624 size_t ndx;
625 int rtn;
626 const char *sym_name;
627 char type;
628 bool filter;
629 int i, j;
630
631 assert(elf != NULL);
632 assert(headp != NULL);
633
634 rtn = 0;
635 for (i = 1; i < shnum; i++) {
636 if ((scn = elf_getscn(elf, i)) == NULL) {
637 warnx("elf_getscn failed: %s", elf_errmsg(-1));
638 continue;
639 }
640 if (gelf_getshdr(scn, &shdr) != &shdr) {
641 warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
642 continue;
643 }
644 if (shdr.sh_type == SHT_SYMTAB) {
645 if (nm_opts.print_symbol != PRINT_SYM_SYM)
646 continue;
647 } else if (shdr.sh_type == SHT_DYNSYM) {
648 if (nm_opts.print_symbol != PRINT_SYM_DYN)
649 continue;
650 } else
651 continue;
652
653 ndx = shdr.sh_type == SHT_DYNSYM ? dynndx : strndx;
654
655 data = NULL;
656 while ((data = elf_getdata(scn, data)) != NULL) {
657 j = 1;
658 while (gelf_getsym(data, j++, &sym) != NULL) {
659 sym_name = get_sym_name(elf, &sym, ndx,
660 sec_table, sec_table_size);
661 filter = false;
662 type = get_sym_type(&sym, type_table);
663 SLIST_FOREACH(fep, &nm_out_filter,
664 filter_entries) {
665 if (!fep->fn(type, &sym, sym_name)) {
666 filter = true;
667 break;
668 }
669 }
670 if (filter == false) {
671 if (sym_list_insert(headp, sym_name,
672 &sym) == 0)
673 return (0);
674 rtn++;
675 }
676 }
677 }
678 }
679
680 return (rtn);
681 }
682
683 static const char *
get_sym_name(Elf * elf,const GElf_Sym * sym,size_t ndx,const char ** sec_table,int sec_table_size)684 get_sym_name(Elf *elf, const GElf_Sym *sym, size_t ndx, const char **sec_table,
685 int sec_table_size)
686 {
687 const char *sym_name;
688
689 sym_name = NULL;
690
691 /* Show section name as symbol name for STT_SECTION symbols. */
692 if (GELF_ST_TYPE(sym->st_info) == STT_SECTION) {
693 if (sec_table != NULL && sym->st_shndx < sec_table_size)
694 sym_name = sec_table[sym->st_shndx];
695 } else
696 sym_name = elf_strptr(elf, ndx, sym->st_name);
697
698 if (sym_name == NULL)
699 sym_name = "(null)";
700
701 return (sym_name);
702 }
703
704 static char
get_sym_type(const GElf_Sym * sym,const char * type_table)705 get_sym_type(const GElf_Sym *sym, const char *type_table)
706 {
707 bool is_local;
708
709 if (sym == NULL || type_table == NULL)
710 return ('?');
711
712 is_local = sym->st_info >> 4 == STB_LOCAL;
713
714 if (sym->st_shndx == SHN_ABS) /* absolute */
715 return (is_local ? 'a' : 'A');
716
717 if (sym->st_shndx == SHN_COMMON) /* common */
718 return ('C');
719
720 if ((sym->st_info) >> 4 == STB_WEAK) { /* weak */
721 if ((sym->st_info & 0xf) == STT_OBJECT)
722 return (sym->st_shndx == SHN_UNDEF ? 'v' : 'V');
723
724 return (sym->st_shndx == SHN_UNDEF ? 'w' : 'W');
725 }
726
727 if (sym->st_shndx == SHN_UNDEF) /* undefined */
728 return ('U');
729
730 return (is_local == true && type_table[sym->st_shndx] != 'N' ?
731 tolower((unsigned char) type_table[sym->st_shndx]) :
732 type_table[sym->st_shndx]);
733 }
734
735 static void
global_dest(void)736 global_dest(void)
737 {
738
739 filter_dest();
740 }
741
742 static void
global_init(void)743 global_init(void)
744 {
745
746 if (elf_version(EV_CURRENT) == EV_NONE)
747 errx(EXIT_FAILURE, "elf_version error");
748
749 nm_info.name = ELFTC_GETPROGNAME();
750 nm_info.def_filename = "a.out";
751 nm_opts.print_symbol = PRINT_SYM_SYM;
752 nm_opts.print_name = PRINT_NAME_NONE;
753 nm_opts.demangle_type = -1;
754 nm_opts.print_debug = false;
755 nm_opts.print_armap = false;
756 nm_opts.print_size = 0;
757 nm_opts.debug_line = false;
758 nm_opts.def_only = 0;
759 nm_opts.undef_only = false;
760 nm_opts.sort_size = 0;
761 nm_opts.sort_reverse = false;
762 nm_opts.no_demangle = 0;
763 nm_opts.sort_fn = &cmp_name;
764 nm_opts.elem_print_fn = &sym_elem_print_all;
765 nm_opts.value_print_fn = &sym_value_dec_print;
766 nm_opts.size_print_fn = &sym_size_dec_print;
767 SLIST_INIT(&nm_out_filter);
768 }
769
770 static bool
is_sec_data(GElf_Shdr * s)771 is_sec_data(GElf_Shdr *s)
772 {
773
774 assert(s != NULL && "shdr is NULL");
775
776 return (((s->sh_flags & SHF_ALLOC) != 0) && s->sh_type != SHT_NOBITS);
777 }
778
779 static bool
is_sec_debug(const char * shname)780 is_sec_debug(const char *shname)
781 {
782 const char *dbg_sec[] = {
783 ".debug",
784 ".gnu.linkonce.wi.",
785 ".line",
786 ".rel.debug",
787 ".rela.debug",
788 ".stab",
789 NULL
790 };
791 const char **p;
792
793 assert(shname != NULL && "shname is NULL");
794
795 for (p = dbg_sec; *p; p++) {
796 if (!strncmp(shname, *p, strlen(*p)))
797 return (true);
798 }
799
800 return (false);
801 }
802
803 static bool
is_sec_nobits(GElf_Shdr * s)804 is_sec_nobits(GElf_Shdr *s)
805 {
806
807 assert(s != NULL && "shdr is NULL");
808
809 return (s->sh_type == SHT_NOBITS);
810 }
811
812 static bool
is_sec_readonly(GElf_Shdr * s)813 is_sec_readonly(GElf_Shdr *s)
814 {
815
816 assert(s != NULL && "shdr is NULL");
817
818 return ((s->sh_flags & SHF_WRITE) == 0);
819 }
820
821 static bool
is_sec_text(GElf_Shdr * s)822 is_sec_text(GElf_Shdr *s)
823 {
824
825 assert(s != NULL && "shdr is NULL");
826
827 return ((s->sh_flags & SHF_EXECINSTR) != 0);
828 }
829
830 static void
print_ar_index(int fd,Elf * arf)831 print_ar_index(int fd, Elf *arf)
832 {
833 Elf *elf;
834 Elf_Arhdr *arhdr;
835 Elf_Arsym *arsym;
836 Elf_Cmd cmd;
837 off_t start;
838 size_t arsym_size;
839
840 if (arf == NULL)
841 return;
842
843 if ((arsym = elf_getarsym(arf, &arsym_size)) == NULL)
844 return;
845
846 printf("\nArchive index:\n");
847
848 start = arsym->as_off;
849 cmd = ELF_C_READ;
850 while (arsym_size > 1) {
851 if (elf_rand(arf, arsym->as_off) == arsym->as_off &&
852 (elf = elf_begin(fd, cmd, arf)) != NULL) {
853 if ((arhdr = elf_getarhdr(elf)) != NULL)
854 printf("%s in %s\n", arsym->as_name,
855 arhdr->ar_name != NULL ?
856 arhdr->ar_name : arhdr->ar_rawname);
857 elf_end(elf);
858 }
859 ++arsym;
860 --arsym_size;
861 }
862
863 elf_rand(arf, start);
864 }
865
866 #define DEMANGLED_BUFFER_SIZE (8 * 1024)
867 #define PRINT_DEMANGLED_NAME(FORMAT, NAME) do { \
868 char _demangled[DEMANGLED_BUFFER_SIZE]; \
869 if (nm_opts.demangle_type < 0 || \
870 elftc_demangle((NAME), _demangled, sizeof(_demangled), \
871 nm_opts.demangle_type) < 0) \
872 printf((FORMAT), (NAME)); \
873 else \
874 printf((FORMAT), _demangled); \
875 } while (0)
876
877 static void
print_header(const char * file,const char * obj)878 print_header(const char *file, const char *obj)
879 {
880
881 if (file == NULL)
882 return;
883
884 if (nm_opts.elem_print_fn == &sym_elem_print_all_sysv) {
885 printf("\n\n%s from %s",
886 nm_opts.undef_only == false ? "Symbols" :
887 "Undefined symbols", file);
888 if (obj != NULL)
889 printf("[%s]", obj);
890 printf(":\n\n");
891
892 printf("\
893 Name Value Class Type Size Line Section\n\n");
894 } else {
895 /* archive file without -A option and POSIX */
896 if (nm_opts.print_name != PRINT_NAME_FULL && obj != NULL) {
897 if (nm_opts.elem_print_fn ==
898 sym_elem_print_all_portable)
899 printf("%s[%s]:\n", file, obj);
900 else if (nm_opts.elem_print_fn == sym_elem_print_all)
901 printf("\n%s:\n", obj);
902 /* multiple files(not archive) without -A option */
903 } else if (nm_opts.print_name == PRINT_NAME_MULTI) {
904 if (nm_opts.elem_print_fn == sym_elem_print_all)
905 printf("\n");
906 printf("%s:\n", file);
907 }
908 }
909 }
910
911 static void
print_version(void)912 print_version(void)
913 {
914
915 (void) printf("%s (%s)\n", nm_info.name, elftc_version());
916 exit(0);
917 }
918
919 static uint64_t
get_block_value(Dwarf_Debug dbg,Dwarf_Block * block)920 get_block_value(Dwarf_Debug dbg, Dwarf_Block *block)
921 {
922 Elf *elf;
923 GElf_Ehdr eh;
924 Dwarf_Error de;
925
926 if (dwarf_get_elf(dbg, &elf, &de) != DW_DLV_OK) {
927 warnx("dwarf_get_elf failed: %s", dwarf_errmsg(de));
928 return (0);
929 }
930
931 if (gelf_getehdr(elf, &eh) != &eh) {
932 warnx("gelf_getehdr failed: %s", elf_errmsg(-1));
933 return (0);
934 }
935
936 if (block->bl_len == 5) {
937 if (eh.e_ident[EI_DATA] == ELFDATA2LSB)
938 return (le32dec((uint8_t *) block->bl_data + 1));
939 else
940 return (be32dec((uint8_t *) block->bl_data + 1));
941 } else if (block->bl_len == 9) {
942 if (eh.e_ident[EI_DATA] == ELFDATA2LSB)
943 return (le64dec((uint8_t *) block->bl_data + 1));
944 else
945 return (be64dec((uint8_t *) block->bl_data + 1));
946 }
947
948 return (0);
949 }
950
951 static char *
find_object_name(Dwarf_Debug dbg,Dwarf_Die die)952 find_object_name(Dwarf_Debug dbg, Dwarf_Die die)
953 {
954 Dwarf_Die ret_die;
955 Dwarf_Attribute at;
956 Dwarf_Off off;
957 Dwarf_Error de;
958 const char *str;
959 char *name;
960
961 if (dwarf_attrval_string(die, DW_AT_name, &str, &de) == DW_DLV_OK) {
962 if ((name = strdup(str)) == NULL) {
963 warn("strdup");
964 return (NULL);
965 }
966 return (name);
967 }
968
969 if (dwarf_attr(die, DW_AT_specification, &at, &de) != DW_DLV_OK)
970 return (NULL);
971
972 if (dwarf_global_formref(at, &off, &de) != DW_DLV_OK)
973 return (NULL);
974
975 if (dwarf_offdie(dbg, off, &ret_die, &de) != DW_DLV_OK)
976 return (NULL);
977
978 return (find_object_name(dbg, ret_die));
979 }
980
981 static void
search_line_attr(Dwarf_Debug dbg,struct func_info_head * func_info,struct var_info_head * var_info,Dwarf_Die die,char ** src_files,Dwarf_Signed filecount)982 search_line_attr(Dwarf_Debug dbg, struct func_info_head *func_info,
983 struct var_info_head *var_info, Dwarf_Die die, char **src_files,
984 Dwarf_Signed filecount)
985 {
986 Dwarf_Attribute at;
987 Dwarf_Unsigned udata;
988 Dwarf_Half tag;
989 Dwarf_Block *block;
990 Dwarf_Bool flag;
991 Dwarf_Die ret_die;
992 Dwarf_Error de;
993 struct func_info_entry *func;
994 struct var_info_entry *var;
995 int ret;
996
997 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
998 warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
999 goto cont_search;
1000 }
1001
1002 /* We're interested in DIEs which define functions or variables. */
1003 if (tag != DW_TAG_subprogram && tag != DW_TAG_entry_point &&
1004 tag != DW_TAG_inlined_subroutine && tag != DW_TAG_variable)
1005 goto cont_search;
1006
1007 if (tag == DW_TAG_variable) {
1008
1009 /* Ignore "artificial" variable. */
1010 if (dwarf_attrval_flag(die, DW_AT_artificial, &flag, &de) ==
1011 DW_DLV_OK && flag)
1012 goto cont_search;
1013
1014 /* Ignore pure declaration. */
1015 if (dwarf_attrval_flag(die, DW_AT_declaration, &flag, &de) ==
1016 DW_DLV_OK && flag)
1017 goto cont_search;
1018
1019 /* Ignore stack varaibles. */
1020 if (dwarf_attrval_flag(die, DW_AT_external, &flag, &de) !=
1021 DW_DLV_OK || !flag)
1022 goto cont_search;
1023
1024 if ((var = calloc(1, sizeof(*var))) == NULL) {
1025 warn("calloc failed");
1026 goto cont_search;
1027 }
1028
1029 if (dwarf_attrval_unsigned(die, DW_AT_decl_file, &udata,
1030 &de) == DW_DLV_OK && udata > 0 &&
1031 (Dwarf_Signed) (udata - 1) < filecount) {
1032 var->file = strdup(src_files[udata - 1]);
1033 if (var->file == NULL) {
1034 warn("strdup");
1035 free(var);
1036 goto cont_search;
1037 }
1038 }
1039
1040 if (dwarf_attrval_unsigned(die, DW_AT_decl_line, &udata, &de) ==
1041 DW_DLV_OK)
1042 var->line = udata;
1043
1044 var->name = find_object_name(dbg, die);
1045 if (var->name == NULL) {
1046 if (var->file)
1047 free(var->file);
1048 free(var);
1049 goto cont_search;
1050 }
1051
1052 if (dwarf_attr(die, DW_AT_location, &at, &de) == DW_DLV_OK &&
1053 dwarf_formblock(at, &block, &de) == DW_DLV_OK) {
1054 /*
1055 * Since we ignored stack variables, the rest are the
1056 * external varaibles which should always use DW_OP_addr
1057 * operator for DW_AT_location value.
1058 */
1059 if (*((uint8_t *)block->bl_data) == DW_OP_addr)
1060 var->addr = get_block_value(dbg, block);
1061 }
1062
1063 SLIST_INSERT_HEAD(var_info, var, entries);
1064
1065 } else {
1066
1067 if ((func = calloc(1, sizeof(*func))) == NULL) {
1068 warn("calloc failed");
1069 goto cont_search;
1070 }
1071
1072 /*
1073 * Note that dwarf_attrval_unsigned() handles DW_AT_abstract_origin
1074 * internally, so it can retrieve DW_AT_decl_file/DW_AT_decl_line
1075 * attributes for inlined functions as well.
1076 */
1077 if (dwarf_attrval_unsigned(die, DW_AT_decl_file, &udata,
1078 &de) == DW_DLV_OK && udata > 0 &&
1079 (Dwarf_Signed) (udata - 1) < filecount) {
1080 func->file = strdup(src_files[udata - 1]);
1081 if (func->file == NULL) {
1082 warn("strdup");
1083 free(func);
1084 goto cont_search;
1085 }
1086 }
1087
1088 if (dwarf_attrval_unsigned(die, DW_AT_decl_line, &udata, &de) ==
1089 DW_DLV_OK)
1090 func->line = udata;
1091
1092 func->name = find_object_name(dbg, die);
1093 if (func->name == NULL) {
1094 if (func->file)
1095 free(func->file);
1096 free(func);
1097 goto cont_search;
1098 }
1099
1100 if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &udata, &de) ==
1101 DW_DLV_OK)
1102 func->lowpc = udata;
1103 if (dwarf_attrval_unsigned(die, DW_AT_high_pc, &udata, &de) ==
1104 DW_DLV_OK)
1105 func->highpc = udata;
1106
1107 SLIST_INSERT_HEAD(func_info, func, entries);
1108 }
1109
1110 cont_search:
1111
1112 /* Search children. */
1113 ret = dwarf_child(die, &ret_die, &de);
1114 if (ret == DW_DLV_ERROR)
1115 warnx("dwarf_child: %s", dwarf_errmsg(de));
1116 else if (ret == DW_DLV_OK)
1117 search_line_attr(dbg, func_info, var_info, ret_die, src_files,
1118 filecount);
1119
1120 /* Search sibling. */
1121 ret = dwarf_siblingof(dbg, die, &ret_die, &de);
1122 if (ret == DW_DLV_ERROR)
1123 warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
1124 else if (ret == DW_DLV_OK)
1125 search_line_attr(dbg, func_info, var_info, ret_die, src_files,
1126 filecount);
1127
1128 dwarf_dealloc(dbg, die, DW_DLA_DIE);
1129 }
1130
1131 /*
1132 * Read elf file and collect symbol information, sort them, print.
1133 * Return 1 at failed, 0 at success.
1134 */
1135 static int
read_elf(Elf * elf,const char * filename,Elf_Kind kind)1136 read_elf(Elf *elf, const char *filename, Elf_Kind kind)
1137 {
1138 Dwarf_Debug dbg;
1139 Dwarf_Die die;
1140 Dwarf_Error de;
1141 Dwarf_Half tag;
1142 Elf_Arhdr *arhdr;
1143 Elf_Scn *scn;
1144 GElf_Shdr shdr;
1145 GElf_Half i;
1146 Dwarf_Line *lbuf;
1147 Dwarf_Unsigned lineno;
1148 Dwarf_Signed lcount, filecount;
1149 Dwarf_Addr lineaddr;
1150 struct sym_print_data p_data;
1151 struct sym_head list_head;
1152 struct line_info_head *line_info;
1153 struct func_info_head *func_info;
1154 struct var_info_head *var_info;
1155 struct line_info_entry *lie;
1156 struct func_info_entry *func;
1157 struct var_info_entry *var;
1158 const char *shname, *objname;
1159 char *type_table, **sec_table, *sfile, **src_files;
1160 size_t shstrndx, shnum, dynndx, strndx;
1161 int ret, rtn, e_err;
1162
1163 #define OBJNAME (objname == NULL ? filename : objname)
1164
1165 assert(filename != NULL && "filename is null");
1166
1167 STAILQ_INIT(&list_head);
1168 type_table = NULL;
1169 sec_table = NULL;
1170 line_info = NULL;
1171 func_info = NULL;
1172 var_info = NULL;
1173 objname = NULL;
1174 dynndx = SHN_UNDEF;
1175 strndx = SHN_UNDEF;
1176 rtn = 0;
1177
1178 nm_elfclass = gelf_getclass(elf);
1179
1180 if (kind == ELF_K_AR) {
1181 if ((arhdr = elf_getarhdr(elf)) == NULL)
1182 goto next_cmd;
1183 objname = arhdr->ar_name != NULL ? arhdr->ar_name :
1184 arhdr->ar_rawname;
1185 }
1186 if (!elf_getshnum(elf, &shnum)) {
1187 if ((e_err = elf_errno()) != 0)
1188 warnx("%s: %s", OBJNAME, elf_errmsg(e_err));
1189 else
1190 warnx("%s: cannot get section number", OBJNAME);
1191 rtn = 1;
1192 goto next_cmd;
1193 }
1194 if (shnum == 0) {
1195 warnx("%s: has no section", OBJNAME);
1196 rtn = 1;
1197 goto next_cmd;
1198 }
1199 if (!elf_getshstrndx(elf, &shstrndx)) {
1200 warnx("%s: cannot get str index", OBJNAME);
1201 rtn = 1;
1202 goto next_cmd;
1203 }
1204 /* type_table for type determine */
1205 if ((type_table = malloc(sizeof(char) * shnum)) == NULL) {
1206 warn("%s: malloc", OBJNAME);
1207 rtn = 1;
1208 goto next_cmd;
1209 }
1210 /* sec_table for section name to display in sysv format */
1211 if ((sec_table = calloc(shnum, sizeof(char *))) == NULL) {
1212 warn("%s: calloc", OBJNAME);
1213 rtn = 1;
1214 goto next_cmd;
1215 }
1216
1217 type_table[0] = 'U';
1218 if ((sec_table[0] = strdup("*UND*")) == NULL) {
1219 warn("strdup");
1220 goto next_cmd;
1221 }
1222
1223 for (i = 1; i < shnum; ++i) {
1224 type_table[i] = 'U';
1225 if ((scn = elf_getscn(elf, i)) == NULL) {
1226 if ((e_err = elf_errno()) != 0)
1227 warnx("%s: %s", OBJNAME, elf_errmsg(e_err));
1228 else
1229 warnx("%s: cannot get section", OBJNAME);
1230 rtn = 1;
1231 goto next_cmd;
1232 }
1233 if (gelf_getshdr(scn, &shdr) == NULL)
1234 goto next_cmd;
1235
1236 /*
1237 * Cannot test by type and attribute for dynstr, strtab
1238 */
1239 shname = elf_strptr(elf, shstrndx, (size_t) shdr.sh_name);
1240 if (shname != NULL) {
1241 if ((sec_table[i] = strdup(shname)) == NULL) {
1242 warn("strdup");
1243 goto next_cmd;
1244 }
1245 if (!strncmp(shname, ".dynstr", 7)) {
1246 dynndx = elf_ndxscn(scn);
1247 if (dynndx == SHN_UNDEF) {
1248 warnx("%s: elf_ndxscn failed: %s",
1249 OBJNAME, elf_errmsg(-1));
1250 goto next_cmd;
1251 }
1252 }
1253 if (!strncmp(shname, ".strtab", 7)) {
1254 strndx = elf_ndxscn(scn);
1255 if (strndx == SHN_UNDEF) {
1256 warnx("%s: elf_ndxscn failed: %s",
1257 OBJNAME, elf_errmsg(-1));
1258 goto next_cmd;
1259 }
1260 }
1261 } else {
1262 sec_table[i] = strdup("*UND*");
1263 if (sec_table[i] == NULL) {
1264 warn("strdup");
1265 goto next_cmd;
1266 }
1267 }
1268
1269
1270 if (is_sec_text(&shdr))
1271 type_table[i] = 'T';
1272 else if (is_sec_data(&shdr)) {
1273 if (is_sec_readonly(&shdr))
1274 type_table[i] = 'R';
1275 else
1276 type_table[i] = 'D';
1277 } else if (is_sec_nobits(&shdr))
1278 type_table[i] = 'B';
1279 else if (is_sec_debug(shname))
1280 type_table[i] = 'N';
1281 else if (is_sec_readonly(&shdr) && !is_sec_nobits(&shdr))
1282 type_table[i] = 'n';
1283 }
1284
1285 print_header(filename, objname);
1286
1287 if ((dynndx == SHN_UNDEF && nm_opts.print_symbol == PRINT_SYM_DYN) ||
1288 (strndx == SHN_UNDEF && nm_opts.print_symbol == PRINT_SYM_SYM)) {
1289 warnx("%s: no symbols", OBJNAME);
1290 /* This is not an error case */
1291 goto next_cmd;
1292 }
1293
1294 STAILQ_INIT(&list_head);
1295
1296 if (!nm_opts.debug_line)
1297 goto process_sym;
1298
1299 /*
1300 * Collect dwarf line number information.
1301 */
1302
1303 if (dwarf_elf_init(elf, DW_DLC_READ, NULL, NULL, &dbg, &de) !=
1304 DW_DLV_OK) {
1305 warnx("dwarf_elf_init failed: %s", dwarf_errmsg(de));
1306 goto process_sym;
1307 }
1308
1309 line_info = malloc(sizeof(struct line_info_head));
1310 func_info = malloc(sizeof(struct func_info_head));
1311 var_info = malloc(sizeof(struct var_info_head));
1312 if (line_info == NULL || func_info == NULL || var_info == NULL) {
1313 warn("malloc");
1314 (void) dwarf_finish(dbg, &de);
1315 goto process_sym;
1316 }
1317 SLIST_INIT(line_info);
1318 SLIST_INIT(func_info);
1319 SLIST_INIT(var_info);
1320
1321 while ((ret = dwarf_next_cu_header(dbg, NULL, NULL, NULL, NULL, NULL,
1322 &de)) == DW_DLV_OK) {
1323 die = NULL;
1324 while (dwarf_siblingof(dbg, die, &die, &de) == DW_DLV_OK) {
1325 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
1326 warnx("dwarf_tag failed: %s",
1327 dwarf_errmsg(de));
1328 continue;
1329 }
1330 /* XXX: What about DW_TAG_partial_unit? */
1331 if (tag == DW_TAG_compile_unit)
1332 break;
1333 }
1334 if (die == NULL) {
1335 warnx("could not find DW_TAG_compile_unit die");
1336 continue;
1337 }
1338
1339 /* Retrieve source file list. */
1340 ret = dwarf_srcfiles(die, &src_files, &filecount, &de);
1341 if (ret == DW_DLV_ERROR)
1342 warnx("dwarf_srclines: %s", dwarf_errmsg(de));
1343 if (ret != DW_DLV_OK)
1344 continue;
1345
1346 /*
1347 * Retrieve line number information from .debug_line section.
1348 */
1349
1350 ret = dwarf_srclines(die, &lbuf, &lcount, &de);
1351 if (ret == DW_DLV_ERROR)
1352 warnx("dwarf_srclines: %s", dwarf_errmsg(de));
1353 if (ret != DW_DLV_OK)
1354 goto line_attr;
1355 for (i = 0; (Dwarf_Signed) i < lcount; i++) {
1356 if (dwarf_lineaddr(lbuf[i], &lineaddr, &de)) {
1357 warnx("dwarf_lineaddr: %s", dwarf_errmsg(de));
1358 continue;
1359 }
1360 if (dwarf_lineno(lbuf[i], &lineno, &de)) {
1361 warnx("dwarf_lineno: %s", dwarf_errmsg(de));
1362 continue;
1363 }
1364 if (dwarf_linesrc(lbuf[i], &sfile, &de)) {
1365 warnx("dwarf_linesrc: %s", dwarf_errmsg(de));
1366 continue;
1367 }
1368 if ((lie = malloc(sizeof(*lie))) == NULL) {
1369 warn("malloc");
1370 continue;
1371 }
1372 lie->addr = lineaddr;
1373 lie->line = lineno;
1374 lie->file = strdup(sfile);
1375 if (lie->file == NULL) {
1376 warn("strdup");
1377 free(lie);
1378 continue;
1379 }
1380 SLIST_INSERT_HEAD(line_info, lie, entries);
1381 }
1382
1383 line_attr:
1384 /* Retrieve line number information from DIEs. */
1385 search_line_attr(dbg, func_info, var_info, die, src_files, filecount);
1386 }
1387
1388 (void) dwarf_finish(dbg, &de);
1389
1390 process_sym:
1391
1392 p_data.list_num = get_sym(elf, &list_head, shnum, dynndx, strndx,
1393 type_table, (void *) sec_table, shnum);
1394
1395 if (p_data.list_num == 0)
1396 goto next_cmd;
1397
1398 p_data.headp = &list_head;
1399 p_data.sh_num = shnum;
1400 p_data.t_table = type_table;
1401 p_data.s_table = (void *) sec_table;
1402 p_data.filename = filename;
1403 p_data.objname = objname;
1404
1405 sym_list_print(&p_data, func_info, var_info, line_info);
1406
1407 next_cmd:
1408 if (nm_opts.debug_line) {
1409 if (func_info != NULL) {
1410 while (!SLIST_EMPTY(func_info)) {
1411 func = SLIST_FIRST(func_info);
1412 SLIST_REMOVE_HEAD(func_info, entries);
1413 free(func->file);
1414 free(func->name);
1415 free(func);
1416 }
1417 free(func_info);
1418 func_info = NULL;
1419 }
1420 if (var_info != NULL) {
1421 while (!SLIST_EMPTY(var_info)) {
1422 var = SLIST_FIRST(var_info);
1423 SLIST_REMOVE_HEAD(var_info, entries);
1424 free(var->file);
1425 free(var->name);
1426 free(var);
1427 }
1428 free(var_info);
1429 var_info = NULL;
1430 }
1431 if (line_info != NULL) {
1432 while (!SLIST_EMPTY(line_info)) {
1433 lie = SLIST_FIRST(line_info);
1434 SLIST_REMOVE_HEAD(line_info, entries);
1435 free(lie->file);
1436 free(lie);
1437 }
1438 free(line_info);
1439 line_info = NULL;
1440 }
1441 }
1442
1443 if (sec_table != NULL)
1444 for (i = 0; i < shnum; ++i)
1445 free(sec_table[i]);
1446 free(sec_table);
1447 free(type_table);
1448
1449 sym_list_dest(&list_head);
1450
1451 return (rtn);
1452
1453 #undef OBJNAME
1454 }
1455
1456 static int
read_object(const char * filename)1457 read_object(const char *filename)
1458 {
1459 Elf *elf, *arf;
1460 Elf_Cmd elf_cmd;
1461 Elf_Kind kind;
1462 int fd, rtn, e_err;
1463
1464 assert(filename != NULL && "filename is null");
1465
1466 if ((fd = open(filename, O_RDONLY)) == -1) {
1467 warn("'%s'", filename);
1468 return (1);
1469 }
1470
1471 elf_cmd = ELF_C_READ;
1472 if ((arf = elf_begin(fd, elf_cmd, (Elf *) NULL)) == NULL) {
1473 if ((e_err = elf_errno()) != 0)
1474 warnx("elf_begin error: %s", elf_errmsg(e_err));
1475 else
1476 warnx("elf_begin error");
1477 close(fd);
1478 return (1);
1479 }
1480
1481 assert(arf != NULL && "arf is null.");
1482
1483 rtn = 0;
1484 if ((kind = elf_kind(arf)) == ELF_K_NONE) {
1485 warnx("%s: File format not recognized", filename);
1486 elf_end(arf);
1487 close(fd);
1488 return (1);
1489 }
1490 if (kind == ELF_K_AR) {
1491 if (nm_opts.print_name == PRINT_NAME_MULTI &&
1492 nm_opts.elem_print_fn == sym_elem_print_all)
1493 printf("\n%s:\n", filename);
1494 if (nm_opts.print_armap == true)
1495 print_ar_index(fd, arf);
1496 }
1497
1498 while ((elf = elf_begin(fd, elf_cmd, arf)) != NULL) {
1499 rtn |= read_elf(elf, filename, kind);
1500
1501 /*
1502 * If file is not archive, elf_next return ELF_C_NULL and
1503 * stop the loop.
1504 */
1505 elf_cmd = elf_next(elf);
1506 elf_end(elf);
1507 }
1508
1509 elf_end(arf);
1510 close(fd);
1511
1512 return (rtn);
1513 }
1514
1515 static int
read_files(int argc,char ** argv)1516 read_files(int argc, char **argv)
1517 {
1518 int rtn = 0;
1519
1520 if (argc < 0 || argv == NULL)
1521 return (1);
1522
1523 if (argc == 0)
1524 rtn |= read_object(nm_info.def_filename);
1525 else {
1526 if (nm_opts.print_name == PRINT_NAME_NONE && argc > 1)
1527 nm_opts.print_name = PRINT_NAME_MULTI;
1528 while (argc > 0) {
1529 rtn |= read_object(*argv);
1530 --argc;
1531 ++argv;
1532 }
1533 }
1534
1535 return (rtn);
1536 }
1537
1538 static void
print_lineno(struct sym_entry * ep,struct func_info_head * func_info,struct var_info_head * var_info,struct line_info_head * line_info)1539 print_lineno(struct sym_entry *ep, struct func_info_head *func_info,
1540 struct var_info_head *var_info, struct line_info_head *line_info)
1541 {
1542 struct func_info_entry *func;
1543 struct var_info_entry *var;
1544 struct line_info_entry *lie;
1545
1546 /* For function symbol, search the function line information list. */
1547 if ((ep->sym->st_info & 0xf) == STT_FUNC && func_info != NULL) {
1548 SLIST_FOREACH(func, func_info, entries) {
1549 if (func->name != NULL &&
1550 !strcmp(ep->name, func->name) &&
1551 ep->sym->st_value >= func->lowpc &&
1552 ep->sym->st_value < func->highpc) {
1553 printf("\t%s:%" PRIu64, func->file, func->line);
1554 return;
1555 }
1556 }
1557 }
1558
1559 /* For variable symbol, search the variable line information list. */
1560 if ((ep->sym->st_info & 0xf) == STT_OBJECT && var_info != NULL) {
1561 SLIST_FOREACH(var, var_info, entries) {
1562 if (!strcmp(ep->name, var->name) &&
1563 ep->sym->st_value == var->addr) {
1564 printf("\t%s:%" PRIu64, var->file, var->line);
1565 return;
1566 }
1567 }
1568 }
1569
1570 /* Otherwise search line number information the .debug_line section. */
1571 if (line_info != NULL) {
1572 SLIST_FOREACH(lie, line_info, entries) {
1573 if (ep->sym->st_value == lie->addr) {
1574 printf("\t%s:%" PRIu64, lie->file, lie->line);
1575 return;
1576 }
1577 }
1578 }
1579 }
1580
1581 static void
set_opt_value_print_fn(enum radix t)1582 set_opt_value_print_fn(enum radix t)
1583 {
1584
1585 switch (t) {
1586 case RADIX_OCT:
1587 nm_opts.value_print_fn = &sym_value_oct_print;
1588 nm_opts.size_print_fn = &sym_size_oct_print;
1589
1590 break;
1591 case RADIX_DEC:
1592 nm_opts.value_print_fn = &sym_value_dec_print;
1593 nm_opts.size_print_fn = &sym_size_dec_print;
1594
1595 break;
1596 case RADIX_HEX:
1597 default :
1598 nm_opts.value_print_fn = &sym_value_hex_print;
1599 nm_opts.size_print_fn = &sym_size_hex_print;
1600 }
1601
1602 assert(nm_opts.value_print_fn != NULL &&
1603 "nm_opts.value_print_fn is null");
1604 }
1605
1606 static void
sym_elem_print_all(char type,const char * sec,const GElf_Sym * sym,const char * name)1607 sym_elem_print_all(char type, const char *sec, const GElf_Sym *sym,
1608 const char *name)
1609 {
1610
1611 if (sec == NULL || sym == NULL || name == NULL ||
1612 nm_opts.value_print_fn == NULL)
1613 return;
1614
1615 if (IS_UNDEF_SYM_TYPE(type)) {
1616 if (nm_opts.t == RADIX_HEX && nm_elfclass == ELFCLASS32)
1617 printf("%-8s", "");
1618 else
1619 printf("%-16s", "");
1620 } else {
1621 switch ((nm_opts.sort_fn == & cmp_size ? 2 : 0) +
1622 nm_opts.print_size) {
1623 case 3:
1624 if (sym->st_size != 0) {
1625 nm_opts.value_print_fn(sym);
1626 printf(" ");
1627 nm_opts.size_print_fn(sym);
1628 }
1629 break;
1630
1631 case 2:
1632 if (sym->st_size != 0)
1633 nm_opts.size_print_fn(sym);
1634 break;
1635
1636 case 1:
1637 nm_opts.value_print_fn(sym);
1638 if (sym->st_size != 0) {
1639 printf(" ");
1640 nm_opts.size_print_fn(sym);
1641 }
1642 break;
1643
1644 case 0:
1645 default:
1646 nm_opts.value_print_fn(sym);
1647 }
1648 }
1649
1650 printf(" %c ", type);
1651 PRINT_DEMANGLED_NAME("%s", name);
1652 }
1653
1654 static void
sym_elem_print_all_portable(char type,const char * sec,const GElf_Sym * sym,const char * name)1655 sym_elem_print_all_portable(char type, const char *sec, const GElf_Sym *sym,
1656 const char *name)
1657 {
1658
1659 if (sec == NULL || sym == NULL || name == NULL ||
1660 nm_opts.value_print_fn == NULL)
1661 return;
1662
1663 PRINT_DEMANGLED_NAME("%s", name);
1664 printf(" %c ", type);
1665 if (!IS_UNDEF_SYM_TYPE(type)) {
1666 nm_opts.value_print_fn(sym);
1667 printf(" ");
1668 if (sym->st_size != 0)
1669 nm_opts.size_print_fn(sym);
1670 } else
1671 printf(" ");
1672 }
1673
1674 static void
sym_elem_print_all_sysv(char type,const char * sec,const GElf_Sym * sym,const char * name)1675 sym_elem_print_all_sysv(char type, const char *sec, const GElf_Sym *sym,
1676 const char *name)
1677 {
1678
1679 if (sec == NULL || sym == NULL || name == NULL ||
1680 nm_opts.value_print_fn == NULL)
1681 return;
1682
1683 PRINT_DEMANGLED_NAME("%-20s|", name);
1684 if (IS_UNDEF_SYM_TYPE(type))
1685 printf(" ");
1686 else
1687 nm_opts.value_print_fn(sym);
1688
1689 printf("| %c |", type);
1690
1691 switch (sym->st_info & 0xf) {
1692 case STT_OBJECT:
1693 printf("%18s|", "OBJECT");
1694 break;
1695
1696 case STT_FUNC:
1697 printf("%18s|", "FUNC");
1698 break;
1699
1700 case STT_SECTION:
1701 printf("%18s|", "SECTION");
1702 break;
1703
1704 case STT_FILE:
1705 printf("%18s|", "FILE");
1706 break;
1707
1708 case STT_LOPROC:
1709 printf("%18s|", "LOPROC");
1710 break;
1711
1712 case STT_HIPROC:
1713 printf("%18s|", "HIPROC");
1714 break;
1715
1716 case STT_NOTYPE:
1717 default:
1718 printf("%18s|", "NOTYPE");
1719 };
1720
1721 if (sym->st_size != 0)
1722 nm_opts.size_print_fn(sym);
1723 else
1724 printf(" ");
1725
1726 printf("| |%s", sec);
1727 }
1728
1729 static int
sym_elem_def(char type,const GElf_Sym * sym,const char * name)1730 sym_elem_def(char type, const GElf_Sym *sym, const char *name)
1731 {
1732
1733 assert(IS_SYM_TYPE((unsigned char) type));
1734
1735 UNUSED(sym);
1736 UNUSED(name);
1737
1738 return (!IS_UNDEF_SYM_TYPE((unsigned char) type));
1739 }
1740
1741 static int
sym_elem_global(char type,const GElf_Sym * sym,const char * name)1742 sym_elem_global(char type, const GElf_Sym *sym, const char *name)
1743 {
1744
1745 assert(IS_SYM_TYPE((unsigned char) type));
1746
1747 UNUSED(sym);
1748 UNUSED(name);
1749
1750 /* weak symbols resemble global. */
1751 return (isupper((unsigned char) type) || type == 'w');
1752 }
1753
1754 static int
sym_elem_global_static(char type,const GElf_Sym * sym,const char * name)1755 sym_elem_global_static(char type, const GElf_Sym *sym, const char *name)
1756 {
1757 unsigned char info;
1758
1759 assert(sym != NULL);
1760
1761 UNUSED(type);
1762 UNUSED(name);
1763
1764 info = sym->st_info >> 4;
1765
1766 return (info == STB_LOCAL ||
1767 info == STB_GLOBAL ||
1768 info == STB_WEAK);
1769 }
1770
1771 static int
sym_elem_nondebug(char type,const GElf_Sym * sym,const char * name)1772 sym_elem_nondebug(char type, const GElf_Sym *sym, const char *name)
1773 {
1774
1775 assert(sym != NULL);
1776
1777 UNUSED(type);
1778 UNUSED(name);
1779
1780 if (sym->st_value == 0 && (sym->st_info & 0xf) == STT_FILE)
1781 return (0);
1782 if (sym->st_name == 0)
1783 return (0);
1784
1785 return (1);
1786 }
1787
1788 static int
sym_elem_nonzero_size(char type,const GElf_Sym * sym,const char * name)1789 sym_elem_nonzero_size(char type, const GElf_Sym *sym, const char *name)
1790 {
1791
1792 assert(sym != NULL);
1793
1794 UNUSED(type);
1795 UNUSED(name);
1796
1797 return (sym->st_size > 0);
1798 }
1799
1800 static int
sym_elem_undef(char type,const GElf_Sym * sym,const char * name)1801 sym_elem_undef(char type, const GElf_Sym *sym, const char *name)
1802 {
1803
1804 assert(IS_SYM_TYPE((unsigned char) type));
1805
1806 UNUSED(sym);
1807 UNUSED(name);
1808
1809 return (IS_UNDEF_SYM_TYPE((unsigned char) type));
1810 }
1811
1812 static void
sym_list_dest(struct sym_head * headp)1813 sym_list_dest(struct sym_head *headp)
1814 {
1815 struct sym_entry *ep, *ep_n;
1816
1817 if (headp == NULL)
1818 return;
1819
1820 ep = STAILQ_FIRST(headp);
1821 while (ep != NULL) {
1822 ep_n = STAILQ_NEXT(ep, sym_entries);
1823 free(ep->sym);
1824 free(ep->name);
1825 free(ep);
1826 ep = ep_n;
1827 }
1828 }
1829
1830 static int
sym_list_insert(struct sym_head * headp,const char * name,const GElf_Sym * sym)1831 sym_list_insert(struct sym_head *headp, const char *name, const GElf_Sym *sym)
1832 {
1833 struct sym_entry *e;
1834
1835 if (headp == NULL || name == NULL || sym == NULL)
1836 return (0);
1837 if ((e = malloc(sizeof(struct sym_entry))) == NULL) {
1838 warn("malloc");
1839 return (0);
1840 }
1841 if ((e->name = strdup(name)) == NULL) {
1842 warn("strdup");
1843 free(e);
1844 return (0);
1845 }
1846 if ((e->sym = malloc(sizeof(GElf_Sym))) == NULL) {
1847 warn("malloc");
1848 free(e->name);
1849 free(e);
1850 return (0);
1851 }
1852
1853 memcpy(e->sym, sym, sizeof(GElf_Sym));
1854
1855 /* Display size instead of value for common symbol. */
1856 if (sym->st_shndx == SHN_COMMON)
1857 e->sym->st_value = sym->st_size;
1858
1859 STAILQ_INSERT_TAIL(headp, e, sym_entries);
1860
1861 return (1);
1862 }
1863
1864 /* If file has not .debug_info, line_info will be NULL */
1865 static void
sym_list_print(struct sym_print_data * p,struct func_info_head * func_info,struct var_info_head * var_info,struct line_info_head * line_info)1866 sym_list_print(struct sym_print_data *p, struct func_info_head *func_info,
1867 struct var_info_head *var_info, struct line_info_head *line_info)
1868 {
1869 struct sym_entry *e_v;
1870 size_t si;
1871 int i;
1872
1873 if (p == NULL || CHECK_SYM_PRINT_DATA(p))
1874 return;
1875 if ((e_v = sym_list_sort(p)) == NULL)
1876 return;
1877 if (nm_opts.sort_reverse == false)
1878 for (si = 0; si != p->list_num; ++si)
1879 sym_list_print_each(&e_v[si], p, func_info, var_info,
1880 line_info);
1881 else
1882 for (i = p->list_num - 1; i != -1; --i)
1883 sym_list_print_each(&e_v[i], p, func_info, var_info,
1884 line_info);
1885
1886 free(e_v);
1887 }
1888
1889 /* If file has not .debug_info, line_info will be NULL */
1890 static void
sym_list_print_each(struct sym_entry * ep,struct sym_print_data * p,struct func_info_head * func_info,struct var_info_head * var_info,struct line_info_head * line_info)1891 sym_list_print_each(struct sym_entry *ep, struct sym_print_data *p,
1892 struct func_info_head *func_info, struct var_info_head *var_info,
1893 struct line_info_head *line_info)
1894 {
1895 const char *sec;
1896 char type;
1897
1898 if (ep == NULL || CHECK_SYM_PRINT_DATA(p))
1899 return;
1900
1901 assert(ep->name != NULL);
1902 assert(ep->sym != NULL);
1903
1904 type = get_sym_type(ep->sym, p->t_table);
1905
1906 if (nm_opts.print_name == PRINT_NAME_FULL) {
1907 printf("%s", p->filename);
1908 if (nm_opts.elem_print_fn == &sym_elem_print_all_portable) {
1909 if (p->objname != NULL)
1910 printf("[%s]", p->objname);
1911 printf(": ");
1912 } else {
1913 if (p->objname != NULL)
1914 printf(":%s", p->objname);
1915 printf(":");
1916 }
1917 }
1918
1919 switch (ep->sym->st_shndx) {
1920 case SHN_LOPROC:
1921 /* LOPROC or LORESERVE */
1922 sec = "*LOPROC*";
1923 break;
1924 case SHN_HIPROC:
1925 sec = "*HIPROC*";
1926 break;
1927 case SHN_LOOS:
1928 sec = "*LOOS*";
1929 break;
1930 case SHN_HIOS:
1931 sec = "*HIOS*";
1932 break;
1933 case SHN_ABS:
1934 sec = "*ABS*";
1935 break;
1936 case SHN_COMMON:
1937 sec = "*COM*";
1938 break;
1939 case SHN_HIRESERVE:
1940 /* HIRESERVE or XINDEX */
1941 sec = "*HIRESERVE*";
1942 break;
1943 default:
1944 if (ep->sym->st_shndx > p->sh_num)
1945 return;
1946 sec = p->s_table[ep->sym->st_shndx];
1947 break;
1948 };
1949
1950 nm_opts.elem_print_fn(type, sec, ep->sym, ep->name);
1951
1952 if (nm_opts.debug_line == true && !IS_UNDEF_SYM_TYPE(type))
1953 print_lineno(ep, func_info, var_info, line_info);
1954
1955 printf("\n");
1956 }
1957
1958 static struct sym_entry *
sym_list_sort(struct sym_print_data * p)1959 sym_list_sort(struct sym_print_data *p)
1960 {
1961 struct sym_entry *ep, *e_v;
1962 int idx;
1963
1964 if (p == NULL || CHECK_SYM_PRINT_DATA(p))
1965 return (NULL);
1966
1967 if ((e_v = malloc(sizeof(struct sym_entry) * p->list_num)) == NULL) {
1968 warn("malloc");
1969 return (NULL);
1970 }
1971
1972 idx = 0;
1973 STAILQ_FOREACH(ep, p->headp, sym_entries) {
1974 if (ep->name != NULL && ep->sym != NULL) {
1975 e_v[idx].name = ep->name;
1976 e_v[idx].sym = ep->sym;
1977 ++idx;
1978 }
1979 }
1980
1981 assert((size_t)idx == p->list_num);
1982
1983 if (nm_opts.sort_fn != &cmp_none) {
1984 nm_print_data = p;
1985 assert(nm_print_data != NULL);
1986 qsort(e_v, p->list_num, sizeof(struct sym_entry),
1987 nm_opts.sort_fn);
1988 }
1989
1990 return (e_v);
1991 }
1992
1993 static void
sym_size_oct_print(const GElf_Sym * sym)1994 sym_size_oct_print(const GElf_Sym *sym)
1995 {
1996
1997 assert(sym != NULL && "sym is null");
1998 printf("%016" PRIo64, sym->st_size);
1999 }
2000
2001 static void
sym_size_hex_print(const GElf_Sym * sym)2002 sym_size_hex_print(const GElf_Sym *sym)
2003 {
2004
2005 assert(sym != NULL && "sym is null");
2006 if (nm_elfclass == ELFCLASS32)
2007 printf("%08" PRIx64, sym->st_size);
2008 else
2009 printf("%016" PRIx64, sym->st_size);
2010 }
2011
2012 static void
sym_size_dec_print(const GElf_Sym * sym)2013 sym_size_dec_print(const GElf_Sym *sym)
2014 {
2015
2016 assert(sym != NULL && "sym is null");
2017 printf("%016" PRId64, sym->st_size);
2018 }
2019
2020 static void
sym_value_oct_print(const GElf_Sym * sym)2021 sym_value_oct_print(const GElf_Sym *sym)
2022 {
2023
2024 assert(sym != NULL && "sym is null");
2025 printf("%016" PRIo64, sym->st_value);
2026 }
2027
2028 static void
sym_value_hex_print(const GElf_Sym * sym)2029 sym_value_hex_print(const GElf_Sym *sym)
2030 {
2031
2032 assert(sym != NULL && "sym is null");
2033 if (nm_elfclass == ELFCLASS32)
2034 printf("%08" PRIx64, sym->st_value);
2035 else
2036 printf("%016" PRIx64, sym->st_value);
2037 }
2038
2039 static void
sym_value_dec_print(const GElf_Sym * sym)2040 sym_value_dec_print(const GElf_Sym *sym)
2041 {
2042
2043 assert(sym != NULL && "sym is null");
2044 printf("%016" PRId64, sym->st_value);
2045 }
2046
2047 static void
usage(int exitcode)2048 usage(int exitcode)
2049 {
2050
2051 printf("Usage: %s [options] file ...\
2052 \n Display symbolic information in file.\n\
2053 \n Options: \
2054 \n -A, --print-file-name Write the full pathname or library name of an\
2055 \n object on each line.\
2056 \n -a, --debug-syms Display all symbols include debugger-only\
2057 \n symbols.", nm_info.name);
2058 printf("\
2059 \n -B Equivalent to specifying \"--format=bsd\".\
2060 \n -C, --demangle[=style] Decode low-level symbol names.\
2061 \n --no-demangle Do not demangle low-level symbol names.\
2062 \n -D, --dynamic Display only dynamic symbols.\
2063 \n -e Display only global and static symbols.");
2064 printf("\
2065 \n -f Produce full output (default).\
2066 \n --format=format Display output in specific format. Allowed\
2067 \n formats are: \"bsd\", \"posix\" and \"sysv\".\
2068 \n -g, --extern-only Display only global symbol information.\
2069 \n -h, --help Show this help message.\
2070 \n -l, --line-numbers Display filename and linenumber using\
2071 \n debugging information.\
2072 \n -n, --numeric-sort Sort symbols numerically by value.");
2073 printf("\
2074 \n -o Write numeric values in octal. Equivalent to\
2075 \n specifying \"-t o\".\
2076 \n -p, --no-sort Do not sort symbols.\
2077 \n -P Write information in a portable output format.\
2078 \n Equivalent to specifying \"--format=posix\".\
2079 \n -r, --reverse-sort Reverse the order of the sort.\
2080 \n -S, --print-size Print symbol sizes instead values.\
2081 \n -s, --print-armap Include an index of archive members.\
2082 \n --size-sort Sort symbols by size.");
2083 printf("\
2084 \n -t, --radix=format Write each numeric value in the specified\
2085 \n format:\
2086 \n d In decimal,\
2087 \n o In octal,\
2088 \n x In hexadecimal.");
2089 printf("\
2090 \n -u, --undefined-only Display only undefined symbols.\
2091 \n --defined-only Display only defined symbols.\
2092 \n -V, --version Show the version identifier for %s.\
2093 \n -v Sort output by value.\
2094 \n -x Write numeric values in hexadecimal.\
2095 \n Equivalent to specifying \"-t x\".",
2096 nm_info.name);
2097 printf("\n\
2098 \n The default options are: output in bsd format, use a hexadecimal radix,\
2099 \n sort by symbol name, do not demangle names.\n");
2100
2101 exit(exitcode);
2102 }
2103
2104 /*
2105 * Display symbolic information in file.
2106 * Return 0 at success, >0 at failed.
2107 */
2108 int
main(int argc,char ** argv)2109 main(int argc, char **argv)
2110 {
2111 int rtn;
2112
2113 global_init();
2114 get_opt(argc, argv);
2115 rtn = read_files(argc - optind, argv + optind);
2116 global_dest();
2117
2118 exit(rtn);
2119 }
2120