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