1 /*-
2  * Copyright (c) 2009-2015 Kai Wang
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/param.h>
28 #include <sys/queue.h>
29 
30 #include <ar.h>
31 #include <assert.h>
32 #include <capsicum_helpers.h>
33 #include <ctype.h>
34 #include <dwarf.h>
35 #include <err.h>
36 #include <fcntl.h>
37 #include <gelf.h>
38 #include <getopt.h>
39 #include <libdwarf.h>
40 #include <libelftc.h>
41 #include <libgen.h>
42 #include <stdarg.h>
43 #include <stdint.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <time.h>
48 #include <unistd.h>
49 
50 #include <libcasper.h>
51 #include <casper/cap_fileargs.h>
52 
53 #include "_elftc.h"
54 
55 ELFTC_VCSID("$Id: readelf.c 3649 2018-11-24 03:26:23Z emaste $");
56 
57 /* Backwards compatability for older FreeBSD releases. */
58 #ifndef	STB_GNU_UNIQUE
59 #define	STB_GNU_UNIQUE 10
60 #endif
61 #ifndef	STT_SPARC_REGISTER
62 #define	STT_SPARC_REGISTER 13
63 #endif
64 
65 
66 /*
67  * readelf(1) options.
68  */
69 #define	RE_AA	0x00000001
70 #define	RE_C	0x00000002
71 #define	RE_DD	0x00000004
72 #define	RE_D	0x00000008
73 #define	RE_G	0x00000010
74 #define	RE_H	0x00000020
75 #define	RE_II	0x00000040
76 #define	RE_I	0x00000080
77 #define	RE_L	0x00000100
78 #define	RE_NN	0x00000200
79 #define	RE_N	0x00000400
80 #define	RE_P	0x00000800
81 #define	RE_R	0x00001000
82 #define	RE_SS	0x00002000
83 #define	RE_S	0x00004000
84 #define	RE_T	0x00008000
85 #define	RE_U	0x00010000
86 #define	RE_VV	0x00020000
87 #define	RE_WW	0x00040000
88 #define	RE_W	0x00080000
89 #define	RE_X	0x00100000
90 
91 /*
92  * dwarf dump options.
93  */
94 #define	DW_A	0x00000001
95 #define	DW_FF	0x00000002
96 #define	DW_F	0x00000004
97 #define	DW_I	0x00000008
98 #define	DW_LL	0x00000010
99 #define	DW_L	0x00000020
100 #define	DW_M	0x00000040
101 #define	DW_O	0x00000080
102 #define	DW_P	0x00000100
103 #define	DW_RR	0x00000200
104 #define	DW_R	0x00000400
105 #define	DW_S	0x00000800
106 
107 #define	DW_DEFAULT_OPTIONS (DW_A | DW_F | DW_I | DW_L | DW_O | DW_P | \
108 	    DW_R | DW_RR | DW_S)
109 
110 /*
111  * readelf(1) run control flags.
112  */
113 #define	DISPLAY_FILENAME	0x0001
114 
115 /*
116  * Internal data structure for sections.
117  */
118 struct section {
119 	const char	*name;		/* section name */
120 	Elf_Scn		*scn;		/* section scn */
121 	uint64_t	 off;		/* section offset */
122 	uint64_t	 sz;		/* section size */
123 	uint64_t	 entsize;	/* section entsize */
124 	uint64_t	 align;		/* section alignment */
125 	uint64_t	 type;		/* section type */
126 	uint64_t	 flags;		/* section flags */
127 	uint64_t	 addr;		/* section virtual addr */
128 	uint32_t	 link;		/* section link ndx */
129 	uint32_t	 info;		/* section info ndx */
130 };
131 
132 struct dumpop {
133 	union {
134 		size_t si;		/* section index */
135 		const char *sn;		/* section name */
136 	} u;
137 	enum {
138 		DUMP_BY_INDEX = 0,
139 		DUMP_BY_NAME
140 	} type;				/* dump type */
141 #define HEX_DUMP	0x0001
142 #define STR_DUMP	0x0002
143 	int op;				/* dump operation */
144 	STAILQ_ENTRY(dumpop) dumpop_list;
145 };
146 
147 struct symver {
148 	const char *name;
149 	int type;
150 };
151 
152 /*
153  * Structure encapsulates the global data for readelf(1).
154  */
155 struct readelf {
156 	const char	 *filename;	/* current processing file. */
157 	int		  options;	/* command line options. */
158 	int		  flags;	/* run control flags. */
159 	int		  dop;		/* dwarf dump options. */
160 	Elf		 *elf;		/* underlying ELF descriptor. */
161 	Elf		 *ar;		/* archive ELF descriptor. */
162 	Dwarf_Debug	  dbg;		/* DWARF handle. */
163 	Dwarf_Half	  cu_psize;	/* DWARF CU pointer size. */
164 	Dwarf_Half	  cu_osize;	/* DWARF CU offset size. */
165 	Dwarf_Half	  cu_ver;	/* DWARF CU version. */
166 	GElf_Ehdr	  ehdr;		/* ELF header. */
167 	int		  ec;		/* ELF class. */
168 	size_t		  shnum;	/* #sections. */
169 	struct section	 *vd_s;		/* Verdef section. */
170 	struct section	 *vn_s;		/* Verneed section. */
171 	struct section	 *vs_s;		/* Versym section. */
172 	uint16_t	 *vs;		/* Versym array. */
173 	int		  vs_sz;	/* Versym array size. */
174 	struct symver	 *ver;		/* Version array. */
175 	int		  ver_sz;	/* Size of version array. */
176 	struct section	 *sl;		/* list of sections. */
177 	STAILQ_HEAD(, dumpop) v_dumpop; /* list of dump ops. */
178 	uint64_t	(*dw_read)(Elf_Data *, uint64_t *, int);
179 	uint64_t	(*dw_decode)(uint8_t **, int);
180 };
181 
182 enum options
183 {
184 	OPTION_DEBUG_DUMP
185 };
186 
187 static struct option longopts[] = {
188 	{"all", no_argument, NULL, 'a'},
189 	{"arch-specific", no_argument, NULL, 'A'},
190 	{"archive-index", no_argument, NULL, 'c'},
191 	{"debug-dump", optional_argument, NULL, OPTION_DEBUG_DUMP},
192 	{"dynamic", no_argument, NULL, 'd'},
193 	{"file-header", no_argument, NULL, 'h'},
194 	{"full-section-name", no_argument, NULL, 'N'},
195 	{"headers", no_argument, NULL, 'e'},
196 	{"help", no_argument, 0, 'H'},
197 	{"hex-dump", required_argument, NULL, 'x'},
198 	{"histogram", no_argument, NULL, 'I'},
199 	{"notes", no_argument, NULL, 'n'},
200 	{"program-headers", no_argument, NULL, 'l'},
201 	{"relocs", no_argument, NULL, 'r'},
202 	{"sections", no_argument, NULL, 'S'},
203 	{"section-headers", no_argument, NULL, 'S'},
204 	{"section-groups", no_argument, NULL, 'g'},
205 	{"section-details", no_argument, NULL, 't'},
206 	{"segments", no_argument, NULL, 'l'},
207 	{"string-dump", required_argument, NULL, 'p'},
208 	{"symbols", no_argument, NULL, 's'},
209 	{"syms", no_argument, NULL, 's'},
210 	{"unwind", no_argument, NULL, 'u'},
211 	{"use-dynamic", no_argument, NULL, 'D'},
212 	{"version-info", no_argument, 0, 'V'},
213 	{"version", no_argument, 0, 'v'},
214 	{"wide", no_argument, 0, 'W'},
215 	{NULL, 0, NULL, 0}
216 };
217 
218 struct eflags_desc {
219 	uint64_t flag;
220 	const char *desc;
221 };
222 
223 struct mips_option {
224 	uint64_t flag;
225 	const char *desc;
226 };
227 
228 struct flag_desc {
229 	uint64_t flag;
230 	const char *desc;
231 };
232 
233 struct loc_at {
234 	Dwarf_Attribute la_at;
235 	Dwarf_Unsigned la_off;
236 	Dwarf_Unsigned la_lowpc;
237 	Dwarf_Half la_cu_psize;
238 	Dwarf_Half la_cu_osize;
239 	Dwarf_Half la_cu_ver;
240 };
241 
242 static void add_dumpop(struct readelf *re, size_t si, const char *sn, int op,
243     int t);
244 static const char *aeabi_adv_simd_arch(uint64_t simd);
245 static const char *aeabi_align_needed(uint64_t an);
246 static const char *aeabi_align_preserved(uint64_t ap);
247 static const char *aeabi_arm_isa(uint64_t ai);
248 static const char *aeabi_cpu_arch(uint64_t arch);
249 static const char *aeabi_cpu_arch_profile(uint64_t pf);
250 static const char *aeabi_div(uint64_t du);
251 static const char *aeabi_enum_size(uint64_t es);
252 static const char *aeabi_fp_16bit_format(uint64_t fp16);
253 static const char *aeabi_fp_arch(uint64_t fp);
254 static const char *aeabi_fp_denormal(uint64_t fd);
255 static const char *aeabi_fp_exceptions(uint64_t fe);
256 static const char *aeabi_fp_hpext(uint64_t fh);
257 static const char *aeabi_fp_number_model(uint64_t fn);
258 static const char *aeabi_fp_optm_goal(uint64_t fog);
259 static const char *aeabi_fp_rounding(uint64_t fr);
260 static const char *aeabi_hardfp(uint64_t hfp);
261 static const char *aeabi_mpext(uint64_t mp);
262 static const char *aeabi_optm_goal(uint64_t og);
263 static const char *aeabi_pcs_config(uint64_t pcs);
264 static const char *aeabi_pcs_got(uint64_t got);
265 static const char *aeabi_pcs_r9(uint64_t r9);
266 static const char *aeabi_pcs_ro(uint64_t ro);
267 static const char *aeabi_pcs_rw(uint64_t rw);
268 static const char *aeabi_pcs_wchar_t(uint64_t wt);
269 static const char *aeabi_t2ee(uint64_t t2ee);
270 static const char *aeabi_thumb_isa(uint64_t ti);
271 static const char *aeabi_fp_user_exceptions(uint64_t fu);
272 static const char *aeabi_unaligned_access(uint64_t ua);
273 static const char *aeabi_vfp_args(uint64_t va);
274 static const char *aeabi_virtual(uint64_t vt);
275 static const char *aeabi_wmmx_arch(uint64_t wmmx);
276 static const char *aeabi_wmmx_args(uint64_t wa);
277 static const char *elf_class(unsigned int class);
278 static const char *elf_endian(unsigned int endian);
279 static const char *elf_machine(unsigned int mach);
280 static const char *elf_osabi(unsigned int abi);
281 static const char *elf_type(unsigned int type);
282 static const char *elf_ver(unsigned int ver);
283 static const char *dt_type(unsigned int mach, unsigned int dtype);
284 static void dump_ar(struct readelf *re, int);
285 static void dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe);
286 static void dump_attributes(struct readelf *re);
287 static uint8_t *dump_compatibility_tag(uint8_t *p, uint8_t *pe);
288 static void dump_dwarf(struct readelf *re);
289 static void dump_dwarf_abbrev(struct readelf *re);
290 static void dump_dwarf_aranges(struct readelf *re);
291 static void dump_dwarf_block(struct readelf *re, uint8_t *b,
292     Dwarf_Unsigned len);
293 static void dump_dwarf_die(struct readelf *re, Dwarf_Die die, int level);
294 static void dump_dwarf_frame(struct readelf *re, int alt);
295 static void dump_dwarf_frame_inst(struct readelf *re, Dwarf_Cie cie,
296     uint8_t *insts, Dwarf_Unsigned len, Dwarf_Unsigned caf, Dwarf_Signed daf,
297     Dwarf_Addr pc, Dwarf_Debug dbg);
298 static int dump_dwarf_frame_regtable(struct readelf *re, Dwarf_Fde fde,
299     Dwarf_Addr pc, Dwarf_Unsigned func_len, Dwarf_Half cie_ra);
300 static void dump_dwarf_frame_section(struct readelf *re, struct section *s,
301     int alt);
302 static void dump_dwarf_info(struct readelf *re, Dwarf_Bool is_info);
303 static void dump_dwarf_macinfo(struct readelf *re);
304 static void dump_dwarf_line(struct readelf *re);
305 static void dump_dwarf_line_decoded(struct readelf *re);
306 static void dump_dwarf_loc(struct readelf *re, Dwarf_Loc *lr);
307 static void dump_dwarf_loclist(struct readelf *re);
308 static void dump_dwarf_pubnames(struct readelf *re);
309 static void dump_dwarf_ranges(struct readelf *re);
310 static void dump_dwarf_ranges_foreach(struct readelf *re, Dwarf_Die die,
311     Dwarf_Addr base);
312 static void dump_dwarf_str(struct readelf *re);
313 static void dump_eflags(struct readelf *re, uint64_t e_flags);
314 static void dump_elf(struct readelf *re);
315 static void dump_flags(struct flag_desc *fd, uint64_t flags);
316 static void dump_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab);
317 static void dump_dynamic(struct readelf *re);
318 static void dump_liblist(struct readelf *re);
319 static void dump_mips_abiflags(struct readelf *re, struct section *s);
320 static void dump_mips_attributes(struct readelf *re, uint8_t *p, uint8_t *pe);
321 static void dump_mips_odk_reginfo(struct readelf *re, uint8_t *p, size_t sz);
322 static void dump_mips_options(struct readelf *re, struct section *s);
323 static void dump_mips_option_flags(const char *name, struct mips_option *opt,
324     uint64_t info);
325 static void dump_mips_reginfo(struct readelf *re, struct section *s);
326 static void dump_mips_specific_info(struct readelf *re);
327 static void dump_notes(struct readelf *re);
328 static void dump_notes_content(struct readelf *re, const char *buf, size_t sz,
329     off_t off);
330 static void dump_notes_data(struct readelf *re, const char *name,
331     uint32_t type, const char *buf, size_t sz);
332 static void dump_svr4_hash(struct section *s);
333 static void dump_svr4_hash64(struct readelf *re, struct section *s);
334 static void dump_gnu_hash(struct readelf *re, struct section *s);
335 static void dump_gnu_property_type_0(struct readelf *re, const char *buf,
336     size_t sz);
337 static void dump_hash(struct readelf *re);
338 static void dump_phdr(struct readelf *re);
339 static void dump_ppc_attributes(uint8_t *p, uint8_t *pe);
340 static void dump_section_groups(struct readelf *re);
341 static void dump_symtab(struct readelf *re, int i);
342 static void dump_symtabs(struct readelf *re);
343 static uint8_t *dump_unknown_tag(uint64_t tag, uint8_t *p, uint8_t *pe);
344 static void dump_ver(struct readelf *re);
345 static void dump_verdef(struct readelf *re, int dump);
346 static void dump_verneed(struct readelf *re, int dump);
347 static void dump_versym(struct readelf *re);
348 static const char *dwarf_reg(unsigned int mach, unsigned int reg);
349 static const char *dwarf_regname(struct readelf *re, unsigned int num);
350 static struct dumpop *find_dumpop(struct readelf *re, size_t si,
351     const char *sn, int op, int t);
352 static int get_ent_count(struct section *s, int *ent_count);
353 static int get_mips_register_size(uint8_t flag);
354 static char *get_regoff_str(struct readelf *re, Dwarf_Half reg,
355     Dwarf_Addr off);
356 static const char *get_string(struct readelf *re, int strtab, size_t off);
357 static const char *get_symbol_name(struct readelf *re, int symtab, int i);
358 static uint64_t get_symbol_value(struct readelf *re, int symtab, int i);
359 static void load_sections(struct readelf *re);
360 static int loc_at_comparator(const void *la1, const void *la2);
361 static const char *mips_abi_fp(uint64_t fp);
362 static const char *note_type(const char *note_name, unsigned int et,
363     unsigned int nt);
364 static const char *note_type_freebsd(unsigned int nt);
365 static const char *note_type_freebsd_core(unsigned int nt);
366 static const char *note_type_linux_core(unsigned int nt);
367 static const char *note_type_gnu(unsigned int nt);
368 static const char *note_type_netbsd(unsigned int nt);
369 static const char *note_type_openbsd(unsigned int nt);
370 static const char *note_type_unknown(unsigned int nt);
371 static const char *note_type_xen(unsigned int nt);
372 static const char *option_kind(uint8_t kind);
373 static const char *phdr_type(unsigned int mach, unsigned int ptype);
374 static const char *ppc_abi_fp(uint64_t fp);
375 static const char *ppc_abi_vector(uint64_t vec);
376 static void readelf_usage(int status);
377 static void readelf_version(void);
378 static void search_loclist_at(struct readelf *re, Dwarf_Die die,
379     Dwarf_Unsigned lowpc, struct loc_at **la_list,
380     size_t *la_list_len, size_t *la_list_cap);
381 static void search_ver(struct readelf *re);
382 static const char *section_type(unsigned int mach, unsigned int stype);
383 static void set_cu_context(struct readelf *re, Dwarf_Half psize,
384     Dwarf_Half osize, Dwarf_Half ver);
385 static const char *st_bind(unsigned int sbind);
386 static const char *st_shndx(unsigned int shndx);
387 static const char *st_type(unsigned int mach, unsigned int os,
388     unsigned int stype);
389 static const char *st_vis(unsigned int svis);
390 static const char *top_tag(unsigned int tag);
391 static void unload_sections(struct readelf *re);
392 static uint64_t _read_lsb(Elf_Data *d, uint64_t *offsetp,
393     int bytes_to_read);
394 static uint64_t _read_msb(Elf_Data *d, uint64_t *offsetp,
395     int bytes_to_read);
396 static uint64_t _decode_lsb(uint8_t **data, int bytes_to_read);
397 static uint64_t _decode_msb(uint8_t **data, int bytes_to_read);
398 static int64_t _decode_sleb128(uint8_t **dp, uint8_t *dpe);
399 static uint64_t _decode_uleb128(uint8_t **dp, uint8_t *dpe);
400 
401 static struct eflags_desc arm_eflags_desc[] = {
402 	{EF_ARM_RELEXEC, "relocatable executable"},
403 	{EF_ARM_HASENTRY, "has entry point"},
404 	{EF_ARM_SYMSARESORTED, "sorted symbol tables"},
405 	{EF_ARM_DYNSYMSUSESEGIDX, "dynamic symbols use segment index"},
406 	{EF_ARM_MAPSYMSFIRST, "mapping symbols precede others"},
407 	{EF_ARM_BE8, "BE8"},
408 	{EF_ARM_LE8, "LE8"},
409 	{EF_ARM_INTERWORK, "interworking enabled"},
410 	{EF_ARM_APCS_26, "uses APCS/26"},
411 	{EF_ARM_APCS_FLOAT, "uses APCS/float"},
412 	{EF_ARM_PIC, "position independent"},
413 	{EF_ARM_ALIGN8, "8 bit structure alignment"},
414 	{EF_ARM_NEW_ABI, "uses new ABI"},
415 	{EF_ARM_OLD_ABI, "uses old ABI"},
416 	{EF_ARM_SOFT_FLOAT, "software FP"},
417 	{EF_ARM_VFP_FLOAT, "VFP"},
418 	{EF_ARM_MAVERICK_FLOAT, "Maverick FP"},
419 	{0, NULL}
420 };
421 
422 static struct eflags_desc mips_eflags_desc[] = {
423 	{EF_MIPS_NOREORDER, "noreorder"},
424 	{EF_MIPS_PIC, "pic"},
425 	{EF_MIPS_CPIC, "cpic"},
426 	{EF_MIPS_UCODE, "ugen_reserved"},
427 	{EF_MIPS_ABI2, "abi2"},
428 	{EF_MIPS_OPTIONS_FIRST, "odk first"},
429 	{EF_MIPS_ARCH_ASE_MDMX, "mdmx"},
430 	{EF_MIPS_ARCH_ASE_M16, "mips16"},
431 	{0, NULL}
432 };
433 
434 static struct eflags_desc powerpc_eflags_desc[] = {
435 	{EF_PPC_EMB, "emb"},
436 	{EF_PPC_RELOCATABLE, "relocatable"},
437 	{EF_PPC_RELOCATABLE_LIB, "relocatable-lib"},
438 	{0, NULL}
439 };
440 
441 static struct eflags_desc riscv_eflags_desc[] = {
442 	{EF_RISCV_RVC, "RVC"},
443 	{EF_RISCV_RVE, "RVE"},
444 	{EF_RISCV_TSO, "TSO"},
445 	{0, NULL}
446 };
447 
448 static struct eflags_desc sparc_eflags_desc[] = {
449 	{EF_SPARC_32PLUS, "v8+"},
450 	{EF_SPARC_SUN_US1, "ultrasparcI"},
451 	{EF_SPARC_HAL_R1, "halr1"},
452 	{EF_SPARC_SUN_US3, "ultrasparcIII"},
453 	{0, NULL}
454 };
455 
456 static const char *
457 elf_osabi(unsigned int abi)
458 {
459 	static char s_abi[32];
460 
461 	switch(abi) {
462 	case ELFOSABI_NONE: return "NONE";
463 	case ELFOSABI_HPUX: return "HPUX";
464 	case ELFOSABI_NETBSD: return "NetBSD";
465 	case ELFOSABI_GNU: return "GNU";
466 	case ELFOSABI_HURD: return "HURD";
467 	case ELFOSABI_86OPEN: return "86OPEN";
468 	case ELFOSABI_SOLARIS: return "Solaris";
469 	case ELFOSABI_AIX: return "AIX";
470 	case ELFOSABI_IRIX: return "IRIX";
471 	case ELFOSABI_FREEBSD: return "FreeBSD";
472 	case ELFOSABI_TRU64: return "TRU64";
473 	case ELFOSABI_MODESTO: return "MODESTO";
474 	case ELFOSABI_OPENBSD: return "OpenBSD";
475 	case ELFOSABI_OPENVMS: return "OpenVMS";
476 	case ELFOSABI_NSK: return "NSK";
477 	case ELFOSABI_CLOUDABI: return "CloudABI";
478 	case ELFOSABI_ARM_AEABI: return "ARM EABI";
479 	case ELFOSABI_ARM: return "ARM";
480 	case ELFOSABI_STANDALONE: return "StandAlone";
481 	default:
482 		snprintf(s_abi, sizeof(s_abi), "<unknown: %#x>", abi);
483 		return (s_abi);
484 	}
485 };
486 
487 static const char *
488 elf_machine(unsigned int mach)
489 {
490 	static char s_mach[32];
491 
492 	switch (mach) {
493 	case EM_NONE: return "Unknown machine";
494 	case EM_M32: return "AT&T WE32100";
495 	case EM_SPARC: return "Sun SPARC";
496 	case EM_386: return "Intel i386";
497 	case EM_68K: return "Motorola 68000";
498 	case EM_IAMCU: return "Intel MCU";
499 	case EM_88K: return "Motorola 88000";
500 	case EM_860: return "Intel i860";
501 	case EM_MIPS: return "MIPS R3000 Big-Endian only";
502 	case EM_S370: return "IBM System/370";
503 	case EM_MIPS_RS3_LE: return "MIPS R3000 Little-Endian";
504 	case EM_PARISC: return "HP PA-RISC";
505 	case EM_VPP500: return "Fujitsu VPP500";
506 	case EM_SPARC32PLUS: return "SPARC v8plus";
507 	case EM_960: return "Intel 80960";
508 	case EM_PPC: return "PowerPC 32-bit";
509 	case EM_PPC64: return "PowerPC 64-bit";
510 	case EM_S390: return "IBM System/390";
511 	case EM_V800: return "NEC V800";
512 	case EM_FR20: return "Fujitsu FR20";
513 	case EM_RH32: return "TRW RH-32";
514 	case EM_RCE: return "Motorola RCE";
515 	case EM_ARM: return "ARM";
516 	case EM_SH: return "Hitachi SH";
517 	case EM_SPARCV9: return "SPARC v9 64-bit";
518 	case EM_TRICORE: return "Siemens TriCore embedded processor";
519 	case EM_ARC: return "Argonaut RISC Core";
520 	case EM_H8_300: return "Hitachi H8/300";
521 	case EM_H8_300H: return "Hitachi H8/300H";
522 	case EM_H8S: return "Hitachi H8S";
523 	case EM_H8_500: return "Hitachi H8/500";
524 	case EM_IA_64: return "Intel IA-64 Processor";
525 	case EM_MIPS_X: return "Stanford MIPS-X";
526 	case EM_COLDFIRE: return "Motorola ColdFire";
527 	case EM_68HC12: return "Motorola M68HC12";
528 	case EM_MMA: return "Fujitsu MMA";
529 	case EM_PCP: return "Siemens PCP";
530 	case EM_NCPU: return "Sony nCPU";
531 	case EM_NDR1: return "Denso NDR1 microprocessor";
532 	case EM_STARCORE: return "Motorola Star*Core processor";
533 	case EM_ME16: return "Toyota ME16 processor";
534 	case EM_ST100: return "STMicroelectronics ST100 processor";
535 	case EM_TINYJ: return "Advanced Logic Corp. TinyJ processor";
536 	case EM_X86_64: return "Advanced Micro Devices x86-64";
537 	case EM_PDSP: return "Sony DSP Processor";
538 	case EM_FX66: return "Siemens FX66 microcontroller";
539 	case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 microcontroller";
540 	case EM_ST7: return "STmicroelectronics ST7 8-bit microcontroller";
541 	case EM_68HC16: return "Motorola MC68HC16 microcontroller";
542 	case EM_68HC11: return "Motorola MC68HC11 microcontroller";
543 	case EM_68HC08: return "Motorola MC68HC08 microcontroller";
544 	case EM_68HC05: return "Motorola MC68HC05 microcontroller";
545 	case EM_SVX: return "Silicon Graphics SVx";
546 	case EM_ST19: return "STMicroelectronics ST19 8-bit mc";
547 	case EM_VAX: return "Digital VAX";
548 	case EM_CRIS: return "Axis Communications 32-bit embedded processor";
549 	case EM_JAVELIN: return "Infineon Tech. 32bit embedded processor";
550 	case EM_FIREPATH: return "Element 14 64-bit DSP Processor";
551 	case EM_ZSP: return "LSI Logic 16-bit DSP Processor";
552 	case EM_MMIX: return "Donald Knuth's educational 64-bit proc";
553 	case EM_HUANY: return "Harvard University MI object files";
554 	case EM_PRISM: return "SiTera Prism";
555 	case EM_AVR: return "Atmel AVR 8-bit microcontroller";
556 	case EM_FR30: return "Fujitsu FR30";
557 	case EM_D10V: return "Mitsubishi D10V";
558 	case EM_D30V: return "Mitsubishi D30V";
559 	case EM_V850: return "NEC v850";
560 	case EM_M32R: return "Mitsubishi M32R";
561 	case EM_MN10300: return "Matsushita MN10300";
562 	case EM_MN10200: return "Matsushita MN10200";
563 	case EM_PJ: return "picoJava";
564 	case EM_OPENRISC: return "OpenRISC 32-bit embedded processor";
565 	case EM_ARC_A5: return "ARC Cores Tangent-A5";
566 	case EM_XTENSA: return "Tensilica Xtensa Architecture";
567 	case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
568 	case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
569 	case EM_NS32K: return "National Semiconductor 32000 series";
570 	case EM_TPC: return "Tenor Network TPC processor";
571 	case EM_SNP1K: return "Trebia SNP 1000 processor";
572 	case EM_ST200: return "STMicroelectronics ST200 microcontroller";
573 	case EM_IP2K: return "Ubicom IP2xxx microcontroller family";
574 	case EM_MAX: return "MAX Processor";
575 	case EM_CR: return "National Semiconductor CompactRISC microprocessor";
576 	case EM_F2MC16: return "Fujitsu F2MC16";
577 	case EM_MSP430: return "TI embedded microcontroller msp430";
578 	case EM_BLACKFIN: return "Analog Devices Blackfin (DSP) processor";
579 	case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
580 	case EM_SEP: return "Sharp embedded microprocessor";
581 	case EM_ARCA: return "Arca RISC Microprocessor";
582 	case EM_UNICORE: return "Microprocessor series from PKU-Unity Ltd";
583 	case EM_AARCH64: return "AArch64";
584 	case EM_RISCV: return "RISC-V";
585 	default:
586 		snprintf(s_mach, sizeof(s_mach), "<unknown: %#x>", mach);
587 		return (s_mach);
588 	}
589 
590 }
591 
592 static const char *
593 elf_class(unsigned int class)
594 {
595 	static char s_class[32];
596 
597 	switch (class) {
598 	case ELFCLASSNONE: return "none";
599 	case ELFCLASS32: return "ELF32";
600 	case ELFCLASS64: return "ELF64";
601 	default:
602 		snprintf(s_class, sizeof(s_class), "<unknown: %#x>", class);
603 		return (s_class);
604 	}
605 }
606 
607 static const char *
608 elf_endian(unsigned int endian)
609 {
610 	static char s_endian[32];
611 
612 	switch (endian) {
613 	case ELFDATANONE: return "none";
614 	case ELFDATA2LSB: return "2's complement, little endian";
615 	case ELFDATA2MSB: return "2's complement, big endian";
616 	default:
617 		snprintf(s_endian, sizeof(s_endian), "<unknown: %#x>", endian);
618 		return (s_endian);
619 	}
620 }
621 
622 static const char *
623 elf_type(unsigned int type)
624 {
625 	static char s_type[32];
626 
627 	switch (type) {
628 	case ET_NONE: return "NONE (None)";
629 	case ET_REL: return "REL (Relocatable file)";
630 	case ET_EXEC: return "EXEC (Executable file)";
631 	case ET_DYN: return "DYN (Shared object file)";
632 	case ET_CORE: return "CORE (Core file)";
633 	default:
634 		if (type >= ET_LOPROC)
635 			snprintf(s_type, sizeof(s_type), "<proc: %#x>", type);
636 		else if (type >= ET_LOOS && type <= ET_HIOS)
637 			snprintf(s_type, sizeof(s_type), "<os: %#x>", type);
638 		else
639 			snprintf(s_type, sizeof(s_type), "<unknown: %#x>",
640 			    type);
641 		return (s_type);
642 	}
643 }
644 
645 static const char *
646 elf_ver(unsigned int ver)
647 {
648 	static char s_ver[32];
649 
650 	switch (ver) {
651 	case EV_CURRENT: return "(current)";
652 	case EV_NONE: return "(none)";
653 	default:
654 		snprintf(s_ver, sizeof(s_ver), "<unknown: %#x>",
655 		    ver);
656 		return (s_ver);
657 	}
658 }
659 
660 static const char *
661 phdr_type(unsigned int mach, unsigned int ptype)
662 {
663 	static char s_ptype[32];
664 
665 	if (ptype >= PT_LOPROC && ptype <= PT_HIPROC) {
666 		switch (mach) {
667 		case EM_ARM:
668 			switch (ptype) {
669 			case PT_ARM_ARCHEXT: return "ARM_ARCHEXT";
670 			case PT_ARM_EXIDX: return "ARM_EXIDX";
671 			}
672 			break;
673 		}
674 		snprintf(s_ptype, sizeof(s_ptype), "LOPROC+%#x",
675 		    ptype - PT_LOPROC);
676 		return (s_ptype);
677 	}
678 
679 	switch (ptype) {
680 	case PT_NULL: return "NULL";
681 	case PT_LOAD: return "LOAD";
682 	case PT_DYNAMIC: return "DYNAMIC";
683 	case PT_INTERP: return "INTERP";
684 	case PT_NOTE: return "NOTE";
685 	case PT_SHLIB: return "SHLIB";
686 	case PT_PHDR: return "PHDR";
687 	case PT_TLS: return "TLS";
688 	case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
689 	case PT_GNU_STACK: return "GNU_STACK";
690 	case PT_GNU_RELRO: return "GNU_RELRO";
691 	case PT_OPENBSD_RANDOMIZE: return "OPENBSD_RANDOMIZE";
692 	case PT_OPENBSD_WXNEEDED: return "OPENBSD_WXNEEDED";
693 	case PT_OPENBSD_BOOTDATA: return "OPENBSD_BOOTDATA";
694 	default:
695 		if (ptype >= PT_LOOS && ptype <= PT_HIOS)
696 			snprintf(s_ptype, sizeof(s_ptype), "LOOS+%#x",
697 			    ptype - PT_LOOS);
698 		else
699 			snprintf(s_ptype, sizeof(s_ptype), "<unknown: %#x>",
700 			    ptype);
701 		return (s_ptype);
702 	}
703 }
704 
705 static const char *
706 section_type(unsigned int mach, unsigned int stype)
707 {
708 	static char s_stype[32];
709 
710 	if (stype >= SHT_LOPROC && stype <= SHT_HIPROC) {
711 		switch (mach) {
712 		case EM_ARM:
713 			switch (stype) {
714 			case SHT_ARM_EXIDX: return "ARM_EXIDX";
715 			case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP";
716 			case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES";
717 			case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY";
718 			case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION";
719 			}
720 			break;
721 		case EM_X86_64:
722 			switch (stype) {
723 			case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
724 			default:
725 				break;
726 			}
727 			break;
728 		case EM_MIPS:
729 		case EM_MIPS_RS3_LE:
730 			switch (stype) {
731 			case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
732 			case SHT_MIPS_MSYM: return "MIPS_MSYM";
733 			case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
734 			case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
735 			case SHT_MIPS_UCODE: return "MIPS_UCODE";
736 			case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
737 			case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
738 			case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
739 			case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
740 			case SHT_MIPS_RELD: return "MIPS_RELD";
741 			case SHT_MIPS_IFACE: return "MIPS_IFACE";
742 			case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
743 			case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
744 			case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
745 			case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
746 			case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
747 			case SHT_MIPS_DWARF: return "MIPS_DWARF";
748 			case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
749 			case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
750 			case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
751 			case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
752 			case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
753 			case SHT_MIPS_XLATE: return "MIPS_XLATE";
754 			case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
755 			case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
756 			case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
757 			case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
758 			case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
759 			case SHT_MIPS_ABIFLAGS: return "MIPS_ABIFLAGS";
760 			default:
761 				break;
762 			}
763 			break;
764 		default:
765 			break;
766 		}
767 
768 		snprintf(s_stype, sizeof(s_stype), "LOPROC+%#x",
769 		    stype - SHT_LOPROC);
770 		return (s_stype);
771 	}
772 
773 	switch (stype) {
774 	case SHT_NULL: return "NULL";
775 	case SHT_PROGBITS: return "PROGBITS";
776 	case SHT_SYMTAB: return "SYMTAB";
777 	case SHT_STRTAB: return "STRTAB";
778 	case SHT_RELA: return "RELA";
779 	case SHT_HASH: return "HASH";
780 	case SHT_DYNAMIC: return "DYNAMIC";
781 	case SHT_NOTE: return "NOTE";
782 	case SHT_NOBITS: return "NOBITS";
783 	case SHT_REL: return "REL";
784 	case SHT_SHLIB: return "SHLIB";
785 	case SHT_DYNSYM: return "DYNSYM";
786 	case SHT_INIT_ARRAY: return "INIT_ARRAY";
787 	case SHT_FINI_ARRAY: return "FINI_ARRAY";
788 	case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
789 	case SHT_GROUP: return "GROUP";
790 	case SHT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
791 	case SHT_SUNW_dof: return "SUNW_dof";
792 	case SHT_SUNW_cap: return "SUNW_cap";
793 	case SHT_GNU_HASH: return "GNU_HASH";
794 	case SHT_SUNW_ANNOTATE: return "SUNW_ANNOTATE";
795 	case SHT_SUNW_DEBUGSTR: return "SUNW_DEBUGSTR";
796 	case SHT_SUNW_DEBUG: return "SUNW_DEBUG";
797 	case SHT_SUNW_move: return "SUNW_move";
798 	case SHT_SUNW_COMDAT: return "SUNW_COMDAT";
799 	case SHT_SUNW_syminfo: return "SUNW_syminfo";
800 	case SHT_SUNW_verdef: return "SUNW_verdef";
801 	case SHT_SUNW_verneed: return "SUNW_verneed";
802 	case SHT_SUNW_versym: return "SUNW_versym";
803 	default:
804 		if (stype >= SHT_LOOS && stype <= SHT_HIOS)
805 			snprintf(s_stype, sizeof(s_stype), "LOOS+%#x",
806 			    stype - SHT_LOOS);
807 		else if (stype >= SHT_LOUSER)
808 			snprintf(s_stype, sizeof(s_stype), "LOUSER+%#x",
809 			    stype - SHT_LOUSER);
810 		else
811 			snprintf(s_stype, sizeof(s_stype), "<unknown: %#x>",
812 			    stype);
813 		return (s_stype);
814 	}
815 }
816 
817 static const char *
818 dt_type(unsigned int mach, unsigned int dtype)
819 {
820 	static char s_dtype[32];
821 
822 	switch (dtype) {
823 	case DT_NULL: return "NULL";
824 	case DT_NEEDED: return "NEEDED";
825 	case DT_PLTRELSZ: return "PLTRELSZ";
826 	case DT_PLTGOT: return "PLTGOT";
827 	case DT_HASH: return "HASH";
828 	case DT_STRTAB: return "STRTAB";
829 	case DT_SYMTAB: return "SYMTAB";
830 	case DT_RELA: return "RELA";
831 	case DT_RELASZ: return "RELASZ";
832 	case DT_RELAENT: return "RELAENT";
833 	case DT_STRSZ: return "STRSZ";
834 	case DT_SYMENT: return "SYMENT";
835 	case DT_INIT: return "INIT";
836 	case DT_FINI: return "FINI";
837 	case DT_SONAME: return "SONAME";
838 	case DT_RPATH: return "RPATH";
839 	case DT_SYMBOLIC: return "SYMBOLIC";
840 	case DT_REL: return "REL";
841 	case DT_RELSZ: return "RELSZ";
842 	case DT_RELENT: return "RELENT";
843 	case DT_PLTREL: return "PLTREL";
844 	case DT_DEBUG: return "DEBUG";
845 	case DT_TEXTREL: return "TEXTREL";
846 	case DT_JMPREL: return "JMPREL";
847 	case DT_BIND_NOW: return "BIND_NOW";
848 	case DT_INIT_ARRAY: return "INIT_ARRAY";
849 	case DT_FINI_ARRAY: return "FINI_ARRAY";
850 	case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
851 	case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
852 	case DT_RUNPATH: return "RUNPATH";
853 	case DT_FLAGS: return "FLAGS";
854 	case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
855 	case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
856 	case DT_MAXPOSTAGS: return "MAXPOSTAGS";
857 	case DT_SUNW_AUXILIARY: return "SUNW_AUXILIARY";
858 	case DT_SUNW_RTLDINF: return "SUNW_RTLDINF";
859 	case DT_SUNW_FILTER: return "SUNW_FILTER";
860 	case DT_SUNW_CAP: return "SUNW_CAP";
861 	case DT_SUNW_ASLR: return "SUNW_ASLR";
862 	case DT_CHECKSUM: return "CHECKSUM";
863 	case DT_PLTPADSZ: return "PLTPADSZ";
864 	case DT_MOVEENT: return "MOVEENT";
865 	case DT_MOVESZ: return "MOVESZ";
866 	case DT_FEATURE: return "FEATURE";
867 	case DT_POSFLAG_1: return "POSFLAG_1";
868 	case DT_SYMINSZ: return "SYMINSZ";
869 	case DT_SYMINENT: return "SYMINENT";
870 	case DT_GNU_HASH: return "GNU_HASH";
871 	case DT_TLSDESC_PLT: return "DT_TLSDESC_PLT";
872 	case DT_TLSDESC_GOT: return "DT_TLSDESC_GOT";
873 	case DT_GNU_CONFLICT: return "GNU_CONFLICT";
874 	case DT_GNU_LIBLIST: return "GNU_LIBLIST";
875 	case DT_CONFIG: return "CONFIG";
876 	case DT_DEPAUDIT: return "DEPAUDIT";
877 	case DT_AUDIT: return "AUDIT";
878 	case DT_PLTPAD: return "PLTPAD";
879 	case DT_MOVETAB: return "MOVETAB";
880 	case DT_SYMINFO: return "SYMINFO";
881 	case DT_VERSYM: return "VERSYM";
882 	case DT_RELACOUNT: return "RELACOUNT";
883 	case DT_RELCOUNT: return "RELCOUNT";
884 	case DT_FLAGS_1: return "FLAGS_1";
885 	case DT_VERDEF: return "VERDEF";
886 	case DT_VERDEFNUM: return "VERDEFNUM";
887 	case DT_VERNEED: return "VERNEED";
888 	case DT_VERNEEDNUM: return "VERNEEDNUM";
889 	case DT_AUXILIARY: return "AUXILIARY";
890 	case DT_USED: return "USED";
891 	case DT_FILTER: return "FILTER";
892 	case DT_GNU_PRELINKED: return "GNU_PRELINKED";
893 	case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
894 	case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
895 	}
896 
897 	if (dtype >= DT_LOPROC && dtype <= DT_HIPROC) {
898 		switch (mach) {
899 		case EM_ARM:
900 			switch (dtype) {
901 			case DT_ARM_SYMTABSZ:
902 				return "ARM_SYMTABSZ";
903 			default:
904 				break;
905 			}
906 			break;
907 		case EM_MIPS:
908 		case EM_MIPS_RS3_LE:
909 			switch (dtype) {
910 			case DT_MIPS_RLD_VERSION:
911 				return "MIPS_RLD_VERSION";
912 			case DT_MIPS_TIME_STAMP:
913 				return "MIPS_TIME_STAMP";
914 			case DT_MIPS_ICHECKSUM:
915 				return "MIPS_ICHECKSUM";
916 			case DT_MIPS_IVERSION:
917 				return "MIPS_IVERSION";
918 			case DT_MIPS_FLAGS:
919 				return "MIPS_FLAGS";
920 			case DT_MIPS_BASE_ADDRESS:
921 				return "MIPS_BASE_ADDRESS";
922 			case DT_MIPS_CONFLICT:
923 				return "MIPS_CONFLICT";
924 			case DT_MIPS_LIBLIST:
925 				return "MIPS_LIBLIST";
926 			case DT_MIPS_LOCAL_GOTNO:
927 				return "MIPS_LOCAL_GOTNO";
928 			case DT_MIPS_CONFLICTNO:
929 				return "MIPS_CONFLICTNO";
930 			case DT_MIPS_LIBLISTNO:
931 				return "MIPS_LIBLISTNO";
932 			case DT_MIPS_SYMTABNO:
933 				return "MIPS_SYMTABNO";
934 			case DT_MIPS_UNREFEXTNO:
935 				return "MIPS_UNREFEXTNO";
936 			case DT_MIPS_GOTSYM:
937 				return "MIPS_GOTSYM";
938 			case DT_MIPS_HIPAGENO:
939 				return "MIPS_HIPAGENO";
940 			case DT_MIPS_RLD_MAP:
941 				return "MIPS_RLD_MAP";
942 			case DT_MIPS_DELTA_CLASS:
943 				return "MIPS_DELTA_CLASS";
944 			case DT_MIPS_DELTA_CLASS_NO:
945 				return "MIPS_DELTA_CLASS_NO";
946 			case DT_MIPS_DELTA_INSTANCE:
947 				return "MIPS_DELTA_INSTANCE";
948 			case DT_MIPS_DELTA_INSTANCE_NO:
949 				return "MIPS_DELTA_INSTANCE_NO";
950 			case DT_MIPS_DELTA_RELOC:
951 				return "MIPS_DELTA_RELOC";
952 			case DT_MIPS_DELTA_RELOC_NO:
953 				return "MIPS_DELTA_RELOC_NO";
954 			case DT_MIPS_DELTA_SYM:
955 				return "MIPS_DELTA_SYM";
956 			case DT_MIPS_DELTA_SYM_NO:
957 				return "MIPS_DELTA_SYM_NO";
958 			case DT_MIPS_DELTA_CLASSSYM:
959 				return "MIPS_DELTA_CLASSSYM";
960 			case DT_MIPS_DELTA_CLASSSYM_NO:
961 				return "MIPS_DELTA_CLASSSYM_NO";
962 			case DT_MIPS_CXX_FLAGS:
963 				return "MIPS_CXX_FLAGS";
964 			case DT_MIPS_PIXIE_INIT:
965 				return "MIPS_PIXIE_INIT";
966 			case DT_MIPS_SYMBOL_LIB:
967 				return "MIPS_SYMBOL_LIB";
968 			case DT_MIPS_LOCALPAGE_GOTIDX:
969 				return "MIPS_LOCALPAGE_GOTIDX";
970 			case DT_MIPS_LOCAL_GOTIDX:
971 				return "MIPS_LOCAL_GOTIDX";
972 			case DT_MIPS_HIDDEN_GOTIDX:
973 				return "MIPS_HIDDEN_GOTIDX";
974 			case DT_MIPS_PROTECTED_GOTIDX:
975 				return "MIPS_PROTECTED_GOTIDX";
976 			case DT_MIPS_OPTIONS:
977 				return "MIPS_OPTIONS";
978 			case DT_MIPS_INTERFACE:
979 				return "MIPS_INTERFACE";
980 			case DT_MIPS_DYNSTR_ALIGN:
981 				return "MIPS_DYNSTR_ALIGN";
982 			case DT_MIPS_INTERFACE_SIZE:
983 				return "MIPS_INTERFACE_SIZE";
984 			case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
985 				return "MIPS_RLD_TEXT_RESOLVE_ADDR";
986 			case DT_MIPS_PERF_SUFFIX:
987 				return "MIPS_PERF_SUFFIX";
988 			case DT_MIPS_COMPACT_SIZE:
989 				return "MIPS_COMPACT_SIZE";
990 			case DT_MIPS_GP_VALUE:
991 				return "MIPS_GP_VALUE";
992 			case DT_MIPS_AUX_DYNAMIC:
993 				return "MIPS_AUX_DYNAMIC";
994 			case DT_MIPS_PLTGOT:
995 				return "MIPS_PLTGOT";
996 			case DT_MIPS_RLD_OBJ_UPDATE:
997 				return "MIPS_RLD_OBJ_UPDATE";
998 			case DT_MIPS_RWPLT:
999 				return "MIPS_RWPLT";
1000 			default:
1001 				break;
1002 			}
1003 			break;
1004 		case EM_SPARC:
1005 		case EM_SPARC32PLUS:
1006 		case EM_SPARCV9:
1007 			switch (dtype) {
1008 			case DT_SPARC_REGISTER:
1009 				return "DT_SPARC_REGISTER";
1010 			default:
1011 				break;
1012 			}
1013 			break;
1014 		default:
1015 			break;
1016 		}
1017 	}
1018 
1019 	snprintf(s_dtype, sizeof(s_dtype), "<unknown: %#x>", dtype);
1020 	return (s_dtype);
1021 }
1022 
1023 static const char *
1024 st_bind(unsigned int sbind)
1025 {
1026 	static char s_sbind[32];
1027 
1028 	switch (sbind) {
1029 	case STB_LOCAL: return "LOCAL";
1030 	case STB_GLOBAL: return "GLOBAL";
1031 	case STB_WEAK: return "WEAK";
1032 	case STB_GNU_UNIQUE: return "UNIQUE";
1033 	default:
1034 		if (sbind >= STB_LOOS && sbind <= STB_HIOS)
1035 			return "OS";
1036 		else if (sbind >= STB_LOPROC && sbind <= STB_HIPROC)
1037 			return "PROC";
1038 		else
1039 			snprintf(s_sbind, sizeof(s_sbind), "<unknown: %#x>",
1040 			    sbind);
1041 		return (s_sbind);
1042 	}
1043 }
1044 
1045 static const char *
1046 st_type(unsigned int mach, unsigned int os, unsigned int stype)
1047 {
1048 	static char s_stype[32];
1049 
1050 	switch (stype) {
1051 	case STT_NOTYPE: return "NOTYPE";
1052 	case STT_OBJECT: return "OBJECT";
1053 	case STT_FUNC: return "FUNC";
1054 	case STT_SECTION: return "SECTION";
1055 	case STT_FILE: return "FILE";
1056 	case STT_COMMON: return "COMMON";
1057 	case STT_TLS: return "TLS";
1058 	default:
1059 		if (stype >= STT_LOOS && stype <= STT_HIOS) {
1060 			if ((os == ELFOSABI_GNU || os == ELFOSABI_FREEBSD) &&
1061 			    stype == STT_GNU_IFUNC)
1062 				return "IFUNC";
1063 			snprintf(s_stype, sizeof(s_stype), "OS+%#x",
1064 			    stype - STT_LOOS);
1065 		} else if (stype >= STT_LOPROC && stype <= STT_HIPROC) {
1066 			if (mach == EM_SPARCV9 && stype == STT_SPARC_REGISTER)
1067 				return "REGISTER";
1068 			snprintf(s_stype, sizeof(s_stype), "PROC+%#x",
1069 			    stype - STT_LOPROC);
1070 		} else
1071 			snprintf(s_stype, sizeof(s_stype), "<unknown: %#x>",
1072 			    stype);
1073 		return (s_stype);
1074 	}
1075 }
1076 
1077 static const char *
1078 st_vis(unsigned int svis)
1079 {
1080 	static char s_svis[32];
1081 
1082 	switch(svis) {
1083 	case STV_DEFAULT: return "DEFAULT";
1084 	case STV_INTERNAL: return "INTERNAL";
1085 	case STV_HIDDEN: return "HIDDEN";
1086 	case STV_PROTECTED: return "PROTECTED";
1087 	default:
1088 		snprintf(s_svis, sizeof(s_svis), "<unknown: %#x>", svis);
1089 		return (s_svis);
1090 	}
1091 }
1092 
1093 static const char *
1094 st_shndx(unsigned int shndx)
1095 {
1096 	static char s_shndx[32];
1097 
1098 	switch (shndx) {
1099 	case SHN_UNDEF: return "UND";
1100 	case SHN_ABS: return "ABS";
1101 	case SHN_COMMON: return "COM";
1102 	default:
1103 		if (shndx >= SHN_LOPROC && shndx <= SHN_HIPROC)
1104 			return "PRC";
1105 		else if (shndx >= SHN_LOOS && shndx <= SHN_HIOS)
1106 			return "OS";
1107 		else
1108 			snprintf(s_shndx, sizeof(s_shndx), "%u", shndx);
1109 		return (s_shndx);
1110 	}
1111 }
1112 
1113 static struct {
1114 	const char *ln;
1115 	char sn;
1116 	int value;
1117 } section_flag[] = {
1118 	{"WRITE", 'W', SHF_WRITE},
1119 	{"ALLOC", 'A', SHF_ALLOC},
1120 	{"EXEC", 'X', SHF_EXECINSTR},
1121 	{"MERGE", 'M', SHF_MERGE},
1122 	{"STRINGS", 'S', SHF_STRINGS},
1123 	{"INFO LINK", 'I', SHF_INFO_LINK},
1124 	{"OS NONCONF", 'O', SHF_OS_NONCONFORMING},
1125 	{"GROUP", 'G', SHF_GROUP},
1126 	{"TLS", 'T', SHF_TLS},
1127 	{"COMPRESSED", 'C', SHF_COMPRESSED},
1128 	{NULL, 0, 0}
1129 };
1130 
1131 static const char *
1132 note_type(const char *name, unsigned int et, unsigned int nt)
1133 {
1134 	if ((strcmp(name, "CORE") == 0 || strcmp(name, "LINUX") == 0) &&
1135 	    et == ET_CORE)
1136 		return note_type_linux_core(nt);
1137 	else if (strcmp(name, "FreeBSD") == 0)
1138 		if (et == ET_CORE)
1139 			return note_type_freebsd_core(nt);
1140 		else
1141 			return note_type_freebsd(nt);
1142 	else if (strcmp(name, "GNU") == 0 && et != ET_CORE)
1143 		return note_type_gnu(nt);
1144 	else if (strcmp(name, "NetBSD") == 0 && et != ET_CORE)
1145 		return note_type_netbsd(nt);
1146 	else if (strcmp(name, "OpenBSD") == 0 && et != ET_CORE)
1147 		return note_type_openbsd(nt);
1148 	else if (strcmp(name, "Xen") == 0 && et != ET_CORE)
1149 		return note_type_xen(nt);
1150 	return note_type_unknown(nt);
1151 }
1152 
1153 static const char *
1154 note_type_freebsd(unsigned int nt)
1155 {
1156 	switch (nt) {
1157 	case 1: return "NT_FREEBSD_ABI_TAG";
1158 	case 2: return "NT_FREEBSD_NOINIT_TAG";
1159 	case 3: return "NT_FREEBSD_ARCH_TAG";
1160 	case 4: return "NT_FREEBSD_FEATURE_CTL";
1161 	default: return (note_type_unknown(nt));
1162 	}
1163 }
1164 
1165 static const char *
1166 note_type_freebsd_core(unsigned int nt)
1167 {
1168 	switch (nt) {
1169 	case 1: return "NT_PRSTATUS";
1170 	case 2: return "NT_FPREGSET";
1171 	case 3: return "NT_PRPSINFO";
1172 	case 7: return "NT_THRMISC";
1173 	case 8: return "NT_PROCSTAT_PROC";
1174 	case 9: return "NT_PROCSTAT_FILES";
1175 	case 10: return "NT_PROCSTAT_VMMAP";
1176 	case 11: return "NT_PROCSTAT_GROUPS";
1177 	case 12: return "NT_PROCSTAT_UMASK";
1178 	case 13: return "NT_PROCSTAT_RLIMIT";
1179 	case 14: return "NT_PROCSTAT_OSREL";
1180 	case 15: return "NT_PROCSTAT_PSSTRINGS";
1181 	case 16: return "NT_PROCSTAT_AUXV";
1182 	case 17: return "NT_PTLWPINFO";
1183 	case 0x202: return "NT_X86_XSTATE (x86 XSAVE extended state)";
1184 	case 0x400: return "NT_ARM_VFP (arm VFP registers)";
1185 	default: return (note_type_unknown(nt));
1186 	}
1187 }
1188 
1189 static const char *
1190 note_type_linux_core(unsigned int nt)
1191 {
1192 	switch (nt) {
1193 	case 1: return "NT_PRSTATUS (Process status)";
1194 	case 2: return "NT_FPREGSET (Floating point information)";
1195 	case 3: return "NT_PRPSINFO (Process information)";
1196 	case 4: return "NT_TASKSTRUCT (Task structure)";
1197 	case 6: return "NT_AUXV (Auxiliary vector)";
1198 	case 10: return "NT_PSTATUS (Linux process status)";
1199 	case 12: return "NT_FPREGS (Linux floating point regset)";
1200 	case 13: return "NT_PSINFO (Linux process information)";
1201 	case 16: return "NT_LWPSTATUS (Linux lwpstatus_t type)";
1202 	case 17: return "NT_LWPSINFO (Linux lwpinfo_t type)";
1203 	case 18: return "NT_WIN32PSTATUS (win32_pstatus structure)";
1204 	case 0x100: return "NT_PPC_VMX (ppc Altivec registers)";
1205 	case 0x102: return "NT_PPC_VSX (ppc VSX registers)";
1206 	case 0x202: return "NT_X86_XSTATE (x86 XSAVE extended state)";
1207 	case 0x300: return "NT_S390_HIGH_GPRS (s390 upper register halves)";
1208 	case 0x301: return "NT_S390_TIMER (s390 timer register)";
1209 	case 0x302: return "NT_S390_TODCMP (s390 TOD comparator register)";
1210 	case 0x303: return "NT_S390_TODPREG (s390 TOD programmable register)";
1211 	case 0x304: return "NT_S390_CTRS (s390 control registers)";
1212 	case 0x305: return "NT_S390_PREFIX (s390 prefix register)";
1213 	case 0x400: return "NT_ARM_VFP (arm VFP registers)";
1214 	case 0x46494c45UL: return "NT_FILE (mapped files)";
1215 	case 0x46E62B7FUL: return "NT_PRXFPREG (Linux user_xfpregs structure)";
1216 	case 0x53494749UL: return "NT_SIGINFO (siginfo_t data)";
1217 	default: return (note_type_unknown(nt));
1218 	}
1219 }
1220 
1221 static const char *
1222 note_type_gnu(unsigned int nt)
1223 {
1224 	switch (nt) {
1225 	case 1: return "NT_GNU_ABI_TAG";
1226 	case 2: return "NT_GNU_HWCAP (Hardware capabilities)";
1227 	case 3: return "NT_GNU_BUILD_ID (Build id set by ld(1))";
1228 	case 4: return "NT_GNU_GOLD_VERSION (GNU gold version)";
1229 	case 5: return "NT_GNU_PROPERTY_TYPE_0";
1230 	default: return (note_type_unknown(nt));
1231 	}
1232 }
1233 
1234 static const char *
1235 note_type_netbsd(unsigned int nt)
1236 {
1237 	switch (nt) {
1238 	case 1: return "NT_NETBSD_IDENT";
1239 	default: return (note_type_unknown(nt));
1240 	}
1241 }
1242 
1243 static const char *
1244 note_type_openbsd(unsigned int nt)
1245 {
1246 	switch (nt) {
1247 	case 1: return "NT_OPENBSD_IDENT";
1248 	default: return (note_type_unknown(nt));
1249 	}
1250 }
1251 
1252 static const char *
1253 note_type_unknown(unsigned int nt)
1254 {
1255 	static char s_nt[32];
1256 
1257 	snprintf(s_nt, sizeof(s_nt),
1258 	    nt >= 0x100 ? "<unknown: 0x%x>" : "<unknown: %u>", nt);
1259 	return (s_nt);
1260 }
1261 
1262 static const char *
1263 note_type_xen(unsigned int nt)
1264 {
1265 	switch (nt) {
1266 	case 0: return "XEN_ELFNOTE_INFO";
1267 	case 1: return "XEN_ELFNOTE_ENTRY";
1268 	case 2: return "XEN_ELFNOTE_HYPERCALL_PAGE";
1269 	case 3: return "XEN_ELFNOTE_VIRT_BASE";
1270 	case 4: return "XEN_ELFNOTE_PADDR_OFFSET";
1271 	case 5: return "XEN_ELFNOTE_XEN_VERSION";
1272 	case 6: return "XEN_ELFNOTE_GUEST_OS";
1273 	case 7: return "XEN_ELFNOTE_GUEST_VERSION";
1274 	case 8: return "XEN_ELFNOTE_LOADER";
1275 	case 9: return "XEN_ELFNOTE_PAE_MODE";
1276 	case 10: return "XEN_ELFNOTE_FEATURES";
1277 	case 11: return "XEN_ELFNOTE_BSD_SYMTAB";
1278 	case 12: return "XEN_ELFNOTE_HV_START_LOW";
1279 	case 13: return "XEN_ELFNOTE_L1_MFN_VALID";
1280 	case 14: return "XEN_ELFNOTE_SUSPEND_CANCEL";
1281 	case 15: return "XEN_ELFNOTE_INIT_P2M";
1282 	case 16: return "XEN_ELFNOTE_MOD_START_PFN";
1283 	case 17: return "XEN_ELFNOTE_SUPPORTED_FEATURES";
1284 	default: return (note_type_unknown(nt));
1285 	}
1286 }
1287 
1288 static struct {
1289 	const char *name;
1290 	int value;
1291 } l_flag[] = {
1292 	{"EXACT_MATCH", LL_EXACT_MATCH},
1293 	{"IGNORE_INT_VER", LL_IGNORE_INT_VER},
1294 	{"REQUIRE_MINOR", LL_REQUIRE_MINOR},
1295 	{"EXPORTS", LL_EXPORTS},
1296 	{"DELAY_LOAD", LL_DELAY_LOAD},
1297 	{"DELTA", LL_DELTA},
1298 	{NULL, 0}
1299 };
1300 
1301 static struct mips_option mips_exceptions_option[] = {
1302 	{OEX_PAGE0, "PAGE0"},
1303 	{OEX_SMM, "SMM"},
1304 	{OEX_PRECISEFP, "PRECISEFP"},
1305 	{OEX_DISMISS, "DISMISS"},
1306 	{0, NULL}
1307 };
1308 
1309 static struct mips_option mips_pad_option[] = {
1310 	{OPAD_PREFIX, "PREFIX"},
1311 	{OPAD_POSTFIX, "POSTFIX"},
1312 	{OPAD_SYMBOL, "SYMBOL"},
1313 	{0, NULL}
1314 };
1315 
1316 static struct mips_option mips_hwpatch_option[] = {
1317 	{OHW_R4KEOP, "R4KEOP"},
1318 	{OHW_R8KPFETCH, "R8KPFETCH"},
1319 	{OHW_R5KEOP, "R5KEOP"},
1320 	{OHW_R5KCVTL, "R5KCVTL"},
1321 	{0, NULL}
1322 };
1323 
1324 static struct mips_option mips_hwa_option[] = {
1325 	{OHWA0_R4KEOP_CHECKED, "R4KEOP_CHECKED"},
1326 	{OHWA0_R4KEOP_CLEAN, "R4KEOP_CLEAN"},
1327 	{0, NULL}
1328 };
1329 
1330 static struct mips_option mips_hwo_option[] = {
1331 	{OHWO0_FIXADE, "FIXADE"},
1332 	{0, NULL}
1333 };
1334 
1335 static const char *
1336 option_kind(uint8_t kind)
1337 {
1338 	static char s_kind[32];
1339 
1340 	switch (kind) {
1341 	case ODK_NULL: return "NULL";
1342 	case ODK_REGINFO: return "REGINFO";
1343 	case ODK_EXCEPTIONS: return "EXCEPTIONS";
1344 	case ODK_PAD: return "PAD";
1345 	case ODK_HWPATCH: return "HWPATCH";
1346 	case ODK_FILL: return "FILL";
1347 	case ODK_TAGS: return "TAGS";
1348 	case ODK_HWAND: return "HWAND";
1349 	case ODK_HWOR: return "HWOR";
1350 	case ODK_GP_GROUP: return "GP_GROUP";
1351 	case ODK_IDENT: return "IDENT";
1352 	default:
1353 		snprintf(s_kind, sizeof(s_kind), "<unknown: %u>", kind);
1354 		return (s_kind);
1355 	}
1356 }
1357 
1358 static const char *
1359 top_tag(unsigned int tag)
1360 {
1361 	static char s_top_tag[32];
1362 
1363 	switch (tag) {
1364 	case 1: return "File Attributes";
1365 	case 2: return "Section Attributes";
1366 	case 3: return "Symbol Attributes";
1367 	default:
1368 		snprintf(s_top_tag, sizeof(s_top_tag), "Unknown tag: %u", tag);
1369 		return (s_top_tag);
1370 	}
1371 }
1372 
1373 static const char *
1374 aeabi_cpu_arch(uint64_t arch)
1375 {
1376 	static char s_cpu_arch[32];
1377 
1378 	switch (arch) {
1379 	case 0: return "Pre-V4";
1380 	case 1: return "ARM v4";
1381 	case 2: return "ARM v4T";
1382 	case 3: return "ARM v5T";
1383 	case 4: return "ARM v5TE";
1384 	case 5: return "ARM v5TEJ";
1385 	case 6: return "ARM v6";
1386 	case 7: return "ARM v6KZ";
1387 	case 8: return "ARM v6T2";
1388 	case 9: return "ARM v6K";
1389 	case 10: return "ARM v7";
1390 	case 11: return "ARM v6-M";
1391 	case 12: return "ARM v6S-M";
1392 	case 13: return "ARM v7E-M";
1393 	default:
1394 		snprintf(s_cpu_arch, sizeof(s_cpu_arch),
1395 		    "Unknown (%ju)", (uintmax_t) arch);
1396 		return (s_cpu_arch);
1397 	}
1398 }
1399 
1400 static const char *
1401 aeabi_cpu_arch_profile(uint64_t pf)
1402 {
1403 	static char s_arch_profile[32];
1404 
1405 	switch (pf) {
1406 	case 0:
1407 		return "Not applicable";
1408 	case 0x41:		/* 'A' */
1409 		return "Application Profile";
1410 	case 0x52:		/* 'R' */
1411 		return "Real-Time Profile";
1412 	case 0x4D:		/* 'M' */
1413 		return "Microcontroller Profile";
1414 	case 0x53:		/* 'S' */
1415 		return "Application or Real-Time Profile";
1416 	default:
1417 		snprintf(s_arch_profile, sizeof(s_arch_profile),
1418 		    "Unknown (%ju)\n", (uintmax_t) pf);
1419 		return (s_arch_profile);
1420 	}
1421 }
1422 
1423 static const char *
1424 aeabi_arm_isa(uint64_t ai)
1425 {
1426 	static char s_ai[32];
1427 
1428 	switch (ai) {
1429 	case 0: return "No";
1430 	case 1: return "Yes";
1431 	default:
1432 		snprintf(s_ai, sizeof(s_ai), "Unknown (%ju)\n",
1433 		    (uintmax_t) ai);
1434 		return (s_ai);
1435 	}
1436 }
1437 
1438 static const char *
1439 aeabi_thumb_isa(uint64_t ti)
1440 {
1441 	static char s_ti[32];
1442 
1443 	switch (ti) {
1444 	case 0: return "No";
1445 	case 1: return "16-bit Thumb";
1446 	case 2: return "32-bit Thumb";
1447 	default:
1448 		snprintf(s_ti, sizeof(s_ti), "Unknown (%ju)\n",
1449 		    (uintmax_t) ti);
1450 		return (s_ti);
1451 	}
1452 }
1453 
1454 static const char *
1455 aeabi_fp_arch(uint64_t fp)
1456 {
1457 	static char s_fp_arch[32];
1458 
1459 	switch (fp) {
1460 	case 0: return "No";
1461 	case 1: return "VFPv1";
1462 	case 2: return "VFPv2";
1463 	case 3: return "VFPv3";
1464 	case 4: return "VFPv3-D16";
1465 	case 5: return "VFPv4";
1466 	case 6: return "VFPv4-D16";
1467 	default:
1468 		snprintf(s_fp_arch, sizeof(s_fp_arch), "Unknown (%ju)",
1469 		    (uintmax_t) fp);
1470 		return (s_fp_arch);
1471 	}
1472 }
1473 
1474 static const char *
1475 aeabi_wmmx_arch(uint64_t wmmx)
1476 {
1477 	static char s_wmmx[32];
1478 
1479 	switch (wmmx) {
1480 	case 0: return "No";
1481 	case 1: return "WMMXv1";
1482 	case 2: return "WMMXv2";
1483 	default:
1484 		snprintf(s_wmmx, sizeof(s_wmmx), "Unknown (%ju)",
1485 		    (uintmax_t) wmmx);
1486 		return (s_wmmx);
1487 	}
1488 }
1489 
1490 static const char *
1491 aeabi_adv_simd_arch(uint64_t simd)
1492 {
1493 	static char s_simd[32];
1494 
1495 	switch (simd) {
1496 	case 0: return "No";
1497 	case 1: return "NEONv1";
1498 	case 2: return "NEONv2";
1499 	default:
1500 		snprintf(s_simd, sizeof(s_simd), "Unknown (%ju)",
1501 		    (uintmax_t) simd);
1502 		return (s_simd);
1503 	}
1504 }
1505 
1506 static const char *
1507 aeabi_pcs_config(uint64_t pcs)
1508 {
1509 	static char s_pcs[32];
1510 
1511 	switch (pcs) {
1512 	case 0: return "None";
1513 	case 1: return "Bare platform";
1514 	case 2: return "Linux";
1515 	case 3: return "Linux DSO";
1516 	case 4: return "Palm OS 2004";
1517 	case 5: return "Palm OS (future)";
1518 	case 6: return "Symbian OS 2004";
1519 	case 7: return "Symbian OS (future)";
1520 	default:
1521 		snprintf(s_pcs, sizeof(s_pcs), "Unknown (%ju)",
1522 		    (uintmax_t) pcs);
1523 		return (s_pcs);
1524 	}
1525 }
1526 
1527 static const char *
1528 aeabi_pcs_r9(uint64_t r9)
1529 {
1530 	static char s_r9[32];
1531 
1532 	switch (r9) {
1533 	case 0: return "V6";
1534 	case 1: return "SB";
1535 	case 2: return "TLS pointer";
1536 	case 3: return "Unused";
1537 	default:
1538 		snprintf(s_r9, sizeof(s_r9), "Unknown (%ju)", (uintmax_t) r9);
1539 		return (s_r9);
1540 	}
1541 }
1542 
1543 static const char *
1544 aeabi_pcs_rw(uint64_t rw)
1545 {
1546 	static char s_rw[32];
1547 
1548 	switch (rw) {
1549 	case 0: return "Absolute";
1550 	case 1: return "PC-relative";
1551 	case 2: return "SB-relative";
1552 	case 3: return "None";
1553 	default:
1554 		snprintf(s_rw, sizeof(s_rw), "Unknown (%ju)", (uintmax_t) rw);
1555 		return (s_rw);
1556 	}
1557 }
1558 
1559 static const char *
1560 aeabi_pcs_ro(uint64_t ro)
1561 {
1562 	static char s_ro[32];
1563 
1564 	switch (ro) {
1565 	case 0: return "Absolute";
1566 	case 1: return "PC-relative";
1567 	case 2: return "None";
1568 	default:
1569 		snprintf(s_ro, sizeof(s_ro), "Unknown (%ju)", (uintmax_t) ro);
1570 		return (s_ro);
1571 	}
1572 }
1573 
1574 static const char *
1575 aeabi_pcs_got(uint64_t got)
1576 {
1577 	static char s_got[32];
1578 
1579 	switch (got) {
1580 	case 0: return "None";
1581 	case 1: return "direct";
1582 	case 2: return "indirect via GOT";
1583 	default:
1584 		snprintf(s_got, sizeof(s_got), "Unknown (%ju)",
1585 		    (uintmax_t) got);
1586 		return (s_got);
1587 	}
1588 }
1589 
1590 static const char *
1591 aeabi_pcs_wchar_t(uint64_t wt)
1592 {
1593 	static char s_wt[32];
1594 
1595 	switch (wt) {
1596 	case 0: return "None";
1597 	case 2: return "wchar_t size 2";
1598 	case 4: return "wchar_t size 4";
1599 	default:
1600 		snprintf(s_wt, sizeof(s_wt), "Unknown (%ju)", (uintmax_t) wt);
1601 		return (s_wt);
1602 	}
1603 }
1604 
1605 static const char *
1606 aeabi_enum_size(uint64_t es)
1607 {
1608 	static char s_es[32];
1609 
1610 	switch (es) {
1611 	case 0: return "None";
1612 	case 1: return "smallest";
1613 	case 2: return "32-bit";
1614 	case 3: return "visible 32-bit";
1615 	default:
1616 		snprintf(s_es, sizeof(s_es), "Unknown (%ju)", (uintmax_t) es);
1617 		return (s_es);
1618 	}
1619 }
1620 
1621 static const char *
1622 aeabi_align_needed(uint64_t an)
1623 {
1624 	static char s_align_n[64];
1625 
1626 	switch (an) {
1627 	case 0: return "No";
1628 	case 1: return "8-byte align";
1629 	case 2: return "4-byte align";
1630 	case 3: return "Reserved";
1631 	default:
1632 		if (an >= 4 && an <= 12)
1633 			snprintf(s_align_n, sizeof(s_align_n), "8-byte align"
1634 			    " and up to 2^%ju-byte extended align",
1635 			    (uintmax_t) an);
1636 		else
1637 			snprintf(s_align_n, sizeof(s_align_n), "Unknown (%ju)",
1638 			    (uintmax_t) an);
1639 		return (s_align_n);
1640 	}
1641 }
1642 
1643 static const char *
1644 aeabi_align_preserved(uint64_t ap)
1645 {
1646 	static char s_align_p[128];
1647 
1648 	switch (ap) {
1649 	case 0: return "No";
1650 	case 1: return "8-byte align";
1651 	case 2: return "8-byte align and SP % 8 == 0";
1652 	case 3: return "Reserved";
1653 	default:
1654 		if (ap >= 4 && ap <= 12)
1655 			snprintf(s_align_p, sizeof(s_align_p), "8-byte align"
1656 			    " and SP %% 8 == 0 and up to 2^%ju-byte extended"
1657 			    " align", (uintmax_t) ap);
1658 		else
1659 			snprintf(s_align_p, sizeof(s_align_p), "Unknown (%ju)",
1660 			    (uintmax_t) ap);
1661 		return (s_align_p);
1662 	}
1663 }
1664 
1665 static const char *
1666 aeabi_fp_rounding(uint64_t fr)
1667 {
1668 	static char s_fp_r[32];
1669 
1670 	switch (fr) {
1671 	case 0: return "Unused";
1672 	case 1: return "Needed";
1673 	default:
1674 		snprintf(s_fp_r, sizeof(s_fp_r), "Unknown (%ju)",
1675 		    (uintmax_t) fr);
1676 		return (s_fp_r);
1677 	}
1678 }
1679 
1680 static const char *
1681 aeabi_fp_denormal(uint64_t fd)
1682 {
1683 	static char s_fp_d[32];
1684 
1685 	switch (fd) {
1686 	case 0: return "Unused";
1687 	case 1: return "Needed";
1688 	case 2: return "Sign Only";
1689 	default:
1690 		snprintf(s_fp_d, sizeof(s_fp_d), "Unknown (%ju)",
1691 		    (uintmax_t) fd);
1692 		return (s_fp_d);
1693 	}
1694 }
1695 
1696 static const char *
1697 aeabi_fp_exceptions(uint64_t fe)
1698 {
1699 	static char s_fp_e[32];
1700 
1701 	switch (fe) {
1702 	case 0: return "Unused";
1703 	case 1: return "Needed";
1704 	default:
1705 		snprintf(s_fp_e, sizeof(s_fp_e), "Unknown (%ju)",
1706 		    (uintmax_t) fe);
1707 		return (s_fp_e);
1708 	}
1709 }
1710 
1711 static const char *
1712 aeabi_fp_user_exceptions(uint64_t fu)
1713 {
1714 	static char s_fp_u[32];
1715 
1716 	switch (fu) {
1717 	case 0: return "Unused";
1718 	case 1: return "Needed";
1719 	default:
1720 		snprintf(s_fp_u, sizeof(s_fp_u), "Unknown (%ju)",
1721 		    (uintmax_t) fu);
1722 		return (s_fp_u);
1723 	}
1724 }
1725 
1726 static const char *
1727 aeabi_fp_number_model(uint64_t fn)
1728 {
1729 	static char s_fp_n[32];
1730 
1731 	switch (fn) {
1732 	case 0: return "Unused";
1733 	case 1: return "IEEE 754 normal";
1734 	case 2: return "RTABI";
1735 	case 3: return "IEEE 754";
1736 	default:
1737 		snprintf(s_fp_n, sizeof(s_fp_n), "Unknown (%ju)",
1738 		    (uintmax_t) fn);
1739 		return (s_fp_n);
1740 	}
1741 }
1742 
1743 static const char *
1744 aeabi_fp_16bit_format(uint64_t fp16)
1745 {
1746 	static char s_fp_16[64];
1747 
1748 	switch (fp16) {
1749 	case 0: return "None";
1750 	case 1: return "IEEE 754";
1751 	case 2: return "VFPv3/Advanced SIMD (alternative format)";
1752 	default:
1753 		snprintf(s_fp_16, sizeof(s_fp_16), "Unknown (%ju)",
1754 		    (uintmax_t) fp16);
1755 		return (s_fp_16);
1756 	}
1757 }
1758 
1759 static const char *
1760 aeabi_mpext(uint64_t mp)
1761 {
1762 	static char s_mp[32];
1763 
1764 	switch (mp) {
1765 	case 0: return "Not allowed";
1766 	case 1: return "Allowed";
1767 	default:
1768 		snprintf(s_mp, sizeof(s_mp), "Unknown (%ju)",
1769 		    (uintmax_t) mp);
1770 		return (s_mp);
1771 	}
1772 }
1773 
1774 static const char *
1775 aeabi_div(uint64_t du)
1776 {
1777 	static char s_du[32];
1778 
1779 	switch (du) {
1780 	case 0: return "Yes (V7-R/V7-M)";
1781 	case 1: return "No";
1782 	case 2: return "Yes (V7-A)";
1783 	default:
1784 		snprintf(s_du, sizeof(s_du), "Unknown (%ju)",
1785 		    (uintmax_t) du);
1786 		return (s_du);
1787 	}
1788 }
1789 
1790 static const char *
1791 aeabi_t2ee(uint64_t t2ee)
1792 {
1793 	static char s_t2ee[32];
1794 
1795 	switch (t2ee) {
1796 	case 0: return "Not allowed";
1797 	case 1: return "Allowed";
1798 	default:
1799 		snprintf(s_t2ee, sizeof(s_t2ee), "Unknown(%ju)",
1800 		    (uintmax_t) t2ee);
1801 		return (s_t2ee);
1802 	}
1803 
1804 }
1805 
1806 static const char *
1807 aeabi_hardfp(uint64_t hfp)
1808 {
1809 	static char s_hfp[32];
1810 
1811 	switch (hfp) {
1812 	case 0: return "Tag_FP_arch";
1813 	case 1: return "only SP";
1814 	case 2: return "only DP";
1815 	case 3: return "both SP and DP";
1816 	default:
1817 		snprintf(s_hfp, sizeof(s_hfp), "Unknown (%ju)",
1818 		    (uintmax_t) hfp);
1819 		return (s_hfp);
1820 	}
1821 }
1822 
1823 static const char *
1824 aeabi_vfp_args(uint64_t va)
1825 {
1826 	static char s_va[32];
1827 
1828 	switch (va) {
1829 	case 0: return "AAPCS (base variant)";
1830 	case 1: return "AAPCS (VFP variant)";
1831 	case 2: return "toolchain-specific";
1832 	default:
1833 		snprintf(s_va, sizeof(s_va), "Unknown (%ju)", (uintmax_t) va);
1834 		return (s_va);
1835 	}
1836 }
1837 
1838 static const char *
1839 aeabi_wmmx_args(uint64_t wa)
1840 {
1841 	static char s_wa[32];
1842 
1843 	switch (wa) {
1844 	case 0: return "AAPCS (base variant)";
1845 	case 1: return "Intel WMMX";
1846 	case 2: return "toolchain-specific";
1847 	default:
1848 		snprintf(s_wa, sizeof(s_wa), "Unknown(%ju)", (uintmax_t) wa);
1849 		return (s_wa);
1850 	}
1851 }
1852 
1853 static const char *
1854 aeabi_unaligned_access(uint64_t ua)
1855 {
1856 	static char s_ua[32];
1857 
1858 	switch (ua) {
1859 	case 0: return "Not allowed";
1860 	case 1: return "Allowed";
1861 	default:
1862 		snprintf(s_ua, sizeof(s_ua), "Unknown(%ju)", (uintmax_t) ua);
1863 		return (s_ua);
1864 	}
1865 }
1866 
1867 static const char *
1868 aeabi_fp_hpext(uint64_t fh)
1869 {
1870 	static char s_fh[32];
1871 
1872 	switch (fh) {
1873 	case 0: return "Not allowed";
1874 	case 1: return "Allowed";
1875 	default:
1876 		snprintf(s_fh, sizeof(s_fh), "Unknown(%ju)", (uintmax_t) fh);
1877 		return (s_fh);
1878 	}
1879 }
1880 
1881 static const char *
1882 aeabi_optm_goal(uint64_t og)
1883 {
1884 	static char s_og[32];
1885 
1886 	switch (og) {
1887 	case 0: return "None";
1888 	case 1: return "Speed";
1889 	case 2: return "Speed aggressive";
1890 	case 3: return "Space";
1891 	case 4: return "Space aggressive";
1892 	case 5: return "Debugging";
1893 	case 6: return "Best Debugging";
1894 	default:
1895 		snprintf(s_og, sizeof(s_og), "Unknown(%ju)", (uintmax_t) og);
1896 		return (s_og);
1897 	}
1898 }
1899 
1900 static const char *
1901 aeabi_fp_optm_goal(uint64_t fog)
1902 {
1903 	static char s_fog[32];
1904 
1905 	switch (fog) {
1906 	case 0: return "None";
1907 	case 1: return "Speed";
1908 	case 2: return "Speed aggressive";
1909 	case 3: return "Space";
1910 	case 4: return "Space aggressive";
1911 	case 5: return "Accurary";
1912 	case 6: return "Best Accurary";
1913 	default:
1914 		snprintf(s_fog, sizeof(s_fog), "Unknown(%ju)",
1915 		    (uintmax_t) fog);
1916 		return (s_fog);
1917 	}
1918 }
1919 
1920 static const char *
1921 aeabi_virtual(uint64_t vt)
1922 {
1923 	static char s_virtual[64];
1924 
1925 	switch (vt) {
1926 	case 0: return "No";
1927 	case 1: return "TrustZone";
1928 	case 2: return "Virtualization extension";
1929 	case 3: return "TrustZone and virtualization extension";
1930 	default:
1931 		snprintf(s_virtual, sizeof(s_virtual), "Unknown(%ju)",
1932 		    (uintmax_t) vt);
1933 		return (s_virtual);
1934 	}
1935 }
1936 
1937 static struct {
1938 	uint64_t tag;
1939 	const char *s_tag;
1940 	const char *(*get_desc)(uint64_t val);
1941 } aeabi_tags[] = {
1942 	{4, "Tag_CPU_raw_name", NULL},
1943 	{5, "Tag_CPU_name", NULL},
1944 	{6, "Tag_CPU_arch", aeabi_cpu_arch},
1945 	{7, "Tag_CPU_arch_profile", aeabi_cpu_arch_profile},
1946 	{8, "Tag_ARM_ISA_use", aeabi_arm_isa},
1947 	{9, "Tag_THUMB_ISA_use", aeabi_thumb_isa},
1948 	{10, "Tag_FP_arch", aeabi_fp_arch},
1949 	{11, "Tag_WMMX_arch", aeabi_wmmx_arch},
1950 	{12, "Tag_Advanced_SIMD_arch", aeabi_adv_simd_arch},
1951 	{13, "Tag_PCS_config", aeabi_pcs_config},
1952 	{14, "Tag_ABI_PCS_R9_use", aeabi_pcs_r9},
1953 	{15, "Tag_ABI_PCS_RW_data", aeabi_pcs_rw},
1954 	{16, "Tag_ABI_PCS_RO_data", aeabi_pcs_ro},
1955 	{17, "Tag_ABI_PCS_GOT_use", aeabi_pcs_got},
1956 	{18, "Tag_ABI_PCS_wchar_t", aeabi_pcs_wchar_t},
1957 	{19, "Tag_ABI_FP_rounding", aeabi_fp_rounding},
1958 	{20, "Tag_ABI_FP_denormal", aeabi_fp_denormal},
1959 	{21, "Tag_ABI_FP_exceptions", aeabi_fp_exceptions},
1960 	{22, "Tag_ABI_FP_user_exceptions", aeabi_fp_user_exceptions},
1961 	{23, "Tag_ABI_FP_number_model", aeabi_fp_number_model},
1962 	{24, "Tag_ABI_align_needed", aeabi_align_needed},
1963 	{25, "Tag_ABI_align_preserved", aeabi_align_preserved},
1964 	{26, "Tag_ABI_enum_size", aeabi_enum_size},
1965 	{27, "Tag_ABI_HardFP_use", aeabi_hardfp},
1966 	{28, "Tag_ABI_VFP_args", aeabi_vfp_args},
1967 	{29, "Tag_ABI_WMMX_args", aeabi_wmmx_args},
1968 	{30, "Tag_ABI_optimization_goals", aeabi_optm_goal},
1969 	{31, "Tag_ABI_FP_optimization_goals", aeabi_fp_optm_goal},
1970 	{32, "Tag_compatibility", NULL},
1971 	{34, "Tag_CPU_unaligned_access", aeabi_unaligned_access},
1972 	{36, "Tag_FP_HP_extension", aeabi_fp_hpext},
1973 	{38, "Tag_ABI_FP_16bit_format", aeabi_fp_16bit_format},
1974 	{42, "Tag_MPextension_use", aeabi_mpext},
1975 	{44, "Tag_DIV_use", aeabi_div},
1976 	{64, "Tag_nodefaults", NULL},
1977 	{65, "Tag_also_compatible_with", NULL},
1978 	{66, "Tag_T2EE_use", aeabi_t2ee},
1979 	{67, "Tag_conformance", NULL},
1980 	{68, "Tag_Virtualization_use", aeabi_virtual},
1981 	{70, "Tag_MPextension_use", aeabi_mpext},
1982 };
1983 
1984 static const char *
1985 mips_abi_fp(uint64_t fp)
1986 {
1987 	static char s_mips_abi_fp[64];
1988 
1989 	switch (fp) {
1990 	case 0: return "N/A";
1991 	case 1: return "Hard float (double precision)";
1992 	case 2: return "Hard float (single precision)";
1993 	case 3: return "Soft float";
1994 	case 4: return "64-bit float (-mips32r2 -mfp64)";
1995 	default:
1996 		snprintf(s_mips_abi_fp, sizeof(s_mips_abi_fp), "Unknown(%ju)",
1997 		    (uintmax_t) fp);
1998 		return (s_mips_abi_fp);
1999 	}
2000 }
2001 
2002 static const char *
2003 ppc_abi_fp(uint64_t fp)
2004 {
2005 	static char s_ppc_abi_fp[64];
2006 
2007 	switch (fp) {
2008 	case 0: return "N/A";
2009 	case 1: return "Hard float (double precision)";
2010 	case 2: return "Soft float";
2011 	case 3: return "Hard float (single precision)";
2012 	default:
2013 		snprintf(s_ppc_abi_fp, sizeof(s_ppc_abi_fp), "Unknown(%ju)",
2014 		    (uintmax_t) fp);
2015 		return (s_ppc_abi_fp);
2016 	}
2017 }
2018 
2019 static const char *
2020 ppc_abi_vector(uint64_t vec)
2021 {
2022 	static char s_vec[64];
2023 
2024 	switch (vec) {
2025 	case 0: return "N/A";
2026 	case 1: return "Generic purpose registers";
2027 	case 2: return "AltiVec registers";
2028 	case 3: return "SPE registers";
2029 	default:
2030 		snprintf(s_vec, sizeof(s_vec), "Unknown(%ju)", (uintmax_t) vec);
2031 		return (s_vec);
2032 	}
2033 }
2034 
2035 static const char *
2036 dwarf_reg(unsigned int mach, unsigned int reg)
2037 {
2038 
2039 	switch (mach) {
2040 	case EM_386:
2041 	case EM_IAMCU:
2042 		switch (reg) {
2043 		case 0: return "eax";
2044 		case 1: return "ecx";
2045 		case 2: return "edx";
2046 		case 3: return "ebx";
2047 		case 4: return "esp";
2048 		case 5: return "ebp";
2049 		case 6: return "esi";
2050 		case 7: return "edi";
2051 		case 8: return "eip";
2052 		case 9: return "eflags";
2053 		case 11: return "st0";
2054 		case 12: return "st1";
2055 		case 13: return "st2";
2056 		case 14: return "st3";
2057 		case 15: return "st4";
2058 		case 16: return "st5";
2059 		case 17: return "st6";
2060 		case 18: return "st7";
2061 		case 21: return "xmm0";
2062 		case 22: return "xmm1";
2063 		case 23: return "xmm2";
2064 		case 24: return "xmm3";
2065 		case 25: return "xmm4";
2066 		case 26: return "xmm5";
2067 		case 27: return "xmm6";
2068 		case 28: return "xmm7";
2069 		case 29: return "mm0";
2070 		case 30: return "mm1";
2071 		case 31: return "mm2";
2072 		case 32: return "mm3";
2073 		case 33: return "mm4";
2074 		case 34: return "mm5";
2075 		case 35: return "mm6";
2076 		case 36: return "mm7";
2077 		case 37: return "fcw";
2078 		case 38: return "fsw";
2079 		case 39: return "mxcsr";
2080 		case 40: return "es";
2081 		case 41: return "cs";
2082 		case 42: return "ss";
2083 		case 43: return "ds";
2084 		case 44: return "fs";
2085 		case 45: return "gs";
2086 		case 48: return "tr";
2087 		case 49: return "ldtr";
2088 		default: return (NULL);
2089 		}
2090 	case EM_RISCV:
2091 		switch (reg) {
2092 		case 0: return "zero";
2093 		case 1: return "ra";
2094 		case 2: return "sp";
2095 		case 3: return "gp";
2096 		case 4: return "tp";
2097 		case 5: return "t0";
2098 		case 6: return "t1";
2099 		case 7: return "t2";
2100 		case 8: return "s0";
2101 		case 9: return "s1";
2102 		case 10: return "a0";
2103 		case 11: return "a1";
2104 		case 12: return "a2";
2105 		case 13: return "a3";
2106 		case 14: return "a4";
2107 		case 15: return "a5";
2108 		case 16: return "a6";
2109 		case 17: return "a7";
2110 		case 18: return "s2";
2111 		case 19: return "s3";
2112 		case 20: return "s4";
2113 		case 21: return "s5";
2114 		case 22: return "s6";
2115 		case 23: return "s7";
2116 		case 24: return "s8";
2117 		case 25: return "s9";
2118 		case 26: return "s10";
2119 		case 27: return "s11";
2120 		case 28: return "t3";
2121 		case 29: return "t4";
2122 		case 30: return "t5";
2123 		case 31: return "t6";
2124 		case 32: return "ft0";
2125 		case 33: return "ft1";
2126 		case 34: return "ft2";
2127 		case 35: return "ft3";
2128 		case 36: return "ft4";
2129 		case 37: return "ft5";
2130 		case 38: return "ft6";
2131 		case 39: return "ft7";
2132 		case 40: return "fs0";
2133 		case 41: return "fs1";
2134 		case 42: return "fa0";
2135 		case 43: return "fa1";
2136 		case 44: return "fa2";
2137 		case 45: return "fa3";
2138 		case 46: return "fa4";
2139 		case 47: return "fa5";
2140 		case 48: return "fa6";
2141 		case 49: return "fa7";
2142 		case 50: return "fs2";
2143 		case 51: return "fs3";
2144 		case 52: return "fs4";
2145 		case 53: return "fs5";
2146 		case 54: return "fs6";
2147 		case 55: return "fs7";
2148 		case 56: return "fs8";
2149 		case 57: return "fs9";
2150 		case 58: return "fs10";
2151 		case 59: return "fs11";
2152 		case 60: return "ft8";
2153 		case 61: return "ft9";
2154 		case 62: return "ft10";
2155 		case 63: return "ft11";
2156 		default: return (NULL);
2157 		}
2158 	case EM_X86_64:
2159 		switch (reg) {
2160 		case 0: return "rax";
2161 		case 1: return "rdx";
2162 		case 2: return "rcx";
2163 		case 3: return "rbx";
2164 		case 4: return "rsi";
2165 		case 5: return "rdi";
2166 		case 6: return "rbp";
2167 		case 7: return "rsp";
2168 		case 16: return "rip";
2169 		case 17: return "xmm0";
2170 		case 18: return "xmm1";
2171 		case 19: return "xmm2";
2172 		case 20: return "xmm3";
2173 		case 21: return "xmm4";
2174 		case 22: return "xmm5";
2175 		case 23: return "xmm6";
2176 		case 24: return "xmm7";
2177 		case 25: return "xmm8";
2178 		case 26: return "xmm9";
2179 		case 27: return "xmm10";
2180 		case 28: return "xmm11";
2181 		case 29: return "xmm12";
2182 		case 30: return "xmm13";
2183 		case 31: return "xmm14";
2184 		case 32: return "xmm15";
2185 		case 33: return "st0";
2186 		case 34: return "st1";
2187 		case 35: return "st2";
2188 		case 36: return "st3";
2189 		case 37: return "st4";
2190 		case 38: return "st5";
2191 		case 39: return "st6";
2192 		case 40: return "st7";
2193 		case 41: return "mm0";
2194 		case 42: return "mm1";
2195 		case 43: return "mm2";
2196 		case 44: return "mm3";
2197 		case 45: return "mm4";
2198 		case 46: return "mm5";
2199 		case 47: return "mm6";
2200 		case 48: return "mm7";
2201 		case 49: return "rflags";
2202 		case 50: return "es";
2203 		case 51: return "cs";
2204 		case 52: return "ss";
2205 		case 53: return "ds";
2206 		case 54: return "fs";
2207 		case 55: return "gs";
2208 		case 58: return "fs.base";
2209 		case 59: return "gs.base";
2210 		case 62: return "tr";
2211 		case 63: return "ldtr";
2212 		case 64: return "mxcsr";
2213 		case 65: return "fcw";
2214 		case 66: return "fsw";
2215 		default: return (NULL);
2216 		}
2217 	default:
2218 		return (NULL);
2219 	}
2220 }
2221 
2222 static void
2223 dump_ehdr(struct readelf *re)
2224 {
2225 	size_t		 phnum, shnum, shstrndx;
2226 	int		 i;
2227 
2228 	printf("ELF Header:\n");
2229 
2230 	/* e_ident[]. */
2231 	printf("  Magic:   ");
2232 	for (i = 0; i < EI_NIDENT; i++)
2233 		printf("%.2x ", re->ehdr.e_ident[i]);
2234 	putchar('\n');
2235 
2236 	/* EI_CLASS. */
2237 	printf("%-37s%s\n", "  Class:", elf_class(re->ehdr.e_ident[EI_CLASS]));
2238 
2239 	/* EI_DATA. */
2240 	printf("%-37s%s\n", "  Data:", elf_endian(re->ehdr.e_ident[EI_DATA]));
2241 
2242 	/* EI_VERSION. */
2243 	printf("%-37s%d %s\n", "  Version:", re->ehdr.e_ident[EI_VERSION],
2244 	    elf_ver(re->ehdr.e_ident[EI_VERSION]));
2245 
2246 	/* EI_OSABI. */
2247 	printf("%-37s%s\n", "  OS/ABI:", elf_osabi(re->ehdr.e_ident[EI_OSABI]));
2248 
2249 	/* EI_ABIVERSION. */
2250 	printf("%-37s%d\n", "  ABI Version:", re->ehdr.e_ident[EI_ABIVERSION]);
2251 
2252 	/* e_type. */
2253 	printf("%-37s%s\n", "  Type:", elf_type(re->ehdr.e_type));
2254 
2255 	/* e_machine. */
2256 	printf("%-37s%s\n", "  Machine:", elf_machine(re->ehdr.e_machine));
2257 
2258 	/* e_version. */
2259 	printf("%-37s%#x\n", "  Version:", re->ehdr.e_version);
2260 
2261 	/* e_entry. */
2262 	printf("%-37s%#jx\n", "  Entry point address:",
2263 	    (uintmax_t)re->ehdr.e_entry);
2264 
2265 	/* e_phoff. */
2266 	printf("%-37s%ju (bytes into file)\n", "  Start of program headers:",
2267 	    (uintmax_t)re->ehdr.e_phoff);
2268 
2269 	/* e_shoff. */
2270 	printf("%-37s%ju (bytes into file)\n", "  Start of section headers:",
2271 	    (uintmax_t)re->ehdr.e_shoff);
2272 
2273 	/* e_flags. */
2274 	printf("%-37s%#x", "  Flags:", re->ehdr.e_flags);
2275 	dump_eflags(re, re->ehdr.e_flags);
2276 	putchar('\n');
2277 
2278 	/* e_ehsize. */
2279 	printf("%-37s%u (bytes)\n", "  Size of this header:",
2280 	    re->ehdr.e_ehsize);
2281 
2282 	/* e_phentsize. */
2283 	printf("%-37s%u (bytes)\n", "  Size of program headers:",
2284 	    re->ehdr.e_phentsize);
2285 
2286 	/* e_phnum. */
2287 	printf("%-37s%u", "  Number of program headers:", re->ehdr.e_phnum);
2288 	if (re->ehdr.e_phnum == PN_XNUM) {
2289 		/* Extended program header numbering is in use. */
2290 		if (elf_getphnum(re->elf, &phnum))
2291 			printf(" (%zu)", phnum);
2292 	}
2293 	putchar('\n');
2294 
2295 	/* e_shentsize. */
2296 	printf("%-37s%u (bytes)\n", "  Size of section headers:",
2297 	    re->ehdr.e_shentsize);
2298 
2299 	/* e_shnum. */
2300 	printf("%-37s%u", "  Number of section headers:", re->ehdr.e_shnum);
2301 	if (re->ehdr.e_shnum == SHN_UNDEF) {
2302 		/* Extended section numbering is in use. */
2303 		if (elf_getshnum(re->elf, &shnum))
2304 			printf(" (%ju)", (uintmax_t)shnum);
2305 	}
2306 	putchar('\n');
2307 
2308 	/* e_shstrndx. */
2309 	printf("%-37s%u", "  Section header string table index:",
2310 	    re->ehdr.e_shstrndx);
2311 	if (re->ehdr.e_shstrndx == SHN_XINDEX) {
2312 		/* Extended section numbering is in use. */
2313 		if (elf_getshstrndx(re->elf, &shstrndx))
2314 			printf(" (%ju)", (uintmax_t)shstrndx);
2315 	}
2316 	putchar('\n');
2317 }
2318 
2319 static void
2320 dump_eflags(struct readelf *re, uint64_t e_flags)
2321 {
2322 	struct eflags_desc *edesc;
2323 	int arm_eabi;
2324 
2325 	edesc = NULL;
2326 	switch (re->ehdr.e_machine) {
2327 	case EM_ARM:
2328 		arm_eabi = (e_flags & EF_ARM_EABIMASK) >> 24;
2329 		if (arm_eabi == 0)
2330 			printf(", GNU EABI");
2331 		else if (arm_eabi <= 5)
2332 			printf(", Version%d EABI", arm_eabi);
2333 		edesc = arm_eflags_desc;
2334 		break;
2335 	case EM_MIPS:
2336 	case EM_MIPS_RS3_LE:
2337 		switch ((e_flags & EF_MIPS_ARCH) >> 28) {
2338 		case 0:	printf(", mips1"); break;
2339 		case 1: printf(", mips2"); break;
2340 		case 2: printf(", mips3"); break;
2341 		case 3: printf(", mips4"); break;
2342 		case 4: printf(", mips5"); break;
2343 		case 5: printf(", mips32"); break;
2344 		case 6: printf(", mips64"); break;
2345 		case 7: printf(", mips32r2"); break;
2346 		case 8: printf(", mips64r2"); break;
2347 		default: break;
2348 		}
2349 		switch ((e_flags & 0x00FF0000) >> 16) {
2350 		case 0x81: printf(", 3900"); break;
2351 		case 0x82: printf(", 4010"); break;
2352 		case 0x83: printf(", 4100"); break;
2353 		case 0x85: printf(", 4650"); break;
2354 		case 0x87: printf(", 4120"); break;
2355 		case 0x88: printf(", 4111"); break;
2356 		case 0x8a: printf(", sb1"); break;
2357 		case 0x8b: printf(", octeon"); break;
2358 		case 0x8c: printf(", xlr"); break;
2359 		case 0x91: printf(", 5400"); break;
2360 		case 0x98: printf(", 5500"); break;
2361 		case 0x99: printf(", 9000"); break;
2362 		case 0xa0: printf(", loongson-2e"); break;
2363 		case 0xa1: printf(", loongson-2f"); break;
2364 		default: break;
2365 		}
2366 		switch ((e_flags & 0x0000F000) >> 12) {
2367 		case 1: printf(", o32"); break;
2368 		case 2: printf(", o64"); break;
2369 		case 3: printf(", eabi32"); break;
2370 		case 4: printf(", eabi64"); break;
2371 		default: break;
2372 		}
2373 		edesc = mips_eflags_desc;
2374 		break;
2375 	case EM_PPC64:
2376 		switch (e_flags) {
2377 		case 0: printf(", Unspecified or Power ELF V1 ABI"); break;
2378 		case 1: printf(", Power ELF V1 ABI"); break;
2379 		case 2: printf(", OpenPOWER ELF V2 ABI"); break;
2380 		default: break;
2381 		}
2382 		/* explicit fall through*/
2383 	case EM_PPC:
2384 		edesc = powerpc_eflags_desc;
2385 		break;
2386 	case EM_RISCV:
2387 		switch (e_flags & EF_RISCV_FLOAT_ABI_MASK) {
2388 		case EF_RISCV_FLOAT_ABI_SOFT:
2389 			printf(", soft-float ABI");
2390 			break;
2391 		case EF_RISCV_FLOAT_ABI_SINGLE:
2392 			printf(", single-float ABI");
2393 			break;
2394 		case EF_RISCV_FLOAT_ABI_DOUBLE:
2395 			printf(", double-float ABI");
2396 			break;
2397 		case EF_RISCV_FLOAT_ABI_QUAD:
2398 			printf(", quad-float ABI");
2399 			break;
2400 		}
2401 		edesc = riscv_eflags_desc;
2402 		break;
2403 	case EM_SPARC:
2404 	case EM_SPARC32PLUS:
2405 	case EM_SPARCV9:
2406 		switch ((e_flags & EF_SPARCV9_MM)) {
2407 		case EF_SPARCV9_TSO: printf(", tso"); break;
2408 		case EF_SPARCV9_PSO: printf(", pso"); break;
2409 		case EF_SPARCV9_MM: printf(", rmo"); break;
2410 		default: break;
2411 		}
2412 		edesc = sparc_eflags_desc;
2413 		break;
2414 	default:
2415 		break;
2416 	}
2417 
2418 	if (edesc != NULL) {
2419 		while (edesc->desc != NULL) {
2420 			if (e_flags & edesc->flag)
2421 				printf(", %s", edesc->desc);
2422 			edesc++;
2423 		}
2424 	}
2425 }
2426 
2427 static void
2428 dump_phdr(struct readelf *re)
2429 {
2430 	const char	*rawfile;
2431 	GElf_Phdr	 phdr;
2432 	size_t		 phnum, size;
2433 	int		 i, j;
2434 
2435 #define	PH_HDR	"Type", "Offset", "VirtAddr", "PhysAddr", "FileSiz",	\
2436 		"MemSiz", "Flg", "Align"
2437 #define	PH_CT	phdr_type(re->ehdr.e_machine, phdr.p_type),		\
2438 		(uintmax_t)phdr.p_offset, (uintmax_t)phdr.p_vaddr,	\
2439 		(uintmax_t)phdr.p_paddr, (uintmax_t)phdr.p_filesz,	\
2440 		(uintmax_t)phdr.p_memsz,				\
2441 		phdr.p_flags & PF_R ? 'R' : ' ',			\
2442 		phdr.p_flags & PF_W ? 'W' : ' ',			\
2443 		phdr.p_flags & PF_X ? 'E' : ' ',			\
2444 		(uintmax_t)phdr.p_align
2445 
2446 	if (elf_getphnum(re->elf, &phnum) == 0) {
2447 		warnx("elf_getphnum failed: %s", elf_errmsg(-1));
2448 		return;
2449 	}
2450 	if (phnum == 0) {
2451 		printf("\nThere are no program headers in this file.\n");
2452 		return;
2453 	}
2454 
2455 	printf("\nElf file type is %s", elf_type(re->ehdr.e_type));
2456 	printf("\nEntry point 0x%jx\n", (uintmax_t)re->ehdr.e_entry);
2457 	printf("There are %ju program headers, starting at offset %ju\n",
2458 	    (uintmax_t)phnum, (uintmax_t)re->ehdr.e_phoff);
2459 
2460 	/* Dump program headers. */
2461 	printf("\nProgram Headers:\n");
2462 	if (re->ec == ELFCLASS32)
2463 		printf("  %-15s%-9s%-11s%-11s%-8s%-8s%-4s%s\n", PH_HDR);
2464 	else if (re->options & RE_WW)
2465 		printf("  %-15s%-9s%-19s%-19s%-9s%-9s%-4s%s\n", PH_HDR);
2466 	else
2467 		printf("  %-15s%-19s%-19s%s\n                 %-19s%-20s"
2468 		    "%-7s%s\n", PH_HDR);
2469 	for (i = 0; (size_t) i < phnum; i++) {
2470 		if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
2471 			warnx("gelf_getphdr failed: %s", elf_errmsg(-1));
2472 			continue;
2473 		}
2474 		/* TODO: Add arch-specific segment type dump. */
2475 		if (re->ec == ELFCLASS32)
2476 			printf("  %-14.14s 0x%6.6jx 0x%8.8jx 0x%8.8jx "
2477 			    "0x%5.5jx 0x%5.5jx %c%c%c %#jx\n", PH_CT);
2478 		else if (re->options & RE_WW)
2479 			printf("  %-14.14s 0x%6.6jx 0x%16.16jx 0x%16.16jx "
2480 			    "0x%6.6jx 0x%6.6jx %c%c%c %#jx\n", PH_CT);
2481 		else
2482 			printf("  %-14.14s 0x%16.16jx 0x%16.16jx 0x%16.16jx\n"
2483 			    "                 0x%16.16jx 0x%16.16jx  %c%c%c"
2484 			    "    %#jx\n", PH_CT);
2485 		if (phdr.p_type == PT_INTERP) {
2486 			if ((rawfile = elf_rawfile(re->elf, &size)) == NULL) {
2487 				warnx("elf_rawfile failed: %s", elf_errmsg(-1));
2488 				continue;
2489 			}
2490 			if (phdr.p_offset >= size) {
2491 				warnx("invalid program header offset");
2492 				continue;
2493 			}
2494 			printf("      [Requesting program interpreter: %s]\n",
2495 				rawfile + phdr.p_offset);
2496 		}
2497 	}
2498 
2499 	/* Dump section to segment mapping. */
2500 	if (re->shnum == 0)
2501 		return;
2502 	printf("\n Section to Segment mapping:\n");
2503 	printf("  Segment Sections...\n");
2504 	for (i = 0; (size_t)i < phnum; i++) {
2505 		if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
2506 			warnx("gelf_getphdr failed: %s", elf_errmsg(-1));
2507 			continue;
2508 		}
2509 		printf("   %2.2d     ", i);
2510 		/* skip NULL section. */
2511 		for (j = 1; (size_t)j < re->shnum; j++) {
2512 			if (re->sl[j].off < phdr.p_offset)
2513 				continue;
2514 			if (re->sl[j].off + re->sl[j].sz >
2515 			    phdr.p_offset + phdr.p_filesz &&
2516 			    re->sl[j].type != SHT_NOBITS)
2517 				continue;
2518 			if (re->sl[j].addr < phdr.p_vaddr ||
2519 			    re->sl[j].addr + re->sl[j].sz >
2520 			    phdr.p_vaddr + phdr.p_memsz)
2521 				continue;
2522 			if (phdr.p_type == PT_TLS &&
2523 			    (re->sl[j].flags & SHF_TLS) == 0)
2524 				continue;
2525 			printf("%s ", re->sl[j].name);
2526 		}
2527 		printf("\n");
2528 	}
2529 #undef	PH_HDR
2530 #undef	PH_CT
2531 }
2532 
2533 static char *
2534 section_flags(struct readelf *re, struct section *s)
2535 {
2536 #define BUF_SZ 256
2537 	static char	buf[BUF_SZ];
2538 	int		i, p, nb;
2539 
2540 	p = 0;
2541 	nb = re->ec == ELFCLASS32 ? 8 : 16;
2542 	if (re->options & RE_T) {
2543 		snprintf(buf, BUF_SZ, "[%*.*jx]: ", nb, nb,
2544 		    (uintmax_t)s->flags);
2545 		p += nb + 4;
2546 	}
2547 	for (i = 0; section_flag[i].ln != NULL; i++) {
2548 		if ((s->flags & section_flag[i].value) == 0)
2549 			continue;
2550 		if (re->options & RE_T) {
2551 			snprintf(&buf[p], BUF_SZ - p, "%s, ",
2552 			    section_flag[i].ln);
2553 			p += strlen(section_flag[i].ln) + 2;
2554 		} else
2555 			buf[p++] = section_flag[i].sn;
2556 	}
2557 	if (re->options & RE_T && p > nb + 4)
2558 		p -= 2;
2559 	buf[p] = '\0';
2560 
2561 	return (buf);
2562 }
2563 
2564 static void
2565 dump_shdr(struct readelf *re)
2566 {
2567 	struct section	*s;
2568 	int		 i;
2569 
2570 #define	S_HDR	"[Nr] Name", "Type", "Addr", "Off", "Size", "ES",	\
2571 		"Flg", "Lk", "Inf", "Al"
2572 #define	S_HDRL	"[Nr] Name", "Type", "Address", "Offset", "Size",	\
2573 		"EntSize", "Flags", "Link", "Info", "Align"
2574 #define	ST_HDR	"[Nr] Name", "Type", "Addr", "Off", "Size", "ES",	\
2575 		"Lk", "Inf", "Al", "Flags"
2576 #define	ST_HDRL	"[Nr] Name", "Type", "Address", "Offset", "Link",	\
2577 		"Size", "EntSize", "Info", "Align", "Flags"
2578 #define	S_CT	i, s->name, section_type(re->ehdr.e_machine, s->type),	\
2579 		(uintmax_t)s->addr, (uintmax_t)s->off, (uintmax_t)s->sz,\
2580 		(uintmax_t)s->entsize, section_flags(re, s),		\
2581 		s->link, s->info, (uintmax_t)s->align
2582 #define	ST_CT	i, s->name, section_type(re->ehdr.e_machine, s->type),  \
2583 		(uintmax_t)s->addr, (uintmax_t)s->off, (uintmax_t)s->sz,\
2584 		(uintmax_t)s->entsize, s->link, s->info,		\
2585 		(uintmax_t)s->align, section_flags(re, s)
2586 #define	ST_CTL	i, s->name, section_type(re->ehdr.e_machine, s->type),  \
2587 		(uintmax_t)s->addr, (uintmax_t)s->off, s->link,		\
2588 		(uintmax_t)s->sz, (uintmax_t)s->entsize, s->info,	\
2589 		(uintmax_t)s->align, section_flags(re, s)
2590 
2591 	if (re->shnum == 0) {
2592 		printf("\nThere are no sections in this file.\n");
2593 		return;
2594 	}
2595 	printf("There are %ju section headers, starting at offset 0x%jx:\n",
2596 	    (uintmax_t)re->shnum, (uintmax_t)re->ehdr.e_shoff);
2597 	printf("\nSection Headers:\n");
2598 	if (re->ec == ELFCLASS32) {
2599 		if (re->options & RE_T)
2600 			printf("  %s\n       %-16s%-9s%-7s%-7s%-5s%-3s%-4s%s\n"
2601 			    "%12s\n", ST_HDR);
2602 		else
2603 			printf("  %-23s%-16s%-9s%-7s%-7s%-3s%-4s%-3s%-4s%s\n",
2604 			    S_HDR);
2605 	} else if (re->options & RE_WW) {
2606 		if (re->options & RE_T)
2607 			printf("  %s\n       %-16s%-17s%-7s%-7s%-5s%-3s%-4s%s\n"
2608 			    "%12s\n", ST_HDR);
2609 		else
2610 			printf("  %-23s%-16s%-17s%-7s%-7s%-3s%-4s%-3s%-4s%s\n",
2611 			    S_HDR);
2612 	} else {
2613 		if (re->options & RE_T)
2614 			printf("  %s\n       %-18s%-17s%-18s%s\n       %-18s"
2615 			    "%-17s%-18s%s\n%12s\n", ST_HDRL);
2616 		else
2617 			printf("  %-23s%-17s%-18s%s\n       %-18s%-17s%-7s%"
2618 			    "-6s%-6s%s\n", S_HDRL);
2619 	}
2620 	for (i = 0; (size_t)i < re->shnum; i++) {
2621 		s = &re->sl[i];
2622 		if (re->ec == ELFCLASS32) {
2623 			if (re->options & RE_T)
2624 				printf("  [%2d] %s\n       %-15.15s %8.8jx"
2625 				    " %6.6jx %6.6jx %2.2jx  %2u %3u %2ju\n"
2626 				    "       %s\n", ST_CT);
2627 			else
2628 				printf("  [%2d] %-17.17s %-15.15s %8.8jx"
2629 				    " %6.6jx %6.6jx %2.2jx %3s %2u %3u %2ju\n",
2630 				    S_CT);
2631 		} else if (re->options & RE_WW) {
2632 			if (re->options & RE_T)
2633 				printf("  [%2d] %s\n       %-15.15s %16.16jx"
2634 				    " %6.6jx %6.6jx %2.2jx  %2u %3u %2ju\n"
2635 				    "       %s\n", ST_CT);
2636 			else
2637 				printf("  [%2d] %-17.17s %-15.15s %16.16jx"
2638 				    " %6.6jx %6.6jx %2.2jx %3s %2u %3u %2ju\n",
2639 				    S_CT);
2640 		} else {
2641 			if (re->options & RE_T)
2642 				printf("  [%2d] %s\n       %-15.15s  %16.16jx"
2643 				    "  %16.16jx  %u\n       %16.16jx %16.16jx"
2644 				    "  %-16u  %ju\n       %s\n", ST_CTL);
2645 			else
2646 				printf("  [%2d] %-17.17s %-15.15s  %16.16jx"
2647 				    "  %8.8jx\n       %16.16jx  %16.16jx "
2648 				    "%3s      %2u   %3u     %ju\n", S_CT);
2649 		}
2650 	}
2651 	if ((re->options & RE_T) == 0)
2652 		printf("Key to Flags:\n  W (write), A (alloc),"
2653 		    " X (execute), M (merge), S (strings)\n"
2654 		    "  I (info), L (link order), G (group), x (unknown)\n"
2655 		    "  O (extra OS processing required)"
2656 		    " o (OS specific), p (processor specific)\n");
2657 
2658 #undef	S_HDR
2659 #undef	S_HDRL
2660 #undef	ST_HDR
2661 #undef	ST_HDRL
2662 #undef	S_CT
2663 #undef	ST_CT
2664 #undef	ST_CTL
2665 }
2666 
2667 /*
2668  * Return number of entries in the given section. We'd prefer ent_count be a
2669  * size_t *, but libelf APIs already use int for section indices.
2670  */
2671 static int
2672 get_ent_count(struct section *s, int *ent_count)
2673 {
2674 	if (s->entsize == 0) {
2675 		warnx("section %s has entry size 0", s->name);
2676 		return (0);
2677 	} else if (s->sz / s->entsize > INT_MAX) {
2678 		warnx("section %s has invalid section count", s->name);
2679 		return (0);
2680 	}
2681 	*ent_count = (int)(s->sz / s->entsize);
2682 	return (1);
2683 }
2684 
2685 static void
2686 dump_dynamic(struct readelf *re)
2687 {
2688 	GElf_Dyn	 dyn;
2689 	Elf_Data	*d;
2690 	struct section	*s;
2691 	int		 elferr, i, is_dynamic, j, jmax, nentries;
2692 
2693 	is_dynamic = 0;
2694 
2695 	for (i = 0; (size_t)i < re->shnum; i++) {
2696 		s = &re->sl[i];
2697 		if (s->type != SHT_DYNAMIC)
2698 			continue;
2699 		(void) elf_errno();
2700 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
2701 			elferr = elf_errno();
2702 			if (elferr != 0)
2703 				warnx("elf_getdata failed: %s", elf_errmsg(-1));
2704 			continue;
2705 		}
2706 		if (d->d_size <= 0)
2707 			continue;
2708 
2709 		is_dynamic = 1;
2710 
2711 		/* Determine the actual number of table entries. */
2712 		nentries = 0;
2713 		if (!get_ent_count(s, &jmax))
2714 			continue;
2715 		for (j = 0; j < jmax; j++) {
2716 			if (gelf_getdyn(d, j, &dyn) != &dyn) {
2717 				warnx("gelf_getdyn failed: %s",
2718 				    elf_errmsg(-1));
2719 				continue;
2720 			}
2721 			nentries ++;
2722 			if (dyn.d_tag == DT_NULL)
2723 				break;
2724                 }
2725 
2726 		printf("\nDynamic section at offset 0x%jx", (uintmax_t)s->off);
2727 		printf(" contains %u entries:\n", nentries);
2728 
2729 		if (re->ec == ELFCLASS32)
2730 			printf("%5s%12s%28s\n", "Tag", "Type", "Name/Value");
2731 		else
2732 			printf("%5s%20s%28s\n", "Tag", "Type", "Name/Value");
2733 
2734 		for (j = 0; j < nentries; j++) {
2735 			if (gelf_getdyn(d, j, &dyn) != &dyn)
2736 				continue;
2737 			/* Dump dynamic entry type. */
2738 			if (re->ec == ELFCLASS32)
2739 				printf(" 0x%8.8jx", (uintmax_t)dyn.d_tag);
2740 			else
2741 				printf(" 0x%16.16jx", (uintmax_t)dyn.d_tag);
2742 			printf(" %-20s", dt_type(re->ehdr.e_machine,
2743 			    dyn.d_tag));
2744 			/* Dump dynamic entry value. */
2745 			dump_dyn_val(re, &dyn, s->link);
2746 		}
2747 	}
2748 
2749 	if (!is_dynamic)
2750 		printf("\nThere is no dynamic section in this file.\n");
2751 }
2752 
2753 static char *
2754 timestamp(time_t ti)
2755 {
2756 	static char ts[32];
2757 	struct tm *t;
2758 
2759 	t = gmtime(&ti);
2760 	snprintf(ts, sizeof(ts), "%04d-%02d-%02dT%02d:%02d:%02d",
2761 	    t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour,
2762 	    t->tm_min, t->tm_sec);
2763 
2764 	return (ts);
2765 }
2766 
2767 static const char *
2768 dyn_str(struct readelf *re, uint32_t stab, uint64_t d_val)
2769 {
2770 	const char *name;
2771 
2772 	if (stab == SHN_UNDEF)
2773 		name = "ERROR";
2774 	else if ((name = elf_strptr(re->elf, stab, d_val)) == NULL) {
2775 		(void) elf_errno(); /* clear error */
2776 		name = "ERROR";
2777 	}
2778 
2779 	return (name);
2780 }
2781 
2782 static void
2783 dump_arch_dyn_val(struct readelf *re, GElf_Dyn *dyn)
2784 {
2785 	switch (re->ehdr.e_machine) {
2786 	case EM_MIPS:
2787 	case EM_MIPS_RS3_LE:
2788 		switch (dyn->d_tag) {
2789 		case DT_MIPS_RLD_VERSION:
2790 		case DT_MIPS_LOCAL_GOTNO:
2791 		case DT_MIPS_CONFLICTNO:
2792 		case DT_MIPS_LIBLISTNO:
2793 		case DT_MIPS_SYMTABNO:
2794 		case DT_MIPS_UNREFEXTNO:
2795 		case DT_MIPS_GOTSYM:
2796 		case DT_MIPS_HIPAGENO:
2797 		case DT_MIPS_DELTA_CLASS_NO:
2798 		case DT_MIPS_DELTA_INSTANCE_NO:
2799 		case DT_MIPS_DELTA_RELOC_NO:
2800 		case DT_MIPS_DELTA_SYM_NO:
2801 		case DT_MIPS_DELTA_CLASSSYM_NO:
2802 		case DT_MIPS_LOCALPAGE_GOTIDX:
2803 		case DT_MIPS_LOCAL_GOTIDX:
2804 		case DT_MIPS_HIDDEN_GOTIDX:
2805 		case DT_MIPS_PROTECTED_GOTIDX:
2806 			printf(" %ju\n", (uintmax_t) dyn->d_un.d_val);
2807 			break;
2808 		case DT_MIPS_ICHECKSUM:
2809 		case DT_MIPS_FLAGS:
2810 		case DT_MIPS_BASE_ADDRESS:
2811 		case DT_MIPS_CONFLICT:
2812 		case DT_MIPS_LIBLIST:
2813 		case DT_MIPS_RLD_MAP:
2814 		case DT_MIPS_DELTA_CLASS:
2815 		case DT_MIPS_DELTA_INSTANCE:
2816 		case DT_MIPS_DELTA_RELOC:
2817 		case DT_MIPS_DELTA_SYM:
2818 		case DT_MIPS_DELTA_CLASSSYM:
2819 		case DT_MIPS_CXX_FLAGS:
2820 		case DT_MIPS_PIXIE_INIT:
2821 		case DT_MIPS_SYMBOL_LIB:
2822 		case DT_MIPS_OPTIONS:
2823 		case DT_MIPS_INTERFACE:
2824 		case DT_MIPS_DYNSTR_ALIGN:
2825 		case DT_MIPS_INTERFACE_SIZE:
2826 		case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
2827 		case DT_MIPS_COMPACT_SIZE:
2828 		case DT_MIPS_GP_VALUE:
2829 		case DT_MIPS_AUX_DYNAMIC:
2830 		case DT_MIPS_PLTGOT:
2831 		case DT_MIPS_RLD_OBJ_UPDATE:
2832 		case DT_MIPS_RWPLT:
2833 			printf(" 0x%jx\n", (uintmax_t) dyn->d_un.d_val);
2834 			break;
2835 		case DT_MIPS_IVERSION:
2836 		case DT_MIPS_PERF_SUFFIX:
2837 		case DT_MIPS_TIME_STAMP:
2838 			printf(" %s\n", timestamp(dyn->d_un.d_val));
2839 			break;
2840 		default:
2841 			printf("\n");
2842 			break;
2843 		}
2844 		break;
2845 	default:
2846 		printf("\n");
2847 		break;
2848 	}
2849 }
2850 
2851 static void
2852 dump_flags(struct flag_desc *desc, uint64_t val)
2853 {
2854 	struct flag_desc *fd;
2855 
2856 	for (fd = desc; fd->flag != 0; fd++) {
2857 		if (val & fd->flag) {
2858 			val &= ~fd->flag;
2859 			printf(" %s", fd->desc);
2860 		}
2861 	}
2862 	if (val != 0)
2863 		printf(" unknown (0x%jx)", (uintmax_t)val);
2864 	printf("\n");
2865 }
2866 
2867 static struct flag_desc dt_flags[] = {
2868 	{ DF_ORIGIN,		"ORIGIN" },
2869 	{ DF_SYMBOLIC,		"SYMBOLIC" },
2870 	{ DF_TEXTREL,		"TEXTREL" },
2871 	{ DF_BIND_NOW,		"BIND_NOW" },
2872 	{ DF_STATIC_TLS,	"STATIC_TLS" },
2873 	{ 0, NULL }
2874 };
2875 
2876 static struct flag_desc dt_flags_1[] = {
2877 	{ DF_1_BIND_NOW,	"NOW" },
2878 	{ DF_1_GLOBAL,		"GLOBAL" },
2879 	{ 0x4,			"GROUP" },
2880 	{ DF_1_NODELETE,	"NODELETE" },
2881 	{ DF_1_LOADFLTR,	"LOADFLTR" },
2882 	{ 0x20,			"INITFIRST" },
2883 	{ DF_1_NOOPEN,		"NOOPEN" },
2884 	{ DF_1_ORIGIN,		"ORIGIN" },
2885 	{ 0x100,		"DIRECT" },
2886 	{ DF_1_INTERPOSE,	"INTERPOSE" },
2887 	{ DF_1_NODEFLIB,	"NODEFLIB" },
2888 	{ 0x1000,		"NODUMP" },
2889 	{ 0x2000,		"CONFALT" },
2890 	{ 0x4000,		"ENDFILTEE" },
2891 	{ 0x8000,		"DISPRELDNE" },
2892 	{ 0x10000,		"DISPRELPND" },
2893 	{ 0x20000,		"NODIRECT" },
2894 	{ 0x40000,		"IGNMULDEF" },
2895 	{ 0x80000,		"NOKSYMS" },
2896 	{ 0x100000,		"NOHDR" },
2897 	{ 0x200000,		"EDITED" },
2898 	{ 0x400000,		"NORELOC" },
2899 	{ 0x800000,		"SYMINTPOSE" },
2900 	{ 0x1000000,		"GLOBAUDIT" },
2901 	{ 0, NULL }
2902 };
2903 
2904 static void
2905 dump_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab)
2906 {
2907 	const char *name;
2908 
2909 	if (dyn->d_tag >= DT_LOPROC && dyn->d_tag <= DT_HIPROC &&
2910 	    dyn->d_tag != DT_AUXILIARY && dyn->d_tag != DT_FILTER) {
2911 		dump_arch_dyn_val(re, dyn);
2912 		return;
2913 	}
2914 
2915 	/* These entry values are index into the string table. */
2916 	name = NULL;
2917 	if (dyn->d_tag == DT_AUXILIARY || dyn->d_tag == DT_FILTER ||
2918 	    dyn->d_tag == DT_NEEDED || dyn->d_tag == DT_SONAME ||
2919 	    dyn->d_tag == DT_RPATH || dyn->d_tag == DT_RUNPATH)
2920 		name = dyn_str(re, stab, dyn->d_un.d_val);
2921 
2922 	switch(dyn->d_tag) {
2923 	case DT_NULL:
2924 	case DT_PLTGOT:
2925 	case DT_HASH:
2926 	case DT_STRTAB:
2927 	case DT_SYMTAB:
2928 	case DT_RELA:
2929 	case DT_INIT:
2930 	case DT_SYMBOLIC:
2931 	case DT_REL:
2932 	case DT_DEBUG:
2933 	case DT_TEXTREL:
2934 	case DT_JMPREL:
2935 	case DT_FINI:
2936 	case DT_VERDEF:
2937 	case DT_VERNEED:
2938 	case DT_VERSYM:
2939 	case DT_GNU_HASH:
2940 	case DT_GNU_LIBLIST:
2941 	case DT_GNU_CONFLICT:
2942 		printf(" 0x%jx\n", (uintmax_t) dyn->d_un.d_val);
2943 		break;
2944 	case DT_PLTRELSZ:
2945 	case DT_RELASZ:
2946 	case DT_RELAENT:
2947 	case DT_STRSZ:
2948 	case DT_SYMENT:
2949 	case DT_RELSZ:
2950 	case DT_RELENT:
2951 	case DT_PREINIT_ARRAYSZ:
2952 	case DT_INIT_ARRAYSZ:
2953 	case DT_FINI_ARRAYSZ:
2954 	case DT_GNU_CONFLICTSZ:
2955 	case DT_GNU_LIBLISTSZ:
2956 		printf(" %ju (bytes)\n", (uintmax_t) dyn->d_un.d_val);
2957 		break;
2958  	case DT_RELACOUNT:
2959 	case DT_RELCOUNT:
2960 	case DT_VERDEFNUM:
2961 	case DT_VERNEEDNUM:
2962 		printf(" %ju\n", (uintmax_t) dyn->d_un.d_val);
2963 		break;
2964 	case DT_AUXILIARY:
2965 		printf(" Auxiliary library: [%s]\n", name);
2966 		break;
2967 	case DT_FILTER:
2968 		printf(" Filter library: [%s]\n", name);
2969 		break;
2970 	case DT_NEEDED:
2971 		printf(" Shared library: [%s]\n", name);
2972 		break;
2973 	case DT_SONAME:
2974 		printf(" Library soname: [%s]\n", name);
2975 		break;
2976 	case DT_RPATH:
2977 		printf(" Library rpath: [%s]\n", name);
2978 		break;
2979 	case DT_RUNPATH:
2980 		printf(" Library runpath: [%s]\n", name);
2981 		break;
2982 	case DT_PLTREL:
2983 		printf(" %s\n", dt_type(re->ehdr.e_machine, dyn->d_un.d_val));
2984 		break;
2985 	case DT_GNU_PRELINKED:
2986 		printf(" %s\n", timestamp(dyn->d_un.d_val));
2987 		break;
2988 	case DT_FLAGS:
2989 		dump_flags(dt_flags, dyn->d_un.d_val);
2990 		break;
2991 	case DT_FLAGS_1:
2992 		dump_flags(dt_flags_1, dyn->d_un.d_val);
2993 		break;
2994 	default:
2995 		printf("\n");
2996 	}
2997 }
2998 
2999 static void
3000 dump_rel(struct readelf *re, struct section *s, Elf_Data *d)
3001 {
3002 	GElf_Rel r;
3003 	const char *symname;
3004 	uint64_t symval;
3005 	int i, len;
3006 	uint32_t type;
3007 	uint8_t type2, type3;
3008 
3009 	if (s->link >= re->shnum)
3010 		return;
3011 
3012 #define	REL_HDR "r_offset", "r_info", "r_type", "st_value", "st_name"
3013 #define	REL_CT32 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
3014 		elftc_reloc_type_str(re->ehdr.e_machine,	    \
3015 		ELF32_R_TYPE(r.r_info)), (uintmax_t)symval, symname
3016 #define	REL_CT64 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
3017 		elftc_reloc_type_str(re->ehdr.e_machine, type),	    \
3018 		(uintmax_t)symval, symname
3019 
3020 	printf("\nRelocation section (%s):\n", s->name);
3021 	if (re->ec == ELFCLASS32)
3022 		printf("%-8s %-8s %-19s %-8s %s\n", REL_HDR);
3023 	else {
3024 		if (re->options & RE_WW)
3025 			printf("%-16s %-16s %-24s %-16s %s\n", REL_HDR);
3026 		else
3027 			printf("%-12s %-12s %-19s %-16s %s\n", REL_HDR);
3028 	}
3029 	assert(d->d_size == s->sz);
3030 	if (!get_ent_count(s, &len))
3031 		return;
3032 	for (i = 0; i < len; i++) {
3033 		if (gelf_getrel(d, i, &r) != &r) {
3034 			warnx("gelf_getrel failed: %s", elf_errmsg(-1));
3035 			continue;
3036 		}
3037 		symname = get_symbol_name(re, s->link, GELF_R_SYM(r.r_info));
3038 		symval = get_symbol_value(re, s->link, GELF_R_SYM(r.r_info));
3039 		if (re->ec == ELFCLASS32) {
3040 			r.r_info = ELF32_R_INFO(ELF64_R_SYM(r.r_info),
3041 			    ELF64_R_TYPE(r.r_info));
3042 			printf("%8.8jx %8.8jx %-19.19s %8.8jx %s\n", REL_CT32);
3043 		} else {
3044 			type = ELF64_R_TYPE(r.r_info);
3045 			if (re->ehdr.e_machine == EM_MIPS) {
3046 				type2 = (type >> 8) & 0xFF;
3047 				type3 = (type >> 16) & 0xFF;
3048 				type = type & 0xFF;
3049 			} else {
3050 				type2 = type3 = 0;
3051 			}
3052 			if (re->options & RE_WW)
3053 				printf("%16.16jx %16.16jx %-24.24s"
3054 				    " %16.16jx %s\n", REL_CT64);
3055 			else
3056 				printf("%12.12jx %12.12jx %-19.19s"
3057 				    " %16.16jx %s\n", REL_CT64);
3058 			if (re->ehdr.e_machine == EM_MIPS) {
3059 				if (re->options & RE_WW) {
3060 					printf("%32s: %s\n", "Type2",
3061 					    elftc_reloc_type_str(EM_MIPS,
3062 					    type2));
3063 					printf("%32s: %s\n", "Type3",
3064 					    elftc_reloc_type_str(EM_MIPS,
3065 					    type3));
3066 				} else {
3067 					printf("%24s: %s\n", "Type2",
3068 					    elftc_reloc_type_str(EM_MIPS,
3069 					    type2));
3070 					printf("%24s: %s\n", "Type3",
3071 					    elftc_reloc_type_str(EM_MIPS,
3072 					    type3));
3073 				}
3074 			}
3075 		}
3076 	}
3077 
3078 #undef	REL_HDR
3079 #undef	REL_CT
3080 }
3081 
3082 static void
3083 dump_rela(struct readelf *re, struct section *s, Elf_Data *d)
3084 {
3085 	GElf_Rela r;
3086 	const char *symname;
3087 	uint64_t symval;
3088 	int i, len;
3089 	uint32_t type;
3090 	uint8_t type2, type3;
3091 
3092 	if (s->link >= re->shnum)
3093 		return;
3094 
3095 #define	RELA_HDR "r_offset", "r_info", "r_type", "st_value", \
3096 		"st_name + r_addend"
3097 #define	RELA_CT32 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
3098 		elftc_reloc_type_str(re->ehdr.e_machine,	    \
3099 		ELF32_R_TYPE(r.r_info)), (uintmax_t)symval, symname
3100 #define	RELA_CT64 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
3101 		elftc_reloc_type_str(re->ehdr.e_machine, type),	    \
3102 		(uintmax_t)symval, symname
3103 
3104 	printf("\nRelocation section with addend (%s):\n", s->name);
3105 	if (re->ec == ELFCLASS32)
3106 		printf("%-8s %-8s %-19s %-8s %s\n", RELA_HDR);
3107 	else {
3108 		if (re->options & RE_WW)
3109 			printf("%-16s %-16s %-24s %-16s %s\n", RELA_HDR);
3110 		else
3111 			printf("%-12s %-12s %-19s %-16s %s\n", RELA_HDR);
3112 	}
3113 	assert(d->d_size == s->sz);
3114 	if (!get_ent_count(s, &len))
3115 		return;
3116 	for (i = 0; i < len; i++) {
3117 		if (gelf_getrela(d, i, &r) != &r) {
3118 			warnx("gelf_getrel failed: %s", elf_errmsg(-1));
3119 			continue;
3120 		}
3121 		symname = get_symbol_name(re, s->link, GELF_R_SYM(r.r_info));
3122 		symval = get_symbol_value(re, s->link, GELF_R_SYM(r.r_info));
3123 		if (re->ec == ELFCLASS32) {
3124 			r.r_info = ELF32_R_INFO(ELF64_R_SYM(r.r_info),
3125 			    ELF64_R_TYPE(r.r_info));
3126 			printf("%8.8jx %8.8jx %-19.19s %8.8jx %s", RELA_CT32);
3127 			printf(" + %x\n", (uint32_t) r.r_addend);
3128 		} else {
3129 			type = ELF64_R_TYPE(r.r_info);
3130 			if (re->ehdr.e_machine == EM_MIPS) {
3131 				type2 = (type >> 8) & 0xFF;
3132 				type3 = (type >> 16) & 0xFF;
3133 				type = type & 0xFF;
3134 			} else {
3135 				type2 = type3 = 0;
3136 			}
3137 			if (re->options & RE_WW)
3138 				printf("%16.16jx %16.16jx %-24.24s"
3139 				    " %16.16jx %s", RELA_CT64);
3140 			else
3141 				printf("%12.12jx %12.12jx %-19.19s"
3142 				    " %16.16jx %s", RELA_CT64);
3143 			printf(" + %jx\n", (uintmax_t) r.r_addend);
3144 			if (re->ehdr.e_machine == EM_MIPS) {
3145 				if (re->options & RE_WW) {
3146 					printf("%32s: %s\n", "Type2",
3147 					    elftc_reloc_type_str(EM_MIPS,
3148 					    type2));
3149 					printf("%32s: %s\n", "Type3",
3150 					    elftc_reloc_type_str(EM_MIPS,
3151 					    type3));
3152 				} else {
3153 					printf("%24s: %s\n", "Type2",
3154 					    elftc_reloc_type_str(EM_MIPS,
3155 					    type2));
3156 					printf("%24s: %s\n", "Type3",
3157 					    elftc_reloc_type_str(EM_MIPS,
3158 					    type3));
3159 				}
3160 			}
3161 		}
3162 	}
3163 
3164 #undef	RELA_HDR
3165 #undef	RELA_CT
3166 }
3167 
3168 static void
3169 dump_reloc(struct readelf *re)
3170 {
3171 	struct section *s;
3172 	Elf_Data *d;
3173 	int i, elferr;
3174 
3175 	for (i = 0; (size_t)i < re->shnum; i++) {
3176 		s = &re->sl[i];
3177 		if (s->type == SHT_REL || s->type == SHT_RELA) {
3178 			(void) elf_errno();
3179 			if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3180 				elferr = elf_errno();
3181 				if (elferr != 0)
3182 					warnx("elf_getdata failed: %s",
3183 					    elf_errmsg(elferr));
3184 				continue;
3185 			}
3186 			if (s->type == SHT_REL)
3187 				dump_rel(re, s, d);
3188 			else
3189 				dump_rela(re, s, d);
3190 		}
3191 	}
3192 }
3193 
3194 static void
3195 dump_symtab(struct readelf *re, int i)
3196 {
3197 	struct section *s;
3198 	Elf_Data *d;
3199 	GElf_Sym sym;
3200 	const char *name;
3201 	uint32_t stab;
3202 	int elferr, j, len;
3203 	uint16_t vs;
3204 
3205 	s = &re->sl[i];
3206 	if (s->link >= re->shnum)
3207 		return;
3208 	stab = s->link;
3209 	(void) elf_errno();
3210 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3211 		elferr = elf_errno();
3212 		if (elferr != 0)
3213 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
3214 		return;
3215 	}
3216 	if (d->d_size <= 0)
3217 		return;
3218 	if (!get_ent_count(s, &len))
3219 		return;
3220 	printf("Symbol table (%s)", s->name);
3221 	printf(" contains %d entries:\n", len);
3222 	printf("%7s%9s%14s%5s%8s%6s%9s%5s\n", "Num:", "Value", "Size", "Type",
3223 	    "Bind", "Vis", "Ndx", "Name");
3224 
3225 	for (j = 0; j < len; j++) {
3226 		if (gelf_getsym(d, j, &sym) != &sym) {
3227 			warnx("gelf_getsym failed: %s", elf_errmsg(-1));
3228 			continue;
3229 		}
3230 		printf("%6d:", j);
3231 		printf(" %16.16jx", (uintmax_t) sym.st_value);
3232 		printf(" %5ju", (uintmax_t) sym.st_size);
3233 		printf(" %-7s", st_type(re->ehdr.e_machine,
3234 		    re->ehdr.e_ident[EI_OSABI], GELF_ST_TYPE(sym.st_info)));
3235 		printf(" %-6s", st_bind(GELF_ST_BIND(sym.st_info)));
3236 		printf(" %-8s", st_vis(GELF_ST_VISIBILITY(sym.st_other)));
3237 		printf(" %3s", st_shndx(sym.st_shndx));
3238 		if ((name = elf_strptr(re->elf, stab, sym.st_name)) != NULL)
3239 			printf(" %s", name);
3240 		/* Append symbol version string for SHT_DYNSYM symbol table. */
3241 		if (s->type == SHT_DYNSYM && re->ver != NULL &&
3242 		    re->vs != NULL && re->vs[j] > 1) {
3243 			vs = re->vs[j] & VERSYM_VERSION;
3244 			if (vs >= re->ver_sz || re->ver[vs].name == NULL) {
3245 				warnx("invalid versym version index %u", vs);
3246 				break;
3247 			}
3248 			if (re->vs[j] & VERSYM_HIDDEN || re->ver[vs].type == 0)
3249 				printf("@%s (%d)", re->ver[vs].name, vs);
3250 			else
3251 				printf("@@%s (%d)", re->ver[vs].name, vs);
3252 		}
3253 		putchar('\n');
3254 	}
3255 
3256 }
3257 
3258 static void
3259 dump_symtabs(struct readelf *re)
3260 {
3261 	GElf_Dyn dyn;
3262 	Elf_Data *d;
3263 	struct section *s;
3264 	uint64_t dyn_off;
3265 	int elferr, i, len;
3266 
3267 	/*
3268 	 * If -D is specified, only dump the symbol table specified by
3269 	 * the DT_SYMTAB entry in the .dynamic section.
3270 	 */
3271 	dyn_off = 0;
3272 	if (re->options & RE_DD) {
3273 		s = NULL;
3274 		for (i = 0; (size_t)i < re->shnum; i++)
3275 			if (re->sl[i].type == SHT_DYNAMIC) {
3276 				s = &re->sl[i];
3277 				break;
3278 			}
3279 		if (s == NULL)
3280 			return;
3281 		(void) elf_errno();
3282 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3283 			elferr = elf_errno();
3284 			if (elferr != 0)
3285 				warnx("elf_getdata failed: %s", elf_errmsg(-1));
3286 			return;
3287 		}
3288 		if (d->d_size <= 0)
3289 			return;
3290 		if (!get_ent_count(s, &len))
3291 			return;
3292 
3293 		for (i = 0; i < len; i++) {
3294 			if (gelf_getdyn(d, i, &dyn) != &dyn) {
3295 				warnx("gelf_getdyn failed: %s", elf_errmsg(-1));
3296 				continue;
3297 			}
3298 			if (dyn.d_tag == DT_SYMTAB) {
3299 				dyn_off = dyn.d_un.d_val;
3300 				break;
3301 			}
3302 		}
3303 	}
3304 
3305 	/* Find and dump symbol tables. */
3306 	for (i = 0; (size_t)i < re->shnum; i++) {
3307 		s = &re->sl[i];
3308 		if (s->type == SHT_SYMTAB || s->type == SHT_DYNSYM) {
3309 			if (re->options & RE_DD) {
3310 				if (dyn_off == s->addr) {
3311 					dump_symtab(re, i);
3312 					break;
3313 				}
3314 			} else
3315 				dump_symtab(re, i);
3316 		}
3317 	}
3318 }
3319 
3320 static void
3321 dump_svr4_hash(struct section *s)
3322 {
3323 	Elf_Data	*d;
3324 	uint32_t	*buf;
3325 	uint32_t	 nbucket, nchain;
3326 	uint32_t	*bucket, *chain;
3327 	uint32_t	*bl, *c, maxl, total;
3328 	int		 elferr, i, j;
3329 
3330 	/* Read and parse the content of .hash section. */
3331 	(void) elf_errno();
3332 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3333 		elferr = elf_errno();
3334 		if (elferr != 0)
3335 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
3336 		return;
3337 	}
3338 	if (d->d_size < 2 * sizeof(uint32_t)) {
3339 		warnx(".hash section too small");
3340 		return;
3341 	}
3342 	buf = d->d_buf;
3343 	nbucket = buf[0];
3344 	nchain = buf[1];
3345 	if (nbucket <= 0 || nchain <= 0) {
3346 		warnx("Malformed .hash section");
3347 		return;
3348 	}
3349 	if (d->d_size != (nbucket + nchain + 2) * sizeof(uint32_t)) {
3350 		warnx("Malformed .hash section");
3351 		return;
3352 	}
3353 	bucket = &buf[2];
3354 	chain = &buf[2 + nbucket];
3355 
3356 	maxl = 0;
3357 	if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
3358 		errx(EXIT_FAILURE, "calloc failed");
3359 	for (i = 0; (uint32_t)i < nbucket; i++)
3360 		for (j = bucket[i]; j > 0 && (uint32_t)j < nchain; j = chain[j])
3361 			if (++bl[i] > maxl)
3362 				maxl = bl[i];
3363 	if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
3364 		errx(EXIT_FAILURE, "calloc failed");
3365 	for (i = 0; (uint32_t)i < nbucket; i++)
3366 		c[bl[i]]++;
3367 	printf("\nHistogram for bucket list length (total of %u buckets):\n",
3368 	    nbucket);
3369 	printf(" Length\tNumber\t\t%% of total\tCoverage\n");
3370 	total = 0;
3371 	for (i = 0; (uint32_t)i <= maxl; i++) {
3372 		total += c[i] * i;
3373 		printf("%7u\t%-10u\t(%5.1f%%)\t%5.1f%%\n", i, c[i],
3374 		    c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
3375 	}
3376 	free(c);
3377 	free(bl);
3378 }
3379 
3380 static void
3381 dump_svr4_hash64(struct readelf *re, struct section *s)
3382 {
3383 	Elf_Data	*d, dst;
3384 	uint64_t	*buf;
3385 	uint64_t	 nbucket, nchain;
3386 	uint64_t	*bucket, *chain;
3387 	uint64_t	*bl, *c, maxl, total;
3388 	int		 elferr, i, j;
3389 
3390 	/*
3391 	 * ALPHA uses 64-bit hash entries. Since libelf assumes that
3392 	 * .hash section contains only 32-bit entry, an explicit
3393 	 * gelf_xlatetom is needed here.
3394 	 */
3395 	(void) elf_errno();
3396 	if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
3397 		elferr = elf_errno();
3398 		if (elferr != 0)
3399 			warnx("elf_rawdata failed: %s",
3400 			    elf_errmsg(elferr));
3401 		return;
3402 	}
3403 	d->d_type = ELF_T_XWORD;
3404 	memcpy(&dst, d, sizeof(Elf_Data));
3405 	if (gelf_xlatetom(re->elf, &dst, d,
3406 		re->ehdr.e_ident[EI_DATA]) != &dst) {
3407 		warnx("gelf_xlatetom failed: %s", elf_errmsg(-1));
3408 		return;
3409 	}
3410 	if (dst.d_size < 2 * sizeof(uint64_t)) {
3411 		warnx(".hash section too small");
3412 		return;
3413 	}
3414 	buf = dst.d_buf;
3415 	nbucket = buf[0];
3416 	nchain = buf[1];
3417 	if (nbucket <= 0 || nchain <= 0) {
3418 		warnx("Malformed .hash section");
3419 		return;
3420 	}
3421 	if (d->d_size != (nbucket + nchain + 2) * sizeof(uint32_t)) {
3422 		warnx("Malformed .hash section");
3423 		return;
3424 	}
3425 	bucket = &buf[2];
3426 	chain = &buf[2 + nbucket];
3427 
3428 	maxl = 0;
3429 	if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
3430 		errx(EXIT_FAILURE, "calloc failed");
3431 	for (i = 0; (uint32_t)i < nbucket; i++)
3432 		for (j = bucket[i]; j > 0 && (uint32_t)j < nchain; j = chain[j])
3433 			if (++bl[i] > maxl)
3434 				maxl = bl[i];
3435 	if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
3436 		errx(EXIT_FAILURE, "calloc failed");
3437 	for (i = 0; (uint64_t)i < nbucket; i++)
3438 		c[bl[i]]++;
3439 	printf("Histogram for bucket list length (total of %ju buckets):\n",
3440 	    (uintmax_t)nbucket);
3441 	printf(" Length\tNumber\t\t%% of total\tCoverage\n");
3442 	total = 0;
3443 	for (i = 0; (uint64_t)i <= maxl; i++) {
3444 		total += c[i] * i;
3445 		printf("%7u\t%-10ju\t(%5.1f%%)\t%5.1f%%\n", i, (uintmax_t)c[i],
3446 		    c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
3447 	}
3448 	free(c);
3449 	free(bl);
3450 }
3451 
3452 static void
3453 dump_gnu_hash(struct readelf *re, struct section *s)
3454 {
3455 	struct section	*ds;
3456 	Elf_Data	*d;
3457 	uint32_t	*buf;
3458 	uint32_t	*bucket, *chain;
3459 	uint32_t	 nbucket, nchain, symndx, maskwords;
3460 	uint32_t	*bl, *c, maxl, total;
3461 	int		 elferr, dynsymcount, i, j;
3462 
3463 	(void) elf_errno();
3464 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3465 		elferr = elf_errno();
3466 		if (elferr != 0)
3467 			warnx("elf_getdata failed: %s",
3468 			    elf_errmsg(elferr));
3469 		return;
3470 	}
3471 	if (d->d_size < 4 * sizeof(uint32_t)) {
3472 		warnx(".gnu.hash section too small");
3473 		return;
3474 	}
3475 	buf = d->d_buf;
3476 	nbucket = buf[0];
3477 	symndx = buf[1];
3478 	maskwords = buf[2];
3479 	buf += 4;
3480 	if (s->link >= re->shnum)
3481 		return;
3482 	ds = &re->sl[s->link];
3483 	if (!get_ent_count(ds, &dynsymcount))
3484 		return;
3485 	if (symndx >= (uint32_t)dynsymcount) {
3486 		warnx("Malformed .gnu.hash section (symndx out of range)");
3487 		return;
3488 	}
3489 	nchain = dynsymcount - symndx;
3490 	if (d->d_size != 4 * sizeof(uint32_t) + maskwords *
3491 	    (re->ec == ELFCLASS32 ? sizeof(uint32_t) : sizeof(uint64_t)) +
3492 	    (nbucket + nchain) * sizeof(uint32_t)) {
3493 		warnx("Malformed .gnu.hash section");
3494 		return;
3495 	}
3496 	bucket = buf + (re->ec == ELFCLASS32 ? maskwords : maskwords * 2);
3497 	chain = bucket + nbucket;
3498 
3499 	maxl = 0;
3500 	if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
3501 		errx(EXIT_FAILURE, "calloc failed");
3502 	for (i = 0; (uint32_t)i < nbucket; i++)
3503 		for (j = bucket[i]; j > 0 && (uint32_t)j - symndx < nchain;
3504 		     j++) {
3505 			if (++bl[i] > maxl)
3506 				maxl = bl[i];
3507 			if (chain[j - symndx] & 1)
3508 				break;
3509 		}
3510 	if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
3511 		errx(EXIT_FAILURE, "calloc failed");
3512 	for (i = 0; (uint32_t)i < nbucket; i++)
3513 		c[bl[i]]++;
3514 	printf("Histogram for bucket list length (total of %u buckets):\n",
3515 	    nbucket);
3516 	printf(" Length\tNumber\t\t%% of total\tCoverage\n");
3517 	total = 0;
3518 	for (i = 0; (uint32_t)i <= maxl; i++) {
3519 		total += c[i] * i;
3520 		printf("%7u\t%-10u\t(%5.1f%%)\t%5.1f%%\n", i, c[i],
3521 		    c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
3522 	}
3523 	free(c);
3524 	free(bl);
3525 }
3526 
3527 static struct flag_desc gnu_property_x86_feature_1_and_bits[] = {
3528 	{ GNU_PROPERTY_X86_FEATURE_1_IBT,	"IBT" },
3529 	{ GNU_PROPERTY_X86_FEATURE_1_SHSTK,	"SHSTK" },
3530 	{ 0, NULL }
3531 };
3532 
3533 static void
3534 dump_gnu_property_type_0(struct readelf *re, const char *buf, size_t sz)
3535 {
3536 	size_t i;
3537 	uint32_t type, prop_sz;
3538 
3539 	printf("      Properties: ");
3540 	while (sz > 0) {
3541 		if (sz < 8)
3542 			goto bad;
3543 
3544 		type = *(const uint32_t *)(const void *)buf;
3545 		prop_sz = *(const uint32_t *)(const void *)(buf + 4);
3546 		buf += 8;
3547 		sz -= 8;
3548 
3549 		if (prop_sz > sz)
3550 			goto bad;
3551 
3552 		if (type >= GNU_PROPERTY_LOPROC &&
3553 		    type <= GNU_PROPERTY_HIPROC) {
3554 			if (re->ehdr.e_machine != EM_X86_64) {
3555 				printf("machine type %x unknown\n",
3556 				    re->ehdr.e_machine);
3557 				goto unknown;
3558 			}
3559 			switch (type) {
3560 			case GNU_PROPERTY_X86_FEATURE_1_AND:
3561 				printf("x86 features:");
3562 				if (prop_sz != 4)
3563 					goto bad;
3564 				dump_flags(gnu_property_x86_feature_1_and_bits,
3565 				    *(const uint32_t *)(const void *)buf);
3566 				break;
3567 			}
3568 		}
3569 
3570 		buf += roundup2(prop_sz, 8);
3571 		sz -= roundup2(prop_sz, 8);
3572 	}
3573 	return;
3574 bad:
3575 	printf("corrupt GNU property\n");
3576 unknown:
3577 	printf("remaining description data:");
3578 	for (i = 0; i < sz; i++)
3579 		printf(" %02x", (unsigned char)buf[i]);
3580 	printf("\n");
3581 }
3582 
3583 static void
3584 dump_hash(struct readelf *re)
3585 {
3586 	struct section	*s;
3587 	int		 i;
3588 
3589 	for (i = 0; (size_t) i < re->shnum; i++) {
3590 		s = &re->sl[i];
3591 		if (s->type == SHT_HASH || s->type == SHT_GNU_HASH) {
3592 			if (s->type == SHT_GNU_HASH)
3593 				dump_gnu_hash(re, s);
3594 			else if (re->ehdr.e_machine == EM_ALPHA &&
3595 			    s->entsize == 8)
3596 				dump_svr4_hash64(re, s);
3597 			else
3598 				dump_svr4_hash(s);
3599 		}
3600 	}
3601 }
3602 
3603 static void
3604 dump_notes(struct readelf *re)
3605 {
3606 	struct section *s;
3607 	const char *rawfile;
3608 	GElf_Phdr phdr;
3609 	Elf_Data *d;
3610 	size_t filesize, phnum;
3611 	int i, elferr;
3612 
3613 	if (re->ehdr.e_type == ET_CORE) {
3614 		/*
3615 		 * Search program headers in the core file for
3616 		 * PT_NOTE entry.
3617 		 */
3618 		if (elf_getphnum(re->elf, &phnum) == 0) {
3619 			warnx("elf_getphnum failed: %s", elf_errmsg(-1));
3620 			return;
3621 		}
3622 		if (phnum == 0)
3623 			return;
3624 		if ((rawfile = elf_rawfile(re->elf, &filesize)) == NULL) {
3625 			warnx("elf_rawfile failed: %s", elf_errmsg(-1));
3626 			return;
3627 		}
3628 		for (i = 0; (size_t) i < phnum; i++) {
3629 			if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
3630 				warnx("gelf_getphdr failed: %s",
3631 				    elf_errmsg(-1));
3632 				continue;
3633 			}
3634 			if (phdr.p_type == PT_NOTE) {
3635 				if (phdr.p_offset >= filesize ||
3636 				    phdr.p_filesz > filesize - phdr.p_offset) {
3637 					warnx("invalid PHDR offset");
3638 					continue;
3639 				}
3640 				dump_notes_content(re, rawfile + phdr.p_offset,
3641 				    phdr.p_filesz, phdr.p_offset);
3642 			}
3643 		}
3644 
3645 	} else {
3646 		/*
3647 		 * For objects other than core files, Search for
3648 		 * SHT_NOTE sections.
3649 		 */
3650 		for (i = 0; (size_t) i < re->shnum; i++) {
3651 			s = &re->sl[i];
3652 			if (s->type == SHT_NOTE) {
3653 				(void) elf_errno();
3654 				if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3655 					elferr = elf_errno();
3656 					if (elferr != 0)
3657 						warnx("elf_getdata failed: %s",
3658 						    elf_errmsg(elferr));
3659 					continue;
3660 				}
3661 				dump_notes_content(re, d->d_buf, d->d_size,
3662 				    s->off);
3663 			}
3664 		}
3665 	}
3666 }
3667 
3668 static struct flag_desc note_feature_ctl_flags[] = {
3669 	{ NT_FREEBSD_FCTL_ASLR_DISABLE,		"ASLR_DISABLE" },
3670 	{ 0, NULL }
3671 };
3672 
3673 static void
3674 dump_notes_data(struct readelf *re, const char *name, uint32_t type,
3675     const char *buf, size_t sz)
3676 {
3677 	size_t i;
3678 	const uint32_t *ubuf;
3679 
3680 	/* Note data is at least 4-byte aligned. */
3681 	if (((uintptr_t)buf & 3) != 0) {
3682 		warnx("bad note data alignment");
3683 		goto unknown;
3684 	}
3685 	ubuf = (const uint32_t *)(const void *)buf;
3686 
3687 	if (strcmp(name, "FreeBSD") == 0) {
3688 		switch (type) {
3689 		case NT_FREEBSD_ABI_TAG:
3690 			if (sz != 4)
3691 				goto unknown;
3692 			printf("   ABI tag: %u\n", ubuf[0]);
3693 			return;
3694 		/* NT_FREEBSD_NOINIT_TAG carries no data, treat as unknown. */
3695 		case NT_FREEBSD_ARCH_TAG:
3696 			if (sz != 4)
3697 				goto unknown;
3698 			printf("   Arch tag: %x\n", ubuf[0]);
3699 			return;
3700 		case NT_FREEBSD_FEATURE_CTL:
3701 			if (sz != 4)
3702 				goto unknown;
3703 			printf("   Features:");
3704 			dump_flags(note_feature_ctl_flags, ubuf[0]);
3705 			return;
3706 		}
3707 	} else if (strcmp(name, "GNU") == 0) {
3708 		switch (type) {
3709 		case NT_GNU_PROPERTY_TYPE_0:
3710 			dump_gnu_property_type_0(re, buf, sz);
3711 			return;
3712 		}
3713 	}
3714 unknown:
3715 	printf("   description data:");
3716 	for (i = 0; i < sz; i++)
3717 		printf(" %02x", (unsigned char)buf[i]);
3718 	printf("\n");
3719 }
3720 
3721 static void
3722 dump_notes_content(struct readelf *re, const char *buf, size_t sz, off_t off)
3723 {
3724 	Elf_Note *note;
3725 	const char *end, *name;
3726 
3727 	printf("\nNotes at offset %#010jx with length %#010jx:\n",
3728 	    (uintmax_t) off, (uintmax_t) sz);
3729 	printf("  %-13s %-15s %s\n", "Owner", "Data size", "Description");
3730 	end = buf + sz;
3731 	while (buf < end) {
3732 		if (buf + sizeof(*note) > end) {
3733 			warnx("invalid note header");
3734 			return;
3735 		}
3736 		note = (Elf_Note *)(uintptr_t) buf;
3737 		buf += sizeof(Elf_Note);
3738 		name = buf;
3739 		buf += roundup2(note->n_namesz, 4);
3740 		/*
3741 		 * The name field is required to be nul-terminated, and
3742 		 * n_namesz includes the terminating nul in observed
3743 		 * implementations (contrary to the ELF-64 spec). A special
3744 		 * case is needed for cores generated by some older Linux
3745 		 * versions, which write a note named "CORE" without a nul
3746 		 * terminator and n_namesz = 4.
3747 		 */
3748 		if (note->n_namesz == 0)
3749 			name = "";
3750 		else if (note->n_namesz == 4 && strncmp(name, "CORE", 4) == 0)
3751 			name = "CORE";
3752 		else if (strnlen(name, note->n_namesz) >= note->n_namesz)
3753 			name = "<invalid>";
3754 		printf("  %-13s %#010jx", name, (uintmax_t) note->n_descsz);
3755 		printf("      %s\n", note_type(name, re->ehdr.e_type,
3756 		    note->n_type));
3757 		dump_notes_data(re, name, note->n_type, buf, note->n_descsz);
3758 		buf += roundup2(note->n_descsz, 4);
3759 	}
3760 }
3761 
3762 /*
3763  * Symbol versioning sections are the same for 32bit and 64bit
3764  * ELF objects.
3765  */
3766 #define Elf_Verdef	Elf32_Verdef
3767 #define	Elf_Verdaux	Elf32_Verdaux
3768 #define	Elf_Verneed	Elf32_Verneed
3769 #define	Elf_Vernaux	Elf32_Vernaux
3770 
3771 #define	SAVE_VERSION_NAME(x, n, t)					\
3772 	do {								\
3773 		while (x >= re->ver_sz) {				\
3774 			nv = realloc(re->ver,				\
3775 			    sizeof(*re->ver) * re->ver_sz * 2);		\
3776 			if (nv == NULL) {				\
3777 				warn("realloc failed");			\
3778 				free(re->ver);				\
3779 				return;					\
3780 			}						\
3781 			re->ver = nv;					\
3782 			for (i = re->ver_sz; i < re->ver_sz * 2; i++) {	\
3783 				re->ver[i].name = NULL;			\
3784 				re->ver[i].type = 0;			\
3785 			}						\
3786 			re->ver_sz *= 2;				\
3787 		}							\
3788 		if (x > 1) {						\
3789 			re->ver[x].name = n;				\
3790 			re->ver[x].type = t;				\
3791 		}							\
3792 	} while (0)
3793 
3794 
3795 static void
3796 dump_verdef(struct readelf *re, int dump)
3797 {
3798 	struct section *s;
3799 	struct symver *nv;
3800 	Elf_Data *d;
3801 	Elf_Verdef *vd;
3802 	Elf_Verdaux *vda;
3803 	uint8_t *buf, *end, *buf2;
3804 	const char *name;
3805 	int elferr, i, j;
3806 
3807 	if ((s = re->vd_s) == NULL)
3808 		return;
3809 	if (s->link >= re->shnum)
3810 		return;
3811 
3812 	if (re->ver == NULL) {
3813 		re->ver_sz = 16;
3814 		if ((re->ver = calloc(re->ver_sz, sizeof(*re->ver))) ==
3815 		    NULL) {
3816 			warn("calloc failed");
3817 			return;
3818 		}
3819 		re->ver[0].name = "*local*";
3820 		re->ver[1].name = "*global*";
3821 	}
3822 
3823 	if (dump)
3824 		printf("\nVersion definition section (%s):\n", s->name);
3825 	(void) elf_errno();
3826 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3827 		elferr = elf_errno();
3828 		if (elferr != 0)
3829 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
3830 		return;
3831 	}
3832 	if (d->d_size == 0)
3833 		return;
3834 
3835 	buf = d->d_buf;
3836 	end = buf + d->d_size;
3837 	while (buf + sizeof(Elf_Verdef) <= end) {
3838 		vd = (Elf_Verdef *) (uintptr_t) buf;
3839 		if (dump) {
3840 			printf("  0x%4.4lx", (unsigned long)
3841 			    (buf - (uint8_t *)d->d_buf));
3842 			printf(" vd_version: %u vd_flags: %d"
3843 			    " vd_ndx: %u vd_cnt: %u", vd->vd_version,
3844 			    vd->vd_flags, vd->vd_ndx, vd->vd_cnt);
3845 		}
3846 		buf2 = buf + vd->vd_aux;
3847 		j = 0;
3848 		while (buf2 + sizeof(Elf_Verdaux) <= end && j < vd->vd_cnt) {
3849 			vda = (Elf_Verdaux *) (uintptr_t) buf2;
3850 			name = get_string(re, s->link, vda->vda_name);
3851 			if (j == 0) {
3852 				if (dump)
3853 					printf(" vda_name: %s\n", name);
3854 				SAVE_VERSION_NAME((int)vd->vd_ndx, name, 1);
3855 			} else if (dump)
3856 				printf("  0x%4.4lx parent: %s\n",
3857 				    (unsigned long) (buf2 -
3858 				    (uint8_t *)d->d_buf), name);
3859 			if (vda->vda_next == 0)
3860 				break;
3861 			buf2 += vda->vda_next;
3862 			j++;
3863 		}
3864 		if (vd->vd_next == 0)
3865 			break;
3866 		buf += vd->vd_next;
3867 	}
3868 }
3869 
3870 static void
3871 dump_verneed(struct readelf *re, int dump)
3872 {
3873 	struct section *s;
3874 	struct symver *nv;
3875 	Elf_Data *d;
3876 	Elf_Verneed *vn;
3877 	Elf_Vernaux *vna;
3878 	uint8_t *buf, *end, *buf2;
3879 	const char *name;
3880 	int elferr, i, j;
3881 
3882 	if ((s = re->vn_s) == NULL)
3883 		return;
3884 	if (s->link >= re->shnum)
3885 		return;
3886 
3887 	if (re->ver == NULL) {
3888 		re->ver_sz = 16;
3889 		if ((re->ver = calloc(re->ver_sz, sizeof(*re->ver))) ==
3890 		    NULL) {
3891 			warn("calloc failed");
3892 			return;
3893 		}
3894 		re->ver[0].name = "*local*";
3895 		re->ver[1].name = "*global*";
3896 	}
3897 
3898 	if (dump)
3899 		printf("\nVersion needed section (%s):\n", s->name);
3900 	(void) elf_errno();
3901 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3902 		elferr = elf_errno();
3903 		if (elferr != 0)
3904 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
3905 		return;
3906 	}
3907 	if (d->d_size == 0)
3908 		return;
3909 
3910 	buf = d->d_buf;
3911 	end = buf + d->d_size;
3912 	while (buf + sizeof(Elf_Verneed) <= end) {
3913 		vn = (Elf_Verneed *) (uintptr_t) buf;
3914 		if (dump) {
3915 			printf("  0x%4.4lx", (unsigned long)
3916 			    (buf - (uint8_t *)d->d_buf));
3917 			printf(" vn_version: %u vn_file: %s vn_cnt: %u\n",
3918 			    vn->vn_version,
3919 			    get_string(re, s->link, vn->vn_file),
3920 			    vn->vn_cnt);
3921 		}
3922 		buf2 = buf + vn->vn_aux;
3923 		j = 0;
3924 		while (buf2 + sizeof(Elf_Vernaux) <= end && j < vn->vn_cnt) {
3925 			vna = (Elf32_Vernaux *) (uintptr_t) buf2;
3926 			if (dump)
3927 				printf("  0x%4.4lx", (unsigned long)
3928 				    (buf2 - (uint8_t *)d->d_buf));
3929 			name = get_string(re, s->link, vna->vna_name);
3930 			if (dump)
3931 				printf("   vna_name: %s vna_flags: %u"
3932 				    " vna_other: %u\n", name,
3933 				    vna->vna_flags, vna->vna_other);
3934 			SAVE_VERSION_NAME((int)vna->vna_other, name, 0);
3935 			if (vna->vna_next == 0)
3936 				break;
3937 			buf2 += vna->vna_next;
3938 			j++;
3939 		}
3940 		if (vn->vn_next == 0)
3941 			break;
3942 		buf += vn->vn_next;
3943 	}
3944 }
3945 
3946 static void
3947 dump_versym(struct readelf *re)
3948 {
3949 	int i;
3950 	uint16_t vs;
3951 
3952 	if (re->vs_s == NULL || re->ver == NULL || re->vs == NULL)
3953 		return;
3954 	printf("\nVersion symbol section (%s):\n", re->vs_s->name);
3955 	for (i = 0; i < re->vs_sz; i++) {
3956 		if ((i & 3) == 0) {
3957 			if (i > 0)
3958 				putchar('\n');
3959 			printf("  %03x:", i);
3960 		}
3961 		vs = re->vs[i] & VERSYM_VERSION;
3962 		if (vs >= re->ver_sz || re->ver[vs].name == NULL) {
3963 			warnx("invalid versym version index %u", re->vs[i]);
3964 			break;
3965 		}
3966 		if (re->vs[i] & VERSYM_HIDDEN)
3967 			printf(" %3xh %-12s ", vs,
3968 			    re->ver[re->vs[i] & VERSYM_VERSION].name);
3969 		else
3970 			printf(" %3x %-12s ", vs, re->ver[re->vs[i]].name);
3971 	}
3972 	putchar('\n');
3973 }
3974 
3975 static void
3976 dump_ver(struct readelf *re)
3977 {
3978 
3979 	if (re->vs_s && re->ver && re->vs)
3980 		dump_versym(re);
3981 	if (re->vd_s)
3982 		dump_verdef(re, 1);
3983 	if (re->vn_s)
3984 		dump_verneed(re, 1);
3985 }
3986 
3987 static void
3988 search_ver(struct readelf *re)
3989 {
3990 	struct section *s;
3991 	Elf_Data *d;
3992 	int elferr, i;
3993 
3994 	for (i = 0; (size_t) i < re->shnum; i++) {
3995 		s = &re->sl[i];
3996 		if (s->type == SHT_SUNW_versym)
3997 			re->vs_s = s;
3998 		if (s->type == SHT_SUNW_verneed)
3999 			re->vn_s = s;
4000 		if (s->type == SHT_SUNW_verdef)
4001 			re->vd_s = s;
4002 	}
4003 	if (re->vd_s)
4004 		dump_verdef(re, 0);
4005 	if (re->vn_s)
4006 		dump_verneed(re, 0);
4007 	if (re->vs_s && re->ver != NULL) {
4008 		(void) elf_errno();
4009 		if ((d = elf_getdata(re->vs_s->scn, NULL)) == NULL) {
4010 			elferr = elf_errno();
4011 			if (elferr != 0)
4012 				warnx("elf_getdata failed: %s",
4013 				    elf_errmsg(elferr));
4014 			return;
4015 		}
4016 		if (d->d_size == 0)
4017 			return;
4018 		re->vs = d->d_buf;
4019 		re->vs_sz = d->d_size / sizeof(Elf32_Half);
4020 	}
4021 }
4022 
4023 #undef	Elf_Verdef
4024 #undef	Elf_Verdaux
4025 #undef	Elf_Verneed
4026 #undef	Elf_Vernaux
4027 #undef	SAVE_VERSION_NAME
4028 
4029 /*
4030  * Elf32_Lib and Elf64_Lib are identical.
4031  */
4032 #define	Elf_Lib		Elf32_Lib
4033 
4034 static void
4035 dump_liblist(struct readelf *re)
4036 {
4037 	struct section *s;
4038 	struct tm *t;
4039 	time_t ti;
4040 	char tbuf[20];
4041 	Elf_Data *d;
4042 	Elf_Lib *lib;
4043 	int i, j, k, elferr, first, len;
4044 
4045 	for (i = 0; (size_t) i < re->shnum; i++) {
4046 		s = &re->sl[i];
4047 		if (s->type != SHT_GNU_LIBLIST)
4048 			continue;
4049 		if (s->link >= re->shnum)
4050 			continue;
4051 		(void) elf_errno();
4052 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
4053 			elferr = elf_errno();
4054 			if (elferr != 0)
4055 				warnx("elf_getdata failed: %s",
4056 				    elf_errmsg(elferr));
4057 			continue;
4058 		}
4059 		if (d->d_size <= 0)
4060 			continue;
4061 		lib = d->d_buf;
4062 		if (!get_ent_count(s, &len))
4063 			continue;
4064 		printf("\nLibrary list section '%s' ", s->name);
4065 		printf("contains %d entries:\n", len);
4066 		printf("%12s%24s%18s%10s%6s\n", "Library", "Time Stamp",
4067 		    "Checksum", "Version", "Flags");
4068 		for (j = 0; (uint64_t) j < s->sz / s->entsize; j++) {
4069 			printf("%3d: ", j);
4070 			printf("%-20.20s ",
4071 			    get_string(re, s->link, lib->l_name));
4072 			ti = lib->l_time_stamp;
4073 			t = gmtime(&ti);
4074 			snprintf(tbuf, sizeof(tbuf), "%04d-%02d-%02dT%02d:%02d"
4075 			    ":%2d", t->tm_year + 1900, t->tm_mon + 1,
4076 			    t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
4077 			printf("%-19.19s ", tbuf);
4078 			printf("0x%08x ", lib->l_checksum);
4079 			printf("%-7d %#x", lib->l_version, lib->l_flags);
4080 			if (lib->l_flags != 0) {
4081 				first = 1;
4082 				putchar('(');
4083 				for (k = 0; l_flag[k].name != NULL; k++) {
4084 					if ((l_flag[k].value & lib->l_flags) ==
4085 					    0)
4086 						continue;
4087 					if (!first)
4088 						putchar(',');
4089 					else
4090 						first = 0;
4091 					printf("%s", l_flag[k].name);
4092 				}
4093 				putchar(')');
4094 			}
4095 			putchar('\n');
4096 			lib++;
4097 		}
4098 	}
4099 }
4100 
4101 #undef Elf_Lib
4102 
4103 static void
4104 dump_section_groups(struct readelf *re)
4105 {
4106 	struct section *s;
4107 	const char *symname;
4108 	Elf_Data *d;
4109 	uint32_t *w;
4110 	int i, j, elferr;
4111 	size_t n;
4112 
4113 	for (i = 0; (size_t) i < re->shnum; i++) {
4114 		s = &re->sl[i];
4115 		if (s->type != SHT_GROUP)
4116 			continue;
4117 		if (s->link >= re->shnum)
4118 			continue;
4119 		(void) elf_errno();
4120 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
4121 			elferr = elf_errno();
4122 			if (elferr != 0)
4123 				warnx("elf_getdata failed: %s",
4124 				    elf_errmsg(elferr));
4125 			continue;
4126 		}
4127 		if (d->d_size <= 0)
4128 			continue;
4129 
4130 		w = d->d_buf;
4131 
4132 		/* We only support COMDAT section. */
4133 #ifndef GRP_COMDAT
4134 #define	GRP_COMDAT 0x1
4135 #endif
4136 		if ((*w++ & GRP_COMDAT) == 0)
4137 			return;
4138 
4139 		if (s->entsize == 0)
4140 			s->entsize = 4;
4141 
4142 		symname = get_symbol_name(re, s->link, s->info);
4143 		n = s->sz / s->entsize;
4144 		if (n-- < 1)
4145 			return;
4146 
4147 		printf("\nCOMDAT group section [%5d] `%s' [%s] contains %ju"
4148 		    " sections:\n", i, s->name, symname, (uintmax_t)n);
4149 		printf("   %-10.10s %s\n", "[Index]", "Name");
4150 		for (j = 0; (size_t) j < n; j++, w++) {
4151 			if (*w >= re->shnum) {
4152 				warnx("invalid section index: %u", *w);
4153 				continue;
4154 			}
4155 			printf("   [%5u]   %s\n", *w, re->sl[*w].name);
4156 		}
4157 	}
4158 }
4159 
4160 static uint8_t *
4161 dump_unknown_tag(uint64_t tag, uint8_t *p, uint8_t *pe)
4162 {
4163 	uint64_t val;
4164 
4165 	/*
4166 	 * According to ARM EABI: For tags > 32, even numbered tags have
4167 	 * a ULEB128 param and odd numbered ones have NUL-terminated
4168 	 * string param. This rule probably also applies for tags <= 32
4169 	 * if the object arch is not ARM.
4170 	 */
4171 
4172 	printf("  Tag_unknown_%ju: ", (uintmax_t) tag);
4173 
4174 	if (tag & 1) {
4175 		printf("%s\n", (char *) p);
4176 		p += strlen((char *) p) + 1;
4177 	} else {
4178 		val = _decode_uleb128(&p, pe);
4179 		printf("%ju\n", (uintmax_t) val);
4180 	}
4181 
4182 	return (p);
4183 }
4184 
4185 static uint8_t *
4186 dump_compatibility_tag(uint8_t *p, uint8_t *pe)
4187 {
4188 	uint64_t val;
4189 
4190 	val = _decode_uleb128(&p, pe);
4191 	printf("flag = %ju, vendor = %s\n", (uintmax_t) val, p);
4192 	p += strlen((char *) p) + 1;
4193 
4194 	return (p);
4195 }
4196 
4197 static void
4198 dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe)
4199 {
4200 	uint64_t tag, val;
4201 	size_t i;
4202 	int found, desc;
4203 
4204 	(void) re;
4205 
4206 	while (p < pe) {
4207 		tag = _decode_uleb128(&p, pe);
4208 		found = desc = 0;
4209 		for (i = 0; i < sizeof(aeabi_tags) / sizeof(aeabi_tags[0]);
4210 		     i++) {
4211 			if (tag == aeabi_tags[i].tag) {
4212 				found = 1;
4213 				printf("  %s: ", aeabi_tags[i].s_tag);
4214 				if (aeabi_tags[i].get_desc) {
4215 					desc = 1;
4216 					val = _decode_uleb128(&p, pe);
4217 					printf("%s\n",
4218 					    aeabi_tags[i].get_desc(val));
4219 				}
4220 				break;
4221 			}
4222 			if (tag < aeabi_tags[i].tag)
4223 				break;
4224 		}
4225 		if (!found) {
4226 			p = dump_unknown_tag(tag, p, pe);
4227 			continue;
4228 		}
4229 		if (desc)
4230 			continue;
4231 
4232 		switch (tag) {
4233 		case 4:		/* Tag_CPU_raw_name */
4234 		case 5:		/* Tag_CPU_name */
4235 		case 67:	/* Tag_conformance */
4236 			printf("%s\n", (char *) p);
4237 			p += strlen((char *) p) + 1;
4238 			break;
4239 		case 32:	/* Tag_compatibility */
4240 			p = dump_compatibility_tag(p, pe);
4241 			break;
4242 		case 64:	/* Tag_nodefaults */
4243 			/* ignored, written as 0. */
4244 			(void) _decode_uleb128(&p, pe);
4245 			printf("True\n");
4246 			break;
4247 		case 65:	/* Tag_also_compatible_with */
4248 			val = _decode_uleb128(&p, pe);
4249 			/* Must be Tag_CPU_arch */
4250 			if (val != 6) {
4251 				printf("unknown\n");
4252 				break;
4253 			}
4254 			val = _decode_uleb128(&p, pe);
4255 			printf("%s\n", aeabi_cpu_arch(val));
4256 			/* Skip NUL terminator. */
4257 			p++;
4258 			break;
4259 		default:
4260 			putchar('\n');
4261 			break;
4262 		}
4263 	}
4264 }
4265 
4266 #ifndef	Tag_GNU_MIPS_ABI_FP
4267 #define	Tag_GNU_MIPS_ABI_FP	4
4268 #endif
4269 
4270 static void
4271 dump_mips_attributes(struct readelf *re, uint8_t *p, uint8_t *pe)
4272 {
4273 	uint64_t tag, val;
4274 
4275 	(void) re;
4276 
4277 	while (p < pe) {
4278 		tag = _decode_uleb128(&p, pe);
4279 		switch (tag) {
4280 		case Tag_GNU_MIPS_ABI_FP:
4281 			val = _decode_uleb128(&p, pe);
4282 			printf("  Tag_GNU_MIPS_ABI_FP: %s\n", mips_abi_fp(val));
4283 			break;
4284 		case 32:	/* Tag_compatibility */
4285 			p = dump_compatibility_tag(p, pe);
4286 			break;
4287 		default:
4288 			p = dump_unknown_tag(tag, p, pe);
4289 			break;
4290 		}
4291 	}
4292 }
4293 
4294 #ifndef Tag_GNU_Power_ABI_FP
4295 #define	Tag_GNU_Power_ABI_FP	4
4296 #endif
4297 
4298 #ifndef Tag_GNU_Power_ABI_Vector
4299 #define	Tag_GNU_Power_ABI_Vector	8
4300 #endif
4301 
4302 static void
4303 dump_ppc_attributes(uint8_t *p, uint8_t *pe)
4304 {
4305 	uint64_t tag, val;
4306 
4307 	while (p < pe) {
4308 		tag = _decode_uleb128(&p, pe);
4309 		switch (tag) {
4310 		case Tag_GNU_Power_ABI_FP:
4311 			val = _decode_uleb128(&p, pe);
4312 			printf("  Tag_GNU_Power_ABI_FP: %s\n", ppc_abi_fp(val));
4313 			break;
4314 		case Tag_GNU_Power_ABI_Vector:
4315 			val = _decode_uleb128(&p, pe);
4316 			printf("  Tag_GNU_Power_ABI_Vector: %s\n",
4317 			    ppc_abi_vector(val));
4318 			break;
4319 		case 32:	/* Tag_compatibility */
4320 			p = dump_compatibility_tag(p, pe);
4321 			break;
4322 		default:
4323 			p = dump_unknown_tag(tag, p, pe);
4324 			break;
4325 		}
4326 	}
4327 }
4328 
4329 static void
4330 dump_attributes(struct readelf *re)
4331 {
4332 	struct section *s;
4333 	Elf_Data *d;
4334 	uint8_t *p, *pe, *sp;
4335 	size_t len, seclen, nlen, sublen;
4336 	uint64_t val;
4337 	int tag, i, elferr;
4338 
4339 	for (i = 0; (size_t) i < re->shnum; i++) {
4340 		s = &re->sl[i];
4341 		if (s->type != SHT_GNU_ATTRIBUTES &&
4342 		    (re->ehdr.e_machine != EM_ARM || s->type != SHT_LOPROC + 3))
4343 			continue;
4344 		(void) elf_errno();
4345 		if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
4346 			elferr = elf_errno();
4347 			if (elferr != 0)
4348 				warnx("elf_rawdata failed: %s",
4349 				    elf_errmsg(elferr));
4350 			continue;
4351 		}
4352 		if (d->d_size <= 0)
4353 			continue;
4354 		p = d->d_buf;
4355 		pe = p + d->d_size;
4356 		if (*p != 'A') {
4357 			printf("Unknown Attribute Section Format: %c\n",
4358 			    (char) *p);
4359 			continue;
4360 		}
4361 		len = d->d_size - 1;
4362 		p++;
4363 		while (len > 0) {
4364 			if (len < 4) {
4365 				warnx("truncated attribute section length");
4366 				return;
4367 			}
4368 			seclen = re->dw_decode(&p, 4);
4369 			if (seclen > len) {
4370 				warnx("invalid attribute section length");
4371 				return;
4372 			}
4373 			len -= seclen;
4374 			nlen = strlen((char *) p) + 1;
4375 			if (nlen + 4 > seclen) {
4376 				warnx("invalid attribute section name");
4377 				return;
4378 			}
4379 			printf("Attribute Section: %s\n", (char *) p);
4380 			p += nlen;
4381 			seclen -= nlen + 4;
4382 			while (seclen > 0) {
4383 				sp = p;
4384 				tag = *p++;
4385 				sublen = re->dw_decode(&p, 4);
4386 				if (sublen > seclen) {
4387 					warnx("invalid attribute sub-section"
4388 					    " length");
4389 					return;
4390 				}
4391 				seclen -= sublen;
4392 				printf("%s", top_tag(tag));
4393 				if (tag == 2 || tag == 3) {
4394 					putchar(':');
4395 					for (;;) {
4396 						val = _decode_uleb128(&p, pe);
4397 						if (val == 0)
4398 							break;
4399 						printf(" %ju", (uintmax_t) val);
4400 					}
4401 				}
4402 				putchar('\n');
4403 				if (re->ehdr.e_machine == EM_ARM &&
4404 				    s->type == SHT_LOPROC + 3)
4405 					dump_arm_attributes(re, p, sp + sublen);
4406 				else if (re->ehdr.e_machine == EM_MIPS ||
4407 				    re->ehdr.e_machine == EM_MIPS_RS3_LE)
4408 					dump_mips_attributes(re, p,
4409 					    sp + sublen);
4410 				else if (re->ehdr.e_machine == EM_PPC)
4411 					dump_ppc_attributes(p, sp + sublen);
4412 				p = sp + sublen;
4413 			}
4414 		}
4415 	}
4416 }
4417 
4418 static void
4419 dump_mips_specific_info(struct readelf *re)
4420 {
4421 	struct section *s;
4422 	int i;
4423 
4424 	s = NULL;
4425 	for (i = 0; (size_t) i < re->shnum; i++) {
4426 		s = &re->sl[i];
4427 		if (s->name != NULL && (!strcmp(s->name, ".MIPS.options") ||
4428 		    (s->type == SHT_MIPS_OPTIONS))) {
4429 			dump_mips_options(re, s);
4430 		}
4431 	}
4432 
4433 	if (s->name != NULL && (!strcmp(s->name, ".MIPS.abiflags") ||
4434 	    (s->type == SHT_MIPS_ABIFLAGS)))
4435 		dump_mips_abiflags(re, s);
4436 
4437 	/*
4438 	 * Dump .reginfo if present (although it will be ignored by an OS if a
4439 	 * .MIPS.options section is present, according to SGI mips64 spec).
4440 	 */
4441 	for (i = 0; (size_t) i < re->shnum; i++) {
4442 		s = &re->sl[i];
4443 		if (s->name != NULL && (!strcmp(s->name, ".reginfo") ||
4444 		    (s->type == SHT_MIPS_REGINFO)))
4445 			dump_mips_reginfo(re, s);
4446 	}
4447 }
4448 
4449 static void
4450 dump_mips_abiflags(struct readelf *re, struct section *s)
4451 {
4452 	Elf_Data *d;
4453 	uint8_t *p;
4454 	int elferr;
4455 	uint32_t isa_ext, ases, flags1, flags2;
4456 	uint16_t version;
4457 	uint8_t isa_level, isa_rev, gpr_size, cpr1_size, cpr2_size, fp_abi;
4458 
4459 	if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
4460 		elferr = elf_errno();
4461 		if (elferr != 0)
4462 			warnx("elf_rawdata failed: %s",
4463 			    elf_errmsg(elferr));
4464 		return;
4465 	}
4466 	if (d->d_size != 24) {
4467 		warnx("invalid MIPS abiflags section size");
4468 		return;
4469 	}
4470 
4471 	p = d->d_buf;
4472 	version = re->dw_decode(&p, 2);
4473 	printf("MIPS ABI Flags Version: %u", version);
4474 	if (version != 0) {
4475 		printf(" (unknown)\n\n");
4476 		return;
4477 	}
4478 	printf("\n\n");
4479 
4480 	isa_level = re->dw_decode(&p, 1);
4481 	isa_rev = re->dw_decode(&p, 1);
4482 	gpr_size = re->dw_decode(&p, 1);
4483 	cpr1_size = re->dw_decode(&p, 1);
4484 	cpr2_size = re->dw_decode(&p, 1);
4485 	fp_abi = re->dw_decode(&p, 1);
4486 	isa_ext = re->dw_decode(&p, 4);
4487 	ases = re->dw_decode(&p, 4);
4488 	flags1 = re->dw_decode(&p, 4);
4489 	flags2 = re->dw_decode(&p, 4);
4490 
4491 	printf("ISA: ");
4492 	if (isa_rev <= 1)
4493 		printf("MIPS%u\n", isa_level);
4494 	else
4495 		printf("MIPS%ur%u\n", isa_level, isa_rev);
4496 	printf("GPR size: %d\n", get_mips_register_size(gpr_size));
4497 	printf("CPR1 size: %d\n", get_mips_register_size(cpr1_size));
4498 	printf("CPR2 size: %d\n", get_mips_register_size(cpr2_size));
4499 	printf("FP ABI: ");
4500 	switch (fp_abi) {
4501 	case 3:
4502 		printf("Soft float");
4503 		break;
4504 	default:
4505 		printf("%u", fp_abi);
4506 		break;
4507 	}
4508 	printf("\nISA Extension: %u\n", isa_ext);
4509 	printf("ASEs: %u\n", ases);
4510 	printf("FLAGS 1: %08x\n", flags1);
4511 	printf("FLAGS 2: %08x\n", flags2);
4512 }
4513 
4514 static int
4515 get_mips_register_size(uint8_t flag)
4516 {
4517 	switch (flag) {
4518 	case 0: return 0;
4519 	case 1: return 32;
4520 	case 2: return 64;
4521 	case 3: return 128;
4522 	default: return -1;
4523 	}
4524 }
4525 static void
4526 dump_mips_reginfo(struct readelf *re, struct section *s)
4527 {
4528 	Elf_Data *d;
4529 	int elferr, len;
4530 
4531 	(void) elf_errno();
4532 	if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
4533 		elferr = elf_errno();
4534 		if (elferr != 0)
4535 			warnx("elf_rawdata failed: %s",
4536 			    elf_errmsg(elferr));
4537 		return;
4538 	}
4539 	if (d->d_size <= 0)
4540 		return;
4541 	if (!get_ent_count(s, &len))
4542 		return;
4543 
4544 	printf("\nSection '%s' contains %d entries:\n", s->name, len);
4545 	dump_mips_odk_reginfo(re, d->d_buf, d->d_size);
4546 }
4547 
4548 static void
4549 dump_mips_options(struct readelf *re, struct section *s)
4550 {
4551 	Elf_Data *d;
4552 	uint32_t info;
4553 	uint16_t sndx;
4554 	uint8_t *p, *pe;
4555 	uint8_t kind, size;
4556 	int elferr;
4557 
4558 	(void) elf_errno();
4559 	if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
4560 		elferr = elf_errno();
4561 		if (elferr != 0)
4562 			warnx("elf_rawdata failed: %s",
4563 			    elf_errmsg(elferr));
4564 		return;
4565 	}
4566 	if (d->d_size == 0)
4567 		return;
4568 
4569 	printf("\nSection %s contains:\n", s->name);
4570 	p = d->d_buf;
4571 	pe = p + d->d_size;
4572 	while (p < pe) {
4573 		if (pe - p < 8) {
4574 			warnx("Truncated MIPS option header");
4575 			return;
4576 		}
4577 		kind = re->dw_decode(&p, 1);
4578 		size = re->dw_decode(&p, 1);
4579 		sndx = re->dw_decode(&p, 2);
4580 		info = re->dw_decode(&p, 4);
4581 		if (size < 8 || size - 8 > pe - p) {
4582 			warnx("Malformed MIPS option header");
4583 			return;
4584 		}
4585 		size -= 8;
4586 		switch (kind) {
4587 		case ODK_REGINFO:
4588 			dump_mips_odk_reginfo(re, p, size);
4589 			break;
4590 		case ODK_EXCEPTIONS:
4591 			printf(" EXCEPTIONS FPU_MIN: %#x\n",
4592 			    info & OEX_FPU_MIN);
4593 			printf("%11.11s FPU_MAX: %#x\n", "",
4594 			    info & OEX_FPU_MAX);
4595 			dump_mips_option_flags("", mips_exceptions_option,
4596 			    info);
4597 			break;
4598 		case ODK_PAD:
4599 			printf(" %-10.10s section: %ju\n", "OPAD",
4600 			    (uintmax_t) sndx);
4601 			dump_mips_option_flags("", mips_pad_option, info);
4602 			break;
4603 		case ODK_HWPATCH:
4604 			dump_mips_option_flags("HWPATCH", mips_hwpatch_option,
4605 			    info);
4606 			break;
4607 		case ODK_HWAND:
4608 			dump_mips_option_flags("HWAND", mips_hwa_option, info);
4609 			break;
4610 		case ODK_HWOR:
4611 			dump_mips_option_flags("HWOR", mips_hwo_option, info);
4612 			break;
4613 		case ODK_FILL:
4614 			printf(" %-10.10s %#jx\n", "FILL", (uintmax_t) info);
4615 			break;
4616 		case ODK_TAGS:
4617 			printf(" %-10.10s\n", "TAGS");
4618 			break;
4619 		case ODK_GP_GROUP:
4620 			printf(" %-10.10s GP group number: %#x\n", "GP_GROUP",
4621 			    info & 0xFFFF);
4622 			if (info & 0x10000)
4623 				printf(" %-10.10s GP group is "
4624 				    "self-contained\n", "");
4625 			break;
4626 		case ODK_IDENT:
4627 			printf(" %-10.10s default GP group number: %#x\n",
4628 			    "IDENT", info & 0xFFFF);
4629 			if (info & 0x10000)
4630 				printf(" %-10.10s default GP group is "
4631 				    "self-contained\n", "");
4632 			break;
4633 		case ODK_PAGESIZE:
4634 			printf(" %-10.10s\n", "PAGESIZE");
4635 			break;
4636 		default:
4637 			break;
4638 		}
4639 		p += size;
4640 	}
4641 }
4642 
4643 static void
4644 dump_mips_option_flags(const char *name, struct mips_option *opt, uint64_t info)
4645 {
4646 	int first;
4647 
4648 	first = 1;
4649 	for (; opt->desc != NULL; opt++) {
4650 		if (info & opt->flag) {
4651 			printf(" %-10.10s %s\n", first ? name : "",
4652 			    opt->desc);
4653 			first = 0;
4654 		}
4655 	}
4656 }
4657 
4658 static void
4659 dump_mips_odk_reginfo(struct readelf *re, uint8_t *p, size_t sz)
4660 {
4661 	uint32_t ri_gprmask;
4662 	uint32_t ri_cprmask[4];
4663 	uint64_t ri_gp_value;
4664 	uint8_t *pe;
4665 	int i;
4666 
4667 	pe = p + sz;
4668 	while (p < pe) {
4669 		ri_gprmask = re->dw_decode(&p, 4);
4670 		/* Skip ri_pad padding field for mips64. */
4671 		if (re->ec == ELFCLASS64)
4672 			re->dw_decode(&p, 4);
4673 		for (i = 0; i < 4; i++)
4674 			ri_cprmask[i] = re->dw_decode(&p, 4);
4675 		if (re->ec == ELFCLASS32)
4676 			ri_gp_value = re->dw_decode(&p, 4);
4677 		else
4678 			ri_gp_value = re->dw_decode(&p, 8);
4679 		printf(" %s    ", option_kind(ODK_REGINFO));
4680 		printf("ri_gprmask:    0x%08jx\n", (uintmax_t) ri_gprmask);
4681 		for (i = 0; i < 4; i++)
4682 			printf("%11.11s ri_cprmask[%d]: 0x%08jx\n", "", i,
4683 			    (uintmax_t) ri_cprmask[i]);
4684 		printf("%12.12s", "");
4685 		printf("ri_gp_value:   %#jx\n", (uintmax_t) ri_gp_value);
4686 	}
4687 }
4688 
4689 static void
4690 dump_arch_specific_info(struct readelf *re)
4691 {
4692 
4693 	dump_liblist(re);
4694 	dump_attributes(re);
4695 
4696 	switch (re->ehdr.e_machine) {
4697 	case EM_MIPS:
4698 	case EM_MIPS_RS3_LE:
4699 		dump_mips_specific_info(re);
4700 	default:
4701 		break;
4702 	}
4703 }
4704 
4705 static const char *
4706 dwarf_regname(struct readelf *re, unsigned int num)
4707 {
4708 	static char rx[32];
4709 	const char *rn;
4710 
4711 	if ((rn = dwarf_reg(re->ehdr.e_machine, num)) != NULL)
4712 		return (rn);
4713 
4714 	snprintf(rx, sizeof(rx), "r%u", num);
4715 
4716 	return (rx);
4717 }
4718 
4719 static void
4720 dump_dwarf_line(struct readelf *re)
4721 {
4722 	struct section *s;
4723 	Dwarf_Die die;
4724 	Dwarf_Error de;
4725 	Dwarf_Half tag, version, pointer_size;
4726 	Dwarf_Unsigned offset, endoff, length, hdrlen, dirndx, mtime, fsize;
4727 	Dwarf_Small minlen, defstmt, lrange, opbase, oplen;
4728 	Elf_Data *d;
4729 	char *pn;
4730 	uint64_t address, file, line, column, isa, opsize, udelta;
4731 	int64_t sdelta;
4732 	uint8_t *p, *pe;
4733 	int8_t lbase;
4734 	int i, is_stmt, dwarf_size, elferr, ret;
4735 
4736 	printf("\nDump of debug contents of section .debug_line:\n");
4737 
4738 	s = NULL;
4739 	for (i = 0; (size_t) i < re->shnum; i++) {
4740 		s = &re->sl[i];
4741 		if (s->name != NULL && !strcmp(s->name, ".debug_line"))
4742 			break;
4743 	}
4744 	if ((size_t) i >= re->shnum)
4745 		return;
4746 
4747 	(void) elf_errno();
4748 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
4749 		elferr = elf_errno();
4750 		if (elferr != 0)
4751 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
4752 		return;
4753 	}
4754 	if (d->d_size <= 0)
4755 		return;
4756 
4757 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
4758 	    NULL, &de)) ==  DW_DLV_OK) {
4759 		die = NULL;
4760 		while (dwarf_siblingof(re->dbg, die, &die, &de) == DW_DLV_OK) {
4761 			if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
4762 				warnx("dwarf_tag failed: %s",
4763 				    dwarf_errmsg(de));
4764 				return;
4765 			}
4766 			/* XXX: What about DW_TAG_partial_unit? */
4767 			if (tag == DW_TAG_compile_unit)
4768 				break;
4769 		}
4770 		if (die == NULL) {
4771 			warnx("could not find DW_TAG_compile_unit die");
4772 			return;
4773 		}
4774 		if (dwarf_attrval_unsigned(die, DW_AT_stmt_list, &offset,
4775 		    &de) != DW_DLV_OK)
4776 			continue;
4777 
4778 		length = re->dw_read(d, &offset, 4);
4779 		if (length == 0xffffffff) {
4780 			dwarf_size = 8;
4781 			length = re->dw_read(d, &offset, 8);
4782 		} else
4783 			dwarf_size = 4;
4784 
4785 		if (length > d->d_size - offset) {
4786 			warnx("invalid .dwarf_line section");
4787 			continue;
4788 		}
4789 
4790 		endoff = offset + length;
4791 		pe = (uint8_t *) d->d_buf + endoff;
4792 		version = re->dw_read(d, &offset, 2);
4793 		hdrlen = re->dw_read(d, &offset, dwarf_size);
4794 		minlen = re->dw_read(d, &offset, 1);
4795 		defstmt = re->dw_read(d, &offset, 1);
4796 		lbase = re->dw_read(d, &offset, 1);
4797 		lrange = re->dw_read(d, &offset, 1);
4798 		opbase = re->dw_read(d, &offset, 1);
4799 
4800 		printf("\n");
4801 		printf("  Length:\t\t\t%ju\n", (uintmax_t) length);
4802 		printf("  DWARF version:\t\t%u\n", version);
4803 		printf("  Prologue Length:\t\t%ju\n", (uintmax_t) hdrlen);
4804 		printf("  Minimum Instruction Length:\t%u\n", minlen);
4805 		printf("  Initial value of 'is_stmt':\t%u\n", defstmt);
4806 		printf("  Line Base:\t\t\t%d\n", lbase);
4807 		printf("  Line Range:\t\t\t%u\n", lrange);
4808 		printf("  Opcode Base:\t\t\t%u\n", opbase);
4809 		(void) dwarf_get_address_size(re->dbg, &pointer_size, &de);
4810 		printf("  (Pointer size:\t\t%u)\n", pointer_size);
4811 
4812 		printf("\n");
4813 		printf(" Opcodes:\n");
4814 		for (i = 1; i < opbase; i++) {
4815 			oplen = re->dw_read(d, &offset, 1);
4816 			printf("  Opcode %d has %u args\n", i, oplen);
4817 		}
4818 
4819 		printf("\n");
4820 		printf(" The Directory Table:\n");
4821 		p = (uint8_t *) d->d_buf + offset;
4822 		while (*p != '\0') {
4823 			printf("  %s\n", (char *) p);
4824 			p += strlen((char *) p) + 1;
4825 		}
4826 
4827 		p++;
4828 		printf("\n");
4829 		printf(" The File Name Table:\n");
4830 		printf("  Entry\tDir\tTime\tSize\tName\n");
4831 		i = 0;
4832 		while (*p != '\0') {
4833 			i++;
4834 			pn = (char *) p;
4835 			p += strlen(pn) + 1;
4836 			dirndx = _decode_uleb128(&p, pe);
4837 			mtime = _decode_uleb128(&p, pe);
4838 			fsize = _decode_uleb128(&p, pe);
4839 			printf("  %d\t%ju\t%ju\t%ju\t%s\n", i,
4840 			    (uintmax_t) dirndx, (uintmax_t) mtime,
4841 			    (uintmax_t) fsize, pn);
4842 		}
4843 
4844 #define	RESET_REGISTERS						\
4845 	do {							\
4846 		address	       = 0;				\
4847 		file	       = 1;				\
4848 		line	       = 1;				\
4849 		column	       = 0;				\
4850 		is_stmt	       = defstmt;			\
4851 	} while(0)
4852 
4853 #define	LINE(x) (lbase + (((x) - opbase) % lrange))
4854 #define	ADDRESS(x) ((((x) - opbase) / lrange) * minlen)
4855 
4856 		p++;
4857 		printf("\n");
4858 		printf(" Line Number Statements:\n");
4859 
4860 		RESET_REGISTERS;
4861 
4862 		while (p < pe) {
4863 
4864 			if (*p == 0) {
4865 				/*
4866 				 * Extended Opcodes.
4867 				 */
4868 				p++;
4869 				opsize = _decode_uleb128(&p, pe);
4870 				printf("  Extended opcode %u: ", *p);
4871 				switch (*p) {
4872 				case DW_LNE_end_sequence:
4873 					p++;
4874 					RESET_REGISTERS;
4875 					printf("End of Sequence\n");
4876 					break;
4877 				case DW_LNE_set_address:
4878 					p++;
4879 					address = re->dw_decode(&p,
4880 					    pointer_size);
4881 					printf("set Address to %#jx\n",
4882 					    (uintmax_t) address);
4883 					break;
4884 				case DW_LNE_define_file:
4885 					p++;
4886 					pn = (char *) p;
4887 					p += strlen(pn) + 1;
4888 					dirndx = _decode_uleb128(&p, pe);
4889 					mtime = _decode_uleb128(&p, pe);
4890 					fsize = _decode_uleb128(&p, pe);
4891 					printf("define new file: %s\n", pn);
4892 					break;
4893 				default:
4894 					/* Unrecognized extened opcodes. */
4895 					p += opsize;
4896 					printf("unknown opcode\n");
4897 				}
4898 			} else if (*p > 0 && *p < opbase) {
4899 				/*
4900 				 * Standard Opcodes.
4901 				 */
4902 				switch(*p++) {
4903 				case DW_LNS_copy:
4904 					printf("  Copy\n");
4905 					break;
4906 				case DW_LNS_advance_pc:
4907 					udelta = _decode_uleb128(&p, pe) *
4908 					    minlen;
4909 					address += udelta;
4910 					printf("  Advance PC by %ju to %#jx\n",
4911 					    (uintmax_t) udelta,
4912 					    (uintmax_t) address);
4913 					break;
4914 				case DW_LNS_advance_line:
4915 					sdelta = _decode_sleb128(&p, pe);
4916 					line += sdelta;
4917 					printf("  Advance Line by %jd to %ju\n",
4918 					    (intmax_t) sdelta,
4919 					    (uintmax_t) line);
4920 					break;
4921 				case DW_LNS_set_file:
4922 					file = _decode_uleb128(&p, pe);
4923 					printf("  Set File to %ju\n",
4924 					    (uintmax_t) file);
4925 					break;
4926 				case DW_LNS_set_column:
4927 					column = _decode_uleb128(&p, pe);
4928 					printf("  Set Column to %ju\n",
4929 					    (uintmax_t) column);
4930 					break;
4931 				case DW_LNS_negate_stmt:
4932 					is_stmt = !is_stmt;
4933 					printf("  Set is_stmt to %d\n", is_stmt);
4934 					break;
4935 				case DW_LNS_set_basic_block:
4936 					printf("  Set basic block flag\n");
4937 					break;
4938 				case DW_LNS_const_add_pc:
4939 					address += ADDRESS(255);
4940 					printf("  Advance PC by constant %ju"
4941 					    " to %#jx\n",
4942 					    (uintmax_t) ADDRESS(255),
4943 					    (uintmax_t) address);
4944 					break;
4945 				case DW_LNS_fixed_advance_pc:
4946 					udelta = re->dw_decode(&p, 2);
4947 					address += udelta;
4948 					printf("  Advance PC by fixed value "
4949 					    "%ju to %#jx\n",
4950 					    (uintmax_t) udelta,
4951 					    (uintmax_t) address);
4952 					break;
4953 				case DW_LNS_set_prologue_end:
4954 					printf("  Set prologue end flag\n");
4955 					break;
4956 				case DW_LNS_set_epilogue_begin:
4957 					printf("  Set epilogue begin flag\n");
4958 					break;
4959 				case DW_LNS_set_isa:
4960 					isa = _decode_uleb128(&p, pe);
4961 					printf("  Set isa to %ju\n",
4962 					    (uintmax_t) isa);
4963 					break;
4964 				default:
4965 					/* Unrecognized extended opcodes. */
4966 					printf("  Unknown extended opcode %u\n",
4967 					    *(p - 1));
4968 					break;
4969 				}
4970 
4971 			} else {
4972 				/*
4973 				 * Special Opcodes.
4974 				 */
4975 				line += LINE(*p);
4976 				address += ADDRESS(*p);
4977 				printf("  Special opcode %u: advance Address "
4978 				    "by %ju to %#jx and Line by %jd to %ju\n",
4979 				    *p - opbase, (uintmax_t) ADDRESS(*p),
4980 				    (uintmax_t) address, (intmax_t) LINE(*p),
4981 				    (uintmax_t) line);
4982 				p++;
4983 			}
4984 
4985 
4986 		}
4987 	}
4988 	if (ret == DW_DLV_ERROR)
4989 		warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
4990 
4991 #undef	RESET_REGISTERS
4992 #undef	LINE
4993 #undef	ADDRESS
4994 }
4995 
4996 static void
4997 dump_dwarf_line_decoded(struct readelf *re)
4998 {
4999 	Dwarf_Die die;
5000 	Dwarf_Line *linebuf, ln;
5001 	Dwarf_Addr lineaddr;
5002 	Dwarf_Signed linecount, srccount;
5003 	Dwarf_Unsigned lineno, fn;
5004 	Dwarf_Error de;
5005 	const char *dir, *file;
5006 	char **srcfiles;
5007 	int i, ret;
5008 
5009 	printf("Decoded dump of debug contents of section .debug_line:\n\n");
5010 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
5011 	    NULL, &de)) == DW_DLV_OK) {
5012 		if (dwarf_siblingof(re->dbg, NULL, &die, &de) != DW_DLV_OK)
5013 			continue;
5014 		if (dwarf_attrval_string(die, DW_AT_name, &file, &de) !=
5015 		    DW_DLV_OK)
5016 			file = NULL;
5017 		if (dwarf_attrval_string(die, DW_AT_comp_dir, &dir, &de) !=
5018 		    DW_DLV_OK)
5019 			dir = NULL;
5020 		printf("CU: ");
5021 		if (dir && file && file[0] != '/')
5022 			printf("%s/", dir);
5023 		if (file)
5024 			printf("%s", file);
5025 		putchar('\n');
5026 		printf("%-37s %11s   %s\n", "Filename", "Line Number",
5027 		    "Starting Address");
5028 		if (dwarf_srclines(die, &linebuf, &linecount, &de) != DW_DLV_OK)
5029 			continue;
5030 		if (dwarf_srcfiles(die, &srcfiles, &srccount, &de) != DW_DLV_OK)
5031 			continue;
5032 		for (i = 0; i < linecount; i++) {
5033 			ln = linebuf[i];
5034 			if (dwarf_line_srcfileno(ln, &fn, &de) != DW_DLV_OK)
5035 				continue;
5036 			if (dwarf_lineno(ln, &lineno, &de) != DW_DLV_OK)
5037 				continue;
5038 			if (dwarf_lineaddr(ln, &lineaddr, &de) != DW_DLV_OK)
5039 				continue;
5040 			printf("%-37s %11ju %#18jx\n",
5041 			    basename(srcfiles[fn - 1]), (uintmax_t) lineno,
5042 			    (uintmax_t) lineaddr);
5043 		}
5044 		putchar('\n');
5045 	}
5046 }
5047 
5048 static void
5049 dump_dwarf_die(struct readelf *re, Dwarf_Die die, int level)
5050 {
5051 	Dwarf_Attribute *attr_list;
5052 	Dwarf_Die ret_die;
5053 	Dwarf_Off dieoff, cuoff, culen, attroff;
5054 	Dwarf_Unsigned ate, lang, v_udata, v_sig;
5055 	Dwarf_Signed attr_count, v_sdata;
5056 	Dwarf_Off v_off;
5057 	Dwarf_Addr v_addr;
5058 	Dwarf_Half tag, attr, form;
5059 	Dwarf_Block *v_block;
5060 	Dwarf_Bool v_bool, is_info;
5061 	Dwarf_Sig8 v_sig8;
5062 	Dwarf_Error de;
5063 	Dwarf_Ptr v_expr;
5064 	const char *tag_str, *attr_str, *ate_str, *lang_str;
5065 	char unk_tag[32], unk_attr[32];
5066 	char *v_str;
5067 	uint8_t *b, *p;
5068 	int i, j, abc, ret;
5069 
5070 	if (dwarf_dieoffset(die, &dieoff, &de) != DW_DLV_OK) {
5071 		warnx("dwarf_dieoffset failed: %s", dwarf_errmsg(de));
5072 		goto cont_search;
5073 	}
5074 
5075 	printf(" <%d><%jx>: ", level, (uintmax_t) dieoff);
5076 
5077 	if (dwarf_die_CU_offset_range(die, &cuoff, &culen, &de) != DW_DLV_OK) {
5078 		warnx("dwarf_die_CU_offset_range failed: %s",
5079 		      dwarf_errmsg(de));
5080 		cuoff = 0;
5081 	}
5082 
5083 	abc = dwarf_die_abbrev_code(die);
5084 	if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
5085 		warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
5086 		goto cont_search;
5087 	}
5088 	if (dwarf_get_TAG_name(tag, &tag_str) != DW_DLV_OK) {
5089 		snprintf(unk_tag, sizeof(unk_tag), "[Unknown Tag: %#x]", tag);
5090 		tag_str = unk_tag;
5091 	}
5092 
5093 	printf("Abbrev Number: %d (%s)\n", abc, tag_str);
5094 
5095 	if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
5096 	    DW_DLV_OK) {
5097 		if (ret == DW_DLV_ERROR)
5098 			warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
5099 		goto cont_search;
5100 	}
5101 
5102 	for (i = 0; i < attr_count; i++) {
5103 		if (dwarf_whatform(attr_list[i], &form, &de) != DW_DLV_OK) {
5104 			warnx("dwarf_whatform failed: %s", dwarf_errmsg(de));
5105 			continue;
5106 		}
5107 		if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
5108 			warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
5109 			continue;
5110 		}
5111 		if (dwarf_get_AT_name(attr, &attr_str) != DW_DLV_OK) {
5112 			snprintf(unk_attr, sizeof(unk_attr),
5113 			    "[Unknown AT: %#x]", attr);
5114 			attr_str = unk_attr;
5115 		}
5116 		if (dwarf_attroffset(attr_list[i], &attroff, &de) !=
5117 		    DW_DLV_OK) {
5118 			warnx("dwarf_attroffset failed: %s", dwarf_errmsg(de));
5119 			attroff = 0;
5120 		}
5121 		printf("    <%jx>   %-18s: ", (uintmax_t) attroff, attr_str);
5122 		switch (form) {
5123 		case DW_FORM_ref_addr:
5124 		case DW_FORM_sec_offset:
5125 			if (dwarf_global_formref(attr_list[i], &v_off, &de) !=
5126 			    DW_DLV_OK) {
5127 				warnx("dwarf_global_formref failed: %s",
5128 				    dwarf_errmsg(de));
5129 				continue;
5130 			}
5131 			if (form == DW_FORM_ref_addr)
5132 				printf("<0x%jx>", (uintmax_t) v_off);
5133 			else
5134 				printf("0x%jx", (uintmax_t) v_off);
5135 			break;
5136 
5137 		case DW_FORM_ref1:
5138 		case DW_FORM_ref2:
5139 		case DW_FORM_ref4:
5140 		case DW_FORM_ref8:
5141 		case DW_FORM_ref_udata:
5142 			if (dwarf_formref(attr_list[i], &v_off, &de) !=
5143 			    DW_DLV_OK) {
5144 				warnx("dwarf_formref failed: %s",
5145 				    dwarf_errmsg(de));
5146 				continue;
5147 			}
5148 			v_off += cuoff;
5149 			printf("<0x%jx>", (uintmax_t) v_off);
5150 			break;
5151 
5152 		case DW_FORM_addr:
5153 			if (dwarf_formaddr(attr_list[i], &v_addr, &de) !=
5154 			    DW_DLV_OK) {
5155 				warnx("dwarf_formaddr failed: %s",
5156 				    dwarf_errmsg(de));
5157 				continue;
5158 			}
5159 			printf("%#jx", (uintmax_t) v_addr);
5160 			break;
5161 
5162 		case DW_FORM_data1:
5163 		case DW_FORM_data2:
5164 		case DW_FORM_data4:
5165 		case DW_FORM_data8:
5166 		case DW_FORM_udata:
5167 			if (dwarf_formudata(attr_list[i], &v_udata, &de) !=
5168 			    DW_DLV_OK) {
5169 				warnx("dwarf_formudata failed: %s",
5170 				    dwarf_errmsg(de));
5171 				continue;
5172 			}
5173 			if (attr == DW_AT_high_pc)
5174 				printf("0x%jx", (uintmax_t) v_udata);
5175 			else
5176 				printf("%ju", (uintmax_t) v_udata);
5177 			break;
5178 
5179 		case DW_FORM_sdata:
5180 			if (dwarf_formsdata(attr_list[i], &v_sdata, &de) !=
5181 			    DW_DLV_OK) {
5182 				warnx("dwarf_formudata failed: %s",
5183 				    dwarf_errmsg(de));
5184 				continue;
5185 			}
5186 			printf("%jd", (intmax_t) v_sdata);
5187 			break;
5188 
5189 		case DW_FORM_flag:
5190 			if (dwarf_formflag(attr_list[i], &v_bool, &de) !=
5191 			    DW_DLV_OK) {
5192 				warnx("dwarf_formflag failed: %s",
5193 				    dwarf_errmsg(de));
5194 				continue;
5195 			}
5196 			printf("%jd", (intmax_t) v_bool);
5197 			break;
5198 
5199 		case DW_FORM_flag_present:
5200 			putchar('1');
5201 			break;
5202 
5203 		case DW_FORM_string:
5204 		case DW_FORM_strp:
5205 			if (dwarf_formstring(attr_list[i], &v_str, &de) !=
5206 			    DW_DLV_OK) {
5207 				warnx("dwarf_formstring failed: %s",
5208 				    dwarf_errmsg(de));
5209 				continue;
5210 			}
5211 			if (form == DW_FORM_string)
5212 				printf("%s", v_str);
5213 			else
5214 				printf("(indirect string) %s", v_str);
5215 			break;
5216 
5217 		case DW_FORM_block:
5218 		case DW_FORM_block1:
5219 		case DW_FORM_block2:
5220 		case DW_FORM_block4:
5221 			if (dwarf_formblock(attr_list[i], &v_block, &de) !=
5222 			    DW_DLV_OK) {
5223 				warnx("dwarf_formblock failed: %s",
5224 				    dwarf_errmsg(de));
5225 				continue;
5226 			}
5227 			printf("%ju byte block:", (uintmax_t) v_block->bl_len);
5228 			b = v_block->bl_data;
5229 			for (j = 0; (Dwarf_Unsigned) j < v_block->bl_len; j++)
5230 				printf(" %x", b[j]);
5231 			printf("\t(");
5232 			dump_dwarf_block(re, v_block->bl_data, v_block->bl_len);
5233 			putchar(')');
5234 			break;
5235 
5236 		case DW_FORM_exprloc:
5237 			if (dwarf_formexprloc(attr_list[i], &v_udata, &v_expr,
5238 			    &de) != DW_DLV_OK) {
5239 				warnx("dwarf_formexprloc failed: %s",
5240 				    dwarf_errmsg(de));
5241 				continue;
5242 			}
5243 			printf("%ju byte block:", (uintmax_t) v_udata);
5244 			b = v_expr;
5245 			for (j = 0; (Dwarf_Unsigned) j < v_udata; j++)
5246 				printf(" %x", b[j]);
5247 			printf("\t(");
5248 			dump_dwarf_block(re, v_expr, v_udata);
5249 			putchar(')');
5250 			break;
5251 
5252 		case DW_FORM_ref_sig8:
5253 			if (dwarf_formsig8(attr_list[i], &v_sig8, &de) !=
5254 			    DW_DLV_OK) {
5255 				warnx("dwarf_formsig8 failed: %s",
5256 				    dwarf_errmsg(de));
5257 				continue;
5258 			}
5259 			p = (uint8_t *)(uintptr_t) &v_sig8.signature[0];
5260 			v_sig = re->dw_decode(&p, 8);
5261 			printf("signature: 0x%jx", (uintmax_t) v_sig);
5262 		}
5263 		switch (attr) {
5264 		case DW_AT_encoding:
5265 			if (dwarf_attrval_unsigned(die, attr, &ate, &de) !=
5266 			    DW_DLV_OK)
5267 				break;
5268 			if (dwarf_get_ATE_name(ate, &ate_str) != DW_DLV_OK)
5269 				ate_str = "DW_ATE_UNKNOWN";
5270 			printf("\t(%s)", &ate_str[strlen("DW_ATE_")]);
5271 			break;
5272 
5273 		case DW_AT_language:
5274 			if (dwarf_attrval_unsigned(die, attr, &lang, &de) !=
5275 			    DW_DLV_OK)
5276 				break;
5277 			if (dwarf_get_LANG_name(lang, &lang_str) != DW_DLV_OK)
5278 				break;
5279 			printf("\t(%s)", &lang_str[strlen("DW_LANG_")]);
5280 			break;
5281 
5282 		case DW_AT_location:
5283 		case DW_AT_string_length:
5284 		case DW_AT_return_addr:
5285 		case DW_AT_data_member_location:
5286 		case DW_AT_frame_base:
5287 		case DW_AT_segment:
5288 		case DW_AT_static_link:
5289 		case DW_AT_use_location:
5290 		case DW_AT_vtable_elem_location:
5291 			switch (form) {
5292 			case DW_FORM_data4:
5293 			case DW_FORM_data8:
5294 			case DW_FORM_sec_offset:
5295 				printf("\t(location list)");
5296 				break;
5297 			default:
5298 				break;
5299 			}
5300 
5301 		default:
5302 			break;
5303 		}
5304 		putchar('\n');
5305 	}
5306 
5307 
5308 cont_search:
5309 	/* Search children. */
5310 	ret = dwarf_child(die, &ret_die, &de);
5311 	if (ret == DW_DLV_ERROR)
5312 		warnx("dwarf_child: %s", dwarf_errmsg(de));
5313 	else if (ret == DW_DLV_OK)
5314 		dump_dwarf_die(re, ret_die, level + 1);
5315 
5316 	/* Search sibling. */
5317 	is_info = dwarf_get_die_infotypes_flag(die);
5318 	ret = dwarf_siblingof_b(re->dbg, die, &ret_die, is_info, &de);
5319 	if (ret == DW_DLV_ERROR)
5320 		warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
5321 	else if (ret == DW_DLV_OK)
5322 		dump_dwarf_die(re, ret_die, level);
5323 
5324 	dwarf_dealloc(re->dbg, die, DW_DLA_DIE);
5325 }
5326 
5327 static void
5328 set_cu_context(struct readelf *re, Dwarf_Half psize, Dwarf_Half osize,
5329     Dwarf_Half ver)
5330 {
5331 
5332 	re->cu_psize = psize;
5333 	re->cu_osize = osize;
5334 	re->cu_ver = ver;
5335 }
5336 
5337 static void
5338 dump_dwarf_info(struct readelf *re, Dwarf_Bool is_info)
5339 {
5340 	struct section *s;
5341 	Dwarf_Die die;
5342 	Dwarf_Error de;
5343 	Dwarf_Half tag, version, pointer_size, off_size;
5344 	Dwarf_Off cu_offset, cu_length;
5345 	Dwarf_Off aboff;
5346 	Dwarf_Unsigned typeoff;
5347 	Dwarf_Sig8 sig8;
5348 	Dwarf_Unsigned sig;
5349 	uint8_t *p;
5350 	const char *sn;
5351 	int i, ret;
5352 
5353 	sn = is_info ? ".debug_info" : ".debug_types";
5354 
5355 	s = NULL;
5356 	for (i = 0; (size_t) i < re->shnum; i++) {
5357 		s = &re->sl[i];
5358 		if (s->name != NULL && !strcmp(s->name, sn))
5359 			break;
5360 	}
5361 	if ((size_t) i >= re->shnum)
5362 		return;
5363 
5364 	do {
5365 		printf("\nDump of debug contents of section %s:\n", sn);
5366 
5367 		while ((ret = dwarf_next_cu_header_c(re->dbg, is_info, NULL,
5368 		    &version, &aboff, &pointer_size, &off_size, NULL, &sig8,
5369 		    &typeoff, NULL, &de)) == DW_DLV_OK) {
5370 			set_cu_context(re, pointer_size, off_size, version);
5371 			die = NULL;
5372 			while (dwarf_siblingof_b(re->dbg, die, &die, is_info,
5373 			    &de) == DW_DLV_OK) {
5374 				if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
5375 					warnx("dwarf_tag failed: %s",
5376 					    dwarf_errmsg(de));
5377 					continue;
5378 				}
5379 				/* XXX: What about DW_TAG_partial_unit? */
5380 				if ((is_info && tag == DW_TAG_compile_unit) ||
5381 				    (!is_info && tag == DW_TAG_type_unit))
5382 					break;
5383 			}
5384 			if (die == NULL && is_info) {
5385 				warnx("could not find DW_TAG_compile_unit "
5386 				    "die");
5387 				continue;
5388 			} else if (die == NULL && !is_info) {
5389 				warnx("could not find DW_TAG_type_unit die");
5390 				continue;
5391 			}
5392 
5393 			if (dwarf_die_CU_offset_range(die, &cu_offset,
5394 			    &cu_length, &de) != DW_DLV_OK) {
5395 				warnx("dwarf_die_CU_offset failed: %s",
5396 				    dwarf_errmsg(de));
5397 				continue;
5398 			}
5399 
5400 			cu_length -= off_size == 4 ? 4 : 12;
5401 
5402 			sig = 0;
5403 			if (!is_info) {
5404 				p = (uint8_t *)(uintptr_t) &sig8.signature[0];
5405 				sig = re->dw_decode(&p, 8);
5406 			}
5407 
5408 			printf("\n  Type Unit @ offset 0x%jx:\n",
5409 			    (uintmax_t) cu_offset);
5410 			printf("    Length:\t\t%#jx (%d-bit)\n",
5411 			    (uintmax_t) cu_length, off_size == 4 ? 32 : 64);
5412 			printf("    Version:\t\t%u\n", version);
5413 			printf("    Abbrev Offset:\t0x%jx\n",
5414 			    (uintmax_t) aboff);
5415 			printf("    Pointer Size:\t%u\n", pointer_size);
5416 			if (!is_info) {
5417 				printf("    Signature:\t\t0x%016jx\n",
5418 				    (uintmax_t) sig);
5419 				printf("    Type Offset:\t0x%jx\n",
5420 				    (uintmax_t) typeoff);
5421 			}
5422 
5423 			dump_dwarf_die(re, die, 0);
5424 		}
5425 		if (ret == DW_DLV_ERROR)
5426 			warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
5427 		if (is_info)
5428 			break;
5429 	} while (dwarf_next_types_section(re->dbg, &de) == DW_DLV_OK);
5430 }
5431 
5432 static void
5433 dump_dwarf_abbrev(struct readelf *re)
5434 {
5435 	Dwarf_Abbrev ab;
5436 	Dwarf_Off aboff, atoff;
5437 	Dwarf_Unsigned length, attr_count;
5438 	Dwarf_Signed flag, form;
5439 	Dwarf_Half tag, attr;
5440 	Dwarf_Error de;
5441 	const char *tag_str, *attr_str, *form_str;
5442 	char unk_tag[32], unk_attr[32], unk_form[32];
5443 	int i, j, ret;
5444 
5445 	printf("\nContents of section .debug_abbrev:\n\n");
5446 
5447 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, &aboff,
5448 	    NULL, NULL, &de)) ==  DW_DLV_OK) {
5449 		printf("  Number TAG\n");
5450 		i = 0;
5451 		while ((ret = dwarf_get_abbrev(re->dbg, aboff, &ab, &length,
5452 		    &attr_count, &de)) == DW_DLV_OK) {
5453 			if (length == 1) {
5454 				dwarf_dealloc(re->dbg, ab, DW_DLA_ABBREV);
5455 				break;
5456 			}
5457 			aboff += length;
5458 			printf("%4d", ++i);
5459 			if (dwarf_get_abbrev_tag(ab, &tag, &de) != DW_DLV_OK) {
5460 				warnx("dwarf_get_abbrev_tag failed: %s",
5461 				    dwarf_errmsg(de));
5462 				goto next_abbrev;
5463 			}
5464 			if (dwarf_get_TAG_name(tag, &tag_str) != DW_DLV_OK) {
5465 				snprintf(unk_tag, sizeof(unk_tag),
5466 				    "[Unknown Tag: %#x]", tag);
5467 				tag_str = unk_tag;
5468 			}
5469 			if (dwarf_get_abbrev_children_flag(ab, &flag, &de) !=
5470 			    DW_DLV_OK) {
5471 				warnx("dwarf_get_abbrev_children_flag failed:"
5472 				    " %s", dwarf_errmsg(de));
5473 				goto next_abbrev;
5474 			}
5475 			printf("      %s    %s\n", tag_str,
5476 			    flag ? "[has children]" : "[no children]");
5477 			for (j = 0; (Dwarf_Unsigned) j < attr_count; j++) {
5478 				if (dwarf_get_abbrev_entry(ab, (Dwarf_Signed) j,
5479 				    &attr, &form, &atoff, &de) != DW_DLV_OK) {
5480 					warnx("dwarf_get_abbrev_entry failed:"
5481 					    " %s", dwarf_errmsg(de));
5482 					continue;
5483 				}
5484 				if (dwarf_get_AT_name(attr, &attr_str) !=
5485 				    DW_DLV_OK) {
5486 					snprintf(unk_attr, sizeof(unk_attr),
5487 					    "[Unknown AT: %#x]", attr);
5488 					attr_str = unk_attr;
5489 				}
5490 				if (dwarf_get_FORM_name(form, &form_str) !=
5491 				    DW_DLV_OK) {
5492 					snprintf(unk_form, sizeof(unk_form),
5493 					    "[Unknown Form: %#x]",
5494 					    (Dwarf_Half) form);
5495 					form_str = unk_form;
5496 				}
5497 				printf("    %-18s %s\n", attr_str, form_str);
5498 			}
5499 		next_abbrev:
5500 			dwarf_dealloc(re->dbg, ab, DW_DLA_ABBREV);
5501 		}
5502 		if (ret != DW_DLV_OK)
5503 			warnx("dwarf_get_abbrev: %s", dwarf_errmsg(de));
5504 	}
5505 	if (ret == DW_DLV_ERROR)
5506 		warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
5507 }
5508 
5509 static void
5510 dump_dwarf_pubnames(struct readelf *re)
5511 {
5512 	struct section *s;
5513 	Dwarf_Off die_off;
5514 	Dwarf_Unsigned offset, length, nt_cu_offset, nt_cu_length;
5515 	Dwarf_Signed cnt;
5516 	Dwarf_Global *globs;
5517 	Dwarf_Half nt_version;
5518 	Dwarf_Error de;
5519 	Elf_Data *d;
5520 	char *glob_name;
5521 	int i, dwarf_size, elferr;
5522 
5523 	printf("\nContents of the .debug_pubnames section:\n");
5524 
5525 	s = NULL;
5526 	for (i = 0; (size_t) i < re->shnum; i++) {
5527 		s = &re->sl[i];
5528 		if (s->name != NULL && !strcmp(s->name, ".debug_pubnames"))
5529 			break;
5530 	}
5531 	if ((size_t) i >= re->shnum)
5532 		return;
5533 
5534 	(void) elf_errno();
5535 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
5536 		elferr = elf_errno();
5537 		if (elferr != 0)
5538 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
5539 		return;
5540 	}
5541 	if (d->d_size <= 0)
5542 		return;
5543 
5544 	/* Read in .debug_pubnames section table header. */
5545 	offset = 0;
5546 	length = re->dw_read(d, &offset, 4);
5547 	if (length == 0xffffffff) {
5548 		dwarf_size = 8;
5549 		length = re->dw_read(d, &offset, 8);
5550 	} else
5551 		dwarf_size = 4;
5552 
5553 	if (length > d->d_size - offset) {
5554 		warnx("invalid .dwarf_pubnames section");
5555 		return;
5556 	}
5557 
5558 	nt_version = re->dw_read(d, &offset, 2);
5559 	nt_cu_offset = re->dw_read(d, &offset, dwarf_size);
5560 	nt_cu_length = re->dw_read(d, &offset, dwarf_size);
5561 	printf("  Length:\t\t\t\t%ju\n", (uintmax_t) length);
5562 	printf("  Version:\t\t\t\t%u\n", nt_version);
5563 	printf("  Offset into .debug_info section:\t%ju\n",
5564 	    (uintmax_t) nt_cu_offset);
5565 	printf("  Size of area in .debug_info section:\t%ju\n",
5566 	    (uintmax_t) nt_cu_length);
5567 
5568 	if (dwarf_get_globals(re->dbg, &globs, &cnt, &de) != DW_DLV_OK) {
5569 		warnx("dwarf_get_globals failed: %s", dwarf_errmsg(de));
5570 		return;
5571 	}
5572 
5573 	printf("\n    Offset      Name\n");
5574 	for (i = 0; i < cnt; i++) {
5575 		if (dwarf_globname(globs[i], &glob_name, &de) != DW_DLV_OK) {
5576 			warnx("dwarf_globname failed: %s", dwarf_errmsg(de));
5577 			continue;
5578 		}
5579 		if (dwarf_global_die_offset(globs[i], &die_off, &de) !=
5580 		    DW_DLV_OK) {
5581 			warnx("dwarf_global_die_offset failed: %s",
5582 			    dwarf_errmsg(de));
5583 			continue;
5584 		}
5585 		printf("    %-11ju %s\n", (uintmax_t) die_off, glob_name);
5586 	}
5587 }
5588 
5589 static void
5590 dump_dwarf_aranges(struct readelf *re)
5591 {
5592 	struct section *s;
5593 	Dwarf_Arange *aranges;
5594 	Dwarf_Addr start;
5595 	Dwarf_Unsigned offset, length, as_cu_offset;
5596 	Dwarf_Off die_off;
5597 	Dwarf_Signed cnt;
5598 	Dwarf_Half as_version, as_addrsz, as_segsz;
5599 	Dwarf_Error de;
5600 	Elf_Data *d;
5601 	int i, dwarf_size, elferr;
5602 
5603 	printf("\nContents of section .debug_aranges:\n");
5604 
5605 	s = NULL;
5606 	for (i = 0; (size_t) i < re->shnum; i++) {
5607 		s = &re->sl[i];
5608 		if (s->name != NULL && !strcmp(s->name, ".debug_aranges"))
5609 			break;
5610 	}
5611 	if ((size_t) i >= re->shnum)
5612 		return;
5613 
5614 	(void) elf_errno();
5615 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
5616 		elferr = elf_errno();
5617 		if (elferr != 0)
5618 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
5619 		return;
5620 	}
5621 	if (d->d_size <= 0)
5622 		return;
5623 
5624 	/* Read in the .debug_aranges section table header. */
5625 	offset = 0;
5626 	length = re->dw_read(d, &offset, 4);
5627 	if (length == 0xffffffff) {
5628 		dwarf_size = 8;
5629 		length = re->dw_read(d, &offset, 8);
5630 	} else
5631 		dwarf_size = 4;
5632 
5633 	if (length > d->d_size - offset) {
5634 		warnx("invalid .dwarf_aranges section");
5635 		return;
5636 	}
5637 
5638 	as_version = re->dw_read(d, &offset, 2);
5639 	as_cu_offset = re->dw_read(d, &offset, dwarf_size);
5640 	as_addrsz = re->dw_read(d, &offset, 1);
5641 	as_segsz = re->dw_read(d, &offset, 1);
5642 
5643 	printf("  Length:\t\t\t%ju\n", (uintmax_t) length);
5644 	printf("  Version:\t\t\t%u\n", as_version);
5645 	printf("  Offset into .debug_info:\t%ju\n", (uintmax_t) as_cu_offset);
5646 	printf("  Pointer Size:\t\t\t%u\n", as_addrsz);
5647 	printf("  Segment Size:\t\t\t%u\n", as_segsz);
5648 
5649 	if (dwarf_get_aranges(re->dbg, &aranges, &cnt, &de) != DW_DLV_OK) {
5650 		warnx("dwarf_get_aranges failed: %s", dwarf_errmsg(de));
5651 		return;
5652 	}
5653 
5654 	printf("\n    Address  Length\n");
5655 	for (i = 0; i < cnt; i++) {
5656 		if (dwarf_get_arange_info(aranges[i], &start, &length,
5657 		    &die_off, &de) != DW_DLV_OK) {
5658 			warnx("dwarf_get_arange_info failed: %s",
5659 			    dwarf_errmsg(de));
5660 			continue;
5661 		}
5662 		printf("    %08jx %ju\n", (uintmax_t) start,
5663 		    (uintmax_t) length);
5664 	}
5665 }
5666 
5667 static void
5668 dump_dwarf_ranges_foreach(struct readelf *re, Dwarf_Die die, Dwarf_Addr base)
5669 {
5670 	Dwarf_Attribute *attr_list;
5671 	Dwarf_Ranges *ranges;
5672 	Dwarf_Die ret_die;
5673 	Dwarf_Error de;
5674 	Dwarf_Addr base0;
5675 	Dwarf_Half attr;
5676 	Dwarf_Signed attr_count, cnt;
5677 	Dwarf_Unsigned off, bytecnt;
5678 	int i, j, ret;
5679 
5680 	if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
5681 	    DW_DLV_OK) {
5682 		if (ret == DW_DLV_ERROR)
5683 			warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
5684 		goto cont_search;
5685 	}
5686 
5687 	for (i = 0; i < attr_count; i++) {
5688 		if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
5689 			warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
5690 			continue;
5691 		}
5692 		if (attr != DW_AT_ranges)
5693 			continue;
5694 		if (dwarf_formudata(attr_list[i], &off, &de) != DW_DLV_OK) {
5695 			warnx("dwarf_formudata failed: %s", dwarf_errmsg(de));
5696 			continue;
5697 		}
5698 		if (dwarf_get_ranges(re->dbg, (Dwarf_Off) off, &ranges, &cnt,
5699 		    &bytecnt, &de) != DW_DLV_OK)
5700 			continue;
5701 		base0 = base;
5702 		for (j = 0; j < cnt; j++) {
5703 			printf("    %08jx ", (uintmax_t) off);
5704 			if (ranges[j].dwr_type == DW_RANGES_END) {
5705 				printf("%s\n", "<End of list>");
5706 				continue;
5707 			} else if (ranges[j].dwr_type ==
5708 			    DW_RANGES_ADDRESS_SELECTION) {
5709 				base0 = ranges[j].dwr_addr2;
5710 				continue;
5711 			}
5712 			if (re->ec == ELFCLASS32)
5713 				printf("%08jx %08jx\n",
5714 				    (uintmax_t) (ranges[j].dwr_addr1 + base0),
5715 				    (uintmax_t) (ranges[j].dwr_addr2 + base0));
5716 			else
5717 				printf("%016jx %016jx\n",
5718 				    (uintmax_t) (ranges[j].dwr_addr1 + base0),
5719 				    (uintmax_t) (ranges[j].dwr_addr2 + base0));
5720 		}
5721 	}
5722 
5723 cont_search:
5724 	/* Search children. */
5725 	ret = dwarf_child(die, &ret_die, &de);
5726 	if (ret == DW_DLV_ERROR)
5727 		warnx("dwarf_child: %s", dwarf_errmsg(de));
5728 	else if (ret == DW_DLV_OK)
5729 		dump_dwarf_ranges_foreach(re, ret_die, base);
5730 
5731 	/* Search sibling. */
5732 	ret = dwarf_siblingof(re->dbg, die, &ret_die, &de);
5733 	if (ret == DW_DLV_ERROR)
5734 		warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
5735 	else if (ret == DW_DLV_OK)
5736 		dump_dwarf_ranges_foreach(re, ret_die, base);
5737 }
5738 
5739 static void
5740 dump_dwarf_ranges(struct readelf *re)
5741 {
5742 	Dwarf_Ranges *ranges;
5743 	Dwarf_Die die;
5744 	Dwarf_Signed cnt;
5745 	Dwarf_Unsigned bytecnt;
5746 	Dwarf_Half tag;
5747 	Dwarf_Error de;
5748 	Dwarf_Unsigned lowpc;
5749 	int ret;
5750 
5751 	if (dwarf_get_ranges(re->dbg, 0, &ranges, &cnt, &bytecnt, &de) !=
5752 	    DW_DLV_OK)
5753 		return;
5754 
5755 	printf("Contents of the .debug_ranges section:\n\n");
5756 	if (re->ec == ELFCLASS32)
5757 		printf("    %-8s %-8s %s\n", "Offset", "Begin", "End");
5758 	else
5759 		printf("    %-8s %-16s %s\n", "Offset", "Begin", "End");
5760 
5761 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
5762 	    NULL, &de)) == DW_DLV_OK) {
5763 		die = NULL;
5764 		if (dwarf_siblingof(re->dbg, die, &die, &de) != DW_DLV_OK)
5765 			continue;
5766 		if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
5767 			warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
5768 			continue;
5769 		}
5770 		/* XXX: What about DW_TAG_partial_unit? */
5771 		lowpc = 0;
5772 		if (tag == DW_TAG_compile_unit) {
5773 			if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lowpc,
5774 			    &de) != DW_DLV_OK)
5775 				lowpc = 0;
5776 		}
5777 
5778 		dump_dwarf_ranges_foreach(re, die, (Dwarf_Addr) lowpc);
5779 	}
5780 	putchar('\n');
5781 }
5782 
5783 static void
5784 dump_dwarf_macinfo(struct readelf *re)
5785 {
5786 	Dwarf_Unsigned offset;
5787 	Dwarf_Signed cnt;
5788 	Dwarf_Macro_Details *md;
5789 	Dwarf_Error de;
5790 	const char *mi_str;
5791 	char unk_mi[32];
5792 	int i;
5793 
5794 #define	_MAX_MACINFO_ENTRY	65535
5795 
5796 	printf("\nContents of section .debug_macinfo:\n\n");
5797 
5798 	offset = 0;
5799 	while (dwarf_get_macro_details(re->dbg, offset, _MAX_MACINFO_ENTRY,
5800 	    &cnt, &md, &de) == DW_DLV_OK) {
5801 		for (i = 0; i < cnt; i++) {
5802 			offset = md[i].dmd_offset + 1;
5803 			if (md[i].dmd_type == 0)
5804 				break;
5805 			if (dwarf_get_MACINFO_name(md[i].dmd_type, &mi_str) !=
5806 			    DW_DLV_OK) {
5807 				snprintf(unk_mi, sizeof(unk_mi),
5808 				    "[Unknown MACINFO: %#x]", md[i].dmd_type);
5809 				mi_str = unk_mi;
5810 			}
5811 			printf(" %s", mi_str);
5812 			switch (md[i].dmd_type) {
5813 			case DW_MACINFO_define:
5814 			case DW_MACINFO_undef:
5815 				printf(" - lineno : %jd macro : %s\n",
5816 				    (intmax_t) md[i].dmd_lineno,
5817 				    md[i].dmd_macro);
5818 				break;
5819 			case DW_MACINFO_start_file:
5820 				printf(" - lineno : %jd filenum : %jd\n",
5821 				    (intmax_t) md[i].dmd_lineno,
5822 				    (intmax_t) md[i].dmd_fileindex);
5823 				break;
5824 			default:
5825 				putchar('\n');
5826 				break;
5827 			}
5828 		}
5829 	}
5830 
5831 #undef	_MAX_MACINFO_ENTRY
5832 }
5833 
5834 static void
5835 dump_dwarf_frame_inst(struct readelf *re, Dwarf_Cie cie, uint8_t *insts,
5836     Dwarf_Unsigned len, Dwarf_Unsigned caf, Dwarf_Signed daf, Dwarf_Addr pc,
5837     Dwarf_Debug dbg)
5838 {
5839 	Dwarf_Frame_Op *oplist;
5840 	Dwarf_Signed opcnt, delta;
5841 	Dwarf_Small op;
5842 	Dwarf_Error de;
5843 	const char *op_str;
5844 	char unk_op[32];
5845 	int i;
5846 
5847 	if (dwarf_expand_frame_instructions(cie, insts, len, &oplist,
5848 	    &opcnt, &de) != DW_DLV_OK) {
5849 		warnx("dwarf_expand_frame_instructions failed: %s",
5850 		    dwarf_errmsg(de));
5851 		return;
5852 	}
5853 
5854 	for (i = 0; i < opcnt; i++) {
5855 		if (oplist[i].fp_base_op != 0)
5856 			op = oplist[i].fp_base_op << 6;
5857 		else
5858 			op = oplist[i].fp_extended_op;
5859 		if (dwarf_get_CFA_name(op, &op_str) != DW_DLV_OK) {
5860 			snprintf(unk_op, sizeof(unk_op), "[Unknown CFA: %#x]",
5861 			    op);
5862 			op_str = unk_op;
5863 		}
5864 		printf("  %s", op_str);
5865 		switch (op) {
5866 		case DW_CFA_advance_loc:
5867 			delta = oplist[i].fp_offset * caf;
5868 			pc += delta;
5869 			printf(": %ju to %08jx", (uintmax_t) delta,
5870 			    (uintmax_t) pc);
5871 			break;
5872 		case DW_CFA_offset:
5873 		case DW_CFA_offset_extended:
5874 		case DW_CFA_offset_extended_sf:
5875 			delta = oplist[i].fp_offset * daf;
5876 			printf(": r%u (%s) at cfa%+jd", oplist[i].fp_register,
5877 			    dwarf_regname(re, oplist[i].fp_register),
5878 			    (intmax_t) delta);
5879 			break;
5880 		case DW_CFA_restore:
5881 			printf(": r%u (%s)", oplist[i].fp_register,
5882 			    dwarf_regname(re, oplist[i].fp_register));
5883 			break;
5884 		case DW_CFA_set_loc:
5885 			pc = oplist[i].fp_offset;
5886 			printf(": to %08jx", (uintmax_t) pc);
5887 			break;
5888 		case DW_CFA_advance_loc1:
5889 		case DW_CFA_advance_loc2:
5890 		case DW_CFA_advance_loc4:
5891 			pc += oplist[i].fp_offset;
5892 			printf(": %jd to %08jx", (intmax_t) oplist[i].fp_offset,
5893 			    (uintmax_t) pc);
5894 			break;
5895 		case DW_CFA_def_cfa:
5896 			printf(": r%u (%s) ofs %ju", oplist[i].fp_register,
5897 			    dwarf_regname(re, oplist[i].fp_register),
5898 			    (uintmax_t) oplist[i].fp_offset);
5899 			break;
5900 		case DW_CFA_def_cfa_sf:
5901 			printf(": r%u (%s) ofs %jd", oplist[i].fp_register,
5902 			    dwarf_regname(re, oplist[i].fp_register),
5903 			    (intmax_t) (oplist[i].fp_offset * daf));
5904 			break;
5905 		case DW_CFA_def_cfa_register:
5906 			printf(": r%u (%s)", oplist[i].fp_register,
5907 			    dwarf_regname(re, oplist[i].fp_register));
5908 			break;
5909 		case DW_CFA_def_cfa_offset:
5910 			printf(": %ju", (uintmax_t) oplist[i].fp_offset);
5911 			break;
5912 		case DW_CFA_def_cfa_offset_sf:
5913 			printf(": %jd", (intmax_t) (oplist[i].fp_offset * daf));
5914 			break;
5915 		default:
5916 			break;
5917 		}
5918 		putchar('\n');
5919 	}
5920 
5921 	dwarf_dealloc(dbg, oplist, DW_DLA_FRAME_BLOCK);
5922 }
5923 
5924 static char *
5925 get_regoff_str(struct readelf *re, Dwarf_Half reg, Dwarf_Addr off)
5926 {
5927 	static char rs[16];
5928 
5929 	if (reg == DW_FRAME_UNDEFINED_VAL || reg == DW_FRAME_REG_INITIAL_VALUE)
5930 		snprintf(rs, sizeof(rs), "%c", 'u');
5931 	else if (reg == DW_FRAME_CFA_COL)
5932 		snprintf(rs, sizeof(rs), "c%+jd", (intmax_t) off);
5933 	else
5934 		snprintf(rs, sizeof(rs), "%s%+jd", dwarf_regname(re, reg),
5935 		    (intmax_t) off);
5936 
5937 	return (rs);
5938 }
5939 
5940 static int
5941 dump_dwarf_frame_regtable(struct readelf *re, Dwarf_Fde fde, Dwarf_Addr pc,
5942     Dwarf_Unsigned func_len, Dwarf_Half cie_ra)
5943 {
5944 	Dwarf_Regtable rt;
5945 	Dwarf_Addr row_pc, end_pc, pre_pc, cur_pc;
5946 	Dwarf_Error de;
5947 	char *vec;
5948 	int i;
5949 
5950 #define BIT_SET(v, n) (v[(n)>>3] |= 1U << ((n) & 7))
5951 #define BIT_CLR(v, n) (v[(n)>>3] &= ~(1U << ((n) & 7)))
5952 #define BIT_ISSET(v, n) (v[(n)>>3] & (1U << ((n) & 7)))
5953 #define	RT(x) rt.rules[(x)]
5954 
5955 	vec = calloc((DW_REG_TABLE_SIZE + 7) / 8, 1);
5956 	if (vec == NULL)
5957 		err(EXIT_FAILURE, "calloc failed");
5958 
5959 	pre_pc = ~((Dwarf_Addr) 0);
5960 	cur_pc = pc;
5961 	end_pc = pc + func_len;
5962 	for (; cur_pc < end_pc; cur_pc++) {
5963 		if (dwarf_get_fde_info_for_all_regs(fde, cur_pc, &rt, &row_pc,
5964 		    &de) != DW_DLV_OK) {
5965 			warnx("dwarf_get_fde_info_for_all_regs failed: %s\n",
5966 			    dwarf_errmsg(de));
5967 			return (-1);
5968 		}
5969 		if (row_pc == pre_pc)
5970 			continue;
5971 		pre_pc = row_pc;
5972 		for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
5973 			if (rt.rules[i].dw_regnum != DW_FRAME_REG_INITIAL_VALUE)
5974 				BIT_SET(vec, i);
5975 		}
5976 	}
5977 
5978 	printf("   LOC   CFA      ");
5979 	for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
5980 		if (BIT_ISSET(vec, i)) {
5981 			if ((Dwarf_Half) i == cie_ra)
5982 				printf("ra   ");
5983 			else
5984 				printf("%-5s",
5985 				    dwarf_regname(re, (unsigned int) i));
5986 		}
5987 	}
5988 	putchar('\n');
5989 
5990 	pre_pc = ~((Dwarf_Addr) 0);
5991 	cur_pc = pc;
5992 	end_pc = pc + func_len;
5993 	for (; cur_pc < end_pc; cur_pc++) {
5994 		if (dwarf_get_fde_info_for_all_regs(fde, cur_pc, &rt, &row_pc,
5995 		    &de) != DW_DLV_OK) {
5996 			warnx("dwarf_get_fde_info_for_all_regs failed: %s\n",
5997 			    dwarf_errmsg(de));
5998 			return (-1);
5999 		}
6000 		if (row_pc == pre_pc)
6001 			continue;
6002 		pre_pc = row_pc;
6003 		printf("%08jx ", (uintmax_t) row_pc);
6004 		printf("%-8s ", get_regoff_str(re, RT(0).dw_regnum,
6005 		    RT(0).dw_offset));
6006 		for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
6007 			if (BIT_ISSET(vec, i)) {
6008 				printf("%-5s", get_regoff_str(re,
6009 				    RT(i).dw_regnum, RT(i).dw_offset));
6010 			}
6011 		}
6012 		putchar('\n');
6013 	}
6014 
6015 	free(vec);
6016 
6017 	return (0);
6018 
6019 #undef	BIT_SET
6020 #undef	BIT_CLR
6021 #undef	BIT_ISSET
6022 #undef	RT
6023 }
6024 
6025 static void
6026 dump_dwarf_frame_section(struct readelf *re, struct section *s, int alt)
6027 {
6028 	Dwarf_Cie *cie_list, cie, pre_cie;
6029 	Dwarf_Fde *fde_list, fde;
6030 	Dwarf_Off cie_offset, fde_offset;
6031 	Dwarf_Unsigned cie_length, fde_instlen;
6032 	Dwarf_Unsigned cie_caf, cie_daf, cie_instlen, func_len, fde_length;
6033 	Dwarf_Signed cie_count, fde_count, cie_index;
6034 	Dwarf_Addr low_pc;
6035 	Dwarf_Half cie_ra;
6036 	Dwarf_Small cie_version;
6037 	Dwarf_Ptr fde_addr, fde_inst, cie_inst;
6038 	char *cie_aug, c;
6039 	int i, eh_frame;
6040 	Dwarf_Error de;
6041 
6042 	printf("\nThe section %s contains:\n\n", s->name);
6043 
6044 	if (!strcmp(s->name, ".debug_frame")) {
6045 		eh_frame = 0;
6046 		if (dwarf_get_fde_list(re->dbg, &cie_list, &cie_count,
6047 		    &fde_list, &fde_count, &de) != DW_DLV_OK) {
6048 			warnx("dwarf_get_fde_list failed: %s",
6049 			    dwarf_errmsg(de));
6050 			return;
6051 		}
6052 	} else if (!strcmp(s->name, ".eh_frame")) {
6053 		eh_frame = 1;
6054 		if (dwarf_get_fde_list_eh(re->dbg, &cie_list, &cie_count,
6055 		    &fde_list, &fde_count, &de) != DW_DLV_OK) {
6056 			warnx("dwarf_get_fde_list_eh failed: %s",
6057 			    dwarf_errmsg(de));
6058 			return;
6059 		}
6060 	} else
6061 		return;
6062 
6063 	pre_cie = NULL;
6064 	for (i = 0; i < fde_count; i++) {
6065 		if (dwarf_get_fde_n(fde_list, i, &fde, &de) != DW_DLV_OK) {
6066 			warnx("dwarf_get_fde_n failed: %s", dwarf_errmsg(de));
6067 			continue;
6068 		}
6069 		if (dwarf_get_cie_of_fde(fde, &cie, &de) != DW_DLV_OK) {
6070 			warnx("dwarf_get_fde_n failed: %s", dwarf_errmsg(de));
6071 			continue;
6072 		}
6073 		if (dwarf_get_fde_range(fde, &low_pc, &func_len, &fde_addr,
6074 		    &fde_length, &cie_offset, &cie_index, &fde_offset,
6075 		    &de) != DW_DLV_OK) {
6076 			warnx("dwarf_get_fde_range failed: %s",
6077 			    dwarf_errmsg(de));
6078 			continue;
6079 		}
6080 		if (dwarf_get_fde_instr_bytes(fde, &fde_inst, &fde_instlen,
6081 		    &de) != DW_DLV_OK) {
6082 			warnx("dwarf_get_fde_instr_bytes failed: %s",
6083 			    dwarf_errmsg(de));
6084 			continue;
6085 		}
6086 		if (pre_cie == NULL || cie != pre_cie) {
6087 			pre_cie = cie;
6088 			if (dwarf_get_cie_info(cie, &cie_length, &cie_version,
6089 			    &cie_aug, &cie_caf, &cie_daf, &cie_ra,
6090 			    &cie_inst, &cie_instlen, &de) != DW_DLV_OK) {
6091 				warnx("dwarf_get_cie_info failed: %s",
6092 				    dwarf_errmsg(de));
6093 				continue;
6094 			}
6095 			printf("%08jx %08jx %8.8jx CIE",
6096 			    (uintmax_t) cie_offset,
6097 			    (uintmax_t) cie_length,
6098 			    (uintmax_t) (eh_frame ? 0 : ~0U));
6099 			if (!alt) {
6100 				putchar('\n');
6101 				printf("  Version:\t\t\t%u\n", cie_version);
6102 				printf("  Augmentation:\t\t\t\"");
6103 				while ((c = *cie_aug++) != '\0')
6104 					putchar(c);
6105 				printf("\"\n");
6106 				printf("  Code alignment factor:\t%ju\n",
6107 				    (uintmax_t) cie_caf);
6108 				printf("  Data alignment factor:\t%jd\n",
6109 				    (intmax_t) cie_daf);
6110 				printf("  Return address column:\t%ju\n",
6111 				    (uintmax_t) cie_ra);
6112 				putchar('\n');
6113 				dump_dwarf_frame_inst(re, cie, cie_inst,
6114 				    cie_instlen, cie_caf, cie_daf, 0,
6115 				    re->dbg);
6116 				putchar('\n');
6117 			} else {
6118 				printf(" \"");
6119 				while ((c = *cie_aug++) != '\0')
6120 					putchar(c);
6121 				putchar('"');
6122 				printf(" cf=%ju df=%jd ra=%ju\n",
6123 				    (uintmax_t) cie_caf,
6124 				    (uintmax_t) cie_daf,
6125 				    (uintmax_t) cie_ra);
6126 				dump_dwarf_frame_regtable(re, fde, low_pc, 1,
6127 				    cie_ra);
6128 				putchar('\n');
6129 			}
6130 		}
6131 		printf("%08jx %08jx %08jx FDE cie=%08jx pc=%08jx..%08jx\n",
6132 		    (uintmax_t) fde_offset, (uintmax_t) fde_length,
6133 		    (uintmax_t) cie_offset,
6134 		    (uintmax_t) (eh_frame ? fde_offset + 4 - cie_offset :
6135 			cie_offset),
6136 		    (uintmax_t) low_pc, (uintmax_t) (low_pc + func_len));
6137 		if (!alt)
6138 			dump_dwarf_frame_inst(re, cie, fde_inst, fde_instlen,
6139 			    cie_caf, cie_daf, low_pc, re->dbg);
6140 		else
6141 			dump_dwarf_frame_regtable(re, fde, low_pc, func_len,
6142 			    cie_ra);
6143 		putchar('\n');
6144 	}
6145 }
6146 
6147 static void
6148 dump_dwarf_frame(struct readelf *re, int alt)
6149 {
6150 	struct section *s;
6151 	int i;
6152 
6153 	(void) dwarf_set_frame_cfa_value(re->dbg, DW_FRAME_CFA_COL);
6154 
6155 	for (i = 0; (size_t) i < re->shnum; i++) {
6156 		s = &re->sl[i];
6157 		if (s->name != NULL && (!strcmp(s->name, ".debug_frame") ||
6158 		    !strcmp(s->name, ".eh_frame")))
6159 			dump_dwarf_frame_section(re, s, alt);
6160 	}
6161 }
6162 
6163 static void
6164 dump_dwarf_str(struct readelf *re)
6165 {
6166 	struct section *s;
6167 	Elf_Data *d;
6168 	unsigned char *p;
6169 	int elferr, end, i, j;
6170 
6171 	printf("\nContents of section .debug_str:\n");
6172 
6173 	s = NULL;
6174 	for (i = 0; (size_t) i < re->shnum; i++) {
6175 		s = &re->sl[i];
6176 		if (s->name != NULL && !strcmp(s->name, ".debug_str"))
6177 			break;
6178 	}
6179 	if ((size_t) i >= re->shnum)
6180 		return;
6181 
6182 	(void) elf_errno();
6183 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
6184 		elferr = elf_errno();
6185 		if (elferr != 0)
6186 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
6187 		return;
6188 	}
6189 	if (d->d_size <= 0)
6190 		return;
6191 
6192 	for (i = 0, p = d->d_buf; (size_t) i < d->d_size; i += 16) {
6193 		printf("  0x%08x", (unsigned int) i);
6194 		if ((size_t) i + 16 > d->d_size)
6195 			end = d->d_size;
6196 		else
6197 			end = i + 16;
6198 		for (j = i; j < i + 16; j++) {
6199 			if ((j - i) % 4 == 0)
6200 				putchar(' ');
6201 			if (j >= end) {
6202 				printf("  ");
6203 				continue;
6204 			}
6205 			printf("%02x", (uint8_t) p[j]);
6206 		}
6207 		putchar(' ');
6208 		for (j = i; j < end; j++) {
6209 			if (isprint(p[j]))
6210 				putchar(p[j]);
6211 			else if (p[j] == 0)
6212 				putchar('.');
6213 			else
6214 				putchar(' ');
6215 		}
6216 		putchar('\n');
6217 	}
6218 }
6219 
6220 static int
6221 loc_at_comparator(const void *la1, const void *la2)
6222 {
6223 	const struct loc_at *left, *right;
6224 
6225 	left = (const struct loc_at *)la1;
6226 	right = (const struct loc_at *)la2;
6227 
6228 	if (left->la_off > right->la_off)
6229 		return (1);
6230 	else if (left->la_off < right->la_off)
6231 		return (-1);
6232 	else
6233 		return (0);
6234 }
6235 
6236 static void
6237 search_loclist_at(struct readelf *re, Dwarf_Die die, Dwarf_Unsigned lowpc,
6238     struct loc_at **la_list, size_t *la_list_len, size_t *la_list_cap)
6239 {
6240 	struct loc_at *la;
6241 	Dwarf_Attribute *attr_list;
6242 	Dwarf_Die ret_die;
6243 	Dwarf_Unsigned off;
6244 	Dwarf_Off ref;
6245 	Dwarf_Signed attr_count;
6246 	Dwarf_Half attr, form;
6247 	Dwarf_Bool is_info;
6248 	Dwarf_Error de;
6249 	int i, ret;
6250 
6251 	is_info = dwarf_get_die_infotypes_flag(die);
6252 
6253 	if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
6254 	    DW_DLV_OK) {
6255 		if (ret == DW_DLV_ERROR)
6256 			warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
6257 		goto cont_search;
6258 	}
6259 	for (i = 0; i < attr_count; i++) {
6260 		if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
6261 			warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
6262 			continue;
6263 		}
6264 		if (attr != DW_AT_location &&
6265 		    attr != DW_AT_string_length &&
6266 		    attr != DW_AT_return_addr &&
6267 		    attr != DW_AT_data_member_location &&
6268 		    attr != DW_AT_frame_base &&
6269 		    attr != DW_AT_segment &&
6270 		    attr != DW_AT_static_link &&
6271 		    attr != DW_AT_use_location &&
6272 		    attr != DW_AT_vtable_elem_location)
6273 			continue;
6274 		if (dwarf_whatform(attr_list[i], &form, &de) != DW_DLV_OK) {
6275 			warnx("dwarf_whatform failed: %s", dwarf_errmsg(de));
6276 			continue;
6277 		}
6278 		if (form == DW_FORM_data4 || form == DW_FORM_data8) {
6279 			if (dwarf_formudata(attr_list[i], &off, &de) !=
6280 			    DW_DLV_OK) {
6281 				warnx("dwarf_formudata failed: %s",
6282 				    dwarf_errmsg(de));
6283 				continue;
6284 			}
6285 		} else if (form == DW_FORM_sec_offset) {
6286 			if (dwarf_global_formref(attr_list[i], &ref, &de) !=
6287 			    DW_DLV_OK) {
6288 				warnx("dwarf_global_formref failed: %s",
6289 				    dwarf_errmsg(de));
6290 				continue;
6291 			}
6292 			off = ref;
6293 		} else
6294 			continue;
6295 
6296 		if (*la_list_cap == *la_list_len) {
6297 			*la_list = realloc(*la_list,
6298 			    *la_list_cap * 2 * sizeof(**la_list));
6299 			if (la_list == NULL)
6300 				errx(EXIT_FAILURE, "realloc failed");
6301 			*la_list_cap *= 2;
6302 		}
6303 		la = &((*la_list)[*la_list_len]);
6304 		la->la_at = attr_list[i];
6305 		la->la_off = off;
6306 		la->la_lowpc = lowpc;
6307 		la->la_cu_psize = re->cu_psize;
6308 		la->la_cu_osize = re->cu_osize;
6309 		la->la_cu_ver = re->cu_ver;
6310 		(*la_list_len)++;
6311 	}
6312 
6313 cont_search:
6314 	/* Search children. */
6315 	ret = dwarf_child(die, &ret_die, &de);
6316 	if (ret == DW_DLV_ERROR)
6317 		warnx("dwarf_child: %s", dwarf_errmsg(de));
6318 	else if (ret == DW_DLV_OK)
6319 		search_loclist_at(re, ret_die, lowpc, la_list,
6320 		    la_list_len, la_list_cap);
6321 
6322 	/* Search sibling. */
6323 	ret = dwarf_siblingof_b(re->dbg, die, &ret_die, is_info, &de);
6324 	if (ret == DW_DLV_ERROR)
6325 		warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
6326 	else if (ret == DW_DLV_OK)
6327 		search_loclist_at(re, ret_die, lowpc, la_list,
6328 		    la_list_len, la_list_cap);
6329 }
6330 
6331 static void
6332 dump_dwarf_loc(struct readelf *re, Dwarf_Loc *lr)
6333 {
6334 	const char *op_str;
6335 	char unk_op[32];
6336 	uint8_t *b, n;
6337 	int i;
6338 
6339 	if (dwarf_get_OP_name(lr->lr_atom, &op_str) !=
6340 	    DW_DLV_OK) {
6341 		snprintf(unk_op, sizeof(unk_op),
6342 		    "[Unknown OP: %#x]", lr->lr_atom);
6343 		op_str = unk_op;
6344 	}
6345 
6346 	printf("%s", op_str);
6347 
6348 	switch (lr->lr_atom) {
6349 	case DW_OP_reg0:
6350 	case DW_OP_reg1:
6351 	case DW_OP_reg2:
6352 	case DW_OP_reg3:
6353 	case DW_OP_reg4:
6354 	case DW_OP_reg5:
6355 	case DW_OP_reg6:
6356 	case DW_OP_reg7:
6357 	case DW_OP_reg8:
6358 	case DW_OP_reg9:
6359 	case DW_OP_reg10:
6360 	case DW_OP_reg11:
6361 	case DW_OP_reg12:
6362 	case DW_OP_reg13:
6363 	case DW_OP_reg14:
6364 	case DW_OP_reg15:
6365 	case DW_OP_reg16:
6366 	case DW_OP_reg17:
6367 	case DW_OP_reg18:
6368 	case DW_OP_reg19:
6369 	case DW_OP_reg20:
6370 	case DW_OP_reg21:
6371 	case DW_OP_reg22:
6372 	case DW_OP_reg23:
6373 	case DW_OP_reg24:
6374 	case DW_OP_reg25:
6375 	case DW_OP_reg26:
6376 	case DW_OP_reg27:
6377 	case DW_OP_reg28:
6378 	case DW_OP_reg29:
6379 	case DW_OP_reg30:
6380 	case DW_OP_reg31:
6381 		printf(" (%s)", dwarf_regname(re, lr->lr_atom - DW_OP_reg0));
6382 		break;
6383 
6384 	case DW_OP_deref:
6385 	case DW_OP_lit0:
6386 	case DW_OP_lit1:
6387 	case DW_OP_lit2:
6388 	case DW_OP_lit3:
6389 	case DW_OP_lit4:
6390 	case DW_OP_lit5:
6391 	case DW_OP_lit6:
6392 	case DW_OP_lit7:
6393 	case DW_OP_lit8:
6394 	case DW_OP_lit9:
6395 	case DW_OP_lit10:
6396 	case DW_OP_lit11:
6397 	case DW_OP_lit12:
6398 	case DW_OP_lit13:
6399 	case DW_OP_lit14:
6400 	case DW_OP_lit15:
6401 	case DW_OP_lit16:
6402 	case DW_OP_lit17:
6403 	case DW_OP_lit18:
6404 	case DW_OP_lit19:
6405 	case DW_OP_lit20:
6406 	case DW_OP_lit21:
6407 	case DW_OP_lit22:
6408 	case DW_OP_lit23:
6409 	case DW_OP_lit24:
6410 	case DW_OP_lit25:
6411 	case DW_OP_lit26:
6412 	case DW_OP_lit27:
6413 	case DW_OP_lit28:
6414 	case DW_OP_lit29:
6415 	case DW_OP_lit30:
6416 	case DW_OP_lit31:
6417 	case DW_OP_dup:
6418 	case DW_OP_drop:
6419 	case DW_OP_over:
6420 	case DW_OP_swap:
6421 	case DW_OP_rot:
6422 	case DW_OP_xderef:
6423 	case DW_OP_abs:
6424 	case DW_OP_and:
6425 	case DW_OP_div:
6426 	case DW_OP_minus:
6427 	case DW_OP_mod:
6428 	case DW_OP_mul:
6429 	case DW_OP_neg:
6430 	case DW_OP_not:
6431 	case DW_OP_or:
6432 	case DW_OP_plus:
6433 	case DW_OP_shl:
6434 	case DW_OP_shr:
6435 	case DW_OP_shra:
6436 	case DW_OP_xor:
6437 	case DW_OP_eq:
6438 	case DW_OP_ge:
6439 	case DW_OP_gt:
6440 	case DW_OP_le:
6441 	case DW_OP_lt:
6442 	case DW_OP_ne:
6443 	case DW_OP_nop:
6444 	case DW_OP_push_object_address:
6445 	case DW_OP_form_tls_address:
6446 	case DW_OP_call_frame_cfa:
6447 	case DW_OP_stack_value:
6448 	case DW_OP_GNU_push_tls_address:
6449 	case DW_OP_GNU_uninit:
6450 		break;
6451 
6452 	case DW_OP_const1u:
6453 	case DW_OP_pick:
6454 	case DW_OP_deref_size:
6455 	case DW_OP_xderef_size:
6456 	case DW_OP_const2u:
6457 	case DW_OP_bra:
6458 	case DW_OP_skip:
6459 	case DW_OP_const4u:
6460 	case DW_OP_const8u:
6461 	case DW_OP_constu:
6462 	case DW_OP_plus_uconst:
6463 	case DW_OP_regx:
6464 	case DW_OP_piece:
6465 		printf(": %ju", (uintmax_t)
6466 		    lr->lr_number);
6467 		break;
6468 
6469 	case DW_OP_const1s:
6470 	case DW_OP_const2s:
6471 	case DW_OP_const4s:
6472 	case DW_OP_const8s:
6473 	case DW_OP_consts:
6474 		printf(": %jd", (intmax_t)
6475 		    lr->lr_number);
6476 		break;
6477 
6478 	case DW_OP_breg0:
6479 	case DW_OP_breg1:
6480 	case DW_OP_breg2:
6481 	case DW_OP_breg3:
6482 	case DW_OP_breg4:
6483 	case DW_OP_breg5:
6484 	case DW_OP_breg6:
6485 	case DW_OP_breg7:
6486 	case DW_OP_breg8:
6487 	case DW_OP_breg9:
6488 	case DW_OP_breg10:
6489 	case DW_OP_breg11:
6490 	case DW_OP_breg12:
6491 	case DW_OP_breg13:
6492 	case DW_OP_breg14:
6493 	case DW_OP_breg15:
6494 	case DW_OP_breg16:
6495 	case DW_OP_breg17:
6496 	case DW_OP_breg18:
6497 	case DW_OP_breg19:
6498 	case DW_OP_breg20:
6499 	case DW_OP_breg21:
6500 	case DW_OP_breg22:
6501 	case DW_OP_breg23:
6502 	case DW_OP_breg24:
6503 	case DW_OP_breg25:
6504 	case DW_OP_breg26:
6505 	case DW_OP_breg27:
6506 	case DW_OP_breg28:
6507 	case DW_OP_breg29:
6508 	case DW_OP_breg30:
6509 	case DW_OP_breg31:
6510 		printf(" (%s): %jd",
6511 		    dwarf_regname(re, lr->lr_atom - DW_OP_breg0),
6512 		    (intmax_t) lr->lr_number);
6513 		break;
6514 
6515 	case DW_OP_fbreg:
6516 		printf(": %jd", (intmax_t)
6517 		    lr->lr_number);
6518 		break;
6519 
6520 	case DW_OP_bregx:
6521 		printf(": %ju (%s) %jd",
6522 		    (uintmax_t) lr->lr_number,
6523 		    dwarf_regname(re, (unsigned int) lr->lr_number),
6524 		    (intmax_t) lr->lr_number2);
6525 		break;
6526 
6527 	case DW_OP_addr:
6528 	case DW_OP_GNU_encoded_addr:
6529 		printf(": %#jx", (uintmax_t)
6530 		    lr->lr_number);
6531 		break;
6532 
6533 	case DW_OP_GNU_implicit_pointer:
6534 		printf(": <0x%jx> %jd", (uintmax_t) lr->lr_number,
6535 		    (intmax_t) lr->lr_number2);
6536 		break;
6537 
6538 	case DW_OP_implicit_value:
6539 		printf(": %ju byte block:", (uintmax_t) lr->lr_number);
6540 		b = (uint8_t *)(uintptr_t) lr->lr_number2;
6541 		for (i = 0; (Dwarf_Unsigned) i < lr->lr_number; i++)
6542 			printf(" %x", b[i]);
6543 		break;
6544 
6545 	case DW_OP_GNU_entry_value:
6546 		printf(": (");
6547 		dump_dwarf_block(re, (uint8_t *)(uintptr_t) lr->lr_number2,
6548 		    lr->lr_number);
6549 		putchar(')');
6550 		break;
6551 
6552 	case DW_OP_GNU_const_type:
6553 		printf(": <0x%jx> ", (uintmax_t) lr->lr_number);
6554 		b = (uint8_t *)(uintptr_t) lr->lr_number2;
6555 		n = *b;
6556 		for (i = 1; (uint8_t) i < n; i++)
6557 			printf(" %x", b[i]);
6558 		break;
6559 
6560 	case DW_OP_GNU_regval_type:
6561 		printf(": %ju (%s) <0x%jx>", (uintmax_t) lr->lr_number,
6562 		    dwarf_regname(re, (unsigned int) lr->lr_number),
6563 		    (uintmax_t) lr->lr_number2);
6564 		break;
6565 
6566 	case DW_OP_GNU_convert:
6567 	case DW_OP_GNU_deref_type:
6568 	case DW_OP_GNU_parameter_ref:
6569 	case DW_OP_GNU_reinterpret:
6570 		printf(": <0x%jx>", (uintmax_t) lr->lr_number);
6571 		break;
6572 
6573 	default:
6574 		break;
6575 	}
6576 }
6577 
6578 static void
6579 dump_dwarf_block(struct readelf *re, uint8_t *b, Dwarf_Unsigned len)
6580 {
6581 	Dwarf_Locdesc *llbuf;
6582 	Dwarf_Signed lcnt;
6583 	Dwarf_Error de;
6584 	int i;
6585 
6586 	if (dwarf_loclist_from_expr_b(re->dbg, b, len, re->cu_psize,
6587 	    re->cu_osize, re->cu_ver, &llbuf, &lcnt, &de) != DW_DLV_OK) {
6588 		warnx("dwarf_loclist_form_expr_b: %s", dwarf_errmsg(de));
6589 		return;
6590 	}
6591 
6592 	for (i = 0; (Dwarf_Half) i < llbuf->ld_cents; i++) {
6593 		dump_dwarf_loc(re, &llbuf->ld_s[i]);
6594 		if (i < llbuf->ld_cents - 1)
6595 			printf("; ");
6596 	}
6597 
6598 	dwarf_dealloc(re->dbg, llbuf->ld_s, DW_DLA_LOC_BLOCK);
6599 	dwarf_dealloc(re->dbg, llbuf, DW_DLA_LOCDESC);
6600 }
6601 
6602 static void
6603 dump_dwarf_loclist(struct readelf *re)
6604 {
6605 	Dwarf_Die die;
6606 	Dwarf_Locdesc **llbuf;
6607 	Dwarf_Unsigned lowpc;
6608 	Dwarf_Signed lcnt;
6609 	Dwarf_Half tag, version, pointer_size, off_size;
6610 	Dwarf_Error de;
6611 	struct loc_at *la_list, *left, *right, *la;
6612 	size_t la_list_len, la_list_cap;
6613 	unsigned int duplicates, k;
6614 	int i, j, ret, has_content;
6615 
6616 	la_list_len = 0;
6617 	la_list_cap = 200;
6618 	if ((la_list = calloc(la_list_cap, sizeof(struct loc_at))) == NULL)
6619 		errx(EXIT_FAILURE, "calloc failed");
6620 	/* Search .debug_info section. */
6621 	while ((ret = dwarf_next_cu_header_b(re->dbg, NULL, &version, NULL,
6622 	    &pointer_size, &off_size, NULL, NULL, &de)) == DW_DLV_OK) {
6623 		set_cu_context(re, pointer_size, off_size, version);
6624 		die = NULL;
6625 		if (dwarf_siblingof(re->dbg, die, &die, &de) != DW_DLV_OK)
6626 			continue;
6627 		if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
6628 			warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
6629 			continue;
6630 		}
6631 		/* XXX: What about DW_TAG_partial_unit? */
6632 		lowpc = 0;
6633 		if (tag == DW_TAG_compile_unit) {
6634 			if (dwarf_attrval_unsigned(die, DW_AT_low_pc,
6635 			    &lowpc, &de) != DW_DLV_OK)
6636 				lowpc = 0;
6637 		}
6638 
6639 		/* Search attributes for reference to .debug_loc section. */
6640 		search_loclist_at(re, die, lowpc, &la_list,
6641 		    &la_list_len, &la_list_cap);
6642 	}
6643 	if (ret == DW_DLV_ERROR)
6644 		warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
6645 
6646 	/* Search .debug_types section. */
6647 	do {
6648 		while ((ret = dwarf_next_cu_header_c(re->dbg, 0, NULL,
6649 		    &version, NULL, &pointer_size, &off_size, NULL, NULL,
6650 		    NULL, NULL, &de)) == DW_DLV_OK) {
6651 			set_cu_context(re, pointer_size, off_size, version);
6652 			die = NULL;
6653 			if (dwarf_siblingof(re->dbg, die, &die, &de) !=
6654 			    DW_DLV_OK)
6655 				continue;
6656 			if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
6657 				warnx("dwarf_tag failed: %s",
6658 				    dwarf_errmsg(de));
6659 				continue;
6660 			}
6661 
6662 			lowpc = 0;
6663 			if (tag == DW_TAG_type_unit) {
6664 				if (dwarf_attrval_unsigned(die, DW_AT_low_pc,
6665 				    &lowpc, &de) != DW_DLV_OK)
6666 					lowpc = 0;
6667 			}
6668 
6669 			/*
6670 			 * Search attributes for reference to .debug_loc
6671 			 * section.
6672 			 */
6673 			search_loclist_at(re, die, lowpc, &la_list,
6674 			    &la_list_len, &la_list_cap);
6675 		}
6676 		if (ret == DW_DLV_ERROR)
6677 			warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
6678 	} while (dwarf_next_types_section(re->dbg, &de) == DW_DLV_OK);
6679 
6680 	if (la_list_len == 0) {
6681 		free(la_list);
6682 		return;
6683 	}
6684 
6685 	/* Sort la_list using loc_at_comparator. */
6686 	qsort(la_list, la_list_len, sizeof(struct loc_at), loc_at_comparator);
6687 
6688 	/* Get rid of the duplicates in la_list. */
6689 	duplicates = 0;
6690 	for (k = 1; k < la_list_len; ++k) {
6691 		left = &la_list[k - 1 - duplicates];
6692 		right = &la_list[k];
6693 
6694 		if (left->la_off == right->la_off)
6695 			duplicates++;
6696 		else
6697 			la_list[k - duplicates] = *right;
6698 	}
6699 	la_list_len -= duplicates;
6700 
6701 	has_content = 0;
6702 	for (k = 0; k < la_list_len; ++k) {
6703 		la = &la_list[k];
6704 		if ((ret = dwarf_loclist_n(la->la_at, &llbuf, &lcnt, &de)) !=
6705 		    DW_DLV_OK) {
6706 			if (ret != DW_DLV_NO_ENTRY)
6707 				warnx("dwarf_loclist_n failed: %s",
6708 				    dwarf_errmsg(de));
6709 			continue;
6710 		}
6711 		if (!has_content) {
6712 			has_content = 1;
6713 			printf("\nContents of section .debug_loc:\n");
6714 			printf("    Offset   Begin    End      Expression\n");
6715 		}
6716 		set_cu_context(re, la->la_cu_psize, la->la_cu_osize,
6717 		    la->la_cu_ver);
6718 		for (i = 0; i < lcnt; i++) {
6719 			printf("    %8.8jx ", (uintmax_t) la->la_off);
6720 			if (llbuf[i]->ld_lopc == 0 && llbuf[i]->ld_hipc == 0) {
6721 				printf("<End of list>\n");
6722 				continue;
6723 			}
6724 
6725 			/* TODO: handle base selection entry. */
6726 
6727 			printf("%8.8jx %8.8jx ",
6728 			    (uintmax_t) (la->la_lowpc + llbuf[i]->ld_lopc),
6729 			    (uintmax_t) (la->la_lowpc + llbuf[i]->ld_hipc));
6730 
6731 			putchar('(');
6732 			for (j = 0; (Dwarf_Half) j < llbuf[i]->ld_cents; j++) {
6733 				dump_dwarf_loc(re, &llbuf[i]->ld_s[j]);
6734 				if (j < llbuf[i]->ld_cents - 1)
6735 					printf("; ");
6736 			}
6737 			putchar(')');
6738 
6739 			if (llbuf[i]->ld_lopc == llbuf[i]->ld_hipc)
6740 				printf(" (start == end)");
6741 			putchar('\n');
6742 		}
6743 		for (i = 0; i < lcnt; i++) {
6744 			dwarf_dealloc(re->dbg, llbuf[i]->ld_s,
6745 			    DW_DLA_LOC_BLOCK);
6746 			dwarf_dealloc(re->dbg, llbuf[i], DW_DLA_LOCDESC);
6747 		}
6748 		dwarf_dealloc(re->dbg, llbuf, DW_DLA_LIST);
6749 	}
6750 
6751 	if (!has_content)
6752 		printf("\nSection '.debug_loc' has no debugging data.\n");
6753 
6754 	free(la_list);
6755 }
6756 
6757 /*
6758  * Retrieve a string using string table section index and the string offset.
6759  */
6760 static const char*
6761 get_string(struct readelf *re, int strtab, size_t off)
6762 {
6763 	const char *name;
6764 
6765 	if ((name = elf_strptr(re->elf, strtab, off)) == NULL)
6766 		return ("");
6767 
6768 	return (name);
6769 }
6770 
6771 /*
6772  * Retrieve the name of a symbol using the section index of the symbol
6773  * table and the index of the symbol within that table.
6774  */
6775 static const char *
6776 get_symbol_name(struct readelf *re, int symtab, int i)
6777 {
6778 	struct section	*s;
6779 	const char	*name;
6780 	GElf_Sym	 sym;
6781 	Elf_Data	*data;
6782 	int		 elferr;
6783 
6784 	s = &re->sl[symtab];
6785 	if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM)
6786 		return ("");
6787 	(void) elf_errno();
6788 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
6789 		elferr = elf_errno();
6790 		if (elferr != 0)
6791 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
6792 		return ("");
6793 	}
6794 	if (gelf_getsym(data, i, &sym) != &sym)
6795 		return ("");
6796 	/* Return section name for STT_SECTION symbol. */
6797 	if (GELF_ST_TYPE(sym.st_info) == STT_SECTION) {
6798 		if (sym.st_shndx < re->shnum &&
6799 		    re->sl[sym.st_shndx].name != NULL)
6800 			return (re->sl[sym.st_shndx].name);
6801 		return ("");
6802 	}
6803 	if (s->link >= re->shnum ||
6804 	    (name = elf_strptr(re->elf, s->link, sym.st_name)) == NULL)
6805 		return ("");
6806 
6807 	return (name);
6808 }
6809 
6810 static uint64_t
6811 get_symbol_value(struct readelf *re, int symtab, int i)
6812 {
6813 	struct section	*s;
6814 	GElf_Sym	 sym;
6815 	Elf_Data	*data;
6816 	int		 elferr;
6817 
6818 	s = &re->sl[symtab];
6819 	if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM)
6820 		return (0);
6821 	(void) elf_errno();
6822 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
6823 		elferr = elf_errno();
6824 		if (elferr != 0)
6825 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
6826 		return (0);
6827 	}
6828 	if (gelf_getsym(data, i, &sym) != &sym)
6829 		return (0);
6830 
6831 	return (sym.st_value);
6832 }
6833 
6834 static void
6835 hex_dump(struct readelf *re)
6836 {
6837 	struct section *s;
6838 	Elf_Data *d;
6839 	uint8_t *buf;
6840 	size_t sz, nbytes;
6841 	uint64_t addr;
6842 	int elferr, i, j;
6843 
6844 	for (i = 1; (size_t) i < re->shnum; i++) {
6845 		s = &re->sl[i];
6846 		if (find_dumpop(re, (size_t) i, s->name, HEX_DUMP, -1) == NULL)
6847 			continue;
6848 		(void) elf_errno();
6849 		if ((d = elf_getdata(s->scn, NULL)) == NULL &&
6850 		    (d = elf_rawdata(s->scn, NULL)) == NULL) {
6851 			elferr = elf_errno();
6852 			if (elferr != 0)
6853 				warnx("elf_getdata failed: %s",
6854 				    elf_errmsg(elferr));
6855 			continue;
6856 		}
6857 		(void) elf_errno();
6858 		if (d->d_size <= 0 || d->d_buf == NULL) {
6859 			printf("\nSection '%s' has no data to dump.\n",
6860 			    s->name);
6861 			continue;
6862 		}
6863 		buf = d->d_buf;
6864 		sz = d->d_size;
6865 		addr = s->addr;
6866 		printf("\nHex dump of section '%s':\n", s->name);
6867 		while (sz > 0) {
6868 			printf("  0x%8.8jx ", (uintmax_t)addr);
6869 			nbytes = sz > 16? 16 : sz;
6870 			for (j = 0; j < 16; j++) {
6871 				if ((size_t)j < nbytes)
6872 					printf("%2.2x", buf[j]);
6873 				else
6874 					printf("  ");
6875 				if ((j & 3) == 3)
6876 					printf(" ");
6877 			}
6878 			for (j = 0; (size_t)j < nbytes; j++) {
6879 				if (isprint(buf[j]))
6880 					printf("%c", buf[j]);
6881 				else
6882 					printf(".");
6883 			}
6884 			printf("\n");
6885 			buf += nbytes;
6886 			addr += nbytes;
6887 			sz -= nbytes;
6888 		}
6889 	}
6890 }
6891 
6892 static void
6893 str_dump(struct readelf *re)
6894 {
6895 	struct section *s;
6896 	Elf_Data *d;
6897 	unsigned char *start, *end, *buf_end;
6898 	unsigned int len;
6899 	int i, j, elferr, found;
6900 
6901 	for (i = 1; (size_t) i < re->shnum; i++) {
6902 		s = &re->sl[i];
6903 		if (find_dumpop(re, (size_t) i, s->name, STR_DUMP, -1) == NULL)
6904 			continue;
6905 		(void) elf_errno();
6906 		if ((d = elf_getdata(s->scn, NULL)) == NULL &&
6907 		    (d = elf_rawdata(s->scn, NULL)) == NULL) {
6908 			elferr = elf_errno();
6909 			if (elferr != 0)
6910 				warnx("elf_getdata failed: %s",
6911 				    elf_errmsg(elferr));
6912 			continue;
6913 		}
6914 		(void) elf_errno();
6915 		if (d->d_size <= 0 || d->d_buf == NULL) {
6916 			printf("\nSection '%s' has no data to dump.\n",
6917 			    s->name);
6918 			continue;
6919 		}
6920 		buf_end = (unsigned char *) d->d_buf + d->d_size;
6921 		start = (unsigned char *) d->d_buf;
6922 		found = 0;
6923 		printf("\nString dump of section '%s':\n", s->name);
6924 		for (;;) {
6925 			while (start < buf_end && !isprint(*start))
6926 				start++;
6927 			if (start >= buf_end)
6928 				break;
6929 			end = start + 1;
6930 			while (end < buf_end && isprint(*end))
6931 				end++;
6932 			printf("  [%6lx]  ",
6933 			    (long) (start - (unsigned char *) d->d_buf));
6934 			len = end - start;
6935 			for (j = 0; (unsigned int) j < len; j++)
6936 				putchar(start[j]);
6937 			putchar('\n');
6938 			found = 1;
6939 			if (end >= buf_end)
6940 				break;
6941 			start = end + 1;
6942 		}
6943 		if (!found)
6944 			printf("  No strings found in this section.");
6945 		putchar('\n');
6946 	}
6947 }
6948 
6949 static void
6950 load_sections(struct readelf *re)
6951 {
6952 	struct section	*s;
6953 	const char	*name;
6954 	Elf_Scn		*scn;
6955 	GElf_Shdr	 sh;
6956 	size_t		 shstrndx, ndx;
6957 	int		 elferr;
6958 
6959 	/* Allocate storage for internal section list. */
6960 	if (!elf_getshnum(re->elf, &re->shnum)) {
6961 		warnx("elf_getshnum failed: %s", elf_errmsg(-1));
6962 		return;
6963 	}
6964 	if (re->sl != NULL)
6965 		free(re->sl);
6966 	if ((re->sl = calloc(re->shnum, sizeof(*re->sl))) == NULL)
6967 		err(EXIT_FAILURE, "calloc failed");
6968 
6969 	/* Get the index of .shstrtab section. */
6970 	if (!elf_getshstrndx(re->elf, &shstrndx)) {
6971 		warnx("elf_getshstrndx failed: %s", elf_errmsg(-1));
6972 		return;
6973 	}
6974 
6975 	if ((scn = elf_getscn(re->elf, 0)) == NULL)
6976 		return;
6977 
6978 	(void) elf_errno();
6979 	do {
6980 		if (gelf_getshdr(scn, &sh) == NULL) {
6981 			warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
6982 			(void) elf_errno();
6983 			continue;
6984 		}
6985 		if ((name = elf_strptr(re->elf, shstrndx, sh.sh_name)) == NULL) {
6986 			(void) elf_errno();
6987 			name = "<no-name>";
6988 		}
6989 		if ((ndx = elf_ndxscn(scn)) == SHN_UNDEF) {
6990 			if ((elferr = elf_errno()) != 0) {
6991 				warnx("elf_ndxscn failed: %s",
6992 				    elf_errmsg(elferr));
6993 				continue;
6994 			}
6995 		}
6996 		if (ndx >= re->shnum) {
6997 			warnx("section index of '%s' out of range", name);
6998 			continue;
6999 		}
7000 		if (sh.sh_link >= re->shnum)
7001 			warnx("section link %llu of '%s' out of range",
7002 			    (unsigned long long)sh.sh_link, name);
7003 		s = &re->sl[ndx];
7004 		s->name = name;
7005 		s->scn = scn;
7006 		s->off = sh.sh_offset;
7007 		s->sz = sh.sh_size;
7008 		s->entsize = sh.sh_entsize;
7009 		s->align = sh.sh_addralign;
7010 		s->type = sh.sh_type;
7011 		s->flags = sh.sh_flags;
7012 		s->addr = sh.sh_addr;
7013 		s->link = sh.sh_link;
7014 		s->info = sh.sh_info;
7015 	} while ((scn = elf_nextscn(re->elf, scn)) != NULL);
7016 	elferr = elf_errno();
7017 	if (elferr != 0)
7018 		warnx("elf_nextscn failed: %s", elf_errmsg(elferr));
7019 }
7020 
7021 static void
7022 unload_sections(struct readelf *re)
7023 {
7024 
7025 	if (re->sl != NULL) {
7026 		free(re->sl);
7027 		re->sl = NULL;
7028 	}
7029 	re->shnum = 0;
7030 	re->vd_s = NULL;
7031 	re->vn_s = NULL;
7032 	re->vs_s = NULL;
7033 	re->vs = NULL;
7034 	re->vs_sz = 0;
7035 	if (re->ver != NULL) {
7036 		free(re->ver);
7037 		re->ver = NULL;
7038 		re->ver_sz = 0;
7039 	}
7040 }
7041 
7042 static void
7043 dump_elf(struct readelf *re)
7044 {
7045 
7046 	/* Fetch ELF header. No need to continue if it fails. */
7047 	if (gelf_getehdr(re->elf, &re->ehdr) == NULL) {
7048 		warnx("gelf_getehdr failed: %s", elf_errmsg(-1));
7049 		return;
7050 	}
7051 	if ((re->ec = gelf_getclass(re->elf)) == ELFCLASSNONE) {
7052 		warnx("gelf_getclass failed: %s", elf_errmsg(-1));
7053 		return;
7054 	}
7055 	if (re->ehdr.e_ident[EI_DATA] == ELFDATA2MSB) {
7056 		re->dw_read = _read_msb;
7057 		re->dw_decode = _decode_msb;
7058 	} else {
7059 		re->dw_read = _read_lsb;
7060 		re->dw_decode = _decode_lsb;
7061 	}
7062 
7063 	if (re->options & ~RE_H)
7064 		load_sections(re);
7065 	if ((re->options & RE_VV) || (re->options & RE_S))
7066 		search_ver(re);
7067 	if (re->options & RE_H)
7068 		dump_ehdr(re);
7069 	if (re->options & RE_L)
7070 		dump_phdr(re);
7071 	if (re->options & RE_SS)
7072 		dump_shdr(re);
7073 	if (re->options & RE_G)
7074 		dump_section_groups(re);
7075 	if (re->options & RE_D)
7076 		dump_dynamic(re);
7077 	if (re->options & RE_R)
7078 		dump_reloc(re);
7079 	if (re->options & RE_S)
7080 		dump_symtabs(re);
7081 	if (re->options & RE_N)
7082 		dump_notes(re);
7083 	if (re->options & RE_II)
7084 		dump_hash(re);
7085 	if (re->options & RE_X)
7086 		hex_dump(re);
7087 	if (re->options & RE_P)
7088 		str_dump(re);
7089 	if (re->options & RE_VV)
7090 		dump_ver(re);
7091 	if (re->options & RE_AA)
7092 		dump_arch_specific_info(re);
7093 	if (re->options & RE_W)
7094 		dump_dwarf(re);
7095 	if (re->options & ~RE_H)
7096 		unload_sections(re);
7097 }
7098 
7099 static void
7100 dump_dwarf(struct readelf *re)
7101 {
7102 	Dwarf_Error de;
7103 	int error;
7104 
7105 	if (dwarf_elf_init(re->elf, DW_DLC_READ, NULL, NULL, &re->dbg, &de)) {
7106 		if ((error = dwarf_errno(de)) != DW_DLE_DEBUG_INFO_NULL)
7107 			errx(EXIT_FAILURE, "dwarf_elf_init failed: %s",
7108 			    dwarf_errmsg(de));
7109 		return;
7110 	}
7111 
7112 	if (re->dop & DW_A)
7113 		dump_dwarf_abbrev(re);
7114 	if (re->dop & DW_L)
7115 		dump_dwarf_line(re);
7116 	if (re->dop & DW_LL)
7117 		dump_dwarf_line_decoded(re);
7118 	if (re->dop & DW_I) {
7119 		dump_dwarf_info(re, 0);
7120 		dump_dwarf_info(re, 1);
7121 	}
7122 	if (re->dop & DW_P)
7123 		dump_dwarf_pubnames(re);
7124 	if (re->dop & DW_R)
7125 		dump_dwarf_aranges(re);
7126 	if (re->dop & DW_RR)
7127 		dump_dwarf_ranges(re);
7128 	if (re->dop & DW_M)
7129 		dump_dwarf_macinfo(re);
7130 	if (re->dop & DW_F)
7131 		dump_dwarf_frame(re, 0);
7132 	else if (re->dop & DW_FF)
7133 		dump_dwarf_frame(re, 1);
7134 	if (re->dop & DW_S)
7135 		dump_dwarf_str(re);
7136 	if (re->dop & DW_O)
7137 		dump_dwarf_loclist(re);
7138 
7139 	dwarf_finish(re->dbg, &de);
7140 }
7141 
7142 static void
7143 dump_ar(struct readelf *re, int fd)
7144 {
7145 	Elf_Arsym *arsym;
7146 	Elf_Arhdr *arhdr;
7147 	Elf_Cmd cmd;
7148 	Elf *e;
7149 	size_t sz;
7150 	off_t off;
7151 	int i;
7152 
7153 	re->ar = re->elf;
7154 
7155 	if (re->options & RE_C) {
7156 		if ((arsym = elf_getarsym(re->ar, &sz)) == NULL) {
7157 			warnx("elf_getarsym() failed: %s", elf_errmsg(-1));
7158 			goto process_members;
7159 		}
7160 		printf("Index of archive %s: (%ju entries)\n", re->filename,
7161 		    (uintmax_t) sz - 1);
7162 		off = 0;
7163 		for (i = 0; (size_t) i < sz; i++) {
7164 			if (arsym[i].as_name == NULL)
7165 				break;
7166 			if (arsym[i].as_off != off) {
7167 				off = arsym[i].as_off;
7168 				if (elf_rand(re->ar, off) != off) {
7169 					warnx("elf_rand() failed: %s",
7170 					    elf_errmsg(-1));
7171 					continue;
7172 				}
7173 				if ((e = elf_begin(fd, ELF_C_READ, re->ar)) ==
7174 				    NULL) {
7175 					warnx("elf_begin() failed: %s",
7176 					    elf_errmsg(-1));
7177 					continue;
7178 				}
7179 				if ((arhdr = elf_getarhdr(e)) == NULL) {
7180 					warnx("elf_getarhdr() failed: %s",
7181 					    elf_errmsg(-1));
7182 					elf_end(e);
7183 					continue;
7184 				}
7185 				printf("Binary %s(%s) contains:\n",
7186 				    re->filename, arhdr->ar_name);
7187 			}
7188 			printf("\t%s\n", arsym[i].as_name);
7189 		}
7190 		if (elf_rand(re->ar, SARMAG) != SARMAG) {
7191 			warnx("elf_rand() failed: %s", elf_errmsg(-1));
7192 			return;
7193 		}
7194 	}
7195 
7196 process_members:
7197 
7198 	if ((re->options & ~RE_C) == 0)
7199 		return;
7200 
7201 	cmd = ELF_C_READ;
7202 	while ((re->elf = elf_begin(fd, cmd, re->ar)) != NULL) {
7203 		if ((arhdr = elf_getarhdr(re->elf)) == NULL) {
7204 			warnx("elf_getarhdr() failed: %s", elf_errmsg(-1));
7205 			goto next_member;
7206 		}
7207 		if (strcmp(arhdr->ar_name, "/") == 0 ||
7208 		    strcmp(arhdr->ar_name, "//") == 0 ||
7209 		    strcmp(arhdr->ar_name, "__.SYMDEF") == 0)
7210 			goto next_member;
7211 		printf("\nFile: %s(%s)\n", re->filename, arhdr->ar_name);
7212 		dump_elf(re);
7213 
7214 	next_member:
7215 		cmd = elf_next(re->elf);
7216 		elf_end(re->elf);
7217 	}
7218 	re->elf = re->ar;
7219 }
7220 
7221 static void
7222 dump_object(struct readelf *re, int fd)
7223 {
7224 	if ((re->flags & DISPLAY_FILENAME) != 0)
7225 		printf("\nFile: %s\n", re->filename);
7226 
7227 	if ((re->elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
7228 		warnx("elf_begin() failed: %s", elf_errmsg(-1));
7229 		return;
7230 	}
7231 
7232 	switch (elf_kind(re->elf)) {
7233 	case ELF_K_NONE:
7234 		warnx("Not an ELF file.");
7235 		return;
7236 	case ELF_K_ELF:
7237 		dump_elf(re);
7238 		break;
7239 	case ELF_K_AR:
7240 		dump_ar(re, fd);
7241 		break;
7242 	default:
7243 		warnx("Internal: libelf returned unknown elf kind.");
7244 		return;
7245 	}
7246 
7247 	elf_end(re->elf);
7248 }
7249 
7250 static void
7251 add_dumpop(struct readelf *re, size_t si, const char *sn, int op, int t)
7252 {
7253 	struct dumpop *d;
7254 
7255 	if ((d = find_dumpop(re, si, sn, -1, t)) == NULL) {
7256 		if ((d = calloc(1, sizeof(*d))) == NULL)
7257 			err(EXIT_FAILURE, "calloc failed");
7258 		if (t == DUMP_BY_INDEX)
7259 			d->u.si = si;
7260 		else
7261 			d->u.sn = sn;
7262 		d->type = t;
7263 		d->op = op;
7264 		STAILQ_INSERT_TAIL(&re->v_dumpop, d, dumpop_list);
7265 	} else
7266 		d->op |= op;
7267 }
7268 
7269 static struct dumpop *
7270 find_dumpop(struct readelf *re, size_t si, const char *sn, int op, int t)
7271 {
7272 	struct dumpop *d;
7273 
7274 	STAILQ_FOREACH(d, &re->v_dumpop, dumpop_list) {
7275 		if ((op == -1 || op & d->op) &&
7276 		    (t == -1 || (unsigned) t == d->type)) {
7277 			if ((d->type == DUMP_BY_INDEX && d->u.si == si) ||
7278 			    (d->type == DUMP_BY_NAME && !strcmp(d->u.sn, sn)))
7279 				return (d);
7280 		}
7281 	}
7282 
7283 	return (NULL);
7284 }
7285 
7286 static struct {
7287 	const char *ln;
7288 	char sn;
7289 	int value;
7290 } dwarf_op[] = {
7291 	{"rawline", 'l', DW_L},
7292 	{"decodedline", 'L', DW_LL},
7293 	{"info", 'i', DW_I},
7294 	{"abbrev", 'a', DW_A},
7295 	{"pubnames", 'p', DW_P},
7296 	{"aranges", 'r', DW_R},
7297 	{"ranges", 'r', DW_R},
7298 	{"Ranges", 'R', DW_RR},
7299 	{"macro", 'm', DW_M},
7300 	{"frames", 'f', DW_F},
7301 	{"frames-interp", 'F', DW_FF},
7302 	{"str", 's', DW_S},
7303 	{"loc", 'o', DW_O},
7304 	{NULL, 0, 0}
7305 };
7306 
7307 static void
7308 parse_dwarf_op_short(struct readelf *re, const char *op)
7309 {
7310 	int i;
7311 
7312 	if (op == NULL) {
7313 		re->dop |= DW_DEFAULT_OPTIONS;
7314 		return;
7315 	}
7316 
7317 	for (; *op != '\0'; op++) {
7318 		for (i = 0; dwarf_op[i].ln != NULL; i++) {
7319 			if (dwarf_op[i].sn == *op) {
7320 				re->dop |= dwarf_op[i].value;
7321 				break;
7322 			}
7323 		}
7324 	}
7325 }
7326 
7327 static void
7328 parse_dwarf_op_long(struct readelf *re, const char *op)
7329 {
7330 	char *p, *token, *bp;
7331 	int i;
7332 
7333 	if (op == NULL) {
7334 		re->dop |= DW_DEFAULT_OPTIONS;
7335 		return;
7336 	}
7337 
7338 	if ((p = strdup(op)) == NULL)
7339 		err(EXIT_FAILURE, "strdup failed");
7340 	bp = p;
7341 
7342 	while ((token = strsep(&p, ",")) != NULL) {
7343 		for (i = 0; dwarf_op[i].ln != NULL; i++) {
7344 			if (!strcmp(token, dwarf_op[i].ln)) {
7345 				re->dop |= dwarf_op[i].value;
7346 				break;
7347 			}
7348 		}
7349 	}
7350 
7351 	free(bp);
7352 }
7353 
7354 static uint64_t
7355 _read_lsb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read)
7356 {
7357 	uint64_t ret;
7358 	uint8_t *src;
7359 
7360 	src = (uint8_t *) d->d_buf + *offsetp;
7361 
7362 	ret = 0;
7363 	switch (bytes_to_read) {
7364 	case 8:
7365 		ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
7366 		ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
7367 		/* FALLTHROUGH */
7368 	case 4:
7369 		ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
7370 		/* FALLTHROUGH */
7371 	case 2:
7372 		ret |= ((uint64_t) src[1]) << 8;
7373 		/* FALLTHROUGH */
7374 	case 1:
7375 		ret |= src[0];
7376 		break;
7377 	default:
7378 		return (0);
7379 	}
7380 
7381 	*offsetp += bytes_to_read;
7382 
7383 	return (ret);
7384 }
7385 
7386 static uint64_t
7387 _read_msb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read)
7388 {
7389 	uint64_t ret;
7390 	uint8_t *src;
7391 
7392 	src = (uint8_t *) d->d_buf + *offsetp;
7393 
7394 	switch (bytes_to_read) {
7395 	case 1:
7396 		ret = src[0];
7397 		break;
7398 	case 2:
7399 		ret = src[1] | ((uint64_t) src[0]) << 8;
7400 		break;
7401 	case 4:
7402 		ret = src[3] | ((uint64_t) src[2]) << 8;
7403 		ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24;
7404 		break;
7405 	case 8:
7406 		ret = src[7] | ((uint64_t) src[6]) << 8;
7407 		ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24;
7408 		ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40;
7409 		ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56;
7410 		break;
7411 	default:
7412 		return (0);
7413 	}
7414 
7415 	*offsetp += bytes_to_read;
7416 
7417 	return (ret);
7418 }
7419 
7420 static uint64_t
7421 _decode_lsb(uint8_t **data, int bytes_to_read)
7422 {
7423 	uint64_t ret;
7424 	uint8_t *src;
7425 
7426 	src = *data;
7427 
7428 	ret = 0;
7429 	switch (bytes_to_read) {
7430 	case 8:
7431 		ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
7432 		ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
7433 		/* FALLTHROUGH */
7434 	case 4:
7435 		ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
7436 		/* FALLTHROUGH */
7437 	case 2:
7438 		ret |= ((uint64_t) src[1]) << 8;
7439 		/* FALLTHROUGH */
7440 	case 1:
7441 		ret |= src[0];
7442 		break;
7443 	default:
7444 		return (0);
7445 	}
7446 
7447 	*data += bytes_to_read;
7448 
7449 	return (ret);
7450 }
7451 
7452 static uint64_t
7453 _decode_msb(uint8_t **data, int bytes_to_read)
7454 {
7455 	uint64_t ret;
7456 	uint8_t *src;
7457 
7458 	src = *data;
7459 
7460 	ret = 0;
7461 	switch (bytes_to_read) {
7462 	case 1:
7463 		ret = src[0];
7464 		break;
7465 	case 2:
7466 		ret = src[1] | ((uint64_t) src[0]) << 8;
7467 		break;
7468 	case 4:
7469 		ret = src[3] | ((uint64_t) src[2]) << 8;
7470 		ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24;
7471 		break;
7472 	case 8:
7473 		ret = src[7] | ((uint64_t) src[6]) << 8;
7474 		ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24;
7475 		ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40;
7476 		ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56;
7477 		break;
7478 	default:
7479 		return (0);
7480 		break;
7481 	}
7482 
7483 	*data += bytes_to_read;
7484 
7485 	return (ret);
7486 }
7487 
7488 static int64_t
7489 _decode_sleb128(uint8_t **dp, uint8_t *dpe)
7490 {
7491 	int64_t ret = 0;
7492 	uint8_t b = 0;
7493 	int shift = 0;
7494 
7495 	uint8_t *src = *dp;
7496 
7497 	do {
7498 		if (src >= dpe)
7499 			break;
7500 		b = *src++;
7501 		ret |= ((b & 0x7f) << shift);
7502 		shift += 7;
7503 	} while ((b & 0x80) != 0);
7504 
7505 	if (shift < 32 && (b & 0x40) != 0)
7506 		ret |= (-1 << shift);
7507 
7508 	*dp = src;
7509 
7510 	return (ret);
7511 }
7512 
7513 static uint64_t
7514 _decode_uleb128(uint8_t **dp, uint8_t *dpe)
7515 {
7516 	uint64_t ret = 0;
7517 	uint8_t b;
7518 	int shift = 0;
7519 
7520 	uint8_t *src = *dp;
7521 
7522 	do {
7523 		if (src >= dpe)
7524 			break;
7525 		b = *src++;
7526 		ret |= ((b & 0x7f) << shift);
7527 		shift += 7;
7528 	} while ((b & 0x80) != 0);
7529 
7530 	*dp = src;
7531 
7532 	return (ret);
7533 }
7534 
7535 static void
7536 readelf_version(void)
7537 {
7538 	(void) printf("%s (%s)\n", ELFTC_GETPROGNAME(),
7539 	    elftc_version());
7540 	exit(EXIT_SUCCESS);
7541 }
7542 
7543 #define	USAGE_MESSAGE	"\
7544 Usage: %s [options] file...\n\
7545   Display information about ELF objects and ar(1) archives.\n\n\
7546   Options:\n\
7547   -a | --all               Equivalent to specifying options '-dhIlrsASV'.\n\
7548   -c | --archive-index     Print the archive symbol table for archives.\n\
7549   -d | --dynamic           Print the contents of SHT_DYNAMIC sections.\n\
7550   -e | --headers           Print all headers in the object.\n\
7551   -g | --section-groups    Print the contents of the section groups.\n\
7552   -h | --file-header       Print the file header for the object.\n\
7553   -l | --program-headers   Print the PHDR table for the object.\n\
7554   -n | --notes             Print the contents of SHT_NOTE sections.\n\
7555   -p INDEX | --string-dump=INDEX\n\
7556                            Print the contents of section at index INDEX.\n\
7557   -r | --relocs            Print relocation information.\n\
7558   -s | --syms | --symbols  Print symbol tables.\n\
7559   -t | --section-details   Print additional information about sections.\n\
7560   -v | --version           Print a version identifier and exit.\n\
7561   -w[afilmoprsFLR] | --debug-dump={abbrev,aranges,decodedline,frames,\n\
7562                                frames-interp,info,loc,macro,pubnames,\n\
7563                                ranges,Ranges,rawline,str}\n\
7564                            Display DWARF information.\n\
7565   -x INDEX | --hex-dump=INDEX\n\
7566                            Display contents of a section as hexadecimal.\n\
7567   -A | --arch-specific     (accepted, but ignored)\n\
7568   -D | --use-dynamic       Print the symbol table specified by the DT_SYMTAB\n\
7569                            entry in the \".dynamic\" section.\n\
7570   -H | --help              Print a help message.\n\
7571   -I | --histogram         Print information on bucket list lengths for \n\
7572                            hash sections.\n\
7573   -N | --full-section-name (accepted, but ignored)\n\
7574   -S | --sections | --section-headers\n\
7575                            Print information about section headers.\n\
7576   -V | --version-info      Print symbol versoning information.\n\
7577   -W | --wide              Print information without wrapping long lines.\n"
7578 
7579 
7580 static void
7581 readelf_usage(int status)
7582 {
7583 	fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());
7584 	exit(status);
7585 }
7586 
7587 int
7588 main(int argc, char **argv)
7589 {
7590 	cap_rights_t	rights;
7591 	fileargs_t	*fa;
7592 	struct readelf	*re, re_storage;
7593 	unsigned long	 si;
7594 	int		 fd, opt, i;
7595 	char		*ep;
7596 
7597 	re = &re_storage;
7598 	memset(re, 0, sizeof(*re));
7599 	STAILQ_INIT(&re->v_dumpop);
7600 
7601 	while ((opt = getopt_long(argc, argv, "AacDdegHhIi:lNnp:rSstuVvWw::x:",
7602 	    longopts, NULL)) != -1) {
7603 		switch(opt) {
7604 		case '?':
7605 			readelf_usage(EXIT_SUCCESS);
7606 			break;
7607 		case 'A':
7608 			re->options |= RE_AA;
7609 			break;
7610 		case 'a':
7611 			re->options |= RE_AA | RE_D | RE_G | RE_H | RE_II |
7612 			    RE_L | RE_R | RE_SS | RE_S | RE_VV;
7613 			break;
7614 		case 'c':
7615 			re->options |= RE_C;
7616 			break;
7617 		case 'D':
7618 			re->options |= RE_DD;
7619 			break;
7620 		case 'd':
7621 			re->options |= RE_D;
7622 			break;
7623 		case 'e':
7624 			re->options |= RE_H | RE_L | RE_SS;
7625 			break;
7626 		case 'g':
7627 			re->options |= RE_G;
7628 			break;
7629 		case 'H':
7630 			readelf_usage(EXIT_SUCCESS);
7631 			break;
7632 		case 'h':
7633 			re->options |= RE_H;
7634 			break;
7635 		case 'I':
7636 			re->options |= RE_II;
7637 			break;
7638 		case 'i':
7639 			/* Not implemented yet. */
7640 			break;
7641 		case 'l':
7642 			re->options |= RE_L;
7643 			break;
7644 		case 'N':
7645 			re->options |= RE_NN;
7646 			break;
7647 		case 'n':
7648 			re->options |= RE_N;
7649 			break;
7650 		case 'p':
7651 			re->options |= RE_P;
7652 			si = strtoul(optarg, &ep, 10);
7653 			if (*ep == '\0')
7654 				add_dumpop(re, (size_t) si, NULL, STR_DUMP,
7655 				    DUMP_BY_INDEX);
7656 			else
7657 				add_dumpop(re, 0, optarg, STR_DUMP,
7658 				    DUMP_BY_NAME);
7659 			break;
7660 		case 'r':
7661 			re->options |= RE_R;
7662 			break;
7663 		case 'S':
7664 			re->options |= RE_SS;
7665 			break;
7666 		case 's':
7667 			re->options |= RE_S;
7668 			break;
7669 		case 't':
7670 			re->options |= RE_SS | RE_T;
7671 			break;
7672 		case 'u':
7673 			re->options |= RE_U;
7674 			break;
7675 		case 'V':
7676 			re->options |= RE_VV;
7677 			break;
7678 		case 'v':
7679 			readelf_version();
7680 			break;
7681 		case 'W':
7682 			re->options |= RE_WW;
7683 			break;
7684 		case 'w':
7685 			re->options |= RE_W;
7686 			parse_dwarf_op_short(re, optarg);
7687 			break;
7688 		case 'x':
7689 			re->options |= RE_X;
7690 			si = strtoul(optarg, &ep, 10);
7691 			if (*ep == '\0')
7692 				add_dumpop(re, (size_t) si, NULL, HEX_DUMP,
7693 				    DUMP_BY_INDEX);
7694 			else
7695 				add_dumpop(re, 0, optarg, HEX_DUMP,
7696 				    DUMP_BY_NAME);
7697 			break;
7698 		case OPTION_DEBUG_DUMP:
7699 			re->options |= RE_W;
7700 			parse_dwarf_op_long(re, optarg);
7701 		}
7702 	}
7703 
7704 	argv += optind;
7705 	argc -= optind;
7706 
7707 	if (argc == 0 || re->options == 0)
7708 		readelf_usage(EXIT_FAILURE);
7709 
7710 	if (argc > 1)
7711 		re->flags |= DISPLAY_FILENAME;
7712 
7713 	if (elf_version(EV_CURRENT) == EV_NONE)
7714 		errx(EXIT_FAILURE, "ELF library initialization failed: %s",
7715 		    elf_errmsg(-1));
7716 
7717 	cap_rights_init(&rights, CAP_FCNTL, CAP_FSTAT, CAP_MMAP_R, CAP_SEEK);
7718 	fa = fileargs_init(argc, argv, O_RDONLY, 0, &rights, FA_OPEN);
7719 	if (fa == NULL)
7720 		err(1, "Unable to initialize casper fileargs");
7721 
7722 	caph_cache_catpages();
7723 	if (caph_limit_stdio() < 0) {
7724 		fileargs_free(fa);
7725 		err(1, "Unable to limit stdio rights");
7726 	}
7727 	if (caph_enter_casper() < 0) {
7728 		fileargs_free(fa);
7729 		err(1, "Unable to enter capability mode");
7730 	}
7731 
7732 	for (i = 0; i < argc; i++) {
7733 		re->filename = argv[i];
7734 		fd = fileargs_open(fa, re->filename);
7735 		if (fd < 0) {
7736 			warn("open %s failed", re->filename);
7737 		} else {
7738 			dump_object(re, fd);
7739 			close(fd);
7740 		}
7741 	}
7742 
7743 	exit(EXIT_SUCCESS);
7744 }
7745