2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2003 David O'Brien. All rights reserved.
5 * Copyright (c) 2001 Jake Burkholder
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/types.h>
35 #include <sys/capsicum.h>
36 #include <sys/elf32.h>
37 #include <sys/elf64.h>
38 #include <sys/endian.h>
41 #include <capsicum_helpers.h>
53 #define ED_EHDR (1<<1)
55 #define ED_HASH (1<<3)
56 #define ED_INTERP (1<<4)
57 #define ED_NOTE (1<<5)
58 #define ED_PHDR (1<<6)
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 */
65 #define elf_get_addr elf_get_quad
66 #define elf_get_off elf_get_quad
67 #define elf_get_size elf_get_quad
70 D_TAG = 1, D_PTR, D_VAL,
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,
76 N_NAMESZ, N_DESCSZ, N_TYPE,
78 P_TYPE, P_OFFSET, P_VADDR, P_PADDR, P_FILESZ, P_MEMSZ, P_FLAGS,
81 SH_NAME, SH_TYPE, SH_FLAGS, SH_ADDR, SH_OFFSET, SH_SIZE, SH_LINK,
82 SH_INFO, SH_ADDRALIGN, SH_ENTSIZE,
84 ST_NAME, ST_VALUE, ST_SIZE, ST_INFO, ST_SHNDX,
88 RA_OFFSET, RA_INFO, RA_ADDEND
91 typedef enum elf_member elf_member_t;
93 static int elf32_offsets[] = {
96 offsetof(Elf32_Dyn, d_tag), offsetof(Elf32_Dyn, d_un.d_ptr),
97 offsetof(Elf32_Dyn, d_un.d_val),
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),
110 offsetof(Elf_Note, n_namesz), offsetof(Elf_Note, n_descsz),
111 offsetof(Elf_Note, n_type),
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),
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),
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),
128 offsetof(Elf32_Rel, r_offset), offsetof(Elf32_Rel, r_info),
130 offsetof(Elf32_Rela, r_offset), offsetof(Elf32_Rela, r_info),
131 offsetof(Elf32_Rela, r_addend)
134 static int elf64_offsets[] = {
137 offsetof(Elf64_Dyn, d_tag), offsetof(Elf64_Dyn, d_un.d_ptr),
138 offsetof(Elf64_Dyn, d_un.d_val),
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),
151 offsetof(Elf_Note, n_namesz), offsetof(Elf_Note, n_descsz),
152 offsetof(Elf_Note, n_type),
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),
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),
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),
169 offsetof(Elf64_Rel, r_offset), offsetof(Elf64_Rel, r_info),
171 offsetof(Elf64_Rela, r_offset), offsetof(Elf64_Rela, r_info),
172 offsetof(Elf64_Rela, r_addend)
175 /* http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#tag_encodings */
177 d_tags(u_int64_t tag)
179 static char unknown_tag[48];
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";
251 snprintf(unknown_tag, sizeof(unknown_tag),
252 "ERROR: TAG NOT DEFINED -- tag 0x%jx", (uintmax_t)tag);
253 return (unknown_tag);
257 e_machines(u_int mach)
259 static char machdesc[64];
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";
281 snprintf(machdesc, sizeof(machdesc),
282 "(unknown machine) -- type 0x%x", mach);
286 static const char *e_types[] = {
287 "ET_NONE", "ET_REL", "ET_EXEC", "ET_DYN", "ET_CORE"
290 static const char *ei_versions[] = {
291 "EV_NONE", "EV_CURRENT"
294 static const char *ei_classes[] = {
295 "ELFCLASSNONE", "ELFCLASS32", "ELFCLASS64"
298 static const char *ei_data[] = {
299 "ELFDATANONE", "ELFDATA2LSB", "ELFDATA2MSB"
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"
310 static const char *p_types[] = {
311 "PT_NULL", "PT_LOAD", "PT_DYNAMIC", "PT_INTERP", "PT_NOTE",
312 "PT_SHLIB", "PT_PHDR", "PT_TLS"
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",
320 #define NT_ELEM(x) [x] = #x,
321 static const char *nt_types[] = {
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)
329 /* http://www.sco.com/developers/gabi/latest/ch4.sheader.html#sh_type */
331 sh_types(uint64_t machine, uint64_t sht) {
332 static char unknown_buf[64];
334 if (sht < 0x60000000) {
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";
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 */
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";
369 snprintf(unknown_buf, sizeof(unknown_buf),
370 "ERROR: OS-SPECIFIC SHT 0x%jx NOT DEFINED",
372 return (unknown_buf);
373 } else if (sht < 0x80000000) {
374 /* 0x70000000-0x7fffffff processor-specific semantics */
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";
389 case 0x70000000: return "SHT_IA_64_EXT";
390 case 0x70000001: return "SHT_IA_64_UNWIND";
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";
402 case 0x7ffffffd: return "XXX:AUXILIARY";
403 case 0x7fffffff: return "XXX:FILTER";
405 snprintf(unknown_buf, sizeof(unknown_buf),
406 "ERROR: PROCESSOR-SPECIFIC SHT 0x%jx NOT DEFINED",
408 return (unknown_buf);
410 /* 0x80000000-0xffffffff application programs */
411 snprintf(unknown_buf, sizeof(unknown_buf),
412 "ERROR: SHT 0x%jx NOT DEFINED",
414 return (unknown_buf);
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"
425 st_type(unsigned int mach, unsigned int type)
427 static char s_type[32];
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";
438 if (mach == EM_SPARCV9)
439 return "STT_SPARC_REGISTER";
442 snprintf(s_type, sizeof(s_type), "<unknown: %#x>", type);
446 static const char *st_bindings[] = {
447 "STB_LOCAL", "STB_GLOBAL", "STB_WEAK"
451 static char *shstrtab;
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);
459 static u_int64_t elf_get_half(Elf32_Ehdr *e, void *base, elf_member_t member);
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);
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);
476 static void usage(void) __dead2;
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.
484 elf_get_shnum(Elf32_Ehdr *e, void *sh)
488 shnum = elf_get_quarter(e, e, E_SHNUM);
490 shnum = elf_get_word(e, (char *)sh, SH_SIZE);
495 elf_get_shstrndx(Elf32_Ehdr *e, void *sh)
499 shstrndx = elf_get_quarter(e, e, E_SHSTRNDX);
500 if (shstrndx == SHN_XINDEX)
501 shstrndx = elf_get_word(e, (char *)sh, SH_LINK);
506 main(int ac, char **av)
531 while ((ch = getopt(ac, av, "acdEeiGhnprsw:")) != -1)
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);
582 if (ac == 0 || flags == 0 || ((flags & ED_IS_ELF) &&
583 (ac != 1 || (flags & ~ED_IS_ELF) || out != stdout)))
585 if ((fd = open(*av, O_RDONLY)) < 0 ||
588 if ((size_t)sb.st_size < sizeof(Elf32_Ehdr)) {
589 if (flags & ED_IS_ELF)
591 errx(1, "not an elf file");
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");
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);
607 if (flags & ED_IS_ELF)
609 errx(1, "not an elf file");
610 } else if (flags & ED_IS_ELF)
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;
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,
624 shstrtab = (char *)e + offset;
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;
640 elf_print_ehdr(e, sh);
642 elf_print_phdr(e, p);
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);
650 if (flags & ED_INTERP)
651 elf_print_interp(e, v);
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);
667 if (flags & ED_SYMTAB)
668 elf_print_symtab(e, v, strtab);
672 elf_print_dynamic(e, v);
676 elf_print_rela(e, v);
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);
689 if (flags & ED_SYMTAB)
690 elf_print_symtab(e, v, dynstr);
693 name = elf_get_word(e, v, SH_NAME);
694 if (flags & ED_GOT &&
695 strcmp(shstrtab + name, ".got") == 0)
700 elf_print_hash(e, v);
714 elf_print_ehdr(Elf32_Ehdr *e, void *sh)
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");
749 fprintf(out, "\te_ident: %s %s %s\n", ei_classes[class], ei_data[data],
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);
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);
771 elf_print_phdr(Elf32_Ehdr *e, void *p)
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);
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);
813 elf_print_shdr(Elf32_Ehdr *e, void *sh)
832 fprintf(out, "\nNo section headers\n");
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);
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);
868 elf_print_symtab(Elf32_Ehdr *e, void *sh, char *str)
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);
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);
909 elf_print_dynamic(Elf32_Ehdr *e, void *sh)
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);
930 fprintf(out, "entry: %d\n", i);
931 fprintf(out, "\td_tag: %s\n", d_tags(tag));
936 fprintf(out, "\td_val: %s\n", dynstr + val);
947 fprintf(out, "\td_val: %jd\n", (intmax_t)val);
957 fprintf(out, "\td_ptr: %#jx\n", ptr);
969 elf_print_rela(Elf32_Ehdr *e, void *sh)
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);
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);
1001 elf_print_rel(Elf32_Ehdr *e, void *sh)
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);
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);
1030 elf_print_interp(Elf32_Ehdr *e, void *p)
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);
1042 elf_print_got(Elf32_Ehdr *e, void *sh)
1045 u_int64_t addralign;
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);
1059 fprintf(out, "entry: %d\n", i);
1060 fprintf(out, "\t%#jx\n", addr);
1065 elf_print_hash(Elf32_Ehdr *e __unused, void *sh __unused)
1070 elf_print_note(Elf32_Ehdr *e, void *sh)
1080 const char *nt_type;
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];
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;
1103 elf_get_byte(Elf32_Ehdr *e, void *base, elf_member_t member)
1108 switch (e->e_ident[EI_CLASS]) {
1110 val = ((uint8_t *)base)[elf32_offsets[member]];
1113 val = ((uint8_t *)base)[elf64_offsets[member]];
1116 errx(1, "invalid class");
1123 elf_get_quarter(Elf32_Ehdr *e, void *base, elf_member_t member)
1128 switch (e->e_ident[EI_CLASS]) {
1130 base = (char *)base + elf32_offsets[member];
1131 switch (e->e_ident[EI_DATA]) {
1133 val = be16dec(base);
1136 val = le16dec(base);
1139 errx(1, "invalid data format");
1143 base = (char *)base + elf64_offsets[member];
1144 switch (e->e_ident[EI_DATA]) {
1146 val = be16dec(base);
1149 val = le16dec(base);
1152 errx(1, "invalid data format");
1156 errx(1, "invalid class");
1164 elf_get_half(Elf32_Ehdr *e, void *base, elf_member_t member)
1169 switch (e->e_ident[EI_CLASS]) {
1171 base = (char *)base + elf32_offsets[member];
1172 switch (e->e_ident[EI_DATA]) {
1174 val = be16dec(base);
1177 val = le16dec(base);
1180 errx(1, "invalid data format");
1184 base = (char *)base + elf64_offsets[member];
1185 switch (e->e_ident[EI_DATA]) {
1187 val = be32dec(base);
1190 val = le32dec(base);
1193 errx(1, "invalid data format");
1197 errx(1, "invalid class");
1205 elf_get_word(Elf32_Ehdr *e, void *base, elf_member_t member)
1210 switch (e->e_ident[EI_CLASS]) {
1212 base = (char *)base + elf32_offsets[member];
1213 switch (e->e_ident[EI_DATA]) {
1215 val = be32dec(base);
1218 val = le32dec(base);
1221 errx(1, "invalid data format");
1225 base = (char *)base + elf64_offsets[member];
1226 switch (e->e_ident[EI_DATA]) {
1228 val = be32dec(base);
1231 val = le32dec(base);
1234 errx(1, "invalid data format");
1238 errx(1, "invalid class");
1245 elf_get_quad(Elf32_Ehdr *e, void *base, elf_member_t member)
1250 switch (e->e_ident[EI_CLASS]) {
1252 base = (char *)base + elf32_offsets[member];
1253 switch (e->e_ident[EI_DATA]) {
1255 val = be32dec(base);
1258 val = le32dec(base);
1261 errx(1, "invalid data format");
1265 base = (char *)base + elf64_offsets[member];
1266 switch (e->e_ident[EI_DATA]) {
1268 val = be64dec(base);
1271 val = le64dec(base);
1274 errx(1, "invalid data format");
1278 errx(1, "invalid class");
1288 "usage: elfdump -a | -E | -cdeGhinprs [-w file] file\n");