1 /*- 2 * Copyright (c) 2003 David O'Brien. All rights reserved. 3 * Copyright (c) 2001 Jake Burkholder 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/types.h> 32 #include <sys/elf32.h> 33 #include <sys/elf64.h> 34 #include <sys/endian.h> 35 #include <sys/mman.h> 36 #include <sys/stat.h> 37 #include <err.h> 38 #include <fcntl.h> 39 #include <inttypes.h> 40 #include <stddef.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <unistd.h> 45 46 #define ED_DYN (1<<0) 47 #define ED_EHDR (1<<1) 48 #define ED_GOT (1<<2) 49 #define ED_HASH (1<<3) 50 #define ED_INTERP (1<<4) 51 #define ED_NOTE (1<<5) 52 #define ED_PHDR (1<<6) 53 #define ED_REL (1<<7) 54 #define ED_SHDR (1<<8) 55 #define ED_SYMTAB (1<<9) 56 #define ED_ALL ((1<<10)-1) 57 58 #define elf_get_addr elf_get_quad 59 #define elf_get_off elf_get_quad 60 #define elf_get_size elf_get_quad 61 62 enum elf_member { 63 D_TAG = 1, D_PTR, D_VAL, 64 65 E_CLASS, E_DATA, E_OSABI, E_TYPE, E_MACHINE, E_VERSION, E_ENTRY, 66 E_PHOFF, E_SHOFF, E_FLAGS, E_EHSIZE, E_PHENTSIZE, E_PHNUM, E_SHENTSIZE, 67 E_SHNUM, E_SHSTRNDX, 68 69 N_NAMESZ, N_DESCSZ, N_TYPE, 70 71 P_TYPE, P_OFFSET, P_VADDR, P_PADDR, P_FILESZ, P_MEMSZ, P_FLAGS, 72 P_ALIGN, 73 74 SH_NAME, SH_TYPE, SH_FLAGS, SH_ADDR, SH_OFFSET, SH_SIZE, SH_LINK, 75 SH_INFO, SH_ADDRALIGN, SH_ENTSIZE, 76 77 ST_NAME, ST_VALUE, ST_SIZE, ST_INFO, ST_SHNDX, 78 79 R_OFFSET, R_INFO, 80 81 RA_OFFSET, RA_INFO, RA_ADDEND 82 }; 83 84 typedef enum elf_member elf_member_t; 85 86 int elf32_offsets[] = { 87 0, 88 89 offsetof(Elf32_Dyn, d_tag), offsetof(Elf32_Dyn, d_un.d_ptr), 90 offsetof(Elf32_Dyn, d_un.d_val), 91 92 offsetof(Elf32_Ehdr, e_ident[EI_CLASS]), 93 offsetof(Elf32_Ehdr, e_ident[EI_DATA]), 94 offsetof(Elf32_Ehdr, e_ident[EI_OSABI]), 95 offsetof(Elf32_Ehdr, e_type), offsetof(Elf32_Ehdr, e_machine), 96 offsetof(Elf32_Ehdr, e_version), offsetof(Elf32_Ehdr, e_entry), 97 offsetof(Elf32_Ehdr, e_phoff), offsetof(Elf32_Ehdr, e_shoff), 98 offsetof(Elf32_Ehdr, e_flags), offsetof(Elf32_Ehdr, e_ehsize), 99 offsetof(Elf32_Ehdr, e_phentsize), offsetof(Elf32_Ehdr, e_phnum), 100 offsetof(Elf32_Ehdr, e_shentsize), offsetof(Elf32_Ehdr, e_shnum), 101 offsetof(Elf32_Ehdr, e_shstrndx), 102 103 offsetof(Elf_Note, n_namesz), offsetof(Elf_Note, n_descsz), 104 offsetof(Elf_Note, n_type), 105 106 offsetof(Elf32_Phdr, p_type), offsetof(Elf32_Phdr, p_offset), 107 offsetof(Elf32_Phdr, p_vaddr), offsetof(Elf32_Phdr, p_paddr), 108 offsetof(Elf32_Phdr, p_filesz), offsetof(Elf32_Phdr, p_memsz), 109 offsetof(Elf32_Phdr, p_flags), offsetof(Elf32_Phdr, p_align), 110 111 offsetof(Elf32_Shdr, sh_name), offsetof(Elf32_Shdr, sh_type), 112 offsetof(Elf32_Shdr, sh_flags), offsetof(Elf32_Shdr, sh_addr), 113 offsetof(Elf32_Shdr, sh_offset), offsetof(Elf32_Shdr, sh_size), 114 offsetof(Elf32_Shdr, sh_link), offsetof(Elf32_Shdr, sh_info), 115 offsetof(Elf32_Shdr, sh_addralign), offsetof(Elf32_Shdr, sh_entsize), 116 117 offsetof(Elf32_Sym, st_name), offsetof(Elf32_Sym, st_value), 118 offsetof(Elf32_Sym, st_size), offsetof(Elf32_Sym, st_info), 119 offsetof(Elf32_Sym, st_shndx), 120 121 offsetof(Elf32_Rel, r_offset), offsetof(Elf32_Rel, r_info), 122 123 offsetof(Elf32_Rela, r_offset), offsetof(Elf32_Rela, r_info), 124 offsetof(Elf32_Rela, r_addend) 125 }; 126 127 int elf64_offsets[] = { 128 0, 129 130 offsetof(Elf64_Dyn, d_tag), offsetof(Elf64_Dyn, d_un.d_ptr), 131 offsetof(Elf64_Dyn, d_un.d_val), 132 133 offsetof(Elf32_Ehdr, e_ident[EI_CLASS]), 134 offsetof(Elf32_Ehdr, e_ident[EI_DATA]), 135 offsetof(Elf32_Ehdr, e_ident[EI_OSABI]), 136 offsetof(Elf64_Ehdr, e_type), offsetof(Elf64_Ehdr, e_machine), 137 offsetof(Elf64_Ehdr, e_version), offsetof(Elf64_Ehdr, e_entry), 138 offsetof(Elf64_Ehdr, e_phoff), offsetof(Elf64_Ehdr, e_shoff), 139 offsetof(Elf64_Ehdr, e_flags), offsetof(Elf64_Ehdr, e_ehsize), 140 offsetof(Elf64_Ehdr, e_phentsize), offsetof(Elf64_Ehdr, e_phnum), 141 offsetof(Elf64_Ehdr, e_shentsize), offsetof(Elf64_Ehdr, e_shnum), 142 offsetof(Elf64_Ehdr, e_shstrndx), 143 144 offsetof(Elf_Note, n_namesz), offsetof(Elf_Note, n_descsz), 145 offsetof(Elf_Note, n_type), 146 147 offsetof(Elf64_Phdr, p_type), offsetof(Elf64_Phdr, p_offset), 148 offsetof(Elf64_Phdr, p_vaddr), offsetof(Elf64_Phdr, p_paddr), 149 offsetof(Elf64_Phdr, p_filesz), offsetof(Elf64_Phdr, p_memsz), 150 offsetof(Elf64_Phdr, p_flags), offsetof(Elf64_Phdr, p_align), 151 152 offsetof(Elf64_Shdr, sh_name), offsetof(Elf64_Shdr, sh_type), 153 offsetof(Elf64_Shdr, sh_flags), offsetof(Elf64_Shdr, sh_addr), 154 offsetof(Elf64_Shdr, sh_offset), offsetof(Elf64_Shdr, sh_size), 155 offsetof(Elf64_Shdr, sh_link), offsetof(Elf64_Shdr, sh_info), 156 offsetof(Elf64_Shdr, sh_addralign), offsetof(Elf64_Shdr, sh_entsize), 157 158 offsetof(Elf64_Sym, st_name), offsetof(Elf64_Sym, st_value), 159 offsetof(Elf64_Sym, st_size), offsetof(Elf64_Sym, st_info), 160 offsetof(Elf64_Sym, st_shndx), 161 162 offsetof(Elf64_Rel, r_offset), offsetof(Elf64_Rel, r_info), 163 164 offsetof(Elf64_Rela, r_offset), offsetof(Elf64_Rela, r_info), 165 offsetof(Elf64_Rela, r_addend) 166 }; 167 168 /* http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#tag_encodings */ 169 static const char * 170 d_tags(u_int64_t tag) { 171 switch (tag) { 172 case 0: return "DT_NULL"; 173 case 1: return "DT_NEEDED"; 174 case 2: return "DT_PLTRELSZ"; 175 case 3: return "DT_PLTGOT"; 176 case 4: return "DT_HASH"; 177 case 5: return "DT_STRTAB"; 178 case 6: return "DT_SYMTAB"; 179 case 7: return "DT_RELA"; 180 case 8: return "DT_RELASZ"; 181 case 9: return "DT_RELAENT"; 182 case 10: return "DT_STRSZ"; 183 case 11: return "DT_SYMENT"; 184 case 12: return "DT_INIT"; 185 case 13: return "DT_FINI"; 186 case 14: return "DT_SONAME"; 187 case 15: return "DT_RPATH"; 188 case 16: return "DT_SYMBOLIC"; 189 case 17: return "DT_REL"; 190 case 18: return "DT_RELSZ"; 191 case 19: return "DT_RELENT"; 192 case 20: return "DT_PLTREL"; 193 case 21: return "DT_DEBUG"; 194 case 22: return "DT_TEXTREL"; 195 case 23: return "DT_JMPREL"; 196 case 24: return "DT_BIND_NOW"; 197 case 25: return "DT_INIT_ARRAY"; 198 case 26: return "DT_FINI_ARRAY"; 199 case 27: return "DT_INIT_ARRAYSZ"; 200 case 28: return "DT_FINI_ARRAYSZ"; 201 case 29: return "DT_RUNPATH"; 202 case 30: return "DT_FLAGS"; 203 case 32: return "DT_PREINIT_ARRAY"; /* XXX: DT_ENCODING */ 204 case 33: return "DT_PREINIT_ARRAYSZ"; 205 /* 0x6000000D - 0x6ffff000 operating system-specific semantics */ 206 case 0x6ffffdf5: return "DT_GNU_PRELINKED"; 207 case 0x6ffffdf6: return "DT_GNU_CONFLICTSZ"; 208 case 0x6ffffdf7: return "DT_GNU_LIBLISTSZ"; 209 case 0x6ffffdf8: return "DT_SUNW_CHECKSUM"; 210 case 0x6ffffdf9: return "DT_PLTPADSZ"; 211 case 0x6ffffdfa: return "DT_MOVEENT"; 212 case 0x6ffffdfb: return "DT_MOVESZ"; 213 case 0x6ffffdfc: return "DT_FEATURE"; 214 case 0x6ffffdfd: return "DT_POSFLAG_1"; 215 case 0x6ffffdfe: return "DT_SYMINSZ"; 216 case 0x6ffffdff: return "DT_SYMINENT (DT_VALRNGHI)"; 217 case 0x6ffffe00: return "DT_ADDRRNGLO"; 218 case 0x6ffffef8: return "DT_GNU_CONFLICT"; 219 case 0x6ffffef9: return "DT_GNU_LIBLIST"; 220 case 0x6ffffefa: return "DT_SUNW_CONFIG"; 221 case 0x6ffffefb: return "DT_SUNW_DEPAUDIT"; 222 case 0x6ffffefc: return "DT_SUNW_AUDIT"; 223 case 0x6ffffefd: return "DT_SUNW_PLTPAD"; 224 case 0x6ffffefe: return "DT_SUNW_MOVETAB"; 225 case 0x6ffffeff: return "DT_SYMINFO (DT_ADDRRNGHI)"; 226 case 0x6ffffff9: return "DT_RELACOUNT"; 227 case 0x6ffffffa: return "DT_RELCOUNT"; 228 case 0x6ffffffb: return "DT_FLAGS_1"; 229 case 0x6ffffffc: return "DT_VERDEF"; 230 case 0x6ffffffd: return "DT_VERDEFNUM"; 231 case 0x6ffffffe: return "DT_VERNEED"; 232 case 0x6fffffff: return "DT_VERNEEDNUM"; 233 case 0x6ffffff0: return "DT_GNU_VERSYM"; 234 /* 0x70000000 - 0x7fffffff processor-specific semantics */ 235 case 0x70000000: return "DT_IA_64_PLT_RESERVE"; 236 case 0x7ffffffd: return "DT_SUNW_AUXILIARY"; 237 case 0x7ffffffe: return "DT_SUNW_USED"; 238 case 0x7fffffff: return "DT_SUNW_FILTER"; 239 default: return "ERROR: TAG NOT DEFINED"; 240 } 241 } 242 243 static const char * 244 e_machines(u_int mach) 245 { 246 static char machdesc[64]; 247 248 switch (mach) { 249 case EM_NONE: return "EM_NONE"; 250 case EM_M32: return "EM_M32"; 251 case EM_SPARC: return "EM_SPARC"; 252 case EM_386: return "EM_386"; 253 case EM_68K: return "EM_68K"; 254 case EM_88K: return "EM_88K"; 255 case EM_860: return "EM_860"; 256 case EM_MIPS: return "EM_MIPS"; 257 case EM_PPC: return "EM_PPC"; 258 case EM_ARM: return "EM_ARM"; 259 case EM_ALPHA: return "EM_ALPHA (legacy)"; 260 case EM_SPARCV9:return "EM_SPARCV9"; 261 case EM_IA_64: return "EM_IA_64"; 262 case EM_X86_64: return "EM_X86_64"; 263 } 264 snprintf(machdesc, sizeof(machdesc), 265 "(unknown machine) -- type 0x%x", mach); 266 return (machdesc); 267 } 268 269 const char *e_types[] = { 270 "ET_NONE", "ET_REL", "ET_EXEC", "ET_DYN", "ET_CORE" 271 }; 272 273 const char *ei_versions[] = { 274 "EV_NONE", "EV_CURRENT" 275 }; 276 277 const char *ei_classes[] = { 278 "ELFCLASSNONE", "ELFCLASS32", "ELFCLASS64" 279 }; 280 281 const char *ei_data[] = { 282 "ELFDATANONE", "ELFDATA2LSB", "ELFDATA2MSB" 283 }; 284 285 const char *ei_abis[] = { 286 "ELFOSABI_SYSV", "ELFOSABI_HPUX", "ELFOSABI_NETBSD", "ELFOSABI_LINUX", 287 "ELFOSABI_HURD", "ELFOSABI_86OPEN", "ELFOSABI_SOLARIS", 288 "ELFOSABI_MONTEREY", "ELFOSABI_IRIX", "ELFOSABI_FREEBSD", 289 "ELFOSABI_TRU64", "ELFOSABI_MODESTO", "ELFOSABI_OPENBSD" 290 }; 291 292 const char *p_types[] = { 293 "PT_NULL", "PT_LOAD", "PT_DYNAMIC", "PT_INTERP", "PT_NOTE", 294 "PT_SHLIB", "PT_PHDR", "PT_TLS" 295 }; 296 297 const char *p_flags[] = { 298 "", "PF_X", "PF_W", "PF_X|PF_W", "PF_R", "PF_X|PF_R", "PF_W|PF_R", 299 "PF_X|PF_W|PF_R" 300 }; 301 302 /* http://www.sco.com/developers/gabi/latest/ch4.sheader.html#sh_type */ 303 static const char * 304 sh_types(u_int64_t sht) { 305 switch (sht) { 306 case 0: return "SHT_NULL"; 307 case 1: return "SHT_PROGBITS"; 308 case 2: return "SHT_SYMTAB"; 309 case 3: return "SHT_STRTAB"; 310 case 4: return "SHT_RELA"; 311 case 5: return "SHT_HASH"; 312 case 6: return "SHT_DYNAMIC"; 313 case 7: return "SHT_NOTE"; 314 case 8: return "SHT_NOBITS"; 315 case 9: return "SHT_REL"; 316 case 10: return "SHT_SHLIB"; 317 case 11: return "SHT_DYNSYM"; 318 case 14: return "SHT_INIT_ARRAY"; 319 case 15: return "SHT_FINI_ARRAY"; 320 case 16: return "SHT_PREINIT_ARRAY"; 321 case 17: return "SHT_GROUP"; 322 case 18: return "SHT_SYMTAB_SHNDX"; 323 /* 0x60000000 - 0x6fffffff operating system-specific semantics */ 324 case 0x6ffffff0: return "XXX:VERSYM"; 325 case 0x6ffffff4: return "SHT_SUNW_dof"; 326 case 0x6ffffff7: return "SHT_GNU_LIBLIST"; 327 case 0x6ffffffc: return "XXX:VERDEF"; 328 case 0x6ffffffd: return "SHT_SUNW(GNU)_verdef"; 329 case 0x6ffffffe: return "SHT_SUNW(GNU)_verneed"; 330 case 0x6fffffff: return "SHT_SUNW(GNU)_versym"; 331 /* 0x70000000 - 0x7fffffff processor-specific semantics */ 332 case 0x70000000: return "SHT_IA_64_EXT"; 333 case 0x70000001: return "SHT_IA_64_UNWIND"; 334 case 0x7ffffffd: return "XXX:AUXILIARY"; 335 case 0x7fffffff: return "XXX:FILTER"; 336 /* 0x80000000 - 0xffffffff application programs */ 337 default: return "ERROR: SHT NOT DEFINED"; 338 } 339 } 340 341 const char *sh_flags[] = { 342 "", "SHF_WRITE", "SHF_ALLOC", "SHF_WRITE|SHF_ALLOC", "SHF_EXECINSTR", 343 "SHF_WRITE|SHF_EXECINSTR", "SHF_ALLOC|SHF_EXECINSTR", 344 "SHF_WRITE|SHF_ALLOC|SHF_EXECINSTR" 345 }; 346 347 const char *st_types[] = { 348 "STT_NOTYPE", "STT_OBJECT", "STT_FUNC", "STT_SECTION", "STT_FILE" 349 }; 350 351 const char *st_bindings[] = { 352 "STB_LOCAL", "STB_GLOBAL", "STB_WEAK" 353 }; 354 355 char *dynstr; 356 char *shstrtab; 357 char *strtab; 358 FILE *out; 359 360 u_int64_t elf_get_byte(Elf32_Ehdr *e, void *base, elf_member_t member); 361 u_int64_t elf_get_quarter(Elf32_Ehdr *e, void *base, elf_member_t member); 362 u_int64_t elf_get_half(Elf32_Ehdr *e, void *base, elf_member_t member); 363 u_int64_t elf_get_word(Elf32_Ehdr *e, void *base, elf_member_t member); 364 u_int64_t elf_get_quad(Elf32_Ehdr *e, void *base, elf_member_t member); 365 366 void elf_print_ehdr(Elf32_Ehdr *e); 367 void elf_print_phdr(Elf32_Ehdr *e, void *p); 368 void elf_print_shdr(Elf32_Ehdr *e, void *sh); 369 void elf_print_symtab(Elf32_Ehdr *e, void *sh, char *str); 370 void elf_print_dynamic(Elf32_Ehdr *e, void *sh); 371 void elf_print_rel(Elf32_Ehdr *e, void *r); 372 void elf_print_rela(Elf32_Ehdr *e, void *ra); 373 void elf_print_interp(Elf32_Ehdr *e, void *p); 374 void elf_print_got(Elf32_Ehdr *e, void *sh); 375 void elf_print_hash(Elf32_Ehdr *e, void *sh); 376 void elf_print_note(Elf32_Ehdr *e, void *sh); 377 378 void usage(void); 379 380 int 381 main(int ac, char **av) 382 { 383 u_int64_t phoff; 384 u_int64_t shoff; 385 u_int64_t phentsize; 386 u_int64_t phnum; 387 u_int64_t shentsize; 388 u_int64_t shnum; 389 u_int64_t shstrndx; 390 u_int64_t offset; 391 u_int64_t name; 392 u_int64_t type; 393 struct stat sb; 394 u_int flags; 395 Elf32_Ehdr *e; 396 void *p; 397 void *sh; 398 void *v; 399 int fd; 400 int ch; 401 int i; 402 403 out = stdout; 404 flags = 0; 405 while ((ch = getopt(ac, av, "acdeiGhnprsw:")) != -1) 406 switch (ch) { 407 case 'a': 408 flags = ED_ALL; 409 break; 410 case 'c': 411 flags |= ED_SHDR; 412 break; 413 case 'd': 414 flags |= ED_DYN; 415 break; 416 case 'e': 417 flags |= ED_EHDR; 418 break; 419 case 'i': 420 flags |= ED_INTERP; 421 break; 422 case 'G': 423 flags |= ED_GOT; 424 break; 425 case 'h': 426 flags |= ED_HASH; 427 break; 428 case 'n': 429 flags |= ED_NOTE; 430 break; 431 case 'p': 432 flags |= ED_PHDR; 433 break; 434 case 'r': 435 flags |= ED_REL; 436 break; 437 case 's': 438 flags |= ED_SYMTAB; 439 break; 440 case 'w': 441 if ((out = fopen(optarg, "w")) == NULL) 442 err(1, "%s", optarg); 443 break; 444 case '?': 445 default: 446 usage(); 447 } 448 ac -= optind; 449 av += optind; 450 if (ac == 0 || flags == 0) 451 usage(); 452 if ((fd = open(*av, O_RDONLY)) < 0 || 453 fstat(fd, &sb) < 0) 454 err(1, "%s", *av); 455 e = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0); 456 if (e == MAP_FAILED) 457 err(1, NULL); 458 if (!IS_ELF(*(Elf32_Ehdr *)e)) 459 errx(1, "not an elf file"); 460 phoff = elf_get_off(e, e, E_PHOFF); 461 shoff = elf_get_off(e, e, E_SHOFF); 462 phentsize = elf_get_quarter(e, e, E_PHENTSIZE); 463 phnum = elf_get_quarter(e, e, E_PHNUM); 464 shentsize = elf_get_quarter(e, e, E_SHENTSIZE); 465 shnum = elf_get_quarter(e, e, E_SHNUM); 466 shstrndx = elf_get_quarter(e, e, E_SHSTRNDX); 467 p = (char *)e + phoff; 468 sh = (char *)e + shoff; 469 offset = elf_get_off(e, (char *)sh + shstrndx * shentsize, SH_OFFSET); 470 shstrtab = (char *)e + offset; 471 for (i = 0; (u_int64_t)i < shnum; i++) { 472 name = elf_get_word(e, (char *)sh + i * shentsize, SH_NAME); 473 offset = elf_get_off(e, (char *)sh + i * shentsize, SH_OFFSET); 474 if (strcmp(shstrtab + name, ".strtab") == 0) 475 strtab = (char *)e + offset; 476 if (strcmp(shstrtab + name, ".dynstr") == 0) 477 dynstr = (char *)e + offset; 478 } 479 if (flags & ED_EHDR) 480 elf_print_ehdr(e); 481 if (flags & ED_PHDR) 482 elf_print_phdr(e, p); 483 if (flags & ED_SHDR) 484 elf_print_shdr(e, sh); 485 for (i = 0; (u_int64_t)i < phnum; i++) { 486 v = (char *)p + i * phentsize; 487 type = elf_get_word(e, v, P_TYPE); 488 switch (type) { 489 case PT_INTERP: 490 if (flags & ED_INTERP) 491 elf_print_interp(e, v); 492 break; 493 case PT_NULL: 494 case PT_LOAD: 495 case PT_DYNAMIC: 496 case PT_NOTE: 497 case PT_SHLIB: 498 case PT_PHDR: 499 break; 500 } 501 } 502 for (i = 0; (u_int64_t)i < shnum; i++) { 503 v = (char *)sh + i * shentsize; 504 type = elf_get_word(e, v, SH_TYPE); 505 switch (type) { 506 case SHT_SYMTAB: 507 if (flags & ED_SYMTAB) 508 elf_print_symtab(e, v, strtab); 509 break; 510 case SHT_DYNAMIC: 511 if (flags & ED_DYN) 512 elf_print_dynamic(e, v); 513 break; 514 case SHT_RELA: 515 if (flags & ED_REL) 516 elf_print_rela(e, v); 517 break; 518 case SHT_REL: 519 if (flags & ED_REL) 520 elf_print_rel(e, v); 521 break; 522 case SHT_NOTE: 523 name = elf_get_word(e, v, SH_NAME); 524 if (flags & ED_NOTE && 525 strcmp(shstrtab + name, ".note.ABI-tag") == 0) 526 elf_print_note(e, v); 527 break; 528 case SHT_DYNSYM: 529 if (flags & ED_SYMTAB) 530 elf_print_symtab(e, v, dynstr); 531 break; 532 case SHT_PROGBITS: 533 name = elf_get_word(e, v, SH_NAME); 534 if (flags & ED_GOT && 535 strcmp(shstrtab + name, ".got") == 0) 536 elf_print_got(e, v); 537 break; 538 case SHT_HASH: 539 if (flags & ED_HASH) 540 elf_print_hash(e, v); 541 break; 542 case SHT_NULL: 543 case SHT_STRTAB: 544 case SHT_NOBITS: 545 case SHT_SHLIB: 546 break; 547 } 548 } 549 550 return 0; 551 } 552 553 void 554 elf_print_ehdr(Elf32_Ehdr *e) 555 { 556 u_int64_t class; 557 u_int64_t data; 558 u_int64_t osabi; 559 u_int64_t type; 560 u_int64_t machine; 561 u_int64_t version; 562 u_int64_t entry; 563 u_int64_t phoff; 564 u_int64_t shoff; 565 u_int64_t flags; 566 u_int64_t ehsize; 567 u_int64_t phentsize; 568 u_int64_t phnum; 569 u_int64_t shentsize; 570 u_int64_t shnum; 571 u_int64_t shstrndx; 572 573 class = elf_get_byte(e, e, E_CLASS); 574 data = elf_get_byte(e, e, E_DATA); 575 osabi = elf_get_byte(e, e, E_OSABI); 576 type = elf_get_quarter(e, e, E_TYPE); 577 machine = elf_get_quarter(e, e, E_MACHINE); 578 version = elf_get_word(e, e, E_VERSION); 579 entry = elf_get_addr(e, e, E_ENTRY); 580 phoff = elf_get_off(e, e, E_PHOFF); 581 shoff = elf_get_off(e, e, E_SHOFF); 582 flags = elf_get_word(e, e, E_FLAGS); 583 ehsize = elf_get_quarter(e, e, E_EHSIZE); 584 phentsize = elf_get_quarter(e, e, E_PHENTSIZE); 585 phnum = elf_get_quarter(e, e, E_PHNUM); 586 shentsize = elf_get_quarter(e, e, E_SHENTSIZE); 587 shnum = elf_get_quarter(e, e, E_SHNUM); 588 shstrndx = elf_get_quarter(e, e, E_SHSTRNDX); 589 fprintf(out, "\nelf header:\n"); 590 fprintf(out, "\n"); 591 fprintf(out, "\te_ident: %s %s %s\n", ei_classes[class], ei_data[data], 592 ei_abis[osabi]); 593 fprintf(out, "\te_type: %s\n", e_types[type]); 594 fprintf(out, "\te_machine: %s\n", e_machines(machine)); 595 fprintf(out, "\te_version: %s\n", ei_versions[version]); 596 fprintf(out, "\te_entry: %#jx\n", (intmax_t)entry); 597 fprintf(out, "\te_phoff: %jd\n", (intmax_t)phoff); 598 fprintf(out, "\te_shoff: %jd\n", (intmax_t)shoff); 599 fprintf(out, "\te_flags: %jd\n", (intmax_t)flags); 600 fprintf(out, "\te_ehsize: %jd\n", (intmax_t)ehsize); 601 fprintf(out, "\te_phentsize: %jd\n", (intmax_t)phentsize); 602 fprintf(out, "\te_phnum: %jd\n", (intmax_t)phnum); 603 fprintf(out, "\te_shentsize: %jd\n", (intmax_t)shentsize); 604 fprintf(out, "\te_shnum: %jd\n", (intmax_t)shnum); 605 fprintf(out, "\te_shstrndx: %jd\n", (intmax_t)shstrndx); 606 } 607 608 void 609 elf_print_phdr(Elf32_Ehdr *e, void *p) 610 { 611 u_int64_t phentsize; 612 u_int64_t phnum; 613 u_int64_t type; 614 u_int64_t offset; 615 u_int64_t vaddr; 616 u_int64_t paddr; 617 u_int64_t filesz; 618 u_int64_t memsz; 619 u_int64_t flags; 620 u_int64_t align; 621 void *v; 622 int i; 623 624 phentsize = elf_get_quarter(e, e, E_PHENTSIZE); 625 phnum = elf_get_quarter(e, e, E_PHNUM); 626 fprintf(out, "\nprogram header:\n"); 627 for (i = 0; (u_int64_t)i < phnum; i++) { 628 v = (char *)p + i * phentsize; 629 type = elf_get_word(e, v, P_TYPE); 630 offset = elf_get_off(e, v, P_OFFSET); 631 vaddr = elf_get_addr(e, v, P_VADDR); 632 paddr = elf_get_addr(e, v, P_PADDR); 633 filesz = elf_get_size(e, v, P_FILESZ); 634 memsz = elf_get_size(e, v, P_MEMSZ); 635 flags = elf_get_word(e, v, P_FLAGS); 636 align = elf_get_size(e, v, P_ALIGN); 637 fprintf(out, "\n"); 638 fprintf(out, "entry: %d\n", i); 639 fprintf(out, "\tp_type: %s\n", p_types[type & 0x7]); 640 fprintf(out, "\tp_offset: %jd\n", (intmax_t)offset); 641 fprintf(out, "\tp_vaddr: %#jx\n", (intmax_t)vaddr); 642 fprintf(out, "\tp_paddr: %#jx\n", (intmax_t)paddr); 643 fprintf(out, "\tp_filesz: %jd\n", (intmax_t)filesz); 644 fprintf(out, "\tp_memsz: %jd\n", (intmax_t)memsz); 645 fprintf(out, "\tp_flags: %s\n", p_flags[flags]); 646 fprintf(out, "\tp_align: %jd\n", (intmax_t)align); 647 } 648 } 649 650 void 651 elf_print_shdr(Elf32_Ehdr *e, void *sh) 652 { 653 u_int64_t shentsize; 654 u_int64_t shnum; 655 u_int64_t name; 656 u_int64_t type; 657 u_int64_t flags; 658 u_int64_t addr; 659 u_int64_t offset; 660 u_int64_t size; 661 u_int64_t shlink; 662 u_int64_t info; 663 u_int64_t addralign; 664 u_int64_t entsize; 665 void *v; 666 int i; 667 668 shentsize = elf_get_quarter(e, e, E_SHENTSIZE); 669 shnum = elf_get_quarter(e, e, E_SHNUM); 670 fprintf(out, "\nsection header:\n"); 671 for (i = 0; (u_int64_t)i < shnum; i++) { 672 v = (char *)sh + i * shentsize; 673 name = elf_get_word(e, v, SH_NAME); 674 type = elf_get_word(e, v, SH_TYPE); 675 flags = elf_get_word(e, v, SH_FLAGS); 676 addr = elf_get_addr(e, v, SH_ADDR); 677 offset = elf_get_off(e, v, SH_OFFSET); 678 size = elf_get_size(e, v, SH_SIZE); 679 shlink = elf_get_word(e, v, SH_LINK); 680 info = elf_get_word(e, v, SH_INFO); 681 addralign = elf_get_size(e, v, SH_ADDRALIGN); 682 entsize = elf_get_size(e, v, SH_ENTSIZE); 683 fprintf(out, "\n"); 684 fprintf(out, "entry: %d\n", i); 685 fprintf(out, "\tsh_name: %s\n", shstrtab + name); 686 fprintf(out, "\tsh_type: %s\n", sh_types(type)); 687 fprintf(out, "\tsh_flags: %s\n", sh_flags[flags & 0x7]); 688 fprintf(out, "\tsh_addr: %#jx\n", addr); 689 fprintf(out, "\tsh_offset: %jd\n", (intmax_t)offset); 690 fprintf(out, "\tsh_size: %jd\n", (intmax_t)size); 691 fprintf(out, "\tsh_link: %jd\n", (intmax_t)shlink); 692 fprintf(out, "\tsh_info: %jd\n", (intmax_t)info); 693 fprintf(out, "\tsh_addralign: %jd\n", (intmax_t)addralign); 694 fprintf(out, "\tsh_entsize: %jd\n", (intmax_t)entsize); 695 } 696 } 697 698 void 699 elf_print_symtab(Elf32_Ehdr *e, void *sh, char *str) 700 { 701 u_int64_t offset; 702 u_int64_t entsize; 703 u_int64_t size; 704 u_int64_t name; 705 u_int64_t value; 706 u_int64_t info; 707 u_int64_t shndx; 708 void *st; 709 int len; 710 int i; 711 712 offset = elf_get_off(e, sh, SH_OFFSET); 713 entsize = elf_get_size(e, sh, SH_ENTSIZE); 714 size = elf_get_size(e, sh, SH_SIZE); 715 name = elf_get_word(e, sh, SH_NAME); 716 len = size / entsize; 717 fprintf(out, "\nsymbol table (%s):\n", shstrtab + name); 718 for (i = 0; i < len; i++) { 719 st = (char *)e + offset + i * entsize; 720 name = elf_get_word(e, st, ST_NAME); 721 value = elf_get_addr(e, st, ST_VALUE); 722 size = elf_get_size(e, st, ST_SIZE); 723 info = elf_get_byte(e, st, ST_INFO); 724 shndx = elf_get_quarter(e, st, ST_SHNDX); 725 fprintf(out, "\n"); 726 fprintf(out, "entry: %d\n", i); 727 fprintf(out, "\tst_name: %s\n", str + name); 728 fprintf(out, "\tst_value: %#jx\n", value); 729 fprintf(out, "\tst_size: %jd\n", (intmax_t)size); 730 fprintf(out, "\tst_info: %s %s\n", 731 st_types[ELF32_ST_TYPE(info)], 732 st_bindings[ELF32_ST_BIND(info)]); 733 fprintf(out, "\tst_shndx: %jd\n", (intmax_t)shndx); 734 } 735 } 736 737 void 738 elf_print_dynamic(Elf32_Ehdr *e, void *sh) 739 { 740 u_int64_t offset; 741 u_int64_t entsize; 742 u_int64_t size; 743 int64_t tag; 744 u_int64_t ptr; 745 u_int64_t val; 746 void *d; 747 int i; 748 749 offset = elf_get_off(e, sh, SH_OFFSET); 750 entsize = elf_get_size(e, sh, SH_ENTSIZE); 751 size = elf_get_size(e, sh, SH_SIZE); 752 fprintf(out, "\ndynamic:\n"); 753 for (i = 0; (u_int64_t)i < size / entsize; i++) { 754 d = (char *)e + offset + i * entsize; 755 tag = elf_get_size(e, d, D_TAG); 756 ptr = elf_get_size(e, d, D_PTR); 757 val = elf_get_addr(e, d, D_VAL); 758 fprintf(out, "\n"); 759 fprintf(out, "entry: %d\n", i); 760 fprintf(out, "\td_tag: %s\n", d_tags(tag)); 761 switch (tag) { 762 case DT_NEEDED: 763 case DT_SONAME: 764 case DT_RPATH: 765 fprintf(out, "\td_val: %s\n", dynstr + val); 766 break; 767 case DT_PLTRELSZ: 768 case DT_RELA: 769 case DT_RELASZ: 770 case DT_RELAENT: 771 case DT_STRSZ: 772 case DT_SYMENT: 773 case DT_RELSZ: 774 case DT_RELENT: 775 case DT_PLTREL: 776 fprintf(out, "\td_val: %jd\n", (intmax_t)val); 777 break; 778 case DT_PLTGOT: 779 case DT_HASH: 780 case DT_STRTAB: 781 case DT_SYMTAB: 782 case DT_INIT: 783 case DT_FINI: 784 case DT_REL: 785 case DT_JMPREL: 786 fprintf(out, "\td_ptr: %#jx\n", ptr); 787 break; 788 case DT_NULL: 789 case DT_SYMBOLIC: 790 case DT_DEBUG: 791 case DT_TEXTREL: 792 break; 793 } 794 } 795 } 796 797 void 798 elf_print_rela(Elf32_Ehdr *e, void *sh) 799 { 800 u_int64_t offset; 801 u_int64_t entsize; 802 u_int64_t size; 803 u_int64_t name; 804 u_int64_t info; 805 int64_t addend; 806 void *ra; 807 void *v; 808 int i; 809 810 offset = elf_get_off(e, sh, SH_OFFSET); 811 entsize = elf_get_size(e, sh, SH_ENTSIZE); 812 size = elf_get_size(e, sh, SH_SIZE); 813 name = elf_get_word(e, sh, SH_NAME); 814 v = (char *)e + offset; 815 fprintf(out, "\nrelocation with addend (%s):\n", shstrtab + name); 816 for (i = 0; (u_int64_t)i < size / entsize; i++) { 817 ra = (char *)v + i * entsize; 818 offset = elf_get_addr(e, ra, RA_OFFSET); 819 info = elf_get_word(e, ra, RA_INFO); 820 addend = elf_get_off(e, ra, RA_ADDEND); 821 fprintf(out, "\n"); 822 fprintf(out, "entry: %d\n", i); 823 fprintf(out, "\tr_offset: %#jx\n", offset); 824 fprintf(out, "\tr_info: %jd\n", (intmax_t)info); 825 fprintf(out, "\tr_addend: %jd\n", (intmax_t)addend); 826 } 827 } 828 829 void 830 elf_print_rel(Elf32_Ehdr *e, void *sh) 831 { 832 u_int64_t offset; 833 u_int64_t entsize; 834 u_int64_t size; 835 u_int64_t name; 836 u_int64_t info; 837 void *r; 838 void *v; 839 int i; 840 841 offset = elf_get_off(e, sh, SH_OFFSET); 842 entsize = elf_get_size(e, sh, SH_ENTSIZE); 843 size = elf_get_size(e, sh, SH_SIZE); 844 name = elf_get_word(e, sh, SH_NAME); 845 v = (char *)e + offset; 846 fprintf(out, "\nrelocation (%s):\n", shstrtab + name); 847 for (i = 0; (u_int64_t)i < size / entsize; i++) { 848 r = (char *)v + i * entsize; 849 offset = elf_get_addr(e, r, R_OFFSET); 850 info = elf_get_word(e, r, R_INFO); 851 fprintf(out, "\n"); 852 fprintf(out, "entry: %d\n", i); 853 fprintf(out, "\tr_offset: %#jx\n", offset); 854 fprintf(out, "\tr_info: %jd\n", (intmax_t)info); 855 } 856 } 857 858 void 859 elf_print_interp(Elf32_Ehdr *e, void *p) 860 { 861 u_int64_t offset; 862 char *s; 863 864 offset = elf_get_off(e, p, P_OFFSET); 865 s = (char *)e + offset; 866 fprintf(out, "\ninterp:\n"); 867 fprintf(out, "\t%s\n", s); 868 } 869 870 void 871 elf_print_got(Elf32_Ehdr *e, void *sh) 872 { 873 u_int64_t offset; 874 u_int64_t addralign; 875 u_int64_t size; 876 u_int64_t addr; 877 void *v; 878 int i; 879 880 offset = elf_get_off(e, sh, SH_OFFSET); 881 addralign = elf_get_size(e, sh, SH_ADDRALIGN); 882 size = elf_get_size(e, sh, SH_SIZE); 883 v = (char *)e + offset; 884 fprintf(out, "\nglobal offset table:\n"); 885 for (i = 0; (u_int64_t)i < size / addralign; i++) { 886 addr = elf_get_addr(e, (char *)v + i * addralign, 0); 887 fprintf(out, "\n"); 888 fprintf(out, "entry: %d\n", i); 889 fprintf(out, "\t%#jx\n", addr); 890 } 891 } 892 893 void 894 elf_print_hash(Elf32_Ehdr *e __unused, void *sh __unused) 895 { 896 } 897 898 void 899 elf_print_note(Elf32_Ehdr *e, void *sh) 900 { 901 u_int64_t offset; 902 u_int64_t size; 903 u_int64_t name; 904 u_int32_t namesz; 905 u_int32_t descsz; 906 u_int32_t type; 907 u_int32_t desc; 908 char *n, *s; 909 910 offset = elf_get_off(e, sh, SH_OFFSET); 911 size = elf_get_size(e, sh, SH_SIZE); 912 name = elf_get_word(e, sh, SH_NAME); 913 n = (char *)e + offset; 914 fprintf(out, "\nnote (%s):\n", shstrtab + name); 915 while (n < ((char *)e + offset + size)) { 916 namesz = elf_get_word(e, n, N_NAMESZ); 917 descsz = elf_get_word(e, n, N_DESCSZ); 918 type = elf_get_word(e, n, N_TYPE); 919 s = n + sizeof(Elf_Note); 920 desc = elf_get_word(e, n + sizeof(Elf_Note) + namesz, 0); 921 fprintf(out, "\t%s %d\n", s, desc); 922 n += sizeof(Elf_Note) + namesz + descsz; 923 } 924 } 925 926 u_int64_t 927 elf_get_byte(Elf32_Ehdr *e, void *base, elf_member_t member) 928 { 929 u_int64_t val; 930 931 val = 0; 932 switch (e->e_ident[EI_CLASS]) { 933 case ELFCLASS32: 934 val = ((char *)base)[elf32_offsets[member]]; 935 break; 936 case ELFCLASS64: 937 val = ((char *)base)[elf64_offsets[member]]; 938 break; 939 case ELFCLASSNONE: 940 errx(1, "invalid class"); 941 } 942 943 return val; 944 } 945 946 u_int64_t 947 elf_get_quarter(Elf32_Ehdr *e, void *base, elf_member_t member) 948 { 949 u_int64_t val; 950 951 val = 0; 952 switch (e->e_ident[EI_CLASS]) { 953 case ELFCLASS32: 954 base = (char *)base + elf32_offsets[member]; 955 switch (e->e_ident[EI_DATA]) { 956 case ELFDATA2MSB: 957 val = be16dec(base); 958 break; 959 case ELFDATA2LSB: 960 val = le16dec(base); 961 break; 962 case ELFDATANONE: 963 errx(1, "invalid data format"); 964 } 965 break; 966 case ELFCLASS64: 967 base = (char *)base + elf64_offsets[member]; 968 switch (e->e_ident[EI_DATA]) { 969 case ELFDATA2MSB: 970 val = be16dec(base); 971 break; 972 case ELFDATA2LSB: 973 val = le16dec(base); 974 break; 975 case ELFDATANONE: 976 errx(1, "invalid data format"); 977 } 978 break; 979 case ELFCLASSNONE: 980 errx(1, "invalid class"); 981 } 982 983 return val; 984 } 985 986 u_int64_t 987 elf_get_half(Elf32_Ehdr *e, void *base, elf_member_t member) 988 { 989 u_int64_t val; 990 991 val = 0; 992 switch (e->e_ident[EI_CLASS]) { 993 case ELFCLASS32: 994 base = (char *)base + elf32_offsets[member]; 995 switch (e->e_ident[EI_DATA]) { 996 case ELFDATA2MSB: 997 val = be16dec(base); 998 break; 999 case ELFDATA2LSB: 1000 val = le16dec(base); 1001 break; 1002 case ELFDATANONE: 1003 errx(1, "invalid data format"); 1004 } 1005 break; 1006 case ELFCLASS64: 1007 base = (char *)base + elf64_offsets[member]; 1008 switch (e->e_ident[EI_DATA]) { 1009 case ELFDATA2MSB: 1010 val = be32dec(base); 1011 break; 1012 case ELFDATA2LSB: 1013 val = le32dec(base); 1014 break; 1015 case ELFDATANONE: 1016 errx(1, "invalid data format"); 1017 } 1018 break; 1019 case ELFCLASSNONE: 1020 errx(1, "invalid class"); 1021 } 1022 1023 return val; 1024 } 1025 1026 u_int64_t 1027 elf_get_word(Elf32_Ehdr *e, void *base, elf_member_t member) 1028 { 1029 u_int64_t val; 1030 1031 val = 0; 1032 switch (e->e_ident[EI_CLASS]) { 1033 case ELFCLASS32: 1034 base = (char *)base + elf32_offsets[member]; 1035 switch (e->e_ident[EI_DATA]) { 1036 case ELFDATA2MSB: 1037 val = be32dec(base); 1038 break; 1039 case ELFDATA2LSB: 1040 val = le32dec(base); 1041 break; 1042 case ELFDATANONE: 1043 errx(1, "invalid data format"); 1044 } 1045 break; 1046 case ELFCLASS64: 1047 base = (char *)base + elf64_offsets[member]; 1048 switch (e->e_ident[EI_DATA]) { 1049 case ELFDATA2MSB: 1050 val = be32dec(base); 1051 break; 1052 case ELFDATA2LSB: 1053 val = le32dec(base); 1054 break; 1055 case ELFDATANONE: 1056 errx(1, "invalid data format"); 1057 } 1058 break; 1059 case ELFCLASSNONE: 1060 errx(1, "invalid class"); 1061 } 1062 1063 return val; 1064 } 1065 1066 u_int64_t 1067 elf_get_quad(Elf32_Ehdr *e, void *base, elf_member_t member) 1068 { 1069 u_int64_t val; 1070 1071 val = 0; 1072 switch (e->e_ident[EI_CLASS]) { 1073 case ELFCLASS32: 1074 base = (char *)base + elf32_offsets[member]; 1075 switch (e->e_ident[EI_DATA]) { 1076 case ELFDATA2MSB: 1077 val = be32dec(base); 1078 break; 1079 case ELFDATA2LSB: 1080 val = le32dec(base); 1081 break; 1082 case ELFDATANONE: 1083 errx(1, "invalid data format"); 1084 } 1085 break; 1086 case ELFCLASS64: 1087 base = (char *)base + elf64_offsets[member]; 1088 switch (e->e_ident[EI_DATA]) { 1089 case ELFDATA2MSB: 1090 val = be64dec(base); 1091 break; 1092 case ELFDATA2LSB: 1093 val = le64dec(base); 1094 break; 1095 case ELFDATANONE: 1096 errx(1, "invalid data format"); 1097 } 1098 break; 1099 case ELFCLASSNONE: 1100 errx(1, "invalid class"); 1101 } 1102 1103 return val; 1104 } 1105 1106 void 1107 usage(void) 1108 { 1109 fprintf(stderr, "usage: elfdump -a | -cdeGhinprs [-w file] file\n"); 1110 exit(1); 1111 } 1112