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