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