2 * Copyright (c) 2007-2012 Kai Wang
3 * Copyright (c) 2003 David O'Brien. All rights reserved.
4 * Copyright (c) 2001 Jake Burkholder
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
30 #include <sys/queue.h>
46 #ifdef USE_LIBARCHIVE_AR
48 #include <archive_entry.h>
53 ELFTC_VCSID("$Id: elfdump.c 3762 2019-06-28 21:06:24Z emaste $");
55 #if defined(ELFTC_NEED_ELF_NOTE_DEFINITION)
56 #include "native-elf-format.h"
57 #if ELFTC_CLASS == ELFCLASS32
58 typedef Elf32_Nhdr Elf_Note;
60 typedef Elf64_Nhdr Elf_Note;
64 /* elfdump(1) options. */
66 #define ED_EHDR (1<<1)
68 #define ED_HASH (1<<3)
69 #define ED_INTERP (1<<4)
70 #define ED_NOTE (1<<5)
71 #define ED_PHDR (1<<6)
73 #define ED_SHDR (1<<8)
74 #define ED_SYMTAB (1<<9)
75 #define ED_SYMVER (1<<10)
76 #define ED_CHECKSUM (1<<11)
77 #define ED_ALL ((1<<12)-1)
79 /* elfdump(1) run control flags. */
80 #define SOLARIS_FMT (1<<0)
81 #define PRINT_FILENAME (1<<1)
82 #define PRINT_ARSYM (1<<2)
83 #define ONLY_ARSYM (1<<3)
85 /* Convenient print macro. */
86 #define PRT(...) fprintf(ed->out, __VA_ARGS__)
88 /* Internal data structure for sections. */
90 const char *name; /* section name */
91 Elf_Scn *scn; /* section scn */
92 uint64_t off; /* section offset */
93 uint64_t sz; /* section size */
94 uint64_t entsize; /* section entsize */
95 uint64_t align; /* section alignment */
96 uint64_t type; /* section type */
97 uint64_t flags; /* section flags */
98 uint64_t addr; /* section virtual addr */
99 uint32_t link; /* section link ndx */
100 uint32_t info; /* section info ndx */
105 STAILQ_ENTRY(spec_name) sn_list;
108 /* Structure encapsulates the global data for readelf(1). */
110 FILE *out; /* output redirection. */
111 const char *filename; /* current processing file. */
112 const char *archive; /* archive name */
113 int options; /* command line options. */
114 int flags; /* run control flags. */
115 Elf *elf; /* underlying ELF descriptor. */
116 #ifndef USE_LIBARCHIVE_AR
117 Elf *ar; /* ar(1) archive descriptor. */
119 GElf_Ehdr ehdr; /* ELF header. */
120 int ec; /* ELF class. */
121 size_t shnum; /* #sections. */
122 struct section *sl; /* list of sections. */
123 STAILQ_HEAD(, spec_name) snl; /* list of names specified by -N. */
126 /* Relocation entry. */
136 #if defined(ELFTC_NEED_BYTEORDER_EXTENSIONS)
137 static __inline uint32_t
138 be32dec(const void *pp)
140 unsigned char const *p = (unsigned char const *)pp;
142 return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
145 static __inline uint32_t
146 le32dec(const void *pp)
148 unsigned char const *p = (unsigned char const *)pp;
150 return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
154 /* http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#tag_encodings */
158 static char unknown_buf[64];
161 case DT_NULL: return "DT_NULL";
162 case DT_NEEDED: return "DT_NEEDED";
163 case DT_PLTRELSZ: return "DT_PLTRELSZ";
164 case DT_PLTGOT: return "DT_PLTGOT";
165 case DT_HASH: return "DT_HASH";
166 case DT_STRTAB: return "DT_STRTAB";
167 case DT_SYMTAB: return "DT_SYMTAB";
168 case DT_RELA: return "DT_RELA";
169 case DT_RELASZ: return "DT_RELASZ";
170 case DT_RELAENT: return "DT_RELAENT";
171 case DT_STRSZ: return "DT_STRSZ";
172 case DT_SYMENT: return "DT_SYMENT";
173 case DT_INIT: return "DT_INIT";
174 case DT_FINI: return "DT_FINI";
175 case DT_SONAME: return "DT_SONAME";
176 case DT_RPATH: return "DT_RPATH";
177 case DT_SYMBOLIC: return "DT_SYMBOLIC";
178 case DT_REL: return "DT_REL";
179 case DT_RELSZ: return "DT_RELSZ";
180 case DT_RELENT: return "DT_RELENT";
181 case DT_PLTREL: return "DT_PLTREL";
182 case DT_DEBUG: return "DT_DEBUG";
183 case DT_TEXTREL: return "DT_TEXTREL";
184 case DT_JMPREL: return "DT_JMPREL";
185 case DT_BIND_NOW: return "DT_BIND_NOW";
186 case DT_INIT_ARRAY: return "DT_INIT_ARRAY";
187 case DT_FINI_ARRAY: return "DT_FINI_ARRAY";
188 case DT_INIT_ARRAYSZ: return "DT_INIT_ARRAYSZ";
189 case DT_FINI_ARRAYSZ: return "DT_FINI_ARRAYSZ";
190 case DT_RUNPATH: return "DT_RUNPATH";
191 case DT_FLAGS: return "DT_FLAGS";
192 case DT_PREINIT_ARRAY: return "DT_PREINIT_ARRAY"; /* XXX DT_ENCODING */
193 case DT_PREINIT_ARRAYSZ:return "DT_PREINIT_ARRAYSZ";
194 /* 0x6000000D - 0x6ffff000 operating system-specific semantics */
195 case 0x6ffffdf5: return "DT_GNU_PRELINKED";
196 case 0x6ffffdf6: return "DT_GNU_CONFLICTSZ";
197 case 0x6ffffdf7: return "DT_GNU_LIBLISTSZ";
198 case 0x6ffffdf8: return "DT_SUNW_CHECKSUM";
199 case DT_PLTPADSZ: return "DT_PLTPADSZ";
200 case DT_MOVEENT: return "DT_MOVEENT";
201 case DT_MOVESZ: return "DT_MOVESZ";
202 case 0x6ffffdfc: return "DT_FEATURE";
203 case DT_POSFLAG_1: return "DT_POSFLAG_1";
204 case DT_SYMINSZ: return "DT_SYMINSZ";
205 case DT_SYMINENT: return "DT_SYMINENT (DT_VALRNGHI)";
206 case DT_ADDRRNGLO: return "DT_ADDRRNGLO";
207 case DT_GNU_HASH: return "DT_GNU_HASH";
208 case 0x6ffffef8: return "DT_GNU_CONFLICT";
209 case 0x6ffffef9: return "DT_GNU_LIBLIST";
210 case 0x6ffffefa: return "DT_CONFIG";
211 case 0x6ffffefb: return "DT_DEPAUDIT";
212 case 0x6ffffefc: return "DT_AUDIT";
213 case 0x6ffffefd: return "DT_PLTPAD";
214 case 0x6ffffefe: return "DT_MOVETAB";
215 case DT_SYMINFO: return "DT_SYMINFO (DT_ADDRRNGHI)";
216 case DT_RELACOUNT: return "DT_RELACOUNT";
217 case DT_RELCOUNT: return "DT_RELCOUNT";
218 case DT_FLAGS_1: return "DT_FLAGS_1";
219 case DT_VERDEF: return "DT_VERDEF";
220 case DT_VERDEFNUM: return "DT_VERDEFNUM";
221 case DT_VERNEED: return "DT_VERNEED";
222 case DT_VERNEEDNUM: return "DT_VERNEEDNUM";
223 case 0x6ffffff0: return "DT_GNU_VERSYM";
224 /* 0x70000000 - 0x7fffffff processor-specific semantics */
225 case 0x70000000: return "DT_IA_64_PLT_RESERVE";
226 case DT_AUXILIARY: return "DT_AUXILIARY";
227 case DT_USED: return "DT_USED";
228 case DT_FILTER: return "DT_FILTER";
231 snprintf(unknown_buf, sizeof(unknown_buf),
232 "<unknown: %#llx>", (unsigned long long)tag);
233 return (unknown_buf);
237 e_machines(unsigned int mach)
239 static char machdesc[64];
242 case EM_NONE: return "EM_NONE";
243 case EM_M32: return "EM_M32";
244 case EM_SPARC: return "EM_SPARC";
245 case EM_386: return "EM_386";
246 case EM_68K: return "EM_68K";
247 case EM_88K: return "EM_88K";
248 case EM_IAMCU: return "EM_IAMCU";
249 case EM_860: return "EM_860";
250 case EM_MIPS: return "EM_MIPS";
251 case EM_PPC: return "EM_PPC";
252 case EM_PPC64: return "EM_PPC64";
253 case EM_ARM: return "EM_ARM";
254 case EM_ALPHA: return "EM_ALPHA (legacy)";
255 case EM_SPARCV9:return "EM_SPARCV9";
256 case EM_IA_64: return "EM_IA_64";
257 case EM_X86_64: return "EM_X86_64";
258 case EM_AARCH64:return "EM_AARCH64";
259 case EM_RISCV: return "EM_RISCV";
261 snprintf(machdesc, sizeof(machdesc),
262 "(unknown machine) -- type 0x%x", mach);
267 elf_type_str(unsigned int type)
269 static char s_type[32];
273 case ET_NONE: return "ET_NONE";
274 case ET_REL: return "ET_REL";
275 case ET_EXEC: return "ET_EXEC";
276 case ET_DYN: return "ET_DYN";
277 case ET_CORE: return "ET_CORE";
279 if (type >= ET_LOPROC)
280 snprintf(s_type, sizeof(s_type), "<proc: %#x>", type);
281 else if (type >= ET_LOOS && type <= ET_HIOS)
282 snprintf(s_type, sizeof(s_type), "<os: %#x>", type);
284 snprintf(s_type, sizeof(s_type), "<unknown: %#x", type);
289 elf_version_str(unsigned int ver)
291 static char s_ver[32];
294 case EV_NONE: return "EV_NONE";
295 case EV_CURRENT: return "EV_CURRENT";
297 snprintf(s_ver, sizeof(s_ver), "<unknown: %#x>", ver);
302 elf_class_str(unsigned int class)
304 static char s_class[32];
307 case ELFCLASSNONE: return "ELFCLASSNONE";
308 case ELFCLASS32: return "ELFCLASS32";
309 case ELFCLASS64: return "ELFCLASS64";
311 snprintf(s_class, sizeof(s_class), "<unknown: %#x>", class);
316 elf_data_str(unsigned int data)
318 static char s_data[32];
321 case ELFDATANONE: return "ELFDATANONE";
322 case ELFDATA2LSB: return "ELFDATA2LSB";
323 case ELFDATA2MSB: return "ELFDATA2MSB";
325 snprintf(s_data, sizeof(s_data), "<unknown: %#x>", data);
329 static const char *ei_abis[256] = {
330 "ELFOSABI_NONE", "ELFOSABI_HPUX", "ELFOSABI_NETBSD", "ELFOSABI_LINUX",
331 "ELFOSABI_HURD", "ELFOSABI_86OPEN", "ELFOSABI_SOLARIS", "ELFOSABI_AIX",
332 "ELFOSABI_IRIX", "ELFOSABI_FREEBSD", "ELFOSABI_TRU64",
333 "ELFOSABI_MODESTO", "ELFOSABI_OPENBSD",
334 [17] = "ELFOSABI_CLOUDABI",
335 [64] = "ELFOSABI_ARM_AEABI",
336 [97] = "ELFOSABI_ARM",
337 [255] = "ELFOSABI_STANDALONE"
341 elf_phdr_type_str(unsigned int type)
343 static char s_type[32];
346 case PT_NULL: return "PT_NULL";
347 case PT_LOAD: return "PT_LOAD";
348 case PT_DYNAMIC: return "PT_DYNAMIC";
349 case PT_INTERP: return "PT_INTERP";
350 case PT_NOTE: return "PT_NOTE";
351 case PT_SHLIB: return "PT_SHLIB";
352 case PT_PHDR: return "PT_PHDR";
353 case PT_TLS: return "PT_TLS";
354 case PT_GNU_EH_FRAME: return "PT_GNU_EH_FRAME";
355 case PT_GNU_STACK: return "PT_GNU_STACK";
356 case PT_GNU_RELRO: return "PT_GNU_RELRO";
357 case PT_OPENBSD_RANDOMIZE: return "PT_OPENBSD_RANDOMIZE";
358 case PT_OPENBSD_WXNEEDED: return "PT_OPENBSD_WXNEEDED";
359 case PT_OPENBSD_BOOTDATA: return "PT_OPENBSD_BOOTDATA";
361 snprintf(s_type, sizeof(s_type), "<unknown: %#x>", type);
365 static const char *p_flags[] = {
366 "", "PF_X", "PF_W", "PF_X|PF_W", "PF_R", "PF_X|PF_R", "PF_W|PF_R",
371 sh_name(struct elfdump *ed, int ndx)
376 case SHN_UNDEF: return "UNDEF";
377 case SHN_ABS: return "ABS";
378 case SHN_COMMON: return "COMMON";
380 if ((uint64_t)ndx < ed->shnum)
381 return (ed->sl[ndx].name);
383 snprintf(num, sizeof(num), "%d", ndx);
389 /* http://www.sco.com/developers/gabi/latest/ch4.sheader.html#sh_type */
391 sh_types(uint64_t mach, uint64_t sht) {
392 static char unknown_buf[64];
394 if (sht < 0x60000000) {
396 case SHT_NULL: return "SHT_NULL";
397 case SHT_PROGBITS: return "SHT_PROGBITS";
398 case SHT_SYMTAB: return "SHT_SYMTAB";
399 case SHT_STRTAB: return "SHT_STRTAB";
400 case SHT_RELA: return "SHT_RELA";
401 case SHT_HASH: return "SHT_HASH";
402 case SHT_DYNAMIC: return "SHT_DYNAMIC";
403 case SHT_NOTE: return "SHT_NOTE";
404 case SHT_NOBITS: return "SHT_NOBITS";
405 case SHT_REL: return "SHT_REL";
406 case SHT_SHLIB: return "SHT_SHLIB";
407 case SHT_DYNSYM: return "SHT_DYNSYM";
408 case SHT_INIT_ARRAY: return "SHT_INIT_ARRAY";
409 case SHT_FINI_ARRAY: return "SHT_FINI_ARRAY";
410 case SHT_PREINIT_ARRAY: return "SHT_PREINIT_ARRAY";
411 case SHT_GROUP: return "SHT_GROUP";
412 case SHT_SYMTAB_SHNDX: return "SHT_SYMTAB_SHNDX";
414 } else if (sht < 0x70000000) {
415 /* 0x60000000-0x6fffffff operating system-specific semantics */
417 case 0x6ffffff0: return "XXX:VERSYM";
418 case SHT_SUNW_dof: return "SHT_SUNW_dof";
419 case SHT_GNU_HASH: return "SHT_GNU_HASH";
420 case 0x6ffffff7: return "SHT_GNU_LIBLIST";
421 case 0x6ffffffc: return "XXX:VERDEF";
422 case SHT_SUNW_verdef: return "SHT_SUNW(GNU)_verdef";
423 case SHT_SUNW_verneed: return "SHT_SUNW(GNU)_verneed";
424 case SHT_SUNW_versym: return "SHT_SUNW(GNU)_versym";
426 } else if (sht < 0x80000000) {
427 /* 0x70000000 - 0x7fffffff processor-specific semantics */
431 case SHT_ARM_EXIDX: return "SHT_ARM_EXIDX";
432 case SHT_ARM_PREEMPTMAP: return "SHT_ARM_PREEMPTMAP";
433 case SHT_ARM_ATTRIBUTES: return "SHT_ARM_ATTRIBUTES";
434 case SHT_ARM_DEBUGOVERLAY:
435 return "SHT_ARM_DEBUGOVERLAY";
436 case SHT_ARM_OVERLAYSECTION:
437 return "SHT_ARM_OVERLAYSECTION";
442 case 0x70000000: return "SHT_IA_64_EXT";
443 case 0x70000001: return "SHT_IA_64_UNWIND";
448 case SHT_MIPS_REGINFO: return "SHT_MIPS_REGINFO";
449 case SHT_MIPS_OPTIONS: return "SHT_MIPS_OPTIONS";
450 case SHT_MIPS_ABIFLAGS: return "SHT_MIPS_ABIFLAGS";
455 case 0x7ffffffd: return "XXX:AUXILIARY";
456 case 0x7fffffff: return "XXX:FILTER";
459 /* 0x80000000 - 0xffffffff application programs */
461 snprintf(unknown_buf, sizeof(unknown_buf),
462 "<unknown: %#llx>", (unsigned long long)sht);
463 return (unknown_buf);
467 * Define known section flags. These flags are defined in the order
468 * they are to be printed out.
470 #define DEFINE_SHFLAGS() \
473 DEFINE_SHF(EXECINSTR) \
475 DEFINE_SHF(STRINGS) \
476 DEFINE_SHF(INFO_LINK) \
477 DEFINE_SHF(LINK_ORDER) \
478 DEFINE_SHF(OS_NONCONFORMING) \
481 DEFINE_SHF(COMPRESSED)
484 #define DEFINE_SHF(F) "SHF_" #F "|"
485 #define ALLSHFLAGS DEFINE_SHFLAGS()
488 sh_flags(uint64_t shf)
490 static char flg[sizeof(ALLSHFLAGS)+1];
495 #define DEFINE_SHF(N) \
497 strcat(flg, "SHF_" #N "|"); \
501 flg[strlen(flg) - 1] = '\0'; /* Remove the trailing "|". */
507 st_type(unsigned int mach, unsigned int type)
509 static char s_type[32];
512 case STT_NOTYPE: return "STT_NOTYPE";
513 case STT_OBJECT: return "STT_OBJECT";
514 case STT_FUNC: return "STT_FUNC";
515 case STT_SECTION: return "STT_SECTION";
516 case STT_FILE: return "STT_FILE";
517 case STT_COMMON: return "STT_COMMON";
518 case STT_TLS: return "STT_TLS";
520 if (mach == EM_SPARCV9)
521 return "STT_SPARC_REGISTER";
524 snprintf(s_type, sizeof(s_type), "<unknown: %#x>", type);
529 st_type_S(unsigned int type)
531 static char s_type[32];
534 case STT_NOTYPE: return "NOTY";
535 case STT_OBJECT: return "OBJT";
536 case STT_FUNC: return "FUNC";
537 case STT_SECTION: return "SECT";
538 case STT_FILE: return "FILE";
540 snprintf(s_type, sizeof(s_type), "<unknown: %#x>", type);
545 st_bindings(unsigned int sbind)
547 static char s_sbind[32];
550 case STB_LOCAL: return "STB_LOCAL";
551 case STB_GLOBAL: return "STB_GLOBAL";
552 case STB_WEAK: return "STB_WEAK";
553 case STB_GNU_UNIQUE: return "STB_GNU_UNIQUE";
555 if (sbind >= STB_LOOS && sbind <= STB_HIOS)
557 else if (sbind >= STB_LOPROC && sbind <= STB_HIPROC)
560 snprintf(s_sbind, sizeof(s_sbind), "<unknown: %#x>",
567 st_bindings_S(unsigned int sbind)
569 static char s_sbind[32];
572 case STB_LOCAL: return "LOCL";
573 case STB_GLOBAL: return "GLOB";
574 case STB_WEAK: return "WEAK";
575 case STB_GNU_UNIQUE: return "UNIQ";
577 if (sbind >= STB_LOOS && sbind <= STB_HIOS)
579 else if (sbind >= STB_LOPROC && sbind <= STB_HIPROC)
582 snprintf(s_sbind, sizeof(s_sbind), "<%#x>",
588 static unsigned char st_others[] = {
592 static void add_name(struct elfdump *ed, const char *name);
593 static void elf_print_object(struct elfdump *ed);
594 static void elf_print_elf(struct elfdump *ed);
595 static void elf_print_ehdr(struct elfdump *ed);
596 static void elf_print_phdr(struct elfdump *ed);
597 static void elf_print_shdr(struct elfdump *ed);
598 static void elf_print_symtab(struct elfdump *ed, int i);
599 static void elf_print_symtabs(struct elfdump *ed);
600 static void elf_print_symver(struct elfdump *ed);
601 static void elf_print_verdef(struct elfdump *ed, struct section *s);
602 static void elf_print_verneed(struct elfdump *ed, struct section *s);
603 static void elf_print_interp(struct elfdump *ed);
604 static void elf_print_dynamic(struct elfdump *ed);
605 static void elf_print_rel_entry(struct elfdump *ed, struct section *s,
606 int j, struct rel_entry *r);
607 static void elf_print_rela(struct elfdump *ed, struct section *s,
609 static void elf_print_rel(struct elfdump *ed, struct section *s,
611 static void elf_print_reloc(struct elfdump *ed);
612 static void elf_print_got(struct elfdump *ed);
613 static void elf_print_got_section(struct elfdump *ed, struct section *s);
614 static void elf_print_note(struct elfdump *ed);
615 static void elf_print_svr4_hash(struct elfdump *ed, struct section *s);
616 static void elf_print_svr4_hash64(struct elfdump *ed, struct section *s);
617 static void elf_print_gnu_hash(struct elfdump *ed, struct section *s);
618 static void elf_print_hash(struct elfdump *ed);
619 static void elf_print_checksum(struct elfdump *ed);
620 static void find_gotrel(struct elfdump *ed, struct section *gs,
621 struct rel_entry *got);
622 static struct spec_name *find_name(struct elfdump *ed, const char *name);
623 static int get_ent_count(const struct section *s, int *ent_count);
624 static const char *get_symbol_name(struct elfdump *ed, uint32_t symtab, int i);
625 static const char *get_string(struct elfdump *ed, int strtab, size_t off);
626 static void get_versym(struct elfdump *ed, int i, uint16_t **vs, int *nvs);
627 static void load_sections(struct elfdump *ed);
628 static void unload_sections(struct elfdump *ed);
629 static void usage(void);
630 #ifdef USE_LIBARCHIVE_AR
631 static int ac_detect_ar(int fd);
632 static void ac_print_ar(struct elfdump *ed, int fd);
634 static void elf_print_ar(struct elfdump *ed, int fd);
635 #endif /* USE_LIBARCHIVE_AR */
637 static struct option elfdump_longopts[] =
639 { "help", no_argument, NULL, 'H' },
640 { "version", no_argument, NULL, 'V' },
645 main(int ac, char **av)
647 struct elfdump *ed, ed_storage;
648 struct spec_name *sn;
652 memset(ed, 0, sizeof(*ed));
653 STAILQ_INIT(&ed->snl);
655 while ((ch = getopt_long(ac, av, "acdeiGHhknN:prsSvVw:",
656 elfdump_longopts, NULL)) != -1)
659 ed->options = ED_ALL;
662 ed->options |= ED_SHDR;
665 ed->options |= ED_DYN;
668 ed->options |= ED_EHDR;
671 ed->options |= ED_INTERP;
674 ed->options |= ED_GOT;
677 ed->options |= ED_HASH;
680 ed->options |= ED_CHECKSUM;
683 ed->options |= ED_NOTE;
686 add_name(ed, optarg);
689 ed->options |= ED_PHDR;
692 ed->options |= ED_REL;
695 ed->options |= ED_SYMTAB;
698 ed->flags |= SOLARIS_FMT;
701 ed->options |= ED_SYMVER;
704 (void) printf("%s (%s)\n", ELFTC_GETPROGNAME(),
709 if ((ed->out = fopen(optarg, "w")) == NULL)
710 err(EXIT_FAILURE, "%s", optarg);
721 if (ed->options == 0)
722 ed->options = ED_ALL;
724 if (ed->options & ED_SYMTAB &&
725 (STAILQ_EMPTY(&ed->snl) || (sn = find_name(ed, "ARSYM")) != NULL)) {
726 ed->flags |= PRINT_ARSYM;
728 STAILQ_REMOVE(&ed->snl, sn, spec_name, sn_list);
729 if (STAILQ_EMPTY(&ed->snl))
730 ed->flags |= ONLY_ARSYM;
736 ed->flags |= PRINT_FILENAME;
737 if (elf_version(EV_CURRENT) == EV_NONE)
738 errx(EXIT_FAILURE, "ELF library initialization failed: %s",
741 for (i = 0; i < ac; i++) {
742 ed->filename = av[i];
744 elf_print_object(ed);
750 #ifdef USE_LIBARCHIVE_AR
752 /* Archive symbol table entry. */
759 * Convenient wrapper for general libarchive error handling.
761 #define AC(CALL) do { \
763 warnx("%s", archive_error_string(a)); \
769 * Detect an ar(1) archive using libarchive(3).
775 struct archive_entry *entry;
779 if ((a = archive_read_new()) == NULL)
781 archive_read_support_format_ar(a);
782 if (archive_read_open_fd(a, fd, 10240) == ARCHIVE_OK)
783 r = archive_read_next_header(a, &entry);
784 archive_read_close(a);
785 archive_read_free(a);
787 return (r == ARCHIVE_OK);
791 * Dump an ar(1) archive using libarchive(3).
794 ac_print_ar(struct elfdump *ed, int fd)
797 struct archive_entry *entry;
798 struct arsym_entry *arsym;
806 if (lseek(fd, 0, SEEK_SET) == -1)
807 err(EXIT_FAILURE, "lseek failed");
808 if ((a = archive_read_new()) == NULL)
809 errx(EXIT_FAILURE, "%s", archive_error_string(a));
810 archive_read_support_format_ar(a);
811 AC(archive_read_open_fd(a, fd, 10240));
813 r = archive_read_next_header(a, &entry);
814 if (r == ARCHIVE_FATAL)
815 errx(EXIT_FAILURE, "%s", archive_error_string(a));
816 if (r == ARCHIVE_EOF)
818 if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY)
819 warnx("%s", archive_error_string(a));
820 if (r == ARCHIVE_RETRY)
822 name = archive_entry_pathname(entry);
823 size = archive_entry_size(entry);
826 if ((buff = malloc(size)) == NULL) {
827 warn("malloc failed");
830 if (archive_read_data(a, buff, size) != (ssize_t)size) {
831 warnx("%s", archive_error_string(a));
837 * Note that when processing arsym via libarchive, there is
838 * no way to tell which member a certain symbol belongs to,
839 * since we can not just "lseek" to a member offset and read
842 if (!strcmp(name, "/") && ed->flags & PRINT_ARSYM) {
849 arsym = calloc(cnt, sizeof(*arsym));
851 err(EXIT_FAILURE, "calloc failed");
852 b += sizeof(uint32_t);
853 for (i = 0; i < cnt; i++) {
854 arsym[i].off = be32dec(b);
855 b += sizeof(uint32_t);
857 for (i = 0; i < cnt; i++) {
858 arsym[i].sym_name = b;
861 if (ed->flags & SOLARIS_FMT) {
862 PRT("\nSymbol Table: (archive)\n");
863 PRT(" index offset symbol\n");
865 PRT("\nsymbol table (archive):\n");
866 for (i = 0; i < cnt; i++) {
867 if (ed->flags & SOLARIS_FMT) {
868 snprintf(idx, sizeof(idx), "[%d]", i);
871 (uintmax_t)arsym[i].off);
872 PRT("%s\n", arsym[i].sym_name);
874 PRT("\nentry: %d\n", i);
875 PRT("\toffset: %#jx\n",
876 (uintmax_t)arsym[i].off);
877 PRT("\tsymbol: %s\n",
883 /* No need to continue if we only dump ARSYM. */
884 if (ed->flags & ONLY_ARSYM) {
885 AC(archive_read_close(a));
886 AC(archive_read_free(a));
891 if ((ed->elf = elf_memory(buff, size)) == NULL) {
892 warnx("elf_memroy() failed: %s",
897 /* Skip non-ELF member. */
898 if (elf_kind(ed->elf) == ELF_K_ELF) {
899 printf("\n%s(%s):\n", ed->archive, name);
905 AC(archive_read_close(a));
906 AC(archive_read_free(a));
909 #else /* USE_LIBARCHIVE_AR */
912 * Dump an ar(1) archive.
915 elf_print_ar(struct elfdump *ed, int fd)
926 if (ed->flags & PRINT_ARSYM) {
928 if ((arsym = elf_getarsym(ed->ar, &cnt)) == NULL) {
929 warnx("elf_getarsym failed: %s", elf_errmsg(-1));
934 if (ed->flags & SOLARIS_FMT) {
935 PRT("\nSymbol Table: (archive)\n");
936 PRT(" index offset member name and symbol\n");
938 PRT("\nsymbol table (archive):\n");
939 for (i = 0; i < cnt - 1; i++) {
940 if (elf_rand(ed->ar, arsym[i].as_off) !=
942 warnx("elf_rand failed: %s", elf_errmsg(-1));
945 if ((e = elf_begin(fd, ELF_C_READ, ed->ar)) == NULL) {
946 warnx("elf_begin failed: %s", elf_errmsg(-1));
949 if ((arh = elf_getarhdr(e)) == NULL) {
950 warnx("elf_getarhdr failed: %s",
954 if (ed->flags & SOLARIS_FMT) {
955 snprintf(idx, sizeof(idx), "[%zu]", i);
958 (uintmax_t)arsym[i].as_off);
959 PRT("(%s):%s\n", arh->ar_name,
962 PRT("\nentry: %zu\n", i);
963 PRT("\toffset: %#jx\n",
964 (uintmax_t)arsym[i].as_off);
965 PRT("\tmember: %s\n", arh->ar_name);
966 PRT("\tsymbol: %s\n", arsym[i].as_name);
971 /* No need to continue if we only dump ARSYM. */
972 if (ed->flags & ONLY_ARSYM)
978 /* Rewind the archive. */
979 if (elf_rand(ed->ar, SARMAG) != SARMAG) {
980 warnx("elf_rand failed: %s", elf_errmsg(-1));
984 /* Dump each member of the archive. */
986 while ((ed->elf = elf_begin(fd, cmd, ed->ar)) != NULL) {
987 /* Skip non-ELF member. */
988 if (elf_kind(ed->elf) == ELF_K_ELF) {
989 if ((arh = elf_getarhdr(ed->elf)) == NULL) {
990 warnx("elf_getarhdr failed: %s",
994 printf("\n%s(%s):\n", ed->archive, arh->ar_name);
997 cmd = elf_next(ed->elf);
1002 #endif /* USE_LIBARCHIVE_AR */
1005 * Dump an object. (ELF object or ar(1) archive)
1008 elf_print_object(struct elfdump *ed)
1012 if ((fd = open(ed->filename, O_RDONLY)) == -1) {
1013 warn("open %s failed", ed->filename);
1017 #ifdef USE_LIBARCHIVE_AR
1018 if (ac_detect_ar(fd)) {
1019 ed->archive = ed->filename;
1020 ac_print_ar(ed, fd);
1023 #endif /* USE_LIBARCHIVE_AR */
1025 if ((ed->elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
1026 warnx("elf_begin() failed: %s", elf_errmsg(-1));
1030 switch (elf_kind(ed->elf)) {
1032 warnx("Not an ELF file.");
1035 if (ed->flags & PRINT_FILENAME)
1036 printf("\n%s:\n", ed->filename);
1040 #ifndef USE_LIBARCHIVE_AR
1041 ed->archive = ed->filename;
1042 elf_print_ar(ed, fd);
1046 warnx("Internal: libelf returned unknown elf kind.");
1054 * Dump an ELF object.
1057 elf_print_elf(struct elfdump *ed)
1060 if (gelf_getehdr(ed->elf, &ed->ehdr) == NULL) {
1061 warnx("gelf_getehdr failed: %s", elf_errmsg(-1));
1064 if ((ed->ec = gelf_getclass(ed->elf)) == ELFCLASSNONE) {
1065 warnx("gelf_getclass failed: %s", elf_errmsg(-1));
1069 if (ed->options & (ED_SHDR | ED_DYN | ED_REL | ED_GOT | ED_SYMTAB |
1070 ED_SYMVER | ED_NOTE | ED_HASH))
1073 if (ed->options & ED_EHDR)
1075 if (ed->options & ED_PHDR)
1077 if (ed->options & ED_INTERP)
1078 elf_print_interp(ed);
1079 if (ed->options & ED_SHDR)
1081 if (ed->options & ED_DYN)
1082 elf_print_dynamic(ed);
1083 if (ed->options & ED_REL)
1084 elf_print_reloc(ed);
1085 if (ed->options & ED_GOT)
1087 if (ed->options & ED_SYMTAB)
1088 elf_print_symtabs(ed);
1089 if (ed->options & ED_SYMVER)
1090 elf_print_symver(ed);
1091 if (ed->options & ED_NOTE)
1093 if (ed->options & ED_HASH)
1095 if (ed->options & ED_CHECKSUM)
1096 elf_print_checksum(ed);
1098 unload_sections(ed);
1102 * Read the section headers from ELF object and store them in the
1106 load_sections(struct elfdump *ed)
1112 size_t shstrndx, ndx;
1115 assert(ed->sl == NULL);
1117 if (!elf_getshnum(ed->elf, &ed->shnum)) {
1118 warnx("elf_getshnum failed: %s", elf_errmsg(-1));
1123 if ((ed->sl = calloc(ed->shnum, sizeof(*ed->sl))) == NULL)
1124 err(EXIT_FAILURE, "calloc failed");
1125 if (!elf_getshstrndx(ed->elf, &shstrndx)) {
1126 warnx("elf_getshstrndx failed: %s", elf_errmsg(-1));
1129 if ((scn = elf_getscn(ed->elf, 0)) == NULL) {
1130 warnx("elf_getscn failed: %s", elf_errmsg(-1));
1135 if (gelf_getshdr(scn, &sh) == NULL) {
1136 warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
1140 if ((name = elf_strptr(ed->elf, shstrndx, sh.sh_name)) == NULL) {
1144 if ((ndx = elf_ndxscn(scn)) == SHN_UNDEF)
1145 if ((elferr = elf_errno()) != 0) {
1146 warnx("elf_ndxscn failed: %s",
1147 elf_errmsg(elferr));
1150 if (ndx >= ed->shnum) {
1151 warnx("section index of '%s' out of range", name);
1157 s->off = sh.sh_offset;
1159 s->entsize = sh.sh_entsize;
1160 s->align = sh.sh_addralign;
1161 s->type = sh.sh_type;
1162 s->flags = sh.sh_flags;
1163 s->addr = sh.sh_addr;
1164 s->link = sh.sh_link;
1165 s->info = sh.sh_info;
1166 } while ((scn = elf_nextscn(ed->elf, scn)) != NULL);
1167 elferr = elf_errno();
1169 warnx("elf_nextscn failed: %s", elf_errmsg(elferr));
1173 * Release section related resources.
1176 unload_sections(struct elfdump *ed)
1178 if (ed->sl != NULL) {
1185 * Add a name to the '-N' name list.
1188 add_name(struct elfdump *ed, const char *name)
1190 struct spec_name *sn;
1192 if (find_name(ed, name))
1194 if ((sn = malloc(sizeof(*sn))) == NULL) {
1195 warn("malloc failed");
1199 STAILQ_INSERT_TAIL(&ed->snl, sn, sn_list);
1203 * Lookup a name in the '-N' name list.
1205 static struct spec_name *
1206 find_name(struct elfdump *ed, const char *name)
1208 struct spec_name *sn;
1210 STAILQ_FOREACH(sn, &ed->snl, sn_list) {
1211 if (!strcmp(sn->name, name))
1219 * Retrieve the name of a symbol using the section index of the symbol
1220 * table and the index of the symbol within that table.
1223 get_symbol_name(struct elfdump *ed, uint32_t symtab, int i)
1225 static char sname[64];
1232 if (symtab >= ed->shnum)
1234 s = &ed->sl[symtab];
1235 if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM)
1238 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
1239 elferr = elf_errno();
1241 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
1244 if (gelf_getsym(data, i, &sym) != &sym)
1246 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION) {
1247 if (sym.st_shndx < ed->shnum) {
1248 snprintf(sname, sizeof(sname), "%s (section)",
1249 ed->sl[sym.st_shndx].name);
1254 if ((name = elf_strptr(ed->elf, s->link, sym.st_name)) == NULL)
1261 * Retrieve a string using string table section index and the string offset.
1264 get_string(struct elfdump *ed, int strtab, size_t off)
1268 if ((name = elf_strptr(ed->elf, strtab, off)) == NULL)
1275 * Dump the ELF Executable Header.
1278 elf_print_ehdr(struct elfdump *ed)
1281 if (!STAILQ_EMPTY(&ed->snl))
1284 if (ed->flags & SOLARIS_FMT) {
1285 PRT("\nELF Header\n");
1286 PRT(" ei_magic: { %#x, %c, %c, %c }\n",
1287 ed->ehdr.e_ident[0], ed->ehdr.e_ident[1],
1288 ed->ehdr.e_ident[2], ed->ehdr.e_ident[3]);
1289 PRT(" ei_class: %-18s",
1290 elf_class_str(ed->ehdr.e_ident[EI_CLASS]));
1291 PRT(" ei_data: %s\n",
1292 elf_data_str(ed->ehdr.e_ident[EI_DATA]));
1293 PRT(" e_machine: %-18s", e_machines(ed->ehdr.e_machine));
1294 PRT(" e_version: %s\n",
1295 elf_version_str(ed->ehdr.e_version));
1296 PRT(" e_type: %s\n", elf_type_str(ed->ehdr.e_type));
1297 PRT(" e_flags: %18d\n", ed->ehdr.e_flags);
1298 PRT(" e_entry: %#18jx", (uintmax_t)ed->ehdr.e_entry);
1299 PRT(" e_ehsize: %6d", ed->ehdr.e_ehsize);
1300 PRT(" e_shstrndx:%5d\n", ed->ehdr.e_shstrndx);
1301 PRT(" e_shoff: %#18jx", (uintmax_t)ed->ehdr.e_shoff);
1302 PRT(" e_shentsize: %3d", ed->ehdr.e_shentsize);
1303 PRT(" e_shnum: %5d\n", ed->ehdr.e_shnum);
1304 PRT(" e_phoff: %#18jx", (uintmax_t)ed->ehdr.e_phoff);
1305 PRT(" e_phentsize: %3d", ed->ehdr.e_phentsize);
1306 PRT(" e_phnum: %5d\n", ed->ehdr.e_phnum);
1308 PRT("\nelf header:\n");
1310 PRT("\te_ident: %s %s %s\n",
1311 elf_class_str(ed->ehdr.e_ident[EI_CLASS]),
1312 elf_data_str(ed->ehdr.e_ident[EI_DATA]),
1313 ei_abis[ed->ehdr.e_ident[EI_OSABI]]);
1314 PRT("\te_type: %s\n", elf_type_str(ed->ehdr.e_type));
1315 PRT("\te_machine: %s\n", e_machines(ed->ehdr.e_machine));
1316 PRT("\te_version: %s\n", elf_version_str(ed->ehdr.e_version));
1317 PRT("\te_entry: %#jx\n", (uintmax_t)ed->ehdr.e_entry);
1318 PRT("\te_phoff: %ju\n", (uintmax_t)ed->ehdr.e_phoff);
1319 PRT("\te_shoff: %ju\n", (uintmax_t) ed->ehdr.e_shoff);
1320 PRT("\te_flags: %u\n", ed->ehdr.e_flags);
1321 PRT("\te_ehsize: %u\n", ed->ehdr.e_ehsize);
1322 PRT("\te_phentsize: %u\n", ed->ehdr.e_phentsize);
1323 PRT("\te_phnum: %u\n", ed->ehdr.e_phnum);
1324 PRT("\te_shentsize: %u\n", ed->ehdr.e_shentsize);
1325 PRT("\te_shnum: %u\n", ed->ehdr.e_shnum);
1326 PRT("\te_shstrndx: %u\n", ed->ehdr.e_shstrndx);
1331 * Dump the ELF Program Header Table.
1334 elf_print_phdr(struct elfdump *ed)
1340 if (elf_getphnum(ed->elf, &phnum) == 0) {
1341 warnx("elf_getphnum failed: %s", elf_errmsg(-1));
1345 for (i = 0; i < phnum; i++) {
1346 if (gelf_getphdr(ed->elf, i, &ph) != &ph) {
1347 warnx("elf_getphdr failed: %s", elf_errmsg(-1));
1350 if (!STAILQ_EMPTY(&ed->snl) &&
1351 find_name(ed, elf_phdr_type_str(ph.p_type)) == NULL)
1353 if (ed->flags & SOLARIS_FMT) {
1354 PRT("\nProgram Header[%zu]:\n", i);
1355 PRT(" p_vaddr: %#-14jx", (uintmax_t)ph.p_vaddr);
1356 PRT(" p_flags: [ %s ]\n",
1357 p_flags[ph.p_flags & 0x7]);
1358 PRT(" p_paddr: %#-14jx", (uintmax_t)ph.p_paddr);
1359 PRT(" p_type: [ %s ]\n",
1360 elf_phdr_type_str(ph.p_type));
1361 PRT(" p_filesz: %#-14jx",
1362 (uintmax_t)ph.p_filesz);
1363 PRT(" p_memsz: %#jx\n", (uintmax_t)ph.p_memsz);
1364 PRT(" p_offset: %#-14jx",
1365 (uintmax_t)ph.p_offset);
1366 PRT(" p_align: %#jx\n", (uintmax_t)ph.p_align);
1369 PRT("\nprogram header:\n");
1373 PRT("entry: %zu\n", i);
1374 PRT("\tp_type: %s\n", elf_phdr_type_str(ph.p_type));
1375 PRT("\tp_offset: %ju\n", (uintmax_t)ph.p_offset);
1376 PRT("\tp_vaddr: %#jx\n", (uintmax_t)ph.p_vaddr);
1377 PRT("\tp_paddr: %#jx\n", (uintmax_t)ph.p_paddr);
1378 PRT("\tp_filesz: %ju\n", (uintmax_t)ph.p_filesz);
1379 PRT("\tp_memsz: %ju\n", (uintmax_t)ph.p_memsz);
1380 PRT("\tp_flags: %s\n", p_flags[ph.p_flags & 0x7]);
1381 PRT("\tp_align: %ju\n", (uintmax_t)ph.p_align);
1387 * Dump the ELF Section Header Table.
1390 elf_print_shdr(struct elfdump *ed)
1395 if (!STAILQ_EMPTY(&ed->snl))
1398 if ((ed->flags & SOLARIS_FMT) == 0)
1399 PRT("\nsection header:\n");
1400 for (i = 0; i < ed->shnum; i++) {
1402 if (ed->flags & SOLARIS_FMT) {
1405 PRT("\nSection Header[%zu]:", i);
1406 PRT(" sh_name: %s\n", s->name);
1407 PRT(" sh_addr: %#-14jx", (uintmax_t)s->addr);
1409 PRT(" sh_flags: [ %s ]\n", sh_flags(s->flags));
1411 PRT(" sh_flags: 0\n");
1412 PRT(" sh_size: %#-14jx", (uintmax_t)s->sz);
1413 PRT(" sh_type: [ %s ]\n",
1414 sh_types(ed->ehdr.e_machine, s->type));
1415 PRT(" sh_offset: %#-14jx", (uintmax_t)s->off);
1416 PRT(" sh_entsize: %#jx\n", (uintmax_t)s->entsize);
1417 PRT(" sh_link: %-14u", s->link);
1418 PRT(" sh_info: %u\n", s->info);
1419 PRT(" sh_addralign: %#jx\n", (uintmax_t)s->align);
1422 PRT("entry: %ju\n", (uintmax_t)i);
1423 PRT("\tsh_name: %s\n", s->name);
1424 PRT("\tsh_type: %s\n",
1425 sh_types(ed->ehdr.e_machine, s->type));
1426 PRT("\tsh_flags: %s\n", sh_flags(s->flags));
1427 PRT("\tsh_addr: %#jx\n", (uintmax_t)s->addr);
1428 PRT("\tsh_offset: %ju\n", (uintmax_t)s->off);
1429 PRT("\tsh_size: %ju\n", (uintmax_t)s->sz);
1430 PRT("\tsh_link: %u\n", s->link);
1431 PRT("\tsh_info: %u\n", s->info);
1432 PRT("\tsh_addralign: %ju\n", (uintmax_t)s->align);
1433 PRT("\tsh_entsize: %ju\n", (uintmax_t)s->entsize);
1439 * Return number of entries in the given section. We'd prefer ent_count be a
1440 * size_t, but libelf APIs already use int for section indices.
1443 get_ent_count(const struct section *s, int *ent_count)
1445 if (s->entsize == 0) {
1446 warnx("section %s has entry size 0", s->name);
1448 } else if (s->sz / s->entsize > INT_MAX) {
1449 warnx("section %s has invalid section count", s->name);
1452 *ent_count = (int)(s->sz / s->entsize);
1457 * Retrieve the content of the corresponding SHT_SUNW_versym section for
1458 * a symbol table section.
1461 get_versym(struct elfdump *ed, int i, uint16_t **vs, int *nvs)
1469 for (j = 0; j < ed->shnum; j++) {
1471 if (s->type == SHT_SUNW_versym && s->link == (uint32_t)i)
1474 if (j >= ed->shnum) {
1479 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
1480 elferr = elf_errno();
1482 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
1488 assert(data->d_size == s->sz);
1489 if (!get_ent_count(s, nvs))
1494 * Dump the symbol table section.
1497 elf_print_symtab(struct elfdump *ed, int i)
1505 int len, j, elferr, nvs;
1508 if (ed->flags & SOLARIS_FMT)
1509 PRT("\nSymbol Table Section: %s\n", s->name);
1511 PRT("\nsymbol table (%s):\n", s->name);
1513 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
1514 elferr = elf_errno();
1516 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
1521 assert(data->d_size == s->sz);
1522 if (!get_ent_count(s, &len))
1524 if (ed->flags & SOLARIS_FMT) {
1525 if (ed->ec == ELFCLASS32)
1526 PRT(" index value ");
1528 PRT(" index value ");
1529 PRT("size type bind oth ver shndx name\n");
1530 get_versym(ed, i, &vs, &nvs);
1531 if (vs != NULL && nvs != len) {
1532 warnx("#symbol not equal to #versym");
1536 for (j = 0; j < len; j++) {
1537 if (gelf_getsym(data, j, &sym) != &sym) {
1538 warnx("gelf_getsym failed: %s", elf_errmsg(-1));
1541 name = get_string(ed, s->link, sym.st_name);
1542 if (ed->flags & SOLARIS_FMT) {
1543 snprintf(idx, sizeof(idx), "[%d]", j);
1544 if (ed->ec == ELFCLASS32)
1548 PRT("0x%8.8jx ", (uintmax_t)sym.st_value);
1549 if (ed->ec == ELFCLASS32)
1550 PRT("0x%8.8jx ", (uintmax_t)sym.st_size);
1552 PRT("0x%12.12jx ", (uintmax_t)sym.st_size);
1553 PRT("%s ", st_type_S(GELF_ST_TYPE(sym.st_info)));
1554 PRT("%s ", st_bindings_S(GELF_ST_BIND(sym.st_info)));
1555 PRT("%c ", st_others[sym.st_other]);
1556 PRT("%3u ", (vs == NULL ? 0 : vs[j]));
1557 PRT("%-11.11s ", sh_name(ed, sym.st_shndx));
1560 PRT("\nentry: %d\n", j);
1561 PRT("\tst_name: %s\n", name);
1562 PRT("\tst_value: %#jx\n", (uintmax_t)sym.st_value);
1563 PRT("\tst_size: %ju\n", (uintmax_t)sym.st_size);
1564 PRT("\tst_info: %s %s\n",
1565 st_type(ed->ehdr.e_machine,
1566 GELF_ST_TYPE(sym.st_info)),
1567 st_bindings(GELF_ST_BIND(sym.st_info)));
1568 PRT("\tst_shndx: %ju\n", (uintmax_t)sym.st_shndx);
1574 * Dump the symbol tables. (.dynsym and .symtab)
1577 elf_print_symtabs(struct elfdump *ed)
1581 for (i = 0; i < ed->shnum; i++)
1582 if ((ed->sl[i].type == SHT_SYMTAB ||
1583 ed->sl[i].type == SHT_DYNSYM) &&
1584 (STAILQ_EMPTY(&ed->snl) || find_name(ed, ed->sl[i].name)))
1585 elf_print_symtab(ed, i);
1589 * Dump the content of .dynamic section.
1592 elf_print_dynamic(struct elfdump *ed)
1602 for (i = 0; (size_t)i < ed->shnum; i++) {
1604 if (s->type == SHT_DYNAMIC &&
1605 (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name)))
1608 if ((size_t)i >= ed->shnum)
1611 if (ed->flags & SOLARIS_FMT) {
1612 PRT("Dynamic Section: %s\n", s->name);
1613 PRT(" index tag value\n");
1615 PRT("\ndynamic:\n");
1617 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
1618 elferr = elf_errno();
1620 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
1623 assert(data->d_size == s->sz);
1624 if (!get_ent_count(s, &len))
1626 for (i = 0; i < len; i++) {
1627 if (gelf_getdyn(data, i, &dyn) != &dyn) {
1628 warnx("gelf_getdyn failed: %s", elf_errmsg(-1));
1632 if (ed->flags & SOLARIS_FMT) {
1633 snprintf(idx, sizeof(idx), "[%d]", i);
1634 PRT("%10s %-16s ", idx, d_tags(dyn.d_tag));
1637 PRT("entry: %d\n", i);
1638 PRT("\td_tag: %s\n", d_tags(dyn.d_tag));
1645 if ((name = elf_strptr(ed->elf, s->link,
1646 dyn.d_un.d_val)) == NULL)
1648 if (ed->flags & SOLARIS_FMT)
1649 PRT("%#-16jx %s\n", (uintmax_t)dyn.d_un.d_val,
1652 PRT("\td_val: %s\n", name);
1669 if (ed->flags & SOLARIS_FMT)
1670 PRT("%#jx\n", (uintmax_t)dyn.d_un.d_val);
1672 PRT("\td_val: %ju\n",
1673 (uintmax_t)dyn.d_un.d_val);
1685 if (ed->flags & SOLARIS_FMT)
1686 PRT("%#jx\n", (uintmax_t)dyn.d_un.d_ptr);
1688 PRT("\td_ptr: %#jx\n",
1689 (uintmax_t)dyn.d_un.d_ptr);
1695 if (ed->flags & SOLARIS_FMT)
1703 * Dump a .rel/.rela section entry.
1706 elf_print_rel_entry(struct elfdump *ed, struct section *s, int j,
1707 struct rel_entry *r)
1710 if (ed->flags & SOLARIS_FMT) {
1711 PRT(" %-23s ", elftc_reloc_type_str(ed->ehdr.e_machine,
1712 GELF_R_TYPE(r->u_r.rel.r_info)));
1713 PRT("%#12jx ", (uintmax_t)r->u_r.rel.r_offset);
1714 if (r->type == SHT_RELA)
1715 PRT("%10jd ", (intmax_t)r->u_r.rela.r_addend);
1718 PRT("%-14s ", s->name);
1719 PRT("%s\n", r->symn);
1722 PRT("entry: %d\n", j);
1723 PRT("\tr_offset: %#jx\n", (uintmax_t)r->u_r.rel.r_offset);
1724 if (ed->ec == ELFCLASS32)
1725 PRT("\tr_info: %#jx\n", (uintmax_t)
1726 ELF32_R_INFO(ELF64_R_SYM(r->u_r.rel.r_info),
1727 ELF64_R_TYPE(r->u_r.rel.r_info)));
1729 PRT("\tr_info: %#jx\n", (uintmax_t)r->u_r.rel.r_info);
1730 if (r->type == SHT_RELA)
1731 PRT("\tr_addend: %jd\n",
1732 (intmax_t)r->u_r.rela.r_addend);
1737 * Dump a relocation section of type SHT_RELA.
1740 elf_print_rela(struct elfdump *ed, struct section *s, Elf_Data *data)
1745 if (ed->flags & SOLARIS_FMT) {
1746 PRT("\nRelocation Section: %s\n", s->name);
1748 "addend section with respect to\n");
1750 PRT("\nrelocation with addend (%s):\n", s->name);
1752 assert(data->d_size == s->sz);
1753 if (!get_ent_count(s, &len))
1755 for (j = 0; j < len; j++) {
1756 if (gelf_getrela(data, j, &r.u_r.rela) != &r.u_r.rela) {
1757 warnx("gelf_getrela failed: %s",
1761 r.symn = get_symbol_name(ed, s->link,
1762 GELF_R_SYM(r.u_r.rela.r_info));
1763 elf_print_rel_entry(ed, s, j, &r);
1768 * Dump a relocation section of type SHT_REL.
1771 elf_print_rel(struct elfdump *ed, struct section *s, Elf_Data *data)
1776 if (ed->flags & SOLARIS_FMT) {
1777 PRT("\nRelocation Section: %s\n", s->name);
1779 "section with respect to\n");
1781 PRT("\nrelocation (%s):\n", s->name);
1783 assert(data->d_size == s->sz);
1784 if (!get_ent_count(s, &len))
1786 for (j = 0; j < len; j++) {
1787 if (gelf_getrel(data, j, &r.u_r.rel) != &r.u_r.rel) {
1788 warnx("gelf_getrel failed: %s", elf_errmsg(-1));
1791 r.symn = get_symbol_name(ed, s->link,
1792 GELF_R_SYM(r.u_r.rel.r_info));
1793 elf_print_rel_entry(ed, s, j, &r);
1798 * Dump relocation sections.
1801 elf_print_reloc(struct elfdump *ed)
1808 for (i = 0; i < ed->shnum; i++) {
1810 if ((s->type == SHT_REL || s->type == SHT_RELA) &&
1811 (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name))) {
1813 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
1814 elferr = elf_errno();
1816 warnx("elf_getdata failed: %s",
1817 elf_errmsg(elferr));
1820 if (s->type == SHT_REL)
1821 elf_print_rel(ed, s, data);
1823 elf_print_rela(ed, s, data);
1829 * Dump the content of PT_INTERP segment.
1832 elf_print_interp(struct elfdump *ed)
1836 size_t filesize, i, phnum;
1838 if (!STAILQ_EMPTY(&ed->snl) && find_name(ed, "PT_INTERP") == NULL)
1841 if ((s = elf_rawfile(ed->elf, &filesize)) == NULL) {
1842 warnx("elf_rawfile failed: %s", elf_errmsg(-1));
1845 if (!elf_getphnum(ed->elf, &phnum)) {
1846 warnx("elf_getphnum failed: %s", elf_errmsg(-1));
1849 for (i = 0; i < phnum; i++) {
1850 if (gelf_getphdr(ed->elf, i, &phdr) != &phdr) {
1851 warnx("elf_getphdr failed: %s", elf_errmsg(-1));
1854 if (phdr.p_type == PT_INTERP) {
1855 if (phdr.p_offset >= filesize) {
1856 warnx("invalid phdr offset");
1860 PRT("\t%s\n", s + phdr.p_offset);
1866 * Search the relocation sections for entries referring to the .got section.
1869 find_gotrel(struct elfdump *ed, struct section *gs, struct rel_entry *got)
1875 int elferr, j, k, len;
1877 for(i = 0; i < ed->shnum; i++) {
1879 if (s->type != SHT_REL && s->type != SHT_RELA)
1882 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
1883 elferr = elf_errno();
1885 warnx("elf_getdata failed: %s",
1886 elf_errmsg(elferr));
1889 memset(&r, 0, sizeof(struct rel_entry));
1891 assert(data->d_size == s->sz);
1892 if (!get_ent_count(s, &len))
1894 for (j = 0; j < len; j++) {
1895 if (s->type == SHT_REL) {
1896 if (gelf_getrel(data, j, &r.u_r.rel) !=
1898 warnx("gelf_getrel failed: %s",
1903 if (gelf_getrela(data, j, &r.u_r.rela) !=
1905 warnx("gelf_getrel failed: %s",
1910 if (r.u_r.rel.r_offset >= gs->addr &&
1911 r.u_r.rel.r_offset < gs->addr + gs->sz) {
1912 r.symn = get_symbol_name(ed, s->link,
1913 GELF_R_SYM(r.u_r.rel.r_info));
1914 k = (r.u_r.rel.r_offset - gs->addr) /
1916 memcpy(&got[k], &r, sizeof(struct rel_entry));
1923 elf_print_got_section(struct elfdump *ed, struct section *s)
1925 struct rel_entry *got;
1926 Elf_Data *data, dst;
1929 if (s->entsize == 0) {
1930 /* XXX IA64 GOT section generated by gcc has entry size 0. */
1932 s->entsize = s->align;
1937 if (!get_ent_count(s, &len))
1939 if (ed->flags & SOLARIS_FMT)
1940 PRT("\nGlobal Offset Table Section: %s (%d entries)\n",
1943 PRT("\nglobal offset table: %s\n", s->name);
1945 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
1946 elferr = elf_errno();
1948 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
1953 * GOT section has section type SHT_PROGBITS, thus libelf treats it as
1954 * byte stream and will not perform any translation on it. As a result,
1955 * an exlicit call to gelf_xlatetom is needed here. Depends on arch,
1956 * GOT section should be translated to either WORD or XWORD.
1958 if (ed->ec == ELFCLASS32)
1959 data->d_type = ELF_T_WORD;
1961 data->d_type = ELF_T_XWORD;
1962 memcpy(&dst, data, sizeof(Elf_Data));
1963 if (gelf_xlatetom(ed->elf, &dst, data, ed->ehdr.e_ident[EI_DATA]) !=
1965 warnx("gelf_xlatetom failed: %s", elf_errmsg(-1));
1968 assert(dst.d_size == s->sz);
1969 if (ed->flags & SOLARIS_FMT) {
1971 * In verbose/Solaris mode, we search the relocation sections
1972 * and try to find the corresponding reloc entry for each GOT
1975 if ((got = calloc(len, sizeof(struct rel_entry))) == NULL)
1976 err(EXIT_FAILURE, "calloc failed");
1977 find_gotrel(ed, s, got);
1978 if (ed->ec == ELFCLASS32) {
1979 PRT(" ndx addr value reloc ");
1980 PRT("addend symbol\n");
1982 PRT(" ndx addr value ");
1983 PRT("reloc addend symbol\n");
1985 for(i = 0; i < len; i++) {
1987 if (ed->ec == ELFCLASS32) {
1989 (uintmax_t) (s->addr + i * s->entsize));
1990 PRT("%-8.8x ", *((uint32_t *)dst.d_buf + i));
1993 (uintmax_t) (s->addr + i * s->entsize));
1995 (uintmax_t) *((uint64_t *)dst.d_buf + i));
1997 PRT("%-18s ", elftc_reloc_type_str(ed->ehdr.e_machine,
1998 GELF_R_TYPE(got[i].u_r.rel.r_info)));
1999 if (ed->ec == ELFCLASS32)
2001 (intmax_t)got[i].u_r.rela.r_addend);
2004 (intmax_t)got[i].u_r.rela.r_addend);
2005 if (got[i].symn == NULL)
2007 PRT("%s\n", got[i].symn);
2011 for(i = 0; i < len; i++) {
2012 PRT("\nentry: %d\n", i);
2013 if (ed->ec == ELFCLASS32)
2014 PRT("\t%#x\n", *((uint32_t *)dst.d_buf + i));
2017 (uintmax_t) *((uint64_t *)dst.d_buf + i));
2023 * Dump the content of Global Offset Table section.
2026 elf_print_got(struct elfdump *ed)
2031 if (!STAILQ_EMPTY(&ed->snl))
2035 for (i = 0; i < ed->shnum; i++) {
2037 if (s->name && !strncmp(s->name, ".got", 4) &&
2038 (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name)))
2039 elf_print_got_section(ed, s);
2044 * Dump the content of .note.ABI-tag section.
2047 elf_print_note(struct elfdump *ed)
2061 for (i = 0; (size_t)i < ed->shnum; i++) {
2063 if (s->type == SHT_NOTE && s->name &&
2064 !strcmp(s->name, ".note.ABI-tag") &&
2065 (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name)))
2068 if ((size_t)i >= ed->shnum)
2070 if (ed->flags & SOLARIS_FMT)
2071 PRT("\nNote Section: %s\n", s->name);
2073 PRT("\nnote (%s):\n", s->name);
2075 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
2076 elferr = elf_errno();
2078 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
2082 count = data->d_size;
2083 while (count > sizeof(Elf_Note)) {
2084 en = (Elf_Note *) (uintptr_t) src;
2085 namesz = en->n_namesz;
2086 descsz = en->n_descsz;
2087 src += sizeof(Elf_Note);
2088 count -= sizeof(Elf_Note);
2089 if (roundup2(namesz, 4) + roundup2(descsz, 4) > count) {
2090 warnx("truncated note section");
2093 if (ed->flags & SOLARIS_FMT) {
2094 PRT("\n type %#x\n", en->n_type);
2095 PRT(" namesz %#x:\n", en->n_namesz);
2099 src += roundup2(namesz, 4);
2100 count -= roundup2(namesz, 4);
2103 * Note that we dump the whole desc part if we're in
2104 * "Solaris mode", while in the normal mode, we only look
2105 * at the first 4 bytes (a 32bit word) of the desc, i.e,
2106 * we assume that it's always a FreeBSD version number.
2108 if (ed->flags & SOLARIS_FMT) {
2109 PRT(" descsz %#x:", en->n_descsz);
2110 for (i = 0; (uint32_t)i < descsz; i++) {
2111 if ((i & 0xF) == 0) {
2112 snprintf(idx, sizeof(idx), "desc[%d]",
2114 PRT("\n %-9s", idx);
2115 } else if ((i & 0x3) == 0)
2117 PRT(" %2.2x", src[i]);
2121 if (ed->ehdr.e_ident[EI_DATA] == ELFDATA2MSB)
2122 desc = be32dec(src);
2124 desc = le32dec(src);
2127 src += roundup2(descsz, 4);
2128 count -= roundup2(descsz, 4);
2133 * Dump a hash table.
2136 elf_print_svr4_hash(struct elfdump *ed, struct section *s)
2140 uint32_t *bucket, *chain;
2141 uint32_t nbucket, nchain;
2142 uint32_t *bl, *c, maxl, total;
2147 if (ed->flags & SOLARIS_FMT)
2148 PRT("\nHash Section: %s\n", s->name);
2150 PRT("\nhash table (%s):\n", s->name);
2152 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
2153 elferr = elf_errno();
2155 warnx("elf_getdata failed: %s",
2156 elf_errmsg(elferr));
2159 if (data->d_size < 2 * sizeof(uint32_t)) {
2160 warnx(".hash section too small");
2166 if (nbucket <= 0 || nchain <= 0) {
2167 warnx("Malformed .hash section");
2171 ((uint64_t)nbucket + (uint64_t)nchain + 2) * sizeof(uint32_t)) {
2172 warnx("Malformed .hash section");
2176 chain = &buf[2 + nbucket];
2178 if (ed->flags & SOLARIS_FMT) {
2180 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
2181 err(EXIT_FAILURE, "calloc failed");
2182 for (i = 0; i < nbucket; i++)
2183 for (j = bucket[i]; j > 0 && j < nchain; j = chain[j])
2186 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
2187 err(EXIT_FAILURE, "calloc failed");
2188 for (i = 0; i < nbucket; i++)
2190 PRT(" bucket symndx name\n");
2191 for (i = 0; i < nbucket; i++) {
2193 for (j = bucket[i]; j > 0 && j < nchain; j = chain[j]) {
2199 snprintf(idx, sizeof(idx), "[%d]", j);
2201 PRT("%s\n", get_symbol_name(ed, s->link, j));
2206 for (i = 0; i <= maxl; i++) {
2208 PRT("%10u buckets contain %8d symbols\n", c[i], i);
2210 PRT("%10u buckets %8u symbols (globals)\n", nbucket,
2213 PRT("\nnbucket: %u\n", nbucket);
2214 PRT("nchain: %u\n\n", nchain);
2215 for (i = 0; i < nbucket; i++)
2216 PRT("bucket[%d]:\n\t%u\n\n", i, bucket[i]);
2217 for (i = 0; i < nchain; i++)
2218 PRT("chain[%d]:\n\t%u\n\n", i, chain[i]);
2223 * Dump a 64bit hash table.
2226 elf_print_svr4_hash64(struct elfdump *ed, struct section *s)
2228 Elf_Data *data, dst;
2230 uint64_t *bucket, *chain;
2231 uint64_t nbucket, nchain;
2232 uint64_t *bl, *c, j, maxl, total;
2237 if (ed->flags & SOLARIS_FMT)
2238 PRT("\nHash Section: %s\n", s->name);
2240 PRT("\nhash table (%s):\n", s->name);
2243 * ALPHA uses 64-bit hash entries. Since libelf assumes that
2244 * .hash section contains only 32-bit entry, an explicit
2245 * gelf_xlatetom is needed here.
2248 if ((data = elf_rawdata(s->scn, NULL)) == NULL) {
2249 elferr = elf_errno();
2251 warnx("elf_rawdata failed: %s",
2252 elf_errmsg(elferr));
2255 data->d_type = ELF_T_XWORD;
2256 memcpy(&dst, data, sizeof(Elf_Data));
2257 if (gelf_xlatetom(ed->elf, &dst, data,
2258 ed->ehdr.e_ident[EI_DATA]) != &dst) {
2259 warnx("gelf_xlatetom failed: %s", elf_errmsg(-1));
2262 if (dst.d_size < 2 * sizeof(uint64_t)) {
2263 warnx(".hash section too small");
2269 if (nbucket <= 0 || nchain <= 0) {
2270 warnx("Malformed .hash section");
2273 if (dst.d_size != (nbucket + nchain + 2) * sizeof(uint64_t)) {
2274 warnx("Malformed .hash section");
2278 chain = &buf[2 + nbucket];
2280 if (ed->flags & SOLARIS_FMT) {
2282 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
2283 err(EXIT_FAILURE, "calloc failed");
2284 for (i = 0; i < nbucket; i++)
2285 for (j = bucket[i]; j > 0 && j < nchain; j = chain[j])
2288 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
2289 err(EXIT_FAILURE, "calloc failed");
2290 for (i = 0; i < nbucket; i++)
2292 PRT(" bucket symndx name\n");
2293 for (i = 0; i < nbucket; i++) {
2295 for (j = bucket[i]; j > 0 && j < nchain; j = chain[j]) {
2301 snprintf(idx, sizeof(idx), "[%zu]", (size_t)j);
2303 PRT("%s\n", get_symbol_name(ed, s->link, j));
2308 for (i = 0; i <= maxl; i++) {
2310 PRT("%10ju buckets contain %8zu symbols\n",
2311 (uintmax_t)c[i], i);
2313 PRT("%10ju buckets %8ju symbols (globals)\n",
2314 (uintmax_t)nbucket, (uintmax_t)total);
2316 PRT("\nnbucket: %ju\n", (uintmax_t)nbucket);
2317 PRT("nchain: %ju\n\n", (uintmax_t)nchain);
2318 for (i = 0; i < nbucket; i++)
2319 PRT("bucket[%zu]:\n\t%ju\n\n", i, (uintmax_t)bucket[i]);
2320 for (i = 0; i < nchain; i++)
2321 PRT("chain[%zu]:\n\t%ju\n\n", i, (uintmax_t)chain[i]);
2327 * Dump a GNU hash table.
2330 elf_print_gnu_hash(struct elfdump *ed, struct section *s)
2335 uint32_t *bucket, *chain;
2336 uint32_t nbucket, nchain, symndx, maskwords, shift2;
2337 uint32_t *bl, *c, maxl, total;
2339 int first, elferr, dynsymcount;
2342 if (ed->flags & SOLARIS_FMT)
2343 PRT("\nGNU Hash Section: %s\n", s->name);
2345 PRT("\ngnu hash table (%s):\n", s->name);
2347 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
2348 elferr = elf_errno();
2350 warnx("elf_getdata failed: %s",
2351 elf_errmsg(elferr));
2354 if (data->d_size < 4 * sizeof(uint32_t)) {
2355 warnx(".gnu.hash section too small");
2364 if (s->link >= ed->shnum) {
2365 warnx("Malformed .gnu.hash section");
2368 ds = &ed->sl[s->link];
2369 if (!get_ent_count(ds, &dynsymcount))
2371 if (symndx >= (uint32_t)dynsymcount) {
2372 warnx("Malformed .gnu.hash section");
2375 nchain = dynsymcount - symndx;
2376 if (data->d_size != 4 * sizeof(uint32_t) + maskwords *
2377 (ed->ec == ELFCLASS32 ? sizeof(uint32_t) : sizeof(uint64_t)) +
2378 ((uint64_t)nbucket + (uint64_t)nchain) * sizeof(uint32_t)) {
2379 warnx("Malformed .gnu.hash section");
2382 bucket = buf + (ed->ec == ELFCLASS32 ? maskwords : maskwords * 2);
2383 chain = bucket + nbucket;
2385 if (ed->flags & SOLARIS_FMT) {
2387 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
2388 err(EXIT_FAILURE, "calloc failed");
2389 for (i = 0; i < nbucket; i++)
2390 for (j = bucket[i]; j > 0 && j - symndx < nchain; j++) {
2393 if (chain[j - symndx] & 1)
2396 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
2397 err(EXIT_FAILURE, "calloc failed");
2398 for (i = 0; i < nbucket; i++)
2400 PRT(" bucket symndx name\n");
2401 for (i = 0; i < nbucket; i++) {
2403 for (j = bucket[i]; j > 0 && j - symndx < nchain; j++) {
2409 snprintf(idx, sizeof(idx), "[%d]", j );
2411 PRT("%s\n", get_symbol_name(ed, s->link, j));
2412 if (chain[j - symndx] & 1)
2418 for (i = 0; i <= maxl; i++) {
2420 PRT("%10u buckets contain %8d symbols\n", c[i], i);
2422 PRT("%10u buckets %8u symbols (globals)\n", nbucket,
2425 PRT("\nnbucket: %u\n", nbucket);
2426 PRT("symndx: %u\n", symndx);
2427 PRT("maskwords: %u\n", maskwords);
2428 PRT("shift2: %u\n", shift2);
2429 PRT("nchain: %u\n\n", nchain);
2430 for (i = 0; i < nbucket; i++)
2431 PRT("bucket[%d]:\n\t%u\n\n", i, bucket[i]);
2432 for (i = 0; i < nchain; i++)
2433 PRT("chain[%d]:\n\t%u\n\n", i, chain[i]);
2441 elf_print_hash(struct elfdump *ed)
2446 for (i = 0; i < ed->shnum; i++) {
2448 if ((s->type == SHT_HASH || s->type == SHT_GNU_HASH) &&
2449 (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name))) {
2450 if (s->type == SHT_GNU_HASH)
2451 elf_print_gnu_hash(ed, s);
2452 else if (ed->ehdr.e_machine == EM_ALPHA &&
2454 elf_print_svr4_hash64(ed, s);
2456 elf_print_svr4_hash(ed, s);
2462 * Dump the content of a Version Definition(SHT_SUNW_Verdef) Section.
2465 elf_print_verdef(struct elfdump *ed, struct section *s)
2472 uint8_t *buf, *end, *buf2;
2473 int i, j, elferr, count;
2475 if (ed->flags & SOLARIS_FMT)
2476 PRT("Version Definition Section: %s\n", s->name);
2478 PRT("\nversion definition section (%s):\n", s->name);
2480 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
2481 elferr = elf_errno();
2483 warnx("elf_getdata failed: %s",
2484 elf_errmsg(elferr));
2488 end = buf + data->d_size;
2490 if (ed->flags & SOLARIS_FMT)
2491 PRT(" index version dependency\n");
2492 while (buf + sizeof(Elf32_Verdef) <= end) {
2493 vd = (Elf32_Verdef *) (uintptr_t) buf;
2494 if (ed->flags & SOLARIS_FMT) {
2495 snprintf(idx, sizeof(idx), "[%d]", vd->vd_ndx);
2498 PRT("\nentry: %d\n", i++);
2499 PRT("\tvd_version: %u\n", vd->vd_version);
2500 PRT("\tvd_flags: %u\n", vd->vd_flags);
2501 PRT("\tvd_ndx: %u\n", vd->vd_ndx);
2502 PRT("\tvd_cnt: %u\n", vd->vd_cnt);
2503 PRT("\tvd_hash: %u\n", vd->vd_hash);
2504 PRT("\tvd_aux: %u\n", vd->vd_aux);
2505 PRT("\tvd_next: %u\n\n", vd->vd_next);
2507 buf2 = buf + vd->vd_aux;
2510 while (buf2 + sizeof(Elf32_Verdaux) <= end && j < vd->vd_cnt) {
2511 vda = (Elf32_Verdaux *) (uintptr_t) buf2;
2512 str = get_string(ed, s->link, vda->vda_name);
2513 if (ed->flags & SOLARIS_FMT) {
2515 PRT("%-26.26s", str);
2516 else if (count == 1)
2517 PRT(" %-20.20s", str);
2519 PRT("\n%40.40s", "");
2523 PRT("\t\tvda: %d\n", j++);
2524 PRT("\t\t\tvda_name: %s\n", str);
2525 PRT("\t\t\tvda_next: %u\n", vda->vda_next);
2527 if (vda->vda_next == 0) {
2528 if (ed->flags & SOLARIS_FMT) {
2529 if (vd->vd_flags & VER_FLG_BASE) {
2531 PRT("%-20.20s", "");
2532 PRT("%s", "[ BASE ]");
2538 if (ed->flags & SOLARIS_FMT)
2540 buf2 += vda->vda_next;
2542 if (vd->vd_next == 0)
2549 * Dump the content of a Version Needed(SHT_SUNW_Verneed) Section.
2552 elf_print_verneed(struct elfdump *ed, struct section *s)
2557 uint8_t *buf, *end, *buf2;
2558 int i, j, elferr, first;
2560 if (ed->flags & SOLARIS_FMT)
2561 PRT("\nVersion Needed Section: %s\n", s->name);
2563 PRT("\nversion need section (%s):\n", s->name);
2565 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
2566 elferr = elf_errno();
2568 warnx("elf_getdata failed: %s",
2569 elf_errmsg(elferr));
2573 end = buf + data->d_size;
2574 if (ed->flags & SOLARIS_FMT)
2575 PRT(" file version\n");
2577 while (buf + sizeof(Elf32_Verneed) <= end) {
2578 vn = (Elf32_Verneed *) (uintptr_t) buf;
2579 if (ed->flags & SOLARIS_FMT)
2581 get_string(ed, s->link, vn->vn_file));
2583 PRT("\nentry: %d\n", i++);
2584 PRT("\tvn_version: %u\n", vn->vn_version);
2585 PRT("\tvn_cnt: %u\n", vn->vn_cnt);
2586 PRT("\tvn_file: %s\n",
2587 get_string(ed, s->link, vn->vn_file));
2588 PRT("\tvn_aux: %u\n", vn->vn_aux);
2589 PRT("\tvn_next: %u\n\n", vn->vn_next);
2591 buf2 = buf + vn->vn_aux;
2594 while (buf2 + sizeof(Elf32_Vernaux) <= end && j < vn->vn_cnt) {
2595 vna = (Elf32_Vernaux *) (uintptr_t) buf2;
2596 if (ed->flags & SOLARIS_FMT) {
2601 PRT("%s\n", get_string(ed, s->link,
2604 PRT("\t\tvna: %d\n", j++);
2605 PRT("\t\t\tvna_hash: %u\n", vna->vna_hash);
2606 PRT("\t\t\tvna_flags: %u\n", vna->vna_flags);
2607 PRT("\t\t\tvna_other: %u\n", vna->vna_other);
2608 PRT("\t\t\tvna_name: %s\n",
2609 get_string(ed, s->link, vna->vna_name));
2610 PRT("\t\t\tvna_next: %u\n", vna->vna_next);
2612 if (vna->vna_next == 0)
2614 buf2 += vna->vna_next;
2616 if (vn->vn_next == 0)
2623 * Dump the symbol-versioning sections.
2626 elf_print_symver(struct elfdump *ed)
2631 for (i = 0; i < ed->shnum; i++) {
2633 if (!STAILQ_EMPTY(&ed->snl) && !find_name(ed, s->name))
2635 if (s->type == SHT_SUNW_verdef)
2636 elf_print_verdef(ed, s);
2637 if (s->type == SHT_SUNW_verneed)
2638 elf_print_verneed(ed, s);
2643 * Dump the ELF checksum. See gelf_checksum(3) for details.
2646 elf_print_checksum(struct elfdump *ed)
2649 if (!STAILQ_EMPTY(&ed->snl))
2652 PRT("\nelf checksum: %#lx\n", gelf_checksum(ed->elf));
2655 #define USAGE_MESSAGE "\
2656 Usage: %s [options] file...\n\
2657 Display information about ELF objects and ar(1) archives.\n\n\
2659 -a Show all information.\n\
2660 -c Show shared headers.\n\
2661 -d Show dynamic symbols.\n\
2662 -e Show the ELF header.\n\
2664 -H | --help Show a usage message and exit.\n\
2665 -h Show hash values.\n\
2666 -i Show the dynamic interpreter.\n\
2667 -k Show the ELF checksum.\n\
2668 -n Show the contents of note sections.\n\
2669 -N NAME Show the section named \"NAME\".\n\
2670 -p Show the program header.\n\
2671 -r Show relocations.\n\
2672 -s Show the symbol table.\n\
2673 -S Use the Solaris elfdump format.\n\
2674 -v Show symbol-versioning information.\n\
2675 -V | --version Print a version identifier and exit.\n\
2676 -w FILE Write output to \"FILE\".\n"
2681 fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());