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