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