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