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