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