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