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