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