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