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