1 /*- 2 * Copyright (c) 2009-2015 Kai Wang 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/param.h> 28 #include <sys/queue.h> 29 #include <ar.h> 30 #include <assert.h> 31 #include <ctype.h> 32 #include <dwarf.h> 33 #include <err.h> 34 #include <fcntl.h> 35 #include <gelf.h> 36 #include <getopt.h> 37 #include <libdwarf.h> 38 #include <libelftc.h> 39 #include <libgen.h> 40 #include <stdarg.h> 41 #include <stdint.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <string.h> 45 #include <time.h> 46 #include <unistd.h> 47 48 #include "_elftc.h" 49 50 ELFTC_VCSID("$Id: readelf.c 3469 2016-05-15 23:16:09Z emaste $"); 51 52 /* Backwards compatability for older FreeBSD releases. */ 53 #ifndef STB_GNU_UNIQUE 54 #define STB_GNU_UNIQUE 10 55 #endif 56 #ifndef STT_SPARC_REGISTER 57 #define STT_SPARC_REGISTER 13 58 #endif 59 60 61 /* 62 * readelf(1) options. 63 */ 64 #define RE_AA 0x00000001 65 #define RE_C 0x00000002 66 #define RE_DD 0x00000004 67 #define RE_D 0x00000008 68 #define RE_G 0x00000010 69 #define RE_H 0x00000020 70 #define RE_II 0x00000040 71 #define RE_I 0x00000080 72 #define RE_L 0x00000100 73 #define RE_NN 0x00000200 74 #define RE_N 0x00000400 75 #define RE_P 0x00000800 76 #define RE_R 0x00001000 77 #define RE_SS 0x00002000 78 #define RE_S 0x00004000 79 #define RE_T 0x00008000 80 #define RE_U 0x00010000 81 #define RE_VV 0x00020000 82 #define RE_WW 0x00040000 83 #define RE_W 0x00080000 84 #define RE_X 0x00100000 85 86 /* 87 * dwarf dump options. 88 */ 89 #define DW_A 0x00000001 90 #define DW_FF 0x00000002 91 #define DW_F 0x00000004 92 #define DW_I 0x00000008 93 #define DW_LL 0x00000010 94 #define DW_L 0x00000020 95 #define DW_M 0x00000040 96 #define DW_O 0x00000080 97 #define DW_P 0x00000100 98 #define DW_RR 0x00000200 99 #define DW_R 0x00000400 100 #define DW_S 0x00000800 101 102 #define DW_DEFAULT_OPTIONS (DW_A | DW_F | DW_I | DW_L | DW_O | DW_P | \ 103 DW_R | DW_RR | DW_S) 104 105 /* 106 * readelf(1) run control flags. 107 */ 108 #define DISPLAY_FILENAME 0x0001 109 110 /* 111 * Internal data structure for sections. 112 */ 113 struct section { 114 const char *name; /* section name */ 115 Elf_Scn *scn; /* section scn */ 116 uint64_t off; /* section offset */ 117 uint64_t sz; /* section size */ 118 uint64_t entsize; /* section entsize */ 119 uint64_t align; /* section alignment */ 120 uint64_t type; /* section type */ 121 uint64_t flags; /* section flags */ 122 uint64_t addr; /* section virtual addr */ 123 uint32_t link; /* section link ndx */ 124 uint32_t info; /* section info ndx */ 125 }; 126 127 struct dumpop { 128 union { 129 size_t si; /* section index */ 130 const char *sn; /* section name */ 131 } u; 132 enum { 133 DUMP_BY_INDEX = 0, 134 DUMP_BY_NAME 135 } type; /* dump type */ 136 #define HEX_DUMP 0x0001 137 #define STR_DUMP 0x0002 138 int op; /* dump operation */ 139 STAILQ_ENTRY(dumpop) dumpop_list; 140 }; 141 142 struct symver { 143 const char *name; 144 int type; 145 }; 146 147 /* 148 * Structure encapsulates the global data for readelf(1). 149 */ 150 struct readelf { 151 const char *filename; /* current processing file. */ 152 int options; /* command line options. */ 153 int flags; /* run control flags. */ 154 int dop; /* dwarf dump options. */ 155 Elf *elf; /* underlying ELF descriptor. */ 156 Elf *ar; /* archive ELF descriptor. */ 157 Dwarf_Debug dbg; /* DWARF handle. */ 158 Dwarf_Half cu_psize; /* DWARF CU pointer size. */ 159 Dwarf_Half cu_osize; /* DWARF CU offset size. */ 160 Dwarf_Half cu_ver; /* DWARF CU version. */ 161 GElf_Ehdr ehdr; /* ELF header. */ 162 int ec; /* ELF class. */ 163 size_t shnum; /* #sections. */ 164 struct section *vd_s; /* Verdef section. */ 165 struct section *vn_s; /* Verneed section. */ 166 struct section *vs_s; /* Versym section. */ 167 uint16_t *vs; /* Versym array. */ 168 int vs_sz; /* Versym array size. */ 169 struct symver *ver; /* Version array. */ 170 int ver_sz; /* Size of version array. */ 171 struct section *sl; /* list of sections. */ 172 STAILQ_HEAD(, dumpop) v_dumpop; /* list of dump ops. */ 173 uint64_t (*dw_read)(Elf_Data *, uint64_t *, int); 174 uint64_t (*dw_decode)(uint8_t **, int); 175 }; 176 177 enum options 178 { 179 OPTION_DEBUG_DUMP 180 }; 181 182 static struct option longopts[] = { 183 {"all", no_argument, NULL, 'a'}, 184 {"arch-specific", no_argument, NULL, 'A'}, 185 {"archive-index", no_argument, NULL, 'c'}, 186 {"debug-dump", optional_argument, NULL, OPTION_DEBUG_DUMP}, 187 {"dynamic", no_argument, NULL, 'd'}, 188 {"file-header", no_argument, NULL, 'h'}, 189 {"full-section-name", no_argument, NULL, 'N'}, 190 {"headers", no_argument, NULL, 'e'}, 191 {"help", no_argument, 0, 'H'}, 192 {"hex-dump", required_argument, NULL, 'x'}, 193 {"histogram", no_argument, NULL, 'I'}, 194 {"notes", no_argument, NULL, 'n'}, 195 {"program-headers", no_argument, NULL, 'l'}, 196 {"relocs", no_argument, NULL, 'r'}, 197 {"sections", no_argument, NULL, 'S'}, 198 {"section-headers", no_argument, NULL, 'S'}, 199 {"section-groups", no_argument, NULL, 'g'}, 200 {"section-details", no_argument, NULL, 't'}, 201 {"segments", no_argument, NULL, 'l'}, 202 {"string-dump", required_argument, NULL, 'p'}, 203 {"symbols", no_argument, NULL, 's'}, 204 {"syms", no_argument, NULL, 's'}, 205 {"unwind", no_argument, NULL, 'u'}, 206 {"use-dynamic", no_argument, NULL, 'D'}, 207 {"version-info", no_argument, 0, 'V'}, 208 {"version", no_argument, 0, 'v'}, 209 {"wide", no_argument, 0, 'W'}, 210 {NULL, 0, NULL, 0} 211 }; 212 213 struct eflags_desc { 214 uint64_t flag; 215 const char *desc; 216 }; 217 218 struct mips_option { 219 uint64_t flag; 220 const char *desc; 221 }; 222 223 static void add_dumpop(struct readelf *re, size_t si, const char *sn, int op, 224 int t); 225 static const char *aeabi_adv_simd_arch(uint64_t simd); 226 static const char *aeabi_align_needed(uint64_t an); 227 static const char *aeabi_align_preserved(uint64_t ap); 228 static const char *aeabi_arm_isa(uint64_t ai); 229 static const char *aeabi_cpu_arch(uint64_t arch); 230 static const char *aeabi_cpu_arch_profile(uint64_t pf); 231 static const char *aeabi_div(uint64_t du); 232 static const char *aeabi_enum_size(uint64_t es); 233 static const char *aeabi_fp_16bit_format(uint64_t fp16); 234 static const char *aeabi_fp_arch(uint64_t fp); 235 static const char *aeabi_fp_denormal(uint64_t fd); 236 static const char *aeabi_fp_exceptions(uint64_t fe); 237 static const char *aeabi_fp_hpext(uint64_t fh); 238 static const char *aeabi_fp_number_model(uint64_t fn); 239 static const char *aeabi_fp_optm_goal(uint64_t fog); 240 static const char *aeabi_fp_rounding(uint64_t fr); 241 static const char *aeabi_hardfp(uint64_t hfp); 242 static const char *aeabi_mpext(uint64_t mp); 243 static const char *aeabi_optm_goal(uint64_t og); 244 static const char *aeabi_pcs_config(uint64_t pcs); 245 static const char *aeabi_pcs_got(uint64_t got); 246 static const char *aeabi_pcs_r9(uint64_t r9); 247 static const char *aeabi_pcs_ro(uint64_t ro); 248 static const char *aeabi_pcs_rw(uint64_t rw); 249 static const char *aeabi_pcs_wchar_t(uint64_t wt); 250 static const char *aeabi_t2ee(uint64_t t2ee); 251 static const char *aeabi_thumb_isa(uint64_t ti); 252 static const char *aeabi_fp_user_exceptions(uint64_t fu); 253 static const char *aeabi_unaligned_access(uint64_t ua); 254 static const char *aeabi_vfp_args(uint64_t va); 255 static const char *aeabi_virtual(uint64_t vt); 256 static const char *aeabi_wmmx_arch(uint64_t wmmx); 257 static const char *aeabi_wmmx_args(uint64_t wa); 258 static const char *elf_class(unsigned int class); 259 static const char *elf_endian(unsigned int endian); 260 static const char *elf_machine(unsigned int mach); 261 static const char *elf_osabi(unsigned int abi); 262 static const char *elf_type(unsigned int type); 263 static const char *elf_ver(unsigned int ver); 264 static const char *dt_type(unsigned int mach, unsigned int dtype); 265 static void dump_ar(struct readelf *re, int); 266 static void dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe); 267 static void dump_attributes(struct readelf *re); 268 static uint8_t *dump_compatibility_tag(uint8_t *p, uint8_t *pe); 269 static void dump_dwarf(struct readelf *re); 270 static void dump_dwarf_abbrev(struct readelf *re); 271 static void dump_dwarf_aranges(struct readelf *re); 272 static void dump_dwarf_block(struct readelf *re, uint8_t *b, 273 Dwarf_Unsigned len); 274 static void dump_dwarf_die(struct readelf *re, Dwarf_Die die, int level); 275 static void dump_dwarf_frame(struct readelf *re, int alt); 276 static void dump_dwarf_frame_inst(struct readelf *re, Dwarf_Cie cie, 277 uint8_t *insts, Dwarf_Unsigned len, Dwarf_Unsigned caf, Dwarf_Signed daf, 278 Dwarf_Addr pc, Dwarf_Debug dbg); 279 static int dump_dwarf_frame_regtable(struct readelf *re, Dwarf_Fde fde, 280 Dwarf_Addr pc, Dwarf_Unsigned func_len, Dwarf_Half cie_ra); 281 static void dump_dwarf_frame_section(struct readelf *re, struct section *s, 282 int alt); 283 static void dump_dwarf_info(struct readelf *re, Dwarf_Bool is_info); 284 static void dump_dwarf_macinfo(struct readelf *re); 285 static void dump_dwarf_line(struct readelf *re); 286 static void dump_dwarf_line_decoded(struct readelf *re); 287 static void dump_dwarf_loc(struct readelf *re, Dwarf_Loc *lr); 288 static void dump_dwarf_loclist(struct readelf *re); 289 static void dump_dwarf_pubnames(struct readelf *re); 290 static void dump_dwarf_ranges(struct readelf *re); 291 static void dump_dwarf_ranges_foreach(struct readelf *re, Dwarf_Die die, 292 Dwarf_Addr base); 293 static void dump_dwarf_str(struct readelf *re); 294 static void dump_eflags(struct readelf *re, uint64_t e_flags); 295 static void dump_elf(struct readelf *re); 296 static void dump_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab); 297 static void dump_dynamic(struct readelf *re); 298 static void dump_liblist(struct readelf *re); 299 static void dump_mips_attributes(struct readelf *re, uint8_t *p, uint8_t *pe); 300 static void dump_mips_odk_reginfo(struct readelf *re, uint8_t *p, size_t sz); 301 static void dump_mips_options(struct readelf *re, struct section *s); 302 static void dump_mips_option_flags(const char *name, struct mips_option *opt, 303 uint64_t info); 304 static void dump_mips_reginfo(struct readelf *re, struct section *s); 305 static void dump_mips_specific_info(struct readelf *re); 306 static void dump_notes(struct readelf *re); 307 static void dump_notes_content(struct readelf *re, const char *buf, size_t sz, 308 off_t off); 309 static void dump_svr4_hash(struct section *s); 310 static void dump_svr4_hash64(struct readelf *re, struct section *s); 311 static void dump_gnu_hash(struct readelf *re, struct section *s); 312 static void dump_hash(struct readelf *re); 313 static void dump_phdr(struct readelf *re); 314 static void dump_ppc_attributes(uint8_t *p, uint8_t *pe); 315 static void dump_section_groups(struct readelf *re); 316 static void dump_symtab(struct readelf *re, int i); 317 static void dump_symtabs(struct readelf *re); 318 static uint8_t *dump_unknown_tag(uint64_t tag, uint8_t *p, uint8_t *pe); 319 static void dump_ver(struct readelf *re); 320 static void dump_verdef(struct readelf *re, int dump); 321 static void dump_verneed(struct readelf *re, int dump); 322 static void dump_versym(struct readelf *re); 323 static const char *dwarf_reg(unsigned int mach, unsigned int reg); 324 static const char *dwarf_regname(struct readelf *re, unsigned int num); 325 static struct dumpop *find_dumpop(struct readelf *re, size_t si, 326 const char *sn, int op, int t); 327 static int get_ent_count(struct section *s, int *ent_count); 328 static char *get_regoff_str(struct readelf *re, Dwarf_Half reg, 329 Dwarf_Addr off); 330 static const char *get_string(struct readelf *re, int strtab, size_t off); 331 static const char *get_symbol_name(struct readelf *re, int symtab, int i); 332 static uint64_t get_symbol_value(struct readelf *re, int symtab, int i); 333 static void load_sections(struct readelf *re); 334 static const char *mips_abi_fp(uint64_t fp); 335 static const char *note_type(const char *note_name, unsigned int et, 336 unsigned int nt); 337 static const char *note_type_freebsd(unsigned int nt); 338 static const char *note_type_freebsd_core(unsigned int nt); 339 static const char *note_type_linux_core(unsigned int nt); 340 static const char *note_type_gnu(unsigned int nt); 341 static const char *note_type_netbsd(unsigned int nt); 342 static const char *note_type_openbsd(unsigned int nt); 343 static const char *note_type_unknown(unsigned int nt); 344 static const char *note_type_xen(unsigned int nt); 345 static const char *option_kind(uint8_t kind); 346 static const char *phdr_type(unsigned int mach, unsigned int ptype); 347 static const char *ppc_abi_fp(uint64_t fp); 348 static const char *ppc_abi_vector(uint64_t vec); 349 static void readelf_usage(int status); 350 static void readelf_version(void); 351 static void search_loclist_at(struct readelf *re, Dwarf_Die die, 352 Dwarf_Unsigned lowpc); 353 static void search_ver(struct readelf *re); 354 static const char *section_type(unsigned int mach, unsigned int stype); 355 static void set_cu_context(struct readelf *re, Dwarf_Half psize, 356 Dwarf_Half osize, Dwarf_Half ver); 357 static const char *st_bind(unsigned int sbind); 358 static const char *st_shndx(unsigned int shndx); 359 static const char *st_type(unsigned int mach, unsigned int os, 360 unsigned int stype); 361 static const char *st_vis(unsigned int svis); 362 static const char *top_tag(unsigned int tag); 363 static void unload_sections(struct readelf *re); 364 static uint64_t _read_lsb(Elf_Data *d, uint64_t *offsetp, 365 int bytes_to_read); 366 static uint64_t _read_msb(Elf_Data *d, uint64_t *offsetp, 367 int bytes_to_read); 368 static uint64_t _decode_lsb(uint8_t **data, int bytes_to_read); 369 static uint64_t _decode_msb(uint8_t **data, int bytes_to_read); 370 static int64_t _decode_sleb128(uint8_t **dp, uint8_t *dpe); 371 static uint64_t _decode_uleb128(uint8_t **dp, uint8_t *dpe); 372 373 static struct eflags_desc arm_eflags_desc[] = { 374 {EF_ARM_RELEXEC, "relocatable executable"}, 375 {EF_ARM_HASENTRY, "has entry point"}, 376 {EF_ARM_SYMSARESORTED, "sorted symbol tables"}, 377 {EF_ARM_DYNSYMSUSESEGIDX, "dynamic symbols use segment index"}, 378 {EF_ARM_MAPSYMSFIRST, "mapping symbols precede others"}, 379 {EF_ARM_BE8, "BE8"}, 380 {EF_ARM_LE8, "LE8"}, 381 {EF_ARM_INTERWORK, "interworking enabled"}, 382 {EF_ARM_APCS_26, "uses APCS/26"}, 383 {EF_ARM_APCS_FLOAT, "uses APCS/float"}, 384 {EF_ARM_PIC, "position independent"}, 385 {EF_ARM_ALIGN8, "8 bit structure alignment"}, 386 {EF_ARM_NEW_ABI, "uses new ABI"}, 387 {EF_ARM_OLD_ABI, "uses old ABI"}, 388 {EF_ARM_SOFT_FLOAT, "software FP"}, 389 {EF_ARM_VFP_FLOAT, "VFP"}, 390 {EF_ARM_MAVERICK_FLOAT, "Maverick FP"}, 391 {0, NULL} 392 }; 393 394 static struct eflags_desc mips_eflags_desc[] = { 395 {EF_MIPS_NOREORDER, "noreorder"}, 396 {EF_MIPS_PIC, "pic"}, 397 {EF_MIPS_CPIC, "cpic"}, 398 {EF_MIPS_UCODE, "ugen_reserved"}, 399 {EF_MIPS_ABI2, "abi2"}, 400 {EF_MIPS_OPTIONS_FIRST, "odk first"}, 401 {EF_MIPS_ARCH_ASE_MDMX, "mdmx"}, 402 {EF_MIPS_ARCH_ASE_M16, "mips16"}, 403 {0, NULL} 404 }; 405 406 static struct eflags_desc powerpc_eflags_desc[] = { 407 {EF_PPC_EMB, "emb"}, 408 {EF_PPC_RELOCATABLE, "relocatable"}, 409 {EF_PPC_RELOCATABLE_LIB, "relocatable-lib"}, 410 {0, NULL} 411 }; 412 413 static struct eflags_desc sparc_eflags_desc[] = { 414 {EF_SPARC_32PLUS, "v8+"}, 415 {EF_SPARC_SUN_US1, "ultrasparcI"}, 416 {EF_SPARC_HAL_R1, "halr1"}, 417 {EF_SPARC_SUN_US3, "ultrasparcIII"}, 418 {0, NULL} 419 }; 420 421 static const char * 422 elf_osabi(unsigned int abi) 423 { 424 static char s_abi[32]; 425 426 switch(abi) { 427 case ELFOSABI_NONE: return "NONE"; 428 case ELFOSABI_HPUX: return "HPUX"; 429 case ELFOSABI_NETBSD: return "NetBSD"; 430 case ELFOSABI_GNU: return "GNU"; 431 case ELFOSABI_HURD: return "HURD"; 432 case ELFOSABI_86OPEN: return "86OPEN"; 433 case ELFOSABI_SOLARIS: return "Solaris"; 434 case ELFOSABI_AIX: return "AIX"; 435 case ELFOSABI_IRIX: return "IRIX"; 436 case ELFOSABI_FREEBSD: return "FreeBSD"; 437 case ELFOSABI_TRU64: return "TRU64"; 438 case ELFOSABI_MODESTO: return "MODESTO"; 439 case ELFOSABI_OPENBSD: return "OpenBSD"; 440 case ELFOSABI_OPENVMS: return "OpenVMS"; 441 case ELFOSABI_NSK: return "NSK"; 442 case ELFOSABI_CLOUDABI: return "CloudABI"; 443 case ELFOSABI_ARM: return "ARM"; 444 case ELFOSABI_STANDALONE: return "StandAlone"; 445 default: 446 snprintf(s_abi, sizeof(s_abi), "<unknown: %#x>", abi); 447 return (s_abi); 448 } 449 }; 450 451 static const char * 452 elf_machine(unsigned int mach) 453 { 454 static char s_mach[32]; 455 456 switch (mach) { 457 case EM_NONE: return "Unknown machine"; 458 case EM_M32: return "AT&T WE32100"; 459 case EM_SPARC: return "Sun SPARC"; 460 case EM_386: return "Intel i386"; 461 case EM_68K: return "Motorola 68000"; 462 case EM_IAMCU: return "Intel MCU"; 463 case EM_88K: return "Motorola 88000"; 464 case EM_860: return "Intel i860"; 465 case EM_MIPS: return "MIPS R3000 Big-Endian only"; 466 case EM_S370: return "IBM System/370"; 467 case EM_MIPS_RS3_LE: return "MIPS R3000 Little-Endian"; 468 case EM_PARISC: return "HP PA-RISC"; 469 case EM_VPP500: return "Fujitsu VPP500"; 470 case EM_SPARC32PLUS: return "SPARC v8plus"; 471 case EM_960: return "Intel 80960"; 472 case EM_PPC: return "PowerPC 32-bit"; 473 case EM_PPC64: return "PowerPC 64-bit"; 474 case EM_S390: return "IBM System/390"; 475 case EM_V800: return "NEC V800"; 476 case EM_FR20: return "Fujitsu FR20"; 477 case EM_RH32: return "TRW RH-32"; 478 case EM_RCE: return "Motorola RCE"; 479 case EM_ARM: return "ARM"; 480 case EM_SH: return "Hitachi SH"; 481 case EM_SPARCV9: return "SPARC v9 64-bit"; 482 case EM_TRICORE: return "Siemens TriCore embedded processor"; 483 case EM_ARC: return "Argonaut RISC Core"; 484 case EM_H8_300: return "Hitachi H8/300"; 485 case EM_H8_300H: return "Hitachi H8/300H"; 486 case EM_H8S: return "Hitachi H8S"; 487 case EM_H8_500: return "Hitachi H8/500"; 488 case EM_IA_64: return "Intel IA-64 Processor"; 489 case EM_MIPS_X: return "Stanford MIPS-X"; 490 case EM_COLDFIRE: return "Motorola ColdFire"; 491 case EM_68HC12: return "Motorola M68HC12"; 492 case EM_MMA: return "Fujitsu MMA"; 493 case EM_PCP: return "Siemens PCP"; 494 case EM_NCPU: return "Sony nCPU"; 495 case EM_NDR1: return "Denso NDR1 microprocessor"; 496 case EM_STARCORE: return "Motorola Star*Core processor"; 497 case EM_ME16: return "Toyota ME16 processor"; 498 case EM_ST100: return "STMicroelectronics ST100 processor"; 499 case EM_TINYJ: return "Advanced Logic Corp. TinyJ processor"; 500 case EM_X86_64: return "Advanced Micro Devices x86-64"; 501 case EM_PDSP: return "Sony DSP Processor"; 502 case EM_FX66: return "Siemens FX66 microcontroller"; 503 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 microcontroller"; 504 case EM_ST7: return "STmicroelectronics ST7 8-bit microcontroller"; 505 case EM_68HC16: return "Motorola MC68HC16 microcontroller"; 506 case EM_68HC11: return "Motorola MC68HC11 microcontroller"; 507 case EM_68HC08: return "Motorola MC68HC08 microcontroller"; 508 case EM_68HC05: return "Motorola MC68HC05 microcontroller"; 509 case EM_SVX: return "Silicon Graphics SVx"; 510 case EM_ST19: return "STMicroelectronics ST19 8-bit mc"; 511 case EM_VAX: return "Digital VAX"; 512 case EM_CRIS: return "Axis Communications 32-bit embedded processor"; 513 case EM_JAVELIN: return "Infineon Tech. 32bit embedded processor"; 514 case EM_FIREPATH: return "Element 14 64-bit DSP Processor"; 515 case EM_ZSP: return "LSI Logic 16-bit DSP Processor"; 516 case EM_MMIX: return "Donald Knuth's educational 64-bit proc"; 517 case EM_HUANY: return "Harvard University MI object files"; 518 case EM_PRISM: return "SiTera Prism"; 519 case EM_AVR: return "Atmel AVR 8-bit microcontroller"; 520 case EM_FR30: return "Fujitsu FR30"; 521 case EM_D10V: return "Mitsubishi D10V"; 522 case EM_D30V: return "Mitsubishi D30V"; 523 case EM_V850: return "NEC v850"; 524 case EM_M32R: return "Mitsubishi M32R"; 525 case EM_MN10300: return "Matsushita MN10300"; 526 case EM_MN10200: return "Matsushita MN10200"; 527 case EM_PJ: return "picoJava"; 528 case EM_OPENRISC: return "OpenRISC 32-bit embedded processor"; 529 case EM_ARC_A5: return "ARC Cores Tangent-A5"; 530 case EM_XTENSA: return "Tensilica Xtensa Architecture"; 531 case EM_VIDEOCORE: return "Alphamosaic VideoCore processor"; 532 case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor"; 533 case EM_NS32K: return "National Semiconductor 32000 series"; 534 case EM_TPC: return "Tenor Network TPC processor"; 535 case EM_SNP1K: return "Trebia SNP 1000 processor"; 536 case EM_ST200: return "STMicroelectronics ST200 microcontroller"; 537 case EM_IP2K: return "Ubicom IP2xxx microcontroller family"; 538 case EM_MAX: return "MAX Processor"; 539 case EM_CR: return "National Semiconductor CompactRISC microprocessor"; 540 case EM_F2MC16: return "Fujitsu F2MC16"; 541 case EM_MSP430: return "TI embedded microcontroller msp430"; 542 case EM_BLACKFIN: return "Analog Devices Blackfin (DSP) processor"; 543 case EM_SE_C33: return "S1C33 Family of Seiko Epson processors"; 544 case EM_SEP: return "Sharp embedded microprocessor"; 545 case EM_ARCA: return "Arca RISC Microprocessor"; 546 case EM_UNICORE: return "Microprocessor series from PKU-Unity Ltd"; 547 case EM_AARCH64: return "AArch64"; 548 case EM_RISCV: return "RISC-V"; 549 default: 550 snprintf(s_mach, sizeof(s_mach), "<unknown: %#x>", mach); 551 return (s_mach); 552 } 553 554 } 555 556 static const char * 557 elf_class(unsigned int class) 558 { 559 static char s_class[32]; 560 561 switch (class) { 562 case ELFCLASSNONE: return "none"; 563 case ELFCLASS32: return "ELF32"; 564 case ELFCLASS64: return "ELF64"; 565 default: 566 snprintf(s_class, sizeof(s_class), "<unknown: %#x>", class); 567 return (s_class); 568 } 569 } 570 571 static const char * 572 elf_endian(unsigned int endian) 573 { 574 static char s_endian[32]; 575 576 switch (endian) { 577 case ELFDATANONE: return "none"; 578 case ELFDATA2LSB: return "2's complement, little endian"; 579 case ELFDATA2MSB: return "2's complement, big endian"; 580 default: 581 snprintf(s_endian, sizeof(s_endian), "<unknown: %#x>", endian); 582 return (s_endian); 583 } 584 } 585 586 static const char * 587 elf_type(unsigned int type) 588 { 589 static char s_type[32]; 590 591 switch (type) { 592 case ET_NONE: return "NONE (None)"; 593 case ET_REL: return "REL (Relocatable file)"; 594 case ET_EXEC: return "EXEC (Executable file)"; 595 case ET_DYN: return "DYN (Shared object file)"; 596 case ET_CORE: return "CORE (Core file)"; 597 default: 598 if (type >= ET_LOPROC) 599 snprintf(s_type, sizeof(s_type), "<proc: %#x>", type); 600 else if (type >= ET_LOOS && type <= ET_HIOS) 601 snprintf(s_type, sizeof(s_type), "<os: %#x>", type); 602 else 603 snprintf(s_type, sizeof(s_type), "<unknown: %#x>", 604 type); 605 return (s_type); 606 } 607 } 608 609 static const char * 610 elf_ver(unsigned int ver) 611 { 612 static char s_ver[32]; 613 614 switch (ver) { 615 case EV_CURRENT: return "(current)"; 616 case EV_NONE: return "(none)"; 617 default: 618 snprintf(s_ver, sizeof(s_ver), "<unknown: %#x>", 619 ver); 620 return (s_ver); 621 } 622 } 623 624 static const char * 625 phdr_type(unsigned int mach, unsigned int ptype) 626 { 627 static char s_ptype[32]; 628 629 if (ptype >= PT_LOPROC && ptype <= PT_HIPROC) { 630 switch (mach) { 631 case EM_ARM: 632 switch (ptype) { 633 case PT_ARM_ARCHEXT: return "ARM_ARCHEXT"; 634 case PT_ARM_EXIDX: return "ARM_EXIDX"; 635 } 636 break; 637 } 638 snprintf(s_ptype, sizeof(s_ptype), "LOPROC+%#x", 639 ptype - PT_LOPROC); 640 return (s_ptype); 641 } 642 643 switch (ptype) { 644 case PT_NULL: return "NULL"; 645 case PT_LOAD: return "LOAD"; 646 case PT_DYNAMIC: return "DYNAMIC"; 647 case PT_INTERP: return "INTERP"; 648 case PT_NOTE: return "NOTE"; 649 case PT_SHLIB: return "SHLIB"; 650 case PT_PHDR: return "PHDR"; 651 case PT_TLS: return "TLS"; 652 case PT_GNU_EH_FRAME: return "GNU_EH_FRAME"; 653 case PT_GNU_STACK: return "GNU_STACK"; 654 case PT_GNU_RELRO: return "GNU_RELRO"; 655 default: 656 if (ptype >= PT_LOOS && ptype <= PT_HIOS) 657 snprintf(s_ptype, sizeof(s_ptype), "LOOS+%#x", 658 ptype - PT_LOOS); 659 else 660 snprintf(s_ptype, sizeof(s_ptype), "<unknown: %#x>", 661 ptype); 662 return (s_ptype); 663 } 664 } 665 666 static const char * 667 section_type(unsigned int mach, unsigned int stype) 668 { 669 static char s_stype[32]; 670 671 if (stype >= SHT_LOPROC && stype <= SHT_HIPROC) { 672 switch (mach) { 673 case EM_ARM: 674 switch (stype) { 675 case SHT_ARM_EXIDX: return "ARM_EXIDX"; 676 case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP"; 677 case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES"; 678 case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY"; 679 case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION"; 680 } 681 break; 682 case EM_X86_64: 683 switch (stype) { 684 case SHT_X86_64_UNWIND: return "X86_64_UNWIND"; 685 default: 686 break; 687 } 688 break; 689 case EM_MIPS: 690 case EM_MIPS_RS3_LE: 691 switch (stype) { 692 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST"; 693 case SHT_MIPS_MSYM: return "MIPS_MSYM"; 694 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT"; 695 case SHT_MIPS_GPTAB: return "MIPS_GPTAB"; 696 case SHT_MIPS_UCODE: return "MIPS_UCODE"; 697 case SHT_MIPS_DEBUG: return "MIPS_DEBUG"; 698 case SHT_MIPS_REGINFO: return "MIPS_REGINFO"; 699 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE"; 700 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM"; 701 case SHT_MIPS_RELD: return "MIPS_RELD"; 702 case SHT_MIPS_IFACE: return "MIPS_IFACE"; 703 case SHT_MIPS_CONTENT: return "MIPS_CONTENT"; 704 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS"; 705 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM"; 706 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST"; 707 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS"; 708 case SHT_MIPS_DWARF: return "MIPS_DWARF"; 709 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL"; 710 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB"; 711 case SHT_MIPS_EVENTS: return "MIPS_EVENTS"; 712 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE"; 713 case SHT_MIPS_PIXIE: return "MIPS_PIXIE"; 714 case SHT_MIPS_XLATE: return "MIPS_XLATE"; 715 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG"; 716 case SHT_MIPS_WHIRL: return "MIPS_WHIRL"; 717 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION"; 718 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD"; 719 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION"; 720 default: 721 break; 722 } 723 break; 724 default: 725 break; 726 } 727 728 snprintf(s_stype, sizeof(s_stype), "LOPROC+%#x", 729 stype - SHT_LOPROC); 730 return (s_stype); 731 } 732 733 switch (stype) { 734 case SHT_NULL: return "NULL"; 735 case SHT_PROGBITS: return "PROGBITS"; 736 case SHT_SYMTAB: return "SYMTAB"; 737 case SHT_STRTAB: return "STRTAB"; 738 case SHT_RELA: return "RELA"; 739 case SHT_HASH: return "HASH"; 740 case SHT_DYNAMIC: return "DYNAMIC"; 741 case SHT_NOTE: return "NOTE"; 742 case SHT_NOBITS: return "NOBITS"; 743 case SHT_REL: return "REL"; 744 case SHT_SHLIB: return "SHLIB"; 745 case SHT_DYNSYM: return "DYNSYM"; 746 case SHT_INIT_ARRAY: return "INIT_ARRAY"; 747 case SHT_FINI_ARRAY: return "FINI_ARRAY"; 748 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY"; 749 case SHT_GROUP: return "GROUP"; 750 case SHT_SYMTAB_SHNDX: return "SYMTAB_SHNDX"; 751 case SHT_SUNW_dof: return "SUNW_dof"; 752 case SHT_SUNW_cap: return "SUNW_cap"; 753 case SHT_GNU_HASH: return "GNU_HASH"; 754 case SHT_SUNW_ANNOTATE: return "SUNW_ANNOTATE"; 755 case SHT_SUNW_DEBUGSTR: return "SUNW_DEBUGSTR"; 756 case SHT_SUNW_DEBUG: return "SUNW_DEBUG"; 757 case SHT_SUNW_move: return "SUNW_move"; 758 case SHT_SUNW_COMDAT: return "SUNW_COMDAT"; 759 case SHT_SUNW_syminfo: return "SUNW_syminfo"; 760 case SHT_SUNW_verdef: return "SUNW_verdef"; 761 case SHT_SUNW_verneed: return "SUNW_verneed"; 762 case SHT_SUNW_versym: return "SUNW_versym"; 763 default: 764 if (stype >= SHT_LOOS && stype <= SHT_HIOS) 765 snprintf(s_stype, sizeof(s_stype), "LOOS+%#x", 766 stype - SHT_LOOS); 767 else if (stype >= SHT_LOUSER) 768 snprintf(s_stype, sizeof(s_stype), "LOUSER+%#x", 769 stype - SHT_LOUSER); 770 else 771 snprintf(s_stype, sizeof(s_stype), "<unknown: %#x>", 772 stype); 773 return (s_stype); 774 } 775 } 776 777 static const char * 778 dt_type(unsigned int mach, unsigned int dtype) 779 { 780 static char s_dtype[32]; 781 782 if (dtype >= DT_LOPROC && dtype <= DT_HIPROC) { 783 switch (mach) { 784 case EM_ARM: 785 switch (dtype) { 786 case DT_ARM_SYMTABSZ: 787 return "ARM_SYMTABSZ"; 788 default: 789 break; 790 } 791 break; 792 case EM_MIPS: 793 case EM_MIPS_RS3_LE: 794 switch (dtype) { 795 case DT_MIPS_RLD_VERSION: 796 return "MIPS_RLD_VERSION"; 797 case DT_MIPS_TIME_STAMP: 798 return "MIPS_TIME_STAMP"; 799 case DT_MIPS_ICHECKSUM: 800 return "MIPS_ICHECKSUM"; 801 case DT_MIPS_IVERSION: 802 return "MIPS_IVERSION"; 803 case DT_MIPS_FLAGS: 804 return "MIPS_FLAGS"; 805 case DT_MIPS_BASE_ADDRESS: 806 return "MIPS_BASE_ADDRESS"; 807 case DT_MIPS_CONFLICT: 808 return "MIPS_CONFLICT"; 809 case DT_MIPS_LIBLIST: 810 return "MIPS_LIBLIST"; 811 case DT_MIPS_LOCAL_GOTNO: 812 return "MIPS_LOCAL_GOTNO"; 813 case DT_MIPS_CONFLICTNO: 814 return "MIPS_CONFLICTNO"; 815 case DT_MIPS_LIBLISTNO: 816 return "MIPS_LIBLISTNO"; 817 case DT_MIPS_SYMTABNO: 818 return "MIPS_SYMTABNO"; 819 case DT_MIPS_UNREFEXTNO: 820 return "MIPS_UNREFEXTNO"; 821 case DT_MIPS_GOTSYM: 822 return "MIPS_GOTSYM"; 823 case DT_MIPS_HIPAGENO: 824 return "MIPS_HIPAGENO"; 825 case DT_MIPS_RLD_MAP: 826 return "MIPS_RLD_MAP"; 827 case DT_MIPS_DELTA_CLASS: 828 return "MIPS_DELTA_CLASS"; 829 case DT_MIPS_DELTA_CLASS_NO: 830 return "MIPS_DELTA_CLASS_NO"; 831 case DT_MIPS_DELTA_INSTANCE: 832 return "MIPS_DELTA_INSTANCE"; 833 case DT_MIPS_DELTA_INSTANCE_NO: 834 return "MIPS_DELTA_INSTANCE_NO"; 835 case DT_MIPS_DELTA_RELOC: 836 return "MIPS_DELTA_RELOC"; 837 case DT_MIPS_DELTA_RELOC_NO: 838 return "MIPS_DELTA_RELOC_NO"; 839 case DT_MIPS_DELTA_SYM: 840 return "MIPS_DELTA_SYM"; 841 case DT_MIPS_DELTA_SYM_NO: 842 return "MIPS_DELTA_SYM_NO"; 843 case DT_MIPS_DELTA_CLASSSYM: 844 return "MIPS_DELTA_CLASSSYM"; 845 case DT_MIPS_DELTA_CLASSSYM_NO: 846 return "MIPS_DELTA_CLASSSYM_NO"; 847 case DT_MIPS_CXX_FLAGS: 848 return "MIPS_CXX_FLAGS"; 849 case DT_MIPS_PIXIE_INIT: 850 return "MIPS_PIXIE_INIT"; 851 case DT_MIPS_SYMBOL_LIB: 852 return "MIPS_SYMBOL_LIB"; 853 case DT_MIPS_LOCALPAGE_GOTIDX: 854 return "MIPS_LOCALPAGE_GOTIDX"; 855 case DT_MIPS_LOCAL_GOTIDX: 856 return "MIPS_LOCAL_GOTIDX"; 857 case DT_MIPS_HIDDEN_GOTIDX: 858 return "MIPS_HIDDEN_GOTIDX"; 859 case DT_MIPS_PROTECTED_GOTIDX: 860 return "MIPS_PROTECTED_GOTIDX"; 861 case DT_MIPS_OPTIONS: 862 return "MIPS_OPTIONS"; 863 case DT_MIPS_INTERFACE: 864 return "MIPS_INTERFACE"; 865 case DT_MIPS_DYNSTR_ALIGN: 866 return "MIPS_DYNSTR_ALIGN"; 867 case DT_MIPS_INTERFACE_SIZE: 868 return "MIPS_INTERFACE_SIZE"; 869 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: 870 return "MIPS_RLD_TEXT_RESOLVE_ADDR"; 871 case DT_MIPS_PERF_SUFFIX: 872 return "MIPS_PERF_SUFFIX"; 873 case DT_MIPS_COMPACT_SIZE: 874 return "MIPS_COMPACT_SIZE"; 875 case DT_MIPS_GP_VALUE: 876 return "MIPS_GP_VALUE"; 877 case DT_MIPS_AUX_DYNAMIC: 878 return "MIPS_AUX_DYNAMIC"; 879 case DT_MIPS_PLTGOT: 880 return "MIPS_PLTGOT"; 881 case DT_MIPS_RLD_OBJ_UPDATE: 882 return "MIPS_RLD_OBJ_UPDATE"; 883 case DT_MIPS_RWPLT: 884 return "MIPS_RWPLT"; 885 default: 886 break; 887 } 888 break; 889 case EM_SPARC: 890 case EM_SPARC32PLUS: 891 case EM_SPARCV9: 892 switch (dtype) { 893 case DT_SPARC_REGISTER: 894 return "DT_SPARC_REGISTER"; 895 default: 896 break; 897 } 898 break; 899 default: 900 break; 901 } 902 snprintf(s_dtype, sizeof(s_dtype), "<unknown: %#x>", dtype); 903 return (s_dtype); 904 } 905 906 switch (dtype) { 907 case DT_NULL: return "NULL"; 908 case DT_NEEDED: return "NEEDED"; 909 case DT_PLTRELSZ: return "PLTRELSZ"; 910 case DT_PLTGOT: return "PLTGOT"; 911 case DT_HASH: return "HASH"; 912 case DT_STRTAB: return "STRTAB"; 913 case DT_SYMTAB: return "SYMTAB"; 914 case DT_RELA: return "RELA"; 915 case DT_RELASZ: return "RELASZ"; 916 case DT_RELAENT: return "RELAENT"; 917 case DT_STRSZ: return "STRSZ"; 918 case DT_SYMENT: return "SYMENT"; 919 case DT_INIT: return "INIT"; 920 case DT_FINI: return "FINI"; 921 case DT_SONAME: return "SONAME"; 922 case DT_RPATH: return "RPATH"; 923 case DT_SYMBOLIC: return "SYMBOLIC"; 924 case DT_REL: return "REL"; 925 case DT_RELSZ: return "RELSZ"; 926 case DT_RELENT: return "RELENT"; 927 case DT_PLTREL: return "PLTREL"; 928 case DT_DEBUG: return "DEBUG"; 929 case DT_TEXTREL: return "TEXTREL"; 930 case DT_JMPREL: return "JMPREL"; 931 case DT_BIND_NOW: return "BIND_NOW"; 932 case DT_INIT_ARRAY: return "INIT_ARRAY"; 933 case DT_FINI_ARRAY: return "FINI_ARRAY"; 934 case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ"; 935 case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ"; 936 case DT_RUNPATH: return "RUNPATH"; 937 case DT_FLAGS: return "FLAGS"; 938 case DT_PREINIT_ARRAY: return "PREINIT_ARRAY"; 939 case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ"; 940 case DT_MAXPOSTAGS: return "MAXPOSTAGS"; 941 case DT_SUNW_AUXILIARY: return "SUNW_AUXILIARY"; 942 case DT_SUNW_RTLDINF: return "SUNW_RTLDINF"; 943 case DT_SUNW_FILTER: return "SUNW_FILTER"; 944 case DT_SUNW_CAP: return "SUNW_CAP"; 945 case DT_CHECKSUM: return "CHECKSUM"; 946 case DT_PLTPADSZ: return "PLTPADSZ"; 947 case DT_MOVEENT: return "MOVEENT"; 948 case DT_MOVESZ: return "MOVESZ"; 949 case DT_FEATURE: return "FEATURE"; 950 case DT_POSFLAG_1: return "POSFLAG_1"; 951 case DT_SYMINSZ: return "SYMINSZ"; 952 case DT_SYMINENT: return "SYMINENT"; 953 case DT_GNU_HASH: return "GNU_HASH"; 954 case DT_TLSDESC_PLT: return "DT_TLSDESC_PLT"; 955 case DT_TLSDESC_GOT: return "DT_TLSDESC_GOT"; 956 case DT_GNU_CONFLICT: return "GNU_CONFLICT"; 957 case DT_GNU_LIBLIST: return "GNU_LIBLIST"; 958 case DT_CONFIG: return "CONFIG"; 959 case DT_DEPAUDIT: return "DEPAUDIT"; 960 case DT_AUDIT: return "AUDIT"; 961 case DT_PLTPAD: return "PLTPAD"; 962 case DT_MOVETAB: return "MOVETAB"; 963 case DT_SYMINFO: return "SYMINFO"; 964 case DT_VERSYM: return "VERSYM"; 965 case DT_RELACOUNT: return "RELACOUNT"; 966 case DT_RELCOUNT: return "RELCOUNT"; 967 case DT_FLAGS_1: return "FLAGS_1"; 968 case DT_VERDEF: return "VERDEF"; 969 case DT_VERDEFNUM: return "VERDEFNUM"; 970 case DT_VERNEED: return "VERNEED"; 971 case DT_VERNEEDNUM: return "VERNEEDNUM"; 972 case DT_AUXILIARY: return "AUXILIARY"; 973 case DT_USED: return "USED"; 974 case DT_FILTER: return "FILTER"; 975 case DT_GNU_PRELINKED: return "GNU_PRELINKED"; 976 case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ"; 977 case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ"; 978 default: 979 snprintf(s_dtype, sizeof(s_dtype), "<unknown: %#x>", dtype); 980 return (s_dtype); 981 } 982 } 983 984 static const char * 985 st_bind(unsigned int sbind) 986 { 987 static char s_sbind[32]; 988 989 switch (sbind) { 990 case STB_LOCAL: return "LOCAL"; 991 case STB_GLOBAL: return "GLOBAL"; 992 case STB_WEAK: return "WEAK"; 993 case STB_GNU_UNIQUE: return "UNIQUE"; 994 default: 995 if (sbind >= STB_LOOS && sbind <= STB_HIOS) 996 return "OS"; 997 else if (sbind >= STB_LOPROC && sbind <= STB_HIPROC) 998 return "PROC"; 999 else 1000 snprintf(s_sbind, sizeof(s_sbind), "<unknown: %#x>", 1001 sbind); 1002 return (s_sbind); 1003 } 1004 } 1005 1006 static const char * 1007 st_type(unsigned int mach, unsigned int os, unsigned int stype) 1008 { 1009 static char s_stype[32]; 1010 1011 switch (stype) { 1012 case STT_NOTYPE: return "NOTYPE"; 1013 case STT_OBJECT: return "OBJECT"; 1014 case STT_FUNC: return "FUNC"; 1015 case STT_SECTION: return "SECTION"; 1016 case STT_FILE: return "FILE"; 1017 case STT_COMMON: return "COMMON"; 1018 case STT_TLS: return "TLS"; 1019 default: 1020 if (stype >= STT_LOOS && stype <= STT_HIOS) { 1021 if ((os == ELFOSABI_GNU || os == ELFOSABI_FREEBSD) && 1022 stype == STT_GNU_IFUNC) 1023 return "IFUNC"; 1024 snprintf(s_stype, sizeof(s_stype), "OS+%#x", 1025 stype - STT_LOOS); 1026 } else if (stype >= STT_LOPROC && stype <= STT_HIPROC) { 1027 if (mach == EM_SPARCV9 && stype == STT_SPARC_REGISTER) 1028 return "REGISTER"; 1029 snprintf(s_stype, sizeof(s_stype), "PROC+%#x", 1030 stype - STT_LOPROC); 1031 } else 1032 snprintf(s_stype, sizeof(s_stype), "<unknown: %#x>", 1033 stype); 1034 return (s_stype); 1035 } 1036 } 1037 1038 static const char * 1039 st_vis(unsigned int svis) 1040 { 1041 static char s_svis[32]; 1042 1043 switch(svis) { 1044 case STV_DEFAULT: return "DEFAULT"; 1045 case STV_INTERNAL: return "INTERNAL"; 1046 case STV_HIDDEN: return "HIDDEN"; 1047 case STV_PROTECTED: return "PROTECTED"; 1048 default: 1049 snprintf(s_svis, sizeof(s_svis), "<unknown: %#x>", svis); 1050 return (s_svis); 1051 } 1052 } 1053 1054 static const char * 1055 st_shndx(unsigned int shndx) 1056 { 1057 static char s_shndx[32]; 1058 1059 switch (shndx) { 1060 case SHN_UNDEF: return "UND"; 1061 case SHN_ABS: return "ABS"; 1062 case SHN_COMMON: return "COM"; 1063 default: 1064 if (shndx >= SHN_LOPROC && shndx <= SHN_HIPROC) 1065 return "PRC"; 1066 else if (shndx >= SHN_LOOS && shndx <= SHN_HIOS) 1067 return "OS"; 1068 else 1069 snprintf(s_shndx, sizeof(s_shndx), "%u", shndx); 1070 return (s_shndx); 1071 } 1072 } 1073 1074 static struct { 1075 const char *ln; 1076 char sn; 1077 int value; 1078 } section_flag[] = { 1079 {"WRITE", 'W', SHF_WRITE}, 1080 {"ALLOC", 'A', SHF_ALLOC}, 1081 {"EXEC", 'X', SHF_EXECINSTR}, 1082 {"MERGE", 'M', SHF_MERGE}, 1083 {"STRINGS", 'S', SHF_STRINGS}, 1084 {"INFO LINK", 'I', SHF_INFO_LINK}, 1085 {"OS NONCONF", 'O', SHF_OS_NONCONFORMING}, 1086 {"GROUP", 'G', SHF_GROUP}, 1087 {"TLS", 'T', SHF_TLS}, 1088 {"COMPRESSED", 'C', SHF_COMPRESSED}, 1089 {NULL, 0, 0} 1090 }; 1091 1092 static const char * 1093 note_type(const char *name, unsigned int et, unsigned int nt) 1094 { 1095 if ((strcmp(name, "CORE") == 0 || strcmp(name, "LINUX") == 0) && 1096 et == ET_CORE) 1097 return note_type_linux_core(nt); 1098 else if (strcmp(name, "FreeBSD") == 0) 1099 if (et == ET_CORE) 1100 return note_type_freebsd_core(nt); 1101 else 1102 return note_type_freebsd(nt); 1103 else if (strcmp(name, "GNU") == 0 && et != ET_CORE) 1104 return note_type_gnu(nt); 1105 else if (strcmp(name, "NetBSD") == 0 && et != ET_CORE) 1106 return note_type_netbsd(nt); 1107 else if (strcmp(name, "OpenBSD") == 0 && et != ET_CORE) 1108 return note_type_openbsd(nt); 1109 else if (strcmp(name, "Xen") == 0 && et != ET_CORE) 1110 return note_type_xen(nt); 1111 return note_type_unknown(nt); 1112 } 1113 1114 static const char * 1115 note_type_freebsd(unsigned int nt) 1116 { 1117 switch (nt) { 1118 case 1: return "NT_FREEBSD_ABI_TAG"; 1119 case 2: return "NT_FREEBSD_NOINIT_TAG"; 1120 case 3: return "NT_FREEBSD_ARCH_TAG"; 1121 default: return (note_type_unknown(nt)); 1122 } 1123 } 1124 1125 static const char * 1126 note_type_freebsd_core(unsigned int nt) 1127 { 1128 switch (nt) { 1129 case 1: return "NT_PRSTATUS"; 1130 case 2: return "NT_FPREGSET"; 1131 case 3: return "NT_PRPSINFO"; 1132 case 7: return "NT_THRMISC"; 1133 case 8: return "NT_PROCSTAT_PROC"; 1134 case 9: return "NT_PROCSTAT_FILES"; 1135 case 10: return "NT_PROCSTAT_VMMAP"; 1136 case 11: return "NT_PROCSTAT_GROUPS"; 1137 case 12: return "NT_PROCSTAT_UMASK"; 1138 case 13: return "NT_PROCSTAT_RLIMIT"; 1139 case 14: return "NT_PROCSTAT_OSREL"; 1140 case 15: return "NT_PROCSTAT_PSSTRINGS"; 1141 case 16: return "NT_PROCSTAT_AUXV"; 1142 case 0x202: return "NT_X86_XSTATE (x86 XSAVE extended state)"; 1143 default: return (note_type_unknown(nt)); 1144 } 1145 } 1146 1147 static const char * 1148 note_type_linux_core(unsigned int nt) 1149 { 1150 switch (nt) { 1151 case 1: return "NT_PRSTATUS (Process status)"; 1152 case 2: return "NT_FPREGSET (Floating point information)"; 1153 case 3: return "NT_PRPSINFO (Process information)"; 1154 case 4: return "NT_TASKSTRUCT (Task structure)"; 1155 case 6: return "NT_AUXV (Auxiliary vector)"; 1156 case 10: return "NT_PSTATUS (Linux process status)"; 1157 case 12: return "NT_FPREGS (Linux floating point regset)"; 1158 case 13: return "NT_PSINFO (Linux process information)"; 1159 case 16: return "NT_LWPSTATUS (Linux lwpstatus_t type)"; 1160 case 17: return "NT_LWPSINFO (Linux lwpinfo_t type)"; 1161 case 18: return "NT_WIN32PSTATUS (win32_pstatus structure)"; 1162 case 0x100: return "NT_PPC_VMX (ppc Altivec registers)"; 1163 case 0x102: return "NT_PPC_VSX (ppc VSX registers)"; 1164 case 0x202: return "NT_X86_XSTATE (x86 XSAVE extended state)"; 1165 case 0x300: return "NT_S390_HIGH_GPRS (s390 upper register halves)"; 1166 case 0x301: return "NT_S390_TIMER (s390 timer register)"; 1167 case 0x302: return "NT_S390_TODCMP (s390 TOD comparator register)"; 1168 case 0x303: return "NT_S390_TODPREG (s390 TOD programmable register)"; 1169 case 0x304: return "NT_S390_CTRS (s390 control registers)"; 1170 case 0x305: return "NT_S390_PREFIX (s390 prefix register)"; 1171 case 0x400: return "NT_ARM_VFP (arm VFP registers)"; 1172 case 0x46494c45UL: return "NT_FILE (mapped files)"; 1173 case 0x46E62B7FUL: return "NT_PRXFPREG (Linux user_xfpregs structure)"; 1174 case 0x53494749UL: return "NT_SIGINFO (siginfo_t data)"; 1175 default: return (note_type_unknown(nt)); 1176 } 1177 } 1178 1179 static const char * 1180 note_type_gnu(unsigned int nt) 1181 { 1182 switch (nt) { 1183 case 1: return "NT_GNU_ABI_TAG"; 1184 case 2: return "NT_GNU_HWCAP (Hardware capabilities)"; 1185 case 3: return "NT_GNU_BUILD_ID (Build id set by ld(1))"; 1186 case 4: return "NT_GNU_GOLD_VERSION (GNU gold version)"; 1187 default: return (note_type_unknown(nt)); 1188 } 1189 } 1190 1191 static const char * 1192 note_type_netbsd(unsigned int nt) 1193 { 1194 switch (nt) { 1195 case 1: return "NT_NETBSD_IDENT"; 1196 default: return (note_type_unknown(nt)); 1197 } 1198 } 1199 1200 static const char * 1201 note_type_openbsd(unsigned int nt) 1202 { 1203 switch (nt) { 1204 case 1: return "NT_OPENBSD_IDENT"; 1205 default: return (note_type_unknown(nt)); 1206 } 1207 } 1208 1209 static const char * 1210 note_type_unknown(unsigned int nt) 1211 { 1212 static char s_nt[32]; 1213 1214 snprintf(s_nt, sizeof(s_nt), 1215 nt >= 0x100 ? "<unknown: 0x%x>" : "<unknown: %u>", nt); 1216 return (s_nt); 1217 } 1218 1219 static const char * 1220 note_type_xen(unsigned int nt) 1221 { 1222 switch (nt) { 1223 case 0: return "XEN_ELFNOTE_INFO"; 1224 case 1: return "XEN_ELFNOTE_ENTRY"; 1225 case 2: return "XEN_ELFNOTE_HYPERCALL_PAGE"; 1226 case 3: return "XEN_ELFNOTE_VIRT_BASE"; 1227 case 4: return "XEN_ELFNOTE_PADDR_OFFSET"; 1228 case 5: return "XEN_ELFNOTE_XEN_VERSION"; 1229 case 6: return "XEN_ELFNOTE_GUEST_OS"; 1230 case 7: return "XEN_ELFNOTE_GUEST_VERSION"; 1231 case 8: return "XEN_ELFNOTE_LOADER"; 1232 case 9: return "XEN_ELFNOTE_PAE_MODE"; 1233 case 10: return "XEN_ELFNOTE_FEATURES"; 1234 case 11: return "XEN_ELFNOTE_BSD_SYMTAB"; 1235 case 12: return "XEN_ELFNOTE_HV_START_LOW"; 1236 case 13: return "XEN_ELFNOTE_L1_MFN_VALID"; 1237 case 14: return "XEN_ELFNOTE_SUSPEND_CANCEL"; 1238 case 15: return "XEN_ELFNOTE_INIT_P2M"; 1239 case 16: return "XEN_ELFNOTE_MOD_START_PFN"; 1240 case 17: return "XEN_ELFNOTE_SUPPORTED_FEATURES"; 1241 default: return (note_type_unknown(nt)); 1242 } 1243 } 1244 1245 static struct { 1246 const char *name; 1247 int value; 1248 } l_flag[] = { 1249 {"EXACT_MATCH", LL_EXACT_MATCH}, 1250 {"IGNORE_INT_VER", LL_IGNORE_INT_VER}, 1251 {"REQUIRE_MINOR", LL_REQUIRE_MINOR}, 1252 {"EXPORTS", LL_EXPORTS}, 1253 {"DELAY_LOAD", LL_DELAY_LOAD}, 1254 {"DELTA", LL_DELTA}, 1255 {NULL, 0} 1256 }; 1257 1258 static struct mips_option mips_exceptions_option[] = { 1259 {OEX_PAGE0, "PAGE0"}, 1260 {OEX_SMM, "SMM"}, 1261 {OEX_PRECISEFP, "PRECISEFP"}, 1262 {OEX_DISMISS, "DISMISS"}, 1263 {0, NULL} 1264 }; 1265 1266 static struct mips_option mips_pad_option[] = { 1267 {OPAD_PREFIX, "PREFIX"}, 1268 {OPAD_POSTFIX, "POSTFIX"}, 1269 {OPAD_SYMBOL, "SYMBOL"}, 1270 {0, NULL} 1271 }; 1272 1273 static struct mips_option mips_hwpatch_option[] = { 1274 {OHW_R4KEOP, "R4KEOP"}, 1275 {OHW_R8KPFETCH, "R8KPFETCH"}, 1276 {OHW_R5KEOP, "R5KEOP"}, 1277 {OHW_R5KCVTL, "R5KCVTL"}, 1278 {0, NULL} 1279 }; 1280 1281 static struct mips_option mips_hwa_option[] = { 1282 {OHWA0_R4KEOP_CHECKED, "R4KEOP_CHECKED"}, 1283 {OHWA0_R4KEOP_CLEAN, "R4KEOP_CLEAN"}, 1284 {0, NULL} 1285 }; 1286 1287 static struct mips_option mips_hwo_option[] = { 1288 {OHWO0_FIXADE, "FIXADE"}, 1289 {0, NULL} 1290 }; 1291 1292 static const char * 1293 option_kind(uint8_t kind) 1294 { 1295 static char s_kind[32]; 1296 1297 switch (kind) { 1298 case ODK_NULL: return "NULL"; 1299 case ODK_REGINFO: return "REGINFO"; 1300 case ODK_EXCEPTIONS: return "EXCEPTIONS"; 1301 case ODK_PAD: return "PAD"; 1302 case ODK_HWPATCH: return "HWPATCH"; 1303 case ODK_FILL: return "FILL"; 1304 case ODK_TAGS: return "TAGS"; 1305 case ODK_HWAND: return "HWAND"; 1306 case ODK_HWOR: return "HWOR"; 1307 case ODK_GP_GROUP: return "GP_GROUP"; 1308 case ODK_IDENT: return "IDENT"; 1309 default: 1310 snprintf(s_kind, sizeof(s_kind), "<unknown: %u>", kind); 1311 return (s_kind); 1312 } 1313 } 1314 1315 static const char * 1316 top_tag(unsigned int tag) 1317 { 1318 static char s_top_tag[32]; 1319 1320 switch (tag) { 1321 case 1: return "File Attributes"; 1322 case 2: return "Section Attributes"; 1323 case 3: return "Symbol Attributes"; 1324 default: 1325 snprintf(s_top_tag, sizeof(s_top_tag), "Unknown tag: %u", tag); 1326 return (s_top_tag); 1327 } 1328 } 1329 1330 static const char * 1331 aeabi_cpu_arch(uint64_t arch) 1332 { 1333 static char s_cpu_arch[32]; 1334 1335 switch (arch) { 1336 case 0: return "Pre-V4"; 1337 case 1: return "ARM v4"; 1338 case 2: return "ARM v4T"; 1339 case 3: return "ARM v5T"; 1340 case 4: return "ARM v5TE"; 1341 case 5: return "ARM v5TEJ"; 1342 case 6: return "ARM v6"; 1343 case 7: return "ARM v6KZ"; 1344 case 8: return "ARM v6T2"; 1345 case 9: return "ARM v6K"; 1346 case 10: return "ARM v7"; 1347 case 11: return "ARM v6-M"; 1348 case 12: return "ARM v6S-M"; 1349 case 13: return "ARM v7E-M"; 1350 default: 1351 snprintf(s_cpu_arch, sizeof(s_cpu_arch), 1352 "Unknown (%ju)", (uintmax_t) arch); 1353 return (s_cpu_arch); 1354 } 1355 } 1356 1357 static const char * 1358 aeabi_cpu_arch_profile(uint64_t pf) 1359 { 1360 static char s_arch_profile[32]; 1361 1362 switch (pf) { 1363 case 0: 1364 return "Not applicable"; 1365 case 0x41: /* 'A' */ 1366 return "Application Profile"; 1367 case 0x52: /* 'R' */ 1368 return "Real-Time Profile"; 1369 case 0x4D: /* 'M' */ 1370 return "Microcontroller Profile"; 1371 case 0x53: /* 'S' */ 1372 return "Application or Real-Time Profile"; 1373 default: 1374 snprintf(s_arch_profile, sizeof(s_arch_profile), 1375 "Unknown (%ju)\n", (uintmax_t) pf); 1376 return (s_arch_profile); 1377 } 1378 } 1379 1380 static const char * 1381 aeabi_arm_isa(uint64_t ai) 1382 { 1383 static char s_ai[32]; 1384 1385 switch (ai) { 1386 case 0: return "No"; 1387 case 1: return "Yes"; 1388 default: 1389 snprintf(s_ai, sizeof(s_ai), "Unknown (%ju)\n", 1390 (uintmax_t) ai); 1391 return (s_ai); 1392 } 1393 } 1394 1395 static const char * 1396 aeabi_thumb_isa(uint64_t ti) 1397 { 1398 static char s_ti[32]; 1399 1400 switch (ti) { 1401 case 0: return "No"; 1402 case 1: return "16-bit Thumb"; 1403 case 2: return "32-bit Thumb"; 1404 default: 1405 snprintf(s_ti, sizeof(s_ti), "Unknown (%ju)\n", 1406 (uintmax_t) ti); 1407 return (s_ti); 1408 } 1409 } 1410 1411 static const char * 1412 aeabi_fp_arch(uint64_t fp) 1413 { 1414 static char s_fp_arch[32]; 1415 1416 switch (fp) { 1417 case 0: return "No"; 1418 case 1: return "VFPv1"; 1419 case 2: return "VFPv2"; 1420 case 3: return "VFPv3"; 1421 case 4: return "VFPv3-D16"; 1422 case 5: return "VFPv4"; 1423 case 6: return "VFPv4-D16"; 1424 default: 1425 snprintf(s_fp_arch, sizeof(s_fp_arch), "Unknown (%ju)", 1426 (uintmax_t) fp); 1427 return (s_fp_arch); 1428 } 1429 } 1430 1431 static const char * 1432 aeabi_wmmx_arch(uint64_t wmmx) 1433 { 1434 static char s_wmmx[32]; 1435 1436 switch (wmmx) { 1437 case 0: return "No"; 1438 case 1: return "WMMXv1"; 1439 case 2: return "WMMXv2"; 1440 default: 1441 snprintf(s_wmmx, sizeof(s_wmmx), "Unknown (%ju)", 1442 (uintmax_t) wmmx); 1443 return (s_wmmx); 1444 } 1445 } 1446 1447 static const char * 1448 aeabi_adv_simd_arch(uint64_t simd) 1449 { 1450 static char s_simd[32]; 1451 1452 switch (simd) { 1453 case 0: return "No"; 1454 case 1: return "NEONv1"; 1455 case 2: return "NEONv2"; 1456 default: 1457 snprintf(s_simd, sizeof(s_simd), "Unknown (%ju)", 1458 (uintmax_t) simd); 1459 return (s_simd); 1460 } 1461 } 1462 1463 static const char * 1464 aeabi_pcs_config(uint64_t pcs) 1465 { 1466 static char s_pcs[32]; 1467 1468 switch (pcs) { 1469 case 0: return "None"; 1470 case 1: return "Bare platform"; 1471 case 2: return "Linux"; 1472 case 3: return "Linux DSO"; 1473 case 4: return "Palm OS 2004"; 1474 case 5: return "Palm OS (future)"; 1475 case 6: return "Symbian OS 2004"; 1476 case 7: return "Symbian OS (future)"; 1477 default: 1478 snprintf(s_pcs, sizeof(s_pcs), "Unknown (%ju)", 1479 (uintmax_t) pcs); 1480 return (s_pcs); 1481 } 1482 } 1483 1484 static const char * 1485 aeabi_pcs_r9(uint64_t r9) 1486 { 1487 static char s_r9[32]; 1488 1489 switch (r9) { 1490 case 0: return "V6"; 1491 case 1: return "SB"; 1492 case 2: return "TLS pointer"; 1493 case 3: return "Unused"; 1494 default: 1495 snprintf(s_r9, sizeof(s_r9), "Unknown (%ju)", (uintmax_t) r9); 1496 return (s_r9); 1497 } 1498 } 1499 1500 static const char * 1501 aeabi_pcs_rw(uint64_t rw) 1502 { 1503 static char s_rw[32]; 1504 1505 switch (rw) { 1506 case 0: return "Absolute"; 1507 case 1: return "PC-relative"; 1508 case 2: return "SB-relative"; 1509 case 3: return "None"; 1510 default: 1511 snprintf(s_rw, sizeof(s_rw), "Unknown (%ju)", (uintmax_t) rw); 1512 return (s_rw); 1513 } 1514 } 1515 1516 static const char * 1517 aeabi_pcs_ro(uint64_t ro) 1518 { 1519 static char s_ro[32]; 1520 1521 switch (ro) { 1522 case 0: return "Absolute"; 1523 case 1: return "PC-relative"; 1524 case 2: return "None"; 1525 default: 1526 snprintf(s_ro, sizeof(s_ro), "Unknown (%ju)", (uintmax_t) ro); 1527 return (s_ro); 1528 } 1529 } 1530 1531 static const char * 1532 aeabi_pcs_got(uint64_t got) 1533 { 1534 static char s_got[32]; 1535 1536 switch (got) { 1537 case 0: return "None"; 1538 case 1: return "direct"; 1539 case 2: return "indirect via GOT"; 1540 default: 1541 snprintf(s_got, sizeof(s_got), "Unknown (%ju)", 1542 (uintmax_t) got); 1543 return (s_got); 1544 } 1545 } 1546 1547 static const char * 1548 aeabi_pcs_wchar_t(uint64_t wt) 1549 { 1550 static char s_wt[32]; 1551 1552 switch (wt) { 1553 case 0: return "None"; 1554 case 2: return "wchar_t size 2"; 1555 case 4: return "wchar_t size 4"; 1556 default: 1557 snprintf(s_wt, sizeof(s_wt), "Unknown (%ju)", (uintmax_t) wt); 1558 return (s_wt); 1559 } 1560 } 1561 1562 static const char * 1563 aeabi_enum_size(uint64_t es) 1564 { 1565 static char s_es[32]; 1566 1567 switch (es) { 1568 case 0: return "None"; 1569 case 1: return "smallest"; 1570 case 2: return "32-bit"; 1571 case 3: return "visible 32-bit"; 1572 default: 1573 snprintf(s_es, sizeof(s_es), "Unknown (%ju)", (uintmax_t) es); 1574 return (s_es); 1575 } 1576 } 1577 1578 static const char * 1579 aeabi_align_needed(uint64_t an) 1580 { 1581 static char s_align_n[64]; 1582 1583 switch (an) { 1584 case 0: return "No"; 1585 case 1: return "8-byte align"; 1586 case 2: return "4-byte align"; 1587 case 3: return "Reserved"; 1588 default: 1589 if (an >= 4 && an <= 12) 1590 snprintf(s_align_n, sizeof(s_align_n), "8-byte align" 1591 " and up to 2^%ju-byte extended align", 1592 (uintmax_t) an); 1593 else 1594 snprintf(s_align_n, sizeof(s_align_n), "Unknown (%ju)", 1595 (uintmax_t) an); 1596 return (s_align_n); 1597 } 1598 } 1599 1600 static const char * 1601 aeabi_align_preserved(uint64_t ap) 1602 { 1603 static char s_align_p[128]; 1604 1605 switch (ap) { 1606 case 0: return "No"; 1607 case 1: return "8-byte align"; 1608 case 2: return "8-byte align and SP % 8 == 0"; 1609 case 3: return "Reserved"; 1610 default: 1611 if (ap >= 4 && ap <= 12) 1612 snprintf(s_align_p, sizeof(s_align_p), "8-byte align" 1613 " and SP %% 8 == 0 and up to 2^%ju-byte extended" 1614 " align", (uintmax_t) ap); 1615 else 1616 snprintf(s_align_p, sizeof(s_align_p), "Unknown (%ju)", 1617 (uintmax_t) ap); 1618 return (s_align_p); 1619 } 1620 } 1621 1622 static const char * 1623 aeabi_fp_rounding(uint64_t fr) 1624 { 1625 static char s_fp_r[32]; 1626 1627 switch (fr) { 1628 case 0: return "Unused"; 1629 case 1: return "Needed"; 1630 default: 1631 snprintf(s_fp_r, sizeof(s_fp_r), "Unknown (%ju)", 1632 (uintmax_t) fr); 1633 return (s_fp_r); 1634 } 1635 } 1636 1637 static const char * 1638 aeabi_fp_denormal(uint64_t fd) 1639 { 1640 static char s_fp_d[32]; 1641 1642 switch (fd) { 1643 case 0: return "Unused"; 1644 case 1: return "Needed"; 1645 case 2: return "Sign Only"; 1646 default: 1647 snprintf(s_fp_d, sizeof(s_fp_d), "Unknown (%ju)", 1648 (uintmax_t) fd); 1649 return (s_fp_d); 1650 } 1651 } 1652 1653 static const char * 1654 aeabi_fp_exceptions(uint64_t fe) 1655 { 1656 static char s_fp_e[32]; 1657 1658 switch (fe) { 1659 case 0: return "Unused"; 1660 case 1: return "Needed"; 1661 default: 1662 snprintf(s_fp_e, sizeof(s_fp_e), "Unknown (%ju)", 1663 (uintmax_t) fe); 1664 return (s_fp_e); 1665 } 1666 } 1667 1668 static const char * 1669 aeabi_fp_user_exceptions(uint64_t fu) 1670 { 1671 static char s_fp_u[32]; 1672 1673 switch (fu) { 1674 case 0: return "Unused"; 1675 case 1: return "Needed"; 1676 default: 1677 snprintf(s_fp_u, sizeof(s_fp_u), "Unknown (%ju)", 1678 (uintmax_t) fu); 1679 return (s_fp_u); 1680 } 1681 } 1682 1683 static const char * 1684 aeabi_fp_number_model(uint64_t fn) 1685 { 1686 static char s_fp_n[32]; 1687 1688 switch (fn) { 1689 case 0: return "Unused"; 1690 case 1: return "IEEE 754 normal"; 1691 case 2: return "RTABI"; 1692 case 3: return "IEEE 754"; 1693 default: 1694 snprintf(s_fp_n, sizeof(s_fp_n), "Unknown (%ju)", 1695 (uintmax_t) fn); 1696 return (s_fp_n); 1697 } 1698 } 1699 1700 static const char * 1701 aeabi_fp_16bit_format(uint64_t fp16) 1702 { 1703 static char s_fp_16[64]; 1704 1705 switch (fp16) { 1706 case 0: return "None"; 1707 case 1: return "IEEE 754"; 1708 case 2: return "VFPv3/Advanced SIMD (alternative format)"; 1709 default: 1710 snprintf(s_fp_16, sizeof(s_fp_16), "Unknown (%ju)", 1711 (uintmax_t) fp16); 1712 return (s_fp_16); 1713 } 1714 } 1715 1716 static const char * 1717 aeabi_mpext(uint64_t mp) 1718 { 1719 static char s_mp[32]; 1720 1721 switch (mp) { 1722 case 0: return "Not allowed"; 1723 case 1: return "Allowed"; 1724 default: 1725 snprintf(s_mp, sizeof(s_mp), "Unknown (%ju)", 1726 (uintmax_t) mp); 1727 return (s_mp); 1728 } 1729 } 1730 1731 static const char * 1732 aeabi_div(uint64_t du) 1733 { 1734 static char s_du[32]; 1735 1736 switch (du) { 1737 case 0: return "Yes (V7-R/V7-M)"; 1738 case 1: return "No"; 1739 case 2: return "Yes (V7-A)"; 1740 default: 1741 snprintf(s_du, sizeof(s_du), "Unknown (%ju)", 1742 (uintmax_t) du); 1743 return (s_du); 1744 } 1745 } 1746 1747 static const char * 1748 aeabi_t2ee(uint64_t t2ee) 1749 { 1750 static char s_t2ee[32]; 1751 1752 switch (t2ee) { 1753 case 0: return "Not allowed"; 1754 case 1: return "Allowed"; 1755 default: 1756 snprintf(s_t2ee, sizeof(s_t2ee), "Unknown(%ju)", 1757 (uintmax_t) t2ee); 1758 return (s_t2ee); 1759 } 1760 1761 } 1762 1763 static const char * 1764 aeabi_hardfp(uint64_t hfp) 1765 { 1766 static char s_hfp[32]; 1767 1768 switch (hfp) { 1769 case 0: return "Tag_FP_arch"; 1770 case 1: return "only SP"; 1771 case 2: return "only DP"; 1772 case 3: return "both SP and DP"; 1773 default: 1774 snprintf(s_hfp, sizeof(s_hfp), "Unknown (%ju)", 1775 (uintmax_t) hfp); 1776 return (s_hfp); 1777 } 1778 } 1779 1780 static const char * 1781 aeabi_vfp_args(uint64_t va) 1782 { 1783 static char s_va[32]; 1784 1785 switch (va) { 1786 case 0: return "AAPCS (base variant)"; 1787 case 1: return "AAPCS (VFP variant)"; 1788 case 2: return "toolchain-specific"; 1789 default: 1790 snprintf(s_va, sizeof(s_va), "Unknown (%ju)", (uintmax_t) va); 1791 return (s_va); 1792 } 1793 } 1794 1795 static const char * 1796 aeabi_wmmx_args(uint64_t wa) 1797 { 1798 static char s_wa[32]; 1799 1800 switch (wa) { 1801 case 0: return "AAPCS (base variant)"; 1802 case 1: return "Intel WMMX"; 1803 case 2: return "toolchain-specific"; 1804 default: 1805 snprintf(s_wa, sizeof(s_wa), "Unknown(%ju)", (uintmax_t) wa); 1806 return (s_wa); 1807 } 1808 } 1809 1810 static const char * 1811 aeabi_unaligned_access(uint64_t ua) 1812 { 1813 static char s_ua[32]; 1814 1815 switch (ua) { 1816 case 0: return "Not allowed"; 1817 case 1: return "Allowed"; 1818 default: 1819 snprintf(s_ua, sizeof(s_ua), "Unknown(%ju)", (uintmax_t) ua); 1820 return (s_ua); 1821 } 1822 } 1823 1824 static const char * 1825 aeabi_fp_hpext(uint64_t fh) 1826 { 1827 static char s_fh[32]; 1828 1829 switch (fh) { 1830 case 0: return "Not allowed"; 1831 case 1: return "Allowed"; 1832 default: 1833 snprintf(s_fh, sizeof(s_fh), "Unknown(%ju)", (uintmax_t) fh); 1834 return (s_fh); 1835 } 1836 } 1837 1838 static const char * 1839 aeabi_optm_goal(uint64_t og) 1840 { 1841 static char s_og[32]; 1842 1843 switch (og) { 1844 case 0: return "None"; 1845 case 1: return "Speed"; 1846 case 2: return "Speed aggressive"; 1847 case 3: return "Space"; 1848 case 4: return "Space aggressive"; 1849 case 5: return "Debugging"; 1850 case 6: return "Best Debugging"; 1851 default: 1852 snprintf(s_og, sizeof(s_og), "Unknown(%ju)", (uintmax_t) og); 1853 return (s_og); 1854 } 1855 } 1856 1857 static const char * 1858 aeabi_fp_optm_goal(uint64_t fog) 1859 { 1860 static char s_fog[32]; 1861 1862 switch (fog) { 1863 case 0: return "None"; 1864 case 1: return "Speed"; 1865 case 2: return "Speed aggressive"; 1866 case 3: return "Space"; 1867 case 4: return "Space aggressive"; 1868 case 5: return "Accurary"; 1869 case 6: return "Best Accurary"; 1870 default: 1871 snprintf(s_fog, sizeof(s_fog), "Unknown(%ju)", 1872 (uintmax_t) fog); 1873 return (s_fog); 1874 } 1875 } 1876 1877 static const char * 1878 aeabi_virtual(uint64_t vt) 1879 { 1880 static char s_virtual[64]; 1881 1882 switch (vt) { 1883 case 0: return "No"; 1884 case 1: return "TrustZone"; 1885 case 2: return "Virtualization extension"; 1886 case 3: return "TrustZone and virtualization extension"; 1887 default: 1888 snprintf(s_virtual, sizeof(s_virtual), "Unknown(%ju)", 1889 (uintmax_t) vt); 1890 return (s_virtual); 1891 } 1892 } 1893 1894 static struct { 1895 uint64_t tag; 1896 const char *s_tag; 1897 const char *(*get_desc)(uint64_t val); 1898 } aeabi_tags[] = { 1899 {4, "Tag_CPU_raw_name", NULL}, 1900 {5, "Tag_CPU_name", NULL}, 1901 {6, "Tag_CPU_arch", aeabi_cpu_arch}, 1902 {7, "Tag_CPU_arch_profile", aeabi_cpu_arch_profile}, 1903 {8, "Tag_ARM_ISA_use", aeabi_arm_isa}, 1904 {9, "Tag_THUMB_ISA_use", aeabi_thumb_isa}, 1905 {10, "Tag_FP_arch", aeabi_fp_arch}, 1906 {11, "Tag_WMMX_arch", aeabi_wmmx_arch}, 1907 {12, "Tag_Advanced_SIMD_arch", aeabi_adv_simd_arch}, 1908 {13, "Tag_PCS_config", aeabi_pcs_config}, 1909 {14, "Tag_ABI_PCS_R9_use", aeabi_pcs_r9}, 1910 {15, "Tag_ABI_PCS_RW_data", aeabi_pcs_rw}, 1911 {16, "Tag_ABI_PCS_RO_data", aeabi_pcs_ro}, 1912 {17, "Tag_ABI_PCS_GOT_use", aeabi_pcs_got}, 1913 {18, "Tag_ABI_PCS_wchar_t", aeabi_pcs_wchar_t}, 1914 {19, "Tag_ABI_FP_rounding", aeabi_fp_rounding}, 1915 {20, "Tag_ABI_FP_denormal", aeabi_fp_denormal}, 1916 {21, "Tag_ABI_FP_exceptions", aeabi_fp_exceptions}, 1917 {22, "Tag_ABI_FP_user_exceptions", aeabi_fp_user_exceptions}, 1918 {23, "Tag_ABI_FP_number_model", aeabi_fp_number_model}, 1919 {24, "Tag_ABI_align_needed", aeabi_align_needed}, 1920 {25, "Tag_ABI_align_preserved", aeabi_align_preserved}, 1921 {26, "Tag_ABI_enum_size", aeabi_enum_size}, 1922 {27, "Tag_ABI_HardFP_use", aeabi_hardfp}, 1923 {28, "Tag_ABI_VFP_args", aeabi_vfp_args}, 1924 {29, "Tag_ABI_WMMX_args", aeabi_wmmx_args}, 1925 {30, "Tag_ABI_optimization_goals", aeabi_optm_goal}, 1926 {31, "Tag_ABI_FP_optimization_goals", aeabi_fp_optm_goal}, 1927 {32, "Tag_compatibility", NULL}, 1928 {34, "Tag_CPU_unaligned_access", aeabi_unaligned_access}, 1929 {36, "Tag_FP_HP_extension", aeabi_fp_hpext}, 1930 {38, "Tag_ABI_FP_16bit_format", aeabi_fp_16bit_format}, 1931 {42, "Tag_MPextension_use", aeabi_mpext}, 1932 {44, "Tag_DIV_use", aeabi_div}, 1933 {64, "Tag_nodefaults", NULL}, 1934 {65, "Tag_also_compatible_with", NULL}, 1935 {66, "Tag_T2EE_use", aeabi_t2ee}, 1936 {67, "Tag_conformance", NULL}, 1937 {68, "Tag_Virtualization_use", aeabi_virtual}, 1938 {70, "Tag_MPextension_use", aeabi_mpext}, 1939 }; 1940 1941 static const char * 1942 mips_abi_fp(uint64_t fp) 1943 { 1944 static char s_mips_abi_fp[64]; 1945 1946 switch (fp) { 1947 case 0: return "N/A"; 1948 case 1: return "Hard float (double precision)"; 1949 case 2: return "Hard float (single precision)"; 1950 case 3: return "Soft float"; 1951 case 4: return "64-bit float (-mips32r2 -mfp64)"; 1952 default: 1953 snprintf(s_mips_abi_fp, sizeof(s_mips_abi_fp), "Unknown(%ju)", 1954 (uintmax_t) fp); 1955 return (s_mips_abi_fp); 1956 } 1957 } 1958 1959 static const char * 1960 ppc_abi_fp(uint64_t fp) 1961 { 1962 static char s_ppc_abi_fp[64]; 1963 1964 switch (fp) { 1965 case 0: return "N/A"; 1966 case 1: return "Hard float (double precision)"; 1967 case 2: return "Soft float"; 1968 case 3: return "Hard float (single precision)"; 1969 default: 1970 snprintf(s_ppc_abi_fp, sizeof(s_ppc_abi_fp), "Unknown(%ju)", 1971 (uintmax_t) fp); 1972 return (s_ppc_abi_fp); 1973 } 1974 } 1975 1976 static const char * 1977 ppc_abi_vector(uint64_t vec) 1978 { 1979 static char s_vec[64]; 1980 1981 switch (vec) { 1982 case 0: return "N/A"; 1983 case 1: return "Generic purpose registers"; 1984 case 2: return "AltiVec registers"; 1985 case 3: return "SPE registers"; 1986 default: 1987 snprintf(s_vec, sizeof(s_vec), "Unknown(%ju)", (uintmax_t) vec); 1988 return (s_vec); 1989 } 1990 } 1991 1992 static const char * 1993 dwarf_reg(unsigned int mach, unsigned int reg) 1994 { 1995 1996 switch (mach) { 1997 case EM_386: 1998 case EM_IAMCU: 1999 switch (reg) { 2000 case 0: return "eax"; 2001 case 1: return "ecx"; 2002 case 2: return "edx"; 2003 case 3: return "ebx"; 2004 case 4: return "esp"; 2005 case 5: return "ebp"; 2006 case 6: return "esi"; 2007 case 7: return "edi"; 2008 case 8: return "eip"; 2009 case 9: return "eflags"; 2010 case 11: return "st0"; 2011 case 12: return "st1"; 2012 case 13: return "st2"; 2013 case 14: return "st3"; 2014 case 15: return "st4"; 2015 case 16: return "st5"; 2016 case 17: return "st6"; 2017 case 18: return "st7"; 2018 case 21: return "xmm0"; 2019 case 22: return "xmm1"; 2020 case 23: return "xmm2"; 2021 case 24: return "xmm3"; 2022 case 25: return "xmm4"; 2023 case 26: return "xmm5"; 2024 case 27: return "xmm6"; 2025 case 28: return "xmm7"; 2026 case 29: return "mm0"; 2027 case 30: return "mm1"; 2028 case 31: return "mm2"; 2029 case 32: return "mm3"; 2030 case 33: return "mm4"; 2031 case 34: return "mm5"; 2032 case 35: return "mm6"; 2033 case 36: return "mm7"; 2034 case 37: return "fcw"; 2035 case 38: return "fsw"; 2036 case 39: return "mxcsr"; 2037 case 40: return "es"; 2038 case 41: return "cs"; 2039 case 42: return "ss"; 2040 case 43: return "ds"; 2041 case 44: return "fs"; 2042 case 45: return "gs"; 2043 case 48: return "tr"; 2044 case 49: return "ldtr"; 2045 default: return (NULL); 2046 } 2047 case EM_X86_64: 2048 switch (reg) { 2049 case 0: return "rax"; 2050 case 1: return "rdx"; 2051 case 2: return "rcx"; 2052 case 3: return "rbx"; 2053 case 4: return "rsi"; 2054 case 5: return "rdi"; 2055 case 6: return "rbp"; 2056 case 7: return "rsp"; 2057 case 16: return "rip"; 2058 case 17: return "xmm0"; 2059 case 18: return "xmm1"; 2060 case 19: return "xmm2"; 2061 case 20: return "xmm3"; 2062 case 21: return "xmm4"; 2063 case 22: return "xmm5"; 2064 case 23: return "xmm6"; 2065 case 24: return "xmm7"; 2066 case 25: return "xmm8"; 2067 case 26: return "xmm9"; 2068 case 27: return "xmm10"; 2069 case 28: return "xmm11"; 2070 case 29: return "xmm12"; 2071 case 30: return "xmm13"; 2072 case 31: return "xmm14"; 2073 case 32: return "xmm15"; 2074 case 33: return "st0"; 2075 case 34: return "st1"; 2076 case 35: return "st2"; 2077 case 36: return "st3"; 2078 case 37: return "st4"; 2079 case 38: return "st5"; 2080 case 39: return "st6"; 2081 case 40: return "st7"; 2082 case 41: return "mm0"; 2083 case 42: return "mm1"; 2084 case 43: return "mm2"; 2085 case 44: return "mm3"; 2086 case 45: return "mm4"; 2087 case 46: return "mm5"; 2088 case 47: return "mm6"; 2089 case 48: return "mm7"; 2090 case 49: return "rflags"; 2091 case 50: return "es"; 2092 case 51: return "cs"; 2093 case 52: return "ss"; 2094 case 53: return "ds"; 2095 case 54: return "fs"; 2096 case 55: return "gs"; 2097 case 58: return "fs.base"; 2098 case 59: return "gs.base"; 2099 case 62: return "tr"; 2100 case 63: return "ldtr"; 2101 case 64: return "mxcsr"; 2102 case 65: return "fcw"; 2103 case 66: return "fsw"; 2104 default: return (NULL); 2105 } 2106 default: 2107 return (NULL); 2108 } 2109 } 2110 2111 static void 2112 dump_ehdr(struct readelf *re) 2113 { 2114 size_t shnum, shstrndx; 2115 int i; 2116 2117 printf("ELF Header:\n"); 2118 2119 /* e_ident[]. */ 2120 printf(" Magic: "); 2121 for (i = 0; i < EI_NIDENT; i++) 2122 printf("%.2x ", re->ehdr.e_ident[i]); 2123 putchar('\n'); 2124 2125 /* EI_CLASS. */ 2126 printf("%-37s%s\n", " Class:", elf_class(re->ehdr.e_ident[EI_CLASS])); 2127 2128 /* EI_DATA. */ 2129 printf("%-37s%s\n", " Data:", elf_endian(re->ehdr.e_ident[EI_DATA])); 2130 2131 /* EI_VERSION. */ 2132 printf("%-37s%d %s\n", " Version:", re->ehdr.e_ident[EI_VERSION], 2133 elf_ver(re->ehdr.e_ident[EI_VERSION])); 2134 2135 /* EI_OSABI. */ 2136 printf("%-37s%s\n", " OS/ABI:", elf_osabi(re->ehdr.e_ident[EI_OSABI])); 2137 2138 /* EI_ABIVERSION. */ 2139 printf("%-37s%d\n", " ABI Version:", re->ehdr.e_ident[EI_ABIVERSION]); 2140 2141 /* e_type. */ 2142 printf("%-37s%s\n", " Type:", elf_type(re->ehdr.e_type)); 2143 2144 /* e_machine. */ 2145 printf("%-37s%s\n", " Machine:", elf_machine(re->ehdr.e_machine)); 2146 2147 /* e_version. */ 2148 printf("%-37s%#x\n", " Version:", re->ehdr.e_version); 2149 2150 /* e_entry. */ 2151 printf("%-37s%#jx\n", " Entry point address:", 2152 (uintmax_t)re->ehdr.e_entry); 2153 2154 /* e_phoff. */ 2155 printf("%-37s%ju (bytes into file)\n", " Start of program headers:", 2156 (uintmax_t)re->ehdr.e_phoff); 2157 2158 /* e_shoff. */ 2159 printf("%-37s%ju (bytes into file)\n", " Start of section headers:", 2160 (uintmax_t)re->ehdr.e_shoff); 2161 2162 /* e_flags. */ 2163 printf("%-37s%#x", " Flags:", re->ehdr.e_flags); 2164 dump_eflags(re, re->ehdr.e_flags); 2165 putchar('\n'); 2166 2167 /* e_ehsize. */ 2168 printf("%-37s%u (bytes)\n", " Size of this header:", 2169 re->ehdr.e_ehsize); 2170 2171 /* e_phentsize. */ 2172 printf("%-37s%u (bytes)\n", " Size of program headers:", 2173 re->ehdr.e_phentsize); 2174 2175 /* e_phnum. */ 2176 printf("%-37s%u\n", " Number of program headers:", re->ehdr.e_phnum); 2177 2178 /* e_shentsize. */ 2179 printf("%-37s%u (bytes)\n", " Size of section headers:", 2180 re->ehdr.e_shentsize); 2181 2182 /* e_shnum. */ 2183 printf("%-37s%u", " Number of section headers:", re->ehdr.e_shnum); 2184 if (re->ehdr.e_shnum == SHN_UNDEF) { 2185 /* Extended section numbering is in use. */ 2186 if (elf_getshnum(re->elf, &shnum)) 2187 printf(" (%ju)", (uintmax_t)shnum); 2188 } 2189 putchar('\n'); 2190 2191 /* e_shstrndx. */ 2192 printf("%-37s%u", " Section header string table index:", 2193 re->ehdr.e_shstrndx); 2194 if (re->ehdr.e_shstrndx == SHN_XINDEX) { 2195 /* Extended section numbering is in use. */ 2196 if (elf_getshstrndx(re->elf, &shstrndx)) 2197 printf(" (%ju)", (uintmax_t)shstrndx); 2198 } 2199 putchar('\n'); 2200 } 2201 2202 static void 2203 dump_eflags(struct readelf *re, uint64_t e_flags) 2204 { 2205 struct eflags_desc *edesc; 2206 int arm_eabi; 2207 2208 edesc = NULL; 2209 switch (re->ehdr.e_machine) { 2210 case EM_ARM: 2211 arm_eabi = (e_flags & EF_ARM_EABIMASK) >> 24; 2212 if (arm_eabi == 0) 2213 printf(", GNU EABI"); 2214 else if (arm_eabi <= 5) 2215 printf(", Version%d EABI", arm_eabi); 2216 edesc = arm_eflags_desc; 2217 break; 2218 case EM_MIPS: 2219 case EM_MIPS_RS3_LE: 2220 switch ((e_flags & EF_MIPS_ARCH) >> 28) { 2221 case 0: printf(", mips1"); break; 2222 case 1: printf(", mips2"); break; 2223 case 2: printf(", mips3"); break; 2224 case 3: printf(", mips4"); break; 2225 case 4: printf(", mips5"); break; 2226 case 5: printf(", mips32"); break; 2227 case 6: printf(", mips64"); break; 2228 case 7: printf(", mips32r2"); break; 2229 case 8: printf(", mips64r2"); break; 2230 default: break; 2231 } 2232 switch ((e_flags & 0x00FF0000) >> 16) { 2233 case 0x81: printf(", 3900"); break; 2234 case 0x82: printf(", 4010"); break; 2235 case 0x83: printf(", 4100"); break; 2236 case 0x85: printf(", 4650"); break; 2237 case 0x87: printf(", 4120"); break; 2238 case 0x88: printf(", 4111"); break; 2239 case 0x8a: printf(", sb1"); break; 2240 case 0x8b: printf(", octeon"); break; 2241 case 0x8c: printf(", xlr"); break; 2242 case 0x91: printf(", 5400"); break; 2243 case 0x98: printf(", 5500"); break; 2244 case 0x99: printf(", 9000"); break; 2245 case 0xa0: printf(", loongson-2e"); break; 2246 case 0xa1: printf(", loongson-2f"); break; 2247 default: break; 2248 } 2249 switch ((e_flags & 0x0000F000) >> 12) { 2250 case 1: printf(", o32"); break; 2251 case 2: printf(", o64"); break; 2252 case 3: printf(", eabi32"); break; 2253 case 4: printf(", eabi64"); break; 2254 default: break; 2255 } 2256 edesc = mips_eflags_desc; 2257 break; 2258 case EM_PPC: 2259 case EM_PPC64: 2260 edesc = powerpc_eflags_desc; 2261 break; 2262 case EM_SPARC: 2263 case EM_SPARC32PLUS: 2264 case EM_SPARCV9: 2265 switch ((e_flags & EF_SPARCV9_MM)) { 2266 case EF_SPARCV9_TSO: printf(", tso"); break; 2267 case EF_SPARCV9_PSO: printf(", pso"); break; 2268 case EF_SPARCV9_MM: printf(", rmo"); break; 2269 default: break; 2270 } 2271 edesc = sparc_eflags_desc; 2272 break; 2273 default: 2274 break; 2275 } 2276 2277 if (edesc != NULL) { 2278 while (edesc->desc != NULL) { 2279 if (e_flags & edesc->flag) 2280 printf(", %s", edesc->desc); 2281 edesc++; 2282 } 2283 } 2284 } 2285 2286 static void 2287 dump_phdr(struct readelf *re) 2288 { 2289 const char *rawfile; 2290 GElf_Phdr phdr; 2291 size_t phnum, size; 2292 int i, j; 2293 2294 #define PH_HDR "Type", "Offset", "VirtAddr", "PhysAddr", "FileSiz", \ 2295 "MemSiz", "Flg", "Align" 2296 #define PH_CT phdr_type(re->ehdr.e_machine, phdr.p_type), \ 2297 (uintmax_t)phdr.p_offset, (uintmax_t)phdr.p_vaddr, \ 2298 (uintmax_t)phdr.p_paddr, (uintmax_t)phdr.p_filesz, \ 2299 (uintmax_t)phdr.p_memsz, \ 2300 phdr.p_flags & PF_R ? 'R' : ' ', \ 2301 phdr.p_flags & PF_W ? 'W' : ' ', \ 2302 phdr.p_flags & PF_X ? 'E' : ' ', \ 2303 (uintmax_t)phdr.p_align 2304 2305 if (elf_getphnum(re->elf, &phnum) == 0) { 2306 warnx("elf_getphnum failed: %s", elf_errmsg(-1)); 2307 return; 2308 } 2309 if (phnum == 0) { 2310 printf("\nThere are no program headers in this file.\n"); 2311 return; 2312 } 2313 2314 printf("\nElf file type is %s", elf_type(re->ehdr.e_type)); 2315 printf("\nEntry point 0x%jx\n", (uintmax_t)re->ehdr.e_entry); 2316 printf("There are %ju program headers, starting at offset %ju\n", 2317 (uintmax_t)phnum, (uintmax_t)re->ehdr.e_phoff); 2318 2319 /* Dump program headers. */ 2320 printf("\nProgram Headers:\n"); 2321 if (re->ec == ELFCLASS32) 2322 printf(" %-15s%-9s%-11s%-11s%-8s%-8s%-4s%s\n", PH_HDR); 2323 else if (re->options & RE_WW) 2324 printf(" %-15s%-9s%-19s%-19s%-9s%-9s%-4s%s\n", PH_HDR); 2325 else 2326 printf(" %-15s%-19s%-19s%s\n %-19s%-20s" 2327 "%-7s%s\n", PH_HDR); 2328 for (i = 0; (size_t) i < phnum; i++) { 2329 if (gelf_getphdr(re->elf, i, &phdr) != &phdr) { 2330 warnx("gelf_getphdr failed: %s", elf_errmsg(-1)); 2331 continue; 2332 } 2333 /* TODO: Add arch-specific segment type dump. */ 2334 if (re->ec == ELFCLASS32) 2335 printf(" %-14.14s 0x%6.6jx 0x%8.8jx 0x%8.8jx " 2336 "0x%5.5jx 0x%5.5jx %c%c%c %#jx\n", PH_CT); 2337 else if (re->options & RE_WW) 2338 printf(" %-14.14s 0x%6.6jx 0x%16.16jx 0x%16.16jx " 2339 "0x%6.6jx 0x%6.6jx %c%c%c %#jx\n", PH_CT); 2340 else 2341 printf(" %-14.14s 0x%16.16jx 0x%16.16jx 0x%16.16jx\n" 2342 " 0x%16.16jx 0x%16.16jx %c%c%c" 2343 " %#jx\n", PH_CT); 2344 if (phdr.p_type == PT_INTERP) { 2345 if ((rawfile = elf_rawfile(re->elf, &size)) == NULL) { 2346 warnx("elf_rawfile failed: %s", elf_errmsg(-1)); 2347 continue; 2348 } 2349 if (phdr.p_offset >= size) { 2350 warnx("invalid program header offset"); 2351 continue; 2352 } 2353 printf(" [Requesting program interpreter: %s]\n", 2354 rawfile + phdr.p_offset); 2355 } 2356 } 2357 2358 /* Dump section to segment mapping. */ 2359 if (re->shnum == 0) 2360 return; 2361 printf("\n Section to Segment mapping:\n"); 2362 printf(" Segment Sections...\n"); 2363 for (i = 0; (size_t)i < phnum; i++) { 2364 if (gelf_getphdr(re->elf, i, &phdr) != &phdr) { 2365 warnx("gelf_getphdr failed: %s", elf_errmsg(-1)); 2366 continue; 2367 } 2368 printf(" %2.2d ", i); 2369 /* skip NULL section. */ 2370 for (j = 1; (size_t)j < re->shnum; j++) 2371 if (re->sl[j].addr >= phdr.p_vaddr && 2372 re->sl[j].addr + re->sl[j].sz <= 2373 phdr.p_vaddr + phdr.p_memsz) 2374 printf("%s ", re->sl[j].name); 2375 printf("\n"); 2376 } 2377 #undef PH_HDR 2378 #undef PH_CT 2379 } 2380 2381 static char * 2382 section_flags(struct readelf *re, struct section *s) 2383 { 2384 #define BUF_SZ 256 2385 static char buf[BUF_SZ]; 2386 int i, p, nb; 2387 2388 p = 0; 2389 nb = re->ec == ELFCLASS32 ? 8 : 16; 2390 if (re->options & RE_T) { 2391 snprintf(buf, BUF_SZ, "[%*.*jx]: ", nb, nb, 2392 (uintmax_t)s->flags); 2393 p += nb + 4; 2394 } 2395 for (i = 0; section_flag[i].ln != NULL; i++) { 2396 if ((s->flags & section_flag[i].value) == 0) 2397 continue; 2398 if (re->options & RE_T) { 2399 snprintf(&buf[p], BUF_SZ - p, "%s, ", 2400 section_flag[i].ln); 2401 p += strlen(section_flag[i].ln) + 2; 2402 } else 2403 buf[p++] = section_flag[i].sn; 2404 } 2405 if (re->options & RE_T && p > nb + 4) 2406 p -= 2; 2407 buf[p] = '\0'; 2408 2409 return (buf); 2410 } 2411 2412 static void 2413 dump_shdr(struct readelf *re) 2414 { 2415 struct section *s; 2416 int i; 2417 2418 #define S_HDR "[Nr] Name", "Type", "Addr", "Off", "Size", "ES", \ 2419 "Flg", "Lk", "Inf", "Al" 2420 #define S_HDRL "[Nr] Name", "Type", "Address", "Offset", "Size", \ 2421 "EntSize", "Flags", "Link", "Info", "Align" 2422 #define ST_HDR "[Nr] Name", "Type", "Addr", "Off", "Size", "ES", \ 2423 "Lk", "Inf", "Al", "Flags" 2424 #define ST_HDRL "[Nr] Name", "Type", "Address", "Offset", "Link", \ 2425 "Size", "EntSize", "Info", "Align", "Flags" 2426 #define S_CT i, s->name, section_type(re->ehdr.e_machine, s->type), \ 2427 (uintmax_t)s->addr, (uintmax_t)s->off, (uintmax_t)s->sz,\ 2428 (uintmax_t)s->entsize, section_flags(re, s), \ 2429 s->link, s->info, (uintmax_t)s->align 2430 #define ST_CT i, s->name, section_type(re->ehdr.e_machine, s->type), \ 2431 (uintmax_t)s->addr, (uintmax_t)s->off, (uintmax_t)s->sz,\ 2432 (uintmax_t)s->entsize, s->link, s->info, \ 2433 (uintmax_t)s->align, section_flags(re, s) 2434 #define ST_CTL i, s->name, section_type(re->ehdr.e_machine, s->type), \ 2435 (uintmax_t)s->addr, (uintmax_t)s->off, s->link, \ 2436 (uintmax_t)s->sz, (uintmax_t)s->entsize, s->info, \ 2437 (uintmax_t)s->align, section_flags(re, s) 2438 2439 if (re->shnum == 0) { 2440 printf("\nThere are no sections in this file.\n"); 2441 return; 2442 } 2443 printf("There are %ju section headers, starting at offset 0x%jx:\n", 2444 (uintmax_t)re->shnum, (uintmax_t)re->ehdr.e_shoff); 2445 printf("\nSection Headers:\n"); 2446 if (re->ec == ELFCLASS32) { 2447 if (re->options & RE_T) 2448 printf(" %s\n %-16s%-9s%-7s%-7s%-5s%-3s%-4s%s\n" 2449 "%12s\n", ST_HDR); 2450 else 2451 printf(" %-23s%-16s%-9s%-7s%-7s%-3s%-4s%-3s%-4s%s\n", 2452 S_HDR); 2453 } else if (re->options & RE_WW) { 2454 if (re->options & RE_T) 2455 printf(" %s\n %-16s%-17s%-7s%-7s%-5s%-3s%-4s%s\n" 2456 "%12s\n", ST_HDR); 2457 else 2458 printf(" %-23s%-16s%-17s%-7s%-7s%-3s%-4s%-3s%-4s%s\n", 2459 S_HDR); 2460 } else { 2461 if (re->options & RE_T) 2462 printf(" %s\n %-18s%-17s%-18s%s\n %-18s" 2463 "%-17s%-18s%s\n%12s\n", ST_HDRL); 2464 else 2465 printf(" %-23s%-17s%-18s%s\n %-18s%-17s%-7s%" 2466 "-6s%-6s%s\n", S_HDRL); 2467 } 2468 for (i = 0; (size_t)i < re->shnum; i++) { 2469 s = &re->sl[i]; 2470 if (re->ec == ELFCLASS32) { 2471 if (re->options & RE_T) 2472 printf(" [%2d] %s\n %-15.15s %8.8jx" 2473 " %6.6jx %6.6jx %2.2jx %2u %3u %2ju\n" 2474 " %s\n", ST_CT); 2475 else 2476 printf(" [%2d] %-17.17s %-15.15s %8.8jx" 2477 " %6.6jx %6.6jx %2.2jx %3s %2u %3u %2ju\n", 2478 S_CT); 2479 } else if (re->options & RE_WW) { 2480 if (re->options & RE_T) 2481 printf(" [%2d] %s\n %-15.15s %16.16jx" 2482 " %6.6jx %6.6jx %2.2jx %2u %3u %2ju\n" 2483 " %s\n", ST_CT); 2484 else 2485 printf(" [%2d] %-17.17s %-15.15s %16.16jx" 2486 " %6.6jx %6.6jx %2.2jx %3s %2u %3u %2ju\n", 2487 S_CT); 2488 } else { 2489 if (re->options & RE_T) 2490 printf(" [%2d] %s\n %-15.15s %16.16jx" 2491 " %16.16jx %u\n %16.16jx %16.16jx" 2492 " %-16u %ju\n %s\n", ST_CTL); 2493 else 2494 printf(" [%2d] %-17.17s %-15.15s %16.16jx" 2495 " %8.8jx\n %16.16jx %16.16jx " 2496 "%3s %2u %3u %ju\n", S_CT); 2497 } 2498 } 2499 if ((re->options & RE_T) == 0) 2500 printf("Key to Flags:\n W (write), A (alloc)," 2501 " X (execute), M (merge), S (strings)\n" 2502 " I (info), L (link order), G (group), x (unknown)\n" 2503 " O (extra OS processing required)" 2504 " o (OS specific), p (processor specific)\n"); 2505 2506 #undef S_HDR 2507 #undef S_HDRL 2508 #undef ST_HDR 2509 #undef ST_HDRL 2510 #undef S_CT 2511 #undef ST_CT 2512 #undef ST_CTL 2513 } 2514 2515 /* 2516 * Return number of entries in the given section. We'd prefer ent_count be a 2517 * size_t *, but libelf APIs already use int for section indices. 2518 */ 2519 static int 2520 get_ent_count(struct section *s, int *ent_count) 2521 { 2522 if (s->entsize == 0) { 2523 warnx("section %s has entry size 0", s->name); 2524 return (0); 2525 } else if (s->sz / s->entsize > INT_MAX) { 2526 warnx("section %s has invalid section count", s->name); 2527 return (0); 2528 } 2529 *ent_count = (int)(s->sz / s->entsize); 2530 return (1); 2531 } 2532 2533 static void 2534 dump_dynamic(struct readelf *re) 2535 { 2536 GElf_Dyn dyn; 2537 Elf_Data *d; 2538 struct section *s; 2539 int elferr, i, is_dynamic, j, jmax, nentries; 2540 2541 is_dynamic = 0; 2542 2543 for (i = 0; (size_t)i < re->shnum; i++) { 2544 s = &re->sl[i]; 2545 if (s->type != SHT_DYNAMIC) 2546 continue; 2547 (void) elf_errno(); 2548 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 2549 elferr = elf_errno(); 2550 if (elferr != 0) 2551 warnx("elf_getdata failed: %s", elf_errmsg(-1)); 2552 continue; 2553 } 2554 if (d->d_size <= 0) 2555 continue; 2556 2557 is_dynamic = 1; 2558 2559 /* Determine the actual number of table entries. */ 2560 nentries = 0; 2561 if (!get_ent_count(s, &jmax)) 2562 continue; 2563 for (j = 0; j < jmax; j++) { 2564 if (gelf_getdyn(d, j, &dyn) != &dyn) { 2565 warnx("gelf_getdyn failed: %s", 2566 elf_errmsg(-1)); 2567 continue; 2568 } 2569 nentries ++; 2570 if (dyn.d_tag == DT_NULL) 2571 break; 2572 } 2573 2574 printf("\nDynamic section at offset 0x%jx", (uintmax_t)s->off); 2575 printf(" contains %u entries:\n", nentries); 2576 2577 if (re->ec == ELFCLASS32) 2578 printf("%5s%12s%28s\n", "Tag", "Type", "Name/Value"); 2579 else 2580 printf("%5s%20s%28s\n", "Tag", "Type", "Name/Value"); 2581 2582 for (j = 0; j < nentries; j++) { 2583 if (gelf_getdyn(d, j, &dyn) != &dyn) 2584 continue; 2585 /* Dump dynamic entry type. */ 2586 if (re->ec == ELFCLASS32) 2587 printf(" 0x%8.8jx", (uintmax_t)dyn.d_tag); 2588 else 2589 printf(" 0x%16.16jx", (uintmax_t)dyn.d_tag); 2590 printf(" %-20s", dt_type(re->ehdr.e_machine, 2591 dyn.d_tag)); 2592 /* Dump dynamic entry value. */ 2593 dump_dyn_val(re, &dyn, s->link); 2594 } 2595 } 2596 2597 if (!is_dynamic) 2598 printf("\nThere is no dynamic section in this file.\n"); 2599 } 2600 2601 static char * 2602 timestamp(time_t ti) 2603 { 2604 static char ts[32]; 2605 struct tm *t; 2606 2607 t = gmtime(&ti); 2608 snprintf(ts, sizeof(ts), "%04d-%02d-%02dT%02d:%02d:%02d", 2609 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, 2610 t->tm_min, t->tm_sec); 2611 2612 return (ts); 2613 } 2614 2615 static const char * 2616 dyn_str(struct readelf *re, uint32_t stab, uint64_t d_val) 2617 { 2618 const char *name; 2619 2620 if (stab == SHN_UNDEF) 2621 name = "ERROR"; 2622 else if ((name = elf_strptr(re->elf, stab, d_val)) == NULL) { 2623 (void) elf_errno(); /* clear error */ 2624 name = "ERROR"; 2625 } 2626 2627 return (name); 2628 } 2629 2630 static void 2631 dump_arch_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab) 2632 { 2633 const char *name; 2634 2635 switch (re->ehdr.e_machine) { 2636 case EM_MIPS: 2637 case EM_MIPS_RS3_LE: 2638 switch (dyn->d_tag) { 2639 case DT_MIPS_RLD_VERSION: 2640 case DT_MIPS_LOCAL_GOTNO: 2641 case DT_MIPS_CONFLICTNO: 2642 case DT_MIPS_LIBLISTNO: 2643 case DT_MIPS_SYMTABNO: 2644 case DT_MIPS_UNREFEXTNO: 2645 case DT_MIPS_GOTSYM: 2646 case DT_MIPS_HIPAGENO: 2647 case DT_MIPS_DELTA_CLASS_NO: 2648 case DT_MIPS_DELTA_INSTANCE_NO: 2649 case DT_MIPS_DELTA_RELOC_NO: 2650 case DT_MIPS_DELTA_SYM_NO: 2651 case DT_MIPS_DELTA_CLASSSYM_NO: 2652 case DT_MIPS_LOCALPAGE_GOTIDX: 2653 case DT_MIPS_LOCAL_GOTIDX: 2654 case DT_MIPS_HIDDEN_GOTIDX: 2655 case DT_MIPS_PROTECTED_GOTIDX: 2656 printf(" %ju\n", (uintmax_t) dyn->d_un.d_val); 2657 break; 2658 case DT_MIPS_ICHECKSUM: 2659 case DT_MIPS_FLAGS: 2660 case DT_MIPS_BASE_ADDRESS: 2661 case DT_MIPS_CONFLICT: 2662 case DT_MIPS_LIBLIST: 2663 case DT_MIPS_RLD_MAP: 2664 case DT_MIPS_DELTA_CLASS: 2665 case DT_MIPS_DELTA_INSTANCE: 2666 case DT_MIPS_DELTA_RELOC: 2667 case DT_MIPS_DELTA_SYM: 2668 case DT_MIPS_DELTA_CLASSSYM: 2669 case DT_MIPS_CXX_FLAGS: 2670 case DT_MIPS_PIXIE_INIT: 2671 case DT_MIPS_SYMBOL_LIB: 2672 case DT_MIPS_OPTIONS: 2673 case DT_MIPS_INTERFACE: 2674 case DT_MIPS_DYNSTR_ALIGN: 2675 case DT_MIPS_INTERFACE_SIZE: 2676 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: 2677 case DT_MIPS_COMPACT_SIZE: 2678 case DT_MIPS_GP_VALUE: 2679 case DT_MIPS_AUX_DYNAMIC: 2680 case DT_MIPS_PLTGOT: 2681 case DT_MIPS_RLD_OBJ_UPDATE: 2682 case DT_MIPS_RWPLT: 2683 printf(" 0x%jx\n", (uintmax_t) dyn->d_un.d_val); 2684 break; 2685 case DT_MIPS_IVERSION: 2686 case DT_MIPS_PERF_SUFFIX: 2687 case DT_AUXILIARY: 2688 case DT_FILTER: 2689 name = dyn_str(re, stab, dyn->d_un.d_val); 2690 printf(" %s\n", name); 2691 break; 2692 case DT_MIPS_TIME_STAMP: 2693 printf(" %s\n", timestamp(dyn->d_un.d_val)); 2694 break; 2695 } 2696 break; 2697 default: 2698 printf("\n"); 2699 break; 2700 } 2701 } 2702 2703 static void 2704 dump_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab) 2705 { 2706 const char *name; 2707 2708 if (dyn->d_tag >= DT_LOPROC && dyn->d_tag <= DT_HIPROC) { 2709 dump_arch_dyn_val(re, dyn, stab); 2710 return; 2711 } 2712 2713 /* These entry values are index into the string table. */ 2714 name = NULL; 2715 if (dyn->d_tag == DT_NEEDED || dyn->d_tag == DT_SONAME || 2716 dyn->d_tag == DT_RPATH || dyn->d_tag == DT_RUNPATH) 2717 name = dyn_str(re, stab, dyn->d_un.d_val); 2718 2719 switch(dyn->d_tag) { 2720 case DT_NULL: 2721 case DT_PLTGOT: 2722 case DT_HASH: 2723 case DT_STRTAB: 2724 case DT_SYMTAB: 2725 case DT_RELA: 2726 case DT_INIT: 2727 case DT_SYMBOLIC: 2728 case DT_REL: 2729 case DT_DEBUG: 2730 case DT_TEXTREL: 2731 case DT_JMPREL: 2732 case DT_FINI: 2733 case DT_VERDEF: 2734 case DT_VERNEED: 2735 case DT_VERSYM: 2736 case DT_GNU_HASH: 2737 case DT_GNU_LIBLIST: 2738 case DT_GNU_CONFLICT: 2739 printf(" 0x%jx\n", (uintmax_t) dyn->d_un.d_val); 2740 break; 2741 case DT_PLTRELSZ: 2742 case DT_RELASZ: 2743 case DT_RELAENT: 2744 case DT_STRSZ: 2745 case DT_SYMENT: 2746 case DT_RELSZ: 2747 case DT_RELENT: 2748 case DT_INIT_ARRAYSZ: 2749 case DT_FINI_ARRAYSZ: 2750 case DT_GNU_CONFLICTSZ: 2751 case DT_GNU_LIBLISTSZ: 2752 printf(" %ju (bytes)\n", (uintmax_t) dyn->d_un.d_val); 2753 break; 2754 case DT_RELACOUNT: 2755 case DT_RELCOUNT: 2756 case DT_VERDEFNUM: 2757 case DT_VERNEEDNUM: 2758 printf(" %ju\n", (uintmax_t) dyn->d_un.d_val); 2759 break; 2760 case DT_NEEDED: 2761 printf(" Shared library: [%s]\n", name); 2762 break; 2763 case DT_SONAME: 2764 printf(" Library soname: [%s]\n", name); 2765 break; 2766 case DT_RPATH: 2767 printf(" Library rpath: [%s]\n", name); 2768 break; 2769 case DT_RUNPATH: 2770 printf(" Library runpath: [%s]\n", name); 2771 break; 2772 case DT_PLTREL: 2773 printf(" %s\n", dt_type(re->ehdr.e_machine, dyn->d_un.d_val)); 2774 break; 2775 case DT_GNU_PRELINKED: 2776 printf(" %s\n", timestamp(dyn->d_un.d_val)); 2777 break; 2778 default: 2779 printf("\n"); 2780 } 2781 } 2782 2783 static void 2784 dump_rel(struct readelf *re, struct section *s, Elf_Data *d) 2785 { 2786 GElf_Rel r; 2787 const char *symname; 2788 uint64_t symval; 2789 int i, len; 2790 2791 if (s->link >= re->shnum) 2792 return; 2793 2794 #define REL_HDR "r_offset", "r_info", "r_type", "st_value", "st_name" 2795 #define REL_CT32 (uintmax_t)r.r_offset, (uintmax_t)r.r_info, \ 2796 elftc_reloc_type_str(re->ehdr.e_machine, \ 2797 ELF32_R_TYPE(r.r_info)), (uintmax_t)symval, symname 2798 #define REL_CT64 (uintmax_t)r.r_offset, (uintmax_t)r.r_info, \ 2799 elftc_reloc_type_str(re->ehdr.e_machine, \ 2800 ELF64_R_TYPE(r.r_info)), (uintmax_t)symval, symname 2801 2802 printf("\nRelocation section (%s):\n", s->name); 2803 if (re->ec == ELFCLASS32) 2804 printf("%-8s %-8s %-19s %-8s %s\n", REL_HDR); 2805 else { 2806 if (re->options & RE_WW) 2807 printf("%-16s %-16s %-24s %-16s %s\n", REL_HDR); 2808 else 2809 printf("%-12s %-12s %-19s %-16s %s\n", REL_HDR); 2810 } 2811 assert(d->d_size == s->sz); 2812 if (!get_ent_count(s, &len)) 2813 return; 2814 for (i = 0; i < len; i++) { 2815 if (gelf_getrel(d, i, &r) != &r) { 2816 warnx("gelf_getrel failed: %s", elf_errmsg(-1)); 2817 continue; 2818 } 2819 symname = get_symbol_name(re, s->link, GELF_R_SYM(r.r_info)); 2820 symval = get_symbol_value(re, s->link, GELF_R_SYM(r.r_info)); 2821 if (re->ec == ELFCLASS32) { 2822 r.r_info = ELF32_R_INFO(ELF64_R_SYM(r.r_info), 2823 ELF64_R_TYPE(r.r_info)); 2824 printf("%8.8jx %8.8jx %-19.19s %8.8jx %s\n", REL_CT32); 2825 } else { 2826 if (re->options & RE_WW) 2827 printf("%16.16jx %16.16jx %-24.24s" 2828 " %16.16jx %s\n", REL_CT64); 2829 else 2830 printf("%12.12jx %12.12jx %-19.19s" 2831 " %16.16jx %s\n", REL_CT64); 2832 } 2833 } 2834 2835 #undef REL_HDR 2836 #undef REL_CT 2837 } 2838 2839 static void 2840 dump_rela(struct readelf *re, struct section *s, Elf_Data *d) 2841 { 2842 GElf_Rela r; 2843 const char *symname; 2844 uint64_t symval; 2845 int i, len; 2846 2847 if (s->link >= re->shnum) 2848 return; 2849 2850 #define RELA_HDR "r_offset", "r_info", "r_type", "st_value", \ 2851 "st_name + r_addend" 2852 #define RELA_CT32 (uintmax_t)r.r_offset, (uintmax_t)r.r_info, \ 2853 elftc_reloc_type_str(re->ehdr.e_machine, \ 2854 ELF32_R_TYPE(r.r_info)), (uintmax_t)symval, symname 2855 #define RELA_CT64 (uintmax_t)r.r_offset, (uintmax_t)r.r_info, \ 2856 elftc_reloc_type_str(re->ehdr.e_machine, \ 2857 ELF64_R_TYPE(r.r_info)), (uintmax_t)symval, symname 2858 2859 printf("\nRelocation section with addend (%s):\n", s->name); 2860 if (re->ec == ELFCLASS32) 2861 printf("%-8s %-8s %-19s %-8s %s\n", RELA_HDR); 2862 else { 2863 if (re->options & RE_WW) 2864 printf("%-16s %-16s %-24s %-16s %s\n", RELA_HDR); 2865 else 2866 printf("%-12s %-12s %-19s %-16s %s\n", RELA_HDR); 2867 } 2868 assert(d->d_size == s->sz); 2869 if (!get_ent_count(s, &len)) 2870 return; 2871 for (i = 0; i < len; i++) { 2872 if (gelf_getrela(d, i, &r) != &r) { 2873 warnx("gelf_getrel failed: %s", elf_errmsg(-1)); 2874 continue; 2875 } 2876 symname = get_symbol_name(re, s->link, GELF_R_SYM(r.r_info)); 2877 symval = get_symbol_value(re, s->link, GELF_R_SYM(r.r_info)); 2878 if (re->ec == ELFCLASS32) { 2879 r.r_info = ELF32_R_INFO(ELF64_R_SYM(r.r_info), 2880 ELF64_R_TYPE(r.r_info)); 2881 printf("%8.8jx %8.8jx %-19.19s %8.8jx %s", RELA_CT32); 2882 printf(" + %x\n", (uint32_t) r.r_addend); 2883 } else { 2884 if (re->options & RE_WW) 2885 printf("%16.16jx %16.16jx %-24.24s" 2886 " %16.16jx %s", RELA_CT64); 2887 else 2888 printf("%12.12jx %12.12jx %-19.19s" 2889 " %16.16jx %s", RELA_CT64); 2890 printf(" + %jx\n", (uintmax_t) r.r_addend); 2891 } 2892 } 2893 2894 #undef RELA_HDR 2895 #undef RELA_CT 2896 } 2897 2898 static void 2899 dump_reloc(struct readelf *re) 2900 { 2901 struct section *s; 2902 Elf_Data *d; 2903 int i, elferr; 2904 2905 for (i = 0; (size_t)i < re->shnum; i++) { 2906 s = &re->sl[i]; 2907 if (s->type == SHT_REL || s->type == SHT_RELA) { 2908 (void) elf_errno(); 2909 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 2910 elferr = elf_errno(); 2911 if (elferr != 0) 2912 warnx("elf_getdata failed: %s", 2913 elf_errmsg(elferr)); 2914 continue; 2915 } 2916 if (s->type == SHT_REL) 2917 dump_rel(re, s, d); 2918 else 2919 dump_rela(re, s, d); 2920 } 2921 } 2922 } 2923 2924 static void 2925 dump_symtab(struct readelf *re, int i) 2926 { 2927 struct section *s; 2928 Elf_Data *d; 2929 GElf_Sym sym; 2930 const char *name; 2931 uint32_t stab; 2932 int elferr, j, len; 2933 uint16_t vs; 2934 2935 s = &re->sl[i]; 2936 if (s->link >= re->shnum) 2937 return; 2938 stab = s->link; 2939 (void) elf_errno(); 2940 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 2941 elferr = elf_errno(); 2942 if (elferr != 0) 2943 warnx("elf_getdata failed: %s", elf_errmsg(elferr)); 2944 return; 2945 } 2946 if (d->d_size <= 0) 2947 return; 2948 if (!get_ent_count(s, &len)) 2949 return; 2950 printf("Symbol table (%s)", s->name); 2951 printf(" contains %d entries:\n", len); 2952 printf("%7s%9s%14s%5s%8s%6s%9s%5s\n", "Num:", "Value", "Size", "Type", 2953 "Bind", "Vis", "Ndx", "Name"); 2954 2955 for (j = 0; j < len; j++) { 2956 if (gelf_getsym(d, j, &sym) != &sym) { 2957 warnx("gelf_getsym failed: %s", elf_errmsg(-1)); 2958 continue; 2959 } 2960 printf("%6d:", j); 2961 printf(" %16.16jx", (uintmax_t) sym.st_value); 2962 printf(" %5ju", (uintmax_t) sym.st_size); 2963 printf(" %-7s", st_type(re->ehdr.e_machine, 2964 re->ehdr.e_ident[EI_OSABI], GELF_ST_TYPE(sym.st_info))); 2965 printf(" %-6s", st_bind(GELF_ST_BIND(sym.st_info))); 2966 printf(" %-8s", st_vis(GELF_ST_VISIBILITY(sym.st_other))); 2967 printf(" %3s", st_shndx(sym.st_shndx)); 2968 if ((name = elf_strptr(re->elf, stab, sym.st_name)) != NULL) 2969 printf(" %s", name); 2970 /* Append symbol version string for SHT_DYNSYM symbol table. */ 2971 if (s->type == SHT_DYNSYM && re->ver != NULL && 2972 re->vs != NULL && re->vs[j] > 1) { 2973 vs = re->vs[j] & VERSYM_VERSION; 2974 if (vs >= re->ver_sz || re->ver[vs].name == NULL) { 2975 warnx("invalid versym version index %u", vs); 2976 break; 2977 } 2978 if (re->vs[j] & VERSYM_HIDDEN || re->ver[vs].type == 0) 2979 printf("@%s (%d)", re->ver[vs].name, vs); 2980 else 2981 printf("@@%s (%d)", re->ver[vs].name, vs); 2982 } 2983 putchar('\n'); 2984 } 2985 2986 } 2987 2988 static void 2989 dump_symtabs(struct readelf *re) 2990 { 2991 GElf_Dyn dyn; 2992 Elf_Data *d; 2993 struct section *s; 2994 uint64_t dyn_off; 2995 int elferr, i, len; 2996 2997 /* 2998 * If -D is specified, only dump the symbol table specified by 2999 * the DT_SYMTAB entry in the .dynamic section. 3000 */ 3001 dyn_off = 0; 3002 if (re->options & RE_DD) { 3003 s = NULL; 3004 for (i = 0; (size_t)i < re->shnum; i++) 3005 if (re->sl[i].type == SHT_DYNAMIC) { 3006 s = &re->sl[i]; 3007 break; 3008 } 3009 if (s == NULL) 3010 return; 3011 (void) elf_errno(); 3012 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 3013 elferr = elf_errno(); 3014 if (elferr != 0) 3015 warnx("elf_getdata failed: %s", elf_errmsg(-1)); 3016 return; 3017 } 3018 if (d->d_size <= 0) 3019 return; 3020 if (!get_ent_count(s, &len)) 3021 return; 3022 3023 for (i = 0; i < len; i++) { 3024 if (gelf_getdyn(d, i, &dyn) != &dyn) { 3025 warnx("gelf_getdyn failed: %s", elf_errmsg(-1)); 3026 continue; 3027 } 3028 if (dyn.d_tag == DT_SYMTAB) { 3029 dyn_off = dyn.d_un.d_val; 3030 break; 3031 } 3032 } 3033 } 3034 3035 /* Find and dump symbol tables. */ 3036 for (i = 0; (size_t)i < re->shnum; i++) { 3037 s = &re->sl[i]; 3038 if (s->type == SHT_SYMTAB || s->type == SHT_DYNSYM) { 3039 if (re->options & RE_DD) { 3040 if (dyn_off == s->addr) { 3041 dump_symtab(re, i); 3042 break; 3043 } 3044 } else 3045 dump_symtab(re, i); 3046 } 3047 } 3048 } 3049 3050 static void 3051 dump_svr4_hash(struct section *s) 3052 { 3053 Elf_Data *d; 3054 uint32_t *buf; 3055 uint32_t nbucket, nchain; 3056 uint32_t *bucket, *chain; 3057 uint32_t *bl, *c, maxl, total; 3058 int elferr, i, j; 3059 3060 /* Read and parse the content of .hash section. */ 3061 (void) elf_errno(); 3062 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 3063 elferr = elf_errno(); 3064 if (elferr != 0) 3065 warnx("elf_getdata failed: %s", elf_errmsg(elferr)); 3066 return; 3067 } 3068 if (d->d_size < 2 * sizeof(uint32_t)) { 3069 warnx(".hash section too small"); 3070 return; 3071 } 3072 buf = d->d_buf; 3073 nbucket = buf[0]; 3074 nchain = buf[1]; 3075 if (nbucket <= 0 || nchain <= 0) { 3076 warnx("Malformed .hash section"); 3077 return; 3078 } 3079 if (d->d_size != (nbucket + nchain + 2) * sizeof(uint32_t)) { 3080 warnx("Malformed .hash section"); 3081 return; 3082 } 3083 bucket = &buf[2]; 3084 chain = &buf[2 + nbucket]; 3085 3086 maxl = 0; 3087 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL) 3088 errx(EXIT_FAILURE, "calloc failed"); 3089 for (i = 0; (uint32_t)i < nbucket; i++) 3090 for (j = bucket[i]; j > 0 && (uint32_t)j < nchain; j = chain[j]) 3091 if (++bl[i] > maxl) 3092 maxl = bl[i]; 3093 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL) 3094 errx(EXIT_FAILURE, "calloc failed"); 3095 for (i = 0; (uint32_t)i < nbucket; i++) 3096 c[bl[i]]++; 3097 printf("\nHistogram for bucket list length (total of %u buckets):\n", 3098 nbucket); 3099 printf(" Length\tNumber\t\t%% of total\tCoverage\n"); 3100 total = 0; 3101 for (i = 0; (uint32_t)i <= maxl; i++) { 3102 total += c[i] * i; 3103 printf("%7u\t%-10u\t(%5.1f%%)\t%5.1f%%\n", i, c[i], 3104 c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1)); 3105 } 3106 free(c); 3107 free(bl); 3108 } 3109 3110 static void 3111 dump_svr4_hash64(struct readelf *re, struct section *s) 3112 { 3113 Elf_Data *d, dst; 3114 uint64_t *buf; 3115 uint64_t nbucket, nchain; 3116 uint64_t *bucket, *chain; 3117 uint64_t *bl, *c, maxl, total; 3118 int elferr, i, j; 3119 3120 /* 3121 * ALPHA uses 64-bit hash entries. Since libelf assumes that 3122 * .hash section contains only 32-bit entry, an explicit 3123 * gelf_xlatetom is needed here. 3124 */ 3125 (void) elf_errno(); 3126 if ((d = elf_rawdata(s->scn, NULL)) == NULL) { 3127 elferr = elf_errno(); 3128 if (elferr != 0) 3129 warnx("elf_rawdata failed: %s", 3130 elf_errmsg(elferr)); 3131 return; 3132 } 3133 d->d_type = ELF_T_XWORD; 3134 memcpy(&dst, d, sizeof(Elf_Data)); 3135 if (gelf_xlatetom(re->elf, &dst, d, 3136 re->ehdr.e_ident[EI_DATA]) != &dst) { 3137 warnx("gelf_xlatetom failed: %s", elf_errmsg(-1)); 3138 return; 3139 } 3140 if (dst.d_size < 2 * sizeof(uint64_t)) { 3141 warnx(".hash section too small"); 3142 return; 3143 } 3144 buf = dst.d_buf; 3145 nbucket = buf[0]; 3146 nchain = buf[1]; 3147 if (nbucket <= 0 || nchain <= 0) { 3148 warnx("Malformed .hash section"); 3149 return; 3150 } 3151 if (d->d_size != (nbucket + nchain + 2) * sizeof(uint32_t)) { 3152 warnx("Malformed .hash section"); 3153 return; 3154 } 3155 bucket = &buf[2]; 3156 chain = &buf[2 + nbucket]; 3157 3158 maxl = 0; 3159 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL) 3160 errx(EXIT_FAILURE, "calloc failed"); 3161 for (i = 0; (uint32_t)i < nbucket; i++) 3162 for (j = bucket[i]; j > 0 && (uint32_t)j < nchain; j = chain[j]) 3163 if (++bl[i] > maxl) 3164 maxl = bl[i]; 3165 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL) 3166 errx(EXIT_FAILURE, "calloc failed"); 3167 for (i = 0; (uint64_t)i < nbucket; i++) 3168 c[bl[i]]++; 3169 printf("Histogram for bucket list length (total of %ju buckets):\n", 3170 (uintmax_t)nbucket); 3171 printf(" Length\tNumber\t\t%% of total\tCoverage\n"); 3172 total = 0; 3173 for (i = 0; (uint64_t)i <= maxl; i++) { 3174 total += c[i] * i; 3175 printf("%7u\t%-10ju\t(%5.1f%%)\t%5.1f%%\n", i, (uintmax_t)c[i], 3176 c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1)); 3177 } 3178 free(c); 3179 free(bl); 3180 } 3181 3182 static void 3183 dump_gnu_hash(struct readelf *re, struct section *s) 3184 { 3185 struct section *ds; 3186 Elf_Data *d; 3187 uint32_t *buf; 3188 uint32_t *bucket, *chain; 3189 uint32_t nbucket, nchain, symndx, maskwords; 3190 uint32_t *bl, *c, maxl, total; 3191 int elferr, dynsymcount, i, j; 3192 3193 (void) elf_errno(); 3194 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 3195 elferr = elf_errno(); 3196 if (elferr != 0) 3197 warnx("elf_getdata failed: %s", 3198 elf_errmsg(elferr)); 3199 return; 3200 } 3201 if (d->d_size < 4 * sizeof(uint32_t)) { 3202 warnx(".gnu.hash section too small"); 3203 return; 3204 } 3205 buf = d->d_buf; 3206 nbucket = buf[0]; 3207 symndx = buf[1]; 3208 maskwords = buf[2]; 3209 buf += 4; 3210 if (s->link >= re->shnum) 3211 return; 3212 ds = &re->sl[s->link]; 3213 if (!get_ent_count(ds, &dynsymcount)) 3214 return; 3215 if (symndx >= (uint32_t)dynsymcount) { 3216 warnx("Malformed .gnu.hash section (symndx out of range)"); 3217 return; 3218 } 3219 nchain = dynsymcount - symndx; 3220 if (d->d_size != 4 * sizeof(uint32_t) + maskwords * 3221 (re->ec == ELFCLASS32 ? sizeof(uint32_t) : sizeof(uint64_t)) + 3222 (nbucket + nchain) * sizeof(uint32_t)) { 3223 warnx("Malformed .gnu.hash section"); 3224 return; 3225 } 3226 bucket = buf + (re->ec == ELFCLASS32 ? maskwords : maskwords * 2); 3227 chain = bucket + nbucket; 3228 3229 maxl = 0; 3230 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL) 3231 errx(EXIT_FAILURE, "calloc failed"); 3232 for (i = 0; (uint32_t)i < nbucket; i++) 3233 for (j = bucket[i]; j > 0 && (uint32_t)j - symndx < nchain; 3234 j++) { 3235 if (++bl[i] > maxl) 3236 maxl = bl[i]; 3237 if (chain[j - symndx] & 1) 3238 break; 3239 } 3240 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL) 3241 errx(EXIT_FAILURE, "calloc failed"); 3242 for (i = 0; (uint32_t)i < nbucket; i++) 3243 c[bl[i]]++; 3244 printf("Histogram for bucket list length (total of %u buckets):\n", 3245 nbucket); 3246 printf(" Length\tNumber\t\t%% of total\tCoverage\n"); 3247 total = 0; 3248 for (i = 0; (uint32_t)i <= maxl; i++) { 3249 total += c[i] * i; 3250 printf("%7u\t%-10u\t(%5.1f%%)\t%5.1f%%\n", i, c[i], 3251 c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1)); 3252 } 3253 free(c); 3254 free(bl); 3255 } 3256 3257 static void 3258 dump_hash(struct readelf *re) 3259 { 3260 struct section *s; 3261 int i; 3262 3263 for (i = 0; (size_t) i < re->shnum; i++) { 3264 s = &re->sl[i]; 3265 if (s->type == SHT_HASH || s->type == SHT_GNU_HASH) { 3266 if (s->type == SHT_GNU_HASH) 3267 dump_gnu_hash(re, s); 3268 else if (re->ehdr.e_machine == EM_ALPHA && 3269 s->entsize == 8) 3270 dump_svr4_hash64(re, s); 3271 else 3272 dump_svr4_hash(s); 3273 } 3274 } 3275 } 3276 3277 static void 3278 dump_notes(struct readelf *re) 3279 { 3280 struct section *s; 3281 const char *rawfile; 3282 GElf_Phdr phdr; 3283 Elf_Data *d; 3284 size_t filesize, phnum; 3285 int i, elferr; 3286 3287 if (re->ehdr.e_type == ET_CORE) { 3288 /* 3289 * Search program headers in the core file for 3290 * PT_NOTE entry. 3291 */ 3292 if (elf_getphnum(re->elf, &phnum) == 0) { 3293 warnx("elf_getphnum failed: %s", elf_errmsg(-1)); 3294 return; 3295 } 3296 if (phnum == 0) 3297 return; 3298 if ((rawfile = elf_rawfile(re->elf, &filesize)) == NULL) { 3299 warnx("elf_rawfile failed: %s", elf_errmsg(-1)); 3300 return; 3301 } 3302 for (i = 0; (size_t) i < phnum; i++) { 3303 if (gelf_getphdr(re->elf, i, &phdr) != &phdr) { 3304 warnx("gelf_getphdr failed: %s", 3305 elf_errmsg(-1)); 3306 continue; 3307 } 3308 if (phdr.p_type == PT_NOTE) { 3309 if (phdr.p_offset >= filesize || 3310 phdr.p_filesz > filesize - phdr.p_offset) { 3311 warnx("invalid PHDR offset"); 3312 continue; 3313 } 3314 dump_notes_content(re, rawfile + phdr.p_offset, 3315 phdr.p_filesz, phdr.p_offset); 3316 } 3317 } 3318 3319 } else { 3320 /* 3321 * For objects other than core files, Search for 3322 * SHT_NOTE sections. 3323 */ 3324 for (i = 0; (size_t) i < re->shnum; i++) { 3325 s = &re->sl[i]; 3326 if (s->type == SHT_NOTE) { 3327 (void) elf_errno(); 3328 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 3329 elferr = elf_errno(); 3330 if (elferr != 0) 3331 warnx("elf_getdata failed: %s", 3332 elf_errmsg(elferr)); 3333 continue; 3334 } 3335 dump_notes_content(re, d->d_buf, d->d_size, 3336 s->off); 3337 } 3338 } 3339 } 3340 } 3341 3342 static void 3343 dump_notes_content(struct readelf *re, const char *buf, size_t sz, off_t off) 3344 { 3345 Elf_Note *note; 3346 const char *end, *name; 3347 3348 printf("\nNotes at offset %#010jx with length %#010jx:\n", 3349 (uintmax_t) off, (uintmax_t) sz); 3350 printf(" %-13s %-15s %s\n", "Owner", "Data size", "Description"); 3351 end = buf + sz; 3352 while (buf < end) { 3353 if (buf + sizeof(*note) > end) { 3354 warnx("invalid note header"); 3355 return; 3356 } 3357 note = (Elf_Note *)(uintptr_t) buf; 3358 name = (char *)(uintptr_t)(note + 1); 3359 /* 3360 * The name field is required to be nul-terminated, and 3361 * n_namesz includes the terminating nul in observed 3362 * implementations (contrary to the ELF-64 spec). A special 3363 * case is needed for cores generated by some older Linux 3364 * versions, which write a note named "CORE" without a nul 3365 * terminator and n_namesz = 4. 3366 */ 3367 if (note->n_namesz == 0) 3368 name = ""; 3369 else if (note->n_namesz == 4 && strncmp(name, "CORE", 4) == 0) 3370 name = "CORE"; 3371 else if (strnlen(name, note->n_namesz) >= note->n_namesz) 3372 name = "<invalid>"; 3373 printf(" %-13s %#010jx", name, (uintmax_t) note->n_descsz); 3374 printf(" %s\n", note_type(name, re->ehdr.e_type, 3375 note->n_type)); 3376 buf += sizeof(Elf_Note) + roundup2(note->n_namesz, 4) + 3377 roundup2(note->n_descsz, 4); 3378 } 3379 } 3380 3381 /* 3382 * Symbol versioning sections are the same for 32bit and 64bit 3383 * ELF objects. 3384 */ 3385 #define Elf_Verdef Elf32_Verdef 3386 #define Elf_Verdaux Elf32_Verdaux 3387 #define Elf_Verneed Elf32_Verneed 3388 #define Elf_Vernaux Elf32_Vernaux 3389 3390 #define SAVE_VERSION_NAME(x, n, t) \ 3391 do { \ 3392 while (x >= re->ver_sz) { \ 3393 nv = realloc(re->ver, \ 3394 sizeof(*re->ver) * re->ver_sz * 2); \ 3395 if (nv == NULL) { \ 3396 warn("realloc failed"); \ 3397 free(re->ver); \ 3398 return; \ 3399 } \ 3400 re->ver = nv; \ 3401 for (i = re->ver_sz; i < re->ver_sz * 2; i++) { \ 3402 re->ver[i].name = NULL; \ 3403 re->ver[i].type = 0; \ 3404 } \ 3405 re->ver_sz *= 2; \ 3406 } \ 3407 if (x > 1) { \ 3408 re->ver[x].name = n; \ 3409 re->ver[x].type = t; \ 3410 } \ 3411 } while (0) 3412 3413 3414 static void 3415 dump_verdef(struct readelf *re, int dump) 3416 { 3417 struct section *s; 3418 struct symver *nv; 3419 Elf_Data *d; 3420 Elf_Verdef *vd; 3421 Elf_Verdaux *vda; 3422 uint8_t *buf, *end, *buf2; 3423 const char *name; 3424 int elferr, i, j; 3425 3426 if ((s = re->vd_s) == NULL) 3427 return; 3428 if (s->link >= re->shnum) 3429 return; 3430 3431 if (re->ver == NULL) { 3432 re->ver_sz = 16; 3433 if ((re->ver = calloc(re->ver_sz, sizeof(*re->ver))) == 3434 NULL) { 3435 warn("calloc failed"); 3436 return; 3437 } 3438 re->ver[0].name = "*local*"; 3439 re->ver[1].name = "*global*"; 3440 } 3441 3442 if (dump) 3443 printf("\nVersion definition section (%s):\n", s->name); 3444 (void) elf_errno(); 3445 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 3446 elferr = elf_errno(); 3447 if (elferr != 0) 3448 warnx("elf_getdata failed: %s", elf_errmsg(elferr)); 3449 return; 3450 } 3451 if (d->d_size == 0) 3452 return; 3453 3454 buf = d->d_buf; 3455 end = buf + d->d_size; 3456 while (buf + sizeof(Elf_Verdef) <= end) { 3457 vd = (Elf_Verdef *) (uintptr_t) buf; 3458 if (dump) { 3459 printf(" 0x%4.4lx", (unsigned long) 3460 (buf - (uint8_t *)d->d_buf)); 3461 printf(" vd_version: %u vd_flags: %d" 3462 " vd_ndx: %u vd_cnt: %u", vd->vd_version, 3463 vd->vd_flags, vd->vd_ndx, vd->vd_cnt); 3464 } 3465 buf2 = buf + vd->vd_aux; 3466 j = 0; 3467 while (buf2 + sizeof(Elf_Verdaux) <= end && j < vd->vd_cnt) { 3468 vda = (Elf_Verdaux *) (uintptr_t) buf2; 3469 name = get_string(re, s->link, vda->vda_name); 3470 if (j == 0) { 3471 if (dump) 3472 printf(" vda_name: %s\n", name); 3473 SAVE_VERSION_NAME((int)vd->vd_ndx, name, 1); 3474 } else if (dump) 3475 printf(" 0x%4.4lx parent: %s\n", 3476 (unsigned long) (buf2 - 3477 (uint8_t *)d->d_buf), name); 3478 if (vda->vda_next == 0) 3479 break; 3480 buf2 += vda->vda_next; 3481 j++; 3482 } 3483 if (vd->vd_next == 0) 3484 break; 3485 buf += vd->vd_next; 3486 } 3487 } 3488 3489 static void 3490 dump_verneed(struct readelf *re, int dump) 3491 { 3492 struct section *s; 3493 struct symver *nv; 3494 Elf_Data *d; 3495 Elf_Verneed *vn; 3496 Elf_Vernaux *vna; 3497 uint8_t *buf, *end, *buf2; 3498 const char *name; 3499 int elferr, i, j; 3500 3501 if ((s = re->vn_s) == NULL) 3502 return; 3503 if (s->link >= re->shnum) 3504 return; 3505 3506 if (re->ver == NULL) { 3507 re->ver_sz = 16; 3508 if ((re->ver = calloc(re->ver_sz, sizeof(*re->ver))) == 3509 NULL) { 3510 warn("calloc failed"); 3511 return; 3512 } 3513 re->ver[0].name = "*local*"; 3514 re->ver[1].name = "*global*"; 3515 } 3516 3517 if (dump) 3518 printf("\nVersion needed section (%s):\n", s->name); 3519 (void) elf_errno(); 3520 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 3521 elferr = elf_errno(); 3522 if (elferr != 0) 3523 warnx("elf_getdata failed: %s", elf_errmsg(elferr)); 3524 return; 3525 } 3526 if (d->d_size == 0) 3527 return; 3528 3529 buf = d->d_buf; 3530 end = buf + d->d_size; 3531 while (buf + sizeof(Elf_Verneed) <= end) { 3532 vn = (Elf_Verneed *) (uintptr_t) buf; 3533 if (dump) { 3534 printf(" 0x%4.4lx", (unsigned long) 3535 (buf - (uint8_t *)d->d_buf)); 3536 printf(" vn_version: %u vn_file: %s vn_cnt: %u\n", 3537 vn->vn_version, 3538 get_string(re, s->link, vn->vn_file), 3539 vn->vn_cnt); 3540 } 3541 buf2 = buf + vn->vn_aux; 3542 j = 0; 3543 while (buf2 + sizeof(Elf_Vernaux) <= end && j < vn->vn_cnt) { 3544 vna = (Elf32_Vernaux *) (uintptr_t) buf2; 3545 if (dump) 3546 printf(" 0x%4.4lx", (unsigned long) 3547 (buf2 - (uint8_t *)d->d_buf)); 3548 name = get_string(re, s->link, vna->vna_name); 3549 if (dump) 3550 printf(" vna_name: %s vna_flags: %u" 3551 " vna_other: %u\n", name, 3552 vna->vna_flags, vna->vna_other); 3553 SAVE_VERSION_NAME((int)vna->vna_other, name, 0); 3554 if (vna->vna_next == 0) 3555 break; 3556 buf2 += vna->vna_next; 3557 j++; 3558 } 3559 if (vn->vn_next == 0) 3560 break; 3561 buf += vn->vn_next; 3562 } 3563 } 3564 3565 static void 3566 dump_versym(struct readelf *re) 3567 { 3568 int i; 3569 uint16_t vs; 3570 3571 if (re->vs_s == NULL || re->ver == NULL || re->vs == NULL) 3572 return; 3573 printf("\nVersion symbol section (%s):\n", re->vs_s->name); 3574 for (i = 0; i < re->vs_sz; i++) { 3575 if ((i & 3) == 0) { 3576 if (i > 0) 3577 putchar('\n'); 3578 printf(" %03x:", i); 3579 } 3580 vs = re->vs[i] & VERSYM_VERSION; 3581 if (vs >= re->ver_sz || re->ver[vs].name == NULL) { 3582 warnx("invalid versym version index %u", re->vs[i]); 3583 break; 3584 } 3585 if (re->vs[i] & VERSYM_HIDDEN) 3586 printf(" %3xh %-12s ", vs, 3587 re->ver[re->vs[i] & VERSYM_VERSION].name); 3588 else 3589 printf(" %3x %-12s ", vs, re->ver[re->vs[i]].name); 3590 } 3591 putchar('\n'); 3592 } 3593 3594 static void 3595 dump_ver(struct readelf *re) 3596 { 3597 3598 if (re->vs_s && re->ver && re->vs) 3599 dump_versym(re); 3600 if (re->vd_s) 3601 dump_verdef(re, 1); 3602 if (re->vn_s) 3603 dump_verneed(re, 1); 3604 } 3605 3606 static void 3607 search_ver(struct readelf *re) 3608 { 3609 struct section *s; 3610 Elf_Data *d; 3611 int elferr, i; 3612 3613 for (i = 0; (size_t) i < re->shnum; i++) { 3614 s = &re->sl[i]; 3615 if (s->type == SHT_SUNW_versym) 3616 re->vs_s = s; 3617 if (s->type == SHT_SUNW_verneed) 3618 re->vn_s = s; 3619 if (s->type == SHT_SUNW_verdef) 3620 re->vd_s = s; 3621 } 3622 if (re->vd_s) 3623 dump_verdef(re, 0); 3624 if (re->vn_s) 3625 dump_verneed(re, 0); 3626 if (re->vs_s && re->ver != NULL) { 3627 (void) elf_errno(); 3628 if ((d = elf_getdata(re->vs_s->scn, NULL)) == NULL) { 3629 elferr = elf_errno(); 3630 if (elferr != 0) 3631 warnx("elf_getdata failed: %s", 3632 elf_errmsg(elferr)); 3633 return; 3634 } 3635 if (d->d_size == 0) 3636 return; 3637 re->vs = d->d_buf; 3638 re->vs_sz = d->d_size / sizeof(Elf32_Half); 3639 } 3640 } 3641 3642 #undef Elf_Verdef 3643 #undef Elf_Verdaux 3644 #undef Elf_Verneed 3645 #undef Elf_Vernaux 3646 #undef SAVE_VERSION_NAME 3647 3648 /* 3649 * Elf32_Lib and Elf64_Lib are identical. 3650 */ 3651 #define Elf_Lib Elf32_Lib 3652 3653 static void 3654 dump_liblist(struct readelf *re) 3655 { 3656 struct section *s; 3657 struct tm *t; 3658 time_t ti; 3659 char tbuf[20]; 3660 Elf_Data *d; 3661 Elf_Lib *lib; 3662 int i, j, k, elferr, first, len; 3663 3664 for (i = 0; (size_t) i < re->shnum; i++) { 3665 s = &re->sl[i]; 3666 if (s->type != SHT_GNU_LIBLIST) 3667 continue; 3668 if (s->link >= re->shnum) 3669 continue; 3670 (void) elf_errno(); 3671 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 3672 elferr = elf_errno(); 3673 if (elferr != 0) 3674 warnx("elf_getdata failed: %s", 3675 elf_errmsg(elferr)); 3676 continue; 3677 } 3678 if (d->d_size <= 0) 3679 continue; 3680 lib = d->d_buf; 3681 if (!get_ent_count(s, &len)) 3682 continue; 3683 printf("\nLibrary list section '%s' ", s->name); 3684 printf("contains %d entries:\n", len); 3685 printf("%12s%24s%18s%10s%6s\n", "Library", "Time Stamp", 3686 "Checksum", "Version", "Flags"); 3687 for (j = 0; (uint64_t) j < s->sz / s->entsize; j++) { 3688 printf("%3d: ", j); 3689 printf("%-20.20s ", 3690 get_string(re, s->link, lib->l_name)); 3691 ti = lib->l_time_stamp; 3692 t = gmtime(&ti); 3693 snprintf(tbuf, sizeof(tbuf), "%04d-%02d-%02dT%02d:%02d" 3694 ":%2d", t->tm_year + 1900, t->tm_mon + 1, 3695 t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec); 3696 printf("%-19.19s ", tbuf); 3697 printf("0x%08x ", lib->l_checksum); 3698 printf("%-7d %#x", lib->l_version, lib->l_flags); 3699 if (lib->l_flags != 0) { 3700 first = 1; 3701 putchar('('); 3702 for (k = 0; l_flag[k].name != NULL; k++) { 3703 if ((l_flag[k].value & lib->l_flags) == 3704 0) 3705 continue; 3706 if (!first) 3707 putchar(','); 3708 else 3709 first = 0; 3710 printf("%s", l_flag[k].name); 3711 } 3712 putchar(')'); 3713 } 3714 putchar('\n'); 3715 lib++; 3716 } 3717 } 3718 } 3719 3720 #undef Elf_Lib 3721 3722 static void 3723 dump_section_groups(struct readelf *re) 3724 { 3725 struct section *s; 3726 const char *symname; 3727 Elf_Data *d; 3728 uint32_t *w; 3729 int i, j, elferr; 3730 size_t n; 3731 3732 for (i = 0; (size_t) i < re->shnum; i++) { 3733 s = &re->sl[i]; 3734 if (s->type != SHT_GROUP) 3735 continue; 3736 if (s->link >= re->shnum) 3737 continue; 3738 (void) elf_errno(); 3739 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 3740 elferr = elf_errno(); 3741 if (elferr != 0) 3742 warnx("elf_getdata failed: %s", 3743 elf_errmsg(elferr)); 3744 continue; 3745 } 3746 if (d->d_size <= 0) 3747 continue; 3748 3749 w = d->d_buf; 3750 3751 /* We only support COMDAT section. */ 3752 #ifndef GRP_COMDAT 3753 #define GRP_COMDAT 0x1 3754 #endif 3755 if ((*w++ & GRP_COMDAT) == 0) 3756 return; 3757 3758 if (s->entsize == 0) 3759 s->entsize = 4; 3760 3761 symname = get_symbol_name(re, s->link, s->info); 3762 n = s->sz / s->entsize; 3763 if (n-- < 1) 3764 return; 3765 3766 printf("\nCOMDAT group section [%5d] `%s' [%s] contains %ju" 3767 " sections:\n", i, s->name, symname, (uintmax_t)n); 3768 printf(" %-10.10s %s\n", "[Index]", "Name"); 3769 for (j = 0; (size_t) j < n; j++, w++) { 3770 if (*w >= re->shnum) { 3771 warnx("invalid section index: %u", *w); 3772 continue; 3773 } 3774 printf(" [%5u] %s\n", *w, re->sl[*w].name); 3775 } 3776 } 3777 } 3778 3779 static uint8_t * 3780 dump_unknown_tag(uint64_t tag, uint8_t *p, uint8_t *pe) 3781 { 3782 uint64_t val; 3783 3784 /* 3785 * According to ARM EABI: For tags > 32, even numbered tags have 3786 * a ULEB128 param and odd numbered ones have NUL-terminated 3787 * string param. This rule probably also applies for tags <= 32 3788 * if the object arch is not ARM. 3789 */ 3790 3791 printf(" Tag_unknown_%ju: ", (uintmax_t) tag); 3792 3793 if (tag & 1) { 3794 printf("%s\n", (char *) p); 3795 p += strlen((char *) p) + 1; 3796 } else { 3797 val = _decode_uleb128(&p, pe); 3798 printf("%ju\n", (uintmax_t) val); 3799 } 3800 3801 return (p); 3802 } 3803 3804 static uint8_t * 3805 dump_compatibility_tag(uint8_t *p, uint8_t *pe) 3806 { 3807 uint64_t val; 3808 3809 val = _decode_uleb128(&p, pe); 3810 printf("flag = %ju, vendor = %s\n", (uintmax_t) val, p); 3811 p += strlen((char *) p) + 1; 3812 3813 return (p); 3814 } 3815 3816 static void 3817 dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe) 3818 { 3819 uint64_t tag, val; 3820 size_t i; 3821 int found, desc; 3822 3823 (void) re; 3824 3825 while (p < pe) { 3826 tag = _decode_uleb128(&p, pe); 3827 found = desc = 0; 3828 for (i = 0; i < sizeof(aeabi_tags) / sizeof(aeabi_tags[0]); 3829 i++) { 3830 if (tag == aeabi_tags[i].tag) { 3831 found = 1; 3832 printf(" %s: ", aeabi_tags[i].s_tag); 3833 if (aeabi_tags[i].get_desc) { 3834 desc = 1; 3835 val = _decode_uleb128(&p, pe); 3836 printf("%s\n", 3837 aeabi_tags[i].get_desc(val)); 3838 } 3839 break; 3840 } 3841 if (tag < aeabi_tags[i].tag) 3842 break; 3843 } 3844 if (!found) { 3845 p = dump_unknown_tag(tag, p, pe); 3846 continue; 3847 } 3848 if (desc) 3849 continue; 3850 3851 switch (tag) { 3852 case 4: /* Tag_CPU_raw_name */ 3853 case 5: /* Tag_CPU_name */ 3854 case 67: /* Tag_conformance */ 3855 printf("%s\n", (char *) p); 3856 p += strlen((char *) p) + 1; 3857 break; 3858 case 32: /* Tag_compatibility */ 3859 p = dump_compatibility_tag(p, pe); 3860 break; 3861 case 64: /* Tag_nodefaults */ 3862 /* ignored, written as 0. */ 3863 (void) _decode_uleb128(&p, pe); 3864 printf("True\n"); 3865 break; 3866 case 65: /* Tag_also_compatible_with */ 3867 val = _decode_uleb128(&p, pe); 3868 /* Must be Tag_CPU_arch */ 3869 if (val != 6) { 3870 printf("unknown\n"); 3871 break; 3872 } 3873 val = _decode_uleb128(&p, pe); 3874 printf("%s\n", aeabi_cpu_arch(val)); 3875 /* Skip NUL terminator. */ 3876 p++; 3877 break; 3878 default: 3879 putchar('\n'); 3880 break; 3881 } 3882 } 3883 } 3884 3885 #ifndef Tag_GNU_MIPS_ABI_FP 3886 #define Tag_GNU_MIPS_ABI_FP 4 3887 #endif 3888 3889 static void 3890 dump_mips_attributes(struct readelf *re, uint8_t *p, uint8_t *pe) 3891 { 3892 uint64_t tag, val; 3893 3894 (void) re; 3895 3896 while (p < pe) { 3897 tag = _decode_uleb128(&p, pe); 3898 switch (tag) { 3899 case Tag_GNU_MIPS_ABI_FP: 3900 val = _decode_uleb128(&p, pe); 3901 printf(" Tag_GNU_MIPS_ABI_FP: %s\n", mips_abi_fp(val)); 3902 break; 3903 case 32: /* Tag_compatibility */ 3904 p = dump_compatibility_tag(p, pe); 3905 break; 3906 default: 3907 p = dump_unknown_tag(tag, p, pe); 3908 break; 3909 } 3910 } 3911 } 3912 3913 #ifndef Tag_GNU_Power_ABI_FP 3914 #define Tag_GNU_Power_ABI_FP 4 3915 #endif 3916 3917 #ifndef Tag_GNU_Power_ABI_Vector 3918 #define Tag_GNU_Power_ABI_Vector 8 3919 #endif 3920 3921 static void 3922 dump_ppc_attributes(uint8_t *p, uint8_t *pe) 3923 { 3924 uint64_t tag, val; 3925 3926 while (p < pe) { 3927 tag = _decode_uleb128(&p, pe); 3928 switch (tag) { 3929 case Tag_GNU_Power_ABI_FP: 3930 val = _decode_uleb128(&p, pe); 3931 printf(" Tag_GNU_Power_ABI_FP: %s\n", ppc_abi_fp(val)); 3932 break; 3933 case Tag_GNU_Power_ABI_Vector: 3934 val = _decode_uleb128(&p, pe); 3935 printf(" Tag_GNU_Power_ABI_Vector: %s\n", 3936 ppc_abi_vector(val)); 3937 break; 3938 case 32: /* Tag_compatibility */ 3939 p = dump_compatibility_tag(p, pe); 3940 break; 3941 default: 3942 p = dump_unknown_tag(tag, p, pe); 3943 break; 3944 } 3945 } 3946 } 3947 3948 static void 3949 dump_attributes(struct readelf *re) 3950 { 3951 struct section *s; 3952 Elf_Data *d; 3953 uint8_t *p, *pe, *sp; 3954 size_t len, seclen, nlen, sublen; 3955 uint64_t val; 3956 int tag, i, elferr; 3957 3958 for (i = 0; (size_t) i < re->shnum; i++) { 3959 s = &re->sl[i]; 3960 if (s->type != SHT_GNU_ATTRIBUTES && 3961 (re->ehdr.e_machine != EM_ARM || s->type != SHT_LOPROC + 3)) 3962 continue; 3963 (void) elf_errno(); 3964 if ((d = elf_rawdata(s->scn, NULL)) == NULL) { 3965 elferr = elf_errno(); 3966 if (elferr != 0) 3967 warnx("elf_rawdata failed: %s", 3968 elf_errmsg(elferr)); 3969 continue; 3970 } 3971 if (d->d_size <= 0) 3972 continue; 3973 p = d->d_buf; 3974 pe = p + d->d_size; 3975 if (*p != 'A') { 3976 printf("Unknown Attribute Section Format: %c\n", 3977 (char) *p); 3978 continue; 3979 } 3980 len = d->d_size - 1; 3981 p++; 3982 while (len > 0) { 3983 if (len < 4) { 3984 warnx("truncated attribute section length"); 3985 return; 3986 } 3987 seclen = re->dw_decode(&p, 4); 3988 if (seclen > len) { 3989 warnx("invalid attribute section length"); 3990 return; 3991 } 3992 len -= seclen; 3993 nlen = strlen((char *) p) + 1; 3994 if (nlen + 4 > seclen) { 3995 warnx("invalid attribute section name"); 3996 return; 3997 } 3998 printf("Attribute Section: %s\n", (char *) p); 3999 p += nlen; 4000 seclen -= nlen + 4; 4001 while (seclen > 0) { 4002 sp = p; 4003 tag = *p++; 4004 sublen = re->dw_decode(&p, 4); 4005 if (sublen > seclen) { 4006 warnx("invalid attribute sub-section" 4007 " length"); 4008 return; 4009 } 4010 seclen -= sublen; 4011 printf("%s", top_tag(tag)); 4012 if (tag == 2 || tag == 3) { 4013 putchar(':'); 4014 for (;;) { 4015 val = _decode_uleb128(&p, pe); 4016 if (val == 0) 4017 break; 4018 printf(" %ju", (uintmax_t) val); 4019 } 4020 } 4021 putchar('\n'); 4022 if (re->ehdr.e_machine == EM_ARM && 4023 s->type == SHT_LOPROC + 3) 4024 dump_arm_attributes(re, p, sp + sublen); 4025 else if (re->ehdr.e_machine == EM_MIPS || 4026 re->ehdr.e_machine == EM_MIPS_RS3_LE) 4027 dump_mips_attributes(re, p, 4028 sp + sublen); 4029 else if (re->ehdr.e_machine == EM_PPC) 4030 dump_ppc_attributes(p, sp + sublen); 4031 p = sp + sublen; 4032 } 4033 } 4034 } 4035 } 4036 4037 static void 4038 dump_mips_specific_info(struct readelf *re) 4039 { 4040 struct section *s; 4041 int i, options_found; 4042 4043 options_found = 0; 4044 s = NULL; 4045 for (i = 0; (size_t) i < re->shnum; i++) { 4046 s = &re->sl[i]; 4047 if (s->name != NULL && (!strcmp(s->name, ".MIPS.options") || 4048 (s->type == SHT_MIPS_OPTIONS))) { 4049 dump_mips_options(re, s); 4050 options_found = 1; 4051 } 4052 } 4053 4054 /* 4055 * According to SGI mips64 spec, .reginfo should be ignored if 4056 * .MIPS.options section is present. 4057 */ 4058 if (!options_found) { 4059 for (i = 0; (size_t) i < re->shnum; i++) { 4060 s = &re->sl[i]; 4061 if (s->name != NULL && (!strcmp(s->name, ".reginfo") || 4062 (s->type == SHT_MIPS_REGINFO))) 4063 dump_mips_reginfo(re, s); 4064 } 4065 } 4066 } 4067 4068 static void 4069 dump_mips_reginfo(struct readelf *re, struct section *s) 4070 { 4071 Elf_Data *d; 4072 int elferr, len; 4073 4074 (void) elf_errno(); 4075 if ((d = elf_rawdata(s->scn, NULL)) == NULL) { 4076 elferr = elf_errno(); 4077 if (elferr != 0) 4078 warnx("elf_rawdata failed: %s", 4079 elf_errmsg(elferr)); 4080 return; 4081 } 4082 if (d->d_size <= 0) 4083 return; 4084 if (!get_ent_count(s, &len)) 4085 return; 4086 4087 printf("\nSection '%s' contains %d entries:\n", s->name, len); 4088 dump_mips_odk_reginfo(re, d->d_buf, d->d_size); 4089 } 4090 4091 static void 4092 dump_mips_options(struct readelf *re, struct section *s) 4093 { 4094 Elf_Data *d; 4095 uint32_t info; 4096 uint16_t sndx; 4097 uint8_t *p, *pe; 4098 uint8_t kind, size; 4099 int elferr; 4100 4101 (void) elf_errno(); 4102 if ((d = elf_rawdata(s->scn, NULL)) == NULL) { 4103 elferr = elf_errno(); 4104 if (elferr != 0) 4105 warnx("elf_rawdata failed: %s", 4106 elf_errmsg(elferr)); 4107 return; 4108 } 4109 if (d->d_size == 0) 4110 return; 4111 4112 printf("\nSection %s contains:\n", s->name); 4113 p = d->d_buf; 4114 pe = p + d->d_size; 4115 while (p < pe) { 4116 if (pe - p < 8) { 4117 warnx("Truncated MIPS option header"); 4118 return; 4119 } 4120 kind = re->dw_decode(&p, 1); 4121 size = re->dw_decode(&p, 1); 4122 sndx = re->dw_decode(&p, 2); 4123 info = re->dw_decode(&p, 4); 4124 if (size < 8 || size - 8 > pe - p) { 4125 warnx("Malformed MIPS option header"); 4126 return; 4127 } 4128 size -= 8; 4129 switch (kind) { 4130 case ODK_REGINFO: 4131 dump_mips_odk_reginfo(re, p, size); 4132 break; 4133 case ODK_EXCEPTIONS: 4134 printf(" EXCEPTIONS FPU_MIN: %#x\n", 4135 info & OEX_FPU_MIN); 4136 printf("%11.11s FPU_MAX: %#x\n", "", 4137 info & OEX_FPU_MAX); 4138 dump_mips_option_flags("", mips_exceptions_option, 4139 info); 4140 break; 4141 case ODK_PAD: 4142 printf(" %-10.10s section: %ju\n", "OPAD", 4143 (uintmax_t) sndx); 4144 dump_mips_option_flags("", mips_pad_option, info); 4145 break; 4146 case ODK_HWPATCH: 4147 dump_mips_option_flags("HWPATCH", mips_hwpatch_option, 4148 info); 4149 break; 4150 case ODK_HWAND: 4151 dump_mips_option_flags("HWAND", mips_hwa_option, info); 4152 break; 4153 case ODK_HWOR: 4154 dump_mips_option_flags("HWOR", mips_hwo_option, info); 4155 break; 4156 case ODK_FILL: 4157 printf(" %-10.10s %#jx\n", "FILL", (uintmax_t) info); 4158 break; 4159 case ODK_TAGS: 4160 printf(" %-10.10s\n", "TAGS"); 4161 break; 4162 case ODK_GP_GROUP: 4163 printf(" %-10.10s GP group number: %#x\n", "GP_GROUP", 4164 info & 0xFFFF); 4165 if (info & 0x10000) 4166 printf(" %-10.10s GP group is " 4167 "self-contained\n", ""); 4168 break; 4169 case ODK_IDENT: 4170 printf(" %-10.10s default GP group number: %#x\n", 4171 "IDENT", info & 0xFFFF); 4172 if (info & 0x10000) 4173 printf(" %-10.10s default GP group is " 4174 "self-contained\n", ""); 4175 break; 4176 case ODK_PAGESIZE: 4177 printf(" %-10.10s\n", "PAGESIZE"); 4178 break; 4179 default: 4180 break; 4181 } 4182 p += size; 4183 } 4184 } 4185 4186 static void 4187 dump_mips_option_flags(const char *name, struct mips_option *opt, uint64_t info) 4188 { 4189 int first; 4190 4191 first = 1; 4192 for (; opt->desc != NULL; opt++) { 4193 if (info & opt->flag) { 4194 printf(" %-10.10s %s\n", first ? name : "", 4195 opt->desc); 4196 first = 0; 4197 } 4198 } 4199 } 4200 4201 static void 4202 dump_mips_odk_reginfo(struct readelf *re, uint8_t *p, size_t sz) 4203 { 4204 uint32_t ri_gprmask; 4205 uint32_t ri_cprmask[4]; 4206 uint64_t ri_gp_value; 4207 uint8_t *pe; 4208 int i; 4209 4210 pe = p + sz; 4211 while (p < pe) { 4212 ri_gprmask = re->dw_decode(&p, 4); 4213 /* Skip ri_pad padding field for mips64. */ 4214 if (re->ec == ELFCLASS64) 4215 re->dw_decode(&p, 4); 4216 for (i = 0; i < 4; i++) 4217 ri_cprmask[i] = re->dw_decode(&p, 4); 4218 if (re->ec == ELFCLASS32) 4219 ri_gp_value = re->dw_decode(&p, 4); 4220 else 4221 ri_gp_value = re->dw_decode(&p, 8); 4222 printf(" %s ", option_kind(ODK_REGINFO)); 4223 printf("ri_gprmask: 0x%08jx\n", (uintmax_t) ri_gprmask); 4224 for (i = 0; i < 4; i++) 4225 printf("%11.11s ri_cprmask[%d]: 0x%08jx\n", "", i, 4226 (uintmax_t) ri_cprmask[i]); 4227 printf("%12.12s", ""); 4228 printf("ri_gp_value: %#jx\n", (uintmax_t) ri_gp_value); 4229 } 4230 } 4231 4232 static void 4233 dump_arch_specific_info(struct readelf *re) 4234 { 4235 4236 dump_liblist(re); 4237 dump_attributes(re); 4238 4239 switch (re->ehdr.e_machine) { 4240 case EM_MIPS: 4241 case EM_MIPS_RS3_LE: 4242 dump_mips_specific_info(re); 4243 default: 4244 break; 4245 } 4246 } 4247 4248 static const char * 4249 dwarf_regname(struct readelf *re, unsigned int num) 4250 { 4251 static char rx[32]; 4252 const char *rn; 4253 4254 if ((rn = dwarf_reg(re->ehdr.e_machine, num)) != NULL) 4255 return (rn); 4256 4257 snprintf(rx, sizeof(rx), "r%u", num); 4258 4259 return (rx); 4260 } 4261 4262 static void 4263 dump_dwarf_line(struct readelf *re) 4264 { 4265 struct section *s; 4266 Dwarf_Die die; 4267 Dwarf_Error de; 4268 Dwarf_Half tag, version, pointer_size; 4269 Dwarf_Unsigned offset, endoff, length, hdrlen, dirndx, mtime, fsize; 4270 Dwarf_Small minlen, defstmt, lrange, opbase, oplen; 4271 Elf_Data *d; 4272 char *pn; 4273 uint64_t address, file, line, column, isa, opsize, udelta; 4274 int64_t sdelta; 4275 uint8_t *p, *pe; 4276 int8_t lbase; 4277 int i, is_stmt, dwarf_size, elferr, ret; 4278 4279 printf("\nDump of debug contents of section .debug_line:\n"); 4280 4281 s = NULL; 4282 for (i = 0; (size_t) i < re->shnum; i++) { 4283 s = &re->sl[i]; 4284 if (s->name != NULL && !strcmp(s->name, ".debug_line")) 4285 break; 4286 } 4287 if ((size_t) i >= re->shnum) 4288 return; 4289 4290 (void) elf_errno(); 4291 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 4292 elferr = elf_errno(); 4293 if (elferr != 0) 4294 warnx("elf_getdata failed: %s", elf_errmsg(-1)); 4295 return; 4296 } 4297 if (d->d_size <= 0) 4298 return; 4299 4300 while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL, 4301 NULL, &de)) == DW_DLV_OK) { 4302 die = NULL; 4303 while (dwarf_siblingof(re->dbg, die, &die, &de) == DW_DLV_OK) { 4304 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { 4305 warnx("dwarf_tag failed: %s", 4306 dwarf_errmsg(de)); 4307 return; 4308 } 4309 /* XXX: What about DW_TAG_partial_unit? */ 4310 if (tag == DW_TAG_compile_unit) 4311 break; 4312 } 4313 if (die == NULL) { 4314 warnx("could not find DW_TAG_compile_unit die"); 4315 return; 4316 } 4317 if (dwarf_attrval_unsigned(die, DW_AT_stmt_list, &offset, 4318 &de) != DW_DLV_OK) 4319 continue; 4320 4321 length = re->dw_read(d, &offset, 4); 4322 if (length == 0xffffffff) { 4323 dwarf_size = 8; 4324 length = re->dw_read(d, &offset, 8); 4325 } else 4326 dwarf_size = 4; 4327 4328 if (length > d->d_size - offset) { 4329 warnx("invalid .dwarf_line section"); 4330 continue; 4331 } 4332 4333 endoff = offset + length; 4334 pe = (uint8_t *) d->d_buf + endoff; 4335 version = re->dw_read(d, &offset, 2); 4336 hdrlen = re->dw_read(d, &offset, dwarf_size); 4337 minlen = re->dw_read(d, &offset, 1); 4338 defstmt = re->dw_read(d, &offset, 1); 4339 lbase = re->dw_read(d, &offset, 1); 4340 lrange = re->dw_read(d, &offset, 1); 4341 opbase = re->dw_read(d, &offset, 1); 4342 4343 printf("\n"); 4344 printf(" Length:\t\t\t%ju\n", (uintmax_t) length); 4345 printf(" DWARF version:\t\t%u\n", version); 4346 printf(" Prologue Length:\t\t%ju\n", (uintmax_t) hdrlen); 4347 printf(" Minimum Instruction Length:\t%u\n", minlen); 4348 printf(" Initial value of 'is_stmt':\t%u\n", defstmt); 4349 printf(" Line Base:\t\t\t%d\n", lbase); 4350 printf(" Line Range:\t\t\t%u\n", lrange); 4351 printf(" Opcode Base:\t\t\t%u\n", opbase); 4352 (void) dwarf_get_address_size(re->dbg, &pointer_size, &de); 4353 printf(" (Pointer size:\t\t%u)\n", pointer_size); 4354 4355 printf("\n"); 4356 printf(" Opcodes:\n"); 4357 for (i = 1; i < opbase; i++) { 4358 oplen = re->dw_read(d, &offset, 1); 4359 printf(" Opcode %d has %u args\n", i, oplen); 4360 } 4361 4362 printf("\n"); 4363 printf(" The Directory Table:\n"); 4364 p = (uint8_t *) d->d_buf + offset; 4365 while (*p != '\0') { 4366 printf(" %s\n", (char *) p); 4367 p += strlen((char *) p) + 1; 4368 } 4369 4370 p++; 4371 printf("\n"); 4372 printf(" The File Name Table:\n"); 4373 printf(" Entry\tDir\tTime\tSize\tName\n"); 4374 i = 0; 4375 while (*p != '\0') { 4376 i++; 4377 pn = (char *) p; 4378 p += strlen(pn) + 1; 4379 dirndx = _decode_uleb128(&p, pe); 4380 mtime = _decode_uleb128(&p, pe); 4381 fsize = _decode_uleb128(&p, pe); 4382 printf(" %d\t%ju\t%ju\t%ju\t%s\n", i, 4383 (uintmax_t) dirndx, (uintmax_t) mtime, 4384 (uintmax_t) fsize, pn); 4385 } 4386 4387 #define RESET_REGISTERS \ 4388 do { \ 4389 address = 0; \ 4390 file = 1; \ 4391 line = 1; \ 4392 column = 0; \ 4393 is_stmt = defstmt; \ 4394 } while(0) 4395 4396 #define LINE(x) (lbase + (((x) - opbase) % lrange)) 4397 #define ADDRESS(x) ((((x) - opbase) / lrange) * minlen) 4398 4399 p++; 4400 printf("\n"); 4401 printf(" Line Number Statements:\n"); 4402 4403 RESET_REGISTERS; 4404 4405 while (p < pe) { 4406 4407 if (*p == 0) { 4408 /* 4409 * Extended Opcodes. 4410 */ 4411 p++; 4412 opsize = _decode_uleb128(&p, pe); 4413 printf(" Extended opcode %u: ", *p); 4414 switch (*p) { 4415 case DW_LNE_end_sequence: 4416 p++; 4417 RESET_REGISTERS; 4418 printf("End of Sequence\n"); 4419 break; 4420 case DW_LNE_set_address: 4421 p++; 4422 address = re->dw_decode(&p, 4423 pointer_size); 4424 printf("set Address to %#jx\n", 4425 (uintmax_t) address); 4426 break; 4427 case DW_LNE_define_file: 4428 p++; 4429 pn = (char *) p; 4430 p += strlen(pn) + 1; 4431 dirndx = _decode_uleb128(&p, pe); 4432 mtime = _decode_uleb128(&p, pe); 4433 fsize = _decode_uleb128(&p, pe); 4434 printf("define new file: %s\n", pn); 4435 break; 4436 default: 4437 /* Unrecognized extened opcodes. */ 4438 p += opsize; 4439 printf("unknown opcode\n"); 4440 } 4441 } else if (*p > 0 && *p < opbase) { 4442 /* 4443 * Standard Opcodes. 4444 */ 4445 switch(*p++) { 4446 case DW_LNS_copy: 4447 printf(" Copy\n"); 4448 break; 4449 case DW_LNS_advance_pc: 4450 udelta = _decode_uleb128(&p, pe) * 4451 minlen; 4452 address += udelta; 4453 printf(" Advance PC by %ju to %#jx\n", 4454 (uintmax_t) udelta, 4455 (uintmax_t) address); 4456 break; 4457 case DW_LNS_advance_line: 4458 sdelta = _decode_sleb128(&p, pe); 4459 line += sdelta; 4460 printf(" Advance Line by %jd to %ju\n", 4461 (intmax_t) sdelta, 4462 (uintmax_t) line); 4463 break; 4464 case DW_LNS_set_file: 4465 file = _decode_uleb128(&p, pe); 4466 printf(" Set File to %ju\n", 4467 (uintmax_t) file); 4468 break; 4469 case DW_LNS_set_column: 4470 column = _decode_uleb128(&p, pe); 4471 printf(" Set Column to %ju\n", 4472 (uintmax_t) column); 4473 break; 4474 case DW_LNS_negate_stmt: 4475 is_stmt = !is_stmt; 4476 printf(" Set is_stmt to %d\n", is_stmt); 4477 break; 4478 case DW_LNS_set_basic_block: 4479 printf(" Set basic block flag\n"); 4480 break; 4481 case DW_LNS_const_add_pc: 4482 address += ADDRESS(255); 4483 printf(" Advance PC by constant %ju" 4484 " to %#jx\n", 4485 (uintmax_t) ADDRESS(255), 4486 (uintmax_t) address); 4487 break; 4488 case DW_LNS_fixed_advance_pc: 4489 udelta = re->dw_decode(&p, 2); 4490 address += udelta; 4491 printf(" Advance PC by fixed value " 4492 "%ju to %#jx\n", 4493 (uintmax_t) udelta, 4494 (uintmax_t) address); 4495 break; 4496 case DW_LNS_set_prologue_end: 4497 printf(" Set prologue end flag\n"); 4498 break; 4499 case DW_LNS_set_epilogue_begin: 4500 printf(" Set epilogue begin flag\n"); 4501 break; 4502 case DW_LNS_set_isa: 4503 isa = _decode_uleb128(&p, pe); 4504 printf(" Set isa to %ju\n", 4505 (uintmax_t) isa); 4506 break; 4507 default: 4508 /* Unrecognized extended opcodes. */ 4509 printf(" Unknown extended opcode %u\n", 4510 *(p - 1)); 4511 break; 4512 } 4513 4514 } else { 4515 /* 4516 * Special Opcodes. 4517 */ 4518 line += LINE(*p); 4519 address += ADDRESS(*p); 4520 printf(" Special opcode %u: advance Address " 4521 "by %ju to %#jx and Line by %jd to %ju\n", 4522 *p - opbase, (uintmax_t) ADDRESS(*p), 4523 (uintmax_t) address, (intmax_t) LINE(*p), 4524 (uintmax_t) line); 4525 p++; 4526 } 4527 4528 4529 } 4530 } 4531 if (ret == DW_DLV_ERROR) 4532 warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de)); 4533 4534 #undef RESET_REGISTERS 4535 #undef LINE 4536 #undef ADDRESS 4537 } 4538 4539 static void 4540 dump_dwarf_line_decoded(struct readelf *re) 4541 { 4542 Dwarf_Die die; 4543 Dwarf_Line *linebuf, ln; 4544 Dwarf_Addr lineaddr; 4545 Dwarf_Signed linecount, srccount; 4546 Dwarf_Unsigned lineno, fn; 4547 Dwarf_Error de; 4548 const char *dir, *file; 4549 char **srcfiles; 4550 int i, ret; 4551 4552 printf("Decoded dump of debug contents of section .debug_line:\n\n"); 4553 while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL, 4554 NULL, &de)) == DW_DLV_OK) { 4555 if (dwarf_siblingof(re->dbg, NULL, &die, &de) != DW_DLV_OK) 4556 continue; 4557 if (dwarf_attrval_string(die, DW_AT_name, &file, &de) != 4558 DW_DLV_OK) 4559 file = NULL; 4560 if (dwarf_attrval_string(die, DW_AT_comp_dir, &dir, &de) != 4561 DW_DLV_OK) 4562 dir = NULL; 4563 printf("CU: "); 4564 if (dir && file) 4565 printf("%s/", dir); 4566 if (file) 4567 printf("%s", file); 4568 putchar('\n'); 4569 printf("%-37s %11s %s\n", "Filename", "Line Number", 4570 "Starting Address"); 4571 if (dwarf_srclines(die, &linebuf, &linecount, &de) != DW_DLV_OK) 4572 continue; 4573 if (dwarf_srcfiles(die, &srcfiles, &srccount, &de) != DW_DLV_OK) 4574 continue; 4575 for (i = 0; i < linecount; i++) { 4576 ln = linebuf[i]; 4577 if (dwarf_line_srcfileno(ln, &fn, &de) != DW_DLV_OK) 4578 continue; 4579 if (dwarf_lineno(ln, &lineno, &de) != DW_DLV_OK) 4580 continue; 4581 if (dwarf_lineaddr(ln, &lineaddr, &de) != DW_DLV_OK) 4582 continue; 4583 printf("%-37s %11ju %#18jx\n", 4584 basename(srcfiles[fn - 1]), (uintmax_t) lineno, 4585 (uintmax_t) lineaddr); 4586 } 4587 putchar('\n'); 4588 } 4589 } 4590 4591 static void 4592 dump_dwarf_die(struct readelf *re, Dwarf_Die die, int level) 4593 { 4594 Dwarf_Attribute *attr_list; 4595 Dwarf_Die ret_die; 4596 Dwarf_Off dieoff, cuoff, culen, attroff; 4597 Dwarf_Unsigned ate, lang, v_udata, v_sig; 4598 Dwarf_Signed attr_count, v_sdata; 4599 Dwarf_Off v_off; 4600 Dwarf_Addr v_addr; 4601 Dwarf_Half tag, attr, form; 4602 Dwarf_Block *v_block; 4603 Dwarf_Bool v_bool, is_info; 4604 Dwarf_Sig8 v_sig8; 4605 Dwarf_Error de; 4606 Dwarf_Ptr v_expr; 4607 const char *tag_str, *attr_str, *ate_str, *lang_str; 4608 char unk_tag[32], unk_attr[32]; 4609 char *v_str; 4610 uint8_t *b, *p; 4611 int i, j, abc, ret; 4612 4613 if (dwarf_dieoffset(die, &dieoff, &de) != DW_DLV_OK) { 4614 warnx("dwarf_dieoffset failed: %s", dwarf_errmsg(de)); 4615 goto cont_search; 4616 } 4617 4618 printf(" <%d><%jx>: ", level, (uintmax_t) dieoff); 4619 4620 if (dwarf_die_CU_offset_range(die, &cuoff, &culen, &de) != DW_DLV_OK) { 4621 warnx("dwarf_die_CU_offset_range failed: %s", 4622 dwarf_errmsg(de)); 4623 cuoff = 0; 4624 } 4625 4626 abc = dwarf_die_abbrev_code(die); 4627 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { 4628 warnx("dwarf_tag failed: %s", dwarf_errmsg(de)); 4629 goto cont_search; 4630 } 4631 if (dwarf_get_TAG_name(tag, &tag_str) != DW_DLV_OK) { 4632 snprintf(unk_tag, sizeof(unk_tag), "[Unknown Tag: %#x]", tag); 4633 tag_str = unk_tag; 4634 } 4635 4636 printf("Abbrev Number: %d (%s)\n", abc, tag_str); 4637 4638 if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) != 4639 DW_DLV_OK) { 4640 if (ret == DW_DLV_ERROR) 4641 warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de)); 4642 goto cont_search; 4643 } 4644 4645 for (i = 0; i < attr_count; i++) { 4646 if (dwarf_whatform(attr_list[i], &form, &de) != DW_DLV_OK) { 4647 warnx("dwarf_whatform failed: %s", dwarf_errmsg(de)); 4648 continue; 4649 } 4650 if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) { 4651 warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de)); 4652 continue; 4653 } 4654 if (dwarf_get_AT_name(attr, &attr_str) != DW_DLV_OK) { 4655 snprintf(unk_attr, sizeof(unk_attr), 4656 "[Unknown AT: %#x]", attr); 4657 attr_str = unk_attr; 4658 } 4659 if (dwarf_attroffset(attr_list[i], &attroff, &de) != 4660 DW_DLV_OK) { 4661 warnx("dwarf_attroffset failed: %s", dwarf_errmsg(de)); 4662 attroff = 0; 4663 } 4664 printf(" <%jx> %-18s: ", (uintmax_t) attroff, attr_str); 4665 switch (form) { 4666 case DW_FORM_ref_addr: 4667 case DW_FORM_sec_offset: 4668 if (dwarf_global_formref(attr_list[i], &v_off, &de) != 4669 DW_DLV_OK) { 4670 warnx("dwarf_global_formref failed: %s", 4671 dwarf_errmsg(de)); 4672 continue; 4673 } 4674 if (form == DW_FORM_ref_addr) 4675 printf("<0x%jx>", (uintmax_t) v_off); 4676 else 4677 printf("0x%jx", (uintmax_t) v_off); 4678 break; 4679 4680 case DW_FORM_ref1: 4681 case DW_FORM_ref2: 4682 case DW_FORM_ref4: 4683 case DW_FORM_ref8: 4684 case DW_FORM_ref_udata: 4685 if (dwarf_formref(attr_list[i], &v_off, &de) != 4686 DW_DLV_OK) { 4687 warnx("dwarf_formref failed: %s", 4688 dwarf_errmsg(de)); 4689 continue; 4690 } 4691 v_off += cuoff; 4692 printf("<0x%jx>", (uintmax_t) v_off); 4693 break; 4694 4695 case DW_FORM_addr: 4696 if (dwarf_formaddr(attr_list[i], &v_addr, &de) != 4697 DW_DLV_OK) { 4698 warnx("dwarf_formaddr failed: %s", 4699 dwarf_errmsg(de)); 4700 continue; 4701 } 4702 printf("%#jx", (uintmax_t) v_addr); 4703 break; 4704 4705 case DW_FORM_data1: 4706 case DW_FORM_data2: 4707 case DW_FORM_data4: 4708 case DW_FORM_data8: 4709 case DW_FORM_udata: 4710 if (dwarf_formudata(attr_list[i], &v_udata, &de) != 4711 DW_DLV_OK) { 4712 warnx("dwarf_formudata failed: %s", 4713 dwarf_errmsg(de)); 4714 continue; 4715 } 4716 if (attr == DW_AT_high_pc) 4717 printf("0x%jx", (uintmax_t) v_udata); 4718 else 4719 printf("%ju", (uintmax_t) v_udata); 4720 break; 4721 4722 case DW_FORM_sdata: 4723 if (dwarf_formsdata(attr_list[i], &v_sdata, &de) != 4724 DW_DLV_OK) { 4725 warnx("dwarf_formudata failed: %s", 4726 dwarf_errmsg(de)); 4727 continue; 4728 } 4729 printf("%jd", (intmax_t) v_sdata); 4730 break; 4731 4732 case DW_FORM_flag: 4733 if (dwarf_formflag(attr_list[i], &v_bool, &de) != 4734 DW_DLV_OK) { 4735 warnx("dwarf_formflag failed: %s", 4736 dwarf_errmsg(de)); 4737 continue; 4738 } 4739 printf("%jd", (intmax_t) v_bool); 4740 break; 4741 4742 case DW_FORM_flag_present: 4743 putchar('1'); 4744 break; 4745 4746 case DW_FORM_string: 4747 case DW_FORM_strp: 4748 if (dwarf_formstring(attr_list[i], &v_str, &de) != 4749 DW_DLV_OK) { 4750 warnx("dwarf_formstring failed: %s", 4751 dwarf_errmsg(de)); 4752 continue; 4753 } 4754 if (form == DW_FORM_string) 4755 printf("%s", v_str); 4756 else 4757 printf("(indirect string) %s", v_str); 4758 break; 4759 4760 case DW_FORM_block: 4761 case DW_FORM_block1: 4762 case DW_FORM_block2: 4763 case DW_FORM_block4: 4764 if (dwarf_formblock(attr_list[i], &v_block, &de) != 4765 DW_DLV_OK) { 4766 warnx("dwarf_formblock failed: %s", 4767 dwarf_errmsg(de)); 4768 continue; 4769 } 4770 printf("%ju byte block:", (uintmax_t) v_block->bl_len); 4771 b = v_block->bl_data; 4772 for (j = 0; (Dwarf_Unsigned) j < v_block->bl_len; j++) 4773 printf(" %x", b[j]); 4774 printf("\t("); 4775 dump_dwarf_block(re, v_block->bl_data, v_block->bl_len); 4776 putchar(')'); 4777 break; 4778 4779 case DW_FORM_exprloc: 4780 if (dwarf_formexprloc(attr_list[i], &v_udata, &v_expr, 4781 &de) != DW_DLV_OK) { 4782 warnx("dwarf_formexprloc failed: %s", 4783 dwarf_errmsg(de)); 4784 continue; 4785 } 4786 printf("%ju byte block:", (uintmax_t) v_udata); 4787 b = v_expr; 4788 for (j = 0; (Dwarf_Unsigned) j < v_udata; j++) 4789 printf(" %x", b[j]); 4790 printf("\t("); 4791 dump_dwarf_block(re, v_expr, v_udata); 4792 putchar(')'); 4793 break; 4794 4795 case DW_FORM_ref_sig8: 4796 if (dwarf_formsig8(attr_list[i], &v_sig8, &de) != 4797 DW_DLV_OK) { 4798 warnx("dwarf_formsig8 failed: %s", 4799 dwarf_errmsg(de)); 4800 continue; 4801 } 4802 p = (uint8_t *)(uintptr_t) &v_sig8.signature[0]; 4803 v_sig = re->dw_decode(&p, 8); 4804 printf("signature: 0x%jx", (uintmax_t) v_sig); 4805 } 4806 switch (attr) { 4807 case DW_AT_encoding: 4808 if (dwarf_attrval_unsigned(die, attr, &ate, &de) != 4809 DW_DLV_OK) 4810 break; 4811 if (dwarf_get_ATE_name(ate, &ate_str) != DW_DLV_OK) 4812 ate_str = "DW_ATE_UNKNOWN"; 4813 printf("\t(%s)", &ate_str[strlen("DW_ATE_")]); 4814 break; 4815 4816 case DW_AT_language: 4817 if (dwarf_attrval_unsigned(die, attr, &lang, &de) != 4818 DW_DLV_OK) 4819 break; 4820 if (dwarf_get_LANG_name(lang, &lang_str) != DW_DLV_OK) 4821 break; 4822 printf("\t(%s)", &lang_str[strlen("DW_LANG_")]); 4823 break; 4824 4825 case DW_AT_location: 4826 case DW_AT_string_length: 4827 case DW_AT_return_addr: 4828 case DW_AT_data_member_location: 4829 case DW_AT_frame_base: 4830 case DW_AT_segment: 4831 case DW_AT_static_link: 4832 case DW_AT_use_location: 4833 case DW_AT_vtable_elem_location: 4834 switch (form) { 4835 case DW_FORM_data4: 4836 case DW_FORM_data8: 4837 case DW_FORM_sec_offset: 4838 printf("\t(location list)"); 4839 break; 4840 default: 4841 break; 4842 } 4843 4844 default: 4845 break; 4846 } 4847 putchar('\n'); 4848 } 4849 4850 4851 cont_search: 4852 /* Search children. */ 4853 ret = dwarf_child(die, &ret_die, &de); 4854 if (ret == DW_DLV_ERROR) 4855 warnx("dwarf_child: %s", dwarf_errmsg(de)); 4856 else if (ret == DW_DLV_OK) 4857 dump_dwarf_die(re, ret_die, level + 1); 4858 4859 /* Search sibling. */ 4860 is_info = dwarf_get_die_infotypes_flag(die); 4861 ret = dwarf_siblingof_b(re->dbg, die, &ret_die, is_info, &de); 4862 if (ret == DW_DLV_ERROR) 4863 warnx("dwarf_siblingof: %s", dwarf_errmsg(de)); 4864 else if (ret == DW_DLV_OK) 4865 dump_dwarf_die(re, ret_die, level); 4866 4867 dwarf_dealloc(re->dbg, die, DW_DLA_DIE); 4868 } 4869 4870 static void 4871 set_cu_context(struct readelf *re, Dwarf_Half psize, Dwarf_Half osize, 4872 Dwarf_Half ver) 4873 { 4874 4875 re->cu_psize = psize; 4876 re->cu_osize = osize; 4877 re->cu_ver = ver; 4878 } 4879 4880 static void 4881 dump_dwarf_info(struct readelf *re, Dwarf_Bool is_info) 4882 { 4883 struct section *s; 4884 Dwarf_Die die; 4885 Dwarf_Error de; 4886 Dwarf_Half tag, version, pointer_size, off_size; 4887 Dwarf_Off cu_offset, cu_length; 4888 Dwarf_Off aboff; 4889 Dwarf_Unsigned typeoff; 4890 Dwarf_Sig8 sig8; 4891 Dwarf_Unsigned sig; 4892 uint8_t *p; 4893 const char *sn; 4894 int i, ret; 4895 4896 sn = is_info ? ".debug_info" : ".debug_types"; 4897 4898 s = NULL; 4899 for (i = 0; (size_t) i < re->shnum; i++) { 4900 s = &re->sl[i]; 4901 if (s->name != NULL && !strcmp(s->name, sn)) 4902 break; 4903 } 4904 if ((size_t) i >= re->shnum) 4905 return; 4906 4907 do { 4908 printf("\nDump of debug contents of section %s:\n", sn); 4909 4910 while ((ret = dwarf_next_cu_header_c(re->dbg, is_info, NULL, 4911 &version, &aboff, &pointer_size, &off_size, NULL, &sig8, 4912 &typeoff, NULL, &de)) == DW_DLV_OK) { 4913 set_cu_context(re, pointer_size, off_size, version); 4914 die = NULL; 4915 while (dwarf_siblingof_b(re->dbg, die, &die, is_info, 4916 &de) == DW_DLV_OK) { 4917 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { 4918 warnx("dwarf_tag failed: %s", 4919 dwarf_errmsg(de)); 4920 continue; 4921 } 4922 /* XXX: What about DW_TAG_partial_unit? */ 4923 if ((is_info && tag == DW_TAG_compile_unit) || 4924 (!is_info && tag == DW_TAG_type_unit)) 4925 break; 4926 } 4927 if (die == NULL && is_info) { 4928 warnx("could not find DW_TAG_compile_unit " 4929 "die"); 4930 continue; 4931 } else if (die == NULL && !is_info) { 4932 warnx("could not find DW_TAG_type_unit die"); 4933 continue; 4934 } 4935 4936 if (dwarf_die_CU_offset_range(die, &cu_offset, 4937 &cu_length, &de) != DW_DLV_OK) { 4938 warnx("dwarf_die_CU_offset failed: %s", 4939 dwarf_errmsg(de)); 4940 continue; 4941 } 4942 4943 cu_length -= off_size == 4 ? 4 : 12; 4944 4945 sig = 0; 4946 if (!is_info) { 4947 p = (uint8_t *)(uintptr_t) &sig8.signature[0]; 4948 sig = re->dw_decode(&p, 8); 4949 } 4950 4951 printf("\n Type Unit @ offset 0x%jx:\n", 4952 (uintmax_t) cu_offset); 4953 printf(" Length:\t\t%#jx (%d-bit)\n", 4954 (uintmax_t) cu_length, off_size == 4 ? 32 : 64); 4955 printf(" Version:\t\t%u\n", version); 4956 printf(" Abbrev Offset:\t0x%jx\n", 4957 (uintmax_t) aboff); 4958 printf(" Pointer Size:\t%u\n", pointer_size); 4959 if (!is_info) { 4960 printf(" Signature:\t\t0x%016jx\n", 4961 (uintmax_t) sig); 4962 printf(" Type Offset:\t0x%jx\n", 4963 (uintmax_t) typeoff); 4964 } 4965 4966 dump_dwarf_die(re, die, 0); 4967 } 4968 if (ret == DW_DLV_ERROR) 4969 warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de)); 4970 if (is_info) 4971 break; 4972 } while (dwarf_next_types_section(re->dbg, &de) == DW_DLV_OK); 4973 } 4974 4975 static void 4976 dump_dwarf_abbrev(struct readelf *re) 4977 { 4978 Dwarf_Abbrev ab; 4979 Dwarf_Off aboff, atoff; 4980 Dwarf_Unsigned length, attr_count; 4981 Dwarf_Signed flag, form; 4982 Dwarf_Half tag, attr; 4983 Dwarf_Error de; 4984 const char *tag_str, *attr_str, *form_str; 4985 char unk_tag[32], unk_attr[32], unk_form[32]; 4986 int i, j, ret; 4987 4988 printf("\nContents of section .debug_abbrev:\n\n"); 4989 4990 while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, &aboff, 4991 NULL, NULL, &de)) == DW_DLV_OK) { 4992 printf(" Number TAG\n"); 4993 i = 0; 4994 while ((ret = dwarf_get_abbrev(re->dbg, aboff, &ab, &length, 4995 &attr_count, &de)) == DW_DLV_OK) { 4996 if (length == 1) { 4997 dwarf_dealloc(re->dbg, ab, DW_DLA_ABBREV); 4998 break; 4999 } 5000 aboff += length; 5001 printf("%4d", ++i); 5002 if (dwarf_get_abbrev_tag(ab, &tag, &de) != DW_DLV_OK) { 5003 warnx("dwarf_get_abbrev_tag failed: %s", 5004 dwarf_errmsg(de)); 5005 goto next_abbrev; 5006 } 5007 if (dwarf_get_TAG_name(tag, &tag_str) != DW_DLV_OK) { 5008 snprintf(unk_tag, sizeof(unk_tag), 5009 "[Unknown Tag: %#x]", tag); 5010 tag_str = unk_tag; 5011 } 5012 if (dwarf_get_abbrev_children_flag(ab, &flag, &de) != 5013 DW_DLV_OK) { 5014 warnx("dwarf_get_abbrev_children_flag failed:" 5015 " %s", dwarf_errmsg(de)); 5016 goto next_abbrev; 5017 } 5018 printf(" %s %s\n", tag_str, 5019 flag ? "[has children]" : "[no children]"); 5020 for (j = 0; (Dwarf_Unsigned) j < attr_count; j++) { 5021 if (dwarf_get_abbrev_entry(ab, (Dwarf_Signed) j, 5022 &attr, &form, &atoff, &de) != DW_DLV_OK) { 5023 warnx("dwarf_get_abbrev_entry failed:" 5024 " %s", dwarf_errmsg(de)); 5025 continue; 5026 } 5027 if (dwarf_get_AT_name(attr, &attr_str) != 5028 DW_DLV_OK) { 5029 snprintf(unk_attr, sizeof(unk_attr), 5030 "[Unknown AT: %#x]", attr); 5031 attr_str = unk_attr; 5032 } 5033 if (dwarf_get_FORM_name(form, &form_str) != 5034 DW_DLV_OK) { 5035 snprintf(unk_form, sizeof(unk_form), 5036 "[Unknown Form: %#x]", 5037 (Dwarf_Half) form); 5038 form_str = unk_form; 5039 } 5040 printf(" %-18s %s\n", attr_str, form_str); 5041 } 5042 next_abbrev: 5043 dwarf_dealloc(re->dbg, ab, DW_DLA_ABBREV); 5044 } 5045 if (ret != DW_DLV_OK) 5046 warnx("dwarf_get_abbrev: %s", dwarf_errmsg(de)); 5047 } 5048 if (ret == DW_DLV_ERROR) 5049 warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de)); 5050 } 5051 5052 static void 5053 dump_dwarf_pubnames(struct readelf *re) 5054 { 5055 struct section *s; 5056 Dwarf_Off die_off; 5057 Dwarf_Unsigned offset, length, nt_cu_offset, nt_cu_length; 5058 Dwarf_Signed cnt; 5059 Dwarf_Global *globs; 5060 Dwarf_Half nt_version; 5061 Dwarf_Error de; 5062 Elf_Data *d; 5063 char *glob_name; 5064 int i, dwarf_size, elferr; 5065 5066 printf("\nContents of the .debug_pubnames section:\n"); 5067 5068 s = NULL; 5069 for (i = 0; (size_t) i < re->shnum; i++) { 5070 s = &re->sl[i]; 5071 if (s->name != NULL && !strcmp(s->name, ".debug_pubnames")) 5072 break; 5073 } 5074 if ((size_t) i >= re->shnum) 5075 return; 5076 5077 (void) elf_errno(); 5078 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 5079 elferr = elf_errno(); 5080 if (elferr != 0) 5081 warnx("elf_getdata failed: %s", elf_errmsg(-1)); 5082 return; 5083 } 5084 if (d->d_size <= 0) 5085 return; 5086 5087 /* Read in .debug_pubnames section table header. */ 5088 offset = 0; 5089 length = re->dw_read(d, &offset, 4); 5090 if (length == 0xffffffff) { 5091 dwarf_size = 8; 5092 length = re->dw_read(d, &offset, 8); 5093 } else 5094 dwarf_size = 4; 5095 5096 if (length > d->d_size - offset) { 5097 warnx("invalid .dwarf_pubnames section"); 5098 return; 5099 } 5100 5101 nt_version = re->dw_read(d, &offset, 2); 5102 nt_cu_offset = re->dw_read(d, &offset, dwarf_size); 5103 nt_cu_length = re->dw_read(d, &offset, dwarf_size); 5104 printf(" Length:\t\t\t\t%ju\n", (uintmax_t) length); 5105 printf(" Version:\t\t\t\t%u\n", nt_version); 5106 printf(" Offset into .debug_info section:\t%ju\n", 5107 (uintmax_t) nt_cu_offset); 5108 printf(" Size of area in .debug_info section:\t%ju\n", 5109 (uintmax_t) nt_cu_length); 5110 5111 if (dwarf_get_globals(re->dbg, &globs, &cnt, &de) != DW_DLV_OK) { 5112 warnx("dwarf_get_globals failed: %s", dwarf_errmsg(de)); 5113 return; 5114 } 5115 5116 printf("\n Offset Name\n"); 5117 for (i = 0; i < cnt; i++) { 5118 if (dwarf_globname(globs[i], &glob_name, &de) != DW_DLV_OK) { 5119 warnx("dwarf_globname failed: %s", dwarf_errmsg(de)); 5120 continue; 5121 } 5122 if (dwarf_global_die_offset(globs[i], &die_off, &de) != 5123 DW_DLV_OK) { 5124 warnx("dwarf_global_die_offset failed: %s", 5125 dwarf_errmsg(de)); 5126 continue; 5127 } 5128 printf(" %-11ju %s\n", (uintmax_t) die_off, glob_name); 5129 } 5130 } 5131 5132 static void 5133 dump_dwarf_aranges(struct readelf *re) 5134 { 5135 struct section *s; 5136 Dwarf_Arange *aranges; 5137 Dwarf_Addr start; 5138 Dwarf_Unsigned offset, length, as_cu_offset; 5139 Dwarf_Off die_off; 5140 Dwarf_Signed cnt; 5141 Dwarf_Half as_version, as_addrsz, as_segsz; 5142 Dwarf_Error de; 5143 Elf_Data *d; 5144 int i, dwarf_size, elferr; 5145 5146 printf("\nContents of section .debug_aranges:\n"); 5147 5148 s = NULL; 5149 for (i = 0; (size_t) i < re->shnum; i++) { 5150 s = &re->sl[i]; 5151 if (s->name != NULL && !strcmp(s->name, ".debug_aranges")) 5152 break; 5153 } 5154 if ((size_t) i >= re->shnum) 5155 return; 5156 5157 (void) elf_errno(); 5158 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 5159 elferr = elf_errno(); 5160 if (elferr != 0) 5161 warnx("elf_getdata failed: %s", elf_errmsg(-1)); 5162 return; 5163 } 5164 if (d->d_size <= 0) 5165 return; 5166 5167 /* Read in the .debug_aranges section table header. */ 5168 offset = 0; 5169 length = re->dw_read(d, &offset, 4); 5170 if (length == 0xffffffff) { 5171 dwarf_size = 8; 5172 length = re->dw_read(d, &offset, 8); 5173 } else 5174 dwarf_size = 4; 5175 5176 if (length > d->d_size - offset) { 5177 warnx("invalid .dwarf_aranges section"); 5178 return; 5179 } 5180 5181 as_version = re->dw_read(d, &offset, 2); 5182 as_cu_offset = re->dw_read(d, &offset, dwarf_size); 5183 as_addrsz = re->dw_read(d, &offset, 1); 5184 as_segsz = re->dw_read(d, &offset, 1); 5185 5186 printf(" Length:\t\t\t%ju\n", (uintmax_t) length); 5187 printf(" Version:\t\t\t%u\n", as_version); 5188 printf(" Offset into .debug_info:\t%ju\n", (uintmax_t) as_cu_offset); 5189 printf(" Pointer Size:\t\t\t%u\n", as_addrsz); 5190 printf(" Segment Size:\t\t\t%u\n", as_segsz); 5191 5192 if (dwarf_get_aranges(re->dbg, &aranges, &cnt, &de) != DW_DLV_OK) { 5193 warnx("dwarf_get_aranges failed: %s", dwarf_errmsg(de)); 5194 return; 5195 } 5196 5197 printf("\n Address Length\n"); 5198 for (i = 0; i < cnt; i++) { 5199 if (dwarf_get_arange_info(aranges[i], &start, &length, 5200 &die_off, &de) != DW_DLV_OK) { 5201 warnx("dwarf_get_arange_info failed: %s", 5202 dwarf_errmsg(de)); 5203 continue; 5204 } 5205 printf(" %08jx %ju\n", (uintmax_t) start, 5206 (uintmax_t) length); 5207 } 5208 } 5209 5210 static void 5211 dump_dwarf_ranges_foreach(struct readelf *re, Dwarf_Die die, Dwarf_Addr base) 5212 { 5213 Dwarf_Attribute *attr_list; 5214 Dwarf_Ranges *ranges; 5215 Dwarf_Die ret_die; 5216 Dwarf_Error de; 5217 Dwarf_Addr base0; 5218 Dwarf_Half attr; 5219 Dwarf_Signed attr_count, cnt; 5220 Dwarf_Unsigned off, bytecnt; 5221 int i, j, ret; 5222 5223 if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) != 5224 DW_DLV_OK) { 5225 if (ret == DW_DLV_ERROR) 5226 warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de)); 5227 goto cont_search; 5228 } 5229 5230 for (i = 0; i < attr_count; i++) { 5231 if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) { 5232 warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de)); 5233 continue; 5234 } 5235 if (attr != DW_AT_ranges) 5236 continue; 5237 if (dwarf_formudata(attr_list[i], &off, &de) != DW_DLV_OK) { 5238 warnx("dwarf_formudata failed: %s", dwarf_errmsg(de)); 5239 continue; 5240 } 5241 if (dwarf_get_ranges(re->dbg, (Dwarf_Off) off, &ranges, &cnt, 5242 &bytecnt, &de) != DW_DLV_OK) 5243 continue; 5244 base0 = base; 5245 for (j = 0; j < cnt; j++) { 5246 printf(" %08jx ", (uintmax_t) off); 5247 if (ranges[j].dwr_type == DW_RANGES_END) { 5248 printf("%s\n", "<End of list>"); 5249 continue; 5250 } else if (ranges[j].dwr_type == 5251 DW_RANGES_ADDRESS_SELECTION) { 5252 base0 = ranges[j].dwr_addr2; 5253 continue; 5254 } 5255 if (re->ec == ELFCLASS32) 5256 printf("%08jx %08jx\n", 5257 (uintmax_t) (ranges[j].dwr_addr1 + base0), 5258 (uintmax_t) (ranges[j].dwr_addr2 + base0)); 5259 else 5260 printf("%016jx %016jx\n", 5261 (uintmax_t) (ranges[j].dwr_addr1 + base0), 5262 (uintmax_t) (ranges[j].dwr_addr2 + base0)); 5263 } 5264 } 5265 5266 cont_search: 5267 /* Search children. */ 5268 ret = dwarf_child(die, &ret_die, &de); 5269 if (ret == DW_DLV_ERROR) 5270 warnx("dwarf_child: %s", dwarf_errmsg(de)); 5271 else if (ret == DW_DLV_OK) 5272 dump_dwarf_ranges_foreach(re, ret_die, base); 5273 5274 /* Search sibling. */ 5275 ret = dwarf_siblingof(re->dbg, die, &ret_die, &de); 5276 if (ret == DW_DLV_ERROR) 5277 warnx("dwarf_siblingof: %s", dwarf_errmsg(de)); 5278 else if (ret == DW_DLV_OK) 5279 dump_dwarf_ranges_foreach(re, ret_die, base); 5280 } 5281 5282 static void 5283 dump_dwarf_ranges(struct readelf *re) 5284 { 5285 Dwarf_Ranges *ranges; 5286 Dwarf_Die die; 5287 Dwarf_Signed cnt; 5288 Dwarf_Unsigned bytecnt; 5289 Dwarf_Half tag; 5290 Dwarf_Error de; 5291 Dwarf_Unsigned lowpc; 5292 int ret; 5293 5294 if (dwarf_get_ranges(re->dbg, 0, &ranges, &cnt, &bytecnt, &de) != 5295 DW_DLV_OK) 5296 return; 5297 5298 printf("Contents of the .debug_ranges section:\n\n"); 5299 if (re->ec == ELFCLASS32) 5300 printf(" %-8s %-8s %s\n", "Offset", "Begin", "End"); 5301 else 5302 printf(" %-8s %-16s %s\n", "Offset", "Begin", "End"); 5303 5304 while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL, 5305 NULL, &de)) == DW_DLV_OK) { 5306 die = NULL; 5307 if (dwarf_siblingof(re->dbg, die, &die, &de) != DW_DLV_OK) 5308 continue; 5309 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { 5310 warnx("dwarf_tag failed: %s", dwarf_errmsg(de)); 5311 continue; 5312 } 5313 /* XXX: What about DW_TAG_partial_unit? */ 5314 lowpc = 0; 5315 if (tag == DW_TAG_compile_unit) { 5316 if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lowpc, 5317 &de) != DW_DLV_OK) 5318 lowpc = 0; 5319 } 5320 5321 dump_dwarf_ranges_foreach(re, die, (Dwarf_Addr) lowpc); 5322 } 5323 putchar('\n'); 5324 } 5325 5326 static void 5327 dump_dwarf_macinfo(struct readelf *re) 5328 { 5329 Dwarf_Unsigned offset; 5330 Dwarf_Signed cnt; 5331 Dwarf_Macro_Details *md; 5332 Dwarf_Error de; 5333 const char *mi_str; 5334 char unk_mi[32]; 5335 int i; 5336 5337 #define _MAX_MACINFO_ENTRY 65535 5338 5339 printf("\nContents of section .debug_macinfo:\n\n"); 5340 5341 offset = 0; 5342 while (dwarf_get_macro_details(re->dbg, offset, _MAX_MACINFO_ENTRY, 5343 &cnt, &md, &de) == DW_DLV_OK) { 5344 for (i = 0; i < cnt; i++) { 5345 offset = md[i].dmd_offset + 1; 5346 if (md[i].dmd_type == 0) 5347 break; 5348 if (dwarf_get_MACINFO_name(md[i].dmd_type, &mi_str) != 5349 DW_DLV_OK) { 5350 snprintf(unk_mi, sizeof(unk_mi), 5351 "[Unknown MACINFO: %#x]", md[i].dmd_type); 5352 mi_str = unk_mi; 5353 } 5354 printf(" %s", mi_str); 5355 switch (md[i].dmd_type) { 5356 case DW_MACINFO_define: 5357 case DW_MACINFO_undef: 5358 printf(" - lineno : %jd macro : %s\n", 5359 (intmax_t) md[i].dmd_lineno, 5360 md[i].dmd_macro); 5361 break; 5362 case DW_MACINFO_start_file: 5363 printf(" - lineno : %jd filenum : %jd\n", 5364 (intmax_t) md[i].dmd_lineno, 5365 (intmax_t) md[i].dmd_fileindex); 5366 break; 5367 default: 5368 putchar('\n'); 5369 break; 5370 } 5371 } 5372 } 5373 5374 #undef _MAX_MACINFO_ENTRY 5375 } 5376 5377 static void 5378 dump_dwarf_frame_inst(struct readelf *re, Dwarf_Cie cie, uint8_t *insts, 5379 Dwarf_Unsigned len, Dwarf_Unsigned caf, Dwarf_Signed daf, Dwarf_Addr pc, 5380 Dwarf_Debug dbg) 5381 { 5382 Dwarf_Frame_Op *oplist; 5383 Dwarf_Signed opcnt, delta; 5384 Dwarf_Small op; 5385 Dwarf_Error de; 5386 const char *op_str; 5387 char unk_op[32]; 5388 int i; 5389 5390 if (dwarf_expand_frame_instructions(cie, insts, len, &oplist, 5391 &opcnt, &de) != DW_DLV_OK) { 5392 warnx("dwarf_expand_frame_instructions failed: %s", 5393 dwarf_errmsg(de)); 5394 return; 5395 } 5396 5397 for (i = 0; i < opcnt; i++) { 5398 if (oplist[i].fp_base_op != 0) 5399 op = oplist[i].fp_base_op << 6; 5400 else 5401 op = oplist[i].fp_extended_op; 5402 if (dwarf_get_CFA_name(op, &op_str) != DW_DLV_OK) { 5403 snprintf(unk_op, sizeof(unk_op), "[Unknown CFA: %#x]", 5404 op); 5405 op_str = unk_op; 5406 } 5407 printf(" %s", op_str); 5408 switch (op) { 5409 case DW_CFA_advance_loc: 5410 delta = oplist[i].fp_offset * caf; 5411 pc += delta; 5412 printf(": %ju to %08jx", (uintmax_t) delta, 5413 (uintmax_t) pc); 5414 break; 5415 case DW_CFA_offset: 5416 case DW_CFA_offset_extended: 5417 case DW_CFA_offset_extended_sf: 5418 delta = oplist[i].fp_offset * daf; 5419 printf(": r%u (%s) at cfa%+jd", oplist[i].fp_register, 5420 dwarf_regname(re, oplist[i].fp_register), 5421 (intmax_t) delta); 5422 break; 5423 case DW_CFA_restore: 5424 printf(": r%u (%s)", oplist[i].fp_register, 5425 dwarf_regname(re, oplist[i].fp_register)); 5426 break; 5427 case DW_CFA_set_loc: 5428 pc = oplist[i].fp_offset; 5429 printf(": to %08jx", (uintmax_t) pc); 5430 break; 5431 case DW_CFA_advance_loc1: 5432 case DW_CFA_advance_loc2: 5433 case DW_CFA_advance_loc4: 5434 pc += oplist[i].fp_offset; 5435 printf(": %jd to %08jx", (intmax_t) oplist[i].fp_offset, 5436 (uintmax_t) pc); 5437 break; 5438 case DW_CFA_def_cfa: 5439 printf(": r%u (%s) ofs %ju", oplist[i].fp_register, 5440 dwarf_regname(re, oplist[i].fp_register), 5441 (uintmax_t) oplist[i].fp_offset); 5442 break; 5443 case DW_CFA_def_cfa_sf: 5444 printf(": r%u (%s) ofs %jd", oplist[i].fp_register, 5445 dwarf_regname(re, oplist[i].fp_register), 5446 (intmax_t) (oplist[i].fp_offset * daf)); 5447 break; 5448 case DW_CFA_def_cfa_register: 5449 printf(": r%u (%s)", oplist[i].fp_register, 5450 dwarf_regname(re, oplist[i].fp_register)); 5451 break; 5452 case DW_CFA_def_cfa_offset: 5453 printf(": %ju", (uintmax_t) oplist[i].fp_offset); 5454 break; 5455 case DW_CFA_def_cfa_offset_sf: 5456 printf(": %jd", (intmax_t) (oplist[i].fp_offset * daf)); 5457 break; 5458 default: 5459 break; 5460 } 5461 putchar('\n'); 5462 } 5463 5464 dwarf_dealloc(dbg, oplist, DW_DLA_FRAME_BLOCK); 5465 } 5466 5467 static char * 5468 get_regoff_str(struct readelf *re, Dwarf_Half reg, Dwarf_Addr off) 5469 { 5470 static char rs[16]; 5471 5472 if (reg == DW_FRAME_UNDEFINED_VAL || reg == DW_FRAME_REG_INITIAL_VALUE) 5473 snprintf(rs, sizeof(rs), "%c", 'u'); 5474 else if (reg == DW_FRAME_CFA_COL) 5475 snprintf(rs, sizeof(rs), "c%+jd", (intmax_t) off); 5476 else 5477 snprintf(rs, sizeof(rs), "%s%+jd", dwarf_regname(re, reg), 5478 (intmax_t) off); 5479 5480 return (rs); 5481 } 5482 5483 static int 5484 dump_dwarf_frame_regtable(struct readelf *re, Dwarf_Fde fde, Dwarf_Addr pc, 5485 Dwarf_Unsigned func_len, Dwarf_Half cie_ra) 5486 { 5487 Dwarf_Regtable rt; 5488 Dwarf_Addr row_pc, end_pc, pre_pc, cur_pc; 5489 Dwarf_Error de; 5490 char *vec; 5491 int i; 5492 5493 #define BIT_SET(v, n) (v[(n)>>3] |= 1U << ((n) & 7)) 5494 #define BIT_CLR(v, n) (v[(n)>>3] &= ~(1U << ((n) & 7))) 5495 #define BIT_ISSET(v, n) (v[(n)>>3] & (1U << ((n) & 7))) 5496 #define RT(x) rt.rules[(x)] 5497 5498 vec = calloc((DW_REG_TABLE_SIZE + 7) / 8, 1); 5499 if (vec == NULL) 5500 err(EXIT_FAILURE, "calloc failed"); 5501 5502 pre_pc = ~((Dwarf_Addr) 0); 5503 cur_pc = pc; 5504 end_pc = pc + func_len; 5505 for (; cur_pc < end_pc; cur_pc++) { 5506 if (dwarf_get_fde_info_for_all_regs(fde, cur_pc, &rt, &row_pc, 5507 &de) != DW_DLV_OK) { 5508 warnx("dwarf_get_fde_info_for_all_regs failed: %s\n", 5509 dwarf_errmsg(de)); 5510 return (-1); 5511 } 5512 if (row_pc == pre_pc) 5513 continue; 5514 pre_pc = row_pc; 5515 for (i = 1; i < DW_REG_TABLE_SIZE; i++) { 5516 if (rt.rules[i].dw_regnum != DW_FRAME_REG_INITIAL_VALUE) 5517 BIT_SET(vec, i); 5518 } 5519 } 5520 5521 printf(" LOC CFA "); 5522 for (i = 1; i < DW_REG_TABLE_SIZE; i++) { 5523 if (BIT_ISSET(vec, i)) { 5524 if ((Dwarf_Half) i == cie_ra) 5525 printf("ra "); 5526 else 5527 printf("%-5s", 5528 dwarf_regname(re, (unsigned int) i)); 5529 } 5530 } 5531 putchar('\n'); 5532 5533 pre_pc = ~((Dwarf_Addr) 0); 5534 cur_pc = pc; 5535 end_pc = pc + func_len; 5536 for (; cur_pc < end_pc; cur_pc++) { 5537 if (dwarf_get_fde_info_for_all_regs(fde, cur_pc, &rt, &row_pc, 5538 &de) != DW_DLV_OK) { 5539 warnx("dwarf_get_fde_info_for_all_regs failed: %s\n", 5540 dwarf_errmsg(de)); 5541 return (-1); 5542 } 5543 if (row_pc == pre_pc) 5544 continue; 5545 pre_pc = row_pc; 5546 printf("%08jx ", (uintmax_t) row_pc); 5547 printf("%-8s ", get_regoff_str(re, RT(0).dw_regnum, 5548 RT(0).dw_offset)); 5549 for (i = 1; i < DW_REG_TABLE_SIZE; i++) { 5550 if (BIT_ISSET(vec, i)) { 5551 printf("%-5s", get_regoff_str(re, 5552 RT(i).dw_regnum, RT(i).dw_offset)); 5553 } 5554 } 5555 putchar('\n'); 5556 } 5557 5558 free(vec); 5559 5560 return (0); 5561 5562 #undef BIT_SET 5563 #undef BIT_CLR 5564 #undef BIT_ISSET 5565 #undef RT 5566 } 5567 5568 static void 5569 dump_dwarf_frame_section(struct readelf *re, struct section *s, int alt) 5570 { 5571 Dwarf_Cie *cie_list, cie, pre_cie; 5572 Dwarf_Fde *fde_list, fde; 5573 Dwarf_Off cie_offset, fde_offset; 5574 Dwarf_Unsigned cie_length, fde_instlen; 5575 Dwarf_Unsigned cie_caf, cie_daf, cie_instlen, func_len, fde_length; 5576 Dwarf_Signed cie_count, fde_count, cie_index; 5577 Dwarf_Addr low_pc; 5578 Dwarf_Half cie_ra; 5579 Dwarf_Small cie_version; 5580 Dwarf_Ptr fde_addr, fde_inst, cie_inst; 5581 char *cie_aug, c; 5582 int i, eh_frame; 5583 Dwarf_Error de; 5584 5585 printf("\nThe section %s contains:\n\n", s->name); 5586 5587 if (!strcmp(s->name, ".debug_frame")) { 5588 eh_frame = 0; 5589 if (dwarf_get_fde_list(re->dbg, &cie_list, &cie_count, 5590 &fde_list, &fde_count, &de) != DW_DLV_OK) { 5591 warnx("dwarf_get_fde_list failed: %s", 5592 dwarf_errmsg(de)); 5593 return; 5594 } 5595 } else if (!strcmp(s->name, ".eh_frame")) { 5596 eh_frame = 1; 5597 if (dwarf_get_fde_list_eh(re->dbg, &cie_list, &cie_count, 5598 &fde_list, &fde_count, &de) != DW_DLV_OK) { 5599 warnx("dwarf_get_fde_list_eh failed: %s", 5600 dwarf_errmsg(de)); 5601 return; 5602 } 5603 } else 5604 return; 5605 5606 pre_cie = NULL; 5607 for (i = 0; i < fde_count; i++) { 5608 if (dwarf_get_fde_n(fde_list, i, &fde, &de) != DW_DLV_OK) { 5609 warnx("dwarf_get_fde_n failed: %s", dwarf_errmsg(de)); 5610 continue; 5611 } 5612 if (dwarf_get_cie_of_fde(fde, &cie, &de) != DW_DLV_OK) { 5613 warnx("dwarf_get_fde_n failed: %s", dwarf_errmsg(de)); 5614 continue; 5615 } 5616 if (dwarf_get_fde_range(fde, &low_pc, &func_len, &fde_addr, 5617 &fde_length, &cie_offset, &cie_index, &fde_offset, 5618 &de) != DW_DLV_OK) { 5619 warnx("dwarf_get_fde_range failed: %s", 5620 dwarf_errmsg(de)); 5621 continue; 5622 } 5623 if (dwarf_get_fde_instr_bytes(fde, &fde_inst, &fde_instlen, 5624 &de) != DW_DLV_OK) { 5625 warnx("dwarf_get_fde_instr_bytes failed: %s", 5626 dwarf_errmsg(de)); 5627 continue; 5628 } 5629 if (pre_cie == NULL || cie != pre_cie) { 5630 pre_cie = cie; 5631 if (dwarf_get_cie_info(cie, &cie_length, &cie_version, 5632 &cie_aug, &cie_caf, &cie_daf, &cie_ra, 5633 &cie_inst, &cie_instlen, &de) != DW_DLV_OK) { 5634 warnx("dwarf_get_cie_info failed: %s", 5635 dwarf_errmsg(de)); 5636 continue; 5637 } 5638 printf("%08jx %08jx %8.8jx CIE", 5639 (uintmax_t) cie_offset, 5640 (uintmax_t) cie_length, 5641 (uintmax_t) (eh_frame ? 0 : ~0U)); 5642 if (!alt) { 5643 putchar('\n'); 5644 printf(" Version:\t\t\t%u\n", cie_version); 5645 printf(" Augmentation:\t\t\t\""); 5646 while ((c = *cie_aug++) != '\0') 5647 putchar(c); 5648 printf("\"\n"); 5649 printf(" Code alignment factor:\t%ju\n", 5650 (uintmax_t) cie_caf); 5651 printf(" Data alignment factor:\t%jd\n", 5652 (intmax_t) cie_daf); 5653 printf(" Return address column:\t%ju\n", 5654 (uintmax_t) cie_ra); 5655 putchar('\n'); 5656 dump_dwarf_frame_inst(re, cie, cie_inst, 5657 cie_instlen, cie_caf, cie_daf, 0, 5658 re->dbg); 5659 putchar('\n'); 5660 } else { 5661 printf(" \""); 5662 while ((c = *cie_aug++) != '\0') 5663 putchar(c); 5664 putchar('"'); 5665 printf(" cf=%ju df=%jd ra=%ju\n", 5666 (uintmax_t) cie_caf, 5667 (uintmax_t) cie_daf, 5668 (uintmax_t) cie_ra); 5669 dump_dwarf_frame_regtable(re, fde, low_pc, 1, 5670 cie_ra); 5671 putchar('\n'); 5672 } 5673 } 5674 printf("%08jx %08jx %08jx FDE cie=%08jx pc=%08jx..%08jx\n", 5675 (uintmax_t) fde_offset, (uintmax_t) fde_length, 5676 (uintmax_t) cie_offset, 5677 (uintmax_t) (eh_frame ? fde_offset + 4 - cie_offset : 5678 cie_offset), 5679 (uintmax_t) low_pc, (uintmax_t) (low_pc + func_len)); 5680 if (!alt) 5681 dump_dwarf_frame_inst(re, cie, fde_inst, fde_instlen, 5682 cie_caf, cie_daf, low_pc, re->dbg); 5683 else 5684 dump_dwarf_frame_regtable(re, fde, low_pc, func_len, 5685 cie_ra); 5686 putchar('\n'); 5687 } 5688 } 5689 5690 static void 5691 dump_dwarf_frame(struct readelf *re, int alt) 5692 { 5693 struct section *s; 5694 int i; 5695 5696 (void) dwarf_set_frame_cfa_value(re->dbg, DW_FRAME_CFA_COL); 5697 5698 for (i = 0; (size_t) i < re->shnum; i++) { 5699 s = &re->sl[i]; 5700 if (s->name != NULL && (!strcmp(s->name, ".debug_frame") || 5701 !strcmp(s->name, ".eh_frame"))) 5702 dump_dwarf_frame_section(re, s, alt); 5703 } 5704 } 5705 5706 static void 5707 dump_dwarf_str(struct readelf *re) 5708 { 5709 struct section *s; 5710 Elf_Data *d; 5711 unsigned char *p; 5712 int elferr, end, i, j; 5713 5714 printf("\nContents of section .debug_str:\n"); 5715 5716 s = NULL; 5717 for (i = 0; (size_t) i < re->shnum; i++) { 5718 s = &re->sl[i]; 5719 if (s->name != NULL && !strcmp(s->name, ".debug_str")) 5720 break; 5721 } 5722 if ((size_t) i >= re->shnum) 5723 return; 5724 5725 (void) elf_errno(); 5726 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 5727 elferr = elf_errno(); 5728 if (elferr != 0) 5729 warnx("elf_getdata failed: %s", elf_errmsg(-1)); 5730 return; 5731 } 5732 if (d->d_size <= 0) 5733 return; 5734 5735 for (i = 0, p = d->d_buf; (size_t) i < d->d_size; i += 16) { 5736 printf(" 0x%08x", (unsigned int) i); 5737 if ((size_t) i + 16 > d->d_size) 5738 end = d->d_size; 5739 else 5740 end = i + 16; 5741 for (j = i; j < i + 16; j++) { 5742 if ((j - i) % 4 == 0) 5743 putchar(' '); 5744 if (j >= end) { 5745 printf(" "); 5746 continue; 5747 } 5748 printf("%02x", (uint8_t) p[j]); 5749 } 5750 putchar(' '); 5751 for (j = i; j < end; j++) { 5752 if (isprint(p[j])) 5753 putchar(p[j]); 5754 else if (p[j] == 0) 5755 putchar('.'); 5756 else 5757 putchar(' '); 5758 } 5759 putchar('\n'); 5760 } 5761 } 5762 5763 struct loc_at { 5764 Dwarf_Attribute la_at; 5765 Dwarf_Unsigned la_off; 5766 Dwarf_Unsigned la_lowpc; 5767 Dwarf_Half la_cu_psize; 5768 Dwarf_Half la_cu_osize; 5769 Dwarf_Half la_cu_ver; 5770 TAILQ_ENTRY(loc_at) la_next; 5771 }; 5772 5773 static TAILQ_HEAD(, loc_at) lalist = TAILQ_HEAD_INITIALIZER(lalist); 5774 5775 static void 5776 search_loclist_at(struct readelf *re, Dwarf_Die die, Dwarf_Unsigned lowpc) 5777 { 5778 Dwarf_Attribute *attr_list; 5779 Dwarf_Die ret_die; 5780 Dwarf_Unsigned off; 5781 Dwarf_Off ref; 5782 Dwarf_Signed attr_count; 5783 Dwarf_Half attr, form; 5784 Dwarf_Bool is_info; 5785 Dwarf_Error de; 5786 struct loc_at *la, *nla; 5787 int i, ret; 5788 5789 is_info = dwarf_get_die_infotypes_flag(die); 5790 5791 if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) != 5792 DW_DLV_OK) { 5793 if (ret == DW_DLV_ERROR) 5794 warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de)); 5795 goto cont_search; 5796 } 5797 for (i = 0; i < attr_count; i++) { 5798 if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) { 5799 warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de)); 5800 continue; 5801 } 5802 if (attr != DW_AT_location && 5803 attr != DW_AT_string_length && 5804 attr != DW_AT_return_addr && 5805 attr != DW_AT_data_member_location && 5806 attr != DW_AT_frame_base && 5807 attr != DW_AT_segment && 5808 attr != DW_AT_static_link && 5809 attr != DW_AT_use_location && 5810 attr != DW_AT_vtable_elem_location) 5811 continue; 5812 if (dwarf_whatform(attr_list[i], &form, &de) != DW_DLV_OK) { 5813 warnx("dwarf_whatform failed: %s", dwarf_errmsg(de)); 5814 continue; 5815 } 5816 if (form == DW_FORM_data4 || form == DW_FORM_data8) { 5817 if (dwarf_formudata(attr_list[i], &off, &de) != 5818 DW_DLV_OK) { 5819 warnx("dwarf_formudata failed: %s", 5820 dwarf_errmsg(de)); 5821 continue; 5822 } 5823 } else if (form == DW_FORM_sec_offset) { 5824 if (dwarf_global_formref(attr_list[i], &ref, &de) != 5825 DW_DLV_OK) { 5826 warnx("dwarf_global_formref failed: %s", 5827 dwarf_errmsg(de)); 5828 continue; 5829 } 5830 off = ref; 5831 } else 5832 continue; 5833 5834 TAILQ_FOREACH(la, &lalist, la_next) { 5835 if (off == la->la_off) 5836 break; 5837 if (off < la->la_off) { 5838 if ((nla = malloc(sizeof(*nla))) == NULL) 5839 err(EXIT_FAILURE, "malloc failed"); 5840 nla->la_at = attr_list[i]; 5841 nla->la_off = off; 5842 nla->la_lowpc = lowpc; 5843 nla->la_cu_psize = re->cu_psize; 5844 nla->la_cu_osize = re->cu_osize; 5845 nla->la_cu_ver = re->cu_ver; 5846 TAILQ_INSERT_BEFORE(la, nla, la_next); 5847 break; 5848 } 5849 } 5850 if (la == NULL) { 5851 if ((nla = malloc(sizeof(*nla))) == NULL) 5852 err(EXIT_FAILURE, "malloc failed"); 5853 nla->la_at = attr_list[i]; 5854 nla->la_off = off; 5855 nla->la_lowpc = lowpc; 5856 nla->la_cu_psize = re->cu_psize; 5857 nla->la_cu_osize = re->cu_osize; 5858 nla->la_cu_ver = re->cu_ver; 5859 TAILQ_INSERT_TAIL(&lalist, nla, la_next); 5860 } 5861 } 5862 5863 cont_search: 5864 /* Search children. */ 5865 ret = dwarf_child(die, &ret_die, &de); 5866 if (ret == DW_DLV_ERROR) 5867 warnx("dwarf_child: %s", dwarf_errmsg(de)); 5868 else if (ret == DW_DLV_OK) 5869 search_loclist_at(re, ret_die, lowpc); 5870 5871 /* Search sibling. */ 5872 ret = dwarf_siblingof_b(re->dbg, die, &ret_die, is_info, &de); 5873 if (ret == DW_DLV_ERROR) 5874 warnx("dwarf_siblingof: %s", dwarf_errmsg(de)); 5875 else if (ret == DW_DLV_OK) 5876 search_loclist_at(re, ret_die, lowpc); 5877 } 5878 5879 static void 5880 dump_dwarf_loc(struct readelf *re, Dwarf_Loc *lr) 5881 { 5882 const char *op_str; 5883 char unk_op[32]; 5884 uint8_t *b, n; 5885 int i; 5886 5887 if (dwarf_get_OP_name(lr->lr_atom, &op_str) != 5888 DW_DLV_OK) { 5889 snprintf(unk_op, sizeof(unk_op), 5890 "[Unknown OP: %#x]", lr->lr_atom); 5891 op_str = unk_op; 5892 } 5893 5894 printf("%s", op_str); 5895 5896 switch (lr->lr_atom) { 5897 case DW_OP_reg0: 5898 case DW_OP_reg1: 5899 case DW_OP_reg2: 5900 case DW_OP_reg3: 5901 case DW_OP_reg4: 5902 case DW_OP_reg5: 5903 case DW_OP_reg6: 5904 case DW_OP_reg7: 5905 case DW_OP_reg8: 5906 case DW_OP_reg9: 5907 case DW_OP_reg10: 5908 case DW_OP_reg11: 5909 case DW_OP_reg12: 5910 case DW_OP_reg13: 5911 case DW_OP_reg14: 5912 case DW_OP_reg15: 5913 case DW_OP_reg16: 5914 case DW_OP_reg17: 5915 case DW_OP_reg18: 5916 case DW_OP_reg19: 5917 case DW_OP_reg20: 5918 case DW_OP_reg21: 5919 case DW_OP_reg22: 5920 case DW_OP_reg23: 5921 case DW_OP_reg24: 5922 case DW_OP_reg25: 5923 case DW_OP_reg26: 5924 case DW_OP_reg27: 5925 case DW_OP_reg28: 5926 case DW_OP_reg29: 5927 case DW_OP_reg30: 5928 case DW_OP_reg31: 5929 printf(" (%s)", dwarf_regname(re, lr->lr_atom - DW_OP_reg0)); 5930 break; 5931 5932 case DW_OP_deref: 5933 case DW_OP_lit0: 5934 case DW_OP_lit1: 5935 case DW_OP_lit2: 5936 case DW_OP_lit3: 5937 case DW_OP_lit4: 5938 case DW_OP_lit5: 5939 case DW_OP_lit6: 5940 case DW_OP_lit7: 5941 case DW_OP_lit8: 5942 case DW_OP_lit9: 5943 case DW_OP_lit10: 5944 case DW_OP_lit11: 5945 case DW_OP_lit12: 5946 case DW_OP_lit13: 5947 case DW_OP_lit14: 5948 case DW_OP_lit15: 5949 case DW_OP_lit16: 5950 case DW_OP_lit17: 5951 case DW_OP_lit18: 5952 case DW_OP_lit19: 5953 case DW_OP_lit20: 5954 case DW_OP_lit21: 5955 case DW_OP_lit22: 5956 case DW_OP_lit23: 5957 case DW_OP_lit24: 5958 case DW_OP_lit25: 5959 case DW_OP_lit26: 5960 case DW_OP_lit27: 5961 case DW_OP_lit28: 5962 case DW_OP_lit29: 5963 case DW_OP_lit30: 5964 case DW_OP_lit31: 5965 case DW_OP_dup: 5966 case DW_OP_drop: 5967 case DW_OP_over: 5968 case DW_OP_swap: 5969 case DW_OP_rot: 5970 case DW_OP_xderef: 5971 case DW_OP_abs: 5972 case DW_OP_and: 5973 case DW_OP_div: 5974 case DW_OP_minus: 5975 case DW_OP_mod: 5976 case DW_OP_mul: 5977 case DW_OP_neg: 5978 case DW_OP_not: 5979 case DW_OP_or: 5980 case DW_OP_plus: 5981 case DW_OP_shl: 5982 case DW_OP_shr: 5983 case DW_OP_shra: 5984 case DW_OP_xor: 5985 case DW_OP_eq: 5986 case DW_OP_ge: 5987 case DW_OP_gt: 5988 case DW_OP_le: 5989 case DW_OP_lt: 5990 case DW_OP_ne: 5991 case DW_OP_nop: 5992 case DW_OP_push_object_address: 5993 case DW_OP_form_tls_address: 5994 case DW_OP_call_frame_cfa: 5995 case DW_OP_stack_value: 5996 case DW_OP_GNU_push_tls_address: 5997 case DW_OP_GNU_uninit: 5998 break; 5999 6000 case DW_OP_const1u: 6001 case DW_OP_pick: 6002 case DW_OP_deref_size: 6003 case DW_OP_xderef_size: 6004 case DW_OP_const2u: 6005 case DW_OP_bra: 6006 case DW_OP_skip: 6007 case DW_OP_const4u: 6008 case DW_OP_const8u: 6009 case DW_OP_constu: 6010 case DW_OP_plus_uconst: 6011 case DW_OP_regx: 6012 case DW_OP_piece: 6013 printf(": %ju", (uintmax_t) 6014 lr->lr_number); 6015 break; 6016 6017 case DW_OP_const1s: 6018 case DW_OP_const2s: 6019 case DW_OP_const4s: 6020 case DW_OP_const8s: 6021 case DW_OP_consts: 6022 printf(": %jd", (intmax_t) 6023 lr->lr_number); 6024 break; 6025 6026 case DW_OP_breg0: 6027 case DW_OP_breg1: 6028 case DW_OP_breg2: 6029 case DW_OP_breg3: 6030 case DW_OP_breg4: 6031 case DW_OP_breg5: 6032 case DW_OP_breg6: 6033 case DW_OP_breg7: 6034 case DW_OP_breg8: 6035 case DW_OP_breg9: 6036 case DW_OP_breg10: 6037 case DW_OP_breg11: 6038 case DW_OP_breg12: 6039 case DW_OP_breg13: 6040 case DW_OP_breg14: 6041 case DW_OP_breg15: 6042 case DW_OP_breg16: 6043 case DW_OP_breg17: 6044 case DW_OP_breg18: 6045 case DW_OP_breg19: 6046 case DW_OP_breg20: 6047 case DW_OP_breg21: 6048 case DW_OP_breg22: 6049 case DW_OP_breg23: 6050 case DW_OP_breg24: 6051 case DW_OP_breg25: 6052 case DW_OP_breg26: 6053 case DW_OP_breg27: 6054 case DW_OP_breg28: 6055 case DW_OP_breg29: 6056 case DW_OP_breg30: 6057 case DW_OP_breg31: 6058 printf(" (%s): %jd", 6059 dwarf_regname(re, lr->lr_atom - DW_OP_breg0), 6060 (intmax_t) lr->lr_number); 6061 break; 6062 6063 case DW_OP_fbreg: 6064 printf(": %jd", (intmax_t) 6065 lr->lr_number); 6066 break; 6067 6068 case DW_OP_bregx: 6069 printf(": %ju (%s) %jd", 6070 (uintmax_t) lr->lr_number, 6071 dwarf_regname(re, (unsigned int) lr->lr_number), 6072 (intmax_t) lr->lr_number2); 6073 break; 6074 6075 case DW_OP_addr: 6076 case DW_OP_GNU_encoded_addr: 6077 printf(": %#jx", (uintmax_t) 6078 lr->lr_number); 6079 break; 6080 6081 case DW_OP_GNU_implicit_pointer: 6082 printf(": <0x%jx> %jd", (uintmax_t) lr->lr_number, 6083 (intmax_t) lr->lr_number2); 6084 break; 6085 6086 case DW_OP_implicit_value: 6087 printf(": %ju byte block:", (uintmax_t) lr->lr_number); 6088 b = (uint8_t *)(uintptr_t) lr->lr_number2; 6089 for (i = 0; (Dwarf_Unsigned) i < lr->lr_number; i++) 6090 printf(" %x", b[i]); 6091 break; 6092 6093 case DW_OP_GNU_entry_value: 6094 printf(": ("); 6095 dump_dwarf_block(re, (uint8_t *)(uintptr_t) lr->lr_number2, 6096 lr->lr_number); 6097 putchar(')'); 6098 break; 6099 6100 case DW_OP_GNU_const_type: 6101 printf(": <0x%jx> ", (uintmax_t) lr->lr_number); 6102 b = (uint8_t *)(uintptr_t) lr->lr_number2; 6103 n = *b; 6104 for (i = 1; (uint8_t) i < n; i++) 6105 printf(" %x", b[i]); 6106 break; 6107 6108 case DW_OP_GNU_regval_type: 6109 printf(": %ju (%s) <0x%jx>", (uintmax_t) lr->lr_number, 6110 dwarf_regname(re, (unsigned int) lr->lr_number), 6111 (uintmax_t) lr->lr_number2); 6112 break; 6113 6114 case DW_OP_GNU_convert: 6115 case DW_OP_GNU_deref_type: 6116 case DW_OP_GNU_parameter_ref: 6117 case DW_OP_GNU_reinterpret: 6118 printf(": <0x%jx>", (uintmax_t) lr->lr_number); 6119 break; 6120 6121 default: 6122 break; 6123 } 6124 } 6125 6126 static void 6127 dump_dwarf_block(struct readelf *re, uint8_t *b, Dwarf_Unsigned len) 6128 { 6129 Dwarf_Locdesc *llbuf; 6130 Dwarf_Signed lcnt; 6131 Dwarf_Error de; 6132 int i; 6133 6134 if (dwarf_loclist_from_expr_b(re->dbg, b, len, re->cu_psize, 6135 re->cu_osize, re->cu_ver, &llbuf, &lcnt, &de) != DW_DLV_OK) { 6136 warnx("dwarf_loclist_form_expr_b: %s", dwarf_errmsg(de)); 6137 return; 6138 } 6139 6140 for (i = 0; (Dwarf_Half) i < llbuf->ld_cents; i++) { 6141 dump_dwarf_loc(re, &llbuf->ld_s[i]); 6142 if (i < llbuf->ld_cents - 1) 6143 printf("; "); 6144 } 6145 6146 dwarf_dealloc(re->dbg, llbuf->ld_s, DW_DLA_LOC_BLOCK); 6147 dwarf_dealloc(re->dbg, llbuf, DW_DLA_LOCDESC); 6148 } 6149 6150 static void 6151 dump_dwarf_loclist(struct readelf *re) 6152 { 6153 Dwarf_Die die; 6154 Dwarf_Locdesc **llbuf; 6155 Dwarf_Unsigned lowpc; 6156 Dwarf_Signed lcnt; 6157 Dwarf_Half tag, version, pointer_size, off_size; 6158 Dwarf_Error de; 6159 struct loc_at *la; 6160 int i, j, ret; 6161 6162 printf("\nContents of section .debug_loc:\n"); 6163 6164 /* Search .debug_info section. */ 6165 while ((ret = dwarf_next_cu_header_b(re->dbg, NULL, &version, NULL, 6166 &pointer_size, &off_size, NULL, NULL, &de)) == DW_DLV_OK) { 6167 set_cu_context(re, pointer_size, off_size, version); 6168 die = NULL; 6169 if (dwarf_siblingof(re->dbg, die, &die, &de) != DW_DLV_OK) 6170 continue; 6171 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { 6172 warnx("dwarf_tag failed: %s", dwarf_errmsg(de)); 6173 continue; 6174 } 6175 /* XXX: What about DW_TAG_partial_unit? */ 6176 lowpc = 0; 6177 if (tag == DW_TAG_compile_unit) { 6178 if (dwarf_attrval_unsigned(die, DW_AT_low_pc, 6179 &lowpc, &de) != DW_DLV_OK) 6180 lowpc = 0; 6181 } 6182 6183 /* Search attributes for reference to .debug_loc section. */ 6184 search_loclist_at(re, die, lowpc); 6185 } 6186 if (ret == DW_DLV_ERROR) 6187 warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de)); 6188 6189 /* Search .debug_types section. */ 6190 do { 6191 while ((ret = dwarf_next_cu_header_c(re->dbg, 0, NULL, 6192 &version, NULL, &pointer_size, &off_size, NULL, NULL, 6193 NULL, NULL, &de)) == DW_DLV_OK) { 6194 set_cu_context(re, pointer_size, off_size, version); 6195 die = NULL; 6196 if (dwarf_siblingof(re->dbg, die, &die, &de) != 6197 DW_DLV_OK) 6198 continue; 6199 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { 6200 warnx("dwarf_tag failed: %s", 6201 dwarf_errmsg(de)); 6202 continue; 6203 } 6204 6205 lowpc = 0; 6206 if (tag == DW_TAG_type_unit) { 6207 if (dwarf_attrval_unsigned(die, DW_AT_low_pc, 6208 &lowpc, &de) != DW_DLV_OK) 6209 lowpc = 0; 6210 } 6211 6212 /* 6213 * Search attributes for reference to .debug_loc 6214 * section. 6215 */ 6216 search_loclist_at(re, die, lowpc); 6217 } 6218 if (ret == DW_DLV_ERROR) 6219 warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de)); 6220 } while (dwarf_next_types_section(re->dbg, &de) == DW_DLV_OK); 6221 6222 if (TAILQ_EMPTY(&lalist)) 6223 return; 6224 6225 printf(" Offset Begin End Expression\n"); 6226 6227 TAILQ_FOREACH(la, &lalist, la_next) { 6228 if (dwarf_loclist_n(la->la_at, &llbuf, &lcnt, &de) != 6229 DW_DLV_OK) { 6230 warnx("dwarf_loclist_n failed: %s", dwarf_errmsg(de)); 6231 continue; 6232 } 6233 set_cu_context(re, la->la_cu_psize, la->la_cu_osize, 6234 la->la_cu_ver); 6235 for (i = 0; i < lcnt; i++) { 6236 printf(" %8.8jx ", (uintmax_t) la->la_off); 6237 if (llbuf[i]->ld_lopc == 0 && llbuf[i]->ld_hipc == 0) { 6238 printf("<End of list>\n"); 6239 continue; 6240 } 6241 6242 /* TODO: handle base selection entry. */ 6243 6244 printf("%8.8jx %8.8jx ", 6245 (uintmax_t) (la->la_lowpc + llbuf[i]->ld_lopc), 6246 (uintmax_t) (la->la_lowpc + llbuf[i]->ld_hipc)); 6247 6248 putchar('('); 6249 for (j = 0; (Dwarf_Half) j < llbuf[i]->ld_cents; j++) { 6250 dump_dwarf_loc(re, &llbuf[i]->ld_s[j]); 6251 if (j < llbuf[i]->ld_cents - 1) 6252 printf("; "); 6253 } 6254 putchar(')'); 6255 6256 if (llbuf[i]->ld_lopc == llbuf[i]->ld_hipc) 6257 printf(" (start == end)"); 6258 putchar('\n'); 6259 } 6260 for (i = 0; i < lcnt; i++) { 6261 dwarf_dealloc(re->dbg, llbuf[i]->ld_s, 6262 DW_DLA_LOC_BLOCK); 6263 dwarf_dealloc(re->dbg, llbuf[i], DW_DLA_LOCDESC); 6264 } 6265 dwarf_dealloc(re->dbg, llbuf, DW_DLA_LIST); 6266 } 6267 } 6268 6269 /* 6270 * Retrieve a string using string table section index and the string offset. 6271 */ 6272 static const char* 6273 get_string(struct readelf *re, int strtab, size_t off) 6274 { 6275 const char *name; 6276 6277 if ((name = elf_strptr(re->elf, strtab, off)) == NULL) 6278 return (""); 6279 6280 return (name); 6281 } 6282 6283 /* 6284 * Retrieve the name of a symbol using the section index of the symbol 6285 * table and the index of the symbol within that table. 6286 */ 6287 static const char * 6288 get_symbol_name(struct readelf *re, int symtab, int i) 6289 { 6290 struct section *s; 6291 const char *name; 6292 GElf_Sym sym; 6293 Elf_Data *data; 6294 int elferr; 6295 6296 s = &re->sl[symtab]; 6297 if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM) 6298 return (""); 6299 (void) elf_errno(); 6300 if ((data = elf_getdata(s->scn, NULL)) == NULL) { 6301 elferr = elf_errno(); 6302 if (elferr != 0) 6303 warnx("elf_getdata failed: %s", elf_errmsg(elferr)); 6304 return (""); 6305 } 6306 if (gelf_getsym(data, i, &sym) != &sym) 6307 return (""); 6308 /* Return section name for STT_SECTION symbol. */ 6309 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION) { 6310 if (sym.st_shndx < re->shnum && 6311 re->sl[sym.st_shndx].name != NULL) 6312 return (re->sl[sym.st_shndx].name); 6313 return (""); 6314 } 6315 if (s->link >= re->shnum || 6316 (name = elf_strptr(re->elf, s->link, sym.st_name)) == NULL) 6317 return (""); 6318 6319 return (name); 6320 } 6321 6322 static uint64_t 6323 get_symbol_value(struct readelf *re, int symtab, int i) 6324 { 6325 struct section *s; 6326 GElf_Sym sym; 6327 Elf_Data *data; 6328 int elferr; 6329 6330 s = &re->sl[symtab]; 6331 if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM) 6332 return (0); 6333 (void) elf_errno(); 6334 if ((data = elf_getdata(s->scn, NULL)) == NULL) { 6335 elferr = elf_errno(); 6336 if (elferr != 0) 6337 warnx("elf_getdata failed: %s", elf_errmsg(elferr)); 6338 return (0); 6339 } 6340 if (gelf_getsym(data, i, &sym) != &sym) 6341 return (0); 6342 6343 return (sym.st_value); 6344 } 6345 6346 static void 6347 hex_dump(struct readelf *re) 6348 { 6349 struct section *s; 6350 Elf_Data *d; 6351 uint8_t *buf; 6352 size_t sz, nbytes; 6353 uint64_t addr; 6354 int elferr, i, j; 6355 6356 for (i = 1; (size_t) i < re->shnum; i++) { 6357 s = &re->sl[i]; 6358 if (find_dumpop(re, (size_t) i, s->name, HEX_DUMP, -1) == NULL) 6359 continue; 6360 (void) elf_errno(); 6361 if ((d = elf_getdata(s->scn, NULL)) == NULL && 6362 (d = elf_rawdata(s->scn, NULL)) == NULL) { 6363 elferr = elf_errno(); 6364 if (elferr != 0) 6365 warnx("elf_getdata failed: %s", 6366 elf_errmsg(elferr)); 6367 continue; 6368 } 6369 (void) elf_errno(); 6370 if (d->d_size <= 0 || d->d_buf == NULL) { 6371 printf("\nSection '%s' has no data to dump.\n", 6372 s->name); 6373 continue; 6374 } 6375 buf = d->d_buf; 6376 sz = d->d_size; 6377 addr = s->addr; 6378 printf("\nHex dump of section '%s':\n", s->name); 6379 while (sz > 0) { 6380 printf(" 0x%8.8jx ", (uintmax_t)addr); 6381 nbytes = sz > 16? 16 : sz; 6382 for (j = 0; j < 16; j++) { 6383 if ((size_t)j < nbytes) 6384 printf("%2.2x", buf[j]); 6385 else 6386 printf(" "); 6387 if ((j & 3) == 3) 6388 printf(" "); 6389 } 6390 for (j = 0; (size_t)j < nbytes; j++) { 6391 if (isprint(buf[j])) 6392 printf("%c", buf[j]); 6393 else 6394 printf("."); 6395 } 6396 printf("\n"); 6397 buf += nbytes; 6398 addr += nbytes; 6399 sz -= nbytes; 6400 } 6401 } 6402 } 6403 6404 static void 6405 str_dump(struct readelf *re) 6406 { 6407 struct section *s; 6408 Elf_Data *d; 6409 unsigned char *start, *end, *buf_end; 6410 unsigned int len; 6411 int i, j, elferr, found; 6412 6413 for (i = 1; (size_t) i < re->shnum; i++) { 6414 s = &re->sl[i]; 6415 if (find_dumpop(re, (size_t) i, s->name, STR_DUMP, -1) == NULL) 6416 continue; 6417 (void) elf_errno(); 6418 if ((d = elf_getdata(s->scn, NULL)) == NULL && 6419 (d = elf_rawdata(s->scn, NULL)) == NULL) { 6420 elferr = elf_errno(); 6421 if (elferr != 0) 6422 warnx("elf_getdata failed: %s", 6423 elf_errmsg(elferr)); 6424 continue; 6425 } 6426 (void) elf_errno(); 6427 if (d->d_size <= 0 || d->d_buf == NULL) { 6428 printf("\nSection '%s' has no data to dump.\n", 6429 s->name); 6430 continue; 6431 } 6432 buf_end = (unsigned char *) d->d_buf + d->d_size; 6433 start = (unsigned char *) d->d_buf; 6434 found = 0; 6435 printf("\nString dump of section '%s':\n", s->name); 6436 for (;;) { 6437 while (start < buf_end && !isprint(*start)) 6438 start++; 6439 if (start >= buf_end) 6440 break; 6441 end = start + 1; 6442 while (end < buf_end && isprint(*end)) 6443 end++; 6444 printf(" [%6lx] ", 6445 (long) (start - (unsigned char *) d->d_buf)); 6446 len = end - start; 6447 for (j = 0; (unsigned int) j < len; j++) 6448 putchar(start[j]); 6449 putchar('\n'); 6450 found = 1; 6451 if (end >= buf_end) 6452 break; 6453 start = end + 1; 6454 } 6455 if (!found) 6456 printf(" No strings found in this section."); 6457 putchar('\n'); 6458 } 6459 } 6460 6461 static void 6462 load_sections(struct readelf *re) 6463 { 6464 struct section *s; 6465 const char *name; 6466 Elf_Scn *scn; 6467 GElf_Shdr sh; 6468 size_t shstrndx, ndx; 6469 int elferr; 6470 6471 /* Allocate storage for internal section list. */ 6472 if (!elf_getshnum(re->elf, &re->shnum)) { 6473 warnx("elf_getshnum failed: %s", elf_errmsg(-1)); 6474 return; 6475 } 6476 if (re->sl != NULL) 6477 free(re->sl); 6478 if ((re->sl = calloc(re->shnum, sizeof(*re->sl))) == NULL) 6479 err(EXIT_FAILURE, "calloc failed"); 6480 6481 /* Get the index of .shstrtab section. */ 6482 if (!elf_getshstrndx(re->elf, &shstrndx)) { 6483 warnx("elf_getshstrndx failed: %s", elf_errmsg(-1)); 6484 return; 6485 } 6486 6487 if ((scn = elf_getscn(re->elf, 0)) == NULL) 6488 return; 6489 6490 (void) elf_errno(); 6491 do { 6492 if (gelf_getshdr(scn, &sh) == NULL) { 6493 warnx("gelf_getshdr failed: %s", elf_errmsg(-1)); 6494 (void) elf_errno(); 6495 continue; 6496 } 6497 if ((name = elf_strptr(re->elf, shstrndx, sh.sh_name)) == NULL) { 6498 (void) elf_errno(); 6499 name = "ERROR"; 6500 } 6501 if ((ndx = elf_ndxscn(scn)) == SHN_UNDEF) { 6502 if ((elferr = elf_errno()) != 0) 6503 warnx("elf_ndxscn failed: %s", 6504 elf_errmsg(elferr)); 6505 continue; 6506 } 6507 if (ndx >= re->shnum) { 6508 warnx("section index of '%s' out of range", name); 6509 continue; 6510 } 6511 if (sh.sh_link >= re->shnum) 6512 warnx("section link %llu of '%s' out of range", 6513 (unsigned long long)sh.sh_link, name); 6514 s = &re->sl[ndx]; 6515 s->name = name; 6516 s->scn = scn; 6517 s->off = sh.sh_offset; 6518 s->sz = sh.sh_size; 6519 s->entsize = sh.sh_entsize; 6520 s->align = sh.sh_addralign; 6521 s->type = sh.sh_type; 6522 s->flags = sh.sh_flags; 6523 s->addr = sh.sh_addr; 6524 s->link = sh.sh_link; 6525 s->info = sh.sh_info; 6526 } while ((scn = elf_nextscn(re->elf, scn)) != NULL); 6527 elferr = elf_errno(); 6528 if (elferr != 0) 6529 warnx("elf_nextscn failed: %s", elf_errmsg(elferr)); 6530 } 6531 6532 static void 6533 unload_sections(struct readelf *re) 6534 { 6535 6536 if (re->sl != NULL) { 6537 free(re->sl); 6538 re->sl = NULL; 6539 } 6540 re->shnum = 0; 6541 re->vd_s = NULL; 6542 re->vn_s = NULL; 6543 re->vs_s = NULL; 6544 re->vs = NULL; 6545 re->vs_sz = 0; 6546 if (re->ver != NULL) { 6547 free(re->ver); 6548 re->ver = NULL; 6549 re->ver_sz = 0; 6550 } 6551 } 6552 6553 static void 6554 dump_elf(struct readelf *re) 6555 { 6556 6557 /* Fetch ELF header. No need to continue if it fails. */ 6558 if (gelf_getehdr(re->elf, &re->ehdr) == NULL) { 6559 warnx("gelf_getehdr failed: %s", elf_errmsg(-1)); 6560 return; 6561 } 6562 if ((re->ec = gelf_getclass(re->elf)) == ELFCLASSNONE) { 6563 warnx("gelf_getclass failed: %s", elf_errmsg(-1)); 6564 return; 6565 } 6566 if (re->ehdr.e_ident[EI_DATA] == ELFDATA2MSB) { 6567 re->dw_read = _read_msb; 6568 re->dw_decode = _decode_msb; 6569 } else { 6570 re->dw_read = _read_lsb; 6571 re->dw_decode = _decode_lsb; 6572 } 6573 6574 if (re->options & ~RE_H) 6575 load_sections(re); 6576 if ((re->options & RE_VV) || (re->options & RE_S)) 6577 search_ver(re); 6578 if (re->options & RE_H) 6579 dump_ehdr(re); 6580 if (re->options & RE_L) 6581 dump_phdr(re); 6582 if (re->options & RE_SS) 6583 dump_shdr(re); 6584 if (re->options & RE_G) 6585 dump_section_groups(re); 6586 if (re->options & RE_D) 6587 dump_dynamic(re); 6588 if (re->options & RE_R) 6589 dump_reloc(re); 6590 if (re->options & RE_S) 6591 dump_symtabs(re); 6592 if (re->options & RE_N) 6593 dump_notes(re); 6594 if (re->options & RE_II) 6595 dump_hash(re); 6596 if (re->options & RE_X) 6597 hex_dump(re); 6598 if (re->options & RE_P) 6599 str_dump(re); 6600 if (re->options & RE_VV) 6601 dump_ver(re); 6602 if (re->options & RE_AA) 6603 dump_arch_specific_info(re); 6604 if (re->options & RE_W) 6605 dump_dwarf(re); 6606 if (re->options & ~RE_H) 6607 unload_sections(re); 6608 } 6609 6610 static void 6611 dump_dwarf(struct readelf *re) 6612 { 6613 int error; 6614 Dwarf_Error de; 6615 6616 if (dwarf_elf_init(re->elf, DW_DLC_READ, NULL, NULL, &re->dbg, &de)) { 6617 if ((error = dwarf_errno(de)) != DW_DLE_DEBUG_INFO_NULL) 6618 errx(EXIT_FAILURE, "dwarf_elf_init failed: %s", 6619 dwarf_errmsg(de)); 6620 return; 6621 } 6622 6623 if (re->dop & DW_A) 6624 dump_dwarf_abbrev(re); 6625 if (re->dop & DW_L) 6626 dump_dwarf_line(re); 6627 if (re->dop & DW_LL) 6628 dump_dwarf_line_decoded(re); 6629 if (re->dop & DW_I) { 6630 dump_dwarf_info(re, 0); 6631 dump_dwarf_info(re, 1); 6632 } 6633 if (re->dop & DW_P) 6634 dump_dwarf_pubnames(re); 6635 if (re->dop & DW_R) 6636 dump_dwarf_aranges(re); 6637 if (re->dop & DW_RR) 6638 dump_dwarf_ranges(re); 6639 if (re->dop & DW_M) 6640 dump_dwarf_macinfo(re); 6641 if (re->dop & DW_F) 6642 dump_dwarf_frame(re, 0); 6643 else if (re->dop & DW_FF) 6644 dump_dwarf_frame(re, 1); 6645 if (re->dop & DW_S) 6646 dump_dwarf_str(re); 6647 if (re->dop & DW_O) 6648 dump_dwarf_loclist(re); 6649 6650 dwarf_finish(re->dbg, &de); 6651 } 6652 6653 static void 6654 dump_ar(struct readelf *re, int fd) 6655 { 6656 Elf_Arsym *arsym; 6657 Elf_Arhdr *arhdr; 6658 Elf_Cmd cmd; 6659 Elf *e; 6660 size_t sz; 6661 off_t off; 6662 int i; 6663 6664 re->ar = re->elf; 6665 6666 if (re->options & RE_C) { 6667 if ((arsym = elf_getarsym(re->ar, &sz)) == NULL) { 6668 warnx("elf_getarsym() failed: %s", elf_errmsg(-1)); 6669 goto process_members; 6670 } 6671 printf("Index of archive %s: (%ju entries)\n", re->filename, 6672 (uintmax_t) sz - 1); 6673 off = 0; 6674 for (i = 0; (size_t) i < sz; i++) { 6675 if (arsym[i].as_name == NULL) 6676 break; 6677 if (arsym[i].as_off != off) { 6678 off = arsym[i].as_off; 6679 if (elf_rand(re->ar, off) != off) { 6680 warnx("elf_rand() failed: %s", 6681 elf_errmsg(-1)); 6682 continue; 6683 } 6684 if ((e = elf_begin(fd, ELF_C_READ, re->ar)) == 6685 NULL) { 6686 warnx("elf_begin() failed: %s", 6687 elf_errmsg(-1)); 6688 continue; 6689 } 6690 if ((arhdr = elf_getarhdr(e)) == NULL) { 6691 warnx("elf_getarhdr() failed: %s", 6692 elf_errmsg(-1)); 6693 elf_end(e); 6694 continue; 6695 } 6696 printf("Binary %s(%s) contains:\n", 6697 re->filename, arhdr->ar_name); 6698 } 6699 printf("\t%s\n", arsym[i].as_name); 6700 } 6701 if (elf_rand(re->ar, SARMAG) != SARMAG) { 6702 warnx("elf_rand() failed: %s", elf_errmsg(-1)); 6703 return; 6704 } 6705 } 6706 6707 process_members: 6708 6709 if ((re->options & ~RE_C) == 0) 6710 return; 6711 6712 cmd = ELF_C_READ; 6713 while ((re->elf = elf_begin(fd, cmd, re->ar)) != NULL) { 6714 if ((arhdr = elf_getarhdr(re->elf)) == NULL) { 6715 warnx("elf_getarhdr() failed: %s", elf_errmsg(-1)); 6716 goto next_member; 6717 } 6718 if (strcmp(arhdr->ar_name, "/") == 0 || 6719 strcmp(arhdr->ar_name, "//") == 0 || 6720 strcmp(arhdr->ar_name, "__.SYMDEF") == 0) 6721 goto next_member; 6722 printf("\nFile: %s(%s)\n", re->filename, arhdr->ar_name); 6723 dump_elf(re); 6724 6725 next_member: 6726 cmd = elf_next(re->elf); 6727 elf_end(re->elf); 6728 } 6729 re->elf = re->ar; 6730 } 6731 6732 static void 6733 dump_object(struct readelf *re) 6734 { 6735 int fd; 6736 6737 if ((fd = open(re->filename, O_RDONLY)) == -1) { 6738 warn("open %s failed", re->filename); 6739 return; 6740 } 6741 6742 if ((re->flags & DISPLAY_FILENAME) != 0) 6743 printf("\nFile: %s\n", re->filename); 6744 6745 if ((re->elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) { 6746 warnx("elf_begin() failed: %s", elf_errmsg(-1)); 6747 return; 6748 } 6749 6750 switch (elf_kind(re->elf)) { 6751 case ELF_K_NONE: 6752 warnx("Not an ELF file."); 6753 return; 6754 case ELF_K_ELF: 6755 dump_elf(re); 6756 break; 6757 case ELF_K_AR: 6758 dump_ar(re, fd); 6759 break; 6760 default: 6761 warnx("Internal: libelf returned unknown elf kind."); 6762 return; 6763 } 6764 6765 elf_end(re->elf); 6766 } 6767 6768 static void 6769 add_dumpop(struct readelf *re, size_t si, const char *sn, int op, int t) 6770 { 6771 struct dumpop *d; 6772 6773 if ((d = find_dumpop(re, si, sn, -1, t)) == NULL) { 6774 if ((d = calloc(1, sizeof(*d))) == NULL) 6775 err(EXIT_FAILURE, "calloc failed"); 6776 if (t == DUMP_BY_INDEX) 6777 d->u.si = si; 6778 else 6779 d->u.sn = sn; 6780 d->type = t; 6781 d->op = op; 6782 STAILQ_INSERT_TAIL(&re->v_dumpop, d, dumpop_list); 6783 } else 6784 d->op |= op; 6785 } 6786 6787 static struct dumpop * 6788 find_dumpop(struct readelf *re, size_t si, const char *sn, int op, int t) 6789 { 6790 struct dumpop *d; 6791 6792 STAILQ_FOREACH(d, &re->v_dumpop, dumpop_list) { 6793 if ((op == -1 || op & d->op) && 6794 (t == -1 || (unsigned) t == d->type)) { 6795 if ((d->type == DUMP_BY_INDEX && d->u.si == si) || 6796 (d->type == DUMP_BY_NAME && !strcmp(d->u.sn, sn))) 6797 return (d); 6798 } 6799 } 6800 6801 return (NULL); 6802 } 6803 6804 static struct { 6805 const char *ln; 6806 char sn; 6807 int value; 6808 } dwarf_op[] = { 6809 {"rawline", 'l', DW_L}, 6810 {"decodedline", 'L', DW_LL}, 6811 {"info", 'i', DW_I}, 6812 {"abbrev", 'a', DW_A}, 6813 {"pubnames", 'p', DW_P}, 6814 {"aranges", 'r', DW_R}, 6815 {"ranges", 'r', DW_R}, 6816 {"Ranges", 'R', DW_RR}, 6817 {"macro", 'm', DW_M}, 6818 {"frames", 'f', DW_F}, 6819 {"frames-interp", 'F', DW_FF}, 6820 {"str", 's', DW_S}, 6821 {"loc", 'o', DW_O}, 6822 {NULL, 0, 0} 6823 }; 6824 6825 static void 6826 parse_dwarf_op_short(struct readelf *re, const char *op) 6827 { 6828 int i; 6829 6830 if (op == NULL) { 6831 re->dop |= DW_DEFAULT_OPTIONS; 6832 return; 6833 } 6834 6835 for (; *op != '\0'; op++) { 6836 for (i = 0; dwarf_op[i].ln != NULL; i++) { 6837 if (dwarf_op[i].sn == *op) { 6838 re->dop |= dwarf_op[i].value; 6839 break; 6840 } 6841 } 6842 } 6843 } 6844 6845 static void 6846 parse_dwarf_op_long(struct readelf *re, const char *op) 6847 { 6848 char *p, *token, *bp; 6849 int i; 6850 6851 if (op == NULL) { 6852 re->dop |= DW_DEFAULT_OPTIONS; 6853 return; 6854 } 6855 6856 if ((p = strdup(op)) == NULL) 6857 err(EXIT_FAILURE, "strdup failed"); 6858 bp = p; 6859 6860 while ((token = strsep(&p, ",")) != NULL) { 6861 for (i = 0; dwarf_op[i].ln != NULL; i++) { 6862 if (!strcmp(token, dwarf_op[i].ln)) { 6863 re->dop |= dwarf_op[i].value; 6864 break; 6865 } 6866 } 6867 } 6868 6869 free(bp); 6870 } 6871 6872 static uint64_t 6873 _read_lsb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read) 6874 { 6875 uint64_t ret; 6876 uint8_t *src; 6877 6878 src = (uint8_t *) d->d_buf + *offsetp; 6879 6880 ret = 0; 6881 switch (bytes_to_read) { 6882 case 8: 6883 ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40; 6884 ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56; 6885 /* FALLTHROUGH */ 6886 case 4: 6887 ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24; 6888 /* FALLTHROUGH */ 6889 case 2: 6890 ret |= ((uint64_t) src[1]) << 8; 6891 /* FALLTHROUGH */ 6892 case 1: 6893 ret |= src[0]; 6894 break; 6895 default: 6896 return (0); 6897 } 6898 6899 *offsetp += bytes_to_read; 6900 6901 return (ret); 6902 } 6903 6904 static uint64_t 6905 _read_msb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read) 6906 { 6907 uint64_t ret; 6908 uint8_t *src; 6909 6910 src = (uint8_t *) d->d_buf + *offsetp; 6911 6912 switch (bytes_to_read) { 6913 case 1: 6914 ret = src[0]; 6915 break; 6916 case 2: 6917 ret = src[1] | ((uint64_t) src[0]) << 8; 6918 break; 6919 case 4: 6920 ret = src[3] | ((uint64_t) src[2]) << 8; 6921 ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24; 6922 break; 6923 case 8: 6924 ret = src[7] | ((uint64_t) src[6]) << 8; 6925 ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24; 6926 ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40; 6927 ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56; 6928 break; 6929 default: 6930 return (0); 6931 } 6932 6933 *offsetp += bytes_to_read; 6934 6935 return (ret); 6936 } 6937 6938 static uint64_t 6939 _decode_lsb(uint8_t **data, int bytes_to_read) 6940 { 6941 uint64_t ret; 6942 uint8_t *src; 6943 6944 src = *data; 6945 6946 ret = 0; 6947 switch (bytes_to_read) { 6948 case 8: 6949 ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40; 6950 ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56; 6951 /* FALLTHROUGH */ 6952 case 4: 6953 ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24; 6954 /* FALLTHROUGH */ 6955 case 2: 6956 ret |= ((uint64_t) src[1]) << 8; 6957 /* FALLTHROUGH */ 6958 case 1: 6959 ret |= src[0]; 6960 break; 6961 default: 6962 return (0); 6963 } 6964 6965 *data += bytes_to_read; 6966 6967 return (ret); 6968 } 6969 6970 static uint64_t 6971 _decode_msb(uint8_t **data, int bytes_to_read) 6972 { 6973 uint64_t ret; 6974 uint8_t *src; 6975 6976 src = *data; 6977 6978 ret = 0; 6979 switch (bytes_to_read) { 6980 case 1: 6981 ret = src[0]; 6982 break; 6983 case 2: 6984 ret = src[1] | ((uint64_t) src[0]) << 8; 6985 break; 6986 case 4: 6987 ret = src[3] | ((uint64_t) src[2]) << 8; 6988 ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24; 6989 break; 6990 case 8: 6991 ret = src[7] | ((uint64_t) src[6]) << 8; 6992 ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24; 6993 ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40; 6994 ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56; 6995 break; 6996 default: 6997 return (0); 6998 break; 6999 } 7000 7001 *data += bytes_to_read; 7002 7003 return (ret); 7004 } 7005 7006 static int64_t 7007 _decode_sleb128(uint8_t **dp, uint8_t *dpe) 7008 { 7009 int64_t ret = 0; 7010 uint8_t b = 0; 7011 int shift = 0; 7012 7013 uint8_t *src = *dp; 7014 7015 do { 7016 if (src >= dpe) 7017 break; 7018 b = *src++; 7019 ret |= ((b & 0x7f) << shift); 7020 shift += 7; 7021 } while ((b & 0x80) != 0); 7022 7023 if (shift < 32 && (b & 0x40) != 0) 7024 ret |= (-1 << shift); 7025 7026 *dp = src; 7027 7028 return (ret); 7029 } 7030 7031 static uint64_t 7032 _decode_uleb128(uint8_t **dp, uint8_t *dpe) 7033 { 7034 uint64_t ret = 0; 7035 uint8_t b; 7036 int shift = 0; 7037 7038 uint8_t *src = *dp; 7039 7040 do { 7041 if (src >= dpe) 7042 break; 7043 b = *src++; 7044 ret |= ((b & 0x7f) << shift); 7045 shift += 7; 7046 } while ((b & 0x80) != 0); 7047 7048 *dp = src; 7049 7050 return (ret); 7051 } 7052 7053 static void 7054 readelf_version(void) 7055 { 7056 (void) printf("%s (%s)\n", ELFTC_GETPROGNAME(), 7057 elftc_version()); 7058 exit(EXIT_SUCCESS); 7059 } 7060 7061 #define USAGE_MESSAGE "\ 7062 Usage: %s [options] file...\n\ 7063 Display information about ELF objects and ar(1) archives.\n\n\ 7064 Options:\n\ 7065 -a | --all Equivalent to specifying options '-dhIlrsASV'.\n\ 7066 -c | --archive-index Print the archive symbol table for archives.\n\ 7067 -d | --dynamic Print the contents of SHT_DYNAMIC sections.\n\ 7068 -e | --headers Print all headers in the object.\n\ 7069 -g | --section-groups Print the contents of the section groups.\n\ 7070 -h | --file-header Print the file header for the object.\n\ 7071 -l | --program-headers Print the PHDR table for the object.\n\ 7072 -n | --notes Print the contents of SHT_NOTE sections.\n\ 7073 -p INDEX | --string-dump=INDEX\n\ 7074 Print the contents of section at index INDEX.\n\ 7075 -r | --relocs Print relocation information.\n\ 7076 -s | --syms | --symbols Print symbol tables.\n\ 7077 -t | --section-details Print additional information about sections.\n\ 7078 -v | --version Print a version identifier and exit.\n\ 7079 -w[afilmoprsFLR] | --debug-dump={abbrev,aranges,decodedline,frames,\n\ 7080 frames-interp,info,loc,macro,pubnames,\n\ 7081 ranges,Ranges,rawline,str}\n\ 7082 Display DWARF information.\n\ 7083 -x INDEX | --hex-dump=INDEX\n\ 7084 Display contents of a section as hexadecimal.\n\ 7085 -A | --arch-specific (accepted, but ignored)\n\ 7086 -D | --use-dynamic Print the symbol table specified by the DT_SYMTAB\n\ 7087 entry in the \".dynamic\" section.\n\ 7088 -H | --help Print a help message.\n\ 7089 -I | --histogram Print information on bucket list lengths for \n\ 7090 hash sections.\n\ 7091 -N | --full-section-name (accepted, but ignored)\n\ 7092 -S | --sections | --section-headers\n\ 7093 Print information about section headers.\n\ 7094 -V | --version-info Print symbol versoning information.\n\ 7095 -W | --wide Print information without wrapping long lines.\n" 7096 7097 7098 static void 7099 readelf_usage(int status) 7100 { 7101 fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME()); 7102 exit(status); 7103 } 7104 7105 int 7106 main(int argc, char **argv) 7107 { 7108 struct readelf *re, re_storage; 7109 unsigned long si; 7110 int opt, i; 7111 char *ep; 7112 7113 re = &re_storage; 7114 memset(re, 0, sizeof(*re)); 7115 STAILQ_INIT(&re->v_dumpop); 7116 7117 while ((opt = getopt_long(argc, argv, "AacDdegHhIi:lNnp:rSstuVvWw::x:", 7118 longopts, NULL)) != -1) { 7119 switch(opt) { 7120 case '?': 7121 readelf_usage(EXIT_SUCCESS); 7122 break; 7123 case 'A': 7124 re->options |= RE_AA; 7125 break; 7126 case 'a': 7127 re->options |= RE_AA | RE_D | RE_G | RE_H | RE_II | 7128 RE_L | RE_R | RE_SS | RE_S | RE_VV; 7129 break; 7130 case 'c': 7131 re->options |= RE_C; 7132 break; 7133 case 'D': 7134 re->options |= RE_DD; 7135 break; 7136 case 'd': 7137 re->options |= RE_D; 7138 break; 7139 case 'e': 7140 re->options |= RE_H | RE_L | RE_SS; 7141 break; 7142 case 'g': 7143 re->options |= RE_G; 7144 break; 7145 case 'H': 7146 readelf_usage(EXIT_SUCCESS); 7147 break; 7148 case 'h': 7149 re->options |= RE_H; 7150 break; 7151 case 'I': 7152 re->options |= RE_II; 7153 break; 7154 case 'i': 7155 /* Not implemented yet. */ 7156 break; 7157 case 'l': 7158 re->options |= RE_L; 7159 break; 7160 case 'N': 7161 re->options |= RE_NN; 7162 break; 7163 case 'n': 7164 re->options |= RE_N; 7165 break; 7166 case 'p': 7167 re->options |= RE_P; 7168 si = strtoul(optarg, &ep, 10); 7169 if (*ep == '\0') 7170 add_dumpop(re, (size_t) si, NULL, STR_DUMP, 7171 DUMP_BY_INDEX); 7172 else 7173 add_dumpop(re, 0, optarg, STR_DUMP, 7174 DUMP_BY_NAME); 7175 break; 7176 case 'r': 7177 re->options |= RE_R; 7178 break; 7179 case 'S': 7180 re->options |= RE_SS; 7181 break; 7182 case 's': 7183 re->options |= RE_S; 7184 break; 7185 case 't': 7186 re->options |= RE_T; 7187 break; 7188 case 'u': 7189 re->options |= RE_U; 7190 break; 7191 case 'V': 7192 re->options |= RE_VV; 7193 break; 7194 case 'v': 7195 readelf_version(); 7196 break; 7197 case 'W': 7198 re->options |= RE_WW; 7199 break; 7200 case 'w': 7201 re->options |= RE_W; 7202 parse_dwarf_op_short(re, optarg); 7203 break; 7204 case 'x': 7205 re->options |= RE_X; 7206 si = strtoul(optarg, &ep, 10); 7207 if (*ep == '\0') 7208 add_dumpop(re, (size_t) si, NULL, HEX_DUMP, 7209 DUMP_BY_INDEX); 7210 else 7211 add_dumpop(re, 0, optarg, HEX_DUMP, 7212 DUMP_BY_NAME); 7213 break; 7214 case OPTION_DEBUG_DUMP: 7215 re->options |= RE_W; 7216 parse_dwarf_op_long(re, optarg); 7217 } 7218 } 7219 7220 argv += optind; 7221 argc -= optind; 7222 7223 if (argc == 0 || re->options == 0) 7224 readelf_usage(EXIT_FAILURE); 7225 7226 if (argc > 1) 7227 re->flags |= DISPLAY_FILENAME; 7228 7229 if (elf_version(EV_CURRENT) == EV_NONE) 7230 errx(EXIT_FAILURE, "ELF library initialization failed: %s", 7231 elf_errmsg(-1)); 7232 7233 for (i = 0; i < argc; i++) { 7234 re->filename = argv[i]; 7235 dump_object(re); 7236 } 7237 7238 exit(EXIT_SUCCESS); 7239 } 7240