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 3497 2016-10-17 20:57:22Z 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";
358 snprintf(s_type, sizeof(s_type), "<unknown: %#x>", type);
362 static const char *p_flags[] = {
363 "", "PF_X", "PF_W", "PF_X|PF_W", "PF_R", "PF_X|PF_R", "PF_W|PF_R",
368 sh_name(struct elfdump *ed, int ndx)
373 case SHN_UNDEF: return "UNDEF";
374 case SHN_ABS: return "ABS";
375 case SHN_COMMON: return "COMMON";
377 if ((uint64_t)ndx < ed->shnum)
378 return (ed->sl[ndx].name);
380 snprintf(num, sizeof(num), "%d", ndx);
386 /* http://www.sco.com/developers/gabi/latest/ch4.sheader.html#sh_type */
388 sh_types(uint64_t mach, uint64_t sht) {
389 static char unknown_buf[64];
391 if (sht < 0x60000000) {
393 case SHT_NULL: return "SHT_NULL";
394 case SHT_PROGBITS: return "SHT_PROGBITS";
395 case SHT_SYMTAB: return "SHT_SYMTAB";
396 case SHT_STRTAB: return "SHT_STRTAB";
397 case SHT_RELA: return "SHT_RELA";
398 case SHT_HASH: return "SHT_HASH";
399 case SHT_DYNAMIC: return "SHT_DYNAMIC";
400 case SHT_NOTE: return "SHT_NOTE";
401 case SHT_NOBITS: return "SHT_NOBITS";
402 case SHT_REL: return "SHT_REL";
403 case SHT_SHLIB: return "SHT_SHLIB";
404 case SHT_DYNSYM: return "SHT_DYNSYM";
405 case SHT_INIT_ARRAY: return "SHT_INIT_ARRAY";
406 case SHT_FINI_ARRAY: return "SHT_FINI_ARRAY";
407 case SHT_PREINIT_ARRAY: return "SHT_PREINIT_ARRAY";
408 case SHT_GROUP: return "SHT_GROUP";
409 case SHT_SYMTAB_SHNDX: return "SHT_SYMTAB_SHNDX";
411 } else if (sht < 0x70000000) {
412 /* 0x60000000-0x6fffffff operating system-specific semantics */
414 case 0x6ffffff0: return "XXX:VERSYM";
415 case SHT_SUNW_dof: return "SHT_SUNW_dof";
416 case SHT_GNU_HASH: return "SHT_GNU_HASH";
417 case 0x6ffffff7: return "SHT_GNU_LIBLIST";
418 case 0x6ffffffc: return "XXX:VERDEF";
419 case SHT_SUNW_verdef: return "SHT_SUNW(GNU)_verdef";
420 case SHT_SUNW_verneed: return "SHT_SUNW(GNU)_verneed";
421 case SHT_SUNW_versym: return "SHT_SUNW(GNU)_versym";
423 } else if (sht < 0x80000000) {
424 /* 0x70000000 - 0x7fffffff processor-specific semantics */
428 case SHT_ARM_EXIDX: return "SHT_ARM_EXIDX";
429 case SHT_ARM_PREEMPTMAP: return "SHT_ARM_PREEMPTMAP";
430 case SHT_ARM_ATTRIBUTES: return "SHT_ARM_ATTRIBUTES";
431 case SHT_ARM_DEBUGOVERLAY:
432 return "SHT_ARM_DEBUGOVERLAY";
433 case SHT_ARM_OVERLAYSECTION:
434 return "SHT_ARM_OVERLAYSECTION";
439 case 0x70000000: return "SHT_IA_64_EXT";
440 case 0x70000001: return "SHT_IA_64_UNWIND";
445 case SHT_MIPS_REGINFO: return "SHT_MIPS_REGINFO";
446 case SHT_MIPS_OPTIONS: return "SHT_MIPS_OPTIONS";
447 case SHT_MIPS_ABIFLAGS: return "SHT_MIPS_ABIFLAGS";
452 case 0x7ffffffd: return "XXX:AUXILIARY";
453 case 0x7fffffff: return "XXX:FILTER";
456 /* 0x80000000 - 0xffffffff application programs */
458 snprintf(unknown_buf, sizeof(unknown_buf),
459 "<unknown: %#llx>", (unsigned long long)sht);
460 return (unknown_buf);
464 * Define known section flags. These flags are defined in the order
465 * they are to be printed out.
467 #define DEFINE_SHFLAGS() \
470 DEFINE_SHF(EXECINSTR) \
472 DEFINE_SHF(STRINGS) \
473 DEFINE_SHF(INFO_LINK) \
474 DEFINE_SHF(LINK_ORDER) \
475 DEFINE_SHF(OS_NONCONFORMING) \
478 DEFINE_SHF(COMPRESSED)
481 #define DEFINE_SHF(F) "SHF_" #F "|"
482 #define ALLSHFLAGS DEFINE_SHFLAGS()
485 sh_flags(uint64_t shf)
487 static char flg[sizeof(ALLSHFLAGS)+1];
492 #define DEFINE_SHF(N) \
494 strcat(flg, "SHF_" #N "|"); \
498 flg[strlen(flg) - 1] = '\0'; /* Remove the trailing "|". */
504 st_type(unsigned int mach, unsigned int type)
506 static char s_type[32];
509 case STT_NOTYPE: return "STT_NOTYPE";
510 case STT_OBJECT: return "STT_OBJECT";
511 case STT_FUNC: return "STT_FUNC";
512 case STT_SECTION: return "STT_SECTION";
513 case STT_FILE: return "STT_FILE";
514 case STT_COMMON: return "STT_COMMON";
515 case STT_TLS: return "STT_TLS";
517 if (mach == EM_SPARCV9)
518 return "STT_SPARC_REGISTER";
521 snprintf(s_type, sizeof(s_type), "<unknown: %#x>", type);
526 st_type_S(unsigned int type)
528 static char s_type[32];
531 case STT_NOTYPE: return "NOTY";
532 case STT_OBJECT: return "OBJT";
533 case STT_FUNC: return "FUNC";
534 case STT_SECTION: return "SECT";
535 case STT_FILE: return "FILE";
537 snprintf(s_type, sizeof(s_type), "<unknown: %#x>", type);
542 st_bindings(unsigned int sbind)
544 static char s_sbind[32];
547 case STB_LOCAL: return "STB_LOCAL";
548 case STB_GLOBAL: return "STB_GLOBAL";
549 case STB_WEAK: return "STB_WEAK";
550 case STB_GNU_UNIQUE: return "STB_GNU_UNIQUE";
552 if (sbind >= STB_LOOS && sbind <= STB_HIOS)
554 else if (sbind >= STB_LOPROC && sbind <= STB_HIPROC)
557 snprintf(s_sbind, sizeof(s_sbind), "<unknown: %#x>",
564 st_bindings_S(unsigned int sbind)
566 static char s_sbind[32];
569 case STB_LOCAL: return "LOCL";
570 case STB_GLOBAL: return "GLOB";
571 case STB_WEAK: return "WEAK";
572 case STB_GNU_UNIQUE: return "UNIQ";
574 if (sbind >= STB_LOOS && sbind <= STB_HIOS)
576 else if (sbind >= STB_LOPROC && sbind <= STB_HIPROC)
579 snprintf(s_sbind, sizeof(s_sbind), "<%#x>",
585 static unsigned char st_others[] = {
589 static void add_name(struct elfdump *ed, const char *name);
590 static void elf_print_object(struct elfdump *ed);
591 static void elf_print_elf(struct elfdump *ed);
592 static void elf_print_ehdr(struct elfdump *ed);
593 static void elf_print_phdr(struct elfdump *ed);
594 static void elf_print_shdr(struct elfdump *ed);
595 static void elf_print_symtab(struct elfdump *ed, int i);
596 static void elf_print_symtabs(struct elfdump *ed);
597 static void elf_print_symver(struct elfdump *ed);
598 static void elf_print_verdef(struct elfdump *ed, struct section *s);
599 static void elf_print_verneed(struct elfdump *ed, struct section *s);
600 static void elf_print_interp(struct elfdump *ed);
601 static void elf_print_dynamic(struct elfdump *ed);
602 static void elf_print_rel_entry(struct elfdump *ed, struct section *s,
603 int j, struct rel_entry *r);
604 static void elf_print_rela(struct elfdump *ed, struct section *s,
606 static void elf_print_rel(struct elfdump *ed, struct section *s,
608 static void elf_print_reloc(struct elfdump *ed);
609 static void elf_print_got(struct elfdump *ed);
610 static void elf_print_got_section(struct elfdump *ed, struct section *s);
611 static void elf_print_note(struct elfdump *ed);
612 static void elf_print_svr4_hash(struct elfdump *ed, struct section *s);
613 static void elf_print_svr4_hash64(struct elfdump *ed, struct section *s);
614 static void elf_print_gnu_hash(struct elfdump *ed, struct section *s);
615 static void elf_print_hash(struct elfdump *ed);
616 static void elf_print_checksum(struct elfdump *ed);
617 static void find_gotrel(struct elfdump *ed, struct section *gs,
618 struct rel_entry *got);
619 static struct spec_name *find_name(struct elfdump *ed, const char *name);
620 static int get_ent_count(const struct section *s, int *ent_count);
621 static const char *get_symbol_name(struct elfdump *ed, uint32_t symtab, int i);
622 static const char *get_string(struct elfdump *ed, int strtab, size_t off);
623 static void get_versym(struct elfdump *ed, int i, uint16_t **vs, int *nvs);
624 static void load_sections(struct elfdump *ed);
625 static void unload_sections(struct elfdump *ed);
626 static void usage(void);
627 #ifdef USE_LIBARCHIVE_AR
628 static int ac_detect_ar(int fd);
629 static void ac_print_ar(struct elfdump *ed, int fd);
631 static void elf_print_ar(struct elfdump *ed, int fd);
632 #endif /* USE_LIBARCHIVE_AR */
634 static struct option elfdump_longopts[] =
636 { "help", no_argument, NULL, 'H' },
637 { "version", no_argument, NULL, 'V' },
642 main(int ac, char **av)
644 struct elfdump *ed, ed_storage;
645 struct spec_name *sn;
649 memset(ed, 0, sizeof(*ed));
650 STAILQ_INIT(&ed->snl);
652 while ((ch = getopt_long(ac, av, "acdeiGHhknN:prsSvVw:",
653 elfdump_longopts, NULL)) != -1)
656 ed->options = ED_ALL;
659 ed->options |= ED_SHDR;
662 ed->options |= ED_DYN;
665 ed->options |= ED_EHDR;
668 ed->options |= ED_INTERP;
671 ed->options |= ED_GOT;
674 ed->options |= ED_HASH;
677 ed->options |= ED_CHECKSUM;
680 ed->options |= ED_NOTE;
683 add_name(ed, optarg);
686 ed->options |= ED_PHDR;
689 ed->options |= ED_REL;
692 ed->options |= ED_SYMTAB;
695 ed->flags |= SOLARIS_FMT;
698 ed->options |= ED_SYMVER;
701 (void) printf("%s (%s)\n", ELFTC_GETPROGNAME(),
706 if ((ed->out = fopen(optarg, "w")) == NULL)
707 err(EXIT_FAILURE, "%s", optarg);
718 if (ed->options == 0)
719 ed->options = ED_ALL;
721 if (ed->options & ED_SYMTAB &&
722 (STAILQ_EMPTY(&ed->snl) || (sn = find_name(ed, "ARSYM")) != NULL)) {
723 ed->flags |= PRINT_ARSYM;
725 STAILQ_REMOVE(&ed->snl, sn, spec_name, sn_list);
726 if (STAILQ_EMPTY(&ed->snl))
727 ed->flags |= ONLY_ARSYM;
733 ed->flags |= PRINT_FILENAME;
734 if (elf_version(EV_CURRENT) == EV_NONE)
735 errx(EXIT_FAILURE, "ELF library initialization failed: %s",
738 for (i = 0; i < ac; i++) {
739 ed->filename = av[i];
741 elf_print_object(ed);
747 #ifdef USE_LIBARCHIVE_AR
749 /* Archive symbol table entry. */
756 * Convenient wrapper for general libarchive error handling.
758 #define AC(CALL) do { \
760 warnx("%s", archive_error_string(a)); \
766 * Detect an ar(1) archive using libarchive(3).
772 struct archive_entry *entry;
776 if ((a = archive_read_new()) == NULL)
778 archive_read_support_format_ar(a);
779 if (archive_read_open_fd(a, fd, 10240) == ARCHIVE_OK)
780 r = archive_read_next_header(a, &entry);
781 archive_read_close(a);
782 archive_read_free(a);
784 return (r == ARCHIVE_OK);
788 * Dump an ar(1) archive using libarchive(3).
791 ac_print_ar(struct elfdump *ed, int fd)
794 struct archive_entry *entry;
795 struct arsym_entry *arsym;
803 if (lseek(fd, 0, SEEK_SET) == -1)
804 err(EXIT_FAILURE, "lseek failed");
805 if ((a = archive_read_new()) == NULL)
806 errx(EXIT_FAILURE, "%s", archive_error_string(a));
807 archive_read_support_format_ar(a);
808 AC(archive_read_open_fd(a, fd, 10240));
810 r = archive_read_next_header(a, &entry);
811 if (r == ARCHIVE_FATAL)
812 errx(EXIT_FAILURE, "%s", archive_error_string(a));
813 if (r == ARCHIVE_EOF)
815 if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY)
816 warnx("%s", archive_error_string(a));
817 if (r == ARCHIVE_RETRY)
819 name = archive_entry_pathname(entry);
820 size = archive_entry_size(entry);
823 if ((buff = malloc(size)) == NULL) {
824 warn("malloc failed");
827 if (archive_read_data(a, buff, size) != (ssize_t)size) {
828 warnx("%s", archive_error_string(a));
834 * Note that when processing arsym via libarchive, there is
835 * no way to tell which member a certain symbol belongs to,
836 * since we can not just "lseek" to a member offset and read
839 if (!strcmp(name, "/") && ed->flags & PRINT_ARSYM) {
846 arsym = calloc(cnt, sizeof(*arsym));
848 err(EXIT_FAILURE, "calloc failed");
849 b += sizeof(uint32_t);
850 for (i = 0; i < cnt; i++) {
851 arsym[i].off = be32dec(b);
852 b += sizeof(uint32_t);
854 for (i = 0; i < cnt; i++) {
855 arsym[i].sym_name = b;
858 if (ed->flags & SOLARIS_FMT) {
859 PRT("\nSymbol Table: (archive)\n");
860 PRT(" index offset symbol\n");
862 PRT("\nsymbol table (archive):\n");
863 for (i = 0; i < cnt; i++) {
864 if (ed->flags & SOLARIS_FMT) {
865 snprintf(idx, sizeof(idx), "[%d]", i);
868 (uintmax_t)arsym[i].off);
869 PRT("%s\n", arsym[i].sym_name);
871 PRT("\nentry: %d\n", i);
872 PRT("\toffset: %#jx\n",
873 (uintmax_t)arsym[i].off);
874 PRT("\tsymbol: %s\n",
880 /* No need to continue if we only dump ARSYM. */
881 if (ed->flags & ONLY_ARSYM) {
882 AC(archive_read_close(a));
883 AC(archive_read_free(a));
888 if ((ed->elf = elf_memory(buff, size)) == NULL) {
889 warnx("elf_memroy() failed: %s",
894 /* Skip non-ELF member. */
895 if (elf_kind(ed->elf) == ELF_K_ELF) {
896 printf("\n%s(%s):\n", ed->archive, name);
902 AC(archive_read_close(a));
903 AC(archive_read_free(a));
906 #else /* USE_LIBARCHIVE_AR */
909 * Dump an ar(1) archive.
912 elf_print_ar(struct elfdump *ed, int fd)
923 if (ed->flags & PRINT_ARSYM) {
925 if ((arsym = elf_getarsym(ed->ar, &cnt)) == NULL) {
926 warnx("elf_getarsym failed: %s", elf_errmsg(-1));
931 if (ed->flags & SOLARIS_FMT) {
932 PRT("\nSymbol Table: (archive)\n");
933 PRT(" index offset member name and symbol\n");
935 PRT("\nsymbol table (archive):\n");
936 for (i = 0; i < cnt - 1; i++) {
937 if (elf_rand(ed->ar, arsym[i].as_off) !=
939 warnx("elf_rand failed: %s", elf_errmsg(-1));
942 if ((e = elf_begin(fd, ELF_C_READ, ed->ar)) == NULL) {
943 warnx("elf_begin failed: %s", elf_errmsg(-1));
946 if ((arh = elf_getarhdr(e)) == NULL) {
947 warnx("elf_getarhdr failed: %s",
951 if (ed->flags & SOLARIS_FMT) {
952 snprintf(idx, sizeof(idx), "[%zu]", i);
955 (uintmax_t)arsym[i].as_off);
956 PRT("(%s):%s\n", arh->ar_name,
959 PRT("\nentry: %zu\n", i);
960 PRT("\toffset: %#jx\n",
961 (uintmax_t)arsym[i].as_off);
962 PRT("\tmember: %s\n", arh->ar_name);
963 PRT("\tsymbol: %s\n", arsym[i].as_name);
968 /* No need to continue if we only dump ARSYM. */
969 if (ed->flags & ONLY_ARSYM)
975 /* Rewind the archive. */
976 if (elf_rand(ed->ar, SARMAG) != SARMAG) {
977 warnx("elf_rand failed: %s", elf_errmsg(-1));
981 /* Dump each member of the archive. */
983 while ((ed->elf = elf_begin(fd, cmd, ed->ar)) != NULL) {
984 /* Skip non-ELF member. */
985 if (elf_kind(ed->elf) == ELF_K_ELF) {
986 if ((arh = elf_getarhdr(ed->elf)) == NULL) {
987 warnx("elf_getarhdr failed: %s",
991 printf("\n%s(%s):\n", ed->archive, arh->ar_name);
994 cmd = elf_next(ed->elf);
999 #endif /* USE_LIBARCHIVE_AR */
1002 * Dump an object. (ELF object or ar(1) archive)
1005 elf_print_object(struct elfdump *ed)
1009 if ((fd = open(ed->filename, O_RDONLY)) == -1) {
1010 warn("open %s failed", ed->filename);
1014 #ifdef USE_LIBARCHIVE_AR
1015 if (ac_detect_ar(fd)) {
1016 ed->archive = ed->filename;
1017 ac_print_ar(ed, fd);
1020 #endif /* USE_LIBARCHIVE_AR */
1022 if ((ed->elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
1023 warnx("elf_begin() failed: %s", elf_errmsg(-1));
1027 switch (elf_kind(ed->elf)) {
1029 warnx("Not an ELF file.");
1032 if (ed->flags & PRINT_FILENAME)
1033 printf("\n%s:\n", ed->filename);
1037 #ifndef USE_LIBARCHIVE_AR
1038 ed->archive = ed->filename;
1039 elf_print_ar(ed, fd);
1043 warnx("Internal: libelf returned unknown elf kind.");
1051 * Dump an ELF object.
1054 elf_print_elf(struct elfdump *ed)
1057 if (gelf_getehdr(ed->elf, &ed->ehdr) == NULL) {
1058 warnx("gelf_getehdr failed: %s", elf_errmsg(-1));
1061 if ((ed->ec = gelf_getclass(ed->elf)) == ELFCLASSNONE) {
1062 warnx("gelf_getclass failed: %s", elf_errmsg(-1));
1066 if (ed->options & (ED_SHDR | ED_DYN | ED_REL | ED_GOT | ED_SYMTAB |
1067 ED_SYMVER | ED_NOTE | ED_HASH))
1070 if (ed->options & ED_EHDR)
1072 if (ed->options & ED_PHDR)
1074 if (ed->options & ED_INTERP)
1075 elf_print_interp(ed);
1076 if (ed->options & ED_SHDR)
1078 if (ed->options & ED_DYN)
1079 elf_print_dynamic(ed);
1080 if (ed->options & ED_REL)
1081 elf_print_reloc(ed);
1082 if (ed->options & ED_GOT)
1084 if (ed->options & ED_SYMTAB)
1085 elf_print_symtabs(ed);
1086 if (ed->options & ED_SYMVER)
1087 elf_print_symver(ed);
1088 if (ed->options & ED_NOTE)
1090 if (ed->options & ED_HASH)
1092 if (ed->options & ED_CHECKSUM)
1093 elf_print_checksum(ed);
1095 unload_sections(ed);
1099 * Read the section headers from ELF object and store them in the
1103 load_sections(struct elfdump *ed)
1109 size_t shstrndx, ndx;
1112 assert(ed->sl == NULL);
1114 if (!elf_getshnum(ed->elf, &ed->shnum)) {
1115 warnx("elf_getshnum failed: %s", elf_errmsg(-1));
1120 if ((ed->sl = calloc(ed->shnum, sizeof(*ed->sl))) == NULL)
1121 err(EXIT_FAILURE, "calloc failed");
1122 if (!elf_getshstrndx(ed->elf, &shstrndx)) {
1123 warnx("elf_getshstrndx failed: %s", elf_errmsg(-1));
1126 if ((scn = elf_getscn(ed->elf, 0)) == NULL) {
1127 warnx("elf_getscn failed: %s", elf_errmsg(-1));
1132 if (gelf_getshdr(scn, &sh) == NULL) {
1133 warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
1137 if ((name = elf_strptr(ed->elf, shstrndx, sh.sh_name)) == NULL) {
1141 if ((ndx = elf_ndxscn(scn)) == SHN_UNDEF)
1142 if ((elferr = elf_errno()) != 0) {
1143 warnx("elf_ndxscn failed: %s",
1144 elf_errmsg(elferr));
1147 if (ndx >= ed->shnum) {
1148 warnx("section index of '%s' out of range", name);
1154 s->off = sh.sh_offset;
1156 s->entsize = sh.sh_entsize;
1157 s->align = sh.sh_addralign;
1158 s->type = sh.sh_type;
1159 s->flags = sh.sh_flags;
1160 s->addr = sh.sh_addr;
1161 s->link = sh.sh_link;
1162 s->info = sh.sh_info;
1163 } while ((scn = elf_nextscn(ed->elf, scn)) != NULL);
1164 elferr = elf_errno();
1166 warnx("elf_nextscn failed: %s", elf_errmsg(elferr));
1170 * Release section related resources.
1173 unload_sections(struct elfdump *ed)
1175 if (ed->sl != NULL) {
1182 * Add a name to the '-N' name list.
1185 add_name(struct elfdump *ed, const char *name)
1187 struct spec_name *sn;
1189 if (find_name(ed, name))
1191 if ((sn = malloc(sizeof(*sn))) == NULL) {
1192 warn("malloc failed");
1196 STAILQ_INSERT_TAIL(&ed->snl, sn, sn_list);
1200 * Lookup a name in the '-N' name list.
1202 static struct spec_name *
1203 find_name(struct elfdump *ed, const char *name)
1205 struct spec_name *sn;
1207 STAILQ_FOREACH(sn, &ed->snl, sn_list) {
1208 if (!strcmp(sn->name, name))
1216 * Retrieve the name of a symbol using the section index of the symbol
1217 * table and the index of the symbol within that table.
1220 get_symbol_name(struct elfdump *ed, uint32_t symtab, int i)
1222 static char sname[64];
1229 if (symtab >= ed->shnum)
1231 s = &ed->sl[symtab];
1232 if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM)
1235 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
1236 elferr = elf_errno();
1238 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
1241 if (gelf_getsym(data, i, &sym) != &sym)
1243 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION) {
1244 if (sym.st_shndx < ed->shnum) {
1245 snprintf(sname, sizeof(sname), "%s (section)",
1246 ed->sl[sym.st_shndx].name);
1251 if ((name = elf_strptr(ed->elf, s->link, sym.st_name)) == NULL)
1258 * Retrieve a string using string table section index and the string offset.
1261 get_string(struct elfdump *ed, int strtab, size_t off)
1265 if ((name = elf_strptr(ed->elf, strtab, off)) == NULL)
1272 * Dump the ELF Executable Header.
1275 elf_print_ehdr(struct elfdump *ed)
1278 if (!STAILQ_EMPTY(&ed->snl))
1281 if (ed->flags & SOLARIS_FMT) {
1282 PRT("\nELF Header\n");
1283 PRT(" ei_magic: { %#x, %c, %c, %c }\n",
1284 ed->ehdr.e_ident[0], ed->ehdr.e_ident[1],
1285 ed->ehdr.e_ident[2], ed->ehdr.e_ident[3]);
1286 PRT(" ei_class: %-18s",
1287 elf_class_str(ed->ehdr.e_ident[EI_CLASS]));
1288 PRT(" ei_data: %s\n",
1289 elf_data_str(ed->ehdr.e_ident[EI_DATA]));
1290 PRT(" e_machine: %-18s", e_machines(ed->ehdr.e_machine));
1291 PRT(" e_version: %s\n",
1292 elf_version_str(ed->ehdr.e_version));
1293 PRT(" e_type: %s\n", elf_type_str(ed->ehdr.e_type));
1294 PRT(" e_flags: %18d\n", ed->ehdr.e_flags);
1295 PRT(" e_entry: %#18jx", (uintmax_t)ed->ehdr.e_entry);
1296 PRT(" e_ehsize: %6d", ed->ehdr.e_ehsize);
1297 PRT(" e_shstrndx:%5d\n", ed->ehdr.e_shstrndx);
1298 PRT(" e_shoff: %#18jx", (uintmax_t)ed->ehdr.e_shoff);
1299 PRT(" e_shentsize: %3d", ed->ehdr.e_shentsize);
1300 PRT(" e_shnum: %5d\n", ed->ehdr.e_shnum);
1301 PRT(" e_phoff: %#18jx", (uintmax_t)ed->ehdr.e_phoff);
1302 PRT(" e_phentsize: %3d", ed->ehdr.e_phentsize);
1303 PRT(" e_phnum: %5d\n", ed->ehdr.e_phnum);
1305 PRT("\nelf header:\n");
1307 PRT("\te_ident: %s %s %s\n",
1308 elf_class_str(ed->ehdr.e_ident[EI_CLASS]),
1309 elf_data_str(ed->ehdr.e_ident[EI_DATA]),
1310 ei_abis[ed->ehdr.e_ident[EI_OSABI]]);
1311 PRT("\te_type: %s\n", elf_type_str(ed->ehdr.e_type));
1312 PRT("\te_machine: %s\n", e_machines(ed->ehdr.e_machine));
1313 PRT("\te_version: %s\n", elf_version_str(ed->ehdr.e_version));
1314 PRT("\te_entry: %#jx\n", (uintmax_t)ed->ehdr.e_entry);
1315 PRT("\te_phoff: %ju\n", (uintmax_t)ed->ehdr.e_phoff);
1316 PRT("\te_shoff: %ju\n", (uintmax_t) ed->ehdr.e_shoff);
1317 PRT("\te_flags: %u\n", ed->ehdr.e_flags);
1318 PRT("\te_ehsize: %u\n", ed->ehdr.e_ehsize);
1319 PRT("\te_phentsize: %u\n", ed->ehdr.e_phentsize);
1320 PRT("\te_phnum: %u\n", ed->ehdr.e_phnum);
1321 PRT("\te_shentsize: %u\n", ed->ehdr.e_shentsize);
1322 PRT("\te_shnum: %u\n", ed->ehdr.e_shnum);
1323 PRT("\te_shstrndx: %u\n", ed->ehdr.e_shstrndx);
1328 * Dump the ELF Program Header Table.
1331 elf_print_phdr(struct elfdump *ed)
1337 if (elf_getphnum(ed->elf, &phnum) == 0) {
1338 warnx("elf_getphnum failed: %s", elf_errmsg(-1));
1342 for (i = 0; i < phnum; i++) {
1343 if (gelf_getphdr(ed->elf, i, &ph) != &ph) {
1344 warnx("elf_getphdr failed: %s", elf_errmsg(-1));
1347 if (!STAILQ_EMPTY(&ed->snl) &&
1348 find_name(ed, elf_phdr_type_str(ph.p_type)) == NULL)
1350 if (ed->flags & SOLARIS_FMT) {
1351 PRT("\nProgram Header[%zu]:\n", i);
1352 PRT(" p_vaddr: %#-14jx", (uintmax_t)ph.p_vaddr);
1353 PRT(" p_flags: [ %s ]\n",
1354 p_flags[ph.p_flags & 0x7]);
1355 PRT(" p_paddr: %#-14jx", (uintmax_t)ph.p_paddr);
1356 PRT(" p_type: [ %s ]\n",
1357 elf_phdr_type_str(ph.p_type));
1358 PRT(" p_filesz: %#-14jx",
1359 (uintmax_t)ph.p_filesz);
1360 PRT(" p_memsz: %#jx\n", (uintmax_t)ph.p_memsz);
1361 PRT(" p_offset: %#-14jx",
1362 (uintmax_t)ph.p_offset);
1363 PRT(" p_align: %#jx\n", (uintmax_t)ph.p_align);
1366 PRT("\nprogram header:\n");
1370 PRT("entry: %zu\n", i);
1371 PRT("\tp_type: %s\n", elf_phdr_type_str(ph.p_type));
1372 PRT("\tp_offset: %ju\n", (uintmax_t)ph.p_offset);
1373 PRT("\tp_vaddr: %#jx\n", (uintmax_t)ph.p_vaddr);
1374 PRT("\tp_paddr: %#jx\n", (uintmax_t)ph.p_paddr);
1375 PRT("\tp_filesz: %ju\n", (uintmax_t)ph.p_filesz);
1376 PRT("\tp_memsz: %ju\n", (uintmax_t)ph.p_memsz);
1377 PRT("\tp_flags: %s\n", p_flags[ph.p_flags & 0x7]);
1378 PRT("\tp_align: %ju\n", (uintmax_t)ph.p_align);
1384 * Dump the ELF Section Header Table.
1387 elf_print_shdr(struct elfdump *ed)
1392 if (!STAILQ_EMPTY(&ed->snl))
1395 if ((ed->flags & SOLARIS_FMT) == 0)
1396 PRT("\nsection header:\n");
1397 for (i = 0; i < ed->shnum; i++) {
1399 if (ed->flags & SOLARIS_FMT) {
1402 PRT("\nSection Header[%zu]:", i);
1403 PRT(" sh_name: %s\n", s->name);
1404 PRT(" sh_addr: %#-14jx", (uintmax_t)s->addr);
1406 PRT(" sh_flags: [ %s ]\n", sh_flags(s->flags));
1408 PRT(" sh_flags: 0\n");
1409 PRT(" sh_size: %#-14jx", (uintmax_t)s->sz);
1410 PRT(" sh_type: [ %s ]\n",
1411 sh_types(ed->ehdr.e_machine, s->type));
1412 PRT(" sh_offset: %#-14jx", (uintmax_t)s->off);
1413 PRT(" sh_entsize: %#jx\n", (uintmax_t)s->entsize);
1414 PRT(" sh_link: %-14u", s->link);
1415 PRT(" sh_info: %u\n", s->info);
1416 PRT(" sh_addralign: %#jx\n", (uintmax_t)s->align);
1419 PRT("entry: %ju\n", (uintmax_t)i);
1420 PRT("\tsh_name: %s\n", s->name);
1421 PRT("\tsh_type: %s\n",
1422 sh_types(ed->ehdr.e_machine, s->type));
1423 PRT("\tsh_flags: %s\n", sh_flags(s->flags));
1424 PRT("\tsh_addr: %#jx\n", (uintmax_t)s->addr);
1425 PRT("\tsh_offset: %ju\n", (uintmax_t)s->off);
1426 PRT("\tsh_size: %ju\n", (uintmax_t)s->sz);
1427 PRT("\tsh_link: %u\n", s->link);
1428 PRT("\tsh_info: %u\n", s->info);
1429 PRT("\tsh_addralign: %ju\n", (uintmax_t)s->align);
1430 PRT("\tsh_entsize: %ju\n", (uintmax_t)s->entsize);
1436 * Return number of entries in the given section. We'd prefer ent_count be a
1437 * size_t, but libelf APIs already use int for section indices.
1440 get_ent_count(const struct section *s, int *ent_count)
1442 if (s->entsize == 0) {
1443 warnx("section %s has entry size 0", s->name);
1445 } else if (s->sz / s->entsize > INT_MAX) {
1446 warnx("section %s has invalid section count", s->name);
1449 *ent_count = (int)(s->sz / s->entsize);
1454 * Retrieve the content of the corresponding SHT_SUNW_versym section for
1455 * a symbol table section.
1458 get_versym(struct elfdump *ed, int i, uint16_t **vs, int *nvs)
1466 for (j = 0; j < ed->shnum; j++) {
1468 if (s->type == SHT_SUNW_versym && s->link == (uint32_t)i)
1471 if (j >= ed->shnum) {
1476 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
1477 elferr = elf_errno();
1479 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
1485 assert(data->d_size == s->sz);
1486 if (!get_ent_count(s, nvs))
1491 * Dump the symbol table section.
1494 elf_print_symtab(struct elfdump *ed, int i)
1502 int len, j, elferr, nvs;
1505 if (ed->flags & SOLARIS_FMT)
1506 PRT("\nSymbol Table Section: %s\n", s->name);
1508 PRT("\nsymbol table (%s):\n", s->name);
1510 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
1511 elferr = elf_errno();
1513 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
1518 assert(data->d_size == s->sz);
1519 if (!get_ent_count(s, &len))
1521 if (ed->flags & SOLARIS_FMT) {
1522 if (ed->ec == ELFCLASS32)
1523 PRT(" index value ");
1525 PRT(" index value ");
1526 PRT("size type bind oth ver shndx name\n");
1527 get_versym(ed, i, &vs, &nvs);
1528 if (vs != NULL && nvs != len) {
1529 warnx("#symbol not equal to #versym");
1533 for (j = 0; j < len; j++) {
1534 if (gelf_getsym(data, j, &sym) != &sym) {
1535 warnx("gelf_getsym failed: %s", elf_errmsg(-1));
1538 name = get_string(ed, s->link, sym.st_name);
1539 if (ed->flags & SOLARIS_FMT) {
1540 snprintf(idx, sizeof(idx), "[%d]", j);
1541 if (ed->ec == ELFCLASS32)
1545 PRT("0x%8.8jx ", (uintmax_t)sym.st_value);
1546 if (ed->ec == ELFCLASS32)
1547 PRT("0x%8.8jx ", (uintmax_t)sym.st_size);
1549 PRT("0x%12.12jx ", (uintmax_t)sym.st_size);
1550 PRT("%s ", st_type_S(GELF_ST_TYPE(sym.st_info)));
1551 PRT("%s ", st_bindings_S(GELF_ST_BIND(sym.st_info)));
1552 PRT("%c ", st_others[sym.st_other]);
1553 PRT("%3u ", (vs == NULL ? 0 : vs[j]));
1554 PRT("%-11.11s ", sh_name(ed, sym.st_shndx));
1557 PRT("\nentry: %d\n", j);
1558 PRT("\tst_name: %s\n", name);
1559 PRT("\tst_value: %#jx\n", (uintmax_t)sym.st_value);
1560 PRT("\tst_size: %ju\n", (uintmax_t)sym.st_size);
1561 PRT("\tst_info: %s %s\n",
1562 st_type(ed->ehdr.e_machine,
1563 GELF_ST_TYPE(sym.st_info)),
1564 st_bindings(GELF_ST_BIND(sym.st_info)));
1565 PRT("\tst_shndx: %ju\n", (uintmax_t)sym.st_shndx);
1571 * Dump the symbol tables. (.dynsym and .symtab)
1574 elf_print_symtabs(struct elfdump *ed)
1578 for (i = 0; i < ed->shnum; i++)
1579 if ((ed->sl[i].type == SHT_SYMTAB ||
1580 ed->sl[i].type == SHT_DYNSYM) &&
1581 (STAILQ_EMPTY(&ed->snl) || find_name(ed, ed->sl[i].name)))
1582 elf_print_symtab(ed, i);
1586 * Dump the content of .dynamic section.
1589 elf_print_dynamic(struct elfdump *ed)
1599 for (i = 0; (size_t)i < ed->shnum; i++) {
1601 if (s->type == SHT_DYNAMIC &&
1602 (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name)))
1605 if ((size_t)i >= ed->shnum)
1608 if (ed->flags & SOLARIS_FMT) {
1609 PRT("Dynamic Section: %s\n", s->name);
1610 PRT(" index tag value\n");
1612 PRT("\ndynamic:\n");
1614 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
1615 elferr = elf_errno();
1617 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
1620 assert(data->d_size == s->sz);
1621 if (!get_ent_count(s, &len))
1623 for (i = 0; i < len; i++) {
1624 if (gelf_getdyn(data, i, &dyn) != &dyn) {
1625 warnx("gelf_getdyn failed: %s", elf_errmsg(-1));
1629 if (ed->flags & SOLARIS_FMT) {
1630 snprintf(idx, sizeof(idx), "[%d]", i);
1631 PRT("%10s %-16s ", idx, d_tags(dyn.d_tag));
1634 PRT("entry: %d\n", i);
1635 PRT("\td_tag: %s\n", d_tags(dyn.d_tag));
1642 if ((name = elf_strptr(ed->elf, s->link,
1643 dyn.d_un.d_val)) == NULL)
1645 if (ed->flags & SOLARIS_FMT)
1646 PRT("%#-16jx %s\n", (uintmax_t)dyn.d_un.d_val,
1649 PRT("\td_val: %s\n", name);
1666 if (ed->flags & SOLARIS_FMT)
1667 PRT("%#jx\n", (uintmax_t)dyn.d_un.d_val);
1669 PRT("\td_val: %ju\n",
1670 (uintmax_t)dyn.d_un.d_val);
1682 if (ed->flags & SOLARIS_FMT)
1683 PRT("%#jx\n", (uintmax_t)dyn.d_un.d_ptr);
1685 PRT("\td_ptr: %#jx\n",
1686 (uintmax_t)dyn.d_un.d_ptr);
1692 if (ed->flags & SOLARIS_FMT)
1700 * Dump a .rel/.rela section entry.
1703 elf_print_rel_entry(struct elfdump *ed, struct section *s, int j,
1704 struct rel_entry *r)
1707 if (ed->flags & SOLARIS_FMT) {
1708 PRT(" %-23s ", elftc_reloc_type_str(ed->ehdr.e_machine,
1709 GELF_R_TYPE(r->u_r.rel.r_info)));
1710 PRT("%#12jx ", (uintmax_t)r->u_r.rel.r_offset);
1711 if (r->type == SHT_RELA)
1712 PRT("%10jd ", (intmax_t)r->u_r.rela.r_addend);
1715 PRT("%-14s ", s->name);
1716 PRT("%s\n", r->symn);
1719 PRT("entry: %d\n", j);
1720 PRT("\tr_offset: %#jx\n", (uintmax_t)r->u_r.rel.r_offset);
1721 if (ed->ec == ELFCLASS32)
1722 PRT("\tr_info: %#jx\n", (uintmax_t)
1723 ELF32_R_INFO(ELF64_R_SYM(r->u_r.rel.r_info),
1724 ELF64_R_TYPE(r->u_r.rel.r_info)));
1726 PRT("\tr_info: %#jx\n", (uintmax_t)r->u_r.rel.r_info);
1727 if (r->type == SHT_RELA)
1728 PRT("\tr_addend: %jd\n",
1729 (intmax_t)r->u_r.rela.r_addend);
1734 * Dump a relocation section of type SHT_RELA.
1737 elf_print_rela(struct elfdump *ed, struct section *s, Elf_Data *data)
1742 if (ed->flags & SOLARIS_FMT) {
1743 PRT("\nRelocation Section: %s\n", s->name);
1745 "addend section with respect to\n");
1747 PRT("\nrelocation with addend (%s):\n", s->name);
1749 assert(data->d_size == s->sz);
1750 if (!get_ent_count(s, &len))
1752 for (j = 0; j < len; j++) {
1753 if (gelf_getrela(data, j, &r.u_r.rela) != &r.u_r.rela) {
1754 warnx("gelf_getrela failed: %s",
1758 r.symn = get_symbol_name(ed, s->link,
1759 GELF_R_SYM(r.u_r.rela.r_info));
1760 elf_print_rel_entry(ed, s, j, &r);
1765 * Dump a relocation section of type SHT_REL.
1768 elf_print_rel(struct elfdump *ed, struct section *s, Elf_Data *data)
1773 if (ed->flags & SOLARIS_FMT) {
1774 PRT("\nRelocation Section: %s\n", s->name);
1776 "section with respect to\n");
1778 PRT("\nrelocation (%s):\n", s->name);
1780 assert(data->d_size == s->sz);
1781 if (!get_ent_count(s, &len))
1783 for (j = 0; j < len; j++) {
1784 if (gelf_getrel(data, j, &r.u_r.rel) != &r.u_r.rel) {
1785 warnx("gelf_getrel failed: %s", elf_errmsg(-1));
1788 r.symn = get_symbol_name(ed, s->link,
1789 GELF_R_SYM(r.u_r.rel.r_info));
1790 elf_print_rel_entry(ed, s, j, &r);
1795 * Dump relocation sections.
1798 elf_print_reloc(struct elfdump *ed)
1805 for (i = 0; i < ed->shnum; i++) {
1807 if ((s->type == SHT_REL || s->type == SHT_RELA) &&
1808 (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name))) {
1810 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
1811 elferr = elf_errno();
1813 warnx("elf_getdata failed: %s",
1814 elf_errmsg(elferr));
1817 if (s->type == SHT_REL)
1818 elf_print_rel(ed, s, data);
1820 elf_print_rela(ed, s, data);
1826 * Dump the content of PT_INTERP segment.
1829 elf_print_interp(struct elfdump *ed)
1833 size_t filesize, i, phnum;
1835 if (!STAILQ_EMPTY(&ed->snl) && find_name(ed, "PT_INTERP") == NULL)
1838 if ((s = elf_rawfile(ed->elf, &filesize)) == NULL) {
1839 warnx("elf_rawfile failed: %s", elf_errmsg(-1));
1842 if (!elf_getphnum(ed->elf, &phnum)) {
1843 warnx("elf_getphnum failed: %s", elf_errmsg(-1));
1846 for (i = 0; i < phnum; i++) {
1847 if (gelf_getphdr(ed->elf, i, &phdr) != &phdr) {
1848 warnx("elf_getphdr failed: %s", elf_errmsg(-1));
1851 if (phdr.p_type == PT_INTERP) {
1852 if (phdr.p_offset >= filesize) {
1853 warnx("invalid phdr offset");
1857 PRT("\t%s\n", s + phdr.p_offset);
1863 * Search the relocation sections for entries referring to the .got section.
1866 find_gotrel(struct elfdump *ed, struct section *gs, struct rel_entry *got)
1872 int elferr, j, k, len;
1874 for(i = 0; i < ed->shnum; i++) {
1876 if (s->type != SHT_REL && s->type != SHT_RELA)
1879 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
1880 elferr = elf_errno();
1882 warnx("elf_getdata failed: %s",
1883 elf_errmsg(elferr));
1886 memset(&r, 0, sizeof(struct rel_entry));
1888 assert(data->d_size == s->sz);
1889 if (!get_ent_count(s, &len))
1891 for (j = 0; j < len; j++) {
1892 if (s->type == SHT_REL) {
1893 if (gelf_getrel(data, j, &r.u_r.rel) !=
1895 warnx("gelf_getrel failed: %s",
1900 if (gelf_getrela(data, j, &r.u_r.rela) !=
1902 warnx("gelf_getrel failed: %s",
1907 if (r.u_r.rel.r_offset >= gs->addr &&
1908 r.u_r.rel.r_offset < gs->addr + gs->sz) {
1909 r.symn = get_symbol_name(ed, s->link,
1910 GELF_R_SYM(r.u_r.rel.r_info));
1911 k = (r.u_r.rel.r_offset - gs->addr) /
1913 memcpy(&got[k], &r, sizeof(struct rel_entry));
1920 elf_print_got_section(struct elfdump *ed, struct section *s)
1922 struct rel_entry *got;
1923 Elf_Data *data, dst;
1926 if (s->entsize == 0) {
1927 /* XXX IA64 GOT section generated by gcc has entry size 0. */
1929 s->entsize = s->align;
1934 if (!get_ent_count(s, &len))
1936 if (ed->flags & SOLARIS_FMT)
1937 PRT("\nGlobal Offset Table Section: %s (%d entries)\n",
1940 PRT("\nglobal offset table: %s\n", s->name);
1942 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
1943 elferr = elf_errno();
1945 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
1950 * GOT section has section type SHT_PROGBITS, thus libelf treats it as
1951 * byte stream and will not perform any translation on it. As a result,
1952 * an exlicit call to gelf_xlatetom is needed here. Depends on arch,
1953 * GOT section should be translated to either WORD or XWORD.
1955 if (ed->ec == ELFCLASS32)
1956 data->d_type = ELF_T_WORD;
1958 data->d_type = ELF_T_XWORD;
1959 memcpy(&dst, data, sizeof(Elf_Data));
1960 if (gelf_xlatetom(ed->elf, &dst, data, ed->ehdr.e_ident[EI_DATA]) !=
1962 warnx("gelf_xlatetom failed: %s", elf_errmsg(-1));
1965 assert(dst.d_size == s->sz);
1966 if (ed->flags & SOLARIS_FMT) {
1968 * In verbose/Solaris mode, we search the relocation sections
1969 * and try to find the corresponding reloc entry for each GOT
1972 if ((got = calloc(len, sizeof(struct rel_entry))) == NULL)
1973 err(EXIT_FAILURE, "calloc failed");
1974 find_gotrel(ed, s, got);
1975 if (ed->ec == ELFCLASS32) {
1976 PRT(" ndx addr value reloc ");
1977 PRT("addend symbol\n");
1979 PRT(" ndx addr value ");
1980 PRT("reloc addend symbol\n");
1982 for(i = 0; i < len; i++) {
1984 if (ed->ec == ELFCLASS32) {
1986 (uintmax_t) (s->addr + i * s->entsize));
1987 PRT("%-8.8x ", *((uint32_t *)dst.d_buf + i));
1990 (uintmax_t) (s->addr + i * s->entsize));
1992 (uintmax_t) *((uint64_t *)dst.d_buf + i));
1994 PRT("%-18s ", elftc_reloc_type_str(ed->ehdr.e_machine,
1995 GELF_R_TYPE(got[i].u_r.rel.r_info)));
1996 if (ed->ec == ELFCLASS32)
1998 (intmax_t)got[i].u_r.rela.r_addend);
2001 (intmax_t)got[i].u_r.rela.r_addend);
2002 if (got[i].symn == NULL)
2004 PRT("%s\n", got[i].symn);
2008 for(i = 0; i < len; i++) {
2009 PRT("\nentry: %d\n", i);
2010 if (ed->ec == ELFCLASS32)
2011 PRT("\t%#x\n", *((uint32_t *)dst.d_buf + i));
2014 (uintmax_t) *((uint64_t *)dst.d_buf + i));
2020 * Dump the content of Global Offset Table section.
2023 elf_print_got(struct elfdump *ed)
2028 if (!STAILQ_EMPTY(&ed->snl))
2032 for (i = 0; i < ed->shnum; i++) {
2034 if (s->name && !strncmp(s->name, ".got", 4) &&
2035 (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name)))
2036 elf_print_got_section(ed, s);
2041 * Dump the content of .note.ABI-tag section.
2044 elf_print_note(struct elfdump *ed)
2058 for (i = 0; (size_t)i < ed->shnum; i++) {
2060 if (s->type == SHT_NOTE && s->name &&
2061 !strcmp(s->name, ".note.ABI-tag") &&
2062 (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name)))
2065 if ((size_t)i >= ed->shnum)
2067 if (ed->flags & SOLARIS_FMT)
2068 PRT("\nNote Section: %s\n", s->name);
2070 PRT("\nnote (%s):\n", s->name);
2072 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
2073 elferr = elf_errno();
2075 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
2079 count = data->d_size;
2080 while (count > sizeof(Elf_Note)) {
2081 en = (Elf_Note *) (uintptr_t) src;
2082 namesz = en->n_namesz;
2083 descsz = en->n_descsz;
2084 src += sizeof(Elf_Note);
2085 count -= sizeof(Elf_Note);
2086 if (roundup2(namesz, 4) + roundup2(descsz, 4) > count) {
2087 warnx("truncated note section");
2090 if (ed->flags & SOLARIS_FMT) {
2091 PRT("\n type %#x\n", en->n_type);
2092 PRT(" namesz %#x:\n", en->n_namesz);
2096 src += roundup2(namesz, 4);
2097 count -= roundup2(namesz, 4);
2100 * Note that we dump the whole desc part if we're in
2101 * "Solaris mode", while in the normal mode, we only look
2102 * at the first 4 bytes (a 32bit word) of the desc, i.e,
2103 * we assume that it's always a FreeBSD version number.
2105 if (ed->flags & SOLARIS_FMT) {
2106 PRT(" descsz %#x:", en->n_descsz);
2107 for (i = 0; (uint32_t)i < descsz; i++) {
2108 if ((i & 0xF) == 0) {
2109 snprintf(idx, sizeof(idx), "desc[%d]",
2111 PRT("\n %-9s", idx);
2112 } else if ((i & 0x3) == 0)
2114 PRT(" %2.2x", src[i]);
2118 if (ed->ehdr.e_ident[EI_DATA] == ELFDATA2MSB)
2119 desc = be32dec(src);
2121 desc = le32dec(src);
2124 src += roundup2(descsz, 4);
2125 count -= roundup2(descsz, 4);
2130 * Dump a hash table.
2133 elf_print_svr4_hash(struct elfdump *ed, struct section *s)
2137 uint32_t *bucket, *chain;
2138 uint32_t nbucket, nchain;
2139 uint32_t *bl, *c, maxl, total;
2144 if (ed->flags & SOLARIS_FMT)
2145 PRT("\nHash Section: %s\n", s->name);
2147 PRT("\nhash table (%s):\n", s->name);
2149 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
2150 elferr = elf_errno();
2152 warnx("elf_getdata failed: %s",
2153 elf_errmsg(elferr));
2156 if (data->d_size < 2 * sizeof(uint32_t)) {
2157 warnx(".hash section too small");
2163 if (nbucket <= 0 || nchain <= 0) {
2164 warnx("Malformed .hash section");
2168 ((uint64_t)nbucket + (uint64_t)nchain + 2) * sizeof(uint32_t)) {
2169 warnx("Malformed .hash section");
2173 chain = &buf[2 + nbucket];
2175 if (ed->flags & SOLARIS_FMT) {
2177 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
2178 err(EXIT_FAILURE, "calloc failed");
2179 for (i = 0; i < nbucket; i++)
2180 for (j = bucket[i]; j > 0 && j < nchain; j = chain[j])
2183 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
2184 err(EXIT_FAILURE, "calloc failed");
2185 for (i = 0; i < nbucket; i++)
2187 PRT(" bucket symndx name\n");
2188 for (i = 0; i < nbucket; i++) {
2190 for (j = bucket[i]; j > 0 && j < nchain; j = chain[j]) {
2196 snprintf(idx, sizeof(idx), "[%d]", j);
2198 PRT("%s\n", get_symbol_name(ed, s->link, j));
2203 for (i = 0; i <= maxl; i++) {
2205 PRT("%10u buckets contain %8d symbols\n", c[i], i);
2207 PRT("%10u buckets %8u symbols (globals)\n", nbucket,
2210 PRT("\nnbucket: %u\n", nbucket);
2211 PRT("nchain: %u\n\n", nchain);
2212 for (i = 0; i < nbucket; i++)
2213 PRT("bucket[%d]:\n\t%u\n\n", i, bucket[i]);
2214 for (i = 0; i < nchain; i++)
2215 PRT("chain[%d]:\n\t%u\n\n", i, chain[i]);
2220 * Dump a 64bit hash table.
2223 elf_print_svr4_hash64(struct elfdump *ed, struct section *s)
2225 Elf_Data *data, dst;
2227 uint64_t *bucket, *chain;
2228 uint64_t nbucket, nchain;
2229 uint64_t *bl, *c, maxl, total;
2234 if (ed->flags & SOLARIS_FMT)
2235 PRT("\nHash Section: %s\n", s->name);
2237 PRT("\nhash table (%s):\n", s->name);
2240 * ALPHA uses 64-bit hash entries. Since libelf assumes that
2241 * .hash section contains only 32-bit entry, an explicit
2242 * gelf_xlatetom is needed here.
2245 if ((data = elf_rawdata(s->scn, NULL)) == NULL) {
2246 elferr = elf_errno();
2248 warnx("elf_rawdata failed: %s",
2249 elf_errmsg(elferr));
2252 data->d_type = ELF_T_XWORD;
2253 memcpy(&dst, data, sizeof(Elf_Data));
2254 if (gelf_xlatetom(ed->elf, &dst, data,
2255 ed->ehdr.e_ident[EI_DATA]) != &dst) {
2256 warnx("gelf_xlatetom failed: %s", elf_errmsg(-1));
2259 if (dst.d_size < 2 * sizeof(uint64_t)) {
2260 warnx(".hash section too small");
2266 if (nbucket <= 0 || nchain <= 0) {
2267 warnx("Malformed .hash section");
2270 if (dst.d_size != (nbucket + nchain + 2) * sizeof(uint64_t)) {
2271 warnx("Malformed .hash section");
2275 chain = &buf[2 + nbucket];
2277 if (ed->flags & SOLARIS_FMT) {
2279 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
2280 err(EXIT_FAILURE, "calloc failed");
2281 for (i = 0; i < nbucket; i++)
2282 for (j = bucket[i]; j > 0 && j < nchain; j = chain[j])
2285 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
2286 err(EXIT_FAILURE, "calloc failed");
2287 for (i = 0; i < nbucket; i++)
2289 PRT(" bucket symndx name\n");
2290 for (i = 0; i < nbucket; i++) {
2292 for (j = bucket[i]; j > 0 && j < nchain; j = chain[j]) {
2298 snprintf(idx, sizeof(idx), "[%zu]", (size_t)j);
2300 PRT("%s\n", get_symbol_name(ed, s->link, j));
2305 for (i = 0; i <= maxl; i++) {
2307 PRT("%10ju buckets contain %8zu symbols\n",
2308 (uintmax_t)c[i], i);
2310 PRT("%10ju buckets %8ju symbols (globals)\n",
2311 (uintmax_t)nbucket, (uintmax_t)total);
2313 PRT("\nnbucket: %ju\n", (uintmax_t)nbucket);
2314 PRT("nchain: %ju\n\n", (uintmax_t)nchain);
2315 for (i = 0; i < nbucket; i++)
2316 PRT("bucket[%zu]:\n\t%ju\n\n", i, (uintmax_t)bucket[i]);
2317 for (i = 0; i < nchain; i++)
2318 PRT("chain[%zu]:\n\t%ju\n\n", i, (uintmax_t)chain[i]);
2324 * Dump a GNU hash table.
2327 elf_print_gnu_hash(struct elfdump *ed, struct section *s)
2332 uint32_t *bucket, *chain;
2333 uint32_t nbucket, nchain, symndx, maskwords, shift2;
2334 uint32_t *bl, *c, maxl, total;
2336 int first, elferr, dynsymcount;
2339 if (ed->flags & SOLARIS_FMT)
2340 PRT("\nGNU Hash Section: %s\n", s->name);
2342 PRT("\ngnu hash table (%s):\n", s->name);
2344 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
2345 elferr = elf_errno();
2347 warnx("elf_getdata failed: %s",
2348 elf_errmsg(elferr));
2351 if (data->d_size < 4 * sizeof(uint32_t)) {
2352 warnx(".gnu.hash section too small");
2361 if (s->link >= ed->shnum) {
2362 warnx("Malformed .gnu.hash section");
2365 ds = &ed->sl[s->link];
2366 if (!get_ent_count(ds, &dynsymcount))
2368 if (symndx >= (uint32_t)dynsymcount) {
2369 warnx("Malformed .gnu.hash section");
2372 nchain = dynsymcount - symndx;
2373 if (data->d_size != 4 * sizeof(uint32_t) + maskwords *
2374 (ed->ec == ELFCLASS32 ? sizeof(uint32_t) : sizeof(uint64_t)) +
2375 ((uint64_t)nbucket + (uint64_t)nchain) * sizeof(uint32_t)) {
2376 warnx("Malformed .gnu.hash section");
2379 bucket = buf + (ed->ec == ELFCLASS32 ? maskwords : maskwords * 2);
2380 chain = bucket + nbucket;
2382 if (ed->flags & SOLARIS_FMT) {
2384 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
2385 err(EXIT_FAILURE, "calloc failed");
2386 for (i = 0; i < nbucket; i++)
2387 for (j = bucket[i]; j > 0 && j - symndx < nchain; j++) {
2390 if (chain[j - symndx] & 1)
2393 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
2394 err(EXIT_FAILURE, "calloc failed");
2395 for (i = 0; i < nbucket; i++)
2397 PRT(" bucket symndx name\n");
2398 for (i = 0; i < nbucket; i++) {
2400 for (j = bucket[i]; j > 0 && j - symndx < nchain; j++) {
2406 snprintf(idx, sizeof(idx), "[%d]", j );
2408 PRT("%s\n", get_symbol_name(ed, s->link, j));
2409 if (chain[j - symndx] & 1)
2415 for (i = 0; i <= maxl; i++) {
2417 PRT("%10u buckets contain %8d symbols\n", c[i], i);
2419 PRT("%10u buckets %8u symbols (globals)\n", nbucket,
2422 PRT("\nnbucket: %u\n", nbucket);
2423 PRT("symndx: %u\n", symndx);
2424 PRT("maskwords: %u\n", maskwords);
2425 PRT("shift2: %u\n", shift2);
2426 PRT("nchain: %u\n\n", nchain);
2427 for (i = 0; i < nbucket; i++)
2428 PRT("bucket[%d]:\n\t%u\n\n", i, bucket[i]);
2429 for (i = 0; i < nchain; i++)
2430 PRT("chain[%d]:\n\t%u\n\n", i, chain[i]);
2438 elf_print_hash(struct elfdump *ed)
2443 for (i = 0; i < ed->shnum; i++) {
2445 if ((s->type == SHT_HASH || s->type == SHT_GNU_HASH) &&
2446 (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name))) {
2447 if (s->type == SHT_GNU_HASH)
2448 elf_print_gnu_hash(ed, s);
2449 else if (ed->ehdr.e_machine == EM_ALPHA &&
2451 elf_print_svr4_hash64(ed, s);
2453 elf_print_svr4_hash(ed, s);
2459 * Dump the content of a Version Definition(SHT_SUNW_Verdef) Section.
2462 elf_print_verdef(struct elfdump *ed, struct section *s)
2469 uint8_t *buf, *end, *buf2;
2470 int i, j, elferr, count;
2472 if (ed->flags & SOLARIS_FMT)
2473 PRT("Version Definition Section: %s\n", s->name);
2475 PRT("\nversion definition section (%s):\n", s->name);
2477 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
2478 elferr = elf_errno();
2480 warnx("elf_getdata failed: %s",
2481 elf_errmsg(elferr));
2485 end = buf + data->d_size;
2487 if (ed->flags & SOLARIS_FMT)
2488 PRT(" index version dependency\n");
2489 while (buf + sizeof(Elf32_Verdef) <= end) {
2490 vd = (Elf32_Verdef *) (uintptr_t) buf;
2491 if (ed->flags & SOLARIS_FMT) {
2492 snprintf(idx, sizeof(idx), "[%d]", vd->vd_ndx);
2495 PRT("\nentry: %d\n", i++);
2496 PRT("\tvd_version: %u\n", vd->vd_version);
2497 PRT("\tvd_flags: %u\n", vd->vd_flags);
2498 PRT("\tvd_ndx: %u\n", vd->vd_ndx);
2499 PRT("\tvd_cnt: %u\n", vd->vd_cnt);
2500 PRT("\tvd_hash: %u\n", vd->vd_hash);
2501 PRT("\tvd_aux: %u\n", vd->vd_aux);
2502 PRT("\tvd_next: %u\n\n", vd->vd_next);
2504 buf2 = buf + vd->vd_aux;
2507 while (buf2 + sizeof(Elf32_Verdaux) <= end && j < vd->vd_cnt) {
2508 vda = (Elf32_Verdaux *) (uintptr_t) buf2;
2509 str = get_string(ed, s->link, vda->vda_name);
2510 if (ed->flags & SOLARIS_FMT) {
2512 PRT("%-26.26s", str);
2513 else if (count == 1)
2514 PRT(" %-20.20s", str);
2516 PRT("\n%40.40s", "");
2520 PRT("\t\tvda: %d\n", j++);
2521 PRT("\t\t\tvda_name: %s\n", str);
2522 PRT("\t\t\tvda_next: %u\n", vda->vda_next);
2524 if (vda->vda_next == 0) {
2525 if (ed->flags & SOLARIS_FMT) {
2526 if (vd->vd_flags & VER_FLG_BASE) {
2528 PRT("%-20.20s", "");
2529 PRT("%s", "[ BASE ]");
2535 if (ed->flags & SOLARIS_FMT)
2537 buf2 += vda->vda_next;
2539 if (vd->vd_next == 0)
2546 * Dump the content of a Version Needed(SHT_SUNW_Verneed) Section.
2549 elf_print_verneed(struct elfdump *ed, struct section *s)
2554 uint8_t *buf, *end, *buf2;
2555 int i, j, elferr, first;
2557 if (ed->flags & SOLARIS_FMT)
2558 PRT("\nVersion Needed Section: %s\n", s->name);
2560 PRT("\nversion need section (%s):\n", s->name);
2562 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
2563 elferr = elf_errno();
2565 warnx("elf_getdata failed: %s",
2566 elf_errmsg(elferr));
2570 end = buf + data->d_size;
2571 if (ed->flags & SOLARIS_FMT)
2572 PRT(" file version\n");
2574 while (buf + sizeof(Elf32_Verneed) <= end) {
2575 vn = (Elf32_Verneed *) (uintptr_t) buf;
2576 if (ed->flags & SOLARIS_FMT)
2578 get_string(ed, s->link, vn->vn_file));
2580 PRT("\nentry: %d\n", i++);
2581 PRT("\tvn_version: %u\n", vn->vn_version);
2582 PRT("\tvn_cnt: %u\n", vn->vn_cnt);
2583 PRT("\tvn_file: %s\n",
2584 get_string(ed, s->link, vn->vn_file));
2585 PRT("\tvn_aux: %u\n", vn->vn_aux);
2586 PRT("\tvn_next: %u\n\n", vn->vn_next);
2588 buf2 = buf + vn->vn_aux;
2591 while (buf2 + sizeof(Elf32_Vernaux) <= end && j < vn->vn_cnt) {
2592 vna = (Elf32_Vernaux *) (uintptr_t) buf2;
2593 if (ed->flags & SOLARIS_FMT) {
2598 PRT("%s\n", get_string(ed, s->link,
2601 PRT("\t\tvna: %d\n", j++);
2602 PRT("\t\t\tvna_hash: %u\n", vna->vna_hash);
2603 PRT("\t\t\tvna_flags: %u\n", vna->vna_flags);
2604 PRT("\t\t\tvna_other: %u\n", vna->vna_other);
2605 PRT("\t\t\tvna_name: %s\n",
2606 get_string(ed, s->link, vna->vna_name));
2607 PRT("\t\t\tvna_next: %u\n", vna->vna_next);
2609 if (vna->vna_next == 0)
2611 buf2 += vna->vna_next;
2613 if (vn->vn_next == 0)
2620 * Dump the symbol-versioning sections.
2623 elf_print_symver(struct elfdump *ed)
2628 for (i = 0; i < ed->shnum; i++) {
2630 if (!STAILQ_EMPTY(&ed->snl) && !find_name(ed, s->name))
2632 if (s->type == SHT_SUNW_verdef)
2633 elf_print_verdef(ed, s);
2634 if (s->type == SHT_SUNW_verneed)
2635 elf_print_verneed(ed, s);
2640 * Dump the ELF checksum. See gelf_checksum(3) for details.
2643 elf_print_checksum(struct elfdump *ed)
2646 if (!STAILQ_EMPTY(&ed->snl))
2649 PRT("\nelf checksum: %#lx\n", gelf_checksum(ed->elf));
2652 #define USAGE_MESSAGE "\
2653 Usage: %s [options] file...\n\
2654 Display information about ELF objects and ar(1) archives.\n\n\
2656 -a Show all information.\n\
2657 -c Show shared headers.\n\
2658 -d Show dynamic symbols.\n\
2659 -e Show the ELF header.\n\
2661 -H | --help Show a usage message and exit.\n\
2662 -h Show hash values.\n\
2663 -i Show the dynamic interpreter.\n\
2664 -k Show the ELF checksum.\n\
2665 -n Show the contents of note sections.\n\
2666 -N NAME Show the section named \"NAME\".\n\
2667 -p Show the program header.\n\
2668 -r Show relocations.\n\
2669 -s Show the symbol table.\n\
2670 -S Use the Solaris elfdump format.\n\
2671 -v Show symbol-versioning information.\n\
2672 -V | --version Print a version identifier and exit.\n\
2673 -w FILE Write output to \"FILE\".\n"
2678 fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());