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 3474 2016-05-17 20:44:53Z emaste $");
55 /* Backwards compatability for older FreeBSD releases. */
63 #if defined(ELFTC_NEED_ELF_NOTE_DEFINITION)
64 #include "native-elf-format.h"
65 #if ELFTC_CLASS == ELFCLASS32
66 typedef Elf32_Nhdr Elf_Note;
68 typedef Elf64_Nhdr Elf_Note;
72 /* elfdump(1) options. */
74 #define ED_EHDR (1<<1)
76 #define ED_HASH (1<<3)
77 #define ED_INTERP (1<<4)
78 #define ED_NOTE (1<<5)
79 #define ED_PHDR (1<<6)
81 #define ED_SHDR (1<<8)
82 #define ED_SYMTAB (1<<9)
83 #define ED_SYMVER (1<<10)
84 #define ED_CHECKSUM (1<<11)
85 #define ED_ALL ((1<<12)-1)
87 /* elfdump(1) run control flags. */
88 #define SOLARIS_FMT (1<<0)
89 #define PRINT_FILENAME (1<<1)
90 #define PRINT_ARSYM (1<<2)
91 #define ONLY_ARSYM (1<<3)
93 /* Convenient print macro. */
94 #define PRT(...) fprintf(ed->out, __VA_ARGS__)
96 /* Internal data structure for sections. */
98 const char *name; /* section name */
99 Elf_Scn *scn; /* section scn */
100 uint64_t off; /* section offset */
101 uint64_t sz; /* section size */
102 uint64_t entsize; /* section entsize */
103 uint64_t align; /* section alignment */
104 uint64_t type; /* section type */
105 uint64_t flags; /* section flags */
106 uint64_t addr; /* section virtual addr */
107 uint32_t link; /* section link ndx */
108 uint32_t info; /* section info ndx */
113 STAILQ_ENTRY(spec_name) sn_list;
116 /* Structure encapsulates the global data for readelf(1). */
118 FILE *out; /* output redirection. */
119 const char *filename; /* current processing file. */
120 const char *archive; /* archive name */
121 int options; /* command line options. */
122 int flags; /* run control flags. */
123 Elf *elf; /* underlying ELF descriptor. */
124 #ifndef USE_LIBARCHIVE_AR
125 Elf *ar; /* ar(1) archive descriptor. */
127 GElf_Ehdr ehdr; /* ELF header. */
128 int ec; /* ELF class. */
129 size_t shnum; /* #sections. */
130 struct section *sl; /* list of sections. */
131 STAILQ_HEAD(, spec_name) snl; /* list of names specified by -N. */
134 /* Relocation entry. */
144 #if defined(ELFTC_NEED_BYTEORDER_EXTENSIONS)
145 static __inline uint32_t
146 be32dec(const void *pp)
148 unsigned char const *p = (unsigned char const *)pp;
150 return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
153 static __inline uint32_t
154 le32dec(const void *pp)
156 unsigned char const *p = (unsigned char const *)pp;
158 return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
162 /* http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#tag_encodings */
166 static char unknown_buf[64];
169 case DT_NULL: return "DT_NULL";
170 case DT_NEEDED: return "DT_NEEDED";
171 case DT_PLTRELSZ: return "DT_PLTRELSZ";
172 case DT_PLTGOT: return "DT_PLTGOT";
173 case DT_HASH: return "DT_HASH";
174 case DT_STRTAB: return "DT_STRTAB";
175 case DT_SYMTAB: return "DT_SYMTAB";
176 case DT_RELA: return "DT_RELA";
177 case DT_RELASZ: return "DT_RELASZ";
178 case DT_RELAENT: return "DT_RELAENT";
179 case DT_STRSZ: return "DT_STRSZ";
180 case DT_SYMENT: return "DT_SYMENT";
181 case DT_INIT: return "DT_INIT";
182 case DT_FINI: return "DT_FINI";
183 case DT_SONAME: return "DT_SONAME";
184 case DT_RPATH: return "DT_RPATH";
185 case DT_SYMBOLIC: return "DT_SYMBOLIC";
186 case DT_REL: return "DT_REL";
187 case DT_RELSZ: return "DT_RELSZ";
188 case DT_RELENT: return "DT_RELENT";
189 case DT_PLTREL: return "DT_PLTREL";
190 case DT_DEBUG: return "DT_DEBUG";
191 case DT_TEXTREL: return "DT_TEXTREL";
192 case DT_JMPREL: return "DT_JMPREL";
193 case DT_BIND_NOW: return "DT_BIND_NOW";
194 case DT_INIT_ARRAY: return "DT_INIT_ARRAY";
195 case DT_FINI_ARRAY: return "DT_FINI_ARRAY";
196 case DT_INIT_ARRAYSZ: return "DT_INIT_ARRAYSZ";
197 case DT_FINI_ARRAYSZ: return "DT_FINI_ARRAYSZ";
198 case DT_RUNPATH: return "DT_RUNPATH";
199 case DT_FLAGS: return "DT_FLAGS";
200 case DT_PREINIT_ARRAY: return "DT_PREINIT_ARRAY"; /* XXX DT_ENCODING */
201 case DT_PREINIT_ARRAYSZ:return "DT_PREINIT_ARRAYSZ";
202 /* 0x6000000D - 0x6ffff000 operating system-specific semantics */
203 case 0x6ffffdf5: return "DT_GNU_PRELINKED";
204 case 0x6ffffdf6: return "DT_GNU_CONFLICTSZ";
205 case 0x6ffffdf7: return "DT_GNU_LIBLISTSZ";
206 case 0x6ffffdf8: return "DT_SUNW_CHECKSUM";
207 case DT_PLTPADSZ: return "DT_PLTPADSZ";
208 case DT_MOVEENT: return "DT_MOVEENT";
209 case DT_MOVESZ: return "DT_MOVESZ";
210 case 0x6ffffdfc: return "DT_FEATURE";
211 case DT_POSFLAG_1: return "DT_POSFLAG_1";
212 case DT_SYMINSZ: return "DT_SYMINSZ";
213 case DT_SYMINENT: return "DT_SYMINENT (DT_VALRNGHI)";
214 case DT_ADDRRNGLO: return "DT_ADDRRNGLO";
215 case DT_GNU_HASH: return "DT_GNU_HASH";
216 case 0x6ffffef8: return "DT_GNU_CONFLICT";
217 case 0x6ffffef9: return "DT_GNU_LIBLIST";
218 case 0x6ffffefa: return "DT_CONFIG";
219 case 0x6ffffefb: return "DT_DEPAUDIT";
220 case 0x6ffffefc: return "DT_AUDIT";
221 case 0x6ffffefd: return "DT_PLTPAD";
222 case 0x6ffffefe: return "DT_MOVETAB";
223 case DT_SYMINFO: return "DT_SYMINFO (DT_ADDRRNGHI)";
224 case DT_RELACOUNT: return "DT_RELACOUNT";
225 case DT_RELCOUNT: return "DT_RELCOUNT";
226 case DT_FLAGS_1: return "DT_FLAGS_1";
227 case DT_VERDEF: return "DT_VERDEF";
228 case DT_VERDEFNUM: return "DT_VERDEFNUM";
229 case DT_VERNEED: return "DT_VERNEED";
230 case DT_VERNEEDNUM: return "DT_VERNEEDNUM";
231 case 0x6ffffff0: return "DT_GNU_VERSYM";
232 /* 0x70000000 - 0x7fffffff processor-specific semantics */
233 case 0x70000000: return "DT_IA_64_PLT_RESERVE";
234 case 0x7ffffffd: return "DT_SUNW_AUXILIARY";
235 case 0x7ffffffe: return "DT_SUNW_USED";
236 case 0x7fffffff: return "DT_SUNW_FILTER";
239 snprintf(unknown_buf, sizeof(unknown_buf),
240 "<unknown: %#llx>", (unsigned long long)tag);
241 return (unknown_buf);
245 e_machines(unsigned int mach)
247 static char machdesc[64];
250 case EM_NONE: return "EM_NONE";
251 case EM_M32: return "EM_M32";
252 case EM_SPARC: return "EM_SPARC";
253 case EM_386: return "EM_386";
254 case EM_68K: return "EM_68K";
255 case EM_88K: return "EM_88K";
256 case EM_IAMCU: return "EM_IAMCU";
257 case EM_860: return "EM_860";
258 case EM_MIPS: return "EM_MIPS";
259 case EM_PPC: return "EM_PPC";
260 case EM_PPC64: return "EM_PPC64";
261 case EM_ARM: return "EM_ARM";
262 case EM_ALPHA: return "EM_ALPHA (legacy)";
263 case EM_SPARCV9:return "EM_SPARCV9";
264 case EM_IA_64: return "EM_IA_64";
265 case EM_X86_64: return "EM_X86_64";
266 case EM_AARCH64:return "EM_AARCH64";
267 case EM_RISCV: return "EM_RISCV";
269 snprintf(machdesc, sizeof(machdesc),
270 "(unknown machine) -- type 0x%x", mach);
275 elf_type_str(unsigned int type)
277 static char s_type[32];
281 case ET_NONE: return "ET_NONE";
282 case ET_REL: return "ET_REL";
283 case ET_EXEC: return "ET_EXEC";
284 case ET_DYN: return "ET_DYN";
285 case ET_CORE: return "ET_CORE";
287 if (type >= ET_LOPROC)
288 snprintf(s_type, sizeof(s_type), "<proc: %#x>", type);
289 else if (type >= ET_LOOS && type <= ET_HIOS)
290 snprintf(s_type, sizeof(s_type), "<os: %#x>", type);
292 snprintf(s_type, sizeof(s_type), "<unknown: %#x", type);
297 elf_version_str(unsigned int ver)
299 static char s_ver[32];
302 case EV_NONE: return "EV_NONE";
303 case EV_CURRENT: return "EV_CURRENT";
305 snprintf(s_ver, sizeof(s_ver), "<unknown: %#x>", ver);
310 elf_class_str(unsigned int class)
312 static char s_class[32];
315 case ELFCLASSNONE: return "ELFCLASSNONE";
316 case ELFCLASS32: return "ELFCLASS32";
317 case ELFCLASS64: return "ELFCLASS64";
319 snprintf(s_class, sizeof(s_class), "<unknown: %#x>", class);
324 elf_data_str(unsigned int data)
326 static char s_data[32];
329 case ELFDATANONE: return "ELFDATANONE";
330 case ELFDATA2LSB: return "ELFDATA2LSB";
331 case ELFDATA2MSB: return "ELFDATA2MSB";
333 snprintf(s_data, sizeof(s_data), "<unknown: %#x>", data);
337 static const char *ei_abis[256] = {
338 "ELFOSABI_NONE", "ELFOSABI_HPUX", "ELFOSABI_NETBSD", "ELFOSABI_LINUX",
339 "ELFOSABI_HURD", "ELFOSABI_86OPEN", "ELFOSABI_SOLARIS", "ELFOSABI_AIX",
340 "ELFOSABI_IRIX", "ELFOSABI_FREEBSD", "ELFOSABI_TRU64",
341 "ELFOSABI_MODESTO", "ELFOSABI_OPENBSD",
342 [17] = "ELFOSABI_CLOUDABI",
343 [255] = "ELFOSABI_STANDALONE"
347 elf_phdr_type_str(unsigned int type)
349 static char s_type[32];
352 case PT_NULL: return "PT_NULL";
353 case PT_LOAD: return "PT_LOAD";
354 case PT_DYNAMIC: return "PT_DYNAMIC";
355 case PT_INTERP: return "PT_INTERP";
356 case PT_NOTE: return "PT_NOTE";
357 case PT_SHLIB: return "PT_SHLIB";
358 case PT_PHDR: return "PT_PHDR";
359 case PT_TLS: return "PT_TLS";
360 case PT_GNU_EH_FRAME: return "PT_GNU_EH_FRAME";
361 case PT_GNU_STACK: return "PT_GNU_STACK";
362 case PT_GNU_RELRO: return "PT_GNU_RELRO";
364 snprintf(s_type, sizeof(s_type), "<unknown: %#x>", type);
368 static const char *p_flags[] = {
369 "", "PF_X", "PF_W", "PF_X|PF_W", "PF_R", "PF_X|PF_R", "PF_W|PF_R",
374 sh_name(struct elfdump *ed, int ndx)
379 case SHN_UNDEF: return "UNDEF";
380 case SHN_ABS: return "ABS";
381 case SHN_COMMON: return "COMMON";
383 if ((uint64_t)ndx < ed->shnum)
384 return (ed->sl[ndx].name);
386 snprintf(num, sizeof(num), "%d", ndx);
392 /* http://www.sco.com/developers/gabi/latest/ch4.sheader.html#sh_type */
394 sh_types(uint64_t mach, uint64_t sht) {
395 static char unknown_buf[64];
397 if (sht < 0x60000000) {
399 case SHT_NULL: return "SHT_NULL";
400 case SHT_PROGBITS: return "SHT_PROGBITS";
401 case SHT_SYMTAB: return "SHT_SYMTAB";
402 case SHT_STRTAB: return "SHT_STRTAB";
403 case SHT_RELA: return "SHT_RELA";
404 case SHT_HASH: return "SHT_HASH";
405 case SHT_DYNAMIC: return "SHT_DYNAMIC";
406 case SHT_NOTE: return "SHT_NOTE";
407 case SHT_NOBITS: return "SHT_NOBITS";
408 case SHT_REL: return "SHT_REL";
409 case SHT_SHLIB: return "SHT_SHLIB";
410 case SHT_DYNSYM: return "SHT_DYNSYM";
411 case SHT_INIT_ARRAY: return "SHT_INIT_ARRAY";
412 case SHT_FINI_ARRAY: return "SHT_FINI_ARRAY";
413 case SHT_PREINIT_ARRAY: return "SHT_PREINIT_ARRAY";
414 case SHT_GROUP: return "SHT_GROUP";
415 case SHT_SYMTAB_SHNDX: return "SHT_SYMTAB_SHNDX";
417 } else if (sht < 0x70000000) {
418 /* 0x60000000-0x6fffffff operating system-specific semantics */
420 case 0x6ffffff0: return "XXX:VERSYM";
421 case SHT_SUNW_dof: return "SHT_SUNW_dof";
422 case SHT_GNU_HASH: return "SHT_GNU_HASH";
423 case 0x6ffffff7: return "SHT_GNU_LIBLIST";
424 case 0x6ffffffc: return "XXX:VERDEF";
425 case SHT_SUNW_verdef: return "SHT_SUNW(GNU)_verdef";
426 case SHT_SUNW_verneed: return "SHT_SUNW(GNU)_verneed";
427 case SHT_SUNW_versym: return "SHT_SUNW(GNU)_versym";
429 } else if (sht < 0x80000000) {
430 /* 0x70000000 - 0x7fffffff processor-specific semantics */
434 case SHT_ARM_EXIDX: return "SHT_ARM_EXIDX";
435 case SHT_ARM_PREEMPTMAP: return "SHT_ARM_PREEMPTMAP";
436 case SHT_ARM_ATTRIBUTES: return "SHT_ARM_ATTRIBUTES";
437 case SHT_ARM_DEBUGOVERLAY:
438 return "SHT_ARM_DEBUGOVERLAY";
439 case SHT_ARM_OVERLAYSECTION:
440 return "SHT_ARM_OVERLAYSECTION";
445 case 0x70000000: return "SHT_IA_64_EXT";
446 case 0x70000001: return "SHT_IA_64_UNWIND";
451 case SHT_MIPS_REGINFO: return "SHT_MIPS_REGINFO";
452 case SHT_MIPS_OPTIONS: return "SHT_MIPS_OPTIONS";
453 case SHT_MIPS_ABIFLAGS: return "SHT_MIPS_ABIFLAGS";
458 case 0x7ffffffd: return "XXX:AUXILIARY";
459 case 0x7fffffff: return "XXX:FILTER";
462 /* 0x80000000 - 0xffffffff application programs */
464 snprintf(unknown_buf, sizeof(unknown_buf),
465 "<unknown: %#llx>", (unsigned long long)sht);
466 return (unknown_buf);
470 * Define known section flags. These flags are defined in the order
471 * they are to be printed out.
473 #define DEFINE_SHFLAGS() \
476 DEFINE_SHF(EXECINSTR) \
478 DEFINE_SHF(STRINGS) \
479 DEFINE_SHF(INFO_LINK) \
480 DEFINE_SHF(LINK_ORDER) \
481 DEFINE_SHF(OS_NONCONFORMING) \
484 DEFINE_SHF(COMPRESSED)
487 #define DEFINE_SHF(F) "SHF_" #F "|"
488 #define ALLSHFLAGS DEFINE_SHFLAGS()
491 sh_flags(uint64_t shf)
493 static char flg[sizeof(ALLSHFLAGS)+1];
498 #define DEFINE_SHF(N) \
500 strcat(flg, "SHF_" #N "|"); \
504 flg[strlen(flg) - 1] = '\0'; /* Remove the trailing "|". */
510 st_type(unsigned int mach, unsigned int type)
512 static char s_type[32];
515 case STT_NOTYPE: return "STT_NOTYPE";
516 case STT_OBJECT: return "STT_OBJECT";
517 case STT_FUNC: return "STT_FUNC";
518 case STT_SECTION: return "STT_SECTION";
519 case STT_FILE: return "STT_FILE";
520 case STT_COMMON: return "STT_COMMON";
521 case STT_TLS: return "STT_TLS";
523 if (mach == EM_SPARCV9)
524 return "STT_SPARC_REGISTER";
527 snprintf(s_type, sizeof(s_type), "<unknown: %#x>", type);
532 st_type_S(unsigned int type)
534 static char s_type[32];
537 case STT_NOTYPE: return "NOTY";
538 case STT_OBJECT: return "OBJT";
539 case STT_FUNC: return "FUNC";
540 case STT_SECTION: return "SECT";
541 case STT_FILE: return "FILE";
543 snprintf(s_type, sizeof(s_type), "<unknown: %#x>", type);
548 st_bindings(unsigned int sbind)
550 static char s_sbind[32];
553 case STB_LOCAL: return "STB_LOCAL";
554 case STB_GLOBAL: return "STB_GLOBAL";
555 case STB_WEAK: return "STB_WEAK";
556 case STB_GNU_UNIQUE: return "STB_GNU_UNIQUE";
558 if (sbind >= STB_LOOS && sbind <= STB_HIOS)
560 else if (sbind >= STB_LOPROC && sbind <= STB_HIPROC)
563 snprintf(s_sbind, sizeof(s_sbind), "<unknown: %#x>",
570 st_bindings_S(unsigned int sbind)
572 static char s_sbind[32];
575 case STB_LOCAL: return "LOCL";
576 case STB_GLOBAL: return "GLOB";
577 case STB_WEAK: return "WEAK";
578 case STB_GNU_UNIQUE: return "UNIQ";
580 if (sbind >= STB_LOOS && sbind <= STB_HIOS)
582 else if (sbind >= STB_LOPROC && sbind <= STB_HIPROC)
585 snprintf(s_sbind, sizeof(s_sbind), "<%#x>",
591 static unsigned char st_others[] = {
595 static void add_name(struct elfdump *ed, const char *name);
596 static void elf_print_object(struct elfdump *ed);
597 static void elf_print_elf(struct elfdump *ed);
598 static void elf_print_ehdr(struct elfdump *ed);
599 static void elf_print_phdr(struct elfdump *ed);
600 static void elf_print_shdr(struct elfdump *ed);
601 static void elf_print_symtab(struct elfdump *ed, int i);
602 static void elf_print_symtabs(struct elfdump *ed);
603 static void elf_print_symver(struct elfdump *ed);
604 static void elf_print_verdef(struct elfdump *ed, struct section *s);
605 static void elf_print_verneed(struct elfdump *ed, struct section *s);
606 static void elf_print_interp(struct elfdump *ed);
607 static void elf_print_dynamic(struct elfdump *ed);
608 static void elf_print_rel_entry(struct elfdump *ed, struct section *s,
609 int j, struct rel_entry *r);
610 static void elf_print_rela(struct elfdump *ed, struct section *s,
612 static void elf_print_rel(struct elfdump *ed, struct section *s,
614 static void elf_print_reloc(struct elfdump *ed);
615 static void elf_print_got(struct elfdump *ed);
616 static void elf_print_got_section(struct elfdump *ed, struct section *s);
617 static void elf_print_note(struct elfdump *ed);
618 static void elf_print_svr4_hash(struct elfdump *ed, struct section *s);
619 static void elf_print_svr4_hash64(struct elfdump *ed, struct section *s);
620 static void elf_print_gnu_hash(struct elfdump *ed, struct section *s);
621 static void elf_print_hash(struct elfdump *ed);
622 static void elf_print_checksum(struct elfdump *ed);
623 static void find_gotrel(struct elfdump *ed, struct section *gs,
624 struct rel_entry *got);
625 static struct spec_name *find_name(struct elfdump *ed, const char *name);
626 static int get_ent_count(const struct section *s, int *ent_count);
627 static const char *get_symbol_name(struct elfdump *ed, uint32_t symtab, int i);
628 static const char *get_string(struct elfdump *ed, int strtab, size_t off);
629 static void get_versym(struct elfdump *ed, int i, uint16_t **vs, int *nvs);
630 static void load_sections(struct elfdump *ed);
631 static void unload_sections(struct elfdump *ed);
632 static void usage(void);
633 #ifdef USE_LIBARCHIVE_AR
634 static int ac_detect_ar(int fd);
635 static void ac_print_ar(struct elfdump *ed, int fd);
637 static void elf_print_ar(struct elfdump *ed, int fd);
638 #endif /* USE_LIBARCHIVE_AR */
640 static struct option elfdump_longopts[] =
642 { "help", no_argument, NULL, 'H' },
643 { "version", no_argument, NULL, 'V' },
648 main(int ac, char **av)
650 struct elfdump *ed, ed_storage;
651 struct spec_name *sn;
655 memset(ed, 0, sizeof(*ed));
656 STAILQ_INIT(&ed->snl);
658 while ((ch = getopt_long(ac, av, "acdeiGHhknN:prsSvVw:",
659 elfdump_longopts, NULL)) != -1)
662 ed->options = ED_ALL;
665 ed->options |= ED_SHDR;
668 ed->options |= ED_DYN;
671 ed->options |= ED_EHDR;
674 ed->options |= ED_INTERP;
677 ed->options |= ED_GOT;
680 ed->options |= ED_HASH;
683 ed->options |= ED_CHECKSUM;
686 ed->options |= ED_NOTE;
689 add_name(ed, optarg);
692 ed->options |= ED_PHDR;
695 ed->options |= ED_REL;
698 ed->options |= ED_SYMTAB;
701 ed->flags |= SOLARIS_FMT;
704 ed->options |= ED_SYMVER;
707 (void) printf("%s (%s)\n", ELFTC_GETPROGNAME(),
712 if ((ed->out = fopen(optarg, "w")) == NULL)
713 err(EXIT_FAILURE, "%s", optarg);
724 if (ed->options == 0)
725 ed->options = ED_ALL;
727 if (ed->options & ED_SYMTAB &&
728 (STAILQ_EMPTY(&ed->snl) || (sn = find_name(ed, "ARSYM")) != NULL)) {
729 ed->flags |= PRINT_ARSYM;
731 STAILQ_REMOVE(&ed->snl, sn, spec_name, sn_list);
732 if (STAILQ_EMPTY(&ed->snl))
733 ed->flags |= ONLY_ARSYM;
739 ed->flags |= PRINT_FILENAME;
740 if (elf_version(EV_CURRENT) == EV_NONE)
741 errx(EXIT_FAILURE, "ELF library initialization failed: %s",
744 for (i = 0; i < ac; i++) {
745 ed->filename = av[i];
747 elf_print_object(ed);
753 #ifdef USE_LIBARCHIVE_AR
755 /* Archive symbol table entry. */
762 * Convenient wrapper for general libarchive error handling.
764 #define AC(CALL) do { \
766 warnx("%s", archive_error_string(a)); \
772 * Detect an ar(1) archive using libarchive(3).
778 struct archive_entry *entry;
782 if ((a = archive_read_new()) == NULL)
784 archive_read_support_format_ar(a);
785 if (archive_read_open_fd(a, fd, 10240) == ARCHIVE_OK)
786 r = archive_read_next_header(a, &entry);
787 archive_read_close(a);
788 archive_read_free(a);
790 return (r == ARCHIVE_OK);
794 * Dump an ar(1) archive using libarchive(3).
797 ac_print_ar(struct elfdump *ed, int fd)
800 struct archive_entry *entry;
801 struct arsym_entry *arsym;
809 if (lseek(fd, 0, SEEK_SET) == -1)
810 err(EXIT_FAILURE, "lseek failed");
811 if ((a = archive_read_new()) == NULL)
812 errx(EXIT_FAILURE, "%s", archive_error_string(a));
813 archive_read_support_format_ar(a);
814 AC(archive_read_open_fd(a, fd, 10240));
816 r = archive_read_next_header(a, &entry);
817 if (r == ARCHIVE_FATAL)
818 errx(EXIT_FAILURE, "%s", archive_error_string(a));
819 if (r == ARCHIVE_EOF)
821 if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY)
822 warnx("%s", archive_error_string(a));
823 if (r == ARCHIVE_RETRY)
825 name = archive_entry_pathname(entry);
826 size = archive_entry_size(entry);
829 if ((buff = malloc(size)) == NULL) {
830 warn("malloc failed");
833 if (archive_read_data(a, buff, size) != (ssize_t)size) {
834 warnx("%s", archive_error_string(a));
840 * Note that when processing arsym via libarchive, there is
841 * no way to tell which member a certain symbol belongs to,
842 * since we can not just "lseek" to a member offset and read
845 if (!strcmp(name, "/") && ed->flags & PRINT_ARSYM) {
852 arsym = calloc(cnt, sizeof(*arsym));
854 err(EXIT_FAILURE, "calloc failed");
855 b += sizeof(uint32_t);
856 for (i = 0; i < cnt; i++) {
857 arsym[i].off = be32dec(b);
858 b += sizeof(uint32_t);
860 for (i = 0; i < cnt; i++) {
861 arsym[i].sym_name = b;
864 if (ed->flags & SOLARIS_FMT) {
865 PRT("\nSymbol Table: (archive)\n");
866 PRT(" index offset symbol\n");
868 PRT("\nsymbol table (archive):\n");
869 for (i = 0; i < cnt; i++) {
870 if (ed->flags & SOLARIS_FMT) {
871 snprintf(idx, sizeof(idx), "[%d]", i);
874 (uintmax_t)arsym[i].off);
875 PRT("%s\n", arsym[i].sym_name);
877 PRT("\nentry: %d\n", i);
878 PRT("\toffset: %#jx\n",
879 (uintmax_t)arsym[i].off);
880 PRT("\tsymbol: %s\n",
886 /* No need to continue if we only dump ARSYM. */
887 if (ed->flags & ONLY_ARSYM) {
888 AC(archive_read_close(a));
889 AC(archive_read_free(a));
894 if ((ed->elf = elf_memory(buff, size)) == NULL) {
895 warnx("elf_memroy() failed: %s",
900 /* Skip non-ELF member. */
901 if (elf_kind(ed->elf) == ELF_K_ELF) {
902 printf("\n%s(%s):\n", ed->archive, name);
908 AC(archive_read_close(a));
909 AC(archive_read_free(a));
912 #else /* USE_LIBARCHIVE_AR */
915 * Dump an ar(1) archive.
918 elf_print_ar(struct elfdump *ed, int fd)
929 if (ed->flags & PRINT_ARSYM) {
931 if ((arsym = elf_getarsym(ed->ar, &cnt)) == NULL) {
932 warnx("elf_getarsym failed: %s", elf_errmsg(-1));
937 if (ed->flags & SOLARIS_FMT) {
938 PRT("\nSymbol Table: (archive)\n");
939 PRT(" index offset member name and symbol\n");
941 PRT("\nsymbol table (archive):\n");
942 for (i = 0; i < cnt - 1; i++) {
943 if (elf_rand(ed->ar, arsym[i].as_off) !=
945 warnx("elf_rand failed: %s", elf_errmsg(-1));
948 if ((e = elf_begin(fd, ELF_C_READ, ed->ar)) == NULL) {
949 warnx("elf_begin failed: %s", elf_errmsg(-1));
952 if ((arh = elf_getarhdr(e)) == NULL) {
953 warnx("elf_getarhdr failed: %s",
957 if (ed->flags & SOLARIS_FMT) {
958 snprintf(idx, sizeof(idx), "[%zu]", i);
961 (uintmax_t)arsym[i].as_off);
962 PRT("(%s):%s\n", arh->ar_name,
965 PRT("\nentry: %zu\n", i);
966 PRT("\toffset: %#jx\n",
967 (uintmax_t)arsym[i].as_off);
968 PRT("\tmember: %s\n", arh->ar_name);
969 PRT("\tsymbol: %s\n", arsym[i].as_name);
974 /* No need to continue if we only dump ARSYM. */
975 if (ed->flags & ONLY_ARSYM)
981 /* Rewind the archive. */
982 if (elf_rand(ed->ar, SARMAG) != SARMAG) {
983 warnx("elf_rand failed: %s", elf_errmsg(-1));
987 /* Dump each member of the archive. */
989 while ((ed->elf = elf_begin(fd, cmd, ed->ar)) != NULL) {
990 /* Skip non-ELF member. */
991 if (elf_kind(ed->elf) == ELF_K_ELF) {
992 if ((arh = elf_getarhdr(ed->elf)) == NULL) {
993 warnx("elf_getarhdr failed: %s",
997 printf("\n%s(%s):\n", ed->archive, arh->ar_name);
1000 cmd = elf_next(ed->elf);
1005 #endif /* USE_LIBARCHIVE_AR */
1008 * Dump an object. (ELF object or ar(1) archive)
1011 elf_print_object(struct elfdump *ed)
1015 if ((fd = open(ed->filename, O_RDONLY)) == -1) {
1016 warn("open %s failed", ed->filename);
1020 #ifdef USE_LIBARCHIVE_AR
1021 if (ac_detect_ar(fd)) {
1022 ed->archive = ed->filename;
1023 ac_print_ar(ed, fd);
1026 #endif /* USE_LIBARCHIVE_AR */
1028 if ((ed->elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
1029 warnx("elf_begin() failed: %s", elf_errmsg(-1));
1033 switch (elf_kind(ed->elf)) {
1035 warnx("Not an ELF file.");
1038 if (ed->flags & PRINT_FILENAME)
1039 printf("\n%s:\n", ed->filename);
1043 #ifndef USE_LIBARCHIVE_AR
1044 ed->archive = ed->filename;
1045 elf_print_ar(ed, fd);
1049 warnx("Internal: libelf returned unknown elf kind.");
1057 * Dump an ELF object.
1060 elf_print_elf(struct elfdump *ed)
1063 if (gelf_getehdr(ed->elf, &ed->ehdr) == NULL) {
1064 warnx("gelf_getehdr failed: %s", elf_errmsg(-1));
1067 if ((ed->ec = gelf_getclass(ed->elf)) == ELFCLASSNONE) {
1068 warnx("gelf_getclass failed: %s", elf_errmsg(-1));
1072 if (ed->options & (ED_SHDR | ED_DYN | ED_REL | ED_GOT | ED_SYMTAB |
1073 ED_SYMVER | ED_NOTE | ED_HASH))
1076 if (ed->options & ED_EHDR)
1078 if (ed->options & ED_PHDR)
1080 if (ed->options & ED_INTERP)
1081 elf_print_interp(ed);
1082 if (ed->options & ED_SHDR)
1084 if (ed->options & ED_DYN)
1085 elf_print_dynamic(ed);
1086 if (ed->options & ED_REL)
1087 elf_print_reloc(ed);
1088 if (ed->options & ED_GOT)
1090 if (ed->options & ED_SYMTAB)
1091 elf_print_symtabs(ed);
1092 if (ed->options & ED_SYMVER)
1093 elf_print_symver(ed);
1094 if (ed->options & ED_NOTE)
1096 if (ed->options & ED_HASH)
1098 if (ed->options & ED_CHECKSUM)
1099 elf_print_checksum(ed);
1101 unload_sections(ed);
1105 * Read the section headers from ELF object and store them in the
1109 load_sections(struct elfdump *ed)
1115 size_t shstrndx, ndx;
1118 assert(ed->sl == NULL);
1120 if (!elf_getshnum(ed->elf, &ed->shnum)) {
1121 warnx("elf_getshnum failed: %s", elf_errmsg(-1));
1126 if ((ed->sl = calloc(ed->shnum, sizeof(*ed->sl))) == NULL)
1127 err(EXIT_FAILURE, "calloc failed");
1128 if (!elf_getshstrndx(ed->elf, &shstrndx)) {
1129 warnx("elf_getshstrndx failed: %s", elf_errmsg(-1));
1132 if ((scn = elf_getscn(ed->elf, 0)) == NULL) {
1133 warnx("elf_getscn failed: %s", elf_errmsg(-1));
1138 if (gelf_getshdr(scn, &sh) == NULL) {
1139 warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
1143 if ((name = elf_strptr(ed->elf, shstrndx, sh.sh_name)) == NULL) {
1147 if ((ndx = elf_ndxscn(scn)) == SHN_UNDEF)
1148 if ((elferr = elf_errno()) != 0) {
1149 warnx("elf_ndxscn failed: %s",
1150 elf_errmsg(elferr));
1153 if (ndx >= ed->shnum) {
1154 warnx("section index of '%s' out of range", name);
1160 s->off = sh.sh_offset;
1162 s->entsize = sh.sh_entsize;
1163 s->align = sh.sh_addralign;
1164 s->type = sh.sh_type;
1165 s->flags = sh.sh_flags;
1166 s->addr = sh.sh_addr;
1167 s->link = sh.sh_link;
1168 s->info = sh.sh_info;
1169 } while ((scn = elf_nextscn(ed->elf, scn)) != NULL);
1170 elferr = elf_errno();
1172 warnx("elf_nextscn failed: %s", elf_errmsg(elferr));
1176 * Release section related resources.
1179 unload_sections(struct elfdump *ed)
1181 if (ed->sl != NULL) {
1188 * Add a name to the '-N' name list.
1191 add_name(struct elfdump *ed, const char *name)
1193 struct spec_name *sn;
1195 if (find_name(ed, name))
1197 if ((sn = malloc(sizeof(*sn))) == NULL) {
1198 warn("malloc failed");
1202 STAILQ_INSERT_TAIL(&ed->snl, sn, sn_list);
1206 * Lookup a name in the '-N' name list.
1208 static struct spec_name *
1209 find_name(struct elfdump *ed, const char *name)
1211 struct spec_name *sn;
1213 STAILQ_FOREACH(sn, &ed->snl, sn_list) {
1214 if (!strcmp(sn->name, name))
1222 * Retrieve the name of a symbol using the section index of the symbol
1223 * table and the index of the symbol within that table.
1226 get_symbol_name(struct elfdump *ed, uint32_t symtab, int i)
1228 static char sname[64];
1235 if (symtab >= ed->shnum)
1237 s = &ed->sl[symtab];
1238 if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM)
1241 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
1242 elferr = elf_errno();
1244 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
1247 if (gelf_getsym(data, i, &sym) != &sym)
1249 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION) {
1250 if (sym.st_shndx < ed->shnum) {
1251 snprintf(sname, sizeof(sname), "%s (section)",
1252 ed->sl[sym.st_shndx].name);
1257 if ((name = elf_strptr(ed->elf, s->link, sym.st_name)) == NULL)
1264 * Retrieve a string using string table section index and the string offset.
1267 get_string(struct elfdump *ed, int strtab, size_t off)
1271 if ((name = elf_strptr(ed->elf, strtab, off)) == NULL)
1278 * Dump the ELF Executable Header.
1281 elf_print_ehdr(struct elfdump *ed)
1284 if (!STAILQ_EMPTY(&ed->snl))
1287 if (ed->flags & SOLARIS_FMT) {
1288 PRT("\nELF Header\n");
1289 PRT(" ei_magic: { %#x, %c, %c, %c }\n",
1290 ed->ehdr.e_ident[0], ed->ehdr.e_ident[1],
1291 ed->ehdr.e_ident[2], ed->ehdr.e_ident[3]);
1292 PRT(" ei_class: %-18s",
1293 elf_class_str(ed->ehdr.e_ident[EI_CLASS]));
1294 PRT(" ei_data: %s\n",
1295 elf_data_str(ed->ehdr.e_ident[EI_DATA]));
1296 PRT(" e_machine: %-18s", e_machines(ed->ehdr.e_machine));
1297 PRT(" e_version: %s\n",
1298 elf_version_str(ed->ehdr.e_version));
1299 PRT(" e_type: %s\n", elf_type_str(ed->ehdr.e_type));
1300 PRT(" e_flags: %18d\n", ed->ehdr.e_flags);
1301 PRT(" e_entry: %#18jx", (uintmax_t)ed->ehdr.e_entry);
1302 PRT(" e_ehsize: %6d", ed->ehdr.e_ehsize);
1303 PRT(" e_shstrndx:%5d\n", ed->ehdr.e_shstrndx);
1304 PRT(" e_shoff: %#18jx", (uintmax_t)ed->ehdr.e_shoff);
1305 PRT(" e_shentsize: %3d", ed->ehdr.e_shentsize);
1306 PRT(" e_shnum: %5d\n", ed->ehdr.e_shnum);
1307 PRT(" e_phoff: %#18jx", (uintmax_t)ed->ehdr.e_phoff);
1308 PRT(" e_phentsize: %3d", ed->ehdr.e_phentsize);
1309 PRT(" e_phnum: %5d\n", ed->ehdr.e_phnum);
1311 PRT("\nelf header:\n");
1313 PRT("\te_ident: %s %s %s\n",
1314 elf_class_str(ed->ehdr.e_ident[EI_CLASS]),
1315 elf_data_str(ed->ehdr.e_ident[EI_DATA]),
1316 ei_abis[ed->ehdr.e_ident[EI_OSABI]]);
1317 PRT("\te_type: %s\n", elf_type_str(ed->ehdr.e_type));
1318 PRT("\te_machine: %s\n", e_machines(ed->ehdr.e_machine));
1319 PRT("\te_version: %s\n", elf_version_str(ed->ehdr.e_version));
1320 PRT("\te_entry: %#jx\n", (uintmax_t)ed->ehdr.e_entry);
1321 PRT("\te_phoff: %ju\n", (uintmax_t)ed->ehdr.e_phoff);
1322 PRT("\te_shoff: %ju\n", (uintmax_t) ed->ehdr.e_shoff);
1323 PRT("\te_flags: %u\n", ed->ehdr.e_flags);
1324 PRT("\te_ehsize: %u\n", ed->ehdr.e_ehsize);
1325 PRT("\te_phentsize: %u\n", ed->ehdr.e_phentsize);
1326 PRT("\te_phnum: %u\n", ed->ehdr.e_phnum);
1327 PRT("\te_shentsize: %u\n", ed->ehdr.e_shentsize);
1328 PRT("\te_shnum: %u\n", ed->ehdr.e_shnum);
1329 PRT("\te_shstrndx: %u\n", ed->ehdr.e_shstrndx);
1334 * Dump the ELF Program Header Table.
1337 elf_print_phdr(struct elfdump *ed)
1343 if (elf_getphnum(ed->elf, &phnum) == 0) {
1344 warnx("elf_getphnum failed: %s", elf_errmsg(-1));
1348 for (i = 0; i < phnum; i++) {
1349 if (gelf_getphdr(ed->elf, i, &ph) != &ph) {
1350 warnx("elf_getphdr failed: %s", elf_errmsg(-1));
1353 if (!STAILQ_EMPTY(&ed->snl) &&
1354 find_name(ed, elf_phdr_type_str(ph.p_type)) == NULL)
1356 if (ed->flags & SOLARIS_FMT) {
1357 PRT("\nProgram Header[%zu]:\n", i);
1358 PRT(" p_vaddr: %#-14jx", (uintmax_t)ph.p_vaddr);
1359 PRT(" p_flags: [ %s ]\n",
1360 p_flags[ph.p_flags & 0x7]);
1361 PRT(" p_paddr: %#-14jx", (uintmax_t)ph.p_paddr);
1362 PRT(" p_type: [ %s ]\n",
1363 elf_phdr_type_str(ph.p_type));
1364 PRT(" p_filesz: %#-14jx",
1365 (uintmax_t)ph.p_filesz);
1366 PRT(" p_memsz: %#jx\n", (uintmax_t)ph.p_memsz);
1367 PRT(" p_offset: %#-14jx",
1368 (uintmax_t)ph.p_offset);
1369 PRT(" p_align: %#jx\n", (uintmax_t)ph.p_align);
1372 PRT("\nprogram header:\n");
1376 PRT("entry: %zu\n", i);
1377 PRT("\tp_type: %s\n", elf_phdr_type_str(ph.p_type));
1378 PRT("\tp_offset: %ju\n", (uintmax_t)ph.p_offset);
1379 PRT("\tp_vaddr: %#jx\n", (uintmax_t)ph.p_vaddr);
1380 PRT("\tp_paddr: %#jx\n", (uintmax_t)ph.p_paddr);
1381 PRT("\tp_filesz: %ju\n", (uintmax_t)ph.p_filesz);
1382 PRT("\tp_memsz: %ju\n", (uintmax_t)ph.p_memsz);
1383 PRT("\tp_flags: %s\n", p_flags[ph.p_flags & 0x7]);
1384 PRT("\tp_align: %ju\n", (uintmax_t)ph.p_align);
1390 * Dump the ELF Section Header Table.
1393 elf_print_shdr(struct elfdump *ed)
1398 if (!STAILQ_EMPTY(&ed->snl))
1401 if ((ed->flags & SOLARIS_FMT) == 0)
1402 PRT("\nsection header:\n");
1403 for (i = 0; i < ed->shnum; i++) {
1405 if (ed->flags & SOLARIS_FMT) {
1408 PRT("\nSection Header[%zu]:", i);
1409 PRT(" sh_name: %s\n", s->name);
1410 PRT(" sh_addr: %#-14jx", (uintmax_t)s->addr);
1412 PRT(" sh_flags: [ %s ]\n", sh_flags(s->flags));
1414 PRT(" sh_flags: 0\n");
1415 PRT(" sh_size: %#-14jx", (uintmax_t)s->sz);
1416 PRT(" sh_type: [ %s ]\n",
1417 sh_types(ed->ehdr.e_machine, s->type));
1418 PRT(" sh_offset: %#-14jx", (uintmax_t)s->off);
1419 PRT(" sh_entsize: %#jx\n", (uintmax_t)s->entsize);
1420 PRT(" sh_link: %-14u", s->link);
1421 PRT(" sh_info: %u\n", s->info);
1422 PRT(" sh_addralign: %#jx\n", (uintmax_t)s->align);
1425 PRT("entry: %ju\n", (uintmax_t)i);
1426 PRT("\tsh_name: %s\n", s->name);
1427 PRT("\tsh_type: %s\n",
1428 sh_types(ed->ehdr.e_machine, s->type));
1429 PRT("\tsh_flags: %s\n", sh_flags(s->flags));
1430 PRT("\tsh_addr: %#jx\n", (uintmax_t)s->addr);
1431 PRT("\tsh_offset: %ju\n", (uintmax_t)s->off);
1432 PRT("\tsh_size: %ju\n", (uintmax_t)s->sz);
1433 PRT("\tsh_link: %u\n", s->link);
1434 PRT("\tsh_info: %u\n", s->info);
1435 PRT("\tsh_addralign: %ju\n", (uintmax_t)s->align);
1436 PRT("\tsh_entsize: %ju\n", (uintmax_t)s->entsize);
1442 * Return number of entries in the given section. We'd prefer ent_count be a
1443 * size_t, but libelf APIs already use int for section indices.
1446 get_ent_count(const struct section *s, int *ent_count)
1448 if (s->entsize == 0) {
1449 warnx("section %s has entry size 0", s->name);
1451 } else if (s->sz / s->entsize > INT_MAX) {
1452 warnx("section %s has invalid section count", s->name);
1455 *ent_count = (int)(s->sz / s->entsize);
1460 * Retrieve the content of the corresponding SHT_SUNW_versym section for
1461 * a symbol table section.
1464 get_versym(struct elfdump *ed, int i, uint16_t **vs, int *nvs)
1472 for (j = 0; j < ed->shnum; j++) {
1474 if (s->type == SHT_SUNW_versym && s->link == (uint32_t)i)
1477 if (j >= ed->shnum) {
1482 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
1483 elferr = elf_errno();
1485 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
1491 assert(data->d_size == s->sz);
1492 if (!get_ent_count(s, nvs))
1497 * Dump the symbol table section.
1500 elf_print_symtab(struct elfdump *ed, int i)
1508 int len, j, elferr, nvs;
1511 if (ed->flags & SOLARIS_FMT)
1512 PRT("\nSymbol Table Section: %s\n", s->name);
1514 PRT("\nsymbol table (%s):\n", s->name);
1516 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
1517 elferr = elf_errno();
1519 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
1524 assert(data->d_size == s->sz);
1525 if (!get_ent_count(s, &len))
1527 if (ed->flags & SOLARIS_FMT) {
1528 if (ed->ec == ELFCLASS32)
1529 PRT(" index value ");
1531 PRT(" index value ");
1532 PRT("size type bind oth ver shndx name\n");
1533 get_versym(ed, i, &vs, &nvs);
1534 if (vs != NULL && nvs != len) {
1535 warnx("#symbol not equal to #versym");
1539 for (j = 0; j < len; j++) {
1540 if (gelf_getsym(data, j, &sym) != &sym) {
1541 warnx("gelf_getsym failed: %s", elf_errmsg(-1));
1544 name = get_string(ed, s->link, sym.st_name);
1545 if (ed->flags & SOLARIS_FMT) {
1546 snprintf(idx, sizeof(idx), "[%d]", j);
1547 if (ed->ec == ELFCLASS32)
1551 PRT("0x%8.8jx ", (uintmax_t)sym.st_value);
1552 if (ed->ec == ELFCLASS32)
1553 PRT("0x%8.8jx ", (uintmax_t)sym.st_size);
1555 PRT("0x%12.12jx ", (uintmax_t)sym.st_size);
1556 PRT("%s ", st_type_S(GELF_ST_TYPE(sym.st_info)));
1557 PRT("%s ", st_bindings_S(GELF_ST_BIND(sym.st_info)));
1558 PRT("%c ", st_others[sym.st_other]);
1559 PRT("%3u ", (vs == NULL ? 0 : vs[j]));
1560 PRT("%-11.11s ", sh_name(ed, sym.st_shndx));
1563 PRT("\nentry: %d\n", j);
1564 PRT("\tst_name: %s\n", name);
1565 PRT("\tst_value: %#jx\n", (uintmax_t)sym.st_value);
1566 PRT("\tst_size: %ju\n", (uintmax_t)sym.st_size);
1567 PRT("\tst_info: %s %s\n",
1568 st_type(ed->ehdr.e_machine,
1569 GELF_ST_TYPE(sym.st_info)),
1570 st_bindings(GELF_ST_BIND(sym.st_info)));
1571 PRT("\tst_shndx: %ju\n", (uintmax_t)sym.st_shndx);
1577 * Dump the symbol tables. (.dynsym and .symtab)
1580 elf_print_symtabs(struct elfdump *ed)
1584 for (i = 0; i < ed->shnum; i++)
1585 if ((ed->sl[i].type == SHT_SYMTAB ||
1586 ed->sl[i].type == SHT_DYNSYM) &&
1587 (STAILQ_EMPTY(&ed->snl) || find_name(ed, ed->sl[i].name)))
1588 elf_print_symtab(ed, i);
1592 * Dump the content of .dynamic section.
1595 elf_print_dynamic(struct elfdump *ed)
1605 for (i = 0; (size_t)i < ed->shnum; i++) {
1607 if (s->type == SHT_DYNAMIC &&
1608 (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name)))
1611 if ((size_t)i >= ed->shnum)
1614 if (ed->flags & SOLARIS_FMT) {
1615 PRT("Dynamic Section: %s\n", s->name);
1616 PRT(" index tag value\n");
1618 PRT("\ndynamic:\n");
1620 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
1621 elferr = elf_errno();
1623 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
1626 assert(data->d_size == s->sz);
1627 if (!get_ent_count(s, &len))
1629 for (i = 0; i < len; i++) {
1630 if (gelf_getdyn(data, i, &dyn) != &dyn) {
1631 warnx("gelf_getdyn failed: %s", elf_errmsg(-1));
1635 if (ed->flags & SOLARIS_FMT) {
1636 snprintf(idx, sizeof(idx), "[%d]", i);
1637 PRT("%10s %-16s ", idx, d_tags(dyn.d_tag));
1640 PRT("entry: %d\n", i);
1641 PRT("\td_tag: %s\n", d_tags(dyn.d_tag));
1648 if ((name = elf_strptr(ed->elf, s->link,
1649 dyn.d_un.d_val)) == NULL)
1651 if (ed->flags & SOLARIS_FMT)
1652 PRT("%#-16jx %s\n", (uintmax_t)dyn.d_un.d_val,
1655 PRT("\td_val: %s\n", name);
1672 if (ed->flags & SOLARIS_FMT)
1673 PRT("%#jx\n", (uintmax_t)dyn.d_un.d_val);
1675 PRT("\td_val: %ju\n",
1676 (uintmax_t)dyn.d_un.d_val);
1688 if (ed->flags & SOLARIS_FMT)
1689 PRT("%#jx\n", (uintmax_t)dyn.d_un.d_ptr);
1691 PRT("\td_ptr: %#jx\n",
1692 (uintmax_t)dyn.d_un.d_ptr);
1698 if (ed->flags & SOLARIS_FMT)
1706 * Dump a .rel/.rela section entry.
1709 elf_print_rel_entry(struct elfdump *ed, struct section *s, int j,
1710 struct rel_entry *r)
1713 if (ed->flags & SOLARIS_FMT) {
1714 PRT(" %-23s ", elftc_reloc_type_str(ed->ehdr.e_machine,
1715 GELF_R_TYPE(r->u_r.rel.r_info)));
1716 PRT("%#12jx ", (uintmax_t)r->u_r.rel.r_offset);
1717 if (r->type == SHT_RELA)
1718 PRT("%10jd ", (intmax_t)r->u_r.rela.r_addend);
1721 PRT("%-14s ", s->name);
1722 PRT("%s\n", r->symn);
1725 PRT("entry: %d\n", j);
1726 PRT("\tr_offset: %#jx\n", (uintmax_t)r->u_r.rel.r_offset);
1727 if (ed->ec == ELFCLASS32)
1728 PRT("\tr_info: %#jx\n", (uintmax_t)
1729 ELF32_R_INFO(ELF64_R_SYM(r->u_r.rel.r_info),
1730 ELF64_R_TYPE(r->u_r.rel.r_info)));
1732 PRT("\tr_info: %#jx\n", (uintmax_t)r->u_r.rel.r_info);
1733 if (r->type == SHT_RELA)
1734 PRT("\tr_addend: %jd\n",
1735 (intmax_t)r->u_r.rela.r_addend);
1740 * Dump a relocation section of type SHT_RELA.
1743 elf_print_rela(struct elfdump *ed, struct section *s, Elf_Data *data)
1748 if (ed->flags & SOLARIS_FMT) {
1749 PRT("\nRelocation Section: %s\n", s->name);
1751 "addend section with respect to\n");
1753 PRT("\nrelocation with addend (%s):\n", s->name);
1755 assert(data->d_size == s->sz);
1756 if (!get_ent_count(s, &len))
1758 for (j = 0; j < len; j++) {
1759 if (gelf_getrela(data, j, &r.u_r.rela) != &r.u_r.rela) {
1760 warnx("gelf_getrela failed: %s",
1764 r.symn = get_symbol_name(ed, s->link,
1765 GELF_R_SYM(r.u_r.rela.r_info));
1766 elf_print_rel_entry(ed, s, j, &r);
1771 * Dump a relocation section of type SHT_REL.
1774 elf_print_rel(struct elfdump *ed, struct section *s, Elf_Data *data)
1779 if (ed->flags & SOLARIS_FMT) {
1780 PRT("\nRelocation Section: %s\n", s->name);
1782 "section with respect to\n");
1784 PRT("\nrelocation (%s):\n", s->name);
1786 assert(data->d_size == s->sz);
1787 if (!get_ent_count(s, &len))
1789 for (j = 0; j < len; j++) {
1790 if (gelf_getrel(data, j, &r.u_r.rel) != &r.u_r.rel) {
1791 warnx("gelf_getrel failed: %s", elf_errmsg(-1));
1794 r.symn = get_symbol_name(ed, s->link,
1795 GELF_R_SYM(r.u_r.rel.r_info));
1796 elf_print_rel_entry(ed, s, j, &r);
1801 * Dump relocation sections.
1804 elf_print_reloc(struct elfdump *ed)
1811 for (i = 0; i < ed->shnum; i++) {
1813 if ((s->type == SHT_REL || s->type == SHT_RELA) &&
1814 (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name))) {
1816 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
1817 elferr = elf_errno();
1819 warnx("elf_getdata failed: %s",
1820 elf_errmsg(elferr));
1823 if (s->type == SHT_REL)
1824 elf_print_rel(ed, s, data);
1826 elf_print_rela(ed, s, data);
1832 * Dump the content of PT_INTERP segment.
1835 elf_print_interp(struct elfdump *ed)
1839 size_t filesize, i, phnum;
1841 if (!STAILQ_EMPTY(&ed->snl) && find_name(ed, "PT_INTERP") == NULL)
1844 if ((s = elf_rawfile(ed->elf, &filesize)) == NULL) {
1845 warnx("elf_rawfile failed: %s", elf_errmsg(-1));
1848 if (!elf_getphnum(ed->elf, &phnum)) {
1849 warnx("elf_getphnum failed: %s", elf_errmsg(-1));
1852 for (i = 0; i < phnum; i++) {
1853 if (gelf_getphdr(ed->elf, i, &phdr) != &phdr) {
1854 warnx("elf_getphdr failed: %s", elf_errmsg(-1));
1857 if (phdr.p_type == PT_INTERP) {
1858 if (phdr.p_offset >= filesize) {
1859 warnx("invalid phdr offset");
1863 PRT("\t%s\n", s + phdr.p_offset);
1869 * Search the relocation sections for entries referring to the .got section.
1872 find_gotrel(struct elfdump *ed, struct section *gs, struct rel_entry *got)
1878 int elferr, j, k, len;
1880 for(i = 0; i < ed->shnum; i++) {
1882 if (s->type != SHT_REL && s->type != SHT_RELA)
1885 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
1886 elferr = elf_errno();
1888 warnx("elf_getdata failed: %s",
1889 elf_errmsg(elferr));
1892 memset(&r, 0, sizeof(struct rel_entry));
1894 assert(data->d_size == s->sz);
1895 if (!get_ent_count(s, &len))
1897 for (j = 0; j < len; j++) {
1898 if (s->type == SHT_REL) {
1899 if (gelf_getrel(data, j, &r.u_r.rel) !=
1901 warnx("gelf_getrel failed: %s",
1906 if (gelf_getrela(data, j, &r.u_r.rela) !=
1908 warnx("gelf_getrel failed: %s",
1913 if (r.u_r.rel.r_offset >= gs->addr &&
1914 r.u_r.rel.r_offset < gs->addr + gs->sz) {
1915 r.symn = get_symbol_name(ed, s->link,
1916 GELF_R_SYM(r.u_r.rel.r_info));
1917 k = (r.u_r.rel.r_offset - gs->addr) /
1919 memcpy(&got[k], &r, sizeof(struct rel_entry));
1926 elf_print_got_section(struct elfdump *ed, struct section *s)
1928 struct rel_entry *got;
1929 Elf_Data *data, dst;
1932 if (s->entsize == 0) {
1933 /* XXX IA64 GOT section generated by gcc has entry size 0. */
1935 s->entsize = s->align;
1940 if (!get_ent_count(s, &len))
1942 if (ed->flags & SOLARIS_FMT)
1943 PRT("\nGlobal Offset Table Section: %s (%d entries)\n",
1946 PRT("\nglobal offset table: %s\n", s->name);
1948 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
1949 elferr = elf_errno();
1951 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
1956 * GOT section has section type SHT_PROGBITS, thus libelf treats it as
1957 * byte stream and will not perform any translation on it. As a result,
1958 * an exlicit call to gelf_xlatetom is needed here. Depends on arch,
1959 * GOT section should be translated to either WORD or XWORD.
1961 if (ed->ec == ELFCLASS32)
1962 data->d_type = ELF_T_WORD;
1964 data->d_type = ELF_T_XWORD;
1965 memcpy(&dst, data, sizeof(Elf_Data));
1966 if (gelf_xlatetom(ed->elf, &dst, data, ed->ehdr.e_ident[EI_DATA]) !=
1968 warnx("gelf_xlatetom failed: %s", elf_errmsg(-1));
1971 assert(dst.d_size == s->sz);
1972 if (ed->flags & SOLARIS_FMT) {
1974 * In verbose/Solaris mode, we search the relocation sections
1975 * and try to find the corresponding reloc entry for each GOT
1978 if ((got = calloc(len, sizeof(struct rel_entry))) == NULL)
1979 err(EXIT_FAILURE, "calloc failed");
1980 find_gotrel(ed, s, got);
1981 if (ed->ec == ELFCLASS32) {
1982 PRT(" ndx addr value reloc ");
1983 PRT("addend symbol\n");
1985 PRT(" ndx addr value ");
1986 PRT("reloc addend symbol\n");
1988 for(i = 0; i < len; i++) {
1990 if (ed->ec == ELFCLASS32) {
1992 (uintmax_t) (s->addr + i * s->entsize));
1993 PRT("%-8.8x ", *((uint32_t *)dst.d_buf + i));
1996 (uintmax_t) (s->addr + i * s->entsize));
1998 (uintmax_t) *((uint64_t *)dst.d_buf + i));
2000 PRT("%-18s ", elftc_reloc_type_str(ed->ehdr.e_machine,
2001 GELF_R_TYPE(got[i].u_r.rel.r_info)));
2002 if (ed->ec == ELFCLASS32)
2004 (intmax_t)got[i].u_r.rela.r_addend);
2007 (intmax_t)got[i].u_r.rela.r_addend);
2008 if (got[i].symn == NULL)
2010 PRT("%s\n", got[i].symn);
2014 for(i = 0; i < len; i++) {
2015 PRT("\nentry: %d\n", i);
2016 if (ed->ec == ELFCLASS32)
2017 PRT("\t%#x\n", *((uint32_t *)dst.d_buf + i));
2020 (uintmax_t) *((uint64_t *)dst.d_buf + i));
2026 * Dump the content of Global Offset Table section.
2029 elf_print_got(struct elfdump *ed)
2034 if (!STAILQ_EMPTY(&ed->snl))
2038 for (i = 0; i < ed->shnum; i++) {
2040 if (s->name && !strncmp(s->name, ".got", 4) &&
2041 (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name)))
2042 elf_print_got_section(ed, s);
2047 * Dump the content of .note.ABI-tag section.
2050 elf_print_note(struct elfdump *ed)
2064 for (i = 0; (size_t)i < ed->shnum; i++) {
2066 if (s->type == SHT_NOTE && s->name &&
2067 !strcmp(s->name, ".note.ABI-tag") &&
2068 (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name)))
2071 if ((size_t)i >= ed->shnum)
2073 if (ed->flags & SOLARIS_FMT)
2074 PRT("\nNote Section: %s\n", s->name);
2076 PRT("\nnote (%s):\n", s->name);
2078 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
2079 elferr = elf_errno();
2081 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
2085 count = data->d_size;
2086 while (count > sizeof(Elf_Note)) {
2087 en = (Elf_Note *) (uintptr_t) src;
2088 namesz = en->n_namesz;
2089 descsz = en->n_descsz;
2090 src += sizeof(Elf_Note);
2091 count -= sizeof(Elf_Note);
2092 if (roundup2(namesz, 4) + roundup2(descsz, 4) > count) {
2093 warnx("truncated note section");
2096 if (ed->flags & SOLARIS_FMT) {
2097 PRT("\n type %#x\n", en->n_type);
2098 PRT(" namesz %#x:\n", en->n_namesz);
2102 src += roundup2(namesz, 4);
2103 count -= roundup2(namesz, 4);
2106 * Note that we dump the whole desc part if we're in
2107 * "Solaris mode", while in the normal mode, we only look
2108 * at the first 4 bytes (a 32bit word) of the desc, i.e,
2109 * we assume that it's always a FreeBSD version number.
2111 if (ed->flags & SOLARIS_FMT) {
2112 PRT(" descsz %#x:", en->n_descsz);
2113 for (i = 0; (uint32_t)i < descsz; i++) {
2114 if ((i & 0xF) == 0) {
2115 snprintf(idx, sizeof(idx), "desc[%d]",
2117 PRT("\n %-9s", idx);
2118 } else if ((i & 0x3) == 0)
2120 PRT(" %2.2x", src[i]);
2124 if (ed->ehdr.e_ident[EI_DATA] == ELFDATA2MSB)
2125 desc = be32dec(src);
2127 desc = le32dec(src);
2130 src += roundup2(descsz, 4);
2131 count -= roundup2(descsz, 4);
2136 * Dump a hash table.
2139 elf_print_svr4_hash(struct elfdump *ed, struct section *s)
2143 uint32_t *bucket, *chain;
2144 uint32_t nbucket, nchain;
2145 uint32_t *bl, *c, maxl, total;
2150 if (ed->flags & SOLARIS_FMT)
2151 PRT("\nHash Section: %s\n", s->name);
2153 PRT("\nhash table (%s):\n", s->name);
2155 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
2156 elferr = elf_errno();
2158 warnx("elf_getdata failed: %s",
2159 elf_errmsg(elferr));
2162 if (data->d_size < 2 * sizeof(uint32_t)) {
2163 warnx(".hash section too small");
2169 if (nbucket <= 0 || nchain <= 0) {
2170 warnx("Malformed .hash section");
2174 ((uint64_t)nbucket + (uint64_t)nchain + 2) * sizeof(uint32_t)) {
2175 warnx("Malformed .hash section");
2179 chain = &buf[2 + nbucket];
2181 if (ed->flags & SOLARIS_FMT) {
2183 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
2184 err(EXIT_FAILURE, "calloc failed");
2185 for (i = 0; i < nbucket; i++)
2186 for (j = bucket[i]; j > 0 && j < nchain; j = chain[j])
2189 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
2190 err(EXIT_FAILURE, "calloc failed");
2191 for (i = 0; i < nbucket; i++)
2193 PRT(" bucket symndx name\n");
2194 for (i = 0; i < nbucket; i++) {
2196 for (j = bucket[i]; j > 0 && j < nchain; j = chain[j]) {
2202 snprintf(idx, sizeof(idx), "[%d]", j);
2204 PRT("%s\n", get_symbol_name(ed, s->link, j));
2209 for (i = 0; i <= maxl; i++) {
2211 PRT("%10u buckets contain %8d symbols\n", c[i], i);
2213 PRT("%10u buckets %8u symbols (globals)\n", nbucket,
2216 PRT("\nnbucket: %u\n", nbucket);
2217 PRT("nchain: %u\n\n", nchain);
2218 for (i = 0; i < nbucket; i++)
2219 PRT("bucket[%d]:\n\t%u\n\n", i, bucket[i]);
2220 for (i = 0; i < nchain; i++)
2221 PRT("chain[%d]:\n\t%u\n\n", i, chain[i]);
2226 * Dump a 64bit hash table.
2229 elf_print_svr4_hash64(struct elfdump *ed, struct section *s)
2231 Elf_Data *data, dst;
2233 uint64_t *bucket, *chain;
2234 uint64_t nbucket, nchain;
2235 uint64_t *bl, *c, maxl, total;
2240 if (ed->flags & SOLARIS_FMT)
2241 PRT("\nHash Section: %s\n", s->name);
2243 PRT("\nhash table (%s):\n", s->name);
2246 * ALPHA uses 64-bit hash entries. Since libelf assumes that
2247 * .hash section contains only 32-bit entry, an explicit
2248 * gelf_xlatetom is needed here.
2251 if ((data = elf_rawdata(s->scn, NULL)) == NULL) {
2252 elferr = elf_errno();
2254 warnx("elf_rawdata failed: %s",
2255 elf_errmsg(elferr));
2258 data->d_type = ELF_T_XWORD;
2259 memcpy(&dst, data, sizeof(Elf_Data));
2260 if (gelf_xlatetom(ed->elf, &dst, data,
2261 ed->ehdr.e_ident[EI_DATA]) != &dst) {
2262 warnx("gelf_xlatetom failed: %s", elf_errmsg(-1));
2265 if (dst.d_size < 2 * sizeof(uint64_t)) {
2266 warnx(".hash section too small");
2272 if (nbucket <= 0 || nchain <= 0) {
2273 warnx("Malformed .hash section");
2276 if (dst.d_size != (nbucket + nchain + 2) * sizeof(uint64_t)) {
2277 warnx("Malformed .hash section");
2281 chain = &buf[2 + nbucket];
2283 if (ed->flags & SOLARIS_FMT) {
2285 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
2286 err(EXIT_FAILURE, "calloc failed");
2287 for (i = 0; i < nbucket; i++)
2288 for (j = bucket[i]; j > 0 && j < nchain; j = chain[j])
2291 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
2292 err(EXIT_FAILURE, "calloc failed");
2293 for (i = 0; i < nbucket; i++)
2295 PRT(" bucket symndx name\n");
2296 for (i = 0; i < nbucket; i++) {
2298 for (j = bucket[i]; j > 0 && j < nchain; j = chain[j]) {
2304 snprintf(idx, sizeof(idx), "[%zu]", (size_t)j);
2306 PRT("%s\n", get_symbol_name(ed, s->link, j));
2311 for (i = 0; i <= maxl; i++) {
2313 PRT("%10ju buckets contain %8zu symbols\n",
2314 (uintmax_t)c[i], i);
2316 PRT("%10ju buckets %8ju symbols (globals)\n",
2317 (uintmax_t)nbucket, (uintmax_t)total);
2319 PRT("\nnbucket: %ju\n", (uintmax_t)nbucket);
2320 PRT("nchain: %ju\n\n", (uintmax_t)nchain);
2321 for (i = 0; i < nbucket; i++)
2322 PRT("bucket[%zu]:\n\t%ju\n\n", i, (uintmax_t)bucket[i]);
2323 for (i = 0; i < nchain; i++)
2324 PRT("chain[%zu]:\n\t%ju\n\n", i, (uintmax_t)chain[i]);
2330 * Dump a GNU hash table.
2333 elf_print_gnu_hash(struct elfdump *ed, struct section *s)
2338 uint32_t *bucket, *chain;
2339 uint32_t nbucket, nchain, symndx, maskwords, shift2;
2340 uint32_t *bl, *c, maxl, total;
2342 int first, elferr, dynsymcount;
2345 if (ed->flags & SOLARIS_FMT)
2346 PRT("\nGNU Hash Section: %s\n", s->name);
2348 PRT("\ngnu hash table (%s):\n", s->name);
2350 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
2351 elferr = elf_errno();
2353 warnx("elf_getdata failed: %s",
2354 elf_errmsg(elferr));
2357 if (data->d_size < 4 * sizeof(uint32_t)) {
2358 warnx(".gnu.hash section too small");
2367 if (s->link >= ed->shnum) {
2368 warnx("Malformed .gnu.hash section");
2371 ds = &ed->sl[s->link];
2372 if (!get_ent_count(ds, &dynsymcount))
2374 if (symndx >= (uint32_t)dynsymcount) {
2375 warnx("Malformed .gnu.hash section");
2378 nchain = dynsymcount - symndx;
2379 if (data->d_size != 4 * sizeof(uint32_t) + maskwords *
2380 (ed->ec == ELFCLASS32 ? sizeof(uint32_t) : sizeof(uint64_t)) +
2381 ((uint64_t)nbucket + (uint64_t)nchain) * sizeof(uint32_t)) {
2382 warnx("Malformed .gnu.hash section");
2385 bucket = buf + (ed->ec == ELFCLASS32 ? maskwords : maskwords * 2);
2386 chain = bucket + nbucket;
2388 if (ed->flags & SOLARIS_FMT) {
2390 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
2391 err(EXIT_FAILURE, "calloc failed");
2392 for (i = 0; i < nbucket; i++)
2393 for (j = bucket[i]; j > 0 && j - symndx < nchain; j++) {
2396 if (chain[j - symndx] & 1)
2399 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
2400 err(EXIT_FAILURE, "calloc failed");
2401 for (i = 0; i < nbucket; i++)
2403 PRT(" bucket symndx name\n");
2404 for (i = 0; i < nbucket; i++) {
2406 for (j = bucket[i]; j > 0 && j - symndx < nchain; j++) {
2412 snprintf(idx, sizeof(idx), "[%d]", j );
2414 PRT("%s\n", get_symbol_name(ed, s->link, j));
2415 if (chain[j - symndx] & 1)
2421 for (i = 0; i <= maxl; i++) {
2423 PRT("%10u buckets contain %8d symbols\n", c[i], i);
2425 PRT("%10u buckets %8u symbols (globals)\n", nbucket,
2428 PRT("\nnbucket: %u\n", nbucket);
2429 PRT("symndx: %u\n", symndx);
2430 PRT("maskwords: %u\n", maskwords);
2431 PRT("shift2: %u\n", shift2);
2432 PRT("nchain: %u\n\n", nchain);
2433 for (i = 0; i < nbucket; i++)
2434 PRT("bucket[%d]:\n\t%u\n\n", i, bucket[i]);
2435 for (i = 0; i < nchain; i++)
2436 PRT("chain[%d]:\n\t%u\n\n", i, chain[i]);
2444 elf_print_hash(struct elfdump *ed)
2449 for (i = 0; i < ed->shnum; i++) {
2451 if ((s->type == SHT_HASH || s->type == SHT_GNU_HASH) &&
2452 (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name))) {
2453 if (s->type == SHT_GNU_HASH)
2454 elf_print_gnu_hash(ed, s);
2455 else if (ed->ehdr.e_machine == EM_ALPHA &&
2457 elf_print_svr4_hash64(ed, s);
2459 elf_print_svr4_hash(ed, s);
2465 * Dump the content of a Version Definition(SHT_SUNW_Verdef) Section.
2468 elf_print_verdef(struct elfdump *ed, struct section *s)
2475 uint8_t *buf, *end, *buf2;
2476 int i, j, elferr, count;
2478 if (ed->flags & SOLARIS_FMT)
2479 PRT("Version Definition Section: %s\n", s->name);
2481 PRT("\nversion definition section (%s):\n", s->name);
2483 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
2484 elferr = elf_errno();
2486 warnx("elf_getdata failed: %s",
2487 elf_errmsg(elferr));
2491 end = buf + data->d_size;
2493 if (ed->flags & SOLARIS_FMT)
2494 PRT(" index version dependency\n");
2495 while (buf + sizeof(Elf32_Verdef) <= end) {
2496 vd = (Elf32_Verdef *) (uintptr_t) buf;
2497 if (ed->flags & SOLARIS_FMT) {
2498 snprintf(idx, sizeof(idx), "[%d]", vd->vd_ndx);
2501 PRT("\nentry: %d\n", i++);
2502 PRT("\tvd_version: %u\n", vd->vd_version);
2503 PRT("\tvd_flags: %u\n", vd->vd_flags);
2504 PRT("\tvd_ndx: %u\n", vd->vd_ndx);
2505 PRT("\tvd_cnt: %u\n", vd->vd_cnt);
2506 PRT("\tvd_hash: %u\n", vd->vd_hash);
2507 PRT("\tvd_aux: %u\n", vd->vd_aux);
2508 PRT("\tvd_next: %u\n\n", vd->vd_next);
2510 buf2 = buf + vd->vd_aux;
2513 while (buf2 + sizeof(Elf32_Verdaux) <= end && j < vd->vd_cnt) {
2514 vda = (Elf32_Verdaux *) (uintptr_t) buf2;
2515 str = get_string(ed, s->link, vda->vda_name);
2516 if (ed->flags & SOLARIS_FMT) {
2518 PRT("%-26.26s", str);
2519 else if (count == 1)
2520 PRT(" %-20.20s", str);
2522 PRT("\n%40.40s", "");
2526 PRT("\t\tvda: %d\n", j++);
2527 PRT("\t\t\tvda_name: %s\n", str);
2528 PRT("\t\t\tvda_next: %u\n", vda->vda_next);
2530 if (vda->vda_next == 0) {
2531 if (ed->flags & SOLARIS_FMT) {
2532 if (vd->vd_flags & VER_FLG_BASE) {
2534 PRT("%-20.20s", "");
2535 PRT("%s", "[ BASE ]");
2541 if (ed->flags & SOLARIS_FMT)
2543 buf2 += vda->vda_next;
2545 if (vd->vd_next == 0)
2552 * Dump the content of a Version Needed(SHT_SUNW_Verneed) Section.
2555 elf_print_verneed(struct elfdump *ed, struct section *s)
2560 uint8_t *buf, *end, *buf2;
2561 int i, j, elferr, first;
2563 if (ed->flags & SOLARIS_FMT)
2564 PRT("\nVersion Needed Section: %s\n", s->name);
2566 PRT("\nversion need section (%s):\n", s->name);
2568 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
2569 elferr = elf_errno();
2571 warnx("elf_getdata failed: %s",
2572 elf_errmsg(elferr));
2576 end = buf + data->d_size;
2577 if (ed->flags & SOLARIS_FMT)
2578 PRT(" file version\n");
2580 while (buf + sizeof(Elf32_Verneed) <= end) {
2581 vn = (Elf32_Verneed *) (uintptr_t) buf;
2582 if (ed->flags & SOLARIS_FMT)
2584 get_string(ed, s->link, vn->vn_file));
2586 PRT("\nentry: %d\n", i++);
2587 PRT("\tvn_version: %u\n", vn->vn_version);
2588 PRT("\tvn_cnt: %u\n", vn->vn_cnt);
2589 PRT("\tvn_file: %s\n",
2590 get_string(ed, s->link, vn->vn_file));
2591 PRT("\tvn_aux: %u\n", vn->vn_aux);
2592 PRT("\tvn_next: %u\n\n", vn->vn_next);
2594 buf2 = buf + vn->vn_aux;
2597 while (buf2 + sizeof(Elf32_Vernaux) <= end && j < vn->vn_cnt) {
2598 vna = (Elf32_Vernaux *) (uintptr_t) buf2;
2599 if (ed->flags & SOLARIS_FMT) {
2604 PRT("%s\n", get_string(ed, s->link,
2607 PRT("\t\tvna: %d\n", j++);
2608 PRT("\t\t\tvna_hash: %u\n", vna->vna_hash);
2609 PRT("\t\t\tvna_flags: %u\n", vna->vna_flags);
2610 PRT("\t\t\tvna_other: %u\n", vna->vna_other);
2611 PRT("\t\t\tvna_name: %s\n",
2612 get_string(ed, s->link, vna->vna_name));
2613 PRT("\t\t\tvna_next: %u\n", vna->vna_next);
2615 if (vna->vna_next == 0)
2617 buf2 += vna->vna_next;
2619 if (vn->vn_next == 0)
2626 * Dump the symbol-versioning sections.
2629 elf_print_symver(struct elfdump *ed)
2634 for (i = 0; i < ed->shnum; i++) {
2636 if (!STAILQ_EMPTY(&ed->snl) && !find_name(ed, s->name))
2638 if (s->type == SHT_SUNW_verdef)
2639 elf_print_verdef(ed, s);
2640 if (s->type == SHT_SUNW_verneed)
2641 elf_print_verneed(ed, s);
2646 * Dump the ELF checksum. See gelf_checksum(3) for details.
2649 elf_print_checksum(struct elfdump *ed)
2652 if (!STAILQ_EMPTY(&ed->snl))
2655 PRT("\nelf checksum: %#lx\n", gelf_checksum(ed->elf));
2658 #define USAGE_MESSAGE "\
2659 Usage: %s [options] file...\n\
2660 Display information about ELF objects and ar(1) archives.\n\n\
2662 -a Show all information.\n\
2663 -c Show shared headers.\n\
2664 -d Show dynamic symbols.\n\
2665 -e Show the ELF header.\n\
2667 -H | --help Show a usage message and exit.\n\
2668 -h Show hash values.\n\
2669 -i Show the dynamic interpreter.\n\
2670 -k Show the ELF checksum.\n\
2671 -n Show the contents of note sections.\n\
2672 -N NAME Show the section named \"NAME\".\n\
2673 -p Show the program header.\n\
2674 -r Show relocations.\n\
2675 -s Show the symbol table.\n\
2676 -S Use the Solaris elfdump format.\n\
2677 -v Show symbol-versioning information.\n\
2678 -V | --version Print a version identifier and exit.\n\
2679 -w FILE Write output to \"FILE\".\n"
2684 fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());