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 3580 2017-09-15 23:29:59Z 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'},
223 static void add_dumpop(struct readelf *re, size_t si, const char *sn, int op,
225 static const char *aeabi_adv_simd_arch(uint64_t simd);
226 static const char *aeabi_align_needed(uint64_t an);
227 static const char *aeabi_align_preserved(uint64_t ap);
228 static const char *aeabi_arm_isa(uint64_t ai);
229 static const char *aeabi_cpu_arch(uint64_t arch);
230 static const char *aeabi_cpu_arch_profile(uint64_t pf);
231 static const char *aeabi_div(uint64_t du);
232 static const char *aeabi_enum_size(uint64_t es);
233 static const char *aeabi_fp_16bit_format(uint64_t fp16);
234 static const char *aeabi_fp_arch(uint64_t fp);
235 static const char *aeabi_fp_denormal(uint64_t fd);
236 static const char *aeabi_fp_exceptions(uint64_t fe);
237 static const char *aeabi_fp_hpext(uint64_t fh);
238 static const char *aeabi_fp_number_model(uint64_t fn);
239 static const char *aeabi_fp_optm_goal(uint64_t fog);
240 static const char *aeabi_fp_rounding(uint64_t fr);
241 static const char *aeabi_hardfp(uint64_t hfp);
242 static const char *aeabi_mpext(uint64_t mp);
243 static const char *aeabi_optm_goal(uint64_t og);
244 static const char *aeabi_pcs_config(uint64_t pcs);
245 static const char *aeabi_pcs_got(uint64_t got);
246 static const char *aeabi_pcs_r9(uint64_t r9);
247 static const char *aeabi_pcs_ro(uint64_t ro);
248 static const char *aeabi_pcs_rw(uint64_t rw);
249 static const char *aeabi_pcs_wchar_t(uint64_t wt);
250 static const char *aeabi_t2ee(uint64_t t2ee);
251 static const char *aeabi_thumb_isa(uint64_t ti);
252 static const char *aeabi_fp_user_exceptions(uint64_t fu);
253 static const char *aeabi_unaligned_access(uint64_t ua);
254 static const char *aeabi_vfp_args(uint64_t va);
255 static const char *aeabi_virtual(uint64_t vt);
256 static const char *aeabi_wmmx_arch(uint64_t wmmx);
257 static const char *aeabi_wmmx_args(uint64_t wa);
258 static const char *elf_class(unsigned int class);
259 static const char *elf_endian(unsigned int endian);
260 static const char *elf_machine(unsigned int mach);
261 static const char *elf_osabi(unsigned int abi);
262 static const char *elf_type(unsigned int type);
263 static const char *elf_ver(unsigned int ver);
264 static const char *dt_type(unsigned int mach, unsigned int dtype);
265 static void dump_ar(struct readelf *re, int);
266 static void dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe);
267 static void dump_attributes(struct readelf *re);
268 static uint8_t *dump_compatibility_tag(uint8_t *p, uint8_t *pe);
269 static void dump_dwarf(struct readelf *re);
270 static void dump_dwarf_abbrev(struct readelf *re);
271 static void dump_dwarf_aranges(struct readelf *re);
272 static void dump_dwarf_block(struct readelf *re, uint8_t *b,
274 static void dump_dwarf_die(struct readelf *re, Dwarf_Die die, int level);
275 static void dump_dwarf_frame(struct readelf *re, int alt);
276 static void dump_dwarf_frame_inst(struct readelf *re, Dwarf_Cie cie,
277 uint8_t *insts, Dwarf_Unsigned len, Dwarf_Unsigned caf, Dwarf_Signed daf,
278 Dwarf_Addr pc, Dwarf_Debug dbg);
279 static int dump_dwarf_frame_regtable(struct readelf *re, Dwarf_Fde fde,
280 Dwarf_Addr pc, Dwarf_Unsigned func_len, Dwarf_Half cie_ra);
281 static void dump_dwarf_frame_section(struct readelf *re, struct section *s,
283 static void dump_dwarf_info(struct readelf *re, Dwarf_Bool is_info);
284 static void dump_dwarf_macinfo(struct readelf *re);
285 static void dump_dwarf_line(struct readelf *re);
286 static void dump_dwarf_line_decoded(struct readelf *re);
287 static void dump_dwarf_loc(struct readelf *re, Dwarf_Loc *lr);
288 static void dump_dwarf_loclist(struct readelf *re);
289 static void dump_dwarf_pubnames(struct readelf *re);
290 static void dump_dwarf_ranges(struct readelf *re);
291 static void dump_dwarf_ranges_foreach(struct readelf *re, Dwarf_Die die,
293 static void dump_dwarf_str(struct readelf *re);
294 static void dump_eflags(struct readelf *re, uint64_t e_flags);
295 static void dump_elf(struct readelf *re);
296 static void dump_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab);
297 static void dump_dynamic(struct readelf *re);
298 static void dump_liblist(struct readelf *re);
299 static void dump_mips_abiflags(struct readelf *re, struct section *s);
300 static void dump_mips_attributes(struct readelf *re, uint8_t *p, uint8_t *pe);
301 static void dump_mips_odk_reginfo(struct readelf *re, uint8_t *p, size_t sz);
302 static void dump_mips_options(struct readelf *re, struct section *s);
303 static void dump_mips_option_flags(const char *name, struct mips_option *opt,
305 static void dump_mips_reginfo(struct readelf *re, struct section *s);
306 static void dump_mips_specific_info(struct readelf *re);
307 static void dump_notes(struct readelf *re);
308 static void dump_notes_content(struct readelf *re, const char *buf, size_t sz,
310 static void dump_svr4_hash(struct section *s);
311 static void dump_svr4_hash64(struct readelf *re, struct section *s);
312 static void dump_gnu_hash(struct readelf *re, struct section *s);
313 static void dump_hash(struct readelf *re);
314 static void dump_phdr(struct readelf *re);
315 static void dump_ppc_attributes(uint8_t *p, uint8_t *pe);
316 static void dump_section_groups(struct readelf *re);
317 static void dump_symtab(struct readelf *re, int i);
318 static void dump_symtabs(struct readelf *re);
319 static uint8_t *dump_unknown_tag(uint64_t tag, uint8_t *p, uint8_t *pe);
320 static void dump_ver(struct readelf *re);
321 static void dump_verdef(struct readelf *re, int dump);
322 static void dump_verneed(struct readelf *re, int dump);
323 static void dump_versym(struct readelf *re);
324 static const char *dwarf_reg(unsigned int mach, unsigned int reg);
325 static const char *dwarf_regname(struct readelf *re, unsigned int num);
326 static struct dumpop *find_dumpop(struct readelf *re, size_t si,
327 const char *sn, int op, int t);
328 static int get_ent_count(struct section *s, int *ent_count);
329 static int get_mips_register_size(uint8_t flag);
330 static char *get_regoff_str(struct readelf *re, Dwarf_Half reg,
332 static const char *get_string(struct readelf *re, int strtab, size_t off);
333 static const char *get_symbol_name(struct readelf *re, int symtab, int i);
334 static uint64_t get_symbol_value(struct readelf *re, int symtab, int i);
335 static void load_sections(struct readelf *re);
336 static const char *mips_abi_fp(uint64_t fp);
337 static const char *note_type(const char *note_name, unsigned int et,
339 static const char *note_type_freebsd(unsigned int nt);
340 static const char *note_type_freebsd_core(unsigned int nt);
341 static const char *note_type_linux_core(unsigned int nt);
342 static const char *note_type_gnu(unsigned int nt);
343 static const char *note_type_netbsd(unsigned int nt);
344 static const char *note_type_openbsd(unsigned int nt);
345 static const char *note_type_unknown(unsigned int nt);
346 static const char *note_type_xen(unsigned int nt);
347 static const char *option_kind(uint8_t kind);
348 static const char *phdr_type(unsigned int mach, unsigned int ptype);
349 static const char *ppc_abi_fp(uint64_t fp);
350 static const char *ppc_abi_vector(uint64_t vec);
351 static void readelf_usage(int status);
352 static void readelf_version(void);
353 static void search_loclist_at(struct readelf *re, Dwarf_Die die,
354 Dwarf_Unsigned lowpc);
355 static void search_ver(struct readelf *re);
356 static const char *section_type(unsigned int mach, unsigned int stype);
357 static void set_cu_context(struct readelf *re, Dwarf_Half psize,
358 Dwarf_Half osize, Dwarf_Half ver);
359 static const char *st_bind(unsigned int sbind);
360 static const char *st_shndx(unsigned int shndx);
361 static const char *st_type(unsigned int mach, unsigned int os,
363 static const char *st_vis(unsigned int svis);
364 static const char *top_tag(unsigned int tag);
365 static void unload_sections(struct readelf *re);
366 static uint64_t _read_lsb(Elf_Data *d, uint64_t *offsetp,
368 static uint64_t _read_msb(Elf_Data *d, uint64_t *offsetp,
370 static uint64_t _decode_lsb(uint8_t **data, int bytes_to_read);
371 static uint64_t _decode_msb(uint8_t **data, int bytes_to_read);
372 static int64_t _decode_sleb128(uint8_t **dp, uint8_t *dpe);
373 static uint64_t _decode_uleb128(uint8_t **dp, uint8_t *dpe);
375 static struct eflags_desc arm_eflags_desc[] = {
376 {EF_ARM_RELEXEC, "relocatable executable"},
377 {EF_ARM_HASENTRY, "has entry point"},
378 {EF_ARM_SYMSARESORTED, "sorted symbol tables"},
379 {EF_ARM_DYNSYMSUSESEGIDX, "dynamic symbols use segment index"},
380 {EF_ARM_MAPSYMSFIRST, "mapping symbols precede others"},
383 {EF_ARM_INTERWORK, "interworking enabled"},
384 {EF_ARM_APCS_26, "uses APCS/26"},
385 {EF_ARM_APCS_FLOAT, "uses APCS/float"},
386 {EF_ARM_PIC, "position independent"},
387 {EF_ARM_ALIGN8, "8 bit structure alignment"},
388 {EF_ARM_NEW_ABI, "uses new ABI"},
389 {EF_ARM_OLD_ABI, "uses old ABI"},
390 {EF_ARM_SOFT_FLOAT, "software FP"},
391 {EF_ARM_VFP_FLOAT, "VFP"},
392 {EF_ARM_MAVERICK_FLOAT, "Maverick FP"},
396 static struct eflags_desc mips_eflags_desc[] = {
397 {EF_MIPS_NOREORDER, "noreorder"},
398 {EF_MIPS_PIC, "pic"},
399 {EF_MIPS_CPIC, "cpic"},
400 {EF_MIPS_UCODE, "ugen_reserved"},
401 {EF_MIPS_ABI2, "abi2"},
402 {EF_MIPS_OPTIONS_FIRST, "odk first"},
403 {EF_MIPS_ARCH_ASE_MDMX, "mdmx"},
404 {EF_MIPS_ARCH_ASE_M16, "mips16"},
408 static struct eflags_desc powerpc_eflags_desc[] = {
410 {EF_PPC_RELOCATABLE, "relocatable"},
411 {EF_PPC_RELOCATABLE_LIB, "relocatable-lib"},
415 static struct eflags_desc sparc_eflags_desc[] = {
416 {EF_SPARC_32PLUS, "v8+"},
417 {EF_SPARC_SUN_US1, "ultrasparcI"},
418 {EF_SPARC_HAL_R1, "halr1"},
419 {EF_SPARC_SUN_US3, "ultrasparcIII"},
424 elf_osabi(unsigned int abi)
426 static char s_abi[32];
429 case ELFOSABI_NONE: return "NONE";
430 case ELFOSABI_HPUX: return "HPUX";
431 case ELFOSABI_NETBSD: return "NetBSD";
432 case ELFOSABI_GNU: return "GNU";
433 case ELFOSABI_HURD: return "HURD";
434 case ELFOSABI_86OPEN: return "86OPEN";
435 case ELFOSABI_SOLARIS: return "Solaris";
436 case ELFOSABI_AIX: return "AIX";
437 case ELFOSABI_IRIX: return "IRIX";
438 case ELFOSABI_FREEBSD: return "FreeBSD";
439 case ELFOSABI_TRU64: return "TRU64";
440 case ELFOSABI_MODESTO: return "MODESTO";
441 case ELFOSABI_OPENBSD: return "OpenBSD";
442 case ELFOSABI_OPENVMS: return "OpenVMS";
443 case ELFOSABI_NSK: return "NSK";
444 case ELFOSABI_CLOUDABI: return "CloudABI";
445 case ELFOSABI_ARM_AEABI: return "ARM EABI";
446 case ELFOSABI_ARM: return "ARM";
447 case ELFOSABI_STANDALONE: return "StandAlone";
449 snprintf(s_abi, sizeof(s_abi), "<unknown: %#x>", abi);
455 elf_machine(unsigned int mach)
457 static char s_mach[32];
460 case EM_NONE: return "Unknown machine";
461 case EM_M32: return "AT&T WE32100";
462 case EM_SPARC: return "Sun SPARC";
463 case EM_386: return "Intel i386";
464 case EM_68K: return "Motorola 68000";
465 case EM_IAMCU: return "Intel MCU";
466 case EM_88K: return "Motorola 88000";
467 case EM_860: return "Intel i860";
468 case EM_MIPS: return "MIPS R3000 Big-Endian only";
469 case EM_S370: return "IBM System/370";
470 case EM_MIPS_RS3_LE: return "MIPS R3000 Little-Endian";
471 case EM_PARISC: return "HP PA-RISC";
472 case EM_VPP500: return "Fujitsu VPP500";
473 case EM_SPARC32PLUS: return "SPARC v8plus";
474 case EM_960: return "Intel 80960";
475 case EM_PPC: return "PowerPC 32-bit";
476 case EM_PPC64: return "PowerPC 64-bit";
477 case EM_S390: return "IBM System/390";
478 case EM_V800: return "NEC V800";
479 case EM_FR20: return "Fujitsu FR20";
480 case EM_RH32: return "TRW RH-32";
481 case EM_RCE: return "Motorola RCE";
482 case EM_ARM: return "ARM";
483 case EM_SH: return "Hitachi SH";
484 case EM_SPARCV9: return "SPARC v9 64-bit";
485 case EM_TRICORE: return "Siemens TriCore embedded processor";
486 case EM_ARC: return "Argonaut RISC Core";
487 case EM_H8_300: return "Hitachi H8/300";
488 case EM_H8_300H: return "Hitachi H8/300H";
489 case EM_H8S: return "Hitachi H8S";
490 case EM_H8_500: return "Hitachi H8/500";
491 case EM_IA_64: return "Intel IA-64 Processor";
492 case EM_MIPS_X: return "Stanford MIPS-X";
493 case EM_COLDFIRE: return "Motorola ColdFire";
494 case EM_68HC12: return "Motorola M68HC12";
495 case EM_MMA: return "Fujitsu MMA";
496 case EM_PCP: return "Siemens PCP";
497 case EM_NCPU: return "Sony nCPU";
498 case EM_NDR1: return "Denso NDR1 microprocessor";
499 case EM_STARCORE: return "Motorola Star*Core processor";
500 case EM_ME16: return "Toyota ME16 processor";
501 case EM_ST100: return "STMicroelectronics ST100 processor";
502 case EM_TINYJ: return "Advanced Logic Corp. TinyJ processor";
503 case EM_X86_64: return "Advanced Micro Devices x86-64";
504 case EM_PDSP: return "Sony DSP Processor";
505 case EM_FX66: return "Siemens FX66 microcontroller";
506 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 microcontroller";
507 case EM_ST7: return "STmicroelectronics ST7 8-bit microcontroller";
508 case EM_68HC16: return "Motorola MC68HC16 microcontroller";
509 case EM_68HC11: return "Motorola MC68HC11 microcontroller";
510 case EM_68HC08: return "Motorola MC68HC08 microcontroller";
511 case EM_68HC05: return "Motorola MC68HC05 microcontroller";
512 case EM_SVX: return "Silicon Graphics SVx";
513 case EM_ST19: return "STMicroelectronics ST19 8-bit mc";
514 case EM_VAX: return "Digital VAX";
515 case EM_CRIS: return "Axis Communications 32-bit embedded processor";
516 case EM_JAVELIN: return "Infineon Tech. 32bit embedded processor";
517 case EM_FIREPATH: return "Element 14 64-bit DSP Processor";
518 case EM_ZSP: return "LSI Logic 16-bit DSP Processor";
519 case EM_MMIX: return "Donald Knuth's educational 64-bit proc";
520 case EM_HUANY: return "Harvard University MI object files";
521 case EM_PRISM: return "SiTera Prism";
522 case EM_AVR: return "Atmel AVR 8-bit microcontroller";
523 case EM_FR30: return "Fujitsu FR30";
524 case EM_D10V: return "Mitsubishi D10V";
525 case EM_D30V: return "Mitsubishi D30V";
526 case EM_V850: return "NEC v850";
527 case EM_M32R: return "Mitsubishi M32R";
528 case EM_MN10300: return "Matsushita MN10300";
529 case EM_MN10200: return "Matsushita MN10200";
530 case EM_PJ: return "picoJava";
531 case EM_OPENRISC: return "OpenRISC 32-bit embedded processor";
532 case EM_ARC_A5: return "ARC Cores Tangent-A5";
533 case EM_XTENSA: return "Tensilica Xtensa Architecture";
534 case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
535 case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
536 case EM_NS32K: return "National Semiconductor 32000 series";
537 case EM_TPC: return "Tenor Network TPC processor";
538 case EM_SNP1K: return "Trebia SNP 1000 processor";
539 case EM_ST200: return "STMicroelectronics ST200 microcontroller";
540 case EM_IP2K: return "Ubicom IP2xxx microcontroller family";
541 case EM_MAX: return "MAX Processor";
542 case EM_CR: return "National Semiconductor CompactRISC microprocessor";
543 case EM_F2MC16: return "Fujitsu F2MC16";
544 case EM_MSP430: return "TI embedded microcontroller msp430";
545 case EM_BLACKFIN: return "Analog Devices Blackfin (DSP) processor";
546 case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
547 case EM_SEP: return "Sharp embedded microprocessor";
548 case EM_ARCA: return "Arca RISC Microprocessor";
549 case EM_UNICORE: return "Microprocessor series from PKU-Unity Ltd";
550 case EM_AARCH64: return "AArch64";
551 case EM_RISCV: return "RISC-V";
553 snprintf(s_mach, sizeof(s_mach), "<unknown: %#x>", mach);
560 elf_class(unsigned int class)
562 static char s_class[32];
565 case ELFCLASSNONE: return "none";
566 case ELFCLASS32: return "ELF32";
567 case ELFCLASS64: return "ELF64";
569 snprintf(s_class, sizeof(s_class), "<unknown: %#x>", class);
575 elf_endian(unsigned int endian)
577 static char s_endian[32];
580 case ELFDATANONE: return "none";
581 case ELFDATA2LSB: return "2's complement, little endian";
582 case ELFDATA2MSB: return "2's complement, big endian";
584 snprintf(s_endian, sizeof(s_endian), "<unknown: %#x>", endian);
590 elf_type(unsigned int type)
592 static char s_type[32];
595 case ET_NONE: return "NONE (None)";
596 case ET_REL: return "REL (Relocatable file)";
597 case ET_EXEC: return "EXEC (Executable file)";
598 case ET_DYN: return "DYN (Shared object file)";
599 case ET_CORE: return "CORE (Core file)";
601 if (type >= ET_LOPROC)
602 snprintf(s_type, sizeof(s_type), "<proc: %#x>", type);
603 else if (type >= ET_LOOS && type <= ET_HIOS)
604 snprintf(s_type, sizeof(s_type), "<os: %#x>", type);
606 snprintf(s_type, sizeof(s_type), "<unknown: %#x>",
613 elf_ver(unsigned int ver)
615 static char s_ver[32];
618 case EV_CURRENT: return "(current)";
619 case EV_NONE: return "(none)";
621 snprintf(s_ver, sizeof(s_ver), "<unknown: %#x>",
628 phdr_type(unsigned int mach, unsigned int ptype)
630 static char s_ptype[32];
632 if (ptype >= PT_LOPROC && ptype <= PT_HIPROC) {
636 case PT_ARM_ARCHEXT: return "ARM_ARCHEXT";
637 case PT_ARM_EXIDX: return "ARM_EXIDX";
641 snprintf(s_ptype, sizeof(s_ptype), "LOPROC+%#x",
647 case PT_NULL: return "NULL";
648 case PT_LOAD: return "LOAD";
649 case PT_DYNAMIC: return "DYNAMIC";
650 case PT_INTERP: return "INTERP";
651 case PT_NOTE: return "NOTE";
652 case PT_SHLIB: return "SHLIB";
653 case PT_PHDR: return "PHDR";
654 case PT_TLS: return "TLS";
655 case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
656 case PT_GNU_STACK: return "GNU_STACK";
657 case PT_GNU_RELRO: return "GNU_RELRO";
659 if (ptype >= PT_LOOS && ptype <= PT_HIOS)
660 snprintf(s_ptype, sizeof(s_ptype), "LOOS+%#x",
663 snprintf(s_ptype, sizeof(s_ptype), "<unknown: %#x>",
670 section_type(unsigned int mach, unsigned int stype)
672 static char s_stype[32];
674 if (stype >= SHT_LOPROC && stype <= SHT_HIPROC) {
678 case SHT_ARM_EXIDX: return "ARM_EXIDX";
679 case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP";
680 case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES";
681 case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY";
682 case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION";
687 case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
695 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
696 case SHT_MIPS_MSYM: return "MIPS_MSYM";
697 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
698 case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
699 case SHT_MIPS_UCODE: return "MIPS_UCODE";
700 case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
701 case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
702 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
703 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
704 case SHT_MIPS_RELD: return "MIPS_RELD";
705 case SHT_MIPS_IFACE: return "MIPS_IFACE";
706 case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
707 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
708 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
709 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
710 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
711 case SHT_MIPS_DWARF: return "MIPS_DWARF";
712 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
713 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
714 case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
715 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
716 case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
717 case SHT_MIPS_XLATE: return "MIPS_XLATE";
718 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
719 case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
720 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
721 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
722 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
723 case SHT_MIPS_ABIFLAGS: return "MIPS_ABIFLAGS";
732 snprintf(s_stype, sizeof(s_stype), "LOPROC+%#x",
738 case SHT_NULL: return "NULL";
739 case SHT_PROGBITS: return "PROGBITS";
740 case SHT_SYMTAB: return "SYMTAB";
741 case SHT_STRTAB: return "STRTAB";
742 case SHT_RELA: return "RELA";
743 case SHT_HASH: return "HASH";
744 case SHT_DYNAMIC: return "DYNAMIC";
745 case SHT_NOTE: return "NOTE";
746 case SHT_NOBITS: return "NOBITS";
747 case SHT_REL: return "REL";
748 case SHT_SHLIB: return "SHLIB";
749 case SHT_DYNSYM: return "DYNSYM";
750 case SHT_INIT_ARRAY: return "INIT_ARRAY";
751 case SHT_FINI_ARRAY: return "FINI_ARRAY";
752 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
753 case SHT_GROUP: return "GROUP";
754 case SHT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
755 case SHT_SUNW_dof: return "SUNW_dof";
756 case SHT_SUNW_cap: return "SUNW_cap";
757 case SHT_GNU_HASH: return "GNU_HASH";
758 case SHT_SUNW_ANNOTATE: return "SUNW_ANNOTATE";
759 case SHT_SUNW_DEBUGSTR: return "SUNW_DEBUGSTR";
760 case SHT_SUNW_DEBUG: return "SUNW_DEBUG";
761 case SHT_SUNW_move: return "SUNW_move";
762 case SHT_SUNW_COMDAT: return "SUNW_COMDAT";
763 case SHT_SUNW_syminfo: return "SUNW_syminfo";
764 case SHT_SUNW_verdef: return "SUNW_verdef";
765 case SHT_SUNW_verneed: return "SUNW_verneed";
766 case SHT_SUNW_versym: return "SUNW_versym";
768 if (stype >= SHT_LOOS && stype <= SHT_HIOS)
769 snprintf(s_stype, sizeof(s_stype), "LOOS+%#x",
771 else if (stype >= SHT_LOUSER)
772 snprintf(s_stype, sizeof(s_stype), "LOUSER+%#x",
775 snprintf(s_stype, sizeof(s_stype), "<unknown: %#x>",
782 dt_type(unsigned int mach, unsigned int dtype)
784 static char s_dtype[32];
787 case DT_NULL: return "NULL";
788 case DT_NEEDED: return "NEEDED";
789 case DT_PLTRELSZ: return "PLTRELSZ";
790 case DT_PLTGOT: return "PLTGOT";
791 case DT_HASH: return "HASH";
792 case DT_STRTAB: return "STRTAB";
793 case DT_SYMTAB: return "SYMTAB";
794 case DT_RELA: return "RELA";
795 case DT_RELASZ: return "RELASZ";
796 case DT_RELAENT: return "RELAENT";
797 case DT_STRSZ: return "STRSZ";
798 case DT_SYMENT: return "SYMENT";
799 case DT_INIT: return "INIT";
800 case DT_FINI: return "FINI";
801 case DT_SONAME: return "SONAME";
802 case DT_RPATH: return "RPATH";
803 case DT_SYMBOLIC: return "SYMBOLIC";
804 case DT_REL: return "REL";
805 case DT_RELSZ: return "RELSZ";
806 case DT_RELENT: return "RELENT";
807 case DT_PLTREL: return "PLTREL";
808 case DT_DEBUG: return "DEBUG";
809 case DT_TEXTREL: return "TEXTREL";
810 case DT_JMPREL: return "JMPREL";
811 case DT_BIND_NOW: return "BIND_NOW";
812 case DT_INIT_ARRAY: return "INIT_ARRAY";
813 case DT_FINI_ARRAY: return "FINI_ARRAY";
814 case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
815 case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
816 case DT_RUNPATH: return "RUNPATH";
817 case DT_FLAGS: return "FLAGS";
818 case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
819 case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
820 case DT_MAXPOSTAGS: return "MAXPOSTAGS";
821 case DT_SUNW_AUXILIARY: return "SUNW_AUXILIARY";
822 case DT_SUNW_RTLDINF: return "SUNW_RTLDINF";
823 case DT_SUNW_FILTER: return "SUNW_FILTER";
824 case DT_SUNW_CAP: return "SUNW_CAP";
825 case DT_SUNW_ASLR: return "SUNW_ASLR";
826 case DT_CHECKSUM: return "CHECKSUM";
827 case DT_PLTPADSZ: return "PLTPADSZ";
828 case DT_MOVEENT: return "MOVEENT";
829 case DT_MOVESZ: return "MOVESZ";
830 case DT_FEATURE: return "FEATURE";
831 case DT_POSFLAG_1: return "POSFLAG_1";
832 case DT_SYMINSZ: return "SYMINSZ";
833 case DT_SYMINENT: return "SYMINENT";
834 case DT_GNU_HASH: return "GNU_HASH";
835 case DT_TLSDESC_PLT: return "DT_TLSDESC_PLT";
836 case DT_TLSDESC_GOT: return "DT_TLSDESC_GOT";
837 case DT_GNU_CONFLICT: return "GNU_CONFLICT";
838 case DT_GNU_LIBLIST: return "GNU_LIBLIST";
839 case DT_CONFIG: return "CONFIG";
840 case DT_DEPAUDIT: return "DEPAUDIT";
841 case DT_AUDIT: return "AUDIT";
842 case DT_PLTPAD: return "PLTPAD";
843 case DT_MOVETAB: return "MOVETAB";
844 case DT_SYMINFO: return "SYMINFO";
845 case DT_VERSYM: return "VERSYM";
846 case DT_RELACOUNT: return "RELACOUNT";
847 case DT_RELCOUNT: return "RELCOUNT";
848 case DT_FLAGS_1: return "FLAGS_1";
849 case DT_VERDEF: return "VERDEF";
850 case DT_VERDEFNUM: return "VERDEFNUM";
851 case DT_VERNEED: return "VERNEED";
852 case DT_VERNEEDNUM: return "VERNEEDNUM";
853 case DT_AUXILIARY: return "AUXILIARY";
854 case DT_USED: return "USED";
855 case DT_FILTER: return "FILTER";
856 case DT_GNU_PRELINKED: return "GNU_PRELINKED";
857 case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
858 case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
861 if (dtype >= DT_LOPROC && dtype <= DT_HIPROC) {
865 case DT_ARM_SYMTABSZ:
866 return "ARM_SYMTABSZ";
874 case DT_MIPS_RLD_VERSION:
875 return "MIPS_RLD_VERSION";
876 case DT_MIPS_TIME_STAMP:
877 return "MIPS_TIME_STAMP";
878 case DT_MIPS_ICHECKSUM:
879 return "MIPS_ICHECKSUM";
880 case DT_MIPS_IVERSION:
881 return "MIPS_IVERSION";
884 case DT_MIPS_BASE_ADDRESS:
885 return "MIPS_BASE_ADDRESS";
886 case DT_MIPS_CONFLICT:
887 return "MIPS_CONFLICT";
888 case DT_MIPS_LIBLIST:
889 return "MIPS_LIBLIST";
890 case DT_MIPS_LOCAL_GOTNO:
891 return "MIPS_LOCAL_GOTNO";
892 case DT_MIPS_CONFLICTNO:
893 return "MIPS_CONFLICTNO";
894 case DT_MIPS_LIBLISTNO:
895 return "MIPS_LIBLISTNO";
896 case DT_MIPS_SYMTABNO:
897 return "MIPS_SYMTABNO";
898 case DT_MIPS_UNREFEXTNO:
899 return "MIPS_UNREFEXTNO";
901 return "MIPS_GOTSYM";
902 case DT_MIPS_HIPAGENO:
903 return "MIPS_HIPAGENO";
904 case DT_MIPS_RLD_MAP:
905 return "MIPS_RLD_MAP";
906 case DT_MIPS_DELTA_CLASS:
907 return "MIPS_DELTA_CLASS";
908 case DT_MIPS_DELTA_CLASS_NO:
909 return "MIPS_DELTA_CLASS_NO";
910 case DT_MIPS_DELTA_INSTANCE:
911 return "MIPS_DELTA_INSTANCE";
912 case DT_MIPS_DELTA_INSTANCE_NO:
913 return "MIPS_DELTA_INSTANCE_NO";
914 case DT_MIPS_DELTA_RELOC:
915 return "MIPS_DELTA_RELOC";
916 case DT_MIPS_DELTA_RELOC_NO:
917 return "MIPS_DELTA_RELOC_NO";
918 case DT_MIPS_DELTA_SYM:
919 return "MIPS_DELTA_SYM";
920 case DT_MIPS_DELTA_SYM_NO:
921 return "MIPS_DELTA_SYM_NO";
922 case DT_MIPS_DELTA_CLASSSYM:
923 return "MIPS_DELTA_CLASSSYM";
924 case DT_MIPS_DELTA_CLASSSYM_NO:
925 return "MIPS_DELTA_CLASSSYM_NO";
926 case DT_MIPS_CXX_FLAGS:
927 return "MIPS_CXX_FLAGS";
928 case DT_MIPS_PIXIE_INIT:
929 return "MIPS_PIXIE_INIT";
930 case DT_MIPS_SYMBOL_LIB:
931 return "MIPS_SYMBOL_LIB";
932 case DT_MIPS_LOCALPAGE_GOTIDX:
933 return "MIPS_LOCALPAGE_GOTIDX";
934 case DT_MIPS_LOCAL_GOTIDX:
935 return "MIPS_LOCAL_GOTIDX";
936 case DT_MIPS_HIDDEN_GOTIDX:
937 return "MIPS_HIDDEN_GOTIDX";
938 case DT_MIPS_PROTECTED_GOTIDX:
939 return "MIPS_PROTECTED_GOTIDX";
940 case DT_MIPS_OPTIONS:
941 return "MIPS_OPTIONS";
942 case DT_MIPS_INTERFACE:
943 return "MIPS_INTERFACE";
944 case DT_MIPS_DYNSTR_ALIGN:
945 return "MIPS_DYNSTR_ALIGN";
946 case DT_MIPS_INTERFACE_SIZE:
947 return "MIPS_INTERFACE_SIZE";
948 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
949 return "MIPS_RLD_TEXT_RESOLVE_ADDR";
950 case DT_MIPS_PERF_SUFFIX:
951 return "MIPS_PERF_SUFFIX";
952 case DT_MIPS_COMPACT_SIZE:
953 return "MIPS_COMPACT_SIZE";
954 case DT_MIPS_GP_VALUE:
955 return "MIPS_GP_VALUE";
956 case DT_MIPS_AUX_DYNAMIC:
957 return "MIPS_AUX_DYNAMIC";
959 return "MIPS_PLTGOT";
960 case DT_MIPS_RLD_OBJ_UPDATE:
961 return "MIPS_RLD_OBJ_UPDATE";
972 case DT_SPARC_REGISTER:
973 return "DT_SPARC_REGISTER";
983 snprintf(s_dtype, sizeof(s_dtype), "<unknown: %#x>", dtype);
988 st_bind(unsigned int sbind)
990 static char s_sbind[32];
993 case STB_LOCAL: return "LOCAL";
994 case STB_GLOBAL: return "GLOBAL";
995 case STB_WEAK: return "WEAK";
996 case STB_GNU_UNIQUE: return "UNIQUE";
998 if (sbind >= STB_LOOS && sbind <= STB_HIOS)
1000 else if (sbind >= STB_LOPROC && sbind <= STB_HIPROC)
1003 snprintf(s_sbind, sizeof(s_sbind), "<unknown: %#x>",
1010 st_type(unsigned int mach, unsigned int os, unsigned int stype)
1012 static char s_stype[32];
1015 case STT_NOTYPE: return "NOTYPE";
1016 case STT_OBJECT: return "OBJECT";
1017 case STT_FUNC: return "FUNC";
1018 case STT_SECTION: return "SECTION";
1019 case STT_FILE: return "FILE";
1020 case STT_COMMON: return "COMMON";
1021 case STT_TLS: return "TLS";
1023 if (stype >= STT_LOOS && stype <= STT_HIOS) {
1024 if ((os == ELFOSABI_GNU || os == ELFOSABI_FREEBSD) &&
1025 stype == STT_GNU_IFUNC)
1027 snprintf(s_stype, sizeof(s_stype), "OS+%#x",
1029 } else if (stype >= STT_LOPROC && stype <= STT_HIPROC) {
1030 if (mach == EM_SPARCV9 && stype == STT_SPARC_REGISTER)
1032 snprintf(s_stype, sizeof(s_stype), "PROC+%#x",
1033 stype - STT_LOPROC);
1035 snprintf(s_stype, sizeof(s_stype), "<unknown: %#x>",
1042 st_vis(unsigned int svis)
1044 static char s_svis[32];
1047 case STV_DEFAULT: return "DEFAULT";
1048 case STV_INTERNAL: return "INTERNAL";
1049 case STV_HIDDEN: return "HIDDEN";
1050 case STV_PROTECTED: return "PROTECTED";
1052 snprintf(s_svis, sizeof(s_svis), "<unknown: %#x>", svis);
1058 st_shndx(unsigned int shndx)
1060 static char s_shndx[32];
1063 case SHN_UNDEF: return "UND";
1064 case SHN_ABS: return "ABS";
1065 case SHN_COMMON: return "COM";
1067 if (shndx >= SHN_LOPROC && shndx <= SHN_HIPROC)
1069 else if (shndx >= SHN_LOOS && shndx <= SHN_HIOS)
1072 snprintf(s_shndx, sizeof(s_shndx), "%u", shndx);
1081 } section_flag[] = {
1082 {"WRITE", 'W', SHF_WRITE},
1083 {"ALLOC", 'A', SHF_ALLOC},
1084 {"EXEC", 'X', SHF_EXECINSTR},
1085 {"MERGE", 'M', SHF_MERGE},
1086 {"STRINGS", 'S', SHF_STRINGS},
1087 {"INFO LINK", 'I', SHF_INFO_LINK},
1088 {"OS NONCONF", 'O', SHF_OS_NONCONFORMING},
1089 {"GROUP", 'G', SHF_GROUP},
1090 {"TLS", 'T', SHF_TLS},
1091 {"COMPRESSED", 'C', SHF_COMPRESSED},
1096 note_type(const char *name, unsigned int et, unsigned int nt)
1098 if ((strcmp(name, "CORE") == 0 || strcmp(name, "LINUX") == 0) &&
1100 return note_type_linux_core(nt);
1101 else if (strcmp(name, "FreeBSD") == 0)
1103 return note_type_freebsd_core(nt);
1105 return note_type_freebsd(nt);
1106 else if (strcmp(name, "GNU") == 0 && et != ET_CORE)
1107 return note_type_gnu(nt);
1108 else if (strcmp(name, "NetBSD") == 0 && et != ET_CORE)
1109 return note_type_netbsd(nt);
1110 else if (strcmp(name, "OpenBSD") == 0 && et != ET_CORE)
1111 return note_type_openbsd(nt);
1112 else if (strcmp(name, "Xen") == 0 && et != ET_CORE)
1113 return note_type_xen(nt);
1114 return note_type_unknown(nt);
1118 note_type_freebsd(unsigned int nt)
1121 case 1: return "NT_FREEBSD_ABI_TAG";
1122 case 2: return "NT_FREEBSD_NOINIT_TAG";
1123 case 3: return "NT_FREEBSD_ARCH_TAG";
1124 default: return (note_type_unknown(nt));
1129 note_type_freebsd_core(unsigned int nt)
1132 case 1: return "NT_PRSTATUS";
1133 case 2: return "NT_FPREGSET";
1134 case 3: return "NT_PRPSINFO";
1135 case 7: return "NT_THRMISC";
1136 case 8: return "NT_PROCSTAT_PROC";
1137 case 9: return "NT_PROCSTAT_FILES";
1138 case 10: return "NT_PROCSTAT_VMMAP";
1139 case 11: return "NT_PROCSTAT_GROUPS";
1140 case 12: return "NT_PROCSTAT_UMASK";
1141 case 13: return "NT_PROCSTAT_RLIMIT";
1142 case 14: return "NT_PROCSTAT_OSREL";
1143 case 15: return "NT_PROCSTAT_PSSTRINGS";
1144 case 16: return "NT_PROCSTAT_AUXV";
1145 case 17: return "NT_PTLWPINFO";
1146 case 0x202: return "NT_X86_XSTATE (x86 XSAVE extended state)";
1147 case 0x400: return "NT_ARM_VFP (arm VFP registers)";
1148 default: return (note_type_unknown(nt));
1153 note_type_linux_core(unsigned int nt)
1156 case 1: return "NT_PRSTATUS (Process status)";
1157 case 2: return "NT_FPREGSET (Floating point information)";
1158 case 3: return "NT_PRPSINFO (Process information)";
1159 case 4: return "NT_TASKSTRUCT (Task structure)";
1160 case 6: return "NT_AUXV (Auxiliary vector)";
1161 case 10: return "NT_PSTATUS (Linux process status)";
1162 case 12: return "NT_FPREGS (Linux floating point regset)";
1163 case 13: return "NT_PSINFO (Linux process information)";
1164 case 16: return "NT_LWPSTATUS (Linux lwpstatus_t type)";
1165 case 17: return "NT_LWPSINFO (Linux lwpinfo_t type)";
1166 case 18: return "NT_WIN32PSTATUS (win32_pstatus structure)";
1167 case 0x100: return "NT_PPC_VMX (ppc Altivec registers)";
1168 case 0x102: return "NT_PPC_VSX (ppc VSX registers)";
1169 case 0x202: return "NT_X86_XSTATE (x86 XSAVE extended state)";
1170 case 0x300: return "NT_S390_HIGH_GPRS (s390 upper register halves)";
1171 case 0x301: return "NT_S390_TIMER (s390 timer register)";
1172 case 0x302: return "NT_S390_TODCMP (s390 TOD comparator register)";
1173 case 0x303: return "NT_S390_TODPREG (s390 TOD programmable register)";
1174 case 0x304: return "NT_S390_CTRS (s390 control registers)";
1175 case 0x305: return "NT_S390_PREFIX (s390 prefix register)";
1176 case 0x400: return "NT_ARM_VFP (arm VFP registers)";
1177 case 0x46494c45UL: return "NT_FILE (mapped files)";
1178 case 0x46E62B7FUL: return "NT_PRXFPREG (Linux user_xfpregs structure)";
1179 case 0x53494749UL: return "NT_SIGINFO (siginfo_t data)";
1180 default: return (note_type_unknown(nt));
1185 note_type_gnu(unsigned int nt)
1188 case 1: return "NT_GNU_ABI_TAG";
1189 case 2: return "NT_GNU_HWCAP (Hardware capabilities)";
1190 case 3: return "NT_GNU_BUILD_ID (Build id set by ld(1))";
1191 case 4: return "NT_GNU_GOLD_VERSION (GNU gold version)";
1192 case 5: return "NT_GNU_PROPERTY_TYPE_0";
1193 default: return (note_type_unknown(nt));
1198 note_type_netbsd(unsigned int nt)
1201 case 1: return "NT_NETBSD_IDENT";
1202 default: return (note_type_unknown(nt));
1207 note_type_openbsd(unsigned int nt)
1210 case 1: return "NT_OPENBSD_IDENT";
1211 default: return (note_type_unknown(nt));
1216 note_type_unknown(unsigned int nt)
1218 static char s_nt[32];
1220 snprintf(s_nt, sizeof(s_nt),
1221 nt >= 0x100 ? "<unknown: 0x%x>" : "<unknown: %u>", nt);
1226 note_type_xen(unsigned int nt)
1229 case 0: return "XEN_ELFNOTE_INFO";
1230 case 1: return "XEN_ELFNOTE_ENTRY";
1231 case 2: return "XEN_ELFNOTE_HYPERCALL_PAGE";
1232 case 3: return "XEN_ELFNOTE_VIRT_BASE";
1233 case 4: return "XEN_ELFNOTE_PADDR_OFFSET";
1234 case 5: return "XEN_ELFNOTE_XEN_VERSION";
1235 case 6: return "XEN_ELFNOTE_GUEST_OS";
1236 case 7: return "XEN_ELFNOTE_GUEST_VERSION";
1237 case 8: return "XEN_ELFNOTE_LOADER";
1238 case 9: return "XEN_ELFNOTE_PAE_MODE";
1239 case 10: return "XEN_ELFNOTE_FEATURES";
1240 case 11: return "XEN_ELFNOTE_BSD_SYMTAB";
1241 case 12: return "XEN_ELFNOTE_HV_START_LOW";
1242 case 13: return "XEN_ELFNOTE_L1_MFN_VALID";
1243 case 14: return "XEN_ELFNOTE_SUSPEND_CANCEL";
1244 case 15: return "XEN_ELFNOTE_INIT_P2M";
1245 case 16: return "XEN_ELFNOTE_MOD_START_PFN";
1246 case 17: return "XEN_ELFNOTE_SUPPORTED_FEATURES";
1247 default: return (note_type_unknown(nt));
1255 {"EXACT_MATCH", LL_EXACT_MATCH},
1256 {"IGNORE_INT_VER", LL_IGNORE_INT_VER},
1257 {"REQUIRE_MINOR", LL_REQUIRE_MINOR},
1258 {"EXPORTS", LL_EXPORTS},
1259 {"DELAY_LOAD", LL_DELAY_LOAD},
1260 {"DELTA", LL_DELTA},
1264 static struct mips_option mips_exceptions_option[] = {
1265 {OEX_PAGE0, "PAGE0"},
1267 {OEX_PRECISEFP, "PRECISEFP"},
1268 {OEX_DISMISS, "DISMISS"},
1272 static struct mips_option mips_pad_option[] = {
1273 {OPAD_PREFIX, "PREFIX"},
1274 {OPAD_POSTFIX, "POSTFIX"},
1275 {OPAD_SYMBOL, "SYMBOL"},
1279 static struct mips_option mips_hwpatch_option[] = {
1280 {OHW_R4KEOP, "R4KEOP"},
1281 {OHW_R8KPFETCH, "R8KPFETCH"},
1282 {OHW_R5KEOP, "R5KEOP"},
1283 {OHW_R5KCVTL, "R5KCVTL"},
1287 static struct mips_option mips_hwa_option[] = {
1288 {OHWA0_R4KEOP_CHECKED, "R4KEOP_CHECKED"},
1289 {OHWA0_R4KEOP_CLEAN, "R4KEOP_CLEAN"},
1293 static struct mips_option mips_hwo_option[] = {
1294 {OHWO0_FIXADE, "FIXADE"},
1299 option_kind(uint8_t kind)
1301 static char s_kind[32];
1304 case ODK_NULL: return "NULL";
1305 case ODK_REGINFO: return "REGINFO";
1306 case ODK_EXCEPTIONS: return "EXCEPTIONS";
1307 case ODK_PAD: return "PAD";
1308 case ODK_HWPATCH: return "HWPATCH";
1309 case ODK_FILL: return "FILL";
1310 case ODK_TAGS: return "TAGS";
1311 case ODK_HWAND: return "HWAND";
1312 case ODK_HWOR: return "HWOR";
1313 case ODK_GP_GROUP: return "GP_GROUP";
1314 case ODK_IDENT: return "IDENT";
1316 snprintf(s_kind, sizeof(s_kind), "<unknown: %u>", kind);
1322 top_tag(unsigned int tag)
1324 static char s_top_tag[32];
1327 case 1: return "File Attributes";
1328 case 2: return "Section Attributes";
1329 case 3: return "Symbol Attributes";
1331 snprintf(s_top_tag, sizeof(s_top_tag), "Unknown tag: %u", tag);
1337 aeabi_cpu_arch(uint64_t arch)
1339 static char s_cpu_arch[32];
1342 case 0: return "Pre-V4";
1343 case 1: return "ARM v4";
1344 case 2: return "ARM v4T";
1345 case 3: return "ARM v5T";
1346 case 4: return "ARM v5TE";
1347 case 5: return "ARM v5TEJ";
1348 case 6: return "ARM v6";
1349 case 7: return "ARM v6KZ";
1350 case 8: return "ARM v6T2";
1351 case 9: return "ARM v6K";
1352 case 10: return "ARM v7";
1353 case 11: return "ARM v6-M";
1354 case 12: return "ARM v6S-M";
1355 case 13: return "ARM v7E-M";
1357 snprintf(s_cpu_arch, sizeof(s_cpu_arch),
1358 "Unknown (%ju)", (uintmax_t) arch);
1359 return (s_cpu_arch);
1364 aeabi_cpu_arch_profile(uint64_t pf)
1366 static char s_arch_profile[32];
1370 return "Not applicable";
1371 case 0x41: /* 'A' */
1372 return "Application Profile";
1373 case 0x52: /* 'R' */
1374 return "Real-Time Profile";
1375 case 0x4D: /* 'M' */
1376 return "Microcontroller Profile";
1377 case 0x53: /* 'S' */
1378 return "Application or Real-Time Profile";
1380 snprintf(s_arch_profile, sizeof(s_arch_profile),
1381 "Unknown (%ju)\n", (uintmax_t) pf);
1382 return (s_arch_profile);
1387 aeabi_arm_isa(uint64_t ai)
1389 static char s_ai[32];
1392 case 0: return "No";
1393 case 1: return "Yes";
1395 snprintf(s_ai, sizeof(s_ai), "Unknown (%ju)\n",
1402 aeabi_thumb_isa(uint64_t ti)
1404 static char s_ti[32];
1407 case 0: return "No";
1408 case 1: return "16-bit Thumb";
1409 case 2: return "32-bit Thumb";
1411 snprintf(s_ti, sizeof(s_ti), "Unknown (%ju)\n",
1418 aeabi_fp_arch(uint64_t fp)
1420 static char s_fp_arch[32];
1423 case 0: return "No";
1424 case 1: return "VFPv1";
1425 case 2: return "VFPv2";
1426 case 3: return "VFPv3";
1427 case 4: return "VFPv3-D16";
1428 case 5: return "VFPv4";
1429 case 6: return "VFPv4-D16";
1431 snprintf(s_fp_arch, sizeof(s_fp_arch), "Unknown (%ju)",
1438 aeabi_wmmx_arch(uint64_t wmmx)
1440 static char s_wmmx[32];
1443 case 0: return "No";
1444 case 1: return "WMMXv1";
1445 case 2: return "WMMXv2";
1447 snprintf(s_wmmx, sizeof(s_wmmx), "Unknown (%ju)",
1454 aeabi_adv_simd_arch(uint64_t simd)
1456 static char s_simd[32];
1459 case 0: return "No";
1460 case 1: return "NEONv1";
1461 case 2: return "NEONv2";
1463 snprintf(s_simd, sizeof(s_simd), "Unknown (%ju)",
1470 aeabi_pcs_config(uint64_t pcs)
1472 static char s_pcs[32];
1475 case 0: return "None";
1476 case 1: return "Bare platform";
1477 case 2: return "Linux";
1478 case 3: return "Linux DSO";
1479 case 4: return "Palm OS 2004";
1480 case 5: return "Palm OS (future)";
1481 case 6: return "Symbian OS 2004";
1482 case 7: return "Symbian OS (future)";
1484 snprintf(s_pcs, sizeof(s_pcs), "Unknown (%ju)",
1491 aeabi_pcs_r9(uint64_t r9)
1493 static char s_r9[32];
1496 case 0: return "V6";
1497 case 1: return "SB";
1498 case 2: return "TLS pointer";
1499 case 3: return "Unused";
1501 snprintf(s_r9, sizeof(s_r9), "Unknown (%ju)", (uintmax_t) r9);
1507 aeabi_pcs_rw(uint64_t rw)
1509 static char s_rw[32];
1512 case 0: return "Absolute";
1513 case 1: return "PC-relative";
1514 case 2: return "SB-relative";
1515 case 3: return "None";
1517 snprintf(s_rw, sizeof(s_rw), "Unknown (%ju)", (uintmax_t) rw);
1523 aeabi_pcs_ro(uint64_t ro)
1525 static char s_ro[32];
1528 case 0: return "Absolute";
1529 case 1: return "PC-relative";
1530 case 2: return "None";
1532 snprintf(s_ro, sizeof(s_ro), "Unknown (%ju)", (uintmax_t) ro);
1538 aeabi_pcs_got(uint64_t got)
1540 static char s_got[32];
1543 case 0: return "None";
1544 case 1: return "direct";
1545 case 2: return "indirect via GOT";
1547 snprintf(s_got, sizeof(s_got), "Unknown (%ju)",
1554 aeabi_pcs_wchar_t(uint64_t wt)
1556 static char s_wt[32];
1559 case 0: return "None";
1560 case 2: return "wchar_t size 2";
1561 case 4: return "wchar_t size 4";
1563 snprintf(s_wt, sizeof(s_wt), "Unknown (%ju)", (uintmax_t) wt);
1569 aeabi_enum_size(uint64_t es)
1571 static char s_es[32];
1574 case 0: return "None";
1575 case 1: return "smallest";
1576 case 2: return "32-bit";
1577 case 3: return "visible 32-bit";
1579 snprintf(s_es, sizeof(s_es), "Unknown (%ju)", (uintmax_t) es);
1585 aeabi_align_needed(uint64_t an)
1587 static char s_align_n[64];
1590 case 0: return "No";
1591 case 1: return "8-byte align";
1592 case 2: return "4-byte align";
1593 case 3: return "Reserved";
1595 if (an >= 4 && an <= 12)
1596 snprintf(s_align_n, sizeof(s_align_n), "8-byte align"
1597 " and up to 2^%ju-byte extended align",
1600 snprintf(s_align_n, sizeof(s_align_n), "Unknown (%ju)",
1607 aeabi_align_preserved(uint64_t ap)
1609 static char s_align_p[128];
1612 case 0: return "No";
1613 case 1: return "8-byte align";
1614 case 2: return "8-byte align and SP % 8 == 0";
1615 case 3: return "Reserved";
1617 if (ap >= 4 && ap <= 12)
1618 snprintf(s_align_p, sizeof(s_align_p), "8-byte align"
1619 " and SP %% 8 == 0 and up to 2^%ju-byte extended"
1620 " align", (uintmax_t) ap);
1622 snprintf(s_align_p, sizeof(s_align_p), "Unknown (%ju)",
1629 aeabi_fp_rounding(uint64_t fr)
1631 static char s_fp_r[32];
1634 case 0: return "Unused";
1635 case 1: return "Needed";
1637 snprintf(s_fp_r, sizeof(s_fp_r), "Unknown (%ju)",
1644 aeabi_fp_denormal(uint64_t fd)
1646 static char s_fp_d[32];
1649 case 0: return "Unused";
1650 case 1: return "Needed";
1651 case 2: return "Sign Only";
1653 snprintf(s_fp_d, sizeof(s_fp_d), "Unknown (%ju)",
1660 aeabi_fp_exceptions(uint64_t fe)
1662 static char s_fp_e[32];
1665 case 0: return "Unused";
1666 case 1: return "Needed";
1668 snprintf(s_fp_e, sizeof(s_fp_e), "Unknown (%ju)",
1675 aeabi_fp_user_exceptions(uint64_t fu)
1677 static char s_fp_u[32];
1680 case 0: return "Unused";
1681 case 1: return "Needed";
1683 snprintf(s_fp_u, sizeof(s_fp_u), "Unknown (%ju)",
1690 aeabi_fp_number_model(uint64_t fn)
1692 static char s_fp_n[32];
1695 case 0: return "Unused";
1696 case 1: return "IEEE 754 normal";
1697 case 2: return "RTABI";
1698 case 3: return "IEEE 754";
1700 snprintf(s_fp_n, sizeof(s_fp_n), "Unknown (%ju)",
1707 aeabi_fp_16bit_format(uint64_t fp16)
1709 static char s_fp_16[64];
1712 case 0: return "None";
1713 case 1: return "IEEE 754";
1714 case 2: return "VFPv3/Advanced SIMD (alternative format)";
1716 snprintf(s_fp_16, sizeof(s_fp_16), "Unknown (%ju)",
1723 aeabi_mpext(uint64_t mp)
1725 static char s_mp[32];
1728 case 0: return "Not allowed";
1729 case 1: return "Allowed";
1731 snprintf(s_mp, sizeof(s_mp), "Unknown (%ju)",
1738 aeabi_div(uint64_t du)
1740 static char s_du[32];
1743 case 0: return "Yes (V7-R/V7-M)";
1744 case 1: return "No";
1745 case 2: return "Yes (V7-A)";
1747 snprintf(s_du, sizeof(s_du), "Unknown (%ju)",
1754 aeabi_t2ee(uint64_t t2ee)
1756 static char s_t2ee[32];
1759 case 0: return "Not allowed";
1760 case 1: return "Allowed";
1762 snprintf(s_t2ee, sizeof(s_t2ee), "Unknown(%ju)",
1770 aeabi_hardfp(uint64_t hfp)
1772 static char s_hfp[32];
1775 case 0: return "Tag_FP_arch";
1776 case 1: return "only SP";
1777 case 2: return "only DP";
1778 case 3: return "both SP and DP";
1780 snprintf(s_hfp, sizeof(s_hfp), "Unknown (%ju)",
1787 aeabi_vfp_args(uint64_t va)
1789 static char s_va[32];
1792 case 0: return "AAPCS (base variant)";
1793 case 1: return "AAPCS (VFP variant)";
1794 case 2: return "toolchain-specific";
1796 snprintf(s_va, sizeof(s_va), "Unknown (%ju)", (uintmax_t) va);
1802 aeabi_wmmx_args(uint64_t wa)
1804 static char s_wa[32];
1807 case 0: return "AAPCS (base variant)";
1808 case 1: return "Intel WMMX";
1809 case 2: return "toolchain-specific";
1811 snprintf(s_wa, sizeof(s_wa), "Unknown(%ju)", (uintmax_t) wa);
1817 aeabi_unaligned_access(uint64_t ua)
1819 static char s_ua[32];
1822 case 0: return "Not allowed";
1823 case 1: return "Allowed";
1825 snprintf(s_ua, sizeof(s_ua), "Unknown(%ju)", (uintmax_t) ua);
1831 aeabi_fp_hpext(uint64_t fh)
1833 static char s_fh[32];
1836 case 0: return "Not allowed";
1837 case 1: return "Allowed";
1839 snprintf(s_fh, sizeof(s_fh), "Unknown(%ju)", (uintmax_t) fh);
1845 aeabi_optm_goal(uint64_t og)
1847 static char s_og[32];
1850 case 0: return "None";
1851 case 1: return "Speed";
1852 case 2: return "Speed aggressive";
1853 case 3: return "Space";
1854 case 4: return "Space aggressive";
1855 case 5: return "Debugging";
1856 case 6: return "Best Debugging";
1858 snprintf(s_og, sizeof(s_og), "Unknown(%ju)", (uintmax_t) og);
1864 aeabi_fp_optm_goal(uint64_t fog)
1866 static char s_fog[32];
1869 case 0: return "None";
1870 case 1: return "Speed";
1871 case 2: return "Speed aggressive";
1872 case 3: return "Space";
1873 case 4: return "Space aggressive";
1874 case 5: return "Accurary";
1875 case 6: return "Best Accurary";
1877 snprintf(s_fog, sizeof(s_fog), "Unknown(%ju)",
1884 aeabi_virtual(uint64_t vt)
1886 static char s_virtual[64];
1889 case 0: return "No";
1890 case 1: return "TrustZone";
1891 case 2: return "Virtualization extension";
1892 case 3: return "TrustZone and virtualization extension";
1894 snprintf(s_virtual, sizeof(s_virtual), "Unknown(%ju)",
1903 const char *(*get_desc)(uint64_t val);
1905 {4, "Tag_CPU_raw_name", NULL},
1906 {5, "Tag_CPU_name", NULL},
1907 {6, "Tag_CPU_arch", aeabi_cpu_arch},
1908 {7, "Tag_CPU_arch_profile", aeabi_cpu_arch_profile},
1909 {8, "Tag_ARM_ISA_use", aeabi_arm_isa},
1910 {9, "Tag_THUMB_ISA_use", aeabi_thumb_isa},
1911 {10, "Tag_FP_arch", aeabi_fp_arch},
1912 {11, "Tag_WMMX_arch", aeabi_wmmx_arch},
1913 {12, "Tag_Advanced_SIMD_arch", aeabi_adv_simd_arch},
1914 {13, "Tag_PCS_config", aeabi_pcs_config},
1915 {14, "Tag_ABI_PCS_R9_use", aeabi_pcs_r9},
1916 {15, "Tag_ABI_PCS_RW_data", aeabi_pcs_rw},
1917 {16, "Tag_ABI_PCS_RO_data", aeabi_pcs_ro},
1918 {17, "Tag_ABI_PCS_GOT_use", aeabi_pcs_got},
1919 {18, "Tag_ABI_PCS_wchar_t", aeabi_pcs_wchar_t},
1920 {19, "Tag_ABI_FP_rounding", aeabi_fp_rounding},
1921 {20, "Tag_ABI_FP_denormal", aeabi_fp_denormal},
1922 {21, "Tag_ABI_FP_exceptions", aeabi_fp_exceptions},
1923 {22, "Tag_ABI_FP_user_exceptions", aeabi_fp_user_exceptions},
1924 {23, "Tag_ABI_FP_number_model", aeabi_fp_number_model},
1925 {24, "Tag_ABI_align_needed", aeabi_align_needed},
1926 {25, "Tag_ABI_align_preserved", aeabi_align_preserved},
1927 {26, "Tag_ABI_enum_size", aeabi_enum_size},
1928 {27, "Tag_ABI_HardFP_use", aeabi_hardfp},
1929 {28, "Tag_ABI_VFP_args", aeabi_vfp_args},
1930 {29, "Tag_ABI_WMMX_args", aeabi_wmmx_args},
1931 {30, "Tag_ABI_optimization_goals", aeabi_optm_goal},
1932 {31, "Tag_ABI_FP_optimization_goals", aeabi_fp_optm_goal},
1933 {32, "Tag_compatibility", NULL},
1934 {34, "Tag_CPU_unaligned_access", aeabi_unaligned_access},
1935 {36, "Tag_FP_HP_extension", aeabi_fp_hpext},
1936 {38, "Tag_ABI_FP_16bit_format", aeabi_fp_16bit_format},
1937 {42, "Tag_MPextension_use", aeabi_mpext},
1938 {44, "Tag_DIV_use", aeabi_div},
1939 {64, "Tag_nodefaults", NULL},
1940 {65, "Tag_also_compatible_with", NULL},
1941 {66, "Tag_T2EE_use", aeabi_t2ee},
1942 {67, "Tag_conformance", NULL},
1943 {68, "Tag_Virtualization_use", aeabi_virtual},
1944 {70, "Tag_MPextension_use", aeabi_mpext},
1948 mips_abi_fp(uint64_t fp)
1950 static char s_mips_abi_fp[64];
1953 case 0: return "N/A";
1954 case 1: return "Hard float (double precision)";
1955 case 2: return "Hard float (single precision)";
1956 case 3: return "Soft float";
1957 case 4: return "64-bit float (-mips32r2 -mfp64)";
1959 snprintf(s_mips_abi_fp, sizeof(s_mips_abi_fp), "Unknown(%ju)",
1961 return (s_mips_abi_fp);
1966 ppc_abi_fp(uint64_t fp)
1968 static char s_ppc_abi_fp[64];
1971 case 0: return "N/A";
1972 case 1: return "Hard float (double precision)";
1973 case 2: return "Soft float";
1974 case 3: return "Hard float (single precision)";
1976 snprintf(s_ppc_abi_fp, sizeof(s_ppc_abi_fp), "Unknown(%ju)",
1978 return (s_ppc_abi_fp);
1983 ppc_abi_vector(uint64_t vec)
1985 static char s_vec[64];
1988 case 0: return "N/A";
1989 case 1: return "Generic purpose registers";
1990 case 2: return "AltiVec registers";
1991 case 3: return "SPE registers";
1993 snprintf(s_vec, sizeof(s_vec), "Unknown(%ju)", (uintmax_t) vec);
1999 dwarf_reg(unsigned int mach, unsigned int reg)
2006 case 0: return "eax";
2007 case 1: return "ecx";
2008 case 2: return "edx";
2009 case 3: return "ebx";
2010 case 4: return "esp";
2011 case 5: return "ebp";
2012 case 6: return "esi";
2013 case 7: return "edi";
2014 case 8: return "eip";
2015 case 9: return "eflags";
2016 case 11: return "st0";
2017 case 12: return "st1";
2018 case 13: return "st2";
2019 case 14: return "st3";
2020 case 15: return "st4";
2021 case 16: return "st5";
2022 case 17: return "st6";
2023 case 18: return "st7";
2024 case 21: return "xmm0";
2025 case 22: return "xmm1";
2026 case 23: return "xmm2";
2027 case 24: return "xmm3";
2028 case 25: return "xmm4";
2029 case 26: return "xmm5";
2030 case 27: return "xmm6";
2031 case 28: return "xmm7";
2032 case 29: return "mm0";
2033 case 30: return "mm1";
2034 case 31: return "mm2";
2035 case 32: return "mm3";
2036 case 33: return "mm4";
2037 case 34: return "mm5";
2038 case 35: return "mm6";
2039 case 36: return "mm7";
2040 case 37: return "fcw";
2041 case 38: return "fsw";
2042 case 39: return "mxcsr";
2043 case 40: return "es";
2044 case 41: return "cs";
2045 case 42: return "ss";
2046 case 43: return "ds";
2047 case 44: return "fs";
2048 case 45: return "gs";
2049 case 48: return "tr";
2050 case 49: return "ldtr";
2051 default: return (NULL);
2055 case 0: return "rax";
2056 case 1: return "rdx";
2057 case 2: return "rcx";
2058 case 3: return "rbx";
2059 case 4: return "rsi";
2060 case 5: return "rdi";
2061 case 6: return "rbp";
2062 case 7: return "rsp";
2063 case 16: return "rip";
2064 case 17: return "xmm0";
2065 case 18: return "xmm1";
2066 case 19: return "xmm2";
2067 case 20: return "xmm3";
2068 case 21: return "xmm4";
2069 case 22: return "xmm5";
2070 case 23: return "xmm6";
2071 case 24: return "xmm7";
2072 case 25: return "xmm8";
2073 case 26: return "xmm9";
2074 case 27: return "xmm10";
2075 case 28: return "xmm11";
2076 case 29: return "xmm12";
2077 case 30: return "xmm13";
2078 case 31: return "xmm14";
2079 case 32: return "xmm15";
2080 case 33: return "st0";
2081 case 34: return "st1";
2082 case 35: return "st2";
2083 case 36: return "st3";
2084 case 37: return "st4";
2085 case 38: return "st5";
2086 case 39: return "st6";
2087 case 40: return "st7";
2088 case 41: return "mm0";
2089 case 42: return "mm1";
2090 case 43: return "mm2";
2091 case 44: return "mm3";
2092 case 45: return "mm4";
2093 case 46: return "mm5";
2094 case 47: return "mm6";
2095 case 48: return "mm7";
2096 case 49: return "rflags";
2097 case 50: return "es";
2098 case 51: return "cs";
2099 case 52: return "ss";
2100 case 53: return "ds";
2101 case 54: return "fs";
2102 case 55: return "gs";
2103 case 58: return "fs.base";
2104 case 59: return "gs.base";
2105 case 62: return "tr";
2106 case 63: return "ldtr";
2107 case 64: return "mxcsr";
2108 case 65: return "fcw";
2109 case 66: return "fsw";
2110 default: return (NULL);
2118 dump_ehdr(struct readelf *re)
2120 size_t phnum, shnum, shstrndx;
2123 printf("ELF Header:\n");
2127 for (i = 0; i < EI_NIDENT; i++)
2128 printf("%.2x ", re->ehdr.e_ident[i]);
2132 printf("%-37s%s\n", " Class:", elf_class(re->ehdr.e_ident[EI_CLASS]));
2135 printf("%-37s%s\n", " Data:", elf_endian(re->ehdr.e_ident[EI_DATA]));
2138 printf("%-37s%d %s\n", " Version:", re->ehdr.e_ident[EI_VERSION],
2139 elf_ver(re->ehdr.e_ident[EI_VERSION]));
2142 printf("%-37s%s\n", " OS/ABI:", elf_osabi(re->ehdr.e_ident[EI_OSABI]));
2144 /* EI_ABIVERSION. */
2145 printf("%-37s%d\n", " ABI Version:", re->ehdr.e_ident[EI_ABIVERSION]);
2148 printf("%-37s%s\n", " Type:", elf_type(re->ehdr.e_type));
2151 printf("%-37s%s\n", " Machine:", elf_machine(re->ehdr.e_machine));
2154 printf("%-37s%#x\n", " Version:", re->ehdr.e_version);
2157 printf("%-37s%#jx\n", " Entry point address:",
2158 (uintmax_t)re->ehdr.e_entry);
2161 printf("%-37s%ju (bytes into file)\n", " Start of program headers:",
2162 (uintmax_t)re->ehdr.e_phoff);
2165 printf("%-37s%ju (bytes into file)\n", " Start of section headers:",
2166 (uintmax_t)re->ehdr.e_shoff);
2169 printf("%-37s%#x", " Flags:", re->ehdr.e_flags);
2170 dump_eflags(re, re->ehdr.e_flags);
2174 printf("%-37s%u (bytes)\n", " Size of this header:",
2178 printf("%-37s%u (bytes)\n", " Size of program headers:",
2179 re->ehdr.e_phentsize);
2182 printf("%-37s%u", " Number of program headers:", re->ehdr.e_phnum);
2183 if (re->ehdr.e_phnum == PN_XNUM) {
2184 /* Extended program header numbering is in use. */
2185 if (elf_getphnum(re->elf, &phnum))
2186 printf(" (%zu)", phnum);
2191 printf("%-37s%u (bytes)\n", " Size of section headers:",
2192 re->ehdr.e_shentsize);
2195 printf("%-37s%u", " Number of section headers:", re->ehdr.e_shnum);
2196 if (re->ehdr.e_shnum == SHN_UNDEF) {
2197 /* Extended section numbering is in use. */
2198 if (elf_getshnum(re->elf, &shnum))
2199 printf(" (%ju)", (uintmax_t)shnum);
2204 printf("%-37s%u", " Section header string table index:",
2205 re->ehdr.e_shstrndx);
2206 if (re->ehdr.e_shstrndx == SHN_XINDEX) {
2207 /* Extended section numbering is in use. */
2208 if (elf_getshstrndx(re->elf, &shstrndx))
2209 printf(" (%ju)", (uintmax_t)shstrndx);
2215 dump_eflags(struct readelf *re, uint64_t e_flags)
2217 struct eflags_desc *edesc;
2221 switch (re->ehdr.e_machine) {
2223 arm_eabi = (e_flags & EF_ARM_EABIMASK) >> 24;
2225 printf(", GNU EABI");
2226 else if (arm_eabi <= 5)
2227 printf(", Version%d EABI", arm_eabi);
2228 edesc = arm_eflags_desc;
2231 case EM_MIPS_RS3_LE:
2232 switch ((e_flags & EF_MIPS_ARCH) >> 28) {
2233 case 0: printf(", mips1"); break;
2234 case 1: printf(", mips2"); break;
2235 case 2: printf(", mips3"); break;
2236 case 3: printf(", mips4"); break;
2237 case 4: printf(", mips5"); break;
2238 case 5: printf(", mips32"); break;
2239 case 6: printf(", mips64"); break;
2240 case 7: printf(", mips32r2"); break;
2241 case 8: printf(", mips64r2"); break;
2244 switch ((e_flags & 0x00FF0000) >> 16) {
2245 case 0x81: printf(", 3900"); break;
2246 case 0x82: printf(", 4010"); break;
2247 case 0x83: printf(", 4100"); break;
2248 case 0x85: printf(", 4650"); break;
2249 case 0x87: printf(", 4120"); break;
2250 case 0x88: printf(", 4111"); break;
2251 case 0x8a: printf(", sb1"); break;
2252 case 0x8b: printf(", octeon"); break;
2253 case 0x8c: printf(", xlr"); break;
2254 case 0x91: printf(", 5400"); break;
2255 case 0x98: printf(", 5500"); break;
2256 case 0x99: printf(", 9000"); break;
2257 case 0xa0: printf(", loongson-2e"); break;
2258 case 0xa1: printf(", loongson-2f"); break;
2261 switch ((e_flags & 0x0000F000) >> 12) {
2262 case 1: printf(", o32"); break;
2263 case 2: printf(", o64"); break;
2264 case 3: printf(", eabi32"); break;
2265 case 4: printf(", eabi64"); break;
2268 edesc = mips_eflags_desc;
2272 edesc = powerpc_eflags_desc;
2275 case EM_SPARC32PLUS:
2277 switch ((e_flags & EF_SPARCV9_MM)) {
2278 case EF_SPARCV9_TSO: printf(", tso"); break;
2279 case EF_SPARCV9_PSO: printf(", pso"); break;
2280 case EF_SPARCV9_MM: printf(", rmo"); break;
2283 edesc = sparc_eflags_desc;
2289 if (edesc != NULL) {
2290 while (edesc->desc != NULL) {
2291 if (e_flags & edesc->flag)
2292 printf(", %s", edesc->desc);
2299 dump_phdr(struct readelf *re)
2301 const char *rawfile;
2306 #define PH_HDR "Type", "Offset", "VirtAddr", "PhysAddr", "FileSiz", \
2307 "MemSiz", "Flg", "Align"
2308 #define PH_CT phdr_type(re->ehdr.e_machine, phdr.p_type), \
2309 (uintmax_t)phdr.p_offset, (uintmax_t)phdr.p_vaddr, \
2310 (uintmax_t)phdr.p_paddr, (uintmax_t)phdr.p_filesz, \
2311 (uintmax_t)phdr.p_memsz, \
2312 phdr.p_flags & PF_R ? 'R' : ' ', \
2313 phdr.p_flags & PF_W ? 'W' : ' ', \
2314 phdr.p_flags & PF_X ? 'E' : ' ', \
2315 (uintmax_t)phdr.p_align
2317 if (elf_getphnum(re->elf, &phnum) == 0) {
2318 warnx("elf_getphnum failed: %s", elf_errmsg(-1));
2322 printf("\nThere are no program headers in this file.\n");
2326 printf("\nElf file type is %s", elf_type(re->ehdr.e_type));
2327 printf("\nEntry point 0x%jx\n", (uintmax_t)re->ehdr.e_entry);
2328 printf("There are %ju program headers, starting at offset %ju\n",
2329 (uintmax_t)phnum, (uintmax_t)re->ehdr.e_phoff);
2331 /* Dump program headers. */
2332 printf("\nProgram Headers:\n");
2333 if (re->ec == ELFCLASS32)
2334 printf(" %-15s%-9s%-11s%-11s%-8s%-8s%-4s%s\n", PH_HDR);
2335 else if (re->options & RE_WW)
2336 printf(" %-15s%-9s%-19s%-19s%-9s%-9s%-4s%s\n", PH_HDR);
2338 printf(" %-15s%-19s%-19s%s\n %-19s%-20s"
2339 "%-7s%s\n", PH_HDR);
2340 for (i = 0; (size_t) i < phnum; i++) {
2341 if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
2342 warnx("gelf_getphdr failed: %s", elf_errmsg(-1));
2345 /* TODO: Add arch-specific segment type dump. */
2346 if (re->ec == ELFCLASS32)
2347 printf(" %-14.14s 0x%6.6jx 0x%8.8jx 0x%8.8jx "
2348 "0x%5.5jx 0x%5.5jx %c%c%c %#jx\n", PH_CT);
2349 else if (re->options & RE_WW)
2350 printf(" %-14.14s 0x%6.6jx 0x%16.16jx 0x%16.16jx "
2351 "0x%6.6jx 0x%6.6jx %c%c%c %#jx\n", PH_CT);
2353 printf(" %-14.14s 0x%16.16jx 0x%16.16jx 0x%16.16jx\n"
2354 " 0x%16.16jx 0x%16.16jx %c%c%c"
2356 if (phdr.p_type == PT_INTERP) {
2357 if ((rawfile = elf_rawfile(re->elf, &size)) == NULL) {
2358 warnx("elf_rawfile failed: %s", elf_errmsg(-1));
2361 if (phdr.p_offset >= size) {
2362 warnx("invalid program header offset");
2365 printf(" [Requesting program interpreter: %s]\n",
2366 rawfile + phdr.p_offset);
2370 /* Dump section to segment mapping. */
2373 printf("\n Section to Segment mapping:\n");
2374 printf(" Segment Sections...\n");
2375 for (i = 0; (size_t)i < phnum; i++) {
2376 if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
2377 warnx("gelf_getphdr failed: %s", elf_errmsg(-1));
2380 printf(" %2.2d ", i);
2381 /* skip NULL section. */
2382 for (j = 1; (size_t)j < re->shnum; j++) {
2383 if (re->sl[j].off < phdr.p_offset)
2385 if (re->sl[j].off + re->sl[j].sz >
2386 phdr.p_offset + phdr.p_filesz &&
2387 re->sl[j].type != SHT_NOBITS)
2389 if (re->sl[j].addr < phdr.p_vaddr ||
2390 re->sl[j].addr + re->sl[j].sz >
2391 phdr.p_vaddr + phdr.p_memsz)
2393 if (phdr.p_type == PT_TLS &&
2394 (re->sl[j].flags & SHF_TLS) == 0)
2396 printf("%s ", re->sl[j].name);
2405 section_flags(struct readelf *re, struct section *s)
2408 static char buf[BUF_SZ];
2412 nb = re->ec == ELFCLASS32 ? 8 : 16;
2413 if (re->options & RE_T) {
2414 snprintf(buf, BUF_SZ, "[%*.*jx]: ", nb, nb,
2415 (uintmax_t)s->flags);
2418 for (i = 0; section_flag[i].ln != NULL; i++) {
2419 if ((s->flags & section_flag[i].value) == 0)
2421 if (re->options & RE_T) {
2422 snprintf(&buf[p], BUF_SZ - p, "%s, ",
2423 section_flag[i].ln);
2424 p += strlen(section_flag[i].ln) + 2;
2426 buf[p++] = section_flag[i].sn;
2428 if (re->options & RE_T && p > nb + 4)
2436 dump_shdr(struct readelf *re)
2441 #define S_HDR "[Nr] Name", "Type", "Addr", "Off", "Size", "ES", \
2442 "Flg", "Lk", "Inf", "Al"
2443 #define S_HDRL "[Nr] Name", "Type", "Address", "Offset", "Size", \
2444 "EntSize", "Flags", "Link", "Info", "Align"
2445 #define ST_HDR "[Nr] Name", "Type", "Addr", "Off", "Size", "ES", \
2446 "Lk", "Inf", "Al", "Flags"
2447 #define ST_HDRL "[Nr] Name", "Type", "Address", "Offset", "Link", \
2448 "Size", "EntSize", "Info", "Align", "Flags"
2449 #define S_CT i, s->name, section_type(re->ehdr.e_machine, s->type), \
2450 (uintmax_t)s->addr, (uintmax_t)s->off, (uintmax_t)s->sz,\
2451 (uintmax_t)s->entsize, section_flags(re, s), \
2452 s->link, s->info, (uintmax_t)s->align
2453 #define ST_CT i, s->name, section_type(re->ehdr.e_machine, s->type), \
2454 (uintmax_t)s->addr, (uintmax_t)s->off, (uintmax_t)s->sz,\
2455 (uintmax_t)s->entsize, s->link, s->info, \
2456 (uintmax_t)s->align, section_flags(re, s)
2457 #define ST_CTL i, s->name, section_type(re->ehdr.e_machine, s->type), \
2458 (uintmax_t)s->addr, (uintmax_t)s->off, s->link, \
2459 (uintmax_t)s->sz, (uintmax_t)s->entsize, s->info, \
2460 (uintmax_t)s->align, section_flags(re, s)
2462 if (re->shnum == 0) {
2463 printf("\nThere are no sections in this file.\n");
2466 printf("There are %ju section headers, starting at offset 0x%jx:\n",
2467 (uintmax_t)re->shnum, (uintmax_t)re->ehdr.e_shoff);
2468 printf("\nSection Headers:\n");
2469 if (re->ec == ELFCLASS32) {
2470 if (re->options & RE_T)
2471 printf(" %s\n %-16s%-9s%-7s%-7s%-5s%-3s%-4s%s\n"
2474 printf(" %-23s%-16s%-9s%-7s%-7s%-3s%-4s%-3s%-4s%s\n",
2476 } else if (re->options & RE_WW) {
2477 if (re->options & RE_T)
2478 printf(" %s\n %-16s%-17s%-7s%-7s%-5s%-3s%-4s%s\n"
2481 printf(" %-23s%-16s%-17s%-7s%-7s%-3s%-4s%-3s%-4s%s\n",
2484 if (re->options & RE_T)
2485 printf(" %s\n %-18s%-17s%-18s%s\n %-18s"
2486 "%-17s%-18s%s\n%12s\n", ST_HDRL);
2488 printf(" %-23s%-17s%-18s%s\n %-18s%-17s%-7s%"
2489 "-6s%-6s%s\n", S_HDRL);
2491 for (i = 0; (size_t)i < re->shnum; i++) {
2493 if (re->ec == ELFCLASS32) {
2494 if (re->options & RE_T)
2495 printf(" [%2d] %s\n %-15.15s %8.8jx"
2496 " %6.6jx %6.6jx %2.2jx %2u %3u %2ju\n"
2499 printf(" [%2d] %-17.17s %-15.15s %8.8jx"
2500 " %6.6jx %6.6jx %2.2jx %3s %2u %3u %2ju\n",
2502 } else if (re->options & RE_WW) {
2503 if (re->options & RE_T)
2504 printf(" [%2d] %s\n %-15.15s %16.16jx"
2505 " %6.6jx %6.6jx %2.2jx %2u %3u %2ju\n"
2508 printf(" [%2d] %-17.17s %-15.15s %16.16jx"
2509 " %6.6jx %6.6jx %2.2jx %3s %2u %3u %2ju\n",
2512 if (re->options & RE_T)
2513 printf(" [%2d] %s\n %-15.15s %16.16jx"
2514 " %16.16jx %u\n %16.16jx %16.16jx"
2515 " %-16u %ju\n %s\n", ST_CTL);
2517 printf(" [%2d] %-17.17s %-15.15s %16.16jx"
2518 " %8.8jx\n %16.16jx %16.16jx "
2519 "%3s %2u %3u %ju\n", S_CT);
2522 if ((re->options & RE_T) == 0)
2523 printf("Key to Flags:\n W (write), A (alloc),"
2524 " X (execute), M (merge), S (strings)\n"
2525 " I (info), L (link order), G (group), x (unknown)\n"
2526 " O (extra OS processing required)"
2527 " o (OS specific), p (processor specific)\n");
2539 * Return number of entries in the given section. We'd prefer ent_count be a
2540 * size_t *, but libelf APIs already use int for section indices.
2543 get_ent_count(struct section *s, int *ent_count)
2545 if (s->entsize == 0) {
2546 warnx("section %s has entry size 0", s->name);
2548 } else if (s->sz / s->entsize > INT_MAX) {
2549 warnx("section %s has invalid section count", s->name);
2552 *ent_count = (int)(s->sz / s->entsize);
2557 dump_dynamic(struct readelf *re)
2562 int elferr, i, is_dynamic, j, jmax, nentries;
2566 for (i = 0; (size_t)i < re->shnum; i++) {
2568 if (s->type != SHT_DYNAMIC)
2571 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
2572 elferr = elf_errno();
2574 warnx("elf_getdata failed: %s", elf_errmsg(-1));
2582 /* Determine the actual number of table entries. */
2584 if (!get_ent_count(s, &jmax))
2586 for (j = 0; j < jmax; j++) {
2587 if (gelf_getdyn(d, j, &dyn) != &dyn) {
2588 warnx("gelf_getdyn failed: %s",
2593 if (dyn.d_tag == DT_NULL)
2597 printf("\nDynamic section at offset 0x%jx", (uintmax_t)s->off);
2598 printf(" contains %u entries:\n", nentries);
2600 if (re->ec == ELFCLASS32)
2601 printf("%5s%12s%28s\n", "Tag", "Type", "Name/Value");
2603 printf("%5s%20s%28s\n", "Tag", "Type", "Name/Value");
2605 for (j = 0; j < nentries; j++) {
2606 if (gelf_getdyn(d, j, &dyn) != &dyn)
2608 /* Dump dynamic entry type. */
2609 if (re->ec == ELFCLASS32)
2610 printf(" 0x%8.8jx", (uintmax_t)dyn.d_tag);
2612 printf(" 0x%16.16jx", (uintmax_t)dyn.d_tag);
2613 printf(" %-20s", dt_type(re->ehdr.e_machine,
2615 /* Dump dynamic entry value. */
2616 dump_dyn_val(re, &dyn, s->link);
2621 printf("\nThere is no dynamic section in this file.\n");
2625 timestamp(time_t ti)
2631 snprintf(ts, sizeof(ts), "%04d-%02d-%02dT%02d:%02d:%02d",
2632 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour,
2633 t->tm_min, t->tm_sec);
2639 dyn_str(struct readelf *re, uint32_t stab, uint64_t d_val)
2643 if (stab == SHN_UNDEF)
2645 else if ((name = elf_strptr(re->elf, stab, d_val)) == NULL) {
2646 (void) elf_errno(); /* clear error */
2654 dump_arch_dyn_val(struct readelf *re, GElf_Dyn *dyn)
2656 switch (re->ehdr.e_machine) {
2658 case EM_MIPS_RS3_LE:
2659 switch (dyn->d_tag) {
2660 case DT_MIPS_RLD_VERSION:
2661 case DT_MIPS_LOCAL_GOTNO:
2662 case DT_MIPS_CONFLICTNO:
2663 case DT_MIPS_LIBLISTNO:
2664 case DT_MIPS_SYMTABNO:
2665 case DT_MIPS_UNREFEXTNO:
2666 case DT_MIPS_GOTSYM:
2667 case DT_MIPS_HIPAGENO:
2668 case DT_MIPS_DELTA_CLASS_NO:
2669 case DT_MIPS_DELTA_INSTANCE_NO:
2670 case DT_MIPS_DELTA_RELOC_NO:
2671 case DT_MIPS_DELTA_SYM_NO:
2672 case DT_MIPS_DELTA_CLASSSYM_NO:
2673 case DT_MIPS_LOCALPAGE_GOTIDX:
2674 case DT_MIPS_LOCAL_GOTIDX:
2675 case DT_MIPS_HIDDEN_GOTIDX:
2676 case DT_MIPS_PROTECTED_GOTIDX:
2677 printf(" %ju\n", (uintmax_t) dyn->d_un.d_val);
2679 case DT_MIPS_ICHECKSUM:
2681 case DT_MIPS_BASE_ADDRESS:
2682 case DT_MIPS_CONFLICT:
2683 case DT_MIPS_LIBLIST:
2684 case DT_MIPS_RLD_MAP:
2685 case DT_MIPS_DELTA_CLASS:
2686 case DT_MIPS_DELTA_INSTANCE:
2687 case DT_MIPS_DELTA_RELOC:
2688 case DT_MIPS_DELTA_SYM:
2689 case DT_MIPS_DELTA_CLASSSYM:
2690 case DT_MIPS_CXX_FLAGS:
2691 case DT_MIPS_PIXIE_INIT:
2692 case DT_MIPS_SYMBOL_LIB:
2693 case DT_MIPS_OPTIONS:
2694 case DT_MIPS_INTERFACE:
2695 case DT_MIPS_DYNSTR_ALIGN:
2696 case DT_MIPS_INTERFACE_SIZE:
2697 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
2698 case DT_MIPS_COMPACT_SIZE:
2699 case DT_MIPS_GP_VALUE:
2700 case DT_MIPS_AUX_DYNAMIC:
2701 case DT_MIPS_PLTGOT:
2702 case DT_MIPS_RLD_OBJ_UPDATE:
2704 printf(" 0x%jx\n", (uintmax_t) dyn->d_un.d_val);
2706 case DT_MIPS_IVERSION:
2707 case DT_MIPS_PERF_SUFFIX:
2708 case DT_MIPS_TIME_STAMP:
2709 printf(" %s\n", timestamp(dyn->d_un.d_val));
2723 dump_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab)
2727 if (dyn->d_tag >= DT_LOPROC && dyn->d_tag <= DT_HIPROC &&
2728 dyn->d_tag != DT_AUXILIARY && dyn->d_tag != DT_FILTER) {
2729 dump_arch_dyn_val(re, dyn);
2733 /* These entry values are index into the string table. */
2735 if (dyn->d_tag == DT_AUXILIARY || dyn->d_tag == DT_FILTER ||
2736 dyn->d_tag == DT_NEEDED || dyn->d_tag == DT_SONAME ||
2737 dyn->d_tag == DT_RPATH || dyn->d_tag == DT_RUNPATH)
2738 name = dyn_str(re, stab, dyn->d_un.d_val);
2740 switch(dyn->d_tag) {
2758 case DT_GNU_LIBLIST:
2759 case DT_GNU_CONFLICT:
2760 printf(" 0x%jx\n", (uintmax_t) dyn->d_un.d_val);
2769 case DT_PREINIT_ARRAYSZ:
2770 case DT_INIT_ARRAYSZ:
2771 case DT_FINI_ARRAYSZ:
2772 case DT_GNU_CONFLICTSZ:
2773 case DT_GNU_LIBLISTSZ:
2774 printf(" %ju (bytes)\n", (uintmax_t) dyn->d_un.d_val);
2780 printf(" %ju\n", (uintmax_t) dyn->d_un.d_val);
2783 printf(" Auxiliary library: [%s]\n", name);
2786 printf(" Filter library: [%s]\n", name);
2789 printf(" Shared library: [%s]\n", name);
2792 printf(" Library soname: [%s]\n", name);
2795 printf(" Library rpath: [%s]\n", name);
2798 printf(" Library runpath: [%s]\n", name);
2801 printf(" %s\n", dt_type(re->ehdr.e_machine, dyn->d_un.d_val));
2803 case DT_GNU_PRELINKED:
2804 printf(" %s\n", timestamp(dyn->d_un.d_val));
2812 dump_rel(struct readelf *re, struct section *s, Elf_Data *d)
2815 const char *symname;
2819 uint8_t type2, type3;
2821 if (s->link >= re->shnum)
2824 #define REL_HDR "r_offset", "r_info", "r_type", "st_value", "st_name"
2825 #define REL_CT32 (uintmax_t)r.r_offset, (uintmax_t)r.r_info, \
2826 elftc_reloc_type_str(re->ehdr.e_machine, \
2827 ELF32_R_TYPE(r.r_info)), (uintmax_t)symval, symname
2828 #define REL_CT64 (uintmax_t)r.r_offset, (uintmax_t)r.r_info, \
2829 elftc_reloc_type_str(re->ehdr.e_machine, type), \
2830 (uintmax_t)symval, symname
2832 printf("\nRelocation section (%s):\n", s->name);
2833 if (re->ec == ELFCLASS32)
2834 printf("%-8s %-8s %-19s %-8s %s\n", REL_HDR);
2836 if (re->options & RE_WW)
2837 printf("%-16s %-16s %-24s %-16s %s\n", REL_HDR);
2839 printf("%-12s %-12s %-19s %-16s %s\n", REL_HDR);
2841 assert(d->d_size == s->sz);
2842 if (!get_ent_count(s, &len))
2844 for (i = 0; i < len; i++) {
2845 if (gelf_getrel(d, i, &r) != &r) {
2846 warnx("gelf_getrel failed: %s", elf_errmsg(-1));
2849 symname = get_symbol_name(re, s->link, GELF_R_SYM(r.r_info));
2850 symval = get_symbol_value(re, s->link, GELF_R_SYM(r.r_info));
2851 if (re->ec == ELFCLASS32) {
2852 r.r_info = ELF32_R_INFO(ELF64_R_SYM(r.r_info),
2853 ELF64_R_TYPE(r.r_info));
2854 printf("%8.8jx %8.8jx %-19.19s %8.8jx %s\n", REL_CT32);
2856 type = ELF64_R_TYPE(r.r_info);
2857 if (re->ehdr.e_machine == EM_MIPS) {
2858 type2 = (type >> 8) & 0xFF;
2859 type3 = (type >> 16) & 0xFF;
2864 if (re->options & RE_WW)
2865 printf("%16.16jx %16.16jx %-24.24s"
2866 " %16.16jx %s\n", REL_CT64);
2868 printf("%12.12jx %12.12jx %-19.19s"
2869 " %16.16jx %s\n", REL_CT64);
2870 if (re->ehdr.e_machine == EM_MIPS) {
2871 if (re->options & RE_WW) {
2872 printf("%32s: %s\n", "Type2",
2873 elftc_reloc_type_str(EM_MIPS,
2875 printf("%32s: %s\n", "Type3",
2876 elftc_reloc_type_str(EM_MIPS,
2879 printf("%24s: %s\n", "Type2",
2880 elftc_reloc_type_str(EM_MIPS,
2882 printf("%24s: %s\n", "Type3",
2883 elftc_reloc_type_str(EM_MIPS,
2895 dump_rela(struct readelf *re, struct section *s, Elf_Data *d)
2898 const char *symname;
2902 uint8_t type2, type3;
2904 if (s->link >= re->shnum)
2907 #define RELA_HDR "r_offset", "r_info", "r_type", "st_value", \
2908 "st_name + r_addend"
2909 #define RELA_CT32 (uintmax_t)r.r_offset, (uintmax_t)r.r_info, \
2910 elftc_reloc_type_str(re->ehdr.e_machine, \
2911 ELF32_R_TYPE(r.r_info)), (uintmax_t)symval, symname
2912 #define RELA_CT64 (uintmax_t)r.r_offset, (uintmax_t)r.r_info, \
2913 elftc_reloc_type_str(re->ehdr.e_machine, type), \
2914 (uintmax_t)symval, symname
2916 printf("\nRelocation section with addend (%s):\n", s->name);
2917 if (re->ec == ELFCLASS32)
2918 printf("%-8s %-8s %-19s %-8s %s\n", RELA_HDR);
2920 if (re->options & RE_WW)
2921 printf("%-16s %-16s %-24s %-16s %s\n", RELA_HDR);
2923 printf("%-12s %-12s %-19s %-16s %s\n", RELA_HDR);
2925 assert(d->d_size == s->sz);
2926 if (!get_ent_count(s, &len))
2928 for (i = 0; i < len; i++) {
2929 if (gelf_getrela(d, i, &r) != &r) {
2930 warnx("gelf_getrel failed: %s", elf_errmsg(-1));
2933 symname = get_symbol_name(re, s->link, GELF_R_SYM(r.r_info));
2934 symval = get_symbol_value(re, s->link, GELF_R_SYM(r.r_info));
2935 if (re->ec == ELFCLASS32) {
2936 r.r_info = ELF32_R_INFO(ELF64_R_SYM(r.r_info),
2937 ELF64_R_TYPE(r.r_info));
2938 printf("%8.8jx %8.8jx %-19.19s %8.8jx %s", RELA_CT32);
2939 printf(" + %x\n", (uint32_t) r.r_addend);
2941 type = ELF64_R_TYPE(r.r_info);
2942 if (re->ehdr.e_machine == EM_MIPS) {
2943 type2 = (type >> 8) & 0xFF;
2944 type3 = (type >> 16) & 0xFF;
2949 if (re->options & RE_WW)
2950 printf("%16.16jx %16.16jx %-24.24s"
2951 " %16.16jx %s", RELA_CT64);
2953 printf("%12.12jx %12.12jx %-19.19s"
2954 " %16.16jx %s", RELA_CT64);
2955 printf(" + %jx\n", (uintmax_t) r.r_addend);
2956 if (re->ehdr.e_machine == EM_MIPS) {
2957 if (re->options & RE_WW) {
2958 printf("%32s: %s\n", "Type2",
2959 elftc_reloc_type_str(EM_MIPS,
2961 printf("%32s: %s\n", "Type3",
2962 elftc_reloc_type_str(EM_MIPS,
2965 printf("%24s: %s\n", "Type2",
2966 elftc_reloc_type_str(EM_MIPS,
2968 printf("%24s: %s\n", "Type3",
2969 elftc_reloc_type_str(EM_MIPS,
2981 dump_reloc(struct readelf *re)
2987 for (i = 0; (size_t)i < re->shnum; i++) {
2989 if (s->type == SHT_REL || s->type == SHT_RELA) {
2991 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
2992 elferr = elf_errno();
2994 warnx("elf_getdata failed: %s",
2995 elf_errmsg(elferr));
2998 if (s->type == SHT_REL)
3001 dump_rela(re, s, d);
3007 dump_symtab(struct readelf *re, int i)
3018 if (s->link >= re->shnum)
3022 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3023 elferr = elf_errno();
3025 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
3030 if (!get_ent_count(s, &len))
3032 printf("Symbol table (%s)", s->name);
3033 printf(" contains %d entries:\n", len);
3034 printf("%7s%9s%14s%5s%8s%6s%9s%5s\n", "Num:", "Value", "Size", "Type",
3035 "Bind", "Vis", "Ndx", "Name");
3037 for (j = 0; j < len; j++) {
3038 if (gelf_getsym(d, j, &sym) != &sym) {
3039 warnx("gelf_getsym failed: %s", elf_errmsg(-1));
3043 printf(" %16.16jx", (uintmax_t) sym.st_value);
3044 printf(" %5ju", (uintmax_t) sym.st_size);
3045 printf(" %-7s", st_type(re->ehdr.e_machine,
3046 re->ehdr.e_ident[EI_OSABI], GELF_ST_TYPE(sym.st_info)));
3047 printf(" %-6s", st_bind(GELF_ST_BIND(sym.st_info)));
3048 printf(" %-8s", st_vis(GELF_ST_VISIBILITY(sym.st_other)));
3049 printf(" %3s", st_shndx(sym.st_shndx));
3050 if ((name = elf_strptr(re->elf, stab, sym.st_name)) != NULL)
3051 printf(" %s", name);
3052 /* Append symbol version string for SHT_DYNSYM symbol table. */
3053 if (s->type == SHT_DYNSYM && re->ver != NULL &&
3054 re->vs != NULL && re->vs[j] > 1) {
3055 vs = re->vs[j] & VERSYM_VERSION;
3056 if (vs >= re->ver_sz || re->ver[vs].name == NULL) {
3057 warnx("invalid versym version index %u", vs);
3060 if (re->vs[j] & VERSYM_HIDDEN || re->ver[vs].type == 0)
3061 printf("@%s (%d)", re->ver[vs].name, vs);
3063 printf("@@%s (%d)", re->ver[vs].name, vs);
3071 dump_symtabs(struct readelf *re)
3080 * If -D is specified, only dump the symbol table specified by
3081 * the DT_SYMTAB entry in the .dynamic section.
3084 if (re->options & RE_DD) {
3086 for (i = 0; (size_t)i < re->shnum; i++)
3087 if (re->sl[i].type == SHT_DYNAMIC) {
3094 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3095 elferr = elf_errno();
3097 warnx("elf_getdata failed: %s", elf_errmsg(-1));
3102 if (!get_ent_count(s, &len))
3105 for (i = 0; i < len; i++) {
3106 if (gelf_getdyn(d, i, &dyn) != &dyn) {
3107 warnx("gelf_getdyn failed: %s", elf_errmsg(-1));
3110 if (dyn.d_tag == DT_SYMTAB) {
3111 dyn_off = dyn.d_un.d_val;
3117 /* Find and dump symbol tables. */
3118 for (i = 0; (size_t)i < re->shnum; i++) {
3120 if (s->type == SHT_SYMTAB || s->type == SHT_DYNSYM) {
3121 if (re->options & RE_DD) {
3122 if (dyn_off == s->addr) {
3133 dump_svr4_hash(struct section *s)
3137 uint32_t nbucket, nchain;
3138 uint32_t *bucket, *chain;
3139 uint32_t *bl, *c, maxl, total;
3142 /* Read and parse the content of .hash section. */
3144 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3145 elferr = elf_errno();
3147 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
3150 if (d->d_size < 2 * sizeof(uint32_t)) {
3151 warnx(".hash section too small");
3157 if (nbucket <= 0 || nchain <= 0) {
3158 warnx("Malformed .hash section");
3161 if (d->d_size != (nbucket + nchain + 2) * sizeof(uint32_t)) {
3162 warnx("Malformed .hash section");
3166 chain = &buf[2 + nbucket];
3169 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
3170 errx(EXIT_FAILURE, "calloc failed");
3171 for (i = 0; (uint32_t)i < nbucket; i++)
3172 for (j = bucket[i]; j > 0 && (uint32_t)j < nchain; j = chain[j])
3175 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
3176 errx(EXIT_FAILURE, "calloc failed");
3177 for (i = 0; (uint32_t)i < nbucket; i++)
3179 printf("\nHistogram for bucket list length (total of %u buckets):\n",
3181 printf(" Length\tNumber\t\t%% of total\tCoverage\n");
3183 for (i = 0; (uint32_t)i <= maxl; i++) {
3185 printf("%7u\t%-10u\t(%5.1f%%)\t%5.1f%%\n", i, c[i],
3186 c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
3193 dump_svr4_hash64(struct readelf *re, struct section *s)
3197 uint64_t nbucket, nchain;
3198 uint64_t *bucket, *chain;
3199 uint64_t *bl, *c, maxl, total;
3203 * ALPHA uses 64-bit hash entries. Since libelf assumes that
3204 * .hash section contains only 32-bit entry, an explicit
3205 * gelf_xlatetom is needed here.
3208 if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
3209 elferr = elf_errno();
3211 warnx("elf_rawdata failed: %s",
3212 elf_errmsg(elferr));
3215 d->d_type = ELF_T_XWORD;
3216 memcpy(&dst, d, sizeof(Elf_Data));
3217 if (gelf_xlatetom(re->elf, &dst, d,
3218 re->ehdr.e_ident[EI_DATA]) != &dst) {
3219 warnx("gelf_xlatetom failed: %s", elf_errmsg(-1));
3222 if (dst.d_size < 2 * sizeof(uint64_t)) {
3223 warnx(".hash section too small");
3229 if (nbucket <= 0 || nchain <= 0) {
3230 warnx("Malformed .hash section");
3233 if (d->d_size != (nbucket + nchain + 2) * sizeof(uint32_t)) {
3234 warnx("Malformed .hash section");
3238 chain = &buf[2 + nbucket];
3241 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
3242 errx(EXIT_FAILURE, "calloc failed");
3243 for (i = 0; (uint32_t)i < nbucket; i++)
3244 for (j = bucket[i]; j > 0 && (uint32_t)j < nchain; j = chain[j])
3247 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
3248 errx(EXIT_FAILURE, "calloc failed");
3249 for (i = 0; (uint64_t)i < nbucket; i++)
3251 printf("Histogram for bucket list length (total of %ju buckets):\n",
3252 (uintmax_t)nbucket);
3253 printf(" Length\tNumber\t\t%% of total\tCoverage\n");
3255 for (i = 0; (uint64_t)i <= maxl; i++) {
3257 printf("%7u\t%-10ju\t(%5.1f%%)\t%5.1f%%\n", i, (uintmax_t)c[i],
3258 c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
3265 dump_gnu_hash(struct readelf *re, struct section *s)
3270 uint32_t *bucket, *chain;
3271 uint32_t nbucket, nchain, symndx, maskwords;
3272 uint32_t *bl, *c, maxl, total;
3273 int elferr, dynsymcount, i, j;
3276 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3277 elferr = elf_errno();
3279 warnx("elf_getdata failed: %s",
3280 elf_errmsg(elferr));
3283 if (d->d_size < 4 * sizeof(uint32_t)) {
3284 warnx(".gnu.hash section too small");
3292 if (s->link >= re->shnum)
3294 ds = &re->sl[s->link];
3295 if (!get_ent_count(ds, &dynsymcount))
3297 if (symndx >= (uint32_t)dynsymcount) {
3298 warnx("Malformed .gnu.hash section (symndx out of range)");
3301 nchain = dynsymcount - symndx;
3302 if (d->d_size != 4 * sizeof(uint32_t) + maskwords *
3303 (re->ec == ELFCLASS32 ? sizeof(uint32_t) : sizeof(uint64_t)) +
3304 (nbucket + nchain) * sizeof(uint32_t)) {
3305 warnx("Malformed .gnu.hash section");
3308 bucket = buf + (re->ec == ELFCLASS32 ? maskwords : maskwords * 2);
3309 chain = bucket + nbucket;
3312 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
3313 errx(EXIT_FAILURE, "calloc failed");
3314 for (i = 0; (uint32_t)i < nbucket; i++)
3315 for (j = bucket[i]; j > 0 && (uint32_t)j - symndx < nchain;
3319 if (chain[j - symndx] & 1)
3322 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
3323 errx(EXIT_FAILURE, "calloc failed");
3324 for (i = 0; (uint32_t)i < nbucket; i++)
3326 printf("Histogram for bucket list length (total of %u buckets):\n",
3328 printf(" Length\tNumber\t\t%% of total\tCoverage\n");
3330 for (i = 0; (uint32_t)i <= maxl; i++) {
3332 printf("%7u\t%-10u\t(%5.1f%%)\t%5.1f%%\n", i, c[i],
3333 c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
3340 dump_hash(struct readelf *re)
3345 for (i = 0; (size_t) i < re->shnum; i++) {
3347 if (s->type == SHT_HASH || s->type == SHT_GNU_HASH) {
3348 if (s->type == SHT_GNU_HASH)
3349 dump_gnu_hash(re, s);
3350 else if (re->ehdr.e_machine == EM_ALPHA &&
3352 dump_svr4_hash64(re, s);
3360 dump_notes(struct readelf *re)
3363 const char *rawfile;
3366 size_t filesize, phnum;
3369 if (re->ehdr.e_type == ET_CORE) {
3371 * Search program headers in the core file for
3374 if (elf_getphnum(re->elf, &phnum) == 0) {
3375 warnx("elf_getphnum failed: %s", elf_errmsg(-1));
3380 if ((rawfile = elf_rawfile(re->elf, &filesize)) == NULL) {
3381 warnx("elf_rawfile failed: %s", elf_errmsg(-1));
3384 for (i = 0; (size_t) i < phnum; i++) {
3385 if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
3386 warnx("gelf_getphdr failed: %s",
3390 if (phdr.p_type == PT_NOTE) {
3391 if (phdr.p_offset >= filesize ||
3392 phdr.p_filesz > filesize - phdr.p_offset) {
3393 warnx("invalid PHDR offset");
3396 dump_notes_content(re, rawfile + phdr.p_offset,
3397 phdr.p_filesz, phdr.p_offset);
3403 * For objects other than core files, Search for
3404 * SHT_NOTE sections.
3406 for (i = 0; (size_t) i < re->shnum; i++) {
3408 if (s->type == SHT_NOTE) {
3410 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3411 elferr = elf_errno();
3413 warnx("elf_getdata failed: %s",
3414 elf_errmsg(elferr));
3417 dump_notes_content(re, d->d_buf, d->d_size,
3425 dump_notes_content(struct readelf *re, const char *buf, size_t sz, off_t off)
3428 const char *end, *name;
3430 printf("\nNotes at offset %#010jx with length %#010jx:\n",
3431 (uintmax_t) off, (uintmax_t) sz);
3432 printf(" %-13s %-15s %s\n", "Owner", "Data size", "Description");
3435 if (buf + sizeof(*note) > end) {
3436 warnx("invalid note header");
3439 note = (Elf_Note *)(uintptr_t) buf;
3440 name = (char *)(uintptr_t)(note + 1);
3442 * The name field is required to be nul-terminated, and
3443 * n_namesz includes the terminating nul in observed
3444 * implementations (contrary to the ELF-64 spec). A special
3445 * case is needed for cores generated by some older Linux
3446 * versions, which write a note named "CORE" without a nul
3447 * terminator and n_namesz = 4.
3449 if (note->n_namesz == 0)
3451 else if (note->n_namesz == 4 && strncmp(name, "CORE", 4) == 0)
3453 else if (strnlen(name, note->n_namesz) >= note->n_namesz)
3455 printf(" %-13s %#010jx", name, (uintmax_t) note->n_descsz);
3456 printf(" %s\n", note_type(name, re->ehdr.e_type,
3458 buf += sizeof(Elf_Note) + roundup2(note->n_namesz, 4) +
3459 roundup2(note->n_descsz, 4);
3464 * Symbol versioning sections are the same for 32bit and 64bit
3467 #define Elf_Verdef Elf32_Verdef
3468 #define Elf_Verdaux Elf32_Verdaux
3469 #define Elf_Verneed Elf32_Verneed
3470 #define Elf_Vernaux Elf32_Vernaux
3472 #define SAVE_VERSION_NAME(x, n, t) \
3474 while (x >= re->ver_sz) { \
3475 nv = realloc(re->ver, \
3476 sizeof(*re->ver) * re->ver_sz * 2); \
3478 warn("realloc failed"); \
3483 for (i = re->ver_sz; i < re->ver_sz * 2; i++) { \
3484 re->ver[i].name = NULL; \
3485 re->ver[i].type = 0; \
3490 re->ver[x].name = n; \
3491 re->ver[x].type = t; \
3497 dump_verdef(struct readelf *re, int dump)
3504 uint8_t *buf, *end, *buf2;
3508 if ((s = re->vd_s) == NULL)
3510 if (s->link >= re->shnum)
3513 if (re->ver == NULL) {
3515 if ((re->ver = calloc(re->ver_sz, sizeof(*re->ver))) ==
3517 warn("calloc failed");
3520 re->ver[0].name = "*local*";
3521 re->ver[1].name = "*global*";
3525 printf("\nVersion definition section (%s):\n", s->name);
3527 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3528 elferr = elf_errno();
3530 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
3537 end = buf + d->d_size;
3538 while (buf + sizeof(Elf_Verdef) <= end) {
3539 vd = (Elf_Verdef *) (uintptr_t) buf;
3541 printf(" 0x%4.4lx", (unsigned long)
3542 (buf - (uint8_t *)d->d_buf));
3543 printf(" vd_version: %u vd_flags: %d"
3544 " vd_ndx: %u vd_cnt: %u", vd->vd_version,
3545 vd->vd_flags, vd->vd_ndx, vd->vd_cnt);
3547 buf2 = buf + vd->vd_aux;
3549 while (buf2 + sizeof(Elf_Verdaux) <= end && j < vd->vd_cnt) {
3550 vda = (Elf_Verdaux *) (uintptr_t) buf2;
3551 name = get_string(re, s->link, vda->vda_name);
3554 printf(" vda_name: %s\n", name);
3555 SAVE_VERSION_NAME((int)vd->vd_ndx, name, 1);
3557 printf(" 0x%4.4lx parent: %s\n",
3558 (unsigned long) (buf2 -
3559 (uint8_t *)d->d_buf), name);
3560 if (vda->vda_next == 0)
3562 buf2 += vda->vda_next;
3565 if (vd->vd_next == 0)
3572 dump_verneed(struct readelf *re, int dump)
3579 uint8_t *buf, *end, *buf2;
3583 if ((s = re->vn_s) == NULL)
3585 if (s->link >= re->shnum)
3588 if (re->ver == NULL) {
3590 if ((re->ver = calloc(re->ver_sz, sizeof(*re->ver))) ==
3592 warn("calloc failed");
3595 re->ver[0].name = "*local*";
3596 re->ver[1].name = "*global*";
3600 printf("\nVersion needed section (%s):\n", s->name);
3602 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3603 elferr = elf_errno();
3605 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
3612 end = buf + d->d_size;
3613 while (buf + sizeof(Elf_Verneed) <= end) {
3614 vn = (Elf_Verneed *) (uintptr_t) buf;
3616 printf(" 0x%4.4lx", (unsigned long)
3617 (buf - (uint8_t *)d->d_buf));
3618 printf(" vn_version: %u vn_file: %s vn_cnt: %u\n",
3620 get_string(re, s->link, vn->vn_file),
3623 buf2 = buf + vn->vn_aux;
3625 while (buf2 + sizeof(Elf_Vernaux) <= end && j < vn->vn_cnt) {
3626 vna = (Elf32_Vernaux *) (uintptr_t) buf2;
3628 printf(" 0x%4.4lx", (unsigned long)
3629 (buf2 - (uint8_t *)d->d_buf));
3630 name = get_string(re, s->link, vna->vna_name);
3632 printf(" vna_name: %s vna_flags: %u"
3633 " vna_other: %u\n", name,
3634 vna->vna_flags, vna->vna_other);
3635 SAVE_VERSION_NAME((int)vna->vna_other, name, 0);
3636 if (vna->vna_next == 0)
3638 buf2 += vna->vna_next;
3641 if (vn->vn_next == 0)
3648 dump_versym(struct readelf *re)
3653 if (re->vs_s == NULL || re->ver == NULL || re->vs == NULL)
3655 printf("\nVersion symbol section (%s):\n", re->vs_s->name);
3656 for (i = 0; i < re->vs_sz; i++) {
3660 printf(" %03x:", i);
3662 vs = re->vs[i] & VERSYM_VERSION;
3663 if (vs >= re->ver_sz || re->ver[vs].name == NULL) {
3664 warnx("invalid versym version index %u", re->vs[i]);
3667 if (re->vs[i] & VERSYM_HIDDEN)
3668 printf(" %3xh %-12s ", vs,
3669 re->ver[re->vs[i] & VERSYM_VERSION].name);
3671 printf(" %3x %-12s ", vs, re->ver[re->vs[i]].name);
3677 dump_ver(struct readelf *re)
3680 if (re->vs_s && re->ver && re->vs)
3685 dump_verneed(re, 1);
3689 search_ver(struct readelf *re)
3695 for (i = 0; (size_t) i < re->shnum; i++) {
3697 if (s->type == SHT_SUNW_versym)
3699 if (s->type == SHT_SUNW_verneed)
3701 if (s->type == SHT_SUNW_verdef)
3707 dump_verneed(re, 0);
3708 if (re->vs_s && re->ver != NULL) {
3710 if ((d = elf_getdata(re->vs_s->scn, NULL)) == NULL) {
3711 elferr = elf_errno();
3713 warnx("elf_getdata failed: %s",
3714 elf_errmsg(elferr));
3720 re->vs_sz = d->d_size / sizeof(Elf32_Half);
3728 #undef SAVE_VERSION_NAME
3731 * Elf32_Lib and Elf64_Lib are identical.
3733 #define Elf_Lib Elf32_Lib
3736 dump_liblist(struct readelf *re)
3744 int i, j, k, elferr, first, len;
3746 for (i = 0; (size_t) i < re->shnum; i++) {
3748 if (s->type != SHT_GNU_LIBLIST)
3750 if (s->link >= re->shnum)
3753 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3754 elferr = elf_errno();
3756 warnx("elf_getdata failed: %s",
3757 elf_errmsg(elferr));
3763 if (!get_ent_count(s, &len))
3765 printf("\nLibrary list section '%s' ", s->name);
3766 printf("contains %d entries:\n", len);
3767 printf("%12s%24s%18s%10s%6s\n", "Library", "Time Stamp",
3768 "Checksum", "Version", "Flags");
3769 for (j = 0; (uint64_t) j < s->sz / s->entsize; j++) {
3772 get_string(re, s->link, lib->l_name));
3773 ti = lib->l_time_stamp;
3775 snprintf(tbuf, sizeof(tbuf), "%04d-%02d-%02dT%02d:%02d"
3776 ":%2d", t->tm_year + 1900, t->tm_mon + 1,
3777 t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
3778 printf("%-19.19s ", tbuf);
3779 printf("0x%08x ", lib->l_checksum);
3780 printf("%-7d %#x", lib->l_version, lib->l_flags);
3781 if (lib->l_flags != 0) {
3784 for (k = 0; l_flag[k].name != NULL; k++) {
3785 if ((l_flag[k].value & lib->l_flags) ==
3792 printf("%s", l_flag[k].name);
3805 dump_section_groups(struct readelf *re)
3808 const char *symname;
3814 for (i = 0; (size_t) i < re->shnum; i++) {
3816 if (s->type != SHT_GROUP)
3818 if (s->link >= re->shnum)
3821 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3822 elferr = elf_errno();
3824 warnx("elf_getdata failed: %s",
3825 elf_errmsg(elferr));
3833 /* We only support COMDAT section. */
3835 #define GRP_COMDAT 0x1
3837 if ((*w++ & GRP_COMDAT) == 0)
3840 if (s->entsize == 0)
3843 symname = get_symbol_name(re, s->link, s->info);
3844 n = s->sz / s->entsize;
3848 printf("\nCOMDAT group section [%5d] `%s' [%s] contains %ju"
3849 " sections:\n", i, s->name, symname, (uintmax_t)n);
3850 printf(" %-10.10s %s\n", "[Index]", "Name");
3851 for (j = 0; (size_t) j < n; j++, w++) {
3852 if (*w >= re->shnum) {
3853 warnx("invalid section index: %u", *w);
3856 printf(" [%5u] %s\n", *w, re->sl[*w].name);
3862 dump_unknown_tag(uint64_t tag, uint8_t *p, uint8_t *pe)
3867 * According to ARM EABI: For tags > 32, even numbered tags have
3868 * a ULEB128 param and odd numbered ones have NUL-terminated
3869 * string param. This rule probably also applies for tags <= 32
3870 * if the object arch is not ARM.
3873 printf(" Tag_unknown_%ju: ", (uintmax_t) tag);
3876 printf("%s\n", (char *) p);
3877 p += strlen((char *) p) + 1;
3879 val = _decode_uleb128(&p, pe);
3880 printf("%ju\n", (uintmax_t) val);
3887 dump_compatibility_tag(uint8_t *p, uint8_t *pe)
3891 val = _decode_uleb128(&p, pe);
3892 printf("flag = %ju, vendor = %s\n", (uintmax_t) val, p);
3893 p += strlen((char *) p) + 1;
3899 dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe)
3908 tag = _decode_uleb128(&p, pe);
3910 for (i = 0; i < sizeof(aeabi_tags) / sizeof(aeabi_tags[0]);
3912 if (tag == aeabi_tags[i].tag) {
3914 printf(" %s: ", aeabi_tags[i].s_tag);
3915 if (aeabi_tags[i].get_desc) {
3917 val = _decode_uleb128(&p, pe);
3919 aeabi_tags[i].get_desc(val));
3923 if (tag < aeabi_tags[i].tag)
3927 p = dump_unknown_tag(tag, p, pe);
3934 case 4: /* Tag_CPU_raw_name */
3935 case 5: /* Tag_CPU_name */
3936 case 67: /* Tag_conformance */
3937 printf("%s\n", (char *) p);
3938 p += strlen((char *) p) + 1;
3940 case 32: /* Tag_compatibility */
3941 p = dump_compatibility_tag(p, pe);
3943 case 64: /* Tag_nodefaults */
3944 /* ignored, written as 0. */
3945 (void) _decode_uleb128(&p, pe);
3948 case 65: /* Tag_also_compatible_with */
3949 val = _decode_uleb128(&p, pe);
3950 /* Must be Tag_CPU_arch */
3952 printf("unknown\n");
3955 val = _decode_uleb128(&p, pe);
3956 printf("%s\n", aeabi_cpu_arch(val));
3957 /* Skip NUL terminator. */
3967 #ifndef Tag_GNU_MIPS_ABI_FP
3968 #define Tag_GNU_MIPS_ABI_FP 4
3972 dump_mips_attributes(struct readelf *re, uint8_t *p, uint8_t *pe)
3979 tag = _decode_uleb128(&p, pe);
3981 case Tag_GNU_MIPS_ABI_FP:
3982 val = _decode_uleb128(&p, pe);
3983 printf(" Tag_GNU_MIPS_ABI_FP: %s\n", mips_abi_fp(val));
3985 case 32: /* Tag_compatibility */
3986 p = dump_compatibility_tag(p, pe);
3989 p = dump_unknown_tag(tag, p, pe);
3995 #ifndef Tag_GNU_Power_ABI_FP
3996 #define Tag_GNU_Power_ABI_FP 4
3999 #ifndef Tag_GNU_Power_ABI_Vector
4000 #define Tag_GNU_Power_ABI_Vector 8
4004 dump_ppc_attributes(uint8_t *p, uint8_t *pe)
4009 tag = _decode_uleb128(&p, pe);
4011 case Tag_GNU_Power_ABI_FP:
4012 val = _decode_uleb128(&p, pe);
4013 printf(" Tag_GNU_Power_ABI_FP: %s\n", ppc_abi_fp(val));
4015 case Tag_GNU_Power_ABI_Vector:
4016 val = _decode_uleb128(&p, pe);
4017 printf(" Tag_GNU_Power_ABI_Vector: %s\n",
4018 ppc_abi_vector(val));
4020 case 32: /* Tag_compatibility */
4021 p = dump_compatibility_tag(p, pe);
4024 p = dump_unknown_tag(tag, p, pe);
4031 dump_attributes(struct readelf *re)
4035 uint8_t *p, *pe, *sp;
4036 size_t len, seclen, nlen, sublen;
4040 for (i = 0; (size_t) i < re->shnum; i++) {
4042 if (s->type != SHT_GNU_ATTRIBUTES &&
4043 (re->ehdr.e_machine != EM_ARM || s->type != SHT_LOPROC + 3))
4046 if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
4047 elferr = elf_errno();
4049 warnx("elf_rawdata failed: %s",
4050 elf_errmsg(elferr));
4058 printf("Unknown Attribute Section Format: %c\n",
4062 len = d->d_size - 1;
4066 warnx("truncated attribute section length");
4069 seclen = re->dw_decode(&p, 4);
4071 warnx("invalid attribute section length");
4075 nlen = strlen((char *) p) + 1;
4076 if (nlen + 4 > seclen) {
4077 warnx("invalid attribute section name");
4080 printf("Attribute Section: %s\n", (char *) p);
4083 while (seclen > 0) {
4086 sublen = re->dw_decode(&p, 4);
4087 if (sublen > seclen) {
4088 warnx("invalid attribute sub-section"
4093 printf("%s", top_tag(tag));
4094 if (tag == 2 || tag == 3) {
4097 val = _decode_uleb128(&p, pe);
4100 printf(" %ju", (uintmax_t) val);
4104 if (re->ehdr.e_machine == EM_ARM &&
4105 s->type == SHT_LOPROC + 3)
4106 dump_arm_attributes(re, p, sp + sublen);
4107 else if (re->ehdr.e_machine == EM_MIPS ||
4108 re->ehdr.e_machine == EM_MIPS_RS3_LE)
4109 dump_mips_attributes(re, p,
4111 else if (re->ehdr.e_machine == EM_PPC)
4112 dump_ppc_attributes(p, sp + sublen);
4120 dump_mips_specific_info(struct readelf *re)
4126 for (i = 0; (size_t) i < re->shnum; i++) {
4128 if (s->name != NULL && (!strcmp(s->name, ".MIPS.options") ||
4129 (s->type == SHT_MIPS_OPTIONS))) {
4130 dump_mips_options(re, s);
4134 if (s->name != NULL && (!strcmp(s->name, ".MIPS.abiflags") ||
4135 (s->type == SHT_MIPS_ABIFLAGS)))
4136 dump_mips_abiflags(re, s);
4139 * Dump .reginfo if present (although it will be ignored by an OS if a
4140 * .MIPS.options section is present, according to SGI mips64 spec).
4142 for (i = 0; (size_t) i < re->shnum; i++) {
4144 if (s->name != NULL && (!strcmp(s->name, ".reginfo") ||
4145 (s->type == SHT_MIPS_REGINFO)))
4146 dump_mips_reginfo(re, s);
4151 dump_mips_abiflags(struct readelf *re, struct section *s)
4156 uint32_t isa_ext, ases, flags1, flags2;
4158 uint8_t isa_level, isa_rev, gpr_size, cpr1_size, cpr2_size, fp_abi;
4160 if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
4161 elferr = elf_errno();
4163 warnx("elf_rawdata failed: %s",
4164 elf_errmsg(elferr));
4167 if (d->d_size != 24) {
4168 warnx("invalid MIPS abiflags section size");
4173 version = re->dw_decode(&p, 2);
4174 printf("MIPS ABI Flags Version: %u", version);
4176 printf(" (unknown)\n\n");
4181 isa_level = re->dw_decode(&p, 1);
4182 isa_rev = re->dw_decode(&p, 1);
4183 gpr_size = re->dw_decode(&p, 1);
4184 cpr1_size = re->dw_decode(&p, 1);
4185 cpr2_size = re->dw_decode(&p, 1);
4186 fp_abi = re->dw_decode(&p, 1);
4187 isa_ext = re->dw_decode(&p, 4);
4188 ases = re->dw_decode(&p, 4);
4189 flags1 = re->dw_decode(&p, 4);
4190 flags2 = re->dw_decode(&p, 4);
4194 printf("MIPS%u\n", isa_level);
4196 printf("MIPS%ur%u\n", isa_level, isa_rev);
4197 printf("GPR size: %d\n", get_mips_register_size(gpr_size));
4198 printf("CPR1 size: %d\n", get_mips_register_size(cpr1_size));
4199 printf("CPR2 size: %d\n", get_mips_register_size(cpr2_size));
4203 printf("Soft float");
4206 printf("%u", fp_abi);
4209 printf("\nISA Extension: %u\n", isa_ext);
4210 printf("ASEs: %u\n", ases);
4211 printf("FLAGS 1: %08x\n", flags1);
4212 printf("FLAGS 2: %08x\n", flags2);
4216 get_mips_register_size(uint8_t flag)
4227 dump_mips_reginfo(struct readelf *re, struct section *s)
4233 if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
4234 elferr = elf_errno();
4236 warnx("elf_rawdata failed: %s",
4237 elf_errmsg(elferr));
4242 if (!get_ent_count(s, &len))
4245 printf("\nSection '%s' contains %d entries:\n", s->name, len);
4246 dump_mips_odk_reginfo(re, d->d_buf, d->d_size);
4250 dump_mips_options(struct readelf *re, struct section *s)
4260 if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
4261 elferr = elf_errno();
4263 warnx("elf_rawdata failed: %s",
4264 elf_errmsg(elferr));
4270 printf("\nSection %s contains:\n", s->name);
4275 warnx("Truncated MIPS option header");
4278 kind = re->dw_decode(&p, 1);
4279 size = re->dw_decode(&p, 1);
4280 sndx = re->dw_decode(&p, 2);
4281 info = re->dw_decode(&p, 4);
4282 if (size < 8 || size - 8 > pe - p) {
4283 warnx("Malformed MIPS option header");
4289 dump_mips_odk_reginfo(re, p, size);
4291 case ODK_EXCEPTIONS:
4292 printf(" EXCEPTIONS FPU_MIN: %#x\n",
4293 info & OEX_FPU_MIN);
4294 printf("%11.11s FPU_MAX: %#x\n", "",
4295 info & OEX_FPU_MAX);
4296 dump_mips_option_flags("", mips_exceptions_option,
4300 printf(" %-10.10s section: %ju\n", "OPAD",
4302 dump_mips_option_flags("", mips_pad_option, info);
4305 dump_mips_option_flags("HWPATCH", mips_hwpatch_option,
4309 dump_mips_option_flags("HWAND", mips_hwa_option, info);
4312 dump_mips_option_flags("HWOR", mips_hwo_option, info);
4315 printf(" %-10.10s %#jx\n", "FILL", (uintmax_t) info);
4318 printf(" %-10.10s\n", "TAGS");
4321 printf(" %-10.10s GP group number: %#x\n", "GP_GROUP",
4324 printf(" %-10.10s GP group is "
4325 "self-contained\n", "");
4328 printf(" %-10.10s default GP group number: %#x\n",
4329 "IDENT", info & 0xFFFF);
4331 printf(" %-10.10s default GP group is "
4332 "self-contained\n", "");
4335 printf(" %-10.10s\n", "PAGESIZE");
4345 dump_mips_option_flags(const char *name, struct mips_option *opt, uint64_t info)
4350 for (; opt->desc != NULL; opt++) {
4351 if (info & opt->flag) {
4352 printf(" %-10.10s %s\n", first ? name : "",
4360 dump_mips_odk_reginfo(struct readelf *re, uint8_t *p, size_t sz)
4362 uint32_t ri_gprmask;
4363 uint32_t ri_cprmask[4];
4364 uint64_t ri_gp_value;
4370 ri_gprmask = re->dw_decode(&p, 4);
4371 /* Skip ri_pad padding field for mips64. */
4372 if (re->ec == ELFCLASS64)
4373 re->dw_decode(&p, 4);
4374 for (i = 0; i < 4; i++)
4375 ri_cprmask[i] = re->dw_decode(&p, 4);
4376 if (re->ec == ELFCLASS32)
4377 ri_gp_value = re->dw_decode(&p, 4);
4379 ri_gp_value = re->dw_decode(&p, 8);
4380 printf(" %s ", option_kind(ODK_REGINFO));
4381 printf("ri_gprmask: 0x%08jx\n", (uintmax_t) ri_gprmask);
4382 for (i = 0; i < 4; i++)
4383 printf("%11.11s ri_cprmask[%d]: 0x%08jx\n", "", i,
4384 (uintmax_t) ri_cprmask[i]);
4385 printf("%12.12s", "");
4386 printf("ri_gp_value: %#jx\n", (uintmax_t) ri_gp_value);
4391 dump_arch_specific_info(struct readelf *re)
4395 dump_attributes(re);
4397 switch (re->ehdr.e_machine) {
4399 case EM_MIPS_RS3_LE:
4400 dump_mips_specific_info(re);
4407 dwarf_regname(struct readelf *re, unsigned int num)
4412 if ((rn = dwarf_reg(re->ehdr.e_machine, num)) != NULL)
4415 snprintf(rx, sizeof(rx), "r%u", num);
4421 dump_dwarf_line(struct readelf *re)
4426 Dwarf_Half tag, version, pointer_size;
4427 Dwarf_Unsigned offset, endoff, length, hdrlen, dirndx, mtime, fsize;
4428 Dwarf_Small minlen, defstmt, lrange, opbase, oplen;
4431 uint64_t address, file, line, column, isa, opsize, udelta;
4435 int i, is_stmt, dwarf_size, elferr, ret;
4437 printf("\nDump of debug contents of section .debug_line:\n");
4440 for (i = 0; (size_t) i < re->shnum; i++) {
4442 if (s->name != NULL && !strcmp(s->name, ".debug_line"))
4445 if ((size_t) i >= re->shnum)
4449 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
4450 elferr = elf_errno();
4452 warnx("elf_getdata failed: %s", elf_errmsg(-1));
4458 while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
4459 NULL, &de)) == DW_DLV_OK) {
4461 while (dwarf_siblingof(re->dbg, die, &die, &de) == DW_DLV_OK) {
4462 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
4463 warnx("dwarf_tag failed: %s",
4467 /* XXX: What about DW_TAG_partial_unit? */
4468 if (tag == DW_TAG_compile_unit)
4472 warnx("could not find DW_TAG_compile_unit die");
4475 if (dwarf_attrval_unsigned(die, DW_AT_stmt_list, &offset,
4479 length = re->dw_read(d, &offset, 4);
4480 if (length == 0xffffffff) {
4482 length = re->dw_read(d, &offset, 8);
4486 if (length > d->d_size - offset) {
4487 warnx("invalid .dwarf_line section");
4491 endoff = offset + length;
4492 pe = (uint8_t *) d->d_buf + endoff;
4493 version = re->dw_read(d, &offset, 2);
4494 hdrlen = re->dw_read(d, &offset, dwarf_size);
4495 minlen = re->dw_read(d, &offset, 1);
4496 defstmt = re->dw_read(d, &offset, 1);
4497 lbase = re->dw_read(d, &offset, 1);
4498 lrange = re->dw_read(d, &offset, 1);
4499 opbase = re->dw_read(d, &offset, 1);
4502 printf(" Length:\t\t\t%ju\n", (uintmax_t) length);
4503 printf(" DWARF version:\t\t%u\n", version);
4504 printf(" Prologue Length:\t\t%ju\n", (uintmax_t) hdrlen);
4505 printf(" Minimum Instruction Length:\t%u\n", minlen);
4506 printf(" Initial value of 'is_stmt':\t%u\n", defstmt);
4507 printf(" Line Base:\t\t\t%d\n", lbase);
4508 printf(" Line Range:\t\t\t%u\n", lrange);
4509 printf(" Opcode Base:\t\t\t%u\n", opbase);
4510 (void) dwarf_get_address_size(re->dbg, &pointer_size, &de);
4511 printf(" (Pointer size:\t\t%u)\n", pointer_size);
4514 printf(" Opcodes:\n");
4515 for (i = 1; i < opbase; i++) {
4516 oplen = re->dw_read(d, &offset, 1);
4517 printf(" Opcode %d has %u args\n", i, oplen);
4521 printf(" The Directory Table:\n");
4522 p = (uint8_t *) d->d_buf + offset;
4523 while (*p != '\0') {
4524 printf(" %s\n", (char *) p);
4525 p += strlen((char *) p) + 1;
4530 printf(" The File Name Table:\n");
4531 printf(" Entry\tDir\tTime\tSize\tName\n");
4533 while (*p != '\0') {
4536 p += strlen(pn) + 1;
4537 dirndx = _decode_uleb128(&p, pe);
4538 mtime = _decode_uleb128(&p, pe);
4539 fsize = _decode_uleb128(&p, pe);
4540 printf(" %d\t%ju\t%ju\t%ju\t%s\n", i,
4541 (uintmax_t) dirndx, (uintmax_t) mtime,
4542 (uintmax_t) fsize, pn);
4545 #define RESET_REGISTERS \
4551 is_stmt = defstmt; \
4554 #define LINE(x) (lbase + (((x) - opbase) % lrange))
4555 #define ADDRESS(x) ((((x) - opbase) / lrange) * minlen)
4559 printf(" Line Number Statements:\n");
4570 opsize = _decode_uleb128(&p, pe);
4571 printf(" Extended opcode %u: ", *p);
4573 case DW_LNE_end_sequence:
4576 printf("End of Sequence\n");
4578 case DW_LNE_set_address:
4580 address = re->dw_decode(&p,
4582 printf("set Address to %#jx\n",
4583 (uintmax_t) address);
4585 case DW_LNE_define_file:
4588 p += strlen(pn) + 1;
4589 dirndx = _decode_uleb128(&p, pe);
4590 mtime = _decode_uleb128(&p, pe);
4591 fsize = _decode_uleb128(&p, pe);
4592 printf("define new file: %s\n", pn);
4595 /* Unrecognized extened opcodes. */
4597 printf("unknown opcode\n");
4599 } else if (*p > 0 && *p < opbase) {
4607 case DW_LNS_advance_pc:
4608 udelta = _decode_uleb128(&p, pe) *
4611 printf(" Advance PC by %ju to %#jx\n",
4613 (uintmax_t) address);
4615 case DW_LNS_advance_line:
4616 sdelta = _decode_sleb128(&p, pe);
4618 printf(" Advance Line by %jd to %ju\n",
4622 case DW_LNS_set_file:
4623 file = _decode_uleb128(&p, pe);
4624 printf(" Set File to %ju\n",
4627 case DW_LNS_set_column:
4628 column = _decode_uleb128(&p, pe);
4629 printf(" Set Column to %ju\n",
4630 (uintmax_t) column);
4632 case DW_LNS_negate_stmt:
4634 printf(" Set is_stmt to %d\n", is_stmt);
4636 case DW_LNS_set_basic_block:
4637 printf(" Set basic block flag\n");
4639 case DW_LNS_const_add_pc:
4640 address += ADDRESS(255);
4641 printf(" Advance PC by constant %ju"
4643 (uintmax_t) ADDRESS(255),
4644 (uintmax_t) address);
4646 case DW_LNS_fixed_advance_pc:
4647 udelta = re->dw_decode(&p, 2);
4649 printf(" Advance PC by fixed value "
4652 (uintmax_t) address);
4654 case DW_LNS_set_prologue_end:
4655 printf(" Set prologue end flag\n");
4657 case DW_LNS_set_epilogue_begin:
4658 printf(" Set epilogue begin flag\n");
4660 case DW_LNS_set_isa:
4661 isa = _decode_uleb128(&p, pe);
4662 printf(" Set isa to %ju\n",
4666 /* Unrecognized extended opcodes. */
4667 printf(" Unknown extended opcode %u\n",
4677 address += ADDRESS(*p);
4678 printf(" Special opcode %u: advance Address "
4679 "by %ju to %#jx and Line by %jd to %ju\n",
4680 *p - opbase, (uintmax_t) ADDRESS(*p),
4681 (uintmax_t) address, (intmax_t) LINE(*p),
4689 if (ret == DW_DLV_ERROR)
4690 warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
4692 #undef RESET_REGISTERS
4698 dump_dwarf_line_decoded(struct readelf *re)
4701 Dwarf_Line *linebuf, ln;
4702 Dwarf_Addr lineaddr;
4703 Dwarf_Signed linecount, srccount;
4704 Dwarf_Unsigned lineno, fn;
4706 const char *dir, *file;
4710 printf("Decoded dump of debug contents of section .debug_line:\n\n");
4711 while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
4712 NULL, &de)) == DW_DLV_OK) {
4713 if (dwarf_siblingof(re->dbg, NULL, &die, &de) != DW_DLV_OK)
4715 if (dwarf_attrval_string(die, DW_AT_name, &file, &de) !=
4718 if (dwarf_attrval_string(die, DW_AT_comp_dir, &dir, &de) !=
4727 printf("%-37s %11s %s\n", "Filename", "Line Number",
4728 "Starting Address");
4729 if (dwarf_srclines(die, &linebuf, &linecount, &de) != DW_DLV_OK)
4731 if (dwarf_srcfiles(die, &srcfiles, &srccount, &de) != DW_DLV_OK)
4733 for (i = 0; i < linecount; i++) {
4735 if (dwarf_line_srcfileno(ln, &fn, &de) != DW_DLV_OK)
4737 if (dwarf_lineno(ln, &lineno, &de) != DW_DLV_OK)
4739 if (dwarf_lineaddr(ln, &lineaddr, &de) != DW_DLV_OK)
4741 printf("%-37s %11ju %#18jx\n",
4742 basename(srcfiles[fn - 1]), (uintmax_t) lineno,
4743 (uintmax_t) lineaddr);
4750 dump_dwarf_die(struct readelf *re, Dwarf_Die die, int level)
4752 Dwarf_Attribute *attr_list;
4754 Dwarf_Off dieoff, cuoff, culen, attroff;
4755 Dwarf_Unsigned ate, lang, v_udata, v_sig;
4756 Dwarf_Signed attr_count, v_sdata;
4759 Dwarf_Half tag, attr, form;
4760 Dwarf_Block *v_block;
4761 Dwarf_Bool v_bool, is_info;
4765 const char *tag_str, *attr_str, *ate_str, *lang_str;
4766 char unk_tag[32], unk_attr[32];
4771 if (dwarf_dieoffset(die, &dieoff, &de) != DW_DLV_OK) {
4772 warnx("dwarf_dieoffset failed: %s", dwarf_errmsg(de));
4776 printf(" <%d><%jx>: ", level, (uintmax_t) dieoff);
4778 if (dwarf_die_CU_offset_range(die, &cuoff, &culen, &de) != DW_DLV_OK) {
4779 warnx("dwarf_die_CU_offset_range failed: %s",
4784 abc = dwarf_die_abbrev_code(die);
4785 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
4786 warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
4789 if (dwarf_get_TAG_name(tag, &tag_str) != DW_DLV_OK) {
4790 snprintf(unk_tag, sizeof(unk_tag), "[Unknown Tag: %#x]", tag);
4794 printf("Abbrev Number: %d (%s)\n", abc, tag_str);
4796 if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
4798 if (ret == DW_DLV_ERROR)
4799 warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
4803 for (i = 0; i < attr_count; i++) {
4804 if (dwarf_whatform(attr_list[i], &form, &de) != DW_DLV_OK) {
4805 warnx("dwarf_whatform failed: %s", dwarf_errmsg(de));
4808 if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
4809 warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
4812 if (dwarf_get_AT_name(attr, &attr_str) != DW_DLV_OK) {
4813 snprintf(unk_attr, sizeof(unk_attr),
4814 "[Unknown AT: %#x]", attr);
4815 attr_str = unk_attr;
4817 if (dwarf_attroffset(attr_list[i], &attroff, &de) !=
4819 warnx("dwarf_attroffset failed: %s", dwarf_errmsg(de));
4822 printf(" <%jx> %-18s: ", (uintmax_t) attroff, attr_str);
4824 case DW_FORM_ref_addr:
4825 case DW_FORM_sec_offset:
4826 if (dwarf_global_formref(attr_list[i], &v_off, &de) !=
4828 warnx("dwarf_global_formref failed: %s",
4832 if (form == DW_FORM_ref_addr)
4833 printf("<0x%jx>", (uintmax_t) v_off);
4835 printf("0x%jx", (uintmax_t) v_off);
4842 case DW_FORM_ref_udata:
4843 if (dwarf_formref(attr_list[i], &v_off, &de) !=
4845 warnx("dwarf_formref failed: %s",
4850 printf("<0x%jx>", (uintmax_t) v_off);
4854 if (dwarf_formaddr(attr_list[i], &v_addr, &de) !=
4856 warnx("dwarf_formaddr failed: %s",
4860 printf("%#jx", (uintmax_t) v_addr);
4868 if (dwarf_formudata(attr_list[i], &v_udata, &de) !=
4870 warnx("dwarf_formudata failed: %s",
4874 if (attr == DW_AT_high_pc)
4875 printf("0x%jx", (uintmax_t) v_udata);
4877 printf("%ju", (uintmax_t) v_udata);
4881 if (dwarf_formsdata(attr_list[i], &v_sdata, &de) !=
4883 warnx("dwarf_formudata failed: %s",
4887 printf("%jd", (intmax_t) v_sdata);
4891 if (dwarf_formflag(attr_list[i], &v_bool, &de) !=
4893 warnx("dwarf_formflag failed: %s",
4897 printf("%jd", (intmax_t) v_bool);
4900 case DW_FORM_flag_present:
4904 case DW_FORM_string:
4906 if (dwarf_formstring(attr_list[i], &v_str, &de) !=
4908 warnx("dwarf_formstring failed: %s",
4912 if (form == DW_FORM_string)
4913 printf("%s", v_str);
4915 printf("(indirect string) %s", v_str);
4919 case DW_FORM_block1:
4920 case DW_FORM_block2:
4921 case DW_FORM_block4:
4922 if (dwarf_formblock(attr_list[i], &v_block, &de) !=
4924 warnx("dwarf_formblock failed: %s",
4928 printf("%ju byte block:", (uintmax_t) v_block->bl_len);
4929 b = v_block->bl_data;
4930 for (j = 0; (Dwarf_Unsigned) j < v_block->bl_len; j++)
4931 printf(" %x", b[j]);
4933 dump_dwarf_block(re, v_block->bl_data, v_block->bl_len);
4937 case DW_FORM_exprloc:
4938 if (dwarf_formexprloc(attr_list[i], &v_udata, &v_expr,
4939 &de) != DW_DLV_OK) {
4940 warnx("dwarf_formexprloc failed: %s",
4944 printf("%ju byte block:", (uintmax_t) v_udata);
4946 for (j = 0; (Dwarf_Unsigned) j < v_udata; j++)
4947 printf(" %x", b[j]);
4949 dump_dwarf_block(re, v_expr, v_udata);
4953 case DW_FORM_ref_sig8:
4954 if (dwarf_formsig8(attr_list[i], &v_sig8, &de) !=
4956 warnx("dwarf_formsig8 failed: %s",
4960 p = (uint8_t *)(uintptr_t) &v_sig8.signature[0];
4961 v_sig = re->dw_decode(&p, 8);
4962 printf("signature: 0x%jx", (uintmax_t) v_sig);
4965 case DW_AT_encoding:
4966 if (dwarf_attrval_unsigned(die, attr, &ate, &de) !=
4969 if (dwarf_get_ATE_name(ate, &ate_str) != DW_DLV_OK)
4970 ate_str = "DW_ATE_UNKNOWN";
4971 printf("\t(%s)", &ate_str[strlen("DW_ATE_")]);
4974 case DW_AT_language:
4975 if (dwarf_attrval_unsigned(die, attr, &lang, &de) !=
4978 if (dwarf_get_LANG_name(lang, &lang_str) != DW_DLV_OK)
4980 printf("\t(%s)", &lang_str[strlen("DW_LANG_")]);
4983 case DW_AT_location:
4984 case DW_AT_string_length:
4985 case DW_AT_return_addr:
4986 case DW_AT_data_member_location:
4987 case DW_AT_frame_base:
4989 case DW_AT_static_link:
4990 case DW_AT_use_location:
4991 case DW_AT_vtable_elem_location:
4995 case DW_FORM_sec_offset:
4996 printf("\t(location list)");
5010 /* Search children. */
5011 ret = dwarf_child(die, &ret_die, &de);
5012 if (ret == DW_DLV_ERROR)
5013 warnx("dwarf_child: %s", dwarf_errmsg(de));
5014 else if (ret == DW_DLV_OK)
5015 dump_dwarf_die(re, ret_die, level + 1);
5017 /* Search sibling. */
5018 is_info = dwarf_get_die_infotypes_flag(die);
5019 ret = dwarf_siblingof_b(re->dbg, die, &ret_die, is_info, &de);
5020 if (ret == DW_DLV_ERROR)
5021 warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
5022 else if (ret == DW_DLV_OK)
5023 dump_dwarf_die(re, ret_die, level);
5025 dwarf_dealloc(re->dbg, die, DW_DLA_DIE);
5029 set_cu_context(struct readelf *re, Dwarf_Half psize, Dwarf_Half osize,
5033 re->cu_psize = psize;
5034 re->cu_osize = osize;
5039 dump_dwarf_info(struct readelf *re, Dwarf_Bool is_info)
5044 Dwarf_Half tag, version, pointer_size, off_size;
5045 Dwarf_Off cu_offset, cu_length;
5047 Dwarf_Unsigned typeoff;
5054 sn = is_info ? ".debug_info" : ".debug_types";
5057 for (i = 0; (size_t) i < re->shnum; i++) {
5059 if (s->name != NULL && !strcmp(s->name, sn))
5062 if ((size_t) i >= re->shnum)
5066 printf("\nDump of debug contents of section %s:\n", sn);
5068 while ((ret = dwarf_next_cu_header_c(re->dbg, is_info, NULL,
5069 &version, &aboff, &pointer_size, &off_size, NULL, &sig8,
5070 &typeoff, NULL, &de)) == DW_DLV_OK) {
5071 set_cu_context(re, pointer_size, off_size, version);
5073 while (dwarf_siblingof_b(re->dbg, die, &die, is_info,
5074 &de) == DW_DLV_OK) {
5075 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
5076 warnx("dwarf_tag failed: %s",
5080 /* XXX: What about DW_TAG_partial_unit? */
5081 if ((is_info && tag == DW_TAG_compile_unit) ||
5082 (!is_info && tag == DW_TAG_type_unit))
5085 if (die == NULL && is_info) {
5086 warnx("could not find DW_TAG_compile_unit "
5089 } else if (die == NULL && !is_info) {
5090 warnx("could not find DW_TAG_type_unit die");
5094 if (dwarf_die_CU_offset_range(die, &cu_offset,
5095 &cu_length, &de) != DW_DLV_OK) {
5096 warnx("dwarf_die_CU_offset failed: %s",
5101 cu_length -= off_size == 4 ? 4 : 12;
5105 p = (uint8_t *)(uintptr_t) &sig8.signature[0];
5106 sig = re->dw_decode(&p, 8);
5109 printf("\n Type Unit @ offset 0x%jx:\n",
5110 (uintmax_t) cu_offset);
5111 printf(" Length:\t\t%#jx (%d-bit)\n",
5112 (uintmax_t) cu_length, off_size == 4 ? 32 : 64);
5113 printf(" Version:\t\t%u\n", version);
5114 printf(" Abbrev Offset:\t0x%jx\n",
5116 printf(" Pointer Size:\t%u\n", pointer_size);
5118 printf(" Signature:\t\t0x%016jx\n",
5120 printf(" Type Offset:\t0x%jx\n",
5121 (uintmax_t) typeoff);
5124 dump_dwarf_die(re, die, 0);
5126 if (ret == DW_DLV_ERROR)
5127 warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
5130 } while (dwarf_next_types_section(re->dbg, &de) == DW_DLV_OK);
5134 dump_dwarf_abbrev(struct readelf *re)
5137 Dwarf_Off aboff, atoff;
5138 Dwarf_Unsigned length, attr_count;
5139 Dwarf_Signed flag, form;
5140 Dwarf_Half tag, attr;
5142 const char *tag_str, *attr_str, *form_str;
5143 char unk_tag[32], unk_attr[32], unk_form[32];
5146 printf("\nContents of section .debug_abbrev:\n\n");
5148 while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, &aboff,
5149 NULL, NULL, &de)) == DW_DLV_OK) {
5150 printf(" Number TAG\n");
5152 while ((ret = dwarf_get_abbrev(re->dbg, aboff, &ab, &length,
5153 &attr_count, &de)) == DW_DLV_OK) {
5155 dwarf_dealloc(re->dbg, ab, DW_DLA_ABBREV);
5160 if (dwarf_get_abbrev_tag(ab, &tag, &de) != DW_DLV_OK) {
5161 warnx("dwarf_get_abbrev_tag failed: %s",
5165 if (dwarf_get_TAG_name(tag, &tag_str) != DW_DLV_OK) {
5166 snprintf(unk_tag, sizeof(unk_tag),
5167 "[Unknown Tag: %#x]", tag);
5170 if (dwarf_get_abbrev_children_flag(ab, &flag, &de) !=
5172 warnx("dwarf_get_abbrev_children_flag failed:"
5173 " %s", dwarf_errmsg(de));
5176 printf(" %s %s\n", tag_str,
5177 flag ? "[has children]" : "[no children]");
5178 for (j = 0; (Dwarf_Unsigned) j < attr_count; j++) {
5179 if (dwarf_get_abbrev_entry(ab, (Dwarf_Signed) j,
5180 &attr, &form, &atoff, &de) != DW_DLV_OK) {
5181 warnx("dwarf_get_abbrev_entry failed:"
5182 " %s", dwarf_errmsg(de));
5185 if (dwarf_get_AT_name(attr, &attr_str) !=
5187 snprintf(unk_attr, sizeof(unk_attr),
5188 "[Unknown AT: %#x]", attr);
5189 attr_str = unk_attr;
5191 if (dwarf_get_FORM_name(form, &form_str) !=
5193 snprintf(unk_form, sizeof(unk_form),
5194 "[Unknown Form: %#x]",
5196 form_str = unk_form;
5198 printf(" %-18s %s\n", attr_str, form_str);
5201 dwarf_dealloc(re->dbg, ab, DW_DLA_ABBREV);
5203 if (ret != DW_DLV_OK)
5204 warnx("dwarf_get_abbrev: %s", dwarf_errmsg(de));
5206 if (ret == DW_DLV_ERROR)
5207 warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
5211 dump_dwarf_pubnames(struct readelf *re)
5215 Dwarf_Unsigned offset, length, nt_cu_offset, nt_cu_length;
5217 Dwarf_Global *globs;
5218 Dwarf_Half nt_version;
5222 int i, dwarf_size, elferr;
5224 printf("\nContents of the .debug_pubnames section:\n");
5227 for (i = 0; (size_t) i < re->shnum; i++) {
5229 if (s->name != NULL && !strcmp(s->name, ".debug_pubnames"))
5232 if ((size_t) i >= re->shnum)
5236 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
5237 elferr = elf_errno();
5239 warnx("elf_getdata failed: %s", elf_errmsg(-1));
5245 /* Read in .debug_pubnames section table header. */
5247 length = re->dw_read(d, &offset, 4);
5248 if (length == 0xffffffff) {
5250 length = re->dw_read(d, &offset, 8);
5254 if (length > d->d_size - offset) {
5255 warnx("invalid .dwarf_pubnames section");
5259 nt_version = re->dw_read(d, &offset, 2);
5260 nt_cu_offset = re->dw_read(d, &offset, dwarf_size);
5261 nt_cu_length = re->dw_read(d, &offset, dwarf_size);
5262 printf(" Length:\t\t\t\t%ju\n", (uintmax_t) length);
5263 printf(" Version:\t\t\t\t%u\n", nt_version);
5264 printf(" Offset into .debug_info section:\t%ju\n",
5265 (uintmax_t) nt_cu_offset);
5266 printf(" Size of area in .debug_info section:\t%ju\n",
5267 (uintmax_t) nt_cu_length);
5269 if (dwarf_get_globals(re->dbg, &globs, &cnt, &de) != DW_DLV_OK) {
5270 warnx("dwarf_get_globals failed: %s", dwarf_errmsg(de));
5274 printf("\n Offset Name\n");
5275 for (i = 0; i < cnt; i++) {
5276 if (dwarf_globname(globs[i], &glob_name, &de) != DW_DLV_OK) {
5277 warnx("dwarf_globname failed: %s", dwarf_errmsg(de));
5280 if (dwarf_global_die_offset(globs[i], &die_off, &de) !=
5282 warnx("dwarf_global_die_offset failed: %s",
5286 printf(" %-11ju %s\n", (uintmax_t) die_off, glob_name);
5291 dump_dwarf_aranges(struct readelf *re)
5294 Dwarf_Arange *aranges;
5296 Dwarf_Unsigned offset, length, as_cu_offset;
5299 Dwarf_Half as_version, as_addrsz, as_segsz;
5302 int i, dwarf_size, elferr;
5304 printf("\nContents of section .debug_aranges:\n");
5307 for (i = 0; (size_t) i < re->shnum; i++) {
5309 if (s->name != NULL && !strcmp(s->name, ".debug_aranges"))
5312 if ((size_t) i >= re->shnum)
5316 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
5317 elferr = elf_errno();
5319 warnx("elf_getdata failed: %s", elf_errmsg(-1));
5325 /* Read in the .debug_aranges section table header. */
5327 length = re->dw_read(d, &offset, 4);
5328 if (length == 0xffffffff) {
5330 length = re->dw_read(d, &offset, 8);
5334 if (length > d->d_size - offset) {
5335 warnx("invalid .dwarf_aranges section");
5339 as_version = re->dw_read(d, &offset, 2);
5340 as_cu_offset = re->dw_read(d, &offset, dwarf_size);
5341 as_addrsz = re->dw_read(d, &offset, 1);
5342 as_segsz = re->dw_read(d, &offset, 1);
5344 printf(" Length:\t\t\t%ju\n", (uintmax_t) length);
5345 printf(" Version:\t\t\t%u\n", as_version);
5346 printf(" Offset into .debug_info:\t%ju\n", (uintmax_t) as_cu_offset);
5347 printf(" Pointer Size:\t\t\t%u\n", as_addrsz);
5348 printf(" Segment Size:\t\t\t%u\n", as_segsz);
5350 if (dwarf_get_aranges(re->dbg, &aranges, &cnt, &de) != DW_DLV_OK) {
5351 warnx("dwarf_get_aranges failed: %s", dwarf_errmsg(de));
5355 printf("\n Address Length\n");
5356 for (i = 0; i < cnt; i++) {
5357 if (dwarf_get_arange_info(aranges[i], &start, &length,
5358 &die_off, &de) != DW_DLV_OK) {
5359 warnx("dwarf_get_arange_info failed: %s",
5363 printf(" %08jx %ju\n", (uintmax_t) start,
5364 (uintmax_t) length);
5369 dump_dwarf_ranges_foreach(struct readelf *re, Dwarf_Die die, Dwarf_Addr base)
5371 Dwarf_Attribute *attr_list;
5372 Dwarf_Ranges *ranges;
5377 Dwarf_Signed attr_count, cnt;
5378 Dwarf_Unsigned off, bytecnt;
5381 if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
5383 if (ret == DW_DLV_ERROR)
5384 warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
5388 for (i = 0; i < attr_count; i++) {
5389 if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
5390 warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
5393 if (attr != DW_AT_ranges)
5395 if (dwarf_formudata(attr_list[i], &off, &de) != DW_DLV_OK) {
5396 warnx("dwarf_formudata failed: %s", dwarf_errmsg(de));
5399 if (dwarf_get_ranges(re->dbg, (Dwarf_Off) off, &ranges, &cnt,
5400 &bytecnt, &de) != DW_DLV_OK)
5403 for (j = 0; j < cnt; j++) {
5404 printf(" %08jx ", (uintmax_t) off);
5405 if (ranges[j].dwr_type == DW_RANGES_END) {
5406 printf("%s\n", "<End of list>");
5408 } else if (ranges[j].dwr_type ==
5409 DW_RANGES_ADDRESS_SELECTION) {
5410 base0 = ranges[j].dwr_addr2;
5413 if (re->ec == ELFCLASS32)
5414 printf("%08jx %08jx\n",
5415 (uintmax_t) (ranges[j].dwr_addr1 + base0),
5416 (uintmax_t) (ranges[j].dwr_addr2 + base0));
5418 printf("%016jx %016jx\n",
5419 (uintmax_t) (ranges[j].dwr_addr1 + base0),
5420 (uintmax_t) (ranges[j].dwr_addr2 + base0));
5425 /* Search children. */
5426 ret = dwarf_child(die, &ret_die, &de);
5427 if (ret == DW_DLV_ERROR)
5428 warnx("dwarf_child: %s", dwarf_errmsg(de));
5429 else if (ret == DW_DLV_OK)
5430 dump_dwarf_ranges_foreach(re, ret_die, base);
5432 /* Search sibling. */
5433 ret = dwarf_siblingof(re->dbg, die, &ret_die, &de);
5434 if (ret == DW_DLV_ERROR)
5435 warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
5436 else if (ret == DW_DLV_OK)
5437 dump_dwarf_ranges_foreach(re, ret_die, base);
5441 dump_dwarf_ranges(struct readelf *re)
5443 Dwarf_Ranges *ranges;
5446 Dwarf_Unsigned bytecnt;
5449 Dwarf_Unsigned lowpc;
5452 if (dwarf_get_ranges(re->dbg, 0, &ranges, &cnt, &bytecnt, &de) !=
5456 printf("Contents of the .debug_ranges section:\n\n");
5457 if (re->ec == ELFCLASS32)
5458 printf(" %-8s %-8s %s\n", "Offset", "Begin", "End");
5460 printf(" %-8s %-16s %s\n", "Offset", "Begin", "End");
5462 while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
5463 NULL, &de)) == DW_DLV_OK) {
5465 if (dwarf_siblingof(re->dbg, die, &die, &de) != DW_DLV_OK)
5467 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
5468 warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
5471 /* XXX: What about DW_TAG_partial_unit? */
5473 if (tag == DW_TAG_compile_unit) {
5474 if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lowpc,
5479 dump_dwarf_ranges_foreach(re, die, (Dwarf_Addr) lowpc);
5485 dump_dwarf_macinfo(struct readelf *re)
5487 Dwarf_Unsigned offset;
5489 Dwarf_Macro_Details *md;
5495 #define _MAX_MACINFO_ENTRY 65535
5497 printf("\nContents of section .debug_macinfo:\n\n");
5500 while (dwarf_get_macro_details(re->dbg, offset, _MAX_MACINFO_ENTRY,
5501 &cnt, &md, &de) == DW_DLV_OK) {
5502 for (i = 0; i < cnt; i++) {
5503 offset = md[i].dmd_offset + 1;
5504 if (md[i].dmd_type == 0)
5506 if (dwarf_get_MACINFO_name(md[i].dmd_type, &mi_str) !=
5508 snprintf(unk_mi, sizeof(unk_mi),
5509 "[Unknown MACINFO: %#x]", md[i].dmd_type);
5512 printf(" %s", mi_str);
5513 switch (md[i].dmd_type) {
5514 case DW_MACINFO_define:
5515 case DW_MACINFO_undef:
5516 printf(" - lineno : %jd macro : %s\n",
5517 (intmax_t) md[i].dmd_lineno,
5520 case DW_MACINFO_start_file:
5521 printf(" - lineno : %jd filenum : %jd\n",
5522 (intmax_t) md[i].dmd_lineno,
5523 (intmax_t) md[i].dmd_fileindex);
5532 #undef _MAX_MACINFO_ENTRY
5536 dump_dwarf_frame_inst(struct readelf *re, Dwarf_Cie cie, uint8_t *insts,
5537 Dwarf_Unsigned len, Dwarf_Unsigned caf, Dwarf_Signed daf, Dwarf_Addr pc,
5540 Dwarf_Frame_Op *oplist;
5541 Dwarf_Signed opcnt, delta;
5548 if (dwarf_expand_frame_instructions(cie, insts, len, &oplist,
5549 &opcnt, &de) != DW_DLV_OK) {
5550 warnx("dwarf_expand_frame_instructions failed: %s",
5555 for (i = 0; i < opcnt; i++) {
5556 if (oplist[i].fp_base_op != 0)
5557 op = oplist[i].fp_base_op << 6;
5559 op = oplist[i].fp_extended_op;
5560 if (dwarf_get_CFA_name(op, &op_str) != DW_DLV_OK) {
5561 snprintf(unk_op, sizeof(unk_op), "[Unknown CFA: %#x]",
5565 printf(" %s", op_str);
5567 case DW_CFA_advance_loc:
5568 delta = oplist[i].fp_offset * caf;
5570 printf(": %ju to %08jx", (uintmax_t) delta,
5574 case DW_CFA_offset_extended:
5575 case DW_CFA_offset_extended_sf:
5576 delta = oplist[i].fp_offset * daf;
5577 printf(": r%u (%s) at cfa%+jd", oplist[i].fp_register,
5578 dwarf_regname(re, oplist[i].fp_register),
5581 case DW_CFA_restore:
5582 printf(": r%u (%s)", oplist[i].fp_register,
5583 dwarf_regname(re, oplist[i].fp_register));
5585 case DW_CFA_set_loc:
5586 pc = oplist[i].fp_offset;
5587 printf(": to %08jx", (uintmax_t) pc);
5589 case DW_CFA_advance_loc1:
5590 case DW_CFA_advance_loc2:
5591 case DW_CFA_advance_loc4:
5592 pc += oplist[i].fp_offset;
5593 printf(": %jd to %08jx", (intmax_t) oplist[i].fp_offset,
5596 case DW_CFA_def_cfa:
5597 printf(": r%u (%s) ofs %ju", oplist[i].fp_register,
5598 dwarf_regname(re, oplist[i].fp_register),
5599 (uintmax_t) oplist[i].fp_offset);
5601 case DW_CFA_def_cfa_sf:
5602 printf(": r%u (%s) ofs %jd", oplist[i].fp_register,
5603 dwarf_regname(re, oplist[i].fp_register),
5604 (intmax_t) (oplist[i].fp_offset * daf));
5606 case DW_CFA_def_cfa_register:
5607 printf(": r%u (%s)", oplist[i].fp_register,
5608 dwarf_regname(re, oplist[i].fp_register));
5610 case DW_CFA_def_cfa_offset:
5611 printf(": %ju", (uintmax_t) oplist[i].fp_offset);
5613 case DW_CFA_def_cfa_offset_sf:
5614 printf(": %jd", (intmax_t) (oplist[i].fp_offset * daf));
5622 dwarf_dealloc(dbg, oplist, DW_DLA_FRAME_BLOCK);
5626 get_regoff_str(struct readelf *re, Dwarf_Half reg, Dwarf_Addr off)
5630 if (reg == DW_FRAME_UNDEFINED_VAL || reg == DW_FRAME_REG_INITIAL_VALUE)
5631 snprintf(rs, sizeof(rs), "%c", 'u');
5632 else if (reg == DW_FRAME_CFA_COL)
5633 snprintf(rs, sizeof(rs), "c%+jd", (intmax_t) off);
5635 snprintf(rs, sizeof(rs), "%s%+jd", dwarf_regname(re, reg),
5642 dump_dwarf_frame_regtable(struct readelf *re, Dwarf_Fde fde, Dwarf_Addr pc,
5643 Dwarf_Unsigned func_len, Dwarf_Half cie_ra)
5646 Dwarf_Addr row_pc, end_pc, pre_pc, cur_pc;
5651 #define BIT_SET(v, n) (v[(n)>>3] |= 1U << ((n) & 7))
5652 #define BIT_CLR(v, n) (v[(n)>>3] &= ~(1U << ((n) & 7)))
5653 #define BIT_ISSET(v, n) (v[(n)>>3] & (1U << ((n) & 7)))
5654 #define RT(x) rt.rules[(x)]
5656 vec = calloc((DW_REG_TABLE_SIZE + 7) / 8, 1);
5658 err(EXIT_FAILURE, "calloc failed");
5660 pre_pc = ~((Dwarf_Addr) 0);
5662 end_pc = pc + func_len;
5663 for (; cur_pc < end_pc; cur_pc++) {
5664 if (dwarf_get_fde_info_for_all_regs(fde, cur_pc, &rt, &row_pc,
5665 &de) != DW_DLV_OK) {
5666 warnx("dwarf_get_fde_info_for_all_regs failed: %s\n",
5670 if (row_pc == pre_pc)
5673 for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
5674 if (rt.rules[i].dw_regnum != DW_FRAME_REG_INITIAL_VALUE)
5679 printf(" LOC CFA ");
5680 for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
5681 if (BIT_ISSET(vec, i)) {
5682 if ((Dwarf_Half) i == cie_ra)
5686 dwarf_regname(re, (unsigned int) i));
5691 pre_pc = ~((Dwarf_Addr) 0);
5693 end_pc = pc + func_len;
5694 for (; cur_pc < end_pc; cur_pc++) {
5695 if (dwarf_get_fde_info_for_all_regs(fde, cur_pc, &rt, &row_pc,
5696 &de) != DW_DLV_OK) {
5697 warnx("dwarf_get_fde_info_for_all_regs failed: %s\n",
5701 if (row_pc == pre_pc)
5704 printf("%08jx ", (uintmax_t) row_pc);
5705 printf("%-8s ", get_regoff_str(re, RT(0).dw_regnum,
5707 for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
5708 if (BIT_ISSET(vec, i)) {
5709 printf("%-5s", get_regoff_str(re,
5710 RT(i).dw_regnum, RT(i).dw_offset));
5727 dump_dwarf_frame_section(struct readelf *re, struct section *s, int alt)
5729 Dwarf_Cie *cie_list, cie, pre_cie;
5730 Dwarf_Fde *fde_list, fde;
5731 Dwarf_Off cie_offset, fde_offset;
5732 Dwarf_Unsigned cie_length, fde_instlen;
5733 Dwarf_Unsigned cie_caf, cie_daf, cie_instlen, func_len, fde_length;
5734 Dwarf_Signed cie_count, fde_count, cie_index;
5737 Dwarf_Small cie_version;
5738 Dwarf_Ptr fde_addr, fde_inst, cie_inst;
5743 printf("\nThe section %s contains:\n\n", s->name);
5745 if (!strcmp(s->name, ".debug_frame")) {
5747 if (dwarf_get_fde_list(re->dbg, &cie_list, &cie_count,
5748 &fde_list, &fde_count, &de) != DW_DLV_OK) {
5749 warnx("dwarf_get_fde_list failed: %s",
5753 } else if (!strcmp(s->name, ".eh_frame")) {
5755 if (dwarf_get_fde_list_eh(re->dbg, &cie_list, &cie_count,
5756 &fde_list, &fde_count, &de) != DW_DLV_OK) {
5757 warnx("dwarf_get_fde_list_eh failed: %s",
5765 for (i = 0; i < fde_count; i++) {
5766 if (dwarf_get_fde_n(fde_list, i, &fde, &de) != DW_DLV_OK) {
5767 warnx("dwarf_get_fde_n failed: %s", dwarf_errmsg(de));
5770 if (dwarf_get_cie_of_fde(fde, &cie, &de) != DW_DLV_OK) {
5771 warnx("dwarf_get_fde_n failed: %s", dwarf_errmsg(de));
5774 if (dwarf_get_fde_range(fde, &low_pc, &func_len, &fde_addr,
5775 &fde_length, &cie_offset, &cie_index, &fde_offset,
5776 &de) != DW_DLV_OK) {
5777 warnx("dwarf_get_fde_range failed: %s",
5781 if (dwarf_get_fde_instr_bytes(fde, &fde_inst, &fde_instlen,
5782 &de) != DW_DLV_OK) {
5783 warnx("dwarf_get_fde_instr_bytes failed: %s",
5787 if (pre_cie == NULL || cie != pre_cie) {
5789 if (dwarf_get_cie_info(cie, &cie_length, &cie_version,
5790 &cie_aug, &cie_caf, &cie_daf, &cie_ra,
5791 &cie_inst, &cie_instlen, &de) != DW_DLV_OK) {
5792 warnx("dwarf_get_cie_info failed: %s",
5796 printf("%08jx %08jx %8.8jx CIE",
5797 (uintmax_t) cie_offset,
5798 (uintmax_t) cie_length,
5799 (uintmax_t) (eh_frame ? 0 : ~0U));
5802 printf(" Version:\t\t\t%u\n", cie_version);
5803 printf(" Augmentation:\t\t\t\"");
5804 while ((c = *cie_aug++) != '\0')
5807 printf(" Code alignment factor:\t%ju\n",
5808 (uintmax_t) cie_caf);
5809 printf(" Data alignment factor:\t%jd\n",
5810 (intmax_t) cie_daf);
5811 printf(" Return address column:\t%ju\n",
5812 (uintmax_t) cie_ra);
5814 dump_dwarf_frame_inst(re, cie, cie_inst,
5815 cie_instlen, cie_caf, cie_daf, 0,
5820 while ((c = *cie_aug++) != '\0')
5823 printf(" cf=%ju df=%jd ra=%ju\n",
5824 (uintmax_t) cie_caf,
5825 (uintmax_t) cie_daf,
5826 (uintmax_t) cie_ra);
5827 dump_dwarf_frame_regtable(re, fde, low_pc, 1,
5832 printf("%08jx %08jx %08jx FDE cie=%08jx pc=%08jx..%08jx\n",
5833 (uintmax_t) fde_offset, (uintmax_t) fde_length,
5834 (uintmax_t) cie_offset,
5835 (uintmax_t) (eh_frame ? fde_offset + 4 - cie_offset :
5837 (uintmax_t) low_pc, (uintmax_t) (low_pc + func_len));
5839 dump_dwarf_frame_inst(re, cie, fde_inst, fde_instlen,
5840 cie_caf, cie_daf, low_pc, re->dbg);
5842 dump_dwarf_frame_regtable(re, fde, low_pc, func_len,
5849 dump_dwarf_frame(struct readelf *re, int alt)
5854 (void) dwarf_set_frame_cfa_value(re->dbg, DW_FRAME_CFA_COL);
5856 for (i = 0; (size_t) i < re->shnum; i++) {
5858 if (s->name != NULL && (!strcmp(s->name, ".debug_frame") ||
5859 !strcmp(s->name, ".eh_frame")))
5860 dump_dwarf_frame_section(re, s, alt);
5865 dump_dwarf_str(struct readelf *re)
5870 int elferr, end, i, j;
5872 printf("\nContents of section .debug_str:\n");
5875 for (i = 0; (size_t) i < re->shnum; i++) {
5877 if (s->name != NULL && !strcmp(s->name, ".debug_str"))
5880 if ((size_t) i >= re->shnum)
5884 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
5885 elferr = elf_errno();
5887 warnx("elf_getdata failed: %s", elf_errmsg(-1));
5893 for (i = 0, p = d->d_buf; (size_t) i < d->d_size; i += 16) {
5894 printf(" 0x%08x", (unsigned int) i);
5895 if ((size_t) i + 16 > d->d_size)
5899 for (j = i; j < i + 16; j++) {
5900 if ((j - i) % 4 == 0)
5906 printf("%02x", (uint8_t) p[j]);
5909 for (j = i; j < end; j++) {
5922 Dwarf_Attribute la_at;
5923 Dwarf_Unsigned la_off;
5924 Dwarf_Unsigned la_lowpc;
5925 Dwarf_Half la_cu_psize;
5926 Dwarf_Half la_cu_osize;
5927 Dwarf_Half la_cu_ver;
5928 TAILQ_ENTRY(loc_at) la_next;
5931 static TAILQ_HEAD(, loc_at) lalist = TAILQ_HEAD_INITIALIZER(lalist);
5934 search_loclist_at(struct readelf *re, Dwarf_Die die, Dwarf_Unsigned lowpc)
5936 Dwarf_Attribute *attr_list;
5940 Dwarf_Signed attr_count;
5941 Dwarf_Half attr, form;
5944 struct loc_at *la, *nla;
5947 is_info = dwarf_get_die_infotypes_flag(die);
5949 if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
5951 if (ret == DW_DLV_ERROR)
5952 warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
5955 for (i = 0; i < attr_count; i++) {
5956 if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
5957 warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
5960 if (attr != DW_AT_location &&
5961 attr != DW_AT_string_length &&
5962 attr != DW_AT_return_addr &&
5963 attr != DW_AT_data_member_location &&
5964 attr != DW_AT_frame_base &&
5965 attr != DW_AT_segment &&
5966 attr != DW_AT_static_link &&
5967 attr != DW_AT_use_location &&
5968 attr != DW_AT_vtable_elem_location)
5970 if (dwarf_whatform(attr_list[i], &form, &de) != DW_DLV_OK) {
5971 warnx("dwarf_whatform failed: %s", dwarf_errmsg(de));
5974 if (form == DW_FORM_data4 || form == DW_FORM_data8) {
5975 if (dwarf_formudata(attr_list[i], &off, &de) !=
5977 warnx("dwarf_formudata failed: %s",
5981 } else if (form == DW_FORM_sec_offset) {
5982 if (dwarf_global_formref(attr_list[i], &ref, &de) !=
5984 warnx("dwarf_global_formref failed: %s",
5992 TAILQ_FOREACH(la, &lalist, la_next) {
5993 if (off == la->la_off)
5995 if (off < la->la_off) {
5996 if ((nla = malloc(sizeof(*nla))) == NULL)
5997 err(EXIT_FAILURE, "malloc failed");
5998 nla->la_at = attr_list[i];
6000 nla->la_lowpc = lowpc;
6001 nla->la_cu_psize = re->cu_psize;
6002 nla->la_cu_osize = re->cu_osize;
6003 nla->la_cu_ver = re->cu_ver;
6004 TAILQ_INSERT_BEFORE(la, nla, la_next);
6009 if ((nla = malloc(sizeof(*nla))) == NULL)
6010 err(EXIT_FAILURE, "malloc failed");
6011 nla->la_at = attr_list[i];
6013 nla->la_lowpc = lowpc;
6014 nla->la_cu_psize = re->cu_psize;
6015 nla->la_cu_osize = re->cu_osize;
6016 nla->la_cu_ver = re->cu_ver;
6017 TAILQ_INSERT_TAIL(&lalist, nla, la_next);
6022 /* Search children. */
6023 ret = dwarf_child(die, &ret_die, &de);
6024 if (ret == DW_DLV_ERROR)
6025 warnx("dwarf_child: %s", dwarf_errmsg(de));
6026 else if (ret == DW_DLV_OK)
6027 search_loclist_at(re, ret_die, lowpc);
6029 /* Search sibling. */
6030 ret = dwarf_siblingof_b(re->dbg, die, &ret_die, is_info, &de);
6031 if (ret == DW_DLV_ERROR)
6032 warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
6033 else if (ret == DW_DLV_OK)
6034 search_loclist_at(re, ret_die, lowpc);
6038 dump_dwarf_loc(struct readelf *re, Dwarf_Loc *lr)
6045 if (dwarf_get_OP_name(lr->lr_atom, &op_str) !=
6047 snprintf(unk_op, sizeof(unk_op),
6048 "[Unknown OP: %#x]", lr->lr_atom);
6052 printf("%s", op_str);
6054 switch (lr->lr_atom) {
6087 printf(" (%s)", dwarf_regname(re, lr->lr_atom - DW_OP_reg0));
6150 case DW_OP_push_object_address:
6151 case DW_OP_form_tls_address:
6152 case DW_OP_call_frame_cfa:
6153 case DW_OP_stack_value:
6154 case DW_OP_GNU_push_tls_address:
6155 case DW_OP_GNU_uninit:
6160 case DW_OP_deref_size:
6161 case DW_OP_xderef_size:
6168 case DW_OP_plus_uconst:
6171 printf(": %ju", (uintmax_t)
6180 printf(": %jd", (intmax_t)
6216 printf(" (%s): %jd",
6217 dwarf_regname(re, lr->lr_atom - DW_OP_breg0),
6218 (intmax_t) lr->lr_number);
6222 printf(": %jd", (intmax_t)
6227 printf(": %ju (%s) %jd",
6228 (uintmax_t) lr->lr_number,
6229 dwarf_regname(re, (unsigned int) lr->lr_number),
6230 (intmax_t) lr->lr_number2);
6234 case DW_OP_GNU_encoded_addr:
6235 printf(": %#jx", (uintmax_t)
6239 case DW_OP_GNU_implicit_pointer:
6240 printf(": <0x%jx> %jd", (uintmax_t) lr->lr_number,
6241 (intmax_t) lr->lr_number2);
6244 case DW_OP_implicit_value:
6245 printf(": %ju byte block:", (uintmax_t) lr->lr_number);
6246 b = (uint8_t *)(uintptr_t) lr->lr_number2;
6247 for (i = 0; (Dwarf_Unsigned) i < lr->lr_number; i++)
6248 printf(" %x", b[i]);
6251 case DW_OP_GNU_entry_value:
6253 dump_dwarf_block(re, (uint8_t *)(uintptr_t) lr->lr_number2,
6258 case DW_OP_GNU_const_type:
6259 printf(": <0x%jx> ", (uintmax_t) lr->lr_number);
6260 b = (uint8_t *)(uintptr_t) lr->lr_number2;
6262 for (i = 1; (uint8_t) i < n; i++)
6263 printf(" %x", b[i]);
6266 case DW_OP_GNU_regval_type:
6267 printf(": %ju (%s) <0x%jx>", (uintmax_t) lr->lr_number,
6268 dwarf_regname(re, (unsigned int) lr->lr_number),
6269 (uintmax_t) lr->lr_number2);
6272 case DW_OP_GNU_convert:
6273 case DW_OP_GNU_deref_type:
6274 case DW_OP_GNU_parameter_ref:
6275 case DW_OP_GNU_reinterpret:
6276 printf(": <0x%jx>", (uintmax_t) lr->lr_number);
6285 dump_dwarf_block(struct readelf *re, uint8_t *b, Dwarf_Unsigned len)
6287 Dwarf_Locdesc *llbuf;
6292 if (dwarf_loclist_from_expr_b(re->dbg, b, len, re->cu_psize,
6293 re->cu_osize, re->cu_ver, &llbuf, &lcnt, &de) != DW_DLV_OK) {
6294 warnx("dwarf_loclist_form_expr_b: %s", dwarf_errmsg(de));
6298 for (i = 0; (Dwarf_Half) i < llbuf->ld_cents; i++) {
6299 dump_dwarf_loc(re, &llbuf->ld_s[i]);
6300 if (i < llbuf->ld_cents - 1)
6304 dwarf_dealloc(re->dbg, llbuf->ld_s, DW_DLA_LOC_BLOCK);
6305 dwarf_dealloc(re->dbg, llbuf, DW_DLA_LOCDESC);
6309 dump_dwarf_loclist(struct readelf *re)
6312 Dwarf_Locdesc **llbuf;
6313 Dwarf_Unsigned lowpc;
6315 Dwarf_Half tag, version, pointer_size, off_size;
6318 int i, j, ret, has_content;
6320 /* Search .debug_info section. */
6321 while ((ret = dwarf_next_cu_header_b(re->dbg, NULL, &version, NULL,
6322 &pointer_size, &off_size, NULL, NULL, &de)) == DW_DLV_OK) {
6323 set_cu_context(re, pointer_size, off_size, version);
6325 if (dwarf_siblingof(re->dbg, die, &die, &de) != DW_DLV_OK)
6327 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
6328 warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
6331 /* XXX: What about DW_TAG_partial_unit? */
6333 if (tag == DW_TAG_compile_unit) {
6334 if (dwarf_attrval_unsigned(die, DW_AT_low_pc,
6335 &lowpc, &de) != DW_DLV_OK)
6339 /* Search attributes for reference to .debug_loc section. */
6340 search_loclist_at(re, die, lowpc);
6342 if (ret == DW_DLV_ERROR)
6343 warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
6345 /* Search .debug_types section. */
6347 while ((ret = dwarf_next_cu_header_c(re->dbg, 0, NULL,
6348 &version, NULL, &pointer_size, &off_size, NULL, NULL,
6349 NULL, NULL, &de)) == DW_DLV_OK) {
6350 set_cu_context(re, pointer_size, off_size, version);
6352 if (dwarf_siblingof(re->dbg, die, &die, &de) !=
6355 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
6356 warnx("dwarf_tag failed: %s",
6362 if (tag == DW_TAG_type_unit) {
6363 if (dwarf_attrval_unsigned(die, DW_AT_low_pc,
6364 &lowpc, &de) != DW_DLV_OK)
6369 * Search attributes for reference to .debug_loc
6372 search_loclist_at(re, die, lowpc);
6374 if (ret == DW_DLV_ERROR)
6375 warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
6376 } while (dwarf_next_types_section(re->dbg, &de) == DW_DLV_OK);
6378 if (TAILQ_EMPTY(&lalist))
6382 TAILQ_FOREACH(la, &lalist, la_next) {
6383 if ((ret = dwarf_loclist_n(la->la_at, &llbuf, &lcnt, &de)) !=
6385 if (ret != DW_DLV_NO_ENTRY)
6386 warnx("dwarf_loclist_n failed: %s",
6392 printf("\nContents of section .debug_loc:\n");
6393 printf(" Offset Begin End Expression\n");
6395 set_cu_context(re, la->la_cu_psize, la->la_cu_osize,
6397 for (i = 0; i < lcnt; i++) {
6398 printf(" %8.8jx ", (uintmax_t) la->la_off);
6399 if (llbuf[i]->ld_lopc == 0 && llbuf[i]->ld_hipc == 0) {
6400 printf("<End of list>\n");
6404 /* TODO: handle base selection entry. */
6406 printf("%8.8jx %8.8jx ",
6407 (uintmax_t) (la->la_lowpc + llbuf[i]->ld_lopc),
6408 (uintmax_t) (la->la_lowpc + llbuf[i]->ld_hipc));
6411 for (j = 0; (Dwarf_Half) j < llbuf[i]->ld_cents; j++) {
6412 dump_dwarf_loc(re, &llbuf[i]->ld_s[j]);
6413 if (j < llbuf[i]->ld_cents - 1)
6418 if (llbuf[i]->ld_lopc == llbuf[i]->ld_hipc)
6419 printf(" (start == end)");
6422 for (i = 0; i < lcnt; i++) {
6423 dwarf_dealloc(re->dbg, llbuf[i]->ld_s,
6425 dwarf_dealloc(re->dbg, llbuf[i], DW_DLA_LOCDESC);
6427 dwarf_dealloc(re->dbg, llbuf, DW_DLA_LIST);
6431 printf("\nSection '.debug_loc' has no debugging data.\n");
6435 * Retrieve a string using string table section index and the string offset.
6438 get_string(struct readelf *re, int strtab, size_t off)
6442 if ((name = elf_strptr(re->elf, strtab, off)) == NULL)
6449 * Retrieve the name of a symbol using the section index of the symbol
6450 * table and the index of the symbol within that table.
6453 get_symbol_name(struct readelf *re, int symtab, int i)
6461 s = &re->sl[symtab];
6462 if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM)
6465 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
6466 elferr = elf_errno();
6468 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
6471 if (gelf_getsym(data, i, &sym) != &sym)
6473 /* Return section name for STT_SECTION symbol. */
6474 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION) {
6475 if (sym.st_shndx < re->shnum &&
6476 re->sl[sym.st_shndx].name != NULL)
6477 return (re->sl[sym.st_shndx].name);
6480 if (s->link >= re->shnum ||
6481 (name = elf_strptr(re->elf, s->link, sym.st_name)) == NULL)
6488 get_symbol_value(struct readelf *re, int symtab, int i)
6495 s = &re->sl[symtab];
6496 if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM)
6499 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
6500 elferr = elf_errno();
6502 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
6505 if (gelf_getsym(data, i, &sym) != &sym)
6508 return (sym.st_value);
6512 hex_dump(struct readelf *re)
6521 for (i = 1; (size_t) i < re->shnum; i++) {
6523 if (find_dumpop(re, (size_t) i, s->name, HEX_DUMP, -1) == NULL)
6526 if ((d = elf_getdata(s->scn, NULL)) == NULL &&
6527 (d = elf_rawdata(s->scn, NULL)) == NULL) {
6528 elferr = elf_errno();
6530 warnx("elf_getdata failed: %s",
6531 elf_errmsg(elferr));
6535 if (d->d_size <= 0 || d->d_buf == NULL) {
6536 printf("\nSection '%s' has no data to dump.\n",
6543 printf("\nHex dump of section '%s':\n", s->name);
6545 printf(" 0x%8.8jx ", (uintmax_t)addr);
6546 nbytes = sz > 16? 16 : sz;
6547 for (j = 0; j < 16; j++) {
6548 if ((size_t)j < nbytes)
6549 printf("%2.2x", buf[j]);
6555 for (j = 0; (size_t)j < nbytes; j++) {
6556 if (isprint(buf[j]))
6557 printf("%c", buf[j]);
6570 str_dump(struct readelf *re)
6574 unsigned char *start, *end, *buf_end;
6576 int i, j, elferr, found;
6578 for (i = 1; (size_t) i < re->shnum; i++) {
6580 if (find_dumpop(re, (size_t) i, s->name, STR_DUMP, -1) == NULL)
6583 if ((d = elf_getdata(s->scn, NULL)) == NULL &&
6584 (d = elf_rawdata(s->scn, NULL)) == NULL) {
6585 elferr = elf_errno();
6587 warnx("elf_getdata failed: %s",
6588 elf_errmsg(elferr));
6592 if (d->d_size <= 0 || d->d_buf == NULL) {
6593 printf("\nSection '%s' has no data to dump.\n",
6597 buf_end = (unsigned char *) d->d_buf + d->d_size;
6598 start = (unsigned char *) d->d_buf;
6600 printf("\nString dump of section '%s':\n", s->name);
6602 while (start < buf_end && !isprint(*start))
6604 if (start >= buf_end)
6607 while (end < buf_end && isprint(*end))
6610 (long) (start - (unsigned char *) d->d_buf));
6612 for (j = 0; (unsigned int) j < len; j++)
6621 printf(" No strings found in this section.");
6627 load_sections(struct readelf *re)
6633 size_t shstrndx, ndx;
6636 /* Allocate storage for internal section list. */
6637 if (!elf_getshnum(re->elf, &re->shnum)) {
6638 warnx("elf_getshnum failed: %s", elf_errmsg(-1));
6643 if ((re->sl = calloc(re->shnum, sizeof(*re->sl))) == NULL)
6644 err(EXIT_FAILURE, "calloc failed");
6646 /* Get the index of .shstrtab section. */
6647 if (!elf_getshstrndx(re->elf, &shstrndx)) {
6648 warnx("elf_getshstrndx failed: %s", elf_errmsg(-1));
6652 if ((scn = elf_getscn(re->elf, 0)) == NULL)
6657 if (gelf_getshdr(scn, &sh) == NULL) {
6658 warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
6662 if ((name = elf_strptr(re->elf, shstrndx, sh.sh_name)) == NULL) {
6666 if ((ndx = elf_ndxscn(scn)) == SHN_UNDEF) {
6667 if ((elferr = elf_errno()) != 0) {
6668 warnx("elf_ndxscn failed: %s",
6669 elf_errmsg(elferr));
6673 if (ndx >= re->shnum) {
6674 warnx("section index of '%s' out of range", name);
6677 if (sh.sh_link >= re->shnum)
6678 warnx("section link %llu of '%s' out of range",
6679 (unsigned long long)sh.sh_link, name);
6683 s->off = sh.sh_offset;
6685 s->entsize = sh.sh_entsize;
6686 s->align = sh.sh_addralign;
6687 s->type = sh.sh_type;
6688 s->flags = sh.sh_flags;
6689 s->addr = sh.sh_addr;
6690 s->link = sh.sh_link;
6691 s->info = sh.sh_info;
6692 } while ((scn = elf_nextscn(re->elf, scn)) != NULL);
6693 elferr = elf_errno();
6695 warnx("elf_nextscn failed: %s", elf_errmsg(elferr));
6699 unload_sections(struct readelf *re)
6702 if (re->sl != NULL) {
6712 if (re->ver != NULL) {
6720 dump_elf(struct readelf *re)
6723 /* Fetch ELF header. No need to continue if it fails. */
6724 if (gelf_getehdr(re->elf, &re->ehdr) == NULL) {
6725 warnx("gelf_getehdr failed: %s", elf_errmsg(-1));
6728 if ((re->ec = gelf_getclass(re->elf)) == ELFCLASSNONE) {
6729 warnx("gelf_getclass failed: %s", elf_errmsg(-1));
6732 if (re->ehdr.e_ident[EI_DATA] == ELFDATA2MSB) {
6733 re->dw_read = _read_msb;
6734 re->dw_decode = _decode_msb;
6736 re->dw_read = _read_lsb;
6737 re->dw_decode = _decode_lsb;
6740 if (re->options & ~RE_H)
6742 if ((re->options & RE_VV) || (re->options & RE_S))
6744 if (re->options & RE_H)
6746 if (re->options & RE_L)
6748 if (re->options & RE_SS)
6750 if (re->options & RE_G)
6751 dump_section_groups(re);
6752 if (re->options & RE_D)
6754 if (re->options & RE_R)
6756 if (re->options & RE_S)
6758 if (re->options & RE_N)
6760 if (re->options & RE_II)
6762 if (re->options & RE_X)
6764 if (re->options & RE_P)
6766 if (re->options & RE_VV)
6768 if (re->options & RE_AA)
6769 dump_arch_specific_info(re);
6770 if (re->options & RE_W)
6772 if (re->options & ~RE_H)
6773 unload_sections(re);
6777 dump_dwarf(struct readelf *re)
6779 struct loc_at *la, *_la;
6783 if (dwarf_elf_init(re->elf, DW_DLC_READ, NULL, NULL, &re->dbg, &de)) {
6784 if ((error = dwarf_errno(de)) != DW_DLE_DEBUG_INFO_NULL)
6785 errx(EXIT_FAILURE, "dwarf_elf_init failed: %s",
6791 dump_dwarf_abbrev(re);
6793 dump_dwarf_line(re);
6794 if (re->dop & DW_LL)
6795 dump_dwarf_line_decoded(re);
6796 if (re->dop & DW_I) {
6797 dump_dwarf_info(re, 0);
6798 dump_dwarf_info(re, 1);
6801 dump_dwarf_pubnames(re);
6803 dump_dwarf_aranges(re);
6804 if (re->dop & DW_RR)
6805 dump_dwarf_ranges(re);
6807 dump_dwarf_macinfo(re);
6809 dump_dwarf_frame(re, 0);
6810 else if (re->dop & DW_FF)
6811 dump_dwarf_frame(re, 1);
6815 dump_dwarf_loclist(re);
6817 TAILQ_FOREACH_SAFE(la, &lalist, la_next, _la) {
6818 TAILQ_REMOVE(&lalist, la, la_next);
6822 dwarf_finish(re->dbg, &de);
6826 dump_ar(struct readelf *re, int fd)
6838 if (re->options & RE_C) {
6839 if ((arsym = elf_getarsym(re->ar, &sz)) == NULL) {
6840 warnx("elf_getarsym() failed: %s", elf_errmsg(-1));
6841 goto process_members;
6843 printf("Index of archive %s: (%ju entries)\n", re->filename,
6844 (uintmax_t) sz - 1);
6846 for (i = 0; (size_t) i < sz; i++) {
6847 if (arsym[i].as_name == NULL)
6849 if (arsym[i].as_off != off) {
6850 off = arsym[i].as_off;
6851 if (elf_rand(re->ar, off) != off) {
6852 warnx("elf_rand() failed: %s",
6856 if ((e = elf_begin(fd, ELF_C_READ, re->ar)) ==
6858 warnx("elf_begin() failed: %s",
6862 if ((arhdr = elf_getarhdr(e)) == NULL) {
6863 warnx("elf_getarhdr() failed: %s",
6868 printf("Binary %s(%s) contains:\n",
6869 re->filename, arhdr->ar_name);
6871 printf("\t%s\n", arsym[i].as_name);
6873 if (elf_rand(re->ar, SARMAG) != SARMAG) {
6874 warnx("elf_rand() failed: %s", elf_errmsg(-1));
6881 if ((re->options & ~RE_C) == 0)
6885 while ((re->elf = elf_begin(fd, cmd, re->ar)) != NULL) {
6886 if ((arhdr = elf_getarhdr(re->elf)) == NULL) {
6887 warnx("elf_getarhdr() failed: %s", elf_errmsg(-1));
6890 if (strcmp(arhdr->ar_name, "/") == 0 ||
6891 strcmp(arhdr->ar_name, "//") == 0 ||
6892 strcmp(arhdr->ar_name, "__.SYMDEF") == 0)
6894 printf("\nFile: %s(%s)\n", re->filename, arhdr->ar_name);
6898 cmd = elf_next(re->elf);
6905 dump_object(struct readelf *re)
6909 if ((fd = open(re->filename, O_RDONLY)) == -1) {
6910 warn("open %s failed", re->filename);
6914 if ((re->flags & DISPLAY_FILENAME) != 0)
6915 printf("\nFile: %s\n", re->filename);
6917 if ((re->elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
6918 warnx("elf_begin() failed: %s", elf_errmsg(-1));
6922 switch (elf_kind(re->elf)) {
6924 warnx("Not an ELF file.");
6933 warnx("Internal: libelf returned unknown elf kind.");
6941 add_dumpop(struct readelf *re, size_t si, const char *sn, int op, int t)
6945 if ((d = find_dumpop(re, si, sn, -1, t)) == NULL) {
6946 if ((d = calloc(1, sizeof(*d))) == NULL)
6947 err(EXIT_FAILURE, "calloc failed");
6948 if (t == DUMP_BY_INDEX)
6954 STAILQ_INSERT_TAIL(&re->v_dumpop, d, dumpop_list);
6959 static struct dumpop *
6960 find_dumpop(struct readelf *re, size_t si, const char *sn, int op, int t)
6964 STAILQ_FOREACH(d, &re->v_dumpop, dumpop_list) {
6965 if ((op == -1 || op & d->op) &&
6966 (t == -1 || (unsigned) t == d->type)) {
6967 if ((d->type == DUMP_BY_INDEX && d->u.si == si) ||
6968 (d->type == DUMP_BY_NAME && !strcmp(d->u.sn, sn)))
6981 {"rawline", 'l', DW_L},
6982 {"decodedline", 'L', DW_LL},
6983 {"info", 'i', DW_I},
6984 {"abbrev", 'a', DW_A},
6985 {"pubnames", 'p', DW_P},
6986 {"aranges", 'r', DW_R},
6987 {"ranges", 'r', DW_R},
6988 {"Ranges", 'R', DW_RR},
6989 {"macro", 'm', DW_M},
6990 {"frames", 'f', DW_F},
6991 {"frames-interp", 'F', DW_FF},
6998 parse_dwarf_op_short(struct readelf *re, const char *op)
7003 re->dop |= DW_DEFAULT_OPTIONS;
7007 for (; *op != '\0'; op++) {
7008 for (i = 0; dwarf_op[i].ln != NULL; i++) {
7009 if (dwarf_op[i].sn == *op) {
7010 re->dop |= dwarf_op[i].value;
7018 parse_dwarf_op_long(struct readelf *re, const char *op)
7020 char *p, *token, *bp;
7024 re->dop |= DW_DEFAULT_OPTIONS;
7028 if ((p = strdup(op)) == NULL)
7029 err(EXIT_FAILURE, "strdup failed");
7032 while ((token = strsep(&p, ",")) != NULL) {
7033 for (i = 0; dwarf_op[i].ln != NULL; i++) {
7034 if (!strcmp(token, dwarf_op[i].ln)) {
7035 re->dop |= dwarf_op[i].value;
7045 _read_lsb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read)
7050 src = (uint8_t *) d->d_buf + *offsetp;
7053 switch (bytes_to_read) {
7055 ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
7056 ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
7059 ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
7062 ret |= ((uint64_t) src[1]) << 8;
7071 *offsetp += bytes_to_read;
7077 _read_msb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read)
7082 src = (uint8_t *) d->d_buf + *offsetp;
7084 switch (bytes_to_read) {
7089 ret = src[1] | ((uint64_t) src[0]) << 8;
7092 ret = src[3] | ((uint64_t) src[2]) << 8;
7093 ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24;
7096 ret = src[7] | ((uint64_t) src[6]) << 8;
7097 ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24;
7098 ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40;
7099 ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56;
7105 *offsetp += bytes_to_read;
7111 _decode_lsb(uint8_t **data, int bytes_to_read)
7119 switch (bytes_to_read) {
7121 ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
7122 ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
7125 ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
7128 ret |= ((uint64_t) src[1]) << 8;
7137 *data += bytes_to_read;
7143 _decode_msb(uint8_t **data, int bytes_to_read)
7151 switch (bytes_to_read) {
7156 ret = src[1] | ((uint64_t) src[0]) << 8;
7159 ret = src[3] | ((uint64_t) src[2]) << 8;
7160 ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24;
7163 ret = src[7] | ((uint64_t) src[6]) << 8;
7164 ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24;
7165 ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40;
7166 ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56;
7173 *data += bytes_to_read;
7179 _decode_sleb128(uint8_t **dp, uint8_t *dpe)
7191 ret |= ((b & 0x7f) << shift);
7193 } while ((b & 0x80) != 0);
7195 if (shift < 32 && (b & 0x40) != 0)
7196 ret |= (-1 << shift);
7204 _decode_uleb128(uint8_t **dp, uint8_t *dpe)
7216 ret |= ((b & 0x7f) << shift);
7218 } while ((b & 0x80) != 0);
7226 readelf_version(void)
7228 (void) printf("%s (%s)\n", ELFTC_GETPROGNAME(),
7233 #define USAGE_MESSAGE "\
7234 Usage: %s [options] file...\n\
7235 Display information about ELF objects and ar(1) archives.\n\n\
7237 -a | --all Equivalent to specifying options '-dhIlrsASV'.\n\
7238 -c | --archive-index Print the archive symbol table for archives.\n\
7239 -d | --dynamic Print the contents of SHT_DYNAMIC sections.\n\
7240 -e | --headers Print all headers in the object.\n\
7241 -g | --section-groups Print the contents of the section groups.\n\
7242 -h | --file-header Print the file header for the object.\n\
7243 -l | --program-headers Print the PHDR table for the object.\n\
7244 -n | --notes Print the contents of SHT_NOTE sections.\n\
7245 -p INDEX | --string-dump=INDEX\n\
7246 Print the contents of section at index INDEX.\n\
7247 -r | --relocs Print relocation information.\n\
7248 -s | --syms | --symbols Print symbol tables.\n\
7249 -t | --section-details Print additional information about sections.\n\
7250 -v | --version Print a version identifier and exit.\n\
7251 -w[afilmoprsFLR] | --debug-dump={abbrev,aranges,decodedline,frames,\n\
7252 frames-interp,info,loc,macro,pubnames,\n\
7253 ranges,Ranges,rawline,str}\n\
7254 Display DWARF information.\n\
7255 -x INDEX | --hex-dump=INDEX\n\
7256 Display contents of a section as hexadecimal.\n\
7257 -A | --arch-specific (accepted, but ignored)\n\
7258 -D | --use-dynamic Print the symbol table specified by the DT_SYMTAB\n\
7259 entry in the \".dynamic\" section.\n\
7260 -H | --help Print a help message.\n\
7261 -I | --histogram Print information on bucket list lengths for \n\
7263 -N | --full-section-name (accepted, but ignored)\n\
7264 -S | --sections | --section-headers\n\
7265 Print information about section headers.\n\
7266 -V | --version-info Print symbol versoning information.\n\
7267 -W | --wide Print information without wrapping long lines.\n"
7271 readelf_usage(int status)
7273 fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());
7278 main(int argc, char **argv)
7280 struct readelf *re, re_storage;
7286 memset(re, 0, sizeof(*re));
7287 STAILQ_INIT(&re->v_dumpop);
7289 while ((opt = getopt_long(argc, argv, "AacDdegHhIi:lNnp:rSstuVvWw::x:",
7290 longopts, NULL)) != -1) {
7293 readelf_usage(EXIT_SUCCESS);
7296 re->options |= RE_AA;
7299 re->options |= RE_AA | RE_D | RE_G | RE_H | RE_II |
7300 RE_L | RE_R | RE_SS | RE_S | RE_VV;
7303 re->options |= RE_C;
7306 re->options |= RE_DD;
7309 re->options |= RE_D;
7312 re->options |= RE_H | RE_L | RE_SS;
7315 re->options |= RE_G;
7318 readelf_usage(EXIT_SUCCESS);
7321 re->options |= RE_H;
7324 re->options |= RE_II;
7327 /* Not implemented yet. */
7330 re->options |= RE_L;
7333 re->options |= RE_NN;
7336 re->options |= RE_N;
7339 re->options |= RE_P;
7340 si = strtoul(optarg, &ep, 10);
7342 add_dumpop(re, (size_t) si, NULL, STR_DUMP,
7345 add_dumpop(re, 0, optarg, STR_DUMP,
7349 re->options |= RE_R;
7352 re->options |= RE_SS;
7355 re->options |= RE_S;
7358 re->options |= RE_T;
7361 re->options |= RE_U;
7364 re->options |= RE_VV;
7370 re->options |= RE_WW;
7373 re->options |= RE_W;
7374 parse_dwarf_op_short(re, optarg);
7377 re->options |= RE_X;
7378 si = strtoul(optarg, &ep, 10);
7380 add_dumpop(re, (size_t) si, NULL, HEX_DUMP,
7383 add_dumpop(re, 0, optarg, HEX_DUMP,
7386 case OPTION_DEBUG_DUMP:
7387 re->options |= RE_W;
7388 parse_dwarf_op_long(re, optarg);
7395 if (argc == 0 || re->options == 0)
7396 readelf_usage(EXIT_FAILURE);
7399 re->flags |= DISPLAY_FILENAME;
7401 if (elf_version(EV_CURRENT) == EV_NONE)
7402 errx(EXIT_FAILURE, "ELF library initialization failed: %s",
7405 for (i = 0; i < argc; i++) {
7406 re->filename = argv[i];