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