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