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