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