2 * Copyright (c) 2007 Hyogeol Lee <hyogeollee@gmail.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/queue.h>
28 #include <sys/types.h>
32 #include <capsicum_helpers.h>
50 #include <libcasper.h>
51 #include <casper/cap_fileargs.h>
55 ELFTC_VCSID("$Id: nm.c 3722 2019-03-23 17:01:58Z jkoshy $");
57 /* symbol information list */
58 STAILQ_HEAD(sym_head, sym_entry);
63 STAILQ_ENTRY(sym_entry) sym_entries;
66 typedef int (*fn_sort)(const void *, const void *);
67 typedef void (*fn_elem_print)(char, const char *, const GElf_Sym *, const char *);
68 typedef void (*fn_sym_print)(const GElf_Sym *);
69 typedef int (*fn_filter)(char, const GElf_Sym *, const char *);
71 /* output filter list */
72 static SLIST_HEAD(filter_head, filter_entry) nm_out_filter =
73 SLIST_HEAD_INITIALIZER(nm_out_filter);
77 SLIST_ENTRY(filter_entry) filter_entries;
80 struct sym_print_data {
81 struct sym_head *headp;
82 size_t sh_num, list_num;
83 const char *t_table, **s_table, *filename, *objname;
88 const char *def_filename;
91 /* List for line number information. */
92 struct line_info_entry {
93 uint64_t addr; /* address */
94 uint64_t line; /* line number */
95 char *file; /* file name with path */
96 SLIST_ENTRY(line_info_entry) entries;
98 SLIST_HEAD(line_info_head, line_info_entry);
100 /* List for function line number information. */
101 struct func_info_entry {
102 char *name; /* function name */
103 char *file; /* file name with path */
104 uint64_t lowpc; /* low address */
105 uint64_t highpc; /* high address */
106 uint64_t line; /* line number */
107 SLIST_ENTRY(func_info_entry) entries;
109 SLIST_HEAD(func_info_head, func_info_entry);
111 /* List for variable line number information. */
112 struct var_info_entry {
113 char *name; /* variable name */
114 char *file; /* file name with path */
115 uint64_t addr; /* address */
116 uint64_t line; /* line number */
117 SLIST_ENTRY(var_info_entry) entries;
119 SLIST_HEAD(var_info_head, var_info_entry);
121 /* output numric type */
128 /* output symbol type, PRINT_SYM_DYN for dynamic symbol only */
134 /* output name type */
141 struct nm_prog_options {
142 enum print_symbol print_symbol;
143 enum print_name print_name;
157 * function pointer to sort symbol list.
158 * possible function - cmp_name, cmp_none, cmp_size, cmp_value
163 * function pointer to print symbol elem.
164 * possible function - sym_elem_print_all
165 * sym_elem_print_all_portable
166 * sym_elem_print_all_sysv
168 fn_elem_print elem_print_fn;
170 fn_sym_print value_print_fn;
171 fn_sym_print size_print_fn;
173 fileargs_t *fileargs;
176 #define CHECK_SYM_PRINT_DATA(p) (p->headp == NULL || p->sh_num == 0 || \
177 p->t_table == NULL || p->s_table == NULL || p->filename == NULL)
178 #define IS_SYM_TYPE(t) ((t) == '?' || isalpha((t)) != 0)
179 #define IS_UNDEF_SYM_TYPE(t) ((t) == 'U' || (t) == 'v' || (t) == 'w')
180 #define UNUSED(p) ((void)p)
182 static int cmp_name(const void *, const void *);
183 static int cmp_none(const void *, const void *);
184 static int cmp_size(const void *, const void *);
185 static int cmp_value(const void *, const void *);
186 static void enter_cap_mode(int, char **);
187 static void filter_dest(void);
188 static int filter_insert(fn_filter);
189 static void get_opt(int *, char ***);
190 static int get_sym(Elf *, struct sym_head *, int, size_t, size_t,
191 const char *, const char **, int);
192 static const char * get_sym_name(Elf *, const GElf_Sym *, size_t,
194 static char get_sym_type(const GElf_Sym *, const char *);
195 static void global_dest(void);
196 static void global_init(void);
197 static bool is_sec_data(GElf_Shdr *);
198 static bool is_sec_debug(const char *);
199 static bool is_sec_nobits(GElf_Shdr *);
200 static bool is_sec_readonly(GElf_Shdr *);
201 static bool is_sec_text(GElf_Shdr *);
202 static void print_ar_index(int, Elf *);
203 static void print_header(const char *, const char *);
204 static void print_version(void);
205 static int read_elf(Elf *, const char *, Elf_Kind);
206 static int read_object(const char *);
207 static int read_files(int, char **);
208 static void set_opt_value_print_fn(enum radix);
209 static int sym_elem_def(char, const GElf_Sym *, const char *);
210 static int sym_elem_global(char, const GElf_Sym *, const char *);
211 static int sym_elem_global_static(char, const GElf_Sym *,
213 static int sym_elem_nondebug(char, const GElf_Sym *, const char *);
214 static int sym_elem_nonzero_size(char, const GElf_Sym *,
216 static void sym_elem_print_all(char, const char *,
217 const GElf_Sym *, const char *);
218 static void sym_elem_print_all_portable(char, const char *,
219 const GElf_Sym *, const char *);
220 static void sym_elem_print_all_sysv(char, const char *,
221 const GElf_Sym *, const char *);
222 static int sym_elem_undef(char, const GElf_Sym *, const char *);
223 static void sym_list_dest(struct sym_head *);
224 static int sym_list_insert(struct sym_head *, const char *,
226 static void sym_list_print(struct sym_print_data *,
227 struct func_info_head *, struct var_info_head *,
228 struct line_info_head *);
229 static void sym_list_print_each(struct sym_entry *,
230 struct sym_print_data *, struct func_info_head *,
231 struct var_info_head *, struct line_info_head *);
232 static struct sym_entry *sym_list_sort(struct sym_print_data *);
233 static void sym_size_oct_print(const GElf_Sym *);
234 static void sym_size_hex_print(const GElf_Sym *);
235 static void sym_size_dec_print(const GElf_Sym *);
236 static void sym_value_oct_print(const GElf_Sym *);
237 static void sym_value_hex_print(const GElf_Sym *);
238 static void sym_value_dec_print(const GElf_Sym *);
239 static void usage(int);
241 static struct nm_prog_info nm_info;
242 static struct nm_prog_options nm_opts;
243 static int nm_elfclass;
246 * Point to current sym_print_data to use portable qsort function.
247 * (e.g. There is no qsort_r function in NetBSD.)
249 * Using in sym_list_sort.
251 static struct sym_print_data *nm_print_data;
253 static const struct option nm_longopts[] = {
254 { "debug-syms", no_argument, NULL, 'a' },
255 { "defined-only", no_argument, &nm_opts.def_only, 1},
256 { "demangle", optional_argument, NULL, 'C' },
257 { "dynamic", no_argument, NULL, 'D' },
258 { "extern-only", no_argument, NULL, 'g' },
259 { "format", required_argument, NULL, 'F' },
260 { "help", no_argument, NULL, 'h' },
261 { "line-numbers", no_argument, NULL, 'l' },
262 { "no-demangle", no_argument, &nm_opts.no_demangle,
264 { "no-sort", no_argument, NULL, 'p' },
265 { "numeric-sort", no_argument, NULL, 'v' },
266 { "print-armap", no_argument, NULL, 's' },
267 { "print-file-name", no_argument, NULL, 'A' },
268 { "print-size", no_argument, NULL, 'S' },
269 { "radix", required_argument, NULL, 't' },
270 { "reverse-sort", no_argument, NULL, 'r' },
271 { "size-sort", no_argument, &nm_opts.sort_size, 1},
272 { "undefined-only", no_argument, NULL, 'u' },
273 { "version", no_argument, NULL, 'V' },
277 #if defined(ELFTC_NEED_BYTEORDER_EXTENSIONS)
278 static __inline uint32_t
279 be32dec(const void *pp)
281 unsigned char const *p = (unsigned char const *)pp;
283 return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
286 static __inline uint32_t
287 le32dec(const void *pp)
289 unsigned char const *p = (unsigned char const *)pp;
291 return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
294 static __inline uint64_t
295 be64dec(const void *pp)
297 unsigned char const *p = (unsigned char const *)pp;
299 return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
302 static __inline uint64_t
303 le64dec(const void *pp)
305 unsigned char const *p = (unsigned char const *)pp;
307 return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
312 cmp_name(const void *l, const void *r)
317 assert(((const struct sym_entry *)l)->name != NULL);
318 assert(((const struct sym_entry *)r)->name != NULL);
320 return (strcmp(((const struct sym_entry *)l)->name,
321 ((const struct sym_entry *)r)->name));
325 cmp_none(const void *l, const void *r)
334 /* Size comparison. If l and r have same size, compare their name. */
336 cmp_size(const void *lp, const void *rp)
338 const struct sym_entry *l, *r;
344 assert(l->name != NULL);
345 assert(l->sym != NULL);
347 assert(r->name != NULL);
348 assert(r->sym != NULL);
350 if (l->sym->st_size == r->sym->st_size)
351 return (strcmp(l->name, r->name));
353 return (l->sym->st_size - r->sym->st_size);
356 /* Value comparison. Undefined symbols come first. */
358 cmp_value(const void *lp, const void *rp)
360 const struct sym_entry *l, *r;
362 int l_is_undef, r_is_undef;
367 assert(nm_print_data != NULL);
368 ttable = nm_print_data->t_table;
371 assert(l->name != NULL);
372 assert(l->sym != NULL);
374 assert(r->name != NULL);
375 assert(r->sym != NULL);
376 assert(ttable != NULL);
378 l_is_undef = IS_UNDEF_SYM_TYPE(get_sym_type(l->sym, ttable)) ? 1 : 0;
379 r_is_undef = IS_UNDEF_SYM_TYPE(get_sym_type(r->sym, ttable)) ? 1 : 0;
381 assert(l_is_undef + r_is_undef >= 0);
382 assert(l_is_undef + r_is_undef <= 2);
384 switch (l_is_undef + r_is_undef) {
387 if (l->sym->st_value == r->sym->st_value)
388 return (strcmp(l->name, r->name));
389 return (l->sym->st_value > r->sym->st_value ? 1 : -1);
392 return (l_is_undef == 0 ? 1 : -1);
395 return (strcmp(l->name, r->name));
399 return (l->sym->st_value - r->sym->st_value);
403 enter_cap_mode(int argc, char **argv)
409 cap_rights_init(&rights, CAP_FSTAT, CAP_MMAP_R);
412 defaultfn = strdup(nm_info.def_filename);
413 if (defaultfn == NULL)
414 err(EXIT_FAILURE, "strdup");
419 fa = fileargs_init(argc, argv, O_RDONLY, 0, &rights, FA_OPEN);
421 err(EXIT_FAILURE, "failed to initialize fileargs");
423 caph_cache_catpages();
424 if (caph_limit_stdio() < 0)
425 err(EXIT_FAILURE, "failed to limit stdio rights");
426 if (caph_enter_casper() < 0)
427 err(EXIT_FAILURE, "failed to enter capability mode");
429 nm_opts.fileargs = fa;
435 struct filter_entry *e;
437 while (!SLIST_EMPTY(&nm_out_filter)) {
438 e = SLIST_FIRST(&nm_out_filter);
439 SLIST_REMOVE_HEAD(&nm_out_filter, filter_entries);
445 filter_insert(fn_filter filter_fn)
447 struct filter_entry *e;
449 assert(filter_fn != NULL);
451 if ((e = malloc(sizeof(struct filter_entry))) == NULL) {
456 SLIST_INSERT_HEAD(&nm_out_filter, e, filter_entries);
462 parse_demangle_option(const char *opt)
466 return (ELFTC_DEM_UNKNOWN);
467 else if (!strncasecmp(opt, "gnu-v2", 6))
468 return (ELFTC_DEM_GNU2);
469 else if (!strncasecmp(opt, "gnu-v3", 6))
470 return (ELFTC_DEM_GNU3);
471 else if (!strncasecmp(opt, "arm", 3))
472 return (ELFTC_DEM_ARM);
474 errx(EXIT_FAILURE, "unknown demangling style '%s'", opt);
481 get_opt(int *argc, char ***argv)
484 bool is_posix, oflag;
486 if (*argc <= 0 || *argv == NULL)
489 oflag = is_posix = false;
490 nm_opts.t = RADIX_HEX;
491 while ((ch = getopt_long(*argc, *argv, "ABCDF:PSVaefghlnoprst:uvx",
492 nm_longopts, NULL)) != -1) {
495 nm_opts.print_name = PRINT_NAME_FULL;
498 nm_opts.elem_print_fn = &sym_elem_print_all;
501 nm_opts.demangle_type = parse_demangle_option(optarg);
504 nm_opts.print_symbol = PRINT_SYM_DYN;
507 /* sysv, bsd, posix */
511 nm_opts.elem_print_fn = &sym_elem_print_all;
516 nm_opts.elem_print_fn =
517 &sym_elem_print_all_portable;
521 nm_opts.elem_print_fn =
522 &sym_elem_print_all_sysv;
525 warnx("%s: Invalid format", optarg);
532 nm_opts.elem_print_fn = &sym_elem_print_all_portable;
535 nm_opts.print_size = 1;
541 nm_opts.print_debug = true;
544 filter_insert(sym_elem_global_static);
549 filter_insert(sym_elem_global);
555 nm_opts.debug_line = true;
559 nm_opts.sort_fn = &cmp_value;
565 nm_opts.sort_fn = &cmp_none;
568 nm_opts.sort_reverse = true;
571 nm_opts.print_armap = true;
574 /* t require always argument to getopt_long */
577 nm_opts.t = RADIX_DEC;
580 nm_opts.t = RADIX_OCT;
583 nm_opts.t = RADIX_HEX;
586 warnx("%s: Invalid radix", optarg);
591 filter_insert(sym_elem_undef);
592 nm_opts.undef_only = true;
594 /* case 'v': see case 'n' above. */
596 nm_opts.t = RADIX_HEX;
599 if (nm_opts.sort_size != 0) {
600 nm_opts.sort_fn = &cmp_size;
601 filter_insert(sym_elem_def);
602 filter_insert(sym_elem_nonzero_size);
604 if (nm_opts.def_only != 0)
605 filter_insert(sym_elem_def);
606 if (nm_opts.no_demangle != 0)
607 nm_opts.demangle_type = -1;
617 * In POSIX mode, the '-o' option controls the output radix.
618 * In non-POSIX mode, the option is a synonym for the '-A' and
619 * '--print-file-name' options.
623 nm_opts.t = RADIX_OCT;
625 nm_opts.print_name = PRINT_NAME_FULL;
628 assert(nm_opts.sort_fn != NULL && "nm_opts.sort_fn is null");
629 assert(nm_opts.elem_print_fn != NULL &&
630 "nm_opts.elem_print_fn is null");
631 assert(nm_opts.value_print_fn != NULL &&
632 "nm_opts.value_print_fn is null");
634 set_opt_value_print_fn(nm_opts.t);
636 if (nm_opts.undef_only == true) {
637 if (nm_opts.sort_fn == &cmp_size)
639 "--size-sort with -u is meaningless");
640 if (nm_opts.def_only != 0)
642 "-u with --defined-only is meaningless");
644 if (nm_opts.print_debug == false)
645 filter_insert(sym_elem_nondebug);
646 if (nm_opts.sort_reverse == true && nm_opts.sort_fn == cmp_none)
647 nm_opts.sort_reverse = false;
651 * Get symbol information from elf.
654 get_sym(Elf *elf, struct sym_head *headp, int shnum, size_t dynndx,
655 size_t strndx, const char *type_table, const char **sec_table,
662 struct filter_entry *fep;
665 const char *sym_name;
671 assert(headp != NULL);
674 for (i = 1; i < shnum; i++) {
675 if ((scn = elf_getscn(elf, i)) == NULL) {
676 warnx("elf_getscn failed: %s", elf_errmsg(-1));
679 if (gelf_getshdr(scn, &shdr) != &shdr) {
680 warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
683 if (shdr.sh_type == SHT_SYMTAB) {
684 if (nm_opts.print_symbol != PRINT_SYM_SYM)
686 } else if (shdr.sh_type == SHT_DYNSYM) {
687 if (nm_opts.print_symbol != PRINT_SYM_DYN)
692 ndx = shdr.sh_type == SHT_DYNSYM ? dynndx : strndx;
695 while ((data = elf_getdata(scn, data)) != NULL) {
697 while (gelf_getsym(data, j++, &sym) != NULL) {
698 sym_name = get_sym_name(elf, &sym, ndx,
699 sec_table, sec_table_size);
701 type = get_sym_type(&sym, type_table);
702 SLIST_FOREACH(fep, &nm_out_filter,
704 if (!fep->fn(type, &sym, sym_name)) {
709 if (filter == false) {
710 if (sym_list_insert(headp, sym_name,
723 get_sym_name(Elf *elf, const GElf_Sym *sym, size_t ndx, const char **sec_table,
726 const char *sym_name;
730 /* Show section name as symbol name for STT_SECTION symbols. */
731 if (GELF_ST_TYPE(sym->st_info) == STT_SECTION) {
732 if (sec_table != NULL && sym->st_shndx < sec_table_size)
733 sym_name = sec_table[sym->st_shndx];
735 sym_name = elf_strptr(elf, ndx, sym->st_name);
737 if (sym_name == NULL)
744 get_sym_type(const GElf_Sym *sym, const char *type_table)
748 if (sym == NULL || type_table == NULL)
751 is_local = sym->st_info >> 4 == STB_LOCAL;
753 if (sym->st_shndx == SHN_ABS) /* absolute */
754 return (is_local ? 'a' : 'A');
756 if (sym->st_shndx == SHN_COMMON) /* common */
759 if ((sym->st_info) >> 4 == STB_WEAK) { /* weak */
760 if ((sym->st_info & 0xf) == STT_OBJECT)
761 return (sym->st_shndx == SHN_UNDEF ? 'v' : 'V');
763 return (sym->st_shndx == SHN_UNDEF ? 'w' : 'W');
766 if (sym->st_shndx == SHN_UNDEF) /* undefined */
769 return (is_local == true && type_table[sym->st_shndx] != 'N' ?
770 tolower((unsigned char) type_table[sym->st_shndx]) :
771 type_table[sym->st_shndx]);
785 if (elf_version(EV_CURRENT) == EV_NONE)
786 errx(EXIT_FAILURE, "elf_version error");
788 nm_info.name = ELFTC_GETPROGNAME();
789 nm_info.def_filename = "a.out";
790 nm_opts.print_symbol = PRINT_SYM_SYM;
791 nm_opts.print_name = PRINT_NAME_NONE;
792 nm_opts.demangle_type = -1;
793 nm_opts.print_debug = false;
794 nm_opts.print_armap = false;
795 nm_opts.print_size = 0;
796 nm_opts.debug_line = false;
797 nm_opts.def_only = 0;
798 nm_opts.undef_only = false;
799 nm_opts.sort_size = 0;
800 nm_opts.sort_reverse = false;
801 nm_opts.no_demangle = 0;
802 nm_opts.sort_fn = &cmp_name;
803 nm_opts.elem_print_fn = &sym_elem_print_all;
804 nm_opts.value_print_fn = &sym_value_dec_print;
805 nm_opts.size_print_fn = &sym_size_dec_print;
806 nm_opts.fileargs = NULL;
807 SLIST_INIT(&nm_out_filter);
811 is_sec_data(GElf_Shdr *s)
814 assert(s != NULL && "shdr is NULL");
816 return (((s->sh_flags & SHF_ALLOC) != 0) && s->sh_type != SHT_NOBITS);
820 is_sec_debug(const char *shname)
822 const char *dbg_sec[] = {
836 for (p = dbg_sec; *p; p++) {
837 if (!strncmp(shname, *p, strlen(*p)))
845 is_sec_nobits(GElf_Shdr *s)
848 assert(s != NULL && "shdr is NULL");
850 return (s->sh_type == SHT_NOBITS);
854 is_sec_readonly(GElf_Shdr *s)
857 assert(s != NULL && "shdr is NULL");
859 return ((s->sh_flags & SHF_WRITE) == 0);
863 is_sec_text(GElf_Shdr *s)
866 assert(s != NULL && "shdr is NULL");
868 return ((s->sh_flags & SHF_EXECINSTR) != 0);
872 print_ar_index(int fd, Elf *arf)
884 if ((arsym = elf_getarsym(arf, &arsym_size)) == NULL)
887 printf("\nArchive index:\n");
889 start = arsym->as_off;
891 while (arsym_size > 1) {
892 if (elf_rand(arf, arsym->as_off) == arsym->as_off &&
893 (elf = elf_begin(fd, cmd, arf)) != NULL) {
894 if ((arhdr = elf_getarhdr(elf)) != NULL)
895 printf("%s in %s\n", arsym->as_name,
896 arhdr->ar_name != NULL ?
897 arhdr->ar_name : arhdr->ar_rawname);
904 elf_rand(arf, start);
907 #define DEMANGLED_BUFFER_SIZE (8 * 1024)
908 #define PRINT_DEMANGLED_NAME(FORMAT, NAME) do { \
909 char _demangled[DEMANGLED_BUFFER_SIZE]; \
910 if (nm_opts.demangle_type < 0 || \
911 elftc_demangle((NAME), _demangled, sizeof(_demangled), \
912 nm_opts.demangle_type) < 0) \
913 printf((FORMAT), (NAME)); \
915 printf((FORMAT), _demangled); \
919 print_header(const char *file, const char *obj)
925 if (nm_opts.elem_print_fn == &sym_elem_print_all_sysv) {
926 printf("\n\n%s from %s",
927 nm_opts.undef_only == false ? "Symbols" :
928 "Undefined symbols", file);
934 Name Value Class Type Size Line Section\n\n");
936 /* archive file without -A option and POSIX */
937 if (nm_opts.print_name != PRINT_NAME_FULL && obj != NULL) {
938 if (nm_opts.elem_print_fn ==
939 sym_elem_print_all_portable)
940 printf("%s[%s]:\n", file, obj);
941 else if (nm_opts.elem_print_fn == sym_elem_print_all)
942 printf("\n%s:\n", obj);
943 /* multiple files(not archive) without -A option */
944 } else if (nm_opts.print_name == PRINT_NAME_MULTI) {
945 if (nm_opts.elem_print_fn == sym_elem_print_all)
947 printf("%s:\n", file);
956 (void) printf("%s (%s)\n", nm_info.name, elftc_version());
961 get_block_value(Dwarf_Debug dbg, Dwarf_Block *block)
967 if (dwarf_get_elf(dbg, &elf, &de) != DW_DLV_OK) {
968 warnx("dwarf_get_elf failed: %s", dwarf_errmsg(de));
972 if (gelf_getehdr(elf, &eh) != &eh) {
973 warnx("gelf_getehdr failed: %s", elf_errmsg(-1));
977 if (block->bl_len == 5) {
978 if (eh.e_ident[EI_DATA] == ELFDATA2LSB)
979 return (le32dec((uint8_t *) block->bl_data + 1));
981 return (be32dec((uint8_t *) block->bl_data + 1));
982 } else if (block->bl_len == 9) {
983 if (eh.e_ident[EI_DATA] == ELFDATA2LSB)
984 return (le64dec((uint8_t *) block->bl_data + 1));
986 return (be64dec((uint8_t *) block->bl_data + 1));
993 find_object_name(Dwarf_Debug dbg, Dwarf_Die die)
1002 if (dwarf_attrval_string(die, DW_AT_name, &str, &de) == DW_DLV_OK) {
1003 if ((name = strdup(str)) == NULL) {
1010 if (dwarf_attr(die, DW_AT_specification, &at, &de) != DW_DLV_OK)
1013 if (dwarf_global_formref(at, &off, &de) != DW_DLV_OK)
1016 if (dwarf_offdie(dbg, off, &ret_die, &de) != DW_DLV_OK)
1019 return (find_object_name(dbg, ret_die));
1023 search_line_attr(Dwarf_Debug dbg, struct func_info_head *func_info,
1024 struct var_info_head *var_info, Dwarf_Die die, char **src_files,
1025 Dwarf_Signed filecount)
1028 Dwarf_Unsigned udata;
1034 struct func_info_entry *func;
1035 struct var_info_entry *var;
1038 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
1039 warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
1043 /* We're interested in DIEs which define functions or variables. */
1044 if (tag != DW_TAG_subprogram && tag != DW_TAG_entry_point &&
1045 tag != DW_TAG_inlined_subroutine && tag != DW_TAG_variable)
1048 if (tag == DW_TAG_variable) {
1050 /* Ignore "artificial" variable. */
1051 if (dwarf_attrval_flag(die, DW_AT_artificial, &flag, &de) ==
1055 /* Ignore pure declaration. */
1056 if (dwarf_attrval_flag(die, DW_AT_declaration, &flag, &de) ==
1060 /* Ignore stack varaibles. */
1061 if (dwarf_attrval_flag(die, DW_AT_external, &flag, &de) !=
1065 if ((var = calloc(1, sizeof(*var))) == NULL) {
1066 warn("calloc failed");
1070 if (dwarf_attrval_unsigned(die, DW_AT_decl_file, &udata,
1071 &de) == DW_DLV_OK && udata > 0 &&
1072 (Dwarf_Signed) (udata - 1) < filecount) {
1073 var->file = strdup(src_files[udata - 1]);
1074 if (var->file == NULL) {
1081 if (dwarf_attrval_unsigned(die, DW_AT_decl_line, &udata, &de) ==
1085 var->name = find_object_name(dbg, die);
1086 if (var->name == NULL) {
1093 if (dwarf_attr(die, DW_AT_location, &at, &de) == DW_DLV_OK &&
1094 dwarf_formblock(at, &block, &de) == DW_DLV_OK) {
1096 * Since we ignored stack variables, the rest are the
1097 * external varaibles which should always use DW_OP_addr
1098 * operator for DW_AT_location value.
1100 if (*((uint8_t *)block->bl_data) == DW_OP_addr)
1101 var->addr = get_block_value(dbg, block);
1104 SLIST_INSERT_HEAD(var_info, var, entries);
1108 if ((func = calloc(1, sizeof(*func))) == NULL) {
1109 warn("calloc failed");
1114 * Note that dwarf_attrval_unsigned() handles DW_AT_abstract_origin
1115 * internally, so it can retrieve DW_AT_decl_file/DW_AT_decl_line
1116 * attributes for inlined functions as well.
1118 if (dwarf_attrval_unsigned(die, DW_AT_decl_file, &udata,
1119 &de) == DW_DLV_OK && udata > 0 &&
1120 (Dwarf_Signed) (udata - 1) < filecount) {
1121 func->file = strdup(src_files[udata - 1]);
1122 if (func->file == NULL) {
1129 if (dwarf_attrval_unsigned(die, DW_AT_decl_line, &udata, &de) ==
1133 func->name = find_object_name(dbg, die);
1134 if (func->name == NULL) {
1141 if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &udata, &de) ==
1143 func->lowpc = udata;
1144 if (dwarf_attrval_unsigned(die, DW_AT_high_pc, &udata, &de) ==
1146 func->highpc = udata;
1148 SLIST_INSERT_HEAD(func_info, func, entries);
1153 /* Search children. */
1154 ret = dwarf_child(die, &ret_die, &de);
1155 if (ret == DW_DLV_ERROR)
1156 warnx("dwarf_child: %s", dwarf_errmsg(de));
1157 else if (ret == DW_DLV_OK)
1158 search_line_attr(dbg, func_info, var_info, ret_die, src_files,
1161 /* Search sibling. */
1162 ret = dwarf_siblingof(dbg, die, &ret_die, &de);
1163 if (ret == DW_DLV_ERROR)
1164 warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
1165 else if (ret == DW_DLV_OK)
1166 search_line_attr(dbg, func_info, var_info, ret_die, src_files,
1169 dwarf_dealloc(dbg, die, DW_DLA_DIE);
1173 * Read elf file and collect symbol information, sort them, print.
1174 * Return 1 at failed, 0 at success.
1177 read_elf(Elf *elf, const char *filename, Elf_Kind kind)
1187 Dwarf_Unsigned lineno;
1188 Dwarf_Signed lcount, filecount;
1189 Dwarf_Addr lineaddr;
1190 struct sym_print_data p_data;
1191 struct sym_head list_head;
1192 struct line_info_head *line_info;
1193 struct func_info_head *func_info;
1194 struct var_info_head *var_info;
1195 struct line_info_entry *lie;
1196 struct func_info_entry *func;
1197 struct var_info_entry *var;
1198 const char *shname, *objname;
1199 char *type_table, **sec_table, *sfile, **src_files;
1200 size_t i, shstrndx, shnum, dynndx, strndx;
1201 int ret, rtn, e_err;
1203 #define OBJNAME (objname == NULL ? filename : objname)
1205 assert(filename != NULL && "filename is null");
1207 STAILQ_INIT(&list_head);
1218 nm_elfclass = gelf_getclass(elf);
1220 if (kind == ELF_K_AR) {
1221 if ((arhdr = elf_getarhdr(elf)) == NULL)
1223 objname = arhdr->ar_name != NULL ? arhdr->ar_name :
1226 if (!elf_getshnum(elf, &shnum)) {
1227 if ((e_err = elf_errno()) != 0)
1228 warnx("%s: %s", OBJNAME, "File format not recognized");
1230 warnx("%s: cannot get section number", OBJNAME);
1235 warnx("%s: has no section", OBJNAME);
1239 if (!elf_getshstrndx(elf, &shstrndx)) {
1240 warnx("%s: cannot get str index", OBJNAME);
1244 /* type_table for type determine */
1245 if ((type_table = malloc(sizeof(char) * shnum)) == NULL) {
1246 warn("%s: malloc", OBJNAME);
1250 /* sec_table for section name to display in sysv format */
1251 if ((sec_table = calloc(shnum, sizeof(char *))) == NULL) {
1252 warn("%s: calloc", OBJNAME);
1257 type_table[0] = 'U';
1258 if ((sec_table[0] = strdup("*UND*")) == NULL) {
1263 for (i = 1; i < shnum; ++i) {
1264 type_table[i] = 'U';
1265 if ((scn = elf_getscn(elf, i)) == NULL) {
1266 if ((e_err = elf_errno()) != 0)
1267 warnx("%s: %s", OBJNAME, elf_errmsg(e_err));
1269 warnx("%s: cannot get section", OBJNAME);
1273 if (gelf_getshdr(scn, &shdr) == NULL)
1277 * Cannot test by type and attribute for dynstr, strtab
1279 shname = elf_strptr(elf, shstrndx, (size_t) shdr.sh_name);
1280 if (shname != NULL) {
1281 if ((sec_table[i] = strdup(shname)) == NULL) {
1285 if (!strncmp(shname, ".dynstr", 7)) {
1286 dynndx = elf_ndxscn(scn);
1287 if (dynndx == SHN_UNDEF) {
1288 warnx("%s: elf_ndxscn failed: %s",
1289 OBJNAME, elf_errmsg(-1));
1293 if (!strncmp(shname, ".strtab", 7)) {
1294 strndx = elf_ndxscn(scn);
1295 if (strndx == SHN_UNDEF) {
1296 warnx("%s: elf_ndxscn failed: %s",
1297 OBJNAME, elf_errmsg(-1));
1302 sec_table[i] = strdup("*UND*");
1303 if (sec_table[i] == NULL) {
1310 if (is_sec_text(&shdr))
1311 type_table[i] = 'T';
1312 else if (is_sec_data(&shdr)) {
1313 if (is_sec_readonly(&shdr))
1314 type_table[i] = 'R';
1316 type_table[i] = 'D';
1317 } else if (is_sec_nobits(&shdr))
1318 type_table[i] = 'B';
1319 else if (is_sec_debug(shname))
1320 type_table[i] = 'N';
1321 else if (is_sec_readonly(&shdr) && !is_sec_nobits(&shdr))
1322 type_table[i] = 'n';
1325 print_header(filename, objname);
1327 if ((dynndx == SHN_UNDEF && nm_opts.print_symbol == PRINT_SYM_DYN) ||
1328 (strndx == SHN_UNDEF && nm_opts.print_symbol == PRINT_SYM_SYM)) {
1329 warnx("%s: no symbols", OBJNAME);
1330 /* This is not an error case */
1334 STAILQ_INIT(&list_head);
1336 if (!nm_opts.debug_line)
1340 * Collect dwarf line number information.
1343 if (dwarf_elf_init(elf, DW_DLC_READ, NULL, NULL, &dbg, &de) !=
1345 warnx("dwarf_elf_init failed: %s", dwarf_errmsg(de));
1349 line_info = malloc(sizeof(struct line_info_head));
1350 func_info = malloc(sizeof(struct func_info_head));
1351 var_info = malloc(sizeof(struct var_info_head));
1352 if (line_info != NULL)
1353 SLIST_INIT(line_info);
1354 if (func_info != NULL)
1355 SLIST_INIT(func_info);
1356 if (var_info != NULL)
1357 SLIST_INIT(var_info);
1358 if (line_info == NULL || func_info == NULL || var_info == NULL) {
1360 (void) dwarf_finish(dbg, &de);
1364 while ((ret = dwarf_next_cu_header(dbg, NULL, NULL, NULL, NULL, NULL,
1365 &de)) == DW_DLV_OK) {
1367 while (dwarf_siblingof(dbg, die, &die, &de) == DW_DLV_OK) {
1368 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
1369 warnx("dwarf_tag failed: %s",
1373 /* XXX: What about DW_TAG_partial_unit? */
1374 if (tag == DW_TAG_compile_unit)
1378 warnx("could not find DW_TAG_compile_unit die");
1382 /* Retrieve source file list. */
1383 ret = dwarf_srcfiles(die, &src_files, &filecount, &de);
1384 if (ret == DW_DLV_ERROR)
1385 warnx("dwarf_srclines: %s", dwarf_errmsg(de));
1386 if (ret != DW_DLV_OK)
1390 * Retrieve line number information from .debug_line section.
1393 ret = dwarf_srclines(die, &lbuf, &lcount, &de);
1394 if (ret == DW_DLV_ERROR)
1395 warnx("dwarf_srclines: %s", dwarf_errmsg(de));
1396 if (ret != DW_DLV_OK)
1398 for (i = 0; (Dwarf_Signed) i < lcount; i++) {
1399 if (dwarf_lineaddr(lbuf[i], &lineaddr, &de)) {
1400 warnx("dwarf_lineaddr: %s", dwarf_errmsg(de));
1403 if (dwarf_lineno(lbuf[i], &lineno, &de)) {
1404 warnx("dwarf_lineno: %s", dwarf_errmsg(de));
1407 if (dwarf_linesrc(lbuf[i], &sfile, &de)) {
1408 warnx("dwarf_linesrc: %s", dwarf_errmsg(de));
1411 if ((lie = malloc(sizeof(*lie))) == NULL) {
1415 lie->addr = lineaddr;
1417 lie->file = strdup(sfile);
1418 if (lie->file == NULL) {
1423 SLIST_INSERT_HEAD(line_info, lie, entries);
1427 /* Retrieve line number information from DIEs. */
1428 search_line_attr(dbg, func_info, var_info, die, src_files, filecount);
1431 (void) dwarf_finish(dbg, &de);
1435 p_data.list_num = get_sym(elf, &list_head, shnum, dynndx, strndx,
1436 type_table, (void *) sec_table, shnum);
1438 if (p_data.list_num == 0)
1441 p_data.headp = &list_head;
1442 p_data.sh_num = shnum;
1443 p_data.t_table = type_table;
1444 p_data.s_table = (void *) sec_table;
1445 p_data.filename = filename;
1446 p_data.objname = objname;
1448 sym_list_print(&p_data, func_info, var_info, line_info);
1451 if (nm_opts.debug_line) {
1452 if (func_info != NULL) {
1453 while (!SLIST_EMPTY(func_info)) {
1454 func = SLIST_FIRST(func_info);
1455 SLIST_REMOVE_HEAD(func_info, entries);
1463 if (var_info != NULL) {
1464 while (!SLIST_EMPTY(var_info)) {
1465 var = SLIST_FIRST(var_info);
1466 SLIST_REMOVE_HEAD(var_info, entries);
1474 if (line_info != NULL) {
1475 while (!SLIST_EMPTY(line_info)) {
1476 lie = SLIST_FIRST(line_info);
1477 SLIST_REMOVE_HEAD(line_info, entries);
1486 if (sec_table != NULL)
1487 for (i = 0; i < shnum; ++i)
1492 sym_list_dest(&list_head);
1500 read_object(const char *filename)
1507 assert(filename != NULL && "filename is null");
1509 if ((fd = fileargs_open(nm_opts.fileargs, filename)) == -1) {
1510 warn("'%s'", filename);
1514 elf_cmd = ELF_C_READ;
1515 if ((arf = elf_begin(fd, elf_cmd, (Elf *) NULL)) == NULL) {
1516 if ((e_err = elf_errno()) != 0)
1517 warnx("elf_begin error: %s", elf_errmsg(e_err));
1519 warnx("elf_begin error");
1524 assert(arf != NULL && "arf is null.");
1527 if ((kind = elf_kind(arf)) == ELF_K_NONE) {
1528 warnx("%s: File format not recognized", filename);
1533 if (kind == ELF_K_AR) {
1534 if (nm_opts.print_name == PRINT_NAME_MULTI &&
1535 nm_opts.elem_print_fn == sym_elem_print_all)
1536 printf("\n%s:\n", filename);
1537 if (nm_opts.print_armap == true)
1538 print_ar_index(fd, arf);
1541 while ((elf = elf_begin(fd, elf_cmd, arf)) != NULL) {
1542 rtn |= read_elf(elf, filename, kind);
1545 * If file is not archive, elf_next return ELF_C_NULL and
1548 elf_cmd = elf_next(elf);
1559 read_files(int argc, char **argv)
1563 if (argc < 0 || argv == NULL)
1567 rtn |= read_object(nm_info.def_filename);
1569 if (nm_opts.print_name == PRINT_NAME_NONE && argc > 1)
1570 nm_opts.print_name = PRINT_NAME_MULTI;
1572 rtn |= read_object(*argv);
1582 print_lineno(struct sym_entry *ep, struct func_info_head *func_info,
1583 struct var_info_head *var_info, struct line_info_head *line_info)
1585 struct func_info_entry *func;
1586 struct var_info_entry *var;
1587 struct line_info_entry *lie;
1589 /* For function symbol, search the function line information list. */
1590 if ((ep->sym->st_info & 0xf) == STT_FUNC && func_info != NULL) {
1591 SLIST_FOREACH(func, func_info, entries) {
1592 if (func->name != NULL &&
1593 !strcmp(ep->name, func->name) &&
1594 ep->sym->st_value >= func->lowpc &&
1595 ep->sym->st_value < func->highpc) {
1596 printf("\t%s:%" PRIu64, func->file, func->line);
1602 /* For variable symbol, search the variable line information list. */
1603 if ((ep->sym->st_info & 0xf) == STT_OBJECT && var_info != NULL) {
1604 SLIST_FOREACH(var, var_info, entries) {
1605 if (!strcmp(ep->name, var->name) &&
1606 ep->sym->st_value == var->addr) {
1607 printf("\t%s:%" PRIu64, var->file, var->line);
1613 /* Otherwise search line number information the .debug_line section. */
1614 if (line_info != NULL) {
1615 SLIST_FOREACH(lie, line_info, entries) {
1616 if (ep->sym->st_value == lie->addr) {
1617 printf("\t%s:%" PRIu64, lie->file, lie->line);
1625 set_opt_value_print_fn(enum radix t)
1630 nm_opts.value_print_fn = &sym_value_oct_print;
1631 nm_opts.size_print_fn = &sym_size_oct_print;
1635 nm_opts.value_print_fn = &sym_value_dec_print;
1636 nm_opts.size_print_fn = &sym_size_dec_print;
1641 nm_opts.value_print_fn = &sym_value_hex_print;
1642 nm_opts.size_print_fn = &sym_size_hex_print;
1645 assert(nm_opts.value_print_fn != NULL &&
1646 "nm_opts.value_print_fn is null");
1650 sym_elem_print_all(char type, const char *sec, const GElf_Sym *sym,
1654 if (sec == NULL || sym == NULL || name == NULL ||
1655 nm_opts.value_print_fn == NULL)
1658 if (IS_UNDEF_SYM_TYPE(type)) {
1659 if (nm_opts.t == RADIX_HEX && nm_elfclass == ELFCLASS32)
1662 printf("%-16s", "");
1664 switch ((nm_opts.sort_fn == & cmp_size ? 2 : 0) +
1665 nm_opts.print_size) {
1667 if (sym->st_size != 0) {
1668 nm_opts.value_print_fn(sym);
1670 nm_opts.size_print_fn(sym);
1675 if (sym->st_size != 0)
1676 nm_opts.size_print_fn(sym);
1680 nm_opts.value_print_fn(sym);
1681 if (sym->st_size != 0) {
1683 nm_opts.size_print_fn(sym);
1689 nm_opts.value_print_fn(sym);
1693 printf(" %c ", type);
1694 PRINT_DEMANGLED_NAME("%s", name);
1698 sym_elem_print_all_portable(char type, const char *sec, const GElf_Sym *sym,
1702 if (sec == NULL || sym == NULL || name == NULL ||
1703 nm_opts.value_print_fn == NULL)
1706 PRINT_DEMANGLED_NAME("%s", name);
1707 printf(" %c ", type);
1708 if (!IS_UNDEF_SYM_TYPE(type)) {
1709 nm_opts.value_print_fn(sym);
1711 if (sym->st_size != 0)
1712 nm_opts.size_print_fn(sym);
1718 sym_elem_print_all_sysv(char type, const char *sec, const GElf_Sym *sym,
1722 if (sec == NULL || sym == NULL || name == NULL ||
1723 nm_opts.value_print_fn == NULL)
1726 PRINT_DEMANGLED_NAME("%-20s|", name);
1727 if (IS_UNDEF_SYM_TYPE(type))
1730 nm_opts.value_print_fn(sym);
1732 printf("| %c |", type);
1734 switch (sym->st_info & 0xf) {
1736 printf("%18s|", "OBJECT");
1740 printf("%18s|", "FUNC");
1744 printf("%18s|", "SECTION");
1748 printf("%18s|", "FILE");
1752 printf("%18s|", "LOPROC");
1756 printf("%18s|", "HIPROC");
1761 printf("%18s|", "NOTYPE");
1764 if (sym->st_size != 0)
1765 nm_opts.size_print_fn(sym);
1769 printf("| |%s", sec);
1773 sym_elem_def(char type, const GElf_Sym *sym, const char *name)
1776 assert(IS_SYM_TYPE((unsigned char) type));
1781 return (!IS_UNDEF_SYM_TYPE((unsigned char) type));
1785 sym_elem_global(char type, const GElf_Sym *sym, const char *name)
1788 assert(IS_SYM_TYPE((unsigned char) type));
1793 /* weak symbols resemble global. */
1794 return (isupper((unsigned char) type) || type == 'w');
1798 sym_elem_global_static(char type, const GElf_Sym *sym, const char *name)
1802 assert(sym != NULL);
1807 info = sym->st_info >> 4;
1809 return (info == STB_LOCAL ||
1810 info == STB_GLOBAL ||
1815 sym_elem_nondebug(char type, const GElf_Sym *sym, const char *name)
1818 assert(sym != NULL);
1823 if (sym->st_value == 0 && (sym->st_info & 0xf) == STT_FILE)
1825 if (sym->st_name == 0)
1832 sym_elem_nonzero_size(char type, const GElf_Sym *sym, const char *name)
1835 assert(sym != NULL);
1840 return (sym->st_size > 0);
1844 sym_elem_undef(char type, const GElf_Sym *sym, const char *name)
1847 assert(IS_SYM_TYPE((unsigned char) type));
1852 return (IS_UNDEF_SYM_TYPE((unsigned char) type));
1856 sym_list_dest(struct sym_head *headp)
1858 struct sym_entry *ep, *ep_n;
1863 ep = STAILQ_FIRST(headp);
1864 while (ep != NULL) {
1865 ep_n = STAILQ_NEXT(ep, sym_entries);
1874 sym_list_insert(struct sym_head *headp, const char *name, const GElf_Sym *sym)
1876 struct sym_entry *e;
1878 if (headp == NULL || name == NULL || sym == NULL)
1880 if ((e = malloc(sizeof(struct sym_entry))) == NULL) {
1884 if ((e->name = strdup(name)) == NULL) {
1889 if ((e->sym = malloc(sizeof(GElf_Sym))) == NULL) {
1896 memcpy(e->sym, sym, sizeof(GElf_Sym));
1898 /* Display size instead of value for common symbol. */
1899 if (sym->st_shndx == SHN_COMMON)
1900 e->sym->st_value = sym->st_size;
1902 STAILQ_INSERT_TAIL(headp, e, sym_entries);
1907 /* If file has not .debug_info, line_info will be NULL */
1909 sym_list_print(struct sym_print_data *p, struct func_info_head *func_info,
1910 struct var_info_head *var_info, struct line_info_head *line_info)
1912 struct sym_entry *e_v;
1916 if (p == NULL || CHECK_SYM_PRINT_DATA(p))
1918 if ((e_v = sym_list_sort(p)) == NULL)
1920 if (nm_opts.sort_reverse == false)
1921 for (si = 0; si != p->list_num; ++si)
1922 sym_list_print_each(&e_v[si], p, func_info, var_info,
1925 for (i = p->list_num - 1; i != -1; --i)
1926 sym_list_print_each(&e_v[i], p, func_info, var_info,
1932 /* If file has not .debug_info, line_info will be NULL */
1934 sym_list_print_each(struct sym_entry *ep, struct sym_print_data *p,
1935 struct func_info_head *func_info, struct var_info_head *var_info,
1936 struct line_info_head *line_info)
1941 if (ep == NULL || CHECK_SYM_PRINT_DATA(p))
1944 assert(ep->name != NULL);
1945 assert(ep->sym != NULL);
1947 type = get_sym_type(ep->sym, p->t_table);
1949 if (nm_opts.print_name == PRINT_NAME_FULL) {
1950 printf("%s", p->filename);
1951 if (nm_opts.elem_print_fn == &sym_elem_print_all_portable) {
1952 if (p->objname != NULL)
1953 printf("[%s]", p->objname);
1956 if (p->objname != NULL)
1957 printf(":%s", p->objname);
1962 switch (ep->sym->st_shndx) {
1964 /* LOPROC or LORESERVE */
1983 /* HIRESERVE or XINDEX */
1984 sec = "*HIRESERVE*";
1987 if (ep->sym->st_shndx > p->sh_num)
1989 sec = p->s_table[ep->sym->st_shndx];
1993 nm_opts.elem_print_fn(type, sec, ep->sym, ep->name);
1995 if (nm_opts.debug_line == true && !IS_UNDEF_SYM_TYPE(type))
1996 print_lineno(ep, func_info, var_info, line_info);
2001 static struct sym_entry *
2002 sym_list_sort(struct sym_print_data *p)
2004 struct sym_entry *ep, *e_v;
2007 if (p == NULL || CHECK_SYM_PRINT_DATA(p))
2010 if ((e_v = malloc(sizeof(struct sym_entry) * p->list_num)) == NULL) {
2016 STAILQ_FOREACH(ep, p->headp, sym_entries) {
2017 if (ep->name != NULL && ep->sym != NULL) {
2018 e_v[idx].name = ep->name;
2019 e_v[idx].sym = ep->sym;
2024 assert((size_t)idx == p->list_num);
2026 if (nm_opts.sort_fn != &cmp_none) {
2028 assert(nm_print_data != NULL);
2029 qsort(e_v, p->list_num, sizeof(struct sym_entry),
2037 sym_size_oct_print(const GElf_Sym *sym)
2040 assert(sym != NULL && "sym is null");
2041 printf("%016" PRIo64, sym->st_size);
2045 sym_size_hex_print(const GElf_Sym *sym)
2048 assert(sym != NULL && "sym is null");
2049 if (nm_elfclass == ELFCLASS32)
2050 printf("%08" PRIx64, sym->st_size);
2052 printf("%016" PRIx64, sym->st_size);
2056 sym_size_dec_print(const GElf_Sym *sym)
2059 assert(sym != NULL && "sym is null");
2060 printf("%016" PRId64, sym->st_size);
2064 sym_value_oct_print(const GElf_Sym *sym)
2067 assert(sym != NULL && "sym is null");
2068 printf("%016" PRIo64, sym->st_value);
2072 sym_value_hex_print(const GElf_Sym *sym)
2075 assert(sym != NULL && "sym is null");
2076 if (nm_elfclass == ELFCLASS32)
2077 printf("%08" PRIx64, sym->st_value);
2079 printf("%016" PRIx64, sym->st_value);
2083 sym_value_dec_print(const GElf_Sym *sym)
2086 assert(sym != NULL && "sym is null");
2087 printf("%016" PRId64, sym->st_value);
2094 printf("Usage: %s [options] file ...\
2095 \n Display symbolic information in file.\n\
2097 \n -A, --print-file-name Write the full pathname or library name of an\
2098 \n object on each line.\
2099 \n -a, --debug-syms Display all symbols include debugger-only\
2100 \n symbols.", nm_info.name);
2102 \n -B Equivalent to specifying \"--format=bsd\".\
2103 \n -C, --demangle[=style] Decode low-level symbol names.\
2104 \n --no-demangle Do not demangle low-level symbol names.\
2105 \n -D, --dynamic Display only dynamic symbols.\
2106 \n -e Display only global and static symbols.");
2108 \n -f Produce full output (default).\
2109 \n --format=format Display output in specific format. Allowed\
2110 \n formats are: \"bsd\", \"posix\" and \"sysv\".\
2111 \n -g, --extern-only Display only global symbol information.\
2112 \n -h, --help Show this help message.\
2113 \n -l, --line-numbers Display filename and linenumber using\
2114 \n debugging information.\
2115 \n -n, --numeric-sort Sort symbols numerically by value.");
2117 \n -o Write numeric values in octal. Equivalent to\
2118 \n specifying \"-t o\".\
2119 \n -p, --no-sort Do not sort symbols.\
2120 \n -P Write information in a portable output format.\
2121 \n Equivalent to specifying \"--format=posix\".\
2122 \n -r, --reverse-sort Reverse the order of the sort.\
2123 \n -S, --print-size Print symbol sizes instead values.\
2124 \n -s, --print-armap Include an index of archive members.\
2125 \n --size-sort Sort symbols by size.");
2127 \n -t, --radix=format Write each numeric value in the specified\
2131 \n x In hexadecimal.");
2133 \n -u, --undefined-only Display only undefined symbols.\
2134 \n --defined-only Display only defined symbols.\
2135 \n -V, --version Show the version identifier for %s.\
2136 \n -v Sort output by value.\
2137 \n -x Write numeric values in hexadecimal.\
2138 \n Equivalent to specifying \"-t x\".",
2141 \n The default options are: output in bsd format, use a hexadecimal radix,\
2142 \n sort by symbol name, do not demangle names.\n");
2148 * Display symbolic information in file.
2149 * Return 0 at success, >0 at failed.
2152 main(int argc, char **argv)
2157 get_opt(&argc, &argv);
2158 enter_cap_mode(argc, argv);
2159 rtn = read_files(argc, argv);