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