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