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