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