xref: /freebsd/usr.bin/elfdump/elfdump.c (revision c6989859ae9388eeb46a24fe88f9b8d07101c710)
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 	cap_rights_init(&rights, CAP_MMAP_R);
589 	if (caph_rights_limit(fd, &rights) < 0)
590 		err(1, "unable to limit rights for %s", *av);
591 	cap_rights_init(&rights);
592 	if (caph_rights_limit(STDIN_FILENO, &rights) < 0 ||
593 	    caph_limit_stdout() < 0 || caph_limit_stderr() < 0) {
594                 err(1, "unable to limit rights for stdio");
595 	}
596 	if (caph_enter() < 0)
597 		err(1, "unable to enter capability mode");
598 	e = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
599 	if (e == MAP_FAILED)
600 		err(1, NULL);
601 	if (!IS_ELF(*(Elf32_Ehdr *)e)) {
602 		if (flags & ED_IS_ELF)
603 			exit(1);
604 		errx(1, "not an elf file");
605 	} else if (flags & ED_IS_ELF)
606 		exit (0);
607 	phoff = elf_get_off(e, e, E_PHOFF);
608 	shoff = elf_get_off(e, e, E_SHOFF);
609 	phentsize = elf_get_quarter(e, e, E_PHENTSIZE);
610 	phnum = elf_get_quarter(e, e, E_PHNUM);
611 	shentsize = elf_get_quarter(e, e, E_SHENTSIZE);
612 	p = (char *)e + phoff;
613 	if (shoff > 0) {
614 		sh = (char *)e + shoff;
615 		shnum = elf_get_shnum(e, sh);
616 		shstrndx = elf_get_shstrndx(e, sh);
617 		offset = elf_get_off(e, (char *)sh + shstrndx * shentsize,
618 		    SH_OFFSET);
619 		shstrtab = (char *)e + offset;
620 	} else {
621 		sh = NULL;
622 		shnum = 0;
623 		shstrndx = 0;
624 		shstrtab = NULL;
625 	}
626 	for (i = 0; (u_int64_t)i < shnum; i++) {
627 		name = elf_get_word(e, (char *)sh + i * shentsize, SH_NAME);
628 		offset = elf_get_off(e, (char *)sh + i * shentsize, SH_OFFSET);
629 		if (strcmp(shstrtab + name, ".strtab") == 0)
630 			strtab = (char *)e + offset;
631 		if (strcmp(shstrtab + name, ".dynstr") == 0)
632 			dynstr = (char *)e + offset;
633 	}
634 	if (flags & ED_EHDR)
635 		elf_print_ehdr(e, sh);
636 	if (flags & ED_PHDR)
637 		elf_print_phdr(e, p);
638 	if (flags & ED_SHDR)
639 		elf_print_shdr(e, sh);
640 	for (i = 0; (u_int64_t)i < phnum; i++) {
641 		v = (char *)p + i * phentsize;
642 		type = elf_get_word(e, v, P_TYPE);
643 		switch (type) {
644 		case PT_INTERP:
645 			if (flags & ED_INTERP)
646 				elf_print_interp(e, v);
647 			break;
648 		case PT_NULL:
649 		case PT_LOAD:
650 		case PT_DYNAMIC:
651 		case PT_NOTE:
652 		case PT_SHLIB:
653 		case PT_PHDR:
654 			break;
655 		}
656 	}
657 	for (i = 0; (u_int64_t)i < shnum; i++) {
658 		v = (char *)sh + i * shentsize;
659 		type = elf_get_word(e, v, SH_TYPE);
660 		switch (type) {
661 		case SHT_SYMTAB:
662 			if (flags & ED_SYMTAB)
663 				elf_print_symtab(e, v, strtab);
664 			break;
665 		case SHT_DYNAMIC:
666 			if (flags & ED_DYN)
667 				elf_print_dynamic(e, v);
668 			break;
669 		case SHT_RELA:
670 			if (flags & ED_REL)
671 				elf_print_rela(e, v);
672 			break;
673 		case SHT_REL:
674 			if (flags & ED_REL)
675 				elf_print_rel(e, v);
676 			break;
677 		case SHT_NOTE:
678 			name = elf_get_word(e, v, SH_NAME);
679 			if (flags & ED_NOTE &&
680 			    strcmp(shstrtab + name, ".note.tag") == 0)
681 				elf_print_note(e, v);
682 			break;
683 		case SHT_DYNSYM:
684 			if (flags & ED_SYMTAB)
685 				elf_print_symtab(e, v, dynstr);
686 			break;
687 		case SHT_PROGBITS:
688 			name = elf_get_word(e, v, SH_NAME);
689 			if (flags & ED_GOT &&
690 			    strcmp(shstrtab + name, ".got") == 0)
691 				elf_print_got(e, v);
692 			break;
693 		case SHT_HASH:
694 			if (flags & ED_HASH)
695 				elf_print_hash(e, v);
696 			break;
697 		case SHT_NULL:
698 		case SHT_STRTAB:
699 		case SHT_NOBITS:
700 		case SHT_SHLIB:
701 			break;
702 		}
703 	}
704 
705 	return 0;
706 }
707 
708 static void
709 elf_print_ehdr(Elf32_Ehdr *e, void *sh)
710 {
711 	u_int64_t class;
712 	u_int64_t data;
713 	u_int64_t osabi;
714 	u_int64_t type;
715 	u_int64_t machine;
716 	u_int64_t version;
717 	u_int64_t entry;
718 	u_int64_t phoff;
719 	u_int64_t shoff;
720 	u_int64_t flags;
721 	u_int64_t ehsize;
722 	u_int64_t phentsize;
723 	u_int64_t phnum;
724 	u_int64_t shentsize;
725 	u_int64_t shnum;
726 	u_int64_t shstrndx;
727 
728 	class = elf_get_byte(e, e, E_CLASS);
729 	data = elf_get_byte(e, e, E_DATA);
730 	osabi = elf_get_byte(e, e, E_OSABI);
731 	type = elf_get_quarter(e, e, E_TYPE);
732 	machine = elf_get_quarter(e, e, E_MACHINE);
733 	version = elf_get_word(e, e, E_VERSION);
734 	entry = elf_get_addr(e, e, E_ENTRY);
735 	phoff = elf_get_off(e, e, E_PHOFF);
736 	shoff = elf_get_off(e, e, E_SHOFF);
737 	flags = elf_get_word(e, e, E_FLAGS);
738 	ehsize = elf_get_quarter(e, e, E_EHSIZE);
739 	phentsize = elf_get_quarter(e, e, E_PHENTSIZE);
740 	phnum = elf_get_quarter(e, e, E_PHNUM);
741 	shentsize = elf_get_quarter(e, e, E_SHENTSIZE);
742 	fprintf(out, "\nelf header:\n");
743 	fprintf(out, "\n");
744 	fprintf(out, "\te_ident: %s %s %s\n", ei_classes[class], ei_data[data],
745 	    ei_abis[osabi]);
746 	fprintf(out, "\te_type: %s\n", e_types[type]);
747 	fprintf(out, "\te_machine: %s\n", e_machines(machine));
748 	fprintf(out, "\te_version: %s\n", ei_versions[version]);
749 	fprintf(out, "\te_entry: %#jx\n", (intmax_t)entry);
750 	fprintf(out, "\te_phoff: %jd\n", (intmax_t)phoff);
751 	fprintf(out, "\te_shoff: %jd\n", (intmax_t)shoff);
752 	fprintf(out, "\te_flags: %jd\n", (intmax_t)flags);
753 	fprintf(out, "\te_ehsize: %jd\n", (intmax_t)ehsize);
754 	fprintf(out, "\te_phentsize: %jd\n", (intmax_t)phentsize);
755 	fprintf(out, "\te_phnum: %jd\n", (intmax_t)phnum);
756 	fprintf(out, "\te_shentsize: %jd\n", (intmax_t)shentsize);
757 	if (sh != NULL) {
758 		shnum = elf_get_shnum(e, sh);
759 		shstrndx = elf_get_shstrndx(e, sh);
760 		fprintf(out, "\te_shnum: %jd\n", (intmax_t)shnum);
761 		fprintf(out, "\te_shstrndx: %jd\n", (intmax_t)shstrndx);
762 	}
763 }
764 
765 static void
766 elf_print_phdr(Elf32_Ehdr *e, void *p)
767 {
768 	u_int64_t phentsize;
769 	u_int64_t phnum;
770 	u_int64_t type;
771 	u_int64_t offset;
772 	u_int64_t vaddr;
773 	u_int64_t paddr;
774 	u_int64_t filesz;
775 	u_int64_t memsz;
776 	u_int64_t flags;
777 	u_int64_t align;
778 	void *v;
779 	int i;
780 
781 	phentsize = elf_get_quarter(e, e, E_PHENTSIZE);
782 	phnum = elf_get_quarter(e, e, E_PHNUM);
783 	fprintf(out, "\nprogram header:\n");
784 	for (i = 0; (u_int64_t)i < phnum; i++) {
785 		v = (char *)p + i * phentsize;
786 		type = elf_get_word(e, v, P_TYPE);
787 		offset = elf_get_off(e, v, P_OFFSET);
788 		vaddr = elf_get_addr(e, v, P_VADDR);
789 		paddr = elf_get_addr(e, v, P_PADDR);
790 		filesz = elf_get_size(e, v, P_FILESZ);
791 		memsz = elf_get_size(e, v, P_MEMSZ);
792 		flags = elf_get_word(e, v, P_FLAGS);
793 		align = elf_get_size(e, v, P_ALIGN);
794 		fprintf(out, "\n");
795 		fprintf(out, "entry: %d\n", i);
796 		fprintf(out, "\tp_type: %s\n", p_types[type & 0x7]);
797 		fprintf(out, "\tp_offset: %jd\n", (intmax_t)offset);
798 		fprintf(out, "\tp_vaddr: %#jx\n", (intmax_t)vaddr);
799 		fprintf(out, "\tp_paddr: %#jx\n", (intmax_t)paddr);
800 		fprintf(out, "\tp_filesz: %jd\n", (intmax_t)filesz);
801 		fprintf(out, "\tp_memsz: %jd\n", (intmax_t)memsz);
802 		fprintf(out, "\tp_flags: %s\n", p_flags[flags]);
803 		fprintf(out, "\tp_align: %jd\n", (intmax_t)align);
804 	}
805 }
806 
807 static void
808 elf_print_shdr(Elf32_Ehdr *e, void *sh)
809 {
810 	u_int64_t shentsize;
811 	u_int64_t shnum;
812 	u_int64_t name;
813 	u_int64_t type;
814 	u_int64_t flags;
815 	u_int64_t addr;
816 	u_int64_t offset;
817 	u_int64_t size;
818 	u_int64_t shlink;
819 	u_int64_t info;
820 	u_int64_t addralign;
821 	u_int64_t entsize;
822 	u_int64_t machine;
823 	void *v;
824 	int i;
825 
826 	if (sh == NULL) {
827 		fprintf(out, "\nNo section headers\n");
828 		return;
829 	}
830 
831 	machine = elf_get_quarter(e, e, E_MACHINE);
832 	shentsize = elf_get_quarter(e, e, E_SHENTSIZE);
833 	shnum = elf_get_shnum(e, sh);
834 	fprintf(out, "\nsection header:\n");
835 	for (i = 0; (u_int64_t)i < shnum; i++) {
836 		v = (char *)sh + i * shentsize;
837 		name = elf_get_word(e, v, SH_NAME);
838 		type = elf_get_word(e, v, SH_TYPE);
839 		flags = elf_get_word(e, v, SH_FLAGS);
840 		addr = elf_get_addr(e, v, SH_ADDR);
841 		offset = elf_get_off(e, v, SH_OFFSET);
842 		size = elf_get_size(e, v, SH_SIZE);
843 		shlink = elf_get_word(e, v, SH_LINK);
844 		info = elf_get_word(e, v, SH_INFO);
845 		addralign = elf_get_size(e, v, SH_ADDRALIGN);
846 		entsize = elf_get_size(e, v, SH_ENTSIZE);
847 		fprintf(out, "\n");
848 		fprintf(out, "entry: %d\n", i);
849 		fprintf(out, "\tsh_name: %s\n", shstrtab + name);
850 		fprintf(out, "\tsh_type: %s\n", sh_types(machine, type));
851 		fprintf(out, "\tsh_flags: %s\n", sh_flags[flags & 0x7]);
852 		fprintf(out, "\tsh_addr: %#jx\n", addr);
853 		fprintf(out, "\tsh_offset: %jd\n", (intmax_t)offset);
854 		fprintf(out, "\tsh_size: %jd\n", (intmax_t)size);
855 		fprintf(out, "\tsh_link: %jd\n", (intmax_t)shlink);
856 		fprintf(out, "\tsh_info: %jd\n", (intmax_t)info);
857 		fprintf(out, "\tsh_addralign: %jd\n", (intmax_t)addralign);
858 		fprintf(out, "\tsh_entsize: %jd\n", (intmax_t)entsize);
859 	}
860 }
861 
862 static void
863 elf_print_symtab(Elf32_Ehdr *e, void *sh, char *str)
864 {
865 	u_int64_t machine;
866 	u_int64_t offset;
867 	u_int64_t entsize;
868 	u_int64_t size;
869 	u_int64_t name;
870 	u_int64_t value;
871 	u_int64_t info;
872 	u_int64_t shndx;
873 	void *st;
874 	int len;
875 	int i;
876 
877 	machine = elf_get_quarter(e, e, E_MACHINE);
878 	offset = elf_get_off(e, sh, SH_OFFSET);
879 	entsize = elf_get_size(e, sh, SH_ENTSIZE);
880 	size = elf_get_size(e, sh, SH_SIZE);
881 	name = elf_get_word(e, sh, SH_NAME);
882 	len = size / entsize;
883 	fprintf(out, "\nsymbol table (%s):\n", shstrtab + name);
884 	for (i = 0; i < len; i++) {
885 		st = (char *)e + offset + i * entsize;
886 		name = elf_get_word(e, st, ST_NAME);
887 		value = elf_get_addr(e, st, ST_VALUE);
888 		size = elf_get_size(e, st, ST_SIZE);
889 		info = elf_get_byte(e, st, ST_INFO);
890 		shndx = elf_get_quarter(e, st, ST_SHNDX);
891 		fprintf(out, "\n");
892 		fprintf(out, "entry: %d\n", i);
893 		fprintf(out, "\tst_name: %s\n", str + name);
894 		fprintf(out, "\tst_value: %#jx\n", value);
895 		fprintf(out, "\tst_size: %jd\n", (intmax_t)size);
896 		fprintf(out, "\tst_info: %s %s\n",
897 		    st_type(machine, ELF32_ST_TYPE(info)),
898 		    st_bindings[ELF32_ST_BIND(info)]);
899 		fprintf(out, "\tst_shndx: %jd\n", (intmax_t)shndx);
900 	}
901 }
902 
903 static void
904 elf_print_dynamic(Elf32_Ehdr *e, void *sh)
905 {
906 	u_int64_t offset;
907 	u_int64_t entsize;
908 	u_int64_t size;
909 	int64_t tag;
910 	u_int64_t ptr;
911 	u_int64_t val;
912 	void *d;
913 	int i;
914 
915 	offset = elf_get_off(e, sh, SH_OFFSET);
916 	entsize = elf_get_size(e, sh, SH_ENTSIZE);
917 	size = elf_get_size(e, sh, SH_SIZE);
918 	fprintf(out, "\ndynamic:\n");
919 	for (i = 0; (u_int64_t)i < size / entsize; i++) {
920 		d = (char *)e + offset + i * entsize;
921 		tag = elf_get_size(e, d, D_TAG);
922 		ptr = elf_get_size(e, d, D_PTR);
923 		val = elf_get_addr(e, d, D_VAL);
924 		fprintf(out, "\n");
925 		fprintf(out, "entry: %d\n", i);
926 		fprintf(out, "\td_tag: %s\n", d_tags(tag));
927 		switch (tag) {
928 		case DT_NEEDED:
929 		case DT_SONAME:
930 		case DT_RPATH:
931 			fprintf(out, "\td_val: %s\n", dynstr + val);
932 			break;
933 		case DT_PLTRELSZ:
934 		case DT_RELA:
935 		case DT_RELASZ:
936 		case DT_RELAENT:
937 		case DT_STRSZ:
938 		case DT_SYMENT:
939 		case DT_RELSZ:
940 		case DT_RELENT:
941 		case DT_PLTREL:
942 			fprintf(out, "\td_val: %jd\n", (intmax_t)val);
943 			break;
944 		case DT_PLTGOT:
945 		case DT_HASH:
946 		case DT_STRTAB:
947 		case DT_SYMTAB:
948 		case DT_INIT:
949 		case DT_FINI:
950 		case DT_REL:
951 		case DT_JMPREL:
952 			fprintf(out, "\td_ptr: %#jx\n", ptr);
953 			break;
954 		case DT_NULL:
955 		case DT_SYMBOLIC:
956 		case DT_DEBUG:
957 		case DT_TEXTREL:
958 			break;
959 		}
960 	}
961 }
962 
963 static void
964 elf_print_rela(Elf32_Ehdr *e, void *sh)
965 {
966 	u_int64_t offset;
967 	u_int64_t entsize;
968 	u_int64_t size;
969 	u_int64_t name;
970 	u_int64_t info;
971 	int64_t addend;
972 	void *ra;
973 	void *v;
974 	int i;
975 
976 	offset = elf_get_off(e, sh, SH_OFFSET);
977 	entsize = elf_get_size(e, sh, SH_ENTSIZE);
978 	size = elf_get_size(e, sh, SH_SIZE);
979 	name = elf_get_word(e, sh, SH_NAME);
980 	v = (char *)e + offset;
981 	fprintf(out, "\nrelocation with addend (%s):\n", shstrtab + name);
982 	for (i = 0; (u_int64_t)i < size / entsize; i++) {
983 		ra = (char *)v + i * entsize;
984 		offset = elf_get_addr(e, ra, RA_OFFSET);
985 		info = elf_get_word(e, ra, RA_INFO);
986 		addend = elf_get_off(e, ra, RA_ADDEND);
987 		fprintf(out, "\n");
988 		fprintf(out, "entry: %d\n", i);
989 		fprintf(out, "\tr_offset: %#jx\n", offset);
990 		fprintf(out, "\tr_info: %jd\n", (intmax_t)info);
991 		fprintf(out, "\tr_addend: %jd\n", (intmax_t)addend);
992 	}
993 }
994 
995 static void
996 elf_print_rel(Elf32_Ehdr *e, void *sh)
997 {
998 	u_int64_t offset;
999 	u_int64_t entsize;
1000 	u_int64_t size;
1001 	u_int64_t name;
1002 	u_int64_t info;
1003 	void *r;
1004 	void *v;
1005 	int i;
1006 
1007 	offset = elf_get_off(e, sh, SH_OFFSET);
1008 	entsize = elf_get_size(e, sh, SH_ENTSIZE);
1009 	size = elf_get_size(e, sh, SH_SIZE);
1010 	name = elf_get_word(e, sh, SH_NAME);
1011 	v = (char *)e + offset;
1012 	fprintf(out, "\nrelocation (%s):\n", shstrtab + name);
1013 	for (i = 0; (u_int64_t)i < size / entsize; i++) {
1014 		r = (char *)v + i * entsize;
1015 		offset = elf_get_addr(e, r, R_OFFSET);
1016 		info = elf_get_word(e, r, R_INFO);
1017 		fprintf(out, "\n");
1018 		fprintf(out, "entry: %d\n", i);
1019 		fprintf(out, "\tr_offset: %#jx\n", offset);
1020 		fprintf(out, "\tr_info: %jd\n", (intmax_t)info);
1021 	}
1022 }
1023 
1024 static void
1025 elf_print_interp(Elf32_Ehdr *e, void *p)
1026 {
1027 	u_int64_t offset;
1028 	char *s;
1029 
1030 	offset = elf_get_off(e, p, P_OFFSET);
1031 	s = (char *)e + offset;
1032 	fprintf(out, "\ninterp:\n");
1033 	fprintf(out, "\t%s\n", s);
1034 }
1035 
1036 static void
1037 elf_print_got(Elf32_Ehdr *e, void *sh)
1038 {
1039 	u_int64_t offset;
1040 	u_int64_t addralign;
1041 	u_int64_t size;
1042 	u_int64_t addr;
1043 	void *v;
1044 	int i;
1045 
1046 	offset = elf_get_off(e, sh, SH_OFFSET);
1047 	addralign = elf_get_size(e, sh, SH_ADDRALIGN);
1048 	size = elf_get_size(e, sh, SH_SIZE);
1049 	v = (char *)e + offset;
1050 	fprintf(out, "\nglobal offset table:\n");
1051 	for (i = 0; (u_int64_t)i < size / addralign; i++) {
1052 		addr = elf_get_addr(e, (char *)v + i * addralign, 0);
1053 		fprintf(out, "\n");
1054 		fprintf(out, "entry: %d\n", i);
1055 		fprintf(out, "\t%#jx\n", addr);
1056 	}
1057 }
1058 
1059 static void
1060 elf_print_hash(Elf32_Ehdr *e __unused, void *sh __unused)
1061 {
1062 }
1063 
1064 static void
1065 elf_print_note(Elf32_Ehdr *e, void *sh)
1066 {
1067 	u_int64_t offset;
1068 	u_int64_t size;
1069 	u_int64_t name;
1070 	u_int32_t namesz;
1071 	u_int32_t descsz;
1072 	u_int32_t desc;
1073 	u_int32_t type;
1074 	char *n, *s;
1075 	const char *nt_type;
1076 
1077 	offset = elf_get_off(e, sh, SH_OFFSET);
1078 	size = elf_get_size(e, sh, SH_SIZE);
1079 	name = elf_get_word(e, sh, SH_NAME);
1080 	n = (char *)e + offset;
1081 	fprintf(out, "\nnote (%s):\n", shstrtab + name);
1082 	while (n < ((char *)e + offset + size)) {
1083 		namesz = elf_get_word(e, n, N_NAMESZ);
1084 		descsz = elf_get_word(e, n, N_DESCSZ);
1085 		type = elf_get_word(e, n, N_TYPE);
1086 		if (type < nitems(nt_types) && nt_types[type] != NULL)
1087 			nt_type = nt_types[type];
1088 		else
1089 			nt_type = "Unknown type";
1090 		s = n + sizeof(Elf_Note);
1091 		desc = elf_get_word(e, n + sizeof(Elf_Note) + namesz, 0);
1092 		fprintf(out, "\t%s %d (%s)\n", s, desc, nt_type);
1093 		n += sizeof(Elf_Note) + namesz + descsz;
1094 	}
1095 }
1096 
1097 static u_int64_t
1098 elf_get_byte(Elf32_Ehdr *e, void *base, elf_member_t member)
1099 {
1100 	u_int64_t val;
1101 
1102 	val = 0;
1103 	switch (e->e_ident[EI_CLASS]) {
1104 	case ELFCLASS32:
1105 		val = ((uint8_t *)base)[elf32_offsets[member]];
1106 		break;
1107 	case ELFCLASS64:
1108 		val = ((uint8_t *)base)[elf64_offsets[member]];
1109 		break;
1110 	case ELFCLASSNONE:
1111 		errx(1, "invalid class");
1112 	}
1113 
1114 	return val;
1115 }
1116 
1117 static u_int64_t
1118 elf_get_quarter(Elf32_Ehdr *e, void *base, elf_member_t member)
1119 {
1120 	u_int64_t val;
1121 
1122 	val = 0;
1123 	switch (e->e_ident[EI_CLASS]) {
1124 	case ELFCLASS32:
1125 		base = (char *)base + elf32_offsets[member];
1126 		switch (e->e_ident[EI_DATA]) {
1127 		case ELFDATA2MSB:
1128 			val = be16dec(base);
1129 			break;
1130 		case ELFDATA2LSB:
1131 			val = le16dec(base);
1132 			break;
1133 		case ELFDATANONE:
1134 			errx(1, "invalid data format");
1135 		}
1136 		break;
1137 	case ELFCLASS64:
1138 		base = (char *)base + elf64_offsets[member];
1139 		switch (e->e_ident[EI_DATA]) {
1140 		case ELFDATA2MSB:
1141 			val = be16dec(base);
1142 			break;
1143 		case ELFDATA2LSB:
1144 			val = le16dec(base);
1145 			break;
1146 		case ELFDATANONE:
1147 			errx(1, "invalid data format");
1148 		}
1149 		break;
1150 	case ELFCLASSNONE:
1151 		errx(1, "invalid class");
1152 	}
1153 
1154 	return val;
1155 }
1156 
1157 #if 0
1158 static u_int64_t
1159 elf_get_half(Elf32_Ehdr *e, void *base, elf_member_t member)
1160 {
1161 	u_int64_t val;
1162 
1163 	val = 0;
1164 	switch (e->e_ident[EI_CLASS]) {
1165 	case ELFCLASS32:
1166 		base = (char *)base + elf32_offsets[member];
1167 		switch (e->e_ident[EI_DATA]) {
1168 		case ELFDATA2MSB:
1169 			val = be16dec(base);
1170 			break;
1171 		case ELFDATA2LSB:
1172 			val = le16dec(base);
1173 			break;
1174 		case ELFDATANONE:
1175 			errx(1, "invalid data format");
1176 		}
1177 		break;
1178 	case ELFCLASS64:
1179 		base = (char *)base + elf64_offsets[member];
1180 		switch (e->e_ident[EI_DATA]) {
1181 		case ELFDATA2MSB:
1182 			val = be32dec(base);
1183 			break;
1184 		case ELFDATA2LSB:
1185 			val = le32dec(base);
1186 			break;
1187 		case ELFDATANONE:
1188 			errx(1, "invalid data format");
1189 		}
1190 		break;
1191 	case ELFCLASSNONE:
1192 		errx(1, "invalid class");
1193 	}
1194 
1195 	return val;
1196 }
1197 #endif
1198 
1199 static u_int64_t
1200 elf_get_word(Elf32_Ehdr *e, void *base, elf_member_t member)
1201 {
1202 	u_int64_t val;
1203 
1204 	val = 0;
1205 	switch (e->e_ident[EI_CLASS]) {
1206 	case ELFCLASS32:
1207 		base = (char *)base + elf32_offsets[member];
1208 		switch (e->e_ident[EI_DATA]) {
1209 		case ELFDATA2MSB:
1210 			val = be32dec(base);
1211 			break;
1212 		case ELFDATA2LSB:
1213 			val = le32dec(base);
1214 			break;
1215 		case ELFDATANONE:
1216 			errx(1, "invalid data format");
1217 		}
1218 		break;
1219 	case ELFCLASS64:
1220 		base = (char *)base + elf64_offsets[member];
1221 		switch (e->e_ident[EI_DATA]) {
1222 		case ELFDATA2MSB:
1223 			val = be32dec(base);
1224 			break;
1225 		case ELFDATA2LSB:
1226 			val = le32dec(base);
1227 			break;
1228 		case ELFDATANONE:
1229 			errx(1, "invalid data format");
1230 		}
1231 		break;
1232 	case ELFCLASSNONE:
1233 		errx(1, "invalid class");
1234 	}
1235 
1236 	return val;
1237 }
1238 
1239 static u_int64_t
1240 elf_get_quad(Elf32_Ehdr *e, void *base, elf_member_t member)
1241 {
1242 	u_int64_t val;
1243 
1244 	val = 0;
1245 	switch (e->e_ident[EI_CLASS]) {
1246 	case ELFCLASS32:
1247 		base = (char *)base + elf32_offsets[member];
1248 		switch (e->e_ident[EI_DATA]) {
1249 		case ELFDATA2MSB:
1250 			val = be32dec(base);
1251 			break;
1252 		case ELFDATA2LSB:
1253 			val = le32dec(base);
1254 			break;
1255 		case ELFDATANONE:
1256 			errx(1, "invalid data format");
1257 		}
1258 		break;
1259 	case ELFCLASS64:
1260 		base = (char *)base + elf64_offsets[member];
1261 		switch (e->e_ident[EI_DATA]) {
1262 		case ELFDATA2MSB:
1263 			val = be64dec(base);
1264 			break;
1265 		case ELFDATA2LSB:
1266 			val = le64dec(base);
1267 			break;
1268 		case ELFDATANONE:
1269 			errx(1, "invalid data format");
1270 		}
1271 		break;
1272 	case ELFCLASSNONE:
1273 		errx(1, "invalid class");
1274 	}
1275 
1276 	return val;
1277 }
1278 
1279 static void
1280 usage(void)
1281 {
1282 	fprintf(stderr,
1283 	    "usage: elfdump -a | -E | -cdeGhinprs [-w file] file\n");
1284 	exit(1);
1285 }
1286