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