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 #define NT_ELEM(x) [x] = #x, 321 static const char *nt_types[] = { 322 "", 323 NT_ELEM(NT_FREEBSD_ABI_TAG) 324 NT_ELEM(NT_FREEBSD_NOINIT_TAG) 325 NT_ELEM(NT_FREEBSD_ARCH_TAG) 326 NT_ELEM(NT_FREEBSD_FEATURE_CTL) 327 }; 328 329 /* http://www.sco.com/developers/gabi/latest/ch4.sheader.html#sh_type */ 330 static const char * 331 sh_types(uint64_t machine, uint64_t sht) { 332 static char unknown_buf[64]; 333 334 if (sht < 0x60000000) { 335 switch (sht) { 336 case SHT_NULL: return "SHT_NULL"; 337 case SHT_PROGBITS: return "SHT_PROGBITS"; 338 case SHT_SYMTAB: return "SHT_SYMTAB"; 339 case SHT_STRTAB: return "SHT_STRTAB"; 340 case SHT_RELA: return "SHT_RELA"; 341 case SHT_HASH: return "SHT_HASH"; 342 case SHT_DYNAMIC: return "SHT_DYNAMIC"; 343 case SHT_NOTE: return "SHT_NOTE"; 344 case SHT_NOBITS: return "SHT_NOBITS"; 345 case SHT_REL: return "SHT_REL"; 346 case SHT_SHLIB: return "SHT_SHLIB"; 347 case SHT_DYNSYM: return "SHT_DYNSYM"; 348 case SHT_INIT_ARRAY: return "SHT_INIT_ARRAY"; 349 case SHT_FINI_ARRAY: return "SHT_FINI_ARRAY"; 350 case SHT_PREINIT_ARRAY: return "SHT_PREINIT_ARRAY"; 351 case SHT_GROUP: return "SHT_GROUP"; 352 case SHT_SYMTAB_SHNDX: return "SHT_SYMTAB_SHNDX"; 353 } 354 snprintf(unknown_buf, sizeof(unknown_buf), 355 "ERROR: SHT %ju NOT DEFINED", (uintmax_t)sht); 356 return (unknown_buf); 357 } else if (sht < 0x70000000) { 358 /* 0x60000000-0x6fffffff operating system-specific semantics */ 359 switch (sht) { 360 case 0x6ffffff0: return "XXX:VERSYM"; 361 case SHT_SUNW_dof: return "SHT_SUNW_dof"; 362 case SHT_GNU_HASH: return "SHT_GNU_HASH"; 363 case 0x6ffffff7: return "SHT_GNU_LIBLIST"; 364 case 0x6ffffffc: return "XXX:VERDEF"; 365 case SHT_SUNW_verdef: return "SHT_SUNW(GNU)_verdef"; 366 case SHT_SUNW_verneed: return "SHT_SUNW(GNU)_verneed"; 367 case SHT_SUNW_versym: return "SHT_SUNW(GNU)_versym"; 368 } 369 snprintf(unknown_buf, sizeof(unknown_buf), 370 "ERROR: OS-SPECIFIC SHT 0x%jx NOT DEFINED", 371 (uintmax_t)sht); 372 return (unknown_buf); 373 } else if (sht < 0x80000000) { 374 /* 0x70000000-0x7fffffff processor-specific semantics */ 375 switch (machine) { 376 case EM_ARM: 377 switch (sht) { 378 case SHT_ARM_EXIDX: return "SHT_ARM_EXIDX"; 379 case SHT_ARM_PREEMPTMAP:return "SHT_ARM_PREEMPTMAP"; 380 case SHT_ARM_ATTRIBUTES:return "SHT_ARM_ATTRIBUTES"; 381 case SHT_ARM_DEBUGOVERLAY: 382 return "SHT_ARM_DEBUGOVERLAY"; 383 case SHT_ARM_OVERLAYSECTION: 384 return "SHT_ARM_OVERLAYSECTION"; 385 } 386 break; 387 case EM_IA_64: 388 switch (sht) { 389 case 0x70000000: return "SHT_IA_64_EXT"; 390 case 0x70000001: return "SHT_IA_64_UNWIND"; 391 } 392 break; 393 case EM_MIPS: 394 switch (sht) { 395 case SHT_MIPS_REGINFO: return "SHT_MIPS_REGINFO"; 396 case SHT_MIPS_OPTIONS: return "SHT_MIPS_OPTIONS"; 397 case SHT_MIPS_ABIFLAGS: return "SHT_MIPS_ABIFLAGS"; 398 } 399 break; 400 } 401 switch (sht) { 402 case 0x7ffffffd: return "XXX:AUXILIARY"; 403 case 0x7fffffff: return "XXX:FILTER"; 404 } 405 snprintf(unknown_buf, sizeof(unknown_buf), 406 "ERROR: PROCESSOR-SPECIFIC SHT 0x%jx NOT DEFINED", 407 (uintmax_t)sht); 408 return (unknown_buf); 409 } else { 410 /* 0x80000000-0xffffffff application programs */ 411 snprintf(unknown_buf, sizeof(unknown_buf), 412 "ERROR: SHT 0x%jx NOT DEFINED", 413 (uintmax_t)sht); 414 return (unknown_buf); 415 } 416 } 417 418 static const char *sh_flags[] = { 419 "", "SHF_WRITE", "SHF_ALLOC", "SHF_WRITE|SHF_ALLOC", "SHF_EXECINSTR", 420 "SHF_WRITE|SHF_EXECINSTR", "SHF_ALLOC|SHF_EXECINSTR", 421 "SHF_WRITE|SHF_ALLOC|SHF_EXECINSTR" 422 }; 423 424 static const char * 425 st_type(unsigned int mach, unsigned int type) 426 { 427 static char s_type[32]; 428 429 switch (type) { 430 case STT_NOTYPE: return "STT_NOTYPE"; 431 case STT_OBJECT: return "STT_OBJECT"; 432 case STT_FUNC: return "STT_FUNC"; 433 case STT_SECTION: return "STT_SECTION"; 434 case STT_FILE: return "STT_FILE"; 435 case STT_COMMON: return "STT_COMMON"; 436 case STT_TLS: return "STT_TLS"; 437 case 13: 438 if (mach == EM_SPARCV9) 439 return "STT_SPARC_REGISTER"; 440 break; 441 } 442 snprintf(s_type, sizeof(s_type), "<unknown: %#x>", type); 443 return (s_type); 444 } 445 446 static const char *st_bindings[] = { 447 "STB_LOCAL", "STB_GLOBAL", "STB_WEAK" 448 }; 449 450 static char *dynstr; 451 static char *shstrtab; 452 static char *strtab; 453 static FILE *out; 454 455 static u_int64_t elf_get_byte(Elf32_Ehdr *e, void *base, elf_member_t member); 456 static u_int64_t elf_get_quarter(Elf32_Ehdr *e, void *base, 457 elf_member_t member); 458 #if 0 459 static u_int64_t elf_get_half(Elf32_Ehdr *e, void *base, elf_member_t member); 460 #endif 461 static u_int64_t elf_get_word(Elf32_Ehdr *e, void *base, elf_member_t member); 462 static u_int64_t elf_get_quad(Elf32_Ehdr *e, void *base, elf_member_t member); 463 464 static void elf_print_ehdr(Elf32_Ehdr *e, void *sh); 465 static void elf_print_phdr(Elf32_Ehdr *e, void *p); 466 static void elf_print_shdr(Elf32_Ehdr *e, void *sh); 467 static void elf_print_symtab(Elf32_Ehdr *e, void *sh, char *str); 468 static void elf_print_dynamic(Elf32_Ehdr *e, void *sh); 469 static void elf_print_rel(Elf32_Ehdr *e, void *r); 470 static void elf_print_rela(Elf32_Ehdr *e, void *ra); 471 static void elf_print_interp(Elf32_Ehdr *e, void *p); 472 static void elf_print_got(Elf32_Ehdr *e, void *sh); 473 static void elf_print_hash(Elf32_Ehdr *e, void *sh); 474 static void elf_print_note(Elf32_Ehdr *e, void *sh); 475 476 static void usage(void); 477 478 /* 479 * Helpers for ELF files with shnum or shstrndx values that don't fit in the 480 * ELF header. If the values are too large then an escape value is used to 481 * indicate that the actual value is found in one of section 0's fields. 482 */ 483 static uint64_t 484 elf_get_shnum(Elf32_Ehdr *e, void *sh) 485 { 486 uint64_t shnum; 487 488 shnum = elf_get_quarter(e, e, E_SHNUM); 489 if (shnum == 0) 490 shnum = elf_get_word(e, (char *)sh, SH_SIZE); 491 return shnum; 492 } 493 494 static uint64_t 495 elf_get_shstrndx(Elf32_Ehdr *e, void *sh) 496 { 497 uint64_t shstrndx; 498 499 shstrndx = elf_get_quarter(e, e, E_SHSTRNDX); 500 if (shstrndx == SHN_XINDEX) 501 shstrndx = elf_get_word(e, (char *)sh, SH_LINK); 502 return shstrndx; 503 } 504 505 int 506 main(int ac, char **av) 507 { 508 cap_rights_t rights; 509 u_int64_t phoff; 510 u_int64_t shoff; 511 u_int64_t phentsize; 512 u_int64_t phnum; 513 u_int64_t shentsize; 514 u_int64_t shnum; 515 u_int64_t shstrndx; 516 u_int64_t offset; 517 u_int64_t name; 518 u_int64_t type; 519 struct stat sb; 520 u_int flags; 521 Elf32_Ehdr *e; 522 void *p; 523 void *sh; 524 void *v; 525 int fd; 526 int ch; 527 int i; 528 529 out = stdout; 530 flags = 0; 531 while ((ch = getopt(ac, av, "acdEeiGhnprsw:")) != -1) 532 switch (ch) { 533 case 'a': 534 flags = ED_ALL; 535 break; 536 case 'c': 537 flags |= ED_SHDR; 538 break; 539 case 'd': 540 flags |= ED_DYN; 541 break; 542 case 'E': 543 flags = ED_IS_ELF; 544 break; 545 case 'e': 546 flags |= ED_EHDR; 547 break; 548 case 'i': 549 flags |= ED_INTERP; 550 break; 551 case 'G': 552 flags |= ED_GOT; 553 break; 554 case 'h': 555 flags |= ED_HASH; 556 break; 557 case 'n': 558 flags |= ED_NOTE; 559 break; 560 case 'p': 561 flags |= ED_PHDR; 562 break; 563 case 'r': 564 flags |= ED_REL; 565 break; 566 case 's': 567 flags |= ED_SYMTAB; 568 break; 569 case 'w': 570 if ((out = fopen(optarg, "w")) == NULL) 571 err(1, "%s", optarg); 572 cap_rights_init(&rights, CAP_FSTAT, CAP_WRITE); 573 if (caph_rights_limit(fileno(out), &rights) < 0) 574 err(1, "unable to limit rights for %s", optarg); 575 break; 576 case '?': 577 default: 578 usage(); 579 } 580 ac -= optind; 581 av += optind; 582 if (ac == 0 || flags == 0 || ((flags & ED_IS_ELF) && 583 (ac != 1 || (flags & ~ED_IS_ELF) || out != stdout))) 584 usage(); 585 if ((fd = open(*av, O_RDONLY)) < 0 || 586 fstat(fd, &sb) < 0) 587 err(1, "%s", *av); 588 if ((size_t)sb.st_size < sizeof(Elf32_Ehdr)) { 589 if (flags & ED_IS_ELF) 590 exit(1); 591 errx(1, "not an elf file"); 592 } 593 cap_rights_init(&rights, CAP_MMAP_R); 594 if (caph_rights_limit(fd, &rights) < 0) 595 err(1, "unable to limit rights for %s", *av); 596 cap_rights_init(&rights); 597 if (caph_rights_limit(STDIN_FILENO, &rights) < 0 || 598 caph_limit_stdout() < 0 || caph_limit_stderr() < 0) { 599 err(1, "unable to limit rights for stdio"); 600 } 601 if (caph_enter() < 0) 602 err(1, "unable to enter capability mode"); 603 e = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0); 604 if (e == MAP_FAILED) 605 err(1, NULL); 606 if (!IS_ELF(*e)) { 607 if (flags & ED_IS_ELF) 608 exit(1); 609 errx(1, "not an elf file"); 610 } else if (flags & ED_IS_ELF) 611 exit (0); 612 phoff = elf_get_off(e, e, E_PHOFF); 613 shoff = elf_get_off(e, e, E_SHOFF); 614 phentsize = elf_get_quarter(e, e, E_PHENTSIZE); 615 phnum = elf_get_quarter(e, e, E_PHNUM); 616 shentsize = elf_get_quarter(e, e, E_SHENTSIZE); 617 p = (char *)e + phoff; 618 if (shoff > 0) { 619 sh = (char *)e + shoff; 620 shnum = elf_get_shnum(e, sh); 621 shstrndx = elf_get_shstrndx(e, sh); 622 offset = elf_get_off(e, (char *)sh + shstrndx * shentsize, 623 SH_OFFSET); 624 shstrtab = (char *)e + offset; 625 } else { 626 sh = NULL; 627 shnum = 0; 628 shstrndx = 0; 629 shstrtab = NULL; 630 } 631 for (i = 0; (u_int64_t)i < shnum; i++) { 632 name = elf_get_word(e, (char *)sh + i * shentsize, SH_NAME); 633 offset = elf_get_off(e, (char *)sh + i * shentsize, SH_OFFSET); 634 if (strcmp(shstrtab + name, ".strtab") == 0) 635 strtab = (char *)e + offset; 636 if (strcmp(shstrtab + name, ".dynstr") == 0) 637 dynstr = (char *)e + offset; 638 } 639 if (flags & ED_EHDR) 640 elf_print_ehdr(e, sh); 641 if (flags & ED_PHDR) 642 elf_print_phdr(e, p); 643 if (flags & ED_SHDR) 644 elf_print_shdr(e, sh); 645 for (i = 0; (u_int64_t)i < phnum; i++) { 646 v = (char *)p + i * phentsize; 647 type = elf_get_word(e, v, P_TYPE); 648 switch (type) { 649 case PT_INTERP: 650 if (flags & ED_INTERP) 651 elf_print_interp(e, v); 652 break; 653 case PT_NULL: 654 case PT_LOAD: 655 case PT_DYNAMIC: 656 case PT_NOTE: 657 case PT_SHLIB: 658 case PT_PHDR: 659 break; 660 } 661 } 662 for (i = 0; (u_int64_t)i < shnum; i++) { 663 v = (char *)sh + i * shentsize; 664 type = elf_get_word(e, v, SH_TYPE); 665 switch (type) { 666 case SHT_SYMTAB: 667 if (flags & ED_SYMTAB) 668 elf_print_symtab(e, v, strtab); 669 break; 670 case SHT_DYNAMIC: 671 if (flags & ED_DYN) 672 elf_print_dynamic(e, v); 673 break; 674 case SHT_RELA: 675 if (flags & ED_REL) 676 elf_print_rela(e, v); 677 break; 678 case SHT_REL: 679 if (flags & ED_REL) 680 elf_print_rel(e, v); 681 break; 682 case SHT_NOTE: 683 name = elf_get_word(e, v, SH_NAME); 684 if (flags & ED_NOTE && 685 strcmp(shstrtab + name, ".note.tag") == 0) 686 elf_print_note(e, v); 687 break; 688 case SHT_DYNSYM: 689 if (flags & ED_SYMTAB) 690 elf_print_symtab(e, v, dynstr); 691 break; 692 case SHT_PROGBITS: 693 name = elf_get_word(e, v, SH_NAME); 694 if (flags & ED_GOT && 695 strcmp(shstrtab + name, ".got") == 0) 696 elf_print_got(e, v); 697 break; 698 case SHT_HASH: 699 if (flags & ED_HASH) 700 elf_print_hash(e, v); 701 break; 702 case SHT_NULL: 703 case SHT_STRTAB: 704 case SHT_NOBITS: 705 case SHT_SHLIB: 706 break; 707 } 708 } 709 710 return 0; 711 } 712 713 static void 714 elf_print_ehdr(Elf32_Ehdr *e, void *sh) 715 { 716 u_int64_t class; 717 u_int64_t data; 718 u_int64_t osabi; 719 u_int64_t type; 720 u_int64_t machine; 721 u_int64_t version; 722 u_int64_t entry; 723 u_int64_t phoff; 724 u_int64_t shoff; 725 u_int64_t flags; 726 u_int64_t ehsize; 727 u_int64_t phentsize; 728 u_int64_t phnum; 729 u_int64_t shentsize; 730 u_int64_t shnum; 731 u_int64_t shstrndx; 732 733 class = elf_get_byte(e, e, E_CLASS); 734 data = elf_get_byte(e, e, E_DATA); 735 osabi = elf_get_byte(e, e, E_OSABI); 736 type = elf_get_quarter(e, e, E_TYPE); 737 machine = elf_get_quarter(e, e, E_MACHINE); 738 version = elf_get_word(e, e, E_VERSION); 739 entry = elf_get_addr(e, e, E_ENTRY); 740 phoff = elf_get_off(e, e, E_PHOFF); 741 shoff = elf_get_off(e, e, E_SHOFF); 742 flags = elf_get_word(e, e, E_FLAGS); 743 ehsize = elf_get_quarter(e, e, E_EHSIZE); 744 phentsize = elf_get_quarter(e, e, E_PHENTSIZE); 745 phnum = elf_get_quarter(e, e, E_PHNUM); 746 shentsize = elf_get_quarter(e, e, E_SHENTSIZE); 747 fprintf(out, "\nelf header:\n"); 748 fprintf(out, "\n"); 749 fprintf(out, "\te_ident: %s %s %s\n", ei_classes[class], ei_data[data], 750 ei_abis[osabi]); 751 fprintf(out, "\te_type: %s\n", e_types[type]); 752 fprintf(out, "\te_machine: %s\n", e_machines(machine)); 753 fprintf(out, "\te_version: %s\n", ei_versions[version]); 754 fprintf(out, "\te_entry: %#jx\n", (intmax_t)entry); 755 fprintf(out, "\te_phoff: %jd\n", (intmax_t)phoff); 756 fprintf(out, "\te_shoff: %jd\n", (intmax_t)shoff); 757 fprintf(out, "\te_flags: %jd\n", (intmax_t)flags); 758 fprintf(out, "\te_ehsize: %jd\n", (intmax_t)ehsize); 759 fprintf(out, "\te_phentsize: %jd\n", (intmax_t)phentsize); 760 fprintf(out, "\te_phnum: %jd\n", (intmax_t)phnum); 761 fprintf(out, "\te_shentsize: %jd\n", (intmax_t)shentsize); 762 if (sh != NULL) { 763 shnum = elf_get_shnum(e, sh); 764 shstrndx = elf_get_shstrndx(e, sh); 765 fprintf(out, "\te_shnum: %jd\n", (intmax_t)shnum); 766 fprintf(out, "\te_shstrndx: %jd\n", (intmax_t)shstrndx); 767 } 768 } 769 770 static void 771 elf_print_phdr(Elf32_Ehdr *e, void *p) 772 { 773 u_int64_t phentsize; 774 u_int64_t phnum; 775 u_int64_t type; 776 u_int64_t offset; 777 u_int64_t vaddr; 778 u_int64_t paddr; 779 u_int64_t filesz; 780 u_int64_t memsz; 781 u_int64_t flags; 782 u_int64_t align; 783 void *v; 784 int i; 785 786 phentsize = elf_get_quarter(e, e, E_PHENTSIZE); 787 phnum = elf_get_quarter(e, e, E_PHNUM); 788 fprintf(out, "\nprogram header:\n"); 789 for (i = 0; (u_int64_t)i < phnum; i++) { 790 v = (char *)p + i * phentsize; 791 type = elf_get_word(e, v, P_TYPE); 792 offset = elf_get_off(e, v, P_OFFSET); 793 vaddr = elf_get_addr(e, v, P_VADDR); 794 paddr = elf_get_addr(e, v, P_PADDR); 795 filesz = elf_get_size(e, v, P_FILESZ); 796 memsz = elf_get_size(e, v, P_MEMSZ); 797 flags = elf_get_word(e, v, P_FLAGS); 798 align = elf_get_size(e, v, P_ALIGN); 799 fprintf(out, "\n"); 800 fprintf(out, "entry: %d\n", i); 801 fprintf(out, "\tp_type: %s\n", p_types[type & 0x7]); 802 fprintf(out, "\tp_offset: %jd\n", (intmax_t)offset); 803 fprintf(out, "\tp_vaddr: %#jx\n", (intmax_t)vaddr); 804 fprintf(out, "\tp_paddr: %#jx\n", (intmax_t)paddr); 805 fprintf(out, "\tp_filesz: %jd\n", (intmax_t)filesz); 806 fprintf(out, "\tp_memsz: %jd\n", (intmax_t)memsz); 807 fprintf(out, "\tp_flags: %s\n", p_flags[flags]); 808 fprintf(out, "\tp_align: %jd\n", (intmax_t)align); 809 } 810 } 811 812 static void 813 elf_print_shdr(Elf32_Ehdr *e, void *sh) 814 { 815 u_int64_t shentsize; 816 u_int64_t shnum; 817 u_int64_t name; 818 u_int64_t type; 819 u_int64_t flags; 820 u_int64_t addr; 821 u_int64_t offset; 822 u_int64_t size; 823 u_int64_t shlink; 824 u_int64_t info; 825 u_int64_t addralign; 826 u_int64_t entsize; 827 u_int64_t machine; 828 void *v; 829 int i; 830 831 if (sh == NULL) { 832 fprintf(out, "\nNo section headers\n"); 833 return; 834 } 835 836 machine = elf_get_quarter(e, e, E_MACHINE); 837 shentsize = elf_get_quarter(e, e, E_SHENTSIZE); 838 shnum = elf_get_shnum(e, sh); 839 fprintf(out, "\nsection header:\n"); 840 for (i = 0; (u_int64_t)i < shnum; i++) { 841 v = (char *)sh + i * shentsize; 842 name = elf_get_word(e, v, SH_NAME); 843 type = elf_get_word(e, v, SH_TYPE); 844 flags = elf_get_word(e, v, SH_FLAGS); 845 addr = elf_get_addr(e, v, SH_ADDR); 846 offset = elf_get_off(e, v, SH_OFFSET); 847 size = elf_get_size(e, v, SH_SIZE); 848 shlink = elf_get_word(e, v, SH_LINK); 849 info = elf_get_word(e, v, SH_INFO); 850 addralign = elf_get_size(e, v, SH_ADDRALIGN); 851 entsize = elf_get_size(e, v, SH_ENTSIZE); 852 fprintf(out, "\n"); 853 fprintf(out, "entry: %d\n", i); 854 fprintf(out, "\tsh_name: %s\n", shstrtab + name); 855 fprintf(out, "\tsh_type: %s\n", sh_types(machine, type)); 856 fprintf(out, "\tsh_flags: %s\n", sh_flags[flags & 0x7]); 857 fprintf(out, "\tsh_addr: %#jx\n", addr); 858 fprintf(out, "\tsh_offset: %jd\n", (intmax_t)offset); 859 fprintf(out, "\tsh_size: %jd\n", (intmax_t)size); 860 fprintf(out, "\tsh_link: %jd\n", (intmax_t)shlink); 861 fprintf(out, "\tsh_info: %jd\n", (intmax_t)info); 862 fprintf(out, "\tsh_addralign: %jd\n", (intmax_t)addralign); 863 fprintf(out, "\tsh_entsize: %jd\n", (intmax_t)entsize); 864 } 865 } 866 867 static void 868 elf_print_symtab(Elf32_Ehdr *e, void *sh, char *str) 869 { 870 u_int64_t machine; 871 u_int64_t offset; 872 u_int64_t entsize; 873 u_int64_t size; 874 u_int64_t name; 875 u_int64_t value; 876 u_int64_t info; 877 u_int64_t shndx; 878 void *st; 879 int len; 880 int i; 881 882 machine = elf_get_quarter(e, e, E_MACHINE); 883 offset = elf_get_off(e, sh, SH_OFFSET); 884 entsize = elf_get_size(e, sh, SH_ENTSIZE); 885 size = elf_get_size(e, sh, SH_SIZE); 886 name = elf_get_word(e, sh, SH_NAME); 887 len = size / entsize; 888 fprintf(out, "\nsymbol table (%s):\n", shstrtab + name); 889 for (i = 0; i < len; i++) { 890 st = (char *)e + offset + i * entsize; 891 name = elf_get_word(e, st, ST_NAME); 892 value = elf_get_addr(e, st, ST_VALUE); 893 size = elf_get_size(e, st, ST_SIZE); 894 info = elf_get_byte(e, st, ST_INFO); 895 shndx = elf_get_quarter(e, st, ST_SHNDX); 896 fprintf(out, "\n"); 897 fprintf(out, "entry: %d\n", i); 898 fprintf(out, "\tst_name: %s\n", str + name); 899 fprintf(out, "\tst_value: %#jx\n", value); 900 fprintf(out, "\tst_size: %jd\n", (intmax_t)size); 901 fprintf(out, "\tst_info: %s %s\n", 902 st_type(machine, ELF32_ST_TYPE(info)), 903 st_bindings[ELF32_ST_BIND(info)]); 904 fprintf(out, "\tst_shndx: %jd\n", (intmax_t)shndx); 905 } 906 } 907 908 static void 909 elf_print_dynamic(Elf32_Ehdr *e, void *sh) 910 { 911 u_int64_t offset; 912 u_int64_t entsize; 913 u_int64_t size; 914 int64_t tag; 915 u_int64_t ptr; 916 u_int64_t val; 917 void *d; 918 int i; 919 920 offset = elf_get_off(e, sh, SH_OFFSET); 921 entsize = elf_get_size(e, sh, SH_ENTSIZE); 922 size = elf_get_size(e, sh, SH_SIZE); 923 fprintf(out, "\ndynamic:\n"); 924 for (i = 0; (u_int64_t)i < size / entsize; i++) { 925 d = (char *)e + offset + i * entsize; 926 tag = elf_get_size(e, d, D_TAG); 927 ptr = elf_get_size(e, d, D_PTR); 928 val = elf_get_addr(e, d, D_VAL); 929 fprintf(out, "\n"); 930 fprintf(out, "entry: %d\n", i); 931 fprintf(out, "\td_tag: %s\n", d_tags(tag)); 932 switch (tag) { 933 case DT_NEEDED: 934 case DT_SONAME: 935 case DT_RPATH: 936 fprintf(out, "\td_val: %s\n", dynstr + val); 937 break; 938 case DT_PLTRELSZ: 939 case DT_RELA: 940 case DT_RELASZ: 941 case DT_RELAENT: 942 case DT_STRSZ: 943 case DT_SYMENT: 944 case DT_RELSZ: 945 case DT_RELENT: 946 case DT_PLTREL: 947 fprintf(out, "\td_val: %jd\n", (intmax_t)val); 948 break; 949 case DT_PLTGOT: 950 case DT_HASH: 951 case DT_STRTAB: 952 case DT_SYMTAB: 953 case DT_INIT: 954 case DT_FINI: 955 case DT_REL: 956 case DT_JMPREL: 957 fprintf(out, "\td_ptr: %#jx\n", ptr); 958 break; 959 case DT_NULL: 960 case DT_SYMBOLIC: 961 case DT_DEBUG: 962 case DT_TEXTREL: 963 break; 964 } 965 } 966 } 967 968 static void 969 elf_print_rela(Elf32_Ehdr *e, void *sh) 970 { 971 u_int64_t offset; 972 u_int64_t entsize; 973 u_int64_t size; 974 u_int64_t name; 975 u_int64_t info; 976 int64_t addend; 977 void *ra; 978 void *v; 979 int i; 980 981 offset = elf_get_off(e, sh, SH_OFFSET); 982 entsize = elf_get_size(e, sh, SH_ENTSIZE); 983 size = elf_get_size(e, sh, SH_SIZE); 984 name = elf_get_word(e, sh, SH_NAME); 985 v = (char *)e + offset; 986 fprintf(out, "\nrelocation with addend (%s):\n", shstrtab + name); 987 for (i = 0; (u_int64_t)i < size / entsize; i++) { 988 ra = (char *)v + i * entsize; 989 offset = elf_get_addr(e, ra, RA_OFFSET); 990 info = elf_get_word(e, ra, RA_INFO); 991 addend = elf_get_off(e, ra, RA_ADDEND); 992 fprintf(out, "\n"); 993 fprintf(out, "entry: %d\n", i); 994 fprintf(out, "\tr_offset: %#jx\n", offset); 995 fprintf(out, "\tr_info: %jd\n", (intmax_t)info); 996 fprintf(out, "\tr_addend: %jd\n", (intmax_t)addend); 997 } 998 } 999 1000 static void 1001 elf_print_rel(Elf32_Ehdr *e, void *sh) 1002 { 1003 u_int64_t offset; 1004 u_int64_t entsize; 1005 u_int64_t size; 1006 u_int64_t name; 1007 u_int64_t info; 1008 void *r; 1009 void *v; 1010 int i; 1011 1012 offset = elf_get_off(e, sh, SH_OFFSET); 1013 entsize = elf_get_size(e, sh, SH_ENTSIZE); 1014 size = elf_get_size(e, sh, SH_SIZE); 1015 name = elf_get_word(e, sh, SH_NAME); 1016 v = (char *)e + offset; 1017 fprintf(out, "\nrelocation (%s):\n", shstrtab + name); 1018 for (i = 0; (u_int64_t)i < size / entsize; i++) { 1019 r = (char *)v + i * entsize; 1020 offset = elf_get_addr(e, r, R_OFFSET); 1021 info = elf_get_word(e, r, R_INFO); 1022 fprintf(out, "\n"); 1023 fprintf(out, "entry: %d\n", i); 1024 fprintf(out, "\tr_offset: %#jx\n", offset); 1025 fprintf(out, "\tr_info: %jd\n", (intmax_t)info); 1026 } 1027 } 1028 1029 static void 1030 elf_print_interp(Elf32_Ehdr *e, void *p) 1031 { 1032 u_int64_t offset; 1033 char *s; 1034 1035 offset = elf_get_off(e, p, P_OFFSET); 1036 s = (char *)e + offset; 1037 fprintf(out, "\ninterp:\n"); 1038 fprintf(out, "\t%s\n", s); 1039 } 1040 1041 static void 1042 elf_print_got(Elf32_Ehdr *e, void *sh) 1043 { 1044 u_int64_t offset; 1045 u_int64_t addralign; 1046 u_int64_t size; 1047 u_int64_t addr; 1048 void *v; 1049 int i; 1050 1051 offset = elf_get_off(e, sh, SH_OFFSET); 1052 addralign = elf_get_size(e, sh, SH_ADDRALIGN); 1053 size = elf_get_size(e, sh, SH_SIZE); 1054 v = (char *)e + offset; 1055 fprintf(out, "\nglobal offset table:\n"); 1056 for (i = 0; (u_int64_t)i < size / addralign; i++) { 1057 addr = elf_get_addr(e, (char *)v + i * addralign, 0); 1058 fprintf(out, "\n"); 1059 fprintf(out, "entry: %d\n", i); 1060 fprintf(out, "\t%#jx\n", addr); 1061 } 1062 } 1063 1064 static void 1065 elf_print_hash(Elf32_Ehdr *e __unused, void *sh __unused) 1066 { 1067 } 1068 1069 static void 1070 elf_print_note(Elf32_Ehdr *e, void *sh) 1071 { 1072 u_int64_t offset; 1073 u_int64_t size; 1074 u_int64_t name; 1075 u_int32_t namesz; 1076 u_int32_t descsz; 1077 u_int32_t desc; 1078 u_int32_t type; 1079 char *n, *s; 1080 const char *nt_type; 1081 1082 offset = elf_get_off(e, sh, SH_OFFSET); 1083 size = elf_get_size(e, sh, SH_SIZE); 1084 name = elf_get_word(e, sh, SH_NAME); 1085 n = (char *)e + offset; 1086 fprintf(out, "\nnote (%s):\n", shstrtab + name); 1087 while (n < ((char *)e + offset + size)) { 1088 namesz = elf_get_word(e, n, N_NAMESZ); 1089 descsz = elf_get_word(e, n, N_DESCSZ); 1090 type = elf_get_word(e, n, N_TYPE); 1091 if (type < nitems(nt_types) && nt_types[type] != NULL) 1092 nt_type = nt_types[type]; 1093 else 1094 nt_type = "Unknown type"; 1095 s = n + sizeof(Elf_Note); 1096 desc = elf_get_word(e, n + sizeof(Elf_Note) + namesz, 0); 1097 fprintf(out, "\t%s %d (%s)\n", s, desc, nt_type); 1098 n += sizeof(Elf_Note) + namesz + descsz; 1099 } 1100 } 1101 1102 static u_int64_t 1103 elf_get_byte(Elf32_Ehdr *e, void *base, elf_member_t member) 1104 { 1105 u_int64_t val; 1106 1107 val = 0; 1108 switch (e->e_ident[EI_CLASS]) { 1109 case ELFCLASS32: 1110 val = ((uint8_t *)base)[elf32_offsets[member]]; 1111 break; 1112 case ELFCLASS64: 1113 val = ((uint8_t *)base)[elf64_offsets[member]]; 1114 break; 1115 case ELFCLASSNONE: 1116 errx(1, "invalid class"); 1117 } 1118 1119 return val; 1120 } 1121 1122 static u_int64_t 1123 elf_get_quarter(Elf32_Ehdr *e, void *base, elf_member_t member) 1124 { 1125 u_int64_t val; 1126 1127 val = 0; 1128 switch (e->e_ident[EI_CLASS]) { 1129 case ELFCLASS32: 1130 base = (char *)base + elf32_offsets[member]; 1131 switch (e->e_ident[EI_DATA]) { 1132 case ELFDATA2MSB: 1133 val = be16dec(base); 1134 break; 1135 case ELFDATA2LSB: 1136 val = le16dec(base); 1137 break; 1138 case ELFDATANONE: 1139 errx(1, "invalid data format"); 1140 } 1141 break; 1142 case ELFCLASS64: 1143 base = (char *)base + elf64_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 ELFCLASSNONE: 1156 errx(1, "invalid class"); 1157 } 1158 1159 return val; 1160 } 1161 1162 #if 0 1163 static u_int64_t 1164 elf_get_half(Elf32_Ehdr *e, void *base, elf_member_t member) 1165 { 1166 u_int64_t val; 1167 1168 val = 0; 1169 switch (e->e_ident[EI_CLASS]) { 1170 case ELFCLASS32: 1171 base = (char *)base + elf32_offsets[member]; 1172 switch (e->e_ident[EI_DATA]) { 1173 case ELFDATA2MSB: 1174 val = be16dec(base); 1175 break; 1176 case ELFDATA2LSB: 1177 val = le16dec(base); 1178 break; 1179 case ELFDATANONE: 1180 errx(1, "invalid data format"); 1181 } 1182 break; 1183 case ELFCLASS64: 1184 base = (char *)base + elf64_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 ELFCLASSNONE: 1197 errx(1, "invalid class"); 1198 } 1199 1200 return val; 1201 } 1202 #endif 1203 1204 static u_int64_t 1205 elf_get_word(Elf32_Ehdr *e, void *base, elf_member_t member) 1206 { 1207 u_int64_t val; 1208 1209 val = 0; 1210 switch (e->e_ident[EI_CLASS]) { 1211 case ELFCLASS32: 1212 base = (char *)base + elf32_offsets[member]; 1213 switch (e->e_ident[EI_DATA]) { 1214 case ELFDATA2MSB: 1215 val = be32dec(base); 1216 break; 1217 case ELFDATA2LSB: 1218 val = le32dec(base); 1219 break; 1220 case ELFDATANONE: 1221 errx(1, "invalid data format"); 1222 } 1223 break; 1224 case ELFCLASS64: 1225 base = (char *)base + elf64_offsets[member]; 1226 switch (e->e_ident[EI_DATA]) { 1227 case ELFDATA2MSB: 1228 val = be32dec(base); 1229 break; 1230 case ELFDATA2LSB: 1231 val = le32dec(base); 1232 break; 1233 case ELFDATANONE: 1234 errx(1, "invalid data format"); 1235 } 1236 break; 1237 case ELFCLASSNONE: 1238 errx(1, "invalid class"); 1239 } 1240 1241 return val; 1242 } 1243 1244 static u_int64_t 1245 elf_get_quad(Elf32_Ehdr *e, void *base, elf_member_t member) 1246 { 1247 u_int64_t val; 1248 1249 val = 0; 1250 switch (e->e_ident[EI_CLASS]) { 1251 case ELFCLASS32: 1252 base = (char *)base + elf32_offsets[member]; 1253 switch (e->e_ident[EI_DATA]) { 1254 case ELFDATA2MSB: 1255 val = be32dec(base); 1256 break; 1257 case ELFDATA2LSB: 1258 val = le32dec(base); 1259 break; 1260 case ELFDATANONE: 1261 errx(1, "invalid data format"); 1262 } 1263 break; 1264 case ELFCLASS64: 1265 base = (char *)base + elf64_offsets[member]; 1266 switch (e->e_ident[EI_DATA]) { 1267 case ELFDATA2MSB: 1268 val = be64dec(base); 1269 break; 1270 case ELFDATA2LSB: 1271 val = le64dec(base); 1272 break; 1273 case ELFDATANONE: 1274 errx(1, "invalid data format"); 1275 } 1276 break; 1277 case ELFCLASSNONE: 1278 errx(1, "invalid class"); 1279 } 1280 1281 return val; 1282 } 1283 1284 static void 1285 usage(void) 1286 { 1287 fprintf(stderr, 1288 "usage: elfdump -a | -E | -cdeGhinprs [-w file] file\n"); 1289 exit(1); 1290 } 1291