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