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("\nSymbol table '%s' contains %d entries:\n", s->name, len); 3267 printf("%7s%9s%14s%5s%8s%6s%9s%5s\n", "Num:", "Value", "Size", "Type", 3268 "Bind", "Vis", "Ndx", "Name"); 3269 3270 for (j = 0; j < len; j++) { 3271 if (gelf_getsym(d, j, &sym) != &sym) { 3272 warnx("gelf_getsym failed: %s", elf_errmsg(-1)); 3273 continue; 3274 } 3275 printf("%6d:", j); 3276 printf(" %16.16jx", (uintmax_t) sym.st_value); 3277 printf(" %5ju", (uintmax_t) sym.st_size); 3278 printf(" %-7s", st_type(re->ehdr.e_machine, 3279 re->ehdr.e_ident[EI_OSABI], GELF_ST_TYPE(sym.st_info))); 3280 printf(" %-6s", st_bind(GELF_ST_BIND(sym.st_info))); 3281 printf(" %-8s", st_vis(GELF_ST_VISIBILITY(sym.st_other))); 3282 printf(" %3s", st_shndx(sym.st_shndx)); 3283 if ((name = elf_strptr(re->elf, stab, sym.st_name)) != NULL) 3284 printf(" %s", name); 3285 /* Append symbol version string for SHT_DYNSYM symbol table. */ 3286 if (s->type == SHT_DYNSYM && re->ver != NULL && 3287 re->vs != NULL && re->vs[j] > 1) { 3288 vs = re->vs[j] & VERSYM_VERSION; 3289 if (vs >= re->ver_sz || re->ver[vs].name == NULL) { 3290 warnx("invalid versym version index %u", vs); 3291 break; 3292 } 3293 if (re->vs[j] & VERSYM_HIDDEN || re->ver[vs].type == 0) 3294 printf("@%s (%d)", re->ver[vs].name, vs); 3295 else 3296 printf("@@%s (%d)", re->ver[vs].name, vs); 3297 } 3298 putchar('\n'); 3299 } 3300 3301 } 3302 3303 static void 3304 dump_symtabs(struct readelf *re) 3305 { 3306 GElf_Dyn dyn; 3307 Elf_Data *d; 3308 struct section *s; 3309 uint64_t dyn_off; 3310 int elferr, i, len; 3311 3312 /* 3313 * If -D is specified, only dump the symbol table specified by 3314 * the DT_SYMTAB entry in the .dynamic section. 3315 */ 3316 dyn_off = 0; 3317 if (re->options & RE_DD) { 3318 s = NULL; 3319 for (i = 0; (size_t)i < re->shnum; i++) 3320 if (re->sl[i].type == SHT_DYNAMIC) { 3321 s = &re->sl[i]; 3322 break; 3323 } 3324 if (s == NULL) 3325 return; 3326 (void) elf_errno(); 3327 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 3328 elferr = elf_errno(); 3329 if (elferr != 0) 3330 warnx("elf_getdata failed: %s", elf_errmsg(-1)); 3331 return; 3332 } 3333 if (d->d_size <= 0) 3334 return; 3335 if (!get_ent_count(s, &len)) 3336 return; 3337 3338 for (i = 0; i < len; i++) { 3339 if (gelf_getdyn(d, i, &dyn) != &dyn) { 3340 warnx("gelf_getdyn failed: %s", elf_errmsg(-1)); 3341 continue; 3342 } 3343 if (dyn.d_tag == DT_SYMTAB) { 3344 dyn_off = dyn.d_un.d_val; 3345 break; 3346 } 3347 } 3348 } 3349 3350 /* Find and dump symbol tables. */ 3351 for (i = 0; (size_t)i < re->shnum; i++) { 3352 s = &re->sl[i]; 3353 if (s->type == SHT_SYMTAB || s->type == SHT_DYNSYM) { 3354 if (re->options & RE_DD) { 3355 if (dyn_off == s->addr) { 3356 dump_symtab(re, i); 3357 break; 3358 } 3359 } else 3360 dump_symtab(re, i); 3361 } 3362 } 3363 } 3364 3365 static void 3366 dump_svr4_hash(struct section *s) 3367 { 3368 Elf_Data *d; 3369 uint32_t *buf; 3370 uint32_t nbucket, nchain; 3371 uint32_t *bucket, *chain; 3372 uint32_t *bl, *c, maxl, total; 3373 int elferr, i, j; 3374 3375 /* Read and parse the content of .hash section. */ 3376 (void) elf_errno(); 3377 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 3378 elferr = elf_errno(); 3379 if (elferr != 0) 3380 warnx("elf_getdata failed: %s", elf_errmsg(elferr)); 3381 return; 3382 } 3383 if (d->d_size < 2 * sizeof(uint32_t)) { 3384 warnx(".hash section too small"); 3385 return; 3386 } 3387 buf = d->d_buf; 3388 nbucket = buf[0]; 3389 nchain = buf[1]; 3390 if (nbucket <= 0 || nchain <= 0) { 3391 warnx("Malformed .hash section"); 3392 return; 3393 } 3394 if (d->d_size != (nbucket + nchain + 2) * sizeof(uint32_t)) { 3395 warnx("Malformed .hash section"); 3396 return; 3397 } 3398 bucket = &buf[2]; 3399 chain = &buf[2 + nbucket]; 3400 3401 maxl = 0; 3402 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL) 3403 errx(EXIT_FAILURE, "calloc failed"); 3404 for (i = 0; (uint32_t)i < nbucket; i++) 3405 for (j = bucket[i]; j > 0 && (uint32_t)j < nchain; j = chain[j]) 3406 if (++bl[i] > maxl) 3407 maxl = bl[i]; 3408 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL) 3409 errx(EXIT_FAILURE, "calloc failed"); 3410 for (i = 0; (uint32_t)i < nbucket; i++) 3411 c[bl[i]]++; 3412 printf("\nHistogram for bucket list length (total of %u buckets):\n", 3413 nbucket); 3414 printf(" Length\tNumber\t\t%% of total\tCoverage\n"); 3415 total = 0; 3416 for (i = 0; (uint32_t)i <= maxl; i++) { 3417 total += c[i] * i; 3418 printf("%7u\t%-10u\t(%5.1f%%)\t%5.1f%%\n", i, c[i], 3419 c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1)); 3420 } 3421 free(c); 3422 free(bl); 3423 } 3424 3425 static void 3426 dump_svr4_hash64(struct readelf *re, struct section *s) 3427 { 3428 Elf_Data *d, dst; 3429 uint64_t *buf; 3430 uint64_t nbucket, nchain; 3431 uint64_t *bucket, *chain; 3432 uint64_t *bl, *c, maxl, total; 3433 int elferr, i, j; 3434 3435 /* 3436 * ALPHA uses 64-bit hash entries. Since libelf assumes that 3437 * .hash section contains only 32-bit entry, an explicit 3438 * gelf_xlatetom is needed here. 3439 */ 3440 (void) elf_errno(); 3441 if ((d = elf_rawdata(s->scn, NULL)) == NULL) { 3442 elferr = elf_errno(); 3443 if (elferr != 0) 3444 warnx("elf_rawdata failed: %s", 3445 elf_errmsg(elferr)); 3446 return; 3447 } 3448 d->d_type = ELF_T_XWORD; 3449 memcpy(&dst, d, sizeof(Elf_Data)); 3450 if (gelf_xlatetom(re->elf, &dst, d, 3451 re->ehdr.e_ident[EI_DATA]) != &dst) { 3452 warnx("gelf_xlatetom failed: %s", elf_errmsg(-1)); 3453 return; 3454 } 3455 if (dst.d_size < 2 * sizeof(uint64_t)) { 3456 warnx(".hash section too small"); 3457 return; 3458 } 3459 buf = dst.d_buf; 3460 nbucket = buf[0]; 3461 nchain = buf[1]; 3462 if (nbucket <= 0 || nchain <= 0) { 3463 warnx("Malformed .hash section"); 3464 return; 3465 } 3466 if (d->d_size != (nbucket + nchain + 2) * sizeof(uint32_t)) { 3467 warnx("Malformed .hash section"); 3468 return; 3469 } 3470 bucket = &buf[2]; 3471 chain = &buf[2 + nbucket]; 3472 3473 maxl = 0; 3474 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL) 3475 errx(EXIT_FAILURE, "calloc failed"); 3476 for (i = 0; (uint32_t)i < nbucket; i++) 3477 for (j = bucket[i]; j > 0 && (uint32_t)j < nchain; j = chain[j]) 3478 if (++bl[i] > maxl) 3479 maxl = bl[i]; 3480 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL) 3481 errx(EXIT_FAILURE, "calloc failed"); 3482 for (i = 0; (uint64_t)i < nbucket; i++) 3483 c[bl[i]]++; 3484 printf("Histogram for bucket list length (total of %ju buckets):\n", 3485 (uintmax_t)nbucket); 3486 printf(" Length\tNumber\t\t%% of total\tCoverage\n"); 3487 total = 0; 3488 for (i = 0; (uint64_t)i <= maxl; i++) { 3489 total += c[i] * i; 3490 printf("%7u\t%-10ju\t(%5.1f%%)\t%5.1f%%\n", i, (uintmax_t)c[i], 3491 c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1)); 3492 } 3493 free(c); 3494 free(bl); 3495 } 3496 3497 static void 3498 dump_gnu_hash(struct readelf *re, struct section *s) 3499 { 3500 struct section *ds; 3501 Elf_Data *d; 3502 uint32_t *buf; 3503 uint32_t *bucket, *chain; 3504 uint32_t nbucket, nchain, symndx, maskwords; 3505 uint32_t *bl, *c, maxl, total; 3506 int elferr, dynsymcount, i, j; 3507 3508 (void) elf_errno(); 3509 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 3510 elferr = elf_errno(); 3511 if (elferr != 0) 3512 warnx("elf_getdata failed: %s", 3513 elf_errmsg(elferr)); 3514 return; 3515 } 3516 if (d->d_size < 4 * sizeof(uint32_t)) { 3517 warnx(".gnu.hash section too small"); 3518 return; 3519 } 3520 buf = d->d_buf; 3521 nbucket = buf[0]; 3522 symndx = buf[1]; 3523 maskwords = buf[2]; 3524 buf += 4; 3525 if (s->link >= re->shnum) 3526 return; 3527 ds = &re->sl[s->link]; 3528 if (!get_ent_count(ds, &dynsymcount)) 3529 return; 3530 if (symndx >= (uint32_t)dynsymcount) { 3531 warnx("Malformed .gnu.hash section (symndx out of range)"); 3532 return; 3533 } 3534 nchain = dynsymcount - symndx; 3535 if (d->d_size != 4 * sizeof(uint32_t) + maskwords * 3536 (re->ec == ELFCLASS32 ? sizeof(uint32_t) : sizeof(uint64_t)) + 3537 (nbucket + nchain) * sizeof(uint32_t)) { 3538 warnx("Malformed .gnu.hash section"); 3539 return; 3540 } 3541 bucket = buf + (re->ec == ELFCLASS32 ? maskwords : maskwords * 2); 3542 chain = bucket + nbucket; 3543 3544 maxl = 0; 3545 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL) 3546 errx(EXIT_FAILURE, "calloc failed"); 3547 for (i = 0; (uint32_t)i < nbucket; i++) 3548 for (j = bucket[i]; j > 0 && (uint32_t)j - symndx < nchain; 3549 j++) { 3550 if (++bl[i] > maxl) 3551 maxl = bl[i]; 3552 if (chain[j - symndx] & 1) 3553 break; 3554 } 3555 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL) 3556 errx(EXIT_FAILURE, "calloc failed"); 3557 for (i = 0; (uint32_t)i < nbucket; i++) 3558 c[bl[i]]++; 3559 printf("Histogram for bucket list length (total of %u buckets):\n", 3560 nbucket); 3561 printf(" Length\tNumber\t\t%% of total\tCoverage\n"); 3562 total = 0; 3563 for (i = 0; (uint32_t)i <= maxl; i++) { 3564 total += c[i] * i; 3565 printf("%7u\t%-10u\t(%5.1f%%)\t%5.1f%%\n", i, c[i], 3566 c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1)); 3567 } 3568 free(c); 3569 free(bl); 3570 } 3571 3572 static struct flag_desc gnu_property_aarch64_feature_1_and_bits[] = { 3573 { GNU_PROPERTY_AARCH64_FEATURE_1_BTI, "BTI" }, 3574 { GNU_PROPERTY_AARCH64_FEATURE_1_PAC, "PAC" }, 3575 { 0, NULL } 3576 }; 3577 3578 static struct flag_desc_list gnu_property_aarch64[] = { 3579 { 3580 GNU_PROPERTY_AARCH64_FEATURE_1_AND, 3581 "AArch64 features", 3582 gnu_property_aarch64_feature_1_and_bits 3583 }, 3584 { 0, NULL, NULL } 3585 }; 3586 3587 static struct flag_desc gnu_property_x86_feature_1_and_bits[] = { 3588 { GNU_PROPERTY_X86_FEATURE_1_IBT, "IBT" }, 3589 { GNU_PROPERTY_X86_FEATURE_1_SHSTK, "SHSTK" }, 3590 { 0, NULL } 3591 }; 3592 3593 static struct flag_desc_list gnu_property_x86[] = { 3594 { 3595 GNU_PROPERTY_X86_FEATURE_1_AND, 3596 "x64 features", 3597 gnu_property_x86_feature_1_and_bits 3598 }, 3599 { 0, NULL, NULL } 3600 }; 3601 3602 static struct { 3603 unsigned int emachine; 3604 struct flag_desc_list *flag_list; 3605 } gnu_property_archs[] = { 3606 { EM_AARCH64, gnu_property_aarch64 }, 3607 { EM_X86_64, gnu_property_x86 }, 3608 { 0, NULL } 3609 }; 3610 3611 static void 3612 dump_gnu_property_type_0(struct readelf *re, const char *buf, size_t sz) 3613 { 3614 struct flag_desc_list *desc_list; 3615 struct flag_desc *desc; 3616 size_t i; 3617 uint32_t type, prop_sz; 3618 3619 printf(" Properties: "); 3620 while (sz > 0) { 3621 if (sz < 8) 3622 goto bad; 3623 3624 type = *(const uint32_t *)(const void *)buf; 3625 prop_sz = *(const uint32_t *)(const void *)(buf + 4); 3626 buf += 8; 3627 sz -= 8; 3628 3629 if (prop_sz > sz) 3630 goto bad; 3631 3632 if (type >= GNU_PROPERTY_LOPROC && 3633 type <= GNU_PROPERTY_HIPROC) { 3634 desc_list = NULL; 3635 for (i = 0; gnu_property_archs[i].flag_list != NULL; 3636 i++) { 3637 if (gnu_property_archs[i].emachine == 3638 re->ehdr.e_machine) { 3639 desc_list = 3640 gnu_property_archs[i].flag_list; 3641 break; 3642 } 3643 } 3644 if (desc_list == NULL) { 3645 printf("machine type %x unknown\n", 3646 re->ehdr.e_machine); 3647 goto unknown; 3648 } 3649 3650 desc = NULL; 3651 for (i = 0; desc_list[i].desc != NULL; i++) { 3652 if (desc_list[i].type == type) { 3653 desc = desc_list[i].desc; 3654 break; 3655 } 3656 } 3657 if (desc != NULL) { 3658 printf("%s:", desc_list[i].desc_str); 3659 if (prop_sz != 4) 3660 goto bad; 3661 dump_flags(desc, 3662 *(const uint32_t *)(const void *)buf); 3663 } 3664 } 3665 3666 buf += roundup2(prop_sz, 8); 3667 sz -= roundup2(prop_sz, 8); 3668 } 3669 return; 3670 bad: 3671 printf("corrupt GNU property\n"); 3672 unknown: 3673 printf("remaining description data:"); 3674 for (i = 0; i < sz; i++) 3675 printf(" %02x", (unsigned char)buf[i]); 3676 printf("\n"); 3677 } 3678 3679 static void 3680 dump_hash(struct readelf *re) 3681 { 3682 struct section *s; 3683 int i; 3684 3685 for (i = 0; (size_t) i < re->shnum; i++) { 3686 s = &re->sl[i]; 3687 if (s->type == SHT_HASH || s->type == SHT_GNU_HASH) { 3688 if (s->type == SHT_GNU_HASH) 3689 dump_gnu_hash(re, s); 3690 else if (re->ehdr.e_machine == EM_ALPHA && 3691 s->entsize == 8) 3692 dump_svr4_hash64(re, s); 3693 else 3694 dump_svr4_hash(s); 3695 } 3696 } 3697 } 3698 3699 static void 3700 dump_notes(struct readelf *re) 3701 { 3702 struct section *s; 3703 const char *rawfile; 3704 GElf_Phdr phdr; 3705 Elf_Data *d; 3706 size_t filesize, phnum; 3707 int i, elferr; 3708 3709 if (re->ehdr.e_type == ET_CORE) { 3710 /* 3711 * Search program headers in the core file for 3712 * PT_NOTE entry. 3713 */ 3714 if (elf_getphnum(re->elf, &phnum) == 0) { 3715 warnx("elf_getphnum failed: %s", elf_errmsg(-1)); 3716 return; 3717 } 3718 if (phnum == 0) 3719 return; 3720 if ((rawfile = elf_rawfile(re->elf, &filesize)) == NULL) { 3721 warnx("elf_rawfile failed: %s", elf_errmsg(-1)); 3722 return; 3723 } 3724 for (i = 0; (size_t) i < phnum; i++) { 3725 if (gelf_getphdr(re->elf, i, &phdr) != &phdr) { 3726 warnx("gelf_getphdr failed: %s", 3727 elf_errmsg(-1)); 3728 continue; 3729 } 3730 if (phdr.p_type == PT_NOTE) { 3731 if (phdr.p_offset >= filesize || 3732 phdr.p_filesz > filesize - phdr.p_offset) { 3733 warnx("invalid PHDR offset"); 3734 continue; 3735 } 3736 dump_notes_content(re, rawfile + phdr.p_offset, 3737 phdr.p_filesz, phdr.p_offset); 3738 } 3739 } 3740 3741 } else { 3742 /* 3743 * For objects other than core files, Search for 3744 * SHT_NOTE sections. 3745 */ 3746 for (i = 0; (size_t) i < re->shnum; i++) { 3747 s = &re->sl[i]; 3748 if (s->type == SHT_NOTE) { 3749 (void) elf_errno(); 3750 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 3751 elferr = elf_errno(); 3752 if (elferr != 0) 3753 warnx("elf_getdata failed: %s", 3754 elf_errmsg(elferr)); 3755 continue; 3756 } 3757 dump_notes_content(re, d->d_buf, d->d_size, 3758 s->off); 3759 } 3760 } 3761 } 3762 } 3763 3764 static struct flag_desc note_feature_ctl_flags[] = { 3765 { NT_FREEBSD_FCTL_ASLR_DISABLE, "ASLR_DISABLE" }, 3766 { NT_FREEBSD_FCTL_PROTMAX_DISABLE, "PROTMAX_DISABLE" }, 3767 { NT_FREEBSD_FCTL_STKGAP_DISABLE, "STKGAP_DISABLE" }, 3768 { NT_FREEBSD_FCTL_WXNEEDED, "WXNEEDED" }, 3769 { NT_FREEBSD_FCTL_LA48, "LA48" }, 3770 { 0, NULL } 3771 }; 3772 3773 static bool 3774 dump_note_string(const char *description, const char *s, size_t len) 3775 { 3776 size_t i; 3777 3778 if (len == 0 || s[--len] != '\0') { 3779 return (false); 3780 } else { 3781 for (i = 0; i < len; i++) 3782 if (!isprint(s[i])) 3783 return (false); 3784 } 3785 3786 printf(" %s: %s\n", description, s); 3787 return (true); 3788 } 3789 3790 struct note_desc { 3791 uint32_t type; 3792 const char *description; 3793 bool (*fp)(const char *, const char *, size_t); 3794 }; 3795 3796 static struct note_desc xen_notes[] = { 3797 { 5, "Xen version", dump_note_string }, 3798 { 6, "Guest OS", dump_note_string }, 3799 { 7, "Guest version", dump_note_string }, 3800 { 8, "Loader", dump_note_string }, 3801 { 9, "PAE mode", dump_note_string }, 3802 { 10, "Features", dump_note_string }, 3803 { 11, "BSD symtab", dump_note_string }, 3804 { 0, NULL, NULL } 3805 }; 3806 3807 static void 3808 dump_notes_data(struct readelf *re, const char *name, uint32_t type, 3809 const char *buf, size_t sz) 3810 { 3811 struct note_desc *nd; 3812 size_t i; 3813 const uint32_t *ubuf; 3814 3815 /* Note data is at least 4-byte aligned. */ 3816 if (((uintptr_t)buf & 3) != 0) { 3817 warnx("bad note data alignment"); 3818 goto unknown; 3819 } 3820 ubuf = (const uint32_t *)(const void *)buf; 3821 3822 if (strcmp(name, "FreeBSD") == 0) { 3823 switch (type) { 3824 case NT_FREEBSD_ABI_TAG: 3825 if (sz != 4) 3826 goto unknown; 3827 printf(" ABI tag: %u\n", ubuf[0]); 3828 return; 3829 /* NT_FREEBSD_NOINIT_TAG carries no data, treat as unknown. */ 3830 case NT_FREEBSD_ARCH_TAG: 3831 printf(" Arch tag: %s\n", buf); 3832 return; 3833 case NT_FREEBSD_FEATURE_CTL: 3834 if (sz != 4) 3835 goto unknown; 3836 printf(" Features:"); 3837 dump_flags(note_feature_ctl_flags, ubuf[0]); 3838 return; 3839 } 3840 } else if (strcmp(name, "Go") == 0) { 3841 if (type == 4) { 3842 printf(" Build ID: "); 3843 for (i = 0; i < sz; i++) { 3844 printf(isprint(buf[i]) ? "%c" : "<%02x>", 3845 buf[i]); 3846 } 3847 printf("\n"); 3848 return; 3849 } 3850 } else if (strcmp(name, "GNU") == 0) { 3851 switch (type) { 3852 case NT_GNU_PROPERTY_TYPE_0: 3853 dump_gnu_property_type_0(re, buf, sz); 3854 return; 3855 case NT_GNU_BUILD_ID: 3856 printf(" Build ID: "); 3857 for (i = 0; i < sz; i++) 3858 printf("%02x", (unsigned char)buf[i]); 3859 printf("\n"); 3860 return; 3861 } 3862 } else if (strcmp(name, "Xen") == 0) { 3863 for (nd = xen_notes; nd->description != NULL; nd++) { 3864 if (nd->type == type) { 3865 if (nd->fp(nd->description, buf, sz)) 3866 return; 3867 else 3868 break; 3869 } 3870 } 3871 } 3872 unknown: 3873 printf(" description data:"); 3874 for (i = 0; i < sz; i++) 3875 printf(" %02x", (unsigned char)buf[i]); 3876 printf("\n"); 3877 } 3878 3879 static void 3880 dump_notes_content(struct readelf *re, const char *buf, size_t sz, off_t off) 3881 { 3882 Elf_Note *note; 3883 const char *end, *name; 3884 uint32_t namesz, descsz; 3885 3886 printf("\nNotes at offset %#010jx with length %#010jx:\n", 3887 (uintmax_t) off, (uintmax_t) sz); 3888 printf(" %-13s %-15s %s\n", "Owner", "Data size", "Description"); 3889 end = buf + sz; 3890 while (buf < end) { 3891 if (buf + sizeof(*note) > end) { 3892 warnx("invalid note header"); 3893 return; 3894 } 3895 note = (Elf_Note *)(uintptr_t) buf; 3896 namesz = roundup2(note->n_namesz, 4); 3897 descsz = roundup2(note->n_descsz, 4); 3898 if (namesz < note->n_namesz || descsz < note->n_descsz || 3899 buf + namesz + descsz > end) { 3900 warnx("invalid note header"); 3901 return; 3902 } 3903 buf += sizeof(Elf_Note); 3904 name = buf; 3905 buf += namesz; 3906 /* 3907 * The name field is required to be nul-terminated, and 3908 * n_namesz includes the terminating nul in observed 3909 * implementations (contrary to the ELF-64 spec). A special 3910 * case is needed for cores generated by some older Linux 3911 * versions, which write a note named "CORE" without a nul 3912 * terminator and n_namesz = 4. 3913 */ 3914 if (note->n_namesz == 0) 3915 name = ""; 3916 else if (note->n_namesz == 4 && strncmp(name, "CORE", 4) == 0) 3917 name = "CORE"; 3918 else if (strnlen(name, note->n_namesz) >= note->n_namesz) 3919 name = "<invalid>"; 3920 printf(" %-13s %#010jx", name, (uintmax_t) note->n_descsz); 3921 printf(" %s\n", note_type(name, re->ehdr.e_type, 3922 note->n_type)); 3923 dump_notes_data(re, name, note->n_type, buf, note->n_descsz); 3924 buf += descsz; 3925 } 3926 } 3927 3928 /* 3929 * Symbol versioning sections are the same for 32bit and 64bit 3930 * ELF objects. 3931 */ 3932 #define Elf_Verdef Elf32_Verdef 3933 #define Elf_Verdaux Elf32_Verdaux 3934 #define Elf_Verneed Elf32_Verneed 3935 #define Elf_Vernaux Elf32_Vernaux 3936 3937 #define SAVE_VERSION_NAME(x, n, t) \ 3938 do { \ 3939 while (x >= re->ver_sz) { \ 3940 nv = realloc(re->ver, \ 3941 sizeof(*re->ver) * re->ver_sz * 2); \ 3942 if (nv == NULL) { \ 3943 warn("realloc failed"); \ 3944 free(re->ver); \ 3945 return; \ 3946 } \ 3947 re->ver = nv; \ 3948 for (i = re->ver_sz; i < re->ver_sz * 2; i++) { \ 3949 re->ver[i].name = NULL; \ 3950 re->ver[i].type = 0; \ 3951 } \ 3952 re->ver_sz *= 2; \ 3953 } \ 3954 if (x > 1) { \ 3955 re->ver[x].name = n; \ 3956 re->ver[x].type = t; \ 3957 } \ 3958 } while (0) 3959 3960 3961 static void 3962 dump_verdef(struct readelf *re, int dump) 3963 { 3964 struct section *s; 3965 struct symver *nv; 3966 Elf_Data *d; 3967 Elf_Verdef *vd; 3968 Elf_Verdaux *vda; 3969 uint8_t *buf, *end, *buf2; 3970 const char *name; 3971 int elferr, i, j; 3972 3973 if ((s = re->vd_s) == NULL) 3974 return; 3975 if (s->link >= re->shnum) 3976 return; 3977 3978 if (re->ver == NULL) { 3979 re->ver_sz = 16; 3980 if ((re->ver = calloc(re->ver_sz, sizeof(*re->ver))) == 3981 NULL) { 3982 warn("calloc failed"); 3983 return; 3984 } 3985 re->ver[0].name = "*local*"; 3986 re->ver[1].name = "*global*"; 3987 } 3988 3989 if (dump) 3990 printf("\nVersion definition section (%s):\n", s->name); 3991 (void) elf_errno(); 3992 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 3993 elferr = elf_errno(); 3994 if (elferr != 0) 3995 warnx("elf_getdata failed: %s", elf_errmsg(elferr)); 3996 return; 3997 } 3998 if (d->d_size == 0) 3999 return; 4000 4001 buf = d->d_buf; 4002 end = buf + d->d_size; 4003 while (buf + sizeof(Elf_Verdef) <= end) { 4004 vd = (Elf_Verdef *) (uintptr_t) buf; 4005 if (dump) { 4006 printf(" 0x%4.4lx", (unsigned long) 4007 (buf - (uint8_t *)d->d_buf)); 4008 printf(" vd_version: %u vd_flags: %d" 4009 " vd_ndx: %u vd_cnt: %u", vd->vd_version, 4010 vd->vd_flags, vd->vd_ndx, vd->vd_cnt); 4011 } 4012 buf2 = buf + vd->vd_aux; 4013 j = 0; 4014 while (buf2 + sizeof(Elf_Verdaux) <= end && j < vd->vd_cnt) { 4015 vda = (Elf_Verdaux *) (uintptr_t) buf2; 4016 name = get_string(re, s->link, vda->vda_name); 4017 if (j == 0) { 4018 if (dump) 4019 printf(" vda_name: %s\n", name); 4020 SAVE_VERSION_NAME((int)vd->vd_ndx, name, 1); 4021 } else if (dump) 4022 printf(" 0x%4.4lx parent: %s\n", 4023 (unsigned long) (buf2 - 4024 (uint8_t *)d->d_buf), name); 4025 if (vda->vda_next == 0) 4026 break; 4027 buf2 += vda->vda_next; 4028 j++; 4029 } 4030 if (vd->vd_next == 0) 4031 break; 4032 buf += vd->vd_next; 4033 } 4034 } 4035 4036 static void 4037 dump_verneed(struct readelf *re, int dump) 4038 { 4039 struct section *s; 4040 struct symver *nv; 4041 Elf_Data *d; 4042 Elf_Verneed *vn; 4043 Elf_Vernaux *vna; 4044 uint8_t *buf, *end, *buf2; 4045 const char *name; 4046 int elferr, i, j; 4047 4048 if ((s = re->vn_s) == NULL) 4049 return; 4050 if (s->link >= re->shnum) 4051 return; 4052 4053 if (re->ver == NULL) { 4054 re->ver_sz = 16; 4055 if ((re->ver = calloc(re->ver_sz, sizeof(*re->ver))) == 4056 NULL) { 4057 warn("calloc failed"); 4058 return; 4059 } 4060 re->ver[0].name = "*local*"; 4061 re->ver[1].name = "*global*"; 4062 } 4063 4064 if (dump) 4065 printf("\nVersion needed section (%s):\n", s->name); 4066 (void) elf_errno(); 4067 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 4068 elferr = elf_errno(); 4069 if (elferr != 0) 4070 warnx("elf_getdata failed: %s", elf_errmsg(elferr)); 4071 return; 4072 } 4073 if (d->d_size == 0) 4074 return; 4075 4076 buf = d->d_buf; 4077 end = buf + d->d_size; 4078 while (buf + sizeof(Elf_Verneed) <= end) { 4079 vn = (Elf_Verneed *) (uintptr_t) buf; 4080 if (dump) { 4081 printf(" 0x%4.4lx", (unsigned long) 4082 (buf - (uint8_t *)d->d_buf)); 4083 printf(" vn_version: %u vn_file: %s vn_cnt: %u\n", 4084 vn->vn_version, 4085 get_string(re, s->link, vn->vn_file), 4086 vn->vn_cnt); 4087 } 4088 buf2 = buf + vn->vn_aux; 4089 j = 0; 4090 while (buf2 + sizeof(Elf_Vernaux) <= end && j < vn->vn_cnt) { 4091 vna = (Elf32_Vernaux *) (uintptr_t) buf2; 4092 if (dump) 4093 printf(" 0x%4.4lx", (unsigned long) 4094 (buf2 - (uint8_t *)d->d_buf)); 4095 name = get_string(re, s->link, vna->vna_name); 4096 if (dump) 4097 printf(" vna_name: %s vna_flags: %u" 4098 " vna_other: %u\n", name, 4099 vna->vna_flags, vna->vna_other); 4100 SAVE_VERSION_NAME((int)vna->vna_other, name, 0); 4101 if (vna->vna_next == 0) 4102 break; 4103 buf2 += vna->vna_next; 4104 j++; 4105 } 4106 if (vn->vn_next == 0) 4107 break; 4108 buf += vn->vn_next; 4109 } 4110 } 4111 4112 static void 4113 dump_versym(struct readelf *re) 4114 { 4115 int i; 4116 uint16_t vs; 4117 4118 if (re->vs_s == NULL || re->ver == NULL || re->vs == NULL) 4119 return; 4120 printf("\nVersion symbol section (%s):\n", re->vs_s->name); 4121 for (i = 0; i < re->vs_sz; i++) { 4122 if ((i & 3) == 0) { 4123 if (i > 0) 4124 putchar('\n'); 4125 printf(" %03x:", i); 4126 } 4127 vs = re->vs[i] & VERSYM_VERSION; 4128 if (vs >= re->ver_sz || re->ver[vs].name == NULL) { 4129 warnx("invalid versym version index %u", re->vs[i]); 4130 break; 4131 } 4132 if (re->vs[i] & VERSYM_HIDDEN) 4133 printf(" %3xh %-12s ", vs, 4134 re->ver[re->vs[i] & VERSYM_VERSION].name); 4135 else 4136 printf(" %3x %-12s ", vs, re->ver[re->vs[i]].name); 4137 } 4138 putchar('\n'); 4139 } 4140 4141 static void 4142 dump_ver(struct readelf *re) 4143 { 4144 4145 if (re->vs_s && re->ver && re->vs) 4146 dump_versym(re); 4147 if (re->vd_s) 4148 dump_verdef(re, 1); 4149 if (re->vn_s) 4150 dump_verneed(re, 1); 4151 } 4152 4153 static void 4154 search_ver(struct readelf *re) 4155 { 4156 struct section *s; 4157 Elf_Data *d; 4158 int elferr, i; 4159 4160 for (i = 0; (size_t) i < re->shnum; i++) { 4161 s = &re->sl[i]; 4162 if (s->type == SHT_SUNW_versym) 4163 re->vs_s = s; 4164 if (s->type == SHT_SUNW_verneed) 4165 re->vn_s = s; 4166 if (s->type == SHT_SUNW_verdef) 4167 re->vd_s = s; 4168 } 4169 if (re->vd_s) 4170 dump_verdef(re, 0); 4171 if (re->vn_s) 4172 dump_verneed(re, 0); 4173 if (re->vs_s && re->ver != NULL) { 4174 (void) elf_errno(); 4175 if ((d = elf_getdata(re->vs_s->scn, NULL)) == NULL) { 4176 elferr = elf_errno(); 4177 if (elferr != 0) 4178 warnx("elf_getdata failed: %s", 4179 elf_errmsg(elferr)); 4180 return; 4181 } 4182 if (d->d_size == 0) 4183 return; 4184 re->vs = d->d_buf; 4185 re->vs_sz = d->d_size / sizeof(Elf32_Half); 4186 } 4187 } 4188 4189 #undef Elf_Verdef 4190 #undef Elf_Verdaux 4191 #undef Elf_Verneed 4192 #undef Elf_Vernaux 4193 #undef SAVE_VERSION_NAME 4194 4195 /* 4196 * Elf32_Lib and Elf64_Lib are identical. 4197 */ 4198 #define Elf_Lib Elf32_Lib 4199 4200 static void 4201 dump_liblist(struct readelf *re) 4202 { 4203 struct section *s; 4204 struct tm *t; 4205 time_t ti; 4206 char tbuf[20]; 4207 Elf_Data *d; 4208 Elf_Lib *lib; 4209 int i, j, k, elferr, first, len; 4210 4211 for (i = 0; (size_t) i < re->shnum; i++) { 4212 s = &re->sl[i]; 4213 if (s->type != SHT_GNU_LIBLIST) 4214 continue; 4215 if (s->link >= re->shnum) 4216 continue; 4217 (void) elf_errno(); 4218 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 4219 elferr = elf_errno(); 4220 if (elferr != 0) 4221 warnx("elf_getdata failed: %s", 4222 elf_errmsg(elferr)); 4223 continue; 4224 } 4225 if (d->d_size <= 0) 4226 continue; 4227 lib = d->d_buf; 4228 if (!get_ent_count(s, &len)) 4229 continue; 4230 printf("\nLibrary list section '%s' ", s->name); 4231 printf("contains %d entries:\n", len); 4232 printf("%12s%24s%18s%10s%6s\n", "Library", "Time Stamp", 4233 "Checksum", "Version", "Flags"); 4234 for (j = 0; (uint64_t) j < s->sz / s->entsize; j++) { 4235 printf("%3d: ", j); 4236 printf("%-20.20s ", 4237 get_string(re, s->link, lib->l_name)); 4238 ti = lib->l_time_stamp; 4239 t = gmtime(&ti); 4240 snprintf(tbuf, sizeof(tbuf), "%04d-%02d-%02dT%02d:%02d" 4241 ":%2d", t->tm_year + 1900, t->tm_mon + 1, 4242 t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec); 4243 printf("%-19.19s ", tbuf); 4244 printf("0x%08x ", lib->l_checksum); 4245 printf("%-7d %#x", lib->l_version, lib->l_flags); 4246 if (lib->l_flags != 0) { 4247 first = 1; 4248 putchar('('); 4249 for (k = 0; l_flag[k].name != NULL; k++) { 4250 if ((l_flag[k].value & lib->l_flags) == 4251 0) 4252 continue; 4253 if (!first) 4254 putchar(','); 4255 else 4256 first = 0; 4257 printf("%s", l_flag[k].name); 4258 } 4259 putchar(')'); 4260 } 4261 putchar('\n'); 4262 lib++; 4263 } 4264 } 4265 } 4266 4267 #undef Elf_Lib 4268 4269 static void 4270 dump_section_groups(struct readelf *re) 4271 { 4272 struct section *s; 4273 const char *symname; 4274 Elf_Data *d; 4275 uint32_t *w; 4276 int i, j, elferr; 4277 size_t n; 4278 4279 for (i = 0; (size_t) i < re->shnum; i++) { 4280 s = &re->sl[i]; 4281 if (s->type != SHT_GROUP) 4282 continue; 4283 if (s->link >= re->shnum) 4284 continue; 4285 (void) elf_errno(); 4286 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 4287 elferr = elf_errno(); 4288 if (elferr != 0) 4289 warnx("elf_getdata failed: %s", 4290 elf_errmsg(elferr)); 4291 continue; 4292 } 4293 if (d->d_size <= 0) 4294 continue; 4295 4296 w = d->d_buf; 4297 4298 /* We only support COMDAT section. */ 4299 #ifndef GRP_COMDAT 4300 #define GRP_COMDAT 0x1 4301 #endif 4302 if ((*w++ & GRP_COMDAT) == 0) 4303 return; 4304 4305 if (s->entsize == 0) 4306 s->entsize = 4; 4307 4308 symname = get_symbol_name(re, s->link, s->info); 4309 n = s->sz / s->entsize; 4310 if (n-- < 1) 4311 return; 4312 4313 printf("\nCOMDAT group section [%5d] `%s' [%s] contains %ju" 4314 " sections:\n", i, s->name, symname, (uintmax_t)n); 4315 printf(" %-10.10s %s\n", "[Index]", "Name"); 4316 for (j = 0; (size_t) j < n; j++, w++) { 4317 if (*w >= re->shnum) { 4318 warnx("invalid section index: %u", *w); 4319 continue; 4320 } 4321 printf(" [%5u] %s\n", *w, re->sl[*w].name); 4322 } 4323 } 4324 } 4325 4326 static uint8_t * 4327 dump_unknown_tag(uint64_t tag, uint8_t *p, uint8_t *pe) 4328 { 4329 uint64_t val; 4330 4331 /* 4332 * According to ARM EABI: For tags > 32, even numbered tags have 4333 * a ULEB128 param and odd numbered ones have NUL-terminated 4334 * string param. This rule probably also applies for tags <= 32 4335 * if the object arch is not ARM. 4336 */ 4337 4338 printf(" Tag_unknown_%ju: ", (uintmax_t) tag); 4339 4340 if (tag & 1) { 4341 printf("%s\n", (char *) p); 4342 p += strlen((char *) p) + 1; 4343 } else { 4344 val = _decode_uleb128(&p, pe); 4345 printf("%ju\n", (uintmax_t) val); 4346 } 4347 4348 return (p); 4349 } 4350 4351 static uint8_t * 4352 dump_compatibility_tag(uint8_t *p, uint8_t *pe) 4353 { 4354 uint64_t val; 4355 4356 val = _decode_uleb128(&p, pe); 4357 printf("flag = %ju, vendor = %s\n", (uintmax_t) val, p); 4358 p += strlen((char *) p) + 1; 4359 4360 return (p); 4361 } 4362 4363 static void 4364 dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe) 4365 { 4366 uint64_t tag, val; 4367 size_t i; 4368 int found, desc; 4369 4370 (void) re; 4371 4372 while (p < pe) { 4373 tag = _decode_uleb128(&p, pe); 4374 found = desc = 0; 4375 for (i = 0; i < sizeof(aeabi_tags) / sizeof(aeabi_tags[0]); 4376 i++) { 4377 if (tag == aeabi_tags[i].tag) { 4378 found = 1; 4379 printf(" %s: ", aeabi_tags[i].s_tag); 4380 if (aeabi_tags[i].get_desc) { 4381 desc = 1; 4382 val = _decode_uleb128(&p, pe); 4383 printf("%s\n", 4384 aeabi_tags[i].get_desc(val)); 4385 } 4386 break; 4387 } 4388 if (tag < aeabi_tags[i].tag) 4389 break; 4390 } 4391 if (!found) { 4392 p = dump_unknown_tag(tag, p, pe); 4393 continue; 4394 } 4395 if (desc) 4396 continue; 4397 4398 switch (tag) { 4399 case 4: /* Tag_CPU_raw_name */ 4400 case 5: /* Tag_CPU_name */ 4401 case 67: /* Tag_conformance */ 4402 printf("%s\n", (char *) p); 4403 p += strlen((char *) p) + 1; 4404 break; 4405 case 32: /* Tag_compatibility */ 4406 p = dump_compatibility_tag(p, pe); 4407 break; 4408 case 64: /* Tag_nodefaults */ 4409 /* ignored, written as 0. */ 4410 (void) _decode_uleb128(&p, pe); 4411 printf("True\n"); 4412 break; 4413 case 65: /* Tag_also_compatible_with */ 4414 val = _decode_uleb128(&p, pe); 4415 /* Must be Tag_CPU_arch */ 4416 if (val != 6) { 4417 printf("unknown\n"); 4418 break; 4419 } 4420 val = _decode_uleb128(&p, pe); 4421 printf("%s\n", aeabi_cpu_arch(val)); 4422 /* Skip NUL terminator. */ 4423 p++; 4424 break; 4425 default: 4426 putchar('\n'); 4427 break; 4428 } 4429 } 4430 } 4431 4432 #ifndef Tag_GNU_MIPS_ABI_FP 4433 #define Tag_GNU_MIPS_ABI_FP 4 4434 #endif 4435 4436 static void 4437 dump_mips_attributes(struct readelf *re, uint8_t *p, uint8_t *pe) 4438 { 4439 uint64_t tag, val; 4440 4441 (void) re; 4442 4443 while (p < pe) { 4444 tag = _decode_uleb128(&p, pe); 4445 switch (tag) { 4446 case Tag_GNU_MIPS_ABI_FP: 4447 val = _decode_uleb128(&p, pe); 4448 printf(" Tag_GNU_MIPS_ABI_FP: %s\n", mips_abi_fp(val)); 4449 break; 4450 case 32: /* Tag_compatibility */ 4451 p = dump_compatibility_tag(p, pe); 4452 break; 4453 default: 4454 p = dump_unknown_tag(tag, p, pe); 4455 break; 4456 } 4457 } 4458 } 4459 4460 #ifndef Tag_GNU_Power_ABI_FP 4461 #define Tag_GNU_Power_ABI_FP 4 4462 #endif 4463 4464 #ifndef Tag_GNU_Power_ABI_Vector 4465 #define Tag_GNU_Power_ABI_Vector 8 4466 #endif 4467 4468 static void 4469 dump_ppc_attributes(uint8_t *p, uint8_t *pe) 4470 { 4471 uint64_t tag, val; 4472 4473 while (p < pe) { 4474 tag = _decode_uleb128(&p, pe); 4475 switch (tag) { 4476 case Tag_GNU_Power_ABI_FP: 4477 val = _decode_uleb128(&p, pe); 4478 printf(" Tag_GNU_Power_ABI_FP: %s\n", ppc_abi_fp(val)); 4479 break; 4480 case Tag_GNU_Power_ABI_Vector: 4481 val = _decode_uleb128(&p, pe); 4482 printf(" Tag_GNU_Power_ABI_Vector: %s\n", 4483 ppc_abi_vector(val)); 4484 break; 4485 case 32: /* Tag_compatibility */ 4486 p = dump_compatibility_tag(p, pe); 4487 break; 4488 default: 4489 p = dump_unknown_tag(tag, p, pe); 4490 break; 4491 } 4492 } 4493 } 4494 4495 static void 4496 dump_attributes(struct readelf *re) 4497 { 4498 struct section *s; 4499 Elf_Data *d; 4500 uint8_t *p, *pe, *sp; 4501 size_t len, seclen, nlen, sublen; 4502 uint64_t val; 4503 int tag, i, elferr; 4504 4505 for (i = 0; (size_t) i < re->shnum; i++) { 4506 s = &re->sl[i]; 4507 if (s->type != SHT_GNU_ATTRIBUTES && 4508 (re->ehdr.e_machine != EM_ARM || s->type != SHT_LOPROC + 3)) 4509 continue; 4510 (void) elf_errno(); 4511 if ((d = elf_rawdata(s->scn, NULL)) == NULL) { 4512 elferr = elf_errno(); 4513 if (elferr != 0) 4514 warnx("elf_rawdata failed: %s", 4515 elf_errmsg(elferr)); 4516 continue; 4517 } 4518 if (d->d_size <= 0) 4519 continue; 4520 p = d->d_buf; 4521 pe = p + d->d_size; 4522 if (*p != 'A') { 4523 printf("Unknown Attribute Section Format: %c\n", 4524 (char) *p); 4525 continue; 4526 } 4527 len = d->d_size - 1; 4528 p++; 4529 while (len > 0) { 4530 if (len < 4) { 4531 warnx("truncated attribute section length"); 4532 return; 4533 } 4534 seclen = re->dw_decode(&p, 4); 4535 if (seclen > len) { 4536 warnx("invalid attribute section length"); 4537 return; 4538 } 4539 len -= seclen; 4540 nlen = strlen((char *) p) + 1; 4541 if (nlen + 4 > seclen) { 4542 warnx("invalid attribute section name"); 4543 return; 4544 } 4545 printf("Attribute Section: %s\n", (char *) p); 4546 p += nlen; 4547 seclen -= nlen + 4; 4548 while (seclen > 0) { 4549 sp = p; 4550 tag = *p++; 4551 sublen = re->dw_decode(&p, 4); 4552 if (sublen > seclen) { 4553 warnx("invalid attribute sub-section" 4554 " length"); 4555 return; 4556 } 4557 seclen -= sublen; 4558 printf("%s", top_tag(tag)); 4559 if (tag == 2 || tag == 3) { 4560 putchar(':'); 4561 for (;;) { 4562 val = _decode_uleb128(&p, pe); 4563 if (val == 0) 4564 break; 4565 printf(" %ju", (uintmax_t) val); 4566 } 4567 } 4568 putchar('\n'); 4569 if (re->ehdr.e_machine == EM_ARM && 4570 s->type == SHT_LOPROC + 3) 4571 dump_arm_attributes(re, p, sp + sublen); 4572 else if (re->ehdr.e_machine == EM_MIPS || 4573 re->ehdr.e_machine == EM_MIPS_RS3_LE) 4574 dump_mips_attributes(re, p, 4575 sp + sublen); 4576 else if (re->ehdr.e_machine == EM_PPC) 4577 dump_ppc_attributes(p, sp + sublen); 4578 p = sp + sublen; 4579 } 4580 } 4581 } 4582 } 4583 4584 static void 4585 dump_mips_specific_info(struct readelf *re) 4586 { 4587 struct section *s; 4588 int i; 4589 4590 s = NULL; 4591 for (i = 0; (size_t) i < re->shnum; i++) { 4592 s = &re->sl[i]; 4593 if (s->name != NULL && (!strcmp(s->name, ".MIPS.options") || 4594 (s->type == SHT_MIPS_OPTIONS))) { 4595 dump_mips_options(re, s); 4596 } 4597 } 4598 4599 if (s->name != NULL && (!strcmp(s->name, ".MIPS.abiflags") || 4600 (s->type == SHT_MIPS_ABIFLAGS))) 4601 dump_mips_abiflags(re, s); 4602 4603 /* 4604 * Dump .reginfo if present (although it will be ignored by an OS if a 4605 * .MIPS.options section is present, according to SGI mips64 spec). 4606 */ 4607 for (i = 0; (size_t) i < re->shnum; i++) { 4608 s = &re->sl[i]; 4609 if (s->name != NULL && (!strcmp(s->name, ".reginfo") || 4610 (s->type == SHT_MIPS_REGINFO))) 4611 dump_mips_reginfo(re, s); 4612 } 4613 } 4614 4615 static void 4616 dump_mips_abiflags(struct readelf *re, struct section *s) 4617 { 4618 Elf_Data *d; 4619 uint8_t *p; 4620 int elferr; 4621 uint32_t isa_ext, ases, flags1, flags2; 4622 uint16_t version; 4623 uint8_t isa_level, isa_rev, gpr_size, cpr1_size, cpr2_size, fp_abi; 4624 4625 if ((d = elf_rawdata(s->scn, NULL)) == NULL) { 4626 elferr = elf_errno(); 4627 if (elferr != 0) 4628 warnx("elf_rawdata failed: %s", 4629 elf_errmsg(elferr)); 4630 return; 4631 } 4632 if (d->d_size != 24) { 4633 warnx("invalid MIPS abiflags section size"); 4634 return; 4635 } 4636 4637 p = d->d_buf; 4638 version = re->dw_decode(&p, 2); 4639 printf("MIPS ABI Flags Version: %u", version); 4640 if (version != 0) { 4641 printf(" (unknown)\n\n"); 4642 return; 4643 } 4644 printf("\n\n"); 4645 4646 isa_level = re->dw_decode(&p, 1); 4647 isa_rev = re->dw_decode(&p, 1); 4648 gpr_size = re->dw_decode(&p, 1); 4649 cpr1_size = re->dw_decode(&p, 1); 4650 cpr2_size = re->dw_decode(&p, 1); 4651 fp_abi = re->dw_decode(&p, 1); 4652 isa_ext = re->dw_decode(&p, 4); 4653 ases = re->dw_decode(&p, 4); 4654 flags1 = re->dw_decode(&p, 4); 4655 flags2 = re->dw_decode(&p, 4); 4656 4657 printf("ISA: "); 4658 if (isa_rev <= 1) 4659 printf("MIPS%u\n", isa_level); 4660 else 4661 printf("MIPS%ur%u\n", isa_level, isa_rev); 4662 printf("GPR size: %d\n", get_mips_register_size(gpr_size)); 4663 printf("CPR1 size: %d\n", get_mips_register_size(cpr1_size)); 4664 printf("CPR2 size: %d\n", get_mips_register_size(cpr2_size)); 4665 printf("FP ABI: "); 4666 switch (fp_abi) { 4667 case 3: 4668 printf("Soft float"); 4669 break; 4670 default: 4671 printf("%u", fp_abi); 4672 break; 4673 } 4674 printf("\nISA Extension: %u\n", isa_ext); 4675 printf("ASEs: %u\n", ases); 4676 printf("FLAGS 1: %08x\n", flags1); 4677 printf("FLAGS 2: %08x\n", flags2); 4678 } 4679 4680 static int 4681 get_mips_register_size(uint8_t flag) 4682 { 4683 switch (flag) { 4684 case 0: return 0; 4685 case 1: return 32; 4686 case 2: return 64; 4687 case 3: return 128; 4688 default: return -1; 4689 } 4690 } 4691 static void 4692 dump_mips_reginfo(struct readelf *re, struct section *s) 4693 { 4694 Elf_Data *d; 4695 int elferr, len; 4696 4697 (void) elf_errno(); 4698 if ((d = elf_rawdata(s->scn, NULL)) == NULL) { 4699 elferr = elf_errno(); 4700 if (elferr != 0) 4701 warnx("elf_rawdata failed: %s", 4702 elf_errmsg(elferr)); 4703 return; 4704 } 4705 if (d->d_size <= 0) 4706 return; 4707 if (!get_ent_count(s, &len)) 4708 return; 4709 4710 printf("\nSection '%s' contains %d entries:\n", s->name, len); 4711 dump_mips_odk_reginfo(re, d->d_buf, d->d_size); 4712 } 4713 4714 static void 4715 dump_mips_options(struct readelf *re, struct section *s) 4716 { 4717 Elf_Data *d; 4718 uint32_t info; 4719 uint16_t sndx; 4720 uint8_t *p, *pe; 4721 uint8_t kind, size; 4722 int elferr; 4723 4724 (void) elf_errno(); 4725 if ((d = elf_rawdata(s->scn, NULL)) == NULL) { 4726 elferr = elf_errno(); 4727 if (elferr != 0) 4728 warnx("elf_rawdata failed: %s", 4729 elf_errmsg(elferr)); 4730 return; 4731 } 4732 if (d->d_size == 0) 4733 return; 4734 4735 printf("\nSection %s contains:\n", s->name); 4736 p = d->d_buf; 4737 pe = p + d->d_size; 4738 while (p < pe) { 4739 if (pe - p < 8) { 4740 warnx("Truncated MIPS option header"); 4741 return; 4742 } 4743 kind = re->dw_decode(&p, 1); 4744 size = re->dw_decode(&p, 1); 4745 sndx = re->dw_decode(&p, 2); 4746 info = re->dw_decode(&p, 4); 4747 if (size < 8 || size - 8 > pe - p) { 4748 warnx("Malformed MIPS option header"); 4749 return; 4750 } 4751 size -= 8; 4752 switch (kind) { 4753 case ODK_REGINFO: 4754 dump_mips_odk_reginfo(re, p, size); 4755 break; 4756 case ODK_EXCEPTIONS: 4757 printf(" EXCEPTIONS FPU_MIN: %#x\n", 4758 info & OEX_FPU_MIN); 4759 printf("%11.11s FPU_MAX: %#x\n", "", 4760 info & OEX_FPU_MAX); 4761 dump_mips_option_flags("", mips_exceptions_option, 4762 info); 4763 break; 4764 case ODK_PAD: 4765 printf(" %-10.10s section: %ju\n", "OPAD", 4766 (uintmax_t) sndx); 4767 dump_mips_option_flags("", mips_pad_option, info); 4768 break; 4769 case ODK_HWPATCH: 4770 dump_mips_option_flags("HWPATCH", mips_hwpatch_option, 4771 info); 4772 break; 4773 case ODK_HWAND: 4774 dump_mips_option_flags("HWAND", mips_hwa_option, info); 4775 break; 4776 case ODK_HWOR: 4777 dump_mips_option_flags("HWOR", mips_hwo_option, info); 4778 break; 4779 case ODK_FILL: 4780 printf(" %-10.10s %#jx\n", "FILL", (uintmax_t) info); 4781 break; 4782 case ODK_TAGS: 4783 printf(" %-10.10s\n", "TAGS"); 4784 break; 4785 case ODK_GP_GROUP: 4786 printf(" %-10.10s GP group number: %#x\n", "GP_GROUP", 4787 info & 0xFFFF); 4788 if (info & 0x10000) 4789 printf(" %-10.10s GP group is " 4790 "self-contained\n", ""); 4791 break; 4792 case ODK_IDENT: 4793 printf(" %-10.10s default GP group number: %#x\n", 4794 "IDENT", info & 0xFFFF); 4795 if (info & 0x10000) 4796 printf(" %-10.10s default GP group is " 4797 "self-contained\n", ""); 4798 break; 4799 case ODK_PAGESIZE: 4800 printf(" %-10.10s\n", "PAGESIZE"); 4801 break; 4802 default: 4803 break; 4804 } 4805 p += size; 4806 } 4807 } 4808 4809 static void 4810 dump_mips_option_flags(const char *name, struct mips_option *opt, uint64_t info) 4811 { 4812 int first; 4813 4814 first = 1; 4815 for (; opt->desc != NULL; opt++) { 4816 if (info & opt->flag) { 4817 printf(" %-10.10s %s\n", first ? name : "", 4818 opt->desc); 4819 first = 0; 4820 } 4821 } 4822 } 4823 4824 static void 4825 dump_mips_odk_reginfo(struct readelf *re, uint8_t *p, size_t sz) 4826 { 4827 uint32_t ri_gprmask; 4828 uint32_t ri_cprmask[4]; 4829 uint64_t ri_gp_value; 4830 uint8_t *pe; 4831 int i; 4832 4833 pe = p + sz; 4834 while (p < pe) { 4835 ri_gprmask = re->dw_decode(&p, 4); 4836 /* Skip ri_pad padding field for mips64. */ 4837 if (re->ec == ELFCLASS64) 4838 re->dw_decode(&p, 4); 4839 for (i = 0; i < 4; i++) 4840 ri_cprmask[i] = re->dw_decode(&p, 4); 4841 if (re->ec == ELFCLASS32) 4842 ri_gp_value = re->dw_decode(&p, 4); 4843 else 4844 ri_gp_value = re->dw_decode(&p, 8); 4845 printf(" %s ", option_kind(ODK_REGINFO)); 4846 printf("ri_gprmask: 0x%08jx\n", (uintmax_t) ri_gprmask); 4847 for (i = 0; i < 4; i++) 4848 printf("%11.11s ri_cprmask[%d]: 0x%08jx\n", "", i, 4849 (uintmax_t) ri_cprmask[i]); 4850 printf("%12.12s", ""); 4851 printf("ri_gp_value: %#jx\n", (uintmax_t) ri_gp_value); 4852 } 4853 } 4854 4855 static void 4856 dump_arch_specific_info(struct readelf *re) 4857 { 4858 4859 dump_liblist(re); 4860 dump_attributes(re); 4861 4862 switch (re->ehdr.e_machine) { 4863 case EM_MIPS: 4864 case EM_MIPS_RS3_LE: 4865 dump_mips_specific_info(re); 4866 default: 4867 break; 4868 } 4869 } 4870 4871 static const char * 4872 dwarf_regname(struct readelf *re, unsigned int num) 4873 { 4874 static char rx[32]; 4875 const char *rn; 4876 4877 if ((rn = dwarf_reg(re->ehdr.e_machine, num)) != NULL) 4878 return (rn); 4879 4880 snprintf(rx, sizeof(rx), "r%u", num); 4881 4882 return (rx); 4883 } 4884 4885 static void 4886 dump_dwarf_line(struct readelf *re) 4887 { 4888 struct section *s; 4889 Dwarf_Die die; 4890 Dwarf_Error de; 4891 Dwarf_Half tag, version, pointer_size; 4892 Dwarf_Unsigned offset, endoff, length, hdrlen, dirndx, mtime, fsize; 4893 Dwarf_Small minlen, defstmt, lrange, opbase, oplen; 4894 Elf_Data *d; 4895 char *pn; 4896 uint64_t address, file, line, column, isa, opsize, udelta; 4897 int64_t sdelta; 4898 uint8_t *p, *pe; 4899 int8_t lbase; 4900 int i, is_stmt, dwarf_size, elferr, ret; 4901 4902 printf("\nDump of debug contents of section .debug_line:\n"); 4903 4904 s = NULL; 4905 for (i = 0; (size_t) i < re->shnum; i++) { 4906 s = &re->sl[i]; 4907 if (s->name != NULL && !strcmp(s->name, ".debug_line")) 4908 break; 4909 } 4910 if ((size_t) i >= re->shnum) 4911 return; 4912 4913 (void) elf_errno(); 4914 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 4915 elferr = elf_errno(); 4916 if (elferr != 0) 4917 warnx("elf_getdata failed: %s", elf_errmsg(-1)); 4918 return; 4919 } 4920 if (d->d_size <= 0) 4921 return; 4922 4923 while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL, 4924 NULL, &de)) == DW_DLV_OK) { 4925 die = NULL; 4926 while (dwarf_siblingof(re->dbg, die, &die, &de) == DW_DLV_OK) { 4927 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { 4928 warnx("dwarf_tag failed: %s", 4929 dwarf_errmsg(de)); 4930 return; 4931 } 4932 /* XXX: What about DW_TAG_partial_unit? */ 4933 if (tag == DW_TAG_compile_unit) 4934 break; 4935 } 4936 if (die == NULL) { 4937 warnx("could not find DW_TAG_compile_unit die"); 4938 return; 4939 } 4940 if (dwarf_attrval_unsigned(die, DW_AT_stmt_list, &offset, 4941 &de) != DW_DLV_OK) 4942 continue; 4943 4944 length = re->dw_read(d, &offset, 4); 4945 if (length == 0xffffffff) { 4946 dwarf_size = 8; 4947 length = re->dw_read(d, &offset, 8); 4948 } else 4949 dwarf_size = 4; 4950 4951 if (length > d->d_size - offset) { 4952 warnx("invalid .dwarf_line section"); 4953 continue; 4954 } 4955 4956 endoff = offset + length; 4957 pe = (uint8_t *) d->d_buf + endoff; 4958 version = re->dw_read(d, &offset, 2); 4959 hdrlen = re->dw_read(d, &offset, dwarf_size); 4960 minlen = re->dw_read(d, &offset, 1); 4961 defstmt = re->dw_read(d, &offset, 1); 4962 lbase = re->dw_read(d, &offset, 1); 4963 lrange = re->dw_read(d, &offset, 1); 4964 opbase = re->dw_read(d, &offset, 1); 4965 4966 printf("\n"); 4967 printf(" Length:\t\t\t%ju\n", (uintmax_t) length); 4968 printf(" DWARF version:\t\t%u\n", version); 4969 printf(" Prologue Length:\t\t%ju\n", (uintmax_t) hdrlen); 4970 printf(" Minimum Instruction Length:\t%u\n", minlen); 4971 printf(" Initial value of 'is_stmt':\t%u\n", defstmt); 4972 printf(" Line Base:\t\t\t%d\n", lbase); 4973 printf(" Line Range:\t\t\t%u\n", lrange); 4974 printf(" Opcode Base:\t\t\t%u\n", opbase); 4975 (void) dwarf_get_address_size(re->dbg, &pointer_size, &de); 4976 printf(" (Pointer size:\t\t%u)\n", pointer_size); 4977 4978 printf("\n"); 4979 printf(" Opcodes:\n"); 4980 for (i = 1; i < opbase; i++) { 4981 oplen = re->dw_read(d, &offset, 1); 4982 printf(" Opcode %d has %u args\n", i, oplen); 4983 } 4984 4985 printf("\n"); 4986 printf(" The Directory Table:\n"); 4987 p = (uint8_t *) d->d_buf + offset; 4988 while (*p != '\0') { 4989 printf(" %s\n", (char *) p); 4990 p += strlen((char *) p) + 1; 4991 } 4992 4993 p++; 4994 printf("\n"); 4995 printf(" The File Name Table:\n"); 4996 printf(" Entry\tDir\tTime\tSize\tName\n"); 4997 i = 0; 4998 while (*p != '\0') { 4999 i++; 5000 pn = (char *) p; 5001 p += strlen(pn) + 1; 5002 dirndx = _decode_uleb128(&p, pe); 5003 mtime = _decode_uleb128(&p, pe); 5004 fsize = _decode_uleb128(&p, pe); 5005 printf(" %d\t%ju\t%ju\t%ju\t%s\n", i, 5006 (uintmax_t) dirndx, (uintmax_t) mtime, 5007 (uintmax_t) fsize, pn); 5008 } 5009 5010 #define RESET_REGISTERS \ 5011 do { \ 5012 address = 0; \ 5013 file = 1; \ 5014 line = 1; \ 5015 column = 0; \ 5016 is_stmt = defstmt; \ 5017 } while(0) 5018 5019 #define LINE(x) (lbase + (((x) - opbase) % lrange)) 5020 #define ADDRESS(x) ((((x) - opbase) / lrange) * minlen) 5021 5022 p++; 5023 printf("\n"); 5024 printf(" Line Number Statements:\n"); 5025 5026 RESET_REGISTERS; 5027 5028 while (p < pe) { 5029 5030 if (*p == 0) { 5031 /* 5032 * Extended Opcodes. 5033 */ 5034 p++; 5035 opsize = _decode_uleb128(&p, pe); 5036 printf(" Extended opcode %u: ", *p); 5037 switch (*p) { 5038 case DW_LNE_end_sequence: 5039 p++; 5040 RESET_REGISTERS; 5041 printf("End of Sequence\n"); 5042 break; 5043 case DW_LNE_set_address: 5044 p++; 5045 address = re->dw_decode(&p, 5046 pointer_size); 5047 printf("set Address to %#jx\n", 5048 (uintmax_t) address); 5049 break; 5050 case DW_LNE_define_file: 5051 p++; 5052 pn = (char *) p; 5053 p += strlen(pn) + 1; 5054 dirndx = _decode_uleb128(&p, pe); 5055 mtime = _decode_uleb128(&p, pe); 5056 fsize = _decode_uleb128(&p, pe); 5057 printf("define new file: %s\n", pn); 5058 break; 5059 default: 5060 /* Unrecognized extened opcodes. */ 5061 p += opsize; 5062 printf("unknown opcode\n"); 5063 } 5064 } else if (*p > 0 && *p < opbase) { 5065 /* 5066 * Standard Opcodes. 5067 */ 5068 switch(*p++) { 5069 case DW_LNS_copy: 5070 printf(" Copy\n"); 5071 break; 5072 case DW_LNS_advance_pc: 5073 udelta = _decode_uleb128(&p, pe) * 5074 minlen; 5075 address += udelta; 5076 printf(" Advance PC by %ju to %#jx\n", 5077 (uintmax_t) udelta, 5078 (uintmax_t) address); 5079 break; 5080 case DW_LNS_advance_line: 5081 sdelta = _decode_sleb128(&p, pe); 5082 line += sdelta; 5083 printf(" Advance Line by %jd to %ju\n", 5084 (intmax_t) sdelta, 5085 (uintmax_t) line); 5086 break; 5087 case DW_LNS_set_file: 5088 file = _decode_uleb128(&p, pe); 5089 printf(" Set File to %ju\n", 5090 (uintmax_t) file); 5091 break; 5092 case DW_LNS_set_column: 5093 column = _decode_uleb128(&p, pe); 5094 printf(" Set Column to %ju\n", 5095 (uintmax_t) column); 5096 break; 5097 case DW_LNS_negate_stmt: 5098 is_stmt = !is_stmt; 5099 printf(" Set is_stmt to %d\n", is_stmt); 5100 break; 5101 case DW_LNS_set_basic_block: 5102 printf(" Set basic block flag\n"); 5103 break; 5104 case DW_LNS_const_add_pc: 5105 address += ADDRESS(255); 5106 printf(" Advance PC by constant %ju" 5107 " to %#jx\n", 5108 (uintmax_t) ADDRESS(255), 5109 (uintmax_t) address); 5110 break; 5111 case DW_LNS_fixed_advance_pc: 5112 udelta = re->dw_decode(&p, 2); 5113 address += udelta; 5114 printf(" Advance PC by fixed value " 5115 "%ju to %#jx\n", 5116 (uintmax_t) udelta, 5117 (uintmax_t) address); 5118 break; 5119 case DW_LNS_set_prologue_end: 5120 printf(" Set prologue end flag\n"); 5121 break; 5122 case DW_LNS_set_epilogue_begin: 5123 printf(" Set epilogue begin flag\n"); 5124 break; 5125 case DW_LNS_set_isa: 5126 isa = _decode_uleb128(&p, pe); 5127 printf(" Set isa to %ju\n", 5128 (uintmax_t) isa); 5129 break; 5130 default: 5131 /* Unrecognized extended opcodes. */ 5132 printf(" Unknown extended opcode %u\n", 5133 *(p - 1)); 5134 break; 5135 } 5136 5137 } else { 5138 /* 5139 * Special Opcodes. 5140 */ 5141 line += LINE(*p); 5142 address += ADDRESS(*p); 5143 printf(" Special opcode %u: advance Address " 5144 "by %ju to %#jx and Line by %jd to %ju\n", 5145 *p - opbase, (uintmax_t) ADDRESS(*p), 5146 (uintmax_t) address, (intmax_t) LINE(*p), 5147 (uintmax_t) line); 5148 p++; 5149 } 5150 5151 5152 } 5153 } 5154 if (ret == DW_DLV_ERROR) 5155 warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de)); 5156 5157 #undef RESET_REGISTERS 5158 #undef LINE 5159 #undef ADDRESS 5160 } 5161 5162 static void 5163 dump_dwarf_line_decoded(struct readelf *re) 5164 { 5165 Dwarf_Die die; 5166 Dwarf_Line *linebuf, ln; 5167 Dwarf_Addr lineaddr; 5168 Dwarf_Signed linecount, srccount; 5169 Dwarf_Unsigned lineno, fn; 5170 Dwarf_Error de; 5171 const char *dir, *file; 5172 char **srcfiles; 5173 int i, ret; 5174 5175 printf("Decoded dump of debug contents of section .debug_line:\n\n"); 5176 while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL, 5177 NULL, &de)) == DW_DLV_OK) { 5178 if (dwarf_siblingof(re->dbg, NULL, &die, &de) != DW_DLV_OK) 5179 continue; 5180 if (dwarf_attrval_string(die, DW_AT_name, &file, &de) != 5181 DW_DLV_OK) 5182 file = NULL; 5183 if (dwarf_attrval_string(die, DW_AT_comp_dir, &dir, &de) != 5184 DW_DLV_OK) 5185 dir = NULL; 5186 printf("CU: "); 5187 if (dir && file && file[0] != '/') 5188 printf("%s/", dir); 5189 if (file) 5190 printf("%s", file); 5191 putchar('\n'); 5192 printf("%-37s %11s %s\n", "Filename", "Line Number", 5193 "Starting Address"); 5194 if (dwarf_srclines(die, &linebuf, &linecount, &de) != DW_DLV_OK) 5195 continue; 5196 if (dwarf_srcfiles(die, &srcfiles, &srccount, &de) != DW_DLV_OK) 5197 continue; 5198 for (i = 0; i < linecount; i++) { 5199 ln = linebuf[i]; 5200 if (dwarf_line_srcfileno(ln, &fn, &de) != DW_DLV_OK) 5201 continue; 5202 if (dwarf_lineno(ln, &lineno, &de) != DW_DLV_OK) 5203 continue; 5204 if (dwarf_lineaddr(ln, &lineaddr, &de) != DW_DLV_OK) 5205 continue; 5206 printf("%-37s %11ju %#18jx\n", 5207 basename(srcfiles[fn - 1]), (uintmax_t) lineno, 5208 (uintmax_t) lineaddr); 5209 } 5210 putchar('\n'); 5211 } 5212 } 5213 5214 static void 5215 dump_dwarf_die(struct readelf *re, Dwarf_Die die, int level) 5216 { 5217 Dwarf_Attribute *attr_list; 5218 Dwarf_Die ret_die; 5219 Dwarf_Off dieoff, cuoff, culen, attroff; 5220 Dwarf_Unsigned ate, lang, v_udata, v_sig; 5221 Dwarf_Signed attr_count, v_sdata; 5222 Dwarf_Off v_off; 5223 Dwarf_Addr v_addr; 5224 Dwarf_Half tag, attr, form; 5225 Dwarf_Block *v_block; 5226 Dwarf_Bool v_bool, is_info; 5227 Dwarf_Sig8 v_sig8; 5228 Dwarf_Error de; 5229 Dwarf_Ptr v_expr; 5230 const char *tag_str, *attr_str, *ate_str, *lang_str; 5231 char unk_tag[32], unk_attr[32]; 5232 char *v_str; 5233 uint8_t *b, *p; 5234 int i, j, abc, ret; 5235 5236 if (dwarf_dieoffset(die, &dieoff, &de) != DW_DLV_OK) { 5237 warnx("dwarf_dieoffset failed: %s", dwarf_errmsg(de)); 5238 goto cont_search; 5239 } 5240 5241 printf(" <%d><%jx>: ", level, (uintmax_t) dieoff); 5242 5243 if (dwarf_die_CU_offset_range(die, &cuoff, &culen, &de) != DW_DLV_OK) { 5244 warnx("dwarf_die_CU_offset_range failed: %s", 5245 dwarf_errmsg(de)); 5246 cuoff = 0; 5247 } 5248 5249 abc = dwarf_die_abbrev_code(die); 5250 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { 5251 warnx("dwarf_tag failed: %s", dwarf_errmsg(de)); 5252 goto cont_search; 5253 } 5254 if (dwarf_get_TAG_name(tag, &tag_str) != DW_DLV_OK) { 5255 snprintf(unk_tag, sizeof(unk_tag), "[Unknown Tag: %#x]", tag); 5256 tag_str = unk_tag; 5257 } 5258 5259 printf("Abbrev Number: %d (%s)\n", abc, tag_str); 5260 5261 if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) != 5262 DW_DLV_OK) { 5263 if (ret == DW_DLV_ERROR) 5264 warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de)); 5265 goto cont_search; 5266 } 5267 5268 for (i = 0; i < attr_count; i++) { 5269 if (dwarf_whatform(attr_list[i], &form, &de) != DW_DLV_OK) { 5270 warnx("dwarf_whatform failed: %s", dwarf_errmsg(de)); 5271 continue; 5272 } 5273 if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) { 5274 warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de)); 5275 continue; 5276 } 5277 if (dwarf_get_AT_name(attr, &attr_str) != DW_DLV_OK) { 5278 snprintf(unk_attr, sizeof(unk_attr), 5279 "[Unknown AT: %#x]", attr); 5280 attr_str = unk_attr; 5281 } 5282 if (dwarf_attroffset(attr_list[i], &attroff, &de) != 5283 DW_DLV_OK) { 5284 warnx("dwarf_attroffset failed: %s", dwarf_errmsg(de)); 5285 attroff = 0; 5286 } 5287 printf(" <%jx> %-18s: ", (uintmax_t) attroff, attr_str); 5288 switch (form) { 5289 case DW_FORM_ref_addr: 5290 case DW_FORM_sec_offset: 5291 if (dwarf_global_formref(attr_list[i], &v_off, &de) != 5292 DW_DLV_OK) { 5293 warnx("dwarf_global_formref failed: %s", 5294 dwarf_errmsg(de)); 5295 continue; 5296 } 5297 if (form == DW_FORM_ref_addr) 5298 printf("<0x%jx>", (uintmax_t) v_off); 5299 else 5300 printf("0x%jx", (uintmax_t) v_off); 5301 break; 5302 5303 case DW_FORM_ref1: 5304 case DW_FORM_ref2: 5305 case DW_FORM_ref4: 5306 case DW_FORM_ref8: 5307 case DW_FORM_ref_udata: 5308 if (dwarf_formref(attr_list[i], &v_off, &de) != 5309 DW_DLV_OK) { 5310 warnx("dwarf_formref failed: %s", 5311 dwarf_errmsg(de)); 5312 continue; 5313 } 5314 v_off += cuoff; 5315 printf("<0x%jx>", (uintmax_t) v_off); 5316 break; 5317 5318 case DW_FORM_addr: 5319 if (dwarf_formaddr(attr_list[i], &v_addr, &de) != 5320 DW_DLV_OK) { 5321 warnx("dwarf_formaddr failed: %s", 5322 dwarf_errmsg(de)); 5323 continue; 5324 } 5325 printf("%#jx", (uintmax_t) v_addr); 5326 break; 5327 5328 case DW_FORM_data1: 5329 case DW_FORM_data2: 5330 case DW_FORM_data4: 5331 case DW_FORM_data8: 5332 case DW_FORM_udata: 5333 if (dwarf_formudata(attr_list[i], &v_udata, &de) != 5334 DW_DLV_OK) { 5335 warnx("dwarf_formudata failed: %s", 5336 dwarf_errmsg(de)); 5337 continue; 5338 } 5339 if (attr == DW_AT_high_pc) 5340 printf("0x%jx", (uintmax_t) v_udata); 5341 else 5342 printf("%ju", (uintmax_t) v_udata); 5343 break; 5344 5345 case DW_FORM_sdata: 5346 if (dwarf_formsdata(attr_list[i], &v_sdata, &de) != 5347 DW_DLV_OK) { 5348 warnx("dwarf_formudata failed: %s", 5349 dwarf_errmsg(de)); 5350 continue; 5351 } 5352 printf("%jd", (intmax_t) v_sdata); 5353 break; 5354 5355 case DW_FORM_flag: 5356 if (dwarf_formflag(attr_list[i], &v_bool, &de) != 5357 DW_DLV_OK) { 5358 warnx("dwarf_formflag failed: %s", 5359 dwarf_errmsg(de)); 5360 continue; 5361 } 5362 printf("%jd", (intmax_t) v_bool); 5363 break; 5364 5365 case DW_FORM_flag_present: 5366 putchar('1'); 5367 break; 5368 5369 case DW_FORM_string: 5370 case DW_FORM_strp: 5371 if (dwarf_formstring(attr_list[i], &v_str, &de) != 5372 DW_DLV_OK) { 5373 warnx("dwarf_formstring failed: %s", 5374 dwarf_errmsg(de)); 5375 continue; 5376 } 5377 if (form == DW_FORM_string) 5378 printf("%s", v_str); 5379 else 5380 printf("(indirect string) %s", v_str); 5381 break; 5382 5383 case DW_FORM_block: 5384 case DW_FORM_block1: 5385 case DW_FORM_block2: 5386 case DW_FORM_block4: 5387 if (dwarf_formblock(attr_list[i], &v_block, &de) != 5388 DW_DLV_OK) { 5389 warnx("dwarf_formblock failed: %s", 5390 dwarf_errmsg(de)); 5391 continue; 5392 } 5393 printf("%ju byte block:", (uintmax_t) v_block->bl_len); 5394 b = v_block->bl_data; 5395 for (j = 0; (Dwarf_Unsigned) j < v_block->bl_len; j++) 5396 printf(" %x", b[j]); 5397 printf("\t("); 5398 dump_dwarf_block(re, v_block->bl_data, v_block->bl_len); 5399 putchar(')'); 5400 break; 5401 5402 case DW_FORM_exprloc: 5403 if (dwarf_formexprloc(attr_list[i], &v_udata, &v_expr, 5404 &de) != DW_DLV_OK) { 5405 warnx("dwarf_formexprloc failed: %s", 5406 dwarf_errmsg(de)); 5407 continue; 5408 } 5409 printf("%ju byte block:", (uintmax_t) v_udata); 5410 b = v_expr; 5411 for (j = 0; (Dwarf_Unsigned) j < v_udata; j++) 5412 printf(" %x", b[j]); 5413 printf("\t("); 5414 dump_dwarf_block(re, v_expr, v_udata); 5415 putchar(')'); 5416 break; 5417 5418 case DW_FORM_ref_sig8: 5419 if (dwarf_formsig8(attr_list[i], &v_sig8, &de) != 5420 DW_DLV_OK) { 5421 warnx("dwarf_formsig8 failed: %s", 5422 dwarf_errmsg(de)); 5423 continue; 5424 } 5425 p = (uint8_t *)(uintptr_t) &v_sig8.signature[0]; 5426 v_sig = re->dw_decode(&p, 8); 5427 printf("signature: 0x%jx", (uintmax_t) v_sig); 5428 } 5429 switch (attr) { 5430 case DW_AT_encoding: 5431 if (dwarf_attrval_unsigned(die, attr, &ate, &de) != 5432 DW_DLV_OK) 5433 break; 5434 if (dwarf_get_ATE_name(ate, &ate_str) != DW_DLV_OK) 5435 ate_str = "DW_ATE_UNKNOWN"; 5436 printf("\t(%s)", &ate_str[strlen("DW_ATE_")]); 5437 break; 5438 5439 case DW_AT_language: 5440 if (dwarf_attrval_unsigned(die, attr, &lang, &de) != 5441 DW_DLV_OK) 5442 break; 5443 if (dwarf_get_LANG_name(lang, &lang_str) != DW_DLV_OK) 5444 break; 5445 printf("\t(%s)", &lang_str[strlen("DW_LANG_")]); 5446 break; 5447 5448 case DW_AT_location: 5449 case DW_AT_string_length: 5450 case DW_AT_return_addr: 5451 case DW_AT_data_member_location: 5452 case DW_AT_frame_base: 5453 case DW_AT_segment: 5454 case DW_AT_static_link: 5455 case DW_AT_use_location: 5456 case DW_AT_vtable_elem_location: 5457 switch (form) { 5458 case DW_FORM_data4: 5459 case DW_FORM_data8: 5460 case DW_FORM_sec_offset: 5461 printf("\t(location list)"); 5462 break; 5463 default: 5464 break; 5465 } 5466 5467 default: 5468 break; 5469 } 5470 putchar('\n'); 5471 } 5472 5473 5474 cont_search: 5475 /* Search children. */ 5476 ret = dwarf_child(die, &ret_die, &de); 5477 if (ret == DW_DLV_ERROR) 5478 warnx("dwarf_child: %s", dwarf_errmsg(de)); 5479 else if (ret == DW_DLV_OK) 5480 dump_dwarf_die(re, ret_die, level + 1); 5481 5482 /* Search sibling. */ 5483 is_info = dwarf_get_die_infotypes_flag(die); 5484 ret = dwarf_siblingof_b(re->dbg, die, &ret_die, is_info, &de); 5485 if (ret == DW_DLV_ERROR) 5486 warnx("dwarf_siblingof: %s", dwarf_errmsg(de)); 5487 else if (ret == DW_DLV_OK) 5488 dump_dwarf_die(re, ret_die, level); 5489 5490 dwarf_dealloc(re->dbg, die, DW_DLA_DIE); 5491 } 5492 5493 static void 5494 set_cu_context(struct readelf *re, Dwarf_Half psize, Dwarf_Half osize, 5495 Dwarf_Half ver) 5496 { 5497 5498 re->cu_psize = psize; 5499 re->cu_osize = osize; 5500 re->cu_ver = ver; 5501 } 5502 5503 static void 5504 dump_dwarf_info(struct readelf *re, Dwarf_Bool is_info) 5505 { 5506 struct section *s; 5507 Dwarf_Die die; 5508 Dwarf_Error de; 5509 Dwarf_Half tag, version, pointer_size, off_size; 5510 Dwarf_Off cu_offset, cu_length; 5511 Dwarf_Off aboff; 5512 Dwarf_Unsigned typeoff; 5513 Dwarf_Sig8 sig8; 5514 Dwarf_Unsigned sig; 5515 uint8_t *p; 5516 const char *sn; 5517 int i, ret; 5518 5519 sn = is_info ? ".debug_info" : ".debug_types"; 5520 5521 s = NULL; 5522 for (i = 0; (size_t) i < re->shnum; i++) { 5523 s = &re->sl[i]; 5524 if (s->name != NULL && !strcmp(s->name, sn)) 5525 break; 5526 } 5527 if ((size_t) i >= re->shnum) 5528 return; 5529 5530 do { 5531 printf("\nDump of debug contents of section %s:\n", sn); 5532 5533 while ((ret = dwarf_next_cu_header_c(re->dbg, is_info, NULL, 5534 &version, &aboff, &pointer_size, &off_size, NULL, &sig8, 5535 &typeoff, NULL, &de)) == DW_DLV_OK) { 5536 set_cu_context(re, pointer_size, off_size, version); 5537 die = NULL; 5538 while (dwarf_siblingof_b(re->dbg, die, &die, is_info, 5539 &de) == DW_DLV_OK) { 5540 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { 5541 warnx("dwarf_tag failed: %s", 5542 dwarf_errmsg(de)); 5543 continue; 5544 } 5545 /* XXX: What about DW_TAG_partial_unit? */ 5546 if ((is_info && tag == DW_TAG_compile_unit) || 5547 (!is_info && tag == DW_TAG_type_unit)) 5548 break; 5549 } 5550 if (die == NULL && is_info) { 5551 warnx("could not find DW_TAG_compile_unit " 5552 "die"); 5553 continue; 5554 } else if (die == NULL && !is_info) { 5555 warnx("could not find DW_TAG_type_unit die"); 5556 continue; 5557 } 5558 5559 if (dwarf_die_CU_offset_range(die, &cu_offset, 5560 &cu_length, &de) != DW_DLV_OK) { 5561 warnx("dwarf_die_CU_offset failed: %s", 5562 dwarf_errmsg(de)); 5563 continue; 5564 } 5565 5566 cu_length -= off_size == 4 ? 4 : 12; 5567 5568 sig = 0; 5569 if (!is_info) { 5570 p = (uint8_t *)(uintptr_t) &sig8.signature[0]; 5571 sig = re->dw_decode(&p, 8); 5572 } 5573 5574 printf("\n Type Unit @ offset 0x%jx:\n", 5575 (uintmax_t) cu_offset); 5576 printf(" Length:\t\t%#jx (%d-bit)\n", 5577 (uintmax_t) cu_length, off_size == 4 ? 32 : 64); 5578 printf(" Version:\t\t%u\n", version); 5579 printf(" Abbrev Offset:\t0x%jx\n", 5580 (uintmax_t) aboff); 5581 printf(" Pointer Size:\t%u\n", pointer_size); 5582 if (!is_info) { 5583 printf(" Signature:\t\t0x%016jx\n", 5584 (uintmax_t) sig); 5585 printf(" Type Offset:\t0x%jx\n", 5586 (uintmax_t) typeoff); 5587 } 5588 5589 dump_dwarf_die(re, die, 0); 5590 } 5591 if (ret == DW_DLV_ERROR) 5592 warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de)); 5593 if (is_info) 5594 break; 5595 } while (dwarf_next_types_section(re->dbg, &de) == DW_DLV_OK); 5596 } 5597 5598 static void 5599 dump_dwarf_abbrev(struct readelf *re) 5600 { 5601 Dwarf_Abbrev ab; 5602 Dwarf_Off aboff, atoff; 5603 Dwarf_Unsigned length, attr_count; 5604 Dwarf_Signed flag, form; 5605 Dwarf_Half tag, attr; 5606 Dwarf_Error de; 5607 const char *tag_str, *attr_str, *form_str; 5608 char unk_tag[32], unk_attr[32], unk_form[32]; 5609 int i, j, ret; 5610 5611 printf("\nContents of section .debug_abbrev:\n\n"); 5612 5613 while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, &aboff, 5614 NULL, NULL, &de)) == DW_DLV_OK) { 5615 printf(" Number TAG\n"); 5616 i = 0; 5617 while ((ret = dwarf_get_abbrev(re->dbg, aboff, &ab, &length, 5618 &attr_count, &de)) == DW_DLV_OK) { 5619 if (length == 1) { 5620 dwarf_dealloc(re->dbg, ab, DW_DLA_ABBREV); 5621 break; 5622 } 5623 aboff += length; 5624 printf("%4d", ++i); 5625 if (dwarf_get_abbrev_tag(ab, &tag, &de) != DW_DLV_OK) { 5626 warnx("dwarf_get_abbrev_tag failed: %s", 5627 dwarf_errmsg(de)); 5628 goto next_abbrev; 5629 } 5630 if (dwarf_get_TAG_name(tag, &tag_str) != DW_DLV_OK) { 5631 snprintf(unk_tag, sizeof(unk_tag), 5632 "[Unknown Tag: %#x]", tag); 5633 tag_str = unk_tag; 5634 } 5635 if (dwarf_get_abbrev_children_flag(ab, &flag, &de) != 5636 DW_DLV_OK) { 5637 warnx("dwarf_get_abbrev_children_flag failed:" 5638 " %s", dwarf_errmsg(de)); 5639 goto next_abbrev; 5640 } 5641 printf(" %s %s\n", tag_str, 5642 flag ? "[has children]" : "[no children]"); 5643 for (j = 0; (Dwarf_Unsigned) j < attr_count; j++) { 5644 if (dwarf_get_abbrev_entry(ab, (Dwarf_Signed) j, 5645 &attr, &form, &atoff, &de) != DW_DLV_OK) { 5646 warnx("dwarf_get_abbrev_entry failed:" 5647 " %s", dwarf_errmsg(de)); 5648 continue; 5649 } 5650 if (dwarf_get_AT_name(attr, &attr_str) != 5651 DW_DLV_OK) { 5652 snprintf(unk_attr, sizeof(unk_attr), 5653 "[Unknown AT: %#x]", attr); 5654 attr_str = unk_attr; 5655 } 5656 if (dwarf_get_FORM_name(form, &form_str) != 5657 DW_DLV_OK) { 5658 snprintf(unk_form, sizeof(unk_form), 5659 "[Unknown Form: %#x]", 5660 (Dwarf_Half) form); 5661 form_str = unk_form; 5662 } 5663 printf(" %-18s %s\n", attr_str, form_str); 5664 } 5665 next_abbrev: 5666 dwarf_dealloc(re->dbg, ab, DW_DLA_ABBREV); 5667 } 5668 if (ret != DW_DLV_OK) 5669 warnx("dwarf_get_abbrev: %s", dwarf_errmsg(de)); 5670 } 5671 if (ret == DW_DLV_ERROR) 5672 warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de)); 5673 } 5674 5675 static void 5676 dump_dwarf_pubnames(struct readelf *re) 5677 { 5678 struct section *s; 5679 Dwarf_Off die_off; 5680 Dwarf_Unsigned offset, length, nt_cu_offset, nt_cu_length; 5681 Dwarf_Signed cnt; 5682 Dwarf_Global *globs; 5683 Dwarf_Half nt_version; 5684 Dwarf_Error de; 5685 Elf_Data *d; 5686 char *glob_name; 5687 int i, dwarf_size, elferr; 5688 5689 printf("\nContents of the .debug_pubnames section:\n"); 5690 5691 s = NULL; 5692 for (i = 0; (size_t) i < re->shnum; i++) { 5693 s = &re->sl[i]; 5694 if (s->name != NULL && !strcmp(s->name, ".debug_pubnames")) 5695 break; 5696 } 5697 if ((size_t) i >= re->shnum) 5698 return; 5699 5700 (void) elf_errno(); 5701 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 5702 elferr = elf_errno(); 5703 if (elferr != 0) 5704 warnx("elf_getdata failed: %s", elf_errmsg(-1)); 5705 return; 5706 } 5707 if (d->d_size <= 0) 5708 return; 5709 5710 /* Read in .debug_pubnames section table header. */ 5711 offset = 0; 5712 length = re->dw_read(d, &offset, 4); 5713 if (length == 0xffffffff) { 5714 dwarf_size = 8; 5715 length = re->dw_read(d, &offset, 8); 5716 } else 5717 dwarf_size = 4; 5718 5719 if (length > d->d_size - offset) { 5720 warnx("invalid .dwarf_pubnames section"); 5721 return; 5722 } 5723 5724 nt_version = re->dw_read(d, &offset, 2); 5725 nt_cu_offset = re->dw_read(d, &offset, dwarf_size); 5726 nt_cu_length = re->dw_read(d, &offset, dwarf_size); 5727 printf(" Length:\t\t\t\t%ju\n", (uintmax_t) length); 5728 printf(" Version:\t\t\t\t%u\n", nt_version); 5729 printf(" Offset into .debug_info section:\t%ju\n", 5730 (uintmax_t) nt_cu_offset); 5731 printf(" Size of area in .debug_info section:\t%ju\n", 5732 (uintmax_t) nt_cu_length); 5733 5734 if (dwarf_get_globals(re->dbg, &globs, &cnt, &de) != DW_DLV_OK) { 5735 warnx("dwarf_get_globals failed: %s", dwarf_errmsg(de)); 5736 return; 5737 } 5738 5739 printf("\n Offset Name\n"); 5740 for (i = 0; i < cnt; i++) { 5741 if (dwarf_globname(globs[i], &glob_name, &de) != DW_DLV_OK) { 5742 warnx("dwarf_globname failed: %s", dwarf_errmsg(de)); 5743 continue; 5744 } 5745 if (dwarf_global_die_offset(globs[i], &die_off, &de) != 5746 DW_DLV_OK) { 5747 warnx("dwarf_global_die_offset failed: %s", 5748 dwarf_errmsg(de)); 5749 continue; 5750 } 5751 printf(" %-11ju %s\n", (uintmax_t) die_off, glob_name); 5752 } 5753 } 5754 5755 static void 5756 dump_dwarf_aranges(struct readelf *re) 5757 { 5758 struct section *s; 5759 Dwarf_Arange *aranges; 5760 Dwarf_Addr start; 5761 Dwarf_Unsigned offset, length, as_cu_offset; 5762 Dwarf_Off die_off; 5763 Dwarf_Signed cnt; 5764 Dwarf_Half as_version, as_addrsz, as_segsz; 5765 Dwarf_Error de; 5766 Elf_Data *d; 5767 int i, dwarf_size, elferr; 5768 5769 printf("\nContents of section .debug_aranges:\n"); 5770 5771 s = NULL; 5772 for (i = 0; (size_t) i < re->shnum; i++) { 5773 s = &re->sl[i]; 5774 if (s->name != NULL && !strcmp(s->name, ".debug_aranges")) 5775 break; 5776 } 5777 if ((size_t) i >= re->shnum) 5778 return; 5779 5780 (void) elf_errno(); 5781 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 5782 elferr = elf_errno(); 5783 if (elferr != 0) 5784 warnx("elf_getdata failed: %s", elf_errmsg(-1)); 5785 return; 5786 } 5787 if (d->d_size <= 0) 5788 return; 5789 5790 /* Read in the .debug_aranges section table header. */ 5791 offset = 0; 5792 length = re->dw_read(d, &offset, 4); 5793 if (length == 0xffffffff) { 5794 dwarf_size = 8; 5795 length = re->dw_read(d, &offset, 8); 5796 } else 5797 dwarf_size = 4; 5798 5799 if (length > d->d_size - offset) { 5800 warnx("invalid .dwarf_aranges section"); 5801 return; 5802 } 5803 5804 as_version = re->dw_read(d, &offset, 2); 5805 as_cu_offset = re->dw_read(d, &offset, dwarf_size); 5806 as_addrsz = re->dw_read(d, &offset, 1); 5807 as_segsz = re->dw_read(d, &offset, 1); 5808 5809 printf(" Length:\t\t\t%ju\n", (uintmax_t) length); 5810 printf(" Version:\t\t\t%u\n", as_version); 5811 printf(" Offset into .debug_info:\t%ju\n", (uintmax_t) as_cu_offset); 5812 printf(" Pointer Size:\t\t\t%u\n", as_addrsz); 5813 printf(" Segment Size:\t\t\t%u\n", as_segsz); 5814 5815 if (dwarf_get_aranges(re->dbg, &aranges, &cnt, &de) != DW_DLV_OK) { 5816 warnx("dwarf_get_aranges failed: %s", dwarf_errmsg(de)); 5817 return; 5818 } 5819 5820 printf("\n Address Length\n"); 5821 for (i = 0; i < cnt; i++) { 5822 if (dwarf_get_arange_info(aranges[i], &start, &length, 5823 &die_off, &de) != DW_DLV_OK) { 5824 warnx("dwarf_get_arange_info failed: %s", 5825 dwarf_errmsg(de)); 5826 continue; 5827 } 5828 printf(" %08jx %ju\n", (uintmax_t) start, 5829 (uintmax_t) length); 5830 } 5831 } 5832 5833 static void 5834 dump_dwarf_ranges_foreach(struct readelf *re, Dwarf_Die die, Dwarf_Addr base) 5835 { 5836 Dwarf_Attribute *attr_list; 5837 Dwarf_Ranges *ranges; 5838 Dwarf_Die ret_die; 5839 Dwarf_Error de; 5840 Dwarf_Addr base0; 5841 Dwarf_Half attr; 5842 Dwarf_Signed attr_count, cnt; 5843 Dwarf_Unsigned off, bytecnt; 5844 int i, j, ret; 5845 5846 if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) != 5847 DW_DLV_OK) { 5848 if (ret == DW_DLV_ERROR) 5849 warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de)); 5850 goto cont_search; 5851 } 5852 5853 for (i = 0; i < attr_count; i++) { 5854 if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) { 5855 warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de)); 5856 continue; 5857 } 5858 if (attr != DW_AT_ranges) 5859 continue; 5860 if (dwarf_formudata(attr_list[i], &off, &de) != DW_DLV_OK) { 5861 warnx("dwarf_formudata failed: %s", dwarf_errmsg(de)); 5862 continue; 5863 } 5864 if (dwarf_get_ranges(re->dbg, (Dwarf_Off) off, &ranges, &cnt, 5865 &bytecnt, &de) != DW_DLV_OK) 5866 continue; 5867 base0 = base; 5868 for (j = 0; j < cnt; j++) { 5869 printf(" %08jx ", (uintmax_t) off); 5870 if (ranges[j].dwr_type == DW_RANGES_END) { 5871 printf("%s\n", "<End of list>"); 5872 continue; 5873 } else if (ranges[j].dwr_type == 5874 DW_RANGES_ADDRESS_SELECTION) { 5875 base0 = ranges[j].dwr_addr2; 5876 continue; 5877 } 5878 if (re->ec == ELFCLASS32) 5879 printf("%08jx %08jx\n", 5880 (uintmax_t) (ranges[j].dwr_addr1 + base0), 5881 (uintmax_t) (ranges[j].dwr_addr2 + base0)); 5882 else 5883 printf("%016jx %016jx\n", 5884 (uintmax_t) (ranges[j].dwr_addr1 + base0), 5885 (uintmax_t) (ranges[j].dwr_addr2 + base0)); 5886 } 5887 } 5888 5889 cont_search: 5890 /* Search children. */ 5891 ret = dwarf_child(die, &ret_die, &de); 5892 if (ret == DW_DLV_ERROR) 5893 warnx("dwarf_child: %s", dwarf_errmsg(de)); 5894 else if (ret == DW_DLV_OK) 5895 dump_dwarf_ranges_foreach(re, ret_die, base); 5896 5897 /* Search sibling. */ 5898 ret = dwarf_siblingof(re->dbg, die, &ret_die, &de); 5899 if (ret == DW_DLV_ERROR) 5900 warnx("dwarf_siblingof: %s", dwarf_errmsg(de)); 5901 else if (ret == DW_DLV_OK) 5902 dump_dwarf_ranges_foreach(re, ret_die, base); 5903 } 5904 5905 static void 5906 dump_dwarf_ranges(struct readelf *re) 5907 { 5908 Dwarf_Ranges *ranges; 5909 Dwarf_Die die; 5910 Dwarf_Signed cnt; 5911 Dwarf_Unsigned bytecnt; 5912 Dwarf_Half tag; 5913 Dwarf_Error de; 5914 Dwarf_Unsigned lowpc; 5915 int ret; 5916 5917 if (dwarf_get_ranges(re->dbg, 0, &ranges, &cnt, &bytecnt, &de) != 5918 DW_DLV_OK) 5919 return; 5920 5921 printf("Contents of the .debug_ranges section:\n\n"); 5922 if (re->ec == ELFCLASS32) 5923 printf(" %-8s %-8s %s\n", "Offset", "Begin", "End"); 5924 else 5925 printf(" %-8s %-16s %s\n", "Offset", "Begin", "End"); 5926 5927 while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL, 5928 NULL, &de)) == DW_DLV_OK) { 5929 die = NULL; 5930 if (dwarf_siblingof(re->dbg, die, &die, &de) != DW_DLV_OK) 5931 continue; 5932 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { 5933 warnx("dwarf_tag failed: %s", dwarf_errmsg(de)); 5934 continue; 5935 } 5936 /* XXX: What about DW_TAG_partial_unit? */ 5937 lowpc = 0; 5938 if (tag == DW_TAG_compile_unit) { 5939 if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lowpc, 5940 &de) != DW_DLV_OK) 5941 lowpc = 0; 5942 } 5943 5944 dump_dwarf_ranges_foreach(re, die, (Dwarf_Addr) lowpc); 5945 } 5946 putchar('\n'); 5947 } 5948 5949 static void 5950 dump_dwarf_macinfo(struct readelf *re) 5951 { 5952 Dwarf_Unsigned offset; 5953 Dwarf_Signed cnt; 5954 Dwarf_Macro_Details *md; 5955 Dwarf_Error de; 5956 const char *mi_str; 5957 char unk_mi[32]; 5958 int i; 5959 5960 #define _MAX_MACINFO_ENTRY 65535 5961 5962 printf("\nContents of section .debug_macinfo:\n\n"); 5963 5964 offset = 0; 5965 while (dwarf_get_macro_details(re->dbg, offset, _MAX_MACINFO_ENTRY, 5966 &cnt, &md, &de) == DW_DLV_OK) { 5967 for (i = 0; i < cnt; i++) { 5968 offset = md[i].dmd_offset + 1; 5969 if (md[i].dmd_type == 0) 5970 break; 5971 if (dwarf_get_MACINFO_name(md[i].dmd_type, &mi_str) != 5972 DW_DLV_OK) { 5973 snprintf(unk_mi, sizeof(unk_mi), 5974 "[Unknown MACINFO: %#x]", md[i].dmd_type); 5975 mi_str = unk_mi; 5976 } 5977 printf(" %s", mi_str); 5978 switch (md[i].dmd_type) { 5979 case DW_MACINFO_define: 5980 case DW_MACINFO_undef: 5981 printf(" - lineno : %jd macro : %s\n", 5982 (intmax_t) md[i].dmd_lineno, 5983 md[i].dmd_macro); 5984 break; 5985 case DW_MACINFO_start_file: 5986 printf(" - lineno : %jd filenum : %jd\n", 5987 (intmax_t) md[i].dmd_lineno, 5988 (intmax_t) md[i].dmd_fileindex); 5989 break; 5990 default: 5991 putchar('\n'); 5992 break; 5993 } 5994 } 5995 } 5996 5997 #undef _MAX_MACINFO_ENTRY 5998 } 5999 6000 static void 6001 dump_dwarf_frame_inst(struct readelf *re, Dwarf_Cie cie, uint8_t *insts, 6002 Dwarf_Unsigned len, Dwarf_Unsigned caf, Dwarf_Signed daf, Dwarf_Addr pc, 6003 Dwarf_Debug dbg) 6004 { 6005 Dwarf_Frame_Op *oplist; 6006 Dwarf_Signed opcnt, delta; 6007 Dwarf_Small op; 6008 Dwarf_Error de; 6009 const char *op_str; 6010 char unk_op[32]; 6011 int i; 6012 6013 if (dwarf_expand_frame_instructions(cie, insts, len, &oplist, 6014 &opcnt, &de) != DW_DLV_OK) { 6015 warnx("dwarf_expand_frame_instructions failed: %s", 6016 dwarf_errmsg(de)); 6017 return; 6018 } 6019 6020 for (i = 0; i < opcnt; i++) { 6021 if (oplist[i].fp_base_op != 0) 6022 op = oplist[i].fp_base_op << 6; 6023 else 6024 op = oplist[i].fp_extended_op; 6025 if (dwarf_get_CFA_name(op, &op_str) != DW_DLV_OK) { 6026 snprintf(unk_op, sizeof(unk_op), "[Unknown CFA: %#x]", 6027 op); 6028 op_str = unk_op; 6029 } 6030 printf(" %s", op_str); 6031 switch (op) { 6032 case DW_CFA_advance_loc: 6033 delta = oplist[i].fp_offset * caf; 6034 pc += delta; 6035 printf(": %ju to %08jx", (uintmax_t) delta, 6036 (uintmax_t) pc); 6037 break; 6038 case DW_CFA_offset: 6039 case DW_CFA_offset_extended: 6040 case DW_CFA_offset_extended_sf: 6041 delta = oplist[i].fp_offset * daf; 6042 printf(": r%u (%s) at cfa%+jd", oplist[i].fp_register, 6043 dwarf_regname(re, oplist[i].fp_register), 6044 (intmax_t) delta); 6045 break; 6046 case DW_CFA_restore: 6047 printf(": r%u (%s)", oplist[i].fp_register, 6048 dwarf_regname(re, oplist[i].fp_register)); 6049 break; 6050 case DW_CFA_set_loc: 6051 pc = oplist[i].fp_offset; 6052 printf(": to %08jx", (uintmax_t) pc); 6053 break; 6054 case DW_CFA_advance_loc1: 6055 case DW_CFA_advance_loc2: 6056 case DW_CFA_advance_loc4: 6057 pc += oplist[i].fp_offset; 6058 printf(": %jd to %08jx", (intmax_t) oplist[i].fp_offset, 6059 (uintmax_t) pc); 6060 break; 6061 case DW_CFA_def_cfa: 6062 printf(": r%u (%s) ofs %ju", oplist[i].fp_register, 6063 dwarf_regname(re, oplist[i].fp_register), 6064 (uintmax_t) oplist[i].fp_offset); 6065 break; 6066 case DW_CFA_def_cfa_sf: 6067 printf(": r%u (%s) ofs %jd", oplist[i].fp_register, 6068 dwarf_regname(re, oplist[i].fp_register), 6069 (intmax_t) (oplist[i].fp_offset * daf)); 6070 break; 6071 case DW_CFA_def_cfa_register: 6072 printf(": r%u (%s)", oplist[i].fp_register, 6073 dwarf_regname(re, oplist[i].fp_register)); 6074 break; 6075 case DW_CFA_def_cfa_offset: 6076 printf(": %ju", (uintmax_t) oplist[i].fp_offset); 6077 break; 6078 case DW_CFA_def_cfa_offset_sf: 6079 printf(": %jd", (intmax_t) (oplist[i].fp_offset * daf)); 6080 break; 6081 default: 6082 break; 6083 } 6084 putchar('\n'); 6085 } 6086 6087 dwarf_dealloc(dbg, oplist, DW_DLA_FRAME_BLOCK); 6088 } 6089 6090 static char * 6091 get_regoff_str(struct readelf *re, Dwarf_Half reg, Dwarf_Addr off) 6092 { 6093 static char rs[16]; 6094 6095 if (reg == DW_FRAME_UNDEFINED_VAL || reg == DW_FRAME_REG_INITIAL_VALUE) 6096 snprintf(rs, sizeof(rs), "%c", 'u'); 6097 else if (reg == DW_FRAME_CFA_COL) 6098 snprintf(rs, sizeof(rs), "c%+jd", (intmax_t) off); 6099 else 6100 snprintf(rs, sizeof(rs), "%s%+jd", dwarf_regname(re, reg), 6101 (intmax_t) off); 6102 6103 return (rs); 6104 } 6105 6106 static int 6107 dump_dwarf_frame_regtable(struct readelf *re, Dwarf_Fde fde, Dwarf_Addr pc, 6108 Dwarf_Unsigned func_len, Dwarf_Half cie_ra) 6109 { 6110 Dwarf_Regtable rt; 6111 Dwarf_Addr row_pc, end_pc, pre_pc, cur_pc; 6112 Dwarf_Error de; 6113 char *vec; 6114 int i; 6115 6116 #define BIT_SET(v, n) (v[(n)>>3] |= 1U << ((n) & 7)) 6117 #define BIT_CLR(v, n) (v[(n)>>3] &= ~(1U << ((n) & 7))) 6118 #define BIT_ISSET(v, n) (v[(n)>>3] & (1U << ((n) & 7))) 6119 #define RT(x) rt.rules[(x)] 6120 6121 vec = calloc((DW_REG_TABLE_SIZE + 7) / 8, 1); 6122 if (vec == NULL) 6123 err(EXIT_FAILURE, "calloc failed"); 6124 6125 pre_pc = ~((Dwarf_Addr) 0); 6126 cur_pc = pc; 6127 end_pc = pc + func_len; 6128 for (; cur_pc < end_pc; cur_pc++) { 6129 if (dwarf_get_fde_info_for_all_regs(fde, cur_pc, &rt, &row_pc, 6130 &de) != DW_DLV_OK) { 6131 free(vec); 6132 warnx("dwarf_get_fde_info_for_all_regs failed: %s\n", 6133 dwarf_errmsg(de)); 6134 return (-1); 6135 } 6136 if (row_pc == pre_pc) 6137 continue; 6138 pre_pc = row_pc; 6139 for (i = 1; i < DW_REG_TABLE_SIZE; i++) { 6140 if (rt.rules[i].dw_regnum != DW_FRAME_REG_INITIAL_VALUE) 6141 BIT_SET(vec, i); 6142 } 6143 } 6144 6145 printf(" LOC CFA "); 6146 for (i = 1; i < DW_REG_TABLE_SIZE; i++) { 6147 if (BIT_ISSET(vec, i)) { 6148 if ((Dwarf_Half) i == cie_ra) 6149 printf("ra "); 6150 else 6151 printf("%-5s", 6152 dwarf_regname(re, (unsigned int) i)); 6153 } 6154 } 6155 putchar('\n'); 6156 6157 pre_pc = ~((Dwarf_Addr) 0); 6158 cur_pc = pc; 6159 end_pc = pc + func_len; 6160 for (; cur_pc < end_pc; cur_pc++) { 6161 if (dwarf_get_fde_info_for_all_regs(fde, cur_pc, &rt, &row_pc, 6162 &de) != DW_DLV_OK) { 6163 free(vec); 6164 warnx("dwarf_get_fde_info_for_all_regs failed: %s\n", 6165 dwarf_errmsg(de)); 6166 return (-1); 6167 } 6168 if (row_pc == pre_pc) 6169 continue; 6170 pre_pc = row_pc; 6171 printf("%08jx ", (uintmax_t) row_pc); 6172 printf("%-8s ", get_regoff_str(re, RT(0).dw_regnum, 6173 RT(0).dw_offset)); 6174 for (i = 1; i < DW_REG_TABLE_SIZE; i++) { 6175 if (BIT_ISSET(vec, i)) { 6176 printf("%-5s", get_regoff_str(re, 6177 RT(i).dw_regnum, RT(i).dw_offset)); 6178 } 6179 } 6180 putchar('\n'); 6181 } 6182 6183 free(vec); 6184 6185 return (0); 6186 6187 #undef BIT_SET 6188 #undef BIT_CLR 6189 #undef BIT_ISSET 6190 #undef RT 6191 } 6192 6193 static void 6194 dump_dwarf_frame_section(struct readelf *re, struct section *s, int alt) 6195 { 6196 Dwarf_Cie *cie_list, cie, pre_cie; 6197 Dwarf_Fde *fde_list, fde; 6198 Dwarf_Off cie_offset, fde_offset; 6199 Dwarf_Unsigned cie_length, fde_instlen; 6200 Dwarf_Unsigned cie_caf, cie_daf, cie_instlen, func_len, fde_length; 6201 Dwarf_Signed cie_count, fde_count, cie_index; 6202 Dwarf_Addr low_pc; 6203 Dwarf_Half cie_ra; 6204 Dwarf_Small cie_version; 6205 Dwarf_Ptr fde_addr, fde_inst, cie_inst; 6206 char *cie_aug, c; 6207 int i, eh_frame; 6208 Dwarf_Error de; 6209 6210 printf("\nThe section %s contains:\n\n", s->name); 6211 6212 if (!strcmp(s->name, ".debug_frame")) { 6213 eh_frame = 0; 6214 if (dwarf_get_fde_list(re->dbg, &cie_list, &cie_count, 6215 &fde_list, &fde_count, &de) != DW_DLV_OK) { 6216 warnx("dwarf_get_fde_list failed: %s", 6217 dwarf_errmsg(de)); 6218 return; 6219 } 6220 } else if (!strcmp(s->name, ".eh_frame")) { 6221 eh_frame = 1; 6222 if (dwarf_get_fde_list_eh(re->dbg, &cie_list, &cie_count, 6223 &fde_list, &fde_count, &de) != DW_DLV_OK) { 6224 warnx("dwarf_get_fde_list_eh failed: %s", 6225 dwarf_errmsg(de)); 6226 return; 6227 } 6228 } else 6229 return; 6230 6231 pre_cie = NULL; 6232 for (i = 0; i < fde_count; i++) { 6233 if (dwarf_get_fde_n(fde_list, i, &fde, &de) != DW_DLV_OK) { 6234 warnx("dwarf_get_fde_n failed: %s", dwarf_errmsg(de)); 6235 continue; 6236 } 6237 if (dwarf_get_cie_of_fde(fde, &cie, &de) != DW_DLV_OK) { 6238 warnx("dwarf_get_fde_n failed: %s", dwarf_errmsg(de)); 6239 continue; 6240 } 6241 if (dwarf_get_fde_range(fde, &low_pc, &func_len, &fde_addr, 6242 &fde_length, &cie_offset, &cie_index, &fde_offset, 6243 &de) != DW_DLV_OK) { 6244 warnx("dwarf_get_fde_range failed: %s", 6245 dwarf_errmsg(de)); 6246 continue; 6247 } 6248 if (dwarf_get_fde_instr_bytes(fde, &fde_inst, &fde_instlen, 6249 &de) != DW_DLV_OK) { 6250 warnx("dwarf_get_fde_instr_bytes failed: %s", 6251 dwarf_errmsg(de)); 6252 continue; 6253 } 6254 if (pre_cie == NULL || cie != pre_cie) { 6255 pre_cie = cie; 6256 if (dwarf_get_cie_info(cie, &cie_length, &cie_version, 6257 &cie_aug, &cie_caf, &cie_daf, &cie_ra, 6258 &cie_inst, &cie_instlen, &de) != DW_DLV_OK) { 6259 warnx("dwarf_get_cie_info failed: %s", 6260 dwarf_errmsg(de)); 6261 continue; 6262 } 6263 printf("%08jx %08jx %8.8jx CIE", 6264 (uintmax_t) cie_offset, 6265 (uintmax_t) cie_length, 6266 (uintmax_t) (eh_frame ? 0 : ~0U)); 6267 if (!alt) { 6268 putchar('\n'); 6269 printf(" Version:\t\t\t%u\n", cie_version); 6270 printf(" Augmentation:\t\t\t\""); 6271 while ((c = *cie_aug++) != '\0') 6272 putchar(c); 6273 printf("\"\n"); 6274 printf(" Code alignment factor:\t%ju\n", 6275 (uintmax_t) cie_caf); 6276 printf(" Data alignment factor:\t%jd\n", 6277 (intmax_t) cie_daf); 6278 printf(" Return address column:\t%ju\n", 6279 (uintmax_t) cie_ra); 6280 putchar('\n'); 6281 dump_dwarf_frame_inst(re, cie, cie_inst, 6282 cie_instlen, cie_caf, cie_daf, 0, 6283 re->dbg); 6284 putchar('\n'); 6285 } else { 6286 printf(" \""); 6287 while ((c = *cie_aug++) != '\0') 6288 putchar(c); 6289 putchar('"'); 6290 printf(" cf=%ju df=%jd ra=%ju\n", 6291 (uintmax_t) cie_caf, 6292 (uintmax_t) cie_daf, 6293 (uintmax_t) cie_ra); 6294 dump_dwarf_frame_regtable(re, fde, low_pc, 1, 6295 cie_ra); 6296 putchar('\n'); 6297 } 6298 } 6299 printf("%08jx %08jx %08jx FDE cie=%08jx pc=%08jx..%08jx\n", 6300 (uintmax_t) fde_offset, (uintmax_t) fde_length, 6301 (uintmax_t) cie_offset, 6302 (uintmax_t) (eh_frame ? fde_offset + 4 - cie_offset : 6303 cie_offset), 6304 (uintmax_t) low_pc, (uintmax_t) (low_pc + func_len)); 6305 if (!alt) 6306 dump_dwarf_frame_inst(re, cie, fde_inst, fde_instlen, 6307 cie_caf, cie_daf, low_pc, re->dbg); 6308 else 6309 dump_dwarf_frame_regtable(re, fde, low_pc, func_len, 6310 cie_ra); 6311 putchar('\n'); 6312 } 6313 } 6314 6315 static void 6316 dump_dwarf_frame(struct readelf *re, int alt) 6317 { 6318 struct section *s; 6319 int i; 6320 6321 (void) dwarf_set_frame_cfa_value(re->dbg, DW_FRAME_CFA_COL); 6322 6323 for (i = 0; (size_t) i < re->shnum; i++) { 6324 s = &re->sl[i]; 6325 if (s->name != NULL && (!strcmp(s->name, ".debug_frame") || 6326 !strcmp(s->name, ".eh_frame"))) 6327 dump_dwarf_frame_section(re, s, alt); 6328 } 6329 } 6330 6331 static void 6332 dump_dwarf_str(struct readelf *re) 6333 { 6334 struct section *s; 6335 Elf_Data *d; 6336 unsigned char *p; 6337 int elferr, end, i, j; 6338 6339 printf("\nContents of section .debug_str:\n"); 6340 6341 s = NULL; 6342 for (i = 0; (size_t) i < re->shnum; i++) { 6343 s = &re->sl[i]; 6344 if (s->name != NULL && !strcmp(s->name, ".debug_str")) 6345 break; 6346 } 6347 if ((size_t) i >= re->shnum) 6348 return; 6349 6350 (void) elf_errno(); 6351 if ((d = elf_getdata(s->scn, NULL)) == NULL) { 6352 elferr = elf_errno(); 6353 if (elferr != 0) 6354 warnx("elf_getdata failed: %s", elf_errmsg(-1)); 6355 return; 6356 } 6357 if (d->d_size <= 0) 6358 return; 6359 6360 for (i = 0, p = d->d_buf; (size_t) i < d->d_size; i += 16) { 6361 printf(" 0x%08x", (unsigned int) i); 6362 if ((size_t) i + 16 > d->d_size) 6363 end = d->d_size; 6364 else 6365 end = i + 16; 6366 for (j = i; j < i + 16; j++) { 6367 if ((j - i) % 4 == 0) 6368 putchar(' '); 6369 if (j >= end) { 6370 printf(" "); 6371 continue; 6372 } 6373 printf("%02x", (uint8_t) p[j]); 6374 } 6375 putchar(' '); 6376 for (j = i; j < end; j++) { 6377 if (isprint(p[j])) 6378 putchar(p[j]); 6379 else if (p[j] == 0) 6380 putchar('.'); 6381 else 6382 putchar(' '); 6383 } 6384 putchar('\n'); 6385 } 6386 } 6387 6388 static int 6389 loc_at_comparator(const void *la1, const void *la2) 6390 { 6391 const struct loc_at *left, *right; 6392 6393 left = (const struct loc_at *)la1; 6394 right = (const struct loc_at *)la2; 6395 6396 if (left->la_off > right->la_off) 6397 return (1); 6398 else if (left->la_off < right->la_off) 6399 return (-1); 6400 else 6401 return (0); 6402 } 6403 6404 static void 6405 search_loclist_at(struct readelf *re, Dwarf_Die die, Dwarf_Unsigned lowpc, 6406 struct loc_at **la_list, size_t *la_list_len, size_t *la_list_cap) 6407 { 6408 struct loc_at *la; 6409 Dwarf_Attribute *attr_list; 6410 Dwarf_Die ret_die; 6411 Dwarf_Unsigned off; 6412 Dwarf_Off ref; 6413 Dwarf_Signed attr_count; 6414 Dwarf_Half attr, form; 6415 Dwarf_Bool is_info; 6416 Dwarf_Error de; 6417 int i, ret; 6418 6419 is_info = dwarf_get_die_infotypes_flag(die); 6420 6421 if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) != 6422 DW_DLV_OK) { 6423 if (ret == DW_DLV_ERROR) 6424 warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de)); 6425 goto cont_search; 6426 } 6427 for (i = 0; i < attr_count; i++) { 6428 if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) { 6429 warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de)); 6430 continue; 6431 } 6432 if (attr != DW_AT_location && 6433 attr != DW_AT_string_length && 6434 attr != DW_AT_return_addr && 6435 attr != DW_AT_data_member_location && 6436 attr != DW_AT_frame_base && 6437 attr != DW_AT_segment && 6438 attr != DW_AT_static_link && 6439 attr != DW_AT_use_location && 6440 attr != DW_AT_vtable_elem_location) 6441 continue; 6442 if (dwarf_whatform(attr_list[i], &form, &de) != DW_DLV_OK) { 6443 warnx("dwarf_whatform failed: %s", dwarf_errmsg(de)); 6444 continue; 6445 } 6446 if (form == DW_FORM_data4 || form == DW_FORM_data8) { 6447 if (dwarf_formudata(attr_list[i], &off, &de) != 6448 DW_DLV_OK) { 6449 warnx("dwarf_formudata failed: %s", 6450 dwarf_errmsg(de)); 6451 continue; 6452 } 6453 } else if (form == DW_FORM_sec_offset) { 6454 if (dwarf_global_formref(attr_list[i], &ref, &de) != 6455 DW_DLV_OK) { 6456 warnx("dwarf_global_formref failed: %s", 6457 dwarf_errmsg(de)); 6458 continue; 6459 } 6460 off = ref; 6461 } else 6462 continue; 6463 6464 if (*la_list_cap == *la_list_len) { 6465 *la_list = realloc(*la_list, 6466 *la_list_cap * 2 * sizeof(**la_list)); 6467 if (*la_list == NULL) 6468 err(EXIT_FAILURE, "realloc failed"); 6469 *la_list_cap *= 2; 6470 } 6471 la = &((*la_list)[*la_list_len]); 6472 la->la_at = attr_list[i]; 6473 la->la_off = off; 6474 la->la_lowpc = lowpc; 6475 la->la_cu_psize = re->cu_psize; 6476 la->la_cu_osize = re->cu_osize; 6477 la->la_cu_ver = re->cu_ver; 6478 (*la_list_len)++; 6479 } 6480 6481 cont_search: 6482 /* Search children. */ 6483 ret = dwarf_child(die, &ret_die, &de); 6484 if (ret == DW_DLV_ERROR) 6485 warnx("dwarf_child: %s", dwarf_errmsg(de)); 6486 else if (ret == DW_DLV_OK) 6487 search_loclist_at(re, ret_die, lowpc, la_list, 6488 la_list_len, la_list_cap); 6489 6490 /* Search sibling. */ 6491 ret = dwarf_siblingof_b(re->dbg, die, &ret_die, is_info, &de); 6492 if (ret == DW_DLV_ERROR) 6493 warnx("dwarf_siblingof: %s", dwarf_errmsg(de)); 6494 else if (ret == DW_DLV_OK) 6495 search_loclist_at(re, ret_die, lowpc, la_list, 6496 la_list_len, la_list_cap); 6497 } 6498 6499 static void 6500 dump_dwarf_loc(struct readelf *re, Dwarf_Loc *lr) 6501 { 6502 const char *op_str; 6503 char unk_op[32]; 6504 uint8_t *b, n; 6505 int i; 6506 6507 if (dwarf_get_OP_name(lr->lr_atom, &op_str) != 6508 DW_DLV_OK) { 6509 snprintf(unk_op, sizeof(unk_op), 6510 "[Unknown OP: %#x]", lr->lr_atom); 6511 op_str = unk_op; 6512 } 6513 6514 printf("%s", op_str); 6515 6516 switch (lr->lr_atom) { 6517 case DW_OP_reg0: 6518 case DW_OP_reg1: 6519 case DW_OP_reg2: 6520 case DW_OP_reg3: 6521 case DW_OP_reg4: 6522 case DW_OP_reg5: 6523 case DW_OP_reg6: 6524 case DW_OP_reg7: 6525 case DW_OP_reg8: 6526 case DW_OP_reg9: 6527 case DW_OP_reg10: 6528 case DW_OP_reg11: 6529 case DW_OP_reg12: 6530 case DW_OP_reg13: 6531 case DW_OP_reg14: 6532 case DW_OP_reg15: 6533 case DW_OP_reg16: 6534 case DW_OP_reg17: 6535 case DW_OP_reg18: 6536 case DW_OP_reg19: 6537 case DW_OP_reg20: 6538 case DW_OP_reg21: 6539 case DW_OP_reg22: 6540 case DW_OP_reg23: 6541 case DW_OP_reg24: 6542 case DW_OP_reg25: 6543 case DW_OP_reg26: 6544 case DW_OP_reg27: 6545 case DW_OP_reg28: 6546 case DW_OP_reg29: 6547 case DW_OP_reg30: 6548 case DW_OP_reg31: 6549 printf(" (%s)", dwarf_regname(re, lr->lr_atom - DW_OP_reg0)); 6550 break; 6551 6552 case DW_OP_deref: 6553 case DW_OP_lit0: 6554 case DW_OP_lit1: 6555 case DW_OP_lit2: 6556 case DW_OP_lit3: 6557 case DW_OP_lit4: 6558 case DW_OP_lit5: 6559 case DW_OP_lit6: 6560 case DW_OP_lit7: 6561 case DW_OP_lit8: 6562 case DW_OP_lit9: 6563 case DW_OP_lit10: 6564 case DW_OP_lit11: 6565 case DW_OP_lit12: 6566 case DW_OP_lit13: 6567 case DW_OP_lit14: 6568 case DW_OP_lit15: 6569 case DW_OP_lit16: 6570 case DW_OP_lit17: 6571 case DW_OP_lit18: 6572 case DW_OP_lit19: 6573 case DW_OP_lit20: 6574 case DW_OP_lit21: 6575 case DW_OP_lit22: 6576 case DW_OP_lit23: 6577 case DW_OP_lit24: 6578 case DW_OP_lit25: 6579 case DW_OP_lit26: 6580 case DW_OP_lit27: 6581 case DW_OP_lit28: 6582 case DW_OP_lit29: 6583 case DW_OP_lit30: 6584 case DW_OP_lit31: 6585 case DW_OP_dup: 6586 case DW_OP_drop: 6587 case DW_OP_over: 6588 case DW_OP_swap: 6589 case DW_OP_rot: 6590 case DW_OP_xderef: 6591 case DW_OP_abs: 6592 case DW_OP_and: 6593 case DW_OP_div: 6594 case DW_OP_minus: 6595 case DW_OP_mod: 6596 case DW_OP_mul: 6597 case DW_OP_neg: 6598 case DW_OP_not: 6599 case DW_OP_or: 6600 case DW_OP_plus: 6601 case DW_OP_shl: 6602 case DW_OP_shr: 6603 case DW_OP_shra: 6604 case DW_OP_xor: 6605 case DW_OP_eq: 6606 case DW_OP_ge: 6607 case DW_OP_gt: 6608 case DW_OP_le: 6609 case DW_OP_lt: 6610 case DW_OP_ne: 6611 case DW_OP_nop: 6612 case DW_OP_push_object_address: 6613 case DW_OP_form_tls_address: 6614 case DW_OP_call_frame_cfa: 6615 case DW_OP_stack_value: 6616 case DW_OP_GNU_push_tls_address: 6617 case DW_OP_GNU_uninit: 6618 break; 6619 6620 case DW_OP_const1u: 6621 case DW_OP_pick: 6622 case DW_OP_deref_size: 6623 case DW_OP_xderef_size: 6624 case DW_OP_const2u: 6625 case DW_OP_bra: 6626 case DW_OP_skip: 6627 case DW_OP_const4u: 6628 case DW_OP_const8u: 6629 case DW_OP_constu: 6630 case DW_OP_plus_uconst: 6631 case DW_OP_regx: 6632 case DW_OP_piece: 6633 printf(": %ju", (uintmax_t) 6634 lr->lr_number); 6635 break; 6636 6637 case DW_OP_const1s: 6638 case DW_OP_const2s: 6639 case DW_OP_const4s: 6640 case DW_OP_const8s: 6641 case DW_OP_consts: 6642 printf(": %jd", (intmax_t) 6643 lr->lr_number); 6644 break; 6645 6646 case DW_OP_breg0: 6647 case DW_OP_breg1: 6648 case DW_OP_breg2: 6649 case DW_OP_breg3: 6650 case DW_OP_breg4: 6651 case DW_OP_breg5: 6652 case DW_OP_breg6: 6653 case DW_OP_breg7: 6654 case DW_OP_breg8: 6655 case DW_OP_breg9: 6656 case DW_OP_breg10: 6657 case DW_OP_breg11: 6658 case DW_OP_breg12: 6659 case DW_OP_breg13: 6660 case DW_OP_breg14: 6661 case DW_OP_breg15: 6662 case DW_OP_breg16: 6663 case DW_OP_breg17: 6664 case DW_OP_breg18: 6665 case DW_OP_breg19: 6666 case DW_OP_breg20: 6667 case DW_OP_breg21: 6668 case DW_OP_breg22: 6669 case DW_OP_breg23: 6670 case DW_OP_breg24: 6671 case DW_OP_breg25: 6672 case DW_OP_breg26: 6673 case DW_OP_breg27: 6674 case DW_OP_breg28: 6675 case DW_OP_breg29: 6676 case DW_OP_breg30: 6677 case DW_OP_breg31: 6678 printf(" (%s): %jd", 6679 dwarf_regname(re, lr->lr_atom - DW_OP_breg0), 6680 (intmax_t) lr->lr_number); 6681 break; 6682 6683 case DW_OP_fbreg: 6684 printf(": %jd", (intmax_t) 6685 lr->lr_number); 6686 break; 6687 6688 case DW_OP_bregx: 6689 printf(": %ju (%s) %jd", 6690 (uintmax_t) lr->lr_number, 6691 dwarf_regname(re, (unsigned int) lr->lr_number), 6692 (intmax_t) lr->lr_number2); 6693 break; 6694 6695 case DW_OP_addr: 6696 case DW_OP_GNU_encoded_addr: 6697 printf(": %#jx", (uintmax_t) 6698 lr->lr_number); 6699 break; 6700 6701 case DW_OP_GNU_implicit_pointer: 6702 printf(": <0x%jx> %jd", (uintmax_t) lr->lr_number, 6703 (intmax_t) lr->lr_number2); 6704 break; 6705 6706 case DW_OP_implicit_value: 6707 printf(": %ju byte block:", (uintmax_t) lr->lr_number); 6708 b = (uint8_t *)(uintptr_t) lr->lr_number2; 6709 for (i = 0; (Dwarf_Unsigned) i < lr->lr_number; i++) 6710 printf(" %x", b[i]); 6711 break; 6712 6713 case DW_OP_GNU_entry_value: 6714 printf(": ("); 6715 dump_dwarf_block(re, (uint8_t *)(uintptr_t) lr->lr_number2, 6716 lr->lr_number); 6717 putchar(')'); 6718 break; 6719 6720 case DW_OP_GNU_const_type: 6721 printf(": <0x%jx> ", (uintmax_t) lr->lr_number); 6722 b = (uint8_t *)(uintptr_t) lr->lr_number2; 6723 n = *b; 6724 for (i = 1; (uint8_t) i < n; i++) 6725 printf(" %x", b[i]); 6726 break; 6727 6728 case DW_OP_GNU_regval_type: 6729 printf(": %ju (%s) <0x%jx>", (uintmax_t) lr->lr_number, 6730 dwarf_regname(re, (unsigned int) lr->lr_number), 6731 (uintmax_t) lr->lr_number2); 6732 break; 6733 6734 case DW_OP_GNU_convert: 6735 case DW_OP_GNU_deref_type: 6736 case DW_OP_GNU_parameter_ref: 6737 case DW_OP_GNU_reinterpret: 6738 printf(": <0x%jx>", (uintmax_t) lr->lr_number); 6739 break; 6740 6741 default: 6742 break; 6743 } 6744 } 6745 6746 static void 6747 dump_dwarf_block(struct readelf *re, uint8_t *b, Dwarf_Unsigned len) 6748 { 6749 Dwarf_Locdesc *llbuf; 6750 Dwarf_Signed lcnt; 6751 Dwarf_Error de; 6752 int i; 6753 6754 if (dwarf_loclist_from_expr_b(re->dbg, b, len, re->cu_psize, 6755 re->cu_osize, re->cu_ver, &llbuf, &lcnt, &de) != DW_DLV_OK) { 6756 warnx("dwarf_loclist_form_expr_b: %s", dwarf_errmsg(de)); 6757 return; 6758 } 6759 6760 for (i = 0; (Dwarf_Half) i < llbuf->ld_cents; i++) { 6761 dump_dwarf_loc(re, &llbuf->ld_s[i]); 6762 if (i < llbuf->ld_cents - 1) 6763 printf("; "); 6764 } 6765 6766 dwarf_dealloc(re->dbg, llbuf->ld_s, DW_DLA_LOC_BLOCK); 6767 dwarf_dealloc(re->dbg, llbuf, DW_DLA_LOCDESC); 6768 } 6769 6770 static void 6771 dump_dwarf_loclist(struct readelf *re) 6772 { 6773 Dwarf_Die die; 6774 Dwarf_Locdesc **llbuf; 6775 Dwarf_Unsigned lowpc; 6776 Dwarf_Signed lcnt; 6777 Dwarf_Half tag, version, pointer_size, off_size; 6778 Dwarf_Error de; 6779 struct loc_at *la_list, *left, *right, *la; 6780 size_t la_list_len, la_list_cap; 6781 unsigned int duplicates, k; 6782 int i, j, ret, has_content; 6783 6784 la_list_len = 0; 6785 la_list_cap = 200; 6786 if ((la_list = calloc(la_list_cap, sizeof(struct loc_at))) == NULL) 6787 errx(EXIT_FAILURE, "calloc failed"); 6788 /* Search .debug_info section. */ 6789 while ((ret = dwarf_next_cu_header_b(re->dbg, NULL, &version, NULL, 6790 &pointer_size, &off_size, NULL, NULL, &de)) == DW_DLV_OK) { 6791 set_cu_context(re, pointer_size, off_size, version); 6792 die = NULL; 6793 if (dwarf_siblingof(re->dbg, die, &die, &de) != DW_DLV_OK) 6794 continue; 6795 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { 6796 warnx("dwarf_tag failed: %s", dwarf_errmsg(de)); 6797 continue; 6798 } 6799 /* XXX: What about DW_TAG_partial_unit? */ 6800 lowpc = 0; 6801 if (tag == DW_TAG_compile_unit) { 6802 if (dwarf_attrval_unsigned(die, DW_AT_low_pc, 6803 &lowpc, &de) != DW_DLV_OK) 6804 lowpc = 0; 6805 } 6806 6807 /* Search attributes for reference to .debug_loc section. */ 6808 search_loclist_at(re, die, lowpc, &la_list, 6809 &la_list_len, &la_list_cap); 6810 } 6811 if (ret == DW_DLV_ERROR) 6812 warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de)); 6813 6814 /* Search .debug_types section. */ 6815 do { 6816 while ((ret = dwarf_next_cu_header_c(re->dbg, 0, NULL, 6817 &version, NULL, &pointer_size, &off_size, NULL, NULL, 6818 NULL, NULL, &de)) == DW_DLV_OK) { 6819 set_cu_context(re, pointer_size, off_size, version); 6820 die = NULL; 6821 if (dwarf_siblingof(re->dbg, die, &die, &de) != 6822 DW_DLV_OK) 6823 continue; 6824 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { 6825 warnx("dwarf_tag failed: %s", 6826 dwarf_errmsg(de)); 6827 continue; 6828 } 6829 6830 lowpc = 0; 6831 if (tag == DW_TAG_type_unit) { 6832 if (dwarf_attrval_unsigned(die, DW_AT_low_pc, 6833 &lowpc, &de) != DW_DLV_OK) 6834 lowpc = 0; 6835 } 6836 6837 /* 6838 * Search attributes for reference to .debug_loc 6839 * section. 6840 */ 6841 search_loclist_at(re, die, lowpc, &la_list, 6842 &la_list_len, &la_list_cap); 6843 } 6844 if (ret == DW_DLV_ERROR) 6845 warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de)); 6846 } while (dwarf_next_types_section(re->dbg, &de) == DW_DLV_OK); 6847 6848 if (la_list_len == 0) { 6849 free(la_list); 6850 return; 6851 } 6852 6853 /* Sort la_list using loc_at_comparator. */ 6854 qsort(la_list, la_list_len, sizeof(struct loc_at), loc_at_comparator); 6855 6856 /* Get rid of the duplicates in la_list. */ 6857 duplicates = 0; 6858 for (k = 1; k < la_list_len; ++k) { 6859 left = &la_list[k - 1 - duplicates]; 6860 right = &la_list[k]; 6861 6862 if (left->la_off == right->la_off) 6863 duplicates++; 6864 else 6865 la_list[k - duplicates] = *right; 6866 } 6867 la_list_len -= duplicates; 6868 6869 has_content = 0; 6870 for (k = 0; k < la_list_len; ++k) { 6871 la = &la_list[k]; 6872 if ((ret = dwarf_loclist_n(la->la_at, &llbuf, &lcnt, &de)) != 6873 DW_DLV_OK) { 6874 if (ret != DW_DLV_NO_ENTRY) 6875 warnx("dwarf_loclist_n failed: %s", 6876 dwarf_errmsg(de)); 6877 continue; 6878 } 6879 if (!has_content) { 6880 has_content = 1; 6881 printf("\nContents of section .debug_loc:\n"); 6882 printf(" Offset Begin End Expression\n"); 6883 } 6884 set_cu_context(re, la->la_cu_psize, la->la_cu_osize, 6885 la->la_cu_ver); 6886 for (i = 0; i < lcnt; i++) { 6887 printf(" %8.8jx ", (uintmax_t) la->la_off); 6888 if (llbuf[i]->ld_lopc == 0 && llbuf[i]->ld_hipc == 0) { 6889 printf("<End of list>\n"); 6890 continue; 6891 } 6892 6893 /* TODO: handle base selection entry. */ 6894 6895 printf("%8.8jx %8.8jx ", 6896 (uintmax_t) (la->la_lowpc + llbuf[i]->ld_lopc), 6897 (uintmax_t) (la->la_lowpc + llbuf[i]->ld_hipc)); 6898 6899 putchar('('); 6900 for (j = 0; (Dwarf_Half) j < llbuf[i]->ld_cents; j++) { 6901 dump_dwarf_loc(re, &llbuf[i]->ld_s[j]); 6902 if (j < llbuf[i]->ld_cents - 1) 6903 printf("; "); 6904 } 6905 putchar(')'); 6906 6907 if (llbuf[i]->ld_lopc == llbuf[i]->ld_hipc) 6908 printf(" (start == end)"); 6909 putchar('\n'); 6910 } 6911 for (i = 0; i < lcnt; i++) { 6912 dwarf_dealloc(re->dbg, llbuf[i]->ld_s, 6913 DW_DLA_LOC_BLOCK); 6914 dwarf_dealloc(re->dbg, llbuf[i], DW_DLA_LOCDESC); 6915 } 6916 dwarf_dealloc(re->dbg, llbuf, DW_DLA_LIST); 6917 } 6918 6919 if (!has_content) 6920 printf("\nSection '.debug_loc' has no debugging data.\n"); 6921 6922 free(la_list); 6923 } 6924 6925 /* 6926 * Retrieve a string using string table section index and the string offset. 6927 */ 6928 static const char* 6929 get_string(struct readelf *re, int strtab, size_t off) 6930 { 6931 const char *name; 6932 6933 if ((name = elf_strptr(re->elf, strtab, off)) == NULL) 6934 return (""); 6935 6936 return (name); 6937 } 6938 6939 /* 6940 * Retrieve the name of a symbol using the section index of the symbol 6941 * table and the index of the symbol within that table. 6942 */ 6943 static const char * 6944 get_symbol_name(struct readelf *re, int symtab, int i) 6945 { 6946 struct section *s; 6947 const char *name; 6948 GElf_Sym sym; 6949 Elf_Data *data; 6950 int elferr; 6951 6952 s = &re->sl[symtab]; 6953 if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM) 6954 return (""); 6955 (void) elf_errno(); 6956 if ((data = elf_getdata(s->scn, NULL)) == NULL) { 6957 elferr = elf_errno(); 6958 if (elferr != 0) 6959 warnx("elf_getdata failed: %s", elf_errmsg(elferr)); 6960 return (""); 6961 } 6962 if (gelf_getsym(data, i, &sym) != &sym) 6963 return (""); 6964 /* Return section name for STT_SECTION symbol. */ 6965 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION) { 6966 if (sym.st_shndx < re->shnum && 6967 re->sl[sym.st_shndx].name != NULL) 6968 return (re->sl[sym.st_shndx].name); 6969 return (""); 6970 } 6971 if (s->link >= re->shnum || 6972 (name = elf_strptr(re->elf, s->link, sym.st_name)) == NULL) 6973 return (""); 6974 6975 return (name); 6976 } 6977 6978 static uint64_t 6979 get_symbol_value(struct readelf *re, int symtab, int i) 6980 { 6981 struct section *s; 6982 GElf_Sym sym; 6983 Elf_Data *data; 6984 int elferr; 6985 6986 s = &re->sl[symtab]; 6987 if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM) 6988 return (0); 6989 (void) elf_errno(); 6990 if ((data = elf_getdata(s->scn, NULL)) == NULL) { 6991 elferr = elf_errno(); 6992 if (elferr != 0) 6993 warnx("elf_getdata failed: %s", elf_errmsg(elferr)); 6994 return (0); 6995 } 6996 if (gelf_getsym(data, i, &sym) != &sym) 6997 return (0); 6998 6999 return (sym.st_value); 7000 } 7001 7002 /* 7003 * Decompress a data section if needed (using ZLIB). 7004 * Returns true if sucessful, false otherwise. 7005 */ 7006 static bool decompress_section(struct section *s, 7007 unsigned char *compressed_data_buffer, size_t compressed_size, 7008 unsigned char **ret_buf, size_t *ret_sz) 7009 { 7010 GElf_Shdr sh; 7011 7012 if (gelf_getshdr(s->scn, &sh) == NULL) 7013 errx(EXIT_FAILURE, "gelf_getshdr() failed: %s", elf_errmsg(-1)); 7014 7015 if (sh.sh_flags & SHF_COMPRESSED) { 7016 int ret; 7017 GElf_Chdr chdr; 7018 Elf64_Xword inflated_size; 7019 unsigned char *uncompressed_data_buffer = NULL; 7020 Elf64_Xword uncompressed_size; 7021 z_stream strm; 7022 7023 if (gelf_getchdr(s->scn, &chdr) == NULL) 7024 errx(EXIT_FAILURE, "gelf_getchdr() failed: %s", elf_errmsg(-1)); 7025 if (chdr.ch_type != ELFCOMPRESS_ZLIB) { 7026 warnx("unknown compression type: %d", chdr.ch_type); 7027 return (false); 7028 } 7029 7030 inflated_size = 0; 7031 uncompressed_size = chdr.ch_size; 7032 uncompressed_data_buffer = malloc(uncompressed_size); 7033 compressed_data_buffer += sizeof(chdr); 7034 compressed_size -= sizeof(chdr); 7035 7036 strm.zalloc = Z_NULL; 7037 strm.zfree = Z_NULL; 7038 strm.opaque = Z_NULL; 7039 strm.avail_in = compressed_size; 7040 strm.avail_out = uncompressed_size; 7041 ret = inflateInit(&strm); 7042 7043 if (ret != Z_OK) 7044 goto fail; 7045 /* 7046 * The section can contain several compressed buffers, 7047 * so decompress in a loop until all data is inflated. 7048 */ 7049 while (inflated_size < compressed_size) { 7050 strm.next_in = compressed_data_buffer + inflated_size; 7051 strm.next_out = uncompressed_data_buffer + inflated_size; 7052 ret = inflate(&strm, Z_FINISH); 7053 if (ret != Z_STREAM_END) 7054 goto fail; 7055 inflated_size = uncompressed_size - strm.avail_out; 7056 ret = inflateReset(&strm); 7057 if (ret != Z_OK) 7058 goto fail; 7059 } 7060 if (strm.avail_out != 0) 7061 warnx("Warning: wrong info in compression header."); 7062 ret = inflateEnd(&strm); 7063 if (ret != Z_OK) 7064 goto fail; 7065 *ret_buf = uncompressed_data_buffer; 7066 *ret_sz = uncompressed_size; 7067 return (true); 7068 fail: 7069 inflateEnd(&strm); 7070 if (strm.msg) 7071 warnx("%s", strm.msg); 7072 else 7073 warnx("ZLIB error: %d", ret); 7074 free(uncompressed_data_buffer); 7075 return (false); 7076 } 7077 return (false); 7078 } 7079 7080 static void 7081 hex_dump(struct readelf *re) 7082 { 7083 struct section *s; 7084 Elf_Data *d; 7085 uint8_t *buf, *new_buf; 7086 size_t sz, nbytes; 7087 uint64_t addr; 7088 int elferr, i, j; 7089 7090 for (i = 1; (size_t) i < re->shnum; i++) { 7091 new_buf = NULL; 7092 s = &re->sl[i]; 7093 if (find_dumpop(re, (size_t) i, s->name, HEX_DUMP, -1) == NULL) 7094 continue; 7095 (void) elf_errno(); 7096 if ((d = elf_getdata(s->scn, NULL)) == NULL && 7097 (d = elf_rawdata(s->scn, NULL)) == NULL) { 7098 elferr = elf_errno(); 7099 if (elferr != 0) 7100 warnx("elf_getdata failed: %s", 7101 elf_errmsg(elferr)); 7102 continue; 7103 } 7104 (void) elf_errno(); 7105 if (d->d_size <= 0 || d->d_buf == NULL) { 7106 printf("\nSection '%s' has no data to dump.\n", 7107 s->name); 7108 continue; 7109 } 7110 buf = d->d_buf; 7111 sz = d->d_size; 7112 addr = s->addr; 7113 if (re->options & RE_Z) { 7114 if (decompress_section(s, d->d_buf, d->d_size, 7115 &new_buf, &sz)) 7116 buf = new_buf; 7117 } 7118 printf("\nHex dump of section '%s':\n", s->name); 7119 while (sz > 0) { 7120 printf(" 0x%8.8jx ", (uintmax_t)addr); 7121 nbytes = sz > 16? 16 : sz; 7122 for (j = 0; j < 16; j++) { 7123 if ((size_t)j < nbytes) 7124 printf("%2.2x", buf[j]); 7125 else 7126 printf(" "); 7127 if ((j & 3) == 3) 7128 printf(" "); 7129 } 7130 for (j = 0; (size_t)j < nbytes; j++) { 7131 if (isprint(buf[j])) 7132 printf("%c", buf[j]); 7133 else 7134 printf("."); 7135 } 7136 printf("\n"); 7137 buf += nbytes; 7138 addr += nbytes; 7139 sz -= nbytes; 7140 } 7141 free(new_buf); 7142 } 7143 } 7144 7145 static void 7146 str_dump(struct readelf *re) 7147 { 7148 struct section *s; 7149 Elf_Data *d; 7150 unsigned char *start, *end, *buf_end, *new_buf; 7151 unsigned int len; 7152 size_t sz; 7153 int i, j, elferr, found; 7154 7155 for (i = 1; (size_t) i < re->shnum; i++) { 7156 new_buf = NULL; 7157 s = &re->sl[i]; 7158 if (find_dumpop(re, (size_t) i, s->name, STR_DUMP, -1) == NULL) 7159 continue; 7160 (void) elf_errno(); 7161 if ((d = elf_getdata(s->scn, NULL)) == NULL && 7162 (d = elf_rawdata(s->scn, NULL)) == NULL) { 7163 elferr = elf_errno(); 7164 if (elferr != 0) 7165 warnx("elf_getdata failed: %s", 7166 elf_errmsg(elferr)); 7167 continue; 7168 } 7169 (void) elf_errno(); 7170 if (d->d_size <= 0 || d->d_buf == NULL) { 7171 printf("\nSection '%s' has no data to dump.\n", 7172 s->name); 7173 continue; 7174 } 7175 found = 0; 7176 start = d->d_buf; 7177 sz = d->d_size; 7178 if (re->options & RE_Z) { 7179 if (decompress_section(s, d->d_buf, d->d_size, 7180 &new_buf, &sz)) 7181 start = new_buf; 7182 } 7183 buf_end = start + sz; 7184 printf("\nString dump of section '%s':\n", s->name); 7185 for (;;) { 7186 while (start < buf_end && !isprint(*start)) 7187 start++; 7188 if (start >= buf_end) 7189 break; 7190 end = start + 1; 7191 while (end < buf_end && isprint(*end)) 7192 end++; 7193 printf(" [%6lx] ", 7194 (long) (start - (unsigned char *) d->d_buf)); 7195 len = end - start; 7196 for (j = 0; (unsigned int) j < len; j++) 7197 putchar(start[j]); 7198 putchar('\n'); 7199 found = 1; 7200 if (end >= buf_end) 7201 break; 7202 start = end + 1; 7203 } 7204 free(new_buf); 7205 if (!found) 7206 printf(" No strings found in this section."); 7207 putchar('\n'); 7208 } 7209 } 7210 7211 static void 7212 load_sections(struct readelf *re) 7213 { 7214 struct section *s; 7215 const char *name; 7216 Elf_Scn *scn; 7217 GElf_Shdr sh; 7218 size_t shstrndx, ndx; 7219 int elferr; 7220 7221 /* Allocate storage for internal section list. */ 7222 if (!elf_getshnum(re->elf, &re->shnum)) { 7223 warnx("elf_getshnum failed: %s", elf_errmsg(-1)); 7224 return; 7225 } 7226 if (re->sl != NULL) 7227 free(re->sl); 7228 if ((re->sl = calloc(re->shnum, sizeof(*re->sl))) == NULL) 7229 err(EXIT_FAILURE, "calloc failed"); 7230 7231 /* Get the index of .shstrtab section. */ 7232 if (!elf_getshstrndx(re->elf, &shstrndx)) { 7233 warnx("elf_getshstrndx failed: %s", elf_errmsg(-1)); 7234 return; 7235 } 7236 7237 if ((scn = elf_getscn(re->elf, 0)) == NULL) 7238 return; 7239 7240 (void) elf_errno(); 7241 do { 7242 if (gelf_getshdr(scn, &sh) == NULL) { 7243 warnx("gelf_getshdr failed: %s", elf_errmsg(-1)); 7244 (void) elf_errno(); 7245 continue; 7246 } 7247 if ((name = elf_strptr(re->elf, shstrndx, sh.sh_name)) == NULL) { 7248 (void) elf_errno(); 7249 name = "<no-name>"; 7250 } 7251 if ((ndx = elf_ndxscn(scn)) == SHN_UNDEF) { 7252 if ((elferr = elf_errno()) != 0) { 7253 warnx("elf_ndxscn failed: %s", 7254 elf_errmsg(elferr)); 7255 continue; 7256 } 7257 } 7258 if (ndx >= re->shnum) { 7259 warnx("section index of '%s' out of range", name); 7260 continue; 7261 } 7262 if (sh.sh_link >= re->shnum) 7263 warnx("section link %llu of '%s' out of range", 7264 (unsigned long long)sh.sh_link, name); 7265 s = &re->sl[ndx]; 7266 s->name = name; 7267 s->scn = scn; 7268 s->off = sh.sh_offset; 7269 s->sz = sh.sh_size; 7270 s->entsize = sh.sh_entsize; 7271 s->align = sh.sh_addralign; 7272 s->type = sh.sh_type; 7273 s->flags = sh.sh_flags; 7274 s->addr = sh.sh_addr; 7275 s->link = sh.sh_link; 7276 s->info = sh.sh_info; 7277 } while ((scn = elf_nextscn(re->elf, scn)) != NULL); 7278 elferr = elf_errno(); 7279 if (elferr != 0) 7280 warnx("elf_nextscn failed: %s", elf_errmsg(elferr)); 7281 } 7282 7283 static void 7284 unload_sections(struct readelf *re) 7285 { 7286 7287 if (re->sl != NULL) { 7288 free(re->sl); 7289 re->sl = NULL; 7290 } 7291 re->shnum = 0; 7292 re->vd_s = NULL; 7293 re->vn_s = NULL; 7294 re->vs_s = NULL; 7295 re->vs = NULL; 7296 re->vs_sz = 0; 7297 if (re->ver != NULL) { 7298 free(re->ver); 7299 re->ver = NULL; 7300 re->ver_sz = 0; 7301 } 7302 } 7303 7304 static bool 7305 dump_elf(struct readelf *re) 7306 { 7307 7308 /* Fetch ELF header. No need to continue if it fails. */ 7309 if (gelf_getehdr(re->elf, &re->ehdr) == NULL) { 7310 warnx("gelf_getehdr failed: %s", elf_errmsg(-1)); 7311 return (false); 7312 } 7313 if ((re->ec = gelf_getclass(re->elf)) == ELFCLASSNONE) { 7314 warnx("gelf_getclass failed: %s", elf_errmsg(-1)); 7315 return (false); 7316 } 7317 if (re->ehdr.e_ident[EI_DATA] == ELFDATA2MSB) { 7318 re->dw_read = _read_msb; 7319 re->dw_decode = _decode_msb; 7320 } else { 7321 re->dw_read = _read_lsb; 7322 re->dw_decode = _decode_lsb; 7323 } 7324 7325 if (re->options & ~RE_H) 7326 load_sections(re); 7327 if ((re->options & RE_VV) || (re->options & RE_S)) 7328 search_ver(re); 7329 if (re->options & RE_H) 7330 dump_ehdr(re); 7331 if (re->options & RE_L) 7332 dump_phdr(re); 7333 if (re->options & RE_SS) 7334 dump_shdr(re); 7335 if (re->options & RE_G) 7336 dump_section_groups(re); 7337 if (re->options & RE_D) 7338 dump_dynamic(re); 7339 if (re->options & RE_R) 7340 dump_reloc(re); 7341 if (re->options & RE_S) 7342 dump_symtabs(re); 7343 if (re->options & RE_N) 7344 dump_notes(re); 7345 if (re->options & RE_II) 7346 dump_hash(re); 7347 if (re->options & RE_X) 7348 hex_dump(re); 7349 if (re->options & RE_P) 7350 str_dump(re); 7351 if (re->options & RE_VV) 7352 dump_ver(re); 7353 if (re->options & RE_AA) 7354 dump_arch_specific_info(re); 7355 if (re->options & RE_W) 7356 dump_dwarf(re); 7357 if (re->options & ~RE_H) 7358 unload_sections(re); 7359 return (true); 7360 } 7361 7362 static void 7363 dump_dwarf(struct readelf *re) 7364 { 7365 Dwarf_Error de; 7366 int error; 7367 7368 if (dwarf_elf_init(re->elf, DW_DLC_READ, NULL, NULL, &re->dbg, &de)) { 7369 if ((error = dwarf_errno(de)) != DW_DLE_DEBUG_INFO_NULL) 7370 errx(EXIT_FAILURE, "dwarf_elf_init failed: %s", 7371 dwarf_errmsg(de)); 7372 return; 7373 } 7374 7375 if (re->dop & DW_A) 7376 dump_dwarf_abbrev(re); 7377 if (re->dop & DW_L) 7378 dump_dwarf_line(re); 7379 if (re->dop & DW_LL) 7380 dump_dwarf_line_decoded(re); 7381 if (re->dop & DW_I) { 7382 dump_dwarf_info(re, 0); 7383 dump_dwarf_info(re, 1); 7384 } 7385 if (re->dop & DW_P) 7386 dump_dwarf_pubnames(re); 7387 if (re->dop & DW_R) 7388 dump_dwarf_aranges(re); 7389 if (re->dop & DW_RR) 7390 dump_dwarf_ranges(re); 7391 if (re->dop & DW_M) 7392 dump_dwarf_macinfo(re); 7393 if (re->dop & DW_F) 7394 dump_dwarf_frame(re, 0); 7395 else if (re->dop & DW_FF) 7396 dump_dwarf_frame(re, 1); 7397 if (re->dop & DW_S) 7398 dump_dwarf_str(re); 7399 if (re->dop & DW_O) 7400 dump_dwarf_loclist(re); 7401 7402 dwarf_finish(re->dbg, &de); 7403 } 7404 7405 static bool 7406 dump_ar(struct readelf *re, int fd) 7407 { 7408 Elf_Arsym *arsym; 7409 Elf_Arhdr *arhdr; 7410 Elf_Cmd cmd; 7411 Elf *e; 7412 size_t sz; 7413 off_t off; 7414 int i; 7415 7416 re->ar = re->elf; 7417 7418 if (re->options & RE_C) { 7419 if ((arsym = elf_getarsym(re->ar, &sz)) == NULL) { 7420 warnx("elf_getarsym() failed: %s", elf_errmsg(-1)); 7421 goto process_members; 7422 } 7423 printf("Index of archive %s: (%ju entries)\n", re->filename, 7424 (uintmax_t) sz - 1); 7425 off = 0; 7426 for (i = 0; (size_t) i < sz; i++) { 7427 if (arsym[i].as_name == NULL) 7428 break; 7429 if (arsym[i].as_off != off) { 7430 off = arsym[i].as_off; 7431 if (elf_rand(re->ar, off) != off) { 7432 warnx("elf_rand() failed: %s", 7433 elf_errmsg(-1)); 7434 continue; 7435 } 7436 if ((e = elf_begin(fd, ELF_C_READ, re->ar)) == 7437 NULL) { 7438 warnx("elf_begin() failed: %s", 7439 elf_errmsg(-1)); 7440 continue; 7441 } 7442 if ((arhdr = elf_getarhdr(e)) == NULL) { 7443 warnx("elf_getarhdr() failed: %s", 7444 elf_errmsg(-1)); 7445 elf_end(e); 7446 continue; 7447 } 7448 printf("Binary %s(%s) contains:\n", 7449 re->filename, arhdr->ar_name); 7450 elf_end(e); 7451 } 7452 printf("\t%s\n", arsym[i].as_name); 7453 } 7454 if (elf_rand(re->ar, SARMAG) != SARMAG) { 7455 warnx("elf_rand() failed: %s", elf_errmsg(-1)); 7456 return (false); 7457 } 7458 } 7459 7460 process_members: 7461 7462 if ((re->options & ~RE_C) == 0) 7463 return (true); 7464 7465 cmd = ELF_C_READ; 7466 while ((re->elf = elf_begin(fd, cmd, re->ar)) != NULL) { 7467 if ((arhdr = elf_getarhdr(re->elf)) == NULL) { 7468 warnx("elf_getarhdr() failed: %s", elf_errmsg(-1)); 7469 goto next_member; 7470 } 7471 if (strcmp(arhdr->ar_name, "/") == 0 || 7472 strcmp(arhdr->ar_name, "//") == 0 || 7473 strcmp(arhdr->ar_name, "__.SYMDEF") == 0) 7474 goto next_member; 7475 printf("\nFile: %s(%s)\n", re->filename, arhdr->ar_name); 7476 dump_elf(re); 7477 7478 next_member: 7479 cmd = elf_next(re->elf); 7480 elf_end(re->elf); 7481 } 7482 re->elf = re->ar; 7483 return (true); 7484 } 7485 7486 static bool 7487 dump_object(struct readelf *re, int fd) 7488 { 7489 bool rv = false; 7490 7491 if ((re->flags & DISPLAY_FILENAME) != 0) 7492 printf("\nFile: %s\n", re->filename); 7493 7494 if ((re->elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) { 7495 warnx("elf_begin() failed: %s", elf_errmsg(-1)); 7496 goto done; 7497 } 7498 7499 switch (elf_kind(re->elf)) { 7500 case ELF_K_NONE: 7501 warnx("Not an ELF file."); 7502 goto done; 7503 case ELF_K_ELF: 7504 rv = dump_elf(re); 7505 break; 7506 case ELF_K_AR: 7507 rv = dump_ar(re, fd); 7508 break; 7509 default: 7510 warnx("Internal: libelf returned unknown elf kind."); 7511 } 7512 7513 done: 7514 elf_end(re->elf); 7515 return (rv); 7516 } 7517 7518 static void 7519 add_dumpop(struct readelf *re, size_t si, const char *sn, int op, int t) 7520 { 7521 struct dumpop *d; 7522 7523 if ((d = find_dumpop(re, si, sn, -1, t)) == NULL) { 7524 if ((d = calloc(1, sizeof(*d))) == NULL) 7525 err(EXIT_FAILURE, "calloc failed"); 7526 if (t == DUMP_BY_INDEX) 7527 d->u.si = si; 7528 else 7529 d->u.sn = sn; 7530 d->type = t; 7531 d->op = op; 7532 STAILQ_INSERT_TAIL(&re->v_dumpop, d, dumpop_list); 7533 } else 7534 d->op |= op; 7535 } 7536 7537 static struct dumpop * 7538 find_dumpop(struct readelf *re, size_t si, const char *sn, int op, int t) 7539 { 7540 struct dumpop *d; 7541 7542 STAILQ_FOREACH(d, &re->v_dumpop, dumpop_list) { 7543 if ((op == -1 || op & d->op) && 7544 (t == -1 || (unsigned) t == d->type)) { 7545 if ((d->type == DUMP_BY_INDEX && d->u.si == si) || 7546 (d->type == DUMP_BY_NAME && !strcmp(d->u.sn, sn))) 7547 return (d); 7548 } 7549 } 7550 7551 return (NULL); 7552 } 7553 7554 static struct { 7555 const char *ln; 7556 char sn; 7557 int value; 7558 } dwarf_op[] = { 7559 {"rawline", 'l', DW_L}, 7560 {"decodedline", 'L', DW_LL}, 7561 {"info", 'i', DW_I}, 7562 {"abbrev", 'a', DW_A}, 7563 {"pubnames", 'p', DW_P}, 7564 {"aranges", 'r', DW_R}, 7565 {"ranges", 'r', DW_R}, 7566 {"Ranges", 'R', DW_RR}, 7567 {"macro", 'm', DW_M}, 7568 {"frames", 'f', DW_F}, 7569 {"frames-interp", 'F', DW_FF}, 7570 {"str", 's', DW_S}, 7571 {"loc", 'o', DW_O}, 7572 {NULL, 0, 0} 7573 }; 7574 7575 static void 7576 parse_dwarf_op_short(struct readelf *re, const char *op) 7577 { 7578 int i; 7579 7580 if (op == NULL) { 7581 re->dop |= DW_DEFAULT_OPTIONS; 7582 return; 7583 } 7584 7585 for (; *op != '\0'; op++) { 7586 for (i = 0; dwarf_op[i].ln != NULL; i++) { 7587 if (dwarf_op[i].sn == *op) { 7588 re->dop |= dwarf_op[i].value; 7589 break; 7590 } 7591 } 7592 } 7593 } 7594 7595 static void 7596 parse_dwarf_op_long(struct readelf *re, const char *op) 7597 { 7598 char *p, *token, *bp; 7599 int i; 7600 7601 if (op == NULL) { 7602 re->dop |= DW_DEFAULT_OPTIONS; 7603 return; 7604 } 7605 7606 if ((p = strdup(op)) == NULL) 7607 err(EXIT_FAILURE, "strdup failed"); 7608 bp = p; 7609 7610 while ((token = strsep(&p, ",")) != NULL) { 7611 for (i = 0; dwarf_op[i].ln != NULL; i++) { 7612 if (!strcmp(token, dwarf_op[i].ln)) { 7613 re->dop |= dwarf_op[i].value; 7614 break; 7615 } 7616 } 7617 } 7618 7619 free(bp); 7620 } 7621 7622 static uint64_t 7623 _read_lsb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read) 7624 { 7625 uint64_t ret; 7626 uint8_t *src; 7627 7628 src = (uint8_t *) d->d_buf + *offsetp; 7629 7630 ret = 0; 7631 switch (bytes_to_read) { 7632 case 8: 7633 ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40; 7634 ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56; 7635 /* FALLTHROUGH */ 7636 case 4: 7637 ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24; 7638 /* FALLTHROUGH */ 7639 case 2: 7640 ret |= ((uint64_t) src[1]) << 8; 7641 /* FALLTHROUGH */ 7642 case 1: 7643 ret |= src[0]; 7644 break; 7645 default: 7646 return (0); 7647 } 7648 7649 *offsetp += bytes_to_read; 7650 7651 return (ret); 7652 } 7653 7654 static uint64_t 7655 _read_msb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read) 7656 { 7657 uint64_t ret; 7658 uint8_t *src; 7659 7660 src = (uint8_t *) d->d_buf + *offsetp; 7661 7662 switch (bytes_to_read) { 7663 case 1: 7664 ret = src[0]; 7665 break; 7666 case 2: 7667 ret = src[1] | ((uint64_t) src[0]) << 8; 7668 break; 7669 case 4: 7670 ret = src[3] | ((uint64_t) src[2]) << 8; 7671 ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24; 7672 break; 7673 case 8: 7674 ret = src[7] | ((uint64_t) src[6]) << 8; 7675 ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24; 7676 ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40; 7677 ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56; 7678 break; 7679 default: 7680 return (0); 7681 } 7682 7683 *offsetp += bytes_to_read; 7684 7685 return (ret); 7686 } 7687 7688 static uint64_t 7689 _decode_lsb(uint8_t **data, int bytes_to_read) 7690 { 7691 uint64_t ret; 7692 uint8_t *src; 7693 7694 src = *data; 7695 7696 ret = 0; 7697 switch (bytes_to_read) { 7698 case 8: 7699 ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40; 7700 ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56; 7701 /* FALLTHROUGH */ 7702 case 4: 7703 ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24; 7704 /* FALLTHROUGH */ 7705 case 2: 7706 ret |= ((uint64_t) src[1]) << 8; 7707 /* FALLTHROUGH */ 7708 case 1: 7709 ret |= src[0]; 7710 break; 7711 default: 7712 return (0); 7713 } 7714 7715 *data += bytes_to_read; 7716 7717 return (ret); 7718 } 7719 7720 static uint64_t 7721 _decode_msb(uint8_t **data, int bytes_to_read) 7722 { 7723 uint64_t ret; 7724 uint8_t *src; 7725 7726 src = *data; 7727 7728 ret = 0; 7729 switch (bytes_to_read) { 7730 case 1: 7731 ret = src[0]; 7732 break; 7733 case 2: 7734 ret = src[1] | ((uint64_t) src[0]) << 8; 7735 break; 7736 case 4: 7737 ret = src[3] | ((uint64_t) src[2]) << 8; 7738 ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24; 7739 break; 7740 case 8: 7741 ret = src[7] | ((uint64_t) src[6]) << 8; 7742 ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24; 7743 ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40; 7744 ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56; 7745 break; 7746 default: 7747 return (0); 7748 break; 7749 } 7750 7751 *data += bytes_to_read; 7752 7753 return (ret); 7754 } 7755 7756 static int64_t 7757 _decode_sleb128(uint8_t **dp, uint8_t *dpe) 7758 { 7759 int64_t ret = 0; 7760 uint8_t b = 0; 7761 int shift = 0; 7762 7763 uint8_t *src = *dp; 7764 7765 do { 7766 if (src >= dpe) 7767 break; 7768 b = *src++; 7769 ret |= ((b & 0x7f) << shift); 7770 shift += 7; 7771 } while ((b & 0x80) != 0); 7772 7773 if (shift < 32 && (b & 0x40) != 0) 7774 ret |= (-1 << shift); 7775 7776 *dp = src; 7777 7778 return (ret); 7779 } 7780 7781 static uint64_t 7782 _decode_uleb128(uint8_t **dp, uint8_t *dpe) 7783 { 7784 uint64_t ret = 0; 7785 uint8_t b; 7786 int shift = 0; 7787 7788 uint8_t *src = *dp; 7789 7790 do { 7791 if (src >= dpe) 7792 break; 7793 b = *src++; 7794 ret |= ((b & 0x7f) << shift); 7795 shift += 7; 7796 } while ((b & 0x80) != 0); 7797 7798 *dp = src; 7799 7800 return (ret); 7801 } 7802 7803 static void 7804 readelf_version(void) 7805 { 7806 (void) printf("%s (%s)\n", ELFTC_GETPROGNAME(), 7807 elftc_version()); 7808 exit(EXIT_SUCCESS); 7809 } 7810 7811 #define USAGE_MESSAGE "\ 7812 Usage: %s [options] file...\n\ 7813 Display information about ELF objects and ar(1) archives.\n\n\ 7814 Options:\n\ 7815 -a | --all Equivalent to specifying options '-dhIlrsASV'.\n\ 7816 -c | --archive-index Print the archive symbol table for archives.\n\ 7817 -d | --dynamic Print the contents of SHT_DYNAMIC sections.\n\ 7818 -e | --headers Print all headers in the object.\n\ 7819 -g | --section-groups Print the contents of the section groups.\n\ 7820 -h | --file-header Print the file header for the object.\n\ 7821 -l | --program-headers Print the PHDR table for the object.\n\ 7822 -n | --notes Print the contents of SHT_NOTE sections.\n\ 7823 -p INDEX | --string-dump=INDEX\n\ 7824 Print the contents of section at index INDEX.\n\ 7825 -r | --relocs Print relocation information.\n\ 7826 -s | --syms | --symbols Print symbol tables.\n\ 7827 -t | --section-details Print additional information about sections.\n\ 7828 -v | --version Print a version identifier and exit.\n\ 7829 -w[afilmoprsFLR] | --debug-dump={abbrev,aranges,decodedline,frames,\n\ 7830 frames-interp,info,loc,macro,pubnames,\n\ 7831 ranges,Ranges,rawline,str}\n\ 7832 Display DWARF information.\n\ 7833 -x INDEX | --hex-dump=INDEX\n\ 7834 Display contents of a section as hexadecimal.\n\ 7835 -z | --decompress Decompress the contents of a section before displaying it.\n\ 7836 -A | --arch-specific (accepted, but ignored)\n\ 7837 -D | --use-dynamic Print the symbol table specified by the DT_SYMTAB\n\ 7838 entry in the \".dynamic\" section.\n\ 7839 -H | --help Print a help message.\n\ 7840 -I | --histogram Print information on bucket list lengths for \n\ 7841 hash sections.\n\ 7842 -N | --full-section-name (accepted, but ignored)\n\ 7843 -S | --sections | --section-headers\n\ 7844 Print information about section headers.\n\ 7845 -V | --version-info Print symbol versoning information.\n\ 7846 -W | --wide Print information without wrapping long lines.\n" 7847 7848 7849 static void 7850 readelf_usage(int status) 7851 { 7852 fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME()); 7853 exit(status); 7854 } 7855 7856 int 7857 main(int argc, char **argv) 7858 { 7859 cap_rights_t rights; 7860 fileargs_t *fa; 7861 struct readelf *re, re_storage; 7862 unsigned long si; 7863 int fd, opt, i, exit_code; 7864 char *ep; 7865 7866 re = &re_storage; 7867 memset(re, 0, sizeof(*re)); 7868 STAILQ_INIT(&re->v_dumpop); 7869 7870 while ((opt = getopt_long(argc, argv, "AacDdegHhIi:lNnp:rSstuVvWw::x:z", 7871 longopts, NULL)) != -1) { 7872 switch(opt) { 7873 case '?': 7874 readelf_usage(EXIT_SUCCESS); 7875 break; 7876 case 'A': 7877 re->options |= RE_AA; 7878 break; 7879 case 'a': 7880 re->options |= RE_AA | RE_D | RE_G | RE_H | RE_II | 7881 RE_L | RE_N | RE_R | RE_SS | RE_S | RE_U | RE_VV; 7882 break; 7883 case 'c': 7884 re->options |= RE_C; 7885 break; 7886 case 'D': 7887 re->options |= RE_DD; 7888 break; 7889 case 'd': 7890 re->options |= RE_D; 7891 break; 7892 case 'e': 7893 re->options |= RE_H | RE_L | RE_SS; 7894 break; 7895 case 'g': 7896 re->options |= RE_G; 7897 break; 7898 case 'H': 7899 readelf_usage(EXIT_SUCCESS); 7900 break; 7901 case 'h': 7902 re->options |= RE_H; 7903 break; 7904 case 'I': 7905 re->options |= RE_II; 7906 break; 7907 case 'i': 7908 /* Not implemented yet. */ 7909 break; 7910 case 'l': 7911 re->options |= RE_L; 7912 break; 7913 case 'N': 7914 re->options |= RE_NN; 7915 break; 7916 case 'n': 7917 re->options |= RE_N; 7918 break; 7919 case 'p': 7920 re->options |= RE_P; 7921 si = strtoul(optarg, &ep, 10); 7922 if (*ep == '\0') 7923 add_dumpop(re, (size_t) si, NULL, STR_DUMP, 7924 DUMP_BY_INDEX); 7925 else 7926 add_dumpop(re, 0, optarg, STR_DUMP, 7927 DUMP_BY_NAME); 7928 break; 7929 case 'r': 7930 re->options |= RE_R; 7931 break; 7932 case 'S': 7933 re->options |= RE_SS; 7934 break; 7935 case 's': 7936 re->options |= RE_S; 7937 break; 7938 case 't': 7939 re->options |= RE_SS | RE_T; 7940 break; 7941 case 'u': 7942 re->options |= RE_U; 7943 break; 7944 case 'V': 7945 re->options |= RE_VV; 7946 break; 7947 case 'v': 7948 readelf_version(); 7949 break; 7950 case 'W': 7951 re->options |= RE_WW; 7952 break; 7953 case 'w': 7954 re->options |= RE_W; 7955 parse_dwarf_op_short(re, optarg); 7956 break; 7957 case 'x': 7958 re->options |= RE_X; 7959 si = strtoul(optarg, &ep, 10); 7960 if (*ep == '\0') 7961 add_dumpop(re, (size_t) si, NULL, HEX_DUMP, 7962 DUMP_BY_INDEX); 7963 else 7964 add_dumpop(re, 0, optarg, HEX_DUMP, 7965 DUMP_BY_NAME); 7966 break; 7967 case 'z': 7968 re->options |= RE_Z; 7969 break; 7970 case OPTION_DEBUG_DUMP: 7971 re->options |= RE_W; 7972 parse_dwarf_op_long(re, optarg); 7973 } 7974 } 7975 7976 argv += optind; 7977 argc -= optind; 7978 7979 if (argc == 0 || re->options == 0) 7980 readelf_usage(EXIT_FAILURE); 7981 7982 if (argc > 1) 7983 re->flags |= DISPLAY_FILENAME; 7984 7985 if (elf_version(EV_CURRENT) == EV_NONE) 7986 errx(EXIT_FAILURE, "ELF library initialization failed: %s", 7987 elf_errmsg(-1)); 7988 7989 cap_rights_init(&rights, CAP_FCNTL, CAP_FSTAT, CAP_MMAP_R, CAP_SEEK); 7990 fa = fileargs_init(argc, argv, O_RDONLY, 0, &rights, FA_OPEN); 7991 if (fa == NULL) 7992 err(1, "Unable to initialize casper fileargs"); 7993 7994 caph_cache_catpages(); 7995 if (caph_limit_stdio() < 0) { 7996 fileargs_free(fa); 7997 err(1, "Unable to limit stdio rights"); 7998 } 7999 if (caph_enter_casper() < 0) { 8000 fileargs_free(fa); 8001 err(1, "Unable to enter capability mode"); 8002 } 8003 8004 exit_code = EXIT_SUCCESS; 8005 for (i = 0; i < argc; i++) { 8006 re->filename = argv[i]; 8007 fd = fileargs_open(fa, re->filename); 8008 if (fd < 0) { 8009 warn("open %s failed", re->filename); 8010 exit_code = EXIT_FAILURE; 8011 } else { 8012 if (!dump_object(re, fd)) 8013 exit_code = EXIT_FAILURE; 8014 close(fd); 8015 } 8016 } 8017 8018 exit(exit_code); 8019 } 8020