xref: /freebsd/contrib/elftoolchain/readelf/readelf.c (revision d0bd2dad6f56e642ccb69de65a74e011c52da5a3)
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 
50b6d812d2SEd Maste ELFTC_VCSID("$Id: readelf.c 3484 2016-08-03 13:36:49Z 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 
2232c23cb7cSEd Maste static void add_dumpop(struct readelf *re, size_t si, const char *sn, int op,
2242c23cb7cSEd Maste     int t);
2252c23cb7cSEd Maste static const char *aeabi_adv_simd_arch(uint64_t simd);
2262c23cb7cSEd Maste static const char *aeabi_align_needed(uint64_t an);
2272c23cb7cSEd Maste static const char *aeabi_align_preserved(uint64_t ap);
2282c23cb7cSEd Maste static const char *aeabi_arm_isa(uint64_t ai);
2292c23cb7cSEd Maste static const char *aeabi_cpu_arch(uint64_t arch);
2302c23cb7cSEd Maste static const char *aeabi_cpu_arch_profile(uint64_t pf);
2312c23cb7cSEd Maste static const char *aeabi_div(uint64_t du);
2322c23cb7cSEd Maste static const char *aeabi_enum_size(uint64_t es);
2332c23cb7cSEd Maste static const char *aeabi_fp_16bit_format(uint64_t fp16);
2342c23cb7cSEd Maste static const char *aeabi_fp_arch(uint64_t fp);
2352c23cb7cSEd Maste static const char *aeabi_fp_denormal(uint64_t fd);
2362c23cb7cSEd Maste static const char *aeabi_fp_exceptions(uint64_t fe);
2372c23cb7cSEd Maste static const char *aeabi_fp_hpext(uint64_t fh);
2382c23cb7cSEd Maste static const char *aeabi_fp_number_model(uint64_t fn);
2392c23cb7cSEd Maste static const char *aeabi_fp_optm_goal(uint64_t fog);
2402c23cb7cSEd Maste static const char *aeabi_fp_rounding(uint64_t fr);
2412c23cb7cSEd Maste static const char *aeabi_hardfp(uint64_t hfp);
2422c23cb7cSEd Maste static const char *aeabi_mpext(uint64_t mp);
2432c23cb7cSEd Maste static const char *aeabi_optm_goal(uint64_t og);
2442c23cb7cSEd Maste static const char *aeabi_pcs_config(uint64_t pcs);
2452c23cb7cSEd Maste static const char *aeabi_pcs_got(uint64_t got);
2462c23cb7cSEd Maste static const char *aeabi_pcs_r9(uint64_t r9);
2472c23cb7cSEd Maste static const char *aeabi_pcs_ro(uint64_t ro);
2482c23cb7cSEd Maste static const char *aeabi_pcs_rw(uint64_t rw);
2492c23cb7cSEd Maste static const char *aeabi_pcs_wchar_t(uint64_t wt);
2502c23cb7cSEd Maste static const char *aeabi_t2ee(uint64_t t2ee);
2512c23cb7cSEd Maste static const char *aeabi_thumb_isa(uint64_t ti);
2522c23cb7cSEd Maste static const char *aeabi_fp_user_exceptions(uint64_t fu);
2532c23cb7cSEd Maste static const char *aeabi_unaligned_access(uint64_t ua);
2542c23cb7cSEd Maste static const char *aeabi_vfp_args(uint64_t va);
2552c23cb7cSEd Maste static const char *aeabi_virtual(uint64_t vt);
2562c23cb7cSEd Maste static const char *aeabi_wmmx_arch(uint64_t wmmx);
2572c23cb7cSEd Maste static const char *aeabi_wmmx_args(uint64_t wa);
2582c23cb7cSEd Maste static const char *elf_class(unsigned int class);
2592c23cb7cSEd Maste static const char *elf_endian(unsigned int endian);
2602c23cb7cSEd Maste static const char *elf_machine(unsigned int mach);
2612c23cb7cSEd Maste static const char *elf_osabi(unsigned int abi);
2622c23cb7cSEd Maste static const char *elf_type(unsigned int type);
2632c23cb7cSEd Maste static const char *elf_ver(unsigned int ver);
2642c23cb7cSEd Maste static const char *dt_type(unsigned int mach, unsigned int dtype);
2652c23cb7cSEd Maste static void dump_ar(struct readelf *re, int);
2662c23cb7cSEd Maste static void dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe);
2672c23cb7cSEd Maste static void dump_attributes(struct readelf *re);
26895fd7f26SEd Maste static uint8_t *dump_compatibility_tag(uint8_t *p, uint8_t *pe);
2692c23cb7cSEd Maste static void dump_dwarf(struct readelf *re);
270cf781b2eSEd Maste static void dump_dwarf_abbrev(struct readelf *re);
271cf781b2eSEd Maste static void dump_dwarf_aranges(struct readelf *re);
272cf781b2eSEd Maste static void dump_dwarf_block(struct readelf *re, uint8_t *b,
273cf781b2eSEd Maste     Dwarf_Unsigned len);
274cf781b2eSEd Maste static void dump_dwarf_die(struct readelf *re, Dwarf_Die die, int level);
275cf781b2eSEd Maste static void dump_dwarf_frame(struct readelf *re, int alt);
276cf781b2eSEd Maste static void dump_dwarf_frame_inst(struct readelf *re, Dwarf_Cie cie,
277cf781b2eSEd Maste     uint8_t *insts, Dwarf_Unsigned len, Dwarf_Unsigned caf, Dwarf_Signed daf,
278cf781b2eSEd Maste     Dwarf_Addr pc, Dwarf_Debug dbg);
279cf781b2eSEd Maste static int dump_dwarf_frame_regtable(struct readelf *re, Dwarf_Fde fde,
280cf781b2eSEd Maste     Dwarf_Addr pc, Dwarf_Unsigned func_len, Dwarf_Half cie_ra);
281cf781b2eSEd Maste static void dump_dwarf_frame_section(struct readelf *re, struct section *s,
282cf781b2eSEd Maste     int alt);
283cf781b2eSEd Maste static void dump_dwarf_info(struct readelf *re, Dwarf_Bool is_info);
284cf781b2eSEd Maste static void dump_dwarf_macinfo(struct readelf *re);
285cf781b2eSEd Maste static void dump_dwarf_line(struct readelf *re);
286cf781b2eSEd Maste static void dump_dwarf_line_decoded(struct readelf *re);
287cf781b2eSEd Maste static void dump_dwarf_loc(struct readelf *re, Dwarf_Loc *lr);
288cf781b2eSEd Maste static void dump_dwarf_loclist(struct readelf *re);
289cf781b2eSEd Maste static void dump_dwarf_pubnames(struct readelf *re);
290cf781b2eSEd Maste static void dump_dwarf_ranges(struct readelf *re);
291cf781b2eSEd Maste static void dump_dwarf_ranges_foreach(struct readelf *re, Dwarf_Die die,
292cf781b2eSEd Maste     Dwarf_Addr base);
293cf781b2eSEd Maste static void dump_dwarf_str(struct readelf *re);
2942c23cb7cSEd Maste static void dump_eflags(struct readelf *re, uint64_t e_flags);
2952c23cb7cSEd Maste static void dump_elf(struct readelf *re);
2962c23cb7cSEd Maste static void dump_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab);
2972c23cb7cSEd Maste static void dump_dynamic(struct readelf *re);
2982c23cb7cSEd Maste static void dump_liblist(struct readelf *re);
2992c23cb7cSEd Maste static void dump_mips_attributes(struct readelf *re, uint8_t *p, uint8_t *pe);
3002c23cb7cSEd Maste static void dump_mips_odk_reginfo(struct readelf *re, uint8_t *p, size_t sz);
3012c23cb7cSEd Maste static void dump_mips_options(struct readelf *re, struct section *s);
3022c23cb7cSEd Maste static void dump_mips_option_flags(const char *name, struct mips_option *opt,
3032c23cb7cSEd Maste     uint64_t info);
3042c23cb7cSEd Maste static void dump_mips_reginfo(struct readelf *re, struct section *s);
3052c23cb7cSEd Maste static void dump_mips_specific_info(struct readelf *re);
3062c23cb7cSEd Maste static void dump_notes(struct readelf *re);
3072c23cb7cSEd Maste static void dump_notes_content(struct readelf *re, const char *buf, size_t sz,
3082c23cb7cSEd Maste     off_t off);
3092c23cb7cSEd Maste static void dump_svr4_hash(struct section *s);
3102c23cb7cSEd Maste static void dump_svr4_hash64(struct readelf *re, struct section *s);
3112c23cb7cSEd Maste static void dump_gnu_hash(struct readelf *re, struct section *s);
3122c23cb7cSEd Maste static void dump_hash(struct readelf *re);
3132c23cb7cSEd Maste static void dump_phdr(struct readelf *re);
3142c23cb7cSEd Maste static void dump_ppc_attributes(uint8_t *p, uint8_t *pe);
3153ef90571SEd Maste static void dump_section_groups(struct readelf *re);
3162c23cb7cSEd Maste static void dump_symtab(struct readelf *re, int i);
3172c23cb7cSEd Maste static void dump_symtabs(struct readelf *re);
31895fd7f26SEd Maste static uint8_t *dump_unknown_tag(uint64_t tag, uint8_t *p, uint8_t *pe);
3192c23cb7cSEd Maste static void dump_ver(struct readelf *re);
3202c23cb7cSEd Maste static void dump_verdef(struct readelf *re, int dump);
3212c23cb7cSEd Maste static void dump_verneed(struct readelf *re, int dump);
3222c23cb7cSEd Maste static void dump_versym(struct readelf *re);
323cf781b2eSEd Maste static const char *dwarf_reg(unsigned int mach, unsigned int reg);
324cf781b2eSEd Maste static const char *dwarf_regname(struct readelf *re, unsigned int num);
325cf781b2eSEd Maste static struct dumpop *find_dumpop(struct readelf *re, size_t si,
326cf781b2eSEd Maste     const char *sn, int op, int t);
32771edbbfdSEd Maste static int get_ent_count(struct section *s, int *ent_count);
328cf781b2eSEd Maste static char *get_regoff_str(struct readelf *re, Dwarf_Half reg,
329cf781b2eSEd Maste     Dwarf_Addr off);
3302c23cb7cSEd Maste static const char *get_string(struct readelf *re, int strtab, size_t off);
3312c23cb7cSEd Maste static const char *get_symbol_name(struct readelf *re, int symtab, int i);
3322c23cb7cSEd Maste static uint64_t get_symbol_value(struct readelf *re, int symtab, int i);
3332c23cb7cSEd Maste static void load_sections(struct readelf *re);
3342c23cb7cSEd Maste static const char *mips_abi_fp(uint64_t fp);
33502b08c90SEd Maste static const char *note_type(const char *note_name, unsigned int et,
3362c23cb7cSEd Maste     unsigned int nt);
33702b08c90SEd Maste static const char *note_type_freebsd(unsigned int nt);
33802b08c90SEd Maste static const char *note_type_freebsd_core(unsigned int nt);
33902b08c90SEd Maste static const char *note_type_linux_core(unsigned int nt);
34002b08c90SEd Maste static const char *note_type_gnu(unsigned int nt);
34102b08c90SEd Maste static const char *note_type_netbsd(unsigned int nt);
34202b08c90SEd Maste static const char *note_type_openbsd(unsigned int nt);
34302b08c90SEd Maste static const char *note_type_unknown(unsigned int nt);
344895f86f1SEd Maste static const char *note_type_xen(unsigned int nt);
3452c23cb7cSEd Maste static const char *option_kind(uint8_t kind);
34620136ffcSEd Maste static const char *phdr_type(unsigned int mach, unsigned int ptype);
3472c23cb7cSEd Maste static const char *ppc_abi_fp(uint64_t fp);
3482c23cb7cSEd Maste static const char *ppc_abi_vector(uint64_t vec);
349839529caSEd Maste static void readelf_usage(int status);
3502c23cb7cSEd Maste static void readelf_version(void);
351cf781b2eSEd Maste static void search_loclist_at(struct readelf *re, Dwarf_Die die,
352cf781b2eSEd Maste     Dwarf_Unsigned lowpc);
3532c23cb7cSEd Maste static void search_ver(struct readelf *re);
3542c23cb7cSEd Maste static const char *section_type(unsigned int mach, unsigned int stype);
355cf781b2eSEd Maste static void set_cu_context(struct readelf *re, Dwarf_Half psize,
356cf781b2eSEd Maste     Dwarf_Half osize, Dwarf_Half ver);
3572c23cb7cSEd Maste static const char *st_bind(unsigned int sbind);
3582c23cb7cSEd Maste static const char *st_shndx(unsigned int shndx);
359b6b6f9ccSEd Maste static const char *st_type(unsigned int mach, unsigned int os,
360b6b6f9ccSEd Maste     unsigned int stype);
3612c23cb7cSEd Maste static const char *st_vis(unsigned int svis);
3622c23cb7cSEd Maste static const char *top_tag(unsigned int tag);
3632c23cb7cSEd Maste static void unload_sections(struct readelf *re);
3642c23cb7cSEd Maste static uint64_t _read_lsb(Elf_Data *d, uint64_t *offsetp,
3652c23cb7cSEd Maste     int bytes_to_read);
3662c23cb7cSEd Maste static uint64_t _read_msb(Elf_Data *d, uint64_t *offsetp,
3672c23cb7cSEd Maste     int bytes_to_read);
3682c23cb7cSEd Maste static uint64_t _decode_lsb(uint8_t **data, int bytes_to_read);
3692c23cb7cSEd Maste static uint64_t _decode_msb(uint8_t **data, int bytes_to_read);
37095fd7f26SEd Maste static int64_t _decode_sleb128(uint8_t **dp, uint8_t *dpe);
37195fd7f26SEd Maste static uint64_t _decode_uleb128(uint8_t **dp, uint8_t *dpe);
3722c23cb7cSEd Maste 
3732c23cb7cSEd Maste static struct eflags_desc arm_eflags_desc[] = {
3742c23cb7cSEd Maste 	{EF_ARM_RELEXEC, "relocatable executable"},
3752c23cb7cSEd Maste 	{EF_ARM_HASENTRY, "has entry point"},
3762c23cb7cSEd Maste 	{EF_ARM_SYMSARESORTED, "sorted symbol tables"},
3772c23cb7cSEd Maste 	{EF_ARM_DYNSYMSUSESEGIDX, "dynamic symbols use segment index"},
3782c23cb7cSEd Maste 	{EF_ARM_MAPSYMSFIRST, "mapping symbols precede others"},
3792c23cb7cSEd Maste 	{EF_ARM_BE8, "BE8"},
3802c23cb7cSEd Maste 	{EF_ARM_LE8, "LE8"},
3812c23cb7cSEd Maste 	{EF_ARM_INTERWORK, "interworking enabled"},
3822c23cb7cSEd Maste 	{EF_ARM_APCS_26, "uses APCS/26"},
3832c23cb7cSEd Maste 	{EF_ARM_APCS_FLOAT, "uses APCS/float"},
3842c23cb7cSEd Maste 	{EF_ARM_PIC, "position independent"},
3852c23cb7cSEd Maste 	{EF_ARM_ALIGN8, "8 bit structure alignment"},
3862c23cb7cSEd Maste 	{EF_ARM_NEW_ABI, "uses new ABI"},
3872c23cb7cSEd Maste 	{EF_ARM_OLD_ABI, "uses old ABI"},
3882c23cb7cSEd Maste 	{EF_ARM_SOFT_FLOAT, "software FP"},
3892c23cb7cSEd Maste 	{EF_ARM_VFP_FLOAT, "VFP"},
3902c23cb7cSEd Maste 	{EF_ARM_MAVERICK_FLOAT, "Maverick FP"},
3912c23cb7cSEd Maste 	{0, NULL}
3922c23cb7cSEd Maste };
3932c23cb7cSEd Maste 
3942c23cb7cSEd Maste static struct eflags_desc mips_eflags_desc[] = {
3952c23cb7cSEd Maste 	{EF_MIPS_NOREORDER, "noreorder"},
3962c23cb7cSEd Maste 	{EF_MIPS_PIC, "pic"},
3972c23cb7cSEd Maste 	{EF_MIPS_CPIC, "cpic"},
3982c23cb7cSEd Maste 	{EF_MIPS_UCODE, "ugen_reserved"},
3992c23cb7cSEd Maste 	{EF_MIPS_ABI2, "abi2"},
4002c23cb7cSEd Maste 	{EF_MIPS_OPTIONS_FIRST, "odk first"},
4012c23cb7cSEd Maste 	{EF_MIPS_ARCH_ASE_MDMX, "mdmx"},
4022c23cb7cSEd Maste 	{EF_MIPS_ARCH_ASE_M16, "mips16"},
4032c23cb7cSEd Maste 	{0, NULL}
4042c23cb7cSEd Maste };
4052c23cb7cSEd Maste 
4062c23cb7cSEd Maste static struct eflags_desc powerpc_eflags_desc[] = {
4072c23cb7cSEd Maste 	{EF_PPC_EMB, "emb"},
4082c23cb7cSEd Maste 	{EF_PPC_RELOCATABLE, "relocatable"},
4092c23cb7cSEd Maste 	{EF_PPC_RELOCATABLE_LIB, "relocatable-lib"},
4102c23cb7cSEd Maste 	{0, NULL}
4112c23cb7cSEd Maste };
4122c23cb7cSEd Maste 
4132c23cb7cSEd Maste static struct eflags_desc sparc_eflags_desc[] = {
4142c23cb7cSEd Maste 	{EF_SPARC_32PLUS, "v8+"},
4152c23cb7cSEd Maste 	{EF_SPARC_SUN_US1, "ultrasparcI"},
4162c23cb7cSEd Maste 	{EF_SPARC_HAL_R1, "halr1"},
4172c23cb7cSEd Maste 	{EF_SPARC_SUN_US3, "ultrasparcIII"},
4182c23cb7cSEd Maste 	{0, NULL}
4192c23cb7cSEd Maste };
4202c23cb7cSEd Maste 
4212c23cb7cSEd Maste static const char *
4222c23cb7cSEd Maste elf_osabi(unsigned int abi)
4232c23cb7cSEd Maste {
4242c23cb7cSEd Maste 	static char s_abi[32];
4252c23cb7cSEd Maste 
4262c23cb7cSEd Maste 	switch(abi) {
427453b09caSEd Maste 	case ELFOSABI_NONE: return "NONE";
428473c31f1SEd Maste 	case ELFOSABI_HPUX: return "HPUX";
4292c23cb7cSEd Maste 	case ELFOSABI_NETBSD: return "NetBSD";
4302c23cb7cSEd Maste 	case ELFOSABI_GNU: return "GNU";
4312c23cb7cSEd Maste 	case ELFOSABI_HURD: return "HURD";
4322c23cb7cSEd Maste 	case ELFOSABI_86OPEN: return "86OPEN";
4332c23cb7cSEd Maste 	case ELFOSABI_SOLARIS: return "Solaris";
4342c23cb7cSEd Maste 	case ELFOSABI_AIX: return "AIX";
4352c23cb7cSEd Maste 	case ELFOSABI_IRIX: return "IRIX";
4362c23cb7cSEd Maste 	case ELFOSABI_FREEBSD: return "FreeBSD";
4372c23cb7cSEd Maste 	case ELFOSABI_TRU64: return "TRU64";
4382c23cb7cSEd Maste 	case ELFOSABI_MODESTO: return "MODESTO";
4392c23cb7cSEd Maste 	case ELFOSABI_OPENBSD: return "OpenBSD";
4402c23cb7cSEd Maste 	case ELFOSABI_OPENVMS: return "OpenVMS";
4412c23cb7cSEd Maste 	case ELFOSABI_NSK: return "NSK";
442b6b6f9ccSEd Maste 	case ELFOSABI_CLOUDABI: return "CloudABI";
443b6d812d2SEd Maste 	case ELFOSABI_ARM_AEABI: return "ARM EABI";
4442c23cb7cSEd Maste 	case ELFOSABI_ARM: return "ARM";
4452c23cb7cSEd Maste 	case ELFOSABI_STANDALONE: return "StandAlone";
4462c23cb7cSEd Maste 	default:
4472c23cb7cSEd Maste 		snprintf(s_abi, sizeof(s_abi), "<unknown: %#x>", abi);
4482c23cb7cSEd Maste 		return (s_abi);
4492c23cb7cSEd Maste 	}
4502c23cb7cSEd Maste };
4512c23cb7cSEd Maste 
4522c23cb7cSEd Maste static const char *
4532c23cb7cSEd Maste elf_machine(unsigned int mach)
4542c23cb7cSEd Maste {
4552c23cb7cSEd Maste 	static char s_mach[32];
4562c23cb7cSEd Maste 
4572c23cb7cSEd Maste 	switch (mach) {
4582c23cb7cSEd Maste 	case EM_NONE: return "Unknown machine";
4592c23cb7cSEd Maste 	case EM_M32: return "AT&T WE32100";
4602c23cb7cSEd Maste 	case EM_SPARC: return "Sun SPARC";
4612c23cb7cSEd Maste 	case EM_386: return "Intel i386";
4622c23cb7cSEd Maste 	case EM_68K: return "Motorola 68000";
4633ef90571SEd Maste 	case EM_IAMCU: return "Intel MCU";
4642c23cb7cSEd Maste 	case EM_88K: return "Motorola 88000";
4652c23cb7cSEd Maste 	case EM_860: return "Intel i860";
4662c23cb7cSEd Maste 	case EM_MIPS: return "MIPS R3000 Big-Endian only";
4672c23cb7cSEd Maste 	case EM_S370: return "IBM System/370";
4682c23cb7cSEd Maste 	case EM_MIPS_RS3_LE: return "MIPS R3000 Little-Endian";
4692c23cb7cSEd Maste 	case EM_PARISC: return "HP PA-RISC";
4702c23cb7cSEd Maste 	case EM_VPP500: return "Fujitsu VPP500";
4712c23cb7cSEd Maste 	case EM_SPARC32PLUS: return "SPARC v8plus";
4722c23cb7cSEd Maste 	case EM_960: return "Intel 80960";
4732c23cb7cSEd Maste 	case EM_PPC: return "PowerPC 32-bit";
4742c23cb7cSEd Maste 	case EM_PPC64: return "PowerPC 64-bit";
4752c23cb7cSEd Maste 	case EM_S390: return "IBM System/390";
4762c23cb7cSEd Maste 	case EM_V800: return "NEC V800";
4772c23cb7cSEd Maste 	case EM_FR20: return "Fujitsu FR20";
4782c23cb7cSEd Maste 	case EM_RH32: return "TRW RH-32";
4792c23cb7cSEd Maste 	case EM_RCE: return "Motorola RCE";
4802c23cb7cSEd Maste 	case EM_ARM: return "ARM";
4812c23cb7cSEd Maste 	case EM_SH: return "Hitachi SH";
4822c23cb7cSEd Maste 	case EM_SPARCV9: return "SPARC v9 64-bit";
4832c23cb7cSEd Maste 	case EM_TRICORE: return "Siemens TriCore embedded processor";
4842c23cb7cSEd Maste 	case EM_ARC: return "Argonaut RISC Core";
4852c23cb7cSEd Maste 	case EM_H8_300: return "Hitachi H8/300";
4862c23cb7cSEd Maste 	case EM_H8_300H: return "Hitachi H8/300H";
4872c23cb7cSEd Maste 	case EM_H8S: return "Hitachi H8S";
4882c23cb7cSEd Maste 	case EM_H8_500: return "Hitachi H8/500";
4892c23cb7cSEd Maste 	case EM_IA_64: return "Intel IA-64 Processor";
4902c23cb7cSEd Maste 	case EM_MIPS_X: return "Stanford MIPS-X";
4912c23cb7cSEd Maste 	case EM_COLDFIRE: return "Motorola ColdFire";
4922c23cb7cSEd Maste 	case EM_68HC12: return "Motorola M68HC12";
4932c23cb7cSEd Maste 	case EM_MMA: return "Fujitsu MMA";
4942c23cb7cSEd Maste 	case EM_PCP: return "Siemens PCP";
4952c23cb7cSEd Maste 	case EM_NCPU: return "Sony nCPU";
4962c23cb7cSEd Maste 	case EM_NDR1: return "Denso NDR1 microprocessor";
4972c23cb7cSEd Maste 	case EM_STARCORE: return "Motorola Star*Core processor";
4982c23cb7cSEd Maste 	case EM_ME16: return "Toyota ME16 processor";
4992c23cb7cSEd Maste 	case EM_ST100: return "STMicroelectronics ST100 processor";
5002c23cb7cSEd Maste 	case EM_TINYJ: return "Advanced Logic Corp. TinyJ processor";
5012c23cb7cSEd Maste 	case EM_X86_64: return "Advanced Micro Devices x86-64";
5022c23cb7cSEd Maste 	case EM_PDSP: return "Sony DSP Processor";
5032c23cb7cSEd Maste 	case EM_FX66: return "Siemens FX66 microcontroller";
5042c23cb7cSEd Maste 	case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 microcontroller";
5052c23cb7cSEd Maste 	case EM_ST7: return "STmicroelectronics ST7 8-bit microcontroller";
5062c23cb7cSEd Maste 	case EM_68HC16: return "Motorola MC68HC16 microcontroller";
5072c23cb7cSEd Maste 	case EM_68HC11: return "Motorola MC68HC11 microcontroller";
5082c23cb7cSEd Maste 	case EM_68HC08: return "Motorola MC68HC08 microcontroller";
5092c23cb7cSEd Maste 	case EM_68HC05: return "Motorola MC68HC05 microcontroller";
5102c23cb7cSEd Maste 	case EM_SVX: return "Silicon Graphics SVx";
5112c23cb7cSEd Maste 	case EM_ST19: return "STMicroelectronics ST19 8-bit mc";
5122c23cb7cSEd Maste 	case EM_VAX: return "Digital VAX";
5132c23cb7cSEd Maste 	case EM_CRIS: return "Axis Communications 32-bit embedded processor";
5142c23cb7cSEd Maste 	case EM_JAVELIN: return "Infineon Tech. 32bit embedded processor";
5152c23cb7cSEd Maste 	case EM_FIREPATH: return "Element 14 64-bit DSP Processor";
5162c23cb7cSEd Maste 	case EM_ZSP: return "LSI Logic 16-bit DSP Processor";
5172c23cb7cSEd Maste 	case EM_MMIX: return "Donald Knuth's educational 64-bit proc";
5182c23cb7cSEd Maste 	case EM_HUANY: return "Harvard University MI object files";
5192c23cb7cSEd Maste 	case EM_PRISM: return "SiTera Prism";
5202c23cb7cSEd Maste 	case EM_AVR: return "Atmel AVR 8-bit microcontroller";
5212c23cb7cSEd Maste 	case EM_FR30: return "Fujitsu FR30";
5222c23cb7cSEd Maste 	case EM_D10V: return "Mitsubishi D10V";
5232c23cb7cSEd Maste 	case EM_D30V: return "Mitsubishi D30V";
5242c23cb7cSEd Maste 	case EM_V850: return "NEC v850";
5252c23cb7cSEd Maste 	case EM_M32R: return "Mitsubishi M32R";
5262c23cb7cSEd Maste 	case EM_MN10300: return "Matsushita MN10300";
5272c23cb7cSEd Maste 	case EM_MN10200: return "Matsushita MN10200";
5282c23cb7cSEd Maste 	case EM_PJ: return "picoJava";
5292c23cb7cSEd Maste 	case EM_OPENRISC: return "OpenRISC 32-bit embedded processor";
5302c23cb7cSEd Maste 	case EM_ARC_A5: return "ARC Cores Tangent-A5";
5312c23cb7cSEd Maste 	case EM_XTENSA: return "Tensilica Xtensa Architecture";
5322c23cb7cSEd Maste 	case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
5332c23cb7cSEd Maste 	case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
5342c23cb7cSEd Maste 	case EM_NS32K: return "National Semiconductor 32000 series";
5352c23cb7cSEd Maste 	case EM_TPC: return "Tenor Network TPC processor";
5362c23cb7cSEd Maste 	case EM_SNP1K: return "Trebia SNP 1000 processor";
5372c23cb7cSEd Maste 	case EM_ST200: return "STMicroelectronics ST200 microcontroller";
5382c23cb7cSEd Maste 	case EM_IP2K: return "Ubicom IP2xxx microcontroller family";
5392c23cb7cSEd Maste 	case EM_MAX: return "MAX Processor";
5402c23cb7cSEd Maste 	case EM_CR: return "National Semiconductor CompactRISC microprocessor";
5412c23cb7cSEd Maste 	case EM_F2MC16: return "Fujitsu F2MC16";
5422c23cb7cSEd Maste 	case EM_MSP430: return "TI embedded microcontroller msp430";
5432c23cb7cSEd Maste 	case EM_BLACKFIN: return "Analog Devices Blackfin (DSP) processor";
5442c23cb7cSEd Maste 	case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
5452c23cb7cSEd Maste 	case EM_SEP: return "Sharp embedded microprocessor";
5462c23cb7cSEd Maste 	case EM_ARCA: return "Arca RISC Microprocessor";
5472c23cb7cSEd Maste 	case EM_UNICORE: return "Microprocessor series from PKU-Unity Ltd";
548b3f26809SEd Maste 	case EM_AARCH64: return "AArch64";
549119b7592SEd Maste 	case EM_RISCV: return "RISC-V";
5502c23cb7cSEd Maste 	default:
5512c23cb7cSEd Maste 		snprintf(s_mach, sizeof(s_mach), "<unknown: %#x>", mach);
5522c23cb7cSEd Maste 		return (s_mach);
5532c23cb7cSEd Maste 	}
5542c23cb7cSEd Maste 
5552c23cb7cSEd Maste }
5562c23cb7cSEd Maste 
5572c23cb7cSEd Maste static const char *
5582c23cb7cSEd Maste elf_class(unsigned int class)
5592c23cb7cSEd Maste {
5602c23cb7cSEd Maste 	static char s_class[32];
5612c23cb7cSEd Maste 
5622c23cb7cSEd Maste 	switch (class) {
5632c23cb7cSEd Maste 	case ELFCLASSNONE: return "none";
5642c23cb7cSEd Maste 	case ELFCLASS32: return "ELF32";
5652c23cb7cSEd Maste 	case ELFCLASS64: return "ELF64";
5662c23cb7cSEd Maste 	default:
5672c23cb7cSEd Maste 		snprintf(s_class, sizeof(s_class), "<unknown: %#x>", class);
5682c23cb7cSEd Maste 		return (s_class);
5692c23cb7cSEd Maste 	}
5702c23cb7cSEd Maste }
5712c23cb7cSEd Maste 
5722c23cb7cSEd Maste static const char *
5732c23cb7cSEd Maste elf_endian(unsigned int endian)
5742c23cb7cSEd Maste {
5752c23cb7cSEd Maste 	static char s_endian[32];
5762c23cb7cSEd Maste 
5772c23cb7cSEd Maste 	switch (endian) {
5782c23cb7cSEd Maste 	case ELFDATANONE: return "none";
5792c23cb7cSEd Maste 	case ELFDATA2LSB: return "2's complement, little endian";
5802c23cb7cSEd Maste 	case ELFDATA2MSB: return "2's complement, big endian";
5812c23cb7cSEd Maste 	default:
5822c23cb7cSEd Maste 		snprintf(s_endian, sizeof(s_endian), "<unknown: %#x>", endian);
5832c23cb7cSEd Maste 		return (s_endian);
5842c23cb7cSEd Maste 	}
5852c23cb7cSEd Maste }
5862c23cb7cSEd Maste 
5872c23cb7cSEd Maste static const char *
5882c23cb7cSEd Maste elf_type(unsigned int type)
5892c23cb7cSEd Maste {
5902c23cb7cSEd Maste 	static char s_type[32];
5912c23cb7cSEd Maste 
5922c23cb7cSEd Maste 	switch (type) {
5932c23cb7cSEd Maste 	case ET_NONE: return "NONE (None)";
5942c23cb7cSEd Maste 	case ET_REL: return "REL (Relocatable file)";
5952c23cb7cSEd Maste 	case ET_EXEC: return "EXEC (Executable file)";
5962c23cb7cSEd Maste 	case ET_DYN: return "DYN (Shared object file)";
5972c23cb7cSEd Maste 	case ET_CORE: return "CORE (Core file)";
5982c23cb7cSEd Maste 	default:
5992c23cb7cSEd Maste 		if (type >= ET_LOPROC)
6002c23cb7cSEd Maste 			snprintf(s_type, sizeof(s_type), "<proc: %#x>", type);
6012c23cb7cSEd Maste 		else if (type >= ET_LOOS && type <= ET_HIOS)
6022c23cb7cSEd Maste 			snprintf(s_type, sizeof(s_type), "<os: %#x>", type);
6032c23cb7cSEd Maste 		else
6042c23cb7cSEd Maste 			snprintf(s_type, sizeof(s_type), "<unknown: %#x>",
6052c23cb7cSEd Maste 			    type);
6062c23cb7cSEd Maste 		return (s_type);
6072c23cb7cSEd Maste 	}
6082c23cb7cSEd Maste }
6092c23cb7cSEd Maste 
6102c23cb7cSEd Maste static const char *
6112c23cb7cSEd Maste elf_ver(unsigned int ver)
6122c23cb7cSEd Maste {
6132c23cb7cSEd Maste 	static char s_ver[32];
6142c23cb7cSEd Maste 
6152c23cb7cSEd Maste 	switch (ver) {
6162c23cb7cSEd Maste 	case EV_CURRENT: return "(current)";
6172c23cb7cSEd Maste 	case EV_NONE: return "(none)";
6182c23cb7cSEd Maste 	default:
6192c23cb7cSEd Maste 		snprintf(s_ver, sizeof(s_ver), "<unknown: %#x>",
6202c23cb7cSEd Maste 		    ver);
6212c23cb7cSEd Maste 		return (s_ver);
6222c23cb7cSEd Maste 	}
6232c23cb7cSEd Maste }
6242c23cb7cSEd Maste 
6252c23cb7cSEd Maste static const char *
62620136ffcSEd Maste phdr_type(unsigned int mach, unsigned int ptype)
6272c23cb7cSEd Maste {
6282c23cb7cSEd Maste 	static char s_ptype[32];
6292c23cb7cSEd Maste 
63020136ffcSEd Maste 	if (ptype >= PT_LOPROC && ptype <= PT_HIPROC) {
63120136ffcSEd Maste 		switch (mach) {
63220136ffcSEd Maste 		case EM_ARM:
63320136ffcSEd Maste 			switch (ptype) {
63420136ffcSEd Maste 			case PT_ARM_ARCHEXT: return "ARM_ARCHEXT";
63520136ffcSEd Maste 			case PT_ARM_EXIDX: return "ARM_EXIDX";
63620136ffcSEd Maste 			}
63720136ffcSEd Maste 			break;
63820136ffcSEd Maste 		}
63920136ffcSEd Maste 		snprintf(s_ptype, sizeof(s_ptype), "LOPROC+%#x",
64020136ffcSEd Maste 		    ptype - PT_LOPROC);
64120136ffcSEd Maste 		return (s_ptype);
64220136ffcSEd Maste 	}
64320136ffcSEd Maste 
6442c23cb7cSEd Maste 	switch (ptype) {
6452c23cb7cSEd Maste 	case PT_NULL: return "NULL";
6462c23cb7cSEd Maste 	case PT_LOAD: return "LOAD";
6472c23cb7cSEd Maste 	case PT_DYNAMIC: return "DYNAMIC";
6482c23cb7cSEd Maste 	case PT_INTERP: return "INTERP";
6492c23cb7cSEd Maste 	case PT_NOTE: return "NOTE";
6502c23cb7cSEd Maste 	case PT_SHLIB: return "SHLIB";
6512c23cb7cSEd Maste 	case PT_PHDR: return "PHDR";
6522c23cb7cSEd Maste 	case PT_TLS: return "TLS";
6532c23cb7cSEd Maste 	case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
6542c23cb7cSEd Maste 	case PT_GNU_STACK: return "GNU_STACK";
6552c23cb7cSEd Maste 	case PT_GNU_RELRO: return "GNU_RELRO";
6562c23cb7cSEd Maste 	default:
65720136ffcSEd Maste 		if (ptype >= PT_LOOS && ptype <= PT_HIOS)
6582c23cb7cSEd Maste 			snprintf(s_ptype, sizeof(s_ptype), "LOOS+%#x",
6592c23cb7cSEd Maste 			    ptype - PT_LOOS);
6602c23cb7cSEd Maste 		else
6612c23cb7cSEd Maste 			snprintf(s_ptype, sizeof(s_ptype), "<unknown: %#x>",
6622c23cb7cSEd Maste 			    ptype);
6632c23cb7cSEd Maste 		return (s_ptype);
6642c23cb7cSEd Maste 	}
6652c23cb7cSEd Maste }
6662c23cb7cSEd Maste 
6672c23cb7cSEd Maste static const char *
6682c23cb7cSEd Maste section_type(unsigned int mach, unsigned int stype)
6692c23cb7cSEd Maste {
6702c23cb7cSEd Maste 	static char s_stype[32];
6712c23cb7cSEd Maste 
6722c23cb7cSEd Maste 	if (stype >= SHT_LOPROC && stype <= SHT_HIPROC) {
6732c23cb7cSEd Maste 		switch (mach) {
67420136ffcSEd Maste 		case EM_ARM:
67520136ffcSEd Maste 			switch (stype) {
67620136ffcSEd Maste 			case SHT_ARM_EXIDX: return "ARM_EXIDX";
67720136ffcSEd Maste 			case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP";
67820136ffcSEd Maste 			case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES";
67920136ffcSEd Maste 			case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY";
68020136ffcSEd Maste 			case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION";
68120136ffcSEd Maste 			}
68220136ffcSEd Maste 			break;
6832c23cb7cSEd Maste 		case EM_X86_64:
6842c23cb7cSEd Maste 			switch (stype) {
685b6b6f9ccSEd Maste 			case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
6862c23cb7cSEd Maste 			default:
6872c23cb7cSEd Maste 				break;
6882c23cb7cSEd Maste 			}
6892c23cb7cSEd Maste 			break;
6902c23cb7cSEd Maste 		case EM_MIPS:
6912c23cb7cSEd Maste 		case EM_MIPS_RS3_LE:
6922c23cb7cSEd Maste 			switch (stype) {
6932c23cb7cSEd Maste 			case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
6942c23cb7cSEd Maste 			case SHT_MIPS_MSYM: return "MIPS_MSYM";
6952c23cb7cSEd Maste 			case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
6962c23cb7cSEd Maste 			case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
6972c23cb7cSEd Maste 			case SHT_MIPS_UCODE: return "MIPS_UCODE";
6982c23cb7cSEd Maste 			case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
6992c23cb7cSEd Maste 			case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
7002c23cb7cSEd Maste 			case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
7012c23cb7cSEd Maste 			case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
7022c23cb7cSEd Maste 			case SHT_MIPS_RELD: return "MIPS_RELD";
7032c23cb7cSEd Maste 			case SHT_MIPS_IFACE: return "MIPS_IFACE";
7042c23cb7cSEd Maste 			case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
7052c23cb7cSEd Maste 			case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
7062c23cb7cSEd Maste 			case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
7072c23cb7cSEd Maste 			case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
7082c23cb7cSEd Maste 			case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
7092c23cb7cSEd Maste 			case SHT_MIPS_DWARF: return "MIPS_DWARF";
7102c23cb7cSEd Maste 			case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
7112c23cb7cSEd Maste 			case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
7122c23cb7cSEd Maste 			case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
7132c23cb7cSEd Maste 			case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
7142c23cb7cSEd Maste 			case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
7152c23cb7cSEd Maste 			case SHT_MIPS_XLATE: return "MIPS_XLATE";
7162c23cb7cSEd Maste 			case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
7172c23cb7cSEd Maste 			case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
7182c23cb7cSEd Maste 			case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
7192c23cb7cSEd Maste 			case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
7202c23cb7cSEd Maste 			case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
7212c23cb7cSEd Maste 			default:
7222c23cb7cSEd Maste 				break;
7232c23cb7cSEd Maste 			}
7242c23cb7cSEd Maste 			break;
7252c23cb7cSEd Maste 		default:
7262c23cb7cSEd Maste 			break;
7272c23cb7cSEd Maste 		}
7282c23cb7cSEd Maste 
7292c23cb7cSEd Maste 		snprintf(s_stype, sizeof(s_stype), "LOPROC+%#x",
7302c23cb7cSEd Maste 		    stype - SHT_LOPROC);
7312c23cb7cSEd Maste 		return (s_stype);
7322c23cb7cSEd Maste 	}
7332c23cb7cSEd Maste 
7342c23cb7cSEd Maste 	switch (stype) {
7352c23cb7cSEd Maste 	case SHT_NULL: return "NULL";
7362c23cb7cSEd Maste 	case SHT_PROGBITS: return "PROGBITS";
7372c23cb7cSEd Maste 	case SHT_SYMTAB: return "SYMTAB";
7382c23cb7cSEd Maste 	case SHT_STRTAB: return "STRTAB";
7392c23cb7cSEd Maste 	case SHT_RELA: return "RELA";
7402c23cb7cSEd Maste 	case SHT_HASH: return "HASH";
7412c23cb7cSEd Maste 	case SHT_DYNAMIC: return "DYNAMIC";
7422c23cb7cSEd Maste 	case SHT_NOTE: return "NOTE";
7432c23cb7cSEd Maste 	case SHT_NOBITS: return "NOBITS";
7442c23cb7cSEd Maste 	case SHT_REL: return "REL";
7452c23cb7cSEd Maste 	case SHT_SHLIB: return "SHLIB";
7462c23cb7cSEd Maste 	case SHT_DYNSYM: return "DYNSYM";
7472c23cb7cSEd Maste 	case SHT_INIT_ARRAY: return "INIT_ARRAY";
7482c23cb7cSEd Maste 	case SHT_FINI_ARRAY: return "FINI_ARRAY";
7492c23cb7cSEd Maste 	case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
7502c23cb7cSEd Maste 	case SHT_GROUP: return "GROUP";
7512c23cb7cSEd Maste 	case SHT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
7522c23cb7cSEd Maste 	case SHT_SUNW_dof: return "SUNW_dof";
7532c23cb7cSEd Maste 	case SHT_SUNW_cap: return "SUNW_cap";
7542c23cb7cSEd Maste 	case SHT_GNU_HASH: return "GNU_HASH";
7552c23cb7cSEd Maste 	case SHT_SUNW_ANNOTATE: return "SUNW_ANNOTATE";
7562c23cb7cSEd Maste 	case SHT_SUNW_DEBUGSTR: return "SUNW_DEBUGSTR";
7572c23cb7cSEd Maste 	case SHT_SUNW_DEBUG: return "SUNW_DEBUG";
7582c23cb7cSEd Maste 	case SHT_SUNW_move: return "SUNW_move";
7592c23cb7cSEd Maste 	case SHT_SUNW_COMDAT: return "SUNW_COMDAT";
7602c23cb7cSEd Maste 	case SHT_SUNW_syminfo: return "SUNW_syminfo";
7612c23cb7cSEd Maste 	case SHT_SUNW_verdef: return "SUNW_verdef";
7622c23cb7cSEd Maste 	case SHT_SUNW_verneed: return "SUNW_verneed";
7632c23cb7cSEd Maste 	case SHT_SUNW_versym: return "SUNW_versym";
7642c23cb7cSEd Maste 	default:
7652c23cb7cSEd Maste 		if (stype >= SHT_LOOS && stype <= SHT_HIOS)
7662c23cb7cSEd Maste 			snprintf(s_stype, sizeof(s_stype), "LOOS+%#x",
7672c23cb7cSEd Maste 			    stype - SHT_LOOS);
7682c23cb7cSEd Maste 		else if (stype >= SHT_LOUSER)
7692c23cb7cSEd Maste 			snprintf(s_stype, sizeof(s_stype), "LOUSER+%#x",
7702c23cb7cSEd Maste 			    stype - SHT_LOUSER);
7712c23cb7cSEd Maste 		else
7722c23cb7cSEd Maste 			snprintf(s_stype, sizeof(s_stype), "<unknown: %#x>",
7732c23cb7cSEd Maste 			    stype);
7742c23cb7cSEd Maste 		return (s_stype);
7752c23cb7cSEd Maste 	}
7762c23cb7cSEd Maste }
7772c23cb7cSEd Maste 
7782c23cb7cSEd Maste static const char *
7792c23cb7cSEd Maste dt_type(unsigned int mach, unsigned int dtype)
7802c23cb7cSEd Maste {
7812c23cb7cSEd Maste 	static char s_dtype[32];
7822c23cb7cSEd Maste 
7832c23cb7cSEd Maste 	if (dtype >= DT_LOPROC && dtype <= DT_HIPROC) {
7842c23cb7cSEd Maste 		switch (mach) {
7852c23cb7cSEd Maste 		case EM_ARM:
7862c23cb7cSEd Maste 			switch (dtype) {
7872c23cb7cSEd Maste 			case DT_ARM_SYMTABSZ:
7882c23cb7cSEd Maste 				return "ARM_SYMTABSZ";
7892c23cb7cSEd Maste 			default:
7902c23cb7cSEd Maste 				break;
7912c23cb7cSEd Maste 			}
7922c23cb7cSEd Maste 			break;
7932c23cb7cSEd Maste 		case EM_MIPS:
7942c23cb7cSEd Maste 		case EM_MIPS_RS3_LE:
7952c23cb7cSEd Maste 			switch (dtype) {
7962c23cb7cSEd Maste 			case DT_MIPS_RLD_VERSION:
7972c23cb7cSEd Maste 				return "MIPS_RLD_VERSION";
7982c23cb7cSEd Maste 			case DT_MIPS_TIME_STAMP:
7992c23cb7cSEd Maste 				return "MIPS_TIME_STAMP";
8002c23cb7cSEd Maste 			case DT_MIPS_ICHECKSUM:
8012c23cb7cSEd Maste 				return "MIPS_ICHECKSUM";
8022c23cb7cSEd Maste 			case DT_MIPS_IVERSION:
8032c23cb7cSEd Maste 				return "MIPS_IVERSION";
8042c23cb7cSEd Maste 			case DT_MIPS_FLAGS:
8052c23cb7cSEd Maste 				return "MIPS_FLAGS";
8062c23cb7cSEd Maste 			case DT_MIPS_BASE_ADDRESS:
8072c23cb7cSEd Maste 				return "MIPS_BASE_ADDRESS";
8082c23cb7cSEd Maste 			case DT_MIPS_CONFLICT:
8092c23cb7cSEd Maste 				return "MIPS_CONFLICT";
8102c23cb7cSEd Maste 			case DT_MIPS_LIBLIST:
8112c23cb7cSEd Maste 				return "MIPS_LIBLIST";
8122c23cb7cSEd Maste 			case DT_MIPS_LOCAL_GOTNO:
8132c23cb7cSEd Maste 				return "MIPS_LOCAL_GOTNO";
8142c23cb7cSEd Maste 			case DT_MIPS_CONFLICTNO:
8152c23cb7cSEd Maste 				return "MIPS_CONFLICTNO";
8162c23cb7cSEd Maste 			case DT_MIPS_LIBLISTNO:
8172c23cb7cSEd Maste 				return "MIPS_LIBLISTNO";
8182c23cb7cSEd Maste 			case DT_MIPS_SYMTABNO:
8192c23cb7cSEd Maste 				return "MIPS_SYMTABNO";
8202c23cb7cSEd Maste 			case DT_MIPS_UNREFEXTNO:
8212c23cb7cSEd Maste 				return "MIPS_UNREFEXTNO";
8222c23cb7cSEd Maste 			case DT_MIPS_GOTSYM:
8232c23cb7cSEd Maste 				return "MIPS_GOTSYM";
8242c23cb7cSEd Maste 			case DT_MIPS_HIPAGENO:
8252c23cb7cSEd Maste 				return "MIPS_HIPAGENO";
8262c23cb7cSEd Maste 			case DT_MIPS_RLD_MAP:
8272c23cb7cSEd Maste 				return "MIPS_RLD_MAP";
8282c23cb7cSEd Maste 			case DT_MIPS_DELTA_CLASS:
8292c23cb7cSEd Maste 				return "MIPS_DELTA_CLASS";
8302c23cb7cSEd Maste 			case DT_MIPS_DELTA_CLASS_NO:
8312c23cb7cSEd Maste 				return "MIPS_DELTA_CLASS_NO";
8322c23cb7cSEd Maste 			case DT_MIPS_DELTA_INSTANCE:
8332c23cb7cSEd Maste 				return "MIPS_DELTA_INSTANCE";
8342c23cb7cSEd Maste 			case DT_MIPS_DELTA_INSTANCE_NO:
8352c23cb7cSEd Maste 				return "MIPS_DELTA_INSTANCE_NO";
8362c23cb7cSEd Maste 			case DT_MIPS_DELTA_RELOC:
8372c23cb7cSEd Maste 				return "MIPS_DELTA_RELOC";
8382c23cb7cSEd Maste 			case DT_MIPS_DELTA_RELOC_NO:
8392c23cb7cSEd Maste 				return "MIPS_DELTA_RELOC_NO";
8402c23cb7cSEd Maste 			case DT_MIPS_DELTA_SYM:
8412c23cb7cSEd Maste 				return "MIPS_DELTA_SYM";
8422c23cb7cSEd Maste 			case DT_MIPS_DELTA_SYM_NO:
8432c23cb7cSEd Maste 				return "MIPS_DELTA_SYM_NO";
8442c23cb7cSEd Maste 			case DT_MIPS_DELTA_CLASSSYM:
8452c23cb7cSEd Maste 				return "MIPS_DELTA_CLASSSYM";
8462c23cb7cSEd Maste 			case DT_MIPS_DELTA_CLASSSYM_NO:
8472c23cb7cSEd Maste 				return "MIPS_DELTA_CLASSSYM_NO";
8482c23cb7cSEd Maste 			case DT_MIPS_CXX_FLAGS:
8492c23cb7cSEd Maste 				return "MIPS_CXX_FLAGS";
8502c23cb7cSEd Maste 			case DT_MIPS_PIXIE_INIT:
8512c23cb7cSEd Maste 				return "MIPS_PIXIE_INIT";
8522c23cb7cSEd Maste 			case DT_MIPS_SYMBOL_LIB:
8532c23cb7cSEd Maste 				return "MIPS_SYMBOL_LIB";
8542c23cb7cSEd Maste 			case DT_MIPS_LOCALPAGE_GOTIDX:
8552c23cb7cSEd Maste 				return "MIPS_LOCALPAGE_GOTIDX";
8562c23cb7cSEd Maste 			case DT_MIPS_LOCAL_GOTIDX:
8572c23cb7cSEd Maste 				return "MIPS_LOCAL_GOTIDX";
8582c23cb7cSEd Maste 			case DT_MIPS_HIDDEN_GOTIDX:
8592c23cb7cSEd Maste 				return "MIPS_HIDDEN_GOTIDX";
8602c23cb7cSEd Maste 			case DT_MIPS_PROTECTED_GOTIDX:
8612c23cb7cSEd Maste 				return "MIPS_PROTECTED_GOTIDX";
8622c23cb7cSEd Maste 			case DT_MIPS_OPTIONS:
8632c23cb7cSEd Maste 				return "MIPS_OPTIONS";
8642c23cb7cSEd Maste 			case DT_MIPS_INTERFACE:
8652c23cb7cSEd Maste 				return "MIPS_INTERFACE";
8662c23cb7cSEd Maste 			case DT_MIPS_DYNSTR_ALIGN:
8672c23cb7cSEd Maste 				return "MIPS_DYNSTR_ALIGN";
8682c23cb7cSEd Maste 			case DT_MIPS_INTERFACE_SIZE:
8692c23cb7cSEd Maste 				return "MIPS_INTERFACE_SIZE";
8702c23cb7cSEd Maste 			case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
8712c23cb7cSEd Maste 				return "MIPS_RLD_TEXT_RESOLVE_ADDR";
8722c23cb7cSEd Maste 			case DT_MIPS_PERF_SUFFIX:
8732c23cb7cSEd Maste 				return "MIPS_PERF_SUFFIX";
8742c23cb7cSEd Maste 			case DT_MIPS_COMPACT_SIZE:
8752c23cb7cSEd Maste 				return "MIPS_COMPACT_SIZE";
8762c23cb7cSEd Maste 			case DT_MIPS_GP_VALUE:
8772c23cb7cSEd Maste 				return "MIPS_GP_VALUE";
8782c23cb7cSEd Maste 			case DT_MIPS_AUX_DYNAMIC:
8792c23cb7cSEd Maste 				return "MIPS_AUX_DYNAMIC";
8802c23cb7cSEd Maste 			case DT_MIPS_PLTGOT:
8812c23cb7cSEd Maste 				return "MIPS_PLTGOT";
8822c23cb7cSEd Maste 			case DT_MIPS_RLD_OBJ_UPDATE:
8832c23cb7cSEd Maste 				return "MIPS_RLD_OBJ_UPDATE";
8842c23cb7cSEd Maste 			case DT_MIPS_RWPLT:
8852c23cb7cSEd Maste 				return "MIPS_RWPLT";
8862c23cb7cSEd Maste 			default:
8872c23cb7cSEd Maste 				break;
8882c23cb7cSEd Maste 			}
8892c23cb7cSEd Maste 			break;
8902c23cb7cSEd Maste 		case EM_SPARC:
8912c23cb7cSEd Maste 		case EM_SPARC32PLUS:
8922c23cb7cSEd Maste 		case EM_SPARCV9:
8932c23cb7cSEd Maste 			switch (dtype) {
8942c23cb7cSEd Maste 			case DT_SPARC_REGISTER:
8952c23cb7cSEd Maste 				return "DT_SPARC_REGISTER";
8962c23cb7cSEd Maste 			default:
8972c23cb7cSEd Maste 				break;
8982c23cb7cSEd Maste 			}
8992c23cb7cSEd Maste 			break;
9002c23cb7cSEd Maste 		default:
9012c23cb7cSEd Maste 			break;
9022c23cb7cSEd Maste 		}
9032c23cb7cSEd Maste 		snprintf(s_dtype, sizeof(s_dtype), "<unknown: %#x>", dtype);
9042c23cb7cSEd Maste 		return (s_dtype);
9052c23cb7cSEd Maste 	}
9062c23cb7cSEd Maste 
9072c23cb7cSEd Maste 	switch (dtype) {
9082c23cb7cSEd Maste 	case DT_NULL: return "NULL";
9092c23cb7cSEd Maste 	case DT_NEEDED: return "NEEDED";
9102c23cb7cSEd Maste 	case DT_PLTRELSZ: return "PLTRELSZ";
9112c23cb7cSEd Maste 	case DT_PLTGOT: return "PLTGOT";
9122c23cb7cSEd Maste 	case DT_HASH: return "HASH";
9132c23cb7cSEd Maste 	case DT_STRTAB: return "STRTAB";
9142c23cb7cSEd Maste 	case DT_SYMTAB: return "SYMTAB";
9152c23cb7cSEd Maste 	case DT_RELA: return "RELA";
9162c23cb7cSEd Maste 	case DT_RELASZ: return "RELASZ";
9172c23cb7cSEd Maste 	case DT_RELAENT: return "RELAENT";
9182c23cb7cSEd Maste 	case DT_STRSZ: return "STRSZ";
9192c23cb7cSEd Maste 	case DT_SYMENT: return "SYMENT";
9202c23cb7cSEd Maste 	case DT_INIT: return "INIT";
9212c23cb7cSEd Maste 	case DT_FINI: return "FINI";
9222c23cb7cSEd Maste 	case DT_SONAME: return "SONAME";
9232c23cb7cSEd Maste 	case DT_RPATH: return "RPATH";
9242c23cb7cSEd Maste 	case DT_SYMBOLIC: return "SYMBOLIC";
9252c23cb7cSEd Maste 	case DT_REL: return "REL";
9262c23cb7cSEd Maste 	case DT_RELSZ: return "RELSZ";
9272c23cb7cSEd Maste 	case DT_RELENT: return "RELENT";
9282c23cb7cSEd Maste 	case DT_PLTREL: return "PLTREL";
9292c23cb7cSEd Maste 	case DT_DEBUG: return "DEBUG";
9302c23cb7cSEd Maste 	case DT_TEXTREL: return "TEXTREL";
9312c23cb7cSEd Maste 	case DT_JMPREL: return "JMPREL";
9322c23cb7cSEd Maste 	case DT_BIND_NOW: return "BIND_NOW";
9332c23cb7cSEd Maste 	case DT_INIT_ARRAY: return "INIT_ARRAY";
9342c23cb7cSEd Maste 	case DT_FINI_ARRAY: return "FINI_ARRAY";
9352c23cb7cSEd Maste 	case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
9362c23cb7cSEd Maste 	case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
9372c23cb7cSEd Maste 	case DT_RUNPATH: return "RUNPATH";
9382c23cb7cSEd Maste 	case DT_FLAGS: return "FLAGS";
9392c23cb7cSEd Maste 	case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
9402c23cb7cSEd Maste 	case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
9412c23cb7cSEd Maste 	case DT_MAXPOSTAGS: return "MAXPOSTAGS";
9422c23cb7cSEd Maste 	case DT_SUNW_AUXILIARY: return "SUNW_AUXILIARY";
9432c23cb7cSEd Maste 	case DT_SUNW_RTLDINF: return "SUNW_RTLDINF";
9442c23cb7cSEd Maste 	case DT_SUNW_FILTER: return "SUNW_FILTER";
9452c23cb7cSEd Maste 	case DT_SUNW_CAP: return "SUNW_CAP";
9462c23cb7cSEd Maste 	case DT_CHECKSUM: return "CHECKSUM";
9472c23cb7cSEd Maste 	case DT_PLTPADSZ: return "PLTPADSZ";
9482c23cb7cSEd Maste 	case DT_MOVEENT: return "MOVEENT";
9492c23cb7cSEd Maste 	case DT_MOVESZ: return "MOVESZ";
9502013b96eSEd Maste 	case DT_FEATURE: return "FEATURE";
9512c23cb7cSEd Maste 	case DT_POSFLAG_1: return "POSFLAG_1";
9522c23cb7cSEd Maste 	case DT_SYMINSZ: return "SYMINSZ";
9532c23cb7cSEd Maste 	case DT_SYMINENT: return "SYMINENT";
9542c23cb7cSEd Maste 	case DT_GNU_HASH: return "GNU_HASH";
955b6b6f9ccSEd Maste 	case DT_TLSDESC_PLT: return "DT_TLSDESC_PLT";
956b6b6f9ccSEd Maste 	case DT_TLSDESC_GOT: return "DT_TLSDESC_GOT";
9572c23cb7cSEd Maste 	case DT_GNU_CONFLICT: return "GNU_CONFLICT";
9582c23cb7cSEd Maste 	case DT_GNU_LIBLIST: return "GNU_LIBLIST";
9592c23cb7cSEd Maste 	case DT_CONFIG: return "CONFIG";
9602c23cb7cSEd Maste 	case DT_DEPAUDIT: return "DEPAUDIT";
9612c23cb7cSEd Maste 	case DT_AUDIT: return "AUDIT";
9622c23cb7cSEd Maste 	case DT_PLTPAD: return "PLTPAD";
9632c23cb7cSEd Maste 	case DT_MOVETAB: return "MOVETAB";
9642c23cb7cSEd Maste 	case DT_SYMINFO: return "SYMINFO";
9652c23cb7cSEd Maste 	case DT_VERSYM: return "VERSYM";
9662c23cb7cSEd Maste 	case DT_RELACOUNT: return "RELACOUNT";
9672c23cb7cSEd Maste 	case DT_RELCOUNT: return "RELCOUNT";
9682c23cb7cSEd Maste 	case DT_FLAGS_1: return "FLAGS_1";
9692c23cb7cSEd Maste 	case DT_VERDEF: return "VERDEF";
9702c23cb7cSEd Maste 	case DT_VERDEFNUM: return "VERDEFNUM";
9712c23cb7cSEd Maste 	case DT_VERNEED: return "VERNEED";
9722c23cb7cSEd Maste 	case DT_VERNEEDNUM: return "VERNEEDNUM";
9732c23cb7cSEd Maste 	case DT_AUXILIARY: return "AUXILIARY";
9742c23cb7cSEd Maste 	case DT_USED: return "USED";
9752c23cb7cSEd Maste 	case DT_FILTER: return "FILTER";
9762c23cb7cSEd Maste 	case DT_GNU_PRELINKED: return "GNU_PRELINKED";
9772c23cb7cSEd Maste 	case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
9782c23cb7cSEd Maste 	case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
9792c23cb7cSEd Maste 	default:
9802c23cb7cSEd Maste 		snprintf(s_dtype, sizeof(s_dtype), "<unknown: %#x>", dtype);
9812c23cb7cSEd Maste 		return (s_dtype);
9822c23cb7cSEd Maste 	}
9832c23cb7cSEd Maste }
9842c23cb7cSEd Maste 
9852c23cb7cSEd Maste static const char *
9862c23cb7cSEd Maste st_bind(unsigned int sbind)
9872c23cb7cSEd Maste {
9882c23cb7cSEd Maste 	static char s_sbind[32];
9892c23cb7cSEd Maste 
9902c23cb7cSEd Maste 	switch (sbind) {
9912c23cb7cSEd Maste 	case STB_LOCAL: return "LOCAL";
9922c23cb7cSEd Maste 	case STB_GLOBAL: return "GLOBAL";
9932c23cb7cSEd Maste 	case STB_WEAK: return "WEAK";
994839529caSEd Maste 	case STB_GNU_UNIQUE: return "UNIQUE";
9952c23cb7cSEd Maste 	default:
9962c23cb7cSEd Maste 		if (sbind >= STB_LOOS && sbind <= STB_HIOS)
9972c23cb7cSEd Maste 			return "OS";
9982c23cb7cSEd Maste 		else if (sbind >= STB_LOPROC && sbind <= STB_HIPROC)
9992c23cb7cSEd Maste 			return "PROC";
10002c23cb7cSEd Maste 		else
10012c23cb7cSEd Maste 			snprintf(s_sbind, sizeof(s_sbind), "<unknown: %#x>",
10022c23cb7cSEd Maste 			    sbind);
10032c23cb7cSEd Maste 		return (s_sbind);
10042c23cb7cSEd Maste 	}
10052c23cb7cSEd Maste }
10062c23cb7cSEd Maste 
10072c23cb7cSEd Maste static const char *
1008b6b6f9ccSEd Maste st_type(unsigned int mach, unsigned int os, unsigned int stype)
10092c23cb7cSEd Maste {
10102c23cb7cSEd Maste 	static char s_stype[32];
10112c23cb7cSEd Maste 
10122c23cb7cSEd Maste 	switch (stype) {
10132c23cb7cSEd Maste 	case STT_NOTYPE: return "NOTYPE";
10142c23cb7cSEd Maste 	case STT_OBJECT: return "OBJECT";
10152c23cb7cSEd Maste 	case STT_FUNC: return "FUNC";
10162c23cb7cSEd Maste 	case STT_SECTION: return "SECTION";
10172c23cb7cSEd Maste 	case STT_FILE: return "FILE";
10182c23cb7cSEd Maste 	case STT_COMMON: return "COMMON";
10192c23cb7cSEd Maste 	case STT_TLS: return "TLS";
10202c23cb7cSEd Maste 	default:
1021b6b6f9ccSEd Maste 		if (stype >= STT_LOOS && stype <= STT_HIOS) {
1022b6b6f9ccSEd Maste 			if ((os == ELFOSABI_GNU || os == ELFOSABI_FREEBSD) &&
1023b6b6f9ccSEd Maste 			    stype == STT_GNU_IFUNC)
1024b6b6f9ccSEd Maste 				return "IFUNC";
10252c23cb7cSEd Maste 			snprintf(s_stype, sizeof(s_stype), "OS+%#x",
10262c23cb7cSEd Maste 			    stype - STT_LOOS);
1027b6b6f9ccSEd Maste 		} else if (stype >= STT_LOPROC && stype <= STT_HIPROC) {
1028839529caSEd Maste 			if (mach == EM_SPARCV9 && stype == STT_SPARC_REGISTER)
1029839529caSEd Maste 				return "REGISTER";
10302c23cb7cSEd Maste 			snprintf(s_stype, sizeof(s_stype), "PROC+%#x",
10312c23cb7cSEd Maste 			    stype - STT_LOPROC);
1032839529caSEd Maste 		} else
10332c23cb7cSEd Maste 			snprintf(s_stype, sizeof(s_stype), "<unknown: %#x>",
10342c23cb7cSEd Maste 			    stype);
10352c23cb7cSEd Maste 		return (s_stype);
10362c23cb7cSEd Maste 	}
10372c23cb7cSEd Maste }
10382c23cb7cSEd Maste 
10392c23cb7cSEd Maste static const char *
10402c23cb7cSEd Maste st_vis(unsigned int svis)
10412c23cb7cSEd Maste {
10422c23cb7cSEd Maste 	static char s_svis[32];
10432c23cb7cSEd Maste 
10442c23cb7cSEd Maste 	switch(svis) {
10452c23cb7cSEd Maste 	case STV_DEFAULT: return "DEFAULT";
10462c23cb7cSEd Maste 	case STV_INTERNAL: return "INTERNAL";
10472c23cb7cSEd Maste 	case STV_HIDDEN: return "HIDDEN";
10482c23cb7cSEd Maste 	case STV_PROTECTED: return "PROTECTED";
10492c23cb7cSEd Maste 	default:
10502c23cb7cSEd Maste 		snprintf(s_svis, sizeof(s_svis), "<unknown: %#x>", svis);
10512c23cb7cSEd Maste 		return (s_svis);
10522c23cb7cSEd Maste 	}
10532c23cb7cSEd Maste }
10542c23cb7cSEd Maste 
10552c23cb7cSEd Maste static const char *
10562c23cb7cSEd Maste st_shndx(unsigned int shndx)
10572c23cb7cSEd Maste {
10582c23cb7cSEd Maste 	static char s_shndx[32];
10592c23cb7cSEd Maste 
10602c23cb7cSEd Maste 	switch (shndx) {
10612c23cb7cSEd Maste 	case SHN_UNDEF: return "UND";
10622c23cb7cSEd Maste 	case SHN_ABS: return "ABS";
10632c23cb7cSEd Maste 	case SHN_COMMON: return "COM";
10642c23cb7cSEd Maste 	default:
10652c23cb7cSEd Maste 		if (shndx >= SHN_LOPROC && shndx <= SHN_HIPROC)
10662c23cb7cSEd Maste 			return "PRC";
10672c23cb7cSEd Maste 		else if (shndx >= SHN_LOOS && shndx <= SHN_HIOS)
10682c23cb7cSEd Maste 			return "OS";
10692c23cb7cSEd Maste 		else
10702c23cb7cSEd Maste 			snprintf(s_shndx, sizeof(s_shndx), "%u", shndx);
10712c23cb7cSEd Maste 		return (s_shndx);
10722c23cb7cSEd Maste 	}
10732c23cb7cSEd Maste }
10742c23cb7cSEd Maste 
10752c23cb7cSEd Maste static struct {
10762c23cb7cSEd Maste 	const char *ln;
10772c23cb7cSEd Maste 	char sn;
10782c23cb7cSEd Maste 	int value;
10792c23cb7cSEd Maste } section_flag[] = {
10802c23cb7cSEd Maste 	{"WRITE", 'W', SHF_WRITE},
10812c23cb7cSEd Maste 	{"ALLOC", 'A', SHF_ALLOC},
10822c23cb7cSEd Maste 	{"EXEC", 'X', SHF_EXECINSTR},
10832c23cb7cSEd Maste 	{"MERGE", 'M', SHF_MERGE},
10842c23cb7cSEd Maste 	{"STRINGS", 'S', SHF_STRINGS},
10852c23cb7cSEd Maste 	{"INFO LINK", 'I', SHF_INFO_LINK},
10862c23cb7cSEd Maste 	{"OS NONCONF", 'O', SHF_OS_NONCONFORMING},
10872c23cb7cSEd Maste 	{"GROUP", 'G', SHF_GROUP},
10882c23cb7cSEd Maste 	{"TLS", 'T', SHF_TLS},
1089b6b6f9ccSEd Maste 	{"COMPRESSED", 'C', SHF_COMPRESSED},
10902c23cb7cSEd Maste 	{NULL, 0, 0}
10912c23cb7cSEd Maste };
10922c23cb7cSEd Maste 
10932c23cb7cSEd Maste static const char *
109402b08c90SEd Maste note_type(const char *name, unsigned int et, unsigned int nt)
109502b08c90SEd Maste {
109671a0c925SEd Maste 	if ((strcmp(name, "CORE") == 0 || strcmp(name, "LINUX") == 0) &&
109771a0c925SEd Maste 	    et == ET_CORE)
109802b08c90SEd Maste 		return note_type_linux_core(nt);
109902b08c90SEd Maste 	else if (strcmp(name, "FreeBSD") == 0)
110002b08c90SEd Maste 		if (et == ET_CORE)
110102b08c90SEd Maste 			return note_type_freebsd_core(nt);
110202b08c90SEd Maste 		else
110302b08c90SEd Maste 			return note_type_freebsd(nt);
110402b08c90SEd Maste 	else if (strcmp(name, "GNU") == 0 && et != ET_CORE)
110502b08c90SEd Maste 		return note_type_gnu(nt);
110602b08c90SEd Maste 	else if (strcmp(name, "NetBSD") == 0 && et != ET_CORE)
110702b08c90SEd Maste 		return note_type_netbsd(nt);
110802b08c90SEd Maste 	else if (strcmp(name, "OpenBSD") == 0 && et != ET_CORE)
110902b08c90SEd Maste 		return note_type_openbsd(nt);
1110895f86f1SEd Maste 	else if (strcmp(name, "Xen") == 0 && et != ET_CORE)
1111895f86f1SEd Maste 		return note_type_xen(nt);
111202b08c90SEd Maste 	return note_type_unknown(nt);
111302b08c90SEd Maste }
111402b08c90SEd Maste 
111502b08c90SEd Maste static const char *
111602b08c90SEd Maste note_type_freebsd(unsigned int nt)
111702b08c90SEd Maste {
111802b08c90SEd Maste 	switch (nt) {
111902b08c90SEd Maste 	case 1: return "NT_FREEBSD_ABI_TAG";
112002b08c90SEd Maste 	case 2: return "NT_FREEBSD_NOINIT_TAG";
112102b08c90SEd Maste 	case 3: return "NT_FREEBSD_ARCH_TAG";
112202b08c90SEd Maste 	default: return (note_type_unknown(nt));
112302b08c90SEd Maste 	}
112402b08c90SEd Maste }
112502b08c90SEd Maste 
112602b08c90SEd Maste static const char *
112702b08c90SEd Maste note_type_freebsd_core(unsigned int nt)
112802b08c90SEd Maste {
112902b08c90SEd Maste 	switch (nt) {
113002b08c90SEd Maste 	case 1: return "NT_PRSTATUS";
113102b08c90SEd Maste 	case 2: return "NT_FPREGSET";
113202b08c90SEd Maste 	case 3: return "NT_PRPSINFO";
113302b08c90SEd Maste 	case 7: return "NT_THRMISC";
113402b08c90SEd Maste 	case 8: return "NT_PROCSTAT_PROC";
113502b08c90SEd Maste 	case 9: return "NT_PROCSTAT_FILES";
113602b08c90SEd Maste 	case 10: return "NT_PROCSTAT_VMMAP";
113702b08c90SEd Maste 	case 11: return "NT_PROCSTAT_GROUPS";
113802b08c90SEd Maste 	case 12: return "NT_PROCSTAT_UMASK";
113902b08c90SEd Maste 	case 13: return "NT_PROCSTAT_RLIMIT";
114002b08c90SEd Maste 	case 14: return "NT_PROCSTAT_OSREL";
114102b08c90SEd Maste 	case 15: return "NT_PROCSTAT_PSSTRINGS";
114202b08c90SEd Maste 	case 16: return "NT_PROCSTAT_AUXV";
114302b08c90SEd Maste 	case 0x202: return "NT_X86_XSTATE (x86 XSAVE extended state)";
114402b08c90SEd Maste 	default: return (note_type_unknown(nt));
114502b08c90SEd Maste 	}
114602b08c90SEd Maste }
114702b08c90SEd Maste 
114802b08c90SEd Maste static const char *
114902b08c90SEd Maste note_type_linux_core(unsigned int nt)
115002b08c90SEd Maste {
115102b08c90SEd Maste 	switch (nt) {
115202b08c90SEd Maste 	case 1: return "NT_PRSTATUS (Process status)";
115302b08c90SEd Maste 	case 2: return "NT_FPREGSET (Floating point information)";
115402b08c90SEd Maste 	case 3: return "NT_PRPSINFO (Process information)";
115571a0c925SEd Maste 	case 4: return "NT_TASKSTRUCT (Task structure)";
115602b08c90SEd Maste 	case 6: return "NT_AUXV (Auxiliary vector)";
115702b08c90SEd Maste 	case 10: return "NT_PSTATUS (Linux process status)";
115802b08c90SEd Maste 	case 12: return "NT_FPREGS (Linux floating point regset)";
115902b08c90SEd Maste 	case 13: return "NT_PSINFO (Linux process information)";
116002b08c90SEd Maste 	case 16: return "NT_LWPSTATUS (Linux lwpstatus_t type)";
116102b08c90SEd Maste 	case 17: return "NT_LWPSINFO (Linux lwpinfo_t type)";
116271a0c925SEd Maste 	case 18: return "NT_WIN32PSTATUS (win32_pstatus structure)";
116371a0c925SEd Maste 	case 0x100: return "NT_PPC_VMX (ppc Altivec registers)";
116471a0c925SEd Maste 	case 0x102: return "NT_PPC_VSX (ppc VSX registers)";
116571a0c925SEd Maste 	case 0x202: return "NT_X86_XSTATE (x86 XSAVE extended state)";
116671a0c925SEd Maste 	case 0x300: return "NT_S390_HIGH_GPRS (s390 upper register halves)";
116771a0c925SEd Maste 	case 0x301: return "NT_S390_TIMER (s390 timer register)";
116871a0c925SEd Maste 	case 0x302: return "NT_S390_TODCMP (s390 TOD comparator register)";
116971a0c925SEd Maste 	case 0x303: return "NT_S390_TODPREG (s390 TOD programmable register)";
117071a0c925SEd Maste 	case 0x304: return "NT_S390_CTRS (s390 control registers)";
117171a0c925SEd Maste 	case 0x305: return "NT_S390_PREFIX (s390 prefix register)";
117271a0c925SEd Maste 	case 0x400: return "NT_ARM_VFP (arm VFP registers)";
117371a0c925SEd Maste 	case 0x46494c45UL: return "NT_FILE (mapped files)";
117471a0c925SEd Maste 	case 0x46E62B7FUL: return "NT_PRXFPREG (Linux user_xfpregs structure)";
117571a0c925SEd Maste 	case 0x53494749UL: return "NT_SIGINFO (siginfo_t data)";
117602b08c90SEd Maste 	default: return (note_type_unknown(nt));
117702b08c90SEd Maste 	}
117802b08c90SEd Maste }
117902b08c90SEd Maste 
118002b08c90SEd Maste static const char *
118102b08c90SEd Maste note_type_gnu(unsigned int nt)
118202b08c90SEd Maste {
118302b08c90SEd Maste 	switch (nt) {
118402b08c90SEd Maste 	case 1: return "NT_GNU_ABI_TAG";
118502b08c90SEd Maste 	case 2: return "NT_GNU_HWCAP (Hardware capabilities)";
118602b08c90SEd Maste 	case 3: return "NT_GNU_BUILD_ID (Build id set by ld(1))";
118702b08c90SEd Maste 	case 4: return "NT_GNU_GOLD_VERSION (GNU gold version)";
118802b08c90SEd Maste 	default: return (note_type_unknown(nt));
118902b08c90SEd Maste 	}
119002b08c90SEd Maste }
119102b08c90SEd Maste 
119202b08c90SEd Maste static const char *
119302b08c90SEd Maste note_type_netbsd(unsigned int nt)
119402b08c90SEd Maste {
119502b08c90SEd Maste 	switch (nt) {
119602b08c90SEd Maste 	case 1: return "NT_NETBSD_IDENT";
119702b08c90SEd Maste 	default: return (note_type_unknown(nt));
119802b08c90SEd Maste 	}
119902b08c90SEd Maste }
120002b08c90SEd Maste 
120102b08c90SEd Maste static const char *
120202b08c90SEd Maste note_type_openbsd(unsigned int nt)
120302b08c90SEd Maste {
120402b08c90SEd Maste 	switch (nt) {
120502b08c90SEd Maste 	case 1: return "NT_OPENBSD_IDENT";
120602b08c90SEd Maste 	default: return (note_type_unknown(nt));
120702b08c90SEd Maste 	}
120802b08c90SEd Maste }
120902b08c90SEd Maste 
121002b08c90SEd Maste static const char *
121102b08c90SEd Maste note_type_unknown(unsigned int nt)
12122c23cb7cSEd Maste {
12132c23cb7cSEd Maste 	static char s_nt[32];
12142c23cb7cSEd Maste 
121571a0c925SEd Maste 	snprintf(s_nt, sizeof(s_nt),
121671a0c925SEd Maste 	    nt >= 0x100 ? "<unknown: 0x%x>" : "<unknown: %u>", nt);
12172c23cb7cSEd Maste 	return (s_nt);
12182c23cb7cSEd Maste }
12192c23cb7cSEd Maste 
1220895f86f1SEd Maste static const char *
1221895f86f1SEd Maste note_type_xen(unsigned int nt)
1222895f86f1SEd Maste {
1223895f86f1SEd Maste 	switch (nt) {
1224895f86f1SEd Maste 	case 0: return "XEN_ELFNOTE_INFO";
1225895f86f1SEd Maste 	case 1: return "XEN_ELFNOTE_ENTRY";
1226895f86f1SEd Maste 	case 2: return "XEN_ELFNOTE_HYPERCALL_PAGE";
1227895f86f1SEd Maste 	case 3: return "XEN_ELFNOTE_VIRT_BASE";
1228895f86f1SEd Maste 	case 4: return "XEN_ELFNOTE_PADDR_OFFSET";
1229895f86f1SEd Maste 	case 5: return "XEN_ELFNOTE_XEN_VERSION";
1230895f86f1SEd Maste 	case 6: return "XEN_ELFNOTE_GUEST_OS";
1231895f86f1SEd Maste 	case 7: return "XEN_ELFNOTE_GUEST_VERSION";
1232895f86f1SEd Maste 	case 8: return "XEN_ELFNOTE_LOADER";
1233895f86f1SEd Maste 	case 9: return "XEN_ELFNOTE_PAE_MODE";
1234895f86f1SEd Maste 	case 10: return "XEN_ELFNOTE_FEATURES";
1235895f86f1SEd Maste 	case 11: return "XEN_ELFNOTE_BSD_SYMTAB";
1236895f86f1SEd Maste 	case 12: return "XEN_ELFNOTE_HV_START_LOW";
1237895f86f1SEd Maste 	case 13: return "XEN_ELFNOTE_L1_MFN_VALID";
1238895f86f1SEd Maste 	case 14: return "XEN_ELFNOTE_SUSPEND_CANCEL";
1239895f86f1SEd Maste 	case 15: return "XEN_ELFNOTE_INIT_P2M";
1240895f86f1SEd Maste 	case 16: return "XEN_ELFNOTE_MOD_START_PFN";
1241895f86f1SEd Maste 	case 17: return "XEN_ELFNOTE_SUPPORTED_FEATURES";
1242895f86f1SEd Maste 	default: return (note_type_unknown(nt));
1243895f86f1SEd Maste 	}
1244895f86f1SEd Maste }
1245895f86f1SEd Maste 
12462c23cb7cSEd Maste static struct {
12472c23cb7cSEd Maste 	const char *name;
12482c23cb7cSEd Maste 	int value;
12492c23cb7cSEd Maste } l_flag[] = {
12502c23cb7cSEd Maste 	{"EXACT_MATCH", LL_EXACT_MATCH},
12512c23cb7cSEd Maste 	{"IGNORE_INT_VER", LL_IGNORE_INT_VER},
12522c23cb7cSEd Maste 	{"REQUIRE_MINOR", LL_REQUIRE_MINOR},
12532c23cb7cSEd Maste 	{"EXPORTS", LL_EXPORTS},
12542c23cb7cSEd Maste 	{"DELAY_LOAD", LL_DELAY_LOAD},
12552c23cb7cSEd Maste 	{"DELTA", LL_DELTA},
12562c23cb7cSEd Maste 	{NULL, 0}
12572c23cb7cSEd Maste };
12582c23cb7cSEd Maste 
12592c23cb7cSEd Maste static struct mips_option mips_exceptions_option[] = {
12602c23cb7cSEd Maste 	{OEX_PAGE0, "PAGE0"},
12612c23cb7cSEd Maste 	{OEX_SMM, "SMM"},
12622c23cb7cSEd Maste 	{OEX_PRECISEFP, "PRECISEFP"},
12632c23cb7cSEd Maste 	{OEX_DISMISS, "DISMISS"},
12642c23cb7cSEd Maste 	{0, NULL}
12652c23cb7cSEd Maste };
12662c23cb7cSEd Maste 
12672c23cb7cSEd Maste static struct mips_option mips_pad_option[] = {
12682c23cb7cSEd Maste 	{OPAD_PREFIX, "PREFIX"},
12692c23cb7cSEd Maste 	{OPAD_POSTFIX, "POSTFIX"},
12702c23cb7cSEd Maste 	{OPAD_SYMBOL, "SYMBOL"},
12712c23cb7cSEd Maste 	{0, NULL}
12722c23cb7cSEd Maste };
12732c23cb7cSEd Maste 
12742c23cb7cSEd Maste static struct mips_option mips_hwpatch_option[] = {
12752c23cb7cSEd Maste 	{OHW_R4KEOP, "R4KEOP"},
12762c23cb7cSEd Maste 	{OHW_R8KPFETCH, "R8KPFETCH"},
12772c23cb7cSEd Maste 	{OHW_R5KEOP, "R5KEOP"},
12782c23cb7cSEd Maste 	{OHW_R5KCVTL, "R5KCVTL"},
12792c23cb7cSEd Maste 	{0, NULL}
12802c23cb7cSEd Maste };
12812c23cb7cSEd Maste 
12822c23cb7cSEd Maste static struct mips_option mips_hwa_option[] = {
12832c23cb7cSEd Maste 	{OHWA0_R4KEOP_CHECKED, "R4KEOP_CHECKED"},
12842c23cb7cSEd Maste 	{OHWA0_R4KEOP_CLEAN, "R4KEOP_CLEAN"},
12852c23cb7cSEd Maste 	{0, NULL}
12862c23cb7cSEd Maste };
12872c23cb7cSEd Maste 
12882c23cb7cSEd Maste static struct mips_option mips_hwo_option[] = {
12892c23cb7cSEd Maste 	{OHWO0_FIXADE, "FIXADE"},
12902c23cb7cSEd Maste 	{0, NULL}
12912c23cb7cSEd Maste };
12922c23cb7cSEd Maste 
12932c23cb7cSEd Maste static const char *
12942c23cb7cSEd Maste option_kind(uint8_t kind)
12952c23cb7cSEd Maste {
12962c23cb7cSEd Maste 	static char s_kind[32];
12972c23cb7cSEd Maste 
12982c23cb7cSEd Maste 	switch (kind) {
12992c23cb7cSEd Maste 	case ODK_NULL: return "NULL";
13002c23cb7cSEd Maste 	case ODK_REGINFO: return "REGINFO";
13012c23cb7cSEd Maste 	case ODK_EXCEPTIONS: return "EXCEPTIONS";
13022c23cb7cSEd Maste 	case ODK_PAD: return "PAD";
13032c23cb7cSEd Maste 	case ODK_HWPATCH: return "HWPATCH";
13042c23cb7cSEd Maste 	case ODK_FILL: return "FILL";
13052c23cb7cSEd Maste 	case ODK_TAGS: return "TAGS";
13062c23cb7cSEd Maste 	case ODK_HWAND: return "HWAND";
13072c23cb7cSEd Maste 	case ODK_HWOR: return "HWOR";
13082c23cb7cSEd Maste 	case ODK_GP_GROUP: return "GP_GROUP";
13092c23cb7cSEd Maste 	case ODK_IDENT: return "IDENT";
13102c23cb7cSEd Maste 	default:
13112c23cb7cSEd Maste 		snprintf(s_kind, sizeof(s_kind), "<unknown: %u>", kind);
13122c23cb7cSEd Maste 		return (s_kind);
13132c23cb7cSEd Maste 	}
13142c23cb7cSEd Maste }
13152c23cb7cSEd Maste 
13162c23cb7cSEd Maste static const char *
13172c23cb7cSEd Maste top_tag(unsigned int tag)
13182c23cb7cSEd Maste {
13192c23cb7cSEd Maste 	static char s_top_tag[32];
13202c23cb7cSEd Maste 
13212c23cb7cSEd Maste 	switch (tag) {
13222c23cb7cSEd Maste 	case 1: return "File Attributes";
13232c23cb7cSEd Maste 	case 2: return "Section Attributes";
13242c23cb7cSEd Maste 	case 3: return "Symbol Attributes";
13252c23cb7cSEd Maste 	default:
13262c23cb7cSEd Maste 		snprintf(s_top_tag, sizeof(s_top_tag), "Unknown tag: %u", tag);
13272c23cb7cSEd Maste 		return (s_top_tag);
13282c23cb7cSEd Maste 	}
13292c23cb7cSEd Maste }
13302c23cb7cSEd Maste 
13312c23cb7cSEd Maste static const char *
13322c23cb7cSEd Maste aeabi_cpu_arch(uint64_t arch)
13332c23cb7cSEd Maste {
13342c23cb7cSEd Maste 	static char s_cpu_arch[32];
13352c23cb7cSEd Maste 
13362c23cb7cSEd Maste 	switch (arch) {
13372c23cb7cSEd Maste 	case 0: return "Pre-V4";
13382c23cb7cSEd Maste 	case 1: return "ARM v4";
13392c23cb7cSEd Maste 	case 2: return "ARM v4T";
13402c23cb7cSEd Maste 	case 3: return "ARM v5T";
13412c23cb7cSEd Maste 	case 4: return "ARM v5TE";
13422c23cb7cSEd Maste 	case 5: return "ARM v5TEJ";
13432c23cb7cSEd Maste 	case 6: return "ARM v6";
13442c23cb7cSEd Maste 	case 7: return "ARM v6KZ";
13452c23cb7cSEd Maste 	case 8: return "ARM v6T2";
13462c23cb7cSEd Maste 	case 9: return "ARM v6K";
13472c23cb7cSEd Maste 	case 10: return "ARM v7";
13482c23cb7cSEd Maste 	case 11: return "ARM v6-M";
13492c23cb7cSEd Maste 	case 12: return "ARM v6S-M";
13502c23cb7cSEd Maste 	case 13: return "ARM v7E-M";
13512c23cb7cSEd Maste 	default:
13522c23cb7cSEd Maste 		snprintf(s_cpu_arch, sizeof(s_cpu_arch),
13532c23cb7cSEd Maste 		    "Unknown (%ju)", (uintmax_t) arch);
13542c23cb7cSEd Maste 		return (s_cpu_arch);
13552c23cb7cSEd Maste 	}
13562c23cb7cSEd Maste }
13572c23cb7cSEd Maste 
13582c23cb7cSEd Maste static const char *
13592c23cb7cSEd Maste aeabi_cpu_arch_profile(uint64_t pf)
13602c23cb7cSEd Maste {
13612c23cb7cSEd Maste 	static char s_arch_profile[32];
13622c23cb7cSEd Maste 
13632c23cb7cSEd Maste 	switch (pf) {
13642c23cb7cSEd Maste 	case 0:
13652c23cb7cSEd Maste 		return "Not applicable";
13662c23cb7cSEd Maste 	case 0x41:		/* 'A' */
13672c23cb7cSEd Maste 		return "Application Profile";
13682c23cb7cSEd Maste 	case 0x52:		/* 'R' */
13692c23cb7cSEd Maste 		return "Real-Time Profile";
13702c23cb7cSEd Maste 	case 0x4D:		/* 'M' */
13712c23cb7cSEd Maste 		return "Microcontroller Profile";
13722c23cb7cSEd Maste 	case 0x53:		/* 'S' */
13732c23cb7cSEd Maste 		return "Application or Real-Time Profile";
13742c23cb7cSEd Maste 	default:
13752c23cb7cSEd Maste 		snprintf(s_arch_profile, sizeof(s_arch_profile),
13762c23cb7cSEd Maste 		    "Unknown (%ju)\n", (uintmax_t) pf);
13772c23cb7cSEd Maste 		return (s_arch_profile);
13782c23cb7cSEd Maste 	}
13792c23cb7cSEd Maste }
13802c23cb7cSEd Maste 
13812c23cb7cSEd Maste static const char *
13822c23cb7cSEd Maste aeabi_arm_isa(uint64_t ai)
13832c23cb7cSEd Maste {
13842c23cb7cSEd Maste 	static char s_ai[32];
13852c23cb7cSEd Maste 
13862c23cb7cSEd Maste 	switch (ai) {
13872c23cb7cSEd Maste 	case 0: return "No";
13882c23cb7cSEd Maste 	case 1: return "Yes";
13892c23cb7cSEd Maste 	default:
13902c23cb7cSEd Maste 		snprintf(s_ai, sizeof(s_ai), "Unknown (%ju)\n",
13912c23cb7cSEd Maste 		    (uintmax_t) ai);
13922c23cb7cSEd Maste 		return (s_ai);
13932c23cb7cSEd Maste 	}
13942c23cb7cSEd Maste }
13952c23cb7cSEd Maste 
13962c23cb7cSEd Maste static const char *
13972c23cb7cSEd Maste aeabi_thumb_isa(uint64_t ti)
13982c23cb7cSEd Maste {
13992c23cb7cSEd Maste 	static char s_ti[32];
14002c23cb7cSEd Maste 
14012c23cb7cSEd Maste 	switch (ti) {
14022c23cb7cSEd Maste 	case 0: return "No";
14032c23cb7cSEd Maste 	case 1: return "16-bit Thumb";
14042c23cb7cSEd Maste 	case 2: return "32-bit Thumb";
14052c23cb7cSEd Maste 	default:
14062c23cb7cSEd Maste 		snprintf(s_ti, sizeof(s_ti), "Unknown (%ju)\n",
14072c23cb7cSEd Maste 		    (uintmax_t) ti);
14082c23cb7cSEd Maste 		return (s_ti);
14092c23cb7cSEd Maste 	}
14102c23cb7cSEd Maste }
14112c23cb7cSEd Maste 
14122c23cb7cSEd Maste static const char *
14132c23cb7cSEd Maste aeabi_fp_arch(uint64_t fp)
14142c23cb7cSEd Maste {
14152c23cb7cSEd Maste 	static char s_fp_arch[32];
14162c23cb7cSEd Maste 
14172c23cb7cSEd Maste 	switch (fp) {
14182c23cb7cSEd Maste 	case 0: return "No";
14192c23cb7cSEd Maste 	case 1: return "VFPv1";
14202c23cb7cSEd Maste 	case 2: return "VFPv2";
14212c23cb7cSEd Maste 	case 3: return "VFPv3";
14222c23cb7cSEd Maste 	case 4: return "VFPv3-D16";
14232c23cb7cSEd Maste 	case 5: return "VFPv4";
14242c23cb7cSEd Maste 	case 6: return "VFPv4-D16";
14252c23cb7cSEd Maste 	default:
14262c23cb7cSEd Maste 		snprintf(s_fp_arch, sizeof(s_fp_arch), "Unknown (%ju)",
14272c23cb7cSEd Maste 		    (uintmax_t) fp);
14282c23cb7cSEd Maste 		return (s_fp_arch);
14292c23cb7cSEd Maste 	}
14302c23cb7cSEd Maste }
14312c23cb7cSEd Maste 
14322c23cb7cSEd Maste static const char *
14332c23cb7cSEd Maste aeabi_wmmx_arch(uint64_t wmmx)
14342c23cb7cSEd Maste {
14352c23cb7cSEd Maste 	static char s_wmmx[32];
14362c23cb7cSEd Maste 
14372c23cb7cSEd Maste 	switch (wmmx) {
14382c23cb7cSEd Maste 	case 0: return "No";
14392c23cb7cSEd Maste 	case 1: return "WMMXv1";
14402c23cb7cSEd Maste 	case 2: return "WMMXv2";
14412c23cb7cSEd Maste 	default:
14422c23cb7cSEd Maste 		snprintf(s_wmmx, sizeof(s_wmmx), "Unknown (%ju)",
14432c23cb7cSEd Maste 		    (uintmax_t) wmmx);
14442c23cb7cSEd Maste 		return (s_wmmx);
14452c23cb7cSEd Maste 	}
14462c23cb7cSEd Maste }
14472c23cb7cSEd Maste 
14482c23cb7cSEd Maste static const char *
14492c23cb7cSEd Maste aeabi_adv_simd_arch(uint64_t simd)
14502c23cb7cSEd Maste {
14512c23cb7cSEd Maste 	static char s_simd[32];
14522c23cb7cSEd Maste 
14532c23cb7cSEd Maste 	switch (simd) {
14542c23cb7cSEd Maste 	case 0: return "No";
14552c23cb7cSEd Maste 	case 1: return "NEONv1";
14562c23cb7cSEd Maste 	case 2: return "NEONv2";
14572c23cb7cSEd Maste 	default:
14582c23cb7cSEd Maste 		snprintf(s_simd, sizeof(s_simd), "Unknown (%ju)",
14592c23cb7cSEd Maste 		    (uintmax_t) simd);
14602c23cb7cSEd Maste 		return (s_simd);
14612c23cb7cSEd Maste 	}
14622c23cb7cSEd Maste }
14632c23cb7cSEd Maste 
14642c23cb7cSEd Maste static const char *
14652c23cb7cSEd Maste aeabi_pcs_config(uint64_t pcs)
14662c23cb7cSEd Maste {
14672c23cb7cSEd Maste 	static char s_pcs[32];
14682c23cb7cSEd Maste 
14692c23cb7cSEd Maste 	switch (pcs) {
14702c23cb7cSEd Maste 	case 0: return "None";
14712c23cb7cSEd Maste 	case 1: return "Bare platform";
14722c23cb7cSEd Maste 	case 2: return "Linux";
14732c23cb7cSEd Maste 	case 3: return "Linux DSO";
14742c23cb7cSEd Maste 	case 4: return "Palm OS 2004";
14752c23cb7cSEd Maste 	case 5: return "Palm OS (future)";
14762c23cb7cSEd Maste 	case 6: return "Symbian OS 2004";
14772c23cb7cSEd Maste 	case 7: return "Symbian OS (future)";
14782c23cb7cSEd Maste 	default:
14792c23cb7cSEd Maste 		snprintf(s_pcs, sizeof(s_pcs), "Unknown (%ju)",
14802c23cb7cSEd Maste 		    (uintmax_t) pcs);
14812c23cb7cSEd Maste 		return (s_pcs);
14822c23cb7cSEd Maste 	}
14832c23cb7cSEd Maste }
14842c23cb7cSEd Maste 
14852c23cb7cSEd Maste static const char *
14862c23cb7cSEd Maste aeabi_pcs_r9(uint64_t r9)
14872c23cb7cSEd Maste {
14882c23cb7cSEd Maste 	static char s_r9[32];
14892c23cb7cSEd Maste 
14902c23cb7cSEd Maste 	switch (r9) {
14912c23cb7cSEd Maste 	case 0: return "V6";
14922c23cb7cSEd Maste 	case 1: return "SB";
14932c23cb7cSEd Maste 	case 2: return "TLS pointer";
14942c23cb7cSEd Maste 	case 3: return "Unused";
14952c23cb7cSEd Maste 	default:
14962c23cb7cSEd Maste 		snprintf(s_r9, sizeof(s_r9), "Unknown (%ju)", (uintmax_t) r9);
14972c23cb7cSEd Maste 		return (s_r9);
14982c23cb7cSEd Maste 	}
14992c23cb7cSEd Maste }
15002c23cb7cSEd Maste 
15012c23cb7cSEd Maste static const char *
15022c23cb7cSEd Maste aeabi_pcs_rw(uint64_t rw)
15032c23cb7cSEd Maste {
15042c23cb7cSEd Maste 	static char s_rw[32];
15052c23cb7cSEd Maste 
15062c23cb7cSEd Maste 	switch (rw) {
15072c23cb7cSEd Maste 	case 0: return "Absolute";
15082c23cb7cSEd Maste 	case 1: return "PC-relative";
15092c23cb7cSEd Maste 	case 2: return "SB-relative";
15102c23cb7cSEd Maste 	case 3: return "None";
15112c23cb7cSEd Maste 	default:
15122c23cb7cSEd Maste 		snprintf(s_rw, sizeof(s_rw), "Unknown (%ju)", (uintmax_t) rw);
15132c23cb7cSEd Maste 		return (s_rw);
15142c23cb7cSEd Maste 	}
15152c23cb7cSEd Maste }
15162c23cb7cSEd Maste 
15172c23cb7cSEd Maste static const char *
15182c23cb7cSEd Maste aeabi_pcs_ro(uint64_t ro)
15192c23cb7cSEd Maste {
15202c23cb7cSEd Maste 	static char s_ro[32];
15212c23cb7cSEd Maste 
15222c23cb7cSEd Maste 	switch (ro) {
15232c23cb7cSEd Maste 	case 0: return "Absolute";
15242c23cb7cSEd Maste 	case 1: return "PC-relative";
15252c23cb7cSEd Maste 	case 2: return "None";
15262c23cb7cSEd Maste 	default:
15272c23cb7cSEd Maste 		snprintf(s_ro, sizeof(s_ro), "Unknown (%ju)", (uintmax_t) ro);
15282c23cb7cSEd Maste 		return (s_ro);
15292c23cb7cSEd Maste 	}
15302c23cb7cSEd Maste }
15312c23cb7cSEd Maste 
15322c23cb7cSEd Maste static const char *
15332c23cb7cSEd Maste aeabi_pcs_got(uint64_t got)
15342c23cb7cSEd Maste {
15352c23cb7cSEd Maste 	static char s_got[32];
15362c23cb7cSEd Maste 
15372c23cb7cSEd Maste 	switch (got) {
15382c23cb7cSEd Maste 	case 0: return "None";
15392c23cb7cSEd Maste 	case 1: return "direct";
15402c23cb7cSEd Maste 	case 2: return "indirect via GOT";
15412c23cb7cSEd Maste 	default:
15422c23cb7cSEd Maste 		snprintf(s_got, sizeof(s_got), "Unknown (%ju)",
15432c23cb7cSEd Maste 		    (uintmax_t) got);
15442c23cb7cSEd Maste 		return (s_got);
15452c23cb7cSEd Maste 	}
15462c23cb7cSEd Maste }
15472c23cb7cSEd Maste 
15482c23cb7cSEd Maste static const char *
15492c23cb7cSEd Maste aeabi_pcs_wchar_t(uint64_t wt)
15502c23cb7cSEd Maste {
15512c23cb7cSEd Maste 	static char s_wt[32];
15522c23cb7cSEd Maste 
15532c23cb7cSEd Maste 	switch (wt) {
15542c23cb7cSEd Maste 	case 0: return "None";
15552c23cb7cSEd Maste 	case 2: return "wchar_t size 2";
15562c23cb7cSEd Maste 	case 4: return "wchar_t size 4";
15572c23cb7cSEd Maste 	default:
15582c23cb7cSEd Maste 		snprintf(s_wt, sizeof(s_wt), "Unknown (%ju)", (uintmax_t) wt);
15592c23cb7cSEd Maste 		return (s_wt);
15602c23cb7cSEd Maste 	}
15612c23cb7cSEd Maste }
15622c23cb7cSEd Maste 
15632c23cb7cSEd Maste static const char *
15642c23cb7cSEd Maste aeabi_enum_size(uint64_t es)
15652c23cb7cSEd Maste {
15662c23cb7cSEd Maste 	static char s_es[32];
15672c23cb7cSEd Maste 
15682c23cb7cSEd Maste 	switch (es) {
15692c23cb7cSEd Maste 	case 0: return "None";
15702c23cb7cSEd Maste 	case 1: return "smallest";
15712c23cb7cSEd Maste 	case 2: return "32-bit";
15722c23cb7cSEd Maste 	case 3: return "visible 32-bit";
15732c23cb7cSEd Maste 	default:
15742c23cb7cSEd Maste 		snprintf(s_es, sizeof(s_es), "Unknown (%ju)", (uintmax_t) es);
15752c23cb7cSEd Maste 		return (s_es);
15762c23cb7cSEd Maste 	}
15772c23cb7cSEd Maste }
15782c23cb7cSEd Maste 
15792c23cb7cSEd Maste static const char *
15802c23cb7cSEd Maste aeabi_align_needed(uint64_t an)
15812c23cb7cSEd Maste {
15822c23cb7cSEd Maste 	static char s_align_n[64];
15832c23cb7cSEd Maste 
15842c23cb7cSEd Maste 	switch (an) {
15852c23cb7cSEd Maste 	case 0: return "No";
15862c23cb7cSEd Maste 	case 1: return "8-byte align";
15872c23cb7cSEd Maste 	case 2: return "4-byte align";
15882c23cb7cSEd Maste 	case 3: return "Reserved";
15892c23cb7cSEd Maste 	default:
15902c23cb7cSEd Maste 		if (an >= 4 && an <= 12)
15912c23cb7cSEd Maste 			snprintf(s_align_n, sizeof(s_align_n), "8-byte align"
15922c23cb7cSEd Maste 			    " and up to 2^%ju-byte extended align",
15932c23cb7cSEd Maste 			    (uintmax_t) an);
15942c23cb7cSEd Maste 		else
15952c23cb7cSEd Maste 			snprintf(s_align_n, sizeof(s_align_n), "Unknown (%ju)",
15962c23cb7cSEd Maste 			    (uintmax_t) an);
15972c23cb7cSEd Maste 		return (s_align_n);
15982c23cb7cSEd Maste 	}
15992c23cb7cSEd Maste }
16002c23cb7cSEd Maste 
16012c23cb7cSEd Maste static const char *
16022c23cb7cSEd Maste aeabi_align_preserved(uint64_t ap)
16032c23cb7cSEd Maste {
16042c23cb7cSEd Maste 	static char s_align_p[128];
16052c23cb7cSEd Maste 
16062c23cb7cSEd Maste 	switch (ap) {
16072c23cb7cSEd Maste 	case 0: return "No";
16082c23cb7cSEd Maste 	case 1: return "8-byte align";
16092c23cb7cSEd Maste 	case 2: return "8-byte align and SP % 8 == 0";
16102c23cb7cSEd Maste 	case 3: return "Reserved";
16112c23cb7cSEd Maste 	default:
16122c23cb7cSEd Maste 		if (ap >= 4 && ap <= 12)
16132c23cb7cSEd Maste 			snprintf(s_align_p, sizeof(s_align_p), "8-byte align"
16142c23cb7cSEd Maste 			    " and SP %% 8 == 0 and up to 2^%ju-byte extended"
16152c23cb7cSEd Maste 			    " align", (uintmax_t) ap);
16162c23cb7cSEd Maste 		else
16172c23cb7cSEd Maste 			snprintf(s_align_p, sizeof(s_align_p), "Unknown (%ju)",
16182c23cb7cSEd Maste 			    (uintmax_t) ap);
16192c23cb7cSEd Maste 		return (s_align_p);
16202c23cb7cSEd Maste 	}
16212c23cb7cSEd Maste }
16222c23cb7cSEd Maste 
16232c23cb7cSEd Maste static const char *
16242c23cb7cSEd Maste aeabi_fp_rounding(uint64_t fr)
16252c23cb7cSEd Maste {
16262c23cb7cSEd Maste 	static char s_fp_r[32];
16272c23cb7cSEd Maste 
16282c23cb7cSEd Maste 	switch (fr) {
16292c23cb7cSEd Maste 	case 0: return "Unused";
16302c23cb7cSEd Maste 	case 1: return "Needed";
16312c23cb7cSEd Maste 	default:
16322c23cb7cSEd Maste 		snprintf(s_fp_r, sizeof(s_fp_r), "Unknown (%ju)",
16332c23cb7cSEd Maste 		    (uintmax_t) fr);
16342c23cb7cSEd Maste 		return (s_fp_r);
16352c23cb7cSEd Maste 	}
16362c23cb7cSEd Maste }
16372c23cb7cSEd Maste 
16382c23cb7cSEd Maste static const char *
16392c23cb7cSEd Maste aeabi_fp_denormal(uint64_t fd)
16402c23cb7cSEd Maste {
16412c23cb7cSEd Maste 	static char s_fp_d[32];
16422c23cb7cSEd Maste 
16432c23cb7cSEd Maste 	switch (fd) {
16442c23cb7cSEd Maste 	case 0: return "Unused";
16452c23cb7cSEd Maste 	case 1: return "Needed";
16462c23cb7cSEd Maste 	case 2: return "Sign Only";
16472c23cb7cSEd Maste 	default:
16482c23cb7cSEd Maste 		snprintf(s_fp_d, sizeof(s_fp_d), "Unknown (%ju)",
16492c23cb7cSEd Maste 		    (uintmax_t) fd);
16502c23cb7cSEd Maste 		return (s_fp_d);
16512c23cb7cSEd Maste 	}
16522c23cb7cSEd Maste }
16532c23cb7cSEd Maste 
16542c23cb7cSEd Maste static const char *
16552c23cb7cSEd Maste aeabi_fp_exceptions(uint64_t fe)
16562c23cb7cSEd Maste {
16572c23cb7cSEd Maste 	static char s_fp_e[32];
16582c23cb7cSEd Maste 
16592c23cb7cSEd Maste 	switch (fe) {
16602c23cb7cSEd Maste 	case 0: return "Unused";
16612c23cb7cSEd Maste 	case 1: return "Needed";
16622c23cb7cSEd Maste 	default:
16632c23cb7cSEd Maste 		snprintf(s_fp_e, sizeof(s_fp_e), "Unknown (%ju)",
16642c23cb7cSEd Maste 		    (uintmax_t) fe);
16652c23cb7cSEd Maste 		return (s_fp_e);
16662c23cb7cSEd Maste 	}
16672c23cb7cSEd Maste }
16682c23cb7cSEd Maste 
16692c23cb7cSEd Maste static const char *
16702c23cb7cSEd Maste aeabi_fp_user_exceptions(uint64_t fu)
16712c23cb7cSEd Maste {
16722c23cb7cSEd Maste 	static char s_fp_u[32];
16732c23cb7cSEd Maste 
16742c23cb7cSEd Maste 	switch (fu) {
16752c23cb7cSEd Maste 	case 0: return "Unused";
16762c23cb7cSEd Maste 	case 1: return "Needed";
16772c23cb7cSEd Maste 	default:
16782c23cb7cSEd Maste 		snprintf(s_fp_u, sizeof(s_fp_u), "Unknown (%ju)",
16792c23cb7cSEd Maste 		    (uintmax_t) fu);
16802c23cb7cSEd Maste 		return (s_fp_u);
16812c23cb7cSEd Maste 	}
16822c23cb7cSEd Maste }
16832c23cb7cSEd Maste 
16842c23cb7cSEd Maste static const char *
16852c23cb7cSEd Maste aeabi_fp_number_model(uint64_t fn)
16862c23cb7cSEd Maste {
16872c23cb7cSEd Maste 	static char s_fp_n[32];
16882c23cb7cSEd Maste 
16892c23cb7cSEd Maste 	switch (fn) {
16902c23cb7cSEd Maste 	case 0: return "Unused";
16912c23cb7cSEd Maste 	case 1: return "IEEE 754 normal";
16922c23cb7cSEd Maste 	case 2: return "RTABI";
16932c23cb7cSEd Maste 	case 3: return "IEEE 754";
16942c23cb7cSEd Maste 	default:
16952c23cb7cSEd Maste 		snprintf(s_fp_n, sizeof(s_fp_n), "Unknown (%ju)",
16962c23cb7cSEd Maste 		    (uintmax_t) fn);
16972c23cb7cSEd Maste 		return (s_fp_n);
16982c23cb7cSEd Maste 	}
16992c23cb7cSEd Maste }
17002c23cb7cSEd Maste 
17012c23cb7cSEd Maste static const char *
17022c23cb7cSEd Maste aeabi_fp_16bit_format(uint64_t fp16)
17032c23cb7cSEd Maste {
17042c23cb7cSEd Maste 	static char s_fp_16[64];
17052c23cb7cSEd Maste 
17062c23cb7cSEd Maste 	switch (fp16) {
17072c23cb7cSEd Maste 	case 0: return "None";
17082c23cb7cSEd Maste 	case 1: return "IEEE 754";
17092c23cb7cSEd Maste 	case 2: return "VFPv3/Advanced SIMD (alternative format)";
17102c23cb7cSEd Maste 	default:
17112c23cb7cSEd Maste 		snprintf(s_fp_16, sizeof(s_fp_16), "Unknown (%ju)",
17122c23cb7cSEd Maste 		    (uintmax_t) fp16);
17132c23cb7cSEd Maste 		return (s_fp_16);
17142c23cb7cSEd Maste 	}
17152c23cb7cSEd Maste }
17162c23cb7cSEd Maste 
17172c23cb7cSEd Maste static const char *
17182c23cb7cSEd Maste aeabi_mpext(uint64_t mp)
17192c23cb7cSEd Maste {
17202c23cb7cSEd Maste 	static char s_mp[32];
17212c23cb7cSEd Maste 
17222c23cb7cSEd Maste 	switch (mp) {
17232c23cb7cSEd Maste 	case 0: return "Not allowed";
17242c23cb7cSEd Maste 	case 1: return "Allowed";
17252c23cb7cSEd Maste 	default:
17262c23cb7cSEd Maste 		snprintf(s_mp, sizeof(s_mp), "Unknown (%ju)",
17272c23cb7cSEd Maste 		    (uintmax_t) mp);
17282c23cb7cSEd Maste 		return (s_mp);
17292c23cb7cSEd Maste 	}
17302c23cb7cSEd Maste }
17312c23cb7cSEd Maste 
17322c23cb7cSEd Maste static const char *
17332c23cb7cSEd Maste aeabi_div(uint64_t du)
17342c23cb7cSEd Maste {
17352c23cb7cSEd Maste 	static char s_du[32];
17362c23cb7cSEd Maste 
17372c23cb7cSEd Maste 	switch (du) {
17382c23cb7cSEd Maste 	case 0: return "Yes (V7-R/V7-M)";
17392c23cb7cSEd Maste 	case 1: return "No";
17402c23cb7cSEd Maste 	case 2: return "Yes (V7-A)";
17412c23cb7cSEd Maste 	default:
17422c23cb7cSEd Maste 		snprintf(s_du, sizeof(s_du), "Unknown (%ju)",
17432c23cb7cSEd Maste 		    (uintmax_t) du);
17442c23cb7cSEd Maste 		return (s_du);
17452c23cb7cSEd Maste 	}
17462c23cb7cSEd Maste }
17472c23cb7cSEd Maste 
17482c23cb7cSEd Maste static const char *
17492c23cb7cSEd Maste aeabi_t2ee(uint64_t t2ee)
17502c23cb7cSEd Maste {
17512c23cb7cSEd Maste 	static char s_t2ee[32];
17522c23cb7cSEd Maste 
17532c23cb7cSEd Maste 	switch (t2ee) {
17542c23cb7cSEd Maste 	case 0: return "Not allowed";
17552c23cb7cSEd Maste 	case 1: return "Allowed";
17562c23cb7cSEd Maste 	default:
17572c23cb7cSEd Maste 		snprintf(s_t2ee, sizeof(s_t2ee), "Unknown(%ju)",
17582c23cb7cSEd Maste 		    (uintmax_t) t2ee);
17592c23cb7cSEd Maste 		return (s_t2ee);
17602c23cb7cSEd Maste 	}
17612c23cb7cSEd Maste 
17622c23cb7cSEd Maste }
17632c23cb7cSEd Maste 
17642c23cb7cSEd Maste static const char *
17652c23cb7cSEd Maste aeabi_hardfp(uint64_t hfp)
17662c23cb7cSEd Maste {
17672c23cb7cSEd Maste 	static char s_hfp[32];
17682c23cb7cSEd Maste 
17692c23cb7cSEd Maste 	switch (hfp) {
17702c23cb7cSEd Maste 	case 0: return "Tag_FP_arch";
17712c23cb7cSEd Maste 	case 1: return "only SP";
17722c23cb7cSEd Maste 	case 2: return "only DP";
17732c23cb7cSEd Maste 	case 3: return "both SP and DP";
17742c23cb7cSEd Maste 	default:
17752c23cb7cSEd Maste 		snprintf(s_hfp, sizeof(s_hfp), "Unknown (%ju)",
17762c23cb7cSEd Maste 		    (uintmax_t) hfp);
17772c23cb7cSEd Maste 		return (s_hfp);
17782c23cb7cSEd Maste 	}
17792c23cb7cSEd Maste }
17802c23cb7cSEd Maste 
17812c23cb7cSEd Maste static const char *
17822c23cb7cSEd Maste aeabi_vfp_args(uint64_t va)
17832c23cb7cSEd Maste {
17842c23cb7cSEd Maste 	static char s_va[32];
17852c23cb7cSEd Maste 
17862c23cb7cSEd Maste 	switch (va) {
17872c23cb7cSEd Maste 	case 0: return "AAPCS (base variant)";
17882c23cb7cSEd Maste 	case 1: return "AAPCS (VFP variant)";
17892c23cb7cSEd Maste 	case 2: return "toolchain-specific";
17902c23cb7cSEd Maste 	default:
17912c23cb7cSEd Maste 		snprintf(s_va, sizeof(s_va), "Unknown (%ju)", (uintmax_t) va);
17922c23cb7cSEd Maste 		return (s_va);
17932c23cb7cSEd Maste 	}
17942c23cb7cSEd Maste }
17952c23cb7cSEd Maste 
17962c23cb7cSEd Maste static const char *
17972c23cb7cSEd Maste aeabi_wmmx_args(uint64_t wa)
17982c23cb7cSEd Maste {
17992c23cb7cSEd Maste 	static char s_wa[32];
18002c23cb7cSEd Maste 
18012c23cb7cSEd Maste 	switch (wa) {
18022c23cb7cSEd Maste 	case 0: return "AAPCS (base variant)";
18032c23cb7cSEd Maste 	case 1: return "Intel WMMX";
18042c23cb7cSEd Maste 	case 2: return "toolchain-specific";
18052c23cb7cSEd Maste 	default:
18062c23cb7cSEd Maste 		snprintf(s_wa, sizeof(s_wa), "Unknown(%ju)", (uintmax_t) wa);
18072c23cb7cSEd Maste 		return (s_wa);
18082c23cb7cSEd Maste 	}
18092c23cb7cSEd Maste }
18102c23cb7cSEd Maste 
18112c23cb7cSEd Maste static const char *
18122c23cb7cSEd Maste aeabi_unaligned_access(uint64_t ua)
18132c23cb7cSEd Maste {
18142c23cb7cSEd Maste 	static char s_ua[32];
18152c23cb7cSEd Maste 
18162c23cb7cSEd Maste 	switch (ua) {
18172c23cb7cSEd Maste 	case 0: return "Not allowed";
18182c23cb7cSEd Maste 	case 1: return "Allowed";
18192c23cb7cSEd Maste 	default:
18202c23cb7cSEd Maste 		snprintf(s_ua, sizeof(s_ua), "Unknown(%ju)", (uintmax_t) ua);
18212c23cb7cSEd Maste 		return (s_ua);
18222c23cb7cSEd Maste 	}
18232c23cb7cSEd Maste }
18242c23cb7cSEd Maste 
18252c23cb7cSEd Maste static const char *
18262c23cb7cSEd Maste aeabi_fp_hpext(uint64_t fh)
18272c23cb7cSEd Maste {
18282c23cb7cSEd Maste 	static char s_fh[32];
18292c23cb7cSEd Maste 
18302c23cb7cSEd Maste 	switch (fh) {
18312c23cb7cSEd Maste 	case 0: return "Not allowed";
18322c23cb7cSEd Maste 	case 1: return "Allowed";
18332c23cb7cSEd Maste 	default:
18342c23cb7cSEd Maste 		snprintf(s_fh, sizeof(s_fh), "Unknown(%ju)", (uintmax_t) fh);
18352c23cb7cSEd Maste 		return (s_fh);
18362c23cb7cSEd Maste 	}
18372c23cb7cSEd Maste }
18382c23cb7cSEd Maste 
18392c23cb7cSEd Maste static const char *
18402c23cb7cSEd Maste aeabi_optm_goal(uint64_t og)
18412c23cb7cSEd Maste {
18422c23cb7cSEd Maste 	static char s_og[32];
18432c23cb7cSEd Maste 
18442c23cb7cSEd Maste 	switch (og) {
18452c23cb7cSEd Maste 	case 0: return "None";
18462c23cb7cSEd Maste 	case 1: return "Speed";
18472c23cb7cSEd Maste 	case 2: return "Speed aggressive";
18482c23cb7cSEd Maste 	case 3: return "Space";
18492c23cb7cSEd Maste 	case 4: return "Space aggressive";
18502c23cb7cSEd Maste 	case 5: return "Debugging";
18512c23cb7cSEd Maste 	case 6: return "Best Debugging";
18522c23cb7cSEd Maste 	default:
18532c23cb7cSEd Maste 		snprintf(s_og, sizeof(s_og), "Unknown(%ju)", (uintmax_t) og);
18542c23cb7cSEd Maste 		return (s_og);
18552c23cb7cSEd Maste 	}
18562c23cb7cSEd Maste }
18572c23cb7cSEd Maste 
18582c23cb7cSEd Maste static const char *
18592c23cb7cSEd Maste aeabi_fp_optm_goal(uint64_t fog)
18602c23cb7cSEd Maste {
18612c23cb7cSEd Maste 	static char s_fog[32];
18622c23cb7cSEd Maste 
18632c23cb7cSEd Maste 	switch (fog) {
18642c23cb7cSEd Maste 	case 0: return "None";
18652c23cb7cSEd Maste 	case 1: return "Speed";
18662c23cb7cSEd Maste 	case 2: return "Speed aggressive";
18672c23cb7cSEd Maste 	case 3: return "Space";
18682c23cb7cSEd Maste 	case 4: return "Space aggressive";
18692c23cb7cSEd Maste 	case 5: return "Accurary";
18702c23cb7cSEd Maste 	case 6: return "Best Accurary";
18712c23cb7cSEd Maste 	default:
18722c23cb7cSEd Maste 		snprintf(s_fog, sizeof(s_fog), "Unknown(%ju)",
18732c23cb7cSEd Maste 		    (uintmax_t) fog);
18742c23cb7cSEd Maste 		return (s_fog);
18752c23cb7cSEd Maste 	}
18762c23cb7cSEd Maste }
18772c23cb7cSEd Maste 
18782c23cb7cSEd Maste static const char *
18792c23cb7cSEd Maste aeabi_virtual(uint64_t vt)
18802c23cb7cSEd Maste {
18812c23cb7cSEd Maste 	static char s_virtual[64];
18822c23cb7cSEd Maste 
18832c23cb7cSEd Maste 	switch (vt) {
18842c23cb7cSEd Maste 	case 0: return "No";
18852c23cb7cSEd Maste 	case 1: return "TrustZone";
18862c23cb7cSEd Maste 	case 2: return "Virtualization extension";
18872c23cb7cSEd Maste 	case 3: return "TrustZone and virtualization extension";
18882c23cb7cSEd Maste 	default:
18892c23cb7cSEd Maste 		snprintf(s_virtual, sizeof(s_virtual), "Unknown(%ju)",
18902c23cb7cSEd Maste 		    (uintmax_t) vt);
18912c23cb7cSEd Maste 		return (s_virtual);
18922c23cb7cSEd Maste 	}
18932c23cb7cSEd Maste }
18942c23cb7cSEd Maste 
18952c23cb7cSEd Maste static struct {
18962c23cb7cSEd Maste 	uint64_t tag;
18972c23cb7cSEd Maste 	const char *s_tag;
18982c23cb7cSEd Maste 	const char *(*get_desc)(uint64_t val);
18992c23cb7cSEd Maste } aeabi_tags[] = {
19002c23cb7cSEd Maste 	{4, "Tag_CPU_raw_name", NULL},
19012c23cb7cSEd Maste 	{5, "Tag_CPU_name", NULL},
19022c23cb7cSEd Maste 	{6, "Tag_CPU_arch", aeabi_cpu_arch},
19032c23cb7cSEd Maste 	{7, "Tag_CPU_arch_profile", aeabi_cpu_arch_profile},
19042c23cb7cSEd Maste 	{8, "Tag_ARM_ISA_use", aeabi_arm_isa},
19052c23cb7cSEd Maste 	{9, "Tag_THUMB_ISA_use", aeabi_thumb_isa},
19062c23cb7cSEd Maste 	{10, "Tag_FP_arch", aeabi_fp_arch},
19072c23cb7cSEd Maste 	{11, "Tag_WMMX_arch", aeabi_wmmx_arch},
19082c23cb7cSEd Maste 	{12, "Tag_Advanced_SIMD_arch", aeabi_adv_simd_arch},
19092c23cb7cSEd Maste 	{13, "Tag_PCS_config", aeabi_pcs_config},
19102c23cb7cSEd Maste 	{14, "Tag_ABI_PCS_R9_use", aeabi_pcs_r9},
19112c23cb7cSEd Maste 	{15, "Tag_ABI_PCS_RW_data", aeabi_pcs_rw},
19122c23cb7cSEd Maste 	{16, "Tag_ABI_PCS_RO_data", aeabi_pcs_ro},
19132c23cb7cSEd Maste 	{17, "Tag_ABI_PCS_GOT_use", aeabi_pcs_got},
19142c23cb7cSEd Maste 	{18, "Tag_ABI_PCS_wchar_t", aeabi_pcs_wchar_t},
19152c23cb7cSEd Maste 	{19, "Tag_ABI_FP_rounding", aeabi_fp_rounding},
19162c23cb7cSEd Maste 	{20, "Tag_ABI_FP_denormal", aeabi_fp_denormal},
19172c23cb7cSEd Maste 	{21, "Tag_ABI_FP_exceptions", aeabi_fp_exceptions},
19182c23cb7cSEd Maste 	{22, "Tag_ABI_FP_user_exceptions", aeabi_fp_user_exceptions},
19192c23cb7cSEd Maste 	{23, "Tag_ABI_FP_number_model", aeabi_fp_number_model},
19202c23cb7cSEd Maste 	{24, "Tag_ABI_align_needed", aeabi_align_needed},
19212c23cb7cSEd Maste 	{25, "Tag_ABI_align_preserved", aeabi_align_preserved},
19222c23cb7cSEd Maste 	{26, "Tag_ABI_enum_size", aeabi_enum_size},
19232c23cb7cSEd Maste 	{27, "Tag_ABI_HardFP_use", aeabi_hardfp},
19242c23cb7cSEd Maste 	{28, "Tag_ABI_VFP_args", aeabi_vfp_args},
19252c23cb7cSEd Maste 	{29, "Tag_ABI_WMMX_args", aeabi_wmmx_args},
19262c23cb7cSEd Maste 	{30, "Tag_ABI_optimization_goals", aeabi_optm_goal},
19272c23cb7cSEd Maste 	{31, "Tag_ABI_FP_optimization_goals", aeabi_fp_optm_goal},
19282c23cb7cSEd Maste 	{32, "Tag_compatibility", NULL},
19292c23cb7cSEd Maste 	{34, "Tag_CPU_unaligned_access", aeabi_unaligned_access},
19302c23cb7cSEd Maste 	{36, "Tag_FP_HP_extension", aeabi_fp_hpext},
19312c23cb7cSEd Maste 	{38, "Tag_ABI_FP_16bit_format", aeabi_fp_16bit_format},
19322c23cb7cSEd Maste 	{42, "Tag_MPextension_use", aeabi_mpext},
19332c23cb7cSEd Maste 	{44, "Tag_DIV_use", aeabi_div},
19342c23cb7cSEd Maste 	{64, "Tag_nodefaults", NULL},
19352c23cb7cSEd Maste 	{65, "Tag_also_compatible_with", NULL},
19362c23cb7cSEd Maste 	{66, "Tag_T2EE_use", aeabi_t2ee},
19372c23cb7cSEd Maste 	{67, "Tag_conformance", NULL},
19382c23cb7cSEd Maste 	{68, "Tag_Virtualization_use", aeabi_virtual},
19392c23cb7cSEd Maste 	{70, "Tag_MPextension_use", aeabi_mpext},
19402c23cb7cSEd Maste };
19412c23cb7cSEd Maste 
19422c23cb7cSEd Maste static const char *
19432c23cb7cSEd Maste mips_abi_fp(uint64_t fp)
19442c23cb7cSEd Maste {
19452c23cb7cSEd Maste 	static char s_mips_abi_fp[64];
19462c23cb7cSEd Maste 
19472c23cb7cSEd Maste 	switch (fp) {
19482c23cb7cSEd Maste 	case 0: return "N/A";
19492c23cb7cSEd Maste 	case 1: return "Hard float (double precision)";
19502c23cb7cSEd Maste 	case 2: return "Hard float (single precision)";
19512c23cb7cSEd Maste 	case 3: return "Soft float";
19522c23cb7cSEd Maste 	case 4: return "64-bit float (-mips32r2 -mfp64)";
19532c23cb7cSEd Maste 	default:
19542c23cb7cSEd Maste 		snprintf(s_mips_abi_fp, sizeof(s_mips_abi_fp), "Unknown(%ju)",
19552c23cb7cSEd Maste 		    (uintmax_t) fp);
19562c23cb7cSEd Maste 		return (s_mips_abi_fp);
19572c23cb7cSEd Maste 	}
19582c23cb7cSEd Maste }
19592c23cb7cSEd Maste 
19602c23cb7cSEd Maste static const char *
19612c23cb7cSEd Maste ppc_abi_fp(uint64_t fp)
19622c23cb7cSEd Maste {
19632c23cb7cSEd Maste 	static char s_ppc_abi_fp[64];
19642c23cb7cSEd Maste 
19652c23cb7cSEd Maste 	switch (fp) {
19662c23cb7cSEd Maste 	case 0: return "N/A";
19672c23cb7cSEd Maste 	case 1: return "Hard float (double precision)";
19682c23cb7cSEd Maste 	case 2: return "Soft float";
19692c23cb7cSEd Maste 	case 3: return "Hard float (single precision)";
19702c23cb7cSEd Maste 	default:
19712c23cb7cSEd Maste 		snprintf(s_ppc_abi_fp, sizeof(s_ppc_abi_fp), "Unknown(%ju)",
19722c23cb7cSEd Maste 		    (uintmax_t) fp);
19732c23cb7cSEd Maste 		return (s_ppc_abi_fp);
19742c23cb7cSEd Maste 	}
19752c23cb7cSEd Maste }
19762c23cb7cSEd Maste 
19772c23cb7cSEd Maste static const char *
19782c23cb7cSEd Maste ppc_abi_vector(uint64_t vec)
19792c23cb7cSEd Maste {
19802c23cb7cSEd Maste 	static char s_vec[64];
19812c23cb7cSEd Maste 
19822c23cb7cSEd Maste 	switch (vec) {
19832c23cb7cSEd Maste 	case 0: return "N/A";
19842c23cb7cSEd Maste 	case 1: return "Generic purpose registers";
19852c23cb7cSEd Maste 	case 2: return "AltiVec registers";
19862c23cb7cSEd Maste 	case 3: return "SPE registers";
19872c23cb7cSEd Maste 	default:
19882c23cb7cSEd Maste 		snprintf(s_vec, sizeof(s_vec), "Unknown(%ju)", (uintmax_t) vec);
19892c23cb7cSEd Maste 		return (s_vec);
19902c23cb7cSEd Maste 	}
19912c23cb7cSEd Maste }
19922c23cb7cSEd Maste 
1993cf781b2eSEd Maste static const char *
1994cf781b2eSEd Maste dwarf_reg(unsigned int mach, unsigned int reg)
1995cf781b2eSEd Maste {
1996cf781b2eSEd Maste 
1997cf781b2eSEd Maste 	switch (mach) {
1998cf781b2eSEd Maste 	case EM_386:
19993ef90571SEd Maste 	case EM_IAMCU:
2000cf781b2eSEd Maste 		switch (reg) {
2001cf781b2eSEd Maste 		case 0: return "eax";
2002cf781b2eSEd Maste 		case 1: return "ecx";
2003cf781b2eSEd Maste 		case 2: return "edx";
2004cf781b2eSEd Maste 		case 3: return "ebx";
2005cf781b2eSEd Maste 		case 4: return "esp";
2006cf781b2eSEd Maste 		case 5: return "ebp";
2007cf781b2eSEd Maste 		case 6: return "esi";
2008cf781b2eSEd Maste 		case 7: return "edi";
2009cf781b2eSEd Maste 		case 8: return "eip";
2010cf781b2eSEd Maste 		case 9: return "eflags";
2011cf781b2eSEd Maste 		case 11: return "st0";
2012cf781b2eSEd Maste 		case 12: return "st1";
2013cf781b2eSEd Maste 		case 13: return "st2";
2014cf781b2eSEd Maste 		case 14: return "st3";
2015cf781b2eSEd Maste 		case 15: return "st4";
2016cf781b2eSEd Maste 		case 16: return "st5";
2017cf781b2eSEd Maste 		case 17: return "st6";
2018cf781b2eSEd Maste 		case 18: return "st7";
2019cf781b2eSEd Maste 		case 21: return "xmm0";
2020cf781b2eSEd Maste 		case 22: return "xmm1";
2021cf781b2eSEd Maste 		case 23: return "xmm2";
2022cf781b2eSEd Maste 		case 24: return "xmm3";
2023cf781b2eSEd Maste 		case 25: return "xmm4";
2024cf781b2eSEd Maste 		case 26: return "xmm5";
2025cf781b2eSEd Maste 		case 27: return "xmm6";
2026cf781b2eSEd Maste 		case 28: return "xmm7";
2027cf781b2eSEd Maste 		case 29: return "mm0";
2028cf781b2eSEd Maste 		case 30: return "mm1";
2029cf781b2eSEd Maste 		case 31: return "mm2";
2030cf781b2eSEd Maste 		case 32: return "mm3";
2031cf781b2eSEd Maste 		case 33: return "mm4";
2032cf781b2eSEd Maste 		case 34: return "mm5";
2033cf781b2eSEd Maste 		case 35: return "mm6";
2034cf781b2eSEd Maste 		case 36: return "mm7";
2035cf781b2eSEd Maste 		case 37: return "fcw";
2036cf781b2eSEd Maste 		case 38: return "fsw";
2037cf781b2eSEd Maste 		case 39: return "mxcsr";
2038cf781b2eSEd Maste 		case 40: return "es";
2039cf781b2eSEd Maste 		case 41: return "cs";
2040cf781b2eSEd Maste 		case 42: return "ss";
2041cf781b2eSEd Maste 		case 43: return "ds";
2042cf781b2eSEd Maste 		case 44: return "fs";
2043cf781b2eSEd Maste 		case 45: return "gs";
2044cf781b2eSEd Maste 		case 48: return "tr";
2045cf781b2eSEd Maste 		case 49: return "ldtr";
2046cf781b2eSEd Maste 		default: return (NULL);
2047cf781b2eSEd Maste 		}
2048cf781b2eSEd Maste 	case EM_X86_64:
2049cf781b2eSEd Maste 		switch (reg) {
2050cf781b2eSEd Maste 		case 0: return "rax";
2051cf781b2eSEd Maste 		case 1: return "rdx";
2052cf781b2eSEd Maste 		case 2: return "rcx";
2053cf781b2eSEd Maste 		case 3: return "rbx";
2054cf781b2eSEd Maste 		case 4: return "rsi";
2055cf781b2eSEd Maste 		case 5: return "rdi";
2056cf781b2eSEd Maste 		case 6: return "rbp";
2057cf781b2eSEd Maste 		case 7: return "rsp";
2058cf781b2eSEd Maste 		case 16: return "rip";
2059cf781b2eSEd Maste 		case 17: return "xmm0";
2060cf781b2eSEd Maste 		case 18: return "xmm1";
2061cf781b2eSEd Maste 		case 19: return "xmm2";
2062cf781b2eSEd Maste 		case 20: return "xmm3";
2063cf781b2eSEd Maste 		case 21: return "xmm4";
2064cf781b2eSEd Maste 		case 22: return "xmm5";
2065cf781b2eSEd Maste 		case 23: return "xmm6";
2066cf781b2eSEd Maste 		case 24: return "xmm7";
2067cf781b2eSEd Maste 		case 25: return "xmm8";
2068cf781b2eSEd Maste 		case 26: return "xmm9";
2069cf781b2eSEd Maste 		case 27: return "xmm10";
2070cf781b2eSEd Maste 		case 28: return "xmm11";
2071cf781b2eSEd Maste 		case 29: return "xmm12";
2072cf781b2eSEd Maste 		case 30: return "xmm13";
2073cf781b2eSEd Maste 		case 31: return "xmm14";
2074cf781b2eSEd Maste 		case 32: return "xmm15";
2075cf781b2eSEd Maste 		case 33: return "st0";
2076cf781b2eSEd Maste 		case 34: return "st1";
2077cf781b2eSEd Maste 		case 35: return "st2";
2078cf781b2eSEd Maste 		case 36: return "st3";
2079cf781b2eSEd Maste 		case 37: return "st4";
2080cf781b2eSEd Maste 		case 38: return "st5";
2081cf781b2eSEd Maste 		case 39: return "st6";
2082cf781b2eSEd Maste 		case 40: return "st7";
2083cf781b2eSEd Maste 		case 41: return "mm0";
2084cf781b2eSEd Maste 		case 42: return "mm1";
2085cf781b2eSEd Maste 		case 43: return "mm2";
2086cf781b2eSEd Maste 		case 44: return "mm3";
2087cf781b2eSEd Maste 		case 45: return "mm4";
2088cf781b2eSEd Maste 		case 46: return "mm5";
2089cf781b2eSEd Maste 		case 47: return "mm6";
2090cf781b2eSEd Maste 		case 48: return "mm7";
2091cf781b2eSEd Maste 		case 49: return "rflags";
2092cf781b2eSEd Maste 		case 50: return "es";
2093cf781b2eSEd Maste 		case 51: return "cs";
2094cf781b2eSEd Maste 		case 52: return "ss";
2095cf781b2eSEd Maste 		case 53: return "ds";
2096cf781b2eSEd Maste 		case 54: return "fs";
2097cf781b2eSEd Maste 		case 55: return "gs";
2098cf781b2eSEd Maste 		case 58: return "fs.base";
2099cf781b2eSEd Maste 		case 59: return "gs.base";
2100cf781b2eSEd Maste 		case 62: return "tr";
2101cf781b2eSEd Maste 		case 63: return "ldtr";
2102cf781b2eSEd Maste 		case 64: return "mxcsr";
2103cf781b2eSEd Maste 		case 65: return "fcw";
2104cf781b2eSEd Maste 		case 66: return "fsw";
2105cf781b2eSEd Maste 		default: return (NULL);
2106cf781b2eSEd Maste 		}
2107cf781b2eSEd Maste 	default:
2108cf781b2eSEd Maste 		return (NULL);
2109cf781b2eSEd Maste 	}
2110cf781b2eSEd Maste }
2111cf781b2eSEd Maste 
21122c23cb7cSEd Maste static void
21132c23cb7cSEd Maste dump_ehdr(struct readelf *re)
21142c23cb7cSEd Maste {
21152c23cb7cSEd Maste 	size_t		 shnum, shstrndx;
21162c23cb7cSEd Maste 	int		 i;
21172c23cb7cSEd Maste 
21182c23cb7cSEd Maste 	printf("ELF Header:\n");
21192c23cb7cSEd Maste 
21202c23cb7cSEd Maste 	/* e_ident[]. */
21212c23cb7cSEd Maste 	printf("  Magic:   ");
21222c23cb7cSEd Maste 	for (i = 0; i < EI_NIDENT; i++)
21232c23cb7cSEd Maste 		printf("%.2x ", re->ehdr.e_ident[i]);
21242c23cb7cSEd Maste 	putchar('\n');
21252c23cb7cSEd Maste 
21262c23cb7cSEd Maste 	/* EI_CLASS. */
21272c23cb7cSEd Maste 	printf("%-37s%s\n", "  Class:", elf_class(re->ehdr.e_ident[EI_CLASS]));
21282c23cb7cSEd Maste 
21292c23cb7cSEd Maste 	/* EI_DATA. */
21302c23cb7cSEd Maste 	printf("%-37s%s\n", "  Data:", elf_endian(re->ehdr.e_ident[EI_DATA]));
21312c23cb7cSEd Maste 
21322c23cb7cSEd Maste 	/* EI_VERSION. */
21332c23cb7cSEd Maste 	printf("%-37s%d %s\n", "  Version:", re->ehdr.e_ident[EI_VERSION],
21342c23cb7cSEd Maste 	    elf_ver(re->ehdr.e_ident[EI_VERSION]));
21352c23cb7cSEd Maste 
21362c23cb7cSEd Maste 	/* EI_OSABI. */
21372c23cb7cSEd Maste 	printf("%-37s%s\n", "  OS/ABI:", elf_osabi(re->ehdr.e_ident[EI_OSABI]));
21382c23cb7cSEd Maste 
21392c23cb7cSEd Maste 	/* EI_ABIVERSION. */
21402c23cb7cSEd Maste 	printf("%-37s%d\n", "  ABI Version:", re->ehdr.e_ident[EI_ABIVERSION]);
21412c23cb7cSEd Maste 
21422c23cb7cSEd Maste 	/* e_type. */
21432c23cb7cSEd Maste 	printf("%-37s%s\n", "  Type:", elf_type(re->ehdr.e_type));
21442c23cb7cSEd Maste 
21452c23cb7cSEd Maste 	/* e_machine. */
21462c23cb7cSEd Maste 	printf("%-37s%s\n", "  Machine:", elf_machine(re->ehdr.e_machine));
21472c23cb7cSEd Maste 
21482c23cb7cSEd Maste 	/* e_version. */
21492c23cb7cSEd Maste 	printf("%-37s%#x\n", "  Version:", re->ehdr.e_version);
21502c23cb7cSEd Maste 
21512c23cb7cSEd Maste 	/* e_entry. */
21522c23cb7cSEd Maste 	printf("%-37s%#jx\n", "  Entry point address:",
21532c23cb7cSEd Maste 	    (uintmax_t)re->ehdr.e_entry);
21542c23cb7cSEd Maste 
21552c23cb7cSEd Maste 	/* e_phoff. */
21562c23cb7cSEd Maste 	printf("%-37s%ju (bytes into file)\n", "  Start of program headers:",
21572c23cb7cSEd Maste 	    (uintmax_t)re->ehdr.e_phoff);
21582c23cb7cSEd Maste 
21592c23cb7cSEd Maste 	/* e_shoff. */
21602c23cb7cSEd Maste 	printf("%-37s%ju (bytes into file)\n", "  Start of section headers:",
21612c23cb7cSEd Maste 	    (uintmax_t)re->ehdr.e_shoff);
21622c23cb7cSEd Maste 
21632c23cb7cSEd Maste 	/* e_flags. */
21642c23cb7cSEd Maste 	printf("%-37s%#x", "  Flags:", re->ehdr.e_flags);
21652c23cb7cSEd Maste 	dump_eflags(re, re->ehdr.e_flags);
21662c23cb7cSEd Maste 	putchar('\n');
21672c23cb7cSEd Maste 
21682c23cb7cSEd Maste 	/* e_ehsize. */
21692c23cb7cSEd Maste 	printf("%-37s%u (bytes)\n", "  Size of this header:",
21702c23cb7cSEd Maste 	    re->ehdr.e_ehsize);
21712c23cb7cSEd Maste 
21722c23cb7cSEd Maste 	/* e_phentsize. */
21732c23cb7cSEd Maste 	printf("%-37s%u (bytes)\n", "  Size of program headers:",
21742c23cb7cSEd Maste 	    re->ehdr.e_phentsize);
21752c23cb7cSEd Maste 
21762c23cb7cSEd Maste 	/* e_phnum. */
21772c23cb7cSEd Maste 	printf("%-37s%u\n", "  Number of program headers:", re->ehdr.e_phnum);
21782c23cb7cSEd Maste 
21792c23cb7cSEd Maste 	/* e_shentsize. */
21802c23cb7cSEd Maste 	printf("%-37s%u (bytes)\n", "  Size of section headers:",
21812c23cb7cSEd Maste 	    re->ehdr.e_shentsize);
21822c23cb7cSEd Maste 
21832c23cb7cSEd Maste 	/* e_shnum. */
21842c23cb7cSEd Maste 	printf("%-37s%u", "  Number of section headers:", re->ehdr.e_shnum);
21852c23cb7cSEd Maste 	if (re->ehdr.e_shnum == SHN_UNDEF) {
21862c23cb7cSEd Maste 		/* Extended section numbering is in use. */
21872c23cb7cSEd Maste 		if (elf_getshnum(re->elf, &shnum))
21882c23cb7cSEd Maste 			printf(" (%ju)", (uintmax_t)shnum);
21892c23cb7cSEd Maste 	}
21902c23cb7cSEd Maste 	putchar('\n');
21912c23cb7cSEd Maste 
21922c23cb7cSEd Maste 	/* e_shstrndx. */
21932c23cb7cSEd Maste 	printf("%-37s%u", "  Section header string table index:",
21942c23cb7cSEd Maste 	    re->ehdr.e_shstrndx);
21952c23cb7cSEd Maste 	if (re->ehdr.e_shstrndx == SHN_XINDEX) {
21962c23cb7cSEd Maste 		/* Extended section numbering is in use. */
21972c23cb7cSEd Maste 		if (elf_getshstrndx(re->elf, &shstrndx))
21982c23cb7cSEd Maste 			printf(" (%ju)", (uintmax_t)shstrndx);
21992c23cb7cSEd Maste 	}
22002c23cb7cSEd Maste 	putchar('\n');
22012c23cb7cSEd Maste }
22022c23cb7cSEd Maste 
22032c23cb7cSEd Maste static void
22042c23cb7cSEd Maste dump_eflags(struct readelf *re, uint64_t e_flags)
22052c23cb7cSEd Maste {
22062c23cb7cSEd Maste 	struct eflags_desc *edesc;
22072c23cb7cSEd Maste 	int arm_eabi;
22082c23cb7cSEd Maste 
22092c23cb7cSEd Maste 	edesc = NULL;
22102c23cb7cSEd Maste 	switch (re->ehdr.e_machine) {
22112c23cb7cSEd Maste 	case EM_ARM:
22122c23cb7cSEd Maste 		arm_eabi = (e_flags & EF_ARM_EABIMASK) >> 24;
22132c23cb7cSEd Maste 		if (arm_eabi == 0)
22142c23cb7cSEd Maste 			printf(", GNU EABI");
22152c23cb7cSEd Maste 		else if (arm_eabi <= 5)
22162c23cb7cSEd Maste 			printf(", Version%d EABI", arm_eabi);
22172c23cb7cSEd Maste 		edesc = arm_eflags_desc;
22182c23cb7cSEd Maste 		break;
22192c23cb7cSEd Maste 	case EM_MIPS:
22202c23cb7cSEd Maste 	case EM_MIPS_RS3_LE:
22212c23cb7cSEd Maste 		switch ((e_flags & EF_MIPS_ARCH) >> 28) {
22222c23cb7cSEd Maste 		case 0:	printf(", mips1"); break;
22232c23cb7cSEd Maste 		case 1: printf(", mips2"); break;
22242c23cb7cSEd Maste 		case 2: printf(", mips3"); break;
22252c23cb7cSEd Maste 		case 3: printf(", mips4"); break;
22262c23cb7cSEd Maste 		case 4: printf(", mips5"); break;
22272c23cb7cSEd Maste 		case 5: printf(", mips32"); break;
22282c23cb7cSEd Maste 		case 6: printf(", mips64"); break;
22292c23cb7cSEd Maste 		case 7: printf(", mips32r2"); break;
22302c23cb7cSEd Maste 		case 8: printf(", mips64r2"); break;
22312c23cb7cSEd Maste 		default: break;
22322c23cb7cSEd Maste 		}
22332c23cb7cSEd Maste 		switch ((e_flags & 0x00FF0000) >> 16) {
22342c23cb7cSEd Maste 		case 0x81: printf(", 3900"); break;
22352c23cb7cSEd Maste 		case 0x82: printf(", 4010"); break;
22362c23cb7cSEd Maste 		case 0x83: printf(", 4100"); break;
22372c23cb7cSEd Maste 		case 0x85: printf(", 4650"); break;
22382c23cb7cSEd Maste 		case 0x87: printf(", 4120"); break;
22392c23cb7cSEd Maste 		case 0x88: printf(", 4111"); break;
22402c23cb7cSEd Maste 		case 0x8a: printf(", sb1"); break;
22412c23cb7cSEd Maste 		case 0x8b: printf(", octeon"); break;
22422c23cb7cSEd Maste 		case 0x8c: printf(", xlr"); break;
22432c23cb7cSEd Maste 		case 0x91: printf(", 5400"); break;
22442c23cb7cSEd Maste 		case 0x98: printf(", 5500"); break;
22452c23cb7cSEd Maste 		case 0x99: printf(", 9000"); break;
22462c23cb7cSEd Maste 		case 0xa0: printf(", loongson-2e"); break;
22472c23cb7cSEd Maste 		case 0xa1: printf(", loongson-2f"); break;
22482c23cb7cSEd Maste 		default: break;
22492c23cb7cSEd Maste 		}
22502c23cb7cSEd Maste 		switch ((e_flags & 0x0000F000) >> 12) {
22512c23cb7cSEd Maste 		case 1: printf(", o32"); break;
22522c23cb7cSEd Maste 		case 2: printf(", o64"); break;
22532c23cb7cSEd Maste 		case 3: printf(", eabi32"); break;
22542c23cb7cSEd Maste 		case 4: printf(", eabi64"); break;
22552c23cb7cSEd Maste 		default: break;
22562c23cb7cSEd Maste 		}
22572c23cb7cSEd Maste 		edesc = mips_eflags_desc;
22582c23cb7cSEd Maste 		break;
22592c23cb7cSEd Maste 	case EM_PPC:
22602c23cb7cSEd Maste 	case EM_PPC64:
22612c23cb7cSEd Maste 		edesc = powerpc_eflags_desc;
22622c23cb7cSEd Maste 		break;
22632c23cb7cSEd Maste 	case EM_SPARC:
22642c23cb7cSEd Maste 	case EM_SPARC32PLUS:
22652c23cb7cSEd Maste 	case EM_SPARCV9:
22662c23cb7cSEd Maste 		switch ((e_flags & EF_SPARCV9_MM)) {
22672c23cb7cSEd Maste 		case EF_SPARCV9_TSO: printf(", tso"); break;
22682c23cb7cSEd Maste 		case EF_SPARCV9_PSO: printf(", pso"); break;
22692c23cb7cSEd Maste 		case EF_SPARCV9_MM: printf(", rmo"); break;
22702c23cb7cSEd Maste 		default: break;
22712c23cb7cSEd Maste 		}
22722c23cb7cSEd Maste 		edesc = sparc_eflags_desc;
22732c23cb7cSEd Maste 		break;
22742c23cb7cSEd Maste 	default:
22752c23cb7cSEd Maste 		break;
22762c23cb7cSEd Maste 	}
22772c23cb7cSEd Maste 
22782c23cb7cSEd Maste 	if (edesc != NULL) {
22792c23cb7cSEd Maste 		while (edesc->desc != NULL) {
22802c23cb7cSEd Maste 			if (e_flags & edesc->flag)
22812c23cb7cSEd Maste 				printf(", %s", edesc->desc);
22822c23cb7cSEd Maste 			edesc++;
22832c23cb7cSEd Maste 		}
22842c23cb7cSEd Maste 	}
22852c23cb7cSEd Maste }
22862c23cb7cSEd Maste 
22872c23cb7cSEd Maste static void
22882c23cb7cSEd Maste dump_phdr(struct readelf *re)
22892c23cb7cSEd Maste {
22902c23cb7cSEd Maste 	const char	*rawfile;
22912c23cb7cSEd Maste 	GElf_Phdr	 phdr;
2292b00fe64fSEd Maste 	size_t		 phnum, size;
22932c23cb7cSEd Maste 	int		 i, j;
22942c23cb7cSEd Maste 
22952c23cb7cSEd Maste #define	PH_HDR	"Type", "Offset", "VirtAddr", "PhysAddr", "FileSiz",	\
22962c23cb7cSEd Maste 		"MemSiz", "Flg", "Align"
229720136ffcSEd Maste #define	PH_CT	phdr_type(re->ehdr.e_machine, phdr.p_type),		\
229820136ffcSEd Maste 		(uintmax_t)phdr.p_offset, (uintmax_t)phdr.p_vaddr,	\
229920136ffcSEd Maste 		(uintmax_t)phdr.p_paddr, (uintmax_t)phdr.p_filesz,	\
230020136ffcSEd Maste 		(uintmax_t)phdr.p_memsz,				\
23012c23cb7cSEd Maste 		phdr.p_flags & PF_R ? 'R' : ' ',			\
23022c23cb7cSEd Maste 		phdr.p_flags & PF_W ? 'W' : ' ',			\
23032c23cb7cSEd Maste 		phdr.p_flags & PF_X ? 'E' : ' ',			\
23042c23cb7cSEd Maste 		(uintmax_t)phdr.p_align
23052c23cb7cSEd Maste 
23062c23cb7cSEd Maste 	if (elf_getphnum(re->elf, &phnum) == 0) {
23072c23cb7cSEd Maste 		warnx("elf_getphnum failed: %s", elf_errmsg(-1));
23082c23cb7cSEd Maste 		return;
23092c23cb7cSEd Maste 	}
23102c23cb7cSEd Maste 	if (phnum == 0) {
23112c23cb7cSEd Maste 		printf("\nThere are no program headers in this file.\n");
23122c23cb7cSEd Maste 		return;
23132c23cb7cSEd Maste 	}
23142c23cb7cSEd Maste 
23152c23cb7cSEd Maste 	printf("\nElf file type is %s", elf_type(re->ehdr.e_type));
23162c23cb7cSEd Maste 	printf("\nEntry point 0x%jx\n", (uintmax_t)re->ehdr.e_entry);
23172c23cb7cSEd Maste 	printf("There are %ju program headers, starting at offset %ju\n",
23182c23cb7cSEd Maste 	    (uintmax_t)phnum, (uintmax_t)re->ehdr.e_phoff);
23192c23cb7cSEd Maste 
23202c23cb7cSEd Maste 	/* Dump program headers. */
23212c23cb7cSEd Maste 	printf("\nProgram Headers:\n");
23222c23cb7cSEd Maste 	if (re->ec == ELFCLASS32)
23232c23cb7cSEd Maste 		printf("  %-15s%-9s%-11s%-11s%-8s%-8s%-4s%s\n", PH_HDR);
23242c23cb7cSEd Maste 	else if (re->options & RE_WW)
23252c23cb7cSEd Maste 		printf("  %-15s%-9s%-19s%-19s%-9s%-9s%-4s%s\n", PH_HDR);
23262c23cb7cSEd Maste 	else
23272c23cb7cSEd Maste 		printf("  %-15s%-19s%-19s%s\n                 %-19s%-20s"
23282c23cb7cSEd Maste 		    "%-7s%s\n", PH_HDR);
23292c23cb7cSEd Maste 	for (i = 0; (size_t) i < phnum; i++) {
23302c23cb7cSEd Maste 		if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
23312c23cb7cSEd Maste 			warnx("gelf_getphdr failed: %s", elf_errmsg(-1));
23322c23cb7cSEd Maste 			continue;
23332c23cb7cSEd Maste 		}
23342c23cb7cSEd Maste 		/* TODO: Add arch-specific segment type dump. */
23352c23cb7cSEd Maste 		if (re->ec == ELFCLASS32)
23362c23cb7cSEd Maste 			printf("  %-14.14s 0x%6.6jx 0x%8.8jx 0x%8.8jx "
23372c23cb7cSEd Maste 			    "0x%5.5jx 0x%5.5jx %c%c%c %#jx\n", PH_CT);
23382c23cb7cSEd Maste 		else if (re->options & RE_WW)
23392c23cb7cSEd Maste 			printf("  %-14.14s 0x%6.6jx 0x%16.16jx 0x%16.16jx "
23402c23cb7cSEd Maste 			    "0x%6.6jx 0x%6.6jx %c%c%c %#jx\n", PH_CT);
23412c23cb7cSEd Maste 		else
23422c23cb7cSEd Maste 			printf("  %-14.14s 0x%16.16jx 0x%16.16jx 0x%16.16jx\n"
23432c23cb7cSEd Maste 			    "                 0x%16.16jx 0x%16.16jx  %c%c%c"
23442c23cb7cSEd Maste 			    "    %#jx\n", PH_CT);
23452c23cb7cSEd Maste 		if (phdr.p_type == PT_INTERP) {
2346b00fe64fSEd Maste 			if ((rawfile = elf_rawfile(re->elf, &size)) == NULL) {
23472c23cb7cSEd Maste 				warnx("elf_rawfile failed: %s", elf_errmsg(-1));
23482c23cb7cSEd Maste 				continue;
23492c23cb7cSEd Maste 			}
2350b00fe64fSEd Maste 			if (phdr.p_offset >= size) {
2351b00fe64fSEd Maste 				warnx("invalid program header offset");
2352b00fe64fSEd Maste 				continue;
2353b00fe64fSEd Maste 			}
23542c23cb7cSEd Maste 			printf("      [Requesting program interpreter: %s]\n",
23552c23cb7cSEd Maste 				rawfile + phdr.p_offset);
23562c23cb7cSEd Maste 		}
23572c23cb7cSEd Maste 	}
23582c23cb7cSEd Maste 
23592c23cb7cSEd Maste 	/* Dump section to segment mapping. */
23602c23cb7cSEd Maste 	if (re->shnum == 0)
23612c23cb7cSEd Maste 		return;
23622c23cb7cSEd Maste 	printf("\n Section to Segment mapping:\n");
23632c23cb7cSEd Maste 	printf("  Segment Sections...\n");
23642c23cb7cSEd Maste 	for (i = 0; (size_t)i < phnum; i++) {
23652c23cb7cSEd Maste 		if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
23662c23cb7cSEd Maste 			warnx("gelf_getphdr failed: %s", elf_errmsg(-1));
23672c23cb7cSEd Maste 			continue;
23682c23cb7cSEd Maste 		}
23692c23cb7cSEd Maste 		printf("   %2.2d     ", i);
23702c23cb7cSEd Maste 		/* skip NULL section. */
23712c23cb7cSEd Maste 		for (j = 1; (size_t)j < re->shnum; j++)
237295fd7f26SEd Maste 			if (re->sl[j].addr >= phdr.p_vaddr &&
237395fd7f26SEd Maste 			    re->sl[j].addr + re->sl[j].sz <=
237495fd7f26SEd Maste 			    phdr.p_vaddr + phdr.p_memsz)
23752c23cb7cSEd Maste 				printf("%s ", re->sl[j].name);
23762c23cb7cSEd Maste 		printf("\n");
23772c23cb7cSEd Maste 	}
23782c23cb7cSEd Maste #undef	PH_HDR
23792c23cb7cSEd Maste #undef	PH_CT
23802c23cb7cSEd Maste }
23812c23cb7cSEd Maste 
23822c23cb7cSEd Maste static char *
23832c23cb7cSEd Maste section_flags(struct readelf *re, struct section *s)
23842c23cb7cSEd Maste {
23852c23cb7cSEd Maste #define BUF_SZ 256
23862c23cb7cSEd Maste 	static char	buf[BUF_SZ];
23872c23cb7cSEd Maste 	int		i, p, nb;
23882c23cb7cSEd Maste 
23892c23cb7cSEd Maste 	p = 0;
23902c23cb7cSEd Maste 	nb = re->ec == ELFCLASS32 ? 8 : 16;
23912c23cb7cSEd Maste 	if (re->options & RE_T) {
23922c23cb7cSEd Maste 		snprintf(buf, BUF_SZ, "[%*.*jx]: ", nb, nb,
23932c23cb7cSEd Maste 		    (uintmax_t)s->flags);
23942c23cb7cSEd Maste 		p += nb + 4;
23952c23cb7cSEd Maste 	}
23962c23cb7cSEd Maste 	for (i = 0; section_flag[i].ln != NULL; i++) {
23972c23cb7cSEd Maste 		if ((s->flags & section_flag[i].value) == 0)
23982c23cb7cSEd Maste 			continue;
23992c23cb7cSEd Maste 		if (re->options & RE_T) {
24002c23cb7cSEd Maste 			snprintf(&buf[p], BUF_SZ - p, "%s, ",
24012c23cb7cSEd Maste 			    section_flag[i].ln);
24022c23cb7cSEd Maste 			p += strlen(section_flag[i].ln) + 2;
24032c23cb7cSEd Maste 		} else
24042c23cb7cSEd Maste 			buf[p++] = section_flag[i].sn;
24052c23cb7cSEd Maste 	}
24062c23cb7cSEd Maste 	if (re->options & RE_T && p > nb + 4)
24072c23cb7cSEd Maste 		p -= 2;
24082c23cb7cSEd Maste 	buf[p] = '\0';
24092c23cb7cSEd Maste 
24102c23cb7cSEd Maste 	return (buf);
24112c23cb7cSEd Maste }
24122c23cb7cSEd Maste 
24132c23cb7cSEd Maste static void
24142c23cb7cSEd Maste dump_shdr(struct readelf *re)
24152c23cb7cSEd Maste {
24162c23cb7cSEd Maste 	struct section	*s;
24172c23cb7cSEd Maste 	int		 i;
24182c23cb7cSEd Maste 
24192c23cb7cSEd Maste #define	S_HDR	"[Nr] Name", "Type", "Addr", "Off", "Size", "ES",	\
24202c23cb7cSEd Maste 		"Flg", "Lk", "Inf", "Al"
24212c23cb7cSEd Maste #define	S_HDRL	"[Nr] Name", "Type", "Address", "Offset", "Size",	\
24222c23cb7cSEd Maste 		"EntSize", "Flags", "Link", "Info", "Align"
24232c23cb7cSEd Maste #define	ST_HDR	"[Nr] Name", "Type", "Addr", "Off", "Size", "ES",	\
24242c23cb7cSEd Maste 		"Lk", "Inf", "Al", "Flags"
24252c23cb7cSEd Maste #define	ST_HDRL	"[Nr] Name", "Type", "Address", "Offset", "Link",	\
24262c23cb7cSEd Maste 		"Size", "EntSize", "Info", "Align", "Flags"
24272c23cb7cSEd Maste #define	S_CT	i, s->name, section_type(re->ehdr.e_machine, s->type),	\
24282c23cb7cSEd Maste 		(uintmax_t)s->addr, (uintmax_t)s->off, (uintmax_t)s->sz,\
24292c23cb7cSEd Maste 		(uintmax_t)s->entsize, section_flags(re, s),		\
24302c23cb7cSEd Maste 		s->link, s->info, (uintmax_t)s->align
24312c23cb7cSEd Maste #define	ST_CT	i, s->name, section_type(re->ehdr.e_machine, s->type),  \
24322c23cb7cSEd Maste 		(uintmax_t)s->addr, (uintmax_t)s->off, (uintmax_t)s->sz,\
24332c23cb7cSEd Maste 		(uintmax_t)s->entsize, s->link, s->info,		\
24342c23cb7cSEd Maste 		(uintmax_t)s->align, section_flags(re, s)
24352c23cb7cSEd Maste #define	ST_CTL	i, s->name, section_type(re->ehdr.e_machine, s->type),  \
24362c23cb7cSEd Maste 		(uintmax_t)s->addr, (uintmax_t)s->off, s->link,		\
24372c23cb7cSEd Maste 		(uintmax_t)s->sz, (uintmax_t)s->entsize, s->info,	\
24382c23cb7cSEd Maste 		(uintmax_t)s->align, section_flags(re, s)
24392c23cb7cSEd Maste 
24402c23cb7cSEd Maste 	if (re->shnum == 0) {
24412c23cb7cSEd Maste 		printf("\nThere are no sections in this file.\n");
24422c23cb7cSEd Maste 		return;
24432c23cb7cSEd Maste 	}
24442c23cb7cSEd Maste 	printf("There are %ju section headers, starting at offset 0x%jx:\n",
24452c23cb7cSEd Maste 	    (uintmax_t)re->shnum, (uintmax_t)re->ehdr.e_shoff);
24462c23cb7cSEd Maste 	printf("\nSection Headers:\n");
24472c23cb7cSEd Maste 	if (re->ec == ELFCLASS32) {
24482c23cb7cSEd Maste 		if (re->options & RE_T)
24492c23cb7cSEd Maste 			printf("  %s\n       %-16s%-9s%-7s%-7s%-5s%-3s%-4s%s\n"
24502c23cb7cSEd Maste 			    "%12s\n", ST_HDR);
24512c23cb7cSEd Maste 		else
24522c23cb7cSEd Maste 			printf("  %-23s%-16s%-9s%-7s%-7s%-3s%-4s%-3s%-4s%s\n",
24532c23cb7cSEd Maste 			    S_HDR);
24542c23cb7cSEd Maste 	} else if (re->options & RE_WW) {
24552c23cb7cSEd Maste 		if (re->options & RE_T)
24562c23cb7cSEd Maste 			printf("  %s\n       %-16s%-17s%-7s%-7s%-5s%-3s%-4s%s\n"
24572c23cb7cSEd Maste 			    "%12s\n", ST_HDR);
24582c23cb7cSEd Maste 		else
24592c23cb7cSEd Maste 			printf("  %-23s%-16s%-17s%-7s%-7s%-3s%-4s%-3s%-4s%s\n",
24602c23cb7cSEd Maste 			    S_HDR);
24612c23cb7cSEd Maste 	} else {
24622c23cb7cSEd Maste 		if (re->options & RE_T)
24632c23cb7cSEd Maste 			printf("  %s\n       %-18s%-17s%-18s%s\n       %-18s"
24642c23cb7cSEd Maste 			    "%-17s%-18s%s\n%12s\n", ST_HDRL);
24652c23cb7cSEd Maste 		else
24662c23cb7cSEd Maste 			printf("  %-23s%-17s%-18s%s\n       %-18s%-17s%-7s%"
24672c23cb7cSEd Maste 			    "-6s%-6s%s\n", S_HDRL);
24682c23cb7cSEd Maste 	}
24692c23cb7cSEd Maste 	for (i = 0; (size_t)i < re->shnum; i++) {
24702c23cb7cSEd Maste 		s = &re->sl[i];
24712c23cb7cSEd Maste 		if (re->ec == ELFCLASS32) {
24722c23cb7cSEd Maste 			if (re->options & RE_T)
24732c23cb7cSEd Maste 				printf("  [%2d] %s\n       %-15.15s %8.8jx"
24742c23cb7cSEd Maste 				    " %6.6jx %6.6jx %2.2jx  %2u %3u %2ju\n"
24752c23cb7cSEd Maste 				    "       %s\n", ST_CT);
24762c23cb7cSEd Maste 			else
24772c23cb7cSEd Maste 				printf("  [%2d] %-17.17s %-15.15s %8.8jx"
24782c23cb7cSEd Maste 				    " %6.6jx %6.6jx %2.2jx %3s %2u %3u %2ju\n",
24792c23cb7cSEd Maste 				    S_CT);
24802c23cb7cSEd Maste 		} else if (re->options & RE_WW) {
24812c23cb7cSEd Maste 			if (re->options & RE_T)
24822c23cb7cSEd Maste 				printf("  [%2d] %s\n       %-15.15s %16.16jx"
24832c23cb7cSEd Maste 				    " %6.6jx %6.6jx %2.2jx  %2u %3u %2ju\n"
24842c23cb7cSEd Maste 				    "       %s\n", ST_CT);
24852c23cb7cSEd Maste 			else
24862c23cb7cSEd Maste 				printf("  [%2d] %-17.17s %-15.15s %16.16jx"
24872c23cb7cSEd Maste 				    " %6.6jx %6.6jx %2.2jx %3s %2u %3u %2ju\n",
24882c23cb7cSEd Maste 				    S_CT);
24892c23cb7cSEd Maste 		} else {
24902c23cb7cSEd Maste 			if (re->options & RE_T)
24912c23cb7cSEd Maste 				printf("  [%2d] %s\n       %-15.15s  %16.16jx"
24922c23cb7cSEd Maste 				    "  %16.16jx  %u\n       %16.16jx %16.16jx"
24932c23cb7cSEd Maste 				    "  %-16u  %ju\n       %s\n", ST_CTL);
24942c23cb7cSEd Maste 			else
24952c23cb7cSEd Maste 				printf("  [%2d] %-17.17s %-15.15s  %16.16jx"
24962c23cb7cSEd Maste 				    "  %8.8jx\n       %16.16jx  %16.16jx "
24972c23cb7cSEd Maste 				    "%3s      %2u   %3u     %ju\n", S_CT);
24982c23cb7cSEd Maste 		}
24992c23cb7cSEd Maste 	}
25002c23cb7cSEd Maste 	if ((re->options & RE_T) == 0)
25012c23cb7cSEd Maste 		printf("Key to Flags:\n  W (write), A (alloc),"
25022c23cb7cSEd Maste 		    " X (execute), M (merge), S (strings)\n"
25032c23cb7cSEd Maste 		    "  I (info), L (link order), G (group), x (unknown)\n"
25042c23cb7cSEd Maste 		    "  O (extra OS processing required)"
25052c23cb7cSEd Maste 		    " o (OS specific), p (processor specific)\n");
25062c23cb7cSEd Maste 
25072c23cb7cSEd Maste #undef	S_HDR
25082c23cb7cSEd Maste #undef	S_HDRL
25092c23cb7cSEd Maste #undef	ST_HDR
25102c23cb7cSEd Maste #undef	ST_HDRL
25112c23cb7cSEd Maste #undef	S_CT
25122c23cb7cSEd Maste #undef	ST_CT
25132c23cb7cSEd Maste #undef	ST_CTL
25142c23cb7cSEd Maste }
25152c23cb7cSEd Maste 
251671edbbfdSEd Maste /*
251771edbbfdSEd Maste  * Return number of entries in the given section. We'd prefer ent_count be a
251871edbbfdSEd Maste  * size_t *, but libelf APIs already use int for section indices.
251971edbbfdSEd Maste  */
252071edbbfdSEd Maste static int
252171edbbfdSEd Maste get_ent_count(struct section *s, int *ent_count)
252271edbbfdSEd Maste {
252371edbbfdSEd Maste 	if (s->entsize == 0) {
252471edbbfdSEd Maste 		warnx("section %s has entry size 0", s->name);
252571edbbfdSEd Maste 		return (0);
252671edbbfdSEd Maste 	} else if (s->sz / s->entsize > INT_MAX) {
252771edbbfdSEd Maste 		warnx("section %s has invalid section count", s->name);
252871edbbfdSEd Maste 		return (0);
252971edbbfdSEd Maste 	}
253071edbbfdSEd Maste 	*ent_count = (int)(s->sz / s->entsize);
253171edbbfdSEd Maste 	return (1);
253271edbbfdSEd Maste }
253371edbbfdSEd Maste 
25342c23cb7cSEd Maste static void
25352c23cb7cSEd Maste dump_dynamic(struct readelf *re)
25362c23cb7cSEd Maste {
25372c23cb7cSEd Maste 	GElf_Dyn	 dyn;
25382c23cb7cSEd Maste 	Elf_Data	*d;
25392c23cb7cSEd Maste 	struct section	*s;
25402c23cb7cSEd Maste 	int		 elferr, i, is_dynamic, j, jmax, nentries;
25412c23cb7cSEd Maste 
25422c23cb7cSEd Maste 	is_dynamic = 0;
25432c23cb7cSEd Maste 
25442c23cb7cSEd Maste 	for (i = 0; (size_t)i < re->shnum; i++) {
25452c23cb7cSEd Maste 		s = &re->sl[i];
25462c23cb7cSEd Maste 		if (s->type != SHT_DYNAMIC)
25472c23cb7cSEd Maste 			continue;
25482c23cb7cSEd Maste 		(void) elf_errno();
25492c23cb7cSEd Maste 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
25502c23cb7cSEd Maste 			elferr = elf_errno();
25512c23cb7cSEd Maste 			if (elferr != 0)
25522c23cb7cSEd Maste 				warnx("elf_getdata failed: %s", elf_errmsg(-1));
25532c23cb7cSEd Maste 			continue;
25542c23cb7cSEd Maste 		}
25552c23cb7cSEd Maste 		if (d->d_size <= 0)
25562c23cb7cSEd Maste 			continue;
25572c23cb7cSEd Maste 
25582c23cb7cSEd Maste 		is_dynamic = 1;
25592c23cb7cSEd Maste 
25602c23cb7cSEd Maste 		/* Determine the actual number of table entries. */
25612c23cb7cSEd Maste 		nentries = 0;
256271edbbfdSEd Maste 		if (!get_ent_count(s, &jmax))
256371edbbfdSEd Maste 			continue;
25642c23cb7cSEd Maste 		for (j = 0; j < jmax; j++) {
25652c23cb7cSEd Maste 			if (gelf_getdyn(d, j, &dyn) != &dyn) {
25662c23cb7cSEd Maste 				warnx("gelf_getdyn failed: %s",
25672c23cb7cSEd Maste 				    elf_errmsg(-1));
25682c23cb7cSEd Maste 				continue;
25692c23cb7cSEd Maste 			}
25702c23cb7cSEd Maste 			nentries ++;
25712c23cb7cSEd Maste 			if (dyn.d_tag == DT_NULL)
25722c23cb7cSEd Maste 				break;
25732c23cb7cSEd Maste                 }
25742c23cb7cSEd Maste 
25752c23cb7cSEd Maste 		printf("\nDynamic section at offset 0x%jx", (uintmax_t)s->off);
25762c23cb7cSEd Maste 		printf(" contains %u entries:\n", nentries);
25772c23cb7cSEd Maste 
25782c23cb7cSEd Maste 		if (re->ec == ELFCLASS32)
25792c23cb7cSEd Maste 			printf("%5s%12s%28s\n", "Tag", "Type", "Name/Value");
25802c23cb7cSEd Maste 		else
25812c23cb7cSEd Maste 			printf("%5s%20s%28s\n", "Tag", "Type", "Name/Value");
25822c23cb7cSEd Maste 
25832c23cb7cSEd Maste 		for (j = 0; j < nentries; j++) {
25842c23cb7cSEd Maste 			if (gelf_getdyn(d, j, &dyn) != &dyn)
25852c23cb7cSEd Maste 				continue;
25862c23cb7cSEd Maste 			/* Dump dynamic entry type. */
25872c23cb7cSEd Maste 			if (re->ec == ELFCLASS32)
25882c23cb7cSEd Maste 				printf(" 0x%8.8jx", (uintmax_t)dyn.d_tag);
25892c23cb7cSEd Maste 			else
25902c23cb7cSEd Maste 				printf(" 0x%16.16jx", (uintmax_t)dyn.d_tag);
25912c23cb7cSEd Maste 			printf(" %-20s", dt_type(re->ehdr.e_machine,
25922c23cb7cSEd Maste 			    dyn.d_tag));
25932c23cb7cSEd Maste 			/* Dump dynamic entry value. */
25942c23cb7cSEd Maste 			dump_dyn_val(re, &dyn, s->link);
25952c23cb7cSEd Maste 		}
25962c23cb7cSEd Maste 	}
25972c23cb7cSEd Maste 
25982c23cb7cSEd Maste 	if (!is_dynamic)
25992c23cb7cSEd Maste 		printf("\nThere is no dynamic section in this file.\n");
26002c23cb7cSEd Maste }
26012c23cb7cSEd Maste 
26022c23cb7cSEd Maste static char *
26032c23cb7cSEd Maste timestamp(time_t ti)
26042c23cb7cSEd Maste {
26052c23cb7cSEd Maste 	static char ts[32];
26062c23cb7cSEd Maste 	struct tm *t;
26072c23cb7cSEd Maste 
26082c23cb7cSEd Maste 	t = gmtime(&ti);
26092c23cb7cSEd Maste 	snprintf(ts, sizeof(ts), "%04d-%02d-%02dT%02d:%02d:%02d",
26102c23cb7cSEd Maste 	    t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour,
26112c23cb7cSEd Maste 	    t->tm_min, t->tm_sec);
26122c23cb7cSEd Maste 
26132c23cb7cSEd Maste 	return (ts);
26142c23cb7cSEd Maste }
26152c23cb7cSEd Maste 
26162c23cb7cSEd Maste static const char *
26172c23cb7cSEd Maste dyn_str(struct readelf *re, uint32_t stab, uint64_t d_val)
26182c23cb7cSEd Maste {
26192c23cb7cSEd Maste 	const char *name;
26202c23cb7cSEd Maste 
26212c23cb7cSEd Maste 	if (stab == SHN_UNDEF)
26222c23cb7cSEd Maste 		name = "ERROR";
26232c23cb7cSEd Maste 	else if ((name = elf_strptr(re->elf, stab, d_val)) == NULL) {
26242c23cb7cSEd Maste 		(void) elf_errno(); /* clear error */
26252c23cb7cSEd Maste 		name = "ERROR";
26262c23cb7cSEd Maste 	}
26272c23cb7cSEd Maste 
26282c23cb7cSEd Maste 	return (name);
26292c23cb7cSEd Maste }
26302c23cb7cSEd Maste 
26312c23cb7cSEd Maste static void
26322c23cb7cSEd Maste dump_arch_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab)
26332c23cb7cSEd Maste {
26342c23cb7cSEd Maste 	const char *name;
26352c23cb7cSEd Maste 
26362c23cb7cSEd Maste 	switch (re->ehdr.e_machine) {
26372c23cb7cSEd Maste 	case EM_MIPS:
26382c23cb7cSEd Maste 	case EM_MIPS_RS3_LE:
26392c23cb7cSEd Maste 		switch (dyn->d_tag) {
26402c23cb7cSEd Maste 		case DT_MIPS_RLD_VERSION:
26412c23cb7cSEd Maste 		case DT_MIPS_LOCAL_GOTNO:
26422c23cb7cSEd Maste 		case DT_MIPS_CONFLICTNO:
26432c23cb7cSEd Maste 		case DT_MIPS_LIBLISTNO:
26442c23cb7cSEd Maste 		case DT_MIPS_SYMTABNO:
26452c23cb7cSEd Maste 		case DT_MIPS_UNREFEXTNO:
26462c23cb7cSEd Maste 		case DT_MIPS_GOTSYM:
26472c23cb7cSEd Maste 		case DT_MIPS_HIPAGENO:
26482c23cb7cSEd Maste 		case DT_MIPS_DELTA_CLASS_NO:
26492c23cb7cSEd Maste 		case DT_MIPS_DELTA_INSTANCE_NO:
26502c23cb7cSEd Maste 		case DT_MIPS_DELTA_RELOC_NO:
26512c23cb7cSEd Maste 		case DT_MIPS_DELTA_SYM_NO:
26522c23cb7cSEd Maste 		case DT_MIPS_DELTA_CLASSSYM_NO:
26532c23cb7cSEd Maste 		case DT_MIPS_LOCALPAGE_GOTIDX:
26542c23cb7cSEd Maste 		case DT_MIPS_LOCAL_GOTIDX:
26552c23cb7cSEd Maste 		case DT_MIPS_HIDDEN_GOTIDX:
26562c23cb7cSEd Maste 		case DT_MIPS_PROTECTED_GOTIDX:
26572c23cb7cSEd Maste 			printf(" %ju\n", (uintmax_t) dyn->d_un.d_val);
26582c23cb7cSEd Maste 			break;
26592c23cb7cSEd Maste 		case DT_MIPS_ICHECKSUM:
26602c23cb7cSEd Maste 		case DT_MIPS_FLAGS:
26612c23cb7cSEd Maste 		case DT_MIPS_BASE_ADDRESS:
26622c23cb7cSEd Maste 		case DT_MIPS_CONFLICT:
26632c23cb7cSEd Maste 		case DT_MIPS_LIBLIST:
26642c23cb7cSEd Maste 		case DT_MIPS_RLD_MAP:
26652c23cb7cSEd Maste 		case DT_MIPS_DELTA_CLASS:
26662c23cb7cSEd Maste 		case DT_MIPS_DELTA_INSTANCE:
26672c23cb7cSEd Maste 		case DT_MIPS_DELTA_RELOC:
26682c23cb7cSEd Maste 		case DT_MIPS_DELTA_SYM:
26692c23cb7cSEd Maste 		case DT_MIPS_DELTA_CLASSSYM:
26702c23cb7cSEd Maste 		case DT_MIPS_CXX_FLAGS:
26712c23cb7cSEd Maste 		case DT_MIPS_PIXIE_INIT:
26722c23cb7cSEd Maste 		case DT_MIPS_SYMBOL_LIB:
26732c23cb7cSEd Maste 		case DT_MIPS_OPTIONS:
26742c23cb7cSEd Maste 		case DT_MIPS_INTERFACE:
26752c23cb7cSEd Maste 		case DT_MIPS_DYNSTR_ALIGN:
26762c23cb7cSEd Maste 		case DT_MIPS_INTERFACE_SIZE:
26772c23cb7cSEd Maste 		case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
26782c23cb7cSEd Maste 		case DT_MIPS_COMPACT_SIZE:
26792c23cb7cSEd Maste 		case DT_MIPS_GP_VALUE:
26802c23cb7cSEd Maste 		case DT_MIPS_AUX_DYNAMIC:
26812c23cb7cSEd Maste 		case DT_MIPS_PLTGOT:
26822c23cb7cSEd Maste 		case DT_MIPS_RLD_OBJ_UPDATE:
26832c23cb7cSEd Maste 		case DT_MIPS_RWPLT:
26842c23cb7cSEd Maste 			printf(" 0x%jx\n", (uintmax_t) dyn->d_un.d_val);
26852c23cb7cSEd Maste 			break;
26862c23cb7cSEd Maste 		case DT_MIPS_IVERSION:
26872c23cb7cSEd Maste 		case DT_MIPS_PERF_SUFFIX:
26882c23cb7cSEd Maste 		case DT_AUXILIARY:
26892c23cb7cSEd Maste 		case DT_FILTER:
26902c23cb7cSEd Maste 			name = dyn_str(re, stab, dyn->d_un.d_val);
26912c23cb7cSEd Maste 			printf(" %s\n", name);
26922c23cb7cSEd Maste 			break;
26932c23cb7cSEd Maste 		case DT_MIPS_TIME_STAMP:
26942c23cb7cSEd Maste 			printf(" %s\n", timestamp(dyn->d_un.d_val));
26952c23cb7cSEd Maste 			break;
26962c23cb7cSEd Maste 		}
26972c23cb7cSEd Maste 		break;
26982c23cb7cSEd Maste 	default:
26992c23cb7cSEd Maste 		printf("\n");
27002c23cb7cSEd Maste 		break;
27012c23cb7cSEd Maste 	}
27022c23cb7cSEd Maste }
27032c23cb7cSEd Maste 
27042c23cb7cSEd Maste static void
27052c23cb7cSEd Maste dump_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab)
27062c23cb7cSEd Maste {
27072c23cb7cSEd Maste 	const char *name;
27082c23cb7cSEd Maste 
27092c23cb7cSEd Maste 	if (dyn->d_tag >= DT_LOPROC && dyn->d_tag <= DT_HIPROC) {
27102c23cb7cSEd Maste 		dump_arch_dyn_val(re, dyn, stab);
27112c23cb7cSEd Maste 		return;
27122c23cb7cSEd Maste 	}
27132c23cb7cSEd Maste 
27142c23cb7cSEd Maste 	/* These entry values are index into the string table. */
27152c23cb7cSEd Maste 	name = NULL;
27162c23cb7cSEd Maste 	if (dyn->d_tag == DT_NEEDED || dyn->d_tag == DT_SONAME ||
27172c23cb7cSEd Maste 	    dyn->d_tag == DT_RPATH || dyn->d_tag == DT_RUNPATH)
27182c23cb7cSEd Maste 		name = dyn_str(re, stab, dyn->d_un.d_val);
27192c23cb7cSEd Maste 
27202c23cb7cSEd Maste 	switch(dyn->d_tag) {
27212c23cb7cSEd Maste 	case DT_NULL:
27222c23cb7cSEd Maste 	case DT_PLTGOT:
27232c23cb7cSEd Maste 	case DT_HASH:
27242c23cb7cSEd Maste 	case DT_STRTAB:
27252c23cb7cSEd Maste 	case DT_SYMTAB:
27262c23cb7cSEd Maste 	case DT_RELA:
27272c23cb7cSEd Maste 	case DT_INIT:
27282c23cb7cSEd Maste 	case DT_SYMBOLIC:
27292c23cb7cSEd Maste 	case DT_REL:
27302c23cb7cSEd Maste 	case DT_DEBUG:
27312c23cb7cSEd Maste 	case DT_TEXTREL:
27322c23cb7cSEd Maste 	case DT_JMPREL:
27332c23cb7cSEd Maste 	case DT_FINI:
27342c23cb7cSEd Maste 	case DT_VERDEF:
27352c23cb7cSEd Maste 	case DT_VERNEED:
27362c23cb7cSEd Maste 	case DT_VERSYM:
27372c23cb7cSEd Maste 	case DT_GNU_HASH:
27382c23cb7cSEd Maste 	case DT_GNU_LIBLIST:
27392c23cb7cSEd Maste 	case DT_GNU_CONFLICT:
27402c23cb7cSEd Maste 		printf(" 0x%jx\n", (uintmax_t) dyn->d_un.d_val);
27412c23cb7cSEd Maste 		break;
27422c23cb7cSEd Maste 	case DT_PLTRELSZ:
27432c23cb7cSEd Maste 	case DT_RELASZ:
27442c23cb7cSEd Maste 	case DT_RELAENT:
27452c23cb7cSEd Maste 	case DT_STRSZ:
27462c23cb7cSEd Maste 	case DT_SYMENT:
27472c23cb7cSEd Maste 	case DT_RELSZ:
27482c23cb7cSEd Maste 	case DT_RELENT:
27492c23cb7cSEd Maste 	case DT_INIT_ARRAYSZ:
27502c23cb7cSEd Maste 	case DT_FINI_ARRAYSZ:
27512c23cb7cSEd Maste 	case DT_GNU_CONFLICTSZ:
27522c23cb7cSEd Maste 	case DT_GNU_LIBLISTSZ:
27532c23cb7cSEd Maste 		printf(" %ju (bytes)\n", (uintmax_t) dyn->d_un.d_val);
27542c23cb7cSEd Maste 		break;
27552c23cb7cSEd Maste  	case DT_RELACOUNT:
27562c23cb7cSEd Maste 	case DT_RELCOUNT:
27572c23cb7cSEd Maste 	case DT_VERDEFNUM:
27582c23cb7cSEd Maste 	case DT_VERNEEDNUM:
27592c23cb7cSEd Maste 		printf(" %ju\n", (uintmax_t) dyn->d_un.d_val);
27602c23cb7cSEd Maste 		break;
27612c23cb7cSEd Maste 	case DT_NEEDED:
27622c23cb7cSEd Maste 		printf(" Shared library: [%s]\n", name);
27632c23cb7cSEd Maste 		break;
27642c23cb7cSEd Maste 	case DT_SONAME:
27652c23cb7cSEd Maste 		printf(" Library soname: [%s]\n", name);
27662c23cb7cSEd Maste 		break;
27672c23cb7cSEd Maste 	case DT_RPATH:
27682c23cb7cSEd Maste 		printf(" Library rpath: [%s]\n", name);
27692c23cb7cSEd Maste 		break;
27702c23cb7cSEd Maste 	case DT_RUNPATH:
27712c23cb7cSEd Maste 		printf(" Library runpath: [%s]\n", name);
27722c23cb7cSEd Maste 		break;
27732c23cb7cSEd Maste 	case DT_PLTREL:
27742c23cb7cSEd Maste 		printf(" %s\n", dt_type(re->ehdr.e_machine, dyn->d_un.d_val));
27752c23cb7cSEd Maste 		break;
27762c23cb7cSEd Maste 	case DT_GNU_PRELINKED:
27772c23cb7cSEd Maste 		printf(" %s\n", timestamp(dyn->d_un.d_val));
27782c23cb7cSEd Maste 		break;
27792c23cb7cSEd Maste 	default:
27802c23cb7cSEd Maste 		printf("\n");
27812c23cb7cSEd Maste 	}
27822c23cb7cSEd Maste }
27832c23cb7cSEd Maste 
27842c23cb7cSEd Maste static void
27852c23cb7cSEd Maste dump_rel(struct readelf *re, struct section *s, Elf_Data *d)
27862c23cb7cSEd Maste {
27872c23cb7cSEd Maste 	GElf_Rel r;
27882c23cb7cSEd Maste 	const char *symname;
27892c23cb7cSEd Maste 	uint64_t symval;
27902c23cb7cSEd Maste 	int i, len;
2791b6d812d2SEd Maste 	uint32_t type;
2792b6d812d2SEd Maste 	uint8_t type2, type3;
27932c23cb7cSEd Maste 
2794656f49f8SEd Maste 	if (s->link >= re->shnum)
2795656f49f8SEd Maste 		return;
2796656f49f8SEd Maste 
27972c23cb7cSEd Maste #define	REL_HDR "r_offset", "r_info", "r_type", "st_value", "st_name"
27982c23cb7cSEd Maste #define	REL_CT32 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
2799b6b6f9ccSEd Maste 		elftc_reloc_type_str(re->ehdr.e_machine,	    \
2800b6b6f9ccSEd Maste 		ELF32_R_TYPE(r.r_info)), (uintmax_t)symval, symname
28012c23cb7cSEd Maste #define	REL_CT64 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
2802b6d812d2SEd Maste 		elftc_reloc_type_str(re->ehdr.e_machine, type),	    \
2803b6d812d2SEd Maste 		(uintmax_t)symval, symname
28042c23cb7cSEd Maste 
28052c23cb7cSEd Maste 	printf("\nRelocation section (%s):\n", s->name);
28062c23cb7cSEd Maste 	if (re->ec == ELFCLASS32)
28072c23cb7cSEd Maste 		printf("%-8s %-8s %-19s %-8s %s\n", REL_HDR);
28082c23cb7cSEd Maste 	else {
28092c23cb7cSEd Maste 		if (re->options & RE_WW)
28102c23cb7cSEd Maste 			printf("%-16s %-16s %-24s %-16s %s\n", REL_HDR);
28112c23cb7cSEd Maste 		else
28122c23cb7cSEd Maste 			printf("%-12s %-12s %-19s %-16s %s\n", REL_HDR);
28132c23cb7cSEd Maste 	}
281471edbbfdSEd Maste 	assert(d->d_size == s->sz);
281571edbbfdSEd Maste 	if (!get_ent_count(s, &len))
281671edbbfdSEd Maste 		return;
28172c23cb7cSEd Maste 	for (i = 0; i < len; i++) {
28182c23cb7cSEd Maste 		if (gelf_getrel(d, i, &r) != &r) {
28192c23cb7cSEd Maste 			warnx("gelf_getrel failed: %s", elf_errmsg(-1));
28202c23cb7cSEd Maste 			continue;
28212c23cb7cSEd Maste 		}
28222c23cb7cSEd Maste 		symname = get_symbol_name(re, s->link, GELF_R_SYM(r.r_info));
28232c23cb7cSEd Maste 		symval = get_symbol_value(re, s->link, GELF_R_SYM(r.r_info));
28242c23cb7cSEd Maste 		if (re->ec == ELFCLASS32) {
28252c23cb7cSEd Maste 			r.r_info = ELF32_R_INFO(ELF64_R_SYM(r.r_info),
28262c23cb7cSEd Maste 			    ELF64_R_TYPE(r.r_info));
28272c23cb7cSEd Maste 			printf("%8.8jx %8.8jx %-19.19s %8.8jx %s\n", REL_CT32);
28282c23cb7cSEd Maste 		} else {
2829b6d812d2SEd Maste 			type = ELF64_R_TYPE(r.r_info);
2830b6d812d2SEd Maste 			if (re->ehdr.e_machine == EM_MIPS) {
2831b6d812d2SEd Maste 				type2 = (type >> 8) & 0xFF;
2832b6d812d2SEd Maste 				type3 = (type >> 16) & 0xFF;
2833b6d812d2SEd Maste 				type = type & 0xFF;
2834*d0bd2dadSEd Maste 			} else {
2835*d0bd2dadSEd Maste 				type2 = type3 = 0;
2836b6d812d2SEd Maste 			}
28372c23cb7cSEd Maste 			if (re->options & RE_WW)
28382c23cb7cSEd Maste 				printf("%16.16jx %16.16jx %-24.24s"
28392c23cb7cSEd Maste 				    " %16.16jx %s\n", REL_CT64);
28402c23cb7cSEd Maste 			else
28412c23cb7cSEd Maste 				printf("%12.12jx %12.12jx %-19.19s"
28422c23cb7cSEd Maste 				    " %16.16jx %s\n", REL_CT64);
2843b6d812d2SEd Maste 			if (re->ehdr.e_machine == EM_MIPS) {
2844b6d812d2SEd Maste 				if (re->options & RE_WW) {
2845b6d812d2SEd Maste 					printf("%32s: %s\n", "Type2",
2846b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
2847b6d812d2SEd Maste 					    type2));
2848b6d812d2SEd Maste 					printf("%32s: %s\n", "Type3",
2849b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
2850b6d812d2SEd Maste 					    type3));
2851b6d812d2SEd Maste 				} else {
2852b6d812d2SEd Maste 					printf("%24s: %s\n", "Type2",
2853b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
2854b6d812d2SEd Maste 					    type2));
2855b6d812d2SEd Maste 					printf("%24s: %s\n", "Type3",
2856b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
2857b6d812d2SEd Maste 					    type3));
2858b6d812d2SEd Maste 				}
2859b6d812d2SEd Maste 			}
28602c23cb7cSEd Maste 		}
28612c23cb7cSEd Maste 	}
28622c23cb7cSEd Maste 
28632c23cb7cSEd Maste #undef	REL_HDR
28642c23cb7cSEd Maste #undef	REL_CT
28652c23cb7cSEd Maste }
28662c23cb7cSEd Maste 
28672c23cb7cSEd Maste static void
28682c23cb7cSEd Maste dump_rela(struct readelf *re, struct section *s, Elf_Data *d)
28692c23cb7cSEd Maste {
28702c23cb7cSEd Maste 	GElf_Rela r;
28712c23cb7cSEd Maste 	const char *symname;
28722c23cb7cSEd Maste 	uint64_t symval;
28732c23cb7cSEd Maste 	int i, len;
2874b6d812d2SEd Maste 	uint32_t type;
2875b6d812d2SEd Maste 	uint8_t type2, type3;
28762c23cb7cSEd Maste 
2877656f49f8SEd Maste 	if (s->link >= re->shnum)
2878656f49f8SEd Maste 		return;
2879656f49f8SEd Maste 
28802c23cb7cSEd Maste #define	RELA_HDR "r_offset", "r_info", "r_type", "st_value", \
28812c23cb7cSEd Maste 		"st_name + r_addend"
28822c23cb7cSEd Maste #define	RELA_CT32 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
2883b6b6f9ccSEd Maste 		elftc_reloc_type_str(re->ehdr.e_machine,	    \
2884b6b6f9ccSEd Maste 		ELF32_R_TYPE(r.r_info)), (uintmax_t)symval, symname
28852c23cb7cSEd Maste #define	RELA_CT64 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
2886b6d812d2SEd Maste 		elftc_reloc_type_str(re->ehdr.e_machine, type),	    \
2887b6d812d2SEd Maste 		(uintmax_t)symval, symname
28882c23cb7cSEd Maste 
28892c23cb7cSEd Maste 	printf("\nRelocation section with addend (%s):\n", s->name);
28902c23cb7cSEd Maste 	if (re->ec == ELFCLASS32)
28912c23cb7cSEd Maste 		printf("%-8s %-8s %-19s %-8s %s\n", RELA_HDR);
28922c23cb7cSEd Maste 	else {
28932c23cb7cSEd Maste 		if (re->options & RE_WW)
28942c23cb7cSEd Maste 			printf("%-16s %-16s %-24s %-16s %s\n", RELA_HDR);
28952c23cb7cSEd Maste 		else
28962c23cb7cSEd Maste 			printf("%-12s %-12s %-19s %-16s %s\n", RELA_HDR);
28972c23cb7cSEd Maste 	}
289871edbbfdSEd Maste 	assert(d->d_size == s->sz);
289971edbbfdSEd Maste 	if (!get_ent_count(s, &len))
290071edbbfdSEd Maste 		return;
29012c23cb7cSEd Maste 	for (i = 0; i < len; i++) {
29022c23cb7cSEd Maste 		if (gelf_getrela(d, i, &r) != &r) {
29032c23cb7cSEd Maste 			warnx("gelf_getrel failed: %s", elf_errmsg(-1));
29042c23cb7cSEd Maste 			continue;
29052c23cb7cSEd Maste 		}
29062c23cb7cSEd Maste 		symname = get_symbol_name(re, s->link, GELF_R_SYM(r.r_info));
29072c23cb7cSEd Maste 		symval = get_symbol_value(re, s->link, GELF_R_SYM(r.r_info));
29082c23cb7cSEd Maste 		if (re->ec == ELFCLASS32) {
29092c23cb7cSEd Maste 			r.r_info = ELF32_R_INFO(ELF64_R_SYM(r.r_info),
29102c23cb7cSEd Maste 			    ELF64_R_TYPE(r.r_info));
29112c23cb7cSEd Maste 			printf("%8.8jx %8.8jx %-19.19s %8.8jx %s", RELA_CT32);
29122c23cb7cSEd Maste 			printf(" + %x\n", (uint32_t) r.r_addend);
29132c23cb7cSEd Maste 		} else {
2914b6d812d2SEd Maste 			type = ELF64_R_TYPE(r.r_info);
2915b6d812d2SEd Maste 			if (re->ehdr.e_machine == EM_MIPS) {
2916b6d812d2SEd Maste 				type2 = (type >> 8) & 0xFF;
2917b6d812d2SEd Maste 				type3 = (type >> 16) & 0xFF;
2918b6d812d2SEd Maste 				type = type & 0xFF;
2919*d0bd2dadSEd Maste 			} else {
2920*d0bd2dadSEd Maste 				type2 = type3 = 0;
2921b6d812d2SEd Maste 			}
29222c23cb7cSEd Maste 			if (re->options & RE_WW)
29232c23cb7cSEd Maste 				printf("%16.16jx %16.16jx %-24.24s"
29242c23cb7cSEd Maste 				    " %16.16jx %s", RELA_CT64);
29252c23cb7cSEd Maste 			else
29262c23cb7cSEd Maste 				printf("%12.12jx %12.12jx %-19.19s"
29272c23cb7cSEd Maste 				    " %16.16jx %s", RELA_CT64);
29282c23cb7cSEd Maste 			printf(" + %jx\n", (uintmax_t) r.r_addend);
2929b6d812d2SEd Maste 			if (re->ehdr.e_machine == EM_MIPS) {
2930b6d812d2SEd Maste 				if (re->options & RE_WW) {
2931b6d812d2SEd Maste 					printf("%32s: %s\n", "Type2",
2932b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
2933b6d812d2SEd Maste 					    type2));
2934b6d812d2SEd Maste 					printf("%32s: %s\n", "Type3",
2935b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
2936b6d812d2SEd Maste 					    type3));
2937b6d812d2SEd Maste 				} else {
2938b6d812d2SEd Maste 					printf("%24s: %s\n", "Type2",
2939b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
2940b6d812d2SEd Maste 					    type2));
2941b6d812d2SEd Maste 					printf("%24s: %s\n", "Type3",
2942b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
2943b6d812d2SEd Maste 					    type3));
2944b6d812d2SEd Maste 				}
2945b6d812d2SEd Maste 			}
29462c23cb7cSEd Maste 		}
29472c23cb7cSEd Maste 	}
29482c23cb7cSEd Maste 
29492c23cb7cSEd Maste #undef	RELA_HDR
29502c23cb7cSEd Maste #undef	RELA_CT
29512c23cb7cSEd Maste }
29522c23cb7cSEd Maste 
29532c23cb7cSEd Maste static void
29542c23cb7cSEd Maste dump_reloc(struct readelf *re)
29552c23cb7cSEd Maste {
29562c23cb7cSEd Maste 	struct section *s;
29572c23cb7cSEd Maste 	Elf_Data *d;
29582c23cb7cSEd Maste 	int i, elferr;
29592c23cb7cSEd Maste 
29602c23cb7cSEd Maste 	for (i = 0; (size_t)i < re->shnum; i++) {
29612c23cb7cSEd Maste 		s = &re->sl[i];
29622c23cb7cSEd Maste 		if (s->type == SHT_REL || s->type == SHT_RELA) {
29632c23cb7cSEd Maste 			(void) elf_errno();
29642c23cb7cSEd Maste 			if ((d = elf_getdata(s->scn, NULL)) == NULL) {
29652c23cb7cSEd Maste 				elferr = elf_errno();
29662c23cb7cSEd Maste 				if (elferr != 0)
29672c23cb7cSEd Maste 					warnx("elf_getdata failed: %s",
29682c23cb7cSEd Maste 					    elf_errmsg(elferr));
29692c23cb7cSEd Maste 				continue;
29702c23cb7cSEd Maste 			}
29712c23cb7cSEd Maste 			if (s->type == SHT_REL)
29722c23cb7cSEd Maste 				dump_rel(re, s, d);
29732c23cb7cSEd Maste 			else
29742c23cb7cSEd Maste 				dump_rela(re, s, d);
29752c23cb7cSEd Maste 		}
29762c23cb7cSEd Maste 	}
29772c23cb7cSEd Maste }
29782c23cb7cSEd Maste 
29792c23cb7cSEd Maste static void
29802c23cb7cSEd Maste dump_symtab(struct readelf *re, int i)
29812c23cb7cSEd Maste {
29822c23cb7cSEd Maste 	struct section *s;
29832c23cb7cSEd Maste 	Elf_Data *d;
29842c23cb7cSEd Maste 	GElf_Sym sym;
29852c23cb7cSEd Maste 	const char *name;
2986656f49f8SEd Maste 	uint32_t stab;
2987656f49f8SEd Maste 	int elferr, j, len;
2988656f49f8SEd Maste 	uint16_t vs;
29892c23cb7cSEd Maste 
29902c23cb7cSEd Maste 	s = &re->sl[i];
2991656f49f8SEd Maste 	if (s->link >= re->shnum)
2992656f49f8SEd Maste 		return;
29932c23cb7cSEd Maste 	stab = s->link;
29942c23cb7cSEd Maste 	(void) elf_errno();
29952c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
29962c23cb7cSEd Maste 		elferr = elf_errno();
29972c23cb7cSEd Maste 		if (elferr != 0)
29982c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
29992c23cb7cSEd Maste 		return;
30002c23cb7cSEd Maste 	}
30012c23cb7cSEd Maste 	if (d->d_size <= 0)
30022c23cb7cSEd Maste 		return;
300371edbbfdSEd Maste 	if (!get_ent_count(s, &len))
300471edbbfdSEd Maste 		return;
30052c23cb7cSEd Maste 	printf("Symbol table (%s)", s->name);
300671edbbfdSEd Maste 	printf(" contains %d entries:\n", len);
30072c23cb7cSEd Maste 	printf("%7s%9s%14s%5s%8s%6s%9s%5s\n", "Num:", "Value", "Size", "Type",
30082c23cb7cSEd Maste 	    "Bind", "Vis", "Ndx", "Name");
30092c23cb7cSEd Maste 
301071edbbfdSEd Maste 	for (j = 0; j < len; j++) {
30112c23cb7cSEd Maste 		if (gelf_getsym(d, j, &sym) != &sym) {
30122c23cb7cSEd Maste 			warnx("gelf_getsym failed: %s", elf_errmsg(-1));
30132c23cb7cSEd Maste 			continue;
30142c23cb7cSEd Maste 		}
30152c23cb7cSEd Maste 		printf("%6d:", j);
30162c23cb7cSEd Maste 		printf(" %16.16jx", (uintmax_t) sym.st_value);
3017839529caSEd Maste 		printf(" %5ju", (uintmax_t) sym.st_size);
3018839529caSEd Maste 		printf(" %-7s", st_type(re->ehdr.e_machine,
3019b6b6f9ccSEd Maste 		    re->ehdr.e_ident[EI_OSABI], GELF_ST_TYPE(sym.st_info)));
30202c23cb7cSEd Maste 		printf(" %-6s", st_bind(GELF_ST_BIND(sym.st_info)));
30212c23cb7cSEd Maste 		printf(" %-8s", st_vis(GELF_ST_VISIBILITY(sym.st_other)));
30222c23cb7cSEd Maste 		printf(" %3s", st_shndx(sym.st_shndx));
30232c23cb7cSEd Maste 		if ((name = elf_strptr(re->elf, stab, sym.st_name)) != NULL)
30242c23cb7cSEd Maste 			printf(" %s", name);
30252c23cb7cSEd Maste 		/* Append symbol version string for SHT_DYNSYM symbol table. */
30262c23cb7cSEd Maste 		if (s->type == SHT_DYNSYM && re->ver != NULL &&
30272c23cb7cSEd Maste 		    re->vs != NULL && re->vs[j] > 1) {
3028656f49f8SEd Maste 			vs = re->vs[j] & VERSYM_VERSION;
3029656f49f8SEd Maste 			if (vs >= re->ver_sz || re->ver[vs].name == NULL) {
3030656f49f8SEd Maste 				warnx("invalid versym version index %u", vs);
3031656f49f8SEd Maste 				break;
3032656f49f8SEd Maste 			}
3033656f49f8SEd Maste 			if (re->vs[j] & VERSYM_HIDDEN || re->ver[vs].type == 0)
3034656f49f8SEd Maste 				printf("@%s (%d)", re->ver[vs].name, vs);
30352c23cb7cSEd Maste 			else
3036656f49f8SEd Maste 				printf("@@%s (%d)", re->ver[vs].name, vs);
30372c23cb7cSEd Maste 		}
30382c23cb7cSEd Maste 		putchar('\n');
30392c23cb7cSEd Maste 	}
30402c23cb7cSEd Maste 
30412c23cb7cSEd Maste }
30422c23cb7cSEd Maste 
30432c23cb7cSEd Maste static void
30442c23cb7cSEd Maste dump_symtabs(struct readelf *re)
30452c23cb7cSEd Maste {
30462c23cb7cSEd Maste 	GElf_Dyn dyn;
30472c23cb7cSEd Maste 	Elf_Data *d;
30482c23cb7cSEd Maste 	struct section *s;
30492c23cb7cSEd Maste 	uint64_t dyn_off;
305071edbbfdSEd Maste 	int elferr, i, len;
30512c23cb7cSEd Maste 
30522c23cb7cSEd Maste 	/*
30532c23cb7cSEd Maste 	 * If -D is specified, only dump the symbol table specified by
30542c23cb7cSEd Maste 	 * the DT_SYMTAB entry in the .dynamic section.
30552c23cb7cSEd Maste 	 */
30562c23cb7cSEd Maste 	dyn_off = 0;
30572c23cb7cSEd Maste 	if (re->options & RE_DD) {
30582c23cb7cSEd Maste 		s = NULL;
30592c23cb7cSEd Maste 		for (i = 0; (size_t)i < re->shnum; i++)
30602c23cb7cSEd Maste 			if (re->sl[i].type == SHT_DYNAMIC) {
30612c23cb7cSEd Maste 				s = &re->sl[i];
30622c23cb7cSEd Maste 				break;
30632c23cb7cSEd Maste 			}
30642c23cb7cSEd Maste 		if (s == NULL)
30652c23cb7cSEd Maste 			return;
30662c23cb7cSEd Maste 		(void) elf_errno();
30672c23cb7cSEd Maste 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
30682c23cb7cSEd Maste 			elferr = elf_errno();
30692c23cb7cSEd Maste 			if (elferr != 0)
30702c23cb7cSEd Maste 				warnx("elf_getdata failed: %s", elf_errmsg(-1));
30712c23cb7cSEd Maste 			return;
30722c23cb7cSEd Maste 		}
30732c23cb7cSEd Maste 		if (d->d_size <= 0)
30742c23cb7cSEd Maste 			return;
307571edbbfdSEd Maste 		if (!get_ent_count(s, &len))
307671edbbfdSEd Maste 			return;
30772c23cb7cSEd Maste 
307871edbbfdSEd Maste 		for (i = 0; i < len; i++) {
30792c23cb7cSEd Maste 			if (gelf_getdyn(d, i, &dyn) != &dyn) {
30802c23cb7cSEd Maste 				warnx("gelf_getdyn failed: %s", elf_errmsg(-1));
30812c23cb7cSEd Maste 				continue;
30822c23cb7cSEd Maste 			}
30832c23cb7cSEd Maste 			if (dyn.d_tag == DT_SYMTAB) {
30842c23cb7cSEd Maste 				dyn_off = dyn.d_un.d_val;
30852c23cb7cSEd Maste 				break;
30862c23cb7cSEd Maste 			}
30872c23cb7cSEd Maste 		}
30882c23cb7cSEd Maste 	}
30892c23cb7cSEd Maste 
30902c23cb7cSEd Maste 	/* Find and dump symbol tables. */
30912c23cb7cSEd Maste 	for (i = 0; (size_t)i < re->shnum; i++) {
30922c23cb7cSEd Maste 		s = &re->sl[i];
30932c23cb7cSEd Maste 		if (s->type == SHT_SYMTAB || s->type == SHT_DYNSYM) {
30942c23cb7cSEd Maste 			if (re->options & RE_DD) {
30952c23cb7cSEd Maste 				if (dyn_off == s->addr) {
30962c23cb7cSEd Maste 					dump_symtab(re, i);
30972c23cb7cSEd Maste 					break;
30982c23cb7cSEd Maste 				}
30992c23cb7cSEd Maste 			} else
31002c23cb7cSEd Maste 				dump_symtab(re, i);
31012c23cb7cSEd Maste 		}
31022c23cb7cSEd Maste 	}
31032c23cb7cSEd Maste }
31042c23cb7cSEd Maste 
31052c23cb7cSEd Maste static void
31062c23cb7cSEd Maste dump_svr4_hash(struct section *s)
31072c23cb7cSEd Maste {
31082c23cb7cSEd Maste 	Elf_Data	*d;
31092c23cb7cSEd Maste 	uint32_t	*buf;
31102c23cb7cSEd Maste 	uint32_t	 nbucket, nchain;
31112c23cb7cSEd Maste 	uint32_t	*bucket, *chain;
31122c23cb7cSEd Maste 	uint32_t	*bl, *c, maxl, total;
31132c23cb7cSEd Maste 	int		 elferr, i, j;
31142c23cb7cSEd Maste 
31152c23cb7cSEd Maste 	/* Read and parse the content of .hash section. */
31162c23cb7cSEd Maste 	(void) elf_errno();
31172c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
31182c23cb7cSEd Maste 		elferr = elf_errno();
31192c23cb7cSEd Maste 		if (elferr != 0)
31202c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
31212c23cb7cSEd Maste 		return;
31222c23cb7cSEd Maste 	}
31232c23cb7cSEd Maste 	if (d->d_size < 2 * sizeof(uint32_t)) {
31242c23cb7cSEd Maste 		warnx(".hash section too small");
31252c23cb7cSEd Maste 		return;
31262c23cb7cSEd Maste 	}
31272c23cb7cSEd Maste 	buf = d->d_buf;
31282c23cb7cSEd Maste 	nbucket = buf[0];
31292c23cb7cSEd Maste 	nchain = buf[1];
31302c23cb7cSEd Maste 	if (nbucket <= 0 || nchain <= 0) {
31312c23cb7cSEd Maste 		warnx("Malformed .hash section");
31322c23cb7cSEd Maste 		return;
31332c23cb7cSEd Maste 	}
31342c23cb7cSEd Maste 	if (d->d_size != (nbucket + nchain + 2) * sizeof(uint32_t)) {
31352c23cb7cSEd Maste 		warnx("Malformed .hash section");
31362c23cb7cSEd Maste 		return;
31372c23cb7cSEd Maste 	}
31382c23cb7cSEd Maste 	bucket = &buf[2];
31392c23cb7cSEd Maste 	chain = &buf[2 + nbucket];
31402c23cb7cSEd Maste 
31412c23cb7cSEd Maste 	maxl = 0;
31422c23cb7cSEd Maste 	if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
31432c23cb7cSEd Maste 		errx(EXIT_FAILURE, "calloc failed");
31442c23cb7cSEd Maste 	for (i = 0; (uint32_t)i < nbucket; i++)
31452c23cb7cSEd Maste 		for (j = bucket[i]; j > 0 && (uint32_t)j < nchain; j = chain[j])
31462c23cb7cSEd Maste 			if (++bl[i] > maxl)
31472c23cb7cSEd Maste 				maxl = bl[i];
31482c23cb7cSEd Maste 	if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
31492c23cb7cSEd Maste 		errx(EXIT_FAILURE, "calloc failed");
31502c23cb7cSEd Maste 	for (i = 0; (uint32_t)i < nbucket; i++)
31512c23cb7cSEd Maste 		c[bl[i]]++;
31522c23cb7cSEd Maste 	printf("\nHistogram for bucket list length (total of %u buckets):\n",
31532c23cb7cSEd Maste 	    nbucket);
31542c23cb7cSEd Maste 	printf(" Length\tNumber\t\t%% of total\tCoverage\n");
31552c23cb7cSEd Maste 	total = 0;
31562c23cb7cSEd Maste 	for (i = 0; (uint32_t)i <= maxl; i++) {
31572c23cb7cSEd Maste 		total += c[i] * i;
31582c23cb7cSEd Maste 		printf("%7u\t%-10u\t(%5.1f%%)\t%5.1f%%\n", i, c[i],
31592c23cb7cSEd Maste 		    c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
31602c23cb7cSEd Maste 	}
31612c23cb7cSEd Maste 	free(c);
31622c23cb7cSEd Maste 	free(bl);
31632c23cb7cSEd Maste }
31642c23cb7cSEd Maste 
31652c23cb7cSEd Maste static void
31662c23cb7cSEd Maste dump_svr4_hash64(struct readelf *re, struct section *s)
31672c23cb7cSEd Maste {
31682c23cb7cSEd Maste 	Elf_Data	*d, dst;
31692c23cb7cSEd Maste 	uint64_t	*buf;
31702c23cb7cSEd Maste 	uint64_t	 nbucket, nchain;
31712c23cb7cSEd Maste 	uint64_t	*bucket, *chain;
31722c23cb7cSEd Maste 	uint64_t	*bl, *c, maxl, total;
31732c23cb7cSEd Maste 	int		 elferr, i, j;
31742c23cb7cSEd Maste 
31752c23cb7cSEd Maste 	/*
31762c23cb7cSEd Maste 	 * ALPHA uses 64-bit hash entries. Since libelf assumes that
31772c23cb7cSEd Maste 	 * .hash section contains only 32-bit entry, an explicit
31782c23cb7cSEd Maste 	 * gelf_xlatetom is needed here.
31792c23cb7cSEd Maste 	 */
31802c23cb7cSEd Maste 	(void) elf_errno();
31812c23cb7cSEd Maste 	if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
31822c23cb7cSEd Maste 		elferr = elf_errno();
31832c23cb7cSEd Maste 		if (elferr != 0)
31842c23cb7cSEd Maste 			warnx("elf_rawdata failed: %s",
31852c23cb7cSEd Maste 			    elf_errmsg(elferr));
31862c23cb7cSEd Maste 		return;
31872c23cb7cSEd Maste 	}
31882c23cb7cSEd Maste 	d->d_type = ELF_T_XWORD;
31892c23cb7cSEd Maste 	memcpy(&dst, d, sizeof(Elf_Data));
31902c23cb7cSEd Maste 	if (gelf_xlatetom(re->elf, &dst, d,
31912c23cb7cSEd Maste 		re->ehdr.e_ident[EI_DATA]) != &dst) {
31922c23cb7cSEd Maste 		warnx("gelf_xlatetom failed: %s", elf_errmsg(-1));
31932c23cb7cSEd Maste 		return;
31942c23cb7cSEd Maste 	}
31952c23cb7cSEd Maste 	if (dst.d_size < 2 * sizeof(uint64_t)) {
31962c23cb7cSEd Maste 		warnx(".hash section too small");
31972c23cb7cSEd Maste 		return;
31982c23cb7cSEd Maste 	}
31992c23cb7cSEd Maste 	buf = dst.d_buf;
32002c23cb7cSEd Maste 	nbucket = buf[0];
32012c23cb7cSEd Maste 	nchain = buf[1];
32022c23cb7cSEd Maste 	if (nbucket <= 0 || nchain <= 0) {
32032c23cb7cSEd Maste 		warnx("Malformed .hash section");
32042c23cb7cSEd Maste 		return;
32052c23cb7cSEd Maste 	}
32062c23cb7cSEd Maste 	if (d->d_size != (nbucket + nchain + 2) * sizeof(uint32_t)) {
32072c23cb7cSEd Maste 		warnx("Malformed .hash section");
32082c23cb7cSEd Maste 		return;
32092c23cb7cSEd Maste 	}
32102c23cb7cSEd Maste 	bucket = &buf[2];
32112c23cb7cSEd Maste 	chain = &buf[2 + nbucket];
32122c23cb7cSEd Maste 
32132c23cb7cSEd Maste 	maxl = 0;
32142c23cb7cSEd Maste 	if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
32152c23cb7cSEd Maste 		errx(EXIT_FAILURE, "calloc failed");
32162c23cb7cSEd Maste 	for (i = 0; (uint32_t)i < nbucket; i++)
32172c23cb7cSEd Maste 		for (j = bucket[i]; j > 0 && (uint32_t)j < nchain; j = chain[j])
32182c23cb7cSEd Maste 			if (++bl[i] > maxl)
32192c23cb7cSEd Maste 				maxl = bl[i];
32202c23cb7cSEd Maste 	if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
32212c23cb7cSEd Maste 		errx(EXIT_FAILURE, "calloc failed");
32222c23cb7cSEd Maste 	for (i = 0; (uint64_t)i < nbucket; i++)
32232c23cb7cSEd Maste 		c[bl[i]]++;
32242c23cb7cSEd Maste 	printf("Histogram for bucket list length (total of %ju buckets):\n",
32252c23cb7cSEd Maste 	    (uintmax_t)nbucket);
32262c23cb7cSEd Maste 	printf(" Length\tNumber\t\t%% of total\tCoverage\n");
32272c23cb7cSEd Maste 	total = 0;
32282c23cb7cSEd Maste 	for (i = 0; (uint64_t)i <= maxl; i++) {
32292c23cb7cSEd Maste 		total += c[i] * i;
32302c23cb7cSEd Maste 		printf("%7u\t%-10ju\t(%5.1f%%)\t%5.1f%%\n", i, (uintmax_t)c[i],
32312c23cb7cSEd Maste 		    c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
32322c23cb7cSEd Maste 	}
32332c23cb7cSEd Maste 	free(c);
32342c23cb7cSEd Maste 	free(bl);
32352c23cb7cSEd Maste }
32362c23cb7cSEd Maste 
32372c23cb7cSEd Maste static void
32382c23cb7cSEd Maste dump_gnu_hash(struct readelf *re, struct section *s)
32392c23cb7cSEd Maste {
32402c23cb7cSEd Maste 	struct section	*ds;
32412c23cb7cSEd Maste 	Elf_Data	*d;
32422c23cb7cSEd Maste 	uint32_t	*buf;
32432c23cb7cSEd Maste 	uint32_t	*bucket, *chain;
3244cf781b2eSEd Maste 	uint32_t	 nbucket, nchain, symndx, maskwords;
32452c23cb7cSEd Maste 	uint32_t	*bl, *c, maxl, total;
32462c23cb7cSEd Maste 	int		 elferr, dynsymcount, i, j;
32472c23cb7cSEd Maste 
32482c23cb7cSEd Maste 	(void) elf_errno();
32492c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
32502c23cb7cSEd Maste 		elferr = elf_errno();
32512c23cb7cSEd Maste 		if (elferr != 0)
32522c23cb7cSEd Maste 			warnx("elf_getdata failed: %s",
32532c23cb7cSEd Maste 			    elf_errmsg(elferr));
32542c23cb7cSEd Maste 		return;
32552c23cb7cSEd Maste 	}
32562c23cb7cSEd Maste 	if (d->d_size < 4 * sizeof(uint32_t)) {
32572c23cb7cSEd Maste 		warnx(".gnu.hash section too small");
32582c23cb7cSEd Maste 		return;
32592c23cb7cSEd Maste 	}
32602c23cb7cSEd Maste 	buf = d->d_buf;
32612c23cb7cSEd Maste 	nbucket = buf[0];
32622c23cb7cSEd Maste 	symndx = buf[1];
32632c23cb7cSEd Maste 	maskwords = buf[2];
32642c23cb7cSEd Maste 	buf += 4;
3265656f49f8SEd Maste 	if (s->link >= re->shnum)
3266656f49f8SEd Maste 		return;
32672c23cb7cSEd Maste 	ds = &re->sl[s->link];
326871edbbfdSEd Maste 	if (!get_ent_count(ds, &dynsymcount))
326971edbbfdSEd Maste 		return;
3270b6b6f9ccSEd Maste 	if (symndx >= (uint32_t)dynsymcount) {
3271b6b6f9ccSEd Maste 		warnx("Malformed .gnu.hash section (symndx out of range)");
3272b6b6f9ccSEd Maste 		return;
3273b6b6f9ccSEd Maste 	}
32742c23cb7cSEd Maste 	nchain = dynsymcount - symndx;
32752c23cb7cSEd Maste 	if (d->d_size != 4 * sizeof(uint32_t) + maskwords *
32762c23cb7cSEd Maste 	    (re->ec == ELFCLASS32 ? sizeof(uint32_t) : sizeof(uint64_t)) +
32772c23cb7cSEd Maste 	    (nbucket + nchain) * sizeof(uint32_t)) {
32782c23cb7cSEd Maste 		warnx("Malformed .gnu.hash section");
32792c23cb7cSEd Maste 		return;
32802c23cb7cSEd Maste 	}
32812c23cb7cSEd Maste 	bucket = buf + (re->ec == ELFCLASS32 ? maskwords : maskwords * 2);
32822c23cb7cSEd Maste 	chain = bucket + nbucket;
32832c23cb7cSEd Maste 
32842c23cb7cSEd Maste 	maxl = 0;
32852c23cb7cSEd Maste 	if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
32862c23cb7cSEd Maste 		errx(EXIT_FAILURE, "calloc failed");
32872c23cb7cSEd Maste 	for (i = 0; (uint32_t)i < nbucket; i++)
32882c23cb7cSEd Maste 		for (j = bucket[i]; j > 0 && (uint32_t)j - symndx < nchain;
32892c23cb7cSEd Maste 		     j++) {
32902c23cb7cSEd Maste 			if (++bl[i] > maxl)
32912c23cb7cSEd Maste 				maxl = bl[i];
32922c23cb7cSEd Maste 			if (chain[j - symndx] & 1)
32932c23cb7cSEd Maste 				break;
32942c23cb7cSEd Maste 		}
32952c23cb7cSEd Maste 	if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
32962c23cb7cSEd Maste 		errx(EXIT_FAILURE, "calloc failed");
32972c23cb7cSEd Maste 	for (i = 0; (uint32_t)i < nbucket; i++)
32982c23cb7cSEd Maste 		c[bl[i]]++;
32992c23cb7cSEd Maste 	printf("Histogram for bucket list length (total of %u buckets):\n",
33002c23cb7cSEd Maste 	    nbucket);
33012c23cb7cSEd Maste 	printf(" Length\tNumber\t\t%% of total\tCoverage\n");
33022c23cb7cSEd Maste 	total = 0;
33032c23cb7cSEd Maste 	for (i = 0; (uint32_t)i <= maxl; i++) {
33042c23cb7cSEd Maste 		total += c[i] * i;
33052c23cb7cSEd Maste 		printf("%7u\t%-10u\t(%5.1f%%)\t%5.1f%%\n", i, c[i],
33062c23cb7cSEd Maste 		    c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
33072c23cb7cSEd Maste 	}
33082c23cb7cSEd Maste 	free(c);
33092c23cb7cSEd Maste 	free(bl);
33102c23cb7cSEd Maste }
33112c23cb7cSEd Maste 
33122c23cb7cSEd Maste static void
33132c23cb7cSEd Maste dump_hash(struct readelf *re)
33142c23cb7cSEd Maste {
33152c23cb7cSEd Maste 	struct section	*s;
33162c23cb7cSEd Maste 	int		 i;
33172c23cb7cSEd Maste 
33182c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
33192c23cb7cSEd Maste 		s = &re->sl[i];
33202c23cb7cSEd Maste 		if (s->type == SHT_HASH || s->type == SHT_GNU_HASH) {
33212c23cb7cSEd Maste 			if (s->type == SHT_GNU_HASH)
33222c23cb7cSEd Maste 				dump_gnu_hash(re, s);
33232c23cb7cSEd Maste 			else if (re->ehdr.e_machine == EM_ALPHA &&
33242c23cb7cSEd Maste 			    s->entsize == 8)
33252c23cb7cSEd Maste 				dump_svr4_hash64(re, s);
33262c23cb7cSEd Maste 			else
33272c23cb7cSEd Maste 				dump_svr4_hash(s);
33282c23cb7cSEd Maste 		}
33292c23cb7cSEd Maste 	}
33302c23cb7cSEd Maste }
33312c23cb7cSEd Maste 
33322c23cb7cSEd Maste static void
33332c23cb7cSEd Maste dump_notes(struct readelf *re)
33342c23cb7cSEd Maste {
33352c23cb7cSEd Maste 	struct section *s;
33362c23cb7cSEd Maste 	const char *rawfile;
33372c23cb7cSEd Maste 	GElf_Phdr phdr;
33382c23cb7cSEd Maste 	Elf_Data *d;
3339b6b6f9ccSEd Maste 	size_t filesize, phnum;
33402c23cb7cSEd Maste 	int i, elferr;
33412c23cb7cSEd Maste 
33422c23cb7cSEd Maste 	if (re->ehdr.e_type == ET_CORE) {
33432c23cb7cSEd Maste 		/*
33442c23cb7cSEd Maste 		 * Search program headers in the core file for
33452c23cb7cSEd Maste 		 * PT_NOTE entry.
33462c23cb7cSEd Maste 		 */
33472c23cb7cSEd Maste 		if (elf_getphnum(re->elf, &phnum) == 0) {
33482c23cb7cSEd Maste 			warnx("elf_getphnum failed: %s", elf_errmsg(-1));
33492c23cb7cSEd Maste 			return;
33502c23cb7cSEd Maste 		}
33512c23cb7cSEd Maste 		if (phnum == 0)
33522c23cb7cSEd Maste 			return;
3353b6b6f9ccSEd Maste 		if ((rawfile = elf_rawfile(re->elf, &filesize)) == NULL) {
33542c23cb7cSEd Maste 			warnx("elf_rawfile failed: %s", elf_errmsg(-1));
33552c23cb7cSEd Maste 			return;
33562c23cb7cSEd Maste 		}
33572c23cb7cSEd Maste 		for (i = 0; (size_t) i < phnum; i++) {
33582c23cb7cSEd Maste 			if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
33592c23cb7cSEd Maste 				warnx("gelf_getphdr failed: %s",
33602c23cb7cSEd Maste 				    elf_errmsg(-1));
33612c23cb7cSEd Maste 				continue;
33622c23cb7cSEd Maste 			}
3363b6b6f9ccSEd Maste 			if (phdr.p_type == PT_NOTE) {
3364b6b6f9ccSEd Maste 				if (phdr.p_offset >= filesize ||
3365b6b6f9ccSEd Maste 				    phdr.p_filesz > filesize - phdr.p_offset) {
3366b6b6f9ccSEd Maste 					warnx("invalid PHDR offset");
3367b6b6f9ccSEd Maste 					continue;
3368b6b6f9ccSEd Maste 				}
33692c23cb7cSEd Maste 				dump_notes_content(re, rawfile + phdr.p_offset,
33702c23cb7cSEd Maste 				    phdr.p_filesz, phdr.p_offset);
33712c23cb7cSEd Maste 			}
3372b6b6f9ccSEd Maste 		}
33732c23cb7cSEd Maste 
33742c23cb7cSEd Maste 	} else {
33752c23cb7cSEd Maste 		/*
33762c23cb7cSEd Maste 		 * For objects other than core files, Search for
33772c23cb7cSEd Maste 		 * SHT_NOTE sections.
33782c23cb7cSEd Maste 		 */
33792c23cb7cSEd Maste 		for (i = 0; (size_t) i < re->shnum; i++) {
33802c23cb7cSEd Maste 			s = &re->sl[i];
33812c23cb7cSEd Maste 			if (s->type == SHT_NOTE) {
33822c23cb7cSEd Maste 				(void) elf_errno();
33832c23cb7cSEd Maste 				if ((d = elf_getdata(s->scn, NULL)) == NULL) {
33842c23cb7cSEd Maste 					elferr = elf_errno();
33852c23cb7cSEd Maste 					if (elferr != 0)
33862c23cb7cSEd Maste 						warnx("elf_getdata failed: %s",
33872c23cb7cSEd Maste 						    elf_errmsg(elferr));
33882c23cb7cSEd Maste 					continue;
33892c23cb7cSEd Maste 				}
33902c23cb7cSEd Maste 				dump_notes_content(re, d->d_buf, d->d_size,
33912c23cb7cSEd Maste 				    s->off);
33922c23cb7cSEd Maste 			}
33932c23cb7cSEd Maste 		}
33942c23cb7cSEd Maste 	}
33952c23cb7cSEd Maste }
33962c23cb7cSEd Maste 
33972c23cb7cSEd Maste static void
33982c23cb7cSEd Maste dump_notes_content(struct readelf *re, const char *buf, size_t sz, off_t off)
33992c23cb7cSEd Maste {
34002c23cb7cSEd Maste 	Elf_Note *note;
340102b08c90SEd Maste 	const char *end, *name;
34022c23cb7cSEd Maste 
34032c23cb7cSEd Maste 	printf("\nNotes at offset %#010jx with length %#010jx:\n",
34042c23cb7cSEd Maste 	    (uintmax_t) off, (uintmax_t) sz);
34052c23cb7cSEd Maste 	printf("  %-13s %-15s %s\n", "Owner", "Data size", "Description");
34062c23cb7cSEd Maste 	end = buf + sz;
34072c23cb7cSEd Maste 	while (buf < end) {
340802b08c90SEd Maste 		if (buf + sizeof(*note) > end) {
340902b08c90SEd Maste 			warnx("invalid note header");
341002b08c90SEd Maste 			return;
341102b08c90SEd Maste 		}
34122c23cb7cSEd Maste 		note = (Elf_Note *)(uintptr_t) buf;
341302b08c90SEd Maste 		name = (char *)(uintptr_t)(note + 1);
341402b08c90SEd Maste 		/*
341502b08c90SEd Maste 		 * The name field is required to be nul-terminated, and
341602b08c90SEd Maste 		 * n_namesz includes the terminating nul in observed
341702b08c90SEd Maste 		 * implementations (contrary to the ELF-64 spec). A special
341802b08c90SEd Maste 		 * case is needed for cores generated by some older Linux
341902b08c90SEd Maste 		 * versions, which write a note named "CORE" without a nul
342002b08c90SEd Maste 		 * terminator and n_namesz = 4.
342102b08c90SEd Maste 		 */
342202b08c90SEd Maste 		if (note->n_namesz == 0)
342302b08c90SEd Maste 			name = "";
342402b08c90SEd Maste 		else if (note->n_namesz == 4 && strncmp(name, "CORE", 4) == 0)
342502b08c90SEd Maste 			name = "CORE";
342602b08c90SEd Maste 		else if (strnlen(name, note->n_namesz) >= note->n_namesz)
342702b08c90SEd Maste 			name = "<invalid>";
342802b08c90SEd Maste 		printf("  %-13s %#010jx", name, (uintmax_t) note->n_descsz);
342902b08c90SEd Maste 		printf("      %s\n", note_type(name, re->ehdr.e_type,
343002b08c90SEd Maste 		    note->n_type));
343134e3f146SEd Maste 		buf += sizeof(Elf_Note) + roundup2(note->n_namesz, 4) +
34322c23cb7cSEd Maste 		    roundup2(note->n_descsz, 4);
34332c23cb7cSEd Maste 	}
34342c23cb7cSEd Maste }
34352c23cb7cSEd Maste 
34362c23cb7cSEd Maste /*
34372c23cb7cSEd Maste  * Symbol versioning sections are the same for 32bit and 64bit
34382c23cb7cSEd Maste  * ELF objects.
34392c23cb7cSEd Maste  */
34402c23cb7cSEd Maste #define Elf_Verdef	Elf32_Verdef
34412c23cb7cSEd Maste #define	Elf_Verdaux	Elf32_Verdaux
34422c23cb7cSEd Maste #define	Elf_Verneed	Elf32_Verneed
34432c23cb7cSEd Maste #define	Elf_Vernaux	Elf32_Vernaux
34442c23cb7cSEd Maste 
34452c23cb7cSEd Maste #define	SAVE_VERSION_NAME(x, n, t)					\
34462c23cb7cSEd Maste 	do {								\
34472c23cb7cSEd Maste 		while (x >= re->ver_sz) {				\
34482c23cb7cSEd Maste 			nv = realloc(re->ver,				\
34492c23cb7cSEd Maste 			    sizeof(*re->ver) * re->ver_sz * 2);		\
34502c23cb7cSEd Maste 			if (nv == NULL) {				\
34512c23cb7cSEd Maste 				warn("realloc failed");			\
34522c23cb7cSEd Maste 				free(re->ver);				\
34532c23cb7cSEd Maste 				return;					\
34542c23cb7cSEd Maste 			}						\
34552c23cb7cSEd Maste 			re->ver = nv;					\
34562c23cb7cSEd Maste 			for (i = re->ver_sz; i < re->ver_sz * 2; i++) {	\
34572c23cb7cSEd Maste 				re->ver[i].name = NULL;			\
34582c23cb7cSEd Maste 				re->ver[i].type = 0;			\
34592c23cb7cSEd Maste 			}						\
34602c23cb7cSEd Maste 			re->ver_sz *= 2;				\
34612c23cb7cSEd Maste 		}							\
34622c23cb7cSEd Maste 		if (x > 1) {						\
34632c23cb7cSEd Maste 			re->ver[x].name = n;				\
34642c23cb7cSEd Maste 			re->ver[x].type = t;				\
34652c23cb7cSEd Maste 		}							\
34662c23cb7cSEd Maste 	} while (0)
34672c23cb7cSEd Maste 
34682c23cb7cSEd Maste 
34692c23cb7cSEd Maste static void
34702c23cb7cSEd Maste dump_verdef(struct readelf *re, int dump)
34712c23cb7cSEd Maste {
34722c23cb7cSEd Maste 	struct section *s;
34732c23cb7cSEd Maste 	struct symver *nv;
34742c23cb7cSEd Maste 	Elf_Data *d;
34752c23cb7cSEd Maste 	Elf_Verdef *vd;
34762c23cb7cSEd Maste 	Elf_Verdaux *vda;
34772c23cb7cSEd Maste 	uint8_t *buf, *end, *buf2;
34782c23cb7cSEd Maste 	const char *name;
34792c23cb7cSEd Maste 	int elferr, i, j;
34802c23cb7cSEd Maste 
34812c23cb7cSEd Maste 	if ((s = re->vd_s) == NULL)
34822c23cb7cSEd Maste 		return;
3483656f49f8SEd Maste 	if (s->link >= re->shnum)
3484656f49f8SEd Maste 		return;
34852c23cb7cSEd Maste 
34862c23cb7cSEd Maste 	if (re->ver == NULL) {
34872c23cb7cSEd Maste 		re->ver_sz = 16;
34882c23cb7cSEd Maste 		if ((re->ver = calloc(re->ver_sz, sizeof(*re->ver))) ==
34892c23cb7cSEd Maste 		    NULL) {
34902c23cb7cSEd Maste 			warn("calloc failed");
34912c23cb7cSEd Maste 			return;
34922c23cb7cSEd Maste 		}
34932c23cb7cSEd Maste 		re->ver[0].name = "*local*";
34942c23cb7cSEd Maste 		re->ver[1].name = "*global*";
34952c23cb7cSEd Maste 	}
34962c23cb7cSEd Maste 
34972c23cb7cSEd Maste 	if (dump)
34982c23cb7cSEd Maste 		printf("\nVersion definition section (%s):\n", s->name);
34992c23cb7cSEd Maste 	(void) elf_errno();
35002c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
35012c23cb7cSEd Maste 		elferr = elf_errno();
35022c23cb7cSEd Maste 		if (elferr != 0)
35032c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
35042c23cb7cSEd Maste 		return;
35052c23cb7cSEd Maste 	}
35062c23cb7cSEd Maste 	if (d->d_size == 0)
35072c23cb7cSEd Maste 		return;
35082c23cb7cSEd Maste 
35092c23cb7cSEd Maste 	buf = d->d_buf;
35102c23cb7cSEd Maste 	end = buf + d->d_size;
35112c23cb7cSEd Maste 	while (buf + sizeof(Elf_Verdef) <= end) {
35122c23cb7cSEd Maste 		vd = (Elf_Verdef *) (uintptr_t) buf;
35132c23cb7cSEd Maste 		if (dump) {
35142c23cb7cSEd Maste 			printf("  0x%4.4lx", (unsigned long)
35152c23cb7cSEd Maste 			    (buf - (uint8_t *)d->d_buf));
35162c23cb7cSEd Maste 			printf(" vd_version: %u vd_flags: %d"
35172c23cb7cSEd Maste 			    " vd_ndx: %u vd_cnt: %u", vd->vd_version,
35182c23cb7cSEd Maste 			    vd->vd_flags, vd->vd_ndx, vd->vd_cnt);
35192c23cb7cSEd Maste 		}
35202c23cb7cSEd Maste 		buf2 = buf + vd->vd_aux;
35212c23cb7cSEd Maste 		j = 0;
35222c23cb7cSEd Maste 		while (buf2 + sizeof(Elf_Verdaux) <= end && j < vd->vd_cnt) {
35232c23cb7cSEd Maste 			vda = (Elf_Verdaux *) (uintptr_t) buf2;
35242c23cb7cSEd Maste 			name = get_string(re, s->link, vda->vda_name);
35252c23cb7cSEd Maste 			if (j == 0) {
35262c23cb7cSEd Maste 				if (dump)
35272c23cb7cSEd Maste 					printf(" vda_name: %s\n", name);
35282c23cb7cSEd Maste 				SAVE_VERSION_NAME((int)vd->vd_ndx, name, 1);
35292c23cb7cSEd Maste 			} else if (dump)
35302c23cb7cSEd Maste 				printf("  0x%4.4lx parent: %s\n",
35312c23cb7cSEd Maste 				    (unsigned long) (buf2 -
35322c23cb7cSEd Maste 				    (uint8_t *)d->d_buf), name);
35332c23cb7cSEd Maste 			if (vda->vda_next == 0)
35342c23cb7cSEd Maste 				break;
35352c23cb7cSEd Maste 			buf2 += vda->vda_next;
35362c23cb7cSEd Maste 			j++;
35372c23cb7cSEd Maste 		}
35382c23cb7cSEd Maste 		if (vd->vd_next == 0)
35392c23cb7cSEd Maste 			break;
35402c23cb7cSEd Maste 		buf += vd->vd_next;
35412c23cb7cSEd Maste 	}
35422c23cb7cSEd Maste }
35432c23cb7cSEd Maste 
35442c23cb7cSEd Maste static void
35452c23cb7cSEd Maste dump_verneed(struct readelf *re, int dump)
35462c23cb7cSEd Maste {
35472c23cb7cSEd Maste 	struct section *s;
35482c23cb7cSEd Maste 	struct symver *nv;
35492c23cb7cSEd Maste 	Elf_Data *d;
35502c23cb7cSEd Maste 	Elf_Verneed *vn;
35512c23cb7cSEd Maste 	Elf_Vernaux *vna;
35522c23cb7cSEd Maste 	uint8_t *buf, *end, *buf2;
35532c23cb7cSEd Maste 	const char *name;
35542c23cb7cSEd Maste 	int elferr, i, j;
35552c23cb7cSEd Maste 
35562c23cb7cSEd Maste 	if ((s = re->vn_s) == NULL)
35572c23cb7cSEd Maste 		return;
3558656f49f8SEd Maste 	if (s->link >= re->shnum)
3559656f49f8SEd Maste 		return;
35602c23cb7cSEd Maste 
35612c23cb7cSEd Maste 	if (re->ver == NULL) {
35622c23cb7cSEd Maste 		re->ver_sz = 16;
35632c23cb7cSEd Maste 		if ((re->ver = calloc(re->ver_sz, sizeof(*re->ver))) ==
35642c23cb7cSEd Maste 		    NULL) {
35652c23cb7cSEd Maste 			warn("calloc failed");
35662c23cb7cSEd Maste 			return;
35672c23cb7cSEd Maste 		}
35682c23cb7cSEd Maste 		re->ver[0].name = "*local*";
35692c23cb7cSEd Maste 		re->ver[1].name = "*global*";
35702c23cb7cSEd Maste 	}
35712c23cb7cSEd Maste 
35722c23cb7cSEd Maste 	if (dump)
35732c23cb7cSEd Maste 		printf("\nVersion needed section (%s):\n", s->name);
35742c23cb7cSEd Maste 	(void) elf_errno();
35752c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
35762c23cb7cSEd Maste 		elferr = elf_errno();
35772c23cb7cSEd Maste 		if (elferr != 0)
35782c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
35792c23cb7cSEd Maste 		return;
35802c23cb7cSEd Maste 	}
35812c23cb7cSEd Maste 	if (d->d_size == 0)
35822c23cb7cSEd Maste 		return;
35832c23cb7cSEd Maste 
35842c23cb7cSEd Maste 	buf = d->d_buf;
35852c23cb7cSEd Maste 	end = buf + d->d_size;
35862c23cb7cSEd Maste 	while (buf + sizeof(Elf_Verneed) <= end) {
35872c23cb7cSEd Maste 		vn = (Elf_Verneed *) (uintptr_t) buf;
35882c23cb7cSEd Maste 		if (dump) {
35892c23cb7cSEd Maste 			printf("  0x%4.4lx", (unsigned long)
35902c23cb7cSEd Maste 			    (buf - (uint8_t *)d->d_buf));
35912c23cb7cSEd Maste 			printf(" vn_version: %u vn_file: %s vn_cnt: %u\n",
35922c23cb7cSEd Maste 			    vn->vn_version,
35932c23cb7cSEd Maste 			    get_string(re, s->link, vn->vn_file),
35942c23cb7cSEd Maste 			    vn->vn_cnt);
35952c23cb7cSEd Maste 		}
35962c23cb7cSEd Maste 		buf2 = buf + vn->vn_aux;
35972c23cb7cSEd Maste 		j = 0;
35982c23cb7cSEd Maste 		while (buf2 + sizeof(Elf_Vernaux) <= end && j < vn->vn_cnt) {
35992c23cb7cSEd Maste 			vna = (Elf32_Vernaux *) (uintptr_t) buf2;
36002c23cb7cSEd Maste 			if (dump)
36012c23cb7cSEd Maste 				printf("  0x%4.4lx", (unsigned long)
36022c23cb7cSEd Maste 				    (buf2 - (uint8_t *)d->d_buf));
36032c23cb7cSEd Maste 			name = get_string(re, s->link, vna->vna_name);
36042c23cb7cSEd Maste 			if (dump)
36052c23cb7cSEd Maste 				printf("   vna_name: %s vna_flags: %u"
36062c23cb7cSEd Maste 				    " vna_other: %u\n", name,
36072c23cb7cSEd Maste 				    vna->vna_flags, vna->vna_other);
36082c23cb7cSEd Maste 			SAVE_VERSION_NAME((int)vna->vna_other, name, 0);
36092c23cb7cSEd Maste 			if (vna->vna_next == 0)
36102c23cb7cSEd Maste 				break;
36112c23cb7cSEd Maste 			buf2 += vna->vna_next;
36122c23cb7cSEd Maste 			j++;
36132c23cb7cSEd Maste 		}
36142c23cb7cSEd Maste 		if (vn->vn_next == 0)
36152c23cb7cSEd Maste 			break;
36162c23cb7cSEd Maste 		buf += vn->vn_next;
36172c23cb7cSEd Maste 	}
36182c23cb7cSEd Maste }
36192c23cb7cSEd Maste 
36202c23cb7cSEd Maste static void
36212c23cb7cSEd Maste dump_versym(struct readelf *re)
36222c23cb7cSEd Maste {
36232c23cb7cSEd Maste 	int i;
3624656f49f8SEd Maste 	uint16_t vs;
36252c23cb7cSEd Maste 
36262c23cb7cSEd Maste 	if (re->vs_s == NULL || re->ver == NULL || re->vs == NULL)
36272c23cb7cSEd Maste 		return;
36282c23cb7cSEd Maste 	printf("\nVersion symbol section (%s):\n", re->vs_s->name);
36292c23cb7cSEd Maste 	for (i = 0; i < re->vs_sz; i++) {
36302c23cb7cSEd Maste 		if ((i & 3) == 0) {
36312c23cb7cSEd Maste 			if (i > 0)
36322c23cb7cSEd Maste 				putchar('\n');
36332c23cb7cSEd Maste 			printf("  %03x:", i);
36342c23cb7cSEd Maste 		}
3635656f49f8SEd Maste 		vs = re->vs[i] & VERSYM_VERSION;
3636656f49f8SEd Maste 		if (vs >= re->ver_sz || re->ver[vs].name == NULL) {
3637656f49f8SEd Maste 			warnx("invalid versym version index %u", re->vs[i]);
3638656f49f8SEd Maste 			break;
3639656f49f8SEd Maste 		}
3640656f49f8SEd Maste 		if (re->vs[i] & VERSYM_HIDDEN)
3641656f49f8SEd Maste 			printf(" %3xh %-12s ", vs,
3642656f49f8SEd Maste 			    re->ver[re->vs[i] & VERSYM_VERSION].name);
36432c23cb7cSEd Maste 		else
3644656f49f8SEd Maste 			printf(" %3x %-12s ", vs, re->ver[re->vs[i]].name);
36452c23cb7cSEd Maste 	}
36462c23cb7cSEd Maste 	putchar('\n');
36472c23cb7cSEd Maste }
36482c23cb7cSEd Maste 
36492c23cb7cSEd Maste static void
36502c23cb7cSEd Maste dump_ver(struct readelf *re)
36512c23cb7cSEd Maste {
36522c23cb7cSEd Maste 
36532c23cb7cSEd Maste 	if (re->vs_s && re->ver && re->vs)
36542c23cb7cSEd Maste 		dump_versym(re);
36552c23cb7cSEd Maste 	if (re->vd_s)
36562c23cb7cSEd Maste 		dump_verdef(re, 1);
36572c23cb7cSEd Maste 	if (re->vn_s)
36582c23cb7cSEd Maste 		dump_verneed(re, 1);
36592c23cb7cSEd Maste }
36602c23cb7cSEd Maste 
36612c23cb7cSEd Maste static void
36622c23cb7cSEd Maste search_ver(struct readelf *re)
36632c23cb7cSEd Maste {
36642c23cb7cSEd Maste 	struct section *s;
36652c23cb7cSEd Maste 	Elf_Data *d;
36662c23cb7cSEd Maste 	int elferr, i;
36672c23cb7cSEd Maste 
36682c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
36692c23cb7cSEd Maste 		s = &re->sl[i];
36702c23cb7cSEd Maste 		if (s->type == SHT_SUNW_versym)
36712c23cb7cSEd Maste 			re->vs_s = s;
36722c23cb7cSEd Maste 		if (s->type == SHT_SUNW_verneed)
36732c23cb7cSEd Maste 			re->vn_s = s;
36742c23cb7cSEd Maste 		if (s->type == SHT_SUNW_verdef)
36752c23cb7cSEd Maste 			re->vd_s = s;
36762c23cb7cSEd Maste 	}
36772c23cb7cSEd Maste 	if (re->vd_s)
36782c23cb7cSEd Maste 		dump_verdef(re, 0);
36792c23cb7cSEd Maste 	if (re->vn_s)
36802c23cb7cSEd Maste 		dump_verneed(re, 0);
36812c23cb7cSEd Maste 	if (re->vs_s && re->ver != NULL) {
36822c23cb7cSEd Maste 		(void) elf_errno();
36832c23cb7cSEd Maste 		if ((d = elf_getdata(re->vs_s->scn, NULL)) == NULL) {
36842c23cb7cSEd Maste 			elferr = elf_errno();
36852c23cb7cSEd Maste 			if (elferr != 0)
36862c23cb7cSEd Maste 				warnx("elf_getdata failed: %s",
36872c23cb7cSEd Maste 				    elf_errmsg(elferr));
36882c23cb7cSEd Maste 			return;
36892c23cb7cSEd Maste 		}
36902c23cb7cSEd Maste 		if (d->d_size == 0)
36912c23cb7cSEd Maste 			return;
36922c23cb7cSEd Maste 		re->vs = d->d_buf;
36932c23cb7cSEd Maste 		re->vs_sz = d->d_size / sizeof(Elf32_Half);
36942c23cb7cSEd Maste 	}
36952c23cb7cSEd Maste }
36962c23cb7cSEd Maste 
36972c23cb7cSEd Maste #undef	Elf_Verdef
36982c23cb7cSEd Maste #undef	Elf_Verdaux
36992c23cb7cSEd Maste #undef	Elf_Verneed
37002c23cb7cSEd Maste #undef	Elf_Vernaux
37012c23cb7cSEd Maste #undef	SAVE_VERSION_NAME
37022c23cb7cSEd Maste 
37032c23cb7cSEd Maste /*
37042c23cb7cSEd Maste  * Elf32_Lib and Elf64_Lib are identical.
37052c23cb7cSEd Maste  */
37062c23cb7cSEd Maste #define	Elf_Lib		Elf32_Lib
37072c23cb7cSEd Maste 
37082c23cb7cSEd Maste static void
37092c23cb7cSEd Maste dump_liblist(struct readelf *re)
37102c23cb7cSEd Maste {
37112c23cb7cSEd Maste 	struct section *s;
37122c23cb7cSEd Maste 	struct tm *t;
37132c23cb7cSEd Maste 	time_t ti;
37142c23cb7cSEd Maste 	char tbuf[20];
37152c23cb7cSEd Maste 	Elf_Data *d;
37162c23cb7cSEd Maste 	Elf_Lib *lib;
371771edbbfdSEd Maste 	int i, j, k, elferr, first, len;
37182c23cb7cSEd Maste 
37192c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
37202c23cb7cSEd Maste 		s = &re->sl[i];
37212c23cb7cSEd Maste 		if (s->type != SHT_GNU_LIBLIST)
37222c23cb7cSEd Maste 			continue;
3723656f49f8SEd Maste 		if (s->link >= re->shnum)
3724656f49f8SEd Maste 			continue;
37252c23cb7cSEd Maste 		(void) elf_errno();
37262c23cb7cSEd Maste 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
37272c23cb7cSEd Maste 			elferr = elf_errno();
37282c23cb7cSEd Maste 			if (elferr != 0)
37292c23cb7cSEd Maste 				warnx("elf_getdata failed: %s",
37302c23cb7cSEd Maste 				    elf_errmsg(elferr));
37312c23cb7cSEd Maste 			continue;
37322c23cb7cSEd Maste 		}
37332c23cb7cSEd Maste 		if (d->d_size <= 0)
37342c23cb7cSEd Maste 			continue;
37352c23cb7cSEd Maste 		lib = d->d_buf;
373671edbbfdSEd Maste 		if (!get_ent_count(s, &len))
373771edbbfdSEd Maste 			continue;
37382c23cb7cSEd Maste 		printf("\nLibrary list section '%s' ", s->name);
373971edbbfdSEd Maste 		printf("contains %d entries:\n", len);
37402c23cb7cSEd Maste 		printf("%12s%24s%18s%10s%6s\n", "Library", "Time Stamp",
37412c23cb7cSEd Maste 		    "Checksum", "Version", "Flags");
37422c23cb7cSEd Maste 		for (j = 0; (uint64_t) j < s->sz / s->entsize; j++) {
37432c23cb7cSEd Maste 			printf("%3d: ", j);
37442c23cb7cSEd Maste 			printf("%-20.20s ",
37452c23cb7cSEd Maste 			    get_string(re, s->link, lib->l_name));
37462c23cb7cSEd Maste 			ti = lib->l_time_stamp;
37472c23cb7cSEd Maste 			t = gmtime(&ti);
37482c23cb7cSEd Maste 			snprintf(tbuf, sizeof(tbuf), "%04d-%02d-%02dT%02d:%02d"
37492c23cb7cSEd Maste 			    ":%2d", t->tm_year + 1900, t->tm_mon + 1,
37502c23cb7cSEd Maste 			    t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
37512c23cb7cSEd Maste 			printf("%-19.19s ", tbuf);
37522c23cb7cSEd Maste 			printf("0x%08x ", lib->l_checksum);
37532c23cb7cSEd Maste 			printf("%-7d %#x", lib->l_version, lib->l_flags);
37542c23cb7cSEd Maste 			if (lib->l_flags != 0) {
37552c23cb7cSEd Maste 				first = 1;
37562c23cb7cSEd Maste 				putchar('(');
37572c23cb7cSEd Maste 				for (k = 0; l_flag[k].name != NULL; k++) {
37582c23cb7cSEd Maste 					if ((l_flag[k].value & lib->l_flags) ==
37592c23cb7cSEd Maste 					    0)
37602c23cb7cSEd Maste 						continue;
37612c23cb7cSEd Maste 					if (!first)
37622c23cb7cSEd Maste 						putchar(',');
37632c23cb7cSEd Maste 					else
37642c23cb7cSEd Maste 						first = 0;
37652c23cb7cSEd Maste 					printf("%s", l_flag[k].name);
37662c23cb7cSEd Maste 				}
37672c23cb7cSEd Maste 				putchar(')');
37682c23cb7cSEd Maste 			}
37692c23cb7cSEd Maste 			putchar('\n');
37702c23cb7cSEd Maste 			lib++;
37712c23cb7cSEd Maste 		}
37722c23cb7cSEd Maste 	}
37732c23cb7cSEd Maste }
37742c23cb7cSEd Maste 
37752c23cb7cSEd Maste #undef Elf_Lib
37762c23cb7cSEd Maste 
37773ef90571SEd Maste static void
37783ef90571SEd Maste dump_section_groups(struct readelf *re)
37793ef90571SEd Maste {
37803ef90571SEd Maste 	struct section *s;
37813ef90571SEd Maste 	const char *symname;
37823ef90571SEd Maste 	Elf_Data *d;
37833ef90571SEd Maste 	uint32_t *w;
37843ef90571SEd Maste 	int i, j, elferr;
37853ef90571SEd Maste 	size_t n;
37863ef90571SEd Maste 
37873ef90571SEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
37883ef90571SEd Maste 		s = &re->sl[i];
37893ef90571SEd Maste 		if (s->type != SHT_GROUP)
37903ef90571SEd Maste 			continue;
3791656f49f8SEd Maste 		if (s->link >= re->shnum)
3792656f49f8SEd Maste 			continue;
37933ef90571SEd Maste 		(void) elf_errno();
37943ef90571SEd Maste 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
37953ef90571SEd Maste 			elferr = elf_errno();
37963ef90571SEd Maste 			if (elferr != 0)
37973ef90571SEd Maste 				warnx("elf_getdata failed: %s",
37983ef90571SEd Maste 				    elf_errmsg(elferr));
37993ef90571SEd Maste 			continue;
38003ef90571SEd Maste 		}
38013ef90571SEd Maste 		if (d->d_size <= 0)
38023ef90571SEd Maste 			continue;
38033ef90571SEd Maste 
38043ef90571SEd Maste 		w = d->d_buf;
38053ef90571SEd Maste 
38063ef90571SEd Maste 		/* We only support COMDAT section. */
38073ef90571SEd Maste #ifndef GRP_COMDAT
38083ef90571SEd Maste #define	GRP_COMDAT 0x1
38093ef90571SEd Maste #endif
38103ef90571SEd Maste 		if ((*w++ & GRP_COMDAT) == 0)
38113ef90571SEd Maste 			return;
38123ef90571SEd Maste 
38133ef90571SEd Maste 		if (s->entsize == 0)
38143ef90571SEd Maste 			s->entsize = 4;
38153ef90571SEd Maste 
38163ef90571SEd Maste 		symname = get_symbol_name(re, s->link, s->info);
38173ef90571SEd Maste 		n = s->sz / s->entsize;
38183ef90571SEd Maste 		if (n-- < 1)
38193ef90571SEd Maste 			return;
38203ef90571SEd Maste 
38213ef90571SEd Maste 		printf("\nCOMDAT group section [%5d] `%s' [%s] contains %ju"
38223ef90571SEd Maste 		    " sections:\n", i, s->name, symname, (uintmax_t)n);
38233ef90571SEd Maste 		printf("   %-10.10s %s\n", "[Index]", "Name");
38243ef90571SEd Maste 		for (j = 0; (size_t) j < n; j++, w++) {
38253ef90571SEd Maste 			if (*w >= re->shnum) {
38263ef90571SEd Maste 				warnx("invalid section index: %u", *w);
38273ef90571SEd Maste 				continue;
38283ef90571SEd Maste 			}
38293ef90571SEd Maste 			printf("   [%5u]   %s\n", *w, re->sl[*w].name);
38303ef90571SEd Maste 		}
38313ef90571SEd Maste 	}
38323ef90571SEd Maste }
38333ef90571SEd Maste 
38342c23cb7cSEd Maste static uint8_t *
383595fd7f26SEd Maste dump_unknown_tag(uint64_t tag, uint8_t *p, uint8_t *pe)
38362c23cb7cSEd Maste {
38372c23cb7cSEd Maste 	uint64_t val;
38382c23cb7cSEd Maste 
38392c23cb7cSEd Maste 	/*
38402c23cb7cSEd Maste 	 * According to ARM EABI: For tags > 32, even numbered tags have
38412c23cb7cSEd Maste 	 * a ULEB128 param and odd numbered ones have NUL-terminated
38422c23cb7cSEd Maste 	 * string param. This rule probably also applies for tags <= 32
38432c23cb7cSEd Maste 	 * if the object arch is not ARM.
38442c23cb7cSEd Maste 	 */
38452c23cb7cSEd Maste 
38462c23cb7cSEd Maste 	printf("  Tag_unknown_%ju: ", (uintmax_t) tag);
38472c23cb7cSEd Maste 
38482c23cb7cSEd Maste 	if (tag & 1) {
38492c23cb7cSEd Maste 		printf("%s\n", (char *) p);
38502c23cb7cSEd Maste 		p += strlen((char *) p) + 1;
38512c23cb7cSEd Maste 	} else {
385295fd7f26SEd Maste 		val = _decode_uleb128(&p, pe);
38532c23cb7cSEd Maste 		printf("%ju\n", (uintmax_t) val);
38542c23cb7cSEd Maste 	}
38552c23cb7cSEd Maste 
38562c23cb7cSEd Maste 	return (p);
38572c23cb7cSEd Maste }
38582c23cb7cSEd Maste 
38592c23cb7cSEd Maste static uint8_t *
386095fd7f26SEd Maste dump_compatibility_tag(uint8_t *p, uint8_t *pe)
38612c23cb7cSEd Maste {
38622c23cb7cSEd Maste 	uint64_t val;
38632c23cb7cSEd Maste 
386495fd7f26SEd Maste 	val = _decode_uleb128(&p, pe);
3865839529caSEd Maste 	printf("flag = %ju, vendor = %s\n", (uintmax_t) val, p);
38662c23cb7cSEd Maste 	p += strlen((char *) p) + 1;
38672c23cb7cSEd Maste 
38682c23cb7cSEd Maste 	return (p);
38692c23cb7cSEd Maste }
38702c23cb7cSEd Maste 
38712c23cb7cSEd Maste static void
38722c23cb7cSEd Maste dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe)
38732c23cb7cSEd Maste {
38742c23cb7cSEd Maste 	uint64_t tag, val;
38752c23cb7cSEd Maste 	size_t i;
38762c23cb7cSEd Maste 	int found, desc;
38772c23cb7cSEd Maste 
38782c23cb7cSEd Maste 	(void) re;
38792c23cb7cSEd Maste 
38802c23cb7cSEd Maste 	while (p < pe) {
388195fd7f26SEd Maste 		tag = _decode_uleb128(&p, pe);
38822c23cb7cSEd Maste 		found = desc = 0;
38832c23cb7cSEd Maste 		for (i = 0; i < sizeof(aeabi_tags) / sizeof(aeabi_tags[0]);
38842c23cb7cSEd Maste 		     i++) {
38852c23cb7cSEd Maste 			if (tag == aeabi_tags[i].tag) {
38862c23cb7cSEd Maste 				found = 1;
38872c23cb7cSEd Maste 				printf("  %s: ", aeabi_tags[i].s_tag);
38882c23cb7cSEd Maste 				if (aeabi_tags[i].get_desc) {
38892c23cb7cSEd Maste 					desc = 1;
389095fd7f26SEd Maste 					val = _decode_uleb128(&p, pe);
38912c23cb7cSEd Maste 					printf("%s\n",
38922c23cb7cSEd Maste 					    aeabi_tags[i].get_desc(val));
38932c23cb7cSEd Maste 				}
38942c23cb7cSEd Maste 				break;
38952c23cb7cSEd Maste 			}
38962c23cb7cSEd Maste 			if (tag < aeabi_tags[i].tag)
38972c23cb7cSEd Maste 				break;
38982c23cb7cSEd Maste 		}
38992c23cb7cSEd Maste 		if (!found) {
390095fd7f26SEd Maste 			p = dump_unknown_tag(tag, p, pe);
39012c23cb7cSEd Maste 			continue;
39022c23cb7cSEd Maste 		}
39032c23cb7cSEd Maste 		if (desc)
39042c23cb7cSEd Maste 			continue;
39052c23cb7cSEd Maste 
39062c23cb7cSEd Maste 		switch (tag) {
39072c23cb7cSEd Maste 		case 4:		/* Tag_CPU_raw_name */
39082c23cb7cSEd Maste 		case 5:		/* Tag_CPU_name */
39092c23cb7cSEd Maste 		case 67:	/* Tag_conformance */
39102c23cb7cSEd Maste 			printf("%s\n", (char *) p);
39112c23cb7cSEd Maste 			p += strlen((char *) p) + 1;
39122c23cb7cSEd Maste 			break;
39132c23cb7cSEd Maste 		case 32:	/* Tag_compatibility */
391495fd7f26SEd Maste 			p = dump_compatibility_tag(p, pe);
39152c23cb7cSEd Maste 			break;
39162c23cb7cSEd Maste 		case 64:	/* Tag_nodefaults */
39172c23cb7cSEd Maste 			/* ignored, written as 0. */
391895fd7f26SEd Maste 			(void) _decode_uleb128(&p, pe);
39192c23cb7cSEd Maste 			printf("True\n");
39202c23cb7cSEd Maste 			break;
39212c23cb7cSEd Maste 		case 65:	/* Tag_also_compatible_with */
392295fd7f26SEd Maste 			val = _decode_uleb128(&p, pe);
39232c23cb7cSEd Maste 			/* Must be Tag_CPU_arch */
39242c23cb7cSEd Maste 			if (val != 6) {
39252c23cb7cSEd Maste 				printf("unknown\n");
39262c23cb7cSEd Maste 				break;
39272c23cb7cSEd Maste 			}
392895fd7f26SEd Maste 			val = _decode_uleb128(&p, pe);
39292c23cb7cSEd Maste 			printf("%s\n", aeabi_cpu_arch(val));
39302c23cb7cSEd Maste 			/* Skip NUL terminator. */
39312c23cb7cSEd Maste 			p++;
39322c23cb7cSEd Maste 			break;
39332c23cb7cSEd Maste 		default:
39342c23cb7cSEd Maste 			putchar('\n');
39352c23cb7cSEd Maste 			break;
39362c23cb7cSEd Maste 		}
39372c23cb7cSEd Maste 	}
39382c23cb7cSEd Maste }
39392c23cb7cSEd Maste 
39402c23cb7cSEd Maste #ifndef	Tag_GNU_MIPS_ABI_FP
39412c23cb7cSEd Maste #define	Tag_GNU_MIPS_ABI_FP	4
39422c23cb7cSEd Maste #endif
39432c23cb7cSEd Maste 
39442c23cb7cSEd Maste static void
39452c23cb7cSEd Maste dump_mips_attributes(struct readelf *re, uint8_t *p, uint8_t *pe)
39462c23cb7cSEd Maste {
39472c23cb7cSEd Maste 	uint64_t tag, val;
39482c23cb7cSEd Maste 
39492c23cb7cSEd Maste 	(void) re;
39502c23cb7cSEd Maste 
39512c23cb7cSEd Maste 	while (p < pe) {
395295fd7f26SEd Maste 		tag = _decode_uleb128(&p, pe);
39532c23cb7cSEd Maste 		switch (tag) {
39542c23cb7cSEd Maste 		case Tag_GNU_MIPS_ABI_FP:
395595fd7f26SEd Maste 			val = _decode_uleb128(&p, pe);
39562c23cb7cSEd Maste 			printf("  Tag_GNU_MIPS_ABI_FP: %s\n", mips_abi_fp(val));
39572c23cb7cSEd Maste 			break;
39582c23cb7cSEd Maste 		case 32:	/* Tag_compatibility */
395995fd7f26SEd Maste 			p = dump_compatibility_tag(p, pe);
39602c23cb7cSEd Maste 			break;
39612c23cb7cSEd Maste 		default:
396295fd7f26SEd Maste 			p = dump_unknown_tag(tag, p, pe);
39632c23cb7cSEd Maste 			break;
39642c23cb7cSEd Maste 		}
39652c23cb7cSEd Maste 	}
39662c23cb7cSEd Maste }
39672c23cb7cSEd Maste 
39682c23cb7cSEd Maste #ifndef Tag_GNU_Power_ABI_FP
39692c23cb7cSEd Maste #define	Tag_GNU_Power_ABI_FP	4
39702c23cb7cSEd Maste #endif
39712c23cb7cSEd Maste 
39722c23cb7cSEd Maste #ifndef Tag_GNU_Power_ABI_Vector
39732c23cb7cSEd Maste #define	Tag_GNU_Power_ABI_Vector	8
39742c23cb7cSEd Maste #endif
39752c23cb7cSEd Maste 
39762c23cb7cSEd Maste static void
39772c23cb7cSEd Maste dump_ppc_attributes(uint8_t *p, uint8_t *pe)
39782c23cb7cSEd Maste {
39792c23cb7cSEd Maste 	uint64_t tag, val;
39802c23cb7cSEd Maste 
39812c23cb7cSEd Maste 	while (p < pe) {
398295fd7f26SEd Maste 		tag = _decode_uleb128(&p, pe);
39832c23cb7cSEd Maste 		switch (tag) {
39842c23cb7cSEd Maste 		case Tag_GNU_Power_ABI_FP:
398595fd7f26SEd Maste 			val = _decode_uleb128(&p, pe);
39862c23cb7cSEd Maste 			printf("  Tag_GNU_Power_ABI_FP: %s\n", ppc_abi_fp(val));
39872c23cb7cSEd Maste 			break;
39882c23cb7cSEd Maste 		case Tag_GNU_Power_ABI_Vector:
398995fd7f26SEd Maste 			val = _decode_uleb128(&p, pe);
39902c23cb7cSEd Maste 			printf("  Tag_GNU_Power_ABI_Vector: %s\n",
39912c23cb7cSEd Maste 			    ppc_abi_vector(val));
39922c23cb7cSEd Maste 			break;
39932c23cb7cSEd Maste 		case 32:	/* Tag_compatibility */
399495fd7f26SEd Maste 			p = dump_compatibility_tag(p, pe);
39952c23cb7cSEd Maste 			break;
39962c23cb7cSEd Maste 		default:
399795fd7f26SEd Maste 			p = dump_unknown_tag(tag, p, pe);
39982c23cb7cSEd Maste 			break;
39992c23cb7cSEd Maste 		}
40002c23cb7cSEd Maste 	}
40012c23cb7cSEd Maste }
40022c23cb7cSEd Maste 
40032c23cb7cSEd Maste static void
40042c23cb7cSEd Maste dump_attributes(struct readelf *re)
40052c23cb7cSEd Maste {
40062c23cb7cSEd Maste 	struct section *s;
40072c23cb7cSEd Maste 	Elf_Data *d;
400895fd7f26SEd Maste 	uint8_t *p, *pe, *sp;
40092c23cb7cSEd Maste 	size_t len, seclen, nlen, sublen;
40102c23cb7cSEd Maste 	uint64_t val;
40112c23cb7cSEd Maste 	int tag, i, elferr;
40122c23cb7cSEd Maste 
40132c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
40142c23cb7cSEd Maste 		s = &re->sl[i];
40152c23cb7cSEd Maste 		if (s->type != SHT_GNU_ATTRIBUTES &&
40162c23cb7cSEd Maste 		    (re->ehdr.e_machine != EM_ARM || s->type != SHT_LOPROC + 3))
40172c23cb7cSEd Maste 			continue;
40182c23cb7cSEd Maste 		(void) elf_errno();
40192c23cb7cSEd Maste 		if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
40202c23cb7cSEd Maste 			elferr = elf_errno();
40212c23cb7cSEd Maste 			if (elferr != 0)
40222c23cb7cSEd Maste 				warnx("elf_rawdata failed: %s",
40232c23cb7cSEd Maste 				    elf_errmsg(elferr));
40242c23cb7cSEd Maste 			continue;
40252c23cb7cSEd Maste 		}
40262c23cb7cSEd Maste 		if (d->d_size <= 0)
40272c23cb7cSEd Maste 			continue;
40282c23cb7cSEd Maste 		p = d->d_buf;
402995fd7f26SEd Maste 		pe = p + d->d_size;
40302c23cb7cSEd Maste 		if (*p != 'A') {
40312c23cb7cSEd Maste 			printf("Unknown Attribute Section Format: %c\n",
40322c23cb7cSEd Maste 			    (char) *p);
40332c23cb7cSEd Maste 			continue;
40342c23cb7cSEd Maste 		}
40352c23cb7cSEd Maste 		len = d->d_size - 1;
40362c23cb7cSEd Maste 		p++;
40372c23cb7cSEd Maste 		while (len > 0) {
403871a0c925SEd Maste 			if (len < 4) {
403971a0c925SEd Maste 				warnx("truncated attribute section length");
404095fd7f26SEd Maste 				return;
404171a0c925SEd Maste 			}
40422c23cb7cSEd Maste 			seclen = re->dw_decode(&p, 4);
40432c23cb7cSEd Maste 			if (seclen > len) {
40442c23cb7cSEd Maste 				warnx("invalid attribute section length");
404595fd7f26SEd Maste 				return;
40462c23cb7cSEd Maste 			}
40472c23cb7cSEd Maste 			len -= seclen;
40482c23cb7cSEd Maste 			nlen = strlen((char *) p) + 1;
404971a0c925SEd Maste 			if (nlen + 4 > seclen) {
405071a0c925SEd Maste 				warnx("invalid attribute section name");
405195fd7f26SEd Maste 				return;
405271a0c925SEd Maste 			}
405371a0c925SEd Maste 			printf("Attribute Section: %s\n", (char *) p);
40542c23cb7cSEd Maste 			p += nlen;
40552c23cb7cSEd Maste 			seclen -= nlen + 4;
40562c23cb7cSEd Maste 			while (seclen > 0) {
40572c23cb7cSEd Maste 				sp = p;
40582c23cb7cSEd Maste 				tag = *p++;
40592c23cb7cSEd Maste 				sublen = re->dw_decode(&p, 4);
40602c23cb7cSEd Maste 				if (sublen > seclen) {
40612c23cb7cSEd Maste 					warnx("invalid attribute sub-section"
40622c23cb7cSEd Maste 					    " length");
406395fd7f26SEd Maste 					return;
40642c23cb7cSEd Maste 				}
40652c23cb7cSEd Maste 				seclen -= sublen;
40662c23cb7cSEd Maste 				printf("%s", top_tag(tag));
40672c23cb7cSEd Maste 				if (tag == 2 || tag == 3) {
40682c23cb7cSEd Maste 					putchar(':');
40692c23cb7cSEd Maste 					for (;;) {
407095fd7f26SEd Maste 						val = _decode_uleb128(&p, pe);
40712c23cb7cSEd Maste 						if (val == 0)
40722c23cb7cSEd Maste 							break;
40732c23cb7cSEd Maste 						printf(" %ju", (uintmax_t) val);
40742c23cb7cSEd Maste 					}
40752c23cb7cSEd Maste 				}
40762c23cb7cSEd Maste 				putchar('\n');
40772c23cb7cSEd Maste 				if (re->ehdr.e_machine == EM_ARM &&
40782c23cb7cSEd Maste 				    s->type == SHT_LOPROC + 3)
40792c23cb7cSEd Maste 					dump_arm_attributes(re, p, sp + sublen);
40802c23cb7cSEd Maste 				else if (re->ehdr.e_machine == EM_MIPS ||
40812c23cb7cSEd Maste 				    re->ehdr.e_machine == EM_MIPS_RS3_LE)
40822c23cb7cSEd Maste 					dump_mips_attributes(re, p,
40832c23cb7cSEd Maste 					    sp + sublen);
40842c23cb7cSEd Maste 				else if (re->ehdr.e_machine == EM_PPC)
40852c23cb7cSEd Maste 					dump_ppc_attributes(p, sp + sublen);
40862c23cb7cSEd Maste 				p = sp + sublen;
40872c23cb7cSEd Maste 			}
40882c23cb7cSEd Maste 		}
40892c23cb7cSEd Maste 	}
40902c23cb7cSEd Maste }
40912c23cb7cSEd Maste 
40922c23cb7cSEd Maste static void
40932c23cb7cSEd Maste dump_mips_specific_info(struct readelf *re)
40942c23cb7cSEd Maste {
40952c23cb7cSEd Maste 	struct section *s;
40962c23cb7cSEd Maste 	int i, options_found;
40972c23cb7cSEd Maste 
40982c23cb7cSEd Maste 	options_found = 0;
40992c23cb7cSEd Maste 	s = NULL;
41002c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
41012c23cb7cSEd Maste 		s = &re->sl[i];
41022c23cb7cSEd Maste 		if (s->name != NULL && (!strcmp(s->name, ".MIPS.options") ||
41032c23cb7cSEd Maste 		    (s->type == SHT_MIPS_OPTIONS))) {
41042c23cb7cSEd Maste 			dump_mips_options(re, s);
41052c23cb7cSEd Maste 			options_found = 1;
41062c23cb7cSEd Maste 		}
41072c23cb7cSEd Maste 	}
41082c23cb7cSEd Maste 
41092c23cb7cSEd Maste 	/*
41102c23cb7cSEd Maste 	 * According to SGI mips64 spec, .reginfo should be ignored if
41112c23cb7cSEd Maste 	 * .MIPS.options section is present.
41122c23cb7cSEd Maste 	 */
41132c23cb7cSEd Maste 	if (!options_found) {
41142c23cb7cSEd Maste 		for (i = 0; (size_t) i < re->shnum; i++) {
41152c23cb7cSEd Maste 			s = &re->sl[i];
41162c23cb7cSEd Maste 			if (s->name != NULL && (!strcmp(s->name, ".reginfo") ||
41172c23cb7cSEd Maste 			    (s->type == SHT_MIPS_REGINFO)))
41182c23cb7cSEd Maste 				dump_mips_reginfo(re, s);
41192c23cb7cSEd Maste 		}
41202c23cb7cSEd Maste 	}
41212c23cb7cSEd Maste }
41222c23cb7cSEd Maste 
41232c23cb7cSEd Maste static void
41242c23cb7cSEd Maste dump_mips_reginfo(struct readelf *re, struct section *s)
41252c23cb7cSEd Maste {
41262c23cb7cSEd Maste 	Elf_Data *d;
412771edbbfdSEd Maste 	int elferr, len;
41282c23cb7cSEd Maste 
41292c23cb7cSEd Maste 	(void) elf_errno();
41302c23cb7cSEd Maste 	if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
41312c23cb7cSEd Maste 		elferr = elf_errno();
41322c23cb7cSEd Maste 		if (elferr != 0)
41332c23cb7cSEd Maste 			warnx("elf_rawdata failed: %s",
41342c23cb7cSEd Maste 			    elf_errmsg(elferr));
41352c23cb7cSEd Maste 		return;
41362c23cb7cSEd Maste 	}
41372c23cb7cSEd Maste 	if (d->d_size <= 0)
41382c23cb7cSEd Maste 		return;
413971edbbfdSEd Maste 	if (!get_ent_count(s, &len))
414071edbbfdSEd Maste 		return;
41412c23cb7cSEd Maste 
414271edbbfdSEd Maste 	printf("\nSection '%s' contains %d entries:\n", s->name, len);
41432c23cb7cSEd Maste 	dump_mips_odk_reginfo(re, d->d_buf, d->d_size);
41442c23cb7cSEd Maste }
41452c23cb7cSEd Maste 
41462c23cb7cSEd Maste static void
41472c23cb7cSEd Maste dump_mips_options(struct readelf *re, struct section *s)
41482c23cb7cSEd Maste {
41492c23cb7cSEd Maste 	Elf_Data *d;
41502c23cb7cSEd Maste 	uint32_t info;
41512c23cb7cSEd Maste 	uint16_t sndx;
41522c23cb7cSEd Maste 	uint8_t *p, *pe;
41532c23cb7cSEd Maste 	uint8_t kind, size;
41542c23cb7cSEd Maste 	int elferr;
41552c23cb7cSEd Maste 
41562c23cb7cSEd Maste 	(void) elf_errno();
41572c23cb7cSEd Maste 	if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
41582c23cb7cSEd Maste 		elferr = elf_errno();
41592c23cb7cSEd Maste 		if (elferr != 0)
41602c23cb7cSEd Maste 			warnx("elf_rawdata failed: %s",
41612c23cb7cSEd Maste 			    elf_errmsg(elferr));
41622c23cb7cSEd Maste 		return;
41632c23cb7cSEd Maste 	}
41642c23cb7cSEd Maste 	if (d->d_size == 0)
41652c23cb7cSEd Maste 		return;
41662c23cb7cSEd Maste 
41672c23cb7cSEd Maste 	printf("\nSection %s contains:\n", s->name);
41682c23cb7cSEd Maste 	p = d->d_buf;
41692c23cb7cSEd Maste 	pe = p + d->d_size;
41702c23cb7cSEd Maste 	while (p < pe) {
4171b00fe64fSEd Maste 		if (pe - p < 8) {
4172b00fe64fSEd Maste 			warnx("Truncated MIPS option header");
4173b00fe64fSEd Maste 			return;
4174b00fe64fSEd Maste 		}
41752c23cb7cSEd Maste 		kind = re->dw_decode(&p, 1);
41762c23cb7cSEd Maste 		size = re->dw_decode(&p, 1);
41772c23cb7cSEd Maste 		sndx = re->dw_decode(&p, 2);
41782c23cb7cSEd Maste 		info = re->dw_decode(&p, 4);
4179b00fe64fSEd Maste 		if (size < 8 || size - 8 > pe - p) {
4180b00fe64fSEd Maste 			warnx("Malformed MIPS option header");
4181b00fe64fSEd Maste 			return;
4182b00fe64fSEd Maste 		}
4183b00fe64fSEd Maste 		size -= 8;
41842c23cb7cSEd Maste 		switch (kind) {
41852c23cb7cSEd Maste 		case ODK_REGINFO:
4186b00fe64fSEd Maste 			dump_mips_odk_reginfo(re, p, size);
41872c23cb7cSEd Maste 			break;
41882c23cb7cSEd Maste 		case ODK_EXCEPTIONS:
41892c23cb7cSEd Maste 			printf(" EXCEPTIONS FPU_MIN: %#x\n",
41902c23cb7cSEd Maste 			    info & OEX_FPU_MIN);
41912c23cb7cSEd Maste 			printf("%11.11s FPU_MAX: %#x\n", "",
41922c23cb7cSEd Maste 			    info & OEX_FPU_MAX);
41932c23cb7cSEd Maste 			dump_mips_option_flags("", mips_exceptions_option,
41942c23cb7cSEd Maste 			    info);
41952c23cb7cSEd Maste 			break;
41962c23cb7cSEd Maste 		case ODK_PAD:
41972c23cb7cSEd Maste 			printf(" %-10.10s section: %ju\n", "OPAD",
41982c23cb7cSEd Maste 			    (uintmax_t) sndx);
41992c23cb7cSEd Maste 			dump_mips_option_flags("", mips_pad_option, info);
42002c23cb7cSEd Maste 			break;
42012c23cb7cSEd Maste 		case ODK_HWPATCH:
42022c23cb7cSEd Maste 			dump_mips_option_flags("HWPATCH", mips_hwpatch_option,
42032c23cb7cSEd Maste 			    info);
42042c23cb7cSEd Maste 			break;
42052c23cb7cSEd Maste 		case ODK_HWAND:
42062c23cb7cSEd Maste 			dump_mips_option_flags("HWAND", mips_hwa_option, info);
42072c23cb7cSEd Maste 			break;
42082c23cb7cSEd Maste 		case ODK_HWOR:
42092c23cb7cSEd Maste 			dump_mips_option_flags("HWOR", mips_hwo_option, info);
42102c23cb7cSEd Maste 			break;
42112c23cb7cSEd Maste 		case ODK_FILL:
42122c23cb7cSEd Maste 			printf(" %-10.10s %#jx\n", "FILL", (uintmax_t) info);
42132c23cb7cSEd Maste 			break;
42142c23cb7cSEd Maste 		case ODK_TAGS:
42152c23cb7cSEd Maste 			printf(" %-10.10s\n", "TAGS");
42162c23cb7cSEd Maste 			break;
42172c23cb7cSEd Maste 		case ODK_GP_GROUP:
42182c23cb7cSEd Maste 			printf(" %-10.10s GP group number: %#x\n", "GP_GROUP",
42192c23cb7cSEd Maste 			    info & 0xFFFF);
42202c23cb7cSEd Maste 			if (info & 0x10000)
42212c23cb7cSEd Maste 				printf(" %-10.10s GP group is "
42222c23cb7cSEd Maste 				    "self-contained\n", "");
42232c23cb7cSEd Maste 			break;
42242c23cb7cSEd Maste 		case ODK_IDENT:
42252c23cb7cSEd Maste 			printf(" %-10.10s default GP group number: %#x\n",
42262c23cb7cSEd Maste 			    "IDENT", info & 0xFFFF);
42272c23cb7cSEd Maste 			if (info & 0x10000)
42282c23cb7cSEd Maste 				printf(" %-10.10s default GP group is "
42292c23cb7cSEd Maste 				    "self-contained\n", "");
42302c23cb7cSEd Maste 			break;
42312c23cb7cSEd Maste 		case ODK_PAGESIZE:
42322c23cb7cSEd Maste 			printf(" %-10.10s\n", "PAGESIZE");
42332c23cb7cSEd Maste 			break;
42342c23cb7cSEd Maste 		default:
42352c23cb7cSEd Maste 			break;
42362c23cb7cSEd Maste 		}
4237b00fe64fSEd Maste 		p += size;
42382c23cb7cSEd Maste 	}
42392c23cb7cSEd Maste }
42402c23cb7cSEd Maste 
42412c23cb7cSEd Maste static void
42422c23cb7cSEd Maste dump_mips_option_flags(const char *name, struct mips_option *opt, uint64_t info)
42432c23cb7cSEd Maste {
42442c23cb7cSEd Maste 	int first;
42452c23cb7cSEd Maste 
42462c23cb7cSEd Maste 	first = 1;
42472c23cb7cSEd Maste 	for (; opt->desc != NULL; opt++) {
42482c23cb7cSEd Maste 		if (info & opt->flag) {
42492c23cb7cSEd Maste 			printf(" %-10.10s %s\n", first ? name : "",
42502c23cb7cSEd Maste 			    opt->desc);
42512c23cb7cSEd Maste 			first = 0;
42522c23cb7cSEd Maste 		}
42532c23cb7cSEd Maste 	}
42542c23cb7cSEd Maste }
42552c23cb7cSEd Maste 
42562c23cb7cSEd Maste static void
42572c23cb7cSEd Maste dump_mips_odk_reginfo(struct readelf *re, uint8_t *p, size_t sz)
42582c23cb7cSEd Maste {
42592c23cb7cSEd Maste 	uint32_t ri_gprmask;
42602c23cb7cSEd Maste 	uint32_t ri_cprmask[4];
42612c23cb7cSEd Maste 	uint64_t ri_gp_value;
42622c23cb7cSEd Maste 	uint8_t *pe;
42632c23cb7cSEd Maste 	int i;
42642c23cb7cSEd Maste 
42652c23cb7cSEd Maste 	pe = p + sz;
42662c23cb7cSEd Maste 	while (p < pe) {
42672c23cb7cSEd Maste 		ri_gprmask = re->dw_decode(&p, 4);
42682c23cb7cSEd Maste 		/* Skip ri_pad padding field for mips64. */
42692c23cb7cSEd Maste 		if (re->ec == ELFCLASS64)
42702c23cb7cSEd Maste 			re->dw_decode(&p, 4);
42712c23cb7cSEd Maste 		for (i = 0; i < 4; i++)
42722c23cb7cSEd Maste 			ri_cprmask[i] = re->dw_decode(&p, 4);
42732c23cb7cSEd Maste 		if (re->ec == ELFCLASS32)
42742c23cb7cSEd Maste 			ri_gp_value = re->dw_decode(&p, 4);
42752c23cb7cSEd Maste 		else
42762c23cb7cSEd Maste 			ri_gp_value = re->dw_decode(&p, 8);
42772c23cb7cSEd Maste 		printf(" %s    ", option_kind(ODK_REGINFO));
42782c23cb7cSEd Maste 		printf("ri_gprmask:    0x%08jx\n", (uintmax_t) ri_gprmask);
42792c23cb7cSEd Maste 		for (i = 0; i < 4; i++)
42802c23cb7cSEd Maste 			printf("%11.11s ri_cprmask[%d]: 0x%08jx\n", "", i,
42812c23cb7cSEd Maste 			    (uintmax_t) ri_cprmask[i]);
42822c23cb7cSEd Maste 		printf("%12.12s", "");
42832c23cb7cSEd Maste 		printf("ri_gp_value:   %#jx\n", (uintmax_t) ri_gp_value);
42842c23cb7cSEd Maste 	}
42852c23cb7cSEd Maste }
42862c23cb7cSEd Maste 
42872c23cb7cSEd Maste static void
42882c23cb7cSEd Maste dump_arch_specific_info(struct readelf *re)
42892c23cb7cSEd Maste {
42902c23cb7cSEd Maste 
42912c23cb7cSEd Maste 	dump_liblist(re);
42922c23cb7cSEd Maste 	dump_attributes(re);
42932c23cb7cSEd Maste 
42942c23cb7cSEd Maste 	switch (re->ehdr.e_machine) {
42952c23cb7cSEd Maste 	case EM_MIPS:
42962c23cb7cSEd Maste 	case EM_MIPS_RS3_LE:
42972c23cb7cSEd Maste 		dump_mips_specific_info(re);
42982c23cb7cSEd Maste 	default:
42992c23cb7cSEd Maste 		break;
43002c23cb7cSEd Maste 	}
43012c23cb7cSEd Maste }
43022c23cb7cSEd Maste 
4303cf781b2eSEd Maste static const char *
4304cf781b2eSEd Maste dwarf_regname(struct readelf *re, unsigned int num)
4305cf781b2eSEd Maste {
4306cf781b2eSEd Maste 	static char rx[32];
4307cf781b2eSEd Maste 	const char *rn;
4308cf781b2eSEd Maste 
4309cf781b2eSEd Maste 	if ((rn = dwarf_reg(re->ehdr.e_machine, num)) != NULL)
4310cf781b2eSEd Maste 		return (rn);
4311cf781b2eSEd Maste 
4312cf781b2eSEd Maste 	snprintf(rx, sizeof(rx), "r%u", num);
4313cf781b2eSEd Maste 
4314cf781b2eSEd Maste 	return (rx);
4315cf781b2eSEd Maste }
4316cf781b2eSEd Maste 
43172c23cb7cSEd Maste static void
43182c23cb7cSEd Maste dump_dwarf_line(struct readelf *re)
43192c23cb7cSEd Maste {
43202c23cb7cSEd Maste 	struct section *s;
43212c23cb7cSEd Maste 	Dwarf_Die die;
43222c23cb7cSEd Maste 	Dwarf_Error de;
43232c23cb7cSEd Maste 	Dwarf_Half tag, version, pointer_size;
43242c23cb7cSEd Maste 	Dwarf_Unsigned offset, endoff, length, hdrlen, dirndx, mtime, fsize;
43252c23cb7cSEd Maste 	Dwarf_Small minlen, defstmt, lrange, opbase, oplen;
43262c23cb7cSEd Maste 	Elf_Data *d;
43272c23cb7cSEd Maste 	char *pn;
43282c23cb7cSEd Maste 	uint64_t address, file, line, column, isa, opsize, udelta;
43292c23cb7cSEd Maste 	int64_t sdelta;
43302c23cb7cSEd Maste 	uint8_t *p, *pe;
43312c23cb7cSEd Maste 	int8_t lbase;
4332cf781b2eSEd Maste 	int i, is_stmt, dwarf_size, elferr, ret;
43332c23cb7cSEd Maste 
43342c23cb7cSEd Maste 	printf("\nDump of debug contents of section .debug_line:\n");
43352c23cb7cSEd Maste 
43362c23cb7cSEd Maste 	s = NULL;
43372c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
43382c23cb7cSEd Maste 		s = &re->sl[i];
43392c23cb7cSEd Maste 		if (s->name != NULL && !strcmp(s->name, ".debug_line"))
43402c23cb7cSEd Maste 			break;
43412c23cb7cSEd Maste 	}
43422c23cb7cSEd Maste 	if ((size_t) i >= re->shnum)
43432c23cb7cSEd Maste 		return;
43442c23cb7cSEd Maste 
43452c23cb7cSEd Maste 	(void) elf_errno();
43462c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
43472c23cb7cSEd Maste 		elferr = elf_errno();
43482c23cb7cSEd Maste 		if (elferr != 0)
43492c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
43502c23cb7cSEd Maste 		return;
43512c23cb7cSEd Maste 	}
43522c23cb7cSEd Maste 	if (d->d_size <= 0)
43532c23cb7cSEd Maste 		return;
43542c23cb7cSEd Maste 
43552c23cb7cSEd Maste 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
43562c23cb7cSEd Maste 	    NULL, &de)) ==  DW_DLV_OK) {
43572c23cb7cSEd Maste 		die = NULL;
43582c23cb7cSEd Maste 		while (dwarf_siblingof(re->dbg, die, &die, &de) == DW_DLV_OK) {
43592c23cb7cSEd Maste 			if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
43602c23cb7cSEd Maste 				warnx("dwarf_tag failed: %s",
43612c23cb7cSEd Maste 				    dwarf_errmsg(de));
43622c23cb7cSEd Maste 				return;
43632c23cb7cSEd Maste 			}
43642c23cb7cSEd Maste 			/* XXX: What about DW_TAG_partial_unit? */
43652c23cb7cSEd Maste 			if (tag == DW_TAG_compile_unit)
43662c23cb7cSEd Maste 				break;
43672c23cb7cSEd Maste 		}
43682c23cb7cSEd Maste 		if (die == NULL) {
43692c23cb7cSEd Maste 			warnx("could not find DW_TAG_compile_unit die");
43702c23cb7cSEd Maste 			return;
43712c23cb7cSEd Maste 		}
43722c23cb7cSEd Maste 		if (dwarf_attrval_unsigned(die, DW_AT_stmt_list, &offset,
43732c23cb7cSEd Maste 		    &de) != DW_DLV_OK)
43742c23cb7cSEd Maste 			continue;
43752c23cb7cSEd Maste 
43762c23cb7cSEd Maste 		length = re->dw_read(d, &offset, 4);
43772c23cb7cSEd Maste 		if (length == 0xffffffff) {
43782c23cb7cSEd Maste 			dwarf_size = 8;
43792c23cb7cSEd Maste 			length = re->dw_read(d, &offset, 8);
43802c23cb7cSEd Maste 		} else
43812c23cb7cSEd Maste 			dwarf_size = 4;
43822c23cb7cSEd Maste 
43832c23cb7cSEd Maste 		if (length > d->d_size - offset) {
43842c23cb7cSEd Maste 			warnx("invalid .dwarf_line section");
43852c23cb7cSEd Maste 			continue;
43862c23cb7cSEd Maste 		}
43872c23cb7cSEd Maste 
43882c23cb7cSEd Maste 		endoff = offset + length;
43898f32e46dSKai Wang 		pe = (uint8_t *) d->d_buf + endoff;
43902c23cb7cSEd Maste 		version = re->dw_read(d, &offset, 2);
43912c23cb7cSEd Maste 		hdrlen = re->dw_read(d, &offset, dwarf_size);
43922c23cb7cSEd Maste 		minlen = re->dw_read(d, &offset, 1);
43932c23cb7cSEd Maste 		defstmt = re->dw_read(d, &offset, 1);
43942c23cb7cSEd Maste 		lbase = re->dw_read(d, &offset, 1);
43952c23cb7cSEd Maste 		lrange = re->dw_read(d, &offset, 1);
43962c23cb7cSEd Maste 		opbase = re->dw_read(d, &offset, 1);
43972c23cb7cSEd Maste 
43982c23cb7cSEd Maste 		printf("\n");
43992c23cb7cSEd Maste 		printf("  Length:\t\t\t%ju\n", (uintmax_t) length);
44002c23cb7cSEd Maste 		printf("  DWARF version:\t\t%u\n", version);
44012c23cb7cSEd Maste 		printf("  Prologue Length:\t\t%ju\n", (uintmax_t) hdrlen);
44022c23cb7cSEd Maste 		printf("  Minimum Instruction Length:\t%u\n", minlen);
44032c23cb7cSEd Maste 		printf("  Initial value of 'is_stmt':\t%u\n", defstmt);
44042c23cb7cSEd Maste 		printf("  Line Base:\t\t\t%d\n", lbase);
44052c23cb7cSEd Maste 		printf("  Line Range:\t\t\t%u\n", lrange);
44062c23cb7cSEd Maste 		printf("  Opcode Base:\t\t\t%u\n", opbase);
44072c23cb7cSEd Maste 		(void) dwarf_get_address_size(re->dbg, &pointer_size, &de);
44082c23cb7cSEd Maste 		printf("  (Pointer size:\t\t%u)\n", pointer_size);
44092c23cb7cSEd Maste 
44102c23cb7cSEd Maste 		printf("\n");
44112c23cb7cSEd Maste 		printf(" Opcodes:\n");
44122c23cb7cSEd Maste 		for (i = 1; i < opbase; i++) {
44132c23cb7cSEd Maste 			oplen = re->dw_read(d, &offset, 1);
44142c23cb7cSEd Maste 			printf("  Opcode %d has %u args\n", i, oplen);
44152c23cb7cSEd Maste 		}
44162c23cb7cSEd Maste 
44172c23cb7cSEd Maste 		printf("\n");
44182c23cb7cSEd Maste 		printf(" The Directory Table:\n");
44192c23cb7cSEd Maste 		p = (uint8_t *) d->d_buf + offset;
44202c23cb7cSEd Maste 		while (*p != '\0') {
44212c23cb7cSEd Maste 			printf("  %s\n", (char *) p);
44222c23cb7cSEd Maste 			p += strlen((char *) p) + 1;
44232c23cb7cSEd Maste 		}
44242c23cb7cSEd Maste 
44252c23cb7cSEd Maste 		p++;
44262c23cb7cSEd Maste 		printf("\n");
44272c23cb7cSEd Maste 		printf(" The File Name Table:\n");
44282c23cb7cSEd Maste 		printf("  Entry\tDir\tTime\tSize\tName\n");
44292c23cb7cSEd Maste 		i = 0;
44302c23cb7cSEd Maste 		while (*p != '\0') {
44312c23cb7cSEd Maste 			i++;
44322c23cb7cSEd Maste 			pn = (char *) p;
44332c23cb7cSEd Maste 			p += strlen(pn) + 1;
443495fd7f26SEd Maste 			dirndx = _decode_uleb128(&p, pe);
443595fd7f26SEd Maste 			mtime = _decode_uleb128(&p, pe);
443695fd7f26SEd Maste 			fsize = _decode_uleb128(&p, pe);
44372c23cb7cSEd Maste 			printf("  %d\t%ju\t%ju\t%ju\t%s\n", i,
44382c23cb7cSEd Maste 			    (uintmax_t) dirndx, (uintmax_t) mtime,
44392c23cb7cSEd Maste 			    (uintmax_t) fsize, pn);
44402c23cb7cSEd Maste 		}
44412c23cb7cSEd Maste 
44422c23cb7cSEd Maste #define	RESET_REGISTERS						\
44432c23cb7cSEd Maste 	do {							\
44442c23cb7cSEd Maste 		address	       = 0;				\
44452c23cb7cSEd Maste 		file	       = 1;				\
44462c23cb7cSEd Maste 		line	       = 1;				\
44472c23cb7cSEd Maste 		column	       = 0;				\
44482c23cb7cSEd Maste 		is_stmt	       = defstmt;			\
44492c23cb7cSEd Maste 	} while(0)
44502c23cb7cSEd Maste 
44512c23cb7cSEd Maste #define	LINE(x) (lbase + (((x) - opbase) % lrange))
44522c23cb7cSEd Maste #define	ADDRESS(x) ((((x) - opbase) / lrange) * minlen)
44532c23cb7cSEd Maste 
44542c23cb7cSEd Maste 		p++;
44552c23cb7cSEd Maste 		printf("\n");
44562c23cb7cSEd Maste 		printf(" Line Number Statements:\n");
44572c23cb7cSEd Maste 
44582c23cb7cSEd Maste 		RESET_REGISTERS;
44592c23cb7cSEd Maste 
44602c23cb7cSEd Maste 		while (p < pe) {
44612c23cb7cSEd Maste 
44622c23cb7cSEd Maste 			if (*p == 0) {
44632c23cb7cSEd Maste 				/*
44642c23cb7cSEd Maste 				 * Extended Opcodes.
44652c23cb7cSEd Maste 				 */
44662c23cb7cSEd Maste 				p++;
446795fd7f26SEd Maste 				opsize = _decode_uleb128(&p, pe);
44682c23cb7cSEd Maste 				printf("  Extended opcode %u: ", *p);
44692c23cb7cSEd Maste 				switch (*p) {
44702c23cb7cSEd Maste 				case DW_LNE_end_sequence:
44712c23cb7cSEd Maste 					p++;
44722c23cb7cSEd Maste 					RESET_REGISTERS;
44732c23cb7cSEd Maste 					printf("End of Sequence\n");
44742c23cb7cSEd Maste 					break;
44752c23cb7cSEd Maste 				case DW_LNE_set_address:
44762c23cb7cSEd Maste 					p++;
44772c23cb7cSEd Maste 					address = re->dw_decode(&p,
44782c23cb7cSEd Maste 					    pointer_size);
44792c23cb7cSEd Maste 					printf("set Address to %#jx\n",
44802c23cb7cSEd Maste 					    (uintmax_t) address);
44812c23cb7cSEd Maste 					break;
44822c23cb7cSEd Maste 				case DW_LNE_define_file:
44832c23cb7cSEd Maste 					p++;
44842c23cb7cSEd Maste 					pn = (char *) p;
44852c23cb7cSEd Maste 					p += strlen(pn) + 1;
448695fd7f26SEd Maste 					dirndx = _decode_uleb128(&p, pe);
448795fd7f26SEd Maste 					mtime = _decode_uleb128(&p, pe);
448895fd7f26SEd Maste 					fsize = _decode_uleb128(&p, pe);
44892c23cb7cSEd Maste 					printf("define new file: %s\n", pn);
44902c23cb7cSEd Maste 					break;
44912c23cb7cSEd Maste 				default:
44922c23cb7cSEd Maste 					/* Unrecognized extened opcodes. */
44932c23cb7cSEd Maste 					p += opsize;
44942c23cb7cSEd Maste 					printf("unknown opcode\n");
44952c23cb7cSEd Maste 				}
44962c23cb7cSEd Maste 			} else if (*p > 0 && *p < opbase) {
44972c23cb7cSEd Maste 				/*
44982c23cb7cSEd Maste 				 * Standard Opcodes.
44992c23cb7cSEd Maste 				 */
45002c23cb7cSEd Maste 				switch(*p++) {
45012c23cb7cSEd Maste 				case DW_LNS_copy:
45022c23cb7cSEd Maste 					printf("  Copy\n");
45032c23cb7cSEd Maste 					break;
45042c23cb7cSEd Maste 				case DW_LNS_advance_pc:
450595fd7f26SEd Maste 					udelta = _decode_uleb128(&p, pe) *
45062c23cb7cSEd Maste 					    minlen;
45072c23cb7cSEd Maste 					address += udelta;
45082c23cb7cSEd Maste 					printf("  Advance PC by %ju to %#jx\n",
45092c23cb7cSEd Maste 					    (uintmax_t) udelta,
45102c23cb7cSEd Maste 					    (uintmax_t) address);
45112c23cb7cSEd Maste 					break;
45122c23cb7cSEd Maste 				case DW_LNS_advance_line:
451395fd7f26SEd Maste 					sdelta = _decode_sleb128(&p, pe);
45142c23cb7cSEd Maste 					line += sdelta;
45152c23cb7cSEd Maste 					printf("  Advance Line by %jd to %ju\n",
45162c23cb7cSEd Maste 					    (intmax_t) sdelta,
45172c23cb7cSEd Maste 					    (uintmax_t) line);
45182c23cb7cSEd Maste 					break;
45192c23cb7cSEd Maste 				case DW_LNS_set_file:
452095fd7f26SEd Maste 					file = _decode_uleb128(&p, pe);
45212c23cb7cSEd Maste 					printf("  Set File to %ju\n",
45222c23cb7cSEd Maste 					    (uintmax_t) file);
45232c23cb7cSEd Maste 					break;
45242c23cb7cSEd Maste 				case DW_LNS_set_column:
452595fd7f26SEd Maste 					column = _decode_uleb128(&p, pe);
45262c23cb7cSEd Maste 					printf("  Set Column to %ju\n",
45272c23cb7cSEd Maste 					    (uintmax_t) column);
45282c23cb7cSEd Maste 					break;
45292c23cb7cSEd Maste 				case DW_LNS_negate_stmt:
45302c23cb7cSEd Maste 					is_stmt = !is_stmt;
45312c23cb7cSEd Maste 					printf("  Set is_stmt to %d\n", is_stmt);
45322c23cb7cSEd Maste 					break;
45332c23cb7cSEd Maste 				case DW_LNS_set_basic_block:
45342c23cb7cSEd Maste 					printf("  Set basic block flag\n");
45352c23cb7cSEd Maste 					break;
45362c23cb7cSEd Maste 				case DW_LNS_const_add_pc:
45372c23cb7cSEd Maste 					address += ADDRESS(255);
45382c23cb7cSEd Maste 					printf("  Advance PC by constant %ju"
45392c23cb7cSEd Maste 					    " to %#jx\n",
45402c23cb7cSEd Maste 					    (uintmax_t) ADDRESS(255),
45412c23cb7cSEd Maste 					    (uintmax_t) address);
45422c23cb7cSEd Maste 					break;
45432c23cb7cSEd Maste 				case DW_LNS_fixed_advance_pc:
45442c23cb7cSEd Maste 					udelta = re->dw_decode(&p, 2);
45452c23cb7cSEd Maste 					address += udelta;
45462c23cb7cSEd Maste 					printf("  Advance PC by fixed value "
45472c23cb7cSEd Maste 					    "%ju to %#jx\n",
45482c23cb7cSEd Maste 					    (uintmax_t) udelta,
45492c23cb7cSEd Maste 					    (uintmax_t) address);
45502c23cb7cSEd Maste 					break;
45512c23cb7cSEd Maste 				case DW_LNS_set_prologue_end:
45522c23cb7cSEd Maste 					printf("  Set prologue end flag\n");
45532c23cb7cSEd Maste 					break;
45542c23cb7cSEd Maste 				case DW_LNS_set_epilogue_begin:
45552c23cb7cSEd Maste 					printf("  Set epilogue begin flag\n");
45562c23cb7cSEd Maste 					break;
45572c23cb7cSEd Maste 				case DW_LNS_set_isa:
455895fd7f26SEd Maste 					isa = _decode_uleb128(&p, pe);
4559839529caSEd Maste 					printf("  Set isa to %ju\n",
4560839529caSEd Maste 					    (uintmax_t) isa);
45612c23cb7cSEd Maste 					break;
45622c23cb7cSEd Maste 				default:
45632c23cb7cSEd Maste 					/* Unrecognized extended opcodes. */
45642c23cb7cSEd Maste 					printf("  Unknown extended opcode %u\n",
45652c23cb7cSEd Maste 					    *(p - 1));
45662c23cb7cSEd Maste 					break;
45672c23cb7cSEd Maste 				}
45682c23cb7cSEd Maste 
45692c23cb7cSEd Maste 			} else {
45702c23cb7cSEd Maste 				/*
45712c23cb7cSEd Maste 				 * Special Opcodes.
45722c23cb7cSEd Maste 				 */
45732c23cb7cSEd Maste 				line += LINE(*p);
45742c23cb7cSEd Maste 				address += ADDRESS(*p);
45752c23cb7cSEd Maste 				printf("  Special opcode %u: advance Address "
45762c23cb7cSEd Maste 				    "by %ju to %#jx and Line by %jd to %ju\n",
45772c23cb7cSEd Maste 				    *p - opbase, (uintmax_t) ADDRESS(*p),
45782c23cb7cSEd Maste 				    (uintmax_t) address, (intmax_t) LINE(*p),
45792c23cb7cSEd Maste 				    (uintmax_t) line);
45802c23cb7cSEd Maste 				p++;
45812c23cb7cSEd Maste 			}
45822c23cb7cSEd Maste 
45832c23cb7cSEd Maste 
45842c23cb7cSEd Maste 		}
45852c23cb7cSEd Maste 	}
45862c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
45872c23cb7cSEd Maste 		warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
45882c23cb7cSEd Maste 
45892c23cb7cSEd Maste #undef	RESET_REGISTERS
45902c23cb7cSEd Maste #undef	LINE
45912c23cb7cSEd Maste #undef	ADDRESS
45922c23cb7cSEd Maste }
45932c23cb7cSEd Maste 
45942c23cb7cSEd Maste static void
45952c23cb7cSEd Maste dump_dwarf_line_decoded(struct readelf *re)
45962c23cb7cSEd Maste {
45972c23cb7cSEd Maste 	Dwarf_Die die;
45982c23cb7cSEd Maste 	Dwarf_Line *linebuf, ln;
45992c23cb7cSEd Maste 	Dwarf_Addr lineaddr;
46002c23cb7cSEd Maste 	Dwarf_Signed linecount, srccount;
46012c23cb7cSEd Maste 	Dwarf_Unsigned lineno, fn;
46022c23cb7cSEd Maste 	Dwarf_Error de;
46032c23cb7cSEd Maste 	const char *dir, *file;
46042c23cb7cSEd Maste 	char **srcfiles;
46052c23cb7cSEd Maste 	int i, ret;
46062c23cb7cSEd Maste 
46072c23cb7cSEd Maste 	printf("Decoded dump of debug contents of section .debug_line:\n\n");
46082c23cb7cSEd Maste 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
46092c23cb7cSEd Maste 	    NULL, &de)) == DW_DLV_OK) {
46102c23cb7cSEd Maste 		if (dwarf_siblingof(re->dbg, NULL, &die, &de) != DW_DLV_OK)
46112c23cb7cSEd Maste 			continue;
46122c23cb7cSEd Maste 		if (dwarf_attrval_string(die, DW_AT_name, &file, &de) !=
46132c23cb7cSEd Maste 		    DW_DLV_OK)
46142c23cb7cSEd Maste 			file = NULL;
46152c23cb7cSEd Maste 		if (dwarf_attrval_string(die, DW_AT_comp_dir, &dir, &de) !=
46162c23cb7cSEd Maste 		    DW_DLV_OK)
46172c23cb7cSEd Maste 			dir = NULL;
46182c23cb7cSEd Maste 		printf("CU: ");
46192c23cb7cSEd Maste 		if (dir && file)
46202c23cb7cSEd Maste 			printf("%s/", dir);
46212c23cb7cSEd Maste 		if (file)
46222c23cb7cSEd Maste 			printf("%s", file);
46232c23cb7cSEd Maste 		putchar('\n');
46242c23cb7cSEd Maste 		printf("%-37s %11s   %s\n", "Filename", "Line Number",
46252c23cb7cSEd Maste 		    "Starting Address");
46262c23cb7cSEd Maste 		if (dwarf_srclines(die, &linebuf, &linecount, &de) != DW_DLV_OK)
46272c23cb7cSEd Maste 			continue;
46282c23cb7cSEd Maste 		if (dwarf_srcfiles(die, &srcfiles, &srccount, &de) != DW_DLV_OK)
46292c23cb7cSEd Maste 			continue;
46302c23cb7cSEd Maste 		for (i = 0; i < linecount; i++) {
46312c23cb7cSEd Maste 			ln = linebuf[i];
46322c23cb7cSEd Maste 			if (dwarf_line_srcfileno(ln, &fn, &de) != DW_DLV_OK)
46332c23cb7cSEd Maste 				continue;
46342c23cb7cSEd Maste 			if (dwarf_lineno(ln, &lineno, &de) != DW_DLV_OK)
46352c23cb7cSEd Maste 				continue;
46362c23cb7cSEd Maste 			if (dwarf_lineaddr(ln, &lineaddr, &de) != DW_DLV_OK)
46372c23cb7cSEd Maste 				continue;
46382c23cb7cSEd Maste 			printf("%-37s %11ju %#18jx\n",
46392c23cb7cSEd Maste 			    basename(srcfiles[fn - 1]), (uintmax_t) lineno,
46402c23cb7cSEd Maste 			    (uintmax_t) lineaddr);
46412c23cb7cSEd Maste 		}
46422c23cb7cSEd Maste 		putchar('\n');
46432c23cb7cSEd Maste 	}
46442c23cb7cSEd Maste }
46452c23cb7cSEd Maste 
46462c23cb7cSEd Maste static void
46472c23cb7cSEd Maste dump_dwarf_die(struct readelf *re, Dwarf_Die die, int level)
46482c23cb7cSEd Maste {
46492c23cb7cSEd Maste 	Dwarf_Attribute *attr_list;
46502c23cb7cSEd Maste 	Dwarf_Die ret_die;
4651cf781b2eSEd Maste 	Dwarf_Off dieoff, cuoff, culen, attroff;
4652cf781b2eSEd Maste 	Dwarf_Unsigned ate, lang, v_udata, v_sig;
46532c23cb7cSEd Maste 	Dwarf_Signed attr_count, v_sdata;
46542c23cb7cSEd Maste 	Dwarf_Off v_off;
46552c23cb7cSEd Maste 	Dwarf_Addr v_addr;
46562c23cb7cSEd Maste 	Dwarf_Half tag, attr, form;
46572c23cb7cSEd Maste 	Dwarf_Block *v_block;
4658cf781b2eSEd Maste 	Dwarf_Bool v_bool, is_info;
4659cf781b2eSEd Maste 	Dwarf_Sig8 v_sig8;
46602c23cb7cSEd Maste 	Dwarf_Error de;
4661cf781b2eSEd Maste 	Dwarf_Ptr v_expr;
4662cf781b2eSEd Maste 	const char *tag_str, *attr_str, *ate_str, *lang_str;
4663cf781b2eSEd Maste 	char unk_tag[32], unk_attr[32];
46642c23cb7cSEd Maste 	char *v_str;
4665cf781b2eSEd Maste 	uint8_t *b, *p;
46662c23cb7cSEd Maste 	int i, j, abc, ret;
46672c23cb7cSEd Maste 
46682c23cb7cSEd Maste 	if (dwarf_dieoffset(die, &dieoff, &de) != DW_DLV_OK) {
46692c23cb7cSEd Maste 		warnx("dwarf_dieoffset failed: %s", dwarf_errmsg(de));
46702c23cb7cSEd Maste 		goto cont_search;
46712c23cb7cSEd Maste 	}
46722c23cb7cSEd Maste 
46732c23cb7cSEd Maste 	printf(" <%d><%jx>: ", level, (uintmax_t) dieoff);
46742c23cb7cSEd Maste 
46752c23cb7cSEd Maste 	if (dwarf_die_CU_offset_range(die, &cuoff, &culen, &de) != DW_DLV_OK) {
46762c23cb7cSEd Maste 		warnx("dwarf_die_CU_offset_range failed: %s",
46772c23cb7cSEd Maste 		      dwarf_errmsg(de));
46782c23cb7cSEd Maste 		cuoff = 0;
46792c23cb7cSEd Maste 	}
46802c23cb7cSEd Maste 
46812c23cb7cSEd Maste 	abc = dwarf_die_abbrev_code(die);
46822c23cb7cSEd Maste 	if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
46832c23cb7cSEd Maste 		warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
46842c23cb7cSEd Maste 		goto cont_search;
46852c23cb7cSEd Maste 	}
46862c23cb7cSEd Maste 	if (dwarf_get_TAG_name(tag, &tag_str) != DW_DLV_OK) {
4687cf781b2eSEd Maste 		snprintf(unk_tag, sizeof(unk_tag), "[Unknown Tag: %#x]", tag);
4688cf781b2eSEd Maste 		tag_str = unk_tag;
46892c23cb7cSEd Maste 	}
46902c23cb7cSEd Maste 
46912c23cb7cSEd Maste 	printf("Abbrev Number: %d (%s)\n", abc, tag_str);
46922c23cb7cSEd Maste 
46932c23cb7cSEd Maste 	if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
46942c23cb7cSEd Maste 	    DW_DLV_OK) {
46952c23cb7cSEd Maste 		if (ret == DW_DLV_ERROR)
46962c23cb7cSEd Maste 			warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
46972c23cb7cSEd Maste 		goto cont_search;
46982c23cb7cSEd Maste 	}
46992c23cb7cSEd Maste 
47002c23cb7cSEd Maste 	for (i = 0; i < attr_count; i++) {
47012c23cb7cSEd Maste 		if (dwarf_whatform(attr_list[i], &form, &de) != DW_DLV_OK) {
47022c23cb7cSEd Maste 			warnx("dwarf_whatform failed: %s", dwarf_errmsg(de));
47032c23cb7cSEd Maste 			continue;
47042c23cb7cSEd Maste 		}
47052c23cb7cSEd Maste 		if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
47062c23cb7cSEd Maste 			warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
47072c23cb7cSEd Maste 			continue;
47082c23cb7cSEd Maste 		}
47092c23cb7cSEd Maste 		if (dwarf_get_AT_name(attr, &attr_str) != DW_DLV_OK) {
4710cf781b2eSEd Maste 			snprintf(unk_attr, sizeof(unk_attr),
4711cf781b2eSEd Maste 			    "[Unknown AT: %#x]", attr);
4712cf781b2eSEd Maste 			attr_str = unk_attr;
47132c23cb7cSEd Maste 		}
4714cf781b2eSEd Maste 		if (dwarf_attroffset(attr_list[i], &attroff, &de) !=
4715cf781b2eSEd Maste 		    DW_DLV_OK) {
4716cf781b2eSEd Maste 			warnx("dwarf_attroffset failed: %s", dwarf_errmsg(de));
4717cf781b2eSEd Maste 			attroff = 0;
4718cf781b2eSEd Maste 		}
4719cf781b2eSEd Maste 		printf("    <%jx>   %-18s: ", (uintmax_t) attroff, attr_str);
47202c23cb7cSEd Maste 		switch (form) {
47212c23cb7cSEd Maste 		case DW_FORM_ref_addr:
4722cf781b2eSEd Maste 		case DW_FORM_sec_offset:
47232c23cb7cSEd Maste 			if (dwarf_global_formref(attr_list[i], &v_off, &de) !=
47242c23cb7cSEd Maste 			    DW_DLV_OK) {
47252c23cb7cSEd Maste 				warnx("dwarf_global_formref failed: %s",
47262c23cb7cSEd Maste 				    dwarf_errmsg(de));
47272c23cb7cSEd Maste 				continue;
47282c23cb7cSEd Maste 			}
4729cf781b2eSEd Maste 			if (form == DW_FORM_ref_addr)
4730cf781b2eSEd Maste 				printf("<0x%jx>", (uintmax_t) v_off);
4731cf781b2eSEd Maste 			else
4732cf781b2eSEd Maste 				printf("0x%jx", (uintmax_t) v_off);
47332c23cb7cSEd Maste 			break;
47342c23cb7cSEd Maste 
47352c23cb7cSEd Maste 		case DW_FORM_ref1:
47362c23cb7cSEd Maste 		case DW_FORM_ref2:
47372c23cb7cSEd Maste 		case DW_FORM_ref4:
47382c23cb7cSEd Maste 		case DW_FORM_ref8:
47392c23cb7cSEd Maste 		case DW_FORM_ref_udata:
47402c23cb7cSEd Maste 			if (dwarf_formref(attr_list[i], &v_off, &de) !=
47412c23cb7cSEd Maste 			    DW_DLV_OK) {
47422c23cb7cSEd Maste 				warnx("dwarf_formref failed: %s",
47432c23cb7cSEd Maste 				    dwarf_errmsg(de));
47442c23cb7cSEd Maste 				continue;
47452c23cb7cSEd Maste 			}
47462c23cb7cSEd Maste 			v_off += cuoff;
4747cf781b2eSEd Maste 			printf("<0x%jx>", (uintmax_t) v_off);
47482c23cb7cSEd Maste 			break;
47492c23cb7cSEd Maste 
47502c23cb7cSEd Maste 		case DW_FORM_addr:
47512c23cb7cSEd Maste 			if (dwarf_formaddr(attr_list[i], &v_addr, &de) !=
47522c23cb7cSEd Maste 			    DW_DLV_OK) {
47532c23cb7cSEd Maste 				warnx("dwarf_formaddr failed: %s",
47542c23cb7cSEd Maste 				    dwarf_errmsg(de));
47552c23cb7cSEd Maste 				continue;
47562c23cb7cSEd Maste 			}
47572c23cb7cSEd Maste 			printf("%#jx", (uintmax_t) v_addr);
47582c23cb7cSEd Maste 			break;
47592c23cb7cSEd Maste 
47602c23cb7cSEd Maste 		case DW_FORM_data1:
47612c23cb7cSEd Maste 		case DW_FORM_data2:
47622c23cb7cSEd Maste 		case DW_FORM_data4:
47632c23cb7cSEd Maste 		case DW_FORM_data8:
47642c23cb7cSEd Maste 		case DW_FORM_udata:
47652c23cb7cSEd Maste 			if (dwarf_formudata(attr_list[i], &v_udata, &de) !=
47662c23cb7cSEd Maste 			    DW_DLV_OK) {
47672c23cb7cSEd Maste 				warnx("dwarf_formudata failed: %s",
47682c23cb7cSEd Maste 				    dwarf_errmsg(de));
47692c23cb7cSEd Maste 				continue;
47702c23cb7cSEd Maste 			}
4771cf781b2eSEd Maste 			if (attr == DW_AT_high_pc)
4772cf781b2eSEd Maste 				printf("0x%jx", (uintmax_t) v_udata);
4773cf781b2eSEd Maste 			else
47742c23cb7cSEd Maste 				printf("%ju", (uintmax_t) v_udata);
47752c23cb7cSEd Maste 			break;
47762c23cb7cSEd Maste 
47772c23cb7cSEd Maste 		case DW_FORM_sdata:
47782c23cb7cSEd Maste 			if (dwarf_formsdata(attr_list[i], &v_sdata, &de) !=
47792c23cb7cSEd Maste 			    DW_DLV_OK) {
47802c23cb7cSEd Maste 				warnx("dwarf_formudata failed: %s",
47812c23cb7cSEd Maste 				    dwarf_errmsg(de));
47822c23cb7cSEd Maste 				continue;
47832c23cb7cSEd Maste 			}
47842c23cb7cSEd Maste 			printf("%jd", (intmax_t) v_sdata);
47852c23cb7cSEd Maste 			break;
47862c23cb7cSEd Maste 
47872c23cb7cSEd Maste 		case DW_FORM_flag:
47882c23cb7cSEd Maste 			if (dwarf_formflag(attr_list[i], &v_bool, &de) !=
47892c23cb7cSEd Maste 			    DW_DLV_OK) {
47902c23cb7cSEd Maste 				warnx("dwarf_formflag failed: %s",
47912c23cb7cSEd Maste 				    dwarf_errmsg(de));
47922c23cb7cSEd Maste 				continue;
47932c23cb7cSEd Maste 			}
47942c23cb7cSEd Maste 			printf("%jd", (intmax_t) v_bool);
47952c23cb7cSEd Maste 			break;
47962c23cb7cSEd Maste 
4797cf781b2eSEd Maste 		case DW_FORM_flag_present:
4798cf781b2eSEd Maste 			putchar('1');
4799cf781b2eSEd Maste 			break;
4800cf781b2eSEd Maste 
48012c23cb7cSEd Maste 		case DW_FORM_string:
48022c23cb7cSEd Maste 		case DW_FORM_strp:
48032c23cb7cSEd Maste 			if (dwarf_formstring(attr_list[i], &v_str, &de) !=
48042c23cb7cSEd Maste 			    DW_DLV_OK) {
48052c23cb7cSEd Maste 				warnx("dwarf_formstring failed: %s",
48062c23cb7cSEd Maste 				    dwarf_errmsg(de));
48072c23cb7cSEd Maste 				continue;
48082c23cb7cSEd Maste 			}
48092c23cb7cSEd Maste 			if (form == DW_FORM_string)
48102c23cb7cSEd Maste 				printf("%s", v_str);
48112c23cb7cSEd Maste 			else
48122c23cb7cSEd Maste 				printf("(indirect string) %s", v_str);
48132c23cb7cSEd Maste 			break;
48142c23cb7cSEd Maste 
48152c23cb7cSEd Maste 		case DW_FORM_block:
48162c23cb7cSEd Maste 		case DW_FORM_block1:
48172c23cb7cSEd Maste 		case DW_FORM_block2:
48182c23cb7cSEd Maste 		case DW_FORM_block4:
48192c23cb7cSEd Maste 			if (dwarf_formblock(attr_list[i], &v_block, &de) !=
48202c23cb7cSEd Maste 			    DW_DLV_OK) {
48212c23cb7cSEd Maste 				warnx("dwarf_formblock failed: %s",
48222c23cb7cSEd Maste 				    dwarf_errmsg(de));
48232c23cb7cSEd Maste 				continue;
48242c23cb7cSEd Maste 			}
4825cf781b2eSEd Maste 			printf("%ju byte block:", (uintmax_t) v_block->bl_len);
48262c23cb7cSEd Maste 			b = v_block->bl_data;
48272c23cb7cSEd Maste 			for (j = 0; (Dwarf_Unsigned) j < v_block->bl_len; j++)
48282c23cb7cSEd Maste 				printf(" %x", b[j]);
4829cf781b2eSEd Maste 			printf("\t(");
4830cf781b2eSEd Maste 			dump_dwarf_block(re, v_block->bl_data, v_block->bl_len);
4831cf781b2eSEd Maste 			putchar(')');
48322c23cb7cSEd Maste 			break;
4833cf781b2eSEd Maste 
4834cf781b2eSEd Maste 		case DW_FORM_exprloc:
4835cf781b2eSEd Maste 			if (dwarf_formexprloc(attr_list[i], &v_udata, &v_expr,
4836cf781b2eSEd Maste 			    &de) != DW_DLV_OK) {
4837cf781b2eSEd Maste 				warnx("dwarf_formexprloc failed: %s",
4838cf781b2eSEd Maste 				    dwarf_errmsg(de));
4839cf781b2eSEd Maste 				continue;
4840cf781b2eSEd Maste 			}
4841cf781b2eSEd Maste 			printf("%ju byte block:", (uintmax_t) v_udata);
4842cf781b2eSEd Maste 			b = v_expr;
4843cf781b2eSEd Maste 			for (j = 0; (Dwarf_Unsigned) j < v_udata; j++)
4844cf781b2eSEd Maste 				printf(" %x", b[j]);
4845cf781b2eSEd Maste 			printf("\t(");
4846cf781b2eSEd Maste 			dump_dwarf_block(re, v_expr, v_udata);
4847cf781b2eSEd Maste 			putchar(')');
4848cf781b2eSEd Maste 			break;
4849cf781b2eSEd Maste 
4850cf781b2eSEd Maste 		case DW_FORM_ref_sig8:
4851cf781b2eSEd Maste 			if (dwarf_formsig8(attr_list[i], &v_sig8, &de) !=
4852cf781b2eSEd Maste 			    DW_DLV_OK) {
4853cf781b2eSEd Maste 				warnx("dwarf_formsig8 failed: %s",
4854cf781b2eSEd Maste 				    dwarf_errmsg(de));
4855cf781b2eSEd Maste 				continue;
4856cf781b2eSEd Maste 			}
4857cf781b2eSEd Maste 			p = (uint8_t *)(uintptr_t) &v_sig8.signature[0];
4858cf781b2eSEd Maste 			v_sig = re->dw_decode(&p, 8);
4859cf781b2eSEd Maste 			printf("signature: 0x%jx", (uintmax_t) v_sig);
48602c23cb7cSEd Maste 		}
48612c23cb7cSEd Maste 		switch (attr) {
48622c23cb7cSEd Maste 		case DW_AT_encoding:
48632c23cb7cSEd Maste 			if (dwarf_attrval_unsigned(die, attr, &ate, &de) !=
48642c23cb7cSEd Maste 			    DW_DLV_OK)
48652c23cb7cSEd Maste 				break;
48662c23cb7cSEd Maste 			if (dwarf_get_ATE_name(ate, &ate_str) != DW_DLV_OK)
4867cf781b2eSEd Maste 				ate_str = "DW_ATE_UNKNOWN";
48682c23cb7cSEd Maste 			printf("\t(%s)", &ate_str[strlen("DW_ATE_")]);
48692c23cb7cSEd Maste 			break;
4870cf781b2eSEd Maste 
4871cf781b2eSEd Maste 		case DW_AT_language:
4872cf781b2eSEd Maste 			if (dwarf_attrval_unsigned(die, attr, &lang, &de) !=
4873cf781b2eSEd Maste 			    DW_DLV_OK)
4874cf781b2eSEd Maste 				break;
4875cf781b2eSEd Maste 			if (dwarf_get_LANG_name(lang, &lang_str) != DW_DLV_OK)
4876cf781b2eSEd Maste 				break;
4877cf781b2eSEd Maste 			printf("\t(%s)", &lang_str[strlen("DW_LANG_")]);
4878cf781b2eSEd Maste 			break;
4879cf781b2eSEd Maste 
4880cf781b2eSEd Maste 		case DW_AT_location:
4881cf781b2eSEd Maste 		case DW_AT_string_length:
4882cf781b2eSEd Maste 		case DW_AT_return_addr:
4883cf781b2eSEd Maste 		case DW_AT_data_member_location:
4884cf781b2eSEd Maste 		case DW_AT_frame_base:
4885cf781b2eSEd Maste 		case DW_AT_segment:
4886cf781b2eSEd Maste 		case DW_AT_static_link:
4887cf781b2eSEd Maste 		case DW_AT_use_location:
4888cf781b2eSEd Maste 		case DW_AT_vtable_elem_location:
4889cf781b2eSEd Maste 			switch (form) {
4890cf781b2eSEd Maste 			case DW_FORM_data4:
4891cf781b2eSEd Maste 			case DW_FORM_data8:
4892cf781b2eSEd Maste 			case DW_FORM_sec_offset:
4893cf781b2eSEd Maste 				printf("\t(location list)");
4894cf781b2eSEd Maste 				break;
4895cf781b2eSEd Maste 			default:
4896cf781b2eSEd Maste 				break;
4897cf781b2eSEd Maste 			}
4898cf781b2eSEd Maste 
48992c23cb7cSEd Maste 		default:
49002c23cb7cSEd Maste 			break;
49012c23cb7cSEd Maste 		}
49022c23cb7cSEd Maste 		putchar('\n');
49032c23cb7cSEd Maste 	}
49042c23cb7cSEd Maste 
49052c23cb7cSEd Maste 
49062c23cb7cSEd Maste cont_search:
49072c23cb7cSEd Maste 	/* Search children. */
49082c23cb7cSEd Maste 	ret = dwarf_child(die, &ret_die, &de);
49092c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
49102c23cb7cSEd Maste 		warnx("dwarf_child: %s", dwarf_errmsg(de));
49112c23cb7cSEd Maste 	else if (ret == DW_DLV_OK)
49122c23cb7cSEd Maste 		dump_dwarf_die(re, ret_die, level + 1);
49132c23cb7cSEd Maste 
49142c23cb7cSEd Maste 	/* Search sibling. */
4915cf781b2eSEd Maste 	is_info = dwarf_get_die_infotypes_flag(die);
4916cf781b2eSEd Maste 	ret = dwarf_siblingof_b(re->dbg, die, &ret_die, is_info, &de);
49172c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
49182c23cb7cSEd Maste 		warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
49192c23cb7cSEd Maste 	else if (ret == DW_DLV_OK)
49202c23cb7cSEd Maste 		dump_dwarf_die(re, ret_die, level);
49212c23cb7cSEd Maste 
49222c23cb7cSEd Maste 	dwarf_dealloc(re->dbg, die, DW_DLA_DIE);
49232c23cb7cSEd Maste }
49242c23cb7cSEd Maste 
49252c23cb7cSEd Maste static void
4926cf781b2eSEd Maste set_cu_context(struct readelf *re, Dwarf_Half psize, Dwarf_Half osize,
4927cf781b2eSEd Maste     Dwarf_Half ver)
4928cf781b2eSEd Maste {
4929cf781b2eSEd Maste 
4930cf781b2eSEd Maste 	re->cu_psize = psize;
4931cf781b2eSEd Maste 	re->cu_osize = osize;
4932cf781b2eSEd Maste 	re->cu_ver = ver;
4933cf781b2eSEd Maste }
4934cf781b2eSEd Maste 
4935cf781b2eSEd Maste static void
4936cf781b2eSEd Maste dump_dwarf_info(struct readelf *re, Dwarf_Bool is_info)
49372c23cb7cSEd Maste {
49382c23cb7cSEd Maste 	struct section *s;
49392c23cb7cSEd Maste 	Dwarf_Die die;
49402c23cb7cSEd Maste 	Dwarf_Error de;
4941cf781b2eSEd Maste 	Dwarf_Half tag, version, pointer_size, off_size;
49422c23cb7cSEd Maste 	Dwarf_Off cu_offset, cu_length;
49432c23cb7cSEd Maste 	Dwarf_Off aboff;
4944cf781b2eSEd Maste 	Dwarf_Unsigned typeoff;
4945cf781b2eSEd Maste 	Dwarf_Sig8 sig8;
4946cf781b2eSEd Maste 	Dwarf_Unsigned sig;
4947cf781b2eSEd Maste 	uint8_t *p;
4948cf781b2eSEd Maste 	const char *sn;
4949cf781b2eSEd Maste 	int i, ret;
49502c23cb7cSEd Maste 
4951cf781b2eSEd Maste 	sn = is_info ? ".debug_info" : ".debug_types";
49522c23cb7cSEd Maste 
49532c23cb7cSEd Maste 	s = NULL;
49542c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
49552c23cb7cSEd Maste 		s = &re->sl[i];
4956cf781b2eSEd Maste 		if (s->name != NULL && !strcmp(s->name, sn))
49572c23cb7cSEd Maste 			break;
49582c23cb7cSEd Maste 	}
49592c23cb7cSEd Maste 	if ((size_t) i >= re->shnum)
49602c23cb7cSEd Maste 		return;
49612c23cb7cSEd Maste 
4962cf781b2eSEd Maste 	do {
4963cf781b2eSEd Maste 		printf("\nDump of debug contents of section %s:\n", sn);
49642c23cb7cSEd Maste 
4965cf781b2eSEd Maste 		while ((ret = dwarf_next_cu_header_c(re->dbg, is_info, NULL,
4966cf781b2eSEd Maste 		    &version, &aboff, &pointer_size, &off_size, NULL, &sig8,
4967cf781b2eSEd Maste 		    &typeoff, NULL, &de)) == DW_DLV_OK) {
4968cf781b2eSEd Maste 			set_cu_context(re, pointer_size, off_size, version);
49692c23cb7cSEd Maste 			die = NULL;
4970cf781b2eSEd Maste 			while (dwarf_siblingof_b(re->dbg, die, &die, is_info,
4971cf781b2eSEd Maste 			    &de) == DW_DLV_OK) {
49722c23cb7cSEd Maste 				if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
49732c23cb7cSEd Maste 					warnx("dwarf_tag failed: %s",
49742c23cb7cSEd Maste 					    dwarf_errmsg(de));
4975cf781b2eSEd Maste 					continue;
49762c23cb7cSEd Maste 				}
49772c23cb7cSEd Maste 				/* XXX: What about DW_TAG_partial_unit? */
4978cf781b2eSEd Maste 				if ((is_info && tag == DW_TAG_compile_unit) ||
4979cf781b2eSEd Maste 				    (!is_info && tag == DW_TAG_type_unit))
49802c23cb7cSEd Maste 					break;
49812c23cb7cSEd Maste 			}
4982cf781b2eSEd Maste 			if (die == NULL && is_info) {
4983cf781b2eSEd Maste 				warnx("could not find DW_TAG_compile_unit "
4984cf781b2eSEd Maste 				    "die");
4985cf781b2eSEd Maste 				continue;
4986cf781b2eSEd Maste 			} else if (die == NULL && !is_info) {
4987cf781b2eSEd Maste 				warnx("could not find DW_TAG_type_unit die");
4988cf781b2eSEd Maste 				continue;
49892c23cb7cSEd Maste 			}
49902c23cb7cSEd Maste 
4991cf781b2eSEd Maste 			if (dwarf_die_CU_offset_range(die, &cu_offset,
4992cf781b2eSEd Maste 			    &cu_length, &de) != DW_DLV_OK) {
49932c23cb7cSEd Maste 				warnx("dwarf_die_CU_offset failed: %s",
49942c23cb7cSEd Maste 				    dwarf_errmsg(de));
49952c23cb7cSEd Maste 				continue;
49962c23cb7cSEd Maste 			}
49972c23cb7cSEd Maste 
4998cf781b2eSEd Maste 			cu_length -= off_size == 4 ? 4 : 12;
4999cf781b2eSEd Maste 
5000cf781b2eSEd Maste 			sig = 0;
5001cf781b2eSEd Maste 			if (!is_info) {
5002cf781b2eSEd Maste 				p = (uint8_t *)(uintptr_t) &sig8.signature[0];
5003cf781b2eSEd Maste 				sig = re->dw_decode(&p, 8);
5004cf781b2eSEd Maste 			}
5005cf781b2eSEd Maste 
5006cf781b2eSEd Maste 			printf("\n  Type Unit @ offset 0x%jx:\n",
5007cf781b2eSEd Maste 			    (uintmax_t) cu_offset);
5008cf781b2eSEd Maste 			printf("    Length:\t\t%#jx (%d-bit)\n",
5009cf781b2eSEd Maste 			    (uintmax_t) cu_length, off_size == 4 ? 32 : 64);
50102c23cb7cSEd Maste 			printf("    Version:\t\t%u\n", version);
5011cf781b2eSEd Maste 			printf("    Abbrev Offset:\t0x%jx\n",
5012cf781b2eSEd Maste 			    (uintmax_t) aboff);
50132c23cb7cSEd Maste 			printf("    Pointer Size:\t%u\n", pointer_size);
5014cf781b2eSEd Maste 			if (!is_info) {
5015cf781b2eSEd Maste 				printf("    Signature:\t\t0x%016jx\n",
5016cf781b2eSEd Maste 				    (uintmax_t) sig);
5017cf781b2eSEd Maste 				printf("    Type Offset:\t0x%jx\n",
5018cf781b2eSEd Maste 				    (uintmax_t) typeoff);
5019cf781b2eSEd Maste 			}
50202c23cb7cSEd Maste 
50212c23cb7cSEd Maste 			dump_dwarf_die(re, die, 0);
50222c23cb7cSEd Maste 		}
50232c23cb7cSEd Maste 		if (ret == DW_DLV_ERROR)
50242c23cb7cSEd Maste 			warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
5025cf781b2eSEd Maste 		if (is_info)
5026cf781b2eSEd Maste 			break;
5027cf781b2eSEd Maste 	} while (dwarf_next_types_section(re->dbg, &de) == DW_DLV_OK);
50282c23cb7cSEd Maste }
50292c23cb7cSEd Maste 
50302c23cb7cSEd Maste static void
50312c23cb7cSEd Maste dump_dwarf_abbrev(struct readelf *re)
50322c23cb7cSEd Maste {
50332c23cb7cSEd Maste 	Dwarf_Abbrev ab;
50342c23cb7cSEd Maste 	Dwarf_Off aboff, atoff;
50352c23cb7cSEd Maste 	Dwarf_Unsigned length, attr_count;
50362c23cb7cSEd Maste 	Dwarf_Signed flag, form;
50372c23cb7cSEd Maste 	Dwarf_Half tag, attr;
50382c23cb7cSEd Maste 	Dwarf_Error de;
50392c23cb7cSEd Maste 	const char *tag_str, *attr_str, *form_str;
5040cf781b2eSEd Maste 	char unk_tag[32], unk_attr[32], unk_form[32];
50412c23cb7cSEd Maste 	int i, j, ret;
50422c23cb7cSEd Maste 
50432c23cb7cSEd Maste 	printf("\nContents of section .debug_abbrev:\n\n");
50442c23cb7cSEd Maste 
50452c23cb7cSEd Maste 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, &aboff,
50462c23cb7cSEd Maste 	    NULL, NULL, &de)) ==  DW_DLV_OK) {
50472c23cb7cSEd Maste 		printf("  Number TAG\n");
50482c23cb7cSEd Maste 		i = 0;
50492c23cb7cSEd Maste 		while ((ret = dwarf_get_abbrev(re->dbg, aboff, &ab, &length,
50502c23cb7cSEd Maste 		    &attr_count, &de)) == DW_DLV_OK) {
50512c23cb7cSEd Maste 			if (length == 1) {
50522c23cb7cSEd Maste 				dwarf_dealloc(re->dbg, ab, DW_DLA_ABBREV);
50532c23cb7cSEd Maste 				break;
50542c23cb7cSEd Maste 			}
50552c23cb7cSEd Maste 			aboff += length;
50562c23cb7cSEd Maste 			printf("%4d", ++i);
50572c23cb7cSEd Maste 			if (dwarf_get_abbrev_tag(ab, &tag, &de) != DW_DLV_OK) {
50582c23cb7cSEd Maste 				warnx("dwarf_get_abbrev_tag failed: %s",
50592c23cb7cSEd Maste 				    dwarf_errmsg(de));
50602c23cb7cSEd Maste 				goto next_abbrev;
50612c23cb7cSEd Maste 			}
50622c23cb7cSEd Maste 			if (dwarf_get_TAG_name(tag, &tag_str) != DW_DLV_OK) {
5063cf781b2eSEd Maste 				snprintf(unk_tag, sizeof(unk_tag),
5064cf781b2eSEd Maste 				    "[Unknown Tag: %#x]", tag);
5065cf781b2eSEd Maste 				tag_str = unk_tag;
50662c23cb7cSEd Maste 			}
50672c23cb7cSEd Maste 			if (dwarf_get_abbrev_children_flag(ab, &flag, &de) !=
50682c23cb7cSEd Maste 			    DW_DLV_OK) {
50692c23cb7cSEd Maste 				warnx("dwarf_get_abbrev_children_flag failed:"
50702c23cb7cSEd Maste 				    " %s", dwarf_errmsg(de));
50712c23cb7cSEd Maste 				goto next_abbrev;
50722c23cb7cSEd Maste 			}
50732c23cb7cSEd Maste 			printf("      %s    %s\n", tag_str,
50742c23cb7cSEd Maste 			    flag ? "[has children]" : "[no children]");
50752c23cb7cSEd Maste 			for (j = 0; (Dwarf_Unsigned) j < attr_count; j++) {
50762c23cb7cSEd Maste 				if (dwarf_get_abbrev_entry(ab, (Dwarf_Signed) j,
50772c23cb7cSEd Maste 				    &attr, &form, &atoff, &de) != DW_DLV_OK) {
50782c23cb7cSEd Maste 					warnx("dwarf_get_abbrev_entry failed:"
50792c23cb7cSEd Maste 					    " %s", dwarf_errmsg(de));
50802c23cb7cSEd Maste 					continue;
50812c23cb7cSEd Maste 				}
50822c23cb7cSEd Maste 				if (dwarf_get_AT_name(attr, &attr_str) !=
50832c23cb7cSEd Maste 				    DW_DLV_OK) {
5084cf781b2eSEd Maste 					snprintf(unk_attr, sizeof(unk_attr),
5085cf781b2eSEd Maste 					    "[Unknown AT: %#x]", attr);
5086cf781b2eSEd Maste 					attr_str = unk_attr;
50872c23cb7cSEd Maste 				}
50882c23cb7cSEd Maste 				if (dwarf_get_FORM_name(form, &form_str) !=
50892c23cb7cSEd Maste 				    DW_DLV_OK) {
5090cf781b2eSEd Maste 					snprintf(unk_form, sizeof(unk_form),
5091cf781b2eSEd Maste 					    "[Unknown Form: %#x]",
5092cf781b2eSEd Maste 					    (Dwarf_Half) form);
5093cf781b2eSEd Maste 					form_str = unk_form;
50942c23cb7cSEd Maste 				}
50952c23cb7cSEd Maste 				printf("    %-18s %s\n", attr_str, form_str);
50962c23cb7cSEd Maste 			}
50972c23cb7cSEd Maste 		next_abbrev:
50982c23cb7cSEd Maste 			dwarf_dealloc(re->dbg, ab, DW_DLA_ABBREV);
50992c23cb7cSEd Maste 		}
51002c23cb7cSEd Maste 		if (ret != DW_DLV_OK)
51012c23cb7cSEd Maste 			warnx("dwarf_get_abbrev: %s", dwarf_errmsg(de));
51022c23cb7cSEd Maste 	}
51032c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
51042c23cb7cSEd Maste 		warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
51052c23cb7cSEd Maste }
51062c23cb7cSEd Maste 
51072c23cb7cSEd Maste static void
51082c23cb7cSEd Maste dump_dwarf_pubnames(struct readelf *re)
51092c23cb7cSEd Maste {
51102c23cb7cSEd Maste 	struct section *s;
51112c23cb7cSEd Maste 	Dwarf_Off die_off;
51122c23cb7cSEd Maste 	Dwarf_Unsigned offset, length, nt_cu_offset, nt_cu_length;
51132c23cb7cSEd Maste 	Dwarf_Signed cnt;
51142c23cb7cSEd Maste 	Dwarf_Global *globs;
51152c23cb7cSEd Maste 	Dwarf_Half nt_version;
51162c23cb7cSEd Maste 	Dwarf_Error de;
51172c23cb7cSEd Maste 	Elf_Data *d;
51182c23cb7cSEd Maste 	char *glob_name;
51192c23cb7cSEd Maste 	int i, dwarf_size, elferr;
51202c23cb7cSEd Maste 
51212c23cb7cSEd Maste 	printf("\nContents of the .debug_pubnames section:\n");
51222c23cb7cSEd Maste 
51232c23cb7cSEd Maste 	s = NULL;
51242c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
51252c23cb7cSEd Maste 		s = &re->sl[i];
51262c23cb7cSEd Maste 		if (s->name != NULL && !strcmp(s->name, ".debug_pubnames"))
51272c23cb7cSEd Maste 			break;
51282c23cb7cSEd Maste 	}
51292c23cb7cSEd Maste 	if ((size_t) i >= re->shnum)
51302c23cb7cSEd Maste 		return;
51312c23cb7cSEd Maste 
51322c23cb7cSEd Maste 	(void) elf_errno();
51332c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
51342c23cb7cSEd Maste 		elferr = elf_errno();
51352c23cb7cSEd Maste 		if (elferr != 0)
51362c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
51372c23cb7cSEd Maste 		return;
51382c23cb7cSEd Maste 	}
51392c23cb7cSEd Maste 	if (d->d_size <= 0)
51402c23cb7cSEd Maste 		return;
51412c23cb7cSEd Maste 
51422c23cb7cSEd Maste 	/* Read in .debug_pubnames section table header. */
51432c23cb7cSEd Maste 	offset = 0;
51442c23cb7cSEd Maste 	length = re->dw_read(d, &offset, 4);
51452c23cb7cSEd Maste 	if (length == 0xffffffff) {
51462c23cb7cSEd Maste 		dwarf_size = 8;
51472c23cb7cSEd Maste 		length = re->dw_read(d, &offset, 8);
51482c23cb7cSEd Maste 	} else
51492c23cb7cSEd Maste 		dwarf_size = 4;
51502c23cb7cSEd Maste 
51512c23cb7cSEd Maste 	if (length > d->d_size - offset) {
51522c23cb7cSEd Maste 		warnx("invalid .dwarf_pubnames section");
51532c23cb7cSEd Maste 		return;
51542c23cb7cSEd Maste 	}
51552c23cb7cSEd Maste 
51562c23cb7cSEd Maste 	nt_version = re->dw_read(d, &offset, 2);
51572c23cb7cSEd Maste 	nt_cu_offset = re->dw_read(d, &offset, dwarf_size);
51582c23cb7cSEd Maste 	nt_cu_length = re->dw_read(d, &offset, dwarf_size);
51592c23cb7cSEd Maste 	printf("  Length:\t\t\t\t%ju\n", (uintmax_t) length);
51602c23cb7cSEd Maste 	printf("  Version:\t\t\t\t%u\n", nt_version);
51612c23cb7cSEd Maste 	printf("  Offset into .debug_info section:\t%ju\n",
51622c23cb7cSEd Maste 	    (uintmax_t) nt_cu_offset);
51632c23cb7cSEd Maste 	printf("  Size of area in .debug_info section:\t%ju\n",
51642c23cb7cSEd Maste 	    (uintmax_t) nt_cu_length);
51652c23cb7cSEd Maste 
51662c23cb7cSEd Maste 	if (dwarf_get_globals(re->dbg, &globs, &cnt, &de) != DW_DLV_OK) {
51672c23cb7cSEd Maste 		warnx("dwarf_get_globals failed: %s", dwarf_errmsg(de));
51682c23cb7cSEd Maste 		return;
51692c23cb7cSEd Maste 	}
51702c23cb7cSEd Maste 
51712c23cb7cSEd Maste 	printf("\n    Offset      Name\n");
51722c23cb7cSEd Maste 	for (i = 0; i < cnt; i++) {
51732c23cb7cSEd Maste 		if (dwarf_globname(globs[i], &glob_name, &de) != DW_DLV_OK) {
51742c23cb7cSEd Maste 			warnx("dwarf_globname failed: %s", dwarf_errmsg(de));
51752c23cb7cSEd Maste 			continue;
51762c23cb7cSEd Maste 		}
51772c23cb7cSEd Maste 		if (dwarf_global_die_offset(globs[i], &die_off, &de) !=
51782c23cb7cSEd Maste 		    DW_DLV_OK) {
51792c23cb7cSEd Maste 			warnx("dwarf_global_die_offset failed: %s",
51802c23cb7cSEd Maste 			    dwarf_errmsg(de));
51812c23cb7cSEd Maste 			continue;
51822c23cb7cSEd Maste 		}
51832c23cb7cSEd Maste 		printf("    %-11ju %s\n", (uintmax_t) die_off, glob_name);
51842c23cb7cSEd Maste 	}
51852c23cb7cSEd Maste }
51862c23cb7cSEd Maste 
51872c23cb7cSEd Maste static void
51882c23cb7cSEd Maste dump_dwarf_aranges(struct readelf *re)
51892c23cb7cSEd Maste {
51902c23cb7cSEd Maste 	struct section *s;
51912c23cb7cSEd Maste 	Dwarf_Arange *aranges;
51922c23cb7cSEd Maste 	Dwarf_Addr start;
51932c23cb7cSEd Maste 	Dwarf_Unsigned offset, length, as_cu_offset;
51942c23cb7cSEd Maste 	Dwarf_Off die_off;
51952c23cb7cSEd Maste 	Dwarf_Signed cnt;
51962c23cb7cSEd Maste 	Dwarf_Half as_version, as_addrsz, as_segsz;
51972c23cb7cSEd Maste 	Dwarf_Error de;
51982c23cb7cSEd Maste 	Elf_Data *d;
51992c23cb7cSEd Maste 	int i, dwarf_size, elferr;
52002c23cb7cSEd Maste 
52012c23cb7cSEd Maste 	printf("\nContents of section .debug_aranges:\n");
52022c23cb7cSEd Maste 
52032c23cb7cSEd Maste 	s = NULL;
52042c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
52052c23cb7cSEd Maste 		s = &re->sl[i];
52062c23cb7cSEd Maste 		if (s->name != NULL && !strcmp(s->name, ".debug_aranges"))
52072c23cb7cSEd Maste 			break;
52082c23cb7cSEd Maste 	}
52092c23cb7cSEd Maste 	if ((size_t) i >= re->shnum)
52102c23cb7cSEd Maste 		return;
52112c23cb7cSEd Maste 
52122c23cb7cSEd Maste 	(void) elf_errno();
52132c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
52142c23cb7cSEd Maste 		elferr = elf_errno();
52152c23cb7cSEd Maste 		if (elferr != 0)
52162c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
52172c23cb7cSEd Maste 		return;
52182c23cb7cSEd Maste 	}
52192c23cb7cSEd Maste 	if (d->d_size <= 0)
52202c23cb7cSEd Maste 		return;
52212c23cb7cSEd Maste 
52222c23cb7cSEd Maste 	/* Read in the .debug_aranges section table header. */
52232c23cb7cSEd Maste 	offset = 0;
52242c23cb7cSEd Maste 	length = re->dw_read(d, &offset, 4);
52252c23cb7cSEd Maste 	if (length == 0xffffffff) {
52262c23cb7cSEd Maste 		dwarf_size = 8;
52272c23cb7cSEd Maste 		length = re->dw_read(d, &offset, 8);
52282c23cb7cSEd Maste 	} else
52292c23cb7cSEd Maste 		dwarf_size = 4;
52302c23cb7cSEd Maste 
52312c23cb7cSEd Maste 	if (length > d->d_size - offset) {
52322c23cb7cSEd Maste 		warnx("invalid .dwarf_aranges section");
52332c23cb7cSEd Maste 		return;
52342c23cb7cSEd Maste 	}
52352c23cb7cSEd Maste 
52362c23cb7cSEd Maste 	as_version = re->dw_read(d, &offset, 2);
52372c23cb7cSEd Maste 	as_cu_offset = re->dw_read(d, &offset, dwarf_size);
52382c23cb7cSEd Maste 	as_addrsz = re->dw_read(d, &offset, 1);
52392c23cb7cSEd Maste 	as_segsz = re->dw_read(d, &offset, 1);
52402c23cb7cSEd Maste 
52412c23cb7cSEd Maste 	printf("  Length:\t\t\t%ju\n", (uintmax_t) length);
52422c23cb7cSEd Maste 	printf("  Version:\t\t\t%u\n", as_version);
52432c23cb7cSEd Maste 	printf("  Offset into .debug_info:\t%ju\n", (uintmax_t) as_cu_offset);
52442c23cb7cSEd Maste 	printf("  Pointer Size:\t\t\t%u\n", as_addrsz);
52452c23cb7cSEd Maste 	printf("  Segment Size:\t\t\t%u\n", as_segsz);
52462c23cb7cSEd Maste 
52472c23cb7cSEd Maste 	if (dwarf_get_aranges(re->dbg, &aranges, &cnt, &de) != DW_DLV_OK) {
52482c23cb7cSEd Maste 		warnx("dwarf_get_aranges failed: %s", dwarf_errmsg(de));
52492c23cb7cSEd Maste 		return;
52502c23cb7cSEd Maste 	}
52512c23cb7cSEd Maste 
52522c23cb7cSEd Maste 	printf("\n    Address  Length\n");
52532c23cb7cSEd Maste 	for (i = 0; i < cnt; i++) {
52542c23cb7cSEd Maste 		if (dwarf_get_arange_info(aranges[i], &start, &length,
52552c23cb7cSEd Maste 		    &die_off, &de) != DW_DLV_OK) {
52562c23cb7cSEd Maste 			warnx("dwarf_get_arange_info failed: %s",
52572c23cb7cSEd Maste 			    dwarf_errmsg(de));
52582c23cb7cSEd Maste 			continue;
52592c23cb7cSEd Maste 		}
52602c23cb7cSEd Maste 		printf("    %08jx %ju\n", (uintmax_t) start,
52612c23cb7cSEd Maste 		    (uintmax_t) length);
52622c23cb7cSEd Maste 	}
52632c23cb7cSEd Maste }
52642c23cb7cSEd Maste 
52652c23cb7cSEd Maste static void
52662c23cb7cSEd Maste dump_dwarf_ranges_foreach(struct readelf *re, Dwarf_Die die, Dwarf_Addr base)
52672c23cb7cSEd Maste {
52682c23cb7cSEd Maste 	Dwarf_Attribute *attr_list;
52692c23cb7cSEd Maste 	Dwarf_Ranges *ranges;
52702c23cb7cSEd Maste 	Dwarf_Die ret_die;
52712c23cb7cSEd Maste 	Dwarf_Error de;
52722c23cb7cSEd Maste 	Dwarf_Addr base0;
52732c23cb7cSEd Maste 	Dwarf_Half attr;
52742c23cb7cSEd Maste 	Dwarf_Signed attr_count, cnt;
52752c23cb7cSEd Maste 	Dwarf_Unsigned off, bytecnt;
52762c23cb7cSEd Maste 	int i, j, ret;
52772c23cb7cSEd Maste 
52782c23cb7cSEd Maste 	if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
52792c23cb7cSEd Maste 	    DW_DLV_OK) {
52802c23cb7cSEd Maste 		if (ret == DW_DLV_ERROR)
52812c23cb7cSEd Maste 			warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
52822c23cb7cSEd Maste 		goto cont_search;
52832c23cb7cSEd Maste 	}
52842c23cb7cSEd Maste 
52852c23cb7cSEd Maste 	for (i = 0; i < attr_count; i++) {
52862c23cb7cSEd Maste 		if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
52872c23cb7cSEd Maste 			warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
52882c23cb7cSEd Maste 			continue;
52892c23cb7cSEd Maste 		}
52902c23cb7cSEd Maste 		if (attr != DW_AT_ranges)
52912c23cb7cSEd Maste 			continue;
52922c23cb7cSEd Maste 		if (dwarf_formudata(attr_list[i], &off, &de) != DW_DLV_OK) {
52932c23cb7cSEd Maste 			warnx("dwarf_formudata failed: %s", dwarf_errmsg(de));
52942c23cb7cSEd Maste 			continue;
52952c23cb7cSEd Maste 		}
52962c23cb7cSEd Maste 		if (dwarf_get_ranges(re->dbg, (Dwarf_Off) off, &ranges, &cnt,
52972c23cb7cSEd Maste 		    &bytecnt, &de) != DW_DLV_OK)
52982c23cb7cSEd Maste 			continue;
52992c23cb7cSEd Maste 		base0 = base;
53002c23cb7cSEd Maste 		for (j = 0; j < cnt; j++) {
53012c23cb7cSEd Maste 			printf("    %08jx ", (uintmax_t) off);
53022c23cb7cSEd Maste 			if (ranges[j].dwr_type == DW_RANGES_END) {
53032c23cb7cSEd Maste 				printf("%s\n", "<End of list>");
53042c23cb7cSEd Maste 				continue;
53052c23cb7cSEd Maste 			} else if (ranges[j].dwr_type ==
53062c23cb7cSEd Maste 			    DW_RANGES_ADDRESS_SELECTION) {
53072c23cb7cSEd Maste 				base0 = ranges[j].dwr_addr2;
53082c23cb7cSEd Maste 				continue;
53092c23cb7cSEd Maste 			}
53102c23cb7cSEd Maste 			if (re->ec == ELFCLASS32)
53112c23cb7cSEd Maste 				printf("%08jx %08jx\n",
5312839529caSEd Maste 				    (uintmax_t) (ranges[j].dwr_addr1 + base0),
5313839529caSEd Maste 				    (uintmax_t) (ranges[j].dwr_addr2 + base0));
53142c23cb7cSEd Maste 			else
53152c23cb7cSEd Maste 				printf("%016jx %016jx\n",
5316839529caSEd Maste 				    (uintmax_t) (ranges[j].dwr_addr1 + base0),
5317839529caSEd Maste 				    (uintmax_t) (ranges[j].dwr_addr2 + base0));
53182c23cb7cSEd Maste 		}
53192c23cb7cSEd Maste 	}
53202c23cb7cSEd Maste 
53212c23cb7cSEd Maste cont_search:
53222c23cb7cSEd Maste 	/* Search children. */
53232c23cb7cSEd Maste 	ret = dwarf_child(die, &ret_die, &de);
53242c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
53252c23cb7cSEd Maste 		warnx("dwarf_child: %s", dwarf_errmsg(de));
53262c23cb7cSEd Maste 	else if (ret == DW_DLV_OK)
53272c23cb7cSEd Maste 		dump_dwarf_ranges_foreach(re, ret_die, base);
53282c23cb7cSEd Maste 
53292c23cb7cSEd Maste 	/* Search sibling. */
53302c23cb7cSEd Maste 	ret = dwarf_siblingof(re->dbg, die, &ret_die, &de);
53312c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
53322c23cb7cSEd Maste 		warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
53332c23cb7cSEd Maste 	else if (ret == DW_DLV_OK)
53342c23cb7cSEd Maste 		dump_dwarf_ranges_foreach(re, ret_die, base);
53352c23cb7cSEd Maste }
53362c23cb7cSEd Maste 
53372c23cb7cSEd Maste static void
53382c23cb7cSEd Maste dump_dwarf_ranges(struct readelf *re)
53392c23cb7cSEd Maste {
53402c23cb7cSEd Maste 	Dwarf_Ranges *ranges;
53412c23cb7cSEd Maste 	Dwarf_Die die;
53422c23cb7cSEd Maste 	Dwarf_Signed cnt;
53432c23cb7cSEd Maste 	Dwarf_Unsigned bytecnt;
53442c23cb7cSEd Maste 	Dwarf_Half tag;
53452c23cb7cSEd Maste 	Dwarf_Error de;
53462c23cb7cSEd Maste 	Dwarf_Unsigned lowpc;
53472c23cb7cSEd Maste 	int ret;
53482c23cb7cSEd Maste 
53492c23cb7cSEd Maste 	if (dwarf_get_ranges(re->dbg, 0, &ranges, &cnt, &bytecnt, &de) !=
53502c23cb7cSEd Maste 	    DW_DLV_OK)
53512c23cb7cSEd Maste 		return;
53522c23cb7cSEd Maste 
53532c23cb7cSEd Maste 	printf("Contents of the .debug_ranges section:\n\n");
53542c23cb7cSEd Maste 	if (re->ec == ELFCLASS32)
53552c23cb7cSEd Maste 		printf("    %-8s %-8s %s\n", "Offset", "Begin", "End");
53562c23cb7cSEd Maste 	else
53572c23cb7cSEd Maste 		printf("    %-8s %-16s %s\n", "Offset", "Begin", "End");
53582c23cb7cSEd Maste 
53592c23cb7cSEd Maste 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
53602c23cb7cSEd Maste 	    NULL, &de)) == DW_DLV_OK) {
53612c23cb7cSEd Maste 		die = NULL;
53622c23cb7cSEd Maste 		if (dwarf_siblingof(re->dbg, die, &die, &de) != DW_DLV_OK)
53632c23cb7cSEd Maste 			continue;
53642c23cb7cSEd Maste 		if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
53652c23cb7cSEd Maste 			warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
53662c23cb7cSEd Maste 			continue;
53672c23cb7cSEd Maste 		}
53682c23cb7cSEd Maste 		/* XXX: What about DW_TAG_partial_unit? */
53692c23cb7cSEd Maste 		lowpc = 0;
53702c23cb7cSEd Maste 		if (tag == DW_TAG_compile_unit) {
53712c23cb7cSEd Maste 			if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lowpc,
53722c23cb7cSEd Maste 			    &de) != DW_DLV_OK)
53732c23cb7cSEd Maste 				lowpc = 0;
53742c23cb7cSEd Maste 		}
53752c23cb7cSEd Maste 
53762c23cb7cSEd Maste 		dump_dwarf_ranges_foreach(re, die, (Dwarf_Addr) lowpc);
53772c23cb7cSEd Maste 	}
53782c23cb7cSEd Maste 	putchar('\n');
53792c23cb7cSEd Maste }
53802c23cb7cSEd Maste 
53812c23cb7cSEd Maste static void
53822c23cb7cSEd Maste dump_dwarf_macinfo(struct readelf *re)
53832c23cb7cSEd Maste {
53842c23cb7cSEd Maste 	Dwarf_Unsigned offset;
53852c23cb7cSEd Maste 	Dwarf_Signed cnt;
53862c23cb7cSEd Maste 	Dwarf_Macro_Details *md;
53872c23cb7cSEd Maste 	Dwarf_Error de;
53882c23cb7cSEd Maste 	const char *mi_str;
5389cf781b2eSEd Maste 	char unk_mi[32];
53902c23cb7cSEd Maste 	int i;
53912c23cb7cSEd Maste 
53922c23cb7cSEd Maste #define	_MAX_MACINFO_ENTRY	65535
53932c23cb7cSEd Maste 
53942c23cb7cSEd Maste 	printf("\nContents of section .debug_macinfo:\n\n");
53952c23cb7cSEd Maste 
53962c23cb7cSEd Maste 	offset = 0;
53972c23cb7cSEd Maste 	while (dwarf_get_macro_details(re->dbg, offset, _MAX_MACINFO_ENTRY,
53982c23cb7cSEd Maste 	    &cnt, &md, &de) == DW_DLV_OK) {
53992c23cb7cSEd Maste 		for (i = 0; i < cnt; i++) {
54002c23cb7cSEd Maste 			offset = md[i].dmd_offset + 1;
54012c23cb7cSEd Maste 			if (md[i].dmd_type == 0)
54022c23cb7cSEd Maste 				break;
54032c23cb7cSEd Maste 			if (dwarf_get_MACINFO_name(md[i].dmd_type, &mi_str) !=
54042c23cb7cSEd Maste 			    DW_DLV_OK) {
5405cf781b2eSEd Maste 				snprintf(unk_mi, sizeof(unk_mi),
5406cf781b2eSEd Maste 				    "[Unknown MACINFO: %#x]", md[i].dmd_type);
5407cf781b2eSEd Maste 				mi_str = unk_mi;
54082c23cb7cSEd Maste 			}
54092c23cb7cSEd Maste 			printf(" %s", mi_str);
54102c23cb7cSEd Maste 			switch (md[i].dmd_type) {
54112c23cb7cSEd Maste 			case DW_MACINFO_define:
54122c23cb7cSEd Maste 			case DW_MACINFO_undef:
54132c23cb7cSEd Maste 				printf(" - lineno : %jd macro : %s\n",
54142c23cb7cSEd Maste 				    (intmax_t) md[i].dmd_lineno,
54152c23cb7cSEd Maste 				    md[i].dmd_macro);
54162c23cb7cSEd Maste 				break;
54172c23cb7cSEd Maste 			case DW_MACINFO_start_file:
54182c23cb7cSEd Maste 				printf(" - lineno : %jd filenum : %jd\n",
54192c23cb7cSEd Maste 				    (intmax_t) md[i].dmd_lineno,
54202c23cb7cSEd Maste 				    (intmax_t) md[i].dmd_fileindex);
54212c23cb7cSEd Maste 				break;
54222c23cb7cSEd Maste 			default:
54232c23cb7cSEd Maste 				putchar('\n');
54242c23cb7cSEd Maste 				break;
54252c23cb7cSEd Maste 			}
54262c23cb7cSEd Maste 		}
54272c23cb7cSEd Maste 	}
54282c23cb7cSEd Maste 
54292c23cb7cSEd Maste #undef	_MAX_MACINFO_ENTRY
54302c23cb7cSEd Maste }
54312c23cb7cSEd Maste 
54322c23cb7cSEd Maste static void
5433cf781b2eSEd Maste dump_dwarf_frame_inst(struct readelf *re, Dwarf_Cie cie, uint8_t *insts,
5434cf781b2eSEd Maste     Dwarf_Unsigned len, Dwarf_Unsigned caf, Dwarf_Signed daf, Dwarf_Addr pc,
5435cf781b2eSEd Maste     Dwarf_Debug dbg)
54362c23cb7cSEd Maste {
54372c23cb7cSEd Maste 	Dwarf_Frame_Op *oplist;
54382c23cb7cSEd Maste 	Dwarf_Signed opcnt, delta;
54392c23cb7cSEd Maste 	Dwarf_Small op;
54402c23cb7cSEd Maste 	Dwarf_Error de;
54412c23cb7cSEd Maste 	const char *op_str;
5442cf781b2eSEd Maste 	char unk_op[32];
54432c23cb7cSEd Maste 	int i;
54442c23cb7cSEd Maste 
54452c23cb7cSEd Maste 	if (dwarf_expand_frame_instructions(cie, insts, len, &oplist,
54462c23cb7cSEd Maste 	    &opcnt, &de) != DW_DLV_OK) {
54472c23cb7cSEd Maste 		warnx("dwarf_expand_frame_instructions failed: %s",
54482c23cb7cSEd Maste 		    dwarf_errmsg(de));
54492c23cb7cSEd Maste 		return;
54502c23cb7cSEd Maste 	}
54512c23cb7cSEd Maste 
54522c23cb7cSEd Maste 	for (i = 0; i < opcnt; i++) {
54532c23cb7cSEd Maste 		if (oplist[i].fp_base_op != 0)
54542c23cb7cSEd Maste 			op = oplist[i].fp_base_op << 6;
54552c23cb7cSEd Maste 		else
54562c23cb7cSEd Maste 			op = oplist[i].fp_extended_op;
54572c23cb7cSEd Maste 		if (dwarf_get_CFA_name(op, &op_str) != DW_DLV_OK) {
5458cf781b2eSEd Maste 			snprintf(unk_op, sizeof(unk_op), "[Unknown CFA: %#x]",
5459cf781b2eSEd Maste 			    op);
5460cf781b2eSEd Maste 			op_str = unk_op;
54612c23cb7cSEd Maste 		}
54622c23cb7cSEd Maste 		printf("  %s", op_str);
54632c23cb7cSEd Maste 		switch (op) {
54642c23cb7cSEd Maste 		case DW_CFA_advance_loc:
54652c23cb7cSEd Maste 			delta = oplist[i].fp_offset * caf;
54662c23cb7cSEd Maste 			pc += delta;
54672c23cb7cSEd Maste 			printf(": %ju to %08jx", (uintmax_t) delta,
54682c23cb7cSEd Maste 			    (uintmax_t) pc);
54692c23cb7cSEd Maste 			break;
54702c23cb7cSEd Maste 		case DW_CFA_offset:
54712c23cb7cSEd Maste 		case DW_CFA_offset_extended:
54722c23cb7cSEd Maste 		case DW_CFA_offset_extended_sf:
54732c23cb7cSEd Maste 			delta = oplist[i].fp_offset * daf;
5474cf781b2eSEd Maste 			printf(": r%u (%s) at cfa%+jd", oplist[i].fp_register,
5475cf781b2eSEd Maste 			    dwarf_regname(re, oplist[i].fp_register),
54762c23cb7cSEd Maste 			    (intmax_t) delta);
54772c23cb7cSEd Maste 			break;
54782c23cb7cSEd Maste 		case DW_CFA_restore:
5479cf781b2eSEd Maste 			printf(": r%u (%s)", oplist[i].fp_register,
5480cf781b2eSEd Maste 			    dwarf_regname(re, oplist[i].fp_register));
54812c23cb7cSEd Maste 			break;
54822c23cb7cSEd Maste 		case DW_CFA_set_loc:
54832c23cb7cSEd Maste 			pc = oplist[i].fp_offset;
54842c23cb7cSEd Maste 			printf(": to %08jx", (uintmax_t) pc);
54852c23cb7cSEd Maste 			break;
54862c23cb7cSEd Maste 		case DW_CFA_advance_loc1:
54872c23cb7cSEd Maste 		case DW_CFA_advance_loc2:
54882c23cb7cSEd Maste 		case DW_CFA_advance_loc4:
54892c23cb7cSEd Maste 			pc += oplist[i].fp_offset;
54902c23cb7cSEd Maste 			printf(": %jd to %08jx", (intmax_t) oplist[i].fp_offset,
54912c23cb7cSEd Maste 			    (uintmax_t) pc);
54922c23cb7cSEd Maste 			break;
54932c23cb7cSEd Maste 		case DW_CFA_def_cfa:
5494cf781b2eSEd Maste 			printf(": r%u (%s) ofs %ju", oplist[i].fp_register,
5495cf781b2eSEd Maste 			    dwarf_regname(re, oplist[i].fp_register),
54962c23cb7cSEd Maste 			    (uintmax_t) oplist[i].fp_offset);
54972c23cb7cSEd Maste 			break;
54982c23cb7cSEd Maste 		case DW_CFA_def_cfa_sf:
5499cf781b2eSEd Maste 			printf(": r%u (%s) ofs %jd", oplist[i].fp_register,
5500cf781b2eSEd Maste 			    dwarf_regname(re, oplist[i].fp_register),
55012c23cb7cSEd Maste 			    (intmax_t) (oplist[i].fp_offset * daf));
55022c23cb7cSEd Maste 			break;
55032c23cb7cSEd Maste 		case DW_CFA_def_cfa_register:
5504cf781b2eSEd Maste 			printf(": r%u (%s)", oplist[i].fp_register,
5505cf781b2eSEd Maste 			    dwarf_regname(re, oplist[i].fp_register));
55062c23cb7cSEd Maste 			break;
55072c23cb7cSEd Maste 		case DW_CFA_def_cfa_offset:
55082c23cb7cSEd Maste 			printf(": %ju", (uintmax_t) oplist[i].fp_offset);
55092c23cb7cSEd Maste 			break;
55102c23cb7cSEd Maste 		case DW_CFA_def_cfa_offset_sf:
55112c23cb7cSEd Maste 			printf(": %jd", (intmax_t) (oplist[i].fp_offset * daf));
55122c23cb7cSEd Maste 			break;
55132c23cb7cSEd Maste 		default:
55142c23cb7cSEd Maste 			break;
55152c23cb7cSEd Maste 		}
55162c23cb7cSEd Maste 		putchar('\n');
55172c23cb7cSEd Maste 	}
55182c23cb7cSEd Maste 
55192c23cb7cSEd Maste 	dwarf_dealloc(dbg, oplist, DW_DLA_FRAME_BLOCK);
55202c23cb7cSEd Maste }
55212c23cb7cSEd Maste 
55222c23cb7cSEd Maste static char *
5523cf781b2eSEd Maste get_regoff_str(struct readelf *re, Dwarf_Half reg, Dwarf_Addr off)
55242c23cb7cSEd Maste {
55252c23cb7cSEd Maste 	static char rs[16];
55262c23cb7cSEd Maste 
55272c23cb7cSEd Maste 	if (reg == DW_FRAME_UNDEFINED_VAL || reg == DW_FRAME_REG_INITIAL_VALUE)
55282c23cb7cSEd Maste 		snprintf(rs, sizeof(rs), "%c", 'u');
55292c23cb7cSEd Maste 	else if (reg == DW_FRAME_CFA_COL)
55302c23cb7cSEd Maste 		snprintf(rs, sizeof(rs), "c%+jd", (intmax_t) off);
55312c23cb7cSEd Maste 	else
5532cf781b2eSEd Maste 		snprintf(rs, sizeof(rs), "%s%+jd", dwarf_regname(re, reg),
5533cf781b2eSEd Maste 		    (intmax_t) off);
55342c23cb7cSEd Maste 
55352c23cb7cSEd Maste 	return (rs);
55362c23cb7cSEd Maste }
55372c23cb7cSEd Maste 
55382c23cb7cSEd Maste static int
5539cf781b2eSEd Maste dump_dwarf_frame_regtable(struct readelf *re, Dwarf_Fde fde, Dwarf_Addr pc,
5540cf781b2eSEd Maste     Dwarf_Unsigned func_len, Dwarf_Half cie_ra)
55412c23cb7cSEd Maste {
55422c23cb7cSEd Maste 	Dwarf_Regtable rt;
55432c23cb7cSEd Maste 	Dwarf_Addr row_pc, end_pc, pre_pc, cur_pc;
55442c23cb7cSEd Maste 	Dwarf_Error de;
55452c23cb7cSEd Maste 	char *vec;
55462c23cb7cSEd Maste 	int i;
55472c23cb7cSEd Maste 
55482c23cb7cSEd Maste #define BIT_SET(v, n) (v[(n)>>3] |= 1U << ((n) & 7))
55492c23cb7cSEd Maste #define BIT_CLR(v, n) (v[(n)>>3] &= ~(1U << ((n) & 7)))
55502c23cb7cSEd Maste #define BIT_ISSET(v, n) (v[(n)>>3] & (1U << ((n) & 7)))
55512c23cb7cSEd Maste #define	RT(x) rt.rules[(x)]
55522c23cb7cSEd Maste 
55532c23cb7cSEd Maste 	vec = calloc((DW_REG_TABLE_SIZE + 7) / 8, 1);
55542c23cb7cSEd Maste 	if (vec == NULL)
55552c23cb7cSEd Maste 		err(EXIT_FAILURE, "calloc failed");
55562c23cb7cSEd Maste 
55572c23cb7cSEd Maste 	pre_pc = ~((Dwarf_Addr) 0);
55582c23cb7cSEd Maste 	cur_pc = pc;
55592c23cb7cSEd Maste 	end_pc = pc + func_len;
55602c23cb7cSEd Maste 	for (; cur_pc < end_pc; cur_pc++) {
55612c23cb7cSEd Maste 		if (dwarf_get_fde_info_for_all_regs(fde, cur_pc, &rt, &row_pc,
55622c23cb7cSEd Maste 		    &de) != DW_DLV_OK) {
55632c23cb7cSEd Maste 			warnx("dwarf_get_fde_info_for_all_regs failed: %s\n",
55642c23cb7cSEd Maste 			    dwarf_errmsg(de));
55652c23cb7cSEd Maste 			return (-1);
55662c23cb7cSEd Maste 		}
55672c23cb7cSEd Maste 		if (row_pc == pre_pc)
55682c23cb7cSEd Maste 			continue;
55692c23cb7cSEd Maste 		pre_pc = row_pc;
55702c23cb7cSEd Maste 		for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
55712c23cb7cSEd Maste 			if (rt.rules[i].dw_regnum != DW_FRAME_REG_INITIAL_VALUE)
55722c23cb7cSEd Maste 				BIT_SET(vec, i);
55732c23cb7cSEd Maste 		}
55742c23cb7cSEd Maste 	}
55752c23cb7cSEd Maste 
55762c23cb7cSEd Maste 	printf("   LOC   CFA      ");
55772c23cb7cSEd Maste 	for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
55782c23cb7cSEd Maste 		if (BIT_ISSET(vec, i)) {
55792c23cb7cSEd Maste 			if ((Dwarf_Half) i == cie_ra)
55802c23cb7cSEd Maste 				printf("ra   ");
5581cf781b2eSEd Maste 			else
5582cf781b2eSEd Maste 				printf("%-5s",
5583cf781b2eSEd Maste 				    dwarf_regname(re, (unsigned int) i));
55842c23cb7cSEd Maste 		}
55852c23cb7cSEd Maste 	}
55862c23cb7cSEd Maste 	putchar('\n');
55872c23cb7cSEd Maste 
55882c23cb7cSEd Maste 	pre_pc = ~((Dwarf_Addr) 0);
55892c23cb7cSEd Maste 	cur_pc = pc;
55902c23cb7cSEd Maste 	end_pc = pc + func_len;
55912c23cb7cSEd Maste 	for (; cur_pc < end_pc; cur_pc++) {
55922c23cb7cSEd Maste 		if (dwarf_get_fde_info_for_all_regs(fde, cur_pc, &rt, &row_pc,
55932c23cb7cSEd Maste 		    &de) != DW_DLV_OK) {
55942c23cb7cSEd Maste 			warnx("dwarf_get_fde_info_for_all_regs failed: %s\n",
55952c23cb7cSEd Maste 			    dwarf_errmsg(de));
55962c23cb7cSEd Maste 			return (-1);
55972c23cb7cSEd Maste 		}
55982c23cb7cSEd Maste 		if (row_pc == pre_pc)
55992c23cb7cSEd Maste 			continue;
56002c23cb7cSEd Maste 		pre_pc = row_pc;
56012c23cb7cSEd Maste 		printf("%08jx ", (uintmax_t) row_pc);
5602cf781b2eSEd Maste 		printf("%-8s ", get_regoff_str(re, RT(0).dw_regnum,
56032c23cb7cSEd Maste 		    RT(0).dw_offset));
56042c23cb7cSEd Maste 		for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
56052c23cb7cSEd Maste 			if (BIT_ISSET(vec, i)) {
5606cf781b2eSEd Maste 				printf("%-5s", get_regoff_str(re,
5607cf781b2eSEd Maste 				    RT(i).dw_regnum, RT(i).dw_offset));
56082c23cb7cSEd Maste 			}
56092c23cb7cSEd Maste 		}
56102c23cb7cSEd Maste 		putchar('\n');
56112c23cb7cSEd Maste 	}
56122c23cb7cSEd Maste 
56132c23cb7cSEd Maste 	free(vec);
56142c23cb7cSEd Maste 
56152c23cb7cSEd Maste 	return (0);
56162c23cb7cSEd Maste 
56172c23cb7cSEd Maste #undef	BIT_SET
56182c23cb7cSEd Maste #undef	BIT_CLR
56192c23cb7cSEd Maste #undef	BIT_ISSET
56202c23cb7cSEd Maste #undef	RT
56212c23cb7cSEd Maste }
56222c23cb7cSEd Maste 
56232c23cb7cSEd Maste static void
56242c23cb7cSEd Maste dump_dwarf_frame_section(struct readelf *re, struct section *s, int alt)
56252c23cb7cSEd Maste {
56262c23cb7cSEd Maste 	Dwarf_Cie *cie_list, cie, pre_cie;
56272c23cb7cSEd Maste 	Dwarf_Fde *fde_list, fde;
56282c23cb7cSEd Maste 	Dwarf_Off cie_offset, fde_offset;
56292c23cb7cSEd Maste 	Dwarf_Unsigned cie_length, fde_instlen;
56302c23cb7cSEd Maste 	Dwarf_Unsigned cie_caf, cie_daf, cie_instlen, func_len, fde_length;
56312c23cb7cSEd Maste 	Dwarf_Signed cie_count, fde_count, cie_index;
56322c23cb7cSEd Maste 	Dwarf_Addr low_pc;
56332c23cb7cSEd Maste 	Dwarf_Half cie_ra;
56342c23cb7cSEd Maste 	Dwarf_Small cie_version;
56352c23cb7cSEd Maste 	Dwarf_Ptr fde_addr, fde_inst, cie_inst;
56362c23cb7cSEd Maste 	char *cie_aug, c;
56372c23cb7cSEd Maste 	int i, eh_frame;
56382c23cb7cSEd Maste 	Dwarf_Error de;
56392c23cb7cSEd Maste 
56402c23cb7cSEd Maste 	printf("\nThe section %s contains:\n\n", s->name);
56412c23cb7cSEd Maste 
56422c23cb7cSEd Maste 	if (!strcmp(s->name, ".debug_frame")) {
56432c23cb7cSEd Maste 		eh_frame = 0;
56442c23cb7cSEd Maste 		if (dwarf_get_fde_list(re->dbg, &cie_list, &cie_count,
56452c23cb7cSEd Maste 		    &fde_list, &fde_count, &de) != DW_DLV_OK) {
56462c23cb7cSEd Maste 			warnx("dwarf_get_fde_list failed: %s",
56472c23cb7cSEd Maste 			    dwarf_errmsg(de));
56482c23cb7cSEd Maste 			return;
56492c23cb7cSEd Maste 		}
56502c23cb7cSEd Maste 	} else if (!strcmp(s->name, ".eh_frame")) {
56512c23cb7cSEd Maste 		eh_frame = 1;
56522c23cb7cSEd Maste 		if (dwarf_get_fde_list_eh(re->dbg, &cie_list, &cie_count,
56532c23cb7cSEd Maste 		    &fde_list, &fde_count, &de) != DW_DLV_OK) {
56542c23cb7cSEd Maste 			warnx("dwarf_get_fde_list_eh failed: %s",
56552c23cb7cSEd Maste 			    dwarf_errmsg(de));
56562c23cb7cSEd Maste 			return;
56572c23cb7cSEd Maste 		}
56582c23cb7cSEd Maste 	} else
56592c23cb7cSEd Maste 		return;
56602c23cb7cSEd Maste 
56612c23cb7cSEd Maste 	pre_cie = NULL;
56622c23cb7cSEd Maste 	for (i = 0; i < fde_count; i++) {
56632c23cb7cSEd Maste 		if (dwarf_get_fde_n(fde_list, i, &fde, &de) != DW_DLV_OK) {
56642c23cb7cSEd Maste 			warnx("dwarf_get_fde_n failed: %s", dwarf_errmsg(de));
56652c23cb7cSEd Maste 			continue;
56662c23cb7cSEd Maste 		}
56672c23cb7cSEd Maste 		if (dwarf_get_cie_of_fde(fde, &cie, &de) != DW_DLV_OK) {
56682c23cb7cSEd Maste 			warnx("dwarf_get_fde_n failed: %s", dwarf_errmsg(de));
56692c23cb7cSEd Maste 			continue;
56702c23cb7cSEd Maste 		}
56712c23cb7cSEd Maste 		if (dwarf_get_fde_range(fde, &low_pc, &func_len, &fde_addr,
56722c23cb7cSEd Maste 		    &fde_length, &cie_offset, &cie_index, &fde_offset,
56732c23cb7cSEd Maste 		    &de) != DW_DLV_OK) {
56742c23cb7cSEd Maste 			warnx("dwarf_get_fde_range failed: %s",
56752c23cb7cSEd Maste 			    dwarf_errmsg(de));
56762c23cb7cSEd Maste 			continue;
56772c23cb7cSEd Maste 		}
56782c23cb7cSEd Maste 		if (dwarf_get_fde_instr_bytes(fde, &fde_inst, &fde_instlen,
56792c23cb7cSEd Maste 		    &de) != DW_DLV_OK) {
56802c23cb7cSEd Maste 			warnx("dwarf_get_fde_instr_bytes failed: %s",
56812c23cb7cSEd Maste 			    dwarf_errmsg(de));
56822c23cb7cSEd Maste 			continue;
56832c23cb7cSEd Maste 		}
56842c23cb7cSEd Maste 		if (pre_cie == NULL || cie != pre_cie) {
56852c23cb7cSEd Maste 			pre_cie = cie;
56862c23cb7cSEd Maste 			if (dwarf_get_cie_info(cie, &cie_length, &cie_version,
56872c23cb7cSEd Maste 			    &cie_aug, &cie_caf, &cie_daf, &cie_ra,
56882c23cb7cSEd Maste 			    &cie_inst, &cie_instlen, &de) != DW_DLV_OK) {
56892c23cb7cSEd Maste 				warnx("dwarf_get_cie_info failed: %s",
56902c23cb7cSEd Maste 				    dwarf_errmsg(de));
56912c23cb7cSEd Maste 				continue;
56922c23cb7cSEd Maste 			}
56932c23cb7cSEd Maste 			printf("%08jx %08jx %8.8jx CIE",
56942c23cb7cSEd Maste 			    (uintmax_t) cie_offset,
56952c23cb7cSEd Maste 			    (uintmax_t) cie_length,
56962c23cb7cSEd Maste 			    (uintmax_t) (eh_frame ? 0 : ~0U));
56972c23cb7cSEd Maste 			if (!alt) {
56982c23cb7cSEd Maste 				putchar('\n');
56992c23cb7cSEd Maste 				printf("  Version:\t\t\t%u\n", cie_version);
57002c23cb7cSEd Maste 				printf("  Augmentation:\t\t\t\"");
57012c23cb7cSEd Maste 				while ((c = *cie_aug++) != '\0')
57022c23cb7cSEd Maste 					putchar(c);
57032c23cb7cSEd Maste 				printf("\"\n");
57042c23cb7cSEd Maste 				printf("  Code alignment factor:\t%ju\n",
57052c23cb7cSEd Maste 				    (uintmax_t) cie_caf);
57062c23cb7cSEd Maste 				printf("  Data alignment factor:\t%jd\n",
57072c23cb7cSEd Maste 				    (intmax_t) cie_daf);
57082c23cb7cSEd Maste 				printf("  Return address column:\t%ju\n",
57092c23cb7cSEd Maste 				    (uintmax_t) cie_ra);
57102c23cb7cSEd Maste 				putchar('\n');
5711cf781b2eSEd Maste 				dump_dwarf_frame_inst(re, cie, cie_inst,
57122c23cb7cSEd Maste 				    cie_instlen, cie_caf, cie_daf, 0,
57132c23cb7cSEd Maste 				    re->dbg);
57142c23cb7cSEd Maste 				putchar('\n');
57152c23cb7cSEd Maste 			} else {
57162c23cb7cSEd Maste 				printf(" \"");
57172c23cb7cSEd Maste 				while ((c = *cie_aug++) != '\0')
57182c23cb7cSEd Maste 					putchar(c);
57192c23cb7cSEd Maste 				putchar('"');
57202c23cb7cSEd Maste 				printf(" cf=%ju df=%jd ra=%ju\n",
57212c23cb7cSEd Maste 				    (uintmax_t) cie_caf,
57222c23cb7cSEd Maste 				    (uintmax_t) cie_daf,
57232c23cb7cSEd Maste 				    (uintmax_t) cie_ra);
5724cf781b2eSEd Maste 				dump_dwarf_frame_regtable(re, fde, low_pc, 1,
57252c23cb7cSEd Maste 				    cie_ra);
57262c23cb7cSEd Maste 				putchar('\n');
57272c23cb7cSEd Maste 			}
57282c23cb7cSEd Maste 		}
57292c23cb7cSEd Maste 		printf("%08jx %08jx %08jx FDE cie=%08jx pc=%08jx..%08jx\n",
57302c23cb7cSEd Maste 		    (uintmax_t) fde_offset, (uintmax_t) fde_length,
57312c23cb7cSEd Maste 		    (uintmax_t) cie_offset,
57322c23cb7cSEd Maste 		    (uintmax_t) (eh_frame ? fde_offset + 4 - cie_offset :
57332c23cb7cSEd Maste 			cie_offset),
57342c23cb7cSEd Maste 		    (uintmax_t) low_pc, (uintmax_t) (low_pc + func_len));
57352c23cb7cSEd Maste 		if (!alt)
5736cf781b2eSEd Maste 			dump_dwarf_frame_inst(re, cie, fde_inst, fde_instlen,
57372c23cb7cSEd Maste 			    cie_caf, cie_daf, low_pc, re->dbg);
57382c23cb7cSEd Maste 		else
5739cf781b2eSEd Maste 			dump_dwarf_frame_regtable(re, fde, low_pc, func_len,
57402c23cb7cSEd Maste 			    cie_ra);
57412c23cb7cSEd Maste 		putchar('\n');
57422c23cb7cSEd Maste 	}
57432c23cb7cSEd Maste }
57442c23cb7cSEd Maste 
57452c23cb7cSEd Maste static void
57462c23cb7cSEd Maste dump_dwarf_frame(struct readelf *re, int alt)
57472c23cb7cSEd Maste {
57482c23cb7cSEd Maste 	struct section *s;
57492c23cb7cSEd Maste 	int i;
57502c23cb7cSEd Maste 
57512c23cb7cSEd Maste 	(void) dwarf_set_frame_cfa_value(re->dbg, DW_FRAME_CFA_COL);
57522c23cb7cSEd Maste 
57532c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
57542c23cb7cSEd Maste 		s = &re->sl[i];
57552c23cb7cSEd Maste 		if (s->name != NULL && (!strcmp(s->name, ".debug_frame") ||
57562c23cb7cSEd Maste 		    !strcmp(s->name, ".eh_frame")))
57572c23cb7cSEd Maste 			dump_dwarf_frame_section(re, s, alt);
57582c23cb7cSEd Maste 	}
57592c23cb7cSEd Maste }
57602c23cb7cSEd Maste 
57612c23cb7cSEd Maste static void
57622c23cb7cSEd Maste dump_dwarf_str(struct readelf *re)
57632c23cb7cSEd Maste {
57642c23cb7cSEd Maste 	struct section *s;
57652c23cb7cSEd Maste 	Elf_Data *d;
57662c23cb7cSEd Maste 	unsigned char *p;
57672c23cb7cSEd Maste 	int elferr, end, i, j;
57682c23cb7cSEd Maste 
57692c23cb7cSEd Maste 	printf("\nContents of section .debug_str:\n");
57702c23cb7cSEd Maste 
57712c23cb7cSEd Maste 	s = NULL;
57722c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
57732c23cb7cSEd Maste 		s = &re->sl[i];
57742c23cb7cSEd Maste 		if (s->name != NULL && !strcmp(s->name, ".debug_str"))
57752c23cb7cSEd Maste 			break;
57762c23cb7cSEd Maste 	}
57772c23cb7cSEd Maste 	if ((size_t) i >= re->shnum)
57782c23cb7cSEd Maste 		return;
57792c23cb7cSEd Maste 
57802c23cb7cSEd Maste 	(void) elf_errno();
57812c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
57822c23cb7cSEd Maste 		elferr = elf_errno();
57832c23cb7cSEd Maste 		if (elferr != 0)
57842c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
57852c23cb7cSEd Maste 		return;
57862c23cb7cSEd Maste 	}
57872c23cb7cSEd Maste 	if (d->d_size <= 0)
57882c23cb7cSEd Maste 		return;
57892c23cb7cSEd Maste 
57902c23cb7cSEd Maste 	for (i = 0, p = d->d_buf; (size_t) i < d->d_size; i += 16) {
57912c23cb7cSEd Maste 		printf("  0x%08x", (unsigned int) i);
57922c23cb7cSEd Maste 		if ((size_t) i + 16 > d->d_size)
57932c23cb7cSEd Maste 			end = d->d_size;
57942c23cb7cSEd Maste 		else
57952c23cb7cSEd Maste 			end = i + 16;
57962c23cb7cSEd Maste 		for (j = i; j < i + 16; j++) {
57972c23cb7cSEd Maste 			if ((j - i) % 4 == 0)
57982c23cb7cSEd Maste 				putchar(' ');
57992c23cb7cSEd Maste 			if (j >= end) {
58002c23cb7cSEd Maste 				printf("  ");
58012c23cb7cSEd Maste 				continue;
58022c23cb7cSEd Maste 			}
58032c23cb7cSEd Maste 			printf("%02x", (uint8_t) p[j]);
58042c23cb7cSEd Maste 		}
58052c23cb7cSEd Maste 		putchar(' ');
58062c23cb7cSEd Maste 		for (j = i; j < end; j++) {
58072c23cb7cSEd Maste 			if (isprint(p[j]))
58082c23cb7cSEd Maste 				putchar(p[j]);
58092c23cb7cSEd Maste 			else if (p[j] == 0)
58102c23cb7cSEd Maste 				putchar('.');
58112c23cb7cSEd Maste 			else
58122c23cb7cSEd Maste 				putchar(' ');
58132c23cb7cSEd Maste 		}
58142c23cb7cSEd Maste 		putchar('\n');
58152c23cb7cSEd Maste 	}
58162c23cb7cSEd Maste }
58172c23cb7cSEd Maste 
58182c23cb7cSEd Maste struct loc_at {
58192c23cb7cSEd Maste 	Dwarf_Attribute la_at;
58202c23cb7cSEd Maste 	Dwarf_Unsigned la_off;
58212c23cb7cSEd Maste 	Dwarf_Unsigned la_lowpc;
5822cf781b2eSEd Maste 	Dwarf_Half la_cu_psize;
5823cf781b2eSEd Maste 	Dwarf_Half la_cu_osize;
5824cf781b2eSEd Maste 	Dwarf_Half la_cu_ver;
58252c23cb7cSEd Maste 	TAILQ_ENTRY(loc_at) la_next;
58262c23cb7cSEd Maste };
58272c23cb7cSEd Maste 
58282c23cb7cSEd Maste static TAILQ_HEAD(, loc_at) lalist = TAILQ_HEAD_INITIALIZER(lalist);
58292c23cb7cSEd Maste 
58302c23cb7cSEd Maste static void
58312c23cb7cSEd Maste search_loclist_at(struct readelf *re, Dwarf_Die die, Dwarf_Unsigned lowpc)
58322c23cb7cSEd Maste {
58332c23cb7cSEd Maste 	Dwarf_Attribute *attr_list;
58342c23cb7cSEd Maste 	Dwarf_Die ret_die;
58352c23cb7cSEd Maste 	Dwarf_Unsigned off;
5836cf781b2eSEd Maste 	Dwarf_Off ref;
58372c23cb7cSEd Maste 	Dwarf_Signed attr_count;
58382c23cb7cSEd Maste 	Dwarf_Half attr, form;
5839cf781b2eSEd Maste 	Dwarf_Bool is_info;
58402c23cb7cSEd Maste 	Dwarf_Error de;
58412c23cb7cSEd Maste 	struct loc_at *la, *nla;
58422c23cb7cSEd Maste 	int i, ret;
58432c23cb7cSEd Maste 
5844cf781b2eSEd Maste 	is_info = dwarf_get_die_infotypes_flag(die);
5845cf781b2eSEd Maste 
58462c23cb7cSEd Maste 	if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
58472c23cb7cSEd Maste 	    DW_DLV_OK) {
58482c23cb7cSEd Maste 		if (ret == DW_DLV_ERROR)
58492c23cb7cSEd Maste 			warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
58502c23cb7cSEd Maste 		goto cont_search;
58512c23cb7cSEd Maste 	}
58522c23cb7cSEd Maste 	for (i = 0; i < attr_count; i++) {
58532c23cb7cSEd Maste 		if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
58542c23cb7cSEd Maste 			warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
58552c23cb7cSEd Maste 			continue;
58562c23cb7cSEd Maste 		}
58572c23cb7cSEd Maste 		if (attr != DW_AT_location &&
58582c23cb7cSEd Maste 		    attr != DW_AT_string_length &&
58592c23cb7cSEd Maste 		    attr != DW_AT_return_addr &&
58602c23cb7cSEd Maste 		    attr != DW_AT_data_member_location &&
58612c23cb7cSEd Maste 		    attr != DW_AT_frame_base &&
58622c23cb7cSEd Maste 		    attr != DW_AT_segment &&
58632c23cb7cSEd Maste 		    attr != DW_AT_static_link &&
58642c23cb7cSEd Maste 		    attr != DW_AT_use_location &&
58652c23cb7cSEd Maste 		    attr != DW_AT_vtable_elem_location)
58662c23cb7cSEd Maste 			continue;
58672c23cb7cSEd Maste 		if (dwarf_whatform(attr_list[i], &form, &de) != DW_DLV_OK) {
58682c23cb7cSEd Maste 			warnx("dwarf_whatform failed: %s", dwarf_errmsg(de));
58692c23cb7cSEd Maste 			continue;
58702c23cb7cSEd Maste 		}
5871cf781b2eSEd Maste 		if (form == DW_FORM_data4 || form == DW_FORM_data8) {
5872cf781b2eSEd Maste 			if (dwarf_formudata(attr_list[i], &off, &de) !=
5873cf781b2eSEd Maste 			    DW_DLV_OK) {
5874cf781b2eSEd Maste 				warnx("dwarf_formudata failed: %s",
5875cf781b2eSEd Maste 				    dwarf_errmsg(de));
58762c23cb7cSEd Maste 				continue;
58772c23cb7cSEd Maste 			}
5878cf781b2eSEd Maste 		} else if (form == DW_FORM_sec_offset) {
5879cf781b2eSEd Maste 			if (dwarf_global_formref(attr_list[i], &ref, &de) !=
5880cf781b2eSEd Maste 			    DW_DLV_OK) {
5881cf781b2eSEd Maste 				warnx("dwarf_global_formref failed: %s",
5882cf781b2eSEd Maste 				    dwarf_errmsg(de));
5883cf781b2eSEd Maste 				continue;
5884cf781b2eSEd Maste 			}
5885cf781b2eSEd Maste 			off = ref;
5886cf781b2eSEd Maste 		} else
5887cf781b2eSEd Maste 			continue;
5888cf781b2eSEd Maste 
58892c23cb7cSEd Maste 		TAILQ_FOREACH(la, &lalist, la_next) {
58902c23cb7cSEd Maste 			if (off == la->la_off)
58912c23cb7cSEd Maste 				break;
58922c23cb7cSEd Maste 			if (off < la->la_off) {
58932c23cb7cSEd Maste 				if ((nla = malloc(sizeof(*nla))) == NULL)
58942c23cb7cSEd Maste 					err(EXIT_FAILURE, "malloc failed");
58952c23cb7cSEd Maste 				nla->la_at = attr_list[i];
58962c23cb7cSEd Maste 				nla->la_off = off;
58972c23cb7cSEd Maste 				nla->la_lowpc = lowpc;
5898cf781b2eSEd Maste 				nla->la_cu_psize = re->cu_psize;
5899cf781b2eSEd Maste 				nla->la_cu_osize = re->cu_osize;
5900cf781b2eSEd Maste 				nla->la_cu_ver = re->cu_ver;
59012c23cb7cSEd Maste 				TAILQ_INSERT_BEFORE(la, nla, la_next);
59022c23cb7cSEd Maste 				break;
59032c23cb7cSEd Maste 			}
59042c23cb7cSEd Maste 		}
59052c23cb7cSEd Maste 		if (la == NULL) {
59062c23cb7cSEd Maste 			if ((nla = malloc(sizeof(*nla))) == NULL)
59072c23cb7cSEd Maste 				err(EXIT_FAILURE, "malloc failed");
59082c23cb7cSEd Maste 			nla->la_at = attr_list[i];
59092c23cb7cSEd Maste 			nla->la_off = off;
59102c23cb7cSEd Maste 			nla->la_lowpc = lowpc;
5911cf781b2eSEd Maste 			nla->la_cu_psize = re->cu_psize;
5912cf781b2eSEd Maste 			nla->la_cu_osize = re->cu_osize;
5913cf781b2eSEd Maste 			nla->la_cu_ver = re->cu_ver;
59142c23cb7cSEd Maste 			TAILQ_INSERT_TAIL(&lalist, nla, la_next);
59152c23cb7cSEd Maste 		}
59162c23cb7cSEd Maste 	}
59172c23cb7cSEd Maste 
59182c23cb7cSEd Maste cont_search:
59192c23cb7cSEd Maste 	/* Search children. */
59202c23cb7cSEd Maste 	ret = dwarf_child(die, &ret_die, &de);
59212c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
59222c23cb7cSEd Maste 		warnx("dwarf_child: %s", dwarf_errmsg(de));
59232c23cb7cSEd Maste 	else if (ret == DW_DLV_OK)
59242c23cb7cSEd Maste 		search_loclist_at(re, ret_die, lowpc);
59252c23cb7cSEd Maste 
59262c23cb7cSEd Maste 	/* Search sibling. */
5927cf781b2eSEd Maste 	ret = dwarf_siblingof_b(re->dbg, die, &ret_die, is_info, &de);
59282c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
59292c23cb7cSEd Maste 		warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
59302c23cb7cSEd Maste 	else if (ret == DW_DLV_OK)
59312c23cb7cSEd Maste 		search_loclist_at(re, ret_die, lowpc);
59322c23cb7cSEd Maste }
59332c23cb7cSEd Maste 
59342c23cb7cSEd Maste static void
5935cf781b2eSEd Maste dump_dwarf_loc(struct readelf *re, Dwarf_Loc *lr)
59362c23cb7cSEd Maste {
59372c23cb7cSEd Maste 	const char *op_str;
5938cf781b2eSEd Maste 	char unk_op[32];
5939cf781b2eSEd Maste 	uint8_t *b, n;
5940cf781b2eSEd Maste 	int i;
59412c23cb7cSEd Maste 
59422c23cb7cSEd Maste 	if (dwarf_get_OP_name(lr->lr_atom, &op_str) !=
59432c23cb7cSEd Maste 	    DW_DLV_OK) {
5944cf781b2eSEd Maste 		snprintf(unk_op, sizeof(unk_op),
5945cf781b2eSEd Maste 		    "[Unknown OP: %#x]", lr->lr_atom);
5946cf781b2eSEd Maste 		op_str = unk_op;
59472c23cb7cSEd Maste 	}
59482c23cb7cSEd Maste 
59492c23cb7cSEd Maste 	printf("%s", op_str);
59502c23cb7cSEd Maste 
59512c23cb7cSEd Maste 	switch (lr->lr_atom) {
59522c23cb7cSEd Maste 	case DW_OP_reg0:
59532c23cb7cSEd Maste 	case DW_OP_reg1:
59542c23cb7cSEd Maste 	case DW_OP_reg2:
59552c23cb7cSEd Maste 	case DW_OP_reg3:
59562c23cb7cSEd Maste 	case DW_OP_reg4:
59572c23cb7cSEd Maste 	case DW_OP_reg5:
59582c23cb7cSEd Maste 	case DW_OP_reg6:
59592c23cb7cSEd Maste 	case DW_OP_reg7:
59602c23cb7cSEd Maste 	case DW_OP_reg8:
59612c23cb7cSEd Maste 	case DW_OP_reg9:
59622c23cb7cSEd Maste 	case DW_OP_reg10:
59632c23cb7cSEd Maste 	case DW_OP_reg11:
59642c23cb7cSEd Maste 	case DW_OP_reg12:
59652c23cb7cSEd Maste 	case DW_OP_reg13:
59662c23cb7cSEd Maste 	case DW_OP_reg14:
59672c23cb7cSEd Maste 	case DW_OP_reg15:
59682c23cb7cSEd Maste 	case DW_OP_reg16:
59692c23cb7cSEd Maste 	case DW_OP_reg17:
59702c23cb7cSEd Maste 	case DW_OP_reg18:
59712c23cb7cSEd Maste 	case DW_OP_reg19:
59722c23cb7cSEd Maste 	case DW_OP_reg20:
59732c23cb7cSEd Maste 	case DW_OP_reg21:
59742c23cb7cSEd Maste 	case DW_OP_reg22:
59752c23cb7cSEd Maste 	case DW_OP_reg23:
59762c23cb7cSEd Maste 	case DW_OP_reg24:
59772c23cb7cSEd Maste 	case DW_OP_reg25:
59782c23cb7cSEd Maste 	case DW_OP_reg26:
59792c23cb7cSEd Maste 	case DW_OP_reg27:
59802c23cb7cSEd Maste 	case DW_OP_reg28:
59812c23cb7cSEd Maste 	case DW_OP_reg29:
59822c23cb7cSEd Maste 	case DW_OP_reg30:
59832c23cb7cSEd Maste 	case DW_OP_reg31:
5984cf781b2eSEd Maste 		printf(" (%s)", dwarf_regname(re, lr->lr_atom - DW_OP_reg0));
5985cf781b2eSEd Maste 		break;
5986cf781b2eSEd Maste 
5987cf781b2eSEd Maste 	case DW_OP_deref:
59882c23cb7cSEd Maste 	case DW_OP_lit0:
59892c23cb7cSEd Maste 	case DW_OP_lit1:
59902c23cb7cSEd Maste 	case DW_OP_lit2:
59912c23cb7cSEd Maste 	case DW_OP_lit3:
59922c23cb7cSEd Maste 	case DW_OP_lit4:
59932c23cb7cSEd Maste 	case DW_OP_lit5:
59942c23cb7cSEd Maste 	case DW_OP_lit6:
59952c23cb7cSEd Maste 	case DW_OP_lit7:
59962c23cb7cSEd Maste 	case DW_OP_lit8:
59972c23cb7cSEd Maste 	case DW_OP_lit9:
59982c23cb7cSEd Maste 	case DW_OP_lit10:
59992c23cb7cSEd Maste 	case DW_OP_lit11:
60002c23cb7cSEd Maste 	case DW_OP_lit12:
60012c23cb7cSEd Maste 	case DW_OP_lit13:
60022c23cb7cSEd Maste 	case DW_OP_lit14:
60032c23cb7cSEd Maste 	case DW_OP_lit15:
60042c23cb7cSEd Maste 	case DW_OP_lit16:
60052c23cb7cSEd Maste 	case DW_OP_lit17:
60062c23cb7cSEd Maste 	case DW_OP_lit18:
60072c23cb7cSEd Maste 	case DW_OP_lit19:
60082c23cb7cSEd Maste 	case DW_OP_lit20:
60092c23cb7cSEd Maste 	case DW_OP_lit21:
60102c23cb7cSEd Maste 	case DW_OP_lit22:
60112c23cb7cSEd Maste 	case DW_OP_lit23:
60122c23cb7cSEd Maste 	case DW_OP_lit24:
60132c23cb7cSEd Maste 	case DW_OP_lit25:
60142c23cb7cSEd Maste 	case DW_OP_lit26:
60152c23cb7cSEd Maste 	case DW_OP_lit27:
60162c23cb7cSEd Maste 	case DW_OP_lit28:
60172c23cb7cSEd Maste 	case DW_OP_lit29:
60182c23cb7cSEd Maste 	case DW_OP_lit30:
60192c23cb7cSEd Maste 	case DW_OP_lit31:
60202c23cb7cSEd Maste 	case DW_OP_dup:
60212c23cb7cSEd Maste 	case DW_OP_drop:
60222c23cb7cSEd Maste 	case DW_OP_over:
60232c23cb7cSEd Maste 	case DW_OP_swap:
60242c23cb7cSEd Maste 	case DW_OP_rot:
60252c23cb7cSEd Maste 	case DW_OP_xderef:
60262c23cb7cSEd Maste 	case DW_OP_abs:
60272c23cb7cSEd Maste 	case DW_OP_and:
60282c23cb7cSEd Maste 	case DW_OP_div:
60292c23cb7cSEd Maste 	case DW_OP_minus:
60302c23cb7cSEd Maste 	case DW_OP_mod:
60312c23cb7cSEd Maste 	case DW_OP_mul:
60322c23cb7cSEd Maste 	case DW_OP_neg:
60332c23cb7cSEd Maste 	case DW_OP_not:
60342c23cb7cSEd Maste 	case DW_OP_or:
60352c23cb7cSEd Maste 	case DW_OP_plus:
60362c23cb7cSEd Maste 	case DW_OP_shl:
60372c23cb7cSEd Maste 	case DW_OP_shr:
60382c23cb7cSEd Maste 	case DW_OP_shra:
60392c23cb7cSEd Maste 	case DW_OP_xor:
60402c23cb7cSEd Maste 	case DW_OP_eq:
60412c23cb7cSEd Maste 	case DW_OP_ge:
60422c23cb7cSEd Maste 	case DW_OP_gt:
60432c23cb7cSEd Maste 	case DW_OP_le:
60442c23cb7cSEd Maste 	case DW_OP_lt:
60452c23cb7cSEd Maste 	case DW_OP_ne:
60462c23cb7cSEd Maste 	case DW_OP_nop:
6047cf781b2eSEd Maste 	case DW_OP_push_object_address:
6048cf781b2eSEd Maste 	case DW_OP_form_tls_address:
6049cf781b2eSEd Maste 	case DW_OP_call_frame_cfa:
6050cf781b2eSEd Maste 	case DW_OP_stack_value:
6051cf781b2eSEd Maste 	case DW_OP_GNU_push_tls_address:
6052cf781b2eSEd Maste 	case DW_OP_GNU_uninit:
60532c23cb7cSEd Maste 		break;
60542c23cb7cSEd Maste 
60552c23cb7cSEd Maste 	case DW_OP_const1u:
60562c23cb7cSEd Maste 	case DW_OP_pick:
60572c23cb7cSEd Maste 	case DW_OP_deref_size:
60582c23cb7cSEd Maste 	case DW_OP_xderef_size:
60592c23cb7cSEd Maste 	case DW_OP_const2u:
60602c23cb7cSEd Maste 	case DW_OP_bra:
60612c23cb7cSEd Maste 	case DW_OP_skip:
60622c23cb7cSEd Maste 	case DW_OP_const4u:
60632c23cb7cSEd Maste 	case DW_OP_const8u:
60642c23cb7cSEd Maste 	case DW_OP_constu:
60652c23cb7cSEd Maste 	case DW_OP_plus_uconst:
60662c23cb7cSEd Maste 	case DW_OP_regx:
60672c23cb7cSEd Maste 	case DW_OP_piece:
60682c23cb7cSEd Maste 		printf(": %ju", (uintmax_t)
60692c23cb7cSEd Maste 		    lr->lr_number);
60702c23cb7cSEd Maste 		break;
60712c23cb7cSEd Maste 
6072cf781b2eSEd Maste 	case DW_OP_const1s:
6073cf781b2eSEd Maste 	case DW_OP_const2s:
6074cf781b2eSEd Maste 	case DW_OP_const4s:
6075cf781b2eSEd Maste 	case DW_OP_const8s:
60762c23cb7cSEd Maste 	case DW_OP_consts:
6077cf781b2eSEd Maste 		printf(": %jd", (intmax_t)
6078cf781b2eSEd Maste 		    lr->lr_number);
6079cf781b2eSEd Maste 		break;
6080cf781b2eSEd Maste 
60812c23cb7cSEd Maste 	case DW_OP_breg0:
60822c23cb7cSEd Maste 	case DW_OP_breg1:
60832c23cb7cSEd Maste 	case DW_OP_breg2:
60842c23cb7cSEd Maste 	case DW_OP_breg3:
60852c23cb7cSEd Maste 	case DW_OP_breg4:
60862c23cb7cSEd Maste 	case DW_OP_breg5:
60872c23cb7cSEd Maste 	case DW_OP_breg6:
60882c23cb7cSEd Maste 	case DW_OP_breg7:
60892c23cb7cSEd Maste 	case DW_OP_breg8:
60902c23cb7cSEd Maste 	case DW_OP_breg9:
60912c23cb7cSEd Maste 	case DW_OP_breg10:
60922c23cb7cSEd Maste 	case DW_OP_breg11:
60932c23cb7cSEd Maste 	case DW_OP_breg12:
60942c23cb7cSEd Maste 	case DW_OP_breg13:
60952c23cb7cSEd Maste 	case DW_OP_breg14:
60962c23cb7cSEd Maste 	case DW_OP_breg15:
60972c23cb7cSEd Maste 	case DW_OP_breg16:
60982c23cb7cSEd Maste 	case DW_OP_breg17:
60992c23cb7cSEd Maste 	case DW_OP_breg18:
61002c23cb7cSEd Maste 	case DW_OP_breg19:
61012c23cb7cSEd Maste 	case DW_OP_breg20:
61022c23cb7cSEd Maste 	case DW_OP_breg21:
61032c23cb7cSEd Maste 	case DW_OP_breg22:
61042c23cb7cSEd Maste 	case DW_OP_breg23:
61052c23cb7cSEd Maste 	case DW_OP_breg24:
61062c23cb7cSEd Maste 	case DW_OP_breg25:
61072c23cb7cSEd Maste 	case DW_OP_breg26:
61082c23cb7cSEd Maste 	case DW_OP_breg27:
61092c23cb7cSEd Maste 	case DW_OP_breg28:
61102c23cb7cSEd Maste 	case DW_OP_breg29:
61112c23cb7cSEd Maste 	case DW_OP_breg30:
61122c23cb7cSEd Maste 	case DW_OP_breg31:
6113cf781b2eSEd Maste 		printf(" (%s): %jd",
6114cf781b2eSEd Maste 		    dwarf_regname(re, lr->lr_atom - DW_OP_breg0),
6115cf781b2eSEd Maste 		    (intmax_t) lr->lr_number);
6116cf781b2eSEd Maste 		break;
6117cf781b2eSEd Maste 
61182c23cb7cSEd Maste 	case DW_OP_fbreg:
61192c23cb7cSEd Maste 		printf(": %jd", (intmax_t)
61202c23cb7cSEd Maste 		    lr->lr_number);
61212c23cb7cSEd Maste 		break;
61222c23cb7cSEd Maste 
61232c23cb7cSEd Maste 	case DW_OP_bregx:
6124cf781b2eSEd Maste 		printf(": %ju (%s) %jd",
61252c23cb7cSEd Maste 		    (uintmax_t) lr->lr_number,
6126cf781b2eSEd Maste 		    dwarf_regname(re, (unsigned int) lr->lr_number),
61272c23cb7cSEd Maste 		    (intmax_t) lr->lr_number2);
61282c23cb7cSEd Maste 		break;
61292c23cb7cSEd Maste 
61302c23cb7cSEd Maste 	case DW_OP_addr:
6131cf781b2eSEd Maste 	case DW_OP_GNU_encoded_addr:
61322c23cb7cSEd Maste 		printf(": %#jx", (uintmax_t)
61332c23cb7cSEd Maste 		    lr->lr_number);
61342c23cb7cSEd Maste 		break;
6135cf781b2eSEd Maste 
6136cf781b2eSEd Maste 	case DW_OP_GNU_implicit_pointer:
6137cf781b2eSEd Maste 		printf(": <0x%jx> %jd", (uintmax_t) lr->lr_number,
6138cf781b2eSEd Maste 		    (intmax_t) lr->lr_number2);
6139cf781b2eSEd Maste 		break;
6140cf781b2eSEd Maste 
6141cf781b2eSEd Maste 	case DW_OP_implicit_value:
6142cf781b2eSEd Maste 		printf(": %ju byte block:", (uintmax_t) lr->lr_number);
6143cf781b2eSEd Maste 		b = (uint8_t *)(uintptr_t) lr->lr_number2;
6144cf781b2eSEd Maste 		for (i = 0; (Dwarf_Unsigned) i < lr->lr_number; i++)
6145cf781b2eSEd Maste 			printf(" %x", b[i]);
6146cf781b2eSEd Maste 		break;
6147cf781b2eSEd Maste 
6148cf781b2eSEd Maste 	case DW_OP_GNU_entry_value:
6149cf781b2eSEd Maste 		printf(": (");
6150cf781b2eSEd Maste 		dump_dwarf_block(re, (uint8_t *)(uintptr_t) lr->lr_number2,
6151cf781b2eSEd Maste 		    lr->lr_number);
6152cf781b2eSEd Maste 		putchar(')');
6153cf781b2eSEd Maste 		break;
6154cf781b2eSEd Maste 
6155cf781b2eSEd Maste 	case DW_OP_GNU_const_type:
6156cf781b2eSEd Maste 		printf(": <0x%jx> ", (uintmax_t) lr->lr_number);
6157cf781b2eSEd Maste 		b = (uint8_t *)(uintptr_t) lr->lr_number2;
6158cf781b2eSEd Maste 		n = *b;
6159cf781b2eSEd Maste 		for (i = 1; (uint8_t) i < n; i++)
6160cf781b2eSEd Maste 			printf(" %x", b[i]);
6161cf781b2eSEd Maste 		break;
6162cf781b2eSEd Maste 
6163cf781b2eSEd Maste 	case DW_OP_GNU_regval_type:
6164cf781b2eSEd Maste 		printf(": %ju (%s) <0x%jx>", (uintmax_t) lr->lr_number,
6165cf781b2eSEd Maste 		    dwarf_regname(re, (unsigned int) lr->lr_number),
6166cf781b2eSEd Maste 		    (uintmax_t) lr->lr_number2);
6167cf781b2eSEd Maste 		break;
6168cf781b2eSEd Maste 
6169cf781b2eSEd Maste 	case DW_OP_GNU_convert:
6170cf781b2eSEd Maste 	case DW_OP_GNU_deref_type:
6171cf781b2eSEd Maste 	case DW_OP_GNU_parameter_ref:
6172cf781b2eSEd Maste 	case DW_OP_GNU_reinterpret:
6173cf781b2eSEd Maste 		printf(": <0x%jx>", (uintmax_t) lr->lr_number);
6174cf781b2eSEd Maste 		break;
6175cf781b2eSEd Maste 
6176cf781b2eSEd Maste 	default:
6177cf781b2eSEd Maste 		break;
61782c23cb7cSEd Maste 	}
6179cf781b2eSEd Maste }
6180cf781b2eSEd Maste 
6181cf781b2eSEd Maste static void
6182cf781b2eSEd Maste dump_dwarf_block(struct readelf *re, uint8_t *b, Dwarf_Unsigned len)
6183cf781b2eSEd Maste {
6184cf781b2eSEd Maste 	Dwarf_Locdesc *llbuf;
6185cf781b2eSEd Maste 	Dwarf_Signed lcnt;
6186cf781b2eSEd Maste 	Dwarf_Error de;
6187cf781b2eSEd Maste 	int i;
6188cf781b2eSEd Maste 
6189cf781b2eSEd Maste 	if (dwarf_loclist_from_expr_b(re->dbg, b, len, re->cu_psize,
6190cf781b2eSEd Maste 	    re->cu_osize, re->cu_ver, &llbuf, &lcnt, &de) != DW_DLV_OK) {
6191cf781b2eSEd Maste 		warnx("dwarf_loclist_form_expr_b: %s", dwarf_errmsg(de));
6192cf781b2eSEd Maste 		return;
6193cf781b2eSEd Maste 	}
6194cf781b2eSEd Maste 
6195cf781b2eSEd Maste 	for (i = 0; (Dwarf_Half) i < llbuf->ld_cents; i++) {
6196cf781b2eSEd Maste 		dump_dwarf_loc(re, &llbuf->ld_s[i]);
6197cf781b2eSEd Maste 		if (i < llbuf->ld_cents - 1)
6198cf781b2eSEd Maste 			printf("; ");
6199cf781b2eSEd Maste 	}
6200cf781b2eSEd Maste 
6201cf781b2eSEd Maste 	dwarf_dealloc(re->dbg, llbuf->ld_s, DW_DLA_LOC_BLOCK);
6202cf781b2eSEd Maste 	dwarf_dealloc(re->dbg, llbuf, DW_DLA_LOCDESC);
6203cf781b2eSEd Maste }
6204cf781b2eSEd Maste 
6205cf781b2eSEd Maste static void
6206cf781b2eSEd Maste dump_dwarf_loclist(struct readelf *re)
6207cf781b2eSEd Maste {
6208cf781b2eSEd Maste 	Dwarf_Die die;
6209cf781b2eSEd Maste 	Dwarf_Locdesc **llbuf;
6210cf781b2eSEd Maste 	Dwarf_Unsigned lowpc;
6211cf781b2eSEd Maste 	Dwarf_Signed lcnt;
6212cf781b2eSEd Maste 	Dwarf_Half tag, version, pointer_size, off_size;
6213cf781b2eSEd Maste 	Dwarf_Error de;
6214cf781b2eSEd Maste 	struct loc_at *la;
6215cf781b2eSEd Maste 	int i, j, ret;
6216cf781b2eSEd Maste 
6217cf781b2eSEd Maste 	printf("\nContents of section .debug_loc:\n");
6218cf781b2eSEd Maste 
6219cf781b2eSEd Maste 	/* Search .debug_info section. */
6220cf781b2eSEd Maste 	while ((ret = dwarf_next_cu_header_b(re->dbg, NULL, &version, NULL,
6221cf781b2eSEd Maste 	    &pointer_size, &off_size, NULL, NULL, &de)) == DW_DLV_OK) {
6222cf781b2eSEd Maste 		set_cu_context(re, pointer_size, off_size, version);
6223cf781b2eSEd Maste 		die = NULL;
6224cf781b2eSEd Maste 		if (dwarf_siblingof(re->dbg, die, &die, &de) != DW_DLV_OK)
6225cf781b2eSEd Maste 			continue;
6226cf781b2eSEd Maste 		if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
6227cf781b2eSEd Maste 			warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
6228cf781b2eSEd Maste 			continue;
6229cf781b2eSEd Maste 		}
6230cf781b2eSEd Maste 		/* XXX: What about DW_TAG_partial_unit? */
6231cf781b2eSEd Maste 		lowpc = 0;
6232cf781b2eSEd Maste 		if (tag == DW_TAG_compile_unit) {
6233cf781b2eSEd Maste 			if (dwarf_attrval_unsigned(die, DW_AT_low_pc,
6234cf781b2eSEd Maste 				&lowpc, &de) != DW_DLV_OK)
6235cf781b2eSEd Maste 				lowpc = 0;
6236cf781b2eSEd Maste 		}
6237cf781b2eSEd Maste 
6238cf781b2eSEd Maste 		/* Search attributes for reference to .debug_loc section. */
6239cf781b2eSEd Maste 		search_loclist_at(re, die, lowpc);
6240cf781b2eSEd Maste 	}
6241cf781b2eSEd Maste 	if (ret == DW_DLV_ERROR)
6242cf781b2eSEd Maste 		warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
6243cf781b2eSEd Maste 
6244cf781b2eSEd Maste 	/* Search .debug_types section. */
6245cf781b2eSEd Maste 	do {
6246cf781b2eSEd Maste 		while ((ret = dwarf_next_cu_header_c(re->dbg, 0, NULL,
6247cf781b2eSEd Maste 		    &version, NULL, &pointer_size, &off_size, NULL, NULL,
6248cf781b2eSEd Maste 		    NULL, NULL, &de)) == DW_DLV_OK) {
6249cf781b2eSEd Maste 			set_cu_context(re, pointer_size, off_size, version);
6250cf781b2eSEd Maste 			die = NULL;
6251cf781b2eSEd Maste 			if (dwarf_siblingof(re->dbg, die, &die, &de) !=
6252cf781b2eSEd Maste 			    DW_DLV_OK)
6253cf781b2eSEd Maste 				continue;
6254cf781b2eSEd Maste 			if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
6255cf781b2eSEd Maste 				warnx("dwarf_tag failed: %s",
6256cf781b2eSEd Maste 				    dwarf_errmsg(de));
6257cf781b2eSEd Maste 				continue;
6258cf781b2eSEd Maste 			}
6259cf781b2eSEd Maste 
6260cf781b2eSEd Maste 			lowpc = 0;
6261cf781b2eSEd Maste 			if (tag == DW_TAG_type_unit) {
6262cf781b2eSEd Maste 				if (dwarf_attrval_unsigned(die, DW_AT_low_pc,
6263cf781b2eSEd Maste 				    &lowpc, &de) != DW_DLV_OK)
6264cf781b2eSEd Maste 					lowpc = 0;
6265cf781b2eSEd Maste 			}
6266cf781b2eSEd Maste 
6267cf781b2eSEd Maste 			/*
6268cf781b2eSEd Maste 			 * Search attributes for reference to .debug_loc
6269cf781b2eSEd Maste 			 * section.
6270cf781b2eSEd Maste 			 */
6271cf781b2eSEd Maste 			search_loclist_at(re, die, lowpc);
6272cf781b2eSEd Maste 		}
6273cf781b2eSEd Maste 		if (ret == DW_DLV_ERROR)
6274cf781b2eSEd Maste 			warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
6275cf781b2eSEd Maste 	} while (dwarf_next_types_section(re->dbg, &de) == DW_DLV_OK);
6276cf781b2eSEd Maste 
6277cf781b2eSEd Maste 	if (TAILQ_EMPTY(&lalist))
6278cf781b2eSEd Maste 		return;
6279cf781b2eSEd Maste 
6280cf781b2eSEd Maste 	printf("    Offset   Begin    End      Expression\n");
6281cf781b2eSEd Maste 
6282cf781b2eSEd Maste 	TAILQ_FOREACH(la, &lalist, la_next) {
6283cf781b2eSEd Maste 		if (dwarf_loclist_n(la->la_at, &llbuf, &lcnt, &de) !=
6284cf781b2eSEd Maste 		    DW_DLV_OK) {
6285cf781b2eSEd Maste 			warnx("dwarf_loclist_n failed: %s", dwarf_errmsg(de));
6286cf781b2eSEd Maste 			continue;
6287cf781b2eSEd Maste 		}
6288cf781b2eSEd Maste 		set_cu_context(re, la->la_cu_psize, la->la_cu_osize,
6289cf781b2eSEd Maste 		    la->la_cu_ver);
6290cf781b2eSEd Maste 		for (i = 0; i < lcnt; i++) {
6291839529caSEd Maste 			printf("    %8.8jx ", (uintmax_t) la->la_off);
6292cf781b2eSEd Maste 			if (llbuf[i]->ld_lopc == 0 && llbuf[i]->ld_hipc == 0) {
6293cf781b2eSEd Maste 				printf("<End of list>\n");
6294cf781b2eSEd Maste 				continue;
6295cf781b2eSEd Maste 			}
6296cf781b2eSEd Maste 
6297cf781b2eSEd Maste 			/* TODO: handle base selection entry. */
6298cf781b2eSEd Maste 
6299cf781b2eSEd Maste 			printf("%8.8jx %8.8jx ",
6300cf781b2eSEd Maste 			    (uintmax_t) (la->la_lowpc + llbuf[i]->ld_lopc),
6301cf781b2eSEd Maste 			    (uintmax_t) (la->la_lowpc + llbuf[i]->ld_hipc));
6302cf781b2eSEd Maste 
6303cf781b2eSEd Maste 			putchar('(');
6304cf781b2eSEd Maste 			for (j = 0; (Dwarf_Half) j < llbuf[i]->ld_cents; j++) {
6305cf781b2eSEd Maste 				dump_dwarf_loc(re, &llbuf[i]->ld_s[j]);
63062c23cb7cSEd Maste 				if (j < llbuf[i]->ld_cents - 1)
6307cf781b2eSEd Maste 					printf("; ");
63082c23cb7cSEd Maste 			}
63092c23cb7cSEd Maste 			putchar(')');
63102c23cb7cSEd Maste 
63112c23cb7cSEd Maste 			if (llbuf[i]->ld_lopc == llbuf[i]->ld_hipc)
63122c23cb7cSEd Maste 				printf(" (start == end)");
63132c23cb7cSEd Maste 			putchar('\n');
63142c23cb7cSEd Maste 		}
6315cf781b2eSEd Maste 		for (i = 0; i < lcnt; i++) {
6316cf781b2eSEd Maste 			dwarf_dealloc(re->dbg, llbuf[i]->ld_s,
6317cf781b2eSEd Maste 			    DW_DLA_LOC_BLOCK);
6318cf781b2eSEd Maste 			dwarf_dealloc(re->dbg, llbuf[i], DW_DLA_LOCDESC);
6319cf781b2eSEd Maste 		}
6320cf781b2eSEd Maste 		dwarf_dealloc(re->dbg, llbuf, DW_DLA_LIST);
63212c23cb7cSEd Maste 	}
63222c23cb7cSEd Maste }
63232c23cb7cSEd Maste 
63242c23cb7cSEd Maste /*
63252c23cb7cSEd Maste  * Retrieve a string using string table section index and the string offset.
63262c23cb7cSEd Maste  */
63272c23cb7cSEd Maste static const char*
63282c23cb7cSEd Maste get_string(struct readelf *re, int strtab, size_t off)
63292c23cb7cSEd Maste {
63302c23cb7cSEd Maste 	const char *name;
63312c23cb7cSEd Maste 
63322c23cb7cSEd Maste 	if ((name = elf_strptr(re->elf, strtab, off)) == NULL)
63332c23cb7cSEd Maste 		return ("");
63342c23cb7cSEd Maste 
63352c23cb7cSEd Maste 	return (name);
63362c23cb7cSEd Maste }
63372c23cb7cSEd Maste 
63382c23cb7cSEd Maste /*
63392c23cb7cSEd Maste  * Retrieve the name of a symbol using the section index of the symbol
63402c23cb7cSEd Maste  * table and the index of the symbol within that table.
63412c23cb7cSEd Maste  */
63422c23cb7cSEd Maste static const char *
63432c23cb7cSEd Maste get_symbol_name(struct readelf *re, int symtab, int i)
63442c23cb7cSEd Maste {
63452c23cb7cSEd Maste 	struct section	*s;
63462c23cb7cSEd Maste 	const char	*name;
63472c23cb7cSEd Maste 	GElf_Sym	 sym;
63482c23cb7cSEd Maste 	Elf_Data	*data;
63492c23cb7cSEd Maste 	int		 elferr;
63502c23cb7cSEd Maste 
63512c23cb7cSEd Maste 	s = &re->sl[symtab];
63522c23cb7cSEd Maste 	if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM)
63532c23cb7cSEd Maste 		return ("");
63542c23cb7cSEd Maste 	(void) elf_errno();
63552c23cb7cSEd Maste 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
63562c23cb7cSEd Maste 		elferr = elf_errno();
63572c23cb7cSEd Maste 		if (elferr != 0)
63582c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
63592c23cb7cSEd Maste 		return ("");
63602c23cb7cSEd Maste 	}
63612c23cb7cSEd Maste 	if (gelf_getsym(data, i, &sym) != &sym)
63622c23cb7cSEd Maste 		return ("");
63632c23cb7cSEd Maste 	/* Return section name for STT_SECTION symbol. */
6364b6b6f9ccSEd Maste 	if (GELF_ST_TYPE(sym.st_info) == STT_SECTION) {
6365b6b6f9ccSEd Maste 		if (sym.st_shndx < re->shnum &&
63662c23cb7cSEd Maste 		    re->sl[sym.st_shndx].name != NULL)
63672c23cb7cSEd Maste 			return (re->sl[sym.st_shndx].name);
6368b6b6f9ccSEd Maste 		return ("");
6369b6b6f9ccSEd Maste 	}
6370656f49f8SEd Maste 	if (s->link >= re->shnum ||
6371656f49f8SEd Maste 	    (name = elf_strptr(re->elf, s->link, sym.st_name)) == NULL)
63722c23cb7cSEd Maste 		return ("");
63732c23cb7cSEd Maste 
63742c23cb7cSEd Maste 	return (name);
63752c23cb7cSEd Maste }
63762c23cb7cSEd Maste 
63772c23cb7cSEd Maste static uint64_t
63782c23cb7cSEd Maste get_symbol_value(struct readelf *re, int symtab, int i)
63792c23cb7cSEd Maste {
63802c23cb7cSEd Maste 	struct section	*s;
63812c23cb7cSEd Maste 	GElf_Sym	 sym;
63822c23cb7cSEd Maste 	Elf_Data	*data;
63832c23cb7cSEd Maste 	int		 elferr;
63842c23cb7cSEd Maste 
63852c23cb7cSEd Maste 	s = &re->sl[symtab];
63862c23cb7cSEd Maste 	if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM)
63872c23cb7cSEd Maste 		return (0);
63882c23cb7cSEd Maste 	(void) elf_errno();
63892c23cb7cSEd Maste 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
63902c23cb7cSEd Maste 		elferr = elf_errno();
63912c23cb7cSEd Maste 		if (elferr != 0)
63922c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
63932c23cb7cSEd Maste 		return (0);
63942c23cb7cSEd Maste 	}
63952c23cb7cSEd Maste 	if (gelf_getsym(data, i, &sym) != &sym)
63962c23cb7cSEd Maste 		return (0);
63972c23cb7cSEd Maste 
63982c23cb7cSEd Maste 	return (sym.st_value);
63992c23cb7cSEd Maste }
64002c23cb7cSEd Maste 
64012c23cb7cSEd Maste static void
64022c23cb7cSEd Maste hex_dump(struct readelf *re)
64032c23cb7cSEd Maste {
64042c23cb7cSEd Maste 	struct section *s;
64052c23cb7cSEd Maste 	Elf_Data *d;
64062c23cb7cSEd Maste 	uint8_t *buf;
64072c23cb7cSEd Maste 	size_t sz, nbytes;
64082c23cb7cSEd Maste 	uint64_t addr;
64092c23cb7cSEd Maste 	int elferr, i, j;
64102c23cb7cSEd Maste 
64112c23cb7cSEd Maste 	for (i = 1; (size_t) i < re->shnum; i++) {
64122c23cb7cSEd Maste 		s = &re->sl[i];
64132c23cb7cSEd Maste 		if (find_dumpop(re, (size_t) i, s->name, HEX_DUMP, -1) == NULL)
64142c23cb7cSEd Maste 			continue;
64152c23cb7cSEd Maste 		(void) elf_errno();
6416839529caSEd Maste 		if ((d = elf_getdata(s->scn, NULL)) == NULL &&
6417839529caSEd Maste 		    (d = elf_rawdata(s->scn, NULL)) == NULL) {
64182c23cb7cSEd Maste 			elferr = elf_errno();
64192c23cb7cSEd Maste 			if (elferr != 0)
64202c23cb7cSEd Maste 				warnx("elf_getdata failed: %s",
64212c23cb7cSEd Maste 				    elf_errmsg(elferr));
64222c23cb7cSEd Maste 			continue;
64232c23cb7cSEd Maste 		}
6424839529caSEd Maste 		(void) elf_errno();
64252c23cb7cSEd Maste 		if (d->d_size <= 0 || d->d_buf == NULL) {
64262c23cb7cSEd Maste 			printf("\nSection '%s' has no data to dump.\n",
64272c23cb7cSEd Maste 			    s->name);
64282c23cb7cSEd Maste 			continue;
64292c23cb7cSEd Maste 		}
64302c23cb7cSEd Maste 		buf = d->d_buf;
64312c23cb7cSEd Maste 		sz = d->d_size;
64322c23cb7cSEd Maste 		addr = s->addr;
64332c23cb7cSEd Maste 		printf("\nHex dump of section '%s':\n", s->name);
64342c23cb7cSEd Maste 		while (sz > 0) {
64352c23cb7cSEd Maste 			printf("  0x%8.8jx ", (uintmax_t)addr);
64362c23cb7cSEd Maste 			nbytes = sz > 16? 16 : sz;
64372c23cb7cSEd Maste 			for (j = 0; j < 16; j++) {
64382c23cb7cSEd Maste 				if ((size_t)j < nbytes)
64392c23cb7cSEd Maste 					printf("%2.2x", buf[j]);
64402c23cb7cSEd Maste 				else
64412c23cb7cSEd Maste 					printf("  ");
64422c23cb7cSEd Maste 				if ((j & 3) == 3)
64432c23cb7cSEd Maste 					printf(" ");
64442c23cb7cSEd Maste 			}
64452c23cb7cSEd Maste 			for (j = 0; (size_t)j < nbytes; j++) {
64462c23cb7cSEd Maste 				if (isprint(buf[j]))
64472c23cb7cSEd Maste 					printf("%c", buf[j]);
64482c23cb7cSEd Maste 				else
64492c23cb7cSEd Maste 					printf(".");
64502c23cb7cSEd Maste 			}
64512c23cb7cSEd Maste 			printf("\n");
64522c23cb7cSEd Maste 			buf += nbytes;
64532c23cb7cSEd Maste 			addr += nbytes;
64542c23cb7cSEd Maste 			sz -= nbytes;
64552c23cb7cSEd Maste 		}
64562c23cb7cSEd Maste 	}
64572c23cb7cSEd Maste }
64582c23cb7cSEd Maste 
64592c23cb7cSEd Maste static void
64602c23cb7cSEd Maste str_dump(struct readelf *re)
64612c23cb7cSEd Maste {
64622c23cb7cSEd Maste 	struct section *s;
64632c23cb7cSEd Maste 	Elf_Data *d;
64642c23cb7cSEd Maste 	unsigned char *start, *end, *buf_end;
64652c23cb7cSEd Maste 	unsigned int len;
64662c23cb7cSEd Maste 	int i, j, elferr, found;
64672c23cb7cSEd Maste 
64682c23cb7cSEd Maste 	for (i = 1; (size_t) i < re->shnum; i++) {
64692c23cb7cSEd Maste 		s = &re->sl[i];
64702c23cb7cSEd Maste 		if (find_dumpop(re, (size_t) i, s->name, STR_DUMP, -1) == NULL)
64712c23cb7cSEd Maste 			continue;
64722c23cb7cSEd Maste 		(void) elf_errno();
6473839529caSEd Maste 		if ((d = elf_getdata(s->scn, NULL)) == NULL &&
6474839529caSEd Maste 		    (d = elf_rawdata(s->scn, NULL)) == NULL) {
64752c23cb7cSEd Maste 			elferr = elf_errno();
64762c23cb7cSEd Maste 			if (elferr != 0)
64772c23cb7cSEd Maste 				warnx("elf_getdata failed: %s",
64782c23cb7cSEd Maste 				    elf_errmsg(elferr));
64792c23cb7cSEd Maste 			continue;
64802c23cb7cSEd Maste 		}
6481839529caSEd Maste 		(void) elf_errno();
64822c23cb7cSEd Maste 		if (d->d_size <= 0 || d->d_buf == NULL) {
64832c23cb7cSEd Maste 			printf("\nSection '%s' has no data to dump.\n",
64842c23cb7cSEd Maste 			    s->name);
64852c23cb7cSEd Maste 			continue;
64862c23cb7cSEd Maste 		}
64872c23cb7cSEd Maste 		buf_end = (unsigned char *) d->d_buf + d->d_size;
64882c23cb7cSEd Maste 		start = (unsigned char *) d->d_buf;
64892c23cb7cSEd Maste 		found = 0;
64902c23cb7cSEd Maste 		printf("\nString dump of section '%s':\n", s->name);
64912c23cb7cSEd Maste 		for (;;) {
64922c23cb7cSEd Maste 			while (start < buf_end && !isprint(*start))
64932c23cb7cSEd Maste 				start++;
64942c23cb7cSEd Maste 			if (start >= buf_end)
64952c23cb7cSEd Maste 				break;
64962c23cb7cSEd Maste 			end = start + 1;
64972c23cb7cSEd Maste 			while (end < buf_end && isprint(*end))
64982c23cb7cSEd Maste 				end++;
64992c23cb7cSEd Maste 			printf("  [%6lx]  ",
65002c23cb7cSEd Maste 			    (long) (start - (unsigned char *) d->d_buf));
65012c23cb7cSEd Maste 			len = end - start;
65022c23cb7cSEd Maste 			for (j = 0; (unsigned int) j < len; j++)
65032c23cb7cSEd Maste 				putchar(start[j]);
65042c23cb7cSEd Maste 			putchar('\n');
65052c23cb7cSEd Maste 			found = 1;
65062c23cb7cSEd Maste 			if (end >= buf_end)
65072c23cb7cSEd Maste 				break;
65082c23cb7cSEd Maste 			start = end + 1;
65092c23cb7cSEd Maste 		}
65102c23cb7cSEd Maste 		if (!found)
65112c23cb7cSEd Maste 			printf("  No strings found in this section.");
65122c23cb7cSEd Maste 		putchar('\n');
65132c23cb7cSEd Maste 	}
65142c23cb7cSEd Maste }
65152c23cb7cSEd Maste 
65162c23cb7cSEd Maste static void
65172c23cb7cSEd Maste load_sections(struct readelf *re)
65182c23cb7cSEd Maste {
65192c23cb7cSEd Maste 	struct section	*s;
65202c23cb7cSEd Maste 	const char	*name;
65212c23cb7cSEd Maste 	Elf_Scn		*scn;
65222c23cb7cSEd Maste 	GElf_Shdr	 sh;
65232c23cb7cSEd Maste 	size_t		 shstrndx, ndx;
65242c23cb7cSEd Maste 	int		 elferr;
65252c23cb7cSEd Maste 
65262c23cb7cSEd Maste 	/* Allocate storage for internal section list. */
65272c23cb7cSEd Maste 	if (!elf_getshnum(re->elf, &re->shnum)) {
65282c23cb7cSEd Maste 		warnx("elf_getshnum failed: %s", elf_errmsg(-1));
65292c23cb7cSEd Maste 		return;
65302c23cb7cSEd Maste 	}
65312c23cb7cSEd Maste 	if (re->sl != NULL)
65322c23cb7cSEd Maste 		free(re->sl);
65332c23cb7cSEd Maste 	if ((re->sl = calloc(re->shnum, sizeof(*re->sl))) == NULL)
65342c23cb7cSEd Maste 		err(EXIT_FAILURE, "calloc failed");
65352c23cb7cSEd Maste 
65362c23cb7cSEd Maste 	/* Get the index of .shstrtab section. */
65372c23cb7cSEd Maste 	if (!elf_getshstrndx(re->elf, &shstrndx)) {
65382c23cb7cSEd Maste 		warnx("elf_getshstrndx failed: %s", elf_errmsg(-1));
65392c23cb7cSEd Maste 		return;
65402c23cb7cSEd Maste 	}
65412c23cb7cSEd Maste 
654271a0c925SEd Maste 	if ((scn = elf_getscn(re->elf, 0)) == NULL)
65432c23cb7cSEd Maste 		return;
65442c23cb7cSEd Maste 
65452c23cb7cSEd Maste 	(void) elf_errno();
65462c23cb7cSEd Maste 	do {
65472c23cb7cSEd Maste 		if (gelf_getshdr(scn, &sh) == NULL) {
65482c23cb7cSEd Maste 			warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
65492c23cb7cSEd Maste 			(void) elf_errno();
65502c23cb7cSEd Maste 			continue;
65512c23cb7cSEd Maste 		}
65522c23cb7cSEd Maste 		if ((name = elf_strptr(re->elf, shstrndx, sh.sh_name)) == NULL) {
65532c23cb7cSEd Maste 			(void) elf_errno();
65542c23cb7cSEd Maste 			name = "ERROR";
65552c23cb7cSEd Maste 		}
65562c23cb7cSEd Maste 		if ((ndx = elf_ndxscn(scn)) == SHN_UNDEF) {
65572c23cb7cSEd Maste 			if ((elferr = elf_errno()) != 0)
65582c23cb7cSEd Maste 				warnx("elf_ndxscn failed: %s",
65592c23cb7cSEd Maste 				    elf_errmsg(elferr));
65602c23cb7cSEd Maste 			continue;
65612c23cb7cSEd Maste 		}
65622c23cb7cSEd Maste 		if (ndx >= re->shnum) {
65632c23cb7cSEd Maste 			warnx("section index of '%s' out of range", name);
65642c23cb7cSEd Maste 			continue;
65652c23cb7cSEd Maste 		}
6566656f49f8SEd Maste 		if (sh.sh_link >= re->shnum)
6567656f49f8SEd Maste 			warnx("section link %llu of '%s' out of range",
6568656f49f8SEd Maste 			    (unsigned long long)sh.sh_link, name);
65692c23cb7cSEd Maste 		s = &re->sl[ndx];
65702c23cb7cSEd Maste 		s->name = name;
65712c23cb7cSEd Maste 		s->scn = scn;
65722c23cb7cSEd Maste 		s->off = sh.sh_offset;
65732c23cb7cSEd Maste 		s->sz = sh.sh_size;
65742c23cb7cSEd Maste 		s->entsize = sh.sh_entsize;
65752c23cb7cSEd Maste 		s->align = sh.sh_addralign;
65762c23cb7cSEd Maste 		s->type = sh.sh_type;
65772c23cb7cSEd Maste 		s->flags = sh.sh_flags;
65782c23cb7cSEd Maste 		s->addr = sh.sh_addr;
65792c23cb7cSEd Maste 		s->link = sh.sh_link;
65802c23cb7cSEd Maste 		s->info = sh.sh_info;
65812c23cb7cSEd Maste 	} while ((scn = elf_nextscn(re->elf, scn)) != NULL);
65822c23cb7cSEd Maste 	elferr = elf_errno();
65832c23cb7cSEd Maste 	if (elferr != 0)
65842c23cb7cSEd Maste 		warnx("elf_nextscn failed: %s", elf_errmsg(elferr));
65852c23cb7cSEd Maste }
65862c23cb7cSEd Maste 
65872c23cb7cSEd Maste static void
65882c23cb7cSEd Maste unload_sections(struct readelf *re)
65892c23cb7cSEd Maste {
65902c23cb7cSEd Maste 
65912c23cb7cSEd Maste 	if (re->sl != NULL) {
65922c23cb7cSEd Maste 		free(re->sl);
65932c23cb7cSEd Maste 		re->sl = NULL;
65942c23cb7cSEd Maste 	}
65952c23cb7cSEd Maste 	re->shnum = 0;
65962c23cb7cSEd Maste 	re->vd_s = NULL;
65972c23cb7cSEd Maste 	re->vn_s = NULL;
65982c23cb7cSEd Maste 	re->vs_s = NULL;
65992c23cb7cSEd Maste 	re->vs = NULL;
66002c23cb7cSEd Maste 	re->vs_sz = 0;
66012c23cb7cSEd Maste 	if (re->ver != NULL) {
66022c23cb7cSEd Maste 		free(re->ver);
66032c23cb7cSEd Maste 		re->ver = NULL;
66042c23cb7cSEd Maste 		re->ver_sz = 0;
66052c23cb7cSEd Maste 	}
66062c23cb7cSEd Maste }
66072c23cb7cSEd Maste 
66082c23cb7cSEd Maste static void
66092c23cb7cSEd Maste dump_elf(struct readelf *re)
66102c23cb7cSEd Maste {
66112c23cb7cSEd Maste 
66122c23cb7cSEd Maste 	/* Fetch ELF header. No need to continue if it fails. */
66132c23cb7cSEd Maste 	if (gelf_getehdr(re->elf, &re->ehdr) == NULL) {
66142c23cb7cSEd Maste 		warnx("gelf_getehdr failed: %s", elf_errmsg(-1));
66152c23cb7cSEd Maste 		return;
66162c23cb7cSEd Maste 	}
66172c23cb7cSEd Maste 	if ((re->ec = gelf_getclass(re->elf)) == ELFCLASSNONE) {
66182c23cb7cSEd Maste 		warnx("gelf_getclass failed: %s", elf_errmsg(-1));
66192c23cb7cSEd Maste 		return;
66202c23cb7cSEd Maste 	}
66212c23cb7cSEd Maste 	if (re->ehdr.e_ident[EI_DATA] == ELFDATA2MSB) {
66222c23cb7cSEd Maste 		re->dw_read = _read_msb;
66232c23cb7cSEd Maste 		re->dw_decode = _decode_msb;
66242c23cb7cSEd Maste 	} else {
66252c23cb7cSEd Maste 		re->dw_read = _read_lsb;
66262c23cb7cSEd Maste 		re->dw_decode = _decode_lsb;
66272c23cb7cSEd Maste 	}
66282c23cb7cSEd Maste 
66292c23cb7cSEd Maste 	if (re->options & ~RE_H)
66302c23cb7cSEd Maste 		load_sections(re);
66312c23cb7cSEd Maste 	if ((re->options & RE_VV) || (re->options & RE_S))
66322c23cb7cSEd Maste 		search_ver(re);
66332c23cb7cSEd Maste 	if (re->options & RE_H)
66342c23cb7cSEd Maste 		dump_ehdr(re);
66352c23cb7cSEd Maste 	if (re->options & RE_L)
66362c23cb7cSEd Maste 		dump_phdr(re);
66372c23cb7cSEd Maste 	if (re->options & RE_SS)
66382c23cb7cSEd Maste 		dump_shdr(re);
66393ef90571SEd Maste 	if (re->options & RE_G)
66403ef90571SEd Maste 		dump_section_groups(re);
66412c23cb7cSEd Maste 	if (re->options & RE_D)
66422c23cb7cSEd Maste 		dump_dynamic(re);
66432c23cb7cSEd Maste 	if (re->options & RE_R)
66442c23cb7cSEd Maste 		dump_reloc(re);
66452c23cb7cSEd Maste 	if (re->options & RE_S)
66462c23cb7cSEd Maste 		dump_symtabs(re);
66472c23cb7cSEd Maste 	if (re->options & RE_N)
66482c23cb7cSEd Maste 		dump_notes(re);
66492c23cb7cSEd Maste 	if (re->options & RE_II)
66502c23cb7cSEd Maste 		dump_hash(re);
66512c23cb7cSEd Maste 	if (re->options & RE_X)
66522c23cb7cSEd Maste 		hex_dump(re);
66532c23cb7cSEd Maste 	if (re->options & RE_P)
66542c23cb7cSEd Maste 		str_dump(re);
66552c23cb7cSEd Maste 	if (re->options & RE_VV)
66562c23cb7cSEd Maste 		dump_ver(re);
66572c23cb7cSEd Maste 	if (re->options & RE_AA)
66582c23cb7cSEd Maste 		dump_arch_specific_info(re);
66592c23cb7cSEd Maste 	if (re->options & RE_W)
66602c23cb7cSEd Maste 		dump_dwarf(re);
66612c23cb7cSEd Maste 	if (re->options & ~RE_H)
66622c23cb7cSEd Maste 		unload_sections(re);
66632c23cb7cSEd Maste }
66642c23cb7cSEd Maste 
66652c23cb7cSEd Maste static void
66662c23cb7cSEd Maste dump_dwarf(struct readelf *re)
66672c23cb7cSEd Maste {
66682c23cb7cSEd Maste 	int error;
66692c23cb7cSEd Maste 	Dwarf_Error de;
66702c23cb7cSEd Maste 
66712c23cb7cSEd Maste 	if (dwarf_elf_init(re->elf, DW_DLC_READ, NULL, NULL, &re->dbg, &de)) {
66722c23cb7cSEd Maste 		if ((error = dwarf_errno(de)) != DW_DLE_DEBUG_INFO_NULL)
66732c23cb7cSEd Maste 			errx(EXIT_FAILURE, "dwarf_elf_init failed: %s",
66742c23cb7cSEd Maste 			    dwarf_errmsg(de));
66752c23cb7cSEd Maste 		return;
66762c23cb7cSEd Maste 	}
66772c23cb7cSEd Maste 
66782c23cb7cSEd Maste 	if (re->dop & DW_A)
66792c23cb7cSEd Maste 		dump_dwarf_abbrev(re);
66802c23cb7cSEd Maste 	if (re->dop & DW_L)
66812c23cb7cSEd Maste 		dump_dwarf_line(re);
66822c23cb7cSEd Maste 	if (re->dop & DW_LL)
66832c23cb7cSEd Maste 		dump_dwarf_line_decoded(re);
6684cf781b2eSEd Maste 	if (re->dop & DW_I) {
6685cf781b2eSEd Maste 		dump_dwarf_info(re, 0);
6686cf781b2eSEd Maste 		dump_dwarf_info(re, 1);
6687cf781b2eSEd Maste 	}
66882c23cb7cSEd Maste 	if (re->dop & DW_P)
66892c23cb7cSEd Maste 		dump_dwarf_pubnames(re);
66902c23cb7cSEd Maste 	if (re->dop & DW_R)
66912c23cb7cSEd Maste 		dump_dwarf_aranges(re);
66922c23cb7cSEd Maste 	if (re->dop & DW_RR)
66932c23cb7cSEd Maste 		dump_dwarf_ranges(re);
66942c23cb7cSEd Maste 	if (re->dop & DW_M)
66952c23cb7cSEd Maste 		dump_dwarf_macinfo(re);
66962c23cb7cSEd Maste 	if (re->dop & DW_F)
66972c23cb7cSEd Maste 		dump_dwarf_frame(re, 0);
66982c23cb7cSEd Maste 	else if (re->dop & DW_FF)
66992c23cb7cSEd Maste 		dump_dwarf_frame(re, 1);
67002c23cb7cSEd Maste 	if (re->dop & DW_S)
67012c23cb7cSEd Maste 		dump_dwarf_str(re);
67022c23cb7cSEd Maste 	if (re->dop & DW_O)
67032c23cb7cSEd Maste 		dump_dwarf_loclist(re);
67042c23cb7cSEd Maste 
67052c23cb7cSEd Maste 	dwarf_finish(re->dbg, &de);
67062c23cb7cSEd Maste }
67072c23cb7cSEd Maste 
67082c23cb7cSEd Maste static void
67092c23cb7cSEd Maste dump_ar(struct readelf *re, int fd)
67102c23cb7cSEd Maste {
67112c23cb7cSEd Maste 	Elf_Arsym *arsym;
67122c23cb7cSEd Maste 	Elf_Arhdr *arhdr;
67132c23cb7cSEd Maste 	Elf_Cmd cmd;
67142c23cb7cSEd Maste 	Elf *e;
67152c23cb7cSEd Maste 	size_t sz;
67162c23cb7cSEd Maste 	off_t off;
67172c23cb7cSEd Maste 	int i;
67182c23cb7cSEd Maste 
67192c23cb7cSEd Maste 	re->ar = re->elf;
67202c23cb7cSEd Maste 
67212c23cb7cSEd Maste 	if (re->options & RE_C) {
67222c23cb7cSEd Maste 		if ((arsym = elf_getarsym(re->ar, &sz)) == NULL) {
67232c23cb7cSEd Maste 			warnx("elf_getarsym() failed: %s", elf_errmsg(-1));
67242c23cb7cSEd Maste 			goto process_members;
67252c23cb7cSEd Maste 		}
67262c23cb7cSEd Maste 		printf("Index of archive %s: (%ju entries)\n", re->filename,
67272c23cb7cSEd Maste 		    (uintmax_t) sz - 1);
67282c23cb7cSEd Maste 		off = 0;
67292c23cb7cSEd Maste 		for (i = 0; (size_t) i < sz; i++) {
67302c23cb7cSEd Maste 			if (arsym[i].as_name == NULL)
67312c23cb7cSEd Maste 				break;
67322c23cb7cSEd Maste 			if (arsym[i].as_off != off) {
67332c23cb7cSEd Maste 				off = arsym[i].as_off;
67342c23cb7cSEd Maste 				if (elf_rand(re->ar, off) != off) {
67352c23cb7cSEd Maste 					warnx("elf_rand() failed: %s",
67362c23cb7cSEd Maste 					    elf_errmsg(-1));
67372c23cb7cSEd Maste 					continue;
67382c23cb7cSEd Maste 				}
67392c23cb7cSEd Maste 				if ((e = elf_begin(fd, ELF_C_READ, re->ar)) ==
67402c23cb7cSEd Maste 				    NULL) {
67412c23cb7cSEd Maste 					warnx("elf_begin() failed: %s",
67422c23cb7cSEd Maste 					    elf_errmsg(-1));
67432c23cb7cSEd Maste 					continue;
67442c23cb7cSEd Maste 				}
67452c23cb7cSEd Maste 				if ((arhdr = elf_getarhdr(e)) == NULL) {
67462c23cb7cSEd Maste 					warnx("elf_getarhdr() failed: %s",
67472c23cb7cSEd Maste 					    elf_errmsg(-1));
67482c23cb7cSEd Maste 					elf_end(e);
67492c23cb7cSEd Maste 					continue;
67502c23cb7cSEd Maste 				}
67512c23cb7cSEd Maste 				printf("Binary %s(%s) contains:\n",
67522c23cb7cSEd Maste 				    re->filename, arhdr->ar_name);
67532c23cb7cSEd Maste 			}
67542c23cb7cSEd Maste 			printf("\t%s\n", arsym[i].as_name);
67552c23cb7cSEd Maste 		}
67562c23cb7cSEd Maste 		if (elf_rand(re->ar, SARMAG) != SARMAG) {
67572c23cb7cSEd Maste 			warnx("elf_rand() failed: %s", elf_errmsg(-1));
67582c23cb7cSEd Maste 			return;
67592c23cb7cSEd Maste 		}
67602c23cb7cSEd Maste 	}
67612c23cb7cSEd Maste 
67622c23cb7cSEd Maste process_members:
67632c23cb7cSEd Maste 
67642c23cb7cSEd Maste 	if ((re->options & ~RE_C) == 0)
67652c23cb7cSEd Maste 		return;
67662c23cb7cSEd Maste 
67672c23cb7cSEd Maste 	cmd = ELF_C_READ;
67682c23cb7cSEd Maste 	while ((re->elf = elf_begin(fd, cmd, re->ar)) != NULL) {
67692c23cb7cSEd Maste 		if ((arhdr = elf_getarhdr(re->elf)) == NULL) {
67702c23cb7cSEd Maste 			warnx("elf_getarhdr() failed: %s", elf_errmsg(-1));
67712c23cb7cSEd Maste 			goto next_member;
67722c23cb7cSEd Maste 		}
67732c23cb7cSEd Maste 		if (strcmp(arhdr->ar_name, "/") == 0 ||
67742c23cb7cSEd Maste 		    strcmp(arhdr->ar_name, "//") == 0 ||
67752c23cb7cSEd Maste 		    strcmp(arhdr->ar_name, "__.SYMDEF") == 0)
67762c23cb7cSEd Maste 			goto next_member;
67772c23cb7cSEd Maste 		printf("\nFile: %s(%s)\n", re->filename, arhdr->ar_name);
67782c23cb7cSEd Maste 		dump_elf(re);
67792c23cb7cSEd Maste 
67802c23cb7cSEd Maste 	next_member:
67812c23cb7cSEd Maste 		cmd = elf_next(re->elf);
67822c23cb7cSEd Maste 		elf_end(re->elf);
67832c23cb7cSEd Maste 	}
67842c23cb7cSEd Maste 	re->elf = re->ar;
67852c23cb7cSEd Maste }
67862c23cb7cSEd Maste 
67872c23cb7cSEd Maste static void
67882c23cb7cSEd Maste dump_object(struct readelf *re)
67892c23cb7cSEd Maste {
67902c23cb7cSEd Maste 	int fd;
67912c23cb7cSEd Maste 
67922c23cb7cSEd Maste 	if ((fd = open(re->filename, O_RDONLY)) == -1) {
67932c23cb7cSEd Maste 		warn("open %s failed", re->filename);
67942c23cb7cSEd Maste 		return;
67952c23cb7cSEd Maste 	}
67962c23cb7cSEd Maste 
67972c23cb7cSEd Maste 	if ((re->flags & DISPLAY_FILENAME) != 0)
67982c23cb7cSEd Maste 		printf("\nFile: %s\n", re->filename);
67992c23cb7cSEd Maste 
68002c23cb7cSEd Maste 	if ((re->elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
68012c23cb7cSEd Maste 		warnx("elf_begin() failed: %s", elf_errmsg(-1));
68022c23cb7cSEd Maste 		return;
68032c23cb7cSEd Maste 	}
68042c23cb7cSEd Maste 
68052c23cb7cSEd Maste 	switch (elf_kind(re->elf)) {
68062c23cb7cSEd Maste 	case ELF_K_NONE:
68072c23cb7cSEd Maste 		warnx("Not an ELF file.");
68082c23cb7cSEd Maste 		return;
68092c23cb7cSEd Maste 	case ELF_K_ELF:
68102c23cb7cSEd Maste 		dump_elf(re);
68112c23cb7cSEd Maste 		break;
68122c23cb7cSEd Maste 	case ELF_K_AR:
68132c23cb7cSEd Maste 		dump_ar(re, fd);
68142c23cb7cSEd Maste 		break;
68152c23cb7cSEd Maste 	default:
68162c23cb7cSEd Maste 		warnx("Internal: libelf returned unknown elf kind.");
68172c23cb7cSEd Maste 		return;
68182c23cb7cSEd Maste 	}
68192c23cb7cSEd Maste 
68202c23cb7cSEd Maste 	elf_end(re->elf);
68212c23cb7cSEd Maste }
68222c23cb7cSEd Maste 
68232c23cb7cSEd Maste static void
68242c23cb7cSEd Maste add_dumpop(struct readelf *re, size_t si, const char *sn, int op, int t)
68252c23cb7cSEd Maste {
68262c23cb7cSEd Maste 	struct dumpop *d;
68272c23cb7cSEd Maste 
68282c23cb7cSEd Maste 	if ((d = find_dumpop(re, si, sn, -1, t)) == NULL) {
68292c23cb7cSEd Maste 		if ((d = calloc(1, sizeof(*d))) == NULL)
68302c23cb7cSEd Maste 			err(EXIT_FAILURE, "calloc failed");
68312c23cb7cSEd Maste 		if (t == DUMP_BY_INDEX)
68322c23cb7cSEd Maste 			d->u.si = si;
68332c23cb7cSEd Maste 		else
68342c23cb7cSEd Maste 			d->u.sn = sn;
68352c23cb7cSEd Maste 		d->type = t;
68362c23cb7cSEd Maste 		d->op = op;
68372c23cb7cSEd Maste 		STAILQ_INSERT_TAIL(&re->v_dumpop, d, dumpop_list);
68382c23cb7cSEd Maste 	} else
68392c23cb7cSEd Maste 		d->op |= op;
68402c23cb7cSEd Maste }
68412c23cb7cSEd Maste 
68422c23cb7cSEd Maste static struct dumpop *
68432c23cb7cSEd Maste find_dumpop(struct readelf *re, size_t si, const char *sn, int op, int t)
68442c23cb7cSEd Maste {
68452c23cb7cSEd Maste 	struct dumpop *d;
68462c23cb7cSEd Maste 
68472c23cb7cSEd Maste 	STAILQ_FOREACH(d, &re->v_dumpop, dumpop_list) {
68482c23cb7cSEd Maste 		if ((op == -1 || op & d->op) &&
68492c23cb7cSEd Maste 		    (t == -1 || (unsigned) t == d->type)) {
68502c23cb7cSEd Maste 			if ((d->type == DUMP_BY_INDEX && d->u.si == si) ||
68512c23cb7cSEd Maste 			    (d->type == DUMP_BY_NAME && !strcmp(d->u.sn, sn)))
68522c23cb7cSEd Maste 				return (d);
68532c23cb7cSEd Maste 		}
68542c23cb7cSEd Maste 	}
68552c23cb7cSEd Maste 
68562c23cb7cSEd Maste 	return (NULL);
68572c23cb7cSEd Maste }
68582c23cb7cSEd Maste 
68592c23cb7cSEd Maste static struct {
68602c23cb7cSEd Maste 	const char *ln;
68612c23cb7cSEd Maste 	char sn;
68622c23cb7cSEd Maste 	int value;
68632c23cb7cSEd Maste } dwarf_op[] = {
68642c23cb7cSEd Maste 	{"rawline", 'l', DW_L},
68652c23cb7cSEd Maste 	{"decodedline", 'L', DW_LL},
68662c23cb7cSEd Maste 	{"info", 'i', DW_I},
68672c23cb7cSEd Maste 	{"abbrev", 'a', DW_A},
68682c23cb7cSEd Maste 	{"pubnames", 'p', DW_P},
68692c23cb7cSEd Maste 	{"aranges", 'r', DW_R},
68702c23cb7cSEd Maste 	{"ranges", 'r', DW_R},
68712c23cb7cSEd Maste 	{"Ranges", 'R', DW_RR},
68722c23cb7cSEd Maste 	{"macro", 'm', DW_M},
68732c23cb7cSEd Maste 	{"frames", 'f', DW_F},
6874cf781b2eSEd Maste 	{"frames-interp", 'F', DW_FF},
68752c23cb7cSEd Maste 	{"str", 's', DW_S},
68762c23cb7cSEd Maste 	{"loc", 'o', DW_O},
68772c23cb7cSEd Maste 	{NULL, 0, 0}
68782c23cb7cSEd Maste };
68792c23cb7cSEd Maste 
68802c23cb7cSEd Maste static void
68812c23cb7cSEd Maste parse_dwarf_op_short(struct readelf *re, const char *op)
68822c23cb7cSEd Maste {
68832c23cb7cSEd Maste 	int i;
68842c23cb7cSEd Maste 
68852c23cb7cSEd Maste 	if (op == NULL) {
68862c23cb7cSEd Maste 		re->dop |= DW_DEFAULT_OPTIONS;
68872c23cb7cSEd Maste 		return;
68882c23cb7cSEd Maste 	}
68892c23cb7cSEd Maste 
68902c23cb7cSEd Maste 	for (; *op != '\0'; op++) {
68912c23cb7cSEd Maste 		for (i = 0; dwarf_op[i].ln != NULL; i++) {
68922c23cb7cSEd Maste 			if (dwarf_op[i].sn == *op) {
68932c23cb7cSEd Maste 				re->dop |= dwarf_op[i].value;
68942c23cb7cSEd Maste 				break;
68952c23cb7cSEd Maste 			}
68962c23cb7cSEd Maste 		}
68972c23cb7cSEd Maste 	}
68982c23cb7cSEd Maste }
68992c23cb7cSEd Maste 
69002c23cb7cSEd Maste static void
69012c23cb7cSEd Maste parse_dwarf_op_long(struct readelf *re, const char *op)
69022c23cb7cSEd Maste {
69032c23cb7cSEd Maste 	char *p, *token, *bp;
69042c23cb7cSEd Maste 	int i;
69052c23cb7cSEd Maste 
69062c23cb7cSEd Maste 	if (op == NULL) {
69072c23cb7cSEd Maste 		re->dop |= DW_DEFAULT_OPTIONS;
69082c23cb7cSEd Maste 		return;
69092c23cb7cSEd Maste 	}
69102c23cb7cSEd Maste 
69112c23cb7cSEd Maste 	if ((p = strdup(op)) == NULL)
69122c23cb7cSEd Maste 		err(EXIT_FAILURE, "strdup failed");
69132c23cb7cSEd Maste 	bp = p;
69142c23cb7cSEd Maste 
69152c23cb7cSEd Maste 	while ((token = strsep(&p, ",")) != NULL) {
69162c23cb7cSEd Maste 		for (i = 0; dwarf_op[i].ln != NULL; i++) {
69172c23cb7cSEd Maste 			if (!strcmp(token, dwarf_op[i].ln)) {
69182c23cb7cSEd Maste 				re->dop |= dwarf_op[i].value;
69192c23cb7cSEd Maste 				break;
69202c23cb7cSEd Maste 			}
69212c23cb7cSEd Maste 		}
69222c23cb7cSEd Maste 	}
69232c23cb7cSEd Maste 
69242c23cb7cSEd Maste 	free(bp);
69252c23cb7cSEd Maste }
69262c23cb7cSEd Maste 
69272c23cb7cSEd Maste static uint64_t
69282c23cb7cSEd Maste _read_lsb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read)
69292c23cb7cSEd Maste {
69302c23cb7cSEd Maste 	uint64_t ret;
69312c23cb7cSEd Maste 	uint8_t *src;
69322c23cb7cSEd Maste 
69332c23cb7cSEd Maste 	src = (uint8_t *) d->d_buf + *offsetp;
69342c23cb7cSEd Maste 
69352c23cb7cSEd Maste 	ret = 0;
69362c23cb7cSEd Maste 	switch (bytes_to_read) {
69372c23cb7cSEd Maste 	case 8:
69382c23cb7cSEd Maste 		ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
69392c23cb7cSEd Maste 		ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
6940839529caSEd Maste 		/* FALLTHROUGH */
69412c23cb7cSEd Maste 	case 4:
69422c23cb7cSEd Maste 		ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
6943839529caSEd Maste 		/* FALLTHROUGH */
69442c23cb7cSEd Maste 	case 2:
69452c23cb7cSEd Maste 		ret |= ((uint64_t) src[1]) << 8;
6946839529caSEd Maste 		/* FALLTHROUGH */
69472c23cb7cSEd Maste 	case 1:
69482c23cb7cSEd Maste 		ret |= src[0];
69492c23cb7cSEd Maste 		break;
69502c23cb7cSEd Maste 	default:
69512c23cb7cSEd Maste 		return (0);
69522c23cb7cSEd Maste 	}
69532c23cb7cSEd Maste 
69542c23cb7cSEd Maste 	*offsetp += bytes_to_read;
69552c23cb7cSEd Maste 
69562c23cb7cSEd Maste 	return (ret);
69572c23cb7cSEd Maste }
69582c23cb7cSEd Maste 
69592c23cb7cSEd Maste static uint64_t
69602c23cb7cSEd Maste _read_msb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read)
69612c23cb7cSEd Maste {
69622c23cb7cSEd Maste 	uint64_t ret;
69632c23cb7cSEd Maste 	uint8_t *src;
69642c23cb7cSEd Maste 
69652c23cb7cSEd Maste 	src = (uint8_t *) d->d_buf + *offsetp;
69662c23cb7cSEd Maste 
69672c23cb7cSEd Maste 	switch (bytes_to_read) {
69682c23cb7cSEd Maste 	case 1:
69692c23cb7cSEd Maste 		ret = src[0];
69702c23cb7cSEd Maste 		break;
69712c23cb7cSEd Maste 	case 2:
69722c23cb7cSEd Maste 		ret = src[1] | ((uint64_t) src[0]) << 8;
69732c23cb7cSEd Maste 		break;
69742c23cb7cSEd Maste 	case 4:
69752c23cb7cSEd Maste 		ret = src[3] | ((uint64_t) src[2]) << 8;
69762c23cb7cSEd Maste 		ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24;
69772c23cb7cSEd Maste 		break;
69782c23cb7cSEd Maste 	case 8:
69792c23cb7cSEd Maste 		ret = src[7] | ((uint64_t) src[6]) << 8;
69802c23cb7cSEd Maste 		ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24;
69812c23cb7cSEd Maste 		ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40;
69822c23cb7cSEd Maste 		ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56;
69832c23cb7cSEd Maste 		break;
69842c23cb7cSEd Maste 	default:
69852c23cb7cSEd Maste 		return (0);
69862c23cb7cSEd Maste 	}
69872c23cb7cSEd Maste 
69882c23cb7cSEd Maste 	*offsetp += bytes_to_read;
69892c23cb7cSEd Maste 
69902c23cb7cSEd Maste 	return (ret);
69912c23cb7cSEd Maste }
69922c23cb7cSEd Maste 
69932c23cb7cSEd Maste static uint64_t
69942c23cb7cSEd Maste _decode_lsb(uint8_t **data, int bytes_to_read)
69952c23cb7cSEd Maste {
69962c23cb7cSEd Maste 	uint64_t ret;
69972c23cb7cSEd Maste 	uint8_t *src;
69982c23cb7cSEd Maste 
69992c23cb7cSEd Maste 	src = *data;
70002c23cb7cSEd Maste 
70012c23cb7cSEd Maste 	ret = 0;
70022c23cb7cSEd Maste 	switch (bytes_to_read) {
70032c23cb7cSEd Maste 	case 8:
70042c23cb7cSEd Maste 		ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
70052c23cb7cSEd Maste 		ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
7006839529caSEd Maste 		/* FALLTHROUGH */
70072c23cb7cSEd Maste 	case 4:
70082c23cb7cSEd Maste 		ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
7009839529caSEd Maste 		/* FALLTHROUGH */
70102c23cb7cSEd Maste 	case 2:
70112c23cb7cSEd Maste 		ret |= ((uint64_t) src[1]) << 8;
7012839529caSEd Maste 		/* FALLTHROUGH */
70132c23cb7cSEd Maste 	case 1:
70142c23cb7cSEd Maste 		ret |= src[0];
70152c23cb7cSEd Maste 		break;
70162c23cb7cSEd Maste 	default:
70172c23cb7cSEd Maste 		return (0);
70182c23cb7cSEd Maste 	}
70192c23cb7cSEd Maste 
70202c23cb7cSEd Maste 	*data += bytes_to_read;
70212c23cb7cSEd Maste 
70222c23cb7cSEd Maste 	return (ret);
70232c23cb7cSEd Maste }
70242c23cb7cSEd Maste 
70252c23cb7cSEd Maste static uint64_t
70262c23cb7cSEd Maste _decode_msb(uint8_t **data, int bytes_to_read)
70272c23cb7cSEd Maste {
70282c23cb7cSEd Maste 	uint64_t ret;
70292c23cb7cSEd Maste 	uint8_t *src;
70302c23cb7cSEd Maste 
70312c23cb7cSEd Maste 	src = *data;
70322c23cb7cSEd Maste 
70332c23cb7cSEd Maste 	ret = 0;
70342c23cb7cSEd Maste 	switch (bytes_to_read) {
70352c23cb7cSEd Maste 	case 1:
70362c23cb7cSEd Maste 		ret = src[0];
70372c23cb7cSEd Maste 		break;
70382c23cb7cSEd Maste 	case 2:
70392c23cb7cSEd Maste 		ret = src[1] | ((uint64_t) src[0]) << 8;
70402c23cb7cSEd Maste 		break;
70412c23cb7cSEd Maste 	case 4:
70422c23cb7cSEd Maste 		ret = src[3] | ((uint64_t) src[2]) << 8;
70432c23cb7cSEd Maste 		ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24;
70442c23cb7cSEd Maste 		break;
70452c23cb7cSEd Maste 	case 8:
70462c23cb7cSEd Maste 		ret = src[7] | ((uint64_t) src[6]) << 8;
70472c23cb7cSEd Maste 		ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24;
70482c23cb7cSEd Maste 		ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40;
70492c23cb7cSEd Maste 		ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56;
70502c23cb7cSEd Maste 		break;
70512c23cb7cSEd Maste 	default:
70522c23cb7cSEd Maste 		return (0);
70532c23cb7cSEd Maste 		break;
70542c23cb7cSEd Maste 	}
70552c23cb7cSEd Maste 
70562c23cb7cSEd Maste 	*data += bytes_to_read;
70572c23cb7cSEd Maste 
70582c23cb7cSEd Maste 	return (ret);
70592c23cb7cSEd Maste }
70602c23cb7cSEd Maste 
70612c23cb7cSEd Maste static int64_t
706295fd7f26SEd Maste _decode_sleb128(uint8_t **dp, uint8_t *dpe)
70632c23cb7cSEd Maste {
70642c23cb7cSEd Maste 	int64_t ret = 0;
70658f32e46dSKai Wang 	uint8_t b = 0;
70662c23cb7cSEd Maste 	int shift = 0;
70672c23cb7cSEd Maste 
70682c23cb7cSEd Maste 	uint8_t *src = *dp;
70692c23cb7cSEd Maste 
70702c23cb7cSEd Maste 	do {
707195fd7f26SEd Maste 		if (src >= dpe)
707295fd7f26SEd Maste 			break;
70732c23cb7cSEd Maste 		b = *src++;
70742c23cb7cSEd Maste 		ret |= ((b & 0x7f) << shift);
70752c23cb7cSEd Maste 		shift += 7;
70762c23cb7cSEd Maste 	} while ((b & 0x80) != 0);
70772c23cb7cSEd Maste 
70782c23cb7cSEd Maste 	if (shift < 32 && (b & 0x40) != 0)
70792c23cb7cSEd Maste 		ret |= (-1 << shift);
70802c23cb7cSEd Maste 
70812c23cb7cSEd Maste 	*dp = src;
70822c23cb7cSEd Maste 
70832c23cb7cSEd Maste 	return (ret);
70842c23cb7cSEd Maste }
70852c23cb7cSEd Maste 
70862c23cb7cSEd Maste static uint64_t
708795fd7f26SEd Maste _decode_uleb128(uint8_t **dp, uint8_t *dpe)
70882c23cb7cSEd Maste {
70892c23cb7cSEd Maste 	uint64_t ret = 0;
70902c23cb7cSEd Maste 	uint8_t b;
70912c23cb7cSEd Maste 	int shift = 0;
70922c23cb7cSEd Maste 
70932c23cb7cSEd Maste 	uint8_t *src = *dp;
70942c23cb7cSEd Maste 
70952c23cb7cSEd Maste 	do {
709695fd7f26SEd Maste 		if (src >= dpe)
709795fd7f26SEd Maste 			break;
70982c23cb7cSEd Maste 		b = *src++;
70992c23cb7cSEd Maste 		ret |= ((b & 0x7f) << shift);
71002c23cb7cSEd Maste 		shift += 7;
71012c23cb7cSEd Maste 	} while ((b & 0x80) != 0);
71022c23cb7cSEd Maste 
71032c23cb7cSEd Maste 	*dp = src;
71042c23cb7cSEd Maste 
71052c23cb7cSEd Maste 	return (ret);
71062c23cb7cSEd Maste }
71072c23cb7cSEd Maste 
71082c23cb7cSEd Maste static void
71092c23cb7cSEd Maste readelf_version(void)
71102c23cb7cSEd Maste {
71112c23cb7cSEd Maste 	(void) printf("%s (%s)\n", ELFTC_GETPROGNAME(),
71122c23cb7cSEd Maste 	    elftc_version());
71132c23cb7cSEd Maste 	exit(EXIT_SUCCESS);
71142c23cb7cSEd Maste }
71152c23cb7cSEd Maste 
71162c23cb7cSEd Maste #define	USAGE_MESSAGE	"\
71172c23cb7cSEd Maste Usage: %s [options] file...\n\
71182c23cb7cSEd Maste   Display information about ELF objects and ar(1) archives.\n\n\
71192c23cb7cSEd Maste   Options:\n\
71202c23cb7cSEd Maste   -a | --all               Equivalent to specifying options '-dhIlrsASV'.\n\
71212c23cb7cSEd Maste   -c | --archive-index     Print the archive symbol table for archives.\n\
71222c23cb7cSEd Maste   -d | --dynamic           Print the contents of SHT_DYNAMIC sections.\n\
71232c23cb7cSEd Maste   -e | --headers           Print all headers in the object.\n\
71243ef90571SEd Maste   -g | --section-groups    Print the contents of the section groups.\n\
71252c23cb7cSEd Maste   -h | --file-header       Print the file header for the object.\n\
71262c23cb7cSEd Maste   -l | --program-headers   Print the PHDR table for the object.\n\
71272c23cb7cSEd Maste   -n | --notes             Print the contents of SHT_NOTE sections.\n\
71282c23cb7cSEd Maste   -p INDEX | --string-dump=INDEX\n\
71292c23cb7cSEd Maste                            Print the contents of section at index INDEX.\n\
71302c23cb7cSEd Maste   -r | --relocs            Print relocation information.\n\
71312c23cb7cSEd Maste   -s | --syms | --symbols  Print symbol tables.\n\
71322c23cb7cSEd Maste   -t | --section-details   Print additional information about sections.\n\
71332c23cb7cSEd Maste   -v | --version           Print a version identifier and exit.\n\
7134839529caSEd Maste   -w[afilmoprsFLR] | --debug-dump={abbrev,aranges,decodedline,frames,\n\
7135839529caSEd Maste                                frames-interp,info,loc,macro,pubnames,\n\
7136839529caSEd Maste                                ranges,Ranges,rawline,str}\n\
7137839529caSEd Maste                            Display DWARF information.\n\
71382c23cb7cSEd Maste   -x INDEX | --hex-dump=INDEX\n\
71392c23cb7cSEd Maste                            Display contents of a section as hexadecimal.\n\
71402c23cb7cSEd Maste   -A | --arch-specific     (accepted, but ignored)\n\
71412c23cb7cSEd Maste   -D | --use-dynamic       Print the symbol table specified by the DT_SYMTAB\n\
71422c23cb7cSEd Maste                            entry in the \".dynamic\" section.\n\
71432c23cb7cSEd Maste   -H | --help              Print a help message.\n\
71442c23cb7cSEd Maste   -I | --histogram         Print information on bucket list lengths for \n\
71452c23cb7cSEd Maste                            hash sections.\n\
71462c23cb7cSEd Maste   -N | --full-section-name (accepted, but ignored)\n\
71472c23cb7cSEd Maste   -S | --sections | --section-headers\n\
71482c23cb7cSEd Maste                            Print information about section headers.\n\
71492c23cb7cSEd Maste   -V | --version-info      Print symbol versoning information.\n\
71502c23cb7cSEd Maste   -W | --wide              Print information without wrapping long lines.\n"
71512c23cb7cSEd Maste 
71522c23cb7cSEd Maste 
71532c23cb7cSEd Maste static void
7154839529caSEd Maste readelf_usage(int status)
71552c23cb7cSEd Maste {
71562c23cb7cSEd Maste 	fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());
7157839529caSEd Maste 	exit(status);
71582c23cb7cSEd Maste }
71592c23cb7cSEd Maste 
71602c23cb7cSEd Maste int
71612c23cb7cSEd Maste main(int argc, char **argv)
71622c23cb7cSEd Maste {
71632c23cb7cSEd Maste 	struct readelf	*re, re_storage;
71642c23cb7cSEd Maste 	unsigned long	 si;
71652c23cb7cSEd Maste 	int		 opt, i;
71662c23cb7cSEd Maste 	char		*ep;
71672c23cb7cSEd Maste 
71682c23cb7cSEd Maste 	re = &re_storage;
71692c23cb7cSEd Maste 	memset(re, 0, sizeof(*re));
71702c23cb7cSEd Maste 	STAILQ_INIT(&re->v_dumpop);
71712c23cb7cSEd Maste 
71722c23cb7cSEd Maste 	while ((opt = getopt_long(argc, argv, "AacDdegHhIi:lNnp:rSstuVvWw::x:",
71732c23cb7cSEd Maste 	    longopts, NULL)) != -1) {
71742c23cb7cSEd Maste 		switch(opt) {
71752c23cb7cSEd Maste 		case '?':
7176839529caSEd Maste 			readelf_usage(EXIT_SUCCESS);
71772c23cb7cSEd Maste 			break;
71782c23cb7cSEd Maste 		case 'A':
71792c23cb7cSEd Maste 			re->options |= RE_AA;
71802c23cb7cSEd Maste 			break;
71812c23cb7cSEd Maste 		case 'a':
71823ef90571SEd Maste 			re->options |= RE_AA | RE_D | RE_G | RE_H | RE_II |
71833ef90571SEd Maste 			    RE_L | RE_R | RE_SS | RE_S | RE_VV;
71842c23cb7cSEd Maste 			break;
71852c23cb7cSEd Maste 		case 'c':
71862c23cb7cSEd Maste 			re->options |= RE_C;
71872c23cb7cSEd Maste 			break;
71882c23cb7cSEd Maste 		case 'D':
71892c23cb7cSEd Maste 			re->options |= RE_DD;
71902c23cb7cSEd Maste 			break;
71912c23cb7cSEd Maste 		case 'd':
71922c23cb7cSEd Maste 			re->options |= RE_D;
71932c23cb7cSEd Maste 			break;
71942c23cb7cSEd Maste 		case 'e':
71952c23cb7cSEd Maste 			re->options |= RE_H | RE_L | RE_SS;
71962c23cb7cSEd Maste 			break;
71972c23cb7cSEd Maste 		case 'g':
71982c23cb7cSEd Maste 			re->options |= RE_G;
71992c23cb7cSEd Maste 			break;
72002c23cb7cSEd Maste 		case 'H':
7201839529caSEd Maste 			readelf_usage(EXIT_SUCCESS);
72022c23cb7cSEd Maste 			break;
72032c23cb7cSEd Maste 		case 'h':
72042c23cb7cSEd Maste 			re->options |= RE_H;
72052c23cb7cSEd Maste 			break;
72062c23cb7cSEd Maste 		case 'I':
72072c23cb7cSEd Maste 			re->options |= RE_II;
72082c23cb7cSEd Maste 			break;
72092c23cb7cSEd Maste 		case 'i':
72102c23cb7cSEd Maste 			/* Not implemented yet. */
72112c23cb7cSEd Maste 			break;
72122c23cb7cSEd Maste 		case 'l':
72132c23cb7cSEd Maste 			re->options |= RE_L;
72142c23cb7cSEd Maste 			break;
72152c23cb7cSEd Maste 		case 'N':
72162c23cb7cSEd Maste 			re->options |= RE_NN;
72172c23cb7cSEd Maste 			break;
72182c23cb7cSEd Maste 		case 'n':
72192c23cb7cSEd Maste 			re->options |= RE_N;
72202c23cb7cSEd Maste 			break;
72212c23cb7cSEd Maste 		case 'p':
72222c23cb7cSEd Maste 			re->options |= RE_P;
72232c23cb7cSEd Maste 			si = strtoul(optarg, &ep, 10);
72242c23cb7cSEd Maste 			if (*ep == '\0')
72252c23cb7cSEd Maste 				add_dumpop(re, (size_t) si, NULL, STR_DUMP,
72262c23cb7cSEd Maste 				    DUMP_BY_INDEX);
72272c23cb7cSEd Maste 			else
72282c23cb7cSEd Maste 				add_dumpop(re, 0, optarg, STR_DUMP,
72292c23cb7cSEd Maste 				    DUMP_BY_NAME);
72302c23cb7cSEd Maste 			break;
72312c23cb7cSEd Maste 		case 'r':
72322c23cb7cSEd Maste 			re->options |= RE_R;
72332c23cb7cSEd Maste 			break;
72342c23cb7cSEd Maste 		case 'S':
72352c23cb7cSEd Maste 			re->options |= RE_SS;
72362c23cb7cSEd Maste 			break;
72372c23cb7cSEd Maste 		case 's':
72382c23cb7cSEd Maste 			re->options |= RE_S;
72392c23cb7cSEd Maste 			break;
72402c23cb7cSEd Maste 		case 't':
72412c23cb7cSEd Maste 			re->options |= RE_T;
72422c23cb7cSEd Maste 			break;
72432c23cb7cSEd Maste 		case 'u':
72442c23cb7cSEd Maste 			re->options |= RE_U;
72452c23cb7cSEd Maste 			break;
72462c23cb7cSEd Maste 		case 'V':
72472c23cb7cSEd Maste 			re->options |= RE_VV;
72482c23cb7cSEd Maste 			break;
72492c23cb7cSEd Maste 		case 'v':
72502c23cb7cSEd Maste 			readelf_version();
72512c23cb7cSEd Maste 			break;
72522c23cb7cSEd Maste 		case 'W':
72532c23cb7cSEd Maste 			re->options |= RE_WW;
72542c23cb7cSEd Maste 			break;
72552c23cb7cSEd Maste 		case 'w':
72562c23cb7cSEd Maste 			re->options |= RE_W;
72572c23cb7cSEd Maste 			parse_dwarf_op_short(re, optarg);
72582c23cb7cSEd Maste 			break;
72592c23cb7cSEd Maste 		case 'x':
72602c23cb7cSEd Maste 			re->options |= RE_X;
72612c23cb7cSEd Maste 			si = strtoul(optarg, &ep, 10);
72622c23cb7cSEd Maste 			if (*ep == '\0')
72632c23cb7cSEd Maste 				add_dumpop(re, (size_t) si, NULL, HEX_DUMP,
72642c23cb7cSEd Maste 				    DUMP_BY_INDEX);
72652c23cb7cSEd Maste 			else
72662c23cb7cSEd Maste 				add_dumpop(re, 0, optarg, HEX_DUMP,
72672c23cb7cSEd Maste 				    DUMP_BY_NAME);
72682c23cb7cSEd Maste 			break;
72692c23cb7cSEd Maste 		case OPTION_DEBUG_DUMP:
72702c23cb7cSEd Maste 			re->options |= RE_W;
72712c23cb7cSEd Maste 			parse_dwarf_op_long(re, optarg);
72722c23cb7cSEd Maste 		}
72732c23cb7cSEd Maste 	}
72742c23cb7cSEd Maste 
72752c23cb7cSEd Maste 	argv += optind;
72762c23cb7cSEd Maste 	argc -= optind;
72772c23cb7cSEd Maste 
72782c23cb7cSEd Maste 	if (argc == 0 || re->options == 0)
7279839529caSEd Maste 		readelf_usage(EXIT_FAILURE);
72802c23cb7cSEd Maste 
72812c23cb7cSEd Maste 	if (argc > 1)
72822c23cb7cSEd Maste 		re->flags |= DISPLAY_FILENAME;
72832c23cb7cSEd Maste 
72842c23cb7cSEd Maste 	if (elf_version(EV_CURRENT) == EV_NONE)
72852c23cb7cSEd Maste 		errx(EXIT_FAILURE, "ELF library initialization failed: %s",
72862c23cb7cSEd Maste 		    elf_errmsg(-1));
72872c23cb7cSEd Maste 
7288b00fe64fSEd Maste 	for (i = 0; i < argc; i++) {
72892c23cb7cSEd Maste 		re->filename = argv[i];
72902c23cb7cSEd Maste 		dump_object(re);
72912c23cb7cSEd Maste 	}
72922c23cb7cSEd Maste 
72932c23cb7cSEd Maste 	exit(EXIT_SUCCESS);
72942c23cb7cSEd Maste }
7295