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 3178 2015-03-30 18:29:13Z 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; 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, NULL)) == NULL) { 2730 warnx("elf_rawfile failed: %s", elf_errmsg(-1)); 2731 continue; 2732 } 2733 printf(" [Requesting program interpreter: %s]\n", 2734 rawfile + phdr.p_offset); 2735 } 2736 } 2737 2738 /* Dump section to segment mapping. */ 2739 if (re->shnum == 0) 2740 return; 2741 printf("\n Section to Segment mapping:\n"); 2742 printf(" Segment Sections...\n"); 2743 for (i = 0; (size_t)i < phnum; i++) { 2744 if (gelf_getphdr(re->elf, i, &phdr) != &phdr) { 2745 warnx("gelf_getphdr failed: %s", elf_errmsg(-1)); 2746 continue; 2747 } 2748 printf(" %2.2d ", i); 2749 /* skip NULL section. */ 2750 for (j = 1; (size_t)j < re->shnum; j++) 2751 if (re->sl[j].off >= phdr.p_offset && 2752 re->sl[j].off + re->sl[j].sz <= 2753 phdr.p_offset + phdr.p_memsz) 2754 printf("%s ", re->sl[j].name); 2755 printf("\n"); 2756 } 2757 #undef PH_HDR 2758 #undef PH_CT 2759 } 2760 2761 static char * 2762 section_flags(struct readelf *re, struct section *s) 2763 { 2764 #define BUF_SZ 256 2765 static char buf[BUF_SZ]; 2766 int i, p, nb; 2767 2768 p = 0; 2769 nb = re->ec == ELFCLASS32 ? 8 : 16; 2770 if (re->options & RE_T) { 2771 snprintf(buf, BUF_SZ, "[%*.*jx]: ", nb, nb, 2772 (uintmax_t)s->flags); 2773 p += nb + 4; 2774 } 2775 for (i = 0; section_flag[i].ln != NULL; i++) { 2776 if ((s->flags & section_flag[i].value) == 0) 2777 continue; 2778 if (re->options & RE_T) { 2779 snprintf(&buf[p], BUF_SZ - p, "%s, ", 2780 section_flag[i].ln); 2781 p += strlen(section_flag[i].ln) + 2; 2782 } else 2783 buf[p++] = section_flag[i].sn; 2784 } 2785 if (re->options & RE_T && p > nb + 4) 2786 p -= 2; 2787 buf[p] = '\0'; 2788 2789 return (buf); 2790 } 2791 2792 static void 2793 dump_shdr(struct readelf *re) 2794 { 2795 struct section *s; 2796 int i; 2797 2798 #define S_HDR "[Nr] Name", "Type", "Addr", "Off", "Size", "ES", \ 2799 "Flg", "Lk", "Inf", "Al" 2800 #define S_HDRL "[Nr] Name", "Type", "Address", "Offset", "Size", \ 2801 "EntSize", "Flags", "Link", "Info", "Align" 2802 #define ST_HDR "[Nr] Name", "Type", "Addr", "Off", "Size", "ES", \ 2803 "Lk", "Inf", "Al", "Flags" 2804 #define ST_HDRL "[Nr] Name", "Type", "Address", "Offset", "Link", \ 2805 "Size", "EntSize", "Info", "Align", "Flags" 2806 #define S_CT i, s->name, section_type(re->ehdr.e_machine, s->type), \ 2807 (uintmax_t)s->addr, (uintmax_t)s->off, (uintmax_t)s->sz,\ 2808 (uintmax_t)s->entsize, section_flags(re, s), \ 2809 s->link, s->info, (uintmax_t)s->align 2810 #define ST_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, s->link, s->info, \ 2813 (uintmax_t)s->align, section_flags(re, s) 2814 #define ST_CTL i, s->name, section_type(re->ehdr.e_machine, s->type), \ 2815 (uintmax_t)s->addr, (uintmax_t)s->off, s->link, \ 2816 (uintmax_t)s->sz, (uintmax_t)s->entsize, s->info, \ 2817 (uintmax_t)s->align, section_flags(re, s) 2818 2819 if (re->shnum == 0) { 2820 printf("\nThere are no sections in this file.\n"); 2821 return; 2822 } 2823 printf("There are %ju section headers, starting at offset 0x%jx:\n", 2824 (uintmax_t)re->shnum, (uintmax_t)re->ehdr.e_shoff); 2825 printf("\nSection Headers:\n"); 2826 if (re->ec == ELFCLASS32) { 2827 if (re->options & RE_T) 2828 printf(" %s\n %-16s%-9s%-7s%-7s%-5s%-3s%-4s%s\n" 2829 "%12s\n", ST_HDR); 2830 else 2831 printf(" %-23s%-16s%-9s%-7s%-7s%-3s%-4s%-3s%-4s%s\n", 2832 S_HDR); 2833 } else if (re->options & RE_WW) { 2834 if (re->options & RE_T) 2835 printf(" %s\n %-16s%-17s%-7s%-7s%-5s%-3s%-4s%s\n" 2836 "%12s\n", ST_HDR); 2837 else 2838 printf(" %-23s%-16s%-17s%-7s%-7s%-3s%-4s%-3s%-4s%s\n", 2839 S_HDR); 2840 } else { 2841 if (re->options & RE_T) 2842 printf(" %s\n %-18s%-17s%-18s%s\n %-18s" 2843 "%-17s%-18s%s\n%12s\n", ST_HDRL); 2844 else 2845 printf(" %-23s%-17s%-18s%s\n %-18s%-17s%-7s%" 2846 "-6s%-6s%s\n", S_HDRL); 2847 } 2848 for (i = 0; (size_t)i < re->shnum; i++) { 2849 s = &re->sl[i]; 2850 if (re->ec == ELFCLASS32) { 2851 if (re->options & RE_T) 2852 printf(" [%2d] %s\n %-15.15s %8.8jx" 2853 " %6.6jx %6.6jx %2.2jx %2u %3u %2ju\n" 2854 " %s\n", ST_CT); 2855 else 2856 printf(" [%2d] %-17.17s %-15.15s %8.8jx" 2857 " %6.6jx %6.6jx %2.2jx %3s %2u %3u %2ju\n", 2858 S_CT); 2859 } else if (re->options & RE_WW) { 2860 if (re->options & RE_T) 2861 printf(" [%2d] %s\n %-15.15s %16.16jx" 2862 " %6.6jx %6.6jx %2.2jx %2u %3u %2ju\n" 2863 " %s\n", ST_CT); 2864 else 2865 printf(" [%2d] %-17.17s %-15.15s %16.16jx" 2866 " %6.6jx %6.6jx %2.2jx %3s %2u %3u %2ju\n", 2867 S_CT); 2868 } else { 2869 if (re->options & RE_T) 2870 printf(" [%2d] %s\n %-15.15s %16.16jx" 2871 " %16.16jx %u\n %16.16jx %16.16jx" 2872 " %-16u %ju\n %s\n", ST_CTL); 2873 else 2874 printf(" [%2d] %-17.17s %-15.15s %16.16jx" 2875 " %8.8jx\n %16.16jx %16.16jx " 2876 "%3s %2u %3u %ju\n", S_CT); 2877 } 2878 } 2879 if ((re->options & RE_T) == 0) 2880 printf("Key to Flags:\n W (write), A (alloc)," 2881 " X (execute), M (merge), S (strings)\n" 2882 " I (info), L (link order), G (group), x (unknown)\n" 2883 " O (extra OS processing required)" 2884 " o (OS specific), p (processor specific)\n"); 2885 2886 #undef S_HDR 2887 #undef S_HDRL 2888 #undef ST_HDR 2889 #undef ST_HDRL 2890 #undef S_CT 2891 #undef ST_CT 2892 #undef ST_CTL 2893 } 2894 2895 static void 2896 dump_dynamic(struct readelf *re) 2897 { 2898 GElf_Dyn dyn; 2899 Elf_Data *d; 2900 struct section *s; 2901 int elferr, i, is_dynamic, j, jmax, nentries; 2902 2903 is_dynamic = 0; 2904 2905 for (i = 0; (size_t)i < re->shnum; i++) { 2906 s = &re->sl[i]; 2907 if (s->type != SHT_DYNAMIC) 2908 continue; 2909 (void) elf_errno(); 2910 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 2911 elferr = elf_errno(); 2912 if (elferr != 0) 2913 warnx("elf_getdata failed: %s", elf_errmsg(-1)); 2914 continue; 2915 } 2916 if (d->d_size <= 0) 2917 continue; 2918 2919 is_dynamic = 1; 2920 2921 /* Determine the actual number of table entries. */ 2922 nentries = 0; 2923 jmax = (int) (s->sz / s->entsize); 2924 2925 for (j = 0; j < jmax; j++) { 2926 if (gelf_getdyn(d, j, &dyn) != &dyn) { 2927 warnx("gelf_getdyn failed: %s", 2928 elf_errmsg(-1)); 2929 continue; 2930 } 2931 nentries ++; 2932 if (dyn.d_tag == DT_NULL) 2933 break; 2934 } 2935 2936 printf("\nDynamic section at offset 0x%jx", (uintmax_t)s->off); 2937 printf(" contains %u entries:\n", nentries); 2938 2939 if (re->ec == ELFCLASS32) 2940 printf("%5s%12s%28s\n", "Tag", "Type", "Name/Value"); 2941 else 2942 printf("%5s%20s%28s\n", "Tag", "Type", "Name/Value"); 2943 2944 for (j = 0; j < nentries; j++) { 2945 if (gelf_getdyn(d, j, &dyn) != &dyn) 2946 continue; 2947 /* Dump dynamic entry type. */ 2948 if (re->ec == ELFCLASS32) 2949 printf(" 0x%8.8jx", (uintmax_t)dyn.d_tag); 2950 else 2951 printf(" 0x%16.16jx", (uintmax_t)dyn.d_tag); 2952 printf(" %-20s", dt_type(re->ehdr.e_machine, 2953 dyn.d_tag)); 2954 /* Dump dynamic entry value. */ 2955 dump_dyn_val(re, &dyn, s->link); 2956 } 2957 } 2958 2959 if (!is_dynamic) 2960 printf("\nThere is no dynamic section in this file.\n"); 2961 } 2962 2963 static char * 2964 timestamp(time_t ti) 2965 { 2966 static char ts[32]; 2967 struct tm *t; 2968 2969 t = gmtime(&ti); 2970 snprintf(ts, sizeof(ts), "%04d-%02d-%02dT%02d:%02d:%02d", 2971 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, 2972 t->tm_min, t->tm_sec); 2973 2974 return (ts); 2975 } 2976 2977 static const char * 2978 dyn_str(struct readelf *re, uint32_t stab, uint64_t d_val) 2979 { 2980 const char *name; 2981 2982 if (stab == SHN_UNDEF) 2983 name = "ERROR"; 2984 else if ((name = elf_strptr(re->elf, stab, d_val)) == NULL) { 2985 (void) elf_errno(); /* clear error */ 2986 name = "ERROR"; 2987 } 2988 2989 return (name); 2990 } 2991 2992 static void 2993 dump_arch_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab) 2994 { 2995 const char *name; 2996 2997 switch (re->ehdr.e_machine) { 2998 case EM_MIPS: 2999 case EM_MIPS_RS3_LE: 3000 switch (dyn->d_tag) { 3001 case DT_MIPS_RLD_VERSION: 3002 case DT_MIPS_LOCAL_GOTNO: 3003 case DT_MIPS_CONFLICTNO: 3004 case DT_MIPS_LIBLISTNO: 3005 case DT_MIPS_SYMTABNO: 3006 case DT_MIPS_UNREFEXTNO: 3007 case DT_MIPS_GOTSYM: 3008 case DT_MIPS_HIPAGENO: 3009 case DT_MIPS_DELTA_CLASS_NO: 3010 case DT_MIPS_DELTA_INSTANCE_NO: 3011 case DT_MIPS_DELTA_RELOC_NO: 3012 case DT_MIPS_DELTA_SYM_NO: 3013 case DT_MIPS_DELTA_CLASSSYM_NO: 3014 case DT_MIPS_LOCALPAGE_GOTIDX: 3015 case DT_MIPS_LOCAL_GOTIDX: 3016 case DT_MIPS_HIDDEN_GOTIDX: 3017 case DT_MIPS_PROTECTED_GOTIDX: 3018 printf(" %ju\n", (uintmax_t) dyn->d_un.d_val); 3019 break; 3020 case DT_MIPS_ICHECKSUM: 3021 case DT_MIPS_FLAGS: 3022 case DT_MIPS_BASE_ADDRESS: 3023 case DT_MIPS_CONFLICT: 3024 case DT_MIPS_LIBLIST: 3025 case DT_MIPS_RLD_MAP: 3026 case DT_MIPS_DELTA_CLASS: 3027 case DT_MIPS_DELTA_INSTANCE: 3028 case DT_MIPS_DELTA_RELOC: 3029 case DT_MIPS_DELTA_SYM: 3030 case DT_MIPS_DELTA_CLASSSYM: 3031 case DT_MIPS_CXX_FLAGS: 3032 case DT_MIPS_PIXIE_INIT: 3033 case DT_MIPS_SYMBOL_LIB: 3034 case DT_MIPS_OPTIONS: 3035 case DT_MIPS_INTERFACE: 3036 case DT_MIPS_DYNSTR_ALIGN: 3037 case DT_MIPS_INTERFACE_SIZE: 3038 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: 3039 case DT_MIPS_COMPACT_SIZE: 3040 case DT_MIPS_GP_VALUE: 3041 case DT_MIPS_AUX_DYNAMIC: 3042 case DT_MIPS_PLTGOT: 3043 case DT_MIPS_RLD_OBJ_UPDATE: 3044 case DT_MIPS_RWPLT: 3045 printf(" 0x%jx\n", (uintmax_t) dyn->d_un.d_val); 3046 break; 3047 case DT_MIPS_IVERSION: 3048 case DT_MIPS_PERF_SUFFIX: 3049 case DT_AUXILIARY: 3050 case DT_FILTER: 3051 name = dyn_str(re, stab, dyn->d_un.d_val); 3052 printf(" %s\n", name); 3053 break; 3054 case DT_MIPS_TIME_STAMP: 3055 printf(" %s\n", timestamp(dyn->d_un.d_val)); 3056 break; 3057 } 3058 break; 3059 default: 3060 printf("\n"); 3061 break; 3062 } 3063 } 3064 3065 static void 3066 dump_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab) 3067 { 3068 const char *name; 3069 3070 if (dyn->d_tag >= DT_LOPROC && dyn->d_tag <= DT_HIPROC) { 3071 dump_arch_dyn_val(re, dyn, stab); 3072 return; 3073 } 3074 3075 /* These entry values are index into the string table. */ 3076 name = NULL; 3077 if (dyn->d_tag == DT_NEEDED || dyn->d_tag == DT_SONAME || 3078 dyn->d_tag == DT_RPATH || dyn->d_tag == DT_RUNPATH) 3079 name = dyn_str(re, stab, dyn->d_un.d_val); 3080 3081 switch(dyn->d_tag) { 3082 case DT_NULL: 3083 case DT_PLTGOT: 3084 case DT_HASH: 3085 case DT_STRTAB: 3086 case DT_SYMTAB: 3087 case DT_RELA: 3088 case DT_INIT: 3089 case DT_SYMBOLIC: 3090 case DT_REL: 3091 case DT_DEBUG: 3092 case DT_TEXTREL: 3093 case DT_JMPREL: 3094 case DT_FINI: 3095 case DT_VERDEF: 3096 case DT_VERNEED: 3097 case DT_VERSYM: 3098 case DT_GNU_HASH: 3099 case DT_GNU_LIBLIST: 3100 case DT_GNU_CONFLICT: 3101 printf(" 0x%jx\n", (uintmax_t) dyn->d_un.d_val); 3102 break; 3103 case DT_PLTRELSZ: 3104 case DT_RELASZ: 3105 case DT_RELAENT: 3106 case DT_STRSZ: 3107 case DT_SYMENT: 3108 case DT_RELSZ: 3109 case DT_RELENT: 3110 case DT_INIT_ARRAYSZ: 3111 case DT_FINI_ARRAYSZ: 3112 case DT_GNU_CONFLICTSZ: 3113 case DT_GNU_LIBLISTSZ: 3114 printf(" %ju (bytes)\n", (uintmax_t) dyn->d_un.d_val); 3115 break; 3116 case DT_RELACOUNT: 3117 case DT_RELCOUNT: 3118 case DT_VERDEFNUM: 3119 case DT_VERNEEDNUM: 3120 printf(" %ju\n", (uintmax_t) dyn->d_un.d_val); 3121 break; 3122 case DT_NEEDED: 3123 printf(" Shared library: [%s]\n", name); 3124 break; 3125 case DT_SONAME: 3126 printf(" Library soname: [%s]\n", name); 3127 break; 3128 case DT_RPATH: 3129 printf(" Library rpath: [%s]\n", name); 3130 break; 3131 case DT_RUNPATH: 3132 printf(" Library runpath: [%s]\n", name); 3133 break; 3134 case DT_PLTREL: 3135 printf(" %s\n", dt_type(re->ehdr.e_machine, dyn->d_un.d_val)); 3136 break; 3137 case DT_GNU_PRELINKED: 3138 printf(" %s\n", timestamp(dyn->d_un.d_val)); 3139 break; 3140 default: 3141 printf("\n"); 3142 } 3143 } 3144 3145 static void 3146 dump_rel(struct readelf *re, struct section *s, Elf_Data *d) 3147 { 3148 GElf_Rel r; 3149 const char *symname; 3150 uint64_t symval; 3151 int i, len; 3152 3153 #define REL_HDR "r_offset", "r_info", "r_type", "st_value", "st_name" 3154 #define REL_CT32 (uintmax_t)r.r_offset, (uintmax_t)r.r_info, \ 3155 r_type(re->ehdr.e_machine, ELF32_R_TYPE(r.r_info)), \ 3156 (uintmax_t)symval, symname 3157 #define REL_CT64 (uintmax_t)r.r_offset, (uintmax_t)r.r_info, \ 3158 r_type(re->ehdr.e_machine, ELF64_R_TYPE(r.r_info)), \ 3159 (uintmax_t)symval, symname 3160 3161 printf("\nRelocation section (%s):\n", s->name); 3162 if (re->ec == ELFCLASS32) 3163 printf("%-8s %-8s %-19s %-8s %s\n", REL_HDR); 3164 else { 3165 if (re->options & RE_WW) 3166 printf("%-16s %-16s %-24s %-16s %s\n", REL_HDR); 3167 else 3168 printf("%-12s %-12s %-19s %-16s %s\n", REL_HDR); 3169 } 3170 len = d->d_size / s->entsize; 3171 for (i = 0; i < len; i++) { 3172 if (gelf_getrel(d, i, &r) != &r) { 3173 warnx("gelf_getrel failed: %s", elf_errmsg(-1)); 3174 continue; 3175 } 3176 if (s->link >= re->shnum) { 3177 warnx("invalid section link index %u", s->link); 3178 continue; 3179 } 3180 symname = get_symbol_name(re, s->link, GELF_R_SYM(r.r_info)); 3181 symval = get_symbol_value(re, s->link, GELF_R_SYM(r.r_info)); 3182 if (re->ec == ELFCLASS32) { 3183 r.r_info = ELF32_R_INFO(ELF64_R_SYM(r.r_info), 3184 ELF64_R_TYPE(r.r_info)); 3185 printf("%8.8jx %8.8jx %-19.19s %8.8jx %s\n", REL_CT32); 3186 } else { 3187 if (re->options & RE_WW) 3188 printf("%16.16jx %16.16jx %-24.24s" 3189 " %16.16jx %s\n", REL_CT64); 3190 else 3191 printf("%12.12jx %12.12jx %-19.19s" 3192 " %16.16jx %s\n", REL_CT64); 3193 } 3194 } 3195 3196 #undef REL_HDR 3197 #undef REL_CT 3198 } 3199 3200 static void 3201 dump_rela(struct readelf *re, struct section *s, Elf_Data *d) 3202 { 3203 GElf_Rela r; 3204 const char *symname; 3205 uint64_t symval; 3206 int i, len; 3207 3208 #define RELA_HDR "r_offset", "r_info", "r_type", "st_value", \ 3209 "st_name + r_addend" 3210 #define RELA_CT32 (uintmax_t)r.r_offset, (uintmax_t)r.r_info, \ 3211 r_type(re->ehdr.e_machine, ELF32_R_TYPE(r.r_info)), \ 3212 (uintmax_t)symval, symname 3213 #define RELA_CT64 (uintmax_t)r.r_offset, (uintmax_t)r.r_info, \ 3214 r_type(re->ehdr.e_machine, ELF64_R_TYPE(r.r_info)), \ 3215 (uintmax_t)symval, symname 3216 3217 printf("\nRelocation section with addend (%s):\n", s->name); 3218 if (re->ec == ELFCLASS32) 3219 printf("%-8s %-8s %-19s %-8s %s\n", RELA_HDR); 3220 else { 3221 if (re->options & RE_WW) 3222 printf("%-16s %-16s %-24s %-16s %s\n", RELA_HDR); 3223 else 3224 printf("%-12s %-12s %-19s %-16s %s\n", RELA_HDR); 3225 } 3226 len = d->d_size / s->entsize; 3227 for (i = 0; i < len; i++) { 3228 if (gelf_getrela(d, i, &r) != &r) { 3229 warnx("gelf_getrel failed: %s", elf_errmsg(-1)); 3230 continue; 3231 } 3232 if (s->link >= re->shnum) { 3233 warnx("invalid section link index %u", s->link); 3234 continue; 3235 } 3236 symname = get_symbol_name(re, s->link, GELF_R_SYM(r.r_info)); 3237 symval = get_symbol_value(re, s->link, GELF_R_SYM(r.r_info)); 3238 if (re->ec == ELFCLASS32) { 3239 r.r_info = ELF32_R_INFO(ELF64_R_SYM(r.r_info), 3240 ELF64_R_TYPE(r.r_info)); 3241 printf("%8.8jx %8.8jx %-19.19s %8.8jx %s", RELA_CT32); 3242 printf(" + %x\n", (uint32_t) r.r_addend); 3243 } else { 3244 if (re->options & RE_WW) 3245 printf("%16.16jx %16.16jx %-24.24s" 3246 " %16.16jx %s", RELA_CT64); 3247 else 3248 printf("%12.12jx %12.12jx %-19.19s" 3249 " %16.16jx %s", RELA_CT64); 3250 printf(" + %jx\n", (uintmax_t) r.r_addend); 3251 } 3252 } 3253 3254 #undef RELA_HDR 3255 #undef RELA_CT 3256 } 3257 3258 static void 3259 dump_reloc(struct readelf *re) 3260 { 3261 struct section *s; 3262 Elf_Data *d; 3263 int i, elferr; 3264 3265 for (i = 0; (size_t)i < re->shnum; i++) { 3266 s = &re->sl[i]; 3267 if (s->type == SHT_REL || s->type == SHT_RELA) { 3268 (void) elf_errno(); 3269 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 3270 elferr = elf_errno(); 3271 if (elferr != 0) 3272 warnx("elf_getdata failed: %s", 3273 elf_errmsg(elferr)); 3274 continue; 3275 } 3276 if (s->type == SHT_REL) 3277 dump_rel(re, s, d); 3278 else 3279 dump_rela(re, s, d); 3280 } 3281 } 3282 } 3283 3284 static void 3285 dump_symtab(struct readelf *re, int i) 3286 { 3287 struct section *s; 3288 Elf_Data *d; 3289 GElf_Sym sym; 3290 const char *name; 3291 int elferr, stab, j; 3292 3293 s = &re->sl[i]; 3294 stab = s->link; 3295 (void) elf_errno(); 3296 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 3297 elferr = elf_errno(); 3298 if (elferr != 0) 3299 warnx("elf_getdata failed: %s", elf_errmsg(elferr)); 3300 return; 3301 } 3302 if (d->d_size <= 0) 3303 return; 3304 printf("Symbol table (%s)", s->name); 3305 printf(" contains %ju entries:\n", s->sz / s->entsize); 3306 printf("%7s%9s%14s%5s%8s%6s%9s%5s\n", "Num:", "Value", "Size", "Type", 3307 "Bind", "Vis", "Ndx", "Name"); 3308 3309 for (j = 0; (uint64_t)j < s->sz / s->entsize; j++) { 3310 if (gelf_getsym(d, j, &sym) != &sym) { 3311 warnx("gelf_getsym failed: %s", elf_errmsg(-1)); 3312 continue; 3313 } 3314 printf("%6d:", j); 3315 printf(" %16.16jx", (uintmax_t)sym.st_value); 3316 printf(" %5ju", sym.st_size); 3317 printf(" %-7s", st_type(GELF_ST_TYPE(sym.st_info))); 3318 printf(" %-6s", st_bind(GELF_ST_BIND(sym.st_info))); 3319 printf(" %-8s", st_vis(GELF_ST_VISIBILITY(sym.st_other))); 3320 printf(" %3s", st_shndx(sym.st_shndx)); 3321 if ((name = elf_strptr(re->elf, stab, sym.st_name)) != NULL) 3322 printf(" %s", name); 3323 /* Append symbol version string for SHT_DYNSYM symbol table. */ 3324 if (s->type == SHT_DYNSYM && re->ver != NULL && 3325 re->vs != NULL && re->vs[j] > 1) { 3326 if (re->vs[j] & 0x8000 || 3327 re->ver[re->vs[j] & 0x7fff].type == 0) 3328 printf("@%s (%d)", 3329 re->ver[re->vs[j] & 0x7fff].name, 3330 re->vs[j] & 0x7fff); 3331 else 3332 printf("@@%s (%d)", re->ver[re->vs[j]].name, 3333 re->vs[j]); 3334 } 3335 putchar('\n'); 3336 } 3337 3338 } 3339 3340 static void 3341 dump_symtabs(struct readelf *re) 3342 { 3343 GElf_Dyn dyn; 3344 Elf_Data *d; 3345 struct section *s; 3346 uint64_t dyn_off; 3347 int elferr, i; 3348 3349 /* 3350 * If -D is specified, only dump the symbol table specified by 3351 * the DT_SYMTAB entry in the .dynamic section. 3352 */ 3353 dyn_off = 0; 3354 if (re->options & RE_DD) { 3355 s = NULL; 3356 for (i = 0; (size_t)i < re->shnum; i++) 3357 if (re->sl[i].type == SHT_DYNAMIC) { 3358 s = &re->sl[i]; 3359 break; 3360 } 3361 if (s == NULL) 3362 return; 3363 (void) elf_errno(); 3364 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 3365 elferr = elf_errno(); 3366 if (elferr != 0) 3367 warnx("elf_getdata failed: %s", elf_errmsg(-1)); 3368 return; 3369 } 3370 if (d->d_size <= 0) 3371 return; 3372 3373 for (i = 0; (uint64_t)i < s->sz / s->entsize; i++) { 3374 if (gelf_getdyn(d, i, &dyn) != &dyn) { 3375 warnx("gelf_getdyn failed: %s", elf_errmsg(-1)); 3376 continue; 3377 } 3378 if (dyn.d_tag == DT_SYMTAB) { 3379 dyn_off = dyn.d_un.d_val; 3380 break; 3381 } 3382 } 3383 } 3384 3385 /* Find and dump symbol tables. */ 3386 for (i = 0; (size_t)i < re->shnum; i++) { 3387 s = &re->sl[i]; 3388 if (s->type == SHT_SYMTAB || s->type == SHT_DYNSYM) { 3389 if (re->options & RE_DD) { 3390 if (dyn_off == s->addr) { 3391 dump_symtab(re, i); 3392 break; 3393 } 3394 } else 3395 dump_symtab(re, i); 3396 } 3397 } 3398 } 3399 3400 static void 3401 dump_svr4_hash(struct section *s) 3402 { 3403 Elf_Data *d; 3404 uint32_t *buf; 3405 uint32_t nbucket, nchain; 3406 uint32_t *bucket, *chain; 3407 uint32_t *bl, *c, maxl, total; 3408 int elferr, i, j; 3409 3410 /* Read and parse the content of .hash section. */ 3411 (void) elf_errno(); 3412 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 3413 elferr = elf_errno(); 3414 if (elferr != 0) 3415 warnx("elf_getdata failed: %s", elf_errmsg(elferr)); 3416 return; 3417 } 3418 if (d->d_size < 2 * sizeof(uint32_t)) { 3419 warnx(".hash section too small"); 3420 return; 3421 } 3422 buf = d->d_buf; 3423 nbucket = buf[0]; 3424 nchain = buf[1]; 3425 if (nbucket <= 0 || nchain <= 0) { 3426 warnx("Malformed .hash section"); 3427 return; 3428 } 3429 if (d->d_size != (nbucket + nchain + 2) * sizeof(uint32_t)) { 3430 warnx("Malformed .hash section"); 3431 return; 3432 } 3433 bucket = &buf[2]; 3434 chain = &buf[2 + nbucket]; 3435 3436 maxl = 0; 3437 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL) 3438 errx(EXIT_FAILURE, "calloc failed"); 3439 for (i = 0; (uint32_t)i < nbucket; i++) 3440 for (j = bucket[i]; j > 0 && (uint32_t)j < nchain; j = chain[j]) 3441 if (++bl[i] > maxl) 3442 maxl = bl[i]; 3443 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL) 3444 errx(EXIT_FAILURE, "calloc failed"); 3445 for (i = 0; (uint32_t)i < nbucket; i++) 3446 c[bl[i]]++; 3447 printf("\nHistogram for bucket list length (total of %u buckets):\n", 3448 nbucket); 3449 printf(" Length\tNumber\t\t%% of total\tCoverage\n"); 3450 total = 0; 3451 for (i = 0; (uint32_t)i <= maxl; i++) { 3452 total += c[i] * i; 3453 printf("%7u\t%-10u\t(%5.1f%%)\t%5.1f%%\n", i, c[i], 3454 c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1)); 3455 } 3456 free(c); 3457 free(bl); 3458 } 3459 3460 static void 3461 dump_svr4_hash64(struct readelf *re, struct section *s) 3462 { 3463 Elf_Data *d, dst; 3464 uint64_t *buf; 3465 uint64_t nbucket, nchain; 3466 uint64_t *bucket, *chain; 3467 uint64_t *bl, *c, maxl, total; 3468 int elferr, i, j; 3469 3470 /* 3471 * ALPHA uses 64-bit hash entries. Since libelf assumes that 3472 * .hash section contains only 32-bit entry, an explicit 3473 * gelf_xlatetom is needed here. 3474 */ 3475 (void) elf_errno(); 3476 if ((d = elf_rawdata(s->scn, NULL)) == NULL) { 3477 elferr = elf_errno(); 3478 if (elferr != 0) 3479 warnx("elf_rawdata failed: %s", 3480 elf_errmsg(elferr)); 3481 return; 3482 } 3483 d->d_type = ELF_T_XWORD; 3484 memcpy(&dst, d, sizeof(Elf_Data)); 3485 if (gelf_xlatetom(re->elf, &dst, d, 3486 re->ehdr.e_ident[EI_DATA]) != &dst) { 3487 warnx("gelf_xlatetom failed: %s", elf_errmsg(-1)); 3488 return; 3489 } 3490 if (dst.d_size < 2 * sizeof(uint64_t)) { 3491 warnx(".hash section too small"); 3492 return; 3493 } 3494 buf = dst.d_buf; 3495 nbucket = buf[0]; 3496 nchain = buf[1]; 3497 if (nbucket <= 0 || nchain <= 0) { 3498 warnx("Malformed .hash section"); 3499 return; 3500 } 3501 if (d->d_size != (nbucket + nchain + 2) * sizeof(uint32_t)) { 3502 warnx("Malformed .hash section"); 3503 return; 3504 } 3505 bucket = &buf[2]; 3506 chain = &buf[2 + nbucket]; 3507 3508 maxl = 0; 3509 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL) 3510 errx(EXIT_FAILURE, "calloc failed"); 3511 for (i = 0; (uint32_t)i < nbucket; i++) 3512 for (j = bucket[i]; j > 0 && (uint32_t)j < nchain; j = chain[j]) 3513 if (++bl[i] > maxl) 3514 maxl = bl[i]; 3515 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL) 3516 errx(EXIT_FAILURE, "calloc failed"); 3517 for (i = 0; (uint64_t)i < nbucket; i++) 3518 c[bl[i]]++; 3519 printf("Histogram for bucket list length (total of %ju buckets):\n", 3520 (uintmax_t)nbucket); 3521 printf(" Length\tNumber\t\t%% of total\tCoverage\n"); 3522 total = 0; 3523 for (i = 0; (uint64_t)i <= maxl; i++) { 3524 total += c[i] * i; 3525 printf("%7u\t%-10ju\t(%5.1f%%)\t%5.1f%%\n", i, (uintmax_t)c[i], 3526 c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1)); 3527 } 3528 free(c); 3529 free(bl); 3530 } 3531 3532 static void 3533 dump_gnu_hash(struct readelf *re, struct section *s) 3534 { 3535 struct section *ds; 3536 Elf_Data *d; 3537 uint32_t *buf; 3538 uint32_t *bucket, *chain; 3539 uint32_t nbucket, nchain, symndx, maskwords; 3540 uint32_t *bl, *c, maxl, total; 3541 int elferr, dynsymcount, i, j; 3542 3543 (void) elf_errno(); 3544 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 3545 elferr = elf_errno(); 3546 if (elferr != 0) 3547 warnx("elf_getdata failed: %s", 3548 elf_errmsg(elferr)); 3549 return; 3550 } 3551 if (d->d_size < 4 * sizeof(uint32_t)) { 3552 warnx(".gnu.hash section too small"); 3553 return; 3554 } 3555 buf = d->d_buf; 3556 nbucket = buf[0]; 3557 symndx = buf[1]; 3558 maskwords = buf[2]; 3559 buf += 4; 3560 ds = &re->sl[s->link]; 3561 dynsymcount = ds->sz / ds->entsize; 3562 nchain = dynsymcount - symndx; 3563 if (d->d_size != 4 * sizeof(uint32_t) + maskwords * 3564 (re->ec == ELFCLASS32 ? sizeof(uint32_t) : sizeof(uint64_t)) + 3565 (nbucket + nchain) * sizeof(uint32_t)) { 3566 warnx("Malformed .gnu.hash section"); 3567 return; 3568 } 3569 bucket = buf + (re->ec == ELFCLASS32 ? maskwords : maskwords * 2); 3570 chain = bucket + nbucket; 3571 3572 maxl = 0; 3573 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL) 3574 errx(EXIT_FAILURE, "calloc failed"); 3575 for (i = 0; (uint32_t)i < nbucket; i++) 3576 for (j = bucket[i]; j > 0 && (uint32_t)j - symndx < nchain; 3577 j++) { 3578 if (++bl[i] > maxl) 3579 maxl = bl[i]; 3580 if (chain[j - symndx] & 1) 3581 break; 3582 } 3583 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL) 3584 errx(EXIT_FAILURE, "calloc failed"); 3585 for (i = 0; (uint32_t)i < nbucket; i++) 3586 c[bl[i]]++; 3587 printf("Histogram for bucket list length (total of %u buckets):\n", 3588 nbucket); 3589 printf(" Length\tNumber\t\t%% of total\tCoverage\n"); 3590 total = 0; 3591 for (i = 0; (uint32_t)i <= maxl; i++) { 3592 total += c[i] * i; 3593 printf("%7u\t%-10u\t(%5.1f%%)\t%5.1f%%\n", i, c[i], 3594 c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1)); 3595 } 3596 free(c); 3597 free(bl); 3598 } 3599 3600 static void 3601 dump_hash(struct readelf *re) 3602 { 3603 struct section *s; 3604 int i; 3605 3606 for (i = 0; (size_t) i < re->shnum; i++) { 3607 s = &re->sl[i]; 3608 if (s->type == SHT_HASH || s->type == SHT_GNU_HASH) { 3609 if (s->type == SHT_GNU_HASH) 3610 dump_gnu_hash(re, s); 3611 else if (re->ehdr.e_machine == EM_ALPHA && 3612 s->entsize == 8) 3613 dump_svr4_hash64(re, s); 3614 else 3615 dump_svr4_hash(s); 3616 } 3617 } 3618 } 3619 3620 static void 3621 dump_notes(struct readelf *re) 3622 { 3623 struct section *s; 3624 const char *rawfile; 3625 GElf_Phdr phdr; 3626 Elf_Data *d; 3627 size_t phnum; 3628 int i, elferr; 3629 3630 if (re->ehdr.e_type == ET_CORE) { 3631 /* 3632 * Search program headers in the core file for 3633 * PT_NOTE entry. 3634 */ 3635 if (elf_getphnum(re->elf, &phnum) == 0) { 3636 warnx("elf_getphnum failed: %s", elf_errmsg(-1)); 3637 return; 3638 } 3639 if (phnum == 0) 3640 return; 3641 if ((rawfile = elf_rawfile(re->elf, NULL)) == NULL) { 3642 warnx("elf_rawfile failed: %s", elf_errmsg(-1)); 3643 return; 3644 } 3645 for (i = 0; (size_t) i < phnum; i++) { 3646 if (gelf_getphdr(re->elf, i, &phdr) != &phdr) { 3647 warnx("gelf_getphdr failed: %s", 3648 elf_errmsg(-1)); 3649 continue; 3650 } 3651 if (phdr.p_type == PT_NOTE) 3652 dump_notes_content(re, rawfile + phdr.p_offset, 3653 phdr.p_filesz, phdr.p_offset); 3654 } 3655 3656 } else { 3657 /* 3658 * For objects other than core files, Search for 3659 * SHT_NOTE sections. 3660 */ 3661 for (i = 0; (size_t) i < re->shnum; i++) { 3662 s = &re->sl[i]; 3663 if (s->type == SHT_NOTE) { 3664 (void) elf_errno(); 3665 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 3666 elferr = elf_errno(); 3667 if (elferr != 0) 3668 warnx("elf_getdata failed: %s", 3669 elf_errmsg(elferr)); 3670 continue; 3671 } 3672 dump_notes_content(re, d->d_buf, d->d_size, 3673 s->off); 3674 } 3675 } 3676 } 3677 } 3678 3679 static void 3680 dump_notes_content(struct readelf *re, const char *buf, size_t sz, off_t off) 3681 { 3682 Elf_Note *note; 3683 const char *end, *name; 3684 3685 printf("\nNotes at offset %#010jx with length %#010jx:\n", 3686 (uintmax_t) off, (uintmax_t) sz); 3687 printf(" %-13s %-15s %s\n", "Owner", "Data size", "Description"); 3688 end = buf + sz; 3689 while (buf < end) { 3690 if (buf + sizeof(*note) > end) { 3691 warnx("invalid note header"); 3692 return; 3693 } 3694 note = (Elf_Note *)(uintptr_t) buf; 3695 name = (char *)(uintptr_t)(note + 1); 3696 /* 3697 * The name field is required to be nul-terminated, and 3698 * n_namesz includes the terminating nul in observed 3699 * implementations (contrary to the ELF-64 spec). A special 3700 * case is needed for cores generated by some older Linux 3701 * versions, which write a note named "CORE" without a nul 3702 * terminator and n_namesz = 4. 3703 */ 3704 if (note->n_namesz == 0) 3705 name = ""; 3706 else if (note->n_namesz == 4 && strncmp(name, "CORE", 4) == 0) 3707 name = "CORE"; 3708 else if (strnlen(name, note->n_namesz) >= note->n_namesz) 3709 name = "<invalid>"; 3710 printf(" %-13s %#010jx", name, (uintmax_t) note->n_descsz); 3711 printf(" %s\n", note_type(name, re->ehdr.e_type, 3712 note->n_type)); 3713 buf += sizeof(Elf_Note) + roundup2(note->n_namesz, 4) + 3714 roundup2(note->n_descsz, 4); 3715 } 3716 } 3717 3718 /* 3719 * Symbol versioning sections are the same for 32bit and 64bit 3720 * ELF objects. 3721 */ 3722 #define Elf_Verdef Elf32_Verdef 3723 #define Elf_Verdaux Elf32_Verdaux 3724 #define Elf_Verneed Elf32_Verneed 3725 #define Elf_Vernaux Elf32_Vernaux 3726 3727 #define SAVE_VERSION_NAME(x, n, t) \ 3728 do { \ 3729 while (x >= re->ver_sz) { \ 3730 nv = realloc(re->ver, \ 3731 sizeof(*re->ver) * re->ver_sz * 2); \ 3732 if (nv == NULL) { \ 3733 warn("realloc failed"); \ 3734 free(re->ver); \ 3735 return; \ 3736 } \ 3737 re->ver = nv; \ 3738 for (i = re->ver_sz; i < re->ver_sz * 2; i++) { \ 3739 re->ver[i].name = NULL; \ 3740 re->ver[i].type = 0; \ 3741 } \ 3742 re->ver_sz *= 2; \ 3743 } \ 3744 if (x > 1) { \ 3745 re->ver[x].name = n; \ 3746 re->ver[x].type = t; \ 3747 } \ 3748 } while (0) 3749 3750 3751 static void 3752 dump_verdef(struct readelf *re, int dump) 3753 { 3754 struct section *s; 3755 struct symver *nv; 3756 Elf_Data *d; 3757 Elf_Verdef *vd; 3758 Elf_Verdaux *vda; 3759 uint8_t *buf, *end, *buf2; 3760 const char *name; 3761 int elferr, i, j; 3762 3763 if ((s = re->vd_s) == NULL) 3764 return; 3765 3766 if (re->ver == NULL) { 3767 re->ver_sz = 16; 3768 if ((re->ver = calloc(re->ver_sz, sizeof(*re->ver))) == 3769 NULL) { 3770 warn("calloc failed"); 3771 return; 3772 } 3773 re->ver[0].name = "*local*"; 3774 re->ver[1].name = "*global*"; 3775 } 3776 3777 if (dump) 3778 printf("\nVersion definition section (%s):\n", s->name); 3779 (void) elf_errno(); 3780 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 3781 elferr = elf_errno(); 3782 if (elferr != 0) 3783 warnx("elf_getdata failed: %s", elf_errmsg(elferr)); 3784 return; 3785 } 3786 if (d->d_size == 0) 3787 return; 3788 3789 buf = d->d_buf; 3790 end = buf + d->d_size; 3791 while (buf + sizeof(Elf_Verdef) <= end) { 3792 vd = (Elf_Verdef *) (uintptr_t) buf; 3793 if (dump) { 3794 printf(" 0x%4.4lx", (unsigned long) 3795 (buf - (uint8_t *)d->d_buf)); 3796 printf(" vd_version: %u vd_flags: %d" 3797 " vd_ndx: %u vd_cnt: %u", vd->vd_version, 3798 vd->vd_flags, vd->vd_ndx, vd->vd_cnt); 3799 } 3800 buf2 = buf + vd->vd_aux; 3801 j = 0; 3802 while (buf2 + sizeof(Elf_Verdaux) <= end && j < vd->vd_cnt) { 3803 vda = (Elf_Verdaux *) (uintptr_t) buf2; 3804 name = get_string(re, s->link, vda->vda_name); 3805 if (j == 0) { 3806 if (dump) 3807 printf(" vda_name: %s\n", name); 3808 SAVE_VERSION_NAME((int)vd->vd_ndx, name, 1); 3809 } else if (dump) 3810 printf(" 0x%4.4lx parent: %s\n", 3811 (unsigned long) (buf2 - 3812 (uint8_t *)d->d_buf), name); 3813 if (vda->vda_next == 0) 3814 break; 3815 buf2 += vda->vda_next; 3816 j++; 3817 } 3818 if (vd->vd_next == 0) 3819 break; 3820 buf += vd->vd_next; 3821 } 3822 } 3823 3824 static void 3825 dump_verneed(struct readelf *re, int dump) 3826 { 3827 struct section *s; 3828 struct symver *nv; 3829 Elf_Data *d; 3830 Elf_Verneed *vn; 3831 Elf_Vernaux *vna; 3832 uint8_t *buf, *end, *buf2; 3833 const char *name; 3834 int elferr, i, j; 3835 3836 if ((s = re->vn_s) == NULL) 3837 return; 3838 3839 if (re->ver == NULL) { 3840 re->ver_sz = 16; 3841 if ((re->ver = calloc(re->ver_sz, sizeof(*re->ver))) == 3842 NULL) { 3843 warn("calloc failed"); 3844 return; 3845 } 3846 re->ver[0].name = "*local*"; 3847 re->ver[1].name = "*global*"; 3848 } 3849 3850 if (dump) 3851 printf("\nVersion needed section (%s):\n", s->name); 3852 (void) elf_errno(); 3853 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 3854 elferr = elf_errno(); 3855 if (elferr != 0) 3856 warnx("elf_getdata failed: %s", elf_errmsg(elferr)); 3857 return; 3858 } 3859 if (d->d_size == 0) 3860 return; 3861 3862 buf = d->d_buf; 3863 end = buf + d->d_size; 3864 while (buf + sizeof(Elf_Verneed) <= end) { 3865 vn = (Elf_Verneed *) (uintptr_t) buf; 3866 if (dump) { 3867 printf(" 0x%4.4lx", (unsigned long) 3868 (buf - (uint8_t *)d->d_buf)); 3869 printf(" vn_version: %u vn_file: %s vn_cnt: %u\n", 3870 vn->vn_version, 3871 get_string(re, s->link, vn->vn_file), 3872 vn->vn_cnt); 3873 } 3874 buf2 = buf + vn->vn_aux; 3875 j = 0; 3876 while (buf2 + sizeof(Elf_Vernaux) <= end && j < vn->vn_cnt) { 3877 vna = (Elf32_Vernaux *) (uintptr_t) buf2; 3878 if (dump) 3879 printf(" 0x%4.4lx", (unsigned long) 3880 (buf2 - (uint8_t *)d->d_buf)); 3881 name = get_string(re, s->link, vna->vna_name); 3882 if (dump) 3883 printf(" vna_name: %s vna_flags: %u" 3884 " vna_other: %u\n", name, 3885 vna->vna_flags, vna->vna_other); 3886 SAVE_VERSION_NAME((int)vna->vna_other, name, 0); 3887 if (vna->vna_next == 0) 3888 break; 3889 buf2 += vna->vna_next; 3890 j++; 3891 } 3892 if (vn->vn_next == 0) 3893 break; 3894 buf += vn->vn_next; 3895 } 3896 } 3897 3898 static void 3899 dump_versym(struct readelf *re) 3900 { 3901 int i; 3902 3903 if (re->vs_s == NULL || re->ver == NULL || re->vs == NULL) 3904 return; 3905 printf("\nVersion symbol section (%s):\n", re->vs_s->name); 3906 for (i = 0; i < re->vs_sz; i++) { 3907 if ((i & 3) == 0) { 3908 if (i > 0) 3909 putchar('\n'); 3910 printf(" %03x:", i); 3911 } 3912 if (re->vs[i] & 0x8000) 3913 printf(" %3xh %-12s ", re->vs[i] & 0x7fff, 3914 re->ver[re->vs[i] & 0x7fff].name); 3915 else 3916 printf(" %3x %-12s ", re->vs[i], 3917 re->ver[re->vs[i]].name); 3918 } 3919 putchar('\n'); 3920 } 3921 3922 static void 3923 dump_ver(struct readelf *re) 3924 { 3925 3926 if (re->vs_s && re->ver && re->vs) 3927 dump_versym(re); 3928 if (re->vd_s) 3929 dump_verdef(re, 1); 3930 if (re->vn_s) 3931 dump_verneed(re, 1); 3932 } 3933 3934 static void 3935 search_ver(struct readelf *re) 3936 { 3937 struct section *s; 3938 Elf_Data *d; 3939 int elferr, i; 3940 3941 for (i = 0; (size_t) i < re->shnum; i++) { 3942 s = &re->sl[i]; 3943 if (s->type == SHT_SUNW_versym) 3944 re->vs_s = s; 3945 if (s->type == SHT_SUNW_verneed) 3946 re->vn_s = s; 3947 if (s->type == SHT_SUNW_verdef) 3948 re->vd_s = s; 3949 } 3950 if (re->vd_s) 3951 dump_verdef(re, 0); 3952 if (re->vn_s) 3953 dump_verneed(re, 0); 3954 if (re->vs_s && re->ver != NULL) { 3955 (void) elf_errno(); 3956 if ((d = elf_getdata(re->vs_s->scn, NULL)) == NULL) { 3957 elferr = elf_errno(); 3958 if (elferr != 0) 3959 warnx("elf_getdata failed: %s", 3960 elf_errmsg(elferr)); 3961 return; 3962 } 3963 if (d->d_size == 0) 3964 return; 3965 re->vs = d->d_buf; 3966 re->vs_sz = d->d_size / sizeof(Elf32_Half); 3967 } 3968 } 3969 3970 #undef Elf_Verdef 3971 #undef Elf_Verdaux 3972 #undef Elf_Verneed 3973 #undef Elf_Vernaux 3974 #undef SAVE_VERSION_NAME 3975 3976 /* 3977 * Elf32_Lib and Elf64_Lib are identical. 3978 */ 3979 #define Elf_Lib Elf32_Lib 3980 3981 static void 3982 dump_liblist(struct readelf *re) 3983 { 3984 struct section *s; 3985 struct tm *t; 3986 time_t ti; 3987 char tbuf[20]; 3988 Elf_Data *d; 3989 Elf_Lib *lib; 3990 int i, j, k, elferr, first; 3991 3992 for (i = 0; (size_t) i < re->shnum; i++) { 3993 s = &re->sl[i]; 3994 if (s->type != SHT_GNU_LIBLIST) 3995 continue; 3996 (void) elf_errno(); 3997 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 3998 elferr = elf_errno(); 3999 if (elferr != 0) 4000 warnx("elf_getdata failed: %s", 4001 elf_errmsg(elferr)); 4002 continue; 4003 } 4004 if (d->d_size <= 0) 4005 continue; 4006 lib = d->d_buf; 4007 printf("\nLibrary list section '%s' ", s->name); 4008 printf("contains %ju entries:\n", s->sz / s->entsize); 4009 printf("%12s%24s%18s%10s%6s\n", "Library", "Time Stamp", 4010 "Checksum", "Version", "Flags"); 4011 for (j = 0; (uint64_t) j < s->sz / s->entsize; j++) { 4012 printf("%3d: ", j); 4013 printf("%-20.20s ", 4014 get_string(re, s->link, lib->l_name)); 4015 ti = lib->l_time_stamp; 4016 t = gmtime(&ti); 4017 snprintf(tbuf, sizeof(tbuf), "%04d-%02d-%02dT%02d:%02d" 4018 ":%2d", t->tm_year + 1900, t->tm_mon + 1, 4019 t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec); 4020 printf("%-19.19s ", tbuf); 4021 printf("0x%08x ", lib->l_checksum); 4022 printf("%-7d %#x", lib->l_version, lib->l_flags); 4023 if (lib->l_flags != 0) { 4024 first = 1; 4025 putchar('('); 4026 for (k = 0; l_flag[k].name != NULL; k++) { 4027 if ((l_flag[k].value & lib->l_flags) == 4028 0) 4029 continue; 4030 if (!first) 4031 putchar(','); 4032 else 4033 first = 0; 4034 printf("%s", l_flag[k].name); 4035 } 4036 putchar(')'); 4037 } 4038 putchar('\n'); 4039 lib++; 4040 } 4041 } 4042 } 4043 4044 #undef Elf_Lib 4045 4046 static uint8_t * 4047 dump_unknown_tag(uint64_t tag, uint8_t *p) 4048 { 4049 uint64_t val; 4050 4051 /* 4052 * According to ARM EABI: For tags > 32, even numbered tags have 4053 * a ULEB128 param and odd numbered ones have NUL-terminated 4054 * string param. This rule probably also applies for tags <= 32 4055 * if the object arch is not ARM. 4056 */ 4057 4058 printf(" Tag_unknown_%ju: ", (uintmax_t) tag); 4059 4060 if (tag & 1) { 4061 printf("%s\n", (char *) p); 4062 p += strlen((char *) p) + 1; 4063 } else { 4064 val = _decode_uleb128(&p); 4065 printf("%ju\n", (uintmax_t) val); 4066 } 4067 4068 return (p); 4069 } 4070 4071 static uint8_t * 4072 dump_compatibility_tag(uint8_t *p) 4073 { 4074 uint64_t val; 4075 4076 val = _decode_uleb128(&p); 4077 printf("flag = %ju, vendor = %s\n", val, p); 4078 p += strlen((char *) p) + 1; 4079 4080 return (p); 4081 } 4082 4083 static void 4084 dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe) 4085 { 4086 uint64_t tag, val; 4087 size_t i; 4088 int found, desc; 4089 4090 (void) re; 4091 4092 while (p < pe) { 4093 tag = _decode_uleb128(&p); 4094 found = desc = 0; 4095 for (i = 0; i < sizeof(aeabi_tags) / sizeof(aeabi_tags[0]); 4096 i++) { 4097 if (tag == aeabi_tags[i].tag) { 4098 found = 1; 4099 printf(" %s: ", aeabi_tags[i].s_tag); 4100 if (aeabi_tags[i].get_desc) { 4101 desc = 1; 4102 val = _decode_uleb128(&p); 4103 printf("%s\n", 4104 aeabi_tags[i].get_desc(val)); 4105 } 4106 break; 4107 } 4108 if (tag < aeabi_tags[i].tag) 4109 break; 4110 } 4111 if (!found) { 4112 p = dump_unknown_tag(tag, p); 4113 continue; 4114 } 4115 if (desc) 4116 continue; 4117 4118 switch (tag) { 4119 case 4: /* Tag_CPU_raw_name */ 4120 case 5: /* Tag_CPU_name */ 4121 case 67: /* Tag_conformance */ 4122 printf("%s\n", (char *) p); 4123 p += strlen((char *) p) + 1; 4124 break; 4125 case 32: /* Tag_compatibility */ 4126 p = dump_compatibility_tag(p); 4127 break; 4128 case 64: /* Tag_nodefaults */ 4129 /* ignored, written as 0. */ 4130 (void) _decode_uleb128(&p); 4131 printf("True\n"); 4132 break; 4133 case 65: /* Tag_also_compatible_with */ 4134 val = _decode_uleb128(&p); 4135 /* Must be Tag_CPU_arch */ 4136 if (val != 6) { 4137 printf("unknown\n"); 4138 break; 4139 } 4140 val = _decode_uleb128(&p); 4141 printf("%s\n", aeabi_cpu_arch(val)); 4142 /* Skip NUL terminator. */ 4143 p++; 4144 break; 4145 default: 4146 putchar('\n'); 4147 break; 4148 } 4149 } 4150 } 4151 4152 #ifndef Tag_GNU_MIPS_ABI_FP 4153 #define Tag_GNU_MIPS_ABI_FP 4 4154 #endif 4155 4156 static void 4157 dump_mips_attributes(struct readelf *re, uint8_t *p, uint8_t *pe) 4158 { 4159 uint64_t tag, val; 4160 4161 (void) re; 4162 4163 while (p < pe) { 4164 tag = _decode_uleb128(&p); 4165 switch (tag) { 4166 case Tag_GNU_MIPS_ABI_FP: 4167 val = _decode_uleb128(&p); 4168 printf(" Tag_GNU_MIPS_ABI_FP: %s\n", mips_abi_fp(val)); 4169 break; 4170 case 32: /* Tag_compatibility */ 4171 p = dump_compatibility_tag(p); 4172 break; 4173 default: 4174 p = dump_unknown_tag(tag, p); 4175 break; 4176 } 4177 } 4178 } 4179 4180 #ifndef Tag_GNU_Power_ABI_FP 4181 #define Tag_GNU_Power_ABI_FP 4 4182 #endif 4183 4184 #ifndef Tag_GNU_Power_ABI_Vector 4185 #define Tag_GNU_Power_ABI_Vector 8 4186 #endif 4187 4188 static void 4189 dump_ppc_attributes(uint8_t *p, uint8_t *pe) 4190 { 4191 uint64_t tag, val; 4192 4193 while (p < pe) { 4194 tag = _decode_uleb128(&p); 4195 switch (tag) { 4196 case Tag_GNU_Power_ABI_FP: 4197 val = _decode_uleb128(&p); 4198 printf(" Tag_GNU_Power_ABI_FP: %s\n", ppc_abi_fp(val)); 4199 break; 4200 case Tag_GNU_Power_ABI_Vector: 4201 val = _decode_uleb128(&p); 4202 printf(" Tag_GNU_Power_ABI_Vector: %s\n", 4203 ppc_abi_vector(val)); 4204 break; 4205 case 32: /* Tag_compatibility */ 4206 p = dump_compatibility_tag(p); 4207 break; 4208 default: 4209 p = dump_unknown_tag(tag, p); 4210 break; 4211 } 4212 } 4213 } 4214 4215 static void 4216 dump_attributes(struct readelf *re) 4217 { 4218 struct section *s; 4219 Elf_Data *d; 4220 uint8_t *p, *sp; 4221 size_t len, seclen, nlen, sublen; 4222 uint64_t val; 4223 int tag, i, elferr; 4224 4225 for (i = 0; (size_t) i < re->shnum; i++) { 4226 s = &re->sl[i]; 4227 if (s->type != SHT_GNU_ATTRIBUTES && 4228 (re->ehdr.e_machine != EM_ARM || s->type != SHT_LOPROC + 3)) 4229 continue; 4230 (void) elf_errno(); 4231 if ((d = elf_rawdata(s->scn, NULL)) == NULL) { 4232 elferr = elf_errno(); 4233 if (elferr != 0) 4234 warnx("elf_rawdata failed: %s", 4235 elf_errmsg(elferr)); 4236 continue; 4237 } 4238 if (d->d_size <= 0) 4239 continue; 4240 p = d->d_buf; 4241 if (*p != 'A') { 4242 printf("Unknown Attribute Section Format: %c\n", 4243 (char) *p); 4244 continue; 4245 } 4246 len = d->d_size - 1; 4247 p++; 4248 while (len > 0) { 4249 if (len < 4) { 4250 warnx("truncated attribute section length"); 4251 break; 4252 } 4253 seclen = re->dw_decode(&p, 4); 4254 if (seclen > len) { 4255 warnx("invalid attribute section length"); 4256 break; 4257 } 4258 len -= seclen; 4259 nlen = strlen((char *) p) + 1; 4260 if (nlen + 4 > seclen) { 4261 warnx("invalid attribute section name"); 4262 break; 4263 } 4264 printf("Attribute Section: %s\n", (char *) p); 4265 p += nlen; 4266 seclen -= nlen + 4; 4267 while (seclen > 0) { 4268 sp = p; 4269 tag = *p++; 4270 sublen = re->dw_decode(&p, 4); 4271 if (sublen > seclen) { 4272 warnx("invalid attribute sub-section" 4273 " length"); 4274 break; 4275 } 4276 seclen -= sublen; 4277 printf("%s", top_tag(tag)); 4278 if (tag == 2 || tag == 3) { 4279 putchar(':'); 4280 for (;;) { 4281 val = _decode_uleb128(&p); 4282 if (val == 0) 4283 break; 4284 printf(" %ju", (uintmax_t) val); 4285 } 4286 } 4287 putchar('\n'); 4288 if (re->ehdr.e_machine == EM_ARM && 4289 s->type == SHT_LOPROC + 3) 4290 dump_arm_attributes(re, p, sp + sublen); 4291 else if (re->ehdr.e_machine == EM_MIPS || 4292 re->ehdr.e_machine == EM_MIPS_RS3_LE) 4293 dump_mips_attributes(re, p, 4294 sp + sublen); 4295 else if (re->ehdr.e_machine == EM_PPC) 4296 dump_ppc_attributes(p, sp + sublen); 4297 p = sp + sublen; 4298 } 4299 } 4300 } 4301 } 4302 4303 static void 4304 dump_mips_specific_info(struct readelf *re) 4305 { 4306 struct section *s; 4307 int i, options_found; 4308 4309 options_found = 0; 4310 s = NULL; 4311 for (i = 0; (size_t) i < re->shnum; i++) { 4312 s = &re->sl[i]; 4313 if (s->name != NULL && (!strcmp(s->name, ".MIPS.options") || 4314 (s->type == SHT_MIPS_OPTIONS))) { 4315 dump_mips_options(re, s); 4316 options_found = 1; 4317 } 4318 } 4319 4320 /* 4321 * According to SGI mips64 spec, .reginfo should be ignored if 4322 * .MIPS.options section is present. 4323 */ 4324 if (!options_found) { 4325 for (i = 0; (size_t) i < re->shnum; i++) { 4326 s = &re->sl[i]; 4327 if (s->name != NULL && (!strcmp(s->name, ".reginfo") || 4328 (s->type == SHT_MIPS_REGINFO))) 4329 dump_mips_reginfo(re, s); 4330 } 4331 } 4332 } 4333 4334 static void 4335 dump_mips_reginfo(struct readelf *re, struct section *s) 4336 { 4337 Elf_Data *d; 4338 int elferr; 4339 4340 (void) elf_errno(); 4341 if ((d = elf_rawdata(s->scn, NULL)) == NULL) { 4342 elferr = elf_errno(); 4343 if (elferr != 0) 4344 warnx("elf_rawdata failed: %s", 4345 elf_errmsg(elferr)); 4346 return; 4347 } 4348 if (d->d_size <= 0) 4349 return; 4350 4351 printf("\nSection '%s' contains %ju entries:\n", s->name, 4352 s->sz / s->entsize); 4353 dump_mips_odk_reginfo(re, d->d_buf, d->d_size); 4354 } 4355 4356 static void 4357 dump_mips_options(struct readelf *re, struct section *s) 4358 { 4359 Elf_Data *d; 4360 uint32_t info; 4361 uint16_t sndx; 4362 uint8_t *p, *pe; 4363 uint8_t kind, size; 4364 int elferr; 4365 4366 (void) elf_errno(); 4367 if ((d = elf_rawdata(s->scn, NULL)) == NULL) { 4368 elferr = elf_errno(); 4369 if (elferr != 0) 4370 warnx("elf_rawdata failed: %s", 4371 elf_errmsg(elferr)); 4372 return; 4373 } 4374 if (d->d_size == 0) 4375 return; 4376 4377 printf("\nSection %s contains:\n", s->name); 4378 p = d->d_buf; 4379 pe = p + d->d_size; 4380 while (p < pe) { 4381 kind = re->dw_decode(&p, 1); 4382 size = re->dw_decode(&p, 1); 4383 sndx = re->dw_decode(&p, 2); 4384 info = re->dw_decode(&p, 4); 4385 switch (kind) { 4386 case ODK_REGINFO: 4387 dump_mips_odk_reginfo(re, p, size - 8); 4388 break; 4389 case ODK_EXCEPTIONS: 4390 printf(" EXCEPTIONS FPU_MIN: %#x\n", 4391 info & OEX_FPU_MIN); 4392 printf("%11.11s FPU_MAX: %#x\n", "", 4393 info & OEX_FPU_MAX); 4394 dump_mips_option_flags("", mips_exceptions_option, 4395 info); 4396 break; 4397 case ODK_PAD: 4398 printf(" %-10.10s section: %ju\n", "OPAD", 4399 (uintmax_t) sndx); 4400 dump_mips_option_flags("", mips_pad_option, info); 4401 break; 4402 case ODK_HWPATCH: 4403 dump_mips_option_flags("HWPATCH", mips_hwpatch_option, 4404 info); 4405 break; 4406 case ODK_HWAND: 4407 dump_mips_option_flags("HWAND", mips_hwa_option, info); 4408 break; 4409 case ODK_HWOR: 4410 dump_mips_option_flags("HWOR", mips_hwo_option, info); 4411 break; 4412 case ODK_FILL: 4413 printf(" %-10.10s %#jx\n", "FILL", (uintmax_t) info); 4414 break; 4415 case ODK_TAGS: 4416 printf(" %-10.10s\n", "TAGS"); 4417 break; 4418 case ODK_GP_GROUP: 4419 printf(" %-10.10s GP group number: %#x\n", "GP_GROUP", 4420 info & 0xFFFF); 4421 if (info & 0x10000) 4422 printf(" %-10.10s GP group is " 4423 "self-contained\n", ""); 4424 break; 4425 case ODK_IDENT: 4426 printf(" %-10.10s default GP group number: %#x\n", 4427 "IDENT", info & 0xFFFF); 4428 if (info & 0x10000) 4429 printf(" %-10.10s default GP group is " 4430 "self-contained\n", ""); 4431 break; 4432 case ODK_PAGESIZE: 4433 printf(" %-10.10s\n", "PAGESIZE"); 4434 break; 4435 default: 4436 break; 4437 } 4438 p += size - 8; 4439 } 4440 } 4441 4442 static void 4443 dump_mips_option_flags(const char *name, struct mips_option *opt, uint64_t info) 4444 { 4445 int first; 4446 4447 first = 1; 4448 for (; opt->desc != NULL; opt++) { 4449 if (info & opt->flag) { 4450 printf(" %-10.10s %s\n", first ? name : "", 4451 opt->desc); 4452 first = 0; 4453 } 4454 } 4455 } 4456 4457 static void 4458 dump_mips_odk_reginfo(struct readelf *re, uint8_t *p, size_t sz) 4459 { 4460 uint32_t ri_gprmask; 4461 uint32_t ri_cprmask[4]; 4462 uint64_t ri_gp_value; 4463 uint8_t *pe; 4464 int i; 4465 4466 pe = p + sz; 4467 while (p < pe) { 4468 ri_gprmask = re->dw_decode(&p, 4); 4469 /* Skip ri_pad padding field for mips64. */ 4470 if (re->ec == ELFCLASS64) 4471 re->dw_decode(&p, 4); 4472 for (i = 0; i < 4; i++) 4473 ri_cprmask[i] = re->dw_decode(&p, 4); 4474 if (re->ec == ELFCLASS32) 4475 ri_gp_value = re->dw_decode(&p, 4); 4476 else 4477 ri_gp_value = re->dw_decode(&p, 8); 4478 printf(" %s ", option_kind(ODK_REGINFO)); 4479 printf("ri_gprmask: 0x%08jx\n", (uintmax_t) ri_gprmask); 4480 for (i = 0; i < 4; i++) 4481 printf("%11.11s ri_cprmask[%d]: 0x%08jx\n", "", i, 4482 (uintmax_t) ri_cprmask[i]); 4483 printf("%12.12s", ""); 4484 printf("ri_gp_value: %#jx\n", (uintmax_t) ri_gp_value); 4485 } 4486 } 4487 4488 static void 4489 dump_arch_specific_info(struct readelf *re) 4490 { 4491 4492 dump_liblist(re); 4493 dump_attributes(re); 4494 4495 switch (re->ehdr.e_machine) { 4496 case EM_MIPS: 4497 case EM_MIPS_RS3_LE: 4498 dump_mips_specific_info(re); 4499 default: 4500 break; 4501 } 4502 } 4503 4504 static const char * 4505 dwarf_regname(struct readelf *re, unsigned int num) 4506 { 4507 static char rx[32]; 4508 const char *rn; 4509 4510 if ((rn = dwarf_reg(re->ehdr.e_machine, num)) != NULL) 4511 return (rn); 4512 4513 snprintf(rx, sizeof(rx), "r%u", num); 4514 4515 return (rx); 4516 } 4517 4518 static void 4519 dump_dwarf_line(struct readelf *re) 4520 { 4521 struct section *s; 4522 Dwarf_Die die; 4523 Dwarf_Error de; 4524 Dwarf_Half tag, version, pointer_size; 4525 Dwarf_Unsigned offset, endoff, length, hdrlen, dirndx, mtime, fsize; 4526 Dwarf_Small minlen, defstmt, lrange, opbase, oplen; 4527 Elf_Data *d; 4528 char *pn; 4529 uint64_t address, file, line, column, isa, opsize, udelta; 4530 int64_t sdelta; 4531 uint8_t *p, *pe; 4532 int8_t lbase; 4533 int i, is_stmt, dwarf_size, elferr, ret; 4534 4535 printf("\nDump of debug contents of section .debug_line:\n"); 4536 4537 s = NULL; 4538 for (i = 0; (size_t) i < re->shnum; i++) { 4539 s = &re->sl[i]; 4540 if (s->name != NULL && !strcmp(s->name, ".debug_line")) 4541 break; 4542 } 4543 if ((size_t) i >= re->shnum) 4544 return; 4545 4546 (void) elf_errno(); 4547 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 4548 elferr = elf_errno(); 4549 if (elferr != 0) 4550 warnx("elf_getdata failed: %s", elf_errmsg(-1)); 4551 return; 4552 } 4553 if (d->d_size <= 0) 4554 return; 4555 4556 while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL, 4557 NULL, &de)) == DW_DLV_OK) { 4558 die = NULL; 4559 while (dwarf_siblingof(re->dbg, die, &die, &de) == DW_DLV_OK) { 4560 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { 4561 warnx("dwarf_tag failed: %s", 4562 dwarf_errmsg(de)); 4563 return; 4564 } 4565 /* XXX: What about DW_TAG_partial_unit? */ 4566 if (tag == DW_TAG_compile_unit) 4567 break; 4568 } 4569 if (die == NULL) { 4570 warnx("could not find DW_TAG_compile_unit die"); 4571 return; 4572 } 4573 if (dwarf_attrval_unsigned(die, DW_AT_stmt_list, &offset, 4574 &de) != DW_DLV_OK) 4575 continue; 4576 4577 length = re->dw_read(d, &offset, 4); 4578 if (length == 0xffffffff) { 4579 dwarf_size = 8; 4580 length = re->dw_read(d, &offset, 8); 4581 } else 4582 dwarf_size = 4; 4583 4584 if (length > d->d_size - offset) { 4585 warnx("invalid .dwarf_line section"); 4586 continue; 4587 } 4588 4589 endoff = offset + length; 4590 version = re->dw_read(d, &offset, 2); 4591 hdrlen = re->dw_read(d, &offset, dwarf_size); 4592 minlen = re->dw_read(d, &offset, 1); 4593 defstmt = re->dw_read(d, &offset, 1); 4594 lbase = re->dw_read(d, &offset, 1); 4595 lrange = re->dw_read(d, &offset, 1); 4596 opbase = re->dw_read(d, &offset, 1); 4597 4598 printf("\n"); 4599 printf(" Length:\t\t\t%ju\n", (uintmax_t) length); 4600 printf(" DWARF version:\t\t%u\n", version); 4601 printf(" Prologue Length:\t\t%ju\n", (uintmax_t) hdrlen); 4602 printf(" Minimum Instruction Length:\t%u\n", minlen); 4603 printf(" Initial value of 'is_stmt':\t%u\n", defstmt); 4604 printf(" Line Base:\t\t\t%d\n", lbase); 4605 printf(" Line Range:\t\t\t%u\n", lrange); 4606 printf(" Opcode Base:\t\t\t%u\n", opbase); 4607 (void) dwarf_get_address_size(re->dbg, &pointer_size, &de); 4608 printf(" (Pointer size:\t\t%u)\n", pointer_size); 4609 4610 printf("\n"); 4611 printf(" Opcodes:\n"); 4612 for (i = 1; i < opbase; i++) { 4613 oplen = re->dw_read(d, &offset, 1); 4614 printf(" Opcode %d has %u args\n", i, oplen); 4615 } 4616 4617 printf("\n"); 4618 printf(" The Directory Table:\n"); 4619 p = (uint8_t *) d->d_buf + offset; 4620 while (*p != '\0') { 4621 printf(" %s\n", (char *) p); 4622 p += strlen((char *) p) + 1; 4623 } 4624 4625 p++; 4626 printf("\n"); 4627 printf(" The File Name Table:\n"); 4628 printf(" Entry\tDir\tTime\tSize\tName\n"); 4629 i = 0; 4630 while (*p != '\0') { 4631 i++; 4632 pn = (char *) p; 4633 p += strlen(pn) + 1; 4634 dirndx = _decode_uleb128(&p); 4635 mtime = _decode_uleb128(&p); 4636 fsize = _decode_uleb128(&p); 4637 printf(" %d\t%ju\t%ju\t%ju\t%s\n", i, 4638 (uintmax_t) dirndx, (uintmax_t) mtime, 4639 (uintmax_t) fsize, pn); 4640 } 4641 4642 #define RESET_REGISTERS \ 4643 do { \ 4644 address = 0; \ 4645 file = 1; \ 4646 line = 1; \ 4647 column = 0; \ 4648 is_stmt = defstmt; \ 4649 } while(0) 4650 4651 #define LINE(x) (lbase + (((x) - opbase) % lrange)) 4652 #define ADDRESS(x) ((((x) - opbase) / lrange) * minlen) 4653 4654 p++; 4655 pe = (uint8_t *) d->d_buf + endoff; 4656 printf("\n"); 4657 printf(" Line Number Statements:\n"); 4658 4659 RESET_REGISTERS; 4660 4661 while (p < pe) { 4662 4663 if (*p == 0) { 4664 /* 4665 * Extended Opcodes. 4666 */ 4667 p++; 4668 opsize = _decode_uleb128(&p); 4669 printf(" Extended opcode %u: ", *p); 4670 switch (*p) { 4671 case DW_LNE_end_sequence: 4672 p++; 4673 RESET_REGISTERS; 4674 printf("End of Sequence\n"); 4675 break; 4676 case DW_LNE_set_address: 4677 p++; 4678 address = re->dw_decode(&p, 4679 pointer_size); 4680 printf("set Address to %#jx\n", 4681 (uintmax_t) address); 4682 break; 4683 case DW_LNE_define_file: 4684 p++; 4685 pn = (char *) p; 4686 p += strlen(pn) + 1; 4687 dirndx = _decode_uleb128(&p); 4688 mtime = _decode_uleb128(&p); 4689 fsize = _decode_uleb128(&p); 4690 printf("define new file: %s\n", pn); 4691 break; 4692 default: 4693 /* Unrecognized extened opcodes. */ 4694 p += opsize; 4695 printf("unknown opcode\n"); 4696 } 4697 } else if (*p > 0 && *p < opbase) { 4698 /* 4699 * Standard Opcodes. 4700 */ 4701 switch(*p++) { 4702 case DW_LNS_copy: 4703 printf(" Copy\n"); 4704 break; 4705 case DW_LNS_advance_pc: 4706 udelta = _decode_uleb128(&p) * 4707 minlen; 4708 address += udelta; 4709 printf(" Advance PC by %ju to %#jx\n", 4710 (uintmax_t) udelta, 4711 (uintmax_t) address); 4712 break; 4713 case DW_LNS_advance_line: 4714 sdelta = _decode_sleb128(&p); 4715 line += sdelta; 4716 printf(" Advance Line by %jd to %ju\n", 4717 (intmax_t) sdelta, 4718 (uintmax_t) line); 4719 break; 4720 case DW_LNS_set_file: 4721 file = _decode_uleb128(&p); 4722 printf(" Set File to %ju\n", 4723 (uintmax_t) file); 4724 break; 4725 case DW_LNS_set_column: 4726 column = _decode_uleb128(&p); 4727 printf(" Set Column to %ju\n", 4728 (uintmax_t) column); 4729 break; 4730 case DW_LNS_negate_stmt: 4731 is_stmt = !is_stmt; 4732 printf(" Set is_stmt to %d\n", is_stmt); 4733 break; 4734 case DW_LNS_set_basic_block: 4735 printf(" Set basic block flag\n"); 4736 break; 4737 case DW_LNS_const_add_pc: 4738 address += ADDRESS(255); 4739 printf(" Advance PC by constant %ju" 4740 " to %#jx\n", 4741 (uintmax_t) ADDRESS(255), 4742 (uintmax_t) address); 4743 break; 4744 case DW_LNS_fixed_advance_pc: 4745 udelta = re->dw_decode(&p, 2); 4746 address += udelta; 4747 printf(" Advance PC by fixed value " 4748 "%ju to %#jx\n", 4749 (uintmax_t) udelta, 4750 (uintmax_t) address); 4751 break; 4752 case DW_LNS_set_prologue_end: 4753 printf(" Set prologue end flag\n"); 4754 break; 4755 case DW_LNS_set_epilogue_begin: 4756 printf(" Set epilogue begin flag\n"); 4757 break; 4758 case DW_LNS_set_isa: 4759 isa = _decode_uleb128(&p); 4760 printf(" Set isa to %ju\n", isa); 4761 break; 4762 default: 4763 /* Unrecognized extended opcodes. */ 4764 printf(" Unknown extended opcode %u\n", 4765 *(p - 1)); 4766 break; 4767 } 4768 4769 } else { 4770 /* 4771 * Special Opcodes. 4772 */ 4773 line += LINE(*p); 4774 address += ADDRESS(*p); 4775 printf(" Special opcode %u: advance Address " 4776 "by %ju to %#jx and Line by %jd to %ju\n", 4777 *p - opbase, (uintmax_t) ADDRESS(*p), 4778 (uintmax_t) address, (intmax_t) LINE(*p), 4779 (uintmax_t) line); 4780 p++; 4781 } 4782 4783 4784 } 4785 } 4786 if (ret == DW_DLV_ERROR) 4787 warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de)); 4788 4789 #undef RESET_REGISTERS 4790 #undef LINE 4791 #undef ADDRESS 4792 } 4793 4794 static void 4795 dump_dwarf_line_decoded(struct readelf *re) 4796 { 4797 Dwarf_Die die; 4798 Dwarf_Line *linebuf, ln; 4799 Dwarf_Addr lineaddr; 4800 Dwarf_Signed linecount, srccount; 4801 Dwarf_Unsigned lineno, fn; 4802 Dwarf_Error de; 4803 const char *dir, *file; 4804 char **srcfiles; 4805 int i, ret; 4806 4807 printf("Decoded dump of debug contents of section .debug_line:\n\n"); 4808 while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL, 4809 NULL, &de)) == DW_DLV_OK) { 4810 if (dwarf_siblingof(re->dbg, NULL, &die, &de) != DW_DLV_OK) 4811 continue; 4812 if (dwarf_attrval_string(die, DW_AT_name, &file, &de) != 4813 DW_DLV_OK) 4814 file = NULL; 4815 if (dwarf_attrval_string(die, DW_AT_comp_dir, &dir, &de) != 4816 DW_DLV_OK) 4817 dir = NULL; 4818 printf("CU: "); 4819 if (dir && file) 4820 printf("%s/", dir); 4821 if (file) 4822 printf("%s", file); 4823 putchar('\n'); 4824 printf("%-37s %11s %s\n", "Filename", "Line Number", 4825 "Starting Address"); 4826 if (dwarf_srclines(die, &linebuf, &linecount, &de) != DW_DLV_OK) 4827 continue; 4828 if (dwarf_srcfiles(die, &srcfiles, &srccount, &de) != DW_DLV_OK) 4829 continue; 4830 for (i = 0; i < linecount; i++) { 4831 ln = linebuf[i]; 4832 if (dwarf_line_srcfileno(ln, &fn, &de) != DW_DLV_OK) 4833 continue; 4834 if (dwarf_lineno(ln, &lineno, &de) != DW_DLV_OK) 4835 continue; 4836 if (dwarf_lineaddr(ln, &lineaddr, &de) != DW_DLV_OK) 4837 continue; 4838 printf("%-37s %11ju %#18jx\n", 4839 basename(srcfiles[fn - 1]), (uintmax_t) lineno, 4840 (uintmax_t) lineaddr); 4841 } 4842 putchar('\n'); 4843 } 4844 } 4845 4846 static void 4847 dump_dwarf_die(struct readelf *re, Dwarf_Die die, int level) 4848 { 4849 Dwarf_Attribute *attr_list; 4850 Dwarf_Die ret_die; 4851 Dwarf_Off dieoff, cuoff, culen, attroff; 4852 Dwarf_Unsigned ate, lang, v_udata, v_sig; 4853 Dwarf_Signed attr_count, v_sdata; 4854 Dwarf_Off v_off; 4855 Dwarf_Addr v_addr; 4856 Dwarf_Half tag, attr, form; 4857 Dwarf_Block *v_block; 4858 Dwarf_Bool v_bool, is_info; 4859 Dwarf_Sig8 v_sig8; 4860 Dwarf_Error de; 4861 Dwarf_Ptr v_expr; 4862 const char *tag_str, *attr_str, *ate_str, *lang_str; 4863 char unk_tag[32], unk_attr[32]; 4864 char *v_str; 4865 uint8_t *b, *p; 4866 int i, j, abc, ret; 4867 4868 if (dwarf_dieoffset(die, &dieoff, &de) != DW_DLV_OK) { 4869 warnx("dwarf_dieoffset failed: %s", dwarf_errmsg(de)); 4870 goto cont_search; 4871 } 4872 4873 printf(" <%d><%jx>: ", level, (uintmax_t) dieoff); 4874 4875 if (dwarf_die_CU_offset_range(die, &cuoff, &culen, &de) != DW_DLV_OK) { 4876 warnx("dwarf_die_CU_offset_range failed: %s", 4877 dwarf_errmsg(de)); 4878 cuoff = 0; 4879 } 4880 4881 abc = dwarf_die_abbrev_code(die); 4882 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { 4883 warnx("dwarf_tag failed: %s", dwarf_errmsg(de)); 4884 goto cont_search; 4885 } 4886 if (dwarf_get_TAG_name(tag, &tag_str) != DW_DLV_OK) { 4887 snprintf(unk_tag, sizeof(unk_tag), "[Unknown Tag: %#x]", tag); 4888 tag_str = unk_tag; 4889 } 4890 4891 printf("Abbrev Number: %d (%s)\n", abc, tag_str); 4892 4893 if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) != 4894 DW_DLV_OK) { 4895 if (ret == DW_DLV_ERROR) 4896 warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de)); 4897 goto cont_search; 4898 } 4899 4900 for (i = 0; i < attr_count; i++) { 4901 if (dwarf_whatform(attr_list[i], &form, &de) != DW_DLV_OK) { 4902 warnx("dwarf_whatform failed: %s", dwarf_errmsg(de)); 4903 continue; 4904 } 4905 if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) { 4906 warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de)); 4907 continue; 4908 } 4909 if (dwarf_get_AT_name(attr, &attr_str) != DW_DLV_OK) { 4910 snprintf(unk_attr, sizeof(unk_attr), 4911 "[Unknown AT: %#x]", attr); 4912 attr_str = unk_attr; 4913 } 4914 if (dwarf_attroffset(attr_list[i], &attroff, &de) != 4915 DW_DLV_OK) { 4916 warnx("dwarf_attroffset failed: %s", dwarf_errmsg(de)); 4917 attroff = 0; 4918 } 4919 printf(" <%jx> %-18s: ", (uintmax_t) attroff, attr_str); 4920 switch (form) { 4921 case DW_FORM_ref_addr: 4922 case DW_FORM_sec_offset: 4923 if (dwarf_global_formref(attr_list[i], &v_off, &de) != 4924 DW_DLV_OK) { 4925 warnx("dwarf_global_formref failed: %s", 4926 dwarf_errmsg(de)); 4927 continue; 4928 } 4929 if (form == DW_FORM_ref_addr) 4930 printf("<0x%jx>", (uintmax_t) v_off); 4931 else 4932 printf("0x%jx", (uintmax_t) v_off); 4933 break; 4934 4935 case DW_FORM_ref1: 4936 case DW_FORM_ref2: 4937 case DW_FORM_ref4: 4938 case DW_FORM_ref8: 4939 case DW_FORM_ref_udata: 4940 if (dwarf_formref(attr_list[i], &v_off, &de) != 4941 DW_DLV_OK) { 4942 warnx("dwarf_formref failed: %s", 4943 dwarf_errmsg(de)); 4944 continue; 4945 } 4946 v_off += cuoff; 4947 printf("<0x%jx>", (uintmax_t) v_off); 4948 break; 4949 4950 case DW_FORM_addr: 4951 if (dwarf_formaddr(attr_list[i], &v_addr, &de) != 4952 DW_DLV_OK) { 4953 warnx("dwarf_formaddr failed: %s", 4954 dwarf_errmsg(de)); 4955 continue; 4956 } 4957 printf("%#jx", (uintmax_t) v_addr); 4958 break; 4959 4960 case DW_FORM_data1: 4961 case DW_FORM_data2: 4962 case DW_FORM_data4: 4963 case DW_FORM_data8: 4964 case DW_FORM_udata: 4965 if (dwarf_formudata(attr_list[i], &v_udata, &de) != 4966 DW_DLV_OK) { 4967 warnx("dwarf_formudata failed: %s", 4968 dwarf_errmsg(de)); 4969 continue; 4970 } 4971 if (attr == DW_AT_high_pc) 4972 printf("0x%jx", (uintmax_t) v_udata); 4973 else 4974 printf("%ju", (uintmax_t) v_udata); 4975 break; 4976 4977 case DW_FORM_sdata: 4978 if (dwarf_formsdata(attr_list[i], &v_sdata, &de) != 4979 DW_DLV_OK) { 4980 warnx("dwarf_formudata failed: %s", 4981 dwarf_errmsg(de)); 4982 continue; 4983 } 4984 printf("%jd", (intmax_t) v_sdata); 4985 break; 4986 4987 case DW_FORM_flag: 4988 if (dwarf_formflag(attr_list[i], &v_bool, &de) != 4989 DW_DLV_OK) { 4990 warnx("dwarf_formflag failed: %s", 4991 dwarf_errmsg(de)); 4992 continue; 4993 } 4994 printf("%jd", (intmax_t) v_bool); 4995 break; 4996 4997 case DW_FORM_flag_present: 4998 putchar('1'); 4999 break; 5000 5001 case DW_FORM_string: 5002 case DW_FORM_strp: 5003 if (dwarf_formstring(attr_list[i], &v_str, &de) != 5004 DW_DLV_OK) { 5005 warnx("dwarf_formstring failed: %s", 5006 dwarf_errmsg(de)); 5007 continue; 5008 } 5009 if (form == DW_FORM_string) 5010 printf("%s", v_str); 5011 else 5012 printf("(indirect string) %s", v_str); 5013 break; 5014 5015 case DW_FORM_block: 5016 case DW_FORM_block1: 5017 case DW_FORM_block2: 5018 case DW_FORM_block4: 5019 if (dwarf_formblock(attr_list[i], &v_block, &de) != 5020 DW_DLV_OK) { 5021 warnx("dwarf_formblock failed: %s", 5022 dwarf_errmsg(de)); 5023 continue; 5024 } 5025 printf("%ju byte block:", (uintmax_t) v_block->bl_len); 5026 b = v_block->bl_data; 5027 for (j = 0; (Dwarf_Unsigned) j < v_block->bl_len; j++) 5028 printf(" %x", b[j]); 5029 printf("\t("); 5030 dump_dwarf_block(re, v_block->bl_data, v_block->bl_len); 5031 putchar(')'); 5032 break; 5033 5034 case DW_FORM_exprloc: 5035 if (dwarf_formexprloc(attr_list[i], &v_udata, &v_expr, 5036 &de) != DW_DLV_OK) { 5037 warnx("dwarf_formexprloc failed: %s", 5038 dwarf_errmsg(de)); 5039 continue; 5040 } 5041 printf("%ju byte block:", (uintmax_t) v_udata); 5042 b = v_expr; 5043 for (j = 0; (Dwarf_Unsigned) j < v_udata; j++) 5044 printf(" %x", b[j]); 5045 printf("\t("); 5046 dump_dwarf_block(re, v_expr, v_udata); 5047 putchar(')'); 5048 break; 5049 5050 case DW_FORM_ref_sig8: 5051 if (dwarf_formsig8(attr_list[i], &v_sig8, &de) != 5052 DW_DLV_OK) { 5053 warnx("dwarf_formsig8 failed: %s", 5054 dwarf_errmsg(de)); 5055 continue; 5056 } 5057 p = (uint8_t *)(uintptr_t) &v_sig8.signature[0]; 5058 v_sig = re->dw_decode(&p, 8); 5059 printf("signature: 0x%jx", (uintmax_t) v_sig); 5060 } 5061 switch (attr) { 5062 case DW_AT_encoding: 5063 if (dwarf_attrval_unsigned(die, attr, &ate, &de) != 5064 DW_DLV_OK) 5065 break; 5066 if (dwarf_get_ATE_name(ate, &ate_str) != DW_DLV_OK) 5067 ate_str = "DW_ATE_UNKNOWN"; 5068 printf("\t(%s)", &ate_str[strlen("DW_ATE_")]); 5069 break; 5070 5071 case DW_AT_language: 5072 if (dwarf_attrval_unsigned(die, attr, &lang, &de) != 5073 DW_DLV_OK) 5074 break; 5075 if (dwarf_get_LANG_name(lang, &lang_str) != DW_DLV_OK) 5076 break; 5077 printf("\t(%s)", &lang_str[strlen("DW_LANG_")]); 5078 break; 5079 5080 case DW_AT_location: 5081 case DW_AT_string_length: 5082 case DW_AT_return_addr: 5083 case DW_AT_data_member_location: 5084 case DW_AT_frame_base: 5085 case DW_AT_segment: 5086 case DW_AT_static_link: 5087 case DW_AT_use_location: 5088 case DW_AT_vtable_elem_location: 5089 switch (form) { 5090 case DW_FORM_data4: 5091 case DW_FORM_data8: 5092 case DW_FORM_sec_offset: 5093 printf("\t(location list)"); 5094 break; 5095 default: 5096 break; 5097 } 5098 5099 default: 5100 break; 5101 } 5102 putchar('\n'); 5103 } 5104 5105 5106 cont_search: 5107 /* Search children. */ 5108 ret = dwarf_child(die, &ret_die, &de); 5109 if (ret == DW_DLV_ERROR) 5110 warnx("dwarf_child: %s", dwarf_errmsg(de)); 5111 else if (ret == DW_DLV_OK) 5112 dump_dwarf_die(re, ret_die, level + 1); 5113 5114 /* Search sibling. */ 5115 is_info = dwarf_get_die_infotypes_flag(die); 5116 ret = dwarf_siblingof_b(re->dbg, die, &ret_die, is_info, &de); 5117 if (ret == DW_DLV_ERROR) 5118 warnx("dwarf_siblingof: %s", dwarf_errmsg(de)); 5119 else if (ret == DW_DLV_OK) 5120 dump_dwarf_die(re, ret_die, level); 5121 5122 dwarf_dealloc(re->dbg, die, DW_DLA_DIE); 5123 } 5124 5125 static void 5126 set_cu_context(struct readelf *re, Dwarf_Half psize, Dwarf_Half osize, 5127 Dwarf_Half ver) 5128 { 5129 5130 re->cu_psize = psize; 5131 re->cu_osize = osize; 5132 re->cu_ver = ver; 5133 } 5134 5135 static void 5136 dump_dwarf_info(struct readelf *re, Dwarf_Bool is_info) 5137 { 5138 struct section *s; 5139 Dwarf_Die die; 5140 Dwarf_Error de; 5141 Dwarf_Half tag, version, pointer_size, off_size; 5142 Dwarf_Off cu_offset, cu_length; 5143 Dwarf_Off aboff; 5144 Dwarf_Unsigned typeoff; 5145 Dwarf_Sig8 sig8; 5146 Dwarf_Unsigned sig; 5147 uint8_t *p; 5148 const char *sn; 5149 int i, ret; 5150 5151 sn = is_info ? ".debug_info" : ".debug_types"; 5152 5153 s = NULL; 5154 for (i = 0; (size_t) i < re->shnum; i++) { 5155 s = &re->sl[i]; 5156 if (s->name != NULL && !strcmp(s->name, sn)) 5157 break; 5158 } 5159 if ((size_t) i >= re->shnum) 5160 return; 5161 5162 do { 5163 printf("\nDump of debug contents of section %s:\n", sn); 5164 5165 while ((ret = dwarf_next_cu_header_c(re->dbg, is_info, NULL, 5166 &version, &aboff, &pointer_size, &off_size, NULL, &sig8, 5167 &typeoff, NULL, &de)) == DW_DLV_OK) { 5168 set_cu_context(re, pointer_size, off_size, version); 5169 die = NULL; 5170 while (dwarf_siblingof_b(re->dbg, die, &die, is_info, 5171 &de) == DW_DLV_OK) { 5172 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { 5173 warnx("dwarf_tag failed: %s", 5174 dwarf_errmsg(de)); 5175 continue; 5176 } 5177 /* XXX: What about DW_TAG_partial_unit? */ 5178 if ((is_info && tag == DW_TAG_compile_unit) || 5179 (!is_info && tag == DW_TAG_type_unit)) 5180 break; 5181 } 5182 if (die == NULL && is_info) { 5183 warnx("could not find DW_TAG_compile_unit " 5184 "die"); 5185 continue; 5186 } else if (die == NULL && !is_info) { 5187 warnx("could not find DW_TAG_type_unit die"); 5188 continue; 5189 } 5190 5191 if (dwarf_die_CU_offset_range(die, &cu_offset, 5192 &cu_length, &de) != DW_DLV_OK) { 5193 warnx("dwarf_die_CU_offset failed: %s", 5194 dwarf_errmsg(de)); 5195 continue; 5196 } 5197 5198 cu_length -= off_size == 4 ? 4 : 12; 5199 5200 sig = 0; 5201 if (!is_info) { 5202 p = (uint8_t *)(uintptr_t) &sig8.signature[0]; 5203 sig = re->dw_decode(&p, 8); 5204 } 5205 5206 printf("\n Type Unit @ offset 0x%jx:\n", 5207 (uintmax_t) cu_offset); 5208 printf(" Length:\t\t%#jx (%d-bit)\n", 5209 (uintmax_t) cu_length, off_size == 4 ? 32 : 64); 5210 printf(" Version:\t\t%u\n", version); 5211 printf(" Abbrev Offset:\t0x%jx\n", 5212 (uintmax_t) aboff); 5213 printf(" Pointer Size:\t%u\n", pointer_size); 5214 if (!is_info) { 5215 printf(" Signature:\t\t0x%016jx\n", 5216 (uintmax_t) sig); 5217 printf(" Type Offset:\t0x%jx\n", 5218 (uintmax_t) typeoff); 5219 } 5220 5221 dump_dwarf_die(re, die, 0); 5222 } 5223 if (ret == DW_DLV_ERROR) 5224 warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de)); 5225 if (is_info) 5226 break; 5227 } while (dwarf_next_types_section(re->dbg, &de) == DW_DLV_OK); 5228 } 5229 5230 static void 5231 dump_dwarf_abbrev(struct readelf *re) 5232 { 5233 Dwarf_Abbrev ab; 5234 Dwarf_Off aboff, atoff; 5235 Dwarf_Unsigned length, attr_count; 5236 Dwarf_Signed flag, form; 5237 Dwarf_Half tag, attr; 5238 Dwarf_Error de; 5239 const char *tag_str, *attr_str, *form_str; 5240 char unk_tag[32], unk_attr[32], unk_form[32]; 5241 int i, j, ret; 5242 5243 printf("\nContents of section .debug_abbrev:\n\n"); 5244 5245 while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, &aboff, 5246 NULL, NULL, &de)) == DW_DLV_OK) { 5247 printf(" Number TAG\n"); 5248 i = 0; 5249 while ((ret = dwarf_get_abbrev(re->dbg, aboff, &ab, &length, 5250 &attr_count, &de)) == DW_DLV_OK) { 5251 if (length == 1) { 5252 dwarf_dealloc(re->dbg, ab, DW_DLA_ABBREV); 5253 break; 5254 } 5255 aboff += length; 5256 printf("%4d", ++i); 5257 if (dwarf_get_abbrev_tag(ab, &tag, &de) != DW_DLV_OK) { 5258 warnx("dwarf_get_abbrev_tag failed: %s", 5259 dwarf_errmsg(de)); 5260 goto next_abbrev; 5261 } 5262 if (dwarf_get_TAG_name(tag, &tag_str) != DW_DLV_OK) { 5263 snprintf(unk_tag, sizeof(unk_tag), 5264 "[Unknown Tag: %#x]", tag); 5265 tag_str = unk_tag; 5266 } 5267 if (dwarf_get_abbrev_children_flag(ab, &flag, &de) != 5268 DW_DLV_OK) { 5269 warnx("dwarf_get_abbrev_children_flag failed:" 5270 " %s", dwarf_errmsg(de)); 5271 goto next_abbrev; 5272 } 5273 printf(" %s %s\n", tag_str, 5274 flag ? "[has children]" : "[no children]"); 5275 for (j = 0; (Dwarf_Unsigned) j < attr_count; j++) { 5276 if (dwarf_get_abbrev_entry(ab, (Dwarf_Signed) j, 5277 &attr, &form, &atoff, &de) != DW_DLV_OK) { 5278 warnx("dwarf_get_abbrev_entry failed:" 5279 " %s", dwarf_errmsg(de)); 5280 continue; 5281 } 5282 if (dwarf_get_AT_name(attr, &attr_str) != 5283 DW_DLV_OK) { 5284 snprintf(unk_attr, sizeof(unk_attr), 5285 "[Unknown AT: %#x]", attr); 5286 attr_str = unk_attr; 5287 } 5288 if (dwarf_get_FORM_name(form, &form_str) != 5289 DW_DLV_OK) { 5290 snprintf(unk_form, sizeof(unk_form), 5291 "[Unknown Form: %#x]", 5292 (Dwarf_Half) form); 5293 form_str = unk_form; 5294 } 5295 printf(" %-18s %s\n", attr_str, form_str); 5296 } 5297 next_abbrev: 5298 dwarf_dealloc(re->dbg, ab, DW_DLA_ABBREV); 5299 } 5300 if (ret != DW_DLV_OK) 5301 warnx("dwarf_get_abbrev: %s", dwarf_errmsg(de)); 5302 } 5303 if (ret == DW_DLV_ERROR) 5304 warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de)); 5305 } 5306 5307 static void 5308 dump_dwarf_pubnames(struct readelf *re) 5309 { 5310 struct section *s; 5311 Dwarf_Off die_off; 5312 Dwarf_Unsigned offset, length, nt_cu_offset, nt_cu_length; 5313 Dwarf_Signed cnt; 5314 Dwarf_Global *globs; 5315 Dwarf_Half nt_version; 5316 Dwarf_Error de; 5317 Elf_Data *d; 5318 char *glob_name; 5319 int i, dwarf_size, elferr; 5320 5321 printf("\nContents of the .debug_pubnames section:\n"); 5322 5323 s = NULL; 5324 for (i = 0; (size_t) i < re->shnum; i++) { 5325 s = &re->sl[i]; 5326 if (s->name != NULL && !strcmp(s->name, ".debug_pubnames")) 5327 break; 5328 } 5329 if ((size_t) i >= re->shnum) 5330 return; 5331 5332 (void) elf_errno(); 5333 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 5334 elferr = elf_errno(); 5335 if (elferr != 0) 5336 warnx("elf_getdata failed: %s", elf_errmsg(-1)); 5337 return; 5338 } 5339 if (d->d_size <= 0) 5340 return; 5341 5342 /* Read in .debug_pubnames section table header. */ 5343 offset = 0; 5344 length = re->dw_read(d, &offset, 4); 5345 if (length == 0xffffffff) { 5346 dwarf_size = 8; 5347 length = re->dw_read(d, &offset, 8); 5348 } else 5349 dwarf_size = 4; 5350 5351 if (length > d->d_size - offset) { 5352 warnx("invalid .dwarf_pubnames section"); 5353 return; 5354 } 5355 5356 nt_version = re->dw_read(d, &offset, 2); 5357 nt_cu_offset = re->dw_read(d, &offset, dwarf_size); 5358 nt_cu_length = re->dw_read(d, &offset, dwarf_size); 5359 printf(" Length:\t\t\t\t%ju\n", (uintmax_t) length); 5360 printf(" Version:\t\t\t\t%u\n", nt_version); 5361 printf(" Offset into .debug_info section:\t%ju\n", 5362 (uintmax_t) nt_cu_offset); 5363 printf(" Size of area in .debug_info section:\t%ju\n", 5364 (uintmax_t) nt_cu_length); 5365 5366 if (dwarf_get_globals(re->dbg, &globs, &cnt, &de) != DW_DLV_OK) { 5367 warnx("dwarf_get_globals failed: %s", dwarf_errmsg(de)); 5368 return; 5369 } 5370 5371 printf("\n Offset Name\n"); 5372 for (i = 0; i < cnt; i++) { 5373 if (dwarf_globname(globs[i], &glob_name, &de) != DW_DLV_OK) { 5374 warnx("dwarf_globname failed: %s", dwarf_errmsg(de)); 5375 continue; 5376 } 5377 if (dwarf_global_die_offset(globs[i], &die_off, &de) != 5378 DW_DLV_OK) { 5379 warnx("dwarf_global_die_offset failed: %s", 5380 dwarf_errmsg(de)); 5381 continue; 5382 } 5383 printf(" %-11ju %s\n", (uintmax_t) die_off, glob_name); 5384 } 5385 } 5386 5387 static void 5388 dump_dwarf_aranges(struct readelf *re) 5389 { 5390 struct section *s; 5391 Dwarf_Arange *aranges; 5392 Dwarf_Addr start; 5393 Dwarf_Unsigned offset, length, as_cu_offset; 5394 Dwarf_Off die_off; 5395 Dwarf_Signed cnt; 5396 Dwarf_Half as_version, as_addrsz, as_segsz; 5397 Dwarf_Error de; 5398 Elf_Data *d; 5399 int i, dwarf_size, elferr; 5400 5401 printf("\nContents of section .debug_aranges:\n"); 5402 5403 s = NULL; 5404 for (i = 0; (size_t) i < re->shnum; i++) { 5405 s = &re->sl[i]; 5406 if (s->name != NULL && !strcmp(s->name, ".debug_aranges")) 5407 break; 5408 } 5409 if ((size_t) i >= re->shnum) 5410 return; 5411 5412 (void) elf_errno(); 5413 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 5414 elferr = elf_errno(); 5415 if (elferr != 0) 5416 warnx("elf_getdata failed: %s", elf_errmsg(-1)); 5417 return; 5418 } 5419 if (d->d_size <= 0) 5420 return; 5421 5422 /* Read in the .debug_aranges section table header. */ 5423 offset = 0; 5424 length = re->dw_read(d, &offset, 4); 5425 if (length == 0xffffffff) { 5426 dwarf_size = 8; 5427 length = re->dw_read(d, &offset, 8); 5428 } else 5429 dwarf_size = 4; 5430 5431 if (length > d->d_size - offset) { 5432 warnx("invalid .dwarf_aranges section"); 5433 return; 5434 } 5435 5436 as_version = re->dw_read(d, &offset, 2); 5437 as_cu_offset = re->dw_read(d, &offset, dwarf_size); 5438 as_addrsz = re->dw_read(d, &offset, 1); 5439 as_segsz = re->dw_read(d, &offset, 1); 5440 5441 printf(" Length:\t\t\t%ju\n", (uintmax_t) length); 5442 printf(" Version:\t\t\t%u\n", as_version); 5443 printf(" Offset into .debug_info:\t%ju\n", (uintmax_t) as_cu_offset); 5444 printf(" Pointer Size:\t\t\t%u\n", as_addrsz); 5445 printf(" Segment Size:\t\t\t%u\n", as_segsz); 5446 5447 if (dwarf_get_aranges(re->dbg, &aranges, &cnt, &de) != DW_DLV_OK) { 5448 warnx("dwarf_get_aranges failed: %s", dwarf_errmsg(de)); 5449 return; 5450 } 5451 5452 printf("\n Address Length\n"); 5453 for (i = 0; i < cnt; i++) { 5454 if (dwarf_get_arange_info(aranges[i], &start, &length, 5455 &die_off, &de) != DW_DLV_OK) { 5456 warnx("dwarf_get_arange_info failed: %s", 5457 dwarf_errmsg(de)); 5458 continue; 5459 } 5460 printf(" %08jx %ju\n", (uintmax_t) start, 5461 (uintmax_t) length); 5462 } 5463 } 5464 5465 static void 5466 dump_dwarf_ranges_foreach(struct readelf *re, Dwarf_Die die, Dwarf_Addr base) 5467 { 5468 Dwarf_Attribute *attr_list; 5469 Dwarf_Ranges *ranges; 5470 Dwarf_Die ret_die; 5471 Dwarf_Error de; 5472 Dwarf_Addr base0; 5473 Dwarf_Half attr; 5474 Dwarf_Signed attr_count, cnt; 5475 Dwarf_Unsigned off, bytecnt; 5476 int i, j, ret; 5477 5478 if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) != 5479 DW_DLV_OK) { 5480 if (ret == DW_DLV_ERROR) 5481 warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de)); 5482 goto cont_search; 5483 } 5484 5485 for (i = 0; i < attr_count; i++) { 5486 if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) { 5487 warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de)); 5488 continue; 5489 } 5490 if (attr != DW_AT_ranges) 5491 continue; 5492 if (dwarf_formudata(attr_list[i], &off, &de) != DW_DLV_OK) { 5493 warnx("dwarf_formudata failed: %s", dwarf_errmsg(de)); 5494 continue; 5495 } 5496 if (dwarf_get_ranges(re->dbg, (Dwarf_Off) off, &ranges, &cnt, 5497 &bytecnt, &de) != DW_DLV_OK) 5498 continue; 5499 base0 = base; 5500 for (j = 0; j < cnt; j++) { 5501 printf(" %08jx ", (uintmax_t) off); 5502 if (ranges[j].dwr_type == DW_RANGES_END) { 5503 printf("%s\n", "<End of list>"); 5504 continue; 5505 } else if (ranges[j].dwr_type == 5506 DW_RANGES_ADDRESS_SELECTION) { 5507 base0 = ranges[j].dwr_addr2; 5508 continue; 5509 } 5510 if (re->ec == ELFCLASS32) 5511 printf("%08jx %08jx\n", 5512 ranges[j].dwr_addr1 + base0, 5513 ranges[j].dwr_addr2 + base0); 5514 else 5515 printf("%016jx %016jx\n", 5516 ranges[j].dwr_addr1 + base0, 5517 ranges[j].dwr_addr2 + base0); 5518 } 5519 } 5520 5521 cont_search: 5522 /* Search children. */ 5523 ret = dwarf_child(die, &ret_die, &de); 5524 if (ret == DW_DLV_ERROR) 5525 warnx("dwarf_child: %s", dwarf_errmsg(de)); 5526 else if (ret == DW_DLV_OK) 5527 dump_dwarf_ranges_foreach(re, ret_die, base); 5528 5529 /* Search sibling. */ 5530 ret = dwarf_siblingof(re->dbg, die, &ret_die, &de); 5531 if (ret == DW_DLV_ERROR) 5532 warnx("dwarf_siblingof: %s", dwarf_errmsg(de)); 5533 else if (ret == DW_DLV_OK) 5534 dump_dwarf_ranges_foreach(re, ret_die, base); 5535 } 5536 5537 static void 5538 dump_dwarf_ranges(struct readelf *re) 5539 { 5540 Dwarf_Ranges *ranges; 5541 Dwarf_Die die; 5542 Dwarf_Signed cnt; 5543 Dwarf_Unsigned bytecnt; 5544 Dwarf_Half tag; 5545 Dwarf_Error de; 5546 Dwarf_Unsigned lowpc; 5547 int ret; 5548 5549 if (dwarf_get_ranges(re->dbg, 0, &ranges, &cnt, &bytecnt, &de) != 5550 DW_DLV_OK) 5551 return; 5552 5553 printf("Contents of the .debug_ranges section:\n\n"); 5554 if (re->ec == ELFCLASS32) 5555 printf(" %-8s %-8s %s\n", "Offset", "Begin", "End"); 5556 else 5557 printf(" %-8s %-16s %s\n", "Offset", "Begin", "End"); 5558 5559 while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL, 5560 NULL, &de)) == DW_DLV_OK) { 5561 die = NULL; 5562 if (dwarf_siblingof(re->dbg, die, &die, &de) != DW_DLV_OK) 5563 continue; 5564 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { 5565 warnx("dwarf_tag failed: %s", dwarf_errmsg(de)); 5566 continue; 5567 } 5568 /* XXX: What about DW_TAG_partial_unit? */ 5569 lowpc = 0; 5570 if (tag == DW_TAG_compile_unit) { 5571 if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lowpc, 5572 &de) != DW_DLV_OK) 5573 lowpc = 0; 5574 } 5575 5576 dump_dwarf_ranges_foreach(re, die, (Dwarf_Addr) lowpc); 5577 } 5578 putchar('\n'); 5579 } 5580 5581 static void 5582 dump_dwarf_macinfo(struct readelf *re) 5583 { 5584 Dwarf_Unsigned offset; 5585 Dwarf_Signed cnt; 5586 Dwarf_Macro_Details *md; 5587 Dwarf_Error de; 5588 const char *mi_str; 5589 char unk_mi[32]; 5590 int i; 5591 5592 #define _MAX_MACINFO_ENTRY 65535 5593 5594 printf("\nContents of section .debug_macinfo:\n\n"); 5595 5596 offset = 0; 5597 while (dwarf_get_macro_details(re->dbg, offset, _MAX_MACINFO_ENTRY, 5598 &cnt, &md, &de) == DW_DLV_OK) { 5599 for (i = 0; i < cnt; i++) { 5600 offset = md[i].dmd_offset + 1; 5601 if (md[i].dmd_type == 0) 5602 break; 5603 if (dwarf_get_MACINFO_name(md[i].dmd_type, &mi_str) != 5604 DW_DLV_OK) { 5605 snprintf(unk_mi, sizeof(unk_mi), 5606 "[Unknown MACINFO: %#x]", md[i].dmd_type); 5607 mi_str = unk_mi; 5608 } 5609 printf(" %s", mi_str); 5610 switch (md[i].dmd_type) { 5611 case DW_MACINFO_define: 5612 case DW_MACINFO_undef: 5613 printf(" - lineno : %jd macro : %s\n", 5614 (intmax_t) md[i].dmd_lineno, 5615 md[i].dmd_macro); 5616 break; 5617 case DW_MACINFO_start_file: 5618 printf(" - lineno : %jd filenum : %jd\n", 5619 (intmax_t) md[i].dmd_lineno, 5620 (intmax_t) md[i].dmd_fileindex); 5621 break; 5622 default: 5623 putchar('\n'); 5624 break; 5625 } 5626 } 5627 } 5628 5629 #undef _MAX_MACINFO_ENTRY 5630 } 5631 5632 static void 5633 dump_dwarf_frame_inst(struct readelf *re, Dwarf_Cie cie, uint8_t *insts, 5634 Dwarf_Unsigned len, Dwarf_Unsigned caf, Dwarf_Signed daf, Dwarf_Addr pc, 5635 Dwarf_Debug dbg) 5636 { 5637 Dwarf_Frame_Op *oplist; 5638 Dwarf_Signed opcnt, delta; 5639 Dwarf_Small op; 5640 Dwarf_Error de; 5641 const char *op_str; 5642 char unk_op[32]; 5643 int i; 5644 5645 if (dwarf_expand_frame_instructions(cie, insts, len, &oplist, 5646 &opcnt, &de) != DW_DLV_OK) { 5647 warnx("dwarf_expand_frame_instructions failed: %s", 5648 dwarf_errmsg(de)); 5649 return; 5650 } 5651 5652 for (i = 0; i < opcnt; i++) { 5653 if (oplist[i].fp_base_op != 0) 5654 op = oplist[i].fp_base_op << 6; 5655 else 5656 op = oplist[i].fp_extended_op; 5657 if (dwarf_get_CFA_name(op, &op_str) != DW_DLV_OK) { 5658 snprintf(unk_op, sizeof(unk_op), "[Unknown CFA: %#x]", 5659 op); 5660 op_str = unk_op; 5661 } 5662 printf(" %s", op_str); 5663 switch (op) { 5664 case DW_CFA_advance_loc: 5665 delta = oplist[i].fp_offset * caf; 5666 pc += delta; 5667 printf(": %ju to %08jx", (uintmax_t) delta, 5668 (uintmax_t) pc); 5669 break; 5670 case DW_CFA_offset: 5671 case DW_CFA_offset_extended: 5672 case DW_CFA_offset_extended_sf: 5673 delta = oplist[i].fp_offset * daf; 5674 printf(": r%u (%s) at cfa%+jd", oplist[i].fp_register, 5675 dwarf_regname(re, oplist[i].fp_register), 5676 (intmax_t) delta); 5677 break; 5678 case DW_CFA_restore: 5679 printf(": r%u (%s)", oplist[i].fp_register, 5680 dwarf_regname(re, oplist[i].fp_register)); 5681 break; 5682 case DW_CFA_set_loc: 5683 pc = oplist[i].fp_offset; 5684 printf(": to %08jx", (uintmax_t) pc); 5685 break; 5686 case DW_CFA_advance_loc1: 5687 case DW_CFA_advance_loc2: 5688 case DW_CFA_advance_loc4: 5689 pc += oplist[i].fp_offset; 5690 printf(": %jd to %08jx", (intmax_t) oplist[i].fp_offset, 5691 (uintmax_t) pc); 5692 break; 5693 case DW_CFA_def_cfa: 5694 printf(": r%u (%s) ofs %ju", oplist[i].fp_register, 5695 dwarf_regname(re, oplist[i].fp_register), 5696 (uintmax_t) oplist[i].fp_offset); 5697 break; 5698 case DW_CFA_def_cfa_sf: 5699 printf(": r%u (%s) ofs %jd", oplist[i].fp_register, 5700 dwarf_regname(re, oplist[i].fp_register), 5701 (intmax_t) (oplist[i].fp_offset * daf)); 5702 break; 5703 case DW_CFA_def_cfa_register: 5704 printf(": r%u (%s)", oplist[i].fp_register, 5705 dwarf_regname(re, oplist[i].fp_register)); 5706 break; 5707 case DW_CFA_def_cfa_offset: 5708 printf(": %ju", (uintmax_t) oplist[i].fp_offset); 5709 break; 5710 case DW_CFA_def_cfa_offset_sf: 5711 printf(": %jd", (intmax_t) (oplist[i].fp_offset * daf)); 5712 break; 5713 default: 5714 break; 5715 } 5716 putchar('\n'); 5717 } 5718 5719 dwarf_dealloc(dbg, oplist, DW_DLA_FRAME_BLOCK); 5720 } 5721 5722 static char * 5723 get_regoff_str(struct readelf *re, Dwarf_Half reg, Dwarf_Addr off) 5724 { 5725 static char rs[16]; 5726 5727 if (reg == DW_FRAME_UNDEFINED_VAL || reg == DW_FRAME_REG_INITIAL_VALUE) 5728 snprintf(rs, sizeof(rs), "%c", 'u'); 5729 else if (reg == DW_FRAME_CFA_COL) 5730 snprintf(rs, sizeof(rs), "c%+jd", (intmax_t) off); 5731 else 5732 snprintf(rs, sizeof(rs), "%s%+jd", dwarf_regname(re, reg), 5733 (intmax_t) off); 5734 5735 return (rs); 5736 } 5737 5738 static int 5739 dump_dwarf_frame_regtable(struct readelf *re, Dwarf_Fde fde, Dwarf_Addr pc, 5740 Dwarf_Unsigned func_len, Dwarf_Half cie_ra) 5741 { 5742 Dwarf_Regtable rt; 5743 Dwarf_Addr row_pc, end_pc, pre_pc, cur_pc; 5744 Dwarf_Error de; 5745 char *vec; 5746 int i; 5747 5748 #define BIT_SET(v, n) (v[(n)>>3] |= 1U << ((n) & 7)) 5749 #define BIT_CLR(v, n) (v[(n)>>3] &= ~(1U << ((n) & 7))) 5750 #define BIT_ISSET(v, n) (v[(n)>>3] & (1U << ((n) & 7))) 5751 #define RT(x) rt.rules[(x)] 5752 5753 vec = calloc((DW_REG_TABLE_SIZE + 7) / 8, 1); 5754 if (vec == NULL) 5755 err(EXIT_FAILURE, "calloc failed"); 5756 5757 pre_pc = ~((Dwarf_Addr) 0); 5758 cur_pc = pc; 5759 end_pc = pc + func_len; 5760 for (; cur_pc < end_pc; cur_pc++) { 5761 if (dwarf_get_fde_info_for_all_regs(fde, cur_pc, &rt, &row_pc, 5762 &de) != DW_DLV_OK) { 5763 warnx("dwarf_get_fde_info_for_all_regs failed: %s\n", 5764 dwarf_errmsg(de)); 5765 return (-1); 5766 } 5767 if (row_pc == pre_pc) 5768 continue; 5769 pre_pc = row_pc; 5770 for (i = 1; i < DW_REG_TABLE_SIZE; i++) { 5771 if (rt.rules[i].dw_regnum != DW_FRAME_REG_INITIAL_VALUE) 5772 BIT_SET(vec, i); 5773 } 5774 } 5775 5776 printf(" LOC CFA "); 5777 for (i = 1; i < DW_REG_TABLE_SIZE; i++) { 5778 if (BIT_ISSET(vec, i)) { 5779 if ((Dwarf_Half) i == cie_ra) 5780 printf("ra "); 5781 else 5782 printf("%-5s", 5783 dwarf_regname(re, (unsigned int) i)); 5784 } 5785 } 5786 putchar('\n'); 5787 5788 pre_pc = ~((Dwarf_Addr) 0); 5789 cur_pc = pc; 5790 end_pc = pc + func_len; 5791 for (; cur_pc < end_pc; cur_pc++) { 5792 if (dwarf_get_fde_info_for_all_regs(fde, cur_pc, &rt, &row_pc, 5793 &de) != DW_DLV_OK) { 5794 warnx("dwarf_get_fde_info_for_all_regs failed: %s\n", 5795 dwarf_errmsg(de)); 5796 return (-1); 5797 } 5798 if (row_pc == pre_pc) 5799 continue; 5800 pre_pc = row_pc; 5801 printf("%08jx ", (uintmax_t) row_pc); 5802 printf("%-8s ", get_regoff_str(re, RT(0).dw_regnum, 5803 RT(0).dw_offset)); 5804 for (i = 1; i < DW_REG_TABLE_SIZE; i++) { 5805 if (BIT_ISSET(vec, i)) { 5806 printf("%-5s", get_regoff_str(re, 5807 RT(i).dw_regnum, RT(i).dw_offset)); 5808 } 5809 } 5810 putchar('\n'); 5811 } 5812 5813 free(vec); 5814 5815 return (0); 5816 5817 #undef BIT_SET 5818 #undef BIT_CLR 5819 #undef BIT_ISSET 5820 #undef RT 5821 } 5822 5823 static void 5824 dump_dwarf_frame_section(struct readelf *re, struct section *s, int alt) 5825 { 5826 Dwarf_Cie *cie_list, cie, pre_cie; 5827 Dwarf_Fde *fde_list, fde; 5828 Dwarf_Off cie_offset, fde_offset; 5829 Dwarf_Unsigned cie_length, fde_instlen; 5830 Dwarf_Unsigned cie_caf, cie_daf, cie_instlen, func_len, fde_length; 5831 Dwarf_Signed cie_count, fde_count, cie_index; 5832 Dwarf_Addr low_pc; 5833 Dwarf_Half cie_ra; 5834 Dwarf_Small cie_version; 5835 Dwarf_Ptr fde_addr, fde_inst, cie_inst; 5836 char *cie_aug, c; 5837 int i, eh_frame; 5838 Dwarf_Error de; 5839 5840 printf("\nThe section %s contains:\n\n", s->name); 5841 5842 if (!strcmp(s->name, ".debug_frame")) { 5843 eh_frame = 0; 5844 if (dwarf_get_fde_list(re->dbg, &cie_list, &cie_count, 5845 &fde_list, &fde_count, &de) != DW_DLV_OK) { 5846 warnx("dwarf_get_fde_list failed: %s", 5847 dwarf_errmsg(de)); 5848 return; 5849 } 5850 } else if (!strcmp(s->name, ".eh_frame")) { 5851 eh_frame = 1; 5852 if (dwarf_get_fde_list_eh(re->dbg, &cie_list, &cie_count, 5853 &fde_list, &fde_count, &de) != DW_DLV_OK) { 5854 warnx("dwarf_get_fde_list_eh failed: %s", 5855 dwarf_errmsg(de)); 5856 return; 5857 } 5858 } else 5859 return; 5860 5861 pre_cie = NULL; 5862 for (i = 0; i < fde_count; i++) { 5863 if (dwarf_get_fde_n(fde_list, i, &fde, &de) != DW_DLV_OK) { 5864 warnx("dwarf_get_fde_n failed: %s", dwarf_errmsg(de)); 5865 continue; 5866 } 5867 if (dwarf_get_cie_of_fde(fde, &cie, &de) != DW_DLV_OK) { 5868 warnx("dwarf_get_fde_n failed: %s", dwarf_errmsg(de)); 5869 continue; 5870 } 5871 if (dwarf_get_fde_range(fde, &low_pc, &func_len, &fde_addr, 5872 &fde_length, &cie_offset, &cie_index, &fde_offset, 5873 &de) != DW_DLV_OK) { 5874 warnx("dwarf_get_fde_range failed: %s", 5875 dwarf_errmsg(de)); 5876 continue; 5877 } 5878 if (dwarf_get_fde_instr_bytes(fde, &fde_inst, &fde_instlen, 5879 &de) != DW_DLV_OK) { 5880 warnx("dwarf_get_fde_instr_bytes failed: %s", 5881 dwarf_errmsg(de)); 5882 continue; 5883 } 5884 if (pre_cie == NULL || cie != pre_cie) { 5885 pre_cie = cie; 5886 if (dwarf_get_cie_info(cie, &cie_length, &cie_version, 5887 &cie_aug, &cie_caf, &cie_daf, &cie_ra, 5888 &cie_inst, &cie_instlen, &de) != DW_DLV_OK) { 5889 warnx("dwarf_get_cie_info failed: %s", 5890 dwarf_errmsg(de)); 5891 continue; 5892 } 5893 printf("%08jx %08jx %8.8jx CIE", 5894 (uintmax_t) cie_offset, 5895 (uintmax_t) cie_length, 5896 (uintmax_t) (eh_frame ? 0 : ~0U)); 5897 if (!alt) { 5898 putchar('\n'); 5899 printf(" Version:\t\t\t%u\n", cie_version); 5900 printf(" Augmentation:\t\t\t\""); 5901 while ((c = *cie_aug++) != '\0') 5902 putchar(c); 5903 printf("\"\n"); 5904 printf(" Code alignment factor:\t%ju\n", 5905 (uintmax_t) cie_caf); 5906 printf(" Data alignment factor:\t%jd\n", 5907 (intmax_t) cie_daf); 5908 printf(" Return address column:\t%ju\n", 5909 (uintmax_t) cie_ra); 5910 putchar('\n'); 5911 dump_dwarf_frame_inst(re, cie, cie_inst, 5912 cie_instlen, cie_caf, cie_daf, 0, 5913 re->dbg); 5914 putchar('\n'); 5915 } else { 5916 printf(" \""); 5917 while ((c = *cie_aug++) != '\0') 5918 putchar(c); 5919 putchar('"'); 5920 printf(" cf=%ju df=%jd ra=%ju\n", 5921 (uintmax_t) cie_caf, 5922 (uintmax_t) cie_daf, 5923 (uintmax_t) cie_ra); 5924 dump_dwarf_frame_regtable(re, fde, low_pc, 1, 5925 cie_ra); 5926 putchar('\n'); 5927 } 5928 } 5929 printf("%08jx %08jx %08jx FDE cie=%08jx pc=%08jx..%08jx\n", 5930 (uintmax_t) fde_offset, (uintmax_t) fde_length, 5931 (uintmax_t) cie_offset, 5932 (uintmax_t) (eh_frame ? fde_offset + 4 - cie_offset : 5933 cie_offset), 5934 (uintmax_t) low_pc, (uintmax_t) (low_pc + func_len)); 5935 if (!alt) 5936 dump_dwarf_frame_inst(re, cie, fde_inst, fde_instlen, 5937 cie_caf, cie_daf, low_pc, re->dbg); 5938 else 5939 dump_dwarf_frame_regtable(re, fde, low_pc, func_len, 5940 cie_ra); 5941 putchar('\n'); 5942 } 5943 } 5944 5945 static void 5946 dump_dwarf_frame(struct readelf *re, int alt) 5947 { 5948 struct section *s; 5949 int i; 5950 5951 (void) dwarf_set_frame_cfa_value(re->dbg, DW_FRAME_CFA_COL); 5952 5953 for (i = 0; (size_t) i < re->shnum; i++) { 5954 s = &re->sl[i]; 5955 if (s->name != NULL && (!strcmp(s->name, ".debug_frame") || 5956 !strcmp(s->name, ".eh_frame"))) 5957 dump_dwarf_frame_section(re, s, alt); 5958 } 5959 } 5960 5961 static void 5962 dump_dwarf_str(struct readelf *re) 5963 { 5964 struct section *s; 5965 Elf_Data *d; 5966 unsigned char *p; 5967 int elferr, end, i, j; 5968 5969 printf("\nContents of section .debug_str:\n"); 5970 5971 s = NULL; 5972 for (i = 0; (size_t) i < re->shnum; i++) { 5973 s = &re->sl[i]; 5974 if (s->name != NULL && !strcmp(s->name, ".debug_str")) 5975 break; 5976 } 5977 if ((size_t) i >= re->shnum) 5978 return; 5979 5980 (void) elf_errno(); 5981 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 5982 elferr = elf_errno(); 5983 if (elferr != 0) 5984 warnx("elf_getdata failed: %s", elf_errmsg(-1)); 5985 return; 5986 } 5987 if (d->d_size <= 0) 5988 return; 5989 5990 for (i = 0, p = d->d_buf; (size_t) i < d->d_size; i += 16) { 5991 printf(" 0x%08x", (unsigned int) i); 5992 if ((size_t) i + 16 > d->d_size) 5993 end = d->d_size; 5994 else 5995 end = i + 16; 5996 for (j = i; j < i + 16; j++) { 5997 if ((j - i) % 4 == 0) 5998 putchar(' '); 5999 if (j >= end) { 6000 printf(" "); 6001 continue; 6002 } 6003 printf("%02x", (uint8_t) p[j]); 6004 } 6005 putchar(' '); 6006 for (j = i; j < end; j++) { 6007 if (isprint(p[j])) 6008 putchar(p[j]); 6009 else if (p[j] == 0) 6010 putchar('.'); 6011 else 6012 putchar(' '); 6013 } 6014 putchar('\n'); 6015 } 6016 } 6017 6018 struct loc_at { 6019 Dwarf_Attribute la_at; 6020 Dwarf_Unsigned la_off; 6021 Dwarf_Unsigned la_lowpc; 6022 Dwarf_Half la_cu_psize; 6023 Dwarf_Half la_cu_osize; 6024 Dwarf_Half la_cu_ver; 6025 TAILQ_ENTRY(loc_at) la_next; 6026 }; 6027 6028 static TAILQ_HEAD(, loc_at) lalist = TAILQ_HEAD_INITIALIZER(lalist); 6029 6030 static void 6031 search_loclist_at(struct readelf *re, Dwarf_Die die, Dwarf_Unsigned lowpc) 6032 { 6033 Dwarf_Attribute *attr_list; 6034 Dwarf_Die ret_die; 6035 Dwarf_Unsigned off; 6036 Dwarf_Off ref; 6037 Dwarf_Signed attr_count; 6038 Dwarf_Half attr, form; 6039 Dwarf_Bool is_info; 6040 Dwarf_Error de; 6041 struct loc_at *la, *nla; 6042 int i, ret; 6043 6044 is_info = dwarf_get_die_infotypes_flag(die); 6045 6046 if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) != 6047 DW_DLV_OK) { 6048 if (ret == DW_DLV_ERROR) 6049 warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de)); 6050 goto cont_search; 6051 } 6052 for (i = 0; i < attr_count; i++) { 6053 if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) { 6054 warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de)); 6055 continue; 6056 } 6057 if (attr != DW_AT_location && 6058 attr != DW_AT_string_length && 6059 attr != DW_AT_return_addr && 6060 attr != DW_AT_data_member_location && 6061 attr != DW_AT_frame_base && 6062 attr != DW_AT_segment && 6063 attr != DW_AT_static_link && 6064 attr != DW_AT_use_location && 6065 attr != DW_AT_vtable_elem_location) 6066 continue; 6067 if (dwarf_whatform(attr_list[i], &form, &de) != DW_DLV_OK) { 6068 warnx("dwarf_whatform failed: %s", dwarf_errmsg(de)); 6069 continue; 6070 } 6071 if (form == DW_FORM_data4 || form == DW_FORM_data8) { 6072 if (dwarf_formudata(attr_list[i], &off, &de) != 6073 DW_DLV_OK) { 6074 warnx("dwarf_formudata failed: %s", 6075 dwarf_errmsg(de)); 6076 continue; 6077 } 6078 } else if (form == DW_FORM_sec_offset) { 6079 if (dwarf_global_formref(attr_list[i], &ref, &de) != 6080 DW_DLV_OK) { 6081 warnx("dwarf_global_formref failed: %s", 6082 dwarf_errmsg(de)); 6083 continue; 6084 } 6085 off = ref; 6086 } else 6087 continue; 6088 6089 TAILQ_FOREACH(la, &lalist, la_next) { 6090 if (off == la->la_off) 6091 break; 6092 if (off < la->la_off) { 6093 if ((nla = malloc(sizeof(*nla))) == NULL) 6094 err(EXIT_FAILURE, "malloc failed"); 6095 nla->la_at = attr_list[i]; 6096 nla->la_off = off; 6097 nla->la_lowpc = lowpc; 6098 nla->la_cu_psize = re->cu_psize; 6099 nla->la_cu_osize = re->cu_osize; 6100 nla->la_cu_ver = re->cu_ver; 6101 TAILQ_INSERT_BEFORE(la, nla, la_next); 6102 break; 6103 } 6104 } 6105 if (la == NULL) { 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_TAIL(&lalist, nla, la_next); 6115 } 6116 } 6117 6118 cont_search: 6119 /* Search children. */ 6120 ret = dwarf_child(die, &ret_die, &de); 6121 if (ret == DW_DLV_ERROR) 6122 warnx("dwarf_child: %s", dwarf_errmsg(de)); 6123 else if (ret == DW_DLV_OK) 6124 search_loclist_at(re, ret_die, lowpc); 6125 6126 /* Search sibling. */ 6127 ret = dwarf_siblingof_b(re->dbg, die, &ret_die, is_info, &de); 6128 if (ret == DW_DLV_ERROR) 6129 warnx("dwarf_siblingof: %s", dwarf_errmsg(de)); 6130 else if (ret == DW_DLV_OK) 6131 search_loclist_at(re, ret_die, lowpc); 6132 } 6133 6134 static void 6135 dump_dwarf_loc(struct readelf *re, Dwarf_Loc *lr) 6136 { 6137 const char *op_str; 6138 char unk_op[32]; 6139 uint8_t *b, n; 6140 int i; 6141 6142 if (dwarf_get_OP_name(lr->lr_atom, &op_str) != 6143 DW_DLV_OK) { 6144 snprintf(unk_op, sizeof(unk_op), 6145 "[Unknown OP: %#x]", lr->lr_atom); 6146 op_str = unk_op; 6147 } 6148 6149 printf("%s", op_str); 6150 6151 switch (lr->lr_atom) { 6152 case DW_OP_reg0: 6153 case DW_OP_reg1: 6154 case DW_OP_reg2: 6155 case DW_OP_reg3: 6156 case DW_OP_reg4: 6157 case DW_OP_reg5: 6158 case DW_OP_reg6: 6159 case DW_OP_reg7: 6160 case DW_OP_reg8: 6161 case DW_OP_reg9: 6162 case DW_OP_reg10: 6163 case DW_OP_reg11: 6164 case DW_OP_reg12: 6165 case DW_OP_reg13: 6166 case DW_OP_reg14: 6167 case DW_OP_reg15: 6168 case DW_OP_reg16: 6169 case DW_OP_reg17: 6170 case DW_OP_reg18: 6171 case DW_OP_reg19: 6172 case DW_OP_reg20: 6173 case DW_OP_reg21: 6174 case DW_OP_reg22: 6175 case DW_OP_reg23: 6176 case DW_OP_reg24: 6177 case DW_OP_reg25: 6178 case DW_OP_reg26: 6179 case DW_OP_reg27: 6180 case DW_OP_reg28: 6181 case DW_OP_reg29: 6182 case DW_OP_reg30: 6183 case DW_OP_reg31: 6184 printf(" (%s)", dwarf_regname(re, lr->lr_atom - DW_OP_reg0)); 6185 break; 6186 6187 case DW_OP_deref: 6188 case DW_OP_lit0: 6189 case DW_OP_lit1: 6190 case DW_OP_lit2: 6191 case DW_OP_lit3: 6192 case DW_OP_lit4: 6193 case DW_OP_lit5: 6194 case DW_OP_lit6: 6195 case DW_OP_lit7: 6196 case DW_OP_lit8: 6197 case DW_OP_lit9: 6198 case DW_OP_lit10: 6199 case DW_OP_lit11: 6200 case DW_OP_lit12: 6201 case DW_OP_lit13: 6202 case DW_OP_lit14: 6203 case DW_OP_lit15: 6204 case DW_OP_lit16: 6205 case DW_OP_lit17: 6206 case DW_OP_lit18: 6207 case DW_OP_lit19: 6208 case DW_OP_lit20: 6209 case DW_OP_lit21: 6210 case DW_OP_lit22: 6211 case DW_OP_lit23: 6212 case DW_OP_lit24: 6213 case DW_OP_lit25: 6214 case DW_OP_lit26: 6215 case DW_OP_lit27: 6216 case DW_OP_lit28: 6217 case DW_OP_lit29: 6218 case DW_OP_lit30: 6219 case DW_OP_lit31: 6220 case DW_OP_dup: 6221 case DW_OP_drop: 6222 case DW_OP_over: 6223 case DW_OP_swap: 6224 case DW_OP_rot: 6225 case DW_OP_xderef: 6226 case DW_OP_abs: 6227 case DW_OP_and: 6228 case DW_OP_div: 6229 case DW_OP_minus: 6230 case DW_OP_mod: 6231 case DW_OP_mul: 6232 case DW_OP_neg: 6233 case DW_OP_not: 6234 case DW_OP_or: 6235 case DW_OP_plus: 6236 case DW_OP_shl: 6237 case DW_OP_shr: 6238 case DW_OP_shra: 6239 case DW_OP_xor: 6240 case DW_OP_eq: 6241 case DW_OP_ge: 6242 case DW_OP_gt: 6243 case DW_OP_le: 6244 case DW_OP_lt: 6245 case DW_OP_ne: 6246 case DW_OP_nop: 6247 case DW_OP_push_object_address: 6248 case DW_OP_form_tls_address: 6249 case DW_OP_call_frame_cfa: 6250 case DW_OP_stack_value: 6251 case DW_OP_GNU_push_tls_address: 6252 case DW_OP_GNU_uninit: 6253 break; 6254 6255 case DW_OP_const1u: 6256 case DW_OP_pick: 6257 case DW_OP_deref_size: 6258 case DW_OP_xderef_size: 6259 case DW_OP_const2u: 6260 case DW_OP_bra: 6261 case DW_OP_skip: 6262 case DW_OP_const4u: 6263 case DW_OP_const8u: 6264 case DW_OP_constu: 6265 case DW_OP_plus_uconst: 6266 case DW_OP_regx: 6267 case DW_OP_piece: 6268 printf(": %ju", (uintmax_t) 6269 lr->lr_number); 6270 break; 6271 6272 case DW_OP_const1s: 6273 case DW_OP_const2s: 6274 case DW_OP_const4s: 6275 case DW_OP_const8s: 6276 case DW_OP_consts: 6277 printf(": %jd", (intmax_t) 6278 lr->lr_number); 6279 break; 6280 6281 case DW_OP_breg0: 6282 case DW_OP_breg1: 6283 case DW_OP_breg2: 6284 case DW_OP_breg3: 6285 case DW_OP_breg4: 6286 case DW_OP_breg5: 6287 case DW_OP_breg6: 6288 case DW_OP_breg7: 6289 case DW_OP_breg8: 6290 case DW_OP_breg9: 6291 case DW_OP_breg10: 6292 case DW_OP_breg11: 6293 case DW_OP_breg12: 6294 case DW_OP_breg13: 6295 case DW_OP_breg14: 6296 case DW_OP_breg15: 6297 case DW_OP_breg16: 6298 case DW_OP_breg17: 6299 case DW_OP_breg18: 6300 case DW_OP_breg19: 6301 case DW_OP_breg20: 6302 case DW_OP_breg21: 6303 case DW_OP_breg22: 6304 case DW_OP_breg23: 6305 case DW_OP_breg24: 6306 case DW_OP_breg25: 6307 case DW_OP_breg26: 6308 case DW_OP_breg27: 6309 case DW_OP_breg28: 6310 case DW_OP_breg29: 6311 case DW_OP_breg30: 6312 case DW_OP_breg31: 6313 printf(" (%s): %jd", 6314 dwarf_regname(re, lr->lr_atom - DW_OP_breg0), 6315 (intmax_t) lr->lr_number); 6316 break; 6317 6318 case DW_OP_fbreg: 6319 printf(": %jd", (intmax_t) 6320 lr->lr_number); 6321 break; 6322 6323 case DW_OP_bregx: 6324 printf(": %ju (%s) %jd", 6325 (uintmax_t) lr->lr_number, 6326 dwarf_regname(re, (unsigned int) lr->lr_number), 6327 (intmax_t) lr->lr_number2); 6328 break; 6329 6330 case DW_OP_addr: 6331 case DW_OP_GNU_encoded_addr: 6332 printf(": %#jx", (uintmax_t) 6333 lr->lr_number); 6334 break; 6335 6336 case DW_OP_GNU_implicit_pointer: 6337 printf(": <0x%jx> %jd", (uintmax_t) lr->lr_number, 6338 (intmax_t) lr->lr_number2); 6339 break; 6340 6341 case DW_OP_implicit_value: 6342 printf(": %ju byte block:", (uintmax_t) lr->lr_number); 6343 b = (uint8_t *)(uintptr_t) lr->lr_number2; 6344 for (i = 0; (Dwarf_Unsigned) i < lr->lr_number; i++) 6345 printf(" %x", b[i]); 6346 break; 6347 6348 case DW_OP_GNU_entry_value: 6349 printf(": ("); 6350 dump_dwarf_block(re, (uint8_t *)(uintptr_t) lr->lr_number2, 6351 lr->lr_number); 6352 putchar(')'); 6353 break; 6354 6355 case DW_OP_GNU_const_type: 6356 printf(": <0x%jx> ", (uintmax_t) lr->lr_number); 6357 b = (uint8_t *)(uintptr_t) lr->lr_number2; 6358 n = *b; 6359 for (i = 1; (uint8_t) i < n; i++) 6360 printf(" %x", b[i]); 6361 break; 6362 6363 case DW_OP_GNU_regval_type: 6364 printf(": %ju (%s) <0x%jx>", (uintmax_t) lr->lr_number, 6365 dwarf_regname(re, (unsigned int) lr->lr_number), 6366 (uintmax_t) lr->lr_number2); 6367 break; 6368 6369 case DW_OP_GNU_convert: 6370 case DW_OP_GNU_deref_type: 6371 case DW_OP_GNU_parameter_ref: 6372 case DW_OP_GNU_reinterpret: 6373 printf(": <0x%jx>", (uintmax_t) lr->lr_number); 6374 break; 6375 6376 default: 6377 break; 6378 } 6379 } 6380 6381 static void 6382 dump_dwarf_block(struct readelf *re, uint8_t *b, Dwarf_Unsigned len) 6383 { 6384 Dwarf_Locdesc *llbuf; 6385 Dwarf_Signed lcnt; 6386 Dwarf_Error de; 6387 int i; 6388 6389 if (dwarf_loclist_from_expr_b(re->dbg, b, len, re->cu_psize, 6390 re->cu_osize, re->cu_ver, &llbuf, &lcnt, &de) != DW_DLV_OK) { 6391 warnx("dwarf_loclist_form_expr_b: %s", dwarf_errmsg(de)); 6392 return; 6393 } 6394 6395 for (i = 0; (Dwarf_Half) i < llbuf->ld_cents; i++) { 6396 dump_dwarf_loc(re, &llbuf->ld_s[i]); 6397 if (i < llbuf->ld_cents - 1) 6398 printf("; "); 6399 } 6400 6401 dwarf_dealloc(re->dbg, llbuf->ld_s, DW_DLA_LOC_BLOCK); 6402 dwarf_dealloc(re->dbg, llbuf, DW_DLA_LOCDESC); 6403 } 6404 6405 static void 6406 dump_dwarf_loclist(struct readelf *re) 6407 { 6408 Dwarf_Die die; 6409 Dwarf_Locdesc **llbuf; 6410 Dwarf_Unsigned lowpc; 6411 Dwarf_Signed lcnt; 6412 Dwarf_Half tag, version, pointer_size, off_size; 6413 Dwarf_Error de; 6414 struct loc_at *la; 6415 int i, j, ret; 6416 6417 printf("\nContents of section .debug_loc:\n"); 6418 6419 /* Search .debug_info section. */ 6420 while ((ret = dwarf_next_cu_header_b(re->dbg, NULL, &version, NULL, 6421 &pointer_size, &off_size, NULL, NULL, &de)) == DW_DLV_OK) { 6422 set_cu_context(re, pointer_size, off_size, version); 6423 die = NULL; 6424 if (dwarf_siblingof(re->dbg, die, &die, &de) != DW_DLV_OK) 6425 continue; 6426 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { 6427 warnx("dwarf_tag failed: %s", dwarf_errmsg(de)); 6428 continue; 6429 } 6430 /* XXX: What about DW_TAG_partial_unit? */ 6431 lowpc = 0; 6432 if (tag == DW_TAG_compile_unit) { 6433 if (dwarf_attrval_unsigned(die, DW_AT_low_pc, 6434 &lowpc, &de) != DW_DLV_OK) 6435 lowpc = 0; 6436 } 6437 6438 /* Search attributes for reference to .debug_loc section. */ 6439 search_loclist_at(re, die, lowpc); 6440 } 6441 if (ret == DW_DLV_ERROR) 6442 warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de)); 6443 6444 /* Search .debug_types section. */ 6445 do { 6446 while ((ret = dwarf_next_cu_header_c(re->dbg, 0, NULL, 6447 &version, NULL, &pointer_size, &off_size, NULL, NULL, 6448 NULL, NULL, &de)) == DW_DLV_OK) { 6449 set_cu_context(re, pointer_size, off_size, version); 6450 die = NULL; 6451 if (dwarf_siblingof(re->dbg, die, &die, &de) != 6452 DW_DLV_OK) 6453 continue; 6454 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { 6455 warnx("dwarf_tag failed: %s", 6456 dwarf_errmsg(de)); 6457 continue; 6458 } 6459 6460 lowpc = 0; 6461 if (tag == DW_TAG_type_unit) { 6462 if (dwarf_attrval_unsigned(die, DW_AT_low_pc, 6463 &lowpc, &de) != DW_DLV_OK) 6464 lowpc = 0; 6465 } 6466 6467 /* 6468 * Search attributes for reference to .debug_loc 6469 * section. 6470 */ 6471 search_loclist_at(re, die, lowpc); 6472 } 6473 if (ret == DW_DLV_ERROR) 6474 warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de)); 6475 } while (dwarf_next_types_section(re->dbg, &de) == DW_DLV_OK); 6476 6477 if (TAILQ_EMPTY(&lalist)) 6478 return; 6479 6480 printf(" Offset Begin End Expression\n"); 6481 6482 TAILQ_FOREACH(la, &lalist, la_next) { 6483 if (dwarf_loclist_n(la->la_at, &llbuf, &lcnt, &de) != 6484 DW_DLV_OK) { 6485 warnx("dwarf_loclist_n failed: %s", dwarf_errmsg(de)); 6486 continue; 6487 } 6488 set_cu_context(re, la->la_cu_psize, la->la_cu_osize, 6489 la->la_cu_ver); 6490 for (i = 0; i < lcnt; i++) { 6491 printf(" %8.8jx ", la->la_off); 6492 if (llbuf[i]->ld_lopc == 0 && llbuf[i]->ld_hipc == 0) { 6493 printf("<End of list>\n"); 6494 continue; 6495 } 6496 6497 /* TODO: handle base selection entry. */ 6498 6499 printf("%8.8jx %8.8jx ", 6500 (uintmax_t) (la->la_lowpc + llbuf[i]->ld_lopc), 6501 (uintmax_t) (la->la_lowpc + llbuf[i]->ld_hipc)); 6502 6503 putchar('('); 6504 for (j = 0; (Dwarf_Half) j < llbuf[i]->ld_cents; j++) { 6505 dump_dwarf_loc(re, &llbuf[i]->ld_s[j]); 6506 if (j < llbuf[i]->ld_cents - 1) 6507 printf("; "); 6508 } 6509 putchar(')'); 6510 6511 if (llbuf[i]->ld_lopc == llbuf[i]->ld_hipc) 6512 printf(" (start == end)"); 6513 putchar('\n'); 6514 } 6515 for (i = 0; i < lcnt; i++) { 6516 dwarf_dealloc(re->dbg, llbuf[i]->ld_s, 6517 DW_DLA_LOC_BLOCK); 6518 dwarf_dealloc(re->dbg, llbuf[i], DW_DLA_LOCDESC); 6519 } 6520 dwarf_dealloc(re->dbg, llbuf, DW_DLA_LIST); 6521 } 6522 } 6523 6524 /* 6525 * Retrieve a string using string table section index and the string offset. 6526 */ 6527 static const char* 6528 get_string(struct readelf *re, int strtab, size_t off) 6529 { 6530 const char *name; 6531 6532 if ((name = elf_strptr(re->elf, strtab, off)) == NULL) 6533 return (""); 6534 6535 return (name); 6536 } 6537 6538 /* 6539 * Retrieve the name of a symbol using the section index of the symbol 6540 * table and the index of the symbol within that table. 6541 */ 6542 static const char * 6543 get_symbol_name(struct readelf *re, int symtab, int i) 6544 { 6545 struct section *s; 6546 const char *name; 6547 GElf_Sym sym; 6548 Elf_Data *data; 6549 int elferr; 6550 6551 s = &re->sl[symtab]; 6552 if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM) 6553 return (""); 6554 (void) elf_errno(); 6555 if ((data = elf_getdata(s->scn, NULL)) == NULL) { 6556 elferr = elf_errno(); 6557 if (elferr != 0) 6558 warnx("elf_getdata failed: %s", elf_errmsg(elferr)); 6559 return (""); 6560 } 6561 if (gelf_getsym(data, i, &sym) != &sym) 6562 return (""); 6563 /* Return section name for STT_SECTION symbol. */ 6564 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION && 6565 re->sl[sym.st_shndx].name != NULL) 6566 return (re->sl[sym.st_shndx].name); 6567 if ((name = elf_strptr(re->elf, s->link, sym.st_name)) == NULL) 6568 return (""); 6569 6570 return (name); 6571 } 6572 6573 static uint64_t 6574 get_symbol_value(struct readelf *re, int symtab, int i) 6575 { 6576 struct section *s; 6577 GElf_Sym sym; 6578 Elf_Data *data; 6579 int elferr; 6580 6581 s = &re->sl[symtab]; 6582 if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM) 6583 return (0); 6584 (void) elf_errno(); 6585 if ((data = elf_getdata(s->scn, NULL)) == NULL) { 6586 elferr = elf_errno(); 6587 if (elferr != 0) 6588 warnx("elf_getdata failed: %s", elf_errmsg(elferr)); 6589 return (0); 6590 } 6591 if (gelf_getsym(data, i, &sym) != &sym) 6592 return (0); 6593 6594 return (sym.st_value); 6595 } 6596 6597 static void 6598 hex_dump(struct readelf *re) 6599 { 6600 struct section *s; 6601 Elf_Data *d; 6602 uint8_t *buf; 6603 size_t sz, nbytes; 6604 uint64_t addr; 6605 int elferr, i, j; 6606 6607 for (i = 1; (size_t) i < re->shnum; i++) { 6608 s = &re->sl[i]; 6609 if (find_dumpop(re, (size_t) i, s->name, HEX_DUMP, -1) == NULL) 6610 continue; 6611 (void) elf_errno(); 6612 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 6613 elferr = elf_errno(); 6614 if (elferr != 0) 6615 warnx("elf_getdata failed: %s", 6616 elf_errmsg(elferr)); 6617 continue; 6618 } 6619 if (d->d_size <= 0 || d->d_buf == NULL) { 6620 printf("\nSection '%s' has no data to dump.\n", 6621 s->name); 6622 continue; 6623 } 6624 buf = d->d_buf; 6625 sz = d->d_size; 6626 addr = s->addr; 6627 printf("\nHex dump of section '%s':\n", s->name); 6628 while (sz > 0) { 6629 printf(" 0x%8.8jx ", (uintmax_t)addr); 6630 nbytes = sz > 16? 16 : sz; 6631 for (j = 0; j < 16; j++) { 6632 if ((size_t)j < nbytes) 6633 printf("%2.2x", buf[j]); 6634 else 6635 printf(" "); 6636 if ((j & 3) == 3) 6637 printf(" "); 6638 } 6639 for (j = 0; (size_t)j < nbytes; j++) { 6640 if (isprint(buf[j])) 6641 printf("%c", buf[j]); 6642 else 6643 printf("."); 6644 } 6645 printf("\n"); 6646 buf += nbytes; 6647 addr += nbytes; 6648 sz -= nbytes; 6649 } 6650 } 6651 } 6652 6653 static void 6654 str_dump(struct readelf *re) 6655 { 6656 struct section *s; 6657 Elf_Data *d; 6658 unsigned char *start, *end, *buf_end; 6659 unsigned int len; 6660 int i, j, elferr, found; 6661 6662 for (i = 1; (size_t) i < re->shnum; i++) { 6663 s = &re->sl[i]; 6664 if (find_dumpop(re, (size_t) i, s->name, STR_DUMP, -1) == NULL) 6665 continue; 6666 (void) elf_errno(); 6667 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 6668 elferr = elf_errno(); 6669 if (elferr != 0) 6670 warnx("elf_getdata failed: %s", 6671 elf_errmsg(elferr)); 6672 continue; 6673 } 6674 if (d->d_size <= 0 || d->d_buf == NULL) { 6675 printf("\nSection '%s' has no data to dump.\n", 6676 s->name); 6677 continue; 6678 } 6679 buf_end = (unsigned char *) d->d_buf + d->d_size; 6680 start = (unsigned char *) d->d_buf; 6681 found = 0; 6682 printf("\nString dump of section '%s':\n", s->name); 6683 for (;;) { 6684 while (start < buf_end && !isprint(*start)) 6685 start++; 6686 if (start >= buf_end) 6687 break; 6688 end = start + 1; 6689 while (end < buf_end && isprint(*end)) 6690 end++; 6691 printf(" [%6lx] ", 6692 (long) (start - (unsigned char *) d->d_buf)); 6693 len = end - start; 6694 for (j = 0; (unsigned int) j < len; j++) 6695 putchar(start[j]); 6696 putchar('\n'); 6697 found = 1; 6698 if (end >= buf_end) 6699 break; 6700 start = end + 1; 6701 } 6702 if (!found) 6703 printf(" No strings found in this section."); 6704 putchar('\n'); 6705 } 6706 } 6707 6708 static void 6709 load_sections(struct readelf *re) 6710 { 6711 struct section *s; 6712 const char *name; 6713 Elf_Scn *scn; 6714 GElf_Shdr sh; 6715 size_t shstrndx, ndx; 6716 int elferr; 6717 6718 /* Allocate storage for internal section list. */ 6719 if (!elf_getshnum(re->elf, &re->shnum)) { 6720 warnx("elf_getshnum failed: %s", elf_errmsg(-1)); 6721 return; 6722 } 6723 if (re->sl != NULL) 6724 free(re->sl); 6725 if ((re->sl = calloc(re->shnum, sizeof(*re->sl))) == NULL) 6726 err(EXIT_FAILURE, "calloc failed"); 6727 6728 /* Get the index of .shstrtab section. */ 6729 if (!elf_getshstrndx(re->elf, &shstrndx)) { 6730 warnx("elf_getshstrndx failed: %s", elf_errmsg(-1)); 6731 return; 6732 } 6733 6734 if ((scn = elf_getscn(re->elf, 0)) == NULL) 6735 return; 6736 6737 (void) elf_errno(); 6738 do { 6739 if (gelf_getshdr(scn, &sh) == NULL) { 6740 warnx("gelf_getshdr failed: %s", elf_errmsg(-1)); 6741 (void) elf_errno(); 6742 continue; 6743 } 6744 if ((name = elf_strptr(re->elf, shstrndx, sh.sh_name)) == NULL) { 6745 (void) elf_errno(); 6746 name = "ERROR"; 6747 } 6748 if ((ndx = elf_ndxscn(scn)) == SHN_UNDEF) { 6749 if ((elferr = elf_errno()) != 0) 6750 warnx("elf_ndxscn failed: %s", 6751 elf_errmsg(elferr)); 6752 continue; 6753 } 6754 if (ndx >= re->shnum) { 6755 warnx("section index of '%s' out of range", name); 6756 continue; 6757 } 6758 s = &re->sl[ndx]; 6759 s->name = name; 6760 s->scn = scn; 6761 s->off = sh.sh_offset; 6762 s->sz = sh.sh_size; 6763 s->entsize = sh.sh_entsize; 6764 s->align = sh.sh_addralign; 6765 s->type = sh.sh_type; 6766 s->flags = sh.sh_flags; 6767 s->addr = sh.sh_addr; 6768 s->link = sh.sh_link; 6769 s->info = sh.sh_info; 6770 } while ((scn = elf_nextscn(re->elf, scn)) != NULL); 6771 elferr = elf_errno(); 6772 if (elferr != 0) 6773 warnx("elf_nextscn failed: %s", elf_errmsg(elferr)); 6774 } 6775 6776 static void 6777 unload_sections(struct readelf *re) 6778 { 6779 6780 if (re->sl != NULL) { 6781 free(re->sl); 6782 re->sl = NULL; 6783 } 6784 re->shnum = 0; 6785 re->vd_s = NULL; 6786 re->vn_s = NULL; 6787 re->vs_s = NULL; 6788 re->vs = NULL; 6789 re->vs_sz = 0; 6790 if (re->ver != NULL) { 6791 free(re->ver); 6792 re->ver = NULL; 6793 re->ver_sz = 0; 6794 } 6795 } 6796 6797 static void 6798 dump_elf(struct readelf *re) 6799 { 6800 6801 /* Fetch ELF header. No need to continue if it fails. */ 6802 if (gelf_getehdr(re->elf, &re->ehdr) == NULL) { 6803 warnx("gelf_getehdr failed: %s", elf_errmsg(-1)); 6804 return; 6805 } 6806 if ((re->ec = gelf_getclass(re->elf)) == ELFCLASSNONE) { 6807 warnx("gelf_getclass failed: %s", elf_errmsg(-1)); 6808 return; 6809 } 6810 if (re->ehdr.e_ident[EI_DATA] == ELFDATA2MSB) { 6811 re->dw_read = _read_msb; 6812 re->dw_decode = _decode_msb; 6813 } else { 6814 re->dw_read = _read_lsb; 6815 re->dw_decode = _decode_lsb; 6816 } 6817 6818 if (re->options & ~RE_H) 6819 load_sections(re); 6820 if ((re->options & RE_VV) || (re->options & RE_S)) 6821 search_ver(re); 6822 if (re->options & RE_H) 6823 dump_ehdr(re); 6824 if (re->options & RE_L) 6825 dump_phdr(re); 6826 if (re->options & RE_SS) 6827 dump_shdr(re); 6828 if (re->options & RE_D) 6829 dump_dynamic(re); 6830 if (re->options & RE_R) 6831 dump_reloc(re); 6832 if (re->options & RE_S) 6833 dump_symtabs(re); 6834 if (re->options & RE_N) 6835 dump_notes(re); 6836 if (re->options & RE_II) 6837 dump_hash(re); 6838 if (re->options & RE_X) 6839 hex_dump(re); 6840 if (re->options & RE_P) 6841 str_dump(re); 6842 if (re->options & RE_VV) 6843 dump_ver(re); 6844 if (re->options & RE_AA) 6845 dump_arch_specific_info(re); 6846 if (re->options & RE_W) 6847 dump_dwarf(re); 6848 if (re->options & ~RE_H) 6849 unload_sections(re); 6850 } 6851 6852 static void 6853 dump_dwarf(struct readelf *re) 6854 { 6855 int error; 6856 Dwarf_Error de; 6857 6858 if (dwarf_elf_init(re->elf, DW_DLC_READ, NULL, NULL, &re->dbg, &de)) { 6859 if ((error = dwarf_errno(de)) != DW_DLE_DEBUG_INFO_NULL) 6860 errx(EXIT_FAILURE, "dwarf_elf_init failed: %s", 6861 dwarf_errmsg(de)); 6862 return; 6863 } 6864 6865 if (re->dop & DW_A) 6866 dump_dwarf_abbrev(re); 6867 if (re->dop & DW_L) 6868 dump_dwarf_line(re); 6869 if (re->dop & DW_LL) 6870 dump_dwarf_line_decoded(re); 6871 if (re->dop & DW_I) { 6872 dump_dwarf_info(re, 0); 6873 dump_dwarf_info(re, 1); 6874 } 6875 if (re->dop & DW_P) 6876 dump_dwarf_pubnames(re); 6877 if (re->dop & DW_R) 6878 dump_dwarf_aranges(re); 6879 if (re->dop & DW_RR) 6880 dump_dwarf_ranges(re); 6881 if (re->dop & DW_M) 6882 dump_dwarf_macinfo(re); 6883 if (re->dop & DW_F) 6884 dump_dwarf_frame(re, 0); 6885 else if (re->dop & DW_FF) 6886 dump_dwarf_frame(re, 1); 6887 if (re->dop & DW_S) 6888 dump_dwarf_str(re); 6889 if (re->dop & DW_O) 6890 dump_dwarf_loclist(re); 6891 6892 dwarf_finish(re->dbg, &de); 6893 } 6894 6895 static void 6896 dump_ar(struct readelf *re, int fd) 6897 { 6898 Elf_Arsym *arsym; 6899 Elf_Arhdr *arhdr; 6900 Elf_Cmd cmd; 6901 Elf *e; 6902 size_t sz; 6903 off_t off; 6904 int i; 6905 6906 re->ar = re->elf; 6907 6908 if (re->options & RE_C) { 6909 if ((arsym = elf_getarsym(re->ar, &sz)) == NULL) { 6910 warnx("elf_getarsym() failed: %s", elf_errmsg(-1)); 6911 goto process_members; 6912 } 6913 printf("Index of archive %s: (%ju entries)\n", re->filename, 6914 (uintmax_t) sz - 1); 6915 off = 0; 6916 for (i = 0; (size_t) i < sz; i++) { 6917 if (arsym[i].as_name == NULL) 6918 break; 6919 if (arsym[i].as_off != off) { 6920 off = arsym[i].as_off; 6921 if (elf_rand(re->ar, off) != off) { 6922 warnx("elf_rand() failed: %s", 6923 elf_errmsg(-1)); 6924 continue; 6925 } 6926 if ((e = elf_begin(fd, ELF_C_READ, re->ar)) == 6927 NULL) { 6928 warnx("elf_begin() failed: %s", 6929 elf_errmsg(-1)); 6930 continue; 6931 } 6932 if ((arhdr = elf_getarhdr(e)) == NULL) { 6933 warnx("elf_getarhdr() failed: %s", 6934 elf_errmsg(-1)); 6935 elf_end(e); 6936 continue; 6937 } 6938 printf("Binary %s(%s) contains:\n", 6939 re->filename, arhdr->ar_name); 6940 } 6941 printf("\t%s\n", arsym[i].as_name); 6942 } 6943 if (elf_rand(re->ar, SARMAG) != SARMAG) { 6944 warnx("elf_rand() failed: %s", elf_errmsg(-1)); 6945 return; 6946 } 6947 } 6948 6949 process_members: 6950 6951 if ((re->options & ~RE_C) == 0) 6952 return; 6953 6954 cmd = ELF_C_READ; 6955 while ((re->elf = elf_begin(fd, cmd, re->ar)) != NULL) { 6956 if ((arhdr = elf_getarhdr(re->elf)) == NULL) { 6957 warnx("elf_getarhdr() failed: %s", elf_errmsg(-1)); 6958 goto next_member; 6959 } 6960 if (strcmp(arhdr->ar_name, "/") == 0 || 6961 strcmp(arhdr->ar_name, "//") == 0 || 6962 strcmp(arhdr->ar_name, "__.SYMDEF") == 0) 6963 goto next_member; 6964 printf("\nFile: %s(%s)\n", re->filename, arhdr->ar_name); 6965 dump_elf(re); 6966 6967 next_member: 6968 cmd = elf_next(re->elf); 6969 elf_end(re->elf); 6970 } 6971 re->elf = re->ar; 6972 } 6973 6974 static void 6975 dump_object(struct readelf *re) 6976 { 6977 int fd; 6978 6979 if ((fd = open(re->filename, O_RDONLY)) == -1) { 6980 warn("open %s failed", re->filename); 6981 return; 6982 } 6983 6984 if ((re->flags & DISPLAY_FILENAME) != 0) 6985 printf("\nFile: %s\n", re->filename); 6986 6987 if ((re->elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) { 6988 warnx("elf_begin() failed: %s", elf_errmsg(-1)); 6989 return; 6990 } 6991 6992 switch (elf_kind(re->elf)) { 6993 case ELF_K_NONE: 6994 warnx("Not an ELF file."); 6995 return; 6996 case ELF_K_ELF: 6997 dump_elf(re); 6998 break; 6999 case ELF_K_AR: 7000 dump_ar(re, fd); 7001 break; 7002 default: 7003 warnx("Internal: libelf returned unknown elf kind."); 7004 return; 7005 } 7006 7007 elf_end(re->elf); 7008 } 7009 7010 static void 7011 add_dumpop(struct readelf *re, size_t si, const char *sn, int op, int t) 7012 { 7013 struct dumpop *d; 7014 7015 if ((d = find_dumpop(re, si, sn, -1, t)) == NULL) { 7016 if ((d = calloc(1, sizeof(*d))) == NULL) 7017 err(EXIT_FAILURE, "calloc failed"); 7018 if (t == DUMP_BY_INDEX) 7019 d->u.si = si; 7020 else 7021 d->u.sn = sn; 7022 d->type = t; 7023 d->op = op; 7024 STAILQ_INSERT_TAIL(&re->v_dumpop, d, dumpop_list); 7025 } else 7026 d->op |= op; 7027 } 7028 7029 static struct dumpop * 7030 find_dumpop(struct readelf *re, size_t si, const char *sn, int op, int t) 7031 { 7032 struct dumpop *d; 7033 7034 STAILQ_FOREACH(d, &re->v_dumpop, dumpop_list) { 7035 if ((op == -1 || op & d->op) && 7036 (t == -1 || (unsigned) t == d->type)) { 7037 if ((d->type == DUMP_BY_INDEX && d->u.si == si) || 7038 (d->type == DUMP_BY_NAME && !strcmp(d->u.sn, sn))) 7039 return (d); 7040 } 7041 } 7042 7043 return (NULL); 7044 } 7045 7046 static struct { 7047 const char *ln; 7048 char sn; 7049 int value; 7050 } dwarf_op[] = { 7051 {"rawline", 'l', DW_L}, 7052 {"decodedline", 'L', DW_LL}, 7053 {"info", 'i', DW_I}, 7054 {"abbrev", 'a', DW_A}, 7055 {"pubnames", 'p', DW_P}, 7056 {"aranges", 'r', DW_R}, 7057 {"ranges", 'r', DW_R}, 7058 {"Ranges", 'R', DW_RR}, 7059 {"macro", 'm', DW_M}, 7060 {"frames", 'f', DW_F}, 7061 {"frames-interp", 'F', DW_FF}, 7062 {"str", 's', DW_S}, 7063 {"loc", 'o', DW_O}, 7064 {NULL, 0, 0} 7065 }; 7066 7067 static void 7068 parse_dwarf_op_short(struct readelf *re, const char *op) 7069 { 7070 int i; 7071 7072 if (op == NULL) { 7073 re->dop |= DW_DEFAULT_OPTIONS; 7074 return; 7075 } 7076 7077 for (; *op != '\0'; op++) { 7078 for (i = 0; dwarf_op[i].ln != NULL; i++) { 7079 if (dwarf_op[i].sn == *op) { 7080 re->dop |= dwarf_op[i].value; 7081 break; 7082 } 7083 } 7084 } 7085 } 7086 7087 static void 7088 parse_dwarf_op_long(struct readelf *re, const char *op) 7089 { 7090 char *p, *token, *bp; 7091 int i; 7092 7093 if (op == NULL) { 7094 re->dop |= DW_DEFAULT_OPTIONS; 7095 return; 7096 } 7097 7098 if ((p = strdup(op)) == NULL) 7099 err(EXIT_FAILURE, "strdup failed"); 7100 bp = p; 7101 7102 while ((token = strsep(&p, ",")) != NULL) { 7103 for (i = 0; dwarf_op[i].ln != NULL; i++) { 7104 if (!strcmp(token, dwarf_op[i].ln)) { 7105 re->dop |= dwarf_op[i].value; 7106 break; 7107 } 7108 } 7109 } 7110 7111 free(bp); 7112 } 7113 7114 static uint64_t 7115 _read_lsb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read) 7116 { 7117 uint64_t ret; 7118 uint8_t *src; 7119 7120 src = (uint8_t *) d->d_buf + *offsetp; 7121 7122 ret = 0; 7123 switch (bytes_to_read) { 7124 case 8: 7125 ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40; 7126 ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56; 7127 case 4: 7128 ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24; 7129 case 2: 7130 ret |= ((uint64_t) src[1]) << 8; 7131 case 1: 7132 ret |= src[0]; 7133 break; 7134 default: 7135 return (0); 7136 } 7137 7138 *offsetp += bytes_to_read; 7139 7140 return (ret); 7141 } 7142 7143 static uint64_t 7144 _read_msb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read) 7145 { 7146 uint64_t ret; 7147 uint8_t *src; 7148 7149 src = (uint8_t *) d->d_buf + *offsetp; 7150 7151 switch (bytes_to_read) { 7152 case 1: 7153 ret = src[0]; 7154 break; 7155 case 2: 7156 ret = src[1] | ((uint64_t) src[0]) << 8; 7157 break; 7158 case 4: 7159 ret = src[3] | ((uint64_t) src[2]) << 8; 7160 ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24; 7161 break; 7162 case 8: 7163 ret = src[7] | ((uint64_t) src[6]) << 8; 7164 ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24; 7165 ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40; 7166 ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56; 7167 break; 7168 default: 7169 return (0); 7170 } 7171 7172 *offsetp += bytes_to_read; 7173 7174 return (ret); 7175 } 7176 7177 static uint64_t 7178 _decode_lsb(uint8_t **data, int bytes_to_read) 7179 { 7180 uint64_t ret; 7181 uint8_t *src; 7182 7183 src = *data; 7184 7185 ret = 0; 7186 switch (bytes_to_read) { 7187 case 8: 7188 ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40; 7189 ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56; 7190 case 4: 7191 ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24; 7192 case 2: 7193 ret |= ((uint64_t) src[1]) << 8; 7194 case 1: 7195 ret |= src[0]; 7196 break; 7197 default: 7198 return (0); 7199 } 7200 7201 *data += bytes_to_read; 7202 7203 return (ret); 7204 } 7205 7206 static uint64_t 7207 _decode_msb(uint8_t **data, int bytes_to_read) 7208 { 7209 uint64_t ret; 7210 uint8_t *src; 7211 7212 src = *data; 7213 7214 ret = 0; 7215 switch (bytes_to_read) { 7216 case 1: 7217 ret = src[0]; 7218 break; 7219 case 2: 7220 ret = src[1] | ((uint64_t) src[0]) << 8; 7221 break; 7222 case 4: 7223 ret = src[3] | ((uint64_t) src[2]) << 8; 7224 ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24; 7225 break; 7226 case 8: 7227 ret = src[7] | ((uint64_t) src[6]) << 8; 7228 ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24; 7229 ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40; 7230 ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56; 7231 break; 7232 default: 7233 return (0); 7234 break; 7235 } 7236 7237 *data += bytes_to_read; 7238 7239 return (ret); 7240 } 7241 7242 static int64_t 7243 _decode_sleb128(uint8_t **dp) 7244 { 7245 int64_t ret = 0; 7246 uint8_t b; 7247 int shift = 0; 7248 7249 uint8_t *src = *dp; 7250 7251 do { 7252 b = *src++; 7253 ret |= ((b & 0x7f) << shift); 7254 shift += 7; 7255 } while ((b & 0x80) != 0); 7256 7257 if (shift < 32 && (b & 0x40) != 0) 7258 ret |= (-1 << shift); 7259 7260 *dp = src; 7261 7262 return (ret); 7263 } 7264 7265 static uint64_t 7266 _decode_uleb128(uint8_t **dp) 7267 { 7268 uint64_t ret = 0; 7269 uint8_t b; 7270 int shift = 0; 7271 7272 uint8_t *src = *dp; 7273 7274 do { 7275 b = *src++; 7276 ret |= ((b & 0x7f) << shift); 7277 shift += 7; 7278 } while ((b & 0x80) != 0); 7279 7280 *dp = src; 7281 7282 return (ret); 7283 } 7284 7285 static void 7286 readelf_version(void) 7287 { 7288 (void) printf("%s (%s)\n", ELFTC_GETPROGNAME(), 7289 elftc_version()); 7290 exit(EXIT_SUCCESS); 7291 } 7292 7293 #define USAGE_MESSAGE "\ 7294 Usage: %s [options] file...\n\ 7295 Display information about ELF objects and ar(1) archives.\n\n\ 7296 Options:\n\ 7297 -a | --all Equivalent to specifying options '-dhIlrsASV'.\n\ 7298 -c | --archive-index Print the archive symbol table for archives.\n\ 7299 -d | --dynamic Print the contents of SHT_DYNAMIC sections.\n\ 7300 -e | --headers Print all headers in the object.\n\ 7301 -g | --section-groups (accepted, but ignored)\n\ 7302 -h | --file-header Print the file header for the object.\n\ 7303 -l | --program-headers Print the PHDR table for the object.\n\ 7304 -n | --notes Print the contents of SHT_NOTE sections.\n\ 7305 -p INDEX | --string-dump=INDEX\n\ 7306 Print the contents of section at index INDEX.\n\ 7307 -r | --relocs Print relocation information.\n\ 7308 -s | --syms | --symbols Print symbol tables.\n\ 7309 -t | --section-details Print additional information about sections.\n\ 7310 -v | --version Print a version identifier and exit.\n\ 7311 -x INDEX | --hex-dump=INDEX\n\ 7312 Display contents of a section as hexadecimal.\n\ 7313 -A | --arch-specific (accepted, but ignored)\n\ 7314 -D | --use-dynamic Print the symbol table specified by the DT_SYMTAB\n\ 7315 entry in the \".dynamic\" section.\n\ 7316 -H | --help Print a help message.\n\ 7317 -I | --histogram Print information on bucket list lengths for \n\ 7318 hash sections.\n\ 7319 -N | --full-section-name (accepted, but ignored)\n\ 7320 -S | --sections | --section-headers\n\ 7321 Print information about section headers.\n\ 7322 -V | --version-info Print symbol versoning information.\n\ 7323 -W | --wide Print information without wrapping long lines.\n" 7324 7325 7326 static void 7327 readelf_usage(void) 7328 { 7329 fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME()); 7330 exit(EXIT_FAILURE); 7331 } 7332 7333 int 7334 main(int argc, char **argv) 7335 { 7336 struct readelf *re, re_storage; 7337 unsigned long si; 7338 int opt, i; 7339 char *ep; 7340 7341 re = &re_storage; 7342 memset(re, 0, sizeof(*re)); 7343 STAILQ_INIT(&re->v_dumpop); 7344 7345 while ((opt = getopt_long(argc, argv, "AacDdegHhIi:lNnp:rSstuVvWw::x:", 7346 longopts, NULL)) != -1) { 7347 switch(opt) { 7348 case '?': 7349 readelf_usage(); 7350 break; 7351 case 'A': 7352 re->options |= RE_AA; 7353 break; 7354 case 'a': 7355 re->options |= RE_AA | RE_D | RE_H | RE_II | RE_L | 7356 RE_R | RE_SS | RE_S | RE_VV; 7357 break; 7358 case 'c': 7359 re->options |= RE_C; 7360 break; 7361 case 'D': 7362 re->options |= RE_DD; 7363 break; 7364 case 'd': 7365 re->options |= RE_D; 7366 break; 7367 case 'e': 7368 re->options |= RE_H | RE_L | RE_SS; 7369 break; 7370 case 'g': 7371 re->options |= RE_G; 7372 break; 7373 case 'H': 7374 readelf_usage(); 7375 break; 7376 case 'h': 7377 re->options |= RE_H; 7378 break; 7379 case 'I': 7380 re->options |= RE_II; 7381 break; 7382 case 'i': 7383 /* Not implemented yet. */ 7384 break; 7385 case 'l': 7386 re->options |= RE_L; 7387 break; 7388 case 'N': 7389 re->options |= RE_NN; 7390 break; 7391 case 'n': 7392 re->options |= RE_N; 7393 break; 7394 case 'p': 7395 re->options |= RE_P; 7396 si = strtoul(optarg, &ep, 10); 7397 if (*ep == '\0') 7398 add_dumpop(re, (size_t) si, NULL, STR_DUMP, 7399 DUMP_BY_INDEX); 7400 else 7401 add_dumpop(re, 0, optarg, STR_DUMP, 7402 DUMP_BY_NAME); 7403 break; 7404 case 'r': 7405 re->options |= RE_R; 7406 break; 7407 case 'S': 7408 re->options |= RE_SS; 7409 break; 7410 case 's': 7411 re->options |= RE_S; 7412 break; 7413 case 't': 7414 re->options |= RE_T; 7415 break; 7416 case 'u': 7417 re->options |= RE_U; 7418 break; 7419 case 'V': 7420 re->options |= RE_VV; 7421 break; 7422 case 'v': 7423 readelf_version(); 7424 break; 7425 case 'W': 7426 re->options |= RE_WW; 7427 break; 7428 case 'w': 7429 re->options |= RE_W; 7430 parse_dwarf_op_short(re, optarg); 7431 break; 7432 case 'x': 7433 re->options |= RE_X; 7434 si = strtoul(optarg, &ep, 10); 7435 if (*ep == '\0') 7436 add_dumpop(re, (size_t) si, NULL, HEX_DUMP, 7437 DUMP_BY_INDEX); 7438 else 7439 add_dumpop(re, 0, optarg, HEX_DUMP, 7440 DUMP_BY_NAME); 7441 break; 7442 case OPTION_DEBUG_DUMP: 7443 re->options |= RE_W; 7444 parse_dwarf_op_long(re, optarg); 7445 } 7446 } 7447 7448 argv += optind; 7449 argc -= optind; 7450 7451 if (argc == 0 || re->options == 0) 7452 readelf_usage(); 7453 7454 if (argc > 1) 7455 re->flags |= DISPLAY_FILENAME; 7456 7457 if (elf_version(EV_CURRENT) == EV_NONE) 7458 errx(EXIT_FAILURE, "ELF library initialization failed: %s", 7459 elf_errmsg(-1)); 7460 7461 for (i = 0; i < argc; i++) 7462 if (argv[i] != NULL) { 7463 re->filename = argv[i]; 7464 dump_object(re); 7465 } 7466 7467 exit(EXIT_SUCCESS); 7468 } 7469