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