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