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