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