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