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