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 3223 2015-05-25 20:37:57Z emaste $");
55 #define RE_AA 0x00000001
56 #define RE_C 0x00000002
57 #define RE_DD 0x00000004
58 #define RE_D 0x00000008
59 #define RE_G 0x00000010
60 #define RE_H 0x00000020
61 #define RE_II 0x00000040
62 #define RE_I 0x00000080
63 #define RE_L 0x00000100
64 #define RE_NN 0x00000200
65 #define RE_N 0x00000400
66 #define RE_P 0x00000800
67 #define RE_R 0x00001000
68 #define RE_SS 0x00002000
69 #define RE_S 0x00004000
70 #define RE_T 0x00008000
71 #define RE_U 0x00010000
72 #define RE_VV 0x00020000
73 #define RE_WW 0x00040000
74 #define RE_W 0x00080000
75 #define RE_X 0x00100000
80 #define DW_A 0x00000001
81 #define DW_FF 0x00000002
82 #define DW_F 0x00000004
83 #define DW_I 0x00000008
84 #define DW_LL 0x00000010
85 #define DW_L 0x00000020
86 #define DW_M 0x00000040
87 #define DW_O 0x00000080
88 #define DW_P 0x00000100
89 #define DW_RR 0x00000200
90 #define DW_R 0x00000400
91 #define DW_S 0x00000800
93 #define DW_DEFAULT_OPTIONS (DW_A | DW_F | DW_I | DW_L | DW_O | DW_P | \
97 * readelf(1) run control flags.
99 #define DISPLAY_FILENAME 0x0001
102 * Internal data structure for sections.
105 const char *name; /* section name */
106 Elf_Scn *scn; /* section scn */
107 uint64_t off; /* section offset */
108 uint64_t sz; /* section size */
109 uint64_t entsize; /* section entsize */
110 uint64_t align; /* section alignment */
111 uint64_t type; /* section type */
112 uint64_t flags; /* section flags */
113 uint64_t addr; /* section virtual addr */
114 uint32_t link; /* section link ndx */
115 uint32_t info; /* section info ndx */
120 size_t si; /* section index */
121 const char *sn; /* section name */
126 } type; /* dump type */
127 #define HEX_DUMP 0x0001
128 #define STR_DUMP 0x0002
129 int op; /* dump operation */
130 STAILQ_ENTRY(dumpop) dumpop_list;
139 * Structure encapsulates the global data for readelf(1).
142 const char *filename; /* current processing file. */
143 int options; /* command line options. */
144 int flags; /* run control flags. */
145 int dop; /* dwarf dump options. */
146 Elf *elf; /* underlying ELF descriptor. */
147 Elf *ar; /* archive ELF descriptor. */
148 Dwarf_Debug dbg; /* DWARF handle. */
149 Dwarf_Half cu_psize; /* DWARF CU pointer size. */
150 Dwarf_Half cu_osize; /* DWARF CU offset size. */
151 Dwarf_Half cu_ver; /* DWARF CU version. */
152 GElf_Ehdr ehdr; /* ELF header. */
153 int ec; /* ELF class. */
154 size_t shnum; /* #sections. */
155 struct section *vd_s; /* Verdef section. */
156 struct section *vn_s; /* Verneed section. */
157 struct section *vs_s; /* Versym section. */
158 uint16_t *vs; /* Versym array. */
159 int vs_sz; /* Versym array size. */
160 struct symver *ver; /* Version array. */
161 int ver_sz; /* Size of version array. */
162 struct section *sl; /* list of sections. */
163 STAILQ_HEAD(, dumpop) v_dumpop; /* list of dump ops. */
164 uint64_t (*dw_read)(Elf_Data *, uint64_t *, int);
165 uint64_t (*dw_decode)(uint8_t **, int);
173 static struct option longopts[] = {
174 {"all", no_argument, NULL, 'a'},
175 {"arch-specific", no_argument, NULL, 'A'},
176 {"archive-index", no_argument, NULL, 'c'},
177 {"debug-dump", optional_argument, NULL, OPTION_DEBUG_DUMP},
178 {"dynamic", no_argument, NULL, 'd'},
179 {"file-header", no_argument, NULL, 'h'},
180 {"full-section-name", no_argument, NULL, 'N'},
181 {"headers", no_argument, NULL, 'e'},
182 {"help", no_argument, 0, 'H'},
183 {"hex-dump", required_argument, NULL, 'x'},
184 {"histogram", no_argument, NULL, 'I'},
185 {"notes", no_argument, NULL, 'n'},
186 {"program-headers", no_argument, NULL, 'l'},
187 {"relocs", no_argument, NULL, 'r'},
188 {"sections", no_argument, NULL, 'S'},
189 {"section-headers", no_argument, NULL, 'S'},
190 {"section-groups", no_argument, NULL, 'g'},
191 {"section-details", no_argument, NULL, 't'},
192 {"segments", no_argument, NULL, 'l'},
193 {"string-dump", required_argument, NULL, 'p'},
194 {"symbols", no_argument, NULL, 's'},
195 {"syms", no_argument, NULL, 's'},
196 {"unwind", no_argument, NULL, 'u'},
197 {"use-dynamic", no_argument, NULL, 'D'},
198 {"version-info", no_argument, 0, 'V'},
199 {"version", no_argument, 0, 'v'},
200 {"wide", no_argument, 0, 'W'},
214 static void add_dumpop(struct readelf *re, size_t si, const char *sn, int op,
216 static const char *aeabi_adv_simd_arch(uint64_t simd);
217 static const char *aeabi_align_needed(uint64_t an);
218 static const char *aeabi_align_preserved(uint64_t ap);
219 static const char *aeabi_arm_isa(uint64_t ai);
220 static const char *aeabi_cpu_arch(uint64_t arch);
221 static const char *aeabi_cpu_arch_profile(uint64_t pf);
222 static const char *aeabi_div(uint64_t du);
223 static const char *aeabi_enum_size(uint64_t es);
224 static const char *aeabi_fp_16bit_format(uint64_t fp16);
225 static const char *aeabi_fp_arch(uint64_t fp);
226 static const char *aeabi_fp_denormal(uint64_t fd);
227 static const char *aeabi_fp_exceptions(uint64_t fe);
228 static const char *aeabi_fp_hpext(uint64_t fh);
229 static const char *aeabi_fp_number_model(uint64_t fn);
230 static const char *aeabi_fp_optm_goal(uint64_t fog);
231 static const char *aeabi_fp_rounding(uint64_t fr);
232 static const char *aeabi_hardfp(uint64_t hfp);
233 static const char *aeabi_mpext(uint64_t mp);
234 static const char *aeabi_optm_goal(uint64_t og);
235 static const char *aeabi_pcs_config(uint64_t pcs);
236 static const char *aeabi_pcs_got(uint64_t got);
237 static const char *aeabi_pcs_r9(uint64_t r9);
238 static const char *aeabi_pcs_ro(uint64_t ro);
239 static const char *aeabi_pcs_rw(uint64_t rw);
240 static const char *aeabi_pcs_wchar_t(uint64_t wt);
241 static const char *aeabi_t2ee(uint64_t t2ee);
242 static const char *aeabi_thumb_isa(uint64_t ti);
243 static const char *aeabi_fp_user_exceptions(uint64_t fu);
244 static const char *aeabi_unaligned_access(uint64_t ua);
245 static const char *aeabi_vfp_args(uint64_t va);
246 static const char *aeabi_virtual(uint64_t vt);
247 static const char *aeabi_wmmx_arch(uint64_t wmmx);
248 static const char *aeabi_wmmx_args(uint64_t wa);
249 static const char *elf_class(unsigned int class);
250 static const char *elf_endian(unsigned int endian);
251 static const char *elf_machine(unsigned int mach);
252 static const char *elf_osabi(unsigned int abi);
253 static const char *elf_type(unsigned int type);
254 static const char *elf_ver(unsigned int ver);
255 static const char *dt_type(unsigned int mach, unsigned int dtype);
256 static void dump_ar(struct readelf *re, int);
257 static void dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe);
258 static void dump_attributes(struct readelf *re);
259 static uint8_t *dump_compatibility_tag(uint8_t *p);
260 static void dump_dwarf(struct readelf *re);
261 static void dump_dwarf_abbrev(struct readelf *re);
262 static void dump_dwarf_aranges(struct readelf *re);
263 static void dump_dwarf_block(struct readelf *re, uint8_t *b,
265 static void dump_dwarf_die(struct readelf *re, Dwarf_Die die, int level);
266 static void dump_dwarf_frame(struct readelf *re, int alt);
267 static void dump_dwarf_frame_inst(struct readelf *re, Dwarf_Cie cie,
268 uint8_t *insts, Dwarf_Unsigned len, Dwarf_Unsigned caf, Dwarf_Signed daf,
269 Dwarf_Addr pc, Dwarf_Debug dbg);
270 static int dump_dwarf_frame_regtable(struct readelf *re, Dwarf_Fde fde,
271 Dwarf_Addr pc, Dwarf_Unsigned func_len, Dwarf_Half cie_ra);
272 static void dump_dwarf_frame_section(struct readelf *re, struct section *s,
274 static void dump_dwarf_info(struct readelf *re, Dwarf_Bool is_info);
275 static void dump_dwarf_macinfo(struct readelf *re);
276 static void dump_dwarf_line(struct readelf *re);
277 static void dump_dwarf_line_decoded(struct readelf *re);
278 static void dump_dwarf_loc(struct readelf *re, Dwarf_Loc *lr);
279 static void dump_dwarf_loclist(struct readelf *re);
280 static void dump_dwarf_pubnames(struct readelf *re);
281 static void dump_dwarf_ranges(struct readelf *re);
282 static void dump_dwarf_ranges_foreach(struct readelf *re, Dwarf_Die die,
284 static void dump_dwarf_str(struct readelf *re);
285 static void dump_eflags(struct readelf *re, uint64_t e_flags);
286 static void dump_elf(struct readelf *re);
287 static void dump_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab);
288 static void dump_dynamic(struct readelf *re);
289 static void dump_liblist(struct readelf *re);
290 static void dump_mips_attributes(struct readelf *re, uint8_t *p, uint8_t *pe);
291 static void dump_mips_odk_reginfo(struct readelf *re, uint8_t *p, size_t sz);
292 static void dump_mips_options(struct readelf *re, struct section *s);
293 static void dump_mips_option_flags(const char *name, struct mips_option *opt,
295 static void dump_mips_reginfo(struct readelf *re, struct section *s);
296 static void dump_mips_specific_info(struct readelf *re);
297 static void dump_notes(struct readelf *re);
298 static void dump_notes_content(struct readelf *re, const char *buf, size_t sz,
300 static void dump_svr4_hash(struct section *s);
301 static void dump_svr4_hash64(struct readelf *re, struct section *s);
302 static void dump_gnu_hash(struct readelf *re, struct section *s);
303 static void dump_hash(struct readelf *re);
304 static void dump_phdr(struct readelf *re);
305 static void dump_ppc_attributes(uint8_t *p, uint8_t *pe);
306 static void dump_section_groups(struct readelf *re);
307 static void dump_symtab(struct readelf *re, int i);
308 static void dump_symtabs(struct readelf *re);
309 static uint8_t *dump_unknown_tag(uint64_t tag, uint8_t *p);
310 static void dump_ver(struct readelf *re);
311 static void dump_verdef(struct readelf *re, int dump);
312 static void dump_verneed(struct readelf *re, int dump);
313 static void dump_versym(struct readelf *re);
314 static const char *dwarf_reg(unsigned int mach, unsigned int reg);
315 static const char *dwarf_regname(struct readelf *re, unsigned int num);
316 static struct dumpop *find_dumpop(struct readelf *re, size_t si,
317 const char *sn, int op, int t);
318 static int get_ent_count(struct section *s, int *ent_count);
319 static char *get_regoff_str(struct readelf *re, Dwarf_Half reg,
321 static const char *get_string(struct readelf *re, int strtab, size_t off);
322 static const char *get_symbol_name(struct readelf *re, int symtab, int i);
323 static uint64_t get_symbol_value(struct readelf *re, int symtab, int i);
324 static void load_sections(struct readelf *re);
325 static const char *mips_abi_fp(uint64_t fp);
326 static const char *note_type(const char *note_name, unsigned int et,
328 static const char *note_type_freebsd(unsigned int nt);
329 static const char *note_type_freebsd_core(unsigned int nt);
330 static const char *note_type_linux_core(unsigned int nt);
331 static const char *note_type_gnu(unsigned int nt);
332 static const char *note_type_netbsd(unsigned int nt);
333 static const char *note_type_openbsd(unsigned int nt);
334 static const char *note_type_unknown(unsigned int nt);
335 static const char *option_kind(uint8_t kind);
336 static const char *phdr_type(unsigned int ptype);
337 static const char *ppc_abi_fp(uint64_t fp);
338 static const char *ppc_abi_vector(uint64_t vec);
339 static const char *r_type(unsigned int mach, unsigned int type);
340 static void readelf_usage(void);
341 static void readelf_version(void);
342 static void search_loclist_at(struct readelf *re, Dwarf_Die die,
343 Dwarf_Unsigned lowpc);
344 static void search_ver(struct readelf *re);
345 static const char *section_type(unsigned int mach, unsigned int stype);
346 static void set_cu_context(struct readelf *re, Dwarf_Half psize,
347 Dwarf_Half osize, Dwarf_Half ver);
348 static const char *st_bind(unsigned int sbind);
349 static const char *st_shndx(unsigned int shndx);
350 static const char *st_type(unsigned int stype);
351 static const char *st_vis(unsigned int svis);
352 static const char *top_tag(unsigned int tag);
353 static void unload_sections(struct readelf *re);
354 static uint64_t _read_lsb(Elf_Data *d, uint64_t *offsetp,
356 static uint64_t _read_msb(Elf_Data *d, uint64_t *offsetp,
358 static uint64_t _decode_lsb(uint8_t **data, int bytes_to_read);
359 static uint64_t _decode_msb(uint8_t **data, int bytes_to_read);
360 static int64_t _decode_sleb128(uint8_t **dp);
361 static uint64_t _decode_uleb128(uint8_t **dp);
363 static struct eflags_desc arm_eflags_desc[] = {
364 {EF_ARM_RELEXEC, "relocatable executable"},
365 {EF_ARM_HASENTRY, "has entry point"},
366 {EF_ARM_SYMSARESORTED, "sorted symbol tables"},
367 {EF_ARM_DYNSYMSUSESEGIDX, "dynamic symbols use segment index"},
368 {EF_ARM_MAPSYMSFIRST, "mapping symbols precede others"},
371 {EF_ARM_INTERWORK, "interworking enabled"},
372 {EF_ARM_APCS_26, "uses APCS/26"},
373 {EF_ARM_APCS_FLOAT, "uses APCS/float"},
374 {EF_ARM_PIC, "position independent"},
375 {EF_ARM_ALIGN8, "8 bit structure alignment"},
376 {EF_ARM_NEW_ABI, "uses new ABI"},
377 {EF_ARM_OLD_ABI, "uses old ABI"},
378 {EF_ARM_SOFT_FLOAT, "software FP"},
379 {EF_ARM_VFP_FLOAT, "VFP"},
380 {EF_ARM_MAVERICK_FLOAT, "Maverick FP"},
384 static struct eflags_desc mips_eflags_desc[] = {
385 {EF_MIPS_NOREORDER, "noreorder"},
386 {EF_MIPS_PIC, "pic"},
387 {EF_MIPS_CPIC, "cpic"},
388 {EF_MIPS_UCODE, "ugen_reserved"},
389 {EF_MIPS_ABI2, "abi2"},
390 {EF_MIPS_OPTIONS_FIRST, "odk first"},
391 {EF_MIPS_ARCH_ASE_MDMX, "mdmx"},
392 {EF_MIPS_ARCH_ASE_M16, "mips16"},
396 static struct eflags_desc powerpc_eflags_desc[] = {
398 {EF_PPC_RELOCATABLE, "relocatable"},
399 {EF_PPC_RELOCATABLE_LIB, "relocatable-lib"},
403 static struct eflags_desc sparc_eflags_desc[] = {
404 {EF_SPARC_32PLUS, "v8+"},
405 {EF_SPARC_SUN_US1, "ultrasparcI"},
406 {EF_SPARC_HAL_R1, "halr1"},
407 {EF_SPARC_SUN_US3, "ultrasparcIII"},
412 elf_osabi(unsigned int abi)
414 static char s_abi[32];
417 case ELFOSABI_NONE: return "NONE";
418 case ELFOSABI_HPUX: return "HPUX";
419 case ELFOSABI_NETBSD: return "NetBSD";
420 case ELFOSABI_GNU: return "GNU";
421 case ELFOSABI_HURD: return "HURD";
422 case ELFOSABI_86OPEN: return "86OPEN";
423 case ELFOSABI_SOLARIS: return "Solaris";
424 case ELFOSABI_AIX: return "AIX";
425 case ELFOSABI_IRIX: return "IRIX";
426 case ELFOSABI_FREEBSD: return "FreeBSD";
427 case ELFOSABI_TRU64: return "TRU64";
428 case ELFOSABI_MODESTO: return "MODESTO";
429 case ELFOSABI_OPENBSD: return "OpenBSD";
430 case ELFOSABI_OPENVMS: return "OpenVMS";
431 case ELFOSABI_NSK: return "NSK";
432 case ELFOSABI_ARM: return "ARM";
433 case ELFOSABI_STANDALONE: return "StandAlone";
435 snprintf(s_abi, sizeof(s_abi), "<unknown: %#x>", abi);
441 elf_machine(unsigned int mach)
443 static char s_mach[32];
446 case EM_NONE: return "Unknown machine";
447 case EM_M32: return "AT&T WE32100";
448 case EM_SPARC: return "Sun SPARC";
449 case EM_386: return "Intel i386";
450 case EM_68K: return "Motorola 68000";
451 case EM_IAMCU: return "Intel MCU";
452 case EM_88K: return "Motorola 88000";
453 case EM_860: return "Intel i860";
454 case EM_MIPS: return "MIPS R3000 Big-Endian only";
455 case EM_S370: return "IBM System/370";
456 case EM_MIPS_RS3_LE: return "MIPS R3000 Little-Endian";
457 case EM_PARISC: return "HP PA-RISC";
458 case EM_VPP500: return "Fujitsu VPP500";
459 case EM_SPARC32PLUS: return "SPARC v8plus";
460 case EM_960: return "Intel 80960";
461 case EM_PPC: return "PowerPC 32-bit";
462 case EM_PPC64: return "PowerPC 64-bit";
463 case EM_S390: return "IBM System/390";
464 case EM_V800: return "NEC V800";
465 case EM_FR20: return "Fujitsu FR20";
466 case EM_RH32: return "TRW RH-32";
467 case EM_RCE: return "Motorola RCE";
468 case EM_ARM: return "ARM";
469 case EM_SH: return "Hitachi SH";
470 case EM_SPARCV9: return "SPARC v9 64-bit";
471 case EM_TRICORE: return "Siemens TriCore embedded processor";
472 case EM_ARC: return "Argonaut RISC Core";
473 case EM_H8_300: return "Hitachi H8/300";
474 case EM_H8_300H: return "Hitachi H8/300H";
475 case EM_H8S: return "Hitachi H8S";
476 case EM_H8_500: return "Hitachi H8/500";
477 case EM_IA_64: return "Intel IA-64 Processor";
478 case EM_MIPS_X: return "Stanford MIPS-X";
479 case EM_COLDFIRE: return "Motorola ColdFire";
480 case EM_68HC12: return "Motorola M68HC12";
481 case EM_MMA: return "Fujitsu MMA";
482 case EM_PCP: return "Siemens PCP";
483 case EM_NCPU: return "Sony nCPU";
484 case EM_NDR1: return "Denso NDR1 microprocessor";
485 case EM_STARCORE: return "Motorola Star*Core processor";
486 case EM_ME16: return "Toyota ME16 processor";
487 case EM_ST100: return "STMicroelectronics ST100 processor";
488 case EM_TINYJ: return "Advanced Logic Corp. TinyJ processor";
489 case EM_X86_64: return "Advanced Micro Devices x86-64";
490 case EM_PDSP: return "Sony DSP Processor";
491 case EM_FX66: return "Siemens FX66 microcontroller";
492 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 microcontroller";
493 case EM_ST7: return "STmicroelectronics ST7 8-bit microcontroller";
494 case EM_68HC16: return "Motorola MC68HC16 microcontroller";
495 case EM_68HC11: return "Motorola MC68HC11 microcontroller";
496 case EM_68HC08: return "Motorola MC68HC08 microcontroller";
497 case EM_68HC05: return "Motorola MC68HC05 microcontroller";
498 case EM_SVX: return "Silicon Graphics SVx";
499 case EM_ST19: return "STMicroelectronics ST19 8-bit mc";
500 case EM_VAX: return "Digital VAX";
501 case EM_CRIS: return "Axis Communications 32-bit embedded processor";
502 case EM_JAVELIN: return "Infineon Tech. 32bit embedded processor";
503 case EM_FIREPATH: return "Element 14 64-bit DSP Processor";
504 case EM_ZSP: return "LSI Logic 16-bit DSP Processor";
505 case EM_MMIX: return "Donald Knuth's educational 64-bit proc";
506 case EM_HUANY: return "Harvard University MI object files";
507 case EM_PRISM: return "SiTera Prism";
508 case EM_AVR: return "Atmel AVR 8-bit microcontroller";
509 case EM_FR30: return "Fujitsu FR30";
510 case EM_D10V: return "Mitsubishi D10V";
511 case EM_D30V: return "Mitsubishi D30V";
512 case EM_V850: return "NEC v850";
513 case EM_M32R: return "Mitsubishi M32R";
514 case EM_MN10300: return "Matsushita MN10300";
515 case EM_MN10200: return "Matsushita MN10200";
516 case EM_PJ: return "picoJava";
517 case EM_OPENRISC: return "OpenRISC 32-bit embedded processor";
518 case EM_ARC_A5: return "ARC Cores Tangent-A5";
519 case EM_XTENSA: return "Tensilica Xtensa Architecture";
520 case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
521 case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
522 case EM_NS32K: return "National Semiconductor 32000 series";
523 case EM_TPC: return "Tenor Network TPC processor";
524 case EM_SNP1K: return "Trebia SNP 1000 processor";
525 case EM_ST200: return "STMicroelectronics ST200 microcontroller";
526 case EM_IP2K: return "Ubicom IP2xxx microcontroller family";
527 case EM_MAX: return "MAX Processor";
528 case EM_CR: return "National Semiconductor CompactRISC microprocessor";
529 case EM_F2MC16: return "Fujitsu F2MC16";
530 case EM_MSP430: return "TI embedded microcontroller msp430";
531 case EM_BLACKFIN: return "Analog Devices Blackfin (DSP) processor";
532 case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
533 case EM_SEP: return "Sharp embedded microprocessor";
534 case EM_ARCA: return "Arca RISC Microprocessor";
535 case EM_UNICORE: return "Microprocessor series from PKU-Unity Ltd";
536 case EM_AARCH64: return "AArch64";
537 case EM_RISCV: return "RISC-V";
539 snprintf(s_mach, sizeof(s_mach), "<unknown: %#x>", mach);
546 elf_class(unsigned int class)
548 static char s_class[32];
551 case ELFCLASSNONE: return "none";
552 case ELFCLASS32: return "ELF32";
553 case ELFCLASS64: return "ELF64";
555 snprintf(s_class, sizeof(s_class), "<unknown: %#x>", class);
561 elf_endian(unsigned int endian)
563 static char s_endian[32];
566 case ELFDATANONE: return "none";
567 case ELFDATA2LSB: return "2's complement, little endian";
568 case ELFDATA2MSB: return "2's complement, big endian";
570 snprintf(s_endian, sizeof(s_endian), "<unknown: %#x>", endian);
576 elf_type(unsigned int type)
578 static char s_type[32];
581 case ET_NONE: return "NONE (None)";
582 case ET_REL: return "REL (Relocatable file)";
583 case ET_EXEC: return "EXEC (Executable file)";
584 case ET_DYN: return "DYN (Shared object file)";
585 case ET_CORE: return "CORE (Core file)";
587 if (type >= ET_LOPROC)
588 snprintf(s_type, sizeof(s_type), "<proc: %#x>", type);
589 else if (type >= ET_LOOS && type <= ET_HIOS)
590 snprintf(s_type, sizeof(s_type), "<os: %#x>", type);
592 snprintf(s_type, sizeof(s_type), "<unknown: %#x>",
599 elf_ver(unsigned int ver)
601 static char s_ver[32];
604 case EV_CURRENT: return "(current)";
605 case EV_NONE: return "(none)";
607 snprintf(s_ver, sizeof(s_ver), "<unknown: %#x>",
614 phdr_type(unsigned int ptype)
616 static char s_ptype[32];
619 case PT_NULL: return "NULL";
620 case PT_LOAD: return "LOAD";
621 case PT_DYNAMIC: return "DYNAMIC";
622 case PT_INTERP: return "INTERP";
623 case PT_NOTE: return "NOTE";
624 case PT_SHLIB: return "SHLIB";
625 case PT_PHDR: return "PHDR";
626 case PT_TLS: return "TLS";
627 case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
628 case PT_GNU_STACK: return "GNU_STACK";
629 case PT_GNU_RELRO: return "GNU_RELRO";
631 if (ptype >= PT_LOPROC && ptype <= PT_HIPROC)
632 snprintf(s_ptype, sizeof(s_ptype), "LOPROC+%#x",
634 else if (ptype >= PT_LOOS && ptype <= PT_HIOS)
635 snprintf(s_ptype, sizeof(s_ptype), "LOOS+%#x",
638 snprintf(s_ptype, sizeof(s_ptype), "<unknown: %#x>",
645 section_type(unsigned int mach, unsigned int stype)
647 static char s_stype[32];
649 if (stype >= SHT_LOPROC && stype <= SHT_HIPROC) {
653 case SHT_AMD64_UNWIND: return "AMD64_UNWIND";
661 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
662 case SHT_MIPS_MSYM: return "MIPS_MSYM";
663 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
664 case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
665 case SHT_MIPS_UCODE: return "MIPS_UCODE";
666 case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
667 case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
668 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
669 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
670 case SHT_MIPS_RELD: return "MIPS_RELD";
671 case SHT_MIPS_IFACE: return "MIPS_IFACE";
672 case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
673 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
674 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
675 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
676 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
677 case SHT_MIPS_DWARF: return "MIPS_DWARF";
678 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
679 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
680 case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
681 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
682 case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
683 case SHT_MIPS_XLATE: return "MIPS_XLATE";
684 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
685 case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
686 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
687 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
688 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
697 snprintf(s_stype, sizeof(s_stype), "LOPROC+%#x",
703 case SHT_NULL: return "NULL";
704 case SHT_PROGBITS: return "PROGBITS";
705 case SHT_SYMTAB: return "SYMTAB";
706 case SHT_STRTAB: return "STRTAB";
707 case SHT_RELA: return "RELA";
708 case SHT_HASH: return "HASH";
709 case SHT_DYNAMIC: return "DYNAMIC";
710 case SHT_NOTE: return "NOTE";
711 case SHT_NOBITS: return "NOBITS";
712 case SHT_REL: return "REL";
713 case SHT_SHLIB: return "SHLIB";
714 case SHT_DYNSYM: return "DYNSYM";
715 case SHT_INIT_ARRAY: return "INIT_ARRAY";
716 case SHT_FINI_ARRAY: return "FINI_ARRAY";
717 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
718 case SHT_GROUP: return "GROUP";
719 case SHT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
720 case SHT_SUNW_dof: return "SUNW_dof";
721 case SHT_SUNW_cap: return "SUNW_cap";
722 case SHT_GNU_HASH: return "GNU_HASH";
723 case SHT_SUNW_ANNOTATE: return "SUNW_ANNOTATE";
724 case SHT_SUNW_DEBUGSTR: return "SUNW_DEBUGSTR";
725 case SHT_SUNW_DEBUG: return "SUNW_DEBUG";
726 case SHT_SUNW_move: return "SUNW_move";
727 case SHT_SUNW_COMDAT: return "SUNW_COMDAT";
728 case SHT_SUNW_syminfo: return "SUNW_syminfo";
729 case SHT_SUNW_verdef: return "SUNW_verdef";
730 case SHT_SUNW_verneed: return "SUNW_verneed";
731 case SHT_SUNW_versym: return "SUNW_versym";
733 if (stype >= SHT_LOOS && stype <= SHT_HIOS)
734 snprintf(s_stype, sizeof(s_stype), "LOOS+%#x",
736 else if (stype >= SHT_LOUSER)
737 snprintf(s_stype, sizeof(s_stype), "LOUSER+%#x",
740 snprintf(s_stype, sizeof(s_stype), "<unknown: %#x>",
747 dt_type(unsigned int mach, unsigned int dtype)
749 static char s_dtype[32];
751 if (dtype >= DT_LOPROC && dtype <= DT_HIPROC) {
755 case DT_ARM_SYMTABSZ:
756 return "ARM_SYMTABSZ";
764 case DT_MIPS_RLD_VERSION:
765 return "MIPS_RLD_VERSION";
766 case DT_MIPS_TIME_STAMP:
767 return "MIPS_TIME_STAMP";
768 case DT_MIPS_ICHECKSUM:
769 return "MIPS_ICHECKSUM";
770 case DT_MIPS_IVERSION:
771 return "MIPS_IVERSION";
774 case DT_MIPS_BASE_ADDRESS:
775 return "MIPS_BASE_ADDRESS";
776 case DT_MIPS_CONFLICT:
777 return "MIPS_CONFLICT";
778 case DT_MIPS_LIBLIST:
779 return "MIPS_LIBLIST";
780 case DT_MIPS_LOCAL_GOTNO:
781 return "MIPS_LOCAL_GOTNO";
782 case DT_MIPS_CONFLICTNO:
783 return "MIPS_CONFLICTNO";
784 case DT_MIPS_LIBLISTNO:
785 return "MIPS_LIBLISTNO";
786 case DT_MIPS_SYMTABNO:
787 return "MIPS_SYMTABNO";
788 case DT_MIPS_UNREFEXTNO:
789 return "MIPS_UNREFEXTNO";
791 return "MIPS_GOTSYM";
792 case DT_MIPS_HIPAGENO:
793 return "MIPS_HIPAGENO";
794 case DT_MIPS_RLD_MAP:
795 return "MIPS_RLD_MAP";
796 case DT_MIPS_DELTA_CLASS:
797 return "MIPS_DELTA_CLASS";
798 case DT_MIPS_DELTA_CLASS_NO:
799 return "MIPS_DELTA_CLASS_NO";
800 case DT_MIPS_DELTA_INSTANCE:
801 return "MIPS_DELTA_INSTANCE";
802 case DT_MIPS_DELTA_INSTANCE_NO:
803 return "MIPS_DELTA_INSTANCE_NO";
804 case DT_MIPS_DELTA_RELOC:
805 return "MIPS_DELTA_RELOC";
806 case DT_MIPS_DELTA_RELOC_NO:
807 return "MIPS_DELTA_RELOC_NO";
808 case DT_MIPS_DELTA_SYM:
809 return "MIPS_DELTA_SYM";
810 case DT_MIPS_DELTA_SYM_NO:
811 return "MIPS_DELTA_SYM_NO";
812 case DT_MIPS_DELTA_CLASSSYM:
813 return "MIPS_DELTA_CLASSSYM";
814 case DT_MIPS_DELTA_CLASSSYM_NO:
815 return "MIPS_DELTA_CLASSSYM_NO";
816 case DT_MIPS_CXX_FLAGS:
817 return "MIPS_CXX_FLAGS";
818 case DT_MIPS_PIXIE_INIT:
819 return "MIPS_PIXIE_INIT";
820 case DT_MIPS_SYMBOL_LIB:
821 return "MIPS_SYMBOL_LIB";
822 case DT_MIPS_LOCALPAGE_GOTIDX:
823 return "MIPS_LOCALPAGE_GOTIDX";
824 case DT_MIPS_LOCAL_GOTIDX:
825 return "MIPS_LOCAL_GOTIDX";
826 case DT_MIPS_HIDDEN_GOTIDX:
827 return "MIPS_HIDDEN_GOTIDX";
828 case DT_MIPS_PROTECTED_GOTIDX:
829 return "MIPS_PROTECTED_GOTIDX";
830 case DT_MIPS_OPTIONS:
831 return "MIPS_OPTIONS";
832 case DT_MIPS_INTERFACE:
833 return "MIPS_INTERFACE";
834 case DT_MIPS_DYNSTR_ALIGN:
835 return "MIPS_DYNSTR_ALIGN";
836 case DT_MIPS_INTERFACE_SIZE:
837 return "MIPS_INTERFACE_SIZE";
838 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
839 return "MIPS_RLD_TEXT_RESOLVE_ADDR";
840 case DT_MIPS_PERF_SUFFIX:
841 return "MIPS_PERF_SUFFIX";
842 case DT_MIPS_COMPACT_SIZE:
843 return "MIPS_COMPACT_SIZE";
844 case DT_MIPS_GP_VALUE:
845 return "MIPS_GP_VALUE";
846 case DT_MIPS_AUX_DYNAMIC:
847 return "MIPS_AUX_DYNAMIC";
849 return "MIPS_PLTGOT";
850 case DT_MIPS_RLD_OBJ_UPDATE:
851 return "MIPS_RLD_OBJ_UPDATE";
862 case DT_SPARC_REGISTER:
863 return "DT_SPARC_REGISTER";
871 snprintf(s_dtype, sizeof(s_dtype), "<unknown: %#x>", dtype);
876 case DT_NULL: return "NULL";
877 case DT_NEEDED: return "NEEDED";
878 case DT_PLTRELSZ: return "PLTRELSZ";
879 case DT_PLTGOT: return "PLTGOT";
880 case DT_HASH: return "HASH";
881 case DT_STRTAB: return "STRTAB";
882 case DT_SYMTAB: return "SYMTAB";
883 case DT_RELA: return "RELA";
884 case DT_RELASZ: return "RELASZ";
885 case DT_RELAENT: return "RELAENT";
886 case DT_STRSZ: return "STRSZ";
887 case DT_SYMENT: return "SYMENT";
888 case DT_INIT: return "INIT";
889 case DT_FINI: return "FINI";
890 case DT_SONAME: return "SONAME";
891 case DT_RPATH: return "RPATH";
892 case DT_SYMBOLIC: return "SYMBOLIC";
893 case DT_REL: return "REL";
894 case DT_RELSZ: return "RELSZ";
895 case DT_RELENT: return "RELENT";
896 case DT_PLTREL: return "PLTREL";
897 case DT_DEBUG: return "DEBUG";
898 case DT_TEXTREL: return "TEXTREL";
899 case DT_JMPREL: return "JMPREL";
900 case DT_BIND_NOW: return "BIND_NOW";
901 case DT_INIT_ARRAY: return "INIT_ARRAY";
902 case DT_FINI_ARRAY: return "FINI_ARRAY";
903 case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
904 case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
905 case DT_RUNPATH: return "RUNPATH";
906 case DT_FLAGS: return "FLAGS";
907 case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
908 case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
909 case DT_MAXPOSTAGS: return "MAXPOSTAGS";
910 case DT_SUNW_AUXILIARY: return "SUNW_AUXILIARY";
911 case DT_SUNW_RTLDINF: return "SUNW_RTLDINF";
912 case DT_SUNW_FILTER: return "SUNW_FILTER";
913 case DT_SUNW_CAP: return "SUNW_CAP";
914 case DT_CHECKSUM: return "CHECKSUM";
915 case DT_PLTPADSZ: return "PLTPADSZ";
916 case DT_MOVEENT: return "MOVEENT";
917 case DT_MOVESZ: return "MOVESZ";
918 case DT_FEATURE: return "FEATURE";
919 case DT_POSFLAG_1: return "POSFLAG_1";
920 case DT_SYMINSZ: return "SYMINSZ";
921 case DT_SYMINENT: return "SYMINENT";
922 case DT_GNU_HASH: return "GNU_HASH";
923 case DT_GNU_CONFLICT: return "GNU_CONFLICT";
924 case DT_GNU_LIBLIST: return "GNU_LIBLIST";
925 case DT_CONFIG: return "CONFIG";
926 case DT_DEPAUDIT: return "DEPAUDIT";
927 case DT_AUDIT: return "AUDIT";
928 case DT_PLTPAD: return "PLTPAD";
929 case DT_MOVETAB: return "MOVETAB";
930 case DT_SYMINFO: return "SYMINFO";
931 case DT_VERSYM: return "VERSYM";
932 case DT_RELACOUNT: return "RELACOUNT";
933 case DT_RELCOUNT: return "RELCOUNT";
934 case DT_FLAGS_1: return "FLAGS_1";
935 case DT_VERDEF: return "VERDEF";
936 case DT_VERDEFNUM: return "VERDEFNUM";
937 case DT_VERNEED: return "VERNEED";
938 case DT_VERNEEDNUM: return "VERNEEDNUM";
939 case DT_AUXILIARY: return "AUXILIARY";
940 case DT_USED: return "USED";
941 case DT_FILTER: return "FILTER";
942 case DT_GNU_PRELINKED: return "GNU_PRELINKED";
943 case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
944 case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
946 snprintf(s_dtype, sizeof(s_dtype), "<unknown: %#x>", dtype);
952 st_bind(unsigned int sbind)
954 static char s_sbind[32];
957 case STB_LOCAL: return "LOCAL";
958 case STB_GLOBAL: return "GLOBAL";
959 case STB_WEAK: return "WEAK";
961 if (sbind >= STB_LOOS && sbind <= STB_HIOS)
963 else if (sbind >= STB_LOPROC && sbind <= STB_HIPROC)
966 snprintf(s_sbind, sizeof(s_sbind), "<unknown: %#x>",
973 st_type(unsigned int stype)
975 static char s_stype[32];
978 case STT_NOTYPE: return "NOTYPE";
979 case STT_OBJECT: return "OBJECT";
980 case STT_FUNC: return "FUNC";
981 case STT_SECTION: return "SECTION";
982 case STT_FILE: return "FILE";
983 case STT_COMMON: return "COMMON";
984 case STT_TLS: return "TLS";
986 if (stype >= STT_LOOS && stype <= STT_HIOS)
987 snprintf(s_stype, sizeof(s_stype), "OS+%#x",
989 else if (stype >= STT_LOPROC && stype <= STT_HIPROC)
990 snprintf(s_stype, sizeof(s_stype), "PROC+%#x",
993 snprintf(s_stype, sizeof(s_stype), "<unknown: %#x>",
1000 st_vis(unsigned int svis)
1002 static char s_svis[32];
1005 case STV_DEFAULT: return "DEFAULT";
1006 case STV_INTERNAL: return "INTERNAL";
1007 case STV_HIDDEN: return "HIDDEN";
1008 case STV_PROTECTED: return "PROTECTED";
1010 snprintf(s_svis, sizeof(s_svis), "<unknown: %#x>", svis);
1016 st_shndx(unsigned int shndx)
1018 static char s_shndx[32];
1021 case SHN_UNDEF: return "UND";
1022 case SHN_ABS: return "ABS";
1023 case SHN_COMMON: return "COM";
1025 if (shndx >= SHN_LOPROC && shndx <= SHN_HIPROC)
1027 else if (shndx >= SHN_LOOS && shndx <= SHN_HIOS)
1030 snprintf(s_shndx, sizeof(s_shndx), "%u", shndx);
1039 } section_flag[] = {
1040 {"WRITE", 'W', SHF_WRITE},
1041 {"ALLOC", 'A', SHF_ALLOC},
1042 {"EXEC", 'X', SHF_EXECINSTR},
1043 {"MERGE", 'M', SHF_MERGE},
1044 {"STRINGS", 'S', SHF_STRINGS},
1045 {"INFO LINK", 'I', SHF_INFO_LINK},
1046 {"OS NONCONF", 'O', SHF_OS_NONCONFORMING},
1047 {"GROUP", 'G', SHF_GROUP},
1048 {"TLS", 'T', SHF_TLS},
1053 r_type(unsigned int mach, unsigned int type)
1056 case EM_NONE: return "";
1060 case 0: return "R_386_NONE";
1061 case 1: return "R_386_32";
1062 case 2: return "R_386_PC32";
1063 case 3: return "R_386_GOT32";
1064 case 4: return "R_386_PLT32";
1065 case 5: return "R_386_COPY";
1066 case 6: return "R_386_GLOB_DAT";
1067 case 7: return "R_386_JMP_SLOT";
1068 case 8: return "R_386_RELATIVE";
1069 case 9: return "R_386_GOTOFF";
1070 case 10: return "R_386_GOTPC";
1071 case 14: return "R_386_TLS_TPOFF";
1072 case 15: return "R_386_TLS_IE";
1073 case 16: return "R_386_TLS_GOTIE";
1074 case 17: return "R_386_TLS_LE";
1075 case 18: return "R_386_TLS_GD";
1076 case 19: return "R_386_TLS_LDM";
1077 case 24: return "R_386_TLS_GD_32";
1078 case 25: return "R_386_TLS_GD_PUSH";
1079 case 26: return "R_386_TLS_GD_CALL";
1080 case 27: return "R_386_TLS_GD_POP";
1081 case 28: return "R_386_TLS_LDM_32";
1082 case 29: return "R_386_TLS_LDM_PUSH";
1083 case 30: return "R_386_TLS_LDM_CALL";
1084 case 31: return "R_386_TLS_LDM_POP";
1085 case 32: return "R_386_TLS_LDO_32";
1086 case 33: return "R_386_TLS_IE_32";
1087 case 34: return "R_386_TLS_LE_32";
1088 case 35: return "R_386_TLS_DTPMOD32";
1089 case 36: return "R_386_TLS_DTPOFF32";
1090 case 37: return "R_386_TLS_TPOFF32";
1095 case 0: return "R_AARCH64_NONE";
1096 case 257: return "R_AARCH64_ABS64";
1097 case 258: return "R_AARCH64_ABS32";
1098 case 259: return "R_AARCH64_ABS16";
1099 case 260: return "R_AARCH64_PREL64";
1100 case 261: return "R_AARCH64_PREL32";
1101 case 262: return "R_AARCH64_PREL16";
1102 case 263: return "R_AARCH64_MOVW_UABS_G0";
1103 case 264: return "R_AARCH64_MOVW_UABS_G0_NC";
1104 case 265: return "R_AARCH64_MOVW_UABS_G1";
1105 case 266: return "R_AARCH64_MOVW_UABS_G1_NC";
1106 case 267: return "R_AARCH64_MOVW_UABS_G2";
1107 case 268: return "R_AARCH64_MOVW_UABS_G2_NC";
1108 case 269: return "R_AARCH64_MOVW_UABS_G3";
1109 case 270: return "R_AARCH64_MOVW_SABS_G0";
1110 case 271: return "R_AARCH64_MOVW_SABS_G1";
1111 case 272: return "R_AARCH64_MOVW_SABS_G2";
1112 case 273: return "R_AARCH64_LD_PREL_LO19";
1113 case 274: return "R_AARCH64_ADR_PREL_LO21";
1114 case 275: return "R_AARCH64_ADR_PREL_PG_HI21";
1115 case 276: return "R_AARCH64_ADR_PREL_PG_HI21_NC";
1116 case 277: return "R_AARCH64_ADD_ABS_LO12_NC";
1117 case 278: return "R_AARCH64_LDST8_ABS_LO12_NC";
1118 case 279: return "R_AARCH64_TSTBR14";
1119 case 280: return "R_AARCH64_CONDBR19";
1120 case 282: return "R_AARCH64_JUMP26";
1121 case 283: return "R_AARCH64_CALL26";
1122 case 284: return "R_AARCH64_LDST16_ABS_LO12_NC";
1123 case 285: return "R_AARCH64_LDST32_ABS_LO12_NC";
1124 case 286: return "R_AARCH64_LDST64_ABS_LO12_NC";
1125 case 287: return "R_AARCH64_MOVW_PREL_G0";
1126 case 288: return "R_AARCH64_MOVW_PREL_G0_NC";
1127 case 289: return "R_AARCH64_MOVW_PREL_G1";
1128 case 290: return "R_AARCH64_MOVW_PREL_G1_NC";
1129 case 291: return "R_AARCH64_MOVW_PREL_G2";
1130 case 292: return "R_AARCH64_MOVW_PREL_G2_NC";
1131 case 293: return "R_AARCH64_MOVW_PREL_G3";
1132 case 299: return "R_AARCH64_LDST128_ABS_LO12_NC";
1133 case 300: return "R_AARCH64_MOVW_GOTOFF_G0";
1134 case 301: return "R_AARCH64_MOVW_GOTOFF_G0_NC";
1135 case 302: return "R_AARCH64_MOVW_GOTOFF_G1";
1136 case 303: return "R_AARCH64_MOVW_GOTOFF_G1_NC";
1137 case 304: return "R_AARCH64_MOVW_GOTOFF_G2";
1138 case 305: return "R_AARCH64_MOVW_GOTOFF_G2_NC";
1139 case 306: return "R_AARCH64_MOVW_GOTOFF_G3";
1140 case 307: return "R_AARCH64_GOTREL64";
1141 case 308: return "R_AARCH64_GOTREL32";
1142 case 309: return "R_AARCH64_GOT_LD_PREL19";
1143 case 310: return "R_AARCH64_LD64_GOTOFF_LO15";
1144 case 311: return "R_AARCH64_ADR_GOT_PAGE";
1145 case 312: return "R_AARCH64_LD64_GOT_LO12_NC";
1146 case 313: return "R_AARCH64_LD64_GOTPAGE_LO15";
1147 case 1024: return "R_AARCH64_COPY";
1148 case 1025: return "R_AARCH64_GLOB_DAT";
1149 case 1026: return "R_AARCH64_JUMP_SLOT";
1150 case 1027: return "R_AARCH64_RELATIVE";
1151 case 1028: return "R_AARCH64_TLS_DTPREL64";
1152 case 1029: return "R_AARCH64_TLS_DTPMOD64";
1153 case 1030: return "R_AARCH64_TLS_TPREL64";
1154 case 1031: return "R_AARCH64_TLSDESC";
1155 case 1032: return "R_AARCH64_IRELATIVE";
1160 case 0: return "R_ARM_NONE";
1161 case 1: return "R_ARM_PC24";
1162 case 2: return "R_ARM_ABS32";
1163 case 3: return "R_ARM_REL32";
1164 case 4: return "R_ARM_PC13";
1165 case 5: return "R_ARM_ABS16";
1166 case 6: return "R_ARM_ABS12";
1167 case 7: return "R_ARM_THM_ABS5";
1168 case 8: return "R_ARM_ABS8";
1169 case 9: return "R_ARM_SBREL32";
1170 case 10: return "R_ARM_THM_PC22";
1171 case 11: return "R_ARM_THM_PC8";
1172 case 12: return "R_ARM_AMP_VCALL9";
1173 case 13: return "R_ARM_SWI24";
1174 case 14: return "R_ARM_THM_SWI8";
1175 case 15: return "R_ARM_XPC25";
1176 case 16: return "R_ARM_THM_XPC22";
1177 case 20: return "R_ARM_COPY";
1178 case 21: return "R_ARM_GLOB_DAT";
1179 case 22: return "R_ARM_JUMP_SLOT";
1180 case 23: return "R_ARM_RELATIVE";
1181 case 24: return "R_ARM_GOTOFF";
1182 case 25: return "R_ARM_GOTPC";
1183 case 26: return "R_ARM_GOT32";
1184 case 27: return "R_ARM_PLT32";
1185 case 100: return "R_ARM_GNU_VTENTRY";
1186 case 101: return "R_ARM_GNU_VTINHERIT";
1187 case 250: return "R_ARM_RSBREL32";
1188 case 251: return "R_ARM_THM_RPC22";
1189 case 252: return "R_ARM_RREL32";
1190 case 253: return "R_ARM_RABS32";
1191 case 254: return "R_ARM_RPC24";
1192 case 255: return "R_ARM_RBASE";
1197 case 0: return "R_IA_64_NONE";
1198 case 33: return "R_IA_64_IMM14";
1199 case 34: return "R_IA_64_IMM22";
1200 case 35: return "R_IA_64_IMM64";
1201 case 36: return "R_IA_64_DIR32MSB";
1202 case 37: return "R_IA_64_DIR32LSB";
1203 case 38: return "R_IA_64_DIR64MSB";
1204 case 39: return "R_IA_64_DIR64LSB";
1205 case 42: return "R_IA_64_GPREL22";
1206 case 43: return "R_IA_64_GPREL64I";
1207 case 44: return "R_IA_64_GPREL32MSB";
1208 case 45: return "R_IA_64_GPREL32LSB";
1209 case 46: return "R_IA_64_GPREL64MSB";
1210 case 47: return "R_IA_64_GPREL64LSB";
1211 case 50: return "R_IA_64_LTOFF22";
1212 case 51: return "R_IA_64_LTOFF64I";
1213 case 58: return "R_IA_64_PLTOFF22";
1214 case 59: return "R_IA_64_PLTOFF64I";
1215 case 62: return "R_IA_64_PLTOFF64MSB";
1216 case 63: return "R_IA_64_PLTOFF64LSB";
1217 case 67: return "R_IA_64_FPTR64I";
1218 case 68: return "R_IA_64_FPTR32MSB";
1219 case 69: return "R_IA_64_FPTR32LSB";
1220 case 70: return "R_IA_64_FPTR64MSB";
1221 case 71: return "R_IA_64_FPTR64LSB";
1222 case 72: return "R_IA_64_PCREL60B";
1223 case 73: return "R_IA_64_PCREL21B";
1224 case 74: return "R_IA_64_PCREL21M";
1225 case 75: return "R_IA_64_PCREL21F";
1226 case 76: return "R_IA_64_PCREL32MSB";
1227 case 77: return "R_IA_64_PCREL32LSB";
1228 case 78: return "R_IA_64_PCREL64MSB";
1229 case 79: return "R_IA_64_PCREL64LSB";
1230 case 82: return "R_IA_64_LTOFF_FPTR22";
1231 case 83: return "R_IA_64_LTOFF_FPTR64I";
1232 case 84: return "R_IA_64_LTOFF_FPTR32MSB";
1233 case 85: return "R_IA_64_LTOFF_FPTR32LSB";
1234 case 86: return "R_IA_64_LTOFF_FPTR64MSB";
1235 case 87: return "R_IA_64_LTOFF_FPTR64LSB";
1236 case 92: return "R_IA_64_SEGREL32MSB";
1237 case 93: return "R_IA_64_SEGREL32LSB";
1238 case 94: return "R_IA_64_SEGREL64MSB";
1239 case 95: return "R_IA_64_SEGREL64LSB";
1240 case 100: return "R_IA_64_SECREL32MSB";
1241 case 101: return "R_IA_64_SECREL32LSB";
1242 case 102: return "R_IA_64_SECREL64MSB";
1243 case 103: return "R_IA_64_SECREL64LSB";
1244 case 108: return "R_IA_64_REL32MSB";
1245 case 109: return "R_IA_64_REL32LSB";
1246 case 110: return "R_IA_64_REL64MSB";
1247 case 111: return "R_IA_64_REL64LSB";
1248 case 116: return "R_IA_64_LTV32MSB";
1249 case 117: return "R_IA_64_LTV32LSB";
1250 case 118: return "R_IA_64_LTV64MSB";
1251 case 119: return "R_IA_64_LTV64LSB";
1252 case 121: return "R_IA_64_PCREL21BI";
1253 case 122: return "R_IA_64_PCREL22";
1254 case 123: return "R_IA_64_PCREL64I";
1255 case 128: return "R_IA_64_IPLTMSB";
1256 case 129: return "R_IA_64_IPLTLSB";
1257 case 133: return "R_IA_64_SUB";
1258 case 134: return "R_IA_64_LTOFF22X";
1259 case 135: return "R_IA_64_LDXMOV";
1260 case 145: return "R_IA_64_TPREL14";
1261 case 146: return "R_IA_64_TPREL22";
1262 case 147: return "R_IA_64_TPREL64I";
1263 case 150: return "R_IA_64_TPREL64MSB";
1264 case 151: return "R_IA_64_TPREL64LSB";
1265 case 154: return "R_IA_64_LTOFF_TPREL22";
1266 case 166: return "R_IA_64_DTPMOD64MSB";
1267 case 167: return "R_IA_64_DTPMOD64LSB";
1268 case 170: return "R_IA_64_LTOFF_DTPMOD22";
1269 case 177: return "R_IA_64_DTPREL14";
1270 case 178: return "R_IA_64_DTPREL22";
1271 case 179: return "R_IA_64_DTPREL64I";
1272 case 180: return "R_IA_64_DTPREL32MSB";
1273 case 181: return "R_IA_64_DTPREL32LSB";
1274 case 182: return "R_IA_64_DTPREL64MSB";
1275 case 183: return "R_IA_64_DTPREL64LSB";
1276 case 186: return "R_IA_64_LTOFF_DTPREL22";
1281 case 0: return "R_MIPS_NONE";
1282 case 1: return "R_MIPS_16";
1283 case 2: return "R_MIPS_32";
1284 case 3: return "R_MIPS_REL32";
1285 case 4: return "R_MIPS_26";
1286 case 5: return "R_MIPS_HI16";
1287 case 6: return "R_MIPS_LO16";
1288 case 7: return "R_MIPS_GPREL16";
1289 case 8: return "R_MIPS_LITERAL";
1290 case 9: return "R_MIPS_GOT16";
1291 case 10: return "R_MIPS_PC16";
1292 case 11: return "R_MIPS_CALL16";
1293 case 12: return "R_MIPS_GPREL32";
1294 case 21: return "R_MIPS_GOTHI16";
1295 case 22: return "R_MIPS_GOTLO16";
1296 case 30: return "R_MIPS_CALLHI16";
1297 case 31: return "R_MIPS_CALLLO16";
1302 case 0: return "R_PPC_NONE";
1303 case 1: return "R_PPC_ADDR32";
1304 case 2: return "R_PPC_ADDR24";
1305 case 3: return "R_PPC_ADDR16";
1306 case 4: return "R_PPC_ADDR16_LO";
1307 case 5: return "R_PPC_ADDR16_HI";
1308 case 6: return "R_PPC_ADDR16_HA";
1309 case 7: return "R_PPC_ADDR14";
1310 case 8: return "R_PPC_ADDR14_BRTAKEN";
1311 case 9: return "R_PPC_ADDR14_BRNTAKEN";
1312 case 10: return "R_PPC_REL24";
1313 case 11: return "R_PPC_REL14";
1314 case 12: return "R_PPC_REL14_BRTAKEN";
1315 case 13: return "R_PPC_REL14_BRNTAKEN";
1316 case 14: return "R_PPC_GOT16";
1317 case 15: return "R_PPC_GOT16_LO";
1318 case 16: return "R_PPC_GOT16_HI";
1319 case 17: return "R_PPC_GOT16_HA";
1320 case 18: return "R_PPC_PLTREL24";
1321 case 19: return "R_PPC_COPY";
1322 case 20: return "R_PPC_GLOB_DAT";
1323 case 21: return "R_PPC_JMP_SLOT";
1324 case 22: return "R_PPC_RELATIVE";
1325 case 23: return "R_PPC_LOCAL24PC";
1326 case 24: return "R_PPC_UADDR32";
1327 case 25: return "R_PPC_UADDR16";
1328 case 26: return "R_PPC_REL32";
1329 case 27: return "R_PPC_PLT32";
1330 case 28: return "R_PPC_PLTREL32";
1331 case 29: return "R_PPC_PLT16_LO";
1332 case 30: return "R_PPC_PLT16_HI";
1333 case 31: return "R_PPC_PLT16_HA";
1334 case 32: return "R_PPC_SDAREL16";
1335 case 33: return "R_PPC_SECTOFF";
1336 case 34: return "R_PPC_SECTOFF_LO";
1337 case 35: return "R_PPC_SECTOFF_HI";
1338 case 36: return "R_PPC_SECTOFF_HA";
1339 case 67: return "R_PPC_TLS";
1340 case 68: return "R_PPC_DTPMOD32";
1341 case 69: return "R_PPC_TPREL16";
1342 case 70: return "R_PPC_TPREL16_LO";
1343 case 71: return "R_PPC_TPREL16_HI";
1344 case 72: return "R_PPC_TPREL16_HA";
1345 case 73: return "R_PPC_TPREL32";
1346 case 74: return "R_PPC_DTPREL16";
1347 case 75: return "R_PPC_DTPREL16_LO";
1348 case 76: return "R_PPC_DTPREL16_HI";
1349 case 77: return "R_PPC_DTPREL16_HA";
1350 case 78: return "R_PPC_DTPREL32";
1351 case 79: return "R_PPC_GOT_TLSGD16";
1352 case 80: return "R_PPC_GOT_TLSGD16_LO";
1353 case 81: return "R_PPC_GOT_TLSGD16_HI";
1354 case 82: return "R_PPC_GOT_TLSGD16_HA";
1355 case 83: return "R_PPC_GOT_TLSLD16";
1356 case 84: return "R_PPC_GOT_TLSLD16_LO";
1357 case 85: return "R_PPC_GOT_TLSLD16_HI";
1358 case 86: return "R_PPC_GOT_TLSLD16_HA";
1359 case 87: return "R_PPC_GOT_TPREL16";
1360 case 88: return "R_PPC_GOT_TPREL16_LO";
1361 case 89: return "R_PPC_GOT_TPREL16_HI";
1362 case 90: return "R_PPC_GOT_TPREL16_HA";
1363 case 101: return "R_PPC_EMB_NADDR32";
1364 case 102: return "R_PPC_EMB_NADDR16";
1365 case 103: return "R_PPC_EMB_NADDR16_LO";
1366 case 104: return "R_PPC_EMB_NADDR16_HI";
1367 case 105: return "R_PPC_EMB_NADDR16_HA";
1368 case 106: return "R_PPC_EMB_SDAI16";
1369 case 107: return "R_PPC_EMB_SDA2I16";
1370 case 108: return "R_PPC_EMB_SDA2REL";
1371 case 109: return "R_PPC_EMB_SDA21";
1372 case 110: return "R_PPC_EMB_MRKREF";
1373 case 111: return "R_PPC_EMB_RELSEC16";
1374 case 112: return "R_PPC_EMB_RELST_LO";
1375 case 113: return "R_PPC_EMB_RELST_HI";
1376 case 114: return "R_PPC_EMB_RELST_HA";
1377 case 115: return "R_PPC_EMB_BIT_FLD";
1378 case 116: return "R_PPC_EMB_RELSDA";
1384 case 0: return "R_SPARC_NONE";
1385 case 1: return "R_SPARC_8";
1386 case 2: return "R_SPARC_16";
1387 case 3: return "R_SPARC_32";
1388 case 4: return "R_SPARC_DISP8";
1389 case 5: return "R_SPARC_DISP16";
1390 case 6: return "R_SPARC_DISP32";
1391 case 7: return "R_SPARC_WDISP30";
1392 case 8: return "R_SPARC_WDISP22";
1393 case 9: return "R_SPARC_HI22";
1394 case 10: return "R_SPARC_22";
1395 case 11: return "R_SPARC_13";
1396 case 12: return "R_SPARC_LO10";
1397 case 13: return "R_SPARC_GOT10";
1398 case 14: return "R_SPARC_GOT13";
1399 case 15: return "R_SPARC_GOT22";
1400 case 16: return "R_SPARC_PC10";
1401 case 17: return "R_SPARC_PC22";
1402 case 18: return "R_SPARC_WPLT30";
1403 case 19: return "R_SPARC_COPY";
1404 case 20: return "R_SPARC_GLOB_DAT";
1405 case 21: return "R_SPARC_JMP_SLOT";
1406 case 22: return "R_SPARC_RELATIVE";
1407 case 23: return "R_SPARC_UA32";
1408 case 24: return "R_SPARC_PLT32";
1409 case 25: return "R_SPARC_HIPLT22";
1410 case 26: return "R_SPARC_LOPLT10";
1411 case 27: return "R_SPARC_PCPLT32";
1412 case 28: return "R_SPARC_PCPLT22";
1413 case 29: return "R_SPARC_PCPLT10";
1414 case 30: return "R_SPARC_10";
1415 case 31: return "R_SPARC_11";
1416 case 32: return "R_SPARC_64";
1417 case 33: return "R_SPARC_OLO10";
1418 case 34: return "R_SPARC_HH22";
1419 case 35: return "R_SPARC_HM10";
1420 case 36: return "R_SPARC_LM22";
1421 case 37: return "R_SPARC_PC_HH22";
1422 case 38: return "R_SPARC_PC_HM10";
1423 case 39: return "R_SPARC_PC_LM22";
1424 case 40: return "R_SPARC_WDISP16";
1425 case 41: return "R_SPARC_WDISP19";
1426 case 42: return "R_SPARC_GLOB_JMP";
1427 case 43: return "R_SPARC_7";
1428 case 44: return "R_SPARC_5";
1429 case 45: return "R_SPARC_6";
1430 case 46: return "R_SPARC_DISP64";
1431 case 47: return "R_SPARC_PLT64";
1432 case 48: return "R_SPARC_HIX22";
1433 case 49: return "R_SPARC_LOX10";
1434 case 50: return "R_SPARC_H44";
1435 case 51: return "R_SPARC_M44";
1436 case 52: return "R_SPARC_L44";
1437 case 53: return "R_SPARC_REGISTER";
1438 case 54: return "R_SPARC_UA64";
1439 case 55: return "R_SPARC_UA16";
1440 case 56: return "R_SPARC_TLS_GD_HI22";
1441 case 57: return "R_SPARC_TLS_GD_LO10";
1442 case 58: return "R_SPARC_TLS_GD_ADD";
1443 case 59: return "R_SPARC_TLS_GD_CALL";
1444 case 60: return "R_SPARC_TLS_LDM_HI22";
1445 case 61: return "R_SPARC_TLS_LDM_LO10";
1446 case 62: return "R_SPARC_TLS_LDM_ADD";
1447 case 63: return "R_SPARC_TLS_LDM_CALL";
1448 case 64: return "R_SPARC_TLS_LDO_HIX22";
1449 case 65: return "R_SPARC_TLS_LDO_LOX10";
1450 case 66: return "R_SPARC_TLS_LDO_ADD";
1451 case 67: return "R_SPARC_TLS_IE_HI22";
1452 case 68: return "R_SPARC_TLS_IE_LO10";
1453 case 69: return "R_SPARC_TLS_IE_LD";
1454 case 70: return "R_SPARC_TLS_IE_LDX";
1455 case 71: return "R_SPARC_TLS_IE_ADD";
1456 case 72: return "R_SPARC_TLS_LE_HIX22";
1457 case 73: return "R_SPARC_TLS_LE_LOX10";
1458 case 74: return "R_SPARC_TLS_DTPMOD32";
1459 case 75: return "R_SPARC_TLS_DTPMOD64";
1460 case 76: return "R_SPARC_TLS_DTPOFF32";
1461 case 77: return "R_SPARC_TLS_DTPOFF64";
1462 case 78: return "R_SPARC_TLS_TPOFF32";
1463 case 79: return "R_SPARC_TLS_TPOFF64";
1468 case 0: return "R_X86_64_NONE";
1469 case 1: return "R_X86_64_64";
1470 case 2: return "R_X86_64_PC32";
1471 case 3: return "R_X86_64_GOT32";
1472 case 4: return "R_X86_64_PLT32";
1473 case 5: return "R_X86_64_COPY";
1474 case 6: return "R_X86_64_GLOB_DAT";
1475 case 7: return "R_X86_64_JMP_SLOT";
1476 case 8: return "R_X86_64_RELATIVE";
1477 case 9: return "R_X86_64_GOTPCREL";
1478 case 10: return "R_X86_64_32";
1479 case 11: return "R_X86_64_32S";
1480 case 12: return "R_X86_64_16";
1481 case 13: return "R_X86_64_PC16";
1482 case 14: return "R_X86_64_8";
1483 case 15: return "R_X86_64_PC8";
1484 case 16: return "R_X86_64_DTPMOD64";
1485 case 17: return "R_X86_64_DTPOFF64";
1486 case 18: return "R_X86_64_TPOFF64";
1487 case 19: return "R_X86_64_TLSGD";
1488 case 20: return "R_X86_64_TLSLD";
1489 case 21: return "R_X86_64_DTPOFF32";
1490 case 22: return "R_X86_64_GOTTPOFF";
1491 case 23: return "R_X86_64_TPOFF32";
1492 case 24: return "R_X86_64_PC64";
1493 case 25: return "R_X86_64_GOTOFF64";
1494 case 26: return "R_X86_64_GOTPC32";
1495 case 27: return "R_X86_64_GOT64";
1496 case 28: return "R_X86_64_GOTPCREL64";
1497 case 29: return "R_X86_64_GOTPC64";
1498 case 30: return "R_X86_64_GOTPLT64";
1499 case 31: return "R_X86_64_PLTOFF64";
1500 case 32: return "R_X86_64_SIZE32";
1501 case 33: return "R_X86_64_SIZE64";
1502 case 34: return "R_X86_64_GOTPC32_TLSDESC";
1503 case 35: return "R_X86_64_TLSDESC_CALL";
1504 case 36: return "R_X86_64_TLSDESC";
1505 case 37: return "R_X86_64_IRELATIVE";
1513 note_type(const char *name, unsigned int et, unsigned int nt)
1515 if ((strcmp(name, "CORE") == 0 || strcmp(name, "LINUX") == 0) &&
1517 return note_type_linux_core(nt);
1518 else if (strcmp(name, "FreeBSD") == 0)
1520 return note_type_freebsd_core(nt);
1522 return note_type_freebsd(nt);
1523 else if (strcmp(name, "GNU") == 0 && et != ET_CORE)
1524 return note_type_gnu(nt);
1525 else if (strcmp(name, "NetBSD") == 0 && et != ET_CORE)
1526 return note_type_netbsd(nt);
1527 else if (strcmp(name, "OpenBSD") == 0 && et != ET_CORE)
1528 return note_type_openbsd(nt);
1529 return note_type_unknown(nt);
1533 note_type_freebsd(unsigned int nt)
1536 case 1: return "NT_FREEBSD_ABI_TAG";
1537 case 2: return "NT_FREEBSD_NOINIT_TAG";
1538 case 3: return "NT_FREEBSD_ARCH_TAG";
1539 default: return (note_type_unknown(nt));
1544 note_type_freebsd_core(unsigned int nt)
1547 case 1: return "NT_PRSTATUS";
1548 case 2: return "NT_FPREGSET";
1549 case 3: return "NT_PRPSINFO";
1550 case 7: return "NT_THRMISC";
1551 case 8: return "NT_PROCSTAT_PROC";
1552 case 9: return "NT_PROCSTAT_FILES";
1553 case 10: return "NT_PROCSTAT_VMMAP";
1554 case 11: return "NT_PROCSTAT_GROUPS";
1555 case 12: return "NT_PROCSTAT_UMASK";
1556 case 13: return "NT_PROCSTAT_RLIMIT";
1557 case 14: return "NT_PROCSTAT_OSREL";
1558 case 15: return "NT_PROCSTAT_PSSTRINGS";
1559 case 16: return "NT_PROCSTAT_AUXV";
1560 case 0x202: return "NT_X86_XSTATE (x86 XSAVE extended state)";
1561 default: return (note_type_unknown(nt));
1566 note_type_linux_core(unsigned int nt)
1569 case 1: return "NT_PRSTATUS (Process status)";
1570 case 2: return "NT_FPREGSET (Floating point information)";
1571 case 3: return "NT_PRPSINFO (Process information)";
1572 case 4: return "NT_TASKSTRUCT (Task structure)";
1573 case 6: return "NT_AUXV (Auxiliary vector)";
1574 case 10: return "NT_PSTATUS (Linux process status)";
1575 case 12: return "NT_FPREGS (Linux floating point regset)";
1576 case 13: return "NT_PSINFO (Linux process information)";
1577 case 16: return "NT_LWPSTATUS (Linux lwpstatus_t type)";
1578 case 17: return "NT_LWPSINFO (Linux lwpinfo_t type)";
1579 case 18: return "NT_WIN32PSTATUS (win32_pstatus structure)";
1580 case 0x100: return "NT_PPC_VMX (ppc Altivec registers)";
1581 case 0x102: return "NT_PPC_VSX (ppc VSX registers)";
1582 case 0x202: return "NT_X86_XSTATE (x86 XSAVE extended state)";
1583 case 0x300: return "NT_S390_HIGH_GPRS (s390 upper register halves)";
1584 case 0x301: return "NT_S390_TIMER (s390 timer register)";
1585 case 0x302: return "NT_S390_TODCMP (s390 TOD comparator register)";
1586 case 0x303: return "NT_S390_TODPREG (s390 TOD programmable register)";
1587 case 0x304: return "NT_S390_CTRS (s390 control registers)";
1588 case 0x305: return "NT_S390_PREFIX (s390 prefix register)";
1589 case 0x400: return "NT_ARM_VFP (arm VFP registers)";
1590 case 0x46494c45UL: return "NT_FILE (mapped files)";
1591 case 0x46E62B7FUL: return "NT_PRXFPREG (Linux user_xfpregs structure)";
1592 case 0x53494749UL: return "NT_SIGINFO (siginfo_t data)";
1593 default: return (note_type_unknown(nt));
1598 note_type_gnu(unsigned int nt)
1601 case 1: return "NT_GNU_ABI_TAG";
1602 case 2: return "NT_GNU_HWCAP (Hardware capabilities)";
1603 case 3: return "NT_GNU_BUILD_ID (Build id set by ld(1))";
1604 case 4: return "NT_GNU_GOLD_VERSION (GNU gold version)";
1605 default: return (note_type_unknown(nt));
1610 note_type_netbsd(unsigned int nt)
1613 case 1: return "NT_NETBSD_IDENT";
1614 default: return (note_type_unknown(nt));
1619 note_type_openbsd(unsigned int nt)
1622 case 1: return "NT_OPENBSD_IDENT";
1623 default: return (note_type_unknown(nt));
1628 note_type_unknown(unsigned int nt)
1630 static char s_nt[32];
1632 snprintf(s_nt, sizeof(s_nt),
1633 nt >= 0x100 ? "<unknown: 0x%x>" : "<unknown: %u>", nt);
1641 {"EXACT_MATCH", LL_EXACT_MATCH},
1642 {"IGNORE_INT_VER", LL_IGNORE_INT_VER},
1643 {"REQUIRE_MINOR", LL_REQUIRE_MINOR},
1644 {"EXPORTS", LL_EXPORTS},
1645 {"DELAY_LOAD", LL_DELAY_LOAD},
1646 {"DELTA", LL_DELTA},
1650 static struct mips_option mips_exceptions_option[] = {
1651 {OEX_PAGE0, "PAGE0"},
1653 {OEX_PRECISEFP, "PRECISEFP"},
1654 {OEX_DISMISS, "DISMISS"},
1658 static struct mips_option mips_pad_option[] = {
1659 {OPAD_PREFIX, "PREFIX"},
1660 {OPAD_POSTFIX, "POSTFIX"},
1661 {OPAD_SYMBOL, "SYMBOL"},
1665 static struct mips_option mips_hwpatch_option[] = {
1666 {OHW_R4KEOP, "R4KEOP"},
1667 {OHW_R8KPFETCH, "R8KPFETCH"},
1668 {OHW_R5KEOP, "R5KEOP"},
1669 {OHW_R5KCVTL, "R5KCVTL"},
1673 static struct mips_option mips_hwa_option[] = {
1674 {OHWA0_R4KEOP_CHECKED, "R4KEOP_CHECKED"},
1675 {OHWA0_R4KEOP_CLEAN, "R4KEOP_CLEAN"},
1679 static struct mips_option mips_hwo_option[] = {
1680 {OHWO0_FIXADE, "FIXADE"},
1685 option_kind(uint8_t kind)
1687 static char s_kind[32];
1690 case ODK_NULL: return "NULL";
1691 case ODK_REGINFO: return "REGINFO";
1692 case ODK_EXCEPTIONS: return "EXCEPTIONS";
1693 case ODK_PAD: return "PAD";
1694 case ODK_HWPATCH: return "HWPATCH";
1695 case ODK_FILL: return "FILL";
1696 case ODK_TAGS: return "TAGS";
1697 case ODK_HWAND: return "HWAND";
1698 case ODK_HWOR: return "HWOR";
1699 case ODK_GP_GROUP: return "GP_GROUP";
1700 case ODK_IDENT: return "IDENT";
1702 snprintf(s_kind, sizeof(s_kind), "<unknown: %u>", kind);
1708 top_tag(unsigned int tag)
1710 static char s_top_tag[32];
1713 case 1: return "File Attributes";
1714 case 2: return "Section Attributes";
1715 case 3: return "Symbol Attributes";
1717 snprintf(s_top_tag, sizeof(s_top_tag), "Unknown tag: %u", tag);
1723 aeabi_cpu_arch(uint64_t arch)
1725 static char s_cpu_arch[32];
1728 case 0: return "Pre-V4";
1729 case 1: return "ARM v4";
1730 case 2: return "ARM v4T";
1731 case 3: return "ARM v5T";
1732 case 4: return "ARM v5TE";
1733 case 5: return "ARM v5TEJ";
1734 case 6: return "ARM v6";
1735 case 7: return "ARM v6KZ";
1736 case 8: return "ARM v6T2";
1737 case 9: return "ARM v6K";
1738 case 10: return "ARM v7";
1739 case 11: return "ARM v6-M";
1740 case 12: return "ARM v6S-M";
1741 case 13: return "ARM v7E-M";
1743 snprintf(s_cpu_arch, sizeof(s_cpu_arch),
1744 "Unknown (%ju)", (uintmax_t) arch);
1745 return (s_cpu_arch);
1750 aeabi_cpu_arch_profile(uint64_t pf)
1752 static char s_arch_profile[32];
1756 return "Not applicable";
1757 case 0x41: /* 'A' */
1758 return "Application Profile";
1759 case 0x52: /* 'R' */
1760 return "Real-Time Profile";
1761 case 0x4D: /* 'M' */
1762 return "Microcontroller Profile";
1763 case 0x53: /* 'S' */
1764 return "Application or Real-Time Profile";
1766 snprintf(s_arch_profile, sizeof(s_arch_profile),
1767 "Unknown (%ju)\n", (uintmax_t) pf);
1768 return (s_arch_profile);
1773 aeabi_arm_isa(uint64_t ai)
1775 static char s_ai[32];
1778 case 0: return "No";
1779 case 1: return "Yes";
1781 snprintf(s_ai, sizeof(s_ai), "Unknown (%ju)\n",
1788 aeabi_thumb_isa(uint64_t ti)
1790 static char s_ti[32];
1793 case 0: return "No";
1794 case 1: return "16-bit Thumb";
1795 case 2: return "32-bit Thumb";
1797 snprintf(s_ti, sizeof(s_ti), "Unknown (%ju)\n",
1804 aeabi_fp_arch(uint64_t fp)
1806 static char s_fp_arch[32];
1809 case 0: return "No";
1810 case 1: return "VFPv1";
1811 case 2: return "VFPv2";
1812 case 3: return "VFPv3";
1813 case 4: return "VFPv3-D16";
1814 case 5: return "VFPv4";
1815 case 6: return "VFPv4-D16";
1817 snprintf(s_fp_arch, sizeof(s_fp_arch), "Unknown (%ju)",
1824 aeabi_wmmx_arch(uint64_t wmmx)
1826 static char s_wmmx[32];
1829 case 0: return "No";
1830 case 1: return "WMMXv1";
1831 case 2: return "WMMXv2";
1833 snprintf(s_wmmx, sizeof(s_wmmx), "Unknown (%ju)",
1840 aeabi_adv_simd_arch(uint64_t simd)
1842 static char s_simd[32];
1845 case 0: return "No";
1846 case 1: return "NEONv1";
1847 case 2: return "NEONv2";
1849 snprintf(s_simd, sizeof(s_simd), "Unknown (%ju)",
1856 aeabi_pcs_config(uint64_t pcs)
1858 static char s_pcs[32];
1861 case 0: return "None";
1862 case 1: return "Bare platform";
1863 case 2: return "Linux";
1864 case 3: return "Linux DSO";
1865 case 4: return "Palm OS 2004";
1866 case 5: return "Palm OS (future)";
1867 case 6: return "Symbian OS 2004";
1868 case 7: return "Symbian OS (future)";
1870 snprintf(s_pcs, sizeof(s_pcs), "Unknown (%ju)",
1877 aeabi_pcs_r9(uint64_t r9)
1879 static char s_r9[32];
1882 case 0: return "V6";
1883 case 1: return "SB";
1884 case 2: return "TLS pointer";
1885 case 3: return "Unused";
1887 snprintf(s_r9, sizeof(s_r9), "Unknown (%ju)", (uintmax_t) r9);
1893 aeabi_pcs_rw(uint64_t rw)
1895 static char s_rw[32];
1898 case 0: return "Absolute";
1899 case 1: return "PC-relative";
1900 case 2: return "SB-relative";
1901 case 3: return "None";
1903 snprintf(s_rw, sizeof(s_rw), "Unknown (%ju)", (uintmax_t) rw);
1909 aeabi_pcs_ro(uint64_t ro)
1911 static char s_ro[32];
1914 case 0: return "Absolute";
1915 case 1: return "PC-relative";
1916 case 2: return "None";
1918 snprintf(s_ro, sizeof(s_ro), "Unknown (%ju)", (uintmax_t) ro);
1924 aeabi_pcs_got(uint64_t got)
1926 static char s_got[32];
1929 case 0: return "None";
1930 case 1: return "direct";
1931 case 2: return "indirect via GOT";
1933 snprintf(s_got, sizeof(s_got), "Unknown (%ju)",
1940 aeabi_pcs_wchar_t(uint64_t wt)
1942 static char s_wt[32];
1945 case 0: return "None";
1946 case 2: return "wchar_t size 2";
1947 case 4: return "wchar_t size 4";
1949 snprintf(s_wt, sizeof(s_wt), "Unknown (%ju)", (uintmax_t) wt);
1955 aeabi_enum_size(uint64_t es)
1957 static char s_es[32];
1960 case 0: return "None";
1961 case 1: return "smallest";
1962 case 2: return "32-bit";
1963 case 3: return "visible 32-bit";
1965 snprintf(s_es, sizeof(s_es), "Unknown (%ju)", (uintmax_t) es);
1971 aeabi_align_needed(uint64_t an)
1973 static char s_align_n[64];
1976 case 0: return "No";
1977 case 1: return "8-byte align";
1978 case 2: return "4-byte align";
1979 case 3: return "Reserved";
1981 if (an >= 4 && an <= 12)
1982 snprintf(s_align_n, sizeof(s_align_n), "8-byte align"
1983 " and up to 2^%ju-byte extended align",
1986 snprintf(s_align_n, sizeof(s_align_n), "Unknown (%ju)",
1993 aeabi_align_preserved(uint64_t ap)
1995 static char s_align_p[128];
1998 case 0: return "No";
1999 case 1: return "8-byte align";
2000 case 2: return "8-byte align and SP % 8 == 0";
2001 case 3: return "Reserved";
2003 if (ap >= 4 && ap <= 12)
2004 snprintf(s_align_p, sizeof(s_align_p), "8-byte align"
2005 " and SP %% 8 == 0 and up to 2^%ju-byte extended"
2006 " align", (uintmax_t) ap);
2008 snprintf(s_align_p, sizeof(s_align_p), "Unknown (%ju)",
2015 aeabi_fp_rounding(uint64_t fr)
2017 static char s_fp_r[32];
2020 case 0: return "Unused";
2021 case 1: return "Needed";
2023 snprintf(s_fp_r, sizeof(s_fp_r), "Unknown (%ju)",
2030 aeabi_fp_denormal(uint64_t fd)
2032 static char s_fp_d[32];
2035 case 0: return "Unused";
2036 case 1: return "Needed";
2037 case 2: return "Sign Only";
2039 snprintf(s_fp_d, sizeof(s_fp_d), "Unknown (%ju)",
2046 aeabi_fp_exceptions(uint64_t fe)
2048 static char s_fp_e[32];
2051 case 0: return "Unused";
2052 case 1: return "Needed";
2054 snprintf(s_fp_e, sizeof(s_fp_e), "Unknown (%ju)",
2061 aeabi_fp_user_exceptions(uint64_t fu)
2063 static char s_fp_u[32];
2066 case 0: return "Unused";
2067 case 1: return "Needed";
2069 snprintf(s_fp_u, sizeof(s_fp_u), "Unknown (%ju)",
2076 aeabi_fp_number_model(uint64_t fn)
2078 static char s_fp_n[32];
2081 case 0: return "Unused";
2082 case 1: return "IEEE 754 normal";
2083 case 2: return "RTABI";
2084 case 3: return "IEEE 754";
2086 snprintf(s_fp_n, sizeof(s_fp_n), "Unknown (%ju)",
2093 aeabi_fp_16bit_format(uint64_t fp16)
2095 static char s_fp_16[64];
2098 case 0: return "None";
2099 case 1: return "IEEE 754";
2100 case 2: return "VFPv3/Advanced SIMD (alternative format)";
2102 snprintf(s_fp_16, sizeof(s_fp_16), "Unknown (%ju)",
2109 aeabi_mpext(uint64_t mp)
2111 static char s_mp[32];
2114 case 0: return "Not allowed";
2115 case 1: return "Allowed";
2117 snprintf(s_mp, sizeof(s_mp), "Unknown (%ju)",
2124 aeabi_div(uint64_t du)
2126 static char s_du[32];
2129 case 0: return "Yes (V7-R/V7-M)";
2130 case 1: return "No";
2131 case 2: return "Yes (V7-A)";
2133 snprintf(s_du, sizeof(s_du), "Unknown (%ju)",
2140 aeabi_t2ee(uint64_t t2ee)
2142 static char s_t2ee[32];
2145 case 0: return "Not allowed";
2146 case 1: return "Allowed";
2148 snprintf(s_t2ee, sizeof(s_t2ee), "Unknown(%ju)",
2156 aeabi_hardfp(uint64_t hfp)
2158 static char s_hfp[32];
2161 case 0: return "Tag_FP_arch";
2162 case 1: return "only SP";
2163 case 2: return "only DP";
2164 case 3: return "both SP and DP";
2166 snprintf(s_hfp, sizeof(s_hfp), "Unknown (%ju)",
2173 aeabi_vfp_args(uint64_t va)
2175 static char s_va[32];
2178 case 0: return "AAPCS (base variant)";
2179 case 1: return "AAPCS (VFP variant)";
2180 case 2: return "toolchain-specific";
2182 snprintf(s_va, sizeof(s_va), "Unknown (%ju)", (uintmax_t) va);
2188 aeabi_wmmx_args(uint64_t wa)
2190 static char s_wa[32];
2193 case 0: return "AAPCS (base variant)";
2194 case 1: return "Intel WMMX";
2195 case 2: return "toolchain-specific";
2197 snprintf(s_wa, sizeof(s_wa), "Unknown(%ju)", (uintmax_t) wa);
2203 aeabi_unaligned_access(uint64_t ua)
2205 static char s_ua[32];
2208 case 0: return "Not allowed";
2209 case 1: return "Allowed";
2211 snprintf(s_ua, sizeof(s_ua), "Unknown(%ju)", (uintmax_t) ua);
2217 aeabi_fp_hpext(uint64_t fh)
2219 static char s_fh[32];
2222 case 0: return "Not allowed";
2223 case 1: return "Allowed";
2225 snprintf(s_fh, sizeof(s_fh), "Unknown(%ju)", (uintmax_t) fh);
2231 aeabi_optm_goal(uint64_t og)
2233 static char s_og[32];
2236 case 0: return "None";
2237 case 1: return "Speed";
2238 case 2: return "Speed aggressive";
2239 case 3: return "Space";
2240 case 4: return "Space aggressive";
2241 case 5: return "Debugging";
2242 case 6: return "Best Debugging";
2244 snprintf(s_og, sizeof(s_og), "Unknown(%ju)", (uintmax_t) og);
2250 aeabi_fp_optm_goal(uint64_t fog)
2252 static char s_fog[32];
2255 case 0: return "None";
2256 case 1: return "Speed";
2257 case 2: return "Speed aggressive";
2258 case 3: return "Space";
2259 case 4: return "Space aggressive";
2260 case 5: return "Accurary";
2261 case 6: return "Best Accurary";
2263 snprintf(s_fog, sizeof(s_fog), "Unknown(%ju)",
2270 aeabi_virtual(uint64_t vt)
2272 static char s_virtual[64];
2275 case 0: return "No";
2276 case 1: return "TrustZone";
2277 case 2: return "Virtualization extension";
2278 case 3: return "TrustZone and virtualization extension";
2280 snprintf(s_virtual, sizeof(s_virtual), "Unknown(%ju)",
2289 const char *(*get_desc)(uint64_t val);
2291 {4, "Tag_CPU_raw_name", NULL},
2292 {5, "Tag_CPU_name", NULL},
2293 {6, "Tag_CPU_arch", aeabi_cpu_arch},
2294 {7, "Tag_CPU_arch_profile", aeabi_cpu_arch_profile},
2295 {8, "Tag_ARM_ISA_use", aeabi_arm_isa},
2296 {9, "Tag_THUMB_ISA_use", aeabi_thumb_isa},
2297 {10, "Tag_FP_arch", aeabi_fp_arch},
2298 {11, "Tag_WMMX_arch", aeabi_wmmx_arch},
2299 {12, "Tag_Advanced_SIMD_arch", aeabi_adv_simd_arch},
2300 {13, "Tag_PCS_config", aeabi_pcs_config},
2301 {14, "Tag_ABI_PCS_R9_use", aeabi_pcs_r9},
2302 {15, "Tag_ABI_PCS_RW_data", aeabi_pcs_rw},
2303 {16, "Tag_ABI_PCS_RO_data", aeabi_pcs_ro},
2304 {17, "Tag_ABI_PCS_GOT_use", aeabi_pcs_got},
2305 {18, "Tag_ABI_PCS_wchar_t", aeabi_pcs_wchar_t},
2306 {19, "Tag_ABI_FP_rounding", aeabi_fp_rounding},
2307 {20, "Tag_ABI_FP_denormal", aeabi_fp_denormal},
2308 {21, "Tag_ABI_FP_exceptions", aeabi_fp_exceptions},
2309 {22, "Tag_ABI_FP_user_exceptions", aeabi_fp_user_exceptions},
2310 {23, "Tag_ABI_FP_number_model", aeabi_fp_number_model},
2311 {24, "Tag_ABI_align_needed", aeabi_align_needed},
2312 {25, "Tag_ABI_align_preserved", aeabi_align_preserved},
2313 {26, "Tag_ABI_enum_size", aeabi_enum_size},
2314 {27, "Tag_ABI_HardFP_use", aeabi_hardfp},
2315 {28, "Tag_ABI_VFP_args", aeabi_vfp_args},
2316 {29, "Tag_ABI_WMMX_args", aeabi_wmmx_args},
2317 {30, "Tag_ABI_optimization_goals", aeabi_optm_goal},
2318 {31, "Tag_ABI_FP_optimization_goals", aeabi_fp_optm_goal},
2319 {32, "Tag_compatibility", NULL},
2320 {34, "Tag_CPU_unaligned_access", aeabi_unaligned_access},
2321 {36, "Tag_FP_HP_extension", aeabi_fp_hpext},
2322 {38, "Tag_ABI_FP_16bit_format", aeabi_fp_16bit_format},
2323 {42, "Tag_MPextension_use", aeabi_mpext},
2324 {44, "Tag_DIV_use", aeabi_div},
2325 {64, "Tag_nodefaults", NULL},
2326 {65, "Tag_also_compatible_with", NULL},
2327 {66, "Tag_T2EE_use", aeabi_t2ee},
2328 {67, "Tag_conformance", NULL},
2329 {68, "Tag_Virtualization_use", aeabi_virtual},
2330 {70, "Tag_MPextension_use", aeabi_mpext},
2334 mips_abi_fp(uint64_t fp)
2336 static char s_mips_abi_fp[64];
2339 case 0: return "N/A";
2340 case 1: return "Hard float (double precision)";
2341 case 2: return "Hard float (single precision)";
2342 case 3: return "Soft float";
2343 case 4: return "64-bit float (-mips32r2 -mfp64)";
2345 snprintf(s_mips_abi_fp, sizeof(s_mips_abi_fp), "Unknown(%ju)",
2347 return (s_mips_abi_fp);
2352 ppc_abi_fp(uint64_t fp)
2354 static char s_ppc_abi_fp[64];
2357 case 0: return "N/A";
2358 case 1: return "Hard float (double precision)";
2359 case 2: return "Soft float";
2360 case 3: return "Hard float (single precision)";
2362 snprintf(s_ppc_abi_fp, sizeof(s_ppc_abi_fp), "Unknown(%ju)",
2364 return (s_ppc_abi_fp);
2369 ppc_abi_vector(uint64_t vec)
2371 static char s_vec[64];
2374 case 0: return "N/A";
2375 case 1: return "Generic purpose registers";
2376 case 2: return "AltiVec registers";
2377 case 3: return "SPE registers";
2379 snprintf(s_vec, sizeof(s_vec), "Unknown(%ju)", (uintmax_t) vec);
2385 dwarf_reg(unsigned int mach, unsigned int reg)
2392 case 0: return "eax";
2393 case 1: return "ecx";
2394 case 2: return "edx";
2395 case 3: return "ebx";
2396 case 4: return "esp";
2397 case 5: return "ebp";
2398 case 6: return "esi";
2399 case 7: return "edi";
2400 case 8: return "eip";
2401 case 9: return "eflags";
2402 case 11: return "st0";
2403 case 12: return "st1";
2404 case 13: return "st2";
2405 case 14: return "st3";
2406 case 15: return "st4";
2407 case 16: return "st5";
2408 case 17: return "st6";
2409 case 18: return "st7";
2410 case 21: return "xmm0";
2411 case 22: return "xmm1";
2412 case 23: return "xmm2";
2413 case 24: return "xmm3";
2414 case 25: return "xmm4";
2415 case 26: return "xmm5";
2416 case 27: return "xmm6";
2417 case 28: return "xmm7";
2418 case 29: return "mm0";
2419 case 30: return "mm1";
2420 case 31: return "mm2";
2421 case 32: return "mm3";
2422 case 33: return "mm4";
2423 case 34: return "mm5";
2424 case 35: return "mm6";
2425 case 36: return "mm7";
2426 case 37: return "fcw";
2427 case 38: return "fsw";
2428 case 39: return "mxcsr";
2429 case 40: return "es";
2430 case 41: return "cs";
2431 case 42: return "ss";
2432 case 43: return "ds";
2433 case 44: return "fs";
2434 case 45: return "gs";
2435 case 48: return "tr";
2436 case 49: return "ldtr";
2437 default: return (NULL);
2441 case 0: return "rax";
2442 case 1: return "rdx";
2443 case 2: return "rcx";
2444 case 3: return "rbx";
2445 case 4: return "rsi";
2446 case 5: return "rdi";
2447 case 6: return "rbp";
2448 case 7: return "rsp";
2449 case 16: return "rip";
2450 case 17: return "xmm0";
2451 case 18: return "xmm1";
2452 case 19: return "xmm2";
2453 case 20: return "xmm3";
2454 case 21: return "xmm4";
2455 case 22: return "xmm5";
2456 case 23: return "xmm6";
2457 case 24: return "xmm7";
2458 case 25: return "xmm8";
2459 case 26: return "xmm9";
2460 case 27: return "xmm10";
2461 case 28: return "xmm11";
2462 case 29: return "xmm12";
2463 case 30: return "xmm13";
2464 case 31: return "xmm14";
2465 case 32: return "xmm15";
2466 case 33: return "st0";
2467 case 34: return "st1";
2468 case 35: return "st2";
2469 case 36: return "st3";
2470 case 37: return "st4";
2471 case 38: return "st5";
2472 case 39: return "st6";
2473 case 40: return "st7";
2474 case 41: return "mm0";
2475 case 42: return "mm1";
2476 case 43: return "mm2";
2477 case 44: return "mm3";
2478 case 45: return "mm4";
2479 case 46: return "mm5";
2480 case 47: return "mm6";
2481 case 48: return "mm7";
2482 case 49: return "rflags";
2483 case 50: return "es";
2484 case 51: return "cs";
2485 case 52: return "ss";
2486 case 53: return "ds";
2487 case 54: return "fs";
2488 case 55: return "gs";
2489 case 58: return "fs.base";
2490 case 59: return "gs.base";
2491 case 62: return "tr";
2492 case 63: return "ldtr";
2493 case 64: return "mxcsr";
2494 case 65: return "fcw";
2495 case 66: return "fsw";
2496 default: return (NULL);
2504 dump_ehdr(struct readelf *re)
2506 size_t shnum, shstrndx;
2509 printf("ELF Header:\n");
2513 for (i = 0; i < EI_NIDENT; i++)
2514 printf("%.2x ", re->ehdr.e_ident[i]);
2518 printf("%-37s%s\n", " Class:", elf_class(re->ehdr.e_ident[EI_CLASS]));
2521 printf("%-37s%s\n", " Data:", elf_endian(re->ehdr.e_ident[EI_DATA]));
2524 printf("%-37s%d %s\n", " Version:", re->ehdr.e_ident[EI_VERSION],
2525 elf_ver(re->ehdr.e_ident[EI_VERSION]));
2528 printf("%-37s%s\n", " OS/ABI:", elf_osabi(re->ehdr.e_ident[EI_OSABI]));
2530 /* EI_ABIVERSION. */
2531 printf("%-37s%d\n", " ABI Version:", re->ehdr.e_ident[EI_ABIVERSION]);
2534 printf("%-37s%s\n", " Type:", elf_type(re->ehdr.e_type));
2537 printf("%-37s%s\n", " Machine:", elf_machine(re->ehdr.e_machine));
2540 printf("%-37s%#x\n", " Version:", re->ehdr.e_version);
2543 printf("%-37s%#jx\n", " Entry point address:",
2544 (uintmax_t)re->ehdr.e_entry);
2547 printf("%-37s%ju (bytes into file)\n", " Start of program headers:",
2548 (uintmax_t)re->ehdr.e_phoff);
2551 printf("%-37s%ju (bytes into file)\n", " Start of section headers:",
2552 (uintmax_t)re->ehdr.e_shoff);
2555 printf("%-37s%#x", " Flags:", re->ehdr.e_flags);
2556 dump_eflags(re, re->ehdr.e_flags);
2560 printf("%-37s%u (bytes)\n", " Size of this header:",
2564 printf("%-37s%u (bytes)\n", " Size of program headers:",
2565 re->ehdr.e_phentsize);
2568 printf("%-37s%u\n", " Number of program headers:", re->ehdr.e_phnum);
2571 printf("%-37s%u (bytes)\n", " Size of section headers:",
2572 re->ehdr.e_shentsize);
2575 printf("%-37s%u", " Number of section headers:", re->ehdr.e_shnum);
2576 if (re->ehdr.e_shnum == SHN_UNDEF) {
2577 /* Extended section numbering is in use. */
2578 if (elf_getshnum(re->elf, &shnum))
2579 printf(" (%ju)", (uintmax_t)shnum);
2584 printf("%-37s%u", " Section header string table index:",
2585 re->ehdr.e_shstrndx);
2586 if (re->ehdr.e_shstrndx == SHN_XINDEX) {
2587 /* Extended section numbering is in use. */
2588 if (elf_getshstrndx(re->elf, &shstrndx))
2589 printf(" (%ju)", (uintmax_t)shstrndx);
2595 dump_eflags(struct readelf *re, uint64_t e_flags)
2597 struct eflags_desc *edesc;
2601 switch (re->ehdr.e_machine) {
2603 arm_eabi = (e_flags & EF_ARM_EABIMASK) >> 24;
2605 printf(", GNU EABI");
2606 else if (arm_eabi <= 5)
2607 printf(", Version%d EABI", arm_eabi);
2608 edesc = arm_eflags_desc;
2611 case EM_MIPS_RS3_LE:
2612 switch ((e_flags & EF_MIPS_ARCH) >> 28) {
2613 case 0: printf(", mips1"); break;
2614 case 1: printf(", mips2"); break;
2615 case 2: printf(", mips3"); break;
2616 case 3: printf(", mips4"); break;
2617 case 4: printf(", mips5"); break;
2618 case 5: printf(", mips32"); break;
2619 case 6: printf(", mips64"); break;
2620 case 7: printf(", mips32r2"); break;
2621 case 8: printf(", mips64r2"); break;
2624 switch ((e_flags & 0x00FF0000) >> 16) {
2625 case 0x81: printf(", 3900"); break;
2626 case 0x82: printf(", 4010"); break;
2627 case 0x83: printf(", 4100"); break;
2628 case 0x85: printf(", 4650"); break;
2629 case 0x87: printf(", 4120"); break;
2630 case 0x88: printf(", 4111"); break;
2631 case 0x8a: printf(", sb1"); break;
2632 case 0x8b: printf(", octeon"); break;
2633 case 0x8c: printf(", xlr"); break;
2634 case 0x91: printf(", 5400"); break;
2635 case 0x98: printf(", 5500"); break;
2636 case 0x99: printf(", 9000"); break;
2637 case 0xa0: printf(", loongson-2e"); break;
2638 case 0xa1: printf(", loongson-2f"); break;
2641 switch ((e_flags & 0x0000F000) >> 12) {
2642 case 1: printf(", o32"); break;
2643 case 2: printf(", o64"); break;
2644 case 3: printf(", eabi32"); break;
2645 case 4: printf(", eabi64"); break;
2648 edesc = mips_eflags_desc;
2652 edesc = powerpc_eflags_desc;
2655 case EM_SPARC32PLUS:
2657 switch ((e_flags & EF_SPARCV9_MM)) {
2658 case EF_SPARCV9_TSO: printf(", tso"); break;
2659 case EF_SPARCV9_PSO: printf(", pso"); break;
2660 case EF_SPARCV9_MM: printf(", rmo"); break;
2663 edesc = sparc_eflags_desc;
2669 if (edesc != NULL) {
2670 while (edesc->desc != NULL) {
2671 if (e_flags & edesc->flag)
2672 printf(", %s", edesc->desc);
2679 dump_phdr(struct readelf *re)
2681 const char *rawfile;
2686 #define PH_HDR "Type", "Offset", "VirtAddr", "PhysAddr", "FileSiz", \
2687 "MemSiz", "Flg", "Align"
2688 #define PH_CT phdr_type(phdr.p_type), (uintmax_t)phdr.p_offset, \
2689 (uintmax_t)phdr.p_vaddr, (uintmax_t)phdr.p_paddr, \
2690 (uintmax_t)phdr.p_filesz, (uintmax_t)phdr.p_memsz, \
2691 phdr.p_flags & PF_R ? 'R' : ' ', \
2692 phdr.p_flags & PF_W ? 'W' : ' ', \
2693 phdr.p_flags & PF_X ? 'E' : ' ', \
2694 (uintmax_t)phdr.p_align
2696 if (elf_getphnum(re->elf, &phnum) == 0) {
2697 warnx("elf_getphnum failed: %s", elf_errmsg(-1));
2701 printf("\nThere are no program headers in this file.\n");
2705 printf("\nElf file type is %s", elf_type(re->ehdr.e_type));
2706 printf("\nEntry point 0x%jx\n", (uintmax_t)re->ehdr.e_entry);
2707 printf("There are %ju program headers, starting at offset %ju\n",
2708 (uintmax_t)phnum, (uintmax_t)re->ehdr.e_phoff);
2710 /* Dump program headers. */
2711 printf("\nProgram Headers:\n");
2712 if (re->ec == ELFCLASS32)
2713 printf(" %-15s%-9s%-11s%-11s%-8s%-8s%-4s%s\n", PH_HDR);
2714 else if (re->options & RE_WW)
2715 printf(" %-15s%-9s%-19s%-19s%-9s%-9s%-4s%s\n", PH_HDR);
2717 printf(" %-15s%-19s%-19s%s\n %-19s%-20s"
2718 "%-7s%s\n", PH_HDR);
2719 for (i = 0; (size_t) i < phnum; i++) {
2720 if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
2721 warnx("gelf_getphdr failed: %s", elf_errmsg(-1));
2724 /* TODO: Add arch-specific segment type dump. */
2725 if (re->ec == ELFCLASS32)
2726 printf(" %-14.14s 0x%6.6jx 0x%8.8jx 0x%8.8jx "
2727 "0x%5.5jx 0x%5.5jx %c%c%c %#jx\n", PH_CT);
2728 else if (re->options & RE_WW)
2729 printf(" %-14.14s 0x%6.6jx 0x%16.16jx 0x%16.16jx "
2730 "0x%6.6jx 0x%6.6jx %c%c%c %#jx\n", PH_CT);
2732 printf(" %-14.14s 0x%16.16jx 0x%16.16jx 0x%16.16jx\n"
2733 " 0x%16.16jx 0x%16.16jx %c%c%c"
2735 if (phdr.p_type == PT_INTERP) {
2736 if ((rawfile = elf_rawfile(re->elf, &size)) == NULL) {
2737 warnx("elf_rawfile failed: %s", elf_errmsg(-1));
2740 if (phdr.p_offset >= size) {
2741 warnx("invalid program header offset");
2744 printf(" [Requesting program interpreter: %s]\n",
2745 rawfile + phdr.p_offset);
2749 /* Dump section to segment mapping. */
2752 printf("\n Section to Segment mapping:\n");
2753 printf(" Segment Sections...\n");
2754 for (i = 0; (size_t)i < phnum; i++) {
2755 if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
2756 warnx("gelf_getphdr failed: %s", elf_errmsg(-1));
2759 printf(" %2.2d ", i);
2760 /* skip NULL section. */
2761 for (j = 1; (size_t)j < re->shnum; j++)
2762 if (re->sl[j].off >= phdr.p_offset &&
2763 re->sl[j].off + re->sl[j].sz <=
2764 phdr.p_offset + phdr.p_memsz)
2765 printf("%s ", re->sl[j].name);
2773 section_flags(struct readelf *re, struct section *s)
2776 static char buf[BUF_SZ];
2780 nb = re->ec == ELFCLASS32 ? 8 : 16;
2781 if (re->options & RE_T) {
2782 snprintf(buf, BUF_SZ, "[%*.*jx]: ", nb, nb,
2783 (uintmax_t)s->flags);
2786 for (i = 0; section_flag[i].ln != NULL; i++) {
2787 if ((s->flags & section_flag[i].value) == 0)
2789 if (re->options & RE_T) {
2790 snprintf(&buf[p], BUF_SZ - p, "%s, ",
2791 section_flag[i].ln);
2792 p += strlen(section_flag[i].ln) + 2;
2794 buf[p++] = section_flag[i].sn;
2796 if (re->options & RE_T && p > nb + 4)
2804 dump_shdr(struct readelf *re)
2809 #define S_HDR "[Nr] Name", "Type", "Addr", "Off", "Size", "ES", \
2810 "Flg", "Lk", "Inf", "Al"
2811 #define S_HDRL "[Nr] Name", "Type", "Address", "Offset", "Size", \
2812 "EntSize", "Flags", "Link", "Info", "Align"
2813 #define ST_HDR "[Nr] Name", "Type", "Addr", "Off", "Size", "ES", \
2814 "Lk", "Inf", "Al", "Flags"
2815 #define ST_HDRL "[Nr] Name", "Type", "Address", "Offset", "Link", \
2816 "Size", "EntSize", "Info", "Align", "Flags"
2817 #define S_CT i, s->name, section_type(re->ehdr.e_machine, s->type), \
2818 (uintmax_t)s->addr, (uintmax_t)s->off, (uintmax_t)s->sz,\
2819 (uintmax_t)s->entsize, section_flags(re, s), \
2820 s->link, s->info, (uintmax_t)s->align
2821 #define ST_CT i, s->name, section_type(re->ehdr.e_machine, s->type), \
2822 (uintmax_t)s->addr, (uintmax_t)s->off, (uintmax_t)s->sz,\
2823 (uintmax_t)s->entsize, s->link, s->info, \
2824 (uintmax_t)s->align, section_flags(re, s)
2825 #define ST_CTL i, s->name, section_type(re->ehdr.e_machine, s->type), \
2826 (uintmax_t)s->addr, (uintmax_t)s->off, s->link, \
2827 (uintmax_t)s->sz, (uintmax_t)s->entsize, s->info, \
2828 (uintmax_t)s->align, section_flags(re, s)
2830 if (re->shnum == 0) {
2831 printf("\nThere are no sections in this file.\n");
2834 printf("There are %ju section headers, starting at offset 0x%jx:\n",
2835 (uintmax_t)re->shnum, (uintmax_t)re->ehdr.e_shoff);
2836 printf("\nSection Headers:\n");
2837 if (re->ec == ELFCLASS32) {
2838 if (re->options & RE_T)
2839 printf(" %s\n %-16s%-9s%-7s%-7s%-5s%-3s%-4s%s\n"
2842 printf(" %-23s%-16s%-9s%-7s%-7s%-3s%-4s%-3s%-4s%s\n",
2844 } else if (re->options & RE_WW) {
2845 if (re->options & RE_T)
2846 printf(" %s\n %-16s%-17s%-7s%-7s%-5s%-3s%-4s%s\n"
2849 printf(" %-23s%-16s%-17s%-7s%-7s%-3s%-4s%-3s%-4s%s\n",
2852 if (re->options & RE_T)
2853 printf(" %s\n %-18s%-17s%-18s%s\n %-18s"
2854 "%-17s%-18s%s\n%12s\n", ST_HDRL);
2856 printf(" %-23s%-17s%-18s%s\n %-18s%-17s%-7s%"
2857 "-6s%-6s%s\n", S_HDRL);
2859 for (i = 0; (size_t)i < re->shnum; i++) {
2861 if (re->ec == ELFCLASS32) {
2862 if (re->options & RE_T)
2863 printf(" [%2d] %s\n %-15.15s %8.8jx"
2864 " %6.6jx %6.6jx %2.2jx %2u %3u %2ju\n"
2867 printf(" [%2d] %-17.17s %-15.15s %8.8jx"
2868 " %6.6jx %6.6jx %2.2jx %3s %2u %3u %2ju\n",
2870 } else if (re->options & RE_WW) {
2871 if (re->options & RE_T)
2872 printf(" [%2d] %s\n %-15.15s %16.16jx"
2873 " %6.6jx %6.6jx %2.2jx %2u %3u %2ju\n"
2876 printf(" [%2d] %-17.17s %-15.15s %16.16jx"
2877 " %6.6jx %6.6jx %2.2jx %3s %2u %3u %2ju\n",
2880 if (re->options & RE_T)
2881 printf(" [%2d] %s\n %-15.15s %16.16jx"
2882 " %16.16jx %u\n %16.16jx %16.16jx"
2883 " %-16u %ju\n %s\n", ST_CTL);
2885 printf(" [%2d] %-17.17s %-15.15s %16.16jx"
2886 " %8.8jx\n %16.16jx %16.16jx "
2887 "%3s %2u %3u %ju\n", S_CT);
2890 if ((re->options & RE_T) == 0)
2891 printf("Key to Flags:\n W (write), A (alloc),"
2892 " X (execute), M (merge), S (strings)\n"
2893 " I (info), L (link order), G (group), x (unknown)\n"
2894 " O (extra OS processing required)"
2895 " o (OS specific), p (processor specific)\n");
2907 * Return number of entries in the given section. We'd prefer ent_count be a
2908 * size_t *, but libelf APIs already use int for section indices.
2911 get_ent_count(struct section *s, int *ent_count)
2913 if (s->entsize == 0) {
2914 warnx("section %s has entry size 0", s->name);
2916 } else if (s->sz / s->entsize > INT_MAX) {
2917 warnx("section %s has invalid section count", s->name);
2920 *ent_count = (int)(s->sz / s->entsize);
2925 dump_dynamic(struct readelf *re)
2930 int elferr, i, is_dynamic, j, jmax, nentries;
2934 for (i = 0; (size_t)i < re->shnum; i++) {
2936 if (s->type != SHT_DYNAMIC)
2939 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
2940 elferr = elf_errno();
2942 warnx("elf_getdata failed: %s", elf_errmsg(-1));
2950 /* Determine the actual number of table entries. */
2952 if (!get_ent_count(s, &jmax))
2954 for (j = 0; j < jmax; j++) {
2955 if (gelf_getdyn(d, j, &dyn) != &dyn) {
2956 warnx("gelf_getdyn failed: %s",
2961 if (dyn.d_tag == DT_NULL)
2965 printf("\nDynamic section at offset 0x%jx", (uintmax_t)s->off);
2966 printf(" contains %u entries:\n", nentries);
2968 if (re->ec == ELFCLASS32)
2969 printf("%5s%12s%28s\n", "Tag", "Type", "Name/Value");
2971 printf("%5s%20s%28s\n", "Tag", "Type", "Name/Value");
2973 for (j = 0; j < nentries; j++) {
2974 if (gelf_getdyn(d, j, &dyn) != &dyn)
2976 /* Dump dynamic entry type. */
2977 if (re->ec == ELFCLASS32)
2978 printf(" 0x%8.8jx", (uintmax_t)dyn.d_tag);
2980 printf(" 0x%16.16jx", (uintmax_t)dyn.d_tag);
2981 printf(" %-20s", dt_type(re->ehdr.e_machine,
2983 /* Dump dynamic entry value. */
2984 dump_dyn_val(re, &dyn, s->link);
2989 printf("\nThere is no dynamic section in this file.\n");
2993 timestamp(time_t ti)
2999 snprintf(ts, sizeof(ts), "%04d-%02d-%02dT%02d:%02d:%02d",
3000 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour,
3001 t->tm_min, t->tm_sec);
3007 dyn_str(struct readelf *re, uint32_t stab, uint64_t d_val)
3011 if (stab == SHN_UNDEF)
3013 else if ((name = elf_strptr(re->elf, stab, d_val)) == NULL) {
3014 (void) elf_errno(); /* clear error */
3022 dump_arch_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab)
3026 switch (re->ehdr.e_machine) {
3028 case EM_MIPS_RS3_LE:
3029 switch (dyn->d_tag) {
3030 case DT_MIPS_RLD_VERSION:
3031 case DT_MIPS_LOCAL_GOTNO:
3032 case DT_MIPS_CONFLICTNO:
3033 case DT_MIPS_LIBLISTNO:
3034 case DT_MIPS_SYMTABNO:
3035 case DT_MIPS_UNREFEXTNO:
3036 case DT_MIPS_GOTSYM:
3037 case DT_MIPS_HIPAGENO:
3038 case DT_MIPS_DELTA_CLASS_NO:
3039 case DT_MIPS_DELTA_INSTANCE_NO:
3040 case DT_MIPS_DELTA_RELOC_NO:
3041 case DT_MIPS_DELTA_SYM_NO:
3042 case DT_MIPS_DELTA_CLASSSYM_NO:
3043 case DT_MIPS_LOCALPAGE_GOTIDX:
3044 case DT_MIPS_LOCAL_GOTIDX:
3045 case DT_MIPS_HIDDEN_GOTIDX:
3046 case DT_MIPS_PROTECTED_GOTIDX:
3047 printf(" %ju\n", (uintmax_t) dyn->d_un.d_val);
3049 case DT_MIPS_ICHECKSUM:
3051 case DT_MIPS_BASE_ADDRESS:
3052 case DT_MIPS_CONFLICT:
3053 case DT_MIPS_LIBLIST:
3054 case DT_MIPS_RLD_MAP:
3055 case DT_MIPS_DELTA_CLASS:
3056 case DT_MIPS_DELTA_INSTANCE:
3057 case DT_MIPS_DELTA_RELOC:
3058 case DT_MIPS_DELTA_SYM:
3059 case DT_MIPS_DELTA_CLASSSYM:
3060 case DT_MIPS_CXX_FLAGS:
3061 case DT_MIPS_PIXIE_INIT:
3062 case DT_MIPS_SYMBOL_LIB:
3063 case DT_MIPS_OPTIONS:
3064 case DT_MIPS_INTERFACE:
3065 case DT_MIPS_DYNSTR_ALIGN:
3066 case DT_MIPS_INTERFACE_SIZE:
3067 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
3068 case DT_MIPS_COMPACT_SIZE:
3069 case DT_MIPS_GP_VALUE:
3070 case DT_MIPS_AUX_DYNAMIC:
3071 case DT_MIPS_PLTGOT:
3072 case DT_MIPS_RLD_OBJ_UPDATE:
3074 printf(" 0x%jx\n", (uintmax_t) dyn->d_un.d_val);
3076 case DT_MIPS_IVERSION:
3077 case DT_MIPS_PERF_SUFFIX:
3080 name = dyn_str(re, stab, dyn->d_un.d_val);
3081 printf(" %s\n", name);
3083 case DT_MIPS_TIME_STAMP:
3084 printf(" %s\n", timestamp(dyn->d_un.d_val));
3095 dump_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab)
3099 if (dyn->d_tag >= DT_LOPROC && dyn->d_tag <= DT_HIPROC) {
3100 dump_arch_dyn_val(re, dyn, stab);
3104 /* These entry values are index into the string table. */
3106 if (dyn->d_tag == DT_NEEDED || dyn->d_tag == DT_SONAME ||
3107 dyn->d_tag == DT_RPATH || dyn->d_tag == DT_RUNPATH)
3108 name = dyn_str(re, stab, dyn->d_un.d_val);
3110 switch(dyn->d_tag) {
3128 case DT_GNU_LIBLIST:
3129 case DT_GNU_CONFLICT:
3130 printf(" 0x%jx\n", (uintmax_t) dyn->d_un.d_val);
3139 case DT_INIT_ARRAYSZ:
3140 case DT_FINI_ARRAYSZ:
3141 case DT_GNU_CONFLICTSZ:
3142 case DT_GNU_LIBLISTSZ:
3143 printf(" %ju (bytes)\n", (uintmax_t) dyn->d_un.d_val);
3149 printf(" %ju\n", (uintmax_t) dyn->d_un.d_val);
3152 printf(" Shared library: [%s]\n", name);
3155 printf(" Library soname: [%s]\n", name);
3158 printf(" Library rpath: [%s]\n", name);
3161 printf(" Library runpath: [%s]\n", name);
3164 printf(" %s\n", dt_type(re->ehdr.e_machine, dyn->d_un.d_val));
3166 case DT_GNU_PRELINKED:
3167 printf(" %s\n", timestamp(dyn->d_un.d_val));
3175 dump_rel(struct readelf *re, struct section *s, Elf_Data *d)
3178 const char *symname;
3182 #define REL_HDR "r_offset", "r_info", "r_type", "st_value", "st_name"
3183 #define REL_CT32 (uintmax_t)r.r_offset, (uintmax_t)r.r_info, \
3184 r_type(re->ehdr.e_machine, ELF32_R_TYPE(r.r_info)), \
3185 (uintmax_t)symval, symname
3186 #define REL_CT64 (uintmax_t)r.r_offset, (uintmax_t)r.r_info, \
3187 r_type(re->ehdr.e_machine, ELF64_R_TYPE(r.r_info)), \
3188 (uintmax_t)symval, symname
3190 printf("\nRelocation section (%s):\n", s->name);
3191 if (re->ec == ELFCLASS32)
3192 printf("%-8s %-8s %-19s %-8s %s\n", REL_HDR);
3194 if (re->options & RE_WW)
3195 printf("%-16s %-16s %-24s %-16s %s\n", REL_HDR);
3197 printf("%-12s %-12s %-19s %-16s %s\n", REL_HDR);
3199 assert(d->d_size == s->sz);
3200 if (!get_ent_count(s, &len))
3202 for (i = 0; i < len; i++) {
3203 if (gelf_getrel(d, i, &r) != &r) {
3204 warnx("gelf_getrel failed: %s", elf_errmsg(-1));
3207 if (s->link >= re->shnum) {
3208 warnx("invalid section link index %u", s->link);
3211 symname = get_symbol_name(re, s->link, GELF_R_SYM(r.r_info));
3212 symval = get_symbol_value(re, s->link, GELF_R_SYM(r.r_info));
3213 if (re->ec == ELFCLASS32) {
3214 r.r_info = ELF32_R_INFO(ELF64_R_SYM(r.r_info),
3215 ELF64_R_TYPE(r.r_info));
3216 printf("%8.8jx %8.8jx %-19.19s %8.8jx %s\n", REL_CT32);
3218 if (re->options & RE_WW)
3219 printf("%16.16jx %16.16jx %-24.24s"
3220 " %16.16jx %s\n", REL_CT64);
3222 printf("%12.12jx %12.12jx %-19.19s"
3223 " %16.16jx %s\n", REL_CT64);
3232 dump_rela(struct readelf *re, struct section *s, Elf_Data *d)
3235 const char *symname;
3239 #define RELA_HDR "r_offset", "r_info", "r_type", "st_value", \
3240 "st_name + r_addend"
3241 #define RELA_CT32 (uintmax_t)r.r_offset, (uintmax_t)r.r_info, \
3242 r_type(re->ehdr.e_machine, ELF32_R_TYPE(r.r_info)), \
3243 (uintmax_t)symval, symname
3244 #define RELA_CT64 (uintmax_t)r.r_offset, (uintmax_t)r.r_info, \
3245 r_type(re->ehdr.e_machine, ELF64_R_TYPE(r.r_info)), \
3246 (uintmax_t)symval, symname
3248 printf("\nRelocation section with addend (%s):\n", s->name);
3249 if (re->ec == ELFCLASS32)
3250 printf("%-8s %-8s %-19s %-8s %s\n", RELA_HDR);
3252 if (re->options & RE_WW)
3253 printf("%-16s %-16s %-24s %-16s %s\n", RELA_HDR);
3255 printf("%-12s %-12s %-19s %-16s %s\n", RELA_HDR);
3257 assert(d->d_size == s->sz);
3258 if (!get_ent_count(s, &len))
3260 for (i = 0; i < len; i++) {
3261 if (gelf_getrela(d, i, &r) != &r) {
3262 warnx("gelf_getrel failed: %s", elf_errmsg(-1));
3265 if (s->link >= re->shnum) {
3266 warnx("invalid section link index %u", s->link);
3269 symname = get_symbol_name(re, s->link, GELF_R_SYM(r.r_info));
3270 symval = get_symbol_value(re, s->link, GELF_R_SYM(r.r_info));
3271 if (re->ec == ELFCLASS32) {
3272 r.r_info = ELF32_R_INFO(ELF64_R_SYM(r.r_info),
3273 ELF64_R_TYPE(r.r_info));
3274 printf("%8.8jx %8.8jx %-19.19s %8.8jx %s", RELA_CT32);
3275 printf(" + %x\n", (uint32_t) r.r_addend);
3277 if (re->options & RE_WW)
3278 printf("%16.16jx %16.16jx %-24.24s"
3279 " %16.16jx %s", RELA_CT64);
3281 printf("%12.12jx %12.12jx %-19.19s"
3282 " %16.16jx %s", RELA_CT64);
3283 printf(" + %jx\n", (uintmax_t) r.r_addend);
3292 dump_reloc(struct readelf *re)
3298 for (i = 0; (size_t)i < re->shnum; i++) {
3300 if (s->type == SHT_REL || s->type == SHT_RELA) {
3302 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3303 elferr = elf_errno();
3305 warnx("elf_getdata failed: %s",
3306 elf_errmsg(elferr));
3309 if (s->type == SHT_REL)
3312 dump_rela(re, s, d);
3318 dump_symtab(struct readelf *re, int i)
3324 int elferr, stab, j, len;
3329 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3330 elferr = elf_errno();
3332 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
3337 if (!get_ent_count(s, &len))
3339 printf("Symbol table (%s)", s->name);
3340 printf(" contains %d entries:\n", len);
3341 printf("%7s%9s%14s%5s%8s%6s%9s%5s\n", "Num:", "Value", "Size", "Type",
3342 "Bind", "Vis", "Ndx", "Name");
3344 for (j = 0; j < len; j++) {
3345 if (gelf_getsym(d, j, &sym) != &sym) {
3346 warnx("gelf_getsym failed: %s", elf_errmsg(-1));
3350 printf(" %16.16jx", (uintmax_t)sym.st_value);
3351 printf(" %5ju", sym.st_size);
3352 printf(" %-7s", st_type(GELF_ST_TYPE(sym.st_info)));
3353 printf(" %-6s", st_bind(GELF_ST_BIND(sym.st_info)));
3354 printf(" %-8s", st_vis(GELF_ST_VISIBILITY(sym.st_other)));
3355 printf(" %3s", st_shndx(sym.st_shndx));
3356 if ((name = elf_strptr(re->elf, stab, sym.st_name)) != NULL)
3357 printf(" %s", name);
3358 /* Append symbol version string for SHT_DYNSYM symbol table. */
3359 if (s->type == SHT_DYNSYM && re->ver != NULL &&
3360 re->vs != NULL && re->vs[j] > 1) {
3361 if (re->vs[j] & 0x8000 ||
3362 re->ver[re->vs[j] & 0x7fff].type == 0)
3364 re->ver[re->vs[j] & 0x7fff].name,
3365 re->vs[j] & 0x7fff);
3367 printf("@@%s (%d)", re->ver[re->vs[j]].name,
3376 dump_symtabs(struct readelf *re)
3385 * If -D is specified, only dump the symbol table specified by
3386 * the DT_SYMTAB entry in the .dynamic section.
3389 if (re->options & RE_DD) {
3391 for (i = 0; (size_t)i < re->shnum; i++)
3392 if (re->sl[i].type == SHT_DYNAMIC) {
3399 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3400 elferr = elf_errno();
3402 warnx("elf_getdata failed: %s", elf_errmsg(-1));
3407 if (!get_ent_count(s, &len))
3410 for (i = 0; i < len; i++) {
3411 if (gelf_getdyn(d, i, &dyn) != &dyn) {
3412 warnx("gelf_getdyn failed: %s", elf_errmsg(-1));
3415 if (dyn.d_tag == DT_SYMTAB) {
3416 dyn_off = dyn.d_un.d_val;
3422 /* Find and dump symbol tables. */
3423 for (i = 0; (size_t)i < re->shnum; i++) {
3425 if (s->type == SHT_SYMTAB || s->type == SHT_DYNSYM) {
3426 if (re->options & RE_DD) {
3427 if (dyn_off == s->addr) {
3438 dump_svr4_hash(struct section *s)
3442 uint32_t nbucket, nchain;
3443 uint32_t *bucket, *chain;
3444 uint32_t *bl, *c, maxl, total;
3447 /* Read and parse the content of .hash section. */
3449 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3450 elferr = elf_errno();
3452 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
3455 if (d->d_size < 2 * sizeof(uint32_t)) {
3456 warnx(".hash section too small");
3462 if (nbucket <= 0 || nchain <= 0) {
3463 warnx("Malformed .hash section");
3466 if (d->d_size != (nbucket + nchain + 2) * sizeof(uint32_t)) {
3467 warnx("Malformed .hash section");
3471 chain = &buf[2 + nbucket];
3474 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
3475 errx(EXIT_FAILURE, "calloc failed");
3476 for (i = 0; (uint32_t)i < nbucket; i++)
3477 for (j = bucket[i]; j > 0 && (uint32_t)j < nchain; j = chain[j])
3480 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
3481 errx(EXIT_FAILURE, "calloc failed");
3482 for (i = 0; (uint32_t)i < nbucket; i++)
3484 printf("\nHistogram for bucket list length (total of %u buckets):\n",
3486 printf(" Length\tNumber\t\t%% of total\tCoverage\n");
3488 for (i = 0; (uint32_t)i <= maxl; i++) {
3490 printf("%7u\t%-10u\t(%5.1f%%)\t%5.1f%%\n", i, c[i],
3491 c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
3498 dump_svr4_hash64(struct readelf *re, struct section *s)
3502 uint64_t nbucket, nchain;
3503 uint64_t *bucket, *chain;
3504 uint64_t *bl, *c, maxl, total;
3508 * ALPHA uses 64-bit hash entries. Since libelf assumes that
3509 * .hash section contains only 32-bit entry, an explicit
3510 * gelf_xlatetom is needed here.
3513 if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
3514 elferr = elf_errno();
3516 warnx("elf_rawdata failed: %s",
3517 elf_errmsg(elferr));
3520 d->d_type = ELF_T_XWORD;
3521 memcpy(&dst, d, sizeof(Elf_Data));
3522 if (gelf_xlatetom(re->elf, &dst, d,
3523 re->ehdr.e_ident[EI_DATA]) != &dst) {
3524 warnx("gelf_xlatetom failed: %s", elf_errmsg(-1));
3527 if (dst.d_size < 2 * sizeof(uint64_t)) {
3528 warnx(".hash section too small");
3534 if (nbucket <= 0 || nchain <= 0) {
3535 warnx("Malformed .hash section");
3538 if (d->d_size != (nbucket + nchain + 2) * sizeof(uint32_t)) {
3539 warnx("Malformed .hash section");
3543 chain = &buf[2 + nbucket];
3546 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
3547 errx(EXIT_FAILURE, "calloc failed");
3548 for (i = 0; (uint32_t)i < nbucket; i++)
3549 for (j = bucket[i]; j > 0 && (uint32_t)j < nchain; j = chain[j])
3552 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
3553 errx(EXIT_FAILURE, "calloc failed");
3554 for (i = 0; (uint64_t)i < nbucket; i++)
3556 printf("Histogram for bucket list length (total of %ju buckets):\n",
3557 (uintmax_t)nbucket);
3558 printf(" Length\tNumber\t\t%% of total\tCoverage\n");
3560 for (i = 0; (uint64_t)i <= maxl; i++) {
3562 printf("%7u\t%-10ju\t(%5.1f%%)\t%5.1f%%\n", i, (uintmax_t)c[i],
3563 c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
3570 dump_gnu_hash(struct readelf *re, struct section *s)
3575 uint32_t *bucket, *chain;
3576 uint32_t nbucket, nchain, symndx, maskwords;
3577 uint32_t *bl, *c, maxl, total;
3578 int elferr, dynsymcount, i, j;
3581 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3582 elferr = elf_errno();
3584 warnx("elf_getdata failed: %s",
3585 elf_errmsg(elferr));
3588 if (d->d_size < 4 * sizeof(uint32_t)) {
3589 warnx(".gnu.hash section too small");
3597 ds = &re->sl[s->link];
3598 if (!get_ent_count(ds, &dynsymcount))
3600 nchain = dynsymcount - symndx;
3601 if (d->d_size != 4 * sizeof(uint32_t) + maskwords *
3602 (re->ec == ELFCLASS32 ? sizeof(uint32_t) : sizeof(uint64_t)) +
3603 (nbucket + nchain) * sizeof(uint32_t)) {
3604 warnx("Malformed .gnu.hash section");
3607 bucket = buf + (re->ec == ELFCLASS32 ? maskwords : maskwords * 2);
3608 chain = bucket + nbucket;
3611 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
3612 errx(EXIT_FAILURE, "calloc failed");
3613 for (i = 0; (uint32_t)i < nbucket; i++)
3614 for (j = bucket[i]; j > 0 && (uint32_t)j - symndx < nchain;
3618 if (chain[j - symndx] & 1)
3621 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
3622 errx(EXIT_FAILURE, "calloc failed");
3623 for (i = 0; (uint32_t)i < nbucket; i++)
3625 printf("Histogram for bucket list length (total of %u buckets):\n",
3627 printf(" Length\tNumber\t\t%% of total\tCoverage\n");
3629 for (i = 0; (uint32_t)i <= maxl; i++) {
3631 printf("%7u\t%-10u\t(%5.1f%%)\t%5.1f%%\n", i, c[i],
3632 c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
3639 dump_hash(struct readelf *re)
3644 for (i = 0; (size_t) i < re->shnum; i++) {
3646 if (s->type == SHT_HASH || s->type == SHT_GNU_HASH) {
3647 if (s->type == SHT_GNU_HASH)
3648 dump_gnu_hash(re, s);
3649 else if (re->ehdr.e_machine == EM_ALPHA &&
3651 dump_svr4_hash64(re, s);
3659 dump_notes(struct readelf *re)
3662 const char *rawfile;
3668 if (re->ehdr.e_type == ET_CORE) {
3670 * Search program headers in the core file for
3673 if (elf_getphnum(re->elf, &phnum) == 0) {
3674 warnx("elf_getphnum failed: %s", elf_errmsg(-1));
3679 if ((rawfile = elf_rawfile(re->elf, NULL)) == NULL) {
3680 warnx("elf_rawfile failed: %s", elf_errmsg(-1));
3683 for (i = 0; (size_t) i < phnum; i++) {
3684 if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
3685 warnx("gelf_getphdr failed: %s",
3689 if (phdr.p_type == PT_NOTE)
3690 dump_notes_content(re, rawfile + phdr.p_offset,
3691 phdr.p_filesz, phdr.p_offset);
3696 * For objects other than core files, Search for
3697 * SHT_NOTE sections.
3699 for (i = 0; (size_t) i < re->shnum; i++) {
3701 if (s->type == SHT_NOTE) {
3703 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3704 elferr = elf_errno();
3706 warnx("elf_getdata failed: %s",
3707 elf_errmsg(elferr));
3710 dump_notes_content(re, d->d_buf, d->d_size,
3718 dump_notes_content(struct readelf *re, const char *buf, size_t sz, off_t off)
3721 const char *end, *name;
3723 printf("\nNotes at offset %#010jx with length %#010jx:\n",
3724 (uintmax_t) off, (uintmax_t) sz);
3725 printf(" %-13s %-15s %s\n", "Owner", "Data size", "Description");
3728 if (buf + sizeof(*note) > end) {
3729 warnx("invalid note header");
3732 note = (Elf_Note *)(uintptr_t) buf;
3733 name = (char *)(uintptr_t)(note + 1);
3735 * The name field is required to be nul-terminated, and
3736 * n_namesz includes the terminating nul in observed
3737 * implementations (contrary to the ELF-64 spec). A special
3738 * case is needed for cores generated by some older Linux
3739 * versions, which write a note named "CORE" without a nul
3740 * terminator and n_namesz = 4.
3742 if (note->n_namesz == 0)
3744 else if (note->n_namesz == 4 && strncmp(name, "CORE", 4) == 0)
3746 else if (strnlen(name, note->n_namesz) >= note->n_namesz)
3748 printf(" %-13s %#010jx", name, (uintmax_t) note->n_descsz);
3749 printf(" %s\n", note_type(name, re->ehdr.e_type,
3751 buf += sizeof(Elf_Note) + roundup2(note->n_namesz, 4) +
3752 roundup2(note->n_descsz, 4);
3757 * Symbol versioning sections are the same for 32bit and 64bit
3760 #define Elf_Verdef Elf32_Verdef
3761 #define Elf_Verdaux Elf32_Verdaux
3762 #define Elf_Verneed Elf32_Verneed
3763 #define Elf_Vernaux Elf32_Vernaux
3765 #define SAVE_VERSION_NAME(x, n, t) \
3767 while (x >= re->ver_sz) { \
3768 nv = realloc(re->ver, \
3769 sizeof(*re->ver) * re->ver_sz * 2); \
3771 warn("realloc failed"); \
3776 for (i = re->ver_sz; i < re->ver_sz * 2; i++) { \
3777 re->ver[i].name = NULL; \
3778 re->ver[i].type = 0; \
3783 re->ver[x].name = n; \
3784 re->ver[x].type = t; \
3790 dump_verdef(struct readelf *re, int dump)
3797 uint8_t *buf, *end, *buf2;
3801 if ((s = re->vd_s) == NULL)
3804 if (re->ver == NULL) {
3806 if ((re->ver = calloc(re->ver_sz, sizeof(*re->ver))) ==
3808 warn("calloc failed");
3811 re->ver[0].name = "*local*";
3812 re->ver[1].name = "*global*";
3816 printf("\nVersion definition section (%s):\n", s->name);
3818 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3819 elferr = elf_errno();
3821 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
3828 end = buf + d->d_size;
3829 while (buf + sizeof(Elf_Verdef) <= end) {
3830 vd = (Elf_Verdef *) (uintptr_t) buf;
3832 printf(" 0x%4.4lx", (unsigned long)
3833 (buf - (uint8_t *)d->d_buf));
3834 printf(" vd_version: %u vd_flags: %d"
3835 " vd_ndx: %u vd_cnt: %u", vd->vd_version,
3836 vd->vd_flags, vd->vd_ndx, vd->vd_cnt);
3838 buf2 = buf + vd->vd_aux;
3840 while (buf2 + sizeof(Elf_Verdaux) <= end && j < vd->vd_cnt) {
3841 vda = (Elf_Verdaux *) (uintptr_t) buf2;
3842 name = get_string(re, s->link, vda->vda_name);
3845 printf(" vda_name: %s\n", name);
3846 SAVE_VERSION_NAME((int)vd->vd_ndx, name, 1);
3848 printf(" 0x%4.4lx parent: %s\n",
3849 (unsigned long) (buf2 -
3850 (uint8_t *)d->d_buf), name);
3851 if (vda->vda_next == 0)
3853 buf2 += vda->vda_next;
3856 if (vd->vd_next == 0)
3863 dump_verneed(struct readelf *re, int dump)
3870 uint8_t *buf, *end, *buf2;
3874 if ((s = re->vn_s) == NULL)
3877 if (re->ver == NULL) {
3879 if ((re->ver = calloc(re->ver_sz, sizeof(*re->ver))) ==
3881 warn("calloc failed");
3884 re->ver[0].name = "*local*";
3885 re->ver[1].name = "*global*";
3889 printf("\nVersion needed section (%s):\n", s->name);
3891 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3892 elferr = elf_errno();
3894 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
3901 end = buf + d->d_size;
3902 while (buf + sizeof(Elf_Verneed) <= end) {
3903 vn = (Elf_Verneed *) (uintptr_t) buf;
3905 printf(" 0x%4.4lx", (unsigned long)
3906 (buf - (uint8_t *)d->d_buf));
3907 printf(" vn_version: %u vn_file: %s vn_cnt: %u\n",
3909 get_string(re, s->link, vn->vn_file),
3912 buf2 = buf + vn->vn_aux;
3914 while (buf2 + sizeof(Elf_Vernaux) <= end && j < vn->vn_cnt) {
3915 vna = (Elf32_Vernaux *) (uintptr_t) buf2;
3917 printf(" 0x%4.4lx", (unsigned long)
3918 (buf2 - (uint8_t *)d->d_buf));
3919 name = get_string(re, s->link, vna->vna_name);
3921 printf(" vna_name: %s vna_flags: %u"
3922 " vna_other: %u\n", name,
3923 vna->vna_flags, vna->vna_other);
3924 SAVE_VERSION_NAME((int)vna->vna_other, name, 0);
3925 if (vna->vna_next == 0)
3927 buf2 += vna->vna_next;
3930 if (vn->vn_next == 0)
3937 dump_versym(struct readelf *re)
3941 if (re->vs_s == NULL || re->ver == NULL || re->vs == NULL)
3943 printf("\nVersion symbol section (%s):\n", re->vs_s->name);
3944 for (i = 0; i < re->vs_sz; i++) {
3948 printf(" %03x:", i);
3950 if (re->vs[i] & 0x8000)
3951 printf(" %3xh %-12s ", re->vs[i] & 0x7fff,
3952 re->ver[re->vs[i] & 0x7fff].name);
3954 printf(" %3x %-12s ", re->vs[i],
3955 re->ver[re->vs[i]].name);
3961 dump_ver(struct readelf *re)
3964 if (re->vs_s && re->ver && re->vs)
3969 dump_verneed(re, 1);
3973 search_ver(struct readelf *re)
3979 for (i = 0; (size_t) i < re->shnum; i++) {
3981 if (s->type == SHT_SUNW_versym)
3983 if (s->type == SHT_SUNW_verneed)
3985 if (s->type == SHT_SUNW_verdef)
3991 dump_verneed(re, 0);
3992 if (re->vs_s && re->ver != NULL) {
3994 if ((d = elf_getdata(re->vs_s->scn, NULL)) == NULL) {
3995 elferr = elf_errno();
3997 warnx("elf_getdata failed: %s",
3998 elf_errmsg(elferr));
4004 re->vs_sz = d->d_size / sizeof(Elf32_Half);
4012 #undef SAVE_VERSION_NAME
4015 * Elf32_Lib and Elf64_Lib are identical.
4017 #define Elf_Lib Elf32_Lib
4020 dump_liblist(struct readelf *re)
4028 int i, j, k, elferr, first, len;
4030 for (i = 0; (size_t) i < re->shnum; i++) {
4032 if (s->type != SHT_GNU_LIBLIST)
4035 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
4036 elferr = elf_errno();
4038 warnx("elf_getdata failed: %s",
4039 elf_errmsg(elferr));
4045 if (!get_ent_count(s, &len))
4047 printf("\nLibrary list section '%s' ", s->name);
4048 printf("contains %d entries:\n", len);
4049 printf("%12s%24s%18s%10s%6s\n", "Library", "Time Stamp",
4050 "Checksum", "Version", "Flags");
4051 for (j = 0; (uint64_t) j < s->sz / s->entsize; j++) {
4054 get_string(re, s->link, lib->l_name));
4055 ti = lib->l_time_stamp;
4057 snprintf(tbuf, sizeof(tbuf), "%04d-%02d-%02dT%02d:%02d"
4058 ":%2d", t->tm_year + 1900, t->tm_mon + 1,
4059 t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
4060 printf("%-19.19s ", tbuf);
4061 printf("0x%08x ", lib->l_checksum);
4062 printf("%-7d %#x", lib->l_version, lib->l_flags);
4063 if (lib->l_flags != 0) {
4066 for (k = 0; l_flag[k].name != NULL; k++) {
4067 if ((l_flag[k].value & lib->l_flags) ==
4074 printf("%s", l_flag[k].name);
4087 dump_section_groups(struct readelf *re)
4090 const char *symname;
4096 for (i = 0; (size_t) i < re->shnum; i++) {
4098 if (s->type != SHT_GROUP)
4101 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
4102 elferr = elf_errno();
4104 warnx("elf_getdata failed: %s",
4105 elf_errmsg(elferr));
4113 /* We only support COMDAT section. */
4115 #define GRP_COMDAT 0x1
4117 if ((*w++ & GRP_COMDAT) == 0)
4120 if (s->entsize == 0)
4123 symname = get_symbol_name(re, s->link, s->info);
4124 n = s->sz / s->entsize;
4128 printf("\nCOMDAT group section [%5d] `%s' [%s] contains %ju"
4129 " sections:\n", i, s->name, symname, (uintmax_t)n);
4130 printf(" %-10.10s %s\n", "[Index]", "Name");
4131 for (j = 0; (size_t) j < n; j++, w++) {
4132 if (*w >= re->shnum) {
4133 warnx("invalid section index: %u", *w);
4136 printf(" [%5u] %s\n", *w, re->sl[*w].name);
4142 dump_unknown_tag(uint64_t tag, uint8_t *p)
4147 * According to ARM EABI: For tags > 32, even numbered tags have
4148 * a ULEB128 param and odd numbered ones have NUL-terminated
4149 * string param. This rule probably also applies for tags <= 32
4150 * if the object arch is not ARM.
4153 printf(" Tag_unknown_%ju: ", (uintmax_t) tag);
4156 printf("%s\n", (char *) p);
4157 p += strlen((char *) p) + 1;
4159 val = _decode_uleb128(&p);
4160 printf("%ju\n", (uintmax_t) val);
4167 dump_compatibility_tag(uint8_t *p)
4171 val = _decode_uleb128(&p);
4172 printf("flag = %ju, vendor = %s\n", val, p);
4173 p += strlen((char *) p) + 1;
4179 dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe)
4188 tag = _decode_uleb128(&p);
4190 for (i = 0; i < sizeof(aeabi_tags) / sizeof(aeabi_tags[0]);
4192 if (tag == aeabi_tags[i].tag) {
4194 printf(" %s: ", aeabi_tags[i].s_tag);
4195 if (aeabi_tags[i].get_desc) {
4197 val = _decode_uleb128(&p);
4199 aeabi_tags[i].get_desc(val));
4203 if (tag < aeabi_tags[i].tag)
4207 p = dump_unknown_tag(tag, p);
4214 case 4: /* Tag_CPU_raw_name */
4215 case 5: /* Tag_CPU_name */
4216 case 67: /* Tag_conformance */
4217 printf("%s\n", (char *) p);
4218 p += strlen((char *) p) + 1;
4220 case 32: /* Tag_compatibility */
4221 p = dump_compatibility_tag(p);
4223 case 64: /* Tag_nodefaults */
4224 /* ignored, written as 0. */
4225 (void) _decode_uleb128(&p);
4228 case 65: /* Tag_also_compatible_with */
4229 val = _decode_uleb128(&p);
4230 /* Must be Tag_CPU_arch */
4232 printf("unknown\n");
4235 val = _decode_uleb128(&p);
4236 printf("%s\n", aeabi_cpu_arch(val));
4237 /* Skip NUL terminator. */
4247 #ifndef Tag_GNU_MIPS_ABI_FP
4248 #define Tag_GNU_MIPS_ABI_FP 4
4252 dump_mips_attributes(struct readelf *re, uint8_t *p, uint8_t *pe)
4259 tag = _decode_uleb128(&p);
4261 case Tag_GNU_MIPS_ABI_FP:
4262 val = _decode_uleb128(&p);
4263 printf(" Tag_GNU_MIPS_ABI_FP: %s\n", mips_abi_fp(val));
4265 case 32: /* Tag_compatibility */
4266 p = dump_compatibility_tag(p);
4269 p = dump_unknown_tag(tag, p);
4275 #ifndef Tag_GNU_Power_ABI_FP
4276 #define Tag_GNU_Power_ABI_FP 4
4279 #ifndef Tag_GNU_Power_ABI_Vector
4280 #define Tag_GNU_Power_ABI_Vector 8
4284 dump_ppc_attributes(uint8_t *p, uint8_t *pe)
4289 tag = _decode_uleb128(&p);
4291 case Tag_GNU_Power_ABI_FP:
4292 val = _decode_uleb128(&p);
4293 printf(" Tag_GNU_Power_ABI_FP: %s\n", ppc_abi_fp(val));
4295 case Tag_GNU_Power_ABI_Vector:
4296 val = _decode_uleb128(&p);
4297 printf(" Tag_GNU_Power_ABI_Vector: %s\n",
4298 ppc_abi_vector(val));
4300 case 32: /* Tag_compatibility */
4301 p = dump_compatibility_tag(p);
4304 p = dump_unknown_tag(tag, p);
4311 dump_attributes(struct readelf *re)
4316 size_t len, seclen, nlen, sublen;
4320 for (i = 0; (size_t) i < re->shnum; i++) {
4322 if (s->type != SHT_GNU_ATTRIBUTES &&
4323 (re->ehdr.e_machine != EM_ARM || s->type != SHT_LOPROC + 3))
4326 if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
4327 elferr = elf_errno();
4329 warnx("elf_rawdata failed: %s",
4330 elf_errmsg(elferr));
4337 printf("Unknown Attribute Section Format: %c\n",
4341 len = d->d_size - 1;
4345 warnx("truncated attribute section length");
4348 seclen = re->dw_decode(&p, 4);
4350 warnx("invalid attribute section length");
4354 nlen = strlen((char *) p) + 1;
4355 if (nlen + 4 > seclen) {
4356 warnx("invalid attribute section name");
4359 printf("Attribute Section: %s\n", (char *) p);
4362 while (seclen > 0) {
4365 sublen = re->dw_decode(&p, 4);
4366 if (sublen > seclen) {
4367 warnx("invalid attribute sub-section"
4372 printf("%s", top_tag(tag));
4373 if (tag == 2 || tag == 3) {
4376 val = _decode_uleb128(&p);
4379 printf(" %ju", (uintmax_t) val);
4383 if (re->ehdr.e_machine == EM_ARM &&
4384 s->type == SHT_LOPROC + 3)
4385 dump_arm_attributes(re, p, sp + sublen);
4386 else if (re->ehdr.e_machine == EM_MIPS ||
4387 re->ehdr.e_machine == EM_MIPS_RS3_LE)
4388 dump_mips_attributes(re, p,
4390 else if (re->ehdr.e_machine == EM_PPC)
4391 dump_ppc_attributes(p, sp + sublen);
4399 dump_mips_specific_info(struct readelf *re)
4402 int i, options_found;
4406 for (i = 0; (size_t) i < re->shnum; i++) {
4408 if (s->name != NULL && (!strcmp(s->name, ".MIPS.options") ||
4409 (s->type == SHT_MIPS_OPTIONS))) {
4410 dump_mips_options(re, s);
4416 * According to SGI mips64 spec, .reginfo should be ignored if
4417 * .MIPS.options section is present.
4419 if (!options_found) {
4420 for (i = 0; (size_t) i < re->shnum; i++) {
4422 if (s->name != NULL && (!strcmp(s->name, ".reginfo") ||
4423 (s->type == SHT_MIPS_REGINFO)))
4424 dump_mips_reginfo(re, s);
4430 dump_mips_reginfo(struct readelf *re, struct section *s)
4436 if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
4437 elferr = elf_errno();
4439 warnx("elf_rawdata failed: %s",
4440 elf_errmsg(elferr));
4445 if (!get_ent_count(s, &len))
4448 printf("\nSection '%s' contains %d entries:\n", s->name, len);
4449 dump_mips_odk_reginfo(re, d->d_buf, d->d_size);
4453 dump_mips_options(struct readelf *re, struct section *s)
4463 if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
4464 elferr = elf_errno();
4466 warnx("elf_rawdata failed: %s",
4467 elf_errmsg(elferr));
4473 printf("\nSection %s contains:\n", s->name);
4478 warnx("Truncated MIPS option header");
4481 kind = re->dw_decode(&p, 1);
4482 size = re->dw_decode(&p, 1);
4483 sndx = re->dw_decode(&p, 2);
4484 info = re->dw_decode(&p, 4);
4485 if (size < 8 || size - 8 > pe - p) {
4486 warnx("Malformed MIPS option header");
4492 dump_mips_odk_reginfo(re, p, size);
4494 case ODK_EXCEPTIONS:
4495 printf(" EXCEPTIONS FPU_MIN: %#x\n",
4496 info & OEX_FPU_MIN);
4497 printf("%11.11s FPU_MAX: %#x\n", "",
4498 info & OEX_FPU_MAX);
4499 dump_mips_option_flags("", mips_exceptions_option,
4503 printf(" %-10.10s section: %ju\n", "OPAD",
4505 dump_mips_option_flags("", mips_pad_option, info);
4508 dump_mips_option_flags("HWPATCH", mips_hwpatch_option,
4512 dump_mips_option_flags("HWAND", mips_hwa_option, info);
4515 dump_mips_option_flags("HWOR", mips_hwo_option, info);
4518 printf(" %-10.10s %#jx\n", "FILL", (uintmax_t) info);
4521 printf(" %-10.10s\n", "TAGS");
4524 printf(" %-10.10s GP group number: %#x\n", "GP_GROUP",
4527 printf(" %-10.10s GP group is "
4528 "self-contained\n", "");
4531 printf(" %-10.10s default GP group number: %#x\n",
4532 "IDENT", info & 0xFFFF);
4534 printf(" %-10.10s default GP group is "
4535 "self-contained\n", "");
4538 printf(" %-10.10s\n", "PAGESIZE");
4548 dump_mips_option_flags(const char *name, struct mips_option *opt, uint64_t info)
4553 for (; opt->desc != NULL; opt++) {
4554 if (info & opt->flag) {
4555 printf(" %-10.10s %s\n", first ? name : "",
4563 dump_mips_odk_reginfo(struct readelf *re, uint8_t *p, size_t sz)
4565 uint32_t ri_gprmask;
4566 uint32_t ri_cprmask[4];
4567 uint64_t ri_gp_value;
4573 ri_gprmask = re->dw_decode(&p, 4);
4574 /* Skip ri_pad padding field for mips64. */
4575 if (re->ec == ELFCLASS64)
4576 re->dw_decode(&p, 4);
4577 for (i = 0; i < 4; i++)
4578 ri_cprmask[i] = re->dw_decode(&p, 4);
4579 if (re->ec == ELFCLASS32)
4580 ri_gp_value = re->dw_decode(&p, 4);
4582 ri_gp_value = re->dw_decode(&p, 8);
4583 printf(" %s ", option_kind(ODK_REGINFO));
4584 printf("ri_gprmask: 0x%08jx\n", (uintmax_t) ri_gprmask);
4585 for (i = 0; i < 4; i++)
4586 printf("%11.11s ri_cprmask[%d]: 0x%08jx\n", "", i,
4587 (uintmax_t) ri_cprmask[i]);
4588 printf("%12.12s", "");
4589 printf("ri_gp_value: %#jx\n", (uintmax_t) ri_gp_value);
4594 dump_arch_specific_info(struct readelf *re)
4598 dump_attributes(re);
4600 switch (re->ehdr.e_machine) {
4602 case EM_MIPS_RS3_LE:
4603 dump_mips_specific_info(re);
4610 dwarf_regname(struct readelf *re, unsigned int num)
4615 if ((rn = dwarf_reg(re->ehdr.e_machine, num)) != NULL)
4618 snprintf(rx, sizeof(rx), "r%u", num);
4624 dump_dwarf_line(struct readelf *re)
4629 Dwarf_Half tag, version, pointer_size;
4630 Dwarf_Unsigned offset, endoff, length, hdrlen, dirndx, mtime, fsize;
4631 Dwarf_Small minlen, defstmt, lrange, opbase, oplen;
4634 uint64_t address, file, line, column, isa, opsize, udelta;
4638 int i, is_stmt, dwarf_size, elferr, ret;
4640 printf("\nDump of debug contents of section .debug_line:\n");
4643 for (i = 0; (size_t) i < re->shnum; i++) {
4645 if (s->name != NULL && !strcmp(s->name, ".debug_line"))
4648 if ((size_t) i >= re->shnum)
4652 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
4653 elferr = elf_errno();
4655 warnx("elf_getdata failed: %s", elf_errmsg(-1));
4661 while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
4662 NULL, &de)) == DW_DLV_OK) {
4664 while (dwarf_siblingof(re->dbg, die, &die, &de) == DW_DLV_OK) {
4665 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
4666 warnx("dwarf_tag failed: %s",
4670 /* XXX: What about DW_TAG_partial_unit? */
4671 if (tag == DW_TAG_compile_unit)
4675 warnx("could not find DW_TAG_compile_unit die");
4678 if (dwarf_attrval_unsigned(die, DW_AT_stmt_list, &offset,
4682 length = re->dw_read(d, &offset, 4);
4683 if (length == 0xffffffff) {
4685 length = re->dw_read(d, &offset, 8);
4689 if (length > d->d_size - offset) {
4690 warnx("invalid .dwarf_line section");
4694 endoff = offset + length;
4695 version = re->dw_read(d, &offset, 2);
4696 hdrlen = re->dw_read(d, &offset, dwarf_size);
4697 minlen = re->dw_read(d, &offset, 1);
4698 defstmt = re->dw_read(d, &offset, 1);
4699 lbase = re->dw_read(d, &offset, 1);
4700 lrange = re->dw_read(d, &offset, 1);
4701 opbase = re->dw_read(d, &offset, 1);
4704 printf(" Length:\t\t\t%ju\n", (uintmax_t) length);
4705 printf(" DWARF version:\t\t%u\n", version);
4706 printf(" Prologue Length:\t\t%ju\n", (uintmax_t) hdrlen);
4707 printf(" Minimum Instruction Length:\t%u\n", minlen);
4708 printf(" Initial value of 'is_stmt':\t%u\n", defstmt);
4709 printf(" Line Base:\t\t\t%d\n", lbase);
4710 printf(" Line Range:\t\t\t%u\n", lrange);
4711 printf(" Opcode Base:\t\t\t%u\n", opbase);
4712 (void) dwarf_get_address_size(re->dbg, &pointer_size, &de);
4713 printf(" (Pointer size:\t\t%u)\n", pointer_size);
4716 printf(" Opcodes:\n");
4717 for (i = 1; i < opbase; i++) {
4718 oplen = re->dw_read(d, &offset, 1);
4719 printf(" Opcode %d has %u args\n", i, oplen);
4723 printf(" The Directory Table:\n");
4724 p = (uint8_t *) d->d_buf + offset;
4725 while (*p != '\0') {
4726 printf(" %s\n", (char *) p);
4727 p += strlen((char *) p) + 1;
4732 printf(" The File Name Table:\n");
4733 printf(" Entry\tDir\tTime\tSize\tName\n");
4735 while (*p != '\0') {
4738 p += strlen(pn) + 1;
4739 dirndx = _decode_uleb128(&p);
4740 mtime = _decode_uleb128(&p);
4741 fsize = _decode_uleb128(&p);
4742 printf(" %d\t%ju\t%ju\t%ju\t%s\n", i,
4743 (uintmax_t) dirndx, (uintmax_t) mtime,
4744 (uintmax_t) fsize, pn);
4747 #define RESET_REGISTERS \
4753 is_stmt = defstmt; \
4756 #define LINE(x) (lbase + (((x) - opbase) % lrange))
4757 #define ADDRESS(x) ((((x) - opbase) / lrange) * minlen)
4760 pe = (uint8_t *) d->d_buf + endoff;
4762 printf(" Line Number Statements:\n");
4773 opsize = _decode_uleb128(&p);
4774 printf(" Extended opcode %u: ", *p);
4776 case DW_LNE_end_sequence:
4779 printf("End of Sequence\n");
4781 case DW_LNE_set_address:
4783 address = re->dw_decode(&p,
4785 printf("set Address to %#jx\n",
4786 (uintmax_t) address);
4788 case DW_LNE_define_file:
4791 p += strlen(pn) + 1;
4792 dirndx = _decode_uleb128(&p);
4793 mtime = _decode_uleb128(&p);
4794 fsize = _decode_uleb128(&p);
4795 printf("define new file: %s\n", pn);
4798 /* Unrecognized extened opcodes. */
4800 printf("unknown opcode\n");
4802 } else if (*p > 0 && *p < opbase) {
4810 case DW_LNS_advance_pc:
4811 udelta = _decode_uleb128(&p) *
4814 printf(" Advance PC by %ju to %#jx\n",
4816 (uintmax_t) address);
4818 case DW_LNS_advance_line:
4819 sdelta = _decode_sleb128(&p);
4821 printf(" Advance Line by %jd to %ju\n",
4825 case DW_LNS_set_file:
4826 file = _decode_uleb128(&p);
4827 printf(" Set File to %ju\n",
4830 case DW_LNS_set_column:
4831 column = _decode_uleb128(&p);
4832 printf(" Set Column to %ju\n",
4833 (uintmax_t) column);
4835 case DW_LNS_negate_stmt:
4837 printf(" Set is_stmt to %d\n", is_stmt);
4839 case DW_LNS_set_basic_block:
4840 printf(" Set basic block flag\n");
4842 case DW_LNS_const_add_pc:
4843 address += ADDRESS(255);
4844 printf(" Advance PC by constant %ju"
4846 (uintmax_t) ADDRESS(255),
4847 (uintmax_t) address);
4849 case DW_LNS_fixed_advance_pc:
4850 udelta = re->dw_decode(&p, 2);
4852 printf(" Advance PC by fixed value "
4855 (uintmax_t) address);
4857 case DW_LNS_set_prologue_end:
4858 printf(" Set prologue end flag\n");
4860 case DW_LNS_set_epilogue_begin:
4861 printf(" Set epilogue begin flag\n");
4863 case DW_LNS_set_isa:
4864 isa = _decode_uleb128(&p);
4865 printf(" Set isa to %ju\n", isa);
4868 /* Unrecognized extended opcodes. */
4869 printf(" Unknown extended opcode %u\n",
4879 address += ADDRESS(*p);
4880 printf(" Special opcode %u: advance Address "
4881 "by %ju to %#jx and Line by %jd to %ju\n",
4882 *p - opbase, (uintmax_t) ADDRESS(*p),
4883 (uintmax_t) address, (intmax_t) LINE(*p),
4891 if (ret == DW_DLV_ERROR)
4892 warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
4894 #undef RESET_REGISTERS
4900 dump_dwarf_line_decoded(struct readelf *re)
4903 Dwarf_Line *linebuf, ln;
4904 Dwarf_Addr lineaddr;
4905 Dwarf_Signed linecount, srccount;
4906 Dwarf_Unsigned lineno, fn;
4908 const char *dir, *file;
4912 printf("Decoded dump of debug contents of section .debug_line:\n\n");
4913 while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
4914 NULL, &de)) == DW_DLV_OK) {
4915 if (dwarf_siblingof(re->dbg, NULL, &die, &de) != DW_DLV_OK)
4917 if (dwarf_attrval_string(die, DW_AT_name, &file, &de) !=
4920 if (dwarf_attrval_string(die, DW_AT_comp_dir, &dir, &de) !=
4929 printf("%-37s %11s %s\n", "Filename", "Line Number",
4930 "Starting Address");
4931 if (dwarf_srclines(die, &linebuf, &linecount, &de) != DW_DLV_OK)
4933 if (dwarf_srcfiles(die, &srcfiles, &srccount, &de) != DW_DLV_OK)
4935 for (i = 0; i < linecount; i++) {
4937 if (dwarf_line_srcfileno(ln, &fn, &de) != DW_DLV_OK)
4939 if (dwarf_lineno(ln, &lineno, &de) != DW_DLV_OK)
4941 if (dwarf_lineaddr(ln, &lineaddr, &de) != DW_DLV_OK)
4943 printf("%-37s %11ju %#18jx\n",
4944 basename(srcfiles[fn - 1]), (uintmax_t) lineno,
4945 (uintmax_t) lineaddr);
4952 dump_dwarf_die(struct readelf *re, Dwarf_Die die, int level)
4954 Dwarf_Attribute *attr_list;
4956 Dwarf_Off dieoff, cuoff, culen, attroff;
4957 Dwarf_Unsigned ate, lang, v_udata, v_sig;
4958 Dwarf_Signed attr_count, v_sdata;
4961 Dwarf_Half tag, attr, form;
4962 Dwarf_Block *v_block;
4963 Dwarf_Bool v_bool, is_info;
4967 const char *tag_str, *attr_str, *ate_str, *lang_str;
4968 char unk_tag[32], unk_attr[32];
4973 if (dwarf_dieoffset(die, &dieoff, &de) != DW_DLV_OK) {
4974 warnx("dwarf_dieoffset failed: %s", dwarf_errmsg(de));
4978 printf(" <%d><%jx>: ", level, (uintmax_t) dieoff);
4980 if (dwarf_die_CU_offset_range(die, &cuoff, &culen, &de) != DW_DLV_OK) {
4981 warnx("dwarf_die_CU_offset_range failed: %s",
4986 abc = dwarf_die_abbrev_code(die);
4987 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
4988 warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
4991 if (dwarf_get_TAG_name(tag, &tag_str) != DW_DLV_OK) {
4992 snprintf(unk_tag, sizeof(unk_tag), "[Unknown Tag: %#x]", tag);
4996 printf("Abbrev Number: %d (%s)\n", abc, tag_str);
4998 if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
5000 if (ret == DW_DLV_ERROR)
5001 warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
5005 for (i = 0; i < attr_count; i++) {
5006 if (dwarf_whatform(attr_list[i], &form, &de) != DW_DLV_OK) {
5007 warnx("dwarf_whatform failed: %s", dwarf_errmsg(de));
5010 if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
5011 warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
5014 if (dwarf_get_AT_name(attr, &attr_str) != DW_DLV_OK) {
5015 snprintf(unk_attr, sizeof(unk_attr),
5016 "[Unknown AT: %#x]", attr);
5017 attr_str = unk_attr;
5019 if (dwarf_attroffset(attr_list[i], &attroff, &de) !=
5021 warnx("dwarf_attroffset failed: %s", dwarf_errmsg(de));
5024 printf(" <%jx> %-18s: ", (uintmax_t) attroff, attr_str);
5026 case DW_FORM_ref_addr:
5027 case DW_FORM_sec_offset:
5028 if (dwarf_global_formref(attr_list[i], &v_off, &de) !=
5030 warnx("dwarf_global_formref failed: %s",
5034 if (form == DW_FORM_ref_addr)
5035 printf("<0x%jx>", (uintmax_t) v_off);
5037 printf("0x%jx", (uintmax_t) v_off);
5044 case DW_FORM_ref_udata:
5045 if (dwarf_formref(attr_list[i], &v_off, &de) !=
5047 warnx("dwarf_formref failed: %s",
5052 printf("<0x%jx>", (uintmax_t) v_off);
5056 if (dwarf_formaddr(attr_list[i], &v_addr, &de) !=
5058 warnx("dwarf_formaddr failed: %s",
5062 printf("%#jx", (uintmax_t) v_addr);
5070 if (dwarf_formudata(attr_list[i], &v_udata, &de) !=
5072 warnx("dwarf_formudata failed: %s",
5076 if (attr == DW_AT_high_pc)
5077 printf("0x%jx", (uintmax_t) v_udata);
5079 printf("%ju", (uintmax_t) v_udata);
5083 if (dwarf_formsdata(attr_list[i], &v_sdata, &de) !=
5085 warnx("dwarf_formudata failed: %s",
5089 printf("%jd", (intmax_t) v_sdata);
5093 if (dwarf_formflag(attr_list[i], &v_bool, &de) !=
5095 warnx("dwarf_formflag failed: %s",
5099 printf("%jd", (intmax_t) v_bool);
5102 case DW_FORM_flag_present:
5106 case DW_FORM_string:
5108 if (dwarf_formstring(attr_list[i], &v_str, &de) !=
5110 warnx("dwarf_formstring failed: %s",
5114 if (form == DW_FORM_string)
5115 printf("%s", v_str);
5117 printf("(indirect string) %s", v_str);
5121 case DW_FORM_block1:
5122 case DW_FORM_block2:
5123 case DW_FORM_block4:
5124 if (dwarf_formblock(attr_list[i], &v_block, &de) !=
5126 warnx("dwarf_formblock failed: %s",
5130 printf("%ju byte block:", (uintmax_t) v_block->bl_len);
5131 b = v_block->bl_data;
5132 for (j = 0; (Dwarf_Unsigned) j < v_block->bl_len; j++)
5133 printf(" %x", b[j]);
5135 dump_dwarf_block(re, v_block->bl_data, v_block->bl_len);
5139 case DW_FORM_exprloc:
5140 if (dwarf_formexprloc(attr_list[i], &v_udata, &v_expr,
5141 &de) != DW_DLV_OK) {
5142 warnx("dwarf_formexprloc failed: %s",
5146 printf("%ju byte block:", (uintmax_t) v_udata);
5148 for (j = 0; (Dwarf_Unsigned) j < v_udata; j++)
5149 printf(" %x", b[j]);
5151 dump_dwarf_block(re, v_expr, v_udata);
5155 case DW_FORM_ref_sig8:
5156 if (dwarf_formsig8(attr_list[i], &v_sig8, &de) !=
5158 warnx("dwarf_formsig8 failed: %s",
5162 p = (uint8_t *)(uintptr_t) &v_sig8.signature[0];
5163 v_sig = re->dw_decode(&p, 8);
5164 printf("signature: 0x%jx", (uintmax_t) v_sig);
5167 case DW_AT_encoding:
5168 if (dwarf_attrval_unsigned(die, attr, &ate, &de) !=
5171 if (dwarf_get_ATE_name(ate, &ate_str) != DW_DLV_OK)
5172 ate_str = "DW_ATE_UNKNOWN";
5173 printf("\t(%s)", &ate_str[strlen("DW_ATE_")]);
5176 case DW_AT_language:
5177 if (dwarf_attrval_unsigned(die, attr, &lang, &de) !=
5180 if (dwarf_get_LANG_name(lang, &lang_str) != DW_DLV_OK)
5182 printf("\t(%s)", &lang_str[strlen("DW_LANG_")]);
5185 case DW_AT_location:
5186 case DW_AT_string_length:
5187 case DW_AT_return_addr:
5188 case DW_AT_data_member_location:
5189 case DW_AT_frame_base:
5191 case DW_AT_static_link:
5192 case DW_AT_use_location:
5193 case DW_AT_vtable_elem_location:
5197 case DW_FORM_sec_offset:
5198 printf("\t(location list)");
5212 /* Search children. */
5213 ret = dwarf_child(die, &ret_die, &de);
5214 if (ret == DW_DLV_ERROR)
5215 warnx("dwarf_child: %s", dwarf_errmsg(de));
5216 else if (ret == DW_DLV_OK)
5217 dump_dwarf_die(re, ret_die, level + 1);
5219 /* Search sibling. */
5220 is_info = dwarf_get_die_infotypes_flag(die);
5221 ret = dwarf_siblingof_b(re->dbg, die, &ret_die, is_info, &de);
5222 if (ret == DW_DLV_ERROR)
5223 warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
5224 else if (ret == DW_DLV_OK)
5225 dump_dwarf_die(re, ret_die, level);
5227 dwarf_dealloc(re->dbg, die, DW_DLA_DIE);
5231 set_cu_context(struct readelf *re, Dwarf_Half psize, Dwarf_Half osize,
5235 re->cu_psize = psize;
5236 re->cu_osize = osize;
5241 dump_dwarf_info(struct readelf *re, Dwarf_Bool is_info)
5246 Dwarf_Half tag, version, pointer_size, off_size;
5247 Dwarf_Off cu_offset, cu_length;
5249 Dwarf_Unsigned typeoff;
5256 sn = is_info ? ".debug_info" : ".debug_types";
5259 for (i = 0; (size_t) i < re->shnum; i++) {
5261 if (s->name != NULL && !strcmp(s->name, sn))
5264 if ((size_t) i >= re->shnum)
5268 printf("\nDump of debug contents of section %s:\n", sn);
5270 while ((ret = dwarf_next_cu_header_c(re->dbg, is_info, NULL,
5271 &version, &aboff, &pointer_size, &off_size, NULL, &sig8,
5272 &typeoff, NULL, &de)) == DW_DLV_OK) {
5273 set_cu_context(re, pointer_size, off_size, version);
5275 while (dwarf_siblingof_b(re->dbg, die, &die, is_info,
5276 &de) == DW_DLV_OK) {
5277 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
5278 warnx("dwarf_tag failed: %s",
5282 /* XXX: What about DW_TAG_partial_unit? */
5283 if ((is_info && tag == DW_TAG_compile_unit) ||
5284 (!is_info && tag == DW_TAG_type_unit))
5287 if (die == NULL && is_info) {
5288 warnx("could not find DW_TAG_compile_unit "
5291 } else if (die == NULL && !is_info) {
5292 warnx("could not find DW_TAG_type_unit die");
5296 if (dwarf_die_CU_offset_range(die, &cu_offset,
5297 &cu_length, &de) != DW_DLV_OK) {
5298 warnx("dwarf_die_CU_offset failed: %s",
5303 cu_length -= off_size == 4 ? 4 : 12;
5307 p = (uint8_t *)(uintptr_t) &sig8.signature[0];
5308 sig = re->dw_decode(&p, 8);
5311 printf("\n Type Unit @ offset 0x%jx:\n",
5312 (uintmax_t) cu_offset);
5313 printf(" Length:\t\t%#jx (%d-bit)\n",
5314 (uintmax_t) cu_length, off_size == 4 ? 32 : 64);
5315 printf(" Version:\t\t%u\n", version);
5316 printf(" Abbrev Offset:\t0x%jx\n",
5318 printf(" Pointer Size:\t%u\n", pointer_size);
5320 printf(" Signature:\t\t0x%016jx\n",
5322 printf(" Type Offset:\t0x%jx\n",
5323 (uintmax_t) typeoff);
5326 dump_dwarf_die(re, die, 0);
5328 if (ret == DW_DLV_ERROR)
5329 warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
5332 } while (dwarf_next_types_section(re->dbg, &de) == DW_DLV_OK);
5336 dump_dwarf_abbrev(struct readelf *re)
5339 Dwarf_Off aboff, atoff;
5340 Dwarf_Unsigned length, attr_count;
5341 Dwarf_Signed flag, form;
5342 Dwarf_Half tag, attr;
5344 const char *tag_str, *attr_str, *form_str;
5345 char unk_tag[32], unk_attr[32], unk_form[32];
5348 printf("\nContents of section .debug_abbrev:\n\n");
5350 while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, &aboff,
5351 NULL, NULL, &de)) == DW_DLV_OK) {
5352 printf(" Number TAG\n");
5354 while ((ret = dwarf_get_abbrev(re->dbg, aboff, &ab, &length,
5355 &attr_count, &de)) == DW_DLV_OK) {
5357 dwarf_dealloc(re->dbg, ab, DW_DLA_ABBREV);
5362 if (dwarf_get_abbrev_tag(ab, &tag, &de) != DW_DLV_OK) {
5363 warnx("dwarf_get_abbrev_tag failed: %s",
5367 if (dwarf_get_TAG_name(tag, &tag_str) != DW_DLV_OK) {
5368 snprintf(unk_tag, sizeof(unk_tag),
5369 "[Unknown Tag: %#x]", tag);
5372 if (dwarf_get_abbrev_children_flag(ab, &flag, &de) !=
5374 warnx("dwarf_get_abbrev_children_flag failed:"
5375 " %s", dwarf_errmsg(de));
5378 printf(" %s %s\n", tag_str,
5379 flag ? "[has children]" : "[no children]");
5380 for (j = 0; (Dwarf_Unsigned) j < attr_count; j++) {
5381 if (dwarf_get_abbrev_entry(ab, (Dwarf_Signed) j,
5382 &attr, &form, &atoff, &de) != DW_DLV_OK) {
5383 warnx("dwarf_get_abbrev_entry failed:"
5384 " %s", dwarf_errmsg(de));
5387 if (dwarf_get_AT_name(attr, &attr_str) !=
5389 snprintf(unk_attr, sizeof(unk_attr),
5390 "[Unknown AT: %#x]", attr);
5391 attr_str = unk_attr;
5393 if (dwarf_get_FORM_name(form, &form_str) !=
5395 snprintf(unk_form, sizeof(unk_form),
5396 "[Unknown Form: %#x]",
5398 form_str = unk_form;
5400 printf(" %-18s %s\n", attr_str, form_str);
5403 dwarf_dealloc(re->dbg, ab, DW_DLA_ABBREV);
5405 if (ret != DW_DLV_OK)
5406 warnx("dwarf_get_abbrev: %s", dwarf_errmsg(de));
5408 if (ret == DW_DLV_ERROR)
5409 warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
5413 dump_dwarf_pubnames(struct readelf *re)
5417 Dwarf_Unsigned offset, length, nt_cu_offset, nt_cu_length;
5419 Dwarf_Global *globs;
5420 Dwarf_Half nt_version;
5424 int i, dwarf_size, elferr;
5426 printf("\nContents of the .debug_pubnames section:\n");
5429 for (i = 0; (size_t) i < re->shnum; i++) {
5431 if (s->name != NULL && !strcmp(s->name, ".debug_pubnames"))
5434 if ((size_t) i >= re->shnum)
5438 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
5439 elferr = elf_errno();
5441 warnx("elf_getdata failed: %s", elf_errmsg(-1));
5447 /* Read in .debug_pubnames section table header. */
5449 length = re->dw_read(d, &offset, 4);
5450 if (length == 0xffffffff) {
5452 length = re->dw_read(d, &offset, 8);
5456 if (length > d->d_size - offset) {
5457 warnx("invalid .dwarf_pubnames section");
5461 nt_version = re->dw_read(d, &offset, 2);
5462 nt_cu_offset = re->dw_read(d, &offset, dwarf_size);
5463 nt_cu_length = re->dw_read(d, &offset, dwarf_size);
5464 printf(" Length:\t\t\t\t%ju\n", (uintmax_t) length);
5465 printf(" Version:\t\t\t\t%u\n", nt_version);
5466 printf(" Offset into .debug_info section:\t%ju\n",
5467 (uintmax_t) nt_cu_offset);
5468 printf(" Size of area in .debug_info section:\t%ju\n",
5469 (uintmax_t) nt_cu_length);
5471 if (dwarf_get_globals(re->dbg, &globs, &cnt, &de) != DW_DLV_OK) {
5472 warnx("dwarf_get_globals failed: %s", dwarf_errmsg(de));
5476 printf("\n Offset Name\n");
5477 for (i = 0; i < cnt; i++) {
5478 if (dwarf_globname(globs[i], &glob_name, &de) != DW_DLV_OK) {
5479 warnx("dwarf_globname failed: %s", dwarf_errmsg(de));
5482 if (dwarf_global_die_offset(globs[i], &die_off, &de) !=
5484 warnx("dwarf_global_die_offset failed: %s",
5488 printf(" %-11ju %s\n", (uintmax_t) die_off, glob_name);
5493 dump_dwarf_aranges(struct readelf *re)
5496 Dwarf_Arange *aranges;
5498 Dwarf_Unsigned offset, length, as_cu_offset;
5501 Dwarf_Half as_version, as_addrsz, as_segsz;
5504 int i, dwarf_size, elferr;
5506 printf("\nContents of section .debug_aranges:\n");
5509 for (i = 0; (size_t) i < re->shnum; i++) {
5511 if (s->name != NULL && !strcmp(s->name, ".debug_aranges"))
5514 if ((size_t) i >= re->shnum)
5518 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
5519 elferr = elf_errno();
5521 warnx("elf_getdata failed: %s", elf_errmsg(-1));
5527 /* Read in the .debug_aranges section table header. */
5529 length = re->dw_read(d, &offset, 4);
5530 if (length == 0xffffffff) {
5532 length = re->dw_read(d, &offset, 8);
5536 if (length > d->d_size - offset) {
5537 warnx("invalid .dwarf_aranges section");
5541 as_version = re->dw_read(d, &offset, 2);
5542 as_cu_offset = re->dw_read(d, &offset, dwarf_size);
5543 as_addrsz = re->dw_read(d, &offset, 1);
5544 as_segsz = re->dw_read(d, &offset, 1);
5546 printf(" Length:\t\t\t%ju\n", (uintmax_t) length);
5547 printf(" Version:\t\t\t%u\n", as_version);
5548 printf(" Offset into .debug_info:\t%ju\n", (uintmax_t) as_cu_offset);
5549 printf(" Pointer Size:\t\t\t%u\n", as_addrsz);
5550 printf(" Segment Size:\t\t\t%u\n", as_segsz);
5552 if (dwarf_get_aranges(re->dbg, &aranges, &cnt, &de) != DW_DLV_OK) {
5553 warnx("dwarf_get_aranges failed: %s", dwarf_errmsg(de));
5557 printf("\n Address Length\n");
5558 for (i = 0; i < cnt; i++) {
5559 if (dwarf_get_arange_info(aranges[i], &start, &length,
5560 &die_off, &de) != DW_DLV_OK) {
5561 warnx("dwarf_get_arange_info failed: %s",
5565 printf(" %08jx %ju\n", (uintmax_t) start,
5566 (uintmax_t) length);
5571 dump_dwarf_ranges_foreach(struct readelf *re, Dwarf_Die die, Dwarf_Addr base)
5573 Dwarf_Attribute *attr_list;
5574 Dwarf_Ranges *ranges;
5579 Dwarf_Signed attr_count, cnt;
5580 Dwarf_Unsigned off, bytecnt;
5583 if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
5585 if (ret == DW_DLV_ERROR)
5586 warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
5590 for (i = 0; i < attr_count; i++) {
5591 if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
5592 warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
5595 if (attr != DW_AT_ranges)
5597 if (dwarf_formudata(attr_list[i], &off, &de) != DW_DLV_OK) {
5598 warnx("dwarf_formudata failed: %s", dwarf_errmsg(de));
5601 if (dwarf_get_ranges(re->dbg, (Dwarf_Off) off, &ranges, &cnt,
5602 &bytecnt, &de) != DW_DLV_OK)
5605 for (j = 0; j < cnt; j++) {
5606 printf(" %08jx ", (uintmax_t) off);
5607 if (ranges[j].dwr_type == DW_RANGES_END) {
5608 printf("%s\n", "<End of list>");
5610 } else if (ranges[j].dwr_type ==
5611 DW_RANGES_ADDRESS_SELECTION) {
5612 base0 = ranges[j].dwr_addr2;
5615 if (re->ec == ELFCLASS32)
5616 printf("%08jx %08jx\n",
5617 ranges[j].dwr_addr1 + base0,
5618 ranges[j].dwr_addr2 + base0);
5620 printf("%016jx %016jx\n",
5621 ranges[j].dwr_addr1 + base0,
5622 ranges[j].dwr_addr2 + base0);
5627 /* Search children. */
5628 ret = dwarf_child(die, &ret_die, &de);
5629 if (ret == DW_DLV_ERROR)
5630 warnx("dwarf_child: %s", dwarf_errmsg(de));
5631 else if (ret == DW_DLV_OK)
5632 dump_dwarf_ranges_foreach(re, ret_die, base);
5634 /* Search sibling. */
5635 ret = dwarf_siblingof(re->dbg, die, &ret_die, &de);
5636 if (ret == DW_DLV_ERROR)
5637 warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
5638 else if (ret == DW_DLV_OK)
5639 dump_dwarf_ranges_foreach(re, ret_die, base);
5643 dump_dwarf_ranges(struct readelf *re)
5645 Dwarf_Ranges *ranges;
5648 Dwarf_Unsigned bytecnt;
5651 Dwarf_Unsigned lowpc;
5654 if (dwarf_get_ranges(re->dbg, 0, &ranges, &cnt, &bytecnt, &de) !=
5658 printf("Contents of the .debug_ranges section:\n\n");
5659 if (re->ec == ELFCLASS32)
5660 printf(" %-8s %-8s %s\n", "Offset", "Begin", "End");
5662 printf(" %-8s %-16s %s\n", "Offset", "Begin", "End");
5664 while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
5665 NULL, &de)) == DW_DLV_OK) {
5667 if (dwarf_siblingof(re->dbg, die, &die, &de) != DW_DLV_OK)
5669 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
5670 warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
5673 /* XXX: What about DW_TAG_partial_unit? */
5675 if (tag == DW_TAG_compile_unit) {
5676 if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lowpc,
5681 dump_dwarf_ranges_foreach(re, die, (Dwarf_Addr) lowpc);
5687 dump_dwarf_macinfo(struct readelf *re)
5689 Dwarf_Unsigned offset;
5691 Dwarf_Macro_Details *md;
5697 #define _MAX_MACINFO_ENTRY 65535
5699 printf("\nContents of section .debug_macinfo:\n\n");
5702 while (dwarf_get_macro_details(re->dbg, offset, _MAX_MACINFO_ENTRY,
5703 &cnt, &md, &de) == DW_DLV_OK) {
5704 for (i = 0; i < cnt; i++) {
5705 offset = md[i].dmd_offset + 1;
5706 if (md[i].dmd_type == 0)
5708 if (dwarf_get_MACINFO_name(md[i].dmd_type, &mi_str) !=
5710 snprintf(unk_mi, sizeof(unk_mi),
5711 "[Unknown MACINFO: %#x]", md[i].dmd_type);
5714 printf(" %s", mi_str);
5715 switch (md[i].dmd_type) {
5716 case DW_MACINFO_define:
5717 case DW_MACINFO_undef:
5718 printf(" - lineno : %jd macro : %s\n",
5719 (intmax_t) md[i].dmd_lineno,
5722 case DW_MACINFO_start_file:
5723 printf(" - lineno : %jd filenum : %jd\n",
5724 (intmax_t) md[i].dmd_lineno,
5725 (intmax_t) md[i].dmd_fileindex);
5734 #undef _MAX_MACINFO_ENTRY
5738 dump_dwarf_frame_inst(struct readelf *re, Dwarf_Cie cie, uint8_t *insts,
5739 Dwarf_Unsigned len, Dwarf_Unsigned caf, Dwarf_Signed daf, Dwarf_Addr pc,
5742 Dwarf_Frame_Op *oplist;
5743 Dwarf_Signed opcnt, delta;
5750 if (dwarf_expand_frame_instructions(cie, insts, len, &oplist,
5751 &opcnt, &de) != DW_DLV_OK) {
5752 warnx("dwarf_expand_frame_instructions failed: %s",
5757 for (i = 0; i < opcnt; i++) {
5758 if (oplist[i].fp_base_op != 0)
5759 op = oplist[i].fp_base_op << 6;
5761 op = oplist[i].fp_extended_op;
5762 if (dwarf_get_CFA_name(op, &op_str) != DW_DLV_OK) {
5763 snprintf(unk_op, sizeof(unk_op), "[Unknown CFA: %#x]",
5767 printf(" %s", op_str);
5769 case DW_CFA_advance_loc:
5770 delta = oplist[i].fp_offset * caf;
5772 printf(": %ju to %08jx", (uintmax_t) delta,
5776 case DW_CFA_offset_extended:
5777 case DW_CFA_offset_extended_sf:
5778 delta = oplist[i].fp_offset * daf;
5779 printf(": r%u (%s) at cfa%+jd", oplist[i].fp_register,
5780 dwarf_regname(re, oplist[i].fp_register),
5783 case DW_CFA_restore:
5784 printf(": r%u (%s)", oplist[i].fp_register,
5785 dwarf_regname(re, oplist[i].fp_register));
5787 case DW_CFA_set_loc:
5788 pc = oplist[i].fp_offset;
5789 printf(": to %08jx", (uintmax_t) pc);
5791 case DW_CFA_advance_loc1:
5792 case DW_CFA_advance_loc2:
5793 case DW_CFA_advance_loc4:
5794 pc += oplist[i].fp_offset;
5795 printf(": %jd to %08jx", (intmax_t) oplist[i].fp_offset,
5798 case DW_CFA_def_cfa:
5799 printf(": r%u (%s) ofs %ju", oplist[i].fp_register,
5800 dwarf_regname(re, oplist[i].fp_register),
5801 (uintmax_t) oplist[i].fp_offset);
5803 case DW_CFA_def_cfa_sf:
5804 printf(": r%u (%s) ofs %jd", oplist[i].fp_register,
5805 dwarf_regname(re, oplist[i].fp_register),
5806 (intmax_t) (oplist[i].fp_offset * daf));
5808 case DW_CFA_def_cfa_register:
5809 printf(": r%u (%s)", oplist[i].fp_register,
5810 dwarf_regname(re, oplist[i].fp_register));
5812 case DW_CFA_def_cfa_offset:
5813 printf(": %ju", (uintmax_t) oplist[i].fp_offset);
5815 case DW_CFA_def_cfa_offset_sf:
5816 printf(": %jd", (intmax_t) (oplist[i].fp_offset * daf));
5824 dwarf_dealloc(dbg, oplist, DW_DLA_FRAME_BLOCK);
5828 get_regoff_str(struct readelf *re, Dwarf_Half reg, Dwarf_Addr off)
5832 if (reg == DW_FRAME_UNDEFINED_VAL || reg == DW_FRAME_REG_INITIAL_VALUE)
5833 snprintf(rs, sizeof(rs), "%c", 'u');
5834 else if (reg == DW_FRAME_CFA_COL)
5835 snprintf(rs, sizeof(rs), "c%+jd", (intmax_t) off);
5837 snprintf(rs, sizeof(rs), "%s%+jd", dwarf_regname(re, reg),
5844 dump_dwarf_frame_regtable(struct readelf *re, Dwarf_Fde fde, Dwarf_Addr pc,
5845 Dwarf_Unsigned func_len, Dwarf_Half cie_ra)
5848 Dwarf_Addr row_pc, end_pc, pre_pc, cur_pc;
5853 #define BIT_SET(v, n) (v[(n)>>3] |= 1U << ((n) & 7))
5854 #define BIT_CLR(v, n) (v[(n)>>3] &= ~(1U << ((n) & 7)))
5855 #define BIT_ISSET(v, n) (v[(n)>>3] & (1U << ((n) & 7)))
5856 #define RT(x) rt.rules[(x)]
5858 vec = calloc((DW_REG_TABLE_SIZE + 7) / 8, 1);
5860 err(EXIT_FAILURE, "calloc failed");
5862 pre_pc = ~((Dwarf_Addr) 0);
5864 end_pc = pc + func_len;
5865 for (; cur_pc < end_pc; cur_pc++) {
5866 if (dwarf_get_fde_info_for_all_regs(fde, cur_pc, &rt, &row_pc,
5867 &de) != DW_DLV_OK) {
5868 warnx("dwarf_get_fde_info_for_all_regs failed: %s\n",
5872 if (row_pc == pre_pc)
5875 for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
5876 if (rt.rules[i].dw_regnum != DW_FRAME_REG_INITIAL_VALUE)
5881 printf(" LOC CFA ");
5882 for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
5883 if (BIT_ISSET(vec, i)) {
5884 if ((Dwarf_Half) i == cie_ra)
5888 dwarf_regname(re, (unsigned int) i));
5893 pre_pc = ~((Dwarf_Addr) 0);
5895 end_pc = pc + func_len;
5896 for (; cur_pc < end_pc; cur_pc++) {
5897 if (dwarf_get_fde_info_for_all_regs(fde, cur_pc, &rt, &row_pc,
5898 &de) != DW_DLV_OK) {
5899 warnx("dwarf_get_fde_info_for_all_regs failed: %s\n",
5903 if (row_pc == pre_pc)
5906 printf("%08jx ", (uintmax_t) row_pc);
5907 printf("%-8s ", get_regoff_str(re, RT(0).dw_regnum,
5909 for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
5910 if (BIT_ISSET(vec, i)) {
5911 printf("%-5s", get_regoff_str(re,
5912 RT(i).dw_regnum, RT(i).dw_offset));
5929 dump_dwarf_frame_section(struct readelf *re, struct section *s, int alt)
5931 Dwarf_Cie *cie_list, cie, pre_cie;
5932 Dwarf_Fde *fde_list, fde;
5933 Dwarf_Off cie_offset, fde_offset;
5934 Dwarf_Unsigned cie_length, fde_instlen;
5935 Dwarf_Unsigned cie_caf, cie_daf, cie_instlen, func_len, fde_length;
5936 Dwarf_Signed cie_count, fde_count, cie_index;
5939 Dwarf_Small cie_version;
5940 Dwarf_Ptr fde_addr, fde_inst, cie_inst;
5945 printf("\nThe section %s contains:\n\n", s->name);
5947 if (!strcmp(s->name, ".debug_frame")) {
5949 if (dwarf_get_fde_list(re->dbg, &cie_list, &cie_count,
5950 &fde_list, &fde_count, &de) != DW_DLV_OK) {
5951 warnx("dwarf_get_fde_list failed: %s",
5955 } else if (!strcmp(s->name, ".eh_frame")) {
5957 if (dwarf_get_fde_list_eh(re->dbg, &cie_list, &cie_count,
5958 &fde_list, &fde_count, &de) != DW_DLV_OK) {
5959 warnx("dwarf_get_fde_list_eh failed: %s",
5967 for (i = 0; i < fde_count; i++) {
5968 if (dwarf_get_fde_n(fde_list, i, &fde, &de) != DW_DLV_OK) {
5969 warnx("dwarf_get_fde_n failed: %s", dwarf_errmsg(de));
5972 if (dwarf_get_cie_of_fde(fde, &cie, &de) != DW_DLV_OK) {
5973 warnx("dwarf_get_fde_n failed: %s", dwarf_errmsg(de));
5976 if (dwarf_get_fde_range(fde, &low_pc, &func_len, &fde_addr,
5977 &fde_length, &cie_offset, &cie_index, &fde_offset,
5978 &de) != DW_DLV_OK) {
5979 warnx("dwarf_get_fde_range failed: %s",
5983 if (dwarf_get_fde_instr_bytes(fde, &fde_inst, &fde_instlen,
5984 &de) != DW_DLV_OK) {
5985 warnx("dwarf_get_fde_instr_bytes failed: %s",
5989 if (pre_cie == NULL || cie != pre_cie) {
5991 if (dwarf_get_cie_info(cie, &cie_length, &cie_version,
5992 &cie_aug, &cie_caf, &cie_daf, &cie_ra,
5993 &cie_inst, &cie_instlen, &de) != DW_DLV_OK) {
5994 warnx("dwarf_get_cie_info failed: %s",
5998 printf("%08jx %08jx %8.8jx CIE",
5999 (uintmax_t) cie_offset,
6000 (uintmax_t) cie_length,
6001 (uintmax_t) (eh_frame ? 0 : ~0U));
6004 printf(" Version:\t\t\t%u\n", cie_version);
6005 printf(" Augmentation:\t\t\t\"");
6006 while ((c = *cie_aug++) != '\0')
6009 printf(" Code alignment factor:\t%ju\n",
6010 (uintmax_t) cie_caf);
6011 printf(" Data alignment factor:\t%jd\n",
6012 (intmax_t) cie_daf);
6013 printf(" Return address column:\t%ju\n",
6014 (uintmax_t) cie_ra);
6016 dump_dwarf_frame_inst(re, cie, cie_inst,
6017 cie_instlen, cie_caf, cie_daf, 0,
6022 while ((c = *cie_aug++) != '\0')
6025 printf(" cf=%ju df=%jd ra=%ju\n",
6026 (uintmax_t) cie_caf,
6027 (uintmax_t) cie_daf,
6028 (uintmax_t) cie_ra);
6029 dump_dwarf_frame_regtable(re, fde, low_pc, 1,
6034 printf("%08jx %08jx %08jx FDE cie=%08jx pc=%08jx..%08jx\n",
6035 (uintmax_t) fde_offset, (uintmax_t) fde_length,
6036 (uintmax_t) cie_offset,
6037 (uintmax_t) (eh_frame ? fde_offset + 4 - cie_offset :
6039 (uintmax_t) low_pc, (uintmax_t) (low_pc + func_len));
6041 dump_dwarf_frame_inst(re, cie, fde_inst, fde_instlen,
6042 cie_caf, cie_daf, low_pc, re->dbg);
6044 dump_dwarf_frame_regtable(re, fde, low_pc, func_len,
6051 dump_dwarf_frame(struct readelf *re, int alt)
6056 (void) dwarf_set_frame_cfa_value(re->dbg, DW_FRAME_CFA_COL);
6058 for (i = 0; (size_t) i < re->shnum; i++) {
6060 if (s->name != NULL && (!strcmp(s->name, ".debug_frame") ||
6061 !strcmp(s->name, ".eh_frame")))
6062 dump_dwarf_frame_section(re, s, alt);
6067 dump_dwarf_str(struct readelf *re)
6072 int elferr, end, i, j;
6074 printf("\nContents of section .debug_str:\n");
6077 for (i = 0; (size_t) i < re->shnum; i++) {
6079 if (s->name != NULL && !strcmp(s->name, ".debug_str"))
6082 if ((size_t) i >= re->shnum)
6086 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
6087 elferr = elf_errno();
6089 warnx("elf_getdata failed: %s", elf_errmsg(-1));
6095 for (i = 0, p = d->d_buf; (size_t) i < d->d_size; i += 16) {
6096 printf(" 0x%08x", (unsigned int) i);
6097 if ((size_t) i + 16 > d->d_size)
6101 for (j = i; j < i + 16; j++) {
6102 if ((j - i) % 4 == 0)
6108 printf("%02x", (uint8_t) p[j]);
6111 for (j = i; j < end; j++) {
6124 Dwarf_Attribute la_at;
6125 Dwarf_Unsigned la_off;
6126 Dwarf_Unsigned la_lowpc;
6127 Dwarf_Half la_cu_psize;
6128 Dwarf_Half la_cu_osize;
6129 Dwarf_Half la_cu_ver;
6130 TAILQ_ENTRY(loc_at) la_next;
6133 static TAILQ_HEAD(, loc_at) lalist = TAILQ_HEAD_INITIALIZER(lalist);
6136 search_loclist_at(struct readelf *re, Dwarf_Die die, Dwarf_Unsigned lowpc)
6138 Dwarf_Attribute *attr_list;
6142 Dwarf_Signed attr_count;
6143 Dwarf_Half attr, form;
6146 struct loc_at *la, *nla;
6149 is_info = dwarf_get_die_infotypes_flag(die);
6151 if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
6153 if (ret == DW_DLV_ERROR)
6154 warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
6157 for (i = 0; i < attr_count; i++) {
6158 if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
6159 warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
6162 if (attr != DW_AT_location &&
6163 attr != DW_AT_string_length &&
6164 attr != DW_AT_return_addr &&
6165 attr != DW_AT_data_member_location &&
6166 attr != DW_AT_frame_base &&
6167 attr != DW_AT_segment &&
6168 attr != DW_AT_static_link &&
6169 attr != DW_AT_use_location &&
6170 attr != DW_AT_vtable_elem_location)
6172 if (dwarf_whatform(attr_list[i], &form, &de) != DW_DLV_OK) {
6173 warnx("dwarf_whatform failed: %s", dwarf_errmsg(de));
6176 if (form == DW_FORM_data4 || form == DW_FORM_data8) {
6177 if (dwarf_formudata(attr_list[i], &off, &de) !=
6179 warnx("dwarf_formudata failed: %s",
6183 } else if (form == DW_FORM_sec_offset) {
6184 if (dwarf_global_formref(attr_list[i], &ref, &de) !=
6186 warnx("dwarf_global_formref failed: %s",
6194 TAILQ_FOREACH(la, &lalist, la_next) {
6195 if (off == la->la_off)
6197 if (off < la->la_off) {
6198 if ((nla = malloc(sizeof(*nla))) == NULL)
6199 err(EXIT_FAILURE, "malloc failed");
6200 nla->la_at = attr_list[i];
6202 nla->la_lowpc = lowpc;
6203 nla->la_cu_psize = re->cu_psize;
6204 nla->la_cu_osize = re->cu_osize;
6205 nla->la_cu_ver = re->cu_ver;
6206 TAILQ_INSERT_BEFORE(la, nla, la_next);
6211 if ((nla = malloc(sizeof(*nla))) == NULL)
6212 err(EXIT_FAILURE, "malloc failed");
6213 nla->la_at = attr_list[i];
6215 nla->la_lowpc = lowpc;
6216 nla->la_cu_psize = re->cu_psize;
6217 nla->la_cu_osize = re->cu_osize;
6218 nla->la_cu_ver = re->cu_ver;
6219 TAILQ_INSERT_TAIL(&lalist, nla, la_next);
6224 /* Search children. */
6225 ret = dwarf_child(die, &ret_die, &de);
6226 if (ret == DW_DLV_ERROR)
6227 warnx("dwarf_child: %s", dwarf_errmsg(de));
6228 else if (ret == DW_DLV_OK)
6229 search_loclist_at(re, ret_die, lowpc);
6231 /* Search sibling. */
6232 ret = dwarf_siblingof_b(re->dbg, die, &ret_die, is_info, &de);
6233 if (ret == DW_DLV_ERROR)
6234 warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
6235 else if (ret == DW_DLV_OK)
6236 search_loclist_at(re, ret_die, lowpc);
6240 dump_dwarf_loc(struct readelf *re, Dwarf_Loc *lr)
6247 if (dwarf_get_OP_name(lr->lr_atom, &op_str) !=
6249 snprintf(unk_op, sizeof(unk_op),
6250 "[Unknown OP: %#x]", lr->lr_atom);
6254 printf("%s", op_str);
6256 switch (lr->lr_atom) {
6289 printf(" (%s)", dwarf_regname(re, lr->lr_atom - DW_OP_reg0));
6352 case DW_OP_push_object_address:
6353 case DW_OP_form_tls_address:
6354 case DW_OP_call_frame_cfa:
6355 case DW_OP_stack_value:
6356 case DW_OP_GNU_push_tls_address:
6357 case DW_OP_GNU_uninit:
6362 case DW_OP_deref_size:
6363 case DW_OP_xderef_size:
6370 case DW_OP_plus_uconst:
6373 printf(": %ju", (uintmax_t)
6382 printf(": %jd", (intmax_t)
6418 printf(" (%s): %jd",
6419 dwarf_regname(re, lr->lr_atom - DW_OP_breg0),
6420 (intmax_t) lr->lr_number);
6424 printf(": %jd", (intmax_t)
6429 printf(": %ju (%s) %jd",
6430 (uintmax_t) lr->lr_number,
6431 dwarf_regname(re, (unsigned int) lr->lr_number),
6432 (intmax_t) lr->lr_number2);
6436 case DW_OP_GNU_encoded_addr:
6437 printf(": %#jx", (uintmax_t)
6441 case DW_OP_GNU_implicit_pointer:
6442 printf(": <0x%jx> %jd", (uintmax_t) lr->lr_number,
6443 (intmax_t) lr->lr_number2);
6446 case DW_OP_implicit_value:
6447 printf(": %ju byte block:", (uintmax_t) lr->lr_number);
6448 b = (uint8_t *)(uintptr_t) lr->lr_number2;
6449 for (i = 0; (Dwarf_Unsigned) i < lr->lr_number; i++)
6450 printf(" %x", b[i]);
6453 case DW_OP_GNU_entry_value:
6455 dump_dwarf_block(re, (uint8_t *)(uintptr_t) lr->lr_number2,
6460 case DW_OP_GNU_const_type:
6461 printf(": <0x%jx> ", (uintmax_t) lr->lr_number);
6462 b = (uint8_t *)(uintptr_t) lr->lr_number2;
6464 for (i = 1; (uint8_t) i < n; i++)
6465 printf(" %x", b[i]);
6468 case DW_OP_GNU_regval_type:
6469 printf(": %ju (%s) <0x%jx>", (uintmax_t) lr->lr_number,
6470 dwarf_regname(re, (unsigned int) lr->lr_number),
6471 (uintmax_t) lr->lr_number2);
6474 case DW_OP_GNU_convert:
6475 case DW_OP_GNU_deref_type:
6476 case DW_OP_GNU_parameter_ref:
6477 case DW_OP_GNU_reinterpret:
6478 printf(": <0x%jx>", (uintmax_t) lr->lr_number);
6487 dump_dwarf_block(struct readelf *re, uint8_t *b, Dwarf_Unsigned len)
6489 Dwarf_Locdesc *llbuf;
6494 if (dwarf_loclist_from_expr_b(re->dbg, b, len, re->cu_psize,
6495 re->cu_osize, re->cu_ver, &llbuf, &lcnt, &de) != DW_DLV_OK) {
6496 warnx("dwarf_loclist_form_expr_b: %s", dwarf_errmsg(de));
6500 for (i = 0; (Dwarf_Half) i < llbuf->ld_cents; i++) {
6501 dump_dwarf_loc(re, &llbuf->ld_s[i]);
6502 if (i < llbuf->ld_cents - 1)
6506 dwarf_dealloc(re->dbg, llbuf->ld_s, DW_DLA_LOC_BLOCK);
6507 dwarf_dealloc(re->dbg, llbuf, DW_DLA_LOCDESC);
6511 dump_dwarf_loclist(struct readelf *re)
6514 Dwarf_Locdesc **llbuf;
6515 Dwarf_Unsigned lowpc;
6517 Dwarf_Half tag, version, pointer_size, off_size;
6522 printf("\nContents of section .debug_loc:\n");
6524 /* Search .debug_info section. */
6525 while ((ret = dwarf_next_cu_header_b(re->dbg, NULL, &version, NULL,
6526 &pointer_size, &off_size, NULL, NULL, &de)) == DW_DLV_OK) {
6527 set_cu_context(re, pointer_size, off_size, version);
6529 if (dwarf_siblingof(re->dbg, die, &die, &de) != DW_DLV_OK)
6531 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
6532 warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
6535 /* XXX: What about DW_TAG_partial_unit? */
6537 if (tag == DW_TAG_compile_unit) {
6538 if (dwarf_attrval_unsigned(die, DW_AT_low_pc,
6539 &lowpc, &de) != DW_DLV_OK)
6543 /* Search attributes for reference to .debug_loc section. */
6544 search_loclist_at(re, die, lowpc);
6546 if (ret == DW_DLV_ERROR)
6547 warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
6549 /* Search .debug_types section. */
6551 while ((ret = dwarf_next_cu_header_c(re->dbg, 0, NULL,
6552 &version, NULL, &pointer_size, &off_size, NULL, NULL,
6553 NULL, NULL, &de)) == DW_DLV_OK) {
6554 set_cu_context(re, pointer_size, off_size, version);
6556 if (dwarf_siblingof(re->dbg, die, &die, &de) !=
6559 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
6560 warnx("dwarf_tag failed: %s",
6566 if (tag == DW_TAG_type_unit) {
6567 if (dwarf_attrval_unsigned(die, DW_AT_low_pc,
6568 &lowpc, &de) != DW_DLV_OK)
6573 * Search attributes for reference to .debug_loc
6576 search_loclist_at(re, die, lowpc);
6578 if (ret == DW_DLV_ERROR)
6579 warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
6580 } while (dwarf_next_types_section(re->dbg, &de) == DW_DLV_OK);
6582 if (TAILQ_EMPTY(&lalist))
6585 printf(" Offset Begin End Expression\n");
6587 TAILQ_FOREACH(la, &lalist, la_next) {
6588 if (dwarf_loclist_n(la->la_at, &llbuf, &lcnt, &de) !=
6590 warnx("dwarf_loclist_n failed: %s", dwarf_errmsg(de));
6593 set_cu_context(re, la->la_cu_psize, la->la_cu_osize,
6595 for (i = 0; i < lcnt; i++) {
6596 printf(" %8.8jx ", la->la_off);
6597 if (llbuf[i]->ld_lopc == 0 && llbuf[i]->ld_hipc == 0) {
6598 printf("<End of list>\n");
6602 /* TODO: handle base selection entry. */
6604 printf("%8.8jx %8.8jx ",
6605 (uintmax_t) (la->la_lowpc + llbuf[i]->ld_lopc),
6606 (uintmax_t) (la->la_lowpc + llbuf[i]->ld_hipc));
6609 for (j = 0; (Dwarf_Half) j < llbuf[i]->ld_cents; j++) {
6610 dump_dwarf_loc(re, &llbuf[i]->ld_s[j]);
6611 if (j < llbuf[i]->ld_cents - 1)
6616 if (llbuf[i]->ld_lopc == llbuf[i]->ld_hipc)
6617 printf(" (start == end)");
6620 for (i = 0; i < lcnt; i++) {
6621 dwarf_dealloc(re->dbg, llbuf[i]->ld_s,
6623 dwarf_dealloc(re->dbg, llbuf[i], DW_DLA_LOCDESC);
6625 dwarf_dealloc(re->dbg, llbuf, DW_DLA_LIST);
6630 * Retrieve a string using string table section index and the string offset.
6633 get_string(struct readelf *re, int strtab, size_t off)
6637 if ((name = elf_strptr(re->elf, strtab, off)) == NULL)
6644 * Retrieve the name of a symbol using the section index of the symbol
6645 * table and the index of the symbol within that table.
6648 get_symbol_name(struct readelf *re, int symtab, int i)
6656 s = &re->sl[symtab];
6657 if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM)
6660 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
6661 elferr = elf_errno();
6663 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
6666 if (gelf_getsym(data, i, &sym) != &sym)
6668 /* Return section name for STT_SECTION symbol. */
6669 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION &&
6670 re->sl[sym.st_shndx].name != NULL)
6671 return (re->sl[sym.st_shndx].name);
6672 if ((name = elf_strptr(re->elf, s->link, sym.st_name)) == NULL)
6679 get_symbol_value(struct readelf *re, int symtab, int i)
6686 s = &re->sl[symtab];
6687 if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM)
6690 if ((data = elf_getdata(s->scn, NULL)) == NULL) {
6691 elferr = elf_errno();
6693 warnx("elf_getdata failed: %s", elf_errmsg(elferr));
6696 if (gelf_getsym(data, i, &sym) != &sym)
6699 return (sym.st_value);
6703 hex_dump(struct readelf *re)
6712 for (i = 1; (size_t) i < re->shnum; i++) {
6714 if (find_dumpop(re, (size_t) i, s->name, HEX_DUMP, -1) == NULL)
6717 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
6718 elferr = elf_errno();
6720 warnx("elf_getdata failed: %s",
6721 elf_errmsg(elferr));
6724 if (d->d_size <= 0 || d->d_buf == NULL) {
6725 printf("\nSection '%s' has no data to dump.\n",
6732 printf("\nHex dump of section '%s':\n", s->name);
6734 printf(" 0x%8.8jx ", (uintmax_t)addr);
6735 nbytes = sz > 16? 16 : sz;
6736 for (j = 0; j < 16; j++) {
6737 if ((size_t)j < nbytes)
6738 printf("%2.2x", buf[j]);
6744 for (j = 0; (size_t)j < nbytes; j++) {
6745 if (isprint(buf[j]))
6746 printf("%c", buf[j]);
6759 str_dump(struct readelf *re)
6763 unsigned char *start, *end, *buf_end;
6765 int i, j, elferr, found;
6767 for (i = 1; (size_t) i < re->shnum; i++) {
6769 if (find_dumpop(re, (size_t) i, s->name, STR_DUMP, -1) == NULL)
6772 if ((d = elf_getdata(s->scn, NULL)) == NULL) {
6773 elferr = elf_errno();
6775 warnx("elf_getdata failed: %s",
6776 elf_errmsg(elferr));
6779 if (d->d_size <= 0 || d->d_buf == NULL) {
6780 printf("\nSection '%s' has no data to dump.\n",
6784 buf_end = (unsigned char *) d->d_buf + d->d_size;
6785 start = (unsigned char *) d->d_buf;
6787 printf("\nString dump of section '%s':\n", s->name);
6789 while (start < buf_end && !isprint(*start))
6791 if (start >= buf_end)
6794 while (end < buf_end && isprint(*end))
6797 (long) (start - (unsigned char *) d->d_buf));
6799 for (j = 0; (unsigned int) j < len; j++)
6808 printf(" No strings found in this section.");
6814 load_sections(struct readelf *re)
6820 size_t shstrndx, ndx;
6823 /* Allocate storage for internal section list. */
6824 if (!elf_getshnum(re->elf, &re->shnum)) {
6825 warnx("elf_getshnum failed: %s", elf_errmsg(-1));
6830 if ((re->sl = calloc(re->shnum, sizeof(*re->sl))) == NULL)
6831 err(EXIT_FAILURE, "calloc failed");
6833 /* Get the index of .shstrtab section. */
6834 if (!elf_getshstrndx(re->elf, &shstrndx)) {
6835 warnx("elf_getshstrndx failed: %s", elf_errmsg(-1));
6839 if ((scn = elf_getscn(re->elf, 0)) == NULL)
6844 if (gelf_getshdr(scn, &sh) == NULL) {
6845 warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
6849 if ((name = elf_strptr(re->elf, shstrndx, sh.sh_name)) == NULL) {
6853 if ((ndx = elf_ndxscn(scn)) == SHN_UNDEF) {
6854 if ((elferr = elf_errno()) != 0)
6855 warnx("elf_ndxscn failed: %s",
6856 elf_errmsg(elferr));
6859 if (ndx >= re->shnum) {
6860 warnx("section index of '%s' out of range", name);
6866 s->off = sh.sh_offset;
6868 s->entsize = sh.sh_entsize;
6869 s->align = sh.sh_addralign;
6870 s->type = sh.sh_type;
6871 s->flags = sh.sh_flags;
6872 s->addr = sh.sh_addr;
6873 s->link = sh.sh_link;
6874 s->info = sh.sh_info;
6875 } while ((scn = elf_nextscn(re->elf, scn)) != NULL);
6876 elferr = elf_errno();
6878 warnx("elf_nextscn failed: %s", elf_errmsg(elferr));
6882 unload_sections(struct readelf *re)
6885 if (re->sl != NULL) {
6895 if (re->ver != NULL) {
6903 dump_elf(struct readelf *re)
6906 /* Fetch ELF header. No need to continue if it fails. */
6907 if (gelf_getehdr(re->elf, &re->ehdr) == NULL) {
6908 warnx("gelf_getehdr failed: %s", elf_errmsg(-1));
6911 if ((re->ec = gelf_getclass(re->elf)) == ELFCLASSNONE) {
6912 warnx("gelf_getclass failed: %s", elf_errmsg(-1));
6915 if (re->ehdr.e_ident[EI_DATA] == ELFDATA2MSB) {
6916 re->dw_read = _read_msb;
6917 re->dw_decode = _decode_msb;
6919 re->dw_read = _read_lsb;
6920 re->dw_decode = _decode_lsb;
6923 if (re->options & ~RE_H)
6925 if ((re->options & RE_VV) || (re->options & RE_S))
6927 if (re->options & RE_H)
6929 if (re->options & RE_L)
6931 if (re->options & RE_SS)
6933 if (re->options & RE_G)
6934 dump_section_groups(re);
6935 if (re->options & RE_D)
6937 if (re->options & RE_R)
6939 if (re->options & RE_S)
6941 if (re->options & RE_N)
6943 if (re->options & RE_II)
6945 if (re->options & RE_X)
6947 if (re->options & RE_P)
6949 if (re->options & RE_VV)
6951 if (re->options & RE_AA)
6952 dump_arch_specific_info(re);
6953 if (re->options & RE_W)
6955 if (re->options & ~RE_H)
6956 unload_sections(re);
6960 dump_dwarf(struct readelf *re)
6965 if (dwarf_elf_init(re->elf, DW_DLC_READ, NULL, NULL, &re->dbg, &de)) {
6966 if ((error = dwarf_errno(de)) != DW_DLE_DEBUG_INFO_NULL)
6967 errx(EXIT_FAILURE, "dwarf_elf_init failed: %s",
6973 dump_dwarf_abbrev(re);
6975 dump_dwarf_line(re);
6976 if (re->dop & DW_LL)
6977 dump_dwarf_line_decoded(re);
6978 if (re->dop & DW_I) {
6979 dump_dwarf_info(re, 0);
6980 dump_dwarf_info(re, 1);
6983 dump_dwarf_pubnames(re);
6985 dump_dwarf_aranges(re);
6986 if (re->dop & DW_RR)
6987 dump_dwarf_ranges(re);
6989 dump_dwarf_macinfo(re);
6991 dump_dwarf_frame(re, 0);
6992 else if (re->dop & DW_FF)
6993 dump_dwarf_frame(re, 1);
6997 dump_dwarf_loclist(re);
6999 dwarf_finish(re->dbg, &de);
7003 dump_ar(struct readelf *re, int fd)
7015 if (re->options & RE_C) {
7016 if ((arsym = elf_getarsym(re->ar, &sz)) == NULL) {
7017 warnx("elf_getarsym() failed: %s", elf_errmsg(-1));
7018 goto process_members;
7020 printf("Index of archive %s: (%ju entries)\n", re->filename,
7021 (uintmax_t) sz - 1);
7023 for (i = 0; (size_t) i < sz; i++) {
7024 if (arsym[i].as_name == NULL)
7026 if (arsym[i].as_off != off) {
7027 off = arsym[i].as_off;
7028 if (elf_rand(re->ar, off) != off) {
7029 warnx("elf_rand() failed: %s",
7033 if ((e = elf_begin(fd, ELF_C_READ, re->ar)) ==
7035 warnx("elf_begin() failed: %s",
7039 if ((arhdr = elf_getarhdr(e)) == NULL) {
7040 warnx("elf_getarhdr() failed: %s",
7045 printf("Binary %s(%s) contains:\n",
7046 re->filename, arhdr->ar_name);
7048 printf("\t%s\n", arsym[i].as_name);
7050 if (elf_rand(re->ar, SARMAG) != SARMAG) {
7051 warnx("elf_rand() failed: %s", elf_errmsg(-1));
7058 if ((re->options & ~RE_C) == 0)
7062 while ((re->elf = elf_begin(fd, cmd, re->ar)) != NULL) {
7063 if ((arhdr = elf_getarhdr(re->elf)) == NULL) {
7064 warnx("elf_getarhdr() failed: %s", elf_errmsg(-1));
7067 if (strcmp(arhdr->ar_name, "/") == 0 ||
7068 strcmp(arhdr->ar_name, "//") == 0 ||
7069 strcmp(arhdr->ar_name, "__.SYMDEF") == 0)
7071 printf("\nFile: %s(%s)\n", re->filename, arhdr->ar_name);
7075 cmd = elf_next(re->elf);
7082 dump_object(struct readelf *re)
7086 if ((fd = open(re->filename, O_RDONLY)) == -1) {
7087 warn("open %s failed", re->filename);
7091 if ((re->flags & DISPLAY_FILENAME) != 0)
7092 printf("\nFile: %s\n", re->filename);
7094 if ((re->elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
7095 warnx("elf_begin() failed: %s", elf_errmsg(-1));
7099 switch (elf_kind(re->elf)) {
7101 warnx("Not an ELF file.");
7110 warnx("Internal: libelf returned unknown elf kind.");
7118 add_dumpop(struct readelf *re, size_t si, const char *sn, int op, int t)
7122 if ((d = find_dumpop(re, si, sn, -1, t)) == NULL) {
7123 if ((d = calloc(1, sizeof(*d))) == NULL)
7124 err(EXIT_FAILURE, "calloc failed");
7125 if (t == DUMP_BY_INDEX)
7131 STAILQ_INSERT_TAIL(&re->v_dumpop, d, dumpop_list);
7136 static struct dumpop *
7137 find_dumpop(struct readelf *re, size_t si, const char *sn, int op, int t)
7141 STAILQ_FOREACH(d, &re->v_dumpop, dumpop_list) {
7142 if ((op == -1 || op & d->op) &&
7143 (t == -1 || (unsigned) t == d->type)) {
7144 if ((d->type == DUMP_BY_INDEX && d->u.si == si) ||
7145 (d->type == DUMP_BY_NAME && !strcmp(d->u.sn, sn)))
7158 {"rawline", 'l', DW_L},
7159 {"decodedline", 'L', DW_LL},
7160 {"info", 'i', DW_I},
7161 {"abbrev", 'a', DW_A},
7162 {"pubnames", 'p', DW_P},
7163 {"aranges", 'r', DW_R},
7164 {"ranges", 'r', DW_R},
7165 {"Ranges", 'R', DW_RR},
7166 {"macro", 'm', DW_M},
7167 {"frames", 'f', DW_F},
7168 {"frames-interp", 'F', DW_FF},
7175 parse_dwarf_op_short(struct readelf *re, const char *op)
7180 re->dop |= DW_DEFAULT_OPTIONS;
7184 for (; *op != '\0'; op++) {
7185 for (i = 0; dwarf_op[i].ln != NULL; i++) {
7186 if (dwarf_op[i].sn == *op) {
7187 re->dop |= dwarf_op[i].value;
7195 parse_dwarf_op_long(struct readelf *re, const char *op)
7197 char *p, *token, *bp;
7201 re->dop |= DW_DEFAULT_OPTIONS;
7205 if ((p = strdup(op)) == NULL)
7206 err(EXIT_FAILURE, "strdup failed");
7209 while ((token = strsep(&p, ",")) != NULL) {
7210 for (i = 0; dwarf_op[i].ln != NULL; i++) {
7211 if (!strcmp(token, dwarf_op[i].ln)) {
7212 re->dop |= dwarf_op[i].value;
7222 _read_lsb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read)
7227 src = (uint8_t *) d->d_buf + *offsetp;
7230 switch (bytes_to_read) {
7232 ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
7233 ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
7235 ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
7237 ret |= ((uint64_t) src[1]) << 8;
7245 *offsetp += bytes_to_read;
7251 _read_msb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read)
7256 src = (uint8_t *) d->d_buf + *offsetp;
7258 switch (bytes_to_read) {
7263 ret = src[1] | ((uint64_t) src[0]) << 8;
7266 ret = src[3] | ((uint64_t) src[2]) << 8;
7267 ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24;
7270 ret = src[7] | ((uint64_t) src[6]) << 8;
7271 ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24;
7272 ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40;
7273 ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56;
7279 *offsetp += bytes_to_read;
7285 _decode_lsb(uint8_t **data, int bytes_to_read)
7293 switch (bytes_to_read) {
7295 ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
7296 ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
7298 ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
7300 ret |= ((uint64_t) src[1]) << 8;
7308 *data += bytes_to_read;
7314 _decode_msb(uint8_t **data, int bytes_to_read)
7322 switch (bytes_to_read) {
7327 ret = src[1] | ((uint64_t) src[0]) << 8;
7330 ret = src[3] | ((uint64_t) src[2]) << 8;
7331 ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24;
7334 ret = src[7] | ((uint64_t) src[6]) << 8;
7335 ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24;
7336 ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40;
7337 ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56;
7344 *data += bytes_to_read;
7350 _decode_sleb128(uint8_t **dp)
7360 ret |= ((b & 0x7f) << shift);
7362 } while ((b & 0x80) != 0);
7364 if (shift < 32 && (b & 0x40) != 0)
7365 ret |= (-1 << shift);
7373 _decode_uleb128(uint8_t **dp)
7383 ret |= ((b & 0x7f) << shift);
7385 } while ((b & 0x80) != 0);
7393 readelf_version(void)
7395 (void) printf("%s (%s)\n", ELFTC_GETPROGNAME(),
7400 #define USAGE_MESSAGE "\
7401 Usage: %s [options] file...\n\
7402 Display information about ELF objects and ar(1) archives.\n\n\
7404 -a | --all Equivalent to specifying options '-dhIlrsASV'.\n\
7405 -c | --archive-index Print the archive symbol table for archives.\n\
7406 -d | --dynamic Print the contents of SHT_DYNAMIC sections.\n\
7407 -e | --headers Print all headers in the object.\n\
7408 -g | --section-groups Print the contents of the section groups.\n\
7409 -h | --file-header Print the file header for the object.\n\
7410 -l | --program-headers Print the PHDR table for the object.\n\
7411 -n | --notes Print the contents of SHT_NOTE sections.\n\
7412 -p INDEX | --string-dump=INDEX\n\
7413 Print the contents of section at index INDEX.\n\
7414 -r | --relocs Print relocation information.\n\
7415 -s | --syms | --symbols Print symbol tables.\n\
7416 -t | --section-details Print additional information about sections.\n\
7417 -v | --version Print a version identifier and exit.\n\
7418 -x INDEX | --hex-dump=INDEX\n\
7419 Display contents of a section as hexadecimal.\n\
7420 -A | --arch-specific (accepted, but ignored)\n\
7421 -D | --use-dynamic Print the symbol table specified by the DT_SYMTAB\n\
7422 entry in the \".dynamic\" section.\n\
7423 -H | --help Print a help message.\n\
7424 -I | --histogram Print information on bucket list lengths for \n\
7426 -N | --full-section-name (accepted, but ignored)\n\
7427 -S | --sections | --section-headers\n\
7428 Print information about section headers.\n\
7429 -V | --version-info Print symbol versoning information.\n\
7430 -W | --wide Print information without wrapping long lines.\n"
7436 fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());
7441 main(int argc, char **argv)
7443 struct readelf *re, re_storage;
7449 memset(re, 0, sizeof(*re));
7450 STAILQ_INIT(&re->v_dumpop);
7452 while ((opt = getopt_long(argc, argv, "AacDdegHhIi:lNnp:rSstuVvWw::x:",
7453 longopts, NULL)) != -1) {
7459 re->options |= RE_AA;
7462 re->options |= RE_AA | RE_D | RE_G | RE_H | RE_II |
7463 RE_L | RE_R | RE_SS | RE_S | RE_VV;
7466 re->options |= RE_C;
7469 re->options |= RE_DD;
7472 re->options |= RE_D;
7475 re->options |= RE_H | RE_L | RE_SS;
7478 re->options |= RE_G;
7484 re->options |= RE_H;
7487 re->options |= RE_II;
7490 /* Not implemented yet. */
7493 re->options |= RE_L;
7496 re->options |= RE_NN;
7499 re->options |= RE_N;
7502 re->options |= RE_P;
7503 si = strtoul(optarg, &ep, 10);
7505 add_dumpop(re, (size_t) si, NULL, STR_DUMP,
7508 add_dumpop(re, 0, optarg, STR_DUMP,
7512 re->options |= RE_R;
7515 re->options |= RE_SS;
7518 re->options |= RE_S;
7521 re->options |= RE_T;
7524 re->options |= RE_U;
7527 re->options |= RE_VV;
7533 re->options |= RE_WW;
7536 re->options |= RE_W;
7537 parse_dwarf_op_short(re, optarg);
7540 re->options |= RE_X;
7541 si = strtoul(optarg, &ep, 10);
7543 add_dumpop(re, (size_t) si, NULL, HEX_DUMP,
7546 add_dumpop(re, 0, optarg, HEX_DUMP,
7549 case OPTION_DEBUG_DUMP:
7550 re->options |= RE_W;
7551 parse_dwarf_op_long(re, optarg);
7558 if (argc == 0 || re->options == 0)
7562 re->flags |= DISPLAY_FILENAME;
7564 if (elf_version(EV_CURRENT) == EV_NONE)
7565 errx(EXIT_FAILURE, "ELF library initialization failed: %s",
7568 for (i = 0; i < argc; i++) {
7569 re->filename = argv[i];