2 * Copyright (c) 2009-2015 Kai Wang
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/param.h>
28 #include <sys/queue.h>
50 ELFTC_VCSID("$Id: readelf.c 3649 2018-11-24 03:26:23Z emaste $");
52 /* Backwards compatability for older FreeBSD releases. */
53 #ifndef STB_GNU_UNIQUE
54 #define STB_GNU_UNIQUE 10
56 #ifndef STT_SPARC_REGISTER
57 #define STT_SPARC_REGISTER 13
64 #define RE_AA 0x00000001
65 #define RE_C 0x00000002
66 #define RE_DD 0x00000004
67 #define RE_D 0x00000008
68 #define RE_G 0x00000010
69 #define RE_H 0x00000020
70 #define RE_II 0x00000040
71 #define RE_I 0x00000080
72 #define RE_L 0x00000100
73 #define RE_NN 0x00000200
74 #define RE_N 0x00000400
75 #define RE_P 0x00000800
76 #define RE_R 0x00001000
77 #define RE_SS 0x00002000
78 #define RE_S 0x00004000
79 #define RE_T 0x00008000
80 #define RE_U 0x00010000
81 #define RE_VV 0x00020000
82 #define RE_WW 0x00040000
83 #define RE_W 0x00080000
84 #define RE_X 0x00100000
89 #define DW_A 0x00000001
90 #define DW_FF 0x00000002
91 #define DW_F 0x00000004
92 #define DW_I 0x00000008
93 #define DW_LL 0x00000010
94 #define DW_L 0x00000020
95 #define DW_M 0x00000040
96 #define DW_O 0x00000080
97 #define DW_P 0x00000100
98 #define DW_RR 0x00000200
99 #define DW_R 0x00000400
100 #define DW_S 0x00000800
102 #define DW_DEFAULT_OPTIONS (DW_A | DW_F | DW_I | DW_L | DW_O | DW_P | \
106 * readelf(1) run control flags.
108 #define DISPLAY_FILENAME 0x0001
111 * Internal data structure for sections.
114 const char *name; /* section name */
115 Elf_Scn *scn; /* section scn */
116 uint64_t off; /* section offset */
117 uint64_t sz; /* section size */
118 uint64_t entsize; /* section entsize */
119 uint64_t align; /* section alignment */
120 uint64_t type; /* section type */
121 uint64_t flags; /* section flags */
122 uint64_t addr; /* section virtual addr */
123 uint32_t link; /* section link ndx */
124 uint32_t info; /* section info ndx */
129 size_t si; /* section index */
130 const char *sn; /* section name */
135 } type; /* dump type */
136 #define HEX_DUMP 0x0001
137 #define STR_DUMP 0x0002
138 int op; /* dump operation */
139 STAILQ_ENTRY(dumpop) dumpop_list;
148 * Structure encapsulates the global data for readelf(1).
151 const char *filename; /* current processing file. */
152 int options; /* command line options. */
153 int flags; /* run control flags. */
154 int dop; /* dwarf dump options. */
155 Elf *elf; /* underlying ELF descriptor. */
156 Elf *ar; /* archive ELF descriptor. */
157 Dwarf_Debug dbg; /* DWARF handle. */
158 Dwarf_Half cu_psize; /* DWARF CU pointer size. */
159 Dwarf_Half cu_osize; /* DWARF CU offset size. */
160 Dwarf_Half cu_ver; /* DWARF CU version. */
161 GElf_Ehdr ehdr; /* ELF header. */
162 int ec; /* ELF class. */
163 size_t shnum; /* #sections. */
164 struct section *vd_s; /* Verdef section. */
165 struct section *vn_s; /* Verneed section. */
166 struct section *vs_s; /* Versym section. */
167 uint16_t *vs; /* Versym array. */
168 int vs_sz; /* Versym array size. */
169 struct symver *ver; /* Version array. */
170 int ver_sz; /* Size of version array. */
171 struct section *sl; /* list of sections. */
172 STAILQ_HEAD(, dumpop) v_dumpop; /* list of dump ops. */
173 uint64_t (*dw_read)(Elf_Data *, uint64_t *, int);
174 uint64_t (*dw_decode)(uint8_t **, int);
182 static struct option longopts[] = {
183 {"all", no_argument, NULL, 'a'},
184 {"arch-specific", no_argument, NULL, 'A'},
185 {"archive-index", no_argument, NULL, 'c'},
186 {"debug-dump", optional_argument, NULL, OPTION_DEBUG_DUMP},
187 {"dynamic", no_argument, NULL, 'd'},
188 {"file-header", no_argument, NULL, 'h'},
189 {"full-section-name", no_argument, NULL, 'N'},
190 {"headers", no_argument, NULL, 'e'},
191 {"help", no_argument, 0, 'H'},
192 {"hex-dump", required_argument, NULL, 'x'},
193 {"histogram", no_argument, NULL, 'I'},
194 {"notes", no_argument, NULL, 'n'},
195 {"program-headers", no_argument, NULL, 'l'},
196 {"relocs", no_argument, NULL, 'r'},
197 {"sections", no_argument, NULL, 'S'},
198 {"section-headers", no_argument, NULL, 'S'},
199 {"section-groups", no_argument, NULL, 'g'},
200 {"section-details", no_argument, NULL, 't'},
201 {"segments", no_argument, NULL, 'l'},
202 {"string-dump", required_argument, NULL, 'p'},
203 {"symbols", no_argument, NULL, 's'},
204 {"syms", no_argument, NULL, 's'},
205 {"unwind", no_argument, NULL, 'u'},
206 {"use-dynamic", no_argument, NULL, 'D'},
207 {"version-info", no_argument, 0, 'V'},
208 {"version", no_argument, 0, 'v'},
209 {"wide", no_argument, 0, 'W'},
229 Dwarf_Attribute la_at;
230 Dwarf_Unsigned la_off;
231 Dwarf_Unsigned la_lowpc;
232 Dwarf_Half la_cu_psize;
233 Dwarf_Half la_cu_osize;
234 Dwarf_Half la_cu_ver;
237 static void add_dumpop(struct readelf *re, size_t si, const char *sn, int op,
239 static const char *aeabi_adv_simd_arch(uint64_t simd);
240 static const char *aeabi_align_needed(uint64_t an);
241 static const char *aeabi_align_preserved(uint64_t ap);
242 static const char *aeabi_arm_isa(uint64_t ai);
243 static const char *aeabi_cpu_arch(uint64_t arch);
244 static const char *aeabi_cpu_arch_profile(uint64_t pf);
245 static const char *aeabi_div(uint64_t du);
246 static const char *aeabi_enum_size(uint64_t es);
247 static const char *aeabi_fp_16bit_format(uint64_t fp16);
248 static const char *aeabi_fp_arch(uint64_t fp);
249 static const char *aeabi_fp_denormal(uint64_t fd);
250 static const char *aeabi_fp_exceptions(uint64_t fe);
251 static const char *aeabi_fp_hpext(uint64_t fh);
252 static const char *aeabi_fp_number_model(uint64_t fn);
253 static const char *aeabi_fp_optm_goal(uint64_t fog);
254 static const char *aeabi_fp_rounding(uint64_t fr);
255 static const char *aeabi_hardfp(uint64_t hfp);
256 static const char *aeabi_mpext(uint64_t mp);
257 static const char *aeabi_optm_goal(uint64_t og);
258 static const char *aeabi_pcs_config(uint64_t pcs);
259 static const char *aeabi_pcs_got(uint64_t got);
260 static const char *aeabi_pcs_r9(uint64_t r9);
261 static const char *aeabi_pcs_ro(uint64_t ro);
262 static const char *aeabi_pcs_rw(uint64_t rw);
263 static const char *aeabi_pcs_wchar_t(uint64_t wt);
264 static const char *aeabi_t2ee(uint64_t t2ee);
265 static const char *aeabi_thumb_isa(uint64_t ti);
266 static const char *aeabi_fp_user_exceptions(uint64_t fu);
267 static const char *aeabi_unaligned_access(uint64_t ua);
268 static const char *aeabi_vfp_args(uint64_t va);
269 static const char *aeabi_virtual(uint64_t vt);
270 static const char *aeabi_wmmx_arch(uint64_t wmmx);
271 static const char *aeabi_wmmx_args(uint64_t wa);
272 static const char *elf_class(unsigned int class);
273 static const char *elf_endian(unsigned int endian);
274 static const char *elf_machine(unsigned int mach);
275 static const char *elf_osabi(unsigned int abi);
276 static const char *elf_type(unsigned int type);
277 static const char *elf_ver(unsigned int ver);
278 static const char *dt_type(unsigned int mach, unsigned int dtype);
279 static void dump_ar(struct readelf *re, int);
280 static void dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe);
281 static void dump_attributes(struct readelf *re);
282 static uint8_t *dump_compatibility_tag(uint8_t *p, uint8_t *pe);
283 static void dump_dwarf(struct readelf *re);
284 static void dump_dwarf_abbrev(struct readelf *re);
285 static void dump_dwarf_aranges(struct readelf *re);
286 static void dump_dwarf_block(struct readelf *re, uint8_t *b,
288 static void dump_dwarf_die(struct readelf *re, Dwarf_Die die, int level);
289 static void dump_dwarf_frame(struct readelf *re, int alt);
290 static void dump_dwarf_frame_inst(struct readelf *re, Dwarf_Cie cie,
291 uint8_t *insts, Dwarf_Unsigned len, Dwarf_Unsigned caf, Dwarf_Signed daf,
292 Dwarf_Addr pc, Dwarf_Debug dbg);
293 static int dump_dwarf_frame_regtable(struct readelf *re, Dwarf_Fde fde,
294 Dwarf_Addr pc, Dwarf_Unsigned func_len, Dwarf_Half cie_ra);
295 static void dump_dwarf_frame_section(struct readelf *re, struct section *s,
297 static void dump_dwarf_info(struct readelf *re, Dwarf_Bool is_info);
298 static void dump_dwarf_macinfo(struct readelf *re);
299 static void dump_dwarf_line(struct readelf *re);
300 static void dump_dwarf_line_decoded(struct readelf *re);
301 static void dump_dwarf_loc(struct readelf *re, Dwarf_Loc *lr);
302 static void dump_dwarf_loclist(struct readelf *re);
303 static void dump_dwarf_pubnames(struct readelf *re);
304 static void dump_dwarf_ranges(struct readelf *re);
305 static void dump_dwarf_ranges_foreach(struct readelf *re, Dwarf_Die die,
307 static void dump_dwarf_str(struct readelf *re);
308 static void dump_eflags(struct readelf *re, uint64_t e_flags);
309 static void dump_elf(struct readelf *re);
310 static void dump_flags(struct flag_desc *fd, uint64_t flags);
311 static void dump_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab);
312 static void dump_dynamic(struct readelf *re);
313 static void dump_liblist(struct readelf *re);
314 static void dump_mips_abiflags(struct readelf *re, struct section *s);
315 static void dump_mips_attributes(struct readelf *re, uint8_t *p, uint8_t *pe);
316 static void dump_mips_odk_reginfo(struct readelf *re, uint8_t *p, size_t sz);
317 static void dump_mips_options(struct readelf *re, struct section *s);
318 static void dump_mips_option_flags(const char *name, struct mips_option *opt,
320 static void dump_mips_reginfo(struct readelf *re, struct section *s);
321 static void dump_mips_specific_info(struct readelf *re);
322 static void dump_notes(struct readelf *re);
323 static void dump_notes_content(struct readelf *re, const char *buf, size_t sz,
325 static void dump_notes_data(const char *name, uint32_t type, const char *buf,
327 static void dump_svr4_hash(struct section *s);
328 static void dump_svr4_hash64(struct readelf *re, struct section *s);
329 static void dump_gnu_hash(struct readelf *re, struct section *s);
330 static void dump_hash(struct readelf *re);
331 static void dump_phdr(struct readelf *re);
332 static void dump_ppc_attributes(uint8_t *p, uint8_t *pe);
333 static void dump_section_groups(struct readelf *re);
334 static void dump_symtab(struct readelf *re, int i);
335 static void dump_symtabs(struct readelf *re);
336 static uint8_t *dump_unknown_tag(uint64_t tag, uint8_t *p, uint8_t *pe);
337 static void dump_ver(struct readelf *re);
338 static void dump_verdef(struct readelf *re, int dump);
339 static void dump_verneed(struct readelf *re, int dump);
340 static void dump_versym(struct readelf *re);
341 static const char *dwarf_reg(unsigned int mach, unsigned int reg);
342 static const char *dwarf_regname(struct readelf *re, unsigned int num);
343 static struct dumpop *find_dumpop(struct readelf *re, size_t si,
344 const char *sn, int op, int t);
345 static int get_ent_count(struct section *s, int *ent_count);
346 static int get_mips_register_size(uint8_t flag);
347 static char *get_regoff_str(struct readelf *re, Dwarf_Half reg,
349 static const char *get_string(struct readelf *re, int strtab, size_t off);
350 static const char *get_symbol_name(struct readelf *re, int symtab, int i);
351 static uint64_t get_symbol_value(struct readelf *re, int symtab, int i);
352 static void load_sections(struct readelf *re);
353 static int loc_at_comparator(const void *la1, const void *la2);
354 static const char *mips_abi_fp(uint64_t fp);
355 static const char *note_type(const char *note_name, unsigned int et,
357 static const char *note_type_freebsd(unsigned int nt);
358 static const char *note_type_freebsd_core(unsigned int nt);
359 static const char *note_type_linux_core(unsigned int nt);
360 static const char *note_type_gnu(unsigned int nt);
361 static const char *note_type_netbsd(unsigned int nt);
362 static const char *note_type_openbsd(unsigned int nt);
363 static const char *note_type_unknown(unsigned int nt);
364 static const char *note_type_xen(unsigned int nt);
365 static const char *option_kind(uint8_t kind);
366 static const char *phdr_type(unsigned int mach, unsigned int ptype);
367 static const char *ppc_abi_fp(uint64_t fp);
368 static const char *ppc_abi_vector(uint64_t vec);
369 static void readelf_usage(int status);
370 static void readelf_version(void);
371 static void search_loclist_at(struct readelf *re, Dwarf_Die die,
372 Dwarf_Unsigned lowpc, struct loc_at **la_list,
373 size_t *la_list_len, size_t *la_list_cap);
374 static void search_ver(struct readelf *re);
375 static const char *section_type(unsigned int mach, unsigned int stype);
376 static void set_cu_context(struct readelf *re, Dwarf_Half psize,
377 Dwarf_Half osize, Dwarf_Half ver);
378 static const char *st_bind(unsigned int sbind);
379 static const char *st_shndx(unsigned int shndx);
380 static const char *st_type(unsigned int mach, unsigned int os,
382 static const char *st_vis(unsigned int svis);
383 static const char *top_tag(unsigned int tag);
384 static void unload_sections(struct readelf *re);
385 static uint64_t _read_lsb(Elf_Data *d, uint64_t *offsetp,
387 static uint64_t _read_msb(Elf_Data *d, uint64_t *offsetp,
389 static uint64_t _decode_lsb(uint8_t **data, int bytes_to_read);
390 static uint64_t _decode_msb(uint8_t **data, int bytes_to_read);
391 static int64_t _decode_sleb128(uint8_t **dp, uint8_t *dpe);
392 static uint64_t _decode_uleb128(uint8_t **dp, uint8_t *dpe);
394 static struct eflags_desc arm_eflags_desc[] = {
395 {EF_ARM_RELEXEC, "relocatable executable"},
396 {EF_ARM_HASENTRY, "has entry point"},
397 {EF_ARM_SYMSARESORTED, "sorted symbol tables"},
398 {EF_ARM_DYNSYMSUSESEGIDX, "dynamic symbols use segment index"},
399 {EF_ARM_MAPSYMSFIRST, "mapping symbols precede others"},
402 {EF_ARM_INTERWORK, "interworking enabled"},
403 {EF_ARM_APCS_26, "uses APCS/26"},
404 {EF_ARM_APCS_FLOAT, "uses APCS/float"},
405 {EF_ARM_PIC, "position independent"},
406 {EF_ARM_ALIGN8, "8 bit structure alignment"},
407 {EF_ARM_NEW_ABI, "uses new ABI"},
408 {EF_ARM_OLD_ABI, "uses old ABI"},
409 {EF_ARM_SOFT_FLOAT, "software FP"},
410 {EF_ARM_VFP_FLOAT, "VFP"},
411 {EF_ARM_MAVERICK_FLOAT, "Maverick FP"},
415 static struct eflags_desc mips_eflags_desc[] = {
416 {EF_MIPS_NOREORDER, "noreorder"},
417 {EF_MIPS_PIC, "pic"},
418 {EF_MIPS_CPIC, "cpic"},
419 {EF_MIPS_UCODE, "ugen_reserved"},
420 {EF_MIPS_ABI2, "abi2"},
421 {EF_MIPS_OPTIONS_FIRST, "odk first"},
422 {EF_MIPS_ARCH_ASE_MDMX, "mdmx"},
423 {EF_MIPS_ARCH_ASE_M16, "mips16"},
427 static struct eflags_desc powerpc_eflags_desc[] = {
429 {EF_PPC_RELOCATABLE, "relocatable"},
430 {EF_PPC_RELOCATABLE_LIB, "relocatable-lib"},
434 static struct eflags_desc riscv_eflags_desc[] = {
435 {EF_RISCV_RVC, "RVC"},
436 {EF_RISCV_RVE, "RVE"},
437 {EF_RISCV_TSO, "TSO"},
441 static struct eflags_desc sparc_eflags_desc[] = {
442 {EF_SPARC_32PLUS, "v8+"},
443 {EF_SPARC_SUN_US1, "ultrasparcI"},
444 {EF_SPARC_HAL_R1, "halr1"},
445 {EF_SPARC_SUN_US3, "ultrasparcIII"},
450 elf_osabi(unsigned int abi)
452 static char s_abi[32];
455 case ELFOSABI_NONE: return "NONE";
456 case ELFOSABI_HPUX: return "HPUX";
457 case ELFOSABI_NETBSD: return "NetBSD";
458 case ELFOSABI_GNU: return "GNU";
459 case ELFOSABI_HURD: return "HURD";
460 case ELFOSABI_86OPEN: return "86OPEN";
461 case ELFOSABI_SOLARIS: return "Solaris";
462 case ELFOSABI_AIX: return "AIX";
463 case ELFOSABI_IRIX: return "IRIX";
464 case ELFOSABI_FREEBSD: return "FreeBSD";
465 case ELFOSABI_TRU64: return "TRU64";
466 case ELFOSABI_MODESTO: return "MODESTO";
467 case ELFOSABI_OPENBSD: return "OpenBSD";
468 case ELFOSABI_OPENVMS: return "OpenVMS";
469 case ELFOSABI_NSK: return "NSK";
470 case ELFOSABI_CLOUDABI: return "CloudABI";
471 case ELFOSABI_ARM_AEABI: return "ARM EABI";
472 case ELFOSABI_ARM: return "ARM";
473 case ELFOSABI_STANDALONE: return "StandAlone";
475 snprintf(s_abi, sizeof(s_abi), "<unknown: %#x>", abi);
481 elf_machine(unsigned int mach)
483 static char s_mach[32];
486 case EM_NONE: return "Unknown machine";
487 case EM_M32: return "AT&T WE32100";
488 case EM_SPARC: return "Sun SPARC";
489 case EM_386: return "Intel i386";
490 case EM_68K: return "Motorola 68000";
491 case EM_IAMCU: return "Intel MCU";
492 case EM_88K: return "Motorola 88000";
493 case EM_860: return "Intel i860";
494 case EM_MIPS: return "MIPS R3000 Big-Endian only";
495 case EM_S370: return "IBM System/370";
496 case EM_MIPS_RS3_LE: return "MIPS R3000 Little-Endian";
497 case EM_PARISC: return "HP PA-RISC";
498 case EM_VPP500: return "Fujitsu VPP500";
499 case EM_SPARC32PLUS: return "SPARC v8plus";
500 case EM_960: return "Intel 80960";
501 case EM_PPC: return "PowerPC 32-bit";
502 case EM_PPC64: return "PowerPC 64-bit";
503 case EM_S390: return "IBM System/390";
504 case EM_V800: return "NEC V800";
505 case EM_FR20: return "Fujitsu FR20";
506 case EM_RH32: return "TRW RH-32";
507 case EM_RCE: return "Motorola RCE";
508 case EM_ARM: return "ARM";
509 case EM_SH: return "Hitachi SH";
510 case EM_SPARCV9: return "SPARC v9 64-bit";
511 case EM_TRICORE: return "Siemens TriCore embedded processor";
512 case EM_ARC: return "Argonaut RISC Core";
513 case EM_H8_300: return "Hitachi H8/300";
514 case EM_H8_300H: return "Hitachi H8/300H";
515 case EM_H8S: return "Hitachi H8S";
516 case EM_H8_500: return "Hitachi H8/500";
517 case EM_IA_64: return "Intel IA-64 Processor";
518 case EM_MIPS_X: return "Stanford MIPS-X";
519 case EM_COLDFIRE: return "Motorola ColdFire";
520 case EM_68HC12: return "Motorola M68HC12";
521 case EM_MMA: return "Fujitsu MMA";
522 case EM_PCP: return "Siemens PCP";
523 case EM_NCPU: return "Sony nCPU";
524 case EM_NDR1: return "Denso NDR1 microprocessor";
525 case EM_STARCORE: return "Motorola Star*Core processor";
526 case EM_ME16: return "Toyota ME16 processor";
527 case EM_ST100: return "STMicroelectronics ST100 processor";
528 case EM_TINYJ: return "Advanced Logic Corp. TinyJ processor";
529 case EM_X86_64: return "Advanced Micro Devices x86-64";
530 case EM_PDSP: return "Sony DSP Processor";
531 case EM_FX66: return "Siemens FX66 microcontroller";
532 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 microcontroller";
533 case EM_ST7: return "STmicroelectronics ST7 8-bit microcontroller";
534 case EM_68HC16: return "Motorola MC68HC16 microcontroller";
535 case EM_68HC11: return "Motorola MC68HC11 microcontroller";
536 case EM_68HC08: return "Motorola MC68HC08 microcontroller";
537 case EM_68HC05: return "Motorola MC68HC05 microcontroller";
538 case EM_SVX: return "Silicon Graphics SVx";
539 case EM_ST19: return "STMicroelectronics ST19 8-bit mc";
540 case EM_VAX: return "Digital VAX";
541 case EM_CRIS: return "Axis Communications 32-bit embedded processor";
542 case EM_JAVELIN: return "Infineon Tech. 32bit embedded processor";
543 case EM_FIREPATH: return "Element 14 64-bit DSP Processor";
544 case EM_ZSP: return "LSI Logic 16-bit DSP Processor";
545 case EM_MMIX: return "Donald Knuth's educational 64-bit proc";
546 case EM_HUANY: return "Harvard University MI object files";
547 case EM_PRISM: return "SiTera Prism";
548 case EM_AVR: return "Atmel AVR 8-bit microcontroller";
549 case EM_FR30: return "Fujitsu FR30";
550 case EM_D10V: return "Mitsubishi D10V";
551 case EM_D30V: return "Mitsubishi D30V";
552 case EM_V850: return "NEC v850";
553 case EM_M32R: return "Mitsubishi M32R";
554 case EM_MN10300: return "Matsushita MN10300";
555 case EM_MN10200: return "Matsushita MN10200";
556 case EM_PJ: return "picoJava";
557 case EM_OPENRISC: return "OpenRISC 32-bit embedded processor";
558 case EM_ARC_A5: return "ARC Cores Tangent-A5";
559 case EM_XTENSA: return "Tensilica Xtensa Architecture";
560 case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
561 case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
562 case EM_NS32K: return "National Semiconductor 32000 series";
563 case EM_TPC: return "Tenor Network TPC processor";
564 case EM_SNP1K: return "Trebia SNP 1000 processor";
565 case EM_ST200: return "STMicroelectronics ST200 microcontroller";
566 case EM_IP2K: return "Ubicom IP2xxx microcontroller family";
567 case EM_MAX: return "MAX Processor";
568 case EM_CR: return "National Semiconductor CompactRISC microprocessor";
569 case EM_F2MC16: return "Fujitsu F2MC16";
570 case EM_MSP430: return "TI embedded microcontroller msp430";
571 case EM_BLACKFIN: return "Analog Devices Blackfin (DSP) processor";
572 case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
573 case EM_SEP: return "Sharp embedded microprocessor";
574 case EM_ARCA: return "Arca RISC Microprocessor";
575 case EM_UNICORE: return "Microprocessor series from PKU-Unity Ltd";
576 case EM_AARCH64: return "AArch64";
577 case EM_RISCV: return "RISC-V";
579 snprintf(s_mach, sizeof(s_mach), "<unknown: %#x>", mach);
586 elf_class(unsigned int class)
588 static char s_class[32];
591 case ELFCLASSNONE: return "none";
592 case ELFCLASS32: return "ELF32";
593 case ELFCLASS64: return "ELF64";
595 snprintf(s_class, sizeof(s_class), "<unknown: %#x>", class);
601 elf_endian(unsigned int endian)
603 static char s_endian[32];
606 case ELFDATANONE: return "none";
607 case ELFDATA2LSB: return "2's complement, little endian";
608 case ELFDATA2MSB: return "2's complement, big endian";
610 snprintf(s_endian, sizeof(s_endian), "<unknown: %#x>", endian);
616 elf_type(unsigned int type)
618 static char s_type[32];
621 case ET_NONE: return "NONE (None)";
622 case ET_REL: return "REL (Relocatable file)";
623 case ET_EXEC: return "EXEC (Executable file)";
624 case ET_DYN: return "DYN (Shared object file)";
625 case ET_CORE: return "CORE (Core file)";
627 if (type >= ET_LOPROC)
628 snprintf(s_type, sizeof(s_type), "<proc: %#x>", type);
629 else if (type >= ET_LOOS && type <= ET_HIOS)
630 snprintf(s_type, sizeof(s_type), "<os: %#x>", type);
632 snprintf(s_type, sizeof(s_type), "<unknown: %#x>",
639 elf_ver(unsigned int ver)
641 static char s_ver[32];
644 case EV_CURRENT: return "(current)";
645 case EV_NONE: return "(none)";
647 snprintf(s_ver, sizeof(s_ver), "<unknown: %#x>",
654 phdr_type(unsigned int mach, unsigned int ptype)
656 static char s_ptype[32];
658 if (ptype >= PT_LOPROC && ptype <= PT_HIPROC) {
662 case PT_ARM_ARCHEXT: return "ARM_ARCHEXT";
663 case PT_ARM_EXIDX: return "ARM_EXIDX";
667 snprintf(s_ptype, sizeof(s_ptype), "LOPROC+%#x",
673 case PT_NULL: return "NULL";
674 case PT_LOAD: return "LOAD";
675 case PT_DYNAMIC: return "DYNAMIC";
676 case PT_INTERP: return "INTERP";
677 case PT_NOTE: return "NOTE";
678 case PT_SHLIB: return "SHLIB";
679 case PT_PHDR: return "PHDR";
680 case PT_TLS: return "TLS";
681 case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
682 case PT_GNU_STACK: return "GNU_STACK";
683 case PT_GNU_RELRO: return "GNU_RELRO";
684 case PT_OPENBSD_RANDOMIZE: return "OPENBSD_RANDOMIZE";
685 case PT_OPENBSD_WXNEEDED: return "OPENBSD_WXNEEDED";
686 case PT_OPENBSD_BOOTDATA: return "OPENBSD_BOOTDATA";
688 if (ptype >= PT_LOOS && ptype <= PT_HIOS)
689 snprintf(s_ptype, sizeof(s_ptype), "LOOS+%#x",
692 snprintf(s_ptype, sizeof(s_ptype), "<unknown: %#x>",
699 section_type(unsigned int mach, unsigned int stype)
701 static char s_stype[32];
703 if (stype >= SHT_LOPROC && stype <= SHT_HIPROC) {
707 case SHT_ARM_EXIDX: return "ARM_EXIDX";
708 case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP";
709 case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES";
710 case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY";
711 case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION";
716 case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
724 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
725 case SHT_MIPS_MSYM: return "MIPS_MSYM";
726 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
727 case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
728 case SHT_MIPS_UCODE: return "MIPS_UCODE";
729 case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
730 case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
731 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
732 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
733 case SHT_MIPS_RELD: return "MIPS_RELD";
734 case SHT_MIPS_IFACE: return "MIPS_IFACE";
735 case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
736 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
737 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
738 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
739 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
740 case SHT_MIPS_DWARF: return "MIPS_DWARF";
741 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
742 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
743 case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
744 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
745 case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
746 case SHT_MIPS_XLATE: return "MIPS_XLATE";
747 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
748 case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
749 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
750 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
751 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
752 case SHT_MIPS_ABIFLAGS: return "MIPS_ABIFLAGS";
761 snprintf(s_stype, sizeof(s_stype), "LOPROC+%#x",
767 case SHT_NULL: return "NULL";
768 case SHT_PROGBITS: return "PROGBITS";
769 case SHT_SYMTAB: return "SYMTAB";
770 case SHT_STRTAB: return "STRTAB";
771 case SHT_RELA: return "RELA";
772 case SHT_HASH: return "HASH";
773 case SHT_DYNAMIC: return "DYNAMIC";
774 case SHT_NOTE: return "NOTE";
775 case SHT_NOBITS: return "NOBITS";
776 case SHT_REL: return "REL";
777 case SHT_SHLIB: return "SHLIB";
778 case SHT_DYNSYM: return "DYNSYM";
779 case SHT_INIT_ARRAY: return "INIT_ARRAY";
780 case SHT_FINI_ARRAY: return "FINI_ARRAY";
781 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
782 case SHT_GROUP: return "GROUP";
783 case SHT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
784 case SHT_SUNW_dof: return "SUNW_dof";
785 case SHT_SUNW_cap: return "SUNW_cap";
786 case SHT_GNU_HASH: return "GNU_HASH";
787 case SHT_SUNW_ANNOTATE: return "SUNW_ANNOTATE";
788 case SHT_SUNW_DEBUGSTR: return "SUNW_DEBUGSTR";
789 case SHT_SUNW_DEBUG: return "SUNW_DEBUG";
790 case SHT_SUNW_move: return "SUNW_move";
791 case SHT_SUNW_COMDAT: return "SUNW_COMDAT";
792 case SHT_SUNW_syminfo: return "SUNW_syminfo";
793 case SHT_SUNW_verdef: return "SUNW_verdef";
794 case SHT_SUNW_verneed: return "SUNW_verneed";
795 case SHT_SUNW_versym: return "SUNW_versym";
797 if (stype >= SHT_LOOS && stype <= SHT_HIOS)
798 snprintf(s_stype, sizeof(s_stype), "LOOS+%#x",
800 else if (stype >= SHT_LOUSER)
801 snprintf(s_stype, sizeof(s_stype), "LOUSER+%#x",
804 snprintf(s_stype, sizeof(s_stype), "<unknown: %#x>",
811 dt_type(unsigned int mach, unsigned int dtype)
813 static char s_dtype[32];
816 case DT_NULL: return "NULL";
817 case DT_NEEDED: return "NEEDED";
818 case DT_PLTRELSZ: return "PLTRELSZ";
819 case DT_PLTGOT: return "PLTGOT";
820 case DT_HASH: return "HASH";
821 case DT_STRTAB: return "STRTAB";
822 case DT_SYMTAB: return "SYMTAB";
823 case DT_RELA: return "RELA";
824 case DT_RELASZ: return "RELASZ";
825 case DT_RELAENT: return "RELAENT";
826 case DT_STRSZ: return "STRSZ";
827 case DT_SYMENT: return "SYMENT";
828 case DT_INIT: return "INIT";
829 case DT_FINI: return "FINI";
830 case DT_SONAME: return "SONAME";
831 case DT_RPATH: return "RPATH";
832 case DT_SYMBOLIC: return "SYMBOLIC";
833 case DT_REL: return "REL";
834 case DT_RELSZ: return "RELSZ";
835 case DT_RELENT: return "RELENT";
836 case DT_PLTREL: return "PLTREL";
837 case DT_DEBUG: return "DEBUG";
838 case DT_TEXTREL: return "TEXTREL";
839 case DT_JMPREL: return "JMPREL";
840 case DT_BIND_NOW: return "BIND_NOW";
841 case DT_INIT_ARRAY: return "INIT_ARRAY";
842 case DT_FINI_ARRAY: return "FINI_ARRAY";
843 case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
844 case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
845 case DT_RUNPATH: return "RUNPATH";
846 case DT_FLAGS: return "FLAGS";
847 case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
848 case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
849 case DT_MAXPOSTAGS: return "MAXPOSTAGS";
850 case DT_SUNW_AUXILIARY: return "SUNW_AUXILIARY";
851 case DT_SUNW_RTLDINF: return "SUNW_RTLDINF";
852 case DT_SUNW_FILTER: return "SUNW_FILTER";
853 case DT_SUNW_CAP: return "SUNW_CAP";
854 case DT_SUNW_ASLR: return "SUNW_ASLR";
855 case DT_CHECKSUM: return "CHECKSUM";
856 case DT_PLTPADSZ: return "PLTPADSZ";
857 case DT_MOVEENT: return "MOVEENT";
858 case DT_MOVESZ: return "MOVESZ";
859 case DT_FEATURE: return "FEATURE";
860 case DT_POSFLAG_1: return "POSFLAG_1";
861 case DT_SYMINSZ: return "SYMINSZ";
862 case DT_SYMINENT: return "SYMINENT";
863 case DT_GNU_HASH: return "GNU_HASH";
864 case DT_TLSDESC_PLT: return "DT_TLSDESC_PLT";
865 case DT_TLSDESC_GOT: return "DT_TLSDESC_GOT";
866 case DT_GNU_CONFLICT: return "GNU_CONFLICT";
867 case DT_GNU_LIBLIST: return "GNU_LIBLIST";
868 case DT_CONFIG: return "CONFIG";
869 case DT_DEPAUDIT: return "DEPAUDIT";
870 case DT_AUDIT: return "AUDIT";
871 case DT_PLTPAD: return "PLTPAD";
872 case DT_MOVETAB: return "MOVETAB";
873 case DT_SYMINFO: return "SYMINFO";
874 case DT_VERSYM: return "VERSYM";
875 case DT_RELACOUNT: return "RELACOUNT";
876 case DT_RELCOUNT: return "RELCOUNT";
877 case DT_FLAGS_1: return "FLAGS_1";
878 case DT_VERDEF: return "VERDEF";
879 case DT_VERDEFNUM: return "VERDEFNUM";
880 case DT_VERNEED: return "VERNEED";
881 case DT_VERNEEDNUM: return "VERNEEDNUM";
882 case DT_AUXILIARY: return "AUXILIARY";
883 case DT_USED: return "USED";
884 case DT_FILTER: return "FILTER";
885 case DT_GNU_PRELINKED: return "GNU_PRELINKED";
886 case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
887 case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
890 if (dtype >= DT_LOPROC && dtype <= DT_HIPROC) {
894 case DT_ARM_SYMTABSZ:
895 return "ARM_SYMTABSZ";
903 case DT_MIPS_RLD_VERSION:
904 return "MIPS_RLD_VERSION";
905 case DT_MIPS_TIME_STAMP:
906 return "MIPS_TIME_STAMP";
907 case DT_MIPS_ICHECKSUM:
908 return "MIPS_ICHECKSUM";
909 case DT_MIPS_IVERSION:
910 return "MIPS_IVERSION";
913 case DT_MIPS_BASE_ADDRESS:
914 return "MIPS_BASE_ADDRESS";
915 case DT_MIPS_CONFLICT:
916 return "MIPS_CONFLICT";
917 case DT_MIPS_LIBLIST:
918 return "MIPS_LIBLIST";
919 case DT_MIPS_LOCAL_GOTNO:
920 return "MIPS_LOCAL_GOTNO";
921 case DT_MIPS_CONFLICTNO:
922 return "MIPS_CONFLICTNO";
923 case DT_MIPS_LIBLISTNO:
924 return "MIPS_LIBLISTNO";
925 case DT_MIPS_SYMTABNO:
926 return "MIPS_SYMTABNO";
927 case DT_MIPS_UNREFEXTNO:
928 return "MIPS_UNREFEXTNO";
930 return "MIPS_GOTSYM";
931 case DT_MIPS_HIPAGENO:
932 return "MIPS_HIPAGENO";
933 case DT_MIPS_RLD_MAP:
934 return "MIPS_RLD_MAP";
935 case DT_MIPS_DELTA_CLASS:
936 return "MIPS_DELTA_CLASS";
937 case DT_MIPS_DELTA_CLASS_NO:
938 return "MIPS_DELTA_CLASS_NO";
939 case DT_MIPS_DELTA_INSTANCE:
940 return "MIPS_DELTA_INSTANCE";
941 case DT_MIPS_DELTA_INSTANCE_NO:
942 return "MIPS_DELTA_INSTANCE_NO";
943 case DT_MIPS_DELTA_RELOC:
944 return "MIPS_DELTA_RELOC";
945 case DT_MIPS_DELTA_RELOC_NO:
946 return "MIPS_DELTA_RELOC_NO";
947 case DT_MIPS_DELTA_SYM:
948 return "MIPS_DELTA_SYM";
949 case DT_MIPS_DELTA_SYM_NO:
950 return "MIPS_DELTA_SYM_NO";
951 case DT_MIPS_DELTA_CLASSSYM:
952 return "MIPS_DELTA_CLASSSYM";
953 case DT_MIPS_DELTA_CLASSSYM_NO:
954 return "MIPS_DELTA_CLASSSYM_NO";
955 case DT_MIPS_CXX_FLAGS:
956 return "MIPS_CXX_FLAGS";
957 case DT_MIPS_PIXIE_INIT:
958 return "MIPS_PIXIE_INIT";
959 case DT_MIPS_SYMBOL_LIB:
960 return "MIPS_SYMBOL_LIB";
961 case DT_MIPS_LOCALPAGE_GOTIDX:
962 return "MIPS_LOCALPAGE_GOTIDX";
963 case DT_MIPS_LOCAL_GOTIDX:
964 return "MIPS_LOCAL_GOTIDX";
965 case DT_MIPS_HIDDEN_GOTIDX:
966 return "MIPS_HIDDEN_GOTIDX";
967 case DT_MIPS_PROTECTED_GOTIDX:
968 return "MIPS_PROTECTED_GOTIDX";
969 case DT_MIPS_OPTIONS:
970 return "MIPS_OPTIONS";
971 case DT_MIPS_INTERFACE:
972 return "MIPS_INTERFACE";
973 case DT_MIPS_DYNSTR_ALIGN:
974 return "MIPS_DYNSTR_ALIGN";
975 case DT_MIPS_INTERFACE_SIZE:
976 return "MIPS_INTERFACE_SIZE";
977 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
978 return "MIPS_RLD_TEXT_RESOLVE_ADDR";
979 case DT_MIPS_PERF_SUFFIX:
980 return "MIPS_PERF_SUFFIX";
981 case DT_MIPS_COMPACT_SIZE:
982 return "MIPS_COMPACT_SIZE";
983 case DT_MIPS_GP_VALUE:
984 return "MIPS_GP_VALUE";
985 case DT_MIPS_AUX_DYNAMIC:
986 return "MIPS_AUX_DYNAMIC";
988 return "MIPS_PLTGOT";
989 case DT_MIPS_RLD_OBJ_UPDATE:
990 return "MIPS_RLD_OBJ_UPDATE";
1001 case DT_SPARC_REGISTER:
1002 return "DT_SPARC_REGISTER";
1012 snprintf(s_dtype, sizeof(s_dtype), "<unknown: %#x>", dtype);
1017 st_bind(unsigned int sbind)
1019 static char s_sbind[32];
1022 case STB_LOCAL: return "LOCAL";
1023 case STB_GLOBAL: return "GLOBAL";
1024 case STB_WEAK: return "WEAK";
1025 case STB_GNU_UNIQUE: return "UNIQUE";
1027 if (sbind >= STB_LOOS && sbind <= STB_HIOS)
1029 else if (sbind >= STB_LOPROC && sbind <= STB_HIPROC)
1032 snprintf(s_sbind, sizeof(s_sbind), "<unknown: %#x>",
1039 st_type(unsigned int mach, unsigned int os, unsigned int stype)
1041 static char s_stype[32];
1044 case STT_NOTYPE: return "NOTYPE";
1045 case STT_OBJECT: return "OBJECT";
1046 case STT_FUNC: return "FUNC";
1047 case STT_SECTION: return "SECTION";
1048 case STT_FILE: return "FILE";
1049 case STT_COMMON: return "COMMON";
1050 case STT_TLS: return "TLS";
1052 if (stype >= STT_LOOS && stype <= STT_HIOS) {
1053 if ((os == ELFOSABI_GNU || os == ELFOSABI_FREEBSD) &&
1054 stype == STT_GNU_IFUNC)
1056 snprintf(s_stype, sizeof(s_stype), "OS+%#x",
1058 } else if (stype >= STT_LOPROC && stype <= STT_HIPROC) {
1059 if (mach == EM_SPARCV9 && stype == STT_SPARC_REGISTER)
1061 snprintf(s_stype, sizeof(s_stype), "PROC+%#x",
1062 stype - STT_LOPROC);
1064 snprintf(s_stype, sizeof(s_stype), "<unknown: %#x>",
1071 st_vis(unsigned int svis)
1073 static char s_svis[32];
1076 case STV_DEFAULT: return "DEFAULT";
1077 case STV_INTERNAL: return "INTERNAL";
1078 case STV_HIDDEN: return "HIDDEN";
1079 case STV_PROTECTED: return "PROTECTED";
1081 snprintf(s_svis, sizeof(s_svis), "<unknown: %#x>", svis);
1087 st_shndx(unsigned int shndx)
1089 static char s_shndx[32];
1092 case SHN_UNDEF: return "UND";
1093 case SHN_ABS: return "ABS";
1094 case SHN_COMMON: return "COM";
1096 if (shndx >= SHN_LOPROC && shndx <= SHN_HIPROC)
1098 else if (shndx >= SHN_LOOS && shndx <= SHN_HIOS)
1101 snprintf(s_shndx, sizeof(s_shndx), "%u", shndx);
1110 } section_flag[] = {
1111 {"WRITE", 'W', SHF_WRITE},
1112 {"ALLOC", 'A', SHF_ALLOC},
1113 {"EXEC", 'X', SHF_EXECINSTR},
1114 {"MERGE", 'M', SHF_MERGE},
1115 {"STRINGS", 'S', SHF_STRINGS},
1116 {"INFO LINK", 'I', SHF_INFO_LINK},
1117 {"OS NONCONF", 'O', SHF_OS_NONCONFORMING},
1118 {"GROUP", 'G', SHF_GROUP},
1119 {"TLS", 'T', SHF_TLS},
1120 {"COMPRESSED", 'C', SHF_COMPRESSED},
1125 note_type(const char *name, unsigned int et, unsigned int nt)
1127 if ((strcmp(name, "CORE") == 0 || strcmp(name, "LINUX") == 0) &&
1129 return note_type_linux_core(nt);
1130 else if (strcmp(name, "FreeBSD") == 0)
1132 return note_type_freebsd_core(nt);
1134 return note_type_freebsd(nt);
1135 else if (strcmp(name, "GNU") == 0 && et != ET_CORE)
1136 return note_type_gnu(nt);
1137 else if (strcmp(name, "NetBSD") == 0 && et != ET_CORE)
1138 return note_type_netbsd(nt);
1139 else if (strcmp(name, "OpenBSD") == 0 && et != ET_CORE)
1140 return note_type_openbsd(nt);
1141 else if (strcmp(name, "Xen") == 0 && et != ET_CORE)
1142 return note_type_xen(nt);
1143 return note_type_unknown(nt);
1147 note_type_freebsd(unsigned int nt)
1150 case 1: return "NT_FREEBSD_ABI_TAG";
1151 case 2: return "NT_FREEBSD_NOINIT_TAG";
1152 case 3: return "NT_FREEBSD_ARCH_TAG";
1153 case 4: return "NT_FREEBSD_FEATURE_CTL";
1154 default: return (note_type_unknown(nt));
1159 note_type_freebsd_core(unsigned int nt)
1162 case 1: return "NT_PRSTATUS";
1163 case 2: return "NT_FPREGSET";
1164 case 3: return "NT_PRPSINFO";
1165 case 7: return "NT_THRMISC";
1166 case 8: return "NT_PROCSTAT_PROC";
1167 case 9: return "NT_PROCSTAT_FILES";
1168 case 10: return "NT_PROCSTAT_VMMAP";
1169 case 11: return "NT_PROCSTAT_GROUPS";
1170 case 12: return "NT_PROCSTAT_UMASK";
1171 case 13: return "NT_PROCSTAT_RLIMIT";
1172 case 14: return "NT_PROCSTAT_OSREL";
1173 case 15: return "NT_PROCSTAT_PSSTRINGS";
1174 case 16: return "NT_PROCSTAT_AUXV";
1175 case 17: return "NT_PTLWPINFO";
1176 case 0x202: return "NT_X86_XSTATE (x86 XSAVE extended state)";
1177 case 0x400: return "NT_ARM_VFP (arm VFP registers)";
1178 default: return (note_type_unknown(nt));
1183 note_type_linux_core(unsigned int nt)
1186 case 1: return "NT_PRSTATUS (Process status)";
1187 case 2: return "NT_FPREGSET (Floating point information)";
1188 case 3: return "NT_PRPSINFO (Process information)";
1189 case 4: return "NT_TASKSTRUCT (Task structure)";
1190 case 6: return "NT_AUXV (Auxiliary vector)";
1191 case 10: return "NT_PSTATUS (Linux process status)";
1192 case 12: return "NT_FPREGS (Linux floating point regset)";
1193 case 13: return "NT_PSINFO (Linux process information)";
1194 case 16: return "NT_LWPSTATUS (Linux lwpstatus_t type)";
1195 case 17: return "NT_LWPSINFO (Linux lwpinfo_t type)";
1196 case 18: return "NT_WIN32PSTATUS (win32_pstatus structure)";
1197 case 0x100: return "NT_PPC_VMX (ppc Altivec registers)";
1198 case 0x102: return "NT_PPC_VSX (ppc VSX registers)";
1199 case 0x202: return "NT_X86_XSTATE (x86 XSAVE extended state)";
1200 case 0x300: return "NT_S390_HIGH_GPRS (s390 upper register halves)";
1201 case 0x301: return "NT_S390_TIMER (s390 timer register)";
1202 case 0x302: return "NT_S390_TODCMP (s390 TOD comparator register)";
1203 case 0x303: return "NT_S390_TODPREG (s390 TOD programmable register)";
1204 case 0x304: return "NT_S390_CTRS (s390 control registers)";
1205 case 0x305: return "NT_S390_PREFIX (s390 prefix register)";
1206 case 0x400: return "NT_ARM_VFP (arm VFP registers)";
1207 case 0x46494c45UL: return "NT_FILE (mapped files)";
1208 case 0x46E62B7FUL: return "NT_PRXFPREG (Linux user_xfpregs structure)";
1209 case 0x53494749UL: return "NT_SIGINFO (siginfo_t data)";
1210 default: return (note_type_unknown(nt));
1215 note_type_gnu(unsigned int nt)
1218 case 1: return "NT_GNU_ABI_TAG";
1219 case 2: return "NT_GNU_HWCAP (Hardware capabilities)";
1220 case 3: return "NT_GNU_BUILD_ID (Build id set by ld(1))";
1221 case 4: return "NT_GNU_GOLD_VERSION (GNU gold version)";
1222 case 5: return "NT_GNU_PROPERTY_TYPE_0";
1223 default: return (note_type_unknown(nt));
1228 note_type_netbsd(unsigned int nt)
1231 case 1: return "NT_NETBSD_IDENT";
1232 default: return (note_type_unknown(nt));
1237 note_type_openbsd(unsigned int nt)
1240 case 1: return "NT_OPENBSD_IDENT";
1241 default: return (note_type_unknown(nt));
1246 note_type_unknown(unsigned int nt)
1248 static char s_nt[32];
1250 snprintf(s_nt, sizeof(s_nt),
1251 nt >= 0x100 ? "<unknown: 0x%x>" : "<unknown: %u>", nt);
1256 note_type_xen(unsigned int nt)
1259 case 0: return "XEN_ELFNOTE_INFO";
1260 case 1: return "XEN_ELFNOTE_ENTRY";
1261 case 2: return "XEN_ELFNOTE_HYPERCALL_PAGE";
1262 case 3: return "XEN_ELFNOTE_VIRT_BASE";
1263 case 4: return "XEN_ELFNOTE_PADDR_OFFSET";
1264 case 5: return "XEN_ELFNOTE_XEN_VERSION";
1265 case 6: return "XEN_ELFNOTE_GUEST_OS";
1266 case 7: return "XEN_ELFNOTE_GUEST_VERSION";
1267 case 8: return "XEN_ELFNOTE_LOADER";
1268 case 9: return "XEN_ELFNOTE_PAE_MODE";
1269 case 10: return "XEN_ELFNOTE_FEATURES";
1270 case 11: return "XEN_ELFNOTE_BSD_SYMTAB";
1271 case 12: return "XEN_ELFNOTE_HV_START_LOW";
1272 case 13: return "XEN_ELFNOTE_L1_MFN_VALID";
1273 case 14: return "XEN_ELFNOTE_SUSPEND_CANCEL";
1274 case 15: return "XEN_ELFNOTE_INIT_P2M";
1275 case 16: return "XEN_ELFNOTE_MOD_START_PFN";
1276 case 17: return "XEN_ELFNOTE_SUPPORTED_FEATURES";
1277 default: return (note_type_unknown(nt));
1285 {"EXACT_MATCH", LL_EXACT_MATCH},
1286 {"IGNORE_INT_VER", LL_IGNORE_INT_VER},
1287 {"REQUIRE_MINOR", LL_REQUIRE_MINOR},
1288 {"EXPORTS", LL_EXPORTS},
1289 {"DELAY_LOAD", LL_DELAY_LOAD},
1290 {"DELTA", LL_DELTA},
1294 static struct mips_option mips_exceptions_option[] = {
1295 {OEX_PAGE0, "PAGE0"},
1297 {OEX_PRECISEFP, "PRECISEFP"},
1298 {OEX_DISMISS, "DISMISS"},
1302 static struct mips_option mips_pad_option[] = {
1303 {OPAD_PREFIX, "PREFIX"},
1304 {OPAD_POSTFIX, "POSTFIX"},
1305 {OPAD_SYMBOL, "SYMBOL"},
1309 static struct mips_option mips_hwpatch_option[] = {
1310 {OHW_R4KEOP, "R4KEOP"},
1311 {OHW_R8KPFETCH, "R8KPFETCH"},
1312 {OHW_R5KEOP, "R5KEOP"},
1313 {OHW_R5KCVTL, "R5KCVTL"},
1317 static struct mips_option mips_hwa_option[] = {
1318 {OHWA0_R4KEOP_CHECKED, "R4KEOP_CHECKED"},
1319 {OHWA0_R4KEOP_CLEAN, "R4KEOP_CLEAN"},
1323 static struct mips_option mips_hwo_option[] = {
1324 {OHWO0_FIXADE, "FIXADE"},
1329 option_kind(uint8_t kind)
1331 static char s_kind[32];
1334 case ODK_NULL: return "NULL";
1335 case ODK_REGINFO: return "REGINFO";
1336 case ODK_EXCEPTIONS: return "EXCEPTIONS";
1337 case ODK_PAD: return "PAD";
1338 case ODK_HWPATCH: return "HWPATCH";
1339 case ODK_FILL: return "FILL";
1340 case ODK_TAGS: return "TAGS";
1341 case ODK_HWAND: return "HWAND";
1342 case ODK_HWOR: return "HWOR";
1343 case ODK_GP_GROUP: return "GP_GROUP";
1344 case ODK_IDENT: return "IDENT";
1346 snprintf(s_kind, sizeof(s_kind), "<unknown: %u>", kind);
1352 top_tag(unsigned int tag)
1354 static char s_top_tag[32];
1357 case 1: return "File Attributes";
1358 case 2: return "Section Attributes";
1359 case 3: return "Symbol Attributes";
1361 snprintf(s_top_tag, sizeof(s_top_tag), "Unknown tag: %u", tag);
1367 aeabi_cpu_arch(uint64_t arch)
1369 static char s_cpu_arch[32];
1372 case 0: return "Pre-V4";
1373 case 1: return "ARM v4";
1374 case 2: return "ARM v4T";
1375 case 3: return "ARM v5T";
1376 case 4: return "ARM v5TE";
1377 case 5: return "ARM v5TEJ";
1378 case 6: return "ARM v6";
1379 case 7: return "ARM v6KZ";
1380 case 8: return "ARM v6T2";
1381 case 9: return "ARM v6K";
1382 case 10: return "ARM v7";
1383 case 11: return "ARM v6-M";
1384 case 12: return "ARM v6S-M";
1385 case 13: return "ARM v7E-M";
1387 snprintf(s_cpu_arch, sizeof(s_cpu_arch),
1388 "Unknown (%ju)", (uintmax_t) arch);
1389 return (s_cpu_arch);
1394 aeabi_cpu_arch_profile(uint64_t pf)
1396 static char s_arch_profile[32];
1400 return "Not applicable";
1401 case 0x41: /* 'A' */
1402 return "Application Profile";
1403 case 0x52: /* 'R' */
1404 return "Real-Time Profile";
1405 case 0x4D: /* 'M' */
1406 return "Microcontroller Profile";
1407 case 0x53: /* 'S' */
1408 return "Application or Real-Time Profile";
1410 snprintf(s_arch_profile, sizeof(s_arch_profile),
1411 "Unknown (%ju)\n", (uintmax_t) pf);
1412 return (s_arch_profile);
1417 aeabi_arm_isa(uint64_t ai)
1419 static char s_ai[32];
1422 case 0: return "No";
1423 case 1: return "Yes";
1425 snprintf(s_ai, sizeof(s_ai), "Unknown (%ju)\n",
1432 aeabi_thumb_isa(uint64_t ti)
1434 static char s_ti[32];
1437 case 0: return "No";
1438 case 1: return "16-bit Thumb";
1439 case 2: return "32-bit Thumb";
1441 snprintf(s_ti, sizeof(s_ti), "Unknown (%ju)\n",
1448 aeabi_fp_arch(uint64_t fp)
1450 static char s_fp_arch[32];
1453 case 0: return "No";
1454 case 1: return "VFPv1";
1455 case 2: return "VFPv2";
1456 case 3: return "VFPv3";
1457 case 4: return "VFPv3-D16";
1458 case 5: return "VFPv4";
1459 case 6: return "VFPv4-D16";
1461 snprintf(s_fp_arch, sizeof(s_fp_arch), "Unknown (%ju)",
1468 aeabi_wmmx_arch(uint64_t wmmx)
1470 static char s_wmmx[32];
1473 case 0: return "No";
1474 case 1: return "WMMXv1";
1475 case 2: return "WMMXv2";
1477 snprintf(s_wmmx, sizeof(s_wmmx), "Unknown (%ju)",
1484 aeabi_adv_simd_arch(uint64_t simd)
1486 static char s_simd[32];
1489 case 0: return "No";
1490 case 1: return "NEONv1";
1491 case 2: return "NEONv2";
1493 snprintf(s_simd, sizeof(s_simd), "Unknown (%ju)",
1500 aeabi_pcs_config(uint64_t pcs)
1502 static char s_pcs[32];
1505 case 0: return "None";
1506 case 1: return "Bare platform";
1507 case 2: return "Linux";
1508 case 3: return "Linux DSO";
1509 case 4: return "Palm OS 2004";
1510 case 5: return "Palm OS (future)";
1511 case 6: return "Symbian OS 2004";
1512 case 7: return "Symbian OS (future)";
1514 snprintf(s_pcs, sizeof(s_pcs), "Unknown (%ju)",
1521 aeabi_pcs_r9(uint64_t r9)
1523 static char s_r9[32];
1526 case 0: return "V6";
1527 case 1: return "SB";
1528 case 2: return "TLS pointer";
1529 case 3: return "Unused";
1531 snprintf(s_r9, sizeof(s_r9), "Unknown (%ju)", (uintmax_t) r9);
1537 aeabi_pcs_rw(uint64_t rw)
1539 static char s_rw[32];
1542 case 0: return "Absolute";
1543 case 1: return "PC-relative";
1544 case 2: return "SB-relative";
1545 case 3: return "None";
1547 snprintf(s_rw, sizeof(s_rw), "Unknown (%ju)", (uintmax_t) rw);
1553 aeabi_pcs_ro(uint64_t ro)
1555 static char s_ro[32];
1558 case 0: return "Absolute";
1559 case 1: return "PC-relative";
1560 case 2: return "None";
1562 snprintf(s_ro, sizeof(s_ro), "Unknown (%ju)", (uintmax_t) ro);
1568 aeabi_pcs_got(uint64_t got)
1570 static char s_got[32];
1573 case 0: return "None";
1574 case 1: return "direct";
1575 case 2: return "indirect via GOT";
1577 snprintf(s_got, sizeof(s_got), "Unknown (%ju)",
1584 aeabi_pcs_wchar_t(uint64_t wt)
1586 static char s_wt[32];
1589 case 0: return "None";
1590 case 2: return "wchar_t size 2";
1591 case 4: return "wchar_t size 4";
1593 snprintf(s_wt, sizeof(s_wt), "Unknown (%ju)", (uintmax_t) wt);
1599 aeabi_enum_size(uint64_t es)
1601 static char s_es[32];
1604 case 0: return "None";
1605 case 1: return "smallest";
1606 case 2: return "32-bit";
1607 case 3: return "visible 32-bit";
1609 snprintf(s_es, sizeof(s_es), "Unknown (%ju)", (uintmax_t) es);
1615 aeabi_align_needed(uint64_t an)
1617 static char s_align_n[64];
1620 case 0: return "No";
1621 case 1: return "8-byte align";
1622 case 2: return "4-byte align";
1623 case 3: return "Reserved";
1625 if (an >= 4 && an <= 12)
1626 snprintf(s_align_n, sizeof(s_align_n), "8-byte align"
1627 " and up to 2^%ju-byte extended align",
1630 snprintf(s_align_n, sizeof(s_align_n), "Unknown (%ju)",
1637 aeabi_align_preserved(uint64_t ap)
1639 static char s_align_p[128];
1642 case 0: return "No";
1643 case 1: return "8-byte align";
1644 case 2: return "8-byte align and SP % 8 == 0";
1645 case 3: return "Reserved";
1647 if (ap >= 4 && ap <= 12)
1648 snprintf(s_align_p, sizeof(s_align_p), "8-byte align"
1649 " and SP %% 8 == 0 and up to 2^%ju-byte extended"
1650 " align", (uintmax_t) ap);
1652 snprintf(s_align_p, sizeof(s_align_p), "Unknown (%ju)",
1659 aeabi_fp_rounding(uint64_t fr)
1661 static char s_fp_r[32];
1664 case 0: return "Unused";
1665 case 1: return "Needed";
1667 snprintf(s_fp_r, sizeof(s_fp_r), "Unknown (%ju)",
1674 aeabi_fp_denormal(uint64_t fd)
1676 static char s_fp_d[32];
1679 case 0: return "Unused";
1680 case 1: return "Needed";
1681 case 2: return "Sign Only";
1683 snprintf(s_fp_d, sizeof(s_fp_d), "Unknown (%ju)",
1690 aeabi_fp_exceptions(uint64_t fe)
1692 static char s_fp_e[32];
1695 case 0: return "Unused";
1696 case 1: return "Needed";
1698 snprintf(s_fp_e, sizeof(s_fp_e), "Unknown (%ju)",
1705 aeabi_fp_user_exceptions(uint64_t fu)
1707 static char s_fp_u[32];
1710 case 0: return "Unused";
1711 case 1: return "Needed";
1713 snprintf(s_fp_u, sizeof(s_fp_u), "Unknown (%ju)",
1720 aeabi_fp_number_model(uint64_t fn)
1722 static char s_fp_n[32];
1725 case 0: return "Unused";
1726 case 1: return "IEEE 754 normal";
1727 case 2: return "RTABI";
1728 case 3: return "IEEE 754";
1730 snprintf(s_fp_n, sizeof(s_fp_n), "Unknown (%ju)",
1737 aeabi_fp_16bit_format(uint64_t fp16)
1739 static char s_fp_16[64];
1742 case 0: return "None";
1743 case 1: return "IEEE 754";
1744 case 2: return "VFPv3/Advanced SIMD (alternative format)";
1746 snprintf(s_fp_16, sizeof(s_fp_16), "Unknown (%ju)",
1753 aeabi_mpext(uint64_t mp)
1755 static char s_mp[32];
1758 case 0: return "Not allowed";
1759 case 1: return "Allowed";
1761 snprintf(s_mp, sizeof(s_mp), "Unknown (%ju)",
1768 aeabi_div(uint64_t du)
1770 static char s_du[32];
1773 case 0: return "Yes (V7-R/V7-M)";
1774 case 1: return "No";
1775 case 2: return "Yes (V7-A)";
1777 snprintf(s_du, sizeof(s_du), "Unknown (%ju)",
1784 aeabi_t2ee(uint64_t t2ee)
1786 static char s_t2ee[32];
1789 case 0: return "Not allowed";
1790 case 1: return "Allowed";
1792 snprintf(s_t2ee, sizeof(s_t2ee), "Unknown(%ju)",
1800 aeabi_hardfp(uint64_t hfp)
1802 static char s_hfp[32];
1805 case 0: return "Tag_FP_arch";
1806 case 1: return "only SP";
1807 case 2: return "only DP";
1808 case 3: return "both SP and DP";
1810 snprintf(s_hfp, sizeof(s_hfp), "Unknown (%ju)",
1817 aeabi_vfp_args(uint64_t va)
1819 static char s_va[32];
1822 case 0: return "AAPCS (base variant)";
1823 case 1: return "AAPCS (VFP variant)";
1824 case 2: return "toolchain-specific";
1826 snprintf(s_va, sizeof(s_va), "Unknown (%ju)", (uintmax_t) va);
1832 aeabi_wmmx_args(uint64_t wa)
1834 static char s_wa[32];
1837 case 0: return "AAPCS (base variant)";
1838 case 1: return "Intel WMMX";
1839 case 2: return "toolchain-specific";
1841 snprintf(s_wa, sizeof(s_wa), "Unknown(%ju)", (uintmax_t) wa);
1847 aeabi_unaligned_access(uint64_t ua)
1849 static char s_ua[32];
1852 case 0: return "Not allowed";
1853 case 1: return "Allowed";
1855 snprintf(s_ua, sizeof(s_ua), "Unknown(%ju)", (uintmax_t) ua);
1861 aeabi_fp_hpext(uint64_t fh)
1863 static char s_fh[32];
1866 case 0: return "Not allowed";
1867 case 1: return "Allowed";
1869 snprintf(s_fh, sizeof(s_fh), "Unknown(%ju)", (uintmax_t) fh);
1875 aeabi_optm_goal(uint64_t og)
1877 static char s_og[32];
1880 case 0: return "None";
1881 case 1: return "Speed";
1882 case 2: return "Speed aggressive";
1883 case 3: return "Space";
1884 case 4: return "Space aggressive";
1885 case 5: return "Debugging";
1886 case 6: return "Best Debugging";
1888 snprintf(s_og, sizeof(s_og), "Unknown(%ju)", (uintmax_t) og);
1894 aeabi_fp_optm_goal(uint64_t fog)
1896 static char s_fog[32];
1899 case 0: return "None";
1900 case 1: return "Speed";
1901 case 2: return "Speed aggressive";
1902 case 3: return "Space";
1903 case 4: return "Space aggressive";
1904 case 5: return "Accurary";
1905 case 6: return "Best Accurary";
1907 snprintf(s_fog, sizeof(s_fog), "Unknown(%ju)",
1914 aeabi_virtual(uint64_t vt)
1916 static char s_virtual[64];
1919 case 0: return "No";
1920 case 1: return "TrustZone";
1921 case 2: return "Virtualization extension";
1922 case 3: return "TrustZone and virtualization extension";
1924 snprintf(s_virtual, sizeof(s_virtual), "Unknown(%ju)",
1933 const char *(*get_desc)(uint64_t val);
1935 {4, "Tag_CPU_raw_name", NULL},
1936 {5, "Tag_CPU_name", NULL},
1937 {6, "Tag_CPU_arch", aeabi_cpu_arch},
1938 {7, "Tag_CPU_arch_profile", aeabi_cpu_arch_profile},
1939 {8, "Tag_ARM_ISA_use", aeabi_arm_isa},
1940 {9, "Tag_THUMB_ISA_use", aeabi_thumb_isa},
1941 {10, "Tag_FP_arch", aeabi_fp_arch},
1942 {11, "Tag_WMMX_arch", aeabi_wmmx_arch},
1943 {12, "Tag_Advanced_SIMD_arch", aeabi_adv_simd_arch},
1944 {13, "Tag_PCS_config", aeabi_pcs_config},
1945 {14, "Tag_ABI_PCS_R9_use", aeabi_pcs_r9},
1946 {15, "Tag_ABI_PCS_RW_data", aeabi_pcs_rw},
1947 {16, "Tag_ABI_PCS_RO_data", aeabi_pcs_ro},
1948 {17, "Tag_ABI_PCS_GOT_use", aeabi_pcs_got},
1949 {18, "Tag_ABI_PCS_wchar_t", aeabi_pcs_wchar_t},
1950 {19, "Tag_ABI_FP_rounding", aeabi_fp_rounding},
1951 {20, "Tag_ABI_FP_denormal", aeabi_fp_denormal},
1952 {21, "Tag_ABI_FP_exceptions", aeabi_fp_exceptions},
1953 {22, "Tag_ABI_FP_user_exceptions", aeabi_fp_user_exceptions},
1954 {23, "Tag_ABI_FP_number_model", aeabi_fp_number_model},
1955 {24, "Tag_ABI_align_needed", aeabi_align_needed},
1956 {25, "Tag_ABI_align_preserved", aeabi_align_preserved},
1957 {26, "Tag_ABI_enum_size", aeabi_enum_size},
1958 {27, "Tag_ABI_HardFP_use", aeabi_hardfp},
1959 {28, "Tag_ABI_VFP_args", aeabi_vfp_args},
1960 {29, "Tag_ABI_WMMX_args", aeabi_wmmx_args},
1961 {30, "Tag_ABI_optimization_goals", aeabi_optm_goal},
1962 {31, "Tag_ABI_FP_optimization_goals", aeabi_fp_optm_goal},
1963 {32, "Tag_compatibility", NULL},
1964 {34, "Tag_CPU_unaligned_access", aeabi_unaligned_access},
1965 {36, "Tag_FP_HP_extension", aeabi_fp_hpext},
1966 {38, "Tag_ABI_FP_16bit_format", aeabi_fp_16bit_format},
1967 {42, "Tag_MPextension_use", aeabi_mpext},
1968 {44, "Tag_DIV_use", aeabi_div},
1969 {64, "Tag_nodefaults", NULL},
1970 {65, "Tag_also_compatible_with", NULL},
1971 {66, "Tag_T2EE_use", aeabi_t2ee},
1972 {67, "Tag_conformance", NULL},
1973 {68, "Tag_Virtualization_use", aeabi_virtual},
1974 {70, "Tag_MPextension_use", aeabi_mpext},
1978 mips_abi_fp(uint64_t fp)
1980 static char s_mips_abi_fp[64];
1983 case 0: return "N/A";
1984 case 1: return "Hard float (double precision)";
1985 case 2: return "Hard float (single precision)";
1986 case 3: return "Soft float";
1987 case 4: return "64-bit float (-mips32r2 -mfp64)";
1989 snprintf(s_mips_abi_fp, sizeof(s_mips_abi_fp), "Unknown(%ju)",
1991 return (s_mips_abi_fp);
1996 ppc_abi_fp(uint64_t fp)
1998 static char s_ppc_abi_fp[64];
2001 case 0: return "N/A";
2002 case 1: return "Hard float (double precision)";
2003 case 2: return "Soft float";
2004 case 3: return "Hard float (single precision)";
2006 snprintf(s_ppc_abi_fp, sizeof(s_ppc_abi_fp), "Unknown(%ju)",
2008 return (s_ppc_abi_fp);
2013 ppc_abi_vector(uint64_t vec)
2015 static char s_vec[64];
2018 case 0: return "N/A";
2019 case 1: return "Generic purpose registers";
2020 case 2: return "AltiVec registers";
2021 case 3: return "SPE registers";
2023 snprintf(s_vec, sizeof(s_vec), "Unknown(%ju)", (uintmax_t) vec);
2029 dwarf_reg(unsigned int mach, unsigned int reg)
2036 case 0: return "eax";
2037 case 1: return "ecx";
2038 case 2: return "edx";
2039 case 3: return "ebx";
2040 case 4: return "esp";
2041 case 5: return "ebp";
2042 case 6: return "esi";
2043 case 7: return "edi";
2044 case 8: return "eip";
2045 case 9: return "eflags";
2046 case 11: return "st0";
2047 case 12: return "st1";
2048 case 13: return "st2";
2049 case 14: return "st3";
2050 case 15: return "st4";
2051 case 16: return "st5";
2052 case 17: return "st6";
2053 case 18: return "st7";
2054 case 21: return "xmm0";
2055 case 22: return "xmm1";
2056 case 23: return "xmm2";
2057 case 24: return "xmm3";
2058 case 25: return "xmm4";
2059 case 26: return "xmm5";
2060 case 27: return "xmm6";
2061 case 28: return "xmm7";
2062 case 29: return "mm0";
2063 case 30: return "mm1";
2064 case 31: return "mm2";
2065 case 32: return "mm3";
2066 case 33: return "mm4";
2067 case 34: return "mm5";
2068 case 35: return "mm6";
2069 case 36: return "mm7";
2070 case 37: return "fcw";
2071 case 38: return "fsw";
2072 case 39: return "mxcsr";
2073 case 40: return "es";
2074 case 41: return "cs";
2075 case 42: return "ss";
2076 case 43: return "ds";
2077 case 44: return "fs";
2078 case 45: return "gs";
2079 case 48: return "tr";
2080 case 49: return "ldtr";
2081 default: return (NULL);
2085 case 0: return "zero";
2086 case 1: return "ra";
2087 case 2: return "sp";
2088 case 3: return "gp";
2089 case 4: return "tp";
2090 case 5: return "t0";
2091 case 6: return "t1";
2092 case 7: return "t2";
2093 case 8: return "s0";
2094 case 9: return "s1";
2095 case 10: return "a0";
2096 case 11: return "a1";
2097 case 12: return "a2";
2098 case 13: return "a3";
2099 case 14: return "a4";
2100 case 15: return "a5";
2101 case 16: return "a6";
2102 case 17: return "a7";
2103 case 18: return "s2";
2104 case 19: return "s3";
2105 case 20: return "s4";
2106 case 21: return "s5";
2107 case 22: return "s6";
2108 case 23: return "s7";
2109 case 24: return "s8";
2110 case 25: return "s9";
2111 case 26: return "s10";
2112 case 27: return "s11";
2113 case 28: return "t3";
2114 case 29: return "t4";
2115 case 30: return "t5";
2116 case 31: return "t6";
2117 case 32: return "ft0";
2118 case 33: return "ft1";
2119 case 34: return "ft2";
2120 case 35: return "ft3";
2121 case 36: return "ft4";
2122 case 37: return "ft5";
2123 case 38: return "ft6";
2124 case 39: return "ft7";
2125 case 40: return "fs0";
2126 case 41: return "fs1";
2127 case 42: return "fa0";
2128 case 43: return "fa1";
2129 case 44: return "fa2";
2130 case 45: return "fa3";
2131 case 46: return "fa4";
2132 case 47: return "fa5";
2133 case 48: return "fa6";
2134 case 49: return "fa7";
2135 case 50: return "fs2";
2136 case 51: return "fs3";
2137 case 52: return "fs4";
2138 case 53: return "fs5";
2139 case 54: return "fs6";
2140 case 55: return "fs7";
2141 case 56: return "fs8";
2142 case 57: return "fs9";
2143 case 58: return "fs10";
2144 case 59: return "fs11";
2145 case 60: return "ft8";
2146 case 61: return "ft9";
2147 case 62: return "ft10";
2148 case 63: return "ft11";
2149 default: return (NULL);
2153 case 0: return "rax";
2154 case 1: return "rdx";
2155 case 2: return "rcx";
2156 case 3: return "rbx";
2157 case 4: return "rsi";
2158 case 5: return "rdi";
2159 case 6: return "rbp";
2160 case 7: return "rsp";
2161 case 16: return "rip";
2162 case 17: return "xmm0";
2163 case 18: return "xmm1";
2164 case 19: return "xmm2";
2165 case 20: return "xmm3";
2166 case 21: return "xmm4";
2167 case 22: return "xmm5";
2168 case 23: return "xmm6";
2169 case 24: return "xmm7";
2170 case 25: return "xmm8";
2171 case 26: return "xmm9";
2172 case 27: return "xmm10";
2173 case 28: return "xmm11";
2174 case 29: return "xmm12";
2175 case 30: return "xmm13";
2176 case 31: return "xmm14";
2177 case 32: return "xmm15";
2178 case 33: return "st0";
2179 case 34: return "st1";
2180 case 35: return "st2";
2181 case 36: return "st3";
2182 case 37: return "st4";
2183 case 38: return "st5";
2184 case 39: return "st6";
2185 case 40: return "st7";
2186 case 41: return "mm0";
2187 case 42: return "mm1";
2188 case 43: return "mm2";
2189 case 44: return "mm3";
2190 case 45: return "mm4";
2191 case 46: return "mm5";
2192 case 47: return "mm6";
2193 case 48: return "mm7";
2194 case 49: return "rflags";
2195 case 50: return "es";
2196 case 51: return "cs";
2197 case 52: return "ss";
2198 case 53: return "ds";
2199 case 54: return "fs";
2200 case 55: return "gs";
2201 case 58: return "fs.base";
2202 case 59: return "gs.base";
2203 case 62: return "tr";
2204 case 63: return "ldtr";
2205 case 64: return "mxcsr";
2206 case 65: return "fcw";
2207 case 66: return "fsw";
2208 default: return (NULL);
2216 dump_ehdr(struct readelf *re)
2218 size_t phnum, shnum, shstrndx;
2221 printf("ELF Header:\n");
2225 for (i = 0; i < EI_NIDENT; i++)
2226 printf("%.2x ", re->ehdr.e_ident[i]);
2230 printf("%-37s%s\n", " Class:", elf_class(re->ehdr.e_ident[EI_CLASS]));
2233 printf("%-37s%s\n", " Data:", elf_endian(re->ehdr.e_ident[EI_DATA]));
2236 printf("%-37s%d %s\n", " Version:", re->ehdr.e_ident[EI_VERSION],
2237 elf_ver(re->ehdr.e_ident[EI_VERSION]));
2240 printf("%-37s%s\n", " OS/ABI:", elf_osabi(re->ehdr.e_ident[EI_OSABI]));
2242 /* EI_ABIVERSION. */
2243 printf("%-37s%d\n", " ABI Version:", re->ehdr.e_ident[EI_ABIVERSION]);
2246 printf("%-37s%s\n", " Type:", elf_type(re->ehdr.e_type));
2249 printf("%-37s%s\n", " Machine:", elf_machine(re->ehdr.e_machine));
2252 printf("%-37s%#x\n", " Version:", re->ehdr.e_version);
2255 printf("%-37s%#jx\n", " Entry point address:",
2256 (uintmax_t)re->ehdr.e_entry);
2259 printf("%-37s%ju (bytes into file)\n", " Start of program headers:",
2260 (uintmax_t)re->ehdr.e_phoff);
2263 printf("%-37s%ju (bytes into file)\n", " Start of section headers:",
2264 (uintmax_t)re->ehdr.e_shoff);
2267 printf("%-37s%#x", " Flags:", re->ehdr.e_flags);
2268 dump_eflags(re, re->ehdr.e_flags);
2272 printf("%-37s%u (bytes)\n", " Size of this header:",
2276 printf("%-37s%u (bytes)\n", " Size of program headers:",
2277 re->ehdr.e_phentsize);
2280 printf("%-37s%u", " Number of program headers:", re->ehdr.e_phnum);
2281 if (re->ehdr.e_phnum == PN_XNUM) {
2282 /* Extended program header numbering is in use. */
2283 if (elf_getphnum(re->elf, &phnum))
2284 printf(" (%zu)", phnum);
2289 printf("%-37s%u (bytes)\n", " Size of section headers:",
2290 re->ehdr.e_shentsize);
2293 printf("%-37s%u", " Number of section headers:", re->ehdr.e_shnum);
2294 if (re->ehdr.e_shnum == SHN_UNDEF) {
2295 /* Extended section numbering is in use. */
2296 if (elf_getshnum(re->elf, &shnum))
2297 printf(" (%ju)", (uintmax_t)shnum);
2302 printf("%-37s%u", " Section header string table index:",
2303 re->ehdr.e_shstrndx);
2304 if (re->ehdr.e_shstrndx == SHN_XINDEX) {
2305 /* Extended section numbering is in use. */
2306 if (elf_getshstrndx(re->elf, &shstrndx))
2307 printf(" (%ju)", (uintmax_t)shstrndx);
2313 dump_eflags(struct readelf *re, uint64_t e_flags)
2315 struct eflags_desc *edesc;
2319 switch (re->ehdr.e_machine) {
2321 arm_eabi = (e_flags & EF_ARM_EABIMASK) >> 24;
2323 printf(", GNU EABI");
2324 else if (arm_eabi <= 5)
2325 printf(", Version%d EABI", arm_eabi);
2326 edesc = arm_eflags_desc;
2329 case EM_MIPS_RS3_LE:
2330 switch ((e_flags & EF_MIPS_ARCH) >> 28) {
2331 case 0: printf(", mips1"); break;
2332 case 1: printf(", mips2"); break;
2333 case 2: printf(", mips3"); break;
2334 case 3: printf(", mips4"); break;
2335 case 4: printf(", mips5"); break;
2336 case 5: printf(", mips32"); break;
2337 case 6: printf(", mips64"); break;
2338 case 7: printf(", mips32r2"); break;
2339 case 8: printf(", mips64r2"); break;
2342 switch ((e_flags & 0x00FF0000) >> 16) {
2343 case 0x81: printf(", 3900"); break;
2344 case 0x82: printf(", 4010"); break;
2345 case 0x83: printf(", 4100"); break;
2346 case 0x85: printf(", 4650"); break;
2347 case 0x87: printf(", 4120"); break;
2348 case 0x88: printf(", 4111"); break;
2349 case 0x8a: printf(", sb1"); break;
2350 case 0x8b: printf(", octeon"); break;
2351 case 0x8c: printf(", xlr"); break;
2352 case 0x91: printf(", 5400"); break;
2353 case 0x98: printf(", 5500"); break;
2354 case 0x99: printf(", 9000"); break;
2355 case 0xa0: printf(", loongson-2e"); break;
2356 case 0xa1: printf(", loongson-2f"); break;
2359 switch ((e_flags & 0x0000F000) >> 12) {
2360 case 1: printf(", o32"); break;
2361 case 2: printf(", o64"); break;
2362 case 3: printf(", eabi32"); break;
2363 case 4: printf(", eabi64"); break;
2366 edesc = mips_eflags_desc;
2370 case 0: printf(", Unspecified or Power ELF V1 ABI"); break;
2371 case 1: printf(", Power ELF V1 ABI"); break;
2372 case 2: printf(", OpenPOWER ELF V2 ABI"); break;
2375 /* explicit fall through*/
2377 edesc = powerpc_eflags_desc;
2380 switch (e_flags & EF_RISCV_FLOAT_ABI_MASK) {
2381 case EF_RISCV_FLOAT_ABI_SOFT:
2382 printf(", soft-float ABI");
2384 case EF_RISCV_FLOAT_ABI_SINGLE:
2385 printf(", single-float ABI");
2387 case EF_RISCV_FLOAT_ABI_DOUBLE:
2388 printf(", double-float ABI");
2390 case EF_RISCV_FLOAT_ABI_QUAD:
2391 printf(", quad-float ABI");
2394 edesc = riscv_eflags_desc;
2397 case EM_SPARC32PLUS:
2399 switch ((e_flags & EF_SPARCV9_MM)) {
2400 case EF_SPARCV9_TSO: printf(", tso"); break;
2401 case EF_SPARCV9_PSO: printf(", pso"); break;
2402 case EF_SPARCV9_MM: printf(", rmo"); break;
2405 edesc = sparc_eflags_desc;
2411 if (edesc != NULL) {
2412 while (edesc->desc != NULL) {
2413 if (e_flags & edesc->flag)
2414 printf(", %s", edesc->desc);
2421 dump_phdr(struct readelf *re)
2423 const char *rawfile;
2428 #define PH_HDR "Type", "Offset", "VirtAddr", "PhysAddr", "FileSiz", \
2429 "MemSiz", "Flg", "Align"
2430 #define PH_CT phdr_type(re->ehdr.e_machine, phdr.p_type), \
2431 (uintmax_t)phdr.p_offset, (uintmax_t)phdr.p_vaddr, \
2432 (uintmax_t)phdr.p_paddr, (uintmax_t)phdr.p_filesz, \
2433 (uintmax_t)phdr.p_memsz, \
2434 phdr.p_flags & PF_R ? 'R' : ' ', \
2435 phdr.p_flags & PF_W ? 'W' : ' ', \
2436 phdr.p_flags & PF_X ? 'E' : ' ', \
2437 (uintmax_t)phdr.p_align
2439 if (elf_getphnum(re->elf, &phnum) == 0) {
2440 warnx("elf_getphnum failed: %s", elf_errmsg(-1));
2444 printf("\nThere are no program headers in this file.\n");
2448 printf("\nElf file type is %s", elf_type(re->ehdr.e_type));
2449 printf("\nEntry point 0x%jx\n", (uintmax_t)re->ehdr.e_entry);
2450 printf("There are %ju program headers, starting at offset %ju\n",
2451 (uintmax_t)phnum, (uintmax_t)re->ehdr.e_phoff);
2453 /* Dump program headers. */
2454 printf("\nProgram Headers:\n");
2455 if (re->ec == ELFCLASS32)
2456 printf(" %-15s%-9s%-11s%-11s%-8s%-8s%-4s%s\n", PH_HDR);
2457 else if (re->options & RE_WW)
2458 printf(" %-15s%-9s%-19s%-19s%-9s%-9s%-4s%s\n", PH_HDR);
2460 printf(" %-15s%-19s%-19s%s\n %-19s%-20s"
2461 "%-7s%s\n", PH_HDR);
2462 for (i = 0; (size_t) i < phnum; i++) {
2463 if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
2464 warnx("gelf_getphdr failed: %s", elf_errmsg(-1));
2467 /* TODO: Add arch-specific segment type dump. */
2468 if (re->ec == ELFCLASS32)
2469 printf(" %-14.14s 0x%6.6jx 0x%8.8jx 0x%8.8jx "
2470 "0x%5.5jx 0x%5.5jx %c%c%c %#jx\n", PH_CT);
2471 else if (re->options & RE_WW)
2472 printf(" %-14.14s 0x%6.6jx 0x%16.16jx 0x%16.16jx "
2473 "0x%6.6jx 0x%6.6jx %c%c%c %#jx\n", PH_CT);
2475 printf(" %-14.14s 0x%16.16jx 0x%16.16jx 0x%16.16jx\n"
2476 " 0x%16.16jx 0x%16.16jx %c%c%c"
2478 if (phdr.p_type == PT_INTERP) {
2479 if ((rawfile = elf_rawfile(re->elf, &size)) == NULL) {
2480 warnx("elf_rawfile failed: %s", elf_errmsg(-1));
2483 if (phdr.p_offset >= size) {
2484 warnx("invalid program header offset");
2487 printf(" [Requesting program interpreter: %s]\n",
2488 rawfile + phdr.p_offset);
2492 /* Dump section to segment mapping. */
2495 printf("\n Section to Segment mapping:\n");
2496 printf(" Segment Sections...\n");
2497 for (i = 0; (size_t)i < phnum; i++) {
2498 if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
2499 warnx("gelf_getphdr failed: %s", elf_errmsg(-1));
2502 printf(" %2.2d ", i);
2503 /* skip NULL section. */
2504 for (j = 1; (size_t)j < re->shnum; j++) {
2505 if (re->sl[j].off < phdr.p_offset)
2507 if (re->sl[j].off + re->sl[j].sz >
2508 phdr.p_offset + phdr.p_filesz &&
2509 re->sl[j].type != SHT_NOBITS)
2511 if (re->sl[j].addr < phdr.p_vaddr ||
2512 re->sl[j].addr + re->sl[j].sz >
2513 phdr.p_vaddr + phdr.p_memsz)
2515 if (phdr.p_type == PT_TLS &&
2516 (re->sl[j].flags & SHF_TLS) == 0)
2518 printf("%s ", re->sl[j].name);
2527 section_flags(struct readelf *re, struct section *s)
2530 static char buf[BUF_SZ];
2534 nb = re->ec == ELFCLASS32 ? 8 : 16;
2535 if (re->options & RE_T) {
2536 snprintf(buf, BUF_SZ, "[%*.*jx]: ", nb, nb,
2537 (uintmax_t)s->flags);
2540 for (i = 0; section_flag[i].ln != NULL; i++) {
2541 if ((s->flags & section_flag[i].value) == 0)
2543 if (re->options & RE_T) {
2544 snprintf(&buf[p], BUF_SZ - p, "%s, ",
2545 section_flag[i].ln);
2546 p += strlen(section_flag[i].ln) + 2;
2548 buf[p++] = section_flag[i].sn;
2550 if (re->options & RE_T && p > nb + 4)
2558 dump_shdr(struct readelf *re)
2563 #define S_HDR "[Nr] Name", "Type", "Addr", "Off", "Size", "ES", \
2564 "Flg", "Lk", "Inf", "Al"
2565 #define S_HDRL "[Nr] Name", "Type", "Address", "Offset", "Size", \
2566 "EntSize", "Flags", "Link", "Info", "Align"
2567 #define ST_HDR "[Nr] Name", "Type", "Addr", "Off", "Size", "ES", \
2568 "Lk", "Inf", "Al", "Flags"
2569 #define ST_HDRL "[Nr] Name", "Type", "Address", "Offset", "Link", \
2570 "Size", "EntSize", "Info", "Align", "Flags"
2571 #define S_CT i, s->name, section_type(re->ehdr.e_machine, s->type), \
2572 (uintmax_t)s->addr, (uintmax_t)s->off, (uintmax_t)s->sz,\
2573 (uintmax_t)s->entsize, section_flags(re, s), \
2574 s->link, s->info, (uintmax_t)s->align
2575 #define ST_CT i, s->name, section_type(re->ehdr.e_machine, s->type), \
2576 (uintmax_t)s->addr, (uintmax_t)s->off, (uintmax_t)s->sz,\
2577 (uintmax_t)s->entsize, s->link, s->info, \
2578 (uintmax_t)s->align, section_flags(re, s)
2579 #define ST_CTL i, s->name, section_type(re->ehdr.e_machine, s->type), \
2580 (uintmax_t)s->addr, (uintmax_t)s->off, s->link, \
2581 (uintmax_t)s->sz, (uintmax_t)s->entsize, s->info, \
2582 (uintmax_t)s->align, section_flags(re, s)
2584 if (re->shnum == 0) {
2585 printf("\nThere are no sections in this file.\n");
2588 printf("There are %ju section headers, starting at offset 0x%jx:\n",
2589 (uintmax_t)re->shnum, (uintmax_t)re->ehdr.e_shoff);
2590 printf("\nSection Headers:\n");
2591 if (re->ec == ELFCLASS32) {
2592 if (re->options & RE_T)
2593 printf(" %s\n %-16s%-9s%-7s%-7s%-5s%-3s%-4s%s\n"
2596 printf(" %-23s%-16s%-9s%-7s%-7s%-3s%-4s%-3s%-4s%s\n",
2598 } else if (re->options & RE_WW) {
2599 if (re->options & RE_T)
2600 printf(" %s\n %-16s%-17s%-7s%-7s%-5s%-3s%-4s%s\n"
2603 printf(" %-23s%-16s%-17s%-7s%-7s%-3s%-4s%-3s%-4s%s\n",
2606 if (re->options & RE_T)
2607 printf(" %s\n %-18s%-17s%-18s%s\n %-18s"
2608 "%-17s%-18s%s\n%12s\n", ST_HDRL);
2610 printf(" %-23s%-17s%-18s%s\n %-18s%-17s%-7s%"
2611 "-6s%-6s%s\n", S_HDRL);
2613 for (i = 0; (size_t)i < re->shnum; i++) {
2615 if (re->ec == ELFCLASS32) {
2616 if (re->options & RE_T)
2617 printf(" [%2d] %s\n %-15.15s %8.8jx"
2618 " %6.6jx %6.6jx %2.2jx %2u %3u %2ju\n"
2621 printf(" [%2d] %-17.17s %-15.15s %8.8jx"
2622 " %6.6jx %6.6jx %2.2jx %3s %2u %3u %2ju\n",
2624 } else if (re->options & RE_WW) {
2625 if (re->options & RE_T)
2626 printf(" [%2d] %s\n %-15.15s %16.16jx"
2627 " %6.6jx %6.6jx %2.2jx %2u %3u %2ju\n"
2630 printf(" [%2d] %-17.17s %-15.15s %16.16jx"
2631 " %6.6jx %6.6jx %2.2jx %3s %2u %3u %2ju\n",
2634 if (re->options & RE_T)
2635 printf(" [%2d] %s\n %-15.15s %16.16jx"
2636 " %16.16jx %u\n %16.16jx %16.16jx"
2637 " %-16u %ju\n %s\n", ST_CTL);
2639 printf(" [%2d] %-17.17s %-15.15s %16.16jx"
2640 " %8.8jx\n %16.16jx %16.16jx "
2641 "%3s %2u %3u %ju\n", S_CT);
2644 if ((re->options & RE_T) == 0)
2645 printf("Key to Flags:\n W (write), A (alloc),"
2646 " X (execute), M (merge), S (strings)\n"
2647 " I (info), L (link order), G (group), x (unknown)\n"
2648 " O (extra OS processing required)"
2649 " o (OS specific), p (processor specific)\n");
2661 * Return number of entries in the given section. We'd prefer ent_count be a
2662 * size_t *, but libelf APIs already use int for section indices.
2665 get_ent_count(struct section *s, int *ent_count)
2667 if (s->entsize == 0) {
2668 warnx("section %s has entry size 0", s->name);
2670 } else if (s->sz / s->entsize > INT_MAX) {
2671 warnx("section %s has invalid section count", s->name);
2674 *ent_count = (int)(s->sz / s->entsize);
2679 dump_dynamic(struct readelf *re)
2684 int elferr, i, is_dynamic, j, jmax, nentries;
2688 for (i = 0; (size_t)i < re->shnum; i++) {
2690 if (s->type != SHT_DYNAMIC)
2693 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
2694 elferr = elf_errno();
2696 warnx("elf_getdata failed: %s", elf_errmsg(-1));
2704 /* Determine the actual number of table entries. */
2706 if (!get_ent_count(s, &jmax))
2708 for (j = 0; j < jmax; j++) {
2709 if (gelf_getdyn(d, j, &dyn) != &dyn) {
2710 warnx("gelf_getdyn failed: %s",
2715 if (dyn.d_tag == DT_NULL)
2719 printf("\nDynamic section at offset 0x%jx", (uintmax_t)s->off);
2720 printf(" contains %u entries:\n", nentries);
2722 if (re->ec == ELFCLASS32)
2723 printf("%5s%12s%28s\n", "Tag", "Type", "Name/Value");
2725 printf("%5s%20s%28s\n", "Tag", "Type", "Name/Value");
2727 for (j = 0; j < nentries; j++) {
2728 if (gelf_getdyn(d, j, &dyn) != &dyn)
2730 /* Dump dynamic entry type. */
2731 if (re->ec == ELFCLASS32)
2732 printf(" 0x%8.8jx", (uintmax_t)dyn.d_tag);
2734 printf(" 0x%16.16jx", (uintmax_t)dyn.d_tag);
2735 printf(" %-20s", dt_type(re->ehdr.e_machine,
2737 /* Dump dynamic entry value. */
2738 dump_dyn_val(re, &dyn, s->link);
2743 printf("\nThere is no dynamic section in this file.\n");
2747 timestamp(time_t ti)
2753 snprintf(ts, sizeof(ts), "%04d-%02d-%02dT%02d:%02d:%02d",
2754 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour,
2755 t->tm_min, t->tm_sec);
2761 dyn_str(struct readelf *re, uint32_t stab, uint64_t d_val)
2765 if (stab == SHN_UNDEF)
2767 else if ((name = elf_strptr(re->elf, stab, d_val)) == NULL) {
2768 (void) elf_errno(); /* clear error */
2776 dump_arch_dyn_val(struct readelf *re, GElf_Dyn *dyn)
2778 switch (re->ehdr.e_machine) {
2780 case EM_MIPS_RS3_LE:
2781 switch (dyn->d_tag) {
2782 case DT_MIPS_RLD_VERSION:
2783 case DT_MIPS_LOCAL_GOTNO:
2784 case DT_MIPS_CONFLICTNO:
2785 case DT_MIPS_LIBLISTNO:
2786 case DT_MIPS_SYMTABNO:
2787 case DT_MIPS_UNREFEXTNO:
2788 case DT_MIPS_GOTSYM:
2789 case DT_MIPS_HIPAGENO:
2790 case DT_MIPS_DELTA_CLASS_NO:
2791 case DT_MIPS_DELTA_INSTANCE_NO:
2792 case DT_MIPS_DELTA_RELOC_NO:
2793 case DT_MIPS_DELTA_SYM_NO:
2794 case DT_MIPS_DELTA_CLASSSYM_NO:
2795 case DT_MIPS_LOCALPAGE_GOTIDX:
2796 case DT_MIPS_LOCAL_GOTIDX:
2797 case DT_MIPS_HIDDEN_GOTIDX:
2798 case DT_MIPS_PROTECTED_GOTIDX:
2799 printf(" %ju\n", (uintmax_t) dyn->d_un.d_val);
2801 case DT_MIPS_ICHECKSUM:
2803 case DT_MIPS_BASE_ADDRESS:
2804 case DT_MIPS_CONFLICT:
2805 case DT_MIPS_LIBLIST:
2806 case DT_MIPS_RLD_MAP:
2807 case DT_MIPS_DELTA_CLASS:
2808 case DT_MIPS_DELTA_INSTANCE:
2809 case DT_MIPS_DELTA_RELOC:
2810 case DT_MIPS_DELTA_SYM:
2811 case DT_MIPS_DELTA_CLASSSYM:
2812 case DT_MIPS_CXX_FLAGS:
2813 case DT_MIPS_PIXIE_INIT:
2814 case DT_MIPS_SYMBOL_LIB:
2815 case DT_MIPS_OPTIONS:
2816 case DT_MIPS_INTERFACE:
2817 case DT_MIPS_DYNSTR_ALIGN:
2818 case DT_MIPS_INTERFACE_SIZE:
2819 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
2820 case DT_MIPS_COMPACT_SIZE:
2821 case DT_MIPS_GP_VALUE:
2822 case DT_MIPS_AUX_DYNAMIC:
2823 case DT_MIPS_PLTGOT:
2824 case DT_MIPS_RLD_OBJ_UPDATE:
2826 printf(" 0x%jx\n", (uintmax_t) dyn->d_un.d_val);
2828 case DT_MIPS_IVERSION:
2829 case DT_MIPS_PERF_SUFFIX:
2830 case DT_MIPS_TIME_STAMP:
2831 printf(" %s\n", timestamp(dyn->d_un.d_val));
2845 dump_flags(struct flag_desc *desc, uint64_t val)
2847 struct flag_desc *fd;
2849 for (fd = desc; fd->flag != 0; fd++) {
2850 if (val & fd->flag) {
2852 printf(" %s", fd->desc);
2856 printf(" unknown (0x%jx)", (uintmax_t)val);
2860 static struct flag_desc dt_flags[] = {
2861 { DF_ORIGIN, "ORIGIN" },
2862 { DF_SYMBOLIC, "SYMBOLIC" },
2863 { DF_TEXTREL, "TEXTREL" },
2864 { DF_BIND_NOW, "BIND_NOW" },
2865 { DF_STATIC_TLS, "STATIC_TLS" },
2869 static struct flag_desc dt_flags_1[] = {
2870 { DF_1_BIND_NOW, "NOW" },
2871 { DF_1_GLOBAL, "GLOBAL" },
2873 { DF_1_NODELETE, "NODELETE" },
2874 { DF_1_LOADFLTR, "LOADFLTR" },
2875 { 0x20, "INITFIRST" },
2876 { DF_1_NOOPEN, "NOOPEN" },
2877 { DF_1_ORIGIN, "ORIGIN" },
2878 { 0x100, "DIRECT" },
2879 { DF_1_INTERPOSE, "INTERPOSE" },
2880 { DF_1_NODEFLIB, "NODEFLIB" },
2881 { 0x1000, "NODUMP" },
2882 { 0x2000, "CONFALT" },
2883 { 0x4000, "ENDFILTEE" },
2884 { 0x8000, "DISPRELDNE" },
2885 { 0x10000, "DISPRELPND" },
2886 { 0x20000, "NODIRECT" },
2887 { 0x40000, "IGNMULDEF" },
2888 { 0x80000, "NOKSYMS" },
2889 { 0x100000, "NOHDR" },
2890 { 0x200000, "EDITED" },
2891 { 0x400000, "NORELOC" },
2892 { 0x800000, "SYMINTPOSE" },
2893 { 0x1000000, "GLOBAUDIT" },
2898 dump_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab)
2902 if (dyn->d_tag >= DT_LOPROC && dyn->d_tag <= DT_HIPROC &&
2903 dyn->d_tag != DT_AUXILIARY && dyn->d_tag != DT_FILTER) {
2904 dump_arch_dyn_val(re, dyn);
2908 /* These entry values are index into the string table. */
2910 if (dyn->d_tag == DT_AUXILIARY || dyn->d_tag == DT_FILTER ||
2911 dyn->d_tag == DT_NEEDED || dyn->d_tag == DT_SONAME ||
2912 dyn->d_tag == DT_RPATH || dyn->d_tag == DT_RUNPATH)
2913 name = dyn_str(re, stab, dyn->d_un.d_val);
2915 switch(dyn->d_tag) {
2933 case DT_GNU_LIBLIST:
2934 case DT_GNU_CONFLICT:
2935 printf(" 0x%jx\n", (uintmax_t) dyn->d_un.d_val);
2944 case DT_PREINIT_ARRAYSZ:
2945 case DT_INIT_ARRAYSZ:
2946 case DT_FINI_ARRAYSZ:
2947 case DT_GNU_CONFLICTSZ:
2948 case DT_GNU_LIBLISTSZ:
2949 printf(" %ju (bytes)\n", (uintmax_t) dyn->d_un.d_val);
2955 printf(" %ju\n", (uintmax_t) dyn->d_un.d_val);
2958 printf(" Auxiliary library: [%s]\n", name);
2961 printf(" Filter library: [%s]\n", name);
2964 printf(" Shared library: [%s]\n", name);
2967 printf(" Library soname: [%s]\n", name);
2970 printf(" Library rpath: [%s]\n", name);
2973 printf(" Library runpath: [%s]\n", name);
2976 printf(" %s\n", dt_type(re->ehdr.e_machine, dyn->d_un.d_val));
2978 case DT_GNU_PRELINKED:
2979 printf(" %s\n", timestamp(dyn->d_un.d_val));
2982 dump_flags(dt_flags, dyn->d_un.d_val);
2985 dump_flags(dt_flags_1, dyn->d_un.d_val);
2993 dump_rel(struct readelf *re, struct section *s, Elf_Data *d)
2996 const char *symname;
3000 uint8_t type2, type3;
3002 if (s->link >= re->shnum)
3005 #define REL_HDR "r_offset", "r_info", "r_type", "st_value", "st_name"
3006 #define REL_CT32 (uintmax_t)r.r_offset, (uintmax_t)r.r_info, \
3007 elftc_reloc_type_str(re->ehdr.e_machine, \
3008 ELF32_R_TYPE(r.r_info)), (uintmax_t)symval, symname
3009 #define REL_CT64 (uintmax_t)r.r_offset, (uintmax_t)r.r_info, \
3010 elftc_reloc_type_str(re->ehdr.e_machine, type), \
3011 (uintmax_t)symval, symname
3013 printf("\nRelocation section (%s):\n", s->name);
3014 if (re->ec == ELFCLASS32)
3015 printf("%-8s %-8s %-19s %-8s %s\n", REL_HDR);
3017 if (re->options & RE_WW)
3018 printf("%-16s %-16s %-24s %-16s %s\n", REL_HDR);
3020 printf("%-12s %-12s %-19s %-16s %s\n", REL_HDR);
3022 assert(d->d_size == s->sz);
3023 if (!get_ent_count(s, &len))
3025 for (i = 0; i < len; i++) {
3026 if (gelf_getrel(d, i, &r) != &r) {
3027 warnx("gelf_getrel failed: %s", elf_errmsg(-1));
3030 symname = get_symbol_name(re, s->link, GELF_R_SYM(r.r_info));
3031 symval = get_symbol_value(re, s->link, GELF_R_SYM(r.r_info));
3032 if (re->ec == ELFCLASS32) {
3033 r.r_info = ELF32_R_INFO(ELF64_R_SYM(r.r_info),
3034 ELF64_R_TYPE(r.r_info));
3035 printf("%8.8jx %8.8jx %-19.19s %8.8jx %s\n", REL_CT32);
3037 type = ELF64_R_TYPE(r.r_info);
3038 if (re->ehdr.e_machine == EM_MIPS) {
3039 type2 = (type >> 8) & 0xFF;
3040 type3 = (type >> 16) & 0xFF;
3045 if (re->options & RE_WW)
3046 printf("%16.16jx %16.16jx %-24.24s"
3047 " %16.16jx %s\n", REL_CT64);
3049 printf("%12.12jx %12.12jx %-19.19s"
3050 " %16.16jx %s\n", REL_CT64);
3051 if (re->ehdr.e_machine == EM_MIPS) {
3052 if (re->options & RE_WW) {
3053 printf("%32s: %s\n", "Type2",
3054 elftc_reloc_type_str(EM_MIPS,
3056 printf("%32s: %s\n", "Type3",
3057 elftc_reloc_type_str(EM_MIPS,
3060 printf("%24s: %s\n", "Type2",
3061 elftc_reloc_type_str(EM_MIPS,
3063 printf("%24s: %s\n", "Type3",
3064 elftc_reloc_type_str(EM_MIPS,
3076 dump_rela(struct readelf *re, struct section *s, Elf_Data *d)
3079 const char *symname;
3083 uint8_t type2, type3;
3085 if (s->link >= re->shnum)
3088 #define RELA_HDR "r_offset", "r_info", "r_type", "st_value", \
3089 "st_name + r_addend"
3090 #define RELA_CT32 (uintmax_t)r.r_offset, (uintmax_t)r.r_info, \
3091 elftc_reloc_type_str(re->ehdr.e_machine, \
3092 ELF32_R_TYPE(r.r_info)), (uintmax_t)symval, symname
3093 #define RELA_CT64 (uintmax_t)r.r_offset, (uintmax_t)r.r_info, \
3094 elftc_reloc_type_str(re->ehdr.e_machine, type), \
3095 (uintmax_t)symval, symname
3097 printf("\nRelocation section with addend (%s):\n", s->name);
3098 if (re->ec == ELFCLASS32)
3099 printf("%-8s %-8s %-19s %-8s %s\n", RELA_HDR);
3101 if (re->options & RE_WW)
3102 printf("%-16s %-16s %-24s %-16s %s\n", RELA_HDR);
3104 printf("%-12s %-12s %-19s %-16s %s\n", RELA_HDR);
3106 assert(d->d_size == s->sz);
3107 if (!get_ent_count(s, &len))
3109 for (i = 0; i < len; i++) {
3110 if (gelf_getrela(d, i, &r) != &r) {
3111 warnx("gelf_getrel failed: %s", elf_errmsg(-1));
3114 symname = get_symbol_name(re, s->link, GELF_R_SYM(r.r_info));
3115 symval = get_symbol_value(re, s->link, GELF_R_SYM(r.r_info));
3116 if (re->ec == ELFCLASS32) {
3117 r.r_info = ELF32_R_INFO(ELF64_R_SYM(r.r_info),
3118 ELF64_R_TYPE(r.r_info));
3119 printf("%8.8jx %8.8jx %-19.19s %8.8jx %s", RELA_CT32);
3120 printf(" + %x\n", (uint32_t) r.r_addend);
3122 type = ELF64_R_TYPE(r.r_info);
3123 if (re->ehdr.e_machine == EM_MIPS) {
3124 type2 = (type >> 8) & 0xFF;
3125 type3 = (type >> 16) & 0xFF;
3130 if (re->options & RE_WW)
3131 printf("%16.16jx %16.16jx %-24.24s"
3132 " %16.16jx %s", RELA_CT64);
3134 printf("%12.12jx %12.12jx %-19.19s"
3135 " %16.16jx %s", RELA_CT64);
3136 printf(" + %jx\n", (uintmax_t) r.r_addend);
3137 if (re->ehdr.e_machine == EM_MIPS) {
3138 if (re->options & RE_WW) {
3139 printf("%32s: %s\n", "Type2",
3140 elftc_reloc_type_str(EM_MIPS,
3142 printf("%32s: %s\n", "Type3",
3143 elftc_reloc_type_str(EM_MIPS,
3146 printf("%24s: %s\n", "Type2",
3147 elftc_reloc_type_str(EM_MIPS,
3149 printf("%24s: %s\n", "Type3",
3150 elftc_reloc_type_str(EM_MIPS,
3162 dump_reloc(struct readelf *re)
3168 for (i = 0; (size_t)i < re->shnum; i++) {
3170 if (s->type == SHT_REL || s->type == SHT_RELA) {
3172 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3173 elferr = elf_errno();
3175 warnx("elf_getdata failed: %s",
3176 elf_errmsg(elferr));
3179 if (s->type == SHT_REL)
3182 dump_rela(re, s, d);
3188 dump_symtab(struct readelf *re, int i)
3199 if (s->link >= re->shnum)
3203 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3204 elferr = elf_errno();
3206 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
3211 if (!get_ent_count(s, &len))
3213 printf("Symbol table (%s)", s->name);
3214 printf(" contains %d entries:\n", len);
3215 printf("%7s%9s%14s%5s%8s%6s%9s%5s\n", "Num:", "Value", "Size", "Type",
3216 "Bind", "Vis", "Ndx", "Name");
3218 for (j = 0; j < len; j++) {
3219 if (gelf_getsym(d, j, &sym) != &sym) {
3220 warnx("gelf_getsym failed: %s", elf_errmsg(-1));
3224 printf(" %16.16jx", (uintmax_t) sym.st_value);
3225 printf(" %5ju", (uintmax_t) sym.st_size);
3226 printf(" %-7s", st_type(re->ehdr.e_machine,
3227 re->ehdr.e_ident[EI_OSABI], GELF_ST_TYPE(sym.st_info)));
3228 printf(" %-6s", st_bind(GELF_ST_BIND(sym.st_info)));
3229 printf(" %-8s", st_vis(GELF_ST_VISIBILITY(sym.st_other)));
3230 printf(" %3s", st_shndx(sym.st_shndx));
3231 if ((name = elf_strptr(re->elf, stab, sym.st_name)) != NULL)
3232 printf(" %s", name);
3233 /* Append symbol version string for SHT_DYNSYM symbol table. */
3234 if (s->type == SHT_DYNSYM && re->ver != NULL &&
3235 re->vs != NULL && re->vs[j] > 1) {
3236 vs = re->vs[j] & VERSYM_VERSION;
3237 if (vs >= re->ver_sz || re->ver[vs].name == NULL) {
3238 warnx("invalid versym version index %u", vs);
3241 if (re->vs[j] & VERSYM_HIDDEN || re->ver[vs].type == 0)
3242 printf("@%s (%d)", re->ver[vs].name, vs);
3244 printf("@@%s (%d)", re->ver[vs].name, vs);
3252 dump_symtabs(struct readelf *re)
3261 * If -D is specified, only dump the symbol table specified by
3262 * the DT_SYMTAB entry in the .dynamic section.
3265 if (re->options & RE_DD) {
3267 for (i = 0; (size_t)i < re->shnum; i++)
3268 if (re->sl[i].type == SHT_DYNAMIC) {
3275 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3276 elferr = elf_errno();
3278 warnx("elf_getdata failed: %s", elf_errmsg(-1));
3283 if (!get_ent_count(s, &len))
3286 for (i = 0; i < len; i++) {
3287 if (gelf_getdyn(d, i, &dyn) != &dyn) {
3288 warnx("gelf_getdyn failed: %s", elf_errmsg(-1));
3291 if (dyn.d_tag == DT_SYMTAB) {
3292 dyn_off = dyn.d_un.d_val;
3298 /* Find and dump symbol tables. */
3299 for (i = 0; (size_t)i < re->shnum; i++) {
3301 if (s->type == SHT_SYMTAB || s->type == SHT_DYNSYM) {
3302 if (re->options & RE_DD) {
3303 if (dyn_off == s->addr) {
3314 dump_svr4_hash(struct section *s)
3318 uint32_t nbucket, nchain;
3319 uint32_t *bucket, *chain;
3320 uint32_t *bl, *c, maxl, total;
3323 /* Read and parse the content of .hash section. */
3325 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3326 elferr = elf_errno();
3328 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
3331 if (d->d_size < 2 * sizeof(uint32_t)) {
3332 warnx(".hash section too small");
3338 if (nbucket <= 0 || nchain <= 0) {
3339 warnx("Malformed .hash section");
3342 if (d->d_size != (nbucket + nchain + 2) * sizeof(uint32_t)) {
3343 warnx("Malformed .hash section");
3347 chain = &buf[2 + nbucket];
3350 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
3351 errx(EXIT_FAILURE, "calloc failed");
3352 for (i = 0; (uint32_t)i < nbucket; i++)
3353 for (j = bucket[i]; j > 0 && (uint32_t)j < nchain; j = chain[j])
3356 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
3357 errx(EXIT_FAILURE, "calloc failed");
3358 for (i = 0; (uint32_t)i < nbucket; i++)
3360 printf("\nHistogram for bucket list length (total of %u buckets):\n",
3362 printf(" Length\tNumber\t\t%% of total\tCoverage\n");
3364 for (i = 0; (uint32_t)i <= maxl; i++) {
3366 printf("%7u\t%-10u\t(%5.1f%%)\t%5.1f%%\n", i, c[i],
3367 c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
3374 dump_svr4_hash64(struct readelf *re, struct section *s)
3378 uint64_t nbucket, nchain;
3379 uint64_t *bucket, *chain;
3380 uint64_t *bl, *c, maxl, total;
3384 * ALPHA uses 64-bit hash entries. Since libelf assumes that
3385 * .hash section contains only 32-bit entry, an explicit
3386 * gelf_xlatetom is needed here.
3389 if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
3390 elferr = elf_errno();
3392 warnx("elf_rawdata failed: %s",
3393 elf_errmsg(elferr));
3396 d->d_type = ELF_T_XWORD;
3397 memcpy(&dst, d, sizeof(Elf_Data));
3398 if (gelf_xlatetom(re->elf, &dst, d,
3399 re->ehdr.e_ident[EI_DATA]) != &dst) {
3400 warnx("gelf_xlatetom failed: %s", elf_errmsg(-1));
3403 if (dst.d_size < 2 * sizeof(uint64_t)) {
3404 warnx(".hash section too small");
3410 if (nbucket <= 0 || nchain <= 0) {
3411 warnx("Malformed .hash section");
3414 if (d->d_size != (nbucket + nchain + 2) * sizeof(uint32_t)) {
3415 warnx("Malformed .hash section");
3419 chain = &buf[2 + nbucket];
3422 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
3423 errx(EXIT_FAILURE, "calloc failed");
3424 for (i = 0; (uint32_t)i < nbucket; i++)
3425 for (j = bucket[i]; j > 0 && (uint32_t)j < nchain; j = chain[j])
3428 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
3429 errx(EXIT_FAILURE, "calloc failed");
3430 for (i = 0; (uint64_t)i < nbucket; i++)
3432 printf("Histogram for bucket list length (total of %ju buckets):\n",
3433 (uintmax_t)nbucket);
3434 printf(" Length\tNumber\t\t%% of total\tCoverage\n");
3436 for (i = 0; (uint64_t)i <= maxl; i++) {
3438 printf("%7u\t%-10ju\t(%5.1f%%)\t%5.1f%%\n", i, (uintmax_t)c[i],
3439 c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
3446 dump_gnu_hash(struct readelf *re, struct section *s)
3451 uint32_t *bucket, *chain;
3452 uint32_t nbucket, nchain, symndx, maskwords;
3453 uint32_t *bl, *c, maxl, total;
3454 int elferr, dynsymcount, i, j;
3457 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3458 elferr = elf_errno();
3460 warnx("elf_getdata failed: %s",
3461 elf_errmsg(elferr));
3464 if (d->d_size < 4 * sizeof(uint32_t)) {
3465 warnx(".gnu.hash section too small");
3473 if (s->link >= re->shnum)
3475 ds = &re->sl[s->link];
3476 if (!get_ent_count(ds, &dynsymcount))
3478 if (symndx >= (uint32_t)dynsymcount) {
3479 warnx("Malformed .gnu.hash section (symndx out of range)");
3482 nchain = dynsymcount - symndx;
3483 if (d->d_size != 4 * sizeof(uint32_t) + maskwords *
3484 (re->ec == ELFCLASS32 ? sizeof(uint32_t) : sizeof(uint64_t)) +
3485 (nbucket + nchain) * sizeof(uint32_t)) {
3486 warnx("Malformed .gnu.hash section");
3489 bucket = buf + (re->ec == ELFCLASS32 ? maskwords : maskwords * 2);
3490 chain = bucket + nbucket;
3493 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
3494 errx(EXIT_FAILURE, "calloc failed");
3495 for (i = 0; (uint32_t)i < nbucket; i++)
3496 for (j = bucket[i]; j > 0 && (uint32_t)j - symndx < nchain;
3500 if (chain[j - symndx] & 1)
3503 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
3504 errx(EXIT_FAILURE, "calloc failed");
3505 for (i = 0; (uint32_t)i < nbucket; i++)
3507 printf("Histogram for bucket list length (total of %u buckets):\n",
3509 printf(" Length\tNumber\t\t%% of total\tCoverage\n");
3511 for (i = 0; (uint32_t)i <= maxl; i++) {
3513 printf("%7u\t%-10u\t(%5.1f%%)\t%5.1f%%\n", i, c[i],
3514 c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
3521 dump_hash(struct readelf *re)
3526 for (i = 0; (size_t) i < re->shnum; i++) {
3528 if (s->type == SHT_HASH || s->type == SHT_GNU_HASH) {
3529 if (s->type == SHT_GNU_HASH)
3530 dump_gnu_hash(re, s);
3531 else if (re->ehdr.e_machine == EM_ALPHA &&
3533 dump_svr4_hash64(re, s);
3541 dump_notes(struct readelf *re)
3544 const char *rawfile;
3547 size_t filesize, phnum;
3550 if (re->ehdr.e_type == ET_CORE) {
3552 * Search program headers in the core file for
3555 if (elf_getphnum(re->elf, &phnum) == 0) {
3556 warnx("elf_getphnum failed: %s", elf_errmsg(-1));
3561 if ((rawfile = elf_rawfile(re->elf, &filesize)) == NULL) {
3562 warnx("elf_rawfile failed: %s", elf_errmsg(-1));
3565 for (i = 0; (size_t) i < phnum; i++) {
3566 if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
3567 warnx("gelf_getphdr failed: %s",
3571 if (phdr.p_type == PT_NOTE) {
3572 if (phdr.p_offset >= filesize ||
3573 phdr.p_filesz > filesize - phdr.p_offset) {
3574 warnx("invalid PHDR offset");
3577 dump_notes_content(re, rawfile + phdr.p_offset,
3578 phdr.p_filesz, phdr.p_offset);
3584 * For objects other than core files, Search for
3585 * SHT_NOTE sections.
3587 for (i = 0; (size_t) i < re->shnum; i++) {
3589 if (s->type == SHT_NOTE) {
3591 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3592 elferr = elf_errno();
3594 warnx("elf_getdata failed: %s",
3595 elf_errmsg(elferr));
3598 dump_notes_content(re, d->d_buf, d->d_size,
3605 static struct flag_desc note_feature_ctl_flags[] = {
3606 { NT_FREEBSD_FCTL_ASLR_DISABLE, "ASLR_DISABLE" },
3611 dump_notes_data(const char *name, uint32_t type, const char *buf, size_t sz)
3614 const uint32_t *ubuf;
3616 /* Note data is at least 4-byte aligned. */
3617 if (((uintptr_t)buf & 3) != 0) {
3618 warnx("bad note data alignment");
3621 ubuf = (const uint32_t *)(const void *)buf;
3623 if (strcmp(name, "FreeBSD") == 0) {
3625 case NT_FREEBSD_ABI_TAG:
3628 printf(" ABI tag: %u\n", ubuf[0]);
3630 /* NT_FREEBSD_NOINIT_TAG carries no data, treat as unknown. */
3631 case NT_FREEBSD_ARCH_TAG:
3634 printf(" Arch tag: %x\n", ubuf[0]);
3636 case NT_FREEBSD_FEATURE_CTL:
3639 printf(" Features:");
3640 dump_flags(note_feature_ctl_flags, ubuf[0]);
3645 printf(" description data:");
3646 for (i = 0; i < sz; i++)
3647 printf(" %02x", (unsigned char)buf[i]);
3652 dump_notes_content(struct readelf *re, const char *buf, size_t sz, off_t off)
3655 const char *end, *name;
3657 printf("\nNotes at offset %#010jx with length %#010jx:\n",
3658 (uintmax_t) off, (uintmax_t) sz);
3659 printf(" %-13s %-15s %s\n", "Owner", "Data size", "Description");
3662 if (buf + sizeof(*note) > end) {
3663 warnx("invalid note header");
3666 note = (Elf_Note *)(uintptr_t) buf;
3667 buf += sizeof(Elf_Note);
3669 buf += roundup2(note->n_namesz, 4);
3671 * The name field is required to be nul-terminated, and
3672 * n_namesz includes the terminating nul in observed
3673 * implementations (contrary to the ELF-64 spec). A special
3674 * case is needed for cores generated by some older Linux
3675 * versions, which write a note named "CORE" without a nul
3676 * terminator and n_namesz = 4.
3678 if (note->n_namesz == 0)
3680 else if (note->n_namesz == 4 && strncmp(name, "CORE", 4) == 0)
3682 else if (strnlen(name, note->n_namesz) >= note->n_namesz)
3684 printf(" %-13s %#010jx", name, (uintmax_t) note->n_descsz);
3685 printf(" %s\n", note_type(name, re->ehdr.e_type,
3687 dump_notes_data(name, note->n_type, buf, note->n_descsz);
3688 buf += roundup2(note->n_descsz, 4);
3693 * Symbol versioning sections are the same for 32bit and 64bit
3696 #define Elf_Verdef Elf32_Verdef
3697 #define Elf_Verdaux Elf32_Verdaux
3698 #define Elf_Verneed Elf32_Verneed
3699 #define Elf_Vernaux Elf32_Vernaux
3701 #define SAVE_VERSION_NAME(x, n, t) \
3703 while (x >= re->ver_sz) { \
3704 nv = realloc(re->ver, \
3705 sizeof(*re->ver) * re->ver_sz * 2); \
3707 warn("realloc failed"); \
3712 for (i = re->ver_sz; i < re->ver_sz * 2; i++) { \
3713 re->ver[i].name = NULL; \
3714 re->ver[i].type = 0; \
3719 re->ver[x].name = n; \
3720 re->ver[x].type = t; \
3726 dump_verdef(struct readelf *re, int dump)
3733 uint8_t *buf, *end, *buf2;
3737 if ((s = re->vd_s) == NULL)
3739 if (s->link >= re->shnum)
3742 if (re->ver == NULL) {
3744 if ((re->ver = calloc(re->ver_sz, sizeof(*re->ver))) ==
3746 warn("calloc failed");
3749 re->ver[0].name = "*local*";
3750 re->ver[1].name = "*global*";
3754 printf("\nVersion definition section (%s):\n", s->name);
3756 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3757 elferr = elf_errno();
3759 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
3766 end = buf + d->d_size;
3767 while (buf + sizeof(Elf_Verdef) <= end) {
3768 vd = (Elf_Verdef *) (uintptr_t) buf;
3770 printf(" 0x%4.4lx", (unsigned long)
3771 (buf - (uint8_t *)d->d_buf));
3772 printf(" vd_version: %u vd_flags: %d"
3773 " vd_ndx: %u vd_cnt: %u", vd->vd_version,
3774 vd->vd_flags, vd->vd_ndx, vd->vd_cnt);
3776 buf2 = buf + vd->vd_aux;
3778 while (buf2 + sizeof(Elf_Verdaux) <= end && j < vd->vd_cnt) {
3779 vda = (Elf_Verdaux *) (uintptr_t) buf2;
3780 name = get_string(re, s->link, vda->vda_name);
3783 printf(" vda_name: %s\n", name);
3784 SAVE_VERSION_NAME((int)vd->vd_ndx, name, 1);
3786 printf(" 0x%4.4lx parent: %s\n",
3787 (unsigned long) (buf2 -
3788 (uint8_t *)d->d_buf), name);
3789 if (vda->vda_next == 0)
3791 buf2 += vda->vda_next;
3794 if (vd->vd_next == 0)
3801 dump_verneed(struct readelf *re, int dump)
3808 uint8_t *buf, *end, *buf2;
3812 if ((s = re->vn_s) == NULL)
3814 if (s->link >= re->shnum)
3817 if (re->ver == NULL) {
3819 if ((re->ver = calloc(re->ver_sz, sizeof(*re->ver))) ==
3821 warn("calloc failed");
3824 re->ver[0].name = "*local*";
3825 re->ver[1].name = "*global*";
3829 printf("\nVersion needed section (%s):\n", s->name);
3831 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3832 elferr = elf_errno();
3834 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
3841 end = buf + d->d_size;
3842 while (buf + sizeof(Elf_Verneed) <= end) {
3843 vn = (Elf_Verneed *) (uintptr_t) buf;
3845 printf(" 0x%4.4lx", (unsigned long)
3846 (buf - (uint8_t *)d->d_buf));
3847 printf(" vn_version: %u vn_file: %s vn_cnt: %u\n",
3849 get_string(re, s->link, vn->vn_file),
3852 buf2 = buf + vn->vn_aux;
3854 while (buf2 + sizeof(Elf_Vernaux) <= end && j < vn->vn_cnt) {
3855 vna = (Elf32_Vernaux *) (uintptr_t) buf2;
3857 printf(" 0x%4.4lx", (unsigned long)
3858 (buf2 - (uint8_t *)d->d_buf));
3859 name = get_string(re, s->link, vna->vna_name);
3861 printf(" vna_name: %s vna_flags: %u"
3862 " vna_other: %u\n", name,
3863 vna->vna_flags, vna->vna_other);
3864 SAVE_VERSION_NAME((int)vna->vna_other, name, 0);
3865 if (vna->vna_next == 0)
3867 buf2 += vna->vna_next;
3870 if (vn->vn_next == 0)
3877 dump_versym(struct readelf *re)
3882 if (re->vs_s == NULL || re->ver == NULL || re->vs == NULL)
3884 printf("\nVersion symbol section (%s):\n", re->vs_s->name);
3885 for (i = 0; i < re->vs_sz; i++) {
3889 printf(" %03x:", i);
3891 vs = re->vs[i] & VERSYM_VERSION;
3892 if (vs >= re->ver_sz || re->ver[vs].name == NULL) {
3893 warnx("invalid versym version index %u", re->vs[i]);
3896 if (re->vs[i] & VERSYM_HIDDEN)
3897 printf(" %3xh %-12s ", vs,
3898 re->ver[re->vs[i] & VERSYM_VERSION].name);
3900 printf(" %3x %-12s ", vs, re->ver[re->vs[i]].name);
3906 dump_ver(struct readelf *re)
3909 if (re->vs_s && re->ver && re->vs)
3914 dump_verneed(re, 1);
3918 search_ver(struct readelf *re)
3924 for (i = 0; (size_t) i < re->shnum; i++) {
3926 if (s->type == SHT_SUNW_versym)
3928 if (s->type == SHT_SUNW_verneed)
3930 if (s->type == SHT_SUNW_verdef)
3936 dump_verneed(re, 0);
3937 if (re->vs_s && re->ver != NULL) {
3939 if ((d = elf_getdata(re->vs_s->scn, NULL)) == NULL) {
3940 elferr = elf_errno();
3942 warnx("elf_getdata failed: %s",
3943 elf_errmsg(elferr));
3949 re->vs_sz = d->d_size / sizeof(Elf32_Half);
3957 #undef SAVE_VERSION_NAME
3960 * Elf32_Lib and Elf64_Lib are identical.
3962 #define Elf_Lib Elf32_Lib
3965 dump_liblist(struct readelf *re)
3973 int i, j, k, elferr, first, len;
3975 for (i = 0; (size_t) i < re->shnum; i++) {
3977 if (s->type != SHT_GNU_LIBLIST)
3979 if (s->link >= re->shnum)
3982 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3983 elferr = elf_errno();
3985 warnx("elf_getdata failed: %s",
3986 elf_errmsg(elferr));
3992 if (!get_ent_count(s, &len))
3994 printf("\nLibrary list section '%s' ", s->name);
3995 printf("contains %d entries:\n", len);
3996 printf("%12s%24s%18s%10s%6s\n", "Library", "Time Stamp",
3997 "Checksum", "Version", "Flags");
3998 for (j = 0; (uint64_t) j < s->sz / s->entsize; j++) {
4001 get_string(re, s->link, lib->l_name));
4002 ti = lib->l_time_stamp;
4004 snprintf(tbuf, sizeof(tbuf), "%04d-%02d-%02dT%02d:%02d"
4005 ":%2d", t->tm_year + 1900, t->tm_mon + 1,
4006 t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
4007 printf("%-19.19s ", tbuf);
4008 printf("0x%08x ", lib->l_checksum);
4009 printf("%-7d %#x", lib->l_version, lib->l_flags);
4010 if (lib->l_flags != 0) {
4013 for (k = 0; l_flag[k].name != NULL; k++) {
4014 if ((l_flag[k].value & lib->l_flags) ==
4021 printf("%s", l_flag[k].name);
4034 dump_section_groups(struct readelf *re)
4037 const char *symname;
4043 for (i = 0; (size_t) i < re->shnum; i++) {
4045 if (s->type != SHT_GROUP)
4047 if (s->link >= re->shnum)
4050 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
4051 elferr = elf_errno();
4053 warnx("elf_getdata failed: %s",
4054 elf_errmsg(elferr));
4062 /* We only support COMDAT section. */
4064 #define GRP_COMDAT 0x1
4066 if ((*w++ & GRP_COMDAT) == 0)
4069 if (s->entsize == 0)
4072 symname = get_symbol_name(re, s->link, s->info);
4073 n = s->sz / s->entsize;
4077 printf("\nCOMDAT group section [%5d] `%s' [%s] contains %ju"
4078 " sections:\n", i, s->name, symname, (uintmax_t)n);
4079 printf(" %-10.10s %s\n", "[Index]", "Name");
4080 for (j = 0; (size_t) j < n; j++, w++) {
4081 if (*w >= re->shnum) {
4082 warnx("invalid section index: %u", *w);
4085 printf(" [%5u] %s\n", *w, re->sl[*w].name);
4091 dump_unknown_tag(uint64_t tag, uint8_t *p, uint8_t *pe)
4096 * According to ARM EABI: For tags > 32, even numbered tags have
4097 * a ULEB128 param and odd numbered ones have NUL-terminated
4098 * string param. This rule probably also applies for tags <= 32
4099 * if the object arch is not ARM.
4102 printf(" Tag_unknown_%ju: ", (uintmax_t) tag);
4105 printf("%s\n", (char *) p);
4106 p += strlen((char *) p) + 1;
4108 val = _decode_uleb128(&p, pe);
4109 printf("%ju\n", (uintmax_t) val);
4116 dump_compatibility_tag(uint8_t *p, uint8_t *pe)
4120 val = _decode_uleb128(&p, pe);
4121 printf("flag = %ju, vendor = %s\n", (uintmax_t) val, p);
4122 p += strlen((char *) p) + 1;
4128 dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe)
4137 tag = _decode_uleb128(&p, pe);
4139 for (i = 0; i < sizeof(aeabi_tags) / sizeof(aeabi_tags[0]);
4141 if (tag == aeabi_tags[i].tag) {
4143 printf(" %s: ", aeabi_tags[i].s_tag);
4144 if (aeabi_tags[i].get_desc) {
4146 val = _decode_uleb128(&p, pe);
4148 aeabi_tags[i].get_desc(val));
4152 if (tag < aeabi_tags[i].tag)
4156 p = dump_unknown_tag(tag, p, pe);
4163 case 4: /* Tag_CPU_raw_name */
4164 case 5: /* Tag_CPU_name */
4165 case 67: /* Tag_conformance */
4166 printf("%s\n", (char *) p);
4167 p += strlen((char *) p) + 1;
4169 case 32: /* Tag_compatibility */
4170 p = dump_compatibility_tag(p, pe);
4172 case 64: /* Tag_nodefaults */
4173 /* ignored, written as 0. */
4174 (void) _decode_uleb128(&p, pe);
4177 case 65: /* Tag_also_compatible_with */
4178 val = _decode_uleb128(&p, pe);
4179 /* Must be Tag_CPU_arch */
4181 printf("unknown\n");
4184 val = _decode_uleb128(&p, pe);
4185 printf("%s\n", aeabi_cpu_arch(val));
4186 /* Skip NUL terminator. */
4196 #ifndef Tag_GNU_MIPS_ABI_FP
4197 #define Tag_GNU_MIPS_ABI_FP 4
4201 dump_mips_attributes(struct readelf *re, uint8_t *p, uint8_t *pe)
4208 tag = _decode_uleb128(&p, pe);
4210 case Tag_GNU_MIPS_ABI_FP:
4211 val = _decode_uleb128(&p, pe);
4212 printf(" Tag_GNU_MIPS_ABI_FP: %s\n", mips_abi_fp(val));
4214 case 32: /* Tag_compatibility */
4215 p = dump_compatibility_tag(p, pe);
4218 p = dump_unknown_tag(tag, p, pe);
4224 #ifndef Tag_GNU_Power_ABI_FP
4225 #define Tag_GNU_Power_ABI_FP 4
4228 #ifndef Tag_GNU_Power_ABI_Vector
4229 #define Tag_GNU_Power_ABI_Vector 8
4233 dump_ppc_attributes(uint8_t *p, uint8_t *pe)
4238 tag = _decode_uleb128(&p, pe);
4240 case Tag_GNU_Power_ABI_FP:
4241 val = _decode_uleb128(&p, pe);
4242 printf(" Tag_GNU_Power_ABI_FP: %s\n", ppc_abi_fp(val));
4244 case Tag_GNU_Power_ABI_Vector:
4245 val = _decode_uleb128(&p, pe);
4246 printf(" Tag_GNU_Power_ABI_Vector: %s\n",
4247 ppc_abi_vector(val));
4249 case 32: /* Tag_compatibility */
4250 p = dump_compatibility_tag(p, pe);
4253 p = dump_unknown_tag(tag, p, pe);
4260 dump_attributes(struct readelf *re)
4264 uint8_t *p, *pe, *sp;
4265 size_t len, seclen, nlen, sublen;
4269 for (i = 0; (size_t) i < re->shnum; i++) {
4271 if (s->type != SHT_GNU_ATTRIBUTES &&
4272 (re->ehdr.e_machine != EM_ARM || s->type != SHT_LOPROC + 3))
4275 if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
4276 elferr = elf_errno();
4278 warnx("elf_rawdata failed: %s",
4279 elf_errmsg(elferr));
4287 printf("Unknown Attribute Section Format: %c\n",
4291 len = d->d_size - 1;
4295 warnx("truncated attribute section length");
4298 seclen = re->dw_decode(&p, 4);
4300 warnx("invalid attribute section length");
4304 nlen = strlen((char *) p) + 1;
4305 if (nlen + 4 > seclen) {
4306 warnx("invalid attribute section name");
4309 printf("Attribute Section: %s\n", (char *) p);
4312 while (seclen > 0) {
4315 sublen = re->dw_decode(&p, 4);
4316 if (sublen > seclen) {
4317 warnx("invalid attribute sub-section"
4322 printf("%s", top_tag(tag));
4323 if (tag == 2 || tag == 3) {
4326 val = _decode_uleb128(&p, pe);
4329 printf(" %ju", (uintmax_t) val);
4333 if (re->ehdr.e_machine == EM_ARM &&
4334 s->type == SHT_LOPROC + 3)
4335 dump_arm_attributes(re, p, sp + sublen);
4336 else if (re->ehdr.e_machine == EM_MIPS ||
4337 re->ehdr.e_machine == EM_MIPS_RS3_LE)
4338 dump_mips_attributes(re, p,
4340 else if (re->ehdr.e_machine == EM_PPC)
4341 dump_ppc_attributes(p, sp + sublen);
4349 dump_mips_specific_info(struct readelf *re)
4355 for (i = 0; (size_t) i < re->shnum; i++) {
4357 if (s->name != NULL && (!strcmp(s->name, ".MIPS.options") ||
4358 (s->type == SHT_MIPS_OPTIONS))) {
4359 dump_mips_options(re, s);
4363 if (s->name != NULL && (!strcmp(s->name, ".MIPS.abiflags") ||
4364 (s->type == SHT_MIPS_ABIFLAGS)))
4365 dump_mips_abiflags(re, s);
4368 * Dump .reginfo if present (although it will be ignored by an OS if a
4369 * .MIPS.options section is present, according to SGI mips64 spec).
4371 for (i = 0; (size_t) i < re->shnum; i++) {
4373 if (s->name != NULL && (!strcmp(s->name, ".reginfo") ||
4374 (s->type == SHT_MIPS_REGINFO)))
4375 dump_mips_reginfo(re, s);
4380 dump_mips_abiflags(struct readelf *re, struct section *s)
4385 uint32_t isa_ext, ases, flags1, flags2;
4387 uint8_t isa_level, isa_rev, gpr_size, cpr1_size, cpr2_size, fp_abi;
4389 if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
4390 elferr = elf_errno();
4392 warnx("elf_rawdata failed: %s",
4393 elf_errmsg(elferr));
4396 if (d->d_size != 24) {
4397 warnx("invalid MIPS abiflags section size");
4402 version = re->dw_decode(&p, 2);
4403 printf("MIPS ABI Flags Version: %u", version);
4405 printf(" (unknown)\n\n");
4410 isa_level = re->dw_decode(&p, 1);
4411 isa_rev = re->dw_decode(&p, 1);
4412 gpr_size = re->dw_decode(&p, 1);
4413 cpr1_size = re->dw_decode(&p, 1);
4414 cpr2_size = re->dw_decode(&p, 1);
4415 fp_abi = re->dw_decode(&p, 1);
4416 isa_ext = re->dw_decode(&p, 4);
4417 ases = re->dw_decode(&p, 4);
4418 flags1 = re->dw_decode(&p, 4);
4419 flags2 = re->dw_decode(&p, 4);
4423 printf("MIPS%u\n", isa_level);
4425 printf("MIPS%ur%u\n", isa_level, isa_rev);
4426 printf("GPR size: %d\n", get_mips_register_size(gpr_size));
4427 printf("CPR1 size: %d\n", get_mips_register_size(cpr1_size));
4428 printf("CPR2 size: %d\n", get_mips_register_size(cpr2_size));
4432 printf("Soft float");
4435 printf("%u", fp_abi);
4438 printf("\nISA Extension: %u\n", isa_ext);
4439 printf("ASEs: %u\n", ases);
4440 printf("FLAGS 1: %08x\n", flags1);
4441 printf("FLAGS 2: %08x\n", flags2);
4445 get_mips_register_size(uint8_t flag)
4456 dump_mips_reginfo(struct readelf *re, struct section *s)
4462 if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
4463 elferr = elf_errno();
4465 warnx("elf_rawdata failed: %s",
4466 elf_errmsg(elferr));
4471 if (!get_ent_count(s, &len))
4474 printf("\nSection '%s' contains %d entries:\n", s->name, len);
4475 dump_mips_odk_reginfo(re, d->d_buf, d->d_size);
4479 dump_mips_options(struct readelf *re, struct section *s)
4489 if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
4490 elferr = elf_errno();
4492 warnx("elf_rawdata failed: %s",
4493 elf_errmsg(elferr));
4499 printf("\nSection %s contains:\n", s->name);
4504 warnx("Truncated MIPS option header");
4507 kind = re->dw_decode(&p, 1);
4508 size = re->dw_decode(&p, 1);
4509 sndx = re->dw_decode(&p, 2);
4510 info = re->dw_decode(&p, 4);
4511 if (size < 8 || size - 8 > pe - p) {
4512 warnx("Malformed MIPS option header");
4518 dump_mips_odk_reginfo(re, p, size);
4520 case ODK_EXCEPTIONS:
4521 printf(" EXCEPTIONS FPU_MIN: %#x\n",
4522 info & OEX_FPU_MIN);
4523 printf("%11.11s FPU_MAX: %#x\n", "",
4524 info & OEX_FPU_MAX);
4525 dump_mips_option_flags("", mips_exceptions_option,
4529 printf(" %-10.10s section: %ju\n", "OPAD",
4531 dump_mips_option_flags("", mips_pad_option, info);
4534 dump_mips_option_flags("HWPATCH", mips_hwpatch_option,
4538 dump_mips_option_flags("HWAND", mips_hwa_option, info);
4541 dump_mips_option_flags("HWOR", mips_hwo_option, info);
4544 printf(" %-10.10s %#jx\n", "FILL", (uintmax_t) info);
4547 printf(" %-10.10s\n", "TAGS");
4550 printf(" %-10.10s GP group number: %#x\n", "GP_GROUP",
4553 printf(" %-10.10s GP group is "
4554 "self-contained\n", "");
4557 printf(" %-10.10s default GP group number: %#x\n",
4558 "IDENT", info & 0xFFFF);
4560 printf(" %-10.10s default GP group is "
4561 "self-contained\n", "");
4564 printf(" %-10.10s\n", "PAGESIZE");
4574 dump_mips_option_flags(const char *name, struct mips_option *opt, uint64_t info)
4579 for (; opt->desc != NULL; opt++) {
4580 if (info & opt->flag) {
4581 printf(" %-10.10s %s\n", first ? name : "",
4589 dump_mips_odk_reginfo(struct readelf *re, uint8_t *p, size_t sz)
4591 uint32_t ri_gprmask;
4592 uint32_t ri_cprmask[4];
4593 uint64_t ri_gp_value;
4599 ri_gprmask = re->dw_decode(&p, 4);
4600 /* Skip ri_pad padding field for mips64. */
4601 if (re->ec == ELFCLASS64)
4602 re->dw_decode(&p, 4);
4603 for (i = 0; i < 4; i++)
4604 ri_cprmask[i] = re->dw_decode(&p, 4);
4605 if (re->ec == ELFCLASS32)
4606 ri_gp_value = re->dw_decode(&p, 4);
4608 ri_gp_value = re->dw_decode(&p, 8);
4609 printf(" %s ", option_kind(ODK_REGINFO));
4610 printf("ri_gprmask: 0x%08jx\n", (uintmax_t) ri_gprmask);
4611 for (i = 0; i < 4; i++)
4612 printf("%11.11s ri_cprmask[%d]: 0x%08jx\n", "", i,
4613 (uintmax_t) ri_cprmask[i]);
4614 printf("%12.12s", "");
4615 printf("ri_gp_value: %#jx\n", (uintmax_t) ri_gp_value);
4620 dump_arch_specific_info(struct readelf *re)
4624 dump_attributes(re);
4626 switch (re->ehdr.e_machine) {
4628 case EM_MIPS_RS3_LE:
4629 dump_mips_specific_info(re);
4636 dwarf_regname(struct readelf *re, unsigned int num)
4641 if ((rn = dwarf_reg(re->ehdr.e_machine, num)) != NULL)
4644 snprintf(rx, sizeof(rx), "r%u", num);
4650 dump_dwarf_line(struct readelf *re)
4655 Dwarf_Half tag, version, pointer_size;
4656 Dwarf_Unsigned offset, endoff, length, hdrlen, dirndx, mtime, fsize;
4657 Dwarf_Small minlen, defstmt, lrange, opbase, oplen;
4660 uint64_t address, file, line, column, isa, opsize, udelta;
4664 int i, is_stmt, dwarf_size, elferr, ret;
4666 printf("\nDump of debug contents of section .debug_line:\n");
4669 for (i = 0; (size_t) i < re->shnum; i++) {
4671 if (s->name != NULL && !strcmp(s->name, ".debug_line"))
4674 if ((size_t) i >= re->shnum)
4678 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
4679 elferr = elf_errno();
4681 warnx("elf_getdata failed: %s", elf_errmsg(-1));
4687 while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
4688 NULL, &de)) == DW_DLV_OK) {
4690 while (dwarf_siblingof(re->dbg, die, &die, &de) == DW_DLV_OK) {
4691 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
4692 warnx("dwarf_tag failed: %s",
4696 /* XXX: What about DW_TAG_partial_unit? */
4697 if (tag == DW_TAG_compile_unit)
4701 warnx("could not find DW_TAG_compile_unit die");
4704 if (dwarf_attrval_unsigned(die, DW_AT_stmt_list, &offset,
4708 length = re->dw_read(d, &offset, 4);
4709 if (length == 0xffffffff) {
4711 length = re->dw_read(d, &offset, 8);
4715 if (length > d->d_size - offset) {
4716 warnx("invalid .dwarf_line section");
4720 endoff = offset + length;
4721 pe = (uint8_t *) d->d_buf + endoff;
4722 version = re->dw_read(d, &offset, 2);
4723 hdrlen = re->dw_read(d, &offset, dwarf_size);
4724 minlen = re->dw_read(d, &offset, 1);
4725 defstmt = re->dw_read(d, &offset, 1);
4726 lbase = re->dw_read(d, &offset, 1);
4727 lrange = re->dw_read(d, &offset, 1);
4728 opbase = re->dw_read(d, &offset, 1);
4731 printf(" Length:\t\t\t%ju\n", (uintmax_t) length);
4732 printf(" DWARF version:\t\t%u\n", version);
4733 printf(" Prologue Length:\t\t%ju\n", (uintmax_t) hdrlen);
4734 printf(" Minimum Instruction Length:\t%u\n", minlen);
4735 printf(" Initial value of 'is_stmt':\t%u\n", defstmt);
4736 printf(" Line Base:\t\t\t%d\n", lbase);
4737 printf(" Line Range:\t\t\t%u\n", lrange);
4738 printf(" Opcode Base:\t\t\t%u\n", opbase);
4739 (void) dwarf_get_address_size(re->dbg, &pointer_size, &de);
4740 printf(" (Pointer size:\t\t%u)\n", pointer_size);
4743 printf(" Opcodes:\n");
4744 for (i = 1; i < opbase; i++) {
4745 oplen = re->dw_read(d, &offset, 1);
4746 printf(" Opcode %d has %u args\n", i, oplen);
4750 printf(" The Directory Table:\n");
4751 p = (uint8_t *) d->d_buf + offset;
4752 while (*p != '\0') {
4753 printf(" %s\n", (char *) p);
4754 p += strlen((char *) p) + 1;
4759 printf(" The File Name Table:\n");
4760 printf(" Entry\tDir\tTime\tSize\tName\n");
4762 while (*p != '\0') {
4765 p += strlen(pn) + 1;
4766 dirndx = _decode_uleb128(&p, pe);
4767 mtime = _decode_uleb128(&p, pe);
4768 fsize = _decode_uleb128(&p, pe);
4769 printf(" %d\t%ju\t%ju\t%ju\t%s\n", i,
4770 (uintmax_t) dirndx, (uintmax_t) mtime,
4771 (uintmax_t) fsize, pn);
4774 #define RESET_REGISTERS \
4780 is_stmt = defstmt; \
4783 #define LINE(x) (lbase + (((x) - opbase) % lrange))
4784 #define ADDRESS(x) ((((x) - opbase) / lrange) * minlen)
4788 printf(" Line Number Statements:\n");
4799 opsize = _decode_uleb128(&p, pe);
4800 printf(" Extended opcode %u: ", *p);
4802 case DW_LNE_end_sequence:
4805 printf("End of Sequence\n");
4807 case DW_LNE_set_address:
4809 address = re->dw_decode(&p,
4811 printf("set Address to %#jx\n",
4812 (uintmax_t) address);
4814 case DW_LNE_define_file:
4817 p += strlen(pn) + 1;
4818 dirndx = _decode_uleb128(&p, pe);
4819 mtime = _decode_uleb128(&p, pe);
4820 fsize = _decode_uleb128(&p, pe);
4821 printf("define new file: %s\n", pn);
4824 /* Unrecognized extened opcodes. */
4826 printf("unknown opcode\n");
4828 } else if (*p > 0 && *p < opbase) {
4836 case DW_LNS_advance_pc:
4837 udelta = _decode_uleb128(&p, pe) *
4840 printf(" Advance PC by %ju to %#jx\n",
4842 (uintmax_t) address);
4844 case DW_LNS_advance_line:
4845 sdelta = _decode_sleb128(&p, pe);
4847 printf(" Advance Line by %jd to %ju\n",
4851 case DW_LNS_set_file:
4852 file = _decode_uleb128(&p, pe);
4853 printf(" Set File to %ju\n",
4856 case DW_LNS_set_column:
4857 column = _decode_uleb128(&p, pe);
4858 printf(" Set Column to %ju\n",
4859 (uintmax_t) column);
4861 case DW_LNS_negate_stmt:
4863 printf(" Set is_stmt to %d\n", is_stmt);
4865 case DW_LNS_set_basic_block:
4866 printf(" Set basic block flag\n");
4868 case DW_LNS_const_add_pc:
4869 address += ADDRESS(255);
4870 printf(" Advance PC by constant %ju"
4872 (uintmax_t) ADDRESS(255),
4873 (uintmax_t) address);
4875 case DW_LNS_fixed_advance_pc:
4876 udelta = re->dw_decode(&p, 2);
4878 printf(" Advance PC by fixed value "
4881 (uintmax_t) address);
4883 case DW_LNS_set_prologue_end:
4884 printf(" Set prologue end flag\n");
4886 case DW_LNS_set_epilogue_begin:
4887 printf(" Set epilogue begin flag\n");
4889 case DW_LNS_set_isa:
4890 isa = _decode_uleb128(&p, pe);
4891 printf(" Set isa to %ju\n",
4895 /* Unrecognized extended opcodes. */
4896 printf(" Unknown extended opcode %u\n",
4906 address += ADDRESS(*p);
4907 printf(" Special opcode %u: advance Address "
4908 "by %ju to %#jx and Line by %jd to %ju\n",
4909 *p - opbase, (uintmax_t) ADDRESS(*p),
4910 (uintmax_t) address, (intmax_t) LINE(*p),
4918 if (ret == DW_DLV_ERROR)
4919 warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
4921 #undef RESET_REGISTERS
4927 dump_dwarf_line_decoded(struct readelf *re)
4930 Dwarf_Line *linebuf, ln;
4931 Dwarf_Addr lineaddr;
4932 Dwarf_Signed linecount, srccount;
4933 Dwarf_Unsigned lineno, fn;
4935 const char *dir, *file;
4939 printf("Decoded dump of debug contents of section .debug_line:\n\n");
4940 while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
4941 NULL, &de)) == DW_DLV_OK) {
4942 if (dwarf_siblingof(re->dbg, NULL, &die, &de) != DW_DLV_OK)
4944 if (dwarf_attrval_string(die, DW_AT_name, &file, &de) !=
4947 if (dwarf_attrval_string(die, DW_AT_comp_dir, &dir, &de) !=
4951 if (dir && file && file[0] != '/')
4956 printf("%-37s %11s %s\n", "Filename", "Line Number",
4957 "Starting Address");
4958 if (dwarf_srclines(die, &linebuf, &linecount, &de) != DW_DLV_OK)
4960 if (dwarf_srcfiles(die, &srcfiles, &srccount, &de) != DW_DLV_OK)
4962 for (i = 0; i < linecount; i++) {
4964 if (dwarf_line_srcfileno(ln, &fn, &de) != DW_DLV_OK)
4966 if (dwarf_lineno(ln, &lineno, &de) != DW_DLV_OK)
4968 if (dwarf_lineaddr(ln, &lineaddr, &de) != DW_DLV_OK)
4970 printf("%-37s %11ju %#18jx\n",
4971 basename(srcfiles[fn - 1]), (uintmax_t) lineno,
4972 (uintmax_t) lineaddr);
4979 dump_dwarf_die(struct readelf *re, Dwarf_Die die, int level)
4981 Dwarf_Attribute *attr_list;
4983 Dwarf_Off dieoff, cuoff, culen, attroff;
4984 Dwarf_Unsigned ate, lang, v_udata, v_sig;
4985 Dwarf_Signed attr_count, v_sdata;
4988 Dwarf_Half tag, attr, form;
4989 Dwarf_Block *v_block;
4990 Dwarf_Bool v_bool, is_info;
4994 const char *tag_str, *attr_str, *ate_str, *lang_str;
4995 char unk_tag[32], unk_attr[32];
5000 if (dwarf_dieoffset(die, &dieoff, &de) != DW_DLV_OK) {
5001 warnx("dwarf_dieoffset failed: %s", dwarf_errmsg(de));
5005 printf(" <%d><%jx>: ", level, (uintmax_t) dieoff);
5007 if (dwarf_die_CU_offset_range(die, &cuoff, &culen, &de) != DW_DLV_OK) {
5008 warnx("dwarf_die_CU_offset_range failed: %s",
5013 abc = dwarf_die_abbrev_code(die);
5014 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
5015 warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
5018 if (dwarf_get_TAG_name(tag, &tag_str) != DW_DLV_OK) {
5019 snprintf(unk_tag, sizeof(unk_tag), "[Unknown Tag: %#x]", tag);
5023 printf("Abbrev Number: %d (%s)\n", abc, tag_str);
5025 if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
5027 if (ret == DW_DLV_ERROR)
5028 warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
5032 for (i = 0; i < attr_count; i++) {
5033 if (dwarf_whatform(attr_list[i], &form, &de) != DW_DLV_OK) {
5034 warnx("dwarf_whatform failed: %s", dwarf_errmsg(de));
5037 if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
5038 warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
5041 if (dwarf_get_AT_name(attr, &attr_str) != DW_DLV_OK) {
5042 snprintf(unk_attr, sizeof(unk_attr),
5043 "[Unknown AT: %#x]", attr);
5044 attr_str = unk_attr;
5046 if (dwarf_attroffset(attr_list[i], &attroff, &de) !=
5048 warnx("dwarf_attroffset failed: %s", dwarf_errmsg(de));
5051 printf(" <%jx> %-18s: ", (uintmax_t) attroff, attr_str);
5053 case DW_FORM_ref_addr:
5054 case DW_FORM_sec_offset:
5055 if (dwarf_global_formref(attr_list[i], &v_off, &de) !=
5057 warnx("dwarf_global_formref failed: %s",
5061 if (form == DW_FORM_ref_addr)
5062 printf("<0x%jx>", (uintmax_t) v_off);
5064 printf("0x%jx", (uintmax_t) v_off);
5071 case DW_FORM_ref_udata:
5072 if (dwarf_formref(attr_list[i], &v_off, &de) !=
5074 warnx("dwarf_formref failed: %s",
5079 printf("<0x%jx>", (uintmax_t) v_off);
5083 if (dwarf_formaddr(attr_list[i], &v_addr, &de) !=
5085 warnx("dwarf_formaddr failed: %s",
5089 printf("%#jx", (uintmax_t) v_addr);
5097 if (dwarf_formudata(attr_list[i], &v_udata, &de) !=
5099 warnx("dwarf_formudata failed: %s",
5103 if (attr == DW_AT_high_pc)
5104 printf("0x%jx", (uintmax_t) v_udata);
5106 printf("%ju", (uintmax_t) v_udata);
5110 if (dwarf_formsdata(attr_list[i], &v_sdata, &de) !=
5112 warnx("dwarf_formudata failed: %s",
5116 printf("%jd", (intmax_t) v_sdata);
5120 if (dwarf_formflag(attr_list[i], &v_bool, &de) !=
5122 warnx("dwarf_formflag failed: %s",
5126 printf("%jd", (intmax_t) v_bool);
5129 case DW_FORM_flag_present:
5133 case DW_FORM_string:
5135 if (dwarf_formstring(attr_list[i], &v_str, &de) !=
5137 warnx("dwarf_formstring failed: %s",
5141 if (form == DW_FORM_string)
5142 printf("%s", v_str);
5144 printf("(indirect string) %s", v_str);
5148 case DW_FORM_block1:
5149 case DW_FORM_block2:
5150 case DW_FORM_block4:
5151 if (dwarf_formblock(attr_list[i], &v_block, &de) !=
5153 warnx("dwarf_formblock failed: %s",
5157 printf("%ju byte block:", (uintmax_t) v_block->bl_len);
5158 b = v_block->bl_data;
5159 for (j = 0; (Dwarf_Unsigned) j < v_block->bl_len; j++)
5160 printf(" %x", b[j]);
5162 dump_dwarf_block(re, v_block->bl_data, v_block->bl_len);
5166 case DW_FORM_exprloc:
5167 if (dwarf_formexprloc(attr_list[i], &v_udata, &v_expr,
5168 &de) != DW_DLV_OK) {
5169 warnx("dwarf_formexprloc failed: %s",
5173 printf("%ju byte block:", (uintmax_t) v_udata);
5175 for (j = 0; (Dwarf_Unsigned) j < v_udata; j++)
5176 printf(" %x", b[j]);
5178 dump_dwarf_block(re, v_expr, v_udata);
5182 case DW_FORM_ref_sig8:
5183 if (dwarf_formsig8(attr_list[i], &v_sig8, &de) !=
5185 warnx("dwarf_formsig8 failed: %s",
5189 p = (uint8_t *)(uintptr_t) &v_sig8.signature[0];
5190 v_sig = re->dw_decode(&p, 8);
5191 printf("signature: 0x%jx", (uintmax_t) v_sig);
5194 case DW_AT_encoding:
5195 if (dwarf_attrval_unsigned(die, attr, &ate, &de) !=
5198 if (dwarf_get_ATE_name(ate, &ate_str) != DW_DLV_OK)
5199 ate_str = "DW_ATE_UNKNOWN";
5200 printf("\t(%s)", &ate_str[strlen("DW_ATE_")]);
5203 case DW_AT_language:
5204 if (dwarf_attrval_unsigned(die, attr, &lang, &de) !=
5207 if (dwarf_get_LANG_name(lang, &lang_str) != DW_DLV_OK)
5209 printf("\t(%s)", &lang_str[strlen("DW_LANG_")]);
5212 case DW_AT_location:
5213 case DW_AT_string_length:
5214 case DW_AT_return_addr:
5215 case DW_AT_data_member_location:
5216 case DW_AT_frame_base:
5218 case DW_AT_static_link:
5219 case DW_AT_use_location:
5220 case DW_AT_vtable_elem_location:
5224 case DW_FORM_sec_offset:
5225 printf("\t(location list)");
5239 /* Search children. */
5240 ret = dwarf_child(die, &ret_die, &de);
5241 if (ret == DW_DLV_ERROR)
5242 warnx("dwarf_child: %s", dwarf_errmsg(de));
5243 else if (ret == DW_DLV_OK)
5244 dump_dwarf_die(re, ret_die, level + 1);
5246 /* Search sibling. */
5247 is_info = dwarf_get_die_infotypes_flag(die);
5248 ret = dwarf_siblingof_b(re->dbg, die, &ret_die, is_info, &de);
5249 if (ret == DW_DLV_ERROR)
5250 warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
5251 else if (ret == DW_DLV_OK)
5252 dump_dwarf_die(re, ret_die, level);
5254 dwarf_dealloc(re->dbg, die, DW_DLA_DIE);
5258 set_cu_context(struct readelf *re, Dwarf_Half psize, Dwarf_Half osize,
5262 re->cu_psize = psize;
5263 re->cu_osize = osize;
5268 dump_dwarf_info(struct readelf *re, Dwarf_Bool is_info)
5273 Dwarf_Half tag, version, pointer_size, off_size;
5274 Dwarf_Off cu_offset, cu_length;
5276 Dwarf_Unsigned typeoff;
5283 sn = is_info ? ".debug_info" : ".debug_types";
5286 for (i = 0; (size_t) i < re->shnum; i++) {
5288 if (s->name != NULL && !strcmp(s->name, sn))
5291 if ((size_t) i >= re->shnum)
5295 printf("\nDump of debug contents of section %s:\n", sn);
5297 while ((ret = dwarf_next_cu_header_c(re->dbg, is_info, NULL,
5298 &version, &aboff, &pointer_size, &off_size, NULL, &sig8,
5299 &typeoff, NULL, &de)) == DW_DLV_OK) {
5300 set_cu_context(re, pointer_size, off_size, version);
5302 while (dwarf_siblingof_b(re->dbg, die, &die, is_info,
5303 &de) == DW_DLV_OK) {
5304 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
5305 warnx("dwarf_tag failed: %s",
5309 /* XXX: What about DW_TAG_partial_unit? */
5310 if ((is_info && tag == DW_TAG_compile_unit) ||
5311 (!is_info && tag == DW_TAG_type_unit))
5314 if (die == NULL && is_info) {
5315 warnx("could not find DW_TAG_compile_unit "
5318 } else if (die == NULL && !is_info) {
5319 warnx("could not find DW_TAG_type_unit die");
5323 if (dwarf_die_CU_offset_range(die, &cu_offset,
5324 &cu_length, &de) != DW_DLV_OK) {
5325 warnx("dwarf_die_CU_offset failed: %s",
5330 cu_length -= off_size == 4 ? 4 : 12;
5334 p = (uint8_t *)(uintptr_t) &sig8.signature[0];
5335 sig = re->dw_decode(&p, 8);
5338 printf("\n Type Unit @ offset 0x%jx:\n",
5339 (uintmax_t) cu_offset);
5340 printf(" Length:\t\t%#jx (%d-bit)\n",
5341 (uintmax_t) cu_length, off_size == 4 ? 32 : 64);
5342 printf(" Version:\t\t%u\n", version);
5343 printf(" Abbrev Offset:\t0x%jx\n",
5345 printf(" Pointer Size:\t%u\n", pointer_size);
5347 printf(" Signature:\t\t0x%016jx\n",
5349 printf(" Type Offset:\t0x%jx\n",
5350 (uintmax_t) typeoff);
5353 dump_dwarf_die(re, die, 0);
5355 if (ret == DW_DLV_ERROR)
5356 warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
5359 } while (dwarf_next_types_section(re->dbg, &de) == DW_DLV_OK);
5363 dump_dwarf_abbrev(struct readelf *re)
5366 Dwarf_Off aboff, atoff;
5367 Dwarf_Unsigned length, attr_count;
5368 Dwarf_Signed flag, form;
5369 Dwarf_Half tag, attr;
5371 const char *tag_str, *attr_str, *form_str;
5372 char unk_tag[32], unk_attr[32], unk_form[32];
5375 printf("\nContents of section .debug_abbrev:\n\n");
5377 while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, &aboff,
5378 NULL, NULL, &de)) == DW_DLV_OK) {
5379 printf(" Number TAG\n");
5381 while ((ret = dwarf_get_abbrev(re->dbg, aboff, &ab, &length,
5382 &attr_count, &de)) == DW_DLV_OK) {
5384 dwarf_dealloc(re->dbg, ab, DW_DLA_ABBREV);
5389 if (dwarf_get_abbrev_tag(ab, &tag, &de) != DW_DLV_OK) {
5390 warnx("dwarf_get_abbrev_tag failed: %s",
5394 if (dwarf_get_TAG_name(tag, &tag_str) != DW_DLV_OK) {
5395 snprintf(unk_tag, sizeof(unk_tag),
5396 "[Unknown Tag: %#x]", tag);
5399 if (dwarf_get_abbrev_children_flag(ab, &flag, &de) !=
5401 warnx("dwarf_get_abbrev_children_flag failed:"
5402 " %s", dwarf_errmsg(de));
5405 printf(" %s %s\n", tag_str,
5406 flag ? "[has children]" : "[no children]");
5407 for (j = 0; (Dwarf_Unsigned) j < attr_count; j++) {
5408 if (dwarf_get_abbrev_entry(ab, (Dwarf_Signed) j,
5409 &attr, &form, &atoff, &de) != DW_DLV_OK) {
5410 warnx("dwarf_get_abbrev_entry failed:"
5411 " %s", dwarf_errmsg(de));
5414 if (dwarf_get_AT_name(attr, &attr_str) !=
5416 snprintf(unk_attr, sizeof(unk_attr),
5417 "[Unknown AT: %#x]", attr);
5418 attr_str = unk_attr;
5420 if (dwarf_get_FORM_name(form, &form_str) !=
5422 snprintf(unk_form, sizeof(unk_form),
5423 "[Unknown Form: %#x]",
5425 form_str = unk_form;
5427 printf(" %-18s %s\n", attr_str, form_str);
5430 dwarf_dealloc(re->dbg, ab, DW_DLA_ABBREV);
5432 if (ret != DW_DLV_OK)
5433 warnx("dwarf_get_abbrev: %s", dwarf_errmsg(de));
5435 if (ret == DW_DLV_ERROR)
5436 warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
5440 dump_dwarf_pubnames(struct readelf *re)
5444 Dwarf_Unsigned offset, length, nt_cu_offset, nt_cu_length;
5446 Dwarf_Global *globs;
5447 Dwarf_Half nt_version;
5451 int i, dwarf_size, elferr;
5453 printf("\nContents of the .debug_pubnames section:\n");
5456 for (i = 0; (size_t) i < re->shnum; i++) {
5458 if (s->name != NULL && !strcmp(s->name, ".debug_pubnames"))
5461 if ((size_t) i >= re->shnum)
5465 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
5466 elferr = elf_errno();
5468 warnx("elf_getdata failed: %s", elf_errmsg(-1));
5474 /* Read in .debug_pubnames section table header. */
5476 length = re->dw_read(d, &offset, 4);
5477 if (length == 0xffffffff) {
5479 length = re->dw_read(d, &offset, 8);
5483 if (length > d->d_size - offset) {
5484 warnx("invalid .dwarf_pubnames section");
5488 nt_version = re->dw_read(d, &offset, 2);
5489 nt_cu_offset = re->dw_read(d, &offset, dwarf_size);
5490 nt_cu_length = re->dw_read(d, &offset, dwarf_size);
5491 printf(" Length:\t\t\t\t%ju\n", (uintmax_t) length);
5492 printf(" Version:\t\t\t\t%u\n", nt_version);
5493 printf(" Offset into .debug_info section:\t%ju\n",
5494 (uintmax_t) nt_cu_offset);
5495 printf(" Size of area in .debug_info section:\t%ju\n",
5496 (uintmax_t) nt_cu_length);
5498 if (dwarf_get_globals(re->dbg, &globs, &cnt, &de) != DW_DLV_OK) {
5499 warnx("dwarf_get_globals failed: %s", dwarf_errmsg(de));
5503 printf("\n Offset Name\n");
5504 for (i = 0; i < cnt; i++) {
5505 if (dwarf_globname(globs[i], &glob_name, &de) != DW_DLV_OK) {
5506 warnx("dwarf_globname failed: %s", dwarf_errmsg(de));
5509 if (dwarf_global_die_offset(globs[i], &die_off, &de) !=
5511 warnx("dwarf_global_die_offset failed: %s",
5515 printf(" %-11ju %s\n", (uintmax_t) die_off, glob_name);
5520 dump_dwarf_aranges(struct readelf *re)
5523 Dwarf_Arange *aranges;
5525 Dwarf_Unsigned offset, length, as_cu_offset;
5528 Dwarf_Half as_version, as_addrsz, as_segsz;
5531 int i, dwarf_size, elferr;
5533 printf("\nContents of section .debug_aranges:\n");
5536 for (i = 0; (size_t) i < re->shnum; i++) {
5538 if (s->name != NULL && !strcmp(s->name, ".debug_aranges"))
5541 if ((size_t) i >= re->shnum)
5545 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
5546 elferr = elf_errno();
5548 warnx("elf_getdata failed: %s", elf_errmsg(-1));
5554 /* Read in the .debug_aranges section table header. */
5556 length = re->dw_read(d, &offset, 4);
5557 if (length == 0xffffffff) {
5559 length = re->dw_read(d, &offset, 8);
5563 if (length > d->d_size - offset) {
5564 warnx("invalid .dwarf_aranges section");
5568 as_version = re->dw_read(d, &offset, 2);
5569 as_cu_offset = re->dw_read(d, &offset, dwarf_size);
5570 as_addrsz = re->dw_read(d, &offset, 1);
5571 as_segsz = re->dw_read(d, &offset, 1);
5573 printf(" Length:\t\t\t%ju\n", (uintmax_t) length);
5574 printf(" Version:\t\t\t%u\n", as_version);
5575 printf(" Offset into .debug_info:\t%ju\n", (uintmax_t) as_cu_offset);
5576 printf(" Pointer Size:\t\t\t%u\n", as_addrsz);
5577 printf(" Segment Size:\t\t\t%u\n", as_segsz);
5579 if (dwarf_get_aranges(re->dbg, &aranges, &cnt, &de) != DW_DLV_OK) {
5580 warnx("dwarf_get_aranges failed: %s", dwarf_errmsg(de));
5584 printf("\n Address Length\n");
5585 for (i = 0; i < cnt; i++) {
5586 if (dwarf_get_arange_info(aranges[i], &start, &length,
5587 &die_off, &de) != DW_DLV_OK) {
5588 warnx("dwarf_get_arange_info failed: %s",
5592 printf(" %08jx %ju\n", (uintmax_t) start,
5593 (uintmax_t) length);
5598 dump_dwarf_ranges_foreach(struct readelf *re, Dwarf_Die die, Dwarf_Addr base)
5600 Dwarf_Attribute *attr_list;
5601 Dwarf_Ranges *ranges;
5606 Dwarf_Signed attr_count, cnt;
5607 Dwarf_Unsigned off, bytecnt;
5610 if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
5612 if (ret == DW_DLV_ERROR)
5613 warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
5617 for (i = 0; i < attr_count; i++) {
5618 if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
5619 warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
5622 if (attr != DW_AT_ranges)
5624 if (dwarf_formudata(attr_list[i], &off, &de) != DW_DLV_OK) {
5625 warnx("dwarf_formudata failed: %s", dwarf_errmsg(de));
5628 if (dwarf_get_ranges(re->dbg, (Dwarf_Off) off, &ranges, &cnt,
5629 &bytecnt, &de) != DW_DLV_OK)
5632 for (j = 0; j < cnt; j++) {
5633 printf(" %08jx ", (uintmax_t) off);
5634 if (ranges[j].dwr_type == DW_RANGES_END) {
5635 printf("%s\n", "<End of list>");
5637 } else if (ranges[j].dwr_type ==
5638 DW_RANGES_ADDRESS_SELECTION) {
5639 base0 = ranges[j].dwr_addr2;
5642 if (re->ec == ELFCLASS32)
5643 printf("%08jx %08jx\n",
5644 (uintmax_t) (ranges[j].dwr_addr1 + base0),
5645 (uintmax_t) (ranges[j].dwr_addr2 + base0));
5647 printf("%016jx %016jx\n",
5648 (uintmax_t) (ranges[j].dwr_addr1 + base0),
5649 (uintmax_t) (ranges[j].dwr_addr2 + base0));
5654 /* Search children. */
5655 ret = dwarf_child(die, &ret_die, &de);
5656 if (ret == DW_DLV_ERROR)
5657 warnx("dwarf_child: %s", dwarf_errmsg(de));
5658 else if (ret == DW_DLV_OK)
5659 dump_dwarf_ranges_foreach(re, ret_die, base);
5661 /* Search sibling. */
5662 ret = dwarf_siblingof(re->dbg, die, &ret_die, &de);
5663 if (ret == DW_DLV_ERROR)
5664 warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
5665 else if (ret == DW_DLV_OK)
5666 dump_dwarf_ranges_foreach(re, ret_die, base);
5670 dump_dwarf_ranges(struct readelf *re)
5672 Dwarf_Ranges *ranges;
5675 Dwarf_Unsigned bytecnt;
5678 Dwarf_Unsigned lowpc;
5681 if (dwarf_get_ranges(re->dbg, 0, &ranges, &cnt, &bytecnt, &de) !=
5685 printf("Contents of the .debug_ranges section:\n\n");
5686 if (re->ec == ELFCLASS32)
5687 printf(" %-8s %-8s %s\n", "Offset", "Begin", "End");
5689 printf(" %-8s %-16s %s\n", "Offset", "Begin", "End");
5691 while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
5692 NULL, &de)) == DW_DLV_OK) {
5694 if (dwarf_siblingof(re->dbg, die, &die, &de) != DW_DLV_OK)
5696 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
5697 warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
5700 /* XXX: What about DW_TAG_partial_unit? */
5702 if (tag == DW_TAG_compile_unit) {
5703 if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lowpc,
5708 dump_dwarf_ranges_foreach(re, die, (Dwarf_Addr) lowpc);
5714 dump_dwarf_macinfo(struct readelf *re)
5716 Dwarf_Unsigned offset;
5718 Dwarf_Macro_Details *md;
5724 #define _MAX_MACINFO_ENTRY 65535
5726 printf("\nContents of section .debug_macinfo:\n\n");
5729 while (dwarf_get_macro_details(re->dbg, offset, _MAX_MACINFO_ENTRY,
5730 &cnt, &md, &de) == DW_DLV_OK) {
5731 for (i = 0; i < cnt; i++) {
5732 offset = md[i].dmd_offset + 1;
5733 if (md[i].dmd_type == 0)
5735 if (dwarf_get_MACINFO_name(md[i].dmd_type, &mi_str) !=
5737 snprintf(unk_mi, sizeof(unk_mi),
5738 "[Unknown MACINFO: %#x]", md[i].dmd_type);
5741 printf(" %s", mi_str);
5742 switch (md[i].dmd_type) {
5743 case DW_MACINFO_define:
5744 case DW_MACINFO_undef:
5745 printf(" - lineno : %jd macro : %s\n",
5746 (intmax_t) md[i].dmd_lineno,
5749 case DW_MACINFO_start_file:
5750 printf(" - lineno : %jd filenum : %jd\n",
5751 (intmax_t) md[i].dmd_lineno,
5752 (intmax_t) md[i].dmd_fileindex);
5761 #undef _MAX_MACINFO_ENTRY
5765 dump_dwarf_frame_inst(struct readelf *re, Dwarf_Cie cie, uint8_t *insts,
5766 Dwarf_Unsigned len, Dwarf_Unsigned caf, Dwarf_Signed daf, Dwarf_Addr pc,
5769 Dwarf_Frame_Op *oplist;
5770 Dwarf_Signed opcnt, delta;
5777 if (dwarf_expand_frame_instructions(cie, insts, len, &oplist,
5778 &opcnt, &de) != DW_DLV_OK) {
5779 warnx("dwarf_expand_frame_instructions failed: %s",
5784 for (i = 0; i < opcnt; i++) {
5785 if (oplist[i].fp_base_op != 0)
5786 op = oplist[i].fp_base_op << 6;
5788 op = oplist[i].fp_extended_op;
5789 if (dwarf_get_CFA_name(op, &op_str) != DW_DLV_OK) {
5790 snprintf(unk_op, sizeof(unk_op), "[Unknown CFA: %#x]",
5794 printf(" %s", op_str);
5796 case DW_CFA_advance_loc:
5797 delta = oplist[i].fp_offset * caf;
5799 printf(": %ju to %08jx", (uintmax_t) delta,
5803 case DW_CFA_offset_extended:
5804 case DW_CFA_offset_extended_sf:
5805 delta = oplist[i].fp_offset * daf;
5806 printf(": r%u (%s) at cfa%+jd", oplist[i].fp_register,
5807 dwarf_regname(re, oplist[i].fp_register),
5810 case DW_CFA_restore:
5811 printf(": r%u (%s)", oplist[i].fp_register,
5812 dwarf_regname(re, oplist[i].fp_register));
5814 case DW_CFA_set_loc:
5815 pc = oplist[i].fp_offset;
5816 printf(": to %08jx", (uintmax_t) pc);
5818 case DW_CFA_advance_loc1:
5819 case DW_CFA_advance_loc2:
5820 case DW_CFA_advance_loc4:
5821 pc += oplist[i].fp_offset;
5822 printf(": %jd to %08jx", (intmax_t) oplist[i].fp_offset,
5825 case DW_CFA_def_cfa:
5826 printf(": r%u (%s) ofs %ju", oplist[i].fp_register,
5827 dwarf_regname(re, oplist[i].fp_register),
5828 (uintmax_t) oplist[i].fp_offset);
5830 case DW_CFA_def_cfa_sf:
5831 printf(": r%u (%s) ofs %jd", oplist[i].fp_register,
5832 dwarf_regname(re, oplist[i].fp_register),
5833 (intmax_t) (oplist[i].fp_offset * daf));
5835 case DW_CFA_def_cfa_register:
5836 printf(": r%u (%s)", oplist[i].fp_register,
5837 dwarf_regname(re, oplist[i].fp_register));
5839 case DW_CFA_def_cfa_offset:
5840 printf(": %ju", (uintmax_t) oplist[i].fp_offset);
5842 case DW_CFA_def_cfa_offset_sf:
5843 printf(": %jd", (intmax_t) (oplist[i].fp_offset * daf));
5851 dwarf_dealloc(dbg, oplist, DW_DLA_FRAME_BLOCK);
5855 get_regoff_str(struct readelf *re, Dwarf_Half reg, Dwarf_Addr off)
5859 if (reg == DW_FRAME_UNDEFINED_VAL || reg == DW_FRAME_REG_INITIAL_VALUE)
5860 snprintf(rs, sizeof(rs), "%c", 'u');
5861 else if (reg == DW_FRAME_CFA_COL)
5862 snprintf(rs, sizeof(rs), "c%+jd", (intmax_t) off);
5864 snprintf(rs, sizeof(rs), "%s%+jd", dwarf_regname(re, reg),
5871 dump_dwarf_frame_regtable(struct readelf *re, Dwarf_Fde fde, Dwarf_Addr pc,
5872 Dwarf_Unsigned func_len, Dwarf_Half cie_ra)
5875 Dwarf_Addr row_pc, end_pc, pre_pc, cur_pc;
5880 #define BIT_SET(v, n) (v[(n)>>3] |= 1U << ((n) & 7))
5881 #define BIT_CLR(v, n) (v[(n)>>3] &= ~(1U << ((n) & 7)))
5882 #define BIT_ISSET(v, n) (v[(n)>>3] & (1U << ((n) & 7)))
5883 #define RT(x) rt.rules[(x)]
5885 vec = calloc((DW_REG_TABLE_SIZE + 7) / 8, 1);
5887 err(EXIT_FAILURE, "calloc failed");
5889 pre_pc = ~((Dwarf_Addr) 0);
5891 end_pc = pc + func_len;
5892 for (; cur_pc < end_pc; cur_pc++) {
5893 if (dwarf_get_fde_info_for_all_regs(fde, cur_pc, &rt, &row_pc,
5894 &de) != DW_DLV_OK) {
5895 warnx("dwarf_get_fde_info_for_all_regs failed: %s\n",
5899 if (row_pc == pre_pc)
5902 for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
5903 if (rt.rules[i].dw_regnum != DW_FRAME_REG_INITIAL_VALUE)
5908 printf(" LOC CFA ");
5909 for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
5910 if (BIT_ISSET(vec, i)) {
5911 if ((Dwarf_Half) i == cie_ra)
5915 dwarf_regname(re, (unsigned int) i));
5920 pre_pc = ~((Dwarf_Addr) 0);
5922 end_pc = pc + func_len;
5923 for (; cur_pc < end_pc; cur_pc++) {
5924 if (dwarf_get_fde_info_for_all_regs(fde, cur_pc, &rt, &row_pc,
5925 &de) != DW_DLV_OK) {
5926 warnx("dwarf_get_fde_info_for_all_regs failed: %s\n",
5930 if (row_pc == pre_pc)
5933 printf("%08jx ", (uintmax_t) row_pc);
5934 printf("%-8s ", get_regoff_str(re, RT(0).dw_regnum,
5936 for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
5937 if (BIT_ISSET(vec, i)) {
5938 printf("%-5s", get_regoff_str(re,
5939 RT(i).dw_regnum, RT(i).dw_offset));
5956 dump_dwarf_frame_section(struct readelf *re, struct section *s, int alt)
5958 Dwarf_Cie *cie_list, cie, pre_cie;
5959 Dwarf_Fde *fde_list, fde;
5960 Dwarf_Off cie_offset, fde_offset;
5961 Dwarf_Unsigned cie_length, fde_instlen;
5962 Dwarf_Unsigned cie_caf, cie_daf, cie_instlen, func_len, fde_length;
5963 Dwarf_Signed cie_count, fde_count, cie_index;
5966 Dwarf_Small cie_version;
5967 Dwarf_Ptr fde_addr, fde_inst, cie_inst;
5972 printf("\nThe section %s contains:\n\n", s->name);
5974 if (!strcmp(s->name, ".debug_frame")) {
5976 if (dwarf_get_fde_list(re->dbg, &cie_list, &cie_count,
5977 &fde_list, &fde_count, &de) != DW_DLV_OK) {
5978 warnx("dwarf_get_fde_list failed: %s",
5982 } else if (!strcmp(s->name, ".eh_frame")) {
5984 if (dwarf_get_fde_list_eh(re->dbg, &cie_list, &cie_count,
5985 &fde_list, &fde_count, &de) != DW_DLV_OK) {
5986 warnx("dwarf_get_fde_list_eh failed: %s",
5994 for (i = 0; i < fde_count; i++) {
5995 if (dwarf_get_fde_n(fde_list, i, &fde, &de) != DW_DLV_OK) {
5996 warnx("dwarf_get_fde_n failed: %s", dwarf_errmsg(de));
5999 if (dwarf_get_cie_of_fde(fde, &cie, &de) != DW_DLV_OK) {
6000 warnx("dwarf_get_fde_n failed: %s", dwarf_errmsg(de));
6003 if (dwarf_get_fde_range(fde, &low_pc, &func_len, &fde_addr,
6004 &fde_length, &cie_offset, &cie_index, &fde_offset,
6005 &de) != DW_DLV_OK) {
6006 warnx("dwarf_get_fde_range failed: %s",
6010 if (dwarf_get_fde_instr_bytes(fde, &fde_inst, &fde_instlen,
6011 &de) != DW_DLV_OK) {
6012 warnx("dwarf_get_fde_instr_bytes failed: %s",
6016 if (pre_cie == NULL || cie != pre_cie) {
6018 if (dwarf_get_cie_info(cie, &cie_length, &cie_version,
6019 &cie_aug, &cie_caf, &cie_daf, &cie_ra,
6020 &cie_inst, &cie_instlen, &de) != DW_DLV_OK) {
6021 warnx("dwarf_get_cie_info failed: %s",
6025 printf("%08jx %08jx %8.8jx CIE",
6026 (uintmax_t) cie_offset,
6027 (uintmax_t) cie_length,
6028 (uintmax_t) (eh_frame ? 0 : ~0U));
6031 printf(" Version:\t\t\t%u\n", cie_version);
6032 printf(" Augmentation:\t\t\t\"");
6033 while ((c = *cie_aug++) != '\0')
6036 printf(" Code alignment factor:\t%ju\n",
6037 (uintmax_t) cie_caf);
6038 printf(" Data alignment factor:\t%jd\n",
6039 (intmax_t) cie_daf);
6040 printf(" Return address column:\t%ju\n",
6041 (uintmax_t) cie_ra);
6043 dump_dwarf_frame_inst(re, cie, cie_inst,
6044 cie_instlen, cie_caf, cie_daf, 0,
6049 while ((c = *cie_aug++) != '\0')
6052 printf(" cf=%ju df=%jd ra=%ju\n",
6053 (uintmax_t) cie_caf,
6054 (uintmax_t) cie_daf,
6055 (uintmax_t) cie_ra);
6056 dump_dwarf_frame_regtable(re, fde, low_pc, 1,
6061 printf("%08jx %08jx %08jx FDE cie=%08jx pc=%08jx..%08jx\n",
6062 (uintmax_t) fde_offset, (uintmax_t) fde_length,
6063 (uintmax_t) cie_offset,
6064 (uintmax_t) (eh_frame ? fde_offset + 4 - cie_offset :
6066 (uintmax_t) low_pc, (uintmax_t) (low_pc + func_len));
6068 dump_dwarf_frame_inst(re, cie, fde_inst, fde_instlen,
6069 cie_caf, cie_daf, low_pc, re->dbg);
6071 dump_dwarf_frame_regtable(re, fde, low_pc, func_len,
6078 dump_dwarf_frame(struct readelf *re, int alt)
6083 (void) dwarf_set_frame_cfa_value(re->dbg, DW_FRAME_CFA_COL);
6085 for (i = 0; (size_t) i < re->shnum; i++) {
6087 if (s->name != NULL && (!strcmp(s->name, ".debug_frame") ||
6088 !strcmp(s->name, ".eh_frame")))
6089 dump_dwarf_frame_section(re, s, alt);
6094 dump_dwarf_str(struct readelf *re)
6099 int elferr, end, i, j;
6101 printf("\nContents of section .debug_str:\n");
6104 for (i = 0; (size_t) i < re->shnum; i++) {
6106 if (s->name != NULL && !strcmp(s->name, ".debug_str"))
6109 if ((size_t) i >= re->shnum)
6113 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
6114 elferr = elf_errno();
6116 warnx("elf_getdata failed: %s", elf_errmsg(-1));
6122 for (i = 0, p = d->d_buf; (size_t) i < d->d_size; i += 16) {
6123 printf(" 0x%08x", (unsigned int) i);
6124 if ((size_t) i + 16 > d->d_size)
6128 for (j = i; j < i + 16; j++) {
6129 if ((j - i) % 4 == 0)
6135 printf("%02x", (uint8_t) p[j]);
6138 for (j = i; j < end; j++) {
6151 loc_at_comparator(const void *la1, const void *la2)
6153 const struct loc_at *left, *right;
6155 left = (const struct loc_at *)la1;
6156 right = (const struct loc_at *)la2;
6158 if (left->la_off > right->la_off)
6160 else if (left->la_off < right->la_off)
6167 search_loclist_at(struct readelf *re, Dwarf_Die die, Dwarf_Unsigned lowpc,
6168 struct loc_at **la_list, size_t *la_list_len, size_t *la_list_cap)
6171 Dwarf_Attribute *attr_list;
6175 Dwarf_Signed attr_count;
6176 Dwarf_Half attr, form;
6181 is_info = dwarf_get_die_infotypes_flag(die);
6183 if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
6185 if (ret == DW_DLV_ERROR)
6186 warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
6189 for (i = 0; i < attr_count; i++) {
6190 if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
6191 warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
6194 if (attr != DW_AT_location &&
6195 attr != DW_AT_string_length &&
6196 attr != DW_AT_return_addr &&
6197 attr != DW_AT_data_member_location &&
6198 attr != DW_AT_frame_base &&
6199 attr != DW_AT_segment &&
6200 attr != DW_AT_static_link &&
6201 attr != DW_AT_use_location &&
6202 attr != DW_AT_vtable_elem_location)
6204 if (dwarf_whatform(attr_list[i], &form, &de) != DW_DLV_OK) {
6205 warnx("dwarf_whatform failed: %s", dwarf_errmsg(de));
6208 if (form == DW_FORM_data4 || form == DW_FORM_data8) {
6209 if (dwarf_formudata(attr_list[i], &off, &de) !=
6211 warnx("dwarf_formudata failed: %s",
6215 } else if (form == DW_FORM_sec_offset) {
6216 if (dwarf_global_formref(attr_list[i], &ref, &de) !=
6218 warnx("dwarf_global_formref failed: %s",
6226 if (*la_list_cap == *la_list_len) {
6227 *la_list = realloc(*la_list,
6228 *la_list_cap * 2 * sizeof(**la_list));
6229 if (la_list == NULL)
6230 errx(EXIT_FAILURE, "realloc failed");
6233 la = &((*la_list)[*la_list_len]);
6234 la->la_at = attr_list[i];
6236 la->la_lowpc = lowpc;
6237 la->la_cu_psize = re->cu_psize;
6238 la->la_cu_osize = re->cu_osize;
6239 la->la_cu_ver = re->cu_ver;
6244 /* Search children. */
6245 ret = dwarf_child(die, &ret_die, &de);
6246 if (ret == DW_DLV_ERROR)
6247 warnx("dwarf_child: %s", dwarf_errmsg(de));
6248 else if (ret == DW_DLV_OK)
6249 search_loclist_at(re, ret_die, lowpc, la_list,
6250 la_list_len, la_list_cap);
6252 /* Search sibling. */
6253 ret = dwarf_siblingof_b(re->dbg, die, &ret_die, is_info, &de);
6254 if (ret == DW_DLV_ERROR)
6255 warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
6256 else if (ret == DW_DLV_OK)
6257 search_loclist_at(re, ret_die, lowpc, la_list,
6258 la_list_len, la_list_cap);
6262 dump_dwarf_loc(struct readelf *re, Dwarf_Loc *lr)
6269 if (dwarf_get_OP_name(lr->lr_atom, &op_str) !=
6271 snprintf(unk_op, sizeof(unk_op),
6272 "[Unknown OP: %#x]", lr->lr_atom);
6276 printf("%s", op_str);
6278 switch (lr->lr_atom) {
6311 printf(" (%s)", dwarf_regname(re, lr->lr_atom - DW_OP_reg0));
6374 case DW_OP_push_object_address:
6375 case DW_OP_form_tls_address:
6376 case DW_OP_call_frame_cfa:
6377 case DW_OP_stack_value:
6378 case DW_OP_GNU_push_tls_address:
6379 case DW_OP_GNU_uninit:
6384 case DW_OP_deref_size:
6385 case DW_OP_xderef_size:
6392 case DW_OP_plus_uconst:
6395 printf(": %ju", (uintmax_t)
6404 printf(": %jd", (intmax_t)
6440 printf(" (%s): %jd",
6441 dwarf_regname(re, lr->lr_atom - DW_OP_breg0),
6442 (intmax_t) lr->lr_number);
6446 printf(": %jd", (intmax_t)
6451 printf(": %ju (%s) %jd",
6452 (uintmax_t) lr->lr_number,
6453 dwarf_regname(re, (unsigned int) lr->lr_number),
6454 (intmax_t) lr->lr_number2);
6458 case DW_OP_GNU_encoded_addr:
6459 printf(": %#jx", (uintmax_t)
6463 case DW_OP_GNU_implicit_pointer:
6464 printf(": <0x%jx> %jd", (uintmax_t) lr->lr_number,
6465 (intmax_t) lr->lr_number2);
6468 case DW_OP_implicit_value:
6469 printf(": %ju byte block:", (uintmax_t) lr->lr_number);
6470 b = (uint8_t *)(uintptr_t) lr->lr_number2;
6471 for (i = 0; (Dwarf_Unsigned) i < lr->lr_number; i++)
6472 printf(" %x", b[i]);
6475 case DW_OP_GNU_entry_value:
6477 dump_dwarf_block(re, (uint8_t *)(uintptr_t) lr->lr_number2,
6482 case DW_OP_GNU_const_type:
6483 printf(": <0x%jx> ", (uintmax_t) lr->lr_number);
6484 b = (uint8_t *)(uintptr_t) lr->lr_number2;
6486 for (i = 1; (uint8_t) i < n; i++)
6487 printf(" %x", b[i]);
6490 case DW_OP_GNU_regval_type:
6491 printf(": %ju (%s) <0x%jx>", (uintmax_t) lr->lr_number,
6492 dwarf_regname(re, (unsigned int) lr->lr_number),
6493 (uintmax_t) lr->lr_number2);
6496 case DW_OP_GNU_convert:
6497 case DW_OP_GNU_deref_type:
6498 case DW_OP_GNU_parameter_ref:
6499 case DW_OP_GNU_reinterpret:
6500 printf(": <0x%jx>", (uintmax_t) lr->lr_number);
6509 dump_dwarf_block(struct readelf *re, uint8_t *b, Dwarf_Unsigned len)
6511 Dwarf_Locdesc *llbuf;
6516 if (dwarf_loclist_from_expr_b(re->dbg, b, len, re->cu_psize,
6517 re->cu_osize, re->cu_ver, &llbuf, &lcnt, &de) != DW_DLV_OK) {
6518 warnx("dwarf_loclist_form_expr_b: %s", dwarf_errmsg(de));
6522 for (i = 0; (Dwarf_Half) i < llbuf->ld_cents; i++) {
6523 dump_dwarf_loc(re, &llbuf->ld_s[i]);
6524 if (i < llbuf->ld_cents - 1)
6528 dwarf_dealloc(re->dbg, llbuf->ld_s, DW_DLA_LOC_BLOCK);
6529 dwarf_dealloc(re->dbg, llbuf, DW_DLA_LOCDESC);
6533 dump_dwarf_loclist(struct readelf *re)
6536 Dwarf_Locdesc **llbuf;
6537 Dwarf_Unsigned lowpc;
6539 Dwarf_Half tag, version, pointer_size, off_size;
6541 struct loc_at *la_list, *left, *right, *la;
6542 size_t la_list_len, la_list_cap;
6543 unsigned int duplicates, k;
6544 int i, j, ret, has_content;
6548 if ((la_list = calloc(la_list_cap, sizeof(struct loc_at))) == NULL)
6549 errx(EXIT_FAILURE, "calloc failed");
6550 /* Search .debug_info section. */
6551 while ((ret = dwarf_next_cu_header_b(re->dbg, NULL, &version, NULL,
6552 &pointer_size, &off_size, NULL, NULL, &de)) == DW_DLV_OK) {
6553 set_cu_context(re, pointer_size, off_size, version);
6555 if (dwarf_siblingof(re->dbg, die, &die, &de) != DW_DLV_OK)
6557 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
6558 warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
6561 /* XXX: What about DW_TAG_partial_unit? */
6563 if (tag == DW_TAG_compile_unit) {
6564 if (dwarf_attrval_unsigned(die, DW_AT_low_pc,
6565 &lowpc, &de) != DW_DLV_OK)
6569 /* Search attributes for reference to .debug_loc section. */
6570 search_loclist_at(re, die, lowpc, &la_list,
6571 &la_list_len, &la_list_cap);
6573 if (ret == DW_DLV_ERROR)
6574 warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
6576 /* Search .debug_types section. */
6578 while ((ret = dwarf_next_cu_header_c(re->dbg, 0, NULL,
6579 &version, NULL, &pointer_size, &off_size, NULL, NULL,
6580 NULL, NULL, &de)) == DW_DLV_OK) {
6581 set_cu_context(re, pointer_size, off_size, version);
6583 if (dwarf_siblingof(re->dbg, die, &die, &de) !=
6586 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
6587 warnx("dwarf_tag failed: %s",
6593 if (tag == DW_TAG_type_unit) {
6594 if (dwarf_attrval_unsigned(die, DW_AT_low_pc,
6595 &lowpc, &de) != DW_DLV_OK)
6600 * Search attributes for reference to .debug_loc
6603 search_loclist_at(re, die, lowpc, &la_list,
6604 &la_list_len, &la_list_cap);
6606 if (ret == DW_DLV_ERROR)
6607 warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
6608 } while (dwarf_next_types_section(re->dbg, &de) == DW_DLV_OK);
6610 if (la_list_len == 0) {
6615 /* Sort la_list using loc_at_comparator. */
6616 qsort(la_list, la_list_len, sizeof(struct loc_at), loc_at_comparator);
6618 /* Get rid of the duplicates in la_list. */
6620 for (k = 1; k < la_list_len; ++k) {
6621 left = &la_list[k - 1 - duplicates];
6622 right = &la_list[k];
6624 if (left->la_off == right->la_off)
6627 la_list[k - duplicates] = *right;
6629 la_list_len -= duplicates;
6632 for (k = 0; k < la_list_len; ++k) {
6634 if ((ret = dwarf_loclist_n(la->la_at, &llbuf, &lcnt, &de)) !=
6636 if (ret != DW_DLV_NO_ENTRY)
6637 warnx("dwarf_loclist_n failed: %s",
6643 printf("\nContents of section .debug_loc:\n");
6644 printf(" Offset Begin End Expression\n");
6646 set_cu_context(re, la->la_cu_psize, la->la_cu_osize,
6648 for (i = 0; i < lcnt; i++) {
6649 printf(" %8.8jx ", (uintmax_t) la->la_off);
6650 if (llbuf[i]->ld_lopc == 0 && llbuf[i]->ld_hipc == 0) {
6651 printf("<End of list>\n");
6655 /* TODO: handle base selection entry. */
6657 printf("%8.8jx %8.8jx ",
6658 (uintmax_t) (la->la_lowpc + llbuf[i]->ld_lopc),
6659 (uintmax_t) (la->la_lowpc + llbuf[i]->ld_hipc));
6662 for (j = 0; (Dwarf_Half) j < llbuf[i]->ld_cents; j++) {
6663 dump_dwarf_loc(re, &llbuf[i]->ld_s[j]);
6664 if (j < llbuf[i]->ld_cents - 1)
6669 if (llbuf[i]->ld_lopc == llbuf[i]->ld_hipc)
6670 printf(" (start == end)");
6673 for (i = 0; i < lcnt; i++) {
6674 dwarf_dealloc(re->dbg, llbuf[i]->ld_s,
6676 dwarf_dealloc(re->dbg, llbuf[i], DW_DLA_LOCDESC);
6678 dwarf_dealloc(re->dbg, llbuf, DW_DLA_LIST);
6682 printf("\nSection '.debug_loc' has no debugging data.\n");
6688 * Retrieve a string using string table section index and the string offset.
6691 get_string(struct readelf *re, int strtab, size_t off)
6695 if ((name = elf_strptr(re->elf, strtab, off)) == NULL)
6702 * Retrieve the name of a symbol using the section index of the symbol
6703 * table and the index of the symbol within that table.
6706 get_symbol_name(struct readelf *re, int symtab, int i)
6714 s = &re->sl[symtab];
6715 if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM)
6718 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
6719 elferr = elf_errno();
6721 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
6724 if (gelf_getsym(data, i, &sym) != &sym)
6726 /* Return section name for STT_SECTION symbol. */
6727 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION) {
6728 if (sym.st_shndx < re->shnum &&
6729 re->sl[sym.st_shndx].name != NULL)
6730 return (re->sl[sym.st_shndx].name);
6733 if (s->link >= re->shnum ||
6734 (name = elf_strptr(re->elf, s->link, sym.st_name)) == NULL)
6741 get_symbol_value(struct readelf *re, int symtab, int i)
6748 s = &re->sl[symtab];
6749 if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM)
6752 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
6753 elferr = elf_errno();
6755 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
6758 if (gelf_getsym(data, i, &sym) != &sym)
6761 return (sym.st_value);
6765 hex_dump(struct readelf *re)
6774 for (i = 1; (size_t) i < re->shnum; i++) {
6776 if (find_dumpop(re, (size_t) i, s->name, HEX_DUMP, -1) == NULL)
6779 if ((d = elf_getdata(s->scn, NULL)) == NULL &&
6780 (d = elf_rawdata(s->scn, NULL)) == NULL) {
6781 elferr = elf_errno();
6783 warnx("elf_getdata failed: %s",
6784 elf_errmsg(elferr));
6788 if (d->d_size <= 0 || d->d_buf == NULL) {
6789 printf("\nSection '%s' has no data to dump.\n",
6796 printf("\nHex dump of section '%s':\n", s->name);
6798 printf(" 0x%8.8jx ", (uintmax_t)addr);
6799 nbytes = sz > 16? 16 : sz;
6800 for (j = 0; j < 16; j++) {
6801 if ((size_t)j < nbytes)
6802 printf("%2.2x", buf[j]);
6808 for (j = 0; (size_t)j < nbytes; j++) {
6809 if (isprint(buf[j]))
6810 printf("%c", buf[j]);
6823 str_dump(struct readelf *re)
6827 unsigned char *start, *end, *buf_end;
6829 int i, j, elferr, found;
6831 for (i = 1; (size_t) i < re->shnum; i++) {
6833 if (find_dumpop(re, (size_t) i, s->name, STR_DUMP, -1) == NULL)
6836 if ((d = elf_getdata(s->scn, NULL)) == NULL &&
6837 (d = elf_rawdata(s->scn, NULL)) == NULL) {
6838 elferr = elf_errno();
6840 warnx("elf_getdata failed: %s",
6841 elf_errmsg(elferr));
6845 if (d->d_size <= 0 || d->d_buf == NULL) {
6846 printf("\nSection '%s' has no data to dump.\n",
6850 buf_end = (unsigned char *) d->d_buf + d->d_size;
6851 start = (unsigned char *) d->d_buf;
6853 printf("\nString dump of section '%s':\n", s->name);
6855 while (start < buf_end && !isprint(*start))
6857 if (start >= buf_end)
6860 while (end < buf_end && isprint(*end))
6863 (long) (start - (unsigned char *) d->d_buf));
6865 for (j = 0; (unsigned int) j < len; j++)
6874 printf(" No strings found in this section.");
6880 load_sections(struct readelf *re)
6886 size_t shstrndx, ndx;
6889 /* Allocate storage for internal section list. */
6890 if (!elf_getshnum(re->elf, &re->shnum)) {
6891 warnx("elf_getshnum failed: %s", elf_errmsg(-1));
6896 if ((re->sl = calloc(re->shnum, sizeof(*re->sl))) == NULL)
6897 err(EXIT_FAILURE, "calloc failed");
6899 /* Get the index of .shstrtab section. */
6900 if (!elf_getshstrndx(re->elf, &shstrndx)) {
6901 warnx("elf_getshstrndx failed: %s", elf_errmsg(-1));
6905 if ((scn = elf_getscn(re->elf, 0)) == NULL)
6910 if (gelf_getshdr(scn, &sh) == NULL) {
6911 warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
6915 if ((name = elf_strptr(re->elf, shstrndx, sh.sh_name)) == NULL) {
6919 if ((ndx = elf_ndxscn(scn)) == SHN_UNDEF) {
6920 if ((elferr = elf_errno()) != 0) {
6921 warnx("elf_ndxscn failed: %s",
6922 elf_errmsg(elferr));
6926 if (ndx >= re->shnum) {
6927 warnx("section index of '%s' out of range", name);
6930 if (sh.sh_link >= re->shnum)
6931 warnx("section link %llu of '%s' out of range",
6932 (unsigned long long)sh.sh_link, name);
6936 s->off = sh.sh_offset;
6938 s->entsize = sh.sh_entsize;
6939 s->align = sh.sh_addralign;
6940 s->type = sh.sh_type;
6941 s->flags = sh.sh_flags;
6942 s->addr = sh.sh_addr;
6943 s->link = sh.sh_link;
6944 s->info = sh.sh_info;
6945 } while ((scn = elf_nextscn(re->elf, scn)) != NULL);
6946 elferr = elf_errno();
6948 warnx("elf_nextscn failed: %s", elf_errmsg(elferr));
6952 unload_sections(struct readelf *re)
6955 if (re->sl != NULL) {
6965 if (re->ver != NULL) {
6973 dump_elf(struct readelf *re)
6976 /* Fetch ELF header. No need to continue if it fails. */
6977 if (gelf_getehdr(re->elf, &re->ehdr) == NULL) {
6978 warnx("gelf_getehdr failed: %s", elf_errmsg(-1));
6981 if ((re->ec = gelf_getclass(re->elf)) == ELFCLASSNONE) {
6982 warnx("gelf_getclass failed: %s", elf_errmsg(-1));
6985 if (re->ehdr.e_ident[EI_DATA] == ELFDATA2MSB) {
6986 re->dw_read = _read_msb;
6987 re->dw_decode = _decode_msb;
6989 re->dw_read = _read_lsb;
6990 re->dw_decode = _decode_lsb;
6993 if (re->options & ~RE_H)
6995 if ((re->options & RE_VV) || (re->options & RE_S))
6997 if (re->options & RE_H)
6999 if (re->options & RE_L)
7001 if (re->options & RE_SS)
7003 if (re->options & RE_G)
7004 dump_section_groups(re);
7005 if (re->options & RE_D)
7007 if (re->options & RE_R)
7009 if (re->options & RE_S)
7011 if (re->options & RE_N)
7013 if (re->options & RE_II)
7015 if (re->options & RE_X)
7017 if (re->options & RE_P)
7019 if (re->options & RE_VV)
7021 if (re->options & RE_AA)
7022 dump_arch_specific_info(re);
7023 if (re->options & RE_W)
7025 if (re->options & ~RE_H)
7026 unload_sections(re);
7030 dump_dwarf(struct readelf *re)
7035 if (dwarf_elf_init(re->elf, DW_DLC_READ, NULL, NULL, &re->dbg, &de)) {
7036 if ((error = dwarf_errno(de)) != DW_DLE_DEBUG_INFO_NULL)
7037 errx(EXIT_FAILURE, "dwarf_elf_init failed: %s",
7043 dump_dwarf_abbrev(re);
7045 dump_dwarf_line(re);
7046 if (re->dop & DW_LL)
7047 dump_dwarf_line_decoded(re);
7048 if (re->dop & DW_I) {
7049 dump_dwarf_info(re, 0);
7050 dump_dwarf_info(re, 1);
7053 dump_dwarf_pubnames(re);
7055 dump_dwarf_aranges(re);
7056 if (re->dop & DW_RR)
7057 dump_dwarf_ranges(re);
7059 dump_dwarf_macinfo(re);
7061 dump_dwarf_frame(re, 0);
7062 else if (re->dop & DW_FF)
7063 dump_dwarf_frame(re, 1);
7067 dump_dwarf_loclist(re);
7069 dwarf_finish(re->dbg, &de);
7073 dump_ar(struct readelf *re, int fd)
7085 if (re->options & RE_C) {
7086 if ((arsym = elf_getarsym(re->ar, &sz)) == NULL) {
7087 warnx("elf_getarsym() failed: %s", elf_errmsg(-1));
7088 goto process_members;
7090 printf("Index of archive %s: (%ju entries)\n", re->filename,
7091 (uintmax_t) sz - 1);
7093 for (i = 0; (size_t) i < sz; i++) {
7094 if (arsym[i].as_name == NULL)
7096 if (arsym[i].as_off != off) {
7097 off = arsym[i].as_off;
7098 if (elf_rand(re->ar, off) != off) {
7099 warnx("elf_rand() failed: %s",
7103 if ((e = elf_begin(fd, ELF_C_READ, re->ar)) ==
7105 warnx("elf_begin() failed: %s",
7109 if ((arhdr = elf_getarhdr(e)) == NULL) {
7110 warnx("elf_getarhdr() failed: %s",
7115 printf("Binary %s(%s) contains:\n",
7116 re->filename, arhdr->ar_name);
7118 printf("\t%s\n", arsym[i].as_name);
7120 if (elf_rand(re->ar, SARMAG) != SARMAG) {
7121 warnx("elf_rand() failed: %s", elf_errmsg(-1));
7128 if ((re->options & ~RE_C) == 0)
7132 while ((re->elf = elf_begin(fd, cmd, re->ar)) != NULL) {
7133 if ((arhdr = elf_getarhdr(re->elf)) == NULL) {
7134 warnx("elf_getarhdr() failed: %s", elf_errmsg(-1));
7137 if (strcmp(arhdr->ar_name, "/") == 0 ||
7138 strcmp(arhdr->ar_name, "//") == 0 ||
7139 strcmp(arhdr->ar_name, "__.SYMDEF") == 0)
7141 printf("\nFile: %s(%s)\n", re->filename, arhdr->ar_name);
7145 cmd = elf_next(re->elf);
7152 dump_object(struct readelf *re)
7156 if ((fd = open(re->filename, O_RDONLY)) == -1) {
7157 warn("open %s failed", re->filename);
7161 if ((re->flags & DISPLAY_FILENAME) != 0)
7162 printf("\nFile: %s\n", re->filename);
7164 if ((re->elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
7165 warnx("elf_begin() failed: %s", elf_errmsg(-1));
7169 switch (elf_kind(re->elf)) {
7171 warnx("Not an ELF file.");
7180 warnx("Internal: libelf returned unknown elf kind.");
7188 add_dumpop(struct readelf *re, size_t si, const char *sn, int op, int t)
7192 if ((d = find_dumpop(re, si, sn, -1, t)) == NULL) {
7193 if ((d = calloc(1, sizeof(*d))) == NULL)
7194 err(EXIT_FAILURE, "calloc failed");
7195 if (t == DUMP_BY_INDEX)
7201 STAILQ_INSERT_TAIL(&re->v_dumpop, d, dumpop_list);
7206 static struct dumpop *
7207 find_dumpop(struct readelf *re, size_t si, const char *sn, int op, int t)
7211 STAILQ_FOREACH(d, &re->v_dumpop, dumpop_list) {
7212 if ((op == -1 || op & d->op) &&
7213 (t == -1 || (unsigned) t == d->type)) {
7214 if ((d->type == DUMP_BY_INDEX && d->u.si == si) ||
7215 (d->type == DUMP_BY_NAME && !strcmp(d->u.sn, sn)))
7228 {"rawline", 'l', DW_L},
7229 {"decodedline", 'L', DW_LL},
7230 {"info", 'i', DW_I},
7231 {"abbrev", 'a', DW_A},
7232 {"pubnames", 'p', DW_P},
7233 {"aranges", 'r', DW_R},
7234 {"ranges", 'r', DW_R},
7235 {"Ranges", 'R', DW_RR},
7236 {"macro", 'm', DW_M},
7237 {"frames", 'f', DW_F},
7238 {"frames-interp", 'F', DW_FF},
7245 parse_dwarf_op_short(struct readelf *re, const char *op)
7250 re->dop |= DW_DEFAULT_OPTIONS;
7254 for (; *op != '\0'; op++) {
7255 for (i = 0; dwarf_op[i].ln != NULL; i++) {
7256 if (dwarf_op[i].sn == *op) {
7257 re->dop |= dwarf_op[i].value;
7265 parse_dwarf_op_long(struct readelf *re, const char *op)
7267 char *p, *token, *bp;
7271 re->dop |= DW_DEFAULT_OPTIONS;
7275 if ((p = strdup(op)) == NULL)
7276 err(EXIT_FAILURE, "strdup failed");
7279 while ((token = strsep(&p, ",")) != NULL) {
7280 for (i = 0; dwarf_op[i].ln != NULL; i++) {
7281 if (!strcmp(token, dwarf_op[i].ln)) {
7282 re->dop |= dwarf_op[i].value;
7292 _read_lsb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read)
7297 src = (uint8_t *) d->d_buf + *offsetp;
7300 switch (bytes_to_read) {
7302 ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
7303 ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
7306 ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
7309 ret |= ((uint64_t) src[1]) << 8;
7318 *offsetp += bytes_to_read;
7324 _read_msb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read)
7329 src = (uint8_t *) d->d_buf + *offsetp;
7331 switch (bytes_to_read) {
7336 ret = src[1] | ((uint64_t) src[0]) << 8;
7339 ret = src[3] | ((uint64_t) src[2]) << 8;
7340 ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24;
7343 ret = src[7] | ((uint64_t) src[6]) << 8;
7344 ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24;
7345 ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40;
7346 ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56;
7352 *offsetp += bytes_to_read;
7358 _decode_lsb(uint8_t **data, int bytes_to_read)
7366 switch (bytes_to_read) {
7368 ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
7369 ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
7372 ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
7375 ret |= ((uint64_t) src[1]) << 8;
7384 *data += bytes_to_read;
7390 _decode_msb(uint8_t **data, int bytes_to_read)
7398 switch (bytes_to_read) {
7403 ret = src[1] | ((uint64_t) src[0]) << 8;
7406 ret = src[3] | ((uint64_t) src[2]) << 8;
7407 ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24;
7410 ret = src[7] | ((uint64_t) src[6]) << 8;
7411 ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24;
7412 ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40;
7413 ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56;
7420 *data += bytes_to_read;
7426 _decode_sleb128(uint8_t **dp, uint8_t *dpe)
7438 ret |= ((b & 0x7f) << shift);
7440 } while ((b & 0x80) != 0);
7442 if (shift < 32 && (b & 0x40) != 0)
7443 ret |= (-1 << shift);
7451 _decode_uleb128(uint8_t **dp, uint8_t *dpe)
7463 ret |= ((b & 0x7f) << shift);
7465 } while ((b & 0x80) != 0);
7473 readelf_version(void)
7475 (void) printf("%s (%s)\n", ELFTC_GETPROGNAME(),
7480 #define USAGE_MESSAGE "\
7481 Usage: %s [options] file...\n\
7482 Display information about ELF objects and ar(1) archives.\n\n\
7484 -a | --all Equivalent to specifying options '-dhIlrsASV'.\n\
7485 -c | --archive-index Print the archive symbol table for archives.\n\
7486 -d | --dynamic Print the contents of SHT_DYNAMIC sections.\n\
7487 -e | --headers Print all headers in the object.\n\
7488 -g | --section-groups Print the contents of the section groups.\n\
7489 -h | --file-header Print the file header for the object.\n\
7490 -l | --program-headers Print the PHDR table for the object.\n\
7491 -n | --notes Print the contents of SHT_NOTE sections.\n\
7492 -p INDEX | --string-dump=INDEX\n\
7493 Print the contents of section at index INDEX.\n\
7494 -r | --relocs Print relocation information.\n\
7495 -s | --syms | --symbols Print symbol tables.\n\
7496 -t | --section-details Print additional information about sections.\n\
7497 -v | --version Print a version identifier and exit.\n\
7498 -w[afilmoprsFLR] | --debug-dump={abbrev,aranges,decodedline,frames,\n\
7499 frames-interp,info,loc,macro,pubnames,\n\
7500 ranges,Ranges,rawline,str}\n\
7501 Display DWARF information.\n\
7502 -x INDEX | --hex-dump=INDEX\n\
7503 Display contents of a section as hexadecimal.\n\
7504 -A | --arch-specific (accepted, but ignored)\n\
7505 -D | --use-dynamic Print the symbol table specified by the DT_SYMTAB\n\
7506 entry in the \".dynamic\" section.\n\
7507 -H | --help Print a help message.\n\
7508 -I | --histogram Print information on bucket list lengths for \n\
7510 -N | --full-section-name (accepted, but ignored)\n\
7511 -S | --sections | --section-headers\n\
7512 Print information about section headers.\n\
7513 -V | --version-info Print symbol versoning information.\n\
7514 -W | --wide Print information without wrapping long lines.\n"
7518 readelf_usage(int status)
7520 fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());
7525 main(int argc, char **argv)
7527 struct readelf *re, re_storage;
7533 memset(re, 0, sizeof(*re));
7534 STAILQ_INIT(&re->v_dumpop);
7536 while ((opt = getopt_long(argc, argv, "AacDdegHhIi:lNnp:rSstuVvWw::x:",
7537 longopts, NULL)) != -1) {
7540 readelf_usage(EXIT_SUCCESS);
7543 re->options |= RE_AA;
7546 re->options |= RE_AA | RE_D | RE_G | RE_H | RE_II |
7547 RE_L | RE_R | RE_SS | RE_S | RE_VV;
7550 re->options |= RE_C;
7553 re->options |= RE_DD;
7556 re->options |= RE_D;
7559 re->options |= RE_H | RE_L | RE_SS;
7562 re->options |= RE_G;
7565 readelf_usage(EXIT_SUCCESS);
7568 re->options |= RE_H;
7571 re->options |= RE_II;
7574 /* Not implemented yet. */
7577 re->options |= RE_L;
7580 re->options |= RE_NN;
7583 re->options |= RE_N;
7586 re->options |= RE_P;
7587 si = strtoul(optarg, &ep, 10);
7589 add_dumpop(re, (size_t) si, NULL, STR_DUMP,
7592 add_dumpop(re, 0, optarg, STR_DUMP,
7596 re->options |= RE_R;
7599 re->options |= RE_SS;
7602 re->options |= RE_S;
7605 re->options |= RE_SS | RE_T;
7608 re->options |= RE_U;
7611 re->options |= RE_VV;
7617 re->options |= RE_WW;
7620 re->options |= RE_W;
7621 parse_dwarf_op_short(re, optarg);
7624 re->options |= RE_X;
7625 si = strtoul(optarg, &ep, 10);
7627 add_dumpop(re, (size_t) si, NULL, HEX_DUMP,
7630 add_dumpop(re, 0, optarg, HEX_DUMP,
7633 case OPTION_DEBUG_DUMP:
7634 re->options |= RE_W;
7635 parse_dwarf_op_long(re, optarg);
7642 if (argc == 0 || re->options == 0)
7643 readelf_usage(EXIT_FAILURE);
7646 re->flags |= DISPLAY_FILENAME;
7648 if (elf_version(EV_CURRENT) == EV_NONE)
7649 errx(EXIT_FAILURE, "ELF library initialization failed: %s",
7652 for (i = 0; i < argc; i++) {
7653 re->filename = argv[i];