1 /*- 2 * Copyright (c) 2007-2012 Kai Wang 3 * Copyright (c) 2003 David O'Brien. All rights reserved. 4 * Copyright (c) 2001 Jake Burkholder 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/param.h> 30 #include <sys/queue.h> 31 #include <sys/stat.h> 32 33 #include <ar.h> 34 #include <assert.h> 35 #include <err.h> 36 #include <fcntl.h> 37 #include <gelf.h> 38 #include <getopt.h> 39 #include <libelftc.h> 40 #include <inttypes.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <unistd.h> 45 46 #ifdef USE_LIBARCHIVE_AR 47 #include <archive.h> 48 #include <archive_entry.h> 49 #endif 50 51 #include "_elftc.h" 52 53 ELFTC_VCSID("$Id: elfdump.c 3584 2017-11-05 20:51:43Z jkoshy $"); 54 55 #if defined(ELFTC_NEED_ELF_NOTE_DEFINITION) 56 #include "native-elf-format.h" 57 #if ELFTC_CLASS == ELFCLASS32 58 typedef Elf32_Nhdr Elf_Note; 59 #else 60 typedef Elf64_Nhdr Elf_Note; 61 #endif 62 #endif 63 64 /* elfdump(1) options. */ 65 #define ED_DYN (1<<0) 66 #define ED_EHDR (1<<1) 67 #define ED_GOT (1<<2) 68 #define ED_HASH (1<<3) 69 #define ED_INTERP (1<<4) 70 #define ED_NOTE (1<<5) 71 #define ED_PHDR (1<<6) 72 #define ED_REL (1<<7) 73 #define ED_SHDR (1<<8) 74 #define ED_SYMTAB (1<<9) 75 #define ED_SYMVER (1<<10) 76 #define ED_CHECKSUM (1<<11) 77 #define ED_ALL ((1<<12)-1) 78 79 /* elfdump(1) run control flags. */ 80 #define SOLARIS_FMT (1<<0) 81 #define PRINT_FILENAME (1<<1) 82 #define PRINT_ARSYM (1<<2) 83 #define ONLY_ARSYM (1<<3) 84 85 /* Convenient print macro. */ 86 #define PRT(...) fprintf(ed->out, __VA_ARGS__) 87 88 /* Internal data structure for sections. */ 89 struct section { 90 const char *name; /* section name */ 91 Elf_Scn *scn; /* section scn */ 92 uint64_t off; /* section offset */ 93 uint64_t sz; /* section size */ 94 uint64_t entsize; /* section entsize */ 95 uint64_t align; /* section alignment */ 96 uint64_t type; /* section type */ 97 uint64_t flags; /* section flags */ 98 uint64_t addr; /* section virtual addr */ 99 uint32_t link; /* section link ndx */ 100 uint32_t info; /* section info ndx */ 101 }; 102 103 struct spec_name { 104 const char *name; 105 STAILQ_ENTRY(spec_name) sn_list; 106 }; 107 108 /* Structure encapsulates the global data for readelf(1). */ 109 struct elfdump { 110 FILE *out; /* output redirection. */ 111 const char *filename; /* current processing file. */ 112 const char *archive; /* archive name */ 113 int options; /* command line options. */ 114 int flags; /* run control flags. */ 115 Elf *elf; /* underlying ELF descriptor. */ 116 #ifndef USE_LIBARCHIVE_AR 117 Elf *ar; /* ar(1) archive descriptor. */ 118 #endif 119 GElf_Ehdr ehdr; /* ELF header. */ 120 int ec; /* ELF class. */ 121 size_t shnum; /* #sections. */ 122 struct section *sl; /* list of sections. */ 123 STAILQ_HEAD(, spec_name) snl; /* list of names specified by -N. */ 124 }; 125 126 /* Relocation entry. */ 127 struct rel_entry { 128 union { 129 GElf_Rel rel; 130 GElf_Rela rela; 131 } u_r; 132 const char *symn; 133 uint32_t type; 134 }; 135 136 #if defined(ELFTC_NEED_BYTEORDER_EXTENSIONS) 137 static __inline uint32_t 138 be32dec(const void *pp) 139 { 140 unsigned char const *p = (unsigned char const *)pp; 141 142 return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]); 143 } 144 145 static __inline uint32_t 146 le32dec(const void *pp) 147 { 148 unsigned char const *p = (unsigned char const *)pp; 149 150 return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]); 151 } 152 #endif 153 154 /* http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#tag_encodings */ 155 static const char * 156 d_tags(uint64_t tag) 157 { 158 static char unknown_buf[64]; 159 160 switch (tag) { 161 case DT_NULL: return "DT_NULL"; 162 case DT_NEEDED: return "DT_NEEDED"; 163 case DT_PLTRELSZ: return "DT_PLTRELSZ"; 164 case DT_PLTGOT: return "DT_PLTGOT"; 165 case DT_HASH: return "DT_HASH"; 166 case DT_STRTAB: return "DT_STRTAB"; 167 case DT_SYMTAB: return "DT_SYMTAB"; 168 case DT_RELA: return "DT_RELA"; 169 case DT_RELASZ: return "DT_RELASZ"; 170 case DT_RELAENT: return "DT_RELAENT"; 171 case DT_STRSZ: return "DT_STRSZ"; 172 case DT_SYMENT: return "DT_SYMENT"; 173 case DT_INIT: return "DT_INIT"; 174 case DT_FINI: return "DT_FINI"; 175 case DT_SONAME: return "DT_SONAME"; 176 case DT_RPATH: return "DT_RPATH"; 177 case DT_SYMBOLIC: return "DT_SYMBOLIC"; 178 case DT_REL: return "DT_REL"; 179 case DT_RELSZ: return "DT_RELSZ"; 180 case DT_RELENT: return "DT_RELENT"; 181 case DT_PLTREL: return "DT_PLTREL"; 182 case DT_DEBUG: return "DT_DEBUG"; 183 case DT_TEXTREL: return "DT_TEXTREL"; 184 case DT_JMPREL: return "DT_JMPREL"; 185 case DT_BIND_NOW: return "DT_BIND_NOW"; 186 case DT_INIT_ARRAY: return "DT_INIT_ARRAY"; 187 case DT_FINI_ARRAY: return "DT_FINI_ARRAY"; 188 case DT_INIT_ARRAYSZ: return "DT_INIT_ARRAYSZ"; 189 case DT_FINI_ARRAYSZ: return "DT_FINI_ARRAYSZ"; 190 case DT_RUNPATH: return "DT_RUNPATH"; 191 case DT_FLAGS: return "DT_FLAGS"; 192 case DT_PREINIT_ARRAY: return "DT_PREINIT_ARRAY"; /* XXX DT_ENCODING */ 193 case DT_PREINIT_ARRAYSZ:return "DT_PREINIT_ARRAYSZ"; 194 /* 0x6000000D - 0x6ffff000 operating system-specific semantics */ 195 case 0x6ffffdf5: return "DT_GNU_PRELINKED"; 196 case 0x6ffffdf6: return "DT_GNU_CONFLICTSZ"; 197 case 0x6ffffdf7: return "DT_GNU_LIBLISTSZ"; 198 case 0x6ffffdf8: return "DT_SUNW_CHECKSUM"; 199 case DT_PLTPADSZ: return "DT_PLTPADSZ"; 200 case DT_MOVEENT: return "DT_MOVEENT"; 201 case DT_MOVESZ: return "DT_MOVESZ"; 202 case 0x6ffffdfc: return "DT_FEATURE"; 203 case DT_POSFLAG_1: return "DT_POSFLAG_1"; 204 case DT_SYMINSZ: return "DT_SYMINSZ"; 205 case DT_SYMINENT: return "DT_SYMINENT (DT_VALRNGHI)"; 206 case DT_ADDRRNGLO: return "DT_ADDRRNGLO"; 207 case DT_GNU_HASH: return "DT_GNU_HASH"; 208 case 0x6ffffef8: return "DT_GNU_CONFLICT"; 209 case 0x6ffffef9: return "DT_GNU_LIBLIST"; 210 case 0x6ffffefa: return "DT_CONFIG"; 211 case 0x6ffffefb: return "DT_DEPAUDIT"; 212 case 0x6ffffefc: return "DT_AUDIT"; 213 case 0x6ffffefd: return "DT_PLTPAD"; 214 case 0x6ffffefe: return "DT_MOVETAB"; 215 case DT_SYMINFO: return "DT_SYMINFO (DT_ADDRRNGHI)"; 216 case DT_RELACOUNT: return "DT_RELACOUNT"; 217 case DT_RELCOUNT: return "DT_RELCOUNT"; 218 case DT_FLAGS_1: return "DT_FLAGS_1"; 219 case DT_VERDEF: return "DT_VERDEF"; 220 case DT_VERDEFNUM: return "DT_VERDEFNUM"; 221 case DT_VERNEED: return "DT_VERNEED"; 222 case DT_VERNEEDNUM: return "DT_VERNEEDNUM"; 223 case 0x6ffffff0: return "DT_GNU_VERSYM"; 224 /* 0x70000000 - 0x7fffffff processor-specific semantics */ 225 case 0x70000000: return "DT_IA_64_PLT_RESERVE"; 226 case DT_AUXILIARY: return "DT_AUXILIARY"; 227 case DT_USED: return "DT_USED"; 228 case DT_FILTER: return "DT_FILTER"; 229 } 230 231 snprintf(unknown_buf, sizeof(unknown_buf), 232 "<unknown: %#llx>", (unsigned long long)tag); 233 return (unknown_buf); 234 } 235 236 static const char * 237 e_machines(unsigned int mach) 238 { 239 static char machdesc[64]; 240 241 switch (mach) { 242 case EM_NONE: return "EM_NONE"; 243 case EM_M32: return "EM_M32"; 244 case EM_SPARC: return "EM_SPARC"; 245 case EM_386: return "EM_386"; 246 case EM_68K: return "EM_68K"; 247 case EM_88K: return "EM_88K"; 248 case EM_IAMCU: return "EM_IAMCU"; 249 case EM_860: return "EM_860"; 250 case EM_MIPS: return "EM_MIPS"; 251 case EM_PPC: return "EM_PPC"; 252 case EM_PPC64: return "EM_PPC64"; 253 case EM_ARM: return "EM_ARM"; 254 case EM_ALPHA: return "EM_ALPHA (legacy)"; 255 case EM_SPARCV9:return "EM_SPARCV9"; 256 case EM_IA_64: return "EM_IA_64"; 257 case EM_X86_64: return "EM_X86_64"; 258 case EM_AARCH64:return "EM_AARCH64"; 259 case EM_RISCV: return "EM_RISCV"; 260 } 261 snprintf(machdesc, sizeof(machdesc), 262 "(unknown machine) -- type 0x%x", mach); 263 return (machdesc); 264 } 265 266 static const char * 267 elf_type_str(unsigned int type) 268 { 269 static char s_type[32]; 270 271 switch (type) 272 { 273 case ET_NONE: return "ET_NONE"; 274 case ET_REL: return "ET_REL"; 275 case ET_EXEC: return "ET_EXEC"; 276 case ET_DYN: return "ET_DYN"; 277 case ET_CORE: return "ET_CORE"; 278 } 279 if (type >= ET_LOPROC) 280 snprintf(s_type, sizeof(s_type), "<proc: %#x>", type); 281 else if (type >= ET_LOOS && type <= ET_HIOS) 282 snprintf(s_type, sizeof(s_type), "<os: %#x>", type); 283 else 284 snprintf(s_type, sizeof(s_type), "<unknown: %#x", type); 285 return (s_type); 286 } 287 288 static const char * 289 elf_version_str(unsigned int ver) 290 { 291 static char s_ver[32]; 292 293 switch (ver) { 294 case EV_NONE: return "EV_NONE"; 295 case EV_CURRENT: return "EV_CURRENT"; 296 } 297 snprintf(s_ver, sizeof(s_ver), "<unknown: %#x>", ver); 298 return (s_ver); 299 } 300 301 static const char * 302 elf_class_str(unsigned int class) 303 { 304 static char s_class[32]; 305 306 switch (class) { 307 case ELFCLASSNONE: return "ELFCLASSNONE"; 308 case ELFCLASS32: return "ELFCLASS32"; 309 case ELFCLASS64: return "ELFCLASS64"; 310 } 311 snprintf(s_class, sizeof(s_class), "<unknown: %#x>", class); 312 return (s_class); 313 } 314 315 static const char * 316 elf_data_str(unsigned int data) 317 { 318 static char s_data[32]; 319 320 switch (data) { 321 case ELFDATANONE: return "ELFDATANONE"; 322 case ELFDATA2LSB: return "ELFDATA2LSB"; 323 case ELFDATA2MSB: return "ELFDATA2MSB"; 324 } 325 snprintf(s_data, sizeof(s_data), "<unknown: %#x>", data); 326 return (s_data); 327 } 328 329 static const char *ei_abis[256] = { 330 "ELFOSABI_NONE", "ELFOSABI_HPUX", "ELFOSABI_NETBSD", "ELFOSABI_LINUX", 331 "ELFOSABI_HURD", "ELFOSABI_86OPEN", "ELFOSABI_SOLARIS", "ELFOSABI_AIX", 332 "ELFOSABI_IRIX", "ELFOSABI_FREEBSD", "ELFOSABI_TRU64", 333 "ELFOSABI_MODESTO", "ELFOSABI_OPENBSD", 334 [17] = "ELFOSABI_CLOUDABI", 335 [64] = "ELFOSABI_ARM_AEABI", 336 [97] = "ELFOSABI_ARM", 337 [255] = "ELFOSABI_STANDALONE" 338 }; 339 340 static const char * 341 elf_phdr_type_str(unsigned int type) 342 { 343 static char s_type[32]; 344 345 switch (type) { 346 case PT_NULL: return "PT_NULL"; 347 case PT_LOAD: return "PT_LOAD"; 348 case PT_DYNAMIC: return "PT_DYNAMIC"; 349 case PT_INTERP: return "PT_INTERP"; 350 case PT_NOTE: return "PT_NOTE"; 351 case PT_SHLIB: return "PT_SHLIB"; 352 case PT_PHDR: return "PT_PHDR"; 353 case PT_TLS: return "PT_TLS"; 354 case PT_GNU_EH_FRAME: return "PT_GNU_EH_FRAME"; 355 case PT_GNU_STACK: return "PT_GNU_STACK"; 356 case PT_GNU_RELRO: return "PT_GNU_RELRO"; 357 } 358 snprintf(s_type, sizeof(s_type), "<unknown: %#x>", type); 359 return (s_type); 360 } 361 362 static const char *p_flags[] = { 363 "", "PF_X", "PF_W", "PF_X|PF_W", "PF_R", "PF_X|PF_R", "PF_W|PF_R", 364 "PF_X|PF_W|PF_R" 365 }; 366 367 static const char * 368 sh_name(struct elfdump *ed, int ndx) 369 { 370 static char num[10]; 371 372 switch (ndx) { 373 case SHN_UNDEF: return "UNDEF"; 374 case SHN_ABS: return "ABS"; 375 case SHN_COMMON: return "COMMON"; 376 default: 377 if ((uint64_t)ndx < ed->shnum) 378 return (ed->sl[ndx].name); 379 else { 380 snprintf(num, sizeof(num), "%d", ndx); 381 return (num); 382 } 383 } 384 } 385 386 /* http://www.sco.com/developers/gabi/latest/ch4.sheader.html#sh_type */ 387 static const char * 388 sh_types(uint64_t mach, uint64_t sht) { 389 static char unknown_buf[64]; 390 391 if (sht < 0x60000000) { 392 switch (sht) { 393 case SHT_NULL: return "SHT_NULL"; 394 case SHT_PROGBITS: return "SHT_PROGBITS"; 395 case SHT_SYMTAB: return "SHT_SYMTAB"; 396 case SHT_STRTAB: return "SHT_STRTAB"; 397 case SHT_RELA: return "SHT_RELA"; 398 case SHT_HASH: return "SHT_HASH"; 399 case SHT_DYNAMIC: return "SHT_DYNAMIC"; 400 case SHT_NOTE: return "SHT_NOTE"; 401 case SHT_NOBITS: return "SHT_NOBITS"; 402 case SHT_REL: return "SHT_REL"; 403 case SHT_SHLIB: return "SHT_SHLIB"; 404 case SHT_DYNSYM: return "SHT_DYNSYM"; 405 case SHT_INIT_ARRAY: return "SHT_INIT_ARRAY"; 406 case SHT_FINI_ARRAY: return "SHT_FINI_ARRAY"; 407 case SHT_PREINIT_ARRAY: return "SHT_PREINIT_ARRAY"; 408 case SHT_GROUP: return "SHT_GROUP"; 409 case SHT_SYMTAB_SHNDX: return "SHT_SYMTAB_SHNDX"; 410 } 411 } else if (sht < 0x70000000) { 412 /* 0x60000000-0x6fffffff operating system-specific semantics */ 413 switch (sht) { 414 case 0x6ffffff0: return "XXX:VERSYM"; 415 case SHT_SUNW_dof: return "SHT_SUNW_dof"; 416 case SHT_GNU_HASH: return "SHT_GNU_HASH"; 417 case 0x6ffffff7: return "SHT_GNU_LIBLIST"; 418 case 0x6ffffffc: return "XXX:VERDEF"; 419 case SHT_SUNW_verdef: return "SHT_SUNW(GNU)_verdef"; 420 case SHT_SUNW_verneed: return "SHT_SUNW(GNU)_verneed"; 421 case SHT_SUNW_versym: return "SHT_SUNW(GNU)_versym"; 422 } 423 } else if (sht < 0x80000000) { 424 /* 0x70000000 - 0x7fffffff processor-specific semantics */ 425 switch (mach) { 426 case EM_ARM: 427 switch (sht) { 428 case SHT_ARM_EXIDX: return "SHT_ARM_EXIDX"; 429 case SHT_ARM_PREEMPTMAP: return "SHT_ARM_PREEMPTMAP"; 430 case SHT_ARM_ATTRIBUTES: return "SHT_ARM_ATTRIBUTES"; 431 case SHT_ARM_DEBUGOVERLAY: 432 return "SHT_ARM_DEBUGOVERLAY"; 433 case SHT_ARM_OVERLAYSECTION: 434 return "SHT_ARM_OVERLAYSECTION"; 435 } 436 break; 437 case EM_IA_64: 438 switch (sht) { 439 case 0x70000000: return "SHT_IA_64_EXT"; 440 case 0x70000001: return "SHT_IA_64_UNWIND"; 441 } 442 break; 443 case EM_MIPS: 444 switch (sht) { 445 case SHT_MIPS_REGINFO: return "SHT_MIPS_REGINFO"; 446 case SHT_MIPS_OPTIONS: return "SHT_MIPS_OPTIONS"; 447 case SHT_MIPS_ABIFLAGS: return "SHT_MIPS_ABIFLAGS"; 448 } 449 break; 450 } 451 switch (sht) { 452 case 0x7ffffffd: return "XXX:AUXILIARY"; 453 case 0x7fffffff: return "XXX:FILTER"; 454 } 455 } 456 /* 0x80000000 - 0xffffffff application programs */ 457 458 snprintf(unknown_buf, sizeof(unknown_buf), 459 "<unknown: %#llx>", (unsigned long long)sht); 460 return (unknown_buf); 461 } 462 463 /* 464 * Define known section flags. These flags are defined in the order 465 * they are to be printed out. 466 */ 467 #define DEFINE_SHFLAGS() \ 468 DEFINE_SHF(WRITE) \ 469 DEFINE_SHF(ALLOC) \ 470 DEFINE_SHF(EXECINSTR) \ 471 DEFINE_SHF(MERGE) \ 472 DEFINE_SHF(STRINGS) \ 473 DEFINE_SHF(INFO_LINK) \ 474 DEFINE_SHF(LINK_ORDER) \ 475 DEFINE_SHF(OS_NONCONFORMING) \ 476 DEFINE_SHF(GROUP) \ 477 DEFINE_SHF(TLS) \ 478 DEFINE_SHF(COMPRESSED) 479 480 #undef DEFINE_SHF 481 #define DEFINE_SHF(F) "SHF_" #F "|" 482 #define ALLSHFLAGS DEFINE_SHFLAGS() 483 484 static const char * 485 sh_flags(uint64_t shf) 486 { 487 static char flg[sizeof(ALLSHFLAGS)+1]; 488 489 flg[0] = '\0'; 490 491 #undef DEFINE_SHF 492 #define DEFINE_SHF(N) \ 493 if (shf & SHF_##N) \ 494 strcat(flg, "SHF_" #N "|"); \ 495 496 DEFINE_SHFLAGS() 497 498 flg[strlen(flg) - 1] = '\0'; /* Remove the trailing "|". */ 499 500 return (flg); 501 } 502 503 static const char * 504 st_type(unsigned int mach, unsigned int type) 505 { 506 static char s_type[32]; 507 508 switch (type) { 509 case STT_NOTYPE: return "STT_NOTYPE"; 510 case STT_OBJECT: return "STT_OBJECT"; 511 case STT_FUNC: return "STT_FUNC"; 512 case STT_SECTION: return "STT_SECTION"; 513 case STT_FILE: return "STT_FILE"; 514 case STT_COMMON: return "STT_COMMON"; 515 case STT_TLS: return "STT_TLS"; 516 case 13: 517 if (mach == EM_SPARCV9) 518 return "STT_SPARC_REGISTER"; 519 break; 520 } 521 snprintf(s_type, sizeof(s_type), "<unknown: %#x>", type); 522 return (s_type); 523 } 524 525 static const char * 526 st_type_S(unsigned int type) 527 { 528 static char s_type[32]; 529 530 switch (type) { 531 case STT_NOTYPE: return "NOTY"; 532 case STT_OBJECT: return "OBJT"; 533 case STT_FUNC: return "FUNC"; 534 case STT_SECTION: return "SECT"; 535 case STT_FILE: return "FILE"; 536 } 537 snprintf(s_type, sizeof(s_type), "<unknown: %#x>", type); 538 return (s_type); 539 } 540 541 static const char * 542 st_bindings(unsigned int sbind) 543 { 544 static char s_sbind[32]; 545 546 switch (sbind) { 547 case STB_LOCAL: return "STB_LOCAL"; 548 case STB_GLOBAL: return "STB_GLOBAL"; 549 case STB_WEAK: return "STB_WEAK"; 550 case STB_GNU_UNIQUE: return "STB_GNU_UNIQUE"; 551 default: 552 if (sbind >= STB_LOOS && sbind <= STB_HIOS) 553 return "OS"; 554 else if (sbind >= STB_LOPROC && sbind <= STB_HIPROC) 555 return "PROC"; 556 else 557 snprintf(s_sbind, sizeof(s_sbind), "<unknown: %#x>", 558 sbind); 559 return (s_sbind); 560 } 561 } 562 563 static const char * 564 st_bindings_S(unsigned int sbind) 565 { 566 static char s_sbind[32]; 567 568 switch (sbind) { 569 case STB_LOCAL: return "LOCL"; 570 case STB_GLOBAL: return "GLOB"; 571 case STB_WEAK: return "WEAK"; 572 case STB_GNU_UNIQUE: return "UNIQ"; 573 default: 574 if (sbind >= STB_LOOS && sbind <= STB_HIOS) 575 return "OS"; 576 else if (sbind >= STB_LOPROC && sbind <= STB_HIPROC) 577 return "PROC"; 578 else 579 snprintf(s_sbind, sizeof(s_sbind), "<%#x>", 580 sbind); 581 return (s_sbind); 582 } 583 } 584 585 static unsigned char st_others[] = { 586 'D', 'I', 'H', 'P' 587 }; 588 589 static void add_name(struct elfdump *ed, const char *name); 590 static void elf_print_object(struct elfdump *ed); 591 static void elf_print_elf(struct elfdump *ed); 592 static void elf_print_ehdr(struct elfdump *ed); 593 static void elf_print_phdr(struct elfdump *ed); 594 static void elf_print_shdr(struct elfdump *ed); 595 static void elf_print_symtab(struct elfdump *ed, int i); 596 static void elf_print_symtabs(struct elfdump *ed); 597 static void elf_print_symver(struct elfdump *ed); 598 static void elf_print_verdef(struct elfdump *ed, struct section *s); 599 static void elf_print_verneed(struct elfdump *ed, struct section *s); 600 static void elf_print_interp(struct elfdump *ed); 601 static void elf_print_dynamic(struct elfdump *ed); 602 static void elf_print_rel_entry(struct elfdump *ed, struct section *s, 603 int j, struct rel_entry *r); 604 static void elf_print_rela(struct elfdump *ed, struct section *s, 605 Elf_Data *data); 606 static void elf_print_rel(struct elfdump *ed, struct section *s, 607 Elf_Data *data); 608 static void elf_print_reloc(struct elfdump *ed); 609 static void elf_print_got(struct elfdump *ed); 610 static void elf_print_got_section(struct elfdump *ed, struct section *s); 611 static void elf_print_note(struct elfdump *ed); 612 static void elf_print_svr4_hash(struct elfdump *ed, struct section *s); 613 static void elf_print_svr4_hash64(struct elfdump *ed, struct section *s); 614 static void elf_print_gnu_hash(struct elfdump *ed, struct section *s); 615 static void elf_print_hash(struct elfdump *ed); 616 static void elf_print_checksum(struct elfdump *ed); 617 static void find_gotrel(struct elfdump *ed, struct section *gs, 618 struct rel_entry *got); 619 static struct spec_name *find_name(struct elfdump *ed, const char *name); 620 static int get_ent_count(const struct section *s, int *ent_count); 621 static const char *get_symbol_name(struct elfdump *ed, uint32_t symtab, int i); 622 static const char *get_string(struct elfdump *ed, int strtab, size_t off); 623 static void get_versym(struct elfdump *ed, int i, uint16_t **vs, int *nvs); 624 static void load_sections(struct elfdump *ed); 625 static void unload_sections(struct elfdump *ed); 626 static void usage(void); 627 #ifdef USE_LIBARCHIVE_AR 628 static int ac_detect_ar(int fd); 629 static void ac_print_ar(struct elfdump *ed, int fd); 630 #else 631 static void elf_print_ar(struct elfdump *ed, int fd); 632 #endif /* USE_LIBARCHIVE_AR */ 633 634 static struct option elfdump_longopts[] = 635 { 636 { "help", no_argument, NULL, 'H' }, 637 { "version", no_argument, NULL, 'V' }, 638 { NULL, 0, NULL, 0 } 639 }; 640 641 int 642 main(int ac, char **av) 643 { 644 struct elfdump *ed, ed_storage; 645 struct spec_name *sn; 646 int ch, i; 647 648 ed = &ed_storage; 649 memset(ed, 0, sizeof(*ed)); 650 STAILQ_INIT(&ed->snl); 651 ed->out = stdout; 652 while ((ch = getopt_long(ac, av, "acdeiGHhknN:prsSvVw:", 653 elfdump_longopts, NULL)) != -1) 654 switch (ch) { 655 case 'a': 656 ed->options = ED_ALL; 657 break; 658 case 'c': 659 ed->options |= ED_SHDR; 660 break; 661 case 'd': 662 ed->options |= ED_DYN; 663 break; 664 case 'e': 665 ed->options |= ED_EHDR; 666 break; 667 case 'i': 668 ed->options |= ED_INTERP; 669 break; 670 case 'G': 671 ed->options |= ED_GOT; 672 break; 673 case 'h': 674 ed->options |= ED_HASH; 675 break; 676 case 'k': 677 ed->options |= ED_CHECKSUM; 678 break; 679 case 'n': 680 ed->options |= ED_NOTE; 681 break; 682 case 'N': 683 add_name(ed, optarg); 684 break; 685 case 'p': 686 ed->options |= ED_PHDR; 687 break; 688 case 'r': 689 ed->options |= ED_REL; 690 break; 691 case 's': 692 ed->options |= ED_SYMTAB; 693 break; 694 case 'S': 695 ed->flags |= SOLARIS_FMT; 696 break; 697 case 'v': 698 ed->options |= ED_SYMVER; 699 break; 700 case 'V': 701 (void) printf("%s (%s)\n", ELFTC_GETPROGNAME(), 702 elftc_version()); 703 exit(EXIT_SUCCESS); 704 break; 705 case 'w': 706 if ((ed->out = fopen(optarg, "w")) == NULL) 707 err(EXIT_FAILURE, "%s", optarg); 708 break; 709 case '?': 710 case 'H': 711 default: 712 usage(); 713 } 714 715 ac -= optind; 716 av += optind; 717 718 if (ed->options == 0) 719 ed->options = ED_ALL; 720 sn = NULL; 721 if (ed->options & ED_SYMTAB && 722 (STAILQ_EMPTY(&ed->snl) || (sn = find_name(ed, "ARSYM")) != NULL)) { 723 ed->flags |= PRINT_ARSYM; 724 if (sn != NULL) { 725 STAILQ_REMOVE(&ed->snl, sn, spec_name, sn_list); 726 if (STAILQ_EMPTY(&ed->snl)) 727 ed->flags |= ONLY_ARSYM; 728 } 729 } 730 if (ac == 0) 731 usage(); 732 if (ac > 1) 733 ed->flags |= PRINT_FILENAME; 734 if (elf_version(EV_CURRENT) == EV_NONE) 735 errx(EXIT_FAILURE, "ELF library initialization failed: %s", 736 elf_errmsg(-1)); 737 738 for (i = 0; i < ac; i++) { 739 ed->filename = av[i]; 740 ed->archive = NULL; 741 elf_print_object(ed); 742 } 743 744 exit(EXIT_SUCCESS); 745 } 746 747 #ifdef USE_LIBARCHIVE_AR 748 749 /* Archive symbol table entry. */ 750 struct arsym_entry { 751 char *sym_name; 752 size_t off; 753 }; 754 755 /* 756 * Convenient wrapper for general libarchive error handling. 757 */ 758 #define AC(CALL) do { \ 759 if ((CALL)) { \ 760 warnx("%s", archive_error_string(a)); \ 761 return; \ 762 } \ 763 } while (0) 764 765 /* 766 * Detect an ar(1) archive using libarchive(3). 767 */ 768 static int 769 ac_detect_ar(int fd) 770 { 771 struct archive *a; 772 struct archive_entry *entry; 773 int r; 774 775 r = -1; 776 if ((a = archive_read_new()) == NULL) 777 return (0); 778 archive_read_support_format_ar(a); 779 if (archive_read_open_fd(a, fd, 10240) == ARCHIVE_OK) 780 r = archive_read_next_header(a, &entry); 781 archive_read_close(a); 782 archive_read_free(a); 783 784 return (r == ARCHIVE_OK); 785 } 786 787 /* 788 * Dump an ar(1) archive using libarchive(3). 789 */ 790 static void 791 ac_print_ar(struct elfdump *ed, int fd) 792 { 793 struct archive *a; 794 struct archive_entry *entry; 795 struct arsym_entry *arsym; 796 const char *name; 797 char idx[10], *b; 798 void *buff; 799 size_t size; 800 uint32_t cnt, i; 801 int r; 802 803 if (lseek(fd, 0, SEEK_SET) == -1) 804 err(EXIT_FAILURE, "lseek failed"); 805 if ((a = archive_read_new()) == NULL) 806 errx(EXIT_FAILURE, "%s", archive_error_string(a)); 807 archive_read_support_format_ar(a); 808 AC(archive_read_open_fd(a, fd, 10240)); 809 for(;;) { 810 r = archive_read_next_header(a, &entry); 811 if (r == ARCHIVE_FATAL) 812 errx(EXIT_FAILURE, "%s", archive_error_string(a)); 813 if (r == ARCHIVE_EOF) 814 break; 815 if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY) 816 warnx("%s", archive_error_string(a)); 817 if (r == ARCHIVE_RETRY) 818 continue; 819 name = archive_entry_pathname(entry); 820 size = archive_entry_size(entry); 821 if (size == 0) 822 continue; 823 if ((buff = malloc(size)) == NULL) { 824 warn("malloc failed"); 825 continue; 826 } 827 if (archive_read_data(a, buff, size) != (ssize_t)size) { 828 warnx("%s", archive_error_string(a)); 829 free(buff); 830 continue; 831 } 832 833 /* 834 * Note that when processing arsym via libarchive, there is 835 * no way to tell which member a certain symbol belongs to, 836 * since we can not just "lseek" to a member offset and read 837 * the member header. 838 */ 839 if (!strcmp(name, "/") && ed->flags & PRINT_ARSYM) { 840 b = buff; 841 cnt = be32dec(b); 842 if (cnt == 0) { 843 free(buff); 844 continue; 845 } 846 arsym = calloc(cnt, sizeof(*arsym)); 847 if (arsym == NULL) 848 err(EXIT_FAILURE, "calloc failed"); 849 b += sizeof(uint32_t); 850 for (i = 0; i < cnt; i++) { 851 arsym[i].off = be32dec(b); 852 b += sizeof(uint32_t); 853 } 854 for (i = 0; i < cnt; i++) { 855 arsym[i].sym_name = b; 856 b += strlen(b) + 1; 857 } 858 if (ed->flags & SOLARIS_FMT) { 859 PRT("\nSymbol Table: (archive)\n"); 860 PRT(" index offset symbol\n"); 861 } else 862 PRT("\nsymbol table (archive):\n"); 863 for (i = 0; i < cnt; i++) { 864 if (ed->flags & SOLARIS_FMT) { 865 snprintf(idx, sizeof(idx), "[%d]", i); 866 PRT("%10s ", idx); 867 PRT("0x%8.8jx ", 868 (uintmax_t)arsym[i].off); 869 PRT("%s\n", arsym[i].sym_name); 870 } else { 871 PRT("\nentry: %d\n", i); 872 PRT("\toffset: %#jx\n", 873 (uintmax_t)arsym[i].off); 874 PRT("\tsymbol: %s\n", 875 arsym[i].sym_name); 876 } 877 } 878 free(arsym); 879 free(buff); 880 /* No need to continue if we only dump ARSYM. */ 881 if (ed->flags & ONLY_ARSYM) { 882 AC(archive_read_close(a)); 883 AC(archive_read_free(a)); 884 return; 885 } 886 continue; 887 } 888 if ((ed->elf = elf_memory(buff, size)) == NULL) { 889 warnx("elf_memroy() failed: %s", 890 elf_errmsg(-1)); 891 free(buff); 892 continue; 893 } 894 /* Skip non-ELF member. */ 895 if (elf_kind(ed->elf) == ELF_K_ELF) { 896 printf("\n%s(%s):\n", ed->archive, name); 897 elf_print_elf(ed); 898 } 899 elf_end(ed->elf); 900 free(buff); 901 } 902 AC(archive_read_close(a)); 903 AC(archive_read_free(a)); 904 } 905 906 #else /* USE_LIBARCHIVE_AR */ 907 908 /* 909 * Dump an ar(1) archive. 910 */ 911 static void 912 elf_print_ar(struct elfdump *ed, int fd) 913 { 914 Elf *e; 915 Elf_Arhdr *arh; 916 Elf_Arsym *arsym; 917 Elf_Cmd cmd; 918 char idx[21]; 919 size_t cnt, i; 920 921 ed->ar = ed->elf; 922 923 if (ed->flags & PRINT_ARSYM) { 924 cnt = 0; 925 if ((arsym = elf_getarsym(ed->ar, &cnt)) == NULL) { 926 warnx("elf_getarsym failed: %s", elf_errmsg(-1)); 927 goto print_members; 928 } 929 if (cnt == 0) 930 goto print_members; 931 if (ed->flags & SOLARIS_FMT) { 932 PRT("\nSymbol Table: (archive)\n"); 933 PRT(" index offset member name and symbol\n"); 934 } else 935 PRT("\nsymbol table (archive):\n"); 936 for (i = 0; i < cnt - 1; i++) { 937 if (elf_rand(ed->ar, arsym[i].as_off) != 938 arsym[i].as_off) { 939 warnx("elf_rand failed: %s", elf_errmsg(-1)); 940 break; 941 } 942 if ((e = elf_begin(fd, ELF_C_READ, ed->ar)) == NULL) { 943 warnx("elf_begin failed: %s", elf_errmsg(-1)); 944 break; 945 } 946 if ((arh = elf_getarhdr(e)) == NULL) { 947 warnx("elf_getarhdr failed: %s", 948 elf_errmsg(-1)); 949 break; 950 } 951 if (ed->flags & SOLARIS_FMT) { 952 snprintf(idx, sizeof(idx), "[%zu]", i); 953 PRT("%10s ", idx); 954 PRT("0x%8.8jx ", 955 (uintmax_t)arsym[i].as_off); 956 PRT("(%s):%s\n", arh->ar_name, 957 arsym[i].as_name); 958 } else { 959 PRT("\nentry: %zu\n", i); 960 PRT("\toffset: %#jx\n", 961 (uintmax_t)arsym[i].as_off); 962 PRT("\tmember: %s\n", arh->ar_name); 963 PRT("\tsymbol: %s\n", arsym[i].as_name); 964 } 965 elf_end(e); 966 } 967 968 /* No need to continue if we only dump ARSYM. */ 969 if (ed->flags & ONLY_ARSYM) 970 return; 971 } 972 973 print_members: 974 975 /* Rewind the archive. */ 976 if (elf_rand(ed->ar, SARMAG) != SARMAG) { 977 warnx("elf_rand failed: %s", elf_errmsg(-1)); 978 return; 979 } 980 981 /* Dump each member of the archive. */ 982 cmd = ELF_C_READ; 983 while ((ed->elf = elf_begin(fd, cmd, ed->ar)) != NULL) { 984 /* Skip non-ELF member. */ 985 if (elf_kind(ed->elf) == ELF_K_ELF) { 986 if ((arh = elf_getarhdr(ed->elf)) == NULL) { 987 warnx("elf_getarhdr failed: %s", 988 elf_errmsg(-1)); 989 break; 990 } 991 printf("\n%s(%s):\n", ed->archive, arh->ar_name); 992 elf_print_elf(ed); 993 } 994 cmd = elf_next(ed->elf); 995 elf_end(ed->elf); 996 } 997 } 998 999 #endif /* USE_LIBARCHIVE_AR */ 1000 1001 /* 1002 * Dump an object. (ELF object or ar(1) archive) 1003 */ 1004 static void 1005 elf_print_object(struct elfdump *ed) 1006 { 1007 int fd; 1008 1009 if ((fd = open(ed->filename, O_RDONLY)) == -1) { 1010 warn("open %s failed", ed->filename); 1011 return; 1012 } 1013 1014 #ifdef USE_LIBARCHIVE_AR 1015 if (ac_detect_ar(fd)) { 1016 ed->archive = ed->filename; 1017 ac_print_ar(ed, fd); 1018 return; 1019 } 1020 #endif /* USE_LIBARCHIVE_AR */ 1021 1022 if ((ed->elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) { 1023 warnx("elf_begin() failed: %s", elf_errmsg(-1)); 1024 return; 1025 } 1026 1027 switch (elf_kind(ed->elf)) { 1028 case ELF_K_NONE: 1029 warnx("Not an ELF file."); 1030 return; 1031 case ELF_K_ELF: 1032 if (ed->flags & PRINT_FILENAME) 1033 printf("\n%s:\n", ed->filename); 1034 elf_print_elf(ed); 1035 break; 1036 case ELF_K_AR: 1037 #ifndef USE_LIBARCHIVE_AR 1038 ed->archive = ed->filename; 1039 elf_print_ar(ed, fd); 1040 #endif 1041 break; 1042 default: 1043 warnx("Internal: libelf returned unknown elf kind."); 1044 return; 1045 } 1046 1047 elf_end(ed->elf); 1048 } 1049 1050 /* 1051 * Dump an ELF object. 1052 */ 1053 static void 1054 elf_print_elf(struct elfdump *ed) 1055 { 1056 1057 if (gelf_getehdr(ed->elf, &ed->ehdr) == NULL) { 1058 warnx("gelf_getehdr failed: %s", elf_errmsg(-1)); 1059 return; 1060 } 1061 if ((ed->ec = gelf_getclass(ed->elf)) == ELFCLASSNONE) { 1062 warnx("gelf_getclass failed: %s", elf_errmsg(-1)); 1063 return; 1064 } 1065 1066 if (ed->options & (ED_SHDR | ED_DYN | ED_REL | ED_GOT | ED_SYMTAB | 1067 ED_SYMVER | ED_NOTE | ED_HASH)) 1068 load_sections(ed); 1069 1070 if (ed->options & ED_EHDR) 1071 elf_print_ehdr(ed); 1072 if (ed->options & ED_PHDR) 1073 elf_print_phdr(ed); 1074 if (ed->options & ED_INTERP) 1075 elf_print_interp(ed); 1076 if (ed->options & ED_SHDR) 1077 elf_print_shdr(ed); 1078 if (ed->options & ED_DYN) 1079 elf_print_dynamic(ed); 1080 if (ed->options & ED_REL) 1081 elf_print_reloc(ed); 1082 if (ed->options & ED_GOT) 1083 elf_print_got(ed); 1084 if (ed->options & ED_SYMTAB) 1085 elf_print_symtabs(ed); 1086 if (ed->options & ED_SYMVER) 1087 elf_print_symver(ed); 1088 if (ed->options & ED_NOTE) 1089 elf_print_note(ed); 1090 if (ed->options & ED_HASH) 1091 elf_print_hash(ed); 1092 if (ed->options & ED_CHECKSUM) 1093 elf_print_checksum(ed); 1094 1095 unload_sections(ed); 1096 } 1097 1098 /* 1099 * Read the section headers from ELF object and store them in the 1100 * internal cache. 1101 */ 1102 static void 1103 load_sections(struct elfdump *ed) 1104 { 1105 struct section *s; 1106 const char *name; 1107 Elf_Scn *scn; 1108 GElf_Shdr sh; 1109 size_t shstrndx, ndx; 1110 int elferr; 1111 1112 assert(ed->sl == NULL); 1113 1114 if (!elf_getshnum(ed->elf, &ed->shnum)) { 1115 warnx("elf_getshnum failed: %s", elf_errmsg(-1)); 1116 return; 1117 } 1118 if (ed->shnum == 0) 1119 return; 1120 if ((ed->sl = calloc(ed->shnum, sizeof(*ed->sl))) == NULL) 1121 err(EXIT_FAILURE, "calloc failed"); 1122 if (!elf_getshstrndx(ed->elf, &shstrndx)) { 1123 warnx("elf_getshstrndx failed: %s", elf_errmsg(-1)); 1124 return; 1125 } 1126 if ((scn = elf_getscn(ed->elf, 0)) == NULL) { 1127 warnx("elf_getscn failed: %s", elf_errmsg(-1)); 1128 return; 1129 } 1130 (void) elf_errno(); 1131 do { 1132 if (gelf_getshdr(scn, &sh) == NULL) { 1133 warnx("gelf_getshdr failed: %s", elf_errmsg(-1)); 1134 (void) elf_errno(); 1135 continue; 1136 } 1137 if ((name = elf_strptr(ed->elf, shstrndx, sh.sh_name)) == NULL) { 1138 (void) elf_errno(); 1139 name = "ERROR"; 1140 } 1141 if ((ndx = elf_ndxscn(scn)) == SHN_UNDEF) 1142 if ((elferr = elf_errno()) != 0) { 1143 warnx("elf_ndxscn failed: %s", 1144 elf_errmsg(elferr)); 1145 continue; 1146 } 1147 if (ndx >= ed->shnum) { 1148 warnx("section index of '%s' out of range", name); 1149 continue; 1150 } 1151 s = &ed->sl[ndx]; 1152 s->name = name; 1153 s->scn = scn; 1154 s->off = sh.sh_offset; 1155 s->sz = sh.sh_size; 1156 s->entsize = sh.sh_entsize; 1157 s->align = sh.sh_addralign; 1158 s->type = sh.sh_type; 1159 s->flags = sh.sh_flags; 1160 s->addr = sh.sh_addr; 1161 s->link = sh.sh_link; 1162 s->info = sh.sh_info; 1163 } while ((scn = elf_nextscn(ed->elf, scn)) != NULL); 1164 elferr = elf_errno(); 1165 if (elferr != 0) 1166 warnx("elf_nextscn failed: %s", elf_errmsg(elferr)); 1167 } 1168 1169 /* 1170 * Release section related resources. 1171 */ 1172 static void 1173 unload_sections(struct elfdump *ed) 1174 { 1175 if (ed->sl != NULL) { 1176 free(ed->sl); 1177 ed->sl = NULL; 1178 } 1179 } 1180 1181 /* 1182 * Add a name to the '-N' name list. 1183 */ 1184 static void 1185 add_name(struct elfdump *ed, const char *name) 1186 { 1187 struct spec_name *sn; 1188 1189 if (find_name(ed, name)) 1190 return; 1191 if ((sn = malloc(sizeof(*sn))) == NULL) { 1192 warn("malloc failed"); 1193 return; 1194 } 1195 sn->name = name; 1196 STAILQ_INSERT_TAIL(&ed->snl, sn, sn_list); 1197 } 1198 1199 /* 1200 * Lookup a name in the '-N' name list. 1201 */ 1202 static struct spec_name * 1203 find_name(struct elfdump *ed, const char *name) 1204 { 1205 struct spec_name *sn; 1206 1207 STAILQ_FOREACH(sn, &ed->snl, sn_list) { 1208 if (!strcmp(sn->name, name)) 1209 return (sn); 1210 } 1211 1212 return (NULL); 1213 } 1214 1215 /* 1216 * Retrieve the name of a symbol using the section index of the symbol 1217 * table and the index of the symbol within that table. 1218 */ 1219 static const char * 1220 get_symbol_name(struct elfdump *ed, uint32_t symtab, int i) 1221 { 1222 static char sname[64]; 1223 struct section *s; 1224 const char *name; 1225 GElf_Sym sym; 1226 Elf_Data *data; 1227 int elferr; 1228 1229 if (symtab >= ed->shnum) 1230 return (""); 1231 s = &ed->sl[symtab]; 1232 if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM) 1233 return (""); 1234 (void) elf_errno(); 1235 if ((data = elf_getdata(s->scn, NULL)) == NULL) { 1236 elferr = elf_errno(); 1237 if (elferr != 0) 1238 warnx("elf_getdata failed: %s", elf_errmsg(elferr)); 1239 return (""); 1240 } 1241 if (gelf_getsym(data, i, &sym) != &sym) 1242 return (""); 1243 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION) { 1244 if (sym.st_shndx < ed->shnum) { 1245 snprintf(sname, sizeof(sname), "%s (section)", 1246 ed->sl[sym.st_shndx].name); 1247 return (sname); 1248 } else 1249 return (""); 1250 } 1251 if ((name = elf_strptr(ed->elf, s->link, sym.st_name)) == NULL) 1252 return (""); 1253 1254 return (name); 1255 } 1256 1257 /* 1258 * Retrieve a string using string table section index and the string offset. 1259 */ 1260 static const char* 1261 get_string(struct elfdump *ed, int strtab, size_t off) 1262 { 1263 const char *name; 1264 1265 if ((name = elf_strptr(ed->elf, strtab, off)) == NULL) 1266 return (""); 1267 1268 return (name); 1269 } 1270 1271 /* 1272 * Dump the ELF Executable Header. 1273 */ 1274 static void 1275 elf_print_ehdr(struct elfdump *ed) 1276 { 1277 1278 if (!STAILQ_EMPTY(&ed->snl)) 1279 return; 1280 1281 if (ed->flags & SOLARIS_FMT) { 1282 PRT("\nELF Header\n"); 1283 PRT(" ei_magic: { %#x, %c, %c, %c }\n", 1284 ed->ehdr.e_ident[0], ed->ehdr.e_ident[1], 1285 ed->ehdr.e_ident[2], ed->ehdr.e_ident[3]); 1286 PRT(" ei_class: %-18s", 1287 elf_class_str(ed->ehdr.e_ident[EI_CLASS])); 1288 PRT(" ei_data: %s\n", 1289 elf_data_str(ed->ehdr.e_ident[EI_DATA])); 1290 PRT(" e_machine: %-18s", e_machines(ed->ehdr.e_machine)); 1291 PRT(" e_version: %s\n", 1292 elf_version_str(ed->ehdr.e_version)); 1293 PRT(" e_type: %s\n", elf_type_str(ed->ehdr.e_type)); 1294 PRT(" e_flags: %18d\n", ed->ehdr.e_flags); 1295 PRT(" e_entry: %#18jx", (uintmax_t)ed->ehdr.e_entry); 1296 PRT(" e_ehsize: %6d", ed->ehdr.e_ehsize); 1297 PRT(" e_shstrndx:%5d\n", ed->ehdr.e_shstrndx); 1298 PRT(" e_shoff: %#18jx", (uintmax_t)ed->ehdr.e_shoff); 1299 PRT(" e_shentsize: %3d", ed->ehdr.e_shentsize); 1300 PRT(" e_shnum: %5d\n", ed->ehdr.e_shnum); 1301 PRT(" e_phoff: %#18jx", (uintmax_t)ed->ehdr.e_phoff); 1302 PRT(" e_phentsize: %3d", ed->ehdr.e_phentsize); 1303 PRT(" e_phnum: %5d\n", ed->ehdr.e_phnum); 1304 } else { 1305 PRT("\nelf header:\n"); 1306 PRT("\n"); 1307 PRT("\te_ident: %s %s %s\n", 1308 elf_class_str(ed->ehdr.e_ident[EI_CLASS]), 1309 elf_data_str(ed->ehdr.e_ident[EI_DATA]), 1310 ei_abis[ed->ehdr.e_ident[EI_OSABI]]); 1311 PRT("\te_type: %s\n", elf_type_str(ed->ehdr.e_type)); 1312 PRT("\te_machine: %s\n", e_machines(ed->ehdr.e_machine)); 1313 PRT("\te_version: %s\n", elf_version_str(ed->ehdr.e_version)); 1314 PRT("\te_entry: %#jx\n", (uintmax_t)ed->ehdr.e_entry); 1315 PRT("\te_phoff: %ju\n", (uintmax_t)ed->ehdr.e_phoff); 1316 PRT("\te_shoff: %ju\n", (uintmax_t) ed->ehdr.e_shoff); 1317 PRT("\te_flags: %u\n", ed->ehdr.e_flags); 1318 PRT("\te_ehsize: %u\n", ed->ehdr.e_ehsize); 1319 PRT("\te_phentsize: %u\n", ed->ehdr.e_phentsize); 1320 PRT("\te_phnum: %u\n", ed->ehdr.e_phnum); 1321 PRT("\te_shentsize: %u\n", ed->ehdr.e_shentsize); 1322 PRT("\te_shnum: %u\n", ed->ehdr.e_shnum); 1323 PRT("\te_shstrndx: %u\n", ed->ehdr.e_shstrndx); 1324 } 1325 } 1326 1327 /* 1328 * Dump the ELF Program Header Table. 1329 */ 1330 static void 1331 elf_print_phdr(struct elfdump *ed) 1332 { 1333 GElf_Phdr ph; 1334 size_t phnum, i; 1335 int header; 1336 1337 if (elf_getphnum(ed->elf, &phnum) == 0) { 1338 warnx("elf_getphnum failed: %s", elf_errmsg(-1)); 1339 return; 1340 } 1341 header = 0; 1342 for (i = 0; i < phnum; i++) { 1343 if (gelf_getphdr(ed->elf, i, &ph) != &ph) { 1344 warnx("elf_getphdr failed: %s", elf_errmsg(-1)); 1345 continue; 1346 } 1347 if (!STAILQ_EMPTY(&ed->snl) && 1348 find_name(ed, elf_phdr_type_str(ph.p_type)) == NULL) 1349 continue; 1350 if (ed->flags & SOLARIS_FMT) { 1351 PRT("\nProgram Header[%zu]:\n", i); 1352 PRT(" p_vaddr: %#-14jx", (uintmax_t)ph.p_vaddr); 1353 PRT(" p_flags: [ %s ]\n", 1354 p_flags[ph.p_flags & 0x7]); 1355 PRT(" p_paddr: %#-14jx", (uintmax_t)ph.p_paddr); 1356 PRT(" p_type: [ %s ]\n", 1357 elf_phdr_type_str(ph.p_type)); 1358 PRT(" p_filesz: %#-14jx", 1359 (uintmax_t)ph.p_filesz); 1360 PRT(" p_memsz: %#jx\n", (uintmax_t)ph.p_memsz); 1361 PRT(" p_offset: %#-14jx", 1362 (uintmax_t)ph.p_offset); 1363 PRT(" p_align: %#jx\n", (uintmax_t)ph.p_align); 1364 } else { 1365 if (!header) { 1366 PRT("\nprogram header:\n"); 1367 header = 1; 1368 } 1369 PRT("\n"); 1370 PRT("entry: %zu\n", i); 1371 PRT("\tp_type: %s\n", elf_phdr_type_str(ph.p_type)); 1372 PRT("\tp_offset: %ju\n", (uintmax_t)ph.p_offset); 1373 PRT("\tp_vaddr: %#jx\n", (uintmax_t)ph.p_vaddr); 1374 PRT("\tp_paddr: %#jx\n", (uintmax_t)ph.p_paddr); 1375 PRT("\tp_filesz: %ju\n", (uintmax_t)ph.p_filesz); 1376 PRT("\tp_memsz: %ju\n", (uintmax_t)ph.p_memsz); 1377 PRT("\tp_flags: %s\n", p_flags[ph.p_flags & 0x7]); 1378 PRT("\tp_align: %ju\n", (uintmax_t)ph.p_align); 1379 } 1380 } 1381 } 1382 1383 /* 1384 * Dump the ELF Section Header Table. 1385 */ 1386 static void 1387 elf_print_shdr(struct elfdump *ed) 1388 { 1389 struct section *s; 1390 size_t i; 1391 1392 if (!STAILQ_EMPTY(&ed->snl)) 1393 return; 1394 1395 if ((ed->flags & SOLARIS_FMT) == 0) 1396 PRT("\nsection header:\n"); 1397 for (i = 0; i < ed->shnum; i++) { 1398 s = &ed->sl[i]; 1399 if (ed->flags & SOLARIS_FMT) { 1400 if (i == 0) 1401 continue; 1402 PRT("\nSection Header[%zu]:", i); 1403 PRT(" sh_name: %s\n", s->name); 1404 PRT(" sh_addr: %#-14jx", (uintmax_t)s->addr); 1405 if (s->flags != 0) 1406 PRT(" sh_flags: [ %s ]\n", sh_flags(s->flags)); 1407 else 1408 PRT(" sh_flags: 0\n"); 1409 PRT(" sh_size: %#-14jx", (uintmax_t)s->sz); 1410 PRT(" sh_type: [ %s ]\n", 1411 sh_types(ed->ehdr.e_machine, s->type)); 1412 PRT(" sh_offset: %#-14jx", (uintmax_t)s->off); 1413 PRT(" sh_entsize: %#jx\n", (uintmax_t)s->entsize); 1414 PRT(" sh_link: %-14u", s->link); 1415 PRT(" sh_info: %u\n", s->info); 1416 PRT(" sh_addralign: %#jx\n", (uintmax_t)s->align); 1417 } else { 1418 PRT("\n"); 1419 PRT("entry: %ju\n", (uintmax_t)i); 1420 PRT("\tsh_name: %s\n", s->name); 1421 PRT("\tsh_type: %s\n", 1422 sh_types(ed->ehdr.e_machine, s->type)); 1423 PRT("\tsh_flags: %s\n", sh_flags(s->flags)); 1424 PRT("\tsh_addr: %#jx\n", (uintmax_t)s->addr); 1425 PRT("\tsh_offset: %ju\n", (uintmax_t)s->off); 1426 PRT("\tsh_size: %ju\n", (uintmax_t)s->sz); 1427 PRT("\tsh_link: %u\n", s->link); 1428 PRT("\tsh_info: %u\n", s->info); 1429 PRT("\tsh_addralign: %ju\n", (uintmax_t)s->align); 1430 PRT("\tsh_entsize: %ju\n", (uintmax_t)s->entsize); 1431 } 1432 } 1433 } 1434 1435 /* 1436 * Return number of entries in the given section. We'd prefer ent_count be a 1437 * size_t, but libelf APIs already use int for section indices. 1438 */ 1439 static int 1440 get_ent_count(const struct section *s, int *ent_count) 1441 { 1442 if (s->entsize == 0) { 1443 warnx("section %s has entry size 0", s->name); 1444 return (0); 1445 } else if (s->sz / s->entsize > INT_MAX) { 1446 warnx("section %s has invalid section count", s->name); 1447 return (0); 1448 } 1449 *ent_count = (int)(s->sz / s->entsize); 1450 return (1); 1451 } 1452 1453 /* 1454 * Retrieve the content of the corresponding SHT_SUNW_versym section for 1455 * a symbol table section. 1456 */ 1457 static void 1458 get_versym(struct elfdump *ed, int i, uint16_t **vs, int *nvs) 1459 { 1460 struct section *s; 1461 Elf_Data *data; 1462 size_t j; 1463 int elferr; 1464 1465 s = NULL; 1466 for (j = 0; j < ed->shnum; j++) { 1467 s = &ed->sl[j]; 1468 if (s->type == SHT_SUNW_versym && s->link == (uint32_t)i) 1469 break; 1470 } 1471 if (j >= ed->shnum) { 1472 *vs = NULL; 1473 return; 1474 } 1475 (void) elf_errno(); 1476 if ((data = elf_getdata(s->scn, NULL)) == NULL) { 1477 elferr = elf_errno(); 1478 if (elferr != 0) 1479 warnx("elf_getdata failed: %s", elf_errmsg(elferr)); 1480 *vs = NULL; 1481 return; 1482 } 1483 1484 *vs = data->d_buf; 1485 assert(data->d_size == s->sz); 1486 if (!get_ent_count(s, nvs)) 1487 *nvs = 0; 1488 } 1489 1490 /* 1491 * Dump the symbol table section. 1492 */ 1493 static void 1494 elf_print_symtab(struct elfdump *ed, int i) 1495 { 1496 struct section *s; 1497 const char *name; 1498 uint16_t *vs; 1499 char idx[13]; 1500 Elf_Data *data; 1501 GElf_Sym sym; 1502 int len, j, elferr, nvs; 1503 1504 s = &ed->sl[i]; 1505 if (ed->flags & SOLARIS_FMT) 1506 PRT("\nSymbol Table Section: %s\n", s->name); 1507 else 1508 PRT("\nsymbol table (%s):\n", s->name); 1509 (void) elf_errno(); 1510 if ((data = elf_getdata(s->scn, NULL)) == NULL) { 1511 elferr = elf_errno(); 1512 if (elferr != 0) 1513 warnx("elf_getdata failed: %s", elf_errmsg(elferr)); 1514 return; 1515 } 1516 vs = NULL; 1517 nvs = 0; 1518 assert(data->d_size == s->sz); 1519 if (!get_ent_count(s, &len)) 1520 return; 1521 if (ed->flags & SOLARIS_FMT) { 1522 if (ed->ec == ELFCLASS32) 1523 PRT(" index value "); 1524 else 1525 PRT(" index value "); 1526 PRT("size type bind oth ver shndx name\n"); 1527 get_versym(ed, i, &vs, &nvs); 1528 if (vs != NULL && nvs != len) { 1529 warnx("#symbol not equal to #versym"); 1530 vs = NULL; 1531 } 1532 } 1533 for (j = 0; j < len; j++) { 1534 if (gelf_getsym(data, j, &sym) != &sym) { 1535 warnx("gelf_getsym failed: %s", elf_errmsg(-1)); 1536 continue; 1537 } 1538 name = get_string(ed, s->link, sym.st_name); 1539 if (ed->flags & SOLARIS_FMT) { 1540 snprintf(idx, sizeof(idx), "[%d]", j); 1541 if (ed->ec == ELFCLASS32) 1542 PRT("%10s ", idx); 1543 else 1544 PRT("%10s ", idx); 1545 PRT("0x%8.8jx ", (uintmax_t)sym.st_value); 1546 if (ed->ec == ELFCLASS32) 1547 PRT("0x%8.8jx ", (uintmax_t)sym.st_size); 1548 else 1549 PRT("0x%12.12jx ", (uintmax_t)sym.st_size); 1550 PRT("%s ", st_type_S(GELF_ST_TYPE(sym.st_info))); 1551 PRT("%s ", st_bindings_S(GELF_ST_BIND(sym.st_info))); 1552 PRT("%c ", st_others[sym.st_other]); 1553 PRT("%3u ", (vs == NULL ? 0 : vs[j])); 1554 PRT("%-11.11s ", sh_name(ed, sym.st_shndx)); 1555 PRT("%s\n", name); 1556 } else { 1557 PRT("\nentry: %d\n", j); 1558 PRT("\tst_name: %s\n", name); 1559 PRT("\tst_value: %#jx\n", (uintmax_t)sym.st_value); 1560 PRT("\tst_size: %ju\n", (uintmax_t)sym.st_size); 1561 PRT("\tst_info: %s %s\n", 1562 st_type(ed->ehdr.e_machine, 1563 GELF_ST_TYPE(sym.st_info)), 1564 st_bindings(GELF_ST_BIND(sym.st_info))); 1565 PRT("\tst_shndx: %ju\n", (uintmax_t)sym.st_shndx); 1566 } 1567 } 1568 } 1569 1570 /* 1571 * Dump the symbol tables. (.dynsym and .symtab) 1572 */ 1573 static void 1574 elf_print_symtabs(struct elfdump *ed) 1575 { 1576 size_t i; 1577 1578 for (i = 0; i < ed->shnum; i++) 1579 if ((ed->sl[i].type == SHT_SYMTAB || 1580 ed->sl[i].type == SHT_DYNSYM) && 1581 (STAILQ_EMPTY(&ed->snl) || find_name(ed, ed->sl[i].name))) 1582 elf_print_symtab(ed, i); 1583 } 1584 1585 /* 1586 * Dump the content of .dynamic section. 1587 */ 1588 static void 1589 elf_print_dynamic(struct elfdump *ed) 1590 { 1591 struct section *s; 1592 const char *name; 1593 char idx[13]; 1594 Elf_Data *data; 1595 GElf_Dyn dyn; 1596 int elferr, i, len; 1597 1598 s = NULL; 1599 for (i = 0; (size_t)i < ed->shnum; i++) { 1600 s = &ed->sl[i]; 1601 if (s->type == SHT_DYNAMIC && 1602 (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name))) 1603 break; 1604 } 1605 if ((size_t)i >= ed->shnum) 1606 return; 1607 1608 if (ed->flags & SOLARIS_FMT) { 1609 PRT("Dynamic Section: %s\n", s->name); 1610 PRT(" index tag value\n"); 1611 } else 1612 PRT("\ndynamic:\n"); 1613 (void) elf_errno(); 1614 if ((data = elf_getdata(s->scn, NULL)) == NULL) { 1615 elferr = elf_errno(); 1616 if (elferr != 0) 1617 warnx("elf_getdata failed: %s", elf_errmsg(elferr)); 1618 return; 1619 } 1620 assert(data->d_size == s->sz); 1621 if (!get_ent_count(s, &len)) 1622 return; 1623 for (i = 0; i < len; i++) { 1624 if (gelf_getdyn(data, i, &dyn) != &dyn) { 1625 warnx("gelf_getdyn failed: %s", elf_errmsg(-1)); 1626 continue; 1627 } 1628 1629 if (ed->flags & SOLARIS_FMT) { 1630 snprintf(idx, sizeof(idx), "[%d]", i); 1631 PRT("%10s %-16s ", idx, d_tags(dyn.d_tag)); 1632 } else { 1633 PRT("\n"); 1634 PRT("entry: %d\n", i); 1635 PRT("\td_tag: %s\n", d_tags(dyn.d_tag)); 1636 } 1637 switch(dyn.d_tag) { 1638 case DT_NEEDED: 1639 case DT_SONAME: 1640 case DT_RPATH: 1641 case DT_RUNPATH: 1642 if ((name = elf_strptr(ed->elf, s->link, 1643 dyn.d_un.d_val)) == NULL) 1644 name = ""; 1645 if (ed->flags & SOLARIS_FMT) 1646 PRT("%#-16jx %s\n", (uintmax_t)dyn.d_un.d_val, 1647 name); 1648 else 1649 PRT("\td_val: %s\n", name); 1650 break; 1651 case DT_PLTRELSZ: 1652 case DT_RELA: 1653 case DT_RELASZ: 1654 case DT_RELAENT: 1655 case DT_RELACOUNT: 1656 case DT_STRSZ: 1657 case DT_SYMENT: 1658 case DT_RELSZ: 1659 case DT_RELENT: 1660 case DT_PLTREL: 1661 case DT_VERDEF: 1662 case DT_VERDEFNUM: 1663 case DT_VERNEED: 1664 case DT_VERNEEDNUM: 1665 case DT_VERSYM: 1666 if (ed->flags & SOLARIS_FMT) 1667 PRT("%#jx\n", (uintmax_t)dyn.d_un.d_val); 1668 else 1669 PRT("\td_val: %ju\n", 1670 (uintmax_t)dyn.d_un.d_val); 1671 break; 1672 case DT_PLTGOT: 1673 case DT_HASH: 1674 case DT_GNU_HASH: 1675 case DT_STRTAB: 1676 case DT_SYMTAB: 1677 case DT_INIT: 1678 case DT_FINI: 1679 case DT_REL: 1680 case DT_JMPREL: 1681 case DT_DEBUG: 1682 if (ed->flags & SOLARIS_FMT) 1683 PRT("%#jx\n", (uintmax_t)dyn.d_un.d_ptr); 1684 else 1685 PRT("\td_ptr: %#jx\n", 1686 (uintmax_t)dyn.d_un.d_ptr); 1687 break; 1688 case DT_NULL: 1689 case DT_SYMBOLIC: 1690 case DT_TEXTREL: 1691 default: 1692 if (ed->flags & SOLARIS_FMT) 1693 PRT("\n"); 1694 break; 1695 } 1696 } 1697 } 1698 1699 /* 1700 * Dump a .rel/.rela section entry. 1701 */ 1702 static void 1703 elf_print_rel_entry(struct elfdump *ed, struct section *s, int j, 1704 struct rel_entry *r) 1705 { 1706 1707 if (ed->flags & SOLARIS_FMT) { 1708 PRT(" %-23s ", elftc_reloc_type_str(ed->ehdr.e_machine, 1709 GELF_R_TYPE(r->u_r.rel.r_info))); 1710 PRT("%#12jx ", (uintmax_t)r->u_r.rel.r_offset); 1711 if (r->type == SHT_RELA) 1712 PRT("%10jd ", (intmax_t)r->u_r.rela.r_addend); 1713 else 1714 PRT(" "); 1715 PRT("%-14s ", s->name); 1716 PRT("%s\n", r->symn); 1717 } else { 1718 PRT("\n"); 1719 PRT("entry: %d\n", j); 1720 PRT("\tr_offset: %#jx\n", (uintmax_t)r->u_r.rel.r_offset); 1721 if (ed->ec == ELFCLASS32) 1722 PRT("\tr_info: %#jx\n", (uintmax_t) 1723 ELF32_R_INFO(ELF64_R_SYM(r->u_r.rel.r_info), 1724 ELF64_R_TYPE(r->u_r.rel.r_info))); 1725 else 1726 PRT("\tr_info: %#jx\n", (uintmax_t)r->u_r.rel.r_info); 1727 if (r->type == SHT_RELA) 1728 PRT("\tr_addend: %jd\n", 1729 (intmax_t)r->u_r.rela.r_addend); 1730 } 1731 } 1732 1733 /* 1734 * Dump a relocation section of type SHT_RELA. 1735 */ 1736 static void 1737 elf_print_rela(struct elfdump *ed, struct section *s, Elf_Data *data) 1738 { 1739 struct rel_entry r; 1740 int j, len; 1741 1742 if (ed->flags & SOLARIS_FMT) { 1743 PRT("\nRelocation Section: %s\n", s->name); 1744 PRT(" type offset " 1745 "addend section with respect to\n"); 1746 } else 1747 PRT("\nrelocation with addend (%s):\n", s->name); 1748 r.type = SHT_RELA; 1749 assert(data->d_size == s->sz); 1750 if (!get_ent_count(s, &len)) 1751 return; 1752 for (j = 0; j < len; j++) { 1753 if (gelf_getrela(data, j, &r.u_r.rela) != &r.u_r.rela) { 1754 warnx("gelf_getrela failed: %s", 1755 elf_errmsg(-1)); 1756 continue; 1757 } 1758 r.symn = get_symbol_name(ed, s->link, 1759 GELF_R_SYM(r.u_r.rela.r_info)); 1760 elf_print_rel_entry(ed, s, j, &r); 1761 } 1762 } 1763 1764 /* 1765 * Dump a relocation section of type SHT_REL. 1766 */ 1767 static void 1768 elf_print_rel(struct elfdump *ed, struct section *s, Elf_Data *data) 1769 { 1770 struct rel_entry r; 1771 int j, len; 1772 1773 if (ed->flags & SOLARIS_FMT) { 1774 PRT("\nRelocation Section: %s\n", s->name); 1775 PRT(" type offset " 1776 "section with respect to\n"); 1777 } else 1778 PRT("\nrelocation (%s):\n", s->name); 1779 r.type = SHT_REL; 1780 assert(data->d_size == s->sz); 1781 if (!get_ent_count(s, &len)) 1782 return; 1783 for (j = 0; j < len; j++) { 1784 if (gelf_getrel(data, j, &r.u_r.rel) != &r.u_r.rel) { 1785 warnx("gelf_getrel failed: %s", elf_errmsg(-1)); 1786 continue; 1787 } 1788 r.symn = get_symbol_name(ed, s->link, 1789 GELF_R_SYM(r.u_r.rel.r_info)); 1790 elf_print_rel_entry(ed, s, j, &r); 1791 } 1792 } 1793 1794 /* 1795 * Dump relocation sections. 1796 */ 1797 static void 1798 elf_print_reloc(struct elfdump *ed) 1799 { 1800 struct section *s; 1801 Elf_Data *data; 1802 size_t i; 1803 int elferr; 1804 1805 for (i = 0; i < ed->shnum; i++) { 1806 s = &ed->sl[i]; 1807 if ((s->type == SHT_REL || s->type == SHT_RELA) && 1808 (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name))) { 1809 (void) elf_errno(); 1810 if ((data = elf_getdata(s->scn, NULL)) == NULL) { 1811 elferr = elf_errno(); 1812 if (elferr != 0) 1813 warnx("elf_getdata failed: %s", 1814 elf_errmsg(elferr)); 1815 continue; 1816 } 1817 if (s->type == SHT_REL) 1818 elf_print_rel(ed, s, data); 1819 else 1820 elf_print_rela(ed, s, data); 1821 } 1822 } 1823 } 1824 1825 /* 1826 * Dump the content of PT_INTERP segment. 1827 */ 1828 static void 1829 elf_print_interp(struct elfdump *ed) 1830 { 1831 const char *s; 1832 GElf_Phdr phdr; 1833 size_t filesize, i, phnum; 1834 1835 if (!STAILQ_EMPTY(&ed->snl) && find_name(ed, "PT_INTERP") == NULL) 1836 return; 1837 1838 if ((s = elf_rawfile(ed->elf, &filesize)) == NULL) { 1839 warnx("elf_rawfile failed: %s", elf_errmsg(-1)); 1840 return; 1841 } 1842 if (!elf_getphnum(ed->elf, &phnum)) { 1843 warnx("elf_getphnum failed: %s", elf_errmsg(-1)); 1844 return; 1845 } 1846 for (i = 0; i < phnum; i++) { 1847 if (gelf_getphdr(ed->elf, i, &phdr) != &phdr) { 1848 warnx("elf_getphdr failed: %s", elf_errmsg(-1)); 1849 continue; 1850 } 1851 if (phdr.p_type == PT_INTERP) { 1852 if (phdr.p_offset >= filesize) { 1853 warnx("invalid phdr offset"); 1854 continue; 1855 } 1856 PRT("\ninterp:\n"); 1857 PRT("\t%s\n", s + phdr.p_offset); 1858 } 1859 } 1860 } 1861 1862 /* 1863 * Search the relocation sections for entries referring to the .got section. 1864 */ 1865 static void 1866 find_gotrel(struct elfdump *ed, struct section *gs, struct rel_entry *got) 1867 { 1868 struct section *s; 1869 struct rel_entry r; 1870 Elf_Data *data; 1871 size_t i; 1872 int elferr, j, k, len; 1873 1874 for(i = 0; i < ed->shnum; i++) { 1875 s = &ed->sl[i]; 1876 if (s->type != SHT_REL && s->type != SHT_RELA) 1877 continue; 1878 (void) elf_errno(); 1879 if ((data = elf_getdata(s->scn, NULL)) == NULL) { 1880 elferr = elf_errno(); 1881 if (elferr != 0) 1882 warnx("elf_getdata failed: %s", 1883 elf_errmsg(elferr)); 1884 return; 1885 } 1886 memset(&r, 0, sizeof(struct rel_entry)); 1887 r.type = s->type; 1888 assert(data->d_size == s->sz); 1889 if (!get_ent_count(s, &len)) 1890 return; 1891 for (j = 0; j < len; j++) { 1892 if (s->type == SHT_REL) { 1893 if (gelf_getrel(data, j, &r.u_r.rel) != 1894 &r.u_r.rel) { 1895 warnx("gelf_getrel failed: %s", 1896 elf_errmsg(-1)); 1897 continue; 1898 } 1899 } else { 1900 if (gelf_getrela(data, j, &r.u_r.rela) != 1901 &r.u_r.rela) { 1902 warnx("gelf_getrel failed: %s", 1903 elf_errmsg(-1)); 1904 continue; 1905 } 1906 } 1907 if (r.u_r.rel.r_offset >= gs->addr && 1908 r.u_r.rel.r_offset < gs->addr + gs->sz) { 1909 r.symn = get_symbol_name(ed, s->link, 1910 GELF_R_SYM(r.u_r.rel.r_info)); 1911 k = (r.u_r.rel.r_offset - gs->addr) / 1912 gs->entsize; 1913 memcpy(&got[k], &r, sizeof(struct rel_entry)); 1914 } 1915 } 1916 } 1917 } 1918 1919 static void 1920 elf_print_got_section(struct elfdump *ed, struct section *s) 1921 { 1922 struct rel_entry *got; 1923 Elf_Data *data, dst; 1924 int elferr, i, len; 1925 1926 if (s->entsize == 0) { 1927 /* XXX IA64 GOT section generated by gcc has entry size 0. */ 1928 if (s->align != 0) 1929 s->entsize = s->align; 1930 else 1931 return; 1932 } 1933 1934 if (!get_ent_count(s, &len)) 1935 return; 1936 if (ed->flags & SOLARIS_FMT) 1937 PRT("\nGlobal Offset Table Section: %s (%d entries)\n", 1938 s->name, len); 1939 else 1940 PRT("\nglobal offset table: %s\n", s->name); 1941 (void) elf_errno(); 1942 if ((data = elf_getdata(s->scn, NULL)) == NULL) { 1943 elferr = elf_errno(); 1944 if (elferr != 0) 1945 warnx("elf_getdata failed: %s", elf_errmsg(elferr)); 1946 return; 1947 } 1948 1949 /* 1950 * GOT section has section type SHT_PROGBITS, thus libelf treats it as 1951 * byte stream and will not perform any translation on it. As a result, 1952 * an exlicit call to gelf_xlatetom is needed here. Depends on arch, 1953 * GOT section should be translated to either WORD or XWORD. 1954 */ 1955 if (ed->ec == ELFCLASS32) 1956 data->d_type = ELF_T_WORD; 1957 else 1958 data->d_type = ELF_T_XWORD; 1959 memcpy(&dst, data, sizeof(Elf_Data)); 1960 if (gelf_xlatetom(ed->elf, &dst, data, ed->ehdr.e_ident[EI_DATA]) != 1961 &dst) { 1962 warnx("gelf_xlatetom failed: %s", elf_errmsg(-1)); 1963 return; 1964 } 1965 assert(dst.d_size == s->sz); 1966 if (ed->flags & SOLARIS_FMT) { 1967 /* 1968 * In verbose/Solaris mode, we search the relocation sections 1969 * and try to find the corresponding reloc entry for each GOT 1970 * section entry. 1971 */ 1972 if ((got = calloc(len, sizeof(struct rel_entry))) == NULL) 1973 err(EXIT_FAILURE, "calloc failed"); 1974 find_gotrel(ed, s, got); 1975 if (ed->ec == ELFCLASS32) { 1976 PRT(" ndx addr value reloc "); 1977 PRT("addend symbol\n"); 1978 } else { 1979 PRT(" ndx addr value "); 1980 PRT("reloc addend symbol\n"); 1981 } 1982 for(i = 0; i < len; i++) { 1983 PRT("[%5.5d] ", i); 1984 if (ed->ec == ELFCLASS32) { 1985 PRT("%-8.8jx ", 1986 (uintmax_t) (s->addr + i * s->entsize)); 1987 PRT("%-8.8x ", *((uint32_t *)dst.d_buf + i)); 1988 } else { 1989 PRT("%-16.16jx ", 1990 (uintmax_t) (s->addr + i * s->entsize)); 1991 PRT("%-16.16jx ", 1992 (uintmax_t) *((uint64_t *)dst.d_buf + i)); 1993 } 1994 PRT("%-18s ", elftc_reloc_type_str(ed->ehdr.e_machine, 1995 GELF_R_TYPE(got[i].u_r.rel.r_info))); 1996 if (ed->ec == ELFCLASS32) 1997 PRT("%-8.8jd ", 1998 (intmax_t)got[i].u_r.rela.r_addend); 1999 else 2000 PRT("%-12.12jd ", 2001 (intmax_t)got[i].u_r.rela.r_addend); 2002 if (got[i].symn == NULL) 2003 got[i].symn = ""; 2004 PRT("%s\n", got[i].symn); 2005 } 2006 free(got); 2007 } else { 2008 for(i = 0; i < len; i++) { 2009 PRT("\nentry: %d\n", i); 2010 if (ed->ec == ELFCLASS32) 2011 PRT("\t%#x\n", *((uint32_t *)dst.d_buf + i)); 2012 else 2013 PRT("\t%#jx\n", 2014 (uintmax_t) *((uint64_t *)dst.d_buf + i)); 2015 } 2016 } 2017 } 2018 2019 /* 2020 * Dump the content of Global Offset Table section. 2021 */ 2022 static void 2023 elf_print_got(struct elfdump *ed) 2024 { 2025 struct section *s; 2026 size_t i; 2027 2028 if (!STAILQ_EMPTY(&ed->snl)) 2029 return; 2030 2031 s = NULL; 2032 for (i = 0; i < ed->shnum; i++) { 2033 s = &ed->sl[i]; 2034 if (s->name && !strncmp(s->name, ".got", 4) && 2035 (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name))) 2036 elf_print_got_section(ed, s); 2037 } 2038 } 2039 2040 /* 2041 * Dump the content of .note.ABI-tag section. 2042 */ 2043 static void 2044 elf_print_note(struct elfdump *ed) 2045 { 2046 struct section *s; 2047 Elf_Data *data; 2048 Elf_Note *en; 2049 uint32_t namesz; 2050 uint32_t descsz; 2051 uint32_t desc; 2052 size_t count; 2053 int elferr, i; 2054 uint8_t *src; 2055 char idx[17]; 2056 2057 s = NULL; 2058 for (i = 0; (size_t)i < ed->shnum; i++) { 2059 s = &ed->sl[i]; 2060 if (s->type == SHT_NOTE && s->name && 2061 !strcmp(s->name, ".note.ABI-tag") && 2062 (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name))) 2063 break; 2064 } 2065 if ((size_t)i >= ed->shnum) 2066 return; 2067 if (ed->flags & SOLARIS_FMT) 2068 PRT("\nNote Section: %s\n", s->name); 2069 else 2070 PRT("\nnote (%s):\n", s->name); 2071 (void) elf_errno(); 2072 if ((data = elf_getdata(s->scn, NULL)) == NULL) { 2073 elferr = elf_errno(); 2074 if (elferr != 0) 2075 warnx("elf_getdata failed: %s", elf_errmsg(elferr)); 2076 return; 2077 } 2078 src = data->d_buf; 2079 count = data->d_size; 2080 while (count > sizeof(Elf_Note)) { 2081 en = (Elf_Note *) (uintptr_t) src; 2082 namesz = en->n_namesz; 2083 descsz = en->n_descsz; 2084 src += sizeof(Elf_Note); 2085 count -= sizeof(Elf_Note); 2086 if (roundup2(namesz, 4) + roundup2(descsz, 4) > count) { 2087 warnx("truncated note section"); 2088 return; 2089 } 2090 if (ed->flags & SOLARIS_FMT) { 2091 PRT("\n type %#x\n", en->n_type); 2092 PRT(" namesz %#x:\n", en->n_namesz); 2093 PRT("%s\n", src); 2094 } else 2095 PRT("\t%s ", src); 2096 src += roundup2(namesz, 4); 2097 count -= roundup2(namesz, 4); 2098 2099 /* 2100 * Note that we dump the whole desc part if we're in 2101 * "Solaris mode", while in the normal mode, we only look 2102 * at the first 4 bytes (a 32bit word) of the desc, i.e, 2103 * we assume that it's always a FreeBSD version number. 2104 */ 2105 if (ed->flags & SOLARIS_FMT) { 2106 PRT(" descsz %#x:", en->n_descsz); 2107 for (i = 0; (uint32_t)i < descsz; i++) { 2108 if ((i & 0xF) == 0) { 2109 snprintf(idx, sizeof(idx), "desc[%d]", 2110 i); 2111 PRT("\n %-9s", idx); 2112 } else if ((i & 0x3) == 0) 2113 PRT(" "); 2114 PRT(" %2.2x", src[i]); 2115 } 2116 PRT("\n"); 2117 } else { 2118 if (ed->ehdr.e_ident[EI_DATA] == ELFDATA2MSB) 2119 desc = be32dec(src); 2120 else 2121 desc = le32dec(src); 2122 PRT("%d\n", desc); 2123 } 2124 src += roundup2(descsz, 4); 2125 count -= roundup2(descsz, 4); 2126 } 2127 } 2128 2129 /* 2130 * Dump a hash table. 2131 */ 2132 static void 2133 elf_print_svr4_hash(struct elfdump *ed, struct section *s) 2134 { 2135 Elf_Data *data; 2136 uint32_t *buf; 2137 uint32_t *bucket, *chain; 2138 uint32_t nbucket, nchain; 2139 uint32_t *bl, *c, maxl, total; 2140 uint32_t i, j; 2141 int first, elferr; 2142 char idx[10]; 2143 2144 if (ed->flags & SOLARIS_FMT) 2145 PRT("\nHash Section: %s\n", s->name); 2146 else 2147 PRT("\nhash table (%s):\n", s->name); 2148 (void) elf_errno(); 2149 if ((data = elf_getdata(s->scn, NULL)) == NULL) { 2150 elferr = elf_errno(); 2151 if (elferr != 0) 2152 warnx("elf_getdata failed: %s", 2153 elf_errmsg(elferr)); 2154 return; 2155 } 2156 if (data->d_size < 2 * sizeof(uint32_t)) { 2157 warnx(".hash section too small"); 2158 return; 2159 } 2160 buf = data->d_buf; 2161 nbucket = buf[0]; 2162 nchain = buf[1]; 2163 if (nbucket <= 0 || nchain <= 0) { 2164 warnx("Malformed .hash section"); 2165 return; 2166 } 2167 if (data->d_size != 2168 ((uint64_t)nbucket + (uint64_t)nchain + 2) * sizeof(uint32_t)) { 2169 warnx("Malformed .hash section"); 2170 return; 2171 } 2172 bucket = &buf[2]; 2173 chain = &buf[2 + nbucket]; 2174 2175 if (ed->flags & SOLARIS_FMT) { 2176 maxl = 0; 2177 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL) 2178 err(EXIT_FAILURE, "calloc failed"); 2179 for (i = 0; i < nbucket; i++) 2180 for (j = bucket[i]; j > 0 && j < nchain; j = chain[j]) 2181 if (++bl[i] > maxl) 2182 maxl = bl[i]; 2183 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL) 2184 err(EXIT_FAILURE, "calloc failed"); 2185 for (i = 0; i < nbucket; i++) 2186 c[bl[i]]++; 2187 PRT(" bucket symndx name\n"); 2188 for (i = 0; i < nbucket; i++) { 2189 first = 1; 2190 for (j = bucket[i]; j > 0 && j < nchain; j = chain[j]) { 2191 if (first) { 2192 PRT("%10d ", i); 2193 first = 0; 2194 } else 2195 PRT(" "); 2196 snprintf(idx, sizeof(idx), "[%d]", j); 2197 PRT("%-10s ", idx); 2198 PRT("%s\n", get_symbol_name(ed, s->link, j)); 2199 } 2200 } 2201 PRT("\n"); 2202 total = 0; 2203 for (i = 0; i <= maxl; i++) { 2204 total += c[i] * i; 2205 PRT("%10u buckets contain %8d symbols\n", c[i], i); 2206 } 2207 PRT("%10u buckets %8u symbols (globals)\n", nbucket, 2208 total); 2209 } else { 2210 PRT("\nnbucket: %u\n", nbucket); 2211 PRT("nchain: %u\n\n", nchain); 2212 for (i = 0; i < nbucket; i++) 2213 PRT("bucket[%d]:\n\t%u\n\n", i, bucket[i]); 2214 for (i = 0; i < nchain; i++) 2215 PRT("chain[%d]:\n\t%u\n\n", i, chain[i]); 2216 } 2217 } 2218 2219 /* 2220 * Dump a 64bit hash table. 2221 */ 2222 static void 2223 elf_print_svr4_hash64(struct elfdump *ed, struct section *s) 2224 { 2225 Elf_Data *data, dst; 2226 uint64_t *buf; 2227 uint64_t *bucket, *chain; 2228 uint64_t nbucket, nchain; 2229 uint64_t *bl, *c, j, maxl, total; 2230 size_t i; 2231 int elferr, first; 2232 char idx[10]; 2233 2234 if (ed->flags & SOLARIS_FMT) 2235 PRT("\nHash Section: %s\n", s->name); 2236 else 2237 PRT("\nhash table (%s):\n", s->name); 2238 2239 /* 2240 * ALPHA uses 64-bit hash entries. Since libelf assumes that 2241 * .hash section contains only 32-bit entry, an explicit 2242 * gelf_xlatetom is needed here. 2243 */ 2244 (void) elf_errno(); 2245 if ((data = elf_rawdata(s->scn, NULL)) == NULL) { 2246 elferr = elf_errno(); 2247 if (elferr != 0) 2248 warnx("elf_rawdata failed: %s", 2249 elf_errmsg(elferr)); 2250 return; 2251 } 2252 data->d_type = ELF_T_XWORD; 2253 memcpy(&dst, data, sizeof(Elf_Data)); 2254 if (gelf_xlatetom(ed->elf, &dst, data, 2255 ed->ehdr.e_ident[EI_DATA]) != &dst) { 2256 warnx("gelf_xlatetom failed: %s", elf_errmsg(-1)); 2257 return; 2258 } 2259 if (dst.d_size < 2 * sizeof(uint64_t)) { 2260 warnx(".hash section too small"); 2261 return; 2262 } 2263 buf = dst.d_buf; 2264 nbucket = buf[0]; 2265 nchain = buf[1]; 2266 if (nbucket <= 0 || nchain <= 0) { 2267 warnx("Malformed .hash section"); 2268 return; 2269 } 2270 if (dst.d_size != (nbucket + nchain + 2) * sizeof(uint64_t)) { 2271 warnx("Malformed .hash section"); 2272 return; 2273 } 2274 bucket = &buf[2]; 2275 chain = &buf[2 + nbucket]; 2276 2277 if (ed->flags & SOLARIS_FMT) { 2278 maxl = 0; 2279 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL) 2280 err(EXIT_FAILURE, "calloc failed"); 2281 for (i = 0; i < nbucket; i++) 2282 for (j = bucket[i]; j > 0 && j < nchain; j = chain[j]) 2283 if (++bl[i] > maxl) 2284 maxl = bl[i]; 2285 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL) 2286 err(EXIT_FAILURE, "calloc failed"); 2287 for (i = 0; i < nbucket; i++) 2288 c[bl[i]]++; 2289 PRT(" bucket symndx name\n"); 2290 for (i = 0; i < nbucket; i++) { 2291 first = 1; 2292 for (j = bucket[i]; j > 0 && j < nchain; j = chain[j]) { 2293 if (first) { 2294 PRT("%10zu ", i); 2295 first = 0; 2296 } else 2297 PRT(" "); 2298 snprintf(idx, sizeof(idx), "[%zu]", (size_t)j); 2299 PRT("%-10s ", idx); 2300 PRT("%s\n", get_symbol_name(ed, s->link, j)); 2301 } 2302 } 2303 PRT("\n"); 2304 total = 0; 2305 for (i = 0; i <= maxl; i++) { 2306 total += c[i] * i; 2307 PRT("%10ju buckets contain %8zu symbols\n", 2308 (uintmax_t)c[i], i); 2309 } 2310 PRT("%10ju buckets %8ju symbols (globals)\n", 2311 (uintmax_t)nbucket, (uintmax_t)total); 2312 } else { 2313 PRT("\nnbucket: %ju\n", (uintmax_t)nbucket); 2314 PRT("nchain: %ju\n\n", (uintmax_t)nchain); 2315 for (i = 0; i < nbucket; i++) 2316 PRT("bucket[%zu]:\n\t%ju\n\n", i, (uintmax_t)bucket[i]); 2317 for (i = 0; i < nchain; i++) 2318 PRT("chain[%zu]:\n\t%ju\n\n", i, (uintmax_t)chain[i]); 2319 } 2320 2321 } 2322 2323 /* 2324 * Dump a GNU hash table. 2325 */ 2326 static void 2327 elf_print_gnu_hash(struct elfdump *ed, struct section *s) 2328 { 2329 struct section *ds; 2330 Elf_Data *data; 2331 uint32_t *buf; 2332 uint32_t *bucket, *chain; 2333 uint32_t nbucket, nchain, symndx, maskwords, shift2; 2334 uint32_t *bl, *c, maxl, total; 2335 uint32_t i, j; 2336 int first, elferr, dynsymcount; 2337 char idx[10]; 2338 2339 if (ed->flags & SOLARIS_FMT) 2340 PRT("\nGNU Hash Section: %s\n", s->name); 2341 else 2342 PRT("\ngnu hash table (%s):\n", s->name); 2343 (void) elf_errno(); 2344 if ((data = elf_getdata(s->scn, NULL)) == NULL) { 2345 elferr = elf_errno(); 2346 if (elferr != 0) 2347 warnx("elf_getdata failed: %s", 2348 elf_errmsg(elferr)); 2349 return; 2350 } 2351 if (data->d_size < 4 * sizeof(uint32_t)) { 2352 warnx(".gnu.hash section too small"); 2353 return; 2354 } 2355 buf = data->d_buf; 2356 nbucket = buf[0]; 2357 symndx = buf[1]; 2358 maskwords = buf[2]; 2359 shift2 = buf[3]; 2360 buf += 4; 2361 if (s->link >= ed->shnum) { 2362 warnx("Malformed .gnu.hash section"); 2363 return; 2364 } 2365 ds = &ed->sl[s->link]; 2366 if (!get_ent_count(ds, &dynsymcount)) 2367 return; 2368 if (symndx >= (uint32_t)dynsymcount) { 2369 warnx("Malformed .gnu.hash section"); 2370 return; 2371 } 2372 nchain = dynsymcount - symndx; 2373 if (data->d_size != 4 * sizeof(uint32_t) + maskwords * 2374 (ed->ec == ELFCLASS32 ? sizeof(uint32_t) : sizeof(uint64_t)) + 2375 ((uint64_t)nbucket + (uint64_t)nchain) * sizeof(uint32_t)) { 2376 warnx("Malformed .gnu.hash section"); 2377 return; 2378 } 2379 bucket = buf + (ed->ec == ELFCLASS32 ? maskwords : maskwords * 2); 2380 chain = bucket + nbucket; 2381 2382 if (ed->flags & SOLARIS_FMT) { 2383 maxl = 0; 2384 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL) 2385 err(EXIT_FAILURE, "calloc failed"); 2386 for (i = 0; i < nbucket; i++) 2387 for (j = bucket[i]; j > 0 && j - symndx < nchain; j++) { 2388 if (++bl[i] > maxl) 2389 maxl = bl[i]; 2390 if (chain[j - symndx] & 1) 2391 break; 2392 } 2393 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL) 2394 err(EXIT_FAILURE, "calloc failed"); 2395 for (i = 0; i < nbucket; i++) 2396 c[bl[i]]++; 2397 PRT(" bucket symndx name\n"); 2398 for (i = 0; i < nbucket; i++) { 2399 first = 1; 2400 for (j = bucket[i]; j > 0 && j - symndx < nchain; j++) { 2401 if (first) { 2402 PRT("%10d ", i); 2403 first = 0; 2404 } else 2405 PRT(" "); 2406 snprintf(idx, sizeof(idx), "[%d]", j ); 2407 PRT("%-10s ", idx); 2408 PRT("%s\n", get_symbol_name(ed, s->link, j)); 2409 if (chain[j - symndx] & 1) 2410 break; 2411 } 2412 } 2413 PRT("\n"); 2414 total = 0; 2415 for (i = 0; i <= maxl; i++) { 2416 total += c[i] * i; 2417 PRT("%10u buckets contain %8d symbols\n", c[i], i); 2418 } 2419 PRT("%10u buckets %8u symbols (globals)\n", nbucket, 2420 total); 2421 } else { 2422 PRT("\nnbucket: %u\n", nbucket); 2423 PRT("symndx: %u\n", symndx); 2424 PRT("maskwords: %u\n", maskwords); 2425 PRT("shift2: %u\n", shift2); 2426 PRT("nchain: %u\n\n", nchain); 2427 for (i = 0; i < nbucket; i++) 2428 PRT("bucket[%d]:\n\t%u\n\n", i, bucket[i]); 2429 for (i = 0; i < nchain; i++) 2430 PRT("chain[%d]:\n\t%u\n\n", i, chain[i]); 2431 } 2432 } 2433 2434 /* 2435 * Dump hash tables. 2436 */ 2437 static void 2438 elf_print_hash(struct elfdump *ed) 2439 { 2440 struct section *s; 2441 size_t i; 2442 2443 for (i = 0; i < ed->shnum; i++) { 2444 s = &ed->sl[i]; 2445 if ((s->type == SHT_HASH || s->type == SHT_GNU_HASH) && 2446 (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name))) { 2447 if (s->type == SHT_GNU_HASH) 2448 elf_print_gnu_hash(ed, s); 2449 else if (ed->ehdr.e_machine == EM_ALPHA && 2450 s->entsize == 8) 2451 elf_print_svr4_hash64(ed, s); 2452 else 2453 elf_print_svr4_hash(ed, s); 2454 } 2455 } 2456 } 2457 2458 /* 2459 * Dump the content of a Version Definition(SHT_SUNW_Verdef) Section. 2460 */ 2461 static void 2462 elf_print_verdef(struct elfdump *ed, struct section *s) 2463 { 2464 Elf_Data *data; 2465 Elf32_Verdef *vd; 2466 Elf32_Verdaux *vda; 2467 const char *str; 2468 char idx[10]; 2469 uint8_t *buf, *end, *buf2; 2470 int i, j, elferr, count; 2471 2472 if (ed->flags & SOLARIS_FMT) 2473 PRT("Version Definition Section: %s\n", s->name); 2474 else 2475 PRT("\nversion definition section (%s):\n", s->name); 2476 (void) elf_errno(); 2477 if ((data = elf_getdata(s->scn, NULL)) == NULL) { 2478 elferr = elf_errno(); 2479 if (elferr != 0) 2480 warnx("elf_getdata failed: %s", 2481 elf_errmsg(elferr)); 2482 return; 2483 } 2484 buf = data->d_buf; 2485 end = buf + data->d_size; 2486 i = 0; 2487 if (ed->flags & SOLARIS_FMT) 2488 PRT(" index version dependency\n"); 2489 while (buf + sizeof(Elf32_Verdef) <= end) { 2490 vd = (Elf32_Verdef *) (uintptr_t) buf; 2491 if (ed->flags & SOLARIS_FMT) { 2492 snprintf(idx, sizeof(idx), "[%d]", vd->vd_ndx); 2493 PRT("%10s ", idx); 2494 } else { 2495 PRT("\nentry: %d\n", i++); 2496 PRT("\tvd_version: %u\n", vd->vd_version); 2497 PRT("\tvd_flags: %u\n", vd->vd_flags); 2498 PRT("\tvd_ndx: %u\n", vd->vd_ndx); 2499 PRT("\tvd_cnt: %u\n", vd->vd_cnt); 2500 PRT("\tvd_hash: %u\n", vd->vd_hash); 2501 PRT("\tvd_aux: %u\n", vd->vd_aux); 2502 PRT("\tvd_next: %u\n\n", vd->vd_next); 2503 } 2504 buf2 = buf + vd->vd_aux; 2505 j = 0; 2506 count = 0; 2507 while (buf2 + sizeof(Elf32_Verdaux) <= end && j < vd->vd_cnt) { 2508 vda = (Elf32_Verdaux *) (uintptr_t) buf2; 2509 str = get_string(ed, s->link, vda->vda_name); 2510 if (ed->flags & SOLARIS_FMT) { 2511 if (count == 0) 2512 PRT("%-26.26s", str); 2513 else if (count == 1) 2514 PRT(" %-20.20s", str); 2515 else { 2516 PRT("\n%40.40s", ""); 2517 PRT("%s", str); 2518 } 2519 } else { 2520 PRT("\t\tvda: %d\n", j++); 2521 PRT("\t\t\tvda_name: %s\n", str); 2522 PRT("\t\t\tvda_next: %u\n", vda->vda_next); 2523 } 2524 if (vda->vda_next == 0) { 2525 if (ed->flags & SOLARIS_FMT) { 2526 if (vd->vd_flags & VER_FLG_BASE) { 2527 if (count == 0) 2528 PRT("%-20.20s", ""); 2529 PRT("%s", "[ BASE ]"); 2530 } 2531 PRT("\n"); 2532 } 2533 break; 2534 } 2535 if (ed->flags & SOLARIS_FMT) 2536 count++; 2537 buf2 += vda->vda_next; 2538 } 2539 if (vd->vd_next == 0) 2540 break; 2541 buf += vd->vd_next; 2542 } 2543 } 2544 2545 /* 2546 * Dump the content of a Version Needed(SHT_SUNW_Verneed) Section. 2547 */ 2548 static void 2549 elf_print_verneed(struct elfdump *ed, struct section *s) 2550 { 2551 Elf_Data *data; 2552 Elf32_Verneed *vn; 2553 Elf32_Vernaux *vna; 2554 uint8_t *buf, *end, *buf2; 2555 int i, j, elferr, first; 2556 2557 if (ed->flags & SOLARIS_FMT) 2558 PRT("\nVersion Needed Section: %s\n", s->name); 2559 else 2560 PRT("\nversion need section (%s):\n", s->name); 2561 (void) elf_errno(); 2562 if ((data = elf_getdata(s->scn, NULL)) == NULL) { 2563 elferr = elf_errno(); 2564 if (elferr != 0) 2565 warnx("elf_getdata failed: %s", 2566 elf_errmsg(elferr)); 2567 return; 2568 } 2569 buf = data->d_buf; 2570 end = buf + data->d_size; 2571 if (ed->flags & SOLARIS_FMT) 2572 PRT(" file version\n"); 2573 i = 0; 2574 while (buf + sizeof(Elf32_Verneed) <= end) { 2575 vn = (Elf32_Verneed *) (uintptr_t) buf; 2576 if (ed->flags & SOLARIS_FMT) 2577 PRT(" %-26.26s ", 2578 get_string(ed, s->link, vn->vn_file)); 2579 else { 2580 PRT("\nentry: %d\n", i++); 2581 PRT("\tvn_version: %u\n", vn->vn_version); 2582 PRT("\tvn_cnt: %u\n", vn->vn_cnt); 2583 PRT("\tvn_file: %s\n", 2584 get_string(ed, s->link, vn->vn_file)); 2585 PRT("\tvn_aux: %u\n", vn->vn_aux); 2586 PRT("\tvn_next: %u\n\n", vn->vn_next); 2587 } 2588 buf2 = buf + vn->vn_aux; 2589 j = 0; 2590 first = 1; 2591 while (buf2 + sizeof(Elf32_Vernaux) <= end && j < vn->vn_cnt) { 2592 vna = (Elf32_Vernaux *) (uintptr_t) buf2; 2593 if (ed->flags & SOLARIS_FMT) { 2594 if (!first) 2595 PRT("%40.40s", ""); 2596 else 2597 first = 0; 2598 PRT("%s\n", get_string(ed, s->link, 2599 vna->vna_name)); 2600 } else { 2601 PRT("\t\tvna: %d\n", j++); 2602 PRT("\t\t\tvna_hash: %u\n", vna->vna_hash); 2603 PRT("\t\t\tvna_flags: %u\n", vna->vna_flags); 2604 PRT("\t\t\tvna_other: %u\n", vna->vna_other); 2605 PRT("\t\t\tvna_name: %s\n", 2606 get_string(ed, s->link, vna->vna_name)); 2607 PRT("\t\t\tvna_next: %u\n", vna->vna_next); 2608 } 2609 if (vna->vna_next == 0) 2610 break; 2611 buf2 += vna->vna_next; 2612 } 2613 if (vn->vn_next == 0) 2614 break; 2615 buf += vn->vn_next; 2616 } 2617 } 2618 2619 /* 2620 * Dump the symbol-versioning sections. 2621 */ 2622 static void 2623 elf_print_symver(struct elfdump *ed) 2624 { 2625 struct section *s; 2626 size_t i; 2627 2628 for (i = 0; i < ed->shnum; i++) { 2629 s = &ed->sl[i]; 2630 if (!STAILQ_EMPTY(&ed->snl) && !find_name(ed, s->name)) 2631 continue; 2632 if (s->type == SHT_SUNW_verdef) 2633 elf_print_verdef(ed, s); 2634 if (s->type == SHT_SUNW_verneed) 2635 elf_print_verneed(ed, s); 2636 } 2637 } 2638 2639 /* 2640 * Dump the ELF checksum. See gelf_checksum(3) for details. 2641 */ 2642 static void 2643 elf_print_checksum(struct elfdump *ed) 2644 { 2645 2646 if (!STAILQ_EMPTY(&ed->snl)) 2647 return; 2648 2649 PRT("\nelf checksum: %#lx\n", gelf_checksum(ed->elf)); 2650 } 2651 2652 #define USAGE_MESSAGE "\ 2653 Usage: %s [options] file...\n\ 2654 Display information about ELF objects and ar(1) archives.\n\n\ 2655 Options:\n\ 2656 -a Show all information.\n\ 2657 -c Show shared headers.\n\ 2658 -d Show dynamic symbols.\n\ 2659 -e Show the ELF header.\n\ 2660 -G Show the GOT.\n\ 2661 -H | --help Show a usage message and exit.\n\ 2662 -h Show hash values.\n\ 2663 -i Show the dynamic interpreter.\n\ 2664 -k Show the ELF checksum.\n\ 2665 -n Show the contents of note sections.\n\ 2666 -N NAME Show the section named \"NAME\".\n\ 2667 -p Show the program header.\n\ 2668 -r Show relocations.\n\ 2669 -s Show the symbol table.\n\ 2670 -S Use the Solaris elfdump format.\n\ 2671 -v Show symbol-versioning information.\n\ 2672 -V | --version Print a version identifier and exit.\n\ 2673 -w FILE Write output to \"FILE\".\n" 2674 2675 static void 2676 usage(void) 2677 { 2678 fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME()); 2679 exit(EXIT_FAILURE); 2680 } 2681