xref: /freebsd/contrib/elftoolchain/readelf/readelf.c (revision 3f0e38d70cd2f01a69d870dac89ece4950e16f36)
12c23cb7cSEd Maste /*-
23ef90571SEd Maste  * Copyright (c) 2009-2015 Kai Wang
32c23cb7cSEd Maste  * All rights reserved.
42c23cb7cSEd Maste  *
52c23cb7cSEd Maste  * Redistribution and use in source and binary forms, with or without
62c23cb7cSEd Maste  * modification, are permitted provided that the following conditions
72c23cb7cSEd Maste  * are met:
82c23cb7cSEd Maste  * 1. Redistributions of source code must retain the above copyright
92c23cb7cSEd Maste  *    notice, this list of conditions and the following disclaimer.
102c23cb7cSEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
112c23cb7cSEd Maste  *    notice, this list of conditions and the following disclaimer in the
122c23cb7cSEd Maste  *    documentation and/or other materials provided with the distribution.
132c23cb7cSEd Maste  *
142c23cb7cSEd Maste  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
152c23cb7cSEd Maste  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
162c23cb7cSEd Maste  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172c23cb7cSEd Maste  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
182c23cb7cSEd Maste  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
192c23cb7cSEd Maste  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
202c23cb7cSEd Maste  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212c23cb7cSEd Maste  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222c23cb7cSEd Maste  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232c23cb7cSEd Maste  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
242c23cb7cSEd Maste  * SUCH DAMAGE.
252c23cb7cSEd Maste  */
262c23cb7cSEd Maste 
272c23cb7cSEd Maste #include <sys/param.h>
282c23cb7cSEd Maste #include <sys/queue.h>
292c23cb7cSEd Maste #include <ar.h>
3071edbbfdSEd Maste #include <assert.h>
312c23cb7cSEd Maste #include <ctype.h>
322c23cb7cSEd Maste #include <dwarf.h>
332c23cb7cSEd Maste #include <err.h>
342c23cb7cSEd Maste #include <fcntl.h>
352c23cb7cSEd Maste #include <gelf.h>
362c23cb7cSEd Maste #include <getopt.h>
372c23cb7cSEd Maste #include <libdwarf.h>
382c23cb7cSEd Maste #include <libelftc.h>
392c23cb7cSEd Maste #include <libgen.h>
402c23cb7cSEd Maste #include <stdarg.h>
413dc58d9cSEd Maste #include <stdint.h>
422c23cb7cSEd Maste #include <stdio.h>
432c23cb7cSEd Maste #include <stdlib.h>
442c23cb7cSEd Maste #include <string.h>
452c23cb7cSEd Maste #include <time.h>
462c23cb7cSEd Maste #include <unistd.h>
472c23cb7cSEd Maste 
482c23cb7cSEd Maste #include "_elftc.h"
492c23cb7cSEd Maste 
50ae500c1fSEd Maste ELFTC_VCSID("$Id: readelf.c 3649 2018-11-24 03:26:23Z emaste $");
51839529caSEd Maste 
52839529caSEd Maste /* Backwards compatability for older FreeBSD releases. */
53839529caSEd Maste #ifndef	STB_GNU_UNIQUE
54839529caSEd Maste #define	STB_GNU_UNIQUE 10
55839529caSEd Maste #endif
56839529caSEd Maste #ifndef	STT_SPARC_REGISTER
57839529caSEd Maste #define	STT_SPARC_REGISTER 13
58839529caSEd Maste #endif
59839529caSEd Maste 
602c23cb7cSEd Maste 
612c23cb7cSEd Maste /*
622c23cb7cSEd Maste  * readelf(1) options.
632c23cb7cSEd Maste  */
642c23cb7cSEd Maste #define	RE_AA	0x00000001
652c23cb7cSEd Maste #define	RE_C	0x00000002
662c23cb7cSEd Maste #define	RE_DD	0x00000004
672c23cb7cSEd Maste #define	RE_D	0x00000008
682c23cb7cSEd Maste #define	RE_G	0x00000010
692c23cb7cSEd Maste #define	RE_H	0x00000020
702c23cb7cSEd Maste #define	RE_II	0x00000040
712c23cb7cSEd Maste #define	RE_I	0x00000080
722c23cb7cSEd Maste #define	RE_L	0x00000100
732c23cb7cSEd Maste #define	RE_NN	0x00000200
742c23cb7cSEd Maste #define	RE_N	0x00000400
752c23cb7cSEd Maste #define	RE_P	0x00000800
762c23cb7cSEd Maste #define	RE_R	0x00001000
772c23cb7cSEd Maste #define	RE_SS	0x00002000
782c23cb7cSEd Maste #define	RE_S	0x00004000
792c23cb7cSEd Maste #define	RE_T	0x00008000
802c23cb7cSEd Maste #define	RE_U	0x00010000
812c23cb7cSEd Maste #define	RE_VV	0x00020000
822c23cb7cSEd Maste #define	RE_WW	0x00040000
832c23cb7cSEd Maste #define	RE_W	0x00080000
842c23cb7cSEd Maste #define	RE_X	0x00100000
852c23cb7cSEd Maste 
862c23cb7cSEd Maste /*
872c23cb7cSEd Maste  * dwarf dump options.
882c23cb7cSEd Maste  */
892c23cb7cSEd Maste #define	DW_A	0x00000001
902c23cb7cSEd Maste #define	DW_FF	0x00000002
912c23cb7cSEd Maste #define	DW_F	0x00000004
922c23cb7cSEd Maste #define	DW_I	0x00000008
932c23cb7cSEd Maste #define	DW_LL	0x00000010
942c23cb7cSEd Maste #define	DW_L	0x00000020
952c23cb7cSEd Maste #define	DW_M	0x00000040
962c23cb7cSEd Maste #define	DW_O	0x00000080
972c23cb7cSEd Maste #define	DW_P	0x00000100
982c23cb7cSEd Maste #define	DW_RR	0x00000200
992c23cb7cSEd Maste #define	DW_R	0x00000400
1002c23cb7cSEd Maste #define	DW_S	0x00000800
1012c23cb7cSEd Maste 
1022c23cb7cSEd Maste #define	DW_DEFAULT_OPTIONS (DW_A | DW_F | DW_I | DW_L | DW_O | DW_P | \
1032c23cb7cSEd Maste 	    DW_R | DW_RR | DW_S)
1042c23cb7cSEd Maste 
1052c23cb7cSEd Maste /*
1062c23cb7cSEd Maste  * readelf(1) run control flags.
1072c23cb7cSEd Maste  */
1082c23cb7cSEd Maste #define	DISPLAY_FILENAME	0x0001
1092c23cb7cSEd Maste 
1102c23cb7cSEd Maste /*
1112c23cb7cSEd Maste  * Internal data structure for sections.
1122c23cb7cSEd Maste  */
1132c23cb7cSEd Maste struct section {
1142c23cb7cSEd Maste 	const char	*name;		/* section name */
1152c23cb7cSEd Maste 	Elf_Scn		*scn;		/* section scn */
1162c23cb7cSEd Maste 	uint64_t	 off;		/* section offset */
1172c23cb7cSEd Maste 	uint64_t	 sz;		/* section size */
1182c23cb7cSEd Maste 	uint64_t	 entsize;	/* section entsize */
1192c23cb7cSEd Maste 	uint64_t	 align;		/* section alignment */
1202c23cb7cSEd Maste 	uint64_t	 type;		/* section type */
1212c23cb7cSEd Maste 	uint64_t	 flags;		/* section flags */
1222c23cb7cSEd Maste 	uint64_t	 addr;		/* section virtual addr */
1232c23cb7cSEd Maste 	uint32_t	 link;		/* section link ndx */
1242c23cb7cSEd Maste 	uint32_t	 info;		/* section info ndx */
1252c23cb7cSEd Maste };
1262c23cb7cSEd Maste 
1272c23cb7cSEd Maste struct dumpop {
1282c23cb7cSEd Maste 	union {
1292c23cb7cSEd Maste 		size_t si;		/* section index */
1302c23cb7cSEd Maste 		const char *sn;		/* section name */
1312c23cb7cSEd Maste 	} u;
1322c23cb7cSEd Maste 	enum {
1332c23cb7cSEd Maste 		DUMP_BY_INDEX = 0,
1342c23cb7cSEd Maste 		DUMP_BY_NAME
1352c23cb7cSEd Maste 	} type;				/* dump type */
1362c23cb7cSEd Maste #define HEX_DUMP	0x0001
1372c23cb7cSEd Maste #define STR_DUMP	0x0002
1382c23cb7cSEd Maste 	int op;				/* dump operation */
1392c23cb7cSEd Maste 	STAILQ_ENTRY(dumpop) dumpop_list;
1402c23cb7cSEd Maste };
1412c23cb7cSEd Maste 
1422c23cb7cSEd Maste struct symver {
1432c23cb7cSEd Maste 	const char *name;
1442c23cb7cSEd Maste 	int type;
1452c23cb7cSEd Maste };
1462c23cb7cSEd Maste 
1472c23cb7cSEd Maste /*
1482c23cb7cSEd Maste  * Structure encapsulates the global data for readelf(1).
1492c23cb7cSEd Maste  */
1502c23cb7cSEd Maste struct readelf {
1512c23cb7cSEd Maste 	const char	 *filename;	/* current processing file. */
1522c23cb7cSEd Maste 	int		  options;	/* command line options. */
1532c23cb7cSEd Maste 	int		  flags;	/* run control flags. */
1542c23cb7cSEd Maste 	int		  dop;		/* dwarf dump options. */
1552c23cb7cSEd Maste 	Elf		 *elf;		/* underlying ELF descriptor. */
1562c23cb7cSEd Maste 	Elf		 *ar;		/* archive ELF descriptor. */
1572c23cb7cSEd Maste 	Dwarf_Debug	  dbg;		/* DWARF handle. */
158cf781b2eSEd Maste 	Dwarf_Half	  cu_psize;	/* DWARF CU pointer size. */
159cf781b2eSEd Maste 	Dwarf_Half	  cu_osize;	/* DWARF CU offset size. */
160cf781b2eSEd Maste 	Dwarf_Half	  cu_ver;	/* DWARF CU version. */
1612c23cb7cSEd Maste 	GElf_Ehdr	  ehdr;		/* ELF header. */
1622c23cb7cSEd Maste 	int		  ec;		/* ELF class. */
1632c23cb7cSEd Maste 	size_t		  shnum;	/* #sections. */
1642c23cb7cSEd Maste 	struct section	 *vd_s;		/* Verdef section. */
1652c23cb7cSEd Maste 	struct section	 *vn_s;		/* Verneed section. */
1662c23cb7cSEd Maste 	struct section	 *vs_s;		/* Versym section. */
1672c23cb7cSEd Maste 	uint16_t	 *vs;		/* Versym array. */
1682c23cb7cSEd Maste 	int		  vs_sz;	/* Versym array size. */
1692c23cb7cSEd Maste 	struct symver	 *ver;		/* Version array. */
1702c23cb7cSEd Maste 	int		  ver_sz;	/* Size of version array. */
1712c23cb7cSEd Maste 	struct section	 *sl;		/* list of sections. */
1722c23cb7cSEd Maste 	STAILQ_HEAD(, dumpop) v_dumpop; /* list of dump ops. */
1732c23cb7cSEd Maste 	uint64_t	(*dw_read)(Elf_Data *, uint64_t *, int);
1742c23cb7cSEd Maste 	uint64_t	(*dw_decode)(uint8_t **, int);
1752c23cb7cSEd Maste };
1762c23cb7cSEd Maste 
1772c23cb7cSEd Maste enum options
1782c23cb7cSEd Maste {
1792c23cb7cSEd Maste 	OPTION_DEBUG_DUMP
1802c23cb7cSEd Maste };
1812c23cb7cSEd Maste 
1822c23cb7cSEd Maste static struct option longopts[] = {
1832c23cb7cSEd Maste 	{"all", no_argument, NULL, 'a'},
1842c23cb7cSEd Maste 	{"arch-specific", no_argument, NULL, 'A'},
1852c23cb7cSEd Maste 	{"archive-index", no_argument, NULL, 'c'},
1862c23cb7cSEd Maste 	{"debug-dump", optional_argument, NULL, OPTION_DEBUG_DUMP},
1872c23cb7cSEd Maste 	{"dynamic", no_argument, NULL, 'd'},
1882c23cb7cSEd Maste 	{"file-header", no_argument, NULL, 'h'},
1892c23cb7cSEd Maste 	{"full-section-name", no_argument, NULL, 'N'},
1902c23cb7cSEd Maste 	{"headers", no_argument, NULL, 'e'},
1912c23cb7cSEd Maste 	{"help", no_argument, 0, 'H'},
1922c23cb7cSEd Maste 	{"hex-dump", required_argument, NULL, 'x'},
1932c23cb7cSEd Maste 	{"histogram", no_argument, NULL, 'I'},
1942c23cb7cSEd Maste 	{"notes", no_argument, NULL, 'n'},
1952c23cb7cSEd Maste 	{"program-headers", no_argument, NULL, 'l'},
1962c23cb7cSEd Maste 	{"relocs", no_argument, NULL, 'r'},
1972c23cb7cSEd Maste 	{"sections", no_argument, NULL, 'S'},
1982c23cb7cSEd Maste 	{"section-headers", no_argument, NULL, 'S'},
1992c23cb7cSEd Maste 	{"section-groups", no_argument, NULL, 'g'},
2002c23cb7cSEd Maste 	{"section-details", no_argument, NULL, 't'},
2012c23cb7cSEd Maste 	{"segments", no_argument, NULL, 'l'},
2022c23cb7cSEd Maste 	{"string-dump", required_argument, NULL, 'p'},
2032c23cb7cSEd Maste 	{"symbols", no_argument, NULL, 's'},
2042c23cb7cSEd Maste 	{"syms", no_argument, NULL, 's'},
2052c23cb7cSEd Maste 	{"unwind", no_argument, NULL, 'u'},
2062c23cb7cSEd Maste 	{"use-dynamic", no_argument, NULL, 'D'},
2072c23cb7cSEd Maste 	{"version-info", no_argument, 0, 'V'},
2082c23cb7cSEd Maste 	{"version", no_argument, 0, 'v'},
2092c23cb7cSEd Maste 	{"wide", no_argument, 0, 'W'},
2102c23cb7cSEd Maste 	{NULL, 0, NULL, 0}
2112c23cb7cSEd Maste };
2122c23cb7cSEd Maste 
2132c23cb7cSEd Maste struct eflags_desc {
2142c23cb7cSEd Maste 	uint64_t flag;
2152c23cb7cSEd Maste 	const char *desc;
2162c23cb7cSEd Maste };
2172c23cb7cSEd Maste 
2182c23cb7cSEd Maste struct mips_option {
2192c23cb7cSEd Maste 	uint64_t flag;
2202c23cb7cSEd Maste 	const char *desc;
2212c23cb7cSEd Maste };
2222c23cb7cSEd Maste 
2236b2779a0SEd Maste struct flag_desc {
2246b2779a0SEd Maste 	uint64_t flag;
2256b2779a0SEd Maste 	const char *desc;
2266b2779a0SEd Maste };
2276b2779a0SEd Maste 
2282c23cb7cSEd Maste static void add_dumpop(struct readelf *re, size_t si, const char *sn, int op,
2292c23cb7cSEd Maste     int t);
2302c23cb7cSEd Maste static const char *aeabi_adv_simd_arch(uint64_t simd);
2312c23cb7cSEd Maste static const char *aeabi_align_needed(uint64_t an);
2322c23cb7cSEd Maste static const char *aeabi_align_preserved(uint64_t ap);
2332c23cb7cSEd Maste static const char *aeabi_arm_isa(uint64_t ai);
2342c23cb7cSEd Maste static const char *aeabi_cpu_arch(uint64_t arch);
2352c23cb7cSEd Maste static const char *aeabi_cpu_arch_profile(uint64_t pf);
2362c23cb7cSEd Maste static const char *aeabi_div(uint64_t du);
2372c23cb7cSEd Maste static const char *aeabi_enum_size(uint64_t es);
2382c23cb7cSEd Maste static const char *aeabi_fp_16bit_format(uint64_t fp16);
2392c23cb7cSEd Maste static const char *aeabi_fp_arch(uint64_t fp);
2402c23cb7cSEd Maste static const char *aeabi_fp_denormal(uint64_t fd);
2412c23cb7cSEd Maste static const char *aeabi_fp_exceptions(uint64_t fe);
2422c23cb7cSEd Maste static const char *aeabi_fp_hpext(uint64_t fh);
2432c23cb7cSEd Maste static const char *aeabi_fp_number_model(uint64_t fn);
2442c23cb7cSEd Maste static const char *aeabi_fp_optm_goal(uint64_t fog);
2452c23cb7cSEd Maste static const char *aeabi_fp_rounding(uint64_t fr);
2462c23cb7cSEd Maste static const char *aeabi_hardfp(uint64_t hfp);
2472c23cb7cSEd Maste static const char *aeabi_mpext(uint64_t mp);
2482c23cb7cSEd Maste static const char *aeabi_optm_goal(uint64_t og);
2492c23cb7cSEd Maste static const char *aeabi_pcs_config(uint64_t pcs);
2502c23cb7cSEd Maste static const char *aeabi_pcs_got(uint64_t got);
2512c23cb7cSEd Maste static const char *aeabi_pcs_r9(uint64_t r9);
2522c23cb7cSEd Maste static const char *aeabi_pcs_ro(uint64_t ro);
2532c23cb7cSEd Maste static const char *aeabi_pcs_rw(uint64_t rw);
2542c23cb7cSEd Maste static const char *aeabi_pcs_wchar_t(uint64_t wt);
2552c23cb7cSEd Maste static const char *aeabi_t2ee(uint64_t t2ee);
2562c23cb7cSEd Maste static const char *aeabi_thumb_isa(uint64_t ti);
2572c23cb7cSEd Maste static const char *aeabi_fp_user_exceptions(uint64_t fu);
2582c23cb7cSEd Maste static const char *aeabi_unaligned_access(uint64_t ua);
2592c23cb7cSEd Maste static const char *aeabi_vfp_args(uint64_t va);
2602c23cb7cSEd Maste static const char *aeabi_virtual(uint64_t vt);
2612c23cb7cSEd Maste static const char *aeabi_wmmx_arch(uint64_t wmmx);
2622c23cb7cSEd Maste static const char *aeabi_wmmx_args(uint64_t wa);
2632c23cb7cSEd Maste static const char *elf_class(unsigned int class);
2642c23cb7cSEd Maste static const char *elf_endian(unsigned int endian);
2652c23cb7cSEd Maste static const char *elf_machine(unsigned int mach);
2662c23cb7cSEd Maste static const char *elf_osabi(unsigned int abi);
2672c23cb7cSEd Maste static const char *elf_type(unsigned int type);
2682c23cb7cSEd Maste static const char *elf_ver(unsigned int ver);
2692c23cb7cSEd Maste static const char *dt_type(unsigned int mach, unsigned int dtype);
2702c23cb7cSEd Maste static void dump_ar(struct readelf *re, int);
2712c23cb7cSEd Maste static void dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe);
2722c23cb7cSEd Maste static void dump_attributes(struct readelf *re);
27395fd7f26SEd Maste static uint8_t *dump_compatibility_tag(uint8_t *p, uint8_t *pe);
2742c23cb7cSEd Maste static void dump_dwarf(struct readelf *re);
275cf781b2eSEd Maste static void dump_dwarf_abbrev(struct readelf *re);
276cf781b2eSEd Maste static void dump_dwarf_aranges(struct readelf *re);
277cf781b2eSEd Maste static void dump_dwarf_block(struct readelf *re, uint8_t *b,
278cf781b2eSEd Maste     Dwarf_Unsigned len);
279cf781b2eSEd Maste static void dump_dwarf_die(struct readelf *re, Dwarf_Die die, int level);
280cf781b2eSEd Maste static void dump_dwarf_frame(struct readelf *re, int alt);
281cf781b2eSEd Maste static void dump_dwarf_frame_inst(struct readelf *re, Dwarf_Cie cie,
282cf781b2eSEd Maste     uint8_t *insts, Dwarf_Unsigned len, Dwarf_Unsigned caf, Dwarf_Signed daf,
283cf781b2eSEd Maste     Dwarf_Addr pc, Dwarf_Debug dbg);
284cf781b2eSEd Maste static int dump_dwarf_frame_regtable(struct readelf *re, Dwarf_Fde fde,
285cf781b2eSEd Maste     Dwarf_Addr pc, Dwarf_Unsigned func_len, Dwarf_Half cie_ra);
286cf781b2eSEd Maste static void dump_dwarf_frame_section(struct readelf *re, struct section *s,
287cf781b2eSEd Maste     int alt);
288cf781b2eSEd Maste static void dump_dwarf_info(struct readelf *re, Dwarf_Bool is_info);
289cf781b2eSEd Maste static void dump_dwarf_macinfo(struct readelf *re);
290cf781b2eSEd Maste static void dump_dwarf_line(struct readelf *re);
291cf781b2eSEd Maste static void dump_dwarf_line_decoded(struct readelf *re);
292cf781b2eSEd Maste static void dump_dwarf_loc(struct readelf *re, Dwarf_Loc *lr);
293cf781b2eSEd Maste static void dump_dwarf_loclist(struct readelf *re);
294cf781b2eSEd Maste static void dump_dwarf_pubnames(struct readelf *re);
295cf781b2eSEd Maste static void dump_dwarf_ranges(struct readelf *re);
296cf781b2eSEd Maste static void dump_dwarf_ranges_foreach(struct readelf *re, Dwarf_Die die,
297cf781b2eSEd Maste     Dwarf_Addr base);
298cf781b2eSEd Maste static void dump_dwarf_str(struct readelf *re);
2992c23cb7cSEd Maste static void dump_eflags(struct readelf *re, uint64_t e_flags);
3002c23cb7cSEd Maste static void dump_elf(struct readelf *re);
3016b2779a0SEd Maste static void dump_flags(struct flag_desc *fd, uint64_t flags);
3022c23cb7cSEd Maste static void dump_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab);
3032c23cb7cSEd Maste static void dump_dynamic(struct readelf *re);
3042c23cb7cSEd Maste static void dump_liblist(struct readelf *re);
305e9f3f88aSEd Maste static void dump_mips_abiflags(struct readelf *re, struct section *s);
3062c23cb7cSEd Maste static void dump_mips_attributes(struct readelf *re, uint8_t *p, uint8_t *pe);
3072c23cb7cSEd Maste static void dump_mips_odk_reginfo(struct readelf *re, uint8_t *p, size_t sz);
3082c23cb7cSEd Maste static void dump_mips_options(struct readelf *re, struct section *s);
3092c23cb7cSEd Maste static void dump_mips_option_flags(const char *name, struct mips_option *opt,
3102c23cb7cSEd Maste     uint64_t info);
3112c23cb7cSEd Maste static void dump_mips_reginfo(struct readelf *re, struct section *s);
3122c23cb7cSEd Maste static void dump_mips_specific_info(struct readelf *re);
3132c23cb7cSEd Maste static void dump_notes(struct readelf *re);
3142c23cb7cSEd Maste static void dump_notes_content(struct readelf *re, const char *buf, size_t sz,
3152c23cb7cSEd Maste     off_t off);
316*3f0e38d7SEd Maste static void dump_notes_data(const char *name, uint32_t type, const char *buf,
317*3f0e38d7SEd Maste     size_t sz);
3182c23cb7cSEd Maste static void dump_svr4_hash(struct section *s);
3192c23cb7cSEd Maste static void dump_svr4_hash64(struct readelf *re, struct section *s);
3202c23cb7cSEd Maste static void dump_gnu_hash(struct readelf *re, struct section *s);
3212c23cb7cSEd Maste static void dump_hash(struct readelf *re);
3222c23cb7cSEd Maste static void dump_phdr(struct readelf *re);
3232c23cb7cSEd Maste static void dump_ppc_attributes(uint8_t *p, uint8_t *pe);
3243ef90571SEd Maste static void dump_section_groups(struct readelf *re);
3252c23cb7cSEd Maste static void dump_symtab(struct readelf *re, int i);
3262c23cb7cSEd Maste static void dump_symtabs(struct readelf *re);
32795fd7f26SEd Maste static uint8_t *dump_unknown_tag(uint64_t tag, uint8_t *p, uint8_t *pe);
3282c23cb7cSEd Maste static void dump_ver(struct readelf *re);
3292c23cb7cSEd Maste static void dump_verdef(struct readelf *re, int dump);
3302c23cb7cSEd Maste static void dump_verneed(struct readelf *re, int dump);
3312c23cb7cSEd Maste static void dump_versym(struct readelf *re);
332cf781b2eSEd Maste static const char *dwarf_reg(unsigned int mach, unsigned int reg);
333cf781b2eSEd Maste static const char *dwarf_regname(struct readelf *re, unsigned int num);
334cf781b2eSEd Maste static struct dumpop *find_dumpop(struct readelf *re, size_t si,
335cf781b2eSEd Maste     const char *sn, int op, int t);
33671edbbfdSEd Maste static int get_ent_count(struct section *s, int *ent_count);
337e9f3f88aSEd Maste static int get_mips_register_size(uint8_t flag);
338cf781b2eSEd Maste static char *get_regoff_str(struct readelf *re, Dwarf_Half reg,
339cf781b2eSEd Maste     Dwarf_Addr off);
3402c23cb7cSEd Maste static const char *get_string(struct readelf *re, int strtab, size_t off);
3412c23cb7cSEd Maste static const char *get_symbol_name(struct readelf *re, int symtab, int i);
3422c23cb7cSEd Maste static uint64_t get_symbol_value(struct readelf *re, int symtab, int i);
3432c23cb7cSEd Maste static void load_sections(struct readelf *re);
3442c23cb7cSEd Maste static const char *mips_abi_fp(uint64_t fp);
34502b08c90SEd Maste static const char *note_type(const char *note_name, unsigned int et,
3462c23cb7cSEd Maste     unsigned int nt);
34702b08c90SEd Maste static const char *note_type_freebsd(unsigned int nt);
34802b08c90SEd Maste static const char *note_type_freebsd_core(unsigned int nt);
34902b08c90SEd Maste static const char *note_type_linux_core(unsigned int nt);
35002b08c90SEd Maste static const char *note_type_gnu(unsigned int nt);
35102b08c90SEd Maste static const char *note_type_netbsd(unsigned int nt);
35202b08c90SEd Maste static const char *note_type_openbsd(unsigned int nt);
35302b08c90SEd Maste static const char *note_type_unknown(unsigned int nt);
354895f86f1SEd Maste static const char *note_type_xen(unsigned int nt);
3552c23cb7cSEd Maste static const char *option_kind(uint8_t kind);
35620136ffcSEd Maste static const char *phdr_type(unsigned int mach, unsigned int ptype);
3572c23cb7cSEd Maste static const char *ppc_abi_fp(uint64_t fp);
3582c23cb7cSEd Maste static const char *ppc_abi_vector(uint64_t vec);
359839529caSEd Maste static void readelf_usage(int status);
3602c23cb7cSEd Maste static void readelf_version(void);
361cf781b2eSEd Maste static void search_loclist_at(struct readelf *re, Dwarf_Die die,
362cf781b2eSEd Maste     Dwarf_Unsigned lowpc);
3632c23cb7cSEd Maste static void search_ver(struct readelf *re);
3642c23cb7cSEd Maste static const char *section_type(unsigned int mach, unsigned int stype);
365cf781b2eSEd Maste static void set_cu_context(struct readelf *re, Dwarf_Half psize,
366cf781b2eSEd Maste     Dwarf_Half osize, Dwarf_Half ver);
3672c23cb7cSEd Maste static const char *st_bind(unsigned int sbind);
3682c23cb7cSEd Maste static const char *st_shndx(unsigned int shndx);
369b6b6f9ccSEd Maste static const char *st_type(unsigned int mach, unsigned int os,
370b6b6f9ccSEd Maste     unsigned int stype);
3712c23cb7cSEd Maste static const char *st_vis(unsigned int svis);
3722c23cb7cSEd Maste static const char *top_tag(unsigned int tag);
3732c23cb7cSEd Maste static void unload_sections(struct readelf *re);
3742c23cb7cSEd Maste static uint64_t _read_lsb(Elf_Data *d, uint64_t *offsetp,
3752c23cb7cSEd Maste     int bytes_to_read);
3762c23cb7cSEd Maste static uint64_t _read_msb(Elf_Data *d, uint64_t *offsetp,
3772c23cb7cSEd Maste     int bytes_to_read);
3782c23cb7cSEd Maste static uint64_t _decode_lsb(uint8_t **data, int bytes_to_read);
3792c23cb7cSEd Maste static uint64_t _decode_msb(uint8_t **data, int bytes_to_read);
38095fd7f26SEd Maste static int64_t _decode_sleb128(uint8_t **dp, uint8_t *dpe);
38195fd7f26SEd Maste static uint64_t _decode_uleb128(uint8_t **dp, uint8_t *dpe);
3822c23cb7cSEd Maste 
3832c23cb7cSEd Maste static struct eflags_desc arm_eflags_desc[] = {
3842c23cb7cSEd Maste 	{EF_ARM_RELEXEC, "relocatable executable"},
3852c23cb7cSEd Maste 	{EF_ARM_HASENTRY, "has entry point"},
3862c23cb7cSEd Maste 	{EF_ARM_SYMSARESORTED, "sorted symbol tables"},
3872c23cb7cSEd Maste 	{EF_ARM_DYNSYMSUSESEGIDX, "dynamic symbols use segment index"},
3882c23cb7cSEd Maste 	{EF_ARM_MAPSYMSFIRST, "mapping symbols precede others"},
3892c23cb7cSEd Maste 	{EF_ARM_BE8, "BE8"},
3902c23cb7cSEd Maste 	{EF_ARM_LE8, "LE8"},
3912c23cb7cSEd Maste 	{EF_ARM_INTERWORK, "interworking enabled"},
3922c23cb7cSEd Maste 	{EF_ARM_APCS_26, "uses APCS/26"},
3932c23cb7cSEd Maste 	{EF_ARM_APCS_FLOAT, "uses APCS/float"},
3942c23cb7cSEd Maste 	{EF_ARM_PIC, "position independent"},
3952c23cb7cSEd Maste 	{EF_ARM_ALIGN8, "8 bit structure alignment"},
3962c23cb7cSEd Maste 	{EF_ARM_NEW_ABI, "uses new ABI"},
3972c23cb7cSEd Maste 	{EF_ARM_OLD_ABI, "uses old ABI"},
3982c23cb7cSEd Maste 	{EF_ARM_SOFT_FLOAT, "software FP"},
3992c23cb7cSEd Maste 	{EF_ARM_VFP_FLOAT, "VFP"},
4002c23cb7cSEd Maste 	{EF_ARM_MAVERICK_FLOAT, "Maverick FP"},
4012c23cb7cSEd Maste 	{0, NULL}
4022c23cb7cSEd Maste };
4032c23cb7cSEd Maste 
4042c23cb7cSEd Maste static struct eflags_desc mips_eflags_desc[] = {
4052c23cb7cSEd Maste 	{EF_MIPS_NOREORDER, "noreorder"},
4062c23cb7cSEd Maste 	{EF_MIPS_PIC, "pic"},
4072c23cb7cSEd Maste 	{EF_MIPS_CPIC, "cpic"},
4082c23cb7cSEd Maste 	{EF_MIPS_UCODE, "ugen_reserved"},
4092c23cb7cSEd Maste 	{EF_MIPS_ABI2, "abi2"},
4102c23cb7cSEd Maste 	{EF_MIPS_OPTIONS_FIRST, "odk first"},
4112c23cb7cSEd Maste 	{EF_MIPS_ARCH_ASE_MDMX, "mdmx"},
4122c23cb7cSEd Maste 	{EF_MIPS_ARCH_ASE_M16, "mips16"},
4132c23cb7cSEd Maste 	{0, NULL}
4142c23cb7cSEd Maste };
4152c23cb7cSEd Maste 
4162c23cb7cSEd Maste static struct eflags_desc powerpc_eflags_desc[] = {
4172c23cb7cSEd Maste 	{EF_PPC_EMB, "emb"},
4182c23cb7cSEd Maste 	{EF_PPC_RELOCATABLE, "relocatable"},
4192c23cb7cSEd Maste 	{EF_PPC_RELOCATABLE_LIB, "relocatable-lib"},
4202c23cb7cSEd Maste 	{0, NULL}
4212c23cb7cSEd Maste };
4222c23cb7cSEd Maste 
4232c23cb7cSEd Maste static struct eflags_desc sparc_eflags_desc[] = {
4242c23cb7cSEd Maste 	{EF_SPARC_32PLUS, "v8+"},
4252c23cb7cSEd Maste 	{EF_SPARC_SUN_US1, "ultrasparcI"},
4262c23cb7cSEd Maste 	{EF_SPARC_HAL_R1, "halr1"},
4272c23cb7cSEd Maste 	{EF_SPARC_SUN_US3, "ultrasparcIII"},
4282c23cb7cSEd Maste 	{0, NULL}
4292c23cb7cSEd Maste };
4302c23cb7cSEd Maste 
4312c23cb7cSEd Maste static const char *
4322c23cb7cSEd Maste elf_osabi(unsigned int abi)
4332c23cb7cSEd Maste {
4342c23cb7cSEd Maste 	static char s_abi[32];
4352c23cb7cSEd Maste 
4362c23cb7cSEd Maste 	switch(abi) {
437453b09caSEd Maste 	case ELFOSABI_NONE: return "NONE";
438473c31f1SEd Maste 	case ELFOSABI_HPUX: return "HPUX";
4392c23cb7cSEd Maste 	case ELFOSABI_NETBSD: return "NetBSD";
4402c23cb7cSEd Maste 	case ELFOSABI_GNU: return "GNU";
4412c23cb7cSEd Maste 	case ELFOSABI_HURD: return "HURD";
4422c23cb7cSEd Maste 	case ELFOSABI_86OPEN: return "86OPEN";
4432c23cb7cSEd Maste 	case ELFOSABI_SOLARIS: return "Solaris";
4442c23cb7cSEd Maste 	case ELFOSABI_AIX: return "AIX";
4452c23cb7cSEd Maste 	case ELFOSABI_IRIX: return "IRIX";
4462c23cb7cSEd Maste 	case ELFOSABI_FREEBSD: return "FreeBSD";
4472c23cb7cSEd Maste 	case ELFOSABI_TRU64: return "TRU64";
4482c23cb7cSEd Maste 	case ELFOSABI_MODESTO: return "MODESTO";
4492c23cb7cSEd Maste 	case ELFOSABI_OPENBSD: return "OpenBSD";
4502c23cb7cSEd Maste 	case ELFOSABI_OPENVMS: return "OpenVMS";
4512c23cb7cSEd Maste 	case ELFOSABI_NSK: return "NSK";
452b6b6f9ccSEd Maste 	case ELFOSABI_CLOUDABI: return "CloudABI";
453b6d812d2SEd Maste 	case ELFOSABI_ARM_AEABI: return "ARM EABI";
4542c23cb7cSEd Maste 	case ELFOSABI_ARM: return "ARM";
4552c23cb7cSEd Maste 	case ELFOSABI_STANDALONE: return "StandAlone";
4562c23cb7cSEd Maste 	default:
4572c23cb7cSEd Maste 		snprintf(s_abi, sizeof(s_abi), "<unknown: %#x>", abi);
4582c23cb7cSEd Maste 		return (s_abi);
4592c23cb7cSEd Maste 	}
4602c23cb7cSEd Maste };
4612c23cb7cSEd Maste 
4622c23cb7cSEd Maste static const char *
4632c23cb7cSEd Maste elf_machine(unsigned int mach)
4642c23cb7cSEd Maste {
4652c23cb7cSEd Maste 	static char s_mach[32];
4662c23cb7cSEd Maste 
4672c23cb7cSEd Maste 	switch (mach) {
4682c23cb7cSEd Maste 	case EM_NONE: return "Unknown machine";
4692c23cb7cSEd Maste 	case EM_M32: return "AT&T WE32100";
4702c23cb7cSEd Maste 	case EM_SPARC: return "Sun SPARC";
4712c23cb7cSEd Maste 	case EM_386: return "Intel i386";
4722c23cb7cSEd Maste 	case EM_68K: return "Motorola 68000";
4733ef90571SEd Maste 	case EM_IAMCU: return "Intel MCU";
4742c23cb7cSEd Maste 	case EM_88K: return "Motorola 88000";
4752c23cb7cSEd Maste 	case EM_860: return "Intel i860";
4762c23cb7cSEd Maste 	case EM_MIPS: return "MIPS R3000 Big-Endian only";
4772c23cb7cSEd Maste 	case EM_S370: return "IBM System/370";
4782c23cb7cSEd Maste 	case EM_MIPS_RS3_LE: return "MIPS R3000 Little-Endian";
4792c23cb7cSEd Maste 	case EM_PARISC: return "HP PA-RISC";
4802c23cb7cSEd Maste 	case EM_VPP500: return "Fujitsu VPP500";
4812c23cb7cSEd Maste 	case EM_SPARC32PLUS: return "SPARC v8plus";
4822c23cb7cSEd Maste 	case EM_960: return "Intel 80960";
4832c23cb7cSEd Maste 	case EM_PPC: return "PowerPC 32-bit";
4842c23cb7cSEd Maste 	case EM_PPC64: return "PowerPC 64-bit";
4852c23cb7cSEd Maste 	case EM_S390: return "IBM System/390";
4862c23cb7cSEd Maste 	case EM_V800: return "NEC V800";
4872c23cb7cSEd Maste 	case EM_FR20: return "Fujitsu FR20";
4882c23cb7cSEd Maste 	case EM_RH32: return "TRW RH-32";
4892c23cb7cSEd Maste 	case EM_RCE: return "Motorola RCE";
4902c23cb7cSEd Maste 	case EM_ARM: return "ARM";
4912c23cb7cSEd Maste 	case EM_SH: return "Hitachi SH";
4922c23cb7cSEd Maste 	case EM_SPARCV9: return "SPARC v9 64-bit";
4932c23cb7cSEd Maste 	case EM_TRICORE: return "Siemens TriCore embedded processor";
4942c23cb7cSEd Maste 	case EM_ARC: return "Argonaut RISC Core";
4952c23cb7cSEd Maste 	case EM_H8_300: return "Hitachi H8/300";
4962c23cb7cSEd Maste 	case EM_H8_300H: return "Hitachi H8/300H";
4972c23cb7cSEd Maste 	case EM_H8S: return "Hitachi H8S";
4982c23cb7cSEd Maste 	case EM_H8_500: return "Hitachi H8/500";
4992c23cb7cSEd Maste 	case EM_IA_64: return "Intel IA-64 Processor";
5002c23cb7cSEd Maste 	case EM_MIPS_X: return "Stanford MIPS-X";
5012c23cb7cSEd Maste 	case EM_COLDFIRE: return "Motorola ColdFire";
5022c23cb7cSEd Maste 	case EM_68HC12: return "Motorola M68HC12";
5032c23cb7cSEd Maste 	case EM_MMA: return "Fujitsu MMA";
5042c23cb7cSEd Maste 	case EM_PCP: return "Siemens PCP";
5052c23cb7cSEd Maste 	case EM_NCPU: return "Sony nCPU";
5062c23cb7cSEd Maste 	case EM_NDR1: return "Denso NDR1 microprocessor";
5072c23cb7cSEd Maste 	case EM_STARCORE: return "Motorola Star*Core processor";
5082c23cb7cSEd Maste 	case EM_ME16: return "Toyota ME16 processor";
5092c23cb7cSEd Maste 	case EM_ST100: return "STMicroelectronics ST100 processor";
5102c23cb7cSEd Maste 	case EM_TINYJ: return "Advanced Logic Corp. TinyJ processor";
5112c23cb7cSEd Maste 	case EM_X86_64: return "Advanced Micro Devices x86-64";
5122c23cb7cSEd Maste 	case EM_PDSP: return "Sony DSP Processor";
5132c23cb7cSEd Maste 	case EM_FX66: return "Siemens FX66 microcontroller";
5142c23cb7cSEd Maste 	case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 microcontroller";
5152c23cb7cSEd Maste 	case EM_ST7: return "STmicroelectronics ST7 8-bit microcontroller";
5162c23cb7cSEd Maste 	case EM_68HC16: return "Motorola MC68HC16 microcontroller";
5172c23cb7cSEd Maste 	case EM_68HC11: return "Motorola MC68HC11 microcontroller";
5182c23cb7cSEd Maste 	case EM_68HC08: return "Motorola MC68HC08 microcontroller";
5192c23cb7cSEd Maste 	case EM_68HC05: return "Motorola MC68HC05 microcontroller";
5202c23cb7cSEd Maste 	case EM_SVX: return "Silicon Graphics SVx";
5212c23cb7cSEd Maste 	case EM_ST19: return "STMicroelectronics ST19 8-bit mc";
5222c23cb7cSEd Maste 	case EM_VAX: return "Digital VAX";
5232c23cb7cSEd Maste 	case EM_CRIS: return "Axis Communications 32-bit embedded processor";
5242c23cb7cSEd Maste 	case EM_JAVELIN: return "Infineon Tech. 32bit embedded processor";
5252c23cb7cSEd Maste 	case EM_FIREPATH: return "Element 14 64-bit DSP Processor";
5262c23cb7cSEd Maste 	case EM_ZSP: return "LSI Logic 16-bit DSP Processor";
5272c23cb7cSEd Maste 	case EM_MMIX: return "Donald Knuth's educational 64-bit proc";
5282c23cb7cSEd Maste 	case EM_HUANY: return "Harvard University MI object files";
5292c23cb7cSEd Maste 	case EM_PRISM: return "SiTera Prism";
5302c23cb7cSEd Maste 	case EM_AVR: return "Atmel AVR 8-bit microcontroller";
5312c23cb7cSEd Maste 	case EM_FR30: return "Fujitsu FR30";
5322c23cb7cSEd Maste 	case EM_D10V: return "Mitsubishi D10V";
5332c23cb7cSEd Maste 	case EM_D30V: return "Mitsubishi D30V";
5342c23cb7cSEd Maste 	case EM_V850: return "NEC v850";
5352c23cb7cSEd Maste 	case EM_M32R: return "Mitsubishi M32R";
5362c23cb7cSEd Maste 	case EM_MN10300: return "Matsushita MN10300";
5372c23cb7cSEd Maste 	case EM_MN10200: return "Matsushita MN10200";
5382c23cb7cSEd Maste 	case EM_PJ: return "picoJava";
5392c23cb7cSEd Maste 	case EM_OPENRISC: return "OpenRISC 32-bit embedded processor";
5402c23cb7cSEd Maste 	case EM_ARC_A5: return "ARC Cores Tangent-A5";
5412c23cb7cSEd Maste 	case EM_XTENSA: return "Tensilica Xtensa Architecture";
5422c23cb7cSEd Maste 	case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
5432c23cb7cSEd Maste 	case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
5442c23cb7cSEd Maste 	case EM_NS32K: return "National Semiconductor 32000 series";
5452c23cb7cSEd Maste 	case EM_TPC: return "Tenor Network TPC processor";
5462c23cb7cSEd Maste 	case EM_SNP1K: return "Trebia SNP 1000 processor";
5472c23cb7cSEd Maste 	case EM_ST200: return "STMicroelectronics ST200 microcontroller";
5482c23cb7cSEd Maste 	case EM_IP2K: return "Ubicom IP2xxx microcontroller family";
5492c23cb7cSEd Maste 	case EM_MAX: return "MAX Processor";
5502c23cb7cSEd Maste 	case EM_CR: return "National Semiconductor CompactRISC microprocessor";
5512c23cb7cSEd Maste 	case EM_F2MC16: return "Fujitsu F2MC16";
5522c23cb7cSEd Maste 	case EM_MSP430: return "TI embedded microcontroller msp430";
5532c23cb7cSEd Maste 	case EM_BLACKFIN: return "Analog Devices Blackfin (DSP) processor";
5542c23cb7cSEd Maste 	case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
5552c23cb7cSEd Maste 	case EM_SEP: return "Sharp embedded microprocessor";
5562c23cb7cSEd Maste 	case EM_ARCA: return "Arca RISC Microprocessor";
5572c23cb7cSEd Maste 	case EM_UNICORE: return "Microprocessor series from PKU-Unity Ltd";
558b3f26809SEd Maste 	case EM_AARCH64: return "AArch64";
559119b7592SEd Maste 	case EM_RISCV: return "RISC-V";
5602c23cb7cSEd Maste 	default:
5612c23cb7cSEd Maste 		snprintf(s_mach, sizeof(s_mach), "<unknown: %#x>", mach);
5622c23cb7cSEd Maste 		return (s_mach);
5632c23cb7cSEd Maste 	}
5642c23cb7cSEd Maste 
5652c23cb7cSEd Maste }
5662c23cb7cSEd Maste 
5672c23cb7cSEd Maste static const char *
5682c23cb7cSEd Maste elf_class(unsigned int class)
5692c23cb7cSEd Maste {
5702c23cb7cSEd Maste 	static char s_class[32];
5712c23cb7cSEd Maste 
5722c23cb7cSEd Maste 	switch (class) {
5732c23cb7cSEd Maste 	case ELFCLASSNONE: return "none";
5742c23cb7cSEd Maste 	case ELFCLASS32: return "ELF32";
5752c23cb7cSEd Maste 	case ELFCLASS64: return "ELF64";
5762c23cb7cSEd Maste 	default:
5772c23cb7cSEd Maste 		snprintf(s_class, sizeof(s_class), "<unknown: %#x>", class);
5782c23cb7cSEd Maste 		return (s_class);
5792c23cb7cSEd Maste 	}
5802c23cb7cSEd Maste }
5812c23cb7cSEd Maste 
5822c23cb7cSEd Maste static const char *
5832c23cb7cSEd Maste elf_endian(unsigned int endian)
5842c23cb7cSEd Maste {
5852c23cb7cSEd Maste 	static char s_endian[32];
5862c23cb7cSEd Maste 
5872c23cb7cSEd Maste 	switch (endian) {
5882c23cb7cSEd Maste 	case ELFDATANONE: return "none";
5892c23cb7cSEd Maste 	case ELFDATA2LSB: return "2's complement, little endian";
5902c23cb7cSEd Maste 	case ELFDATA2MSB: return "2's complement, big endian";
5912c23cb7cSEd Maste 	default:
5922c23cb7cSEd Maste 		snprintf(s_endian, sizeof(s_endian), "<unknown: %#x>", endian);
5932c23cb7cSEd Maste 		return (s_endian);
5942c23cb7cSEd Maste 	}
5952c23cb7cSEd Maste }
5962c23cb7cSEd Maste 
5972c23cb7cSEd Maste static const char *
5982c23cb7cSEd Maste elf_type(unsigned int type)
5992c23cb7cSEd Maste {
6002c23cb7cSEd Maste 	static char s_type[32];
6012c23cb7cSEd Maste 
6022c23cb7cSEd Maste 	switch (type) {
6032c23cb7cSEd Maste 	case ET_NONE: return "NONE (None)";
6042c23cb7cSEd Maste 	case ET_REL: return "REL (Relocatable file)";
6052c23cb7cSEd Maste 	case ET_EXEC: return "EXEC (Executable file)";
6062c23cb7cSEd Maste 	case ET_DYN: return "DYN (Shared object file)";
6072c23cb7cSEd Maste 	case ET_CORE: return "CORE (Core file)";
6082c23cb7cSEd Maste 	default:
6092c23cb7cSEd Maste 		if (type >= ET_LOPROC)
6102c23cb7cSEd Maste 			snprintf(s_type, sizeof(s_type), "<proc: %#x>", type);
6112c23cb7cSEd Maste 		else if (type >= ET_LOOS && type <= ET_HIOS)
6122c23cb7cSEd Maste 			snprintf(s_type, sizeof(s_type), "<os: %#x>", type);
6132c23cb7cSEd Maste 		else
6142c23cb7cSEd Maste 			snprintf(s_type, sizeof(s_type), "<unknown: %#x>",
6152c23cb7cSEd Maste 			    type);
6162c23cb7cSEd Maste 		return (s_type);
6172c23cb7cSEd Maste 	}
6182c23cb7cSEd Maste }
6192c23cb7cSEd Maste 
6202c23cb7cSEd Maste static const char *
6212c23cb7cSEd Maste elf_ver(unsigned int ver)
6222c23cb7cSEd Maste {
6232c23cb7cSEd Maste 	static char s_ver[32];
6242c23cb7cSEd Maste 
6252c23cb7cSEd Maste 	switch (ver) {
6262c23cb7cSEd Maste 	case EV_CURRENT: return "(current)";
6272c23cb7cSEd Maste 	case EV_NONE: return "(none)";
6282c23cb7cSEd Maste 	default:
6292c23cb7cSEd Maste 		snprintf(s_ver, sizeof(s_ver), "<unknown: %#x>",
6302c23cb7cSEd Maste 		    ver);
6312c23cb7cSEd Maste 		return (s_ver);
6322c23cb7cSEd Maste 	}
6332c23cb7cSEd Maste }
6342c23cb7cSEd Maste 
6352c23cb7cSEd Maste static const char *
63620136ffcSEd Maste phdr_type(unsigned int mach, unsigned int ptype)
6372c23cb7cSEd Maste {
6382c23cb7cSEd Maste 	static char s_ptype[32];
6392c23cb7cSEd Maste 
64020136ffcSEd Maste 	if (ptype >= PT_LOPROC && ptype <= PT_HIPROC) {
64120136ffcSEd Maste 		switch (mach) {
64220136ffcSEd Maste 		case EM_ARM:
64320136ffcSEd Maste 			switch (ptype) {
64420136ffcSEd Maste 			case PT_ARM_ARCHEXT: return "ARM_ARCHEXT";
64520136ffcSEd Maste 			case PT_ARM_EXIDX: return "ARM_EXIDX";
64620136ffcSEd Maste 			}
64720136ffcSEd Maste 			break;
64820136ffcSEd Maste 		}
64920136ffcSEd Maste 		snprintf(s_ptype, sizeof(s_ptype), "LOPROC+%#x",
65020136ffcSEd Maste 		    ptype - PT_LOPROC);
65120136ffcSEd Maste 		return (s_ptype);
65220136ffcSEd Maste 	}
65320136ffcSEd Maste 
6542c23cb7cSEd Maste 	switch (ptype) {
6552c23cb7cSEd Maste 	case PT_NULL: return "NULL";
6562c23cb7cSEd Maste 	case PT_LOAD: return "LOAD";
6572c23cb7cSEd Maste 	case PT_DYNAMIC: return "DYNAMIC";
6582c23cb7cSEd Maste 	case PT_INTERP: return "INTERP";
6592c23cb7cSEd Maste 	case PT_NOTE: return "NOTE";
6602c23cb7cSEd Maste 	case PT_SHLIB: return "SHLIB";
6612c23cb7cSEd Maste 	case PT_PHDR: return "PHDR";
6622c23cb7cSEd Maste 	case PT_TLS: return "TLS";
6632c23cb7cSEd Maste 	case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
6642c23cb7cSEd Maste 	case PT_GNU_STACK: return "GNU_STACK";
6652c23cb7cSEd Maste 	case PT_GNU_RELRO: return "GNU_RELRO";
6662c23cb7cSEd Maste 	default:
66720136ffcSEd Maste 		if (ptype >= PT_LOOS && ptype <= PT_HIOS)
6682c23cb7cSEd Maste 			snprintf(s_ptype, sizeof(s_ptype), "LOOS+%#x",
6692c23cb7cSEd Maste 			    ptype - PT_LOOS);
6702c23cb7cSEd Maste 		else
6712c23cb7cSEd Maste 			snprintf(s_ptype, sizeof(s_ptype), "<unknown: %#x>",
6722c23cb7cSEd Maste 			    ptype);
6732c23cb7cSEd Maste 		return (s_ptype);
6742c23cb7cSEd Maste 	}
6752c23cb7cSEd Maste }
6762c23cb7cSEd Maste 
6772c23cb7cSEd Maste static const char *
6782c23cb7cSEd Maste section_type(unsigned int mach, unsigned int stype)
6792c23cb7cSEd Maste {
6802c23cb7cSEd Maste 	static char s_stype[32];
6812c23cb7cSEd Maste 
6822c23cb7cSEd Maste 	if (stype >= SHT_LOPROC && stype <= SHT_HIPROC) {
6832c23cb7cSEd Maste 		switch (mach) {
68420136ffcSEd Maste 		case EM_ARM:
68520136ffcSEd Maste 			switch (stype) {
68620136ffcSEd Maste 			case SHT_ARM_EXIDX: return "ARM_EXIDX";
68720136ffcSEd Maste 			case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP";
68820136ffcSEd Maste 			case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES";
68920136ffcSEd Maste 			case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY";
69020136ffcSEd Maste 			case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION";
69120136ffcSEd Maste 			}
69220136ffcSEd Maste 			break;
6932c23cb7cSEd Maste 		case EM_X86_64:
6942c23cb7cSEd Maste 			switch (stype) {
695b6b6f9ccSEd Maste 			case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
6962c23cb7cSEd Maste 			default:
6972c23cb7cSEd Maste 				break;
6982c23cb7cSEd Maste 			}
6992c23cb7cSEd Maste 			break;
7002c23cb7cSEd Maste 		case EM_MIPS:
7012c23cb7cSEd Maste 		case EM_MIPS_RS3_LE:
7022c23cb7cSEd Maste 			switch (stype) {
7032c23cb7cSEd Maste 			case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
7042c23cb7cSEd Maste 			case SHT_MIPS_MSYM: return "MIPS_MSYM";
7052c23cb7cSEd Maste 			case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
7062c23cb7cSEd Maste 			case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
7072c23cb7cSEd Maste 			case SHT_MIPS_UCODE: return "MIPS_UCODE";
7082c23cb7cSEd Maste 			case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
7092c23cb7cSEd Maste 			case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
7102c23cb7cSEd Maste 			case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
7112c23cb7cSEd Maste 			case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
7122c23cb7cSEd Maste 			case SHT_MIPS_RELD: return "MIPS_RELD";
7132c23cb7cSEd Maste 			case SHT_MIPS_IFACE: return "MIPS_IFACE";
7142c23cb7cSEd Maste 			case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
7152c23cb7cSEd Maste 			case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
7162c23cb7cSEd Maste 			case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
7172c23cb7cSEd Maste 			case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
7182c23cb7cSEd Maste 			case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
7192c23cb7cSEd Maste 			case SHT_MIPS_DWARF: return "MIPS_DWARF";
7202c23cb7cSEd Maste 			case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
7212c23cb7cSEd Maste 			case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
7222c23cb7cSEd Maste 			case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
7232c23cb7cSEd Maste 			case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
7242c23cb7cSEd Maste 			case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
7252c23cb7cSEd Maste 			case SHT_MIPS_XLATE: return "MIPS_XLATE";
7262c23cb7cSEd Maste 			case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
7272c23cb7cSEd Maste 			case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
7282c23cb7cSEd Maste 			case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
7292c23cb7cSEd Maste 			case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
7302c23cb7cSEd Maste 			case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
731bee2765cSEd Maste 			case SHT_MIPS_ABIFLAGS: return "MIPS_ABIFLAGS";
7322c23cb7cSEd Maste 			default:
7332c23cb7cSEd Maste 				break;
7342c23cb7cSEd Maste 			}
7352c23cb7cSEd Maste 			break;
7362c23cb7cSEd Maste 		default:
7372c23cb7cSEd Maste 			break;
7382c23cb7cSEd Maste 		}
7392c23cb7cSEd Maste 
7402c23cb7cSEd Maste 		snprintf(s_stype, sizeof(s_stype), "LOPROC+%#x",
7412c23cb7cSEd Maste 		    stype - SHT_LOPROC);
7422c23cb7cSEd Maste 		return (s_stype);
7432c23cb7cSEd Maste 	}
7442c23cb7cSEd Maste 
7452c23cb7cSEd Maste 	switch (stype) {
7462c23cb7cSEd Maste 	case SHT_NULL: return "NULL";
7472c23cb7cSEd Maste 	case SHT_PROGBITS: return "PROGBITS";
7482c23cb7cSEd Maste 	case SHT_SYMTAB: return "SYMTAB";
7492c23cb7cSEd Maste 	case SHT_STRTAB: return "STRTAB";
7502c23cb7cSEd Maste 	case SHT_RELA: return "RELA";
7512c23cb7cSEd Maste 	case SHT_HASH: return "HASH";
7522c23cb7cSEd Maste 	case SHT_DYNAMIC: return "DYNAMIC";
7532c23cb7cSEd Maste 	case SHT_NOTE: return "NOTE";
7542c23cb7cSEd Maste 	case SHT_NOBITS: return "NOBITS";
7552c23cb7cSEd Maste 	case SHT_REL: return "REL";
7562c23cb7cSEd Maste 	case SHT_SHLIB: return "SHLIB";
7572c23cb7cSEd Maste 	case SHT_DYNSYM: return "DYNSYM";
7582c23cb7cSEd Maste 	case SHT_INIT_ARRAY: return "INIT_ARRAY";
7592c23cb7cSEd Maste 	case SHT_FINI_ARRAY: return "FINI_ARRAY";
7602c23cb7cSEd Maste 	case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
7612c23cb7cSEd Maste 	case SHT_GROUP: return "GROUP";
7622c23cb7cSEd Maste 	case SHT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
7632c23cb7cSEd Maste 	case SHT_SUNW_dof: return "SUNW_dof";
7642c23cb7cSEd Maste 	case SHT_SUNW_cap: return "SUNW_cap";
7652c23cb7cSEd Maste 	case SHT_GNU_HASH: return "GNU_HASH";
7662c23cb7cSEd Maste 	case SHT_SUNW_ANNOTATE: return "SUNW_ANNOTATE";
7672c23cb7cSEd Maste 	case SHT_SUNW_DEBUGSTR: return "SUNW_DEBUGSTR";
7682c23cb7cSEd Maste 	case SHT_SUNW_DEBUG: return "SUNW_DEBUG";
7692c23cb7cSEd Maste 	case SHT_SUNW_move: return "SUNW_move";
7702c23cb7cSEd Maste 	case SHT_SUNW_COMDAT: return "SUNW_COMDAT";
7712c23cb7cSEd Maste 	case SHT_SUNW_syminfo: return "SUNW_syminfo";
7722c23cb7cSEd Maste 	case SHT_SUNW_verdef: return "SUNW_verdef";
7732c23cb7cSEd Maste 	case SHT_SUNW_verneed: return "SUNW_verneed";
7742c23cb7cSEd Maste 	case SHT_SUNW_versym: return "SUNW_versym";
7752c23cb7cSEd Maste 	default:
7762c23cb7cSEd Maste 		if (stype >= SHT_LOOS && stype <= SHT_HIOS)
7772c23cb7cSEd Maste 			snprintf(s_stype, sizeof(s_stype), "LOOS+%#x",
7782c23cb7cSEd Maste 			    stype - SHT_LOOS);
7792c23cb7cSEd Maste 		else if (stype >= SHT_LOUSER)
7802c23cb7cSEd Maste 			snprintf(s_stype, sizeof(s_stype), "LOUSER+%#x",
7812c23cb7cSEd Maste 			    stype - SHT_LOUSER);
7822c23cb7cSEd Maste 		else
7832c23cb7cSEd Maste 			snprintf(s_stype, sizeof(s_stype), "<unknown: %#x>",
7842c23cb7cSEd Maste 			    stype);
7852c23cb7cSEd Maste 		return (s_stype);
7862c23cb7cSEd Maste 	}
7872c23cb7cSEd Maste }
7882c23cb7cSEd Maste 
7892c23cb7cSEd Maste static const char *
7902c23cb7cSEd Maste dt_type(unsigned int mach, unsigned int dtype)
7912c23cb7cSEd Maste {
7922c23cb7cSEd Maste 	static char s_dtype[32];
7932c23cb7cSEd Maste 
794d62ecb4eSEd Maste 	switch (dtype) {
795d62ecb4eSEd Maste 	case DT_NULL: return "NULL";
796d62ecb4eSEd Maste 	case DT_NEEDED: return "NEEDED";
797d62ecb4eSEd Maste 	case DT_PLTRELSZ: return "PLTRELSZ";
798d62ecb4eSEd Maste 	case DT_PLTGOT: return "PLTGOT";
799d62ecb4eSEd Maste 	case DT_HASH: return "HASH";
800d62ecb4eSEd Maste 	case DT_STRTAB: return "STRTAB";
801d62ecb4eSEd Maste 	case DT_SYMTAB: return "SYMTAB";
802d62ecb4eSEd Maste 	case DT_RELA: return "RELA";
803d62ecb4eSEd Maste 	case DT_RELASZ: return "RELASZ";
804d62ecb4eSEd Maste 	case DT_RELAENT: return "RELAENT";
805d62ecb4eSEd Maste 	case DT_STRSZ: return "STRSZ";
806d62ecb4eSEd Maste 	case DT_SYMENT: return "SYMENT";
807d62ecb4eSEd Maste 	case DT_INIT: return "INIT";
808d62ecb4eSEd Maste 	case DT_FINI: return "FINI";
809d62ecb4eSEd Maste 	case DT_SONAME: return "SONAME";
810d62ecb4eSEd Maste 	case DT_RPATH: return "RPATH";
811d62ecb4eSEd Maste 	case DT_SYMBOLIC: return "SYMBOLIC";
812d62ecb4eSEd Maste 	case DT_REL: return "REL";
813d62ecb4eSEd Maste 	case DT_RELSZ: return "RELSZ";
814d62ecb4eSEd Maste 	case DT_RELENT: return "RELENT";
815d62ecb4eSEd Maste 	case DT_PLTREL: return "PLTREL";
816d62ecb4eSEd Maste 	case DT_DEBUG: return "DEBUG";
817d62ecb4eSEd Maste 	case DT_TEXTREL: return "TEXTREL";
818d62ecb4eSEd Maste 	case DT_JMPREL: return "JMPREL";
819d62ecb4eSEd Maste 	case DT_BIND_NOW: return "BIND_NOW";
820d62ecb4eSEd Maste 	case DT_INIT_ARRAY: return "INIT_ARRAY";
821d62ecb4eSEd Maste 	case DT_FINI_ARRAY: return "FINI_ARRAY";
822d62ecb4eSEd Maste 	case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
823d62ecb4eSEd Maste 	case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
824d62ecb4eSEd Maste 	case DT_RUNPATH: return "RUNPATH";
825d62ecb4eSEd Maste 	case DT_FLAGS: return "FLAGS";
826d62ecb4eSEd Maste 	case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
827d62ecb4eSEd Maste 	case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
828d62ecb4eSEd Maste 	case DT_MAXPOSTAGS: return "MAXPOSTAGS";
829d62ecb4eSEd Maste 	case DT_SUNW_AUXILIARY: return "SUNW_AUXILIARY";
830d62ecb4eSEd Maste 	case DT_SUNW_RTLDINF: return "SUNW_RTLDINF";
831d62ecb4eSEd Maste 	case DT_SUNW_FILTER: return "SUNW_FILTER";
832d62ecb4eSEd Maste 	case DT_SUNW_CAP: return "SUNW_CAP";
833715d1396SEd Maste 	case DT_SUNW_ASLR: return "SUNW_ASLR";
834d62ecb4eSEd Maste 	case DT_CHECKSUM: return "CHECKSUM";
835d62ecb4eSEd Maste 	case DT_PLTPADSZ: return "PLTPADSZ";
836d62ecb4eSEd Maste 	case DT_MOVEENT: return "MOVEENT";
837d62ecb4eSEd Maste 	case DT_MOVESZ: return "MOVESZ";
838d62ecb4eSEd Maste 	case DT_FEATURE: return "FEATURE";
839d62ecb4eSEd Maste 	case DT_POSFLAG_1: return "POSFLAG_1";
840d62ecb4eSEd Maste 	case DT_SYMINSZ: return "SYMINSZ";
841d62ecb4eSEd Maste 	case DT_SYMINENT: return "SYMINENT";
842d62ecb4eSEd Maste 	case DT_GNU_HASH: return "GNU_HASH";
843d62ecb4eSEd Maste 	case DT_TLSDESC_PLT: return "DT_TLSDESC_PLT";
844d62ecb4eSEd Maste 	case DT_TLSDESC_GOT: return "DT_TLSDESC_GOT";
845d62ecb4eSEd Maste 	case DT_GNU_CONFLICT: return "GNU_CONFLICT";
846d62ecb4eSEd Maste 	case DT_GNU_LIBLIST: return "GNU_LIBLIST";
847d62ecb4eSEd Maste 	case DT_CONFIG: return "CONFIG";
848d62ecb4eSEd Maste 	case DT_DEPAUDIT: return "DEPAUDIT";
849d62ecb4eSEd Maste 	case DT_AUDIT: return "AUDIT";
850d62ecb4eSEd Maste 	case DT_PLTPAD: return "PLTPAD";
851d62ecb4eSEd Maste 	case DT_MOVETAB: return "MOVETAB";
852d62ecb4eSEd Maste 	case DT_SYMINFO: return "SYMINFO";
853d62ecb4eSEd Maste 	case DT_VERSYM: return "VERSYM";
854d62ecb4eSEd Maste 	case DT_RELACOUNT: return "RELACOUNT";
855d62ecb4eSEd Maste 	case DT_RELCOUNT: return "RELCOUNT";
856d62ecb4eSEd Maste 	case DT_FLAGS_1: return "FLAGS_1";
857d62ecb4eSEd Maste 	case DT_VERDEF: return "VERDEF";
858d62ecb4eSEd Maste 	case DT_VERDEFNUM: return "VERDEFNUM";
859d62ecb4eSEd Maste 	case DT_VERNEED: return "VERNEED";
860d62ecb4eSEd Maste 	case DT_VERNEEDNUM: return "VERNEEDNUM";
861d62ecb4eSEd Maste 	case DT_AUXILIARY: return "AUXILIARY";
862d62ecb4eSEd Maste 	case DT_USED: return "USED";
863d62ecb4eSEd Maste 	case DT_FILTER: return "FILTER";
864d62ecb4eSEd Maste 	case DT_GNU_PRELINKED: return "GNU_PRELINKED";
865d62ecb4eSEd Maste 	case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
866d62ecb4eSEd Maste 	case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
867d62ecb4eSEd Maste 	}
868d62ecb4eSEd Maste 
8692c23cb7cSEd Maste 	if (dtype >= DT_LOPROC && dtype <= DT_HIPROC) {
8702c23cb7cSEd Maste 		switch (mach) {
8712c23cb7cSEd Maste 		case EM_ARM:
8722c23cb7cSEd Maste 			switch (dtype) {
8732c23cb7cSEd Maste 			case DT_ARM_SYMTABSZ:
8742c23cb7cSEd Maste 				return "ARM_SYMTABSZ";
8752c23cb7cSEd Maste 			default:
8762c23cb7cSEd Maste 				break;
8772c23cb7cSEd Maste 			}
8782c23cb7cSEd Maste 			break;
8792c23cb7cSEd Maste 		case EM_MIPS:
8802c23cb7cSEd Maste 		case EM_MIPS_RS3_LE:
8812c23cb7cSEd Maste 			switch (dtype) {
8822c23cb7cSEd Maste 			case DT_MIPS_RLD_VERSION:
8832c23cb7cSEd Maste 				return "MIPS_RLD_VERSION";
8842c23cb7cSEd Maste 			case DT_MIPS_TIME_STAMP:
8852c23cb7cSEd Maste 				return "MIPS_TIME_STAMP";
8862c23cb7cSEd Maste 			case DT_MIPS_ICHECKSUM:
8872c23cb7cSEd Maste 				return "MIPS_ICHECKSUM";
8882c23cb7cSEd Maste 			case DT_MIPS_IVERSION:
8892c23cb7cSEd Maste 				return "MIPS_IVERSION";
8902c23cb7cSEd Maste 			case DT_MIPS_FLAGS:
8912c23cb7cSEd Maste 				return "MIPS_FLAGS";
8922c23cb7cSEd Maste 			case DT_MIPS_BASE_ADDRESS:
8932c23cb7cSEd Maste 				return "MIPS_BASE_ADDRESS";
8942c23cb7cSEd Maste 			case DT_MIPS_CONFLICT:
8952c23cb7cSEd Maste 				return "MIPS_CONFLICT";
8962c23cb7cSEd Maste 			case DT_MIPS_LIBLIST:
8972c23cb7cSEd Maste 				return "MIPS_LIBLIST";
8982c23cb7cSEd Maste 			case DT_MIPS_LOCAL_GOTNO:
8992c23cb7cSEd Maste 				return "MIPS_LOCAL_GOTNO";
9002c23cb7cSEd Maste 			case DT_MIPS_CONFLICTNO:
9012c23cb7cSEd Maste 				return "MIPS_CONFLICTNO";
9022c23cb7cSEd Maste 			case DT_MIPS_LIBLISTNO:
9032c23cb7cSEd Maste 				return "MIPS_LIBLISTNO";
9042c23cb7cSEd Maste 			case DT_MIPS_SYMTABNO:
9052c23cb7cSEd Maste 				return "MIPS_SYMTABNO";
9062c23cb7cSEd Maste 			case DT_MIPS_UNREFEXTNO:
9072c23cb7cSEd Maste 				return "MIPS_UNREFEXTNO";
9082c23cb7cSEd Maste 			case DT_MIPS_GOTSYM:
9092c23cb7cSEd Maste 				return "MIPS_GOTSYM";
9102c23cb7cSEd Maste 			case DT_MIPS_HIPAGENO:
9112c23cb7cSEd Maste 				return "MIPS_HIPAGENO";
9122c23cb7cSEd Maste 			case DT_MIPS_RLD_MAP:
9132c23cb7cSEd Maste 				return "MIPS_RLD_MAP";
9142c23cb7cSEd Maste 			case DT_MIPS_DELTA_CLASS:
9152c23cb7cSEd Maste 				return "MIPS_DELTA_CLASS";
9162c23cb7cSEd Maste 			case DT_MIPS_DELTA_CLASS_NO:
9172c23cb7cSEd Maste 				return "MIPS_DELTA_CLASS_NO";
9182c23cb7cSEd Maste 			case DT_MIPS_DELTA_INSTANCE:
9192c23cb7cSEd Maste 				return "MIPS_DELTA_INSTANCE";
9202c23cb7cSEd Maste 			case DT_MIPS_DELTA_INSTANCE_NO:
9212c23cb7cSEd Maste 				return "MIPS_DELTA_INSTANCE_NO";
9222c23cb7cSEd Maste 			case DT_MIPS_DELTA_RELOC:
9232c23cb7cSEd Maste 				return "MIPS_DELTA_RELOC";
9242c23cb7cSEd Maste 			case DT_MIPS_DELTA_RELOC_NO:
9252c23cb7cSEd Maste 				return "MIPS_DELTA_RELOC_NO";
9262c23cb7cSEd Maste 			case DT_MIPS_DELTA_SYM:
9272c23cb7cSEd Maste 				return "MIPS_DELTA_SYM";
9282c23cb7cSEd Maste 			case DT_MIPS_DELTA_SYM_NO:
9292c23cb7cSEd Maste 				return "MIPS_DELTA_SYM_NO";
9302c23cb7cSEd Maste 			case DT_MIPS_DELTA_CLASSSYM:
9312c23cb7cSEd Maste 				return "MIPS_DELTA_CLASSSYM";
9322c23cb7cSEd Maste 			case DT_MIPS_DELTA_CLASSSYM_NO:
9332c23cb7cSEd Maste 				return "MIPS_DELTA_CLASSSYM_NO";
9342c23cb7cSEd Maste 			case DT_MIPS_CXX_FLAGS:
9352c23cb7cSEd Maste 				return "MIPS_CXX_FLAGS";
9362c23cb7cSEd Maste 			case DT_MIPS_PIXIE_INIT:
9372c23cb7cSEd Maste 				return "MIPS_PIXIE_INIT";
9382c23cb7cSEd Maste 			case DT_MIPS_SYMBOL_LIB:
9392c23cb7cSEd Maste 				return "MIPS_SYMBOL_LIB";
9402c23cb7cSEd Maste 			case DT_MIPS_LOCALPAGE_GOTIDX:
9412c23cb7cSEd Maste 				return "MIPS_LOCALPAGE_GOTIDX";
9422c23cb7cSEd Maste 			case DT_MIPS_LOCAL_GOTIDX:
9432c23cb7cSEd Maste 				return "MIPS_LOCAL_GOTIDX";
9442c23cb7cSEd Maste 			case DT_MIPS_HIDDEN_GOTIDX:
9452c23cb7cSEd Maste 				return "MIPS_HIDDEN_GOTIDX";
9462c23cb7cSEd Maste 			case DT_MIPS_PROTECTED_GOTIDX:
9472c23cb7cSEd Maste 				return "MIPS_PROTECTED_GOTIDX";
9482c23cb7cSEd Maste 			case DT_MIPS_OPTIONS:
9492c23cb7cSEd Maste 				return "MIPS_OPTIONS";
9502c23cb7cSEd Maste 			case DT_MIPS_INTERFACE:
9512c23cb7cSEd Maste 				return "MIPS_INTERFACE";
9522c23cb7cSEd Maste 			case DT_MIPS_DYNSTR_ALIGN:
9532c23cb7cSEd Maste 				return "MIPS_DYNSTR_ALIGN";
9542c23cb7cSEd Maste 			case DT_MIPS_INTERFACE_SIZE:
9552c23cb7cSEd Maste 				return "MIPS_INTERFACE_SIZE";
9562c23cb7cSEd Maste 			case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
9572c23cb7cSEd Maste 				return "MIPS_RLD_TEXT_RESOLVE_ADDR";
9582c23cb7cSEd Maste 			case DT_MIPS_PERF_SUFFIX:
9592c23cb7cSEd Maste 				return "MIPS_PERF_SUFFIX";
9602c23cb7cSEd Maste 			case DT_MIPS_COMPACT_SIZE:
9612c23cb7cSEd Maste 				return "MIPS_COMPACT_SIZE";
9622c23cb7cSEd Maste 			case DT_MIPS_GP_VALUE:
9632c23cb7cSEd Maste 				return "MIPS_GP_VALUE";
9642c23cb7cSEd Maste 			case DT_MIPS_AUX_DYNAMIC:
9652c23cb7cSEd Maste 				return "MIPS_AUX_DYNAMIC";
9662c23cb7cSEd Maste 			case DT_MIPS_PLTGOT:
9672c23cb7cSEd Maste 				return "MIPS_PLTGOT";
9682c23cb7cSEd Maste 			case DT_MIPS_RLD_OBJ_UPDATE:
9692c23cb7cSEd Maste 				return "MIPS_RLD_OBJ_UPDATE";
9702c23cb7cSEd Maste 			case DT_MIPS_RWPLT:
9712c23cb7cSEd Maste 				return "MIPS_RWPLT";
9722c23cb7cSEd Maste 			default:
9732c23cb7cSEd Maste 				break;
9742c23cb7cSEd Maste 			}
9752c23cb7cSEd Maste 			break;
9762c23cb7cSEd Maste 		case EM_SPARC:
9772c23cb7cSEd Maste 		case EM_SPARC32PLUS:
9782c23cb7cSEd Maste 		case EM_SPARCV9:
9792c23cb7cSEd Maste 			switch (dtype) {
9802c23cb7cSEd Maste 			case DT_SPARC_REGISTER:
9812c23cb7cSEd Maste 				return "DT_SPARC_REGISTER";
9822c23cb7cSEd Maste 			default:
9832c23cb7cSEd Maste 				break;
9842c23cb7cSEd Maste 			}
9852c23cb7cSEd Maste 			break;
9862c23cb7cSEd Maste 		default:
9872c23cb7cSEd Maste 			break;
9882c23cb7cSEd Maste 		}
9892c23cb7cSEd Maste 	}
9902c23cb7cSEd Maste 
9912c23cb7cSEd Maste 	snprintf(s_dtype, sizeof(s_dtype), "<unknown: %#x>", dtype);
9922c23cb7cSEd Maste 	return (s_dtype);
9932c23cb7cSEd Maste }
9942c23cb7cSEd Maste 
9952c23cb7cSEd Maste static const char *
9962c23cb7cSEd Maste st_bind(unsigned int sbind)
9972c23cb7cSEd Maste {
9982c23cb7cSEd Maste 	static char s_sbind[32];
9992c23cb7cSEd Maste 
10002c23cb7cSEd Maste 	switch (sbind) {
10012c23cb7cSEd Maste 	case STB_LOCAL: return "LOCAL";
10022c23cb7cSEd Maste 	case STB_GLOBAL: return "GLOBAL";
10032c23cb7cSEd Maste 	case STB_WEAK: return "WEAK";
1004839529caSEd Maste 	case STB_GNU_UNIQUE: return "UNIQUE";
10052c23cb7cSEd Maste 	default:
10062c23cb7cSEd Maste 		if (sbind >= STB_LOOS && sbind <= STB_HIOS)
10072c23cb7cSEd Maste 			return "OS";
10082c23cb7cSEd Maste 		else if (sbind >= STB_LOPROC && sbind <= STB_HIPROC)
10092c23cb7cSEd Maste 			return "PROC";
10102c23cb7cSEd Maste 		else
10112c23cb7cSEd Maste 			snprintf(s_sbind, sizeof(s_sbind), "<unknown: %#x>",
10122c23cb7cSEd Maste 			    sbind);
10132c23cb7cSEd Maste 		return (s_sbind);
10142c23cb7cSEd Maste 	}
10152c23cb7cSEd Maste }
10162c23cb7cSEd Maste 
10172c23cb7cSEd Maste static const char *
1018b6b6f9ccSEd Maste st_type(unsigned int mach, unsigned int os, unsigned int stype)
10192c23cb7cSEd Maste {
10202c23cb7cSEd Maste 	static char s_stype[32];
10212c23cb7cSEd Maste 
10222c23cb7cSEd Maste 	switch (stype) {
10232c23cb7cSEd Maste 	case STT_NOTYPE: return "NOTYPE";
10242c23cb7cSEd Maste 	case STT_OBJECT: return "OBJECT";
10252c23cb7cSEd Maste 	case STT_FUNC: return "FUNC";
10262c23cb7cSEd Maste 	case STT_SECTION: return "SECTION";
10272c23cb7cSEd Maste 	case STT_FILE: return "FILE";
10282c23cb7cSEd Maste 	case STT_COMMON: return "COMMON";
10292c23cb7cSEd Maste 	case STT_TLS: return "TLS";
10302c23cb7cSEd Maste 	default:
1031b6b6f9ccSEd Maste 		if (stype >= STT_LOOS && stype <= STT_HIOS) {
1032b6b6f9ccSEd Maste 			if ((os == ELFOSABI_GNU || os == ELFOSABI_FREEBSD) &&
1033b6b6f9ccSEd Maste 			    stype == STT_GNU_IFUNC)
1034b6b6f9ccSEd Maste 				return "IFUNC";
10352c23cb7cSEd Maste 			snprintf(s_stype, sizeof(s_stype), "OS+%#x",
10362c23cb7cSEd Maste 			    stype - STT_LOOS);
1037b6b6f9ccSEd Maste 		} else if (stype >= STT_LOPROC && stype <= STT_HIPROC) {
1038839529caSEd Maste 			if (mach == EM_SPARCV9 && stype == STT_SPARC_REGISTER)
1039839529caSEd Maste 				return "REGISTER";
10402c23cb7cSEd Maste 			snprintf(s_stype, sizeof(s_stype), "PROC+%#x",
10412c23cb7cSEd Maste 			    stype - STT_LOPROC);
1042839529caSEd Maste 		} else
10432c23cb7cSEd Maste 			snprintf(s_stype, sizeof(s_stype), "<unknown: %#x>",
10442c23cb7cSEd Maste 			    stype);
10452c23cb7cSEd Maste 		return (s_stype);
10462c23cb7cSEd Maste 	}
10472c23cb7cSEd Maste }
10482c23cb7cSEd Maste 
10492c23cb7cSEd Maste static const char *
10502c23cb7cSEd Maste st_vis(unsigned int svis)
10512c23cb7cSEd Maste {
10522c23cb7cSEd Maste 	static char s_svis[32];
10532c23cb7cSEd Maste 
10542c23cb7cSEd Maste 	switch(svis) {
10552c23cb7cSEd Maste 	case STV_DEFAULT: return "DEFAULT";
10562c23cb7cSEd Maste 	case STV_INTERNAL: return "INTERNAL";
10572c23cb7cSEd Maste 	case STV_HIDDEN: return "HIDDEN";
10582c23cb7cSEd Maste 	case STV_PROTECTED: return "PROTECTED";
10592c23cb7cSEd Maste 	default:
10602c23cb7cSEd Maste 		snprintf(s_svis, sizeof(s_svis), "<unknown: %#x>", svis);
10612c23cb7cSEd Maste 		return (s_svis);
10622c23cb7cSEd Maste 	}
10632c23cb7cSEd Maste }
10642c23cb7cSEd Maste 
10652c23cb7cSEd Maste static const char *
10662c23cb7cSEd Maste st_shndx(unsigned int shndx)
10672c23cb7cSEd Maste {
10682c23cb7cSEd Maste 	static char s_shndx[32];
10692c23cb7cSEd Maste 
10702c23cb7cSEd Maste 	switch (shndx) {
10712c23cb7cSEd Maste 	case SHN_UNDEF: return "UND";
10722c23cb7cSEd Maste 	case SHN_ABS: return "ABS";
10732c23cb7cSEd Maste 	case SHN_COMMON: return "COM";
10742c23cb7cSEd Maste 	default:
10752c23cb7cSEd Maste 		if (shndx >= SHN_LOPROC && shndx <= SHN_HIPROC)
10762c23cb7cSEd Maste 			return "PRC";
10772c23cb7cSEd Maste 		else if (shndx >= SHN_LOOS && shndx <= SHN_HIOS)
10782c23cb7cSEd Maste 			return "OS";
10792c23cb7cSEd Maste 		else
10802c23cb7cSEd Maste 			snprintf(s_shndx, sizeof(s_shndx), "%u", shndx);
10812c23cb7cSEd Maste 		return (s_shndx);
10822c23cb7cSEd Maste 	}
10832c23cb7cSEd Maste }
10842c23cb7cSEd Maste 
10852c23cb7cSEd Maste static struct {
10862c23cb7cSEd Maste 	const char *ln;
10872c23cb7cSEd Maste 	char sn;
10882c23cb7cSEd Maste 	int value;
10892c23cb7cSEd Maste } section_flag[] = {
10902c23cb7cSEd Maste 	{"WRITE", 'W', SHF_WRITE},
10912c23cb7cSEd Maste 	{"ALLOC", 'A', SHF_ALLOC},
10922c23cb7cSEd Maste 	{"EXEC", 'X', SHF_EXECINSTR},
10932c23cb7cSEd Maste 	{"MERGE", 'M', SHF_MERGE},
10942c23cb7cSEd Maste 	{"STRINGS", 'S', SHF_STRINGS},
10952c23cb7cSEd Maste 	{"INFO LINK", 'I', SHF_INFO_LINK},
10962c23cb7cSEd Maste 	{"OS NONCONF", 'O', SHF_OS_NONCONFORMING},
10972c23cb7cSEd Maste 	{"GROUP", 'G', SHF_GROUP},
10982c23cb7cSEd Maste 	{"TLS", 'T', SHF_TLS},
1099b6b6f9ccSEd Maste 	{"COMPRESSED", 'C', SHF_COMPRESSED},
11002c23cb7cSEd Maste 	{NULL, 0, 0}
11012c23cb7cSEd Maste };
11022c23cb7cSEd Maste 
11032c23cb7cSEd Maste static const char *
110402b08c90SEd Maste note_type(const char *name, unsigned int et, unsigned int nt)
110502b08c90SEd Maste {
110671a0c925SEd Maste 	if ((strcmp(name, "CORE") == 0 || strcmp(name, "LINUX") == 0) &&
110771a0c925SEd Maste 	    et == ET_CORE)
110802b08c90SEd Maste 		return note_type_linux_core(nt);
110902b08c90SEd Maste 	else if (strcmp(name, "FreeBSD") == 0)
111002b08c90SEd Maste 		if (et == ET_CORE)
111102b08c90SEd Maste 			return note_type_freebsd_core(nt);
111202b08c90SEd Maste 		else
111302b08c90SEd Maste 			return note_type_freebsd(nt);
111402b08c90SEd Maste 	else if (strcmp(name, "GNU") == 0 && et != ET_CORE)
111502b08c90SEd Maste 		return note_type_gnu(nt);
111602b08c90SEd Maste 	else if (strcmp(name, "NetBSD") == 0 && et != ET_CORE)
111702b08c90SEd Maste 		return note_type_netbsd(nt);
111802b08c90SEd Maste 	else if (strcmp(name, "OpenBSD") == 0 && et != ET_CORE)
111902b08c90SEd Maste 		return note_type_openbsd(nt);
1120895f86f1SEd Maste 	else if (strcmp(name, "Xen") == 0 && et != ET_CORE)
1121895f86f1SEd Maste 		return note_type_xen(nt);
112202b08c90SEd Maste 	return note_type_unknown(nt);
112302b08c90SEd Maste }
112402b08c90SEd Maste 
112502b08c90SEd Maste static const char *
112602b08c90SEd Maste note_type_freebsd(unsigned int nt)
112702b08c90SEd Maste {
112802b08c90SEd Maste 	switch (nt) {
112902b08c90SEd Maste 	case 1: return "NT_FREEBSD_ABI_TAG";
113002b08c90SEd Maste 	case 2: return "NT_FREEBSD_NOINIT_TAG";
113102b08c90SEd Maste 	case 3: return "NT_FREEBSD_ARCH_TAG";
1132e74f411dSEd Maste 	case 4: return "NT_FREEBSD_FEATURE_CTL";
113302b08c90SEd Maste 	default: return (note_type_unknown(nt));
113402b08c90SEd Maste 	}
113502b08c90SEd Maste }
113602b08c90SEd Maste 
113702b08c90SEd Maste static const char *
113802b08c90SEd Maste note_type_freebsd_core(unsigned int nt)
113902b08c90SEd Maste {
114002b08c90SEd Maste 	switch (nt) {
114102b08c90SEd Maste 	case 1: return "NT_PRSTATUS";
114202b08c90SEd Maste 	case 2: return "NT_FPREGSET";
114302b08c90SEd Maste 	case 3: return "NT_PRPSINFO";
114402b08c90SEd Maste 	case 7: return "NT_THRMISC";
114502b08c90SEd Maste 	case 8: return "NT_PROCSTAT_PROC";
114602b08c90SEd Maste 	case 9: return "NT_PROCSTAT_FILES";
114702b08c90SEd Maste 	case 10: return "NT_PROCSTAT_VMMAP";
114802b08c90SEd Maste 	case 11: return "NT_PROCSTAT_GROUPS";
114902b08c90SEd Maste 	case 12: return "NT_PROCSTAT_UMASK";
115002b08c90SEd Maste 	case 13: return "NT_PROCSTAT_RLIMIT";
115102b08c90SEd Maste 	case 14: return "NT_PROCSTAT_OSREL";
115202b08c90SEd Maste 	case 15: return "NT_PROCSTAT_PSSTRINGS";
115302b08c90SEd Maste 	case 16: return "NT_PROCSTAT_AUXV";
1154369bd05bSJohn Baldwin 	case 17: return "NT_PTLWPINFO";
115502b08c90SEd Maste 	case 0x202: return "NT_X86_XSTATE (x86 XSAVE extended state)";
1156369bd05bSJohn Baldwin 	case 0x400: return "NT_ARM_VFP (arm VFP registers)";
115702b08c90SEd Maste 	default: return (note_type_unknown(nt));
115802b08c90SEd Maste 	}
115902b08c90SEd Maste }
116002b08c90SEd Maste 
116102b08c90SEd Maste static const char *
116202b08c90SEd Maste note_type_linux_core(unsigned int nt)
116302b08c90SEd Maste {
116402b08c90SEd Maste 	switch (nt) {
116502b08c90SEd Maste 	case 1: return "NT_PRSTATUS (Process status)";
116602b08c90SEd Maste 	case 2: return "NT_FPREGSET (Floating point information)";
116702b08c90SEd Maste 	case 3: return "NT_PRPSINFO (Process information)";
116871a0c925SEd Maste 	case 4: return "NT_TASKSTRUCT (Task structure)";
116902b08c90SEd Maste 	case 6: return "NT_AUXV (Auxiliary vector)";
117002b08c90SEd Maste 	case 10: return "NT_PSTATUS (Linux process status)";
117102b08c90SEd Maste 	case 12: return "NT_FPREGS (Linux floating point regset)";
117202b08c90SEd Maste 	case 13: return "NT_PSINFO (Linux process information)";
117302b08c90SEd Maste 	case 16: return "NT_LWPSTATUS (Linux lwpstatus_t type)";
117402b08c90SEd Maste 	case 17: return "NT_LWPSINFO (Linux lwpinfo_t type)";
117571a0c925SEd Maste 	case 18: return "NT_WIN32PSTATUS (win32_pstatus structure)";
117671a0c925SEd Maste 	case 0x100: return "NT_PPC_VMX (ppc Altivec registers)";
117771a0c925SEd Maste 	case 0x102: return "NT_PPC_VSX (ppc VSX registers)";
117871a0c925SEd Maste 	case 0x202: return "NT_X86_XSTATE (x86 XSAVE extended state)";
117971a0c925SEd Maste 	case 0x300: return "NT_S390_HIGH_GPRS (s390 upper register halves)";
118071a0c925SEd Maste 	case 0x301: return "NT_S390_TIMER (s390 timer register)";
118171a0c925SEd Maste 	case 0x302: return "NT_S390_TODCMP (s390 TOD comparator register)";
118271a0c925SEd Maste 	case 0x303: return "NT_S390_TODPREG (s390 TOD programmable register)";
118371a0c925SEd Maste 	case 0x304: return "NT_S390_CTRS (s390 control registers)";
118471a0c925SEd Maste 	case 0x305: return "NT_S390_PREFIX (s390 prefix register)";
118571a0c925SEd Maste 	case 0x400: return "NT_ARM_VFP (arm VFP registers)";
118671a0c925SEd Maste 	case 0x46494c45UL: return "NT_FILE (mapped files)";
118771a0c925SEd Maste 	case 0x46E62B7FUL: return "NT_PRXFPREG (Linux user_xfpregs structure)";
118871a0c925SEd Maste 	case 0x53494749UL: return "NT_SIGINFO (siginfo_t data)";
118902b08c90SEd Maste 	default: return (note_type_unknown(nt));
119002b08c90SEd Maste 	}
119102b08c90SEd Maste }
119202b08c90SEd Maste 
119302b08c90SEd Maste static const char *
119402b08c90SEd Maste note_type_gnu(unsigned int nt)
119502b08c90SEd Maste {
119602b08c90SEd Maste 	switch (nt) {
119702b08c90SEd Maste 	case 1: return "NT_GNU_ABI_TAG";
119802b08c90SEd Maste 	case 2: return "NT_GNU_HWCAP (Hardware capabilities)";
119902b08c90SEd Maste 	case 3: return "NT_GNU_BUILD_ID (Build id set by ld(1))";
120002b08c90SEd Maste 	case 4: return "NT_GNU_GOLD_VERSION (GNU gold version)";
1201ce0c6340SEd Maste 	case 5: return "NT_GNU_PROPERTY_TYPE_0";
120202b08c90SEd Maste 	default: return (note_type_unknown(nt));
120302b08c90SEd Maste 	}
120402b08c90SEd Maste }
120502b08c90SEd Maste 
120602b08c90SEd Maste static const char *
120702b08c90SEd Maste note_type_netbsd(unsigned int nt)
120802b08c90SEd Maste {
120902b08c90SEd Maste 	switch (nt) {
121002b08c90SEd Maste 	case 1: return "NT_NETBSD_IDENT";
121102b08c90SEd Maste 	default: return (note_type_unknown(nt));
121202b08c90SEd Maste 	}
121302b08c90SEd Maste }
121402b08c90SEd Maste 
121502b08c90SEd Maste static const char *
121602b08c90SEd Maste note_type_openbsd(unsigned int nt)
121702b08c90SEd Maste {
121802b08c90SEd Maste 	switch (nt) {
121902b08c90SEd Maste 	case 1: return "NT_OPENBSD_IDENT";
122002b08c90SEd Maste 	default: return (note_type_unknown(nt));
122102b08c90SEd Maste 	}
122202b08c90SEd Maste }
122302b08c90SEd Maste 
122402b08c90SEd Maste static const char *
122502b08c90SEd Maste note_type_unknown(unsigned int nt)
12262c23cb7cSEd Maste {
12272c23cb7cSEd Maste 	static char s_nt[32];
12282c23cb7cSEd Maste 
122971a0c925SEd Maste 	snprintf(s_nt, sizeof(s_nt),
123071a0c925SEd Maste 	    nt >= 0x100 ? "<unknown: 0x%x>" : "<unknown: %u>", nt);
12312c23cb7cSEd Maste 	return (s_nt);
12322c23cb7cSEd Maste }
12332c23cb7cSEd Maste 
1234895f86f1SEd Maste static const char *
1235895f86f1SEd Maste note_type_xen(unsigned int nt)
1236895f86f1SEd Maste {
1237895f86f1SEd Maste 	switch (nt) {
1238895f86f1SEd Maste 	case 0: return "XEN_ELFNOTE_INFO";
1239895f86f1SEd Maste 	case 1: return "XEN_ELFNOTE_ENTRY";
1240895f86f1SEd Maste 	case 2: return "XEN_ELFNOTE_HYPERCALL_PAGE";
1241895f86f1SEd Maste 	case 3: return "XEN_ELFNOTE_VIRT_BASE";
1242895f86f1SEd Maste 	case 4: return "XEN_ELFNOTE_PADDR_OFFSET";
1243895f86f1SEd Maste 	case 5: return "XEN_ELFNOTE_XEN_VERSION";
1244895f86f1SEd Maste 	case 6: return "XEN_ELFNOTE_GUEST_OS";
1245895f86f1SEd Maste 	case 7: return "XEN_ELFNOTE_GUEST_VERSION";
1246895f86f1SEd Maste 	case 8: return "XEN_ELFNOTE_LOADER";
1247895f86f1SEd Maste 	case 9: return "XEN_ELFNOTE_PAE_MODE";
1248895f86f1SEd Maste 	case 10: return "XEN_ELFNOTE_FEATURES";
1249895f86f1SEd Maste 	case 11: return "XEN_ELFNOTE_BSD_SYMTAB";
1250895f86f1SEd Maste 	case 12: return "XEN_ELFNOTE_HV_START_LOW";
1251895f86f1SEd Maste 	case 13: return "XEN_ELFNOTE_L1_MFN_VALID";
1252895f86f1SEd Maste 	case 14: return "XEN_ELFNOTE_SUSPEND_CANCEL";
1253895f86f1SEd Maste 	case 15: return "XEN_ELFNOTE_INIT_P2M";
1254895f86f1SEd Maste 	case 16: return "XEN_ELFNOTE_MOD_START_PFN";
1255895f86f1SEd Maste 	case 17: return "XEN_ELFNOTE_SUPPORTED_FEATURES";
1256895f86f1SEd Maste 	default: return (note_type_unknown(nt));
1257895f86f1SEd Maste 	}
1258895f86f1SEd Maste }
1259895f86f1SEd Maste 
12602c23cb7cSEd Maste static struct {
12612c23cb7cSEd Maste 	const char *name;
12622c23cb7cSEd Maste 	int value;
12632c23cb7cSEd Maste } l_flag[] = {
12642c23cb7cSEd Maste 	{"EXACT_MATCH", LL_EXACT_MATCH},
12652c23cb7cSEd Maste 	{"IGNORE_INT_VER", LL_IGNORE_INT_VER},
12662c23cb7cSEd Maste 	{"REQUIRE_MINOR", LL_REQUIRE_MINOR},
12672c23cb7cSEd Maste 	{"EXPORTS", LL_EXPORTS},
12682c23cb7cSEd Maste 	{"DELAY_LOAD", LL_DELAY_LOAD},
12692c23cb7cSEd Maste 	{"DELTA", LL_DELTA},
12702c23cb7cSEd Maste 	{NULL, 0}
12712c23cb7cSEd Maste };
12722c23cb7cSEd Maste 
12732c23cb7cSEd Maste static struct mips_option mips_exceptions_option[] = {
12742c23cb7cSEd Maste 	{OEX_PAGE0, "PAGE0"},
12752c23cb7cSEd Maste 	{OEX_SMM, "SMM"},
12762c23cb7cSEd Maste 	{OEX_PRECISEFP, "PRECISEFP"},
12772c23cb7cSEd Maste 	{OEX_DISMISS, "DISMISS"},
12782c23cb7cSEd Maste 	{0, NULL}
12792c23cb7cSEd Maste };
12802c23cb7cSEd Maste 
12812c23cb7cSEd Maste static struct mips_option mips_pad_option[] = {
12822c23cb7cSEd Maste 	{OPAD_PREFIX, "PREFIX"},
12832c23cb7cSEd Maste 	{OPAD_POSTFIX, "POSTFIX"},
12842c23cb7cSEd Maste 	{OPAD_SYMBOL, "SYMBOL"},
12852c23cb7cSEd Maste 	{0, NULL}
12862c23cb7cSEd Maste };
12872c23cb7cSEd Maste 
12882c23cb7cSEd Maste static struct mips_option mips_hwpatch_option[] = {
12892c23cb7cSEd Maste 	{OHW_R4KEOP, "R4KEOP"},
12902c23cb7cSEd Maste 	{OHW_R8KPFETCH, "R8KPFETCH"},
12912c23cb7cSEd Maste 	{OHW_R5KEOP, "R5KEOP"},
12922c23cb7cSEd Maste 	{OHW_R5KCVTL, "R5KCVTL"},
12932c23cb7cSEd Maste 	{0, NULL}
12942c23cb7cSEd Maste };
12952c23cb7cSEd Maste 
12962c23cb7cSEd Maste static struct mips_option mips_hwa_option[] = {
12972c23cb7cSEd Maste 	{OHWA0_R4KEOP_CHECKED, "R4KEOP_CHECKED"},
12982c23cb7cSEd Maste 	{OHWA0_R4KEOP_CLEAN, "R4KEOP_CLEAN"},
12992c23cb7cSEd Maste 	{0, NULL}
13002c23cb7cSEd Maste };
13012c23cb7cSEd Maste 
13022c23cb7cSEd Maste static struct mips_option mips_hwo_option[] = {
13032c23cb7cSEd Maste 	{OHWO0_FIXADE, "FIXADE"},
13042c23cb7cSEd Maste 	{0, NULL}
13052c23cb7cSEd Maste };
13062c23cb7cSEd Maste 
13072c23cb7cSEd Maste static const char *
13082c23cb7cSEd Maste option_kind(uint8_t kind)
13092c23cb7cSEd Maste {
13102c23cb7cSEd Maste 	static char s_kind[32];
13112c23cb7cSEd Maste 
13122c23cb7cSEd Maste 	switch (kind) {
13132c23cb7cSEd Maste 	case ODK_NULL: return "NULL";
13142c23cb7cSEd Maste 	case ODK_REGINFO: return "REGINFO";
13152c23cb7cSEd Maste 	case ODK_EXCEPTIONS: return "EXCEPTIONS";
13162c23cb7cSEd Maste 	case ODK_PAD: return "PAD";
13172c23cb7cSEd Maste 	case ODK_HWPATCH: return "HWPATCH";
13182c23cb7cSEd Maste 	case ODK_FILL: return "FILL";
13192c23cb7cSEd Maste 	case ODK_TAGS: return "TAGS";
13202c23cb7cSEd Maste 	case ODK_HWAND: return "HWAND";
13212c23cb7cSEd Maste 	case ODK_HWOR: return "HWOR";
13222c23cb7cSEd Maste 	case ODK_GP_GROUP: return "GP_GROUP";
13232c23cb7cSEd Maste 	case ODK_IDENT: return "IDENT";
13242c23cb7cSEd Maste 	default:
13252c23cb7cSEd Maste 		snprintf(s_kind, sizeof(s_kind), "<unknown: %u>", kind);
13262c23cb7cSEd Maste 		return (s_kind);
13272c23cb7cSEd Maste 	}
13282c23cb7cSEd Maste }
13292c23cb7cSEd Maste 
13302c23cb7cSEd Maste static const char *
13312c23cb7cSEd Maste top_tag(unsigned int tag)
13322c23cb7cSEd Maste {
13332c23cb7cSEd Maste 	static char s_top_tag[32];
13342c23cb7cSEd Maste 
13352c23cb7cSEd Maste 	switch (tag) {
13362c23cb7cSEd Maste 	case 1: return "File Attributes";
13372c23cb7cSEd Maste 	case 2: return "Section Attributes";
13382c23cb7cSEd Maste 	case 3: return "Symbol Attributes";
13392c23cb7cSEd Maste 	default:
13402c23cb7cSEd Maste 		snprintf(s_top_tag, sizeof(s_top_tag), "Unknown tag: %u", tag);
13412c23cb7cSEd Maste 		return (s_top_tag);
13422c23cb7cSEd Maste 	}
13432c23cb7cSEd Maste }
13442c23cb7cSEd Maste 
13452c23cb7cSEd Maste static const char *
13462c23cb7cSEd Maste aeabi_cpu_arch(uint64_t arch)
13472c23cb7cSEd Maste {
13482c23cb7cSEd Maste 	static char s_cpu_arch[32];
13492c23cb7cSEd Maste 
13502c23cb7cSEd Maste 	switch (arch) {
13512c23cb7cSEd Maste 	case 0: return "Pre-V4";
13522c23cb7cSEd Maste 	case 1: return "ARM v4";
13532c23cb7cSEd Maste 	case 2: return "ARM v4T";
13542c23cb7cSEd Maste 	case 3: return "ARM v5T";
13552c23cb7cSEd Maste 	case 4: return "ARM v5TE";
13562c23cb7cSEd Maste 	case 5: return "ARM v5TEJ";
13572c23cb7cSEd Maste 	case 6: return "ARM v6";
13582c23cb7cSEd Maste 	case 7: return "ARM v6KZ";
13592c23cb7cSEd Maste 	case 8: return "ARM v6T2";
13602c23cb7cSEd Maste 	case 9: return "ARM v6K";
13612c23cb7cSEd Maste 	case 10: return "ARM v7";
13622c23cb7cSEd Maste 	case 11: return "ARM v6-M";
13632c23cb7cSEd Maste 	case 12: return "ARM v6S-M";
13642c23cb7cSEd Maste 	case 13: return "ARM v7E-M";
13652c23cb7cSEd Maste 	default:
13662c23cb7cSEd Maste 		snprintf(s_cpu_arch, sizeof(s_cpu_arch),
13672c23cb7cSEd Maste 		    "Unknown (%ju)", (uintmax_t) arch);
13682c23cb7cSEd Maste 		return (s_cpu_arch);
13692c23cb7cSEd Maste 	}
13702c23cb7cSEd Maste }
13712c23cb7cSEd Maste 
13722c23cb7cSEd Maste static const char *
13732c23cb7cSEd Maste aeabi_cpu_arch_profile(uint64_t pf)
13742c23cb7cSEd Maste {
13752c23cb7cSEd Maste 	static char s_arch_profile[32];
13762c23cb7cSEd Maste 
13772c23cb7cSEd Maste 	switch (pf) {
13782c23cb7cSEd Maste 	case 0:
13792c23cb7cSEd Maste 		return "Not applicable";
13802c23cb7cSEd Maste 	case 0x41:		/* 'A' */
13812c23cb7cSEd Maste 		return "Application Profile";
13822c23cb7cSEd Maste 	case 0x52:		/* 'R' */
13832c23cb7cSEd Maste 		return "Real-Time Profile";
13842c23cb7cSEd Maste 	case 0x4D:		/* 'M' */
13852c23cb7cSEd Maste 		return "Microcontroller Profile";
13862c23cb7cSEd Maste 	case 0x53:		/* 'S' */
13872c23cb7cSEd Maste 		return "Application or Real-Time Profile";
13882c23cb7cSEd Maste 	default:
13892c23cb7cSEd Maste 		snprintf(s_arch_profile, sizeof(s_arch_profile),
13902c23cb7cSEd Maste 		    "Unknown (%ju)\n", (uintmax_t) pf);
13912c23cb7cSEd Maste 		return (s_arch_profile);
13922c23cb7cSEd Maste 	}
13932c23cb7cSEd Maste }
13942c23cb7cSEd Maste 
13952c23cb7cSEd Maste static const char *
13962c23cb7cSEd Maste aeabi_arm_isa(uint64_t ai)
13972c23cb7cSEd Maste {
13982c23cb7cSEd Maste 	static char s_ai[32];
13992c23cb7cSEd Maste 
14002c23cb7cSEd Maste 	switch (ai) {
14012c23cb7cSEd Maste 	case 0: return "No";
14022c23cb7cSEd Maste 	case 1: return "Yes";
14032c23cb7cSEd Maste 	default:
14042c23cb7cSEd Maste 		snprintf(s_ai, sizeof(s_ai), "Unknown (%ju)\n",
14052c23cb7cSEd Maste 		    (uintmax_t) ai);
14062c23cb7cSEd Maste 		return (s_ai);
14072c23cb7cSEd Maste 	}
14082c23cb7cSEd Maste }
14092c23cb7cSEd Maste 
14102c23cb7cSEd Maste static const char *
14112c23cb7cSEd Maste aeabi_thumb_isa(uint64_t ti)
14122c23cb7cSEd Maste {
14132c23cb7cSEd Maste 	static char s_ti[32];
14142c23cb7cSEd Maste 
14152c23cb7cSEd Maste 	switch (ti) {
14162c23cb7cSEd Maste 	case 0: return "No";
14172c23cb7cSEd Maste 	case 1: return "16-bit Thumb";
14182c23cb7cSEd Maste 	case 2: return "32-bit Thumb";
14192c23cb7cSEd Maste 	default:
14202c23cb7cSEd Maste 		snprintf(s_ti, sizeof(s_ti), "Unknown (%ju)\n",
14212c23cb7cSEd Maste 		    (uintmax_t) ti);
14222c23cb7cSEd Maste 		return (s_ti);
14232c23cb7cSEd Maste 	}
14242c23cb7cSEd Maste }
14252c23cb7cSEd Maste 
14262c23cb7cSEd Maste static const char *
14272c23cb7cSEd Maste aeabi_fp_arch(uint64_t fp)
14282c23cb7cSEd Maste {
14292c23cb7cSEd Maste 	static char s_fp_arch[32];
14302c23cb7cSEd Maste 
14312c23cb7cSEd Maste 	switch (fp) {
14322c23cb7cSEd Maste 	case 0: return "No";
14332c23cb7cSEd Maste 	case 1: return "VFPv1";
14342c23cb7cSEd Maste 	case 2: return "VFPv2";
14352c23cb7cSEd Maste 	case 3: return "VFPv3";
14362c23cb7cSEd Maste 	case 4: return "VFPv3-D16";
14372c23cb7cSEd Maste 	case 5: return "VFPv4";
14382c23cb7cSEd Maste 	case 6: return "VFPv4-D16";
14392c23cb7cSEd Maste 	default:
14402c23cb7cSEd Maste 		snprintf(s_fp_arch, sizeof(s_fp_arch), "Unknown (%ju)",
14412c23cb7cSEd Maste 		    (uintmax_t) fp);
14422c23cb7cSEd Maste 		return (s_fp_arch);
14432c23cb7cSEd Maste 	}
14442c23cb7cSEd Maste }
14452c23cb7cSEd Maste 
14462c23cb7cSEd Maste static const char *
14472c23cb7cSEd Maste aeabi_wmmx_arch(uint64_t wmmx)
14482c23cb7cSEd Maste {
14492c23cb7cSEd Maste 	static char s_wmmx[32];
14502c23cb7cSEd Maste 
14512c23cb7cSEd Maste 	switch (wmmx) {
14522c23cb7cSEd Maste 	case 0: return "No";
14532c23cb7cSEd Maste 	case 1: return "WMMXv1";
14542c23cb7cSEd Maste 	case 2: return "WMMXv2";
14552c23cb7cSEd Maste 	default:
14562c23cb7cSEd Maste 		snprintf(s_wmmx, sizeof(s_wmmx), "Unknown (%ju)",
14572c23cb7cSEd Maste 		    (uintmax_t) wmmx);
14582c23cb7cSEd Maste 		return (s_wmmx);
14592c23cb7cSEd Maste 	}
14602c23cb7cSEd Maste }
14612c23cb7cSEd Maste 
14622c23cb7cSEd Maste static const char *
14632c23cb7cSEd Maste aeabi_adv_simd_arch(uint64_t simd)
14642c23cb7cSEd Maste {
14652c23cb7cSEd Maste 	static char s_simd[32];
14662c23cb7cSEd Maste 
14672c23cb7cSEd Maste 	switch (simd) {
14682c23cb7cSEd Maste 	case 0: return "No";
14692c23cb7cSEd Maste 	case 1: return "NEONv1";
14702c23cb7cSEd Maste 	case 2: return "NEONv2";
14712c23cb7cSEd Maste 	default:
14722c23cb7cSEd Maste 		snprintf(s_simd, sizeof(s_simd), "Unknown (%ju)",
14732c23cb7cSEd Maste 		    (uintmax_t) simd);
14742c23cb7cSEd Maste 		return (s_simd);
14752c23cb7cSEd Maste 	}
14762c23cb7cSEd Maste }
14772c23cb7cSEd Maste 
14782c23cb7cSEd Maste static const char *
14792c23cb7cSEd Maste aeabi_pcs_config(uint64_t pcs)
14802c23cb7cSEd Maste {
14812c23cb7cSEd Maste 	static char s_pcs[32];
14822c23cb7cSEd Maste 
14832c23cb7cSEd Maste 	switch (pcs) {
14842c23cb7cSEd Maste 	case 0: return "None";
14852c23cb7cSEd Maste 	case 1: return "Bare platform";
14862c23cb7cSEd Maste 	case 2: return "Linux";
14872c23cb7cSEd Maste 	case 3: return "Linux DSO";
14882c23cb7cSEd Maste 	case 4: return "Palm OS 2004";
14892c23cb7cSEd Maste 	case 5: return "Palm OS (future)";
14902c23cb7cSEd Maste 	case 6: return "Symbian OS 2004";
14912c23cb7cSEd Maste 	case 7: return "Symbian OS (future)";
14922c23cb7cSEd Maste 	default:
14932c23cb7cSEd Maste 		snprintf(s_pcs, sizeof(s_pcs), "Unknown (%ju)",
14942c23cb7cSEd Maste 		    (uintmax_t) pcs);
14952c23cb7cSEd Maste 		return (s_pcs);
14962c23cb7cSEd Maste 	}
14972c23cb7cSEd Maste }
14982c23cb7cSEd Maste 
14992c23cb7cSEd Maste static const char *
15002c23cb7cSEd Maste aeabi_pcs_r9(uint64_t r9)
15012c23cb7cSEd Maste {
15022c23cb7cSEd Maste 	static char s_r9[32];
15032c23cb7cSEd Maste 
15042c23cb7cSEd Maste 	switch (r9) {
15052c23cb7cSEd Maste 	case 0: return "V6";
15062c23cb7cSEd Maste 	case 1: return "SB";
15072c23cb7cSEd Maste 	case 2: return "TLS pointer";
15082c23cb7cSEd Maste 	case 3: return "Unused";
15092c23cb7cSEd Maste 	default:
15102c23cb7cSEd Maste 		snprintf(s_r9, sizeof(s_r9), "Unknown (%ju)", (uintmax_t) r9);
15112c23cb7cSEd Maste 		return (s_r9);
15122c23cb7cSEd Maste 	}
15132c23cb7cSEd Maste }
15142c23cb7cSEd Maste 
15152c23cb7cSEd Maste static const char *
15162c23cb7cSEd Maste aeabi_pcs_rw(uint64_t rw)
15172c23cb7cSEd Maste {
15182c23cb7cSEd Maste 	static char s_rw[32];
15192c23cb7cSEd Maste 
15202c23cb7cSEd Maste 	switch (rw) {
15212c23cb7cSEd Maste 	case 0: return "Absolute";
15222c23cb7cSEd Maste 	case 1: return "PC-relative";
15232c23cb7cSEd Maste 	case 2: return "SB-relative";
15242c23cb7cSEd Maste 	case 3: return "None";
15252c23cb7cSEd Maste 	default:
15262c23cb7cSEd Maste 		snprintf(s_rw, sizeof(s_rw), "Unknown (%ju)", (uintmax_t) rw);
15272c23cb7cSEd Maste 		return (s_rw);
15282c23cb7cSEd Maste 	}
15292c23cb7cSEd Maste }
15302c23cb7cSEd Maste 
15312c23cb7cSEd Maste static const char *
15322c23cb7cSEd Maste aeabi_pcs_ro(uint64_t ro)
15332c23cb7cSEd Maste {
15342c23cb7cSEd Maste 	static char s_ro[32];
15352c23cb7cSEd Maste 
15362c23cb7cSEd Maste 	switch (ro) {
15372c23cb7cSEd Maste 	case 0: return "Absolute";
15382c23cb7cSEd Maste 	case 1: return "PC-relative";
15392c23cb7cSEd Maste 	case 2: return "None";
15402c23cb7cSEd Maste 	default:
15412c23cb7cSEd Maste 		snprintf(s_ro, sizeof(s_ro), "Unknown (%ju)", (uintmax_t) ro);
15422c23cb7cSEd Maste 		return (s_ro);
15432c23cb7cSEd Maste 	}
15442c23cb7cSEd Maste }
15452c23cb7cSEd Maste 
15462c23cb7cSEd Maste static const char *
15472c23cb7cSEd Maste aeabi_pcs_got(uint64_t got)
15482c23cb7cSEd Maste {
15492c23cb7cSEd Maste 	static char s_got[32];
15502c23cb7cSEd Maste 
15512c23cb7cSEd Maste 	switch (got) {
15522c23cb7cSEd Maste 	case 0: return "None";
15532c23cb7cSEd Maste 	case 1: return "direct";
15542c23cb7cSEd Maste 	case 2: return "indirect via GOT";
15552c23cb7cSEd Maste 	default:
15562c23cb7cSEd Maste 		snprintf(s_got, sizeof(s_got), "Unknown (%ju)",
15572c23cb7cSEd Maste 		    (uintmax_t) got);
15582c23cb7cSEd Maste 		return (s_got);
15592c23cb7cSEd Maste 	}
15602c23cb7cSEd Maste }
15612c23cb7cSEd Maste 
15622c23cb7cSEd Maste static const char *
15632c23cb7cSEd Maste aeabi_pcs_wchar_t(uint64_t wt)
15642c23cb7cSEd Maste {
15652c23cb7cSEd Maste 	static char s_wt[32];
15662c23cb7cSEd Maste 
15672c23cb7cSEd Maste 	switch (wt) {
15682c23cb7cSEd Maste 	case 0: return "None";
15692c23cb7cSEd Maste 	case 2: return "wchar_t size 2";
15702c23cb7cSEd Maste 	case 4: return "wchar_t size 4";
15712c23cb7cSEd Maste 	default:
15722c23cb7cSEd Maste 		snprintf(s_wt, sizeof(s_wt), "Unknown (%ju)", (uintmax_t) wt);
15732c23cb7cSEd Maste 		return (s_wt);
15742c23cb7cSEd Maste 	}
15752c23cb7cSEd Maste }
15762c23cb7cSEd Maste 
15772c23cb7cSEd Maste static const char *
15782c23cb7cSEd Maste aeabi_enum_size(uint64_t es)
15792c23cb7cSEd Maste {
15802c23cb7cSEd Maste 	static char s_es[32];
15812c23cb7cSEd Maste 
15822c23cb7cSEd Maste 	switch (es) {
15832c23cb7cSEd Maste 	case 0: return "None";
15842c23cb7cSEd Maste 	case 1: return "smallest";
15852c23cb7cSEd Maste 	case 2: return "32-bit";
15862c23cb7cSEd Maste 	case 3: return "visible 32-bit";
15872c23cb7cSEd Maste 	default:
15882c23cb7cSEd Maste 		snprintf(s_es, sizeof(s_es), "Unknown (%ju)", (uintmax_t) es);
15892c23cb7cSEd Maste 		return (s_es);
15902c23cb7cSEd Maste 	}
15912c23cb7cSEd Maste }
15922c23cb7cSEd Maste 
15932c23cb7cSEd Maste static const char *
15942c23cb7cSEd Maste aeabi_align_needed(uint64_t an)
15952c23cb7cSEd Maste {
15962c23cb7cSEd Maste 	static char s_align_n[64];
15972c23cb7cSEd Maste 
15982c23cb7cSEd Maste 	switch (an) {
15992c23cb7cSEd Maste 	case 0: return "No";
16002c23cb7cSEd Maste 	case 1: return "8-byte align";
16012c23cb7cSEd Maste 	case 2: return "4-byte align";
16022c23cb7cSEd Maste 	case 3: return "Reserved";
16032c23cb7cSEd Maste 	default:
16042c23cb7cSEd Maste 		if (an >= 4 && an <= 12)
16052c23cb7cSEd Maste 			snprintf(s_align_n, sizeof(s_align_n), "8-byte align"
16062c23cb7cSEd Maste 			    " and up to 2^%ju-byte extended align",
16072c23cb7cSEd Maste 			    (uintmax_t) an);
16082c23cb7cSEd Maste 		else
16092c23cb7cSEd Maste 			snprintf(s_align_n, sizeof(s_align_n), "Unknown (%ju)",
16102c23cb7cSEd Maste 			    (uintmax_t) an);
16112c23cb7cSEd Maste 		return (s_align_n);
16122c23cb7cSEd Maste 	}
16132c23cb7cSEd Maste }
16142c23cb7cSEd Maste 
16152c23cb7cSEd Maste static const char *
16162c23cb7cSEd Maste aeabi_align_preserved(uint64_t ap)
16172c23cb7cSEd Maste {
16182c23cb7cSEd Maste 	static char s_align_p[128];
16192c23cb7cSEd Maste 
16202c23cb7cSEd Maste 	switch (ap) {
16212c23cb7cSEd Maste 	case 0: return "No";
16222c23cb7cSEd Maste 	case 1: return "8-byte align";
16232c23cb7cSEd Maste 	case 2: return "8-byte align and SP % 8 == 0";
16242c23cb7cSEd Maste 	case 3: return "Reserved";
16252c23cb7cSEd Maste 	default:
16262c23cb7cSEd Maste 		if (ap >= 4 && ap <= 12)
16272c23cb7cSEd Maste 			snprintf(s_align_p, sizeof(s_align_p), "8-byte align"
16282c23cb7cSEd Maste 			    " and SP %% 8 == 0 and up to 2^%ju-byte extended"
16292c23cb7cSEd Maste 			    " align", (uintmax_t) ap);
16302c23cb7cSEd Maste 		else
16312c23cb7cSEd Maste 			snprintf(s_align_p, sizeof(s_align_p), "Unknown (%ju)",
16322c23cb7cSEd Maste 			    (uintmax_t) ap);
16332c23cb7cSEd Maste 		return (s_align_p);
16342c23cb7cSEd Maste 	}
16352c23cb7cSEd Maste }
16362c23cb7cSEd Maste 
16372c23cb7cSEd Maste static const char *
16382c23cb7cSEd Maste aeabi_fp_rounding(uint64_t fr)
16392c23cb7cSEd Maste {
16402c23cb7cSEd Maste 	static char s_fp_r[32];
16412c23cb7cSEd Maste 
16422c23cb7cSEd Maste 	switch (fr) {
16432c23cb7cSEd Maste 	case 0: return "Unused";
16442c23cb7cSEd Maste 	case 1: return "Needed";
16452c23cb7cSEd Maste 	default:
16462c23cb7cSEd Maste 		snprintf(s_fp_r, sizeof(s_fp_r), "Unknown (%ju)",
16472c23cb7cSEd Maste 		    (uintmax_t) fr);
16482c23cb7cSEd Maste 		return (s_fp_r);
16492c23cb7cSEd Maste 	}
16502c23cb7cSEd Maste }
16512c23cb7cSEd Maste 
16522c23cb7cSEd Maste static const char *
16532c23cb7cSEd Maste aeabi_fp_denormal(uint64_t fd)
16542c23cb7cSEd Maste {
16552c23cb7cSEd Maste 	static char s_fp_d[32];
16562c23cb7cSEd Maste 
16572c23cb7cSEd Maste 	switch (fd) {
16582c23cb7cSEd Maste 	case 0: return "Unused";
16592c23cb7cSEd Maste 	case 1: return "Needed";
16602c23cb7cSEd Maste 	case 2: return "Sign Only";
16612c23cb7cSEd Maste 	default:
16622c23cb7cSEd Maste 		snprintf(s_fp_d, sizeof(s_fp_d), "Unknown (%ju)",
16632c23cb7cSEd Maste 		    (uintmax_t) fd);
16642c23cb7cSEd Maste 		return (s_fp_d);
16652c23cb7cSEd Maste 	}
16662c23cb7cSEd Maste }
16672c23cb7cSEd Maste 
16682c23cb7cSEd Maste static const char *
16692c23cb7cSEd Maste aeabi_fp_exceptions(uint64_t fe)
16702c23cb7cSEd Maste {
16712c23cb7cSEd Maste 	static char s_fp_e[32];
16722c23cb7cSEd Maste 
16732c23cb7cSEd Maste 	switch (fe) {
16742c23cb7cSEd Maste 	case 0: return "Unused";
16752c23cb7cSEd Maste 	case 1: return "Needed";
16762c23cb7cSEd Maste 	default:
16772c23cb7cSEd Maste 		snprintf(s_fp_e, sizeof(s_fp_e), "Unknown (%ju)",
16782c23cb7cSEd Maste 		    (uintmax_t) fe);
16792c23cb7cSEd Maste 		return (s_fp_e);
16802c23cb7cSEd Maste 	}
16812c23cb7cSEd Maste }
16822c23cb7cSEd Maste 
16832c23cb7cSEd Maste static const char *
16842c23cb7cSEd Maste aeabi_fp_user_exceptions(uint64_t fu)
16852c23cb7cSEd Maste {
16862c23cb7cSEd Maste 	static char s_fp_u[32];
16872c23cb7cSEd Maste 
16882c23cb7cSEd Maste 	switch (fu) {
16892c23cb7cSEd Maste 	case 0: return "Unused";
16902c23cb7cSEd Maste 	case 1: return "Needed";
16912c23cb7cSEd Maste 	default:
16922c23cb7cSEd Maste 		snprintf(s_fp_u, sizeof(s_fp_u), "Unknown (%ju)",
16932c23cb7cSEd Maste 		    (uintmax_t) fu);
16942c23cb7cSEd Maste 		return (s_fp_u);
16952c23cb7cSEd Maste 	}
16962c23cb7cSEd Maste }
16972c23cb7cSEd Maste 
16982c23cb7cSEd Maste static const char *
16992c23cb7cSEd Maste aeabi_fp_number_model(uint64_t fn)
17002c23cb7cSEd Maste {
17012c23cb7cSEd Maste 	static char s_fp_n[32];
17022c23cb7cSEd Maste 
17032c23cb7cSEd Maste 	switch (fn) {
17042c23cb7cSEd Maste 	case 0: return "Unused";
17052c23cb7cSEd Maste 	case 1: return "IEEE 754 normal";
17062c23cb7cSEd Maste 	case 2: return "RTABI";
17072c23cb7cSEd Maste 	case 3: return "IEEE 754";
17082c23cb7cSEd Maste 	default:
17092c23cb7cSEd Maste 		snprintf(s_fp_n, sizeof(s_fp_n), "Unknown (%ju)",
17102c23cb7cSEd Maste 		    (uintmax_t) fn);
17112c23cb7cSEd Maste 		return (s_fp_n);
17122c23cb7cSEd Maste 	}
17132c23cb7cSEd Maste }
17142c23cb7cSEd Maste 
17152c23cb7cSEd Maste static const char *
17162c23cb7cSEd Maste aeabi_fp_16bit_format(uint64_t fp16)
17172c23cb7cSEd Maste {
17182c23cb7cSEd Maste 	static char s_fp_16[64];
17192c23cb7cSEd Maste 
17202c23cb7cSEd Maste 	switch (fp16) {
17212c23cb7cSEd Maste 	case 0: return "None";
17222c23cb7cSEd Maste 	case 1: return "IEEE 754";
17232c23cb7cSEd Maste 	case 2: return "VFPv3/Advanced SIMD (alternative format)";
17242c23cb7cSEd Maste 	default:
17252c23cb7cSEd Maste 		snprintf(s_fp_16, sizeof(s_fp_16), "Unknown (%ju)",
17262c23cb7cSEd Maste 		    (uintmax_t) fp16);
17272c23cb7cSEd Maste 		return (s_fp_16);
17282c23cb7cSEd Maste 	}
17292c23cb7cSEd Maste }
17302c23cb7cSEd Maste 
17312c23cb7cSEd Maste static const char *
17322c23cb7cSEd Maste aeabi_mpext(uint64_t mp)
17332c23cb7cSEd Maste {
17342c23cb7cSEd Maste 	static char s_mp[32];
17352c23cb7cSEd Maste 
17362c23cb7cSEd Maste 	switch (mp) {
17372c23cb7cSEd Maste 	case 0: return "Not allowed";
17382c23cb7cSEd Maste 	case 1: return "Allowed";
17392c23cb7cSEd Maste 	default:
17402c23cb7cSEd Maste 		snprintf(s_mp, sizeof(s_mp), "Unknown (%ju)",
17412c23cb7cSEd Maste 		    (uintmax_t) mp);
17422c23cb7cSEd Maste 		return (s_mp);
17432c23cb7cSEd Maste 	}
17442c23cb7cSEd Maste }
17452c23cb7cSEd Maste 
17462c23cb7cSEd Maste static const char *
17472c23cb7cSEd Maste aeabi_div(uint64_t du)
17482c23cb7cSEd Maste {
17492c23cb7cSEd Maste 	static char s_du[32];
17502c23cb7cSEd Maste 
17512c23cb7cSEd Maste 	switch (du) {
17522c23cb7cSEd Maste 	case 0: return "Yes (V7-R/V7-M)";
17532c23cb7cSEd Maste 	case 1: return "No";
17542c23cb7cSEd Maste 	case 2: return "Yes (V7-A)";
17552c23cb7cSEd Maste 	default:
17562c23cb7cSEd Maste 		snprintf(s_du, sizeof(s_du), "Unknown (%ju)",
17572c23cb7cSEd Maste 		    (uintmax_t) du);
17582c23cb7cSEd Maste 		return (s_du);
17592c23cb7cSEd Maste 	}
17602c23cb7cSEd Maste }
17612c23cb7cSEd Maste 
17622c23cb7cSEd Maste static const char *
17632c23cb7cSEd Maste aeabi_t2ee(uint64_t t2ee)
17642c23cb7cSEd Maste {
17652c23cb7cSEd Maste 	static char s_t2ee[32];
17662c23cb7cSEd Maste 
17672c23cb7cSEd Maste 	switch (t2ee) {
17682c23cb7cSEd Maste 	case 0: return "Not allowed";
17692c23cb7cSEd Maste 	case 1: return "Allowed";
17702c23cb7cSEd Maste 	default:
17712c23cb7cSEd Maste 		snprintf(s_t2ee, sizeof(s_t2ee), "Unknown(%ju)",
17722c23cb7cSEd Maste 		    (uintmax_t) t2ee);
17732c23cb7cSEd Maste 		return (s_t2ee);
17742c23cb7cSEd Maste 	}
17752c23cb7cSEd Maste 
17762c23cb7cSEd Maste }
17772c23cb7cSEd Maste 
17782c23cb7cSEd Maste static const char *
17792c23cb7cSEd Maste aeabi_hardfp(uint64_t hfp)
17802c23cb7cSEd Maste {
17812c23cb7cSEd Maste 	static char s_hfp[32];
17822c23cb7cSEd Maste 
17832c23cb7cSEd Maste 	switch (hfp) {
17842c23cb7cSEd Maste 	case 0: return "Tag_FP_arch";
17852c23cb7cSEd Maste 	case 1: return "only SP";
17862c23cb7cSEd Maste 	case 2: return "only DP";
17872c23cb7cSEd Maste 	case 3: return "both SP and DP";
17882c23cb7cSEd Maste 	default:
17892c23cb7cSEd Maste 		snprintf(s_hfp, sizeof(s_hfp), "Unknown (%ju)",
17902c23cb7cSEd Maste 		    (uintmax_t) hfp);
17912c23cb7cSEd Maste 		return (s_hfp);
17922c23cb7cSEd Maste 	}
17932c23cb7cSEd Maste }
17942c23cb7cSEd Maste 
17952c23cb7cSEd Maste static const char *
17962c23cb7cSEd Maste aeabi_vfp_args(uint64_t va)
17972c23cb7cSEd Maste {
17982c23cb7cSEd Maste 	static char s_va[32];
17992c23cb7cSEd Maste 
18002c23cb7cSEd Maste 	switch (va) {
18012c23cb7cSEd Maste 	case 0: return "AAPCS (base variant)";
18022c23cb7cSEd Maste 	case 1: return "AAPCS (VFP variant)";
18032c23cb7cSEd Maste 	case 2: return "toolchain-specific";
18042c23cb7cSEd Maste 	default:
18052c23cb7cSEd Maste 		snprintf(s_va, sizeof(s_va), "Unknown (%ju)", (uintmax_t) va);
18062c23cb7cSEd Maste 		return (s_va);
18072c23cb7cSEd Maste 	}
18082c23cb7cSEd Maste }
18092c23cb7cSEd Maste 
18102c23cb7cSEd Maste static const char *
18112c23cb7cSEd Maste aeabi_wmmx_args(uint64_t wa)
18122c23cb7cSEd Maste {
18132c23cb7cSEd Maste 	static char s_wa[32];
18142c23cb7cSEd Maste 
18152c23cb7cSEd Maste 	switch (wa) {
18162c23cb7cSEd Maste 	case 0: return "AAPCS (base variant)";
18172c23cb7cSEd Maste 	case 1: return "Intel WMMX";
18182c23cb7cSEd Maste 	case 2: return "toolchain-specific";
18192c23cb7cSEd Maste 	default:
18202c23cb7cSEd Maste 		snprintf(s_wa, sizeof(s_wa), "Unknown(%ju)", (uintmax_t) wa);
18212c23cb7cSEd Maste 		return (s_wa);
18222c23cb7cSEd Maste 	}
18232c23cb7cSEd Maste }
18242c23cb7cSEd Maste 
18252c23cb7cSEd Maste static const char *
18262c23cb7cSEd Maste aeabi_unaligned_access(uint64_t ua)
18272c23cb7cSEd Maste {
18282c23cb7cSEd Maste 	static char s_ua[32];
18292c23cb7cSEd Maste 
18302c23cb7cSEd Maste 	switch (ua) {
18312c23cb7cSEd Maste 	case 0: return "Not allowed";
18322c23cb7cSEd Maste 	case 1: return "Allowed";
18332c23cb7cSEd Maste 	default:
18342c23cb7cSEd Maste 		snprintf(s_ua, sizeof(s_ua), "Unknown(%ju)", (uintmax_t) ua);
18352c23cb7cSEd Maste 		return (s_ua);
18362c23cb7cSEd Maste 	}
18372c23cb7cSEd Maste }
18382c23cb7cSEd Maste 
18392c23cb7cSEd Maste static const char *
18402c23cb7cSEd Maste aeabi_fp_hpext(uint64_t fh)
18412c23cb7cSEd Maste {
18422c23cb7cSEd Maste 	static char s_fh[32];
18432c23cb7cSEd Maste 
18442c23cb7cSEd Maste 	switch (fh) {
18452c23cb7cSEd Maste 	case 0: return "Not allowed";
18462c23cb7cSEd Maste 	case 1: return "Allowed";
18472c23cb7cSEd Maste 	default:
18482c23cb7cSEd Maste 		snprintf(s_fh, sizeof(s_fh), "Unknown(%ju)", (uintmax_t) fh);
18492c23cb7cSEd Maste 		return (s_fh);
18502c23cb7cSEd Maste 	}
18512c23cb7cSEd Maste }
18522c23cb7cSEd Maste 
18532c23cb7cSEd Maste static const char *
18542c23cb7cSEd Maste aeabi_optm_goal(uint64_t og)
18552c23cb7cSEd Maste {
18562c23cb7cSEd Maste 	static char s_og[32];
18572c23cb7cSEd Maste 
18582c23cb7cSEd Maste 	switch (og) {
18592c23cb7cSEd Maste 	case 0: return "None";
18602c23cb7cSEd Maste 	case 1: return "Speed";
18612c23cb7cSEd Maste 	case 2: return "Speed aggressive";
18622c23cb7cSEd Maste 	case 3: return "Space";
18632c23cb7cSEd Maste 	case 4: return "Space aggressive";
18642c23cb7cSEd Maste 	case 5: return "Debugging";
18652c23cb7cSEd Maste 	case 6: return "Best Debugging";
18662c23cb7cSEd Maste 	default:
18672c23cb7cSEd Maste 		snprintf(s_og, sizeof(s_og), "Unknown(%ju)", (uintmax_t) og);
18682c23cb7cSEd Maste 		return (s_og);
18692c23cb7cSEd Maste 	}
18702c23cb7cSEd Maste }
18712c23cb7cSEd Maste 
18722c23cb7cSEd Maste static const char *
18732c23cb7cSEd Maste aeabi_fp_optm_goal(uint64_t fog)
18742c23cb7cSEd Maste {
18752c23cb7cSEd Maste 	static char s_fog[32];
18762c23cb7cSEd Maste 
18772c23cb7cSEd Maste 	switch (fog) {
18782c23cb7cSEd Maste 	case 0: return "None";
18792c23cb7cSEd Maste 	case 1: return "Speed";
18802c23cb7cSEd Maste 	case 2: return "Speed aggressive";
18812c23cb7cSEd Maste 	case 3: return "Space";
18822c23cb7cSEd Maste 	case 4: return "Space aggressive";
18832c23cb7cSEd Maste 	case 5: return "Accurary";
18842c23cb7cSEd Maste 	case 6: return "Best Accurary";
18852c23cb7cSEd Maste 	default:
18862c23cb7cSEd Maste 		snprintf(s_fog, sizeof(s_fog), "Unknown(%ju)",
18872c23cb7cSEd Maste 		    (uintmax_t) fog);
18882c23cb7cSEd Maste 		return (s_fog);
18892c23cb7cSEd Maste 	}
18902c23cb7cSEd Maste }
18912c23cb7cSEd Maste 
18922c23cb7cSEd Maste static const char *
18932c23cb7cSEd Maste aeabi_virtual(uint64_t vt)
18942c23cb7cSEd Maste {
18952c23cb7cSEd Maste 	static char s_virtual[64];
18962c23cb7cSEd Maste 
18972c23cb7cSEd Maste 	switch (vt) {
18982c23cb7cSEd Maste 	case 0: return "No";
18992c23cb7cSEd Maste 	case 1: return "TrustZone";
19002c23cb7cSEd Maste 	case 2: return "Virtualization extension";
19012c23cb7cSEd Maste 	case 3: return "TrustZone and virtualization extension";
19022c23cb7cSEd Maste 	default:
19032c23cb7cSEd Maste 		snprintf(s_virtual, sizeof(s_virtual), "Unknown(%ju)",
19042c23cb7cSEd Maste 		    (uintmax_t) vt);
19052c23cb7cSEd Maste 		return (s_virtual);
19062c23cb7cSEd Maste 	}
19072c23cb7cSEd Maste }
19082c23cb7cSEd Maste 
19092c23cb7cSEd Maste static struct {
19102c23cb7cSEd Maste 	uint64_t tag;
19112c23cb7cSEd Maste 	const char *s_tag;
19122c23cb7cSEd Maste 	const char *(*get_desc)(uint64_t val);
19132c23cb7cSEd Maste } aeabi_tags[] = {
19142c23cb7cSEd Maste 	{4, "Tag_CPU_raw_name", NULL},
19152c23cb7cSEd Maste 	{5, "Tag_CPU_name", NULL},
19162c23cb7cSEd Maste 	{6, "Tag_CPU_arch", aeabi_cpu_arch},
19172c23cb7cSEd Maste 	{7, "Tag_CPU_arch_profile", aeabi_cpu_arch_profile},
19182c23cb7cSEd Maste 	{8, "Tag_ARM_ISA_use", aeabi_arm_isa},
19192c23cb7cSEd Maste 	{9, "Tag_THUMB_ISA_use", aeabi_thumb_isa},
19202c23cb7cSEd Maste 	{10, "Tag_FP_arch", aeabi_fp_arch},
19212c23cb7cSEd Maste 	{11, "Tag_WMMX_arch", aeabi_wmmx_arch},
19222c23cb7cSEd Maste 	{12, "Tag_Advanced_SIMD_arch", aeabi_adv_simd_arch},
19232c23cb7cSEd Maste 	{13, "Tag_PCS_config", aeabi_pcs_config},
19242c23cb7cSEd Maste 	{14, "Tag_ABI_PCS_R9_use", aeabi_pcs_r9},
19252c23cb7cSEd Maste 	{15, "Tag_ABI_PCS_RW_data", aeabi_pcs_rw},
19262c23cb7cSEd Maste 	{16, "Tag_ABI_PCS_RO_data", aeabi_pcs_ro},
19272c23cb7cSEd Maste 	{17, "Tag_ABI_PCS_GOT_use", aeabi_pcs_got},
19282c23cb7cSEd Maste 	{18, "Tag_ABI_PCS_wchar_t", aeabi_pcs_wchar_t},
19292c23cb7cSEd Maste 	{19, "Tag_ABI_FP_rounding", aeabi_fp_rounding},
19302c23cb7cSEd Maste 	{20, "Tag_ABI_FP_denormal", aeabi_fp_denormal},
19312c23cb7cSEd Maste 	{21, "Tag_ABI_FP_exceptions", aeabi_fp_exceptions},
19322c23cb7cSEd Maste 	{22, "Tag_ABI_FP_user_exceptions", aeabi_fp_user_exceptions},
19332c23cb7cSEd Maste 	{23, "Tag_ABI_FP_number_model", aeabi_fp_number_model},
19342c23cb7cSEd Maste 	{24, "Tag_ABI_align_needed", aeabi_align_needed},
19352c23cb7cSEd Maste 	{25, "Tag_ABI_align_preserved", aeabi_align_preserved},
19362c23cb7cSEd Maste 	{26, "Tag_ABI_enum_size", aeabi_enum_size},
19372c23cb7cSEd Maste 	{27, "Tag_ABI_HardFP_use", aeabi_hardfp},
19382c23cb7cSEd Maste 	{28, "Tag_ABI_VFP_args", aeabi_vfp_args},
19392c23cb7cSEd Maste 	{29, "Tag_ABI_WMMX_args", aeabi_wmmx_args},
19402c23cb7cSEd Maste 	{30, "Tag_ABI_optimization_goals", aeabi_optm_goal},
19412c23cb7cSEd Maste 	{31, "Tag_ABI_FP_optimization_goals", aeabi_fp_optm_goal},
19422c23cb7cSEd Maste 	{32, "Tag_compatibility", NULL},
19432c23cb7cSEd Maste 	{34, "Tag_CPU_unaligned_access", aeabi_unaligned_access},
19442c23cb7cSEd Maste 	{36, "Tag_FP_HP_extension", aeabi_fp_hpext},
19452c23cb7cSEd Maste 	{38, "Tag_ABI_FP_16bit_format", aeabi_fp_16bit_format},
19462c23cb7cSEd Maste 	{42, "Tag_MPextension_use", aeabi_mpext},
19472c23cb7cSEd Maste 	{44, "Tag_DIV_use", aeabi_div},
19482c23cb7cSEd Maste 	{64, "Tag_nodefaults", NULL},
19492c23cb7cSEd Maste 	{65, "Tag_also_compatible_with", NULL},
19502c23cb7cSEd Maste 	{66, "Tag_T2EE_use", aeabi_t2ee},
19512c23cb7cSEd Maste 	{67, "Tag_conformance", NULL},
19522c23cb7cSEd Maste 	{68, "Tag_Virtualization_use", aeabi_virtual},
19532c23cb7cSEd Maste 	{70, "Tag_MPextension_use", aeabi_mpext},
19542c23cb7cSEd Maste };
19552c23cb7cSEd Maste 
19562c23cb7cSEd Maste static const char *
19572c23cb7cSEd Maste mips_abi_fp(uint64_t fp)
19582c23cb7cSEd Maste {
19592c23cb7cSEd Maste 	static char s_mips_abi_fp[64];
19602c23cb7cSEd Maste 
19612c23cb7cSEd Maste 	switch (fp) {
19622c23cb7cSEd Maste 	case 0: return "N/A";
19632c23cb7cSEd Maste 	case 1: return "Hard float (double precision)";
19642c23cb7cSEd Maste 	case 2: return "Hard float (single precision)";
19652c23cb7cSEd Maste 	case 3: return "Soft float";
19662c23cb7cSEd Maste 	case 4: return "64-bit float (-mips32r2 -mfp64)";
19672c23cb7cSEd Maste 	default:
19682c23cb7cSEd Maste 		snprintf(s_mips_abi_fp, sizeof(s_mips_abi_fp), "Unknown(%ju)",
19692c23cb7cSEd Maste 		    (uintmax_t) fp);
19702c23cb7cSEd Maste 		return (s_mips_abi_fp);
19712c23cb7cSEd Maste 	}
19722c23cb7cSEd Maste }
19732c23cb7cSEd Maste 
19742c23cb7cSEd Maste static const char *
19752c23cb7cSEd Maste ppc_abi_fp(uint64_t fp)
19762c23cb7cSEd Maste {
19772c23cb7cSEd Maste 	static char s_ppc_abi_fp[64];
19782c23cb7cSEd Maste 
19792c23cb7cSEd Maste 	switch (fp) {
19802c23cb7cSEd Maste 	case 0: return "N/A";
19812c23cb7cSEd Maste 	case 1: return "Hard float (double precision)";
19822c23cb7cSEd Maste 	case 2: return "Soft float";
19832c23cb7cSEd Maste 	case 3: return "Hard float (single precision)";
19842c23cb7cSEd Maste 	default:
19852c23cb7cSEd Maste 		snprintf(s_ppc_abi_fp, sizeof(s_ppc_abi_fp), "Unknown(%ju)",
19862c23cb7cSEd Maste 		    (uintmax_t) fp);
19872c23cb7cSEd Maste 		return (s_ppc_abi_fp);
19882c23cb7cSEd Maste 	}
19892c23cb7cSEd Maste }
19902c23cb7cSEd Maste 
19912c23cb7cSEd Maste static const char *
19922c23cb7cSEd Maste ppc_abi_vector(uint64_t vec)
19932c23cb7cSEd Maste {
19942c23cb7cSEd Maste 	static char s_vec[64];
19952c23cb7cSEd Maste 
19962c23cb7cSEd Maste 	switch (vec) {
19972c23cb7cSEd Maste 	case 0: return "N/A";
19982c23cb7cSEd Maste 	case 1: return "Generic purpose registers";
19992c23cb7cSEd Maste 	case 2: return "AltiVec registers";
20002c23cb7cSEd Maste 	case 3: return "SPE registers";
20012c23cb7cSEd Maste 	default:
20022c23cb7cSEd Maste 		snprintf(s_vec, sizeof(s_vec), "Unknown(%ju)", (uintmax_t) vec);
20032c23cb7cSEd Maste 		return (s_vec);
20042c23cb7cSEd Maste 	}
20052c23cb7cSEd Maste }
20062c23cb7cSEd Maste 
2007cf781b2eSEd Maste static const char *
2008cf781b2eSEd Maste dwarf_reg(unsigned int mach, unsigned int reg)
2009cf781b2eSEd Maste {
2010cf781b2eSEd Maste 
2011cf781b2eSEd Maste 	switch (mach) {
2012cf781b2eSEd Maste 	case EM_386:
20133ef90571SEd Maste 	case EM_IAMCU:
2014cf781b2eSEd Maste 		switch (reg) {
2015cf781b2eSEd Maste 		case 0: return "eax";
2016cf781b2eSEd Maste 		case 1: return "ecx";
2017cf781b2eSEd Maste 		case 2: return "edx";
2018cf781b2eSEd Maste 		case 3: return "ebx";
2019cf781b2eSEd Maste 		case 4: return "esp";
2020cf781b2eSEd Maste 		case 5: return "ebp";
2021cf781b2eSEd Maste 		case 6: return "esi";
2022cf781b2eSEd Maste 		case 7: return "edi";
2023cf781b2eSEd Maste 		case 8: return "eip";
2024cf781b2eSEd Maste 		case 9: return "eflags";
2025cf781b2eSEd Maste 		case 11: return "st0";
2026cf781b2eSEd Maste 		case 12: return "st1";
2027cf781b2eSEd Maste 		case 13: return "st2";
2028cf781b2eSEd Maste 		case 14: return "st3";
2029cf781b2eSEd Maste 		case 15: return "st4";
2030cf781b2eSEd Maste 		case 16: return "st5";
2031cf781b2eSEd Maste 		case 17: return "st6";
2032cf781b2eSEd Maste 		case 18: return "st7";
2033cf781b2eSEd Maste 		case 21: return "xmm0";
2034cf781b2eSEd Maste 		case 22: return "xmm1";
2035cf781b2eSEd Maste 		case 23: return "xmm2";
2036cf781b2eSEd Maste 		case 24: return "xmm3";
2037cf781b2eSEd Maste 		case 25: return "xmm4";
2038cf781b2eSEd Maste 		case 26: return "xmm5";
2039cf781b2eSEd Maste 		case 27: return "xmm6";
2040cf781b2eSEd Maste 		case 28: return "xmm7";
2041cf781b2eSEd Maste 		case 29: return "mm0";
2042cf781b2eSEd Maste 		case 30: return "mm1";
2043cf781b2eSEd Maste 		case 31: return "mm2";
2044cf781b2eSEd Maste 		case 32: return "mm3";
2045cf781b2eSEd Maste 		case 33: return "mm4";
2046cf781b2eSEd Maste 		case 34: return "mm5";
2047cf781b2eSEd Maste 		case 35: return "mm6";
2048cf781b2eSEd Maste 		case 36: return "mm7";
2049cf781b2eSEd Maste 		case 37: return "fcw";
2050cf781b2eSEd Maste 		case 38: return "fsw";
2051cf781b2eSEd Maste 		case 39: return "mxcsr";
2052cf781b2eSEd Maste 		case 40: return "es";
2053cf781b2eSEd Maste 		case 41: return "cs";
2054cf781b2eSEd Maste 		case 42: return "ss";
2055cf781b2eSEd Maste 		case 43: return "ds";
2056cf781b2eSEd Maste 		case 44: return "fs";
2057cf781b2eSEd Maste 		case 45: return "gs";
2058cf781b2eSEd Maste 		case 48: return "tr";
2059cf781b2eSEd Maste 		case 49: return "ldtr";
2060cf781b2eSEd Maste 		default: return (NULL);
2061cf781b2eSEd Maste 		}
2062cf781b2eSEd Maste 	case EM_X86_64:
2063cf781b2eSEd Maste 		switch (reg) {
2064cf781b2eSEd Maste 		case 0: return "rax";
2065cf781b2eSEd Maste 		case 1: return "rdx";
2066cf781b2eSEd Maste 		case 2: return "rcx";
2067cf781b2eSEd Maste 		case 3: return "rbx";
2068cf781b2eSEd Maste 		case 4: return "rsi";
2069cf781b2eSEd Maste 		case 5: return "rdi";
2070cf781b2eSEd Maste 		case 6: return "rbp";
2071cf781b2eSEd Maste 		case 7: return "rsp";
2072cf781b2eSEd Maste 		case 16: return "rip";
2073cf781b2eSEd Maste 		case 17: return "xmm0";
2074cf781b2eSEd Maste 		case 18: return "xmm1";
2075cf781b2eSEd Maste 		case 19: return "xmm2";
2076cf781b2eSEd Maste 		case 20: return "xmm3";
2077cf781b2eSEd Maste 		case 21: return "xmm4";
2078cf781b2eSEd Maste 		case 22: return "xmm5";
2079cf781b2eSEd Maste 		case 23: return "xmm6";
2080cf781b2eSEd Maste 		case 24: return "xmm7";
2081cf781b2eSEd Maste 		case 25: return "xmm8";
2082cf781b2eSEd Maste 		case 26: return "xmm9";
2083cf781b2eSEd Maste 		case 27: return "xmm10";
2084cf781b2eSEd Maste 		case 28: return "xmm11";
2085cf781b2eSEd Maste 		case 29: return "xmm12";
2086cf781b2eSEd Maste 		case 30: return "xmm13";
2087cf781b2eSEd Maste 		case 31: return "xmm14";
2088cf781b2eSEd Maste 		case 32: return "xmm15";
2089cf781b2eSEd Maste 		case 33: return "st0";
2090cf781b2eSEd Maste 		case 34: return "st1";
2091cf781b2eSEd Maste 		case 35: return "st2";
2092cf781b2eSEd Maste 		case 36: return "st3";
2093cf781b2eSEd Maste 		case 37: return "st4";
2094cf781b2eSEd Maste 		case 38: return "st5";
2095cf781b2eSEd Maste 		case 39: return "st6";
2096cf781b2eSEd Maste 		case 40: return "st7";
2097cf781b2eSEd Maste 		case 41: return "mm0";
2098cf781b2eSEd Maste 		case 42: return "mm1";
2099cf781b2eSEd Maste 		case 43: return "mm2";
2100cf781b2eSEd Maste 		case 44: return "mm3";
2101cf781b2eSEd Maste 		case 45: return "mm4";
2102cf781b2eSEd Maste 		case 46: return "mm5";
2103cf781b2eSEd Maste 		case 47: return "mm6";
2104cf781b2eSEd Maste 		case 48: return "mm7";
2105cf781b2eSEd Maste 		case 49: return "rflags";
2106cf781b2eSEd Maste 		case 50: return "es";
2107cf781b2eSEd Maste 		case 51: return "cs";
2108cf781b2eSEd Maste 		case 52: return "ss";
2109cf781b2eSEd Maste 		case 53: return "ds";
2110cf781b2eSEd Maste 		case 54: return "fs";
2111cf781b2eSEd Maste 		case 55: return "gs";
2112cf781b2eSEd Maste 		case 58: return "fs.base";
2113cf781b2eSEd Maste 		case 59: return "gs.base";
2114cf781b2eSEd Maste 		case 62: return "tr";
2115cf781b2eSEd Maste 		case 63: return "ldtr";
2116cf781b2eSEd Maste 		case 64: return "mxcsr";
2117cf781b2eSEd Maste 		case 65: return "fcw";
2118cf781b2eSEd Maste 		case 66: return "fsw";
2119cf781b2eSEd Maste 		default: return (NULL);
2120cf781b2eSEd Maste 		}
2121cf781b2eSEd Maste 	default:
2122cf781b2eSEd Maste 		return (NULL);
2123cf781b2eSEd Maste 	}
2124cf781b2eSEd Maste }
2125cf781b2eSEd Maste 
21262c23cb7cSEd Maste static void
21272c23cb7cSEd Maste dump_ehdr(struct readelf *re)
21282c23cb7cSEd Maste {
2129714935beSConrad Meyer 	size_t		 phnum, shnum, shstrndx;
21302c23cb7cSEd Maste 	int		 i;
21312c23cb7cSEd Maste 
21322c23cb7cSEd Maste 	printf("ELF Header:\n");
21332c23cb7cSEd Maste 
21342c23cb7cSEd Maste 	/* e_ident[]. */
21352c23cb7cSEd Maste 	printf("  Magic:   ");
21362c23cb7cSEd Maste 	for (i = 0; i < EI_NIDENT; i++)
21372c23cb7cSEd Maste 		printf("%.2x ", re->ehdr.e_ident[i]);
21382c23cb7cSEd Maste 	putchar('\n');
21392c23cb7cSEd Maste 
21402c23cb7cSEd Maste 	/* EI_CLASS. */
21412c23cb7cSEd Maste 	printf("%-37s%s\n", "  Class:", elf_class(re->ehdr.e_ident[EI_CLASS]));
21422c23cb7cSEd Maste 
21432c23cb7cSEd Maste 	/* EI_DATA. */
21442c23cb7cSEd Maste 	printf("%-37s%s\n", "  Data:", elf_endian(re->ehdr.e_ident[EI_DATA]));
21452c23cb7cSEd Maste 
21462c23cb7cSEd Maste 	/* EI_VERSION. */
21472c23cb7cSEd Maste 	printf("%-37s%d %s\n", "  Version:", re->ehdr.e_ident[EI_VERSION],
21482c23cb7cSEd Maste 	    elf_ver(re->ehdr.e_ident[EI_VERSION]));
21492c23cb7cSEd Maste 
21502c23cb7cSEd Maste 	/* EI_OSABI. */
21512c23cb7cSEd Maste 	printf("%-37s%s\n", "  OS/ABI:", elf_osabi(re->ehdr.e_ident[EI_OSABI]));
21522c23cb7cSEd Maste 
21532c23cb7cSEd Maste 	/* EI_ABIVERSION. */
21542c23cb7cSEd Maste 	printf("%-37s%d\n", "  ABI Version:", re->ehdr.e_ident[EI_ABIVERSION]);
21552c23cb7cSEd Maste 
21562c23cb7cSEd Maste 	/* e_type. */
21572c23cb7cSEd Maste 	printf("%-37s%s\n", "  Type:", elf_type(re->ehdr.e_type));
21582c23cb7cSEd Maste 
21592c23cb7cSEd Maste 	/* e_machine. */
21602c23cb7cSEd Maste 	printf("%-37s%s\n", "  Machine:", elf_machine(re->ehdr.e_machine));
21612c23cb7cSEd Maste 
21622c23cb7cSEd Maste 	/* e_version. */
21632c23cb7cSEd Maste 	printf("%-37s%#x\n", "  Version:", re->ehdr.e_version);
21642c23cb7cSEd Maste 
21652c23cb7cSEd Maste 	/* e_entry. */
21662c23cb7cSEd Maste 	printf("%-37s%#jx\n", "  Entry point address:",
21672c23cb7cSEd Maste 	    (uintmax_t)re->ehdr.e_entry);
21682c23cb7cSEd Maste 
21692c23cb7cSEd Maste 	/* e_phoff. */
21702c23cb7cSEd Maste 	printf("%-37s%ju (bytes into file)\n", "  Start of program headers:",
21712c23cb7cSEd Maste 	    (uintmax_t)re->ehdr.e_phoff);
21722c23cb7cSEd Maste 
21732c23cb7cSEd Maste 	/* e_shoff. */
21742c23cb7cSEd Maste 	printf("%-37s%ju (bytes into file)\n", "  Start of section headers:",
21752c23cb7cSEd Maste 	    (uintmax_t)re->ehdr.e_shoff);
21762c23cb7cSEd Maste 
21772c23cb7cSEd Maste 	/* e_flags. */
21782c23cb7cSEd Maste 	printf("%-37s%#x", "  Flags:", re->ehdr.e_flags);
21792c23cb7cSEd Maste 	dump_eflags(re, re->ehdr.e_flags);
21802c23cb7cSEd Maste 	putchar('\n');
21812c23cb7cSEd Maste 
21822c23cb7cSEd Maste 	/* e_ehsize. */
21832c23cb7cSEd Maste 	printf("%-37s%u (bytes)\n", "  Size of this header:",
21842c23cb7cSEd Maste 	    re->ehdr.e_ehsize);
21852c23cb7cSEd Maste 
21862c23cb7cSEd Maste 	/* e_phentsize. */
21872c23cb7cSEd Maste 	printf("%-37s%u (bytes)\n", "  Size of program headers:",
21882c23cb7cSEd Maste 	    re->ehdr.e_phentsize);
21892c23cb7cSEd Maste 
21902c23cb7cSEd Maste 	/* e_phnum. */
2191714935beSConrad Meyer 	printf("%-37s%u", "  Number of program headers:", re->ehdr.e_phnum);
2192714935beSConrad Meyer 	if (re->ehdr.e_phnum == PN_XNUM) {
2193714935beSConrad Meyer 		/* Extended program header numbering is in use. */
2194714935beSConrad Meyer 		if (elf_getphnum(re->elf, &phnum))
2195714935beSConrad Meyer 			printf(" (%zu)", phnum);
2196714935beSConrad Meyer 	}
2197714935beSConrad Meyer 	putchar('\n');
21982c23cb7cSEd Maste 
21992c23cb7cSEd Maste 	/* e_shentsize. */
22002c23cb7cSEd Maste 	printf("%-37s%u (bytes)\n", "  Size of section headers:",
22012c23cb7cSEd Maste 	    re->ehdr.e_shentsize);
22022c23cb7cSEd Maste 
22032c23cb7cSEd Maste 	/* e_shnum. */
22042c23cb7cSEd Maste 	printf("%-37s%u", "  Number of section headers:", re->ehdr.e_shnum);
22052c23cb7cSEd Maste 	if (re->ehdr.e_shnum == SHN_UNDEF) {
22062c23cb7cSEd Maste 		/* Extended section numbering is in use. */
22072c23cb7cSEd Maste 		if (elf_getshnum(re->elf, &shnum))
22082c23cb7cSEd Maste 			printf(" (%ju)", (uintmax_t)shnum);
22092c23cb7cSEd Maste 	}
22102c23cb7cSEd Maste 	putchar('\n');
22112c23cb7cSEd Maste 
22122c23cb7cSEd Maste 	/* e_shstrndx. */
22132c23cb7cSEd Maste 	printf("%-37s%u", "  Section header string table index:",
22142c23cb7cSEd Maste 	    re->ehdr.e_shstrndx);
22152c23cb7cSEd Maste 	if (re->ehdr.e_shstrndx == SHN_XINDEX) {
22162c23cb7cSEd Maste 		/* Extended section numbering is in use. */
22172c23cb7cSEd Maste 		if (elf_getshstrndx(re->elf, &shstrndx))
22182c23cb7cSEd Maste 			printf(" (%ju)", (uintmax_t)shstrndx);
22192c23cb7cSEd Maste 	}
22202c23cb7cSEd Maste 	putchar('\n');
22212c23cb7cSEd Maste }
22222c23cb7cSEd Maste 
22232c23cb7cSEd Maste static void
22242c23cb7cSEd Maste dump_eflags(struct readelf *re, uint64_t e_flags)
22252c23cb7cSEd Maste {
22262c23cb7cSEd Maste 	struct eflags_desc *edesc;
22272c23cb7cSEd Maste 	int arm_eabi;
22282c23cb7cSEd Maste 
22292c23cb7cSEd Maste 	edesc = NULL;
22302c23cb7cSEd Maste 	switch (re->ehdr.e_machine) {
22312c23cb7cSEd Maste 	case EM_ARM:
22322c23cb7cSEd Maste 		arm_eabi = (e_flags & EF_ARM_EABIMASK) >> 24;
22332c23cb7cSEd Maste 		if (arm_eabi == 0)
22342c23cb7cSEd Maste 			printf(", GNU EABI");
22352c23cb7cSEd Maste 		else if (arm_eabi <= 5)
22362c23cb7cSEd Maste 			printf(", Version%d EABI", arm_eabi);
22372c23cb7cSEd Maste 		edesc = arm_eflags_desc;
22382c23cb7cSEd Maste 		break;
22392c23cb7cSEd Maste 	case EM_MIPS:
22402c23cb7cSEd Maste 	case EM_MIPS_RS3_LE:
22412c23cb7cSEd Maste 		switch ((e_flags & EF_MIPS_ARCH) >> 28) {
22422c23cb7cSEd Maste 		case 0:	printf(", mips1"); break;
22432c23cb7cSEd Maste 		case 1: printf(", mips2"); break;
22442c23cb7cSEd Maste 		case 2: printf(", mips3"); break;
22452c23cb7cSEd Maste 		case 3: printf(", mips4"); break;
22462c23cb7cSEd Maste 		case 4: printf(", mips5"); break;
22472c23cb7cSEd Maste 		case 5: printf(", mips32"); break;
22482c23cb7cSEd Maste 		case 6: printf(", mips64"); break;
22492c23cb7cSEd Maste 		case 7: printf(", mips32r2"); break;
22502c23cb7cSEd Maste 		case 8: printf(", mips64r2"); break;
22512c23cb7cSEd Maste 		default: break;
22522c23cb7cSEd Maste 		}
22532c23cb7cSEd Maste 		switch ((e_flags & 0x00FF0000) >> 16) {
22542c23cb7cSEd Maste 		case 0x81: printf(", 3900"); break;
22552c23cb7cSEd Maste 		case 0x82: printf(", 4010"); break;
22562c23cb7cSEd Maste 		case 0x83: printf(", 4100"); break;
22572c23cb7cSEd Maste 		case 0x85: printf(", 4650"); break;
22582c23cb7cSEd Maste 		case 0x87: printf(", 4120"); break;
22592c23cb7cSEd Maste 		case 0x88: printf(", 4111"); break;
22602c23cb7cSEd Maste 		case 0x8a: printf(", sb1"); break;
22612c23cb7cSEd Maste 		case 0x8b: printf(", octeon"); break;
22622c23cb7cSEd Maste 		case 0x8c: printf(", xlr"); break;
22632c23cb7cSEd Maste 		case 0x91: printf(", 5400"); break;
22642c23cb7cSEd Maste 		case 0x98: printf(", 5500"); break;
22652c23cb7cSEd Maste 		case 0x99: printf(", 9000"); break;
22662c23cb7cSEd Maste 		case 0xa0: printf(", loongson-2e"); break;
22672c23cb7cSEd Maste 		case 0xa1: printf(", loongson-2f"); break;
22682c23cb7cSEd Maste 		default: break;
22692c23cb7cSEd Maste 		}
22702c23cb7cSEd Maste 		switch ((e_flags & 0x0000F000) >> 12) {
22712c23cb7cSEd Maste 		case 1: printf(", o32"); break;
22722c23cb7cSEd Maste 		case 2: printf(", o64"); break;
22732c23cb7cSEd Maste 		case 3: printf(", eabi32"); break;
22742c23cb7cSEd Maste 		case 4: printf(", eabi64"); break;
22752c23cb7cSEd Maste 		default: break;
22762c23cb7cSEd Maste 		}
22772c23cb7cSEd Maste 		edesc = mips_eflags_desc;
22782c23cb7cSEd Maste 		break;
22792c23cb7cSEd Maste 	case EM_PPC:
22802c23cb7cSEd Maste 	case EM_PPC64:
22812c23cb7cSEd Maste 		edesc = powerpc_eflags_desc;
22822c23cb7cSEd Maste 		break;
22832c23cb7cSEd Maste 	case EM_SPARC:
22842c23cb7cSEd Maste 	case EM_SPARC32PLUS:
22852c23cb7cSEd Maste 	case EM_SPARCV9:
22862c23cb7cSEd Maste 		switch ((e_flags & EF_SPARCV9_MM)) {
22872c23cb7cSEd Maste 		case EF_SPARCV9_TSO: printf(", tso"); break;
22882c23cb7cSEd Maste 		case EF_SPARCV9_PSO: printf(", pso"); break;
22892c23cb7cSEd Maste 		case EF_SPARCV9_MM: printf(", rmo"); break;
22902c23cb7cSEd Maste 		default: break;
22912c23cb7cSEd Maste 		}
22922c23cb7cSEd Maste 		edesc = sparc_eflags_desc;
22932c23cb7cSEd Maste 		break;
22942c23cb7cSEd Maste 	default:
22952c23cb7cSEd Maste 		break;
22962c23cb7cSEd Maste 	}
22972c23cb7cSEd Maste 
22982c23cb7cSEd Maste 	if (edesc != NULL) {
22992c23cb7cSEd Maste 		while (edesc->desc != NULL) {
23002c23cb7cSEd Maste 			if (e_flags & edesc->flag)
23012c23cb7cSEd Maste 				printf(", %s", edesc->desc);
23022c23cb7cSEd Maste 			edesc++;
23032c23cb7cSEd Maste 		}
23042c23cb7cSEd Maste 	}
23052c23cb7cSEd Maste }
23062c23cb7cSEd Maste 
23072c23cb7cSEd Maste static void
23082c23cb7cSEd Maste dump_phdr(struct readelf *re)
23092c23cb7cSEd Maste {
23102c23cb7cSEd Maste 	const char	*rawfile;
23112c23cb7cSEd Maste 	GElf_Phdr	 phdr;
2312b00fe64fSEd Maste 	size_t		 phnum, size;
23132c23cb7cSEd Maste 	int		 i, j;
23142c23cb7cSEd Maste 
23152c23cb7cSEd Maste #define	PH_HDR	"Type", "Offset", "VirtAddr", "PhysAddr", "FileSiz",	\
23162c23cb7cSEd Maste 		"MemSiz", "Flg", "Align"
231720136ffcSEd Maste #define	PH_CT	phdr_type(re->ehdr.e_machine, phdr.p_type),		\
231820136ffcSEd Maste 		(uintmax_t)phdr.p_offset, (uintmax_t)phdr.p_vaddr,	\
231920136ffcSEd Maste 		(uintmax_t)phdr.p_paddr, (uintmax_t)phdr.p_filesz,	\
232020136ffcSEd Maste 		(uintmax_t)phdr.p_memsz,				\
23212c23cb7cSEd Maste 		phdr.p_flags & PF_R ? 'R' : ' ',			\
23222c23cb7cSEd Maste 		phdr.p_flags & PF_W ? 'W' : ' ',			\
23232c23cb7cSEd Maste 		phdr.p_flags & PF_X ? 'E' : ' ',			\
23242c23cb7cSEd Maste 		(uintmax_t)phdr.p_align
23252c23cb7cSEd Maste 
23262c23cb7cSEd Maste 	if (elf_getphnum(re->elf, &phnum) == 0) {
23272c23cb7cSEd Maste 		warnx("elf_getphnum failed: %s", elf_errmsg(-1));
23282c23cb7cSEd Maste 		return;
23292c23cb7cSEd Maste 	}
23302c23cb7cSEd Maste 	if (phnum == 0) {
23312c23cb7cSEd Maste 		printf("\nThere are no program headers in this file.\n");
23322c23cb7cSEd Maste 		return;
23332c23cb7cSEd Maste 	}
23342c23cb7cSEd Maste 
23352c23cb7cSEd Maste 	printf("\nElf file type is %s", elf_type(re->ehdr.e_type));
23362c23cb7cSEd Maste 	printf("\nEntry point 0x%jx\n", (uintmax_t)re->ehdr.e_entry);
23372c23cb7cSEd Maste 	printf("There are %ju program headers, starting at offset %ju\n",
23382c23cb7cSEd Maste 	    (uintmax_t)phnum, (uintmax_t)re->ehdr.e_phoff);
23392c23cb7cSEd Maste 
23402c23cb7cSEd Maste 	/* Dump program headers. */
23412c23cb7cSEd Maste 	printf("\nProgram Headers:\n");
23422c23cb7cSEd Maste 	if (re->ec == ELFCLASS32)
23432c23cb7cSEd Maste 		printf("  %-15s%-9s%-11s%-11s%-8s%-8s%-4s%s\n", PH_HDR);
23442c23cb7cSEd Maste 	else if (re->options & RE_WW)
23452c23cb7cSEd Maste 		printf("  %-15s%-9s%-19s%-19s%-9s%-9s%-4s%s\n", PH_HDR);
23462c23cb7cSEd Maste 	else
23472c23cb7cSEd Maste 		printf("  %-15s%-19s%-19s%s\n                 %-19s%-20s"
23482c23cb7cSEd Maste 		    "%-7s%s\n", PH_HDR);
23492c23cb7cSEd Maste 	for (i = 0; (size_t) i < phnum; i++) {
23502c23cb7cSEd Maste 		if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
23512c23cb7cSEd Maste 			warnx("gelf_getphdr failed: %s", elf_errmsg(-1));
23522c23cb7cSEd Maste 			continue;
23532c23cb7cSEd Maste 		}
23542c23cb7cSEd Maste 		/* TODO: Add arch-specific segment type dump. */
23552c23cb7cSEd Maste 		if (re->ec == ELFCLASS32)
23562c23cb7cSEd Maste 			printf("  %-14.14s 0x%6.6jx 0x%8.8jx 0x%8.8jx "
23572c23cb7cSEd Maste 			    "0x%5.5jx 0x%5.5jx %c%c%c %#jx\n", PH_CT);
23582c23cb7cSEd Maste 		else if (re->options & RE_WW)
23592c23cb7cSEd Maste 			printf("  %-14.14s 0x%6.6jx 0x%16.16jx 0x%16.16jx "
23602c23cb7cSEd Maste 			    "0x%6.6jx 0x%6.6jx %c%c%c %#jx\n", PH_CT);
23612c23cb7cSEd Maste 		else
23622c23cb7cSEd Maste 			printf("  %-14.14s 0x%16.16jx 0x%16.16jx 0x%16.16jx\n"
23632c23cb7cSEd Maste 			    "                 0x%16.16jx 0x%16.16jx  %c%c%c"
23642c23cb7cSEd Maste 			    "    %#jx\n", PH_CT);
23652c23cb7cSEd Maste 		if (phdr.p_type == PT_INTERP) {
2366b00fe64fSEd Maste 			if ((rawfile = elf_rawfile(re->elf, &size)) == NULL) {
23672c23cb7cSEd Maste 				warnx("elf_rawfile failed: %s", elf_errmsg(-1));
23682c23cb7cSEd Maste 				continue;
23692c23cb7cSEd Maste 			}
2370b00fe64fSEd Maste 			if (phdr.p_offset >= size) {
2371b00fe64fSEd Maste 				warnx("invalid program header offset");
2372b00fe64fSEd Maste 				continue;
2373b00fe64fSEd Maste 			}
23742c23cb7cSEd Maste 			printf("      [Requesting program interpreter: %s]\n",
23752c23cb7cSEd Maste 				rawfile + phdr.p_offset);
23762c23cb7cSEd Maste 		}
23772c23cb7cSEd Maste 	}
23782c23cb7cSEd Maste 
23792c23cb7cSEd Maste 	/* Dump section to segment mapping. */
23802c23cb7cSEd Maste 	if (re->shnum == 0)
23812c23cb7cSEd Maste 		return;
23822c23cb7cSEd Maste 	printf("\n Section to Segment mapping:\n");
23832c23cb7cSEd Maste 	printf("  Segment Sections...\n");
23842c23cb7cSEd Maste 	for (i = 0; (size_t)i < phnum; i++) {
23852c23cb7cSEd Maste 		if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
23862c23cb7cSEd Maste 			warnx("gelf_getphdr failed: %s", elf_errmsg(-1));
23872c23cb7cSEd Maste 			continue;
23882c23cb7cSEd Maste 		}
23892c23cb7cSEd Maste 		printf("   %2.2d     ", i);
23902c23cb7cSEd Maste 		/* skip NULL section. */
239148d41ef0SPhil Shafer 		for (j = 1; (size_t)j < re->shnum; j++) {
239248d41ef0SPhil Shafer 			if (re->sl[j].off < phdr.p_offset)
239348d41ef0SPhil Shafer 				continue;
239448d41ef0SPhil Shafer 			if (re->sl[j].off + re->sl[j].sz >
239548d41ef0SPhil Shafer 			    phdr.p_offset + phdr.p_filesz &&
239648d41ef0SPhil Shafer 			    re->sl[j].type != SHT_NOBITS)
239748d41ef0SPhil Shafer 				continue;
239848d41ef0SPhil Shafer 			if (re->sl[j].addr < phdr.p_vaddr ||
239948d41ef0SPhil Shafer 			    re->sl[j].addr + re->sl[j].sz >
240095fd7f26SEd Maste 			    phdr.p_vaddr + phdr.p_memsz)
240148d41ef0SPhil Shafer 				continue;
240248d41ef0SPhil Shafer 			if (phdr.p_type == PT_TLS &&
240348d41ef0SPhil Shafer 			    (re->sl[j].flags & SHF_TLS) == 0)
240448d41ef0SPhil Shafer 				continue;
24052c23cb7cSEd Maste 			printf("%s ", re->sl[j].name);
240648d41ef0SPhil Shafer 		}
24072c23cb7cSEd Maste 		printf("\n");
24082c23cb7cSEd Maste 	}
24092c23cb7cSEd Maste #undef	PH_HDR
24102c23cb7cSEd Maste #undef	PH_CT
24112c23cb7cSEd Maste }
24122c23cb7cSEd Maste 
24132c23cb7cSEd Maste static char *
24142c23cb7cSEd Maste section_flags(struct readelf *re, struct section *s)
24152c23cb7cSEd Maste {
24162c23cb7cSEd Maste #define BUF_SZ 256
24172c23cb7cSEd Maste 	static char	buf[BUF_SZ];
24182c23cb7cSEd Maste 	int		i, p, nb;
24192c23cb7cSEd Maste 
24202c23cb7cSEd Maste 	p = 0;
24212c23cb7cSEd Maste 	nb = re->ec == ELFCLASS32 ? 8 : 16;
24222c23cb7cSEd Maste 	if (re->options & RE_T) {
24232c23cb7cSEd Maste 		snprintf(buf, BUF_SZ, "[%*.*jx]: ", nb, nb,
24242c23cb7cSEd Maste 		    (uintmax_t)s->flags);
24252c23cb7cSEd Maste 		p += nb + 4;
24262c23cb7cSEd Maste 	}
24272c23cb7cSEd Maste 	for (i = 0; section_flag[i].ln != NULL; i++) {
24282c23cb7cSEd Maste 		if ((s->flags & section_flag[i].value) == 0)
24292c23cb7cSEd Maste 			continue;
24302c23cb7cSEd Maste 		if (re->options & RE_T) {
24312c23cb7cSEd Maste 			snprintf(&buf[p], BUF_SZ - p, "%s, ",
24322c23cb7cSEd Maste 			    section_flag[i].ln);
24332c23cb7cSEd Maste 			p += strlen(section_flag[i].ln) + 2;
24342c23cb7cSEd Maste 		} else
24352c23cb7cSEd Maste 			buf[p++] = section_flag[i].sn;
24362c23cb7cSEd Maste 	}
24372c23cb7cSEd Maste 	if (re->options & RE_T && p > nb + 4)
24382c23cb7cSEd Maste 		p -= 2;
24392c23cb7cSEd Maste 	buf[p] = '\0';
24402c23cb7cSEd Maste 
24412c23cb7cSEd Maste 	return (buf);
24422c23cb7cSEd Maste }
24432c23cb7cSEd Maste 
24442c23cb7cSEd Maste static void
24452c23cb7cSEd Maste dump_shdr(struct readelf *re)
24462c23cb7cSEd Maste {
24472c23cb7cSEd Maste 	struct section	*s;
24482c23cb7cSEd Maste 	int		 i;
24492c23cb7cSEd Maste 
24502c23cb7cSEd Maste #define	S_HDR	"[Nr] Name", "Type", "Addr", "Off", "Size", "ES",	\
24512c23cb7cSEd Maste 		"Flg", "Lk", "Inf", "Al"
24522c23cb7cSEd Maste #define	S_HDRL	"[Nr] Name", "Type", "Address", "Offset", "Size",	\
24532c23cb7cSEd Maste 		"EntSize", "Flags", "Link", "Info", "Align"
24542c23cb7cSEd Maste #define	ST_HDR	"[Nr] Name", "Type", "Addr", "Off", "Size", "ES",	\
24552c23cb7cSEd Maste 		"Lk", "Inf", "Al", "Flags"
24562c23cb7cSEd Maste #define	ST_HDRL	"[Nr] Name", "Type", "Address", "Offset", "Link",	\
24572c23cb7cSEd Maste 		"Size", "EntSize", "Info", "Align", "Flags"
24582c23cb7cSEd Maste #define	S_CT	i, s->name, section_type(re->ehdr.e_machine, s->type),	\
24592c23cb7cSEd Maste 		(uintmax_t)s->addr, (uintmax_t)s->off, (uintmax_t)s->sz,\
24602c23cb7cSEd Maste 		(uintmax_t)s->entsize, section_flags(re, s),		\
24612c23cb7cSEd Maste 		s->link, s->info, (uintmax_t)s->align
24622c23cb7cSEd Maste #define	ST_CT	i, s->name, section_type(re->ehdr.e_machine, s->type),  \
24632c23cb7cSEd Maste 		(uintmax_t)s->addr, (uintmax_t)s->off, (uintmax_t)s->sz,\
24642c23cb7cSEd Maste 		(uintmax_t)s->entsize, s->link, s->info,		\
24652c23cb7cSEd Maste 		(uintmax_t)s->align, section_flags(re, s)
24662c23cb7cSEd Maste #define	ST_CTL	i, s->name, section_type(re->ehdr.e_machine, s->type),  \
24672c23cb7cSEd Maste 		(uintmax_t)s->addr, (uintmax_t)s->off, s->link,		\
24682c23cb7cSEd Maste 		(uintmax_t)s->sz, (uintmax_t)s->entsize, s->info,	\
24692c23cb7cSEd Maste 		(uintmax_t)s->align, section_flags(re, s)
24702c23cb7cSEd Maste 
24712c23cb7cSEd Maste 	if (re->shnum == 0) {
24722c23cb7cSEd Maste 		printf("\nThere are no sections in this file.\n");
24732c23cb7cSEd Maste 		return;
24742c23cb7cSEd Maste 	}
24752c23cb7cSEd Maste 	printf("There are %ju section headers, starting at offset 0x%jx:\n",
24762c23cb7cSEd Maste 	    (uintmax_t)re->shnum, (uintmax_t)re->ehdr.e_shoff);
24772c23cb7cSEd Maste 	printf("\nSection Headers:\n");
24782c23cb7cSEd Maste 	if (re->ec == ELFCLASS32) {
24792c23cb7cSEd Maste 		if (re->options & RE_T)
24802c23cb7cSEd Maste 			printf("  %s\n       %-16s%-9s%-7s%-7s%-5s%-3s%-4s%s\n"
24812c23cb7cSEd Maste 			    "%12s\n", ST_HDR);
24822c23cb7cSEd Maste 		else
24832c23cb7cSEd Maste 			printf("  %-23s%-16s%-9s%-7s%-7s%-3s%-4s%-3s%-4s%s\n",
24842c23cb7cSEd Maste 			    S_HDR);
24852c23cb7cSEd Maste 	} else if (re->options & RE_WW) {
24862c23cb7cSEd Maste 		if (re->options & RE_T)
24872c23cb7cSEd Maste 			printf("  %s\n       %-16s%-17s%-7s%-7s%-5s%-3s%-4s%s\n"
24882c23cb7cSEd Maste 			    "%12s\n", ST_HDR);
24892c23cb7cSEd Maste 		else
24902c23cb7cSEd Maste 			printf("  %-23s%-16s%-17s%-7s%-7s%-3s%-4s%-3s%-4s%s\n",
24912c23cb7cSEd Maste 			    S_HDR);
24922c23cb7cSEd Maste 	} else {
24932c23cb7cSEd Maste 		if (re->options & RE_T)
24942c23cb7cSEd Maste 			printf("  %s\n       %-18s%-17s%-18s%s\n       %-18s"
24952c23cb7cSEd Maste 			    "%-17s%-18s%s\n%12s\n", ST_HDRL);
24962c23cb7cSEd Maste 		else
24972c23cb7cSEd Maste 			printf("  %-23s%-17s%-18s%s\n       %-18s%-17s%-7s%"
24982c23cb7cSEd Maste 			    "-6s%-6s%s\n", S_HDRL);
24992c23cb7cSEd Maste 	}
25002c23cb7cSEd Maste 	for (i = 0; (size_t)i < re->shnum; i++) {
25012c23cb7cSEd Maste 		s = &re->sl[i];
25022c23cb7cSEd Maste 		if (re->ec == ELFCLASS32) {
25032c23cb7cSEd Maste 			if (re->options & RE_T)
25042c23cb7cSEd Maste 				printf("  [%2d] %s\n       %-15.15s %8.8jx"
25052c23cb7cSEd Maste 				    " %6.6jx %6.6jx %2.2jx  %2u %3u %2ju\n"
25062c23cb7cSEd Maste 				    "       %s\n", ST_CT);
25072c23cb7cSEd Maste 			else
25082c23cb7cSEd Maste 				printf("  [%2d] %-17.17s %-15.15s %8.8jx"
25092c23cb7cSEd Maste 				    " %6.6jx %6.6jx %2.2jx %3s %2u %3u %2ju\n",
25102c23cb7cSEd Maste 				    S_CT);
25112c23cb7cSEd Maste 		} else if (re->options & RE_WW) {
25122c23cb7cSEd Maste 			if (re->options & RE_T)
25132c23cb7cSEd Maste 				printf("  [%2d] %s\n       %-15.15s %16.16jx"
25142c23cb7cSEd Maste 				    " %6.6jx %6.6jx %2.2jx  %2u %3u %2ju\n"
25152c23cb7cSEd Maste 				    "       %s\n", ST_CT);
25162c23cb7cSEd Maste 			else
25172c23cb7cSEd Maste 				printf("  [%2d] %-17.17s %-15.15s %16.16jx"
25182c23cb7cSEd Maste 				    " %6.6jx %6.6jx %2.2jx %3s %2u %3u %2ju\n",
25192c23cb7cSEd Maste 				    S_CT);
25202c23cb7cSEd Maste 		} else {
25212c23cb7cSEd Maste 			if (re->options & RE_T)
25222c23cb7cSEd Maste 				printf("  [%2d] %s\n       %-15.15s  %16.16jx"
25232c23cb7cSEd Maste 				    "  %16.16jx  %u\n       %16.16jx %16.16jx"
25242c23cb7cSEd Maste 				    "  %-16u  %ju\n       %s\n", ST_CTL);
25252c23cb7cSEd Maste 			else
25262c23cb7cSEd Maste 				printf("  [%2d] %-17.17s %-15.15s  %16.16jx"
25272c23cb7cSEd Maste 				    "  %8.8jx\n       %16.16jx  %16.16jx "
25282c23cb7cSEd Maste 				    "%3s      %2u   %3u     %ju\n", S_CT);
25292c23cb7cSEd Maste 		}
25302c23cb7cSEd Maste 	}
25312c23cb7cSEd Maste 	if ((re->options & RE_T) == 0)
25322c23cb7cSEd Maste 		printf("Key to Flags:\n  W (write), A (alloc),"
25332c23cb7cSEd Maste 		    " X (execute), M (merge), S (strings)\n"
25342c23cb7cSEd Maste 		    "  I (info), L (link order), G (group), x (unknown)\n"
25352c23cb7cSEd Maste 		    "  O (extra OS processing required)"
25362c23cb7cSEd Maste 		    " o (OS specific), p (processor specific)\n");
25372c23cb7cSEd Maste 
25382c23cb7cSEd Maste #undef	S_HDR
25392c23cb7cSEd Maste #undef	S_HDRL
25402c23cb7cSEd Maste #undef	ST_HDR
25412c23cb7cSEd Maste #undef	ST_HDRL
25422c23cb7cSEd Maste #undef	S_CT
25432c23cb7cSEd Maste #undef	ST_CT
25442c23cb7cSEd Maste #undef	ST_CTL
25452c23cb7cSEd Maste }
25462c23cb7cSEd Maste 
254771edbbfdSEd Maste /*
254871edbbfdSEd Maste  * Return number of entries in the given section. We'd prefer ent_count be a
254971edbbfdSEd Maste  * size_t *, but libelf APIs already use int for section indices.
255071edbbfdSEd Maste  */
255171edbbfdSEd Maste static int
255271edbbfdSEd Maste get_ent_count(struct section *s, int *ent_count)
255371edbbfdSEd Maste {
255471edbbfdSEd Maste 	if (s->entsize == 0) {
255571edbbfdSEd Maste 		warnx("section %s has entry size 0", s->name);
255671edbbfdSEd Maste 		return (0);
255771edbbfdSEd Maste 	} else if (s->sz / s->entsize > INT_MAX) {
255871edbbfdSEd Maste 		warnx("section %s has invalid section count", s->name);
255971edbbfdSEd Maste 		return (0);
256071edbbfdSEd Maste 	}
256171edbbfdSEd Maste 	*ent_count = (int)(s->sz / s->entsize);
256271edbbfdSEd Maste 	return (1);
256371edbbfdSEd Maste }
256471edbbfdSEd Maste 
25652c23cb7cSEd Maste static void
25662c23cb7cSEd Maste dump_dynamic(struct readelf *re)
25672c23cb7cSEd Maste {
25682c23cb7cSEd Maste 	GElf_Dyn	 dyn;
25692c23cb7cSEd Maste 	Elf_Data	*d;
25702c23cb7cSEd Maste 	struct section	*s;
25712c23cb7cSEd Maste 	int		 elferr, i, is_dynamic, j, jmax, nentries;
25722c23cb7cSEd Maste 
25732c23cb7cSEd Maste 	is_dynamic = 0;
25742c23cb7cSEd Maste 
25752c23cb7cSEd Maste 	for (i = 0; (size_t)i < re->shnum; i++) {
25762c23cb7cSEd Maste 		s = &re->sl[i];
25772c23cb7cSEd Maste 		if (s->type != SHT_DYNAMIC)
25782c23cb7cSEd Maste 			continue;
25792c23cb7cSEd Maste 		(void) elf_errno();
25802c23cb7cSEd Maste 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
25812c23cb7cSEd Maste 			elferr = elf_errno();
25822c23cb7cSEd Maste 			if (elferr != 0)
25832c23cb7cSEd Maste 				warnx("elf_getdata failed: %s", elf_errmsg(-1));
25842c23cb7cSEd Maste 			continue;
25852c23cb7cSEd Maste 		}
25862c23cb7cSEd Maste 		if (d->d_size <= 0)
25872c23cb7cSEd Maste 			continue;
25882c23cb7cSEd Maste 
25892c23cb7cSEd Maste 		is_dynamic = 1;
25902c23cb7cSEd Maste 
25912c23cb7cSEd Maste 		/* Determine the actual number of table entries. */
25922c23cb7cSEd Maste 		nentries = 0;
259371edbbfdSEd Maste 		if (!get_ent_count(s, &jmax))
259471edbbfdSEd Maste 			continue;
25952c23cb7cSEd Maste 		for (j = 0; j < jmax; j++) {
25962c23cb7cSEd Maste 			if (gelf_getdyn(d, j, &dyn) != &dyn) {
25972c23cb7cSEd Maste 				warnx("gelf_getdyn failed: %s",
25982c23cb7cSEd Maste 				    elf_errmsg(-1));
25992c23cb7cSEd Maste 				continue;
26002c23cb7cSEd Maste 			}
26012c23cb7cSEd Maste 			nentries ++;
26022c23cb7cSEd Maste 			if (dyn.d_tag == DT_NULL)
26032c23cb7cSEd Maste 				break;
26042c23cb7cSEd Maste                 }
26052c23cb7cSEd Maste 
26062c23cb7cSEd Maste 		printf("\nDynamic section at offset 0x%jx", (uintmax_t)s->off);
26072c23cb7cSEd Maste 		printf(" contains %u entries:\n", nentries);
26082c23cb7cSEd Maste 
26092c23cb7cSEd Maste 		if (re->ec == ELFCLASS32)
26102c23cb7cSEd Maste 			printf("%5s%12s%28s\n", "Tag", "Type", "Name/Value");
26112c23cb7cSEd Maste 		else
26122c23cb7cSEd Maste 			printf("%5s%20s%28s\n", "Tag", "Type", "Name/Value");
26132c23cb7cSEd Maste 
26142c23cb7cSEd Maste 		for (j = 0; j < nentries; j++) {
26152c23cb7cSEd Maste 			if (gelf_getdyn(d, j, &dyn) != &dyn)
26162c23cb7cSEd Maste 				continue;
26172c23cb7cSEd Maste 			/* Dump dynamic entry type. */
26182c23cb7cSEd Maste 			if (re->ec == ELFCLASS32)
26192c23cb7cSEd Maste 				printf(" 0x%8.8jx", (uintmax_t)dyn.d_tag);
26202c23cb7cSEd Maste 			else
26212c23cb7cSEd Maste 				printf(" 0x%16.16jx", (uintmax_t)dyn.d_tag);
26222c23cb7cSEd Maste 			printf(" %-20s", dt_type(re->ehdr.e_machine,
26232c23cb7cSEd Maste 			    dyn.d_tag));
26242c23cb7cSEd Maste 			/* Dump dynamic entry value. */
26252c23cb7cSEd Maste 			dump_dyn_val(re, &dyn, s->link);
26262c23cb7cSEd Maste 		}
26272c23cb7cSEd Maste 	}
26282c23cb7cSEd Maste 
26292c23cb7cSEd Maste 	if (!is_dynamic)
26302c23cb7cSEd Maste 		printf("\nThere is no dynamic section in this file.\n");
26312c23cb7cSEd Maste }
26322c23cb7cSEd Maste 
26332c23cb7cSEd Maste static char *
26342c23cb7cSEd Maste timestamp(time_t ti)
26352c23cb7cSEd Maste {
26362c23cb7cSEd Maste 	static char ts[32];
26372c23cb7cSEd Maste 	struct tm *t;
26382c23cb7cSEd Maste 
26392c23cb7cSEd Maste 	t = gmtime(&ti);
26402c23cb7cSEd Maste 	snprintf(ts, sizeof(ts), "%04d-%02d-%02dT%02d:%02d:%02d",
26412c23cb7cSEd Maste 	    t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour,
26422c23cb7cSEd Maste 	    t->tm_min, t->tm_sec);
26432c23cb7cSEd Maste 
26442c23cb7cSEd Maste 	return (ts);
26452c23cb7cSEd Maste }
26462c23cb7cSEd Maste 
26472c23cb7cSEd Maste static const char *
26482c23cb7cSEd Maste dyn_str(struct readelf *re, uint32_t stab, uint64_t d_val)
26492c23cb7cSEd Maste {
26502c23cb7cSEd Maste 	const char *name;
26512c23cb7cSEd Maste 
26522c23cb7cSEd Maste 	if (stab == SHN_UNDEF)
26532c23cb7cSEd Maste 		name = "ERROR";
26542c23cb7cSEd Maste 	else if ((name = elf_strptr(re->elf, stab, d_val)) == NULL) {
26552c23cb7cSEd Maste 		(void) elf_errno(); /* clear error */
26562c23cb7cSEd Maste 		name = "ERROR";
26572c23cb7cSEd Maste 	}
26582c23cb7cSEd Maste 
26592c23cb7cSEd Maste 	return (name);
26602c23cb7cSEd Maste }
26612c23cb7cSEd Maste 
26622c23cb7cSEd Maste static void
266375826696SEd Maste dump_arch_dyn_val(struct readelf *re, GElf_Dyn *dyn)
26642c23cb7cSEd Maste {
26652c23cb7cSEd Maste 	switch (re->ehdr.e_machine) {
26662c23cb7cSEd Maste 	case EM_MIPS:
26672c23cb7cSEd Maste 	case EM_MIPS_RS3_LE:
26682c23cb7cSEd Maste 		switch (dyn->d_tag) {
26692c23cb7cSEd Maste 		case DT_MIPS_RLD_VERSION:
26702c23cb7cSEd Maste 		case DT_MIPS_LOCAL_GOTNO:
26712c23cb7cSEd Maste 		case DT_MIPS_CONFLICTNO:
26722c23cb7cSEd Maste 		case DT_MIPS_LIBLISTNO:
26732c23cb7cSEd Maste 		case DT_MIPS_SYMTABNO:
26742c23cb7cSEd Maste 		case DT_MIPS_UNREFEXTNO:
26752c23cb7cSEd Maste 		case DT_MIPS_GOTSYM:
26762c23cb7cSEd Maste 		case DT_MIPS_HIPAGENO:
26772c23cb7cSEd Maste 		case DT_MIPS_DELTA_CLASS_NO:
26782c23cb7cSEd Maste 		case DT_MIPS_DELTA_INSTANCE_NO:
26792c23cb7cSEd Maste 		case DT_MIPS_DELTA_RELOC_NO:
26802c23cb7cSEd Maste 		case DT_MIPS_DELTA_SYM_NO:
26812c23cb7cSEd Maste 		case DT_MIPS_DELTA_CLASSSYM_NO:
26822c23cb7cSEd Maste 		case DT_MIPS_LOCALPAGE_GOTIDX:
26832c23cb7cSEd Maste 		case DT_MIPS_LOCAL_GOTIDX:
26842c23cb7cSEd Maste 		case DT_MIPS_HIDDEN_GOTIDX:
26852c23cb7cSEd Maste 		case DT_MIPS_PROTECTED_GOTIDX:
26862c23cb7cSEd Maste 			printf(" %ju\n", (uintmax_t) dyn->d_un.d_val);
26872c23cb7cSEd Maste 			break;
26882c23cb7cSEd Maste 		case DT_MIPS_ICHECKSUM:
26892c23cb7cSEd Maste 		case DT_MIPS_FLAGS:
26902c23cb7cSEd Maste 		case DT_MIPS_BASE_ADDRESS:
26912c23cb7cSEd Maste 		case DT_MIPS_CONFLICT:
26922c23cb7cSEd Maste 		case DT_MIPS_LIBLIST:
26932c23cb7cSEd Maste 		case DT_MIPS_RLD_MAP:
26942c23cb7cSEd Maste 		case DT_MIPS_DELTA_CLASS:
26952c23cb7cSEd Maste 		case DT_MIPS_DELTA_INSTANCE:
26962c23cb7cSEd Maste 		case DT_MIPS_DELTA_RELOC:
26972c23cb7cSEd Maste 		case DT_MIPS_DELTA_SYM:
26982c23cb7cSEd Maste 		case DT_MIPS_DELTA_CLASSSYM:
26992c23cb7cSEd Maste 		case DT_MIPS_CXX_FLAGS:
27002c23cb7cSEd Maste 		case DT_MIPS_PIXIE_INIT:
27012c23cb7cSEd Maste 		case DT_MIPS_SYMBOL_LIB:
27022c23cb7cSEd Maste 		case DT_MIPS_OPTIONS:
27032c23cb7cSEd Maste 		case DT_MIPS_INTERFACE:
27042c23cb7cSEd Maste 		case DT_MIPS_DYNSTR_ALIGN:
27052c23cb7cSEd Maste 		case DT_MIPS_INTERFACE_SIZE:
27062c23cb7cSEd Maste 		case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
27072c23cb7cSEd Maste 		case DT_MIPS_COMPACT_SIZE:
27082c23cb7cSEd Maste 		case DT_MIPS_GP_VALUE:
27092c23cb7cSEd Maste 		case DT_MIPS_AUX_DYNAMIC:
27102c23cb7cSEd Maste 		case DT_MIPS_PLTGOT:
27112c23cb7cSEd Maste 		case DT_MIPS_RLD_OBJ_UPDATE:
27122c23cb7cSEd Maste 		case DT_MIPS_RWPLT:
27132c23cb7cSEd Maste 			printf(" 0x%jx\n", (uintmax_t) dyn->d_un.d_val);
27142c23cb7cSEd Maste 			break;
27152c23cb7cSEd Maste 		case DT_MIPS_IVERSION:
27162c23cb7cSEd Maste 		case DT_MIPS_PERF_SUFFIX:
27172c23cb7cSEd Maste 		case DT_MIPS_TIME_STAMP:
27182c23cb7cSEd Maste 			printf(" %s\n", timestamp(dyn->d_un.d_val));
27192c23cb7cSEd Maste 			break;
27209fb35c8dSJohn Baldwin 		default:
27219fb35c8dSJohn Baldwin 			printf("\n");
27229fb35c8dSJohn Baldwin 			break;
27232c23cb7cSEd Maste 		}
27242c23cb7cSEd Maste 		break;
27252c23cb7cSEd Maste 	default:
27262c23cb7cSEd Maste 		printf("\n");
27272c23cb7cSEd Maste 		break;
27282c23cb7cSEd Maste 	}
27292c23cb7cSEd Maste }
27302c23cb7cSEd Maste 
27312c23cb7cSEd Maste static void
27326b2779a0SEd Maste dump_flags(struct flag_desc *desc, uint64_t val)
27336b2779a0SEd Maste {
27346b2779a0SEd Maste 	struct flag_desc *fd;
27356b2779a0SEd Maste 
27366b2779a0SEd Maste 	for (fd = desc; fd->flag != 0; fd++) {
27376b2779a0SEd Maste 		if (val & fd->flag) {
27386b2779a0SEd Maste 			val &= ~fd->flag;
27396b2779a0SEd Maste 			printf(" %s", fd->desc);
27406b2779a0SEd Maste 		}
27416b2779a0SEd Maste 	}
27426b2779a0SEd Maste 	if (val != 0)
27436b2779a0SEd Maste 		printf(" unknown (0x%jx)", (uintmax_t)val);
27446b2779a0SEd Maste }
27456b2779a0SEd Maste 
27466b2779a0SEd Maste static struct flag_desc dt_flags[] = {
27476b2779a0SEd Maste 	{ DF_ORIGIN,		"ORIGIN" },
27486b2779a0SEd Maste 	{ DF_SYMBOLIC,		"SYMBOLIC" },
27496b2779a0SEd Maste 	{ DF_TEXTREL,		"TEXTREL" },
27506b2779a0SEd Maste 	{ DF_BIND_NOW,		"BIND_NOW" },
27516b2779a0SEd Maste 	{ DF_STATIC_TLS,	"STATIC_TLS" },
27526b2779a0SEd Maste 	{ 0, NULL }
27536b2779a0SEd Maste };
27546b2779a0SEd Maste 
27556b2779a0SEd Maste static struct flag_desc dt_flags_1[] = {
27566b2779a0SEd Maste 	{ DF_1_BIND_NOW,	"NOW" },
27576b2779a0SEd Maste 	{ DF_1_GLOBAL,		"GLOBAL" },
27586b2779a0SEd Maste 	{ 0x4,			"GROUP" },
27596b2779a0SEd Maste 	{ DF_1_NODELETE,	"NODELETE" },
27606b2779a0SEd Maste 	{ DF_1_LOADFLTR,	"LOADFLTR" },
27616b2779a0SEd Maste 	{ 0x20,			"INITFIRST" },
27626b2779a0SEd Maste 	{ DF_1_NOOPEN,		"NOOPEN" },
27636b2779a0SEd Maste 	{ DF_1_ORIGIN,		"ORIGIN" },
27646b2779a0SEd Maste 	{ 0x100,		"DIRECT" },
27656b2779a0SEd Maste 	{ DF_1_INTERPOSE,	"INTERPOSE" },
27666b2779a0SEd Maste 	{ DF_1_NODEFLIB,	"NODEFLIB" },
27676b2779a0SEd Maste 	{ 0x1000,		"NODUMP" },
27686b2779a0SEd Maste 	{ 0x2000,		"CONFALT" },
27696b2779a0SEd Maste 	{ 0x4000,		"ENDFILTEE" },
27706b2779a0SEd Maste 	{ 0x8000,		"DISPRELDNE" },
27716b2779a0SEd Maste 	{ 0x10000,		"DISPRELPND" },
27726b2779a0SEd Maste 	{ 0x20000,		"NODIRECT" },
27736b2779a0SEd Maste 	{ 0x40000,		"IGNMULDEF" },
27746b2779a0SEd Maste 	{ 0x80000,		"NOKSYMS" },
27756b2779a0SEd Maste 	{ 0x100000,		"NOHDR" },
27766b2779a0SEd Maste 	{ 0x200000,		"EDITED" },
27776b2779a0SEd Maste 	{ 0x400000,		"NORELOC" },
27786b2779a0SEd Maste 	{ 0x800000,		"SYMINTPOSE" },
27796b2779a0SEd Maste 	{ 0x1000000,		"GLOBAUDIT" },
27806b2779a0SEd Maste 	{ 0, NULL }
27816b2779a0SEd Maste };
27826b2779a0SEd Maste 
27836b2779a0SEd Maste static void
27842c23cb7cSEd Maste dump_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab)
27852c23cb7cSEd Maste {
27862c23cb7cSEd Maste 	const char *name;
27872c23cb7cSEd Maste 
278875826696SEd Maste 	if (dyn->d_tag >= DT_LOPROC && dyn->d_tag <= DT_HIPROC &&
278975826696SEd Maste 	    dyn->d_tag != DT_AUXILIARY && dyn->d_tag != DT_FILTER) {
279075826696SEd Maste 		dump_arch_dyn_val(re, dyn);
27912c23cb7cSEd Maste 		return;
27922c23cb7cSEd Maste 	}
27932c23cb7cSEd Maste 
27942c23cb7cSEd Maste 	/* These entry values are index into the string table. */
27952c23cb7cSEd Maste 	name = NULL;
279675826696SEd Maste 	if (dyn->d_tag == DT_AUXILIARY || dyn->d_tag == DT_FILTER ||
279775826696SEd Maste 	    dyn->d_tag == DT_NEEDED || dyn->d_tag == DT_SONAME ||
27982c23cb7cSEd Maste 	    dyn->d_tag == DT_RPATH || dyn->d_tag == DT_RUNPATH)
27992c23cb7cSEd Maste 		name = dyn_str(re, stab, dyn->d_un.d_val);
28002c23cb7cSEd Maste 
28012c23cb7cSEd Maste 	switch(dyn->d_tag) {
28022c23cb7cSEd Maste 	case DT_NULL:
28032c23cb7cSEd Maste 	case DT_PLTGOT:
28042c23cb7cSEd Maste 	case DT_HASH:
28052c23cb7cSEd Maste 	case DT_STRTAB:
28062c23cb7cSEd Maste 	case DT_SYMTAB:
28072c23cb7cSEd Maste 	case DT_RELA:
28082c23cb7cSEd Maste 	case DT_INIT:
28092c23cb7cSEd Maste 	case DT_SYMBOLIC:
28102c23cb7cSEd Maste 	case DT_REL:
28112c23cb7cSEd Maste 	case DT_DEBUG:
28122c23cb7cSEd Maste 	case DT_TEXTREL:
28132c23cb7cSEd Maste 	case DT_JMPREL:
28142c23cb7cSEd Maste 	case DT_FINI:
28152c23cb7cSEd Maste 	case DT_VERDEF:
28162c23cb7cSEd Maste 	case DT_VERNEED:
28172c23cb7cSEd Maste 	case DT_VERSYM:
28182c23cb7cSEd Maste 	case DT_GNU_HASH:
28192c23cb7cSEd Maste 	case DT_GNU_LIBLIST:
28202c23cb7cSEd Maste 	case DT_GNU_CONFLICT:
28212c23cb7cSEd Maste 		printf(" 0x%jx\n", (uintmax_t) dyn->d_un.d_val);
28222c23cb7cSEd Maste 		break;
28232c23cb7cSEd Maste 	case DT_PLTRELSZ:
28242c23cb7cSEd Maste 	case DT_RELASZ:
28252c23cb7cSEd Maste 	case DT_RELAENT:
28262c23cb7cSEd Maste 	case DT_STRSZ:
28272c23cb7cSEd Maste 	case DT_SYMENT:
28282c23cb7cSEd Maste 	case DT_RELSZ:
28292c23cb7cSEd Maste 	case DT_RELENT:
28307003cfb3SEd Maste 	case DT_PREINIT_ARRAYSZ:
28312c23cb7cSEd Maste 	case DT_INIT_ARRAYSZ:
28322c23cb7cSEd Maste 	case DT_FINI_ARRAYSZ:
28332c23cb7cSEd Maste 	case DT_GNU_CONFLICTSZ:
28342c23cb7cSEd Maste 	case DT_GNU_LIBLISTSZ:
28352c23cb7cSEd Maste 		printf(" %ju (bytes)\n", (uintmax_t) dyn->d_un.d_val);
28362c23cb7cSEd Maste 		break;
28372c23cb7cSEd Maste  	case DT_RELACOUNT:
28382c23cb7cSEd Maste 	case DT_RELCOUNT:
28392c23cb7cSEd Maste 	case DT_VERDEFNUM:
28402c23cb7cSEd Maste 	case DT_VERNEEDNUM:
28412c23cb7cSEd Maste 		printf(" %ju\n", (uintmax_t) dyn->d_un.d_val);
28422c23cb7cSEd Maste 		break;
284375826696SEd Maste 	case DT_AUXILIARY:
284475826696SEd Maste 		printf(" Auxiliary library: [%s]\n", name);
284575826696SEd Maste 		break;
284675826696SEd Maste 	case DT_FILTER:
284775826696SEd Maste 		printf(" Filter library: [%s]\n", name);
284875826696SEd Maste 		break;
28492c23cb7cSEd Maste 	case DT_NEEDED:
28502c23cb7cSEd Maste 		printf(" Shared library: [%s]\n", name);
28512c23cb7cSEd Maste 		break;
28522c23cb7cSEd Maste 	case DT_SONAME:
28532c23cb7cSEd Maste 		printf(" Library soname: [%s]\n", name);
28542c23cb7cSEd Maste 		break;
28552c23cb7cSEd Maste 	case DT_RPATH:
28562c23cb7cSEd Maste 		printf(" Library rpath: [%s]\n", name);
28572c23cb7cSEd Maste 		break;
28582c23cb7cSEd Maste 	case DT_RUNPATH:
28592c23cb7cSEd Maste 		printf(" Library runpath: [%s]\n", name);
28602c23cb7cSEd Maste 		break;
28612c23cb7cSEd Maste 	case DT_PLTREL:
28622c23cb7cSEd Maste 		printf(" %s\n", dt_type(re->ehdr.e_machine, dyn->d_un.d_val));
28632c23cb7cSEd Maste 		break;
28642c23cb7cSEd Maste 	case DT_GNU_PRELINKED:
28652c23cb7cSEd Maste 		printf(" %s\n", timestamp(dyn->d_un.d_val));
28662c23cb7cSEd Maste 		break;
286787a8583bSEd Maste 	case DT_FLAGS:
28686b2779a0SEd Maste 		dump_flags(dt_flags, dyn->d_un.d_val);
286987a8583bSEd Maste 		break;
287087a8583bSEd Maste 	case DT_FLAGS_1:
28716b2779a0SEd Maste 		dump_flags(dt_flags_1, dyn->d_un.d_val);
287287a8583bSEd Maste 		break;
28732c23cb7cSEd Maste 	default:
28742c23cb7cSEd Maste 		printf("\n");
28752c23cb7cSEd Maste 	}
28762c23cb7cSEd Maste }
28772c23cb7cSEd Maste 
28782c23cb7cSEd Maste static void
28792c23cb7cSEd Maste dump_rel(struct readelf *re, struct section *s, Elf_Data *d)
28802c23cb7cSEd Maste {
28812c23cb7cSEd Maste 	GElf_Rel r;
28822c23cb7cSEd Maste 	const char *symname;
28832c23cb7cSEd Maste 	uint64_t symval;
28842c23cb7cSEd Maste 	int i, len;
2885b6d812d2SEd Maste 	uint32_t type;
2886b6d812d2SEd Maste 	uint8_t type2, type3;
28872c23cb7cSEd Maste 
2888656f49f8SEd Maste 	if (s->link >= re->shnum)
2889656f49f8SEd Maste 		return;
2890656f49f8SEd Maste 
28912c23cb7cSEd Maste #define	REL_HDR "r_offset", "r_info", "r_type", "st_value", "st_name"
28922c23cb7cSEd Maste #define	REL_CT32 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
2893b6b6f9ccSEd Maste 		elftc_reloc_type_str(re->ehdr.e_machine,	    \
2894b6b6f9ccSEd Maste 		ELF32_R_TYPE(r.r_info)), (uintmax_t)symval, symname
28952c23cb7cSEd Maste #define	REL_CT64 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
2896b6d812d2SEd Maste 		elftc_reloc_type_str(re->ehdr.e_machine, type),	    \
2897b6d812d2SEd Maste 		(uintmax_t)symval, symname
28982c23cb7cSEd Maste 
28992c23cb7cSEd Maste 	printf("\nRelocation section (%s):\n", s->name);
29002c23cb7cSEd Maste 	if (re->ec == ELFCLASS32)
29012c23cb7cSEd Maste 		printf("%-8s %-8s %-19s %-8s %s\n", REL_HDR);
29022c23cb7cSEd Maste 	else {
29032c23cb7cSEd Maste 		if (re->options & RE_WW)
29042c23cb7cSEd Maste 			printf("%-16s %-16s %-24s %-16s %s\n", REL_HDR);
29052c23cb7cSEd Maste 		else
29062c23cb7cSEd Maste 			printf("%-12s %-12s %-19s %-16s %s\n", REL_HDR);
29072c23cb7cSEd Maste 	}
290871edbbfdSEd Maste 	assert(d->d_size == s->sz);
290971edbbfdSEd Maste 	if (!get_ent_count(s, &len))
291071edbbfdSEd Maste 		return;
29112c23cb7cSEd Maste 	for (i = 0; i < len; i++) {
29122c23cb7cSEd Maste 		if (gelf_getrel(d, i, &r) != &r) {
29132c23cb7cSEd Maste 			warnx("gelf_getrel failed: %s", elf_errmsg(-1));
29142c23cb7cSEd Maste 			continue;
29152c23cb7cSEd Maste 		}
29162c23cb7cSEd Maste 		symname = get_symbol_name(re, s->link, GELF_R_SYM(r.r_info));
29172c23cb7cSEd Maste 		symval = get_symbol_value(re, s->link, GELF_R_SYM(r.r_info));
29182c23cb7cSEd Maste 		if (re->ec == ELFCLASS32) {
29192c23cb7cSEd Maste 			r.r_info = ELF32_R_INFO(ELF64_R_SYM(r.r_info),
29202c23cb7cSEd Maste 			    ELF64_R_TYPE(r.r_info));
29212c23cb7cSEd Maste 			printf("%8.8jx %8.8jx %-19.19s %8.8jx %s\n", REL_CT32);
29222c23cb7cSEd Maste 		} else {
2923b6d812d2SEd Maste 			type = ELF64_R_TYPE(r.r_info);
2924b6d812d2SEd Maste 			if (re->ehdr.e_machine == EM_MIPS) {
2925b6d812d2SEd Maste 				type2 = (type >> 8) & 0xFF;
2926b6d812d2SEd Maste 				type3 = (type >> 16) & 0xFF;
2927b6d812d2SEd Maste 				type = type & 0xFF;
2928d0bd2dadSEd Maste 			} else {
2929d0bd2dadSEd Maste 				type2 = type3 = 0;
2930b6d812d2SEd Maste 			}
29312c23cb7cSEd Maste 			if (re->options & RE_WW)
29322c23cb7cSEd Maste 				printf("%16.16jx %16.16jx %-24.24s"
29332c23cb7cSEd Maste 				    " %16.16jx %s\n", REL_CT64);
29342c23cb7cSEd Maste 			else
29352c23cb7cSEd Maste 				printf("%12.12jx %12.12jx %-19.19s"
29362c23cb7cSEd Maste 				    " %16.16jx %s\n", REL_CT64);
2937b6d812d2SEd Maste 			if (re->ehdr.e_machine == EM_MIPS) {
2938b6d812d2SEd Maste 				if (re->options & RE_WW) {
2939b6d812d2SEd Maste 					printf("%32s: %s\n", "Type2",
2940b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
2941b6d812d2SEd Maste 					    type2));
2942b6d812d2SEd Maste 					printf("%32s: %s\n", "Type3",
2943b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
2944b6d812d2SEd Maste 					    type3));
2945b6d812d2SEd Maste 				} else {
2946b6d812d2SEd Maste 					printf("%24s: %s\n", "Type2",
2947b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
2948b6d812d2SEd Maste 					    type2));
2949b6d812d2SEd Maste 					printf("%24s: %s\n", "Type3",
2950b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
2951b6d812d2SEd Maste 					    type3));
2952b6d812d2SEd Maste 				}
2953b6d812d2SEd Maste 			}
29542c23cb7cSEd Maste 		}
29552c23cb7cSEd Maste 	}
29562c23cb7cSEd Maste 
29572c23cb7cSEd Maste #undef	REL_HDR
29582c23cb7cSEd Maste #undef	REL_CT
29592c23cb7cSEd Maste }
29602c23cb7cSEd Maste 
29612c23cb7cSEd Maste static void
29622c23cb7cSEd Maste dump_rela(struct readelf *re, struct section *s, Elf_Data *d)
29632c23cb7cSEd Maste {
29642c23cb7cSEd Maste 	GElf_Rela r;
29652c23cb7cSEd Maste 	const char *symname;
29662c23cb7cSEd Maste 	uint64_t symval;
29672c23cb7cSEd Maste 	int i, len;
2968b6d812d2SEd Maste 	uint32_t type;
2969b6d812d2SEd Maste 	uint8_t type2, type3;
29702c23cb7cSEd Maste 
2971656f49f8SEd Maste 	if (s->link >= re->shnum)
2972656f49f8SEd Maste 		return;
2973656f49f8SEd Maste 
29742c23cb7cSEd Maste #define	RELA_HDR "r_offset", "r_info", "r_type", "st_value", \
29752c23cb7cSEd Maste 		"st_name + r_addend"
29762c23cb7cSEd Maste #define	RELA_CT32 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
2977b6b6f9ccSEd Maste 		elftc_reloc_type_str(re->ehdr.e_machine,	    \
2978b6b6f9ccSEd Maste 		ELF32_R_TYPE(r.r_info)), (uintmax_t)symval, symname
29792c23cb7cSEd Maste #define	RELA_CT64 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
2980b6d812d2SEd Maste 		elftc_reloc_type_str(re->ehdr.e_machine, type),	    \
2981b6d812d2SEd Maste 		(uintmax_t)symval, symname
29822c23cb7cSEd Maste 
29832c23cb7cSEd Maste 	printf("\nRelocation section with addend (%s):\n", s->name);
29842c23cb7cSEd Maste 	if (re->ec == ELFCLASS32)
29852c23cb7cSEd Maste 		printf("%-8s %-8s %-19s %-8s %s\n", RELA_HDR);
29862c23cb7cSEd Maste 	else {
29872c23cb7cSEd Maste 		if (re->options & RE_WW)
29882c23cb7cSEd Maste 			printf("%-16s %-16s %-24s %-16s %s\n", RELA_HDR);
29892c23cb7cSEd Maste 		else
29902c23cb7cSEd Maste 			printf("%-12s %-12s %-19s %-16s %s\n", RELA_HDR);
29912c23cb7cSEd Maste 	}
299271edbbfdSEd Maste 	assert(d->d_size == s->sz);
299371edbbfdSEd Maste 	if (!get_ent_count(s, &len))
299471edbbfdSEd Maste 		return;
29952c23cb7cSEd Maste 	for (i = 0; i < len; i++) {
29962c23cb7cSEd Maste 		if (gelf_getrela(d, i, &r) != &r) {
29972c23cb7cSEd Maste 			warnx("gelf_getrel failed: %s", elf_errmsg(-1));
29982c23cb7cSEd Maste 			continue;
29992c23cb7cSEd Maste 		}
30002c23cb7cSEd Maste 		symname = get_symbol_name(re, s->link, GELF_R_SYM(r.r_info));
30012c23cb7cSEd Maste 		symval = get_symbol_value(re, s->link, GELF_R_SYM(r.r_info));
30022c23cb7cSEd Maste 		if (re->ec == ELFCLASS32) {
30032c23cb7cSEd Maste 			r.r_info = ELF32_R_INFO(ELF64_R_SYM(r.r_info),
30042c23cb7cSEd Maste 			    ELF64_R_TYPE(r.r_info));
30052c23cb7cSEd Maste 			printf("%8.8jx %8.8jx %-19.19s %8.8jx %s", RELA_CT32);
30062c23cb7cSEd Maste 			printf(" + %x\n", (uint32_t) r.r_addend);
30072c23cb7cSEd Maste 		} else {
3008b6d812d2SEd Maste 			type = ELF64_R_TYPE(r.r_info);
3009b6d812d2SEd Maste 			if (re->ehdr.e_machine == EM_MIPS) {
3010b6d812d2SEd Maste 				type2 = (type >> 8) & 0xFF;
3011b6d812d2SEd Maste 				type3 = (type >> 16) & 0xFF;
3012b6d812d2SEd Maste 				type = type & 0xFF;
3013d0bd2dadSEd Maste 			} else {
3014d0bd2dadSEd Maste 				type2 = type3 = 0;
3015b6d812d2SEd Maste 			}
30162c23cb7cSEd Maste 			if (re->options & RE_WW)
30172c23cb7cSEd Maste 				printf("%16.16jx %16.16jx %-24.24s"
30182c23cb7cSEd Maste 				    " %16.16jx %s", RELA_CT64);
30192c23cb7cSEd Maste 			else
30202c23cb7cSEd Maste 				printf("%12.12jx %12.12jx %-19.19s"
30212c23cb7cSEd Maste 				    " %16.16jx %s", RELA_CT64);
30222c23cb7cSEd Maste 			printf(" + %jx\n", (uintmax_t) r.r_addend);
3023b6d812d2SEd Maste 			if (re->ehdr.e_machine == EM_MIPS) {
3024b6d812d2SEd Maste 				if (re->options & RE_WW) {
3025b6d812d2SEd Maste 					printf("%32s: %s\n", "Type2",
3026b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
3027b6d812d2SEd Maste 					    type2));
3028b6d812d2SEd Maste 					printf("%32s: %s\n", "Type3",
3029b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
3030b6d812d2SEd Maste 					    type3));
3031b6d812d2SEd Maste 				} else {
3032b6d812d2SEd Maste 					printf("%24s: %s\n", "Type2",
3033b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
3034b6d812d2SEd Maste 					    type2));
3035b6d812d2SEd Maste 					printf("%24s: %s\n", "Type3",
3036b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
3037b6d812d2SEd Maste 					    type3));
3038b6d812d2SEd Maste 				}
3039b6d812d2SEd Maste 			}
30402c23cb7cSEd Maste 		}
30412c23cb7cSEd Maste 	}
30422c23cb7cSEd Maste 
30432c23cb7cSEd Maste #undef	RELA_HDR
30442c23cb7cSEd Maste #undef	RELA_CT
30452c23cb7cSEd Maste }
30462c23cb7cSEd Maste 
30472c23cb7cSEd Maste static void
30482c23cb7cSEd Maste dump_reloc(struct readelf *re)
30492c23cb7cSEd Maste {
30502c23cb7cSEd Maste 	struct section *s;
30512c23cb7cSEd Maste 	Elf_Data *d;
30522c23cb7cSEd Maste 	int i, elferr;
30532c23cb7cSEd Maste 
30542c23cb7cSEd Maste 	for (i = 0; (size_t)i < re->shnum; i++) {
30552c23cb7cSEd Maste 		s = &re->sl[i];
30562c23cb7cSEd Maste 		if (s->type == SHT_REL || s->type == SHT_RELA) {
30572c23cb7cSEd Maste 			(void) elf_errno();
30582c23cb7cSEd Maste 			if ((d = elf_getdata(s->scn, NULL)) == NULL) {
30592c23cb7cSEd Maste 				elferr = elf_errno();
30602c23cb7cSEd Maste 				if (elferr != 0)
30612c23cb7cSEd Maste 					warnx("elf_getdata failed: %s",
30622c23cb7cSEd Maste 					    elf_errmsg(elferr));
30632c23cb7cSEd Maste 				continue;
30642c23cb7cSEd Maste 			}
30652c23cb7cSEd Maste 			if (s->type == SHT_REL)
30662c23cb7cSEd Maste 				dump_rel(re, s, d);
30672c23cb7cSEd Maste 			else
30682c23cb7cSEd Maste 				dump_rela(re, s, d);
30692c23cb7cSEd Maste 		}
30702c23cb7cSEd Maste 	}
30712c23cb7cSEd Maste }
30722c23cb7cSEd Maste 
30732c23cb7cSEd Maste static void
30742c23cb7cSEd Maste dump_symtab(struct readelf *re, int i)
30752c23cb7cSEd Maste {
30762c23cb7cSEd Maste 	struct section *s;
30772c23cb7cSEd Maste 	Elf_Data *d;
30782c23cb7cSEd Maste 	GElf_Sym sym;
30792c23cb7cSEd Maste 	const char *name;
3080656f49f8SEd Maste 	uint32_t stab;
3081656f49f8SEd Maste 	int elferr, j, len;
3082656f49f8SEd Maste 	uint16_t vs;
30832c23cb7cSEd Maste 
30842c23cb7cSEd Maste 	s = &re->sl[i];
3085656f49f8SEd Maste 	if (s->link >= re->shnum)
3086656f49f8SEd Maste 		return;
30872c23cb7cSEd Maste 	stab = s->link;
30882c23cb7cSEd Maste 	(void) elf_errno();
30892c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
30902c23cb7cSEd Maste 		elferr = elf_errno();
30912c23cb7cSEd Maste 		if (elferr != 0)
30922c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
30932c23cb7cSEd Maste 		return;
30942c23cb7cSEd Maste 	}
30952c23cb7cSEd Maste 	if (d->d_size <= 0)
30962c23cb7cSEd Maste 		return;
309771edbbfdSEd Maste 	if (!get_ent_count(s, &len))
309871edbbfdSEd Maste 		return;
30992c23cb7cSEd Maste 	printf("Symbol table (%s)", s->name);
310071edbbfdSEd Maste 	printf(" contains %d entries:\n", len);
31012c23cb7cSEd Maste 	printf("%7s%9s%14s%5s%8s%6s%9s%5s\n", "Num:", "Value", "Size", "Type",
31022c23cb7cSEd Maste 	    "Bind", "Vis", "Ndx", "Name");
31032c23cb7cSEd Maste 
310471edbbfdSEd Maste 	for (j = 0; j < len; j++) {
31052c23cb7cSEd Maste 		if (gelf_getsym(d, j, &sym) != &sym) {
31062c23cb7cSEd Maste 			warnx("gelf_getsym failed: %s", elf_errmsg(-1));
31072c23cb7cSEd Maste 			continue;
31082c23cb7cSEd Maste 		}
31092c23cb7cSEd Maste 		printf("%6d:", j);
31102c23cb7cSEd Maste 		printf(" %16.16jx", (uintmax_t) sym.st_value);
3111839529caSEd Maste 		printf(" %5ju", (uintmax_t) sym.st_size);
3112839529caSEd Maste 		printf(" %-7s", st_type(re->ehdr.e_machine,
3113b6b6f9ccSEd Maste 		    re->ehdr.e_ident[EI_OSABI], GELF_ST_TYPE(sym.st_info)));
31142c23cb7cSEd Maste 		printf(" %-6s", st_bind(GELF_ST_BIND(sym.st_info)));
31152c23cb7cSEd Maste 		printf(" %-8s", st_vis(GELF_ST_VISIBILITY(sym.st_other)));
31162c23cb7cSEd Maste 		printf(" %3s", st_shndx(sym.st_shndx));
31172c23cb7cSEd Maste 		if ((name = elf_strptr(re->elf, stab, sym.st_name)) != NULL)
31182c23cb7cSEd Maste 			printf(" %s", name);
31192c23cb7cSEd Maste 		/* Append symbol version string for SHT_DYNSYM symbol table. */
31202c23cb7cSEd Maste 		if (s->type == SHT_DYNSYM && re->ver != NULL &&
31212c23cb7cSEd Maste 		    re->vs != NULL && re->vs[j] > 1) {
3122656f49f8SEd Maste 			vs = re->vs[j] & VERSYM_VERSION;
3123656f49f8SEd Maste 			if (vs >= re->ver_sz || re->ver[vs].name == NULL) {
3124656f49f8SEd Maste 				warnx("invalid versym version index %u", vs);
3125656f49f8SEd Maste 				break;
3126656f49f8SEd Maste 			}
3127656f49f8SEd Maste 			if (re->vs[j] & VERSYM_HIDDEN || re->ver[vs].type == 0)
3128656f49f8SEd Maste 				printf("@%s (%d)", re->ver[vs].name, vs);
31292c23cb7cSEd Maste 			else
3130656f49f8SEd Maste 				printf("@@%s (%d)", re->ver[vs].name, vs);
31312c23cb7cSEd Maste 		}
31322c23cb7cSEd Maste 		putchar('\n');
31332c23cb7cSEd Maste 	}
31342c23cb7cSEd Maste 
31352c23cb7cSEd Maste }
31362c23cb7cSEd Maste 
31372c23cb7cSEd Maste static void
31382c23cb7cSEd Maste dump_symtabs(struct readelf *re)
31392c23cb7cSEd Maste {
31402c23cb7cSEd Maste 	GElf_Dyn dyn;
31412c23cb7cSEd Maste 	Elf_Data *d;
31422c23cb7cSEd Maste 	struct section *s;
31432c23cb7cSEd Maste 	uint64_t dyn_off;
314471edbbfdSEd Maste 	int elferr, i, len;
31452c23cb7cSEd Maste 
31462c23cb7cSEd Maste 	/*
31472c23cb7cSEd Maste 	 * If -D is specified, only dump the symbol table specified by
31482c23cb7cSEd Maste 	 * the DT_SYMTAB entry in the .dynamic section.
31492c23cb7cSEd Maste 	 */
31502c23cb7cSEd Maste 	dyn_off = 0;
31512c23cb7cSEd Maste 	if (re->options & RE_DD) {
31522c23cb7cSEd Maste 		s = NULL;
31532c23cb7cSEd Maste 		for (i = 0; (size_t)i < re->shnum; i++)
31542c23cb7cSEd Maste 			if (re->sl[i].type == SHT_DYNAMIC) {
31552c23cb7cSEd Maste 				s = &re->sl[i];
31562c23cb7cSEd Maste 				break;
31572c23cb7cSEd Maste 			}
31582c23cb7cSEd Maste 		if (s == NULL)
31592c23cb7cSEd Maste 			return;
31602c23cb7cSEd Maste 		(void) elf_errno();
31612c23cb7cSEd Maste 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
31622c23cb7cSEd Maste 			elferr = elf_errno();
31632c23cb7cSEd Maste 			if (elferr != 0)
31642c23cb7cSEd Maste 				warnx("elf_getdata failed: %s", elf_errmsg(-1));
31652c23cb7cSEd Maste 			return;
31662c23cb7cSEd Maste 		}
31672c23cb7cSEd Maste 		if (d->d_size <= 0)
31682c23cb7cSEd Maste 			return;
316971edbbfdSEd Maste 		if (!get_ent_count(s, &len))
317071edbbfdSEd Maste 			return;
31712c23cb7cSEd Maste 
317271edbbfdSEd Maste 		for (i = 0; i < len; i++) {
31732c23cb7cSEd Maste 			if (gelf_getdyn(d, i, &dyn) != &dyn) {
31742c23cb7cSEd Maste 				warnx("gelf_getdyn failed: %s", elf_errmsg(-1));
31752c23cb7cSEd Maste 				continue;
31762c23cb7cSEd Maste 			}
31772c23cb7cSEd Maste 			if (dyn.d_tag == DT_SYMTAB) {
31782c23cb7cSEd Maste 				dyn_off = dyn.d_un.d_val;
31792c23cb7cSEd Maste 				break;
31802c23cb7cSEd Maste 			}
31812c23cb7cSEd Maste 		}
31822c23cb7cSEd Maste 	}
31832c23cb7cSEd Maste 
31842c23cb7cSEd Maste 	/* Find and dump symbol tables. */
31852c23cb7cSEd Maste 	for (i = 0; (size_t)i < re->shnum; i++) {
31862c23cb7cSEd Maste 		s = &re->sl[i];
31872c23cb7cSEd Maste 		if (s->type == SHT_SYMTAB || s->type == SHT_DYNSYM) {
31882c23cb7cSEd Maste 			if (re->options & RE_DD) {
31892c23cb7cSEd Maste 				if (dyn_off == s->addr) {
31902c23cb7cSEd Maste 					dump_symtab(re, i);
31912c23cb7cSEd Maste 					break;
31922c23cb7cSEd Maste 				}
31932c23cb7cSEd Maste 			} else
31942c23cb7cSEd Maste 				dump_symtab(re, i);
31952c23cb7cSEd Maste 		}
31962c23cb7cSEd Maste 	}
31972c23cb7cSEd Maste }
31982c23cb7cSEd Maste 
31992c23cb7cSEd Maste static void
32002c23cb7cSEd Maste dump_svr4_hash(struct section *s)
32012c23cb7cSEd Maste {
32022c23cb7cSEd Maste 	Elf_Data	*d;
32032c23cb7cSEd Maste 	uint32_t	*buf;
32042c23cb7cSEd Maste 	uint32_t	 nbucket, nchain;
32052c23cb7cSEd Maste 	uint32_t	*bucket, *chain;
32062c23cb7cSEd Maste 	uint32_t	*bl, *c, maxl, total;
32072c23cb7cSEd Maste 	int		 elferr, i, j;
32082c23cb7cSEd Maste 
32092c23cb7cSEd Maste 	/* Read and parse the content of .hash section. */
32102c23cb7cSEd Maste 	(void) elf_errno();
32112c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
32122c23cb7cSEd Maste 		elferr = elf_errno();
32132c23cb7cSEd Maste 		if (elferr != 0)
32142c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
32152c23cb7cSEd Maste 		return;
32162c23cb7cSEd Maste 	}
32172c23cb7cSEd Maste 	if (d->d_size < 2 * sizeof(uint32_t)) {
32182c23cb7cSEd Maste 		warnx(".hash section too small");
32192c23cb7cSEd Maste 		return;
32202c23cb7cSEd Maste 	}
32212c23cb7cSEd Maste 	buf = d->d_buf;
32222c23cb7cSEd Maste 	nbucket = buf[0];
32232c23cb7cSEd Maste 	nchain = buf[1];
32242c23cb7cSEd Maste 	if (nbucket <= 0 || nchain <= 0) {
32252c23cb7cSEd Maste 		warnx("Malformed .hash section");
32262c23cb7cSEd Maste 		return;
32272c23cb7cSEd Maste 	}
32282c23cb7cSEd Maste 	if (d->d_size != (nbucket + nchain + 2) * sizeof(uint32_t)) {
32292c23cb7cSEd Maste 		warnx("Malformed .hash section");
32302c23cb7cSEd Maste 		return;
32312c23cb7cSEd Maste 	}
32322c23cb7cSEd Maste 	bucket = &buf[2];
32332c23cb7cSEd Maste 	chain = &buf[2 + nbucket];
32342c23cb7cSEd Maste 
32352c23cb7cSEd Maste 	maxl = 0;
32362c23cb7cSEd Maste 	if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
32372c23cb7cSEd Maste 		errx(EXIT_FAILURE, "calloc failed");
32382c23cb7cSEd Maste 	for (i = 0; (uint32_t)i < nbucket; i++)
32392c23cb7cSEd Maste 		for (j = bucket[i]; j > 0 && (uint32_t)j < nchain; j = chain[j])
32402c23cb7cSEd Maste 			if (++bl[i] > maxl)
32412c23cb7cSEd Maste 				maxl = bl[i];
32422c23cb7cSEd Maste 	if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
32432c23cb7cSEd Maste 		errx(EXIT_FAILURE, "calloc failed");
32442c23cb7cSEd Maste 	for (i = 0; (uint32_t)i < nbucket; i++)
32452c23cb7cSEd Maste 		c[bl[i]]++;
32462c23cb7cSEd Maste 	printf("\nHistogram for bucket list length (total of %u buckets):\n",
32472c23cb7cSEd Maste 	    nbucket);
32482c23cb7cSEd Maste 	printf(" Length\tNumber\t\t%% of total\tCoverage\n");
32492c23cb7cSEd Maste 	total = 0;
32502c23cb7cSEd Maste 	for (i = 0; (uint32_t)i <= maxl; i++) {
32512c23cb7cSEd Maste 		total += c[i] * i;
32522c23cb7cSEd Maste 		printf("%7u\t%-10u\t(%5.1f%%)\t%5.1f%%\n", i, c[i],
32532c23cb7cSEd Maste 		    c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
32542c23cb7cSEd Maste 	}
32552c23cb7cSEd Maste 	free(c);
32562c23cb7cSEd Maste 	free(bl);
32572c23cb7cSEd Maste }
32582c23cb7cSEd Maste 
32592c23cb7cSEd Maste static void
32602c23cb7cSEd Maste dump_svr4_hash64(struct readelf *re, struct section *s)
32612c23cb7cSEd Maste {
32622c23cb7cSEd Maste 	Elf_Data	*d, dst;
32632c23cb7cSEd Maste 	uint64_t	*buf;
32642c23cb7cSEd Maste 	uint64_t	 nbucket, nchain;
32652c23cb7cSEd Maste 	uint64_t	*bucket, *chain;
32662c23cb7cSEd Maste 	uint64_t	*bl, *c, maxl, total;
32672c23cb7cSEd Maste 	int		 elferr, i, j;
32682c23cb7cSEd Maste 
32692c23cb7cSEd Maste 	/*
32702c23cb7cSEd Maste 	 * ALPHA uses 64-bit hash entries. Since libelf assumes that
32712c23cb7cSEd Maste 	 * .hash section contains only 32-bit entry, an explicit
32722c23cb7cSEd Maste 	 * gelf_xlatetom is needed here.
32732c23cb7cSEd Maste 	 */
32742c23cb7cSEd Maste 	(void) elf_errno();
32752c23cb7cSEd Maste 	if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
32762c23cb7cSEd Maste 		elferr = elf_errno();
32772c23cb7cSEd Maste 		if (elferr != 0)
32782c23cb7cSEd Maste 			warnx("elf_rawdata failed: %s",
32792c23cb7cSEd Maste 			    elf_errmsg(elferr));
32802c23cb7cSEd Maste 		return;
32812c23cb7cSEd Maste 	}
32822c23cb7cSEd Maste 	d->d_type = ELF_T_XWORD;
32832c23cb7cSEd Maste 	memcpy(&dst, d, sizeof(Elf_Data));
32842c23cb7cSEd Maste 	if (gelf_xlatetom(re->elf, &dst, d,
32852c23cb7cSEd Maste 		re->ehdr.e_ident[EI_DATA]) != &dst) {
32862c23cb7cSEd Maste 		warnx("gelf_xlatetom failed: %s", elf_errmsg(-1));
32872c23cb7cSEd Maste 		return;
32882c23cb7cSEd Maste 	}
32892c23cb7cSEd Maste 	if (dst.d_size < 2 * sizeof(uint64_t)) {
32902c23cb7cSEd Maste 		warnx(".hash section too small");
32912c23cb7cSEd Maste 		return;
32922c23cb7cSEd Maste 	}
32932c23cb7cSEd Maste 	buf = dst.d_buf;
32942c23cb7cSEd Maste 	nbucket = buf[0];
32952c23cb7cSEd Maste 	nchain = buf[1];
32962c23cb7cSEd Maste 	if (nbucket <= 0 || nchain <= 0) {
32972c23cb7cSEd Maste 		warnx("Malformed .hash section");
32982c23cb7cSEd Maste 		return;
32992c23cb7cSEd Maste 	}
33002c23cb7cSEd Maste 	if (d->d_size != (nbucket + nchain + 2) * sizeof(uint32_t)) {
33012c23cb7cSEd Maste 		warnx("Malformed .hash section");
33022c23cb7cSEd Maste 		return;
33032c23cb7cSEd Maste 	}
33042c23cb7cSEd Maste 	bucket = &buf[2];
33052c23cb7cSEd Maste 	chain = &buf[2 + nbucket];
33062c23cb7cSEd Maste 
33072c23cb7cSEd Maste 	maxl = 0;
33082c23cb7cSEd Maste 	if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
33092c23cb7cSEd Maste 		errx(EXIT_FAILURE, "calloc failed");
33102c23cb7cSEd Maste 	for (i = 0; (uint32_t)i < nbucket; i++)
33112c23cb7cSEd Maste 		for (j = bucket[i]; j > 0 && (uint32_t)j < nchain; j = chain[j])
33122c23cb7cSEd Maste 			if (++bl[i] > maxl)
33132c23cb7cSEd Maste 				maxl = bl[i];
33142c23cb7cSEd Maste 	if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
33152c23cb7cSEd Maste 		errx(EXIT_FAILURE, "calloc failed");
33162c23cb7cSEd Maste 	for (i = 0; (uint64_t)i < nbucket; i++)
33172c23cb7cSEd Maste 		c[bl[i]]++;
33182c23cb7cSEd Maste 	printf("Histogram for bucket list length (total of %ju buckets):\n",
33192c23cb7cSEd Maste 	    (uintmax_t)nbucket);
33202c23cb7cSEd Maste 	printf(" Length\tNumber\t\t%% of total\tCoverage\n");
33212c23cb7cSEd Maste 	total = 0;
33222c23cb7cSEd Maste 	for (i = 0; (uint64_t)i <= maxl; i++) {
33232c23cb7cSEd Maste 		total += c[i] * i;
33242c23cb7cSEd Maste 		printf("%7u\t%-10ju\t(%5.1f%%)\t%5.1f%%\n", i, (uintmax_t)c[i],
33252c23cb7cSEd Maste 		    c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
33262c23cb7cSEd Maste 	}
33272c23cb7cSEd Maste 	free(c);
33282c23cb7cSEd Maste 	free(bl);
33292c23cb7cSEd Maste }
33302c23cb7cSEd Maste 
33312c23cb7cSEd Maste static void
33322c23cb7cSEd Maste dump_gnu_hash(struct readelf *re, struct section *s)
33332c23cb7cSEd Maste {
33342c23cb7cSEd Maste 	struct section	*ds;
33352c23cb7cSEd Maste 	Elf_Data	*d;
33362c23cb7cSEd Maste 	uint32_t	*buf;
33372c23cb7cSEd Maste 	uint32_t	*bucket, *chain;
3338cf781b2eSEd Maste 	uint32_t	 nbucket, nchain, symndx, maskwords;
33392c23cb7cSEd Maste 	uint32_t	*bl, *c, maxl, total;
33402c23cb7cSEd Maste 	int		 elferr, dynsymcount, i, j;
33412c23cb7cSEd Maste 
33422c23cb7cSEd Maste 	(void) elf_errno();
33432c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
33442c23cb7cSEd Maste 		elferr = elf_errno();
33452c23cb7cSEd Maste 		if (elferr != 0)
33462c23cb7cSEd Maste 			warnx("elf_getdata failed: %s",
33472c23cb7cSEd Maste 			    elf_errmsg(elferr));
33482c23cb7cSEd Maste 		return;
33492c23cb7cSEd Maste 	}
33502c23cb7cSEd Maste 	if (d->d_size < 4 * sizeof(uint32_t)) {
33512c23cb7cSEd Maste 		warnx(".gnu.hash section too small");
33522c23cb7cSEd Maste 		return;
33532c23cb7cSEd Maste 	}
33542c23cb7cSEd Maste 	buf = d->d_buf;
33552c23cb7cSEd Maste 	nbucket = buf[0];
33562c23cb7cSEd Maste 	symndx = buf[1];
33572c23cb7cSEd Maste 	maskwords = buf[2];
33582c23cb7cSEd Maste 	buf += 4;
3359656f49f8SEd Maste 	if (s->link >= re->shnum)
3360656f49f8SEd Maste 		return;
33612c23cb7cSEd Maste 	ds = &re->sl[s->link];
336271edbbfdSEd Maste 	if (!get_ent_count(ds, &dynsymcount))
336371edbbfdSEd Maste 		return;
3364b6b6f9ccSEd Maste 	if (symndx >= (uint32_t)dynsymcount) {
3365b6b6f9ccSEd Maste 		warnx("Malformed .gnu.hash section (symndx out of range)");
3366b6b6f9ccSEd Maste 		return;
3367b6b6f9ccSEd Maste 	}
33682c23cb7cSEd Maste 	nchain = dynsymcount - symndx;
33692c23cb7cSEd Maste 	if (d->d_size != 4 * sizeof(uint32_t) + maskwords *
33702c23cb7cSEd Maste 	    (re->ec == ELFCLASS32 ? sizeof(uint32_t) : sizeof(uint64_t)) +
33712c23cb7cSEd Maste 	    (nbucket + nchain) * sizeof(uint32_t)) {
33722c23cb7cSEd Maste 		warnx("Malformed .gnu.hash section");
33732c23cb7cSEd Maste 		return;
33742c23cb7cSEd Maste 	}
33752c23cb7cSEd Maste 	bucket = buf + (re->ec == ELFCLASS32 ? maskwords : maskwords * 2);
33762c23cb7cSEd Maste 	chain = bucket + nbucket;
33772c23cb7cSEd Maste 
33782c23cb7cSEd Maste 	maxl = 0;
33792c23cb7cSEd Maste 	if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
33802c23cb7cSEd Maste 		errx(EXIT_FAILURE, "calloc failed");
33812c23cb7cSEd Maste 	for (i = 0; (uint32_t)i < nbucket; i++)
33822c23cb7cSEd Maste 		for (j = bucket[i]; j > 0 && (uint32_t)j - symndx < nchain;
33832c23cb7cSEd Maste 		     j++) {
33842c23cb7cSEd Maste 			if (++bl[i] > maxl)
33852c23cb7cSEd Maste 				maxl = bl[i];
33862c23cb7cSEd Maste 			if (chain[j - symndx] & 1)
33872c23cb7cSEd Maste 				break;
33882c23cb7cSEd Maste 		}
33892c23cb7cSEd Maste 	if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
33902c23cb7cSEd Maste 		errx(EXIT_FAILURE, "calloc failed");
33912c23cb7cSEd Maste 	for (i = 0; (uint32_t)i < nbucket; i++)
33922c23cb7cSEd Maste 		c[bl[i]]++;
33932c23cb7cSEd Maste 	printf("Histogram for bucket list length (total of %u buckets):\n",
33942c23cb7cSEd Maste 	    nbucket);
33952c23cb7cSEd Maste 	printf(" Length\tNumber\t\t%% of total\tCoverage\n");
33962c23cb7cSEd Maste 	total = 0;
33972c23cb7cSEd Maste 	for (i = 0; (uint32_t)i <= maxl; i++) {
33982c23cb7cSEd Maste 		total += c[i] * i;
33992c23cb7cSEd Maste 		printf("%7u\t%-10u\t(%5.1f%%)\t%5.1f%%\n", i, c[i],
34002c23cb7cSEd Maste 		    c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
34012c23cb7cSEd Maste 	}
34022c23cb7cSEd Maste 	free(c);
34032c23cb7cSEd Maste 	free(bl);
34042c23cb7cSEd Maste }
34052c23cb7cSEd Maste 
34062c23cb7cSEd Maste static void
34072c23cb7cSEd Maste dump_hash(struct readelf *re)
34082c23cb7cSEd Maste {
34092c23cb7cSEd Maste 	struct section	*s;
34102c23cb7cSEd Maste 	int		 i;
34112c23cb7cSEd Maste 
34122c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
34132c23cb7cSEd Maste 		s = &re->sl[i];
34142c23cb7cSEd Maste 		if (s->type == SHT_HASH || s->type == SHT_GNU_HASH) {
34152c23cb7cSEd Maste 			if (s->type == SHT_GNU_HASH)
34162c23cb7cSEd Maste 				dump_gnu_hash(re, s);
34172c23cb7cSEd Maste 			else if (re->ehdr.e_machine == EM_ALPHA &&
34182c23cb7cSEd Maste 			    s->entsize == 8)
34192c23cb7cSEd Maste 				dump_svr4_hash64(re, s);
34202c23cb7cSEd Maste 			else
34212c23cb7cSEd Maste 				dump_svr4_hash(s);
34222c23cb7cSEd Maste 		}
34232c23cb7cSEd Maste 	}
34242c23cb7cSEd Maste }
34252c23cb7cSEd Maste 
34262c23cb7cSEd Maste static void
34272c23cb7cSEd Maste dump_notes(struct readelf *re)
34282c23cb7cSEd Maste {
34292c23cb7cSEd Maste 	struct section *s;
34302c23cb7cSEd Maste 	const char *rawfile;
34312c23cb7cSEd Maste 	GElf_Phdr phdr;
34322c23cb7cSEd Maste 	Elf_Data *d;
3433b6b6f9ccSEd Maste 	size_t filesize, phnum;
34342c23cb7cSEd Maste 	int i, elferr;
34352c23cb7cSEd Maste 
34362c23cb7cSEd Maste 	if (re->ehdr.e_type == ET_CORE) {
34372c23cb7cSEd Maste 		/*
34382c23cb7cSEd Maste 		 * Search program headers in the core file for
34392c23cb7cSEd Maste 		 * PT_NOTE entry.
34402c23cb7cSEd Maste 		 */
34412c23cb7cSEd Maste 		if (elf_getphnum(re->elf, &phnum) == 0) {
34422c23cb7cSEd Maste 			warnx("elf_getphnum failed: %s", elf_errmsg(-1));
34432c23cb7cSEd Maste 			return;
34442c23cb7cSEd Maste 		}
34452c23cb7cSEd Maste 		if (phnum == 0)
34462c23cb7cSEd Maste 			return;
3447b6b6f9ccSEd Maste 		if ((rawfile = elf_rawfile(re->elf, &filesize)) == NULL) {
34482c23cb7cSEd Maste 			warnx("elf_rawfile failed: %s", elf_errmsg(-1));
34492c23cb7cSEd Maste 			return;
34502c23cb7cSEd Maste 		}
34512c23cb7cSEd Maste 		for (i = 0; (size_t) i < phnum; i++) {
34522c23cb7cSEd Maste 			if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
34532c23cb7cSEd Maste 				warnx("gelf_getphdr failed: %s",
34542c23cb7cSEd Maste 				    elf_errmsg(-1));
34552c23cb7cSEd Maste 				continue;
34562c23cb7cSEd Maste 			}
3457b6b6f9ccSEd Maste 			if (phdr.p_type == PT_NOTE) {
3458b6b6f9ccSEd Maste 				if (phdr.p_offset >= filesize ||
3459b6b6f9ccSEd Maste 				    phdr.p_filesz > filesize - phdr.p_offset) {
3460b6b6f9ccSEd Maste 					warnx("invalid PHDR offset");
3461b6b6f9ccSEd Maste 					continue;
3462b6b6f9ccSEd Maste 				}
34632c23cb7cSEd Maste 				dump_notes_content(re, rawfile + phdr.p_offset,
34642c23cb7cSEd Maste 				    phdr.p_filesz, phdr.p_offset);
34652c23cb7cSEd Maste 			}
3466b6b6f9ccSEd Maste 		}
34672c23cb7cSEd Maste 
34682c23cb7cSEd Maste 	} else {
34692c23cb7cSEd Maste 		/*
34702c23cb7cSEd Maste 		 * For objects other than core files, Search for
34712c23cb7cSEd Maste 		 * SHT_NOTE sections.
34722c23cb7cSEd Maste 		 */
34732c23cb7cSEd Maste 		for (i = 0; (size_t) i < re->shnum; i++) {
34742c23cb7cSEd Maste 			s = &re->sl[i];
34752c23cb7cSEd Maste 			if (s->type == SHT_NOTE) {
34762c23cb7cSEd Maste 				(void) elf_errno();
34772c23cb7cSEd Maste 				if ((d = elf_getdata(s->scn, NULL)) == NULL) {
34782c23cb7cSEd Maste 					elferr = elf_errno();
34792c23cb7cSEd Maste 					if (elferr != 0)
34802c23cb7cSEd Maste 						warnx("elf_getdata failed: %s",
34812c23cb7cSEd Maste 						    elf_errmsg(elferr));
34822c23cb7cSEd Maste 					continue;
34832c23cb7cSEd Maste 				}
34842c23cb7cSEd Maste 				dump_notes_content(re, d->d_buf, d->d_size,
34852c23cb7cSEd Maste 				    s->off);
34862c23cb7cSEd Maste 			}
34872c23cb7cSEd Maste 		}
34882c23cb7cSEd Maste 	}
34892c23cb7cSEd Maste }
34902c23cb7cSEd Maste 
3491*3f0e38d7SEd Maste static struct flag_desc note_feature_ctl_flags[] = {
3492*3f0e38d7SEd Maste 	{ NT_FREEBSD_FCTL_ASLR_DISABLE,		"ASLR_DISABLE" },
3493*3f0e38d7SEd Maste 	{ 0, NULL }
3494*3f0e38d7SEd Maste };
3495*3f0e38d7SEd Maste 
3496*3f0e38d7SEd Maste static void
3497*3f0e38d7SEd Maste dump_notes_data(const char *name, uint32_t type, const char *buf, size_t sz)
3498*3f0e38d7SEd Maste {
3499*3f0e38d7SEd Maste 	size_t i;
3500*3f0e38d7SEd Maste 	const uint32_t *ubuf;
3501*3f0e38d7SEd Maste 
3502*3f0e38d7SEd Maste 	/* Note data is at least 4-byte aligned. */
3503*3f0e38d7SEd Maste 	if (((uintptr_t)buf & 3) != 0) {
3504*3f0e38d7SEd Maste 		warnx("bad note data alignment");
3505*3f0e38d7SEd Maste 		goto unknown;
3506*3f0e38d7SEd Maste 	}
3507*3f0e38d7SEd Maste 	ubuf = (const uint32_t *)(const void *)buf;
3508*3f0e38d7SEd Maste 
3509*3f0e38d7SEd Maste 	if (strcmp(name, "FreeBSD") == 0) {
3510*3f0e38d7SEd Maste 		switch (type) {
3511*3f0e38d7SEd Maste 		case NT_FREEBSD_ABI_TAG:
3512*3f0e38d7SEd Maste 			if (sz != 4)
3513*3f0e38d7SEd Maste 				goto unknown;
3514*3f0e38d7SEd Maste 			printf("   ABI tag: %u\n", ubuf[0]);
3515*3f0e38d7SEd Maste 			return;
3516*3f0e38d7SEd Maste 		/* NT_FREEBSD_NOINIT_TAG carries no data, treat as unknown. */
3517*3f0e38d7SEd Maste 		case NT_FREEBSD_ARCH_TAG:
3518*3f0e38d7SEd Maste 			if (sz != 4)
3519*3f0e38d7SEd Maste 				goto unknown;
3520*3f0e38d7SEd Maste 			printf("   Arch tag: %x\n", ubuf[0]);
3521*3f0e38d7SEd Maste 			return;
3522*3f0e38d7SEd Maste 		case NT_FREEBSD_FEATURE_CTL:
3523*3f0e38d7SEd Maste 			if (sz != 4)
3524*3f0e38d7SEd Maste 				goto unknown;
3525*3f0e38d7SEd Maste 			printf("   Features:");
3526*3f0e38d7SEd Maste 			dump_flags(note_feature_ctl_flags, ubuf[0]);
3527*3f0e38d7SEd Maste 			printf("\n");
3528*3f0e38d7SEd Maste 			return;
3529*3f0e38d7SEd Maste 		}
3530*3f0e38d7SEd Maste 	}
3531*3f0e38d7SEd Maste unknown:
3532*3f0e38d7SEd Maste 	printf("   description data:");
3533*3f0e38d7SEd Maste 	for (i = 0; i < sz; i++)
3534*3f0e38d7SEd Maste 		printf(" %02x", (unsigned char)buf[i]);
3535*3f0e38d7SEd Maste 	printf("\n");
3536*3f0e38d7SEd Maste }
3537*3f0e38d7SEd Maste 
35382c23cb7cSEd Maste static void
35392c23cb7cSEd Maste dump_notes_content(struct readelf *re, const char *buf, size_t sz, off_t off)
35402c23cb7cSEd Maste {
35412c23cb7cSEd Maste 	Elf_Note *note;
354202b08c90SEd Maste 	const char *end, *name;
35432c23cb7cSEd Maste 
35442c23cb7cSEd Maste 	printf("\nNotes at offset %#010jx with length %#010jx:\n",
35452c23cb7cSEd Maste 	    (uintmax_t) off, (uintmax_t) sz);
35462c23cb7cSEd Maste 	printf("  %-13s %-15s %s\n", "Owner", "Data size", "Description");
35472c23cb7cSEd Maste 	end = buf + sz;
35482c23cb7cSEd Maste 	while (buf < end) {
354902b08c90SEd Maste 		if (buf + sizeof(*note) > end) {
355002b08c90SEd Maste 			warnx("invalid note header");
355102b08c90SEd Maste 			return;
355202b08c90SEd Maste 		}
35532c23cb7cSEd Maste 		note = (Elf_Note *)(uintptr_t) buf;
3554675f752cSEd Maste 		buf += sizeof(Elf_Note);
3555675f752cSEd Maste 		name = buf;
3556675f752cSEd Maste 		buf += roundup2(note->n_namesz, 4);
355702b08c90SEd Maste 		/*
355802b08c90SEd Maste 		 * The name field is required to be nul-terminated, and
355902b08c90SEd Maste 		 * n_namesz includes the terminating nul in observed
356002b08c90SEd Maste 		 * implementations (contrary to the ELF-64 spec). A special
356102b08c90SEd Maste 		 * case is needed for cores generated by some older Linux
356202b08c90SEd Maste 		 * versions, which write a note named "CORE" without a nul
356302b08c90SEd Maste 		 * terminator and n_namesz = 4.
356402b08c90SEd Maste 		 */
356502b08c90SEd Maste 		if (note->n_namesz == 0)
356602b08c90SEd Maste 			name = "";
356702b08c90SEd Maste 		else if (note->n_namesz == 4 && strncmp(name, "CORE", 4) == 0)
356802b08c90SEd Maste 			name = "CORE";
356902b08c90SEd Maste 		else if (strnlen(name, note->n_namesz) >= note->n_namesz)
357002b08c90SEd Maste 			name = "<invalid>";
357102b08c90SEd Maste 		printf("  %-13s %#010jx", name, (uintmax_t) note->n_descsz);
357202b08c90SEd Maste 		printf("      %s\n", note_type(name, re->ehdr.e_type,
357302b08c90SEd Maste 		    note->n_type));
3574*3f0e38d7SEd Maste 		dump_notes_data(name, note->n_type, buf, note->n_descsz);
3575675f752cSEd Maste 		buf += roundup2(note->n_descsz, 4);
35762c23cb7cSEd Maste 	}
35772c23cb7cSEd Maste }
35782c23cb7cSEd Maste 
35792c23cb7cSEd Maste /*
35802c23cb7cSEd Maste  * Symbol versioning sections are the same for 32bit and 64bit
35812c23cb7cSEd Maste  * ELF objects.
35822c23cb7cSEd Maste  */
35832c23cb7cSEd Maste #define Elf_Verdef	Elf32_Verdef
35842c23cb7cSEd Maste #define	Elf_Verdaux	Elf32_Verdaux
35852c23cb7cSEd Maste #define	Elf_Verneed	Elf32_Verneed
35862c23cb7cSEd Maste #define	Elf_Vernaux	Elf32_Vernaux
35872c23cb7cSEd Maste 
35882c23cb7cSEd Maste #define	SAVE_VERSION_NAME(x, n, t)					\
35892c23cb7cSEd Maste 	do {								\
35902c23cb7cSEd Maste 		while (x >= re->ver_sz) {				\
35912c23cb7cSEd Maste 			nv = realloc(re->ver,				\
35922c23cb7cSEd Maste 			    sizeof(*re->ver) * re->ver_sz * 2);		\
35932c23cb7cSEd Maste 			if (nv == NULL) {				\
35942c23cb7cSEd Maste 				warn("realloc failed");			\
35952c23cb7cSEd Maste 				free(re->ver);				\
35962c23cb7cSEd Maste 				return;					\
35972c23cb7cSEd Maste 			}						\
35982c23cb7cSEd Maste 			re->ver = nv;					\
35992c23cb7cSEd Maste 			for (i = re->ver_sz; i < re->ver_sz * 2; i++) {	\
36002c23cb7cSEd Maste 				re->ver[i].name = NULL;			\
36012c23cb7cSEd Maste 				re->ver[i].type = 0;			\
36022c23cb7cSEd Maste 			}						\
36032c23cb7cSEd Maste 			re->ver_sz *= 2;				\
36042c23cb7cSEd Maste 		}							\
36052c23cb7cSEd Maste 		if (x > 1) {						\
36062c23cb7cSEd Maste 			re->ver[x].name = n;				\
36072c23cb7cSEd Maste 			re->ver[x].type = t;				\
36082c23cb7cSEd Maste 		}							\
36092c23cb7cSEd Maste 	} while (0)
36102c23cb7cSEd Maste 
36112c23cb7cSEd Maste 
36122c23cb7cSEd Maste static void
36132c23cb7cSEd Maste dump_verdef(struct readelf *re, int dump)
36142c23cb7cSEd Maste {
36152c23cb7cSEd Maste 	struct section *s;
36162c23cb7cSEd Maste 	struct symver *nv;
36172c23cb7cSEd Maste 	Elf_Data *d;
36182c23cb7cSEd Maste 	Elf_Verdef *vd;
36192c23cb7cSEd Maste 	Elf_Verdaux *vda;
36202c23cb7cSEd Maste 	uint8_t *buf, *end, *buf2;
36212c23cb7cSEd Maste 	const char *name;
36222c23cb7cSEd Maste 	int elferr, i, j;
36232c23cb7cSEd Maste 
36242c23cb7cSEd Maste 	if ((s = re->vd_s) == NULL)
36252c23cb7cSEd Maste 		return;
3626656f49f8SEd Maste 	if (s->link >= re->shnum)
3627656f49f8SEd Maste 		return;
36282c23cb7cSEd Maste 
36292c23cb7cSEd Maste 	if (re->ver == NULL) {
36302c23cb7cSEd Maste 		re->ver_sz = 16;
36312c23cb7cSEd Maste 		if ((re->ver = calloc(re->ver_sz, sizeof(*re->ver))) ==
36322c23cb7cSEd Maste 		    NULL) {
36332c23cb7cSEd Maste 			warn("calloc failed");
36342c23cb7cSEd Maste 			return;
36352c23cb7cSEd Maste 		}
36362c23cb7cSEd Maste 		re->ver[0].name = "*local*";
36372c23cb7cSEd Maste 		re->ver[1].name = "*global*";
36382c23cb7cSEd Maste 	}
36392c23cb7cSEd Maste 
36402c23cb7cSEd Maste 	if (dump)
36412c23cb7cSEd Maste 		printf("\nVersion definition section (%s):\n", s->name);
36422c23cb7cSEd Maste 	(void) elf_errno();
36432c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
36442c23cb7cSEd Maste 		elferr = elf_errno();
36452c23cb7cSEd Maste 		if (elferr != 0)
36462c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
36472c23cb7cSEd Maste 		return;
36482c23cb7cSEd Maste 	}
36492c23cb7cSEd Maste 	if (d->d_size == 0)
36502c23cb7cSEd Maste 		return;
36512c23cb7cSEd Maste 
36522c23cb7cSEd Maste 	buf = d->d_buf;
36532c23cb7cSEd Maste 	end = buf + d->d_size;
36542c23cb7cSEd Maste 	while (buf + sizeof(Elf_Verdef) <= end) {
36552c23cb7cSEd Maste 		vd = (Elf_Verdef *) (uintptr_t) buf;
36562c23cb7cSEd Maste 		if (dump) {
36572c23cb7cSEd Maste 			printf("  0x%4.4lx", (unsigned long)
36582c23cb7cSEd Maste 			    (buf - (uint8_t *)d->d_buf));
36592c23cb7cSEd Maste 			printf(" vd_version: %u vd_flags: %d"
36602c23cb7cSEd Maste 			    " vd_ndx: %u vd_cnt: %u", vd->vd_version,
36612c23cb7cSEd Maste 			    vd->vd_flags, vd->vd_ndx, vd->vd_cnt);
36622c23cb7cSEd Maste 		}
36632c23cb7cSEd Maste 		buf2 = buf + vd->vd_aux;
36642c23cb7cSEd Maste 		j = 0;
36652c23cb7cSEd Maste 		while (buf2 + sizeof(Elf_Verdaux) <= end && j < vd->vd_cnt) {
36662c23cb7cSEd Maste 			vda = (Elf_Verdaux *) (uintptr_t) buf2;
36672c23cb7cSEd Maste 			name = get_string(re, s->link, vda->vda_name);
36682c23cb7cSEd Maste 			if (j == 0) {
36692c23cb7cSEd Maste 				if (dump)
36702c23cb7cSEd Maste 					printf(" vda_name: %s\n", name);
36712c23cb7cSEd Maste 				SAVE_VERSION_NAME((int)vd->vd_ndx, name, 1);
36722c23cb7cSEd Maste 			} else if (dump)
36732c23cb7cSEd Maste 				printf("  0x%4.4lx parent: %s\n",
36742c23cb7cSEd Maste 				    (unsigned long) (buf2 -
36752c23cb7cSEd Maste 				    (uint8_t *)d->d_buf), name);
36762c23cb7cSEd Maste 			if (vda->vda_next == 0)
36772c23cb7cSEd Maste 				break;
36782c23cb7cSEd Maste 			buf2 += vda->vda_next;
36792c23cb7cSEd Maste 			j++;
36802c23cb7cSEd Maste 		}
36812c23cb7cSEd Maste 		if (vd->vd_next == 0)
36822c23cb7cSEd Maste 			break;
36832c23cb7cSEd Maste 		buf += vd->vd_next;
36842c23cb7cSEd Maste 	}
36852c23cb7cSEd Maste }
36862c23cb7cSEd Maste 
36872c23cb7cSEd Maste static void
36882c23cb7cSEd Maste dump_verneed(struct readelf *re, int dump)
36892c23cb7cSEd Maste {
36902c23cb7cSEd Maste 	struct section *s;
36912c23cb7cSEd Maste 	struct symver *nv;
36922c23cb7cSEd Maste 	Elf_Data *d;
36932c23cb7cSEd Maste 	Elf_Verneed *vn;
36942c23cb7cSEd Maste 	Elf_Vernaux *vna;
36952c23cb7cSEd Maste 	uint8_t *buf, *end, *buf2;
36962c23cb7cSEd Maste 	const char *name;
36972c23cb7cSEd Maste 	int elferr, i, j;
36982c23cb7cSEd Maste 
36992c23cb7cSEd Maste 	if ((s = re->vn_s) == NULL)
37002c23cb7cSEd Maste 		return;
3701656f49f8SEd Maste 	if (s->link >= re->shnum)
3702656f49f8SEd Maste 		return;
37032c23cb7cSEd Maste 
37042c23cb7cSEd Maste 	if (re->ver == NULL) {
37052c23cb7cSEd Maste 		re->ver_sz = 16;
37062c23cb7cSEd Maste 		if ((re->ver = calloc(re->ver_sz, sizeof(*re->ver))) ==
37072c23cb7cSEd Maste 		    NULL) {
37082c23cb7cSEd Maste 			warn("calloc failed");
37092c23cb7cSEd Maste 			return;
37102c23cb7cSEd Maste 		}
37112c23cb7cSEd Maste 		re->ver[0].name = "*local*";
37122c23cb7cSEd Maste 		re->ver[1].name = "*global*";
37132c23cb7cSEd Maste 	}
37142c23cb7cSEd Maste 
37152c23cb7cSEd Maste 	if (dump)
37162c23cb7cSEd Maste 		printf("\nVersion needed section (%s):\n", s->name);
37172c23cb7cSEd Maste 	(void) elf_errno();
37182c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
37192c23cb7cSEd Maste 		elferr = elf_errno();
37202c23cb7cSEd Maste 		if (elferr != 0)
37212c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
37222c23cb7cSEd Maste 		return;
37232c23cb7cSEd Maste 	}
37242c23cb7cSEd Maste 	if (d->d_size == 0)
37252c23cb7cSEd Maste 		return;
37262c23cb7cSEd Maste 
37272c23cb7cSEd Maste 	buf = d->d_buf;
37282c23cb7cSEd Maste 	end = buf + d->d_size;
37292c23cb7cSEd Maste 	while (buf + sizeof(Elf_Verneed) <= end) {
37302c23cb7cSEd Maste 		vn = (Elf_Verneed *) (uintptr_t) buf;
37312c23cb7cSEd Maste 		if (dump) {
37322c23cb7cSEd Maste 			printf("  0x%4.4lx", (unsigned long)
37332c23cb7cSEd Maste 			    (buf - (uint8_t *)d->d_buf));
37342c23cb7cSEd Maste 			printf(" vn_version: %u vn_file: %s vn_cnt: %u\n",
37352c23cb7cSEd Maste 			    vn->vn_version,
37362c23cb7cSEd Maste 			    get_string(re, s->link, vn->vn_file),
37372c23cb7cSEd Maste 			    vn->vn_cnt);
37382c23cb7cSEd Maste 		}
37392c23cb7cSEd Maste 		buf2 = buf + vn->vn_aux;
37402c23cb7cSEd Maste 		j = 0;
37412c23cb7cSEd Maste 		while (buf2 + sizeof(Elf_Vernaux) <= end && j < vn->vn_cnt) {
37422c23cb7cSEd Maste 			vna = (Elf32_Vernaux *) (uintptr_t) buf2;
37432c23cb7cSEd Maste 			if (dump)
37442c23cb7cSEd Maste 				printf("  0x%4.4lx", (unsigned long)
37452c23cb7cSEd Maste 				    (buf2 - (uint8_t *)d->d_buf));
37462c23cb7cSEd Maste 			name = get_string(re, s->link, vna->vna_name);
37472c23cb7cSEd Maste 			if (dump)
37482c23cb7cSEd Maste 				printf("   vna_name: %s vna_flags: %u"
37492c23cb7cSEd Maste 				    " vna_other: %u\n", name,
37502c23cb7cSEd Maste 				    vna->vna_flags, vna->vna_other);
37512c23cb7cSEd Maste 			SAVE_VERSION_NAME((int)vna->vna_other, name, 0);
37522c23cb7cSEd Maste 			if (vna->vna_next == 0)
37532c23cb7cSEd Maste 				break;
37542c23cb7cSEd Maste 			buf2 += vna->vna_next;
37552c23cb7cSEd Maste 			j++;
37562c23cb7cSEd Maste 		}
37572c23cb7cSEd Maste 		if (vn->vn_next == 0)
37582c23cb7cSEd Maste 			break;
37592c23cb7cSEd Maste 		buf += vn->vn_next;
37602c23cb7cSEd Maste 	}
37612c23cb7cSEd Maste }
37622c23cb7cSEd Maste 
37632c23cb7cSEd Maste static void
37642c23cb7cSEd Maste dump_versym(struct readelf *re)
37652c23cb7cSEd Maste {
37662c23cb7cSEd Maste 	int i;
3767656f49f8SEd Maste 	uint16_t vs;
37682c23cb7cSEd Maste 
37692c23cb7cSEd Maste 	if (re->vs_s == NULL || re->ver == NULL || re->vs == NULL)
37702c23cb7cSEd Maste 		return;
37712c23cb7cSEd Maste 	printf("\nVersion symbol section (%s):\n", re->vs_s->name);
37722c23cb7cSEd Maste 	for (i = 0; i < re->vs_sz; i++) {
37732c23cb7cSEd Maste 		if ((i & 3) == 0) {
37742c23cb7cSEd Maste 			if (i > 0)
37752c23cb7cSEd Maste 				putchar('\n');
37762c23cb7cSEd Maste 			printf("  %03x:", i);
37772c23cb7cSEd Maste 		}
3778656f49f8SEd Maste 		vs = re->vs[i] & VERSYM_VERSION;
3779656f49f8SEd Maste 		if (vs >= re->ver_sz || re->ver[vs].name == NULL) {
3780656f49f8SEd Maste 			warnx("invalid versym version index %u", re->vs[i]);
3781656f49f8SEd Maste 			break;
3782656f49f8SEd Maste 		}
3783656f49f8SEd Maste 		if (re->vs[i] & VERSYM_HIDDEN)
3784656f49f8SEd Maste 			printf(" %3xh %-12s ", vs,
3785656f49f8SEd Maste 			    re->ver[re->vs[i] & VERSYM_VERSION].name);
37862c23cb7cSEd Maste 		else
3787656f49f8SEd Maste 			printf(" %3x %-12s ", vs, re->ver[re->vs[i]].name);
37882c23cb7cSEd Maste 	}
37892c23cb7cSEd Maste 	putchar('\n');
37902c23cb7cSEd Maste }
37912c23cb7cSEd Maste 
37922c23cb7cSEd Maste static void
37932c23cb7cSEd Maste dump_ver(struct readelf *re)
37942c23cb7cSEd Maste {
37952c23cb7cSEd Maste 
37962c23cb7cSEd Maste 	if (re->vs_s && re->ver && re->vs)
37972c23cb7cSEd Maste 		dump_versym(re);
37982c23cb7cSEd Maste 	if (re->vd_s)
37992c23cb7cSEd Maste 		dump_verdef(re, 1);
38002c23cb7cSEd Maste 	if (re->vn_s)
38012c23cb7cSEd Maste 		dump_verneed(re, 1);
38022c23cb7cSEd Maste }
38032c23cb7cSEd Maste 
38042c23cb7cSEd Maste static void
38052c23cb7cSEd Maste search_ver(struct readelf *re)
38062c23cb7cSEd Maste {
38072c23cb7cSEd Maste 	struct section *s;
38082c23cb7cSEd Maste 	Elf_Data *d;
38092c23cb7cSEd Maste 	int elferr, i;
38102c23cb7cSEd Maste 
38112c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
38122c23cb7cSEd Maste 		s = &re->sl[i];
38132c23cb7cSEd Maste 		if (s->type == SHT_SUNW_versym)
38142c23cb7cSEd Maste 			re->vs_s = s;
38152c23cb7cSEd Maste 		if (s->type == SHT_SUNW_verneed)
38162c23cb7cSEd Maste 			re->vn_s = s;
38172c23cb7cSEd Maste 		if (s->type == SHT_SUNW_verdef)
38182c23cb7cSEd Maste 			re->vd_s = s;
38192c23cb7cSEd Maste 	}
38202c23cb7cSEd Maste 	if (re->vd_s)
38212c23cb7cSEd Maste 		dump_verdef(re, 0);
38222c23cb7cSEd Maste 	if (re->vn_s)
38232c23cb7cSEd Maste 		dump_verneed(re, 0);
38242c23cb7cSEd Maste 	if (re->vs_s && re->ver != NULL) {
38252c23cb7cSEd Maste 		(void) elf_errno();
38262c23cb7cSEd Maste 		if ((d = elf_getdata(re->vs_s->scn, NULL)) == NULL) {
38272c23cb7cSEd Maste 			elferr = elf_errno();
38282c23cb7cSEd Maste 			if (elferr != 0)
38292c23cb7cSEd Maste 				warnx("elf_getdata failed: %s",
38302c23cb7cSEd Maste 				    elf_errmsg(elferr));
38312c23cb7cSEd Maste 			return;
38322c23cb7cSEd Maste 		}
38332c23cb7cSEd Maste 		if (d->d_size == 0)
38342c23cb7cSEd Maste 			return;
38352c23cb7cSEd Maste 		re->vs = d->d_buf;
38362c23cb7cSEd Maste 		re->vs_sz = d->d_size / sizeof(Elf32_Half);
38372c23cb7cSEd Maste 	}
38382c23cb7cSEd Maste }
38392c23cb7cSEd Maste 
38402c23cb7cSEd Maste #undef	Elf_Verdef
38412c23cb7cSEd Maste #undef	Elf_Verdaux
38422c23cb7cSEd Maste #undef	Elf_Verneed
38432c23cb7cSEd Maste #undef	Elf_Vernaux
38442c23cb7cSEd Maste #undef	SAVE_VERSION_NAME
38452c23cb7cSEd Maste 
38462c23cb7cSEd Maste /*
38472c23cb7cSEd Maste  * Elf32_Lib and Elf64_Lib are identical.
38482c23cb7cSEd Maste  */
38492c23cb7cSEd Maste #define	Elf_Lib		Elf32_Lib
38502c23cb7cSEd Maste 
38512c23cb7cSEd Maste static void
38522c23cb7cSEd Maste dump_liblist(struct readelf *re)
38532c23cb7cSEd Maste {
38542c23cb7cSEd Maste 	struct section *s;
38552c23cb7cSEd Maste 	struct tm *t;
38562c23cb7cSEd Maste 	time_t ti;
38572c23cb7cSEd Maste 	char tbuf[20];
38582c23cb7cSEd Maste 	Elf_Data *d;
38592c23cb7cSEd Maste 	Elf_Lib *lib;
386071edbbfdSEd Maste 	int i, j, k, elferr, first, len;
38612c23cb7cSEd Maste 
38622c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
38632c23cb7cSEd Maste 		s = &re->sl[i];
38642c23cb7cSEd Maste 		if (s->type != SHT_GNU_LIBLIST)
38652c23cb7cSEd Maste 			continue;
3866656f49f8SEd Maste 		if (s->link >= re->shnum)
3867656f49f8SEd Maste 			continue;
38682c23cb7cSEd Maste 		(void) elf_errno();
38692c23cb7cSEd Maste 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
38702c23cb7cSEd Maste 			elferr = elf_errno();
38712c23cb7cSEd Maste 			if (elferr != 0)
38722c23cb7cSEd Maste 				warnx("elf_getdata failed: %s",
38732c23cb7cSEd Maste 				    elf_errmsg(elferr));
38742c23cb7cSEd Maste 			continue;
38752c23cb7cSEd Maste 		}
38762c23cb7cSEd Maste 		if (d->d_size <= 0)
38772c23cb7cSEd Maste 			continue;
38782c23cb7cSEd Maste 		lib = d->d_buf;
387971edbbfdSEd Maste 		if (!get_ent_count(s, &len))
388071edbbfdSEd Maste 			continue;
38812c23cb7cSEd Maste 		printf("\nLibrary list section '%s' ", s->name);
388271edbbfdSEd Maste 		printf("contains %d entries:\n", len);
38832c23cb7cSEd Maste 		printf("%12s%24s%18s%10s%6s\n", "Library", "Time Stamp",
38842c23cb7cSEd Maste 		    "Checksum", "Version", "Flags");
38852c23cb7cSEd Maste 		for (j = 0; (uint64_t) j < s->sz / s->entsize; j++) {
38862c23cb7cSEd Maste 			printf("%3d: ", j);
38872c23cb7cSEd Maste 			printf("%-20.20s ",
38882c23cb7cSEd Maste 			    get_string(re, s->link, lib->l_name));
38892c23cb7cSEd Maste 			ti = lib->l_time_stamp;
38902c23cb7cSEd Maste 			t = gmtime(&ti);
38912c23cb7cSEd Maste 			snprintf(tbuf, sizeof(tbuf), "%04d-%02d-%02dT%02d:%02d"
38922c23cb7cSEd Maste 			    ":%2d", t->tm_year + 1900, t->tm_mon + 1,
38932c23cb7cSEd Maste 			    t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
38942c23cb7cSEd Maste 			printf("%-19.19s ", tbuf);
38952c23cb7cSEd Maste 			printf("0x%08x ", lib->l_checksum);
38962c23cb7cSEd Maste 			printf("%-7d %#x", lib->l_version, lib->l_flags);
38972c23cb7cSEd Maste 			if (lib->l_flags != 0) {
38982c23cb7cSEd Maste 				first = 1;
38992c23cb7cSEd Maste 				putchar('(');
39002c23cb7cSEd Maste 				for (k = 0; l_flag[k].name != NULL; k++) {
39012c23cb7cSEd Maste 					if ((l_flag[k].value & lib->l_flags) ==
39022c23cb7cSEd Maste 					    0)
39032c23cb7cSEd Maste 						continue;
39042c23cb7cSEd Maste 					if (!first)
39052c23cb7cSEd Maste 						putchar(',');
39062c23cb7cSEd Maste 					else
39072c23cb7cSEd Maste 						first = 0;
39082c23cb7cSEd Maste 					printf("%s", l_flag[k].name);
39092c23cb7cSEd Maste 				}
39102c23cb7cSEd Maste 				putchar(')');
39112c23cb7cSEd Maste 			}
39122c23cb7cSEd Maste 			putchar('\n');
39132c23cb7cSEd Maste 			lib++;
39142c23cb7cSEd Maste 		}
39152c23cb7cSEd Maste 	}
39162c23cb7cSEd Maste }
39172c23cb7cSEd Maste 
39182c23cb7cSEd Maste #undef Elf_Lib
39192c23cb7cSEd Maste 
39203ef90571SEd Maste static void
39213ef90571SEd Maste dump_section_groups(struct readelf *re)
39223ef90571SEd Maste {
39233ef90571SEd Maste 	struct section *s;
39243ef90571SEd Maste 	const char *symname;
39253ef90571SEd Maste 	Elf_Data *d;
39263ef90571SEd Maste 	uint32_t *w;
39273ef90571SEd Maste 	int i, j, elferr;
39283ef90571SEd Maste 	size_t n;
39293ef90571SEd Maste 
39303ef90571SEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
39313ef90571SEd Maste 		s = &re->sl[i];
39323ef90571SEd Maste 		if (s->type != SHT_GROUP)
39333ef90571SEd Maste 			continue;
3934656f49f8SEd Maste 		if (s->link >= re->shnum)
3935656f49f8SEd Maste 			continue;
39363ef90571SEd Maste 		(void) elf_errno();
39373ef90571SEd Maste 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
39383ef90571SEd Maste 			elferr = elf_errno();
39393ef90571SEd Maste 			if (elferr != 0)
39403ef90571SEd Maste 				warnx("elf_getdata failed: %s",
39413ef90571SEd Maste 				    elf_errmsg(elferr));
39423ef90571SEd Maste 			continue;
39433ef90571SEd Maste 		}
39443ef90571SEd Maste 		if (d->d_size <= 0)
39453ef90571SEd Maste 			continue;
39463ef90571SEd Maste 
39473ef90571SEd Maste 		w = d->d_buf;
39483ef90571SEd Maste 
39493ef90571SEd Maste 		/* We only support COMDAT section. */
39503ef90571SEd Maste #ifndef GRP_COMDAT
39513ef90571SEd Maste #define	GRP_COMDAT 0x1
39523ef90571SEd Maste #endif
39533ef90571SEd Maste 		if ((*w++ & GRP_COMDAT) == 0)
39543ef90571SEd Maste 			return;
39553ef90571SEd Maste 
39563ef90571SEd Maste 		if (s->entsize == 0)
39573ef90571SEd Maste 			s->entsize = 4;
39583ef90571SEd Maste 
39593ef90571SEd Maste 		symname = get_symbol_name(re, s->link, s->info);
39603ef90571SEd Maste 		n = s->sz / s->entsize;
39613ef90571SEd Maste 		if (n-- < 1)
39623ef90571SEd Maste 			return;
39633ef90571SEd Maste 
39643ef90571SEd Maste 		printf("\nCOMDAT group section [%5d] `%s' [%s] contains %ju"
39653ef90571SEd Maste 		    " sections:\n", i, s->name, symname, (uintmax_t)n);
39663ef90571SEd Maste 		printf("   %-10.10s %s\n", "[Index]", "Name");
39673ef90571SEd Maste 		for (j = 0; (size_t) j < n; j++, w++) {
39683ef90571SEd Maste 			if (*w >= re->shnum) {
39693ef90571SEd Maste 				warnx("invalid section index: %u", *w);
39703ef90571SEd Maste 				continue;
39713ef90571SEd Maste 			}
39723ef90571SEd Maste 			printf("   [%5u]   %s\n", *w, re->sl[*w].name);
39733ef90571SEd Maste 		}
39743ef90571SEd Maste 	}
39753ef90571SEd Maste }
39763ef90571SEd Maste 
39772c23cb7cSEd Maste static uint8_t *
397895fd7f26SEd Maste dump_unknown_tag(uint64_t tag, uint8_t *p, uint8_t *pe)
39792c23cb7cSEd Maste {
39802c23cb7cSEd Maste 	uint64_t val;
39812c23cb7cSEd Maste 
39822c23cb7cSEd Maste 	/*
39832c23cb7cSEd Maste 	 * According to ARM EABI: For tags > 32, even numbered tags have
39842c23cb7cSEd Maste 	 * a ULEB128 param and odd numbered ones have NUL-terminated
39852c23cb7cSEd Maste 	 * string param. This rule probably also applies for tags <= 32
39862c23cb7cSEd Maste 	 * if the object arch is not ARM.
39872c23cb7cSEd Maste 	 */
39882c23cb7cSEd Maste 
39892c23cb7cSEd Maste 	printf("  Tag_unknown_%ju: ", (uintmax_t) tag);
39902c23cb7cSEd Maste 
39912c23cb7cSEd Maste 	if (tag & 1) {
39922c23cb7cSEd Maste 		printf("%s\n", (char *) p);
39932c23cb7cSEd Maste 		p += strlen((char *) p) + 1;
39942c23cb7cSEd Maste 	} else {
399595fd7f26SEd Maste 		val = _decode_uleb128(&p, pe);
39962c23cb7cSEd Maste 		printf("%ju\n", (uintmax_t) val);
39972c23cb7cSEd Maste 	}
39982c23cb7cSEd Maste 
39992c23cb7cSEd Maste 	return (p);
40002c23cb7cSEd Maste }
40012c23cb7cSEd Maste 
40022c23cb7cSEd Maste static uint8_t *
400395fd7f26SEd Maste dump_compatibility_tag(uint8_t *p, uint8_t *pe)
40042c23cb7cSEd Maste {
40052c23cb7cSEd Maste 	uint64_t val;
40062c23cb7cSEd Maste 
400795fd7f26SEd Maste 	val = _decode_uleb128(&p, pe);
4008839529caSEd Maste 	printf("flag = %ju, vendor = %s\n", (uintmax_t) val, p);
40092c23cb7cSEd Maste 	p += strlen((char *) p) + 1;
40102c23cb7cSEd Maste 
40112c23cb7cSEd Maste 	return (p);
40122c23cb7cSEd Maste }
40132c23cb7cSEd Maste 
40142c23cb7cSEd Maste static void
40152c23cb7cSEd Maste dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe)
40162c23cb7cSEd Maste {
40172c23cb7cSEd Maste 	uint64_t tag, val;
40182c23cb7cSEd Maste 	size_t i;
40192c23cb7cSEd Maste 	int found, desc;
40202c23cb7cSEd Maste 
40212c23cb7cSEd Maste 	(void) re;
40222c23cb7cSEd Maste 
40232c23cb7cSEd Maste 	while (p < pe) {
402495fd7f26SEd Maste 		tag = _decode_uleb128(&p, pe);
40252c23cb7cSEd Maste 		found = desc = 0;
40262c23cb7cSEd Maste 		for (i = 0; i < sizeof(aeabi_tags) / sizeof(aeabi_tags[0]);
40272c23cb7cSEd Maste 		     i++) {
40282c23cb7cSEd Maste 			if (tag == aeabi_tags[i].tag) {
40292c23cb7cSEd Maste 				found = 1;
40302c23cb7cSEd Maste 				printf("  %s: ", aeabi_tags[i].s_tag);
40312c23cb7cSEd Maste 				if (aeabi_tags[i].get_desc) {
40322c23cb7cSEd Maste 					desc = 1;
403395fd7f26SEd Maste 					val = _decode_uleb128(&p, pe);
40342c23cb7cSEd Maste 					printf("%s\n",
40352c23cb7cSEd Maste 					    aeabi_tags[i].get_desc(val));
40362c23cb7cSEd Maste 				}
40372c23cb7cSEd Maste 				break;
40382c23cb7cSEd Maste 			}
40392c23cb7cSEd Maste 			if (tag < aeabi_tags[i].tag)
40402c23cb7cSEd Maste 				break;
40412c23cb7cSEd Maste 		}
40422c23cb7cSEd Maste 		if (!found) {
404395fd7f26SEd Maste 			p = dump_unknown_tag(tag, p, pe);
40442c23cb7cSEd Maste 			continue;
40452c23cb7cSEd Maste 		}
40462c23cb7cSEd Maste 		if (desc)
40472c23cb7cSEd Maste 			continue;
40482c23cb7cSEd Maste 
40492c23cb7cSEd Maste 		switch (tag) {
40502c23cb7cSEd Maste 		case 4:		/* Tag_CPU_raw_name */
40512c23cb7cSEd Maste 		case 5:		/* Tag_CPU_name */
40522c23cb7cSEd Maste 		case 67:	/* Tag_conformance */
40532c23cb7cSEd Maste 			printf("%s\n", (char *) p);
40542c23cb7cSEd Maste 			p += strlen((char *) p) + 1;
40552c23cb7cSEd Maste 			break;
40562c23cb7cSEd Maste 		case 32:	/* Tag_compatibility */
405795fd7f26SEd Maste 			p = dump_compatibility_tag(p, pe);
40582c23cb7cSEd Maste 			break;
40592c23cb7cSEd Maste 		case 64:	/* Tag_nodefaults */
40602c23cb7cSEd Maste 			/* ignored, written as 0. */
406195fd7f26SEd Maste 			(void) _decode_uleb128(&p, pe);
40622c23cb7cSEd Maste 			printf("True\n");
40632c23cb7cSEd Maste 			break;
40642c23cb7cSEd Maste 		case 65:	/* Tag_also_compatible_with */
406595fd7f26SEd Maste 			val = _decode_uleb128(&p, pe);
40662c23cb7cSEd Maste 			/* Must be Tag_CPU_arch */
40672c23cb7cSEd Maste 			if (val != 6) {
40682c23cb7cSEd Maste 				printf("unknown\n");
40692c23cb7cSEd Maste 				break;
40702c23cb7cSEd Maste 			}
407195fd7f26SEd Maste 			val = _decode_uleb128(&p, pe);
40722c23cb7cSEd Maste 			printf("%s\n", aeabi_cpu_arch(val));
40732c23cb7cSEd Maste 			/* Skip NUL terminator. */
40742c23cb7cSEd Maste 			p++;
40752c23cb7cSEd Maste 			break;
40762c23cb7cSEd Maste 		default:
40772c23cb7cSEd Maste 			putchar('\n');
40782c23cb7cSEd Maste 			break;
40792c23cb7cSEd Maste 		}
40802c23cb7cSEd Maste 	}
40812c23cb7cSEd Maste }
40822c23cb7cSEd Maste 
40832c23cb7cSEd Maste #ifndef	Tag_GNU_MIPS_ABI_FP
40842c23cb7cSEd Maste #define	Tag_GNU_MIPS_ABI_FP	4
40852c23cb7cSEd Maste #endif
40862c23cb7cSEd Maste 
40872c23cb7cSEd Maste static void
40882c23cb7cSEd Maste dump_mips_attributes(struct readelf *re, uint8_t *p, uint8_t *pe)
40892c23cb7cSEd Maste {
40902c23cb7cSEd Maste 	uint64_t tag, val;
40912c23cb7cSEd Maste 
40922c23cb7cSEd Maste 	(void) re;
40932c23cb7cSEd Maste 
40942c23cb7cSEd Maste 	while (p < pe) {
409595fd7f26SEd Maste 		tag = _decode_uleb128(&p, pe);
40962c23cb7cSEd Maste 		switch (tag) {
40972c23cb7cSEd Maste 		case Tag_GNU_MIPS_ABI_FP:
409895fd7f26SEd Maste 			val = _decode_uleb128(&p, pe);
40992c23cb7cSEd Maste 			printf("  Tag_GNU_MIPS_ABI_FP: %s\n", mips_abi_fp(val));
41002c23cb7cSEd Maste 			break;
41012c23cb7cSEd Maste 		case 32:	/* Tag_compatibility */
410295fd7f26SEd Maste 			p = dump_compatibility_tag(p, pe);
41032c23cb7cSEd Maste 			break;
41042c23cb7cSEd Maste 		default:
410595fd7f26SEd Maste 			p = dump_unknown_tag(tag, p, pe);
41062c23cb7cSEd Maste 			break;
41072c23cb7cSEd Maste 		}
41082c23cb7cSEd Maste 	}
41092c23cb7cSEd Maste }
41102c23cb7cSEd Maste 
41112c23cb7cSEd Maste #ifndef Tag_GNU_Power_ABI_FP
41122c23cb7cSEd Maste #define	Tag_GNU_Power_ABI_FP	4
41132c23cb7cSEd Maste #endif
41142c23cb7cSEd Maste 
41152c23cb7cSEd Maste #ifndef Tag_GNU_Power_ABI_Vector
41162c23cb7cSEd Maste #define	Tag_GNU_Power_ABI_Vector	8
41172c23cb7cSEd Maste #endif
41182c23cb7cSEd Maste 
41192c23cb7cSEd Maste static void
41202c23cb7cSEd Maste dump_ppc_attributes(uint8_t *p, uint8_t *pe)
41212c23cb7cSEd Maste {
41222c23cb7cSEd Maste 	uint64_t tag, val;
41232c23cb7cSEd Maste 
41242c23cb7cSEd Maste 	while (p < pe) {
412595fd7f26SEd Maste 		tag = _decode_uleb128(&p, pe);
41262c23cb7cSEd Maste 		switch (tag) {
41272c23cb7cSEd Maste 		case Tag_GNU_Power_ABI_FP:
412895fd7f26SEd Maste 			val = _decode_uleb128(&p, pe);
41292c23cb7cSEd Maste 			printf("  Tag_GNU_Power_ABI_FP: %s\n", ppc_abi_fp(val));
41302c23cb7cSEd Maste 			break;
41312c23cb7cSEd Maste 		case Tag_GNU_Power_ABI_Vector:
413295fd7f26SEd Maste 			val = _decode_uleb128(&p, pe);
41332c23cb7cSEd Maste 			printf("  Tag_GNU_Power_ABI_Vector: %s\n",
41342c23cb7cSEd Maste 			    ppc_abi_vector(val));
41352c23cb7cSEd Maste 			break;
41362c23cb7cSEd Maste 		case 32:	/* Tag_compatibility */
413795fd7f26SEd Maste 			p = dump_compatibility_tag(p, pe);
41382c23cb7cSEd Maste 			break;
41392c23cb7cSEd Maste 		default:
414095fd7f26SEd Maste 			p = dump_unknown_tag(tag, p, pe);
41412c23cb7cSEd Maste 			break;
41422c23cb7cSEd Maste 		}
41432c23cb7cSEd Maste 	}
41442c23cb7cSEd Maste }
41452c23cb7cSEd Maste 
41462c23cb7cSEd Maste static void
41472c23cb7cSEd Maste dump_attributes(struct readelf *re)
41482c23cb7cSEd Maste {
41492c23cb7cSEd Maste 	struct section *s;
41502c23cb7cSEd Maste 	Elf_Data *d;
415195fd7f26SEd Maste 	uint8_t *p, *pe, *sp;
41522c23cb7cSEd Maste 	size_t len, seclen, nlen, sublen;
41532c23cb7cSEd Maste 	uint64_t val;
41542c23cb7cSEd Maste 	int tag, i, elferr;
41552c23cb7cSEd Maste 
41562c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
41572c23cb7cSEd Maste 		s = &re->sl[i];
41582c23cb7cSEd Maste 		if (s->type != SHT_GNU_ATTRIBUTES &&
41592c23cb7cSEd Maste 		    (re->ehdr.e_machine != EM_ARM || s->type != SHT_LOPROC + 3))
41602c23cb7cSEd Maste 			continue;
41612c23cb7cSEd Maste 		(void) elf_errno();
41622c23cb7cSEd Maste 		if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
41632c23cb7cSEd Maste 			elferr = elf_errno();
41642c23cb7cSEd Maste 			if (elferr != 0)
41652c23cb7cSEd Maste 				warnx("elf_rawdata failed: %s",
41662c23cb7cSEd Maste 				    elf_errmsg(elferr));
41672c23cb7cSEd Maste 			continue;
41682c23cb7cSEd Maste 		}
41692c23cb7cSEd Maste 		if (d->d_size <= 0)
41702c23cb7cSEd Maste 			continue;
41712c23cb7cSEd Maste 		p = d->d_buf;
417295fd7f26SEd Maste 		pe = p + d->d_size;
41732c23cb7cSEd Maste 		if (*p != 'A') {
41742c23cb7cSEd Maste 			printf("Unknown Attribute Section Format: %c\n",
41752c23cb7cSEd Maste 			    (char) *p);
41762c23cb7cSEd Maste 			continue;
41772c23cb7cSEd Maste 		}
41782c23cb7cSEd Maste 		len = d->d_size - 1;
41792c23cb7cSEd Maste 		p++;
41802c23cb7cSEd Maste 		while (len > 0) {
418171a0c925SEd Maste 			if (len < 4) {
418271a0c925SEd Maste 				warnx("truncated attribute section length");
418395fd7f26SEd Maste 				return;
418471a0c925SEd Maste 			}
41852c23cb7cSEd Maste 			seclen = re->dw_decode(&p, 4);
41862c23cb7cSEd Maste 			if (seclen > len) {
41872c23cb7cSEd Maste 				warnx("invalid attribute section length");
418895fd7f26SEd Maste 				return;
41892c23cb7cSEd Maste 			}
41902c23cb7cSEd Maste 			len -= seclen;
41912c23cb7cSEd Maste 			nlen = strlen((char *) p) + 1;
419271a0c925SEd Maste 			if (nlen + 4 > seclen) {
419371a0c925SEd Maste 				warnx("invalid attribute section name");
419495fd7f26SEd Maste 				return;
419571a0c925SEd Maste 			}
419671a0c925SEd Maste 			printf("Attribute Section: %s\n", (char *) p);
41972c23cb7cSEd Maste 			p += nlen;
41982c23cb7cSEd Maste 			seclen -= nlen + 4;
41992c23cb7cSEd Maste 			while (seclen > 0) {
42002c23cb7cSEd Maste 				sp = p;
42012c23cb7cSEd Maste 				tag = *p++;
42022c23cb7cSEd Maste 				sublen = re->dw_decode(&p, 4);
42032c23cb7cSEd Maste 				if (sublen > seclen) {
42042c23cb7cSEd Maste 					warnx("invalid attribute sub-section"
42052c23cb7cSEd Maste 					    " length");
420695fd7f26SEd Maste 					return;
42072c23cb7cSEd Maste 				}
42082c23cb7cSEd Maste 				seclen -= sublen;
42092c23cb7cSEd Maste 				printf("%s", top_tag(tag));
42102c23cb7cSEd Maste 				if (tag == 2 || tag == 3) {
42112c23cb7cSEd Maste 					putchar(':');
42122c23cb7cSEd Maste 					for (;;) {
421395fd7f26SEd Maste 						val = _decode_uleb128(&p, pe);
42142c23cb7cSEd Maste 						if (val == 0)
42152c23cb7cSEd Maste 							break;
42162c23cb7cSEd Maste 						printf(" %ju", (uintmax_t) val);
42172c23cb7cSEd Maste 					}
42182c23cb7cSEd Maste 				}
42192c23cb7cSEd Maste 				putchar('\n');
42202c23cb7cSEd Maste 				if (re->ehdr.e_machine == EM_ARM &&
42212c23cb7cSEd Maste 				    s->type == SHT_LOPROC + 3)
42222c23cb7cSEd Maste 					dump_arm_attributes(re, p, sp + sublen);
42232c23cb7cSEd Maste 				else if (re->ehdr.e_machine == EM_MIPS ||
42242c23cb7cSEd Maste 				    re->ehdr.e_machine == EM_MIPS_RS3_LE)
42252c23cb7cSEd Maste 					dump_mips_attributes(re, p,
42262c23cb7cSEd Maste 					    sp + sublen);
42272c23cb7cSEd Maste 				else if (re->ehdr.e_machine == EM_PPC)
42282c23cb7cSEd Maste 					dump_ppc_attributes(p, sp + sublen);
42292c23cb7cSEd Maste 				p = sp + sublen;
42302c23cb7cSEd Maste 			}
42312c23cb7cSEd Maste 		}
42322c23cb7cSEd Maste 	}
42332c23cb7cSEd Maste }
42342c23cb7cSEd Maste 
42352c23cb7cSEd Maste static void
42362c23cb7cSEd Maste dump_mips_specific_info(struct readelf *re)
42372c23cb7cSEd Maste {
42382c23cb7cSEd Maste 	struct section *s;
4239bee2765cSEd Maste 	int i;
42402c23cb7cSEd Maste 
42412c23cb7cSEd Maste 	s = NULL;
42422c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
42432c23cb7cSEd Maste 		s = &re->sl[i];
42442c23cb7cSEd Maste 		if (s->name != NULL && (!strcmp(s->name, ".MIPS.options") ||
42452c23cb7cSEd Maste 		    (s->type == SHT_MIPS_OPTIONS))) {
42462c23cb7cSEd Maste 			dump_mips_options(re, s);
42472c23cb7cSEd Maste 		}
42482c23cb7cSEd Maste 	}
42492c23cb7cSEd Maste 
4250e9f3f88aSEd Maste 	if (s->name != NULL && (!strcmp(s->name, ".MIPS.abiflags") ||
4251e9f3f88aSEd Maste 	    (s->type == SHT_MIPS_ABIFLAGS)))
4252e9f3f88aSEd Maste 		dump_mips_abiflags(re, s);
4253e9f3f88aSEd Maste 
42542c23cb7cSEd Maste 	/*
4255bee2765cSEd Maste 	 * Dump .reginfo if present (although it will be ignored by an OS if a
4256bee2765cSEd Maste 	 * .MIPS.options section is present, according to SGI mips64 spec).
42572c23cb7cSEd Maste 	 */
42582c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
42592c23cb7cSEd Maste 		s = &re->sl[i];
42602c23cb7cSEd Maste 		if (s->name != NULL && (!strcmp(s->name, ".reginfo") ||
42612c23cb7cSEd Maste 		    (s->type == SHT_MIPS_REGINFO)))
42622c23cb7cSEd Maste 			dump_mips_reginfo(re, s);
42632c23cb7cSEd Maste 	}
42642c23cb7cSEd Maste }
42652c23cb7cSEd Maste 
42662c23cb7cSEd Maste static void
4267e9f3f88aSEd Maste dump_mips_abiflags(struct readelf *re, struct section *s)
4268e9f3f88aSEd Maste {
4269e9f3f88aSEd Maste 	Elf_Data *d;
4270e9f3f88aSEd Maste 	uint8_t *p;
4271e9f3f88aSEd Maste 	int elferr;
4272e9f3f88aSEd Maste 	uint32_t isa_ext, ases, flags1, flags2;
4273e9f3f88aSEd Maste 	uint16_t version;
4274e9f3f88aSEd Maste 	uint8_t isa_level, isa_rev, gpr_size, cpr1_size, cpr2_size, fp_abi;
4275e9f3f88aSEd Maste 
4276e9f3f88aSEd Maste 	if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
4277e9f3f88aSEd Maste 		elferr = elf_errno();
4278e9f3f88aSEd Maste 		if (elferr != 0)
4279e9f3f88aSEd Maste 			warnx("elf_rawdata failed: %s",
4280e9f3f88aSEd Maste 			    elf_errmsg(elferr));
4281e9f3f88aSEd Maste 		return;
4282e9f3f88aSEd Maste 	}
4283e9f3f88aSEd Maste 	if (d->d_size != 24) {
4284e9f3f88aSEd Maste 		warnx("invalid MIPS abiflags section size");
4285e9f3f88aSEd Maste 		return;
4286e9f3f88aSEd Maste 	}
4287e9f3f88aSEd Maste 
4288e9f3f88aSEd Maste 	p = d->d_buf;
4289e9f3f88aSEd Maste 	version = re->dw_decode(&p, 2);
4290e9f3f88aSEd Maste 	printf("MIPS ABI Flags Version: %u", version);
4291e9f3f88aSEd Maste 	if (version != 0) {
4292e9f3f88aSEd Maste 		printf(" (unknown)\n\n");
4293e9f3f88aSEd Maste 		return;
4294e9f3f88aSEd Maste 	}
4295e9f3f88aSEd Maste 	printf("\n\n");
4296e9f3f88aSEd Maste 
4297e9f3f88aSEd Maste 	isa_level = re->dw_decode(&p, 1);
4298e9f3f88aSEd Maste 	isa_rev = re->dw_decode(&p, 1);
4299e9f3f88aSEd Maste 	gpr_size = re->dw_decode(&p, 1);
4300e9f3f88aSEd Maste 	cpr1_size = re->dw_decode(&p, 1);
4301e9f3f88aSEd Maste 	cpr2_size = re->dw_decode(&p, 1);
4302e9f3f88aSEd Maste 	fp_abi = re->dw_decode(&p, 1);
4303e9f3f88aSEd Maste 	isa_ext = re->dw_decode(&p, 4);
4304e9f3f88aSEd Maste 	ases = re->dw_decode(&p, 4);
4305e9f3f88aSEd Maste 	flags1 = re->dw_decode(&p, 4);
4306e9f3f88aSEd Maste 	flags2 = re->dw_decode(&p, 4);
4307e9f3f88aSEd Maste 
4308e9f3f88aSEd Maste 	printf("ISA: ");
4309e9f3f88aSEd Maste 	if (isa_rev <= 1)
4310e9f3f88aSEd Maste 		printf("MIPS%u\n", isa_level);
4311e9f3f88aSEd Maste 	else
4312e9f3f88aSEd Maste 		printf("MIPS%ur%u\n", isa_level, isa_rev);
4313e9f3f88aSEd Maste 	printf("GPR size: %d\n", get_mips_register_size(gpr_size));
4314e9f3f88aSEd Maste 	printf("CPR1 size: %d\n", get_mips_register_size(cpr1_size));
4315e9f3f88aSEd Maste 	printf("CPR2 size: %d\n", get_mips_register_size(cpr2_size));
4316e9f3f88aSEd Maste 	printf("FP ABI: ");
4317e9f3f88aSEd Maste 	switch (fp_abi) {
4318e9f3f88aSEd Maste 	case 3:
4319e9f3f88aSEd Maste 		printf("Soft float");
4320e9f3f88aSEd Maste 		break;
4321e9f3f88aSEd Maste 	default:
4322e9f3f88aSEd Maste 		printf("%u", fp_abi);
4323e9f3f88aSEd Maste 		break;
4324e9f3f88aSEd Maste 	}
4325e9f3f88aSEd Maste 	printf("\nISA Extension: %u\n", isa_ext);
4326e9f3f88aSEd Maste 	printf("ASEs: %u\n", ases);
4327e9f3f88aSEd Maste 	printf("FLAGS 1: %08x\n", flags1);
4328e9f3f88aSEd Maste 	printf("FLAGS 2: %08x\n", flags2);
4329e9f3f88aSEd Maste }
4330e9f3f88aSEd Maste 
4331e9f3f88aSEd Maste static int
4332e9f3f88aSEd Maste get_mips_register_size(uint8_t flag)
4333e9f3f88aSEd Maste {
4334e9f3f88aSEd Maste 	switch (flag) {
4335e9f3f88aSEd Maste 	case 0: return 0;
4336e9f3f88aSEd Maste 	case 1: return 32;
4337e9f3f88aSEd Maste 	case 2: return 64;
4338e9f3f88aSEd Maste 	case 3: return 128;
4339e9f3f88aSEd Maste 	default: return -1;
4340e9f3f88aSEd Maste 	}
4341e9f3f88aSEd Maste }
4342e9f3f88aSEd Maste static void
43432c23cb7cSEd Maste dump_mips_reginfo(struct readelf *re, struct section *s)
43442c23cb7cSEd Maste {
43452c23cb7cSEd Maste 	Elf_Data *d;
434671edbbfdSEd Maste 	int elferr, len;
43472c23cb7cSEd Maste 
43482c23cb7cSEd Maste 	(void) elf_errno();
43492c23cb7cSEd Maste 	if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
43502c23cb7cSEd Maste 		elferr = elf_errno();
43512c23cb7cSEd Maste 		if (elferr != 0)
43522c23cb7cSEd Maste 			warnx("elf_rawdata failed: %s",
43532c23cb7cSEd Maste 			    elf_errmsg(elferr));
43542c23cb7cSEd Maste 		return;
43552c23cb7cSEd Maste 	}
43562c23cb7cSEd Maste 	if (d->d_size <= 0)
43572c23cb7cSEd Maste 		return;
435871edbbfdSEd Maste 	if (!get_ent_count(s, &len))
435971edbbfdSEd Maste 		return;
43602c23cb7cSEd Maste 
436171edbbfdSEd Maste 	printf("\nSection '%s' contains %d entries:\n", s->name, len);
43622c23cb7cSEd Maste 	dump_mips_odk_reginfo(re, d->d_buf, d->d_size);
43632c23cb7cSEd Maste }
43642c23cb7cSEd Maste 
43652c23cb7cSEd Maste static void
43662c23cb7cSEd Maste dump_mips_options(struct readelf *re, struct section *s)
43672c23cb7cSEd Maste {
43682c23cb7cSEd Maste 	Elf_Data *d;
43692c23cb7cSEd Maste 	uint32_t info;
43702c23cb7cSEd Maste 	uint16_t sndx;
43712c23cb7cSEd Maste 	uint8_t *p, *pe;
43722c23cb7cSEd Maste 	uint8_t kind, size;
43732c23cb7cSEd Maste 	int elferr;
43742c23cb7cSEd Maste 
43752c23cb7cSEd Maste 	(void) elf_errno();
43762c23cb7cSEd Maste 	if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
43772c23cb7cSEd Maste 		elferr = elf_errno();
43782c23cb7cSEd Maste 		if (elferr != 0)
43792c23cb7cSEd Maste 			warnx("elf_rawdata failed: %s",
43802c23cb7cSEd Maste 			    elf_errmsg(elferr));
43812c23cb7cSEd Maste 		return;
43822c23cb7cSEd Maste 	}
43832c23cb7cSEd Maste 	if (d->d_size == 0)
43842c23cb7cSEd Maste 		return;
43852c23cb7cSEd Maste 
43862c23cb7cSEd Maste 	printf("\nSection %s contains:\n", s->name);
43872c23cb7cSEd Maste 	p = d->d_buf;
43882c23cb7cSEd Maste 	pe = p + d->d_size;
43892c23cb7cSEd Maste 	while (p < pe) {
4390b00fe64fSEd Maste 		if (pe - p < 8) {
4391b00fe64fSEd Maste 			warnx("Truncated MIPS option header");
4392b00fe64fSEd Maste 			return;
4393b00fe64fSEd Maste 		}
43942c23cb7cSEd Maste 		kind = re->dw_decode(&p, 1);
43952c23cb7cSEd Maste 		size = re->dw_decode(&p, 1);
43962c23cb7cSEd Maste 		sndx = re->dw_decode(&p, 2);
43972c23cb7cSEd Maste 		info = re->dw_decode(&p, 4);
4398b00fe64fSEd Maste 		if (size < 8 || size - 8 > pe - p) {
4399b00fe64fSEd Maste 			warnx("Malformed MIPS option header");
4400b00fe64fSEd Maste 			return;
4401b00fe64fSEd Maste 		}
4402b00fe64fSEd Maste 		size -= 8;
44032c23cb7cSEd Maste 		switch (kind) {
44042c23cb7cSEd Maste 		case ODK_REGINFO:
4405b00fe64fSEd Maste 			dump_mips_odk_reginfo(re, p, size);
44062c23cb7cSEd Maste 			break;
44072c23cb7cSEd Maste 		case ODK_EXCEPTIONS:
44082c23cb7cSEd Maste 			printf(" EXCEPTIONS FPU_MIN: %#x\n",
44092c23cb7cSEd Maste 			    info & OEX_FPU_MIN);
44102c23cb7cSEd Maste 			printf("%11.11s FPU_MAX: %#x\n", "",
44112c23cb7cSEd Maste 			    info & OEX_FPU_MAX);
44122c23cb7cSEd Maste 			dump_mips_option_flags("", mips_exceptions_option,
44132c23cb7cSEd Maste 			    info);
44142c23cb7cSEd Maste 			break;
44152c23cb7cSEd Maste 		case ODK_PAD:
44162c23cb7cSEd Maste 			printf(" %-10.10s section: %ju\n", "OPAD",
44172c23cb7cSEd Maste 			    (uintmax_t) sndx);
44182c23cb7cSEd Maste 			dump_mips_option_flags("", mips_pad_option, info);
44192c23cb7cSEd Maste 			break;
44202c23cb7cSEd Maste 		case ODK_HWPATCH:
44212c23cb7cSEd Maste 			dump_mips_option_flags("HWPATCH", mips_hwpatch_option,
44222c23cb7cSEd Maste 			    info);
44232c23cb7cSEd Maste 			break;
44242c23cb7cSEd Maste 		case ODK_HWAND:
44252c23cb7cSEd Maste 			dump_mips_option_flags("HWAND", mips_hwa_option, info);
44262c23cb7cSEd Maste 			break;
44272c23cb7cSEd Maste 		case ODK_HWOR:
44282c23cb7cSEd Maste 			dump_mips_option_flags("HWOR", mips_hwo_option, info);
44292c23cb7cSEd Maste 			break;
44302c23cb7cSEd Maste 		case ODK_FILL:
44312c23cb7cSEd Maste 			printf(" %-10.10s %#jx\n", "FILL", (uintmax_t) info);
44322c23cb7cSEd Maste 			break;
44332c23cb7cSEd Maste 		case ODK_TAGS:
44342c23cb7cSEd Maste 			printf(" %-10.10s\n", "TAGS");
44352c23cb7cSEd Maste 			break;
44362c23cb7cSEd Maste 		case ODK_GP_GROUP:
44372c23cb7cSEd Maste 			printf(" %-10.10s GP group number: %#x\n", "GP_GROUP",
44382c23cb7cSEd Maste 			    info & 0xFFFF);
44392c23cb7cSEd Maste 			if (info & 0x10000)
44402c23cb7cSEd Maste 				printf(" %-10.10s GP group is "
44412c23cb7cSEd Maste 				    "self-contained\n", "");
44422c23cb7cSEd Maste 			break;
44432c23cb7cSEd Maste 		case ODK_IDENT:
44442c23cb7cSEd Maste 			printf(" %-10.10s default GP group number: %#x\n",
44452c23cb7cSEd Maste 			    "IDENT", info & 0xFFFF);
44462c23cb7cSEd Maste 			if (info & 0x10000)
44472c23cb7cSEd Maste 				printf(" %-10.10s default GP group is "
44482c23cb7cSEd Maste 				    "self-contained\n", "");
44492c23cb7cSEd Maste 			break;
44502c23cb7cSEd Maste 		case ODK_PAGESIZE:
44512c23cb7cSEd Maste 			printf(" %-10.10s\n", "PAGESIZE");
44522c23cb7cSEd Maste 			break;
44532c23cb7cSEd Maste 		default:
44542c23cb7cSEd Maste 			break;
44552c23cb7cSEd Maste 		}
4456b00fe64fSEd Maste 		p += size;
44572c23cb7cSEd Maste 	}
44582c23cb7cSEd Maste }
44592c23cb7cSEd Maste 
44602c23cb7cSEd Maste static void
44612c23cb7cSEd Maste dump_mips_option_flags(const char *name, struct mips_option *opt, uint64_t info)
44622c23cb7cSEd Maste {
44632c23cb7cSEd Maste 	int first;
44642c23cb7cSEd Maste 
44652c23cb7cSEd Maste 	first = 1;
44662c23cb7cSEd Maste 	for (; opt->desc != NULL; opt++) {
44672c23cb7cSEd Maste 		if (info & opt->flag) {
44682c23cb7cSEd Maste 			printf(" %-10.10s %s\n", first ? name : "",
44692c23cb7cSEd Maste 			    opt->desc);
44702c23cb7cSEd Maste 			first = 0;
44712c23cb7cSEd Maste 		}
44722c23cb7cSEd Maste 	}
44732c23cb7cSEd Maste }
44742c23cb7cSEd Maste 
44752c23cb7cSEd Maste static void
44762c23cb7cSEd Maste dump_mips_odk_reginfo(struct readelf *re, uint8_t *p, size_t sz)
44772c23cb7cSEd Maste {
44782c23cb7cSEd Maste 	uint32_t ri_gprmask;
44792c23cb7cSEd Maste 	uint32_t ri_cprmask[4];
44802c23cb7cSEd Maste 	uint64_t ri_gp_value;
44812c23cb7cSEd Maste 	uint8_t *pe;
44822c23cb7cSEd Maste 	int i;
44832c23cb7cSEd Maste 
44842c23cb7cSEd Maste 	pe = p + sz;
44852c23cb7cSEd Maste 	while (p < pe) {
44862c23cb7cSEd Maste 		ri_gprmask = re->dw_decode(&p, 4);
44872c23cb7cSEd Maste 		/* Skip ri_pad padding field for mips64. */
44882c23cb7cSEd Maste 		if (re->ec == ELFCLASS64)
44892c23cb7cSEd Maste 			re->dw_decode(&p, 4);
44902c23cb7cSEd Maste 		for (i = 0; i < 4; i++)
44912c23cb7cSEd Maste 			ri_cprmask[i] = re->dw_decode(&p, 4);
44922c23cb7cSEd Maste 		if (re->ec == ELFCLASS32)
44932c23cb7cSEd Maste 			ri_gp_value = re->dw_decode(&p, 4);
44942c23cb7cSEd Maste 		else
44952c23cb7cSEd Maste 			ri_gp_value = re->dw_decode(&p, 8);
44962c23cb7cSEd Maste 		printf(" %s    ", option_kind(ODK_REGINFO));
44972c23cb7cSEd Maste 		printf("ri_gprmask:    0x%08jx\n", (uintmax_t) ri_gprmask);
44982c23cb7cSEd Maste 		for (i = 0; i < 4; i++)
44992c23cb7cSEd Maste 			printf("%11.11s ri_cprmask[%d]: 0x%08jx\n", "", i,
45002c23cb7cSEd Maste 			    (uintmax_t) ri_cprmask[i]);
45012c23cb7cSEd Maste 		printf("%12.12s", "");
45022c23cb7cSEd Maste 		printf("ri_gp_value:   %#jx\n", (uintmax_t) ri_gp_value);
45032c23cb7cSEd Maste 	}
45042c23cb7cSEd Maste }
45052c23cb7cSEd Maste 
45062c23cb7cSEd Maste static void
45072c23cb7cSEd Maste dump_arch_specific_info(struct readelf *re)
45082c23cb7cSEd Maste {
45092c23cb7cSEd Maste 
45102c23cb7cSEd Maste 	dump_liblist(re);
45112c23cb7cSEd Maste 	dump_attributes(re);
45122c23cb7cSEd Maste 
45132c23cb7cSEd Maste 	switch (re->ehdr.e_machine) {
45142c23cb7cSEd Maste 	case EM_MIPS:
45152c23cb7cSEd Maste 	case EM_MIPS_RS3_LE:
45162c23cb7cSEd Maste 		dump_mips_specific_info(re);
45172c23cb7cSEd Maste 	default:
45182c23cb7cSEd Maste 		break;
45192c23cb7cSEd Maste 	}
45202c23cb7cSEd Maste }
45212c23cb7cSEd Maste 
4522cf781b2eSEd Maste static const char *
4523cf781b2eSEd Maste dwarf_regname(struct readelf *re, unsigned int num)
4524cf781b2eSEd Maste {
4525cf781b2eSEd Maste 	static char rx[32];
4526cf781b2eSEd Maste 	const char *rn;
4527cf781b2eSEd Maste 
4528cf781b2eSEd Maste 	if ((rn = dwarf_reg(re->ehdr.e_machine, num)) != NULL)
4529cf781b2eSEd Maste 		return (rn);
4530cf781b2eSEd Maste 
4531cf781b2eSEd Maste 	snprintf(rx, sizeof(rx), "r%u", num);
4532cf781b2eSEd Maste 
4533cf781b2eSEd Maste 	return (rx);
4534cf781b2eSEd Maste }
4535cf781b2eSEd Maste 
45362c23cb7cSEd Maste static void
45372c23cb7cSEd Maste dump_dwarf_line(struct readelf *re)
45382c23cb7cSEd Maste {
45392c23cb7cSEd Maste 	struct section *s;
45402c23cb7cSEd Maste 	Dwarf_Die die;
45412c23cb7cSEd Maste 	Dwarf_Error de;
45422c23cb7cSEd Maste 	Dwarf_Half tag, version, pointer_size;
45432c23cb7cSEd Maste 	Dwarf_Unsigned offset, endoff, length, hdrlen, dirndx, mtime, fsize;
45442c23cb7cSEd Maste 	Dwarf_Small minlen, defstmt, lrange, opbase, oplen;
45452c23cb7cSEd Maste 	Elf_Data *d;
45462c23cb7cSEd Maste 	char *pn;
45472c23cb7cSEd Maste 	uint64_t address, file, line, column, isa, opsize, udelta;
45482c23cb7cSEd Maste 	int64_t sdelta;
45492c23cb7cSEd Maste 	uint8_t *p, *pe;
45502c23cb7cSEd Maste 	int8_t lbase;
4551cf781b2eSEd Maste 	int i, is_stmt, dwarf_size, elferr, ret;
45522c23cb7cSEd Maste 
45532c23cb7cSEd Maste 	printf("\nDump of debug contents of section .debug_line:\n");
45542c23cb7cSEd Maste 
45552c23cb7cSEd Maste 	s = NULL;
45562c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
45572c23cb7cSEd Maste 		s = &re->sl[i];
45582c23cb7cSEd Maste 		if (s->name != NULL && !strcmp(s->name, ".debug_line"))
45592c23cb7cSEd Maste 			break;
45602c23cb7cSEd Maste 	}
45612c23cb7cSEd Maste 	if ((size_t) i >= re->shnum)
45622c23cb7cSEd Maste 		return;
45632c23cb7cSEd Maste 
45642c23cb7cSEd Maste 	(void) elf_errno();
45652c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
45662c23cb7cSEd Maste 		elferr = elf_errno();
45672c23cb7cSEd Maste 		if (elferr != 0)
45682c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
45692c23cb7cSEd Maste 		return;
45702c23cb7cSEd Maste 	}
45712c23cb7cSEd Maste 	if (d->d_size <= 0)
45722c23cb7cSEd Maste 		return;
45732c23cb7cSEd Maste 
45742c23cb7cSEd Maste 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
45752c23cb7cSEd Maste 	    NULL, &de)) ==  DW_DLV_OK) {
45762c23cb7cSEd Maste 		die = NULL;
45772c23cb7cSEd Maste 		while (dwarf_siblingof(re->dbg, die, &die, &de) == DW_DLV_OK) {
45782c23cb7cSEd Maste 			if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
45792c23cb7cSEd Maste 				warnx("dwarf_tag failed: %s",
45802c23cb7cSEd Maste 				    dwarf_errmsg(de));
45812c23cb7cSEd Maste 				return;
45822c23cb7cSEd Maste 			}
45832c23cb7cSEd Maste 			/* XXX: What about DW_TAG_partial_unit? */
45842c23cb7cSEd Maste 			if (tag == DW_TAG_compile_unit)
45852c23cb7cSEd Maste 				break;
45862c23cb7cSEd Maste 		}
45872c23cb7cSEd Maste 		if (die == NULL) {
45882c23cb7cSEd Maste 			warnx("could not find DW_TAG_compile_unit die");
45892c23cb7cSEd Maste 			return;
45902c23cb7cSEd Maste 		}
45912c23cb7cSEd Maste 		if (dwarf_attrval_unsigned(die, DW_AT_stmt_list, &offset,
45922c23cb7cSEd Maste 		    &de) != DW_DLV_OK)
45932c23cb7cSEd Maste 			continue;
45942c23cb7cSEd Maste 
45952c23cb7cSEd Maste 		length = re->dw_read(d, &offset, 4);
45962c23cb7cSEd Maste 		if (length == 0xffffffff) {
45972c23cb7cSEd Maste 			dwarf_size = 8;
45982c23cb7cSEd Maste 			length = re->dw_read(d, &offset, 8);
45992c23cb7cSEd Maste 		} else
46002c23cb7cSEd Maste 			dwarf_size = 4;
46012c23cb7cSEd Maste 
46022c23cb7cSEd Maste 		if (length > d->d_size - offset) {
46032c23cb7cSEd Maste 			warnx("invalid .dwarf_line section");
46042c23cb7cSEd Maste 			continue;
46052c23cb7cSEd Maste 		}
46062c23cb7cSEd Maste 
46072c23cb7cSEd Maste 		endoff = offset + length;
46088f32e46dSKai Wang 		pe = (uint8_t *) d->d_buf + endoff;
46092c23cb7cSEd Maste 		version = re->dw_read(d, &offset, 2);
46102c23cb7cSEd Maste 		hdrlen = re->dw_read(d, &offset, dwarf_size);
46112c23cb7cSEd Maste 		minlen = re->dw_read(d, &offset, 1);
46122c23cb7cSEd Maste 		defstmt = re->dw_read(d, &offset, 1);
46132c23cb7cSEd Maste 		lbase = re->dw_read(d, &offset, 1);
46142c23cb7cSEd Maste 		lrange = re->dw_read(d, &offset, 1);
46152c23cb7cSEd Maste 		opbase = re->dw_read(d, &offset, 1);
46162c23cb7cSEd Maste 
46172c23cb7cSEd Maste 		printf("\n");
46182c23cb7cSEd Maste 		printf("  Length:\t\t\t%ju\n", (uintmax_t) length);
46192c23cb7cSEd Maste 		printf("  DWARF version:\t\t%u\n", version);
46202c23cb7cSEd Maste 		printf("  Prologue Length:\t\t%ju\n", (uintmax_t) hdrlen);
46212c23cb7cSEd Maste 		printf("  Minimum Instruction Length:\t%u\n", minlen);
46222c23cb7cSEd Maste 		printf("  Initial value of 'is_stmt':\t%u\n", defstmt);
46232c23cb7cSEd Maste 		printf("  Line Base:\t\t\t%d\n", lbase);
46242c23cb7cSEd Maste 		printf("  Line Range:\t\t\t%u\n", lrange);
46252c23cb7cSEd Maste 		printf("  Opcode Base:\t\t\t%u\n", opbase);
46262c23cb7cSEd Maste 		(void) dwarf_get_address_size(re->dbg, &pointer_size, &de);
46272c23cb7cSEd Maste 		printf("  (Pointer size:\t\t%u)\n", pointer_size);
46282c23cb7cSEd Maste 
46292c23cb7cSEd Maste 		printf("\n");
46302c23cb7cSEd Maste 		printf(" Opcodes:\n");
46312c23cb7cSEd Maste 		for (i = 1; i < opbase; i++) {
46322c23cb7cSEd Maste 			oplen = re->dw_read(d, &offset, 1);
46332c23cb7cSEd Maste 			printf("  Opcode %d has %u args\n", i, oplen);
46342c23cb7cSEd Maste 		}
46352c23cb7cSEd Maste 
46362c23cb7cSEd Maste 		printf("\n");
46372c23cb7cSEd Maste 		printf(" The Directory Table:\n");
46382c23cb7cSEd Maste 		p = (uint8_t *) d->d_buf + offset;
46392c23cb7cSEd Maste 		while (*p != '\0') {
46402c23cb7cSEd Maste 			printf("  %s\n", (char *) p);
46412c23cb7cSEd Maste 			p += strlen((char *) p) + 1;
46422c23cb7cSEd Maste 		}
46432c23cb7cSEd Maste 
46442c23cb7cSEd Maste 		p++;
46452c23cb7cSEd Maste 		printf("\n");
46462c23cb7cSEd Maste 		printf(" The File Name Table:\n");
46472c23cb7cSEd Maste 		printf("  Entry\tDir\tTime\tSize\tName\n");
46482c23cb7cSEd Maste 		i = 0;
46492c23cb7cSEd Maste 		while (*p != '\0') {
46502c23cb7cSEd Maste 			i++;
46512c23cb7cSEd Maste 			pn = (char *) p;
46522c23cb7cSEd Maste 			p += strlen(pn) + 1;
465395fd7f26SEd Maste 			dirndx = _decode_uleb128(&p, pe);
465495fd7f26SEd Maste 			mtime = _decode_uleb128(&p, pe);
465595fd7f26SEd Maste 			fsize = _decode_uleb128(&p, pe);
46562c23cb7cSEd Maste 			printf("  %d\t%ju\t%ju\t%ju\t%s\n", i,
46572c23cb7cSEd Maste 			    (uintmax_t) dirndx, (uintmax_t) mtime,
46582c23cb7cSEd Maste 			    (uintmax_t) fsize, pn);
46592c23cb7cSEd Maste 		}
46602c23cb7cSEd Maste 
46612c23cb7cSEd Maste #define	RESET_REGISTERS						\
46622c23cb7cSEd Maste 	do {							\
46632c23cb7cSEd Maste 		address	       = 0;				\
46642c23cb7cSEd Maste 		file	       = 1;				\
46652c23cb7cSEd Maste 		line	       = 1;				\
46662c23cb7cSEd Maste 		column	       = 0;				\
46672c23cb7cSEd Maste 		is_stmt	       = defstmt;			\
46682c23cb7cSEd Maste 	} while(0)
46692c23cb7cSEd Maste 
46702c23cb7cSEd Maste #define	LINE(x) (lbase + (((x) - opbase) % lrange))
46712c23cb7cSEd Maste #define	ADDRESS(x) ((((x) - opbase) / lrange) * minlen)
46722c23cb7cSEd Maste 
46732c23cb7cSEd Maste 		p++;
46742c23cb7cSEd Maste 		printf("\n");
46752c23cb7cSEd Maste 		printf(" Line Number Statements:\n");
46762c23cb7cSEd Maste 
46772c23cb7cSEd Maste 		RESET_REGISTERS;
46782c23cb7cSEd Maste 
46792c23cb7cSEd Maste 		while (p < pe) {
46802c23cb7cSEd Maste 
46812c23cb7cSEd Maste 			if (*p == 0) {
46822c23cb7cSEd Maste 				/*
46832c23cb7cSEd Maste 				 * Extended Opcodes.
46842c23cb7cSEd Maste 				 */
46852c23cb7cSEd Maste 				p++;
468695fd7f26SEd Maste 				opsize = _decode_uleb128(&p, pe);
46872c23cb7cSEd Maste 				printf("  Extended opcode %u: ", *p);
46882c23cb7cSEd Maste 				switch (*p) {
46892c23cb7cSEd Maste 				case DW_LNE_end_sequence:
46902c23cb7cSEd Maste 					p++;
46912c23cb7cSEd Maste 					RESET_REGISTERS;
46922c23cb7cSEd Maste 					printf("End of Sequence\n");
46932c23cb7cSEd Maste 					break;
46942c23cb7cSEd Maste 				case DW_LNE_set_address:
46952c23cb7cSEd Maste 					p++;
46962c23cb7cSEd Maste 					address = re->dw_decode(&p,
46972c23cb7cSEd Maste 					    pointer_size);
46982c23cb7cSEd Maste 					printf("set Address to %#jx\n",
46992c23cb7cSEd Maste 					    (uintmax_t) address);
47002c23cb7cSEd Maste 					break;
47012c23cb7cSEd Maste 				case DW_LNE_define_file:
47022c23cb7cSEd Maste 					p++;
47032c23cb7cSEd Maste 					pn = (char *) p;
47042c23cb7cSEd Maste 					p += strlen(pn) + 1;
470595fd7f26SEd Maste 					dirndx = _decode_uleb128(&p, pe);
470695fd7f26SEd Maste 					mtime = _decode_uleb128(&p, pe);
470795fd7f26SEd Maste 					fsize = _decode_uleb128(&p, pe);
47082c23cb7cSEd Maste 					printf("define new file: %s\n", pn);
47092c23cb7cSEd Maste 					break;
47102c23cb7cSEd Maste 				default:
47112c23cb7cSEd Maste 					/* Unrecognized extened opcodes. */
47122c23cb7cSEd Maste 					p += opsize;
47132c23cb7cSEd Maste 					printf("unknown opcode\n");
47142c23cb7cSEd Maste 				}
47152c23cb7cSEd Maste 			} else if (*p > 0 && *p < opbase) {
47162c23cb7cSEd Maste 				/*
47172c23cb7cSEd Maste 				 * Standard Opcodes.
47182c23cb7cSEd Maste 				 */
47192c23cb7cSEd Maste 				switch(*p++) {
47202c23cb7cSEd Maste 				case DW_LNS_copy:
47212c23cb7cSEd Maste 					printf("  Copy\n");
47222c23cb7cSEd Maste 					break;
47232c23cb7cSEd Maste 				case DW_LNS_advance_pc:
472495fd7f26SEd Maste 					udelta = _decode_uleb128(&p, pe) *
47252c23cb7cSEd Maste 					    minlen;
47262c23cb7cSEd Maste 					address += udelta;
47272c23cb7cSEd Maste 					printf("  Advance PC by %ju to %#jx\n",
47282c23cb7cSEd Maste 					    (uintmax_t) udelta,
47292c23cb7cSEd Maste 					    (uintmax_t) address);
47302c23cb7cSEd Maste 					break;
47312c23cb7cSEd Maste 				case DW_LNS_advance_line:
473295fd7f26SEd Maste 					sdelta = _decode_sleb128(&p, pe);
47332c23cb7cSEd Maste 					line += sdelta;
47342c23cb7cSEd Maste 					printf("  Advance Line by %jd to %ju\n",
47352c23cb7cSEd Maste 					    (intmax_t) sdelta,
47362c23cb7cSEd Maste 					    (uintmax_t) line);
47372c23cb7cSEd Maste 					break;
47382c23cb7cSEd Maste 				case DW_LNS_set_file:
473995fd7f26SEd Maste 					file = _decode_uleb128(&p, pe);
47402c23cb7cSEd Maste 					printf("  Set File to %ju\n",
47412c23cb7cSEd Maste 					    (uintmax_t) file);
47422c23cb7cSEd Maste 					break;
47432c23cb7cSEd Maste 				case DW_LNS_set_column:
474495fd7f26SEd Maste 					column = _decode_uleb128(&p, pe);
47452c23cb7cSEd Maste 					printf("  Set Column to %ju\n",
47462c23cb7cSEd Maste 					    (uintmax_t) column);
47472c23cb7cSEd Maste 					break;
47482c23cb7cSEd Maste 				case DW_LNS_negate_stmt:
47492c23cb7cSEd Maste 					is_stmt = !is_stmt;
47502c23cb7cSEd Maste 					printf("  Set is_stmt to %d\n", is_stmt);
47512c23cb7cSEd Maste 					break;
47522c23cb7cSEd Maste 				case DW_LNS_set_basic_block:
47532c23cb7cSEd Maste 					printf("  Set basic block flag\n");
47542c23cb7cSEd Maste 					break;
47552c23cb7cSEd Maste 				case DW_LNS_const_add_pc:
47562c23cb7cSEd Maste 					address += ADDRESS(255);
47572c23cb7cSEd Maste 					printf("  Advance PC by constant %ju"
47582c23cb7cSEd Maste 					    " to %#jx\n",
47592c23cb7cSEd Maste 					    (uintmax_t) ADDRESS(255),
47602c23cb7cSEd Maste 					    (uintmax_t) address);
47612c23cb7cSEd Maste 					break;
47622c23cb7cSEd Maste 				case DW_LNS_fixed_advance_pc:
47632c23cb7cSEd Maste 					udelta = re->dw_decode(&p, 2);
47642c23cb7cSEd Maste 					address += udelta;
47652c23cb7cSEd Maste 					printf("  Advance PC by fixed value "
47662c23cb7cSEd Maste 					    "%ju to %#jx\n",
47672c23cb7cSEd Maste 					    (uintmax_t) udelta,
47682c23cb7cSEd Maste 					    (uintmax_t) address);
47692c23cb7cSEd Maste 					break;
47702c23cb7cSEd Maste 				case DW_LNS_set_prologue_end:
47712c23cb7cSEd Maste 					printf("  Set prologue end flag\n");
47722c23cb7cSEd Maste 					break;
47732c23cb7cSEd Maste 				case DW_LNS_set_epilogue_begin:
47742c23cb7cSEd Maste 					printf("  Set epilogue begin flag\n");
47752c23cb7cSEd Maste 					break;
47762c23cb7cSEd Maste 				case DW_LNS_set_isa:
477795fd7f26SEd Maste 					isa = _decode_uleb128(&p, pe);
4778839529caSEd Maste 					printf("  Set isa to %ju\n",
4779839529caSEd Maste 					    (uintmax_t) isa);
47802c23cb7cSEd Maste 					break;
47812c23cb7cSEd Maste 				default:
47822c23cb7cSEd Maste 					/* Unrecognized extended opcodes. */
47832c23cb7cSEd Maste 					printf("  Unknown extended opcode %u\n",
47842c23cb7cSEd Maste 					    *(p - 1));
47852c23cb7cSEd Maste 					break;
47862c23cb7cSEd Maste 				}
47872c23cb7cSEd Maste 
47882c23cb7cSEd Maste 			} else {
47892c23cb7cSEd Maste 				/*
47902c23cb7cSEd Maste 				 * Special Opcodes.
47912c23cb7cSEd Maste 				 */
47922c23cb7cSEd Maste 				line += LINE(*p);
47932c23cb7cSEd Maste 				address += ADDRESS(*p);
47942c23cb7cSEd Maste 				printf("  Special opcode %u: advance Address "
47952c23cb7cSEd Maste 				    "by %ju to %#jx and Line by %jd to %ju\n",
47962c23cb7cSEd Maste 				    *p - opbase, (uintmax_t) ADDRESS(*p),
47972c23cb7cSEd Maste 				    (uintmax_t) address, (intmax_t) LINE(*p),
47982c23cb7cSEd Maste 				    (uintmax_t) line);
47992c23cb7cSEd Maste 				p++;
48002c23cb7cSEd Maste 			}
48012c23cb7cSEd Maste 
48022c23cb7cSEd Maste 
48032c23cb7cSEd Maste 		}
48042c23cb7cSEd Maste 	}
48052c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
48062c23cb7cSEd Maste 		warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
48072c23cb7cSEd Maste 
48082c23cb7cSEd Maste #undef	RESET_REGISTERS
48092c23cb7cSEd Maste #undef	LINE
48102c23cb7cSEd Maste #undef	ADDRESS
48112c23cb7cSEd Maste }
48122c23cb7cSEd Maste 
48132c23cb7cSEd Maste static void
48142c23cb7cSEd Maste dump_dwarf_line_decoded(struct readelf *re)
48152c23cb7cSEd Maste {
48162c23cb7cSEd Maste 	Dwarf_Die die;
48172c23cb7cSEd Maste 	Dwarf_Line *linebuf, ln;
48182c23cb7cSEd Maste 	Dwarf_Addr lineaddr;
48192c23cb7cSEd Maste 	Dwarf_Signed linecount, srccount;
48202c23cb7cSEd Maste 	Dwarf_Unsigned lineno, fn;
48212c23cb7cSEd Maste 	Dwarf_Error de;
48222c23cb7cSEd Maste 	const char *dir, *file;
48232c23cb7cSEd Maste 	char **srcfiles;
48242c23cb7cSEd Maste 	int i, ret;
48252c23cb7cSEd Maste 
48262c23cb7cSEd Maste 	printf("Decoded dump of debug contents of section .debug_line:\n\n");
48272c23cb7cSEd Maste 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
48282c23cb7cSEd Maste 	    NULL, &de)) == DW_DLV_OK) {
48292c23cb7cSEd Maste 		if (dwarf_siblingof(re->dbg, NULL, &die, &de) != DW_DLV_OK)
48302c23cb7cSEd Maste 			continue;
48312c23cb7cSEd Maste 		if (dwarf_attrval_string(die, DW_AT_name, &file, &de) !=
48322c23cb7cSEd Maste 		    DW_DLV_OK)
48332c23cb7cSEd Maste 			file = NULL;
48342c23cb7cSEd Maste 		if (dwarf_attrval_string(die, DW_AT_comp_dir, &dir, &de) !=
48352c23cb7cSEd Maste 		    DW_DLV_OK)
48362c23cb7cSEd Maste 			dir = NULL;
48372c23cb7cSEd Maste 		printf("CU: ");
483865caaa0eSMaxim Sobolev 		if (dir && file && file[0] != '/')
48392c23cb7cSEd Maste 			printf("%s/", dir);
48402c23cb7cSEd Maste 		if (file)
48412c23cb7cSEd Maste 			printf("%s", file);
48422c23cb7cSEd Maste 		putchar('\n');
48432c23cb7cSEd Maste 		printf("%-37s %11s   %s\n", "Filename", "Line Number",
48442c23cb7cSEd Maste 		    "Starting Address");
48452c23cb7cSEd Maste 		if (dwarf_srclines(die, &linebuf, &linecount, &de) != DW_DLV_OK)
48462c23cb7cSEd Maste 			continue;
48472c23cb7cSEd Maste 		if (dwarf_srcfiles(die, &srcfiles, &srccount, &de) != DW_DLV_OK)
48482c23cb7cSEd Maste 			continue;
48492c23cb7cSEd Maste 		for (i = 0; i < linecount; i++) {
48502c23cb7cSEd Maste 			ln = linebuf[i];
48512c23cb7cSEd Maste 			if (dwarf_line_srcfileno(ln, &fn, &de) != DW_DLV_OK)
48522c23cb7cSEd Maste 				continue;
48532c23cb7cSEd Maste 			if (dwarf_lineno(ln, &lineno, &de) != DW_DLV_OK)
48542c23cb7cSEd Maste 				continue;
48552c23cb7cSEd Maste 			if (dwarf_lineaddr(ln, &lineaddr, &de) != DW_DLV_OK)
48562c23cb7cSEd Maste 				continue;
48572c23cb7cSEd Maste 			printf("%-37s %11ju %#18jx\n",
48582c23cb7cSEd Maste 			    basename(srcfiles[fn - 1]), (uintmax_t) lineno,
48592c23cb7cSEd Maste 			    (uintmax_t) lineaddr);
48602c23cb7cSEd Maste 		}
48612c23cb7cSEd Maste 		putchar('\n');
48622c23cb7cSEd Maste 	}
48632c23cb7cSEd Maste }
48642c23cb7cSEd Maste 
48652c23cb7cSEd Maste static void
48662c23cb7cSEd Maste dump_dwarf_die(struct readelf *re, Dwarf_Die die, int level)
48672c23cb7cSEd Maste {
48682c23cb7cSEd Maste 	Dwarf_Attribute *attr_list;
48692c23cb7cSEd Maste 	Dwarf_Die ret_die;
4870cf781b2eSEd Maste 	Dwarf_Off dieoff, cuoff, culen, attroff;
4871cf781b2eSEd Maste 	Dwarf_Unsigned ate, lang, v_udata, v_sig;
48722c23cb7cSEd Maste 	Dwarf_Signed attr_count, v_sdata;
48732c23cb7cSEd Maste 	Dwarf_Off v_off;
48742c23cb7cSEd Maste 	Dwarf_Addr v_addr;
48752c23cb7cSEd Maste 	Dwarf_Half tag, attr, form;
48762c23cb7cSEd Maste 	Dwarf_Block *v_block;
4877cf781b2eSEd Maste 	Dwarf_Bool v_bool, is_info;
4878cf781b2eSEd Maste 	Dwarf_Sig8 v_sig8;
48792c23cb7cSEd Maste 	Dwarf_Error de;
4880cf781b2eSEd Maste 	Dwarf_Ptr v_expr;
4881cf781b2eSEd Maste 	const char *tag_str, *attr_str, *ate_str, *lang_str;
4882cf781b2eSEd Maste 	char unk_tag[32], unk_attr[32];
48832c23cb7cSEd Maste 	char *v_str;
4884cf781b2eSEd Maste 	uint8_t *b, *p;
48852c23cb7cSEd Maste 	int i, j, abc, ret;
48862c23cb7cSEd Maste 
48872c23cb7cSEd Maste 	if (dwarf_dieoffset(die, &dieoff, &de) != DW_DLV_OK) {
48882c23cb7cSEd Maste 		warnx("dwarf_dieoffset failed: %s", dwarf_errmsg(de));
48892c23cb7cSEd Maste 		goto cont_search;
48902c23cb7cSEd Maste 	}
48912c23cb7cSEd Maste 
48922c23cb7cSEd Maste 	printf(" <%d><%jx>: ", level, (uintmax_t) dieoff);
48932c23cb7cSEd Maste 
48942c23cb7cSEd Maste 	if (dwarf_die_CU_offset_range(die, &cuoff, &culen, &de) != DW_DLV_OK) {
48952c23cb7cSEd Maste 		warnx("dwarf_die_CU_offset_range failed: %s",
48962c23cb7cSEd Maste 		      dwarf_errmsg(de));
48972c23cb7cSEd Maste 		cuoff = 0;
48982c23cb7cSEd Maste 	}
48992c23cb7cSEd Maste 
49002c23cb7cSEd Maste 	abc = dwarf_die_abbrev_code(die);
49012c23cb7cSEd Maste 	if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
49022c23cb7cSEd Maste 		warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
49032c23cb7cSEd Maste 		goto cont_search;
49042c23cb7cSEd Maste 	}
49052c23cb7cSEd Maste 	if (dwarf_get_TAG_name(tag, &tag_str) != DW_DLV_OK) {
4906cf781b2eSEd Maste 		snprintf(unk_tag, sizeof(unk_tag), "[Unknown Tag: %#x]", tag);
4907cf781b2eSEd Maste 		tag_str = unk_tag;
49082c23cb7cSEd Maste 	}
49092c23cb7cSEd Maste 
49102c23cb7cSEd Maste 	printf("Abbrev Number: %d (%s)\n", abc, tag_str);
49112c23cb7cSEd Maste 
49122c23cb7cSEd Maste 	if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
49132c23cb7cSEd Maste 	    DW_DLV_OK) {
49142c23cb7cSEd Maste 		if (ret == DW_DLV_ERROR)
49152c23cb7cSEd Maste 			warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
49162c23cb7cSEd Maste 		goto cont_search;
49172c23cb7cSEd Maste 	}
49182c23cb7cSEd Maste 
49192c23cb7cSEd Maste 	for (i = 0; i < attr_count; i++) {
49202c23cb7cSEd Maste 		if (dwarf_whatform(attr_list[i], &form, &de) != DW_DLV_OK) {
49212c23cb7cSEd Maste 			warnx("dwarf_whatform failed: %s", dwarf_errmsg(de));
49222c23cb7cSEd Maste 			continue;
49232c23cb7cSEd Maste 		}
49242c23cb7cSEd Maste 		if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
49252c23cb7cSEd Maste 			warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
49262c23cb7cSEd Maste 			continue;
49272c23cb7cSEd Maste 		}
49282c23cb7cSEd Maste 		if (dwarf_get_AT_name(attr, &attr_str) != DW_DLV_OK) {
4929cf781b2eSEd Maste 			snprintf(unk_attr, sizeof(unk_attr),
4930cf781b2eSEd Maste 			    "[Unknown AT: %#x]", attr);
4931cf781b2eSEd Maste 			attr_str = unk_attr;
49322c23cb7cSEd Maste 		}
4933cf781b2eSEd Maste 		if (dwarf_attroffset(attr_list[i], &attroff, &de) !=
4934cf781b2eSEd Maste 		    DW_DLV_OK) {
4935cf781b2eSEd Maste 			warnx("dwarf_attroffset failed: %s", dwarf_errmsg(de));
4936cf781b2eSEd Maste 			attroff = 0;
4937cf781b2eSEd Maste 		}
4938cf781b2eSEd Maste 		printf("    <%jx>   %-18s: ", (uintmax_t) attroff, attr_str);
49392c23cb7cSEd Maste 		switch (form) {
49402c23cb7cSEd Maste 		case DW_FORM_ref_addr:
4941cf781b2eSEd Maste 		case DW_FORM_sec_offset:
49422c23cb7cSEd Maste 			if (dwarf_global_formref(attr_list[i], &v_off, &de) !=
49432c23cb7cSEd Maste 			    DW_DLV_OK) {
49442c23cb7cSEd Maste 				warnx("dwarf_global_formref failed: %s",
49452c23cb7cSEd Maste 				    dwarf_errmsg(de));
49462c23cb7cSEd Maste 				continue;
49472c23cb7cSEd Maste 			}
4948cf781b2eSEd Maste 			if (form == DW_FORM_ref_addr)
4949cf781b2eSEd Maste 				printf("<0x%jx>", (uintmax_t) v_off);
4950cf781b2eSEd Maste 			else
4951cf781b2eSEd Maste 				printf("0x%jx", (uintmax_t) v_off);
49522c23cb7cSEd Maste 			break;
49532c23cb7cSEd Maste 
49542c23cb7cSEd Maste 		case DW_FORM_ref1:
49552c23cb7cSEd Maste 		case DW_FORM_ref2:
49562c23cb7cSEd Maste 		case DW_FORM_ref4:
49572c23cb7cSEd Maste 		case DW_FORM_ref8:
49582c23cb7cSEd Maste 		case DW_FORM_ref_udata:
49592c23cb7cSEd Maste 			if (dwarf_formref(attr_list[i], &v_off, &de) !=
49602c23cb7cSEd Maste 			    DW_DLV_OK) {
49612c23cb7cSEd Maste 				warnx("dwarf_formref failed: %s",
49622c23cb7cSEd Maste 				    dwarf_errmsg(de));
49632c23cb7cSEd Maste 				continue;
49642c23cb7cSEd Maste 			}
49652c23cb7cSEd Maste 			v_off += cuoff;
4966cf781b2eSEd Maste 			printf("<0x%jx>", (uintmax_t) v_off);
49672c23cb7cSEd Maste 			break;
49682c23cb7cSEd Maste 
49692c23cb7cSEd Maste 		case DW_FORM_addr:
49702c23cb7cSEd Maste 			if (dwarf_formaddr(attr_list[i], &v_addr, &de) !=
49712c23cb7cSEd Maste 			    DW_DLV_OK) {
49722c23cb7cSEd Maste 				warnx("dwarf_formaddr failed: %s",
49732c23cb7cSEd Maste 				    dwarf_errmsg(de));
49742c23cb7cSEd Maste 				continue;
49752c23cb7cSEd Maste 			}
49762c23cb7cSEd Maste 			printf("%#jx", (uintmax_t) v_addr);
49772c23cb7cSEd Maste 			break;
49782c23cb7cSEd Maste 
49792c23cb7cSEd Maste 		case DW_FORM_data1:
49802c23cb7cSEd Maste 		case DW_FORM_data2:
49812c23cb7cSEd Maste 		case DW_FORM_data4:
49822c23cb7cSEd Maste 		case DW_FORM_data8:
49832c23cb7cSEd Maste 		case DW_FORM_udata:
49842c23cb7cSEd Maste 			if (dwarf_formudata(attr_list[i], &v_udata, &de) !=
49852c23cb7cSEd Maste 			    DW_DLV_OK) {
49862c23cb7cSEd Maste 				warnx("dwarf_formudata failed: %s",
49872c23cb7cSEd Maste 				    dwarf_errmsg(de));
49882c23cb7cSEd Maste 				continue;
49892c23cb7cSEd Maste 			}
4990cf781b2eSEd Maste 			if (attr == DW_AT_high_pc)
4991cf781b2eSEd Maste 				printf("0x%jx", (uintmax_t) v_udata);
4992cf781b2eSEd Maste 			else
49932c23cb7cSEd Maste 				printf("%ju", (uintmax_t) v_udata);
49942c23cb7cSEd Maste 			break;
49952c23cb7cSEd Maste 
49962c23cb7cSEd Maste 		case DW_FORM_sdata:
49972c23cb7cSEd Maste 			if (dwarf_formsdata(attr_list[i], &v_sdata, &de) !=
49982c23cb7cSEd Maste 			    DW_DLV_OK) {
49992c23cb7cSEd Maste 				warnx("dwarf_formudata failed: %s",
50002c23cb7cSEd Maste 				    dwarf_errmsg(de));
50012c23cb7cSEd Maste 				continue;
50022c23cb7cSEd Maste 			}
50032c23cb7cSEd Maste 			printf("%jd", (intmax_t) v_sdata);
50042c23cb7cSEd Maste 			break;
50052c23cb7cSEd Maste 
50062c23cb7cSEd Maste 		case DW_FORM_flag:
50072c23cb7cSEd Maste 			if (dwarf_formflag(attr_list[i], &v_bool, &de) !=
50082c23cb7cSEd Maste 			    DW_DLV_OK) {
50092c23cb7cSEd Maste 				warnx("dwarf_formflag failed: %s",
50102c23cb7cSEd Maste 				    dwarf_errmsg(de));
50112c23cb7cSEd Maste 				continue;
50122c23cb7cSEd Maste 			}
50132c23cb7cSEd Maste 			printf("%jd", (intmax_t) v_bool);
50142c23cb7cSEd Maste 			break;
50152c23cb7cSEd Maste 
5016cf781b2eSEd Maste 		case DW_FORM_flag_present:
5017cf781b2eSEd Maste 			putchar('1');
5018cf781b2eSEd Maste 			break;
5019cf781b2eSEd Maste 
50202c23cb7cSEd Maste 		case DW_FORM_string:
50212c23cb7cSEd Maste 		case DW_FORM_strp:
50222c23cb7cSEd Maste 			if (dwarf_formstring(attr_list[i], &v_str, &de) !=
50232c23cb7cSEd Maste 			    DW_DLV_OK) {
50242c23cb7cSEd Maste 				warnx("dwarf_formstring failed: %s",
50252c23cb7cSEd Maste 				    dwarf_errmsg(de));
50262c23cb7cSEd Maste 				continue;
50272c23cb7cSEd Maste 			}
50282c23cb7cSEd Maste 			if (form == DW_FORM_string)
50292c23cb7cSEd Maste 				printf("%s", v_str);
50302c23cb7cSEd Maste 			else
50312c23cb7cSEd Maste 				printf("(indirect string) %s", v_str);
50322c23cb7cSEd Maste 			break;
50332c23cb7cSEd Maste 
50342c23cb7cSEd Maste 		case DW_FORM_block:
50352c23cb7cSEd Maste 		case DW_FORM_block1:
50362c23cb7cSEd Maste 		case DW_FORM_block2:
50372c23cb7cSEd Maste 		case DW_FORM_block4:
50382c23cb7cSEd Maste 			if (dwarf_formblock(attr_list[i], &v_block, &de) !=
50392c23cb7cSEd Maste 			    DW_DLV_OK) {
50402c23cb7cSEd Maste 				warnx("dwarf_formblock failed: %s",
50412c23cb7cSEd Maste 				    dwarf_errmsg(de));
50422c23cb7cSEd Maste 				continue;
50432c23cb7cSEd Maste 			}
5044cf781b2eSEd Maste 			printf("%ju byte block:", (uintmax_t) v_block->bl_len);
50452c23cb7cSEd Maste 			b = v_block->bl_data;
50462c23cb7cSEd Maste 			for (j = 0; (Dwarf_Unsigned) j < v_block->bl_len; j++)
50472c23cb7cSEd Maste 				printf(" %x", b[j]);
5048cf781b2eSEd Maste 			printf("\t(");
5049cf781b2eSEd Maste 			dump_dwarf_block(re, v_block->bl_data, v_block->bl_len);
5050cf781b2eSEd Maste 			putchar(')');
50512c23cb7cSEd Maste 			break;
5052cf781b2eSEd Maste 
5053cf781b2eSEd Maste 		case DW_FORM_exprloc:
5054cf781b2eSEd Maste 			if (dwarf_formexprloc(attr_list[i], &v_udata, &v_expr,
5055cf781b2eSEd Maste 			    &de) != DW_DLV_OK) {
5056cf781b2eSEd Maste 				warnx("dwarf_formexprloc failed: %s",
5057cf781b2eSEd Maste 				    dwarf_errmsg(de));
5058cf781b2eSEd Maste 				continue;
5059cf781b2eSEd Maste 			}
5060cf781b2eSEd Maste 			printf("%ju byte block:", (uintmax_t) v_udata);
5061cf781b2eSEd Maste 			b = v_expr;
5062cf781b2eSEd Maste 			for (j = 0; (Dwarf_Unsigned) j < v_udata; j++)
5063cf781b2eSEd Maste 				printf(" %x", b[j]);
5064cf781b2eSEd Maste 			printf("\t(");
5065cf781b2eSEd Maste 			dump_dwarf_block(re, v_expr, v_udata);
5066cf781b2eSEd Maste 			putchar(')');
5067cf781b2eSEd Maste 			break;
5068cf781b2eSEd Maste 
5069cf781b2eSEd Maste 		case DW_FORM_ref_sig8:
5070cf781b2eSEd Maste 			if (dwarf_formsig8(attr_list[i], &v_sig8, &de) !=
5071cf781b2eSEd Maste 			    DW_DLV_OK) {
5072cf781b2eSEd Maste 				warnx("dwarf_formsig8 failed: %s",
5073cf781b2eSEd Maste 				    dwarf_errmsg(de));
5074cf781b2eSEd Maste 				continue;
5075cf781b2eSEd Maste 			}
5076cf781b2eSEd Maste 			p = (uint8_t *)(uintptr_t) &v_sig8.signature[0];
5077cf781b2eSEd Maste 			v_sig = re->dw_decode(&p, 8);
5078cf781b2eSEd Maste 			printf("signature: 0x%jx", (uintmax_t) v_sig);
50792c23cb7cSEd Maste 		}
50802c23cb7cSEd Maste 		switch (attr) {
50812c23cb7cSEd Maste 		case DW_AT_encoding:
50822c23cb7cSEd Maste 			if (dwarf_attrval_unsigned(die, attr, &ate, &de) !=
50832c23cb7cSEd Maste 			    DW_DLV_OK)
50842c23cb7cSEd Maste 				break;
50852c23cb7cSEd Maste 			if (dwarf_get_ATE_name(ate, &ate_str) != DW_DLV_OK)
5086cf781b2eSEd Maste 				ate_str = "DW_ATE_UNKNOWN";
50872c23cb7cSEd Maste 			printf("\t(%s)", &ate_str[strlen("DW_ATE_")]);
50882c23cb7cSEd Maste 			break;
5089cf781b2eSEd Maste 
5090cf781b2eSEd Maste 		case DW_AT_language:
5091cf781b2eSEd Maste 			if (dwarf_attrval_unsigned(die, attr, &lang, &de) !=
5092cf781b2eSEd Maste 			    DW_DLV_OK)
5093cf781b2eSEd Maste 				break;
5094cf781b2eSEd Maste 			if (dwarf_get_LANG_name(lang, &lang_str) != DW_DLV_OK)
5095cf781b2eSEd Maste 				break;
5096cf781b2eSEd Maste 			printf("\t(%s)", &lang_str[strlen("DW_LANG_")]);
5097cf781b2eSEd Maste 			break;
5098cf781b2eSEd Maste 
5099cf781b2eSEd Maste 		case DW_AT_location:
5100cf781b2eSEd Maste 		case DW_AT_string_length:
5101cf781b2eSEd Maste 		case DW_AT_return_addr:
5102cf781b2eSEd Maste 		case DW_AT_data_member_location:
5103cf781b2eSEd Maste 		case DW_AT_frame_base:
5104cf781b2eSEd Maste 		case DW_AT_segment:
5105cf781b2eSEd Maste 		case DW_AT_static_link:
5106cf781b2eSEd Maste 		case DW_AT_use_location:
5107cf781b2eSEd Maste 		case DW_AT_vtable_elem_location:
5108cf781b2eSEd Maste 			switch (form) {
5109cf781b2eSEd Maste 			case DW_FORM_data4:
5110cf781b2eSEd Maste 			case DW_FORM_data8:
5111cf781b2eSEd Maste 			case DW_FORM_sec_offset:
5112cf781b2eSEd Maste 				printf("\t(location list)");
5113cf781b2eSEd Maste 				break;
5114cf781b2eSEd Maste 			default:
5115cf781b2eSEd Maste 				break;
5116cf781b2eSEd Maste 			}
5117cf781b2eSEd Maste 
51182c23cb7cSEd Maste 		default:
51192c23cb7cSEd Maste 			break;
51202c23cb7cSEd Maste 		}
51212c23cb7cSEd Maste 		putchar('\n');
51222c23cb7cSEd Maste 	}
51232c23cb7cSEd Maste 
51242c23cb7cSEd Maste 
51252c23cb7cSEd Maste cont_search:
51262c23cb7cSEd Maste 	/* Search children. */
51272c23cb7cSEd Maste 	ret = dwarf_child(die, &ret_die, &de);
51282c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
51292c23cb7cSEd Maste 		warnx("dwarf_child: %s", dwarf_errmsg(de));
51302c23cb7cSEd Maste 	else if (ret == DW_DLV_OK)
51312c23cb7cSEd Maste 		dump_dwarf_die(re, ret_die, level + 1);
51322c23cb7cSEd Maste 
51332c23cb7cSEd Maste 	/* Search sibling. */
5134cf781b2eSEd Maste 	is_info = dwarf_get_die_infotypes_flag(die);
5135cf781b2eSEd Maste 	ret = dwarf_siblingof_b(re->dbg, die, &ret_die, is_info, &de);
51362c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
51372c23cb7cSEd Maste 		warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
51382c23cb7cSEd Maste 	else if (ret == DW_DLV_OK)
51392c23cb7cSEd Maste 		dump_dwarf_die(re, ret_die, level);
51402c23cb7cSEd Maste 
51412c23cb7cSEd Maste 	dwarf_dealloc(re->dbg, die, DW_DLA_DIE);
51422c23cb7cSEd Maste }
51432c23cb7cSEd Maste 
51442c23cb7cSEd Maste static void
5145cf781b2eSEd Maste set_cu_context(struct readelf *re, Dwarf_Half psize, Dwarf_Half osize,
5146cf781b2eSEd Maste     Dwarf_Half ver)
5147cf781b2eSEd Maste {
5148cf781b2eSEd Maste 
5149cf781b2eSEd Maste 	re->cu_psize = psize;
5150cf781b2eSEd Maste 	re->cu_osize = osize;
5151cf781b2eSEd Maste 	re->cu_ver = ver;
5152cf781b2eSEd Maste }
5153cf781b2eSEd Maste 
5154cf781b2eSEd Maste static void
5155cf781b2eSEd Maste dump_dwarf_info(struct readelf *re, Dwarf_Bool is_info)
51562c23cb7cSEd Maste {
51572c23cb7cSEd Maste 	struct section *s;
51582c23cb7cSEd Maste 	Dwarf_Die die;
51592c23cb7cSEd Maste 	Dwarf_Error de;
5160cf781b2eSEd Maste 	Dwarf_Half tag, version, pointer_size, off_size;
51612c23cb7cSEd Maste 	Dwarf_Off cu_offset, cu_length;
51622c23cb7cSEd Maste 	Dwarf_Off aboff;
5163cf781b2eSEd Maste 	Dwarf_Unsigned typeoff;
5164cf781b2eSEd Maste 	Dwarf_Sig8 sig8;
5165cf781b2eSEd Maste 	Dwarf_Unsigned sig;
5166cf781b2eSEd Maste 	uint8_t *p;
5167cf781b2eSEd Maste 	const char *sn;
5168cf781b2eSEd Maste 	int i, ret;
51692c23cb7cSEd Maste 
5170cf781b2eSEd Maste 	sn = is_info ? ".debug_info" : ".debug_types";
51712c23cb7cSEd Maste 
51722c23cb7cSEd Maste 	s = NULL;
51732c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
51742c23cb7cSEd Maste 		s = &re->sl[i];
5175cf781b2eSEd Maste 		if (s->name != NULL && !strcmp(s->name, sn))
51762c23cb7cSEd Maste 			break;
51772c23cb7cSEd Maste 	}
51782c23cb7cSEd Maste 	if ((size_t) i >= re->shnum)
51792c23cb7cSEd Maste 		return;
51802c23cb7cSEd Maste 
5181cf781b2eSEd Maste 	do {
5182cf781b2eSEd Maste 		printf("\nDump of debug contents of section %s:\n", sn);
51832c23cb7cSEd Maste 
5184cf781b2eSEd Maste 		while ((ret = dwarf_next_cu_header_c(re->dbg, is_info, NULL,
5185cf781b2eSEd Maste 		    &version, &aboff, &pointer_size, &off_size, NULL, &sig8,
5186cf781b2eSEd Maste 		    &typeoff, NULL, &de)) == DW_DLV_OK) {
5187cf781b2eSEd Maste 			set_cu_context(re, pointer_size, off_size, version);
51882c23cb7cSEd Maste 			die = NULL;
5189cf781b2eSEd Maste 			while (dwarf_siblingof_b(re->dbg, die, &die, is_info,
5190cf781b2eSEd Maste 			    &de) == DW_DLV_OK) {
51912c23cb7cSEd Maste 				if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
51922c23cb7cSEd Maste 					warnx("dwarf_tag failed: %s",
51932c23cb7cSEd Maste 					    dwarf_errmsg(de));
5194cf781b2eSEd Maste 					continue;
51952c23cb7cSEd Maste 				}
51962c23cb7cSEd Maste 				/* XXX: What about DW_TAG_partial_unit? */
5197cf781b2eSEd Maste 				if ((is_info && tag == DW_TAG_compile_unit) ||
5198cf781b2eSEd Maste 				    (!is_info && tag == DW_TAG_type_unit))
51992c23cb7cSEd Maste 					break;
52002c23cb7cSEd Maste 			}
5201cf781b2eSEd Maste 			if (die == NULL && is_info) {
5202cf781b2eSEd Maste 				warnx("could not find DW_TAG_compile_unit "
5203cf781b2eSEd Maste 				    "die");
5204cf781b2eSEd Maste 				continue;
5205cf781b2eSEd Maste 			} else if (die == NULL && !is_info) {
5206cf781b2eSEd Maste 				warnx("could not find DW_TAG_type_unit die");
5207cf781b2eSEd Maste 				continue;
52082c23cb7cSEd Maste 			}
52092c23cb7cSEd Maste 
5210cf781b2eSEd Maste 			if (dwarf_die_CU_offset_range(die, &cu_offset,
5211cf781b2eSEd Maste 			    &cu_length, &de) != DW_DLV_OK) {
52122c23cb7cSEd Maste 				warnx("dwarf_die_CU_offset failed: %s",
52132c23cb7cSEd Maste 				    dwarf_errmsg(de));
52142c23cb7cSEd Maste 				continue;
52152c23cb7cSEd Maste 			}
52162c23cb7cSEd Maste 
5217cf781b2eSEd Maste 			cu_length -= off_size == 4 ? 4 : 12;
5218cf781b2eSEd Maste 
5219cf781b2eSEd Maste 			sig = 0;
5220cf781b2eSEd Maste 			if (!is_info) {
5221cf781b2eSEd Maste 				p = (uint8_t *)(uintptr_t) &sig8.signature[0];
5222cf781b2eSEd Maste 				sig = re->dw_decode(&p, 8);
5223cf781b2eSEd Maste 			}
5224cf781b2eSEd Maste 
5225cf781b2eSEd Maste 			printf("\n  Type Unit @ offset 0x%jx:\n",
5226cf781b2eSEd Maste 			    (uintmax_t) cu_offset);
5227cf781b2eSEd Maste 			printf("    Length:\t\t%#jx (%d-bit)\n",
5228cf781b2eSEd Maste 			    (uintmax_t) cu_length, off_size == 4 ? 32 : 64);
52292c23cb7cSEd Maste 			printf("    Version:\t\t%u\n", version);
5230cf781b2eSEd Maste 			printf("    Abbrev Offset:\t0x%jx\n",
5231cf781b2eSEd Maste 			    (uintmax_t) aboff);
52322c23cb7cSEd Maste 			printf("    Pointer Size:\t%u\n", pointer_size);
5233cf781b2eSEd Maste 			if (!is_info) {
5234cf781b2eSEd Maste 				printf("    Signature:\t\t0x%016jx\n",
5235cf781b2eSEd Maste 				    (uintmax_t) sig);
5236cf781b2eSEd Maste 				printf("    Type Offset:\t0x%jx\n",
5237cf781b2eSEd Maste 				    (uintmax_t) typeoff);
5238cf781b2eSEd Maste 			}
52392c23cb7cSEd Maste 
52402c23cb7cSEd Maste 			dump_dwarf_die(re, die, 0);
52412c23cb7cSEd Maste 		}
52422c23cb7cSEd Maste 		if (ret == DW_DLV_ERROR)
52432c23cb7cSEd Maste 			warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
5244cf781b2eSEd Maste 		if (is_info)
5245cf781b2eSEd Maste 			break;
5246cf781b2eSEd Maste 	} while (dwarf_next_types_section(re->dbg, &de) == DW_DLV_OK);
52472c23cb7cSEd Maste }
52482c23cb7cSEd Maste 
52492c23cb7cSEd Maste static void
52502c23cb7cSEd Maste dump_dwarf_abbrev(struct readelf *re)
52512c23cb7cSEd Maste {
52522c23cb7cSEd Maste 	Dwarf_Abbrev ab;
52532c23cb7cSEd Maste 	Dwarf_Off aboff, atoff;
52542c23cb7cSEd Maste 	Dwarf_Unsigned length, attr_count;
52552c23cb7cSEd Maste 	Dwarf_Signed flag, form;
52562c23cb7cSEd Maste 	Dwarf_Half tag, attr;
52572c23cb7cSEd Maste 	Dwarf_Error de;
52582c23cb7cSEd Maste 	const char *tag_str, *attr_str, *form_str;
5259cf781b2eSEd Maste 	char unk_tag[32], unk_attr[32], unk_form[32];
52602c23cb7cSEd Maste 	int i, j, ret;
52612c23cb7cSEd Maste 
52622c23cb7cSEd Maste 	printf("\nContents of section .debug_abbrev:\n\n");
52632c23cb7cSEd Maste 
52642c23cb7cSEd Maste 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, &aboff,
52652c23cb7cSEd Maste 	    NULL, NULL, &de)) ==  DW_DLV_OK) {
52662c23cb7cSEd Maste 		printf("  Number TAG\n");
52672c23cb7cSEd Maste 		i = 0;
52682c23cb7cSEd Maste 		while ((ret = dwarf_get_abbrev(re->dbg, aboff, &ab, &length,
52692c23cb7cSEd Maste 		    &attr_count, &de)) == DW_DLV_OK) {
52702c23cb7cSEd Maste 			if (length == 1) {
52712c23cb7cSEd Maste 				dwarf_dealloc(re->dbg, ab, DW_DLA_ABBREV);
52722c23cb7cSEd Maste 				break;
52732c23cb7cSEd Maste 			}
52742c23cb7cSEd Maste 			aboff += length;
52752c23cb7cSEd Maste 			printf("%4d", ++i);
52762c23cb7cSEd Maste 			if (dwarf_get_abbrev_tag(ab, &tag, &de) != DW_DLV_OK) {
52772c23cb7cSEd Maste 				warnx("dwarf_get_abbrev_tag failed: %s",
52782c23cb7cSEd Maste 				    dwarf_errmsg(de));
52792c23cb7cSEd Maste 				goto next_abbrev;
52802c23cb7cSEd Maste 			}
52812c23cb7cSEd Maste 			if (dwarf_get_TAG_name(tag, &tag_str) != DW_DLV_OK) {
5282cf781b2eSEd Maste 				snprintf(unk_tag, sizeof(unk_tag),
5283cf781b2eSEd Maste 				    "[Unknown Tag: %#x]", tag);
5284cf781b2eSEd Maste 				tag_str = unk_tag;
52852c23cb7cSEd Maste 			}
52862c23cb7cSEd Maste 			if (dwarf_get_abbrev_children_flag(ab, &flag, &de) !=
52872c23cb7cSEd Maste 			    DW_DLV_OK) {
52882c23cb7cSEd Maste 				warnx("dwarf_get_abbrev_children_flag failed:"
52892c23cb7cSEd Maste 				    " %s", dwarf_errmsg(de));
52902c23cb7cSEd Maste 				goto next_abbrev;
52912c23cb7cSEd Maste 			}
52922c23cb7cSEd Maste 			printf("      %s    %s\n", tag_str,
52932c23cb7cSEd Maste 			    flag ? "[has children]" : "[no children]");
52942c23cb7cSEd Maste 			for (j = 0; (Dwarf_Unsigned) j < attr_count; j++) {
52952c23cb7cSEd Maste 				if (dwarf_get_abbrev_entry(ab, (Dwarf_Signed) j,
52962c23cb7cSEd Maste 				    &attr, &form, &atoff, &de) != DW_DLV_OK) {
52972c23cb7cSEd Maste 					warnx("dwarf_get_abbrev_entry failed:"
52982c23cb7cSEd Maste 					    " %s", dwarf_errmsg(de));
52992c23cb7cSEd Maste 					continue;
53002c23cb7cSEd Maste 				}
53012c23cb7cSEd Maste 				if (dwarf_get_AT_name(attr, &attr_str) !=
53022c23cb7cSEd Maste 				    DW_DLV_OK) {
5303cf781b2eSEd Maste 					snprintf(unk_attr, sizeof(unk_attr),
5304cf781b2eSEd Maste 					    "[Unknown AT: %#x]", attr);
5305cf781b2eSEd Maste 					attr_str = unk_attr;
53062c23cb7cSEd Maste 				}
53072c23cb7cSEd Maste 				if (dwarf_get_FORM_name(form, &form_str) !=
53082c23cb7cSEd Maste 				    DW_DLV_OK) {
5309cf781b2eSEd Maste 					snprintf(unk_form, sizeof(unk_form),
5310cf781b2eSEd Maste 					    "[Unknown Form: %#x]",
5311cf781b2eSEd Maste 					    (Dwarf_Half) form);
5312cf781b2eSEd Maste 					form_str = unk_form;
53132c23cb7cSEd Maste 				}
53142c23cb7cSEd Maste 				printf("    %-18s %s\n", attr_str, form_str);
53152c23cb7cSEd Maste 			}
53162c23cb7cSEd Maste 		next_abbrev:
53172c23cb7cSEd Maste 			dwarf_dealloc(re->dbg, ab, DW_DLA_ABBREV);
53182c23cb7cSEd Maste 		}
53192c23cb7cSEd Maste 		if (ret != DW_DLV_OK)
53202c23cb7cSEd Maste 			warnx("dwarf_get_abbrev: %s", dwarf_errmsg(de));
53212c23cb7cSEd Maste 	}
53222c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
53232c23cb7cSEd Maste 		warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
53242c23cb7cSEd Maste }
53252c23cb7cSEd Maste 
53262c23cb7cSEd Maste static void
53272c23cb7cSEd Maste dump_dwarf_pubnames(struct readelf *re)
53282c23cb7cSEd Maste {
53292c23cb7cSEd Maste 	struct section *s;
53302c23cb7cSEd Maste 	Dwarf_Off die_off;
53312c23cb7cSEd Maste 	Dwarf_Unsigned offset, length, nt_cu_offset, nt_cu_length;
53322c23cb7cSEd Maste 	Dwarf_Signed cnt;
53332c23cb7cSEd Maste 	Dwarf_Global *globs;
53342c23cb7cSEd Maste 	Dwarf_Half nt_version;
53352c23cb7cSEd Maste 	Dwarf_Error de;
53362c23cb7cSEd Maste 	Elf_Data *d;
53372c23cb7cSEd Maste 	char *glob_name;
53382c23cb7cSEd Maste 	int i, dwarf_size, elferr;
53392c23cb7cSEd Maste 
53402c23cb7cSEd Maste 	printf("\nContents of the .debug_pubnames section:\n");
53412c23cb7cSEd Maste 
53422c23cb7cSEd Maste 	s = NULL;
53432c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
53442c23cb7cSEd Maste 		s = &re->sl[i];
53452c23cb7cSEd Maste 		if (s->name != NULL && !strcmp(s->name, ".debug_pubnames"))
53462c23cb7cSEd Maste 			break;
53472c23cb7cSEd Maste 	}
53482c23cb7cSEd Maste 	if ((size_t) i >= re->shnum)
53492c23cb7cSEd Maste 		return;
53502c23cb7cSEd Maste 
53512c23cb7cSEd Maste 	(void) elf_errno();
53522c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
53532c23cb7cSEd Maste 		elferr = elf_errno();
53542c23cb7cSEd Maste 		if (elferr != 0)
53552c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
53562c23cb7cSEd Maste 		return;
53572c23cb7cSEd Maste 	}
53582c23cb7cSEd Maste 	if (d->d_size <= 0)
53592c23cb7cSEd Maste 		return;
53602c23cb7cSEd Maste 
53612c23cb7cSEd Maste 	/* Read in .debug_pubnames section table header. */
53622c23cb7cSEd Maste 	offset = 0;
53632c23cb7cSEd Maste 	length = re->dw_read(d, &offset, 4);
53642c23cb7cSEd Maste 	if (length == 0xffffffff) {
53652c23cb7cSEd Maste 		dwarf_size = 8;
53662c23cb7cSEd Maste 		length = re->dw_read(d, &offset, 8);
53672c23cb7cSEd Maste 	} else
53682c23cb7cSEd Maste 		dwarf_size = 4;
53692c23cb7cSEd Maste 
53702c23cb7cSEd Maste 	if (length > d->d_size - offset) {
53712c23cb7cSEd Maste 		warnx("invalid .dwarf_pubnames section");
53722c23cb7cSEd Maste 		return;
53732c23cb7cSEd Maste 	}
53742c23cb7cSEd Maste 
53752c23cb7cSEd Maste 	nt_version = re->dw_read(d, &offset, 2);
53762c23cb7cSEd Maste 	nt_cu_offset = re->dw_read(d, &offset, dwarf_size);
53772c23cb7cSEd Maste 	nt_cu_length = re->dw_read(d, &offset, dwarf_size);
53782c23cb7cSEd Maste 	printf("  Length:\t\t\t\t%ju\n", (uintmax_t) length);
53792c23cb7cSEd Maste 	printf("  Version:\t\t\t\t%u\n", nt_version);
53802c23cb7cSEd Maste 	printf("  Offset into .debug_info section:\t%ju\n",
53812c23cb7cSEd Maste 	    (uintmax_t) nt_cu_offset);
53822c23cb7cSEd Maste 	printf("  Size of area in .debug_info section:\t%ju\n",
53832c23cb7cSEd Maste 	    (uintmax_t) nt_cu_length);
53842c23cb7cSEd Maste 
53852c23cb7cSEd Maste 	if (dwarf_get_globals(re->dbg, &globs, &cnt, &de) != DW_DLV_OK) {
53862c23cb7cSEd Maste 		warnx("dwarf_get_globals failed: %s", dwarf_errmsg(de));
53872c23cb7cSEd Maste 		return;
53882c23cb7cSEd Maste 	}
53892c23cb7cSEd Maste 
53902c23cb7cSEd Maste 	printf("\n    Offset      Name\n");
53912c23cb7cSEd Maste 	for (i = 0; i < cnt; i++) {
53922c23cb7cSEd Maste 		if (dwarf_globname(globs[i], &glob_name, &de) != DW_DLV_OK) {
53932c23cb7cSEd Maste 			warnx("dwarf_globname failed: %s", dwarf_errmsg(de));
53942c23cb7cSEd Maste 			continue;
53952c23cb7cSEd Maste 		}
53962c23cb7cSEd Maste 		if (dwarf_global_die_offset(globs[i], &die_off, &de) !=
53972c23cb7cSEd Maste 		    DW_DLV_OK) {
53982c23cb7cSEd Maste 			warnx("dwarf_global_die_offset failed: %s",
53992c23cb7cSEd Maste 			    dwarf_errmsg(de));
54002c23cb7cSEd Maste 			continue;
54012c23cb7cSEd Maste 		}
54022c23cb7cSEd Maste 		printf("    %-11ju %s\n", (uintmax_t) die_off, glob_name);
54032c23cb7cSEd Maste 	}
54042c23cb7cSEd Maste }
54052c23cb7cSEd Maste 
54062c23cb7cSEd Maste static void
54072c23cb7cSEd Maste dump_dwarf_aranges(struct readelf *re)
54082c23cb7cSEd Maste {
54092c23cb7cSEd Maste 	struct section *s;
54102c23cb7cSEd Maste 	Dwarf_Arange *aranges;
54112c23cb7cSEd Maste 	Dwarf_Addr start;
54122c23cb7cSEd Maste 	Dwarf_Unsigned offset, length, as_cu_offset;
54132c23cb7cSEd Maste 	Dwarf_Off die_off;
54142c23cb7cSEd Maste 	Dwarf_Signed cnt;
54152c23cb7cSEd Maste 	Dwarf_Half as_version, as_addrsz, as_segsz;
54162c23cb7cSEd Maste 	Dwarf_Error de;
54172c23cb7cSEd Maste 	Elf_Data *d;
54182c23cb7cSEd Maste 	int i, dwarf_size, elferr;
54192c23cb7cSEd Maste 
54202c23cb7cSEd Maste 	printf("\nContents of section .debug_aranges:\n");
54212c23cb7cSEd Maste 
54222c23cb7cSEd Maste 	s = NULL;
54232c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
54242c23cb7cSEd Maste 		s = &re->sl[i];
54252c23cb7cSEd Maste 		if (s->name != NULL && !strcmp(s->name, ".debug_aranges"))
54262c23cb7cSEd Maste 			break;
54272c23cb7cSEd Maste 	}
54282c23cb7cSEd Maste 	if ((size_t) i >= re->shnum)
54292c23cb7cSEd Maste 		return;
54302c23cb7cSEd Maste 
54312c23cb7cSEd Maste 	(void) elf_errno();
54322c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
54332c23cb7cSEd Maste 		elferr = elf_errno();
54342c23cb7cSEd Maste 		if (elferr != 0)
54352c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
54362c23cb7cSEd Maste 		return;
54372c23cb7cSEd Maste 	}
54382c23cb7cSEd Maste 	if (d->d_size <= 0)
54392c23cb7cSEd Maste 		return;
54402c23cb7cSEd Maste 
54412c23cb7cSEd Maste 	/* Read in the .debug_aranges section table header. */
54422c23cb7cSEd Maste 	offset = 0;
54432c23cb7cSEd Maste 	length = re->dw_read(d, &offset, 4);
54442c23cb7cSEd Maste 	if (length == 0xffffffff) {
54452c23cb7cSEd Maste 		dwarf_size = 8;
54462c23cb7cSEd Maste 		length = re->dw_read(d, &offset, 8);
54472c23cb7cSEd Maste 	} else
54482c23cb7cSEd Maste 		dwarf_size = 4;
54492c23cb7cSEd Maste 
54502c23cb7cSEd Maste 	if (length > d->d_size - offset) {
54512c23cb7cSEd Maste 		warnx("invalid .dwarf_aranges section");
54522c23cb7cSEd Maste 		return;
54532c23cb7cSEd Maste 	}
54542c23cb7cSEd Maste 
54552c23cb7cSEd Maste 	as_version = re->dw_read(d, &offset, 2);
54562c23cb7cSEd Maste 	as_cu_offset = re->dw_read(d, &offset, dwarf_size);
54572c23cb7cSEd Maste 	as_addrsz = re->dw_read(d, &offset, 1);
54582c23cb7cSEd Maste 	as_segsz = re->dw_read(d, &offset, 1);
54592c23cb7cSEd Maste 
54602c23cb7cSEd Maste 	printf("  Length:\t\t\t%ju\n", (uintmax_t) length);
54612c23cb7cSEd Maste 	printf("  Version:\t\t\t%u\n", as_version);
54622c23cb7cSEd Maste 	printf("  Offset into .debug_info:\t%ju\n", (uintmax_t) as_cu_offset);
54632c23cb7cSEd Maste 	printf("  Pointer Size:\t\t\t%u\n", as_addrsz);
54642c23cb7cSEd Maste 	printf("  Segment Size:\t\t\t%u\n", as_segsz);
54652c23cb7cSEd Maste 
54662c23cb7cSEd Maste 	if (dwarf_get_aranges(re->dbg, &aranges, &cnt, &de) != DW_DLV_OK) {
54672c23cb7cSEd Maste 		warnx("dwarf_get_aranges failed: %s", dwarf_errmsg(de));
54682c23cb7cSEd Maste 		return;
54692c23cb7cSEd Maste 	}
54702c23cb7cSEd Maste 
54712c23cb7cSEd Maste 	printf("\n    Address  Length\n");
54722c23cb7cSEd Maste 	for (i = 0; i < cnt; i++) {
54732c23cb7cSEd Maste 		if (dwarf_get_arange_info(aranges[i], &start, &length,
54742c23cb7cSEd Maste 		    &die_off, &de) != DW_DLV_OK) {
54752c23cb7cSEd Maste 			warnx("dwarf_get_arange_info failed: %s",
54762c23cb7cSEd Maste 			    dwarf_errmsg(de));
54772c23cb7cSEd Maste 			continue;
54782c23cb7cSEd Maste 		}
54792c23cb7cSEd Maste 		printf("    %08jx %ju\n", (uintmax_t) start,
54802c23cb7cSEd Maste 		    (uintmax_t) length);
54812c23cb7cSEd Maste 	}
54822c23cb7cSEd Maste }
54832c23cb7cSEd Maste 
54842c23cb7cSEd Maste static void
54852c23cb7cSEd Maste dump_dwarf_ranges_foreach(struct readelf *re, Dwarf_Die die, Dwarf_Addr base)
54862c23cb7cSEd Maste {
54872c23cb7cSEd Maste 	Dwarf_Attribute *attr_list;
54882c23cb7cSEd Maste 	Dwarf_Ranges *ranges;
54892c23cb7cSEd Maste 	Dwarf_Die ret_die;
54902c23cb7cSEd Maste 	Dwarf_Error de;
54912c23cb7cSEd Maste 	Dwarf_Addr base0;
54922c23cb7cSEd Maste 	Dwarf_Half attr;
54932c23cb7cSEd Maste 	Dwarf_Signed attr_count, cnt;
54942c23cb7cSEd Maste 	Dwarf_Unsigned off, bytecnt;
54952c23cb7cSEd Maste 	int i, j, ret;
54962c23cb7cSEd Maste 
54972c23cb7cSEd Maste 	if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
54982c23cb7cSEd Maste 	    DW_DLV_OK) {
54992c23cb7cSEd Maste 		if (ret == DW_DLV_ERROR)
55002c23cb7cSEd Maste 			warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
55012c23cb7cSEd Maste 		goto cont_search;
55022c23cb7cSEd Maste 	}
55032c23cb7cSEd Maste 
55042c23cb7cSEd Maste 	for (i = 0; i < attr_count; i++) {
55052c23cb7cSEd Maste 		if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
55062c23cb7cSEd Maste 			warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
55072c23cb7cSEd Maste 			continue;
55082c23cb7cSEd Maste 		}
55092c23cb7cSEd Maste 		if (attr != DW_AT_ranges)
55102c23cb7cSEd Maste 			continue;
55112c23cb7cSEd Maste 		if (dwarf_formudata(attr_list[i], &off, &de) != DW_DLV_OK) {
55122c23cb7cSEd Maste 			warnx("dwarf_formudata failed: %s", dwarf_errmsg(de));
55132c23cb7cSEd Maste 			continue;
55142c23cb7cSEd Maste 		}
55152c23cb7cSEd Maste 		if (dwarf_get_ranges(re->dbg, (Dwarf_Off) off, &ranges, &cnt,
55162c23cb7cSEd Maste 		    &bytecnt, &de) != DW_DLV_OK)
55172c23cb7cSEd Maste 			continue;
55182c23cb7cSEd Maste 		base0 = base;
55192c23cb7cSEd Maste 		for (j = 0; j < cnt; j++) {
55202c23cb7cSEd Maste 			printf("    %08jx ", (uintmax_t) off);
55212c23cb7cSEd Maste 			if (ranges[j].dwr_type == DW_RANGES_END) {
55222c23cb7cSEd Maste 				printf("%s\n", "<End of list>");
55232c23cb7cSEd Maste 				continue;
55242c23cb7cSEd Maste 			} else if (ranges[j].dwr_type ==
55252c23cb7cSEd Maste 			    DW_RANGES_ADDRESS_SELECTION) {
55262c23cb7cSEd Maste 				base0 = ranges[j].dwr_addr2;
55272c23cb7cSEd Maste 				continue;
55282c23cb7cSEd Maste 			}
55292c23cb7cSEd Maste 			if (re->ec == ELFCLASS32)
55302c23cb7cSEd Maste 				printf("%08jx %08jx\n",
5531839529caSEd Maste 				    (uintmax_t) (ranges[j].dwr_addr1 + base0),
5532839529caSEd Maste 				    (uintmax_t) (ranges[j].dwr_addr2 + base0));
55332c23cb7cSEd Maste 			else
55342c23cb7cSEd Maste 				printf("%016jx %016jx\n",
5535839529caSEd Maste 				    (uintmax_t) (ranges[j].dwr_addr1 + base0),
5536839529caSEd Maste 				    (uintmax_t) (ranges[j].dwr_addr2 + base0));
55372c23cb7cSEd Maste 		}
55382c23cb7cSEd Maste 	}
55392c23cb7cSEd Maste 
55402c23cb7cSEd Maste cont_search:
55412c23cb7cSEd Maste 	/* Search children. */
55422c23cb7cSEd Maste 	ret = dwarf_child(die, &ret_die, &de);
55432c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
55442c23cb7cSEd Maste 		warnx("dwarf_child: %s", dwarf_errmsg(de));
55452c23cb7cSEd Maste 	else if (ret == DW_DLV_OK)
55462c23cb7cSEd Maste 		dump_dwarf_ranges_foreach(re, ret_die, base);
55472c23cb7cSEd Maste 
55482c23cb7cSEd Maste 	/* Search sibling. */
55492c23cb7cSEd Maste 	ret = dwarf_siblingof(re->dbg, die, &ret_die, &de);
55502c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
55512c23cb7cSEd Maste 		warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
55522c23cb7cSEd Maste 	else if (ret == DW_DLV_OK)
55532c23cb7cSEd Maste 		dump_dwarf_ranges_foreach(re, ret_die, base);
55542c23cb7cSEd Maste }
55552c23cb7cSEd Maste 
55562c23cb7cSEd Maste static void
55572c23cb7cSEd Maste dump_dwarf_ranges(struct readelf *re)
55582c23cb7cSEd Maste {
55592c23cb7cSEd Maste 	Dwarf_Ranges *ranges;
55602c23cb7cSEd Maste 	Dwarf_Die die;
55612c23cb7cSEd Maste 	Dwarf_Signed cnt;
55622c23cb7cSEd Maste 	Dwarf_Unsigned bytecnt;
55632c23cb7cSEd Maste 	Dwarf_Half tag;
55642c23cb7cSEd Maste 	Dwarf_Error de;
55652c23cb7cSEd Maste 	Dwarf_Unsigned lowpc;
55662c23cb7cSEd Maste 	int ret;
55672c23cb7cSEd Maste 
55682c23cb7cSEd Maste 	if (dwarf_get_ranges(re->dbg, 0, &ranges, &cnt, &bytecnt, &de) !=
55692c23cb7cSEd Maste 	    DW_DLV_OK)
55702c23cb7cSEd Maste 		return;
55712c23cb7cSEd Maste 
55722c23cb7cSEd Maste 	printf("Contents of the .debug_ranges section:\n\n");
55732c23cb7cSEd Maste 	if (re->ec == ELFCLASS32)
55742c23cb7cSEd Maste 		printf("    %-8s %-8s %s\n", "Offset", "Begin", "End");
55752c23cb7cSEd Maste 	else
55762c23cb7cSEd Maste 		printf("    %-8s %-16s %s\n", "Offset", "Begin", "End");
55772c23cb7cSEd Maste 
55782c23cb7cSEd Maste 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
55792c23cb7cSEd Maste 	    NULL, &de)) == DW_DLV_OK) {
55802c23cb7cSEd Maste 		die = NULL;
55812c23cb7cSEd Maste 		if (dwarf_siblingof(re->dbg, die, &die, &de) != DW_DLV_OK)
55822c23cb7cSEd Maste 			continue;
55832c23cb7cSEd Maste 		if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
55842c23cb7cSEd Maste 			warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
55852c23cb7cSEd Maste 			continue;
55862c23cb7cSEd Maste 		}
55872c23cb7cSEd Maste 		/* XXX: What about DW_TAG_partial_unit? */
55882c23cb7cSEd Maste 		lowpc = 0;
55892c23cb7cSEd Maste 		if (tag == DW_TAG_compile_unit) {
55902c23cb7cSEd Maste 			if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lowpc,
55912c23cb7cSEd Maste 			    &de) != DW_DLV_OK)
55922c23cb7cSEd Maste 				lowpc = 0;
55932c23cb7cSEd Maste 		}
55942c23cb7cSEd Maste 
55952c23cb7cSEd Maste 		dump_dwarf_ranges_foreach(re, die, (Dwarf_Addr) lowpc);
55962c23cb7cSEd Maste 	}
55972c23cb7cSEd Maste 	putchar('\n');
55982c23cb7cSEd Maste }
55992c23cb7cSEd Maste 
56002c23cb7cSEd Maste static void
56012c23cb7cSEd Maste dump_dwarf_macinfo(struct readelf *re)
56022c23cb7cSEd Maste {
56032c23cb7cSEd Maste 	Dwarf_Unsigned offset;
56042c23cb7cSEd Maste 	Dwarf_Signed cnt;
56052c23cb7cSEd Maste 	Dwarf_Macro_Details *md;
56062c23cb7cSEd Maste 	Dwarf_Error de;
56072c23cb7cSEd Maste 	const char *mi_str;
5608cf781b2eSEd Maste 	char unk_mi[32];
56092c23cb7cSEd Maste 	int i;
56102c23cb7cSEd Maste 
56112c23cb7cSEd Maste #define	_MAX_MACINFO_ENTRY	65535
56122c23cb7cSEd Maste 
56132c23cb7cSEd Maste 	printf("\nContents of section .debug_macinfo:\n\n");
56142c23cb7cSEd Maste 
56152c23cb7cSEd Maste 	offset = 0;
56162c23cb7cSEd Maste 	while (dwarf_get_macro_details(re->dbg, offset, _MAX_MACINFO_ENTRY,
56172c23cb7cSEd Maste 	    &cnt, &md, &de) == DW_DLV_OK) {
56182c23cb7cSEd Maste 		for (i = 0; i < cnt; i++) {
56192c23cb7cSEd Maste 			offset = md[i].dmd_offset + 1;
56202c23cb7cSEd Maste 			if (md[i].dmd_type == 0)
56212c23cb7cSEd Maste 				break;
56222c23cb7cSEd Maste 			if (dwarf_get_MACINFO_name(md[i].dmd_type, &mi_str) !=
56232c23cb7cSEd Maste 			    DW_DLV_OK) {
5624cf781b2eSEd Maste 				snprintf(unk_mi, sizeof(unk_mi),
5625cf781b2eSEd Maste 				    "[Unknown MACINFO: %#x]", md[i].dmd_type);
5626cf781b2eSEd Maste 				mi_str = unk_mi;
56272c23cb7cSEd Maste 			}
56282c23cb7cSEd Maste 			printf(" %s", mi_str);
56292c23cb7cSEd Maste 			switch (md[i].dmd_type) {
56302c23cb7cSEd Maste 			case DW_MACINFO_define:
56312c23cb7cSEd Maste 			case DW_MACINFO_undef:
56322c23cb7cSEd Maste 				printf(" - lineno : %jd macro : %s\n",
56332c23cb7cSEd Maste 				    (intmax_t) md[i].dmd_lineno,
56342c23cb7cSEd Maste 				    md[i].dmd_macro);
56352c23cb7cSEd Maste 				break;
56362c23cb7cSEd Maste 			case DW_MACINFO_start_file:
56372c23cb7cSEd Maste 				printf(" - lineno : %jd filenum : %jd\n",
56382c23cb7cSEd Maste 				    (intmax_t) md[i].dmd_lineno,
56392c23cb7cSEd Maste 				    (intmax_t) md[i].dmd_fileindex);
56402c23cb7cSEd Maste 				break;
56412c23cb7cSEd Maste 			default:
56422c23cb7cSEd Maste 				putchar('\n');
56432c23cb7cSEd Maste 				break;
56442c23cb7cSEd Maste 			}
56452c23cb7cSEd Maste 		}
56462c23cb7cSEd Maste 	}
56472c23cb7cSEd Maste 
56482c23cb7cSEd Maste #undef	_MAX_MACINFO_ENTRY
56492c23cb7cSEd Maste }
56502c23cb7cSEd Maste 
56512c23cb7cSEd Maste static void
5652cf781b2eSEd Maste dump_dwarf_frame_inst(struct readelf *re, Dwarf_Cie cie, uint8_t *insts,
5653cf781b2eSEd Maste     Dwarf_Unsigned len, Dwarf_Unsigned caf, Dwarf_Signed daf, Dwarf_Addr pc,
5654cf781b2eSEd Maste     Dwarf_Debug dbg)
56552c23cb7cSEd Maste {
56562c23cb7cSEd Maste 	Dwarf_Frame_Op *oplist;
56572c23cb7cSEd Maste 	Dwarf_Signed opcnt, delta;
56582c23cb7cSEd Maste 	Dwarf_Small op;
56592c23cb7cSEd Maste 	Dwarf_Error de;
56602c23cb7cSEd Maste 	const char *op_str;
5661cf781b2eSEd Maste 	char unk_op[32];
56622c23cb7cSEd Maste 	int i;
56632c23cb7cSEd Maste 
56642c23cb7cSEd Maste 	if (dwarf_expand_frame_instructions(cie, insts, len, &oplist,
56652c23cb7cSEd Maste 	    &opcnt, &de) != DW_DLV_OK) {
56662c23cb7cSEd Maste 		warnx("dwarf_expand_frame_instructions failed: %s",
56672c23cb7cSEd Maste 		    dwarf_errmsg(de));
56682c23cb7cSEd Maste 		return;
56692c23cb7cSEd Maste 	}
56702c23cb7cSEd Maste 
56712c23cb7cSEd Maste 	for (i = 0; i < opcnt; i++) {
56722c23cb7cSEd Maste 		if (oplist[i].fp_base_op != 0)
56732c23cb7cSEd Maste 			op = oplist[i].fp_base_op << 6;
56742c23cb7cSEd Maste 		else
56752c23cb7cSEd Maste 			op = oplist[i].fp_extended_op;
56762c23cb7cSEd Maste 		if (dwarf_get_CFA_name(op, &op_str) != DW_DLV_OK) {
5677cf781b2eSEd Maste 			snprintf(unk_op, sizeof(unk_op), "[Unknown CFA: %#x]",
5678cf781b2eSEd Maste 			    op);
5679cf781b2eSEd Maste 			op_str = unk_op;
56802c23cb7cSEd Maste 		}
56812c23cb7cSEd Maste 		printf("  %s", op_str);
56822c23cb7cSEd Maste 		switch (op) {
56832c23cb7cSEd Maste 		case DW_CFA_advance_loc:
56842c23cb7cSEd Maste 			delta = oplist[i].fp_offset * caf;
56852c23cb7cSEd Maste 			pc += delta;
56862c23cb7cSEd Maste 			printf(": %ju to %08jx", (uintmax_t) delta,
56872c23cb7cSEd Maste 			    (uintmax_t) pc);
56882c23cb7cSEd Maste 			break;
56892c23cb7cSEd Maste 		case DW_CFA_offset:
56902c23cb7cSEd Maste 		case DW_CFA_offset_extended:
56912c23cb7cSEd Maste 		case DW_CFA_offset_extended_sf:
56922c23cb7cSEd Maste 			delta = oplist[i].fp_offset * daf;
5693cf781b2eSEd Maste 			printf(": r%u (%s) at cfa%+jd", oplist[i].fp_register,
5694cf781b2eSEd Maste 			    dwarf_regname(re, oplist[i].fp_register),
56952c23cb7cSEd Maste 			    (intmax_t) delta);
56962c23cb7cSEd Maste 			break;
56972c23cb7cSEd Maste 		case DW_CFA_restore:
5698cf781b2eSEd Maste 			printf(": r%u (%s)", oplist[i].fp_register,
5699cf781b2eSEd Maste 			    dwarf_regname(re, oplist[i].fp_register));
57002c23cb7cSEd Maste 			break;
57012c23cb7cSEd Maste 		case DW_CFA_set_loc:
57022c23cb7cSEd Maste 			pc = oplist[i].fp_offset;
57032c23cb7cSEd Maste 			printf(": to %08jx", (uintmax_t) pc);
57042c23cb7cSEd Maste 			break;
57052c23cb7cSEd Maste 		case DW_CFA_advance_loc1:
57062c23cb7cSEd Maste 		case DW_CFA_advance_loc2:
57072c23cb7cSEd Maste 		case DW_CFA_advance_loc4:
57082c23cb7cSEd Maste 			pc += oplist[i].fp_offset;
57092c23cb7cSEd Maste 			printf(": %jd to %08jx", (intmax_t) oplist[i].fp_offset,
57102c23cb7cSEd Maste 			    (uintmax_t) pc);
57112c23cb7cSEd Maste 			break;
57122c23cb7cSEd Maste 		case DW_CFA_def_cfa:
5713cf781b2eSEd Maste 			printf(": r%u (%s) ofs %ju", oplist[i].fp_register,
5714cf781b2eSEd Maste 			    dwarf_regname(re, oplist[i].fp_register),
57152c23cb7cSEd Maste 			    (uintmax_t) oplist[i].fp_offset);
57162c23cb7cSEd Maste 			break;
57172c23cb7cSEd Maste 		case DW_CFA_def_cfa_sf:
5718cf781b2eSEd Maste 			printf(": r%u (%s) ofs %jd", oplist[i].fp_register,
5719cf781b2eSEd Maste 			    dwarf_regname(re, oplist[i].fp_register),
57202c23cb7cSEd Maste 			    (intmax_t) (oplist[i].fp_offset * daf));
57212c23cb7cSEd Maste 			break;
57222c23cb7cSEd Maste 		case DW_CFA_def_cfa_register:
5723cf781b2eSEd Maste 			printf(": r%u (%s)", oplist[i].fp_register,
5724cf781b2eSEd Maste 			    dwarf_regname(re, oplist[i].fp_register));
57252c23cb7cSEd Maste 			break;
57262c23cb7cSEd Maste 		case DW_CFA_def_cfa_offset:
57272c23cb7cSEd Maste 			printf(": %ju", (uintmax_t) oplist[i].fp_offset);
57282c23cb7cSEd Maste 			break;
57292c23cb7cSEd Maste 		case DW_CFA_def_cfa_offset_sf:
57302c23cb7cSEd Maste 			printf(": %jd", (intmax_t) (oplist[i].fp_offset * daf));
57312c23cb7cSEd Maste 			break;
57322c23cb7cSEd Maste 		default:
57332c23cb7cSEd Maste 			break;
57342c23cb7cSEd Maste 		}
57352c23cb7cSEd Maste 		putchar('\n');
57362c23cb7cSEd Maste 	}
57372c23cb7cSEd Maste 
57382c23cb7cSEd Maste 	dwarf_dealloc(dbg, oplist, DW_DLA_FRAME_BLOCK);
57392c23cb7cSEd Maste }
57402c23cb7cSEd Maste 
57412c23cb7cSEd Maste static char *
5742cf781b2eSEd Maste get_regoff_str(struct readelf *re, Dwarf_Half reg, Dwarf_Addr off)
57432c23cb7cSEd Maste {
57442c23cb7cSEd Maste 	static char rs[16];
57452c23cb7cSEd Maste 
57462c23cb7cSEd Maste 	if (reg == DW_FRAME_UNDEFINED_VAL || reg == DW_FRAME_REG_INITIAL_VALUE)
57472c23cb7cSEd Maste 		snprintf(rs, sizeof(rs), "%c", 'u');
57482c23cb7cSEd Maste 	else if (reg == DW_FRAME_CFA_COL)
57492c23cb7cSEd Maste 		snprintf(rs, sizeof(rs), "c%+jd", (intmax_t) off);
57502c23cb7cSEd Maste 	else
5751cf781b2eSEd Maste 		snprintf(rs, sizeof(rs), "%s%+jd", dwarf_regname(re, reg),
5752cf781b2eSEd Maste 		    (intmax_t) off);
57532c23cb7cSEd Maste 
57542c23cb7cSEd Maste 	return (rs);
57552c23cb7cSEd Maste }
57562c23cb7cSEd Maste 
57572c23cb7cSEd Maste static int
5758cf781b2eSEd Maste dump_dwarf_frame_regtable(struct readelf *re, Dwarf_Fde fde, Dwarf_Addr pc,
5759cf781b2eSEd Maste     Dwarf_Unsigned func_len, Dwarf_Half cie_ra)
57602c23cb7cSEd Maste {
57612c23cb7cSEd Maste 	Dwarf_Regtable rt;
57622c23cb7cSEd Maste 	Dwarf_Addr row_pc, end_pc, pre_pc, cur_pc;
57632c23cb7cSEd Maste 	Dwarf_Error de;
57642c23cb7cSEd Maste 	char *vec;
57652c23cb7cSEd Maste 	int i;
57662c23cb7cSEd Maste 
57672c23cb7cSEd Maste #define BIT_SET(v, n) (v[(n)>>3] |= 1U << ((n) & 7))
57682c23cb7cSEd Maste #define BIT_CLR(v, n) (v[(n)>>3] &= ~(1U << ((n) & 7)))
57692c23cb7cSEd Maste #define BIT_ISSET(v, n) (v[(n)>>3] & (1U << ((n) & 7)))
57702c23cb7cSEd Maste #define	RT(x) rt.rules[(x)]
57712c23cb7cSEd Maste 
57722c23cb7cSEd Maste 	vec = calloc((DW_REG_TABLE_SIZE + 7) / 8, 1);
57732c23cb7cSEd Maste 	if (vec == NULL)
57742c23cb7cSEd Maste 		err(EXIT_FAILURE, "calloc failed");
57752c23cb7cSEd Maste 
57762c23cb7cSEd Maste 	pre_pc = ~((Dwarf_Addr) 0);
57772c23cb7cSEd Maste 	cur_pc = pc;
57782c23cb7cSEd Maste 	end_pc = pc + func_len;
57792c23cb7cSEd Maste 	for (; cur_pc < end_pc; cur_pc++) {
57802c23cb7cSEd Maste 		if (dwarf_get_fde_info_for_all_regs(fde, cur_pc, &rt, &row_pc,
57812c23cb7cSEd Maste 		    &de) != DW_DLV_OK) {
57822c23cb7cSEd Maste 			warnx("dwarf_get_fde_info_for_all_regs failed: %s\n",
57832c23cb7cSEd Maste 			    dwarf_errmsg(de));
57842c23cb7cSEd Maste 			return (-1);
57852c23cb7cSEd Maste 		}
57862c23cb7cSEd Maste 		if (row_pc == pre_pc)
57872c23cb7cSEd Maste 			continue;
57882c23cb7cSEd Maste 		pre_pc = row_pc;
57892c23cb7cSEd Maste 		for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
57902c23cb7cSEd Maste 			if (rt.rules[i].dw_regnum != DW_FRAME_REG_INITIAL_VALUE)
57912c23cb7cSEd Maste 				BIT_SET(vec, i);
57922c23cb7cSEd Maste 		}
57932c23cb7cSEd Maste 	}
57942c23cb7cSEd Maste 
57952c23cb7cSEd Maste 	printf("   LOC   CFA      ");
57962c23cb7cSEd Maste 	for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
57972c23cb7cSEd Maste 		if (BIT_ISSET(vec, i)) {
57982c23cb7cSEd Maste 			if ((Dwarf_Half) i == cie_ra)
57992c23cb7cSEd Maste 				printf("ra   ");
5800cf781b2eSEd Maste 			else
5801cf781b2eSEd Maste 				printf("%-5s",
5802cf781b2eSEd Maste 				    dwarf_regname(re, (unsigned int) i));
58032c23cb7cSEd Maste 		}
58042c23cb7cSEd Maste 	}
58052c23cb7cSEd Maste 	putchar('\n');
58062c23cb7cSEd Maste 
58072c23cb7cSEd Maste 	pre_pc = ~((Dwarf_Addr) 0);
58082c23cb7cSEd Maste 	cur_pc = pc;
58092c23cb7cSEd Maste 	end_pc = pc + func_len;
58102c23cb7cSEd Maste 	for (; cur_pc < end_pc; cur_pc++) {
58112c23cb7cSEd Maste 		if (dwarf_get_fde_info_for_all_regs(fde, cur_pc, &rt, &row_pc,
58122c23cb7cSEd Maste 		    &de) != DW_DLV_OK) {
58132c23cb7cSEd Maste 			warnx("dwarf_get_fde_info_for_all_regs failed: %s\n",
58142c23cb7cSEd Maste 			    dwarf_errmsg(de));
58152c23cb7cSEd Maste 			return (-1);
58162c23cb7cSEd Maste 		}
58172c23cb7cSEd Maste 		if (row_pc == pre_pc)
58182c23cb7cSEd Maste 			continue;
58192c23cb7cSEd Maste 		pre_pc = row_pc;
58202c23cb7cSEd Maste 		printf("%08jx ", (uintmax_t) row_pc);
5821cf781b2eSEd Maste 		printf("%-8s ", get_regoff_str(re, RT(0).dw_regnum,
58222c23cb7cSEd Maste 		    RT(0).dw_offset));
58232c23cb7cSEd Maste 		for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
58242c23cb7cSEd Maste 			if (BIT_ISSET(vec, i)) {
5825cf781b2eSEd Maste 				printf("%-5s", get_regoff_str(re,
5826cf781b2eSEd Maste 				    RT(i).dw_regnum, RT(i).dw_offset));
58272c23cb7cSEd Maste 			}
58282c23cb7cSEd Maste 		}
58292c23cb7cSEd Maste 		putchar('\n');
58302c23cb7cSEd Maste 	}
58312c23cb7cSEd Maste 
58322c23cb7cSEd Maste 	free(vec);
58332c23cb7cSEd Maste 
58342c23cb7cSEd Maste 	return (0);
58352c23cb7cSEd Maste 
58362c23cb7cSEd Maste #undef	BIT_SET
58372c23cb7cSEd Maste #undef	BIT_CLR
58382c23cb7cSEd Maste #undef	BIT_ISSET
58392c23cb7cSEd Maste #undef	RT
58402c23cb7cSEd Maste }
58412c23cb7cSEd Maste 
58422c23cb7cSEd Maste static void
58432c23cb7cSEd Maste dump_dwarf_frame_section(struct readelf *re, struct section *s, int alt)
58442c23cb7cSEd Maste {
58452c23cb7cSEd Maste 	Dwarf_Cie *cie_list, cie, pre_cie;
58462c23cb7cSEd Maste 	Dwarf_Fde *fde_list, fde;
58472c23cb7cSEd Maste 	Dwarf_Off cie_offset, fde_offset;
58482c23cb7cSEd Maste 	Dwarf_Unsigned cie_length, fde_instlen;
58492c23cb7cSEd Maste 	Dwarf_Unsigned cie_caf, cie_daf, cie_instlen, func_len, fde_length;
58502c23cb7cSEd Maste 	Dwarf_Signed cie_count, fde_count, cie_index;
58512c23cb7cSEd Maste 	Dwarf_Addr low_pc;
58522c23cb7cSEd Maste 	Dwarf_Half cie_ra;
58532c23cb7cSEd Maste 	Dwarf_Small cie_version;
58542c23cb7cSEd Maste 	Dwarf_Ptr fde_addr, fde_inst, cie_inst;
58552c23cb7cSEd Maste 	char *cie_aug, c;
58562c23cb7cSEd Maste 	int i, eh_frame;
58572c23cb7cSEd Maste 	Dwarf_Error de;
58582c23cb7cSEd Maste 
58592c23cb7cSEd Maste 	printf("\nThe section %s contains:\n\n", s->name);
58602c23cb7cSEd Maste 
58612c23cb7cSEd Maste 	if (!strcmp(s->name, ".debug_frame")) {
58622c23cb7cSEd Maste 		eh_frame = 0;
58632c23cb7cSEd Maste 		if (dwarf_get_fde_list(re->dbg, &cie_list, &cie_count,
58642c23cb7cSEd Maste 		    &fde_list, &fde_count, &de) != DW_DLV_OK) {
58652c23cb7cSEd Maste 			warnx("dwarf_get_fde_list failed: %s",
58662c23cb7cSEd Maste 			    dwarf_errmsg(de));
58672c23cb7cSEd Maste 			return;
58682c23cb7cSEd Maste 		}
58692c23cb7cSEd Maste 	} else if (!strcmp(s->name, ".eh_frame")) {
58702c23cb7cSEd Maste 		eh_frame = 1;
58712c23cb7cSEd Maste 		if (dwarf_get_fde_list_eh(re->dbg, &cie_list, &cie_count,
58722c23cb7cSEd Maste 		    &fde_list, &fde_count, &de) != DW_DLV_OK) {
58732c23cb7cSEd Maste 			warnx("dwarf_get_fde_list_eh failed: %s",
58742c23cb7cSEd Maste 			    dwarf_errmsg(de));
58752c23cb7cSEd Maste 			return;
58762c23cb7cSEd Maste 		}
58772c23cb7cSEd Maste 	} else
58782c23cb7cSEd Maste 		return;
58792c23cb7cSEd Maste 
58802c23cb7cSEd Maste 	pre_cie = NULL;
58812c23cb7cSEd Maste 	for (i = 0; i < fde_count; i++) {
58822c23cb7cSEd Maste 		if (dwarf_get_fde_n(fde_list, i, &fde, &de) != DW_DLV_OK) {
58832c23cb7cSEd Maste 			warnx("dwarf_get_fde_n failed: %s", dwarf_errmsg(de));
58842c23cb7cSEd Maste 			continue;
58852c23cb7cSEd Maste 		}
58862c23cb7cSEd Maste 		if (dwarf_get_cie_of_fde(fde, &cie, &de) != DW_DLV_OK) {
58872c23cb7cSEd Maste 			warnx("dwarf_get_fde_n failed: %s", dwarf_errmsg(de));
58882c23cb7cSEd Maste 			continue;
58892c23cb7cSEd Maste 		}
58902c23cb7cSEd Maste 		if (dwarf_get_fde_range(fde, &low_pc, &func_len, &fde_addr,
58912c23cb7cSEd Maste 		    &fde_length, &cie_offset, &cie_index, &fde_offset,
58922c23cb7cSEd Maste 		    &de) != DW_DLV_OK) {
58932c23cb7cSEd Maste 			warnx("dwarf_get_fde_range failed: %s",
58942c23cb7cSEd Maste 			    dwarf_errmsg(de));
58952c23cb7cSEd Maste 			continue;
58962c23cb7cSEd Maste 		}
58972c23cb7cSEd Maste 		if (dwarf_get_fde_instr_bytes(fde, &fde_inst, &fde_instlen,
58982c23cb7cSEd Maste 		    &de) != DW_DLV_OK) {
58992c23cb7cSEd Maste 			warnx("dwarf_get_fde_instr_bytes failed: %s",
59002c23cb7cSEd Maste 			    dwarf_errmsg(de));
59012c23cb7cSEd Maste 			continue;
59022c23cb7cSEd Maste 		}
59032c23cb7cSEd Maste 		if (pre_cie == NULL || cie != pre_cie) {
59042c23cb7cSEd Maste 			pre_cie = cie;
59052c23cb7cSEd Maste 			if (dwarf_get_cie_info(cie, &cie_length, &cie_version,
59062c23cb7cSEd Maste 			    &cie_aug, &cie_caf, &cie_daf, &cie_ra,
59072c23cb7cSEd Maste 			    &cie_inst, &cie_instlen, &de) != DW_DLV_OK) {
59082c23cb7cSEd Maste 				warnx("dwarf_get_cie_info failed: %s",
59092c23cb7cSEd Maste 				    dwarf_errmsg(de));
59102c23cb7cSEd Maste 				continue;
59112c23cb7cSEd Maste 			}
59122c23cb7cSEd Maste 			printf("%08jx %08jx %8.8jx CIE",
59132c23cb7cSEd Maste 			    (uintmax_t) cie_offset,
59142c23cb7cSEd Maste 			    (uintmax_t) cie_length,
59152c23cb7cSEd Maste 			    (uintmax_t) (eh_frame ? 0 : ~0U));
59162c23cb7cSEd Maste 			if (!alt) {
59172c23cb7cSEd Maste 				putchar('\n');
59182c23cb7cSEd Maste 				printf("  Version:\t\t\t%u\n", cie_version);
59192c23cb7cSEd Maste 				printf("  Augmentation:\t\t\t\"");
59202c23cb7cSEd Maste 				while ((c = *cie_aug++) != '\0')
59212c23cb7cSEd Maste 					putchar(c);
59222c23cb7cSEd Maste 				printf("\"\n");
59232c23cb7cSEd Maste 				printf("  Code alignment factor:\t%ju\n",
59242c23cb7cSEd Maste 				    (uintmax_t) cie_caf);
59252c23cb7cSEd Maste 				printf("  Data alignment factor:\t%jd\n",
59262c23cb7cSEd Maste 				    (intmax_t) cie_daf);
59272c23cb7cSEd Maste 				printf("  Return address column:\t%ju\n",
59282c23cb7cSEd Maste 				    (uintmax_t) cie_ra);
59292c23cb7cSEd Maste 				putchar('\n');
5930cf781b2eSEd Maste 				dump_dwarf_frame_inst(re, cie, cie_inst,
59312c23cb7cSEd Maste 				    cie_instlen, cie_caf, cie_daf, 0,
59322c23cb7cSEd Maste 				    re->dbg);
59332c23cb7cSEd Maste 				putchar('\n');
59342c23cb7cSEd Maste 			} else {
59352c23cb7cSEd Maste 				printf(" \"");
59362c23cb7cSEd Maste 				while ((c = *cie_aug++) != '\0')
59372c23cb7cSEd Maste 					putchar(c);
59382c23cb7cSEd Maste 				putchar('"');
59392c23cb7cSEd Maste 				printf(" cf=%ju df=%jd ra=%ju\n",
59402c23cb7cSEd Maste 				    (uintmax_t) cie_caf,
59412c23cb7cSEd Maste 				    (uintmax_t) cie_daf,
59422c23cb7cSEd Maste 				    (uintmax_t) cie_ra);
5943cf781b2eSEd Maste 				dump_dwarf_frame_regtable(re, fde, low_pc, 1,
59442c23cb7cSEd Maste 				    cie_ra);
59452c23cb7cSEd Maste 				putchar('\n');
59462c23cb7cSEd Maste 			}
59472c23cb7cSEd Maste 		}
59482c23cb7cSEd Maste 		printf("%08jx %08jx %08jx FDE cie=%08jx pc=%08jx..%08jx\n",
59492c23cb7cSEd Maste 		    (uintmax_t) fde_offset, (uintmax_t) fde_length,
59502c23cb7cSEd Maste 		    (uintmax_t) cie_offset,
59512c23cb7cSEd Maste 		    (uintmax_t) (eh_frame ? fde_offset + 4 - cie_offset :
59522c23cb7cSEd Maste 			cie_offset),
59532c23cb7cSEd Maste 		    (uintmax_t) low_pc, (uintmax_t) (low_pc + func_len));
59542c23cb7cSEd Maste 		if (!alt)
5955cf781b2eSEd Maste 			dump_dwarf_frame_inst(re, cie, fde_inst, fde_instlen,
59562c23cb7cSEd Maste 			    cie_caf, cie_daf, low_pc, re->dbg);
59572c23cb7cSEd Maste 		else
5958cf781b2eSEd Maste 			dump_dwarf_frame_regtable(re, fde, low_pc, func_len,
59592c23cb7cSEd Maste 			    cie_ra);
59602c23cb7cSEd Maste 		putchar('\n');
59612c23cb7cSEd Maste 	}
59622c23cb7cSEd Maste }
59632c23cb7cSEd Maste 
59642c23cb7cSEd Maste static void
59652c23cb7cSEd Maste dump_dwarf_frame(struct readelf *re, int alt)
59662c23cb7cSEd Maste {
59672c23cb7cSEd Maste 	struct section *s;
59682c23cb7cSEd Maste 	int i;
59692c23cb7cSEd Maste 
59702c23cb7cSEd Maste 	(void) dwarf_set_frame_cfa_value(re->dbg, DW_FRAME_CFA_COL);
59712c23cb7cSEd Maste 
59722c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
59732c23cb7cSEd Maste 		s = &re->sl[i];
59742c23cb7cSEd Maste 		if (s->name != NULL && (!strcmp(s->name, ".debug_frame") ||
59752c23cb7cSEd Maste 		    !strcmp(s->name, ".eh_frame")))
59762c23cb7cSEd Maste 			dump_dwarf_frame_section(re, s, alt);
59772c23cb7cSEd Maste 	}
59782c23cb7cSEd Maste }
59792c23cb7cSEd Maste 
59802c23cb7cSEd Maste static void
59812c23cb7cSEd Maste dump_dwarf_str(struct readelf *re)
59822c23cb7cSEd Maste {
59832c23cb7cSEd Maste 	struct section *s;
59842c23cb7cSEd Maste 	Elf_Data *d;
59852c23cb7cSEd Maste 	unsigned char *p;
59862c23cb7cSEd Maste 	int elferr, end, i, j;
59872c23cb7cSEd Maste 
59882c23cb7cSEd Maste 	printf("\nContents of section .debug_str:\n");
59892c23cb7cSEd Maste 
59902c23cb7cSEd Maste 	s = NULL;
59912c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
59922c23cb7cSEd Maste 		s = &re->sl[i];
59932c23cb7cSEd Maste 		if (s->name != NULL && !strcmp(s->name, ".debug_str"))
59942c23cb7cSEd Maste 			break;
59952c23cb7cSEd Maste 	}
59962c23cb7cSEd Maste 	if ((size_t) i >= re->shnum)
59972c23cb7cSEd Maste 		return;
59982c23cb7cSEd Maste 
59992c23cb7cSEd Maste 	(void) elf_errno();
60002c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
60012c23cb7cSEd Maste 		elferr = elf_errno();
60022c23cb7cSEd Maste 		if (elferr != 0)
60032c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
60042c23cb7cSEd Maste 		return;
60052c23cb7cSEd Maste 	}
60062c23cb7cSEd Maste 	if (d->d_size <= 0)
60072c23cb7cSEd Maste 		return;
60082c23cb7cSEd Maste 
60092c23cb7cSEd Maste 	for (i = 0, p = d->d_buf; (size_t) i < d->d_size; i += 16) {
60102c23cb7cSEd Maste 		printf("  0x%08x", (unsigned int) i);
60112c23cb7cSEd Maste 		if ((size_t) i + 16 > d->d_size)
60122c23cb7cSEd Maste 			end = d->d_size;
60132c23cb7cSEd Maste 		else
60142c23cb7cSEd Maste 			end = i + 16;
60152c23cb7cSEd Maste 		for (j = i; j < i + 16; j++) {
60162c23cb7cSEd Maste 			if ((j - i) % 4 == 0)
60172c23cb7cSEd Maste 				putchar(' ');
60182c23cb7cSEd Maste 			if (j >= end) {
60192c23cb7cSEd Maste 				printf("  ");
60202c23cb7cSEd Maste 				continue;
60212c23cb7cSEd Maste 			}
60222c23cb7cSEd Maste 			printf("%02x", (uint8_t) p[j]);
60232c23cb7cSEd Maste 		}
60242c23cb7cSEd Maste 		putchar(' ');
60252c23cb7cSEd Maste 		for (j = i; j < end; j++) {
60262c23cb7cSEd Maste 			if (isprint(p[j]))
60272c23cb7cSEd Maste 				putchar(p[j]);
60282c23cb7cSEd Maste 			else if (p[j] == 0)
60292c23cb7cSEd Maste 				putchar('.');
60302c23cb7cSEd Maste 			else
60312c23cb7cSEd Maste 				putchar(' ');
60322c23cb7cSEd Maste 		}
60332c23cb7cSEd Maste 		putchar('\n');
60342c23cb7cSEd Maste 	}
60352c23cb7cSEd Maste }
60362c23cb7cSEd Maste 
60372c23cb7cSEd Maste struct loc_at {
60382c23cb7cSEd Maste 	Dwarf_Attribute la_at;
60392c23cb7cSEd Maste 	Dwarf_Unsigned la_off;
60402c23cb7cSEd Maste 	Dwarf_Unsigned la_lowpc;
6041cf781b2eSEd Maste 	Dwarf_Half la_cu_psize;
6042cf781b2eSEd Maste 	Dwarf_Half la_cu_osize;
6043cf781b2eSEd Maste 	Dwarf_Half la_cu_ver;
60442c23cb7cSEd Maste 	TAILQ_ENTRY(loc_at) la_next;
60452c23cb7cSEd Maste };
60462c23cb7cSEd Maste 
60472c23cb7cSEd Maste static TAILQ_HEAD(, loc_at) lalist = TAILQ_HEAD_INITIALIZER(lalist);
60482c23cb7cSEd Maste 
60492c23cb7cSEd Maste static void
60502c23cb7cSEd Maste search_loclist_at(struct readelf *re, Dwarf_Die die, Dwarf_Unsigned lowpc)
60512c23cb7cSEd Maste {
60522c23cb7cSEd Maste 	Dwarf_Attribute *attr_list;
60532c23cb7cSEd Maste 	Dwarf_Die ret_die;
60542c23cb7cSEd Maste 	Dwarf_Unsigned off;
6055cf781b2eSEd Maste 	Dwarf_Off ref;
60562c23cb7cSEd Maste 	Dwarf_Signed attr_count;
60572c23cb7cSEd Maste 	Dwarf_Half attr, form;
6058cf781b2eSEd Maste 	Dwarf_Bool is_info;
60592c23cb7cSEd Maste 	Dwarf_Error de;
60602c23cb7cSEd Maste 	struct loc_at *la, *nla;
60612c23cb7cSEd Maste 	int i, ret;
60622c23cb7cSEd Maste 
6063cf781b2eSEd Maste 	is_info = dwarf_get_die_infotypes_flag(die);
6064cf781b2eSEd Maste 
60652c23cb7cSEd Maste 	if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
60662c23cb7cSEd Maste 	    DW_DLV_OK) {
60672c23cb7cSEd Maste 		if (ret == DW_DLV_ERROR)
60682c23cb7cSEd Maste 			warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
60692c23cb7cSEd Maste 		goto cont_search;
60702c23cb7cSEd Maste 	}
60712c23cb7cSEd Maste 	for (i = 0; i < attr_count; i++) {
60722c23cb7cSEd Maste 		if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
60732c23cb7cSEd Maste 			warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
60742c23cb7cSEd Maste 			continue;
60752c23cb7cSEd Maste 		}
60762c23cb7cSEd Maste 		if (attr != DW_AT_location &&
60772c23cb7cSEd Maste 		    attr != DW_AT_string_length &&
60782c23cb7cSEd Maste 		    attr != DW_AT_return_addr &&
60792c23cb7cSEd Maste 		    attr != DW_AT_data_member_location &&
60802c23cb7cSEd Maste 		    attr != DW_AT_frame_base &&
60812c23cb7cSEd Maste 		    attr != DW_AT_segment &&
60822c23cb7cSEd Maste 		    attr != DW_AT_static_link &&
60832c23cb7cSEd Maste 		    attr != DW_AT_use_location &&
60842c23cb7cSEd Maste 		    attr != DW_AT_vtable_elem_location)
60852c23cb7cSEd Maste 			continue;
60862c23cb7cSEd Maste 		if (dwarf_whatform(attr_list[i], &form, &de) != DW_DLV_OK) {
60872c23cb7cSEd Maste 			warnx("dwarf_whatform failed: %s", dwarf_errmsg(de));
60882c23cb7cSEd Maste 			continue;
60892c23cb7cSEd Maste 		}
6090cf781b2eSEd Maste 		if (form == DW_FORM_data4 || form == DW_FORM_data8) {
6091cf781b2eSEd Maste 			if (dwarf_formudata(attr_list[i], &off, &de) !=
6092cf781b2eSEd Maste 			    DW_DLV_OK) {
6093cf781b2eSEd Maste 				warnx("dwarf_formudata failed: %s",
6094cf781b2eSEd Maste 				    dwarf_errmsg(de));
60952c23cb7cSEd Maste 				continue;
60962c23cb7cSEd Maste 			}
6097cf781b2eSEd Maste 		} else if (form == DW_FORM_sec_offset) {
6098cf781b2eSEd Maste 			if (dwarf_global_formref(attr_list[i], &ref, &de) !=
6099cf781b2eSEd Maste 			    DW_DLV_OK) {
6100cf781b2eSEd Maste 				warnx("dwarf_global_formref failed: %s",
6101cf781b2eSEd Maste 				    dwarf_errmsg(de));
6102cf781b2eSEd Maste 				continue;
6103cf781b2eSEd Maste 			}
6104cf781b2eSEd Maste 			off = ref;
6105cf781b2eSEd Maste 		} else
6106cf781b2eSEd Maste 			continue;
6107cf781b2eSEd Maste 
61082c23cb7cSEd Maste 		TAILQ_FOREACH(la, &lalist, la_next) {
61092c23cb7cSEd Maste 			if (off == la->la_off)
61102c23cb7cSEd Maste 				break;
61112c23cb7cSEd Maste 			if (off < la->la_off) {
61122c23cb7cSEd Maste 				if ((nla = malloc(sizeof(*nla))) == NULL)
61132c23cb7cSEd Maste 					err(EXIT_FAILURE, "malloc failed");
61142c23cb7cSEd Maste 				nla->la_at = attr_list[i];
61152c23cb7cSEd Maste 				nla->la_off = off;
61162c23cb7cSEd Maste 				nla->la_lowpc = lowpc;
6117cf781b2eSEd Maste 				nla->la_cu_psize = re->cu_psize;
6118cf781b2eSEd Maste 				nla->la_cu_osize = re->cu_osize;
6119cf781b2eSEd Maste 				nla->la_cu_ver = re->cu_ver;
61202c23cb7cSEd Maste 				TAILQ_INSERT_BEFORE(la, nla, la_next);
61212c23cb7cSEd Maste 				break;
61222c23cb7cSEd Maste 			}
61232c23cb7cSEd Maste 		}
61242c23cb7cSEd Maste 		if (la == NULL) {
61252c23cb7cSEd Maste 			if ((nla = malloc(sizeof(*nla))) == NULL)
61262c23cb7cSEd Maste 				err(EXIT_FAILURE, "malloc failed");
61272c23cb7cSEd Maste 			nla->la_at = attr_list[i];
61282c23cb7cSEd Maste 			nla->la_off = off;
61292c23cb7cSEd Maste 			nla->la_lowpc = lowpc;
6130cf781b2eSEd Maste 			nla->la_cu_psize = re->cu_psize;
6131cf781b2eSEd Maste 			nla->la_cu_osize = re->cu_osize;
6132cf781b2eSEd Maste 			nla->la_cu_ver = re->cu_ver;
61332c23cb7cSEd Maste 			TAILQ_INSERT_TAIL(&lalist, nla, la_next);
61342c23cb7cSEd Maste 		}
61352c23cb7cSEd Maste 	}
61362c23cb7cSEd Maste 
61372c23cb7cSEd Maste cont_search:
61382c23cb7cSEd Maste 	/* Search children. */
61392c23cb7cSEd Maste 	ret = dwarf_child(die, &ret_die, &de);
61402c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
61412c23cb7cSEd Maste 		warnx("dwarf_child: %s", dwarf_errmsg(de));
61422c23cb7cSEd Maste 	else if (ret == DW_DLV_OK)
61432c23cb7cSEd Maste 		search_loclist_at(re, ret_die, lowpc);
61442c23cb7cSEd Maste 
61452c23cb7cSEd Maste 	/* Search sibling. */
6146cf781b2eSEd Maste 	ret = dwarf_siblingof_b(re->dbg, die, &ret_die, is_info, &de);
61472c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
61482c23cb7cSEd Maste 		warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
61492c23cb7cSEd Maste 	else if (ret == DW_DLV_OK)
61502c23cb7cSEd Maste 		search_loclist_at(re, ret_die, lowpc);
61512c23cb7cSEd Maste }
61522c23cb7cSEd Maste 
61532c23cb7cSEd Maste static void
6154cf781b2eSEd Maste dump_dwarf_loc(struct readelf *re, Dwarf_Loc *lr)
61552c23cb7cSEd Maste {
61562c23cb7cSEd Maste 	const char *op_str;
6157cf781b2eSEd Maste 	char unk_op[32];
6158cf781b2eSEd Maste 	uint8_t *b, n;
6159cf781b2eSEd Maste 	int i;
61602c23cb7cSEd Maste 
61612c23cb7cSEd Maste 	if (dwarf_get_OP_name(lr->lr_atom, &op_str) !=
61622c23cb7cSEd Maste 	    DW_DLV_OK) {
6163cf781b2eSEd Maste 		snprintf(unk_op, sizeof(unk_op),
6164cf781b2eSEd Maste 		    "[Unknown OP: %#x]", lr->lr_atom);
6165cf781b2eSEd Maste 		op_str = unk_op;
61662c23cb7cSEd Maste 	}
61672c23cb7cSEd Maste 
61682c23cb7cSEd Maste 	printf("%s", op_str);
61692c23cb7cSEd Maste 
61702c23cb7cSEd Maste 	switch (lr->lr_atom) {
61712c23cb7cSEd Maste 	case DW_OP_reg0:
61722c23cb7cSEd Maste 	case DW_OP_reg1:
61732c23cb7cSEd Maste 	case DW_OP_reg2:
61742c23cb7cSEd Maste 	case DW_OP_reg3:
61752c23cb7cSEd Maste 	case DW_OP_reg4:
61762c23cb7cSEd Maste 	case DW_OP_reg5:
61772c23cb7cSEd Maste 	case DW_OP_reg6:
61782c23cb7cSEd Maste 	case DW_OP_reg7:
61792c23cb7cSEd Maste 	case DW_OP_reg8:
61802c23cb7cSEd Maste 	case DW_OP_reg9:
61812c23cb7cSEd Maste 	case DW_OP_reg10:
61822c23cb7cSEd Maste 	case DW_OP_reg11:
61832c23cb7cSEd Maste 	case DW_OP_reg12:
61842c23cb7cSEd Maste 	case DW_OP_reg13:
61852c23cb7cSEd Maste 	case DW_OP_reg14:
61862c23cb7cSEd Maste 	case DW_OP_reg15:
61872c23cb7cSEd Maste 	case DW_OP_reg16:
61882c23cb7cSEd Maste 	case DW_OP_reg17:
61892c23cb7cSEd Maste 	case DW_OP_reg18:
61902c23cb7cSEd Maste 	case DW_OP_reg19:
61912c23cb7cSEd Maste 	case DW_OP_reg20:
61922c23cb7cSEd Maste 	case DW_OP_reg21:
61932c23cb7cSEd Maste 	case DW_OP_reg22:
61942c23cb7cSEd Maste 	case DW_OP_reg23:
61952c23cb7cSEd Maste 	case DW_OP_reg24:
61962c23cb7cSEd Maste 	case DW_OP_reg25:
61972c23cb7cSEd Maste 	case DW_OP_reg26:
61982c23cb7cSEd Maste 	case DW_OP_reg27:
61992c23cb7cSEd Maste 	case DW_OP_reg28:
62002c23cb7cSEd Maste 	case DW_OP_reg29:
62012c23cb7cSEd Maste 	case DW_OP_reg30:
62022c23cb7cSEd Maste 	case DW_OP_reg31:
6203cf781b2eSEd Maste 		printf(" (%s)", dwarf_regname(re, lr->lr_atom - DW_OP_reg0));
6204cf781b2eSEd Maste 		break;
6205cf781b2eSEd Maste 
6206cf781b2eSEd Maste 	case DW_OP_deref:
62072c23cb7cSEd Maste 	case DW_OP_lit0:
62082c23cb7cSEd Maste 	case DW_OP_lit1:
62092c23cb7cSEd Maste 	case DW_OP_lit2:
62102c23cb7cSEd Maste 	case DW_OP_lit3:
62112c23cb7cSEd Maste 	case DW_OP_lit4:
62122c23cb7cSEd Maste 	case DW_OP_lit5:
62132c23cb7cSEd Maste 	case DW_OP_lit6:
62142c23cb7cSEd Maste 	case DW_OP_lit7:
62152c23cb7cSEd Maste 	case DW_OP_lit8:
62162c23cb7cSEd Maste 	case DW_OP_lit9:
62172c23cb7cSEd Maste 	case DW_OP_lit10:
62182c23cb7cSEd Maste 	case DW_OP_lit11:
62192c23cb7cSEd Maste 	case DW_OP_lit12:
62202c23cb7cSEd Maste 	case DW_OP_lit13:
62212c23cb7cSEd Maste 	case DW_OP_lit14:
62222c23cb7cSEd Maste 	case DW_OP_lit15:
62232c23cb7cSEd Maste 	case DW_OP_lit16:
62242c23cb7cSEd Maste 	case DW_OP_lit17:
62252c23cb7cSEd Maste 	case DW_OP_lit18:
62262c23cb7cSEd Maste 	case DW_OP_lit19:
62272c23cb7cSEd Maste 	case DW_OP_lit20:
62282c23cb7cSEd Maste 	case DW_OP_lit21:
62292c23cb7cSEd Maste 	case DW_OP_lit22:
62302c23cb7cSEd Maste 	case DW_OP_lit23:
62312c23cb7cSEd Maste 	case DW_OP_lit24:
62322c23cb7cSEd Maste 	case DW_OP_lit25:
62332c23cb7cSEd Maste 	case DW_OP_lit26:
62342c23cb7cSEd Maste 	case DW_OP_lit27:
62352c23cb7cSEd Maste 	case DW_OP_lit28:
62362c23cb7cSEd Maste 	case DW_OP_lit29:
62372c23cb7cSEd Maste 	case DW_OP_lit30:
62382c23cb7cSEd Maste 	case DW_OP_lit31:
62392c23cb7cSEd Maste 	case DW_OP_dup:
62402c23cb7cSEd Maste 	case DW_OP_drop:
62412c23cb7cSEd Maste 	case DW_OP_over:
62422c23cb7cSEd Maste 	case DW_OP_swap:
62432c23cb7cSEd Maste 	case DW_OP_rot:
62442c23cb7cSEd Maste 	case DW_OP_xderef:
62452c23cb7cSEd Maste 	case DW_OP_abs:
62462c23cb7cSEd Maste 	case DW_OP_and:
62472c23cb7cSEd Maste 	case DW_OP_div:
62482c23cb7cSEd Maste 	case DW_OP_minus:
62492c23cb7cSEd Maste 	case DW_OP_mod:
62502c23cb7cSEd Maste 	case DW_OP_mul:
62512c23cb7cSEd Maste 	case DW_OP_neg:
62522c23cb7cSEd Maste 	case DW_OP_not:
62532c23cb7cSEd Maste 	case DW_OP_or:
62542c23cb7cSEd Maste 	case DW_OP_plus:
62552c23cb7cSEd Maste 	case DW_OP_shl:
62562c23cb7cSEd Maste 	case DW_OP_shr:
62572c23cb7cSEd Maste 	case DW_OP_shra:
62582c23cb7cSEd Maste 	case DW_OP_xor:
62592c23cb7cSEd Maste 	case DW_OP_eq:
62602c23cb7cSEd Maste 	case DW_OP_ge:
62612c23cb7cSEd Maste 	case DW_OP_gt:
62622c23cb7cSEd Maste 	case DW_OP_le:
62632c23cb7cSEd Maste 	case DW_OP_lt:
62642c23cb7cSEd Maste 	case DW_OP_ne:
62652c23cb7cSEd Maste 	case DW_OP_nop:
6266cf781b2eSEd Maste 	case DW_OP_push_object_address:
6267cf781b2eSEd Maste 	case DW_OP_form_tls_address:
6268cf781b2eSEd Maste 	case DW_OP_call_frame_cfa:
6269cf781b2eSEd Maste 	case DW_OP_stack_value:
6270cf781b2eSEd Maste 	case DW_OP_GNU_push_tls_address:
6271cf781b2eSEd Maste 	case DW_OP_GNU_uninit:
62722c23cb7cSEd Maste 		break;
62732c23cb7cSEd Maste 
62742c23cb7cSEd Maste 	case DW_OP_const1u:
62752c23cb7cSEd Maste 	case DW_OP_pick:
62762c23cb7cSEd Maste 	case DW_OP_deref_size:
62772c23cb7cSEd Maste 	case DW_OP_xderef_size:
62782c23cb7cSEd Maste 	case DW_OP_const2u:
62792c23cb7cSEd Maste 	case DW_OP_bra:
62802c23cb7cSEd Maste 	case DW_OP_skip:
62812c23cb7cSEd Maste 	case DW_OP_const4u:
62822c23cb7cSEd Maste 	case DW_OP_const8u:
62832c23cb7cSEd Maste 	case DW_OP_constu:
62842c23cb7cSEd Maste 	case DW_OP_plus_uconst:
62852c23cb7cSEd Maste 	case DW_OP_regx:
62862c23cb7cSEd Maste 	case DW_OP_piece:
62872c23cb7cSEd Maste 		printf(": %ju", (uintmax_t)
62882c23cb7cSEd Maste 		    lr->lr_number);
62892c23cb7cSEd Maste 		break;
62902c23cb7cSEd Maste 
6291cf781b2eSEd Maste 	case DW_OP_const1s:
6292cf781b2eSEd Maste 	case DW_OP_const2s:
6293cf781b2eSEd Maste 	case DW_OP_const4s:
6294cf781b2eSEd Maste 	case DW_OP_const8s:
62952c23cb7cSEd Maste 	case DW_OP_consts:
6296cf781b2eSEd Maste 		printf(": %jd", (intmax_t)
6297cf781b2eSEd Maste 		    lr->lr_number);
6298cf781b2eSEd Maste 		break;
6299cf781b2eSEd Maste 
63002c23cb7cSEd Maste 	case DW_OP_breg0:
63012c23cb7cSEd Maste 	case DW_OP_breg1:
63022c23cb7cSEd Maste 	case DW_OP_breg2:
63032c23cb7cSEd Maste 	case DW_OP_breg3:
63042c23cb7cSEd Maste 	case DW_OP_breg4:
63052c23cb7cSEd Maste 	case DW_OP_breg5:
63062c23cb7cSEd Maste 	case DW_OP_breg6:
63072c23cb7cSEd Maste 	case DW_OP_breg7:
63082c23cb7cSEd Maste 	case DW_OP_breg8:
63092c23cb7cSEd Maste 	case DW_OP_breg9:
63102c23cb7cSEd Maste 	case DW_OP_breg10:
63112c23cb7cSEd Maste 	case DW_OP_breg11:
63122c23cb7cSEd Maste 	case DW_OP_breg12:
63132c23cb7cSEd Maste 	case DW_OP_breg13:
63142c23cb7cSEd Maste 	case DW_OP_breg14:
63152c23cb7cSEd Maste 	case DW_OP_breg15:
63162c23cb7cSEd Maste 	case DW_OP_breg16:
63172c23cb7cSEd Maste 	case DW_OP_breg17:
63182c23cb7cSEd Maste 	case DW_OP_breg18:
63192c23cb7cSEd Maste 	case DW_OP_breg19:
63202c23cb7cSEd Maste 	case DW_OP_breg20:
63212c23cb7cSEd Maste 	case DW_OP_breg21:
63222c23cb7cSEd Maste 	case DW_OP_breg22:
63232c23cb7cSEd Maste 	case DW_OP_breg23:
63242c23cb7cSEd Maste 	case DW_OP_breg24:
63252c23cb7cSEd Maste 	case DW_OP_breg25:
63262c23cb7cSEd Maste 	case DW_OP_breg26:
63272c23cb7cSEd Maste 	case DW_OP_breg27:
63282c23cb7cSEd Maste 	case DW_OP_breg28:
63292c23cb7cSEd Maste 	case DW_OP_breg29:
63302c23cb7cSEd Maste 	case DW_OP_breg30:
63312c23cb7cSEd Maste 	case DW_OP_breg31:
6332cf781b2eSEd Maste 		printf(" (%s): %jd",
6333cf781b2eSEd Maste 		    dwarf_regname(re, lr->lr_atom - DW_OP_breg0),
6334cf781b2eSEd Maste 		    (intmax_t) lr->lr_number);
6335cf781b2eSEd Maste 		break;
6336cf781b2eSEd Maste 
63372c23cb7cSEd Maste 	case DW_OP_fbreg:
63382c23cb7cSEd Maste 		printf(": %jd", (intmax_t)
63392c23cb7cSEd Maste 		    lr->lr_number);
63402c23cb7cSEd Maste 		break;
63412c23cb7cSEd Maste 
63422c23cb7cSEd Maste 	case DW_OP_bregx:
6343cf781b2eSEd Maste 		printf(": %ju (%s) %jd",
63442c23cb7cSEd Maste 		    (uintmax_t) lr->lr_number,
6345cf781b2eSEd Maste 		    dwarf_regname(re, (unsigned int) lr->lr_number),
63462c23cb7cSEd Maste 		    (intmax_t) lr->lr_number2);
63472c23cb7cSEd Maste 		break;
63482c23cb7cSEd Maste 
63492c23cb7cSEd Maste 	case DW_OP_addr:
6350cf781b2eSEd Maste 	case DW_OP_GNU_encoded_addr:
63512c23cb7cSEd Maste 		printf(": %#jx", (uintmax_t)
63522c23cb7cSEd Maste 		    lr->lr_number);
63532c23cb7cSEd Maste 		break;
6354cf781b2eSEd Maste 
6355cf781b2eSEd Maste 	case DW_OP_GNU_implicit_pointer:
6356cf781b2eSEd Maste 		printf(": <0x%jx> %jd", (uintmax_t) lr->lr_number,
6357cf781b2eSEd Maste 		    (intmax_t) lr->lr_number2);
6358cf781b2eSEd Maste 		break;
6359cf781b2eSEd Maste 
6360cf781b2eSEd Maste 	case DW_OP_implicit_value:
6361cf781b2eSEd Maste 		printf(": %ju byte block:", (uintmax_t) lr->lr_number);
6362cf781b2eSEd Maste 		b = (uint8_t *)(uintptr_t) lr->lr_number2;
6363cf781b2eSEd Maste 		for (i = 0; (Dwarf_Unsigned) i < lr->lr_number; i++)
6364cf781b2eSEd Maste 			printf(" %x", b[i]);
6365cf781b2eSEd Maste 		break;
6366cf781b2eSEd Maste 
6367cf781b2eSEd Maste 	case DW_OP_GNU_entry_value:
6368cf781b2eSEd Maste 		printf(": (");
6369cf781b2eSEd Maste 		dump_dwarf_block(re, (uint8_t *)(uintptr_t) lr->lr_number2,
6370cf781b2eSEd Maste 		    lr->lr_number);
6371cf781b2eSEd Maste 		putchar(')');
6372cf781b2eSEd Maste 		break;
6373cf781b2eSEd Maste 
6374cf781b2eSEd Maste 	case DW_OP_GNU_const_type:
6375cf781b2eSEd Maste 		printf(": <0x%jx> ", (uintmax_t) lr->lr_number);
6376cf781b2eSEd Maste 		b = (uint8_t *)(uintptr_t) lr->lr_number2;
6377cf781b2eSEd Maste 		n = *b;
6378cf781b2eSEd Maste 		for (i = 1; (uint8_t) i < n; i++)
6379cf781b2eSEd Maste 			printf(" %x", b[i]);
6380cf781b2eSEd Maste 		break;
6381cf781b2eSEd Maste 
6382cf781b2eSEd Maste 	case DW_OP_GNU_regval_type:
6383cf781b2eSEd Maste 		printf(": %ju (%s) <0x%jx>", (uintmax_t) lr->lr_number,
6384cf781b2eSEd Maste 		    dwarf_regname(re, (unsigned int) lr->lr_number),
6385cf781b2eSEd Maste 		    (uintmax_t) lr->lr_number2);
6386cf781b2eSEd Maste 		break;
6387cf781b2eSEd Maste 
6388cf781b2eSEd Maste 	case DW_OP_GNU_convert:
6389cf781b2eSEd Maste 	case DW_OP_GNU_deref_type:
6390cf781b2eSEd Maste 	case DW_OP_GNU_parameter_ref:
6391cf781b2eSEd Maste 	case DW_OP_GNU_reinterpret:
6392cf781b2eSEd Maste 		printf(": <0x%jx>", (uintmax_t) lr->lr_number);
6393cf781b2eSEd Maste 		break;
6394cf781b2eSEd Maste 
6395cf781b2eSEd Maste 	default:
6396cf781b2eSEd Maste 		break;
63972c23cb7cSEd Maste 	}
6398cf781b2eSEd Maste }
6399cf781b2eSEd Maste 
6400cf781b2eSEd Maste static void
6401cf781b2eSEd Maste dump_dwarf_block(struct readelf *re, uint8_t *b, Dwarf_Unsigned len)
6402cf781b2eSEd Maste {
6403cf781b2eSEd Maste 	Dwarf_Locdesc *llbuf;
6404cf781b2eSEd Maste 	Dwarf_Signed lcnt;
6405cf781b2eSEd Maste 	Dwarf_Error de;
6406cf781b2eSEd Maste 	int i;
6407cf781b2eSEd Maste 
6408cf781b2eSEd Maste 	if (dwarf_loclist_from_expr_b(re->dbg, b, len, re->cu_psize,
6409cf781b2eSEd Maste 	    re->cu_osize, re->cu_ver, &llbuf, &lcnt, &de) != DW_DLV_OK) {
6410cf781b2eSEd Maste 		warnx("dwarf_loclist_form_expr_b: %s", dwarf_errmsg(de));
6411cf781b2eSEd Maste 		return;
6412cf781b2eSEd Maste 	}
6413cf781b2eSEd Maste 
6414cf781b2eSEd Maste 	for (i = 0; (Dwarf_Half) i < llbuf->ld_cents; i++) {
6415cf781b2eSEd Maste 		dump_dwarf_loc(re, &llbuf->ld_s[i]);
6416cf781b2eSEd Maste 		if (i < llbuf->ld_cents - 1)
6417cf781b2eSEd Maste 			printf("; ");
6418cf781b2eSEd Maste 	}
6419cf781b2eSEd Maste 
6420cf781b2eSEd Maste 	dwarf_dealloc(re->dbg, llbuf->ld_s, DW_DLA_LOC_BLOCK);
6421cf781b2eSEd Maste 	dwarf_dealloc(re->dbg, llbuf, DW_DLA_LOCDESC);
6422cf781b2eSEd Maste }
6423cf781b2eSEd Maste 
6424cf781b2eSEd Maste static void
6425cf781b2eSEd Maste dump_dwarf_loclist(struct readelf *re)
6426cf781b2eSEd Maste {
6427cf781b2eSEd Maste 	Dwarf_Die die;
6428cf781b2eSEd Maste 	Dwarf_Locdesc **llbuf;
6429cf781b2eSEd Maste 	Dwarf_Unsigned lowpc;
6430cf781b2eSEd Maste 	Dwarf_Signed lcnt;
6431cf781b2eSEd Maste 	Dwarf_Half tag, version, pointer_size, off_size;
6432cf781b2eSEd Maste 	Dwarf_Error de;
6433cf781b2eSEd Maste 	struct loc_at *la;
6434bee2765cSEd Maste 	int i, j, ret, has_content;
6435cf781b2eSEd Maste 
6436cf781b2eSEd Maste 	/* Search .debug_info section. */
6437cf781b2eSEd Maste 	while ((ret = dwarf_next_cu_header_b(re->dbg, NULL, &version, NULL,
6438cf781b2eSEd Maste 	    &pointer_size, &off_size, NULL, NULL, &de)) == DW_DLV_OK) {
6439cf781b2eSEd Maste 		set_cu_context(re, pointer_size, off_size, version);
6440cf781b2eSEd Maste 		die = NULL;
6441cf781b2eSEd Maste 		if (dwarf_siblingof(re->dbg, die, &die, &de) != DW_DLV_OK)
6442cf781b2eSEd Maste 			continue;
6443cf781b2eSEd Maste 		if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
6444cf781b2eSEd Maste 			warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
6445cf781b2eSEd Maste 			continue;
6446cf781b2eSEd Maste 		}
6447cf781b2eSEd Maste 		/* XXX: What about DW_TAG_partial_unit? */
6448cf781b2eSEd Maste 		lowpc = 0;
6449cf781b2eSEd Maste 		if (tag == DW_TAG_compile_unit) {
6450cf781b2eSEd Maste 			if (dwarf_attrval_unsigned(die, DW_AT_low_pc,
6451cf781b2eSEd Maste 			    &lowpc, &de) != DW_DLV_OK)
6452cf781b2eSEd Maste 				lowpc = 0;
6453cf781b2eSEd Maste 		}
6454cf781b2eSEd Maste 
6455cf781b2eSEd Maste 		/* Search attributes for reference to .debug_loc section. */
6456cf781b2eSEd Maste 		search_loclist_at(re, die, lowpc);
6457cf781b2eSEd Maste 	}
6458cf781b2eSEd Maste 	if (ret == DW_DLV_ERROR)
6459cf781b2eSEd Maste 		warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
6460cf781b2eSEd Maste 
6461cf781b2eSEd Maste 	/* Search .debug_types section. */
6462cf781b2eSEd Maste 	do {
6463cf781b2eSEd Maste 		while ((ret = dwarf_next_cu_header_c(re->dbg, 0, NULL,
6464cf781b2eSEd Maste 		    &version, NULL, &pointer_size, &off_size, NULL, NULL,
6465cf781b2eSEd Maste 		    NULL, NULL, &de)) == DW_DLV_OK) {
6466cf781b2eSEd Maste 			set_cu_context(re, pointer_size, off_size, version);
6467cf781b2eSEd Maste 			die = NULL;
6468cf781b2eSEd Maste 			if (dwarf_siblingof(re->dbg, die, &die, &de) !=
6469cf781b2eSEd Maste 			    DW_DLV_OK)
6470cf781b2eSEd Maste 				continue;
6471cf781b2eSEd Maste 			if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
6472cf781b2eSEd Maste 				warnx("dwarf_tag failed: %s",
6473cf781b2eSEd Maste 				    dwarf_errmsg(de));
6474cf781b2eSEd Maste 				continue;
6475cf781b2eSEd Maste 			}
6476cf781b2eSEd Maste 
6477cf781b2eSEd Maste 			lowpc = 0;
6478cf781b2eSEd Maste 			if (tag == DW_TAG_type_unit) {
6479cf781b2eSEd Maste 				if (dwarf_attrval_unsigned(die, DW_AT_low_pc,
6480cf781b2eSEd Maste 				    &lowpc, &de) != DW_DLV_OK)
6481cf781b2eSEd Maste 					lowpc = 0;
6482cf781b2eSEd Maste 			}
6483cf781b2eSEd Maste 
6484cf781b2eSEd Maste 			/*
6485cf781b2eSEd Maste 			 * Search attributes for reference to .debug_loc
6486cf781b2eSEd Maste 			 * section.
6487cf781b2eSEd Maste 			 */
6488cf781b2eSEd Maste 			search_loclist_at(re, die, lowpc);
6489cf781b2eSEd Maste 		}
6490cf781b2eSEd Maste 		if (ret == DW_DLV_ERROR)
6491cf781b2eSEd Maste 			warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
6492cf781b2eSEd Maste 	} while (dwarf_next_types_section(re->dbg, &de) == DW_DLV_OK);
6493cf781b2eSEd Maste 
6494cf781b2eSEd Maste 	if (TAILQ_EMPTY(&lalist))
6495cf781b2eSEd Maste 		return;
6496cf781b2eSEd Maste 
6497bee2765cSEd Maste 	has_content = 0;
6498cf781b2eSEd Maste 	TAILQ_FOREACH(la, &lalist, la_next) {
6499bee2765cSEd Maste 		if ((ret = dwarf_loclist_n(la->la_at, &llbuf, &lcnt, &de)) !=
6500cf781b2eSEd Maste 		    DW_DLV_OK) {
6501bee2765cSEd Maste 			if (ret != DW_DLV_NO_ENTRY)
6502bee2765cSEd Maste 				warnx("dwarf_loclist_n failed: %s",
6503bee2765cSEd Maste 				    dwarf_errmsg(de));
6504cf781b2eSEd Maste 			continue;
6505cf781b2eSEd Maste 		}
6506bee2765cSEd Maste 		if (!has_content) {
6507bee2765cSEd Maste 			has_content = 1;
6508bee2765cSEd Maste 			printf("\nContents of section .debug_loc:\n");
6509bee2765cSEd Maste 			printf("    Offset   Begin    End      Expression\n");
6510bee2765cSEd Maste 		}
6511cf781b2eSEd Maste 		set_cu_context(re, la->la_cu_psize, la->la_cu_osize,
6512cf781b2eSEd Maste 		    la->la_cu_ver);
6513cf781b2eSEd Maste 		for (i = 0; i < lcnt; i++) {
6514839529caSEd Maste 			printf("    %8.8jx ", (uintmax_t) la->la_off);
6515cf781b2eSEd Maste 			if (llbuf[i]->ld_lopc == 0 && llbuf[i]->ld_hipc == 0) {
6516cf781b2eSEd Maste 				printf("<End of list>\n");
6517cf781b2eSEd Maste 				continue;
6518cf781b2eSEd Maste 			}
6519cf781b2eSEd Maste 
6520cf781b2eSEd Maste 			/* TODO: handle base selection entry. */
6521cf781b2eSEd Maste 
6522cf781b2eSEd Maste 			printf("%8.8jx %8.8jx ",
6523cf781b2eSEd Maste 			    (uintmax_t) (la->la_lowpc + llbuf[i]->ld_lopc),
6524cf781b2eSEd Maste 			    (uintmax_t) (la->la_lowpc + llbuf[i]->ld_hipc));
6525cf781b2eSEd Maste 
6526cf781b2eSEd Maste 			putchar('(');
6527cf781b2eSEd Maste 			for (j = 0; (Dwarf_Half) j < llbuf[i]->ld_cents; j++) {
6528cf781b2eSEd Maste 				dump_dwarf_loc(re, &llbuf[i]->ld_s[j]);
65292c23cb7cSEd Maste 				if (j < llbuf[i]->ld_cents - 1)
6530cf781b2eSEd Maste 					printf("; ");
65312c23cb7cSEd Maste 			}
65322c23cb7cSEd Maste 			putchar(')');
65332c23cb7cSEd Maste 
65342c23cb7cSEd Maste 			if (llbuf[i]->ld_lopc == llbuf[i]->ld_hipc)
65352c23cb7cSEd Maste 				printf(" (start == end)");
65362c23cb7cSEd Maste 			putchar('\n');
65372c23cb7cSEd Maste 		}
6538cf781b2eSEd Maste 		for (i = 0; i < lcnt; i++) {
6539cf781b2eSEd Maste 			dwarf_dealloc(re->dbg, llbuf[i]->ld_s,
6540cf781b2eSEd Maste 			    DW_DLA_LOC_BLOCK);
6541cf781b2eSEd Maste 			dwarf_dealloc(re->dbg, llbuf[i], DW_DLA_LOCDESC);
6542cf781b2eSEd Maste 		}
6543cf781b2eSEd Maste 		dwarf_dealloc(re->dbg, llbuf, DW_DLA_LIST);
65442c23cb7cSEd Maste 	}
6545bee2765cSEd Maste 
6546bee2765cSEd Maste 	if (!has_content)
6547bee2765cSEd Maste 		printf("\nSection '.debug_loc' has no debugging data.\n");
65482c23cb7cSEd Maste }
65492c23cb7cSEd Maste 
65502c23cb7cSEd Maste /*
65512c23cb7cSEd Maste  * Retrieve a string using string table section index and the string offset.
65522c23cb7cSEd Maste  */
65532c23cb7cSEd Maste static const char*
65542c23cb7cSEd Maste get_string(struct readelf *re, int strtab, size_t off)
65552c23cb7cSEd Maste {
65562c23cb7cSEd Maste 	const char *name;
65572c23cb7cSEd Maste 
65582c23cb7cSEd Maste 	if ((name = elf_strptr(re->elf, strtab, off)) == NULL)
65592c23cb7cSEd Maste 		return ("");
65602c23cb7cSEd Maste 
65612c23cb7cSEd Maste 	return (name);
65622c23cb7cSEd Maste }
65632c23cb7cSEd Maste 
65642c23cb7cSEd Maste /*
65652c23cb7cSEd Maste  * Retrieve the name of a symbol using the section index of the symbol
65662c23cb7cSEd Maste  * table and the index of the symbol within that table.
65672c23cb7cSEd Maste  */
65682c23cb7cSEd Maste static const char *
65692c23cb7cSEd Maste get_symbol_name(struct readelf *re, int symtab, int i)
65702c23cb7cSEd Maste {
65712c23cb7cSEd Maste 	struct section	*s;
65722c23cb7cSEd Maste 	const char	*name;
65732c23cb7cSEd Maste 	GElf_Sym	 sym;
65742c23cb7cSEd Maste 	Elf_Data	*data;
65752c23cb7cSEd Maste 	int		 elferr;
65762c23cb7cSEd Maste 
65772c23cb7cSEd Maste 	s = &re->sl[symtab];
65782c23cb7cSEd Maste 	if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM)
65792c23cb7cSEd Maste 		return ("");
65802c23cb7cSEd Maste 	(void) elf_errno();
65812c23cb7cSEd Maste 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
65822c23cb7cSEd Maste 		elferr = elf_errno();
65832c23cb7cSEd Maste 		if (elferr != 0)
65842c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
65852c23cb7cSEd Maste 		return ("");
65862c23cb7cSEd Maste 	}
65872c23cb7cSEd Maste 	if (gelf_getsym(data, i, &sym) != &sym)
65882c23cb7cSEd Maste 		return ("");
65892c23cb7cSEd Maste 	/* Return section name for STT_SECTION symbol. */
6590b6b6f9ccSEd Maste 	if (GELF_ST_TYPE(sym.st_info) == STT_SECTION) {
6591b6b6f9ccSEd Maste 		if (sym.st_shndx < re->shnum &&
65922c23cb7cSEd Maste 		    re->sl[sym.st_shndx].name != NULL)
65932c23cb7cSEd Maste 			return (re->sl[sym.st_shndx].name);
6594b6b6f9ccSEd Maste 		return ("");
6595b6b6f9ccSEd Maste 	}
6596656f49f8SEd Maste 	if (s->link >= re->shnum ||
6597656f49f8SEd Maste 	    (name = elf_strptr(re->elf, s->link, sym.st_name)) == NULL)
65982c23cb7cSEd Maste 		return ("");
65992c23cb7cSEd Maste 
66002c23cb7cSEd Maste 	return (name);
66012c23cb7cSEd Maste }
66022c23cb7cSEd Maste 
66032c23cb7cSEd Maste static uint64_t
66042c23cb7cSEd Maste get_symbol_value(struct readelf *re, int symtab, int i)
66052c23cb7cSEd Maste {
66062c23cb7cSEd Maste 	struct section	*s;
66072c23cb7cSEd Maste 	GElf_Sym	 sym;
66082c23cb7cSEd Maste 	Elf_Data	*data;
66092c23cb7cSEd Maste 	int		 elferr;
66102c23cb7cSEd Maste 
66112c23cb7cSEd Maste 	s = &re->sl[symtab];
66122c23cb7cSEd Maste 	if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM)
66132c23cb7cSEd Maste 		return (0);
66142c23cb7cSEd Maste 	(void) elf_errno();
66152c23cb7cSEd Maste 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
66162c23cb7cSEd Maste 		elferr = elf_errno();
66172c23cb7cSEd Maste 		if (elferr != 0)
66182c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
66192c23cb7cSEd Maste 		return (0);
66202c23cb7cSEd Maste 	}
66212c23cb7cSEd Maste 	if (gelf_getsym(data, i, &sym) != &sym)
66222c23cb7cSEd Maste 		return (0);
66232c23cb7cSEd Maste 
66242c23cb7cSEd Maste 	return (sym.st_value);
66252c23cb7cSEd Maste }
66262c23cb7cSEd Maste 
66272c23cb7cSEd Maste static void
66282c23cb7cSEd Maste hex_dump(struct readelf *re)
66292c23cb7cSEd Maste {
66302c23cb7cSEd Maste 	struct section *s;
66312c23cb7cSEd Maste 	Elf_Data *d;
66322c23cb7cSEd Maste 	uint8_t *buf;
66332c23cb7cSEd Maste 	size_t sz, nbytes;
66342c23cb7cSEd Maste 	uint64_t addr;
66352c23cb7cSEd Maste 	int elferr, i, j;
66362c23cb7cSEd Maste 
66372c23cb7cSEd Maste 	for (i = 1; (size_t) i < re->shnum; i++) {
66382c23cb7cSEd Maste 		s = &re->sl[i];
66392c23cb7cSEd Maste 		if (find_dumpop(re, (size_t) i, s->name, HEX_DUMP, -1) == NULL)
66402c23cb7cSEd Maste 			continue;
66412c23cb7cSEd Maste 		(void) elf_errno();
6642839529caSEd Maste 		if ((d = elf_getdata(s->scn, NULL)) == NULL &&
6643839529caSEd Maste 		    (d = elf_rawdata(s->scn, NULL)) == NULL) {
66442c23cb7cSEd Maste 			elferr = elf_errno();
66452c23cb7cSEd Maste 			if (elferr != 0)
66462c23cb7cSEd Maste 				warnx("elf_getdata failed: %s",
66472c23cb7cSEd Maste 				    elf_errmsg(elferr));
66482c23cb7cSEd Maste 			continue;
66492c23cb7cSEd Maste 		}
6650839529caSEd Maste 		(void) elf_errno();
66512c23cb7cSEd Maste 		if (d->d_size <= 0 || d->d_buf == NULL) {
66522c23cb7cSEd Maste 			printf("\nSection '%s' has no data to dump.\n",
66532c23cb7cSEd Maste 			    s->name);
66542c23cb7cSEd Maste 			continue;
66552c23cb7cSEd Maste 		}
66562c23cb7cSEd Maste 		buf = d->d_buf;
66572c23cb7cSEd Maste 		sz = d->d_size;
66582c23cb7cSEd Maste 		addr = s->addr;
66592c23cb7cSEd Maste 		printf("\nHex dump of section '%s':\n", s->name);
66602c23cb7cSEd Maste 		while (sz > 0) {
66612c23cb7cSEd Maste 			printf("  0x%8.8jx ", (uintmax_t)addr);
66622c23cb7cSEd Maste 			nbytes = sz > 16? 16 : sz;
66632c23cb7cSEd Maste 			for (j = 0; j < 16; j++) {
66642c23cb7cSEd Maste 				if ((size_t)j < nbytes)
66652c23cb7cSEd Maste 					printf("%2.2x", buf[j]);
66662c23cb7cSEd Maste 				else
66672c23cb7cSEd Maste 					printf("  ");
66682c23cb7cSEd Maste 				if ((j & 3) == 3)
66692c23cb7cSEd Maste 					printf(" ");
66702c23cb7cSEd Maste 			}
66712c23cb7cSEd Maste 			for (j = 0; (size_t)j < nbytes; j++) {
66722c23cb7cSEd Maste 				if (isprint(buf[j]))
66732c23cb7cSEd Maste 					printf("%c", buf[j]);
66742c23cb7cSEd Maste 				else
66752c23cb7cSEd Maste 					printf(".");
66762c23cb7cSEd Maste 			}
66772c23cb7cSEd Maste 			printf("\n");
66782c23cb7cSEd Maste 			buf += nbytes;
66792c23cb7cSEd Maste 			addr += nbytes;
66802c23cb7cSEd Maste 			sz -= nbytes;
66812c23cb7cSEd Maste 		}
66822c23cb7cSEd Maste 	}
66832c23cb7cSEd Maste }
66842c23cb7cSEd Maste 
66852c23cb7cSEd Maste static void
66862c23cb7cSEd Maste str_dump(struct readelf *re)
66872c23cb7cSEd Maste {
66882c23cb7cSEd Maste 	struct section *s;
66892c23cb7cSEd Maste 	Elf_Data *d;
66902c23cb7cSEd Maste 	unsigned char *start, *end, *buf_end;
66912c23cb7cSEd Maste 	unsigned int len;
66922c23cb7cSEd Maste 	int i, j, elferr, found;
66932c23cb7cSEd Maste 
66942c23cb7cSEd Maste 	for (i = 1; (size_t) i < re->shnum; i++) {
66952c23cb7cSEd Maste 		s = &re->sl[i];
66962c23cb7cSEd Maste 		if (find_dumpop(re, (size_t) i, s->name, STR_DUMP, -1) == NULL)
66972c23cb7cSEd Maste 			continue;
66982c23cb7cSEd Maste 		(void) elf_errno();
6699839529caSEd Maste 		if ((d = elf_getdata(s->scn, NULL)) == NULL &&
6700839529caSEd Maste 		    (d = elf_rawdata(s->scn, NULL)) == NULL) {
67012c23cb7cSEd Maste 			elferr = elf_errno();
67022c23cb7cSEd Maste 			if (elferr != 0)
67032c23cb7cSEd Maste 				warnx("elf_getdata failed: %s",
67042c23cb7cSEd Maste 				    elf_errmsg(elferr));
67052c23cb7cSEd Maste 			continue;
67062c23cb7cSEd Maste 		}
6707839529caSEd Maste 		(void) elf_errno();
67082c23cb7cSEd Maste 		if (d->d_size <= 0 || d->d_buf == NULL) {
67092c23cb7cSEd Maste 			printf("\nSection '%s' has no data to dump.\n",
67102c23cb7cSEd Maste 			    s->name);
67112c23cb7cSEd Maste 			continue;
67122c23cb7cSEd Maste 		}
67132c23cb7cSEd Maste 		buf_end = (unsigned char *) d->d_buf + d->d_size;
67142c23cb7cSEd Maste 		start = (unsigned char *) d->d_buf;
67152c23cb7cSEd Maste 		found = 0;
67162c23cb7cSEd Maste 		printf("\nString dump of section '%s':\n", s->name);
67172c23cb7cSEd Maste 		for (;;) {
67182c23cb7cSEd Maste 			while (start < buf_end && !isprint(*start))
67192c23cb7cSEd Maste 				start++;
67202c23cb7cSEd Maste 			if (start >= buf_end)
67212c23cb7cSEd Maste 				break;
67222c23cb7cSEd Maste 			end = start + 1;
67232c23cb7cSEd Maste 			while (end < buf_end && isprint(*end))
67242c23cb7cSEd Maste 				end++;
67252c23cb7cSEd Maste 			printf("  [%6lx]  ",
67262c23cb7cSEd Maste 			    (long) (start - (unsigned char *) d->d_buf));
67272c23cb7cSEd Maste 			len = end - start;
67282c23cb7cSEd Maste 			for (j = 0; (unsigned int) j < len; j++)
67292c23cb7cSEd Maste 				putchar(start[j]);
67302c23cb7cSEd Maste 			putchar('\n');
67312c23cb7cSEd Maste 			found = 1;
67322c23cb7cSEd Maste 			if (end >= buf_end)
67332c23cb7cSEd Maste 				break;
67342c23cb7cSEd Maste 			start = end + 1;
67352c23cb7cSEd Maste 		}
67362c23cb7cSEd Maste 		if (!found)
67372c23cb7cSEd Maste 			printf("  No strings found in this section.");
67382c23cb7cSEd Maste 		putchar('\n');
67392c23cb7cSEd Maste 	}
67402c23cb7cSEd Maste }
67412c23cb7cSEd Maste 
67422c23cb7cSEd Maste static void
67432c23cb7cSEd Maste load_sections(struct readelf *re)
67442c23cb7cSEd Maste {
67452c23cb7cSEd Maste 	struct section	*s;
67462c23cb7cSEd Maste 	const char	*name;
67472c23cb7cSEd Maste 	Elf_Scn		*scn;
67482c23cb7cSEd Maste 	GElf_Shdr	 sh;
67492c23cb7cSEd Maste 	size_t		 shstrndx, ndx;
67502c23cb7cSEd Maste 	int		 elferr;
67512c23cb7cSEd Maste 
67522c23cb7cSEd Maste 	/* Allocate storage for internal section list. */
67532c23cb7cSEd Maste 	if (!elf_getshnum(re->elf, &re->shnum)) {
67542c23cb7cSEd Maste 		warnx("elf_getshnum failed: %s", elf_errmsg(-1));
67552c23cb7cSEd Maste 		return;
67562c23cb7cSEd Maste 	}
67572c23cb7cSEd Maste 	if (re->sl != NULL)
67582c23cb7cSEd Maste 		free(re->sl);
67592c23cb7cSEd Maste 	if ((re->sl = calloc(re->shnum, sizeof(*re->sl))) == NULL)
67602c23cb7cSEd Maste 		err(EXIT_FAILURE, "calloc failed");
67612c23cb7cSEd Maste 
67622c23cb7cSEd Maste 	/* Get the index of .shstrtab section. */
67632c23cb7cSEd Maste 	if (!elf_getshstrndx(re->elf, &shstrndx)) {
67642c23cb7cSEd Maste 		warnx("elf_getshstrndx failed: %s", elf_errmsg(-1));
67652c23cb7cSEd Maste 		return;
67662c23cb7cSEd Maste 	}
67672c23cb7cSEd Maste 
676871a0c925SEd Maste 	if ((scn = elf_getscn(re->elf, 0)) == NULL)
67692c23cb7cSEd Maste 		return;
67702c23cb7cSEd Maste 
67712c23cb7cSEd Maste 	(void) elf_errno();
67722c23cb7cSEd Maste 	do {
67732c23cb7cSEd Maste 		if (gelf_getshdr(scn, &sh) == NULL) {
67742c23cb7cSEd Maste 			warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
67752c23cb7cSEd Maste 			(void) elf_errno();
67762c23cb7cSEd Maste 			continue;
67772c23cb7cSEd Maste 		}
67782c23cb7cSEd Maste 		if ((name = elf_strptr(re->elf, shstrndx, sh.sh_name)) == NULL) {
67792c23cb7cSEd Maste 			(void) elf_errno();
67807dc45d65SConrad Meyer 			name = "<no-name>";
67812c23cb7cSEd Maste 		}
67822c23cb7cSEd Maste 		if ((ndx = elf_ndxscn(scn)) == SHN_UNDEF) {
67837dc45d65SConrad Meyer 			if ((elferr = elf_errno()) != 0) {
67842c23cb7cSEd Maste 				warnx("elf_ndxscn failed: %s",
67852c23cb7cSEd Maste 				    elf_errmsg(elferr));
67862c23cb7cSEd Maste 				continue;
67872c23cb7cSEd Maste 			}
67887dc45d65SConrad Meyer 		}
67892c23cb7cSEd Maste 		if (ndx >= re->shnum) {
67902c23cb7cSEd Maste 			warnx("section index of '%s' out of range", name);
67912c23cb7cSEd Maste 			continue;
67922c23cb7cSEd Maste 		}
6793656f49f8SEd Maste 		if (sh.sh_link >= re->shnum)
6794656f49f8SEd Maste 			warnx("section link %llu of '%s' out of range",
6795656f49f8SEd Maste 			    (unsigned long long)sh.sh_link, name);
67962c23cb7cSEd Maste 		s = &re->sl[ndx];
67972c23cb7cSEd Maste 		s->name = name;
67982c23cb7cSEd Maste 		s->scn = scn;
67992c23cb7cSEd Maste 		s->off = sh.sh_offset;
68002c23cb7cSEd Maste 		s->sz = sh.sh_size;
68012c23cb7cSEd Maste 		s->entsize = sh.sh_entsize;
68022c23cb7cSEd Maste 		s->align = sh.sh_addralign;
68032c23cb7cSEd Maste 		s->type = sh.sh_type;
68042c23cb7cSEd Maste 		s->flags = sh.sh_flags;
68052c23cb7cSEd Maste 		s->addr = sh.sh_addr;
68062c23cb7cSEd Maste 		s->link = sh.sh_link;
68072c23cb7cSEd Maste 		s->info = sh.sh_info;
68082c23cb7cSEd Maste 	} while ((scn = elf_nextscn(re->elf, scn)) != NULL);
68092c23cb7cSEd Maste 	elferr = elf_errno();
68102c23cb7cSEd Maste 	if (elferr != 0)
68112c23cb7cSEd Maste 		warnx("elf_nextscn failed: %s", elf_errmsg(elferr));
68122c23cb7cSEd Maste }
68132c23cb7cSEd Maste 
68142c23cb7cSEd Maste static void
68152c23cb7cSEd Maste unload_sections(struct readelf *re)
68162c23cb7cSEd Maste {
68172c23cb7cSEd Maste 
68182c23cb7cSEd Maste 	if (re->sl != NULL) {
68192c23cb7cSEd Maste 		free(re->sl);
68202c23cb7cSEd Maste 		re->sl = NULL;
68212c23cb7cSEd Maste 	}
68222c23cb7cSEd Maste 	re->shnum = 0;
68232c23cb7cSEd Maste 	re->vd_s = NULL;
68242c23cb7cSEd Maste 	re->vn_s = NULL;
68252c23cb7cSEd Maste 	re->vs_s = NULL;
68262c23cb7cSEd Maste 	re->vs = NULL;
68272c23cb7cSEd Maste 	re->vs_sz = 0;
68282c23cb7cSEd Maste 	if (re->ver != NULL) {
68292c23cb7cSEd Maste 		free(re->ver);
68302c23cb7cSEd Maste 		re->ver = NULL;
68312c23cb7cSEd Maste 		re->ver_sz = 0;
68322c23cb7cSEd Maste 	}
68332c23cb7cSEd Maste }
68342c23cb7cSEd Maste 
68352c23cb7cSEd Maste static void
68362c23cb7cSEd Maste dump_elf(struct readelf *re)
68372c23cb7cSEd Maste {
68382c23cb7cSEd Maste 
68392c23cb7cSEd Maste 	/* Fetch ELF header. No need to continue if it fails. */
68402c23cb7cSEd Maste 	if (gelf_getehdr(re->elf, &re->ehdr) == NULL) {
68412c23cb7cSEd Maste 		warnx("gelf_getehdr failed: %s", elf_errmsg(-1));
68422c23cb7cSEd Maste 		return;
68432c23cb7cSEd Maste 	}
68442c23cb7cSEd Maste 	if ((re->ec = gelf_getclass(re->elf)) == ELFCLASSNONE) {
68452c23cb7cSEd Maste 		warnx("gelf_getclass failed: %s", elf_errmsg(-1));
68462c23cb7cSEd Maste 		return;
68472c23cb7cSEd Maste 	}
68482c23cb7cSEd Maste 	if (re->ehdr.e_ident[EI_DATA] == ELFDATA2MSB) {
68492c23cb7cSEd Maste 		re->dw_read = _read_msb;
68502c23cb7cSEd Maste 		re->dw_decode = _decode_msb;
68512c23cb7cSEd Maste 	} else {
68522c23cb7cSEd Maste 		re->dw_read = _read_lsb;
68532c23cb7cSEd Maste 		re->dw_decode = _decode_lsb;
68542c23cb7cSEd Maste 	}
68552c23cb7cSEd Maste 
68562c23cb7cSEd Maste 	if (re->options & ~RE_H)
68572c23cb7cSEd Maste 		load_sections(re);
68582c23cb7cSEd Maste 	if ((re->options & RE_VV) || (re->options & RE_S))
68592c23cb7cSEd Maste 		search_ver(re);
68602c23cb7cSEd Maste 	if (re->options & RE_H)
68612c23cb7cSEd Maste 		dump_ehdr(re);
68622c23cb7cSEd Maste 	if (re->options & RE_L)
68632c23cb7cSEd Maste 		dump_phdr(re);
68642c23cb7cSEd Maste 	if (re->options & RE_SS)
68652c23cb7cSEd Maste 		dump_shdr(re);
68663ef90571SEd Maste 	if (re->options & RE_G)
68673ef90571SEd Maste 		dump_section_groups(re);
68682c23cb7cSEd Maste 	if (re->options & RE_D)
68692c23cb7cSEd Maste 		dump_dynamic(re);
68702c23cb7cSEd Maste 	if (re->options & RE_R)
68712c23cb7cSEd Maste 		dump_reloc(re);
68722c23cb7cSEd Maste 	if (re->options & RE_S)
68732c23cb7cSEd Maste 		dump_symtabs(re);
68742c23cb7cSEd Maste 	if (re->options & RE_N)
68752c23cb7cSEd Maste 		dump_notes(re);
68762c23cb7cSEd Maste 	if (re->options & RE_II)
68772c23cb7cSEd Maste 		dump_hash(re);
68782c23cb7cSEd Maste 	if (re->options & RE_X)
68792c23cb7cSEd Maste 		hex_dump(re);
68802c23cb7cSEd Maste 	if (re->options & RE_P)
68812c23cb7cSEd Maste 		str_dump(re);
68822c23cb7cSEd Maste 	if (re->options & RE_VV)
68832c23cb7cSEd Maste 		dump_ver(re);
68842c23cb7cSEd Maste 	if (re->options & RE_AA)
68852c23cb7cSEd Maste 		dump_arch_specific_info(re);
68862c23cb7cSEd Maste 	if (re->options & RE_W)
68872c23cb7cSEd Maste 		dump_dwarf(re);
68882c23cb7cSEd Maste 	if (re->options & ~RE_H)
68892c23cb7cSEd Maste 		unload_sections(re);
68902c23cb7cSEd Maste }
68912c23cb7cSEd Maste 
68922c23cb7cSEd Maste static void
68932c23cb7cSEd Maste dump_dwarf(struct readelf *re)
68942c23cb7cSEd Maste {
6895bee2765cSEd Maste 	struct loc_at *la, *_la;
68962c23cb7cSEd Maste 	Dwarf_Error de;
6897bee2765cSEd Maste 	int error;
68982c23cb7cSEd Maste 
68992c23cb7cSEd Maste 	if (dwarf_elf_init(re->elf, DW_DLC_READ, NULL, NULL, &re->dbg, &de)) {
69002c23cb7cSEd Maste 		if ((error = dwarf_errno(de)) != DW_DLE_DEBUG_INFO_NULL)
69012c23cb7cSEd Maste 			errx(EXIT_FAILURE, "dwarf_elf_init failed: %s",
69022c23cb7cSEd Maste 			    dwarf_errmsg(de));
69032c23cb7cSEd Maste 		return;
69042c23cb7cSEd Maste 	}
69052c23cb7cSEd Maste 
69062c23cb7cSEd Maste 	if (re->dop & DW_A)
69072c23cb7cSEd Maste 		dump_dwarf_abbrev(re);
69082c23cb7cSEd Maste 	if (re->dop & DW_L)
69092c23cb7cSEd Maste 		dump_dwarf_line(re);
69102c23cb7cSEd Maste 	if (re->dop & DW_LL)
69112c23cb7cSEd Maste 		dump_dwarf_line_decoded(re);
6912cf781b2eSEd Maste 	if (re->dop & DW_I) {
6913cf781b2eSEd Maste 		dump_dwarf_info(re, 0);
6914cf781b2eSEd Maste 		dump_dwarf_info(re, 1);
6915cf781b2eSEd Maste 	}
69162c23cb7cSEd Maste 	if (re->dop & DW_P)
69172c23cb7cSEd Maste 		dump_dwarf_pubnames(re);
69182c23cb7cSEd Maste 	if (re->dop & DW_R)
69192c23cb7cSEd Maste 		dump_dwarf_aranges(re);
69202c23cb7cSEd Maste 	if (re->dop & DW_RR)
69212c23cb7cSEd Maste 		dump_dwarf_ranges(re);
69222c23cb7cSEd Maste 	if (re->dop & DW_M)
69232c23cb7cSEd Maste 		dump_dwarf_macinfo(re);
69242c23cb7cSEd Maste 	if (re->dop & DW_F)
69252c23cb7cSEd Maste 		dump_dwarf_frame(re, 0);
69262c23cb7cSEd Maste 	else if (re->dop & DW_FF)
69272c23cb7cSEd Maste 		dump_dwarf_frame(re, 1);
69282c23cb7cSEd Maste 	if (re->dop & DW_S)
69292c23cb7cSEd Maste 		dump_dwarf_str(re);
69302c23cb7cSEd Maste 	if (re->dop & DW_O)
69312c23cb7cSEd Maste 		dump_dwarf_loclist(re);
69322c23cb7cSEd Maste 
6933bee2765cSEd Maste 	TAILQ_FOREACH_SAFE(la, &lalist, la_next, _la) {
6934bee2765cSEd Maste 		TAILQ_REMOVE(&lalist, la, la_next);
6935bee2765cSEd Maste 		free(la);
6936bee2765cSEd Maste 	}
6937bee2765cSEd Maste 
69382c23cb7cSEd Maste 	dwarf_finish(re->dbg, &de);
69392c23cb7cSEd Maste }
69402c23cb7cSEd Maste 
69412c23cb7cSEd Maste static void
69422c23cb7cSEd Maste dump_ar(struct readelf *re, int fd)
69432c23cb7cSEd Maste {
69442c23cb7cSEd Maste 	Elf_Arsym *arsym;
69452c23cb7cSEd Maste 	Elf_Arhdr *arhdr;
69462c23cb7cSEd Maste 	Elf_Cmd cmd;
69472c23cb7cSEd Maste 	Elf *e;
69482c23cb7cSEd Maste 	size_t sz;
69492c23cb7cSEd Maste 	off_t off;
69502c23cb7cSEd Maste 	int i;
69512c23cb7cSEd Maste 
69522c23cb7cSEd Maste 	re->ar = re->elf;
69532c23cb7cSEd Maste 
69542c23cb7cSEd Maste 	if (re->options & RE_C) {
69552c23cb7cSEd Maste 		if ((arsym = elf_getarsym(re->ar, &sz)) == NULL) {
69562c23cb7cSEd Maste 			warnx("elf_getarsym() failed: %s", elf_errmsg(-1));
69572c23cb7cSEd Maste 			goto process_members;
69582c23cb7cSEd Maste 		}
69592c23cb7cSEd Maste 		printf("Index of archive %s: (%ju entries)\n", re->filename,
69602c23cb7cSEd Maste 		    (uintmax_t) sz - 1);
69612c23cb7cSEd Maste 		off = 0;
69622c23cb7cSEd Maste 		for (i = 0; (size_t) i < sz; i++) {
69632c23cb7cSEd Maste 			if (arsym[i].as_name == NULL)
69642c23cb7cSEd Maste 				break;
69652c23cb7cSEd Maste 			if (arsym[i].as_off != off) {
69662c23cb7cSEd Maste 				off = arsym[i].as_off;
69672c23cb7cSEd Maste 				if (elf_rand(re->ar, off) != off) {
69682c23cb7cSEd Maste 					warnx("elf_rand() failed: %s",
69692c23cb7cSEd Maste 					    elf_errmsg(-1));
69702c23cb7cSEd Maste 					continue;
69712c23cb7cSEd Maste 				}
69722c23cb7cSEd Maste 				if ((e = elf_begin(fd, ELF_C_READ, re->ar)) ==
69732c23cb7cSEd Maste 				    NULL) {
69742c23cb7cSEd Maste 					warnx("elf_begin() failed: %s",
69752c23cb7cSEd Maste 					    elf_errmsg(-1));
69762c23cb7cSEd Maste 					continue;
69772c23cb7cSEd Maste 				}
69782c23cb7cSEd Maste 				if ((arhdr = elf_getarhdr(e)) == NULL) {
69792c23cb7cSEd Maste 					warnx("elf_getarhdr() failed: %s",
69802c23cb7cSEd Maste 					    elf_errmsg(-1));
69812c23cb7cSEd Maste 					elf_end(e);
69822c23cb7cSEd Maste 					continue;
69832c23cb7cSEd Maste 				}
69842c23cb7cSEd Maste 				printf("Binary %s(%s) contains:\n",
69852c23cb7cSEd Maste 				    re->filename, arhdr->ar_name);
69862c23cb7cSEd Maste 			}
69872c23cb7cSEd Maste 			printf("\t%s\n", arsym[i].as_name);
69882c23cb7cSEd Maste 		}
69892c23cb7cSEd Maste 		if (elf_rand(re->ar, SARMAG) != SARMAG) {
69902c23cb7cSEd Maste 			warnx("elf_rand() failed: %s", elf_errmsg(-1));
69912c23cb7cSEd Maste 			return;
69922c23cb7cSEd Maste 		}
69932c23cb7cSEd Maste 	}
69942c23cb7cSEd Maste 
69952c23cb7cSEd Maste process_members:
69962c23cb7cSEd Maste 
69972c23cb7cSEd Maste 	if ((re->options & ~RE_C) == 0)
69982c23cb7cSEd Maste 		return;
69992c23cb7cSEd Maste 
70002c23cb7cSEd Maste 	cmd = ELF_C_READ;
70012c23cb7cSEd Maste 	while ((re->elf = elf_begin(fd, cmd, re->ar)) != NULL) {
70022c23cb7cSEd Maste 		if ((arhdr = elf_getarhdr(re->elf)) == NULL) {
70032c23cb7cSEd Maste 			warnx("elf_getarhdr() failed: %s", elf_errmsg(-1));
70042c23cb7cSEd Maste 			goto next_member;
70052c23cb7cSEd Maste 		}
70062c23cb7cSEd Maste 		if (strcmp(arhdr->ar_name, "/") == 0 ||
70072c23cb7cSEd Maste 		    strcmp(arhdr->ar_name, "//") == 0 ||
70082c23cb7cSEd Maste 		    strcmp(arhdr->ar_name, "__.SYMDEF") == 0)
70092c23cb7cSEd Maste 			goto next_member;
70102c23cb7cSEd Maste 		printf("\nFile: %s(%s)\n", re->filename, arhdr->ar_name);
70112c23cb7cSEd Maste 		dump_elf(re);
70122c23cb7cSEd Maste 
70132c23cb7cSEd Maste 	next_member:
70142c23cb7cSEd Maste 		cmd = elf_next(re->elf);
70152c23cb7cSEd Maste 		elf_end(re->elf);
70162c23cb7cSEd Maste 	}
70172c23cb7cSEd Maste 	re->elf = re->ar;
70182c23cb7cSEd Maste }
70192c23cb7cSEd Maste 
70202c23cb7cSEd Maste static void
70212c23cb7cSEd Maste dump_object(struct readelf *re)
70222c23cb7cSEd Maste {
70232c23cb7cSEd Maste 	int fd;
70242c23cb7cSEd Maste 
70252c23cb7cSEd Maste 	if ((fd = open(re->filename, O_RDONLY)) == -1) {
70262c23cb7cSEd Maste 		warn("open %s failed", re->filename);
70272c23cb7cSEd Maste 		return;
70282c23cb7cSEd Maste 	}
70292c23cb7cSEd Maste 
70302c23cb7cSEd Maste 	if ((re->flags & DISPLAY_FILENAME) != 0)
70312c23cb7cSEd Maste 		printf("\nFile: %s\n", re->filename);
70322c23cb7cSEd Maste 
70332c23cb7cSEd Maste 	if ((re->elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
70342c23cb7cSEd Maste 		warnx("elf_begin() failed: %s", elf_errmsg(-1));
70352c23cb7cSEd Maste 		return;
70362c23cb7cSEd Maste 	}
70372c23cb7cSEd Maste 
70382c23cb7cSEd Maste 	switch (elf_kind(re->elf)) {
70392c23cb7cSEd Maste 	case ELF_K_NONE:
70402c23cb7cSEd Maste 		warnx("Not an ELF file.");
70412c23cb7cSEd Maste 		return;
70422c23cb7cSEd Maste 	case ELF_K_ELF:
70432c23cb7cSEd Maste 		dump_elf(re);
70442c23cb7cSEd Maste 		break;
70452c23cb7cSEd Maste 	case ELF_K_AR:
70462c23cb7cSEd Maste 		dump_ar(re, fd);
70472c23cb7cSEd Maste 		break;
70482c23cb7cSEd Maste 	default:
70492c23cb7cSEd Maste 		warnx("Internal: libelf returned unknown elf kind.");
70502c23cb7cSEd Maste 		return;
70512c23cb7cSEd Maste 	}
70522c23cb7cSEd Maste 
70532c23cb7cSEd Maste 	elf_end(re->elf);
70542c23cb7cSEd Maste }
70552c23cb7cSEd Maste 
70562c23cb7cSEd Maste static void
70572c23cb7cSEd Maste add_dumpop(struct readelf *re, size_t si, const char *sn, int op, int t)
70582c23cb7cSEd Maste {
70592c23cb7cSEd Maste 	struct dumpop *d;
70602c23cb7cSEd Maste 
70612c23cb7cSEd Maste 	if ((d = find_dumpop(re, si, sn, -1, t)) == NULL) {
70622c23cb7cSEd Maste 		if ((d = calloc(1, sizeof(*d))) == NULL)
70632c23cb7cSEd Maste 			err(EXIT_FAILURE, "calloc failed");
70642c23cb7cSEd Maste 		if (t == DUMP_BY_INDEX)
70652c23cb7cSEd Maste 			d->u.si = si;
70662c23cb7cSEd Maste 		else
70672c23cb7cSEd Maste 			d->u.sn = sn;
70682c23cb7cSEd Maste 		d->type = t;
70692c23cb7cSEd Maste 		d->op = op;
70702c23cb7cSEd Maste 		STAILQ_INSERT_TAIL(&re->v_dumpop, d, dumpop_list);
70712c23cb7cSEd Maste 	} else
70722c23cb7cSEd Maste 		d->op |= op;
70732c23cb7cSEd Maste }
70742c23cb7cSEd Maste 
70752c23cb7cSEd Maste static struct dumpop *
70762c23cb7cSEd Maste find_dumpop(struct readelf *re, size_t si, const char *sn, int op, int t)
70772c23cb7cSEd Maste {
70782c23cb7cSEd Maste 	struct dumpop *d;
70792c23cb7cSEd Maste 
70802c23cb7cSEd Maste 	STAILQ_FOREACH(d, &re->v_dumpop, dumpop_list) {
70812c23cb7cSEd Maste 		if ((op == -1 || op & d->op) &&
70822c23cb7cSEd Maste 		    (t == -1 || (unsigned) t == d->type)) {
70832c23cb7cSEd Maste 			if ((d->type == DUMP_BY_INDEX && d->u.si == si) ||
70842c23cb7cSEd Maste 			    (d->type == DUMP_BY_NAME && !strcmp(d->u.sn, sn)))
70852c23cb7cSEd Maste 				return (d);
70862c23cb7cSEd Maste 		}
70872c23cb7cSEd Maste 	}
70882c23cb7cSEd Maste 
70892c23cb7cSEd Maste 	return (NULL);
70902c23cb7cSEd Maste }
70912c23cb7cSEd Maste 
70922c23cb7cSEd Maste static struct {
70932c23cb7cSEd Maste 	const char *ln;
70942c23cb7cSEd Maste 	char sn;
70952c23cb7cSEd Maste 	int value;
70962c23cb7cSEd Maste } dwarf_op[] = {
70972c23cb7cSEd Maste 	{"rawline", 'l', DW_L},
70982c23cb7cSEd Maste 	{"decodedline", 'L', DW_LL},
70992c23cb7cSEd Maste 	{"info", 'i', DW_I},
71002c23cb7cSEd Maste 	{"abbrev", 'a', DW_A},
71012c23cb7cSEd Maste 	{"pubnames", 'p', DW_P},
71022c23cb7cSEd Maste 	{"aranges", 'r', DW_R},
71032c23cb7cSEd Maste 	{"ranges", 'r', DW_R},
71042c23cb7cSEd Maste 	{"Ranges", 'R', DW_RR},
71052c23cb7cSEd Maste 	{"macro", 'm', DW_M},
71062c23cb7cSEd Maste 	{"frames", 'f', DW_F},
7107cf781b2eSEd Maste 	{"frames-interp", 'F', DW_FF},
71082c23cb7cSEd Maste 	{"str", 's', DW_S},
71092c23cb7cSEd Maste 	{"loc", 'o', DW_O},
71102c23cb7cSEd Maste 	{NULL, 0, 0}
71112c23cb7cSEd Maste };
71122c23cb7cSEd Maste 
71132c23cb7cSEd Maste static void
71142c23cb7cSEd Maste parse_dwarf_op_short(struct readelf *re, const char *op)
71152c23cb7cSEd Maste {
71162c23cb7cSEd Maste 	int i;
71172c23cb7cSEd Maste 
71182c23cb7cSEd Maste 	if (op == NULL) {
71192c23cb7cSEd Maste 		re->dop |= DW_DEFAULT_OPTIONS;
71202c23cb7cSEd Maste 		return;
71212c23cb7cSEd Maste 	}
71222c23cb7cSEd Maste 
71232c23cb7cSEd Maste 	for (; *op != '\0'; op++) {
71242c23cb7cSEd Maste 		for (i = 0; dwarf_op[i].ln != NULL; i++) {
71252c23cb7cSEd Maste 			if (dwarf_op[i].sn == *op) {
71262c23cb7cSEd Maste 				re->dop |= dwarf_op[i].value;
71272c23cb7cSEd Maste 				break;
71282c23cb7cSEd Maste 			}
71292c23cb7cSEd Maste 		}
71302c23cb7cSEd Maste 	}
71312c23cb7cSEd Maste }
71322c23cb7cSEd Maste 
71332c23cb7cSEd Maste static void
71342c23cb7cSEd Maste parse_dwarf_op_long(struct readelf *re, const char *op)
71352c23cb7cSEd Maste {
71362c23cb7cSEd Maste 	char *p, *token, *bp;
71372c23cb7cSEd Maste 	int i;
71382c23cb7cSEd Maste 
71392c23cb7cSEd Maste 	if (op == NULL) {
71402c23cb7cSEd Maste 		re->dop |= DW_DEFAULT_OPTIONS;
71412c23cb7cSEd Maste 		return;
71422c23cb7cSEd Maste 	}
71432c23cb7cSEd Maste 
71442c23cb7cSEd Maste 	if ((p = strdup(op)) == NULL)
71452c23cb7cSEd Maste 		err(EXIT_FAILURE, "strdup failed");
71462c23cb7cSEd Maste 	bp = p;
71472c23cb7cSEd Maste 
71482c23cb7cSEd Maste 	while ((token = strsep(&p, ",")) != NULL) {
71492c23cb7cSEd Maste 		for (i = 0; dwarf_op[i].ln != NULL; i++) {
71502c23cb7cSEd Maste 			if (!strcmp(token, dwarf_op[i].ln)) {
71512c23cb7cSEd Maste 				re->dop |= dwarf_op[i].value;
71522c23cb7cSEd Maste 				break;
71532c23cb7cSEd Maste 			}
71542c23cb7cSEd Maste 		}
71552c23cb7cSEd Maste 	}
71562c23cb7cSEd Maste 
71572c23cb7cSEd Maste 	free(bp);
71582c23cb7cSEd Maste }
71592c23cb7cSEd Maste 
71602c23cb7cSEd Maste static uint64_t
71612c23cb7cSEd Maste _read_lsb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read)
71622c23cb7cSEd Maste {
71632c23cb7cSEd Maste 	uint64_t ret;
71642c23cb7cSEd Maste 	uint8_t *src;
71652c23cb7cSEd Maste 
71662c23cb7cSEd Maste 	src = (uint8_t *) d->d_buf + *offsetp;
71672c23cb7cSEd Maste 
71682c23cb7cSEd Maste 	ret = 0;
71692c23cb7cSEd Maste 	switch (bytes_to_read) {
71702c23cb7cSEd Maste 	case 8:
71712c23cb7cSEd Maste 		ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
71722c23cb7cSEd Maste 		ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
7173839529caSEd Maste 		/* FALLTHROUGH */
71742c23cb7cSEd Maste 	case 4:
71752c23cb7cSEd Maste 		ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
7176839529caSEd Maste 		/* FALLTHROUGH */
71772c23cb7cSEd Maste 	case 2:
71782c23cb7cSEd Maste 		ret |= ((uint64_t) src[1]) << 8;
7179839529caSEd Maste 		/* FALLTHROUGH */
71802c23cb7cSEd Maste 	case 1:
71812c23cb7cSEd Maste 		ret |= src[0];
71822c23cb7cSEd Maste 		break;
71832c23cb7cSEd Maste 	default:
71842c23cb7cSEd Maste 		return (0);
71852c23cb7cSEd Maste 	}
71862c23cb7cSEd Maste 
71872c23cb7cSEd Maste 	*offsetp += bytes_to_read;
71882c23cb7cSEd Maste 
71892c23cb7cSEd Maste 	return (ret);
71902c23cb7cSEd Maste }
71912c23cb7cSEd Maste 
71922c23cb7cSEd Maste static uint64_t
71932c23cb7cSEd Maste _read_msb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read)
71942c23cb7cSEd Maste {
71952c23cb7cSEd Maste 	uint64_t ret;
71962c23cb7cSEd Maste 	uint8_t *src;
71972c23cb7cSEd Maste 
71982c23cb7cSEd Maste 	src = (uint8_t *) d->d_buf + *offsetp;
71992c23cb7cSEd Maste 
72002c23cb7cSEd Maste 	switch (bytes_to_read) {
72012c23cb7cSEd Maste 	case 1:
72022c23cb7cSEd Maste 		ret = src[0];
72032c23cb7cSEd Maste 		break;
72042c23cb7cSEd Maste 	case 2:
72052c23cb7cSEd Maste 		ret = src[1] | ((uint64_t) src[0]) << 8;
72062c23cb7cSEd Maste 		break;
72072c23cb7cSEd Maste 	case 4:
72082c23cb7cSEd Maste 		ret = src[3] | ((uint64_t) src[2]) << 8;
72092c23cb7cSEd Maste 		ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24;
72102c23cb7cSEd Maste 		break;
72112c23cb7cSEd Maste 	case 8:
72122c23cb7cSEd Maste 		ret = src[7] | ((uint64_t) src[6]) << 8;
72132c23cb7cSEd Maste 		ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24;
72142c23cb7cSEd Maste 		ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40;
72152c23cb7cSEd Maste 		ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56;
72162c23cb7cSEd Maste 		break;
72172c23cb7cSEd Maste 	default:
72182c23cb7cSEd Maste 		return (0);
72192c23cb7cSEd Maste 	}
72202c23cb7cSEd Maste 
72212c23cb7cSEd Maste 	*offsetp += bytes_to_read;
72222c23cb7cSEd Maste 
72232c23cb7cSEd Maste 	return (ret);
72242c23cb7cSEd Maste }
72252c23cb7cSEd Maste 
72262c23cb7cSEd Maste static uint64_t
72272c23cb7cSEd Maste _decode_lsb(uint8_t **data, int bytes_to_read)
72282c23cb7cSEd Maste {
72292c23cb7cSEd Maste 	uint64_t ret;
72302c23cb7cSEd Maste 	uint8_t *src;
72312c23cb7cSEd Maste 
72322c23cb7cSEd Maste 	src = *data;
72332c23cb7cSEd Maste 
72342c23cb7cSEd Maste 	ret = 0;
72352c23cb7cSEd Maste 	switch (bytes_to_read) {
72362c23cb7cSEd Maste 	case 8:
72372c23cb7cSEd Maste 		ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
72382c23cb7cSEd Maste 		ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
7239839529caSEd Maste 		/* FALLTHROUGH */
72402c23cb7cSEd Maste 	case 4:
72412c23cb7cSEd Maste 		ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
7242839529caSEd Maste 		/* FALLTHROUGH */
72432c23cb7cSEd Maste 	case 2:
72442c23cb7cSEd Maste 		ret |= ((uint64_t) src[1]) << 8;
7245839529caSEd Maste 		/* FALLTHROUGH */
72462c23cb7cSEd Maste 	case 1:
72472c23cb7cSEd Maste 		ret |= src[0];
72482c23cb7cSEd Maste 		break;
72492c23cb7cSEd Maste 	default:
72502c23cb7cSEd Maste 		return (0);
72512c23cb7cSEd Maste 	}
72522c23cb7cSEd Maste 
72532c23cb7cSEd Maste 	*data += bytes_to_read;
72542c23cb7cSEd Maste 
72552c23cb7cSEd Maste 	return (ret);
72562c23cb7cSEd Maste }
72572c23cb7cSEd Maste 
72582c23cb7cSEd Maste static uint64_t
72592c23cb7cSEd Maste _decode_msb(uint8_t **data, int bytes_to_read)
72602c23cb7cSEd Maste {
72612c23cb7cSEd Maste 	uint64_t ret;
72622c23cb7cSEd Maste 	uint8_t *src;
72632c23cb7cSEd Maste 
72642c23cb7cSEd Maste 	src = *data;
72652c23cb7cSEd Maste 
72662c23cb7cSEd Maste 	ret = 0;
72672c23cb7cSEd Maste 	switch (bytes_to_read) {
72682c23cb7cSEd Maste 	case 1:
72692c23cb7cSEd Maste 		ret = src[0];
72702c23cb7cSEd Maste 		break;
72712c23cb7cSEd Maste 	case 2:
72722c23cb7cSEd Maste 		ret = src[1] | ((uint64_t) src[0]) << 8;
72732c23cb7cSEd Maste 		break;
72742c23cb7cSEd Maste 	case 4:
72752c23cb7cSEd Maste 		ret = src[3] | ((uint64_t) src[2]) << 8;
72762c23cb7cSEd Maste 		ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24;
72772c23cb7cSEd Maste 		break;
72782c23cb7cSEd Maste 	case 8:
72792c23cb7cSEd Maste 		ret = src[7] | ((uint64_t) src[6]) << 8;
72802c23cb7cSEd Maste 		ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24;
72812c23cb7cSEd Maste 		ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40;
72822c23cb7cSEd Maste 		ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56;
72832c23cb7cSEd Maste 		break;
72842c23cb7cSEd Maste 	default:
72852c23cb7cSEd Maste 		return (0);
72862c23cb7cSEd Maste 		break;
72872c23cb7cSEd Maste 	}
72882c23cb7cSEd Maste 
72892c23cb7cSEd Maste 	*data += bytes_to_read;
72902c23cb7cSEd Maste 
72912c23cb7cSEd Maste 	return (ret);
72922c23cb7cSEd Maste }
72932c23cb7cSEd Maste 
72942c23cb7cSEd Maste static int64_t
729595fd7f26SEd Maste _decode_sleb128(uint8_t **dp, uint8_t *dpe)
72962c23cb7cSEd Maste {
72972c23cb7cSEd Maste 	int64_t ret = 0;
72988f32e46dSKai Wang 	uint8_t b = 0;
72992c23cb7cSEd Maste 	int shift = 0;
73002c23cb7cSEd Maste 
73012c23cb7cSEd Maste 	uint8_t *src = *dp;
73022c23cb7cSEd Maste 
73032c23cb7cSEd Maste 	do {
730495fd7f26SEd Maste 		if (src >= dpe)
730595fd7f26SEd Maste 			break;
73062c23cb7cSEd Maste 		b = *src++;
73072c23cb7cSEd Maste 		ret |= ((b & 0x7f) << shift);
73082c23cb7cSEd Maste 		shift += 7;
73092c23cb7cSEd Maste 	} while ((b & 0x80) != 0);
73102c23cb7cSEd Maste 
73112c23cb7cSEd Maste 	if (shift < 32 && (b & 0x40) != 0)
73122c23cb7cSEd Maste 		ret |= (-1 << shift);
73132c23cb7cSEd Maste 
73142c23cb7cSEd Maste 	*dp = src;
73152c23cb7cSEd Maste 
73162c23cb7cSEd Maste 	return (ret);
73172c23cb7cSEd Maste }
73182c23cb7cSEd Maste 
73192c23cb7cSEd Maste static uint64_t
732095fd7f26SEd Maste _decode_uleb128(uint8_t **dp, uint8_t *dpe)
73212c23cb7cSEd Maste {
73222c23cb7cSEd Maste 	uint64_t ret = 0;
73232c23cb7cSEd Maste 	uint8_t b;
73242c23cb7cSEd Maste 	int shift = 0;
73252c23cb7cSEd Maste 
73262c23cb7cSEd Maste 	uint8_t *src = *dp;
73272c23cb7cSEd Maste 
73282c23cb7cSEd Maste 	do {
732995fd7f26SEd Maste 		if (src >= dpe)
733095fd7f26SEd Maste 			break;
73312c23cb7cSEd Maste 		b = *src++;
73322c23cb7cSEd Maste 		ret |= ((b & 0x7f) << shift);
73332c23cb7cSEd Maste 		shift += 7;
73342c23cb7cSEd Maste 	} while ((b & 0x80) != 0);
73352c23cb7cSEd Maste 
73362c23cb7cSEd Maste 	*dp = src;
73372c23cb7cSEd Maste 
73382c23cb7cSEd Maste 	return (ret);
73392c23cb7cSEd Maste }
73402c23cb7cSEd Maste 
73412c23cb7cSEd Maste static void
73422c23cb7cSEd Maste readelf_version(void)
73432c23cb7cSEd Maste {
73442c23cb7cSEd Maste 	(void) printf("%s (%s)\n", ELFTC_GETPROGNAME(),
73452c23cb7cSEd Maste 	    elftc_version());
73462c23cb7cSEd Maste 	exit(EXIT_SUCCESS);
73472c23cb7cSEd Maste }
73482c23cb7cSEd Maste 
73492c23cb7cSEd Maste #define	USAGE_MESSAGE	"\
73502c23cb7cSEd Maste Usage: %s [options] file...\n\
73512c23cb7cSEd Maste   Display information about ELF objects and ar(1) archives.\n\n\
73522c23cb7cSEd Maste   Options:\n\
73532c23cb7cSEd Maste   -a | --all               Equivalent to specifying options '-dhIlrsASV'.\n\
73542c23cb7cSEd Maste   -c | --archive-index     Print the archive symbol table for archives.\n\
73552c23cb7cSEd Maste   -d | --dynamic           Print the contents of SHT_DYNAMIC sections.\n\
73562c23cb7cSEd Maste   -e | --headers           Print all headers in the object.\n\
73573ef90571SEd Maste   -g | --section-groups    Print the contents of the section groups.\n\
73582c23cb7cSEd Maste   -h | --file-header       Print the file header for the object.\n\
73592c23cb7cSEd Maste   -l | --program-headers   Print the PHDR table for the object.\n\
73602c23cb7cSEd Maste   -n | --notes             Print the contents of SHT_NOTE sections.\n\
73612c23cb7cSEd Maste   -p INDEX | --string-dump=INDEX\n\
73622c23cb7cSEd Maste                            Print the contents of section at index INDEX.\n\
73632c23cb7cSEd Maste   -r | --relocs            Print relocation information.\n\
73642c23cb7cSEd Maste   -s | --syms | --symbols  Print symbol tables.\n\
73652c23cb7cSEd Maste   -t | --section-details   Print additional information about sections.\n\
73662c23cb7cSEd Maste   -v | --version           Print a version identifier and exit.\n\
7367839529caSEd Maste   -w[afilmoprsFLR] | --debug-dump={abbrev,aranges,decodedline,frames,\n\
7368839529caSEd Maste                                frames-interp,info,loc,macro,pubnames,\n\
7369839529caSEd Maste                                ranges,Ranges,rawline,str}\n\
7370839529caSEd Maste                            Display DWARF information.\n\
73712c23cb7cSEd Maste   -x INDEX | --hex-dump=INDEX\n\
73722c23cb7cSEd Maste                            Display contents of a section as hexadecimal.\n\
73732c23cb7cSEd Maste   -A | --arch-specific     (accepted, but ignored)\n\
73742c23cb7cSEd Maste   -D | --use-dynamic       Print the symbol table specified by the DT_SYMTAB\n\
73752c23cb7cSEd Maste                            entry in the \".dynamic\" section.\n\
73762c23cb7cSEd Maste   -H | --help              Print a help message.\n\
73772c23cb7cSEd Maste   -I | --histogram         Print information on bucket list lengths for \n\
73782c23cb7cSEd Maste                            hash sections.\n\
73792c23cb7cSEd Maste   -N | --full-section-name (accepted, but ignored)\n\
73802c23cb7cSEd Maste   -S | --sections | --section-headers\n\
73812c23cb7cSEd Maste                            Print information about section headers.\n\
73822c23cb7cSEd Maste   -V | --version-info      Print symbol versoning information.\n\
73832c23cb7cSEd Maste   -W | --wide              Print information without wrapping long lines.\n"
73842c23cb7cSEd Maste 
73852c23cb7cSEd Maste 
73862c23cb7cSEd Maste static void
7387839529caSEd Maste readelf_usage(int status)
73882c23cb7cSEd Maste {
73892c23cb7cSEd Maste 	fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());
7390839529caSEd Maste 	exit(status);
73912c23cb7cSEd Maste }
73922c23cb7cSEd Maste 
73932c23cb7cSEd Maste int
73942c23cb7cSEd Maste main(int argc, char **argv)
73952c23cb7cSEd Maste {
73962c23cb7cSEd Maste 	struct readelf	*re, re_storage;
73972c23cb7cSEd Maste 	unsigned long	 si;
73982c23cb7cSEd Maste 	int		 opt, i;
73992c23cb7cSEd Maste 	char		*ep;
74002c23cb7cSEd Maste 
74012c23cb7cSEd Maste 	re = &re_storage;
74022c23cb7cSEd Maste 	memset(re, 0, sizeof(*re));
74032c23cb7cSEd Maste 	STAILQ_INIT(&re->v_dumpop);
74042c23cb7cSEd Maste 
74052c23cb7cSEd Maste 	while ((opt = getopt_long(argc, argv, "AacDdegHhIi:lNnp:rSstuVvWw::x:",
74062c23cb7cSEd Maste 	    longopts, NULL)) != -1) {
74072c23cb7cSEd Maste 		switch(opt) {
74082c23cb7cSEd Maste 		case '?':
7409839529caSEd Maste 			readelf_usage(EXIT_SUCCESS);
74102c23cb7cSEd Maste 			break;
74112c23cb7cSEd Maste 		case 'A':
74122c23cb7cSEd Maste 			re->options |= RE_AA;
74132c23cb7cSEd Maste 			break;
74142c23cb7cSEd Maste 		case 'a':
74153ef90571SEd Maste 			re->options |= RE_AA | RE_D | RE_G | RE_H | RE_II |
74163ef90571SEd Maste 			    RE_L | RE_R | RE_SS | RE_S | RE_VV;
74172c23cb7cSEd Maste 			break;
74182c23cb7cSEd Maste 		case 'c':
74192c23cb7cSEd Maste 			re->options |= RE_C;
74202c23cb7cSEd Maste 			break;
74212c23cb7cSEd Maste 		case 'D':
74222c23cb7cSEd Maste 			re->options |= RE_DD;
74232c23cb7cSEd Maste 			break;
74242c23cb7cSEd Maste 		case 'd':
74252c23cb7cSEd Maste 			re->options |= RE_D;
74262c23cb7cSEd Maste 			break;
74272c23cb7cSEd Maste 		case 'e':
74282c23cb7cSEd Maste 			re->options |= RE_H | RE_L | RE_SS;
74292c23cb7cSEd Maste 			break;
74302c23cb7cSEd Maste 		case 'g':
74312c23cb7cSEd Maste 			re->options |= RE_G;
74322c23cb7cSEd Maste 			break;
74332c23cb7cSEd Maste 		case 'H':
7434839529caSEd Maste 			readelf_usage(EXIT_SUCCESS);
74352c23cb7cSEd Maste 			break;
74362c23cb7cSEd Maste 		case 'h':
74372c23cb7cSEd Maste 			re->options |= RE_H;
74382c23cb7cSEd Maste 			break;
74392c23cb7cSEd Maste 		case 'I':
74402c23cb7cSEd Maste 			re->options |= RE_II;
74412c23cb7cSEd Maste 			break;
74422c23cb7cSEd Maste 		case 'i':
74432c23cb7cSEd Maste 			/* Not implemented yet. */
74442c23cb7cSEd Maste 			break;
74452c23cb7cSEd Maste 		case 'l':
74462c23cb7cSEd Maste 			re->options |= RE_L;
74472c23cb7cSEd Maste 			break;
74482c23cb7cSEd Maste 		case 'N':
74492c23cb7cSEd Maste 			re->options |= RE_NN;
74502c23cb7cSEd Maste 			break;
74512c23cb7cSEd Maste 		case 'n':
74522c23cb7cSEd Maste 			re->options |= RE_N;
74532c23cb7cSEd Maste 			break;
74542c23cb7cSEd Maste 		case 'p':
74552c23cb7cSEd Maste 			re->options |= RE_P;
74562c23cb7cSEd Maste 			si = strtoul(optarg, &ep, 10);
74572c23cb7cSEd Maste 			if (*ep == '\0')
74582c23cb7cSEd Maste 				add_dumpop(re, (size_t) si, NULL, STR_DUMP,
74592c23cb7cSEd Maste 				    DUMP_BY_INDEX);
74602c23cb7cSEd Maste 			else
74612c23cb7cSEd Maste 				add_dumpop(re, 0, optarg, STR_DUMP,
74622c23cb7cSEd Maste 				    DUMP_BY_NAME);
74632c23cb7cSEd Maste 			break;
74642c23cb7cSEd Maste 		case 'r':
74652c23cb7cSEd Maste 			re->options |= RE_R;
74662c23cb7cSEd Maste 			break;
74672c23cb7cSEd Maste 		case 'S':
74682c23cb7cSEd Maste 			re->options |= RE_SS;
74692c23cb7cSEd Maste 			break;
74702c23cb7cSEd Maste 		case 's':
74712c23cb7cSEd Maste 			re->options |= RE_S;
74722c23cb7cSEd Maste 			break;
74732c23cb7cSEd Maste 		case 't':
74742c23cb7cSEd Maste 			re->options |= RE_T;
74752c23cb7cSEd Maste 			break;
74762c23cb7cSEd Maste 		case 'u':
74772c23cb7cSEd Maste 			re->options |= RE_U;
74782c23cb7cSEd Maste 			break;
74792c23cb7cSEd Maste 		case 'V':
74802c23cb7cSEd Maste 			re->options |= RE_VV;
74812c23cb7cSEd Maste 			break;
74822c23cb7cSEd Maste 		case 'v':
74832c23cb7cSEd Maste 			readelf_version();
74842c23cb7cSEd Maste 			break;
74852c23cb7cSEd Maste 		case 'W':
74862c23cb7cSEd Maste 			re->options |= RE_WW;
74872c23cb7cSEd Maste 			break;
74882c23cb7cSEd Maste 		case 'w':
74892c23cb7cSEd Maste 			re->options |= RE_W;
74902c23cb7cSEd Maste 			parse_dwarf_op_short(re, optarg);
74912c23cb7cSEd Maste 			break;
74922c23cb7cSEd Maste 		case 'x':
74932c23cb7cSEd Maste 			re->options |= RE_X;
74942c23cb7cSEd Maste 			si = strtoul(optarg, &ep, 10);
74952c23cb7cSEd Maste 			if (*ep == '\0')
74962c23cb7cSEd Maste 				add_dumpop(re, (size_t) si, NULL, HEX_DUMP,
74972c23cb7cSEd Maste 				    DUMP_BY_INDEX);
74982c23cb7cSEd Maste 			else
74992c23cb7cSEd Maste 				add_dumpop(re, 0, optarg, HEX_DUMP,
75002c23cb7cSEd Maste 				    DUMP_BY_NAME);
75012c23cb7cSEd Maste 			break;
75022c23cb7cSEd Maste 		case OPTION_DEBUG_DUMP:
75032c23cb7cSEd Maste 			re->options |= RE_W;
75042c23cb7cSEd Maste 			parse_dwarf_op_long(re, optarg);
75052c23cb7cSEd Maste 		}
75062c23cb7cSEd Maste 	}
75072c23cb7cSEd Maste 
75082c23cb7cSEd Maste 	argv += optind;
75092c23cb7cSEd Maste 	argc -= optind;
75102c23cb7cSEd Maste 
75112c23cb7cSEd Maste 	if (argc == 0 || re->options == 0)
7512839529caSEd Maste 		readelf_usage(EXIT_FAILURE);
75132c23cb7cSEd Maste 
75142c23cb7cSEd Maste 	if (argc > 1)
75152c23cb7cSEd Maste 		re->flags |= DISPLAY_FILENAME;
75162c23cb7cSEd Maste 
75172c23cb7cSEd Maste 	if (elf_version(EV_CURRENT) == EV_NONE)
75182c23cb7cSEd Maste 		errx(EXIT_FAILURE, "ELF library initialization failed: %s",
75192c23cb7cSEd Maste 		    elf_errmsg(-1));
75202c23cb7cSEd Maste 
7521b00fe64fSEd Maste 	for (i = 0; i < argc; i++) {
75222c23cb7cSEd Maste 		re->filename = argv[i];
75232c23cb7cSEd Maste 		dump_object(re);
75242c23cb7cSEd Maste 	}
75252c23cb7cSEd Maste 
75262c23cb7cSEd Maste 	exit(EXIT_SUCCESS);
75272c23cb7cSEd Maste }
7528