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