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