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_IA64_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 switch (mach) { 247 case EM_NONE: return "EM_NONE"; 248 case EM_M32: return "EM_M32"; 249 case EM_SPARC: return "EM_SPARC"; 250 case EM_386: return "EM_386"; 251 case EM_68K: return "EM_68K"; 252 case EM_88K: return "EM_88K"; 253 case EM_486: return "EM_486"; 254 case EM_860: return "EM_860"; 255 case EM_MIPS: return "EM_MIPS"; 256 case EM_IA_64: return "EM_IA_64"; 257 } 258 return "(unknown machine)"; 259 } 260 261 const char *e_types[] = { 262 "ET_NONE", "ET_REL", "ET_EXEC", "ET_DYN", "ET_CORE" 263 }; 264 265 const char *ei_versions[] = { 266 "EV_NONE", "EV_CURRENT" 267 }; 268 269 const char *ei_classes[] = { 270 "ELFCLASSNONE", "ELFCLASS32", "ELFCLASS64" 271 }; 272 273 const char *ei_data[] = { 274 "ELFDATANONE", "ELFDATA2LSB", "ELFDATA2MSB" 275 }; 276 277 const char *ei_abis[] = { 278 "ELFOSABI_SYSV", "ELFOSABI_HPUX", "ELFOSABI_NETBSD", "ELFOSABI_LINUX", 279 "ELFOSABI_HURD", "ELFOSABI_86OPEN", "ELFOSABI_SOLARIS", 280 "ELFOSABI_MONTEREY", "ELFOSABI_IRIX", "ELFOSABI_FREEBSD", 281 "ELFOSABI_TRU64", "ELFOSABI_MODESTO", "ELFOSABI_OPENBSD" 282 }; 283 284 const char *p_types[] = { 285 "PT_NULL", "PT_LOAD", "PT_DYNAMIC", "PT_INTERP", "PT_NOTE", 286 "PT_SHLIB", "PT_PHDR", "PT_TLS" 287 }; 288 289 const char *p_flags[] = { 290 "", "PF_X", "PF_W", "PF_X|PF_W", "PF_R", "PF_X|PF_R", "PF_W|PF_R", 291 "PF_X|PF_W|PF_R" 292 }; 293 294 /* http://www.sco.com/developers/gabi/latest/ch4.sheader.html#sh_type */ 295 static const char * 296 sh_types(u_int64_t sht) { 297 switch (sht) { 298 case 0: return "SHT_NULL"; 299 case 1: return "SHT_PROGBITS"; 300 case 2: return "SHT_SYMTAB"; 301 case 3: return "SHT_STRTAB"; 302 case 4: return "SHT_RELA"; 303 case 5: return "SHT_HASH"; 304 case 6: return "SHT_DYNAMIC"; 305 case 7: return "SHT_NOTE"; 306 case 8: return "SHT_NOBITS"; 307 case 9: return "SHT_REL"; 308 case 10: return "SHT_SHLIB"; 309 case 11: return "SHT_DYNSYM"; 310 case 14: return "SHT_INIT_ARRAY"; 311 case 15: return "SHT_FINI_ARRAY"; 312 case 16: return "SHT_PREINIT_ARRAY"; 313 case 17: return "SHT_GROUP"; 314 case 18: return "SHT_SYMTAB_SHNDX"; 315 /* 0x60000000 - 0x6fffffff operating system-specific semantics */ 316 case 0x6ffffff0: return "XXX:VERSYM"; 317 case 0x6ffffff7: return "SHT_GNU_LIBLIST"; 318 case 0x6ffffffc: return "XXX:VERDEF"; 319 case 0x6ffffffd: return "SHT_SUNW(GNU)_verdef"; 320 case 0x6ffffffe: return "SHT_SUNW(GNU)_verneed"; 321 case 0x6fffffff: return "SHT_SUNW(GNU)_versym"; 322 /* 0x70000000 - 0x7fffffff processor-specific semantics */ 323 case 0x70000000: return "SHT_IA_64_EXT"; 324 case 0x70000001: return "SHT_IA_64_UNWIND"; 325 case 0x7ffffffd: return "XXX:AUXILIARY"; 326 case 0x7fffffff: return "XXX:FILTER"; 327 /* 0x80000000 - 0xffffffff application programs */ 328 default: return "ERROR: SHT NOT DEFINED"; 329 } 330 } 331 332 const char *sh_flags[] = { 333 "", "SHF_WRITE", "SHF_ALLOC", "SHF_WRITE|SHF_ALLOC", "SHF_EXECINSTR", 334 "SHF_WRITE|SHF_EXECINSTR", "SHF_ALLOC|SHF_EXECINSTR", 335 "SHF_WRITE|SHF_ALLOC|SHF_EXECINSTR" 336 }; 337 338 const char *st_types[] = { 339 "STT_NOTYPE", "STT_OBJECT", "STT_FUNC", "STT_SECTION", "STT_FILE" 340 }; 341 342 const char *st_bindings[] = { 343 "STB_LOCAL", "STB_GLOBAL", "STB_WEAK" 344 }; 345 346 char *dynstr; 347 char *shstrtab; 348 char *strtab; 349 FILE *out; 350 351 u_int64_t elf_get_byte(Elf32_Ehdr *e, void *base, elf_member_t member); 352 u_int64_t elf_get_quarter(Elf32_Ehdr *e, void *base, elf_member_t member); 353 u_int64_t elf_get_half(Elf32_Ehdr *e, void *base, elf_member_t member); 354 u_int64_t elf_get_word(Elf32_Ehdr *e, void *base, elf_member_t member); 355 u_int64_t elf_get_quad(Elf32_Ehdr *e, void *base, elf_member_t member); 356 357 void elf_print_ehdr(Elf32_Ehdr *e); 358 void elf_print_phdr(Elf32_Ehdr *e, void *p); 359 void elf_print_shdr(Elf32_Ehdr *e, void *sh); 360 void elf_print_symtab(Elf32_Ehdr *e, void *sh, char *str); 361 void elf_print_dynamic(Elf32_Ehdr *e, void *sh); 362 void elf_print_rel(Elf32_Ehdr *e, void *r); 363 void elf_print_rela(Elf32_Ehdr *e, void *ra); 364 void elf_print_interp(Elf32_Ehdr *e, void *p); 365 void elf_print_got(Elf32_Ehdr *e, void *sh); 366 void elf_print_hash(Elf32_Ehdr *e, void *sh); 367 void elf_print_note(Elf32_Ehdr *e, void *sh); 368 369 void usage(void); 370 371 int 372 main(int ac, char **av) 373 { 374 u_int64_t phoff; 375 u_int64_t shoff; 376 u_int64_t phentsize; 377 u_int64_t phnum; 378 u_int64_t shentsize; 379 u_int64_t shnum; 380 u_int64_t shstrndx; 381 u_int64_t offset; 382 u_int64_t name; 383 u_int64_t type; 384 struct stat sb; 385 u_int flags; 386 Elf32_Ehdr *e; 387 void *p; 388 void *sh; 389 void *v; 390 int fd; 391 int ch; 392 int i; 393 394 out = stdout; 395 flags = 0; 396 while ((ch = getopt(ac, av, "acdeiGhnprsw:")) != -1) 397 switch (ch) { 398 case 'a': 399 flags = ED_ALL; 400 break; 401 case 'c': 402 flags |= ED_SHDR; 403 break; 404 case 'd': 405 flags |= ED_DYN; 406 break; 407 case 'e': 408 flags |= ED_EHDR; 409 break; 410 case 'i': 411 flags |= ED_INTERP; 412 break; 413 case 'G': 414 flags |= ED_GOT; 415 break; 416 case 'h': 417 flags |= ED_HASH; 418 break; 419 case 'n': 420 flags |= ED_NOTE; 421 break; 422 case 'p': 423 flags |= ED_PHDR; 424 break; 425 case 'r': 426 flags |= ED_REL; 427 break; 428 case 's': 429 flags |= ED_SYMTAB; 430 break; 431 case 'w': 432 if ((out = fopen(optarg, "w")) == NULL) 433 err(1, "%s", optarg); 434 break; 435 case '?': 436 default: 437 usage(); 438 } 439 ac -= optind; 440 av += optind; 441 if (ac == 0 || flags == 0) 442 usage(); 443 if ((fd = open(*av, O_RDONLY)) < 0 || 444 fstat(fd, &sb) < 0) 445 err(1, "%s", *av); 446 e = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0); 447 if (e == MAP_FAILED) 448 err(1, NULL); 449 if (!IS_ELF(*(Elf32_Ehdr *)e)) 450 errx(1, "not an elf file"); 451 phoff = elf_get_off(e, e, E_PHOFF); 452 shoff = elf_get_off(e, e, E_SHOFF); 453 phentsize = elf_get_quarter(e, e, E_PHENTSIZE); 454 phnum = elf_get_quarter(e, e, E_PHNUM); 455 shentsize = elf_get_quarter(e, e, E_SHENTSIZE); 456 shnum = elf_get_quarter(e, e, E_SHNUM); 457 shstrndx = elf_get_quarter(e, e, E_SHSTRNDX); 458 p = (char *)e + phoff; 459 sh = (char *)e + shoff; 460 offset = elf_get_off(e, (char *)sh + shstrndx * shentsize, SH_OFFSET); 461 shstrtab = (char *)e + offset; 462 for (i = 0; (u_int64_t)i < shnum; i++) { 463 name = elf_get_word(e, (char *)sh + i * shentsize, SH_NAME); 464 offset = elf_get_off(e, (char *)sh + i * shentsize, SH_OFFSET); 465 if (strcmp(shstrtab + name, ".strtab") == 0) 466 strtab = (char *)e + offset; 467 if (strcmp(shstrtab + name, ".dynstr") == 0) 468 dynstr = (char *)e + offset; 469 } 470 if (flags & ED_EHDR) 471 elf_print_ehdr(e); 472 if (flags & ED_PHDR) 473 elf_print_phdr(e, p); 474 if (flags & ED_SHDR) 475 elf_print_shdr(e, sh); 476 for (i = 0; (u_int64_t)i < phnum; i++) { 477 v = (char *)p + i * phentsize; 478 type = elf_get_word(e, v, P_TYPE); 479 switch (type) { 480 case PT_INTERP: 481 if (flags & ED_INTERP) 482 elf_print_interp(e, v); 483 break; 484 case PT_NULL: 485 case PT_LOAD: 486 case PT_DYNAMIC: 487 case PT_NOTE: 488 case PT_SHLIB: 489 case PT_PHDR: 490 break; 491 } 492 } 493 for (i = 0; (u_int64_t)i < shnum; i++) { 494 v = (char *)sh + i * shentsize; 495 type = elf_get_word(e, v, SH_TYPE); 496 switch (type) { 497 case SHT_SYMTAB: 498 if (flags & ED_SYMTAB) 499 elf_print_symtab(e, v, strtab); 500 break; 501 case SHT_DYNAMIC: 502 if (flags & ED_DYN) 503 elf_print_dynamic(e, v); 504 break; 505 case SHT_RELA: 506 if (flags & ED_REL) 507 elf_print_rela(e, v); 508 break; 509 case SHT_REL: 510 if (flags & ED_REL) 511 elf_print_rel(e, v); 512 break; 513 case SHT_NOTE: 514 name = elf_get_word(e, v, SH_NAME); 515 if (flags & ED_NOTE && 516 strcmp(shstrtab + name, ".note.ABI-tag") == 0) 517 elf_print_note(e, v); 518 break; 519 case SHT_DYNSYM: 520 if (flags & ED_SYMTAB) 521 elf_print_symtab(e, v, dynstr); 522 break; 523 case SHT_PROGBITS: 524 name = elf_get_word(e, v, SH_NAME); 525 if (flags & ED_GOT && 526 strcmp(shstrtab + name, ".got") == 0) 527 elf_print_got(e, v); 528 break; 529 case SHT_HASH: 530 if (flags & ED_HASH) 531 elf_print_hash(e, v); 532 break; 533 case SHT_NULL: 534 case SHT_STRTAB: 535 case SHT_NOBITS: 536 case SHT_SHLIB: 537 break; 538 } 539 } 540 541 return 0; 542 } 543 544 void 545 elf_print_ehdr(Elf32_Ehdr *e) 546 { 547 u_int64_t class; 548 u_int64_t data; 549 u_int64_t osabi; 550 u_int64_t type; 551 u_int64_t machine; 552 u_int64_t version; 553 u_int64_t entry; 554 u_int64_t phoff; 555 u_int64_t shoff; 556 u_int64_t flags; 557 u_int64_t ehsize; 558 u_int64_t phentsize; 559 u_int64_t phnum; 560 u_int64_t shentsize; 561 u_int64_t shnum; 562 u_int64_t shstrndx; 563 564 class = elf_get_byte(e, e, E_CLASS); 565 data = elf_get_byte(e, e, E_DATA); 566 osabi = elf_get_byte(e, e, E_OSABI); 567 type = elf_get_quarter(e, e, E_TYPE); 568 machine = elf_get_quarter(e, e, E_MACHINE); 569 version = elf_get_word(e, e, E_VERSION); 570 entry = elf_get_addr(e, e, E_ENTRY); 571 phoff = elf_get_off(e, e, E_PHOFF); 572 shoff = elf_get_off(e, e, E_SHOFF); 573 flags = elf_get_word(e, e, E_FLAGS); 574 ehsize = elf_get_quarter(e, e, E_EHSIZE); 575 phentsize = elf_get_quarter(e, e, E_PHENTSIZE); 576 phnum = elf_get_quarter(e, e, E_PHNUM); 577 shentsize = elf_get_quarter(e, e, E_SHENTSIZE); 578 shnum = elf_get_quarter(e, e, E_SHNUM); 579 shstrndx = elf_get_quarter(e, e, E_SHSTRNDX); 580 fprintf(out, "\nelf header:\n"); 581 fprintf(out, "\n"); 582 fprintf(out, "\te_ident: %s %s %s\n", ei_classes[class], ei_data[data], 583 ei_abis[osabi]); 584 fprintf(out, "\te_type: %s\n", e_types[type]); 585 fprintf(out, "\te_machine: %s\n", e_machines(machine)); 586 fprintf(out, "\te_version: %s\n", ei_versions[version]); 587 fprintf(out, "\te_entry: %#jx\n", (intmax_t)entry); 588 fprintf(out, "\te_phoff: %jd\n", (intmax_t)phoff); 589 fprintf(out, "\te_shoff: %jd\n", (intmax_t)shoff); 590 fprintf(out, "\te_flags: %jd\n", (intmax_t)flags); 591 fprintf(out, "\te_ehsize: %jd\n", (intmax_t)ehsize); 592 fprintf(out, "\te_phentsize: %jd\n", (intmax_t)phentsize); 593 fprintf(out, "\te_phnum: %jd\n", (intmax_t)phnum); 594 fprintf(out, "\te_shentsize: %jd\n", (intmax_t)shentsize); 595 fprintf(out, "\te_shnum: %jd\n", (intmax_t)shnum); 596 fprintf(out, "\te_shstrndx: %jd\n", (intmax_t)shstrndx); 597 } 598 599 void 600 elf_print_phdr(Elf32_Ehdr *e, void *p) 601 { 602 u_int64_t phentsize; 603 u_int64_t phnum; 604 u_int64_t type; 605 u_int64_t offset; 606 u_int64_t vaddr; 607 u_int64_t paddr; 608 u_int64_t filesz; 609 u_int64_t memsz; 610 u_int64_t flags; 611 u_int64_t align; 612 void *v; 613 int i; 614 615 phentsize = elf_get_quarter(e, e, E_PHENTSIZE); 616 phnum = elf_get_quarter(e, e, E_PHNUM); 617 fprintf(out, "\nprogram header:\n"); 618 for (i = 0; (u_int64_t)i < phnum; i++) { 619 v = (char *)p + i * phentsize; 620 type = elf_get_word(e, v, P_TYPE); 621 offset = elf_get_off(e, v, P_OFFSET); 622 vaddr = elf_get_addr(e, v, P_VADDR); 623 paddr = elf_get_addr(e, v, P_PADDR); 624 filesz = elf_get_size(e, v, P_FILESZ); 625 memsz = elf_get_size(e, v, P_MEMSZ); 626 flags = elf_get_word(e, v, P_FLAGS); 627 align = elf_get_size(e, v, P_ALIGN); 628 fprintf(out, "\n"); 629 fprintf(out, "entry: %d\n", i); 630 fprintf(out, "\tp_type: %s\n", p_types[type & 0x7]); 631 fprintf(out, "\tp_offset: %jd\n", (intmax_t)offset); 632 fprintf(out, "\tp_vaddr: %#jx\n", (intmax_t)vaddr); 633 fprintf(out, "\tp_paddr: %#jx\n", (intmax_t)paddr); 634 fprintf(out, "\tp_filesz: %jd\n", (intmax_t)filesz); 635 fprintf(out, "\tp_memsz: %jd\n", (intmax_t)memsz); 636 fprintf(out, "\tp_flags: %s\n", p_flags[flags]); 637 fprintf(out, "\tp_align: %jd\n", (intmax_t)align); 638 } 639 } 640 641 void 642 elf_print_shdr(Elf32_Ehdr *e, void *sh) 643 { 644 u_int64_t shentsize; 645 u_int64_t shnum; 646 u_int64_t name; 647 u_int64_t type; 648 u_int64_t flags; 649 u_int64_t addr; 650 u_int64_t offset; 651 u_int64_t size; 652 u_int64_t shlink; 653 u_int64_t info; 654 u_int64_t addralign; 655 u_int64_t entsize; 656 void *v; 657 int i; 658 659 shentsize = elf_get_quarter(e, e, E_SHENTSIZE); 660 shnum = elf_get_quarter(e, e, E_SHNUM); 661 fprintf(out, "\nsection header:\n"); 662 for (i = 0; (u_int64_t)i < shnum; i++) { 663 v = (char *)sh + i * shentsize; 664 name = elf_get_word(e, v, SH_NAME); 665 type = elf_get_word(e, v, SH_TYPE); 666 flags = elf_get_word(e, v, SH_FLAGS); 667 addr = elf_get_addr(e, v, SH_ADDR); 668 offset = elf_get_off(e, v, SH_OFFSET); 669 size = elf_get_size(e, v, SH_SIZE); 670 shlink = elf_get_word(e, v, SH_LINK); 671 info = elf_get_word(e, v, SH_INFO); 672 addralign = elf_get_size(e, v, SH_ADDRALIGN); 673 entsize = elf_get_size(e, v, SH_ENTSIZE); 674 fprintf(out, "\n"); 675 fprintf(out, "entry: %d\n", i); 676 fprintf(out, "\tsh_name: %s\n", shstrtab + name); 677 fprintf(out, "\tsh_type: %s\n", sh_types(type)); 678 fprintf(out, "\tsh_flags: %s\n", sh_flags[flags & 0x7]); 679 fprintf(out, "\tsh_addr: %#jx\n", addr); 680 fprintf(out, "\tsh_offset: %jd\n", (intmax_t)offset); 681 fprintf(out, "\tsh_size: %jd\n", (intmax_t)size); 682 fprintf(out, "\tsh_link: %jd\n", (intmax_t)shlink); 683 fprintf(out, "\tsh_info: %jd\n", (intmax_t)info); 684 fprintf(out, "\tsh_addralign: %jd\n", (intmax_t)addralign); 685 fprintf(out, "\tsh_entsize: %jd\n", (intmax_t)entsize); 686 } 687 } 688 689 void 690 elf_print_symtab(Elf32_Ehdr *e, void *sh, char *str) 691 { 692 u_int64_t offset; 693 u_int64_t entsize; 694 u_int64_t size; 695 u_int64_t name; 696 u_int64_t value; 697 u_int64_t info; 698 u_int64_t shndx; 699 void *st; 700 int len; 701 int i; 702 703 offset = elf_get_off(e, sh, SH_OFFSET); 704 entsize = elf_get_size(e, sh, SH_ENTSIZE); 705 size = elf_get_size(e, sh, SH_SIZE); 706 name = elf_get_word(e, sh, SH_NAME); 707 len = size / entsize; 708 fprintf(out, "\nsymbol table (%s):\n", shstrtab + name); 709 for (i = 0; i < len; i++) { 710 st = (char *)e + offset + i * entsize; 711 name = elf_get_word(e, st, ST_NAME); 712 value = elf_get_addr(e, st, ST_VALUE); 713 size = elf_get_size(e, st, ST_SIZE); 714 info = elf_get_byte(e, st, ST_INFO); 715 shndx = elf_get_quarter(e, st, ST_SHNDX); 716 fprintf(out, "\n"); 717 fprintf(out, "entry: %d\n", i); 718 fprintf(out, "\tst_name: %s\n", str + name); 719 fprintf(out, "\tst_value: %#jx\n", value); 720 fprintf(out, "\tst_size: %jd\n", (intmax_t)size); 721 fprintf(out, "\tst_info: %s %s\n", 722 st_types[ELF32_ST_TYPE(info)], 723 st_bindings[ELF32_ST_BIND(info)]); 724 fprintf(out, "\tst_shndx: %jd\n", (intmax_t)shndx); 725 } 726 } 727 728 void 729 elf_print_dynamic(Elf32_Ehdr *e, void *sh) 730 { 731 u_int64_t offset; 732 u_int64_t entsize; 733 u_int64_t size; 734 int64_t tag; 735 u_int64_t ptr; 736 u_int64_t val; 737 void *d; 738 int i; 739 740 offset = elf_get_off(e, sh, SH_OFFSET); 741 entsize = elf_get_size(e, sh, SH_ENTSIZE); 742 size = elf_get_size(e, sh, SH_SIZE); 743 fprintf(out, "\ndynamic:\n"); 744 for (i = 0; (u_int64_t)i < size / entsize; i++) { 745 d = (char *)e + offset + i * entsize; 746 tag = elf_get_size(e, d, D_TAG); 747 ptr = elf_get_size(e, d, D_PTR); 748 val = elf_get_addr(e, d, D_VAL); 749 fprintf(out, "\n"); 750 fprintf(out, "entry: %d\n", i); 751 fprintf(out, "\td_tag: %s\n", d_tags(tag)); 752 switch (tag) { 753 case DT_NEEDED: 754 case DT_SONAME: 755 case DT_RPATH: 756 fprintf(out, "\td_val: %s\n", dynstr + val); 757 break; 758 case DT_PLTRELSZ: 759 case DT_RELA: 760 case DT_RELASZ: 761 case DT_RELAENT: 762 case DT_STRSZ: 763 case DT_SYMENT: 764 case DT_RELSZ: 765 case DT_RELENT: 766 case DT_PLTREL: 767 fprintf(out, "\td_val: %jd\n", (intmax_t)val); 768 break; 769 case DT_PLTGOT: 770 case DT_HASH: 771 case DT_STRTAB: 772 case DT_SYMTAB: 773 case DT_INIT: 774 case DT_FINI: 775 case DT_REL: 776 case DT_JMPREL: 777 fprintf(out, "\td_ptr: %#jx\n", ptr); 778 break; 779 case DT_NULL: 780 case DT_SYMBOLIC: 781 case DT_DEBUG: 782 case DT_TEXTREL: 783 break; 784 } 785 } 786 } 787 788 void 789 elf_print_rela(Elf32_Ehdr *e, void *sh) 790 { 791 u_int64_t offset; 792 u_int64_t entsize; 793 u_int64_t size; 794 u_int64_t name; 795 u_int64_t info; 796 int64_t addend; 797 void *ra; 798 void *v; 799 int i; 800 801 offset = elf_get_off(e, sh, SH_OFFSET); 802 entsize = elf_get_size(e, sh, SH_ENTSIZE); 803 size = elf_get_size(e, sh, SH_SIZE); 804 name = elf_get_word(e, sh, SH_NAME); 805 v = (char *)e + offset; 806 fprintf(out, "\nrelocation with addend (%s):\n", shstrtab + name); 807 for (i = 0; (u_int64_t)i < size / entsize; i++) { 808 ra = (char *)v + i * entsize; 809 offset = elf_get_addr(e, ra, RA_OFFSET); 810 info = elf_get_word(e, ra, RA_INFO); 811 addend = elf_get_off(e, ra, RA_ADDEND); 812 fprintf(out, "\n"); 813 fprintf(out, "entry: %d\n", i); 814 fprintf(out, "\tr_offset: %#jx\n", offset); 815 fprintf(out, "\tr_info: %jd\n", (intmax_t)info); 816 fprintf(out, "\tr_addend: %jd\n", (intmax_t)addend); 817 } 818 } 819 820 void 821 elf_print_rel(Elf32_Ehdr *e, void *sh) 822 { 823 u_int64_t offset; 824 u_int64_t entsize; 825 u_int64_t size; 826 u_int64_t name; 827 u_int64_t info; 828 void *r; 829 void *v; 830 int i; 831 832 offset = elf_get_off(e, sh, SH_OFFSET); 833 entsize = elf_get_size(e, sh, SH_ENTSIZE); 834 size = elf_get_size(e, sh, SH_SIZE); 835 name = elf_get_word(e, sh, SH_NAME); 836 v = (char *)e + offset; 837 fprintf(out, "\nrelocation (%s):\n", shstrtab + name); 838 for (i = 0; (u_int64_t)i < size / entsize; i++) { 839 r = (char *)v + i * entsize; 840 offset = elf_get_addr(e, r, R_OFFSET); 841 info = elf_get_word(e, r, R_INFO); 842 fprintf(out, "\n"); 843 fprintf(out, "entry: %d\n", i); 844 fprintf(out, "\tr_offset: %#jx\n", offset); 845 fprintf(out, "\tr_info: %jd\n", (intmax_t)info); 846 } 847 } 848 849 void 850 elf_print_interp(Elf32_Ehdr *e, void *p) 851 { 852 u_int64_t offset; 853 char *s; 854 855 offset = elf_get_off(e, p, P_OFFSET); 856 s = (char *)e + offset; 857 fprintf(out, "\ninterp:\n"); 858 fprintf(out, "\t%s\n", s); 859 } 860 861 void 862 elf_print_got(Elf32_Ehdr *e, void *sh) 863 { 864 u_int64_t offset; 865 u_int64_t addralign; 866 u_int64_t size; 867 u_int64_t addr; 868 void *v; 869 int i; 870 871 offset = elf_get_off(e, sh, SH_OFFSET); 872 addralign = elf_get_size(e, sh, SH_ADDRALIGN); 873 size = elf_get_size(e, sh, SH_SIZE); 874 v = (char *)e + offset; 875 fprintf(out, "\nglobal offset table:\n"); 876 for (i = 0; (u_int64_t)i < size / addralign; i++) { 877 addr = elf_get_addr(e, (char *)v + i * addralign, 0); 878 fprintf(out, "\n"); 879 fprintf(out, "entry: %d\n", i); 880 fprintf(out, "\t%#jx\n", addr); 881 } 882 } 883 884 void 885 elf_print_hash(Elf32_Ehdr *e __unused, void *sh __unused) 886 { 887 } 888 889 void 890 elf_print_note(Elf32_Ehdr *e, void *sh) 891 { 892 u_int64_t offset; 893 u_int64_t size; 894 u_int64_t name; 895 u_int32_t namesz; 896 u_int32_t descsz; 897 u_int32_t type; 898 u_int32_t desc; 899 char *n, *s; 900 901 offset = elf_get_off(e, sh, SH_OFFSET); 902 size = elf_get_size(e, sh, SH_SIZE); 903 name = elf_get_word(e, sh, SH_NAME); 904 n = (char *)e + offset; 905 fprintf(out, "\nnote (%s):\n", shstrtab + name); 906 while (n < ((char *)e + offset + size)) { 907 namesz = elf_get_word(e, n, N_NAMESZ); 908 descsz = elf_get_word(e, n, N_DESCSZ); 909 type = elf_get_word(e, n, N_TYPE); 910 s = n + sizeof(Elf_Note); 911 desc = elf_get_word(e, n + sizeof(Elf_Note) + namesz, 0); 912 fprintf(out, "\t%s %d\n", s, desc); 913 n += sizeof(Elf_Note) + namesz + descsz; 914 } 915 } 916 917 u_int64_t 918 elf_get_byte(Elf32_Ehdr *e, void *base, elf_member_t member) 919 { 920 u_int64_t val; 921 922 val = 0; 923 switch (e->e_ident[EI_CLASS]) { 924 case ELFCLASS32: 925 val = ((char *)base)[elf32_offsets[member]]; 926 break; 927 case ELFCLASS64: 928 val = ((char *)base)[elf64_offsets[member]]; 929 break; 930 case ELFCLASSNONE: 931 errx(1, "invalid class"); 932 } 933 934 return val; 935 } 936 937 u_int64_t 938 elf_get_quarter(Elf32_Ehdr *e, void *base, elf_member_t member) 939 { 940 u_int64_t val; 941 942 val = 0; 943 switch (e->e_ident[EI_CLASS]) { 944 case ELFCLASS32: 945 base = (char *)base + elf32_offsets[member]; 946 switch (e->e_ident[EI_DATA]) { 947 case ELFDATA2MSB: 948 val = be16dec(base); 949 break; 950 case ELFDATA2LSB: 951 val = le16dec(base); 952 break; 953 case ELFDATANONE: 954 errx(1, "invalid data format"); 955 } 956 break; 957 case ELFCLASS64: 958 base = (char *)base + elf64_offsets[member]; 959 switch (e->e_ident[EI_DATA]) { 960 case ELFDATA2MSB: 961 val = be16dec(base); 962 break; 963 case ELFDATA2LSB: 964 val = le16dec(base); 965 break; 966 case ELFDATANONE: 967 errx(1, "invalid data format"); 968 } 969 break; 970 case ELFCLASSNONE: 971 errx(1, "invalid class"); 972 } 973 974 return val; 975 } 976 977 u_int64_t 978 elf_get_half(Elf32_Ehdr *e, void *base, elf_member_t member) 979 { 980 u_int64_t val; 981 982 val = 0; 983 switch (e->e_ident[EI_CLASS]) { 984 case ELFCLASS32: 985 base = (char *)base + elf32_offsets[member]; 986 switch (e->e_ident[EI_DATA]) { 987 case ELFDATA2MSB: 988 val = be16dec(base); 989 break; 990 case ELFDATA2LSB: 991 val = le16dec(base); 992 break; 993 case ELFDATANONE: 994 errx(1, "invalid data format"); 995 } 996 break; 997 case ELFCLASS64: 998 base = (char *)base + elf64_offsets[member]; 999 switch (e->e_ident[EI_DATA]) { 1000 case ELFDATA2MSB: 1001 val = be32dec(base); 1002 break; 1003 case ELFDATA2LSB: 1004 val = le32dec(base); 1005 break; 1006 case ELFDATANONE: 1007 errx(1, "invalid data format"); 1008 } 1009 break; 1010 case ELFCLASSNONE: 1011 errx(1, "invalid class"); 1012 } 1013 1014 return val; 1015 } 1016 1017 u_int64_t 1018 elf_get_word(Elf32_Ehdr *e, void *base, elf_member_t member) 1019 { 1020 u_int64_t val; 1021 1022 val = 0; 1023 switch (e->e_ident[EI_CLASS]) { 1024 case ELFCLASS32: 1025 base = (char *)base + elf32_offsets[member]; 1026 switch (e->e_ident[EI_DATA]) { 1027 case ELFDATA2MSB: 1028 val = be32dec(base); 1029 break; 1030 case ELFDATA2LSB: 1031 val = le32dec(base); 1032 break; 1033 case ELFDATANONE: 1034 errx(1, "invalid data format"); 1035 } 1036 break; 1037 case ELFCLASS64: 1038 base = (char *)base + elf64_offsets[member]; 1039 switch (e->e_ident[EI_DATA]) { 1040 case ELFDATA2MSB: 1041 val = be32dec(base); 1042 break; 1043 case ELFDATA2LSB: 1044 val = le32dec(base); 1045 break; 1046 case ELFDATANONE: 1047 errx(1, "invalid data format"); 1048 } 1049 break; 1050 case ELFCLASSNONE: 1051 errx(1, "invalid class"); 1052 } 1053 1054 return val; 1055 } 1056 1057 u_int64_t 1058 elf_get_quad(Elf32_Ehdr *e, void *base, elf_member_t member) 1059 { 1060 u_int64_t val; 1061 1062 val = 0; 1063 switch (e->e_ident[EI_CLASS]) { 1064 case ELFCLASS32: 1065 base = (char *)base + elf32_offsets[member]; 1066 switch (e->e_ident[EI_DATA]) { 1067 case ELFDATA2MSB: 1068 val = be32dec(base); 1069 break; 1070 case ELFDATA2LSB: 1071 val = le32dec(base); 1072 break; 1073 case ELFDATANONE: 1074 errx(1, "invalid data format"); 1075 } 1076 break; 1077 case ELFCLASS64: 1078 base = (char *)base + elf64_offsets[member]; 1079 switch (e->e_ident[EI_DATA]) { 1080 case ELFDATA2MSB: 1081 val = be64dec(base); 1082 break; 1083 case ELFDATA2LSB: 1084 val = le64dec(base); 1085 break; 1086 case ELFDATANONE: 1087 errx(1, "invalid data format"); 1088 } 1089 break; 1090 case ELFCLASSNONE: 1091 errx(1, "invalid class"); 1092 } 1093 1094 return val; 1095 } 1096 1097 void 1098 usage(void) 1099 { 1100 fprintf(stderr, "usage: elfdump -a | -cdeGhinprs [-w file] file\n"); 1101 exit(1); 1102 } 1103