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