xref: /freebsd/contrib/elftoolchain/readelf/readelf.c (revision ec0e626bafb335b30c499d06066997f54b10c092)
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 3155 2015-02-15 19:15:57Z emaste $");
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 || strcmp(name, "LINUX") == 0) &&
1507 	    et == ET_CORE)
1508 		return note_type_linux_core(nt);
1509 	else if (strcmp(name, "FreeBSD") == 0)
1510 		if (et == ET_CORE)
1511 			return note_type_freebsd_core(nt);
1512 		else
1513 			return note_type_freebsd(nt);
1514 	else if (strcmp(name, "GNU") == 0 && et != ET_CORE)
1515 		return note_type_gnu(nt);
1516 	else if (strcmp(name, "NetBSD") == 0 && et != ET_CORE)
1517 		return note_type_netbsd(nt);
1518 	else if (strcmp(name, "OpenBSD") == 0 && et != ET_CORE)
1519 		return note_type_openbsd(nt);
1520 	return note_type_unknown(nt);
1521 }
1522 
1523 static const char *
1524 note_type_freebsd(unsigned int nt)
1525 {
1526 	switch (nt) {
1527 	case 1: return "NT_FREEBSD_ABI_TAG";
1528 	case 2: return "NT_FREEBSD_NOINIT_TAG";
1529 	case 3: return "NT_FREEBSD_ARCH_TAG";
1530 	default: return (note_type_unknown(nt));
1531 	}
1532 }
1533 
1534 static const char *
1535 note_type_freebsd_core(unsigned int nt)
1536 {
1537 	switch (nt) {
1538 	case 1: return "NT_PRSTATUS";
1539 	case 2: return "NT_FPREGSET";
1540 	case 3: return "NT_PRPSINFO";
1541 	case 7: return "NT_THRMISC";
1542 	case 8: return "NT_PROCSTAT_PROC";
1543 	case 9: return "NT_PROCSTAT_FILES";
1544 	case 10: return "NT_PROCSTAT_VMMAP";
1545 	case 11: return "NT_PROCSTAT_GROUPS";
1546 	case 12: return "NT_PROCSTAT_UMASK";
1547 	case 13: return "NT_PROCSTAT_RLIMIT";
1548 	case 14: return "NT_PROCSTAT_OSREL";
1549 	case 15: return "NT_PROCSTAT_PSSTRINGS";
1550 	case 16: return "NT_PROCSTAT_AUXV";
1551 	case 0x202: return "NT_X86_XSTATE (x86 XSAVE extended state)";
1552 	default: return (note_type_unknown(nt));
1553 	}
1554 }
1555 
1556 static const char *
1557 note_type_linux_core(unsigned int nt)
1558 {
1559 	switch (nt) {
1560 	case 1: return "NT_PRSTATUS (Process status)";
1561 	case 2: return "NT_FPREGSET (Floating point information)";
1562 	case 3: return "NT_PRPSINFO (Process information)";
1563 	case 4: return "NT_TASKSTRUCT (Task structure)";
1564 	case 6: return "NT_AUXV (Auxiliary vector)";
1565 	case 10: return "NT_PSTATUS (Linux process status)";
1566 	case 12: return "NT_FPREGS (Linux floating point regset)";
1567 	case 13: return "NT_PSINFO (Linux process information)";
1568 	case 16: return "NT_LWPSTATUS (Linux lwpstatus_t type)";
1569 	case 17: return "NT_LWPSINFO (Linux lwpinfo_t type)";
1570 	case 18: return "NT_WIN32PSTATUS (win32_pstatus structure)";
1571 	case 0x100: return "NT_PPC_VMX (ppc Altivec registers)";
1572 	case 0x102: return "NT_PPC_VSX (ppc VSX registers)";
1573 	case 0x202: return "NT_X86_XSTATE (x86 XSAVE extended state)";
1574 	case 0x300: return "NT_S390_HIGH_GPRS (s390 upper register halves)";
1575 	case 0x301: return "NT_S390_TIMER (s390 timer register)";
1576 	case 0x302: return "NT_S390_TODCMP (s390 TOD comparator register)";
1577 	case 0x303: return "NT_S390_TODPREG (s390 TOD programmable register)";
1578 	case 0x304: return "NT_S390_CTRS (s390 control registers)";
1579 	case 0x305: return "NT_S390_PREFIX (s390 prefix register)";
1580 	case 0x400: return "NT_ARM_VFP (arm VFP registers)";
1581 	case 0x46494c45UL: return "NT_FILE (mapped files)";
1582 	case 0x46E62B7FUL: return "NT_PRXFPREG (Linux user_xfpregs structure)";
1583 	case 0x53494749UL: return "NT_SIGINFO (siginfo_t data)";
1584 	default: return (note_type_unknown(nt));
1585 	}
1586 }
1587 
1588 static const char *
1589 note_type_gnu(unsigned int nt)
1590 {
1591 	switch (nt) {
1592 	case 1: return "NT_GNU_ABI_TAG";
1593 	case 2: return "NT_GNU_HWCAP (Hardware capabilities)";
1594 	case 3: return "NT_GNU_BUILD_ID (Build id set by ld(1))";
1595 	case 4: return "NT_GNU_GOLD_VERSION (GNU gold version)";
1596 	default: return (note_type_unknown(nt));
1597 	}
1598 }
1599 
1600 static const char *
1601 note_type_netbsd(unsigned int nt)
1602 {
1603 	switch (nt) {
1604 	case 1: return "NT_NETBSD_IDENT";
1605 	default: return (note_type_unknown(nt));
1606 	}
1607 }
1608 
1609 static const char *
1610 note_type_openbsd(unsigned int nt)
1611 {
1612 	switch (nt) {
1613 	case 1: return "NT_OPENBSD_IDENT";
1614 	default: return (note_type_unknown(nt));
1615 	}
1616 }
1617 
1618 static const char *
1619 note_type_unknown(unsigned int nt)
1620 {
1621 	static char s_nt[32];
1622 
1623 	snprintf(s_nt, sizeof(s_nt),
1624 	    nt >= 0x100 ? "<unknown: 0x%x>" : "<unknown: %u>", nt);
1625 	return (s_nt);
1626 }
1627 
1628 static struct {
1629 	const char *name;
1630 	int value;
1631 } l_flag[] = {
1632 	{"EXACT_MATCH", LL_EXACT_MATCH},
1633 	{"IGNORE_INT_VER", LL_IGNORE_INT_VER},
1634 	{"REQUIRE_MINOR", LL_REQUIRE_MINOR},
1635 	{"EXPORTS", LL_EXPORTS},
1636 	{"DELAY_LOAD", LL_DELAY_LOAD},
1637 	{"DELTA", LL_DELTA},
1638 	{NULL, 0}
1639 };
1640 
1641 static struct mips_option mips_exceptions_option[] = {
1642 	{OEX_PAGE0, "PAGE0"},
1643 	{OEX_SMM, "SMM"},
1644 	{OEX_PRECISEFP, "PRECISEFP"},
1645 	{OEX_DISMISS, "DISMISS"},
1646 	{0, NULL}
1647 };
1648 
1649 static struct mips_option mips_pad_option[] = {
1650 	{OPAD_PREFIX, "PREFIX"},
1651 	{OPAD_POSTFIX, "POSTFIX"},
1652 	{OPAD_SYMBOL, "SYMBOL"},
1653 	{0, NULL}
1654 };
1655 
1656 static struct mips_option mips_hwpatch_option[] = {
1657 	{OHW_R4KEOP, "R4KEOP"},
1658 	{OHW_R8KPFETCH, "R8KPFETCH"},
1659 	{OHW_R5KEOP, "R5KEOP"},
1660 	{OHW_R5KCVTL, "R5KCVTL"},
1661 	{0, NULL}
1662 };
1663 
1664 static struct mips_option mips_hwa_option[] = {
1665 	{OHWA0_R4KEOP_CHECKED, "R4KEOP_CHECKED"},
1666 	{OHWA0_R4KEOP_CLEAN, "R4KEOP_CLEAN"},
1667 	{0, NULL}
1668 };
1669 
1670 static struct mips_option mips_hwo_option[] = {
1671 	{OHWO0_FIXADE, "FIXADE"},
1672 	{0, NULL}
1673 };
1674 
1675 static const char *
1676 option_kind(uint8_t kind)
1677 {
1678 	static char s_kind[32];
1679 
1680 	switch (kind) {
1681 	case ODK_NULL: return "NULL";
1682 	case ODK_REGINFO: return "REGINFO";
1683 	case ODK_EXCEPTIONS: return "EXCEPTIONS";
1684 	case ODK_PAD: return "PAD";
1685 	case ODK_HWPATCH: return "HWPATCH";
1686 	case ODK_FILL: return "FILL";
1687 	case ODK_TAGS: return "TAGS";
1688 	case ODK_HWAND: return "HWAND";
1689 	case ODK_HWOR: return "HWOR";
1690 	case ODK_GP_GROUP: return "GP_GROUP";
1691 	case ODK_IDENT: return "IDENT";
1692 	default:
1693 		snprintf(s_kind, sizeof(s_kind), "<unknown: %u>", kind);
1694 		return (s_kind);
1695 	}
1696 }
1697 
1698 static const char *
1699 top_tag(unsigned int tag)
1700 {
1701 	static char s_top_tag[32];
1702 
1703 	switch (tag) {
1704 	case 1: return "File Attributes";
1705 	case 2: return "Section Attributes";
1706 	case 3: return "Symbol Attributes";
1707 	default:
1708 		snprintf(s_top_tag, sizeof(s_top_tag), "Unknown tag: %u", tag);
1709 		return (s_top_tag);
1710 	}
1711 }
1712 
1713 static const char *
1714 aeabi_cpu_arch(uint64_t arch)
1715 {
1716 	static char s_cpu_arch[32];
1717 
1718 	switch (arch) {
1719 	case 0: return "Pre-V4";
1720 	case 1: return "ARM v4";
1721 	case 2: return "ARM v4T";
1722 	case 3: return "ARM v5T";
1723 	case 4: return "ARM v5TE";
1724 	case 5: return "ARM v5TEJ";
1725 	case 6: return "ARM v6";
1726 	case 7: return "ARM v6KZ";
1727 	case 8: return "ARM v6T2";
1728 	case 9: return "ARM v6K";
1729 	case 10: return "ARM v7";
1730 	case 11: return "ARM v6-M";
1731 	case 12: return "ARM v6S-M";
1732 	case 13: return "ARM v7E-M";
1733 	default:
1734 		snprintf(s_cpu_arch, sizeof(s_cpu_arch),
1735 		    "Unknown (%ju)", (uintmax_t) arch);
1736 		return (s_cpu_arch);
1737 	}
1738 }
1739 
1740 static const char *
1741 aeabi_cpu_arch_profile(uint64_t pf)
1742 {
1743 	static char s_arch_profile[32];
1744 
1745 	switch (pf) {
1746 	case 0:
1747 		return "Not applicable";
1748 	case 0x41:		/* 'A' */
1749 		return "Application Profile";
1750 	case 0x52:		/* 'R' */
1751 		return "Real-Time Profile";
1752 	case 0x4D:		/* 'M' */
1753 		return "Microcontroller Profile";
1754 	case 0x53:		/* 'S' */
1755 		return "Application or Real-Time Profile";
1756 	default:
1757 		snprintf(s_arch_profile, sizeof(s_arch_profile),
1758 		    "Unknown (%ju)\n", (uintmax_t) pf);
1759 		return (s_arch_profile);
1760 	}
1761 }
1762 
1763 static const char *
1764 aeabi_arm_isa(uint64_t ai)
1765 {
1766 	static char s_ai[32];
1767 
1768 	switch (ai) {
1769 	case 0: return "No";
1770 	case 1: return "Yes";
1771 	default:
1772 		snprintf(s_ai, sizeof(s_ai), "Unknown (%ju)\n",
1773 		    (uintmax_t) ai);
1774 		return (s_ai);
1775 	}
1776 }
1777 
1778 static const char *
1779 aeabi_thumb_isa(uint64_t ti)
1780 {
1781 	static char s_ti[32];
1782 
1783 	switch (ti) {
1784 	case 0: return "No";
1785 	case 1: return "16-bit Thumb";
1786 	case 2: return "32-bit Thumb";
1787 	default:
1788 		snprintf(s_ti, sizeof(s_ti), "Unknown (%ju)\n",
1789 		    (uintmax_t) ti);
1790 		return (s_ti);
1791 	}
1792 }
1793 
1794 static const char *
1795 aeabi_fp_arch(uint64_t fp)
1796 {
1797 	static char s_fp_arch[32];
1798 
1799 	switch (fp) {
1800 	case 0: return "No";
1801 	case 1: return "VFPv1";
1802 	case 2: return "VFPv2";
1803 	case 3: return "VFPv3";
1804 	case 4: return "VFPv3-D16";
1805 	case 5: return "VFPv4";
1806 	case 6: return "VFPv4-D16";
1807 	default:
1808 		snprintf(s_fp_arch, sizeof(s_fp_arch), "Unknown (%ju)",
1809 		    (uintmax_t) fp);
1810 		return (s_fp_arch);
1811 	}
1812 }
1813 
1814 static const char *
1815 aeabi_wmmx_arch(uint64_t wmmx)
1816 {
1817 	static char s_wmmx[32];
1818 
1819 	switch (wmmx) {
1820 	case 0: return "No";
1821 	case 1: return "WMMXv1";
1822 	case 2: return "WMMXv2";
1823 	default:
1824 		snprintf(s_wmmx, sizeof(s_wmmx), "Unknown (%ju)",
1825 		    (uintmax_t) wmmx);
1826 		return (s_wmmx);
1827 	}
1828 }
1829 
1830 static const char *
1831 aeabi_adv_simd_arch(uint64_t simd)
1832 {
1833 	static char s_simd[32];
1834 
1835 	switch (simd) {
1836 	case 0: return "No";
1837 	case 1: return "NEONv1";
1838 	case 2: return "NEONv2";
1839 	default:
1840 		snprintf(s_simd, sizeof(s_simd), "Unknown (%ju)",
1841 		    (uintmax_t) simd);
1842 		return (s_simd);
1843 	}
1844 }
1845 
1846 static const char *
1847 aeabi_pcs_config(uint64_t pcs)
1848 {
1849 	static char s_pcs[32];
1850 
1851 	switch (pcs) {
1852 	case 0: return "None";
1853 	case 1: return "Bare platform";
1854 	case 2: return "Linux";
1855 	case 3: return "Linux DSO";
1856 	case 4: return "Palm OS 2004";
1857 	case 5: return "Palm OS (future)";
1858 	case 6: return "Symbian OS 2004";
1859 	case 7: return "Symbian OS (future)";
1860 	default:
1861 		snprintf(s_pcs, sizeof(s_pcs), "Unknown (%ju)",
1862 		    (uintmax_t) pcs);
1863 		return (s_pcs);
1864 	}
1865 }
1866 
1867 static const char *
1868 aeabi_pcs_r9(uint64_t r9)
1869 {
1870 	static char s_r9[32];
1871 
1872 	switch (r9) {
1873 	case 0: return "V6";
1874 	case 1: return "SB";
1875 	case 2: return "TLS pointer";
1876 	case 3: return "Unused";
1877 	default:
1878 		snprintf(s_r9, sizeof(s_r9), "Unknown (%ju)", (uintmax_t) r9);
1879 		return (s_r9);
1880 	}
1881 }
1882 
1883 static const char *
1884 aeabi_pcs_rw(uint64_t rw)
1885 {
1886 	static char s_rw[32];
1887 
1888 	switch (rw) {
1889 	case 0: return "Absolute";
1890 	case 1: return "PC-relative";
1891 	case 2: return "SB-relative";
1892 	case 3: return "None";
1893 	default:
1894 		snprintf(s_rw, sizeof(s_rw), "Unknown (%ju)", (uintmax_t) rw);
1895 		return (s_rw);
1896 	}
1897 }
1898 
1899 static const char *
1900 aeabi_pcs_ro(uint64_t ro)
1901 {
1902 	static char s_ro[32];
1903 
1904 	switch (ro) {
1905 	case 0: return "Absolute";
1906 	case 1: return "PC-relative";
1907 	case 2: return "None";
1908 	default:
1909 		snprintf(s_ro, sizeof(s_ro), "Unknown (%ju)", (uintmax_t) ro);
1910 		return (s_ro);
1911 	}
1912 }
1913 
1914 static const char *
1915 aeabi_pcs_got(uint64_t got)
1916 {
1917 	static char s_got[32];
1918 
1919 	switch (got) {
1920 	case 0: return "None";
1921 	case 1: return "direct";
1922 	case 2: return "indirect via GOT";
1923 	default:
1924 		snprintf(s_got, sizeof(s_got), "Unknown (%ju)",
1925 		    (uintmax_t) got);
1926 		return (s_got);
1927 	}
1928 }
1929 
1930 static const char *
1931 aeabi_pcs_wchar_t(uint64_t wt)
1932 {
1933 	static char s_wt[32];
1934 
1935 	switch (wt) {
1936 	case 0: return "None";
1937 	case 2: return "wchar_t size 2";
1938 	case 4: return "wchar_t size 4";
1939 	default:
1940 		snprintf(s_wt, sizeof(s_wt), "Unknown (%ju)", (uintmax_t) wt);
1941 		return (s_wt);
1942 	}
1943 }
1944 
1945 static const char *
1946 aeabi_enum_size(uint64_t es)
1947 {
1948 	static char s_es[32];
1949 
1950 	switch (es) {
1951 	case 0: return "None";
1952 	case 1: return "smallest";
1953 	case 2: return "32-bit";
1954 	case 3: return "visible 32-bit";
1955 	default:
1956 		snprintf(s_es, sizeof(s_es), "Unknown (%ju)", (uintmax_t) es);
1957 		return (s_es);
1958 	}
1959 }
1960 
1961 static const char *
1962 aeabi_align_needed(uint64_t an)
1963 {
1964 	static char s_align_n[64];
1965 
1966 	switch (an) {
1967 	case 0: return "No";
1968 	case 1: return "8-byte align";
1969 	case 2: return "4-byte align";
1970 	case 3: return "Reserved";
1971 	default:
1972 		if (an >= 4 && an <= 12)
1973 			snprintf(s_align_n, sizeof(s_align_n), "8-byte align"
1974 			    " and up to 2^%ju-byte extended align",
1975 			    (uintmax_t) an);
1976 		else
1977 			snprintf(s_align_n, sizeof(s_align_n), "Unknown (%ju)",
1978 			    (uintmax_t) an);
1979 		return (s_align_n);
1980 	}
1981 }
1982 
1983 static const char *
1984 aeabi_align_preserved(uint64_t ap)
1985 {
1986 	static char s_align_p[128];
1987 
1988 	switch (ap) {
1989 	case 0: return "No";
1990 	case 1: return "8-byte align";
1991 	case 2: return "8-byte align and SP % 8 == 0";
1992 	case 3: return "Reserved";
1993 	default:
1994 		if (ap >= 4 && ap <= 12)
1995 			snprintf(s_align_p, sizeof(s_align_p), "8-byte align"
1996 			    " and SP %% 8 == 0 and up to 2^%ju-byte extended"
1997 			    " align", (uintmax_t) ap);
1998 		else
1999 			snprintf(s_align_p, sizeof(s_align_p), "Unknown (%ju)",
2000 			    (uintmax_t) ap);
2001 		return (s_align_p);
2002 	}
2003 }
2004 
2005 static const char *
2006 aeabi_fp_rounding(uint64_t fr)
2007 {
2008 	static char s_fp_r[32];
2009 
2010 	switch (fr) {
2011 	case 0: return "Unused";
2012 	case 1: return "Needed";
2013 	default:
2014 		snprintf(s_fp_r, sizeof(s_fp_r), "Unknown (%ju)",
2015 		    (uintmax_t) fr);
2016 		return (s_fp_r);
2017 	}
2018 }
2019 
2020 static const char *
2021 aeabi_fp_denormal(uint64_t fd)
2022 {
2023 	static char s_fp_d[32];
2024 
2025 	switch (fd) {
2026 	case 0: return "Unused";
2027 	case 1: return "Needed";
2028 	case 2: return "Sign Only";
2029 	default:
2030 		snprintf(s_fp_d, sizeof(s_fp_d), "Unknown (%ju)",
2031 		    (uintmax_t) fd);
2032 		return (s_fp_d);
2033 	}
2034 }
2035 
2036 static const char *
2037 aeabi_fp_exceptions(uint64_t fe)
2038 {
2039 	static char s_fp_e[32];
2040 
2041 	switch (fe) {
2042 	case 0: return "Unused";
2043 	case 1: return "Needed";
2044 	default:
2045 		snprintf(s_fp_e, sizeof(s_fp_e), "Unknown (%ju)",
2046 		    (uintmax_t) fe);
2047 		return (s_fp_e);
2048 	}
2049 }
2050 
2051 static const char *
2052 aeabi_fp_user_exceptions(uint64_t fu)
2053 {
2054 	static char s_fp_u[32];
2055 
2056 	switch (fu) {
2057 	case 0: return "Unused";
2058 	case 1: return "Needed";
2059 	default:
2060 		snprintf(s_fp_u, sizeof(s_fp_u), "Unknown (%ju)",
2061 		    (uintmax_t) fu);
2062 		return (s_fp_u);
2063 	}
2064 }
2065 
2066 static const char *
2067 aeabi_fp_number_model(uint64_t fn)
2068 {
2069 	static char s_fp_n[32];
2070 
2071 	switch (fn) {
2072 	case 0: return "Unused";
2073 	case 1: return "IEEE 754 normal";
2074 	case 2: return "RTABI";
2075 	case 3: return "IEEE 754";
2076 	default:
2077 		snprintf(s_fp_n, sizeof(s_fp_n), "Unknown (%ju)",
2078 		    (uintmax_t) fn);
2079 		return (s_fp_n);
2080 	}
2081 }
2082 
2083 static const char *
2084 aeabi_fp_16bit_format(uint64_t fp16)
2085 {
2086 	static char s_fp_16[64];
2087 
2088 	switch (fp16) {
2089 	case 0: return "None";
2090 	case 1: return "IEEE 754";
2091 	case 2: return "VFPv3/Advanced SIMD (alternative format)";
2092 	default:
2093 		snprintf(s_fp_16, sizeof(s_fp_16), "Unknown (%ju)",
2094 		    (uintmax_t) fp16);
2095 		return (s_fp_16);
2096 	}
2097 }
2098 
2099 static const char *
2100 aeabi_mpext(uint64_t mp)
2101 {
2102 	static char s_mp[32];
2103 
2104 	switch (mp) {
2105 	case 0: return "Not allowed";
2106 	case 1: return "Allowed";
2107 	default:
2108 		snprintf(s_mp, sizeof(s_mp), "Unknown (%ju)",
2109 		    (uintmax_t) mp);
2110 		return (s_mp);
2111 	}
2112 }
2113 
2114 static const char *
2115 aeabi_div(uint64_t du)
2116 {
2117 	static char s_du[32];
2118 
2119 	switch (du) {
2120 	case 0: return "Yes (V7-R/V7-M)";
2121 	case 1: return "No";
2122 	case 2: return "Yes (V7-A)";
2123 	default:
2124 		snprintf(s_du, sizeof(s_du), "Unknown (%ju)",
2125 		    (uintmax_t) du);
2126 		return (s_du);
2127 	}
2128 }
2129 
2130 static const char *
2131 aeabi_t2ee(uint64_t t2ee)
2132 {
2133 	static char s_t2ee[32];
2134 
2135 	switch (t2ee) {
2136 	case 0: return "Not allowed";
2137 	case 1: return "Allowed";
2138 	default:
2139 		snprintf(s_t2ee, sizeof(s_t2ee), "Unknown(%ju)",
2140 		    (uintmax_t) t2ee);
2141 		return (s_t2ee);
2142 	}
2143 
2144 }
2145 
2146 static const char *
2147 aeabi_hardfp(uint64_t hfp)
2148 {
2149 	static char s_hfp[32];
2150 
2151 	switch (hfp) {
2152 	case 0: return "Tag_FP_arch";
2153 	case 1: return "only SP";
2154 	case 2: return "only DP";
2155 	case 3: return "both SP and DP";
2156 	default:
2157 		snprintf(s_hfp, sizeof(s_hfp), "Unknown (%ju)",
2158 		    (uintmax_t) hfp);
2159 		return (s_hfp);
2160 	}
2161 }
2162 
2163 static const char *
2164 aeabi_vfp_args(uint64_t va)
2165 {
2166 	static char s_va[32];
2167 
2168 	switch (va) {
2169 	case 0: return "AAPCS (base variant)";
2170 	case 1: return "AAPCS (VFP variant)";
2171 	case 2: return "toolchain-specific";
2172 	default:
2173 		snprintf(s_va, sizeof(s_va), "Unknown (%ju)", (uintmax_t) va);
2174 		return (s_va);
2175 	}
2176 }
2177 
2178 static const char *
2179 aeabi_wmmx_args(uint64_t wa)
2180 {
2181 	static char s_wa[32];
2182 
2183 	switch (wa) {
2184 	case 0: return "AAPCS (base variant)";
2185 	case 1: return "Intel WMMX";
2186 	case 2: return "toolchain-specific";
2187 	default:
2188 		snprintf(s_wa, sizeof(s_wa), "Unknown(%ju)", (uintmax_t) wa);
2189 		return (s_wa);
2190 	}
2191 }
2192 
2193 static const char *
2194 aeabi_unaligned_access(uint64_t ua)
2195 {
2196 	static char s_ua[32];
2197 
2198 	switch (ua) {
2199 	case 0: return "Not allowed";
2200 	case 1: return "Allowed";
2201 	default:
2202 		snprintf(s_ua, sizeof(s_ua), "Unknown(%ju)", (uintmax_t) ua);
2203 		return (s_ua);
2204 	}
2205 }
2206 
2207 static const char *
2208 aeabi_fp_hpext(uint64_t fh)
2209 {
2210 	static char s_fh[32];
2211 
2212 	switch (fh) {
2213 	case 0: return "Not allowed";
2214 	case 1: return "Allowed";
2215 	default:
2216 		snprintf(s_fh, sizeof(s_fh), "Unknown(%ju)", (uintmax_t) fh);
2217 		return (s_fh);
2218 	}
2219 }
2220 
2221 static const char *
2222 aeabi_optm_goal(uint64_t og)
2223 {
2224 	static char s_og[32];
2225 
2226 	switch (og) {
2227 	case 0: return "None";
2228 	case 1: return "Speed";
2229 	case 2: return "Speed aggressive";
2230 	case 3: return "Space";
2231 	case 4: return "Space aggressive";
2232 	case 5: return "Debugging";
2233 	case 6: return "Best Debugging";
2234 	default:
2235 		snprintf(s_og, sizeof(s_og), "Unknown(%ju)", (uintmax_t) og);
2236 		return (s_og);
2237 	}
2238 }
2239 
2240 static const char *
2241 aeabi_fp_optm_goal(uint64_t fog)
2242 {
2243 	static char s_fog[32];
2244 
2245 	switch (fog) {
2246 	case 0: return "None";
2247 	case 1: return "Speed";
2248 	case 2: return "Speed aggressive";
2249 	case 3: return "Space";
2250 	case 4: return "Space aggressive";
2251 	case 5: return "Accurary";
2252 	case 6: return "Best Accurary";
2253 	default:
2254 		snprintf(s_fog, sizeof(s_fog), "Unknown(%ju)",
2255 		    (uintmax_t) fog);
2256 		return (s_fog);
2257 	}
2258 }
2259 
2260 static const char *
2261 aeabi_virtual(uint64_t vt)
2262 {
2263 	static char s_virtual[64];
2264 
2265 	switch (vt) {
2266 	case 0: return "No";
2267 	case 1: return "TrustZone";
2268 	case 2: return "Virtualization extension";
2269 	case 3: return "TrustZone and virtualization extension";
2270 	default:
2271 		snprintf(s_virtual, sizeof(s_virtual), "Unknown(%ju)",
2272 		    (uintmax_t) vt);
2273 		return (s_virtual);
2274 	}
2275 }
2276 
2277 static struct {
2278 	uint64_t tag;
2279 	const char *s_tag;
2280 	const char *(*get_desc)(uint64_t val);
2281 } aeabi_tags[] = {
2282 	{4, "Tag_CPU_raw_name", NULL},
2283 	{5, "Tag_CPU_name", NULL},
2284 	{6, "Tag_CPU_arch", aeabi_cpu_arch},
2285 	{7, "Tag_CPU_arch_profile", aeabi_cpu_arch_profile},
2286 	{8, "Tag_ARM_ISA_use", aeabi_arm_isa},
2287 	{9, "Tag_THUMB_ISA_use", aeabi_thumb_isa},
2288 	{10, "Tag_FP_arch", aeabi_fp_arch},
2289 	{11, "Tag_WMMX_arch", aeabi_wmmx_arch},
2290 	{12, "Tag_Advanced_SIMD_arch", aeabi_adv_simd_arch},
2291 	{13, "Tag_PCS_config", aeabi_pcs_config},
2292 	{14, "Tag_ABI_PCS_R9_use", aeabi_pcs_r9},
2293 	{15, "Tag_ABI_PCS_RW_data", aeabi_pcs_rw},
2294 	{16, "Tag_ABI_PCS_RO_data", aeabi_pcs_ro},
2295 	{17, "Tag_ABI_PCS_GOT_use", aeabi_pcs_got},
2296 	{18, "Tag_ABI_PCS_wchar_t", aeabi_pcs_wchar_t},
2297 	{19, "Tag_ABI_FP_rounding", aeabi_fp_rounding},
2298 	{20, "Tag_ABI_FP_denormal", aeabi_fp_denormal},
2299 	{21, "Tag_ABI_FP_exceptions", aeabi_fp_exceptions},
2300 	{22, "Tag_ABI_FP_user_exceptions", aeabi_fp_user_exceptions},
2301 	{23, "Tag_ABI_FP_number_model", aeabi_fp_number_model},
2302 	{24, "Tag_ABI_align_needed", aeabi_align_needed},
2303 	{25, "Tag_ABI_align_preserved", aeabi_align_preserved},
2304 	{26, "Tag_ABI_enum_size", aeabi_enum_size},
2305 	{27, "Tag_ABI_HardFP_use", aeabi_hardfp},
2306 	{28, "Tag_ABI_VFP_args", aeabi_vfp_args},
2307 	{29, "Tag_ABI_WMMX_args", aeabi_wmmx_args},
2308 	{30, "Tag_ABI_optimization_goals", aeabi_optm_goal},
2309 	{31, "Tag_ABI_FP_optimization_goals", aeabi_fp_optm_goal},
2310 	{32, "Tag_compatibility", NULL},
2311 	{34, "Tag_CPU_unaligned_access", aeabi_unaligned_access},
2312 	{36, "Tag_FP_HP_extension", aeabi_fp_hpext},
2313 	{38, "Tag_ABI_FP_16bit_format", aeabi_fp_16bit_format},
2314 	{42, "Tag_MPextension_use", aeabi_mpext},
2315 	{44, "Tag_DIV_use", aeabi_div},
2316 	{64, "Tag_nodefaults", NULL},
2317 	{65, "Tag_also_compatible_with", NULL},
2318 	{66, "Tag_T2EE_use", aeabi_t2ee},
2319 	{67, "Tag_conformance", NULL},
2320 	{68, "Tag_Virtualization_use", aeabi_virtual},
2321 	{70, "Tag_MPextension_use", aeabi_mpext},
2322 };
2323 
2324 static const char *
2325 mips_abi_fp(uint64_t fp)
2326 {
2327 	static char s_mips_abi_fp[64];
2328 
2329 	switch (fp) {
2330 	case 0: return "N/A";
2331 	case 1: return "Hard float (double precision)";
2332 	case 2: return "Hard float (single precision)";
2333 	case 3: return "Soft float";
2334 	case 4: return "64-bit float (-mips32r2 -mfp64)";
2335 	default:
2336 		snprintf(s_mips_abi_fp, sizeof(s_mips_abi_fp), "Unknown(%ju)",
2337 		    (uintmax_t) fp);
2338 		return (s_mips_abi_fp);
2339 	}
2340 }
2341 
2342 static const char *
2343 ppc_abi_fp(uint64_t fp)
2344 {
2345 	static char s_ppc_abi_fp[64];
2346 
2347 	switch (fp) {
2348 	case 0: return "N/A";
2349 	case 1: return "Hard float (double precision)";
2350 	case 2: return "Soft float";
2351 	case 3: return "Hard float (single precision)";
2352 	default:
2353 		snprintf(s_ppc_abi_fp, sizeof(s_ppc_abi_fp), "Unknown(%ju)",
2354 		    (uintmax_t) fp);
2355 		return (s_ppc_abi_fp);
2356 	}
2357 }
2358 
2359 static const char *
2360 ppc_abi_vector(uint64_t vec)
2361 {
2362 	static char s_vec[64];
2363 
2364 	switch (vec) {
2365 	case 0: return "N/A";
2366 	case 1: return "Generic purpose registers";
2367 	case 2: return "AltiVec registers";
2368 	case 3: return "SPE registers";
2369 	default:
2370 		snprintf(s_vec, sizeof(s_vec), "Unknown(%ju)", (uintmax_t) vec);
2371 		return (s_vec);
2372 	}
2373 }
2374 
2375 static const char *
2376 dwarf_reg(unsigned int mach, unsigned int reg)
2377 {
2378 
2379 	switch (mach) {
2380 	case EM_386:
2381 		switch (reg) {
2382 		case 0: return "eax";
2383 		case 1: return "ecx";
2384 		case 2: return "edx";
2385 		case 3: return "ebx";
2386 		case 4: return "esp";
2387 		case 5: return "ebp";
2388 		case 6: return "esi";
2389 		case 7: return "edi";
2390 		case 8: return "eip";
2391 		case 9: return "eflags";
2392 		case 11: return "st0";
2393 		case 12: return "st1";
2394 		case 13: return "st2";
2395 		case 14: return "st3";
2396 		case 15: return "st4";
2397 		case 16: return "st5";
2398 		case 17: return "st6";
2399 		case 18: return "st7";
2400 		case 21: return "xmm0";
2401 		case 22: return "xmm1";
2402 		case 23: return "xmm2";
2403 		case 24: return "xmm3";
2404 		case 25: return "xmm4";
2405 		case 26: return "xmm5";
2406 		case 27: return "xmm6";
2407 		case 28: return "xmm7";
2408 		case 29: return "mm0";
2409 		case 30: return "mm1";
2410 		case 31: return "mm2";
2411 		case 32: return "mm3";
2412 		case 33: return "mm4";
2413 		case 34: return "mm5";
2414 		case 35: return "mm6";
2415 		case 36: return "mm7";
2416 		case 37: return "fcw";
2417 		case 38: return "fsw";
2418 		case 39: return "mxcsr";
2419 		case 40: return "es";
2420 		case 41: return "cs";
2421 		case 42: return "ss";
2422 		case 43: return "ds";
2423 		case 44: return "fs";
2424 		case 45: return "gs";
2425 		case 48: return "tr";
2426 		case 49: return "ldtr";
2427 		default: return (NULL);
2428 		}
2429 	case EM_X86_64:
2430 		switch (reg) {
2431 		case 0: return "rax";
2432 		case 1: return "rdx";
2433 		case 2: return "rcx";
2434 		case 3: return "rbx";
2435 		case 4: return "rsi";
2436 		case 5: return "rdi";
2437 		case 6: return "rbp";
2438 		case 7: return "rsp";
2439 		case 16: return "rip";
2440 		case 17: return "xmm0";
2441 		case 18: return "xmm1";
2442 		case 19: return "xmm2";
2443 		case 20: return "xmm3";
2444 		case 21: return "xmm4";
2445 		case 22: return "xmm5";
2446 		case 23: return "xmm6";
2447 		case 24: return "xmm7";
2448 		case 25: return "xmm8";
2449 		case 26: return "xmm9";
2450 		case 27: return "xmm10";
2451 		case 28: return "xmm11";
2452 		case 29: return "xmm12";
2453 		case 30: return "xmm13";
2454 		case 31: return "xmm14";
2455 		case 32: return "xmm15";
2456 		case 33: return "st0";
2457 		case 34: return "st1";
2458 		case 35: return "st2";
2459 		case 36: return "st3";
2460 		case 37: return "st4";
2461 		case 38: return "st5";
2462 		case 39: return "st6";
2463 		case 40: return "st7";
2464 		case 41: return "mm0";
2465 		case 42: return "mm1";
2466 		case 43: return "mm2";
2467 		case 44: return "mm3";
2468 		case 45: return "mm4";
2469 		case 46: return "mm5";
2470 		case 47: return "mm6";
2471 		case 48: return "mm7";
2472 		case 49: return "rflags";
2473 		case 50: return "es";
2474 		case 51: return "cs";
2475 		case 52: return "ss";
2476 		case 53: return "ds";
2477 		case 54: return "fs";
2478 		case 55: return "gs";
2479 		case 58: return "fs.base";
2480 		case 59: return "gs.base";
2481 		case 62: return "tr";
2482 		case 63: return "ldtr";
2483 		case 64: return "mxcsr";
2484 		case 65: return "fcw";
2485 		case 66: return "fsw";
2486 		default: return (NULL);
2487 		}
2488 	default:
2489 		return (NULL);
2490 	}
2491 }
2492 
2493 static void
2494 dump_ehdr(struct readelf *re)
2495 {
2496 	size_t		 shnum, shstrndx;
2497 	int		 i;
2498 
2499 	printf("ELF Header:\n");
2500 
2501 	/* e_ident[]. */
2502 	printf("  Magic:   ");
2503 	for (i = 0; i < EI_NIDENT; i++)
2504 		printf("%.2x ", re->ehdr.e_ident[i]);
2505 	putchar('\n');
2506 
2507 	/* EI_CLASS. */
2508 	printf("%-37s%s\n", "  Class:", elf_class(re->ehdr.e_ident[EI_CLASS]));
2509 
2510 	/* EI_DATA. */
2511 	printf("%-37s%s\n", "  Data:", elf_endian(re->ehdr.e_ident[EI_DATA]));
2512 
2513 	/* EI_VERSION. */
2514 	printf("%-37s%d %s\n", "  Version:", re->ehdr.e_ident[EI_VERSION],
2515 	    elf_ver(re->ehdr.e_ident[EI_VERSION]));
2516 
2517 	/* EI_OSABI. */
2518 	printf("%-37s%s\n", "  OS/ABI:", elf_osabi(re->ehdr.e_ident[EI_OSABI]));
2519 
2520 	/* EI_ABIVERSION. */
2521 	printf("%-37s%d\n", "  ABI Version:", re->ehdr.e_ident[EI_ABIVERSION]);
2522 
2523 	/* e_type. */
2524 	printf("%-37s%s\n", "  Type:", elf_type(re->ehdr.e_type));
2525 
2526 	/* e_machine. */
2527 	printf("%-37s%s\n", "  Machine:", elf_machine(re->ehdr.e_machine));
2528 
2529 	/* e_version. */
2530 	printf("%-37s%#x\n", "  Version:", re->ehdr.e_version);
2531 
2532 	/* e_entry. */
2533 	printf("%-37s%#jx\n", "  Entry point address:",
2534 	    (uintmax_t)re->ehdr.e_entry);
2535 
2536 	/* e_phoff. */
2537 	printf("%-37s%ju (bytes into file)\n", "  Start of program headers:",
2538 	    (uintmax_t)re->ehdr.e_phoff);
2539 
2540 	/* e_shoff. */
2541 	printf("%-37s%ju (bytes into file)\n", "  Start of section headers:",
2542 	    (uintmax_t)re->ehdr.e_shoff);
2543 
2544 	/* e_flags. */
2545 	printf("%-37s%#x", "  Flags:", re->ehdr.e_flags);
2546 	dump_eflags(re, re->ehdr.e_flags);
2547 	putchar('\n');
2548 
2549 	/* e_ehsize. */
2550 	printf("%-37s%u (bytes)\n", "  Size of this header:",
2551 	    re->ehdr.e_ehsize);
2552 
2553 	/* e_phentsize. */
2554 	printf("%-37s%u (bytes)\n", "  Size of program headers:",
2555 	    re->ehdr.e_phentsize);
2556 
2557 	/* e_phnum. */
2558 	printf("%-37s%u\n", "  Number of program headers:", re->ehdr.e_phnum);
2559 
2560 	/* e_shentsize. */
2561 	printf("%-37s%u (bytes)\n", "  Size of section headers:",
2562 	    re->ehdr.e_shentsize);
2563 
2564 	/* e_shnum. */
2565 	printf("%-37s%u", "  Number of section headers:", re->ehdr.e_shnum);
2566 	if (re->ehdr.e_shnum == SHN_UNDEF) {
2567 		/* Extended section numbering is in use. */
2568 		if (elf_getshnum(re->elf, &shnum))
2569 			printf(" (%ju)", (uintmax_t)shnum);
2570 	}
2571 	putchar('\n');
2572 
2573 	/* e_shstrndx. */
2574 	printf("%-37s%u", "  Section header string table index:",
2575 	    re->ehdr.e_shstrndx);
2576 	if (re->ehdr.e_shstrndx == SHN_XINDEX) {
2577 		/* Extended section numbering is in use. */
2578 		if (elf_getshstrndx(re->elf, &shstrndx))
2579 			printf(" (%ju)", (uintmax_t)shstrndx);
2580 	}
2581 	putchar('\n');
2582 }
2583 
2584 static void
2585 dump_eflags(struct readelf *re, uint64_t e_flags)
2586 {
2587 	struct eflags_desc *edesc;
2588 	int arm_eabi;
2589 
2590 	edesc = NULL;
2591 	switch (re->ehdr.e_machine) {
2592 	case EM_ARM:
2593 		arm_eabi = (e_flags & EF_ARM_EABIMASK) >> 24;
2594 		if (arm_eabi == 0)
2595 			printf(", GNU EABI");
2596 		else if (arm_eabi <= 5)
2597 			printf(", Version%d EABI", arm_eabi);
2598 		edesc = arm_eflags_desc;
2599 		break;
2600 	case EM_MIPS:
2601 	case EM_MIPS_RS3_LE:
2602 		switch ((e_flags & EF_MIPS_ARCH) >> 28) {
2603 		case 0:	printf(", mips1"); break;
2604 		case 1: printf(", mips2"); break;
2605 		case 2: printf(", mips3"); break;
2606 		case 3: printf(", mips4"); break;
2607 		case 4: printf(", mips5"); break;
2608 		case 5: printf(", mips32"); break;
2609 		case 6: printf(", mips64"); break;
2610 		case 7: printf(", mips32r2"); break;
2611 		case 8: printf(", mips64r2"); break;
2612 		default: break;
2613 		}
2614 		switch ((e_flags & 0x00FF0000) >> 16) {
2615 		case 0x81: printf(", 3900"); break;
2616 		case 0x82: printf(", 4010"); break;
2617 		case 0x83: printf(", 4100"); break;
2618 		case 0x85: printf(", 4650"); break;
2619 		case 0x87: printf(", 4120"); break;
2620 		case 0x88: printf(", 4111"); break;
2621 		case 0x8a: printf(", sb1"); break;
2622 		case 0x8b: printf(", octeon"); break;
2623 		case 0x8c: printf(", xlr"); break;
2624 		case 0x91: printf(", 5400"); break;
2625 		case 0x98: printf(", 5500"); break;
2626 		case 0x99: printf(", 9000"); break;
2627 		case 0xa0: printf(", loongson-2e"); break;
2628 		case 0xa1: printf(", loongson-2f"); break;
2629 		default: break;
2630 		}
2631 		switch ((e_flags & 0x0000F000) >> 12) {
2632 		case 1: printf(", o32"); break;
2633 		case 2: printf(", o64"); break;
2634 		case 3: printf(", eabi32"); break;
2635 		case 4: printf(", eabi64"); break;
2636 		default: break;
2637 		}
2638 		edesc = mips_eflags_desc;
2639 		break;
2640 	case EM_PPC:
2641 	case EM_PPC64:
2642 		edesc = powerpc_eflags_desc;
2643 		break;
2644 	case EM_SPARC:
2645 	case EM_SPARC32PLUS:
2646 	case EM_SPARCV9:
2647 		switch ((e_flags & EF_SPARCV9_MM)) {
2648 		case EF_SPARCV9_TSO: printf(", tso"); break;
2649 		case EF_SPARCV9_PSO: printf(", pso"); break;
2650 		case EF_SPARCV9_MM: printf(", rmo"); break;
2651 		default: break;
2652 		}
2653 		edesc = sparc_eflags_desc;
2654 		break;
2655 	default:
2656 		break;
2657 	}
2658 
2659 	if (edesc != NULL) {
2660 		while (edesc->desc != NULL) {
2661 			if (e_flags & edesc->flag)
2662 				printf(", %s", edesc->desc);
2663 			edesc++;
2664 		}
2665 	}
2666 }
2667 
2668 static void
2669 dump_phdr(struct readelf *re)
2670 {
2671 	const char	*rawfile;
2672 	GElf_Phdr	 phdr;
2673 	size_t		 phnum;
2674 	int		 i, j;
2675 
2676 #define	PH_HDR	"Type", "Offset", "VirtAddr", "PhysAddr", "FileSiz",	\
2677 		"MemSiz", "Flg", "Align"
2678 #define	PH_CT	phdr_type(phdr.p_type), (uintmax_t)phdr.p_offset,	\
2679 		(uintmax_t)phdr.p_vaddr, (uintmax_t)phdr.p_paddr,	\
2680 		(uintmax_t)phdr.p_filesz, (uintmax_t)phdr.p_memsz,	\
2681 		phdr.p_flags & PF_R ? 'R' : ' ',			\
2682 		phdr.p_flags & PF_W ? 'W' : ' ',			\
2683 		phdr.p_flags & PF_X ? 'E' : ' ',			\
2684 		(uintmax_t)phdr.p_align
2685 
2686 	if (elf_getphnum(re->elf, &phnum) == 0) {
2687 		warnx("elf_getphnum failed: %s", elf_errmsg(-1));
2688 		return;
2689 	}
2690 	if (phnum == 0) {
2691 		printf("\nThere are no program headers in this file.\n");
2692 		return;
2693 	}
2694 
2695 	printf("\nElf file type is %s", elf_type(re->ehdr.e_type));
2696 	printf("\nEntry point 0x%jx\n", (uintmax_t)re->ehdr.e_entry);
2697 	printf("There are %ju program headers, starting at offset %ju\n",
2698 	    (uintmax_t)phnum, (uintmax_t)re->ehdr.e_phoff);
2699 
2700 	/* Dump program headers. */
2701 	printf("\nProgram Headers:\n");
2702 	if (re->ec == ELFCLASS32)
2703 		printf("  %-15s%-9s%-11s%-11s%-8s%-8s%-4s%s\n", PH_HDR);
2704 	else if (re->options & RE_WW)
2705 		printf("  %-15s%-9s%-19s%-19s%-9s%-9s%-4s%s\n", PH_HDR);
2706 	else
2707 		printf("  %-15s%-19s%-19s%s\n                 %-19s%-20s"
2708 		    "%-7s%s\n", PH_HDR);
2709 	for (i = 0; (size_t) i < phnum; i++) {
2710 		if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
2711 			warnx("gelf_getphdr failed: %s", elf_errmsg(-1));
2712 			continue;
2713 		}
2714 		/* TODO: Add arch-specific segment type dump. */
2715 		if (re->ec == ELFCLASS32)
2716 			printf("  %-14.14s 0x%6.6jx 0x%8.8jx 0x%8.8jx "
2717 			    "0x%5.5jx 0x%5.5jx %c%c%c %#jx\n", PH_CT);
2718 		else if (re->options & RE_WW)
2719 			printf("  %-14.14s 0x%6.6jx 0x%16.16jx 0x%16.16jx "
2720 			    "0x%6.6jx 0x%6.6jx %c%c%c %#jx\n", PH_CT);
2721 		else
2722 			printf("  %-14.14s 0x%16.16jx 0x%16.16jx 0x%16.16jx\n"
2723 			    "                 0x%16.16jx 0x%16.16jx  %c%c%c"
2724 			    "    %#jx\n", PH_CT);
2725 		if (phdr.p_type == PT_INTERP) {
2726 			if ((rawfile = elf_rawfile(re->elf, NULL)) == NULL) {
2727 				warnx("elf_rawfile failed: %s", elf_errmsg(-1));
2728 				continue;
2729 			}
2730 			printf("      [Requesting program interpreter: %s]\n",
2731 				rawfile + phdr.p_offset);
2732 		}
2733 	}
2734 
2735 	/* Dump section to segment mapping. */
2736 	if (re->shnum == 0)
2737 		return;
2738 	printf("\n Section to Segment mapping:\n");
2739 	printf("  Segment Sections...\n");
2740 	for (i = 0; (size_t)i < phnum; i++) {
2741 		if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
2742 			warnx("gelf_getphdr failed: %s", elf_errmsg(-1));
2743 			continue;
2744 		}
2745 		printf("   %2.2d     ", i);
2746 		/* skip NULL section. */
2747 		for (j = 1; (size_t)j < re->shnum; j++)
2748 			if (re->sl[j].off >= phdr.p_offset &&
2749 			    re->sl[j].off + re->sl[j].sz <=
2750 			    phdr.p_offset + phdr.p_memsz)
2751 				printf("%s ", re->sl[j].name);
2752 		printf("\n");
2753 	}
2754 #undef	PH_HDR
2755 #undef	PH_CT
2756 }
2757 
2758 static char *
2759 section_flags(struct readelf *re, struct section *s)
2760 {
2761 #define BUF_SZ 256
2762 	static char	buf[BUF_SZ];
2763 	int		i, p, nb;
2764 
2765 	p = 0;
2766 	nb = re->ec == ELFCLASS32 ? 8 : 16;
2767 	if (re->options & RE_T) {
2768 		snprintf(buf, BUF_SZ, "[%*.*jx]: ", nb, nb,
2769 		    (uintmax_t)s->flags);
2770 		p += nb + 4;
2771 	}
2772 	for (i = 0; section_flag[i].ln != NULL; i++) {
2773 		if ((s->flags & section_flag[i].value) == 0)
2774 			continue;
2775 		if (re->options & RE_T) {
2776 			snprintf(&buf[p], BUF_SZ - p, "%s, ",
2777 			    section_flag[i].ln);
2778 			p += strlen(section_flag[i].ln) + 2;
2779 		} else
2780 			buf[p++] = section_flag[i].sn;
2781 	}
2782 	if (re->options & RE_T && p > nb + 4)
2783 		p -= 2;
2784 	buf[p] = '\0';
2785 
2786 	return (buf);
2787 }
2788 
2789 static void
2790 dump_shdr(struct readelf *re)
2791 {
2792 	struct section	*s;
2793 	int		 i;
2794 
2795 #define	S_HDR	"[Nr] Name", "Type", "Addr", "Off", "Size", "ES",	\
2796 		"Flg", "Lk", "Inf", "Al"
2797 #define	S_HDRL	"[Nr] Name", "Type", "Address", "Offset", "Size",	\
2798 		"EntSize", "Flags", "Link", "Info", "Align"
2799 #define	ST_HDR	"[Nr] Name", "Type", "Addr", "Off", "Size", "ES",	\
2800 		"Lk", "Inf", "Al", "Flags"
2801 #define	ST_HDRL	"[Nr] Name", "Type", "Address", "Offset", "Link",	\
2802 		"Size", "EntSize", "Info", "Align", "Flags"
2803 #define	S_CT	i, s->name, section_type(re->ehdr.e_machine, s->type),	\
2804 		(uintmax_t)s->addr, (uintmax_t)s->off, (uintmax_t)s->sz,\
2805 		(uintmax_t)s->entsize, section_flags(re, s),		\
2806 		s->link, s->info, (uintmax_t)s->align
2807 #define	ST_CT	i, s->name, section_type(re->ehdr.e_machine, s->type),  \
2808 		(uintmax_t)s->addr, (uintmax_t)s->off, (uintmax_t)s->sz,\
2809 		(uintmax_t)s->entsize, s->link, s->info,		\
2810 		(uintmax_t)s->align, section_flags(re, s)
2811 #define	ST_CTL	i, s->name, section_type(re->ehdr.e_machine, s->type),  \
2812 		(uintmax_t)s->addr, (uintmax_t)s->off, s->link,		\
2813 		(uintmax_t)s->sz, (uintmax_t)s->entsize, s->info,	\
2814 		(uintmax_t)s->align, section_flags(re, s)
2815 
2816 	if (re->shnum == 0) {
2817 		printf("\nThere are no sections in this file.\n");
2818 		return;
2819 	}
2820 	printf("There are %ju section headers, starting at offset 0x%jx:\n",
2821 	    (uintmax_t)re->shnum, (uintmax_t)re->ehdr.e_shoff);
2822 	printf("\nSection Headers:\n");
2823 	if (re->ec == ELFCLASS32) {
2824 		if (re->options & RE_T)
2825 			printf("  %s\n       %-16s%-9s%-7s%-7s%-5s%-3s%-4s%s\n"
2826 			    "%12s\n", ST_HDR);
2827 		else
2828 			printf("  %-23s%-16s%-9s%-7s%-7s%-3s%-4s%-3s%-4s%s\n",
2829 			    S_HDR);
2830 	} else if (re->options & RE_WW) {
2831 		if (re->options & RE_T)
2832 			printf("  %s\n       %-16s%-17s%-7s%-7s%-5s%-3s%-4s%s\n"
2833 			    "%12s\n", ST_HDR);
2834 		else
2835 			printf("  %-23s%-16s%-17s%-7s%-7s%-3s%-4s%-3s%-4s%s\n",
2836 			    S_HDR);
2837 	} else {
2838 		if (re->options & RE_T)
2839 			printf("  %s\n       %-18s%-17s%-18s%s\n       %-18s"
2840 			    "%-17s%-18s%s\n%12s\n", ST_HDRL);
2841 		else
2842 			printf("  %-23s%-17s%-18s%s\n       %-18s%-17s%-7s%"
2843 			    "-6s%-6s%s\n", S_HDRL);
2844 	}
2845 	for (i = 0; (size_t)i < re->shnum; i++) {
2846 		s = &re->sl[i];
2847 		if (re->ec == ELFCLASS32) {
2848 			if (re->options & RE_T)
2849 				printf("  [%2d] %s\n       %-15.15s %8.8jx"
2850 				    " %6.6jx %6.6jx %2.2jx  %2u %3u %2ju\n"
2851 				    "       %s\n", ST_CT);
2852 			else
2853 				printf("  [%2d] %-17.17s %-15.15s %8.8jx"
2854 				    " %6.6jx %6.6jx %2.2jx %3s %2u %3u %2ju\n",
2855 				    S_CT);
2856 		} else if (re->options & RE_WW) {
2857 			if (re->options & RE_T)
2858 				printf("  [%2d] %s\n       %-15.15s %16.16jx"
2859 				    " %6.6jx %6.6jx %2.2jx  %2u %3u %2ju\n"
2860 				    "       %s\n", ST_CT);
2861 			else
2862 				printf("  [%2d] %-17.17s %-15.15s %16.16jx"
2863 				    " %6.6jx %6.6jx %2.2jx %3s %2u %3u %2ju\n",
2864 				    S_CT);
2865 		} else {
2866 			if (re->options & RE_T)
2867 				printf("  [%2d] %s\n       %-15.15s  %16.16jx"
2868 				    "  %16.16jx  %u\n       %16.16jx %16.16jx"
2869 				    "  %-16u  %ju\n       %s\n", ST_CTL);
2870 			else
2871 				printf("  [%2d] %-17.17s %-15.15s  %16.16jx"
2872 				    "  %8.8jx\n       %16.16jx  %16.16jx "
2873 				    "%3s      %2u   %3u     %ju\n", S_CT);
2874 		}
2875 	}
2876 	if ((re->options & RE_T) == 0)
2877 		printf("Key to Flags:\n  W (write), A (alloc),"
2878 		    " X (execute), M (merge), S (strings)\n"
2879 		    "  I (info), L (link order), G (group), x (unknown)\n"
2880 		    "  O (extra OS processing required)"
2881 		    " o (OS specific), p (processor specific)\n");
2882 
2883 #undef	S_HDR
2884 #undef	S_HDRL
2885 #undef	ST_HDR
2886 #undef	ST_HDRL
2887 #undef	S_CT
2888 #undef	ST_CT
2889 #undef	ST_CTL
2890 }
2891 
2892 static void
2893 dump_dynamic(struct readelf *re)
2894 {
2895 	GElf_Dyn	 dyn;
2896 	Elf_Data	*d;
2897 	struct section	*s;
2898 	int		 elferr, i, is_dynamic, j, jmax, nentries;
2899 
2900 	is_dynamic = 0;
2901 
2902 	for (i = 0; (size_t)i < re->shnum; i++) {
2903 		s = &re->sl[i];
2904 		if (s->type != SHT_DYNAMIC)
2905 			continue;
2906 		(void) elf_errno();
2907 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
2908 			elferr = elf_errno();
2909 			if (elferr != 0)
2910 				warnx("elf_getdata failed: %s", elf_errmsg(-1));
2911 			continue;
2912 		}
2913 		if (d->d_size <= 0)
2914 			continue;
2915 
2916 		is_dynamic = 1;
2917 
2918 		/* Determine the actual number of table entries. */
2919 		nentries = 0;
2920 		jmax = (int) (s->sz / s->entsize);
2921 
2922 		for (j = 0; j < jmax; j++) {
2923 			if (gelf_getdyn(d, j, &dyn) != &dyn) {
2924 				warnx("gelf_getdyn failed: %s",
2925 				    elf_errmsg(-1));
2926 				continue;
2927 			}
2928 			nentries ++;
2929 			if (dyn.d_tag == DT_NULL)
2930 				break;
2931                 }
2932 
2933 		printf("\nDynamic section at offset 0x%jx", (uintmax_t)s->off);
2934 		printf(" contains %u entries:\n", nentries);
2935 
2936 		if (re->ec == ELFCLASS32)
2937 			printf("%5s%12s%28s\n", "Tag", "Type", "Name/Value");
2938 		else
2939 			printf("%5s%20s%28s\n", "Tag", "Type", "Name/Value");
2940 
2941 		for (j = 0; j < nentries; j++) {
2942 			if (gelf_getdyn(d, j, &dyn) != &dyn)
2943 				continue;
2944 			/* Dump dynamic entry type. */
2945 			if (re->ec == ELFCLASS32)
2946 				printf(" 0x%8.8jx", (uintmax_t)dyn.d_tag);
2947 			else
2948 				printf(" 0x%16.16jx", (uintmax_t)dyn.d_tag);
2949 			printf(" %-20s", dt_type(re->ehdr.e_machine,
2950 			    dyn.d_tag));
2951 			/* Dump dynamic entry value. */
2952 			dump_dyn_val(re, &dyn, s->link);
2953 		}
2954 	}
2955 
2956 	if (!is_dynamic)
2957 		printf("\nThere is no dynamic section in this file.\n");
2958 }
2959 
2960 static char *
2961 timestamp(time_t ti)
2962 {
2963 	static char ts[32];
2964 	struct tm *t;
2965 
2966 	t = gmtime(&ti);
2967 	snprintf(ts, sizeof(ts), "%04d-%02d-%02dT%02d:%02d:%02d",
2968 	    t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour,
2969 	    t->tm_min, t->tm_sec);
2970 
2971 	return (ts);
2972 }
2973 
2974 static const char *
2975 dyn_str(struct readelf *re, uint32_t stab, uint64_t d_val)
2976 {
2977 	const char *name;
2978 
2979 	if (stab == SHN_UNDEF)
2980 		name = "ERROR";
2981 	else if ((name = elf_strptr(re->elf, stab, d_val)) == NULL) {
2982 		(void) elf_errno(); /* clear error */
2983 		name = "ERROR";
2984 	}
2985 
2986 	return (name);
2987 }
2988 
2989 static void
2990 dump_arch_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab)
2991 {
2992 	const char *name;
2993 
2994 	switch (re->ehdr.e_machine) {
2995 	case EM_MIPS:
2996 	case EM_MIPS_RS3_LE:
2997 		switch (dyn->d_tag) {
2998 		case DT_MIPS_RLD_VERSION:
2999 		case DT_MIPS_LOCAL_GOTNO:
3000 		case DT_MIPS_CONFLICTNO:
3001 		case DT_MIPS_LIBLISTNO:
3002 		case DT_MIPS_SYMTABNO:
3003 		case DT_MIPS_UNREFEXTNO:
3004 		case DT_MIPS_GOTSYM:
3005 		case DT_MIPS_HIPAGENO:
3006 		case DT_MIPS_DELTA_CLASS_NO:
3007 		case DT_MIPS_DELTA_INSTANCE_NO:
3008 		case DT_MIPS_DELTA_RELOC_NO:
3009 		case DT_MIPS_DELTA_SYM_NO:
3010 		case DT_MIPS_DELTA_CLASSSYM_NO:
3011 		case DT_MIPS_LOCALPAGE_GOTIDX:
3012 		case DT_MIPS_LOCAL_GOTIDX:
3013 		case DT_MIPS_HIDDEN_GOTIDX:
3014 		case DT_MIPS_PROTECTED_GOTIDX:
3015 			printf(" %ju\n", (uintmax_t) dyn->d_un.d_val);
3016 			break;
3017 		case DT_MIPS_ICHECKSUM:
3018 		case DT_MIPS_FLAGS:
3019 		case DT_MIPS_BASE_ADDRESS:
3020 		case DT_MIPS_CONFLICT:
3021 		case DT_MIPS_LIBLIST:
3022 		case DT_MIPS_RLD_MAP:
3023 		case DT_MIPS_DELTA_CLASS:
3024 		case DT_MIPS_DELTA_INSTANCE:
3025 		case DT_MIPS_DELTA_RELOC:
3026 		case DT_MIPS_DELTA_SYM:
3027 		case DT_MIPS_DELTA_CLASSSYM:
3028 		case DT_MIPS_CXX_FLAGS:
3029 		case DT_MIPS_PIXIE_INIT:
3030 		case DT_MIPS_SYMBOL_LIB:
3031 		case DT_MIPS_OPTIONS:
3032 		case DT_MIPS_INTERFACE:
3033 		case DT_MIPS_DYNSTR_ALIGN:
3034 		case DT_MIPS_INTERFACE_SIZE:
3035 		case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
3036 		case DT_MIPS_COMPACT_SIZE:
3037 		case DT_MIPS_GP_VALUE:
3038 		case DT_MIPS_AUX_DYNAMIC:
3039 		case DT_MIPS_PLTGOT:
3040 		case DT_MIPS_RLD_OBJ_UPDATE:
3041 		case DT_MIPS_RWPLT:
3042 			printf(" 0x%jx\n", (uintmax_t) dyn->d_un.d_val);
3043 			break;
3044 		case DT_MIPS_IVERSION:
3045 		case DT_MIPS_PERF_SUFFIX:
3046 		case DT_AUXILIARY:
3047 		case DT_FILTER:
3048 			name = dyn_str(re, stab, dyn->d_un.d_val);
3049 			printf(" %s\n", name);
3050 			break;
3051 		case DT_MIPS_TIME_STAMP:
3052 			printf(" %s\n", timestamp(dyn->d_un.d_val));
3053 			break;
3054 		}
3055 		break;
3056 	default:
3057 		printf("\n");
3058 		break;
3059 	}
3060 }
3061 
3062 static void
3063 dump_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab)
3064 {
3065 	const char *name;
3066 
3067 	if (dyn->d_tag >= DT_LOPROC && dyn->d_tag <= DT_HIPROC) {
3068 		dump_arch_dyn_val(re, dyn, stab);
3069 		return;
3070 	}
3071 
3072 	/* These entry values are index into the string table. */
3073 	name = NULL;
3074 	if (dyn->d_tag == DT_NEEDED || dyn->d_tag == DT_SONAME ||
3075 	    dyn->d_tag == DT_RPATH || dyn->d_tag == DT_RUNPATH)
3076 		name = dyn_str(re, stab, dyn->d_un.d_val);
3077 
3078 	switch(dyn->d_tag) {
3079 	case DT_NULL:
3080 	case DT_PLTGOT:
3081 	case DT_HASH:
3082 	case DT_STRTAB:
3083 	case DT_SYMTAB:
3084 	case DT_RELA:
3085 	case DT_INIT:
3086 	case DT_SYMBOLIC:
3087 	case DT_REL:
3088 	case DT_DEBUG:
3089 	case DT_TEXTREL:
3090 	case DT_JMPREL:
3091 	case DT_FINI:
3092 	case DT_VERDEF:
3093 	case DT_VERNEED:
3094 	case DT_VERSYM:
3095 	case DT_GNU_HASH:
3096 	case DT_GNU_LIBLIST:
3097 	case DT_GNU_CONFLICT:
3098 		printf(" 0x%jx\n", (uintmax_t) dyn->d_un.d_val);
3099 		break;
3100 	case DT_PLTRELSZ:
3101 	case DT_RELASZ:
3102 	case DT_RELAENT:
3103 	case DT_STRSZ:
3104 	case DT_SYMENT:
3105 	case DT_RELSZ:
3106 	case DT_RELENT:
3107 	case DT_INIT_ARRAYSZ:
3108 	case DT_FINI_ARRAYSZ:
3109 	case DT_GNU_CONFLICTSZ:
3110 	case DT_GNU_LIBLISTSZ:
3111 		printf(" %ju (bytes)\n", (uintmax_t) dyn->d_un.d_val);
3112 		break;
3113  	case DT_RELACOUNT:
3114 	case DT_RELCOUNT:
3115 	case DT_VERDEFNUM:
3116 	case DT_VERNEEDNUM:
3117 		printf(" %ju\n", (uintmax_t) dyn->d_un.d_val);
3118 		break;
3119 	case DT_NEEDED:
3120 		printf(" Shared library: [%s]\n", name);
3121 		break;
3122 	case DT_SONAME:
3123 		printf(" Library soname: [%s]\n", name);
3124 		break;
3125 	case DT_RPATH:
3126 		printf(" Library rpath: [%s]\n", name);
3127 		break;
3128 	case DT_RUNPATH:
3129 		printf(" Library runpath: [%s]\n", name);
3130 		break;
3131 	case DT_PLTREL:
3132 		printf(" %s\n", dt_type(re->ehdr.e_machine, dyn->d_un.d_val));
3133 		break;
3134 	case DT_GNU_PRELINKED:
3135 		printf(" %s\n", timestamp(dyn->d_un.d_val));
3136 		break;
3137 	default:
3138 		printf("\n");
3139 	}
3140 }
3141 
3142 static void
3143 dump_rel(struct readelf *re, struct section *s, Elf_Data *d)
3144 {
3145 	GElf_Rel r;
3146 	const char *symname;
3147 	uint64_t symval;
3148 	int i, len;
3149 
3150 #define	REL_HDR "r_offset", "r_info", "r_type", "st_value", "st_name"
3151 #define	REL_CT32 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
3152 		r_type(re->ehdr.e_machine, ELF32_R_TYPE(r.r_info)), \
3153 		(uintmax_t)symval, symname
3154 #define	REL_CT64 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
3155 		r_type(re->ehdr.e_machine, ELF64_R_TYPE(r.r_info)), \
3156 		(uintmax_t)symval, symname
3157 
3158 	printf("\nRelocation section (%s):\n", s->name);
3159 	if (re->ec == ELFCLASS32)
3160 		printf("%-8s %-8s %-19s %-8s %s\n", REL_HDR);
3161 	else {
3162 		if (re->options & RE_WW)
3163 			printf("%-16s %-16s %-24s %-16s %s\n", REL_HDR);
3164 		else
3165 			printf("%-12s %-12s %-19s %-16s %s\n", REL_HDR);
3166 	}
3167 	len = d->d_size / s->entsize;
3168 	for (i = 0; i < len; i++) {
3169 		if (gelf_getrel(d, i, &r) != &r) {
3170 			warnx("gelf_getrel failed: %s", elf_errmsg(-1));
3171 			continue;
3172 		}
3173 		if (s->link >= re->shnum) {
3174 			warnx("invalid section link index %u", s->link);
3175 			continue;
3176 		}
3177 		symname = get_symbol_name(re, s->link, GELF_R_SYM(r.r_info));
3178 		symval = get_symbol_value(re, s->link, GELF_R_SYM(r.r_info));
3179 		if (re->ec == ELFCLASS32) {
3180 			r.r_info = ELF32_R_INFO(ELF64_R_SYM(r.r_info),
3181 			    ELF64_R_TYPE(r.r_info));
3182 			printf("%8.8jx %8.8jx %-19.19s %8.8jx %s\n", REL_CT32);
3183 		} else {
3184 			if (re->options & RE_WW)
3185 				printf("%16.16jx %16.16jx %-24.24s"
3186 				    " %16.16jx %s\n", REL_CT64);
3187 			else
3188 				printf("%12.12jx %12.12jx %-19.19s"
3189 				    " %16.16jx %s\n", REL_CT64);
3190 		}
3191 	}
3192 
3193 #undef	REL_HDR
3194 #undef	REL_CT
3195 }
3196 
3197 static void
3198 dump_rela(struct readelf *re, struct section *s, Elf_Data *d)
3199 {
3200 	GElf_Rela r;
3201 	const char *symname;
3202 	uint64_t symval;
3203 	int i, len;
3204 
3205 #define	RELA_HDR "r_offset", "r_info", "r_type", "st_value", \
3206 		"st_name + r_addend"
3207 #define	RELA_CT32 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
3208 		r_type(re->ehdr.e_machine, ELF32_R_TYPE(r.r_info)), \
3209 		(uintmax_t)symval, symname
3210 #define	RELA_CT64 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
3211 		r_type(re->ehdr.e_machine, ELF64_R_TYPE(r.r_info)), \
3212 		(uintmax_t)symval, symname
3213 
3214 	printf("\nRelocation section with addend (%s):\n", s->name);
3215 	if (re->ec == ELFCLASS32)
3216 		printf("%-8s %-8s %-19s %-8s %s\n", RELA_HDR);
3217 	else {
3218 		if (re->options & RE_WW)
3219 			printf("%-16s %-16s %-24s %-16s %s\n", RELA_HDR);
3220 		else
3221 			printf("%-12s %-12s %-19s %-16s %s\n", RELA_HDR);
3222 	}
3223 	len = d->d_size / s->entsize;
3224 	for (i = 0; i < len; i++) {
3225 		if (gelf_getrela(d, i, &r) != &r) {
3226 			warnx("gelf_getrel failed: %s", elf_errmsg(-1));
3227 			continue;
3228 		}
3229 		if (s->link >= re->shnum) {
3230 			warnx("invalid section link index %u", s->link);
3231 			continue;
3232 		}
3233 		symname = get_symbol_name(re, s->link, GELF_R_SYM(r.r_info));
3234 		symval = get_symbol_value(re, s->link, GELF_R_SYM(r.r_info));
3235 		if (re->ec == ELFCLASS32) {
3236 			r.r_info = ELF32_R_INFO(ELF64_R_SYM(r.r_info),
3237 			    ELF64_R_TYPE(r.r_info));
3238 			printf("%8.8jx %8.8jx %-19.19s %8.8jx %s", RELA_CT32);
3239 			printf(" + %x\n", (uint32_t) r.r_addend);
3240 		} else {
3241 			if (re->options & RE_WW)
3242 				printf("%16.16jx %16.16jx %-24.24s"
3243 				    " %16.16jx %s", RELA_CT64);
3244 			else
3245 				printf("%12.12jx %12.12jx %-19.19s"
3246 				    " %16.16jx %s", RELA_CT64);
3247 			printf(" + %jx\n", (uintmax_t) r.r_addend);
3248 		}
3249 	}
3250 
3251 #undef	RELA_HDR
3252 #undef	RELA_CT
3253 }
3254 
3255 static void
3256 dump_reloc(struct readelf *re)
3257 {
3258 	struct section *s;
3259 	Elf_Data *d;
3260 	int i, elferr;
3261 
3262 	for (i = 0; (size_t)i < re->shnum; i++) {
3263 		s = &re->sl[i];
3264 		if (s->type == SHT_REL || s->type == SHT_RELA) {
3265 			(void) elf_errno();
3266 			if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3267 				elferr = elf_errno();
3268 				if (elferr != 0)
3269 					warnx("elf_getdata failed: %s",
3270 					    elf_errmsg(elferr));
3271 				continue;
3272 			}
3273 			if (s->type == SHT_REL)
3274 				dump_rel(re, s, d);
3275 			else
3276 				dump_rela(re, s, d);
3277 		}
3278 	}
3279 }
3280 
3281 static void
3282 dump_symtab(struct readelf *re, int i)
3283 {
3284 	struct section *s;
3285 	Elf_Data *d;
3286 	GElf_Sym sym;
3287 	const char *name;
3288 	int elferr, stab, j;
3289 
3290 	s = &re->sl[i];
3291 	stab = s->link;
3292 	(void) elf_errno();
3293 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3294 		elferr = elf_errno();
3295 		if (elferr != 0)
3296 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
3297 		return;
3298 	}
3299 	if (d->d_size <= 0)
3300 		return;
3301 	printf("Symbol table (%s)", s->name);
3302 	printf(" contains %ju entries:\n", s->sz / s->entsize);
3303 	printf("%7s%9s%14s%5s%8s%6s%9s%5s\n", "Num:", "Value", "Size", "Type",
3304 	    "Bind", "Vis", "Ndx", "Name");
3305 
3306 	for (j = 0; (uint64_t)j < s->sz / s->entsize; j++) {
3307 		if (gelf_getsym(d, j, &sym) != &sym) {
3308 			warnx("gelf_getsym failed: %s", elf_errmsg(-1));
3309 			continue;
3310 		}
3311 		printf("%6d:", j);
3312 		printf(" %16.16jx", (uintmax_t)sym.st_value);
3313 		printf(" %5ju", sym.st_size);
3314 		printf(" %-7s", st_type(GELF_ST_TYPE(sym.st_info)));
3315 		printf(" %-6s", st_bind(GELF_ST_BIND(sym.st_info)));
3316 		printf(" %-8s", st_vis(GELF_ST_VISIBILITY(sym.st_other)));
3317 		printf(" %3s", st_shndx(sym.st_shndx));
3318 		if ((name = elf_strptr(re->elf, stab, sym.st_name)) != NULL)
3319 			printf(" %s", name);
3320 		/* Append symbol version string for SHT_DYNSYM symbol table. */
3321 		if (s->type == SHT_DYNSYM && re->ver != NULL &&
3322 		    re->vs != NULL && re->vs[j] > 1) {
3323 			if (re->vs[j] & 0x8000 ||
3324 			    re->ver[re->vs[j] & 0x7fff].type == 0)
3325 				printf("@%s (%d)",
3326 				    re->ver[re->vs[j] & 0x7fff].name,
3327 				    re->vs[j] & 0x7fff);
3328 			else
3329 				printf("@@%s (%d)", re->ver[re->vs[j]].name,
3330 				    re->vs[j]);
3331 		}
3332 		putchar('\n');
3333 	}
3334 
3335 }
3336 
3337 static void
3338 dump_symtabs(struct readelf *re)
3339 {
3340 	GElf_Dyn dyn;
3341 	Elf_Data *d;
3342 	struct section *s;
3343 	uint64_t dyn_off;
3344 	int elferr, i;
3345 
3346 	/*
3347 	 * If -D is specified, only dump the symbol table specified by
3348 	 * the DT_SYMTAB entry in the .dynamic section.
3349 	 */
3350 	dyn_off = 0;
3351 	if (re->options & RE_DD) {
3352 		s = NULL;
3353 		for (i = 0; (size_t)i < re->shnum; i++)
3354 			if (re->sl[i].type == SHT_DYNAMIC) {
3355 				s = &re->sl[i];
3356 				break;
3357 			}
3358 		if (s == NULL)
3359 			return;
3360 		(void) elf_errno();
3361 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3362 			elferr = elf_errno();
3363 			if (elferr != 0)
3364 				warnx("elf_getdata failed: %s", elf_errmsg(-1));
3365 			return;
3366 		}
3367 		if (d->d_size <= 0)
3368 			return;
3369 
3370 		for (i = 0; (uint64_t)i < s->sz / s->entsize; i++) {
3371 			if (gelf_getdyn(d, i, &dyn) != &dyn) {
3372 				warnx("gelf_getdyn failed: %s", elf_errmsg(-1));
3373 				continue;
3374 			}
3375 			if (dyn.d_tag == DT_SYMTAB) {
3376 				dyn_off = dyn.d_un.d_val;
3377 				break;
3378 			}
3379 		}
3380 	}
3381 
3382 	/* Find and dump symbol tables. */
3383 	for (i = 0; (size_t)i < re->shnum; i++) {
3384 		s = &re->sl[i];
3385 		if (s->type == SHT_SYMTAB || s->type == SHT_DYNSYM) {
3386 			if (re->options & RE_DD) {
3387 				if (dyn_off == s->addr) {
3388 					dump_symtab(re, i);
3389 					break;
3390 				}
3391 			} else
3392 				dump_symtab(re, i);
3393 		}
3394 	}
3395 }
3396 
3397 static void
3398 dump_svr4_hash(struct section *s)
3399 {
3400 	Elf_Data	*d;
3401 	uint32_t	*buf;
3402 	uint32_t	 nbucket, nchain;
3403 	uint32_t	*bucket, *chain;
3404 	uint32_t	*bl, *c, maxl, total;
3405 	int		 elferr, i, j;
3406 
3407 	/* Read and parse the content of .hash section. */
3408 	(void) elf_errno();
3409 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3410 		elferr = elf_errno();
3411 		if (elferr != 0)
3412 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
3413 		return;
3414 	}
3415 	if (d->d_size < 2 * sizeof(uint32_t)) {
3416 		warnx(".hash section too small");
3417 		return;
3418 	}
3419 	buf = d->d_buf;
3420 	nbucket = buf[0];
3421 	nchain = buf[1];
3422 	if (nbucket <= 0 || nchain <= 0) {
3423 		warnx("Malformed .hash section");
3424 		return;
3425 	}
3426 	if (d->d_size != (nbucket + nchain + 2) * sizeof(uint32_t)) {
3427 		warnx("Malformed .hash section");
3428 		return;
3429 	}
3430 	bucket = &buf[2];
3431 	chain = &buf[2 + nbucket];
3432 
3433 	maxl = 0;
3434 	if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
3435 		errx(EXIT_FAILURE, "calloc failed");
3436 	for (i = 0; (uint32_t)i < nbucket; i++)
3437 		for (j = bucket[i]; j > 0 && (uint32_t)j < nchain; j = chain[j])
3438 			if (++bl[i] > maxl)
3439 				maxl = bl[i];
3440 	if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
3441 		errx(EXIT_FAILURE, "calloc failed");
3442 	for (i = 0; (uint32_t)i < nbucket; i++)
3443 		c[bl[i]]++;
3444 	printf("\nHistogram for bucket list length (total of %u buckets):\n",
3445 	    nbucket);
3446 	printf(" Length\tNumber\t\t%% of total\tCoverage\n");
3447 	total = 0;
3448 	for (i = 0; (uint32_t)i <= maxl; i++) {
3449 		total += c[i] * i;
3450 		printf("%7u\t%-10u\t(%5.1f%%)\t%5.1f%%\n", i, c[i],
3451 		    c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
3452 	}
3453 	free(c);
3454 	free(bl);
3455 }
3456 
3457 static void
3458 dump_svr4_hash64(struct readelf *re, struct section *s)
3459 {
3460 	Elf_Data	*d, dst;
3461 	uint64_t	*buf;
3462 	uint64_t	 nbucket, nchain;
3463 	uint64_t	*bucket, *chain;
3464 	uint64_t	*bl, *c, maxl, total;
3465 	int		 elferr, i, j;
3466 
3467 	/*
3468 	 * ALPHA uses 64-bit hash entries. Since libelf assumes that
3469 	 * .hash section contains only 32-bit entry, an explicit
3470 	 * gelf_xlatetom is needed here.
3471 	 */
3472 	(void) elf_errno();
3473 	if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
3474 		elferr = elf_errno();
3475 		if (elferr != 0)
3476 			warnx("elf_rawdata failed: %s",
3477 			    elf_errmsg(elferr));
3478 		return;
3479 	}
3480 	d->d_type = ELF_T_XWORD;
3481 	memcpy(&dst, d, sizeof(Elf_Data));
3482 	if (gelf_xlatetom(re->elf, &dst, d,
3483 		re->ehdr.e_ident[EI_DATA]) != &dst) {
3484 		warnx("gelf_xlatetom failed: %s", elf_errmsg(-1));
3485 		return;
3486 	}
3487 	if (dst.d_size < 2 * sizeof(uint64_t)) {
3488 		warnx(".hash section too small");
3489 		return;
3490 	}
3491 	buf = dst.d_buf;
3492 	nbucket = buf[0];
3493 	nchain = buf[1];
3494 	if (nbucket <= 0 || nchain <= 0) {
3495 		warnx("Malformed .hash section");
3496 		return;
3497 	}
3498 	if (d->d_size != (nbucket + nchain + 2) * sizeof(uint32_t)) {
3499 		warnx("Malformed .hash section");
3500 		return;
3501 	}
3502 	bucket = &buf[2];
3503 	chain = &buf[2 + nbucket];
3504 
3505 	maxl = 0;
3506 	if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
3507 		errx(EXIT_FAILURE, "calloc failed");
3508 	for (i = 0; (uint32_t)i < nbucket; i++)
3509 		for (j = bucket[i]; j > 0 && (uint32_t)j < nchain; j = chain[j])
3510 			if (++bl[i] > maxl)
3511 				maxl = bl[i];
3512 	if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
3513 		errx(EXIT_FAILURE, "calloc failed");
3514 	for (i = 0; (uint64_t)i < nbucket; i++)
3515 		c[bl[i]]++;
3516 	printf("Histogram for bucket list length (total of %ju buckets):\n",
3517 	    (uintmax_t)nbucket);
3518 	printf(" Length\tNumber\t\t%% of total\tCoverage\n");
3519 	total = 0;
3520 	for (i = 0; (uint64_t)i <= maxl; i++) {
3521 		total += c[i] * i;
3522 		printf("%7u\t%-10ju\t(%5.1f%%)\t%5.1f%%\n", i, (uintmax_t)c[i],
3523 		    c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
3524 	}
3525 	free(c);
3526 	free(bl);
3527 }
3528 
3529 static void
3530 dump_gnu_hash(struct readelf *re, struct section *s)
3531 {
3532 	struct section	*ds;
3533 	Elf_Data	*d;
3534 	uint32_t	*buf;
3535 	uint32_t	*bucket, *chain;
3536 	uint32_t	 nbucket, nchain, symndx, maskwords;
3537 	uint32_t	*bl, *c, maxl, total;
3538 	int		 elferr, dynsymcount, i, j;
3539 
3540 	(void) elf_errno();
3541 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3542 		elferr = elf_errno();
3543 		if (elferr != 0)
3544 			warnx("elf_getdata failed: %s",
3545 			    elf_errmsg(elferr));
3546 		return;
3547 	}
3548 	if (d->d_size < 4 * sizeof(uint32_t)) {
3549 		warnx(".gnu.hash section too small");
3550 		return;
3551 	}
3552 	buf = d->d_buf;
3553 	nbucket = buf[0];
3554 	symndx = buf[1];
3555 	maskwords = buf[2];
3556 	buf += 4;
3557 	ds = &re->sl[s->link];
3558 	dynsymcount = ds->sz / ds->entsize;
3559 	nchain = dynsymcount - symndx;
3560 	if (d->d_size != 4 * sizeof(uint32_t) + maskwords *
3561 	    (re->ec == ELFCLASS32 ? sizeof(uint32_t) : sizeof(uint64_t)) +
3562 	    (nbucket + nchain) * sizeof(uint32_t)) {
3563 		warnx("Malformed .gnu.hash section");
3564 		return;
3565 	}
3566 	bucket = buf + (re->ec == ELFCLASS32 ? maskwords : maskwords * 2);
3567 	chain = bucket + nbucket;
3568 
3569 	maxl = 0;
3570 	if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
3571 		errx(EXIT_FAILURE, "calloc failed");
3572 	for (i = 0; (uint32_t)i < nbucket; i++)
3573 		for (j = bucket[i]; j > 0 && (uint32_t)j - symndx < nchain;
3574 		     j++) {
3575 			if (++bl[i] > maxl)
3576 				maxl = bl[i];
3577 			if (chain[j - symndx] & 1)
3578 				break;
3579 		}
3580 	if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
3581 		errx(EXIT_FAILURE, "calloc failed");
3582 	for (i = 0; (uint32_t)i < nbucket; i++)
3583 		c[bl[i]]++;
3584 	printf("Histogram for bucket list length (total of %u buckets):\n",
3585 	    nbucket);
3586 	printf(" Length\tNumber\t\t%% of total\tCoverage\n");
3587 	total = 0;
3588 	for (i = 0; (uint32_t)i <= maxl; i++) {
3589 		total += c[i] * i;
3590 		printf("%7u\t%-10u\t(%5.1f%%)\t%5.1f%%\n", i, c[i],
3591 		    c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
3592 	}
3593 	free(c);
3594 	free(bl);
3595 }
3596 
3597 static void
3598 dump_hash(struct readelf *re)
3599 {
3600 	struct section	*s;
3601 	int		 i;
3602 
3603 	for (i = 0; (size_t) i < re->shnum; i++) {
3604 		s = &re->sl[i];
3605 		if (s->type == SHT_HASH || s->type == SHT_GNU_HASH) {
3606 			if (s->type == SHT_GNU_HASH)
3607 				dump_gnu_hash(re, s);
3608 			else if (re->ehdr.e_machine == EM_ALPHA &&
3609 			    s->entsize == 8)
3610 				dump_svr4_hash64(re, s);
3611 			else
3612 				dump_svr4_hash(s);
3613 		}
3614 	}
3615 }
3616 
3617 static void
3618 dump_notes(struct readelf *re)
3619 {
3620 	struct section *s;
3621 	const char *rawfile;
3622 	GElf_Phdr phdr;
3623 	Elf_Data *d;
3624 	size_t phnum;
3625 	int i, elferr;
3626 
3627 	if (re->ehdr.e_type == ET_CORE) {
3628 		/*
3629 		 * Search program headers in the core file for
3630 		 * PT_NOTE entry.
3631 		 */
3632 		if (elf_getphnum(re->elf, &phnum) == 0) {
3633 			warnx("elf_getphnum failed: %s", elf_errmsg(-1));
3634 			return;
3635 		}
3636 		if (phnum == 0)
3637 			return;
3638 		if ((rawfile = elf_rawfile(re->elf, NULL)) == NULL) {
3639 			warnx("elf_rawfile failed: %s", elf_errmsg(-1));
3640 			return;
3641 		}
3642 		for (i = 0; (size_t) i < phnum; i++) {
3643 			if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
3644 				warnx("gelf_getphdr failed: %s",
3645 				    elf_errmsg(-1));
3646 				continue;
3647 			}
3648 			if (phdr.p_type == PT_NOTE)
3649 				dump_notes_content(re, rawfile + phdr.p_offset,
3650 				    phdr.p_filesz, phdr.p_offset);
3651 		}
3652 
3653 	} else {
3654 		/*
3655 		 * For objects other than core files, Search for
3656 		 * SHT_NOTE sections.
3657 		 */
3658 		for (i = 0; (size_t) i < re->shnum; i++) {
3659 			s = &re->sl[i];
3660 			if (s->type == SHT_NOTE) {
3661 				(void) elf_errno();
3662 				if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3663 					elferr = elf_errno();
3664 					if (elferr != 0)
3665 						warnx("elf_getdata failed: %s",
3666 						    elf_errmsg(elferr));
3667 					continue;
3668 				}
3669 				dump_notes_content(re, d->d_buf, d->d_size,
3670 				    s->off);
3671 			}
3672 		}
3673 	}
3674 }
3675 
3676 static void
3677 dump_notes_content(struct readelf *re, const char *buf, size_t sz, off_t off)
3678 {
3679 	Elf_Note *note;
3680 	const char *end, *name;
3681 
3682 	printf("\nNotes at offset %#010jx with length %#010jx:\n",
3683 	    (uintmax_t) off, (uintmax_t) sz);
3684 	printf("  %-13s %-15s %s\n", "Owner", "Data size", "Description");
3685 	end = buf + sz;
3686 	while (buf < end) {
3687 		if (buf + sizeof(*note) > end) {
3688 			warnx("invalid note header");
3689 			return;
3690 		}
3691 		note = (Elf_Note *)(uintptr_t) buf;
3692 		name = (char *)(uintptr_t)(note + 1);
3693 		/*
3694 		 * The name field is required to be nul-terminated, and
3695 		 * n_namesz includes the terminating nul in observed
3696 		 * implementations (contrary to the ELF-64 spec). A special
3697 		 * case is needed for cores generated by some older Linux
3698 		 * versions, which write a note named "CORE" without a nul
3699 		 * terminator and n_namesz = 4.
3700 		 */
3701 		if (note->n_namesz == 0)
3702 			name = "";
3703 		else if (note->n_namesz == 4 && strncmp(name, "CORE", 4) == 0)
3704 			name = "CORE";
3705 		else if (strnlen(name, note->n_namesz) >= note->n_namesz)
3706 			name = "<invalid>";
3707 		printf("  %-13s %#010jx", name, (uintmax_t) note->n_descsz);
3708 		printf("      %s\n", note_type(name, re->ehdr.e_type,
3709 		    note->n_type));
3710 		buf += sizeof(Elf_Note) + roundup2(note->n_namesz, 4) +
3711 		    roundup2(note->n_descsz, 4);
3712 	}
3713 }
3714 
3715 /*
3716  * Symbol versioning sections are the same for 32bit and 64bit
3717  * ELF objects.
3718  */
3719 #define Elf_Verdef	Elf32_Verdef
3720 #define	Elf_Verdaux	Elf32_Verdaux
3721 #define	Elf_Verneed	Elf32_Verneed
3722 #define	Elf_Vernaux	Elf32_Vernaux
3723 
3724 #define	SAVE_VERSION_NAME(x, n, t)					\
3725 	do {								\
3726 		while (x >= re->ver_sz) {				\
3727 			nv = realloc(re->ver,				\
3728 			    sizeof(*re->ver) * re->ver_sz * 2);		\
3729 			if (nv == NULL) {				\
3730 				warn("realloc failed");			\
3731 				free(re->ver);				\
3732 				return;					\
3733 			}						\
3734 			re->ver = nv;					\
3735 			for (i = re->ver_sz; i < re->ver_sz * 2; i++) {	\
3736 				re->ver[i].name = NULL;			\
3737 				re->ver[i].type = 0;			\
3738 			}						\
3739 			re->ver_sz *= 2;				\
3740 		}							\
3741 		if (x > 1) {						\
3742 			re->ver[x].name = n;				\
3743 			re->ver[x].type = t;				\
3744 		}							\
3745 	} while (0)
3746 
3747 
3748 static void
3749 dump_verdef(struct readelf *re, int dump)
3750 {
3751 	struct section *s;
3752 	struct symver *nv;
3753 	Elf_Data *d;
3754 	Elf_Verdef *vd;
3755 	Elf_Verdaux *vda;
3756 	uint8_t *buf, *end, *buf2;
3757 	const char *name;
3758 	int elferr, i, j;
3759 
3760 	if ((s = re->vd_s) == NULL)
3761 		return;
3762 
3763 	if (re->ver == NULL) {
3764 		re->ver_sz = 16;
3765 		if ((re->ver = calloc(re->ver_sz, sizeof(*re->ver))) ==
3766 		    NULL) {
3767 			warn("calloc failed");
3768 			return;
3769 		}
3770 		re->ver[0].name = "*local*";
3771 		re->ver[1].name = "*global*";
3772 	}
3773 
3774 	if (dump)
3775 		printf("\nVersion definition section (%s):\n", s->name);
3776 	(void) elf_errno();
3777 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3778 		elferr = elf_errno();
3779 		if (elferr != 0)
3780 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
3781 		return;
3782 	}
3783 	if (d->d_size == 0)
3784 		return;
3785 
3786 	buf = d->d_buf;
3787 	end = buf + d->d_size;
3788 	while (buf + sizeof(Elf_Verdef) <= end) {
3789 		vd = (Elf_Verdef *) (uintptr_t) buf;
3790 		if (dump) {
3791 			printf("  0x%4.4lx", (unsigned long)
3792 			    (buf - (uint8_t *)d->d_buf));
3793 			printf(" vd_version: %u vd_flags: %d"
3794 			    " vd_ndx: %u vd_cnt: %u", vd->vd_version,
3795 			    vd->vd_flags, vd->vd_ndx, vd->vd_cnt);
3796 		}
3797 		buf2 = buf + vd->vd_aux;
3798 		j = 0;
3799 		while (buf2 + sizeof(Elf_Verdaux) <= end && j < vd->vd_cnt) {
3800 			vda = (Elf_Verdaux *) (uintptr_t) buf2;
3801 			name = get_string(re, s->link, vda->vda_name);
3802 			if (j == 0) {
3803 				if (dump)
3804 					printf(" vda_name: %s\n", name);
3805 				SAVE_VERSION_NAME((int)vd->vd_ndx, name, 1);
3806 			} else if (dump)
3807 				printf("  0x%4.4lx parent: %s\n",
3808 				    (unsigned long) (buf2 -
3809 				    (uint8_t *)d->d_buf), name);
3810 			if (vda->vda_next == 0)
3811 				break;
3812 			buf2 += vda->vda_next;
3813 			j++;
3814 		}
3815 		if (vd->vd_next == 0)
3816 			break;
3817 		buf += vd->vd_next;
3818 	}
3819 }
3820 
3821 static void
3822 dump_verneed(struct readelf *re, int dump)
3823 {
3824 	struct section *s;
3825 	struct symver *nv;
3826 	Elf_Data *d;
3827 	Elf_Verneed *vn;
3828 	Elf_Vernaux *vna;
3829 	uint8_t *buf, *end, *buf2;
3830 	const char *name;
3831 	int elferr, i, j;
3832 
3833 	if ((s = re->vn_s) == NULL)
3834 		return;
3835 
3836 	if (re->ver == NULL) {
3837 		re->ver_sz = 16;
3838 		if ((re->ver = calloc(re->ver_sz, sizeof(*re->ver))) ==
3839 		    NULL) {
3840 			warn("calloc failed");
3841 			return;
3842 		}
3843 		re->ver[0].name = "*local*";
3844 		re->ver[1].name = "*global*";
3845 	}
3846 
3847 	if (dump)
3848 		printf("\nVersion needed section (%s):\n", s->name);
3849 	(void) elf_errno();
3850 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3851 		elferr = elf_errno();
3852 		if (elferr != 0)
3853 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
3854 		return;
3855 	}
3856 	if (d->d_size == 0)
3857 		return;
3858 
3859 	buf = d->d_buf;
3860 	end = buf + d->d_size;
3861 	while (buf + sizeof(Elf_Verneed) <= end) {
3862 		vn = (Elf_Verneed *) (uintptr_t) buf;
3863 		if (dump) {
3864 			printf("  0x%4.4lx", (unsigned long)
3865 			    (buf - (uint8_t *)d->d_buf));
3866 			printf(" vn_version: %u vn_file: %s vn_cnt: %u\n",
3867 			    vn->vn_version,
3868 			    get_string(re, s->link, vn->vn_file),
3869 			    vn->vn_cnt);
3870 		}
3871 		buf2 = buf + vn->vn_aux;
3872 		j = 0;
3873 		while (buf2 + sizeof(Elf_Vernaux) <= end && j < vn->vn_cnt) {
3874 			vna = (Elf32_Vernaux *) (uintptr_t) buf2;
3875 			if (dump)
3876 				printf("  0x%4.4lx", (unsigned long)
3877 				    (buf2 - (uint8_t *)d->d_buf));
3878 			name = get_string(re, s->link, vna->vna_name);
3879 			if (dump)
3880 				printf("   vna_name: %s vna_flags: %u"
3881 				    " vna_other: %u\n", name,
3882 				    vna->vna_flags, vna->vna_other);
3883 			SAVE_VERSION_NAME((int)vna->vna_other, name, 0);
3884 			if (vna->vna_next == 0)
3885 				break;
3886 			buf2 += vna->vna_next;
3887 			j++;
3888 		}
3889 		if (vn->vn_next == 0)
3890 			break;
3891 		buf += vn->vn_next;
3892 	}
3893 }
3894 
3895 static void
3896 dump_versym(struct readelf *re)
3897 {
3898 	int i;
3899 
3900 	if (re->vs_s == NULL || re->ver == NULL || re->vs == NULL)
3901 		return;
3902 	printf("\nVersion symbol section (%s):\n", re->vs_s->name);
3903 	for (i = 0; i < re->vs_sz; i++) {
3904 		if ((i & 3) == 0) {
3905 			if (i > 0)
3906 				putchar('\n');
3907 			printf("  %03x:", i);
3908 		}
3909 		if (re->vs[i] & 0x8000)
3910 			printf(" %3xh %-12s ", re->vs[i] & 0x7fff,
3911 			    re->ver[re->vs[i] & 0x7fff].name);
3912 		else
3913 			printf(" %3x %-12s ", re->vs[i],
3914 			    re->ver[re->vs[i]].name);
3915 	}
3916 	putchar('\n');
3917 }
3918 
3919 static void
3920 dump_ver(struct readelf *re)
3921 {
3922 
3923 	if (re->vs_s && re->ver && re->vs)
3924 		dump_versym(re);
3925 	if (re->vd_s)
3926 		dump_verdef(re, 1);
3927 	if (re->vn_s)
3928 		dump_verneed(re, 1);
3929 }
3930 
3931 static void
3932 search_ver(struct readelf *re)
3933 {
3934 	struct section *s;
3935 	Elf_Data *d;
3936 	int elferr, i;
3937 
3938 	for (i = 0; (size_t) i < re->shnum; i++) {
3939 		s = &re->sl[i];
3940 		if (s->type == SHT_SUNW_versym)
3941 			re->vs_s = s;
3942 		if (s->type == SHT_SUNW_verneed)
3943 			re->vn_s = s;
3944 		if (s->type == SHT_SUNW_verdef)
3945 			re->vd_s = s;
3946 	}
3947 	if (re->vd_s)
3948 		dump_verdef(re, 0);
3949 	if (re->vn_s)
3950 		dump_verneed(re, 0);
3951 	if (re->vs_s && re->ver != NULL) {
3952 		(void) elf_errno();
3953 		if ((d = elf_getdata(re->vs_s->scn, NULL)) == NULL) {
3954 			elferr = elf_errno();
3955 			if (elferr != 0)
3956 				warnx("elf_getdata failed: %s",
3957 				    elf_errmsg(elferr));
3958 			return;
3959 		}
3960 		if (d->d_size == 0)
3961 			return;
3962 		re->vs = d->d_buf;
3963 		re->vs_sz = d->d_size / sizeof(Elf32_Half);
3964 	}
3965 }
3966 
3967 #undef	Elf_Verdef
3968 #undef	Elf_Verdaux
3969 #undef	Elf_Verneed
3970 #undef	Elf_Vernaux
3971 #undef	SAVE_VERSION_NAME
3972 
3973 /*
3974  * Elf32_Lib and Elf64_Lib are identical.
3975  */
3976 #define	Elf_Lib		Elf32_Lib
3977 
3978 static void
3979 dump_liblist(struct readelf *re)
3980 {
3981 	struct section *s;
3982 	struct tm *t;
3983 	time_t ti;
3984 	char tbuf[20];
3985 	Elf_Data *d;
3986 	Elf_Lib *lib;
3987 	int i, j, k, elferr, first;
3988 
3989 	for (i = 0; (size_t) i < re->shnum; i++) {
3990 		s = &re->sl[i];
3991 		if (s->type != SHT_GNU_LIBLIST)
3992 			continue;
3993 		(void) elf_errno();
3994 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3995 			elferr = elf_errno();
3996 			if (elferr != 0)
3997 				warnx("elf_getdata failed: %s",
3998 				    elf_errmsg(elferr));
3999 			continue;
4000 		}
4001 		if (d->d_size <= 0)
4002 			continue;
4003 		lib = d->d_buf;
4004 		printf("\nLibrary list section '%s' ", s->name);
4005 		printf("contains %ju entries:\n", s->sz / s->entsize);
4006 		printf("%12s%24s%18s%10s%6s\n", "Library", "Time Stamp",
4007 		    "Checksum", "Version", "Flags");
4008 		for (j = 0; (uint64_t) j < s->sz / s->entsize; j++) {
4009 			printf("%3d: ", j);
4010 			printf("%-20.20s ",
4011 			    get_string(re, s->link, lib->l_name));
4012 			ti = lib->l_time_stamp;
4013 			t = gmtime(&ti);
4014 			snprintf(tbuf, sizeof(tbuf), "%04d-%02d-%02dT%02d:%02d"
4015 			    ":%2d", t->tm_year + 1900, t->tm_mon + 1,
4016 			    t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
4017 			printf("%-19.19s ", tbuf);
4018 			printf("0x%08x ", lib->l_checksum);
4019 			printf("%-7d %#x", lib->l_version, lib->l_flags);
4020 			if (lib->l_flags != 0) {
4021 				first = 1;
4022 				putchar('(');
4023 				for (k = 0; l_flag[k].name != NULL; k++) {
4024 					if ((l_flag[k].value & lib->l_flags) ==
4025 					    0)
4026 						continue;
4027 					if (!first)
4028 						putchar(',');
4029 					else
4030 						first = 0;
4031 					printf("%s", l_flag[k].name);
4032 				}
4033 				putchar(')');
4034 			}
4035 			putchar('\n');
4036 			lib++;
4037 		}
4038 	}
4039 }
4040 
4041 #undef Elf_Lib
4042 
4043 static uint8_t *
4044 dump_unknown_tag(uint64_t tag, uint8_t *p)
4045 {
4046 	uint64_t val;
4047 
4048 	/*
4049 	 * According to ARM EABI: For tags > 32, even numbered tags have
4050 	 * a ULEB128 param and odd numbered ones have NUL-terminated
4051 	 * string param. This rule probably also applies for tags <= 32
4052 	 * if the object arch is not ARM.
4053 	 */
4054 
4055 	printf("  Tag_unknown_%ju: ", (uintmax_t) tag);
4056 
4057 	if (tag & 1) {
4058 		printf("%s\n", (char *) p);
4059 		p += strlen((char *) p) + 1;
4060 	} else {
4061 		val = _decode_uleb128(&p);
4062 		printf("%ju\n", (uintmax_t) val);
4063 	}
4064 
4065 	return (p);
4066 }
4067 
4068 static uint8_t *
4069 dump_compatibility_tag(uint8_t *p)
4070 {
4071 	uint64_t val;
4072 
4073 	val = _decode_uleb128(&p);
4074 	printf("flag = %ju, vendor = %s\n", val, p);
4075 	p += strlen((char *) p) + 1;
4076 
4077 	return (p);
4078 }
4079 
4080 static void
4081 dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe)
4082 {
4083 	uint64_t tag, val;
4084 	size_t i;
4085 	int found, desc;
4086 
4087 	(void) re;
4088 
4089 	while (p < pe) {
4090 		tag = _decode_uleb128(&p);
4091 		found = desc = 0;
4092 		for (i = 0; i < sizeof(aeabi_tags) / sizeof(aeabi_tags[0]);
4093 		     i++) {
4094 			if (tag == aeabi_tags[i].tag) {
4095 				found = 1;
4096 				printf("  %s: ", aeabi_tags[i].s_tag);
4097 				if (aeabi_tags[i].get_desc) {
4098 					desc = 1;
4099 					val = _decode_uleb128(&p);
4100 					printf("%s\n",
4101 					    aeabi_tags[i].get_desc(val));
4102 				}
4103 				break;
4104 			}
4105 			if (tag < aeabi_tags[i].tag)
4106 				break;
4107 		}
4108 		if (!found) {
4109 			p = dump_unknown_tag(tag, p);
4110 			continue;
4111 		}
4112 		if (desc)
4113 			continue;
4114 
4115 		switch (tag) {
4116 		case 4:		/* Tag_CPU_raw_name */
4117 		case 5:		/* Tag_CPU_name */
4118 		case 67:	/* Tag_conformance */
4119 			printf("%s\n", (char *) p);
4120 			p += strlen((char *) p) + 1;
4121 			break;
4122 		case 32:	/* Tag_compatibility */
4123 			p = dump_compatibility_tag(p);
4124 			break;
4125 		case 64:	/* Tag_nodefaults */
4126 			/* ignored, written as 0. */
4127 			(void) _decode_uleb128(&p);
4128 			printf("True\n");
4129 			break;
4130 		case 65:	/* Tag_also_compatible_with */
4131 			val = _decode_uleb128(&p);
4132 			/* Must be Tag_CPU_arch */
4133 			if (val != 6) {
4134 				printf("unknown\n");
4135 				break;
4136 			}
4137 			val = _decode_uleb128(&p);
4138 			printf("%s\n", aeabi_cpu_arch(val));
4139 			/* Skip NUL terminator. */
4140 			p++;
4141 			break;
4142 		default:
4143 			putchar('\n');
4144 			break;
4145 		}
4146 	}
4147 }
4148 
4149 #ifndef	Tag_GNU_MIPS_ABI_FP
4150 #define	Tag_GNU_MIPS_ABI_FP	4
4151 #endif
4152 
4153 static void
4154 dump_mips_attributes(struct readelf *re, uint8_t *p, uint8_t *pe)
4155 {
4156 	uint64_t tag, val;
4157 
4158 	(void) re;
4159 
4160 	while (p < pe) {
4161 		tag = _decode_uleb128(&p);
4162 		switch (tag) {
4163 		case Tag_GNU_MIPS_ABI_FP:
4164 			val = _decode_uleb128(&p);
4165 			printf("  Tag_GNU_MIPS_ABI_FP: %s\n", mips_abi_fp(val));
4166 			break;
4167 		case 32:	/* Tag_compatibility */
4168 			p = dump_compatibility_tag(p);
4169 			break;
4170 		default:
4171 			p = dump_unknown_tag(tag, p);
4172 			break;
4173 		}
4174 	}
4175 }
4176 
4177 #ifndef Tag_GNU_Power_ABI_FP
4178 #define	Tag_GNU_Power_ABI_FP	4
4179 #endif
4180 
4181 #ifndef Tag_GNU_Power_ABI_Vector
4182 #define	Tag_GNU_Power_ABI_Vector	8
4183 #endif
4184 
4185 static void
4186 dump_ppc_attributes(uint8_t *p, uint8_t *pe)
4187 {
4188 	uint64_t tag, val;
4189 
4190 	while (p < pe) {
4191 		tag = _decode_uleb128(&p);
4192 		switch (tag) {
4193 		case Tag_GNU_Power_ABI_FP:
4194 			val = _decode_uleb128(&p);
4195 			printf("  Tag_GNU_Power_ABI_FP: %s\n", ppc_abi_fp(val));
4196 			break;
4197 		case Tag_GNU_Power_ABI_Vector:
4198 			val = _decode_uleb128(&p);
4199 			printf("  Tag_GNU_Power_ABI_Vector: %s\n",
4200 			    ppc_abi_vector(val));
4201 			break;
4202 		case 32:	/* Tag_compatibility */
4203 			p = dump_compatibility_tag(p);
4204 			break;
4205 		default:
4206 			p = dump_unknown_tag(tag, p);
4207 			break;
4208 		}
4209 	}
4210 }
4211 
4212 static void
4213 dump_attributes(struct readelf *re)
4214 {
4215 	struct section *s;
4216 	Elf_Data *d;
4217 	uint8_t *p, *sp;
4218 	size_t len, seclen, nlen, sublen;
4219 	uint64_t val;
4220 	int tag, i, elferr;
4221 
4222 	for (i = 0; (size_t) i < re->shnum; i++) {
4223 		s = &re->sl[i];
4224 		if (s->type != SHT_GNU_ATTRIBUTES &&
4225 		    (re->ehdr.e_machine != EM_ARM || s->type != SHT_LOPROC + 3))
4226 			continue;
4227 		(void) elf_errno();
4228 		if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
4229 			elferr = elf_errno();
4230 			if (elferr != 0)
4231 				warnx("elf_rawdata failed: %s",
4232 				    elf_errmsg(elferr));
4233 			continue;
4234 		}
4235 		if (d->d_size <= 0)
4236 			continue;
4237 		p = d->d_buf;
4238 		if (*p != 'A') {
4239 			printf("Unknown Attribute Section Format: %c\n",
4240 			    (char) *p);
4241 			continue;
4242 		}
4243 		len = d->d_size - 1;
4244 		p++;
4245 		while (len > 0) {
4246 			if (len < 4) {
4247 				warnx("truncated attribute section length");
4248 				break;
4249 			}
4250 			seclen = re->dw_decode(&p, 4);
4251 			if (seclen > len) {
4252 				warnx("invalid attribute section length");
4253 				break;
4254 			}
4255 			len -= seclen;
4256 			nlen = strlen((char *) p) + 1;
4257 			if (nlen + 4 > seclen) {
4258 				warnx("invalid attribute section name");
4259 				break;
4260 			}
4261 			printf("Attribute Section: %s\n", (char *) p);
4262 			p += nlen;
4263 			seclen -= nlen + 4;
4264 			while (seclen > 0) {
4265 				sp = p;
4266 				tag = *p++;
4267 				sublen = re->dw_decode(&p, 4);
4268 				if (sublen > seclen) {
4269 					warnx("invalid attribute sub-section"
4270 					    " length");
4271 					break;
4272 				}
4273 				seclen -= sublen;
4274 				printf("%s", top_tag(tag));
4275 				if (tag == 2 || tag == 3) {
4276 					putchar(':');
4277 					for (;;) {
4278 						val = _decode_uleb128(&p);
4279 						if (val == 0)
4280 							break;
4281 						printf(" %ju", (uintmax_t) val);
4282 					}
4283 				}
4284 				putchar('\n');
4285 				if (re->ehdr.e_machine == EM_ARM &&
4286 				    s->type == SHT_LOPROC + 3)
4287 					dump_arm_attributes(re, p, sp + sublen);
4288 				else if (re->ehdr.e_machine == EM_MIPS ||
4289 				    re->ehdr.e_machine == EM_MIPS_RS3_LE)
4290 					dump_mips_attributes(re, p,
4291 					    sp + sublen);
4292 				else if (re->ehdr.e_machine == EM_PPC)
4293 					dump_ppc_attributes(p, sp + sublen);
4294 				p = sp + sublen;
4295 			}
4296 		}
4297 	}
4298 }
4299 
4300 static void
4301 dump_mips_specific_info(struct readelf *re)
4302 {
4303 	struct section *s;
4304 	int i, options_found;
4305 
4306 	options_found = 0;
4307 	s = NULL;
4308 	for (i = 0; (size_t) i < re->shnum; i++) {
4309 		s = &re->sl[i];
4310 		if (s->name != NULL && (!strcmp(s->name, ".MIPS.options") ||
4311 		    (s->type == SHT_MIPS_OPTIONS))) {
4312 			dump_mips_options(re, s);
4313 			options_found = 1;
4314 		}
4315 	}
4316 
4317 	/*
4318 	 * According to SGI mips64 spec, .reginfo should be ignored if
4319 	 * .MIPS.options section is present.
4320 	 */
4321 	if (!options_found) {
4322 		for (i = 0; (size_t) i < re->shnum; i++) {
4323 			s = &re->sl[i];
4324 			if (s->name != NULL && (!strcmp(s->name, ".reginfo") ||
4325 			    (s->type == SHT_MIPS_REGINFO)))
4326 				dump_mips_reginfo(re, s);
4327 		}
4328 	}
4329 }
4330 
4331 static void
4332 dump_mips_reginfo(struct readelf *re, struct section *s)
4333 {
4334 	Elf_Data *d;
4335 	int elferr;
4336 
4337 	(void) elf_errno();
4338 	if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
4339 		elferr = elf_errno();
4340 		if (elferr != 0)
4341 			warnx("elf_rawdata failed: %s",
4342 			    elf_errmsg(elferr));
4343 		return;
4344 	}
4345 	if (d->d_size <= 0)
4346 		return;
4347 
4348 	printf("\nSection '%s' contains %ju entries:\n", s->name,
4349 	    s->sz / s->entsize);
4350 	dump_mips_odk_reginfo(re, d->d_buf, d->d_size);
4351 }
4352 
4353 static void
4354 dump_mips_options(struct readelf *re, struct section *s)
4355 {
4356 	Elf_Data *d;
4357 	uint32_t info;
4358 	uint16_t sndx;
4359 	uint8_t *p, *pe;
4360 	uint8_t kind, size;
4361 	int elferr;
4362 
4363 	(void) elf_errno();
4364 	if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
4365 		elferr = elf_errno();
4366 		if (elferr != 0)
4367 			warnx("elf_rawdata failed: %s",
4368 			    elf_errmsg(elferr));
4369 		return;
4370 	}
4371 	if (d->d_size == 0)
4372 		return;
4373 
4374 	printf("\nSection %s contains:\n", s->name);
4375 	p = d->d_buf;
4376 	pe = p + d->d_size;
4377 	while (p < pe) {
4378 		kind = re->dw_decode(&p, 1);
4379 		size = re->dw_decode(&p, 1);
4380 		sndx = re->dw_decode(&p, 2);
4381 		info = re->dw_decode(&p, 4);
4382 		switch (kind) {
4383 		case ODK_REGINFO:
4384 			dump_mips_odk_reginfo(re, p, size - 8);
4385 			break;
4386 		case ODK_EXCEPTIONS:
4387 			printf(" EXCEPTIONS FPU_MIN: %#x\n",
4388 			    info & OEX_FPU_MIN);
4389 			printf("%11.11s FPU_MAX: %#x\n", "",
4390 			    info & OEX_FPU_MAX);
4391 			dump_mips_option_flags("", mips_exceptions_option,
4392 			    info);
4393 			break;
4394 		case ODK_PAD:
4395 			printf(" %-10.10s section: %ju\n", "OPAD",
4396 			    (uintmax_t) sndx);
4397 			dump_mips_option_flags("", mips_pad_option, info);
4398 			break;
4399 		case ODK_HWPATCH:
4400 			dump_mips_option_flags("HWPATCH", mips_hwpatch_option,
4401 			    info);
4402 			break;
4403 		case ODK_HWAND:
4404 			dump_mips_option_flags("HWAND", mips_hwa_option, info);
4405 			break;
4406 		case ODK_HWOR:
4407 			dump_mips_option_flags("HWOR", mips_hwo_option, info);
4408 			break;
4409 		case ODK_FILL:
4410 			printf(" %-10.10s %#jx\n", "FILL", (uintmax_t) info);
4411 			break;
4412 		case ODK_TAGS:
4413 			printf(" %-10.10s\n", "TAGS");
4414 			break;
4415 		case ODK_GP_GROUP:
4416 			printf(" %-10.10s GP group number: %#x\n", "GP_GROUP",
4417 			    info & 0xFFFF);
4418 			if (info & 0x10000)
4419 				printf(" %-10.10s GP group is "
4420 				    "self-contained\n", "");
4421 			break;
4422 		case ODK_IDENT:
4423 			printf(" %-10.10s default GP group number: %#x\n",
4424 			    "IDENT", info & 0xFFFF);
4425 			if (info & 0x10000)
4426 				printf(" %-10.10s default GP group is "
4427 				    "self-contained\n", "");
4428 			break;
4429 		case ODK_PAGESIZE:
4430 			printf(" %-10.10s\n", "PAGESIZE");
4431 			break;
4432 		default:
4433 			break;
4434 		}
4435 		p += size - 8;
4436 	}
4437 }
4438 
4439 static void
4440 dump_mips_option_flags(const char *name, struct mips_option *opt, uint64_t info)
4441 {
4442 	int first;
4443 
4444 	first = 1;
4445 	for (; opt->desc != NULL; opt++) {
4446 		if (info & opt->flag) {
4447 			printf(" %-10.10s %s\n", first ? name : "",
4448 			    opt->desc);
4449 			first = 0;
4450 		}
4451 	}
4452 }
4453 
4454 static void
4455 dump_mips_odk_reginfo(struct readelf *re, uint8_t *p, size_t sz)
4456 {
4457 	uint32_t ri_gprmask;
4458 	uint32_t ri_cprmask[4];
4459 	uint64_t ri_gp_value;
4460 	uint8_t *pe;
4461 	int i;
4462 
4463 	pe = p + sz;
4464 	while (p < pe) {
4465 		ri_gprmask = re->dw_decode(&p, 4);
4466 		/* Skip ri_pad padding field for mips64. */
4467 		if (re->ec == ELFCLASS64)
4468 			re->dw_decode(&p, 4);
4469 		for (i = 0; i < 4; i++)
4470 			ri_cprmask[i] = re->dw_decode(&p, 4);
4471 		if (re->ec == ELFCLASS32)
4472 			ri_gp_value = re->dw_decode(&p, 4);
4473 		else
4474 			ri_gp_value = re->dw_decode(&p, 8);
4475 		printf(" %s    ", option_kind(ODK_REGINFO));
4476 		printf("ri_gprmask:    0x%08jx\n", (uintmax_t) ri_gprmask);
4477 		for (i = 0; i < 4; i++)
4478 			printf("%11.11s ri_cprmask[%d]: 0x%08jx\n", "", i,
4479 			    (uintmax_t) ri_cprmask[i]);
4480 		printf("%12.12s", "");
4481 		printf("ri_gp_value:   %#jx\n", (uintmax_t) ri_gp_value);
4482 	}
4483 }
4484 
4485 static void
4486 dump_arch_specific_info(struct readelf *re)
4487 {
4488 
4489 	dump_liblist(re);
4490 	dump_attributes(re);
4491 
4492 	switch (re->ehdr.e_machine) {
4493 	case EM_MIPS:
4494 	case EM_MIPS_RS3_LE:
4495 		dump_mips_specific_info(re);
4496 	default:
4497 		break;
4498 	}
4499 }
4500 
4501 static const char *
4502 dwarf_regname(struct readelf *re, unsigned int num)
4503 {
4504 	static char rx[32];
4505 	const char *rn;
4506 
4507 	if ((rn = dwarf_reg(re->ehdr.e_machine, num)) != NULL)
4508 		return (rn);
4509 
4510 	snprintf(rx, sizeof(rx), "r%u", num);
4511 
4512 	return (rx);
4513 }
4514 
4515 static void
4516 dump_dwarf_line(struct readelf *re)
4517 {
4518 	struct section *s;
4519 	Dwarf_Die die;
4520 	Dwarf_Error de;
4521 	Dwarf_Half tag, version, pointer_size;
4522 	Dwarf_Unsigned offset, endoff, length, hdrlen, dirndx, mtime, fsize;
4523 	Dwarf_Small minlen, defstmt, lrange, opbase, oplen;
4524 	Elf_Data *d;
4525 	char *pn;
4526 	uint64_t address, file, line, column, isa, opsize, udelta;
4527 	int64_t sdelta;
4528 	uint8_t *p, *pe;
4529 	int8_t lbase;
4530 	int i, is_stmt, dwarf_size, elferr, ret;
4531 
4532 	printf("\nDump of debug contents of section .debug_line:\n");
4533 
4534 	s = NULL;
4535 	for (i = 0; (size_t) i < re->shnum; i++) {
4536 		s = &re->sl[i];
4537 		if (s->name != NULL && !strcmp(s->name, ".debug_line"))
4538 			break;
4539 	}
4540 	if ((size_t) i >= re->shnum)
4541 		return;
4542 
4543 	(void) elf_errno();
4544 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
4545 		elferr = elf_errno();
4546 		if (elferr != 0)
4547 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
4548 		return;
4549 	}
4550 	if (d->d_size <= 0)
4551 		return;
4552 
4553 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
4554 	    NULL, &de)) ==  DW_DLV_OK) {
4555 		die = NULL;
4556 		while (dwarf_siblingof(re->dbg, die, &die, &de) == DW_DLV_OK) {
4557 			if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
4558 				warnx("dwarf_tag failed: %s",
4559 				    dwarf_errmsg(de));
4560 				return;
4561 			}
4562 			/* XXX: What about DW_TAG_partial_unit? */
4563 			if (tag == DW_TAG_compile_unit)
4564 				break;
4565 		}
4566 		if (die == NULL) {
4567 			warnx("could not find DW_TAG_compile_unit die");
4568 			return;
4569 		}
4570 		if (dwarf_attrval_unsigned(die, DW_AT_stmt_list, &offset,
4571 		    &de) != DW_DLV_OK)
4572 			continue;
4573 
4574 		length = re->dw_read(d, &offset, 4);
4575 		if (length == 0xffffffff) {
4576 			dwarf_size = 8;
4577 			length = re->dw_read(d, &offset, 8);
4578 		} else
4579 			dwarf_size = 4;
4580 
4581 		if (length > d->d_size - offset) {
4582 			warnx("invalid .dwarf_line section");
4583 			continue;
4584 		}
4585 
4586 		endoff = offset + length;
4587 		version = re->dw_read(d, &offset, 2);
4588 		hdrlen = re->dw_read(d, &offset, dwarf_size);
4589 		minlen = re->dw_read(d, &offset, 1);
4590 		defstmt = re->dw_read(d, &offset, 1);
4591 		lbase = re->dw_read(d, &offset, 1);
4592 		lrange = re->dw_read(d, &offset, 1);
4593 		opbase = re->dw_read(d, &offset, 1);
4594 
4595 		printf("\n");
4596 		printf("  Length:\t\t\t%ju\n", (uintmax_t) length);
4597 		printf("  DWARF version:\t\t%u\n", version);
4598 		printf("  Prologue Length:\t\t%ju\n", (uintmax_t) hdrlen);
4599 		printf("  Minimum Instruction Length:\t%u\n", minlen);
4600 		printf("  Initial value of 'is_stmt':\t%u\n", defstmt);
4601 		printf("  Line Base:\t\t\t%d\n", lbase);
4602 		printf("  Line Range:\t\t\t%u\n", lrange);
4603 		printf("  Opcode Base:\t\t\t%u\n", opbase);
4604 		(void) dwarf_get_address_size(re->dbg, &pointer_size, &de);
4605 		printf("  (Pointer size:\t\t%u)\n", pointer_size);
4606 
4607 		printf("\n");
4608 		printf(" Opcodes:\n");
4609 		for (i = 1; i < opbase; i++) {
4610 			oplen = re->dw_read(d, &offset, 1);
4611 			printf("  Opcode %d has %u args\n", i, oplen);
4612 		}
4613 
4614 		printf("\n");
4615 		printf(" The Directory Table:\n");
4616 		p = (uint8_t *) d->d_buf + offset;
4617 		while (*p != '\0') {
4618 			printf("  %s\n", (char *) p);
4619 			p += strlen((char *) p) + 1;
4620 		}
4621 
4622 		p++;
4623 		printf("\n");
4624 		printf(" The File Name Table:\n");
4625 		printf("  Entry\tDir\tTime\tSize\tName\n");
4626 		i = 0;
4627 		while (*p != '\0') {
4628 			i++;
4629 			pn = (char *) p;
4630 			p += strlen(pn) + 1;
4631 			dirndx = _decode_uleb128(&p);
4632 			mtime = _decode_uleb128(&p);
4633 			fsize = _decode_uleb128(&p);
4634 			printf("  %d\t%ju\t%ju\t%ju\t%s\n", i,
4635 			    (uintmax_t) dirndx, (uintmax_t) mtime,
4636 			    (uintmax_t) fsize, pn);
4637 		}
4638 
4639 #define	RESET_REGISTERS						\
4640 	do {							\
4641 		address	       = 0;				\
4642 		file	       = 1;				\
4643 		line	       = 1;				\
4644 		column	       = 0;				\
4645 		is_stmt	       = defstmt;			\
4646 	} while(0)
4647 
4648 #define	LINE(x) (lbase + (((x) - opbase) % lrange))
4649 #define	ADDRESS(x) ((((x) - opbase) / lrange) * minlen)
4650 
4651 		p++;
4652 		pe = (uint8_t *) d->d_buf + endoff;
4653 		printf("\n");
4654 		printf(" Line Number Statements:\n");
4655 
4656 		RESET_REGISTERS;
4657 
4658 		while (p < pe) {
4659 
4660 			if (*p == 0) {
4661 				/*
4662 				 * Extended Opcodes.
4663 				 */
4664 				p++;
4665 				opsize = _decode_uleb128(&p);
4666 				printf("  Extended opcode %u: ", *p);
4667 				switch (*p) {
4668 				case DW_LNE_end_sequence:
4669 					p++;
4670 					RESET_REGISTERS;
4671 					printf("End of Sequence\n");
4672 					break;
4673 				case DW_LNE_set_address:
4674 					p++;
4675 					address = re->dw_decode(&p,
4676 					    pointer_size);
4677 					printf("set Address to %#jx\n",
4678 					    (uintmax_t) address);
4679 					break;
4680 				case DW_LNE_define_file:
4681 					p++;
4682 					pn = (char *) p;
4683 					p += strlen(pn) + 1;
4684 					dirndx = _decode_uleb128(&p);
4685 					mtime = _decode_uleb128(&p);
4686 					fsize = _decode_uleb128(&p);
4687 					printf("define new file: %s\n", pn);
4688 					break;
4689 				default:
4690 					/* Unrecognized extened opcodes. */
4691 					p += opsize;
4692 					printf("unknown opcode\n");
4693 				}
4694 			} else if (*p > 0 && *p < opbase) {
4695 				/*
4696 				 * Standard Opcodes.
4697 				 */
4698 				switch(*p++) {
4699 				case DW_LNS_copy:
4700 					printf("  Copy\n");
4701 					break;
4702 				case DW_LNS_advance_pc:
4703 					udelta = _decode_uleb128(&p) *
4704 					    minlen;
4705 					address += udelta;
4706 					printf("  Advance PC by %ju to %#jx\n",
4707 					    (uintmax_t) udelta,
4708 					    (uintmax_t) address);
4709 					break;
4710 				case DW_LNS_advance_line:
4711 					sdelta = _decode_sleb128(&p);
4712 					line += sdelta;
4713 					printf("  Advance Line by %jd to %ju\n",
4714 					    (intmax_t) sdelta,
4715 					    (uintmax_t) line);
4716 					break;
4717 				case DW_LNS_set_file:
4718 					file = _decode_uleb128(&p);
4719 					printf("  Set File to %ju\n",
4720 					    (uintmax_t) file);
4721 					break;
4722 				case DW_LNS_set_column:
4723 					column = _decode_uleb128(&p);
4724 					printf("  Set Column to %ju\n",
4725 					    (uintmax_t) column);
4726 					break;
4727 				case DW_LNS_negate_stmt:
4728 					is_stmt = !is_stmt;
4729 					printf("  Set is_stmt to %d\n", is_stmt);
4730 					break;
4731 				case DW_LNS_set_basic_block:
4732 					printf("  Set basic block flag\n");
4733 					break;
4734 				case DW_LNS_const_add_pc:
4735 					address += ADDRESS(255);
4736 					printf("  Advance PC by constant %ju"
4737 					    " to %#jx\n",
4738 					    (uintmax_t) ADDRESS(255),
4739 					    (uintmax_t) address);
4740 					break;
4741 				case DW_LNS_fixed_advance_pc:
4742 					udelta = re->dw_decode(&p, 2);
4743 					address += udelta;
4744 					printf("  Advance PC by fixed value "
4745 					    "%ju to %#jx\n",
4746 					    (uintmax_t) udelta,
4747 					    (uintmax_t) address);
4748 					break;
4749 				case DW_LNS_set_prologue_end:
4750 					printf("  Set prologue end flag\n");
4751 					break;
4752 				case DW_LNS_set_epilogue_begin:
4753 					printf("  Set epilogue begin flag\n");
4754 					break;
4755 				case DW_LNS_set_isa:
4756 					isa = _decode_uleb128(&p);
4757 					printf("  Set isa to %ju\n", isa);
4758 					break;
4759 				default:
4760 					/* Unrecognized extended opcodes. */
4761 					printf("  Unknown extended opcode %u\n",
4762 					    *(p - 1));
4763 					break;
4764 				}
4765 
4766 			} else {
4767 				/*
4768 				 * Special Opcodes.
4769 				 */
4770 				line += LINE(*p);
4771 				address += ADDRESS(*p);
4772 				printf("  Special opcode %u: advance Address "
4773 				    "by %ju to %#jx and Line by %jd to %ju\n",
4774 				    *p - opbase, (uintmax_t) ADDRESS(*p),
4775 				    (uintmax_t) address, (intmax_t) LINE(*p),
4776 				    (uintmax_t) line);
4777 				p++;
4778 			}
4779 
4780 
4781 		}
4782 	}
4783 	if (ret == DW_DLV_ERROR)
4784 		warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
4785 
4786 #undef	RESET_REGISTERS
4787 #undef	LINE
4788 #undef	ADDRESS
4789 }
4790 
4791 static void
4792 dump_dwarf_line_decoded(struct readelf *re)
4793 {
4794 	Dwarf_Die die;
4795 	Dwarf_Line *linebuf, ln;
4796 	Dwarf_Addr lineaddr;
4797 	Dwarf_Signed linecount, srccount;
4798 	Dwarf_Unsigned lineno, fn;
4799 	Dwarf_Error de;
4800 	const char *dir, *file;
4801 	char **srcfiles;
4802 	int i, ret;
4803 
4804 	printf("Decoded dump of debug contents of section .debug_line:\n\n");
4805 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
4806 	    NULL, &de)) == DW_DLV_OK) {
4807 		if (dwarf_siblingof(re->dbg, NULL, &die, &de) != DW_DLV_OK)
4808 			continue;
4809 		if (dwarf_attrval_string(die, DW_AT_name, &file, &de) !=
4810 		    DW_DLV_OK)
4811 			file = NULL;
4812 		if (dwarf_attrval_string(die, DW_AT_comp_dir, &dir, &de) !=
4813 		    DW_DLV_OK)
4814 			dir = NULL;
4815 		printf("CU: ");
4816 		if (dir && file)
4817 			printf("%s/", dir);
4818 		if (file)
4819 			printf("%s", file);
4820 		putchar('\n');
4821 		printf("%-37s %11s   %s\n", "Filename", "Line Number",
4822 		    "Starting Address");
4823 		if (dwarf_srclines(die, &linebuf, &linecount, &de) != DW_DLV_OK)
4824 			continue;
4825 		if (dwarf_srcfiles(die, &srcfiles, &srccount, &de) != DW_DLV_OK)
4826 			continue;
4827 		for (i = 0; i < linecount; i++) {
4828 			ln = linebuf[i];
4829 			if (dwarf_line_srcfileno(ln, &fn, &de) != DW_DLV_OK)
4830 				continue;
4831 			if (dwarf_lineno(ln, &lineno, &de) != DW_DLV_OK)
4832 				continue;
4833 			if (dwarf_lineaddr(ln, &lineaddr, &de) != DW_DLV_OK)
4834 				continue;
4835 			printf("%-37s %11ju %#18jx\n",
4836 			    basename(srcfiles[fn - 1]), (uintmax_t) lineno,
4837 			    (uintmax_t) lineaddr);
4838 		}
4839 		putchar('\n');
4840 	}
4841 }
4842 
4843 static void
4844 dump_dwarf_die(struct readelf *re, Dwarf_Die die, int level)
4845 {
4846 	Dwarf_Attribute *attr_list;
4847 	Dwarf_Die ret_die;
4848 	Dwarf_Off dieoff, cuoff, culen, attroff;
4849 	Dwarf_Unsigned ate, lang, v_udata, v_sig;
4850 	Dwarf_Signed attr_count, v_sdata;
4851 	Dwarf_Off v_off;
4852 	Dwarf_Addr v_addr;
4853 	Dwarf_Half tag, attr, form;
4854 	Dwarf_Block *v_block;
4855 	Dwarf_Bool v_bool, is_info;
4856 	Dwarf_Sig8 v_sig8;
4857 	Dwarf_Error de;
4858 	Dwarf_Ptr v_expr;
4859 	const char *tag_str, *attr_str, *ate_str, *lang_str;
4860 	char unk_tag[32], unk_attr[32];
4861 	char *v_str;
4862 	uint8_t *b, *p;
4863 	int i, j, abc, ret;
4864 
4865 	if (dwarf_dieoffset(die, &dieoff, &de) != DW_DLV_OK) {
4866 		warnx("dwarf_dieoffset failed: %s", dwarf_errmsg(de));
4867 		goto cont_search;
4868 	}
4869 
4870 	printf(" <%d><%jx>: ", level, (uintmax_t) dieoff);
4871 
4872 	if (dwarf_die_CU_offset_range(die, &cuoff, &culen, &de) != DW_DLV_OK) {
4873 		warnx("dwarf_die_CU_offset_range failed: %s",
4874 		      dwarf_errmsg(de));
4875 		cuoff = 0;
4876 	}
4877 
4878 	abc = dwarf_die_abbrev_code(die);
4879 	if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
4880 		warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
4881 		goto cont_search;
4882 	}
4883 	if (dwarf_get_TAG_name(tag, &tag_str) != DW_DLV_OK) {
4884 		snprintf(unk_tag, sizeof(unk_tag), "[Unknown Tag: %#x]", tag);
4885 		tag_str = unk_tag;
4886 	}
4887 
4888 	printf("Abbrev Number: %d (%s)\n", abc, tag_str);
4889 
4890 	if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
4891 	    DW_DLV_OK) {
4892 		if (ret == DW_DLV_ERROR)
4893 			warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
4894 		goto cont_search;
4895 	}
4896 
4897 	for (i = 0; i < attr_count; i++) {
4898 		if (dwarf_whatform(attr_list[i], &form, &de) != DW_DLV_OK) {
4899 			warnx("dwarf_whatform failed: %s", dwarf_errmsg(de));
4900 			continue;
4901 		}
4902 		if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
4903 			warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
4904 			continue;
4905 		}
4906 		if (dwarf_get_AT_name(attr, &attr_str) != DW_DLV_OK) {
4907 			snprintf(unk_attr, sizeof(unk_attr),
4908 			    "[Unknown AT: %#x]", attr);
4909 			attr_str = unk_attr;
4910 		}
4911 		if (dwarf_attroffset(attr_list[i], &attroff, &de) !=
4912 		    DW_DLV_OK) {
4913 			warnx("dwarf_attroffset failed: %s", dwarf_errmsg(de));
4914 			attroff = 0;
4915 		}
4916 		printf("    <%jx>   %-18s: ", (uintmax_t) attroff, attr_str);
4917 		switch (form) {
4918 		case DW_FORM_ref_addr:
4919 		case DW_FORM_sec_offset:
4920 			if (dwarf_global_formref(attr_list[i], &v_off, &de) !=
4921 			    DW_DLV_OK) {
4922 				warnx("dwarf_global_formref failed: %s",
4923 				    dwarf_errmsg(de));
4924 				continue;
4925 			}
4926 			if (form == DW_FORM_ref_addr)
4927 				printf("<0x%jx>", (uintmax_t) v_off);
4928 			else
4929 				printf("0x%jx", (uintmax_t) v_off);
4930 			break;
4931 
4932 		case DW_FORM_ref1:
4933 		case DW_FORM_ref2:
4934 		case DW_FORM_ref4:
4935 		case DW_FORM_ref8:
4936 		case DW_FORM_ref_udata:
4937 			if (dwarf_formref(attr_list[i], &v_off, &de) !=
4938 			    DW_DLV_OK) {
4939 				warnx("dwarf_formref failed: %s",
4940 				    dwarf_errmsg(de));
4941 				continue;
4942 			}
4943 			v_off += cuoff;
4944 			printf("<0x%jx>", (uintmax_t) v_off);
4945 			break;
4946 
4947 		case DW_FORM_addr:
4948 			if (dwarf_formaddr(attr_list[i], &v_addr, &de) !=
4949 			    DW_DLV_OK) {
4950 				warnx("dwarf_formaddr failed: %s",
4951 				    dwarf_errmsg(de));
4952 				continue;
4953 			}
4954 			printf("%#jx", (uintmax_t) v_addr);
4955 			break;
4956 
4957 		case DW_FORM_data1:
4958 		case DW_FORM_data2:
4959 		case DW_FORM_data4:
4960 		case DW_FORM_data8:
4961 		case DW_FORM_udata:
4962 			if (dwarf_formudata(attr_list[i], &v_udata, &de) !=
4963 			    DW_DLV_OK) {
4964 				warnx("dwarf_formudata failed: %s",
4965 				    dwarf_errmsg(de));
4966 				continue;
4967 			}
4968 			if (attr == DW_AT_high_pc)
4969 				printf("0x%jx", (uintmax_t) v_udata);
4970 			else
4971 				printf("%ju", (uintmax_t) v_udata);
4972 			break;
4973 
4974 		case DW_FORM_sdata:
4975 			if (dwarf_formsdata(attr_list[i], &v_sdata, &de) !=
4976 			    DW_DLV_OK) {
4977 				warnx("dwarf_formudata failed: %s",
4978 				    dwarf_errmsg(de));
4979 				continue;
4980 			}
4981 			printf("%jd", (intmax_t) v_sdata);
4982 			break;
4983 
4984 		case DW_FORM_flag:
4985 			if (dwarf_formflag(attr_list[i], &v_bool, &de) !=
4986 			    DW_DLV_OK) {
4987 				warnx("dwarf_formflag failed: %s",
4988 				    dwarf_errmsg(de));
4989 				continue;
4990 			}
4991 			printf("%jd", (intmax_t) v_bool);
4992 			break;
4993 
4994 		case DW_FORM_flag_present:
4995 			putchar('1');
4996 			break;
4997 
4998 		case DW_FORM_string:
4999 		case DW_FORM_strp:
5000 			if (dwarf_formstring(attr_list[i], &v_str, &de) !=
5001 			    DW_DLV_OK) {
5002 				warnx("dwarf_formstring failed: %s",
5003 				    dwarf_errmsg(de));
5004 				continue;
5005 			}
5006 			if (form == DW_FORM_string)
5007 				printf("%s", v_str);
5008 			else
5009 				printf("(indirect string) %s", v_str);
5010 			break;
5011 
5012 		case DW_FORM_block:
5013 		case DW_FORM_block1:
5014 		case DW_FORM_block2:
5015 		case DW_FORM_block4:
5016 			if (dwarf_formblock(attr_list[i], &v_block, &de) !=
5017 			    DW_DLV_OK) {
5018 				warnx("dwarf_formblock failed: %s",
5019 				    dwarf_errmsg(de));
5020 				continue;
5021 			}
5022 			printf("%ju byte block:", (uintmax_t) v_block->bl_len);
5023 			b = v_block->bl_data;
5024 			for (j = 0; (Dwarf_Unsigned) j < v_block->bl_len; j++)
5025 				printf(" %x", b[j]);
5026 			printf("\t(");
5027 			dump_dwarf_block(re, v_block->bl_data, v_block->bl_len);
5028 			putchar(')');
5029 			break;
5030 
5031 		case DW_FORM_exprloc:
5032 			if (dwarf_formexprloc(attr_list[i], &v_udata, &v_expr,
5033 			    &de) != DW_DLV_OK) {
5034 				warnx("dwarf_formexprloc failed: %s",
5035 				    dwarf_errmsg(de));
5036 				continue;
5037 			}
5038 			printf("%ju byte block:", (uintmax_t) v_udata);
5039 			b = v_expr;
5040 			for (j = 0; (Dwarf_Unsigned) j < v_udata; j++)
5041 				printf(" %x", b[j]);
5042 			printf("\t(");
5043 			dump_dwarf_block(re, v_expr, v_udata);
5044 			putchar(')');
5045 			break;
5046 
5047 		case DW_FORM_ref_sig8:
5048 			if (dwarf_formsig8(attr_list[i], &v_sig8, &de) !=
5049 			    DW_DLV_OK) {
5050 				warnx("dwarf_formsig8 failed: %s",
5051 				    dwarf_errmsg(de));
5052 				continue;
5053 			}
5054 			p = (uint8_t *)(uintptr_t) &v_sig8.signature[0];
5055 			v_sig = re->dw_decode(&p, 8);
5056 			printf("signature: 0x%jx", (uintmax_t) v_sig);
5057 		}
5058 		switch (attr) {
5059 		case DW_AT_encoding:
5060 			if (dwarf_attrval_unsigned(die, attr, &ate, &de) !=
5061 			    DW_DLV_OK)
5062 				break;
5063 			if (dwarf_get_ATE_name(ate, &ate_str) != DW_DLV_OK)
5064 				ate_str = "DW_ATE_UNKNOWN";
5065 			printf("\t(%s)", &ate_str[strlen("DW_ATE_")]);
5066 			break;
5067 
5068 		case DW_AT_language:
5069 			if (dwarf_attrval_unsigned(die, attr, &lang, &de) !=
5070 			    DW_DLV_OK)
5071 				break;
5072 			if (dwarf_get_LANG_name(lang, &lang_str) != DW_DLV_OK)
5073 				break;
5074 			printf("\t(%s)", &lang_str[strlen("DW_LANG_")]);
5075 			break;
5076 
5077 		case DW_AT_location:
5078 		case DW_AT_string_length:
5079 		case DW_AT_return_addr:
5080 		case DW_AT_data_member_location:
5081 		case DW_AT_frame_base:
5082 		case DW_AT_segment:
5083 		case DW_AT_static_link:
5084 		case DW_AT_use_location:
5085 		case DW_AT_vtable_elem_location:
5086 			switch (form) {
5087 			case DW_FORM_data4:
5088 			case DW_FORM_data8:
5089 			case DW_FORM_sec_offset:
5090 				printf("\t(location list)");
5091 				break;
5092 			default:
5093 				break;
5094 			}
5095 
5096 		default:
5097 			break;
5098 		}
5099 		putchar('\n');
5100 	}
5101 
5102 
5103 cont_search:
5104 	/* Search children. */
5105 	ret = dwarf_child(die, &ret_die, &de);
5106 	if (ret == DW_DLV_ERROR)
5107 		warnx("dwarf_child: %s", dwarf_errmsg(de));
5108 	else if (ret == DW_DLV_OK)
5109 		dump_dwarf_die(re, ret_die, level + 1);
5110 
5111 	/* Search sibling. */
5112 	is_info = dwarf_get_die_infotypes_flag(die);
5113 	ret = dwarf_siblingof_b(re->dbg, die, &ret_die, is_info, &de);
5114 	if (ret == DW_DLV_ERROR)
5115 		warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
5116 	else if (ret == DW_DLV_OK)
5117 		dump_dwarf_die(re, ret_die, level);
5118 
5119 	dwarf_dealloc(re->dbg, die, DW_DLA_DIE);
5120 }
5121 
5122 static void
5123 set_cu_context(struct readelf *re, Dwarf_Half psize, Dwarf_Half osize,
5124     Dwarf_Half ver)
5125 {
5126 
5127 	re->cu_psize = psize;
5128 	re->cu_osize = osize;
5129 	re->cu_ver = ver;
5130 }
5131 
5132 static void
5133 dump_dwarf_info(struct readelf *re, Dwarf_Bool is_info)
5134 {
5135 	struct section *s;
5136 	Dwarf_Die die;
5137 	Dwarf_Error de;
5138 	Dwarf_Half tag, version, pointer_size, off_size;
5139 	Dwarf_Off cu_offset, cu_length;
5140 	Dwarf_Off aboff;
5141 	Dwarf_Unsigned typeoff;
5142 	Dwarf_Sig8 sig8;
5143 	Dwarf_Unsigned sig;
5144 	uint8_t *p;
5145 	const char *sn;
5146 	int i, ret;
5147 
5148 	sn = is_info ? ".debug_info" : ".debug_types";
5149 
5150 	s = NULL;
5151 	for (i = 0; (size_t) i < re->shnum; i++) {
5152 		s = &re->sl[i];
5153 		if (s->name != NULL && !strcmp(s->name, sn))
5154 			break;
5155 	}
5156 	if ((size_t) i >= re->shnum)
5157 		return;
5158 
5159 	do {
5160 		printf("\nDump of debug contents of section %s:\n", sn);
5161 
5162 		while ((ret = dwarf_next_cu_header_c(re->dbg, is_info, NULL,
5163 		    &version, &aboff, &pointer_size, &off_size, NULL, &sig8,
5164 		    &typeoff, NULL, &de)) == DW_DLV_OK) {
5165 			set_cu_context(re, pointer_size, off_size, version);
5166 			die = NULL;
5167 			while (dwarf_siblingof_b(re->dbg, die, &die, is_info,
5168 			    &de) == DW_DLV_OK) {
5169 				if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
5170 					warnx("dwarf_tag failed: %s",
5171 					    dwarf_errmsg(de));
5172 					continue;
5173 				}
5174 				/* XXX: What about DW_TAG_partial_unit? */
5175 				if ((is_info && tag == DW_TAG_compile_unit) ||
5176 				    (!is_info && tag == DW_TAG_type_unit))
5177 					break;
5178 			}
5179 			if (die == NULL && is_info) {
5180 				warnx("could not find DW_TAG_compile_unit "
5181 				    "die");
5182 				continue;
5183 			} else if (die == NULL && !is_info) {
5184 				warnx("could not find DW_TAG_type_unit die");
5185 				continue;
5186 			}
5187 
5188 			if (dwarf_die_CU_offset_range(die, &cu_offset,
5189 			    &cu_length, &de) != DW_DLV_OK) {
5190 				warnx("dwarf_die_CU_offset failed: %s",
5191 				    dwarf_errmsg(de));
5192 				continue;
5193 			}
5194 
5195 			cu_length -= off_size == 4 ? 4 : 12;
5196 
5197 			sig = 0;
5198 			if (!is_info) {
5199 				p = (uint8_t *)(uintptr_t) &sig8.signature[0];
5200 				sig = re->dw_decode(&p, 8);
5201 			}
5202 
5203 			printf("\n  Type Unit @ offset 0x%jx:\n",
5204 			    (uintmax_t) cu_offset);
5205 			printf("    Length:\t\t%#jx (%d-bit)\n",
5206 			    (uintmax_t) cu_length, off_size == 4 ? 32 : 64);
5207 			printf("    Version:\t\t%u\n", version);
5208 			printf("    Abbrev Offset:\t0x%jx\n",
5209 			    (uintmax_t) aboff);
5210 			printf("    Pointer Size:\t%u\n", pointer_size);
5211 			if (!is_info) {
5212 				printf("    Signature:\t\t0x%016jx\n",
5213 				    (uintmax_t) sig);
5214 				printf("    Type Offset:\t0x%jx\n",
5215 				    (uintmax_t) typeoff);
5216 			}
5217 
5218 			dump_dwarf_die(re, die, 0);
5219 		}
5220 		if (ret == DW_DLV_ERROR)
5221 			warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
5222 		if (is_info)
5223 			break;
5224 	} while (dwarf_next_types_section(re->dbg, &de) == DW_DLV_OK);
5225 }
5226 
5227 static void
5228 dump_dwarf_abbrev(struct readelf *re)
5229 {
5230 	Dwarf_Abbrev ab;
5231 	Dwarf_Off aboff, atoff;
5232 	Dwarf_Unsigned length, attr_count;
5233 	Dwarf_Signed flag, form;
5234 	Dwarf_Half tag, attr;
5235 	Dwarf_Error de;
5236 	const char *tag_str, *attr_str, *form_str;
5237 	char unk_tag[32], unk_attr[32], unk_form[32];
5238 	int i, j, ret;
5239 
5240 	printf("\nContents of section .debug_abbrev:\n\n");
5241 
5242 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, &aboff,
5243 	    NULL, NULL, &de)) ==  DW_DLV_OK) {
5244 		printf("  Number TAG\n");
5245 		i = 0;
5246 		while ((ret = dwarf_get_abbrev(re->dbg, aboff, &ab, &length,
5247 		    &attr_count, &de)) == DW_DLV_OK) {
5248 			if (length == 1) {
5249 				dwarf_dealloc(re->dbg, ab, DW_DLA_ABBREV);
5250 				break;
5251 			}
5252 			aboff += length;
5253 			printf("%4d", ++i);
5254 			if (dwarf_get_abbrev_tag(ab, &tag, &de) != DW_DLV_OK) {
5255 				warnx("dwarf_get_abbrev_tag failed: %s",
5256 				    dwarf_errmsg(de));
5257 				goto next_abbrev;
5258 			}
5259 			if (dwarf_get_TAG_name(tag, &tag_str) != DW_DLV_OK) {
5260 				snprintf(unk_tag, sizeof(unk_tag),
5261 				    "[Unknown Tag: %#x]", tag);
5262 				tag_str = unk_tag;
5263 			}
5264 			if (dwarf_get_abbrev_children_flag(ab, &flag, &de) !=
5265 			    DW_DLV_OK) {
5266 				warnx("dwarf_get_abbrev_children_flag failed:"
5267 				    " %s", dwarf_errmsg(de));
5268 				goto next_abbrev;
5269 			}
5270 			printf("      %s    %s\n", tag_str,
5271 			    flag ? "[has children]" : "[no children]");
5272 			for (j = 0; (Dwarf_Unsigned) j < attr_count; j++) {
5273 				if (dwarf_get_abbrev_entry(ab, (Dwarf_Signed) j,
5274 				    &attr, &form, &atoff, &de) != DW_DLV_OK) {
5275 					warnx("dwarf_get_abbrev_entry failed:"
5276 					    " %s", dwarf_errmsg(de));
5277 					continue;
5278 				}
5279 				if (dwarf_get_AT_name(attr, &attr_str) !=
5280 				    DW_DLV_OK) {
5281 					snprintf(unk_attr, sizeof(unk_attr),
5282 					    "[Unknown AT: %#x]", attr);
5283 					attr_str = unk_attr;
5284 				}
5285 				if (dwarf_get_FORM_name(form, &form_str) !=
5286 				    DW_DLV_OK) {
5287 					snprintf(unk_form, sizeof(unk_form),
5288 					    "[Unknown Form: %#x]",
5289 					    (Dwarf_Half) form);
5290 					form_str = unk_form;
5291 				}
5292 				printf("    %-18s %s\n", attr_str, form_str);
5293 			}
5294 		next_abbrev:
5295 			dwarf_dealloc(re->dbg, ab, DW_DLA_ABBREV);
5296 		}
5297 		if (ret != DW_DLV_OK)
5298 			warnx("dwarf_get_abbrev: %s", dwarf_errmsg(de));
5299 	}
5300 	if (ret == DW_DLV_ERROR)
5301 		warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
5302 }
5303 
5304 static void
5305 dump_dwarf_pubnames(struct readelf *re)
5306 {
5307 	struct section *s;
5308 	Dwarf_Off die_off;
5309 	Dwarf_Unsigned offset, length, nt_cu_offset, nt_cu_length;
5310 	Dwarf_Signed cnt;
5311 	Dwarf_Global *globs;
5312 	Dwarf_Half nt_version;
5313 	Dwarf_Error de;
5314 	Elf_Data *d;
5315 	char *glob_name;
5316 	int i, dwarf_size, elferr;
5317 
5318 	printf("\nContents of the .debug_pubnames section:\n");
5319 
5320 	s = NULL;
5321 	for (i = 0; (size_t) i < re->shnum; i++) {
5322 		s = &re->sl[i];
5323 		if (s->name != NULL && !strcmp(s->name, ".debug_pubnames"))
5324 			break;
5325 	}
5326 	if ((size_t) i >= re->shnum)
5327 		return;
5328 
5329 	(void) elf_errno();
5330 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
5331 		elferr = elf_errno();
5332 		if (elferr != 0)
5333 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
5334 		return;
5335 	}
5336 	if (d->d_size <= 0)
5337 		return;
5338 
5339 	/* Read in .debug_pubnames section table header. */
5340 	offset = 0;
5341 	length = re->dw_read(d, &offset, 4);
5342 	if (length == 0xffffffff) {
5343 		dwarf_size = 8;
5344 		length = re->dw_read(d, &offset, 8);
5345 	} else
5346 		dwarf_size = 4;
5347 
5348 	if (length > d->d_size - offset) {
5349 		warnx("invalid .dwarf_pubnames section");
5350 		return;
5351 	}
5352 
5353 	nt_version = re->dw_read(d, &offset, 2);
5354 	nt_cu_offset = re->dw_read(d, &offset, dwarf_size);
5355 	nt_cu_length = re->dw_read(d, &offset, dwarf_size);
5356 	printf("  Length:\t\t\t\t%ju\n", (uintmax_t) length);
5357 	printf("  Version:\t\t\t\t%u\n", nt_version);
5358 	printf("  Offset into .debug_info section:\t%ju\n",
5359 	    (uintmax_t) nt_cu_offset);
5360 	printf("  Size of area in .debug_info section:\t%ju\n",
5361 	    (uintmax_t) nt_cu_length);
5362 
5363 	if (dwarf_get_globals(re->dbg, &globs, &cnt, &de) != DW_DLV_OK) {
5364 		warnx("dwarf_get_globals failed: %s", dwarf_errmsg(de));
5365 		return;
5366 	}
5367 
5368 	printf("\n    Offset      Name\n");
5369 	for (i = 0; i < cnt; i++) {
5370 		if (dwarf_globname(globs[i], &glob_name, &de) != DW_DLV_OK) {
5371 			warnx("dwarf_globname failed: %s", dwarf_errmsg(de));
5372 			continue;
5373 		}
5374 		if (dwarf_global_die_offset(globs[i], &die_off, &de) !=
5375 		    DW_DLV_OK) {
5376 			warnx("dwarf_global_die_offset failed: %s",
5377 			    dwarf_errmsg(de));
5378 			continue;
5379 		}
5380 		printf("    %-11ju %s\n", (uintmax_t) die_off, glob_name);
5381 	}
5382 }
5383 
5384 static void
5385 dump_dwarf_aranges(struct readelf *re)
5386 {
5387 	struct section *s;
5388 	Dwarf_Arange *aranges;
5389 	Dwarf_Addr start;
5390 	Dwarf_Unsigned offset, length, as_cu_offset;
5391 	Dwarf_Off die_off;
5392 	Dwarf_Signed cnt;
5393 	Dwarf_Half as_version, as_addrsz, as_segsz;
5394 	Dwarf_Error de;
5395 	Elf_Data *d;
5396 	int i, dwarf_size, elferr;
5397 
5398 	printf("\nContents of section .debug_aranges:\n");
5399 
5400 	s = NULL;
5401 	for (i = 0; (size_t) i < re->shnum; i++) {
5402 		s = &re->sl[i];
5403 		if (s->name != NULL && !strcmp(s->name, ".debug_aranges"))
5404 			break;
5405 	}
5406 	if ((size_t) i >= re->shnum)
5407 		return;
5408 
5409 	(void) elf_errno();
5410 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
5411 		elferr = elf_errno();
5412 		if (elferr != 0)
5413 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
5414 		return;
5415 	}
5416 	if (d->d_size <= 0)
5417 		return;
5418 
5419 	/* Read in the .debug_aranges section table header. */
5420 	offset = 0;
5421 	length = re->dw_read(d, &offset, 4);
5422 	if (length == 0xffffffff) {
5423 		dwarf_size = 8;
5424 		length = re->dw_read(d, &offset, 8);
5425 	} else
5426 		dwarf_size = 4;
5427 
5428 	if (length > d->d_size - offset) {
5429 		warnx("invalid .dwarf_aranges section");
5430 		return;
5431 	}
5432 
5433 	as_version = re->dw_read(d, &offset, 2);
5434 	as_cu_offset = re->dw_read(d, &offset, dwarf_size);
5435 	as_addrsz = re->dw_read(d, &offset, 1);
5436 	as_segsz = re->dw_read(d, &offset, 1);
5437 
5438 	printf("  Length:\t\t\t%ju\n", (uintmax_t) length);
5439 	printf("  Version:\t\t\t%u\n", as_version);
5440 	printf("  Offset into .debug_info:\t%ju\n", (uintmax_t) as_cu_offset);
5441 	printf("  Pointer Size:\t\t\t%u\n", as_addrsz);
5442 	printf("  Segment Size:\t\t\t%u\n", as_segsz);
5443 
5444 	if (dwarf_get_aranges(re->dbg, &aranges, &cnt, &de) != DW_DLV_OK) {
5445 		warnx("dwarf_get_aranges failed: %s", dwarf_errmsg(de));
5446 		return;
5447 	}
5448 
5449 	printf("\n    Address  Length\n");
5450 	for (i = 0; i < cnt; i++) {
5451 		if (dwarf_get_arange_info(aranges[i], &start, &length,
5452 		    &die_off, &de) != DW_DLV_OK) {
5453 			warnx("dwarf_get_arange_info failed: %s",
5454 			    dwarf_errmsg(de));
5455 			continue;
5456 		}
5457 		printf("    %08jx %ju\n", (uintmax_t) start,
5458 		    (uintmax_t) length);
5459 	}
5460 }
5461 
5462 static void
5463 dump_dwarf_ranges_foreach(struct readelf *re, Dwarf_Die die, Dwarf_Addr base)
5464 {
5465 	Dwarf_Attribute *attr_list;
5466 	Dwarf_Ranges *ranges;
5467 	Dwarf_Die ret_die;
5468 	Dwarf_Error de;
5469 	Dwarf_Addr base0;
5470 	Dwarf_Half attr;
5471 	Dwarf_Signed attr_count, cnt;
5472 	Dwarf_Unsigned off, bytecnt;
5473 	int i, j, ret;
5474 
5475 	if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
5476 	    DW_DLV_OK) {
5477 		if (ret == DW_DLV_ERROR)
5478 			warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
5479 		goto cont_search;
5480 	}
5481 
5482 	for (i = 0; i < attr_count; i++) {
5483 		if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
5484 			warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
5485 			continue;
5486 		}
5487 		if (attr != DW_AT_ranges)
5488 			continue;
5489 		if (dwarf_formudata(attr_list[i], &off, &de) != DW_DLV_OK) {
5490 			warnx("dwarf_formudata failed: %s", dwarf_errmsg(de));
5491 			continue;
5492 		}
5493 		if (dwarf_get_ranges(re->dbg, (Dwarf_Off) off, &ranges, &cnt,
5494 		    &bytecnt, &de) != DW_DLV_OK)
5495 			continue;
5496 		base0 = base;
5497 		for (j = 0; j < cnt; j++) {
5498 			printf("    %08jx ", (uintmax_t) off);
5499 			if (ranges[j].dwr_type == DW_RANGES_END) {
5500 				printf("%s\n", "<End of list>");
5501 				continue;
5502 			} else if (ranges[j].dwr_type ==
5503 			    DW_RANGES_ADDRESS_SELECTION) {
5504 				base0 = ranges[j].dwr_addr2;
5505 				continue;
5506 			}
5507 			if (re->ec == ELFCLASS32)
5508 				printf("%08jx %08jx\n",
5509 				    ranges[j].dwr_addr1 + base0,
5510 				    ranges[j].dwr_addr2 + base0);
5511 			else
5512 				printf("%016jx %016jx\n",
5513 				    ranges[j].dwr_addr1 + base0,
5514 				    ranges[j].dwr_addr2 + base0);
5515 		}
5516 	}
5517 
5518 cont_search:
5519 	/* Search children. */
5520 	ret = dwarf_child(die, &ret_die, &de);
5521 	if (ret == DW_DLV_ERROR)
5522 		warnx("dwarf_child: %s", dwarf_errmsg(de));
5523 	else if (ret == DW_DLV_OK)
5524 		dump_dwarf_ranges_foreach(re, ret_die, base);
5525 
5526 	/* Search sibling. */
5527 	ret = dwarf_siblingof(re->dbg, die, &ret_die, &de);
5528 	if (ret == DW_DLV_ERROR)
5529 		warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
5530 	else if (ret == DW_DLV_OK)
5531 		dump_dwarf_ranges_foreach(re, ret_die, base);
5532 }
5533 
5534 static void
5535 dump_dwarf_ranges(struct readelf *re)
5536 {
5537 	Dwarf_Ranges *ranges;
5538 	Dwarf_Die die;
5539 	Dwarf_Signed cnt;
5540 	Dwarf_Unsigned bytecnt;
5541 	Dwarf_Half tag;
5542 	Dwarf_Error de;
5543 	Dwarf_Unsigned lowpc;
5544 	int ret;
5545 
5546 	if (dwarf_get_ranges(re->dbg, 0, &ranges, &cnt, &bytecnt, &de) !=
5547 	    DW_DLV_OK)
5548 		return;
5549 
5550 	printf("Contents of the .debug_ranges section:\n\n");
5551 	if (re->ec == ELFCLASS32)
5552 		printf("    %-8s %-8s %s\n", "Offset", "Begin", "End");
5553 	else
5554 		printf("    %-8s %-16s %s\n", "Offset", "Begin", "End");
5555 
5556 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
5557 	    NULL, &de)) == DW_DLV_OK) {
5558 		die = NULL;
5559 		if (dwarf_siblingof(re->dbg, die, &die, &de) != DW_DLV_OK)
5560 			continue;
5561 		if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
5562 			warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
5563 			continue;
5564 		}
5565 		/* XXX: What about DW_TAG_partial_unit? */
5566 		lowpc = 0;
5567 		if (tag == DW_TAG_compile_unit) {
5568 			if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lowpc,
5569 			    &de) != DW_DLV_OK)
5570 				lowpc = 0;
5571 		}
5572 
5573 		dump_dwarf_ranges_foreach(re, die, (Dwarf_Addr) lowpc);
5574 	}
5575 	putchar('\n');
5576 }
5577 
5578 static void
5579 dump_dwarf_macinfo(struct readelf *re)
5580 {
5581 	Dwarf_Unsigned offset;
5582 	Dwarf_Signed cnt;
5583 	Dwarf_Macro_Details *md;
5584 	Dwarf_Error de;
5585 	const char *mi_str;
5586 	char unk_mi[32];
5587 	int i;
5588 
5589 #define	_MAX_MACINFO_ENTRY	65535
5590 
5591 	printf("\nContents of section .debug_macinfo:\n\n");
5592 
5593 	offset = 0;
5594 	while (dwarf_get_macro_details(re->dbg, offset, _MAX_MACINFO_ENTRY,
5595 	    &cnt, &md, &de) == DW_DLV_OK) {
5596 		for (i = 0; i < cnt; i++) {
5597 			offset = md[i].dmd_offset + 1;
5598 			if (md[i].dmd_type == 0)
5599 				break;
5600 			if (dwarf_get_MACINFO_name(md[i].dmd_type, &mi_str) !=
5601 			    DW_DLV_OK) {
5602 				snprintf(unk_mi, sizeof(unk_mi),
5603 				    "[Unknown MACINFO: %#x]", md[i].dmd_type);
5604 				mi_str = unk_mi;
5605 			}
5606 			printf(" %s", mi_str);
5607 			switch (md[i].dmd_type) {
5608 			case DW_MACINFO_define:
5609 			case DW_MACINFO_undef:
5610 				printf(" - lineno : %jd macro : %s\n",
5611 				    (intmax_t) md[i].dmd_lineno,
5612 				    md[i].dmd_macro);
5613 				break;
5614 			case DW_MACINFO_start_file:
5615 				printf(" - lineno : %jd filenum : %jd\n",
5616 				    (intmax_t) md[i].dmd_lineno,
5617 				    (intmax_t) md[i].dmd_fileindex);
5618 				break;
5619 			default:
5620 				putchar('\n');
5621 				break;
5622 			}
5623 		}
5624 	}
5625 
5626 #undef	_MAX_MACINFO_ENTRY
5627 }
5628 
5629 static void
5630 dump_dwarf_frame_inst(struct readelf *re, Dwarf_Cie cie, uint8_t *insts,
5631     Dwarf_Unsigned len, Dwarf_Unsigned caf, Dwarf_Signed daf, Dwarf_Addr pc,
5632     Dwarf_Debug dbg)
5633 {
5634 	Dwarf_Frame_Op *oplist;
5635 	Dwarf_Signed opcnt, delta;
5636 	Dwarf_Small op;
5637 	Dwarf_Error de;
5638 	const char *op_str;
5639 	char unk_op[32];
5640 	int i;
5641 
5642 	if (dwarf_expand_frame_instructions(cie, insts, len, &oplist,
5643 	    &opcnt, &de) != DW_DLV_OK) {
5644 		warnx("dwarf_expand_frame_instructions failed: %s",
5645 		    dwarf_errmsg(de));
5646 		return;
5647 	}
5648 
5649 	for (i = 0; i < opcnt; i++) {
5650 		if (oplist[i].fp_base_op != 0)
5651 			op = oplist[i].fp_base_op << 6;
5652 		else
5653 			op = oplist[i].fp_extended_op;
5654 		if (dwarf_get_CFA_name(op, &op_str) != DW_DLV_OK) {
5655 			snprintf(unk_op, sizeof(unk_op), "[Unknown CFA: %#x]",
5656 			    op);
5657 			op_str = unk_op;
5658 		}
5659 		printf("  %s", op_str);
5660 		switch (op) {
5661 		case DW_CFA_advance_loc:
5662 			delta = oplist[i].fp_offset * caf;
5663 			pc += delta;
5664 			printf(": %ju to %08jx", (uintmax_t) delta,
5665 			    (uintmax_t) pc);
5666 			break;
5667 		case DW_CFA_offset:
5668 		case DW_CFA_offset_extended:
5669 		case DW_CFA_offset_extended_sf:
5670 			delta = oplist[i].fp_offset * daf;
5671 			printf(": r%u (%s) at cfa%+jd", oplist[i].fp_register,
5672 			    dwarf_regname(re, oplist[i].fp_register),
5673 			    (intmax_t) delta);
5674 			break;
5675 		case DW_CFA_restore:
5676 			printf(": r%u (%s)", oplist[i].fp_register,
5677 			    dwarf_regname(re, oplist[i].fp_register));
5678 			break;
5679 		case DW_CFA_set_loc:
5680 			pc = oplist[i].fp_offset;
5681 			printf(": to %08jx", (uintmax_t) pc);
5682 			break;
5683 		case DW_CFA_advance_loc1:
5684 		case DW_CFA_advance_loc2:
5685 		case DW_CFA_advance_loc4:
5686 			pc += oplist[i].fp_offset;
5687 			printf(": %jd to %08jx", (intmax_t) oplist[i].fp_offset,
5688 			    (uintmax_t) pc);
5689 			break;
5690 		case DW_CFA_def_cfa:
5691 			printf(": r%u (%s) ofs %ju", oplist[i].fp_register,
5692 			    dwarf_regname(re, oplist[i].fp_register),
5693 			    (uintmax_t) oplist[i].fp_offset);
5694 			break;
5695 		case DW_CFA_def_cfa_sf:
5696 			printf(": r%u (%s) ofs %jd", oplist[i].fp_register,
5697 			    dwarf_regname(re, oplist[i].fp_register),
5698 			    (intmax_t) (oplist[i].fp_offset * daf));
5699 			break;
5700 		case DW_CFA_def_cfa_register:
5701 			printf(": r%u (%s)", oplist[i].fp_register,
5702 			    dwarf_regname(re, oplist[i].fp_register));
5703 			break;
5704 		case DW_CFA_def_cfa_offset:
5705 			printf(": %ju", (uintmax_t) oplist[i].fp_offset);
5706 			break;
5707 		case DW_CFA_def_cfa_offset_sf:
5708 			printf(": %jd", (intmax_t) (oplist[i].fp_offset * daf));
5709 			break;
5710 		default:
5711 			break;
5712 		}
5713 		putchar('\n');
5714 	}
5715 
5716 	dwarf_dealloc(dbg, oplist, DW_DLA_FRAME_BLOCK);
5717 }
5718 
5719 static char *
5720 get_regoff_str(struct readelf *re, Dwarf_Half reg, Dwarf_Addr off)
5721 {
5722 	static char rs[16];
5723 
5724 	if (reg == DW_FRAME_UNDEFINED_VAL || reg == DW_FRAME_REG_INITIAL_VALUE)
5725 		snprintf(rs, sizeof(rs), "%c", 'u');
5726 	else if (reg == DW_FRAME_CFA_COL)
5727 		snprintf(rs, sizeof(rs), "c%+jd", (intmax_t) off);
5728 	else
5729 		snprintf(rs, sizeof(rs), "%s%+jd", dwarf_regname(re, reg),
5730 		    (intmax_t) off);
5731 
5732 	return (rs);
5733 }
5734 
5735 static int
5736 dump_dwarf_frame_regtable(struct readelf *re, Dwarf_Fde fde, Dwarf_Addr pc,
5737     Dwarf_Unsigned func_len, Dwarf_Half cie_ra)
5738 {
5739 	Dwarf_Regtable rt;
5740 	Dwarf_Addr row_pc, end_pc, pre_pc, cur_pc;
5741 	Dwarf_Error de;
5742 	char *vec;
5743 	int i;
5744 
5745 #define BIT_SET(v, n) (v[(n)>>3] |= 1U << ((n) & 7))
5746 #define BIT_CLR(v, n) (v[(n)>>3] &= ~(1U << ((n) & 7)))
5747 #define BIT_ISSET(v, n) (v[(n)>>3] & (1U << ((n) & 7)))
5748 #define	RT(x) rt.rules[(x)]
5749 
5750 	vec = calloc((DW_REG_TABLE_SIZE + 7) / 8, 1);
5751 	if (vec == NULL)
5752 		err(EXIT_FAILURE, "calloc failed");
5753 
5754 	pre_pc = ~((Dwarf_Addr) 0);
5755 	cur_pc = pc;
5756 	end_pc = pc + func_len;
5757 	for (; cur_pc < end_pc; cur_pc++) {
5758 		if (dwarf_get_fde_info_for_all_regs(fde, cur_pc, &rt, &row_pc,
5759 		    &de) != DW_DLV_OK) {
5760 			warnx("dwarf_get_fde_info_for_all_regs failed: %s\n",
5761 			    dwarf_errmsg(de));
5762 			return (-1);
5763 		}
5764 		if (row_pc == pre_pc)
5765 			continue;
5766 		pre_pc = row_pc;
5767 		for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
5768 			if (rt.rules[i].dw_regnum != DW_FRAME_REG_INITIAL_VALUE)
5769 				BIT_SET(vec, i);
5770 		}
5771 	}
5772 
5773 	printf("   LOC   CFA      ");
5774 	for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
5775 		if (BIT_ISSET(vec, i)) {
5776 			if ((Dwarf_Half) i == cie_ra)
5777 				printf("ra   ");
5778 			else
5779 				printf("%-5s",
5780 				    dwarf_regname(re, (unsigned int) i));
5781 		}
5782 	}
5783 	putchar('\n');
5784 
5785 	pre_pc = ~((Dwarf_Addr) 0);
5786 	cur_pc = pc;
5787 	end_pc = pc + func_len;
5788 	for (; cur_pc < end_pc; cur_pc++) {
5789 		if (dwarf_get_fde_info_for_all_regs(fde, cur_pc, &rt, &row_pc,
5790 		    &de) != DW_DLV_OK) {
5791 			warnx("dwarf_get_fde_info_for_all_regs failed: %s\n",
5792 			    dwarf_errmsg(de));
5793 			return (-1);
5794 		}
5795 		if (row_pc == pre_pc)
5796 			continue;
5797 		pre_pc = row_pc;
5798 		printf("%08jx ", (uintmax_t) row_pc);
5799 		printf("%-8s ", get_regoff_str(re, RT(0).dw_regnum,
5800 		    RT(0).dw_offset));
5801 		for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
5802 			if (BIT_ISSET(vec, i)) {
5803 				printf("%-5s", get_regoff_str(re,
5804 				    RT(i).dw_regnum, RT(i).dw_offset));
5805 			}
5806 		}
5807 		putchar('\n');
5808 	}
5809 
5810 	free(vec);
5811 
5812 	return (0);
5813 
5814 #undef	BIT_SET
5815 #undef	BIT_CLR
5816 #undef	BIT_ISSET
5817 #undef	RT
5818 }
5819 
5820 static void
5821 dump_dwarf_frame_section(struct readelf *re, struct section *s, int alt)
5822 {
5823 	Dwarf_Cie *cie_list, cie, pre_cie;
5824 	Dwarf_Fde *fde_list, fde;
5825 	Dwarf_Off cie_offset, fde_offset;
5826 	Dwarf_Unsigned cie_length, fde_instlen;
5827 	Dwarf_Unsigned cie_caf, cie_daf, cie_instlen, func_len, fde_length;
5828 	Dwarf_Signed cie_count, fde_count, cie_index;
5829 	Dwarf_Addr low_pc;
5830 	Dwarf_Half cie_ra;
5831 	Dwarf_Small cie_version;
5832 	Dwarf_Ptr fde_addr, fde_inst, cie_inst;
5833 	char *cie_aug, c;
5834 	int i, eh_frame;
5835 	Dwarf_Error de;
5836 
5837 	printf("\nThe section %s contains:\n\n", s->name);
5838 
5839 	if (!strcmp(s->name, ".debug_frame")) {
5840 		eh_frame = 0;
5841 		if (dwarf_get_fde_list(re->dbg, &cie_list, &cie_count,
5842 		    &fde_list, &fde_count, &de) != DW_DLV_OK) {
5843 			warnx("dwarf_get_fde_list failed: %s",
5844 			    dwarf_errmsg(de));
5845 			return;
5846 		}
5847 	} else if (!strcmp(s->name, ".eh_frame")) {
5848 		eh_frame = 1;
5849 		if (dwarf_get_fde_list_eh(re->dbg, &cie_list, &cie_count,
5850 		    &fde_list, &fde_count, &de) != DW_DLV_OK) {
5851 			warnx("dwarf_get_fde_list_eh failed: %s",
5852 			    dwarf_errmsg(de));
5853 			return;
5854 		}
5855 	} else
5856 		return;
5857 
5858 	pre_cie = NULL;
5859 	for (i = 0; i < fde_count; i++) {
5860 		if (dwarf_get_fde_n(fde_list, i, &fde, &de) != DW_DLV_OK) {
5861 			warnx("dwarf_get_fde_n failed: %s", dwarf_errmsg(de));
5862 			continue;
5863 		}
5864 		if (dwarf_get_cie_of_fde(fde, &cie, &de) != DW_DLV_OK) {
5865 			warnx("dwarf_get_fde_n failed: %s", dwarf_errmsg(de));
5866 			continue;
5867 		}
5868 		if (dwarf_get_fde_range(fde, &low_pc, &func_len, &fde_addr,
5869 		    &fde_length, &cie_offset, &cie_index, &fde_offset,
5870 		    &de) != DW_DLV_OK) {
5871 			warnx("dwarf_get_fde_range failed: %s",
5872 			    dwarf_errmsg(de));
5873 			continue;
5874 		}
5875 		if (dwarf_get_fde_instr_bytes(fde, &fde_inst, &fde_instlen,
5876 		    &de) != DW_DLV_OK) {
5877 			warnx("dwarf_get_fde_instr_bytes failed: %s",
5878 			    dwarf_errmsg(de));
5879 			continue;
5880 		}
5881 		if (pre_cie == NULL || cie != pre_cie) {
5882 			pre_cie = cie;
5883 			if (dwarf_get_cie_info(cie, &cie_length, &cie_version,
5884 			    &cie_aug, &cie_caf, &cie_daf, &cie_ra,
5885 			    &cie_inst, &cie_instlen, &de) != DW_DLV_OK) {
5886 				warnx("dwarf_get_cie_info failed: %s",
5887 				    dwarf_errmsg(de));
5888 				continue;
5889 			}
5890 			printf("%08jx %08jx %8.8jx CIE",
5891 			    (uintmax_t) cie_offset,
5892 			    (uintmax_t) cie_length,
5893 			    (uintmax_t) (eh_frame ? 0 : ~0U));
5894 			if (!alt) {
5895 				putchar('\n');
5896 				printf("  Version:\t\t\t%u\n", cie_version);
5897 				printf("  Augmentation:\t\t\t\"");
5898 				while ((c = *cie_aug++) != '\0')
5899 					putchar(c);
5900 				printf("\"\n");
5901 				printf("  Code alignment factor:\t%ju\n",
5902 				    (uintmax_t) cie_caf);
5903 				printf("  Data alignment factor:\t%jd\n",
5904 				    (intmax_t) cie_daf);
5905 				printf("  Return address column:\t%ju\n",
5906 				    (uintmax_t) cie_ra);
5907 				putchar('\n');
5908 				dump_dwarf_frame_inst(re, cie, cie_inst,
5909 				    cie_instlen, cie_caf, cie_daf, 0,
5910 				    re->dbg);
5911 				putchar('\n');
5912 			} else {
5913 				printf(" \"");
5914 				while ((c = *cie_aug++) != '\0')
5915 					putchar(c);
5916 				putchar('"');
5917 				printf(" cf=%ju df=%jd ra=%ju\n",
5918 				    (uintmax_t) cie_caf,
5919 				    (uintmax_t) cie_daf,
5920 				    (uintmax_t) cie_ra);
5921 				dump_dwarf_frame_regtable(re, fde, low_pc, 1,
5922 				    cie_ra);
5923 				putchar('\n');
5924 			}
5925 		}
5926 		printf("%08jx %08jx %08jx FDE cie=%08jx pc=%08jx..%08jx\n",
5927 		    (uintmax_t) fde_offset, (uintmax_t) fde_length,
5928 		    (uintmax_t) cie_offset,
5929 		    (uintmax_t) (eh_frame ? fde_offset + 4 - cie_offset :
5930 			cie_offset),
5931 		    (uintmax_t) low_pc, (uintmax_t) (low_pc + func_len));
5932 		if (!alt)
5933 			dump_dwarf_frame_inst(re, cie, fde_inst, fde_instlen,
5934 			    cie_caf, cie_daf, low_pc, re->dbg);
5935 		else
5936 			dump_dwarf_frame_regtable(re, fde, low_pc, func_len,
5937 			    cie_ra);
5938 		putchar('\n');
5939 	}
5940 }
5941 
5942 static void
5943 dump_dwarf_frame(struct readelf *re, int alt)
5944 {
5945 	struct section *s;
5946 	int i;
5947 
5948 	(void) dwarf_set_frame_cfa_value(re->dbg, DW_FRAME_CFA_COL);
5949 
5950 	for (i = 0; (size_t) i < re->shnum; i++) {
5951 		s = &re->sl[i];
5952 		if (s->name != NULL && (!strcmp(s->name, ".debug_frame") ||
5953 		    !strcmp(s->name, ".eh_frame")))
5954 			dump_dwarf_frame_section(re, s, alt);
5955 	}
5956 }
5957 
5958 static void
5959 dump_dwarf_str(struct readelf *re)
5960 {
5961 	struct section *s;
5962 	Elf_Data *d;
5963 	unsigned char *p;
5964 	int elferr, end, i, j;
5965 
5966 	printf("\nContents of section .debug_str:\n");
5967 
5968 	s = NULL;
5969 	for (i = 0; (size_t) i < re->shnum; i++) {
5970 		s = &re->sl[i];
5971 		if (s->name != NULL && !strcmp(s->name, ".debug_str"))
5972 			break;
5973 	}
5974 	if ((size_t) i >= re->shnum)
5975 		return;
5976 
5977 	(void) elf_errno();
5978 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
5979 		elferr = elf_errno();
5980 		if (elferr != 0)
5981 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
5982 		return;
5983 	}
5984 	if (d->d_size <= 0)
5985 		return;
5986 
5987 	for (i = 0, p = d->d_buf; (size_t) i < d->d_size; i += 16) {
5988 		printf("  0x%08x", (unsigned int) i);
5989 		if ((size_t) i + 16 > d->d_size)
5990 			end = d->d_size;
5991 		else
5992 			end = i + 16;
5993 		for (j = i; j < i + 16; j++) {
5994 			if ((j - i) % 4 == 0)
5995 				putchar(' ');
5996 			if (j >= end) {
5997 				printf("  ");
5998 				continue;
5999 			}
6000 			printf("%02x", (uint8_t) p[j]);
6001 		}
6002 		putchar(' ');
6003 		for (j = i; j < end; j++) {
6004 			if (isprint(p[j]))
6005 				putchar(p[j]);
6006 			else if (p[j] == 0)
6007 				putchar('.');
6008 			else
6009 				putchar(' ');
6010 		}
6011 		putchar('\n');
6012 	}
6013 }
6014 
6015 struct loc_at {
6016 	Dwarf_Attribute la_at;
6017 	Dwarf_Unsigned la_off;
6018 	Dwarf_Unsigned la_lowpc;
6019 	Dwarf_Half la_cu_psize;
6020 	Dwarf_Half la_cu_osize;
6021 	Dwarf_Half la_cu_ver;
6022 	TAILQ_ENTRY(loc_at) la_next;
6023 };
6024 
6025 static TAILQ_HEAD(, loc_at) lalist = TAILQ_HEAD_INITIALIZER(lalist);
6026 
6027 static void
6028 search_loclist_at(struct readelf *re, Dwarf_Die die, Dwarf_Unsigned lowpc)
6029 {
6030 	Dwarf_Attribute *attr_list;
6031 	Dwarf_Die ret_die;
6032 	Dwarf_Unsigned off;
6033 	Dwarf_Off ref;
6034 	Dwarf_Signed attr_count;
6035 	Dwarf_Half attr, form;
6036 	Dwarf_Bool is_info;
6037 	Dwarf_Error de;
6038 	struct loc_at *la, *nla;
6039 	int i, ret;
6040 
6041 	is_info = dwarf_get_die_infotypes_flag(die);
6042 
6043 	if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
6044 	    DW_DLV_OK) {
6045 		if (ret == DW_DLV_ERROR)
6046 			warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
6047 		goto cont_search;
6048 	}
6049 	for (i = 0; i < attr_count; i++) {
6050 		if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
6051 			warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
6052 			continue;
6053 		}
6054 		if (attr != DW_AT_location &&
6055 		    attr != DW_AT_string_length &&
6056 		    attr != DW_AT_return_addr &&
6057 		    attr != DW_AT_data_member_location &&
6058 		    attr != DW_AT_frame_base &&
6059 		    attr != DW_AT_segment &&
6060 		    attr != DW_AT_static_link &&
6061 		    attr != DW_AT_use_location &&
6062 		    attr != DW_AT_vtable_elem_location)
6063 			continue;
6064 		if (dwarf_whatform(attr_list[i], &form, &de) != DW_DLV_OK) {
6065 			warnx("dwarf_whatform failed: %s", dwarf_errmsg(de));
6066 			continue;
6067 		}
6068 		if (form == DW_FORM_data4 || form == DW_FORM_data8) {
6069 			if (dwarf_formudata(attr_list[i], &off, &de) !=
6070 			    DW_DLV_OK) {
6071 				warnx("dwarf_formudata failed: %s",
6072 				    dwarf_errmsg(de));
6073 				continue;
6074 			}
6075 		} else if (form == DW_FORM_sec_offset) {
6076 			if (dwarf_global_formref(attr_list[i], &ref, &de) !=
6077 			    DW_DLV_OK) {
6078 				warnx("dwarf_global_formref failed: %s",
6079 				    dwarf_errmsg(de));
6080 				continue;
6081 			}
6082 			off = ref;
6083 		} else
6084 			continue;
6085 
6086 		TAILQ_FOREACH(la, &lalist, la_next) {
6087 			if (off == la->la_off)
6088 				break;
6089 			if (off < la->la_off) {
6090 				if ((nla = malloc(sizeof(*nla))) == NULL)
6091 					err(EXIT_FAILURE, "malloc failed");
6092 				nla->la_at = attr_list[i];
6093 				nla->la_off = off;
6094 				nla->la_lowpc = lowpc;
6095 				nla->la_cu_psize = re->cu_psize;
6096 				nla->la_cu_osize = re->cu_osize;
6097 				nla->la_cu_ver = re->cu_ver;
6098 				TAILQ_INSERT_BEFORE(la, nla, la_next);
6099 				break;
6100 			}
6101 		}
6102 		if (la == NULL) {
6103 			if ((nla = malloc(sizeof(*nla))) == NULL)
6104 				err(EXIT_FAILURE, "malloc failed");
6105 			nla->la_at = attr_list[i];
6106 			nla->la_off = off;
6107 			nla->la_lowpc = lowpc;
6108 			nla->la_cu_psize = re->cu_psize;
6109 			nla->la_cu_osize = re->cu_osize;
6110 			nla->la_cu_ver = re->cu_ver;
6111 			TAILQ_INSERT_TAIL(&lalist, nla, la_next);
6112 		}
6113 	}
6114 
6115 cont_search:
6116 	/* Search children. */
6117 	ret = dwarf_child(die, &ret_die, &de);
6118 	if (ret == DW_DLV_ERROR)
6119 		warnx("dwarf_child: %s", dwarf_errmsg(de));
6120 	else if (ret == DW_DLV_OK)
6121 		search_loclist_at(re, ret_die, lowpc);
6122 
6123 	/* Search sibling. */
6124 	ret = dwarf_siblingof_b(re->dbg, die, &ret_die, is_info, &de);
6125 	if (ret == DW_DLV_ERROR)
6126 		warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
6127 	else if (ret == DW_DLV_OK)
6128 		search_loclist_at(re, ret_die, lowpc);
6129 }
6130 
6131 static void
6132 dump_dwarf_loc(struct readelf *re, Dwarf_Loc *lr)
6133 {
6134 	const char *op_str;
6135 	char unk_op[32];
6136 	uint8_t *b, n;
6137 	int i;
6138 
6139 	if (dwarf_get_OP_name(lr->lr_atom, &op_str) !=
6140 	    DW_DLV_OK) {
6141 		snprintf(unk_op, sizeof(unk_op),
6142 		    "[Unknown OP: %#x]", lr->lr_atom);
6143 		op_str = unk_op;
6144 	}
6145 
6146 	printf("%s", op_str);
6147 
6148 	switch (lr->lr_atom) {
6149 	case DW_OP_reg0:
6150 	case DW_OP_reg1:
6151 	case DW_OP_reg2:
6152 	case DW_OP_reg3:
6153 	case DW_OP_reg4:
6154 	case DW_OP_reg5:
6155 	case DW_OP_reg6:
6156 	case DW_OP_reg7:
6157 	case DW_OP_reg8:
6158 	case DW_OP_reg9:
6159 	case DW_OP_reg10:
6160 	case DW_OP_reg11:
6161 	case DW_OP_reg12:
6162 	case DW_OP_reg13:
6163 	case DW_OP_reg14:
6164 	case DW_OP_reg15:
6165 	case DW_OP_reg16:
6166 	case DW_OP_reg17:
6167 	case DW_OP_reg18:
6168 	case DW_OP_reg19:
6169 	case DW_OP_reg20:
6170 	case DW_OP_reg21:
6171 	case DW_OP_reg22:
6172 	case DW_OP_reg23:
6173 	case DW_OP_reg24:
6174 	case DW_OP_reg25:
6175 	case DW_OP_reg26:
6176 	case DW_OP_reg27:
6177 	case DW_OP_reg28:
6178 	case DW_OP_reg29:
6179 	case DW_OP_reg30:
6180 	case DW_OP_reg31:
6181 		printf(" (%s)", dwarf_regname(re, lr->lr_atom - DW_OP_reg0));
6182 		break;
6183 
6184 	case DW_OP_deref:
6185 	case DW_OP_lit0:
6186 	case DW_OP_lit1:
6187 	case DW_OP_lit2:
6188 	case DW_OP_lit3:
6189 	case DW_OP_lit4:
6190 	case DW_OP_lit5:
6191 	case DW_OP_lit6:
6192 	case DW_OP_lit7:
6193 	case DW_OP_lit8:
6194 	case DW_OP_lit9:
6195 	case DW_OP_lit10:
6196 	case DW_OP_lit11:
6197 	case DW_OP_lit12:
6198 	case DW_OP_lit13:
6199 	case DW_OP_lit14:
6200 	case DW_OP_lit15:
6201 	case DW_OP_lit16:
6202 	case DW_OP_lit17:
6203 	case DW_OP_lit18:
6204 	case DW_OP_lit19:
6205 	case DW_OP_lit20:
6206 	case DW_OP_lit21:
6207 	case DW_OP_lit22:
6208 	case DW_OP_lit23:
6209 	case DW_OP_lit24:
6210 	case DW_OP_lit25:
6211 	case DW_OP_lit26:
6212 	case DW_OP_lit27:
6213 	case DW_OP_lit28:
6214 	case DW_OP_lit29:
6215 	case DW_OP_lit30:
6216 	case DW_OP_lit31:
6217 	case DW_OP_dup:
6218 	case DW_OP_drop:
6219 	case DW_OP_over:
6220 	case DW_OP_swap:
6221 	case DW_OP_rot:
6222 	case DW_OP_xderef:
6223 	case DW_OP_abs:
6224 	case DW_OP_and:
6225 	case DW_OP_div:
6226 	case DW_OP_minus:
6227 	case DW_OP_mod:
6228 	case DW_OP_mul:
6229 	case DW_OP_neg:
6230 	case DW_OP_not:
6231 	case DW_OP_or:
6232 	case DW_OP_plus:
6233 	case DW_OP_shl:
6234 	case DW_OP_shr:
6235 	case DW_OP_shra:
6236 	case DW_OP_xor:
6237 	case DW_OP_eq:
6238 	case DW_OP_ge:
6239 	case DW_OP_gt:
6240 	case DW_OP_le:
6241 	case DW_OP_lt:
6242 	case DW_OP_ne:
6243 	case DW_OP_nop:
6244 	case DW_OP_push_object_address:
6245 	case DW_OP_form_tls_address:
6246 	case DW_OP_call_frame_cfa:
6247 	case DW_OP_stack_value:
6248 	case DW_OP_GNU_push_tls_address:
6249 	case DW_OP_GNU_uninit:
6250 		break;
6251 
6252 	case DW_OP_const1u:
6253 	case DW_OP_pick:
6254 	case DW_OP_deref_size:
6255 	case DW_OP_xderef_size:
6256 	case DW_OP_const2u:
6257 	case DW_OP_bra:
6258 	case DW_OP_skip:
6259 	case DW_OP_const4u:
6260 	case DW_OP_const8u:
6261 	case DW_OP_constu:
6262 	case DW_OP_plus_uconst:
6263 	case DW_OP_regx:
6264 	case DW_OP_piece:
6265 		printf(": %ju", (uintmax_t)
6266 		    lr->lr_number);
6267 		break;
6268 
6269 	case DW_OP_const1s:
6270 	case DW_OP_const2s:
6271 	case DW_OP_const4s:
6272 	case DW_OP_const8s:
6273 	case DW_OP_consts:
6274 		printf(": %jd", (intmax_t)
6275 		    lr->lr_number);
6276 		break;
6277 
6278 	case DW_OP_breg0:
6279 	case DW_OP_breg1:
6280 	case DW_OP_breg2:
6281 	case DW_OP_breg3:
6282 	case DW_OP_breg4:
6283 	case DW_OP_breg5:
6284 	case DW_OP_breg6:
6285 	case DW_OP_breg7:
6286 	case DW_OP_breg8:
6287 	case DW_OP_breg9:
6288 	case DW_OP_breg10:
6289 	case DW_OP_breg11:
6290 	case DW_OP_breg12:
6291 	case DW_OP_breg13:
6292 	case DW_OP_breg14:
6293 	case DW_OP_breg15:
6294 	case DW_OP_breg16:
6295 	case DW_OP_breg17:
6296 	case DW_OP_breg18:
6297 	case DW_OP_breg19:
6298 	case DW_OP_breg20:
6299 	case DW_OP_breg21:
6300 	case DW_OP_breg22:
6301 	case DW_OP_breg23:
6302 	case DW_OP_breg24:
6303 	case DW_OP_breg25:
6304 	case DW_OP_breg26:
6305 	case DW_OP_breg27:
6306 	case DW_OP_breg28:
6307 	case DW_OP_breg29:
6308 	case DW_OP_breg30:
6309 	case DW_OP_breg31:
6310 		printf(" (%s): %jd",
6311 		    dwarf_regname(re, lr->lr_atom - DW_OP_breg0),
6312 		    (intmax_t) lr->lr_number);
6313 		break;
6314 
6315 	case DW_OP_fbreg:
6316 		printf(": %jd", (intmax_t)
6317 		    lr->lr_number);
6318 		break;
6319 
6320 	case DW_OP_bregx:
6321 		printf(": %ju (%s) %jd",
6322 		    (uintmax_t) lr->lr_number,
6323 		    dwarf_regname(re, (unsigned int) lr->lr_number),
6324 		    (intmax_t) lr->lr_number2);
6325 		break;
6326 
6327 	case DW_OP_addr:
6328 	case DW_OP_GNU_encoded_addr:
6329 		printf(": %#jx", (uintmax_t)
6330 		    lr->lr_number);
6331 		break;
6332 
6333 	case DW_OP_GNU_implicit_pointer:
6334 		printf(": <0x%jx> %jd", (uintmax_t) lr->lr_number,
6335 		    (intmax_t) lr->lr_number2);
6336 		break;
6337 
6338 	case DW_OP_implicit_value:
6339 		printf(": %ju byte block:", (uintmax_t) lr->lr_number);
6340 		b = (uint8_t *)(uintptr_t) lr->lr_number2;
6341 		for (i = 0; (Dwarf_Unsigned) i < lr->lr_number; i++)
6342 			printf(" %x", b[i]);
6343 		break;
6344 
6345 	case DW_OP_GNU_entry_value:
6346 		printf(": (");
6347 		dump_dwarf_block(re, (uint8_t *)(uintptr_t) lr->lr_number2,
6348 		    lr->lr_number);
6349 		putchar(')');
6350 		break;
6351 
6352 	case DW_OP_GNU_const_type:
6353 		printf(": <0x%jx> ", (uintmax_t) lr->lr_number);
6354 		b = (uint8_t *)(uintptr_t) lr->lr_number2;
6355 		n = *b;
6356 		for (i = 1; (uint8_t) i < n; i++)
6357 			printf(" %x", b[i]);
6358 		break;
6359 
6360 	case DW_OP_GNU_regval_type:
6361 		printf(": %ju (%s) <0x%jx>", (uintmax_t) lr->lr_number,
6362 		    dwarf_regname(re, (unsigned int) lr->lr_number),
6363 		    (uintmax_t) lr->lr_number2);
6364 		break;
6365 
6366 	case DW_OP_GNU_convert:
6367 	case DW_OP_GNU_deref_type:
6368 	case DW_OP_GNU_parameter_ref:
6369 	case DW_OP_GNU_reinterpret:
6370 		printf(": <0x%jx>", (uintmax_t) lr->lr_number);
6371 		break;
6372 
6373 	default:
6374 		break;
6375 	}
6376 }
6377 
6378 static void
6379 dump_dwarf_block(struct readelf *re, uint8_t *b, Dwarf_Unsigned len)
6380 {
6381 	Dwarf_Locdesc *llbuf;
6382 	Dwarf_Signed lcnt;
6383 	Dwarf_Error de;
6384 	int i;
6385 
6386 	if (dwarf_loclist_from_expr_b(re->dbg, b, len, re->cu_psize,
6387 	    re->cu_osize, re->cu_ver, &llbuf, &lcnt, &de) != DW_DLV_OK) {
6388 		warnx("dwarf_loclist_form_expr_b: %s", dwarf_errmsg(de));
6389 		return;
6390 	}
6391 
6392 	for (i = 0; (Dwarf_Half) i < llbuf->ld_cents; i++) {
6393 		dump_dwarf_loc(re, &llbuf->ld_s[i]);
6394 		if (i < llbuf->ld_cents - 1)
6395 			printf("; ");
6396 	}
6397 
6398 	dwarf_dealloc(re->dbg, llbuf->ld_s, DW_DLA_LOC_BLOCK);
6399 	dwarf_dealloc(re->dbg, llbuf, DW_DLA_LOCDESC);
6400 }
6401 
6402 static void
6403 dump_dwarf_loclist(struct readelf *re)
6404 {
6405 	Dwarf_Die die;
6406 	Dwarf_Locdesc **llbuf;
6407 	Dwarf_Unsigned lowpc;
6408 	Dwarf_Signed lcnt;
6409 	Dwarf_Half tag, version, pointer_size, off_size;
6410 	Dwarf_Error de;
6411 	struct loc_at *la;
6412 	int i, j, ret;
6413 
6414 	printf("\nContents of section .debug_loc:\n");
6415 
6416 	/* Search .debug_info section. */
6417 	while ((ret = dwarf_next_cu_header_b(re->dbg, NULL, &version, NULL,
6418 	    &pointer_size, &off_size, NULL, NULL, &de)) == DW_DLV_OK) {
6419 		set_cu_context(re, pointer_size, off_size, version);
6420 		die = NULL;
6421 		if (dwarf_siblingof(re->dbg, die, &die, &de) != DW_DLV_OK)
6422 			continue;
6423 		if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
6424 			warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
6425 			continue;
6426 		}
6427 		/* XXX: What about DW_TAG_partial_unit? */
6428 		lowpc = 0;
6429 		if (tag == DW_TAG_compile_unit) {
6430 			if (dwarf_attrval_unsigned(die, DW_AT_low_pc,
6431 				&lowpc, &de) != DW_DLV_OK)
6432 				lowpc = 0;
6433 		}
6434 
6435 		/* Search attributes for reference to .debug_loc section. */
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 
6441 	/* Search .debug_types section. */
6442 	do {
6443 		while ((ret = dwarf_next_cu_header_c(re->dbg, 0, NULL,
6444 		    &version, NULL, &pointer_size, &off_size, NULL, NULL,
6445 		    NULL, NULL, &de)) == DW_DLV_OK) {
6446 			set_cu_context(re, pointer_size, off_size, version);
6447 			die = NULL;
6448 			if (dwarf_siblingof(re->dbg, die, &die, &de) !=
6449 			    DW_DLV_OK)
6450 				continue;
6451 			if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
6452 				warnx("dwarf_tag failed: %s",
6453 				    dwarf_errmsg(de));
6454 				continue;
6455 			}
6456 
6457 			lowpc = 0;
6458 			if (tag == DW_TAG_type_unit) {
6459 				if (dwarf_attrval_unsigned(die, DW_AT_low_pc,
6460 				    &lowpc, &de) != DW_DLV_OK)
6461 					lowpc = 0;
6462 			}
6463 
6464 			/*
6465 			 * Search attributes for reference to .debug_loc
6466 			 * section.
6467 			 */
6468 			search_loclist_at(re, die, lowpc);
6469 		}
6470 		if (ret == DW_DLV_ERROR)
6471 			warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
6472 	} while (dwarf_next_types_section(re->dbg, &de) == DW_DLV_OK);
6473 
6474 	if (TAILQ_EMPTY(&lalist))
6475 		return;
6476 
6477 	printf("    Offset   Begin    End      Expression\n");
6478 
6479 	TAILQ_FOREACH(la, &lalist, la_next) {
6480 		if (dwarf_loclist_n(la->la_at, &llbuf, &lcnt, &de) !=
6481 		    DW_DLV_OK) {
6482 			warnx("dwarf_loclist_n failed: %s", dwarf_errmsg(de));
6483 			continue;
6484 		}
6485 		set_cu_context(re, la->la_cu_psize, la->la_cu_osize,
6486 		    la->la_cu_ver);
6487 		for (i = 0; i < lcnt; i++) {
6488 			printf("    %8.8jx ", la->la_off);
6489 			if (llbuf[i]->ld_lopc == 0 && llbuf[i]->ld_hipc == 0) {
6490 				printf("<End of list>\n");
6491 				continue;
6492 			}
6493 
6494 			/* TODO: handle base selection entry. */
6495 
6496 			printf("%8.8jx %8.8jx ",
6497 			    (uintmax_t) (la->la_lowpc + llbuf[i]->ld_lopc),
6498 			    (uintmax_t) (la->la_lowpc + llbuf[i]->ld_hipc));
6499 
6500 			putchar('(');
6501 			for (j = 0; (Dwarf_Half) j < llbuf[i]->ld_cents; j++) {
6502 				dump_dwarf_loc(re, &llbuf[i]->ld_s[j]);
6503 				if (j < llbuf[i]->ld_cents - 1)
6504 					printf("; ");
6505 			}
6506 			putchar(')');
6507 
6508 			if (llbuf[i]->ld_lopc == llbuf[i]->ld_hipc)
6509 				printf(" (start == end)");
6510 			putchar('\n');
6511 		}
6512 		for (i = 0; i < lcnt; i++) {
6513 			dwarf_dealloc(re->dbg, llbuf[i]->ld_s,
6514 			    DW_DLA_LOC_BLOCK);
6515 			dwarf_dealloc(re->dbg, llbuf[i], DW_DLA_LOCDESC);
6516 		}
6517 		dwarf_dealloc(re->dbg, llbuf, DW_DLA_LIST);
6518 	}
6519 }
6520 
6521 /*
6522  * Retrieve a string using string table section index and the string offset.
6523  */
6524 static const char*
6525 get_string(struct readelf *re, int strtab, size_t off)
6526 {
6527 	const char *name;
6528 
6529 	if ((name = elf_strptr(re->elf, strtab, off)) == NULL)
6530 		return ("");
6531 
6532 	return (name);
6533 }
6534 
6535 /*
6536  * Retrieve the name of a symbol using the section index of the symbol
6537  * table and the index of the symbol within that table.
6538  */
6539 static const char *
6540 get_symbol_name(struct readelf *re, int symtab, int i)
6541 {
6542 	struct section	*s;
6543 	const char	*name;
6544 	GElf_Sym	 sym;
6545 	Elf_Data	*data;
6546 	int		 elferr;
6547 
6548 	s = &re->sl[symtab];
6549 	if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM)
6550 		return ("");
6551 	(void) elf_errno();
6552 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
6553 		elferr = elf_errno();
6554 		if (elferr != 0)
6555 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
6556 		return ("");
6557 	}
6558 	if (gelf_getsym(data, i, &sym) != &sym)
6559 		return ("");
6560 	/* Return section name for STT_SECTION symbol. */
6561 	if (GELF_ST_TYPE(sym.st_info) == STT_SECTION &&
6562 	    re->sl[sym.st_shndx].name != NULL)
6563 		return (re->sl[sym.st_shndx].name);
6564 	if ((name = elf_strptr(re->elf, s->link, sym.st_name)) == NULL)
6565 		return ("");
6566 
6567 	return (name);
6568 }
6569 
6570 static uint64_t
6571 get_symbol_value(struct readelf *re, int symtab, int i)
6572 {
6573 	struct section	*s;
6574 	GElf_Sym	 sym;
6575 	Elf_Data	*data;
6576 	int		 elferr;
6577 
6578 	s = &re->sl[symtab];
6579 	if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM)
6580 		return (0);
6581 	(void) elf_errno();
6582 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
6583 		elferr = elf_errno();
6584 		if (elferr != 0)
6585 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
6586 		return (0);
6587 	}
6588 	if (gelf_getsym(data, i, &sym) != &sym)
6589 		return (0);
6590 
6591 	return (sym.st_value);
6592 }
6593 
6594 static void
6595 hex_dump(struct readelf *re)
6596 {
6597 	struct section *s;
6598 	Elf_Data *d;
6599 	uint8_t *buf;
6600 	size_t sz, nbytes;
6601 	uint64_t addr;
6602 	int elferr, i, j;
6603 
6604 	for (i = 1; (size_t) i < re->shnum; i++) {
6605 		s = &re->sl[i];
6606 		if (find_dumpop(re, (size_t) i, s->name, HEX_DUMP, -1) == NULL)
6607 			continue;
6608 		(void) elf_errno();
6609 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
6610 			elferr = elf_errno();
6611 			if (elferr != 0)
6612 				warnx("elf_getdata failed: %s",
6613 				    elf_errmsg(elferr));
6614 			continue;
6615 		}
6616 		if (d->d_size <= 0 || d->d_buf == NULL) {
6617 			printf("\nSection '%s' has no data to dump.\n",
6618 			    s->name);
6619 			continue;
6620 		}
6621 		buf = d->d_buf;
6622 		sz = d->d_size;
6623 		addr = s->addr;
6624 		printf("\nHex dump of section '%s':\n", s->name);
6625 		while (sz > 0) {
6626 			printf("  0x%8.8jx ", (uintmax_t)addr);
6627 			nbytes = sz > 16? 16 : sz;
6628 			for (j = 0; j < 16; j++) {
6629 				if ((size_t)j < nbytes)
6630 					printf("%2.2x", buf[j]);
6631 				else
6632 					printf("  ");
6633 				if ((j & 3) == 3)
6634 					printf(" ");
6635 			}
6636 			for (j = 0; (size_t)j < nbytes; j++) {
6637 				if (isprint(buf[j]))
6638 					printf("%c", buf[j]);
6639 				else
6640 					printf(".");
6641 			}
6642 			printf("\n");
6643 			buf += nbytes;
6644 			addr += nbytes;
6645 			sz -= nbytes;
6646 		}
6647 	}
6648 }
6649 
6650 static void
6651 str_dump(struct readelf *re)
6652 {
6653 	struct section *s;
6654 	Elf_Data *d;
6655 	unsigned char *start, *end, *buf_end;
6656 	unsigned int len;
6657 	int i, j, elferr, found;
6658 
6659 	for (i = 1; (size_t) i < re->shnum; i++) {
6660 		s = &re->sl[i];
6661 		if (find_dumpop(re, (size_t) i, s->name, STR_DUMP, -1) == NULL)
6662 			continue;
6663 		(void) elf_errno();
6664 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
6665 			elferr = elf_errno();
6666 			if (elferr != 0)
6667 				warnx("elf_getdata failed: %s",
6668 				    elf_errmsg(elferr));
6669 			continue;
6670 		}
6671 		if (d->d_size <= 0 || d->d_buf == NULL) {
6672 			printf("\nSection '%s' has no data to dump.\n",
6673 			    s->name);
6674 			continue;
6675 		}
6676 		buf_end = (unsigned char *) d->d_buf + d->d_size;
6677 		start = (unsigned char *) d->d_buf;
6678 		found = 0;
6679 		printf("\nString dump of section '%s':\n", s->name);
6680 		for (;;) {
6681 			while (start < buf_end && !isprint(*start))
6682 				start++;
6683 			if (start >= buf_end)
6684 				break;
6685 			end = start + 1;
6686 			while (end < buf_end && isprint(*end))
6687 				end++;
6688 			printf("  [%6lx]  ",
6689 			    (long) (start - (unsigned char *) d->d_buf));
6690 			len = end - start;
6691 			for (j = 0; (unsigned int) j < len; j++)
6692 				putchar(start[j]);
6693 			putchar('\n');
6694 			found = 1;
6695 			if (end >= buf_end)
6696 				break;
6697 			start = end + 1;
6698 		}
6699 		if (!found)
6700 			printf("  No strings found in this section.");
6701 		putchar('\n');
6702 	}
6703 }
6704 
6705 static void
6706 load_sections(struct readelf *re)
6707 {
6708 	struct section	*s;
6709 	const char	*name;
6710 	Elf_Scn		*scn;
6711 	GElf_Shdr	 sh;
6712 	size_t		 shstrndx, ndx;
6713 	int		 elferr;
6714 
6715 	/* Allocate storage for internal section list. */
6716 	if (!elf_getshnum(re->elf, &re->shnum)) {
6717 		warnx("elf_getshnum failed: %s", elf_errmsg(-1));
6718 		return;
6719 	}
6720 	if (re->sl != NULL)
6721 		free(re->sl);
6722 	if ((re->sl = calloc(re->shnum, sizeof(*re->sl))) == NULL)
6723 		err(EXIT_FAILURE, "calloc failed");
6724 
6725 	/* Get the index of .shstrtab section. */
6726 	if (!elf_getshstrndx(re->elf, &shstrndx)) {
6727 		warnx("elf_getshstrndx failed: %s", elf_errmsg(-1));
6728 		return;
6729 	}
6730 
6731 	if ((scn = elf_getscn(re->elf, 0)) == NULL)
6732 		return;
6733 
6734 	(void) elf_errno();
6735 	do {
6736 		if (gelf_getshdr(scn, &sh) == NULL) {
6737 			warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
6738 			(void) elf_errno();
6739 			continue;
6740 		}
6741 		if ((name = elf_strptr(re->elf, shstrndx, sh.sh_name)) == NULL) {
6742 			(void) elf_errno();
6743 			name = "ERROR";
6744 		}
6745 		if ((ndx = elf_ndxscn(scn)) == SHN_UNDEF) {
6746 			if ((elferr = elf_errno()) != 0)
6747 				warnx("elf_ndxscn failed: %s",
6748 				    elf_errmsg(elferr));
6749 			continue;
6750 		}
6751 		if (ndx >= re->shnum) {
6752 			warnx("section index of '%s' out of range", name);
6753 			continue;
6754 		}
6755 		s = &re->sl[ndx];
6756 		s->name = name;
6757 		s->scn = scn;
6758 		s->off = sh.sh_offset;
6759 		s->sz = sh.sh_size;
6760 		s->entsize = sh.sh_entsize;
6761 		s->align = sh.sh_addralign;
6762 		s->type = sh.sh_type;
6763 		s->flags = sh.sh_flags;
6764 		s->addr = sh.sh_addr;
6765 		s->link = sh.sh_link;
6766 		s->info = sh.sh_info;
6767 	} while ((scn = elf_nextscn(re->elf, scn)) != NULL);
6768 	elferr = elf_errno();
6769 	if (elferr != 0)
6770 		warnx("elf_nextscn failed: %s", elf_errmsg(elferr));
6771 }
6772 
6773 static void
6774 unload_sections(struct readelf *re)
6775 {
6776 
6777 	if (re->sl != NULL) {
6778 		free(re->sl);
6779 		re->sl = NULL;
6780 	}
6781 	re->shnum = 0;
6782 	re->vd_s = NULL;
6783 	re->vn_s = NULL;
6784 	re->vs_s = NULL;
6785 	re->vs = NULL;
6786 	re->vs_sz = 0;
6787 	if (re->ver != NULL) {
6788 		free(re->ver);
6789 		re->ver = NULL;
6790 		re->ver_sz = 0;
6791 	}
6792 }
6793 
6794 static void
6795 dump_elf(struct readelf *re)
6796 {
6797 
6798 	/* Fetch ELF header. No need to continue if it fails. */
6799 	if (gelf_getehdr(re->elf, &re->ehdr) == NULL) {
6800 		warnx("gelf_getehdr failed: %s", elf_errmsg(-1));
6801 		return;
6802 	}
6803 	if ((re->ec = gelf_getclass(re->elf)) == ELFCLASSNONE) {
6804 		warnx("gelf_getclass failed: %s", elf_errmsg(-1));
6805 		return;
6806 	}
6807 	if (re->ehdr.e_ident[EI_DATA] == ELFDATA2MSB) {
6808 		re->dw_read = _read_msb;
6809 		re->dw_decode = _decode_msb;
6810 	} else {
6811 		re->dw_read = _read_lsb;
6812 		re->dw_decode = _decode_lsb;
6813 	}
6814 
6815 	if (re->options & ~RE_H)
6816 		load_sections(re);
6817 	if ((re->options & RE_VV) || (re->options & RE_S))
6818 		search_ver(re);
6819 	if (re->options & RE_H)
6820 		dump_ehdr(re);
6821 	if (re->options & RE_L)
6822 		dump_phdr(re);
6823 	if (re->options & RE_SS)
6824 		dump_shdr(re);
6825 	if (re->options & RE_D)
6826 		dump_dynamic(re);
6827 	if (re->options & RE_R)
6828 		dump_reloc(re);
6829 	if (re->options & RE_S)
6830 		dump_symtabs(re);
6831 	if (re->options & RE_N)
6832 		dump_notes(re);
6833 	if (re->options & RE_II)
6834 		dump_hash(re);
6835 	if (re->options & RE_X)
6836 		hex_dump(re);
6837 	if (re->options & RE_P)
6838 		str_dump(re);
6839 	if (re->options & RE_VV)
6840 		dump_ver(re);
6841 	if (re->options & RE_AA)
6842 		dump_arch_specific_info(re);
6843 	if (re->options & RE_W)
6844 		dump_dwarf(re);
6845 	if (re->options & ~RE_H)
6846 		unload_sections(re);
6847 }
6848 
6849 static void
6850 dump_dwarf(struct readelf *re)
6851 {
6852 	int error;
6853 	Dwarf_Error de;
6854 
6855 	if (dwarf_elf_init(re->elf, DW_DLC_READ, NULL, NULL, &re->dbg, &de)) {
6856 		if ((error = dwarf_errno(de)) != DW_DLE_DEBUG_INFO_NULL)
6857 			errx(EXIT_FAILURE, "dwarf_elf_init failed: %s",
6858 			    dwarf_errmsg(de));
6859 		return;
6860 	}
6861 
6862 	if (re->dop & DW_A)
6863 		dump_dwarf_abbrev(re);
6864 	if (re->dop & DW_L)
6865 		dump_dwarf_line(re);
6866 	if (re->dop & DW_LL)
6867 		dump_dwarf_line_decoded(re);
6868 	if (re->dop & DW_I) {
6869 		dump_dwarf_info(re, 0);
6870 		dump_dwarf_info(re, 1);
6871 	}
6872 	if (re->dop & DW_P)
6873 		dump_dwarf_pubnames(re);
6874 	if (re->dop & DW_R)
6875 		dump_dwarf_aranges(re);
6876 	if (re->dop & DW_RR)
6877 		dump_dwarf_ranges(re);
6878 	if (re->dop & DW_M)
6879 		dump_dwarf_macinfo(re);
6880 	if (re->dop & DW_F)
6881 		dump_dwarf_frame(re, 0);
6882 	else if (re->dop & DW_FF)
6883 		dump_dwarf_frame(re, 1);
6884 	if (re->dop & DW_S)
6885 		dump_dwarf_str(re);
6886 	if (re->dop & DW_O)
6887 		dump_dwarf_loclist(re);
6888 
6889 	dwarf_finish(re->dbg, &de);
6890 }
6891 
6892 static void
6893 dump_ar(struct readelf *re, int fd)
6894 {
6895 	Elf_Arsym *arsym;
6896 	Elf_Arhdr *arhdr;
6897 	Elf_Cmd cmd;
6898 	Elf *e;
6899 	size_t sz;
6900 	off_t off;
6901 	int i;
6902 
6903 	re->ar = re->elf;
6904 
6905 	if (re->options & RE_C) {
6906 		if ((arsym = elf_getarsym(re->ar, &sz)) == NULL) {
6907 			warnx("elf_getarsym() failed: %s", elf_errmsg(-1));
6908 			goto process_members;
6909 		}
6910 		printf("Index of archive %s: (%ju entries)\n", re->filename,
6911 		    (uintmax_t) sz - 1);
6912 		off = 0;
6913 		for (i = 0; (size_t) i < sz; i++) {
6914 			if (arsym[i].as_name == NULL)
6915 				break;
6916 			if (arsym[i].as_off != off) {
6917 				off = arsym[i].as_off;
6918 				if (elf_rand(re->ar, off) != off) {
6919 					warnx("elf_rand() failed: %s",
6920 					    elf_errmsg(-1));
6921 					continue;
6922 				}
6923 				if ((e = elf_begin(fd, ELF_C_READ, re->ar)) ==
6924 				    NULL) {
6925 					warnx("elf_begin() failed: %s",
6926 					    elf_errmsg(-1));
6927 					continue;
6928 				}
6929 				if ((arhdr = elf_getarhdr(e)) == NULL) {
6930 					warnx("elf_getarhdr() failed: %s",
6931 					    elf_errmsg(-1));
6932 					elf_end(e);
6933 					continue;
6934 				}
6935 				printf("Binary %s(%s) contains:\n",
6936 				    re->filename, arhdr->ar_name);
6937 			}
6938 			printf("\t%s\n", arsym[i].as_name);
6939 		}
6940 		if (elf_rand(re->ar, SARMAG) != SARMAG) {
6941 			warnx("elf_rand() failed: %s", elf_errmsg(-1));
6942 			return;
6943 		}
6944 	}
6945 
6946 process_members:
6947 
6948 	if ((re->options & ~RE_C) == 0)
6949 		return;
6950 
6951 	cmd = ELF_C_READ;
6952 	while ((re->elf = elf_begin(fd, cmd, re->ar)) != NULL) {
6953 		if ((arhdr = elf_getarhdr(re->elf)) == NULL) {
6954 			warnx("elf_getarhdr() failed: %s", elf_errmsg(-1));
6955 			goto next_member;
6956 		}
6957 		if (strcmp(arhdr->ar_name, "/") == 0 ||
6958 		    strcmp(arhdr->ar_name, "//") == 0 ||
6959 		    strcmp(arhdr->ar_name, "__.SYMDEF") == 0)
6960 			goto next_member;
6961 		printf("\nFile: %s(%s)\n", re->filename, arhdr->ar_name);
6962 		dump_elf(re);
6963 
6964 	next_member:
6965 		cmd = elf_next(re->elf);
6966 		elf_end(re->elf);
6967 	}
6968 	re->elf = re->ar;
6969 }
6970 
6971 static void
6972 dump_object(struct readelf *re)
6973 {
6974 	int fd;
6975 
6976 	if ((fd = open(re->filename, O_RDONLY)) == -1) {
6977 		warn("open %s failed", re->filename);
6978 		return;
6979 	}
6980 
6981 	if ((re->flags & DISPLAY_FILENAME) != 0)
6982 		printf("\nFile: %s\n", re->filename);
6983 
6984 	if ((re->elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
6985 		warnx("elf_begin() failed: %s", elf_errmsg(-1));
6986 		return;
6987 	}
6988 
6989 	switch (elf_kind(re->elf)) {
6990 	case ELF_K_NONE:
6991 		warnx("Not an ELF file.");
6992 		return;
6993 	case ELF_K_ELF:
6994 		dump_elf(re);
6995 		break;
6996 	case ELF_K_AR:
6997 		dump_ar(re, fd);
6998 		break;
6999 	default:
7000 		warnx("Internal: libelf returned unknown elf kind.");
7001 		return;
7002 	}
7003 
7004 	elf_end(re->elf);
7005 }
7006 
7007 static void
7008 add_dumpop(struct readelf *re, size_t si, const char *sn, int op, int t)
7009 {
7010 	struct dumpop *d;
7011 
7012 	if ((d = find_dumpop(re, si, sn, -1, t)) == NULL) {
7013 		if ((d = calloc(1, sizeof(*d))) == NULL)
7014 			err(EXIT_FAILURE, "calloc failed");
7015 		if (t == DUMP_BY_INDEX)
7016 			d->u.si = si;
7017 		else
7018 			d->u.sn = sn;
7019 		d->type = t;
7020 		d->op = op;
7021 		STAILQ_INSERT_TAIL(&re->v_dumpop, d, dumpop_list);
7022 	} else
7023 		d->op |= op;
7024 }
7025 
7026 static struct dumpop *
7027 find_dumpop(struct readelf *re, size_t si, const char *sn, int op, int t)
7028 {
7029 	struct dumpop *d;
7030 
7031 	STAILQ_FOREACH(d, &re->v_dumpop, dumpop_list) {
7032 		if ((op == -1 || op & d->op) &&
7033 		    (t == -1 || (unsigned) t == d->type)) {
7034 			if ((d->type == DUMP_BY_INDEX && d->u.si == si) ||
7035 			    (d->type == DUMP_BY_NAME && !strcmp(d->u.sn, sn)))
7036 				return (d);
7037 		}
7038 	}
7039 
7040 	return (NULL);
7041 }
7042 
7043 static struct {
7044 	const char *ln;
7045 	char sn;
7046 	int value;
7047 } dwarf_op[] = {
7048 	{"rawline", 'l', DW_L},
7049 	{"decodedline", 'L', DW_LL},
7050 	{"info", 'i', DW_I},
7051 	{"abbrev", 'a', DW_A},
7052 	{"pubnames", 'p', DW_P},
7053 	{"aranges", 'r', DW_R},
7054 	{"ranges", 'r', DW_R},
7055 	{"Ranges", 'R', DW_RR},
7056 	{"macro", 'm', DW_M},
7057 	{"frames", 'f', DW_F},
7058 	{"frames-interp", 'F', DW_FF},
7059 	{"str", 's', DW_S},
7060 	{"loc", 'o', DW_O},
7061 	{NULL, 0, 0}
7062 };
7063 
7064 static void
7065 parse_dwarf_op_short(struct readelf *re, const char *op)
7066 {
7067 	int i;
7068 
7069 	if (op == NULL) {
7070 		re->dop |= DW_DEFAULT_OPTIONS;
7071 		return;
7072 	}
7073 
7074 	for (; *op != '\0'; op++) {
7075 		for (i = 0; dwarf_op[i].ln != NULL; i++) {
7076 			if (dwarf_op[i].sn == *op) {
7077 				re->dop |= dwarf_op[i].value;
7078 				break;
7079 			}
7080 		}
7081 	}
7082 }
7083 
7084 static void
7085 parse_dwarf_op_long(struct readelf *re, const char *op)
7086 {
7087 	char *p, *token, *bp;
7088 	int i;
7089 
7090 	if (op == NULL) {
7091 		re->dop |= DW_DEFAULT_OPTIONS;
7092 		return;
7093 	}
7094 
7095 	if ((p = strdup(op)) == NULL)
7096 		err(EXIT_FAILURE, "strdup failed");
7097 	bp = p;
7098 
7099 	while ((token = strsep(&p, ",")) != NULL) {
7100 		for (i = 0; dwarf_op[i].ln != NULL; i++) {
7101 			if (!strcmp(token, dwarf_op[i].ln)) {
7102 				re->dop |= dwarf_op[i].value;
7103 				break;
7104 			}
7105 		}
7106 	}
7107 
7108 	free(bp);
7109 }
7110 
7111 static uint64_t
7112 _read_lsb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read)
7113 {
7114 	uint64_t ret;
7115 	uint8_t *src;
7116 
7117 	src = (uint8_t *) d->d_buf + *offsetp;
7118 
7119 	ret = 0;
7120 	switch (bytes_to_read) {
7121 	case 8:
7122 		ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
7123 		ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
7124 	case 4:
7125 		ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
7126 	case 2:
7127 		ret |= ((uint64_t) src[1]) << 8;
7128 	case 1:
7129 		ret |= src[0];
7130 		break;
7131 	default:
7132 		return (0);
7133 	}
7134 
7135 	*offsetp += bytes_to_read;
7136 
7137 	return (ret);
7138 }
7139 
7140 static uint64_t
7141 _read_msb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read)
7142 {
7143 	uint64_t ret;
7144 	uint8_t *src;
7145 
7146 	src = (uint8_t *) d->d_buf + *offsetp;
7147 
7148 	switch (bytes_to_read) {
7149 	case 1:
7150 		ret = src[0];
7151 		break;
7152 	case 2:
7153 		ret = src[1] | ((uint64_t) src[0]) << 8;
7154 		break;
7155 	case 4:
7156 		ret = src[3] | ((uint64_t) src[2]) << 8;
7157 		ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24;
7158 		break;
7159 	case 8:
7160 		ret = src[7] | ((uint64_t) src[6]) << 8;
7161 		ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24;
7162 		ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40;
7163 		ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56;
7164 		break;
7165 	default:
7166 		return (0);
7167 	}
7168 
7169 	*offsetp += bytes_to_read;
7170 
7171 	return (ret);
7172 }
7173 
7174 static uint64_t
7175 _decode_lsb(uint8_t **data, int bytes_to_read)
7176 {
7177 	uint64_t ret;
7178 	uint8_t *src;
7179 
7180 	src = *data;
7181 
7182 	ret = 0;
7183 	switch (bytes_to_read) {
7184 	case 8:
7185 		ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
7186 		ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
7187 	case 4:
7188 		ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
7189 	case 2:
7190 		ret |= ((uint64_t) src[1]) << 8;
7191 	case 1:
7192 		ret |= src[0];
7193 		break;
7194 	default:
7195 		return (0);
7196 	}
7197 
7198 	*data += bytes_to_read;
7199 
7200 	return (ret);
7201 }
7202 
7203 static uint64_t
7204 _decode_msb(uint8_t **data, int bytes_to_read)
7205 {
7206 	uint64_t ret;
7207 	uint8_t *src;
7208 
7209 	src = *data;
7210 
7211 	ret = 0;
7212 	switch (bytes_to_read) {
7213 	case 1:
7214 		ret = src[0];
7215 		break;
7216 	case 2:
7217 		ret = src[1] | ((uint64_t) src[0]) << 8;
7218 		break;
7219 	case 4:
7220 		ret = src[3] | ((uint64_t) src[2]) << 8;
7221 		ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24;
7222 		break;
7223 	case 8:
7224 		ret = src[7] | ((uint64_t) src[6]) << 8;
7225 		ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24;
7226 		ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40;
7227 		ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56;
7228 		break;
7229 	default:
7230 		return (0);
7231 		break;
7232 	}
7233 
7234 	*data += bytes_to_read;
7235 
7236 	return (ret);
7237 }
7238 
7239 static int64_t
7240 _decode_sleb128(uint8_t **dp)
7241 {
7242 	int64_t ret = 0;
7243 	uint8_t b;
7244 	int shift = 0;
7245 
7246 	uint8_t *src = *dp;
7247 
7248 	do {
7249 		b = *src++;
7250 		ret |= ((b & 0x7f) << shift);
7251 		shift += 7;
7252 	} while ((b & 0x80) != 0);
7253 
7254 	if (shift < 32 && (b & 0x40) != 0)
7255 		ret |= (-1 << shift);
7256 
7257 	*dp = src;
7258 
7259 	return (ret);
7260 }
7261 
7262 static uint64_t
7263 _decode_uleb128(uint8_t **dp)
7264 {
7265 	uint64_t ret = 0;
7266 	uint8_t b;
7267 	int shift = 0;
7268 
7269 	uint8_t *src = *dp;
7270 
7271 	do {
7272 		b = *src++;
7273 		ret |= ((b & 0x7f) << shift);
7274 		shift += 7;
7275 	} while ((b & 0x80) != 0);
7276 
7277 	*dp = src;
7278 
7279 	return (ret);
7280 }
7281 
7282 static void
7283 readelf_version(void)
7284 {
7285 	(void) printf("%s (%s)\n", ELFTC_GETPROGNAME(),
7286 	    elftc_version());
7287 	exit(EXIT_SUCCESS);
7288 }
7289 
7290 #define	USAGE_MESSAGE	"\
7291 Usage: %s [options] file...\n\
7292   Display information about ELF objects and ar(1) archives.\n\n\
7293   Options:\n\
7294   -a | --all               Equivalent to specifying options '-dhIlrsASV'.\n\
7295   -c | --archive-index     Print the archive symbol table for archives.\n\
7296   -d | --dynamic           Print the contents of SHT_DYNAMIC sections.\n\
7297   -e | --headers           Print all headers in the object.\n\
7298   -g | --section-groups    (accepted, but ignored)\n\
7299   -h | --file-header       Print the file header for the object.\n\
7300   -l | --program-headers   Print the PHDR table for the object.\n\
7301   -n | --notes             Print the contents of SHT_NOTE sections.\n\
7302   -p INDEX | --string-dump=INDEX\n\
7303                            Print the contents of section at index INDEX.\n\
7304   -r | --relocs            Print relocation information.\n\
7305   -s | --syms | --symbols  Print symbol tables.\n\
7306   -t | --section-details   Print additional information about sections.\n\
7307   -v | --version           Print a version identifier and exit.\n\
7308   -x INDEX | --hex-dump=INDEX\n\
7309                            Display contents of a section as hexadecimal.\n\
7310   -A | --arch-specific     (accepted, but ignored)\n\
7311   -D | --use-dynamic       Print the symbol table specified by the DT_SYMTAB\n\
7312                            entry in the \".dynamic\" section.\n\
7313   -H | --help              Print a help message.\n\
7314   -I | --histogram         Print information on bucket list lengths for \n\
7315                            hash sections.\n\
7316   -N | --full-section-name (accepted, but ignored)\n\
7317   -S | --sections | --section-headers\n\
7318                            Print information about section headers.\n\
7319   -V | --version-info      Print symbol versoning information.\n\
7320   -W | --wide              Print information without wrapping long lines.\n"
7321 
7322 
7323 static void
7324 readelf_usage(void)
7325 {
7326 	fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());
7327 	exit(EXIT_FAILURE);
7328 }
7329 
7330 int
7331 main(int argc, char **argv)
7332 {
7333 	struct readelf	*re, re_storage;
7334 	unsigned long	 si;
7335 	int		 opt, i;
7336 	char		*ep;
7337 
7338 	re = &re_storage;
7339 	memset(re, 0, sizeof(*re));
7340 	STAILQ_INIT(&re->v_dumpop);
7341 
7342 	while ((opt = getopt_long(argc, argv, "AacDdegHhIi:lNnp:rSstuVvWw::x:",
7343 	    longopts, NULL)) != -1) {
7344 		switch(opt) {
7345 		case '?':
7346 			readelf_usage();
7347 			break;
7348 		case 'A':
7349 			re->options |= RE_AA;
7350 			break;
7351 		case 'a':
7352 			re->options |= RE_AA | RE_D | RE_H | RE_II | RE_L |
7353 			    RE_R | RE_SS | RE_S | RE_VV;
7354 			break;
7355 		case 'c':
7356 			re->options |= RE_C;
7357 			break;
7358 		case 'D':
7359 			re->options |= RE_DD;
7360 			break;
7361 		case 'd':
7362 			re->options |= RE_D;
7363 			break;
7364 		case 'e':
7365 			re->options |= RE_H | RE_L | RE_SS;
7366 			break;
7367 		case 'g':
7368 			re->options |= RE_G;
7369 			break;
7370 		case 'H':
7371 			readelf_usage();
7372 			break;
7373 		case 'h':
7374 			re->options |= RE_H;
7375 			break;
7376 		case 'I':
7377 			re->options |= RE_II;
7378 			break;
7379 		case 'i':
7380 			/* Not implemented yet. */
7381 			break;
7382 		case 'l':
7383 			re->options |= RE_L;
7384 			break;
7385 		case 'N':
7386 			re->options |= RE_NN;
7387 			break;
7388 		case 'n':
7389 			re->options |= RE_N;
7390 			break;
7391 		case 'p':
7392 			re->options |= RE_P;
7393 			si = strtoul(optarg, &ep, 10);
7394 			if (*ep == '\0')
7395 				add_dumpop(re, (size_t) si, NULL, STR_DUMP,
7396 				    DUMP_BY_INDEX);
7397 			else
7398 				add_dumpop(re, 0, optarg, STR_DUMP,
7399 				    DUMP_BY_NAME);
7400 			break;
7401 		case 'r':
7402 			re->options |= RE_R;
7403 			break;
7404 		case 'S':
7405 			re->options |= RE_SS;
7406 			break;
7407 		case 's':
7408 			re->options |= RE_S;
7409 			break;
7410 		case 't':
7411 			re->options |= RE_T;
7412 			break;
7413 		case 'u':
7414 			re->options |= RE_U;
7415 			break;
7416 		case 'V':
7417 			re->options |= RE_VV;
7418 			break;
7419 		case 'v':
7420 			readelf_version();
7421 			break;
7422 		case 'W':
7423 			re->options |= RE_WW;
7424 			break;
7425 		case 'w':
7426 			re->options |= RE_W;
7427 			parse_dwarf_op_short(re, optarg);
7428 			break;
7429 		case 'x':
7430 			re->options |= RE_X;
7431 			si = strtoul(optarg, &ep, 10);
7432 			if (*ep == '\0')
7433 				add_dumpop(re, (size_t) si, NULL, HEX_DUMP,
7434 				    DUMP_BY_INDEX);
7435 			else
7436 				add_dumpop(re, 0, optarg, HEX_DUMP,
7437 				    DUMP_BY_NAME);
7438 			break;
7439 		case OPTION_DEBUG_DUMP:
7440 			re->options |= RE_W;
7441 			parse_dwarf_op_long(re, optarg);
7442 		}
7443 	}
7444 
7445 	argv += optind;
7446 	argc -= optind;
7447 
7448 	if (argc == 0 || re->options == 0)
7449 		readelf_usage();
7450 
7451 	if (argc > 1)
7452 		re->flags |= DISPLAY_FILENAME;
7453 
7454 	if (elf_version(EV_CURRENT) == EV_NONE)
7455 		errx(EXIT_FAILURE, "ELF library initialization failed: %s",
7456 		    elf_errmsg(-1));
7457 
7458 	for (i = 0; i < argc; i++)
7459 		if (argv[i] != NULL) {
7460 			re->filename = argv[i];
7461 			dump_object(re);
7462 		}
7463 
7464 	exit(EXIT_SUCCESS);
7465 }
7466