xref: /freebsd/contrib/elftoolchain/readelf/readelf.c (revision 453b09caf5d11a43b6459aa2cf8302b5b98ad456)
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 
503ef90571SEd Maste ELFTC_VCSID("$Id: readelf.c 3223 2015-05-25 20:37:57Z emaste $");
512c23cb7cSEd Maste 
522c23cb7cSEd Maste /*
532c23cb7cSEd Maste  * readelf(1) options.
542c23cb7cSEd Maste  */
552c23cb7cSEd Maste #define	RE_AA	0x00000001
562c23cb7cSEd Maste #define	RE_C	0x00000002
572c23cb7cSEd Maste #define	RE_DD	0x00000004
582c23cb7cSEd Maste #define	RE_D	0x00000008
592c23cb7cSEd Maste #define	RE_G	0x00000010
602c23cb7cSEd Maste #define	RE_H	0x00000020
612c23cb7cSEd Maste #define	RE_II	0x00000040
622c23cb7cSEd Maste #define	RE_I	0x00000080
632c23cb7cSEd Maste #define	RE_L	0x00000100
642c23cb7cSEd Maste #define	RE_NN	0x00000200
652c23cb7cSEd Maste #define	RE_N	0x00000400
662c23cb7cSEd Maste #define	RE_P	0x00000800
672c23cb7cSEd Maste #define	RE_R	0x00001000
682c23cb7cSEd Maste #define	RE_SS	0x00002000
692c23cb7cSEd Maste #define	RE_S	0x00004000
702c23cb7cSEd Maste #define	RE_T	0x00008000
712c23cb7cSEd Maste #define	RE_U	0x00010000
722c23cb7cSEd Maste #define	RE_VV	0x00020000
732c23cb7cSEd Maste #define	RE_WW	0x00040000
742c23cb7cSEd Maste #define	RE_W	0x00080000
752c23cb7cSEd Maste #define	RE_X	0x00100000
762c23cb7cSEd Maste 
772c23cb7cSEd Maste /*
782c23cb7cSEd Maste  * dwarf dump options.
792c23cb7cSEd Maste  */
802c23cb7cSEd Maste #define	DW_A	0x00000001
812c23cb7cSEd Maste #define	DW_FF	0x00000002
822c23cb7cSEd Maste #define	DW_F	0x00000004
832c23cb7cSEd Maste #define	DW_I	0x00000008
842c23cb7cSEd Maste #define	DW_LL	0x00000010
852c23cb7cSEd Maste #define	DW_L	0x00000020
862c23cb7cSEd Maste #define	DW_M	0x00000040
872c23cb7cSEd Maste #define	DW_O	0x00000080
882c23cb7cSEd Maste #define	DW_P	0x00000100
892c23cb7cSEd Maste #define	DW_RR	0x00000200
902c23cb7cSEd Maste #define	DW_R	0x00000400
912c23cb7cSEd Maste #define	DW_S	0x00000800
922c23cb7cSEd Maste 
932c23cb7cSEd Maste #define	DW_DEFAULT_OPTIONS (DW_A | DW_F | DW_I | DW_L | DW_O | DW_P | \
942c23cb7cSEd Maste 	    DW_R | DW_RR | DW_S)
952c23cb7cSEd Maste 
962c23cb7cSEd Maste /*
972c23cb7cSEd Maste  * readelf(1) run control flags.
982c23cb7cSEd Maste  */
992c23cb7cSEd Maste #define	DISPLAY_FILENAME	0x0001
1002c23cb7cSEd Maste 
1012c23cb7cSEd Maste /*
1022c23cb7cSEd Maste  * Internal data structure for sections.
1032c23cb7cSEd Maste  */
1042c23cb7cSEd Maste struct section {
1052c23cb7cSEd Maste 	const char	*name;		/* section name */
1062c23cb7cSEd Maste 	Elf_Scn		*scn;		/* section scn */
1072c23cb7cSEd Maste 	uint64_t	 off;		/* section offset */
1082c23cb7cSEd Maste 	uint64_t	 sz;		/* section size */
1092c23cb7cSEd Maste 	uint64_t	 entsize;	/* section entsize */
1102c23cb7cSEd Maste 	uint64_t	 align;		/* section alignment */
1112c23cb7cSEd Maste 	uint64_t	 type;		/* section type */
1122c23cb7cSEd Maste 	uint64_t	 flags;		/* section flags */
1132c23cb7cSEd Maste 	uint64_t	 addr;		/* section virtual addr */
1142c23cb7cSEd Maste 	uint32_t	 link;		/* section link ndx */
1152c23cb7cSEd Maste 	uint32_t	 info;		/* section info ndx */
1162c23cb7cSEd Maste };
1172c23cb7cSEd Maste 
1182c23cb7cSEd Maste struct dumpop {
1192c23cb7cSEd Maste 	union {
1202c23cb7cSEd Maste 		size_t si;		/* section index */
1212c23cb7cSEd Maste 		const char *sn;		/* section name */
1222c23cb7cSEd Maste 	} u;
1232c23cb7cSEd Maste 	enum {
1242c23cb7cSEd Maste 		DUMP_BY_INDEX = 0,
1252c23cb7cSEd Maste 		DUMP_BY_NAME
1262c23cb7cSEd Maste 	} type;				/* dump type */
1272c23cb7cSEd Maste #define HEX_DUMP	0x0001
1282c23cb7cSEd Maste #define STR_DUMP	0x0002
1292c23cb7cSEd Maste 	int op;				/* dump operation */
1302c23cb7cSEd Maste 	STAILQ_ENTRY(dumpop) dumpop_list;
1312c23cb7cSEd Maste };
1322c23cb7cSEd Maste 
1332c23cb7cSEd Maste struct symver {
1342c23cb7cSEd Maste 	const char *name;
1352c23cb7cSEd Maste 	int type;
1362c23cb7cSEd Maste };
1372c23cb7cSEd Maste 
1382c23cb7cSEd Maste /*
1392c23cb7cSEd Maste  * Structure encapsulates the global data for readelf(1).
1402c23cb7cSEd Maste  */
1412c23cb7cSEd Maste struct readelf {
1422c23cb7cSEd Maste 	const char	 *filename;	/* current processing file. */
1432c23cb7cSEd Maste 	int		  options;	/* command line options. */
1442c23cb7cSEd Maste 	int		  flags;	/* run control flags. */
1452c23cb7cSEd Maste 	int		  dop;		/* dwarf dump options. */
1462c23cb7cSEd Maste 	Elf		 *elf;		/* underlying ELF descriptor. */
1472c23cb7cSEd Maste 	Elf		 *ar;		/* archive ELF descriptor. */
1482c23cb7cSEd Maste 	Dwarf_Debug	  dbg;		/* DWARF handle. */
149cf781b2eSEd Maste 	Dwarf_Half	  cu_psize;	/* DWARF CU pointer size. */
150cf781b2eSEd Maste 	Dwarf_Half	  cu_osize;	/* DWARF CU offset size. */
151cf781b2eSEd Maste 	Dwarf_Half	  cu_ver;	/* DWARF CU version. */
1522c23cb7cSEd Maste 	GElf_Ehdr	  ehdr;		/* ELF header. */
1532c23cb7cSEd Maste 	int		  ec;		/* ELF class. */
1542c23cb7cSEd Maste 	size_t		  shnum;	/* #sections. */
1552c23cb7cSEd Maste 	struct section	 *vd_s;		/* Verdef section. */
1562c23cb7cSEd Maste 	struct section	 *vn_s;		/* Verneed section. */
1572c23cb7cSEd Maste 	struct section	 *vs_s;		/* Versym section. */
1582c23cb7cSEd Maste 	uint16_t	 *vs;		/* Versym array. */
1592c23cb7cSEd Maste 	int		  vs_sz;	/* Versym array size. */
1602c23cb7cSEd Maste 	struct symver	 *ver;		/* Version array. */
1612c23cb7cSEd Maste 	int		  ver_sz;	/* Size of version array. */
1622c23cb7cSEd Maste 	struct section	 *sl;		/* list of sections. */
1632c23cb7cSEd Maste 	STAILQ_HEAD(, dumpop) v_dumpop; /* list of dump ops. */
1642c23cb7cSEd Maste 	uint64_t	(*dw_read)(Elf_Data *, uint64_t *, int);
1652c23cb7cSEd Maste 	uint64_t	(*dw_decode)(uint8_t **, int);
1662c23cb7cSEd Maste };
1672c23cb7cSEd Maste 
1682c23cb7cSEd Maste enum options
1692c23cb7cSEd Maste {
1702c23cb7cSEd Maste 	OPTION_DEBUG_DUMP
1712c23cb7cSEd Maste };
1722c23cb7cSEd Maste 
1732c23cb7cSEd Maste static struct option longopts[] = {
1742c23cb7cSEd Maste 	{"all", no_argument, NULL, 'a'},
1752c23cb7cSEd Maste 	{"arch-specific", no_argument, NULL, 'A'},
1762c23cb7cSEd Maste 	{"archive-index", no_argument, NULL, 'c'},
1772c23cb7cSEd Maste 	{"debug-dump", optional_argument, NULL, OPTION_DEBUG_DUMP},
1782c23cb7cSEd Maste 	{"dynamic", no_argument, NULL, 'd'},
1792c23cb7cSEd Maste 	{"file-header", no_argument, NULL, 'h'},
1802c23cb7cSEd Maste 	{"full-section-name", no_argument, NULL, 'N'},
1812c23cb7cSEd Maste 	{"headers", no_argument, NULL, 'e'},
1822c23cb7cSEd Maste 	{"help", no_argument, 0, 'H'},
1832c23cb7cSEd Maste 	{"hex-dump", required_argument, NULL, 'x'},
1842c23cb7cSEd Maste 	{"histogram", no_argument, NULL, 'I'},
1852c23cb7cSEd Maste 	{"notes", no_argument, NULL, 'n'},
1862c23cb7cSEd Maste 	{"program-headers", no_argument, NULL, 'l'},
1872c23cb7cSEd Maste 	{"relocs", no_argument, NULL, 'r'},
1882c23cb7cSEd Maste 	{"sections", no_argument, NULL, 'S'},
1892c23cb7cSEd Maste 	{"section-headers", no_argument, NULL, 'S'},
1902c23cb7cSEd Maste 	{"section-groups", no_argument, NULL, 'g'},
1912c23cb7cSEd Maste 	{"section-details", no_argument, NULL, 't'},
1922c23cb7cSEd Maste 	{"segments", no_argument, NULL, 'l'},
1932c23cb7cSEd Maste 	{"string-dump", required_argument, NULL, 'p'},
1942c23cb7cSEd Maste 	{"symbols", no_argument, NULL, 's'},
1952c23cb7cSEd Maste 	{"syms", no_argument, NULL, 's'},
1962c23cb7cSEd Maste 	{"unwind", no_argument, NULL, 'u'},
1972c23cb7cSEd Maste 	{"use-dynamic", no_argument, NULL, 'D'},
1982c23cb7cSEd Maste 	{"version-info", no_argument, 0, 'V'},
1992c23cb7cSEd Maste 	{"version", no_argument, 0, 'v'},
2002c23cb7cSEd Maste 	{"wide", no_argument, 0, 'W'},
2012c23cb7cSEd Maste 	{NULL, 0, NULL, 0}
2022c23cb7cSEd Maste };
2032c23cb7cSEd Maste 
2042c23cb7cSEd Maste struct eflags_desc {
2052c23cb7cSEd Maste 	uint64_t flag;
2062c23cb7cSEd Maste 	const char *desc;
2072c23cb7cSEd Maste };
2082c23cb7cSEd Maste 
2092c23cb7cSEd Maste struct mips_option {
2102c23cb7cSEd Maste 	uint64_t flag;
2112c23cb7cSEd Maste 	const char *desc;
2122c23cb7cSEd Maste };
2132c23cb7cSEd Maste 
2142c23cb7cSEd Maste static void add_dumpop(struct readelf *re, size_t si, const char *sn, int op,
2152c23cb7cSEd Maste     int t);
2162c23cb7cSEd Maste static const char *aeabi_adv_simd_arch(uint64_t simd);
2172c23cb7cSEd Maste static const char *aeabi_align_needed(uint64_t an);
2182c23cb7cSEd Maste static const char *aeabi_align_preserved(uint64_t ap);
2192c23cb7cSEd Maste static const char *aeabi_arm_isa(uint64_t ai);
2202c23cb7cSEd Maste static const char *aeabi_cpu_arch(uint64_t arch);
2212c23cb7cSEd Maste static const char *aeabi_cpu_arch_profile(uint64_t pf);
2222c23cb7cSEd Maste static const char *aeabi_div(uint64_t du);
2232c23cb7cSEd Maste static const char *aeabi_enum_size(uint64_t es);
2242c23cb7cSEd Maste static const char *aeabi_fp_16bit_format(uint64_t fp16);
2252c23cb7cSEd Maste static const char *aeabi_fp_arch(uint64_t fp);
2262c23cb7cSEd Maste static const char *aeabi_fp_denormal(uint64_t fd);
2272c23cb7cSEd Maste static const char *aeabi_fp_exceptions(uint64_t fe);
2282c23cb7cSEd Maste static const char *aeabi_fp_hpext(uint64_t fh);
2292c23cb7cSEd Maste static const char *aeabi_fp_number_model(uint64_t fn);
2302c23cb7cSEd Maste static const char *aeabi_fp_optm_goal(uint64_t fog);
2312c23cb7cSEd Maste static const char *aeabi_fp_rounding(uint64_t fr);
2322c23cb7cSEd Maste static const char *aeabi_hardfp(uint64_t hfp);
2332c23cb7cSEd Maste static const char *aeabi_mpext(uint64_t mp);
2342c23cb7cSEd Maste static const char *aeabi_optm_goal(uint64_t og);
2352c23cb7cSEd Maste static const char *aeabi_pcs_config(uint64_t pcs);
2362c23cb7cSEd Maste static const char *aeabi_pcs_got(uint64_t got);
2372c23cb7cSEd Maste static const char *aeabi_pcs_r9(uint64_t r9);
2382c23cb7cSEd Maste static const char *aeabi_pcs_ro(uint64_t ro);
2392c23cb7cSEd Maste static const char *aeabi_pcs_rw(uint64_t rw);
2402c23cb7cSEd Maste static const char *aeabi_pcs_wchar_t(uint64_t wt);
2412c23cb7cSEd Maste static const char *aeabi_t2ee(uint64_t t2ee);
2422c23cb7cSEd Maste static const char *aeabi_thumb_isa(uint64_t ti);
2432c23cb7cSEd Maste static const char *aeabi_fp_user_exceptions(uint64_t fu);
2442c23cb7cSEd Maste static const char *aeabi_unaligned_access(uint64_t ua);
2452c23cb7cSEd Maste static const char *aeabi_vfp_args(uint64_t va);
2462c23cb7cSEd Maste static const char *aeabi_virtual(uint64_t vt);
2472c23cb7cSEd Maste static const char *aeabi_wmmx_arch(uint64_t wmmx);
2482c23cb7cSEd Maste static const char *aeabi_wmmx_args(uint64_t wa);
2492c23cb7cSEd Maste static const char *elf_class(unsigned int class);
2502c23cb7cSEd Maste static const char *elf_endian(unsigned int endian);
2512c23cb7cSEd Maste static const char *elf_machine(unsigned int mach);
2522c23cb7cSEd Maste static const char *elf_osabi(unsigned int abi);
2532c23cb7cSEd Maste static const char *elf_type(unsigned int type);
2542c23cb7cSEd Maste static const char *elf_ver(unsigned int ver);
2552c23cb7cSEd Maste static const char *dt_type(unsigned int mach, unsigned int dtype);
2562c23cb7cSEd Maste static void dump_ar(struct readelf *re, int);
2572c23cb7cSEd Maste static void dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe);
2582c23cb7cSEd Maste static void dump_attributes(struct readelf *re);
2592c23cb7cSEd Maste static uint8_t *dump_compatibility_tag(uint8_t *p);
2602c23cb7cSEd Maste static void dump_dwarf(struct readelf *re);
261cf781b2eSEd Maste static void dump_dwarf_abbrev(struct readelf *re);
262cf781b2eSEd Maste static void dump_dwarf_aranges(struct readelf *re);
263cf781b2eSEd Maste static void dump_dwarf_block(struct readelf *re, uint8_t *b,
264cf781b2eSEd Maste     Dwarf_Unsigned len);
265cf781b2eSEd Maste static void dump_dwarf_die(struct readelf *re, Dwarf_Die die, int level);
266cf781b2eSEd Maste static void dump_dwarf_frame(struct readelf *re, int alt);
267cf781b2eSEd Maste static void dump_dwarf_frame_inst(struct readelf *re, Dwarf_Cie cie,
268cf781b2eSEd Maste     uint8_t *insts, Dwarf_Unsigned len, Dwarf_Unsigned caf, Dwarf_Signed daf,
269cf781b2eSEd Maste     Dwarf_Addr pc, Dwarf_Debug dbg);
270cf781b2eSEd Maste static int dump_dwarf_frame_regtable(struct readelf *re, Dwarf_Fde fde,
271cf781b2eSEd Maste     Dwarf_Addr pc, Dwarf_Unsigned func_len, Dwarf_Half cie_ra);
272cf781b2eSEd Maste static void dump_dwarf_frame_section(struct readelf *re, struct section *s,
273cf781b2eSEd Maste     int alt);
274cf781b2eSEd Maste static void dump_dwarf_info(struct readelf *re, Dwarf_Bool is_info);
275cf781b2eSEd Maste static void dump_dwarf_macinfo(struct readelf *re);
276cf781b2eSEd Maste static void dump_dwarf_line(struct readelf *re);
277cf781b2eSEd Maste static void dump_dwarf_line_decoded(struct readelf *re);
278cf781b2eSEd Maste static void dump_dwarf_loc(struct readelf *re, Dwarf_Loc *lr);
279cf781b2eSEd Maste static void dump_dwarf_loclist(struct readelf *re);
280cf781b2eSEd Maste static void dump_dwarf_pubnames(struct readelf *re);
281cf781b2eSEd Maste static void dump_dwarf_ranges(struct readelf *re);
282cf781b2eSEd Maste static void dump_dwarf_ranges_foreach(struct readelf *re, Dwarf_Die die,
283cf781b2eSEd Maste     Dwarf_Addr base);
284cf781b2eSEd Maste static void dump_dwarf_str(struct readelf *re);
2852c23cb7cSEd Maste static void dump_eflags(struct readelf *re, uint64_t e_flags);
2862c23cb7cSEd Maste static void dump_elf(struct readelf *re);
2872c23cb7cSEd Maste static void dump_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab);
2882c23cb7cSEd Maste static void dump_dynamic(struct readelf *re);
2892c23cb7cSEd Maste static void dump_liblist(struct readelf *re);
2902c23cb7cSEd Maste static void dump_mips_attributes(struct readelf *re, uint8_t *p, uint8_t *pe);
2912c23cb7cSEd Maste static void dump_mips_odk_reginfo(struct readelf *re, uint8_t *p, size_t sz);
2922c23cb7cSEd Maste static void dump_mips_options(struct readelf *re, struct section *s);
2932c23cb7cSEd Maste static void dump_mips_option_flags(const char *name, struct mips_option *opt,
2942c23cb7cSEd Maste     uint64_t info);
2952c23cb7cSEd Maste static void dump_mips_reginfo(struct readelf *re, struct section *s);
2962c23cb7cSEd Maste static void dump_mips_specific_info(struct readelf *re);
2972c23cb7cSEd Maste static void dump_notes(struct readelf *re);
2982c23cb7cSEd Maste static void dump_notes_content(struct readelf *re, const char *buf, size_t sz,
2992c23cb7cSEd Maste     off_t off);
3002c23cb7cSEd Maste static void dump_svr4_hash(struct section *s);
3012c23cb7cSEd Maste static void dump_svr4_hash64(struct readelf *re, struct section *s);
3022c23cb7cSEd Maste static void dump_gnu_hash(struct readelf *re, struct section *s);
3032c23cb7cSEd Maste static void dump_hash(struct readelf *re);
3042c23cb7cSEd Maste static void dump_phdr(struct readelf *re);
3052c23cb7cSEd Maste static void dump_ppc_attributes(uint8_t *p, uint8_t *pe);
3063ef90571SEd Maste static void dump_section_groups(struct readelf *re);
3072c23cb7cSEd Maste static void dump_symtab(struct readelf *re, int i);
3082c23cb7cSEd Maste static void dump_symtabs(struct readelf *re);
3092c23cb7cSEd Maste static uint8_t *dump_unknown_tag(uint64_t tag, uint8_t *p);
3102c23cb7cSEd Maste static void dump_ver(struct readelf *re);
3112c23cb7cSEd Maste static void dump_verdef(struct readelf *re, int dump);
3122c23cb7cSEd Maste static void dump_verneed(struct readelf *re, int dump);
3132c23cb7cSEd Maste static void dump_versym(struct readelf *re);
314cf781b2eSEd Maste static const char *dwarf_reg(unsigned int mach, unsigned int reg);
315cf781b2eSEd Maste static const char *dwarf_regname(struct readelf *re, unsigned int num);
316cf781b2eSEd Maste static struct dumpop *find_dumpop(struct readelf *re, size_t si,
317cf781b2eSEd Maste     const char *sn, int op, int t);
31871edbbfdSEd Maste static int get_ent_count(struct section *s, int *ent_count);
319cf781b2eSEd Maste static char *get_regoff_str(struct readelf *re, Dwarf_Half reg,
320cf781b2eSEd Maste     Dwarf_Addr off);
3212c23cb7cSEd Maste static const char *get_string(struct readelf *re, int strtab, size_t off);
3222c23cb7cSEd Maste static const char *get_symbol_name(struct readelf *re, int symtab, int i);
3232c23cb7cSEd Maste static uint64_t get_symbol_value(struct readelf *re, int symtab, int i);
3242c23cb7cSEd Maste static void load_sections(struct readelf *re);
3252c23cb7cSEd Maste static const char *mips_abi_fp(uint64_t fp);
32602b08c90SEd Maste static const char *note_type(const char *note_name, unsigned int et,
3272c23cb7cSEd Maste     unsigned int nt);
32802b08c90SEd Maste static const char *note_type_freebsd(unsigned int nt);
32902b08c90SEd Maste static const char *note_type_freebsd_core(unsigned int nt);
33002b08c90SEd Maste static const char *note_type_linux_core(unsigned int nt);
33102b08c90SEd Maste static const char *note_type_gnu(unsigned int nt);
33202b08c90SEd Maste static const char *note_type_netbsd(unsigned int nt);
33302b08c90SEd Maste static const char *note_type_openbsd(unsigned int nt);
33402b08c90SEd Maste static const char *note_type_unknown(unsigned int nt);
3352c23cb7cSEd Maste static const char *option_kind(uint8_t kind);
3362c23cb7cSEd Maste static const char *phdr_type(unsigned int ptype);
3372c23cb7cSEd Maste static const char *ppc_abi_fp(uint64_t fp);
3382c23cb7cSEd Maste static const char *ppc_abi_vector(uint64_t vec);
3392c23cb7cSEd Maste static const char *r_type(unsigned int mach, unsigned int type);
3402c23cb7cSEd Maste static void readelf_usage(void);
3412c23cb7cSEd Maste static void readelf_version(void);
342cf781b2eSEd Maste static void search_loclist_at(struct readelf *re, Dwarf_Die die,
343cf781b2eSEd Maste     Dwarf_Unsigned lowpc);
3442c23cb7cSEd Maste static void search_ver(struct readelf *re);
3452c23cb7cSEd Maste static const char *section_type(unsigned int mach, unsigned int stype);
346cf781b2eSEd Maste static void set_cu_context(struct readelf *re, Dwarf_Half psize,
347cf781b2eSEd Maste     Dwarf_Half osize, Dwarf_Half ver);
3482c23cb7cSEd Maste static const char *st_bind(unsigned int sbind);
3492c23cb7cSEd Maste static const char *st_shndx(unsigned int shndx);
3502c23cb7cSEd Maste static const char *st_type(unsigned int stype);
3512c23cb7cSEd Maste static const char *st_vis(unsigned int svis);
3522c23cb7cSEd Maste static const char *top_tag(unsigned int tag);
3532c23cb7cSEd Maste static void unload_sections(struct readelf *re);
3542c23cb7cSEd Maste static uint64_t _read_lsb(Elf_Data *d, uint64_t *offsetp,
3552c23cb7cSEd Maste     int bytes_to_read);
3562c23cb7cSEd Maste static uint64_t _read_msb(Elf_Data *d, uint64_t *offsetp,
3572c23cb7cSEd Maste     int bytes_to_read);
3582c23cb7cSEd Maste static uint64_t _decode_lsb(uint8_t **data, int bytes_to_read);
3592c23cb7cSEd Maste static uint64_t _decode_msb(uint8_t **data, int bytes_to_read);
3602c23cb7cSEd Maste static int64_t _decode_sleb128(uint8_t **dp);
3612c23cb7cSEd Maste static uint64_t _decode_uleb128(uint8_t **dp);
3622c23cb7cSEd Maste 
3632c23cb7cSEd Maste static struct eflags_desc arm_eflags_desc[] = {
3642c23cb7cSEd Maste 	{EF_ARM_RELEXEC, "relocatable executable"},
3652c23cb7cSEd Maste 	{EF_ARM_HASENTRY, "has entry point"},
3662c23cb7cSEd Maste 	{EF_ARM_SYMSARESORTED, "sorted symbol tables"},
3672c23cb7cSEd Maste 	{EF_ARM_DYNSYMSUSESEGIDX, "dynamic symbols use segment index"},
3682c23cb7cSEd Maste 	{EF_ARM_MAPSYMSFIRST, "mapping symbols precede others"},
3692c23cb7cSEd Maste 	{EF_ARM_BE8, "BE8"},
3702c23cb7cSEd Maste 	{EF_ARM_LE8, "LE8"},
3712c23cb7cSEd Maste 	{EF_ARM_INTERWORK, "interworking enabled"},
3722c23cb7cSEd Maste 	{EF_ARM_APCS_26, "uses APCS/26"},
3732c23cb7cSEd Maste 	{EF_ARM_APCS_FLOAT, "uses APCS/float"},
3742c23cb7cSEd Maste 	{EF_ARM_PIC, "position independent"},
3752c23cb7cSEd Maste 	{EF_ARM_ALIGN8, "8 bit structure alignment"},
3762c23cb7cSEd Maste 	{EF_ARM_NEW_ABI, "uses new ABI"},
3772c23cb7cSEd Maste 	{EF_ARM_OLD_ABI, "uses old ABI"},
3782c23cb7cSEd Maste 	{EF_ARM_SOFT_FLOAT, "software FP"},
3792c23cb7cSEd Maste 	{EF_ARM_VFP_FLOAT, "VFP"},
3802c23cb7cSEd Maste 	{EF_ARM_MAVERICK_FLOAT, "Maverick FP"},
3812c23cb7cSEd Maste 	{0, NULL}
3822c23cb7cSEd Maste };
3832c23cb7cSEd Maste 
3842c23cb7cSEd Maste static struct eflags_desc mips_eflags_desc[] = {
3852c23cb7cSEd Maste 	{EF_MIPS_NOREORDER, "noreorder"},
3862c23cb7cSEd Maste 	{EF_MIPS_PIC, "pic"},
3872c23cb7cSEd Maste 	{EF_MIPS_CPIC, "cpic"},
3882c23cb7cSEd Maste 	{EF_MIPS_UCODE, "ugen_reserved"},
3892c23cb7cSEd Maste 	{EF_MIPS_ABI2, "abi2"},
3902c23cb7cSEd Maste 	{EF_MIPS_OPTIONS_FIRST, "odk first"},
3912c23cb7cSEd Maste 	{EF_MIPS_ARCH_ASE_MDMX, "mdmx"},
3922c23cb7cSEd Maste 	{EF_MIPS_ARCH_ASE_M16, "mips16"},
3932c23cb7cSEd Maste 	{0, NULL}
3942c23cb7cSEd Maste };
3952c23cb7cSEd Maste 
3962c23cb7cSEd Maste static struct eflags_desc powerpc_eflags_desc[] = {
3972c23cb7cSEd Maste 	{EF_PPC_EMB, "emb"},
3982c23cb7cSEd Maste 	{EF_PPC_RELOCATABLE, "relocatable"},
3992c23cb7cSEd Maste 	{EF_PPC_RELOCATABLE_LIB, "relocatable-lib"},
4002c23cb7cSEd Maste 	{0, NULL}
4012c23cb7cSEd Maste };
4022c23cb7cSEd Maste 
4032c23cb7cSEd Maste static struct eflags_desc sparc_eflags_desc[] = {
4042c23cb7cSEd Maste 	{EF_SPARC_32PLUS, "v8+"},
4052c23cb7cSEd Maste 	{EF_SPARC_SUN_US1, "ultrasparcI"},
4062c23cb7cSEd Maste 	{EF_SPARC_HAL_R1, "halr1"},
4072c23cb7cSEd Maste 	{EF_SPARC_SUN_US3, "ultrasparcIII"},
4082c23cb7cSEd Maste 	{0, NULL}
4092c23cb7cSEd Maste };
4102c23cb7cSEd Maste 
4112c23cb7cSEd Maste static const char *
4122c23cb7cSEd Maste elf_osabi(unsigned int abi)
4132c23cb7cSEd Maste {
4142c23cb7cSEd Maste 	static char s_abi[32];
4152c23cb7cSEd Maste 
4162c23cb7cSEd Maste 	switch(abi) {
417*453b09caSEd Maste 	case ELFOSABI_NONE: return "NONE";
418473c31f1SEd Maste 	case ELFOSABI_HPUX: return "HPUX";
4192c23cb7cSEd Maste 	case ELFOSABI_NETBSD: return "NetBSD";
4202c23cb7cSEd Maste 	case ELFOSABI_GNU: return "GNU";
4212c23cb7cSEd Maste 	case ELFOSABI_HURD: return "HURD";
4222c23cb7cSEd Maste 	case ELFOSABI_86OPEN: return "86OPEN";
4232c23cb7cSEd Maste 	case ELFOSABI_SOLARIS: return "Solaris";
4242c23cb7cSEd Maste 	case ELFOSABI_AIX: return "AIX";
4252c23cb7cSEd Maste 	case ELFOSABI_IRIX: return "IRIX";
4262c23cb7cSEd Maste 	case ELFOSABI_FREEBSD: return "FreeBSD";
4272c23cb7cSEd Maste 	case ELFOSABI_TRU64: return "TRU64";
4282c23cb7cSEd Maste 	case ELFOSABI_MODESTO: return "MODESTO";
4292c23cb7cSEd Maste 	case ELFOSABI_OPENBSD: return "OpenBSD";
4302c23cb7cSEd Maste 	case ELFOSABI_OPENVMS: return "OpenVMS";
4312c23cb7cSEd Maste 	case ELFOSABI_NSK: return "NSK";
4322c23cb7cSEd Maste 	case ELFOSABI_ARM: return "ARM";
4332c23cb7cSEd Maste 	case ELFOSABI_STANDALONE: return "StandAlone";
4342c23cb7cSEd Maste 	default:
4352c23cb7cSEd Maste 		snprintf(s_abi, sizeof(s_abi), "<unknown: %#x>", abi);
4362c23cb7cSEd Maste 		return (s_abi);
4372c23cb7cSEd Maste 	}
4382c23cb7cSEd Maste };
4392c23cb7cSEd Maste 
4402c23cb7cSEd Maste static const char *
4412c23cb7cSEd Maste elf_machine(unsigned int mach)
4422c23cb7cSEd Maste {
4432c23cb7cSEd Maste 	static char s_mach[32];
4442c23cb7cSEd Maste 
4452c23cb7cSEd Maste 	switch (mach) {
4462c23cb7cSEd Maste 	case EM_NONE: return "Unknown machine";
4472c23cb7cSEd Maste 	case EM_M32: return "AT&T WE32100";
4482c23cb7cSEd Maste 	case EM_SPARC: return "Sun SPARC";
4492c23cb7cSEd Maste 	case EM_386: return "Intel i386";
4502c23cb7cSEd Maste 	case EM_68K: return "Motorola 68000";
4513ef90571SEd Maste 	case EM_IAMCU: return "Intel MCU";
4522c23cb7cSEd Maste 	case EM_88K: return "Motorola 88000";
4532c23cb7cSEd Maste 	case EM_860: return "Intel i860";
4542c23cb7cSEd Maste 	case EM_MIPS: return "MIPS R3000 Big-Endian only";
4552c23cb7cSEd Maste 	case EM_S370: return "IBM System/370";
4562c23cb7cSEd Maste 	case EM_MIPS_RS3_LE: return "MIPS R3000 Little-Endian";
4572c23cb7cSEd Maste 	case EM_PARISC: return "HP PA-RISC";
4582c23cb7cSEd Maste 	case EM_VPP500: return "Fujitsu VPP500";
4592c23cb7cSEd Maste 	case EM_SPARC32PLUS: return "SPARC v8plus";
4602c23cb7cSEd Maste 	case EM_960: return "Intel 80960";
4612c23cb7cSEd Maste 	case EM_PPC: return "PowerPC 32-bit";
4622c23cb7cSEd Maste 	case EM_PPC64: return "PowerPC 64-bit";
4632c23cb7cSEd Maste 	case EM_S390: return "IBM System/390";
4642c23cb7cSEd Maste 	case EM_V800: return "NEC V800";
4652c23cb7cSEd Maste 	case EM_FR20: return "Fujitsu FR20";
4662c23cb7cSEd Maste 	case EM_RH32: return "TRW RH-32";
4672c23cb7cSEd Maste 	case EM_RCE: return "Motorola RCE";
4682c23cb7cSEd Maste 	case EM_ARM: return "ARM";
4692c23cb7cSEd Maste 	case EM_SH: return "Hitachi SH";
4702c23cb7cSEd Maste 	case EM_SPARCV9: return "SPARC v9 64-bit";
4712c23cb7cSEd Maste 	case EM_TRICORE: return "Siemens TriCore embedded processor";
4722c23cb7cSEd Maste 	case EM_ARC: return "Argonaut RISC Core";
4732c23cb7cSEd Maste 	case EM_H8_300: return "Hitachi H8/300";
4742c23cb7cSEd Maste 	case EM_H8_300H: return "Hitachi H8/300H";
4752c23cb7cSEd Maste 	case EM_H8S: return "Hitachi H8S";
4762c23cb7cSEd Maste 	case EM_H8_500: return "Hitachi H8/500";
4772c23cb7cSEd Maste 	case EM_IA_64: return "Intel IA-64 Processor";
4782c23cb7cSEd Maste 	case EM_MIPS_X: return "Stanford MIPS-X";
4792c23cb7cSEd Maste 	case EM_COLDFIRE: return "Motorola ColdFire";
4802c23cb7cSEd Maste 	case EM_68HC12: return "Motorola M68HC12";
4812c23cb7cSEd Maste 	case EM_MMA: return "Fujitsu MMA";
4822c23cb7cSEd Maste 	case EM_PCP: return "Siemens PCP";
4832c23cb7cSEd Maste 	case EM_NCPU: return "Sony nCPU";
4842c23cb7cSEd Maste 	case EM_NDR1: return "Denso NDR1 microprocessor";
4852c23cb7cSEd Maste 	case EM_STARCORE: return "Motorola Star*Core processor";
4862c23cb7cSEd Maste 	case EM_ME16: return "Toyota ME16 processor";
4872c23cb7cSEd Maste 	case EM_ST100: return "STMicroelectronics ST100 processor";
4882c23cb7cSEd Maste 	case EM_TINYJ: return "Advanced Logic Corp. TinyJ processor";
4892c23cb7cSEd Maste 	case EM_X86_64: return "Advanced Micro Devices x86-64";
4902c23cb7cSEd Maste 	case EM_PDSP: return "Sony DSP Processor";
4912c23cb7cSEd Maste 	case EM_FX66: return "Siemens FX66 microcontroller";
4922c23cb7cSEd Maste 	case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 microcontroller";
4932c23cb7cSEd Maste 	case EM_ST7: return "STmicroelectronics ST7 8-bit microcontroller";
4942c23cb7cSEd Maste 	case EM_68HC16: return "Motorola MC68HC16 microcontroller";
4952c23cb7cSEd Maste 	case EM_68HC11: return "Motorola MC68HC11 microcontroller";
4962c23cb7cSEd Maste 	case EM_68HC08: return "Motorola MC68HC08 microcontroller";
4972c23cb7cSEd Maste 	case EM_68HC05: return "Motorola MC68HC05 microcontroller";
4982c23cb7cSEd Maste 	case EM_SVX: return "Silicon Graphics SVx";
4992c23cb7cSEd Maste 	case EM_ST19: return "STMicroelectronics ST19 8-bit mc";
5002c23cb7cSEd Maste 	case EM_VAX: return "Digital VAX";
5012c23cb7cSEd Maste 	case EM_CRIS: return "Axis Communications 32-bit embedded processor";
5022c23cb7cSEd Maste 	case EM_JAVELIN: return "Infineon Tech. 32bit embedded processor";
5032c23cb7cSEd Maste 	case EM_FIREPATH: return "Element 14 64-bit DSP Processor";
5042c23cb7cSEd Maste 	case EM_ZSP: return "LSI Logic 16-bit DSP Processor";
5052c23cb7cSEd Maste 	case EM_MMIX: return "Donald Knuth's educational 64-bit proc";
5062c23cb7cSEd Maste 	case EM_HUANY: return "Harvard University MI object files";
5072c23cb7cSEd Maste 	case EM_PRISM: return "SiTera Prism";
5082c23cb7cSEd Maste 	case EM_AVR: return "Atmel AVR 8-bit microcontroller";
5092c23cb7cSEd Maste 	case EM_FR30: return "Fujitsu FR30";
5102c23cb7cSEd Maste 	case EM_D10V: return "Mitsubishi D10V";
5112c23cb7cSEd Maste 	case EM_D30V: return "Mitsubishi D30V";
5122c23cb7cSEd Maste 	case EM_V850: return "NEC v850";
5132c23cb7cSEd Maste 	case EM_M32R: return "Mitsubishi M32R";
5142c23cb7cSEd Maste 	case EM_MN10300: return "Matsushita MN10300";
5152c23cb7cSEd Maste 	case EM_MN10200: return "Matsushita MN10200";
5162c23cb7cSEd Maste 	case EM_PJ: return "picoJava";
5172c23cb7cSEd Maste 	case EM_OPENRISC: return "OpenRISC 32-bit embedded processor";
5182c23cb7cSEd Maste 	case EM_ARC_A5: return "ARC Cores Tangent-A5";
5192c23cb7cSEd Maste 	case EM_XTENSA: return "Tensilica Xtensa Architecture";
5202c23cb7cSEd Maste 	case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
5212c23cb7cSEd Maste 	case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
5222c23cb7cSEd Maste 	case EM_NS32K: return "National Semiconductor 32000 series";
5232c23cb7cSEd Maste 	case EM_TPC: return "Tenor Network TPC processor";
5242c23cb7cSEd Maste 	case EM_SNP1K: return "Trebia SNP 1000 processor";
5252c23cb7cSEd Maste 	case EM_ST200: return "STMicroelectronics ST200 microcontroller";
5262c23cb7cSEd Maste 	case EM_IP2K: return "Ubicom IP2xxx microcontroller family";
5272c23cb7cSEd Maste 	case EM_MAX: return "MAX Processor";
5282c23cb7cSEd Maste 	case EM_CR: return "National Semiconductor CompactRISC microprocessor";
5292c23cb7cSEd Maste 	case EM_F2MC16: return "Fujitsu F2MC16";
5302c23cb7cSEd Maste 	case EM_MSP430: return "TI embedded microcontroller msp430";
5312c23cb7cSEd Maste 	case EM_BLACKFIN: return "Analog Devices Blackfin (DSP) processor";
5322c23cb7cSEd Maste 	case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
5332c23cb7cSEd Maste 	case EM_SEP: return "Sharp embedded microprocessor";
5342c23cb7cSEd Maste 	case EM_ARCA: return "Arca RISC Microprocessor";
5352c23cb7cSEd Maste 	case EM_UNICORE: return "Microprocessor series from PKU-Unity Ltd";
536b3f26809SEd Maste 	case EM_AARCH64: return "AArch64";
537119b7592SEd Maste 	case EM_RISCV: return "RISC-V";
5382c23cb7cSEd Maste 	default:
5392c23cb7cSEd Maste 		snprintf(s_mach, sizeof(s_mach), "<unknown: %#x>", mach);
5402c23cb7cSEd Maste 		return (s_mach);
5412c23cb7cSEd Maste 	}
5422c23cb7cSEd Maste 
5432c23cb7cSEd Maste }
5442c23cb7cSEd Maste 
5452c23cb7cSEd Maste static const char *
5462c23cb7cSEd Maste elf_class(unsigned int class)
5472c23cb7cSEd Maste {
5482c23cb7cSEd Maste 	static char s_class[32];
5492c23cb7cSEd Maste 
5502c23cb7cSEd Maste 	switch (class) {
5512c23cb7cSEd Maste 	case ELFCLASSNONE: return "none";
5522c23cb7cSEd Maste 	case ELFCLASS32: return "ELF32";
5532c23cb7cSEd Maste 	case ELFCLASS64: return "ELF64";
5542c23cb7cSEd Maste 	default:
5552c23cb7cSEd Maste 		snprintf(s_class, sizeof(s_class), "<unknown: %#x>", class);
5562c23cb7cSEd Maste 		return (s_class);
5572c23cb7cSEd Maste 	}
5582c23cb7cSEd Maste }
5592c23cb7cSEd Maste 
5602c23cb7cSEd Maste static const char *
5612c23cb7cSEd Maste elf_endian(unsigned int endian)
5622c23cb7cSEd Maste {
5632c23cb7cSEd Maste 	static char s_endian[32];
5642c23cb7cSEd Maste 
5652c23cb7cSEd Maste 	switch (endian) {
5662c23cb7cSEd Maste 	case ELFDATANONE: return "none";
5672c23cb7cSEd Maste 	case ELFDATA2LSB: return "2's complement, little endian";
5682c23cb7cSEd Maste 	case ELFDATA2MSB: return "2's complement, big endian";
5692c23cb7cSEd Maste 	default:
5702c23cb7cSEd Maste 		snprintf(s_endian, sizeof(s_endian), "<unknown: %#x>", endian);
5712c23cb7cSEd Maste 		return (s_endian);
5722c23cb7cSEd Maste 	}
5732c23cb7cSEd Maste }
5742c23cb7cSEd Maste 
5752c23cb7cSEd Maste static const char *
5762c23cb7cSEd Maste elf_type(unsigned int type)
5772c23cb7cSEd Maste {
5782c23cb7cSEd Maste 	static char s_type[32];
5792c23cb7cSEd Maste 
5802c23cb7cSEd Maste 	switch (type) {
5812c23cb7cSEd Maste 	case ET_NONE: return "NONE (None)";
5822c23cb7cSEd Maste 	case ET_REL: return "REL (Relocatable file)";
5832c23cb7cSEd Maste 	case ET_EXEC: return "EXEC (Executable file)";
5842c23cb7cSEd Maste 	case ET_DYN: return "DYN (Shared object file)";
5852c23cb7cSEd Maste 	case ET_CORE: return "CORE (Core file)";
5862c23cb7cSEd Maste 	default:
5872c23cb7cSEd Maste 		if (type >= ET_LOPROC)
5882c23cb7cSEd Maste 			snprintf(s_type, sizeof(s_type), "<proc: %#x>", type);
5892c23cb7cSEd Maste 		else if (type >= ET_LOOS && type <= ET_HIOS)
5902c23cb7cSEd Maste 			snprintf(s_type, sizeof(s_type), "<os: %#x>", type);
5912c23cb7cSEd Maste 		else
5922c23cb7cSEd Maste 			snprintf(s_type, sizeof(s_type), "<unknown: %#x>",
5932c23cb7cSEd Maste 			    type);
5942c23cb7cSEd Maste 		return (s_type);
5952c23cb7cSEd Maste 	}
5962c23cb7cSEd Maste }
5972c23cb7cSEd Maste 
5982c23cb7cSEd Maste static const char *
5992c23cb7cSEd Maste elf_ver(unsigned int ver)
6002c23cb7cSEd Maste {
6012c23cb7cSEd Maste 	static char s_ver[32];
6022c23cb7cSEd Maste 
6032c23cb7cSEd Maste 	switch (ver) {
6042c23cb7cSEd Maste 	case EV_CURRENT: return "(current)";
6052c23cb7cSEd Maste 	case EV_NONE: return "(none)";
6062c23cb7cSEd Maste 	default:
6072c23cb7cSEd Maste 		snprintf(s_ver, sizeof(s_ver), "<unknown: %#x>",
6082c23cb7cSEd Maste 		    ver);
6092c23cb7cSEd Maste 		return (s_ver);
6102c23cb7cSEd Maste 	}
6112c23cb7cSEd Maste }
6122c23cb7cSEd Maste 
6132c23cb7cSEd Maste static const char *
6142c23cb7cSEd Maste phdr_type(unsigned int ptype)
6152c23cb7cSEd Maste {
6162c23cb7cSEd Maste 	static char s_ptype[32];
6172c23cb7cSEd Maste 
6182c23cb7cSEd Maste 	switch (ptype) {
6192c23cb7cSEd Maste 	case PT_NULL: return "NULL";
6202c23cb7cSEd Maste 	case PT_LOAD: return "LOAD";
6212c23cb7cSEd Maste 	case PT_DYNAMIC: return "DYNAMIC";
6222c23cb7cSEd Maste 	case PT_INTERP: return "INTERP";
6232c23cb7cSEd Maste 	case PT_NOTE: return "NOTE";
6242c23cb7cSEd Maste 	case PT_SHLIB: return "SHLIB";
6252c23cb7cSEd Maste 	case PT_PHDR: return "PHDR";
6262c23cb7cSEd Maste 	case PT_TLS: return "TLS";
6272c23cb7cSEd Maste 	case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
6282c23cb7cSEd Maste 	case PT_GNU_STACK: return "GNU_STACK";
6292c23cb7cSEd Maste 	case PT_GNU_RELRO: return "GNU_RELRO";
6302c23cb7cSEd Maste 	default:
6312c23cb7cSEd Maste 		if (ptype >= PT_LOPROC && ptype <= PT_HIPROC)
6322c23cb7cSEd Maste 			snprintf(s_ptype, sizeof(s_ptype), "LOPROC+%#x",
6332c23cb7cSEd Maste 			    ptype - PT_LOPROC);
6342c23cb7cSEd Maste 		else if (ptype >= PT_LOOS && ptype <= PT_HIOS)
6352c23cb7cSEd Maste 			snprintf(s_ptype, sizeof(s_ptype), "LOOS+%#x",
6362c23cb7cSEd Maste 			    ptype - PT_LOOS);
6372c23cb7cSEd Maste 		else
6382c23cb7cSEd Maste 			snprintf(s_ptype, sizeof(s_ptype), "<unknown: %#x>",
6392c23cb7cSEd Maste 			    ptype);
6402c23cb7cSEd Maste 		return (s_ptype);
6412c23cb7cSEd Maste 	}
6422c23cb7cSEd Maste }
6432c23cb7cSEd Maste 
6442c23cb7cSEd Maste static const char *
6452c23cb7cSEd Maste section_type(unsigned int mach, unsigned int stype)
6462c23cb7cSEd Maste {
6472c23cb7cSEd Maste 	static char s_stype[32];
6482c23cb7cSEd Maste 
6492c23cb7cSEd Maste 	if (stype >= SHT_LOPROC && stype <= SHT_HIPROC) {
6502c23cb7cSEd Maste 		switch (mach) {
6512c23cb7cSEd Maste 		case EM_X86_64:
6522c23cb7cSEd Maste 			switch (stype) {
6532c23cb7cSEd Maste 			case SHT_AMD64_UNWIND: return "AMD64_UNWIND";
6542c23cb7cSEd Maste 			default:
6552c23cb7cSEd Maste 				break;
6562c23cb7cSEd Maste 			}
6572c23cb7cSEd Maste 			break;
6582c23cb7cSEd Maste 		case EM_MIPS:
6592c23cb7cSEd Maste 		case EM_MIPS_RS3_LE:
6602c23cb7cSEd Maste 			switch (stype) {
6612c23cb7cSEd Maste 			case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
6622c23cb7cSEd Maste 			case SHT_MIPS_MSYM: return "MIPS_MSYM";
6632c23cb7cSEd Maste 			case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
6642c23cb7cSEd Maste 			case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
6652c23cb7cSEd Maste 			case SHT_MIPS_UCODE: return "MIPS_UCODE";
6662c23cb7cSEd Maste 			case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
6672c23cb7cSEd Maste 			case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
6682c23cb7cSEd Maste 			case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
6692c23cb7cSEd Maste 			case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
6702c23cb7cSEd Maste 			case SHT_MIPS_RELD: return "MIPS_RELD";
6712c23cb7cSEd Maste 			case SHT_MIPS_IFACE: return "MIPS_IFACE";
6722c23cb7cSEd Maste 			case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
6732c23cb7cSEd Maste 			case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
6742c23cb7cSEd Maste 			case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
6752c23cb7cSEd Maste 			case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
6762c23cb7cSEd Maste 			case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
6772c23cb7cSEd Maste 			case SHT_MIPS_DWARF: return "MIPS_DWARF";
6782c23cb7cSEd Maste 			case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
6792c23cb7cSEd Maste 			case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
6802c23cb7cSEd Maste 			case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
6812c23cb7cSEd Maste 			case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
6822c23cb7cSEd Maste 			case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
6832c23cb7cSEd Maste 			case SHT_MIPS_XLATE: return "MIPS_XLATE";
6842c23cb7cSEd Maste 			case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
6852c23cb7cSEd Maste 			case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
6862c23cb7cSEd Maste 			case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
6872c23cb7cSEd Maste 			case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
6882c23cb7cSEd Maste 			case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
6892c23cb7cSEd Maste 			default:
6902c23cb7cSEd Maste 				break;
6912c23cb7cSEd Maste 			}
6922c23cb7cSEd Maste 			break;
6932c23cb7cSEd Maste 		default:
6942c23cb7cSEd Maste 			break;
6952c23cb7cSEd Maste 		}
6962c23cb7cSEd Maste 
6972c23cb7cSEd Maste 		snprintf(s_stype, sizeof(s_stype), "LOPROC+%#x",
6982c23cb7cSEd Maste 		    stype - SHT_LOPROC);
6992c23cb7cSEd Maste 		return (s_stype);
7002c23cb7cSEd Maste 	}
7012c23cb7cSEd Maste 
7022c23cb7cSEd Maste 	switch (stype) {
7032c23cb7cSEd Maste 	case SHT_NULL: return "NULL";
7042c23cb7cSEd Maste 	case SHT_PROGBITS: return "PROGBITS";
7052c23cb7cSEd Maste 	case SHT_SYMTAB: return "SYMTAB";
7062c23cb7cSEd Maste 	case SHT_STRTAB: return "STRTAB";
7072c23cb7cSEd Maste 	case SHT_RELA: return "RELA";
7082c23cb7cSEd Maste 	case SHT_HASH: return "HASH";
7092c23cb7cSEd Maste 	case SHT_DYNAMIC: return "DYNAMIC";
7102c23cb7cSEd Maste 	case SHT_NOTE: return "NOTE";
7112c23cb7cSEd Maste 	case SHT_NOBITS: return "NOBITS";
7122c23cb7cSEd Maste 	case SHT_REL: return "REL";
7132c23cb7cSEd Maste 	case SHT_SHLIB: return "SHLIB";
7142c23cb7cSEd Maste 	case SHT_DYNSYM: return "DYNSYM";
7152c23cb7cSEd Maste 	case SHT_INIT_ARRAY: return "INIT_ARRAY";
7162c23cb7cSEd Maste 	case SHT_FINI_ARRAY: return "FINI_ARRAY";
7172c23cb7cSEd Maste 	case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
7182c23cb7cSEd Maste 	case SHT_GROUP: return "GROUP";
7192c23cb7cSEd Maste 	case SHT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
7202c23cb7cSEd Maste 	case SHT_SUNW_dof: return "SUNW_dof";
7212c23cb7cSEd Maste 	case SHT_SUNW_cap: return "SUNW_cap";
7222c23cb7cSEd Maste 	case SHT_GNU_HASH: return "GNU_HASH";
7232c23cb7cSEd Maste 	case SHT_SUNW_ANNOTATE: return "SUNW_ANNOTATE";
7242c23cb7cSEd Maste 	case SHT_SUNW_DEBUGSTR: return "SUNW_DEBUGSTR";
7252c23cb7cSEd Maste 	case SHT_SUNW_DEBUG: return "SUNW_DEBUG";
7262c23cb7cSEd Maste 	case SHT_SUNW_move: return "SUNW_move";
7272c23cb7cSEd Maste 	case SHT_SUNW_COMDAT: return "SUNW_COMDAT";
7282c23cb7cSEd Maste 	case SHT_SUNW_syminfo: return "SUNW_syminfo";
7292c23cb7cSEd Maste 	case SHT_SUNW_verdef: return "SUNW_verdef";
7302c23cb7cSEd Maste 	case SHT_SUNW_verneed: return "SUNW_verneed";
7312c23cb7cSEd Maste 	case SHT_SUNW_versym: return "SUNW_versym";
7322c23cb7cSEd Maste 	default:
7332c23cb7cSEd Maste 		if (stype >= SHT_LOOS && stype <= SHT_HIOS)
7342c23cb7cSEd Maste 			snprintf(s_stype, sizeof(s_stype), "LOOS+%#x",
7352c23cb7cSEd Maste 			    stype - SHT_LOOS);
7362c23cb7cSEd Maste 		else if (stype >= SHT_LOUSER)
7372c23cb7cSEd Maste 			snprintf(s_stype, sizeof(s_stype), "LOUSER+%#x",
7382c23cb7cSEd Maste 			    stype - SHT_LOUSER);
7392c23cb7cSEd Maste 		else
7402c23cb7cSEd Maste 			snprintf(s_stype, sizeof(s_stype), "<unknown: %#x>",
7412c23cb7cSEd Maste 			    stype);
7422c23cb7cSEd Maste 		return (s_stype);
7432c23cb7cSEd Maste 	}
7442c23cb7cSEd Maste }
7452c23cb7cSEd Maste 
7462c23cb7cSEd Maste static const char *
7472c23cb7cSEd Maste dt_type(unsigned int mach, unsigned int dtype)
7482c23cb7cSEd Maste {
7492c23cb7cSEd Maste 	static char s_dtype[32];
7502c23cb7cSEd Maste 
7512c23cb7cSEd Maste 	if (dtype >= DT_LOPROC && dtype <= DT_HIPROC) {
7522c23cb7cSEd Maste 		switch (mach) {
7532c23cb7cSEd Maste 		case EM_ARM:
7542c23cb7cSEd Maste 			switch (dtype) {
7552c23cb7cSEd Maste 			case DT_ARM_SYMTABSZ:
7562c23cb7cSEd Maste 				return "ARM_SYMTABSZ";
7572c23cb7cSEd Maste 			default:
7582c23cb7cSEd Maste 				break;
7592c23cb7cSEd Maste 			}
7602c23cb7cSEd Maste 			break;
7612c23cb7cSEd Maste 		case EM_MIPS:
7622c23cb7cSEd Maste 		case EM_MIPS_RS3_LE:
7632c23cb7cSEd Maste 			switch (dtype) {
7642c23cb7cSEd Maste 			case DT_MIPS_RLD_VERSION:
7652c23cb7cSEd Maste 				return "MIPS_RLD_VERSION";
7662c23cb7cSEd Maste 			case DT_MIPS_TIME_STAMP:
7672c23cb7cSEd Maste 				return "MIPS_TIME_STAMP";
7682c23cb7cSEd Maste 			case DT_MIPS_ICHECKSUM:
7692c23cb7cSEd Maste 				return "MIPS_ICHECKSUM";
7702c23cb7cSEd Maste 			case DT_MIPS_IVERSION:
7712c23cb7cSEd Maste 				return "MIPS_IVERSION";
7722c23cb7cSEd Maste 			case DT_MIPS_FLAGS:
7732c23cb7cSEd Maste 				return "MIPS_FLAGS";
7742c23cb7cSEd Maste 			case DT_MIPS_BASE_ADDRESS:
7752c23cb7cSEd Maste 				return "MIPS_BASE_ADDRESS";
7762c23cb7cSEd Maste 			case DT_MIPS_CONFLICT:
7772c23cb7cSEd Maste 				return "MIPS_CONFLICT";
7782c23cb7cSEd Maste 			case DT_MIPS_LIBLIST:
7792c23cb7cSEd Maste 				return "MIPS_LIBLIST";
7802c23cb7cSEd Maste 			case DT_MIPS_LOCAL_GOTNO:
7812c23cb7cSEd Maste 				return "MIPS_LOCAL_GOTNO";
7822c23cb7cSEd Maste 			case DT_MIPS_CONFLICTNO:
7832c23cb7cSEd Maste 				return "MIPS_CONFLICTNO";
7842c23cb7cSEd Maste 			case DT_MIPS_LIBLISTNO:
7852c23cb7cSEd Maste 				return "MIPS_LIBLISTNO";
7862c23cb7cSEd Maste 			case DT_MIPS_SYMTABNO:
7872c23cb7cSEd Maste 				return "MIPS_SYMTABNO";
7882c23cb7cSEd Maste 			case DT_MIPS_UNREFEXTNO:
7892c23cb7cSEd Maste 				return "MIPS_UNREFEXTNO";
7902c23cb7cSEd Maste 			case DT_MIPS_GOTSYM:
7912c23cb7cSEd Maste 				return "MIPS_GOTSYM";
7922c23cb7cSEd Maste 			case DT_MIPS_HIPAGENO:
7932c23cb7cSEd Maste 				return "MIPS_HIPAGENO";
7942c23cb7cSEd Maste 			case DT_MIPS_RLD_MAP:
7952c23cb7cSEd Maste 				return "MIPS_RLD_MAP";
7962c23cb7cSEd Maste 			case DT_MIPS_DELTA_CLASS:
7972c23cb7cSEd Maste 				return "MIPS_DELTA_CLASS";
7982c23cb7cSEd Maste 			case DT_MIPS_DELTA_CLASS_NO:
7992c23cb7cSEd Maste 				return "MIPS_DELTA_CLASS_NO";
8002c23cb7cSEd Maste 			case DT_MIPS_DELTA_INSTANCE:
8012c23cb7cSEd Maste 				return "MIPS_DELTA_INSTANCE";
8022c23cb7cSEd Maste 			case DT_MIPS_DELTA_INSTANCE_NO:
8032c23cb7cSEd Maste 				return "MIPS_DELTA_INSTANCE_NO";
8042c23cb7cSEd Maste 			case DT_MIPS_DELTA_RELOC:
8052c23cb7cSEd Maste 				return "MIPS_DELTA_RELOC";
8062c23cb7cSEd Maste 			case DT_MIPS_DELTA_RELOC_NO:
8072c23cb7cSEd Maste 				return "MIPS_DELTA_RELOC_NO";
8082c23cb7cSEd Maste 			case DT_MIPS_DELTA_SYM:
8092c23cb7cSEd Maste 				return "MIPS_DELTA_SYM";
8102c23cb7cSEd Maste 			case DT_MIPS_DELTA_SYM_NO:
8112c23cb7cSEd Maste 				return "MIPS_DELTA_SYM_NO";
8122c23cb7cSEd Maste 			case DT_MIPS_DELTA_CLASSSYM:
8132c23cb7cSEd Maste 				return "MIPS_DELTA_CLASSSYM";
8142c23cb7cSEd Maste 			case DT_MIPS_DELTA_CLASSSYM_NO:
8152c23cb7cSEd Maste 				return "MIPS_DELTA_CLASSSYM_NO";
8162c23cb7cSEd Maste 			case DT_MIPS_CXX_FLAGS:
8172c23cb7cSEd Maste 				return "MIPS_CXX_FLAGS";
8182c23cb7cSEd Maste 			case DT_MIPS_PIXIE_INIT:
8192c23cb7cSEd Maste 				return "MIPS_PIXIE_INIT";
8202c23cb7cSEd Maste 			case DT_MIPS_SYMBOL_LIB:
8212c23cb7cSEd Maste 				return "MIPS_SYMBOL_LIB";
8222c23cb7cSEd Maste 			case DT_MIPS_LOCALPAGE_GOTIDX:
8232c23cb7cSEd Maste 				return "MIPS_LOCALPAGE_GOTIDX";
8242c23cb7cSEd Maste 			case DT_MIPS_LOCAL_GOTIDX:
8252c23cb7cSEd Maste 				return "MIPS_LOCAL_GOTIDX";
8262c23cb7cSEd Maste 			case DT_MIPS_HIDDEN_GOTIDX:
8272c23cb7cSEd Maste 				return "MIPS_HIDDEN_GOTIDX";
8282c23cb7cSEd Maste 			case DT_MIPS_PROTECTED_GOTIDX:
8292c23cb7cSEd Maste 				return "MIPS_PROTECTED_GOTIDX";
8302c23cb7cSEd Maste 			case DT_MIPS_OPTIONS:
8312c23cb7cSEd Maste 				return "MIPS_OPTIONS";
8322c23cb7cSEd Maste 			case DT_MIPS_INTERFACE:
8332c23cb7cSEd Maste 				return "MIPS_INTERFACE";
8342c23cb7cSEd Maste 			case DT_MIPS_DYNSTR_ALIGN:
8352c23cb7cSEd Maste 				return "MIPS_DYNSTR_ALIGN";
8362c23cb7cSEd Maste 			case DT_MIPS_INTERFACE_SIZE:
8372c23cb7cSEd Maste 				return "MIPS_INTERFACE_SIZE";
8382c23cb7cSEd Maste 			case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
8392c23cb7cSEd Maste 				return "MIPS_RLD_TEXT_RESOLVE_ADDR";
8402c23cb7cSEd Maste 			case DT_MIPS_PERF_SUFFIX:
8412c23cb7cSEd Maste 				return "MIPS_PERF_SUFFIX";
8422c23cb7cSEd Maste 			case DT_MIPS_COMPACT_SIZE:
8432c23cb7cSEd Maste 				return "MIPS_COMPACT_SIZE";
8442c23cb7cSEd Maste 			case DT_MIPS_GP_VALUE:
8452c23cb7cSEd Maste 				return "MIPS_GP_VALUE";
8462c23cb7cSEd Maste 			case DT_MIPS_AUX_DYNAMIC:
8472c23cb7cSEd Maste 				return "MIPS_AUX_DYNAMIC";
8482c23cb7cSEd Maste 			case DT_MIPS_PLTGOT:
8492c23cb7cSEd Maste 				return "MIPS_PLTGOT";
8502c23cb7cSEd Maste 			case DT_MIPS_RLD_OBJ_UPDATE:
8512c23cb7cSEd Maste 				return "MIPS_RLD_OBJ_UPDATE";
8522c23cb7cSEd Maste 			case DT_MIPS_RWPLT:
8532c23cb7cSEd Maste 				return "MIPS_RWPLT";
8542c23cb7cSEd Maste 			default:
8552c23cb7cSEd Maste 				break;
8562c23cb7cSEd Maste 			}
8572c23cb7cSEd Maste 			break;
8582c23cb7cSEd Maste 		case EM_SPARC:
8592c23cb7cSEd Maste 		case EM_SPARC32PLUS:
8602c23cb7cSEd Maste 		case EM_SPARCV9:
8612c23cb7cSEd Maste 			switch (dtype) {
8622c23cb7cSEd Maste 			case DT_SPARC_REGISTER:
8632c23cb7cSEd Maste 				return "DT_SPARC_REGISTER";
8642c23cb7cSEd Maste 			default:
8652c23cb7cSEd Maste 				break;
8662c23cb7cSEd Maste 			}
8672c23cb7cSEd Maste 			break;
8682c23cb7cSEd Maste 		default:
8692c23cb7cSEd Maste 			break;
8702c23cb7cSEd Maste 		}
8712c23cb7cSEd Maste 		snprintf(s_dtype, sizeof(s_dtype), "<unknown: %#x>", dtype);
8722c23cb7cSEd Maste 		return (s_dtype);
8732c23cb7cSEd Maste 	}
8742c23cb7cSEd Maste 
8752c23cb7cSEd Maste 	switch (dtype) {
8762c23cb7cSEd Maste 	case DT_NULL: return "NULL";
8772c23cb7cSEd Maste 	case DT_NEEDED: return "NEEDED";
8782c23cb7cSEd Maste 	case DT_PLTRELSZ: return "PLTRELSZ";
8792c23cb7cSEd Maste 	case DT_PLTGOT: return "PLTGOT";
8802c23cb7cSEd Maste 	case DT_HASH: return "HASH";
8812c23cb7cSEd Maste 	case DT_STRTAB: return "STRTAB";
8822c23cb7cSEd Maste 	case DT_SYMTAB: return "SYMTAB";
8832c23cb7cSEd Maste 	case DT_RELA: return "RELA";
8842c23cb7cSEd Maste 	case DT_RELASZ: return "RELASZ";
8852c23cb7cSEd Maste 	case DT_RELAENT: return "RELAENT";
8862c23cb7cSEd Maste 	case DT_STRSZ: return "STRSZ";
8872c23cb7cSEd Maste 	case DT_SYMENT: return "SYMENT";
8882c23cb7cSEd Maste 	case DT_INIT: return "INIT";
8892c23cb7cSEd Maste 	case DT_FINI: return "FINI";
8902c23cb7cSEd Maste 	case DT_SONAME: return "SONAME";
8912c23cb7cSEd Maste 	case DT_RPATH: return "RPATH";
8922c23cb7cSEd Maste 	case DT_SYMBOLIC: return "SYMBOLIC";
8932c23cb7cSEd Maste 	case DT_REL: return "REL";
8942c23cb7cSEd Maste 	case DT_RELSZ: return "RELSZ";
8952c23cb7cSEd Maste 	case DT_RELENT: return "RELENT";
8962c23cb7cSEd Maste 	case DT_PLTREL: return "PLTREL";
8972c23cb7cSEd Maste 	case DT_DEBUG: return "DEBUG";
8982c23cb7cSEd Maste 	case DT_TEXTREL: return "TEXTREL";
8992c23cb7cSEd Maste 	case DT_JMPREL: return "JMPREL";
9002c23cb7cSEd Maste 	case DT_BIND_NOW: return "BIND_NOW";
9012c23cb7cSEd Maste 	case DT_INIT_ARRAY: return "INIT_ARRAY";
9022c23cb7cSEd Maste 	case DT_FINI_ARRAY: return "FINI_ARRAY";
9032c23cb7cSEd Maste 	case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
9042c23cb7cSEd Maste 	case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
9052c23cb7cSEd Maste 	case DT_RUNPATH: return "RUNPATH";
9062c23cb7cSEd Maste 	case DT_FLAGS: return "FLAGS";
9072c23cb7cSEd Maste 	case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
9082c23cb7cSEd Maste 	case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
9092c23cb7cSEd Maste 	case DT_MAXPOSTAGS: return "MAXPOSTAGS";
9102c23cb7cSEd Maste 	case DT_SUNW_AUXILIARY: return "SUNW_AUXILIARY";
9112c23cb7cSEd Maste 	case DT_SUNW_RTLDINF: return "SUNW_RTLDINF";
9122c23cb7cSEd Maste 	case DT_SUNW_FILTER: return "SUNW_FILTER";
9132c23cb7cSEd Maste 	case DT_SUNW_CAP: return "SUNW_CAP";
9142c23cb7cSEd Maste 	case DT_CHECKSUM: return "CHECKSUM";
9152c23cb7cSEd Maste 	case DT_PLTPADSZ: return "PLTPADSZ";
9162c23cb7cSEd Maste 	case DT_MOVEENT: return "MOVEENT";
9172c23cb7cSEd Maste 	case DT_MOVESZ: return "MOVESZ";
9182013b96eSEd Maste 	case DT_FEATURE: return "FEATURE";
9192c23cb7cSEd Maste 	case DT_POSFLAG_1: return "POSFLAG_1";
9202c23cb7cSEd Maste 	case DT_SYMINSZ: return "SYMINSZ";
9212c23cb7cSEd Maste 	case DT_SYMINENT: return "SYMINENT";
9222c23cb7cSEd Maste 	case DT_GNU_HASH: return "GNU_HASH";
9232c23cb7cSEd Maste 	case DT_GNU_CONFLICT: return "GNU_CONFLICT";
9242c23cb7cSEd Maste 	case DT_GNU_LIBLIST: return "GNU_LIBLIST";
9252c23cb7cSEd Maste 	case DT_CONFIG: return "CONFIG";
9262c23cb7cSEd Maste 	case DT_DEPAUDIT: return "DEPAUDIT";
9272c23cb7cSEd Maste 	case DT_AUDIT: return "AUDIT";
9282c23cb7cSEd Maste 	case DT_PLTPAD: return "PLTPAD";
9292c23cb7cSEd Maste 	case DT_MOVETAB: return "MOVETAB";
9302c23cb7cSEd Maste 	case DT_SYMINFO: return "SYMINFO";
9312c23cb7cSEd Maste 	case DT_VERSYM: return "VERSYM";
9322c23cb7cSEd Maste 	case DT_RELACOUNT: return "RELACOUNT";
9332c23cb7cSEd Maste 	case DT_RELCOUNT: return "RELCOUNT";
9342c23cb7cSEd Maste 	case DT_FLAGS_1: return "FLAGS_1";
9352c23cb7cSEd Maste 	case DT_VERDEF: return "VERDEF";
9362c23cb7cSEd Maste 	case DT_VERDEFNUM: return "VERDEFNUM";
9372c23cb7cSEd Maste 	case DT_VERNEED: return "VERNEED";
9382c23cb7cSEd Maste 	case DT_VERNEEDNUM: return "VERNEEDNUM";
9392c23cb7cSEd Maste 	case DT_AUXILIARY: return "AUXILIARY";
9402c23cb7cSEd Maste 	case DT_USED: return "USED";
9412c23cb7cSEd Maste 	case DT_FILTER: return "FILTER";
9422c23cb7cSEd Maste 	case DT_GNU_PRELINKED: return "GNU_PRELINKED";
9432c23cb7cSEd Maste 	case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
9442c23cb7cSEd Maste 	case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
9452c23cb7cSEd Maste 	default:
9462c23cb7cSEd Maste 		snprintf(s_dtype, sizeof(s_dtype), "<unknown: %#x>", dtype);
9472c23cb7cSEd Maste 		return (s_dtype);
9482c23cb7cSEd Maste 	}
9492c23cb7cSEd Maste }
9502c23cb7cSEd Maste 
9512c23cb7cSEd Maste static const char *
9522c23cb7cSEd Maste st_bind(unsigned int sbind)
9532c23cb7cSEd Maste {
9542c23cb7cSEd Maste 	static char s_sbind[32];
9552c23cb7cSEd Maste 
9562c23cb7cSEd Maste 	switch (sbind) {
9572c23cb7cSEd Maste 	case STB_LOCAL: return "LOCAL";
9582c23cb7cSEd Maste 	case STB_GLOBAL: return "GLOBAL";
9592c23cb7cSEd Maste 	case STB_WEAK: return "WEAK";
9602c23cb7cSEd Maste 	default:
9612c23cb7cSEd Maste 		if (sbind >= STB_LOOS && sbind <= STB_HIOS)
9622c23cb7cSEd Maste 			return "OS";
9632c23cb7cSEd Maste 		else if (sbind >= STB_LOPROC && sbind <= STB_HIPROC)
9642c23cb7cSEd Maste 			return "PROC";
9652c23cb7cSEd Maste 		else
9662c23cb7cSEd Maste 			snprintf(s_sbind, sizeof(s_sbind), "<unknown: %#x>",
9672c23cb7cSEd Maste 			    sbind);
9682c23cb7cSEd Maste 		return (s_sbind);
9692c23cb7cSEd Maste 	}
9702c23cb7cSEd Maste }
9712c23cb7cSEd Maste 
9722c23cb7cSEd Maste static const char *
9732c23cb7cSEd Maste st_type(unsigned int stype)
9742c23cb7cSEd Maste {
9752c23cb7cSEd Maste 	static char s_stype[32];
9762c23cb7cSEd Maste 
9772c23cb7cSEd Maste 	switch (stype) {
9782c23cb7cSEd Maste 	case STT_NOTYPE: return "NOTYPE";
9792c23cb7cSEd Maste 	case STT_OBJECT: return "OBJECT";
9802c23cb7cSEd Maste 	case STT_FUNC: return "FUNC";
9812c23cb7cSEd Maste 	case STT_SECTION: return "SECTION";
9822c23cb7cSEd Maste 	case STT_FILE: return "FILE";
9832c23cb7cSEd Maste 	case STT_COMMON: return "COMMON";
9842c23cb7cSEd Maste 	case STT_TLS: return "TLS";
9852c23cb7cSEd Maste 	default:
9862c23cb7cSEd Maste 		if (stype >= STT_LOOS && stype <= STT_HIOS)
9872c23cb7cSEd Maste 			snprintf(s_stype, sizeof(s_stype), "OS+%#x",
9882c23cb7cSEd Maste 			    stype - STT_LOOS);
9892c23cb7cSEd Maste 		else if (stype >= STT_LOPROC && stype <= STT_HIPROC)
9902c23cb7cSEd Maste 			snprintf(s_stype, sizeof(s_stype), "PROC+%#x",
9912c23cb7cSEd Maste 			    stype - STT_LOPROC);
9922c23cb7cSEd Maste 		else
9932c23cb7cSEd Maste 			snprintf(s_stype, sizeof(s_stype), "<unknown: %#x>",
9942c23cb7cSEd Maste 			    stype);
9952c23cb7cSEd Maste 		return (s_stype);
9962c23cb7cSEd Maste 	}
9972c23cb7cSEd Maste }
9982c23cb7cSEd Maste 
9992c23cb7cSEd Maste static const char *
10002c23cb7cSEd Maste st_vis(unsigned int svis)
10012c23cb7cSEd Maste {
10022c23cb7cSEd Maste 	static char s_svis[32];
10032c23cb7cSEd Maste 
10042c23cb7cSEd Maste 	switch(svis) {
10052c23cb7cSEd Maste 	case STV_DEFAULT: return "DEFAULT";
10062c23cb7cSEd Maste 	case STV_INTERNAL: return "INTERNAL";
10072c23cb7cSEd Maste 	case STV_HIDDEN: return "HIDDEN";
10082c23cb7cSEd Maste 	case STV_PROTECTED: return "PROTECTED";
10092c23cb7cSEd Maste 	default:
10102c23cb7cSEd Maste 		snprintf(s_svis, sizeof(s_svis), "<unknown: %#x>", svis);
10112c23cb7cSEd Maste 		return (s_svis);
10122c23cb7cSEd Maste 	}
10132c23cb7cSEd Maste }
10142c23cb7cSEd Maste 
10152c23cb7cSEd Maste static const char *
10162c23cb7cSEd Maste st_shndx(unsigned int shndx)
10172c23cb7cSEd Maste {
10182c23cb7cSEd Maste 	static char s_shndx[32];
10192c23cb7cSEd Maste 
10202c23cb7cSEd Maste 	switch (shndx) {
10212c23cb7cSEd Maste 	case SHN_UNDEF: return "UND";
10222c23cb7cSEd Maste 	case SHN_ABS: return "ABS";
10232c23cb7cSEd Maste 	case SHN_COMMON: return "COM";
10242c23cb7cSEd Maste 	default:
10252c23cb7cSEd Maste 		if (shndx >= SHN_LOPROC && shndx <= SHN_HIPROC)
10262c23cb7cSEd Maste 			return "PRC";
10272c23cb7cSEd Maste 		else if (shndx >= SHN_LOOS && shndx <= SHN_HIOS)
10282c23cb7cSEd Maste 			return "OS";
10292c23cb7cSEd Maste 		else
10302c23cb7cSEd Maste 			snprintf(s_shndx, sizeof(s_shndx), "%u", shndx);
10312c23cb7cSEd Maste 		return (s_shndx);
10322c23cb7cSEd Maste 	}
10332c23cb7cSEd Maste }
10342c23cb7cSEd Maste 
10352c23cb7cSEd Maste static struct {
10362c23cb7cSEd Maste 	const char *ln;
10372c23cb7cSEd Maste 	char sn;
10382c23cb7cSEd Maste 	int value;
10392c23cb7cSEd Maste } section_flag[] = {
10402c23cb7cSEd Maste 	{"WRITE", 'W', SHF_WRITE},
10412c23cb7cSEd Maste 	{"ALLOC", 'A', SHF_ALLOC},
10422c23cb7cSEd Maste 	{"EXEC", 'X', SHF_EXECINSTR},
10432c23cb7cSEd Maste 	{"MERGE", 'M', SHF_MERGE},
10442c23cb7cSEd Maste 	{"STRINGS", 'S', SHF_STRINGS},
10452c23cb7cSEd Maste 	{"INFO LINK", 'I', SHF_INFO_LINK},
10462c23cb7cSEd Maste 	{"OS NONCONF", 'O', SHF_OS_NONCONFORMING},
10472c23cb7cSEd Maste 	{"GROUP", 'G', SHF_GROUP},
10482c23cb7cSEd Maste 	{"TLS", 'T', SHF_TLS},
10492c23cb7cSEd Maste 	{NULL, 0, 0}
10502c23cb7cSEd Maste };
10512c23cb7cSEd Maste 
10522c23cb7cSEd Maste static const char *
10532c23cb7cSEd Maste r_type(unsigned int mach, unsigned int type)
10542c23cb7cSEd Maste {
10552c23cb7cSEd Maste 	switch(mach) {
10562c23cb7cSEd Maste 	case EM_NONE: return "";
10572c23cb7cSEd Maste 	case EM_386:
10583ef90571SEd Maste 	case EM_IAMCU:
10592c23cb7cSEd Maste 		switch(type) {
10602c23cb7cSEd Maste 		case 0: return "R_386_NONE";
10612c23cb7cSEd Maste 		case 1: return "R_386_32";
10622c23cb7cSEd Maste 		case 2: return "R_386_PC32";
10632c23cb7cSEd Maste 		case 3: return "R_386_GOT32";
10642c23cb7cSEd Maste 		case 4: return "R_386_PLT32";
10652c23cb7cSEd Maste 		case 5: return "R_386_COPY";
10662c23cb7cSEd Maste 		case 6: return "R_386_GLOB_DAT";
10672c23cb7cSEd Maste 		case 7: return "R_386_JMP_SLOT";
10682c23cb7cSEd Maste 		case 8: return "R_386_RELATIVE";
10692c23cb7cSEd Maste 		case 9: return "R_386_GOTOFF";
10702c23cb7cSEd Maste 		case 10: return "R_386_GOTPC";
10712c23cb7cSEd Maste 		case 14: return "R_386_TLS_TPOFF";
10722c23cb7cSEd Maste 		case 15: return "R_386_TLS_IE";
10732c23cb7cSEd Maste 		case 16: return "R_386_TLS_GOTIE";
10742c23cb7cSEd Maste 		case 17: return "R_386_TLS_LE";
10752c23cb7cSEd Maste 		case 18: return "R_386_TLS_GD";
10762c23cb7cSEd Maste 		case 19: return "R_386_TLS_LDM";
10772c23cb7cSEd Maste 		case 24: return "R_386_TLS_GD_32";
10782c23cb7cSEd Maste 		case 25: return "R_386_TLS_GD_PUSH";
10792c23cb7cSEd Maste 		case 26: return "R_386_TLS_GD_CALL";
10802c23cb7cSEd Maste 		case 27: return "R_386_TLS_GD_POP";
10812c23cb7cSEd Maste 		case 28: return "R_386_TLS_LDM_32";
10822c23cb7cSEd Maste 		case 29: return "R_386_TLS_LDM_PUSH";
10832c23cb7cSEd Maste 		case 30: return "R_386_TLS_LDM_CALL";
10842c23cb7cSEd Maste 		case 31: return "R_386_TLS_LDM_POP";
10852c23cb7cSEd Maste 		case 32: return "R_386_TLS_LDO_32";
10862c23cb7cSEd Maste 		case 33: return "R_386_TLS_IE_32";
10872c23cb7cSEd Maste 		case 34: return "R_386_TLS_LE_32";
10882c23cb7cSEd Maste 		case 35: return "R_386_TLS_DTPMOD32";
10892c23cb7cSEd Maste 		case 36: return "R_386_TLS_DTPOFF32";
10902c23cb7cSEd Maste 		case 37: return "R_386_TLS_TPOFF32";
10912c23cb7cSEd Maste 		default: return "";
10922c23cb7cSEd Maste 		}
1093b3f26809SEd Maste 	case EM_AARCH64:
1094b3f26809SEd Maste 		switch(type) {
1095b3f26809SEd Maste 		case 0: return "R_AARCH64_NONE";
1096b3f26809SEd Maste 		case 257: return "R_AARCH64_ABS64";
1097b3f26809SEd Maste 		case 258: return "R_AARCH64_ABS32";
1098b3f26809SEd Maste 		case 259: return "R_AARCH64_ABS16";
1099b3f26809SEd Maste 		case 260: return "R_AARCH64_PREL64";
1100b3f26809SEd Maste 		case 261: return "R_AARCH64_PREL32";
1101b3f26809SEd Maste 		case 262: return "R_AARCH64_PREL16";
1102b3f26809SEd Maste 		case 263: return "R_AARCH64_MOVW_UABS_G0";
1103b3f26809SEd Maste 		case 264: return "R_AARCH64_MOVW_UABS_G0_NC";
1104b3f26809SEd Maste 		case 265: return "R_AARCH64_MOVW_UABS_G1";
1105b3f26809SEd Maste 		case 266: return "R_AARCH64_MOVW_UABS_G1_NC";
1106b3f26809SEd Maste 		case 267: return "R_AARCH64_MOVW_UABS_G2";
1107b3f26809SEd Maste 		case 268: return "R_AARCH64_MOVW_UABS_G2_NC";
1108b3f26809SEd Maste 		case 269: return "R_AARCH64_MOVW_UABS_G3";
1109b3f26809SEd Maste 		case 270: return "R_AARCH64_MOVW_SABS_G0";
1110b3f26809SEd Maste 		case 271: return "R_AARCH64_MOVW_SABS_G1";
1111b3f26809SEd Maste 		case 272: return "R_AARCH64_MOVW_SABS_G2";
1112b3f26809SEd Maste 		case 273: return "R_AARCH64_LD_PREL_LO19";
1113b3f26809SEd Maste 		case 274: return "R_AARCH64_ADR_PREL_LO21";
1114b3f26809SEd Maste 		case 275: return "R_AARCH64_ADR_PREL_PG_HI21";
1115b3f26809SEd Maste 		case 276: return "R_AARCH64_ADR_PREL_PG_HI21_NC";
1116b3f26809SEd Maste 		case 277: return "R_AARCH64_ADD_ABS_LO12_NC";
1117b3f26809SEd Maste 		case 278: return "R_AARCH64_LDST8_ABS_LO12_NC";
1118b3f26809SEd Maste 		case 279: return "R_AARCH64_TSTBR14";
1119b3f26809SEd Maste 		case 280: return "R_AARCH64_CONDBR19";
1120b3f26809SEd Maste 		case 282: return "R_AARCH64_JUMP26";
1121b3f26809SEd Maste 		case 283: return "R_AARCH64_CALL26";
1122b3f26809SEd Maste 		case 284: return "R_AARCH64_LDST16_ABS_LO12_NC";
1123b3f26809SEd Maste 		case 285: return "R_AARCH64_LDST32_ABS_LO12_NC";
1124b3f26809SEd Maste 		case 286: return "R_AARCH64_LDST64_ABS_LO12_NC";
1125b3f26809SEd Maste 		case 287: return "R_AARCH64_MOVW_PREL_G0";
1126b3f26809SEd Maste 		case 288: return "R_AARCH64_MOVW_PREL_G0_NC";
1127b3f26809SEd Maste 		case 289: return "R_AARCH64_MOVW_PREL_G1";
1128b3f26809SEd Maste 		case 290: return "R_AARCH64_MOVW_PREL_G1_NC";
1129b3f26809SEd Maste 		case 291: return "R_AARCH64_MOVW_PREL_G2";
1130b3f26809SEd Maste 		case 292: return "R_AARCH64_MOVW_PREL_G2_NC";
1131b3f26809SEd Maste 		case 293: return "R_AARCH64_MOVW_PREL_G3";
1132b3f26809SEd Maste 		case 299: return "R_AARCH64_LDST128_ABS_LO12_NC";
1133b3f26809SEd Maste 		case 300: return "R_AARCH64_MOVW_GOTOFF_G0";
1134b3f26809SEd Maste 		case 301: return "R_AARCH64_MOVW_GOTOFF_G0_NC";
1135b3f26809SEd Maste 		case 302: return "R_AARCH64_MOVW_GOTOFF_G1";
1136b3f26809SEd Maste 		case 303: return "R_AARCH64_MOVW_GOTOFF_G1_NC";
1137b3f26809SEd Maste 		case 304: return "R_AARCH64_MOVW_GOTOFF_G2";
1138b3f26809SEd Maste 		case 305: return "R_AARCH64_MOVW_GOTOFF_G2_NC";
1139b3f26809SEd Maste 		case 306: return "R_AARCH64_MOVW_GOTOFF_G3";
1140b3f26809SEd Maste 		case 307: return "R_AARCH64_GOTREL64";
1141b3f26809SEd Maste 		case 308: return "R_AARCH64_GOTREL32";
1142b3f26809SEd Maste 		case 309: return "R_AARCH64_GOT_LD_PREL19";
1143b3f26809SEd Maste 		case 310: return "R_AARCH64_LD64_GOTOFF_LO15";
1144b3f26809SEd Maste 		case 311: return "R_AARCH64_ADR_GOT_PAGE";
1145b3f26809SEd Maste 		case 312: return "R_AARCH64_LD64_GOT_LO12_NC";
1146b3f26809SEd Maste 		case 313: return "R_AARCH64_LD64_GOTPAGE_LO15";
1147b3f26809SEd Maste 		case 1024: return "R_AARCH64_COPY";
1148b3f26809SEd Maste 		case 1025: return "R_AARCH64_GLOB_DAT";
1149b3f26809SEd Maste 		case 1026: return "R_AARCH64_JUMP_SLOT";
1150b3f26809SEd Maste 		case 1027: return "R_AARCH64_RELATIVE";
115167d97fe7SEd Maste 		case 1028: return "R_AARCH64_TLS_DTPREL64";
115267d97fe7SEd Maste 		case 1029: return "R_AARCH64_TLS_DTPMOD64";
115367d97fe7SEd Maste 		case 1030: return "R_AARCH64_TLS_TPREL64";
1154b3f26809SEd Maste 		case 1031: return "R_AARCH64_TLSDESC";
115567d97fe7SEd Maste 		case 1032: return "R_AARCH64_IRELATIVE";
1156b3f26809SEd Maste 		default: return "";
1157b3f26809SEd Maste 		}
11582c23cb7cSEd Maste 	case EM_ARM:
11592c23cb7cSEd Maste 		switch(type) {
11602c23cb7cSEd Maste 		case 0: return "R_ARM_NONE";
11612c23cb7cSEd Maste 		case 1: return "R_ARM_PC24";
11622c23cb7cSEd Maste 		case 2: return "R_ARM_ABS32";
11632c23cb7cSEd Maste 		case 3: return "R_ARM_REL32";
11642c23cb7cSEd Maste 		case 4: return "R_ARM_PC13";
11652c23cb7cSEd Maste 		case 5: return "R_ARM_ABS16";
11662c23cb7cSEd Maste 		case 6: return "R_ARM_ABS12";
11672c23cb7cSEd Maste 		case 7: return "R_ARM_THM_ABS5";
11682c23cb7cSEd Maste 		case 8: return "R_ARM_ABS8";
11692c23cb7cSEd Maste 		case 9: return "R_ARM_SBREL32";
11702c23cb7cSEd Maste 		case 10: return "R_ARM_THM_PC22";
11712c23cb7cSEd Maste 		case 11: return "R_ARM_THM_PC8";
11722c23cb7cSEd Maste 		case 12: return "R_ARM_AMP_VCALL9";
11732c23cb7cSEd Maste 		case 13: return "R_ARM_SWI24";
11742c23cb7cSEd Maste 		case 14: return "R_ARM_THM_SWI8";
11752c23cb7cSEd Maste 		case 15: return "R_ARM_XPC25";
11762c23cb7cSEd Maste 		case 16: return "R_ARM_THM_XPC22";
11772c23cb7cSEd Maste 		case 20: return "R_ARM_COPY";
11782c23cb7cSEd Maste 		case 21: return "R_ARM_GLOB_DAT";
11792c23cb7cSEd Maste 		case 22: return "R_ARM_JUMP_SLOT";
11802c23cb7cSEd Maste 		case 23: return "R_ARM_RELATIVE";
11812c23cb7cSEd Maste 		case 24: return "R_ARM_GOTOFF";
11822c23cb7cSEd Maste 		case 25: return "R_ARM_GOTPC";
11832c23cb7cSEd Maste 		case 26: return "R_ARM_GOT32";
11842c23cb7cSEd Maste 		case 27: return "R_ARM_PLT32";
11852c23cb7cSEd Maste 		case 100: return "R_ARM_GNU_VTENTRY";
11862c23cb7cSEd Maste 		case 101: return "R_ARM_GNU_VTINHERIT";
11872c23cb7cSEd Maste 		case 250: return "R_ARM_RSBREL32";
11882c23cb7cSEd Maste 		case 251: return "R_ARM_THM_RPC22";
11892c23cb7cSEd Maste 		case 252: return "R_ARM_RREL32";
11902c23cb7cSEd Maste 		case 253: return "R_ARM_RABS32";
11912c23cb7cSEd Maste 		case 254: return "R_ARM_RPC24";
11922c23cb7cSEd Maste 		case 255: return "R_ARM_RBASE";
11932c23cb7cSEd Maste 		default: return "";
11942c23cb7cSEd Maste 		}
11952c23cb7cSEd Maste 	case EM_IA_64:
11962c23cb7cSEd Maste 		switch(type) {
11972c23cb7cSEd Maste 		case 0: return "R_IA_64_NONE";
11982c23cb7cSEd Maste 		case 33: return "R_IA_64_IMM14";
11992c23cb7cSEd Maste 		case 34: return "R_IA_64_IMM22";
12002c23cb7cSEd Maste 		case 35: return "R_IA_64_IMM64";
12012c23cb7cSEd Maste 		case 36: return "R_IA_64_DIR32MSB";
12022c23cb7cSEd Maste 		case 37: return "R_IA_64_DIR32LSB";
12032c23cb7cSEd Maste 		case 38: return "R_IA_64_DIR64MSB";
12042c23cb7cSEd Maste 		case 39: return "R_IA_64_DIR64LSB";
12052c23cb7cSEd Maste 		case 42: return "R_IA_64_GPREL22";
12062c23cb7cSEd Maste 		case 43: return "R_IA_64_GPREL64I";
12072c23cb7cSEd Maste 		case 44: return "R_IA_64_GPREL32MSB";
12082c23cb7cSEd Maste 		case 45: return "R_IA_64_GPREL32LSB";
12092c23cb7cSEd Maste 		case 46: return "R_IA_64_GPREL64MSB";
12102c23cb7cSEd Maste 		case 47: return "R_IA_64_GPREL64LSB";
12112c23cb7cSEd Maste 		case 50: return "R_IA_64_LTOFF22";
12122c23cb7cSEd Maste 		case 51: return "R_IA_64_LTOFF64I";
12132c23cb7cSEd Maste 		case 58: return "R_IA_64_PLTOFF22";
12142c23cb7cSEd Maste 		case 59: return "R_IA_64_PLTOFF64I";
12152c23cb7cSEd Maste 		case 62: return "R_IA_64_PLTOFF64MSB";
12162c23cb7cSEd Maste 		case 63: return "R_IA_64_PLTOFF64LSB";
12172c23cb7cSEd Maste 		case 67: return "R_IA_64_FPTR64I";
12182c23cb7cSEd Maste 		case 68: return "R_IA_64_FPTR32MSB";
12192c23cb7cSEd Maste 		case 69: return "R_IA_64_FPTR32LSB";
12202c23cb7cSEd Maste 		case 70: return "R_IA_64_FPTR64MSB";
12212c23cb7cSEd Maste 		case 71: return "R_IA_64_FPTR64LSB";
12222c23cb7cSEd Maste 		case 72: return "R_IA_64_PCREL60B";
12232c23cb7cSEd Maste 		case 73: return "R_IA_64_PCREL21B";
12242c23cb7cSEd Maste 		case 74: return "R_IA_64_PCREL21M";
12252c23cb7cSEd Maste 		case 75: return "R_IA_64_PCREL21F";
12262c23cb7cSEd Maste 		case 76: return "R_IA_64_PCREL32MSB";
12272c23cb7cSEd Maste 		case 77: return "R_IA_64_PCREL32LSB";
12282c23cb7cSEd Maste 		case 78: return "R_IA_64_PCREL64MSB";
12292c23cb7cSEd Maste 		case 79: return "R_IA_64_PCREL64LSB";
12302c23cb7cSEd Maste 		case 82: return "R_IA_64_LTOFF_FPTR22";
12312c23cb7cSEd Maste 		case 83: return "R_IA_64_LTOFF_FPTR64I";
12322c23cb7cSEd Maste 		case 84: return "R_IA_64_LTOFF_FPTR32MSB";
12332c23cb7cSEd Maste 		case 85: return "R_IA_64_LTOFF_FPTR32LSB";
12342c23cb7cSEd Maste 		case 86: return "R_IA_64_LTOFF_FPTR64MSB";
12352c23cb7cSEd Maste 		case 87: return "R_IA_64_LTOFF_FPTR64LSB";
12362c23cb7cSEd Maste 		case 92: return "R_IA_64_SEGREL32MSB";
12372c23cb7cSEd Maste 		case 93: return "R_IA_64_SEGREL32LSB";
12382c23cb7cSEd Maste 		case 94: return "R_IA_64_SEGREL64MSB";
12392c23cb7cSEd Maste 		case 95: return "R_IA_64_SEGREL64LSB";
12402c23cb7cSEd Maste 		case 100: return "R_IA_64_SECREL32MSB";
12412c23cb7cSEd Maste 		case 101: return "R_IA_64_SECREL32LSB";
12422c23cb7cSEd Maste 		case 102: return "R_IA_64_SECREL64MSB";
12432c23cb7cSEd Maste 		case 103: return "R_IA_64_SECREL64LSB";
12442c23cb7cSEd Maste 		case 108: return "R_IA_64_REL32MSB";
12452c23cb7cSEd Maste 		case 109: return "R_IA_64_REL32LSB";
12462c23cb7cSEd Maste 		case 110: return "R_IA_64_REL64MSB";
12472c23cb7cSEd Maste 		case 111: return "R_IA_64_REL64LSB";
12482c23cb7cSEd Maste 		case 116: return "R_IA_64_LTV32MSB";
12492c23cb7cSEd Maste 		case 117: return "R_IA_64_LTV32LSB";
12502c23cb7cSEd Maste 		case 118: return "R_IA_64_LTV64MSB";
12512c23cb7cSEd Maste 		case 119: return "R_IA_64_LTV64LSB";
12522c23cb7cSEd Maste 		case 121: return "R_IA_64_PCREL21BI";
12532c23cb7cSEd Maste 		case 122: return "R_IA_64_PCREL22";
12542c23cb7cSEd Maste 		case 123: return "R_IA_64_PCREL64I";
12552c23cb7cSEd Maste 		case 128: return "R_IA_64_IPLTMSB";
12562c23cb7cSEd Maste 		case 129: return "R_IA_64_IPLTLSB";
12572c23cb7cSEd Maste 		case 133: return "R_IA_64_SUB";
12582c23cb7cSEd Maste 		case 134: return "R_IA_64_LTOFF22X";
12592c23cb7cSEd Maste 		case 135: return "R_IA_64_LDXMOV";
12602c23cb7cSEd Maste 		case 145: return "R_IA_64_TPREL14";
12612c23cb7cSEd Maste 		case 146: return "R_IA_64_TPREL22";
12622c23cb7cSEd Maste 		case 147: return "R_IA_64_TPREL64I";
12632c23cb7cSEd Maste 		case 150: return "R_IA_64_TPREL64MSB";
12642c23cb7cSEd Maste 		case 151: return "R_IA_64_TPREL64LSB";
12652c23cb7cSEd Maste 		case 154: return "R_IA_64_LTOFF_TPREL22";
12662c23cb7cSEd Maste 		case 166: return "R_IA_64_DTPMOD64MSB";
12672c23cb7cSEd Maste 		case 167: return "R_IA_64_DTPMOD64LSB";
12682c23cb7cSEd Maste 		case 170: return "R_IA_64_LTOFF_DTPMOD22";
12692c23cb7cSEd Maste 		case 177: return "R_IA_64_DTPREL14";
12702c23cb7cSEd Maste 		case 178: return "R_IA_64_DTPREL22";
12712c23cb7cSEd Maste 		case 179: return "R_IA_64_DTPREL64I";
12722c23cb7cSEd Maste 		case 180: return "R_IA_64_DTPREL32MSB";
12732c23cb7cSEd Maste 		case 181: return "R_IA_64_DTPREL32LSB";
12742c23cb7cSEd Maste 		case 182: return "R_IA_64_DTPREL64MSB";
12752c23cb7cSEd Maste 		case 183: return "R_IA_64_DTPREL64LSB";
12762c23cb7cSEd Maste 		case 186: return "R_IA_64_LTOFF_DTPREL22";
12772c23cb7cSEd Maste 		default: return "";
12782c23cb7cSEd Maste 		}
12792c23cb7cSEd Maste 	case EM_MIPS:
12802c23cb7cSEd Maste 		switch(type) {
12812c23cb7cSEd Maste 		case 0: return "R_MIPS_NONE";
12822c23cb7cSEd Maste 		case 1: return "R_MIPS_16";
12832c23cb7cSEd Maste 		case 2: return "R_MIPS_32";
12842c23cb7cSEd Maste 		case 3: return "R_MIPS_REL32";
12852c23cb7cSEd Maste 		case 4: return "R_MIPS_26";
12862c23cb7cSEd Maste 		case 5: return "R_MIPS_HI16";
12872c23cb7cSEd Maste 		case 6: return "R_MIPS_LO16";
12882c23cb7cSEd Maste 		case 7: return "R_MIPS_GPREL16";
12892c23cb7cSEd Maste 		case 8: return "R_MIPS_LITERAL";
12902c23cb7cSEd Maste 		case 9: return "R_MIPS_GOT16";
12912c23cb7cSEd Maste 		case 10: return "R_MIPS_PC16";
12922c23cb7cSEd Maste 		case 11: return "R_MIPS_CALL16";
12932c23cb7cSEd Maste 		case 12: return "R_MIPS_GPREL32";
12942c23cb7cSEd Maste 		case 21: return "R_MIPS_GOTHI16";
12952c23cb7cSEd Maste 		case 22: return "R_MIPS_GOTLO16";
12962c23cb7cSEd Maste 		case 30: return "R_MIPS_CALLHI16";
12972c23cb7cSEd Maste 		case 31: return "R_MIPS_CALLLO16";
12982c23cb7cSEd Maste 		default: return "";
12992c23cb7cSEd Maste 		}
13002c23cb7cSEd Maste 	case EM_PPC:
13012c23cb7cSEd Maste 		switch(type) {
13022c23cb7cSEd Maste 		case 0: return "R_PPC_NONE";
13032c23cb7cSEd Maste 		case 1: return "R_PPC_ADDR32";
13042c23cb7cSEd Maste 		case 2: return "R_PPC_ADDR24";
13052c23cb7cSEd Maste 		case 3: return "R_PPC_ADDR16";
13062c23cb7cSEd Maste 		case 4: return "R_PPC_ADDR16_LO";
13072c23cb7cSEd Maste 		case 5: return "R_PPC_ADDR16_HI";
13082c23cb7cSEd Maste 		case 6: return "R_PPC_ADDR16_HA";
13092c23cb7cSEd Maste 		case 7: return "R_PPC_ADDR14";
13102c23cb7cSEd Maste 		case 8: return "R_PPC_ADDR14_BRTAKEN";
13112c23cb7cSEd Maste 		case 9: return "R_PPC_ADDR14_BRNTAKEN";
13122c23cb7cSEd Maste 		case 10: return "R_PPC_REL24";
13132c23cb7cSEd Maste 		case 11: return "R_PPC_REL14";
13142c23cb7cSEd Maste 		case 12: return "R_PPC_REL14_BRTAKEN";
13152c23cb7cSEd Maste 		case 13: return "R_PPC_REL14_BRNTAKEN";
13162c23cb7cSEd Maste 		case 14: return "R_PPC_GOT16";
13172c23cb7cSEd Maste 		case 15: return "R_PPC_GOT16_LO";
13182c23cb7cSEd Maste 		case 16: return "R_PPC_GOT16_HI";
13192c23cb7cSEd Maste 		case 17: return "R_PPC_GOT16_HA";
13202c23cb7cSEd Maste 		case 18: return "R_PPC_PLTREL24";
13212c23cb7cSEd Maste 		case 19: return "R_PPC_COPY";
13222c23cb7cSEd Maste 		case 20: return "R_PPC_GLOB_DAT";
13232c23cb7cSEd Maste 		case 21: return "R_PPC_JMP_SLOT";
13242c23cb7cSEd Maste 		case 22: return "R_PPC_RELATIVE";
13252c23cb7cSEd Maste 		case 23: return "R_PPC_LOCAL24PC";
13262c23cb7cSEd Maste 		case 24: return "R_PPC_UADDR32";
13272c23cb7cSEd Maste 		case 25: return "R_PPC_UADDR16";
13282c23cb7cSEd Maste 		case 26: return "R_PPC_REL32";
13292c23cb7cSEd Maste 		case 27: return "R_PPC_PLT32";
13302c23cb7cSEd Maste 		case 28: return "R_PPC_PLTREL32";
13312c23cb7cSEd Maste 		case 29: return "R_PPC_PLT16_LO";
13322c23cb7cSEd Maste 		case 30: return "R_PPC_PLT16_HI";
13332c23cb7cSEd Maste 		case 31: return "R_PPC_PLT16_HA";
13342c23cb7cSEd Maste 		case 32: return "R_PPC_SDAREL16";
13352c23cb7cSEd Maste 		case 33: return "R_PPC_SECTOFF";
13362c23cb7cSEd Maste 		case 34: return "R_PPC_SECTOFF_LO";
13372c23cb7cSEd Maste 		case 35: return "R_PPC_SECTOFF_HI";
13382c23cb7cSEd Maste 		case 36: return "R_PPC_SECTOFF_HA";
13392c23cb7cSEd Maste 		case 67: return "R_PPC_TLS";
13402c23cb7cSEd Maste 		case 68: return "R_PPC_DTPMOD32";
13412c23cb7cSEd Maste 		case 69: return "R_PPC_TPREL16";
13422c23cb7cSEd Maste 		case 70: return "R_PPC_TPREL16_LO";
13432c23cb7cSEd Maste 		case 71: return "R_PPC_TPREL16_HI";
13442c23cb7cSEd Maste 		case 72: return "R_PPC_TPREL16_HA";
13452c23cb7cSEd Maste 		case 73: return "R_PPC_TPREL32";
13462c23cb7cSEd Maste 		case 74: return "R_PPC_DTPREL16";
13472c23cb7cSEd Maste 		case 75: return "R_PPC_DTPREL16_LO";
13482c23cb7cSEd Maste 		case 76: return "R_PPC_DTPREL16_HI";
13492c23cb7cSEd Maste 		case 77: return "R_PPC_DTPREL16_HA";
13502c23cb7cSEd Maste 		case 78: return "R_PPC_DTPREL32";
13512c23cb7cSEd Maste 		case 79: return "R_PPC_GOT_TLSGD16";
13522c23cb7cSEd Maste 		case 80: return "R_PPC_GOT_TLSGD16_LO";
13532c23cb7cSEd Maste 		case 81: return "R_PPC_GOT_TLSGD16_HI";
13542c23cb7cSEd Maste 		case 82: return "R_PPC_GOT_TLSGD16_HA";
13552c23cb7cSEd Maste 		case 83: return "R_PPC_GOT_TLSLD16";
13562c23cb7cSEd Maste 		case 84: return "R_PPC_GOT_TLSLD16_LO";
13572c23cb7cSEd Maste 		case 85: return "R_PPC_GOT_TLSLD16_HI";
13582c23cb7cSEd Maste 		case 86: return "R_PPC_GOT_TLSLD16_HA";
13592c23cb7cSEd Maste 		case 87: return "R_PPC_GOT_TPREL16";
13602c23cb7cSEd Maste 		case 88: return "R_PPC_GOT_TPREL16_LO";
13612c23cb7cSEd Maste 		case 89: return "R_PPC_GOT_TPREL16_HI";
13622c23cb7cSEd Maste 		case 90: return "R_PPC_GOT_TPREL16_HA";
13632c23cb7cSEd Maste 		case 101: return "R_PPC_EMB_NADDR32";
13642c23cb7cSEd Maste 		case 102: return "R_PPC_EMB_NADDR16";
13652c23cb7cSEd Maste 		case 103: return "R_PPC_EMB_NADDR16_LO";
13662c23cb7cSEd Maste 		case 104: return "R_PPC_EMB_NADDR16_HI";
13672c23cb7cSEd Maste 		case 105: return "R_PPC_EMB_NADDR16_HA";
13682c23cb7cSEd Maste 		case 106: return "R_PPC_EMB_SDAI16";
13692c23cb7cSEd Maste 		case 107: return "R_PPC_EMB_SDA2I16";
13702c23cb7cSEd Maste 		case 108: return "R_PPC_EMB_SDA2REL";
13712c23cb7cSEd Maste 		case 109: return "R_PPC_EMB_SDA21";
13722c23cb7cSEd Maste 		case 110: return "R_PPC_EMB_MRKREF";
13732c23cb7cSEd Maste 		case 111: return "R_PPC_EMB_RELSEC16";
13742c23cb7cSEd Maste 		case 112: return "R_PPC_EMB_RELST_LO";
13752c23cb7cSEd Maste 		case 113: return "R_PPC_EMB_RELST_HI";
13762c23cb7cSEd Maste 		case 114: return "R_PPC_EMB_RELST_HA";
13772c23cb7cSEd Maste 		case 115: return "R_PPC_EMB_BIT_FLD";
13782c23cb7cSEd Maste 		case 116: return "R_PPC_EMB_RELSDA";
13792c23cb7cSEd Maste 		default: return "";
13802c23cb7cSEd Maste 		}
13812c23cb7cSEd Maste 	case EM_SPARC:
13822c23cb7cSEd Maste 	case EM_SPARCV9:
13832c23cb7cSEd Maste 		switch(type) {
13842c23cb7cSEd Maste 		case 0: return "R_SPARC_NONE";
13852c23cb7cSEd Maste 		case 1: return "R_SPARC_8";
13862c23cb7cSEd Maste 		case 2: return "R_SPARC_16";
13872c23cb7cSEd Maste 		case 3: return "R_SPARC_32";
13882c23cb7cSEd Maste 		case 4: return "R_SPARC_DISP8";
13892c23cb7cSEd Maste 		case 5: return "R_SPARC_DISP16";
13902c23cb7cSEd Maste 		case 6: return "R_SPARC_DISP32";
13912c23cb7cSEd Maste 		case 7: return "R_SPARC_WDISP30";
13922c23cb7cSEd Maste 		case 8: return "R_SPARC_WDISP22";
13932c23cb7cSEd Maste 		case 9: return "R_SPARC_HI22";
13942c23cb7cSEd Maste 		case 10: return "R_SPARC_22";
13952c23cb7cSEd Maste 		case 11: return "R_SPARC_13";
13962c23cb7cSEd Maste 		case 12: return "R_SPARC_LO10";
13972c23cb7cSEd Maste 		case 13: return "R_SPARC_GOT10";
13982c23cb7cSEd Maste 		case 14: return "R_SPARC_GOT13";
13992c23cb7cSEd Maste 		case 15: return "R_SPARC_GOT22";
14002c23cb7cSEd Maste 		case 16: return "R_SPARC_PC10";
14012c23cb7cSEd Maste 		case 17: return "R_SPARC_PC22";
14022c23cb7cSEd Maste 		case 18: return "R_SPARC_WPLT30";
14032c23cb7cSEd Maste 		case 19: return "R_SPARC_COPY";
14042c23cb7cSEd Maste 		case 20: return "R_SPARC_GLOB_DAT";
14052c23cb7cSEd Maste 		case 21: return "R_SPARC_JMP_SLOT";
14062c23cb7cSEd Maste 		case 22: return "R_SPARC_RELATIVE";
14072c23cb7cSEd Maste 		case 23: return "R_SPARC_UA32";
14082c23cb7cSEd Maste 		case 24: return "R_SPARC_PLT32";
14092c23cb7cSEd Maste 		case 25: return "R_SPARC_HIPLT22";
14102c23cb7cSEd Maste 		case 26: return "R_SPARC_LOPLT10";
14112c23cb7cSEd Maste 		case 27: return "R_SPARC_PCPLT32";
14122c23cb7cSEd Maste 		case 28: return "R_SPARC_PCPLT22";
14132c23cb7cSEd Maste 		case 29: return "R_SPARC_PCPLT10";
14142c23cb7cSEd Maste 		case 30: return "R_SPARC_10";
14152c23cb7cSEd Maste 		case 31: return "R_SPARC_11";
14162c23cb7cSEd Maste 		case 32: return "R_SPARC_64";
14172c23cb7cSEd Maste 		case 33: return "R_SPARC_OLO10";
14182c23cb7cSEd Maste 		case 34: return "R_SPARC_HH22";
14192c23cb7cSEd Maste 		case 35: return "R_SPARC_HM10";
14202c23cb7cSEd Maste 		case 36: return "R_SPARC_LM22";
14212c23cb7cSEd Maste 		case 37: return "R_SPARC_PC_HH22";
14222c23cb7cSEd Maste 		case 38: return "R_SPARC_PC_HM10";
14232c23cb7cSEd Maste 		case 39: return "R_SPARC_PC_LM22";
14242c23cb7cSEd Maste 		case 40: return "R_SPARC_WDISP16";
14252c23cb7cSEd Maste 		case 41: return "R_SPARC_WDISP19";
14262c23cb7cSEd Maste 		case 42: return "R_SPARC_GLOB_JMP";
14272c23cb7cSEd Maste 		case 43: return "R_SPARC_7";
14282c23cb7cSEd Maste 		case 44: return "R_SPARC_5";
14292c23cb7cSEd Maste 		case 45: return "R_SPARC_6";
14302c23cb7cSEd Maste 		case 46: return "R_SPARC_DISP64";
14312c23cb7cSEd Maste 		case 47: return "R_SPARC_PLT64";
14322c23cb7cSEd Maste 		case 48: return "R_SPARC_HIX22";
14332c23cb7cSEd Maste 		case 49: return "R_SPARC_LOX10";
14342c23cb7cSEd Maste 		case 50: return "R_SPARC_H44";
14352c23cb7cSEd Maste 		case 51: return "R_SPARC_M44";
14362c23cb7cSEd Maste 		case 52: return "R_SPARC_L44";
14372c23cb7cSEd Maste 		case 53: return "R_SPARC_REGISTER";
14382c23cb7cSEd Maste 		case 54: return "R_SPARC_UA64";
14392c23cb7cSEd Maste 		case 55: return "R_SPARC_UA16";
14402c23cb7cSEd Maste 		case 56: return "R_SPARC_TLS_GD_HI22";
14412c23cb7cSEd Maste 		case 57: return "R_SPARC_TLS_GD_LO10";
14422c23cb7cSEd Maste 		case 58: return "R_SPARC_TLS_GD_ADD";
14432c23cb7cSEd Maste 		case 59: return "R_SPARC_TLS_GD_CALL";
14442c23cb7cSEd Maste 		case 60: return "R_SPARC_TLS_LDM_HI22";
14452c23cb7cSEd Maste 		case 61: return "R_SPARC_TLS_LDM_LO10";
14462c23cb7cSEd Maste 		case 62: return "R_SPARC_TLS_LDM_ADD";
14472c23cb7cSEd Maste 		case 63: return "R_SPARC_TLS_LDM_CALL";
14482c23cb7cSEd Maste 		case 64: return "R_SPARC_TLS_LDO_HIX22";
14492c23cb7cSEd Maste 		case 65: return "R_SPARC_TLS_LDO_LOX10";
14502c23cb7cSEd Maste 		case 66: return "R_SPARC_TLS_LDO_ADD";
14512c23cb7cSEd Maste 		case 67: return "R_SPARC_TLS_IE_HI22";
14522c23cb7cSEd Maste 		case 68: return "R_SPARC_TLS_IE_LO10";
14532c23cb7cSEd Maste 		case 69: return "R_SPARC_TLS_IE_LD";
14542c23cb7cSEd Maste 		case 70: return "R_SPARC_TLS_IE_LDX";
14552c23cb7cSEd Maste 		case 71: return "R_SPARC_TLS_IE_ADD";
14562c23cb7cSEd Maste 		case 72: return "R_SPARC_TLS_LE_HIX22";
14572c23cb7cSEd Maste 		case 73: return "R_SPARC_TLS_LE_LOX10";
14582c23cb7cSEd Maste 		case 74: return "R_SPARC_TLS_DTPMOD32";
14592c23cb7cSEd Maste 		case 75: return "R_SPARC_TLS_DTPMOD64";
14602c23cb7cSEd Maste 		case 76: return "R_SPARC_TLS_DTPOFF32";
14612c23cb7cSEd Maste 		case 77: return "R_SPARC_TLS_DTPOFF64";
14622c23cb7cSEd Maste 		case 78: return "R_SPARC_TLS_TPOFF32";
14632c23cb7cSEd Maste 		case 79: return "R_SPARC_TLS_TPOFF64";
14642c23cb7cSEd Maste 		default: return "";
14652c23cb7cSEd Maste 		}
14662c23cb7cSEd Maste 	case EM_X86_64:
14672c23cb7cSEd Maste 		switch(type) {
14682c23cb7cSEd Maste 		case 0: return "R_X86_64_NONE";
14692c23cb7cSEd Maste 		case 1: return "R_X86_64_64";
14702c23cb7cSEd Maste 		case 2: return "R_X86_64_PC32";
14712c23cb7cSEd Maste 		case 3: return "R_X86_64_GOT32";
14722c23cb7cSEd Maste 		case 4: return "R_X86_64_PLT32";
14732c23cb7cSEd Maste 		case 5: return "R_X86_64_COPY";
14742c23cb7cSEd Maste 		case 6: return "R_X86_64_GLOB_DAT";
14752c23cb7cSEd Maste 		case 7: return "R_X86_64_JMP_SLOT";
14762c23cb7cSEd Maste 		case 8: return "R_X86_64_RELATIVE";
14772c23cb7cSEd Maste 		case 9: return "R_X86_64_GOTPCREL";
14782c23cb7cSEd Maste 		case 10: return "R_X86_64_32";
14792c23cb7cSEd Maste 		case 11: return "R_X86_64_32S";
14802c23cb7cSEd Maste 		case 12: return "R_X86_64_16";
14812c23cb7cSEd Maste 		case 13: return "R_X86_64_PC16";
14822c23cb7cSEd Maste 		case 14: return "R_X86_64_8";
14832c23cb7cSEd Maste 		case 15: return "R_X86_64_PC8";
14842c23cb7cSEd Maste 		case 16: return "R_X86_64_DTPMOD64";
14852c23cb7cSEd Maste 		case 17: return "R_X86_64_DTPOFF64";
14862c23cb7cSEd Maste 		case 18: return "R_X86_64_TPOFF64";
14872c23cb7cSEd Maste 		case 19: return "R_X86_64_TLSGD";
14882c23cb7cSEd Maste 		case 20: return "R_X86_64_TLSLD";
14892c23cb7cSEd Maste 		case 21: return "R_X86_64_DTPOFF32";
14902c23cb7cSEd Maste 		case 22: return "R_X86_64_GOTTPOFF";
14912c23cb7cSEd Maste 		case 23: return "R_X86_64_TPOFF32";
149257736250SEd Maste 		case 24: return "R_X86_64_PC64";
149357736250SEd Maste 		case 25: return "R_X86_64_GOTOFF64";
149457736250SEd Maste 		case 26: return "R_X86_64_GOTPC32";
149557736250SEd Maste 		case 27: return "R_X86_64_GOT64";
149657736250SEd Maste 		case 28: return "R_X86_64_GOTPCREL64";
149757736250SEd Maste 		case 29: return "R_X86_64_GOTPC64";
149857736250SEd Maste 		case 30: return "R_X86_64_GOTPLT64";
149957736250SEd Maste 		case 31: return "R_X86_64_PLTOFF64";
150057736250SEd Maste 		case 32: return "R_X86_64_SIZE32";
150157736250SEd Maste 		case 33: return "R_X86_64_SIZE64";
150257736250SEd Maste 		case 34: return "R_X86_64_GOTPC32_TLSDESC";
150357736250SEd Maste 		case 35: return "R_X86_64_TLSDESC_CALL";
150457736250SEd Maste 		case 36: return "R_X86_64_TLSDESC";
150557736250SEd Maste 		case 37: return "R_X86_64_IRELATIVE";
15062c23cb7cSEd Maste 		default: return "";
15072c23cb7cSEd Maste 		}
15082c23cb7cSEd Maste 	default: return "";
15092c23cb7cSEd Maste 	}
15102c23cb7cSEd Maste }
15112c23cb7cSEd Maste 
15122c23cb7cSEd Maste static const char *
151302b08c90SEd Maste note_type(const char *name, unsigned int et, unsigned int nt)
151402b08c90SEd Maste {
151571a0c925SEd Maste 	if ((strcmp(name, "CORE") == 0 || strcmp(name, "LINUX") == 0) &&
151671a0c925SEd Maste 	    et == ET_CORE)
151702b08c90SEd Maste 		return note_type_linux_core(nt);
151802b08c90SEd Maste 	else if (strcmp(name, "FreeBSD") == 0)
151902b08c90SEd Maste 		if (et == ET_CORE)
152002b08c90SEd Maste 			return note_type_freebsd_core(nt);
152102b08c90SEd Maste 		else
152202b08c90SEd Maste 			return note_type_freebsd(nt);
152302b08c90SEd Maste 	else if (strcmp(name, "GNU") == 0 && et != ET_CORE)
152402b08c90SEd Maste 		return note_type_gnu(nt);
152502b08c90SEd Maste 	else if (strcmp(name, "NetBSD") == 0 && et != ET_CORE)
152602b08c90SEd Maste 		return note_type_netbsd(nt);
152702b08c90SEd Maste 	else if (strcmp(name, "OpenBSD") == 0 && et != ET_CORE)
152802b08c90SEd Maste 		return note_type_openbsd(nt);
152902b08c90SEd Maste 	return note_type_unknown(nt);
153002b08c90SEd Maste }
153102b08c90SEd Maste 
153202b08c90SEd Maste static const char *
153302b08c90SEd Maste note_type_freebsd(unsigned int nt)
153402b08c90SEd Maste {
153502b08c90SEd Maste 	switch (nt) {
153602b08c90SEd Maste 	case 1: return "NT_FREEBSD_ABI_TAG";
153702b08c90SEd Maste 	case 2: return "NT_FREEBSD_NOINIT_TAG";
153802b08c90SEd Maste 	case 3: return "NT_FREEBSD_ARCH_TAG";
153902b08c90SEd Maste 	default: return (note_type_unknown(nt));
154002b08c90SEd Maste 	}
154102b08c90SEd Maste }
154202b08c90SEd Maste 
154302b08c90SEd Maste static const char *
154402b08c90SEd Maste note_type_freebsd_core(unsigned int nt)
154502b08c90SEd Maste {
154602b08c90SEd Maste 	switch (nt) {
154702b08c90SEd Maste 	case 1: return "NT_PRSTATUS";
154802b08c90SEd Maste 	case 2: return "NT_FPREGSET";
154902b08c90SEd Maste 	case 3: return "NT_PRPSINFO";
155002b08c90SEd Maste 	case 7: return "NT_THRMISC";
155102b08c90SEd Maste 	case 8: return "NT_PROCSTAT_PROC";
155202b08c90SEd Maste 	case 9: return "NT_PROCSTAT_FILES";
155302b08c90SEd Maste 	case 10: return "NT_PROCSTAT_VMMAP";
155402b08c90SEd Maste 	case 11: return "NT_PROCSTAT_GROUPS";
155502b08c90SEd Maste 	case 12: return "NT_PROCSTAT_UMASK";
155602b08c90SEd Maste 	case 13: return "NT_PROCSTAT_RLIMIT";
155702b08c90SEd Maste 	case 14: return "NT_PROCSTAT_OSREL";
155802b08c90SEd Maste 	case 15: return "NT_PROCSTAT_PSSTRINGS";
155902b08c90SEd Maste 	case 16: return "NT_PROCSTAT_AUXV";
156002b08c90SEd Maste 	case 0x202: return "NT_X86_XSTATE (x86 XSAVE extended state)";
156102b08c90SEd Maste 	default: return (note_type_unknown(nt));
156202b08c90SEd Maste 	}
156302b08c90SEd Maste }
156402b08c90SEd Maste 
156502b08c90SEd Maste static const char *
156602b08c90SEd Maste note_type_linux_core(unsigned int nt)
156702b08c90SEd Maste {
156802b08c90SEd Maste 	switch (nt) {
156902b08c90SEd Maste 	case 1: return "NT_PRSTATUS (Process status)";
157002b08c90SEd Maste 	case 2: return "NT_FPREGSET (Floating point information)";
157102b08c90SEd Maste 	case 3: return "NT_PRPSINFO (Process information)";
157271a0c925SEd Maste 	case 4: return "NT_TASKSTRUCT (Task structure)";
157302b08c90SEd Maste 	case 6: return "NT_AUXV (Auxiliary vector)";
157402b08c90SEd Maste 	case 10: return "NT_PSTATUS (Linux process status)";
157502b08c90SEd Maste 	case 12: return "NT_FPREGS (Linux floating point regset)";
157602b08c90SEd Maste 	case 13: return "NT_PSINFO (Linux process information)";
157702b08c90SEd Maste 	case 16: return "NT_LWPSTATUS (Linux lwpstatus_t type)";
157802b08c90SEd Maste 	case 17: return "NT_LWPSINFO (Linux lwpinfo_t type)";
157971a0c925SEd Maste 	case 18: return "NT_WIN32PSTATUS (win32_pstatus structure)";
158071a0c925SEd Maste 	case 0x100: return "NT_PPC_VMX (ppc Altivec registers)";
158171a0c925SEd Maste 	case 0x102: return "NT_PPC_VSX (ppc VSX registers)";
158271a0c925SEd Maste 	case 0x202: return "NT_X86_XSTATE (x86 XSAVE extended state)";
158371a0c925SEd Maste 	case 0x300: return "NT_S390_HIGH_GPRS (s390 upper register halves)";
158471a0c925SEd Maste 	case 0x301: return "NT_S390_TIMER (s390 timer register)";
158571a0c925SEd Maste 	case 0x302: return "NT_S390_TODCMP (s390 TOD comparator register)";
158671a0c925SEd Maste 	case 0x303: return "NT_S390_TODPREG (s390 TOD programmable register)";
158771a0c925SEd Maste 	case 0x304: return "NT_S390_CTRS (s390 control registers)";
158871a0c925SEd Maste 	case 0x305: return "NT_S390_PREFIX (s390 prefix register)";
158971a0c925SEd Maste 	case 0x400: return "NT_ARM_VFP (arm VFP registers)";
159071a0c925SEd Maste 	case 0x46494c45UL: return "NT_FILE (mapped files)";
159171a0c925SEd Maste 	case 0x46E62B7FUL: return "NT_PRXFPREG (Linux user_xfpregs structure)";
159271a0c925SEd Maste 	case 0x53494749UL: return "NT_SIGINFO (siginfo_t data)";
159302b08c90SEd Maste 	default: return (note_type_unknown(nt));
159402b08c90SEd Maste 	}
159502b08c90SEd Maste }
159602b08c90SEd Maste 
159702b08c90SEd Maste static const char *
159802b08c90SEd Maste note_type_gnu(unsigned int nt)
159902b08c90SEd Maste {
160002b08c90SEd Maste 	switch (nt) {
160102b08c90SEd Maste 	case 1: return "NT_GNU_ABI_TAG";
160202b08c90SEd Maste 	case 2: return "NT_GNU_HWCAP (Hardware capabilities)";
160302b08c90SEd Maste 	case 3: return "NT_GNU_BUILD_ID (Build id set by ld(1))";
160402b08c90SEd Maste 	case 4: return "NT_GNU_GOLD_VERSION (GNU gold version)";
160502b08c90SEd Maste 	default: return (note_type_unknown(nt));
160602b08c90SEd Maste 	}
160702b08c90SEd Maste }
160802b08c90SEd Maste 
160902b08c90SEd Maste static const char *
161002b08c90SEd Maste note_type_netbsd(unsigned int nt)
161102b08c90SEd Maste {
161202b08c90SEd Maste 	switch (nt) {
161302b08c90SEd Maste 	case 1: return "NT_NETBSD_IDENT";
161402b08c90SEd Maste 	default: return (note_type_unknown(nt));
161502b08c90SEd Maste 	}
161602b08c90SEd Maste }
161702b08c90SEd Maste 
161802b08c90SEd Maste static const char *
161902b08c90SEd Maste note_type_openbsd(unsigned int nt)
162002b08c90SEd Maste {
162102b08c90SEd Maste 	switch (nt) {
162202b08c90SEd Maste 	case 1: return "NT_OPENBSD_IDENT";
162302b08c90SEd Maste 	default: return (note_type_unknown(nt));
162402b08c90SEd Maste 	}
162502b08c90SEd Maste }
162602b08c90SEd Maste 
162702b08c90SEd Maste static const char *
162802b08c90SEd Maste note_type_unknown(unsigned int nt)
16292c23cb7cSEd Maste {
16302c23cb7cSEd Maste 	static char s_nt[32];
16312c23cb7cSEd Maste 
163271a0c925SEd Maste 	snprintf(s_nt, sizeof(s_nt),
163371a0c925SEd Maste 	    nt >= 0x100 ? "<unknown: 0x%x>" : "<unknown: %u>", nt);
16342c23cb7cSEd Maste 	return (s_nt);
16352c23cb7cSEd Maste }
16362c23cb7cSEd Maste 
16372c23cb7cSEd Maste static struct {
16382c23cb7cSEd Maste 	const char *name;
16392c23cb7cSEd Maste 	int value;
16402c23cb7cSEd Maste } l_flag[] = {
16412c23cb7cSEd Maste 	{"EXACT_MATCH", LL_EXACT_MATCH},
16422c23cb7cSEd Maste 	{"IGNORE_INT_VER", LL_IGNORE_INT_VER},
16432c23cb7cSEd Maste 	{"REQUIRE_MINOR", LL_REQUIRE_MINOR},
16442c23cb7cSEd Maste 	{"EXPORTS", LL_EXPORTS},
16452c23cb7cSEd Maste 	{"DELAY_LOAD", LL_DELAY_LOAD},
16462c23cb7cSEd Maste 	{"DELTA", LL_DELTA},
16472c23cb7cSEd Maste 	{NULL, 0}
16482c23cb7cSEd Maste };
16492c23cb7cSEd Maste 
16502c23cb7cSEd Maste static struct mips_option mips_exceptions_option[] = {
16512c23cb7cSEd Maste 	{OEX_PAGE0, "PAGE0"},
16522c23cb7cSEd Maste 	{OEX_SMM, "SMM"},
16532c23cb7cSEd Maste 	{OEX_PRECISEFP, "PRECISEFP"},
16542c23cb7cSEd Maste 	{OEX_DISMISS, "DISMISS"},
16552c23cb7cSEd Maste 	{0, NULL}
16562c23cb7cSEd Maste };
16572c23cb7cSEd Maste 
16582c23cb7cSEd Maste static struct mips_option mips_pad_option[] = {
16592c23cb7cSEd Maste 	{OPAD_PREFIX, "PREFIX"},
16602c23cb7cSEd Maste 	{OPAD_POSTFIX, "POSTFIX"},
16612c23cb7cSEd Maste 	{OPAD_SYMBOL, "SYMBOL"},
16622c23cb7cSEd Maste 	{0, NULL}
16632c23cb7cSEd Maste };
16642c23cb7cSEd Maste 
16652c23cb7cSEd Maste static struct mips_option mips_hwpatch_option[] = {
16662c23cb7cSEd Maste 	{OHW_R4KEOP, "R4KEOP"},
16672c23cb7cSEd Maste 	{OHW_R8KPFETCH, "R8KPFETCH"},
16682c23cb7cSEd Maste 	{OHW_R5KEOP, "R5KEOP"},
16692c23cb7cSEd Maste 	{OHW_R5KCVTL, "R5KCVTL"},
16702c23cb7cSEd Maste 	{0, NULL}
16712c23cb7cSEd Maste };
16722c23cb7cSEd Maste 
16732c23cb7cSEd Maste static struct mips_option mips_hwa_option[] = {
16742c23cb7cSEd Maste 	{OHWA0_R4KEOP_CHECKED, "R4KEOP_CHECKED"},
16752c23cb7cSEd Maste 	{OHWA0_R4KEOP_CLEAN, "R4KEOP_CLEAN"},
16762c23cb7cSEd Maste 	{0, NULL}
16772c23cb7cSEd Maste };
16782c23cb7cSEd Maste 
16792c23cb7cSEd Maste static struct mips_option mips_hwo_option[] = {
16802c23cb7cSEd Maste 	{OHWO0_FIXADE, "FIXADE"},
16812c23cb7cSEd Maste 	{0, NULL}
16822c23cb7cSEd Maste };
16832c23cb7cSEd Maste 
16842c23cb7cSEd Maste static const char *
16852c23cb7cSEd Maste option_kind(uint8_t kind)
16862c23cb7cSEd Maste {
16872c23cb7cSEd Maste 	static char s_kind[32];
16882c23cb7cSEd Maste 
16892c23cb7cSEd Maste 	switch (kind) {
16902c23cb7cSEd Maste 	case ODK_NULL: return "NULL";
16912c23cb7cSEd Maste 	case ODK_REGINFO: return "REGINFO";
16922c23cb7cSEd Maste 	case ODK_EXCEPTIONS: return "EXCEPTIONS";
16932c23cb7cSEd Maste 	case ODK_PAD: return "PAD";
16942c23cb7cSEd Maste 	case ODK_HWPATCH: return "HWPATCH";
16952c23cb7cSEd Maste 	case ODK_FILL: return "FILL";
16962c23cb7cSEd Maste 	case ODK_TAGS: return "TAGS";
16972c23cb7cSEd Maste 	case ODK_HWAND: return "HWAND";
16982c23cb7cSEd Maste 	case ODK_HWOR: return "HWOR";
16992c23cb7cSEd Maste 	case ODK_GP_GROUP: return "GP_GROUP";
17002c23cb7cSEd Maste 	case ODK_IDENT: return "IDENT";
17012c23cb7cSEd Maste 	default:
17022c23cb7cSEd Maste 		snprintf(s_kind, sizeof(s_kind), "<unknown: %u>", kind);
17032c23cb7cSEd Maste 		return (s_kind);
17042c23cb7cSEd Maste 	}
17052c23cb7cSEd Maste }
17062c23cb7cSEd Maste 
17072c23cb7cSEd Maste static const char *
17082c23cb7cSEd Maste top_tag(unsigned int tag)
17092c23cb7cSEd Maste {
17102c23cb7cSEd Maste 	static char s_top_tag[32];
17112c23cb7cSEd Maste 
17122c23cb7cSEd Maste 	switch (tag) {
17132c23cb7cSEd Maste 	case 1: return "File Attributes";
17142c23cb7cSEd Maste 	case 2: return "Section Attributes";
17152c23cb7cSEd Maste 	case 3: return "Symbol Attributes";
17162c23cb7cSEd Maste 	default:
17172c23cb7cSEd Maste 		snprintf(s_top_tag, sizeof(s_top_tag), "Unknown tag: %u", tag);
17182c23cb7cSEd Maste 		return (s_top_tag);
17192c23cb7cSEd Maste 	}
17202c23cb7cSEd Maste }
17212c23cb7cSEd Maste 
17222c23cb7cSEd Maste static const char *
17232c23cb7cSEd Maste aeabi_cpu_arch(uint64_t arch)
17242c23cb7cSEd Maste {
17252c23cb7cSEd Maste 	static char s_cpu_arch[32];
17262c23cb7cSEd Maste 
17272c23cb7cSEd Maste 	switch (arch) {
17282c23cb7cSEd Maste 	case 0: return "Pre-V4";
17292c23cb7cSEd Maste 	case 1: return "ARM v4";
17302c23cb7cSEd Maste 	case 2: return "ARM v4T";
17312c23cb7cSEd Maste 	case 3: return "ARM v5T";
17322c23cb7cSEd Maste 	case 4: return "ARM v5TE";
17332c23cb7cSEd Maste 	case 5: return "ARM v5TEJ";
17342c23cb7cSEd Maste 	case 6: return "ARM v6";
17352c23cb7cSEd Maste 	case 7: return "ARM v6KZ";
17362c23cb7cSEd Maste 	case 8: return "ARM v6T2";
17372c23cb7cSEd Maste 	case 9: return "ARM v6K";
17382c23cb7cSEd Maste 	case 10: return "ARM v7";
17392c23cb7cSEd Maste 	case 11: return "ARM v6-M";
17402c23cb7cSEd Maste 	case 12: return "ARM v6S-M";
17412c23cb7cSEd Maste 	case 13: return "ARM v7E-M";
17422c23cb7cSEd Maste 	default:
17432c23cb7cSEd Maste 		snprintf(s_cpu_arch, sizeof(s_cpu_arch),
17442c23cb7cSEd Maste 		    "Unknown (%ju)", (uintmax_t) arch);
17452c23cb7cSEd Maste 		return (s_cpu_arch);
17462c23cb7cSEd Maste 	}
17472c23cb7cSEd Maste }
17482c23cb7cSEd Maste 
17492c23cb7cSEd Maste static const char *
17502c23cb7cSEd Maste aeabi_cpu_arch_profile(uint64_t pf)
17512c23cb7cSEd Maste {
17522c23cb7cSEd Maste 	static char s_arch_profile[32];
17532c23cb7cSEd Maste 
17542c23cb7cSEd Maste 	switch (pf) {
17552c23cb7cSEd Maste 	case 0:
17562c23cb7cSEd Maste 		return "Not applicable";
17572c23cb7cSEd Maste 	case 0x41:		/* 'A' */
17582c23cb7cSEd Maste 		return "Application Profile";
17592c23cb7cSEd Maste 	case 0x52:		/* 'R' */
17602c23cb7cSEd Maste 		return "Real-Time Profile";
17612c23cb7cSEd Maste 	case 0x4D:		/* 'M' */
17622c23cb7cSEd Maste 		return "Microcontroller Profile";
17632c23cb7cSEd Maste 	case 0x53:		/* 'S' */
17642c23cb7cSEd Maste 		return "Application or Real-Time Profile";
17652c23cb7cSEd Maste 	default:
17662c23cb7cSEd Maste 		snprintf(s_arch_profile, sizeof(s_arch_profile),
17672c23cb7cSEd Maste 		    "Unknown (%ju)\n", (uintmax_t) pf);
17682c23cb7cSEd Maste 		return (s_arch_profile);
17692c23cb7cSEd Maste 	}
17702c23cb7cSEd Maste }
17712c23cb7cSEd Maste 
17722c23cb7cSEd Maste static const char *
17732c23cb7cSEd Maste aeabi_arm_isa(uint64_t ai)
17742c23cb7cSEd Maste {
17752c23cb7cSEd Maste 	static char s_ai[32];
17762c23cb7cSEd Maste 
17772c23cb7cSEd Maste 	switch (ai) {
17782c23cb7cSEd Maste 	case 0: return "No";
17792c23cb7cSEd Maste 	case 1: return "Yes";
17802c23cb7cSEd Maste 	default:
17812c23cb7cSEd Maste 		snprintf(s_ai, sizeof(s_ai), "Unknown (%ju)\n",
17822c23cb7cSEd Maste 		    (uintmax_t) ai);
17832c23cb7cSEd Maste 		return (s_ai);
17842c23cb7cSEd Maste 	}
17852c23cb7cSEd Maste }
17862c23cb7cSEd Maste 
17872c23cb7cSEd Maste static const char *
17882c23cb7cSEd Maste aeabi_thumb_isa(uint64_t ti)
17892c23cb7cSEd Maste {
17902c23cb7cSEd Maste 	static char s_ti[32];
17912c23cb7cSEd Maste 
17922c23cb7cSEd Maste 	switch (ti) {
17932c23cb7cSEd Maste 	case 0: return "No";
17942c23cb7cSEd Maste 	case 1: return "16-bit Thumb";
17952c23cb7cSEd Maste 	case 2: return "32-bit Thumb";
17962c23cb7cSEd Maste 	default:
17972c23cb7cSEd Maste 		snprintf(s_ti, sizeof(s_ti), "Unknown (%ju)\n",
17982c23cb7cSEd Maste 		    (uintmax_t) ti);
17992c23cb7cSEd Maste 		return (s_ti);
18002c23cb7cSEd Maste 	}
18012c23cb7cSEd Maste }
18022c23cb7cSEd Maste 
18032c23cb7cSEd Maste static const char *
18042c23cb7cSEd Maste aeabi_fp_arch(uint64_t fp)
18052c23cb7cSEd Maste {
18062c23cb7cSEd Maste 	static char s_fp_arch[32];
18072c23cb7cSEd Maste 
18082c23cb7cSEd Maste 	switch (fp) {
18092c23cb7cSEd Maste 	case 0: return "No";
18102c23cb7cSEd Maste 	case 1: return "VFPv1";
18112c23cb7cSEd Maste 	case 2: return "VFPv2";
18122c23cb7cSEd Maste 	case 3: return "VFPv3";
18132c23cb7cSEd Maste 	case 4: return "VFPv3-D16";
18142c23cb7cSEd Maste 	case 5: return "VFPv4";
18152c23cb7cSEd Maste 	case 6: return "VFPv4-D16";
18162c23cb7cSEd Maste 	default:
18172c23cb7cSEd Maste 		snprintf(s_fp_arch, sizeof(s_fp_arch), "Unknown (%ju)",
18182c23cb7cSEd Maste 		    (uintmax_t) fp);
18192c23cb7cSEd Maste 		return (s_fp_arch);
18202c23cb7cSEd Maste 	}
18212c23cb7cSEd Maste }
18222c23cb7cSEd Maste 
18232c23cb7cSEd Maste static const char *
18242c23cb7cSEd Maste aeabi_wmmx_arch(uint64_t wmmx)
18252c23cb7cSEd Maste {
18262c23cb7cSEd Maste 	static char s_wmmx[32];
18272c23cb7cSEd Maste 
18282c23cb7cSEd Maste 	switch (wmmx) {
18292c23cb7cSEd Maste 	case 0: return "No";
18302c23cb7cSEd Maste 	case 1: return "WMMXv1";
18312c23cb7cSEd Maste 	case 2: return "WMMXv2";
18322c23cb7cSEd Maste 	default:
18332c23cb7cSEd Maste 		snprintf(s_wmmx, sizeof(s_wmmx), "Unknown (%ju)",
18342c23cb7cSEd Maste 		    (uintmax_t) wmmx);
18352c23cb7cSEd Maste 		return (s_wmmx);
18362c23cb7cSEd Maste 	}
18372c23cb7cSEd Maste }
18382c23cb7cSEd Maste 
18392c23cb7cSEd Maste static const char *
18402c23cb7cSEd Maste aeabi_adv_simd_arch(uint64_t simd)
18412c23cb7cSEd Maste {
18422c23cb7cSEd Maste 	static char s_simd[32];
18432c23cb7cSEd Maste 
18442c23cb7cSEd Maste 	switch (simd) {
18452c23cb7cSEd Maste 	case 0: return "No";
18462c23cb7cSEd Maste 	case 1: return "NEONv1";
18472c23cb7cSEd Maste 	case 2: return "NEONv2";
18482c23cb7cSEd Maste 	default:
18492c23cb7cSEd Maste 		snprintf(s_simd, sizeof(s_simd), "Unknown (%ju)",
18502c23cb7cSEd Maste 		    (uintmax_t) simd);
18512c23cb7cSEd Maste 		return (s_simd);
18522c23cb7cSEd Maste 	}
18532c23cb7cSEd Maste }
18542c23cb7cSEd Maste 
18552c23cb7cSEd Maste static const char *
18562c23cb7cSEd Maste aeabi_pcs_config(uint64_t pcs)
18572c23cb7cSEd Maste {
18582c23cb7cSEd Maste 	static char s_pcs[32];
18592c23cb7cSEd Maste 
18602c23cb7cSEd Maste 	switch (pcs) {
18612c23cb7cSEd Maste 	case 0: return "None";
18622c23cb7cSEd Maste 	case 1: return "Bare platform";
18632c23cb7cSEd Maste 	case 2: return "Linux";
18642c23cb7cSEd Maste 	case 3: return "Linux DSO";
18652c23cb7cSEd Maste 	case 4: return "Palm OS 2004";
18662c23cb7cSEd Maste 	case 5: return "Palm OS (future)";
18672c23cb7cSEd Maste 	case 6: return "Symbian OS 2004";
18682c23cb7cSEd Maste 	case 7: return "Symbian OS (future)";
18692c23cb7cSEd Maste 	default:
18702c23cb7cSEd Maste 		snprintf(s_pcs, sizeof(s_pcs), "Unknown (%ju)",
18712c23cb7cSEd Maste 		    (uintmax_t) pcs);
18722c23cb7cSEd Maste 		return (s_pcs);
18732c23cb7cSEd Maste 	}
18742c23cb7cSEd Maste }
18752c23cb7cSEd Maste 
18762c23cb7cSEd Maste static const char *
18772c23cb7cSEd Maste aeabi_pcs_r9(uint64_t r9)
18782c23cb7cSEd Maste {
18792c23cb7cSEd Maste 	static char s_r9[32];
18802c23cb7cSEd Maste 
18812c23cb7cSEd Maste 	switch (r9) {
18822c23cb7cSEd Maste 	case 0: return "V6";
18832c23cb7cSEd Maste 	case 1: return "SB";
18842c23cb7cSEd Maste 	case 2: return "TLS pointer";
18852c23cb7cSEd Maste 	case 3: return "Unused";
18862c23cb7cSEd Maste 	default:
18872c23cb7cSEd Maste 		snprintf(s_r9, sizeof(s_r9), "Unknown (%ju)", (uintmax_t) r9);
18882c23cb7cSEd Maste 		return (s_r9);
18892c23cb7cSEd Maste 	}
18902c23cb7cSEd Maste }
18912c23cb7cSEd Maste 
18922c23cb7cSEd Maste static const char *
18932c23cb7cSEd Maste aeabi_pcs_rw(uint64_t rw)
18942c23cb7cSEd Maste {
18952c23cb7cSEd Maste 	static char s_rw[32];
18962c23cb7cSEd Maste 
18972c23cb7cSEd Maste 	switch (rw) {
18982c23cb7cSEd Maste 	case 0: return "Absolute";
18992c23cb7cSEd Maste 	case 1: return "PC-relative";
19002c23cb7cSEd Maste 	case 2: return "SB-relative";
19012c23cb7cSEd Maste 	case 3: return "None";
19022c23cb7cSEd Maste 	default:
19032c23cb7cSEd Maste 		snprintf(s_rw, sizeof(s_rw), "Unknown (%ju)", (uintmax_t) rw);
19042c23cb7cSEd Maste 		return (s_rw);
19052c23cb7cSEd Maste 	}
19062c23cb7cSEd Maste }
19072c23cb7cSEd Maste 
19082c23cb7cSEd Maste static const char *
19092c23cb7cSEd Maste aeabi_pcs_ro(uint64_t ro)
19102c23cb7cSEd Maste {
19112c23cb7cSEd Maste 	static char s_ro[32];
19122c23cb7cSEd Maste 
19132c23cb7cSEd Maste 	switch (ro) {
19142c23cb7cSEd Maste 	case 0: return "Absolute";
19152c23cb7cSEd Maste 	case 1: return "PC-relative";
19162c23cb7cSEd Maste 	case 2: return "None";
19172c23cb7cSEd Maste 	default:
19182c23cb7cSEd Maste 		snprintf(s_ro, sizeof(s_ro), "Unknown (%ju)", (uintmax_t) ro);
19192c23cb7cSEd Maste 		return (s_ro);
19202c23cb7cSEd Maste 	}
19212c23cb7cSEd Maste }
19222c23cb7cSEd Maste 
19232c23cb7cSEd Maste static const char *
19242c23cb7cSEd Maste aeabi_pcs_got(uint64_t got)
19252c23cb7cSEd Maste {
19262c23cb7cSEd Maste 	static char s_got[32];
19272c23cb7cSEd Maste 
19282c23cb7cSEd Maste 	switch (got) {
19292c23cb7cSEd Maste 	case 0: return "None";
19302c23cb7cSEd Maste 	case 1: return "direct";
19312c23cb7cSEd Maste 	case 2: return "indirect via GOT";
19322c23cb7cSEd Maste 	default:
19332c23cb7cSEd Maste 		snprintf(s_got, sizeof(s_got), "Unknown (%ju)",
19342c23cb7cSEd Maste 		    (uintmax_t) got);
19352c23cb7cSEd Maste 		return (s_got);
19362c23cb7cSEd Maste 	}
19372c23cb7cSEd Maste }
19382c23cb7cSEd Maste 
19392c23cb7cSEd Maste static const char *
19402c23cb7cSEd Maste aeabi_pcs_wchar_t(uint64_t wt)
19412c23cb7cSEd Maste {
19422c23cb7cSEd Maste 	static char s_wt[32];
19432c23cb7cSEd Maste 
19442c23cb7cSEd Maste 	switch (wt) {
19452c23cb7cSEd Maste 	case 0: return "None";
19462c23cb7cSEd Maste 	case 2: return "wchar_t size 2";
19472c23cb7cSEd Maste 	case 4: return "wchar_t size 4";
19482c23cb7cSEd Maste 	default:
19492c23cb7cSEd Maste 		snprintf(s_wt, sizeof(s_wt), "Unknown (%ju)", (uintmax_t) wt);
19502c23cb7cSEd Maste 		return (s_wt);
19512c23cb7cSEd Maste 	}
19522c23cb7cSEd Maste }
19532c23cb7cSEd Maste 
19542c23cb7cSEd Maste static const char *
19552c23cb7cSEd Maste aeabi_enum_size(uint64_t es)
19562c23cb7cSEd Maste {
19572c23cb7cSEd Maste 	static char s_es[32];
19582c23cb7cSEd Maste 
19592c23cb7cSEd Maste 	switch (es) {
19602c23cb7cSEd Maste 	case 0: return "None";
19612c23cb7cSEd Maste 	case 1: return "smallest";
19622c23cb7cSEd Maste 	case 2: return "32-bit";
19632c23cb7cSEd Maste 	case 3: return "visible 32-bit";
19642c23cb7cSEd Maste 	default:
19652c23cb7cSEd Maste 		snprintf(s_es, sizeof(s_es), "Unknown (%ju)", (uintmax_t) es);
19662c23cb7cSEd Maste 		return (s_es);
19672c23cb7cSEd Maste 	}
19682c23cb7cSEd Maste }
19692c23cb7cSEd Maste 
19702c23cb7cSEd Maste static const char *
19712c23cb7cSEd Maste aeabi_align_needed(uint64_t an)
19722c23cb7cSEd Maste {
19732c23cb7cSEd Maste 	static char s_align_n[64];
19742c23cb7cSEd Maste 
19752c23cb7cSEd Maste 	switch (an) {
19762c23cb7cSEd Maste 	case 0: return "No";
19772c23cb7cSEd Maste 	case 1: return "8-byte align";
19782c23cb7cSEd Maste 	case 2: return "4-byte align";
19792c23cb7cSEd Maste 	case 3: return "Reserved";
19802c23cb7cSEd Maste 	default:
19812c23cb7cSEd Maste 		if (an >= 4 && an <= 12)
19822c23cb7cSEd Maste 			snprintf(s_align_n, sizeof(s_align_n), "8-byte align"
19832c23cb7cSEd Maste 			    " and up to 2^%ju-byte extended align",
19842c23cb7cSEd Maste 			    (uintmax_t) an);
19852c23cb7cSEd Maste 		else
19862c23cb7cSEd Maste 			snprintf(s_align_n, sizeof(s_align_n), "Unknown (%ju)",
19872c23cb7cSEd Maste 			    (uintmax_t) an);
19882c23cb7cSEd Maste 		return (s_align_n);
19892c23cb7cSEd Maste 	}
19902c23cb7cSEd Maste }
19912c23cb7cSEd Maste 
19922c23cb7cSEd Maste static const char *
19932c23cb7cSEd Maste aeabi_align_preserved(uint64_t ap)
19942c23cb7cSEd Maste {
19952c23cb7cSEd Maste 	static char s_align_p[128];
19962c23cb7cSEd Maste 
19972c23cb7cSEd Maste 	switch (ap) {
19982c23cb7cSEd Maste 	case 0: return "No";
19992c23cb7cSEd Maste 	case 1: return "8-byte align";
20002c23cb7cSEd Maste 	case 2: return "8-byte align and SP % 8 == 0";
20012c23cb7cSEd Maste 	case 3: return "Reserved";
20022c23cb7cSEd Maste 	default:
20032c23cb7cSEd Maste 		if (ap >= 4 && ap <= 12)
20042c23cb7cSEd Maste 			snprintf(s_align_p, sizeof(s_align_p), "8-byte align"
20052c23cb7cSEd Maste 			    " and SP %% 8 == 0 and up to 2^%ju-byte extended"
20062c23cb7cSEd Maste 			    " align", (uintmax_t) ap);
20072c23cb7cSEd Maste 		else
20082c23cb7cSEd Maste 			snprintf(s_align_p, sizeof(s_align_p), "Unknown (%ju)",
20092c23cb7cSEd Maste 			    (uintmax_t) ap);
20102c23cb7cSEd Maste 		return (s_align_p);
20112c23cb7cSEd Maste 	}
20122c23cb7cSEd Maste }
20132c23cb7cSEd Maste 
20142c23cb7cSEd Maste static const char *
20152c23cb7cSEd Maste aeabi_fp_rounding(uint64_t fr)
20162c23cb7cSEd Maste {
20172c23cb7cSEd Maste 	static char s_fp_r[32];
20182c23cb7cSEd Maste 
20192c23cb7cSEd Maste 	switch (fr) {
20202c23cb7cSEd Maste 	case 0: return "Unused";
20212c23cb7cSEd Maste 	case 1: return "Needed";
20222c23cb7cSEd Maste 	default:
20232c23cb7cSEd Maste 		snprintf(s_fp_r, sizeof(s_fp_r), "Unknown (%ju)",
20242c23cb7cSEd Maste 		    (uintmax_t) fr);
20252c23cb7cSEd Maste 		return (s_fp_r);
20262c23cb7cSEd Maste 	}
20272c23cb7cSEd Maste }
20282c23cb7cSEd Maste 
20292c23cb7cSEd Maste static const char *
20302c23cb7cSEd Maste aeabi_fp_denormal(uint64_t fd)
20312c23cb7cSEd Maste {
20322c23cb7cSEd Maste 	static char s_fp_d[32];
20332c23cb7cSEd Maste 
20342c23cb7cSEd Maste 	switch (fd) {
20352c23cb7cSEd Maste 	case 0: return "Unused";
20362c23cb7cSEd Maste 	case 1: return "Needed";
20372c23cb7cSEd Maste 	case 2: return "Sign Only";
20382c23cb7cSEd Maste 	default:
20392c23cb7cSEd Maste 		snprintf(s_fp_d, sizeof(s_fp_d), "Unknown (%ju)",
20402c23cb7cSEd Maste 		    (uintmax_t) fd);
20412c23cb7cSEd Maste 		return (s_fp_d);
20422c23cb7cSEd Maste 	}
20432c23cb7cSEd Maste }
20442c23cb7cSEd Maste 
20452c23cb7cSEd Maste static const char *
20462c23cb7cSEd Maste aeabi_fp_exceptions(uint64_t fe)
20472c23cb7cSEd Maste {
20482c23cb7cSEd Maste 	static char s_fp_e[32];
20492c23cb7cSEd Maste 
20502c23cb7cSEd Maste 	switch (fe) {
20512c23cb7cSEd Maste 	case 0: return "Unused";
20522c23cb7cSEd Maste 	case 1: return "Needed";
20532c23cb7cSEd Maste 	default:
20542c23cb7cSEd Maste 		snprintf(s_fp_e, sizeof(s_fp_e), "Unknown (%ju)",
20552c23cb7cSEd Maste 		    (uintmax_t) fe);
20562c23cb7cSEd Maste 		return (s_fp_e);
20572c23cb7cSEd Maste 	}
20582c23cb7cSEd Maste }
20592c23cb7cSEd Maste 
20602c23cb7cSEd Maste static const char *
20612c23cb7cSEd Maste aeabi_fp_user_exceptions(uint64_t fu)
20622c23cb7cSEd Maste {
20632c23cb7cSEd Maste 	static char s_fp_u[32];
20642c23cb7cSEd Maste 
20652c23cb7cSEd Maste 	switch (fu) {
20662c23cb7cSEd Maste 	case 0: return "Unused";
20672c23cb7cSEd Maste 	case 1: return "Needed";
20682c23cb7cSEd Maste 	default:
20692c23cb7cSEd Maste 		snprintf(s_fp_u, sizeof(s_fp_u), "Unknown (%ju)",
20702c23cb7cSEd Maste 		    (uintmax_t) fu);
20712c23cb7cSEd Maste 		return (s_fp_u);
20722c23cb7cSEd Maste 	}
20732c23cb7cSEd Maste }
20742c23cb7cSEd Maste 
20752c23cb7cSEd Maste static const char *
20762c23cb7cSEd Maste aeabi_fp_number_model(uint64_t fn)
20772c23cb7cSEd Maste {
20782c23cb7cSEd Maste 	static char s_fp_n[32];
20792c23cb7cSEd Maste 
20802c23cb7cSEd Maste 	switch (fn) {
20812c23cb7cSEd Maste 	case 0: return "Unused";
20822c23cb7cSEd Maste 	case 1: return "IEEE 754 normal";
20832c23cb7cSEd Maste 	case 2: return "RTABI";
20842c23cb7cSEd Maste 	case 3: return "IEEE 754";
20852c23cb7cSEd Maste 	default:
20862c23cb7cSEd Maste 		snprintf(s_fp_n, sizeof(s_fp_n), "Unknown (%ju)",
20872c23cb7cSEd Maste 		    (uintmax_t) fn);
20882c23cb7cSEd Maste 		return (s_fp_n);
20892c23cb7cSEd Maste 	}
20902c23cb7cSEd Maste }
20912c23cb7cSEd Maste 
20922c23cb7cSEd Maste static const char *
20932c23cb7cSEd Maste aeabi_fp_16bit_format(uint64_t fp16)
20942c23cb7cSEd Maste {
20952c23cb7cSEd Maste 	static char s_fp_16[64];
20962c23cb7cSEd Maste 
20972c23cb7cSEd Maste 	switch (fp16) {
20982c23cb7cSEd Maste 	case 0: return "None";
20992c23cb7cSEd Maste 	case 1: return "IEEE 754";
21002c23cb7cSEd Maste 	case 2: return "VFPv3/Advanced SIMD (alternative format)";
21012c23cb7cSEd Maste 	default:
21022c23cb7cSEd Maste 		snprintf(s_fp_16, sizeof(s_fp_16), "Unknown (%ju)",
21032c23cb7cSEd Maste 		    (uintmax_t) fp16);
21042c23cb7cSEd Maste 		return (s_fp_16);
21052c23cb7cSEd Maste 	}
21062c23cb7cSEd Maste }
21072c23cb7cSEd Maste 
21082c23cb7cSEd Maste static const char *
21092c23cb7cSEd Maste aeabi_mpext(uint64_t mp)
21102c23cb7cSEd Maste {
21112c23cb7cSEd Maste 	static char s_mp[32];
21122c23cb7cSEd Maste 
21132c23cb7cSEd Maste 	switch (mp) {
21142c23cb7cSEd Maste 	case 0: return "Not allowed";
21152c23cb7cSEd Maste 	case 1: return "Allowed";
21162c23cb7cSEd Maste 	default:
21172c23cb7cSEd Maste 		snprintf(s_mp, sizeof(s_mp), "Unknown (%ju)",
21182c23cb7cSEd Maste 		    (uintmax_t) mp);
21192c23cb7cSEd Maste 		return (s_mp);
21202c23cb7cSEd Maste 	}
21212c23cb7cSEd Maste }
21222c23cb7cSEd Maste 
21232c23cb7cSEd Maste static const char *
21242c23cb7cSEd Maste aeabi_div(uint64_t du)
21252c23cb7cSEd Maste {
21262c23cb7cSEd Maste 	static char s_du[32];
21272c23cb7cSEd Maste 
21282c23cb7cSEd Maste 	switch (du) {
21292c23cb7cSEd Maste 	case 0: return "Yes (V7-R/V7-M)";
21302c23cb7cSEd Maste 	case 1: return "No";
21312c23cb7cSEd Maste 	case 2: return "Yes (V7-A)";
21322c23cb7cSEd Maste 	default:
21332c23cb7cSEd Maste 		snprintf(s_du, sizeof(s_du), "Unknown (%ju)",
21342c23cb7cSEd Maste 		    (uintmax_t) du);
21352c23cb7cSEd Maste 		return (s_du);
21362c23cb7cSEd Maste 	}
21372c23cb7cSEd Maste }
21382c23cb7cSEd Maste 
21392c23cb7cSEd Maste static const char *
21402c23cb7cSEd Maste aeabi_t2ee(uint64_t t2ee)
21412c23cb7cSEd Maste {
21422c23cb7cSEd Maste 	static char s_t2ee[32];
21432c23cb7cSEd Maste 
21442c23cb7cSEd Maste 	switch (t2ee) {
21452c23cb7cSEd Maste 	case 0: return "Not allowed";
21462c23cb7cSEd Maste 	case 1: return "Allowed";
21472c23cb7cSEd Maste 	default:
21482c23cb7cSEd Maste 		snprintf(s_t2ee, sizeof(s_t2ee), "Unknown(%ju)",
21492c23cb7cSEd Maste 		    (uintmax_t) t2ee);
21502c23cb7cSEd Maste 		return (s_t2ee);
21512c23cb7cSEd Maste 	}
21522c23cb7cSEd Maste 
21532c23cb7cSEd Maste }
21542c23cb7cSEd Maste 
21552c23cb7cSEd Maste static const char *
21562c23cb7cSEd Maste aeabi_hardfp(uint64_t hfp)
21572c23cb7cSEd Maste {
21582c23cb7cSEd Maste 	static char s_hfp[32];
21592c23cb7cSEd Maste 
21602c23cb7cSEd Maste 	switch (hfp) {
21612c23cb7cSEd Maste 	case 0: return "Tag_FP_arch";
21622c23cb7cSEd Maste 	case 1: return "only SP";
21632c23cb7cSEd Maste 	case 2: return "only DP";
21642c23cb7cSEd Maste 	case 3: return "both SP and DP";
21652c23cb7cSEd Maste 	default:
21662c23cb7cSEd Maste 		snprintf(s_hfp, sizeof(s_hfp), "Unknown (%ju)",
21672c23cb7cSEd Maste 		    (uintmax_t) hfp);
21682c23cb7cSEd Maste 		return (s_hfp);
21692c23cb7cSEd Maste 	}
21702c23cb7cSEd Maste }
21712c23cb7cSEd Maste 
21722c23cb7cSEd Maste static const char *
21732c23cb7cSEd Maste aeabi_vfp_args(uint64_t va)
21742c23cb7cSEd Maste {
21752c23cb7cSEd Maste 	static char s_va[32];
21762c23cb7cSEd Maste 
21772c23cb7cSEd Maste 	switch (va) {
21782c23cb7cSEd Maste 	case 0: return "AAPCS (base variant)";
21792c23cb7cSEd Maste 	case 1: return "AAPCS (VFP variant)";
21802c23cb7cSEd Maste 	case 2: return "toolchain-specific";
21812c23cb7cSEd Maste 	default:
21822c23cb7cSEd Maste 		snprintf(s_va, sizeof(s_va), "Unknown (%ju)", (uintmax_t) va);
21832c23cb7cSEd Maste 		return (s_va);
21842c23cb7cSEd Maste 	}
21852c23cb7cSEd Maste }
21862c23cb7cSEd Maste 
21872c23cb7cSEd Maste static const char *
21882c23cb7cSEd Maste aeabi_wmmx_args(uint64_t wa)
21892c23cb7cSEd Maste {
21902c23cb7cSEd Maste 	static char s_wa[32];
21912c23cb7cSEd Maste 
21922c23cb7cSEd Maste 	switch (wa) {
21932c23cb7cSEd Maste 	case 0: return "AAPCS (base variant)";
21942c23cb7cSEd Maste 	case 1: return "Intel WMMX";
21952c23cb7cSEd Maste 	case 2: return "toolchain-specific";
21962c23cb7cSEd Maste 	default:
21972c23cb7cSEd Maste 		snprintf(s_wa, sizeof(s_wa), "Unknown(%ju)", (uintmax_t) wa);
21982c23cb7cSEd Maste 		return (s_wa);
21992c23cb7cSEd Maste 	}
22002c23cb7cSEd Maste }
22012c23cb7cSEd Maste 
22022c23cb7cSEd Maste static const char *
22032c23cb7cSEd Maste aeabi_unaligned_access(uint64_t ua)
22042c23cb7cSEd Maste {
22052c23cb7cSEd Maste 	static char s_ua[32];
22062c23cb7cSEd Maste 
22072c23cb7cSEd Maste 	switch (ua) {
22082c23cb7cSEd Maste 	case 0: return "Not allowed";
22092c23cb7cSEd Maste 	case 1: return "Allowed";
22102c23cb7cSEd Maste 	default:
22112c23cb7cSEd Maste 		snprintf(s_ua, sizeof(s_ua), "Unknown(%ju)", (uintmax_t) ua);
22122c23cb7cSEd Maste 		return (s_ua);
22132c23cb7cSEd Maste 	}
22142c23cb7cSEd Maste }
22152c23cb7cSEd Maste 
22162c23cb7cSEd Maste static const char *
22172c23cb7cSEd Maste aeabi_fp_hpext(uint64_t fh)
22182c23cb7cSEd Maste {
22192c23cb7cSEd Maste 	static char s_fh[32];
22202c23cb7cSEd Maste 
22212c23cb7cSEd Maste 	switch (fh) {
22222c23cb7cSEd Maste 	case 0: return "Not allowed";
22232c23cb7cSEd Maste 	case 1: return "Allowed";
22242c23cb7cSEd Maste 	default:
22252c23cb7cSEd Maste 		snprintf(s_fh, sizeof(s_fh), "Unknown(%ju)", (uintmax_t) fh);
22262c23cb7cSEd Maste 		return (s_fh);
22272c23cb7cSEd Maste 	}
22282c23cb7cSEd Maste }
22292c23cb7cSEd Maste 
22302c23cb7cSEd Maste static const char *
22312c23cb7cSEd Maste aeabi_optm_goal(uint64_t og)
22322c23cb7cSEd Maste {
22332c23cb7cSEd Maste 	static char s_og[32];
22342c23cb7cSEd Maste 
22352c23cb7cSEd Maste 	switch (og) {
22362c23cb7cSEd Maste 	case 0: return "None";
22372c23cb7cSEd Maste 	case 1: return "Speed";
22382c23cb7cSEd Maste 	case 2: return "Speed aggressive";
22392c23cb7cSEd Maste 	case 3: return "Space";
22402c23cb7cSEd Maste 	case 4: return "Space aggressive";
22412c23cb7cSEd Maste 	case 5: return "Debugging";
22422c23cb7cSEd Maste 	case 6: return "Best Debugging";
22432c23cb7cSEd Maste 	default:
22442c23cb7cSEd Maste 		snprintf(s_og, sizeof(s_og), "Unknown(%ju)", (uintmax_t) og);
22452c23cb7cSEd Maste 		return (s_og);
22462c23cb7cSEd Maste 	}
22472c23cb7cSEd Maste }
22482c23cb7cSEd Maste 
22492c23cb7cSEd Maste static const char *
22502c23cb7cSEd Maste aeabi_fp_optm_goal(uint64_t fog)
22512c23cb7cSEd Maste {
22522c23cb7cSEd Maste 	static char s_fog[32];
22532c23cb7cSEd Maste 
22542c23cb7cSEd Maste 	switch (fog) {
22552c23cb7cSEd Maste 	case 0: return "None";
22562c23cb7cSEd Maste 	case 1: return "Speed";
22572c23cb7cSEd Maste 	case 2: return "Speed aggressive";
22582c23cb7cSEd Maste 	case 3: return "Space";
22592c23cb7cSEd Maste 	case 4: return "Space aggressive";
22602c23cb7cSEd Maste 	case 5: return "Accurary";
22612c23cb7cSEd Maste 	case 6: return "Best Accurary";
22622c23cb7cSEd Maste 	default:
22632c23cb7cSEd Maste 		snprintf(s_fog, sizeof(s_fog), "Unknown(%ju)",
22642c23cb7cSEd Maste 		    (uintmax_t) fog);
22652c23cb7cSEd Maste 		return (s_fog);
22662c23cb7cSEd Maste 	}
22672c23cb7cSEd Maste }
22682c23cb7cSEd Maste 
22692c23cb7cSEd Maste static const char *
22702c23cb7cSEd Maste aeabi_virtual(uint64_t vt)
22712c23cb7cSEd Maste {
22722c23cb7cSEd Maste 	static char s_virtual[64];
22732c23cb7cSEd Maste 
22742c23cb7cSEd Maste 	switch (vt) {
22752c23cb7cSEd Maste 	case 0: return "No";
22762c23cb7cSEd Maste 	case 1: return "TrustZone";
22772c23cb7cSEd Maste 	case 2: return "Virtualization extension";
22782c23cb7cSEd Maste 	case 3: return "TrustZone and virtualization extension";
22792c23cb7cSEd Maste 	default:
22802c23cb7cSEd Maste 		snprintf(s_virtual, sizeof(s_virtual), "Unknown(%ju)",
22812c23cb7cSEd Maste 		    (uintmax_t) vt);
22822c23cb7cSEd Maste 		return (s_virtual);
22832c23cb7cSEd Maste 	}
22842c23cb7cSEd Maste }
22852c23cb7cSEd Maste 
22862c23cb7cSEd Maste static struct {
22872c23cb7cSEd Maste 	uint64_t tag;
22882c23cb7cSEd Maste 	const char *s_tag;
22892c23cb7cSEd Maste 	const char *(*get_desc)(uint64_t val);
22902c23cb7cSEd Maste } aeabi_tags[] = {
22912c23cb7cSEd Maste 	{4, "Tag_CPU_raw_name", NULL},
22922c23cb7cSEd Maste 	{5, "Tag_CPU_name", NULL},
22932c23cb7cSEd Maste 	{6, "Tag_CPU_arch", aeabi_cpu_arch},
22942c23cb7cSEd Maste 	{7, "Tag_CPU_arch_profile", aeabi_cpu_arch_profile},
22952c23cb7cSEd Maste 	{8, "Tag_ARM_ISA_use", aeabi_arm_isa},
22962c23cb7cSEd Maste 	{9, "Tag_THUMB_ISA_use", aeabi_thumb_isa},
22972c23cb7cSEd Maste 	{10, "Tag_FP_arch", aeabi_fp_arch},
22982c23cb7cSEd Maste 	{11, "Tag_WMMX_arch", aeabi_wmmx_arch},
22992c23cb7cSEd Maste 	{12, "Tag_Advanced_SIMD_arch", aeabi_adv_simd_arch},
23002c23cb7cSEd Maste 	{13, "Tag_PCS_config", aeabi_pcs_config},
23012c23cb7cSEd Maste 	{14, "Tag_ABI_PCS_R9_use", aeabi_pcs_r9},
23022c23cb7cSEd Maste 	{15, "Tag_ABI_PCS_RW_data", aeabi_pcs_rw},
23032c23cb7cSEd Maste 	{16, "Tag_ABI_PCS_RO_data", aeabi_pcs_ro},
23042c23cb7cSEd Maste 	{17, "Tag_ABI_PCS_GOT_use", aeabi_pcs_got},
23052c23cb7cSEd Maste 	{18, "Tag_ABI_PCS_wchar_t", aeabi_pcs_wchar_t},
23062c23cb7cSEd Maste 	{19, "Tag_ABI_FP_rounding", aeabi_fp_rounding},
23072c23cb7cSEd Maste 	{20, "Tag_ABI_FP_denormal", aeabi_fp_denormal},
23082c23cb7cSEd Maste 	{21, "Tag_ABI_FP_exceptions", aeabi_fp_exceptions},
23092c23cb7cSEd Maste 	{22, "Tag_ABI_FP_user_exceptions", aeabi_fp_user_exceptions},
23102c23cb7cSEd Maste 	{23, "Tag_ABI_FP_number_model", aeabi_fp_number_model},
23112c23cb7cSEd Maste 	{24, "Tag_ABI_align_needed", aeabi_align_needed},
23122c23cb7cSEd Maste 	{25, "Tag_ABI_align_preserved", aeabi_align_preserved},
23132c23cb7cSEd Maste 	{26, "Tag_ABI_enum_size", aeabi_enum_size},
23142c23cb7cSEd Maste 	{27, "Tag_ABI_HardFP_use", aeabi_hardfp},
23152c23cb7cSEd Maste 	{28, "Tag_ABI_VFP_args", aeabi_vfp_args},
23162c23cb7cSEd Maste 	{29, "Tag_ABI_WMMX_args", aeabi_wmmx_args},
23172c23cb7cSEd Maste 	{30, "Tag_ABI_optimization_goals", aeabi_optm_goal},
23182c23cb7cSEd Maste 	{31, "Tag_ABI_FP_optimization_goals", aeabi_fp_optm_goal},
23192c23cb7cSEd Maste 	{32, "Tag_compatibility", NULL},
23202c23cb7cSEd Maste 	{34, "Tag_CPU_unaligned_access", aeabi_unaligned_access},
23212c23cb7cSEd Maste 	{36, "Tag_FP_HP_extension", aeabi_fp_hpext},
23222c23cb7cSEd Maste 	{38, "Tag_ABI_FP_16bit_format", aeabi_fp_16bit_format},
23232c23cb7cSEd Maste 	{42, "Tag_MPextension_use", aeabi_mpext},
23242c23cb7cSEd Maste 	{44, "Tag_DIV_use", aeabi_div},
23252c23cb7cSEd Maste 	{64, "Tag_nodefaults", NULL},
23262c23cb7cSEd Maste 	{65, "Tag_also_compatible_with", NULL},
23272c23cb7cSEd Maste 	{66, "Tag_T2EE_use", aeabi_t2ee},
23282c23cb7cSEd Maste 	{67, "Tag_conformance", NULL},
23292c23cb7cSEd Maste 	{68, "Tag_Virtualization_use", aeabi_virtual},
23302c23cb7cSEd Maste 	{70, "Tag_MPextension_use", aeabi_mpext},
23312c23cb7cSEd Maste };
23322c23cb7cSEd Maste 
23332c23cb7cSEd Maste static const char *
23342c23cb7cSEd Maste mips_abi_fp(uint64_t fp)
23352c23cb7cSEd Maste {
23362c23cb7cSEd Maste 	static char s_mips_abi_fp[64];
23372c23cb7cSEd Maste 
23382c23cb7cSEd Maste 	switch (fp) {
23392c23cb7cSEd Maste 	case 0: return "N/A";
23402c23cb7cSEd Maste 	case 1: return "Hard float (double precision)";
23412c23cb7cSEd Maste 	case 2: return "Hard float (single precision)";
23422c23cb7cSEd Maste 	case 3: return "Soft float";
23432c23cb7cSEd Maste 	case 4: return "64-bit float (-mips32r2 -mfp64)";
23442c23cb7cSEd Maste 	default:
23452c23cb7cSEd Maste 		snprintf(s_mips_abi_fp, sizeof(s_mips_abi_fp), "Unknown(%ju)",
23462c23cb7cSEd Maste 		    (uintmax_t) fp);
23472c23cb7cSEd Maste 		return (s_mips_abi_fp);
23482c23cb7cSEd Maste 	}
23492c23cb7cSEd Maste }
23502c23cb7cSEd Maste 
23512c23cb7cSEd Maste static const char *
23522c23cb7cSEd Maste ppc_abi_fp(uint64_t fp)
23532c23cb7cSEd Maste {
23542c23cb7cSEd Maste 	static char s_ppc_abi_fp[64];
23552c23cb7cSEd Maste 
23562c23cb7cSEd Maste 	switch (fp) {
23572c23cb7cSEd Maste 	case 0: return "N/A";
23582c23cb7cSEd Maste 	case 1: return "Hard float (double precision)";
23592c23cb7cSEd Maste 	case 2: return "Soft float";
23602c23cb7cSEd Maste 	case 3: return "Hard float (single precision)";
23612c23cb7cSEd Maste 	default:
23622c23cb7cSEd Maste 		snprintf(s_ppc_abi_fp, sizeof(s_ppc_abi_fp), "Unknown(%ju)",
23632c23cb7cSEd Maste 		    (uintmax_t) fp);
23642c23cb7cSEd Maste 		return (s_ppc_abi_fp);
23652c23cb7cSEd Maste 	}
23662c23cb7cSEd Maste }
23672c23cb7cSEd Maste 
23682c23cb7cSEd Maste static const char *
23692c23cb7cSEd Maste ppc_abi_vector(uint64_t vec)
23702c23cb7cSEd Maste {
23712c23cb7cSEd Maste 	static char s_vec[64];
23722c23cb7cSEd Maste 
23732c23cb7cSEd Maste 	switch (vec) {
23742c23cb7cSEd Maste 	case 0: return "N/A";
23752c23cb7cSEd Maste 	case 1: return "Generic purpose registers";
23762c23cb7cSEd Maste 	case 2: return "AltiVec registers";
23772c23cb7cSEd Maste 	case 3: return "SPE registers";
23782c23cb7cSEd Maste 	default:
23792c23cb7cSEd Maste 		snprintf(s_vec, sizeof(s_vec), "Unknown(%ju)", (uintmax_t) vec);
23802c23cb7cSEd Maste 		return (s_vec);
23812c23cb7cSEd Maste 	}
23822c23cb7cSEd Maste }
23832c23cb7cSEd Maste 
2384cf781b2eSEd Maste static const char *
2385cf781b2eSEd Maste dwarf_reg(unsigned int mach, unsigned int reg)
2386cf781b2eSEd Maste {
2387cf781b2eSEd Maste 
2388cf781b2eSEd Maste 	switch (mach) {
2389cf781b2eSEd Maste 	case EM_386:
23903ef90571SEd Maste 	case EM_IAMCU:
2391cf781b2eSEd Maste 		switch (reg) {
2392cf781b2eSEd Maste 		case 0: return "eax";
2393cf781b2eSEd Maste 		case 1: return "ecx";
2394cf781b2eSEd Maste 		case 2: return "edx";
2395cf781b2eSEd Maste 		case 3: return "ebx";
2396cf781b2eSEd Maste 		case 4: return "esp";
2397cf781b2eSEd Maste 		case 5: return "ebp";
2398cf781b2eSEd Maste 		case 6: return "esi";
2399cf781b2eSEd Maste 		case 7: return "edi";
2400cf781b2eSEd Maste 		case 8: return "eip";
2401cf781b2eSEd Maste 		case 9: return "eflags";
2402cf781b2eSEd Maste 		case 11: return "st0";
2403cf781b2eSEd Maste 		case 12: return "st1";
2404cf781b2eSEd Maste 		case 13: return "st2";
2405cf781b2eSEd Maste 		case 14: return "st3";
2406cf781b2eSEd Maste 		case 15: return "st4";
2407cf781b2eSEd Maste 		case 16: return "st5";
2408cf781b2eSEd Maste 		case 17: return "st6";
2409cf781b2eSEd Maste 		case 18: return "st7";
2410cf781b2eSEd Maste 		case 21: return "xmm0";
2411cf781b2eSEd Maste 		case 22: return "xmm1";
2412cf781b2eSEd Maste 		case 23: return "xmm2";
2413cf781b2eSEd Maste 		case 24: return "xmm3";
2414cf781b2eSEd Maste 		case 25: return "xmm4";
2415cf781b2eSEd Maste 		case 26: return "xmm5";
2416cf781b2eSEd Maste 		case 27: return "xmm6";
2417cf781b2eSEd Maste 		case 28: return "xmm7";
2418cf781b2eSEd Maste 		case 29: return "mm0";
2419cf781b2eSEd Maste 		case 30: return "mm1";
2420cf781b2eSEd Maste 		case 31: return "mm2";
2421cf781b2eSEd Maste 		case 32: return "mm3";
2422cf781b2eSEd Maste 		case 33: return "mm4";
2423cf781b2eSEd Maste 		case 34: return "mm5";
2424cf781b2eSEd Maste 		case 35: return "mm6";
2425cf781b2eSEd Maste 		case 36: return "mm7";
2426cf781b2eSEd Maste 		case 37: return "fcw";
2427cf781b2eSEd Maste 		case 38: return "fsw";
2428cf781b2eSEd Maste 		case 39: return "mxcsr";
2429cf781b2eSEd Maste 		case 40: return "es";
2430cf781b2eSEd Maste 		case 41: return "cs";
2431cf781b2eSEd Maste 		case 42: return "ss";
2432cf781b2eSEd Maste 		case 43: return "ds";
2433cf781b2eSEd Maste 		case 44: return "fs";
2434cf781b2eSEd Maste 		case 45: return "gs";
2435cf781b2eSEd Maste 		case 48: return "tr";
2436cf781b2eSEd Maste 		case 49: return "ldtr";
2437cf781b2eSEd Maste 		default: return (NULL);
2438cf781b2eSEd Maste 		}
2439cf781b2eSEd Maste 	case EM_X86_64:
2440cf781b2eSEd Maste 		switch (reg) {
2441cf781b2eSEd Maste 		case 0: return "rax";
2442cf781b2eSEd Maste 		case 1: return "rdx";
2443cf781b2eSEd Maste 		case 2: return "rcx";
2444cf781b2eSEd Maste 		case 3: return "rbx";
2445cf781b2eSEd Maste 		case 4: return "rsi";
2446cf781b2eSEd Maste 		case 5: return "rdi";
2447cf781b2eSEd Maste 		case 6: return "rbp";
2448cf781b2eSEd Maste 		case 7: return "rsp";
2449cf781b2eSEd Maste 		case 16: return "rip";
2450cf781b2eSEd Maste 		case 17: return "xmm0";
2451cf781b2eSEd Maste 		case 18: return "xmm1";
2452cf781b2eSEd Maste 		case 19: return "xmm2";
2453cf781b2eSEd Maste 		case 20: return "xmm3";
2454cf781b2eSEd Maste 		case 21: return "xmm4";
2455cf781b2eSEd Maste 		case 22: return "xmm5";
2456cf781b2eSEd Maste 		case 23: return "xmm6";
2457cf781b2eSEd Maste 		case 24: return "xmm7";
2458cf781b2eSEd Maste 		case 25: return "xmm8";
2459cf781b2eSEd Maste 		case 26: return "xmm9";
2460cf781b2eSEd Maste 		case 27: return "xmm10";
2461cf781b2eSEd Maste 		case 28: return "xmm11";
2462cf781b2eSEd Maste 		case 29: return "xmm12";
2463cf781b2eSEd Maste 		case 30: return "xmm13";
2464cf781b2eSEd Maste 		case 31: return "xmm14";
2465cf781b2eSEd Maste 		case 32: return "xmm15";
2466cf781b2eSEd Maste 		case 33: return "st0";
2467cf781b2eSEd Maste 		case 34: return "st1";
2468cf781b2eSEd Maste 		case 35: return "st2";
2469cf781b2eSEd Maste 		case 36: return "st3";
2470cf781b2eSEd Maste 		case 37: return "st4";
2471cf781b2eSEd Maste 		case 38: return "st5";
2472cf781b2eSEd Maste 		case 39: return "st6";
2473cf781b2eSEd Maste 		case 40: return "st7";
2474cf781b2eSEd Maste 		case 41: return "mm0";
2475cf781b2eSEd Maste 		case 42: return "mm1";
2476cf781b2eSEd Maste 		case 43: return "mm2";
2477cf781b2eSEd Maste 		case 44: return "mm3";
2478cf781b2eSEd Maste 		case 45: return "mm4";
2479cf781b2eSEd Maste 		case 46: return "mm5";
2480cf781b2eSEd Maste 		case 47: return "mm6";
2481cf781b2eSEd Maste 		case 48: return "mm7";
2482cf781b2eSEd Maste 		case 49: return "rflags";
2483cf781b2eSEd Maste 		case 50: return "es";
2484cf781b2eSEd Maste 		case 51: return "cs";
2485cf781b2eSEd Maste 		case 52: return "ss";
2486cf781b2eSEd Maste 		case 53: return "ds";
2487cf781b2eSEd Maste 		case 54: return "fs";
2488cf781b2eSEd Maste 		case 55: return "gs";
2489cf781b2eSEd Maste 		case 58: return "fs.base";
2490cf781b2eSEd Maste 		case 59: return "gs.base";
2491cf781b2eSEd Maste 		case 62: return "tr";
2492cf781b2eSEd Maste 		case 63: return "ldtr";
2493cf781b2eSEd Maste 		case 64: return "mxcsr";
2494cf781b2eSEd Maste 		case 65: return "fcw";
2495cf781b2eSEd Maste 		case 66: return "fsw";
2496cf781b2eSEd Maste 		default: return (NULL);
2497cf781b2eSEd Maste 		}
2498cf781b2eSEd Maste 	default:
2499cf781b2eSEd Maste 		return (NULL);
2500cf781b2eSEd Maste 	}
2501cf781b2eSEd Maste }
2502cf781b2eSEd Maste 
25032c23cb7cSEd Maste static void
25042c23cb7cSEd Maste dump_ehdr(struct readelf *re)
25052c23cb7cSEd Maste {
25062c23cb7cSEd Maste 	size_t		 shnum, shstrndx;
25072c23cb7cSEd Maste 	int		 i;
25082c23cb7cSEd Maste 
25092c23cb7cSEd Maste 	printf("ELF Header:\n");
25102c23cb7cSEd Maste 
25112c23cb7cSEd Maste 	/* e_ident[]. */
25122c23cb7cSEd Maste 	printf("  Magic:   ");
25132c23cb7cSEd Maste 	for (i = 0; i < EI_NIDENT; i++)
25142c23cb7cSEd Maste 		printf("%.2x ", re->ehdr.e_ident[i]);
25152c23cb7cSEd Maste 	putchar('\n');
25162c23cb7cSEd Maste 
25172c23cb7cSEd Maste 	/* EI_CLASS. */
25182c23cb7cSEd Maste 	printf("%-37s%s\n", "  Class:", elf_class(re->ehdr.e_ident[EI_CLASS]));
25192c23cb7cSEd Maste 
25202c23cb7cSEd Maste 	/* EI_DATA. */
25212c23cb7cSEd Maste 	printf("%-37s%s\n", "  Data:", elf_endian(re->ehdr.e_ident[EI_DATA]));
25222c23cb7cSEd Maste 
25232c23cb7cSEd Maste 	/* EI_VERSION. */
25242c23cb7cSEd Maste 	printf("%-37s%d %s\n", "  Version:", re->ehdr.e_ident[EI_VERSION],
25252c23cb7cSEd Maste 	    elf_ver(re->ehdr.e_ident[EI_VERSION]));
25262c23cb7cSEd Maste 
25272c23cb7cSEd Maste 	/* EI_OSABI. */
25282c23cb7cSEd Maste 	printf("%-37s%s\n", "  OS/ABI:", elf_osabi(re->ehdr.e_ident[EI_OSABI]));
25292c23cb7cSEd Maste 
25302c23cb7cSEd Maste 	/* EI_ABIVERSION. */
25312c23cb7cSEd Maste 	printf("%-37s%d\n", "  ABI Version:", re->ehdr.e_ident[EI_ABIVERSION]);
25322c23cb7cSEd Maste 
25332c23cb7cSEd Maste 	/* e_type. */
25342c23cb7cSEd Maste 	printf("%-37s%s\n", "  Type:", elf_type(re->ehdr.e_type));
25352c23cb7cSEd Maste 
25362c23cb7cSEd Maste 	/* e_machine. */
25372c23cb7cSEd Maste 	printf("%-37s%s\n", "  Machine:", elf_machine(re->ehdr.e_machine));
25382c23cb7cSEd Maste 
25392c23cb7cSEd Maste 	/* e_version. */
25402c23cb7cSEd Maste 	printf("%-37s%#x\n", "  Version:", re->ehdr.e_version);
25412c23cb7cSEd Maste 
25422c23cb7cSEd Maste 	/* e_entry. */
25432c23cb7cSEd Maste 	printf("%-37s%#jx\n", "  Entry point address:",
25442c23cb7cSEd Maste 	    (uintmax_t)re->ehdr.e_entry);
25452c23cb7cSEd Maste 
25462c23cb7cSEd Maste 	/* e_phoff. */
25472c23cb7cSEd Maste 	printf("%-37s%ju (bytes into file)\n", "  Start of program headers:",
25482c23cb7cSEd Maste 	    (uintmax_t)re->ehdr.e_phoff);
25492c23cb7cSEd Maste 
25502c23cb7cSEd Maste 	/* e_shoff. */
25512c23cb7cSEd Maste 	printf("%-37s%ju (bytes into file)\n", "  Start of section headers:",
25522c23cb7cSEd Maste 	    (uintmax_t)re->ehdr.e_shoff);
25532c23cb7cSEd Maste 
25542c23cb7cSEd Maste 	/* e_flags. */
25552c23cb7cSEd Maste 	printf("%-37s%#x", "  Flags:", re->ehdr.e_flags);
25562c23cb7cSEd Maste 	dump_eflags(re, re->ehdr.e_flags);
25572c23cb7cSEd Maste 	putchar('\n');
25582c23cb7cSEd Maste 
25592c23cb7cSEd Maste 	/* e_ehsize. */
25602c23cb7cSEd Maste 	printf("%-37s%u (bytes)\n", "  Size of this header:",
25612c23cb7cSEd Maste 	    re->ehdr.e_ehsize);
25622c23cb7cSEd Maste 
25632c23cb7cSEd Maste 	/* e_phentsize. */
25642c23cb7cSEd Maste 	printf("%-37s%u (bytes)\n", "  Size of program headers:",
25652c23cb7cSEd Maste 	    re->ehdr.e_phentsize);
25662c23cb7cSEd Maste 
25672c23cb7cSEd Maste 	/* e_phnum. */
25682c23cb7cSEd Maste 	printf("%-37s%u\n", "  Number of program headers:", re->ehdr.e_phnum);
25692c23cb7cSEd Maste 
25702c23cb7cSEd Maste 	/* e_shentsize. */
25712c23cb7cSEd Maste 	printf("%-37s%u (bytes)\n", "  Size of section headers:",
25722c23cb7cSEd Maste 	    re->ehdr.e_shentsize);
25732c23cb7cSEd Maste 
25742c23cb7cSEd Maste 	/* e_shnum. */
25752c23cb7cSEd Maste 	printf("%-37s%u", "  Number of section headers:", re->ehdr.e_shnum);
25762c23cb7cSEd Maste 	if (re->ehdr.e_shnum == SHN_UNDEF) {
25772c23cb7cSEd Maste 		/* Extended section numbering is in use. */
25782c23cb7cSEd Maste 		if (elf_getshnum(re->elf, &shnum))
25792c23cb7cSEd Maste 			printf(" (%ju)", (uintmax_t)shnum);
25802c23cb7cSEd Maste 	}
25812c23cb7cSEd Maste 	putchar('\n');
25822c23cb7cSEd Maste 
25832c23cb7cSEd Maste 	/* e_shstrndx. */
25842c23cb7cSEd Maste 	printf("%-37s%u", "  Section header string table index:",
25852c23cb7cSEd Maste 	    re->ehdr.e_shstrndx);
25862c23cb7cSEd Maste 	if (re->ehdr.e_shstrndx == SHN_XINDEX) {
25872c23cb7cSEd Maste 		/* Extended section numbering is in use. */
25882c23cb7cSEd Maste 		if (elf_getshstrndx(re->elf, &shstrndx))
25892c23cb7cSEd Maste 			printf(" (%ju)", (uintmax_t)shstrndx);
25902c23cb7cSEd Maste 	}
25912c23cb7cSEd Maste 	putchar('\n');
25922c23cb7cSEd Maste }
25932c23cb7cSEd Maste 
25942c23cb7cSEd Maste static void
25952c23cb7cSEd Maste dump_eflags(struct readelf *re, uint64_t e_flags)
25962c23cb7cSEd Maste {
25972c23cb7cSEd Maste 	struct eflags_desc *edesc;
25982c23cb7cSEd Maste 	int arm_eabi;
25992c23cb7cSEd Maste 
26002c23cb7cSEd Maste 	edesc = NULL;
26012c23cb7cSEd Maste 	switch (re->ehdr.e_machine) {
26022c23cb7cSEd Maste 	case EM_ARM:
26032c23cb7cSEd Maste 		arm_eabi = (e_flags & EF_ARM_EABIMASK) >> 24;
26042c23cb7cSEd Maste 		if (arm_eabi == 0)
26052c23cb7cSEd Maste 			printf(", GNU EABI");
26062c23cb7cSEd Maste 		else if (arm_eabi <= 5)
26072c23cb7cSEd Maste 			printf(", Version%d EABI", arm_eabi);
26082c23cb7cSEd Maste 		edesc = arm_eflags_desc;
26092c23cb7cSEd Maste 		break;
26102c23cb7cSEd Maste 	case EM_MIPS:
26112c23cb7cSEd Maste 	case EM_MIPS_RS3_LE:
26122c23cb7cSEd Maste 		switch ((e_flags & EF_MIPS_ARCH) >> 28) {
26132c23cb7cSEd Maste 		case 0:	printf(", mips1"); break;
26142c23cb7cSEd Maste 		case 1: printf(", mips2"); break;
26152c23cb7cSEd Maste 		case 2: printf(", mips3"); break;
26162c23cb7cSEd Maste 		case 3: printf(", mips4"); break;
26172c23cb7cSEd Maste 		case 4: printf(", mips5"); break;
26182c23cb7cSEd Maste 		case 5: printf(", mips32"); break;
26192c23cb7cSEd Maste 		case 6: printf(", mips64"); break;
26202c23cb7cSEd Maste 		case 7: printf(", mips32r2"); break;
26212c23cb7cSEd Maste 		case 8: printf(", mips64r2"); break;
26222c23cb7cSEd Maste 		default: break;
26232c23cb7cSEd Maste 		}
26242c23cb7cSEd Maste 		switch ((e_flags & 0x00FF0000) >> 16) {
26252c23cb7cSEd Maste 		case 0x81: printf(", 3900"); break;
26262c23cb7cSEd Maste 		case 0x82: printf(", 4010"); break;
26272c23cb7cSEd Maste 		case 0x83: printf(", 4100"); break;
26282c23cb7cSEd Maste 		case 0x85: printf(", 4650"); break;
26292c23cb7cSEd Maste 		case 0x87: printf(", 4120"); break;
26302c23cb7cSEd Maste 		case 0x88: printf(", 4111"); break;
26312c23cb7cSEd Maste 		case 0x8a: printf(", sb1"); break;
26322c23cb7cSEd Maste 		case 0x8b: printf(", octeon"); break;
26332c23cb7cSEd Maste 		case 0x8c: printf(", xlr"); break;
26342c23cb7cSEd Maste 		case 0x91: printf(", 5400"); break;
26352c23cb7cSEd Maste 		case 0x98: printf(", 5500"); break;
26362c23cb7cSEd Maste 		case 0x99: printf(", 9000"); break;
26372c23cb7cSEd Maste 		case 0xa0: printf(", loongson-2e"); break;
26382c23cb7cSEd Maste 		case 0xa1: printf(", loongson-2f"); break;
26392c23cb7cSEd Maste 		default: break;
26402c23cb7cSEd Maste 		}
26412c23cb7cSEd Maste 		switch ((e_flags & 0x0000F000) >> 12) {
26422c23cb7cSEd Maste 		case 1: printf(", o32"); break;
26432c23cb7cSEd Maste 		case 2: printf(", o64"); break;
26442c23cb7cSEd Maste 		case 3: printf(", eabi32"); break;
26452c23cb7cSEd Maste 		case 4: printf(", eabi64"); break;
26462c23cb7cSEd Maste 		default: break;
26472c23cb7cSEd Maste 		}
26482c23cb7cSEd Maste 		edesc = mips_eflags_desc;
26492c23cb7cSEd Maste 		break;
26502c23cb7cSEd Maste 	case EM_PPC:
26512c23cb7cSEd Maste 	case EM_PPC64:
26522c23cb7cSEd Maste 		edesc = powerpc_eflags_desc;
26532c23cb7cSEd Maste 		break;
26542c23cb7cSEd Maste 	case EM_SPARC:
26552c23cb7cSEd Maste 	case EM_SPARC32PLUS:
26562c23cb7cSEd Maste 	case EM_SPARCV9:
26572c23cb7cSEd Maste 		switch ((e_flags & EF_SPARCV9_MM)) {
26582c23cb7cSEd Maste 		case EF_SPARCV9_TSO: printf(", tso"); break;
26592c23cb7cSEd Maste 		case EF_SPARCV9_PSO: printf(", pso"); break;
26602c23cb7cSEd Maste 		case EF_SPARCV9_MM: printf(", rmo"); break;
26612c23cb7cSEd Maste 		default: break;
26622c23cb7cSEd Maste 		}
26632c23cb7cSEd Maste 		edesc = sparc_eflags_desc;
26642c23cb7cSEd Maste 		break;
26652c23cb7cSEd Maste 	default:
26662c23cb7cSEd Maste 		break;
26672c23cb7cSEd Maste 	}
26682c23cb7cSEd Maste 
26692c23cb7cSEd Maste 	if (edesc != NULL) {
26702c23cb7cSEd Maste 		while (edesc->desc != NULL) {
26712c23cb7cSEd Maste 			if (e_flags & edesc->flag)
26722c23cb7cSEd Maste 				printf(", %s", edesc->desc);
26732c23cb7cSEd Maste 			edesc++;
26742c23cb7cSEd Maste 		}
26752c23cb7cSEd Maste 	}
26762c23cb7cSEd Maste }
26772c23cb7cSEd Maste 
26782c23cb7cSEd Maste static void
26792c23cb7cSEd Maste dump_phdr(struct readelf *re)
26802c23cb7cSEd Maste {
26812c23cb7cSEd Maste 	const char	*rawfile;
26822c23cb7cSEd Maste 	GElf_Phdr	 phdr;
2683b00fe64fSEd Maste 	size_t		 phnum, size;
26842c23cb7cSEd Maste 	int		 i, j;
26852c23cb7cSEd Maste 
26862c23cb7cSEd Maste #define	PH_HDR	"Type", "Offset", "VirtAddr", "PhysAddr", "FileSiz",	\
26872c23cb7cSEd Maste 		"MemSiz", "Flg", "Align"
26882c23cb7cSEd Maste #define	PH_CT	phdr_type(phdr.p_type), (uintmax_t)phdr.p_offset,	\
26892c23cb7cSEd Maste 		(uintmax_t)phdr.p_vaddr, (uintmax_t)phdr.p_paddr,	\
26902c23cb7cSEd Maste 		(uintmax_t)phdr.p_filesz, (uintmax_t)phdr.p_memsz,	\
26912c23cb7cSEd Maste 		phdr.p_flags & PF_R ? 'R' : ' ',			\
26922c23cb7cSEd Maste 		phdr.p_flags & PF_W ? 'W' : ' ',			\
26932c23cb7cSEd Maste 		phdr.p_flags & PF_X ? 'E' : ' ',			\
26942c23cb7cSEd Maste 		(uintmax_t)phdr.p_align
26952c23cb7cSEd Maste 
26962c23cb7cSEd Maste 	if (elf_getphnum(re->elf, &phnum) == 0) {
26972c23cb7cSEd Maste 		warnx("elf_getphnum failed: %s", elf_errmsg(-1));
26982c23cb7cSEd Maste 		return;
26992c23cb7cSEd Maste 	}
27002c23cb7cSEd Maste 	if (phnum == 0) {
27012c23cb7cSEd Maste 		printf("\nThere are no program headers in this file.\n");
27022c23cb7cSEd Maste 		return;
27032c23cb7cSEd Maste 	}
27042c23cb7cSEd Maste 
27052c23cb7cSEd Maste 	printf("\nElf file type is %s", elf_type(re->ehdr.e_type));
27062c23cb7cSEd Maste 	printf("\nEntry point 0x%jx\n", (uintmax_t)re->ehdr.e_entry);
27072c23cb7cSEd Maste 	printf("There are %ju program headers, starting at offset %ju\n",
27082c23cb7cSEd Maste 	    (uintmax_t)phnum, (uintmax_t)re->ehdr.e_phoff);
27092c23cb7cSEd Maste 
27102c23cb7cSEd Maste 	/* Dump program headers. */
27112c23cb7cSEd Maste 	printf("\nProgram Headers:\n");
27122c23cb7cSEd Maste 	if (re->ec == ELFCLASS32)
27132c23cb7cSEd Maste 		printf("  %-15s%-9s%-11s%-11s%-8s%-8s%-4s%s\n", PH_HDR);
27142c23cb7cSEd Maste 	else if (re->options & RE_WW)
27152c23cb7cSEd Maste 		printf("  %-15s%-9s%-19s%-19s%-9s%-9s%-4s%s\n", PH_HDR);
27162c23cb7cSEd Maste 	else
27172c23cb7cSEd Maste 		printf("  %-15s%-19s%-19s%s\n                 %-19s%-20s"
27182c23cb7cSEd Maste 		    "%-7s%s\n", PH_HDR);
27192c23cb7cSEd Maste 	for (i = 0; (size_t) i < phnum; i++) {
27202c23cb7cSEd Maste 		if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
27212c23cb7cSEd Maste 			warnx("gelf_getphdr failed: %s", elf_errmsg(-1));
27222c23cb7cSEd Maste 			continue;
27232c23cb7cSEd Maste 		}
27242c23cb7cSEd Maste 		/* TODO: Add arch-specific segment type dump. */
27252c23cb7cSEd Maste 		if (re->ec == ELFCLASS32)
27262c23cb7cSEd Maste 			printf("  %-14.14s 0x%6.6jx 0x%8.8jx 0x%8.8jx "
27272c23cb7cSEd Maste 			    "0x%5.5jx 0x%5.5jx %c%c%c %#jx\n", PH_CT);
27282c23cb7cSEd Maste 		else if (re->options & RE_WW)
27292c23cb7cSEd Maste 			printf("  %-14.14s 0x%6.6jx 0x%16.16jx 0x%16.16jx "
27302c23cb7cSEd Maste 			    "0x%6.6jx 0x%6.6jx %c%c%c %#jx\n", PH_CT);
27312c23cb7cSEd Maste 		else
27322c23cb7cSEd Maste 			printf("  %-14.14s 0x%16.16jx 0x%16.16jx 0x%16.16jx\n"
27332c23cb7cSEd Maste 			    "                 0x%16.16jx 0x%16.16jx  %c%c%c"
27342c23cb7cSEd Maste 			    "    %#jx\n", PH_CT);
27352c23cb7cSEd Maste 		if (phdr.p_type == PT_INTERP) {
2736b00fe64fSEd Maste 			if ((rawfile = elf_rawfile(re->elf, &size)) == NULL) {
27372c23cb7cSEd Maste 				warnx("elf_rawfile failed: %s", elf_errmsg(-1));
27382c23cb7cSEd Maste 				continue;
27392c23cb7cSEd Maste 			}
2740b00fe64fSEd Maste 			if (phdr.p_offset >= size) {
2741b00fe64fSEd Maste 				warnx("invalid program header offset");
2742b00fe64fSEd Maste 				continue;
2743b00fe64fSEd Maste 			}
27442c23cb7cSEd Maste 			printf("      [Requesting program interpreter: %s]\n",
27452c23cb7cSEd Maste 				rawfile + phdr.p_offset);
27462c23cb7cSEd Maste 		}
27472c23cb7cSEd Maste 	}
27482c23cb7cSEd Maste 
27492c23cb7cSEd Maste 	/* Dump section to segment mapping. */
27502c23cb7cSEd Maste 	if (re->shnum == 0)
27512c23cb7cSEd Maste 		return;
27522c23cb7cSEd Maste 	printf("\n Section to Segment mapping:\n");
27532c23cb7cSEd Maste 	printf("  Segment Sections...\n");
27542c23cb7cSEd Maste 	for (i = 0; (size_t)i < phnum; i++) {
27552c23cb7cSEd Maste 		if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
27562c23cb7cSEd Maste 			warnx("gelf_getphdr failed: %s", elf_errmsg(-1));
27572c23cb7cSEd Maste 			continue;
27582c23cb7cSEd Maste 		}
27592c23cb7cSEd Maste 		printf("   %2.2d     ", i);
27602c23cb7cSEd Maste 		/* skip NULL section. */
27612c23cb7cSEd Maste 		for (j = 1; (size_t)j < re->shnum; j++)
27622c23cb7cSEd Maste 			if (re->sl[j].off >= phdr.p_offset &&
27632c23cb7cSEd Maste 			    re->sl[j].off + re->sl[j].sz <=
27642c23cb7cSEd Maste 			    phdr.p_offset + phdr.p_memsz)
27652c23cb7cSEd Maste 				printf("%s ", re->sl[j].name);
27662c23cb7cSEd Maste 		printf("\n");
27672c23cb7cSEd Maste 	}
27682c23cb7cSEd Maste #undef	PH_HDR
27692c23cb7cSEd Maste #undef	PH_CT
27702c23cb7cSEd Maste }
27712c23cb7cSEd Maste 
27722c23cb7cSEd Maste static char *
27732c23cb7cSEd Maste section_flags(struct readelf *re, struct section *s)
27742c23cb7cSEd Maste {
27752c23cb7cSEd Maste #define BUF_SZ 256
27762c23cb7cSEd Maste 	static char	buf[BUF_SZ];
27772c23cb7cSEd Maste 	int		i, p, nb;
27782c23cb7cSEd Maste 
27792c23cb7cSEd Maste 	p = 0;
27802c23cb7cSEd Maste 	nb = re->ec == ELFCLASS32 ? 8 : 16;
27812c23cb7cSEd Maste 	if (re->options & RE_T) {
27822c23cb7cSEd Maste 		snprintf(buf, BUF_SZ, "[%*.*jx]: ", nb, nb,
27832c23cb7cSEd Maste 		    (uintmax_t)s->flags);
27842c23cb7cSEd Maste 		p += nb + 4;
27852c23cb7cSEd Maste 	}
27862c23cb7cSEd Maste 	for (i = 0; section_flag[i].ln != NULL; i++) {
27872c23cb7cSEd Maste 		if ((s->flags & section_flag[i].value) == 0)
27882c23cb7cSEd Maste 			continue;
27892c23cb7cSEd Maste 		if (re->options & RE_T) {
27902c23cb7cSEd Maste 			snprintf(&buf[p], BUF_SZ - p, "%s, ",
27912c23cb7cSEd Maste 			    section_flag[i].ln);
27922c23cb7cSEd Maste 			p += strlen(section_flag[i].ln) + 2;
27932c23cb7cSEd Maste 		} else
27942c23cb7cSEd Maste 			buf[p++] = section_flag[i].sn;
27952c23cb7cSEd Maste 	}
27962c23cb7cSEd Maste 	if (re->options & RE_T && p > nb + 4)
27972c23cb7cSEd Maste 		p -= 2;
27982c23cb7cSEd Maste 	buf[p] = '\0';
27992c23cb7cSEd Maste 
28002c23cb7cSEd Maste 	return (buf);
28012c23cb7cSEd Maste }
28022c23cb7cSEd Maste 
28032c23cb7cSEd Maste static void
28042c23cb7cSEd Maste dump_shdr(struct readelf *re)
28052c23cb7cSEd Maste {
28062c23cb7cSEd Maste 	struct section	*s;
28072c23cb7cSEd Maste 	int		 i;
28082c23cb7cSEd Maste 
28092c23cb7cSEd Maste #define	S_HDR	"[Nr] Name", "Type", "Addr", "Off", "Size", "ES",	\
28102c23cb7cSEd Maste 		"Flg", "Lk", "Inf", "Al"
28112c23cb7cSEd Maste #define	S_HDRL	"[Nr] Name", "Type", "Address", "Offset", "Size",	\
28122c23cb7cSEd Maste 		"EntSize", "Flags", "Link", "Info", "Align"
28132c23cb7cSEd Maste #define	ST_HDR	"[Nr] Name", "Type", "Addr", "Off", "Size", "ES",	\
28142c23cb7cSEd Maste 		"Lk", "Inf", "Al", "Flags"
28152c23cb7cSEd Maste #define	ST_HDRL	"[Nr] Name", "Type", "Address", "Offset", "Link",	\
28162c23cb7cSEd Maste 		"Size", "EntSize", "Info", "Align", "Flags"
28172c23cb7cSEd Maste #define	S_CT	i, s->name, section_type(re->ehdr.e_machine, s->type),	\
28182c23cb7cSEd Maste 		(uintmax_t)s->addr, (uintmax_t)s->off, (uintmax_t)s->sz,\
28192c23cb7cSEd Maste 		(uintmax_t)s->entsize, section_flags(re, s),		\
28202c23cb7cSEd Maste 		s->link, s->info, (uintmax_t)s->align
28212c23cb7cSEd Maste #define	ST_CT	i, s->name, section_type(re->ehdr.e_machine, s->type),  \
28222c23cb7cSEd Maste 		(uintmax_t)s->addr, (uintmax_t)s->off, (uintmax_t)s->sz,\
28232c23cb7cSEd Maste 		(uintmax_t)s->entsize, s->link, s->info,		\
28242c23cb7cSEd Maste 		(uintmax_t)s->align, section_flags(re, s)
28252c23cb7cSEd Maste #define	ST_CTL	i, s->name, section_type(re->ehdr.e_machine, s->type),  \
28262c23cb7cSEd Maste 		(uintmax_t)s->addr, (uintmax_t)s->off, s->link,		\
28272c23cb7cSEd Maste 		(uintmax_t)s->sz, (uintmax_t)s->entsize, s->info,	\
28282c23cb7cSEd Maste 		(uintmax_t)s->align, section_flags(re, s)
28292c23cb7cSEd Maste 
28302c23cb7cSEd Maste 	if (re->shnum == 0) {
28312c23cb7cSEd Maste 		printf("\nThere are no sections in this file.\n");
28322c23cb7cSEd Maste 		return;
28332c23cb7cSEd Maste 	}
28342c23cb7cSEd Maste 	printf("There are %ju section headers, starting at offset 0x%jx:\n",
28352c23cb7cSEd Maste 	    (uintmax_t)re->shnum, (uintmax_t)re->ehdr.e_shoff);
28362c23cb7cSEd Maste 	printf("\nSection Headers:\n");
28372c23cb7cSEd Maste 	if (re->ec == ELFCLASS32) {
28382c23cb7cSEd Maste 		if (re->options & RE_T)
28392c23cb7cSEd Maste 			printf("  %s\n       %-16s%-9s%-7s%-7s%-5s%-3s%-4s%s\n"
28402c23cb7cSEd Maste 			    "%12s\n", ST_HDR);
28412c23cb7cSEd Maste 		else
28422c23cb7cSEd Maste 			printf("  %-23s%-16s%-9s%-7s%-7s%-3s%-4s%-3s%-4s%s\n",
28432c23cb7cSEd Maste 			    S_HDR);
28442c23cb7cSEd Maste 	} else if (re->options & RE_WW) {
28452c23cb7cSEd Maste 		if (re->options & RE_T)
28462c23cb7cSEd Maste 			printf("  %s\n       %-16s%-17s%-7s%-7s%-5s%-3s%-4s%s\n"
28472c23cb7cSEd Maste 			    "%12s\n", ST_HDR);
28482c23cb7cSEd Maste 		else
28492c23cb7cSEd Maste 			printf("  %-23s%-16s%-17s%-7s%-7s%-3s%-4s%-3s%-4s%s\n",
28502c23cb7cSEd Maste 			    S_HDR);
28512c23cb7cSEd Maste 	} else {
28522c23cb7cSEd Maste 		if (re->options & RE_T)
28532c23cb7cSEd Maste 			printf("  %s\n       %-18s%-17s%-18s%s\n       %-18s"
28542c23cb7cSEd Maste 			    "%-17s%-18s%s\n%12s\n", ST_HDRL);
28552c23cb7cSEd Maste 		else
28562c23cb7cSEd Maste 			printf("  %-23s%-17s%-18s%s\n       %-18s%-17s%-7s%"
28572c23cb7cSEd Maste 			    "-6s%-6s%s\n", S_HDRL);
28582c23cb7cSEd Maste 	}
28592c23cb7cSEd Maste 	for (i = 0; (size_t)i < re->shnum; i++) {
28602c23cb7cSEd Maste 		s = &re->sl[i];
28612c23cb7cSEd Maste 		if (re->ec == ELFCLASS32) {
28622c23cb7cSEd Maste 			if (re->options & RE_T)
28632c23cb7cSEd Maste 				printf("  [%2d] %s\n       %-15.15s %8.8jx"
28642c23cb7cSEd Maste 				    " %6.6jx %6.6jx %2.2jx  %2u %3u %2ju\n"
28652c23cb7cSEd Maste 				    "       %s\n", ST_CT);
28662c23cb7cSEd Maste 			else
28672c23cb7cSEd Maste 				printf("  [%2d] %-17.17s %-15.15s %8.8jx"
28682c23cb7cSEd Maste 				    " %6.6jx %6.6jx %2.2jx %3s %2u %3u %2ju\n",
28692c23cb7cSEd Maste 				    S_CT);
28702c23cb7cSEd Maste 		} else if (re->options & RE_WW) {
28712c23cb7cSEd Maste 			if (re->options & RE_T)
28722c23cb7cSEd Maste 				printf("  [%2d] %s\n       %-15.15s %16.16jx"
28732c23cb7cSEd Maste 				    " %6.6jx %6.6jx %2.2jx  %2u %3u %2ju\n"
28742c23cb7cSEd Maste 				    "       %s\n", ST_CT);
28752c23cb7cSEd Maste 			else
28762c23cb7cSEd Maste 				printf("  [%2d] %-17.17s %-15.15s %16.16jx"
28772c23cb7cSEd Maste 				    " %6.6jx %6.6jx %2.2jx %3s %2u %3u %2ju\n",
28782c23cb7cSEd Maste 				    S_CT);
28792c23cb7cSEd Maste 		} else {
28802c23cb7cSEd Maste 			if (re->options & RE_T)
28812c23cb7cSEd Maste 				printf("  [%2d] %s\n       %-15.15s  %16.16jx"
28822c23cb7cSEd Maste 				    "  %16.16jx  %u\n       %16.16jx %16.16jx"
28832c23cb7cSEd Maste 				    "  %-16u  %ju\n       %s\n", ST_CTL);
28842c23cb7cSEd Maste 			else
28852c23cb7cSEd Maste 				printf("  [%2d] %-17.17s %-15.15s  %16.16jx"
28862c23cb7cSEd Maste 				    "  %8.8jx\n       %16.16jx  %16.16jx "
28872c23cb7cSEd Maste 				    "%3s      %2u   %3u     %ju\n", S_CT);
28882c23cb7cSEd Maste 		}
28892c23cb7cSEd Maste 	}
28902c23cb7cSEd Maste 	if ((re->options & RE_T) == 0)
28912c23cb7cSEd Maste 		printf("Key to Flags:\n  W (write), A (alloc),"
28922c23cb7cSEd Maste 		    " X (execute), M (merge), S (strings)\n"
28932c23cb7cSEd Maste 		    "  I (info), L (link order), G (group), x (unknown)\n"
28942c23cb7cSEd Maste 		    "  O (extra OS processing required)"
28952c23cb7cSEd Maste 		    " o (OS specific), p (processor specific)\n");
28962c23cb7cSEd Maste 
28972c23cb7cSEd Maste #undef	S_HDR
28982c23cb7cSEd Maste #undef	S_HDRL
28992c23cb7cSEd Maste #undef	ST_HDR
29002c23cb7cSEd Maste #undef	ST_HDRL
29012c23cb7cSEd Maste #undef	S_CT
29022c23cb7cSEd Maste #undef	ST_CT
29032c23cb7cSEd Maste #undef	ST_CTL
29042c23cb7cSEd Maste }
29052c23cb7cSEd Maste 
290671edbbfdSEd Maste /*
290771edbbfdSEd Maste  * Return number of entries in the given section. We'd prefer ent_count be a
290871edbbfdSEd Maste  * size_t *, but libelf APIs already use int for section indices.
290971edbbfdSEd Maste  */
291071edbbfdSEd Maste static int
291171edbbfdSEd Maste get_ent_count(struct section *s, int *ent_count)
291271edbbfdSEd Maste {
291371edbbfdSEd Maste 	if (s->entsize == 0) {
291471edbbfdSEd Maste 		warnx("section %s has entry size 0", s->name);
291571edbbfdSEd Maste 		return (0);
291671edbbfdSEd Maste 	} else if (s->sz / s->entsize > INT_MAX) {
291771edbbfdSEd Maste 		warnx("section %s has invalid section count", s->name);
291871edbbfdSEd Maste 		return (0);
291971edbbfdSEd Maste 	}
292071edbbfdSEd Maste 	*ent_count = (int)(s->sz / s->entsize);
292171edbbfdSEd Maste 	return (1);
292271edbbfdSEd Maste }
292371edbbfdSEd Maste 
29242c23cb7cSEd Maste static void
29252c23cb7cSEd Maste dump_dynamic(struct readelf *re)
29262c23cb7cSEd Maste {
29272c23cb7cSEd Maste 	GElf_Dyn	 dyn;
29282c23cb7cSEd Maste 	Elf_Data	*d;
29292c23cb7cSEd Maste 	struct section	*s;
29302c23cb7cSEd Maste 	int		 elferr, i, is_dynamic, j, jmax, nentries;
29312c23cb7cSEd Maste 
29322c23cb7cSEd Maste 	is_dynamic = 0;
29332c23cb7cSEd Maste 
29342c23cb7cSEd Maste 	for (i = 0; (size_t)i < re->shnum; i++) {
29352c23cb7cSEd Maste 		s = &re->sl[i];
29362c23cb7cSEd Maste 		if (s->type != SHT_DYNAMIC)
29372c23cb7cSEd Maste 			continue;
29382c23cb7cSEd Maste 		(void) elf_errno();
29392c23cb7cSEd Maste 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
29402c23cb7cSEd Maste 			elferr = elf_errno();
29412c23cb7cSEd Maste 			if (elferr != 0)
29422c23cb7cSEd Maste 				warnx("elf_getdata failed: %s", elf_errmsg(-1));
29432c23cb7cSEd Maste 			continue;
29442c23cb7cSEd Maste 		}
29452c23cb7cSEd Maste 		if (d->d_size <= 0)
29462c23cb7cSEd Maste 			continue;
29472c23cb7cSEd Maste 
29482c23cb7cSEd Maste 		is_dynamic = 1;
29492c23cb7cSEd Maste 
29502c23cb7cSEd Maste 		/* Determine the actual number of table entries. */
29512c23cb7cSEd Maste 		nentries = 0;
295271edbbfdSEd Maste 		if (!get_ent_count(s, &jmax))
295371edbbfdSEd Maste 			continue;
29542c23cb7cSEd Maste 		for (j = 0; j < jmax; j++) {
29552c23cb7cSEd Maste 			if (gelf_getdyn(d, j, &dyn) != &dyn) {
29562c23cb7cSEd Maste 				warnx("gelf_getdyn failed: %s",
29572c23cb7cSEd Maste 				    elf_errmsg(-1));
29582c23cb7cSEd Maste 				continue;
29592c23cb7cSEd Maste 			}
29602c23cb7cSEd Maste 			nentries ++;
29612c23cb7cSEd Maste 			if (dyn.d_tag == DT_NULL)
29622c23cb7cSEd Maste 				break;
29632c23cb7cSEd Maste                 }
29642c23cb7cSEd Maste 
29652c23cb7cSEd Maste 		printf("\nDynamic section at offset 0x%jx", (uintmax_t)s->off);
29662c23cb7cSEd Maste 		printf(" contains %u entries:\n", nentries);
29672c23cb7cSEd Maste 
29682c23cb7cSEd Maste 		if (re->ec == ELFCLASS32)
29692c23cb7cSEd Maste 			printf("%5s%12s%28s\n", "Tag", "Type", "Name/Value");
29702c23cb7cSEd Maste 		else
29712c23cb7cSEd Maste 			printf("%5s%20s%28s\n", "Tag", "Type", "Name/Value");
29722c23cb7cSEd Maste 
29732c23cb7cSEd Maste 		for (j = 0; j < nentries; j++) {
29742c23cb7cSEd Maste 			if (gelf_getdyn(d, j, &dyn) != &dyn)
29752c23cb7cSEd Maste 				continue;
29762c23cb7cSEd Maste 			/* Dump dynamic entry type. */
29772c23cb7cSEd Maste 			if (re->ec == ELFCLASS32)
29782c23cb7cSEd Maste 				printf(" 0x%8.8jx", (uintmax_t)dyn.d_tag);
29792c23cb7cSEd Maste 			else
29802c23cb7cSEd Maste 				printf(" 0x%16.16jx", (uintmax_t)dyn.d_tag);
29812c23cb7cSEd Maste 			printf(" %-20s", dt_type(re->ehdr.e_machine,
29822c23cb7cSEd Maste 			    dyn.d_tag));
29832c23cb7cSEd Maste 			/* Dump dynamic entry value. */
29842c23cb7cSEd Maste 			dump_dyn_val(re, &dyn, s->link);
29852c23cb7cSEd Maste 		}
29862c23cb7cSEd Maste 	}
29872c23cb7cSEd Maste 
29882c23cb7cSEd Maste 	if (!is_dynamic)
29892c23cb7cSEd Maste 		printf("\nThere is no dynamic section in this file.\n");
29902c23cb7cSEd Maste }
29912c23cb7cSEd Maste 
29922c23cb7cSEd Maste static char *
29932c23cb7cSEd Maste timestamp(time_t ti)
29942c23cb7cSEd Maste {
29952c23cb7cSEd Maste 	static char ts[32];
29962c23cb7cSEd Maste 	struct tm *t;
29972c23cb7cSEd Maste 
29982c23cb7cSEd Maste 	t = gmtime(&ti);
29992c23cb7cSEd Maste 	snprintf(ts, sizeof(ts), "%04d-%02d-%02dT%02d:%02d:%02d",
30002c23cb7cSEd Maste 	    t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour,
30012c23cb7cSEd Maste 	    t->tm_min, t->tm_sec);
30022c23cb7cSEd Maste 
30032c23cb7cSEd Maste 	return (ts);
30042c23cb7cSEd Maste }
30052c23cb7cSEd Maste 
30062c23cb7cSEd Maste static const char *
30072c23cb7cSEd Maste dyn_str(struct readelf *re, uint32_t stab, uint64_t d_val)
30082c23cb7cSEd Maste {
30092c23cb7cSEd Maste 	const char *name;
30102c23cb7cSEd Maste 
30112c23cb7cSEd Maste 	if (stab == SHN_UNDEF)
30122c23cb7cSEd Maste 		name = "ERROR";
30132c23cb7cSEd Maste 	else if ((name = elf_strptr(re->elf, stab, d_val)) == NULL) {
30142c23cb7cSEd Maste 		(void) elf_errno(); /* clear error */
30152c23cb7cSEd Maste 		name = "ERROR";
30162c23cb7cSEd Maste 	}
30172c23cb7cSEd Maste 
30182c23cb7cSEd Maste 	return (name);
30192c23cb7cSEd Maste }
30202c23cb7cSEd Maste 
30212c23cb7cSEd Maste static void
30222c23cb7cSEd Maste dump_arch_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab)
30232c23cb7cSEd Maste {
30242c23cb7cSEd Maste 	const char *name;
30252c23cb7cSEd Maste 
30262c23cb7cSEd Maste 	switch (re->ehdr.e_machine) {
30272c23cb7cSEd Maste 	case EM_MIPS:
30282c23cb7cSEd Maste 	case EM_MIPS_RS3_LE:
30292c23cb7cSEd Maste 		switch (dyn->d_tag) {
30302c23cb7cSEd Maste 		case DT_MIPS_RLD_VERSION:
30312c23cb7cSEd Maste 		case DT_MIPS_LOCAL_GOTNO:
30322c23cb7cSEd Maste 		case DT_MIPS_CONFLICTNO:
30332c23cb7cSEd Maste 		case DT_MIPS_LIBLISTNO:
30342c23cb7cSEd Maste 		case DT_MIPS_SYMTABNO:
30352c23cb7cSEd Maste 		case DT_MIPS_UNREFEXTNO:
30362c23cb7cSEd Maste 		case DT_MIPS_GOTSYM:
30372c23cb7cSEd Maste 		case DT_MIPS_HIPAGENO:
30382c23cb7cSEd Maste 		case DT_MIPS_DELTA_CLASS_NO:
30392c23cb7cSEd Maste 		case DT_MIPS_DELTA_INSTANCE_NO:
30402c23cb7cSEd Maste 		case DT_MIPS_DELTA_RELOC_NO:
30412c23cb7cSEd Maste 		case DT_MIPS_DELTA_SYM_NO:
30422c23cb7cSEd Maste 		case DT_MIPS_DELTA_CLASSSYM_NO:
30432c23cb7cSEd Maste 		case DT_MIPS_LOCALPAGE_GOTIDX:
30442c23cb7cSEd Maste 		case DT_MIPS_LOCAL_GOTIDX:
30452c23cb7cSEd Maste 		case DT_MIPS_HIDDEN_GOTIDX:
30462c23cb7cSEd Maste 		case DT_MIPS_PROTECTED_GOTIDX:
30472c23cb7cSEd Maste 			printf(" %ju\n", (uintmax_t) dyn->d_un.d_val);
30482c23cb7cSEd Maste 			break;
30492c23cb7cSEd Maste 		case DT_MIPS_ICHECKSUM:
30502c23cb7cSEd Maste 		case DT_MIPS_FLAGS:
30512c23cb7cSEd Maste 		case DT_MIPS_BASE_ADDRESS:
30522c23cb7cSEd Maste 		case DT_MIPS_CONFLICT:
30532c23cb7cSEd Maste 		case DT_MIPS_LIBLIST:
30542c23cb7cSEd Maste 		case DT_MIPS_RLD_MAP:
30552c23cb7cSEd Maste 		case DT_MIPS_DELTA_CLASS:
30562c23cb7cSEd Maste 		case DT_MIPS_DELTA_INSTANCE:
30572c23cb7cSEd Maste 		case DT_MIPS_DELTA_RELOC:
30582c23cb7cSEd Maste 		case DT_MIPS_DELTA_SYM:
30592c23cb7cSEd Maste 		case DT_MIPS_DELTA_CLASSSYM:
30602c23cb7cSEd Maste 		case DT_MIPS_CXX_FLAGS:
30612c23cb7cSEd Maste 		case DT_MIPS_PIXIE_INIT:
30622c23cb7cSEd Maste 		case DT_MIPS_SYMBOL_LIB:
30632c23cb7cSEd Maste 		case DT_MIPS_OPTIONS:
30642c23cb7cSEd Maste 		case DT_MIPS_INTERFACE:
30652c23cb7cSEd Maste 		case DT_MIPS_DYNSTR_ALIGN:
30662c23cb7cSEd Maste 		case DT_MIPS_INTERFACE_SIZE:
30672c23cb7cSEd Maste 		case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
30682c23cb7cSEd Maste 		case DT_MIPS_COMPACT_SIZE:
30692c23cb7cSEd Maste 		case DT_MIPS_GP_VALUE:
30702c23cb7cSEd Maste 		case DT_MIPS_AUX_DYNAMIC:
30712c23cb7cSEd Maste 		case DT_MIPS_PLTGOT:
30722c23cb7cSEd Maste 		case DT_MIPS_RLD_OBJ_UPDATE:
30732c23cb7cSEd Maste 		case DT_MIPS_RWPLT:
30742c23cb7cSEd Maste 			printf(" 0x%jx\n", (uintmax_t) dyn->d_un.d_val);
30752c23cb7cSEd Maste 			break;
30762c23cb7cSEd Maste 		case DT_MIPS_IVERSION:
30772c23cb7cSEd Maste 		case DT_MIPS_PERF_SUFFIX:
30782c23cb7cSEd Maste 		case DT_AUXILIARY:
30792c23cb7cSEd Maste 		case DT_FILTER:
30802c23cb7cSEd Maste 			name = dyn_str(re, stab, dyn->d_un.d_val);
30812c23cb7cSEd Maste 			printf(" %s\n", name);
30822c23cb7cSEd Maste 			break;
30832c23cb7cSEd Maste 		case DT_MIPS_TIME_STAMP:
30842c23cb7cSEd Maste 			printf(" %s\n", timestamp(dyn->d_un.d_val));
30852c23cb7cSEd Maste 			break;
30862c23cb7cSEd Maste 		}
30872c23cb7cSEd Maste 		break;
30882c23cb7cSEd Maste 	default:
30892c23cb7cSEd Maste 		printf("\n");
30902c23cb7cSEd Maste 		break;
30912c23cb7cSEd Maste 	}
30922c23cb7cSEd Maste }
30932c23cb7cSEd Maste 
30942c23cb7cSEd Maste static void
30952c23cb7cSEd Maste dump_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab)
30962c23cb7cSEd Maste {
30972c23cb7cSEd Maste 	const char *name;
30982c23cb7cSEd Maste 
30992c23cb7cSEd Maste 	if (dyn->d_tag >= DT_LOPROC && dyn->d_tag <= DT_HIPROC) {
31002c23cb7cSEd Maste 		dump_arch_dyn_val(re, dyn, stab);
31012c23cb7cSEd Maste 		return;
31022c23cb7cSEd Maste 	}
31032c23cb7cSEd Maste 
31042c23cb7cSEd Maste 	/* These entry values are index into the string table. */
31052c23cb7cSEd Maste 	name = NULL;
31062c23cb7cSEd Maste 	if (dyn->d_tag == DT_NEEDED || dyn->d_tag == DT_SONAME ||
31072c23cb7cSEd Maste 	    dyn->d_tag == DT_RPATH || dyn->d_tag == DT_RUNPATH)
31082c23cb7cSEd Maste 		name = dyn_str(re, stab, dyn->d_un.d_val);
31092c23cb7cSEd Maste 
31102c23cb7cSEd Maste 	switch(dyn->d_tag) {
31112c23cb7cSEd Maste 	case DT_NULL:
31122c23cb7cSEd Maste 	case DT_PLTGOT:
31132c23cb7cSEd Maste 	case DT_HASH:
31142c23cb7cSEd Maste 	case DT_STRTAB:
31152c23cb7cSEd Maste 	case DT_SYMTAB:
31162c23cb7cSEd Maste 	case DT_RELA:
31172c23cb7cSEd Maste 	case DT_INIT:
31182c23cb7cSEd Maste 	case DT_SYMBOLIC:
31192c23cb7cSEd Maste 	case DT_REL:
31202c23cb7cSEd Maste 	case DT_DEBUG:
31212c23cb7cSEd Maste 	case DT_TEXTREL:
31222c23cb7cSEd Maste 	case DT_JMPREL:
31232c23cb7cSEd Maste 	case DT_FINI:
31242c23cb7cSEd Maste 	case DT_VERDEF:
31252c23cb7cSEd Maste 	case DT_VERNEED:
31262c23cb7cSEd Maste 	case DT_VERSYM:
31272c23cb7cSEd Maste 	case DT_GNU_HASH:
31282c23cb7cSEd Maste 	case DT_GNU_LIBLIST:
31292c23cb7cSEd Maste 	case DT_GNU_CONFLICT:
31302c23cb7cSEd Maste 		printf(" 0x%jx\n", (uintmax_t) dyn->d_un.d_val);
31312c23cb7cSEd Maste 		break;
31322c23cb7cSEd Maste 	case DT_PLTRELSZ:
31332c23cb7cSEd Maste 	case DT_RELASZ:
31342c23cb7cSEd Maste 	case DT_RELAENT:
31352c23cb7cSEd Maste 	case DT_STRSZ:
31362c23cb7cSEd Maste 	case DT_SYMENT:
31372c23cb7cSEd Maste 	case DT_RELSZ:
31382c23cb7cSEd Maste 	case DT_RELENT:
31392c23cb7cSEd Maste 	case DT_INIT_ARRAYSZ:
31402c23cb7cSEd Maste 	case DT_FINI_ARRAYSZ:
31412c23cb7cSEd Maste 	case DT_GNU_CONFLICTSZ:
31422c23cb7cSEd Maste 	case DT_GNU_LIBLISTSZ:
31432c23cb7cSEd Maste 		printf(" %ju (bytes)\n", (uintmax_t) dyn->d_un.d_val);
31442c23cb7cSEd Maste 		break;
31452c23cb7cSEd Maste  	case DT_RELACOUNT:
31462c23cb7cSEd Maste 	case DT_RELCOUNT:
31472c23cb7cSEd Maste 	case DT_VERDEFNUM:
31482c23cb7cSEd Maste 	case DT_VERNEEDNUM:
31492c23cb7cSEd Maste 		printf(" %ju\n", (uintmax_t) dyn->d_un.d_val);
31502c23cb7cSEd Maste 		break;
31512c23cb7cSEd Maste 	case DT_NEEDED:
31522c23cb7cSEd Maste 		printf(" Shared library: [%s]\n", name);
31532c23cb7cSEd Maste 		break;
31542c23cb7cSEd Maste 	case DT_SONAME:
31552c23cb7cSEd Maste 		printf(" Library soname: [%s]\n", name);
31562c23cb7cSEd Maste 		break;
31572c23cb7cSEd Maste 	case DT_RPATH:
31582c23cb7cSEd Maste 		printf(" Library rpath: [%s]\n", name);
31592c23cb7cSEd Maste 		break;
31602c23cb7cSEd Maste 	case DT_RUNPATH:
31612c23cb7cSEd Maste 		printf(" Library runpath: [%s]\n", name);
31622c23cb7cSEd Maste 		break;
31632c23cb7cSEd Maste 	case DT_PLTREL:
31642c23cb7cSEd Maste 		printf(" %s\n", dt_type(re->ehdr.e_machine, dyn->d_un.d_val));
31652c23cb7cSEd Maste 		break;
31662c23cb7cSEd Maste 	case DT_GNU_PRELINKED:
31672c23cb7cSEd Maste 		printf(" %s\n", timestamp(dyn->d_un.d_val));
31682c23cb7cSEd Maste 		break;
31692c23cb7cSEd Maste 	default:
31702c23cb7cSEd Maste 		printf("\n");
31712c23cb7cSEd Maste 	}
31722c23cb7cSEd Maste }
31732c23cb7cSEd Maste 
31742c23cb7cSEd Maste static void
31752c23cb7cSEd Maste dump_rel(struct readelf *re, struct section *s, Elf_Data *d)
31762c23cb7cSEd Maste {
31772c23cb7cSEd Maste 	GElf_Rel r;
31782c23cb7cSEd Maste 	const char *symname;
31792c23cb7cSEd Maste 	uint64_t symval;
31802c23cb7cSEd Maste 	int i, len;
31812c23cb7cSEd Maste 
31822c23cb7cSEd Maste #define	REL_HDR "r_offset", "r_info", "r_type", "st_value", "st_name"
31832c23cb7cSEd Maste #define	REL_CT32 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
31842c23cb7cSEd Maste 		r_type(re->ehdr.e_machine, ELF32_R_TYPE(r.r_info)), \
31852c23cb7cSEd Maste 		(uintmax_t)symval, symname
31862c23cb7cSEd Maste #define	REL_CT64 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
31872c23cb7cSEd Maste 		r_type(re->ehdr.e_machine, ELF64_R_TYPE(r.r_info)), \
31882c23cb7cSEd Maste 		(uintmax_t)symval, symname
31892c23cb7cSEd Maste 
31902c23cb7cSEd Maste 	printf("\nRelocation section (%s):\n", s->name);
31912c23cb7cSEd Maste 	if (re->ec == ELFCLASS32)
31922c23cb7cSEd Maste 		printf("%-8s %-8s %-19s %-8s %s\n", REL_HDR);
31932c23cb7cSEd Maste 	else {
31942c23cb7cSEd Maste 		if (re->options & RE_WW)
31952c23cb7cSEd Maste 			printf("%-16s %-16s %-24s %-16s %s\n", REL_HDR);
31962c23cb7cSEd Maste 		else
31972c23cb7cSEd Maste 			printf("%-12s %-12s %-19s %-16s %s\n", REL_HDR);
31982c23cb7cSEd Maste 	}
319971edbbfdSEd Maste 	assert(d->d_size == s->sz);
320071edbbfdSEd Maste 	if (!get_ent_count(s, &len))
320171edbbfdSEd Maste 		return;
32022c23cb7cSEd Maste 	for (i = 0; i < len; i++) {
32032c23cb7cSEd Maste 		if (gelf_getrel(d, i, &r) != &r) {
32042c23cb7cSEd Maste 			warnx("gelf_getrel failed: %s", elf_errmsg(-1));
32052c23cb7cSEd Maste 			continue;
32062c23cb7cSEd Maste 		}
320771a0c925SEd Maste 		if (s->link >= re->shnum) {
320871a0c925SEd Maste 			warnx("invalid section link index %u", s->link);
320971a0c925SEd Maste 			continue;
321071a0c925SEd Maste 		}
32112c23cb7cSEd Maste 		symname = get_symbol_name(re, s->link, GELF_R_SYM(r.r_info));
32122c23cb7cSEd Maste 		symval = get_symbol_value(re, s->link, GELF_R_SYM(r.r_info));
32132c23cb7cSEd Maste 		if (re->ec == ELFCLASS32) {
32142c23cb7cSEd Maste 			r.r_info = ELF32_R_INFO(ELF64_R_SYM(r.r_info),
32152c23cb7cSEd Maste 			    ELF64_R_TYPE(r.r_info));
32162c23cb7cSEd Maste 			printf("%8.8jx %8.8jx %-19.19s %8.8jx %s\n", REL_CT32);
32172c23cb7cSEd Maste 		} else {
32182c23cb7cSEd Maste 			if (re->options & RE_WW)
32192c23cb7cSEd Maste 				printf("%16.16jx %16.16jx %-24.24s"
32202c23cb7cSEd Maste 				    " %16.16jx %s\n", REL_CT64);
32212c23cb7cSEd Maste 			else
32222c23cb7cSEd Maste 				printf("%12.12jx %12.12jx %-19.19s"
32232c23cb7cSEd Maste 				    " %16.16jx %s\n", REL_CT64);
32242c23cb7cSEd Maste 		}
32252c23cb7cSEd Maste 	}
32262c23cb7cSEd Maste 
32272c23cb7cSEd Maste #undef	REL_HDR
32282c23cb7cSEd Maste #undef	REL_CT
32292c23cb7cSEd Maste }
32302c23cb7cSEd Maste 
32312c23cb7cSEd Maste static void
32322c23cb7cSEd Maste dump_rela(struct readelf *re, struct section *s, Elf_Data *d)
32332c23cb7cSEd Maste {
32342c23cb7cSEd Maste 	GElf_Rela r;
32352c23cb7cSEd Maste 	const char *symname;
32362c23cb7cSEd Maste 	uint64_t symval;
32372c23cb7cSEd Maste 	int i, len;
32382c23cb7cSEd Maste 
32392c23cb7cSEd Maste #define	RELA_HDR "r_offset", "r_info", "r_type", "st_value", \
32402c23cb7cSEd Maste 		"st_name + r_addend"
32412c23cb7cSEd Maste #define	RELA_CT32 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
32422c23cb7cSEd Maste 		r_type(re->ehdr.e_machine, ELF32_R_TYPE(r.r_info)), \
32432c23cb7cSEd Maste 		(uintmax_t)symval, symname
32442c23cb7cSEd Maste #define	RELA_CT64 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
32452c23cb7cSEd Maste 		r_type(re->ehdr.e_machine, ELF64_R_TYPE(r.r_info)), \
32462c23cb7cSEd Maste 		(uintmax_t)symval, symname
32472c23cb7cSEd Maste 
32482c23cb7cSEd Maste 	printf("\nRelocation section with addend (%s):\n", s->name);
32492c23cb7cSEd Maste 	if (re->ec == ELFCLASS32)
32502c23cb7cSEd Maste 		printf("%-8s %-8s %-19s %-8s %s\n", RELA_HDR);
32512c23cb7cSEd Maste 	else {
32522c23cb7cSEd Maste 		if (re->options & RE_WW)
32532c23cb7cSEd Maste 			printf("%-16s %-16s %-24s %-16s %s\n", RELA_HDR);
32542c23cb7cSEd Maste 		else
32552c23cb7cSEd Maste 			printf("%-12s %-12s %-19s %-16s %s\n", RELA_HDR);
32562c23cb7cSEd Maste 	}
325771edbbfdSEd Maste 	assert(d->d_size == s->sz);
325871edbbfdSEd Maste 	if (!get_ent_count(s, &len))
325971edbbfdSEd Maste 		return;
32602c23cb7cSEd Maste 	for (i = 0; i < len; i++) {
32612c23cb7cSEd Maste 		if (gelf_getrela(d, i, &r) != &r) {
32622c23cb7cSEd Maste 			warnx("gelf_getrel failed: %s", elf_errmsg(-1));
32632c23cb7cSEd Maste 			continue;
32642c23cb7cSEd Maste 		}
326571a0c925SEd Maste 		if (s->link >= re->shnum) {
326671a0c925SEd Maste 			warnx("invalid section link index %u", s->link);
326771a0c925SEd Maste 			continue;
326871a0c925SEd Maste 		}
32692c23cb7cSEd Maste 		symname = get_symbol_name(re, s->link, GELF_R_SYM(r.r_info));
32702c23cb7cSEd Maste 		symval = get_symbol_value(re, s->link, GELF_R_SYM(r.r_info));
32712c23cb7cSEd Maste 		if (re->ec == ELFCLASS32) {
32722c23cb7cSEd Maste 			r.r_info = ELF32_R_INFO(ELF64_R_SYM(r.r_info),
32732c23cb7cSEd Maste 			    ELF64_R_TYPE(r.r_info));
32742c23cb7cSEd Maste 			printf("%8.8jx %8.8jx %-19.19s %8.8jx %s", RELA_CT32);
32752c23cb7cSEd Maste 			printf(" + %x\n", (uint32_t) r.r_addend);
32762c23cb7cSEd Maste 		} else {
32772c23cb7cSEd Maste 			if (re->options & RE_WW)
32782c23cb7cSEd Maste 				printf("%16.16jx %16.16jx %-24.24s"
32792c23cb7cSEd Maste 				    " %16.16jx %s", RELA_CT64);
32802c23cb7cSEd Maste 			else
32812c23cb7cSEd Maste 				printf("%12.12jx %12.12jx %-19.19s"
32822c23cb7cSEd Maste 				    " %16.16jx %s", RELA_CT64);
32832c23cb7cSEd Maste 			printf(" + %jx\n", (uintmax_t) r.r_addend);
32842c23cb7cSEd Maste 		}
32852c23cb7cSEd Maste 	}
32862c23cb7cSEd Maste 
32872c23cb7cSEd Maste #undef	RELA_HDR
32882c23cb7cSEd Maste #undef	RELA_CT
32892c23cb7cSEd Maste }
32902c23cb7cSEd Maste 
32912c23cb7cSEd Maste static void
32922c23cb7cSEd Maste dump_reloc(struct readelf *re)
32932c23cb7cSEd Maste {
32942c23cb7cSEd Maste 	struct section *s;
32952c23cb7cSEd Maste 	Elf_Data *d;
32962c23cb7cSEd Maste 	int i, elferr;
32972c23cb7cSEd Maste 
32982c23cb7cSEd Maste 	for (i = 0; (size_t)i < re->shnum; i++) {
32992c23cb7cSEd Maste 		s = &re->sl[i];
33002c23cb7cSEd Maste 		if (s->type == SHT_REL || s->type == SHT_RELA) {
33012c23cb7cSEd Maste 			(void) elf_errno();
33022c23cb7cSEd Maste 			if ((d = elf_getdata(s->scn, NULL)) == NULL) {
33032c23cb7cSEd Maste 				elferr = elf_errno();
33042c23cb7cSEd Maste 				if (elferr != 0)
33052c23cb7cSEd Maste 					warnx("elf_getdata failed: %s",
33062c23cb7cSEd Maste 					    elf_errmsg(elferr));
33072c23cb7cSEd Maste 				continue;
33082c23cb7cSEd Maste 			}
33092c23cb7cSEd Maste 			if (s->type == SHT_REL)
33102c23cb7cSEd Maste 				dump_rel(re, s, d);
33112c23cb7cSEd Maste 			else
33122c23cb7cSEd Maste 				dump_rela(re, s, d);
33132c23cb7cSEd Maste 		}
33142c23cb7cSEd Maste 	}
33152c23cb7cSEd Maste }
33162c23cb7cSEd Maste 
33172c23cb7cSEd Maste static void
33182c23cb7cSEd Maste dump_symtab(struct readelf *re, int i)
33192c23cb7cSEd Maste {
33202c23cb7cSEd Maste 	struct section *s;
33212c23cb7cSEd Maste 	Elf_Data *d;
33222c23cb7cSEd Maste 	GElf_Sym sym;
33232c23cb7cSEd Maste 	const char *name;
332471edbbfdSEd Maste 	int elferr, stab, j, len;
33252c23cb7cSEd Maste 
33262c23cb7cSEd Maste 	s = &re->sl[i];
33272c23cb7cSEd Maste 	stab = s->link;
33282c23cb7cSEd Maste 	(void) elf_errno();
33292c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
33302c23cb7cSEd Maste 		elferr = elf_errno();
33312c23cb7cSEd Maste 		if (elferr != 0)
33322c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
33332c23cb7cSEd Maste 		return;
33342c23cb7cSEd Maste 	}
33352c23cb7cSEd Maste 	if (d->d_size <= 0)
33362c23cb7cSEd Maste 		return;
333771edbbfdSEd Maste 	if (!get_ent_count(s, &len))
333871edbbfdSEd Maste 		return;
33392c23cb7cSEd Maste 	printf("Symbol table (%s)", s->name);
334071edbbfdSEd Maste 	printf(" contains %d entries:\n", len);
33412c23cb7cSEd Maste 	printf("%7s%9s%14s%5s%8s%6s%9s%5s\n", "Num:", "Value", "Size", "Type",
33422c23cb7cSEd Maste 	    "Bind", "Vis", "Ndx", "Name");
33432c23cb7cSEd Maste 
334471edbbfdSEd Maste 	for (j = 0; j < len; j++) {
33452c23cb7cSEd Maste 		if (gelf_getsym(d, j, &sym) != &sym) {
33462c23cb7cSEd Maste 			warnx("gelf_getsym failed: %s", elf_errmsg(-1));
33472c23cb7cSEd Maste 			continue;
33482c23cb7cSEd Maste 		}
33492c23cb7cSEd Maste 		printf("%6d:", j);
33502c23cb7cSEd Maste 		printf(" %16.16jx", (uintmax_t)sym.st_value);
33512c23cb7cSEd Maste 		printf(" %5ju", sym.st_size);
33522c23cb7cSEd Maste 		printf(" %-7s", st_type(GELF_ST_TYPE(sym.st_info)));
33532c23cb7cSEd Maste 		printf(" %-6s", st_bind(GELF_ST_BIND(sym.st_info)));
33542c23cb7cSEd Maste 		printf(" %-8s", st_vis(GELF_ST_VISIBILITY(sym.st_other)));
33552c23cb7cSEd Maste 		printf(" %3s", st_shndx(sym.st_shndx));
33562c23cb7cSEd Maste 		if ((name = elf_strptr(re->elf, stab, sym.st_name)) != NULL)
33572c23cb7cSEd Maste 			printf(" %s", name);
33582c23cb7cSEd Maste 		/* Append symbol version string for SHT_DYNSYM symbol table. */
33592c23cb7cSEd Maste 		if (s->type == SHT_DYNSYM && re->ver != NULL &&
33602c23cb7cSEd Maste 		    re->vs != NULL && re->vs[j] > 1) {
33612c23cb7cSEd Maste 			if (re->vs[j] & 0x8000 ||
33622c23cb7cSEd Maste 			    re->ver[re->vs[j] & 0x7fff].type == 0)
33632c23cb7cSEd Maste 				printf("@%s (%d)",
33642c23cb7cSEd Maste 				    re->ver[re->vs[j] & 0x7fff].name,
33652c23cb7cSEd Maste 				    re->vs[j] & 0x7fff);
33662c23cb7cSEd Maste 			else
33672c23cb7cSEd Maste 				printf("@@%s (%d)", re->ver[re->vs[j]].name,
33682c23cb7cSEd Maste 				    re->vs[j]);
33692c23cb7cSEd Maste 		}
33702c23cb7cSEd Maste 		putchar('\n');
33712c23cb7cSEd Maste 	}
33722c23cb7cSEd Maste 
33732c23cb7cSEd Maste }
33742c23cb7cSEd Maste 
33752c23cb7cSEd Maste static void
33762c23cb7cSEd Maste dump_symtabs(struct readelf *re)
33772c23cb7cSEd Maste {
33782c23cb7cSEd Maste 	GElf_Dyn dyn;
33792c23cb7cSEd Maste 	Elf_Data *d;
33802c23cb7cSEd Maste 	struct section *s;
33812c23cb7cSEd Maste 	uint64_t dyn_off;
338271edbbfdSEd Maste 	int elferr, i, len;
33832c23cb7cSEd Maste 
33842c23cb7cSEd Maste 	/*
33852c23cb7cSEd Maste 	 * If -D is specified, only dump the symbol table specified by
33862c23cb7cSEd Maste 	 * the DT_SYMTAB entry in the .dynamic section.
33872c23cb7cSEd Maste 	 */
33882c23cb7cSEd Maste 	dyn_off = 0;
33892c23cb7cSEd Maste 	if (re->options & RE_DD) {
33902c23cb7cSEd Maste 		s = NULL;
33912c23cb7cSEd Maste 		for (i = 0; (size_t)i < re->shnum; i++)
33922c23cb7cSEd Maste 			if (re->sl[i].type == SHT_DYNAMIC) {
33932c23cb7cSEd Maste 				s = &re->sl[i];
33942c23cb7cSEd Maste 				break;
33952c23cb7cSEd Maste 			}
33962c23cb7cSEd Maste 		if (s == NULL)
33972c23cb7cSEd Maste 			return;
33982c23cb7cSEd Maste 		(void) elf_errno();
33992c23cb7cSEd Maste 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
34002c23cb7cSEd Maste 			elferr = elf_errno();
34012c23cb7cSEd Maste 			if (elferr != 0)
34022c23cb7cSEd Maste 				warnx("elf_getdata failed: %s", elf_errmsg(-1));
34032c23cb7cSEd Maste 			return;
34042c23cb7cSEd Maste 		}
34052c23cb7cSEd Maste 		if (d->d_size <= 0)
34062c23cb7cSEd Maste 			return;
340771edbbfdSEd Maste 		if (!get_ent_count(s, &len))
340871edbbfdSEd Maste 			return;
34092c23cb7cSEd Maste 
341071edbbfdSEd Maste 		for (i = 0; i < len; i++) {
34112c23cb7cSEd Maste 			if (gelf_getdyn(d, i, &dyn) != &dyn) {
34122c23cb7cSEd Maste 				warnx("gelf_getdyn failed: %s", elf_errmsg(-1));
34132c23cb7cSEd Maste 				continue;
34142c23cb7cSEd Maste 			}
34152c23cb7cSEd Maste 			if (dyn.d_tag == DT_SYMTAB) {
34162c23cb7cSEd Maste 				dyn_off = dyn.d_un.d_val;
34172c23cb7cSEd Maste 				break;
34182c23cb7cSEd Maste 			}
34192c23cb7cSEd Maste 		}
34202c23cb7cSEd Maste 	}
34212c23cb7cSEd Maste 
34222c23cb7cSEd Maste 	/* Find and dump symbol tables. */
34232c23cb7cSEd Maste 	for (i = 0; (size_t)i < re->shnum; i++) {
34242c23cb7cSEd Maste 		s = &re->sl[i];
34252c23cb7cSEd Maste 		if (s->type == SHT_SYMTAB || s->type == SHT_DYNSYM) {
34262c23cb7cSEd Maste 			if (re->options & RE_DD) {
34272c23cb7cSEd Maste 				if (dyn_off == s->addr) {
34282c23cb7cSEd Maste 					dump_symtab(re, i);
34292c23cb7cSEd Maste 					break;
34302c23cb7cSEd Maste 				}
34312c23cb7cSEd Maste 			} else
34322c23cb7cSEd Maste 				dump_symtab(re, i);
34332c23cb7cSEd Maste 		}
34342c23cb7cSEd Maste 	}
34352c23cb7cSEd Maste }
34362c23cb7cSEd Maste 
34372c23cb7cSEd Maste static void
34382c23cb7cSEd Maste dump_svr4_hash(struct section *s)
34392c23cb7cSEd Maste {
34402c23cb7cSEd Maste 	Elf_Data	*d;
34412c23cb7cSEd Maste 	uint32_t	*buf;
34422c23cb7cSEd Maste 	uint32_t	 nbucket, nchain;
34432c23cb7cSEd Maste 	uint32_t	*bucket, *chain;
34442c23cb7cSEd Maste 	uint32_t	*bl, *c, maxl, total;
34452c23cb7cSEd Maste 	int		 elferr, i, j;
34462c23cb7cSEd Maste 
34472c23cb7cSEd Maste 	/* Read and parse the content of .hash section. */
34482c23cb7cSEd Maste 	(void) elf_errno();
34492c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
34502c23cb7cSEd Maste 		elferr = elf_errno();
34512c23cb7cSEd Maste 		if (elferr != 0)
34522c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
34532c23cb7cSEd Maste 		return;
34542c23cb7cSEd Maste 	}
34552c23cb7cSEd Maste 	if (d->d_size < 2 * sizeof(uint32_t)) {
34562c23cb7cSEd Maste 		warnx(".hash section too small");
34572c23cb7cSEd Maste 		return;
34582c23cb7cSEd Maste 	}
34592c23cb7cSEd Maste 	buf = d->d_buf;
34602c23cb7cSEd Maste 	nbucket = buf[0];
34612c23cb7cSEd Maste 	nchain = buf[1];
34622c23cb7cSEd Maste 	if (nbucket <= 0 || nchain <= 0) {
34632c23cb7cSEd Maste 		warnx("Malformed .hash section");
34642c23cb7cSEd Maste 		return;
34652c23cb7cSEd Maste 	}
34662c23cb7cSEd Maste 	if (d->d_size != (nbucket + nchain + 2) * sizeof(uint32_t)) {
34672c23cb7cSEd Maste 		warnx("Malformed .hash section");
34682c23cb7cSEd Maste 		return;
34692c23cb7cSEd Maste 	}
34702c23cb7cSEd Maste 	bucket = &buf[2];
34712c23cb7cSEd Maste 	chain = &buf[2 + nbucket];
34722c23cb7cSEd Maste 
34732c23cb7cSEd Maste 	maxl = 0;
34742c23cb7cSEd Maste 	if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
34752c23cb7cSEd Maste 		errx(EXIT_FAILURE, "calloc failed");
34762c23cb7cSEd Maste 	for (i = 0; (uint32_t)i < nbucket; i++)
34772c23cb7cSEd Maste 		for (j = bucket[i]; j > 0 && (uint32_t)j < nchain; j = chain[j])
34782c23cb7cSEd Maste 			if (++bl[i] > maxl)
34792c23cb7cSEd Maste 				maxl = bl[i];
34802c23cb7cSEd Maste 	if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
34812c23cb7cSEd Maste 		errx(EXIT_FAILURE, "calloc failed");
34822c23cb7cSEd Maste 	for (i = 0; (uint32_t)i < nbucket; i++)
34832c23cb7cSEd Maste 		c[bl[i]]++;
34842c23cb7cSEd Maste 	printf("\nHistogram for bucket list length (total of %u buckets):\n",
34852c23cb7cSEd Maste 	    nbucket);
34862c23cb7cSEd Maste 	printf(" Length\tNumber\t\t%% of total\tCoverage\n");
34872c23cb7cSEd Maste 	total = 0;
34882c23cb7cSEd Maste 	for (i = 0; (uint32_t)i <= maxl; i++) {
34892c23cb7cSEd Maste 		total += c[i] * i;
34902c23cb7cSEd Maste 		printf("%7u\t%-10u\t(%5.1f%%)\t%5.1f%%\n", i, c[i],
34912c23cb7cSEd Maste 		    c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
34922c23cb7cSEd Maste 	}
34932c23cb7cSEd Maste 	free(c);
34942c23cb7cSEd Maste 	free(bl);
34952c23cb7cSEd Maste }
34962c23cb7cSEd Maste 
34972c23cb7cSEd Maste static void
34982c23cb7cSEd Maste dump_svr4_hash64(struct readelf *re, struct section *s)
34992c23cb7cSEd Maste {
35002c23cb7cSEd Maste 	Elf_Data	*d, dst;
35012c23cb7cSEd Maste 	uint64_t	*buf;
35022c23cb7cSEd Maste 	uint64_t	 nbucket, nchain;
35032c23cb7cSEd Maste 	uint64_t	*bucket, *chain;
35042c23cb7cSEd Maste 	uint64_t	*bl, *c, maxl, total;
35052c23cb7cSEd Maste 	int		 elferr, i, j;
35062c23cb7cSEd Maste 
35072c23cb7cSEd Maste 	/*
35082c23cb7cSEd Maste 	 * ALPHA uses 64-bit hash entries. Since libelf assumes that
35092c23cb7cSEd Maste 	 * .hash section contains only 32-bit entry, an explicit
35102c23cb7cSEd Maste 	 * gelf_xlatetom is needed here.
35112c23cb7cSEd Maste 	 */
35122c23cb7cSEd Maste 	(void) elf_errno();
35132c23cb7cSEd Maste 	if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
35142c23cb7cSEd Maste 		elferr = elf_errno();
35152c23cb7cSEd Maste 		if (elferr != 0)
35162c23cb7cSEd Maste 			warnx("elf_rawdata failed: %s",
35172c23cb7cSEd Maste 			    elf_errmsg(elferr));
35182c23cb7cSEd Maste 		return;
35192c23cb7cSEd Maste 	}
35202c23cb7cSEd Maste 	d->d_type = ELF_T_XWORD;
35212c23cb7cSEd Maste 	memcpy(&dst, d, sizeof(Elf_Data));
35222c23cb7cSEd Maste 	if (gelf_xlatetom(re->elf, &dst, d,
35232c23cb7cSEd Maste 		re->ehdr.e_ident[EI_DATA]) != &dst) {
35242c23cb7cSEd Maste 		warnx("gelf_xlatetom failed: %s", elf_errmsg(-1));
35252c23cb7cSEd Maste 		return;
35262c23cb7cSEd Maste 	}
35272c23cb7cSEd Maste 	if (dst.d_size < 2 * sizeof(uint64_t)) {
35282c23cb7cSEd Maste 		warnx(".hash section too small");
35292c23cb7cSEd Maste 		return;
35302c23cb7cSEd Maste 	}
35312c23cb7cSEd Maste 	buf = dst.d_buf;
35322c23cb7cSEd Maste 	nbucket = buf[0];
35332c23cb7cSEd Maste 	nchain = buf[1];
35342c23cb7cSEd Maste 	if (nbucket <= 0 || nchain <= 0) {
35352c23cb7cSEd Maste 		warnx("Malformed .hash section");
35362c23cb7cSEd Maste 		return;
35372c23cb7cSEd Maste 	}
35382c23cb7cSEd Maste 	if (d->d_size != (nbucket + nchain + 2) * sizeof(uint32_t)) {
35392c23cb7cSEd Maste 		warnx("Malformed .hash section");
35402c23cb7cSEd Maste 		return;
35412c23cb7cSEd Maste 	}
35422c23cb7cSEd Maste 	bucket = &buf[2];
35432c23cb7cSEd Maste 	chain = &buf[2 + nbucket];
35442c23cb7cSEd Maste 
35452c23cb7cSEd Maste 	maxl = 0;
35462c23cb7cSEd Maste 	if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
35472c23cb7cSEd Maste 		errx(EXIT_FAILURE, "calloc failed");
35482c23cb7cSEd Maste 	for (i = 0; (uint32_t)i < nbucket; i++)
35492c23cb7cSEd Maste 		for (j = bucket[i]; j > 0 && (uint32_t)j < nchain; j = chain[j])
35502c23cb7cSEd Maste 			if (++bl[i] > maxl)
35512c23cb7cSEd Maste 				maxl = bl[i];
35522c23cb7cSEd Maste 	if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
35532c23cb7cSEd Maste 		errx(EXIT_FAILURE, "calloc failed");
35542c23cb7cSEd Maste 	for (i = 0; (uint64_t)i < nbucket; i++)
35552c23cb7cSEd Maste 		c[bl[i]]++;
35562c23cb7cSEd Maste 	printf("Histogram for bucket list length (total of %ju buckets):\n",
35572c23cb7cSEd Maste 	    (uintmax_t)nbucket);
35582c23cb7cSEd Maste 	printf(" Length\tNumber\t\t%% of total\tCoverage\n");
35592c23cb7cSEd Maste 	total = 0;
35602c23cb7cSEd Maste 	for (i = 0; (uint64_t)i <= maxl; i++) {
35612c23cb7cSEd Maste 		total += c[i] * i;
35622c23cb7cSEd Maste 		printf("%7u\t%-10ju\t(%5.1f%%)\t%5.1f%%\n", i, (uintmax_t)c[i],
35632c23cb7cSEd Maste 		    c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
35642c23cb7cSEd Maste 	}
35652c23cb7cSEd Maste 	free(c);
35662c23cb7cSEd Maste 	free(bl);
35672c23cb7cSEd Maste }
35682c23cb7cSEd Maste 
35692c23cb7cSEd Maste static void
35702c23cb7cSEd Maste dump_gnu_hash(struct readelf *re, struct section *s)
35712c23cb7cSEd Maste {
35722c23cb7cSEd Maste 	struct section	*ds;
35732c23cb7cSEd Maste 	Elf_Data	*d;
35742c23cb7cSEd Maste 	uint32_t	*buf;
35752c23cb7cSEd Maste 	uint32_t	*bucket, *chain;
3576cf781b2eSEd Maste 	uint32_t	 nbucket, nchain, symndx, maskwords;
35772c23cb7cSEd Maste 	uint32_t	*bl, *c, maxl, total;
35782c23cb7cSEd Maste 	int		 elferr, dynsymcount, i, j;
35792c23cb7cSEd Maste 
35802c23cb7cSEd Maste 	(void) elf_errno();
35812c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
35822c23cb7cSEd Maste 		elferr = elf_errno();
35832c23cb7cSEd Maste 		if (elferr != 0)
35842c23cb7cSEd Maste 			warnx("elf_getdata failed: %s",
35852c23cb7cSEd Maste 			    elf_errmsg(elferr));
35862c23cb7cSEd Maste 		return;
35872c23cb7cSEd Maste 	}
35882c23cb7cSEd Maste 	if (d->d_size < 4 * sizeof(uint32_t)) {
35892c23cb7cSEd Maste 		warnx(".gnu.hash section too small");
35902c23cb7cSEd Maste 		return;
35912c23cb7cSEd Maste 	}
35922c23cb7cSEd Maste 	buf = d->d_buf;
35932c23cb7cSEd Maste 	nbucket = buf[0];
35942c23cb7cSEd Maste 	symndx = buf[1];
35952c23cb7cSEd Maste 	maskwords = buf[2];
35962c23cb7cSEd Maste 	buf += 4;
35972c23cb7cSEd Maste 	ds = &re->sl[s->link];
359871edbbfdSEd Maste 	if (!get_ent_count(ds, &dynsymcount))
359971edbbfdSEd Maste 		return;
36002c23cb7cSEd Maste 	nchain = dynsymcount - symndx;
36012c23cb7cSEd Maste 	if (d->d_size != 4 * sizeof(uint32_t) + maskwords *
36022c23cb7cSEd Maste 	    (re->ec == ELFCLASS32 ? sizeof(uint32_t) : sizeof(uint64_t)) +
36032c23cb7cSEd Maste 	    (nbucket + nchain) * sizeof(uint32_t)) {
36042c23cb7cSEd Maste 		warnx("Malformed .gnu.hash section");
36052c23cb7cSEd Maste 		return;
36062c23cb7cSEd Maste 	}
36072c23cb7cSEd Maste 	bucket = buf + (re->ec == ELFCLASS32 ? maskwords : maskwords * 2);
36082c23cb7cSEd Maste 	chain = bucket + nbucket;
36092c23cb7cSEd Maste 
36102c23cb7cSEd Maste 	maxl = 0;
36112c23cb7cSEd Maste 	if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
36122c23cb7cSEd Maste 		errx(EXIT_FAILURE, "calloc failed");
36132c23cb7cSEd Maste 	for (i = 0; (uint32_t)i < nbucket; i++)
36142c23cb7cSEd Maste 		for (j = bucket[i]; j > 0 && (uint32_t)j - symndx < nchain;
36152c23cb7cSEd Maste 		     j++) {
36162c23cb7cSEd Maste 			if (++bl[i] > maxl)
36172c23cb7cSEd Maste 				maxl = bl[i];
36182c23cb7cSEd Maste 			if (chain[j - symndx] & 1)
36192c23cb7cSEd Maste 				break;
36202c23cb7cSEd Maste 		}
36212c23cb7cSEd Maste 	if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
36222c23cb7cSEd Maste 		errx(EXIT_FAILURE, "calloc failed");
36232c23cb7cSEd Maste 	for (i = 0; (uint32_t)i < nbucket; i++)
36242c23cb7cSEd Maste 		c[bl[i]]++;
36252c23cb7cSEd Maste 	printf("Histogram for bucket list length (total of %u buckets):\n",
36262c23cb7cSEd Maste 	    nbucket);
36272c23cb7cSEd Maste 	printf(" Length\tNumber\t\t%% of total\tCoverage\n");
36282c23cb7cSEd Maste 	total = 0;
36292c23cb7cSEd Maste 	for (i = 0; (uint32_t)i <= maxl; i++) {
36302c23cb7cSEd Maste 		total += c[i] * i;
36312c23cb7cSEd Maste 		printf("%7u\t%-10u\t(%5.1f%%)\t%5.1f%%\n", i, c[i],
36322c23cb7cSEd Maste 		    c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
36332c23cb7cSEd Maste 	}
36342c23cb7cSEd Maste 	free(c);
36352c23cb7cSEd Maste 	free(bl);
36362c23cb7cSEd Maste }
36372c23cb7cSEd Maste 
36382c23cb7cSEd Maste static void
36392c23cb7cSEd Maste dump_hash(struct readelf *re)
36402c23cb7cSEd Maste {
36412c23cb7cSEd Maste 	struct section	*s;
36422c23cb7cSEd Maste 	int		 i;
36432c23cb7cSEd Maste 
36442c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
36452c23cb7cSEd Maste 		s = &re->sl[i];
36462c23cb7cSEd Maste 		if (s->type == SHT_HASH || s->type == SHT_GNU_HASH) {
36472c23cb7cSEd Maste 			if (s->type == SHT_GNU_HASH)
36482c23cb7cSEd Maste 				dump_gnu_hash(re, s);
36492c23cb7cSEd Maste 			else if (re->ehdr.e_machine == EM_ALPHA &&
36502c23cb7cSEd Maste 			    s->entsize == 8)
36512c23cb7cSEd Maste 				dump_svr4_hash64(re, s);
36522c23cb7cSEd Maste 			else
36532c23cb7cSEd Maste 				dump_svr4_hash(s);
36542c23cb7cSEd Maste 		}
36552c23cb7cSEd Maste 	}
36562c23cb7cSEd Maste }
36572c23cb7cSEd Maste 
36582c23cb7cSEd Maste static void
36592c23cb7cSEd Maste dump_notes(struct readelf *re)
36602c23cb7cSEd Maste {
36612c23cb7cSEd Maste 	struct section *s;
36622c23cb7cSEd Maste 	const char *rawfile;
36632c23cb7cSEd Maste 	GElf_Phdr phdr;
36642c23cb7cSEd Maste 	Elf_Data *d;
36652c23cb7cSEd Maste 	size_t phnum;
36662c23cb7cSEd Maste 	int i, elferr;
36672c23cb7cSEd Maste 
36682c23cb7cSEd Maste 	if (re->ehdr.e_type == ET_CORE) {
36692c23cb7cSEd Maste 		/*
36702c23cb7cSEd Maste 		 * Search program headers in the core file for
36712c23cb7cSEd Maste 		 * PT_NOTE entry.
36722c23cb7cSEd Maste 		 */
36732c23cb7cSEd Maste 		if (elf_getphnum(re->elf, &phnum) == 0) {
36742c23cb7cSEd Maste 			warnx("elf_getphnum failed: %s", elf_errmsg(-1));
36752c23cb7cSEd Maste 			return;
36762c23cb7cSEd Maste 		}
36772c23cb7cSEd Maste 		if (phnum == 0)
36782c23cb7cSEd Maste 			return;
36792c23cb7cSEd Maste 		if ((rawfile = elf_rawfile(re->elf, NULL)) == NULL) {
36802c23cb7cSEd Maste 			warnx("elf_rawfile failed: %s", elf_errmsg(-1));
36812c23cb7cSEd Maste 			return;
36822c23cb7cSEd Maste 		}
36832c23cb7cSEd Maste 		for (i = 0; (size_t) i < phnum; i++) {
36842c23cb7cSEd Maste 			if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
36852c23cb7cSEd Maste 				warnx("gelf_getphdr failed: %s",
36862c23cb7cSEd Maste 				    elf_errmsg(-1));
36872c23cb7cSEd Maste 				continue;
36882c23cb7cSEd Maste 			}
36892c23cb7cSEd Maste 			if (phdr.p_type == PT_NOTE)
36902c23cb7cSEd Maste 				dump_notes_content(re, rawfile + phdr.p_offset,
36912c23cb7cSEd Maste 				    phdr.p_filesz, phdr.p_offset);
36922c23cb7cSEd Maste 		}
36932c23cb7cSEd Maste 
36942c23cb7cSEd Maste 	} else {
36952c23cb7cSEd Maste 		/*
36962c23cb7cSEd Maste 		 * For objects other than core files, Search for
36972c23cb7cSEd Maste 		 * SHT_NOTE sections.
36982c23cb7cSEd Maste 		 */
36992c23cb7cSEd Maste 		for (i = 0; (size_t) i < re->shnum; i++) {
37002c23cb7cSEd Maste 			s = &re->sl[i];
37012c23cb7cSEd Maste 			if (s->type == SHT_NOTE) {
37022c23cb7cSEd Maste 				(void) elf_errno();
37032c23cb7cSEd Maste 				if ((d = elf_getdata(s->scn, NULL)) == NULL) {
37042c23cb7cSEd Maste 					elferr = elf_errno();
37052c23cb7cSEd Maste 					if (elferr != 0)
37062c23cb7cSEd Maste 						warnx("elf_getdata failed: %s",
37072c23cb7cSEd Maste 						    elf_errmsg(elferr));
37082c23cb7cSEd Maste 					continue;
37092c23cb7cSEd Maste 				}
37102c23cb7cSEd Maste 				dump_notes_content(re, d->d_buf, d->d_size,
37112c23cb7cSEd Maste 				    s->off);
37122c23cb7cSEd Maste 			}
37132c23cb7cSEd Maste 		}
37142c23cb7cSEd Maste 	}
37152c23cb7cSEd Maste }
37162c23cb7cSEd Maste 
37172c23cb7cSEd Maste static void
37182c23cb7cSEd Maste dump_notes_content(struct readelf *re, const char *buf, size_t sz, off_t off)
37192c23cb7cSEd Maste {
37202c23cb7cSEd Maste 	Elf_Note *note;
372102b08c90SEd Maste 	const char *end, *name;
37222c23cb7cSEd Maste 
37232c23cb7cSEd Maste 	printf("\nNotes at offset %#010jx with length %#010jx:\n",
37242c23cb7cSEd Maste 	    (uintmax_t) off, (uintmax_t) sz);
37252c23cb7cSEd Maste 	printf("  %-13s %-15s %s\n", "Owner", "Data size", "Description");
37262c23cb7cSEd Maste 	end = buf + sz;
37272c23cb7cSEd Maste 	while (buf < end) {
372802b08c90SEd Maste 		if (buf + sizeof(*note) > end) {
372902b08c90SEd Maste 			warnx("invalid note header");
373002b08c90SEd Maste 			return;
373102b08c90SEd Maste 		}
37322c23cb7cSEd Maste 		note = (Elf_Note *)(uintptr_t) buf;
373302b08c90SEd Maste 		name = (char *)(uintptr_t)(note + 1);
373402b08c90SEd Maste 		/*
373502b08c90SEd Maste 		 * The name field is required to be nul-terminated, and
373602b08c90SEd Maste 		 * n_namesz includes the terminating nul in observed
373702b08c90SEd Maste 		 * implementations (contrary to the ELF-64 spec). A special
373802b08c90SEd Maste 		 * case is needed for cores generated by some older Linux
373902b08c90SEd Maste 		 * versions, which write a note named "CORE" without a nul
374002b08c90SEd Maste 		 * terminator and n_namesz = 4.
374102b08c90SEd Maste 		 */
374202b08c90SEd Maste 		if (note->n_namesz == 0)
374302b08c90SEd Maste 			name = "";
374402b08c90SEd Maste 		else if (note->n_namesz == 4 && strncmp(name, "CORE", 4) == 0)
374502b08c90SEd Maste 			name = "CORE";
374602b08c90SEd Maste 		else if (strnlen(name, note->n_namesz) >= note->n_namesz)
374702b08c90SEd Maste 			name = "<invalid>";
374802b08c90SEd Maste 		printf("  %-13s %#010jx", name, (uintmax_t) note->n_descsz);
374902b08c90SEd Maste 		printf("      %s\n", note_type(name, re->ehdr.e_type,
375002b08c90SEd Maste 		    note->n_type));
375134e3f146SEd Maste 		buf += sizeof(Elf_Note) + roundup2(note->n_namesz, 4) +
37522c23cb7cSEd Maste 		    roundup2(note->n_descsz, 4);
37532c23cb7cSEd Maste 	}
37542c23cb7cSEd Maste }
37552c23cb7cSEd Maste 
37562c23cb7cSEd Maste /*
37572c23cb7cSEd Maste  * Symbol versioning sections are the same for 32bit and 64bit
37582c23cb7cSEd Maste  * ELF objects.
37592c23cb7cSEd Maste  */
37602c23cb7cSEd Maste #define Elf_Verdef	Elf32_Verdef
37612c23cb7cSEd Maste #define	Elf_Verdaux	Elf32_Verdaux
37622c23cb7cSEd Maste #define	Elf_Verneed	Elf32_Verneed
37632c23cb7cSEd Maste #define	Elf_Vernaux	Elf32_Vernaux
37642c23cb7cSEd Maste 
37652c23cb7cSEd Maste #define	SAVE_VERSION_NAME(x, n, t)					\
37662c23cb7cSEd Maste 	do {								\
37672c23cb7cSEd Maste 		while (x >= re->ver_sz) {				\
37682c23cb7cSEd Maste 			nv = realloc(re->ver,				\
37692c23cb7cSEd Maste 			    sizeof(*re->ver) * re->ver_sz * 2);		\
37702c23cb7cSEd Maste 			if (nv == NULL) {				\
37712c23cb7cSEd Maste 				warn("realloc failed");			\
37722c23cb7cSEd Maste 				free(re->ver);				\
37732c23cb7cSEd Maste 				return;					\
37742c23cb7cSEd Maste 			}						\
37752c23cb7cSEd Maste 			re->ver = nv;					\
37762c23cb7cSEd Maste 			for (i = re->ver_sz; i < re->ver_sz * 2; i++) {	\
37772c23cb7cSEd Maste 				re->ver[i].name = NULL;			\
37782c23cb7cSEd Maste 				re->ver[i].type = 0;			\
37792c23cb7cSEd Maste 			}						\
37802c23cb7cSEd Maste 			re->ver_sz *= 2;				\
37812c23cb7cSEd Maste 		}							\
37822c23cb7cSEd Maste 		if (x > 1) {						\
37832c23cb7cSEd Maste 			re->ver[x].name = n;				\
37842c23cb7cSEd Maste 			re->ver[x].type = t;				\
37852c23cb7cSEd Maste 		}							\
37862c23cb7cSEd Maste 	} while (0)
37872c23cb7cSEd Maste 
37882c23cb7cSEd Maste 
37892c23cb7cSEd Maste static void
37902c23cb7cSEd Maste dump_verdef(struct readelf *re, int dump)
37912c23cb7cSEd Maste {
37922c23cb7cSEd Maste 	struct section *s;
37932c23cb7cSEd Maste 	struct symver *nv;
37942c23cb7cSEd Maste 	Elf_Data *d;
37952c23cb7cSEd Maste 	Elf_Verdef *vd;
37962c23cb7cSEd Maste 	Elf_Verdaux *vda;
37972c23cb7cSEd Maste 	uint8_t *buf, *end, *buf2;
37982c23cb7cSEd Maste 	const char *name;
37992c23cb7cSEd Maste 	int elferr, i, j;
38002c23cb7cSEd Maste 
38012c23cb7cSEd Maste 	if ((s = re->vd_s) == NULL)
38022c23cb7cSEd Maste 		return;
38032c23cb7cSEd Maste 
38042c23cb7cSEd Maste 	if (re->ver == NULL) {
38052c23cb7cSEd Maste 		re->ver_sz = 16;
38062c23cb7cSEd Maste 		if ((re->ver = calloc(re->ver_sz, sizeof(*re->ver))) ==
38072c23cb7cSEd Maste 		    NULL) {
38082c23cb7cSEd Maste 			warn("calloc failed");
38092c23cb7cSEd Maste 			return;
38102c23cb7cSEd Maste 		}
38112c23cb7cSEd Maste 		re->ver[0].name = "*local*";
38122c23cb7cSEd Maste 		re->ver[1].name = "*global*";
38132c23cb7cSEd Maste 	}
38142c23cb7cSEd Maste 
38152c23cb7cSEd Maste 	if (dump)
38162c23cb7cSEd Maste 		printf("\nVersion definition section (%s):\n", s->name);
38172c23cb7cSEd Maste 	(void) elf_errno();
38182c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
38192c23cb7cSEd Maste 		elferr = elf_errno();
38202c23cb7cSEd Maste 		if (elferr != 0)
38212c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
38222c23cb7cSEd Maste 		return;
38232c23cb7cSEd Maste 	}
38242c23cb7cSEd Maste 	if (d->d_size == 0)
38252c23cb7cSEd Maste 		return;
38262c23cb7cSEd Maste 
38272c23cb7cSEd Maste 	buf = d->d_buf;
38282c23cb7cSEd Maste 	end = buf + d->d_size;
38292c23cb7cSEd Maste 	while (buf + sizeof(Elf_Verdef) <= end) {
38302c23cb7cSEd Maste 		vd = (Elf_Verdef *) (uintptr_t) buf;
38312c23cb7cSEd Maste 		if (dump) {
38322c23cb7cSEd Maste 			printf("  0x%4.4lx", (unsigned long)
38332c23cb7cSEd Maste 			    (buf - (uint8_t *)d->d_buf));
38342c23cb7cSEd Maste 			printf(" vd_version: %u vd_flags: %d"
38352c23cb7cSEd Maste 			    " vd_ndx: %u vd_cnt: %u", vd->vd_version,
38362c23cb7cSEd Maste 			    vd->vd_flags, vd->vd_ndx, vd->vd_cnt);
38372c23cb7cSEd Maste 		}
38382c23cb7cSEd Maste 		buf2 = buf + vd->vd_aux;
38392c23cb7cSEd Maste 		j = 0;
38402c23cb7cSEd Maste 		while (buf2 + sizeof(Elf_Verdaux) <= end && j < vd->vd_cnt) {
38412c23cb7cSEd Maste 			vda = (Elf_Verdaux *) (uintptr_t) buf2;
38422c23cb7cSEd Maste 			name = get_string(re, s->link, vda->vda_name);
38432c23cb7cSEd Maste 			if (j == 0) {
38442c23cb7cSEd Maste 				if (dump)
38452c23cb7cSEd Maste 					printf(" vda_name: %s\n", name);
38462c23cb7cSEd Maste 				SAVE_VERSION_NAME((int)vd->vd_ndx, name, 1);
38472c23cb7cSEd Maste 			} else if (dump)
38482c23cb7cSEd Maste 				printf("  0x%4.4lx parent: %s\n",
38492c23cb7cSEd Maste 				    (unsigned long) (buf2 -
38502c23cb7cSEd Maste 				    (uint8_t *)d->d_buf), name);
38512c23cb7cSEd Maste 			if (vda->vda_next == 0)
38522c23cb7cSEd Maste 				break;
38532c23cb7cSEd Maste 			buf2 += vda->vda_next;
38542c23cb7cSEd Maste 			j++;
38552c23cb7cSEd Maste 		}
38562c23cb7cSEd Maste 		if (vd->vd_next == 0)
38572c23cb7cSEd Maste 			break;
38582c23cb7cSEd Maste 		buf += vd->vd_next;
38592c23cb7cSEd Maste 	}
38602c23cb7cSEd Maste }
38612c23cb7cSEd Maste 
38622c23cb7cSEd Maste static void
38632c23cb7cSEd Maste dump_verneed(struct readelf *re, int dump)
38642c23cb7cSEd Maste {
38652c23cb7cSEd Maste 	struct section *s;
38662c23cb7cSEd Maste 	struct symver *nv;
38672c23cb7cSEd Maste 	Elf_Data *d;
38682c23cb7cSEd Maste 	Elf_Verneed *vn;
38692c23cb7cSEd Maste 	Elf_Vernaux *vna;
38702c23cb7cSEd Maste 	uint8_t *buf, *end, *buf2;
38712c23cb7cSEd Maste 	const char *name;
38722c23cb7cSEd Maste 	int elferr, i, j;
38732c23cb7cSEd Maste 
38742c23cb7cSEd Maste 	if ((s = re->vn_s) == NULL)
38752c23cb7cSEd Maste 		return;
38762c23cb7cSEd Maste 
38772c23cb7cSEd Maste 	if (re->ver == NULL) {
38782c23cb7cSEd Maste 		re->ver_sz = 16;
38792c23cb7cSEd Maste 		if ((re->ver = calloc(re->ver_sz, sizeof(*re->ver))) ==
38802c23cb7cSEd Maste 		    NULL) {
38812c23cb7cSEd Maste 			warn("calloc failed");
38822c23cb7cSEd Maste 			return;
38832c23cb7cSEd Maste 		}
38842c23cb7cSEd Maste 		re->ver[0].name = "*local*";
38852c23cb7cSEd Maste 		re->ver[1].name = "*global*";
38862c23cb7cSEd Maste 	}
38872c23cb7cSEd Maste 
38882c23cb7cSEd Maste 	if (dump)
38892c23cb7cSEd Maste 		printf("\nVersion needed section (%s):\n", s->name);
38902c23cb7cSEd Maste 	(void) elf_errno();
38912c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
38922c23cb7cSEd Maste 		elferr = elf_errno();
38932c23cb7cSEd Maste 		if (elferr != 0)
38942c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
38952c23cb7cSEd Maste 		return;
38962c23cb7cSEd Maste 	}
38972c23cb7cSEd Maste 	if (d->d_size == 0)
38982c23cb7cSEd Maste 		return;
38992c23cb7cSEd Maste 
39002c23cb7cSEd Maste 	buf = d->d_buf;
39012c23cb7cSEd Maste 	end = buf + d->d_size;
39022c23cb7cSEd Maste 	while (buf + sizeof(Elf_Verneed) <= end) {
39032c23cb7cSEd Maste 		vn = (Elf_Verneed *) (uintptr_t) buf;
39042c23cb7cSEd Maste 		if (dump) {
39052c23cb7cSEd Maste 			printf("  0x%4.4lx", (unsigned long)
39062c23cb7cSEd Maste 			    (buf - (uint8_t *)d->d_buf));
39072c23cb7cSEd Maste 			printf(" vn_version: %u vn_file: %s vn_cnt: %u\n",
39082c23cb7cSEd Maste 			    vn->vn_version,
39092c23cb7cSEd Maste 			    get_string(re, s->link, vn->vn_file),
39102c23cb7cSEd Maste 			    vn->vn_cnt);
39112c23cb7cSEd Maste 		}
39122c23cb7cSEd Maste 		buf2 = buf + vn->vn_aux;
39132c23cb7cSEd Maste 		j = 0;
39142c23cb7cSEd Maste 		while (buf2 + sizeof(Elf_Vernaux) <= end && j < vn->vn_cnt) {
39152c23cb7cSEd Maste 			vna = (Elf32_Vernaux *) (uintptr_t) buf2;
39162c23cb7cSEd Maste 			if (dump)
39172c23cb7cSEd Maste 				printf("  0x%4.4lx", (unsigned long)
39182c23cb7cSEd Maste 				    (buf2 - (uint8_t *)d->d_buf));
39192c23cb7cSEd Maste 			name = get_string(re, s->link, vna->vna_name);
39202c23cb7cSEd Maste 			if (dump)
39212c23cb7cSEd Maste 				printf("   vna_name: %s vna_flags: %u"
39222c23cb7cSEd Maste 				    " vna_other: %u\n", name,
39232c23cb7cSEd Maste 				    vna->vna_flags, vna->vna_other);
39242c23cb7cSEd Maste 			SAVE_VERSION_NAME((int)vna->vna_other, name, 0);
39252c23cb7cSEd Maste 			if (vna->vna_next == 0)
39262c23cb7cSEd Maste 				break;
39272c23cb7cSEd Maste 			buf2 += vna->vna_next;
39282c23cb7cSEd Maste 			j++;
39292c23cb7cSEd Maste 		}
39302c23cb7cSEd Maste 		if (vn->vn_next == 0)
39312c23cb7cSEd Maste 			break;
39322c23cb7cSEd Maste 		buf += vn->vn_next;
39332c23cb7cSEd Maste 	}
39342c23cb7cSEd Maste }
39352c23cb7cSEd Maste 
39362c23cb7cSEd Maste static void
39372c23cb7cSEd Maste dump_versym(struct readelf *re)
39382c23cb7cSEd Maste {
39392c23cb7cSEd Maste 	int i;
39402c23cb7cSEd Maste 
39412c23cb7cSEd Maste 	if (re->vs_s == NULL || re->ver == NULL || re->vs == NULL)
39422c23cb7cSEd Maste 		return;
39432c23cb7cSEd Maste 	printf("\nVersion symbol section (%s):\n", re->vs_s->name);
39442c23cb7cSEd Maste 	for (i = 0; i < re->vs_sz; i++) {
39452c23cb7cSEd Maste 		if ((i & 3) == 0) {
39462c23cb7cSEd Maste 			if (i > 0)
39472c23cb7cSEd Maste 				putchar('\n');
39482c23cb7cSEd Maste 			printf("  %03x:", i);
39492c23cb7cSEd Maste 		}
39502c23cb7cSEd Maste 		if (re->vs[i] & 0x8000)
39512c23cb7cSEd Maste 			printf(" %3xh %-12s ", re->vs[i] & 0x7fff,
39522c23cb7cSEd Maste 			    re->ver[re->vs[i] & 0x7fff].name);
39532c23cb7cSEd Maste 		else
39542c23cb7cSEd Maste 			printf(" %3x %-12s ", re->vs[i],
39552c23cb7cSEd Maste 			    re->ver[re->vs[i]].name);
39562c23cb7cSEd Maste 	}
39572c23cb7cSEd Maste 	putchar('\n');
39582c23cb7cSEd Maste }
39592c23cb7cSEd Maste 
39602c23cb7cSEd Maste static void
39612c23cb7cSEd Maste dump_ver(struct readelf *re)
39622c23cb7cSEd Maste {
39632c23cb7cSEd Maste 
39642c23cb7cSEd Maste 	if (re->vs_s && re->ver && re->vs)
39652c23cb7cSEd Maste 		dump_versym(re);
39662c23cb7cSEd Maste 	if (re->vd_s)
39672c23cb7cSEd Maste 		dump_verdef(re, 1);
39682c23cb7cSEd Maste 	if (re->vn_s)
39692c23cb7cSEd Maste 		dump_verneed(re, 1);
39702c23cb7cSEd Maste }
39712c23cb7cSEd Maste 
39722c23cb7cSEd Maste static void
39732c23cb7cSEd Maste search_ver(struct readelf *re)
39742c23cb7cSEd Maste {
39752c23cb7cSEd Maste 	struct section *s;
39762c23cb7cSEd Maste 	Elf_Data *d;
39772c23cb7cSEd Maste 	int elferr, i;
39782c23cb7cSEd Maste 
39792c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
39802c23cb7cSEd Maste 		s = &re->sl[i];
39812c23cb7cSEd Maste 		if (s->type == SHT_SUNW_versym)
39822c23cb7cSEd Maste 			re->vs_s = s;
39832c23cb7cSEd Maste 		if (s->type == SHT_SUNW_verneed)
39842c23cb7cSEd Maste 			re->vn_s = s;
39852c23cb7cSEd Maste 		if (s->type == SHT_SUNW_verdef)
39862c23cb7cSEd Maste 			re->vd_s = s;
39872c23cb7cSEd Maste 	}
39882c23cb7cSEd Maste 	if (re->vd_s)
39892c23cb7cSEd Maste 		dump_verdef(re, 0);
39902c23cb7cSEd Maste 	if (re->vn_s)
39912c23cb7cSEd Maste 		dump_verneed(re, 0);
39922c23cb7cSEd Maste 	if (re->vs_s && re->ver != NULL) {
39932c23cb7cSEd Maste 		(void) elf_errno();
39942c23cb7cSEd Maste 		if ((d = elf_getdata(re->vs_s->scn, NULL)) == NULL) {
39952c23cb7cSEd Maste 			elferr = elf_errno();
39962c23cb7cSEd Maste 			if (elferr != 0)
39972c23cb7cSEd Maste 				warnx("elf_getdata failed: %s",
39982c23cb7cSEd Maste 				    elf_errmsg(elferr));
39992c23cb7cSEd Maste 			return;
40002c23cb7cSEd Maste 		}
40012c23cb7cSEd Maste 		if (d->d_size == 0)
40022c23cb7cSEd Maste 			return;
40032c23cb7cSEd Maste 		re->vs = d->d_buf;
40042c23cb7cSEd Maste 		re->vs_sz = d->d_size / sizeof(Elf32_Half);
40052c23cb7cSEd Maste 	}
40062c23cb7cSEd Maste }
40072c23cb7cSEd Maste 
40082c23cb7cSEd Maste #undef	Elf_Verdef
40092c23cb7cSEd Maste #undef	Elf_Verdaux
40102c23cb7cSEd Maste #undef	Elf_Verneed
40112c23cb7cSEd Maste #undef	Elf_Vernaux
40122c23cb7cSEd Maste #undef	SAVE_VERSION_NAME
40132c23cb7cSEd Maste 
40142c23cb7cSEd Maste /*
40152c23cb7cSEd Maste  * Elf32_Lib and Elf64_Lib are identical.
40162c23cb7cSEd Maste  */
40172c23cb7cSEd Maste #define	Elf_Lib		Elf32_Lib
40182c23cb7cSEd Maste 
40192c23cb7cSEd Maste static void
40202c23cb7cSEd Maste dump_liblist(struct readelf *re)
40212c23cb7cSEd Maste {
40222c23cb7cSEd Maste 	struct section *s;
40232c23cb7cSEd Maste 	struct tm *t;
40242c23cb7cSEd Maste 	time_t ti;
40252c23cb7cSEd Maste 	char tbuf[20];
40262c23cb7cSEd Maste 	Elf_Data *d;
40272c23cb7cSEd Maste 	Elf_Lib *lib;
402871edbbfdSEd Maste 	int i, j, k, elferr, first, len;
40292c23cb7cSEd Maste 
40302c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
40312c23cb7cSEd Maste 		s = &re->sl[i];
40322c23cb7cSEd Maste 		if (s->type != SHT_GNU_LIBLIST)
40332c23cb7cSEd Maste 			continue;
40342c23cb7cSEd Maste 		(void) elf_errno();
40352c23cb7cSEd Maste 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
40362c23cb7cSEd Maste 			elferr = elf_errno();
40372c23cb7cSEd Maste 			if (elferr != 0)
40382c23cb7cSEd Maste 				warnx("elf_getdata failed: %s",
40392c23cb7cSEd Maste 				    elf_errmsg(elferr));
40402c23cb7cSEd Maste 			continue;
40412c23cb7cSEd Maste 		}
40422c23cb7cSEd Maste 		if (d->d_size <= 0)
40432c23cb7cSEd Maste 			continue;
40442c23cb7cSEd Maste 		lib = d->d_buf;
404571edbbfdSEd Maste 		if (!get_ent_count(s, &len))
404671edbbfdSEd Maste 			continue;
40472c23cb7cSEd Maste 		printf("\nLibrary list section '%s' ", s->name);
404871edbbfdSEd Maste 		printf("contains %d entries:\n", len);
40492c23cb7cSEd Maste 		printf("%12s%24s%18s%10s%6s\n", "Library", "Time Stamp",
40502c23cb7cSEd Maste 		    "Checksum", "Version", "Flags");
40512c23cb7cSEd Maste 		for (j = 0; (uint64_t) j < s->sz / s->entsize; j++) {
40522c23cb7cSEd Maste 			printf("%3d: ", j);
40532c23cb7cSEd Maste 			printf("%-20.20s ",
40542c23cb7cSEd Maste 			    get_string(re, s->link, lib->l_name));
40552c23cb7cSEd Maste 			ti = lib->l_time_stamp;
40562c23cb7cSEd Maste 			t = gmtime(&ti);
40572c23cb7cSEd Maste 			snprintf(tbuf, sizeof(tbuf), "%04d-%02d-%02dT%02d:%02d"
40582c23cb7cSEd Maste 			    ":%2d", t->tm_year + 1900, t->tm_mon + 1,
40592c23cb7cSEd Maste 			    t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
40602c23cb7cSEd Maste 			printf("%-19.19s ", tbuf);
40612c23cb7cSEd Maste 			printf("0x%08x ", lib->l_checksum);
40622c23cb7cSEd Maste 			printf("%-7d %#x", lib->l_version, lib->l_flags);
40632c23cb7cSEd Maste 			if (lib->l_flags != 0) {
40642c23cb7cSEd Maste 				first = 1;
40652c23cb7cSEd Maste 				putchar('(');
40662c23cb7cSEd Maste 				for (k = 0; l_flag[k].name != NULL; k++) {
40672c23cb7cSEd Maste 					if ((l_flag[k].value & lib->l_flags) ==
40682c23cb7cSEd Maste 					    0)
40692c23cb7cSEd Maste 						continue;
40702c23cb7cSEd Maste 					if (!first)
40712c23cb7cSEd Maste 						putchar(',');
40722c23cb7cSEd Maste 					else
40732c23cb7cSEd Maste 						first = 0;
40742c23cb7cSEd Maste 					printf("%s", l_flag[k].name);
40752c23cb7cSEd Maste 				}
40762c23cb7cSEd Maste 				putchar(')');
40772c23cb7cSEd Maste 			}
40782c23cb7cSEd Maste 			putchar('\n');
40792c23cb7cSEd Maste 			lib++;
40802c23cb7cSEd Maste 		}
40812c23cb7cSEd Maste 	}
40822c23cb7cSEd Maste }
40832c23cb7cSEd Maste 
40842c23cb7cSEd Maste #undef Elf_Lib
40852c23cb7cSEd Maste 
40863ef90571SEd Maste static void
40873ef90571SEd Maste dump_section_groups(struct readelf *re)
40883ef90571SEd Maste {
40893ef90571SEd Maste 	struct section *s;
40903ef90571SEd Maste 	const char *symname;
40913ef90571SEd Maste 	Elf_Data *d;
40923ef90571SEd Maste 	uint32_t *w;
40933ef90571SEd Maste 	int i, j, elferr;
40943ef90571SEd Maste 	size_t n;
40953ef90571SEd Maste 
40963ef90571SEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
40973ef90571SEd Maste 		s = &re->sl[i];
40983ef90571SEd Maste 		if (s->type != SHT_GROUP)
40993ef90571SEd Maste 			continue;
41003ef90571SEd Maste 		(void) elf_errno();
41013ef90571SEd Maste 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
41023ef90571SEd Maste 			elferr = elf_errno();
41033ef90571SEd Maste 			if (elferr != 0)
41043ef90571SEd Maste 				warnx("elf_getdata failed: %s",
41053ef90571SEd Maste 				    elf_errmsg(elferr));
41063ef90571SEd Maste 			continue;
41073ef90571SEd Maste 		}
41083ef90571SEd Maste 		if (d->d_size <= 0)
41093ef90571SEd Maste 			continue;
41103ef90571SEd Maste 
41113ef90571SEd Maste 		w = d->d_buf;
41123ef90571SEd Maste 
41133ef90571SEd Maste 		/* We only support COMDAT section. */
41143ef90571SEd Maste #ifndef GRP_COMDAT
41153ef90571SEd Maste #define	GRP_COMDAT 0x1
41163ef90571SEd Maste #endif
41173ef90571SEd Maste 		if ((*w++ & GRP_COMDAT) == 0)
41183ef90571SEd Maste 			return;
41193ef90571SEd Maste 
41203ef90571SEd Maste 		if (s->entsize == 0)
41213ef90571SEd Maste 			s->entsize = 4;
41223ef90571SEd Maste 
41233ef90571SEd Maste 		symname = get_symbol_name(re, s->link, s->info);
41243ef90571SEd Maste 		n = s->sz / s->entsize;
41253ef90571SEd Maste 		if (n-- < 1)
41263ef90571SEd Maste 			return;
41273ef90571SEd Maste 
41283ef90571SEd Maste 		printf("\nCOMDAT group section [%5d] `%s' [%s] contains %ju"
41293ef90571SEd Maste 		    " sections:\n", i, s->name, symname, (uintmax_t)n);
41303ef90571SEd Maste 		printf("   %-10.10s %s\n", "[Index]", "Name");
41313ef90571SEd Maste 		for (j = 0; (size_t) j < n; j++, w++) {
41323ef90571SEd Maste 			if (*w >= re->shnum) {
41333ef90571SEd Maste 				warnx("invalid section index: %u", *w);
41343ef90571SEd Maste 				continue;
41353ef90571SEd Maste 			}
41363ef90571SEd Maste 			printf("   [%5u]   %s\n", *w, re->sl[*w].name);
41373ef90571SEd Maste 		}
41383ef90571SEd Maste 	}
41393ef90571SEd Maste }
41403ef90571SEd Maste 
41412c23cb7cSEd Maste static uint8_t *
41422c23cb7cSEd Maste dump_unknown_tag(uint64_t tag, uint8_t *p)
41432c23cb7cSEd Maste {
41442c23cb7cSEd Maste 	uint64_t val;
41452c23cb7cSEd Maste 
41462c23cb7cSEd Maste 	/*
41472c23cb7cSEd Maste 	 * According to ARM EABI: For tags > 32, even numbered tags have
41482c23cb7cSEd Maste 	 * a ULEB128 param and odd numbered ones have NUL-terminated
41492c23cb7cSEd Maste 	 * string param. This rule probably also applies for tags <= 32
41502c23cb7cSEd Maste 	 * if the object arch is not ARM.
41512c23cb7cSEd Maste 	 */
41522c23cb7cSEd Maste 
41532c23cb7cSEd Maste 	printf("  Tag_unknown_%ju: ", (uintmax_t) tag);
41542c23cb7cSEd Maste 
41552c23cb7cSEd Maste 	if (tag & 1) {
41562c23cb7cSEd Maste 		printf("%s\n", (char *) p);
41572c23cb7cSEd Maste 		p += strlen((char *) p) + 1;
41582c23cb7cSEd Maste 	} else {
41592c23cb7cSEd Maste 		val = _decode_uleb128(&p);
41602c23cb7cSEd Maste 		printf("%ju\n", (uintmax_t) val);
41612c23cb7cSEd Maste 	}
41622c23cb7cSEd Maste 
41632c23cb7cSEd Maste 	return (p);
41642c23cb7cSEd Maste }
41652c23cb7cSEd Maste 
41662c23cb7cSEd Maste static uint8_t *
41672c23cb7cSEd Maste dump_compatibility_tag(uint8_t *p)
41682c23cb7cSEd Maste {
41692c23cb7cSEd Maste 	uint64_t val;
41702c23cb7cSEd Maste 
41712c23cb7cSEd Maste 	val = _decode_uleb128(&p);
41722c23cb7cSEd Maste 	printf("flag = %ju, vendor = %s\n", val, p);
41732c23cb7cSEd Maste 	p += strlen((char *) p) + 1;
41742c23cb7cSEd Maste 
41752c23cb7cSEd Maste 	return (p);
41762c23cb7cSEd Maste }
41772c23cb7cSEd Maste 
41782c23cb7cSEd Maste static void
41792c23cb7cSEd Maste dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe)
41802c23cb7cSEd Maste {
41812c23cb7cSEd Maste 	uint64_t tag, val;
41822c23cb7cSEd Maste 	size_t i;
41832c23cb7cSEd Maste 	int found, desc;
41842c23cb7cSEd Maste 
41852c23cb7cSEd Maste 	(void) re;
41862c23cb7cSEd Maste 
41872c23cb7cSEd Maste 	while (p < pe) {
41882c23cb7cSEd Maste 		tag = _decode_uleb128(&p);
41892c23cb7cSEd Maste 		found = desc = 0;
41902c23cb7cSEd Maste 		for (i = 0; i < sizeof(aeabi_tags) / sizeof(aeabi_tags[0]);
41912c23cb7cSEd Maste 		     i++) {
41922c23cb7cSEd Maste 			if (tag == aeabi_tags[i].tag) {
41932c23cb7cSEd Maste 				found = 1;
41942c23cb7cSEd Maste 				printf("  %s: ", aeabi_tags[i].s_tag);
41952c23cb7cSEd Maste 				if (aeabi_tags[i].get_desc) {
41962c23cb7cSEd Maste 					desc = 1;
41972c23cb7cSEd Maste 					val = _decode_uleb128(&p);
41982c23cb7cSEd Maste 					printf("%s\n",
41992c23cb7cSEd Maste 					    aeabi_tags[i].get_desc(val));
42002c23cb7cSEd Maste 				}
42012c23cb7cSEd Maste 				break;
42022c23cb7cSEd Maste 			}
42032c23cb7cSEd Maste 			if (tag < aeabi_tags[i].tag)
42042c23cb7cSEd Maste 				break;
42052c23cb7cSEd Maste 		}
42062c23cb7cSEd Maste 		if (!found) {
42072c23cb7cSEd Maste 			p = dump_unknown_tag(tag, p);
42082c23cb7cSEd Maste 			continue;
42092c23cb7cSEd Maste 		}
42102c23cb7cSEd Maste 		if (desc)
42112c23cb7cSEd Maste 			continue;
42122c23cb7cSEd Maste 
42132c23cb7cSEd Maste 		switch (tag) {
42142c23cb7cSEd Maste 		case 4:		/* Tag_CPU_raw_name */
42152c23cb7cSEd Maste 		case 5:		/* Tag_CPU_name */
42162c23cb7cSEd Maste 		case 67:	/* Tag_conformance */
42172c23cb7cSEd Maste 			printf("%s\n", (char *) p);
42182c23cb7cSEd Maste 			p += strlen((char *) p) + 1;
42192c23cb7cSEd Maste 			break;
42202c23cb7cSEd Maste 		case 32:	/* Tag_compatibility */
42212c23cb7cSEd Maste 			p = dump_compatibility_tag(p);
42222c23cb7cSEd Maste 			break;
42232c23cb7cSEd Maste 		case 64:	/* Tag_nodefaults */
42242c23cb7cSEd Maste 			/* ignored, written as 0. */
42252c23cb7cSEd Maste 			(void) _decode_uleb128(&p);
42262c23cb7cSEd Maste 			printf("True\n");
42272c23cb7cSEd Maste 			break;
42282c23cb7cSEd Maste 		case 65:	/* Tag_also_compatible_with */
42292c23cb7cSEd Maste 			val = _decode_uleb128(&p);
42302c23cb7cSEd Maste 			/* Must be Tag_CPU_arch */
42312c23cb7cSEd Maste 			if (val != 6) {
42322c23cb7cSEd Maste 				printf("unknown\n");
42332c23cb7cSEd Maste 				break;
42342c23cb7cSEd Maste 			}
42352c23cb7cSEd Maste 			val = _decode_uleb128(&p);
42362c23cb7cSEd Maste 			printf("%s\n", aeabi_cpu_arch(val));
42372c23cb7cSEd Maste 			/* Skip NUL terminator. */
42382c23cb7cSEd Maste 			p++;
42392c23cb7cSEd Maste 			break;
42402c23cb7cSEd Maste 		default:
42412c23cb7cSEd Maste 			putchar('\n');
42422c23cb7cSEd Maste 			break;
42432c23cb7cSEd Maste 		}
42442c23cb7cSEd Maste 	}
42452c23cb7cSEd Maste }
42462c23cb7cSEd Maste 
42472c23cb7cSEd Maste #ifndef	Tag_GNU_MIPS_ABI_FP
42482c23cb7cSEd Maste #define	Tag_GNU_MIPS_ABI_FP	4
42492c23cb7cSEd Maste #endif
42502c23cb7cSEd Maste 
42512c23cb7cSEd Maste static void
42522c23cb7cSEd Maste dump_mips_attributes(struct readelf *re, uint8_t *p, uint8_t *pe)
42532c23cb7cSEd Maste {
42542c23cb7cSEd Maste 	uint64_t tag, val;
42552c23cb7cSEd Maste 
42562c23cb7cSEd Maste 	(void) re;
42572c23cb7cSEd Maste 
42582c23cb7cSEd Maste 	while (p < pe) {
42592c23cb7cSEd Maste 		tag = _decode_uleb128(&p);
42602c23cb7cSEd Maste 		switch (tag) {
42612c23cb7cSEd Maste 		case Tag_GNU_MIPS_ABI_FP:
42622c23cb7cSEd Maste 			val = _decode_uleb128(&p);
42632c23cb7cSEd Maste 			printf("  Tag_GNU_MIPS_ABI_FP: %s\n", mips_abi_fp(val));
42642c23cb7cSEd Maste 			break;
42652c23cb7cSEd Maste 		case 32:	/* Tag_compatibility */
42662c23cb7cSEd Maste 			p = dump_compatibility_tag(p);
42672c23cb7cSEd Maste 			break;
42682c23cb7cSEd Maste 		default:
42692c23cb7cSEd Maste 			p = dump_unknown_tag(tag, p);
42702c23cb7cSEd Maste 			break;
42712c23cb7cSEd Maste 		}
42722c23cb7cSEd Maste 	}
42732c23cb7cSEd Maste }
42742c23cb7cSEd Maste 
42752c23cb7cSEd Maste #ifndef Tag_GNU_Power_ABI_FP
42762c23cb7cSEd Maste #define	Tag_GNU_Power_ABI_FP	4
42772c23cb7cSEd Maste #endif
42782c23cb7cSEd Maste 
42792c23cb7cSEd Maste #ifndef Tag_GNU_Power_ABI_Vector
42802c23cb7cSEd Maste #define	Tag_GNU_Power_ABI_Vector	8
42812c23cb7cSEd Maste #endif
42822c23cb7cSEd Maste 
42832c23cb7cSEd Maste static void
42842c23cb7cSEd Maste dump_ppc_attributes(uint8_t *p, uint8_t *pe)
42852c23cb7cSEd Maste {
42862c23cb7cSEd Maste 	uint64_t tag, val;
42872c23cb7cSEd Maste 
42882c23cb7cSEd Maste 	while (p < pe) {
42892c23cb7cSEd Maste 		tag = _decode_uleb128(&p);
42902c23cb7cSEd Maste 		switch (tag) {
42912c23cb7cSEd Maste 		case Tag_GNU_Power_ABI_FP:
42922c23cb7cSEd Maste 			val = _decode_uleb128(&p);
42932c23cb7cSEd Maste 			printf("  Tag_GNU_Power_ABI_FP: %s\n", ppc_abi_fp(val));
42942c23cb7cSEd Maste 			break;
42952c23cb7cSEd Maste 		case Tag_GNU_Power_ABI_Vector:
42962c23cb7cSEd Maste 			val = _decode_uleb128(&p);
42972c23cb7cSEd Maste 			printf("  Tag_GNU_Power_ABI_Vector: %s\n",
42982c23cb7cSEd Maste 			    ppc_abi_vector(val));
42992c23cb7cSEd Maste 			break;
43002c23cb7cSEd Maste 		case 32:	/* Tag_compatibility */
43012c23cb7cSEd Maste 			p = dump_compatibility_tag(p);
43022c23cb7cSEd Maste 			break;
43032c23cb7cSEd Maste 		default:
43042c23cb7cSEd Maste 			p = dump_unknown_tag(tag, p);
43052c23cb7cSEd Maste 			break;
43062c23cb7cSEd Maste 		}
43072c23cb7cSEd Maste 	}
43082c23cb7cSEd Maste }
43092c23cb7cSEd Maste 
43102c23cb7cSEd Maste static void
43112c23cb7cSEd Maste dump_attributes(struct readelf *re)
43122c23cb7cSEd Maste {
43132c23cb7cSEd Maste 	struct section *s;
43142c23cb7cSEd Maste 	Elf_Data *d;
43152c23cb7cSEd Maste 	uint8_t *p, *sp;
43162c23cb7cSEd Maste 	size_t len, seclen, nlen, sublen;
43172c23cb7cSEd Maste 	uint64_t val;
43182c23cb7cSEd Maste 	int tag, i, elferr;
43192c23cb7cSEd Maste 
43202c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
43212c23cb7cSEd Maste 		s = &re->sl[i];
43222c23cb7cSEd Maste 		if (s->type != SHT_GNU_ATTRIBUTES &&
43232c23cb7cSEd Maste 		    (re->ehdr.e_machine != EM_ARM || s->type != SHT_LOPROC + 3))
43242c23cb7cSEd Maste 			continue;
43252c23cb7cSEd Maste 		(void) elf_errno();
43262c23cb7cSEd Maste 		if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
43272c23cb7cSEd Maste 			elferr = elf_errno();
43282c23cb7cSEd Maste 			if (elferr != 0)
43292c23cb7cSEd Maste 				warnx("elf_rawdata failed: %s",
43302c23cb7cSEd Maste 				    elf_errmsg(elferr));
43312c23cb7cSEd Maste 			continue;
43322c23cb7cSEd Maste 		}
43332c23cb7cSEd Maste 		if (d->d_size <= 0)
43342c23cb7cSEd Maste 			continue;
43352c23cb7cSEd Maste 		p = d->d_buf;
43362c23cb7cSEd Maste 		if (*p != 'A') {
43372c23cb7cSEd Maste 			printf("Unknown Attribute Section Format: %c\n",
43382c23cb7cSEd Maste 			    (char) *p);
43392c23cb7cSEd Maste 			continue;
43402c23cb7cSEd Maste 		}
43412c23cb7cSEd Maste 		len = d->d_size - 1;
43422c23cb7cSEd Maste 		p++;
43432c23cb7cSEd Maste 		while (len > 0) {
434471a0c925SEd Maste 			if (len < 4) {
434571a0c925SEd Maste 				warnx("truncated attribute section length");
434671a0c925SEd Maste 				break;
434771a0c925SEd Maste 			}
43482c23cb7cSEd Maste 			seclen = re->dw_decode(&p, 4);
43492c23cb7cSEd Maste 			if (seclen > len) {
43502c23cb7cSEd Maste 				warnx("invalid attribute section length");
43512c23cb7cSEd Maste 				break;
43522c23cb7cSEd Maste 			}
43532c23cb7cSEd Maste 			len -= seclen;
43542c23cb7cSEd Maste 			nlen = strlen((char *) p) + 1;
435571a0c925SEd Maste 			if (nlen + 4 > seclen) {
435671a0c925SEd Maste 				warnx("invalid attribute section name");
435771a0c925SEd Maste 				break;
435871a0c925SEd Maste 			}
435971a0c925SEd Maste 			printf("Attribute Section: %s\n", (char *) p);
43602c23cb7cSEd Maste 			p += nlen;
43612c23cb7cSEd Maste 			seclen -= nlen + 4;
43622c23cb7cSEd Maste 			while (seclen > 0) {
43632c23cb7cSEd Maste 				sp = p;
43642c23cb7cSEd Maste 				tag = *p++;
43652c23cb7cSEd Maste 				sublen = re->dw_decode(&p, 4);
43662c23cb7cSEd Maste 				if (sublen > seclen) {
43672c23cb7cSEd Maste 					warnx("invalid attribute sub-section"
43682c23cb7cSEd Maste 					    " length");
43692c23cb7cSEd Maste 					break;
43702c23cb7cSEd Maste 				}
43712c23cb7cSEd Maste 				seclen -= sublen;
43722c23cb7cSEd Maste 				printf("%s", top_tag(tag));
43732c23cb7cSEd Maste 				if (tag == 2 || tag == 3) {
43742c23cb7cSEd Maste 					putchar(':');
43752c23cb7cSEd Maste 					for (;;) {
43762c23cb7cSEd Maste 						val = _decode_uleb128(&p);
43772c23cb7cSEd Maste 						if (val == 0)
43782c23cb7cSEd Maste 							break;
43792c23cb7cSEd Maste 						printf(" %ju", (uintmax_t) val);
43802c23cb7cSEd Maste 					}
43812c23cb7cSEd Maste 				}
43822c23cb7cSEd Maste 				putchar('\n');
43832c23cb7cSEd Maste 				if (re->ehdr.e_machine == EM_ARM &&
43842c23cb7cSEd Maste 				    s->type == SHT_LOPROC + 3)
43852c23cb7cSEd Maste 					dump_arm_attributes(re, p, sp + sublen);
43862c23cb7cSEd Maste 				else if (re->ehdr.e_machine == EM_MIPS ||
43872c23cb7cSEd Maste 				    re->ehdr.e_machine == EM_MIPS_RS3_LE)
43882c23cb7cSEd Maste 					dump_mips_attributes(re, p,
43892c23cb7cSEd Maste 					    sp + sublen);
43902c23cb7cSEd Maste 				else if (re->ehdr.e_machine == EM_PPC)
43912c23cb7cSEd Maste 					dump_ppc_attributes(p, sp + sublen);
43922c23cb7cSEd Maste 				p = sp + sublen;
43932c23cb7cSEd Maste 			}
43942c23cb7cSEd Maste 		}
43952c23cb7cSEd Maste 	}
43962c23cb7cSEd Maste }
43972c23cb7cSEd Maste 
43982c23cb7cSEd Maste static void
43992c23cb7cSEd Maste dump_mips_specific_info(struct readelf *re)
44002c23cb7cSEd Maste {
44012c23cb7cSEd Maste 	struct section *s;
44022c23cb7cSEd Maste 	int i, options_found;
44032c23cb7cSEd Maste 
44042c23cb7cSEd Maste 	options_found = 0;
44052c23cb7cSEd Maste 	s = NULL;
44062c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
44072c23cb7cSEd Maste 		s = &re->sl[i];
44082c23cb7cSEd Maste 		if (s->name != NULL && (!strcmp(s->name, ".MIPS.options") ||
44092c23cb7cSEd Maste 		    (s->type == SHT_MIPS_OPTIONS))) {
44102c23cb7cSEd Maste 			dump_mips_options(re, s);
44112c23cb7cSEd Maste 			options_found = 1;
44122c23cb7cSEd Maste 		}
44132c23cb7cSEd Maste 	}
44142c23cb7cSEd Maste 
44152c23cb7cSEd Maste 	/*
44162c23cb7cSEd Maste 	 * According to SGI mips64 spec, .reginfo should be ignored if
44172c23cb7cSEd Maste 	 * .MIPS.options section is present.
44182c23cb7cSEd Maste 	 */
44192c23cb7cSEd Maste 	if (!options_found) {
44202c23cb7cSEd Maste 		for (i = 0; (size_t) i < re->shnum; i++) {
44212c23cb7cSEd Maste 			s = &re->sl[i];
44222c23cb7cSEd Maste 			if (s->name != NULL && (!strcmp(s->name, ".reginfo") ||
44232c23cb7cSEd Maste 			    (s->type == SHT_MIPS_REGINFO)))
44242c23cb7cSEd Maste 				dump_mips_reginfo(re, s);
44252c23cb7cSEd Maste 		}
44262c23cb7cSEd Maste 	}
44272c23cb7cSEd Maste }
44282c23cb7cSEd Maste 
44292c23cb7cSEd Maste static void
44302c23cb7cSEd Maste dump_mips_reginfo(struct readelf *re, struct section *s)
44312c23cb7cSEd Maste {
44322c23cb7cSEd Maste 	Elf_Data *d;
443371edbbfdSEd Maste 	int elferr, len;
44342c23cb7cSEd Maste 
44352c23cb7cSEd Maste 	(void) elf_errno();
44362c23cb7cSEd Maste 	if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
44372c23cb7cSEd Maste 		elferr = elf_errno();
44382c23cb7cSEd Maste 		if (elferr != 0)
44392c23cb7cSEd Maste 			warnx("elf_rawdata failed: %s",
44402c23cb7cSEd Maste 			    elf_errmsg(elferr));
44412c23cb7cSEd Maste 		return;
44422c23cb7cSEd Maste 	}
44432c23cb7cSEd Maste 	if (d->d_size <= 0)
44442c23cb7cSEd Maste 		return;
444571edbbfdSEd Maste 	if (!get_ent_count(s, &len))
444671edbbfdSEd Maste 		return;
44472c23cb7cSEd Maste 
444871edbbfdSEd Maste 	printf("\nSection '%s' contains %d entries:\n", s->name, len);
44492c23cb7cSEd Maste 	dump_mips_odk_reginfo(re, d->d_buf, d->d_size);
44502c23cb7cSEd Maste }
44512c23cb7cSEd Maste 
44522c23cb7cSEd Maste static void
44532c23cb7cSEd Maste dump_mips_options(struct readelf *re, struct section *s)
44542c23cb7cSEd Maste {
44552c23cb7cSEd Maste 	Elf_Data *d;
44562c23cb7cSEd Maste 	uint32_t info;
44572c23cb7cSEd Maste 	uint16_t sndx;
44582c23cb7cSEd Maste 	uint8_t *p, *pe;
44592c23cb7cSEd Maste 	uint8_t kind, size;
44602c23cb7cSEd Maste 	int elferr;
44612c23cb7cSEd Maste 
44622c23cb7cSEd Maste 	(void) elf_errno();
44632c23cb7cSEd Maste 	if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
44642c23cb7cSEd Maste 		elferr = elf_errno();
44652c23cb7cSEd Maste 		if (elferr != 0)
44662c23cb7cSEd Maste 			warnx("elf_rawdata failed: %s",
44672c23cb7cSEd Maste 			    elf_errmsg(elferr));
44682c23cb7cSEd Maste 		return;
44692c23cb7cSEd Maste 	}
44702c23cb7cSEd Maste 	if (d->d_size == 0)
44712c23cb7cSEd Maste 		return;
44722c23cb7cSEd Maste 
44732c23cb7cSEd Maste 	printf("\nSection %s contains:\n", s->name);
44742c23cb7cSEd Maste 	p = d->d_buf;
44752c23cb7cSEd Maste 	pe = p + d->d_size;
44762c23cb7cSEd Maste 	while (p < pe) {
4477b00fe64fSEd Maste 		if (pe - p < 8) {
4478b00fe64fSEd Maste 			warnx("Truncated MIPS option header");
4479b00fe64fSEd Maste 			return;
4480b00fe64fSEd Maste 		}
44812c23cb7cSEd Maste 		kind = re->dw_decode(&p, 1);
44822c23cb7cSEd Maste 		size = re->dw_decode(&p, 1);
44832c23cb7cSEd Maste 		sndx = re->dw_decode(&p, 2);
44842c23cb7cSEd Maste 		info = re->dw_decode(&p, 4);
4485b00fe64fSEd Maste 		if (size < 8 || size - 8 > pe - p) {
4486b00fe64fSEd Maste 			warnx("Malformed MIPS option header");
4487b00fe64fSEd Maste 			return;
4488b00fe64fSEd Maste 		}
4489b00fe64fSEd Maste 		size -= 8;
44902c23cb7cSEd Maste 		switch (kind) {
44912c23cb7cSEd Maste 		case ODK_REGINFO:
4492b00fe64fSEd Maste 			dump_mips_odk_reginfo(re, p, size);
44932c23cb7cSEd Maste 			break;
44942c23cb7cSEd Maste 		case ODK_EXCEPTIONS:
44952c23cb7cSEd Maste 			printf(" EXCEPTIONS FPU_MIN: %#x\n",
44962c23cb7cSEd Maste 			    info & OEX_FPU_MIN);
44972c23cb7cSEd Maste 			printf("%11.11s FPU_MAX: %#x\n", "",
44982c23cb7cSEd Maste 			    info & OEX_FPU_MAX);
44992c23cb7cSEd Maste 			dump_mips_option_flags("", mips_exceptions_option,
45002c23cb7cSEd Maste 			    info);
45012c23cb7cSEd Maste 			break;
45022c23cb7cSEd Maste 		case ODK_PAD:
45032c23cb7cSEd Maste 			printf(" %-10.10s section: %ju\n", "OPAD",
45042c23cb7cSEd Maste 			    (uintmax_t) sndx);
45052c23cb7cSEd Maste 			dump_mips_option_flags("", mips_pad_option, info);
45062c23cb7cSEd Maste 			break;
45072c23cb7cSEd Maste 		case ODK_HWPATCH:
45082c23cb7cSEd Maste 			dump_mips_option_flags("HWPATCH", mips_hwpatch_option,
45092c23cb7cSEd Maste 			    info);
45102c23cb7cSEd Maste 			break;
45112c23cb7cSEd Maste 		case ODK_HWAND:
45122c23cb7cSEd Maste 			dump_mips_option_flags("HWAND", mips_hwa_option, info);
45132c23cb7cSEd Maste 			break;
45142c23cb7cSEd Maste 		case ODK_HWOR:
45152c23cb7cSEd Maste 			dump_mips_option_flags("HWOR", mips_hwo_option, info);
45162c23cb7cSEd Maste 			break;
45172c23cb7cSEd Maste 		case ODK_FILL:
45182c23cb7cSEd Maste 			printf(" %-10.10s %#jx\n", "FILL", (uintmax_t) info);
45192c23cb7cSEd Maste 			break;
45202c23cb7cSEd Maste 		case ODK_TAGS:
45212c23cb7cSEd Maste 			printf(" %-10.10s\n", "TAGS");
45222c23cb7cSEd Maste 			break;
45232c23cb7cSEd Maste 		case ODK_GP_GROUP:
45242c23cb7cSEd Maste 			printf(" %-10.10s GP group number: %#x\n", "GP_GROUP",
45252c23cb7cSEd Maste 			    info & 0xFFFF);
45262c23cb7cSEd Maste 			if (info & 0x10000)
45272c23cb7cSEd Maste 				printf(" %-10.10s GP group is "
45282c23cb7cSEd Maste 				    "self-contained\n", "");
45292c23cb7cSEd Maste 			break;
45302c23cb7cSEd Maste 		case ODK_IDENT:
45312c23cb7cSEd Maste 			printf(" %-10.10s default GP group number: %#x\n",
45322c23cb7cSEd Maste 			    "IDENT", info & 0xFFFF);
45332c23cb7cSEd Maste 			if (info & 0x10000)
45342c23cb7cSEd Maste 				printf(" %-10.10s default GP group is "
45352c23cb7cSEd Maste 				    "self-contained\n", "");
45362c23cb7cSEd Maste 			break;
45372c23cb7cSEd Maste 		case ODK_PAGESIZE:
45382c23cb7cSEd Maste 			printf(" %-10.10s\n", "PAGESIZE");
45392c23cb7cSEd Maste 			break;
45402c23cb7cSEd Maste 		default:
45412c23cb7cSEd Maste 			break;
45422c23cb7cSEd Maste 		}
4543b00fe64fSEd Maste 		p += size;
45442c23cb7cSEd Maste 	}
45452c23cb7cSEd Maste }
45462c23cb7cSEd Maste 
45472c23cb7cSEd Maste static void
45482c23cb7cSEd Maste dump_mips_option_flags(const char *name, struct mips_option *opt, uint64_t info)
45492c23cb7cSEd Maste {
45502c23cb7cSEd Maste 	int first;
45512c23cb7cSEd Maste 
45522c23cb7cSEd Maste 	first = 1;
45532c23cb7cSEd Maste 	for (; opt->desc != NULL; opt++) {
45542c23cb7cSEd Maste 		if (info & opt->flag) {
45552c23cb7cSEd Maste 			printf(" %-10.10s %s\n", first ? name : "",
45562c23cb7cSEd Maste 			    opt->desc);
45572c23cb7cSEd Maste 			first = 0;
45582c23cb7cSEd Maste 		}
45592c23cb7cSEd Maste 	}
45602c23cb7cSEd Maste }
45612c23cb7cSEd Maste 
45622c23cb7cSEd Maste static void
45632c23cb7cSEd Maste dump_mips_odk_reginfo(struct readelf *re, uint8_t *p, size_t sz)
45642c23cb7cSEd Maste {
45652c23cb7cSEd Maste 	uint32_t ri_gprmask;
45662c23cb7cSEd Maste 	uint32_t ri_cprmask[4];
45672c23cb7cSEd Maste 	uint64_t ri_gp_value;
45682c23cb7cSEd Maste 	uint8_t *pe;
45692c23cb7cSEd Maste 	int i;
45702c23cb7cSEd Maste 
45712c23cb7cSEd Maste 	pe = p + sz;
45722c23cb7cSEd Maste 	while (p < pe) {
45732c23cb7cSEd Maste 		ri_gprmask = re->dw_decode(&p, 4);
45742c23cb7cSEd Maste 		/* Skip ri_pad padding field for mips64. */
45752c23cb7cSEd Maste 		if (re->ec == ELFCLASS64)
45762c23cb7cSEd Maste 			re->dw_decode(&p, 4);
45772c23cb7cSEd Maste 		for (i = 0; i < 4; i++)
45782c23cb7cSEd Maste 			ri_cprmask[i] = re->dw_decode(&p, 4);
45792c23cb7cSEd Maste 		if (re->ec == ELFCLASS32)
45802c23cb7cSEd Maste 			ri_gp_value = re->dw_decode(&p, 4);
45812c23cb7cSEd Maste 		else
45822c23cb7cSEd Maste 			ri_gp_value = re->dw_decode(&p, 8);
45832c23cb7cSEd Maste 		printf(" %s    ", option_kind(ODK_REGINFO));
45842c23cb7cSEd Maste 		printf("ri_gprmask:    0x%08jx\n", (uintmax_t) ri_gprmask);
45852c23cb7cSEd Maste 		for (i = 0; i < 4; i++)
45862c23cb7cSEd Maste 			printf("%11.11s ri_cprmask[%d]: 0x%08jx\n", "", i,
45872c23cb7cSEd Maste 			    (uintmax_t) ri_cprmask[i]);
45882c23cb7cSEd Maste 		printf("%12.12s", "");
45892c23cb7cSEd Maste 		printf("ri_gp_value:   %#jx\n", (uintmax_t) ri_gp_value);
45902c23cb7cSEd Maste 	}
45912c23cb7cSEd Maste }
45922c23cb7cSEd Maste 
45932c23cb7cSEd Maste static void
45942c23cb7cSEd Maste dump_arch_specific_info(struct readelf *re)
45952c23cb7cSEd Maste {
45962c23cb7cSEd Maste 
45972c23cb7cSEd Maste 	dump_liblist(re);
45982c23cb7cSEd Maste 	dump_attributes(re);
45992c23cb7cSEd Maste 
46002c23cb7cSEd Maste 	switch (re->ehdr.e_machine) {
46012c23cb7cSEd Maste 	case EM_MIPS:
46022c23cb7cSEd Maste 	case EM_MIPS_RS3_LE:
46032c23cb7cSEd Maste 		dump_mips_specific_info(re);
46042c23cb7cSEd Maste 	default:
46052c23cb7cSEd Maste 		break;
46062c23cb7cSEd Maste 	}
46072c23cb7cSEd Maste }
46082c23cb7cSEd Maste 
4609cf781b2eSEd Maste static const char *
4610cf781b2eSEd Maste dwarf_regname(struct readelf *re, unsigned int num)
4611cf781b2eSEd Maste {
4612cf781b2eSEd Maste 	static char rx[32];
4613cf781b2eSEd Maste 	const char *rn;
4614cf781b2eSEd Maste 
4615cf781b2eSEd Maste 	if ((rn = dwarf_reg(re->ehdr.e_machine, num)) != NULL)
4616cf781b2eSEd Maste 		return (rn);
4617cf781b2eSEd Maste 
4618cf781b2eSEd Maste 	snprintf(rx, sizeof(rx), "r%u", num);
4619cf781b2eSEd Maste 
4620cf781b2eSEd Maste 	return (rx);
4621cf781b2eSEd Maste }
4622cf781b2eSEd Maste 
46232c23cb7cSEd Maste static void
46242c23cb7cSEd Maste dump_dwarf_line(struct readelf *re)
46252c23cb7cSEd Maste {
46262c23cb7cSEd Maste 	struct section *s;
46272c23cb7cSEd Maste 	Dwarf_Die die;
46282c23cb7cSEd Maste 	Dwarf_Error de;
46292c23cb7cSEd Maste 	Dwarf_Half tag, version, pointer_size;
46302c23cb7cSEd Maste 	Dwarf_Unsigned offset, endoff, length, hdrlen, dirndx, mtime, fsize;
46312c23cb7cSEd Maste 	Dwarf_Small minlen, defstmt, lrange, opbase, oplen;
46322c23cb7cSEd Maste 	Elf_Data *d;
46332c23cb7cSEd Maste 	char *pn;
46342c23cb7cSEd Maste 	uint64_t address, file, line, column, isa, opsize, udelta;
46352c23cb7cSEd Maste 	int64_t sdelta;
46362c23cb7cSEd Maste 	uint8_t *p, *pe;
46372c23cb7cSEd Maste 	int8_t lbase;
4638cf781b2eSEd Maste 	int i, is_stmt, dwarf_size, elferr, ret;
46392c23cb7cSEd Maste 
46402c23cb7cSEd Maste 	printf("\nDump of debug contents of section .debug_line:\n");
46412c23cb7cSEd Maste 
46422c23cb7cSEd Maste 	s = NULL;
46432c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
46442c23cb7cSEd Maste 		s = &re->sl[i];
46452c23cb7cSEd Maste 		if (s->name != NULL && !strcmp(s->name, ".debug_line"))
46462c23cb7cSEd Maste 			break;
46472c23cb7cSEd Maste 	}
46482c23cb7cSEd Maste 	if ((size_t) i >= re->shnum)
46492c23cb7cSEd Maste 		return;
46502c23cb7cSEd Maste 
46512c23cb7cSEd Maste 	(void) elf_errno();
46522c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
46532c23cb7cSEd Maste 		elferr = elf_errno();
46542c23cb7cSEd Maste 		if (elferr != 0)
46552c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
46562c23cb7cSEd Maste 		return;
46572c23cb7cSEd Maste 	}
46582c23cb7cSEd Maste 	if (d->d_size <= 0)
46592c23cb7cSEd Maste 		return;
46602c23cb7cSEd Maste 
46612c23cb7cSEd Maste 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
46622c23cb7cSEd Maste 	    NULL, &de)) ==  DW_DLV_OK) {
46632c23cb7cSEd Maste 		die = NULL;
46642c23cb7cSEd Maste 		while (dwarf_siblingof(re->dbg, die, &die, &de) == DW_DLV_OK) {
46652c23cb7cSEd Maste 			if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
46662c23cb7cSEd Maste 				warnx("dwarf_tag failed: %s",
46672c23cb7cSEd Maste 				    dwarf_errmsg(de));
46682c23cb7cSEd Maste 				return;
46692c23cb7cSEd Maste 			}
46702c23cb7cSEd Maste 			/* XXX: What about DW_TAG_partial_unit? */
46712c23cb7cSEd Maste 			if (tag == DW_TAG_compile_unit)
46722c23cb7cSEd Maste 				break;
46732c23cb7cSEd Maste 		}
46742c23cb7cSEd Maste 		if (die == NULL) {
46752c23cb7cSEd Maste 			warnx("could not find DW_TAG_compile_unit die");
46762c23cb7cSEd Maste 			return;
46772c23cb7cSEd Maste 		}
46782c23cb7cSEd Maste 		if (dwarf_attrval_unsigned(die, DW_AT_stmt_list, &offset,
46792c23cb7cSEd Maste 		    &de) != DW_DLV_OK)
46802c23cb7cSEd Maste 			continue;
46812c23cb7cSEd Maste 
46822c23cb7cSEd Maste 		length = re->dw_read(d, &offset, 4);
46832c23cb7cSEd Maste 		if (length == 0xffffffff) {
46842c23cb7cSEd Maste 			dwarf_size = 8;
46852c23cb7cSEd Maste 			length = re->dw_read(d, &offset, 8);
46862c23cb7cSEd Maste 		} else
46872c23cb7cSEd Maste 			dwarf_size = 4;
46882c23cb7cSEd Maste 
46892c23cb7cSEd Maste 		if (length > d->d_size - offset) {
46902c23cb7cSEd Maste 			warnx("invalid .dwarf_line section");
46912c23cb7cSEd Maste 			continue;
46922c23cb7cSEd Maste 		}
46932c23cb7cSEd Maste 
46942c23cb7cSEd Maste 		endoff = offset + length;
46952c23cb7cSEd Maste 		version = re->dw_read(d, &offset, 2);
46962c23cb7cSEd Maste 		hdrlen = re->dw_read(d, &offset, dwarf_size);
46972c23cb7cSEd Maste 		minlen = re->dw_read(d, &offset, 1);
46982c23cb7cSEd Maste 		defstmt = re->dw_read(d, &offset, 1);
46992c23cb7cSEd Maste 		lbase = re->dw_read(d, &offset, 1);
47002c23cb7cSEd Maste 		lrange = re->dw_read(d, &offset, 1);
47012c23cb7cSEd Maste 		opbase = re->dw_read(d, &offset, 1);
47022c23cb7cSEd Maste 
47032c23cb7cSEd Maste 		printf("\n");
47042c23cb7cSEd Maste 		printf("  Length:\t\t\t%ju\n", (uintmax_t) length);
47052c23cb7cSEd Maste 		printf("  DWARF version:\t\t%u\n", version);
47062c23cb7cSEd Maste 		printf("  Prologue Length:\t\t%ju\n", (uintmax_t) hdrlen);
47072c23cb7cSEd Maste 		printf("  Minimum Instruction Length:\t%u\n", minlen);
47082c23cb7cSEd Maste 		printf("  Initial value of 'is_stmt':\t%u\n", defstmt);
47092c23cb7cSEd Maste 		printf("  Line Base:\t\t\t%d\n", lbase);
47102c23cb7cSEd Maste 		printf("  Line Range:\t\t\t%u\n", lrange);
47112c23cb7cSEd Maste 		printf("  Opcode Base:\t\t\t%u\n", opbase);
47122c23cb7cSEd Maste 		(void) dwarf_get_address_size(re->dbg, &pointer_size, &de);
47132c23cb7cSEd Maste 		printf("  (Pointer size:\t\t%u)\n", pointer_size);
47142c23cb7cSEd Maste 
47152c23cb7cSEd Maste 		printf("\n");
47162c23cb7cSEd Maste 		printf(" Opcodes:\n");
47172c23cb7cSEd Maste 		for (i = 1; i < opbase; i++) {
47182c23cb7cSEd Maste 			oplen = re->dw_read(d, &offset, 1);
47192c23cb7cSEd Maste 			printf("  Opcode %d has %u args\n", i, oplen);
47202c23cb7cSEd Maste 		}
47212c23cb7cSEd Maste 
47222c23cb7cSEd Maste 		printf("\n");
47232c23cb7cSEd Maste 		printf(" The Directory Table:\n");
47242c23cb7cSEd Maste 		p = (uint8_t *) d->d_buf + offset;
47252c23cb7cSEd Maste 		while (*p != '\0') {
47262c23cb7cSEd Maste 			printf("  %s\n", (char *) p);
47272c23cb7cSEd Maste 			p += strlen((char *) p) + 1;
47282c23cb7cSEd Maste 		}
47292c23cb7cSEd Maste 
47302c23cb7cSEd Maste 		p++;
47312c23cb7cSEd Maste 		printf("\n");
47322c23cb7cSEd Maste 		printf(" The File Name Table:\n");
47332c23cb7cSEd Maste 		printf("  Entry\tDir\tTime\tSize\tName\n");
47342c23cb7cSEd Maste 		i = 0;
47352c23cb7cSEd Maste 		while (*p != '\0') {
47362c23cb7cSEd Maste 			i++;
47372c23cb7cSEd Maste 			pn = (char *) p;
47382c23cb7cSEd Maste 			p += strlen(pn) + 1;
47392c23cb7cSEd Maste 			dirndx = _decode_uleb128(&p);
47402c23cb7cSEd Maste 			mtime = _decode_uleb128(&p);
47412c23cb7cSEd Maste 			fsize = _decode_uleb128(&p);
47422c23cb7cSEd Maste 			printf("  %d\t%ju\t%ju\t%ju\t%s\n", i,
47432c23cb7cSEd Maste 			    (uintmax_t) dirndx, (uintmax_t) mtime,
47442c23cb7cSEd Maste 			    (uintmax_t) fsize, pn);
47452c23cb7cSEd Maste 		}
47462c23cb7cSEd Maste 
47472c23cb7cSEd Maste #define	RESET_REGISTERS						\
47482c23cb7cSEd Maste 	do {							\
47492c23cb7cSEd Maste 		address	       = 0;				\
47502c23cb7cSEd Maste 		file	       = 1;				\
47512c23cb7cSEd Maste 		line	       = 1;				\
47522c23cb7cSEd Maste 		column	       = 0;				\
47532c23cb7cSEd Maste 		is_stmt	       = defstmt;			\
47542c23cb7cSEd Maste 	} while(0)
47552c23cb7cSEd Maste 
47562c23cb7cSEd Maste #define	LINE(x) (lbase + (((x) - opbase) % lrange))
47572c23cb7cSEd Maste #define	ADDRESS(x) ((((x) - opbase) / lrange) * minlen)
47582c23cb7cSEd Maste 
47592c23cb7cSEd Maste 		p++;
47602c23cb7cSEd Maste 		pe = (uint8_t *) d->d_buf + endoff;
47612c23cb7cSEd Maste 		printf("\n");
47622c23cb7cSEd Maste 		printf(" Line Number Statements:\n");
47632c23cb7cSEd Maste 
47642c23cb7cSEd Maste 		RESET_REGISTERS;
47652c23cb7cSEd Maste 
47662c23cb7cSEd Maste 		while (p < pe) {
47672c23cb7cSEd Maste 
47682c23cb7cSEd Maste 			if (*p == 0) {
47692c23cb7cSEd Maste 				/*
47702c23cb7cSEd Maste 				 * Extended Opcodes.
47712c23cb7cSEd Maste 				 */
47722c23cb7cSEd Maste 				p++;
47732c23cb7cSEd Maste 				opsize = _decode_uleb128(&p);
47742c23cb7cSEd Maste 				printf("  Extended opcode %u: ", *p);
47752c23cb7cSEd Maste 				switch (*p) {
47762c23cb7cSEd Maste 				case DW_LNE_end_sequence:
47772c23cb7cSEd Maste 					p++;
47782c23cb7cSEd Maste 					RESET_REGISTERS;
47792c23cb7cSEd Maste 					printf("End of Sequence\n");
47802c23cb7cSEd Maste 					break;
47812c23cb7cSEd Maste 				case DW_LNE_set_address:
47822c23cb7cSEd Maste 					p++;
47832c23cb7cSEd Maste 					address = re->dw_decode(&p,
47842c23cb7cSEd Maste 					    pointer_size);
47852c23cb7cSEd Maste 					printf("set Address to %#jx\n",
47862c23cb7cSEd Maste 					    (uintmax_t) address);
47872c23cb7cSEd Maste 					break;
47882c23cb7cSEd Maste 				case DW_LNE_define_file:
47892c23cb7cSEd Maste 					p++;
47902c23cb7cSEd Maste 					pn = (char *) p;
47912c23cb7cSEd Maste 					p += strlen(pn) + 1;
47922c23cb7cSEd Maste 					dirndx = _decode_uleb128(&p);
47932c23cb7cSEd Maste 					mtime = _decode_uleb128(&p);
47942c23cb7cSEd Maste 					fsize = _decode_uleb128(&p);
47952c23cb7cSEd Maste 					printf("define new file: %s\n", pn);
47962c23cb7cSEd Maste 					break;
47972c23cb7cSEd Maste 				default:
47982c23cb7cSEd Maste 					/* Unrecognized extened opcodes. */
47992c23cb7cSEd Maste 					p += opsize;
48002c23cb7cSEd Maste 					printf("unknown opcode\n");
48012c23cb7cSEd Maste 				}
48022c23cb7cSEd Maste 			} else if (*p > 0 && *p < opbase) {
48032c23cb7cSEd Maste 				/*
48042c23cb7cSEd Maste 				 * Standard Opcodes.
48052c23cb7cSEd Maste 				 */
48062c23cb7cSEd Maste 				switch(*p++) {
48072c23cb7cSEd Maste 				case DW_LNS_copy:
48082c23cb7cSEd Maste 					printf("  Copy\n");
48092c23cb7cSEd Maste 					break;
48102c23cb7cSEd Maste 				case DW_LNS_advance_pc:
48112c23cb7cSEd Maste 					udelta = _decode_uleb128(&p) *
48122c23cb7cSEd Maste 					    minlen;
48132c23cb7cSEd Maste 					address += udelta;
48142c23cb7cSEd Maste 					printf("  Advance PC by %ju to %#jx\n",
48152c23cb7cSEd Maste 					    (uintmax_t) udelta,
48162c23cb7cSEd Maste 					    (uintmax_t) address);
48172c23cb7cSEd Maste 					break;
48182c23cb7cSEd Maste 				case DW_LNS_advance_line:
48192c23cb7cSEd Maste 					sdelta = _decode_sleb128(&p);
48202c23cb7cSEd Maste 					line += sdelta;
48212c23cb7cSEd Maste 					printf("  Advance Line by %jd to %ju\n",
48222c23cb7cSEd Maste 					    (intmax_t) sdelta,
48232c23cb7cSEd Maste 					    (uintmax_t) line);
48242c23cb7cSEd Maste 					break;
48252c23cb7cSEd Maste 				case DW_LNS_set_file:
48262c23cb7cSEd Maste 					file = _decode_uleb128(&p);
48272c23cb7cSEd Maste 					printf("  Set File to %ju\n",
48282c23cb7cSEd Maste 					    (uintmax_t) file);
48292c23cb7cSEd Maste 					break;
48302c23cb7cSEd Maste 				case DW_LNS_set_column:
48312c23cb7cSEd Maste 					column = _decode_uleb128(&p);
48322c23cb7cSEd Maste 					printf("  Set Column to %ju\n",
48332c23cb7cSEd Maste 					    (uintmax_t) column);
48342c23cb7cSEd Maste 					break;
48352c23cb7cSEd Maste 				case DW_LNS_negate_stmt:
48362c23cb7cSEd Maste 					is_stmt = !is_stmt;
48372c23cb7cSEd Maste 					printf("  Set is_stmt to %d\n", is_stmt);
48382c23cb7cSEd Maste 					break;
48392c23cb7cSEd Maste 				case DW_LNS_set_basic_block:
48402c23cb7cSEd Maste 					printf("  Set basic block flag\n");
48412c23cb7cSEd Maste 					break;
48422c23cb7cSEd Maste 				case DW_LNS_const_add_pc:
48432c23cb7cSEd Maste 					address += ADDRESS(255);
48442c23cb7cSEd Maste 					printf("  Advance PC by constant %ju"
48452c23cb7cSEd Maste 					    " to %#jx\n",
48462c23cb7cSEd Maste 					    (uintmax_t) ADDRESS(255),
48472c23cb7cSEd Maste 					    (uintmax_t) address);
48482c23cb7cSEd Maste 					break;
48492c23cb7cSEd Maste 				case DW_LNS_fixed_advance_pc:
48502c23cb7cSEd Maste 					udelta = re->dw_decode(&p, 2);
48512c23cb7cSEd Maste 					address += udelta;
48522c23cb7cSEd Maste 					printf("  Advance PC by fixed value "
48532c23cb7cSEd Maste 					    "%ju to %#jx\n",
48542c23cb7cSEd Maste 					    (uintmax_t) udelta,
48552c23cb7cSEd Maste 					    (uintmax_t) address);
48562c23cb7cSEd Maste 					break;
48572c23cb7cSEd Maste 				case DW_LNS_set_prologue_end:
48582c23cb7cSEd Maste 					printf("  Set prologue end flag\n");
48592c23cb7cSEd Maste 					break;
48602c23cb7cSEd Maste 				case DW_LNS_set_epilogue_begin:
48612c23cb7cSEd Maste 					printf("  Set epilogue begin flag\n");
48622c23cb7cSEd Maste 					break;
48632c23cb7cSEd Maste 				case DW_LNS_set_isa:
48642c23cb7cSEd Maste 					isa = _decode_uleb128(&p);
48652c23cb7cSEd Maste 					printf("  Set isa to %ju\n", isa);
48662c23cb7cSEd Maste 					break;
48672c23cb7cSEd Maste 				default:
48682c23cb7cSEd Maste 					/* Unrecognized extended opcodes. */
48692c23cb7cSEd Maste 					printf("  Unknown extended opcode %u\n",
48702c23cb7cSEd Maste 					    *(p - 1));
48712c23cb7cSEd Maste 					break;
48722c23cb7cSEd Maste 				}
48732c23cb7cSEd Maste 
48742c23cb7cSEd Maste 			} else {
48752c23cb7cSEd Maste 				/*
48762c23cb7cSEd Maste 				 * Special Opcodes.
48772c23cb7cSEd Maste 				 */
48782c23cb7cSEd Maste 				line += LINE(*p);
48792c23cb7cSEd Maste 				address += ADDRESS(*p);
48802c23cb7cSEd Maste 				printf("  Special opcode %u: advance Address "
48812c23cb7cSEd Maste 				    "by %ju to %#jx and Line by %jd to %ju\n",
48822c23cb7cSEd Maste 				    *p - opbase, (uintmax_t) ADDRESS(*p),
48832c23cb7cSEd Maste 				    (uintmax_t) address, (intmax_t) LINE(*p),
48842c23cb7cSEd Maste 				    (uintmax_t) line);
48852c23cb7cSEd Maste 				p++;
48862c23cb7cSEd Maste 			}
48872c23cb7cSEd Maste 
48882c23cb7cSEd Maste 
48892c23cb7cSEd Maste 		}
48902c23cb7cSEd Maste 	}
48912c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
48922c23cb7cSEd Maste 		warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
48932c23cb7cSEd Maste 
48942c23cb7cSEd Maste #undef	RESET_REGISTERS
48952c23cb7cSEd Maste #undef	LINE
48962c23cb7cSEd Maste #undef	ADDRESS
48972c23cb7cSEd Maste }
48982c23cb7cSEd Maste 
48992c23cb7cSEd Maste static void
49002c23cb7cSEd Maste dump_dwarf_line_decoded(struct readelf *re)
49012c23cb7cSEd Maste {
49022c23cb7cSEd Maste 	Dwarf_Die die;
49032c23cb7cSEd Maste 	Dwarf_Line *linebuf, ln;
49042c23cb7cSEd Maste 	Dwarf_Addr lineaddr;
49052c23cb7cSEd Maste 	Dwarf_Signed linecount, srccount;
49062c23cb7cSEd Maste 	Dwarf_Unsigned lineno, fn;
49072c23cb7cSEd Maste 	Dwarf_Error de;
49082c23cb7cSEd Maste 	const char *dir, *file;
49092c23cb7cSEd Maste 	char **srcfiles;
49102c23cb7cSEd Maste 	int i, ret;
49112c23cb7cSEd Maste 
49122c23cb7cSEd Maste 	printf("Decoded dump of debug contents of section .debug_line:\n\n");
49132c23cb7cSEd Maste 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
49142c23cb7cSEd Maste 	    NULL, &de)) == DW_DLV_OK) {
49152c23cb7cSEd Maste 		if (dwarf_siblingof(re->dbg, NULL, &die, &de) != DW_DLV_OK)
49162c23cb7cSEd Maste 			continue;
49172c23cb7cSEd Maste 		if (dwarf_attrval_string(die, DW_AT_name, &file, &de) !=
49182c23cb7cSEd Maste 		    DW_DLV_OK)
49192c23cb7cSEd Maste 			file = NULL;
49202c23cb7cSEd Maste 		if (dwarf_attrval_string(die, DW_AT_comp_dir, &dir, &de) !=
49212c23cb7cSEd Maste 		    DW_DLV_OK)
49222c23cb7cSEd Maste 			dir = NULL;
49232c23cb7cSEd Maste 		printf("CU: ");
49242c23cb7cSEd Maste 		if (dir && file)
49252c23cb7cSEd Maste 			printf("%s/", dir);
49262c23cb7cSEd Maste 		if (file)
49272c23cb7cSEd Maste 			printf("%s", file);
49282c23cb7cSEd Maste 		putchar('\n');
49292c23cb7cSEd Maste 		printf("%-37s %11s   %s\n", "Filename", "Line Number",
49302c23cb7cSEd Maste 		    "Starting Address");
49312c23cb7cSEd Maste 		if (dwarf_srclines(die, &linebuf, &linecount, &de) != DW_DLV_OK)
49322c23cb7cSEd Maste 			continue;
49332c23cb7cSEd Maste 		if (dwarf_srcfiles(die, &srcfiles, &srccount, &de) != DW_DLV_OK)
49342c23cb7cSEd Maste 			continue;
49352c23cb7cSEd Maste 		for (i = 0; i < linecount; i++) {
49362c23cb7cSEd Maste 			ln = linebuf[i];
49372c23cb7cSEd Maste 			if (dwarf_line_srcfileno(ln, &fn, &de) != DW_DLV_OK)
49382c23cb7cSEd Maste 				continue;
49392c23cb7cSEd Maste 			if (dwarf_lineno(ln, &lineno, &de) != DW_DLV_OK)
49402c23cb7cSEd Maste 				continue;
49412c23cb7cSEd Maste 			if (dwarf_lineaddr(ln, &lineaddr, &de) != DW_DLV_OK)
49422c23cb7cSEd Maste 				continue;
49432c23cb7cSEd Maste 			printf("%-37s %11ju %#18jx\n",
49442c23cb7cSEd Maste 			    basename(srcfiles[fn - 1]), (uintmax_t) lineno,
49452c23cb7cSEd Maste 			    (uintmax_t) lineaddr);
49462c23cb7cSEd Maste 		}
49472c23cb7cSEd Maste 		putchar('\n');
49482c23cb7cSEd Maste 	}
49492c23cb7cSEd Maste }
49502c23cb7cSEd Maste 
49512c23cb7cSEd Maste static void
49522c23cb7cSEd Maste dump_dwarf_die(struct readelf *re, Dwarf_Die die, int level)
49532c23cb7cSEd Maste {
49542c23cb7cSEd Maste 	Dwarf_Attribute *attr_list;
49552c23cb7cSEd Maste 	Dwarf_Die ret_die;
4956cf781b2eSEd Maste 	Dwarf_Off dieoff, cuoff, culen, attroff;
4957cf781b2eSEd Maste 	Dwarf_Unsigned ate, lang, v_udata, v_sig;
49582c23cb7cSEd Maste 	Dwarf_Signed attr_count, v_sdata;
49592c23cb7cSEd Maste 	Dwarf_Off v_off;
49602c23cb7cSEd Maste 	Dwarf_Addr v_addr;
49612c23cb7cSEd Maste 	Dwarf_Half tag, attr, form;
49622c23cb7cSEd Maste 	Dwarf_Block *v_block;
4963cf781b2eSEd Maste 	Dwarf_Bool v_bool, is_info;
4964cf781b2eSEd Maste 	Dwarf_Sig8 v_sig8;
49652c23cb7cSEd Maste 	Dwarf_Error de;
4966cf781b2eSEd Maste 	Dwarf_Ptr v_expr;
4967cf781b2eSEd Maste 	const char *tag_str, *attr_str, *ate_str, *lang_str;
4968cf781b2eSEd Maste 	char unk_tag[32], unk_attr[32];
49692c23cb7cSEd Maste 	char *v_str;
4970cf781b2eSEd Maste 	uint8_t *b, *p;
49712c23cb7cSEd Maste 	int i, j, abc, ret;
49722c23cb7cSEd Maste 
49732c23cb7cSEd Maste 	if (dwarf_dieoffset(die, &dieoff, &de) != DW_DLV_OK) {
49742c23cb7cSEd Maste 		warnx("dwarf_dieoffset failed: %s", dwarf_errmsg(de));
49752c23cb7cSEd Maste 		goto cont_search;
49762c23cb7cSEd Maste 	}
49772c23cb7cSEd Maste 
49782c23cb7cSEd Maste 	printf(" <%d><%jx>: ", level, (uintmax_t) dieoff);
49792c23cb7cSEd Maste 
49802c23cb7cSEd Maste 	if (dwarf_die_CU_offset_range(die, &cuoff, &culen, &de) != DW_DLV_OK) {
49812c23cb7cSEd Maste 		warnx("dwarf_die_CU_offset_range failed: %s",
49822c23cb7cSEd Maste 		      dwarf_errmsg(de));
49832c23cb7cSEd Maste 		cuoff = 0;
49842c23cb7cSEd Maste 	}
49852c23cb7cSEd Maste 
49862c23cb7cSEd Maste 	abc = dwarf_die_abbrev_code(die);
49872c23cb7cSEd Maste 	if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
49882c23cb7cSEd Maste 		warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
49892c23cb7cSEd Maste 		goto cont_search;
49902c23cb7cSEd Maste 	}
49912c23cb7cSEd Maste 	if (dwarf_get_TAG_name(tag, &tag_str) != DW_DLV_OK) {
4992cf781b2eSEd Maste 		snprintf(unk_tag, sizeof(unk_tag), "[Unknown Tag: %#x]", tag);
4993cf781b2eSEd Maste 		tag_str = unk_tag;
49942c23cb7cSEd Maste 	}
49952c23cb7cSEd Maste 
49962c23cb7cSEd Maste 	printf("Abbrev Number: %d (%s)\n", abc, tag_str);
49972c23cb7cSEd Maste 
49982c23cb7cSEd Maste 	if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
49992c23cb7cSEd Maste 	    DW_DLV_OK) {
50002c23cb7cSEd Maste 		if (ret == DW_DLV_ERROR)
50012c23cb7cSEd Maste 			warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
50022c23cb7cSEd Maste 		goto cont_search;
50032c23cb7cSEd Maste 	}
50042c23cb7cSEd Maste 
50052c23cb7cSEd Maste 	for (i = 0; i < attr_count; i++) {
50062c23cb7cSEd Maste 		if (dwarf_whatform(attr_list[i], &form, &de) != DW_DLV_OK) {
50072c23cb7cSEd Maste 			warnx("dwarf_whatform failed: %s", dwarf_errmsg(de));
50082c23cb7cSEd Maste 			continue;
50092c23cb7cSEd Maste 		}
50102c23cb7cSEd Maste 		if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
50112c23cb7cSEd Maste 			warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
50122c23cb7cSEd Maste 			continue;
50132c23cb7cSEd Maste 		}
50142c23cb7cSEd Maste 		if (dwarf_get_AT_name(attr, &attr_str) != DW_DLV_OK) {
5015cf781b2eSEd Maste 			snprintf(unk_attr, sizeof(unk_attr),
5016cf781b2eSEd Maste 			    "[Unknown AT: %#x]", attr);
5017cf781b2eSEd Maste 			attr_str = unk_attr;
50182c23cb7cSEd Maste 		}
5019cf781b2eSEd Maste 		if (dwarf_attroffset(attr_list[i], &attroff, &de) !=
5020cf781b2eSEd Maste 		    DW_DLV_OK) {
5021cf781b2eSEd Maste 			warnx("dwarf_attroffset failed: %s", dwarf_errmsg(de));
5022cf781b2eSEd Maste 			attroff = 0;
5023cf781b2eSEd Maste 		}
5024cf781b2eSEd Maste 		printf("    <%jx>   %-18s: ", (uintmax_t) attroff, attr_str);
50252c23cb7cSEd Maste 		switch (form) {
50262c23cb7cSEd Maste 		case DW_FORM_ref_addr:
5027cf781b2eSEd Maste 		case DW_FORM_sec_offset:
50282c23cb7cSEd Maste 			if (dwarf_global_formref(attr_list[i], &v_off, &de) !=
50292c23cb7cSEd Maste 			    DW_DLV_OK) {
50302c23cb7cSEd Maste 				warnx("dwarf_global_formref failed: %s",
50312c23cb7cSEd Maste 				    dwarf_errmsg(de));
50322c23cb7cSEd Maste 				continue;
50332c23cb7cSEd Maste 			}
5034cf781b2eSEd Maste 			if (form == DW_FORM_ref_addr)
5035cf781b2eSEd Maste 				printf("<0x%jx>", (uintmax_t) v_off);
5036cf781b2eSEd Maste 			else
5037cf781b2eSEd Maste 				printf("0x%jx", (uintmax_t) v_off);
50382c23cb7cSEd Maste 			break;
50392c23cb7cSEd Maste 
50402c23cb7cSEd Maste 		case DW_FORM_ref1:
50412c23cb7cSEd Maste 		case DW_FORM_ref2:
50422c23cb7cSEd Maste 		case DW_FORM_ref4:
50432c23cb7cSEd Maste 		case DW_FORM_ref8:
50442c23cb7cSEd Maste 		case DW_FORM_ref_udata:
50452c23cb7cSEd Maste 			if (dwarf_formref(attr_list[i], &v_off, &de) !=
50462c23cb7cSEd Maste 			    DW_DLV_OK) {
50472c23cb7cSEd Maste 				warnx("dwarf_formref failed: %s",
50482c23cb7cSEd Maste 				    dwarf_errmsg(de));
50492c23cb7cSEd Maste 				continue;
50502c23cb7cSEd Maste 			}
50512c23cb7cSEd Maste 			v_off += cuoff;
5052cf781b2eSEd Maste 			printf("<0x%jx>", (uintmax_t) v_off);
50532c23cb7cSEd Maste 			break;
50542c23cb7cSEd Maste 
50552c23cb7cSEd Maste 		case DW_FORM_addr:
50562c23cb7cSEd Maste 			if (dwarf_formaddr(attr_list[i], &v_addr, &de) !=
50572c23cb7cSEd Maste 			    DW_DLV_OK) {
50582c23cb7cSEd Maste 				warnx("dwarf_formaddr failed: %s",
50592c23cb7cSEd Maste 				    dwarf_errmsg(de));
50602c23cb7cSEd Maste 				continue;
50612c23cb7cSEd Maste 			}
50622c23cb7cSEd Maste 			printf("%#jx", (uintmax_t) v_addr);
50632c23cb7cSEd Maste 			break;
50642c23cb7cSEd Maste 
50652c23cb7cSEd Maste 		case DW_FORM_data1:
50662c23cb7cSEd Maste 		case DW_FORM_data2:
50672c23cb7cSEd Maste 		case DW_FORM_data4:
50682c23cb7cSEd Maste 		case DW_FORM_data8:
50692c23cb7cSEd Maste 		case DW_FORM_udata:
50702c23cb7cSEd Maste 			if (dwarf_formudata(attr_list[i], &v_udata, &de) !=
50712c23cb7cSEd Maste 			    DW_DLV_OK) {
50722c23cb7cSEd Maste 				warnx("dwarf_formudata failed: %s",
50732c23cb7cSEd Maste 				    dwarf_errmsg(de));
50742c23cb7cSEd Maste 				continue;
50752c23cb7cSEd Maste 			}
5076cf781b2eSEd Maste 			if (attr == DW_AT_high_pc)
5077cf781b2eSEd Maste 				printf("0x%jx", (uintmax_t) v_udata);
5078cf781b2eSEd Maste 			else
50792c23cb7cSEd Maste 				printf("%ju", (uintmax_t) v_udata);
50802c23cb7cSEd Maste 			break;
50812c23cb7cSEd Maste 
50822c23cb7cSEd Maste 		case DW_FORM_sdata:
50832c23cb7cSEd Maste 			if (dwarf_formsdata(attr_list[i], &v_sdata, &de) !=
50842c23cb7cSEd Maste 			    DW_DLV_OK) {
50852c23cb7cSEd Maste 				warnx("dwarf_formudata failed: %s",
50862c23cb7cSEd Maste 				    dwarf_errmsg(de));
50872c23cb7cSEd Maste 				continue;
50882c23cb7cSEd Maste 			}
50892c23cb7cSEd Maste 			printf("%jd", (intmax_t) v_sdata);
50902c23cb7cSEd Maste 			break;
50912c23cb7cSEd Maste 
50922c23cb7cSEd Maste 		case DW_FORM_flag:
50932c23cb7cSEd Maste 			if (dwarf_formflag(attr_list[i], &v_bool, &de) !=
50942c23cb7cSEd Maste 			    DW_DLV_OK) {
50952c23cb7cSEd Maste 				warnx("dwarf_formflag failed: %s",
50962c23cb7cSEd Maste 				    dwarf_errmsg(de));
50972c23cb7cSEd Maste 				continue;
50982c23cb7cSEd Maste 			}
50992c23cb7cSEd Maste 			printf("%jd", (intmax_t) v_bool);
51002c23cb7cSEd Maste 			break;
51012c23cb7cSEd Maste 
5102cf781b2eSEd Maste 		case DW_FORM_flag_present:
5103cf781b2eSEd Maste 			putchar('1');
5104cf781b2eSEd Maste 			break;
5105cf781b2eSEd Maste 
51062c23cb7cSEd Maste 		case DW_FORM_string:
51072c23cb7cSEd Maste 		case DW_FORM_strp:
51082c23cb7cSEd Maste 			if (dwarf_formstring(attr_list[i], &v_str, &de) !=
51092c23cb7cSEd Maste 			    DW_DLV_OK) {
51102c23cb7cSEd Maste 				warnx("dwarf_formstring failed: %s",
51112c23cb7cSEd Maste 				    dwarf_errmsg(de));
51122c23cb7cSEd Maste 				continue;
51132c23cb7cSEd Maste 			}
51142c23cb7cSEd Maste 			if (form == DW_FORM_string)
51152c23cb7cSEd Maste 				printf("%s", v_str);
51162c23cb7cSEd Maste 			else
51172c23cb7cSEd Maste 				printf("(indirect string) %s", v_str);
51182c23cb7cSEd Maste 			break;
51192c23cb7cSEd Maste 
51202c23cb7cSEd Maste 		case DW_FORM_block:
51212c23cb7cSEd Maste 		case DW_FORM_block1:
51222c23cb7cSEd Maste 		case DW_FORM_block2:
51232c23cb7cSEd Maste 		case DW_FORM_block4:
51242c23cb7cSEd Maste 			if (dwarf_formblock(attr_list[i], &v_block, &de) !=
51252c23cb7cSEd Maste 			    DW_DLV_OK) {
51262c23cb7cSEd Maste 				warnx("dwarf_formblock failed: %s",
51272c23cb7cSEd Maste 				    dwarf_errmsg(de));
51282c23cb7cSEd Maste 				continue;
51292c23cb7cSEd Maste 			}
5130cf781b2eSEd Maste 			printf("%ju byte block:", (uintmax_t) v_block->bl_len);
51312c23cb7cSEd Maste 			b = v_block->bl_data;
51322c23cb7cSEd Maste 			for (j = 0; (Dwarf_Unsigned) j < v_block->bl_len; j++)
51332c23cb7cSEd Maste 				printf(" %x", b[j]);
5134cf781b2eSEd Maste 			printf("\t(");
5135cf781b2eSEd Maste 			dump_dwarf_block(re, v_block->bl_data, v_block->bl_len);
5136cf781b2eSEd Maste 			putchar(')');
51372c23cb7cSEd Maste 			break;
5138cf781b2eSEd Maste 
5139cf781b2eSEd Maste 		case DW_FORM_exprloc:
5140cf781b2eSEd Maste 			if (dwarf_formexprloc(attr_list[i], &v_udata, &v_expr,
5141cf781b2eSEd Maste 			    &de) != DW_DLV_OK) {
5142cf781b2eSEd Maste 				warnx("dwarf_formexprloc failed: %s",
5143cf781b2eSEd Maste 				    dwarf_errmsg(de));
5144cf781b2eSEd Maste 				continue;
5145cf781b2eSEd Maste 			}
5146cf781b2eSEd Maste 			printf("%ju byte block:", (uintmax_t) v_udata);
5147cf781b2eSEd Maste 			b = v_expr;
5148cf781b2eSEd Maste 			for (j = 0; (Dwarf_Unsigned) j < v_udata; j++)
5149cf781b2eSEd Maste 				printf(" %x", b[j]);
5150cf781b2eSEd Maste 			printf("\t(");
5151cf781b2eSEd Maste 			dump_dwarf_block(re, v_expr, v_udata);
5152cf781b2eSEd Maste 			putchar(')');
5153cf781b2eSEd Maste 			break;
5154cf781b2eSEd Maste 
5155cf781b2eSEd Maste 		case DW_FORM_ref_sig8:
5156cf781b2eSEd Maste 			if (dwarf_formsig8(attr_list[i], &v_sig8, &de) !=
5157cf781b2eSEd Maste 			    DW_DLV_OK) {
5158cf781b2eSEd Maste 				warnx("dwarf_formsig8 failed: %s",
5159cf781b2eSEd Maste 				    dwarf_errmsg(de));
5160cf781b2eSEd Maste 				continue;
5161cf781b2eSEd Maste 			}
5162cf781b2eSEd Maste 			p = (uint8_t *)(uintptr_t) &v_sig8.signature[0];
5163cf781b2eSEd Maste 			v_sig = re->dw_decode(&p, 8);
5164cf781b2eSEd Maste 			printf("signature: 0x%jx", (uintmax_t) v_sig);
51652c23cb7cSEd Maste 		}
51662c23cb7cSEd Maste 		switch (attr) {
51672c23cb7cSEd Maste 		case DW_AT_encoding:
51682c23cb7cSEd Maste 			if (dwarf_attrval_unsigned(die, attr, &ate, &de) !=
51692c23cb7cSEd Maste 			    DW_DLV_OK)
51702c23cb7cSEd Maste 				break;
51712c23cb7cSEd Maste 			if (dwarf_get_ATE_name(ate, &ate_str) != DW_DLV_OK)
5172cf781b2eSEd Maste 				ate_str = "DW_ATE_UNKNOWN";
51732c23cb7cSEd Maste 			printf("\t(%s)", &ate_str[strlen("DW_ATE_")]);
51742c23cb7cSEd Maste 			break;
5175cf781b2eSEd Maste 
5176cf781b2eSEd Maste 		case DW_AT_language:
5177cf781b2eSEd Maste 			if (dwarf_attrval_unsigned(die, attr, &lang, &de) !=
5178cf781b2eSEd Maste 			    DW_DLV_OK)
5179cf781b2eSEd Maste 				break;
5180cf781b2eSEd Maste 			if (dwarf_get_LANG_name(lang, &lang_str) != DW_DLV_OK)
5181cf781b2eSEd Maste 				break;
5182cf781b2eSEd Maste 			printf("\t(%s)", &lang_str[strlen("DW_LANG_")]);
5183cf781b2eSEd Maste 			break;
5184cf781b2eSEd Maste 
5185cf781b2eSEd Maste 		case DW_AT_location:
5186cf781b2eSEd Maste 		case DW_AT_string_length:
5187cf781b2eSEd Maste 		case DW_AT_return_addr:
5188cf781b2eSEd Maste 		case DW_AT_data_member_location:
5189cf781b2eSEd Maste 		case DW_AT_frame_base:
5190cf781b2eSEd Maste 		case DW_AT_segment:
5191cf781b2eSEd Maste 		case DW_AT_static_link:
5192cf781b2eSEd Maste 		case DW_AT_use_location:
5193cf781b2eSEd Maste 		case DW_AT_vtable_elem_location:
5194cf781b2eSEd Maste 			switch (form) {
5195cf781b2eSEd Maste 			case DW_FORM_data4:
5196cf781b2eSEd Maste 			case DW_FORM_data8:
5197cf781b2eSEd Maste 			case DW_FORM_sec_offset:
5198cf781b2eSEd Maste 				printf("\t(location list)");
5199cf781b2eSEd Maste 				break;
5200cf781b2eSEd Maste 			default:
5201cf781b2eSEd Maste 				break;
5202cf781b2eSEd Maste 			}
5203cf781b2eSEd Maste 
52042c23cb7cSEd Maste 		default:
52052c23cb7cSEd Maste 			break;
52062c23cb7cSEd Maste 		}
52072c23cb7cSEd Maste 		putchar('\n');
52082c23cb7cSEd Maste 	}
52092c23cb7cSEd Maste 
52102c23cb7cSEd Maste 
52112c23cb7cSEd Maste cont_search:
52122c23cb7cSEd Maste 	/* Search children. */
52132c23cb7cSEd Maste 	ret = dwarf_child(die, &ret_die, &de);
52142c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
52152c23cb7cSEd Maste 		warnx("dwarf_child: %s", dwarf_errmsg(de));
52162c23cb7cSEd Maste 	else if (ret == DW_DLV_OK)
52172c23cb7cSEd Maste 		dump_dwarf_die(re, ret_die, level + 1);
52182c23cb7cSEd Maste 
52192c23cb7cSEd Maste 	/* Search sibling. */
5220cf781b2eSEd Maste 	is_info = dwarf_get_die_infotypes_flag(die);
5221cf781b2eSEd Maste 	ret = dwarf_siblingof_b(re->dbg, die, &ret_die, is_info, &de);
52222c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
52232c23cb7cSEd Maste 		warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
52242c23cb7cSEd Maste 	else if (ret == DW_DLV_OK)
52252c23cb7cSEd Maste 		dump_dwarf_die(re, ret_die, level);
52262c23cb7cSEd Maste 
52272c23cb7cSEd Maste 	dwarf_dealloc(re->dbg, die, DW_DLA_DIE);
52282c23cb7cSEd Maste }
52292c23cb7cSEd Maste 
52302c23cb7cSEd Maste static void
5231cf781b2eSEd Maste set_cu_context(struct readelf *re, Dwarf_Half psize, Dwarf_Half osize,
5232cf781b2eSEd Maste     Dwarf_Half ver)
5233cf781b2eSEd Maste {
5234cf781b2eSEd Maste 
5235cf781b2eSEd Maste 	re->cu_psize = psize;
5236cf781b2eSEd Maste 	re->cu_osize = osize;
5237cf781b2eSEd Maste 	re->cu_ver = ver;
5238cf781b2eSEd Maste }
5239cf781b2eSEd Maste 
5240cf781b2eSEd Maste static void
5241cf781b2eSEd Maste dump_dwarf_info(struct readelf *re, Dwarf_Bool is_info)
52422c23cb7cSEd Maste {
52432c23cb7cSEd Maste 	struct section *s;
52442c23cb7cSEd Maste 	Dwarf_Die die;
52452c23cb7cSEd Maste 	Dwarf_Error de;
5246cf781b2eSEd Maste 	Dwarf_Half tag, version, pointer_size, off_size;
52472c23cb7cSEd Maste 	Dwarf_Off cu_offset, cu_length;
52482c23cb7cSEd Maste 	Dwarf_Off aboff;
5249cf781b2eSEd Maste 	Dwarf_Unsigned typeoff;
5250cf781b2eSEd Maste 	Dwarf_Sig8 sig8;
5251cf781b2eSEd Maste 	Dwarf_Unsigned sig;
5252cf781b2eSEd Maste 	uint8_t *p;
5253cf781b2eSEd Maste 	const char *sn;
5254cf781b2eSEd Maste 	int i, ret;
52552c23cb7cSEd Maste 
5256cf781b2eSEd Maste 	sn = is_info ? ".debug_info" : ".debug_types";
52572c23cb7cSEd Maste 
52582c23cb7cSEd Maste 	s = NULL;
52592c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
52602c23cb7cSEd Maste 		s = &re->sl[i];
5261cf781b2eSEd Maste 		if (s->name != NULL && !strcmp(s->name, sn))
52622c23cb7cSEd Maste 			break;
52632c23cb7cSEd Maste 	}
52642c23cb7cSEd Maste 	if ((size_t) i >= re->shnum)
52652c23cb7cSEd Maste 		return;
52662c23cb7cSEd Maste 
5267cf781b2eSEd Maste 	do {
5268cf781b2eSEd Maste 		printf("\nDump of debug contents of section %s:\n", sn);
52692c23cb7cSEd Maste 
5270cf781b2eSEd Maste 		while ((ret = dwarf_next_cu_header_c(re->dbg, is_info, NULL,
5271cf781b2eSEd Maste 		    &version, &aboff, &pointer_size, &off_size, NULL, &sig8,
5272cf781b2eSEd Maste 		    &typeoff, NULL, &de)) == DW_DLV_OK) {
5273cf781b2eSEd Maste 			set_cu_context(re, pointer_size, off_size, version);
52742c23cb7cSEd Maste 			die = NULL;
5275cf781b2eSEd Maste 			while (dwarf_siblingof_b(re->dbg, die, &die, is_info,
5276cf781b2eSEd Maste 			    &de) == DW_DLV_OK) {
52772c23cb7cSEd Maste 				if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
52782c23cb7cSEd Maste 					warnx("dwarf_tag failed: %s",
52792c23cb7cSEd Maste 					    dwarf_errmsg(de));
5280cf781b2eSEd Maste 					continue;
52812c23cb7cSEd Maste 				}
52822c23cb7cSEd Maste 				/* XXX: What about DW_TAG_partial_unit? */
5283cf781b2eSEd Maste 				if ((is_info && tag == DW_TAG_compile_unit) ||
5284cf781b2eSEd Maste 				    (!is_info && tag == DW_TAG_type_unit))
52852c23cb7cSEd Maste 					break;
52862c23cb7cSEd Maste 			}
5287cf781b2eSEd Maste 			if (die == NULL && is_info) {
5288cf781b2eSEd Maste 				warnx("could not find DW_TAG_compile_unit "
5289cf781b2eSEd Maste 				    "die");
5290cf781b2eSEd Maste 				continue;
5291cf781b2eSEd Maste 			} else if (die == NULL && !is_info) {
5292cf781b2eSEd Maste 				warnx("could not find DW_TAG_type_unit die");
5293cf781b2eSEd Maste 				continue;
52942c23cb7cSEd Maste 			}
52952c23cb7cSEd Maste 
5296cf781b2eSEd Maste 			if (dwarf_die_CU_offset_range(die, &cu_offset,
5297cf781b2eSEd Maste 			    &cu_length, &de) != DW_DLV_OK) {
52982c23cb7cSEd Maste 				warnx("dwarf_die_CU_offset failed: %s",
52992c23cb7cSEd Maste 				    dwarf_errmsg(de));
53002c23cb7cSEd Maste 				continue;
53012c23cb7cSEd Maste 			}
53022c23cb7cSEd Maste 
5303cf781b2eSEd Maste 			cu_length -= off_size == 4 ? 4 : 12;
5304cf781b2eSEd Maste 
5305cf781b2eSEd Maste 			sig = 0;
5306cf781b2eSEd Maste 			if (!is_info) {
5307cf781b2eSEd Maste 				p = (uint8_t *)(uintptr_t) &sig8.signature[0];
5308cf781b2eSEd Maste 				sig = re->dw_decode(&p, 8);
5309cf781b2eSEd Maste 			}
5310cf781b2eSEd Maste 
5311cf781b2eSEd Maste 			printf("\n  Type Unit @ offset 0x%jx:\n",
5312cf781b2eSEd Maste 			    (uintmax_t) cu_offset);
5313cf781b2eSEd Maste 			printf("    Length:\t\t%#jx (%d-bit)\n",
5314cf781b2eSEd Maste 			    (uintmax_t) cu_length, off_size == 4 ? 32 : 64);
53152c23cb7cSEd Maste 			printf("    Version:\t\t%u\n", version);
5316cf781b2eSEd Maste 			printf("    Abbrev Offset:\t0x%jx\n",
5317cf781b2eSEd Maste 			    (uintmax_t) aboff);
53182c23cb7cSEd Maste 			printf("    Pointer Size:\t%u\n", pointer_size);
5319cf781b2eSEd Maste 			if (!is_info) {
5320cf781b2eSEd Maste 				printf("    Signature:\t\t0x%016jx\n",
5321cf781b2eSEd Maste 				    (uintmax_t) sig);
5322cf781b2eSEd Maste 				printf("    Type Offset:\t0x%jx\n",
5323cf781b2eSEd Maste 				    (uintmax_t) typeoff);
5324cf781b2eSEd Maste 			}
53252c23cb7cSEd Maste 
53262c23cb7cSEd Maste 			dump_dwarf_die(re, die, 0);
53272c23cb7cSEd Maste 		}
53282c23cb7cSEd Maste 		if (ret == DW_DLV_ERROR)
53292c23cb7cSEd Maste 			warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
5330cf781b2eSEd Maste 		if (is_info)
5331cf781b2eSEd Maste 			break;
5332cf781b2eSEd Maste 	} while (dwarf_next_types_section(re->dbg, &de) == DW_DLV_OK);
53332c23cb7cSEd Maste }
53342c23cb7cSEd Maste 
53352c23cb7cSEd Maste static void
53362c23cb7cSEd Maste dump_dwarf_abbrev(struct readelf *re)
53372c23cb7cSEd Maste {
53382c23cb7cSEd Maste 	Dwarf_Abbrev ab;
53392c23cb7cSEd Maste 	Dwarf_Off aboff, atoff;
53402c23cb7cSEd Maste 	Dwarf_Unsigned length, attr_count;
53412c23cb7cSEd Maste 	Dwarf_Signed flag, form;
53422c23cb7cSEd Maste 	Dwarf_Half tag, attr;
53432c23cb7cSEd Maste 	Dwarf_Error de;
53442c23cb7cSEd Maste 	const char *tag_str, *attr_str, *form_str;
5345cf781b2eSEd Maste 	char unk_tag[32], unk_attr[32], unk_form[32];
53462c23cb7cSEd Maste 	int i, j, ret;
53472c23cb7cSEd Maste 
53482c23cb7cSEd Maste 	printf("\nContents of section .debug_abbrev:\n\n");
53492c23cb7cSEd Maste 
53502c23cb7cSEd Maste 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, &aboff,
53512c23cb7cSEd Maste 	    NULL, NULL, &de)) ==  DW_DLV_OK) {
53522c23cb7cSEd Maste 		printf("  Number TAG\n");
53532c23cb7cSEd Maste 		i = 0;
53542c23cb7cSEd Maste 		while ((ret = dwarf_get_abbrev(re->dbg, aboff, &ab, &length,
53552c23cb7cSEd Maste 		    &attr_count, &de)) == DW_DLV_OK) {
53562c23cb7cSEd Maste 			if (length == 1) {
53572c23cb7cSEd Maste 				dwarf_dealloc(re->dbg, ab, DW_DLA_ABBREV);
53582c23cb7cSEd Maste 				break;
53592c23cb7cSEd Maste 			}
53602c23cb7cSEd Maste 			aboff += length;
53612c23cb7cSEd Maste 			printf("%4d", ++i);
53622c23cb7cSEd Maste 			if (dwarf_get_abbrev_tag(ab, &tag, &de) != DW_DLV_OK) {
53632c23cb7cSEd Maste 				warnx("dwarf_get_abbrev_tag failed: %s",
53642c23cb7cSEd Maste 				    dwarf_errmsg(de));
53652c23cb7cSEd Maste 				goto next_abbrev;
53662c23cb7cSEd Maste 			}
53672c23cb7cSEd Maste 			if (dwarf_get_TAG_name(tag, &tag_str) != DW_DLV_OK) {
5368cf781b2eSEd Maste 				snprintf(unk_tag, sizeof(unk_tag),
5369cf781b2eSEd Maste 				    "[Unknown Tag: %#x]", tag);
5370cf781b2eSEd Maste 				tag_str = unk_tag;
53712c23cb7cSEd Maste 			}
53722c23cb7cSEd Maste 			if (dwarf_get_abbrev_children_flag(ab, &flag, &de) !=
53732c23cb7cSEd Maste 			    DW_DLV_OK) {
53742c23cb7cSEd Maste 				warnx("dwarf_get_abbrev_children_flag failed:"
53752c23cb7cSEd Maste 				    " %s", dwarf_errmsg(de));
53762c23cb7cSEd Maste 				goto next_abbrev;
53772c23cb7cSEd Maste 			}
53782c23cb7cSEd Maste 			printf("      %s    %s\n", tag_str,
53792c23cb7cSEd Maste 			    flag ? "[has children]" : "[no children]");
53802c23cb7cSEd Maste 			for (j = 0; (Dwarf_Unsigned) j < attr_count; j++) {
53812c23cb7cSEd Maste 				if (dwarf_get_abbrev_entry(ab, (Dwarf_Signed) j,
53822c23cb7cSEd Maste 				    &attr, &form, &atoff, &de) != DW_DLV_OK) {
53832c23cb7cSEd Maste 					warnx("dwarf_get_abbrev_entry failed:"
53842c23cb7cSEd Maste 					    " %s", dwarf_errmsg(de));
53852c23cb7cSEd Maste 					continue;
53862c23cb7cSEd Maste 				}
53872c23cb7cSEd Maste 				if (dwarf_get_AT_name(attr, &attr_str) !=
53882c23cb7cSEd Maste 				    DW_DLV_OK) {
5389cf781b2eSEd Maste 					snprintf(unk_attr, sizeof(unk_attr),
5390cf781b2eSEd Maste 					    "[Unknown AT: %#x]", attr);
5391cf781b2eSEd Maste 					attr_str = unk_attr;
53922c23cb7cSEd Maste 				}
53932c23cb7cSEd Maste 				if (dwarf_get_FORM_name(form, &form_str) !=
53942c23cb7cSEd Maste 				    DW_DLV_OK) {
5395cf781b2eSEd Maste 					snprintf(unk_form, sizeof(unk_form),
5396cf781b2eSEd Maste 					    "[Unknown Form: %#x]",
5397cf781b2eSEd Maste 					    (Dwarf_Half) form);
5398cf781b2eSEd Maste 					form_str = unk_form;
53992c23cb7cSEd Maste 				}
54002c23cb7cSEd Maste 				printf("    %-18s %s\n", attr_str, form_str);
54012c23cb7cSEd Maste 			}
54022c23cb7cSEd Maste 		next_abbrev:
54032c23cb7cSEd Maste 			dwarf_dealloc(re->dbg, ab, DW_DLA_ABBREV);
54042c23cb7cSEd Maste 		}
54052c23cb7cSEd Maste 		if (ret != DW_DLV_OK)
54062c23cb7cSEd Maste 			warnx("dwarf_get_abbrev: %s", dwarf_errmsg(de));
54072c23cb7cSEd Maste 	}
54082c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
54092c23cb7cSEd Maste 		warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
54102c23cb7cSEd Maste }
54112c23cb7cSEd Maste 
54122c23cb7cSEd Maste static void
54132c23cb7cSEd Maste dump_dwarf_pubnames(struct readelf *re)
54142c23cb7cSEd Maste {
54152c23cb7cSEd Maste 	struct section *s;
54162c23cb7cSEd Maste 	Dwarf_Off die_off;
54172c23cb7cSEd Maste 	Dwarf_Unsigned offset, length, nt_cu_offset, nt_cu_length;
54182c23cb7cSEd Maste 	Dwarf_Signed cnt;
54192c23cb7cSEd Maste 	Dwarf_Global *globs;
54202c23cb7cSEd Maste 	Dwarf_Half nt_version;
54212c23cb7cSEd Maste 	Dwarf_Error de;
54222c23cb7cSEd Maste 	Elf_Data *d;
54232c23cb7cSEd Maste 	char *glob_name;
54242c23cb7cSEd Maste 	int i, dwarf_size, elferr;
54252c23cb7cSEd Maste 
54262c23cb7cSEd Maste 	printf("\nContents of the .debug_pubnames section:\n");
54272c23cb7cSEd Maste 
54282c23cb7cSEd Maste 	s = NULL;
54292c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
54302c23cb7cSEd Maste 		s = &re->sl[i];
54312c23cb7cSEd Maste 		if (s->name != NULL && !strcmp(s->name, ".debug_pubnames"))
54322c23cb7cSEd Maste 			break;
54332c23cb7cSEd Maste 	}
54342c23cb7cSEd Maste 	if ((size_t) i >= re->shnum)
54352c23cb7cSEd Maste 		return;
54362c23cb7cSEd Maste 
54372c23cb7cSEd Maste 	(void) elf_errno();
54382c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
54392c23cb7cSEd Maste 		elferr = elf_errno();
54402c23cb7cSEd Maste 		if (elferr != 0)
54412c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
54422c23cb7cSEd Maste 		return;
54432c23cb7cSEd Maste 	}
54442c23cb7cSEd Maste 	if (d->d_size <= 0)
54452c23cb7cSEd Maste 		return;
54462c23cb7cSEd Maste 
54472c23cb7cSEd Maste 	/* Read in .debug_pubnames section table header. */
54482c23cb7cSEd Maste 	offset = 0;
54492c23cb7cSEd Maste 	length = re->dw_read(d, &offset, 4);
54502c23cb7cSEd Maste 	if (length == 0xffffffff) {
54512c23cb7cSEd Maste 		dwarf_size = 8;
54522c23cb7cSEd Maste 		length = re->dw_read(d, &offset, 8);
54532c23cb7cSEd Maste 	} else
54542c23cb7cSEd Maste 		dwarf_size = 4;
54552c23cb7cSEd Maste 
54562c23cb7cSEd Maste 	if (length > d->d_size - offset) {
54572c23cb7cSEd Maste 		warnx("invalid .dwarf_pubnames section");
54582c23cb7cSEd Maste 		return;
54592c23cb7cSEd Maste 	}
54602c23cb7cSEd Maste 
54612c23cb7cSEd Maste 	nt_version = re->dw_read(d, &offset, 2);
54622c23cb7cSEd Maste 	nt_cu_offset = re->dw_read(d, &offset, dwarf_size);
54632c23cb7cSEd Maste 	nt_cu_length = re->dw_read(d, &offset, dwarf_size);
54642c23cb7cSEd Maste 	printf("  Length:\t\t\t\t%ju\n", (uintmax_t) length);
54652c23cb7cSEd Maste 	printf("  Version:\t\t\t\t%u\n", nt_version);
54662c23cb7cSEd Maste 	printf("  Offset into .debug_info section:\t%ju\n",
54672c23cb7cSEd Maste 	    (uintmax_t) nt_cu_offset);
54682c23cb7cSEd Maste 	printf("  Size of area in .debug_info section:\t%ju\n",
54692c23cb7cSEd Maste 	    (uintmax_t) nt_cu_length);
54702c23cb7cSEd Maste 
54712c23cb7cSEd Maste 	if (dwarf_get_globals(re->dbg, &globs, &cnt, &de) != DW_DLV_OK) {
54722c23cb7cSEd Maste 		warnx("dwarf_get_globals failed: %s", dwarf_errmsg(de));
54732c23cb7cSEd Maste 		return;
54742c23cb7cSEd Maste 	}
54752c23cb7cSEd Maste 
54762c23cb7cSEd Maste 	printf("\n    Offset      Name\n");
54772c23cb7cSEd Maste 	for (i = 0; i < cnt; i++) {
54782c23cb7cSEd Maste 		if (dwarf_globname(globs[i], &glob_name, &de) != DW_DLV_OK) {
54792c23cb7cSEd Maste 			warnx("dwarf_globname failed: %s", dwarf_errmsg(de));
54802c23cb7cSEd Maste 			continue;
54812c23cb7cSEd Maste 		}
54822c23cb7cSEd Maste 		if (dwarf_global_die_offset(globs[i], &die_off, &de) !=
54832c23cb7cSEd Maste 		    DW_DLV_OK) {
54842c23cb7cSEd Maste 			warnx("dwarf_global_die_offset failed: %s",
54852c23cb7cSEd Maste 			    dwarf_errmsg(de));
54862c23cb7cSEd Maste 			continue;
54872c23cb7cSEd Maste 		}
54882c23cb7cSEd Maste 		printf("    %-11ju %s\n", (uintmax_t) die_off, glob_name);
54892c23cb7cSEd Maste 	}
54902c23cb7cSEd Maste }
54912c23cb7cSEd Maste 
54922c23cb7cSEd Maste static void
54932c23cb7cSEd Maste dump_dwarf_aranges(struct readelf *re)
54942c23cb7cSEd Maste {
54952c23cb7cSEd Maste 	struct section *s;
54962c23cb7cSEd Maste 	Dwarf_Arange *aranges;
54972c23cb7cSEd Maste 	Dwarf_Addr start;
54982c23cb7cSEd Maste 	Dwarf_Unsigned offset, length, as_cu_offset;
54992c23cb7cSEd Maste 	Dwarf_Off die_off;
55002c23cb7cSEd Maste 	Dwarf_Signed cnt;
55012c23cb7cSEd Maste 	Dwarf_Half as_version, as_addrsz, as_segsz;
55022c23cb7cSEd Maste 	Dwarf_Error de;
55032c23cb7cSEd Maste 	Elf_Data *d;
55042c23cb7cSEd Maste 	int i, dwarf_size, elferr;
55052c23cb7cSEd Maste 
55062c23cb7cSEd Maste 	printf("\nContents of section .debug_aranges:\n");
55072c23cb7cSEd Maste 
55082c23cb7cSEd Maste 	s = NULL;
55092c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
55102c23cb7cSEd Maste 		s = &re->sl[i];
55112c23cb7cSEd Maste 		if (s->name != NULL && !strcmp(s->name, ".debug_aranges"))
55122c23cb7cSEd Maste 			break;
55132c23cb7cSEd Maste 	}
55142c23cb7cSEd Maste 	if ((size_t) i >= re->shnum)
55152c23cb7cSEd Maste 		return;
55162c23cb7cSEd Maste 
55172c23cb7cSEd Maste 	(void) elf_errno();
55182c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
55192c23cb7cSEd Maste 		elferr = elf_errno();
55202c23cb7cSEd Maste 		if (elferr != 0)
55212c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
55222c23cb7cSEd Maste 		return;
55232c23cb7cSEd Maste 	}
55242c23cb7cSEd Maste 	if (d->d_size <= 0)
55252c23cb7cSEd Maste 		return;
55262c23cb7cSEd Maste 
55272c23cb7cSEd Maste 	/* Read in the .debug_aranges section table header. */
55282c23cb7cSEd Maste 	offset = 0;
55292c23cb7cSEd Maste 	length = re->dw_read(d, &offset, 4);
55302c23cb7cSEd Maste 	if (length == 0xffffffff) {
55312c23cb7cSEd Maste 		dwarf_size = 8;
55322c23cb7cSEd Maste 		length = re->dw_read(d, &offset, 8);
55332c23cb7cSEd Maste 	} else
55342c23cb7cSEd Maste 		dwarf_size = 4;
55352c23cb7cSEd Maste 
55362c23cb7cSEd Maste 	if (length > d->d_size - offset) {
55372c23cb7cSEd Maste 		warnx("invalid .dwarf_aranges section");
55382c23cb7cSEd Maste 		return;
55392c23cb7cSEd Maste 	}
55402c23cb7cSEd Maste 
55412c23cb7cSEd Maste 	as_version = re->dw_read(d, &offset, 2);
55422c23cb7cSEd Maste 	as_cu_offset = re->dw_read(d, &offset, dwarf_size);
55432c23cb7cSEd Maste 	as_addrsz = re->dw_read(d, &offset, 1);
55442c23cb7cSEd Maste 	as_segsz = re->dw_read(d, &offset, 1);
55452c23cb7cSEd Maste 
55462c23cb7cSEd Maste 	printf("  Length:\t\t\t%ju\n", (uintmax_t) length);
55472c23cb7cSEd Maste 	printf("  Version:\t\t\t%u\n", as_version);
55482c23cb7cSEd Maste 	printf("  Offset into .debug_info:\t%ju\n", (uintmax_t) as_cu_offset);
55492c23cb7cSEd Maste 	printf("  Pointer Size:\t\t\t%u\n", as_addrsz);
55502c23cb7cSEd Maste 	printf("  Segment Size:\t\t\t%u\n", as_segsz);
55512c23cb7cSEd Maste 
55522c23cb7cSEd Maste 	if (dwarf_get_aranges(re->dbg, &aranges, &cnt, &de) != DW_DLV_OK) {
55532c23cb7cSEd Maste 		warnx("dwarf_get_aranges failed: %s", dwarf_errmsg(de));
55542c23cb7cSEd Maste 		return;
55552c23cb7cSEd Maste 	}
55562c23cb7cSEd Maste 
55572c23cb7cSEd Maste 	printf("\n    Address  Length\n");
55582c23cb7cSEd Maste 	for (i = 0; i < cnt; i++) {
55592c23cb7cSEd Maste 		if (dwarf_get_arange_info(aranges[i], &start, &length,
55602c23cb7cSEd Maste 		    &die_off, &de) != DW_DLV_OK) {
55612c23cb7cSEd Maste 			warnx("dwarf_get_arange_info failed: %s",
55622c23cb7cSEd Maste 			    dwarf_errmsg(de));
55632c23cb7cSEd Maste 			continue;
55642c23cb7cSEd Maste 		}
55652c23cb7cSEd Maste 		printf("    %08jx %ju\n", (uintmax_t) start,
55662c23cb7cSEd Maste 		    (uintmax_t) length);
55672c23cb7cSEd Maste 	}
55682c23cb7cSEd Maste }
55692c23cb7cSEd Maste 
55702c23cb7cSEd Maste static void
55712c23cb7cSEd Maste dump_dwarf_ranges_foreach(struct readelf *re, Dwarf_Die die, Dwarf_Addr base)
55722c23cb7cSEd Maste {
55732c23cb7cSEd Maste 	Dwarf_Attribute *attr_list;
55742c23cb7cSEd Maste 	Dwarf_Ranges *ranges;
55752c23cb7cSEd Maste 	Dwarf_Die ret_die;
55762c23cb7cSEd Maste 	Dwarf_Error de;
55772c23cb7cSEd Maste 	Dwarf_Addr base0;
55782c23cb7cSEd Maste 	Dwarf_Half attr;
55792c23cb7cSEd Maste 	Dwarf_Signed attr_count, cnt;
55802c23cb7cSEd Maste 	Dwarf_Unsigned off, bytecnt;
55812c23cb7cSEd Maste 	int i, j, ret;
55822c23cb7cSEd Maste 
55832c23cb7cSEd Maste 	if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
55842c23cb7cSEd Maste 	    DW_DLV_OK) {
55852c23cb7cSEd Maste 		if (ret == DW_DLV_ERROR)
55862c23cb7cSEd Maste 			warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
55872c23cb7cSEd Maste 		goto cont_search;
55882c23cb7cSEd Maste 	}
55892c23cb7cSEd Maste 
55902c23cb7cSEd Maste 	for (i = 0; i < attr_count; i++) {
55912c23cb7cSEd Maste 		if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
55922c23cb7cSEd Maste 			warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
55932c23cb7cSEd Maste 			continue;
55942c23cb7cSEd Maste 		}
55952c23cb7cSEd Maste 		if (attr != DW_AT_ranges)
55962c23cb7cSEd Maste 			continue;
55972c23cb7cSEd Maste 		if (dwarf_formudata(attr_list[i], &off, &de) != DW_DLV_OK) {
55982c23cb7cSEd Maste 			warnx("dwarf_formudata failed: %s", dwarf_errmsg(de));
55992c23cb7cSEd Maste 			continue;
56002c23cb7cSEd Maste 		}
56012c23cb7cSEd Maste 		if (dwarf_get_ranges(re->dbg, (Dwarf_Off) off, &ranges, &cnt,
56022c23cb7cSEd Maste 		    &bytecnt, &de) != DW_DLV_OK)
56032c23cb7cSEd Maste 			continue;
56042c23cb7cSEd Maste 		base0 = base;
56052c23cb7cSEd Maste 		for (j = 0; j < cnt; j++) {
56062c23cb7cSEd Maste 			printf("    %08jx ", (uintmax_t) off);
56072c23cb7cSEd Maste 			if (ranges[j].dwr_type == DW_RANGES_END) {
56082c23cb7cSEd Maste 				printf("%s\n", "<End of list>");
56092c23cb7cSEd Maste 				continue;
56102c23cb7cSEd Maste 			} else if (ranges[j].dwr_type ==
56112c23cb7cSEd Maste 			    DW_RANGES_ADDRESS_SELECTION) {
56122c23cb7cSEd Maste 				base0 = ranges[j].dwr_addr2;
56132c23cb7cSEd Maste 				continue;
56142c23cb7cSEd Maste 			}
56152c23cb7cSEd Maste 			if (re->ec == ELFCLASS32)
56162c23cb7cSEd Maste 				printf("%08jx %08jx\n",
56172c23cb7cSEd Maste 				    ranges[j].dwr_addr1 + base0,
56182c23cb7cSEd Maste 				    ranges[j].dwr_addr2 + base0);
56192c23cb7cSEd Maste 			else
56202c23cb7cSEd Maste 				printf("%016jx %016jx\n",
56212c23cb7cSEd Maste 				    ranges[j].dwr_addr1 + base0,
56222c23cb7cSEd Maste 				    ranges[j].dwr_addr2 + base0);
56232c23cb7cSEd Maste 		}
56242c23cb7cSEd Maste 	}
56252c23cb7cSEd Maste 
56262c23cb7cSEd Maste cont_search:
56272c23cb7cSEd Maste 	/* Search children. */
56282c23cb7cSEd Maste 	ret = dwarf_child(die, &ret_die, &de);
56292c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
56302c23cb7cSEd Maste 		warnx("dwarf_child: %s", dwarf_errmsg(de));
56312c23cb7cSEd Maste 	else if (ret == DW_DLV_OK)
56322c23cb7cSEd Maste 		dump_dwarf_ranges_foreach(re, ret_die, base);
56332c23cb7cSEd Maste 
56342c23cb7cSEd Maste 	/* Search sibling. */
56352c23cb7cSEd Maste 	ret = dwarf_siblingof(re->dbg, die, &ret_die, &de);
56362c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
56372c23cb7cSEd Maste 		warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
56382c23cb7cSEd Maste 	else if (ret == DW_DLV_OK)
56392c23cb7cSEd Maste 		dump_dwarf_ranges_foreach(re, ret_die, base);
56402c23cb7cSEd Maste }
56412c23cb7cSEd Maste 
56422c23cb7cSEd Maste static void
56432c23cb7cSEd Maste dump_dwarf_ranges(struct readelf *re)
56442c23cb7cSEd Maste {
56452c23cb7cSEd Maste 	Dwarf_Ranges *ranges;
56462c23cb7cSEd Maste 	Dwarf_Die die;
56472c23cb7cSEd Maste 	Dwarf_Signed cnt;
56482c23cb7cSEd Maste 	Dwarf_Unsigned bytecnt;
56492c23cb7cSEd Maste 	Dwarf_Half tag;
56502c23cb7cSEd Maste 	Dwarf_Error de;
56512c23cb7cSEd Maste 	Dwarf_Unsigned lowpc;
56522c23cb7cSEd Maste 	int ret;
56532c23cb7cSEd Maste 
56542c23cb7cSEd Maste 	if (dwarf_get_ranges(re->dbg, 0, &ranges, &cnt, &bytecnt, &de) !=
56552c23cb7cSEd Maste 	    DW_DLV_OK)
56562c23cb7cSEd Maste 		return;
56572c23cb7cSEd Maste 
56582c23cb7cSEd Maste 	printf("Contents of the .debug_ranges section:\n\n");
56592c23cb7cSEd Maste 	if (re->ec == ELFCLASS32)
56602c23cb7cSEd Maste 		printf("    %-8s %-8s %s\n", "Offset", "Begin", "End");
56612c23cb7cSEd Maste 	else
56622c23cb7cSEd Maste 		printf("    %-8s %-16s %s\n", "Offset", "Begin", "End");
56632c23cb7cSEd Maste 
56642c23cb7cSEd Maste 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
56652c23cb7cSEd Maste 	    NULL, &de)) == DW_DLV_OK) {
56662c23cb7cSEd Maste 		die = NULL;
56672c23cb7cSEd Maste 		if (dwarf_siblingof(re->dbg, die, &die, &de) != DW_DLV_OK)
56682c23cb7cSEd Maste 			continue;
56692c23cb7cSEd Maste 		if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
56702c23cb7cSEd Maste 			warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
56712c23cb7cSEd Maste 			continue;
56722c23cb7cSEd Maste 		}
56732c23cb7cSEd Maste 		/* XXX: What about DW_TAG_partial_unit? */
56742c23cb7cSEd Maste 		lowpc = 0;
56752c23cb7cSEd Maste 		if (tag == DW_TAG_compile_unit) {
56762c23cb7cSEd Maste 			if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lowpc,
56772c23cb7cSEd Maste 			    &de) != DW_DLV_OK)
56782c23cb7cSEd Maste 				lowpc = 0;
56792c23cb7cSEd Maste 		}
56802c23cb7cSEd Maste 
56812c23cb7cSEd Maste 		dump_dwarf_ranges_foreach(re, die, (Dwarf_Addr) lowpc);
56822c23cb7cSEd Maste 	}
56832c23cb7cSEd Maste 	putchar('\n');
56842c23cb7cSEd Maste }
56852c23cb7cSEd Maste 
56862c23cb7cSEd Maste static void
56872c23cb7cSEd Maste dump_dwarf_macinfo(struct readelf *re)
56882c23cb7cSEd Maste {
56892c23cb7cSEd Maste 	Dwarf_Unsigned offset;
56902c23cb7cSEd Maste 	Dwarf_Signed cnt;
56912c23cb7cSEd Maste 	Dwarf_Macro_Details *md;
56922c23cb7cSEd Maste 	Dwarf_Error de;
56932c23cb7cSEd Maste 	const char *mi_str;
5694cf781b2eSEd Maste 	char unk_mi[32];
56952c23cb7cSEd Maste 	int i;
56962c23cb7cSEd Maste 
56972c23cb7cSEd Maste #define	_MAX_MACINFO_ENTRY	65535
56982c23cb7cSEd Maste 
56992c23cb7cSEd Maste 	printf("\nContents of section .debug_macinfo:\n\n");
57002c23cb7cSEd Maste 
57012c23cb7cSEd Maste 	offset = 0;
57022c23cb7cSEd Maste 	while (dwarf_get_macro_details(re->dbg, offset, _MAX_MACINFO_ENTRY,
57032c23cb7cSEd Maste 	    &cnt, &md, &de) == DW_DLV_OK) {
57042c23cb7cSEd Maste 		for (i = 0; i < cnt; i++) {
57052c23cb7cSEd Maste 			offset = md[i].dmd_offset + 1;
57062c23cb7cSEd Maste 			if (md[i].dmd_type == 0)
57072c23cb7cSEd Maste 				break;
57082c23cb7cSEd Maste 			if (dwarf_get_MACINFO_name(md[i].dmd_type, &mi_str) !=
57092c23cb7cSEd Maste 			    DW_DLV_OK) {
5710cf781b2eSEd Maste 				snprintf(unk_mi, sizeof(unk_mi),
5711cf781b2eSEd Maste 				    "[Unknown MACINFO: %#x]", md[i].dmd_type);
5712cf781b2eSEd Maste 				mi_str = unk_mi;
57132c23cb7cSEd Maste 			}
57142c23cb7cSEd Maste 			printf(" %s", mi_str);
57152c23cb7cSEd Maste 			switch (md[i].dmd_type) {
57162c23cb7cSEd Maste 			case DW_MACINFO_define:
57172c23cb7cSEd Maste 			case DW_MACINFO_undef:
57182c23cb7cSEd Maste 				printf(" - lineno : %jd macro : %s\n",
57192c23cb7cSEd Maste 				    (intmax_t) md[i].dmd_lineno,
57202c23cb7cSEd Maste 				    md[i].dmd_macro);
57212c23cb7cSEd Maste 				break;
57222c23cb7cSEd Maste 			case DW_MACINFO_start_file:
57232c23cb7cSEd Maste 				printf(" - lineno : %jd filenum : %jd\n",
57242c23cb7cSEd Maste 				    (intmax_t) md[i].dmd_lineno,
57252c23cb7cSEd Maste 				    (intmax_t) md[i].dmd_fileindex);
57262c23cb7cSEd Maste 				break;
57272c23cb7cSEd Maste 			default:
57282c23cb7cSEd Maste 				putchar('\n');
57292c23cb7cSEd Maste 				break;
57302c23cb7cSEd Maste 			}
57312c23cb7cSEd Maste 		}
57322c23cb7cSEd Maste 	}
57332c23cb7cSEd Maste 
57342c23cb7cSEd Maste #undef	_MAX_MACINFO_ENTRY
57352c23cb7cSEd Maste }
57362c23cb7cSEd Maste 
57372c23cb7cSEd Maste static void
5738cf781b2eSEd Maste dump_dwarf_frame_inst(struct readelf *re, Dwarf_Cie cie, uint8_t *insts,
5739cf781b2eSEd Maste     Dwarf_Unsigned len, Dwarf_Unsigned caf, Dwarf_Signed daf, Dwarf_Addr pc,
5740cf781b2eSEd Maste     Dwarf_Debug dbg)
57412c23cb7cSEd Maste {
57422c23cb7cSEd Maste 	Dwarf_Frame_Op *oplist;
57432c23cb7cSEd Maste 	Dwarf_Signed opcnt, delta;
57442c23cb7cSEd Maste 	Dwarf_Small op;
57452c23cb7cSEd Maste 	Dwarf_Error de;
57462c23cb7cSEd Maste 	const char *op_str;
5747cf781b2eSEd Maste 	char unk_op[32];
57482c23cb7cSEd Maste 	int i;
57492c23cb7cSEd Maste 
57502c23cb7cSEd Maste 	if (dwarf_expand_frame_instructions(cie, insts, len, &oplist,
57512c23cb7cSEd Maste 	    &opcnt, &de) != DW_DLV_OK) {
57522c23cb7cSEd Maste 		warnx("dwarf_expand_frame_instructions failed: %s",
57532c23cb7cSEd Maste 		    dwarf_errmsg(de));
57542c23cb7cSEd Maste 		return;
57552c23cb7cSEd Maste 	}
57562c23cb7cSEd Maste 
57572c23cb7cSEd Maste 	for (i = 0; i < opcnt; i++) {
57582c23cb7cSEd Maste 		if (oplist[i].fp_base_op != 0)
57592c23cb7cSEd Maste 			op = oplist[i].fp_base_op << 6;
57602c23cb7cSEd Maste 		else
57612c23cb7cSEd Maste 			op = oplist[i].fp_extended_op;
57622c23cb7cSEd Maste 		if (dwarf_get_CFA_name(op, &op_str) != DW_DLV_OK) {
5763cf781b2eSEd Maste 			snprintf(unk_op, sizeof(unk_op), "[Unknown CFA: %#x]",
5764cf781b2eSEd Maste 			    op);
5765cf781b2eSEd Maste 			op_str = unk_op;
57662c23cb7cSEd Maste 		}
57672c23cb7cSEd Maste 		printf("  %s", op_str);
57682c23cb7cSEd Maste 		switch (op) {
57692c23cb7cSEd Maste 		case DW_CFA_advance_loc:
57702c23cb7cSEd Maste 			delta = oplist[i].fp_offset * caf;
57712c23cb7cSEd Maste 			pc += delta;
57722c23cb7cSEd Maste 			printf(": %ju to %08jx", (uintmax_t) delta,
57732c23cb7cSEd Maste 			    (uintmax_t) pc);
57742c23cb7cSEd Maste 			break;
57752c23cb7cSEd Maste 		case DW_CFA_offset:
57762c23cb7cSEd Maste 		case DW_CFA_offset_extended:
57772c23cb7cSEd Maste 		case DW_CFA_offset_extended_sf:
57782c23cb7cSEd Maste 			delta = oplist[i].fp_offset * daf;
5779cf781b2eSEd Maste 			printf(": r%u (%s) at cfa%+jd", oplist[i].fp_register,
5780cf781b2eSEd Maste 			    dwarf_regname(re, oplist[i].fp_register),
57812c23cb7cSEd Maste 			    (intmax_t) delta);
57822c23cb7cSEd Maste 			break;
57832c23cb7cSEd Maste 		case DW_CFA_restore:
5784cf781b2eSEd Maste 			printf(": r%u (%s)", oplist[i].fp_register,
5785cf781b2eSEd Maste 			    dwarf_regname(re, oplist[i].fp_register));
57862c23cb7cSEd Maste 			break;
57872c23cb7cSEd Maste 		case DW_CFA_set_loc:
57882c23cb7cSEd Maste 			pc = oplist[i].fp_offset;
57892c23cb7cSEd Maste 			printf(": to %08jx", (uintmax_t) pc);
57902c23cb7cSEd Maste 			break;
57912c23cb7cSEd Maste 		case DW_CFA_advance_loc1:
57922c23cb7cSEd Maste 		case DW_CFA_advance_loc2:
57932c23cb7cSEd Maste 		case DW_CFA_advance_loc4:
57942c23cb7cSEd Maste 			pc += oplist[i].fp_offset;
57952c23cb7cSEd Maste 			printf(": %jd to %08jx", (intmax_t) oplist[i].fp_offset,
57962c23cb7cSEd Maste 			    (uintmax_t) pc);
57972c23cb7cSEd Maste 			break;
57982c23cb7cSEd Maste 		case DW_CFA_def_cfa:
5799cf781b2eSEd Maste 			printf(": r%u (%s) ofs %ju", oplist[i].fp_register,
5800cf781b2eSEd Maste 			    dwarf_regname(re, oplist[i].fp_register),
58012c23cb7cSEd Maste 			    (uintmax_t) oplist[i].fp_offset);
58022c23cb7cSEd Maste 			break;
58032c23cb7cSEd Maste 		case DW_CFA_def_cfa_sf:
5804cf781b2eSEd Maste 			printf(": r%u (%s) ofs %jd", oplist[i].fp_register,
5805cf781b2eSEd Maste 			    dwarf_regname(re, oplist[i].fp_register),
58062c23cb7cSEd Maste 			    (intmax_t) (oplist[i].fp_offset * daf));
58072c23cb7cSEd Maste 			break;
58082c23cb7cSEd Maste 		case DW_CFA_def_cfa_register:
5809cf781b2eSEd Maste 			printf(": r%u (%s)", oplist[i].fp_register,
5810cf781b2eSEd Maste 			    dwarf_regname(re, oplist[i].fp_register));
58112c23cb7cSEd Maste 			break;
58122c23cb7cSEd Maste 		case DW_CFA_def_cfa_offset:
58132c23cb7cSEd Maste 			printf(": %ju", (uintmax_t) oplist[i].fp_offset);
58142c23cb7cSEd Maste 			break;
58152c23cb7cSEd Maste 		case DW_CFA_def_cfa_offset_sf:
58162c23cb7cSEd Maste 			printf(": %jd", (intmax_t) (oplist[i].fp_offset * daf));
58172c23cb7cSEd Maste 			break;
58182c23cb7cSEd Maste 		default:
58192c23cb7cSEd Maste 			break;
58202c23cb7cSEd Maste 		}
58212c23cb7cSEd Maste 		putchar('\n');
58222c23cb7cSEd Maste 	}
58232c23cb7cSEd Maste 
58242c23cb7cSEd Maste 	dwarf_dealloc(dbg, oplist, DW_DLA_FRAME_BLOCK);
58252c23cb7cSEd Maste }
58262c23cb7cSEd Maste 
58272c23cb7cSEd Maste static char *
5828cf781b2eSEd Maste get_regoff_str(struct readelf *re, Dwarf_Half reg, Dwarf_Addr off)
58292c23cb7cSEd Maste {
58302c23cb7cSEd Maste 	static char rs[16];
58312c23cb7cSEd Maste 
58322c23cb7cSEd Maste 	if (reg == DW_FRAME_UNDEFINED_VAL || reg == DW_FRAME_REG_INITIAL_VALUE)
58332c23cb7cSEd Maste 		snprintf(rs, sizeof(rs), "%c", 'u');
58342c23cb7cSEd Maste 	else if (reg == DW_FRAME_CFA_COL)
58352c23cb7cSEd Maste 		snprintf(rs, sizeof(rs), "c%+jd", (intmax_t) off);
58362c23cb7cSEd Maste 	else
5837cf781b2eSEd Maste 		snprintf(rs, sizeof(rs), "%s%+jd", dwarf_regname(re, reg),
5838cf781b2eSEd Maste 		    (intmax_t) off);
58392c23cb7cSEd Maste 
58402c23cb7cSEd Maste 	return (rs);
58412c23cb7cSEd Maste }
58422c23cb7cSEd Maste 
58432c23cb7cSEd Maste static int
5844cf781b2eSEd Maste dump_dwarf_frame_regtable(struct readelf *re, Dwarf_Fde fde, Dwarf_Addr pc,
5845cf781b2eSEd Maste     Dwarf_Unsigned func_len, Dwarf_Half cie_ra)
58462c23cb7cSEd Maste {
58472c23cb7cSEd Maste 	Dwarf_Regtable rt;
58482c23cb7cSEd Maste 	Dwarf_Addr row_pc, end_pc, pre_pc, cur_pc;
58492c23cb7cSEd Maste 	Dwarf_Error de;
58502c23cb7cSEd Maste 	char *vec;
58512c23cb7cSEd Maste 	int i;
58522c23cb7cSEd Maste 
58532c23cb7cSEd Maste #define BIT_SET(v, n) (v[(n)>>3] |= 1U << ((n) & 7))
58542c23cb7cSEd Maste #define BIT_CLR(v, n) (v[(n)>>3] &= ~(1U << ((n) & 7)))
58552c23cb7cSEd Maste #define BIT_ISSET(v, n) (v[(n)>>3] & (1U << ((n) & 7)))
58562c23cb7cSEd Maste #define	RT(x) rt.rules[(x)]
58572c23cb7cSEd Maste 
58582c23cb7cSEd Maste 	vec = calloc((DW_REG_TABLE_SIZE + 7) / 8, 1);
58592c23cb7cSEd Maste 	if (vec == NULL)
58602c23cb7cSEd Maste 		err(EXIT_FAILURE, "calloc failed");
58612c23cb7cSEd Maste 
58622c23cb7cSEd Maste 	pre_pc = ~((Dwarf_Addr) 0);
58632c23cb7cSEd Maste 	cur_pc = pc;
58642c23cb7cSEd Maste 	end_pc = pc + func_len;
58652c23cb7cSEd Maste 	for (; cur_pc < end_pc; cur_pc++) {
58662c23cb7cSEd Maste 		if (dwarf_get_fde_info_for_all_regs(fde, cur_pc, &rt, &row_pc,
58672c23cb7cSEd Maste 		    &de) != DW_DLV_OK) {
58682c23cb7cSEd Maste 			warnx("dwarf_get_fde_info_for_all_regs failed: %s\n",
58692c23cb7cSEd Maste 			    dwarf_errmsg(de));
58702c23cb7cSEd Maste 			return (-1);
58712c23cb7cSEd Maste 		}
58722c23cb7cSEd Maste 		if (row_pc == pre_pc)
58732c23cb7cSEd Maste 			continue;
58742c23cb7cSEd Maste 		pre_pc = row_pc;
58752c23cb7cSEd Maste 		for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
58762c23cb7cSEd Maste 			if (rt.rules[i].dw_regnum != DW_FRAME_REG_INITIAL_VALUE)
58772c23cb7cSEd Maste 				BIT_SET(vec, i);
58782c23cb7cSEd Maste 		}
58792c23cb7cSEd Maste 	}
58802c23cb7cSEd Maste 
58812c23cb7cSEd Maste 	printf("   LOC   CFA      ");
58822c23cb7cSEd Maste 	for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
58832c23cb7cSEd Maste 		if (BIT_ISSET(vec, i)) {
58842c23cb7cSEd Maste 			if ((Dwarf_Half) i == cie_ra)
58852c23cb7cSEd Maste 				printf("ra   ");
5886cf781b2eSEd Maste 			else
5887cf781b2eSEd Maste 				printf("%-5s",
5888cf781b2eSEd Maste 				    dwarf_regname(re, (unsigned int) i));
58892c23cb7cSEd Maste 		}
58902c23cb7cSEd Maste 	}
58912c23cb7cSEd Maste 	putchar('\n');
58922c23cb7cSEd Maste 
58932c23cb7cSEd Maste 	pre_pc = ~((Dwarf_Addr) 0);
58942c23cb7cSEd Maste 	cur_pc = pc;
58952c23cb7cSEd Maste 	end_pc = pc + func_len;
58962c23cb7cSEd Maste 	for (; cur_pc < end_pc; cur_pc++) {
58972c23cb7cSEd Maste 		if (dwarf_get_fde_info_for_all_regs(fde, cur_pc, &rt, &row_pc,
58982c23cb7cSEd Maste 		    &de) != DW_DLV_OK) {
58992c23cb7cSEd Maste 			warnx("dwarf_get_fde_info_for_all_regs failed: %s\n",
59002c23cb7cSEd Maste 			    dwarf_errmsg(de));
59012c23cb7cSEd Maste 			return (-1);
59022c23cb7cSEd Maste 		}
59032c23cb7cSEd Maste 		if (row_pc == pre_pc)
59042c23cb7cSEd Maste 			continue;
59052c23cb7cSEd Maste 		pre_pc = row_pc;
59062c23cb7cSEd Maste 		printf("%08jx ", (uintmax_t) row_pc);
5907cf781b2eSEd Maste 		printf("%-8s ", get_regoff_str(re, RT(0).dw_regnum,
59082c23cb7cSEd Maste 		    RT(0).dw_offset));
59092c23cb7cSEd Maste 		for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
59102c23cb7cSEd Maste 			if (BIT_ISSET(vec, i)) {
5911cf781b2eSEd Maste 				printf("%-5s", get_regoff_str(re,
5912cf781b2eSEd Maste 				    RT(i).dw_regnum, RT(i).dw_offset));
59132c23cb7cSEd Maste 			}
59142c23cb7cSEd Maste 		}
59152c23cb7cSEd Maste 		putchar('\n');
59162c23cb7cSEd Maste 	}
59172c23cb7cSEd Maste 
59182c23cb7cSEd Maste 	free(vec);
59192c23cb7cSEd Maste 
59202c23cb7cSEd Maste 	return (0);
59212c23cb7cSEd Maste 
59222c23cb7cSEd Maste #undef	BIT_SET
59232c23cb7cSEd Maste #undef	BIT_CLR
59242c23cb7cSEd Maste #undef	BIT_ISSET
59252c23cb7cSEd Maste #undef	RT
59262c23cb7cSEd Maste }
59272c23cb7cSEd Maste 
59282c23cb7cSEd Maste static void
59292c23cb7cSEd Maste dump_dwarf_frame_section(struct readelf *re, struct section *s, int alt)
59302c23cb7cSEd Maste {
59312c23cb7cSEd Maste 	Dwarf_Cie *cie_list, cie, pre_cie;
59322c23cb7cSEd Maste 	Dwarf_Fde *fde_list, fde;
59332c23cb7cSEd Maste 	Dwarf_Off cie_offset, fde_offset;
59342c23cb7cSEd Maste 	Dwarf_Unsigned cie_length, fde_instlen;
59352c23cb7cSEd Maste 	Dwarf_Unsigned cie_caf, cie_daf, cie_instlen, func_len, fde_length;
59362c23cb7cSEd Maste 	Dwarf_Signed cie_count, fde_count, cie_index;
59372c23cb7cSEd Maste 	Dwarf_Addr low_pc;
59382c23cb7cSEd Maste 	Dwarf_Half cie_ra;
59392c23cb7cSEd Maste 	Dwarf_Small cie_version;
59402c23cb7cSEd Maste 	Dwarf_Ptr fde_addr, fde_inst, cie_inst;
59412c23cb7cSEd Maste 	char *cie_aug, c;
59422c23cb7cSEd Maste 	int i, eh_frame;
59432c23cb7cSEd Maste 	Dwarf_Error de;
59442c23cb7cSEd Maste 
59452c23cb7cSEd Maste 	printf("\nThe section %s contains:\n\n", s->name);
59462c23cb7cSEd Maste 
59472c23cb7cSEd Maste 	if (!strcmp(s->name, ".debug_frame")) {
59482c23cb7cSEd Maste 		eh_frame = 0;
59492c23cb7cSEd Maste 		if (dwarf_get_fde_list(re->dbg, &cie_list, &cie_count,
59502c23cb7cSEd Maste 		    &fde_list, &fde_count, &de) != DW_DLV_OK) {
59512c23cb7cSEd Maste 			warnx("dwarf_get_fde_list failed: %s",
59522c23cb7cSEd Maste 			    dwarf_errmsg(de));
59532c23cb7cSEd Maste 			return;
59542c23cb7cSEd Maste 		}
59552c23cb7cSEd Maste 	} else if (!strcmp(s->name, ".eh_frame")) {
59562c23cb7cSEd Maste 		eh_frame = 1;
59572c23cb7cSEd Maste 		if (dwarf_get_fde_list_eh(re->dbg, &cie_list, &cie_count,
59582c23cb7cSEd Maste 		    &fde_list, &fde_count, &de) != DW_DLV_OK) {
59592c23cb7cSEd Maste 			warnx("dwarf_get_fde_list_eh failed: %s",
59602c23cb7cSEd Maste 			    dwarf_errmsg(de));
59612c23cb7cSEd Maste 			return;
59622c23cb7cSEd Maste 		}
59632c23cb7cSEd Maste 	} else
59642c23cb7cSEd Maste 		return;
59652c23cb7cSEd Maste 
59662c23cb7cSEd Maste 	pre_cie = NULL;
59672c23cb7cSEd Maste 	for (i = 0; i < fde_count; i++) {
59682c23cb7cSEd Maste 		if (dwarf_get_fde_n(fde_list, i, &fde, &de) != DW_DLV_OK) {
59692c23cb7cSEd Maste 			warnx("dwarf_get_fde_n failed: %s", dwarf_errmsg(de));
59702c23cb7cSEd Maste 			continue;
59712c23cb7cSEd Maste 		}
59722c23cb7cSEd Maste 		if (dwarf_get_cie_of_fde(fde, &cie, &de) != DW_DLV_OK) {
59732c23cb7cSEd Maste 			warnx("dwarf_get_fde_n failed: %s", dwarf_errmsg(de));
59742c23cb7cSEd Maste 			continue;
59752c23cb7cSEd Maste 		}
59762c23cb7cSEd Maste 		if (dwarf_get_fde_range(fde, &low_pc, &func_len, &fde_addr,
59772c23cb7cSEd Maste 		    &fde_length, &cie_offset, &cie_index, &fde_offset,
59782c23cb7cSEd Maste 		    &de) != DW_DLV_OK) {
59792c23cb7cSEd Maste 			warnx("dwarf_get_fde_range failed: %s",
59802c23cb7cSEd Maste 			    dwarf_errmsg(de));
59812c23cb7cSEd Maste 			continue;
59822c23cb7cSEd Maste 		}
59832c23cb7cSEd Maste 		if (dwarf_get_fde_instr_bytes(fde, &fde_inst, &fde_instlen,
59842c23cb7cSEd Maste 		    &de) != DW_DLV_OK) {
59852c23cb7cSEd Maste 			warnx("dwarf_get_fde_instr_bytes failed: %s",
59862c23cb7cSEd Maste 			    dwarf_errmsg(de));
59872c23cb7cSEd Maste 			continue;
59882c23cb7cSEd Maste 		}
59892c23cb7cSEd Maste 		if (pre_cie == NULL || cie != pre_cie) {
59902c23cb7cSEd Maste 			pre_cie = cie;
59912c23cb7cSEd Maste 			if (dwarf_get_cie_info(cie, &cie_length, &cie_version,
59922c23cb7cSEd Maste 			    &cie_aug, &cie_caf, &cie_daf, &cie_ra,
59932c23cb7cSEd Maste 			    &cie_inst, &cie_instlen, &de) != DW_DLV_OK) {
59942c23cb7cSEd Maste 				warnx("dwarf_get_cie_info failed: %s",
59952c23cb7cSEd Maste 				    dwarf_errmsg(de));
59962c23cb7cSEd Maste 				continue;
59972c23cb7cSEd Maste 			}
59982c23cb7cSEd Maste 			printf("%08jx %08jx %8.8jx CIE",
59992c23cb7cSEd Maste 			    (uintmax_t) cie_offset,
60002c23cb7cSEd Maste 			    (uintmax_t) cie_length,
60012c23cb7cSEd Maste 			    (uintmax_t) (eh_frame ? 0 : ~0U));
60022c23cb7cSEd Maste 			if (!alt) {
60032c23cb7cSEd Maste 				putchar('\n');
60042c23cb7cSEd Maste 				printf("  Version:\t\t\t%u\n", cie_version);
60052c23cb7cSEd Maste 				printf("  Augmentation:\t\t\t\"");
60062c23cb7cSEd Maste 				while ((c = *cie_aug++) != '\0')
60072c23cb7cSEd Maste 					putchar(c);
60082c23cb7cSEd Maste 				printf("\"\n");
60092c23cb7cSEd Maste 				printf("  Code alignment factor:\t%ju\n",
60102c23cb7cSEd Maste 				    (uintmax_t) cie_caf);
60112c23cb7cSEd Maste 				printf("  Data alignment factor:\t%jd\n",
60122c23cb7cSEd Maste 				    (intmax_t) cie_daf);
60132c23cb7cSEd Maste 				printf("  Return address column:\t%ju\n",
60142c23cb7cSEd Maste 				    (uintmax_t) cie_ra);
60152c23cb7cSEd Maste 				putchar('\n');
6016cf781b2eSEd Maste 				dump_dwarf_frame_inst(re, cie, cie_inst,
60172c23cb7cSEd Maste 				    cie_instlen, cie_caf, cie_daf, 0,
60182c23cb7cSEd Maste 				    re->dbg);
60192c23cb7cSEd Maste 				putchar('\n');
60202c23cb7cSEd Maste 			} else {
60212c23cb7cSEd Maste 				printf(" \"");
60222c23cb7cSEd Maste 				while ((c = *cie_aug++) != '\0')
60232c23cb7cSEd Maste 					putchar(c);
60242c23cb7cSEd Maste 				putchar('"');
60252c23cb7cSEd Maste 				printf(" cf=%ju df=%jd ra=%ju\n",
60262c23cb7cSEd Maste 				    (uintmax_t) cie_caf,
60272c23cb7cSEd Maste 				    (uintmax_t) cie_daf,
60282c23cb7cSEd Maste 				    (uintmax_t) cie_ra);
6029cf781b2eSEd Maste 				dump_dwarf_frame_regtable(re, fde, low_pc, 1,
60302c23cb7cSEd Maste 				    cie_ra);
60312c23cb7cSEd Maste 				putchar('\n');
60322c23cb7cSEd Maste 			}
60332c23cb7cSEd Maste 		}
60342c23cb7cSEd Maste 		printf("%08jx %08jx %08jx FDE cie=%08jx pc=%08jx..%08jx\n",
60352c23cb7cSEd Maste 		    (uintmax_t) fde_offset, (uintmax_t) fde_length,
60362c23cb7cSEd Maste 		    (uintmax_t) cie_offset,
60372c23cb7cSEd Maste 		    (uintmax_t) (eh_frame ? fde_offset + 4 - cie_offset :
60382c23cb7cSEd Maste 			cie_offset),
60392c23cb7cSEd Maste 		    (uintmax_t) low_pc, (uintmax_t) (low_pc + func_len));
60402c23cb7cSEd Maste 		if (!alt)
6041cf781b2eSEd Maste 			dump_dwarf_frame_inst(re, cie, fde_inst, fde_instlen,
60422c23cb7cSEd Maste 			    cie_caf, cie_daf, low_pc, re->dbg);
60432c23cb7cSEd Maste 		else
6044cf781b2eSEd Maste 			dump_dwarf_frame_regtable(re, fde, low_pc, func_len,
60452c23cb7cSEd Maste 			    cie_ra);
60462c23cb7cSEd Maste 		putchar('\n');
60472c23cb7cSEd Maste 	}
60482c23cb7cSEd Maste }
60492c23cb7cSEd Maste 
60502c23cb7cSEd Maste static void
60512c23cb7cSEd Maste dump_dwarf_frame(struct readelf *re, int alt)
60522c23cb7cSEd Maste {
60532c23cb7cSEd Maste 	struct section *s;
60542c23cb7cSEd Maste 	int i;
60552c23cb7cSEd Maste 
60562c23cb7cSEd Maste 	(void) dwarf_set_frame_cfa_value(re->dbg, DW_FRAME_CFA_COL);
60572c23cb7cSEd Maste 
60582c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
60592c23cb7cSEd Maste 		s = &re->sl[i];
60602c23cb7cSEd Maste 		if (s->name != NULL && (!strcmp(s->name, ".debug_frame") ||
60612c23cb7cSEd Maste 		    !strcmp(s->name, ".eh_frame")))
60622c23cb7cSEd Maste 			dump_dwarf_frame_section(re, s, alt);
60632c23cb7cSEd Maste 	}
60642c23cb7cSEd Maste }
60652c23cb7cSEd Maste 
60662c23cb7cSEd Maste static void
60672c23cb7cSEd Maste dump_dwarf_str(struct readelf *re)
60682c23cb7cSEd Maste {
60692c23cb7cSEd Maste 	struct section *s;
60702c23cb7cSEd Maste 	Elf_Data *d;
60712c23cb7cSEd Maste 	unsigned char *p;
60722c23cb7cSEd Maste 	int elferr, end, i, j;
60732c23cb7cSEd Maste 
60742c23cb7cSEd Maste 	printf("\nContents of section .debug_str:\n");
60752c23cb7cSEd Maste 
60762c23cb7cSEd Maste 	s = NULL;
60772c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
60782c23cb7cSEd Maste 		s = &re->sl[i];
60792c23cb7cSEd Maste 		if (s->name != NULL && !strcmp(s->name, ".debug_str"))
60802c23cb7cSEd Maste 			break;
60812c23cb7cSEd Maste 	}
60822c23cb7cSEd Maste 	if ((size_t) i >= re->shnum)
60832c23cb7cSEd Maste 		return;
60842c23cb7cSEd Maste 
60852c23cb7cSEd Maste 	(void) elf_errno();
60862c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
60872c23cb7cSEd Maste 		elferr = elf_errno();
60882c23cb7cSEd Maste 		if (elferr != 0)
60892c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
60902c23cb7cSEd Maste 		return;
60912c23cb7cSEd Maste 	}
60922c23cb7cSEd Maste 	if (d->d_size <= 0)
60932c23cb7cSEd Maste 		return;
60942c23cb7cSEd Maste 
60952c23cb7cSEd Maste 	for (i = 0, p = d->d_buf; (size_t) i < d->d_size; i += 16) {
60962c23cb7cSEd Maste 		printf("  0x%08x", (unsigned int) i);
60972c23cb7cSEd Maste 		if ((size_t) i + 16 > d->d_size)
60982c23cb7cSEd Maste 			end = d->d_size;
60992c23cb7cSEd Maste 		else
61002c23cb7cSEd Maste 			end = i + 16;
61012c23cb7cSEd Maste 		for (j = i; j < i + 16; j++) {
61022c23cb7cSEd Maste 			if ((j - i) % 4 == 0)
61032c23cb7cSEd Maste 				putchar(' ');
61042c23cb7cSEd Maste 			if (j >= end) {
61052c23cb7cSEd Maste 				printf("  ");
61062c23cb7cSEd Maste 				continue;
61072c23cb7cSEd Maste 			}
61082c23cb7cSEd Maste 			printf("%02x", (uint8_t) p[j]);
61092c23cb7cSEd Maste 		}
61102c23cb7cSEd Maste 		putchar(' ');
61112c23cb7cSEd Maste 		for (j = i; j < end; j++) {
61122c23cb7cSEd Maste 			if (isprint(p[j]))
61132c23cb7cSEd Maste 				putchar(p[j]);
61142c23cb7cSEd Maste 			else if (p[j] == 0)
61152c23cb7cSEd Maste 				putchar('.');
61162c23cb7cSEd Maste 			else
61172c23cb7cSEd Maste 				putchar(' ');
61182c23cb7cSEd Maste 		}
61192c23cb7cSEd Maste 		putchar('\n');
61202c23cb7cSEd Maste 	}
61212c23cb7cSEd Maste }
61222c23cb7cSEd Maste 
61232c23cb7cSEd Maste struct loc_at {
61242c23cb7cSEd Maste 	Dwarf_Attribute la_at;
61252c23cb7cSEd Maste 	Dwarf_Unsigned la_off;
61262c23cb7cSEd Maste 	Dwarf_Unsigned la_lowpc;
6127cf781b2eSEd Maste 	Dwarf_Half la_cu_psize;
6128cf781b2eSEd Maste 	Dwarf_Half la_cu_osize;
6129cf781b2eSEd Maste 	Dwarf_Half la_cu_ver;
61302c23cb7cSEd Maste 	TAILQ_ENTRY(loc_at) la_next;
61312c23cb7cSEd Maste };
61322c23cb7cSEd Maste 
61332c23cb7cSEd Maste static TAILQ_HEAD(, loc_at) lalist = TAILQ_HEAD_INITIALIZER(lalist);
61342c23cb7cSEd Maste 
61352c23cb7cSEd Maste static void
61362c23cb7cSEd Maste search_loclist_at(struct readelf *re, Dwarf_Die die, Dwarf_Unsigned lowpc)
61372c23cb7cSEd Maste {
61382c23cb7cSEd Maste 	Dwarf_Attribute *attr_list;
61392c23cb7cSEd Maste 	Dwarf_Die ret_die;
61402c23cb7cSEd Maste 	Dwarf_Unsigned off;
6141cf781b2eSEd Maste 	Dwarf_Off ref;
61422c23cb7cSEd Maste 	Dwarf_Signed attr_count;
61432c23cb7cSEd Maste 	Dwarf_Half attr, form;
6144cf781b2eSEd Maste 	Dwarf_Bool is_info;
61452c23cb7cSEd Maste 	Dwarf_Error de;
61462c23cb7cSEd Maste 	struct loc_at *la, *nla;
61472c23cb7cSEd Maste 	int i, ret;
61482c23cb7cSEd Maste 
6149cf781b2eSEd Maste 	is_info = dwarf_get_die_infotypes_flag(die);
6150cf781b2eSEd Maste 
61512c23cb7cSEd Maste 	if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
61522c23cb7cSEd Maste 	    DW_DLV_OK) {
61532c23cb7cSEd Maste 		if (ret == DW_DLV_ERROR)
61542c23cb7cSEd Maste 			warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
61552c23cb7cSEd Maste 		goto cont_search;
61562c23cb7cSEd Maste 	}
61572c23cb7cSEd Maste 	for (i = 0; i < attr_count; i++) {
61582c23cb7cSEd Maste 		if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
61592c23cb7cSEd Maste 			warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
61602c23cb7cSEd Maste 			continue;
61612c23cb7cSEd Maste 		}
61622c23cb7cSEd Maste 		if (attr != DW_AT_location &&
61632c23cb7cSEd Maste 		    attr != DW_AT_string_length &&
61642c23cb7cSEd Maste 		    attr != DW_AT_return_addr &&
61652c23cb7cSEd Maste 		    attr != DW_AT_data_member_location &&
61662c23cb7cSEd Maste 		    attr != DW_AT_frame_base &&
61672c23cb7cSEd Maste 		    attr != DW_AT_segment &&
61682c23cb7cSEd Maste 		    attr != DW_AT_static_link &&
61692c23cb7cSEd Maste 		    attr != DW_AT_use_location &&
61702c23cb7cSEd Maste 		    attr != DW_AT_vtable_elem_location)
61712c23cb7cSEd Maste 			continue;
61722c23cb7cSEd Maste 		if (dwarf_whatform(attr_list[i], &form, &de) != DW_DLV_OK) {
61732c23cb7cSEd Maste 			warnx("dwarf_whatform failed: %s", dwarf_errmsg(de));
61742c23cb7cSEd Maste 			continue;
61752c23cb7cSEd Maste 		}
6176cf781b2eSEd Maste 		if (form == DW_FORM_data4 || form == DW_FORM_data8) {
6177cf781b2eSEd Maste 			if (dwarf_formudata(attr_list[i], &off, &de) !=
6178cf781b2eSEd Maste 			    DW_DLV_OK) {
6179cf781b2eSEd Maste 				warnx("dwarf_formudata failed: %s",
6180cf781b2eSEd Maste 				    dwarf_errmsg(de));
61812c23cb7cSEd Maste 				continue;
61822c23cb7cSEd Maste 			}
6183cf781b2eSEd Maste 		} else if (form == DW_FORM_sec_offset) {
6184cf781b2eSEd Maste 			if (dwarf_global_formref(attr_list[i], &ref, &de) !=
6185cf781b2eSEd Maste 			    DW_DLV_OK) {
6186cf781b2eSEd Maste 				warnx("dwarf_global_formref failed: %s",
6187cf781b2eSEd Maste 				    dwarf_errmsg(de));
6188cf781b2eSEd Maste 				continue;
6189cf781b2eSEd Maste 			}
6190cf781b2eSEd Maste 			off = ref;
6191cf781b2eSEd Maste 		} else
6192cf781b2eSEd Maste 			continue;
6193cf781b2eSEd Maste 
61942c23cb7cSEd Maste 		TAILQ_FOREACH(la, &lalist, la_next) {
61952c23cb7cSEd Maste 			if (off == la->la_off)
61962c23cb7cSEd Maste 				break;
61972c23cb7cSEd Maste 			if (off < la->la_off) {
61982c23cb7cSEd Maste 				if ((nla = malloc(sizeof(*nla))) == NULL)
61992c23cb7cSEd Maste 					err(EXIT_FAILURE, "malloc failed");
62002c23cb7cSEd Maste 				nla->la_at = attr_list[i];
62012c23cb7cSEd Maste 				nla->la_off = off;
62022c23cb7cSEd Maste 				nla->la_lowpc = lowpc;
6203cf781b2eSEd Maste 				nla->la_cu_psize = re->cu_psize;
6204cf781b2eSEd Maste 				nla->la_cu_osize = re->cu_osize;
6205cf781b2eSEd Maste 				nla->la_cu_ver = re->cu_ver;
62062c23cb7cSEd Maste 				TAILQ_INSERT_BEFORE(la, nla, la_next);
62072c23cb7cSEd Maste 				break;
62082c23cb7cSEd Maste 			}
62092c23cb7cSEd Maste 		}
62102c23cb7cSEd Maste 		if (la == NULL) {
62112c23cb7cSEd Maste 			if ((nla = malloc(sizeof(*nla))) == NULL)
62122c23cb7cSEd Maste 				err(EXIT_FAILURE, "malloc failed");
62132c23cb7cSEd Maste 			nla->la_at = attr_list[i];
62142c23cb7cSEd Maste 			nla->la_off = off;
62152c23cb7cSEd Maste 			nla->la_lowpc = lowpc;
6216cf781b2eSEd Maste 			nla->la_cu_psize = re->cu_psize;
6217cf781b2eSEd Maste 			nla->la_cu_osize = re->cu_osize;
6218cf781b2eSEd Maste 			nla->la_cu_ver = re->cu_ver;
62192c23cb7cSEd Maste 			TAILQ_INSERT_TAIL(&lalist, nla, la_next);
62202c23cb7cSEd Maste 		}
62212c23cb7cSEd Maste 	}
62222c23cb7cSEd Maste 
62232c23cb7cSEd Maste cont_search:
62242c23cb7cSEd Maste 	/* Search children. */
62252c23cb7cSEd Maste 	ret = dwarf_child(die, &ret_die, &de);
62262c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
62272c23cb7cSEd Maste 		warnx("dwarf_child: %s", dwarf_errmsg(de));
62282c23cb7cSEd Maste 	else if (ret == DW_DLV_OK)
62292c23cb7cSEd Maste 		search_loclist_at(re, ret_die, lowpc);
62302c23cb7cSEd Maste 
62312c23cb7cSEd Maste 	/* Search sibling. */
6232cf781b2eSEd Maste 	ret = dwarf_siblingof_b(re->dbg, die, &ret_die, is_info, &de);
62332c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
62342c23cb7cSEd Maste 		warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
62352c23cb7cSEd Maste 	else if (ret == DW_DLV_OK)
62362c23cb7cSEd Maste 		search_loclist_at(re, ret_die, lowpc);
62372c23cb7cSEd Maste }
62382c23cb7cSEd Maste 
62392c23cb7cSEd Maste static void
6240cf781b2eSEd Maste dump_dwarf_loc(struct readelf *re, Dwarf_Loc *lr)
62412c23cb7cSEd Maste {
62422c23cb7cSEd Maste 	const char *op_str;
6243cf781b2eSEd Maste 	char unk_op[32];
6244cf781b2eSEd Maste 	uint8_t *b, n;
6245cf781b2eSEd Maste 	int i;
62462c23cb7cSEd Maste 
62472c23cb7cSEd Maste 	if (dwarf_get_OP_name(lr->lr_atom, &op_str) !=
62482c23cb7cSEd Maste 	    DW_DLV_OK) {
6249cf781b2eSEd Maste 		snprintf(unk_op, sizeof(unk_op),
6250cf781b2eSEd Maste 		    "[Unknown OP: %#x]", lr->lr_atom);
6251cf781b2eSEd Maste 		op_str = unk_op;
62522c23cb7cSEd Maste 	}
62532c23cb7cSEd Maste 
62542c23cb7cSEd Maste 	printf("%s", op_str);
62552c23cb7cSEd Maste 
62562c23cb7cSEd Maste 	switch (lr->lr_atom) {
62572c23cb7cSEd Maste 	case DW_OP_reg0:
62582c23cb7cSEd Maste 	case DW_OP_reg1:
62592c23cb7cSEd Maste 	case DW_OP_reg2:
62602c23cb7cSEd Maste 	case DW_OP_reg3:
62612c23cb7cSEd Maste 	case DW_OP_reg4:
62622c23cb7cSEd Maste 	case DW_OP_reg5:
62632c23cb7cSEd Maste 	case DW_OP_reg6:
62642c23cb7cSEd Maste 	case DW_OP_reg7:
62652c23cb7cSEd Maste 	case DW_OP_reg8:
62662c23cb7cSEd Maste 	case DW_OP_reg9:
62672c23cb7cSEd Maste 	case DW_OP_reg10:
62682c23cb7cSEd Maste 	case DW_OP_reg11:
62692c23cb7cSEd Maste 	case DW_OP_reg12:
62702c23cb7cSEd Maste 	case DW_OP_reg13:
62712c23cb7cSEd Maste 	case DW_OP_reg14:
62722c23cb7cSEd Maste 	case DW_OP_reg15:
62732c23cb7cSEd Maste 	case DW_OP_reg16:
62742c23cb7cSEd Maste 	case DW_OP_reg17:
62752c23cb7cSEd Maste 	case DW_OP_reg18:
62762c23cb7cSEd Maste 	case DW_OP_reg19:
62772c23cb7cSEd Maste 	case DW_OP_reg20:
62782c23cb7cSEd Maste 	case DW_OP_reg21:
62792c23cb7cSEd Maste 	case DW_OP_reg22:
62802c23cb7cSEd Maste 	case DW_OP_reg23:
62812c23cb7cSEd Maste 	case DW_OP_reg24:
62822c23cb7cSEd Maste 	case DW_OP_reg25:
62832c23cb7cSEd Maste 	case DW_OP_reg26:
62842c23cb7cSEd Maste 	case DW_OP_reg27:
62852c23cb7cSEd Maste 	case DW_OP_reg28:
62862c23cb7cSEd Maste 	case DW_OP_reg29:
62872c23cb7cSEd Maste 	case DW_OP_reg30:
62882c23cb7cSEd Maste 	case DW_OP_reg31:
6289cf781b2eSEd Maste 		printf(" (%s)", dwarf_regname(re, lr->lr_atom - DW_OP_reg0));
6290cf781b2eSEd Maste 		break;
6291cf781b2eSEd Maste 
6292cf781b2eSEd Maste 	case DW_OP_deref:
62932c23cb7cSEd Maste 	case DW_OP_lit0:
62942c23cb7cSEd Maste 	case DW_OP_lit1:
62952c23cb7cSEd Maste 	case DW_OP_lit2:
62962c23cb7cSEd Maste 	case DW_OP_lit3:
62972c23cb7cSEd Maste 	case DW_OP_lit4:
62982c23cb7cSEd Maste 	case DW_OP_lit5:
62992c23cb7cSEd Maste 	case DW_OP_lit6:
63002c23cb7cSEd Maste 	case DW_OP_lit7:
63012c23cb7cSEd Maste 	case DW_OP_lit8:
63022c23cb7cSEd Maste 	case DW_OP_lit9:
63032c23cb7cSEd Maste 	case DW_OP_lit10:
63042c23cb7cSEd Maste 	case DW_OP_lit11:
63052c23cb7cSEd Maste 	case DW_OP_lit12:
63062c23cb7cSEd Maste 	case DW_OP_lit13:
63072c23cb7cSEd Maste 	case DW_OP_lit14:
63082c23cb7cSEd Maste 	case DW_OP_lit15:
63092c23cb7cSEd Maste 	case DW_OP_lit16:
63102c23cb7cSEd Maste 	case DW_OP_lit17:
63112c23cb7cSEd Maste 	case DW_OP_lit18:
63122c23cb7cSEd Maste 	case DW_OP_lit19:
63132c23cb7cSEd Maste 	case DW_OP_lit20:
63142c23cb7cSEd Maste 	case DW_OP_lit21:
63152c23cb7cSEd Maste 	case DW_OP_lit22:
63162c23cb7cSEd Maste 	case DW_OP_lit23:
63172c23cb7cSEd Maste 	case DW_OP_lit24:
63182c23cb7cSEd Maste 	case DW_OP_lit25:
63192c23cb7cSEd Maste 	case DW_OP_lit26:
63202c23cb7cSEd Maste 	case DW_OP_lit27:
63212c23cb7cSEd Maste 	case DW_OP_lit28:
63222c23cb7cSEd Maste 	case DW_OP_lit29:
63232c23cb7cSEd Maste 	case DW_OP_lit30:
63242c23cb7cSEd Maste 	case DW_OP_lit31:
63252c23cb7cSEd Maste 	case DW_OP_dup:
63262c23cb7cSEd Maste 	case DW_OP_drop:
63272c23cb7cSEd Maste 	case DW_OP_over:
63282c23cb7cSEd Maste 	case DW_OP_swap:
63292c23cb7cSEd Maste 	case DW_OP_rot:
63302c23cb7cSEd Maste 	case DW_OP_xderef:
63312c23cb7cSEd Maste 	case DW_OP_abs:
63322c23cb7cSEd Maste 	case DW_OP_and:
63332c23cb7cSEd Maste 	case DW_OP_div:
63342c23cb7cSEd Maste 	case DW_OP_minus:
63352c23cb7cSEd Maste 	case DW_OP_mod:
63362c23cb7cSEd Maste 	case DW_OP_mul:
63372c23cb7cSEd Maste 	case DW_OP_neg:
63382c23cb7cSEd Maste 	case DW_OP_not:
63392c23cb7cSEd Maste 	case DW_OP_or:
63402c23cb7cSEd Maste 	case DW_OP_plus:
63412c23cb7cSEd Maste 	case DW_OP_shl:
63422c23cb7cSEd Maste 	case DW_OP_shr:
63432c23cb7cSEd Maste 	case DW_OP_shra:
63442c23cb7cSEd Maste 	case DW_OP_xor:
63452c23cb7cSEd Maste 	case DW_OP_eq:
63462c23cb7cSEd Maste 	case DW_OP_ge:
63472c23cb7cSEd Maste 	case DW_OP_gt:
63482c23cb7cSEd Maste 	case DW_OP_le:
63492c23cb7cSEd Maste 	case DW_OP_lt:
63502c23cb7cSEd Maste 	case DW_OP_ne:
63512c23cb7cSEd Maste 	case DW_OP_nop:
6352cf781b2eSEd Maste 	case DW_OP_push_object_address:
6353cf781b2eSEd Maste 	case DW_OP_form_tls_address:
6354cf781b2eSEd Maste 	case DW_OP_call_frame_cfa:
6355cf781b2eSEd Maste 	case DW_OP_stack_value:
6356cf781b2eSEd Maste 	case DW_OP_GNU_push_tls_address:
6357cf781b2eSEd Maste 	case DW_OP_GNU_uninit:
63582c23cb7cSEd Maste 		break;
63592c23cb7cSEd Maste 
63602c23cb7cSEd Maste 	case DW_OP_const1u:
63612c23cb7cSEd Maste 	case DW_OP_pick:
63622c23cb7cSEd Maste 	case DW_OP_deref_size:
63632c23cb7cSEd Maste 	case DW_OP_xderef_size:
63642c23cb7cSEd Maste 	case DW_OP_const2u:
63652c23cb7cSEd Maste 	case DW_OP_bra:
63662c23cb7cSEd Maste 	case DW_OP_skip:
63672c23cb7cSEd Maste 	case DW_OP_const4u:
63682c23cb7cSEd Maste 	case DW_OP_const8u:
63692c23cb7cSEd Maste 	case DW_OP_constu:
63702c23cb7cSEd Maste 	case DW_OP_plus_uconst:
63712c23cb7cSEd Maste 	case DW_OP_regx:
63722c23cb7cSEd Maste 	case DW_OP_piece:
63732c23cb7cSEd Maste 		printf(": %ju", (uintmax_t)
63742c23cb7cSEd Maste 		    lr->lr_number);
63752c23cb7cSEd Maste 		break;
63762c23cb7cSEd Maste 
6377cf781b2eSEd Maste 	case DW_OP_const1s:
6378cf781b2eSEd Maste 	case DW_OP_const2s:
6379cf781b2eSEd Maste 	case DW_OP_const4s:
6380cf781b2eSEd Maste 	case DW_OP_const8s:
63812c23cb7cSEd Maste 	case DW_OP_consts:
6382cf781b2eSEd Maste 		printf(": %jd", (intmax_t)
6383cf781b2eSEd Maste 		    lr->lr_number);
6384cf781b2eSEd Maste 		break;
6385cf781b2eSEd Maste 
63862c23cb7cSEd Maste 	case DW_OP_breg0:
63872c23cb7cSEd Maste 	case DW_OP_breg1:
63882c23cb7cSEd Maste 	case DW_OP_breg2:
63892c23cb7cSEd Maste 	case DW_OP_breg3:
63902c23cb7cSEd Maste 	case DW_OP_breg4:
63912c23cb7cSEd Maste 	case DW_OP_breg5:
63922c23cb7cSEd Maste 	case DW_OP_breg6:
63932c23cb7cSEd Maste 	case DW_OP_breg7:
63942c23cb7cSEd Maste 	case DW_OP_breg8:
63952c23cb7cSEd Maste 	case DW_OP_breg9:
63962c23cb7cSEd Maste 	case DW_OP_breg10:
63972c23cb7cSEd Maste 	case DW_OP_breg11:
63982c23cb7cSEd Maste 	case DW_OP_breg12:
63992c23cb7cSEd Maste 	case DW_OP_breg13:
64002c23cb7cSEd Maste 	case DW_OP_breg14:
64012c23cb7cSEd Maste 	case DW_OP_breg15:
64022c23cb7cSEd Maste 	case DW_OP_breg16:
64032c23cb7cSEd Maste 	case DW_OP_breg17:
64042c23cb7cSEd Maste 	case DW_OP_breg18:
64052c23cb7cSEd Maste 	case DW_OP_breg19:
64062c23cb7cSEd Maste 	case DW_OP_breg20:
64072c23cb7cSEd Maste 	case DW_OP_breg21:
64082c23cb7cSEd Maste 	case DW_OP_breg22:
64092c23cb7cSEd Maste 	case DW_OP_breg23:
64102c23cb7cSEd Maste 	case DW_OP_breg24:
64112c23cb7cSEd Maste 	case DW_OP_breg25:
64122c23cb7cSEd Maste 	case DW_OP_breg26:
64132c23cb7cSEd Maste 	case DW_OP_breg27:
64142c23cb7cSEd Maste 	case DW_OP_breg28:
64152c23cb7cSEd Maste 	case DW_OP_breg29:
64162c23cb7cSEd Maste 	case DW_OP_breg30:
64172c23cb7cSEd Maste 	case DW_OP_breg31:
6418cf781b2eSEd Maste 		printf(" (%s): %jd",
6419cf781b2eSEd Maste 		    dwarf_regname(re, lr->lr_atom - DW_OP_breg0),
6420cf781b2eSEd Maste 		    (intmax_t) lr->lr_number);
6421cf781b2eSEd Maste 		break;
6422cf781b2eSEd Maste 
64232c23cb7cSEd Maste 	case DW_OP_fbreg:
64242c23cb7cSEd Maste 		printf(": %jd", (intmax_t)
64252c23cb7cSEd Maste 		    lr->lr_number);
64262c23cb7cSEd Maste 		break;
64272c23cb7cSEd Maste 
64282c23cb7cSEd Maste 	case DW_OP_bregx:
6429cf781b2eSEd Maste 		printf(": %ju (%s) %jd",
64302c23cb7cSEd Maste 		    (uintmax_t) lr->lr_number,
6431cf781b2eSEd Maste 		    dwarf_regname(re, (unsigned int) lr->lr_number),
64322c23cb7cSEd Maste 		    (intmax_t) lr->lr_number2);
64332c23cb7cSEd Maste 		break;
64342c23cb7cSEd Maste 
64352c23cb7cSEd Maste 	case DW_OP_addr:
6436cf781b2eSEd Maste 	case DW_OP_GNU_encoded_addr:
64372c23cb7cSEd Maste 		printf(": %#jx", (uintmax_t)
64382c23cb7cSEd Maste 		    lr->lr_number);
64392c23cb7cSEd Maste 		break;
6440cf781b2eSEd Maste 
6441cf781b2eSEd Maste 	case DW_OP_GNU_implicit_pointer:
6442cf781b2eSEd Maste 		printf(": <0x%jx> %jd", (uintmax_t) lr->lr_number,
6443cf781b2eSEd Maste 		    (intmax_t) lr->lr_number2);
6444cf781b2eSEd Maste 		break;
6445cf781b2eSEd Maste 
6446cf781b2eSEd Maste 	case DW_OP_implicit_value:
6447cf781b2eSEd Maste 		printf(": %ju byte block:", (uintmax_t) lr->lr_number);
6448cf781b2eSEd Maste 		b = (uint8_t *)(uintptr_t) lr->lr_number2;
6449cf781b2eSEd Maste 		for (i = 0; (Dwarf_Unsigned) i < lr->lr_number; i++)
6450cf781b2eSEd Maste 			printf(" %x", b[i]);
6451cf781b2eSEd Maste 		break;
6452cf781b2eSEd Maste 
6453cf781b2eSEd Maste 	case DW_OP_GNU_entry_value:
6454cf781b2eSEd Maste 		printf(": (");
6455cf781b2eSEd Maste 		dump_dwarf_block(re, (uint8_t *)(uintptr_t) lr->lr_number2,
6456cf781b2eSEd Maste 		    lr->lr_number);
6457cf781b2eSEd Maste 		putchar(')');
6458cf781b2eSEd Maste 		break;
6459cf781b2eSEd Maste 
6460cf781b2eSEd Maste 	case DW_OP_GNU_const_type:
6461cf781b2eSEd Maste 		printf(": <0x%jx> ", (uintmax_t) lr->lr_number);
6462cf781b2eSEd Maste 		b = (uint8_t *)(uintptr_t) lr->lr_number2;
6463cf781b2eSEd Maste 		n = *b;
6464cf781b2eSEd Maste 		for (i = 1; (uint8_t) i < n; i++)
6465cf781b2eSEd Maste 			printf(" %x", b[i]);
6466cf781b2eSEd Maste 		break;
6467cf781b2eSEd Maste 
6468cf781b2eSEd Maste 	case DW_OP_GNU_regval_type:
6469cf781b2eSEd Maste 		printf(": %ju (%s) <0x%jx>", (uintmax_t) lr->lr_number,
6470cf781b2eSEd Maste 		    dwarf_regname(re, (unsigned int) lr->lr_number),
6471cf781b2eSEd Maste 		    (uintmax_t) lr->lr_number2);
6472cf781b2eSEd Maste 		break;
6473cf781b2eSEd Maste 
6474cf781b2eSEd Maste 	case DW_OP_GNU_convert:
6475cf781b2eSEd Maste 	case DW_OP_GNU_deref_type:
6476cf781b2eSEd Maste 	case DW_OP_GNU_parameter_ref:
6477cf781b2eSEd Maste 	case DW_OP_GNU_reinterpret:
6478cf781b2eSEd Maste 		printf(": <0x%jx>", (uintmax_t) lr->lr_number);
6479cf781b2eSEd Maste 		break;
6480cf781b2eSEd Maste 
6481cf781b2eSEd Maste 	default:
6482cf781b2eSEd Maste 		break;
64832c23cb7cSEd Maste 	}
6484cf781b2eSEd Maste }
6485cf781b2eSEd Maste 
6486cf781b2eSEd Maste static void
6487cf781b2eSEd Maste dump_dwarf_block(struct readelf *re, uint8_t *b, Dwarf_Unsigned len)
6488cf781b2eSEd Maste {
6489cf781b2eSEd Maste 	Dwarf_Locdesc *llbuf;
6490cf781b2eSEd Maste 	Dwarf_Signed lcnt;
6491cf781b2eSEd Maste 	Dwarf_Error de;
6492cf781b2eSEd Maste 	int i;
6493cf781b2eSEd Maste 
6494cf781b2eSEd Maste 	if (dwarf_loclist_from_expr_b(re->dbg, b, len, re->cu_psize,
6495cf781b2eSEd Maste 	    re->cu_osize, re->cu_ver, &llbuf, &lcnt, &de) != DW_DLV_OK) {
6496cf781b2eSEd Maste 		warnx("dwarf_loclist_form_expr_b: %s", dwarf_errmsg(de));
6497cf781b2eSEd Maste 		return;
6498cf781b2eSEd Maste 	}
6499cf781b2eSEd Maste 
6500cf781b2eSEd Maste 	for (i = 0; (Dwarf_Half) i < llbuf->ld_cents; i++) {
6501cf781b2eSEd Maste 		dump_dwarf_loc(re, &llbuf->ld_s[i]);
6502cf781b2eSEd Maste 		if (i < llbuf->ld_cents - 1)
6503cf781b2eSEd Maste 			printf("; ");
6504cf781b2eSEd Maste 	}
6505cf781b2eSEd Maste 
6506cf781b2eSEd Maste 	dwarf_dealloc(re->dbg, llbuf->ld_s, DW_DLA_LOC_BLOCK);
6507cf781b2eSEd Maste 	dwarf_dealloc(re->dbg, llbuf, DW_DLA_LOCDESC);
6508cf781b2eSEd Maste }
6509cf781b2eSEd Maste 
6510cf781b2eSEd Maste static void
6511cf781b2eSEd Maste dump_dwarf_loclist(struct readelf *re)
6512cf781b2eSEd Maste {
6513cf781b2eSEd Maste 	Dwarf_Die die;
6514cf781b2eSEd Maste 	Dwarf_Locdesc **llbuf;
6515cf781b2eSEd Maste 	Dwarf_Unsigned lowpc;
6516cf781b2eSEd Maste 	Dwarf_Signed lcnt;
6517cf781b2eSEd Maste 	Dwarf_Half tag, version, pointer_size, off_size;
6518cf781b2eSEd Maste 	Dwarf_Error de;
6519cf781b2eSEd Maste 	struct loc_at *la;
6520cf781b2eSEd Maste 	int i, j, ret;
6521cf781b2eSEd Maste 
6522cf781b2eSEd Maste 	printf("\nContents of section .debug_loc:\n");
6523cf781b2eSEd Maste 
6524cf781b2eSEd Maste 	/* Search .debug_info section. */
6525cf781b2eSEd Maste 	while ((ret = dwarf_next_cu_header_b(re->dbg, NULL, &version, NULL,
6526cf781b2eSEd Maste 	    &pointer_size, &off_size, NULL, NULL, &de)) == DW_DLV_OK) {
6527cf781b2eSEd Maste 		set_cu_context(re, pointer_size, off_size, version);
6528cf781b2eSEd Maste 		die = NULL;
6529cf781b2eSEd Maste 		if (dwarf_siblingof(re->dbg, die, &die, &de) != DW_DLV_OK)
6530cf781b2eSEd Maste 			continue;
6531cf781b2eSEd Maste 		if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
6532cf781b2eSEd Maste 			warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
6533cf781b2eSEd Maste 			continue;
6534cf781b2eSEd Maste 		}
6535cf781b2eSEd Maste 		/* XXX: What about DW_TAG_partial_unit? */
6536cf781b2eSEd Maste 		lowpc = 0;
6537cf781b2eSEd Maste 		if (tag == DW_TAG_compile_unit) {
6538cf781b2eSEd Maste 			if (dwarf_attrval_unsigned(die, DW_AT_low_pc,
6539cf781b2eSEd Maste 				&lowpc, &de) != DW_DLV_OK)
6540cf781b2eSEd Maste 				lowpc = 0;
6541cf781b2eSEd Maste 		}
6542cf781b2eSEd Maste 
6543cf781b2eSEd Maste 		/* Search attributes for reference to .debug_loc section. */
6544cf781b2eSEd Maste 		search_loclist_at(re, die, lowpc);
6545cf781b2eSEd Maste 	}
6546cf781b2eSEd Maste 	if (ret == DW_DLV_ERROR)
6547cf781b2eSEd Maste 		warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
6548cf781b2eSEd Maste 
6549cf781b2eSEd Maste 	/* Search .debug_types section. */
6550cf781b2eSEd Maste 	do {
6551cf781b2eSEd Maste 		while ((ret = dwarf_next_cu_header_c(re->dbg, 0, NULL,
6552cf781b2eSEd Maste 		    &version, NULL, &pointer_size, &off_size, NULL, NULL,
6553cf781b2eSEd Maste 		    NULL, NULL, &de)) == DW_DLV_OK) {
6554cf781b2eSEd Maste 			set_cu_context(re, pointer_size, off_size, version);
6555cf781b2eSEd Maste 			die = NULL;
6556cf781b2eSEd Maste 			if (dwarf_siblingof(re->dbg, die, &die, &de) !=
6557cf781b2eSEd Maste 			    DW_DLV_OK)
6558cf781b2eSEd Maste 				continue;
6559cf781b2eSEd Maste 			if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
6560cf781b2eSEd Maste 				warnx("dwarf_tag failed: %s",
6561cf781b2eSEd Maste 				    dwarf_errmsg(de));
6562cf781b2eSEd Maste 				continue;
6563cf781b2eSEd Maste 			}
6564cf781b2eSEd Maste 
6565cf781b2eSEd Maste 			lowpc = 0;
6566cf781b2eSEd Maste 			if (tag == DW_TAG_type_unit) {
6567cf781b2eSEd Maste 				if (dwarf_attrval_unsigned(die, DW_AT_low_pc,
6568cf781b2eSEd Maste 				    &lowpc, &de) != DW_DLV_OK)
6569cf781b2eSEd Maste 					lowpc = 0;
6570cf781b2eSEd Maste 			}
6571cf781b2eSEd Maste 
6572cf781b2eSEd Maste 			/*
6573cf781b2eSEd Maste 			 * Search attributes for reference to .debug_loc
6574cf781b2eSEd Maste 			 * section.
6575cf781b2eSEd Maste 			 */
6576cf781b2eSEd Maste 			search_loclist_at(re, die, lowpc);
6577cf781b2eSEd Maste 		}
6578cf781b2eSEd Maste 		if (ret == DW_DLV_ERROR)
6579cf781b2eSEd Maste 			warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
6580cf781b2eSEd Maste 	} while (dwarf_next_types_section(re->dbg, &de) == DW_DLV_OK);
6581cf781b2eSEd Maste 
6582cf781b2eSEd Maste 	if (TAILQ_EMPTY(&lalist))
6583cf781b2eSEd Maste 		return;
6584cf781b2eSEd Maste 
6585cf781b2eSEd Maste 	printf("    Offset   Begin    End      Expression\n");
6586cf781b2eSEd Maste 
6587cf781b2eSEd Maste 	TAILQ_FOREACH(la, &lalist, la_next) {
6588cf781b2eSEd Maste 		if (dwarf_loclist_n(la->la_at, &llbuf, &lcnt, &de) !=
6589cf781b2eSEd Maste 		    DW_DLV_OK) {
6590cf781b2eSEd Maste 			warnx("dwarf_loclist_n failed: %s", dwarf_errmsg(de));
6591cf781b2eSEd Maste 			continue;
6592cf781b2eSEd Maste 		}
6593cf781b2eSEd Maste 		set_cu_context(re, la->la_cu_psize, la->la_cu_osize,
6594cf781b2eSEd Maste 		    la->la_cu_ver);
6595cf781b2eSEd Maste 		for (i = 0; i < lcnt; i++) {
6596cf781b2eSEd Maste 			printf("    %8.8jx ", la->la_off);
6597cf781b2eSEd Maste 			if (llbuf[i]->ld_lopc == 0 && llbuf[i]->ld_hipc == 0) {
6598cf781b2eSEd Maste 				printf("<End of list>\n");
6599cf781b2eSEd Maste 				continue;
6600cf781b2eSEd Maste 			}
6601cf781b2eSEd Maste 
6602cf781b2eSEd Maste 			/* TODO: handle base selection entry. */
6603cf781b2eSEd Maste 
6604cf781b2eSEd Maste 			printf("%8.8jx %8.8jx ",
6605cf781b2eSEd Maste 			    (uintmax_t) (la->la_lowpc + llbuf[i]->ld_lopc),
6606cf781b2eSEd Maste 			    (uintmax_t) (la->la_lowpc + llbuf[i]->ld_hipc));
6607cf781b2eSEd Maste 
6608cf781b2eSEd Maste 			putchar('(');
6609cf781b2eSEd Maste 			for (j = 0; (Dwarf_Half) j < llbuf[i]->ld_cents; j++) {
6610cf781b2eSEd Maste 				dump_dwarf_loc(re, &llbuf[i]->ld_s[j]);
66112c23cb7cSEd Maste 				if (j < llbuf[i]->ld_cents - 1)
6612cf781b2eSEd Maste 					printf("; ");
66132c23cb7cSEd Maste 			}
66142c23cb7cSEd Maste 			putchar(')');
66152c23cb7cSEd Maste 
66162c23cb7cSEd Maste 			if (llbuf[i]->ld_lopc == llbuf[i]->ld_hipc)
66172c23cb7cSEd Maste 				printf(" (start == end)");
66182c23cb7cSEd Maste 			putchar('\n');
66192c23cb7cSEd Maste 		}
6620cf781b2eSEd Maste 		for (i = 0; i < lcnt; i++) {
6621cf781b2eSEd Maste 			dwarf_dealloc(re->dbg, llbuf[i]->ld_s,
6622cf781b2eSEd Maste 			    DW_DLA_LOC_BLOCK);
6623cf781b2eSEd Maste 			dwarf_dealloc(re->dbg, llbuf[i], DW_DLA_LOCDESC);
6624cf781b2eSEd Maste 		}
6625cf781b2eSEd Maste 		dwarf_dealloc(re->dbg, llbuf, DW_DLA_LIST);
66262c23cb7cSEd Maste 	}
66272c23cb7cSEd Maste }
66282c23cb7cSEd Maste 
66292c23cb7cSEd Maste /*
66302c23cb7cSEd Maste  * Retrieve a string using string table section index and the string offset.
66312c23cb7cSEd Maste  */
66322c23cb7cSEd Maste static const char*
66332c23cb7cSEd Maste get_string(struct readelf *re, int strtab, size_t off)
66342c23cb7cSEd Maste {
66352c23cb7cSEd Maste 	const char *name;
66362c23cb7cSEd Maste 
66372c23cb7cSEd Maste 	if ((name = elf_strptr(re->elf, strtab, off)) == NULL)
66382c23cb7cSEd Maste 		return ("");
66392c23cb7cSEd Maste 
66402c23cb7cSEd Maste 	return (name);
66412c23cb7cSEd Maste }
66422c23cb7cSEd Maste 
66432c23cb7cSEd Maste /*
66442c23cb7cSEd Maste  * Retrieve the name of a symbol using the section index of the symbol
66452c23cb7cSEd Maste  * table and the index of the symbol within that table.
66462c23cb7cSEd Maste  */
66472c23cb7cSEd Maste static const char *
66482c23cb7cSEd Maste get_symbol_name(struct readelf *re, int symtab, int i)
66492c23cb7cSEd Maste {
66502c23cb7cSEd Maste 	struct section	*s;
66512c23cb7cSEd Maste 	const char	*name;
66522c23cb7cSEd Maste 	GElf_Sym	 sym;
66532c23cb7cSEd Maste 	Elf_Data	*data;
66542c23cb7cSEd Maste 	int		 elferr;
66552c23cb7cSEd Maste 
66562c23cb7cSEd Maste 	s = &re->sl[symtab];
66572c23cb7cSEd Maste 	if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM)
66582c23cb7cSEd Maste 		return ("");
66592c23cb7cSEd Maste 	(void) elf_errno();
66602c23cb7cSEd Maste 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
66612c23cb7cSEd Maste 		elferr = elf_errno();
66622c23cb7cSEd Maste 		if (elferr != 0)
66632c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
66642c23cb7cSEd Maste 		return ("");
66652c23cb7cSEd Maste 	}
66662c23cb7cSEd Maste 	if (gelf_getsym(data, i, &sym) != &sym)
66672c23cb7cSEd Maste 		return ("");
66682c23cb7cSEd Maste 	/* Return section name for STT_SECTION symbol. */
66692c23cb7cSEd Maste 	if (GELF_ST_TYPE(sym.st_info) == STT_SECTION &&
66702c23cb7cSEd Maste 	    re->sl[sym.st_shndx].name != NULL)
66712c23cb7cSEd Maste 		return (re->sl[sym.st_shndx].name);
66722c23cb7cSEd Maste 	if ((name = elf_strptr(re->elf, s->link, sym.st_name)) == NULL)
66732c23cb7cSEd Maste 		return ("");
66742c23cb7cSEd Maste 
66752c23cb7cSEd Maste 	return (name);
66762c23cb7cSEd Maste }
66772c23cb7cSEd Maste 
66782c23cb7cSEd Maste static uint64_t
66792c23cb7cSEd Maste get_symbol_value(struct readelf *re, int symtab, int i)
66802c23cb7cSEd Maste {
66812c23cb7cSEd Maste 	struct section	*s;
66822c23cb7cSEd Maste 	GElf_Sym	 sym;
66832c23cb7cSEd Maste 	Elf_Data	*data;
66842c23cb7cSEd Maste 	int		 elferr;
66852c23cb7cSEd Maste 
66862c23cb7cSEd Maste 	s = &re->sl[symtab];
66872c23cb7cSEd Maste 	if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM)
66882c23cb7cSEd Maste 		return (0);
66892c23cb7cSEd Maste 	(void) elf_errno();
66902c23cb7cSEd Maste 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
66912c23cb7cSEd Maste 		elferr = elf_errno();
66922c23cb7cSEd Maste 		if (elferr != 0)
66932c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
66942c23cb7cSEd Maste 		return (0);
66952c23cb7cSEd Maste 	}
66962c23cb7cSEd Maste 	if (gelf_getsym(data, i, &sym) != &sym)
66972c23cb7cSEd Maste 		return (0);
66982c23cb7cSEd Maste 
66992c23cb7cSEd Maste 	return (sym.st_value);
67002c23cb7cSEd Maste }
67012c23cb7cSEd Maste 
67022c23cb7cSEd Maste static void
67032c23cb7cSEd Maste hex_dump(struct readelf *re)
67042c23cb7cSEd Maste {
67052c23cb7cSEd Maste 	struct section *s;
67062c23cb7cSEd Maste 	Elf_Data *d;
67072c23cb7cSEd Maste 	uint8_t *buf;
67082c23cb7cSEd Maste 	size_t sz, nbytes;
67092c23cb7cSEd Maste 	uint64_t addr;
67102c23cb7cSEd Maste 	int elferr, i, j;
67112c23cb7cSEd Maste 
67122c23cb7cSEd Maste 	for (i = 1; (size_t) i < re->shnum; i++) {
67132c23cb7cSEd Maste 		s = &re->sl[i];
67142c23cb7cSEd Maste 		if (find_dumpop(re, (size_t) i, s->name, HEX_DUMP, -1) == NULL)
67152c23cb7cSEd Maste 			continue;
67162c23cb7cSEd Maste 		(void) elf_errno();
67172c23cb7cSEd Maste 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
67182c23cb7cSEd Maste 			elferr = elf_errno();
67192c23cb7cSEd Maste 			if (elferr != 0)
67202c23cb7cSEd Maste 				warnx("elf_getdata failed: %s",
67212c23cb7cSEd Maste 				    elf_errmsg(elferr));
67222c23cb7cSEd Maste 			continue;
67232c23cb7cSEd Maste 		}
67242c23cb7cSEd Maste 		if (d->d_size <= 0 || d->d_buf == NULL) {
67252c23cb7cSEd Maste 			printf("\nSection '%s' has no data to dump.\n",
67262c23cb7cSEd Maste 			    s->name);
67272c23cb7cSEd Maste 			continue;
67282c23cb7cSEd Maste 		}
67292c23cb7cSEd Maste 		buf = d->d_buf;
67302c23cb7cSEd Maste 		sz = d->d_size;
67312c23cb7cSEd Maste 		addr = s->addr;
67322c23cb7cSEd Maste 		printf("\nHex dump of section '%s':\n", s->name);
67332c23cb7cSEd Maste 		while (sz > 0) {
67342c23cb7cSEd Maste 			printf("  0x%8.8jx ", (uintmax_t)addr);
67352c23cb7cSEd Maste 			nbytes = sz > 16? 16 : sz;
67362c23cb7cSEd Maste 			for (j = 0; j < 16; j++) {
67372c23cb7cSEd Maste 				if ((size_t)j < nbytes)
67382c23cb7cSEd Maste 					printf("%2.2x", buf[j]);
67392c23cb7cSEd Maste 				else
67402c23cb7cSEd Maste 					printf("  ");
67412c23cb7cSEd Maste 				if ((j & 3) == 3)
67422c23cb7cSEd Maste 					printf(" ");
67432c23cb7cSEd Maste 			}
67442c23cb7cSEd Maste 			for (j = 0; (size_t)j < nbytes; j++) {
67452c23cb7cSEd Maste 				if (isprint(buf[j]))
67462c23cb7cSEd Maste 					printf("%c", buf[j]);
67472c23cb7cSEd Maste 				else
67482c23cb7cSEd Maste 					printf(".");
67492c23cb7cSEd Maste 			}
67502c23cb7cSEd Maste 			printf("\n");
67512c23cb7cSEd Maste 			buf += nbytes;
67522c23cb7cSEd Maste 			addr += nbytes;
67532c23cb7cSEd Maste 			sz -= nbytes;
67542c23cb7cSEd Maste 		}
67552c23cb7cSEd Maste 	}
67562c23cb7cSEd Maste }
67572c23cb7cSEd Maste 
67582c23cb7cSEd Maste static void
67592c23cb7cSEd Maste str_dump(struct readelf *re)
67602c23cb7cSEd Maste {
67612c23cb7cSEd Maste 	struct section *s;
67622c23cb7cSEd Maste 	Elf_Data *d;
67632c23cb7cSEd Maste 	unsigned char *start, *end, *buf_end;
67642c23cb7cSEd Maste 	unsigned int len;
67652c23cb7cSEd Maste 	int i, j, elferr, found;
67662c23cb7cSEd Maste 
67672c23cb7cSEd Maste 	for (i = 1; (size_t) i < re->shnum; i++) {
67682c23cb7cSEd Maste 		s = &re->sl[i];
67692c23cb7cSEd Maste 		if (find_dumpop(re, (size_t) i, s->name, STR_DUMP, -1) == NULL)
67702c23cb7cSEd Maste 			continue;
67712c23cb7cSEd Maste 		(void) elf_errno();
67722c23cb7cSEd Maste 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
67732c23cb7cSEd Maste 			elferr = elf_errno();
67742c23cb7cSEd Maste 			if (elferr != 0)
67752c23cb7cSEd Maste 				warnx("elf_getdata failed: %s",
67762c23cb7cSEd Maste 				    elf_errmsg(elferr));
67772c23cb7cSEd Maste 			continue;
67782c23cb7cSEd Maste 		}
67792c23cb7cSEd Maste 		if (d->d_size <= 0 || d->d_buf == NULL) {
67802c23cb7cSEd Maste 			printf("\nSection '%s' has no data to dump.\n",
67812c23cb7cSEd Maste 			    s->name);
67822c23cb7cSEd Maste 			continue;
67832c23cb7cSEd Maste 		}
67842c23cb7cSEd Maste 		buf_end = (unsigned char *) d->d_buf + d->d_size;
67852c23cb7cSEd Maste 		start = (unsigned char *) d->d_buf;
67862c23cb7cSEd Maste 		found = 0;
67872c23cb7cSEd Maste 		printf("\nString dump of section '%s':\n", s->name);
67882c23cb7cSEd Maste 		for (;;) {
67892c23cb7cSEd Maste 			while (start < buf_end && !isprint(*start))
67902c23cb7cSEd Maste 				start++;
67912c23cb7cSEd Maste 			if (start >= buf_end)
67922c23cb7cSEd Maste 				break;
67932c23cb7cSEd Maste 			end = start + 1;
67942c23cb7cSEd Maste 			while (end < buf_end && isprint(*end))
67952c23cb7cSEd Maste 				end++;
67962c23cb7cSEd Maste 			printf("  [%6lx]  ",
67972c23cb7cSEd Maste 			    (long) (start - (unsigned char *) d->d_buf));
67982c23cb7cSEd Maste 			len = end - start;
67992c23cb7cSEd Maste 			for (j = 0; (unsigned int) j < len; j++)
68002c23cb7cSEd Maste 				putchar(start[j]);
68012c23cb7cSEd Maste 			putchar('\n');
68022c23cb7cSEd Maste 			found = 1;
68032c23cb7cSEd Maste 			if (end >= buf_end)
68042c23cb7cSEd Maste 				break;
68052c23cb7cSEd Maste 			start = end + 1;
68062c23cb7cSEd Maste 		}
68072c23cb7cSEd Maste 		if (!found)
68082c23cb7cSEd Maste 			printf("  No strings found in this section.");
68092c23cb7cSEd Maste 		putchar('\n');
68102c23cb7cSEd Maste 	}
68112c23cb7cSEd Maste }
68122c23cb7cSEd Maste 
68132c23cb7cSEd Maste static void
68142c23cb7cSEd Maste load_sections(struct readelf *re)
68152c23cb7cSEd Maste {
68162c23cb7cSEd Maste 	struct section	*s;
68172c23cb7cSEd Maste 	const char	*name;
68182c23cb7cSEd Maste 	Elf_Scn		*scn;
68192c23cb7cSEd Maste 	GElf_Shdr	 sh;
68202c23cb7cSEd Maste 	size_t		 shstrndx, ndx;
68212c23cb7cSEd Maste 	int		 elferr;
68222c23cb7cSEd Maste 
68232c23cb7cSEd Maste 	/* Allocate storage for internal section list. */
68242c23cb7cSEd Maste 	if (!elf_getshnum(re->elf, &re->shnum)) {
68252c23cb7cSEd Maste 		warnx("elf_getshnum failed: %s", elf_errmsg(-1));
68262c23cb7cSEd Maste 		return;
68272c23cb7cSEd Maste 	}
68282c23cb7cSEd Maste 	if (re->sl != NULL)
68292c23cb7cSEd Maste 		free(re->sl);
68302c23cb7cSEd Maste 	if ((re->sl = calloc(re->shnum, sizeof(*re->sl))) == NULL)
68312c23cb7cSEd Maste 		err(EXIT_FAILURE, "calloc failed");
68322c23cb7cSEd Maste 
68332c23cb7cSEd Maste 	/* Get the index of .shstrtab section. */
68342c23cb7cSEd Maste 	if (!elf_getshstrndx(re->elf, &shstrndx)) {
68352c23cb7cSEd Maste 		warnx("elf_getshstrndx failed: %s", elf_errmsg(-1));
68362c23cb7cSEd Maste 		return;
68372c23cb7cSEd Maste 	}
68382c23cb7cSEd Maste 
683971a0c925SEd Maste 	if ((scn = elf_getscn(re->elf, 0)) == NULL)
68402c23cb7cSEd Maste 		return;
68412c23cb7cSEd Maste 
68422c23cb7cSEd Maste 	(void) elf_errno();
68432c23cb7cSEd Maste 	do {
68442c23cb7cSEd Maste 		if (gelf_getshdr(scn, &sh) == NULL) {
68452c23cb7cSEd Maste 			warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
68462c23cb7cSEd Maste 			(void) elf_errno();
68472c23cb7cSEd Maste 			continue;
68482c23cb7cSEd Maste 		}
68492c23cb7cSEd Maste 		if ((name = elf_strptr(re->elf, shstrndx, sh.sh_name)) == NULL) {
68502c23cb7cSEd Maste 			(void) elf_errno();
68512c23cb7cSEd Maste 			name = "ERROR";
68522c23cb7cSEd Maste 		}
68532c23cb7cSEd Maste 		if ((ndx = elf_ndxscn(scn)) == SHN_UNDEF) {
68542c23cb7cSEd Maste 			if ((elferr = elf_errno()) != 0)
68552c23cb7cSEd Maste 				warnx("elf_ndxscn failed: %s",
68562c23cb7cSEd Maste 				    elf_errmsg(elferr));
68572c23cb7cSEd Maste 			continue;
68582c23cb7cSEd Maste 		}
68592c23cb7cSEd Maste 		if (ndx >= re->shnum) {
68602c23cb7cSEd Maste 			warnx("section index of '%s' out of range", name);
68612c23cb7cSEd Maste 			continue;
68622c23cb7cSEd Maste 		}
68632c23cb7cSEd Maste 		s = &re->sl[ndx];
68642c23cb7cSEd Maste 		s->name = name;
68652c23cb7cSEd Maste 		s->scn = scn;
68662c23cb7cSEd Maste 		s->off = sh.sh_offset;
68672c23cb7cSEd Maste 		s->sz = sh.sh_size;
68682c23cb7cSEd Maste 		s->entsize = sh.sh_entsize;
68692c23cb7cSEd Maste 		s->align = sh.sh_addralign;
68702c23cb7cSEd Maste 		s->type = sh.sh_type;
68712c23cb7cSEd Maste 		s->flags = sh.sh_flags;
68722c23cb7cSEd Maste 		s->addr = sh.sh_addr;
68732c23cb7cSEd Maste 		s->link = sh.sh_link;
68742c23cb7cSEd Maste 		s->info = sh.sh_info;
68752c23cb7cSEd Maste 	} while ((scn = elf_nextscn(re->elf, scn)) != NULL);
68762c23cb7cSEd Maste 	elferr = elf_errno();
68772c23cb7cSEd Maste 	if (elferr != 0)
68782c23cb7cSEd Maste 		warnx("elf_nextscn failed: %s", elf_errmsg(elferr));
68792c23cb7cSEd Maste }
68802c23cb7cSEd Maste 
68812c23cb7cSEd Maste static void
68822c23cb7cSEd Maste unload_sections(struct readelf *re)
68832c23cb7cSEd Maste {
68842c23cb7cSEd Maste 
68852c23cb7cSEd Maste 	if (re->sl != NULL) {
68862c23cb7cSEd Maste 		free(re->sl);
68872c23cb7cSEd Maste 		re->sl = NULL;
68882c23cb7cSEd Maste 	}
68892c23cb7cSEd Maste 	re->shnum = 0;
68902c23cb7cSEd Maste 	re->vd_s = NULL;
68912c23cb7cSEd Maste 	re->vn_s = NULL;
68922c23cb7cSEd Maste 	re->vs_s = NULL;
68932c23cb7cSEd Maste 	re->vs = NULL;
68942c23cb7cSEd Maste 	re->vs_sz = 0;
68952c23cb7cSEd Maste 	if (re->ver != NULL) {
68962c23cb7cSEd Maste 		free(re->ver);
68972c23cb7cSEd Maste 		re->ver = NULL;
68982c23cb7cSEd Maste 		re->ver_sz = 0;
68992c23cb7cSEd Maste 	}
69002c23cb7cSEd Maste }
69012c23cb7cSEd Maste 
69022c23cb7cSEd Maste static void
69032c23cb7cSEd Maste dump_elf(struct readelf *re)
69042c23cb7cSEd Maste {
69052c23cb7cSEd Maste 
69062c23cb7cSEd Maste 	/* Fetch ELF header. No need to continue if it fails. */
69072c23cb7cSEd Maste 	if (gelf_getehdr(re->elf, &re->ehdr) == NULL) {
69082c23cb7cSEd Maste 		warnx("gelf_getehdr failed: %s", elf_errmsg(-1));
69092c23cb7cSEd Maste 		return;
69102c23cb7cSEd Maste 	}
69112c23cb7cSEd Maste 	if ((re->ec = gelf_getclass(re->elf)) == ELFCLASSNONE) {
69122c23cb7cSEd Maste 		warnx("gelf_getclass failed: %s", elf_errmsg(-1));
69132c23cb7cSEd Maste 		return;
69142c23cb7cSEd Maste 	}
69152c23cb7cSEd Maste 	if (re->ehdr.e_ident[EI_DATA] == ELFDATA2MSB) {
69162c23cb7cSEd Maste 		re->dw_read = _read_msb;
69172c23cb7cSEd Maste 		re->dw_decode = _decode_msb;
69182c23cb7cSEd Maste 	} else {
69192c23cb7cSEd Maste 		re->dw_read = _read_lsb;
69202c23cb7cSEd Maste 		re->dw_decode = _decode_lsb;
69212c23cb7cSEd Maste 	}
69222c23cb7cSEd Maste 
69232c23cb7cSEd Maste 	if (re->options & ~RE_H)
69242c23cb7cSEd Maste 		load_sections(re);
69252c23cb7cSEd Maste 	if ((re->options & RE_VV) || (re->options & RE_S))
69262c23cb7cSEd Maste 		search_ver(re);
69272c23cb7cSEd Maste 	if (re->options & RE_H)
69282c23cb7cSEd Maste 		dump_ehdr(re);
69292c23cb7cSEd Maste 	if (re->options & RE_L)
69302c23cb7cSEd Maste 		dump_phdr(re);
69312c23cb7cSEd Maste 	if (re->options & RE_SS)
69322c23cb7cSEd Maste 		dump_shdr(re);
69333ef90571SEd Maste 	if (re->options & RE_G)
69343ef90571SEd Maste 		dump_section_groups(re);
69352c23cb7cSEd Maste 	if (re->options & RE_D)
69362c23cb7cSEd Maste 		dump_dynamic(re);
69372c23cb7cSEd Maste 	if (re->options & RE_R)
69382c23cb7cSEd Maste 		dump_reloc(re);
69392c23cb7cSEd Maste 	if (re->options & RE_S)
69402c23cb7cSEd Maste 		dump_symtabs(re);
69412c23cb7cSEd Maste 	if (re->options & RE_N)
69422c23cb7cSEd Maste 		dump_notes(re);
69432c23cb7cSEd Maste 	if (re->options & RE_II)
69442c23cb7cSEd Maste 		dump_hash(re);
69452c23cb7cSEd Maste 	if (re->options & RE_X)
69462c23cb7cSEd Maste 		hex_dump(re);
69472c23cb7cSEd Maste 	if (re->options & RE_P)
69482c23cb7cSEd Maste 		str_dump(re);
69492c23cb7cSEd Maste 	if (re->options & RE_VV)
69502c23cb7cSEd Maste 		dump_ver(re);
69512c23cb7cSEd Maste 	if (re->options & RE_AA)
69522c23cb7cSEd Maste 		dump_arch_specific_info(re);
69532c23cb7cSEd Maste 	if (re->options & RE_W)
69542c23cb7cSEd Maste 		dump_dwarf(re);
69552c23cb7cSEd Maste 	if (re->options & ~RE_H)
69562c23cb7cSEd Maste 		unload_sections(re);
69572c23cb7cSEd Maste }
69582c23cb7cSEd Maste 
69592c23cb7cSEd Maste static void
69602c23cb7cSEd Maste dump_dwarf(struct readelf *re)
69612c23cb7cSEd Maste {
69622c23cb7cSEd Maste 	int error;
69632c23cb7cSEd Maste 	Dwarf_Error de;
69642c23cb7cSEd Maste 
69652c23cb7cSEd Maste 	if (dwarf_elf_init(re->elf, DW_DLC_READ, NULL, NULL, &re->dbg, &de)) {
69662c23cb7cSEd Maste 		if ((error = dwarf_errno(de)) != DW_DLE_DEBUG_INFO_NULL)
69672c23cb7cSEd Maste 			errx(EXIT_FAILURE, "dwarf_elf_init failed: %s",
69682c23cb7cSEd Maste 			    dwarf_errmsg(de));
69692c23cb7cSEd Maste 		return;
69702c23cb7cSEd Maste 	}
69712c23cb7cSEd Maste 
69722c23cb7cSEd Maste 	if (re->dop & DW_A)
69732c23cb7cSEd Maste 		dump_dwarf_abbrev(re);
69742c23cb7cSEd Maste 	if (re->dop & DW_L)
69752c23cb7cSEd Maste 		dump_dwarf_line(re);
69762c23cb7cSEd Maste 	if (re->dop & DW_LL)
69772c23cb7cSEd Maste 		dump_dwarf_line_decoded(re);
6978cf781b2eSEd Maste 	if (re->dop & DW_I) {
6979cf781b2eSEd Maste 		dump_dwarf_info(re, 0);
6980cf781b2eSEd Maste 		dump_dwarf_info(re, 1);
6981cf781b2eSEd Maste 	}
69822c23cb7cSEd Maste 	if (re->dop & DW_P)
69832c23cb7cSEd Maste 		dump_dwarf_pubnames(re);
69842c23cb7cSEd Maste 	if (re->dop & DW_R)
69852c23cb7cSEd Maste 		dump_dwarf_aranges(re);
69862c23cb7cSEd Maste 	if (re->dop & DW_RR)
69872c23cb7cSEd Maste 		dump_dwarf_ranges(re);
69882c23cb7cSEd Maste 	if (re->dop & DW_M)
69892c23cb7cSEd Maste 		dump_dwarf_macinfo(re);
69902c23cb7cSEd Maste 	if (re->dop & DW_F)
69912c23cb7cSEd Maste 		dump_dwarf_frame(re, 0);
69922c23cb7cSEd Maste 	else if (re->dop & DW_FF)
69932c23cb7cSEd Maste 		dump_dwarf_frame(re, 1);
69942c23cb7cSEd Maste 	if (re->dop & DW_S)
69952c23cb7cSEd Maste 		dump_dwarf_str(re);
69962c23cb7cSEd Maste 	if (re->dop & DW_O)
69972c23cb7cSEd Maste 		dump_dwarf_loclist(re);
69982c23cb7cSEd Maste 
69992c23cb7cSEd Maste 	dwarf_finish(re->dbg, &de);
70002c23cb7cSEd Maste }
70012c23cb7cSEd Maste 
70022c23cb7cSEd Maste static void
70032c23cb7cSEd Maste dump_ar(struct readelf *re, int fd)
70042c23cb7cSEd Maste {
70052c23cb7cSEd Maste 	Elf_Arsym *arsym;
70062c23cb7cSEd Maste 	Elf_Arhdr *arhdr;
70072c23cb7cSEd Maste 	Elf_Cmd cmd;
70082c23cb7cSEd Maste 	Elf *e;
70092c23cb7cSEd Maste 	size_t sz;
70102c23cb7cSEd Maste 	off_t off;
70112c23cb7cSEd Maste 	int i;
70122c23cb7cSEd Maste 
70132c23cb7cSEd Maste 	re->ar = re->elf;
70142c23cb7cSEd Maste 
70152c23cb7cSEd Maste 	if (re->options & RE_C) {
70162c23cb7cSEd Maste 		if ((arsym = elf_getarsym(re->ar, &sz)) == NULL) {
70172c23cb7cSEd Maste 			warnx("elf_getarsym() failed: %s", elf_errmsg(-1));
70182c23cb7cSEd Maste 			goto process_members;
70192c23cb7cSEd Maste 		}
70202c23cb7cSEd Maste 		printf("Index of archive %s: (%ju entries)\n", re->filename,
70212c23cb7cSEd Maste 		    (uintmax_t) sz - 1);
70222c23cb7cSEd Maste 		off = 0;
70232c23cb7cSEd Maste 		for (i = 0; (size_t) i < sz; i++) {
70242c23cb7cSEd Maste 			if (arsym[i].as_name == NULL)
70252c23cb7cSEd Maste 				break;
70262c23cb7cSEd Maste 			if (arsym[i].as_off != off) {
70272c23cb7cSEd Maste 				off = arsym[i].as_off;
70282c23cb7cSEd Maste 				if (elf_rand(re->ar, off) != off) {
70292c23cb7cSEd Maste 					warnx("elf_rand() failed: %s",
70302c23cb7cSEd Maste 					    elf_errmsg(-1));
70312c23cb7cSEd Maste 					continue;
70322c23cb7cSEd Maste 				}
70332c23cb7cSEd Maste 				if ((e = elf_begin(fd, ELF_C_READ, re->ar)) ==
70342c23cb7cSEd Maste 				    NULL) {
70352c23cb7cSEd Maste 					warnx("elf_begin() failed: %s",
70362c23cb7cSEd Maste 					    elf_errmsg(-1));
70372c23cb7cSEd Maste 					continue;
70382c23cb7cSEd Maste 				}
70392c23cb7cSEd Maste 				if ((arhdr = elf_getarhdr(e)) == NULL) {
70402c23cb7cSEd Maste 					warnx("elf_getarhdr() failed: %s",
70412c23cb7cSEd Maste 					    elf_errmsg(-1));
70422c23cb7cSEd Maste 					elf_end(e);
70432c23cb7cSEd Maste 					continue;
70442c23cb7cSEd Maste 				}
70452c23cb7cSEd Maste 				printf("Binary %s(%s) contains:\n",
70462c23cb7cSEd Maste 				    re->filename, arhdr->ar_name);
70472c23cb7cSEd Maste 			}
70482c23cb7cSEd Maste 			printf("\t%s\n", arsym[i].as_name);
70492c23cb7cSEd Maste 		}
70502c23cb7cSEd Maste 		if (elf_rand(re->ar, SARMAG) != SARMAG) {
70512c23cb7cSEd Maste 			warnx("elf_rand() failed: %s", elf_errmsg(-1));
70522c23cb7cSEd Maste 			return;
70532c23cb7cSEd Maste 		}
70542c23cb7cSEd Maste 	}
70552c23cb7cSEd Maste 
70562c23cb7cSEd Maste process_members:
70572c23cb7cSEd Maste 
70582c23cb7cSEd Maste 	if ((re->options & ~RE_C) == 0)
70592c23cb7cSEd Maste 		return;
70602c23cb7cSEd Maste 
70612c23cb7cSEd Maste 	cmd = ELF_C_READ;
70622c23cb7cSEd Maste 	while ((re->elf = elf_begin(fd, cmd, re->ar)) != NULL) {
70632c23cb7cSEd Maste 		if ((arhdr = elf_getarhdr(re->elf)) == NULL) {
70642c23cb7cSEd Maste 			warnx("elf_getarhdr() failed: %s", elf_errmsg(-1));
70652c23cb7cSEd Maste 			goto next_member;
70662c23cb7cSEd Maste 		}
70672c23cb7cSEd Maste 		if (strcmp(arhdr->ar_name, "/") == 0 ||
70682c23cb7cSEd Maste 		    strcmp(arhdr->ar_name, "//") == 0 ||
70692c23cb7cSEd Maste 		    strcmp(arhdr->ar_name, "__.SYMDEF") == 0)
70702c23cb7cSEd Maste 			goto next_member;
70712c23cb7cSEd Maste 		printf("\nFile: %s(%s)\n", re->filename, arhdr->ar_name);
70722c23cb7cSEd Maste 		dump_elf(re);
70732c23cb7cSEd Maste 
70742c23cb7cSEd Maste 	next_member:
70752c23cb7cSEd Maste 		cmd = elf_next(re->elf);
70762c23cb7cSEd Maste 		elf_end(re->elf);
70772c23cb7cSEd Maste 	}
70782c23cb7cSEd Maste 	re->elf = re->ar;
70792c23cb7cSEd Maste }
70802c23cb7cSEd Maste 
70812c23cb7cSEd Maste static void
70822c23cb7cSEd Maste dump_object(struct readelf *re)
70832c23cb7cSEd Maste {
70842c23cb7cSEd Maste 	int fd;
70852c23cb7cSEd Maste 
70862c23cb7cSEd Maste 	if ((fd = open(re->filename, O_RDONLY)) == -1) {
70872c23cb7cSEd Maste 		warn("open %s failed", re->filename);
70882c23cb7cSEd Maste 		return;
70892c23cb7cSEd Maste 	}
70902c23cb7cSEd Maste 
70912c23cb7cSEd Maste 	if ((re->flags & DISPLAY_FILENAME) != 0)
70922c23cb7cSEd Maste 		printf("\nFile: %s\n", re->filename);
70932c23cb7cSEd Maste 
70942c23cb7cSEd Maste 	if ((re->elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
70952c23cb7cSEd Maste 		warnx("elf_begin() failed: %s", elf_errmsg(-1));
70962c23cb7cSEd Maste 		return;
70972c23cb7cSEd Maste 	}
70982c23cb7cSEd Maste 
70992c23cb7cSEd Maste 	switch (elf_kind(re->elf)) {
71002c23cb7cSEd Maste 	case ELF_K_NONE:
71012c23cb7cSEd Maste 		warnx("Not an ELF file.");
71022c23cb7cSEd Maste 		return;
71032c23cb7cSEd Maste 	case ELF_K_ELF:
71042c23cb7cSEd Maste 		dump_elf(re);
71052c23cb7cSEd Maste 		break;
71062c23cb7cSEd Maste 	case ELF_K_AR:
71072c23cb7cSEd Maste 		dump_ar(re, fd);
71082c23cb7cSEd Maste 		break;
71092c23cb7cSEd Maste 	default:
71102c23cb7cSEd Maste 		warnx("Internal: libelf returned unknown elf kind.");
71112c23cb7cSEd Maste 		return;
71122c23cb7cSEd Maste 	}
71132c23cb7cSEd Maste 
71142c23cb7cSEd Maste 	elf_end(re->elf);
71152c23cb7cSEd Maste }
71162c23cb7cSEd Maste 
71172c23cb7cSEd Maste static void
71182c23cb7cSEd Maste add_dumpop(struct readelf *re, size_t si, const char *sn, int op, int t)
71192c23cb7cSEd Maste {
71202c23cb7cSEd Maste 	struct dumpop *d;
71212c23cb7cSEd Maste 
71222c23cb7cSEd Maste 	if ((d = find_dumpop(re, si, sn, -1, t)) == NULL) {
71232c23cb7cSEd Maste 		if ((d = calloc(1, sizeof(*d))) == NULL)
71242c23cb7cSEd Maste 			err(EXIT_FAILURE, "calloc failed");
71252c23cb7cSEd Maste 		if (t == DUMP_BY_INDEX)
71262c23cb7cSEd Maste 			d->u.si = si;
71272c23cb7cSEd Maste 		else
71282c23cb7cSEd Maste 			d->u.sn = sn;
71292c23cb7cSEd Maste 		d->type = t;
71302c23cb7cSEd Maste 		d->op = op;
71312c23cb7cSEd Maste 		STAILQ_INSERT_TAIL(&re->v_dumpop, d, dumpop_list);
71322c23cb7cSEd Maste 	} else
71332c23cb7cSEd Maste 		d->op |= op;
71342c23cb7cSEd Maste }
71352c23cb7cSEd Maste 
71362c23cb7cSEd Maste static struct dumpop *
71372c23cb7cSEd Maste find_dumpop(struct readelf *re, size_t si, const char *sn, int op, int t)
71382c23cb7cSEd Maste {
71392c23cb7cSEd Maste 	struct dumpop *d;
71402c23cb7cSEd Maste 
71412c23cb7cSEd Maste 	STAILQ_FOREACH(d, &re->v_dumpop, dumpop_list) {
71422c23cb7cSEd Maste 		if ((op == -1 || op & d->op) &&
71432c23cb7cSEd Maste 		    (t == -1 || (unsigned) t == d->type)) {
71442c23cb7cSEd Maste 			if ((d->type == DUMP_BY_INDEX && d->u.si == si) ||
71452c23cb7cSEd Maste 			    (d->type == DUMP_BY_NAME && !strcmp(d->u.sn, sn)))
71462c23cb7cSEd Maste 				return (d);
71472c23cb7cSEd Maste 		}
71482c23cb7cSEd Maste 	}
71492c23cb7cSEd Maste 
71502c23cb7cSEd Maste 	return (NULL);
71512c23cb7cSEd Maste }
71522c23cb7cSEd Maste 
71532c23cb7cSEd Maste static struct {
71542c23cb7cSEd Maste 	const char *ln;
71552c23cb7cSEd Maste 	char sn;
71562c23cb7cSEd Maste 	int value;
71572c23cb7cSEd Maste } dwarf_op[] = {
71582c23cb7cSEd Maste 	{"rawline", 'l', DW_L},
71592c23cb7cSEd Maste 	{"decodedline", 'L', DW_LL},
71602c23cb7cSEd Maste 	{"info", 'i', DW_I},
71612c23cb7cSEd Maste 	{"abbrev", 'a', DW_A},
71622c23cb7cSEd Maste 	{"pubnames", 'p', DW_P},
71632c23cb7cSEd Maste 	{"aranges", 'r', DW_R},
71642c23cb7cSEd Maste 	{"ranges", 'r', DW_R},
71652c23cb7cSEd Maste 	{"Ranges", 'R', DW_RR},
71662c23cb7cSEd Maste 	{"macro", 'm', DW_M},
71672c23cb7cSEd Maste 	{"frames", 'f', DW_F},
7168cf781b2eSEd Maste 	{"frames-interp", 'F', DW_FF},
71692c23cb7cSEd Maste 	{"str", 's', DW_S},
71702c23cb7cSEd Maste 	{"loc", 'o', DW_O},
71712c23cb7cSEd Maste 	{NULL, 0, 0}
71722c23cb7cSEd Maste };
71732c23cb7cSEd Maste 
71742c23cb7cSEd Maste static void
71752c23cb7cSEd Maste parse_dwarf_op_short(struct readelf *re, const char *op)
71762c23cb7cSEd Maste {
71772c23cb7cSEd Maste 	int i;
71782c23cb7cSEd Maste 
71792c23cb7cSEd Maste 	if (op == NULL) {
71802c23cb7cSEd Maste 		re->dop |= DW_DEFAULT_OPTIONS;
71812c23cb7cSEd Maste 		return;
71822c23cb7cSEd Maste 	}
71832c23cb7cSEd Maste 
71842c23cb7cSEd Maste 	for (; *op != '\0'; op++) {
71852c23cb7cSEd Maste 		for (i = 0; dwarf_op[i].ln != NULL; i++) {
71862c23cb7cSEd Maste 			if (dwarf_op[i].sn == *op) {
71872c23cb7cSEd Maste 				re->dop |= dwarf_op[i].value;
71882c23cb7cSEd Maste 				break;
71892c23cb7cSEd Maste 			}
71902c23cb7cSEd Maste 		}
71912c23cb7cSEd Maste 	}
71922c23cb7cSEd Maste }
71932c23cb7cSEd Maste 
71942c23cb7cSEd Maste static void
71952c23cb7cSEd Maste parse_dwarf_op_long(struct readelf *re, const char *op)
71962c23cb7cSEd Maste {
71972c23cb7cSEd Maste 	char *p, *token, *bp;
71982c23cb7cSEd Maste 	int i;
71992c23cb7cSEd Maste 
72002c23cb7cSEd Maste 	if (op == NULL) {
72012c23cb7cSEd Maste 		re->dop |= DW_DEFAULT_OPTIONS;
72022c23cb7cSEd Maste 		return;
72032c23cb7cSEd Maste 	}
72042c23cb7cSEd Maste 
72052c23cb7cSEd Maste 	if ((p = strdup(op)) == NULL)
72062c23cb7cSEd Maste 		err(EXIT_FAILURE, "strdup failed");
72072c23cb7cSEd Maste 	bp = p;
72082c23cb7cSEd Maste 
72092c23cb7cSEd Maste 	while ((token = strsep(&p, ",")) != NULL) {
72102c23cb7cSEd Maste 		for (i = 0; dwarf_op[i].ln != NULL; i++) {
72112c23cb7cSEd Maste 			if (!strcmp(token, dwarf_op[i].ln)) {
72122c23cb7cSEd Maste 				re->dop |= dwarf_op[i].value;
72132c23cb7cSEd Maste 				break;
72142c23cb7cSEd Maste 			}
72152c23cb7cSEd Maste 		}
72162c23cb7cSEd Maste 	}
72172c23cb7cSEd Maste 
72182c23cb7cSEd Maste 	free(bp);
72192c23cb7cSEd Maste }
72202c23cb7cSEd Maste 
72212c23cb7cSEd Maste static uint64_t
72222c23cb7cSEd Maste _read_lsb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read)
72232c23cb7cSEd Maste {
72242c23cb7cSEd Maste 	uint64_t ret;
72252c23cb7cSEd Maste 	uint8_t *src;
72262c23cb7cSEd Maste 
72272c23cb7cSEd Maste 	src = (uint8_t *) d->d_buf + *offsetp;
72282c23cb7cSEd Maste 
72292c23cb7cSEd Maste 	ret = 0;
72302c23cb7cSEd Maste 	switch (bytes_to_read) {
72312c23cb7cSEd Maste 	case 8:
72322c23cb7cSEd Maste 		ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
72332c23cb7cSEd Maste 		ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
72342c23cb7cSEd Maste 	case 4:
72352c23cb7cSEd Maste 		ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
72362c23cb7cSEd Maste 	case 2:
72372c23cb7cSEd Maste 		ret |= ((uint64_t) src[1]) << 8;
72382c23cb7cSEd Maste 	case 1:
72392c23cb7cSEd Maste 		ret |= src[0];
72402c23cb7cSEd Maste 		break;
72412c23cb7cSEd Maste 	default:
72422c23cb7cSEd Maste 		return (0);
72432c23cb7cSEd Maste 	}
72442c23cb7cSEd Maste 
72452c23cb7cSEd Maste 	*offsetp += bytes_to_read;
72462c23cb7cSEd Maste 
72472c23cb7cSEd Maste 	return (ret);
72482c23cb7cSEd Maste }
72492c23cb7cSEd Maste 
72502c23cb7cSEd Maste static uint64_t
72512c23cb7cSEd Maste _read_msb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read)
72522c23cb7cSEd Maste {
72532c23cb7cSEd Maste 	uint64_t ret;
72542c23cb7cSEd Maste 	uint8_t *src;
72552c23cb7cSEd Maste 
72562c23cb7cSEd Maste 	src = (uint8_t *) d->d_buf + *offsetp;
72572c23cb7cSEd Maste 
72582c23cb7cSEd Maste 	switch (bytes_to_read) {
72592c23cb7cSEd Maste 	case 1:
72602c23cb7cSEd Maste 		ret = src[0];
72612c23cb7cSEd Maste 		break;
72622c23cb7cSEd Maste 	case 2:
72632c23cb7cSEd Maste 		ret = src[1] | ((uint64_t) src[0]) << 8;
72642c23cb7cSEd Maste 		break;
72652c23cb7cSEd Maste 	case 4:
72662c23cb7cSEd Maste 		ret = src[3] | ((uint64_t) src[2]) << 8;
72672c23cb7cSEd Maste 		ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24;
72682c23cb7cSEd Maste 		break;
72692c23cb7cSEd Maste 	case 8:
72702c23cb7cSEd Maste 		ret = src[7] | ((uint64_t) src[6]) << 8;
72712c23cb7cSEd Maste 		ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24;
72722c23cb7cSEd Maste 		ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40;
72732c23cb7cSEd Maste 		ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56;
72742c23cb7cSEd Maste 		break;
72752c23cb7cSEd Maste 	default:
72762c23cb7cSEd Maste 		return (0);
72772c23cb7cSEd Maste 	}
72782c23cb7cSEd Maste 
72792c23cb7cSEd Maste 	*offsetp += bytes_to_read;
72802c23cb7cSEd Maste 
72812c23cb7cSEd Maste 	return (ret);
72822c23cb7cSEd Maste }
72832c23cb7cSEd Maste 
72842c23cb7cSEd Maste static uint64_t
72852c23cb7cSEd Maste _decode_lsb(uint8_t **data, int bytes_to_read)
72862c23cb7cSEd Maste {
72872c23cb7cSEd Maste 	uint64_t ret;
72882c23cb7cSEd Maste 	uint8_t *src;
72892c23cb7cSEd Maste 
72902c23cb7cSEd Maste 	src = *data;
72912c23cb7cSEd Maste 
72922c23cb7cSEd Maste 	ret = 0;
72932c23cb7cSEd Maste 	switch (bytes_to_read) {
72942c23cb7cSEd Maste 	case 8:
72952c23cb7cSEd Maste 		ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
72962c23cb7cSEd Maste 		ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
72972c23cb7cSEd Maste 	case 4:
72982c23cb7cSEd Maste 		ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
72992c23cb7cSEd Maste 	case 2:
73002c23cb7cSEd Maste 		ret |= ((uint64_t) src[1]) << 8;
73012c23cb7cSEd Maste 	case 1:
73022c23cb7cSEd Maste 		ret |= src[0];
73032c23cb7cSEd Maste 		break;
73042c23cb7cSEd Maste 	default:
73052c23cb7cSEd Maste 		return (0);
73062c23cb7cSEd Maste 	}
73072c23cb7cSEd Maste 
73082c23cb7cSEd Maste 	*data += bytes_to_read;
73092c23cb7cSEd Maste 
73102c23cb7cSEd Maste 	return (ret);
73112c23cb7cSEd Maste }
73122c23cb7cSEd Maste 
73132c23cb7cSEd Maste static uint64_t
73142c23cb7cSEd Maste _decode_msb(uint8_t **data, int bytes_to_read)
73152c23cb7cSEd Maste {
73162c23cb7cSEd Maste 	uint64_t ret;
73172c23cb7cSEd Maste 	uint8_t *src;
73182c23cb7cSEd Maste 
73192c23cb7cSEd Maste 	src = *data;
73202c23cb7cSEd Maste 
73212c23cb7cSEd Maste 	ret = 0;
73222c23cb7cSEd Maste 	switch (bytes_to_read) {
73232c23cb7cSEd Maste 	case 1:
73242c23cb7cSEd Maste 		ret = src[0];
73252c23cb7cSEd Maste 		break;
73262c23cb7cSEd Maste 	case 2:
73272c23cb7cSEd Maste 		ret = src[1] | ((uint64_t) src[0]) << 8;
73282c23cb7cSEd Maste 		break;
73292c23cb7cSEd Maste 	case 4:
73302c23cb7cSEd Maste 		ret = src[3] | ((uint64_t) src[2]) << 8;
73312c23cb7cSEd Maste 		ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24;
73322c23cb7cSEd Maste 		break;
73332c23cb7cSEd Maste 	case 8:
73342c23cb7cSEd Maste 		ret = src[7] | ((uint64_t) src[6]) << 8;
73352c23cb7cSEd Maste 		ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24;
73362c23cb7cSEd Maste 		ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40;
73372c23cb7cSEd Maste 		ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56;
73382c23cb7cSEd Maste 		break;
73392c23cb7cSEd Maste 	default:
73402c23cb7cSEd Maste 		return (0);
73412c23cb7cSEd Maste 		break;
73422c23cb7cSEd Maste 	}
73432c23cb7cSEd Maste 
73442c23cb7cSEd Maste 	*data += bytes_to_read;
73452c23cb7cSEd Maste 
73462c23cb7cSEd Maste 	return (ret);
73472c23cb7cSEd Maste }
73482c23cb7cSEd Maste 
73492c23cb7cSEd Maste static int64_t
73502c23cb7cSEd Maste _decode_sleb128(uint8_t **dp)
73512c23cb7cSEd Maste {
73522c23cb7cSEd Maste 	int64_t ret = 0;
73532c23cb7cSEd Maste 	uint8_t b;
73542c23cb7cSEd Maste 	int shift = 0;
73552c23cb7cSEd Maste 
73562c23cb7cSEd Maste 	uint8_t *src = *dp;
73572c23cb7cSEd Maste 
73582c23cb7cSEd Maste 	do {
73592c23cb7cSEd Maste 		b = *src++;
73602c23cb7cSEd Maste 		ret |= ((b & 0x7f) << shift);
73612c23cb7cSEd Maste 		shift += 7;
73622c23cb7cSEd Maste 	} while ((b & 0x80) != 0);
73632c23cb7cSEd Maste 
73642c23cb7cSEd Maste 	if (shift < 32 && (b & 0x40) != 0)
73652c23cb7cSEd Maste 		ret |= (-1 << shift);
73662c23cb7cSEd Maste 
73672c23cb7cSEd Maste 	*dp = src;
73682c23cb7cSEd Maste 
73692c23cb7cSEd Maste 	return (ret);
73702c23cb7cSEd Maste }
73712c23cb7cSEd Maste 
73722c23cb7cSEd Maste static uint64_t
73732c23cb7cSEd Maste _decode_uleb128(uint8_t **dp)
73742c23cb7cSEd Maste {
73752c23cb7cSEd Maste 	uint64_t ret = 0;
73762c23cb7cSEd Maste 	uint8_t b;
73772c23cb7cSEd Maste 	int shift = 0;
73782c23cb7cSEd Maste 
73792c23cb7cSEd Maste 	uint8_t *src = *dp;
73802c23cb7cSEd Maste 
73812c23cb7cSEd Maste 	do {
73822c23cb7cSEd Maste 		b = *src++;
73832c23cb7cSEd Maste 		ret |= ((b & 0x7f) << shift);
73842c23cb7cSEd Maste 		shift += 7;
73852c23cb7cSEd Maste 	} while ((b & 0x80) != 0);
73862c23cb7cSEd Maste 
73872c23cb7cSEd Maste 	*dp = src;
73882c23cb7cSEd Maste 
73892c23cb7cSEd Maste 	return (ret);
73902c23cb7cSEd Maste }
73912c23cb7cSEd Maste 
73922c23cb7cSEd Maste static void
73932c23cb7cSEd Maste readelf_version(void)
73942c23cb7cSEd Maste {
73952c23cb7cSEd Maste 	(void) printf("%s (%s)\n", ELFTC_GETPROGNAME(),
73962c23cb7cSEd Maste 	    elftc_version());
73972c23cb7cSEd Maste 	exit(EXIT_SUCCESS);
73982c23cb7cSEd Maste }
73992c23cb7cSEd Maste 
74002c23cb7cSEd Maste #define	USAGE_MESSAGE	"\
74012c23cb7cSEd Maste Usage: %s [options] file...\n\
74022c23cb7cSEd Maste   Display information about ELF objects and ar(1) archives.\n\n\
74032c23cb7cSEd Maste   Options:\n\
74042c23cb7cSEd Maste   -a | --all               Equivalent to specifying options '-dhIlrsASV'.\n\
74052c23cb7cSEd Maste   -c | --archive-index     Print the archive symbol table for archives.\n\
74062c23cb7cSEd Maste   -d | --dynamic           Print the contents of SHT_DYNAMIC sections.\n\
74072c23cb7cSEd Maste   -e | --headers           Print all headers in the object.\n\
74083ef90571SEd Maste   -g | --section-groups    Print the contents of the section groups.\n\
74092c23cb7cSEd Maste   -h | --file-header       Print the file header for the object.\n\
74102c23cb7cSEd Maste   -l | --program-headers   Print the PHDR table for the object.\n\
74112c23cb7cSEd Maste   -n | --notes             Print the contents of SHT_NOTE sections.\n\
74122c23cb7cSEd Maste   -p INDEX | --string-dump=INDEX\n\
74132c23cb7cSEd Maste                            Print the contents of section at index INDEX.\n\
74142c23cb7cSEd Maste   -r | --relocs            Print relocation information.\n\
74152c23cb7cSEd Maste   -s | --syms | --symbols  Print symbol tables.\n\
74162c23cb7cSEd Maste   -t | --section-details   Print additional information about sections.\n\
74172c23cb7cSEd Maste   -v | --version           Print a version identifier and exit.\n\
74182c23cb7cSEd Maste   -x INDEX | --hex-dump=INDEX\n\
74192c23cb7cSEd Maste                            Display contents of a section as hexadecimal.\n\
74202c23cb7cSEd Maste   -A | --arch-specific     (accepted, but ignored)\n\
74212c23cb7cSEd Maste   -D | --use-dynamic       Print the symbol table specified by the DT_SYMTAB\n\
74222c23cb7cSEd Maste                            entry in the \".dynamic\" section.\n\
74232c23cb7cSEd Maste   -H | --help              Print a help message.\n\
74242c23cb7cSEd Maste   -I | --histogram         Print information on bucket list lengths for \n\
74252c23cb7cSEd Maste                            hash sections.\n\
74262c23cb7cSEd Maste   -N | --full-section-name (accepted, but ignored)\n\
74272c23cb7cSEd Maste   -S | --sections | --section-headers\n\
74282c23cb7cSEd Maste                            Print information about section headers.\n\
74292c23cb7cSEd Maste   -V | --version-info      Print symbol versoning information.\n\
74302c23cb7cSEd Maste   -W | --wide              Print information without wrapping long lines.\n"
74312c23cb7cSEd Maste 
74322c23cb7cSEd Maste 
74332c23cb7cSEd Maste static void
74342c23cb7cSEd Maste readelf_usage(void)
74352c23cb7cSEd Maste {
74362c23cb7cSEd Maste 	fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());
74372c23cb7cSEd Maste 	exit(EXIT_FAILURE);
74382c23cb7cSEd Maste }
74392c23cb7cSEd Maste 
74402c23cb7cSEd Maste int
74412c23cb7cSEd Maste main(int argc, char **argv)
74422c23cb7cSEd Maste {
74432c23cb7cSEd Maste 	struct readelf	*re, re_storage;
74442c23cb7cSEd Maste 	unsigned long	 si;
74452c23cb7cSEd Maste 	int		 opt, i;
74462c23cb7cSEd Maste 	char		*ep;
74472c23cb7cSEd Maste 
74482c23cb7cSEd Maste 	re = &re_storage;
74492c23cb7cSEd Maste 	memset(re, 0, sizeof(*re));
74502c23cb7cSEd Maste 	STAILQ_INIT(&re->v_dumpop);
74512c23cb7cSEd Maste 
74522c23cb7cSEd Maste 	while ((opt = getopt_long(argc, argv, "AacDdegHhIi:lNnp:rSstuVvWw::x:",
74532c23cb7cSEd Maste 	    longopts, NULL)) != -1) {
74542c23cb7cSEd Maste 		switch(opt) {
74552c23cb7cSEd Maste 		case '?':
74562c23cb7cSEd Maste 			readelf_usage();
74572c23cb7cSEd Maste 			break;
74582c23cb7cSEd Maste 		case 'A':
74592c23cb7cSEd Maste 			re->options |= RE_AA;
74602c23cb7cSEd Maste 			break;
74612c23cb7cSEd Maste 		case 'a':
74623ef90571SEd Maste 			re->options |= RE_AA | RE_D | RE_G | RE_H | RE_II |
74633ef90571SEd Maste 			    RE_L | RE_R | RE_SS | RE_S | RE_VV;
74642c23cb7cSEd Maste 			break;
74652c23cb7cSEd Maste 		case 'c':
74662c23cb7cSEd Maste 			re->options |= RE_C;
74672c23cb7cSEd Maste 			break;
74682c23cb7cSEd Maste 		case 'D':
74692c23cb7cSEd Maste 			re->options |= RE_DD;
74702c23cb7cSEd Maste 			break;
74712c23cb7cSEd Maste 		case 'd':
74722c23cb7cSEd Maste 			re->options |= RE_D;
74732c23cb7cSEd Maste 			break;
74742c23cb7cSEd Maste 		case 'e':
74752c23cb7cSEd Maste 			re->options |= RE_H | RE_L | RE_SS;
74762c23cb7cSEd Maste 			break;
74772c23cb7cSEd Maste 		case 'g':
74782c23cb7cSEd Maste 			re->options |= RE_G;
74792c23cb7cSEd Maste 			break;
74802c23cb7cSEd Maste 		case 'H':
74812c23cb7cSEd Maste 			readelf_usage();
74822c23cb7cSEd Maste 			break;
74832c23cb7cSEd Maste 		case 'h':
74842c23cb7cSEd Maste 			re->options |= RE_H;
74852c23cb7cSEd Maste 			break;
74862c23cb7cSEd Maste 		case 'I':
74872c23cb7cSEd Maste 			re->options |= RE_II;
74882c23cb7cSEd Maste 			break;
74892c23cb7cSEd Maste 		case 'i':
74902c23cb7cSEd Maste 			/* Not implemented yet. */
74912c23cb7cSEd Maste 			break;
74922c23cb7cSEd Maste 		case 'l':
74932c23cb7cSEd Maste 			re->options |= RE_L;
74942c23cb7cSEd Maste 			break;
74952c23cb7cSEd Maste 		case 'N':
74962c23cb7cSEd Maste 			re->options |= RE_NN;
74972c23cb7cSEd Maste 			break;
74982c23cb7cSEd Maste 		case 'n':
74992c23cb7cSEd Maste 			re->options |= RE_N;
75002c23cb7cSEd Maste 			break;
75012c23cb7cSEd Maste 		case 'p':
75022c23cb7cSEd Maste 			re->options |= RE_P;
75032c23cb7cSEd Maste 			si = strtoul(optarg, &ep, 10);
75042c23cb7cSEd Maste 			if (*ep == '\0')
75052c23cb7cSEd Maste 				add_dumpop(re, (size_t) si, NULL, STR_DUMP,
75062c23cb7cSEd Maste 				    DUMP_BY_INDEX);
75072c23cb7cSEd Maste 			else
75082c23cb7cSEd Maste 				add_dumpop(re, 0, optarg, STR_DUMP,
75092c23cb7cSEd Maste 				    DUMP_BY_NAME);
75102c23cb7cSEd Maste 			break;
75112c23cb7cSEd Maste 		case 'r':
75122c23cb7cSEd Maste 			re->options |= RE_R;
75132c23cb7cSEd Maste 			break;
75142c23cb7cSEd Maste 		case 'S':
75152c23cb7cSEd Maste 			re->options |= RE_SS;
75162c23cb7cSEd Maste 			break;
75172c23cb7cSEd Maste 		case 's':
75182c23cb7cSEd Maste 			re->options |= RE_S;
75192c23cb7cSEd Maste 			break;
75202c23cb7cSEd Maste 		case 't':
75212c23cb7cSEd Maste 			re->options |= RE_T;
75222c23cb7cSEd Maste 			break;
75232c23cb7cSEd Maste 		case 'u':
75242c23cb7cSEd Maste 			re->options |= RE_U;
75252c23cb7cSEd Maste 			break;
75262c23cb7cSEd Maste 		case 'V':
75272c23cb7cSEd Maste 			re->options |= RE_VV;
75282c23cb7cSEd Maste 			break;
75292c23cb7cSEd Maste 		case 'v':
75302c23cb7cSEd Maste 			readelf_version();
75312c23cb7cSEd Maste 			break;
75322c23cb7cSEd Maste 		case 'W':
75332c23cb7cSEd Maste 			re->options |= RE_WW;
75342c23cb7cSEd Maste 			break;
75352c23cb7cSEd Maste 		case 'w':
75362c23cb7cSEd Maste 			re->options |= RE_W;
75372c23cb7cSEd Maste 			parse_dwarf_op_short(re, optarg);
75382c23cb7cSEd Maste 			break;
75392c23cb7cSEd Maste 		case 'x':
75402c23cb7cSEd Maste 			re->options |= RE_X;
75412c23cb7cSEd Maste 			si = strtoul(optarg, &ep, 10);
75422c23cb7cSEd Maste 			if (*ep == '\0')
75432c23cb7cSEd Maste 				add_dumpop(re, (size_t) si, NULL, HEX_DUMP,
75442c23cb7cSEd Maste 				    DUMP_BY_INDEX);
75452c23cb7cSEd Maste 			else
75462c23cb7cSEd Maste 				add_dumpop(re, 0, optarg, HEX_DUMP,
75472c23cb7cSEd Maste 				    DUMP_BY_NAME);
75482c23cb7cSEd Maste 			break;
75492c23cb7cSEd Maste 		case OPTION_DEBUG_DUMP:
75502c23cb7cSEd Maste 			re->options |= RE_W;
75512c23cb7cSEd Maste 			parse_dwarf_op_long(re, optarg);
75522c23cb7cSEd Maste 		}
75532c23cb7cSEd Maste 	}
75542c23cb7cSEd Maste 
75552c23cb7cSEd Maste 	argv += optind;
75562c23cb7cSEd Maste 	argc -= optind;
75572c23cb7cSEd Maste 
75582c23cb7cSEd Maste 	if (argc == 0 || re->options == 0)
75592c23cb7cSEd Maste 		readelf_usage();
75602c23cb7cSEd Maste 
75612c23cb7cSEd Maste 	if (argc > 1)
75622c23cb7cSEd Maste 		re->flags |= DISPLAY_FILENAME;
75632c23cb7cSEd Maste 
75642c23cb7cSEd Maste 	if (elf_version(EV_CURRENT) == EV_NONE)
75652c23cb7cSEd Maste 		errx(EXIT_FAILURE, "ELF library initialization failed: %s",
75662c23cb7cSEd Maste 		    elf_errmsg(-1));
75672c23cb7cSEd Maste 
7568b00fe64fSEd Maste 	for (i = 0; i < argc; i++) {
75692c23cb7cSEd Maste 		re->filename = argv[i];
75702c23cb7cSEd Maste 		dump_object(re);
75712c23cb7cSEd Maste 	}
75722c23cb7cSEd Maste 
75732c23cb7cSEd Maste 	exit(EXIT_SUCCESS);
75742c23cb7cSEd Maste }
7575