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