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