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>
29802c2095SMark Johnston 
302c23cb7cSEd Maste #include <ar.h>
3171edbbfdSEd Maste #include <assert.h>
32802c2095SMark Johnston #include <capsicum_helpers.h>
332c23cb7cSEd Maste #include <ctype.h>
342c23cb7cSEd Maste #include <dwarf.h>
352c23cb7cSEd Maste #include <err.h>
362c23cb7cSEd Maste #include <fcntl.h>
372c23cb7cSEd Maste #include <gelf.h>
382c23cb7cSEd Maste #include <getopt.h>
392c23cb7cSEd Maste #include <libdwarf.h>
402c23cb7cSEd Maste #include <libelftc.h>
412c23cb7cSEd Maste #include <libgen.h>
422c23cb7cSEd Maste #include <stdarg.h>
43e128bd0fSEd Maste #include <stdbool.h>
443dc58d9cSEd Maste #include <stdint.h>
452c23cb7cSEd Maste #include <stdio.h>
462c23cb7cSEd Maste #include <stdlib.h>
472c23cb7cSEd Maste #include <string.h>
482c23cb7cSEd Maste #include <time.h>
492c23cb7cSEd Maste #include <unistd.h>
50e128bd0fSEd Maste #include <zlib.h>
512c23cb7cSEd Maste 
52802c2095SMark Johnston #include <libcasper.h>
53802c2095SMark Johnston #include <casper/cap_fileargs.h>
54802c2095SMark Johnston 
552c23cb7cSEd Maste #include "_elftc.h"
562c23cb7cSEd Maste 
57d003e0d7SEd Maste ELFTC_VCSID("$Id: readelf.c 3769 2019-06-29 15:15:02Z emaste $");
58839529caSEd Maste 
59839529caSEd Maste /* Backwards compatability for older FreeBSD releases. */
60839529caSEd Maste #ifndef	STB_GNU_UNIQUE
61839529caSEd Maste #define	STB_GNU_UNIQUE 10
62839529caSEd Maste #endif
63839529caSEd Maste #ifndef	STT_SPARC_REGISTER
64839529caSEd Maste #define	STT_SPARC_REGISTER 13
65839529caSEd Maste #endif
66839529caSEd Maste 
672c23cb7cSEd Maste 
682c23cb7cSEd Maste /*
692c23cb7cSEd Maste  * readelf(1) options.
702c23cb7cSEd Maste  */
712c23cb7cSEd Maste #define	RE_AA	0x00000001
722c23cb7cSEd Maste #define	RE_C	0x00000002
732c23cb7cSEd Maste #define	RE_DD	0x00000004
742c23cb7cSEd Maste #define	RE_D	0x00000008
752c23cb7cSEd Maste #define	RE_G	0x00000010
762c23cb7cSEd Maste #define	RE_H	0x00000020
772c23cb7cSEd Maste #define	RE_II	0x00000040
782c23cb7cSEd Maste #define	RE_I	0x00000080
792c23cb7cSEd Maste #define	RE_L	0x00000100
802c23cb7cSEd Maste #define	RE_NN	0x00000200
812c23cb7cSEd Maste #define	RE_N	0x00000400
822c23cb7cSEd Maste #define	RE_P	0x00000800
832c23cb7cSEd Maste #define	RE_R	0x00001000
842c23cb7cSEd Maste #define	RE_SS	0x00002000
852c23cb7cSEd Maste #define	RE_S	0x00004000
862c23cb7cSEd Maste #define	RE_T	0x00008000
872c23cb7cSEd Maste #define	RE_U	0x00010000
882c23cb7cSEd Maste #define	RE_VV	0x00020000
892c23cb7cSEd Maste #define	RE_WW	0x00040000
902c23cb7cSEd Maste #define	RE_W	0x00080000
912c23cb7cSEd Maste #define	RE_X	0x00100000
92e128bd0fSEd Maste #define	RE_Z	0x00200000
932c23cb7cSEd Maste 
942c23cb7cSEd Maste /*
952c23cb7cSEd Maste  * dwarf dump options.
962c23cb7cSEd Maste  */
972c23cb7cSEd Maste #define	DW_A	0x00000001
982c23cb7cSEd Maste #define	DW_FF	0x00000002
992c23cb7cSEd Maste #define	DW_F	0x00000004
1002c23cb7cSEd Maste #define	DW_I	0x00000008
1012c23cb7cSEd Maste #define	DW_LL	0x00000010
1022c23cb7cSEd Maste #define	DW_L	0x00000020
1032c23cb7cSEd Maste #define	DW_M	0x00000040
1042c23cb7cSEd Maste #define	DW_O	0x00000080
1052c23cb7cSEd Maste #define	DW_P	0x00000100
1062c23cb7cSEd Maste #define	DW_RR	0x00000200
1072c23cb7cSEd Maste #define	DW_R	0x00000400
1082c23cb7cSEd Maste #define	DW_S	0x00000800
1092c23cb7cSEd Maste 
1102c23cb7cSEd Maste #define	DW_DEFAULT_OPTIONS (DW_A | DW_F | DW_I | DW_L | DW_O | DW_P | \
1112c23cb7cSEd Maste 	    DW_R | DW_RR | DW_S)
1122c23cb7cSEd Maste 
1132c23cb7cSEd Maste /*
1142c23cb7cSEd Maste  * readelf(1) run control flags.
1152c23cb7cSEd Maste  */
1162c23cb7cSEd Maste #define	DISPLAY_FILENAME	0x0001
1172c23cb7cSEd Maste 
1182c23cb7cSEd Maste /*
1192c23cb7cSEd Maste  * Internal data structure for sections.
1202c23cb7cSEd Maste  */
1212c23cb7cSEd Maste struct section {
1222c23cb7cSEd Maste 	const char	*name;		/* section name */
1232c23cb7cSEd Maste 	Elf_Scn		*scn;		/* section scn */
1242c23cb7cSEd Maste 	uint64_t	 off;		/* section offset */
1252c23cb7cSEd Maste 	uint64_t	 sz;		/* section size */
1262c23cb7cSEd Maste 	uint64_t	 entsize;	/* section entsize */
1272c23cb7cSEd Maste 	uint64_t	 align;		/* section alignment */
1282c23cb7cSEd Maste 	uint64_t	 type;		/* section type */
1292c23cb7cSEd Maste 	uint64_t	 flags;		/* section flags */
1302c23cb7cSEd Maste 	uint64_t	 addr;		/* section virtual addr */
1312c23cb7cSEd Maste 	uint32_t	 link;		/* section link ndx */
1322c23cb7cSEd Maste 	uint32_t	 info;		/* section info ndx */
1332c23cb7cSEd Maste };
1342c23cb7cSEd Maste 
1352c23cb7cSEd Maste struct dumpop {
1362c23cb7cSEd Maste 	union {
1372c23cb7cSEd Maste 		size_t si;		/* section index */
1382c23cb7cSEd Maste 		const char *sn;		/* section name */
1392c23cb7cSEd Maste 	} u;
1402c23cb7cSEd Maste 	enum {
1412c23cb7cSEd Maste 		DUMP_BY_INDEX = 0,
1422c23cb7cSEd Maste 		DUMP_BY_NAME
1432c23cb7cSEd Maste 	} type;				/* dump type */
1442c23cb7cSEd Maste #define HEX_DUMP	0x0001
1452c23cb7cSEd Maste #define STR_DUMP	0x0002
1462c23cb7cSEd Maste 	int op;				/* dump operation */
1472c23cb7cSEd Maste 	STAILQ_ENTRY(dumpop) dumpop_list;
1482c23cb7cSEd Maste };
1492c23cb7cSEd Maste 
1502c23cb7cSEd Maste struct symver {
1512c23cb7cSEd Maste 	const char *name;
1522c23cb7cSEd Maste 	int type;
1532c23cb7cSEd Maste };
1542c23cb7cSEd Maste 
1552c23cb7cSEd Maste /*
1562c23cb7cSEd Maste  * Structure encapsulates the global data for readelf(1).
1572c23cb7cSEd Maste  */
1582c23cb7cSEd Maste struct readelf {
1592c23cb7cSEd Maste 	const char	 *filename;	/* current processing file. */
1602c23cb7cSEd Maste 	int		  options;	/* command line options. */
1612c23cb7cSEd Maste 	int		  flags;	/* run control flags. */
1622c23cb7cSEd Maste 	int		  dop;		/* dwarf dump options. */
1632c23cb7cSEd Maste 	Elf		 *elf;		/* underlying ELF descriptor. */
1642c23cb7cSEd Maste 	Elf		 *ar;		/* archive ELF descriptor. */
1652c23cb7cSEd Maste 	Dwarf_Debug	  dbg;		/* DWARF handle. */
166cf781b2eSEd Maste 	Dwarf_Half	  cu_psize;	/* DWARF CU pointer size. */
167cf781b2eSEd Maste 	Dwarf_Half	  cu_osize;	/* DWARF CU offset size. */
168cf781b2eSEd Maste 	Dwarf_Half	  cu_ver;	/* DWARF CU version. */
1692c23cb7cSEd Maste 	GElf_Ehdr	  ehdr;		/* ELF header. */
1702c23cb7cSEd Maste 	int		  ec;		/* ELF class. */
1712c23cb7cSEd Maste 	size_t		  shnum;	/* #sections. */
1722c23cb7cSEd Maste 	struct section	 *vd_s;		/* Verdef section. */
1732c23cb7cSEd Maste 	struct section	 *vn_s;		/* Verneed section. */
1742c23cb7cSEd Maste 	struct section	 *vs_s;		/* Versym section. */
1752c23cb7cSEd Maste 	uint16_t	 *vs;		/* Versym array. */
1762c23cb7cSEd Maste 	int		  vs_sz;	/* Versym array size. */
1772c23cb7cSEd Maste 	struct symver	 *ver;		/* Version array. */
1782c23cb7cSEd Maste 	int		  ver_sz;	/* Size of version array. */
1792c23cb7cSEd Maste 	struct section	 *sl;		/* list of sections. */
1802c23cb7cSEd Maste 	STAILQ_HEAD(, dumpop) v_dumpop; /* list of dump ops. */
1812c23cb7cSEd Maste 	uint64_t	(*dw_read)(Elf_Data *, uint64_t *, int);
1822c23cb7cSEd Maste 	uint64_t	(*dw_decode)(uint8_t **, int);
1832c23cb7cSEd Maste };
1842c23cb7cSEd Maste 
1852c23cb7cSEd Maste enum options
1862c23cb7cSEd Maste {
1872c23cb7cSEd Maste 	OPTION_DEBUG_DUMP
1882c23cb7cSEd Maste };
1892c23cb7cSEd Maste 
1902c23cb7cSEd Maste static struct option longopts[] = {
1912c23cb7cSEd Maste 	{"all", no_argument, NULL, 'a'},
1922c23cb7cSEd Maste 	{"arch-specific", no_argument, NULL, 'A'},
1932c23cb7cSEd Maste 	{"archive-index", no_argument, NULL, 'c'},
1942c23cb7cSEd Maste 	{"debug-dump", optional_argument, NULL, OPTION_DEBUG_DUMP},
195e128bd0fSEd Maste 	{"decompress", no_argument, 0, 'z'},
1962c23cb7cSEd Maste 	{"dynamic", no_argument, NULL, 'd'},
1972c23cb7cSEd Maste 	{"file-header", no_argument, NULL, 'h'},
1982c23cb7cSEd Maste 	{"full-section-name", no_argument, NULL, 'N'},
1992c23cb7cSEd Maste 	{"headers", no_argument, NULL, 'e'},
2002c23cb7cSEd Maste 	{"help", no_argument, 0, 'H'},
2012c23cb7cSEd Maste 	{"hex-dump", required_argument, NULL, 'x'},
2022c23cb7cSEd Maste 	{"histogram", no_argument, NULL, 'I'},
2032c23cb7cSEd Maste 	{"notes", no_argument, NULL, 'n'},
2042c23cb7cSEd Maste 	{"program-headers", no_argument, NULL, 'l'},
2052c23cb7cSEd Maste 	{"relocs", no_argument, NULL, 'r'},
2062c23cb7cSEd Maste 	{"sections", no_argument, NULL, 'S'},
2072c23cb7cSEd Maste 	{"section-headers", no_argument, NULL, 'S'},
2082c23cb7cSEd Maste 	{"section-groups", no_argument, NULL, 'g'},
2092c23cb7cSEd Maste 	{"section-details", no_argument, NULL, 't'},
2102c23cb7cSEd Maste 	{"segments", no_argument, NULL, 'l'},
2112c23cb7cSEd Maste 	{"string-dump", required_argument, NULL, 'p'},
2122c23cb7cSEd Maste 	{"symbols", no_argument, NULL, 's'},
2132c23cb7cSEd Maste 	{"syms", no_argument, NULL, 's'},
2142c23cb7cSEd Maste 	{"unwind", no_argument, NULL, 'u'},
2152c23cb7cSEd Maste 	{"use-dynamic", no_argument, NULL, 'D'},
2162c23cb7cSEd Maste 	{"version-info", no_argument, 0, 'V'},
2172c23cb7cSEd Maste 	{"version", no_argument, 0, 'v'},
2182c23cb7cSEd Maste 	{"wide", no_argument, 0, 'W'},
2192c23cb7cSEd Maste 	{NULL, 0, NULL, 0}
2202c23cb7cSEd Maste };
2212c23cb7cSEd Maste 
2222c23cb7cSEd Maste struct eflags_desc {
2232c23cb7cSEd Maste 	uint64_t flag;
2242c23cb7cSEd Maste 	const char *desc;
2252c23cb7cSEd Maste };
2262c23cb7cSEd Maste 
227d003e0d7SEd Maste struct flag_desc {
2282c23cb7cSEd Maste 	uint64_t flag;
2292c23cb7cSEd Maste 	const char *desc;
2302c23cb7cSEd Maste };
2312c23cb7cSEd Maste 
232aacbf3fbSAndrew Turner struct flag_desc_list {
233aacbf3fbSAndrew Turner 	uint32_t type;
234aacbf3fbSAndrew Turner 	const char *desc_str;
235aacbf3fbSAndrew Turner 	struct flag_desc *desc;
236aacbf3fbSAndrew Turner };
237aacbf3fbSAndrew Turner 
238d003e0d7SEd Maste struct mips_option {
2396b2779a0SEd Maste 	uint64_t flag;
2406b2779a0SEd Maste 	const char *desc;
2416b2779a0SEd Maste };
2426b2779a0SEd Maste 
2439817bae9SEd Maste struct loc_at {
2449817bae9SEd Maste 	Dwarf_Attribute la_at;
2459817bae9SEd Maste 	Dwarf_Unsigned la_off;
2469817bae9SEd Maste 	Dwarf_Unsigned la_lowpc;
2479817bae9SEd Maste 	Dwarf_Half la_cu_psize;
2489817bae9SEd Maste 	Dwarf_Half la_cu_osize;
2499817bae9SEd Maste 	Dwarf_Half la_cu_ver;
2509817bae9SEd Maste };
2519817bae9SEd Maste 
2522c23cb7cSEd Maste static void add_dumpop(struct readelf *re, size_t si, const char *sn, int op,
2532c23cb7cSEd Maste     int t);
2542c23cb7cSEd Maste static const char *aeabi_adv_simd_arch(uint64_t simd);
2552c23cb7cSEd Maste static const char *aeabi_align_needed(uint64_t an);
2562c23cb7cSEd Maste static const char *aeabi_align_preserved(uint64_t ap);
2572c23cb7cSEd Maste static const char *aeabi_arm_isa(uint64_t ai);
2582c23cb7cSEd Maste static const char *aeabi_cpu_arch(uint64_t arch);
2592c23cb7cSEd Maste static const char *aeabi_cpu_arch_profile(uint64_t pf);
2602c23cb7cSEd Maste static const char *aeabi_div(uint64_t du);
2612c23cb7cSEd Maste static const char *aeabi_enum_size(uint64_t es);
2622c23cb7cSEd Maste static const char *aeabi_fp_16bit_format(uint64_t fp16);
2632c23cb7cSEd Maste static const char *aeabi_fp_arch(uint64_t fp);
2642c23cb7cSEd Maste static const char *aeabi_fp_denormal(uint64_t fd);
2652c23cb7cSEd Maste static const char *aeabi_fp_exceptions(uint64_t fe);
2662c23cb7cSEd Maste static const char *aeabi_fp_hpext(uint64_t fh);
2672c23cb7cSEd Maste static const char *aeabi_fp_number_model(uint64_t fn);
2682c23cb7cSEd Maste static const char *aeabi_fp_optm_goal(uint64_t fog);
2692c23cb7cSEd Maste static const char *aeabi_fp_rounding(uint64_t fr);
2702c23cb7cSEd Maste static const char *aeabi_hardfp(uint64_t hfp);
2712c23cb7cSEd Maste static const char *aeabi_mpext(uint64_t mp);
2722c23cb7cSEd Maste static const char *aeabi_optm_goal(uint64_t og);
2732c23cb7cSEd Maste static const char *aeabi_pcs_config(uint64_t pcs);
2742c23cb7cSEd Maste static const char *aeabi_pcs_got(uint64_t got);
2752c23cb7cSEd Maste static const char *aeabi_pcs_r9(uint64_t r9);
2762c23cb7cSEd Maste static const char *aeabi_pcs_ro(uint64_t ro);
2772c23cb7cSEd Maste static const char *aeabi_pcs_rw(uint64_t rw);
2782c23cb7cSEd Maste static const char *aeabi_pcs_wchar_t(uint64_t wt);
2792c23cb7cSEd Maste static const char *aeabi_t2ee(uint64_t t2ee);
2802c23cb7cSEd Maste static const char *aeabi_thumb_isa(uint64_t ti);
2812c23cb7cSEd Maste static const char *aeabi_fp_user_exceptions(uint64_t fu);
2822c23cb7cSEd Maste static const char *aeabi_unaligned_access(uint64_t ua);
2832c23cb7cSEd Maste static const char *aeabi_vfp_args(uint64_t va);
2842c23cb7cSEd Maste static const char *aeabi_virtual(uint64_t vt);
2852c23cb7cSEd Maste static const char *aeabi_wmmx_arch(uint64_t wmmx);
2862c23cb7cSEd Maste static const char *aeabi_wmmx_args(uint64_t wa);
2872c23cb7cSEd Maste static const char *elf_class(unsigned int class);
2882c23cb7cSEd Maste static const char *elf_endian(unsigned int endian);
2892c23cb7cSEd Maste static const char *elf_machine(unsigned int mach);
2902c23cb7cSEd Maste static const char *elf_osabi(unsigned int abi);
2912c23cb7cSEd Maste static const char *elf_type(unsigned int type);
2922c23cb7cSEd Maste static const char *elf_ver(unsigned int ver);
2932c23cb7cSEd Maste static const char *dt_type(unsigned int mach, unsigned int dtype);
294ea444392SEd Maste static bool dump_ar(struct readelf *re, int);
2952c23cb7cSEd Maste static void dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe);
2962c23cb7cSEd Maste static void dump_attributes(struct readelf *re);
29795fd7f26SEd Maste static uint8_t *dump_compatibility_tag(uint8_t *p, uint8_t *pe);
2982c23cb7cSEd Maste static void dump_dwarf(struct readelf *re);
299cf781b2eSEd Maste static void dump_dwarf_abbrev(struct readelf *re);
300cf781b2eSEd Maste static void dump_dwarf_aranges(struct readelf *re);
301cf781b2eSEd Maste static void dump_dwarf_block(struct readelf *re, uint8_t *b,
302cf781b2eSEd Maste     Dwarf_Unsigned len);
303cf781b2eSEd Maste static void dump_dwarf_die(struct readelf *re, Dwarf_Die die, int level);
304cf781b2eSEd Maste static void dump_dwarf_frame(struct readelf *re, int alt);
305cf781b2eSEd Maste static void dump_dwarf_frame_inst(struct readelf *re, Dwarf_Cie cie,
306cf781b2eSEd Maste     uint8_t *insts, Dwarf_Unsigned len, Dwarf_Unsigned caf, Dwarf_Signed daf,
307cf781b2eSEd Maste     Dwarf_Addr pc, Dwarf_Debug dbg);
308cf781b2eSEd Maste static int dump_dwarf_frame_regtable(struct readelf *re, Dwarf_Fde fde,
309cf781b2eSEd Maste     Dwarf_Addr pc, Dwarf_Unsigned func_len, Dwarf_Half cie_ra);
310cf781b2eSEd Maste static void dump_dwarf_frame_section(struct readelf *re, struct section *s,
311cf781b2eSEd Maste     int alt);
312cf781b2eSEd Maste static void dump_dwarf_info(struct readelf *re, Dwarf_Bool is_info);
313cf781b2eSEd Maste static void dump_dwarf_macinfo(struct readelf *re);
314cf781b2eSEd Maste static void dump_dwarf_line(struct readelf *re);
315cf781b2eSEd Maste static void dump_dwarf_line_decoded(struct readelf *re);
316cf781b2eSEd Maste static void dump_dwarf_loc(struct readelf *re, Dwarf_Loc *lr);
317cf781b2eSEd Maste static void dump_dwarf_loclist(struct readelf *re);
318cf781b2eSEd Maste static void dump_dwarf_pubnames(struct readelf *re);
319cf781b2eSEd Maste static void dump_dwarf_ranges(struct readelf *re);
320cf781b2eSEd Maste static void dump_dwarf_ranges_foreach(struct readelf *re, Dwarf_Die die,
321cf781b2eSEd Maste     Dwarf_Addr base);
322cf781b2eSEd Maste static void dump_dwarf_str(struct readelf *re);
3232c23cb7cSEd Maste static void dump_eflags(struct readelf *re, uint64_t e_flags);
324ea444392SEd Maste static bool dump_elf(struct readelf *re);
3256b2779a0SEd Maste static void dump_flags(struct flag_desc *fd, uint64_t flags);
3262c23cb7cSEd Maste static void dump_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab);
3272c23cb7cSEd Maste static void dump_dynamic(struct readelf *re);
3282c23cb7cSEd Maste static void dump_liblist(struct readelf *re);
329e9f3f88aSEd Maste static void dump_mips_abiflags(struct readelf *re, struct section *s);
3302c23cb7cSEd Maste static void dump_mips_attributes(struct readelf *re, uint8_t *p, uint8_t *pe);
3312c23cb7cSEd Maste static void dump_mips_odk_reginfo(struct readelf *re, uint8_t *p, size_t sz);
3322c23cb7cSEd Maste static void dump_mips_options(struct readelf *re, struct section *s);
3332c23cb7cSEd Maste static void dump_mips_option_flags(const char *name, struct mips_option *opt,
3342c23cb7cSEd Maste     uint64_t info);
3352c23cb7cSEd Maste static void dump_mips_reginfo(struct readelf *re, struct section *s);
3362c23cb7cSEd Maste static void dump_mips_specific_info(struct readelf *re);
3372c23cb7cSEd Maste static void dump_notes(struct readelf *re);
3382c23cb7cSEd Maste static void dump_notes_content(struct readelf *re, const char *buf, size_t sz,
3392c23cb7cSEd Maste     off_t off);
34014a345d9SEd Maste static void dump_notes_data(struct readelf *re, const char *name,
34114a345d9SEd Maste     uint32_t type, const char *buf, size_t sz);
3422c23cb7cSEd Maste static void dump_svr4_hash(struct section *s);
3432c23cb7cSEd Maste static void dump_svr4_hash64(struct readelf *re, struct section *s);
3442c23cb7cSEd Maste static void dump_gnu_hash(struct readelf *re, struct section *s);
34514a345d9SEd Maste static void dump_gnu_property_type_0(struct readelf *re, const char *buf,
34614a345d9SEd Maste     size_t sz);
3472c23cb7cSEd Maste static void dump_hash(struct readelf *re);
3482c23cb7cSEd Maste static void dump_phdr(struct readelf *re);
3492c23cb7cSEd Maste static void dump_ppc_attributes(uint8_t *p, uint8_t *pe);
3503ef90571SEd Maste static void dump_section_groups(struct readelf *re);
3512c23cb7cSEd Maste static void dump_symtab(struct readelf *re, int i);
3522c23cb7cSEd Maste static void dump_symtabs(struct readelf *re);
35395fd7f26SEd Maste static uint8_t *dump_unknown_tag(uint64_t tag, uint8_t *p, uint8_t *pe);
3542c23cb7cSEd Maste static void dump_ver(struct readelf *re);
3552c23cb7cSEd Maste static void dump_verdef(struct readelf *re, int dump);
3562c23cb7cSEd Maste static void dump_verneed(struct readelf *re, int dump);
3572c23cb7cSEd Maste static void dump_versym(struct readelf *re);
358cf781b2eSEd Maste static const char *dwarf_reg(unsigned int mach, unsigned int reg);
359cf781b2eSEd Maste static const char *dwarf_regname(struct readelf *re, unsigned int num);
360cf781b2eSEd Maste static struct dumpop *find_dumpop(struct readelf *re, size_t si,
361cf781b2eSEd Maste     const char *sn, int op, int t);
36271edbbfdSEd Maste static int get_ent_count(struct section *s, int *ent_count);
363e9f3f88aSEd Maste static int get_mips_register_size(uint8_t flag);
364cf781b2eSEd Maste static char *get_regoff_str(struct readelf *re, Dwarf_Half reg,
365cf781b2eSEd Maste     Dwarf_Addr off);
3662c23cb7cSEd Maste static const char *get_string(struct readelf *re, int strtab, size_t off);
3672c23cb7cSEd Maste static const char *get_symbol_name(struct readelf *re, int symtab, int i);
3682c23cb7cSEd Maste static uint64_t get_symbol_value(struct readelf *re, int symtab, int i);
3692c23cb7cSEd Maste static void load_sections(struct readelf *re);
3709817bae9SEd Maste static int loc_at_comparator(const void *la1, const void *la2);
3712c23cb7cSEd Maste static const char *mips_abi_fp(uint64_t fp);
37202b08c90SEd Maste static const char *note_type(const char *note_name, unsigned int et,
3732c23cb7cSEd Maste     unsigned int nt);
37402b08c90SEd Maste static const char *note_type_freebsd(unsigned int nt);
37502b08c90SEd Maste static const char *note_type_freebsd_core(unsigned int nt);
376ca457394SEd Maste static const char *note_type_go(unsigned int nt);
37702b08c90SEd Maste static const char *note_type_gnu(unsigned int nt);
378ca457394SEd Maste static const char *note_type_linux_core(unsigned int nt);
37902b08c90SEd Maste static const char *note_type_netbsd(unsigned int nt);
38002b08c90SEd Maste static const char *note_type_openbsd(unsigned int nt);
38102b08c90SEd Maste static const char *note_type_unknown(unsigned int nt);
382895f86f1SEd Maste static const char *note_type_xen(unsigned int nt);
3832c23cb7cSEd Maste static const char *option_kind(uint8_t kind);
38420136ffcSEd Maste static const char *phdr_type(unsigned int mach, unsigned int ptype);
3852c23cb7cSEd Maste static const char *ppc_abi_fp(uint64_t fp);
3862c23cb7cSEd Maste static const char *ppc_abi_vector(uint64_t vec);
387839529caSEd Maste static void readelf_usage(int status);
3882c23cb7cSEd Maste static void readelf_version(void);
389cf781b2eSEd Maste static void search_loclist_at(struct readelf *re, Dwarf_Die die,
3909817bae9SEd Maste     Dwarf_Unsigned lowpc, struct loc_at **la_list,
391cb12a690SEd Maste     size_t *la_list_len, size_t *la_list_cap);
3922c23cb7cSEd Maste static void search_ver(struct readelf *re);
3932c23cb7cSEd Maste static const char *section_type(unsigned int mach, unsigned int stype);
394cf781b2eSEd Maste static void set_cu_context(struct readelf *re, Dwarf_Half psize,
395cf781b2eSEd Maste     Dwarf_Half osize, Dwarf_Half ver);
3962c23cb7cSEd Maste static const char *st_bind(unsigned int sbind);
3972c23cb7cSEd Maste static const char *st_shndx(unsigned int shndx);
398b6b6f9ccSEd Maste static const char *st_type(unsigned int mach, unsigned int os,
399b6b6f9ccSEd Maste     unsigned int stype);
4002c23cb7cSEd Maste static const char *st_vis(unsigned int svis);
4012c23cb7cSEd Maste static const char *top_tag(unsigned int tag);
4022c23cb7cSEd Maste static void unload_sections(struct readelf *re);
4032c23cb7cSEd Maste static uint64_t _read_lsb(Elf_Data *d, uint64_t *offsetp,
4042c23cb7cSEd Maste     int bytes_to_read);
4052c23cb7cSEd Maste static uint64_t _read_msb(Elf_Data *d, uint64_t *offsetp,
4062c23cb7cSEd Maste     int bytes_to_read);
4072c23cb7cSEd Maste static uint64_t _decode_lsb(uint8_t **data, int bytes_to_read);
4082c23cb7cSEd Maste static uint64_t _decode_msb(uint8_t **data, int bytes_to_read);
40995fd7f26SEd Maste static int64_t _decode_sleb128(uint8_t **dp, uint8_t *dpe);
41095fd7f26SEd Maste static uint64_t _decode_uleb128(uint8_t **dp, uint8_t *dpe);
4112c23cb7cSEd Maste 
4122c23cb7cSEd Maste static struct eflags_desc arm_eflags_desc[] = {
4132c23cb7cSEd Maste 	{EF_ARM_RELEXEC, "relocatable executable"},
4142c23cb7cSEd Maste 	{EF_ARM_HASENTRY, "has entry point"},
4152c23cb7cSEd Maste 	{EF_ARM_SYMSARESORTED, "sorted symbol tables"},
4162c23cb7cSEd Maste 	{EF_ARM_DYNSYMSUSESEGIDX, "dynamic symbols use segment index"},
4172c23cb7cSEd Maste 	{EF_ARM_MAPSYMSFIRST, "mapping symbols precede others"},
4182c23cb7cSEd Maste 	{EF_ARM_BE8, "BE8"},
4192c23cb7cSEd Maste 	{EF_ARM_LE8, "LE8"},
4202c23cb7cSEd Maste 	{EF_ARM_INTERWORK, "interworking enabled"},
4212c23cb7cSEd Maste 	{EF_ARM_APCS_26, "uses APCS/26"},
4222c23cb7cSEd Maste 	{EF_ARM_APCS_FLOAT, "uses APCS/float"},
4232c23cb7cSEd Maste 	{EF_ARM_PIC, "position independent"},
4242c23cb7cSEd Maste 	{EF_ARM_ALIGN8, "8 bit structure alignment"},
4252c23cb7cSEd Maste 	{EF_ARM_NEW_ABI, "uses new ABI"},
4262c23cb7cSEd Maste 	{EF_ARM_OLD_ABI, "uses old ABI"},
4272c23cb7cSEd Maste 	{EF_ARM_SOFT_FLOAT, "software FP"},
4282c23cb7cSEd Maste 	{EF_ARM_VFP_FLOAT, "VFP"},
4292c23cb7cSEd Maste 	{EF_ARM_MAVERICK_FLOAT, "Maverick FP"},
4302c23cb7cSEd Maste 	{0, NULL}
4312c23cb7cSEd Maste };
4322c23cb7cSEd Maste 
4332c23cb7cSEd Maste static struct eflags_desc mips_eflags_desc[] = {
4342c23cb7cSEd Maste 	{EF_MIPS_NOREORDER, "noreorder"},
4352c23cb7cSEd Maste 	{EF_MIPS_PIC, "pic"},
4362c23cb7cSEd Maste 	{EF_MIPS_CPIC, "cpic"},
4372c23cb7cSEd Maste 	{EF_MIPS_UCODE, "ugen_reserved"},
4382c23cb7cSEd Maste 	{EF_MIPS_ABI2, "abi2"},
4392c23cb7cSEd Maste 	{EF_MIPS_OPTIONS_FIRST, "odk first"},
4402c23cb7cSEd Maste 	{EF_MIPS_ARCH_ASE_MDMX, "mdmx"},
4412c23cb7cSEd Maste 	{EF_MIPS_ARCH_ASE_M16, "mips16"},
4422c23cb7cSEd Maste 	{0, NULL}
4432c23cb7cSEd Maste };
4442c23cb7cSEd Maste 
4452c23cb7cSEd Maste static struct eflags_desc powerpc_eflags_desc[] = {
4462c23cb7cSEd Maste 	{EF_PPC_EMB, "emb"},
4472c23cb7cSEd Maste 	{EF_PPC_RELOCATABLE, "relocatable"},
4482c23cb7cSEd Maste 	{EF_PPC_RELOCATABLE_LIB, "relocatable-lib"},
4492c23cb7cSEd Maste 	{0, NULL}
4502c23cb7cSEd Maste };
4512c23cb7cSEd Maste 
452b0084180SMitchell Horne static struct eflags_desc riscv_eflags_desc[] = {
453b0084180SMitchell Horne 	{EF_RISCV_RVC, "RVC"},
454b0084180SMitchell Horne 	{EF_RISCV_RVE, "RVE"},
455b0084180SMitchell Horne 	{EF_RISCV_TSO, "TSO"},
456b0084180SMitchell Horne 	{0, NULL}
457b0084180SMitchell Horne };
458b0084180SMitchell Horne 
4592c23cb7cSEd Maste static struct eflags_desc sparc_eflags_desc[] = {
4602c23cb7cSEd Maste 	{EF_SPARC_32PLUS, "v8+"},
4612c23cb7cSEd Maste 	{EF_SPARC_SUN_US1, "ultrasparcI"},
4622c23cb7cSEd Maste 	{EF_SPARC_HAL_R1, "halr1"},
4632c23cb7cSEd Maste 	{EF_SPARC_SUN_US3, "ultrasparcIII"},
4642c23cb7cSEd Maste 	{0, NULL}
4652c23cb7cSEd Maste };
4662c23cb7cSEd Maste 
4672c23cb7cSEd Maste static const char *
elf_osabi(unsigned int abi)4682c23cb7cSEd Maste elf_osabi(unsigned int abi)
4692c23cb7cSEd Maste {
4702c23cb7cSEd Maste 	static char s_abi[32];
4712c23cb7cSEd Maste 
4722c23cb7cSEd Maste 	switch(abi) {
473453b09caSEd Maste 	case ELFOSABI_NONE: return "NONE";
474473c31f1SEd Maste 	case ELFOSABI_HPUX: return "HPUX";
4752c23cb7cSEd Maste 	case ELFOSABI_NETBSD: return "NetBSD";
4762c23cb7cSEd Maste 	case ELFOSABI_GNU: return "GNU";
4772c23cb7cSEd Maste 	case ELFOSABI_HURD: return "HURD";
4782c23cb7cSEd Maste 	case ELFOSABI_86OPEN: return "86OPEN";
4792c23cb7cSEd Maste 	case ELFOSABI_SOLARIS: return "Solaris";
4802c23cb7cSEd Maste 	case ELFOSABI_AIX: return "AIX";
4812c23cb7cSEd Maste 	case ELFOSABI_IRIX: return "IRIX";
4822c23cb7cSEd Maste 	case ELFOSABI_FREEBSD: return "FreeBSD";
4832c23cb7cSEd Maste 	case ELFOSABI_TRU64: return "TRU64";
4842c23cb7cSEd Maste 	case ELFOSABI_MODESTO: return "MODESTO";
4852c23cb7cSEd Maste 	case ELFOSABI_OPENBSD: return "OpenBSD";
4862c23cb7cSEd Maste 	case ELFOSABI_OPENVMS: return "OpenVMS";
4872c23cb7cSEd Maste 	case ELFOSABI_NSK: return "NSK";
488b6b6f9ccSEd Maste 	case ELFOSABI_CLOUDABI: return "CloudABI";
489b6d812d2SEd Maste 	case ELFOSABI_ARM_AEABI: return "ARM EABI";
4902c23cb7cSEd Maste 	case ELFOSABI_ARM: return "ARM";
4912c23cb7cSEd Maste 	case ELFOSABI_STANDALONE: return "StandAlone";
4922c23cb7cSEd Maste 	default:
4932c23cb7cSEd Maste 		snprintf(s_abi, sizeof(s_abi), "<unknown: %#x>", abi);
4942c23cb7cSEd Maste 		return (s_abi);
4952c23cb7cSEd Maste 	}
4962c23cb7cSEd Maste };
4972c23cb7cSEd Maste 
4982c23cb7cSEd Maste static const char *
elf_machine(unsigned int mach)4992c23cb7cSEd Maste elf_machine(unsigned int mach)
5002c23cb7cSEd Maste {
5012c23cb7cSEd Maste 	static char s_mach[32];
5022c23cb7cSEd Maste 
5032c23cb7cSEd Maste 	switch (mach) {
5042c23cb7cSEd Maste 	case EM_NONE: return "Unknown machine";
5052c23cb7cSEd Maste 	case EM_M32: return "AT&T WE32100";
5062c23cb7cSEd Maste 	case EM_SPARC: return "Sun SPARC";
5072c23cb7cSEd Maste 	case EM_386: return "Intel i386";
5082c23cb7cSEd Maste 	case EM_68K: return "Motorola 68000";
5093ef90571SEd Maste 	case EM_IAMCU: return "Intel MCU";
5102c23cb7cSEd Maste 	case EM_88K: return "Motorola 88000";
5112c23cb7cSEd Maste 	case EM_860: return "Intel i860";
5122c23cb7cSEd Maste 	case EM_MIPS: return "MIPS R3000 Big-Endian only";
5132c23cb7cSEd Maste 	case EM_S370: return "IBM System/370";
5142c23cb7cSEd Maste 	case EM_MIPS_RS3_LE: return "MIPS R3000 Little-Endian";
5152c23cb7cSEd Maste 	case EM_PARISC: return "HP PA-RISC";
5162c23cb7cSEd Maste 	case EM_VPP500: return "Fujitsu VPP500";
5172c23cb7cSEd Maste 	case EM_SPARC32PLUS: return "SPARC v8plus";
5182c23cb7cSEd Maste 	case EM_960: return "Intel 80960";
5192c23cb7cSEd Maste 	case EM_PPC: return "PowerPC 32-bit";
5202c23cb7cSEd Maste 	case EM_PPC64: return "PowerPC 64-bit";
5212c23cb7cSEd Maste 	case EM_S390: return "IBM System/390";
5222c23cb7cSEd Maste 	case EM_V800: return "NEC V800";
5232c23cb7cSEd Maste 	case EM_FR20: return "Fujitsu FR20";
5242c23cb7cSEd Maste 	case EM_RH32: return "TRW RH-32";
5252c23cb7cSEd Maste 	case EM_RCE: return "Motorola RCE";
5262c23cb7cSEd Maste 	case EM_ARM: return "ARM";
5272c23cb7cSEd Maste 	case EM_SH: return "Hitachi SH";
5282c23cb7cSEd Maste 	case EM_SPARCV9: return "SPARC v9 64-bit";
5292c23cb7cSEd Maste 	case EM_TRICORE: return "Siemens TriCore embedded processor";
5302c23cb7cSEd Maste 	case EM_ARC: return "Argonaut RISC Core";
5312c23cb7cSEd Maste 	case EM_H8_300: return "Hitachi H8/300";
5322c23cb7cSEd Maste 	case EM_H8_300H: return "Hitachi H8/300H";
5332c23cb7cSEd Maste 	case EM_H8S: return "Hitachi H8S";
5342c23cb7cSEd Maste 	case EM_H8_500: return "Hitachi H8/500";
5352c23cb7cSEd Maste 	case EM_IA_64: return "Intel IA-64 Processor";
5362c23cb7cSEd Maste 	case EM_MIPS_X: return "Stanford MIPS-X";
5372c23cb7cSEd Maste 	case EM_COLDFIRE: return "Motorola ColdFire";
5382c23cb7cSEd Maste 	case EM_68HC12: return "Motorola M68HC12";
5392c23cb7cSEd Maste 	case EM_MMA: return "Fujitsu MMA";
5402c23cb7cSEd Maste 	case EM_PCP: return "Siemens PCP";
5412c23cb7cSEd Maste 	case EM_NCPU: return "Sony nCPU";
5422c23cb7cSEd Maste 	case EM_NDR1: return "Denso NDR1 microprocessor";
5432c23cb7cSEd Maste 	case EM_STARCORE: return "Motorola Star*Core processor";
5442c23cb7cSEd Maste 	case EM_ME16: return "Toyota ME16 processor";
5452c23cb7cSEd Maste 	case EM_ST100: return "STMicroelectronics ST100 processor";
5462c23cb7cSEd Maste 	case EM_TINYJ: return "Advanced Logic Corp. TinyJ processor";
5472c23cb7cSEd Maste 	case EM_X86_64: return "Advanced Micro Devices x86-64";
5482c23cb7cSEd Maste 	case EM_PDSP: return "Sony DSP Processor";
5492c23cb7cSEd Maste 	case EM_FX66: return "Siemens FX66 microcontroller";
5502c23cb7cSEd Maste 	case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 microcontroller";
5512c23cb7cSEd Maste 	case EM_ST7: return "STmicroelectronics ST7 8-bit microcontroller";
5522c23cb7cSEd Maste 	case EM_68HC16: return "Motorola MC68HC16 microcontroller";
5532c23cb7cSEd Maste 	case EM_68HC11: return "Motorola MC68HC11 microcontroller";
5542c23cb7cSEd Maste 	case EM_68HC08: return "Motorola MC68HC08 microcontroller";
5552c23cb7cSEd Maste 	case EM_68HC05: return "Motorola MC68HC05 microcontroller";
5562c23cb7cSEd Maste 	case EM_SVX: return "Silicon Graphics SVx";
5572c23cb7cSEd Maste 	case EM_ST19: return "STMicroelectronics ST19 8-bit mc";
5582c23cb7cSEd Maste 	case EM_VAX: return "Digital VAX";
5592c23cb7cSEd Maste 	case EM_CRIS: return "Axis Communications 32-bit embedded processor";
5602c23cb7cSEd Maste 	case EM_JAVELIN: return "Infineon Tech. 32bit embedded processor";
5612c23cb7cSEd Maste 	case EM_FIREPATH: return "Element 14 64-bit DSP Processor";
5622c23cb7cSEd Maste 	case EM_ZSP: return "LSI Logic 16-bit DSP Processor";
5632c23cb7cSEd Maste 	case EM_MMIX: return "Donald Knuth's educational 64-bit proc";
5642c23cb7cSEd Maste 	case EM_HUANY: return "Harvard University MI object files";
5652c23cb7cSEd Maste 	case EM_PRISM: return "SiTera Prism";
5662c23cb7cSEd Maste 	case EM_AVR: return "Atmel AVR 8-bit microcontroller";
5672c23cb7cSEd Maste 	case EM_FR30: return "Fujitsu FR30";
5682c23cb7cSEd Maste 	case EM_D10V: return "Mitsubishi D10V";
5692c23cb7cSEd Maste 	case EM_D30V: return "Mitsubishi D30V";
5702c23cb7cSEd Maste 	case EM_V850: return "NEC v850";
5712c23cb7cSEd Maste 	case EM_M32R: return "Mitsubishi M32R";
5722c23cb7cSEd Maste 	case EM_MN10300: return "Matsushita MN10300";
5732c23cb7cSEd Maste 	case EM_MN10200: return "Matsushita MN10200";
5742c23cb7cSEd Maste 	case EM_PJ: return "picoJava";
5752c23cb7cSEd Maste 	case EM_OPENRISC: return "OpenRISC 32-bit embedded processor";
5762c23cb7cSEd Maste 	case EM_ARC_A5: return "ARC Cores Tangent-A5";
5772c23cb7cSEd Maste 	case EM_XTENSA: return "Tensilica Xtensa Architecture";
5782c23cb7cSEd Maste 	case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
5792c23cb7cSEd Maste 	case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
5802c23cb7cSEd Maste 	case EM_NS32K: return "National Semiconductor 32000 series";
5812c23cb7cSEd Maste 	case EM_TPC: return "Tenor Network TPC processor";
5822c23cb7cSEd Maste 	case EM_SNP1K: return "Trebia SNP 1000 processor";
5832c23cb7cSEd Maste 	case EM_ST200: return "STMicroelectronics ST200 microcontroller";
5842c23cb7cSEd Maste 	case EM_IP2K: return "Ubicom IP2xxx microcontroller family";
5852c23cb7cSEd Maste 	case EM_MAX: return "MAX Processor";
5862c23cb7cSEd Maste 	case EM_CR: return "National Semiconductor CompactRISC microprocessor";
5872c23cb7cSEd Maste 	case EM_F2MC16: return "Fujitsu F2MC16";
5882c23cb7cSEd Maste 	case EM_MSP430: return "TI embedded microcontroller msp430";
5892c23cb7cSEd Maste 	case EM_BLACKFIN: return "Analog Devices Blackfin (DSP) processor";
5902c23cb7cSEd Maste 	case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
5912c23cb7cSEd Maste 	case EM_SEP: return "Sharp embedded microprocessor";
5922c23cb7cSEd Maste 	case EM_ARCA: return "Arca RISC Microprocessor";
5932c23cb7cSEd Maste 	case EM_UNICORE: return "Microprocessor series from PKU-Unity Ltd";
594b3f26809SEd Maste 	case EM_AARCH64: return "AArch64";
595119b7592SEd Maste 	case EM_RISCV: return "RISC-V";
5962c23cb7cSEd Maste 	default:
5972c23cb7cSEd Maste 		snprintf(s_mach, sizeof(s_mach), "<unknown: %#x>", mach);
5982c23cb7cSEd Maste 		return (s_mach);
5992c23cb7cSEd Maste 	}
6002c23cb7cSEd Maste 
6012c23cb7cSEd Maste }
6022c23cb7cSEd Maste 
6032c23cb7cSEd Maste static const char *
elf_class(unsigned int class)6042c23cb7cSEd Maste elf_class(unsigned int class)
6052c23cb7cSEd Maste {
6062c23cb7cSEd Maste 	static char s_class[32];
6072c23cb7cSEd Maste 
6082c23cb7cSEd Maste 	switch (class) {
6092c23cb7cSEd Maste 	case ELFCLASSNONE: return "none";
6102c23cb7cSEd Maste 	case ELFCLASS32: return "ELF32";
6112c23cb7cSEd Maste 	case ELFCLASS64: return "ELF64";
6122c23cb7cSEd Maste 	default:
6132c23cb7cSEd Maste 		snprintf(s_class, sizeof(s_class), "<unknown: %#x>", class);
6142c23cb7cSEd Maste 		return (s_class);
6152c23cb7cSEd Maste 	}
6162c23cb7cSEd Maste }
6172c23cb7cSEd Maste 
6182c23cb7cSEd Maste static const char *
elf_endian(unsigned int endian)6192c23cb7cSEd Maste elf_endian(unsigned int endian)
6202c23cb7cSEd Maste {
6212c23cb7cSEd Maste 	static char s_endian[32];
6222c23cb7cSEd Maste 
6232c23cb7cSEd Maste 	switch (endian) {
6242c23cb7cSEd Maste 	case ELFDATANONE: return "none";
6252c23cb7cSEd Maste 	case ELFDATA2LSB: return "2's complement, little endian";
6262c23cb7cSEd Maste 	case ELFDATA2MSB: return "2's complement, big endian";
6272c23cb7cSEd Maste 	default:
6282c23cb7cSEd Maste 		snprintf(s_endian, sizeof(s_endian), "<unknown: %#x>", endian);
6292c23cb7cSEd Maste 		return (s_endian);
6302c23cb7cSEd Maste 	}
6312c23cb7cSEd Maste }
6322c23cb7cSEd Maste 
6332c23cb7cSEd Maste static const char *
elf_type(unsigned int type)6342c23cb7cSEd Maste elf_type(unsigned int type)
6352c23cb7cSEd Maste {
6362c23cb7cSEd Maste 	static char s_type[32];
6372c23cb7cSEd Maste 
6382c23cb7cSEd Maste 	switch (type) {
6392c23cb7cSEd Maste 	case ET_NONE: return "NONE (None)";
6402c23cb7cSEd Maste 	case ET_REL: return "REL (Relocatable file)";
6412c23cb7cSEd Maste 	case ET_EXEC: return "EXEC (Executable file)";
6422c23cb7cSEd Maste 	case ET_DYN: return "DYN (Shared object file)";
6432c23cb7cSEd Maste 	case ET_CORE: return "CORE (Core file)";
6442c23cb7cSEd Maste 	default:
6452c23cb7cSEd Maste 		if (type >= ET_LOPROC)
6462c23cb7cSEd Maste 			snprintf(s_type, sizeof(s_type), "<proc: %#x>", type);
6472c23cb7cSEd Maste 		else if (type >= ET_LOOS && type <= ET_HIOS)
6482c23cb7cSEd Maste 			snprintf(s_type, sizeof(s_type), "<os: %#x>", type);
6492c23cb7cSEd Maste 		else
6502c23cb7cSEd Maste 			snprintf(s_type, sizeof(s_type), "<unknown: %#x>",
6512c23cb7cSEd Maste 			    type);
6522c23cb7cSEd Maste 		return (s_type);
6532c23cb7cSEd Maste 	}
6542c23cb7cSEd Maste }
6552c23cb7cSEd Maste 
6562c23cb7cSEd Maste static const char *
elf_ver(unsigned int ver)6572c23cb7cSEd Maste elf_ver(unsigned int ver)
6582c23cb7cSEd Maste {
6592c23cb7cSEd Maste 	static char s_ver[32];
6602c23cb7cSEd Maste 
6612c23cb7cSEd Maste 	switch (ver) {
6622c23cb7cSEd Maste 	case EV_CURRENT: return "(current)";
6632c23cb7cSEd Maste 	case EV_NONE: return "(none)";
6642c23cb7cSEd Maste 	default:
6652c23cb7cSEd Maste 		snprintf(s_ver, sizeof(s_ver), "<unknown: %#x>",
6662c23cb7cSEd Maste 		    ver);
6672c23cb7cSEd Maste 		return (s_ver);
6682c23cb7cSEd Maste 	}
6692c23cb7cSEd Maste }
6702c23cb7cSEd Maste 
6712c23cb7cSEd Maste static const char *
phdr_type(unsigned int mach,unsigned int ptype)67220136ffcSEd Maste phdr_type(unsigned int mach, unsigned int ptype)
6732c23cb7cSEd Maste {
6742c23cb7cSEd Maste 	static char s_ptype[32];
6752c23cb7cSEd Maste 
67620136ffcSEd Maste 	if (ptype >= PT_LOPROC && ptype <= PT_HIPROC) {
67720136ffcSEd Maste 		switch (mach) {
67820136ffcSEd Maste 		case EM_ARM:
67920136ffcSEd Maste 			switch (ptype) {
68020136ffcSEd Maste 			case PT_ARM_ARCHEXT: return "ARM_ARCHEXT";
68120136ffcSEd Maste 			case PT_ARM_EXIDX: return "ARM_EXIDX";
68220136ffcSEd Maste 			}
68320136ffcSEd Maste 			break;
68420136ffcSEd Maste 		}
68520136ffcSEd Maste 		snprintf(s_ptype, sizeof(s_ptype), "LOPROC+%#x",
68620136ffcSEd Maste 		    ptype - PT_LOPROC);
68720136ffcSEd Maste 		return (s_ptype);
68820136ffcSEd Maste 	}
68920136ffcSEd Maste 
6902c23cb7cSEd Maste 	switch (ptype) {
6912c23cb7cSEd Maste 	case PT_NULL: return "NULL";
6922c23cb7cSEd Maste 	case PT_LOAD: return "LOAD";
6932c23cb7cSEd Maste 	case PT_DYNAMIC: return "DYNAMIC";
6942c23cb7cSEd Maste 	case PT_INTERP: return "INTERP";
6952c23cb7cSEd Maste 	case PT_NOTE: return "NOTE";
6962c23cb7cSEd Maste 	case PT_SHLIB: return "SHLIB";
6972c23cb7cSEd Maste 	case PT_PHDR: return "PHDR";
6982c23cb7cSEd Maste 	case PT_TLS: return "TLS";
6992c23cb7cSEd Maste 	case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
7002c23cb7cSEd Maste 	case PT_GNU_STACK: return "GNU_STACK";
7012c23cb7cSEd Maste 	case PT_GNU_RELRO: return "GNU_RELRO";
702ca307559SChristian S.J. Peron 	case PT_OPENBSD_RANDOMIZE: return "OPENBSD_RANDOMIZE";
703ca307559SChristian S.J. Peron 	case PT_OPENBSD_WXNEEDED: return "OPENBSD_WXNEEDED";
704ca307559SChristian S.J. Peron 	case PT_OPENBSD_BOOTDATA: return "OPENBSD_BOOTDATA";
7052c23cb7cSEd Maste 	default:
70620136ffcSEd Maste 		if (ptype >= PT_LOOS && ptype <= PT_HIOS)
7072c23cb7cSEd Maste 			snprintf(s_ptype, sizeof(s_ptype), "LOOS+%#x",
7082c23cb7cSEd Maste 			    ptype - PT_LOOS);
7092c23cb7cSEd Maste 		else
7102c23cb7cSEd Maste 			snprintf(s_ptype, sizeof(s_ptype), "<unknown: %#x>",
7112c23cb7cSEd Maste 			    ptype);
7122c23cb7cSEd Maste 		return (s_ptype);
7132c23cb7cSEd Maste 	}
7142c23cb7cSEd Maste }
7152c23cb7cSEd Maste 
7162c23cb7cSEd Maste static const char *
section_type(unsigned int mach,unsigned int stype)7172c23cb7cSEd Maste section_type(unsigned int mach, unsigned int stype)
7182c23cb7cSEd Maste {
7192c23cb7cSEd Maste 	static char s_stype[32];
7202c23cb7cSEd Maste 
7212c23cb7cSEd Maste 	if (stype >= SHT_LOPROC && stype <= SHT_HIPROC) {
7222c23cb7cSEd Maste 		switch (mach) {
72320136ffcSEd Maste 		case EM_ARM:
72420136ffcSEd Maste 			switch (stype) {
72520136ffcSEd Maste 			case SHT_ARM_EXIDX: return "ARM_EXIDX";
72620136ffcSEd Maste 			case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP";
72720136ffcSEd Maste 			case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES";
72820136ffcSEd Maste 			case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY";
72920136ffcSEd Maste 			case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION";
73020136ffcSEd Maste 			}
73120136ffcSEd Maste 			break;
7322c23cb7cSEd Maste 		case EM_X86_64:
7332c23cb7cSEd Maste 			switch (stype) {
734b6b6f9ccSEd Maste 			case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
7352c23cb7cSEd Maste 			default:
7362c23cb7cSEd Maste 				break;
7372c23cb7cSEd Maste 			}
7382c23cb7cSEd Maste 			break;
7392c23cb7cSEd Maste 		case EM_MIPS:
7402c23cb7cSEd Maste 		case EM_MIPS_RS3_LE:
7412c23cb7cSEd Maste 			switch (stype) {
7422c23cb7cSEd Maste 			case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
7432c23cb7cSEd Maste 			case SHT_MIPS_MSYM: return "MIPS_MSYM";
7442c23cb7cSEd Maste 			case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
7452c23cb7cSEd Maste 			case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
7462c23cb7cSEd Maste 			case SHT_MIPS_UCODE: return "MIPS_UCODE";
7472c23cb7cSEd Maste 			case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
7482c23cb7cSEd Maste 			case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
7492c23cb7cSEd Maste 			case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
7502c23cb7cSEd Maste 			case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
7512c23cb7cSEd Maste 			case SHT_MIPS_RELD: return "MIPS_RELD";
7522c23cb7cSEd Maste 			case SHT_MIPS_IFACE: return "MIPS_IFACE";
7532c23cb7cSEd Maste 			case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
7542c23cb7cSEd Maste 			case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
7552c23cb7cSEd Maste 			case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
7562c23cb7cSEd Maste 			case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
7572c23cb7cSEd Maste 			case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
7582c23cb7cSEd Maste 			case SHT_MIPS_DWARF: return "MIPS_DWARF";
7592c23cb7cSEd Maste 			case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
7602c23cb7cSEd Maste 			case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
7612c23cb7cSEd Maste 			case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
7622c23cb7cSEd Maste 			case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
7632c23cb7cSEd Maste 			case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
7642c23cb7cSEd Maste 			case SHT_MIPS_XLATE: return "MIPS_XLATE";
7652c23cb7cSEd Maste 			case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
7662c23cb7cSEd Maste 			case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
7672c23cb7cSEd Maste 			case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
7682c23cb7cSEd Maste 			case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
7692c23cb7cSEd Maste 			case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
770bee2765cSEd Maste 			case SHT_MIPS_ABIFLAGS: return "MIPS_ABIFLAGS";
7712c23cb7cSEd Maste 			default:
7722c23cb7cSEd Maste 				break;
7732c23cb7cSEd Maste 			}
7742c23cb7cSEd Maste 			break;
7752c23cb7cSEd Maste 		default:
7762c23cb7cSEd Maste 			break;
7772c23cb7cSEd Maste 		}
7782c23cb7cSEd Maste 
7792c23cb7cSEd Maste 		snprintf(s_stype, sizeof(s_stype), "LOPROC+%#x",
7802c23cb7cSEd Maste 		    stype - SHT_LOPROC);
7812c23cb7cSEd Maste 		return (s_stype);
7822c23cb7cSEd Maste 	}
7832c23cb7cSEd Maste 
7842c23cb7cSEd Maste 	switch (stype) {
7852c23cb7cSEd Maste 	case SHT_NULL: return "NULL";
7862c23cb7cSEd Maste 	case SHT_PROGBITS: return "PROGBITS";
7872c23cb7cSEd Maste 	case SHT_SYMTAB: return "SYMTAB";
7882c23cb7cSEd Maste 	case SHT_STRTAB: return "STRTAB";
7892c23cb7cSEd Maste 	case SHT_RELA: return "RELA";
7902c23cb7cSEd Maste 	case SHT_HASH: return "HASH";
7912c23cb7cSEd Maste 	case SHT_DYNAMIC: return "DYNAMIC";
7922c23cb7cSEd Maste 	case SHT_NOTE: return "NOTE";
7932c23cb7cSEd Maste 	case SHT_NOBITS: return "NOBITS";
7942c23cb7cSEd Maste 	case SHT_REL: return "REL";
7952c23cb7cSEd Maste 	case SHT_SHLIB: return "SHLIB";
7962c23cb7cSEd Maste 	case SHT_DYNSYM: return "DYNSYM";
7972c23cb7cSEd Maste 	case SHT_INIT_ARRAY: return "INIT_ARRAY";
7982c23cb7cSEd Maste 	case SHT_FINI_ARRAY: return "FINI_ARRAY";
7992c23cb7cSEd Maste 	case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
8002c23cb7cSEd Maste 	case SHT_GROUP: return "GROUP";
8012c23cb7cSEd Maste 	case SHT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
8022c23cb7cSEd Maste 	case SHT_SUNW_dof: return "SUNW_dof";
8032c23cb7cSEd Maste 	case SHT_SUNW_cap: return "SUNW_cap";
8042c23cb7cSEd Maste 	case SHT_GNU_HASH: return "GNU_HASH";
8052c23cb7cSEd Maste 	case SHT_SUNW_ANNOTATE: return "SUNW_ANNOTATE";
8062c23cb7cSEd Maste 	case SHT_SUNW_DEBUGSTR: return "SUNW_DEBUGSTR";
8072c23cb7cSEd Maste 	case SHT_SUNW_DEBUG: return "SUNW_DEBUG";
8082c23cb7cSEd Maste 	case SHT_SUNW_move: return "SUNW_move";
8092c23cb7cSEd Maste 	case SHT_SUNW_COMDAT: return "SUNW_COMDAT";
8102c23cb7cSEd Maste 	case SHT_SUNW_syminfo: return "SUNW_syminfo";
8112c23cb7cSEd Maste 	case SHT_SUNW_verdef: return "SUNW_verdef";
8122c23cb7cSEd Maste 	case SHT_SUNW_verneed: return "SUNW_verneed";
8132c23cb7cSEd Maste 	case SHT_SUNW_versym: return "SUNW_versym";
8142c23cb7cSEd Maste 	default:
8152c23cb7cSEd Maste 		if (stype >= SHT_LOOS && stype <= SHT_HIOS)
8162c23cb7cSEd Maste 			snprintf(s_stype, sizeof(s_stype), "LOOS+%#x",
8172c23cb7cSEd Maste 			    stype - SHT_LOOS);
8182c23cb7cSEd Maste 		else if (stype >= SHT_LOUSER)
8192c23cb7cSEd Maste 			snprintf(s_stype, sizeof(s_stype), "LOUSER+%#x",
8202c23cb7cSEd Maste 			    stype - SHT_LOUSER);
8212c23cb7cSEd Maste 		else
8222c23cb7cSEd Maste 			snprintf(s_stype, sizeof(s_stype), "<unknown: %#x>",
8232c23cb7cSEd Maste 			    stype);
8242c23cb7cSEd Maste 		return (s_stype);
8252c23cb7cSEd Maste 	}
8262c23cb7cSEd Maste }
8272c23cb7cSEd Maste 
8282c23cb7cSEd Maste static const char *
dt_type(unsigned int mach,unsigned int dtype)8292c23cb7cSEd Maste dt_type(unsigned int mach, unsigned int dtype)
8302c23cb7cSEd Maste {
8312c23cb7cSEd Maste 	static char s_dtype[32];
8322c23cb7cSEd Maste 
833d62ecb4eSEd Maste 	switch (dtype) {
834d62ecb4eSEd Maste 	case DT_NULL: return "NULL";
835d62ecb4eSEd Maste 	case DT_NEEDED: return "NEEDED";
836d62ecb4eSEd Maste 	case DT_PLTRELSZ: return "PLTRELSZ";
837d62ecb4eSEd Maste 	case DT_PLTGOT: return "PLTGOT";
838d62ecb4eSEd Maste 	case DT_HASH: return "HASH";
839d62ecb4eSEd Maste 	case DT_STRTAB: return "STRTAB";
840d62ecb4eSEd Maste 	case DT_SYMTAB: return "SYMTAB";
841d62ecb4eSEd Maste 	case DT_RELA: return "RELA";
842d62ecb4eSEd Maste 	case DT_RELASZ: return "RELASZ";
843d62ecb4eSEd Maste 	case DT_RELAENT: return "RELAENT";
844d62ecb4eSEd Maste 	case DT_STRSZ: return "STRSZ";
845d62ecb4eSEd Maste 	case DT_SYMENT: return "SYMENT";
846d62ecb4eSEd Maste 	case DT_INIT: return "INIT";
847d62ecb4eSEd Maste 	case DT_FINI: return "FINI";
848d62ecb4eSEd Maste 	case DT_SONAME: return "SONAME";
849d62ecb4eSEd Maste 	case DT_RPATH: return "RPATH";
850d62ecb4eSEd Maste 	case DT_SYMBOLIC: return "SYMBOLIC";
851d62ecb4eSEd Maste 	case DT_REL: return "REL";
852d62ecb4eSEd Maste 	case DT_RELSZ: return "RELSZ";
853d62ecb4eSEd Maste 	case DT_RELENT: return "RELENT";
854d62ecb4eSEd Maste 	case DT_PLTREL: return "PLTREL";
855d62ecb4eSEd Maste 	case DT_DEBUG: return "DEBUG";
856d62ecb4eSEd Maste 	case DT_TEXTREL: return "TEXTREL";
857d62ecb4eSEd Maste 	case DT_JMPREL: return "JMPREL";
858d62ecb4eSEd Maste 	case DT_BIND_NOW: return "BIND_NOW";
859d62ecb4eSEd Maste 	case DT_INIT_ARRAY: return "INIT_ARRAY";
860d62ecb4eSEd Maste 	case DT_FINI_ARRAY: return "FINI_ARRAY";
861d62ecb4eSEd Maste 	case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
862d62ecb4eSEd Maste 	case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
863d62ecb4eSEd Maste 	case DT_RUNPATH: return "RUNPATH";
864d62ecb4eSEd Maste 	case DT_FLAGS: return "FLAGS";
865d62ecb4eSEd Maste 	case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
866d62ecb4eSEd Maste 	case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
867d62ecb4eSEd Maste 	case DT_MAXPOSTAGS: return "MAXPOSTAGS";
868d62ecb4eSEd Maste 	case DT_SUNW_AUXILIARY: return "SUNW_AUXILIARY";
869d62ecb4eSEd Maste 	case DT_SUNW_RTLDINF: return "SUNW_RTLDINF";
870d62ecb4eSEd Maste 	case DT_SUNW_FILTER: return "SUNW_FILTER";
871d62ecb4eSEd Maste 	case DT_SUNW_CAP: return "SUNW_CAP";
872715d1396SEd Maste 	case DT_SUNW_ASLR: return "SUNW_ASLR";
873d62ecb4eSEd Maste 	case DT_CHECKSUM: return "CHECKSUM";
874d62ecb4eSEd Maste 	case DT_PLTPADSZ: return "PLTPADSZ";
875d62ecb4eSEd Maste 	case DT_MOVEENT: return "MOVEENT";
876d62ecb4eSEd Maste 	case DT_MOVESZ: return "MOVESZ";
877d62ecb4eSEd Maste 	case DT_FEATURE: return "FEATURE";
878d62ecb4eSEd Maste 	case DT_POSFLAG_1: return "POSFLAG_1";
879d62ecb4eSEd Maste 	case DT_SYMINSZ: return "SYMINSZ";
880d62ecb4eSEd Maste 	case DT_SYMINENT: return "SYMINENT";
881d62ecb4eSEd Maste 	case DT_GNU_HASH: return "GNU_HASH";
882d62ecb4eSEd Maste 	case DT_TLSDESC_PLT: return "DT_TLSDESC_PLT";
883d62ecb4eSEd Maste 	case DT_TLSDESC_GOT: return "DT_TLSDESC_GOT";
884d62ecb4eSEd Maste 	case DT_GNU_CONFLICT: return "GNU_CONFLICT";
885d62ecb4eSEd Maste 	case DT_GNU_LIBLIST: return "GNU_LIBLIST";
886d62ecb4eSEd Maste 	case DT_CONFIG: return "CONFIG";
887d62ecb4eSEd Maste 	case DT_DEPAUDIT: return "DEPAUDIT";
888d62ecb4eSEd Maste 	case DT_AUDIT: return "AUDIT";
889d62ecb4eSEd Maste 	case DT_PLTPAD: return "PLTPAD";
890d62ecb4eSEd Maste 	case DT_MOVETAB: return "MOVETAB";
891d62ecb4eSEd Maste 	case DT_SYMINFO: return "SYMINFO";
892d62ecb4eSEd Maste 	case DT_VERSYM: return "VERSYM";
893d62ecb4eSEd Maste 	case DT_RELACOUNT: return "RELACOUNT";
894d62ecb4eSEd Maste 	case DT_RELCOUNT: return "RELCOUNT";
895d62ecb4eSEd Maste 	case DT_FLAGS_1: return "FLAGS_1";
896d62ecb4eSEd Maste 	case DT_VERDEF: return "VERDEF";
897d62ecb4eSEd Maste 	case DT_VERDEFNUM: return "VERDEFNUM";
898d62ecb4eSEd Maste 	case DT_VERNEED: return "VERNEED";
899d62ecb4eSEd Maste 	case DT_VERNEEDNUM: return "VERNEEDNUM";
900d62ecb4eSEd Maste 	case DT_AUXILIARY: return "AUXILIARY";
901d62ecb4eSEd Maste 	case DT_USED: return "USED";
902d62ecb4eSEd Maste 	case DT_FILTER: return "FILTER";
903d62ecb4eSEd Maste 	case DT_GNU_PRELINKED: return "GNU_PRELINKED";
904d62ecb4eSEd Maste 	case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
905d62ecb4eSEd Maste 	case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
906d62ecb4eSEd Maste 	}
907d62ecb4eSEd Maste 
9082c23cb7cSEd Maste 	if (dtype >= DT_LOPROC && dtype <= DT_HIPROC) {
9092c23cb7cSEd Maste 		switch (mach) {
9102c23cb7cSEd Maste 		case EM_ARM:
9112c23cb7cSEd Maste 			switch (dtype) {
9122c23cb7cSEd Maste 			case DT_ARM_SYMTABSZ:
9132c23cb7cSEd Maste 				return "ARM_SYMTABSZ";
9142c23cb7cSEd Maste 			default:
9152c23cb7cSEd Maste 				break;
9162c23cb7cSEd Maste 			}
9172c23cb7cSEd Maste 			break;
9182c23cb7cSEd Maste 		case EM_MIPS:
9192c23cb7cSEd Maste 		case EM_MIPS_RS3_LE:
9202c23cb7cSEd Maste 			switch (dtype) {
9212c23cb7cSEd Maste 			case DT_MIPS_RLD_VERSION:
9222c23cb7cSEd Maste 				return "MIPS_RLD_VERSION";
9232c23cb7cSEd Maste 			case DT_MIPS_TIME_STAMP:
9242c23cb7cSEd Maste 				return "MIPS_TIME_STAMP";
9252c23cb7cSEd Maste 			case DT_MIPS_ICHECKSUM:
9262c23cb7cSEd Maste 				return "MIPS_ICHECKSUM";
9272c23cb7cSEd Maste 			case DT_MIPS_IVERSION:
9282c23cb7cSEd Maste 				return "MIPS_IVERSION";
9292c23cb7cSEd Maste 			case DT_MIPS_FLAGS:
9302c23cb7cSEd Maste 				return "MIPS_FLAGS";
9312c23cb7cSEd Maste 			case DT_MIPS_BASE_ADDRESS:
9322c23cb7cSEd Maste 				return "MIPS_BASE_ADDRESS";
9332c23cb7cSEd Maste 			case DT_MIPS_CONFLICT:
9342c23cb7cSEd Maste 				return "MIPS_CONFLICT";
9352c23cb7cSEd Maste 			case DT_MIPS_LIBLIST:
9362c23cb7cSEd Maste 				return "MIPS_LIBLIST";
9372c23cb7cSEd Maste 			case DT_MIPS_LOCAL_GOTNO:
9382c23cb7cSEd Maste 				return "MIPS_LOCAL_GOTNO";
9392c23cb7cSEd Maste 			case DT_MIPS_CONFLICTNO:
9402c23cb7cSEd Maste 				return "MIPS_CONFLICTNO";
9412c23cb7cSEd Maste 			case DT_MIPS_LIBLISTNO:
9422c23cb7cSEd Maste 				return "MIPS_LIBLISTNO";
9432c23cb7cSEd Maste 			case DT_MIPS_SYMTABNO:
9442c23cb7cSEd Maste 				return "MIPS_SYMTABNO";
9452c23cb7cSEd Maste 			case DT_MIPS_UNREFEXTNO:
9462c23cb7cSEd Maste 				return "MIPS_UNREFEXTNO";
9472c23cb7cSEd Maste 			case DT_MIPS_GOTSYM:
9482c23cb7cSEd Maste 				return "MIPS_GOTSYM";
9492c23cb7cSEd Maste 			case DT_MIPS_HIPAGENO:
9502c23cb7cSEd Maste 				return "MIPS_HIPAGENO";
9512c23cb7cSEd Maste 			case DT_MIPS_RLD_MAP:
9522c23cb7cSEd Maste 				return "MIPS_RLD_MAP";
9532c23cb7cSEd Maste 			case DT_MIPS_DELTA_CLASS:
9542c23cb7cSEd Maste 				return "MIPS_DELTA_CLASS";
9552c23cb7cSEd Maste 			case DT_MIPS_DELTA_CLASS_NO:
9562c23cb7cSEd Maste 				return "MIPS_DELTA_CLASS_NO";
9572c23cb7cSEd Maste 			case DT_MIPS_DELTA_INSTANCE:
9582c23cb7cSEd Maste 				return "MIPS_DELTA_INSTANCE";
9592c23cb7cSEd Maste 			case DT_MIPS_DELTA_INSTANCE_NO:
9602c23cb7cSEd Maste 				return "MIPS_DELTA_INSTANCE_NO";
9612c23cb7cSEd Maste 			case DT_MIPS_DELTA_RELOC:
9622c23cb7cSEd Maste 				return "MIPS_DELTA_RELOC";
9632c23cb7cSEd Maste 			case DT_MIPS_DELTA_RELOC_NO:
9642c23cb7cSEd Maste 				return "MIPS_DELTA_RELOC_NO";
9652c23cb7cSEd Maste 			case DT_MIPS_DELTA_SYM:
9662c23cb7cSEd Maste 				return "MIPS_DELTA_SYM";
9672c23cb7cSEd Maste 			case DT_MIPS_DELTA_SYM_NO:
9682c23cb7cSEd Maste 				return "MIPS_DELTA_SYM_NO";
9692c23cb7cSEd Maste 			case DT_MIPS_DELTA_CLASSSYM:
9702c23cb7cSEd Maste 				return "MIPS_DELTA_CLASSSYM";
9712c23cb7cSEd Maste 			case DT_MIPS_DELTA_CLASSSYM_NO:
9722c23cb7cSEd Maste 				return "MIPS_DELTA_CLASSSYM_NO";
9732c23cb7cSEd Maste 			case DT_MIPS_CXX_FLAGS:
9742c23cb7cSEd Maste 				return "MIPS_CXX_FLAGS";
9752c23cb7cSEd Maste 			case DT_MIPS_PIXIE_INIT:
9762c23cb7cSEd Maste 				return "MIPS_PIXIE_INIT";
9772c23cb7cSEd Maste 			case DT_MIPS_SYMBOL_LIB:
9782c23cb7cSEd Maste 				return "MIPS_SYMBOL_LIB";
9792c23cb7cSEd Maste 			case DT_MIPS_LOCALPAGE_GOTIDX:
9802c23cb7cSEd Maste 				return "MIPS_LOCALPAGE_GOTIDX";
9812c23cb7cSEd Maste 			case DT_MIPS_LOCAL_GOTIDX:
9822c23cb7cSEd Maste 				return "MIPS_LOCAL_GOTIDX";
9832c23cb7cSEd Maste 			case DT_MIPS_HIDDEN_GOTIDX:
9842c23cb7cSEd Maste 				return "MIPS_HIDDEN_GOTIDX";
9852c23cb7cSEd Maste 			case DT_MIPS_PROTECTED_GOTIDX:
9862c23cb7cSEd Maste 				return "MIPS_PROTECTED_GOTIDX";
9872c23cb7cSEd Maste 			case DT_MIPS_OPTIONS:
9882c23cb7cSEd Maste 				return "MIPS_OPTIONS";
9892c23cb7cSEd Maste 			case DT_MIPS_INTERFACE:
9902c23cb7cSEd Maste 				return "MIPS_INTERFACE";
9912c23cb7cSEd Maste 			case DT_MIPS_DYNSTR_ALIGN:
9922c23cb7cSEd Maste 				return "MIPS_DYNSTR_ALIGN";
9932c23cb7cSEd Maste 			case DT_MIPS_INTERFACE_SIZE:
9942c23cb7cSEd Maste 				return "MIPS_INTERFACE_SIZE";
9952c23cb7cSEd Maste 			case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
9962c23cb7cSEd Maste 				return "MIPS_RLD_TEXT_RESOLVE_ADDR";
9972c23cb7cSEd Maste 			case DT_MIPS_PERF_SUFFIX:
9982c23cb7cSEd Maste 				return "MIPS_PERF_SUFFIX";
9992c23cb7cSEd Maste 			case DT_MIPS_COMPACT_SIZE:
10002c23cb7cSEd Maste 				return "MIPS_COMPACT_SIZE";
10012c23cb7cSEd Maste 			case DT_MIPS_GP_VALUE:
10022c23cb7cSEd Maste 				return "MIPS_GP_VALUE";
10032c23cb7cSEd Maste 			case DT_MIPS_AUX_DYNAMIC:
10042c23cb7cSEd Maste 				return "MIPS_AUX_DYNAMIC";
10052c23cb7cSEd Maste 			case DT_MIPS_PLTGOT:
10062c23cb7cSEd Maste 				return "MIPS_PLTGOT";
10072c23cb7cSEd Maste 			case DT_MIPS_RLD_OBJ_UPDATE:
10082c23cb7cSEd Maste 				return "MIPS_RLD_OBJ_UPDATE";
10092c23cb7cSEd Maste 			case DT_MIPS_RWPLT:
10102c23cb7cSEd Maste 				return "MIPS_RWPLT";
10112c23cb7cSEd Maste 			default:
10122c23cb7cSEd Maste 				break;
10132c23cb7cSEd Maste 			}
10142c23cb7cSEd Maste 			break;
10152c23cb7cSEd Maste 		case EM_SPARC:
10162c23cb7cSEd Maste 		case EM_SPARC32PLUS:
10172c23cb7cSEd Maste 		case EM_SPARCV9:
10182c23cb7cSEd Maste 			switch (dtype) {
10192c23cb7cSEd Maste 			case DT_SPARC_REGISTER:
10202c23cb7cSEd Maste 				return "DT_SPARC_REGISTER";
10212c23cb7cSEd Maste 			default:
10222c23cb7cSEd Maste 				break;
10232c23cb7cSEd Maste 			}
10242c23cb7cSEd Maste 			break;
10252c23cb7cSEd Maste 		default:
10262c23cb7cSEd Maste 			break;
10272c23cb7cSEd Maste 		}
10282c23cb7cSEd Maste 	}
10292c23cb7cSEd Maste 
10302c23cb7cSEd Maste 	snprintf(s_dtype, sizeof(s_dtype), "<unknown: %#x>", dtype);
10312c23cb7cSEd Maste 	return (s_dtype);
10322c23cb7cSEd Maste }
10332c23cb7cSEd Maste 
10342c23cb7cSEd Maste static const char *
st_bind(unsigned int sbind)10352c23cb7cSEd Maste st_bind(unsigned int sbind)
10362c23cb7cSEd Maste {
10372c23cb7cSEd Maste 	static char s_sbind[32];
10382c23cb7cSEd Maste 
10392c23cb7cSEd Maste 	switch (sbind) {
10402c23cb7cSEd Maste 	case STB_LOCAL: return "LOCAL";
10412c23cb7cSEd Maste 	case STB_GLOBAL: return "GLOBAL";
10422c23cb7cSEd Maste 	case STB_WEAK: return "WEAK";
1043839529caSEd Maste 	case STB_GNU_UNIQUE: return "UNIQUE";
10442c23cb7cSEd Maste 	default:
10452c23cb7cSEd Maste 		if (sbind >= STB_LOOS && sbind <= STB_HIOS)
10462c23cb7cSEd Maste 			return "OS";
10472c23cb7cSEd Maste 		else if (sbind >= STB_LOPROC && sbind <= STB_HIPROC)
10482c23cb7cSEd Maste 			return "PROC";
10492c23cb7cSEd Maste 		else
10502c23cb7cSEd Maste 			snprintf(s_sbind, sizeof(s_sbind), "<unknown: %#x>",
10512c23cb7cSEd Maste 			    sbind);
10522c23cb7cSEd Maste 		return (s_sbind);
10532c23cb7cSEd Maste 	}
10542c23cb7cSEd Maste }
10552c23cb7cSEd Maste 
10562c23cb7cSEd Maste static const char *
st_type(unsigned int mach,unsigned int os,unsigned int stype)1057b6b6f9ccSEd Maste st_type(unsigned int mach, unsigned int os, unsigned int stype)
10582c23cb7cSEd Maste {
10592c23cb7cSEd Maste 	static char s_stype[32];
10602c23cb7cSEd Maste 
10612c23cb7cSEd Maste 	switch (stype) {
10622c23cb7cSEd Maste 	case STT_NOTYPE: return "NOTYPE";
10632c23cb7cSEd Maste 	case STT_OBJECT: return "OBJECT";
10642c23cb7cSEd Maste 	case STT_FUNC: return "FUNC";
10652c23cb7cSEd Maste 	case STT_SECTION: return "SECTION";
10662c23cb7cSEd Maste 	case STT_FILE: return "FILE";
10672c23cb7cSEd Maste 	case STT_COMMON: return "COMMON";
10682c23cb7cSEd Maste 	case STT_TLS: return "TLS";
10692c23cb7cSEd Maste 	default:
1070b6b6f9ccSEd Maste 		if (stype >= STT_LOOS && stype <= STT_HIOS) {
1071b6b6f9ccSEd Maste 			if ((os == ELFOSABI_GNU || os == ELFOSABI_FREEBSD) &&
1072b6b6f9ccSEd Maste 			    stype == STT_GNU_IFUNC)
1073b6b6f9ccSEd Maste 				return "IFUNC";
10742c23cb7cSEd Maste 			snprintf(s_stype, sizeof(s_stype), "OS+%#x",
10752c23cb7cSEd Maste 			    stype - STT_LOOS);
1076b6b6f9ccSEd Maste 		} else if (stype >= STT_LOPROC && stype <= STT_HIPROC) {
1077839529caSEd Maste 			if (mach == EM_SPARCV9 && stype == STT_SPARC_REGISTER)
1078839529caSEd Maste 				return "REGISTER";
10792c23cb7cSEd Maste 			snprintf(s_stype, sizeof(s_stype), "PROC+%#x",
10802c23cb7cSEd Maste 			    stype - STT_LOPROC);
1081839529caSEd Maste 		} else
10822c23cb7cSEd Maste 			snprintf(s_stype, sizeof(s_stype), "<unknown: %#x>",
10832c23cb7cSEd Maste 			    stype);
10842c23cb7cSEd Maste 		return (s_stype);
10852c23cb7cSEd Maste 	}
10862c23cb7cSEd Maste }
10872c23cb7cSEd Maste 
10882c23cb7cSEd Maste static const char *
st_vis(unsigned int svis)10892c23cb7cSEd Maste st_vis(unsigned int svis)
10902c23cb7cSEd Maste {
10912c23cb7cSEd Maste 	static char s_svis[32];
10922c23cb7cSEd Maste 
10932c23cb7cSEd Maste 	switch(svis) {
10942c23cb7cSEd Maste 	case STV_DEFAULT: return "DEFAULT";
10952c23cb7cSEd Maste 	case STV_INTERNAL: return "INTERNAL";
10962c23cb7cSEd Maste 	case STV_HIDDEN: return "HIDDEN";
10972c23cb7cSEd Maste 	case STV_PROTECTED: return "PROTECTED";
10982c23cb7cSEd Maste 	default:
10992c23cb7cSEd Maste 		snprintf(s_svis, sizeof(s_svis), "<unknown: %#x>", svis);
11002c23cb7cSEd Maste 		return (s_svis);
11012c23cb7cSEd Maste 	}
11022c23cb7cSEd Maste }
11032c23cb7cSEd Maste 
11042c23cb7cSEd Maste static const char *
st_shndx(unsigned int shndx)11052c23cb7cSEd Maste st_shndx(unsigned int shndx)
11062c23cb7cSEd Maste {
11072c23cb7cSEd Maste 	static char s_shndx[32];
11082c23cb7cSEd Maste 
11092c23cb7cSEd Maste 	switch (shndx) {
11102c23cb7cSEd Maste 	case SHN_UNDEF: return "UND";
11112c23cb7cSEd Maste 	case SHN_ABS: return "ABS";
11122c23cb7cSEd Maste 	case SHN_COMMON: return "COM";
11132c23cb7cSEd Maste 	default:
11142c23cb7cSEd Maste 		if (shndx >= SHN_LOPROC && shndx <= SHN_HIPROC)
11152c23cb7cSEd Maste 			return "PRC";
11162c23cb7cSEd Maste 		else if (shndx >= SHN_LOOS && shndx <= SHN_HIOS)
11172c23cb7cSEd Maste 			return "OS";
11182c23cb7cSEd Maste 		else
11192c23cb7cSEd Maste 			snprintf(s_shndx, sizeof(s_shndx), "%u", shndx);
11202c23cb7cSEd Maste 		return (s_shndx);
11212c23cb7cSEd Maste 	}
11222c23cb7cSEd Maste }
11232c23cb7cSEd Maste 
11242c23cb7cSEd Maste static struct {
11252c23cb7cSEd Maste 	const char *ln;
11262c23cb7cSEd Maste 	char sn;
11272c23cb7cSEd Maste 	int value;
11282c23cb7cSEd Maste } section_flag[] = {
11292c23cb7cSEd Maste 	{"WRITE", 'W', SHF_WRITE},
11302c23cb7cSEd Maste 	{"ALLOC", 'A', SHF_ALLOC},
11312c23cb7cSEd Maste 	{"EXEC", 'X', SHF_EXECINSTR},
11322c23cb7cSEd Maste 	{"MERGE", 'M', SHF_MERGE},
11332c23cb7cSEd Maste 	{"STRINGS", 'S', SHF_STRINGS},
11342c23cb7cSEd Maste 	{"INFO LINK", 'I', SHF_INFO_LINK},
11352c23cb7cSEd Maste 	{"OS NONCONF", 'O', SHF_OS_NONCONFORMING},
11362c23cb7cSEd Maste 	{"GROUP", 'G', SHF_GROUP},
11372c23cb7cSEd Maste 	{"TLS", 'T', SHF_TLS},
1138b6b6f9ccSEd Maste 	{"COMPRESSED", 'C', SHF_COMPRESSED},
11392c23cb7cSEd Maste 	{NULL, 0, 0}
11402c23cb7cSEd Maste };
11412c23cb7cSEd Maste 
11422c23cb7cSEd Maste static const char *
note_type(const char * name,unsigned int et,unsigned int nt)114302b08c90SEd Maste note_type(const char *name, unsigned int et, unsigned int nt)
114402b08c90SEd Maste {
114571a0c925SEd Maste 	if ((strcmp(name, "CORE") == 0 || strcmp(name, "LINUX") == 0) &&
114671a0c925SEd Maste 	    et == ET_CORE)
114702b08c90SEd Maste 		return note_type_linux_core(nt);
114802b08c90SEd Maste 	else if (strcmp(name, "FreeBSD") == 0)
114902b08c90SEd Maste 		if (et == ET_CORE)
115002b08c90SEd Maste 			return note_type_freebsd_core(nt);
115102b08c90SEd Maste 		else
115202b08c90SEd Maste 			return note_type_freebsd(nt);
115302b08c90SEd Maste 	else if (strcmp(name, "GNU") == 0 && et != ET_CORE)
115402b08c90SEd Maste 		return note_type_gnu(nt);
1155ca457394SEd Maste 	else if (strcmp(name, "Go") == 0 && et != ET_CORE)
1156ca457394SEd Maste 		return note_type_go(nt);
115702b08c90SEd Maste 	else if (strcmp(name, "NetBSD") == 0 && et != ET_CORE)
115802b08c90SEd Maste 		return note_type_netbsd(nt);
115902b08c90SEd Maste 	else if (strcmp(name, "OpenBSD") == 0 && et != ET_CORE)
116002b08c90SEd Maste 		return note_type_openbsd(nt);
1161895f86f1SEd Maste 	else if (strcmp(name, "Xen") == 0 && et != ET_CORE)
1162895f86f1SEd Maste 		return note_type_xen(nt);
116302b08c90SEd Maste 	return note_type_unknown(nt);
116402b08c90SEd Maste }
116502b08c90SEd Maste 
116602b08c90SEd Maste static const char *
note_type_freebsd(unsigned int nt)116702b08c90SEd Maste note_type_freebsd(unsigned int nt)
116802b08c90SEd Maste {
116902b08c90SEd Maste 	switch (nt) {
117002b08c90SEd Maste 	case 1: return "NT_FREEBSD_ABI_TAG";
117102b08c90SEd Maste 	case 2: return "NT_FREEBSD_NOINIT_TAG";
117202b08c90SEd Maste 	case 3: return "NT_FREEBSD_ARCH_TAG";
1173e74f411dSEd Maste 	case 4: return "NT_FREEBSD_FEATURE_CTL";
117402b08c90SEd Maste 	default: return (note_type_unknown(nt));
117502b08c90SEd Maste 	}
117602b08c90SEd Maste }
117702b08c90SEd Maste 
117802b08c90SEd Maste static const char *
note_type_freebsd_core(unsigned int nt)117902b08c90SEd Maste note_type_freebsd_core(unsigned int nt)
118002b08c90SEd Maste {
118102b08c90SEd Maste 	switch (nt) {
118202b08c90SEd Maste 	case 1: return "NT_PRSTATUS";
118302b08c90SEd Maste 	case 2: return "NT_FPREGSET";
118402b08c90SEd Maste 	case 3: return "NT_PRPSINFO";
118502b08c90SEd Maste 	case 7: return "NT_THRMISC";
118602b08c90SEd Maste 	case 8: return "NT_PROCSTAT_PROC";
118702b08c90SEd Maste 	case 9: return "NT_PROCSTAT_FILES";
118802b08c90SEd Maste 	case 10: return "NT_PROCSTAT_VMMAP";
118902b08c90SEd Maste 	case 11: return "NT_PROCSTAT_GROUPS";
119002b08c90SEd Maste 	case 12: return "NT_PROCSTAT_UMASK";
119102b08c90SEd Maste 	case 13: return "NT_PROCSTAT_RLIMIT";
119202b08c90SEd Maste 	case 14: return "NT_PROCSTAT_OSREL";
119302b08c90SEd Maste 	case 15: return "NT_PROCSTAT_PSSTRINGS";
119402b08c90SEd Maste 	case 16: return "NT_PROCSTAT_AUXV";
1195369bd05bSJohn Baldwin 	case 17: return "NT_PTLWPINFO";
119612f7c1e8SJustin Hibbits 	case 0x100: return "NT_PPC_VMX (ppc Altivec registers)";
119712f7c1e8SJustin Hibbits 	case 0x102: return "NT_PPC_VSX (ppc VSX registers)";
1198931983eeSJohn Baldwin 	case 0x200: return "NT_X86_SEGBASES (x86 segment base registers)";
119902b08c90SEd Maste 	case 0x202: return "NT_X86_XSTATE (x86 XSAVE extended state)";
1200369bd05bSJohn Baldwin 	case 0x400: return "NT_ARM_VFP (arm VFP registers)";
1201b2cb74c2SJohn Baldwin 	case 0x401: return "NT_ARM_TLS (arm TLS register)";
1202817e68d8SEd Maste 	case 0x406: return "NT_ARM_ADDR_MASK (arm address mask)";
120302b08c90SEd Maste 	default: return (note_type_unknown(nt));
120402b08c90SEd Maste 	}
120502b08c90SEd Maste }
120602b08c90SEd Maste 
120702b08c90SEd Maste static const char *
note_type_linux_core(unsigned int nt)120802b08c90SEd Maste note_type_linux_core(unsigned int nt)
120902b08c90SEd Maste {
121002b08c90SEd Maste 	switch (nt) {
121102b08c90SEd Maste 	case 1: return "NT_PRSTATUS (Process status)";
121202b08c90SEd Maste 	case 2: return "NT_FPREGSET (Floating point information)";
121302b08c90SEd Maste 	case 3: return "NT_PRPSINFO (Process information)";
121471a0c925SEd Maste 	case 4: return "NT_TASKSTRUCT (Task structure)";
121502b08c90SEd Maste 	case 6: return "NT_AUXV (Auxiliary vector)";
121602b08c90SEd Maste 	case 10: return "NT_PSTATUS (Linux process status)";
121702b08c90SEd Maste 	case 12: return "NT_FPREGS (Linux floating point regset)";
121802b08c90SEd Maste 	case 13: return "NT_PSINFO (Linux process information)";
121902b08c90SEd Maste 	case 16: return "NT_LWPSTATUS (Linux lwpstatus_t type)";
122002b08c90SEd Maste 	case 17: return "NT_LWPSINFO (Linux lwpinfo_t type)";
122171a0c925SEd Maste 	case 18: return "NT_WIN32PSTATUS (win32_pstatus structure)";
122271a0c925SEd Maste 	case 0x100: return "NT_PPC_VMX (ppc Altivec registers)";
122371a0c925SEd Maste 	case 0x102: return "NT_PPC_VSX (ppc VSX registers)";
122471a0c925SEd Maste 	case 0x202: return "NT_X86_XSTATE (x86 XSAVE extended state)";
122571a0c925SEd Maste 	case 0x300: return "NT_S390_HIGH_GPRS (s390 upper register halves)";
122671a0c925SEd Maste 	case 0x301: return "NT_S390_TIMER (s390 timer register)";
122771a0c925SEd Maste 	case 0x302: return "NT_S390_TODCMP (s390 TOD comparator register)";
122871a0c925SEd Maste 	case 0x303: return "NT_S390_TODPREG (s390 TOD programmable register)";
122971a0c925SEd Maste 	case 0x304: return "NT_S390_CTRS (s390 control registers)";
123071a0c925SEd Maste 	case 0x305: return "NT_S390_PREFIX (s390 prefix register)";
123171a0c925SEd Maste 	case 0x400: return "NT_ARM_VFP (arm VFP registers)";
1232089eb4eaSEd Maste 	case 0x401: return "NT_ARM_TLS (arm TLS register)";
1233089eb4eaSEd Maste 	case 0x402: return "NT_ARM_HW_BREAK (arm hardware breakpoint registers)";
1234089eb4eaSEd Maste 	case 0x403: return "NT_ARM_HW_WATCH (arm hardware watchpoint registers)";
1235089eb4eaSEd Maste 	case 0x404: return "NT_ARM_SYSTEM_CALL (arm system call number)";
1236089eb4eaSEd Maste 	case 0x405: return "NT_ARM_SVE (arm scalable vector extension registers)";
1237089eb4eaSEd Maste 	case 0x406: return "NT_ARM_PAC_MASK (arm pointer authentication code mask)";
1238089eb4eaSEd Maste 	case 0x407: return "NT_ARM_PACA_KEYS (arm pointer authentication address keys)";
1239089eb4eaSEd Maste 	case 0x408: return "NT_ARM_PACG_KEYS (arm pointer authentication generic keys)";
1240089eb4eaSEd Maste 	case 0x409: return "NT_ARM_TAGGED_ADDR_CTRL (arm64 tagged address control)";
1241089eb4eaSEd Maste 	case 0x40a: return "NT_ARM_PAC_ENABLED_KEYS (arm64 ptr auth enabled keys)";
124271a0c925SEd Maste 	case 0x46494c45UL: return "NT_FILE (mapped files)";
124371a0c925SEd Maste 	case 0x46E62B7FUL: return "NT_PRXFPREG (Linux user_xfpregs structure)";
124471a0c925SEd Maste 	case 0x53494749UL: return "NT_SIGINFO (siginfo_t data)";
124502b08c90SEd Maste 	default: return (note_type_unknown(nt));
124602b08c90SEd Maste 	}
124702b08c90SEd Maste }
124802b08c90SEd Maste 
124902b08c90SEd Maste static const char *
note_type_gnu(unsigned int nt)125002b08c90SEd Maste note_type_gnu(unsigned int nt)
125102b08c90SEd Maste {
125202b08c90SEd Maste 	switch (nt) {
125302b08c90SEd Maste 	case 1: return "NT_GNU_ABI_TAG";
125402b08c90SEd Maste 	case 2: return "NT_GNU_HWCAP (Hardware capabilities)";
125502b08c90SEd Maste 	case 3: return "NT_GNU_BUILD_ID (Build id set by ld(1))";
125602b08c90SEd Maste 	case 4: return "NT_GNU_GOLD_VERSION (GNU gold version)";
1257ce0c6340SEd Maste 	case 5: return "NT_GNU_PROPERTY_TYPE_0";
125802b08c90SEd Maste 	default: return (note_type_unknown(nt));
125902b08c90SEd Maste 	}
126002b08c90SEd Maste }
126102b08c90SEd Maste 
126202b08c90SEd Maste static const char *
note_type_go(unsigned int nt)1263ca457394SEd Maste note_type_go(unsigned int nt)
1264ca457394SEd Maste {
1265ca457394SEd Maste 	switch (nt) {
1266ca457394SEd Maste 	case 4: return "elfGoBuildIDTag";
1267ca457394SEd Maste 	default: return (note_type_unknown(nt));
1268ca457394SEd Maste 	}
1269ca457394SEd Maste }
1270ca457394SEd Maste 
1271ca457394SEd Maste static const char *
note_type_netbsd(unsigned int nt)127202b08c90SEd Maste note_type_netbsd(unsigned int nt)
127302b08c90SEd Maste {
127402b08c90SEd Maste 	switch (nt) {
127502b08c90SEd Maste 	case 1: return "NT_NETBSD_IDENT";
127602b08c90SEd Maste 	default: return (note_type_unknown(nt));
127702b08c90SEd Maste 	}
127802b08c90SEd Maste }
127902b08c90SEd Maste 
128002b08c90SEd Maste static const char *
note_type_openbsd(unsigned int nt)128102b08c90SEd Maste note_type_openbsd(unsigned int nt)
128202b08c90SEd Maste {
128302b08c90SEd Maste 	switch (nt) {
128402b08c90SEd Maste 	case 1: return "NT_OPENBSD_IDENT";
128502b08c90SEd Maste 	default: return (note_type_unknown(nt));
128602b08c90SEd Maste 	}
128702b08c90SEd Maste }
128802b08c90SEd Maste 
128902b08c90SEd Maste static const char *
note_type_unknown(unsigned int nt)129002b08c90SEd Maste note_type_unknown(unsigned int nt)
12912c23cb7cSEd Maste {
12922c23cb7cSEd Maste 	static char s_nt[32];
12932c23cb7cSEd Maste 
129471a0c925SEd Maste 	snprintf(s_nt, sizeof(s_nt),
129571a0c925SEd Maste 	    nt >= 0x100 ? "<unknown: 0x%x>" : "<unknown: %u>", nt);
12962c23cb7cSEd Maste 	return (s_nt);
12972c23cb7cSEd Maste }
12982c23cb7cSEd Maste 
1299895f86f1SEd Maste static const char *
note_type_xen(unsigned int nt)1300895f86f1SEd Maste note_type_xen(unsigned int nt)
1301895f86f1SEd Maste {
1302895f86f1SEd Maste 	switch (nt) {
1303895f86f1SEd Maste 	case 0: return "XEN_ELFNOTE_INFO";
1304895f86f1SEd Maste 	case 1: return "XEN_ELFNOTE_ENTRY";
1305895f86f1SEd Maste 	case 2: return "XEN_ELFNOTE_HYPERCALL_PAGE";
1306895f86f1SEd Maste 	case 3: return "XEN_ELFNOTE_VIRT_BASE";
1307895f86f1SEd Maste 	case 4: return "XEN_ELFNOTE_PADDR_OFFSET";
1308895f86f1SEd Maste 	case 5: return "XEN_ELFNOTE_XEN_VERSION";
1309895f86f1SEd Maste 	case 6: return "XEN_ELFNOTE_GUEST_OS";
1310895f86f1SEd Maste 	case 7: return "XEN_ELFNOTE_GUEST_VERSION";
1311895f86f1SEd Maste 	case 8: return "XEN_ELFNOTE_LOADER";
1312895f86f1SEd Maste 	case 9: return "XEN_ELFNOTE_PAE_MODE";
1313895f86f1SEd Maste 	case 10: return "XEN_ELFNOTE_FEATURES";
1314895f86f1SEd Maste 	case 11: return "XEN_ELFNOTE_BSD_SYMTAB";
1315895f86f1SEd Maste 	case 12: return "XEN_ELFNOTE_HV_START_LOW";
1316895f86f1SEd Maste 	case 13: return "XEN_ELFNOTE_L1_MFN_VALID";
1317895f86f1SEd Maste 	case 14: return "XEN_ELFNOTE_SUSPEND_CANCEL";
1318895f86f1SEd Maste 	case 15: return "XEN_ELFNOTE_INIT_P2M";
1319895f86f1SEd Maste 	case 16: return "XEN_ELFNOTE_MOD_START_PFN";
1320895f86f1SEd Maste 	case 17: return "XEN_ELFNOTE_SUPPORTED_FEATURES";
13214d8a9fafSEd Maste 	case 18: return "XEN_ELFNOTE_PHYS32_ENTRY";
1322895f86f1SEd Maste 	default: return (note_type_unknown(nt));
1323895f86f1SEd Maste 	}
1324895f86f1SEd Maste }
1325895f86f1SEd Maste 
13262c23cb7cSEd Maste static struct {
13272c23cb7cSEd Maste 	const char *name;
13282c23cb7cSEd Maste 	int value;
13292c23cb7cSEd Maste } l_flag[] = {
13302c23cb7cSEd Maste 	{"EXACT_MATCH", LL_EXACT_MATCH},
13312c23cb7cSEd Maste 	{"IGNORE_INT_VER", LL_IGNORE_INT_VER},
13322c23cb7cSEd Maste 	{"REQUIRE_MINOR", LL_REQUIRE_MINOR},
13332c23cb7cSEd Maste 	{"EXPORTS", LL_EXPORTS},
13342c23cb7cSEd Maste 	{"DELAY_LOAD", LL_DELAY_LOAD},
13352c23cb7cSEd Maste 	{"DELTA", LL_DELTA},
13362c23cb7cSEd Maste 	{NULL, 0}
13372c23cb7cSEd Maste };
13382c23cb7cSEd Maste 
13392c23cb7cSEd Maste static struct mips_option mips_exceptions_option[] = {
13402c23cb7cSEd Maste 	{OEX_PAGE0, "PAGE0"},
13412c23cb7cSEd Maste 	{OEX_SMM, "SMM"},
13422c23cb7cSEd Maste 	{OEX_PRECISEFP, "PRECISEFP"},
13432c23cb7cSEd Maste 	{OEX_DISMISS, "DISMISS"},
13442c23cb7cSEd Maste 	{0, NULL}
13452c23cb7cSEd Maste };
13462c23cb7cSEd Maste 
13472c23cb7cSEd Maste static struct mips_option mips_pad_option[] = {
13482c23cb7cSEd Maste 	{OPAD_PREFIX, "PREFIX"},
13492c23cb7cSEd Maste 	{OPAD_POSTFIX, "POSTFIX"},
13502c23cb7cSEd Maste 	{OPAD_SYMBOL, "SYMBOL"},
13512c23cb7cSEd Maste 	{0, NULL}
13522c23cb7cSEd Maste };
13532c23cb7cSEd Maste 
13542c23cb7cSEd Maste static struct mips_option mips_hwpatch_option[] = {
13552c23cb7cSEd Maste 	{OHW_R4KEOP, "R4KEOP"},
13562c23cb7cSEd Maste 	{OHW_R8KPFETCH, "R8KPFETCH"},
13572c23cb7cSEd Maste 	{OHW_R5KEOP, "R5KEOP"},
13582c23cb7cSEd Maste 	{OHW_R5KCVTL, "R5KCVTL"},
13592c23cb7cSEd Maste 	{0, NULL}
13602c23cb7cSEd Maste };
13612c23cb7cSEd Maste 
13622c23cb7cSEd Maste static struct mips_option mips_hwa_option[] = {
13632c23cb7cSEd Maste 	{OHWA0_R4KEOP_CHECKED, "R4KEOP_CHECKED"},
13642c23cb7cSEd Maste 	{OHWA0_R4KEOP_CLEAN, "R4KEOP_CLEAN"},
13652c23cb7cSEd Maste 	{0, NULL}
13662c23cb7cSEd Maste };
13672c23cb7cSEd Maste 
13682c23cb7cSEd Maste static struct mips_option mips_hwo_option[] = {
13692c23cb7cSEd Maste 	{OHWO0_FIXADE, "FIXADE"},
13702c23cb7cSEd Maste 	{0, NULL}
13712c23cb7cSEd Maste };
13722c23cb7cSEd Maste 
13732c23cb7cSEd Maste static const char *
option_kind(uint8_t kind)13742c23cb7cSEd Maste option_kind(uint8_t kind)
13752c23cb7cSEd Maste {
13762c23cb7cSEd Maste 	static char s_kind[32];
13772c23cb7cSEd Maste 
13782c23cb7cSEd Maste 	switch (kind) {
13792c23cb7cSEd Maste 	case ODK_NULL: return "NULL";
13802c23cb7cSEd Maste 	case ODK_REGINFO: return "REGINFO";
13812c23cb7cSEd Maste 	case ODK_EXCEPTIONS: return "EXCEPTIONS";
13822c23cb7cSEd Maste 	case ODK_PAD: return "PAD";
13832c23cb7cSEd Maste 	case ODK_HWPATCH: return "HWPATCH";
13842c23cb7cSEd Maste 	case ODK_FILL: return "FILL";
13852c23cb7cSEd Maste 	case ODK_TAGS: return "TAGS";
13862c23cb7cSEd Maste 	case ODK_HWAND: return "HWAND";
13872c23cb7cSEd Maste 	case ODK_HWOR: return "HWOR";
13882c23cb7cSEd Maste 	case ODK_GP_GROUP: return "GP_GROUP";
13892c23cb7cSEd Maste 	case ODK_IDENT: return "IDENT";
13902c23cb7cSEd Maste 	default:
13912c23cb7cSEd Maste 		snprintf(s_kind, sizeof(s_kind), "<unknown: %u>", kind);
13922c23cb7cSEd Maste 		return (s_kind);
13932c23cb7cSEd Maste 	}
13942c23cb7cSEd Maste }
13952c23cb7cSEd Maste 
13962c23cb7cSEd Maste static const char *
top_tag(unsigned int tag)13972c23cb7cSEd Maste top_tag(unsigned int tag)
13982c23cb7cSEd Maste {
13992c23cb7cSEd Maste 	static char s_top_tag[32];
14002c23cb7cSEd Maste 
14012c23cb7cSEd Maste 	switch (tag) {
14022c23cb7cSEd Maste 	case 1: return "File Attributes";
14032c23cb7cSEd Maste 	case 2: return "Section Attributes";
14042c23cb7cSEd Maste 	case 3: return "Symbol Attributes";
14052c23cb7cSEd Maste 	default:
14062c23cb7cSEd Maste 		snprintf(s_top_tag, sizeof(s_top_tag), "Unknown tag: %u", tag);
14072c23cb7cSEd Maste 		return (s_top_tag);
14082c23cb7cSEd Maste 	}
14092c23cb7cSEd Maste }
14102c23cb7cSEd Maste 
14112c23cb7cSEd Maste static const char *
aeabi_cpu_arch(uint64_t arch)14122c23cb7cSEd Maste aeabi_cpu_arch(uint64_t arch)
14132c23cb7cSEd Maste {
14142c23cb7cSEd Maste 	static char s_cpu_arch[32];
14152c23cb7cSEd Maste 
14162c23cb7cSEd Maste 	switch (arch) {
14172c23cb7cSEd Maste 	case 0: return "Pre-V4";
14182c23cb7cSEd Maste 	case 1: return "ARM v4";
14192c23cb7cSEd Maste 	case 2: return "ARM v4T";
14202c23cb7cSEd Maste 	case 3: return "ARM v5T";
14212c23cb7cSEd Maste 	case 4: return "ARM v5TE";
14222c23cb7cSEd Maste 	case 5: return "ARM v5TEJ";
14232c23cb7cSEd Maste 	case 6: return "ARM v6";
14242c23cb7cSEd Maste 	case 7: return "ARM v6KZ";
14252c23cb7cSEd Maste 	case 8: return "ARM v6T2";
14262c23cb7cSEd Maste 	case 9: return "ARM v6K";
14272c23cb7cSEd Maste 	case 10: return "ARM v7";
14282c23cb7cSEd Maste 	case 11: return "ARM v6-M";
14292c23cb7cSEd Maste 	case 12: return "ARM v6S-M";
14302c23cb7cSEd Maste 	case 13: return "ARM v7E-M";
14312c23cb7cSEd Maste 	default:
14322c23cb7cSEd Maste 		snprintf(s_cpu_arch, sizeof(s_cpu_arch),
14332c23cb7cSEd Maste 		    "Unknown (%ju)", (uintmax_t) arch);
14342c23cb7cSEd Maste 		return (s_cpu_arch);
14352c23cb7cSEd Maste 	}
14362c23cb7cSEd Maste }
14372c23cb7cSEd Maste 
14382c23cb7cSEd Maste static const char *
aeabi_cpu_arch_profile(uint64_t pf)14392c23cb7cSEd Maste aeabi_cpu_arch_profile(uint64_t pf)
14402c23cb7cSEd Maste {
14412c23cb7cSEd Maste 	static char s_arch_profile[32];
14422c23cb7cSEd Maste 
14432c23cb7cSEd Maste 	switch (pf) {
14442c23cb7cSEd Maste 	case 0:
14452c23cb7cSEd Maste 		return "Not applicable";
14462c23cb7cSEd Maste 	case 0x41:		/* 'A' */
14472c23cb7cSEd Maste 		return "Application Profile";
14482c23cb7cSEd Maste 	case 0x52:		/* 'R' */
14492c23cb7cSEd Maste 		return "Real-Time Profile";
14502c23cb7cSEd Maste 	case 0x4D:		/* 'M' */
14512c23cb7cSEd Maste 		return "Microcontroller Profile";
14522c23cb7cSEd Maste 	case 0x53:		/* 'S' */
14532c23cb7cSEd Maste 		return "Application or Real-Time Profile";
14542c23cb7cSEd Maste 	default:
14552c23cb7cSEd Maste 		snprintf(s_arch_profile, sizeof(s_arch_profile),
14562c23cb7cSEd Maste 		    "Unknown (%ju)\n", (uintmax_t) pf);
14572c23cb7cSEd Maste 		return (s_arch_profile);
14582c23cb7cSEd Maste 	}
14592c23cb7cSEd Maste }
14602c23cb7cSEd Maste 
14612c23cb7cSEd Maste static const char *
aeabi_arm_isa(uint64_t ai)14622c23cb7cSEd Maste aeabi_arm_isa(uint64_t ai)
14632c23cb7cSEd Maste {
14642c23cb7cSEd Maste 	static char s_ai[32];
14652c23cb7cSEd Maste 
14662c23cb7cSEd Maste 	switch (ai) {
14672c23cb7cSEd Maste 	case 0: return "No";
14682c23cb7cSEd Maste 	case 1: return "Yes";
14692c23cb7cSEd Maste 	default:
14702c23cb7cSEd Maste 		snprintf(s_ai, sizeof(s_ai), "Unknown (%ju)\n",
14712c23cb7cSEd Maste 		    (uintmax_t) ai);
14722c23cb7cSEd Maste 		return (s_ai);
14732c23cb7cSEd Maste 	}
14742c23cb7cSEd Maste }
14752c23cb7cSEd Maste 
14762c23cb7cSEd Maste static const char *
aeabi_thumb_isa(uint64_t ti)14772c23cb7cSEd Maste aeabi_thumb_isa(uint64_t ti)
14782c23cb7cSEd Maste {
14792c23cb7cSEd Maste 	static char s_ti[32];
14802c23cb7cSEd Maste 
14812c23cb7cSEd Maste 	switch (ti) {
14822c23cb7cSEd Maste 	case 0: return "No";
14832c23cb7cSEd Maste 	case 1: return "16-bit Thumb";
14842c23cb7cSEd Maste 	case 2: return "32-bit Thumb";
14852c23cb7cSEd Maste 	default:
14862c23cb7cSEd Maste 		snprintf(s_ti, sizeof(s_ti), "Unknown (%ju)\n",
14872c23cb7cSEd Maste 		    (uintmax_t) ti);
14882c23cb7cSEd Maste 		return (s_ti);
14892c23cb7cSEd Maste 	}
14902c23cb7cSEd Maste }
14912c23cb7cSEd Maste 
14922c23cb7cSEd Maste static const char *
aeabi_fp_arch(uint64_t fp)14932c23cb7cSEd Maste aeabi_fp_arch(uint64_t fp)
14942c23cb7cSEd Maste {
14952c23cb7cSEd Maste 	static char s_fp_arch[32];
14962c23cb7cSEd Maste 
14972c23cb7cSEd Maste 	switch (fp) {
14982c23cb7cSEd Maste 	case 0: return "No";
14992c23cb7cSEd Maste 	case 1: return "VFPv1";
15002c23cb7cSEd Maste 	case 2: return "VFPv2";
15012c23cb7cSEd Maste 	case 3: return "VFPv3";
15022c23cb7cSEd Maste 	case 4: return "VFPv3-D16";
15032c23cb7cSEd Maste 	case 5: return "VFPv4";
15042c23cb7cSEd Maste 	case 6: return "VFPv4-D16";
15052c23cb7cSEd Maste 	default:
15062c23cb7cSEd Maste 		snprintf(s_fp_arch, sizeof(s_fp_arch), "Unknown (%ju)",
15072c23cb7cSEd Maste 		    (uintmax_t) fp);
15082c23cb7cSEd Maste 		return (s_fp_arch);
15092c23cb7cSEd Maste 	}
15102c23cb7cSEd Maste }
15112c23cb7cSEd Maste 
15122c23cb7cSEd Maste static const char *
aeabi_wmmx_arch(uint64_t wmmx)15132c23cb7cSEd Maste aeabi_wmmx_arch(uint64_t wmmx)
15142c23cb7cSEd Maste {
15152c23cb7cSEd Maste 	static char s_wmmx[32];
15162c23cb7cSEd Maste 
15172c23cb7cSEd Maste 	switch (wmmx) {
15182c23cb7cSEd Maste 	case 0: return "No";
15192c23cb7cSEd Maste 	case 1: return "WMMXv1";
15202c23cb7cSEd Maste 	case 2: return "WMMXv2";
15212c23cb7cSEd Maste 	default:
15222c23cb7cSEd Maste 		snprintf(s_wmmx, sizeof(s_wmmx), "Unknown (%ju)",
15232c23cb7cSEd Maste 		    (uintmax_t) wmmx);
15242c23cb7cSEd Maste 		return (s_wmmx);
15252c23cb7cSEd Maste 	}
15262c23cb7cSEd Maste }
15272c23cb7cSEd Maste 
15282c23cb7cSEd Maste static const char *
aeabi_adv_simd_arch(uint64_t simd)15292c23cb7cSEd Maste aeabi_adv_simd_arch(uint64_t simd)
15302c23cb7cSEd Maste {
15312c23cb7cSEd Maste 	static char s_simd[32];
15322c23cb7cSEd Maste 
15332c23cb7cSEd Maste 	switch (simd) {
15342c23cb7cSEd Maste 	case 0: return "No";
15352c23cb7cSEd Maste 	case 1: return "NEONv1";
15362c23cb7cSEd Maste 	case 2: return "NEONv2";
15372c23cb7cSEd Maste 	default:
15382c23cb7cSEd Maste 		snprintf(s_simd, sizeof(s_simd), "Unknown (%ju)",
15392c23cb7cSEd Maste 		    (uintmax_t) simd);
15402c23cb7cSEd Maste 		return (s_simd);
15412c23cb7cSEd Maste 	}
15422c23cb7cSEd Maste }
15432c23cb7cSEd Maste 
15442c23cb7cSEd Maste static const char *
aeabi_pcs_config(uint64_t pcs)15452c23cb7cSEd Maste aeabi_pcs_config(uint64_t pcs)
15462c23cb7cSEd Maste {
15472c23cb7cSEd Maste 	static char s_pcs[32];
15482c23cb7cSEd Maste 
15492c23cb7cSEd Maste 	switch (pcs) {
15502c23cb7cSEd Maste 	case 0: return "None";
15512c23cb7cSEd Maste 	case 1: return "Bare platform";
15522c23cb7cSEd Maste 	case 2: return "Linux";
15532c23cb7cSEd Maste 	case 3: return "Linux DSO";
15542c23cb7cSEd Maste 	case 4: return "Palm OS 2004";
15552c23cb7cSEd Maste 	case 5: return "Palm OS (future)";
15562c23cb7cSEd Maste 	case 6: return "Symbian OS 2004";
15572c23cb7cSEd Maste 	case 7: return "Symbian OS (future)";
15582c23cb7cSEd Maste 	default:
15592c23cb7cSEd Maste 		snprintf(s_pcs, sizeof(s_pcs), "Unknown (%ju)",
15602c23cb7cSEd Maste 		    (uintmax_t) pcs);
15612c23cb7cSEd Maste 		return (s_pcs);
15622c23cb7cSEd Maste 	}
15632c23cb7cSEd Maste }
15642c23cb7cSEd Maste 
15652c23cb7cSEd Maste static const char *
aeabi_pcs_r9(uint64_t r9)15662c23cb7cSEd Maste aeabi_pcs_r9(uint64_t r9)
15672c23cb7cSEd Maste {
15682c23cb7cSEd Maste 	static char s_r9[32];
15692c23cb7cSEd Maste 
15702c23cb7cSEd Maste 	switch (r9) {
15712c23cb7cSEd Maste 	case 0: return "V6";
15722c23cb7cSEd Maste 	case 1: return "SB";
15732c23cb7cSEd Maste 	case 2: return "TLS pointer";
15742c23cb7cSEd Maste 	case 3: return "Unused";
15752c23cb7cSEd Maste 	default:
15762c23cb7cSEd Maste 		snprintf(s_r9, sizeof(s_r9), "Unknown (%ju)", (uintmax_t) r9);
15772c23cb7cSEd Maste 		return (s_r9);
15782c23cb7cSEd Maste 	}
15792c23cb7cSEd Maste }
15802c23cb7cSEd Maste 
15812c23cb7cSEd Maste static const char *
aeabi_pcs_rw(uint64_t rw)15822c23cb7cSEd Maste aeabi_pcs_rw(uint64_t rw)
15832c23cb7cSEd Maste {
15842c23cb7cSEd Maste 	static char s_rw[32];
15852c23cb7cSEd Maste 
15862c23cb7cSEd Maste 	switch (rw) {
15872c23cb7cSEd Maste 	case 0: return "Absolute";
15882c23cb7cSEd Maste 	case 1: return "PC-relative";
15892c23cb7cSEd Maste 	case 2: return "SB-relative";
15902c23cb7cSEd Maste 	case 3: return "None";
15912c23cb7cSEd Maste 	default:
15922c23cb7cSEd Maste 		snprintf(s_rw, sizeof(s_rw), "Unknown (%ju)", (uintmax_t) rw);
15932c23cb7cSEd Maste 		return (s_rw);
15942c23cb7cSEd Maste 	}
15952c23cb7cSEd Maste }
15962c23cb7cSEd Maste 
15972c23cb7cSEd Maste static const char *
aeabi_pcs_ro(uint64_t ro)15982c23cb7cSEd Maste aeabi_pcs_ro(uint64_t ro)
15992c23cb7cSEd Maste {
16002c23cb7cSEd Maste 	static char s_ro[32];
16012c23cb7cSEd Maste 
16022c23cb7cSEd Maste 	switch (ro) {
16032c23cb7cSEd Maste 	case 0: return "Absolute";
16042c23cb7cSEd Maste 	case 1: return "PC-relative";
16052c23cb7cSEd Maste 	case 2: return "None";
16062c23cb7cSEd Maste 	default:
16072c23cb7cSEd Maste 		snprintf(s_ro, sizeof(s_ro), "Unknown (%ju)", (uintmax_t) ro);
16082c23cb7cSEd Maste 		return (s_ro);
16092c23cb7cSEd Maste 	}
16102c23cb7cSEd Maste }
16112c23cb7cSEd Maste 
16122c23cb7cSEd Maste static const char *
aeabi_pcs_got(uint64_t got)16132c23cb7cSEd Maste aeabi_pcs_got(uint64_t got)
16142c23cb7cSEd Maste {
16152c23cb7cSEd Maste 	static char s_got[32];
16162c23cb7cSEd Maste 
16172c23cb7cSEd Maste 	switch (got) {
16182c23cb7cSEd Maste 	case 0: return "None";
16192c23cb7cSEd Maste 	case 1: return "direct";
16202c23cb7cSEd Maste 	case 2: return "indirect via GOT";
16212c23cb7cSEd Maste 	default:
16222c23cb7cSEd Maste 		snprintf(s_got, sizeof(s_got), "Unknown (%ju)",
16232c23cb7cSEd Maste 		    (uintmax_t) got);
16242c23cb7cSEd Maste 		return (s_got);
16252c23cb7cSEd Maste 	}
16262c23cb7cSEd Maste }
16272c23cb7cSEd Maste 
16282c23cb7cSEd Maste static const char *
aeabi_pcs_wchar_t(uint64_t wt)16292c23cb7cSEd Maste aeabi_pcs_wchar_t(uint64_t wt)
16302c23cb7cSEd Maste {
16312c23cb7cSEd Maste 	static char s_wt[32];
16322c23cb7cSEd Maste 
16332c23cb7cSEd Maste 	switch (wt) {
16342c23cb7cSEd Maste 	case 0: return "None";
16352c23cb7cSEd Maste 	case 2: return "wchar_t size 2";
16362c23cb7cSEd Maste 	case 4: return "wchar_t size 4";
16372c23cb7cSEd Maste 	default:
16382c23cb7cSEd Maste 		snprintf(s_wt, sizeof(s_wt), "Unknown (%ju)", (uintmax_t) wt);
16392c23cb7cSEd Maste 		return (s_wt);
16402c23cb7cSEd Maste 	}
16412c23cb7cSEd Maste }
16422c23cb7cSEd Maste 
16432c23cb7cSEd Maste static const char *
aeabi_enum_size(uint64_t es)16442c23cb7cSEd Maste aeabi_enum_size(uint64_t es)
16452c23cb7cSEd Maste {
16462c23cb7cSEd Maste 	static char s_es[32];
16472c23cb7cSEd Maste 
16482c23cb7cSEd Maste 	switch (es) {
16492c23cb7cSEd Maste 	case 0: return "None";
16502c23cb7cSEd Maste 	case 1: return "smallest";
16512c23cb7cSEd Maste 	case 2: return "32-bit";
16522c23cb7cSEd Maste 	case 3: return "visible 32-bit";
16532c23cb7cSEd Maste 	default:
16542c23cb7cSEd Maste 		snprintf(s_es, sizeof(s_es), "Unknown (%ju)", (uintmax_t) es);
16552c23cb7cSEd Maste 		return (s_es);
16562c23cb7cSEd Maste 	}
16572c23cb7cSEd Maste }
16582c23cb7cSEd Maste 
16592c23cb7cSEd Maste static const char *
aeabi_align_needed(uint64_t an)16602c23cb7cSEd Maste aeabi_align_needed(uint64_t an)
16612c23cb7cSEd Maste {
16622c23cb7cSEd Maste 	static char s_align_n[64];
16632c23cb7cSEd Maste 
16642c23cb7cSEd Maste 	switch (an) {
16652c23cb7cSEd Maste 	case 0: return "No";
16662c23cb7cSEd Maste 	case 1: return "8-byte align";
16672c23cb7cSEd Maste 	case 2: return "4-byte align";
16682c23cb7cSEd Maste 	case 3: return "Reserved";
16692c23cb7cSEd Maste 	default:
16702c23cb7cSEd Maste 		if (an >= 4 && an <= 12)
16712c23cb7cSEd Maste 			snprintf(s_align_n, sizeof(s_align_n), "8-byte align"
16722c23cb7cSEd Maste 			    " and up to 2^%ju-byte extended align",
16732c23cb7cSEd Maste 			    (uintmax_t) an);
16742c23cb7cSEd Maste 		else
16752c23cb7cSEd Maste 			snprintf(s_align_n, sizeof(s_align_n), "Unknown (%ju)",
16762c23cb7cSEd Maste 			    (uintmax_t) an);
16772c23cb7cSEd Maste 		return (s_align_n);
16782c23cb7cSEd Maste 	}
16792c23cb7cSEd Maste }
16802c23cb7cSEd Maste 
16812c23cb7cSEd Maste static const char *
aeabi_align_preserved(uint64_t ap)16822c23cb7cSEd Maste aeabi_align_preserved(uint64_t ap)
16832c23cb7cSEd Maste {
16842c23cb7cSEd Maste 	static char s_align_p[128];
16852c23cb7cSEd Maste 
16862c23cb7cSEd Maste 	switch (ap) {
16872c23cb7cSEd Maste 	case 0: return "No";
16882c23cb7cSEd Maste 	case 1: return "8-byte align";
16892c23cb7cSEd Maste 	case 2: return "8-byte align and SP % 8 == 0";
16902c23cb7cSEd Maste 	case 3: return "Reserved";
16912c23cb7cSEd Maste 	default:
16922c23cb7cSEd Maste 		if (ap >= 4 && ap <= 12)
16932c23cb7cSEd Maste 			snprintf(s_align_p, sizeof(s_align_p), "8-byte align"
16942c23cb7cSEd Maste 			    " and SP %% 8 == 0 and up to 2^%ju-byte extended"
16952c23cb7cSEd Maste 			    " align", (uintmax_t) ap);
16962c23cb7cSEd Maste 		else
16972c23cb7cSEd Maste 			snprintf(s_align_p, sizeof(s_align_p), "Unknown (%ju)",
16982c23cb7cSEd Maste 			    (uintmax_t) ap);
16992c23cb7cSEd Maste 		return (s_align_p);
17002c23cb7cSEd Maste 	}
17012c23cb7cSEd Maste }
17022c23cb7cSEd Maste 
17032c23cb7cSEd Maste static const char *
aeabi_fp_rounding(uint64_t fr)17042c23cb7cSEd Maste aeabi_fp_rounding(uint64_t fr)
17052c23cb7cSEd Maste {
17062c23cb7cSEd Maste 	static char s_fp_r[32];
17072c23cb7cSEd Maste 
17082c23cb7cSEd Maste 	switch (fr) {
17092c23cb7cSEd Maste 	case 0: return "Unused";
17102c23cb7cSEd Maste 	case 1: return "Needed";
17112c23cb7cSEd Maste 	default:
17122c23cb7cSEd Maste 		snprintf(s_fp_r, sizeof(s_fp_r), "Unknown (%ju)",
17132c23cb7cSEd Maste 		    (uintmax_t) fr);
17142c23cb7cSEd Maste 		return (s_fp_r);
17152c23cb7cSEd Maste 	}
17162c23cb7cSEd Maste }
17172c23cb7cSEd Maste 
17182c23cb7cSEd Maste static const char *
aeabi_fp_denormal(uint64_t fd)17192c23cb7cSEd Maste aeabi_fp_denormal(uint64_t fd)
17202c23cb7cSEd Maste {
17212c23cb7cSEd Maste 	static char s_fp_d[32];
17222c23cb7cSEd Maste 
17232c23cb7cSEd Maste 	switch (fd) {
17242c23cb7cSEd Maste 	case 0: return "Unused";
17252c23cb7cSEd Maste 	case 1: return "Needed";
17262c23cb7cSEd Maste 	case 2: return "Sign Only";
17272c23cb7cSEd Maste 	default:
17282c23cb7cSEd Maste 		snprintf(s_fp_d, sizeof(s_fp_d), "Unknown (%ju)",
17292c23cb7cSEd Maste 		    (uintmax_t) fd);
17302c23cb7cSEd Maste 		return (s_fp_d);
17312c23cb7cSEd Maste 	}
17322c23cb7cSEd Maste }
17332c23cb7cSEd Maste 
17342c23cb7cSEd Maste static const char *
aeabi_fp_exceptions(uint64_t fe)17352c23cb7cSEd Maste aeabi_fp_exceptions(uint64_t fe)
17362c23cb7cSEd Maste {
17372c23cb7cSEd Maste 	static char s_fp_e[32];
17382c23cb7cSEd Maste 
17392c23cb7cSEd Maste 	switch (fe) {
17402c23cb7cSEd Maste 	case 0: return "Unused";
17412c23cb7cSEd Maste 	case 1: return "Needed";
17422c23cb7cSEd Maste 	default:
17432c23cb7cSEd Maste 		snprintf(s_fp_e, sizeof(s_fp_e), "Unknown (%ju)",
17442c23cb7cSEd Maste 		    (uintmax_t) fe);
17452c23cb7cSEd Maste 		return (s_fp_e);
17462c23cb7cSEd Maste 	}
17472c23cb7cSEd Maste }
17482c23cb7cSEd Maste 
17492c23cb7cSEd Maste static const char *
aeabi_fp_user_exceptions(uint64_t fu)17502c23cb7cSEd Maste aeabi_fp_user_exceptions(uint64_t fu)
17512c23cb7cSEd Maste {
17522c23cb7cSEd Maste 	static char s_fp_u[32];
17532c23cb7cSEd Maste 
17542c23cb7cSEd Maste 	switch (fu) {
17552c23cb7cSEd Maste 	case 0: return "Unused";
17562c23cb7cSEd Maste 	case 1: return "Needed";
17572c23cb7cSEd Maste 	default:
17582c23cb7cSEd Maste 		snprintf(s_fp_u, sizeof(s_fp_u), "Unknown (%ju)",
17592c23cb7cSEd Maste 		    (uintmax_t) fu);
17602c23cb7cSEd Maste 		return (s_fp_u);
17612c23cb7cSEd Maste 	}
17622c23cb7cSEd Maste }
17632c23cb7cSEd Maste 
17642c23cb7cSEd Maste static const char *
aeabi_fp_number_model(uint64_t fn)17652c23cb7cSEd Maste aeabi_fp_number_model(uint64_t fn)
17662c23cb7cSEd Maste {
17672c23cb7cSEd Maste 	static char s_fp_n[32];
17682c23cb7cSEd Maste 
17692c23cb7cSEd Maste 	switch (fn) {
17702c23cb7cSEd Maste 	case 0: return "Unused";
17712c23cb7cSEd Maste 	case 1: return "IEEE 754 normal";
17722c23cb7cSEd Maste 	case 2: return "RTABI";
17732c23cb7cSEd Maste 	case 3: return "IEEE 754";
17742c23cb7cSEd Maste 	default:
17752c23cb7cSEd Maste 		snprintf(s_fp_n, sizeof(s_fp_n), "Unknown (%ju)",
17762c23cb7cSEd Maste 		    (uintmax_t) fn);
17772c23cb7cSEd Maste 		return (s_fp_n);
17782c23cb7cSEd Maste 	}
17792c23cb7cSEd Maste }
17802c23cb7cSEd Maste 
17812c23cb7cSEd Maste static const char *
aeabi_fp_16bit_format(uint64_t fp16)17822c23cb7cSEd Maste aeabi_fp_16bit_format(uint64_t fp16)
17832c23cb7cSEd Maste {
17842c23cb7cSEd Maste 	static char s_fp_16[64];
17852c23cb7cSEd Maste 
17862c23cb7cSEd Maste 	switch (fp16) {
17872c23cb7cSEd Maste 	case 0: return "None";
17882c23cb7cSEd Maste 	case 1: return "IEEE 754";
17892c23cb7cSEd Maste 	case 2: return "VFPv3/Advanced SIMD (alternative format)";
17902c23cb7cSEd Maste 	default:
17912c23cb7cSEd Maste 		snprintf(s_fp_16, sizeof(s_fp_16), "Unknown (%ju)",
17922c23cb7cSEd Maste 		    (uintmax_t) fp16);
17932c23cb7cSEd Maste 		return (s_fp_16);
17942c23cb7cSEd Maste 	}
17952c23cb7cSEd Maste }
17962c23cb7cSEd Maste 
17972c23cb7cSEd Maste static const char *
aeabi_mpext(uint64_t mp)17982c23cb7cSEd Maste aeabi_mpext(uint64_t mp)
17992c23cb7cSEd Maste {
18002c23cb7cSEd Maste 	static char s_mp[32];
18012c23cb7cSEd Maste 
18022c23cb7cSEd Maste 	switch (mp) {
18032c23cb7cSEd Maste 	case 0: return "Not allowed";
18042c23cb7cSEd Maste 	case 1: return "Allowed";
18052c23cb7cSEd Maste 	default:
18062c23cb7cSEd Maste 		snprintf(s_mp, sizeof(s_mp), "Unknown (%ju)",
18072c23cb7cSEd Maste 		    (uintmax_t) mp);
18082c23cb7cSEd Maste 		return (s_mp);
18092c23cb7cSEd Maste 	}
18102c23cb7cSEd Maste }
18112c23cb7cSEd Maste 
18122c23cb7cSEd Maste static const char *
aeabi_div(uint64_t du)18132c23cb7cSEd Maste aeabi_div(uint64_t du)
18142c23cb7cSEd Maste {
18152c23cb7cSEd Maste 	static char s_du[32];
18162c23cb7cSEd Maste 
18172c23cb7cSEd Maste 	switch (du) {
18182c23cb7cSEd Maste 	case 0: return "Yes (V7-R/V7-M)";
18192c23cb7cSEd Maste 	case 1: return "No";
18202c23cb7cSEd Maste 	case 2: return "Yes (V7-A)";
18212c23cb7cSEd Maste 	default:
18222c23cb7cSEd Maste 		snprintf(s_du, sizeof(s_du), "Unknown (%ju)",
18232c23cb7cSEd Maste 		    (uintmax_t) du);
18242c23cb7cSEd Maste 		return (s_du);
18252c23cb7cSEd Maste 	}
18262c23cb7cSEd Maste }
18272c23cb7cSEd Maste 
18282c23cb7cSEd Maste static const char *
aeabi_t2ee(uint64_t t2ee)18292c23cb7cSEd Maste aeabi_t2ee(uint64_t t2ee)
18302c23cb7cSEd Maste {
18312c23cb7cSEd Maste 	static char s_t2ee[32];
18322c23cb7cSEd Maste 
18332c23cb7cSEd Maste 	switch (t2ee) {
18342c23cb7cSEd Maste 	case 0: return "Not allowed";
18352c23cb7cSEd Maste 	case 1: return "Allowed";
18362c23cb7cSEd Maste 	default:
18372c23cb7cSEd Maste 		snprintf(s_t2ee, sizeof(s_t2ee), "Unknown(%ju)",
18382c23cb7cSEd Maste 		    (uintmax_t) t2ee);
18392c23cb7cSEd Maste 		return (s_t2ee);
18402c23cb7cSEd Maste 	}
18412c23cb7cSEd Maste 
18422c23cb7cSEd Maste }
18432c23cb7cSEd Maste 
18442c23cb7cSEd Maste static const char *
aeabi_hardfp(uint64_t hfp)18452c23cb7cSEd Maste aeabi_hardfp(uint64_t hfp)
18462c23cb7cSEd Maste {
18472c23cb7cSEd Maste 	static char s_hfp[32];
18482c23cb7cSEd Maste 
18492c23cb7cSEd Maste 	switch (hfp) {
18502c23cb7cSEd Maste 	case 0: return "Tag_FP_arch";
18512c23cb7cSEd Maste 	case 1: return "only SP";
18522c23cb7cSEd Maste 	case 2: return "only DP";
18532c23cb7cSEd Maste 	case 3: return "both SP and DP";
18542c23cb7cSEd Maste 	default:
18552c23cb7cSEd Maste 		snprintf(s_hfp, sizeof(s_hfp), "Unknown (%ju)",
18562c23cb7cSEd Maste 		    (uintmax_t) hfp);
18572c23cb7cSEd Maste 		return (s_hfp);
18582c23cb7cSEd Maste 	}
18592c23cb7cSEd Maste }
18602c23cb7cSEd Maste 
18612c23cb7cSEd Maste static const char *
aeabi_vfp_args(uint64_t va)18622c23cb7cSEd Maste aeabi_vfp_args(uint64_t va)
18632c23cb7cSEd Maste {
18642c23cb7cSEd Maste 	static char s_va[32];
18652c23cb7cSEd Maste 
18662c23cb7cSEd Maste 	switch (va) {
18672c23cb7cSEd Maste 	case 0: return "AAPCS (base variant)";
18682c23cb7cSEd Maste 	case 1: return "AAPCS (VFP variant)";
18692c23cb7cSEd Maste 	case 2: return "toolchain-specific";
18702c23cb7cSEd Maste 	default:
18712c23cb7cSEd Maste 		snprintf(s_va, sizeof(s_va), "Unknown (%ju)", (uintmax_t) va);
18722c23cb7cSEd Maste 		return (s_va);
18732c23cb7cSEd Maste 	}
18742c23cb7cSEd Maste }
18752c23cb7cSEd Maste 
18762c23cb7cSEd Maste static const char *
aeabi_wmmx_args(uint64_t wa)18772c23cb7cSEd Maste aeabi_wmmx_args(uint64_t wa)
18782c23cb7cSEd Maste {
18792c23cb7cSEd Maste 	static char s_wa[32];
18802c23cb7cSEd Maste 
18812c23cb7cSEd Maste 	switch (wa) {
18822c23cb7cSEd Maste 	case 0: return "AAPCS (base variant)";
18832c23cb7cSEd Maste 	case 1: return "Intel WMMX";
18842c23cb7cSEd Maste 	case 2: return "toolchain-specific";
18852c23cb7cSEd Maste 	default:
18862c23cb7cSEd Maste 		snprintf(s_wa, sizeof(s_wa), "Unknown(%ju)", (uintmax_t) wa);
18872c23cb7cSEd Maste 		return (s_wa);
18882c23cb7cSEd Maste 	}
18892c23cb7cSEd Maste }
18902c23cb7cSEd Maste 
18912c23cb7cSEd Maste static const char *
aeabi_unaligned_access(uint64_t ua)18922c23cb7cSEd Maste aeabi_unaligned_access(uint64_t ua)
18932c23cb7cSEd Maste {
18942c23cb7cSEd Maste 	static char s_ua[32];
18952c23cb7cSEd Maste 
18962c23cb7cSEd Maste 	switch (ua) {
18972c23cb7cSEd Maste 	case 0: return "Not allowed";
18982c23cb7cSEd Maste 	case 1: return "Allowed";
18992c23cb7cSEd Maste 	default:
19002c23cb7cSEd Maste 		snprintf(s_ua, sizeof(s_ua), "Unknown(%ju)", (uintmax_t) ua);
19012c23cb7cSEd Maste 		return (s_ua);
19022c23cb7cSEd Maste 	}
19032c23cb7cSEd Maste }
19042c23cb7cSEd Maste 
19052c23cb7cSEd Maste static const char *
aeabi_fp_hpext(uint64_t fh)19062c23cb7cSEd Maste aeabi_fp_hpext(uint64_t fh)
19072c23cb7cSEd Maste {
19082c23cb7cSEd Maste 	static char s_fh[32];
19092c23cb7cSEd Maste 
19102c23cb7cSEd Maste 	switch (fh) {
19112c23cb7cSEd Maste 	case 0: return "Not allowed";
19122c23cb7cSEd Maste 	case 1: return "Allowed";
19132c23cb7cSEd Maste 	default:
19142c23cb7cSEd Maste 		snprintf(s_fh, sizeof(s_fh), "Unknown(%ju)", (uintmax_t) fh);
19152c23cb7cSEd Maste 		return (s_fh);
19162c23cb7cSEd Maste 	}
19172c23cb7cSEd Maste }
19182c23cb7cSEd Maste 
19192c23cb7cSEd Maste static const char *
aeabi_optm_goal(uint64_t og)19202c23cb7cSEd Maste aeabi_optm_goal(uint64_t og)
19212c23cb7cSEd Maste {
19222c23cb7cSEd Maste 	static char s_og[32];
19232c23cb7cSEd Maste 
19242c23cb7cSEd Maste 	switch (og) {
19252c23cb7cSEd Maste 	case 0: return "None";
19262c23cb7cSEd Maste 	case 1: return "Speed";
19272c23cb7cSEd Maste 	case 2: return "Speed aggressive";
19282c23cb7cSEd Maste 	case 3: return "Space";
19292c23cb7cSEd Maste 	case 4: return "Space aggressive";
19302c23cb7cSEd Maste 	case 5: return "Debugging";
19312c23cb7cSEd Maste 	case 6: return "Best Debugging";
19322c23cb7cSEd Maste 	default:
19332c23cb7cSEd Maste 		snprintf(s_og, sizeof(s_og), "Unknown(%ju)", (uintmax_t) og);
19342c23cb7cSEd Maste 		return (s_og);
19352c23cb7cSEd Maste 	}
19362c23cb7cSEd Maste }
19372c23cb7cSEd Maste 
19382c23cb7cSEd Maste static const char *
aeabi_fp_optm_goal(uint64_t fog)19392c23cb7cSEd Maste aeabi_fp_optm_goal(uint64_t fog)
19402c23cb7cSEd Maste {
19412c23cb7cSEd Maste 	static char s_fog[32];
19422c23cb7cSEd Maste 
19432c23cb7cSEd Maste 	switch (fog) {
19442c23cb7cSEd Maste 	case 0: return "None";
19452c23cb7cSEd Maste 	case 1: return "Speed";
19462c23cb7cSEd Maste 	case 2: return "Speed aggressive";
19472c23cb7cSEd Maste 	case 3: return "Space";
19482c23cb7cSEd Maste 	case 4: return "Space aggressive";
19492c23cb7cSEd Maste 	case 5: return "Accurary";
19502c23cb7cSEd Maste 	case 6: return "Best Accurary";
19512c23cb7cSEd Maste 	default:
19522c23cb7cSEd Maste 		snprintf(s_fog, sizeof(s_fog), "Unknown(%ju)",
19532c23cb7cSEd Maste 		    (uintmax_t) fog);
19542c23cb7cSEd Maste 		return (s_fog);
19552c23cb7cSEd Maste 	}
19562c23cb7cSEd Maste }
19572c23cb7cSEd Maste 
19582c23cb7cSEd Maste static const char *
aeabi_virtual(uint64_t vt)19592c23cb7cSEd Maste aeabi_virtual(uint64_t vt)
19602c23cb7cSEd Maste {
19612c23cb7cSEd Maste 	static char s_virtual[64];
19622c23cb7cSEd Maste 
19632c23cb7cSEd Maste 	switch (vt) {
19642c23cb7cSEd Maste 	case 0: return "No";
19652c23cb7cSEd Maste 	case 1: return "TrustZone";
19662c23cb7cSEd Maste 	case 2: return "Virtualization extension";
19672c23cb7cSEd Maste 	case 3: return "TrustZone and virtualization extension";
19682c23cb7cSEd Maste 	default:
19692c23cb7cSEd Maste 		snprintf(s_virtual, sizeof(s_virtual), "Unknown(%ju)",
19702c23cb7cSEd Maste 		    (uintmax_t) vt);
19712c23cb7cSEd Maste 		return (s_virtual);
19722c23cb7cSEd Maste 	}
19732c23cb7cSEd Maste }
19742c23cb7cSEd Maste 
19752c23cb7cSEd Maste static struct {
19762c23cb7cSEd Maste 	uint64_t tag;
19772c23cb7cSEd Maste 	const char *s_tag;
19782c23cb7cSEd Maste 	const char *(*get_desc)(uint64_t val);
19792c23cb7cSEd Maste } aeabi_tags[] = {
19802c23cb7cSEd Maste 	{4, "Tag_CPU_raw_name", NULL},
19812c23cb7cSEd Maste 	{5, "Tag_CPU_name", NULL},
19822c23cb7cSEd Maste 	{6, "Tag_CPU_arch", aeabi_cpu_arch},
19832c23cb7cSEd Maste 	{7, "Tag_CPU_arch_profile", aeabi_cpu_arch_profile},
19842c23cb7cSEd Maste 	{8, "Tag_ARM_ISA_use", aeabi_arm_isa},
19852c23cb7cSEd Maste 	{9, "Tag_THUMB_ISA_use", aeabi_thumb_isa},
19862c23cb7cSEd Maste 	{10, "Tag_FP_arch", aeabi_fp_arch},
19872c23cb7cSEd Maste 	{11, "Tag_WMMX_arch", aeabi_wmmx_arch},
19882c23cb7cSEd Maste 	{12, "Tag_Advanced_SIMD_arch", aeabi_adv_simd_arch},
19892c23cb7cSEd Maste 	{13, "Tag_PCS_config", aeabi_pcs_config},
19902c23cb7cSEd Maste 	{14, "Tag_ABI_PCS_R9_use", aeabi_pcs_r9},
19912c23cb7cSEd Maste 	{15, "Tag_ABI_PCS_RW_data", aeabi_pcs_rw},
19922c23cb7cSEd Maste 	{16, "Tag_ABI_PCS_RO_data", aeabi_pcs_ro},
19932c23cb7cSEd Maste 	{17, "Tag_ABI_PCS_GOT_use", aeabi_pcs_got},
19942c23cb7cSEd Maste 	{18, "Tag_ABI_PCS_wchar_t", aeabi_pcs_wchar_t},
19952c23cb7cSEd Maste 	{19, "Tag_ABI_FP_rounding", aeabi_fp_rounding},
19962c23cb7cSEd Maste 	{20, "Tag_ABI_FP_denormal", aeabi_fp_denormal},
19972c23cb7cSEd Maste 	{21, "Tag_ABI_FP_exceptions", aeabi_fp_exceptions},
19982c23cb7cSEd Maste 	{22, "Tag_ABI_FP_user_exceptions", aeabi_fp_user_exceptions},
19992c23cb7cSEd Maste 	{23, "Tag_ABI_FP_number_model", aeabi_fp_number_model},
20002c23cb7cSEd Maste 	{24, "Tag_ABI_align_needed", aeabi_align_needed},
20012c23cb7cSEd Maste 	{25, "Tag_ABI_align_preserved", aeabi_align_preserved},
20022c23cb7cSEd Maste 	{26, "Tag_ABI_enum_size", aeabi_enum_size},
20032c23cb7cSEd Maste 	{27, "Tag_ABI_HardFP_use", aeabi_hardfp},
20042c23cb7cSEd Maste 	{28, "Tag_ABI_VFP_args", aeabi_vfp_args},
20052c23cb7cSEd Maste 	{29, "Tag_ABI_WMMX_args", aeabi_wmmx_args},
20062c23cb7cSEd Maste 	{30, "Tag_ABI_optimization_goals", aeabi_optm_goal},
20072c23cb7cSEd Maste 	{31, "Tag_ABI_FP_optimization_goals", aeabi_fp_optm_goal},
20082c23cb7cSEd Maste 	{32, "Tag_compatibility", NULL},
20092c23cb7cSEd Maste 	{34, "Tag_CPU_unaligned_access", aeabi_unaligned_access},
20102c23cb7cSEd Maste 	{36, "Tag_FP_HP_extension", aeabi_fp_hpext},
20112c23cb7cSEd Maste 	{38, "Tag_ABI_FP_16bit_format", aeabi_fp_16bit_format},
20122c23cb7cSEd Maste 	{42, "Tag_MPextension_use", aeabi_mpext},
20132c23cb7cSEd Maste 	{44, "Tag_DIV_use", aeabi_div},
20142c23cb7cSEd Maste 	{64, "Tag_nodefaults", NULL},
20152c23cb7cSEd Maste 	{65, "Tag_also_compatible_with", NULL},
20162c23cb7cSEd Maste 	{66, "Tag_T2EE_use", aeabi_t2ee},
20172c23cb7cSEd Maste 	{67, "Tag_conformance", NULL},
20182c23cb7cSEd Maste 	{68, "Tag_Virtualization_use", aeabi_virtual},
20192c23cb7cSEd Maste 	{70, "Tag_MPextension_use", aeabi_mpext},
20202c23cb7cSEd Maste };
20212c23cb7cSEd Maste 
20222c23cb7cSEd Maste static const char *
mips_abi_fp(uint64_t fp)20232c23cb7cSEd Maste mips_abi_fp(uint64_t fp)
20242c23cb7cSEd Maste {
20252c23cb7cSEd Maste 	static char s_mips_abi_fp[64];
20262c23cb7cSEd Maste 
20272c23cb7cSEd Maste 	switch (fp) {
20282c23cb7cSEd Maste 	case 0: return "N/A";
20292c23cb7cSEd Maste 	case 1: return "Hard float (double precision)";
20302c23cb7cSEd Maste 	case 2: return "Hard float (single precision)";
20312c23cb7cSEd Maste 	case 3: return "Soft float";
20322c23cb7cSEd Maste 	case 4: return "64-bit float (-mips32r2 -mfp64)";
20332c23cb7cSEd Maste 	default:
20342c23cb7cSEd Maste 		snprintf(s_mips_abi_fp, sizeof(s_mips_abi_fp), "Unknown(%ju)",
20352c23cb7cSEd Maste 		    (uintmax_t) fp);
20362c23cb7cSEd Maste 		return (s_mips_abi_fp);
20372c23cb7cSEd Maste 	}
20382c23cb7cSEd Maste }
20392c23cb7cSEd Maste 
20402c23cb7cSEd Maste static const char *
ppc_abi_fp(uint64_t fp)20412c23cb7cSEd Maste ppc_abi_fp(uint64_t fp)
20422c23cb7cSEd Maste {
20432c23cb7cSEd Maste 	static char s_ppc_abi_fp[64];
20442c23cb7cSEd Maste 
20452c23cb7cSEd Maste 	switch (fp) {
20462c23cb7cSEd Maste 	case 0: return "N/A";
20472c23cb7cSEd Maste 	case 1: return "Hard float (double precision)";
20482c23cb7cSEd Maste 	case 2: return "Soft float";
20492c23cb7cSEd Maste 	case 3: return "Hard float (single precision)";
20502c23cb7cSEd Maste 	default:
20512c23cb7cSEd Maste 		snprintf(s_ppc_abi_fp, sizeof(s_ppc_abi_fp), "Unknown(%ju)",
20522c23cb7cSEd Maste 		    (uintmax_t) fp);
20532c23cb7cSEd Maste 		return (s_ppc_abi_fp);
20542c23cb7cSEd Maste 	}
20552c23cb7cSEd Maste }
20562c23cb7cSEd Maste 
20572c23cb7cSEd Maste static const char *
ppc_abi_vector(uint64_t vec)20582c23cb7cSEd Maste ppc_abi_vector(uint64_t vec)
20592c23cb7cSEd Maste {
20602c23cb7cSEd Maste 	static char s_vec[64];
20612c23cb7cSEd Maste 
20622c23cb7cSEd Maste 	switch (vec) {
20632c23cb7cSEd Maste 	case 0: return "N/A";
20642c23cb7cSEd Maste 	case 1: return "Generic purpose registers";
20652c23cb7cSEd Maste 	case 2: return "AltiVec registers";
20662c23cb7cSEd Maste 	case 3: return "SPE registers";
20672c23cb7cSEd Maste 	default:
20682c23cb7cSEd Maste 		snprintf(s_vec, sizeof(s_vec), "Unknown(%ju)", (uintmax_t) vec);
20692c23cb7cSEd Maste 		return (s_vec);
20702c23cb7cSEd Maste 	}
20712c23cb7cSEd Maste }
20722c23cb7cSEd Maste 
2073cf781b2eSEd Maste static const char *
dwarf_reg(unsigned int mach,unsigned int reg)2074cf781b2eSEd Maste dwarf_reg(unsigned int mach, unsigned int reg)
2075cf781b2eSEd Maste {
2076cf781b2eSEd Maste 
2077cf781b2eSEd Maste 	switch (mach) {
2078cf781b2eSEd Maste 	case EM_386:
20793ef90571SEd Maste 	case EM_IAMCU:
2080cf781b2eSEd Maste 		switch (reg) {
2081cf781b2eSEd Maste 		case 0: return "eax";
2082cf781b2eSEd Maste 		case 1: return "ecx";
2083cf781b2eSEd Maste 		case 2: return "edx";
2084cf781b2eSEd Maste 		case 3: return "ebx";
2085cf781b2eSEd Maste 		case 4: return "esp";
2086cf781b2eSEd Maste 		case 5: return "ebp";
2087cf781b2eSEd Maste 		case 6: return "esi";
2088cf781b2eSEd Maste 		case 7: return "edi";
2089cf781b2eSEd Maste 		case 8: return "eip";
2090cf781b2eSEd Maste 		case 9: return "eflags";
2091cf781b2eSEd Maste 		case 11: return "st0";
2092cf781b2eSEd Maste 		case 12: return "st1";
2093cf781b2eSEd Maste 		case 13: return "st2";
2094cf781b2eSEd Maste 		case 14: return "st3";
2095cf781b2eSEd Maste 		case 15: return "st4";
2096cf781b2eSEd Maste 		case 16: return "st5";
2097cf781b2eSEd Maste 		case 17: return "st6";
2098cf781b2eSEd Maste 		case 18: return "st7";
2099cf781b2eSEd Maste 		case 21: return "xmm0";
2100cf781b2eSEd Maste 		case 22: return "xmm1";
2101cf781b2eSEd Maste 		case 23: return "xmm2";
2102cf781b2eSEd Maste 		case 24: return "xmm3";
2103cf781b2eSEd Maste 		case 25: return "xmm4";
2104cf781b2eSEd Maste 		case 26: return "xmm5";
2105cf781b2eSEd Maste 		case 27: return "xmm6";
2106cf781b2eSEd Maste 		case 28: return "xmm7";
2107cf781b2eSEd Maste 		case 29: return "mm0";
2108cf781b2eSEd Maste 		case 30: return "mm1";
2109cf781b2eSEd Maste 		case 31: return "mm2";
2110cf781b2eSEd Maste 		case 32: return "mm3";
2111cf781b2eSEd Maste 		case 33: return "mm4";
2112cf781b2eSEd Maste 		case 34: return "mm5";
2113cf781b2eSEd Maste 		case 35: return "mm6";
2114cf781b2eSEd Maste 		case 36: return "mm7";
2115cf781b2eSEd Maste 		case 37: return "fcw";
2116cf781b2eSEd Maste 		case 38: return "fsw";
2117cf781b2eSEd Maste 		case 39: return "mxcsr";
2118cf781b2eSEd Maste 		case 40: return "es";
2119cf781b2eSEd Maste 		case 41: return "cs";
2120cf781b2eSEd Maste 		case 42: return "ss";
2121cf781b2eSEd Maste 		case 43: return "ds";
2122cf781b2eSEd Maste 		case 44: return "fs";
2123cf781b2eSEd Maste 		case 45: return "gs";
2124cf781b2eSEd Maste 		case 48: return "tr";
2125cf781b2eSEd Maste 		case 49: return "ldtr";
2126cf781b2eSEd Maste 		default: return (NULL);
2127cf781b2eSEd Maste 		}
21281a0c2201SMitchell Horne 	case EM_RISCV:
21291a0c2201SMitchell Horne 		switch (reg) {
21301a0c2201SMitchell Horne 		case 0: return "zero";
21311a0c2201SMitchell Horne 		case 1: return "ra";
21321a0c2201SMitchell Horne 		case 2: return "sp";
21331a0c2201SMitchell Horne 		case 3: return "gp";
21341a0c2201SMitchell Horne 		case 4: return "tp";
21351a0c2201SMitchell Horne 		case 5: return "t0";
21361a0c2201SMitchell Horne 		case 6: return "t1";
21371a0c2201SMitchell Horne 		case 7: return "t2";
21381a0c2201SMitchell Horne 		case 8: return "s0";
21391a0c2201SMitchell Horne 		case 9: return "s1";
21401a0c2201SMitchell Horne 		case 10: return "a0";
21411a0c2201SMitchell Horne 		case 11: return "a1";
21421a0c2201SMitchell Horne 		case 12: return "a2";
21431a0c2201SMitchell Horne 		case 13: return "a3";
21441a0c2201SMitchell Horne 		case 14: return "a4";
21451a0c2201SMitchell Horne 		case 15: return "a5";
21461a0c2201SMitchell Horne 		case 16: return "a6";
21471a0c2201SMitchell Horne 		case 17: return "a7";
21481a0c2201SMitchell Horne 		case 18: return "s2";
21491a0c2201SMitchell Horne 		case 19: return "s3";
21501a0c2201SMitchell Horne 		case 20: return "s4";
21511a0c2201SMitchell Horne 		case 21: return "s5";
21521a0c2201SMitchell Horne 		case 22: return "s6";
21531a0c2201SMitchell Horne 		case 23: return "s7";
21541a0c2201SMitchell Horne 		case 24: return "s8";
21551a0c2201SMitchell Horne 		case 25: return "s9";
21561a0c2201SMitchell Horne 		case 26: return "s10";
21571a0c2201SMitchell Horne 		case 27: return "s11";
21581a0c2201SMitchell Horne 		case 28: return "t3";
21591a0c2201SMitchell Horne 		case 29: return "t4";
21601a0c2201SMitchell Horne 		case 30: return "t5";
21611a0c2201SMitchell Horne 		case 31: return "t6";
21621a0c2201SMitchell Horne 		case 32: return "ft0";
21631a0c2201SMitchell Horne 		case 33: return "ft1";
21641a0c2201SMitchell Horne 		case 34: return "ft2";
21651a0c2201SMitchell Horne 		case 35: return "ft3";
21661a0c2201SMitchell Horne 		case 36: return "ft4";
21671a0c2201SMitchell Horne 		case 37: return "ft5";
21681a0c2201SMitchell Horne 		case 38: return "ft6";
21691a0c2201SMitchell Horne 		case 39: return "ft7";
21701a0c2201SMitchell Horne 		case 40: return "fs0";
21711a0c2201SMitchell Horne 		case 41: return "fs1";
21721a0c2201SMitchell Horne 		case 42: return "fa0";
21731a0c2201SMitchell Horne 		case 43: return "fa1";
21741a0c2201SMitchell Horne 		case 44: return "fa2";
21751a0c2201SMitchell Horne 		case 45: return "fa3";
21761a0c2201SMitchell Horne 		case 46: return "fa4";
21771a0c2201SMitchell Horne 		case 47: return "fa5";
21781a0c2201SMitchell Horne 		case 48: return "fa6";
21791a0c2201SMitchell Horne 		case 49: return "fa7";
21801a0c2201SMitchell Horne 		case 50: return "fs2";
21811a0c2201SMitchell Horne 		case 51: return "fs3";
21821a0c2201SMitchell Horne 		case 52: return "fs4";
21831a0c2201SMitchell Horne 		case 53: return "fs5";
21841a0c2201SMitchell Horne 		case 54: return "fs6";
21851a0c2201SMitchell Horne 		case 55: return "fs7";
21861a0c2201SMitchell Horne 		case 56: return "fs8";
21871a0c2201SMitchell Horne 		case 57: return "fs9";
21881a0c2201SMitchell Horne 		case 58: return "fs10";
21891a0c2201SMitchell Horne 		case 59: return "fs11";
21901a0c2201SMitchell Horne 		case 60: return "ft8";
21911a0c2201SMitchell Horne 		case 61: return "ft9";
21921a0c2201SMitchell Horne 		case 62: return "ft10";
21931a0c2201SMitchell Horne 		case 63: return "ft11";
21941a0c2201SMitchell Horne 		default: return (NULL);
21951a0c2201SMitchell Horne 		}
2196cf781b2eSEd Maste 	case EM_X86_64:
2197cf781b2eSEd Maste 		switch (reg) {
2198cf781b2eSEd Maste 		case 0: return "rax";
2199cf781b2eSEd Maste 		case 1: return "rdx";
2200cf781b2eSEd Maste 		case 2: return "rcx";
2201cf781b2eSEd Maste 		case 3: return "rbx";
2202cf781b2eSEd Maste 		case 4: return "rsi";
2203cf781b2eSEd Maste 		case 5: return "rdi";
2204cf781b2eSEd Maste 		case 6: return "rbp";
2205cf781b2eSEd Maste 		case 7: return "rsp";
2206cf781b2eSEd Maste 		case 16: return "rip";
2207cf781b2eSEd Maste 		case 17: return "xmm0";
2208cf781b2eSEd Maste 		case 18: return "xmm1";
2209cf781b2eSEd Maste 		case 19: return "xmm2";
2210cf781b2eSEd Maste 		case 20: return "xmm3";
2211cf781b2eSEd Maste 		case 21: return "xmm4";
2212cf781b2eSEd Maste 		case 22: return "xmm5";
2213cf781b2eSEd Maste 		case 23: return "xmm6";
2214cf781b2eSEd Maste 		case 24: return "xmm7";
2215cf781b2eSEd Maste 		case 25: return "xmm8";
2216cf781b2eSEd Maste 		case 26: return "xmm9";
2217cf781b2eSEd Maste 		case 27: return "xmm10";
2218cf781b2eSEd Maste 		case 28: return "xmm11";
2219cf781b2eSEd Maste 		case 29: return "xmm12";
2220cf781b2eSEd Maste 		case 30: return "xmm13";
2221cf781b2eSEd Maste 		case 31: return "xmm14";
2222cf781b2eSEd Maste 		case 32: return "xmm15";
2223cf781b2eSEd Maste 		case 33: return "st0";
2224cf781b2eSEd Maste 		case 34: return "st1";
2225cf781b2eSEd Maste 		case 35: return "st2";
2226cf781b2eSEd Maste 		case 36: return "st3";
2227cf781b2eSEd Maste 		case 37: return "st4";
2228cf781b2eSEd Maste 		case 38: return "st5";
2229cf781b2eSEd Maste 		case 39: return "st6";
2230cf781b2eSEd Maste 		case 40: return "st7";
2231cf781b2eSEd Maste 		case 41: return "mm0";
2232cf781b2eSEd Maste 		case 42: return "mm1";
2233cf781b2eSEd Maste 		case 43: return "mm2";
2234cf781b2eSEd Maste 		case 44: return "mm3";
2235cf781b2eSEd Maste 		case 45: return "mm4";
2236cf781b2eSEd Maste 		case 46: return "mm5";
2237cf781b2eSEd Maste 		case 47: return "mm6";
2238cf781b2eSEd Maste 		case 48: return "mm7";
2239cf781b2eSEd Maste 		case 49: return "rflags";
2240cf781b2eSEd Maste 		case 50: return "es";
2241cf781b2eSEd Maste 		case 51: return "cs";
2242cf781b2eSEd Maste 		case 52: return "ss";
2243cf781b2eSEd Maste 		case 53: return "ds";
2244cf781b2eSEd Maste 		case 54: return "fs";
2245cf781b2eSEd Maste 		case 55: return "gs";
2246cf781b2eSEd Maste 		case 58: return "fs.base";
2247cf781b2eSEd Maste 		case 59: return "gs.base";
2248cf781b2eSEd Maste 		case 62: return "tr";
2249cf781b2eSEd Maste 		case 63: return "ldtr";
2250cf781b2eSEd Maste 		case 64: return "mxcsr";
2251cf781b2eSEd Maste 		case 65: return "fcw";
2252cf781b2eSEd Maste 		case 66: return "fsw";
2253cf781b2eSEd Maste 		default: return (NULL);
2254cf781b2eSEd Maste 		}
2255cf781b2eSEd Maste 	default:
2256cf781b2eSEd Maste 		return (NULL);
2257cf781b2eSEd Maste 	}
2258cf781b2eSEd Maste }
2259cf781b2eSEd Maste 
22602c23cb7cSEd Maste static void
dump_ehdr(struct readelf * re)22612c23cb7cSEd Maste dump_ehdr(struct readelf *re)
22622c23cb7cSEd Maste {
2263714935beSConrad Meyer 	size_t		 phnum, shnum, shstrndx;
22642c23cb7cSEd Maste 	int		 i;
22652c23cb7cSEd Maste 
22662c23cb7cSEd Maste 	printf("ELF Header:\n");
22672c23cb7cSEd Maste 
22682c23cb7cSEd Maste 	/* e_ident[]. */
22692c23cb7cSEd Maste 	printf("  Magic:   ");
22702c23cb7cSEd Maste 	for (i = 0; i < EI_NIDENT; i++)
22712c23cb7cSEd Maste 		printf("%.2x ", re->ehdr.e_ident[i]);
22722c23cb7cSEd Maste 	putchar('\n');
22732c23cb7cSEd Maste 
22742c23cb7cSEd Maste 	/* EI_CLASS. */
22752c23cb7cSEd Maste 	printf("%-37s%s\n", "  Class:", elf_class(re->ehdr.e_ident[EI_CLASS]));
22762c23cb7cSEd Maste 
22772c23cb7cSEd Maste 	/* EI_DATA. */
22782c23cb7cSEd Maste 	printf("%-37s%s\n", "  Data:", elf_endian(re->ehdr.e_ident[EI_DATA]));
22792c23cb7cSEd Maste 
22802c23cb7cSEd Maste 	/* EI_VERSION. */
22812c23cb7cSEd Maste 	printf("%-37s%d %s\n", "  Version:", re->ehdr.e_ident[EI_VERSION],
22822c23cb7cSEd Maste 	    elf_ver(re->ehdr.e_ident[EI_VERSION]));
22832c23cb7cSEd Maste 
22842c23cb7cSEd Maste 	/* EI_OSABI. */
22852c23cb7cSEd Maste 	printf("%-37s%s\n", "  OS/ABI:", elf_osabi(re->ehdr.e_ident[EI_OSABI]));
22862c23cb7cSEd Maste 
22872c23cb7cSEd Maste 	/* EI_ABIVERSION. */
22882c23cb7cSEd Maste 	printf("%-37s%d\n", "  ABI Version:", re->ehdr.e_ident[EI_ABIVERSION]);
22892c23cb7cSEd Maste 
22902c23cb7cSEd Maste 	/* e_type. */
22912c23cb7cSEd Maste 	printf("%-37s%s\n", "  Type:", elf_type(re->ehdr.e_type));
22922c23cb7cSEd Maste 
22932c23cb7cSEd Maste 	/* e_machine. */
22942c23cb7cSEd Maste 	printf("%-37s%s\n", "  Machine:", elf_machine(re->ehdr.e_machine));
22952c23cb7cSEd Maste 
22962c23cb7cSEd Maste 	/* e_version. */
22972c23cb7cSEd Maste 	printf("%-37s%#x\n", "  Version:", re->ehdr.e_version);
22982c23cb7cSEd Maste 
22992c23cb7cSEd Maste 	/* e_entry. */
23002c23cb7cSEd Maste 	printf("%-37s%#jx\n", "  Entry point address:",
23012c23cb7cSEd Maste 	    (uintmax_t)re->ehdr.e_entry);
23022c23cb7cSEd Maste 
23032c23cb7cSEd Maste 	/* e_phoff. */
23042c23cb7cSEd Maste 	printf("%-37s%ju (bytes into file)\n", "  Start of program headers:",
23052c23cb7cSEd Maste 	    (uintmax_t)re->ehdr.e_phoff);
23062c23cb7cSEd Maste 
23072c23cb7cSEd Maste 	/* e_shoff. */
23082c23cb7cSEd Maste 	printf("%-37s%ju (bytes into file)\n", "  Start of section headers:",
23092c23cb7cSEd Maste 	    (uintmax_t)re->ehdr.e_shoff);
23102c23cb7cSEd Maste 
23112c23cb7cSEd Maste 	/* e_flags. */
23122c23cb7cSEd Maste 	printf("%-37s%#x", "  Flags:", re->ehdr.e_flags);
23132c23cb7cSEd Maste 	dump_eflags(re, re->ehdr.e_flags);
23142c23cb7cSEd Maste 	putchar('\n');
23152c23cb7cSEd Maste 
23162c23cb7cSEd Maste 	/* e_ehsize. */
23172c23cb7cSEd Maste 	printf("%-37s%u (bytes)\n", "  Size of this header:",
23182c23cb7cSEd Maste 	    re->ehdr.e_ehsize);
23192c23cb7cSEd Maste 
23202c23cb7cSEd Maste 	/* e_phentsize. */
23212c23cb7cSEd Maste 	printf("%-37s%u (bytes)\n", "  Size of program headers:",
23222c23cb7cSEd Maste 	    re->ehdr.e_phentsize);
23232c23cb7cSEd Maste 
23242c23cb7cSEd Maste 	/* e_phnum. */
2325714935beSConrad Meyer 	printf("%-37s%u", "  Number of program headers:", re->ehdr.e_phnum);
2326714935beSConrad Meyer 	if (re->ehdr.e_phnum == PN_XNUM) {
2327714935beSConrad Meyer 		/* Extended program header numbering is in use. */
2328714935beSConrad Meyer 		if (elf_getphnum(re->elf, &phnum))
2329714935beSConrad Meyer 			printf(" (%zu)", phnum);
2330714935beSConrad Meyer 	}
2331714935beSConrad Meyer 	putchar('\n');
23322c23cb7cSEd Maste 
23332c23cb7cSEd Maste 	/* e_shentsize. */
23342c23cb7cSEd Maste 	printf("%-37s%u (bytes)\n", "  Size of section headers:",
23352c23cb7cSEd Maste 	    re->ehdr.e_shentsize);
23362c23cb7cSEd Maste 
23372c23cb7cSEd Maste 	/* e_shnum. */
23382c23cb7cSEd Maste 	printf("%-37s%u", "  Number of section headers:", re->ehdr.e_shnum);
23392c23cb7cSEd Maste 	if (re->ehdr.e_shnum == SHN_UNDEF) {
23402c23cb7cSEd Maste 		/* Extended section numbering is in use. */
23412c23cb7cSEd Maste 		if (elf_getshnum(re->elf, &shnum))
23422c23cb7cSEd Maste 			printf(" (%ju)", (uintmax_t)shnum);
23432c23cb7cSEd Maste 	}
23442c23cb7cSEd Maste 	putchar('\n');
23452c23cb7cSEd Maste 
23462c23cb7cSEd Maste 	/* e_shstrndx. */
23472c23cb7cSEd Maste 	printf("%-37s%u", "  Section header string table index:",
23482c23cb7cSEd Maste 	    re->ehdr.e_shstrndx);
23492c23cb7cSEd Maste 	if (re->ehdr.e_shstrndx == SHN_XINDEX) {
23502c23cb7cSEd Maste 		/* Extended section numbering is in use. */
23512c23cb7cSEd Maste 		if (elf_getshstrndx(re->elf, &shstrndx))
23522c23cb7cSEd Maste 			printf(" (%ju)", (uintmax_t)shstrndx);
23532c23cb7cSEd Maste 	}
23542c23cb7cSEd Maste 	putchar('\n');
23552c23cb7cSEd Maste }
23562c23cb7cSEd Maste 
23572c23cb7cSEd Maste static void
dump_eflags(struct readelf * re,uint64_t e_flags)23582c23cb7cSEd Maste dump_eflags(struct readelf *re, uint64_t e_flags)
23592c23cb7cSEd Maste {
23602c23cb7cSEd Maste 	struct eflags_desc *edesc;
23612c23cb7cSEd Maste 	int arm_eabi;
23622c23cb7cSEd Maste 
23632c23cb7cSEd Maste 	edesc = NULL;
23642c23cb7cSEd Maste 	switch (re->ehdr.e_machine) {
23652c23cb7cSEd Maste 	case EM_ARM:
23662c23cb7cSEd Maste 		arm_eabi = (e_flags & EF_ARM_EABIMASK) >> 24;
23672c23cb7cSEd Maste 		if (arm_eabi == 0)
23682c23cb7cSEd Maste 			printf(", GNU EABI");
23692c23cb7cSEd Maste 		else if (arm_eabi <= 5)
23702c23cb7cSEd Maste 			printf(", Version%d EABI", arm_eabi);
23712c23cb7cSEd Maste 		edesc = arm_eflags_desc;
23722c23cb7cSEd Maste 		break;
23732c23cb7cSEd Maste 	case EM_MIPS:
23742c23cb7cSEd Maste 	case EM_MIPS_RS3_LE:
23752c23cb7cSEd Maste 		switch ((e_flags & EF_MIPS_ARCH) >> 28) {
23762c23cb7cSEd Maste 		case 0:	printf(", mips1"); break;
23772c23cb7cSEd Maste 		case 1: printf(", mips2"); break;
23782c23cb7cSEd Maste 		case 2: printf(", mips3"); break;
23792c23cb7cSEd Maste 		case 3: printf(", mips4"); break;
23802c23cb7cSEd Maste 		case 4: printf(", mips5"); break;
23812c23cb7cSEd Maste 		case 5: printf(", mips32"); break;
23822c23cb7cSEd Maste 		case 6: printf(", mips64"); break;
23832c23cb7cSEd Maste 		case 7: printf(", mips32r2"); break;
23842c23cb7cSEd Maste 		case 8: printf(", mips64r2"); break;
23852c23cb7cSEd Maste 		default: break;
23862c23cb7cSEd Maste 		}
23872c23cb7cSEd Maste 		switch ((e_flags & 0x00FF0000) >> 16) {
23882c23cb7cSEd Maste 		case 0x81: printf(", 3900"); break;
23892c23cb7cSEd Maste 		case 0x82: printf(", 4010"); break;
23902c23cb7cSEd Maste 		case 0x83: printf(", 4100"); break;
23912c23cb7cSEd Maste 		case 0x85: printf(", 4650"); break;
23922c23cb7cSEd Maste 		case 0x87: printf(", 4120"); break;
23932c23cb7cSEd Maste 		case 0x88: printf(", 4111"); break;
23942c23cb7cSEd Maste 		case 0x8a: printf(", sb1"); break;
23952c23cb7cSEd Maste 		case 0x8b: printf(", octeon"); break;
23962c23cb7cSEd Maste 		case 0x8c: printf(", xlr"); break;
23972c23cb7cSEd Maste 		case 0x91: printf(", 5400"); break;
23982c23cb7cSEd Maste 		case 0x98: printf(", 5500"); break;
23992c23cb7cSEd Maste 		case 0x99: printf(", 9000"); break;
24002c23cb7cSEd Maste 		case 0xa0: printf(", loongson-2e"); break;
24012c23cb7cSEd Maste 		case 0xa1: printf(", loongson-2f"); break;
24022c23cb7cSEd Maste 		default: break;
24032c23cb7cSEd Maste 		}
24042c23cb7cSEd Maste 		switch ((e_flags & 0x0000F000) >> 12) {
24052c23cb7cSEd Maste 		case 1: printf(", o32"); break;
24062c23cb7cSEd Maste 		case 2: printf(", o64"); break;
24072c23cb7cSEd Maste 		case 3: printf(", eabi32"); break;
24082c23cb7cSEd Maste 		case 4: printf(", eabi64"); break;
24092c23cb7cSEd Maste 		default: break;
24102c23cb7cSEd Maste 		}
24112c23cb7cSEd Maste 		edesc = mips_eflags_desc;
24122c23cb7cSEd Maste 		break;
24132c23cb7cSEd Maste 	case EM_PPC64:
2414857e20a2SLeandro Lupori 		switch (e_flags) {
2415857e20a2SLeandro Lupori 		case 0: printf(", Unspecified or Power ELF V1 ABI"); break;
2416857e20a2SLeandro Lupori 		case 1: printf(", Power ELF V1 ABI"); break;
2417857e20a2SLeandro Lupori 		case 2: printf(", OpenPOWER ELF V2 ABI"); break;
2418857e20a2SLeandro Lupori 		default: break;
2419857e20a2SLeandro Lupori 		}
2420d003e0d7SEd Maste 		/* FALLTHROUGH */
2421857e20a2SLeandro Lupori 	case EM_PPC:
24222c23cb7cSEd Maste 		edesc = powerpc_eflags_desc;
24232c23cb7cSEd Maste 		break;
2424b0084180SMitchell Horne 	case EM_RISCV:
2425b0084180SMitchell Horne 		switch (e_flags & EF_RISCV_FLOAT_ABI_MASK) {
2426b0084180SMitchell Horne 		case EF_RISCV_FLOAT_ABI_SOFT:
2427b0084180SMitchell Horne 			printf(", soft-float ABI");
2428b0084180SMitchell Horne 			break;
2429b0084180SMitchell Horne 		case EF_RISCV_FLOAT_ABI_SINGLE:
2430b0084180SMitchell Horne 			printf(", single-float ABI");
2431b0084180SMitchell Horne 			break;
2432b0084180SMitchell Horne 		case EF_RISCV_FLOAT_ABI_DOUBLE:
2433b0084180SMitchell Horne 			printf(", double-float ABI");
2434b0084180SMitchell Horne 			break;
2435b0084180SMitchell Horne 		case EF_RISCV_FLOAT_ABI_QUAD:
2436b0084180SMitchell Horne 			printf(", quad-float ABI");
2437b0084180SMitchell Horne 			break;
2438b0084180SMitchell Horne 		}
2439b0084180SMitchell Horne 		edesc = riscv_eflags_desc;
2440b0084180SMitchell Horne 		break;
24412c23cb7cSEd Maste 	case EM_SPARC:
24422c23cb7cSEd Maste 	case EM_SPARC32PLUS:
24432c23cb7cSEd Maste 	case EM_SPARCV9:
24442c23cb7cSEd Maste 		switch ((e_flags & EF_SPARCV9_MM)) {
24452c23cb7cSEd Maste 		case EF_SPARCV9_TSO: printf(", tso"); break;
24462c23cb7cSEd Maste 		case EF_SPARCV9_PSO: printf(", pso"); break;
24472c23cb7cSEd Maste 		case EF_SPARCV9_MM: printf(", rmo"); break;
24482c23cb7cSEd Maste 		default: break;
24492c23cb7cSEd Maste 		}
24502c23cb7cSEd Maste 		edesc = sparc_eflags_desc;
24512c23cb7cSEd Maste 		break;
24522c23cb7cSEd Maste 	default:
24532c23cb7cSEd Maste 		break;
24542c23cb7cSEd Maste 	}
24552c23cb7cSEd Maste 
24562c23cb7cSEd Maste 	if (edesc != NULL) {
24572c23cb7cSEd Maste 		while (edesc->desc != NULL) {
24582c23cb7cSEd Maste 			if (e_flags & edesc->flag)
24592c23cb7cSEd Maste 				printf(", %s", edesc->desc);
24602c23cb7cSEd Maste 			edesc++;
24612c23cb7cSEd Maste 		}
24622c23cb7cSEd Maste 	}
24632c23cb7cSEd Maste }
24642c23cb7cSEd Maste 
24652c23cb7cSEd Maste static void
dump_phdr(struct readelf * re)24662c23cb7cSEd Maste dump_phdr(struct readelf *re)
24672c23cb7cSEd Maste {
24682c23cb7cSEd Maste 	const char	*rawfile;
24692c23cb7cSEd Maste 	GElf_Phdr	 phdr;
2470b00fe64fSEd Maste 	size_t		 phnum, size;
24712c23cb7cSEd Maste 	int		 i, j;
24722c23cb7cSEd Maste 
24732c23cb7cSEd Maste #define	PH_HDR	"Type", "Offset", "VirtAddr", "PhysAddr", "FileSiz",	\
24742c23cb7cSEd Maste 		"MemSiz", "Flg", "Align"
247520136ffcSEd Maste #define	PH_CT	phdr_type(re->ehdr.e_machine, phdr.p_type),		\
247620136ffcSEd Maste 		(uintmax_t)phdr.p_offset, (uintmax_t)phdr.p_vaddr,	\
247720136ffcSEd Maste 		(uintmax_t)phdr.p_paddr, (uintmax_t)phdr.p_filesz,	\
247820136ffcSEd Maste 		(uintmax_t)phdr.p_memsz,				\
24792c23cb7cSEd Maste 		phdr.p_flags & PF_R ? 'R' : ' ',			\
24802c23cb7cSEd Maste 		phdr.p_flags & PF_W ? 'W' : ' ',			\
24812c23cb7cSEd Maste 		phdr.p_flags & PF_X ? 'E' : ' ',			\
24822c23cb7cSEd Maste 		(uintmax_t)phdr.p_align
24832c23cb7cSEd Maste 
24842c23cb7cSEd Maste 	if (elf_getphnum(re->elf, &phnum) == 0) {
24852c23cb7cSEd Maste 		warnx("elf_getphnum failed: %s", elf_errmsg(-1));
24862c23cb7cSEd Maste 		return;
24872c23cb7cSEd Maste 	}
24882c23cb7cSEd Maste 	if (phnum == 0) {
24892c23cb7cSEd Maste 		printf("\nThere are no program headers in this file.\n");
24902c23cb7cSEd Maste 		return;
24912c23cb7cSEd Maste 	}
24922c23cb7cSEd Maste 
24932c23cb7cSEd Maste 	printf("\nElf file type is %s", elf_type(re->ehdr.e_type));
24942c23cb7cSEd Maste 	printf("\nEntry point 0x%jx\n", (uintmax_t)re->ehdr.e_entry);
24952c23cb7cSEd Maste 	printf("There are %ju program headers, starting at offset %ju\n",
24962c23cb7cSEd Maste 	    (uintmax_t)phnum, (uintmax_t)re->ehdr.e_phoff);
24972c23cb7cSEd Maste 
24982c23cb7cSEd Maste 	/* Dump program headers. */
24992c23cb7cSEd Maste 	printf("\nProgram Headers:\n");
25002c23cb7cSEd Maste 	if (re->ec == ELFCLASS32)
25012c23cb7cSEd Maste 		printf("  %-15s%-9s%-11s%-11s%-8s%-8s%-4s%s\n", PH_HDR);
25022c23cb7cSEd Maste 	else if (re->options & RE_WW)
25032c23cb7cSEd Maste 		printf("  %-15s%-9s%-19s%-19s%-9s%-9s%-4s%s\n", PH_HDR);
25042c23cb7cSEd Maste 	else
25052c23cb7cSEd Maste 		printf("  %-15s%-19s%-19s%s\n                 %-19s%-20s"
25062c23cb7cSEd Maste 		    "%-7s%s\n", PH_HDR);
25072c23cb7cSEd Maste 	for (i = 0; (size_t) i < phnum; i++) {
25082c23cb7cSEd Maste 		if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
25092c23cb7cSEd Maste 			warnx("gelf_getphdr failed: %s", elf_errmsg(-1));
25102c23cb7cSEd Maste 			continue;
25112c23cb7cSEd Maste 		}
25122c23cb7cSEd Maste 		/* TODO: Add arch-specific segment type dump. */
25132c23cb7cSEd Maste 		if (re->ec == ELFCLASS32)
25142c23cb7cSEd Maste 			printf("  %-14.14s 0x%6.6jx 0x%8.8jx 0x%8.8jx "
25152c23cb7cSEd Maste 			    "0x%5.5jx 0x%5.5jx %c%c%c %#jx\n", PH_CT);
25162c23cb7cSEd Maste 		else if (re->options & RE_WW)
25172c23cb7cSEd Maste 			printf("  %-14.14s 0x%6.6jx 0x%16.16jx 0x%16.16jx "
25182c23cb7cSEd Maste 			    "0x%6.6jx 0x%6.6jx %c%c%c %#jx\n", PH_CT);
25192c23cb7cSEd Maste 		else
25202c23cb7cSEd Maste 			printf("  %-14.14s 0x%16.16jx 0x%16.16jx 0x%16.16jx\n"
25212c23cb7cSEd Maste 			    "                 0x%16.16jx 0x%16.16jx  %c%c%c"
25222c23cb7cSEd Maste 			    "    %#jx\n", PH_CT);
25232c23cb7cSEd Maste 		if (phdr.p_type == PT_INTERP) {
2524b00fe64fSEd Maste 			if ((rawfile = elf_rawfile(re->elf, &size)) == NULL) {
25252c23cb7cSEd Maste 				warnx("elf_rawfile failed: %s", elf_errmsg(-1));
25262c23cb7cSEd Maste 				continue;
25272c23cb7cSEd Maste 			}
2528b00fe64fSEd Maste 			if (phdr.p_offset >= size) {
2529b00fe64fSEd Maste 				warnx("invalid program header offset");
2530b00fe64fSEd Maste 				continue;
2531b00fe64fSEd Maste 			}
25322c23cb7cSEd Maste 			printf("      [Requesting program interpreter: %s]\n",
25332c23cb7cSEd Maste 				rawfile + phdr.p_offset);
25342c23cb7cSEd Maste 		}
25352c23cb7cSEd Maste 	}
25362c23cb7cSEd Maste 
25372c23cb7cSEd Maste 	/* Dump section to segment mapping. */
25382c23cb7cSEd Maste 	if (re->shnum == 0)
25392c23cb7cSEd Maste 		return;
25402c23cb7cSEd Maste 	printf("\n Section to Segment mapping:\n");
25412c23cb7cSEd Maste 	printf("  Segment Sections...\n");
25422c23cb7cSEd Maste 	for (i = 0; (size_t)i < phnum; i++) {
25432c23cb7cSEd Maste 		if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
25442c23cb7cSEd Maste 			warnx("gelf_getphdr failed: %s", elf_errmsg(-1));
25452c23cb7cSEd Maste 			continue;
25462c23cb7cSEd Maste 		}
25472c23cb7cSEd Maste 		printf("   %2.2d     ", i);
25482c23cb7cSEd Maste 		/* skip NULL section. */
254948d41ef0SPhil Shafer 		for (j = 1; (size_t)j < re->shnum; j++) {
255048d41ef0SPhil Shafer 			if (re->sl[j].off < phdr.p_offset)
255148d41ef0SPhil Shafer 				continue;
255248d41ef0SPhil Shafer 			if (re->sl[j].off + re->sl[j].sz >
255348d41ef0SPhil Shafer 			    phdr.p_offset + phdr.p_filesz &&
255448d41ef0SPhil Shafer 			    re->sl[j].type != SHT_NOBITS)
255548d41ef0SPhil Shafer 				continue;
255648d41ef0SPhil Shafer 			if (re->sl[j].addr < phdr.p_vaddr ||
255748d41ef0SPhil Shafer 			    re->sl[j].addr + re->sl[j].sz >
255895fd7f26SEd Maste 			    phdr.p_vaddr + phdr.p_memsz)
255948d41ef0SPhil Shafer 				continue;
256048d41ef0SPhil Shafer 			if (phdr.p_type == PT_TLS &&
256148d41ef0SPhil Shafer 			    (re->sl[j].flags & SHF_TLS) == 0)
256248d41ef0SPhil Shafer 				continue;
25632c23cb7cSEd Maste 			printf("%s ", re->sl[j].name);
256448d41ef0SPhil Shafer 		}
25652c23cb7cSEd Maste 		printf("\n");
25662c23cb7cSEd Maste 	}
25672c23cb7cSEd Maste #undef	PH_HDR
25682c23cb7cSEd Maste #undef	PH_CT
25692c23cb7cSEd Maste }
25702c23cb7cSEd Maste 
25712c23cb7cSEd Maste static char *
section_flags(struct readelf * re,struct section * s)25722c23cb7cSEd Maste section_flags(struct readelf *re, struct section *s)
25732c23cb7cSEd Maste {
25742c23cb7cSEd Maste #define BUF_SZ 256
25752c23cb7cSEd Maste 	static char	buf[BUF_SZ];
25762c23cb7cSEd Maste 	int		i, p, nb;
25772c23cb7cSEd Maste 
25782c23cb7cSEd Maste 	p = 0;
25792c23cb7cSEd Maste 	nb = re->ec == ELFCLASS32 ? 8 : 16;
25802c23cb7cSEd Maste 	if (re->options & RE_T) {
25812c23cb7cSEd Maste 		snprintf(buf, BUF_SZ, "[%*.*jx]: ", nb, nb,
25822c23cb7cSEd Maste 		    (uintmax_t)s->flags);
25832c23cb7cSEd Maste 		p += nb + 4;
25842c23cb7cSEd Maste 	}
25852c23cb7cSEd Maste 	for (i = 0; section_flag[i].ln != NULL; i++) {
25862c23cb7cSEd Maste 		if ((s->flags & section_flag[i].value) == 0)
25872c23cb7cSEd Maste 			continue;
25882c23cb7cSEd Maste 		if (re->options & RE_T) {
25892c23cb7cSEd Maste 			snprintf(&buf[p], BUF_SZ - p, "%s, ",
25902c23cb7cSEd Maste 			    section_flag[i].ln);
25912c23cb7cSEd Maste 			p += strlen(section_flag[i].ln) + 2;
25922c23cb7cSEd Maste 		} else
25932c23cb7cSEd Maste 			buf[p++] = section_flag[i].sn;
25942c23cb7cSEd Maste 	}
25952c23cb7cSEd Maste 	if (re->options & RE_T && p > nb + 4)
25962c23cb7cSEd Maste 		p -= 2;
25972c23cb7cSEd Maste 	buf[p] = '\0';
25982c23cb7cSEd Maste 
25992c23cb7cSEd Maste 	return (buf);
26002c23cb7cSEd Maste }
26012c23cb7cSEd Maste 
26022c23cb7cSEd Maste static void
dump_shdr(struct readelf * re)26032c23cb7cSEd Maste dump_shdr(struct readelf *re)
26042c23cb7cSEd Maste {
26052c23cb7cSEd Maste 	struct section	*s;
26062c23cb7cSEd Maste 	int		 i;
26072c23cb7cSEd Maste 
26082c23cb7cSEd Maste #define	S_HDR	"[Nr] Name", "Type", "Addr", "Off", "Size", "ES",	\
26092c23cb7cSEd Maste 		"Flg", "Lk", "Inf", "Al"
26102c23cb7cSEd Maste #define	S_HDRL	"[Nr] Name", "Type", "Address", "Offset", "Size",	\
26112c23cb7cSEd Maste 		"EntSize", "Flags", "Link", "Info", "Align"
26122c23cb7cSEd Maste #define	ST_HDR	"[Nr] Name", "Type", "Addr", "Off", "Size", "ES",	\
26132c23cb7cSEd Maste 		"Lk", "Inf", "Al", "Flags"
26142c23cb7cSEd Maste #define	ST_HDRL	"[Nr] Name", "Type", "Address", "Offset", "Link",	\
26152c23cb7cSEd Maste 		"Size", "EntSize", "Info", "Align", "Flags"
26162c23cb7cSEd Maste #define	S_CT	i, s->name, section_type(re->ehdr.e_machine, s->type),	\
26172c23cb7cSEd Maste 		(uintmax_t)s->addr, (uintmax_t)s->off, (uintmax_t)s->sz,\
26182c23cb7cSEd Maste 		(uintmax_t)s->entsize, section_flags(re, s),		\
26192c23cb7cSEd Maste 		s->link, s->info, (uintmax_t)s->align
26202c23cb7cSEd Maste #define	ST_CT	i, s->name, section_type(re->ehdr.e_machine, s->type),  \
26212c23cb7cSEd Maste 		(uintmax_t)s->addr, (uintmax_t)s->off, (uintmax_t)s->sz,\
26222c23cb7cSEd Maste 		(uintmax_t)s->entsize, s->link, s->info,		\
26232c23cb7cSEd Maste 		(uintmax_t)s->align, section_flags(re, s)
26242c23cb7cSEd Maste #define	ST_CTL	i, s->name, section_type(re->ehdr.e_machine, s->type),  \
26252c23cb7cSEd Maste 		(uintmax_t)s->addr, (uintmax_t)s->off, s->link,		\
26262c23cb7cSEd Maste 		(uintmax_t)s->sz, (uintmax_t)s->entsize, s->info,	\
26272c23cb7cSEd Maste 		(uintmax_t)s->align, section_flags(re, s)
26282c23cb7cSEd Maste 
26292c23cb7cSEd Maste 	if (re->shnum == 0) {
26302c23cb7cSEd Maste 		printf("\nThere are no sections in this file.\n");
26312c23cb7cSEd Maste 		return;
26322c23cb7cSEd Maste 	}
26332c23cb7cSEd Maste 	printf("There are %ju section headers, starting at offset 0x%jx:\n",
26342c23cb7cSEd Maste 	    (uintmax_t)re->shnum, (uintmax_t)re->ehdr.e_shoff);
26352c23cb7cSEd Maste 	printf("\nSection Headers:\n");
26362c23cb7cSEd Maste 	if (re->ec == ELFCLASS32) {
26372c23cb7cSEd Maste 		if (re->options & RE_T)
26382c23cb7cSEd Maste 			printf("  %s\n       %-16s%-9s%-7s%-7s%-5s%-3s%-4s%s\n"
26392c23cb7cSEd Maste 			    "%12s\n", ST_HDR);
26402c23cb7cSEd Maste 		else
26412c23cb7cSEd Maste 			printf("  %-23s%-16s%-9s%-7s%-7s%-3s%-4s%-3s%-4s%s\n",
26422c23cb7cSEd Maste 			    S_HDR);
26432c23cb7cSEd Maste 	} else if (re->options & RE_WW) {
26442c23cb7cSEd Maste 		if (re->options & RE_T)
26452c23cb7cSEd Maste 			printf("  %s\n       %-16s%-17s%-7s%-7s%-5s%-3s%-4s%s\n"
26462c23cb7cSEd Maste 			    "%12s\n", ST_HDR);
26472c23cb7cSEd Maste 		else
26482c23cb7cSEd Maste 			printf("  %-23s%-16s%-17s%-7s%-7s%-3s%-4s%-3s%-4s%s\n",
26492c23cb7cSEd Maste 			    S_HDR);
26502c23cb7cSEd Maste 	} else {
26512c23cb7cSEd Maste 		if (re->options & RE_T)
26522c23cb7cSEd Maste 			printf("  %s\n       %-18s%-17s%-18s%s\n       %-18s"
26532c23cb7cSEd Maste 			    "%-17s%-18s%s\n%12s\n", ST_HDRL);
26542c23cb7cSEd Maste 		else
26552c23cb7cSEd Maste 			printf("  %-23s%-17s%-18s%s\n       %-18s%-17s%-7s%"
26562c23cb7cSEd Maste 			    "-6s%-6s%s\n", S_HDRL);
26572c23cb7cSEd Maste 	}
26582c23cb7cSEd Maste 	for (i = 0; (size_t)i < re->shnum; i++) {
26592c23cb7cSEd Maste 		s = &re->sl[i];
26602c23cb7cSEd Maste 		if (re->ec == ELFCLASS32) {
26612c23cb7cSEd Maste 			if (re->options & RE_T)
26622c23cb7cSEd Maste 				printf("  [%2d] %s\n       %-15.15s %8.8jx"
26632c23cb7cSEd Maste 				    " %6.6jx %6.6jx %2.2jx  %2u %3u %2ju\n"
26642c23cb7cSEd Maste 				    "       %s\n", ST_CT);
26652c23cb7cSEd Maste 			else
2666847dfd28SEd Maste 				if (re->options & RE_WW)
2667847dfd28SEd Maste 					printf("  [%2d] %-17s %-15.15s "
2668847dfd28SEd Maste 					    "%8.8jx %6.6jx %6.6jx %2.2jx %3s "
2669847dfd28SEd Maste 					    "%2u %3u %2ju\n", S_CT);
2670847dfd28SEd Maste 				else
2671847dfd28SEd Maste 					printf("  [%2d] %-17.17s %-15.15s "
2672847dfd28SEd Maste 					    "%8.8jx %6.6jx %6.6jx %2.2jx %3s "
2673847dfd28SEd Maste 					    "%2u %3u %2ju\n", S_CT);
26742c23cb7cSEd Maste 		} else if (re->options & RE_WW) {
26752c23cb7cSEd Maste 			if (re->options & RE_T)
26762c23cb7cSEd Maste 				printf("  [%2d] %s\n       %-15.15s %16.16jx"
26772c23cb7cSEd Maste 				    " %6.6jx %6.6jx %2.2jx  %2u %3u %2ju\n"
26782c23cb7cSEd Maste 				    "       %s\n", ST_CT);
26792c23cb7cSEd Maste 			else
2680847dfd28SEd Maste 				printf("  [%2d] %-17s %-15.15s %16.16jx"
26812c23cb7cSEd Maste 				    " %6.6jx %6.6jx %2.2jx %3s %2u %3u %2ju\n",
26822c23cb7cSEd Maste 				    S_CT);
26832c23cb7cSEd Maste 		} else {
26842c23cb7cSEd Maste 			if (re->options & RE_T)
26852c23cb7cSEd Maste 				printf("  [%2d] %s\n       %-15.15s  %16.16jx"
26862c23cb7cSEd Maste 				    "  %16.16jx  %u\n       %16.16jx %16.16jx"
26872c23cb7cSEd Maste 				    "  %-16u  %ju\n       %s\n", ST_CTL);
26882c23cb7cSEd Maste 			else
26892c23cb7cSEd Maste 				printf("  [%2d] %-17.17s %-15.15s  %16.16jx"
26902c23cb7cSEd Maste 				    "  %8.8jx\n       %16.16jx  %16.16jx "
26912c23cb7cSEd Maste 				    "%3s      %2u   %3u     %ju\n", S_CT);
26922c23cb7cSEd Maste 		}
26932c23cb7cSEd Maste 	}
26942c23cb7cSEd Maste 	if ((re->options & RE_T) == 0)
26952c23cb7cSEd Maste 		printf("Key to Flags:\n  W (write), A (alloc),"
26962c23cb7cSEd Maste 		    " X (execute), M (merge), S (strings)\n"
26972c23cb7cSEd Maste 		    "  I (info), L (link order), G (group), x (unknown)\n"
26982c23cb7cSEd Maste 		    "  O (extra OS processing required)"
26992c23cb7cSEd Maste 		    " o (OS specific), p (processor specific)\n");
27002c23cb7cSEd Maste 
27012c23cb7cSEd Maste #undef	S_HDR
27022c23cb7cSEd Maste #undef	S_HDRL
27032c23cb7cSEd Maste #undef	ST_HDR
27042c23cb7cSEd Maste #undef	ST_HDRL
27052c23cb7cSEd Maste #undef	S_CT
27062c23cb7cSEd Maste #undef	ST_CT
27072c23cb7cSEd Maste #undef	ST_CTL
27082c23cb7cSEd Maste }
27092c23cb7cSEd Maste 
271071edbbfdSEd Maste /*
271171edbbfdSEd Maste  * Return number of entries in the given section. We'd prefer ent_count be a
271271edbbfdSEd Maste  * size_t *, but libelf APIs already use int for section indices.
271371edbbfdSEd Maste  */
271471edbbfdSEd Maste static int
get_ent_count(struct section * s,int * ent_count)271571edbbfdSEd Maste get_ent_count(struct section *s, int *ent_count)
271671edbbfdSEd Maste {
271771edbbfdSEd Maste 	if (s->entsize == 0) {
271871edbbfdSEd Maste 		warnx("section %s has entry size 0", s->name);
271971edbbfdSEd Maste 		return (0);
272071edbbfdSEd Maste 	} else if (s->sz / s->entsize > INT_MAX) {
272171edbbfdSEd Maste 		warnx("section %s has invalid section count", s->name);
272271edbbfdSEd Maste 		return (0);
272371edbbfdSEd Maste 	}
272471edbbfdSEd Maste 	*ent_count = (int)(s->sz / s->entsize);
272571edbbfdSEd Maste 	return (1);
272671edbbfdSEd Maste }
272771edbbfdSEd Maste 
27282c23cb7cSEd Maste static void
dump_dynamic(struct readelf * re)27292c23cb7cSEd Maste dump_dynamic(struct readelf *re)
27302c23cb7cSEd Maste {
27312c23cb7cSEd Maste 	GElf_Dyn	 dyn;
27322c23cb7cSEd Maste 	Elf_Data	*d;
27332c23cb7cSEd Maste 	struct section	*s;
27342c23cb7cSEd Maste 	int		 elferr, i, is_dynamic, j, jmax, nentries;
27352c23cb7cSEd Maste 
27362c23cb7cSEd Maste 	is_dynamic = 0;
27372c23cb7cSEd Maste 
27382c23cb7cSEd Maste 	for (i = 0; (size_t)i < re->shnum; i++) {
27392c23cb7cSEd Maste 		s = &re->sl[i];
27402c23cb7cSEd Maste 		if (s->type != SHT_DYNAMIC)
27412c23cb7cSEd Maste 			continue;
27422c23cb7cSEd Maste 		(void) elf_errno();
27432c23cb7cSEd Maste 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
27442c23cb7cSEd Maste 			elferr = elf_errno();
27452c23cb7cSEd Maste 			if (elferr != 0)
27462c23cb7cSEd Maste 				warnx("elf_getdata failed: %s", elf_errmsg(-1));
27472c23cb7cSEd Maste 			continue;
27482c23cb7cSEd Maste 		}
27492c23cb7cSEd Maste 		if (d->d_size <= 0)
27502c23cb7cSEd Maste 			continue;
27512c23cb7cSEd Maste 
27522c23cb7cSEd Maste 		is_dynamic = 1;
27532c23cb7cSEd Maste 
27542c23cb7cSEd Maste 		/* Determine the actual number of table entries. */
27552c23cb7cSEd Maste 		nentries = 0;
275671edbbfdSEd Maste 		if (!get_ent_count(s, &jmax))
275771edbbfdSEd Maste 			continue;
27582c23cb7cSEd Maste 		for (j = 0; j < jmax; j++) {
27592c23cb7cSEd Maste 			if (gelf_getdyn(d, j, &dyn) != &dyn) {
27602c23cb7cSEd Maste 				warnx("gelf_getdyn failed: %s",
27612c23cb7cSEd Maste 				    elf_errmsg(-1));
27622c23cb7cSEd Maste 				continue;
27632c23cb7cSEd Maste 			}
27642c23cb7cSEd Maste 			nentries ++;
27652c23cb7cSEd Maste 			if (dyn.d_tag == DT_NULL)
27662c23cb7cSEd Maste 				break;
27672c23cb7cSEd Maste                 }
27682c23cb7cSEd Maste 
27692c23cb7cSEd Maste 		printf("\nDynamic section at offset 0x%jx", (uintmax_t)s->off);
27702c23cb7cSEd Maste 		printf(" contains %u entries:\n", nentries);
27712c23cb7cSEd Maste 
27722c23cb7cSEd Maste 		if (re->ec == ELFCLASS32)
27732c23cb7cSEd Maste 			printf("%5s%12s%28s\n", "Tag", "Type", "Name/Value");
27742c23cb7cSEd Maste 		else
27752c23cb7cSEd Maste 			printf("%5s%20s%28s\n", "Tag", "Type", "Name/Value");
27762c23cb7cSEd Maste 
27772c23cb7cSEd Maste 		for (j = 0; j < nentries; j++) {
27782c23cb7cSEd Maste 			if (gelf_getdyn(d, j, &dyn) != &dyn)
27792c23cb7cSEd Maste 				continue;
27802c23cb7cSEd Maste 			/* Dump dynamic entry type. */
27812c23cb7cSEd Maste 			if (re->ec == ELFCLASS32)
27822c23cb7cSEd Maste 				printf(" 0x%8.8jx", (uintmax_t)dyn.d_tag);
27832c23cb7cSEd Maste 			else
27842c23cb7cSEd Maste 				printf(" 0x%16.16jx", (uintmax_t)dyn.d_tag);
27852c23cb7cSEd Maste 			printf(" %-20s", dt_type(re->ehdr.e_machine,
27862c23cb7cSEd Maste 			    dyn.d_tag));
27872c23cb7cSEd Maste 			/* Dump dynamic entry value. */
27882c23cb7cSEd Maste 			dump_dyn_val(re, &dyn, s->link);
27892c23cb7cSEd Maste 		}
27902c23cb7cSEd Maste 	}
27912c23cb7cSEd Maste 
27922c23cb7cSEd Maste 	if (!is_dynamic)
27932c23cb7cSEd Maste 		printf("\nThere is no dynamic section in this file.\n");
27942c23cb7cSEd Maste }
27952c23cb7cSEd Maste 
27962c23cb7cSEd Maste static char *
timestamp(time_t ti)27972c23cb7cSEd Maste timestamp(time_t ti)
27982c23cb7cSEd Maste {
27992c23cb7cSEd Maste 	static char ts[32];
28002c23cb7cSEd Maste 	struct tm *t;
28012c23cb7cSEd Maste 
28022c23cb7cSEd Maste 	t = gmtime(&ti);
28032c23cb7cSEd Maste 	snprintf(ts, sizeof(ts), "%04d-%02d-%02dT%02d:%02d:%02d",
28042c23cb7cSEd Maste 	    t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour,
28052c23cb7cSEd Maste 	    t->tm_min, t->tm_sec);
28062c23cb7cSEd Maste 
28072c23cb7cSEd Maste 	return (ts);
28082c23cb7cSEd Maste }
28092c23cb7cSEd Maste 
28102c23cb7cSEd Maste static const char *
dyn_str(struct readelf * re,uint32_t stab,uint64_t d_val)28112c23cb7cSEd Maste dyn_str(struct readelf *re, uint32_t stab, uint64_t d_val)
28122c23cb7cSEd Maste {
28132c23cb7cSEd Maste 	const char *name;
28142c23cb7cSEd Maste 
28152c23cb7cSEd Maste 	if (stab == SHN_UNDEF)
28162c23cb7cSEd Maste 		name = "ERROR";
28172c23cb7cSEd Maste 	else if ((name = elf_strptr(re->elf, stab, d_val)) == NULL) {
28182c23cb7cSEd Maste 		(void) elf_errno(); /* clear error */
28192c23cb7cSEd Maste 		name = "ERROR";
28202c23cb7cSEd Maste 	}
28212c23cb7cSEd Maste 
28222c23cb7cSEd Maste 	return (name);
28232c23cb7cSEd Maste }
28242c23cb7cSEd Maste 
28252c23cb7cSEd Maste static void
dump_arch_dyn_val(struct readelf * re,GElf_Dyn * dyn)282675826696SEd Maste dump_arch_dyn_val(struct readelf *re, GElf_Dyn *dyn)
28272c23cb7cSEd Maste {
28282c23cb7cSEd Maste 	switch (re->ehdr.e_machine) {
28292c23cb7cSEd Maste 	case EM_MIPS:
28302c23cb7cSEd Maste 	case EM_MIPS_RS3_LE:
28312c23cb7cSEd Maste 		switch (dyn->d_tag) {
28322c23cb7cSEd Maste 		case DT_MIPS_RLD_VERSION:
28332c23cb7cSEd Maste 		case DT_MIPS_LOCAL_GOTNO:
28342c23cb7cSEd Maste 		case DT_MIPS_CONFLICTNO:
28352c23cb7cSEd Maste 		case DT_MIPS_LIBLISTNO:
28362c23cb7cSEd Maste 		case DT_MIPS_SYMTABNO:
28372c23cb7cSEd Maste 		case DT_MIPS_UNREFEXTNO:
28382c23cb7cSEd Maste 		case DT_MIPS_GOTSYM:
28392c23cb7cSEd Maste 		case DT_MIPS_HIPAGENO:
28402c23cb7cSEd Maste 		case DT_MIPS_DELTA_CLASS_NO:
28412c23cb7cSEd Maste 		case DT_MIPS_DELTA_INSTANCE_NO:
28422c23cb7cSEd Maste 		case DT_MIPS_DELTA_RELOC_NO:
28432c23cb7cSEd Maste 		case DT_MIPS_DELTA_SYM_NO:
28442c23cb7cSEd Maste 		case DT_MIPS_DELTA_CLASSSYM_NO:
28452c23cb7cSEd Maste 		case DT_MIPS_LOCALPAGE_GOTIDX:
28462c23cb7cSEd Maste 		case DT_MIPS_LOCAL_GOTIDX:
28472c23cb7cSEd Maste 		case DT_MIPS_HIDDEN_GOTIDX:
28482c23cb7cSEd Maste 		case DT_MIPS_PROTECTED_GOTIDX:
28492c23cb7cSEd Maste 			printf(" %ju\n", (uintmax_t) dyn->d_un.d_val);
28502c23cb7cSEd Maste 			break;
28512c23cb7cSEd Maste 		case DT_MIPS_ICHECKSUM:
28522c23cb7cSEd Maste 		case DT_MIPS_FLAGS:
28532c23cb7cSEd Maste 		case DT_MIPS_BASE_ADDRESS:
28542c23cb7cSEd Maste 		case DT_MIPS_CONFLICT:
28552c23cb7cSEd Maste 		case DT_MIPS_LIBLIST:
28562c23cb7cSEd Maste 		case DT_MIPS_RLD_MAP:
28572c23cb7cSEd Maste 		case DT_MIPS_DELTA_CLASS:
28582c23cb7cSEd Maste 		case DT_MIPS_DELTA_INSTANCE:
28592c23cb7cSEd Maste 		case DT_MIPS_DELTA_RELOC:
28602c23cb7cSEd Maste 		case DT_MIPS_DELTA_SYM:
28612c23cb7cSEd Maste 		case DT_MIPS_DELTA_CLASSSYM:
28622c23cb7cSEd Maste 		case DT_MIPS_CXX_FLAGS:
28632c23cb7cSEd Maste 		case DT_MIPS_PIXIE_INIT:
28642c23cb7cSEd Maste 		case DT_MIPS_SYMBOL_LIB:
28652c23cb7cSEd Maste 		case DT_MIPS_OPTIONS:
28662c23cb7cSEd Maste 		case DT_MIPS_INTERFACE:
28672c23cb7cSEd Maste 		case DT_MIPS_DYNSTR_ALIGN:
28682c23cb7cSEd Maste 		case DT_MIPS_INTERFACE_SIZE:
28692c23cb7cSEd Maste 		case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
28702c23cb7cSEd Maste 		case DT_MIPS_COMPACT_SIZE:
28712c23cb7cSEd Maste 		case DT_MIPS_GP_VALUE:
28722c23cb7cSEd Maste 		case DT_MIPS_AUX_DYNAMIC:
28732c23cb7cSEd Maste 		case DT_MIPS_PLTGOT:
28742c23cb7cSEd Maste 		case DT_MIPS_RLD_OBJ_UPDATE:
28752c23cb7cSEd Maste 		case DT_MIPS_RWPLT:
28762c23cb7cSEd Maste 			printf(" 0x%jx\n", (uintmax_t) dyn->d_un.d_val);
28772c23cb7cSEd Maste 			break;
28782c23cb7cSEd Maste 		case DT_MIPS_IVERSION:
28792c23cb7cSEd Maste 		case DT_MIPS_PERF_SUFFIX:
28802c23cb7cSEd Maste 		case DT_MIPS_TIME_STAMP:
28812c23cb7cSEd Maste 			printf(" %s\n", timestamp(dyn->d_un.d_val));
28822c23cb7cSEd Maste 			break;
28839fb35c8dSJohn Baldwin 		default:
28849fb35c8dSJohn Baldwin 			printf("\n");
28859fb35c8dSJohn Baldwin 			break;
28862c23cb7cSEd Maste 		}
28872c23cb7cSEd Maste 		break;
28882c23cb7cSEd Maste 	default:
28892c23cb7cSEd Maste 		printf("\n");
28902c23cb7cSEd Maste 		break;
28912c23cb7cSEd Maste 	}
28922c23cb7cSEd Maste }
28932c23cb7cSEd Maste 
28942c23cb7cSEd Maste static void
dump_flags(struct flag_desc * desc,uint64_t val)28956b2779a0SEd Maste dump_flags(struct flag_desc *desc, uint64_t val)
28966b2779a0SEd Maste {
28976b2779a0SEd Maste 	struct flag_desc *fd;
28986b2779a0SEd Maste 
28996b2779a0SEd Maste 	for (fd = desc; fd->flag != 0; fd++) {
29006b2779a0SEd Maste 		if (val & fd->flag) {
29016b2779a0SEd Maste 			val &= ~fd->flag;
29026b2779a0SEd Maste 			printf(" %s", fd->desc);
29036b2779a0SEd Maste 		}
29046b2779a0SEd Maste 	}
29056b2779a0SEd Maste 	if (val != 0)
29066b2779a0SEd Maste 		printf(" unknown (0x%jx)", (uintmax_t)val);
290777f552e2SEd Maste 	printf("\n");
29086b2779a0SEd Maste }
29096b2779a0SEd Maste 
29106b2779a0SEd Maste static struct flag_desc dt_flags[] = {
29116b2779a0SEd Maste 	{ DF_ORIGIN,		"ORIGIN" },
29126b2779a0SEd Maste 	{ DF_SYMBOLIC,		"SYMBOLIC" },
29136b2779a0SEd Maste 	{ DF_TEXTREL,		"TEXTREL" },
29146b2779a0SEd Maste 	{ DF_BIND_NOW,		"BIND_NOW" },
29156b2779a0SEd Maste 	{ DF_STATIC_TLS,	"STATIC_TLS" },
29166b2779a0SEd Maste 	{ 0, NULL }
29176b2779a0SEd Maste };
29186b2779a0SEd Maste 
29196b2779a0SEd Maste static struct flag_desc dt_flags_1[] = {
29206b2779a0SEd Maste 	{ DF_1_BIND_NOW,	"NOW" },
29216b2779a0SEd Maste 	{ DF_1_GLOBAL,		"GLOBAL" },
29226b2779a0SEd Maste 	{ 0x4,			"GROUP" },
29236b2779a0SEd Maste 	{ DF_1_NODELETE,	"NODELETE" },
29246b2779a0SEd Maste 	{ DF_1_LOADFLTR,	"LOADFLTR" },
29256b2779a0SEd Maste 	{ 0x20,			"INITFIRST" },
29266b2779a0SEd Maste 	{ DF_1_NOOPEN,		"NOOPEN" },
29276b2779a0SEd Maste 	{ DF_1_ORIGIN,		"ORIGIN" },
29286b2779a0SEd Maste 	{ 0x100,		"DIRECT" },
29296b2779a0SEd Maste 	{ DF_1_INTERPOSE,	"INTERPOSE" },
29306b2779a0SEd Maste 	{ DF_1_NODEFLIB,	"NODEFLIB" },
29316b2779a0SEd Maste 	{ 0x1000,		"NODUMP" },
29326b2779a0SEd Maste 	{ 0x2000,		"CONFALT" },
29336b2779a0SEd Maste 	{ 0x4000,		"ENDFILTEE" },
29346b2779a0SEd Maste 	{ 0x8000,		"DISPRELDNE" },
29356b2779a0SEd Maste 	{ 0x10000,		"DISPRELPND" },
29366b2779a0SEd Maste 	{ 0x20000,		"NODIRECT" },
29376b2779a0SEd Maste 	{ 0x40000,		"IGNMULDEF" },
29386b2779a0SEd Maste 	{ 0x80000,		"NOKSYMS" },
29396b2779a0SEd Maste 	{ 0x100000,		"NOHDR" },
29406b2779a0SEd Maste 	{ 0x200000,		"EDITED" },
29416b2779a0SEd Maste 	{ 0x400000,		"NORELOC" },
29426b2779a0SEd Maste 	{ 0x800000,		"SYMINTPOSE" },
29436b2779a0SEd Maste 	{ 0x1000000,		"GLOBAUDIT" },
2944796bf313SEd Maste 	{ 0x02000000,		"SINGLETON" },
2945796bf313SEd Maste 	{ 0x04000000,		"STUB" },
2946796bf313SEd Maste 	{ DF_1_PIE,		"PIE" },
29476b2779a0SEd Maste 	{ 0, NULL }
29486b2779a0SEd Maste };
29496b2779a0SEd Maste 
29506b2779a0SEd Maste static void
dump_dyn_val(struct readelf * re,GElf_Dyn * dyn,uint32_t stab)29512c23cb7cSEd Maste dump_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab)
29522c23cb7cSEd Maste {
29532c23cb7cSEd Maste 	const char *name;
29542c23cb7cSEd Maste 
295575826696SEd Maste 	if (dyn->d_tag >= DT_LOPROC && dyn->d_tag <= DT_HIPROC &&
295675826696SEd Maste 	    dyn->d_tag != DT_AUXILIARY && dyn->d_tag != DT_FILTER) {
295775826696SEd Maste 		dump_arch_dyn_val(re, dyn);
29582c23cb7cSEd Maste 		return;
29592c23cb7cSEd Maste 	}
29602c23cb7cSEd Maste 
29612c23cb7cSEd Maste 	/* These entry values are index into the string table. */
29622c23cb7cSEd Maste 	name = NULL;
296375826696SEd Maste 	if (dyn->d_tag == DT_AUXILIARY || dyn->d_tag == DT_FILTER ||
296475826696SEd Maste 	    dyn->d_tag == DT_NEEDED || dyn->d_tag == DT_SONAME ||
29652c23cb7cSEd Maste 	    dyn->d_tag == DT_RPATH || dyn->d_tag == DT_RUNPATH)
29662c23cb7cSEd Maste 		name = dyn_str(re, stab, dyn->d_un.d_val);
29672c23cb7cSEd Maste 
29682c23cb7cSEd Maste 	switch(dyn->d_tag) {
29692c23cb7cSEd Maste 	case DT_NULL:
29702c23cb7cSEd Maste 	case DT_PLTGOT:
29712c23cb7cSEd Maste 	case DT_HASH:
29722c23cb7cSEd Maste 	case DT_STRTAB:
29732c23cb7cSEd Maste 	case DT_SYMTAB:
29742c23cb7cSEd Maste 	case DT_RELA:
29752c23cb7cSEd Maste 	case DT_INIT:
29762c23cb7cSEd Maste 	case DT_SYMBOLIC:
29772c23cb7cSEd Maste 	case DT_REL:
29782c23cb7cSEd Maste 	case DT_DEBUG:
29792c23cb7cSEd Maste 	case DT_TEXTREL:
29802c23cb7cSEd Maste 	case DT_JMPREL:
29812c23cb7cSEd Maste 	case DT_FINI:
29822c23cb7cSEd Maste 	case DT_VERDEF:
29832c23cb7cSEd Maste 	case DT_VERNEED:
29842c23cb7cSEd Maste 	case DT_VERSYM:
29852c23cb7cSEd Maste 	case DT_GNU_HASH:
29862c23cb7cSEd Maste 	case DT_GNU_LIBLIST:
29872c23cb7cSEd Maste 	case DT_GNU_CONFLICT:
29882c23cb7cSEd Maste 		printf(" 0x%jx\n", (uintmax_t) dyn->d_un.d_val);
29892c23cb7cSEd Maste 		break;
29902c23cb7cSEd Maste 	case DT_PLTRELSZ:
29912c23cb7cSEd Maste 	case DT_RELASZ:
29922c23cb7cSEd Maste 	case DT_RELAENT:
29932c23cb7cSEd Maste 	case DT_STRSZ:
29942c23cb7cSEd Maste 	case DT_SYMENT:
29952c23cb7cSEd Maste 	case DT_RELSZ:
29962c23cb7cSEd Maste 	case DT_RELENT:
29977003cfb3SEd Maste 	case DT_PREINIT_ARRAYSZ:
29982c23cb7cSEd Maste 	case DT_INIT_ARRAYSZ:
29992c23cb7cSEd Maste 	case DT_FINI_ARRAYSZ:
30002c23cb7cSEd Maste 	case DT_GNU_CONFLICTSZ:
30012c23cb7cSEd Maste 	case DT_GNU_LIBLISTSZ:
30022c23cb7cSEd Maste 		printf(" %ju (bytes)\n", (uintmax_t) dyn->d_un.d_val);
30032c23cb7cSEd Maste 		break;
30042c23cb7cSEd Maste  	case DT_RELACOUNT:
30052c23cb7cSEd Maste 	case DT_RELCOUNT:
30062c23cb7cSEd Maste 	case DT_VERDEFNUM:
30072c23cb7cSEd Maste 	case DT_VERNEEDNUM:
30082c23cb7cSEd Maste 		printf(" %ju\n", (uintmax_t) dyn->d_un.d_val);
30092c23cb7cSEd Maste 		break;
301075826696SEd Maste 	case DT_AUXILIARY:
301175826696SEd Maste 		printf(" Auxiliary library: [%s]\n", name);
301275826696SEd Maste 		break;
301375826696SEd Maste 	case DT_FILTER:
301475826696SEd Maste 		printf(" Filter library: [%s]\n", name);
301575826696SEd Maste 		break;
30162c23cb7cSEd Maste 	case DT_NEEDED:
30172c23cb7cSEd Maste 		printf(" Shared library: [%s]\n", name);
30182c23cb7cSEd Maste 		break;
30192c23cb7cSEd Maste 	case DT_SONAME:
30202c23cb7cSEd Maste 		printf(" Library soname: [%s]\n", name);
30212c23cb7cSEd Maste 		break;
30222c23cb7cSEd Maste 	case DT_RPATH:
30232c23cb7cSEd Maste 		printf(" Library rpath: [%s]\n", name);
30242c23cb7cSEd Maste 		break;
30252c23cb7cSEd Maste 	case DT_RUNPATH:
30262c23cb7cSEd Maste 		printf(" Library runpath: [%s]\n", name);
30272c23cb7cSEd Maste 		break;
30282c23cb7cSEd Maste 	case DT_PLTREL:
30292c23cb7cSEd Maste 		printf(" %s\n", dt_type(re->ehdr.e_machine, dyn->d_un.d_val));
30302c23cb7cSEd Maste 		break;
30312c23cb7cSEd Maste 	case DT_GNU_PRELINKED:
30322c23cb7cSEd Maste 		printf(" %s\n", timestamp(dyn->d_un.d_val));
30332c23cb7cSEd Maste 		break;
303487a8583bSEd Maste 	case DT_FLAGS:
30356b2779a0SEd Maste 		dump_flags(dt_flags, dyn->d_un.d_val);
303687a8583bSEd Maste 		break;
303787a8583bSEd Maste 	case DT_FLAGS_1:
30386b2779a0SEd Maste 		dump_flags(dt_flags_1, dyn->d_un.d_val);
303987a8583bSEd Maste 		break;
30402c23cb7cSEd Maste 	default:
30412c23cb7cSEd Maste 		printf("\n");
30422c23cb7cSEd Maste 	}
30432c23cb7cSEd Maste }
30442c23cb7cSEd Maste 
30452c23cb7cSEd Maste static void
dump_rel(struct readelf * re,struct section * s,Elf_Data * d)30462c23cb7cSEd Maste dump_rel(struct readelf *re, struct section *s, Elf_Data *d)
30472c23cb7cSEd Maste {
30482c23cb7cSEd Maste 	GElf_Rel r;
30492c23cb7cSEd Maste 	const char *symname;
30502c23cb7cSEd Maste 	uint64_t symval;
30512c23cb7cSEd Maste 	int i, len;
3052b6d812d2SEd Maste 	uint32_t type;
3053b6d812d2SEd Maste 	uint8_t type2, type3;
30542c23cb7cSEd Maste 
3055656f49f8SEd Maste 	if (s->link >= re->shnum)
3056656f49f8SEd Maste 		return;
3057656f49f8SEd Maste 
30582c23cb7cSEd Maste #define	REL_HDR "r_offset", "r_info", "r_type", "st_value", "st_name"
30592c23cb7cSEd Maste #define	REL_CT32 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
3060b6b6f9ccSEd Maste 		elftc_reloc_type_str(re->ehdr.e_machine,	    \
3061b6b6f9ccSEd Maste 		ELF32_R_TYPE(r.r_info)), (uintmax_t)symval, symname
30622c23cb7cSEd Maste #define	REL_CT64 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
3063b6d812d2SEd Maste 		elftc_reloc_type_str(re->ehdr.e_machine, type),	    \
3064b6d812d2SEd Maste 		(uintmax_t)symval, symname
30652c23cb7cSEd Maste 
30662c23cb7cSEd Maste 	printf("\nRelocation section (%s):\n", s->name);
30672c23cb7cSEd Maste 	if (re->ec == ELFCLASS32)
30682c23cb7cSEd Maste 		printf("%-8s %-8s %-19s %-8s %s\n", REL_HDR);
30692c23cb7cSEd Maste 	else {
30702c23cb7cSEd Maste 		if (re->options & RE_WW)
30712c23cb7cSEd Maste 			printf("%-16s %-16s %-24s %-16s %s\n", REL_HDR);
30722c23cb7cSEd Maste 		else
30732c23cb7cSEd Maste 			printf("%-12s %-12s %-19s %-16s %s\n", REL_HDR);
30742c23cb7cSEd Maste 	}
307571edbbfdSEd Maste 	assert(d->d_size == s->sz);
307671edbbfdSEd Maste 	if (!get_ent_count(s, &len))
307771edbbfdSEd Maste 		return;
30782c23cb7cSEd Maste 	for (i = 0; i < len; i++) {
30792c23cb7cSEd Maste 		if (gelf_getrel(d, i, &r) != &r) {
30802c23cb7cSEd Maste 			warnx("gelf_getrel failed: %s", elf_errmsg(-1));
30812c23cb7cSEd Maste 			continue;
30822c23cb7cSEd Maste 		}
30832c23cb7cSEd Maste 		symname = get_symbol_name(re, s->link, GELF_R_SYM(r.r_info));
30842c23cb7cSEd Maste 		symval = get_symbol_value(re, s->link, GELF_R_SYM(r.r_info));
30852c23cb7cSEd Maste 		if (re->ec == ELFCLASS32) {
30862c23cb7cSEd Maste 			r.r_info = ELF32_R_INFO(ELF64_R_SYM(r.r_info),
30872c23cb7cSEd Maste 			    ELF64_R_TYPE(r.r_info));
30882c23cb7cSEd Maste 			printf("%8.8jx %8.8jx %-19.19s %8.8jx %s\n", REL_CT32);
30892c23cb7cSEd Maste 		} else {
3090b6d812d2SEd Maste 			type = ELF64_R_TYPE(r.r_info);
3091b6d812d2SEd Maste 			if (re->ehdr.e_machine == EM_MIPS) {
3092b6d812d2SEd Maste 				type2 = (type >> 8) & 0xFF;
3093b6d812d2SEd Maste 				type3 = (type >> 16) & 0xFF;
3094b6d812d2SEd Maste 				type = type & 0xFF;
3095d0bd2dadSEd Maste 			} else {
3096d0bd2dadSEd Maste 				type2 = type3 = 0;
3097b6d812d2SEd Maste 			}
30982c23cb7cSEd Maste 			if (re->options & RE_WW)
30992c23cb7cSEd Maste 				printf("%16.16jx %16.16jx %-24.24s"
31002c23cb7cSEd Maste 				    " %16.16jx %s\n", REL_CT64);
31012c23cb7cSEd Maste 			else
31022c23cb7cSEd Maste 				printf("%12.12jx %12.12jx %-19.19s"
31032c23cb7cSEd Maste 				    " %16.16jx %s\n", REL_CT64);
3104b6d812d2SEd Maste 			if (re->ehdr.e_machine == EM_MIPS) {
3105b6d812d2SEd Maste 				if (re->options & RE_WW) {
3106b6d812d2SEd Maste 					printf("%32s: %s\n", "Type2",
3107b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
3108b6d812d2SEd Maste 					    type2));
3109b6d812d2SEd Maste 					printf("%32s: %s\n", "Type3",
3110b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
3111b6d812d2SEd Maste 					    type3));
3112b6d812d2SEd Maste 				} else {
3113b6d812d2SEd Maste 					printf("%24s: %s\n", "Type2",
3114b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
3115b6d812d2SEd Maste 					    type2));
3116b6d812d2SEd Maste 					printf("%24s: %s\n", "Type3",
3117b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
3118b6d812d2SEd Maste 					    type3));
3119b6d812d2SEd Maste 				}
3120b6d812d2SEd Maste 			}
31212c23cb7cSEd Maste 		}
31222c23cb7cSEd Maste 	}
31232c23cb7cSEd Maste 
31242c23cb7cSEd Maste #undef	REL_HDR
31252c23cb7cSEd Maste #undef	REL_CT
31262c23cb7cSEd Maste }
31272c23cb7cSEd Maste 
31282c23cb7cSEd Maste static void
dump_rela(struct readelf * re,struct section * s,Elf_Data * d)31292c23cb7cSEd Maste dump_rela(struct readelf *re, struct section *s, Elf_Data *d)
31302c23cb7cSEd Maste {
31312c23cb7cSEd Maste 	GElf_Rela r;
31322c23cb7cSEd Maste 	const char *symname;
31332c23cb7cSEd Maste 	uint64_t symval;
31342c23cb7cSEd Maste 	int i, len;
3135b6d812d2SEd Maste 	uint32_t type;
3136b6d812d2SEd Maste 	uint8_t type2, type3;
31372c23cb7cSEd Maste 
3138656f49f8SEd Maste 	if (s->link >= re->shnum)
3139656f49f8SEd Maste 		return;
3140656f49f8SEd Maste 
31412c23cb7cSEd Maste #define	RELA_HDR "r_offset", "r_info", "r_type", "st_value", \
31422c23cb7cSEd Maste 		"st_name + r_addend"
31432c23cb7cSEd Maste #define	RELA_CT32 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
3144b6b6f9ccSEd Maste 		elftc_reloc_type_str(re->ehdr.e_machine,	    \
3145b6b6f9ccSEd Maste 		ELF32_R_TYPE(r.r_info)), (uintmax_t)symval, symname
31462c23cb7cSEd Maste #define	RELA_CT64 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
3147b6d812d2SEd Maste 		elftc_reloc_type_str(re->ehdr.e_machine, type),	    \
3148b6d812d2SEd Maste 		(uintmax_t)symval, symname
31492c23cb7cSEd Maste 
31502c23cb7cSEd Maste 	printf("\nRelocation section with addend (%s):\n", s->name);
31512c23cb7cSEd Maste 	if (re->ec == ELFCLASS32)
31522c23cb7cSEd Maste 		printf("%-8s %-8s %-19s %-8s %s\n", RELA_HDR);
31532c23cb7cSEd Maste 	else {
31542c23cb7cSEd Maste 		if (re->options & RE_WW)
31552c23cb7cSEd Maste 			printf("%-16s %-16s %-24s %-16s %s\n", RELA_HDR);
31562c23cb7cSEd Maste 		else
31572c23cb7cSEd Maste 			printf("%-12s %-12s %-19s %-16s %s\n", RELA_HDR);
31582c23cb7cSEd Maste 	}
315971edbbfdSEd Maste 	assert(d->d_size == s->sz);
316071edbbfdSEd Maste 	if (!get_ent_count(s, &len))
316171edbbfdSEd Maste 		return;
31622c23cb7cSEd Maste 	for (i = 0; i < len; i++) {
31632c23cb7cSEd Maste 		if (gelf_getrela(d, i, &r) != &r) {
31642c23cb7cSEd Maste 			warnx("gelf_getrel failed: %s", elf_errmsg(-1));
31652c23cb7cSEd Maste 			continue;
31662c23cb7cSEd Maste 		}
31672c23cb7cSEd Maste 		symname = get_symbol_name(re, s->link, GELF_R_SYM(r.r_info));
31682c23cb7cSEd Maste 		symval = get_symbol_value(re, s->link, GELF_R_SYM(r.r_info));
31692c23cb7cSEd Maste 		if (re->ec == ELFCLASS32) {
31702c23cb7cSEd Maste 			r.r_info = ELF32_R_INFO(ELF64_R_SYM(r.r_info),
31712c23cb7cSEd Maste 			    ELF64_R_TYPE(r.r_info));
31722c23cb7cSEd Maste 			printf("%8.8jx %8.8jx %-19.19s %8.8jx %s", RELA_CT32);
31732c23cb7cSEd Maste 			printf(" + %x\n", (uint32_t) r.r_addend);
31742c23cb7cSEd Maste 		} else {
3175b6d812d2SEd Maste 			type = ELF64_R_TYPE(r.r_info);
3176b6d812d2SEd Maste 			if (re->ehdr.e_machine == EM_MIPS) {
3177b6d812d2SEd Maste 				type2 = (type >> 8) & 0xFF;
3178b6d812d2SEd Maste 				type3 = (type >> 16) & 0xFF;
3179b6d812d2SEd Maste 				type = type & 0xFF;
3180d0bd2dadSEd Maste 			} else {
3181d0bd2dadSEd Maste 				type2 = type3 = 0;
3182b6d812d2SEd Maste 			}
31832c23cb7cSEd Maste 			if (re->options & RE_WW)
31842c23cb7cSEd Maste 				printf("%16.16jx %16.16jx %-24.24s"
31852c23cb7cSEd Maste 				    " %16.16jx %s", RELA_CT64);
31862c23cb7cSEd Maste 			else
31872c23cb7cSEd Maste 				printf("%12.12jx %12.12jx %-19.19s"
31882c23cb7cSEd Maste 				    " %16.16jx %s", RELA_CT64);
31892c23cb7cSEd Maste 			printf(" + %jx\n", (uintmax_t) r.r_addend);
3190b6d812d2SEd Maste 			if (re->ehdr.e_machine == EM_MIPS) {
3191b6d812d2SEd Maste 				if (re->options & RE_WW) {
3192b6d812d2SEd Maste 					printf("%32s: %s\n", "Type2",
3193b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
3194b6d812d2SEd Maste 					    type2));
3195b6d812d2SEd Maste 					printf("%32s: %s\n", "Type3",
3196b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
3197b6d812d2SEd Maste 					    type3));
3198b6d812d2SEd Maste 				} else {
3199b6d812d2SEd Maste 					printf("%24s: %s\n", "Type2",
3200b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
3201b6d812d2SEd Maste 					    type2));
3202b6d812d2SEd Maste 					printf("%24s: %s\n", "Type3",
3203b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
3204b6d812d2SEd Maste 					    type3));
3205b6d812d2SEd Maste 				}
3206b6d812d2SEd Maste 			}
32072c23cb7cSEd Maste 		}
32082c23cb7cSEd Maste 	}
32092c23cb7cSEd Maste 
32102c23cb7cSEd Maste #undef	RELA_HDR
32112c23cb7cSEd Maste #undef	RELA_CT
32122c23cb7cSEd Maste }
32132c23cb7cSEd Maste 
32142c23cb7cSEd Maste static void
dump_reloc(struct readelf * re)32152c23cb7cSEd Maste dump_reloc(struct readelf *re)
32162c23cb7cSEd Maste {
32172c23cb7cSEd Maste 	struct section *s;
32182c23cb7cSEd Maste 	Elf_Data *d;
32192c23cb7cSEd Maste 	int i, elferr;
32202c23cb7cSEd Maste 
32212c23cb7cSEd Maste 	for (i = 0; (size_t)i < re->shnum; i++) {
32222c23cb7cSEd Maste 		s = &re->sl[i];
32232c23cb7cSEd Maste 		if (s->type == SHT_REL || s->type == SHT_RELA) {
32242c23cb7cSEd Maste 			(void) elf_errno();
32252c23cb7cSEd Maste 			if ((d = elf_getdata(s->scn, NULL)) == NULL) {
32262c23cb7cSEd Maste 				elferr = elf_errno();
32272c23cb7cSEd Maste 				if (elferr != 0)
32282c23cb7cSEd Maste 					warnx("elf_getdata failed: %s",
32292c23cb7cSEd Maste 					    elf_errmsg(elferr));
32302c23cb7cSEd Maste 				continue;
32312c23cb7cSEd Maste 			}
32322c23cb7cSEd Maste 			if (s->type == SHT_REL)
32332c23cb7cSEd Maste 				dump_rel(re, s, d);
32342c23cb7cSEd Maste 			else
32352c23cb7cSEd Maste 				dump_rela(re, s, d);
32362c23cb7cSEd Maste 		}
32372c23cb7cSEd Maste 	}
32382c23cb7cSEd Maste }
32392c23cb7cSEd Maste 
32402c23cb7cSEd Maste static void
dump_symtab(struct readelf * re,int i)32412c23cb7cSEd Maste dump_symtab(struct readelf *re, int i)
32422c23cb7cSEd Maste {
32432c23cb7cSEd Maste 	struct section *s;
32442c23cb7cSEd Maste 	Elf_Data *d;
32452c23cb7cSEd Maste 	GElf_Sym sym;
32462c23cb7cSEd Maste 	const char *name;
3247656f49f8SEd Maste 	uint32_t stab;
3248656f49f8SEd Maste 	int elferr, j, len;
3249656f49f8SEd Maste 	uint16_t vs;
32502c23cb7cSEd Maste 
32512c23cb7cSEd Maste 	s = &re->sl[i];
3252656f49f8SEd Maste 	if (s->link >= re->shnum)
3253656f49f8SEd Maste 		return;
32542c23cb7cSEd Maste 	stab = s->link;
32552c23cb7cSEd Maste 	(void) elf_errno();
32562c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
32572c23cb7cSEd Maste 		elferr = elf_errno();
32582c23cb7cSEd Maste 		if (elferr != 0)
32592c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
32602c23cb7cSEd Maste 		return;
32612c23cb7cSEd Maste 	}
32622c23cb7cSEd Maste 	if (d->d_size <= 0)
32632c23cb7cSEd Maste 		return;
326471edbbfdSEd Maste 	if (!get_ent_count(s, &len))
326571edbbfdSEd Maste 		return;
326676614563SEd Maste 	printf("\nSymbol table '%s' contains %d entries:\n", s->name, len);
32672c23cb7cSEd Maste 	printf("%7s%9s%14s%5s%8s%6s%9s%5s\n", "Num:", "Value", "Size", "Type",
32682c23cb7cSEd Maste 	    "Bind", "Vis", "Ndx", "Name");
32692c23cb7cSEd Maste 
327071edbbfdSEd Maste 	for (j = 0; j < len; j++) {
32712c23cb7cSEd Maste 		if (gelf_getsym(d, j, &sym) != &sym) {
32722c23cb7cSEd Maste 			warnx("gelf_getsym failed: %s", elf_errmsg(-1));
32732c23cb7cSEd Maste 			continue;
32742c23cb7cSEd Maste 		}
32752c23cb7cSEd Maste 		printf("%6d:", j);
32762c23cb7cSEd Maste 		printf(" %16.16jx", (uintmax_t) sym.st_value);
3277839529caSEd Maste 		printf(" %5ju", (uintmax_t) sym.st_size);
3278839529caSEd Maste 		printf(" %-7s", st_type(re->ehdr.e_machine,
3279b6b6f9ccSEd Maste 		    re->ehdr.e_ident[EI_OSABI], GELF_ST_TYPE(sym.st_info)));
32802c23cb7cSEd Maste 		printf(" %-6s", st_bind(GELF_ST_BIND(sym.st_info)));
32812c23cb7cSEd Maste 		printf(" %-8s", st_vis(GELF_ST_VISIBILITY(sym.st_other)));
32822c23cb7cSEd Maste 		printf(" %3s", st_shndx(sym.st_shndx));
32832c23cb7cSEd Maste 		if ((name = elf_strptr(re->elf, stab, sym.st_name)) != NULL)
32842c23cb7cSEd Maste 			printf(" %s", name);
32852c23cb7cSEd Maste 		/* Append symbol version string for SHT_DYNSYM symbol table. */
32862c23cb7cSEd Maste 		if (s->type == SHT_DYNSYM && re->ver != NULL &&
32872c23cb7cSEd Maste 		    re->vs != NULL && re->vs[j] > 1) {
3288656f49f8SEd Maste 			vs = re->vs[j] & VERSYM_VERSION;
3289656f49f8SEd Maste 			if (vs >= re->ver_sz || re->ver[vs].name == NULL) {
3290656f49f8SEd Maste 				warnx("invalid versym version index %u", vs);
3291656f49f8SEd Maste 				break;
3292656f49f8SEd Maste 			}
3293656f49f8SEd Maste 			if (re->vs[j] & VERSYM_HIDDEN || re->ver[vs].type == 0)
3294656f49f8SEd Maste 				printf("@%s (%d)", re->ver[vs].name, vs);
32952c23cb7cSEd Maste 			else
3296656f49f8SEd Maste 				printf("@@%s (%d)", re->ver[vs].name, vs);
32972c23cb7cSEd Maste 		}
32982c23cb7cSEd Maste 		putchar('\n');
32992c23cb7cSEd Maste 	}
33002c23cb7cSEd Maste 
33012c23cb7cSEd Maste }
33022c23cb7cSEd Maste 
33032c23cb7cSEd Maste static void
dump_symtabs(struct readelf * re)33042c23cb7cSEd Maste dump_symtabs(struct readelf *re)
33052c23cb7cSEd Maste {
33062c23cb7cSEd Maste 	GElf_Dyn dyn;
33072c23cb7cSEd Maste 	Elf_Data *d;
33082c23cb7cSEd Maste 	struct section *s;
33092c23cb7cSEd Maste 	uint64_t dyn_off;
331071edbbfdSEd Maste 	int elferr, i, len;
33112c23cb7cSEd Maste 
33122c23cb7cSEd Maste 	/*
33132c23cb7cSEd Maste 	 * If -D is specified, only dump the symbol table specified by
33142c23cb7cSEd Maste 	 * the DT_SYMTAB entry in the .dynamic section.
33152c23cb7cSEd Maste 	 */
33162c23cb7cSEd Maste 	dyn_off = 0;
33172c23cb7cSEd Maste 	if (re->options & RE_DD) {
33182c23cb7cSEd Maste 		s = NULL;
33192c23cb7cSEd Maste 		for (i = 0; (size_t)i < re->shnum; i++)
33202c23cb7cSEd Maste 			if (re->sl[i].type == SHT_DYNAMIC) {
33212c23cb7cSEd Maste 				s = &re->sl[i];
33222c23cb7cSEd Maste 				break;
33232c23cb7cSEd Maste 			}
33242c23cb7cSEd Maste 		if (s == NULL)
33252c23cb7cSEd Maste 			return;
33262c23cb7cSEd Maste 		(void) elf_errno();
33272c23cb7cSEd Maste 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
33282c23cb7cSEd Maste 			elferr = elf_errno();
33292c23cb7cSEd Maste 			if (elferr != 0)
33302c23cb7cSEd Maste 				warnx("elf_getdata failed: %s", elf_errmsg(-1));
33312c23cb7cSEd Maste 			return;
33322c23cb7cSEd Maste 		}
33332c23cb7cSEd Maste 		if (d->d_size <= 0)
33342c23cb7cSEd Maste 			return;
333571edbbfdSEd Maste 		if (!get_ent_count(s, &len))
333671edbbfdSEd Maste 			return;
33372c23cb7cSEd Maste 
333871edbbfdSEd Maste 		for (i = 0; i < len; i++) {
33392c23cb7cSEd Maste 			if (gelf_getdyn(d, i, &dyn) != &dyn) {
33402c23cb7cSEd Maste 				warnx("gelf_getdyn failed: %s", elf_errmsg(-1));
33412c23cb7cSEd Maste 				continue;
33422c23cb7cSEd Maste 			}
33432c23cb7cSEd Maste 			if (dyn.d_tag == DT_SYMTAB) {
33442c23cb7cSEd Maste 				dyn_off = dyn.d_un.d_val;
33452c23cb7cSEd Maste 				break;
33462c23cb7cSEd Maste 			}
33472c23cb7cSEd Maste 		}
33482c23cb7cSEd Maste 	}
33492c23cb7cSEd Maste 
33502c23cb7cSEd Maste 	/* Find and dump symbol tables. */
33512c23cb7cSEd Maste 	for (i = 0; (size_t)i < re->shnum; i++) {
33522c23cb7cSEd Maste 		s = &re->sl[i];
33532c23cb7cSEd Maste 		if (s->type == SHT_SYMTAB || s->type == SHT_DYNSYM) {
33542c23cb7cSEd Maste 			if (re->options & RE_DD) {
33552c23cb7cSEd Maste 				if (dyn_off == s->addr) {
33562c23cb7cSEd Maste 					dump_symtab(re, i);
33572c23cb7cSEd Maste 					break;
33582c23cb7cSEd Maste 				}
33592c23cb7cSEd Maste 			} else
33602c23cb7cSEd Maste 				dump_symtab(re, i);
33612c23cb7cSEd Maste 		}
33622c23cb7cSEd Maste 	}
33632c23cb7cSEd Maste }
33642c23cb7cSEd Maste 
33652c23cb7cSEd Maste static void
dump_svr4_hash(struct section * s)33662c23cb7cSEd Maste dump_svr4_hash(struct section *s)
33672c23cb7cSEd Maste {
33682c23cb7cSEd Maste 	Elf_Data	*d;
33692c23cb7cSEd Maste 	uint32_t	*buf;
33702c23cb7cSEd Maste 	uint32_t	 nbucket, nchain;
33712c23cb7cSEd Maste 	uint32_t	*bucket, *chain;
33722c23cb7cSEd Maste 	uint32_t	*bl, *c, maxl, total;
33732c23cb7cSEd Maste 	int		 elferr, i, j;
33742c23cb7cSEd Maste 
33752c23cb7cSEd Maste 	/* Read and parse the content of .hash section. */
33762c23cb7cSEd Maste 	(void) elf_errno();
33772c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
33782c23cb7cSEd Maste 		elferr = elf_errno();
33792c23cb7cSEd Maste 		if (elferr != 0)
33802c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
33812c23cb7cSEd Maste 		return;
33822c23cb7cSEd Maste 	}
33832c23cb7cSEd Maste 	if (d->d_size < 2 * sizeof(uint32_t)) {
33842c23cb7cSEd Maste 		warnx(".hash section too small");
33852c23cb7cSEd Maste 		return;
33862c23cb7cSEd Maste 	}
33872c23cb7cSEd Maste 	buf = d->d_buf;
33882c23cb7cSEd Maste 	nbucket = buf[0];
33892c23cb7cSEd Maste 	nchain = buf[1];
33902c23cb7cSEd Maste 	if (nbucket <= 0 || nchain <= 0) {
33912c23cb7cSEd Maste 		warnx("Malformed .hash section");
33922c23cb7cSEd Maste 		return;
33932c23cb7cSEd Maste 	}
33942c23cb7cSEd Maste 	if (d->d_size != (nbucket + nchain + 2) * sizeof(uint32_t)) {
33952c23cb7cSEd Maste 		warnx("Malformed .hash section");
33962c23cb7cSEd Maste 		return;
33972c23cb7cSEd Maste 	}
33982c23cb7cSEd Maste 	bucket = &buf[2];
33992c23cb7cSEd Maste 	chain = &buf[2 + nbucket];
34002c23cb7cSEd Maste 
34012c23cb7cSEd Maste 	maxl = 0;
34022c23cb7cSEd Maste 	if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
34032c23cb7cSEd Maste 		errx(EXIT_FAILURE, "calloc failed");
34042c23cb7cSEd Maste 	for (i = 0; (uint32_t)i < nbucket; i++)
34052c23cb7cSEd Maste 		for (j = bucket[i]; j > 0 && (uint32_t)j < nchain; j = chain[j])
34062c23cb7cSEd Maste 			if (++bl[i] > maxl)
34072c23cb7cSEd Maste 				maxl = bl[i];
34082c23cb7cSEd Maste 	if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
34092c23cb7cSEd Maste 		errx(EXIT_FAILURE, "calloc failed");
34102c23cb7cSEd Maste 	for (i = 0; (uint32_t)i < nbucket; i++)
34112c23cb7cSEd Maste 		c[bl[i]]++;
34122c23cb7cSEd Maste 	printf("\nHistogram for bucket list length (total of %u buckets):\n",
34132c23cb7cSEd Maste 	    nbucket);
34142c23cb7cSEd Maste 	printf(" Length\tNumber\t\t%% of total\tCoverage\n");
34152c23cb7cSEd Maste 	total = 0;
34162c23cb7cSEd Maste 	for (i = 0; (uint32_t)i <= maxl; i++) {
34172c23cb7cSEd Maste 		total += c[i] * i;
34182c23cb7cSEd Maste 		printf("%7u\t%-10u\t(%5.1f%%)\t%5.1f%%\n", i, c[i],
34192c23cb7cSEd Maste 		    c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
34202c23cb7cSEd Maste 	}
34212c23cb7cSEd Maste 	free(c);
34222c23cb7cSEd Maste 	free(bl);
34232c23cb7cSEd Maste }
34242c23cb7cSEd Maste 
34252c23cb7cSEd Maste static void
dump_svr4_hash64(struct readelf * re,struct section * s)34262c23cb7cSEd Maste dump_svr4_hash64(struct readelf *re, struct section *s)
34272c23cb7cSEd Maste {
34282c23cb7cSEd Maste 	Elf_Data	*d, dst;
34292c23cb7cSEd Maste 	uint64_t	*buf;
34302c23cb7cSEd Maste 	uint64_t	 nbucket, nchain;
34312c23cb7cSEd Maste 	uint64_t	*bucket, *chain;
34322c23cb7cSEd Maste 	uint64_t	*bl, *c, maxl, total;
34332c23cb7cSEd Maste 	int		 elferr, i, j;
34342c23cb7cSEd Maste 
34352c23cb7cSEd Maste 	/*
34362c23cb7cSEd Maste 	 * ALPHA uses 64-bit hash entries. Since libelf assumes that
34372c23cb7cSEd Maste 	 * .hash section contains only 32-bit entry, an explicit
34382c23cb7cSEd Maste 	 * gelf_xlatetom is needed here.
34392c23cb7cSEd Maste 	 */
34402c23cb7cSEd Maste 	(void) elf_errno();
34412c23cb7cSEd Maste 	if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
34422c23cb7cSEd Maste 		elferr = elf_errno();
34432c23cb7cSEd Maste 		if (elferr != 0)
34442c23cb7cSEd Maste 			warnx("elf_rawdata failed: %s",
34452c23cb7cSEd Maste 			    elf_errmsg(elferr));
34462c23cb7cSEd Maste 		return;
34472c23cb7cSEd Maste 	}
34482c23cb7cSEd Maste 	d->d_type = ELF_T_XWORD;
34492c23cb7cSEd Maste 	memcpy(&dst, d, sizeof(Elf_Data));
34502c23cb7cSEd Maste 	if (gelf_xlatetom(re->elf, &dst, d,
34512c23cb7cSEd Maste 		re->ehdr.e_ident[EI_DATA]) != &dst) {
34522c23cb7cSEd Maste 		warnx("gelf_xlatetom failed: %s", elf_errmsg(-1));
34532c23cb7cSEd Maste 		return;
34542c23cb7cSEd Maste 	}
34552c23cb7cSEd Maste 	if (dst.d_size < 2 * sizeof(uint64_t)) {
34562c23cb7cSEd Maste 		warnx(".hash section too small");
34572c23cb7cSEd Maste 		return;
34582c23cb7cSEd Maste 	}
34592c23cb7cSEd Maste 	buf = dst.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; (uint64_t)i < nbucket; i++)
34832c23cb7cSEd Maste 		c[bl[i]]++;
34842c23cb7cSEd Maste 	printf("Histogram for bucket list length (total of %ju buckets):\n",
34852c23cb7cSEd Maste 	    (uintmax_t)nbucket);
34862c23cb7cSEd Maste 	printf(" Length\tNumber\t\t%% of total\tCoverage\n");
34872c23cb7cSEd Maste 	total = 0;
34882c23cb7cSEd Maste 	for (i = 0; (uint64_t)i <= maxl; i++) {
34892c23cb7cSEd Maste 		total += c[i] * i;
34902c23cb7cSEd Maste 		printf("%7u\t%-10ju\t(%5.1f%%)\t%5.1f%%\n", i, (uintmax_t)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
dump_gnu_hash(struct readelf * re,struct section * s)34982c23cb7cSEd Maste dump_gnu_hash(struct readelf *re, struct section *s)
34992c23cb7cSEd Maste {
35002c23cb7cSEd Maste 	struct section	*ds;
35012c23cb7cSEd Maste 	Elf_Data	*d;
35022c23cb7cSEd Maste 	uint32_t	*buf;
35032c23cb7cSEd Maste 	uint32_t	*bucket, *chain;
3504cf781b2eSEd Maste 	uint32_t	 nbucket, nchain, symndx, maskwords;
35052c23cb7cSEd Maste 	uint32_t	*bl, *c, maxl, total;
35062c23cb7cSEd Maste 	int		 elferr, dynsymcount, i, j;
35072c23cb7cSEd Maste 
35082c23cb7cSEd Maste 	(void) elf_errno();
35092c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
35102c23cb7cSEd Maste 		elferr = elf_errno();
35112c23cb7cSEd Maste 		if (elferr != 0)
35122c23cb7cSEd Maste 			warnx("elf_getdata failed: %s",
35132c23cb7cSEd Maste 			    elf_errmsg(elferr));
35142c23cb7cSEd Maste 		return;
35152c23cb7cSEd Maste 	}
35162c23cb7cSEd Maste 	if (d->d_size < 4 * sizeof(uint32_t)) {
35172c23cb7cSEd Maste 		warnx(".gnu.hash section too small");
35182c23cb7cSEd Maste 		return;
35192c23cb7cSEd Maste 	}
35202c23cb7cSEd Maste 	buf = d->d_buf;
35212c23cb7cSEd Maste 	nbucket = buf[0];
35222c23cb7cSEd Maste 	symndx = buf[1];
35232c23cb7cSEd Maste 	maskwords = buf[2];
35242c23cb7cSEd Maste 	buf += 4;
3525656f49f8SEd Maste 	if (s->link >= re->shnum)
3526656f49f8SEd Maste 		return;
35272c23cb7cSEd Maste 	ds = &re->sl[s->link];
352871edbbfdSEd Maste 	if (!get_ent_count(ds, &dynsymcount))
352971edbbfdSEd Maste 		return;
3530b6b6f9ccSEd Maste 	if (symndx >= (uint32_t)dynsymcount) {
3531b6b6f9ccSEd Maste 		warnx("Malformed .gnu.hash section (symndx out of range)");
3532b6b6f9ccSEd Maste 		return;
3533b6b6f9ccSEd Maste 	}
35342c23cb7cSEd Maste 	nchain = dynsymcount - symndx;
35352c23cb7cSEd Maste 	if (d->d_size != 4 * sizeof(uint32_t) + maskwords *
35362c23cb7cSEd Maste 	    (re->ec == ELFCLASS32 ? sizeof(uint32_t) : sizeof(uint64_t)) +
35372c23cb7cSEd Maste 	    (nbucket + nchain) * sizeof(uint32_t)) {
35382c23cb7cSEd Maste 		warnx("Malformed .gnu.hash section");
35392c23cb7cSEd Maste 		return;
35402c23cb7cSEd Maste 	}
35412c23cb7cSEd Maste 	bucket = buf + (re->ec == ELFCLASS32 ? maskwords : maskwords * 2);
35422c23cb7cSEd Maste 	chain = bucket + nbucket;
35432c23cb7cSEd Maste 
35442c23cb7cSEd Maste 	maxl = 0;
35452c23cb7cSEd Maste 	if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
35462c23cb7cSEd Maste 		errx(EXIT_FAILURE, "calloc failed");
35472c23cb7cSEd Maste 	for (i = 0; (uint32_t)i < nbucket; i++)
35482c23cb7cSEd Maste 		for (j = bucket[i]; j > 0 && (uint32_t)j - symndx < nchain;
35492c23cb7cSEd Maste 		     j++) {
35502c23cb7cSEd Maste 			if (++bl[i] > maxl)
35512c23cb7cSEd Maste 				maxl = bl[i];
35522c23cb7cSEd Maste 			if (chain[j - symndx] & 1)
35532c23cb7cSEd Maste 				break;
35542c23cb7cSEd Maste 		}
35552c23cb7cSEd Maste 	if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
35562c23cb7cSEd Maste 		errx(EXIT_FAILURE, "calloc failed");
35572c23cb7cSEd Maste 	for (i = 0; (uint32_t)i < nbucket; i++)
35582c23cb7cSEd Maste 		c[bl[i]]++;
35592c23cb7cSEd Maste 	printf("Histogram for bucket list length (total of %u buckets):\n",
35602c23cb7cSEd Maste 	    nbucket);
35612c23cb7cSEd Maste 	printf(" Length\tNumber\t\t%% of total\tCoverage\n");
35622c23cb7cSEd Maste 	total = 0;
35632c23cb7cSEd Maste 	for (i = 0; (uint32_t)i <= maxl; i++) {
35642c23cb7cSEd Maste 		total += c[i] * i;
35652c23cb7cSEd Maste 		printf("%7u\t%-10u\t(%5.1f%%)\t%5.1f%%\n", i, c[i],
35662c23cb7cSEd Maste 		    c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
35672c23cb7cSEd Maste 	}
35682c23cb7cSEd Maste 	free(c);
35692c23cb7cSEd Maste 	free(bl);
35702c23cb7cSEd Maste }
35712c23cb7cSEd Maste 
3572aacbf3fbSAndrew Turner static struct flag_desc gnu_property_aarch64_feature_1_and_bits[] = {
3573aacbf3fbSAndrew Turner 	{ GNU_PROPERTY_AARCH64_FEATURE_1_BTI,	"BTI" },
3574aacbf3fbSAndrew Turner 	{ GNU_PROPERTY_AARCH64_FEATURE_1_PAC,	"PAC" },
3575aacbf3fbSAndrew Turner 	{ 0, NULL }
3576aacbf3fbSAndrew Turner };
3577aacbf3fbSAndrew Turner 
3578aacbf3fbSAndrew Turner static struct flag_desc_list gnu_property_aarch64[] = {
3579aacbf3fbSAndrew Turner 	{
3580aacbf3fbSAndrew Turner 	    GNU_PROPERTY_AARCH64_FEATURE_1_AND,
3581aacbf3fbSAndrew Turner 	    "AArch64 features",
3582aacbf3fbSAndrew Turner 	    gnu_property_aarch64_feature_1_and_bits
3583aacbf3fbSAndrew Turner 	},
3584aacbf3fbSAndrew Turner 	{ 0, NULL, NULL }
3585aacbf3fbSAndrew Turner };
3586aacbf3fbSAndrew Turner 
358714a345d9SEd Maste static struct flag_desc gnu_property_x86_feature_1_and_bits[] = {
358814a345d9SEd Maste 	{ GNU_PROPERTY_X86_FEATURE_1_IBT,	"IBT" },
358914a345d9SEd Maste 	{ GNU_PROPERTY_X86_FEATURE_1_SHSTK,	"SHSTK" },
359014a345d9SEd Maste 	{ 0, NULL }
359114a345d9SEd Maste };
359214a345d9SEd Maste 
3593aacbf3fbSAndrew Turner static struct flag_desc_list gnu_property_x86[] = {
3594aacbf3fbSAndrew Turner 	{
3595aacbf3fbSAndrew Turner 	    GNU_PROPERTY_X86_FEATURE_1_AND,
3596aacbf3fbSAndrew Turner 	    "x64 features",
3597aacbf3fbSAndrew Turner 	    gnu_property_x86_feature_1_and_bits
3598aacbf3fbSAndrew Turner 	},
3599aacbf3fbSAndrew Turner 	{ 0, NULL, NULL }
3600aacbf3fbSAndrew Turner };
3601aacbf3fbSAndrew Turner 
3602aacbf3fbSAndrew Turner static struct {
3603aacbf3fbSAndrew Turner 	unsigned int emachine;
3604aacbf3fbSAndrew Turner 	struct flag_desc_list *flag_list;
3605aacbf3fbSAndrew Turner } gnu_property_archs[] = {
3606aacbf3fbSAndrew Turner 	{ EM_AARCH64, gnu_property_aarch64 },
3607aacbf3fbSAndrew Turner 	{ EM_X86_64, gnu_property_x86 },
3608aacbf3fbSAndrew Turner 	{ 0, NULL }
3609aacbf3fbSAndrew Turner };
3610aacbf3fbSAndrew Turner 
361114a345d9SEd Maste static void
dump_gnu_property_type_0(struct readelf * re,const char * buf,size_t sz)361214a345d9SEd Maste dump_gnu_property_type_0(struct readelf *re, const char *buf, size_t sz)
361314a345d9SEd Maste {
3614aacbf3fbSAndrew Turner 	struct flag_desc_list *desc_list;
3615aacbf3fbSAndrew Turner 	struct flag_desc *desc;
361614a345d9SEd Maste 	size_t i;
361714a345d9SEd Maste 	uint32_t type, prop_sz;
361814a345d9SEd Maste 
361914a345d9SEd Maste 	printf("      Properties: ");
362014a345d9SEd Maste 	while (sz > 0) {
362114a345d9SEd Maste 		if (sz < 8)
362214a345d9SEd Maste 			goto bad;
362314a345d9SEd Maste 
362414a345d9SEd Maste 		type = *(const uint32_t *)(const void *)buf;
362514a345d9SEd Maste 		prop_sz = *(const uint32_t *)(const void *)(buf + 4);
362614a345d9SEd Maste 		buf += 8;
362714a345d9SEd Maste 		sz -= 8;
362814a345d9SEd Maste 
362914a345d9SEd Maste 		if (prop_sz > sz)
363014a345d9SEd Maste 			goto bad;
363114a345d9SEd Maste 
363214a345d9SEd Maste 		if (type >= GNU_PROPERTY_LOPROC &&
363314a345d9SEd Maste 		    type <= GNU_PROPERTY_HIPROC) {
3634aacbf3fbSAndrew Turner 			desc_list = NULL;
3635aacbf3fbSAndrew Turner 			for (i = 0; gnu_property_archs[i].flag_list != NULL;
3636aacbf3fbSAndrew Turner 			    i++) {
3637aacbf3fbSAndrew Turner 				if (gnu_property_archs[i].emachine ==
3638aacbf3fbSAndrew Turner 				    re->ehdr.e_machine) {
3639aacbf3fbSAndrew Turner 					desc_list =
3640aacbf3fbSAndrew Turner 					    gnu_property_archs[i].flag_list;
3641aacbf3fbSAndrew Turner 					break;
3642aacbf3fbSAndrew Turner 				}
3643aacbf3fbSAndrew Turner 			}
3644aacbf3fbSAndrew Turner 			if (desc_list == NULL) {
364514a345d9SEd Maste 				printf("machine type %x unknown\n",
364614a345d9SEd Maste 				    re->ehdr.e_machine);
364714a345d9SEd Maste 				goto unknown;
364814a345d9SEd Maste 			}
3649aacbf3fbSAndrew Turner 
3650aacbf3fbSAndrew Turner 			desc = NULL;
3651aacbf3fbSAndrew Turner 			for (i = 0; desc_list[i].desc != NULL; i++) {
3652aacbf3fbSAndrew Turner 				if (desc_list[i].type == type) {
3653aacbf3fbSAndrew Turner 					desc = desc_list[i].desc;
3654aacbf3fbSAndrew Turner 					break;
3655aacbf3fbSAndrew Turner 				}
3656aacbf3fbSAndrew Turner 			}
3657aacbf3fbSAndrew Turner 			if (desc != NULL) {
3658aacbf3fbSAndrew Turner 				printf("%s:", desc_list[i].desc_str);
365914a345d9SEd Maste 				if (prop_sz != 4)
366014a345d9SEd Maste 					goto bad;
3661aacbf3fbSAndrew Turner 				dump_flags(desc,
366214a345d9SEd Maste 				    *(const uint32_t *)(const void *)buf);
366314a345d9SEd Maste 			}
366414a345d9SEd Maste 		}
366514a345d9SEd Maste 
366614a345d9SEd Maste 		buf += roundup2(prop_sz, 8);
366714a345d9SEd Maste 		sz -= roundup2(prop_sz, 8);
366814a345d9SEd Maste 	}
366914a345d9SEd Maste 	return;
367014a345d9SEd Maste bad:
367114a345d9SEd Maste 	printf("corrupt GNU property\n");
367214a345d9SEd Maste unknown:
367314a345d9SEd Maste 	printf("remaining description data:");
367414a345d9SEd Maste 	for (i = 0; i < sz; i++)
367514a345d9SEd Maste 		printf(" %02x", (unsigned char)buf[i]);
367614a345d9SEd Maste 	printf("\n");
367714a345d9SEd Maste }
367814a345d9SEd Maste 
36792c23cb7cSEd Maste static void
dump_hash(struct readelf * re)36802c23cb7cSEd Maste dump_hash(struct readelf *re)
36812c23cb7cSEd Maste {
36822c23cb7cSEd Maste 	struct section	*s;
36832c23cb7cSEd Maste 	int		 i;
36842c23cb7cSEd Maste 
36852c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
36862c23cb7cSEd Maste 		s = &re->sl[i];
36872c23cb7cSEd Maste 		if (s->type == SHT_HASH || s->type == SHT_GNU_HASH) {
36882c23cb7cSEd Maste 			if (s->type == SHT_GNU_HASH)
36892c23cb7cSEd Maste 				dump_gnu_hash(re, s);
36902c23cb7cSEd Maste 			else if (re->ehdr.e_machine == EM_ALPHA &&
36912c23cb7cSEd Maste 			    s->entsize == 8)
36922c23cb7cSEd Maste 				dump_svr4_hash64(re, s);
36932c23cb7cSEd Maste 			else
36942c23cb7cSEd Maste 				dump_svr4_hash(s);
36952c23cb7cSEd Maste 		}
36962c23cb7cSEd Maste 	}
36972c23cb7cSEd Maste }
36982c23cb7cSEd Maste 
36992c23cb7cSEd Maste static void
dump_notes(struct readelf * re)37002c23cb7cSEd Maste dump_notes(struct readelf *re)
37012c23cb7cSEd Maste {
37022c23cb7cSEd Maste 	struct section *s;
37032c23cb7cSEd Maste 	const char *rawfile;
37042c23cb7cSEd Maste 	GElf_Phdr phdr;
37052c23cb7cSEd Maste 	Elf_Data *d;
3706b6b6f9ccSEd Maste 	size_t filesize, phnum;
37072c23cb7cSEd Maste 	int i, elferr;
37082c23cb7cSEd Maste 
37092c23cb7cSEd Maste 	if (re->ehdr.e_type == ET_CORE) {
37102c23cb7cSEd Maste 		/*
37112c23cb7cSEd Maste 		 * Search program headers in the core file for
37122c23cb7cSEd Maste 		 * PT_NOTE entry.
37132c23cb7cSEd Maste 		 */
37142c23cb7cSEd Maste 		if (elf_getphnum(re->elf, &phnum) == 0) {
37152c23cb7cSEd Maste 			warnx("elf_getphnum failed: %s", elf_errmsg(-1));
37162c23cb7cSEd Maste 			return;
37172c23cb7cSEd Maste 		}
37182c23cb7cSEd Maste 		if (phnum == 0)
37192c23cb7cSEd Maste 			return;
3720b6b6f9ccSEd Maste 		if ((rawfile = elf_rawfile(re->elf, &filesize)) == NULL) {
37212c23cb7cSEd Maste 			warnx("elf_rawfile failed: %s", elf_errmsg(-1));
37222c23cb7cSEd Maste 			return;
37232c23cb7cSEd Maste 		}
37242c23cb7cSEd Maste 		for (i = 0; (size_t) i < phnum; i++) {
37252c23cb7cSEd Maste 			if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
37262c23cb7cSEd Maste 				warnx("gelf_getphdr failed: %s",
37272c23cb7cSEd Maste 				    elf_errmsg(-1));
37282c23cb7cSEd Maste 				continue;
37292c23cb7cSEd Maste 			}
3730b6b6f9ccSEd Maste 			if (phdr.p_type == PT_NOTE) {
3731b6b6f9ccSEd Maste 				if (phdr.p_offset >= filesize ||
3732b6b6f9ccSEd Maste 				    phdr.p_filesz > filesize - phdr.p_offset) {
3733b6b6f9ccSEd Maste 					warnx("invalid PHDR offset");
3734b6b6f9ccSEd Maste 					continue;
3735b6b6f9ccSEd Maste 				}
37362c23cb7cSEd Maste 				dump_notes_content(re, rawfile + phdr.p_offset,
37372c23cb7cSEd Maste 				    phdr.p_filesz, phdr.p_offset);
37382c23cb7cSEd Maste 			}
3739b6b6f9ccSEd Maste 		}
37402c23cb7cSEd Maste 
37412c23cb7cSEd Maste 	} else {
37422c23cb7cSEd Maste 		/*
37432c23cb7cSEd Maste 		 * For objects other than core files, Search for
37442c23cb7cSEd Maste 		 * SHT_NOTE sections.
37452c23cb7cSEd Maste 		 */
37462c23cb7cSEd Maste 		for (i = 0; (size_t) i < re->shnum; i++) {
37472c23cb7cSEd Maste 			s = &re->sl[i];
37482c23cb7cSEd Maste 			if (s->type == SHT_NOTE) {
37492c23cb7cSEd Maste 				(void) elf_errno();
37502c23cb7cSEd Maste 				if ((d = elf_getdata(s->scn, NULL)) == NULL) {
37512c23cb7cSEd Maste 					elferr = elf_errno();
37522c23cb7cSEd Maste 					if (elferr != 0)
37532c23cb7cSEd Maste 						warnx("elf_getdata failed: %s",
37542c23cb7cSEd Maste 						    elf_errmsg(elferr));
37552c23cb7cSEd Maste 					continue;
37562c23cb7cSEd Maste 				}
37572c23cb7cSEd Maste 				dump_notes_content(re, d->d_buf, d->d_size,
37582c23cb7cSEd Maste 				    s->off);
37592c23cb7cSEd Maste 			}
37602c23cb7cSEd Maste 		}
37612c23cb7cSEd Maste 	}
37622c23cb7cSEd Maste }
37632c23cb7cSEd Maste 
37643f0e38d7SEd Maste static struct flag_desc note_feature_ctl_flags[] = {
37653f0e38d7SEd Maste 	{ NT_FREEBSD_FCTL_ASLR_DISABLE,		"ASLR_DISABLE" },
37668e7e7da5SEd Maste 	{ NT_FREEBSD_FCTL_PROTMAX_DISABLE,	"PROTMAX_DISABLE" },
37678e7e7da5SEd Maste 	{ NT_FREEBSD_FCTL_STKGAP_DISABLE,	"STKGAP_DISABLE" },
3768d06e23f9SEd Maste 	{ NT_FREEBSD_FCTL_WXNEEDED,		"WXNEEDED" },
3769e79b51e2SEd Maste 	{ NT_FREEBSD_FCTL_LA48,			"LA48" },
37703f0e38d7SEd Maste 	{ 0, NULL }
37713f0e38d7SEd Maste };
37723f0e38d7SEd Maste 
3773e982f6f9SEd Maste static bool
dump_note_string(const char * description,const char * s,size_t len)3774ff0f134bSEd Maste dump_note_string(const char *description, const char *s, size_t len)
3775ff0f134bSEd Maste {
3776ff0f134bSEd Maste 	size_t i;
3777ff0f134bSEd Maste 
3778ff0f134bSEd Maste 	if (len == 0 || s[--len] != '\0') {
3779e982f6f9SEd Maste 		return (false);
3780ff0f134bSEd Maste 	} else {
3781e982f6f9SEd Maste 		for (i = 0; i < len; i++)
3782e982f6f9SEd Maste 			if (!isprint(s[i]))
3783e982f6f9SEd Maste 				return (false);
3784ff0f134bSEd Maste 	}
3785ff0f134bSEd Maste 
3786ff0f134bSEd Maste 	printf("   %s: %s\n", description, s);
3787e982f6f9SEd Maste 	return (true);
3788ff0f134bSEd Maste }
3789e982f6f9SEd Maste 
3790e982f6f9SEd Maste struct note_desc {
3791e982f6f9SEd Maste 	uint32_t type;
3792e982f6f9SEd Maste 	const char *description;
3793e982f6f9SEd Maste 	bool (*fp)(const char *, const char *, size_t);
3794e982f6f9SEd Maste };
3795e982f6f9SEd Maste 
3796e982f6f9SEd Maste static struct note_desc xen_notes[] = {
3797e982f6f9SEd Maste 	{ 5, "Xen version", dump_note_string },
3798e982f6f9SEd Maste 	{ 6, "Guest OS", dump_note_string },
3799e982f6f9SEd Maste 	{ 7, "Guest version", dump_note_string },
3800e982f6f9SEd Maste 	{ 8, "Loader", dump_note_string },
3801e982f6f9SEd Maste 	{ 9, "PAE mode", dump_note_string },
3802e982f6f9SEd Maste 	{ 10, "Features", dump_note_string },
3803e982f6f9SEd Maste 	{ 11, "BSD symtab", dump_note_string },
3804e982f6f9SEd Maste 	{ 0, NULL, NULL }
3805e982f6f9SEd Maste };
3806ff0f134bSEd Maste 
3807ff0f134bSEd Maste static void
dump_notes_data(struct readelf * re,const char * name,uint32_t type,const char * buf,size_t sz)380814a345d9SEd Maste dump_notes_data(struct readelf *re, const char *name, uint32_t type,
380914a345d9SEd Maste     const char *buf, size_t sz)
38103f0e38d7SEd Maste {
3811e982f6f9SEd Maste 	struct note_desc *nd;
38123f0e38d7SEd Maste 	size_t i;
38133f0e38d7SEd Maste 	const uint32_t *ubuf;
38143f0e38d7SEd Maste 
38153f0e38d7SEd Maste 	/* Note data is at least 4-byte aligned. */
38163f0e38d7SEd Maste 	if (((uintptr_t)buf & 3) != 0) {
38173f0e38d7SEd Maste 		warnx("bad note data alignment");
38183f0e38d7SEd Maste 		goto unknown;
38193f0e38d7SEd Maste 	}
38203f0e38d7SEd Maste 	ubuf = (const uint32_t *)(const void *)buf;
38213f0e38d7SEd Maste 
38223f0e38d7SEd Maste 	if (strcmp(name, "FreeBSD") == 0) {
38233f0e38d7SEd Maste 		switch (type) {
38243f0e38d7SEd Maste 		case NT_FREEBSD_ABI_TAG:
38253f0e38d7SEd Maste 			if (sz != 4)
38263f0e38d7SEd Maste 				goto unknown;
38273f0e38d7SEd Maste 			printf("   ABI tag: %u\n", ubuf[0]);
38283f0e38d7SEd Maste 			return;
38293f0e38d7SEd Maste 		/* NT_FREEBSD_NOINIT_TAG carries no data, treat as unknown. */
38303f0e38d7SEd Maste 		case NT_FREEBSD_ARCH_TAG:
38312a399193SAlex Richardson 			printf("   Arch tag: %s\n", buf);
38323f0e38d7SEd Maste 			return;
38333f0e38d7SEd Maste 		case NT_FREEBSD_FEATURE_CTL:
38343f0e38d7SEd Maste 			if (sz != 4)
38353f0e38d7SEd Maste 				goto unknown;
38363f0e38d7SEd Maste 			printf("   Features:");
38373f0e38d7SEd Maste 			dump_flags(note_feature_ctl_flags, ubuf[0]);
38383f0e38d7SEd Maste 			return;
38393f0e38d7SEd Maste 		}
3840ca457394SEd Maste 	} else if (strcmp(name, "Go") == 0) {
3841ca457394SEd Maste 		if (type == 4) {
3842ca457394SEd Maste 			printf("   Build ID: ");
3843ca457394SEd Maste 			for (i = 0; i < sz; i++) {
3844ca457394SEd Maste 				printf(isprint(buf[i]) ? "%c" : "<%02x>",
3845ca457394SEd Maste 				    buf[i]);
3846ca457394SEd Maste 			}
3847ca457394SEd Maste 			printf("\n");
3848ca457394SEd Maste 			return;
3849ca457394SEd Maste 		}
385014a345d9SEd Maste 	} else if (strcmp(name, "GNU") == 0) {
385114a345d9SEd Maste 		switch (type) {
385214a345d9SEd Maste 		case NT_GNU_PROPERTY_TYPE_0:
385314a345d9SEd Maste 			dump_gnu_property_type_0(re, buf, sz);
385414a345d9SEd Maste 			return;
38556c37d603SEd Maste 		case NT_GNU_BUILD_ID:
38566c37d603SEd Maste 			printf("   Build ID: ");
38576c37d603SEd Maste 			for (i = 0; i < sz; i++)
38586c37d603SEd Maste 				printf("%02x", (unsigned char)buf[i]);
38596c37d603SEd Maste 			printf("\n");
38606c37d603SEd Maste 			return;
386114a345d9SEd Maste 		}
3862ff0f134bSEd Maste 	} else if (strcmp(name, "Xen") == 0) {
3863e982f6f9SEd Maste 		for (nd = xen_notes; nd->description != NULL; nd++) {
3864e982f6f9SEd Maste 			if (nd->type == type) {
3865e982f6f9SEd Maste 				if (nd->fp(nd->description, buf, sz))
3866ff0f134bSEd Maste 					return;
3867e982f6f9SEd Maste 				else
3868e982f6f9SEd Maste 					break;
3869e982f6f9SEd Maste 			}
3870ff0f134bSEd Maste 		}
38713f0e38d7SEd Maste 	}
38723f0e38d7SEd Maste unknown:
38733f0e38d7SEd Maste 	printf("   description data:");
38743f0e38d7SEd Maste 	for (i = 0; i < sz; i++)
38753f0e38d7SEd Maste 		printf(" %02x", (unsigned char)buf[i]);
38763f0e38d7SEd Maste 	printf("\n");
38773f0e38d7SEd Maste }
38783f0e38d7SEd Maste 
38792c23cb7cSEd Maste static void
dump_notes_content(struct readelf * re,const char * buf,size_t sz,off_t off)38802c23cb7cSEd Maste dump_notes_content(struct readelf *re, const char *buf, size_t sz, off_t off)
38812c23cb7cSEd Maste {
38822c23cb7cSEd Maste 	Elf_Note *note;
388302b08c90SEd Maste 	const char *end, *name;
388489839cadSEd Maste 	uint32_t namesz, descsz;
38852c23cb7cSEd Maste 
38862c23cb7cSEd Maste 	printf("\nNotes at offset %#010jx with length %#010jx:\n",
38872c23cb7cSEd Maste 	    (uintmax_t) off, (uintmax_t) sz);
38882c23cb7cSEd Maste 	printf("  %-13s %-15s %s\n", "Owner", "Data size", "Description");
38892c23cb7cSEd Maste 	end = buf + sz;
38902c23cb7cSEd Maste 	while (buf < end) {
389102b08c90SEd Maste 		if (buf + sizeof(*note) > end) {
389202b08c90SEd Maste 			warnx("invalid note header");
389302b08c90SEd Maste 			return;
389402b08c90SEd Maste 		}
38952c23cb7cSEd Maste 		note = (Elf_Note *)(uintptr_t) buf;
389689839cadSEd Maste 		namesz = roundup2(note->n_namesz, 4);
389789839cadSEd Maste 		descsz = roundup2(note->n_descsz, 4);
389889839cadSEd Maste 		if (namesz < note->n_namesz || descsz < note->n_descsz ||
389989839cadSEd Maste 		    buf + namesz + descsz > end) {
390089839cadSEd Maste 			warnx("invalid note header");
3901721ac29cSEd Maste 			return;
3902721ac29cSEd Maste 		}
390389839cadSEd Maste 		buf += sizeof(Elf_Note);
3904675f752cSEd Maste 		name = buf;
390589839cadSEd Maste 		buf += namesz;
390602b08c90SEd Maste 		/*
390702b08c90SEd Maste 		 * The name field is required to be nul-terminated, and
390802b08c90SEd Maste 		 * n_namesz includes the terminating nul in observed
390902b08c90SEd Maste 		 * implementations (contrary to the ELF-64 spec). A special
391002b08c90SEd Maste 		 * case is needed for cores generated by some older Linux
391102b08c90SEd Maste 		 * versions, which write a note named "CORE" without a nul
391202b08c90SEd Maste 		 * terminator and n_namesz = 4.
391302b08c90SEd Maste 		 */
391402b08c90SEd Maste 		if (note->n_namesz == 0)
391502b08c90SEd Maste 			name = "";
391602b08c90SEd Maste 		else if (note->n_namesz == 4 && strncmp(name, "CORE", 4) == 0)
391702b08c90SEd Maste 			name = "CORE";
391802b08c90SEd Maste 		else if (strnlen(name, note->n_namesz) >= note->n_namesz)
391902b08c90SEd Maste 			name = "<invalid>";
392002b08c90SEd Maste 		printf("  %-13s %#010jx", name, (uintmax_t) note->n_descsz);
392102b08c90SEd Maste 		printf("      %s\n", note_type(name, re->ehdr.e_type,
392202b08c90SEd Maste 		    note->n_type));
392314a345d9SEd Maste 		dump_notes_data(re, name, note->n_type, buf, note->n_descsz);
392489839cadSEd Maste 		buf += descsz;
39252c23cb7cSEd Maste 	}
39262c23cb7cSEd Maste }
39272c23cb7cSEd Maste 
39282c23cb7cSEd Maste /*
39292c23cb7cSEd Maste  * Symbol versioning sections are the same for 32bit and 64bit
39302c23cb7cSEd Maste  * ELF objects.
39312c23cb7cSEd Maste  */
39322c23cb7cSEd Maste #define Elf_Verdef	Elf32_Verdef
39332c23cb7cSEd Maste #define	Elf_Verdaux	Elf32_Verdaux
39342c23cb7cSEd Maste #define	Elf_Verneed	Elf32_Verneed
39352c23cb7cSEd Maste #define	Elf_Vernaux	Elf32_Vernaux
39362c23cb7cSEd Maste 
39372c23cb7cSEd Maste #define	SAVE_VERSION_NAME(x, n, t)					\
39382c23cb7cSEd Maste 	do {								\
39392c23cb7cSEd Maste 		while (x >= re->ver_sz) {				\
39402c23cb7cSEd Maste 			nv = realloc(re->ver,				\
39412c23cb7cSEd Maste 			    sizeof(*re->ver) * re->ver_sz * 2);		\
39422c23cb7cSEd Maste 			if (nv == NULL) {				\
39432c23cb7cSEd Maste 				warn("realloc failed");			\
39442c23cb7cSEd Maste 				free(re->ver);				\
39452c23cb7cSEd Maste 				return;					\
39462c23cb7cSEd Maste 			}						\
39472c23cb7cSEd Maste 			re->ver = nv;					\
39482c23cb7cSEd Maste 			for (i = re->ver_sz; i < re->ver_sz * 2; i++) {	\
39492c23cb7cSEd Maste 				re->ver[i].name = NULL;			\
39502c23cb7cSEd Maste 				re->ver[i].type = 0;			\
39512c23cb7cSEd Maste 			}						\
39522c23cb7cSEd Maste 			re->ver_sz *= 2;				\
39532c23cb7cSEd Maste 		}							\
39542c23cb7cSEd Maste 		if (x > 1) {						\
39552c23cb7cSEd Maste 			re->ver[x].name = n;				\
39562c23cb7cSEd Maste 			re->ver[x].type = t;				\
39572c23cb7cSEd Maste 		}							\
39582c23cb7cSEd Maste 	} while (0)
39592c23cb7cSEd Maste 
39602c23cb7cSEd Maste 
39612c23cb7cSEd Maste static void
dump_verdef(struct readelf * re,int dump)39622c23cb7cSEd Maste dump_verdef(struct readelf *re, int dump)
39632c23cb7cSEd Maste {
39642c23cb7cSEd Maste 	struct section *s;
39652c23cb7cSEd Maste 	struct symver *nv;
39662c23cb7cSEd Maste 	Elf_Data *d;
39672c23cb7cSEd Maste 	Elf_Verdef *vd;
39682c23cb7cSEd Maste 	Elf_Verdaux *vda;
39692c23cb7cSEd Maste 	uint8_t *buf, *end, *buf2;
39702c23cb7cSEd Maste 	const char *name;
39712c23cb7cSEd Maste 	int elferr, i, j;
39722c23cb7cSEd Maste 
39732c23cb7cSEd Maste 	if ((s = re->vd_s) == NULL)
39742c23cb7cSEd Maste 		return;
3975656f49f8SEd Maste 	if (s->link >= re->shnum)
3976656f49f8SEd Maste 		return;
39772c23cb7cSEd Maste 
39782c23cb7cSEd Maste 	if (re->ver == NULL) {
39792c23cb7cSEd Maste 		re->ver_sz = 16;
39802c23cb7cSEd Maste 		if ((re->ver = calloc(re->ver_sz, sizeof(*re->ver))) ==
39812c23cb7cSEd Maste 		    NULL) {
39822c23cb7cSEd Maste 			warn("calloc failed");
39832c23cb7cSEd Maste 			return;
39842c23cb7cSEd Maste 		}
39852c23cb7cSEd Maste 		re->ver[0].name = "*local*";
39862c23cb7cSEd Maste 		re->ver[1].name = "*global*";
39872c23cb7cSEd Maste 	}
39882c23cb7cSEd Maste 
39892c23cb7cSEd Maste 	if (dump)
39902c23cb7cSEd Maste 		printf("\nVersion definition section (%s):\n", s->name);
39912c23cb7cSEd Maste 	(void) elf_errno();
39922c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
39932c23cb7cSEd Maste 		elferr = elf_errno();
39942c23cb7cSEd Maste 		if (elferr != 0)
39952c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
39962c23cb7cSEd Maste 		return;
39972c23cb7cSEd Maste 	}
39982c23cb7cSEd Maste 	if (d->d_size == 0)
39992c23cb7cSEd Maste 		return;
40002c23cb7cSEd Maste 
40012c23cb7cSEd Maste 	buf = d->d_buf;
40022c23cb7cSEd Maste 	end = buf + d->d_size;
40032c23cb7cSEd Maste 	while (buf + sizeof(Elf_Verdef) <= end) {
40042c23cb7cSEd Maste 		vd = (Elf_Verdef *) (uintptr_t) buf;
40052c23cb7cSEd Maste 		if (dump) {
40062c23cb7cSEd Maste 			printf("  0x%4.4lx", (unsigned long)
40072c23cb7cSEd Maste 			    (buf - (uint8_t *)d->d_buf));
40082c23cb7cSEd Maste 			printf(" vd_version: %u vd_flags: %d"
40092c23cb7cSEd Maste 			    " vd_ndx: %u vd_cnt: %u", vd->vd_version,
40102c23cb7cSEd Maste 			    vd->vd_flags, vd->vd_ndx, vd->vd_cnt);
40112c23cb7cSEd Maste 		}
40122c23cb7cSEd Maste 		buf2 = buf + vd->vd_aux;
40132c23cb7cSEd Maste 		j = 0;
40142c23cb7cSEd Maste 		while (buf2 + sizeof(Elf_Verdaux) <= end && j < vd->vd_cnt) {
40152c23cb7cSEd Maste 			vda = (Elf_Verdaux *) (uintptr_t) buf2;
40162c23cb7cSEd Maste 			name = get_string(re, s->link, vda->vda_name);
40172c23cb7cSEd Maste 			if (j == 0) {
40182c23cb7cSEd Maste 				if (dump)
40192c23cb7cSEd Maste 					printf(" vda_name: %s\n", name);
40202c23cb7cSEd Maste 				SAVE_VERSION_NAME((int)vd->vd_ndx, name, 1);
40212c23cb7cSEd Maste 			} else if (dump)
40222c23cb7cSEd Maste 				printf("  0x%4.4lx parent: %s\n",
40232c23cb7cSEd Maste 				    (unsigned long) (buf2 -
40242c23cb7cSEd Maste 				    (uint8_t *)d->d_buf), name);
40252c23cb7cSEd Maste 			if (vda->vda_next == 0)
40262c23cb7cSEd Maste 				break;
40272c23cb7cSEd Maste 			buf2 += vda->vda_next;
40282c23cb7cSEd Maste 			j++;
40292c23cb7cSEd Maste 		}
40302c23cb7cSEd Maste 		if (vd->vd_next == 0)
40312c23cb7cSEd Maste 			break;
40322c23cb7cSEd Maste 		buf += vd->vd_next;
40332c23cb7cSEd Maste 	}
40342c23cb7cSEd Maste }
40352c23cb7cSEd Maste 
40362c23cb7cSEd Maste static void
dump_verneed(struct readelf * re,int dump)40372c23cb7cSEd Maste dump_verneed(struct readelf *re, int dump)
40382c23cb7cSEd Maste {
40392c23cb7cSEd Maste 	struct section *s;
40402c23cb7cSEd Maste 	struct symver *nv;
40412c23cb7cSEd Maste 	Elf_Data *d;
40422c23cb7cSEd Maste 	Elf_Verneed *vn;
40432c23cb7cSEd Maste 	Elf_Vernaux *vna;
40442c23cb7cSEd Maste 	uint8_t *buf, *end, *buf2;
40452c23cb7cSEd Maste 	const char *name;
40462c23cb7cSEd Maste 	int elferr, i, j;
40472c23cb7cSEd Maste 
40482c23cb7cSEd Maste 	if ((s = re->vn_s) == NULL)
40492c23cb7cSEd Maste 		return;
4050656f49f8SEd Maste 	if (s->link >= re->shnum)
4051656f49f8SEd Maste 		return;
40522c23cb7cSEd Maste 
40532c23cb7cSEd Maste 	if (re->ver == NULL) {
40542c23cb7cSEd Maste 		re->ver_sz = 16;
40552c23cb7cSEd Maste 		if ((re->ver = calloc(re->ver_sz, sizeof(*re->ver))) ==
40562c23cb7cSEd Maste 		    NULL) {
40572c23cb7cSEd Maste 			warn("calloc failed");
40582c23cb7cSEd Maste 			return;
40592c23cb7cSEd Maste 		}
40602c23cb7cSEd Maste 		re->ver[0].name = "*local*";
40612c23cb7cSEd Maste 		re->ver[1].name = "*global*";
40622c23cb7cSEd Maste 	}
40632c23cb7cSEd Maste 
40642c23cb7cSEd Maste 	if (dump)
40652c23cb7cSEd Maste 		printf("\nVersion needed section (%s):\n", s->name);
40662c23cb7cSEd Maste 	(void) elf_errno();
40672c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
40682c23cb7cSEd Maste 		elferr = elf_errno();
40692c23cb7cSEd Maste 		if (elferr != 0)
40702c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
40712c23cb7cSEd Maste 		return;
40722c23cb7cSEd Maste 	}
40732c23cb7cSEd Maste 	if (d->d_size == 0)
40742c23cb7cSEd Maste 		return;
40752c23cb7cSEd Maste 
40762c23cb7cSEd Maste 	buf = d->d_buf;
40772c23cb7cSEd Maste 	end = buf + d->d_size;
40782c23cb7cSEd Maste 	while (buf + sizeof(Elf_Verneed) <= end) {
40792c23cb7cSEd Maste 		vn = (Elf_Verneed *) (uintptr_t) buf;
40802c23cb7cSEd Maste 		if (dump) {
40812c23cb7cSEd Maste 			printf("  0x%4.4lx", (unsigned long)
40822c23cb7cSEd Maste 			    (buf - (uint8_t *)d->d_buf));
40832c23cb7cSEd Maste 			printf(" vn_version: %u vn_file: %s vn_cnt: %u\n",
40842c23cb7cSEd Maste 			    vn->vn_version,
40852c23cb7cSEd Maste 			    get_string(re, s->link, vn->vn_file),
40862c23cb7cSEd Maste 			    vn->vn_cnt);
40872c23cb7cSEd Maste 		}
40882c23cb7cSEd Maste 		buf2 = buf + vn->vn_aux;
40892c23cb7cSEd Maste 		j = 0;
40902c23cb7cSEd Maste 		while (buf2 + sizeof(Elf_Vernaux) <= end && j < vn->vn_cnt) {
40912c23cb7cSEd Maste 			vna = (Elf32_Vernaux *) (uintptr_t) buf2;
40922c23cb7cSEd Maste 			if (dump)
40932c23cb7cSEd Maste 				printf("  0x%4.4lx", (unsigned long)
40942c23cb7cSEd Maste 				    (buf2 - (uint8_t *)d->d_buf));
40952c23cb7cSEd Maste 			name = get_string(re, s->link, vna->vna_name);
40962c23cb7cSEd Maste 			if (dump)
40972c23cb7cSEd Maste 				printf("   vna_name: %s vna_flags: %u"
40982c23cb7cSEd Maste 				    " vna_other: %u\n", name,
40992c23cb7cSEd Maste 				    vna->vna_flags, vna->vna_other);
41002c23cb7cSEd Maste 			SAVE_VERSION_NAME((int)vna->vna_other, name, 0);
41012c23cb7cSEd Maste 			if (vna->vna_next == 0)
41022c23cb7cSEd Maste 				break;
41032c23cb7cSEd Maste 			buf2 += vna->vna_next;
41042c23cb7cSEd Maste 			j++;
41052c23cb7cSEd Maste 		}
41062c23cb7cSEd Maste 		if (vn->vn_next == 0)
41072c23cb7cSEd Maste 			break;
41082c23cb7cSEd Maste 		buf += vn->vn_next;
41092c23cb7cSEd Maste 	}
41102c23cb7cSEd Maste }
41112c23cb7cSEd Maste 
41122c23cb7cSEd Maste static void
dump_versym(struct readelf * re)41132c23cb7cSEd Maste dump_versym(struct readelf *re)
41142c23cb7cSEd Maste {
41152c23cb7cSEd Maste 	int i;
4116656f49f8SEd Maste 	uint16_t vs;
41172c23cb7cSEd Maste 
41182c23cb7cSEd Maste 	if (re->vs_s == NULL || re->ver == NULL || re->vs == NULL)
41192c23cb7cSEd Maste 		return;
41202c23cb7cSEd Maste 	printf("\nVersion symbol section (%s):\n", re->vs_s->name);
41212c23cb7cSEd Maste 	for (i = 0; i < re->vs_sz; i++) {
41222c23cb7cSEd Maste 		if ((i & 3) == 0) {
41232c23cb7cSEd Maste 			if (i > 0)
41242c23cb7cSEd Maste 				putchar('\n');
41252c23cb7cSEd Maste 			printf("  %03x:", i);
41262c23cb7cSEd Maste 		}
4127656f49f8SEd Maste 		vs = re->vs[i] & VERSYM_VERSION;
4128656f49f8SEd Maste 		if (vs >= re->ver_sz || re->ver[vs].name == NULL) {
4129656f49f8SEd Maste 			warnx("invalid versym version index %u", re->vs[i]);
4130656f49f8SEd Maste 			break;
4131656f49f8SEd Maste 		}
4132656f49f8SEd Maste 		if (re->vs[i] & VERSYM_HIDDEN)
4133656f49f8SEd Maste 			printf(" %3xh %-12s ", vs,
4134656f49f8SEd Maste 			    re->ver[re->vs[i] & VERSYM_VERSION].name);
41352c23cb7cSEd Maste 		else
4136656f49f8SEd Maste 			printf(" %3x %-12s ", vs, re->ver[re->vs[i]].name);
41372c23cb7cSEd Maste 	}
41382c23cb7cSEd Maste 	putchar('\n');
41392c23cb7cSEd Maste }
41402c23cb7cSEd Maste 
41412c23cb7cSEd Maste static void
dump_ver(struct readelf * re)41422c23cb7cSEd Maste dump_ver(struct readelf *re)
41432c23cb7cSEd Maste {
41442c23cb7cSEd Maste 
41452c23cb7cSEd Maste 	if (re->vs_s && re->ver && re->vs)
41462c23cb7cSEd Maste 		dump_versym(re);
41472c23cb7cSEd Maste 	if (re->vd_s)
41482c23cb7cSEd Maste 		dump_verdef(re, 1);
41492c23cb7cSEd Maste 	if (re->vn_s)
41502c23cb7cSEd Maste 		dump_verneed(re, 1);
41512c23cb7cSEd Maste }
41522c23cb7cSEd Maste 
41532c23cb7cSEd Maste static void
search_ver(struct readelf * re)41542c23cb7cSEd Maste search_ver(struct readelf *re)
41552c23cb7cSEd Maste {
41562c23cb7cSEd Maste 	struct section *s;
41572c23cb7cSEd Maste 	Elf_Data *d;
41582c23cb7cSEd Maste 	int elferr, i;
41592c23cb7cSEd Maste 
41602c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
41612c23cb7cSEd Maste 		s = &re->sl[i];
41622c23cb7cSEd Maste 		if (s->type == SHT_SUNW_versym)
41632c23cb7cSEd Maste 			re->vs_s = s;
41642c23cb7cSEd Maste 		if (s->type == SHT_SUNW_verneed)
41652c23cb7cSEd Maste 			re->vn_s = s;
41662c23cb7cSEd Maste 		if (s->type == SHT_SUNW_verdef)
41672c23cb7cSEd Maste 			re->vd_s = s;
41682c23cb7cSEd Maste 	}
41692c23cb7cSEd Maste 	if (re->vd_s)
41702c23cb7cSEd Maste 		dump_verdef(re, 0);
41712c23cb7cSEd Maste 	if (re->vn_s)
41722c23cb7cSEd Maste 		dump_verneed(re, 0);
41732c23cb7cSEd Maste 	if (re->vs_s && re->ver != NULL) {
41742c23cb7cSEd Maste 		(void) elf_errno();
41752c23cb7cSEd Maste 		if ((d = elf_getdata(re->vs_s->scn, NULL)) == NULL) {
41762c23cb7cSEd Maste 			elferr = elf_errno();
41772c23cb7cSEd Maste 			if (elferr != 0)
41782c23cb7cSEd Maste 				warnx("elf_getdata failed: %s",
41792c23cb7cSEd Maste 				    elf_errmsg(elferr));
41802c23cb7cSEd Maste 			return;
41812c23cb7cSEd Maste 		}
41822c23cb7cSEd Maste 		if (d->d_size == 0)
41832c23cb7cSEd Maste 			return;
41842c23cb7cSEd Maste 		re->vs = d->d_buf;
41852c23cb7cSEd Maste 		re->vs_sz = d->d_size / sizeof(Elf32_Half);
41862c23cb7cSEd Maste 	}
41872c23cb7cSEd Maste }
41882c23cb7cSEd Maste 
41892c23cb7cSEd Maste #undef	Elf_Verdef
41902c23cb7cSEd Maste #undef	Elf_Verdaux
41912c23cb7cSEd Maste #undef	Elf_Verneed
41922c23cb7cSEd Maste #undef	Elf_Vernaux
41932c23cb7cSEd Maste #undef	SAVE_VERSION_NAME
41942c23cb7cSEd Maste 
41952c23cb7cSEd Maste /*
41962c23cb7cSEd Maste  * Elf32_Lib and Elf64_Lib are identical.
41972c23cb7cSEd Maste  */
41982c23cb7cSEd Maste #define	Elf_Lib		Elf32_Lib
41992c23cb7cSEd Maste 
42002c23cb7cSEd Maste static void
dump_liblist(struct readelf * re)42012c23cb7cSEd Maste dump_liblist(struct readelf *re)
42022c23cb7cSEd Maste {
42032c23cb7cSEd Maste 	struct section *s;
42042c23cb7cSEd Maste 	struct tm *t;
42052c23cb7cSEd Maste 	time_t ti;
42062c23cb7cSEd Maste 	char tbuf[20];
42072c23cb7cSEd Maste 	Elf_Data *d;
42082c23cb7cSEd Maste 	Elf_Lib *lib;
420971edbbfdSEd Maste 	int i, j, k, elferr, first, len;
42102c23cb7cSEd Maste 
42112c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
42122c23cb7cSEd Maste 		s = &re->sl[i];
42132c23cb7cSEd Maste 		if (s->type != SHT_GNU_LIBLIST)
42142c23cb7cSEd Maste 			continue;
4215656f49f8SEd Maste 		if (s->link >= re->shnum)
4216656f49f8SEd Maste 			continue;
42172c23cb7cSEd Maste 		(void) elf_errno();
42182c23cb7cSEd Maste 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
42192c23cb7cSEd Maste 			elferr = elf_errno();
42202c23cb7cSEd Maste 			if (elferr != 0)
42212c23cb7cSEd Maste 				warnx("elf_getdata failed: %s",
42222c23cb7cSEd Maste 				    elf_errmsg(elferr));
42232c23cb7cSEd Maste 			continue;
42242c23cb7cSEd Maste 		}
42252c23cb7cSEd Maste 		if (d->d_size <= 0)
42262c23cb7cSEd Maste 			continue;
42272c23cb7cSEd Maste 		lib = d->d_buf;
422871edbbfdSEd Maste 		if (!get_ent_count(s, &len))
422971edbbfdSEd Maste 			continue;
42302c23cb7cSEd Maste 		printf("\nLibrary list section '%s' ", s->name);
423171edbbfdSEd Maste 		printf("contains %d entries:\n", len);
42322c23cb7cSEd Maste 		printf("%12s%24s%18s%10s%6s\n", "Library", "Time Stamp",
42332c23cb7cSEd Maste 		    "Checksum", "Version", "Flags");
42342c23cb7cSEd Maste 		for (j = 0; (uint64_t) j < s->sz / s->entsize; j++) {
42352c23cb7cSEd Maste 			printf("%3d: ", j);
42362c23cb7cSEd Maste 			printf("%-20.20s ",
42372c23cb7cSEd Maste 			    get_string(re, s->link, lib->l_name));
42382c23cb7cSEd Maste 			ti = lib->l_time_stamp;
42392c23cb7cSEd Maste 			t = gmtime(&ti);
42402c23cb7cSEd Maste 			snprintf(tbuf, sizeof(tbuf), "%04d-%02d-%02dT%02d:%02d"
42412c23cb7cSEd Maste 			    ":%2d", t->tm_year + 1900, t->tm_mon + 1,
42422c23cb7cSEd Maste 			    t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
42432c23cb7cSEd Maste 			printf("%-19.19s ", tbuf);
42442c23cb7cSEd Maste 			printf("0x%08x ", lib->l_checksum);
42452c23cb7cSEd Maste 			printf("%-7d %#x", lib->l_version, lib->l_flags);
42462c23cb7cSEd Maste 			if (lib->l_flags != 0) {
42472c23cb7cSEd Maste 				first = 1;
42482c23cb7cSEd Maste 				putchar('(');
42492c23cb7cSEd Maste 				for (k = 0; l_flag[k].name != NULL; k++) {
42502c23cb7cSEd Maste 					if ((l_flag[k].value & lib->l_flags) ==
42512c23cb7cSEd Maste 					    0)
42522c23cb7cSEd Maste 						continue;
42532c23cb7cSEd Maste 					if (!first)
42542c23cb7cSEd Maste 						putchar(',');
42552c23cb7cSEd Maste 					else
42562c23cb7cSEd Maste 						first = 0;
42572c23cb7cSEd Maste 					printf("%s", l_flag[k].name);
42582c23cb7cSEd Maste 				}
42592c23cb7cSEd Maste 				putchar(')');
42602c23cb7cSEd Maste 			}
42612c23cb7cSEd Maste 			putchar('\n');
42622c23cb7cSEd Maste 			lib++;
42632c23cb7cSEd Maste 		}
42642c23cb7cSEd Maste 	}
42652c23cb7cSEd Maste }
42662c23cb7cSEd Maste 
42672c23cb7cSEd Maste #undef Elf_Lib
42682c23cb7cSEd Maste 
42693ef90571SEd Maste static void
dump_section_groups(struct readelf * re)42703ef90571SEd Maste dump_section_groups(struct readelf *re)
42713ef90571SEd Maste {
42723ef90571SEd Maste 	struct section *s;
42733ef90571SEd Maste 	const char *symname;
42743ef90571SEd Maste 	Elf_Data *d;
42753ef90571SEd Maste 	uint32_t *w;
42763ef90571SEd Maste 	int i, j, elferr;
42773ef90571SEd Maste 	size_t n;
42783ef90571SEd Maste 
42793ef90571SEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
42803ef90571SEd Maste 		s = &re->sl[i];
42813ef90571SEd Maste 		if (s->type != SHT_GROUP)
42823ef90571SEd Maste 			continue;
4283656f49f8SEd Maste 		if (s->link >= re->shnum)
4284656f49f8SEd Maste 			continue;
42853ef90571SEd Maste 		(void) elf_errno();
42863ef90571SEd Maste 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
42873ef90571SEd Maste 			elferr = elf_errno();
42883ef90571SEd Maste 			if (elferr != 0)
42893ef90571SEd Maste 				warnx("elf_getdata failed: %s",
42903ef90571SEd Maste 				    elf_errmsg(elferr));
42913ef90571SEd Maste 			continue;
42923ef90571SEd Maste 		}
42933ef90571SEd Maste 		if (d->d_size <= 0)
42943ef90571SEd Maste 			continue;
42953ef90571SEd Maste 
42963ef90571SEd Maste 		w = d->d_buf;
42973ef90571SEd Maste 
42983ef90571SEd Maste 		/* We only support COMDAT section. */
42993ef90571SEd Maste #ifndef GRP_COMDAT
43003ef90571SEd Maste #define	GRP_COMDAT 0x1
43013ef90571SEd Maste #endif
43023ef90571SEd Maste 		if ((*w++ & GRP_COMDAT) == 0)
43033ef90571SEd Maste 			return;
43043ef90571SEd Maste 
43053ef90571SEd Maste 		if (s->entsize == 0)
43063ef90571SEd Maste 			s->entsize = 4;
43073ef90571SEd Maste 
43083ef90571SEd Maste 		symname = get_symbol_name(re, s->link, s->info);
43093ef90571SEd Maste 		n = s->sz / s->entsize;
43103ef90571SEd Maste 		if (n-- < 1)
43113ef90571SEd Maste 			return;
43123ef90571SEd Maste 
43133ef90571SEd Maste 		printf("\nCOMDAT group section [%5d] `%s' [%s] contains %ju"
43143ef90571SEd Maste 		    " sections:\n", i, s->name, symname, (uintmax_t)n);
43153ef90571SEd Maste 		printf("   %-10.10s %s\n", "[Index]", "Name");
43163ef90571SEd Maste 		for (j = 0; (size_t) j < n; j++, w++) {
43173ef90571SEd Maste 			if (*w >= re->shnum) {
43183ef90571SEd Maste 				warnx("invalid section index: %u", *w);
43193ef90571SEd Maste 				continue;
43203ef90571SEd Maste 			}
43213ef90571SEd Maste 			printf("   [%5u]   %s\n", *w, re->sl[*w].name);
43223ef90571SEd Maste 		}
43233ef90571SEd Maste 	}
43243ef90571SEd Maste }
43253ef90571SEd Maste 
43262c23cb7cSEd Maste static uint8_t *
dump_unknown_tag(uint64_t tag,uint8_t * p,uint8_t * pe)432795fd7f26SEd Maste dump_unknown_tag(uint64_t tag, uint8_t *p, uint8_t *pe)
43282c23cb7cSEd Maste {
43292c23cb7cSEd Maste 	uint64_t val;
43302c23cb7cSEd Maste 
43312c23cb7cSEd Maste 	/*
43322c23cb7cSEd Maste 	 * According to ARM EABI: For tags > 32, even numbered tags have
43332c23cb7cSEd Maste 	 * a ULEB128 param and odd numbered ones have NUL-terminated
43342c23cb7cSEd Maste 	 * string param. This rule probably also applies for tags <= 32
43352c23cb7cSEd Maste 	 * if the object arch is not ARM.
43362c23cb7cSEd Maste 	 */
43372c23cb7cSEd Maste 
43382c23cb7cSEd Maste 	printf("  Tag_unknown_%ju: ", (uintmax_t) tag);
43392c23cb7cSEd Maste 
43402c23cb7cSEd Maste 	if (tag & 1) {
43412c23cb7cSEd Maste 		printf("%s\n", (char *) p);
43422c23cb7cSEd Maste 		p += strlen((char *) p) + 1;
43432c23cb7cSEd Maste 	} else {
434495fd7f26SEd Maste 		val = _decode_uleb128(&p, pe);
43452c23cb7cSEd Maste 		printf("%ju\n", (uintmax_t) val);
43462c23cb7cSEd Maste 	}
43472c23cb7cSEd Maste 
43482c23cb7cSEd Maste 	return (p);
43492c23cb7cSEd Maste }
43502c23cb7cSEd Maste 
43512c23cb7cSEd Maste static uint8_t *
dump_compatibility_tag(uint8_t * p,uint8_t * pe)435295fd7f26SEd Maste dump_compatibility_tag(uint8_t *p, uint8_t *pe)
43532c23cb7cSEd Maste {
43542c23cb7cSEd Maste 	uint64_t val;
43552c23cb7cSEd Maste 
435695fd7f26SEd Maste 	val = _decode_uleb128(&p, pe);
4357839529caSEd Maste 	printf("flag = %ju, vendor = %s\n", (uintmax_t) val, p);
43582c23cb7cSEd Maste 	p += strlen((char *) p) + 1;
43592c23cb7cSEd Maste 
43602c23cb7cSEd Maste 	return (p);
43612c23cb7cSEd Maste }
43622c23cb7cSEd Maste 
43632c23cb7cSEd Maste static void
dump_arm_attributes(struct readelf * re,uint8_t * p,uint8_t * pe)43642c23cb7cSEd Maste dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe)
43652c23cb7cSEd Maste {
43662c23cb7cSEd Maste 	uint64_t tag, val;
43672c23cb7cSEd Maste 	size_t i;
43682c23cb7cSEd Maste 	int found, desc;
43692c23cb7cSEd Maste 
43702c23cb7cSEd Maste 	(void) re;
43712c23cb7cSEd Maste 
43722c23cb7cSEd Maste 	while (p < pe) {
437395fd7f26SEd Maste 		tag = _decode_uleb128(&p, pe);
43742c23cb7cSEd Maste 		found = desc = 0;
43752c23cb7cSEd Maste 		for (i = 0; i < sizeof(aeabi_tags) / sizeof(aeabi_tags[0]);
43762c23cb7cSEd Maste 		     i++) {
43772c23cb7cSEd Maste 			if (tag == aeabi_tags[i].tag) {
43782c23cb7cSEd Maste 				found = 1;
43792c23cb7cSEd Maste 				printf("  %s: ", aeabi_tags[i].s_tag);
43802c23cb7cSEd Maste 				if (aeabi_tags[i].get_desc) {
43812c23cb7cSEd Maste 					desc = 1;
438295fd7f26SEd Maste 					val = _decode_uleb128(&p, pe);
43832c23cb7cSEd Maste 					printf("%s\n",
43842c23cb7cSEd Maste 					    aeabi_tags[i].get_desc(val));
43852c23cb7cSEd Maste 				}
43862c23cb7cSEd Maste 				break;
43872c23cb7cSEd Maste 			}
43882c23cb7cSEd Maste 			if (tag < aeabi_tags[i].tag)
43892c23cb7cSEd Maste 				break;
43902c23cb7cSEd Maste 		}
43912c23cb7cSEd Maste 		if (!found) {
439295fd7f26SEd Maste 			p = dump_unknown_tag(tag, p, pe);
43932c23cb7cSEd Maste 			continue;
43942c23cb7cSEd Maste 		}
43952c23cb7cSEd Maste 		if (desc)
43962c23cb7cSEd Maste 			continue;
43972c23cb7cSEd Maste 
43982c23cb7cSEd Maste 		switch (tag) {
43992c23cb7cSEd Maste 		case 4:		/* Tag_CPU_raw_name */
44002c23cb7cSEd Maste 		case 5:		/* Tag_CPU_name */
44012c23cb7cSEd Maste 		case 67:	/* Tag_conformance */
44022c23cb7cSEd Maste 			printf("%s\n", (char *) p);
44032c23cb7cSEd Maste 			p += strlen((char *) p) + 1;
44042c23cb7cSEd Maste 			break;
44052c23cb7cSEd Maste 		case 32:	/* Tag_compatibility */
440695fd7f26SEd Maste 			p = dump_compatibility_tag(p, pe);
44072c23cb7cSEd Maste 			break;
44082c23cb7cSEd Maste 		case 64:	/* Tag_nodefaults */
44092c23cb7cSEd Maste 			/* ignored, written as 0. */
441095fd7f26SEd Maste 			(void) _decode_uleb128(&p, pe);
44112c23cb7cSEd Maste 			printf("True\n");
44122c23cb7cSEd Maste 			break;
44132c23cb7cSEd Maste 		case 65:	/* Tag_also_compatible_with */
441495fd7f26SEd Maste 			val = _decode_uleb128(&p, pe);
44152c23cb7cSEd Maste 			/* Must be Tag_CPU_arch */
44162c23cb7cSEd Maste 			if (val != 6) {
44172c23cb7cSEd Maste 				printf("unknown\n");
44182c23cb7cSEd Maste 				break;
44192c23cb7cSEd Maste 			}
442095fd7f26SEd Maste 			val = _decode_uleb128(&p, pe);
44212c23cb7cSEd Maste 			printf("%s\n", aeabi_cpu_arch(val));
44222c23cb7cSEd Maste 			/* Skip NUL terminator. */
44232c23cb7cSEd Maste 			p++;
44242c23cb7cSEd Maste 			break;
44252c23cb7cSEd Maste 		default:
44262c23cb7cSEd Maste 			putchar('\n');
44272c23cb7cSEd Maste 			break;
44282c23cb7cSEd Maste 		}
44292c23cb7cSEd Maste 	}
44302c23cb7cSEd Maste }
44312c23cb7cSEd Maste 
44322c23cb7cSEd Maste #ifndef	Tag_GNU_MIPS_ABI_FP
44332c23cb7cSEd Maste #define	Tag_GNU_MIPS_ABI_FP	4
44342c23cb7cSEd Maste #endif
44352c23cb7cSEd Maste 
44362c23cb7cSEd Maste static void
dump_mips_attributes(struct readelf * re,uint8_t * p,uint8_t * pe)44372c23cb7cSEd Maste dump_mips_attributes(struct readelf *re, uint8_t *p, uint8_t *pe)
44382c23cb7cSEd Maste {
44392c23cb7cSEd Maste 	uint64_t tag, val;
44402c23cb7cSEd Maste 
44412c23cb7cSEd Maste 	(void) re;
44422c23cb7cSEd Maste 
44432c23cb7cSEd Maste 	while (p < pe) {
444495fd7f26SEd Maste 		tag = _decode_uleb128(&p, pe);
44452c23cb7cSEd Maste 		switch (tag) {
44462c23cb7cSEd Maste 		case Tag_GNU_MIPS_ABI_FP:
444795fd7f26SEd Maste 			val = _decode_uleb128(&p, pe);
44482c23cb7cSEd Maste 			printf("  Tag_GNU_MIPS_ABI_FP: %s\n", mips_abi_fp(val));
44492c23cb7cSEd Maste 			break;
44502c23cb7cSEd Maste 		case 32:	/* Tag_compatibility */
445195fd7f26SEd Maste 			p = dump_compatibility_tag(p, pe);
44522c23cb7cSEd Maste 			break;
44532c23cb7cSEd Maste 		default:
445495fd7f26SEd Maste 			p = dump_unknown_tag(tag, p, pe);
44552c23cb7cSEd Maste 			break;
44562c23cb7cSEd Maste 		}
44572c23cb7cSEd Maste 	}
44582c23cb7cSEd Maste }
44592c23cb7cSEd Maste 
44602c23cb7cSEd Maste #ifndef Tag_GNU_Power_ABI_FP
44612c23cb7cSEd Maste #define	Tag_GNU_Power_ABI_FP	4
44622c23cb7cSEd Maste #endif
44632c23cb7cSEd Maste 
44642c23cb7cSEd Maste #ifndef Tag_GNU_Power_ABI_Vector
44652c23cb7cSEd Maste #define	Tag_GNU_Power_ABI_Vector	8
44662c23cb7cSEd Maste #endif
44672c23cb7cSEd Maste 
44682c23cb7cSEd Maste static void
dump_ppc_attributes(uint8_t * p,uint8_t * pe)44692c23cb7cSEd Maste dump_ppc_attributes(uint8_t *p, uint8_t *pe)
44702c23cb7cSEd Maste {
44712c23cb7cSEd Maste 	uint64_t tag, val;
44722c23cb7cSEd Maste 
44732c23cb7cSEd Maste 	while (p < pe) {
447495fd7f26SEd Maste 		tag = _decode_uleb128(&p, pe);
44752c23cb7cSEd Maste 		switch (tag) {
44762c23cb7cSEd Maste 		case Tag_GNU_Power_ABI_FP:
447795fd7f26SEd Maste 			val = _decode_uleb128(&p, pe);
44782c23cb7cSEd Maste 			printf("  Tag_GNU_Power_ABI_FP: %s\n", ppc_abi_fp(val));
44792c23cb7cSEd Maste 			break;
44802c23cb7cSEd Maste 		case Tag_GNU_Power_ABI_Vector:
448195fd7f26SEd Maste 			val = _decode_uleb128(&p, pe);
44822c23cb7cSEd Maste 			printf("  Tag_GNU_Power_ABI_Vector: %s\n",
44832c23cb7cSEd Maste 			    ppc_abi_vector(val));
44842c23cb7cSEd Maste 			break;
44852c23cb7cSEd Maste 		case 32:	/* Tag_compatibility */
448695fd7f26SEd Maste 			p = dump_compatibility_tag(p, pe);
44872c23cb7cSEd Maste 			break;
44882c23cb7cSEd Maste 		default:
448995fd7f26SEd Maste 			p = dump_unknown_tag(tag, p, pe);
44902c23cb7cSEd Maste 			break;
44912c23cb7cSEd Maste 		}
44922c23cb7cSEd Maste 	}
44932c23cb7cSEd Maste }
44942c23cb7cSEd Maste 
44952c23cb7cSEd Maste static void
dump_attributes(struct readelf * re)44962c23cb7cSEd Maste dump_attributes(struct readelf *re)
44972c23cb7cSEd Maste {
44982c23cb7cSEd Maste 	struct section *s;
44992c23cb7cSEd Maste 	Elf_Data *d;
450095fd7f26SEd Maste 	uint8_t *p, *pe, *sp;
45012c23cb7cSEd Maste 	size_t len, seclen, nlen, sublen;
45022c23cb7cSEd Maste 	uint64_t val;
45032c23cb7cSEd Maste 	int tag, i, elferr;
45042c23cb7cSEd Maste 
45052c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
45062c23cb7cSEd Maste 		s = &re->sl[i];
45072c23cb7cSEd Maste 		if (s->type != SHT_GNU_ATTRIBUTES &&
45082c23cb7cSEd Maste 		    (re->ehdr.e_machine != EM_ARM || s->type != SHT_LOPROC + 3))
45092c23cb7cSEd Maste 			continue;
45102c23cb7cSEd Maste 		(void) elf_errno();
45112c23cb7cSEd Maste 		if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
45122c23cb7cSEd Maste 			elferr = elf_errno();
45132c23cb7cSEd Maste 			if (elferr != 0)
45142c23cb7cSEd Maste 				warnx("elf_rawdata failed: %s",
45152c23cb7cSEd Maste 				    elf_errmsg(elferr));
45162c23cb7cSEd Maste 			continue;
45172c23cb7cSEd Maste 		}
45182c23cb7cSEd Maste 		if (d->d_size <= 0)
45192c23cb7cSEd Maste 			continue;
45202c23cb7cSEd Maste 		p = d->d_buf;
452195fd7f26SEd Maste 		pe = p + d->d_size;
45222c23cb7cSEd Maste 		if (*p != 'A') {
45232c23cb7cSEd Maste 			printf("Unknown Attribute Section Format: %c\n",
45242c23cb7cSEd Maste 			    (char) *p);
45252c23cb7cSEd Maste 			continue;
45262c23cb7cSEd Maste 		}
45272c23cb7cSEd Maste 		len = d->d_size - 1;
45282c23cb7cSEd Maste 		p++;
45292c23cb7cSEd Maste 		while (len > 0) {
453071a0c925SEd Maste 			if (len < 4) {
453171a0c925SEd Maste 				warnx("truncated attribute section length");
453295fd7f26SEd Maste 				return;
453371a0c925SEd Maste 			}
45342c23cb7cSEd Maste 			seclen = re->dw_decode(&p, 4);
45352c23cb7cSEd Maste 			if (seclen > len) {
45362c23cb7cSEd Maste 				warnx("invalid attribute section length");
453795fd7f26SEd Maste 				return;
45382c23cb7cSEd Maste 			}
45392c23cb7cSEd Maste 			len -= seclen;
45402c23cb7cSEd Maste 			nlen = strlen((char *) p) + 1;
454171a0c925SEd Maste 			if (nlen + 4 > seclen) {
454271a0c925SEd Maste 				warnx("invalid attribute section name");
454395fd7f26SEd Maste 				return;
454471a0c925SEd Maste 			}
454571a0c925SEd Maste 			printf("Attribute Section: %s\n", (char *) p);
45462c23cb7cSEd Maste 			p += nlen;
45472c23cb7cSEd Maste 			seclen -= nlen + 4;
45482c23cb7cSEd Maste 			while (seclen > 0) {
45492c23cb7cSEd Maste 				sp = p;
45502c23cb7cSEd Maste 				tag = *p++;
45512c23cb7cSEd Maste 				sublen = re->dw_decode(&p, 4);
45522c23cb7cSEd Maste 				if (sublen > seclen) {
45532c23cb7cSEd Maste 					warnx("invalid attribute sub-section"
45542c23cb7cSEd Maste 					    " length");
455595fd7f26SEd Maste 					return;
45562c23cb7cSEd Maste 				}
45572c23cb7cSEd Maste 				seclen -= sublen;
45582c23cb7cSEd Maste 				printf("%s", top_tag(tag));
45592c23cb7cSEd Maste 				if (tag == 2 || tag == 3) {
45602c23cb7cSEd Maste 					putchar(':');
45612c23cb7cSEd Maste 					for (;;) {
456295fd7f26SEd Maste 						val = _decode_uleb128(&p, pe);
45632c23cb7cSEd Maste 						if (val == 0)
45642c23cb7cSEd Maste 							break;
45652c23cb7cSEd Maste 						printf(" %ju", (uintmax_t) val);
45662c23cb7cSEd Maste 					}
45672c23cb7cSEd Maste 				}
45682c23cb7cSEd Maste 				putchar('\n');
45692c23cb7cSEd Maste 				if (re->ehdr.e_machine == EM_ARM &&
45702c23cb7cSEd Maste 				    s->type == SHT_LOPROC + 3)
45712c23cb7cSEd Maste 					dump_arm_attributes(re, p, sp + sublen);
45722c23cb7cSEd Maste 				else if (re->ehdr.e_machine == EM_MIPS ||
45732c23cb7cSEd Maste 				    re->ehdr.e_machine == EM_MIPS_RS3_LE)
45742c23cb7cSEd Maste 					dump_mips_attributes(re, p,
45752c23cb7cSEd Maste 					    sp + sublen);
45762c23cb7cSEd Maste 				else if (re->ehdr.e_machine == EM_PPC)
45772c23cb7cSEd Maste 					dump_ppc_attributes(p, sp + sublen);
45782c23cb7cSEd Maste 				p = sp + sublen;
45792c23cb7cSEd Maste 			}
45802c23cb7cSEd Maste 		}
45812c23cb7cSEd Maste 	}
45822c23cb7cSEd Maste }
45832c23cb7cSEd Maste 
45842c23cb7cSEd Maste static void
dump_mips_specific_info(struct readelf * re)45852c23cb7cSEd Maste dump_mips_specific_info(struct readelf *re)
45862c23cb7cSEd Maste {
45872c23cb7cSEd Maste 	struct section *s;
4588bee2765cSEd Maste 	int i;
45892c23cb7cSEd Maste 
45902c23cb7cSEd Maste 	s = NULL;
45912c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
45922c23cb7cSEd Maste 		s = &re->sl[i];
45932c23cb7cSEd Maste 		if (s->name != NULL && (!strcmp(s->name, ".MIPS.options") ||
45942c23cb7cSEd Maste 		    (s->type == SHT_MIPS_OPTIONS))) {
45952c23cb7cSEd Maste 			dump_mips_options(re, s);
45962c23cb7cSEd Maste 		}
45972c23cb7cSEd Maste 	}
45982c23cb7cSEd Maste 
4599e9f3f88aSEd Maste 	if (s->name != NULL && (!strcmp(s->name, ".MIPS.abiflags") ||
4600e9f3f88aSEd Maste 	    (s->type == SHT_MIPS_ABIFLAGS)))
4601e9f3f88aSEd Maste 		dump_mips_abiflags(re, s);
4602e9f3f88aSEd Maste 
46032c23cb7cSEd Maste 	/*
4604bee2765cSEd Maste 	 * Dump .reginfo if present (although it will be ignored by an OS if a
4605bee2765cSEd Maste 	 * .MIPS.options section is present, according to SGI mips64 spec).
46062c23cb7cSEd Maste 	 */
46072c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
46082c23cb7cSEd Maste 		s = &re->sl[i];
46092c23cb7cSEd Maste 		if (s->name != NULL && (!strcmp(s->name, ".reginfo") ||
46102c23cb7cSEd Maste 		    (s->type == SHT_MIPS_REGINFO)))
46112c23cb7cSEd Maste 			dump_mips_reginfo(re, s);
46122c23cb7cSEd Maste 	}
46132c23cb7cSEd Maste }
46142c23cb7cSEd Maste 
46152c23cb7cSEd Maste static void
dump_mips_abiflags(struct readelf * re,struct section * s)4616e9f3f88aSEd Maste dump_mips_abiflags(struct readelf *re, struct section *s)
4617e9f3f88aSEd Maste {
4618e9f3f88aSEd Maste 	Elf_Data *d;
4619e9f3f88aSEd Maste 	uint8_t *p;
4620e9f3f88aSEd Maste 	int elferr;
4621e9f3f88aSEd Maste 	uint32_t isa_ext, ases, flags1, flags2;
4622e9f3f88aSEd Maste 	uint16_t version;
4623e9f3f88aSEd Maste 	uint8_t isa_level, isa_rev, gpr_size, cpr1_size, cpr2_size, fp_abi;
4624e9f3f88aSEd Maste 
4625e9f3f88aSEd Maste 	if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
4626e9f3f88aSEd Maste 		elferr = elf_errno();
4627e9f3f88aSEd Maste 		if (elferr != 0)
4628e9f3f88aSEd Maste 			warnx("elf_rawdata failed: %s",
4629e9f3f88aSEd Maste 			    elf_errmsg(elferr));
4630e9f3f88aSEd Maste 		return;
4631e9f3f88aSEd Maste 	}
4632e9f3f88aSEd Maste 	if (d->d_size != 24) {
4633e9f3f88aSEd Maste 		warnx("invalid MIPS abiflags section size");
4634e9f3f88aSEd Maste 		return;
4635e9f3f88aSEd Maste 	}
4636e9f3f88aSEd Maste 
4637e9f3f88aSEd Maste 	p = d->d_buf;
4638e9f3f88aSEd Maste 	version = re->dw_decode(&p, 2);
4639e9f3f88aSEd Maste 	printf("MIPS ABI Flags Version: %u", version);
4640e9f3f88aSEd Maste 	if (version != 0) {
4641e9f3f88aSEd Maste 		printf(" (unknown)\n\n");
4642e9f3f88aSEd Maste 		return;
4643e9f3f88aSEd Maste 	}
4644e9f3f88aSEd Maste 	printf("\n\n");
4645e9f3f88aSEd Maste 
4646e9f3f88aSEd Maste 	isa_level = re->dw_decode(&p, 1);
4647e9f3f88aSEd Maste 	isa_rev = re->dw_decode(&p, 1);
4648e9f3f88aSEd Maste 	gpr_size = re->dw_decode(&p, 1);
4649e9f3f88aSEd Maste 	cpr1_size = re->dw_decode(&p, 1);
4650e9f3f88aSEd Maste 	cpr2_size = re->dw_decode(&p, 1);
4651e9f3f88aSEd Maste 	fp_abi = re->dw_decode(&p, 1);
4652e9f3f88aSEd Maste 	isa_ext = re->dw_decode(&p, 4);
4653e9f3f88aSEd Maste 	ases = re->dw_decode(&p, 4);
4654e9f3f88aSEd Maste 	flags1 = re->dw_decode(&p, 4);
4655e9f3f88aSEd Maste 	flags2 = re->dw_decode(&p, 4);
4656e9f3f88aSEd Maste 
4657e9f3f88aSEd Maste 	printf("ISA: ");
4658e9f3f88aSEd Maste 	if (isa_rev <= 1)
4659e9f3f88aSEd Maste 		printf("MIPS%u\n", isa_level);
4660e9f3f88aSEd Maste 	else
4661e9f3f88aSEd Maste 		printf("MIPS%ur%u\n", isa_level, isa_rev);
4662e9f3f88aSEd Maste 	printf("GPR size: %d\n", get_mips_register_size(gpr_size));
4663e9f3f88aSEd Maste 	printf("CPR1 size: %d\n", get_mips_register_size(cpr1_size));
4664e9f3f88aSEd Maste 	printf("CPR2 size: %d\n", get_mips_register_size(cpr2_size));
4665e9f3f88aSEd Maste 	printf("FP ABI: ");
4666e9f3f88aSEd Maste 	switch (fp_abi) {
4667e9f3f88aSEd Maste 	case 3:
4668e9f3f88aSEd Maste 		printf("Soft float");
4669e9f3f88aSEd Maste 		break;
4670e9f3f88aSEd Maste 	default:
4671e9f3f88aSEd Maste 		printf("%u", fp_abi);
4672e9f3f88aSEd Maste 		break;
4673e9f3f88aSEd Maste 	}
4674e9f3f88aSEd Maste 	printf("\nISA Extension: %u\n", isa_ext);
4675e9f3f88aSEd Maste 	printf("ASEs: %u\n", ases);
4676e9f3f88aSEd Maste 	printf("FLAGS 1: %08x\n", flags1);
4677e9f3f88aSEd Maste 	printf("FLAGS 2: %08x\n", flags2);
4678e9f3f88aSEd Maste }
4679e9f3f88aSEd Maste 
4680e9f3f88aSEd Maste static int
get_mips_register_size(uint8_t flag)4681e9f3f88aSEd Maste get_mips_register_size(uint8_t flag)
4682e9f3f88aSEd Maste {
4683e9f3f88aSEd Maste 	switch (flag) {
4684e9f3f88aSEd Maste 	case 0: return 0;
4685e9f3f88aSEd Maste 	case 1: return 32;
4686e9f3f88aSEd Maste 	case 2: return 64;
4687e9f3f88aSEd Maste 	case 3: return 128;
4688e9f3f88aSEd Maste 	default: return -1;
4689e9f3f88aSEd Maste 	}
4690e9f3f88aSEd Maste }
4691e9f3f88aSEd Maste static void
dump_mips_reginfo(struct readelf * re,struct section * s)46922c23cb7cSEd Maste dump_mips_reginfo(struct readelf *re, struct section *s)
46932c23cb7cSEd Maste {
46942c23cb7cSEd Maste 	Elf_Data *d;
469571edbbfdSEd Maste 	int elferr, len;
46962c23cb7cSEd Maste 
46972c23cb7cSEd Maste 	(void) elf_errno();
46982c23cb7cSEd Maste 	if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
46992c23cb7cSEd Maste 		elferr = elf_errno();
47002c23cb7cSEd Maste 		if (elferr != 0)
47012c23cb7cSEd Maste 			warnx("elf_rawdata failed: %s",
47022c23cb7cSEd Maste 			    elf_errmsg(elferr));
47032c23cb7cSEd Maste 		return;
47042c23cb7cSEd Maste 	}
47052c23cb7cSEd Maste 	if (d->d_size <= 0)
47062c23cb7cSEd Maste 		return;
470771edbbfdSEd Maste 	if (!get_ent_count(s, &len))
470871edbbfdSEd Maste 		return;
47092c23cb7cSEd Maste 
471071edbbfdSEd Maste 	printf("\nSection '%s' contains %d entries:\n", s->name, len);
47112c23cb7cSEd Maste 	dump_mips_odk_reginfo(re, d->d_buf, d->d_size);
47122c23cb7cSEd Maste }
47132c23cb7cSEd Maste 
47142c23cb7cSEd Maste static void
dump_mips_options(struct readelf * re,struct section * s)47152c23cb7cSEd Maste dump_mips_options(struct readelf *re, struct section *s)
47162c23cb7cSEd Maste {
47172c23cb7cSEd Maste 	Elf_Data *d;
47182c23cb7cSEd Maste 	uint32_t info;
47192c23cb7cSEd Maste 	uint16_t sndx;
47202c23cb7cSEd Maste 	uint8_t *p, *pe;
47212c23cb7cSEd Maste 	uint8_t kind, size;
47222c23cb7cSEd Maste 	int elferr;
47232c23cb7cSEd Maste 
47242c23cb7cSEd Maste 	(void) elf_errno();
47252c23cb7cSEd Maste 	if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
47262c23cb7cSEd Maste 		elferr = elf_errno();
47272c23cb7cSEd Maste 		if (elferr != 0)
47282c23cb7cSEd Maste 			warnx("elf_rawdata failed: %s",
47292c23cb7cSEd Maste 			    elf_errmsg(elferr));
47302c23cb7cSEd Maste 		return;
47312c23cb7cSEd Maste 	}
47322c23cb7cSEd Maste 	if (d->d_size == 0)
47332c23cb7cSEd Maste 		return;
47342c23cb7cSEd Maste 
47352c23cb7cSEd Maste 	printf("\nSection %s contains:\n", s->name);
47362c23cb7cSEd Maste 	p = d->d_buf;
47372c23cb7cSEd Maste 	pe = p + d->d_size;
47382c23cb7cSEd Maste 	while (p < pe) {
4739b00fe64fSEd Maste 		if (pe - p < 8) {
4740b00fe64fSEd Maste 			warnx("Truncated MIPS option header");
4741b00fe64fSEd Maste 			return;
4742b00fe64fSEd Maste 		}
47432c23cb7cSEd Maste 		kind = re->dw_decode(&p, 1);
47442c23cb7cSEd Maste 		size = re->dw_decode(&p, 1);
47452c23cb7cSEd Maste 		sndx = re->dw_decode(&p, 2);
47462c23cb7cSEd Maste 		info = re->dw_decode(&p, 4);
4747b00fe64fSEd Maste 		if (size < 8 || size - 8 > pe - p) {
4748b00fe64fSEd Maste 			warnx("Malformed MIPS option header");
4749b00fe64fSEd Maste 			return;
4750b00fe64fSEd Maste 		}
4751b00fe64fSEd Maste 		size -= 8;
47522c23cb7cSEd Maste 		switch (kind) {
47532c23cb7cSEd Maste 		case ODK_REGINFO:
4754b00fe64fSEd Maste 			dump_mips_odk_reginfo(re, p, size);
47552c23cb7cSEd Maste 			break;
47562c23cb7cSEd Maste 		case ODK_EXCEPTIONS:
47572c23cb7cSEd Maste 			printf(" EXCEPTIONS FPU_MIN: %#x\n",
47582c23cb7cSEd Maste 			    info & OEX_FPU_MIN);
47592c23cb7cSEd Maste 			printf("%11.11s FPU_MAX: %#x\n", "",
47602c23cb7cSEd Maste 			    info & OEX_FPU_MAX);
47612c23cb7cSEd Maste 			dump_mips_option_flags("", mips_exceptions_option,
47622c23cb7cSEd Maste 			    info);
47632c23cb7cSEd Maste 			break;
47642c23cb7cSEd Maste 		case ODK_PAD:
47652c23cb7cSEd Maste 			printf(" %-10.10s section: %ju\n", "OPAD",
47662c23cb7cSEd Maste 			    (uintmax_t) sndx);
47672c23cb7cSEd Maste 			dump_mips_option_flags("", mips_pad_option, info);
47682c23cb7cSEd Maste 			break;
47692c23cb7cSEd Maste 		case ODK_HWPATCH:
47702c23cb7cSEd Maste 			dump_mips_option_flags("HWPATCH", mips_hwpatch_option,
47712c23cb7cSEd Maste 			    info);
47722c23cb7cSEd Maste 			break;
47732c23cb7cSEd Maste 		case ODK_HWAND:
47742c23cb7cSEd Maste 			dump_mips_option_flags("HWAND", mips_hwa_option, info);
47752c23cb7cSEd Maste 			break;
47762c23cb7cSEd Maste 		case ODK_HWOR:
47772c23cb7cSEd Maste 			dump_mips_option_flags("HWOR", mips_hwo_option, info);
47782c23cb7cSEd Maste 			break;
47792c23cb7cSEd Maste 		case ODK_FILL:
47802c23cb7cSEd Maste 			printf(" %-10.10s %#jx\n", "FILL", (uintmax_t) info);
47812c23cb7cSEd Maste 			break;
47822c23cb7cSEd Maste 		case ODK_TAGS:
47832c23cb7cSEd Maste 			printf(" %-10.10s\n", "TAGS");
47842c23cb7cSEd Maste 			break;
47852c23cb7cSEd Maste 		case ODK_GP_GROUP:
47862c23cb7cSEd Maste 			printf(" %-10.10s GP group number: %#x\n", "GP_GROUP",
47872c23cb7cSEd Maste 			    info & 0xFFFF);
47882c23cb7cSEd Maste 			if (info & 0x10000)
47892c23cb7cSEd Maste 				printf(" %-10.10s GP group is "
47902c23cb7cSEd Maste 				    "self-contained\n", "");
47912c23cb7cSEd Maste 			break;
47922c23cb7cSEd Maste 		case ODK_IDENT:
47932c23cb7cSEd Maste 			printf(" %-10.10s default GP group number: %#x\n",
47942c23cb7cSEd Maste 			    "IDENT", info & 0xFFFF);
47952c23cb7cSEd Maste 			if (info & 0x10000)
47962c23cb7cSEd Maste 				printf(" %-10.10s default GP group is "
47972c23cb7cSEd Maste 				    "self-contained\n", "");
47982c23cb7cSEd Maste 			break;
47992c23cb7cSEd Maste 		case ODK_PAGESIZE:
48002c23cb7cSEd Maste 			printf(" %-10.10s\n", "PAGESIZE");
48012c23cb7cSEd Maste 			break;
48022c23cb7cSEd Maste 		default:
48032c23cb7cSEd Maste 			break;
48042c23cb7cSEd Maste 		}
4805b00fe64fSEd Maste 		p += size;
48062c23cb7cSEd Maste 	}
48072c23cb7cSEd Maste }
48082c23cb7cSEd Maste 
48092c23cb7cSEd Maste static void
dump_mips_option_flags(const char * name,struct mips_option * opt,uint64_t info)48102c23cb7cSEd Maste dump_mips_option_flags(const char *name, struct mips_option *opt, uint64_t info)
48112c23cb7cSEd Maste {
48122c23cb7cSEd Maste 	int first;
48132c23cb7cSEd Maste 
48142c23cb7cSEd Maste 	first = 1;
48152c23cb7cSEd Maste 	for (; opt->desc != NULL; opt++) {
48162c23cb7cSEd Maste 		if (info & opt->flag) {
48172c23cb7cSEd Maste 			printf(" %-10.10s %s\n", first ? name : "",
48182c23cb7cSEd Maste 			    opt->desc);
48192c23cb7cSEd Maste 			first = 0;
48202c23cb7cSEd Maste 		}
48212c23cb7cSEd Maste 	}
48222c23cb7cSEd Maste }
48232c23cb7cSEd Maste 
48242c23cb7cSEd Maste static void
dump_mips_odk_reginfo(struct readelf * re,uint8_t * p,size_t sz)48252c23cb7cSEd Maste dump_mips_odk_reginfo(struct readelf *re, uint8_t *p, size_t sz)
48262c23cb7cSEd Maste {
48272c23cb7cSEd Maste 	uint32_t ri_gprmask;
48282c23cb7cSEd Maste 	uint32_t ri_cprmask[4];
48292c23cb7cSEd Maste 	uint64_t ri_gp_value;
48302c23cb7cSEd Maste 	uint8_t *pe;
48312c23cb7cSEd Maste 	int i;
48322c23cb7cSEd Maste 
48332c23cb7cSEd Maste 	pe = p + sz;
48342c23cb7cSEd Maste 	while (p < pe) {
48352c23cb7cSEd Maste 		ri_gprmask = re->dw_decode(&p, 4);
48362c23cb7cSEd Maste 		/* Skip ri_pad padding field for mips64. */
48372c23cb7cSEd Maste 		if (re->ec == ELFCLASS64)
48382c23cb7cSEd Maste 			re->dw_decode(&p, 4);
48392c23cb7cSEd Maste 		for (i = 0; i < 4; i++)
48402c23cb7cSEd Maste 			ri_cprmask[i] = re->dw_decode(&p, 4);
48412c23cb7cSEd Maste 		if (re->ec == ELFCLASS32)
48422c23cb7cSEd Maste 			ri_gp_value = re->dw_decode(&p, 4);
48432c23cb7cSEd Maste 		else
48442c23cb7cSEd Maste 			ri_gp_value = re->dw_decode(&p, 8);
48452c23cb7cSEd Maste 		printf(" %s    ", option_kind(ODK_REGINFO));
48462c23cb7cSEd Maste 		printf("ri_gprmask:    0x%08jx\n", (uintmax_t) ri_gprmask);
48472c23cb7cSEd Maste 		for (i = 0; i < 4; i++)
48482c23cb7cSEd Maste 			printf("%11.11s ri_cprmask[%d]: 0x%08jx\n", "", i,
48492c23cb7cSEd Maste 			    (uintmax_t) ri_cprmask[i]);
48502c23cb7cSEd Maste 		printf("%12.12s", "");
48512c23cb7cSEd Maste 		printf("ri_gp_value:   %#jx\n", (uintmax_t) ri_gp_value);
48522c23cb7cSEd Maste 	}
48532c23cb7cSEd Maste }
48542c23cb7cSEd Maste 
48552c23cb7cSEd Maste static void
dump_arch_specific_info(struct readelf * re)48562c23cb7cSEd Maste dump_arch_specific_info(struct readelf *re)
48572c23cb7cSEd Maste {
48582c23cb7cSEd Maste 
48592c23cb7cSEd Maste 	dump_liblist(re);
48602c23cb7cSEd Maste 	dump_attributes(re);
48612c23cb7cSEd Maste 
48622c23cb7cSEd Maste 	switch (re->ehdr.e_machine) {
48632c23cb7cSEd Maste 	case EM_MIPS:
48642c23cb7cSEd Maste 	case EM_MIPS_RS3_LE:
48652c23cb7cSEd Maste 		dump_mips_specific_info(re);
48662c23cb7cSEd Maste 	default:
48672c23cb7cSEd Maste 		break;
48682c23cb7cSEd Maste 	}
48692c23cb7cSEd Maste }
48702c23cb7cSEd Maste 
4871cf781b2eSEd Maste static const char *
dwarf_regname(struct readelf * re,unsigned int num)4872cf781b2eSEd Maste dwarf_regname(struct readelf *re, unsigned int num)
4873cf781b2eSEd Maste {
4874cf781b2eSEd Maste 	static char rx[32];
4875cf781b2eSEd Maste 	const char *rn;
4876cf781b2eSEd Maste 
4877cf781b2eSEd Maste 	if ((rn = dwarf_reg(re->ehdr.e_machine, num)) != NULL)
4878cf781b2eSEd Maste 		return (rn);
4879cf781b2eSEd Maste 
4880cf781b2eSEd Maste 	snprintf(rx, sizeof(rx), "r%u", num);
4881cf781b2eSEd Maste 
4882cf781b2eSEd Maste 	return (rx);
4883cf781b2eSEd Maste }
4884cf781b2eSEd Maste 
48852c23cb7cSEd Maste static void
dump_dwarf_line(struct readelf * re)48862c23cb7cSEd Maste dump_dwarf_line(struct readelf *re)
48872c23cb7cSEd Maste {
48882c23cb7cSEd Maste 	struct section *s;
48892c23cb7cSEd Maste 	Dwarf_Die die;
48902c23cb7cSEd Maste 	Dwarf_Error de;
48912c23cb7cSEd Maste 	Dwarf_Half tag, version, pointer_size;
48922c23cb7cSEd Maste 	Dwarf_Unsigned offset, endoff, length, hdrlen, dirndx, mtime, fsize;
48932c23cb7cSEd Maste 	Dwarf_Small minlen, defstmt, lrange, opbase, oplen;
48942c23cb7cSEd Maste 	Elf_Data *d;
48952c23cb7cSEd Maste 	char *pn;
48962c23cb7cSEd Maste 	uint64_t address, file, line, column, isa, opsize, udelta;
48972c23cb7cSEd Maste 	int64_t sdelta;
48982c23cb7cSEd Maste 	uint8_t *p, *pe;
48992c23cb7cSEd Maste 	int8_t lbase;
4900cf781b2eSEd Maste 	int i, is_stmt, dwarf_size, elferr, ret;
49012c23cb7cSEd Maste 
49022c23cb7cSEd Maste 	printf("\nDump of debug contents of section .debug_line:\n");
49032c23cb7cSEd Maste 
49042c23cb7cSEd Maste 	s = NULL;
49052c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
49062c23cb7cSEd Maste 		s = &re->sl[i];
49072c23cb7cSEd Maste 		if (s->name != NULL && !strcmp(s->name, ".debug_line"))
49082c23cb7cSEd Maste 			break;
49092c23cb7cSEd Maste 	}
49102c23cb7cSEd Maste 	if ((size_t) i >= re->shnum)
49112c23cb7cSEd Maste 		return;
49122c23cb7cSEd Maste 
49132c23cb7cSEd Maste 	(void) elf_errno();
49142c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
49152c23cb7cSEd Maste 		elferr = elf_errno();
49162c23cb7cSEd Maste 		if (elferr != 0)
49172c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
49182c23cb7cSEd Maste 		return;
49192c23cb7cSEd Maste 	}
49202c23cb7cSEd Maste 	if (d->d_size <= 0)
49212c23cb7cSEd Maste 		return;
49222c23cb7cSEd Maste 
49232c23cb7cSEd Maste 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
49242c23cb7cSEd Maste 	    NULL, &de)) ==  DW_DLV_OK) {
49252c23cb7cSEd Maste 		die = NULL;
49262c23cb7cSEd Maste 		while (dwarf_siblingof(re->dbg, die, &die, &de) == DW_DLV_OK) {
49272c23cb7cSEd Maste 			if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
49282c23cb7cSEd Maste 				warnx("dwarf_tag failed: %s",
49292c23cb7cSEd Maste 				    dwarf_errmsg(de));
49302c23cb7cSEd Maste 				return;
49312c23cb7cSEd Maste 			}
49322c23cb7cSEd Maste 			/* XXX: What about DW_TAG_partial_unit? */
49332c23cb7cSEd Maste 			if (tag == DW_TAG_compile_unit)
49342c23cb7cSEd Maste 				break;
49352c23cb7cSEd Maste 		}
49362c23cb7cSEd Maste 		if (die == NULL) {
49372c23cb7cSEd Maste 			warnx("could not find DW_TAG_compile_unit die");
49382c23cb7cSEd Maste 			return;
49392c23cb7cSEd Maste 		}
49402c23cb7cSEd Maste 		if (dwarf_attrval_unsigned(die, DW_AT_stmt_list, &offset,
4941cec2d0b1SChristos Margiolis 		    &de) != DW_DLV_OK) {
4942cec2d0b1SChristos Margiolis 			dwarf_dealloc(re->dbg, die, DW_DLA_DIE);
49432c23cb7cSEd Maste 			continue;
4944cec2d0b1SChristos Margiolis 		}
49452c23cb7cSEd Maste 
49462c23cb7cSEd Maste 		length = re->dw_read(d, &offset, 4);
49472c23cb7cSEd Maste 		if (length == 0xffffffff) {
49482c23cb7cSEd Maste 			dwarf_size = 8;
49492c23cb7cSEd Maste 			length = re->dw_read(d, &offset, 8);
49502c23cb7cSEd Maste 		} else
49512c23cb7cSEd Maste 			dwarf_size = 4;
49522c23cb7cSEd Maste 
49532c23cb7cSEd Maste 		if (length > d->d_size - offset) {
49542c23cb7cSEd Maste 			warnx("invalid .dwarf_line section");
4955cec2d0b1SChristos Margiolis 			dwarf_dealloc(re->dbg, die, DW_DLA_DIE);
49562c23cb7cSEd Maste 			continue;
49572c23cb7cSEd Maste 		}
49582c23cb7cSEd Maste 
49592c23cb7cSEd Maste 		endoff = offset + length;
49608f32e46dSKai Wang 		pe = (uint8_t *) d->d_buf + endoff;
49612c23cb7cSEd Maste 		version = re->dw_read(d, &offset, 2);
49622c23cb7cSEd Maste 		hdrlen = re->dw_read(d, &offset, dwarf_size);
49632c23cb7cSEd Maste 		minlen = re->dw_read(d, &offset, 1);
49642c23cb7cSEd Maste 		defstmt = re->dw_read(d, &offset, 1);
49652c23cb7cSEd Maste 		lbase = re->dw_read(d, &offset, 1);
49662c23cb7cSEd Maste 		lrange = re->dw_read(d, &offset, 1);
49672c23cb7cSEd Maste 		opbase = re->dw_read(d, &offset, 1);
49682c23cb7cSEd Maste 
49692c23cb7cSEd Maste 		printf("\n");
49702c23cb7cSEd Maste 		printf("  Length:\t\t\t%ju\n", (uintmax_t) length);
49712c23cb7cSEd Maste 		printf("  DWARF version:\t\t%u\n", version);
49722c23cb7cSEd Maste 		printf("  Prologue Length:\t\t%ju\n", (uintmax_t) hdrlen);
49732c23cb7cSEd Maste 		printf("  Minimum Instruction Length:\t%u\n", minlen);
49742c23cb7cSEd Maste 		printf("  Initial value of 'is_stmt':\t%u\n", defstmt);
49752c23cb7cSEd Maste 		printf("  Line Base:\t\t\t%d\n", lbase);
49762c23cb7cSEd Maste 		printf("  Line Range:\t\t\t%u\n", lrange);
49772c23cb7cSEd Maste 		printf("  Opcode Base:\t\t\t%u\n", opbase);
49782c23cb7cSEd Maste 		(void) dwarf_get_address_size(re->dbg, &pointer_size, &de);
49792c23cb7cSEd Maste 		printf("  (Pointer size:\t\t%u)\n", pointer_size);
49802c23cb7cSEd Maste 
49812c23cb7cSEd Maste 		printf("\n");
49822c23cb7cSEd Maste 		printf(" Opcodes:\n");
49832c23cb7cSEd Maste 		for (i = 1; i < opbase; i++) {
49842c23cb7cSEd Maste 			oplen = re->dw_read(d, &offset, 1);
49852c23cb7cSEd Maste 			printf("  Opcode %d has %u args\n", i, oplen);
49862c23cb7cSEd Maste 		}
49872c23cb7cSEd Maste 
49882c23cb7cSEd Maste 		printf("\n");
49892c23cb7cSEd Maste 		printf(" The Directory Table:\n");
49902c23cb7cSEd Maste 		p = (uint8_t *) d->d_buf + offset;
49912c23cb7cSEd Maste 		while (*p != '\0') {
49922c23cb7cSEd Maste 			printf("  %s\n", (char *) p);
49932c23cb7cSEd Maste 			p += strlen((char *) p) + 1;
49942c23cb7cSEd Maste 		}
49952c23cb7cSEd Maste 
49962c23cb7cSEd Maste 		p++;
49972c23cb7cSEd Maste 		printf("\n");
49982c23cb7cSEd Maste 		printf(" The File Name Table:\n");
49992c23cb7cSEd Maste 		printf("  Entry\tDir\tTime\tSize\tName\n");
50002c23cb7cSEd Maste 		i = 0;
50012c23cb7cSEd Maste 		while (*p != '\0') {
50022c23cb7cSEd Maste 			i++;
50032c23cb7cSEd Maste 			pn = (char *) p;
50042c23cb7cSEd Maste 			p += strlen(pn) + 1;
500595fd7f26SEd Maste 			dirndx = _decode_uleb128(&p, pe);
500695fd7f26SEd Maste 			mtime = _decode_uleb128(&p, pe);
500795fd7f26SEd Maste 			fsize = _decode_uleb128(&p, pe);
50082c23cb7cSEd Maste 			printf("  %d\t%ju\t%ju\t%ju\t%s\n", i,
50092c23cb7cSEd Maste 			    (uintmax_t) dirndx, (uintmax_t) mtime,
50102c23cb7cSEd Maste 			    (uintmax_t) fsize, pn);
50112c23cb7cSEd Maste 		}
50122c23cb7cSEd Maste 
50132c23cb7cSEd Maste #define	RESET_REGISTERS						\
50142c23cb7cSEd Maste 	do {							\
50152c23cb7cSEd Maste 		address	       = 0;				\
50162c23cb7cSEd Maste 		file	       = 1;				\
50172c23cb7cSEd Maste 		line	       = 1;				\
50182c23cb7cSEd Maste 		column	       = 0;				\
50192c23cb7cSEd Maste 		is_stmt	       = defstmt;			\
50202c23cb7cSEd Maste 	} while(0)
50212c23cb7cSEd Maste 
50222c23cb7cSEd Maste #define	LINE(x) (lbase + (((x) - opbase) % lrange))
50232c23cb7cSEd Maste #define	ADDRESS(x) ((((x) - opbase) / lrange) * minlen)
50242c23cb7cSEd Maste 
50252c23cb7cSEd Maste 		p++;
50262c23cb7cSEd Maste 		printf("\n");
50272c23cb7cSEd Maste 		printf(" Line Number Statements:\n");
50282c23cb7cSEd Maste 
50292c23cb7cSEd Maste 		RESET_REGISTERS;
50302c23cb7cSEd Maste 
50312c23cb7cSEd Maste 		while (p < pe) {
50322c23cb7cSEd Maste 
50332c23cb7cSEd Maste 			if (*p == 0) {
50342c23cb7cSEd Maste 				/*
50352c23cb7cSEd Maste 				 * Extended Opcodes.
50362c23cb7cSEd Maste 				 */
50372c23cb7cSEd Maste 				p++;
503895fd7f26SEd Maste 				opsize = _decode_uleb128(&p, pe);
50392c23cb7cSEd Maste 				printf("  Extended opcode %u: ", *p);
50402c23cb7cSEd Maste 				switch (*p) {
50412c23cb7cSEd Maste 				case DW_LNE_end_sequence:
50422c23cb7cSEd Maste 					p++;
50432c23cb7cSEd Maste 					RESET_REGISTERS;
50442c23cb7cSEd Maste 					printf("End of Sequence\n");
50452c23cb7cSEd Maste 					break;
50462c23cb7cSEd Maste 				case DW_LNE_set_address:
50472c23cb7cSEd Maste 					p++;
50482c23cb7cSEd Maste 					address = re->dw_decode(&p,
50492c23cb7cSEd Maste 					    pointer_size);
50502c23cb7cSEd Maste 					printf("set Address to %#jx\n",
50512c23cb7cSEd Maste 					    (uintmax_t) address);
50522c23cb7cSEd Maste 					break;
50532c23cb7cSEd Maste 				case DW_LNE_define_file:
50542c23cb7cSEd Maste 					p++;
50552c23cb7cSEd Maste 					pn = (char *) p;
50562c23cb7cSEd Maste 					p += strlen(pn) + 1;
505795fd7f26SEd Maste 					dirndx = _decode_uleb128(&p, pe);
505895fd7f26SEd Maste 					mtime = _decode_uleb128(&p, pe);
505995fd7f26SEd Maste 					fsize = _decode_uleb128(&p, pe);
50602c23cb7cSEd Maste 					printf("define new file: %s\n", pn);
50612c23cb7cSEd Maste 					break;
50622c23cb7cSEd Maste 				default:
50632c23cb7cSEd Maste 					/* Unrecognized extened opcodes. */
50642c23cb7cSEd Maste 					p += opsize;
50652c23cb7cSEd Maste 					printf("unknown opcode\n");
50662c23cb7cSEd Maste 				}
50672c23cb7cSEd Maste 			} else if (*p > 0 && *p < opbase) {
50682c23cb7cSEd Maste 				/*
50692c23cb7cSEd Maste 				 * Standard Opcodes.
50702c23cb7cSEd Maste 				 */
50712c23cb7cSEd Maste 				switch(*p++) {
50722c23cb7cSEd Maste 				case DW_LNS_copy:
50732c23cb7cSEd Maste 					printf("  Copy\n");
50742c23cb7cSEd Maste 					break;
50752c23cb7cSEd Maste 				case DW_LNS_advance_pc:
507695fd7f26SEd Maste 					udelta = _decode_uleb128(&p, pe) *
50772c23cb7cSEd Maste 					    minlen;
50782c23cb7cSEd Maste 					address += udelta;
50792c23cb7cSEd Maste 					printf("  Advance PC by %ju to %#jx\n",
50802c23cb7cSEd Maste 					    (uintmax_t) udelta,
50812c23cb7cSEd Maste 					    (uintmax_t) address);
50822c23cb7cSEd Maste 					break;
50832c23cb7cSEd Maste 				case DW_LNS_advance_line:
508495fd7f26SEd Maste 					sdelta = _decode_sleb128(&p, pe);
50852c23cb7cSEd Maste 					line += sdelta;
50862c23cb7cSEd Maste 					printf("  Advance Line by %jd to %ju\n",
50872c23cb7cSEd Maste 					    (intmax_t) sdelta,
50882c23cb7cSEd Maste 					    (uintmax_t) line);
50892c23cb7cSEd Maste 					break;
50902c23cb7cSEd Maste 				case DW_LNS_set_file:
509195fd7f26SEd Maste 					file = _decode_uleb128(&p, pe);
50922c23cb7cSEd Maste 					printf("  Set File to %ju\n",
50932c23cb7cSEd Maste 					    (uintmax_t) file);
50942c23cb7cSEd Maste 					break;
50952c23cb7cSEd Maste 				case DW_LNS_set_column:
509695fd7f26SEd Maste 					column = _decode_uleb128(&p, pe);
50972c23cb7cSEd Maste 					printf("  Set Column to %ju\n",
50982c23cb7cSEd Maste 					    (uintmax_t) column);
50992c23cb7cSEd Maste 					break;
51002c23cb7cSEd Maste 				case DW_LNS_negate_stmt:
51012c23cb7cSEd Maste 					is_stmt = !is_stmt;
51022c23cb7cSEd Maste 					printf("  Set is_stmt to %d\n", is_stmt);
51032c23cb7cSEd Maste 					break;
51042c23cb7cSEd Maste 				case DW_LNS_set_basic_block:
51052c23cb7cSEd Maste 					printf("  Set basic block flag\n");
51062c23cb7cSEd Maste 					break;
51072c23cb7cSEd Maste 				case DW_LNS_const_add_pc:
51082c23cb7cSEd Maste 					address += ADDRESS(255);
51092c23cb7cSEd Maste 					printf("  Advance PC by constant %ju"
51102c23cb7cSEd Maste 					    " to %#jx\n",
51112c23cb7cSEd Maste 					    (uintmax_t) ADDRESS(255),
51122c23cb7cSEd Maste 					    (uintmax_t) address);
51132c23cb7cSEd Maste 					break;
51142c23cb7cSEd Maste 				case DW_LNS_fixed_advance_pc:
51152c23cb7cSEd Maste 					udelta = re->dw_decode(&p, 2);
51162c23cb7cSEd Maste 					address += udelta;
51172c23cb7cSEd Maste 					printf("  Advance PC by fixed value "
51182c23cb7cSEd Maste 					    "%ju to %#jx\n",
51192c23cb7cSEd Maste 					    (uintmax_t) udelta,
51202c23cb7cSEd Maste 					    (uintmax_t) address);
51212c23cb7cSEd Maste 					break;
51222c23cb7cSEd Maste 				case DW_LNS_set_prologue_end:
51232c23cb7cSEd Maste 					printf("  Set prologue end flag\n");
51242c23cb7cSEd Maste 					break;
51252c23cb7cSEd Maste 				case DW_LNS_set_epilogue_begin:
51262c23cb7cSEd Maste 					printf("  Set epilogue begin flag\n");
51272c23cb7cSEd Maste 					break;
51282c23cb7cSEd Maste 				case DW_LNS_set_isa:
512995fd7f26SEd Maste 					isa = _decode_uleb128(&p, pe);
5130839529caSEd Maste 					printf("  Set isa to %ju\n",
5131839529caSEd Maste 					    (uintmax_t) isa);
51322c23cb7cSEd Maste 					break;
51332c23cb7cSEd Maste 				default:
51342c23cb7cSEd Maste 					/* Unrecognized extended opcodes. */
51352c23cb7cSEd Maste 					printf("  Unknown extended opcode %u\n",
51362c23cb7cSEd Maste 					    *(p - 1));
51372c23cb7cSEd Maste 					break;
51382c23cb7cSEd Maste 				}
51392c23cb7cSEd Maste 
51402c23cb7cSEd Maste 			} else {
51412c23cb7cSEd Maste 				/*
51422c23cb7cSEd Maste 				 * Special Opcodes.
51432c23cb7cSEd Maste 				 */
51442c23cb7cSEd Maste 				line += LINE(*p);
51452c23cb7cSEd Maste 				address += ADDRESS(*p);
51462c23cb7cSEd Maste 				printf("  Special opcode %u: advance Address "
51472c23cb7cSEd Maste 				    "by %ju to %#jx and Line by %jd to %ju\n",
51482c23cb7cSEd Maste 				    *p - opbase, (uintmax_t) ADDRESS(*p),
51492c23cb7cSEd Maste 				    (uintmax_t) address, (intmax_t) LINE(*p),
51502c23cb7cSEd Maste 				    (uintmax_t) line);
51512c23cb7cSEd Maste 				p++;
51522c23cb7cSEd Maste 			}
51532c23cb7cSEd Maste 		}
5154cec2d0b1SChristos Margiolis 		dwarf_dealloc(re->dbg, die, DW_DLA_DIE);
51552c23cb7cSEd Maste 	}
51562c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
51572c23cb7cSEd Maste 		warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
51582c23cb7cSEd Maste 
51592c23cb7cSEd Maste #undef	RESET_REGISTERS
51602c23cb7cSEd Maste #undef	LINE
51612c23cb7cSEd Maste #undef	ADDRESS
51622c23cb7cSEd Maste }
51632c23cb7cSEd Maste 
51642c23cb7cSEd Maste static void
dump_dwarf_line_decoded(struct readelf * re)51652c23cb7cSEd Maste dump_dwarf_line_decoded(struct readelf *re)
51662c23cb7cSEd Maste {
51672c23cb7cSEd Maste 	Dwarf_Die die;
51682c23cb7cSEd Maste 	Dwarf_Line *linebuf, ln;
51692c23cb7cSEd Maste 	Dwarf_Addr lineaddr;
51702c23cb7cSEd Maste 	Dwarf_Signed linecount, srccount;
51712c23cb7cSEd Maste 	Dwarf_Unsigned lineno, fn;
51722c23cb7cSEd Maste 	Dwarf_Error de;
51732c23cb7cSEd Maste 	const char *dir, *file;
51742c23cb7cSEd Maste 	char **srcfiles;
51752c23cb7cSEd Maste 	int i, ret;
51762c23cb7cSEd Maste 
51772c23cb7cSEd Maste 	printf("Decoded dump of debug contents of section .debug_line:\n\n");
51782c23cb7cSEd Maste 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
51792c23cb7cSEd Maste 	    NULL, &de)) == DW_DLV_OK) {
51802c23cb7cSEd Maste 		if (dwarf_siblingof(re->dbg, NULL, &die, &de) != DW_DLV_OK)
51812c23cb7cSEd Maste 			continue;
51822c23cb7cSEd Maste 		if (dwarf_attrval_string(die, DW_AT_name, &file, &de) !=
51832c23cb7cSEd Maste 		    DW_DLV_OK)
51842c23cb7cSEd Maste 			file = NULL;
51852c23cb7cSEd Maste 		if (dwarf_attrval_string(die, DW_AT_comp_dir, &dir, &de) !=
51862c23cb7cSEd Maste 		    DW_DLV_OK)
51872c23cb7cSEd Maste 			dir = NULL;
51882c23cb7cSEd Maste 		printf("CU: ");
518965caaa0eSMaxim Sobolev 		if (dir && file && file[0] != '/')
51902c23cb7cSEd Maste 			printf("%s/", dir);
51912c23cb7cSEd Maste 		if (file)
51922c23cb7cSEd Maste 			printf("%s", file);
51932c23cb7cSEd Maste 		putchar('\n');
51942c23cb7cSEd Maste 		printf("%-37s %11s   %s\n", "Filename", "Line Number",
51952c23cb7cSEd Maste 		    "Starting Address");
51962c23cb7cSEd Maste 		if (dwarf_srclines(die, &linebuf, &linecount, &de) != DW_DLV_OK)
5197cec2d0b1SChristos Margiolis 			goto done;
51982c23cb7cSEd Maste 		if (dwarf_srcfiles(die, &srcfiles, &srccount, &de) != DW_DLV_OK)
5199cec2d0b1SChristos Margiolis 			goto done;
52002c23cb7cSEd Maste 		for (i = 0; i < linecount; i++) {
52012c23cb7cSEd Maste 			ln = linebuf[i];
52022c23cb7cSEd Maste 			if (dwarf_line_srcfileno(ln, &fn, &de) != DW_DLV_OK)
52032c23cb7cSEd Maste 				continue;
52042c23cb7cSEd Maste 			if (dwarf_lineno(ln, &lineno, &de) != DW_DLV_OK)
52052c23cb7cSEd Maste 				continue;
52062c23cb7cSEd Maste 			if (dwarf_lineaddr(ln, &lineaddr, &de) != DW_DLV_OK)
52072c23cb7cSEd Maste 				continue;
52082c23cb7cSEd Maste 			printf("%-37s %11ju %#18jx\n",
52092c23cb7cSEd Maste 			    basename(srcfiles[fn - 1]), (uintmax_t) lineno,
52102c23cb7cSEd Maste 			    (uintmax_t) lineaddr);
52112c23cb7cSEd Maste 		}
52122c23cb7cSEd Maste 		putchar('\n');
5213cec2d0b1SChristos Margiolis done:
5214cec2d0b1SChristos Margiolis 		dwarf_dealloc(re->dbg, die, DW_DLA_DIE);
52152c23cb7cSEd Maste 	}
52162c23cb7cSEd Maste }
52172c23cb7cSEd Maste 
52182c23cb7cSEd Maste static void
dump_dwarf_die(struct readelf * re,Dwarf_Die die,int level)52192c23cb7cSEd Maste dump_dwarf_die(struct readelf *re, Dwarf_Die die, int level)
52202c23cb7cSEd Maste {
52212c23cb7cSEd Maste 	Dwarf_Attribute *attr_list;
52222c23cb7cSEd Maste 	Dwarf_Die ret_die;
5223cf781b2eSEd Maste 	Dwarf_Off dieoff, cuoff, culen, attroff;
5224cf781b2eSEd Maste 	Dwarf_Unsigned ate, lang, v_udata, v_sig;
52252c23cb7cSEd Maste 	Dwarf_Signed attr_count, v_sdata;
52262c23cb7cSEd Maste 	Dwarf_Off v_off;
52272c23cb7cSEd Maste 	Dwarf_Addr v_addr;
52282c23cb7cSEd Maste 	Dwarf_Half tag, attr, form;
52292c23cb7cSEd Maste 	Dwarf_Block *v_block;
5230cf781b2eSEd Maste 	Dwarf_Bool v_bool, is_info;
5231cf781b2eSEd Maste 	Dwarf_Sig8 v_sig8;
52322c23cb7cSEd Maste 	Dwarf_Error de;
5233cf781b2eSEd Maste 	Dwarf_Ptr v_expr;
5234cf781b2eSEd Maste 	const char *tag_str, *attr_str, *ate_str, *lang_str;
5235cf781b2eSEd Maste 	char unk_tag[32], unk_attr[32];
52362c23cb7cSEd Maste 	char *v_str;
5237cf781b2eSEd Maste 	uint8_t *b, *p;
52382c23cb7cSEd Maste 	int i, j, abc, ret;
52392c23cb7cSEd Maste 
52402c23cb7cSEd Maste 	if (dwarf_dieoffset(die, &dieoff, &de) != DW_DLV_OK) {
52412c23cb7cSEd Maste 		warnx("dwarf_dieoffset failed: %s", dwarf_errmsg(de));
52422c23cb7cSEd Maste 		goto cont_search;
52432c23cb7cSEd Maste 	}
52442c23cb7cSEd Maste 
52452c23cb7cSEd Maste 	printf(" <%d><%jx>: ", level, (uintmax_t) dieoff);
52462c23cb7cSEd Maste 
52472c23cb7cSEd Maste 	if (dwarf_die_CU_offset_range(die, &cuoff, &culen, &de) != DW_DLV_OK) {
52482c23cb7cSEd Maste 		warnx("dwarf_die_CU_offset_range failed: %s",
52492c23cb7cSEd Maste 		      dwarf_errmsg(de));
52502c23cb7cSEd Maste 		cuoff = 0;
52512c23cb7cSEd Maste 	}
52522c23cb7cSEd Maste 
52532c23cb7cSEd Maste 	abc = dwarf_die_abbrev_code(die);
52542c23cb7cSEd Maste 	if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
52552c23cb7cSEd Maste 		warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
52562c23cb7cSEd Maste 		goto cont_search;
52572c23cb7cSEd Maste 	}
52582c23cb7cSEd Maste 	if (dwarf_get_TAG_name(tag, &tag_str) != DW_DLV_OK) {
5259cf781b2eSEd Maste 		snprintf(unk_tag, sizeof(unk_tag), "[Unknown Tag: %#x]", tag);
5260cf781b2eSEd Maste 		tag_str = unk_tag;
52612c23cb7cSEd Maste 	}
52622c23cb7cSEd Maste 
52632c23cb7cSEd Maste 	printf("Abbrev Number: %d (%s)\n", abc, tag_str);
52642c23cb7cSEd Maste 
52652c23cb7cSEd Maste 	if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
52662c23cb7cSEd Maste 	    DW_DLV_OK) {
52672c23cb7cSEd Maste 		if (ret == DW_DLV_ERROR)
52682c23cb7cSEd Maste 			warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
52692c23cb7cSEd Maste 		goto cont_search;
52702c23cb7cSEd Maste 	}
52712c23cb7cSEd Maste 
52722c23cb7cSEd Maste 	for (i = 0; i < attr_count; i++) {
52732c23cb7cSEd Maste 		if (dwarf_whatform(attr_list[i], &form, &de) != DW_DLV_OK) {
52742c23cb7cSEd Maste 			warnx("dwarf_whatform failed: %s", dwarf_errmsg(de));
52752c23cb7cSEd Maste 			continue;
52762c23cb7cSEd Maste 		}
52772c23cb7cSEd Maste 		if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
52782c23cb7cSEd Maste 			warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
52792c23cb7cSEd Maste 			continue;
52802c23cb7cSEd Maste 		}
52812c23cb7cSEd Maste 		if (dwarf_get_AT_name(attr, &attr_str) != DW_DLV_OK) {
5282cf781b2eSEd Maste 			snprintf(unk_attr, sizeof(unk_attr),
5283cf781b2eSEd Maste 			    "[Unknown AT: %#x]", attr);
5284cf781b2eSEd Maste 			attr_str = unk_attr;
52852c23cb7cSEd Maste 		}
5286cf781b2eSEd Maste 		if (dwarf_attroffset(attr_list[i], &attroff, &de) !=
5287cf781b2eSEd Maste 		    DW_DLV_OK) {
5288cf781b2eSEd Maste 			warnx("dwarf_attroffset failed: %s", dwarf_errmsg(de));
5289cf781b2eSEd Maste 			attroff = 0;
5290cf781b2eSEd Maste 		}
5291cf781b2eSEd Maste 		printf("    <%jx>   %-18s: ", (uintmax_t) attroff, attr_str);
52922c23cb7cSEd Maste 		switch (form) {
52932c23cb7cSEd Maste 		case DW_FORM_ref_addr:
5294cf781b2eSEd Maste 		case DW_FORM_sec_offset:
52952c23cb7cSEd Maste 			if (dwarf_global_formref(attr_list[i], &v_off, &de) !=
52962c23cb7cSEd Maste 			    DW_DLV_OK) {
52972c23cb7cSEd Maste 				warnx("dwarf_global_formref failed: %s",
52982c23cb7cSEd Maste 				    dwarf_errmsg(de));
52992c23cb7cSEd Maste 				continue;
53002c23cb7cSEd Maste 			}
5301cf781b2eSEd Maste 			if (form == DW_FORM_ref_addr)
5302cf781b2eSEd Maste 				printf("<0x%jx>", (uintmax_t) v_off);
5303cf781b2eSEd Maste 			else
5304cf781b2eSEd Maste 				printf("0x%jx", (uintmax_t) v_off);
53052c23cb7cSEd Maste 			break;
53062c23cb7cSEd Maste 
53072c23cb7cSEd Maste 		case DW_FORM_ref1:
53082c23cb7cSEd Maste 		case DW_FORM_ref2:
53092c23cb7cSEd Maste 		case DW_FORM_ref4:
53102c23cb7cSEd Maste 		case DW_FORM_ref8:
53112c23cb7cSEd Maste 		case DW_FORM_ref_udata:
53122c23cb7cSEd Maste 			if (dwarf_formref(attr_list[i], &v_off, &de) !=
53132c23cb7cSEd Maste 			    DW_DLV_OK) {
53142c23cb7cSEd Maste 				warnx("dwarf_formref failed: %s",
53152c23cb7cSEd Maste 				    dwarf_errmsg(de));
53162c23cb7cSEd Maste 				continue;
53172c23cb7cSEd Maste 			}
53182c23cb7cSEd Maste 			v_off += cuoff;
5319cf781b2eSEd Maste 			printf("<0x%jx>", (uintmax_t) v_off);
53202c23cb7cSEd Maste 			break;
53212c23cb7cSEd Maste 
53222c23cb7cSEd Maste 		case DW_FORM_addr:
53232c23cb7cSEd Maste 			if (dwarf_formaddr(attr_list[i], &v_addr, &de) !=
53242c23cb7cSEd Maste 			    DW_DLV_OK) {
53252c23cb7cSEd Maste 				warnx("dwarf_formaddr failed: %s",
53262c23cb7cSEd Maste 				    dwarf_errmsg(de));
53272c23cb7cSEd Maste 				continue;
53282c23cb7cSEd Maste 			}
53292c23cb7cSEd Maste 			printf("%#jx", (uintmax_t) v_addr);
53302c23cb7cSEd Maste 			break;
53312c23cb7cSEd Maste 
53322c23cb7cSEd Maste 		case DW_FORM_data1:
53332c23cb7cSEd Maste 		case DW_FORM_data2:
53342c23cb7cSEd Maste 		case DW_FORM_data4:
53352c23cb7cSEd Maste 		case DW_FORM_data8:
53362c23cb7cSEd Maste 		case DW_FORM_udata:
53372c23cb7cSEd Maste 			if (dwarf_formudata(attr_list[i], &v_udata, &de) !=
53382c23cb7cSEd Maste 			    DW_DLV_OK) {
53392c23cb7cSEd Maste 				warnx("dwarf_formudata failed: %s",
53402c23cb7cSEd Maste 				    dwarf_errmsg(de));
53412c23cb7cSEd Maste 				continue;
53422c23cb7cSEd Maste 			}
5343cf781b2eSEd Maste 			if (attr == DW_AT_high_pc)
5344cf781b2eSEd Maste 				printf("0x%jx", (uintmax_t) v_udata);
5345cf781b2eSEd Maste 			else
53462c23cb7cSEd Maste 				printf("%ju", (uintmax_t) v_udata);
53472c23cb7cSEd Maste 			break;
53482c23cb7cSEd Maste 
53492c23cb7cSEd Maste 		case DW_FORM_sdata:
53502c23cb7cSEd Maste 			if (dwarf_formsdata(attr_list[i], &v_sdata, &de) !=
53512c23cb7cSEd Maste 			    DW_DLV_OK) {
53522c23cb7cSEd Maste 				warnx("dwarf_formudata failed: %s",
53532c23cb7cSEd Maste 				    dwarf_errmsg(de));
53542c23cb7cSEd Maste 				continue;
53552c23cb7cSEd Maste 			}
53562c23cb7cSEd Maste 			printf("%jd", (intmax_t) v_sdata);
53572c23cb7cSEd Maste 			break;
53582c23cb7cSEd Maste 
53592c23cb7cSEd Maste 		case DW_FORM_flag:
53602c23cb7cSEd Maste 			if (dwarf_formflag(attr_list[i], &v_bool, &de) !=
53612c23cb7cSEd Maste 			    DW_DLV_OK) {
53622c23cb7cSEd Maste 				warnx("dwarf_formflag failed: %s",
53632c23cb7cSEd Maste 				    dwarf_errmsg(de));
53642c23cb7cSEd Maste 				continue;
53652c23cb7cSEd Maste 			}
53662c23cb7cSEd Maste 			printf("%jd", (intmax_t) v_bool);
53672c23cb7cSEd Maste 			break;
53682c23cb7cSEd Maste 
5369cf781b2eSEd Maste 		case DW_FORM_flag_present:
5370cf781b2eSEd Maste 			putchar('1');
5371cf781b2eSEd Maste 			break;
5372cf781b2eSEd Maste 
53732c23cb7cSEd Maste 		case DW_FORM_string:
53742c23cb7cSEd Maste 		case DW_FORM_strp:
53752c23cb7cSEd Maste 			if (dwarf_formstring(attr_list[i], &v_str, &de) !=
53762c23cb7cSEd Maste 			    DW_DLV_OK) {
53772c23cb7cSEd Maste 				warnx("dwarf_formstring failed: %s",
53782c23cb7cSEd Maste 				    dwarf_errmsg(de));
53792c23cb7cSEd Maste 				continue;
53802c23cb7cSEd Maste 			}
53812c23cb7cSEd Maste 			if (form == DW_FORM_string)
53822c23cb7cSEd Maste 				printf("%s", v_str);
53832c23cb7cSEd Maste 			else
53842c23cb7cSEd Maste 				printf("(indirect string) %s", v_str);
53852c23cb7cSEd Maste 			break;
53862c23cb7cSEd Maste 
53872c23cb7cSEd Maste 		case DW_FORM_block:
53882c23cb7cSEd Maste 		case DW_FORM_block1:
53892c23cb7cSEd Maste 		case DW_FORM_block2:
53902c23cb7cSEd Maste 		case DW_FORM_block4:
53912c23cb7cSEd Maste 			if (dwarf_formblock(attr_list[i], &v_block, &de) !=
53922c23cb7cSEd Maste 			    DW_DLV_OK) {
53932c23cb7cSEd Maste 				warnx("dwarf_formblock failed: %s",
53942c23cb7cSEd Maste 				    dwarf_errmsg(de));
53952c23cb7cSEd Maste 				continue;
53962c23cb7cSEd Maste 			}
5397cf781b2eSEd Maste 			printf("%ju byte block:", (uintmax_t) v_block->bl_len);
53982c23cb7cSEd Maste 			b = v_block->bl_data;
53992c23cb7cSEd Maste 			for (j = 0; (Dwarf_Unsigned) j < v_block->bl_len; j++)
54002c23cb7cSEd Maste 				printf(" %x", b[j]);
5401cf781b2eSEd Maste 			printf("\t(");
5402cf781b2eSEd Maste 			dump_dwarf_block(re, v_block->bl_data, v_block->bl_len);
5403cf781b2eSEd Maste 			putchar(')');
54042c23cb7cSEd Maste 			break;
5405cf781b2eSEd Maste 
5406cf781b2eSEd Maste 		case DW_FORM_exprloc:
5407cf781b2eSEd Maste 			if (dwarf_formexprloc(attr_list[i], &v_udata, &v_expr,
5408cf781b2eSEd Maste 			    &de) != DW_DLV_OK) {
5409cf781b2eSEd Maste 				warnx("dwarf_formexprloc failed: %s",
5410cf781b2eSEd Maste 				    dwarf_errmsg(de));
5411cf781b2eSEd Maste 				continue;
5412cf781b2eSEd Maste 			}
5413cf781b2eSEd Maste 			printf("%ju byte block:", (uintmax_t) v_udata);
5414cf781b2eSEd Maste 			b = v_expr;
5415cf781b2eSEd Maste 			for (j = 0; (Dwarf_Unsigned) j < v_udata; j++)
5416cf781b2eSEd Maste 				printf(" %x", b[j]);
5417cf781b2eSEd Maste 			printf("\t(");
5418cf781b2eSEd Maste 			dump_dwarf_block(re, v_expr, v_udata);
5419cf781b2eSEd Maste 			putchar(')');
5420cf781b2eSEd Maste 			break;
5421cf781b2eSEd Maste 
5422cf781b2eSEd Maste 		case DW_FORM_ref_sig8:
5423cf781b2eSEd Maste 			if (dwarf_formsig8(attr_list[i], &v_sig8, &de) !=
5424cf781b2eSEd Maste 			    DW_DLV_OK) {
5425cf781b2eSEd Maste 				warnx("dwarf_formsig8 failed: %s",
5426cf781b2eSEd Maste 				    dwarf_errmsg(de));
5427cf781b2eSEd Maste 				continue;
5428cf781b2eSEd Maste 			}
5429cf781b2eSEd Maste 			p = (uint8_t *)(uintptr_t) &v_sig8.signature[0];
5430cf781b2eSEd Maste 			v_sig = re->dw_decode(&p, 8);
5431cf781b2eSEd Maste 			printf("signature: 0x%jx", (uintmax_t) v_sig);
54322c23cb7cSEd Maste 		}
54332c23cb7cSEd Maste 		switch (attr) {
54342c23cb7cSEd Maste 		case DW_AT_encoding:
54352c23cb7cSEd Maste 			if (dwarf_attrval_unsigned(die, attr, &ate, &de) !=
54362c23cb7cSEd Maste 			    DW_DLV_OK)
54372c23cb7cSEd Maste 				break;
54382c23cb7cSEd Maste 			if (dwarf_get_ATE_name(ate, &ate_str) != DW_DLV_OK)
5439cf781b2eSEd Maste 				ate_str = "DW_ATE_UNKNOWN";
54402c23cb7cSEd Maste 			printf("\t(%s)", &ate_str[strlen("DW_ATE_")]);
54412c23cb7cSEd Maste 			break;
5442cf781b2eSEd Maste 
5443cf781b2eSEd Maste 		case DW_AT_language:
5444cf781b2eSEd Maste 			if (dwarf_attrval_unsigned(die, attr, &lang, &de) !=
5445cf781b2eSEd Maste 			    DW_DLV_OK)
5446cf781b2eSEd Maste 				break;
5447cf781b2eSEd Maste 			if (dwarf_get_LANG_name(lang, &lang_str) != DW_DLV_OK)
5448cf781b2eSEd Maste 				break;
5449cf781b2eSEd Maste 			printf("\t(%s)", &lang_str[strlen("DW_LANG_")]);
5450cf781b2eSEd Maste 			break;
5451cf781b2eSEd Maste 
5452cf781b2eSEd Maste 		case DW_AT_location:
5453cf781b2eSEd Maste 		case DW_AT_string_length:
5454cf781b2eSEd Maste 		case DW_AT_return_addr:
5455cf781b2eSEd Maste 		case DW_AT_data_member_location:
5456cf781b2eSEd Maste 		case DW_AT_frame_base:
5457cf781b2eSEd Maste 		case DW_AT_segment:
5458cf781b2eSEd Maste 		case DW_AT_static_link:
5459cf781b2eSEd Maste 		case DW_AT_use_location:
5460cf781b2eSEd Maste 		case DW_AT_vtable_elem_location:
5461cf781b2eSEd Maste 			switch (form) {
5462cf781b2eSEd Maste 			case DW_FORM_data4:
5463cf781b2eSEd Maste 			case DW_FORM_data8:
5464cf781b2eSEd Maste 			case DW_FORM_sec_offset:
5465cf781b2eSEd Maste 				printf("\t(location list)");
5466cf781b2eSEd Maste 				break;
5467cf781b2eSEd Maste 			default:
5468cf781b2eSEd Maste 				break;
5469cf781b2eSEd Maste 			}
5470cf781b2eSEd Maste 
54712c23cb7cSEd Maste 		default:
54722c23cb7cSEd Maste 			break;
54732c23cb7cSEd Maste 		}
54742c23cb7cSEd Maste 		putchar('\n');
54752c23cb7cSEd Maste 	}
54762c23cb7cSEd Maste 
54772c23cb7cSEd Maste 
54782c23cb7cSEd Maste cont_search:
54792c23cb7cSEd Maste 	/* Search children. */
54802c23cb7cSEd Maste 	ret = dwarf_child(die, &ret_die, &de);
54812c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
54822c23cb7cSEd Maste 		warnx("dwarf_child: %s", dwarf_errmsg(de));
54832c23cb7cSEd Maste 	else if (ret == DW_DLV_OK)
54842c23cb7cSEd Maste 		dump_dwarf_die(re, ret_die, level + 1);
54852c23cb7cSEd Maste 
54862c23cb7cSEd Maste 	/* Search sibling. */
5487cf781b2eSEd Maste 	is_info = dwarf_get_die_infotypes_flag(die);
5488cf781b2eSEd Maste 	ret = dwarf_siblingof_b(re->dbg, die, &ret_die, is_info, &de);
54892c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
54902c23cb7cSEd Maste 		warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
54912c23cb7cSEd Maste 	else if (ret == DW_DLV_OK)
54922c23cb7cSEd Maste 		dump_dwarf_die(re, ret_die, level);
54932c23cb7cSEd Maste 
54942c23cb7cSEd Maste 	dwarf_dealloc(re->dbg, die, DW_DLA_DIE);
54952c23cb7cSEd Maste }
54962c23cb7cSEd Maste 
54972c23cb7cSEd Maste static void
set_cu_context(struct readelf * re,Dwarf_Half psize,Dwarf_Half osize,Dwarf_Half ver)5498cf781b2eSEd Maste set_cu_context(struct readelf *re, Dwarf_Half psize, Dwarf_Half osize,
5499cf781b2eSEd Maste     Dwarf_Half ver)
5500cf781b2eSEd Maste {
5501cf781b2eSEd Maste 
5502cf781b2eSEd Maste 	re->cu_psize = psize;
5503cf781b2eSEd Maste 	re->cu_osize = osize;
5504cf781b2eSEd Maste 	re->cu_ver = ver;
5505cf781b2eSEd Maste }
5506cf781b2eSEd Maste 
5507cf781b2eSEd Maste static void
dump_dwarf_info(struct readelf * re,Dwarf_Bool is_info)5508cf781b2eSEd Maste dump_dwarf_info(struct readelf *re, Dwarf_Bool is_info)
55092c23cb7cSEd Maste {
55102c23cb7cSEd Maste 	struct section *s;
55112c23cb7cSEd Maste 	Dwarf_Die die;
55122c23cb7cSEd Maste 	Dwarf_Error de;
5513cf781b2eSEd Maste 	Dwarf_Half tag, version, pointer_size, off_size;
55142c23cb7cSEd Maste 	Dwarf_Off cu_offset, cu_length;
55152c23cb7cSEd Maste 	Dwarf_Off aboff;
5516cf781b2eSEd Maste 	Dwarf_Unsigned typeoff;
5517cf781b2eSEd Maste 	Dwarf_Sig8 sig8;
5518cf781b2eSEd Maste 	Dwarf_Unsigned sig;
5519cf781b2eSEd Maste 	uint8_t *p;
5520cf781b2eSEd Maste 	const char *sn;
5521cf781b2eSEd Maste 	int i, ret;
55222c23cb7cSEd Maste 
5523cf781b2eSEd Maste 	sn = is_info ? ".debug_info" : ".debug_types";
55242c23cb7cSEd Maste 
55252c23cb7cSEd Maste 	s = NULL;
55262c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
55272c23cb7cSEd Maste 		s = &re->sl[i];
5528cf781b2eSEd Maste 		if (s->name != NULL && !strcmp(s->name, sn))
55292c23cb7cSEd Maste 			break;
55302c23cb7cSEd Maste 	}
55312c23cb7cSEd Maste 	if ((size_t) i >= re->shnum)
55322c23cb7cSEd Maste 		return;
55332c23cb7cSEd Maste 
5534cf781b2eSEd Maste 	do {
5535cf781b2eSEd Maste 		printf("\nDump of debug contents of section %s:\n", sn);
55362c23cb7cSEd Maste 
5537cf781b2eSEd Maste 		while ((ret = dwarf_next_cu_header_c(re->dbg, is_info, NULL,
5538cf781b2eSEd Maste 		    &version, &aboff, &pointer_size, &off_size, NULL, &sig8,
5539cf781b2eSEd Maste 		    &typeoff, NULL, &de)) == DW_DLV_OK) {
5540cf781b2eSEd Maste 			set_cu_context(re, pointer_size, off_size, version);
55412c23cb7cSEd Maste 			die = NULL;
5542cf781b2eSEd Maste 			while (dwarf_siblingof_b(re->dbg, die, &die, is_info,
5543cf781b2eSEd Maste 			    &de) == DW_DLV_OK) {
55442c23cb7cSEd Maste 				if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
55452c23cb7cSEd Maste 					warnx("dwarf_tag failed: %s",
55462c23cb7cSEd Maste 					    dwarf_errmsg(de));
5547cf781b2eSEd Maste 					continue;
55482c23cb7cSEd Maste 				}
55492c23cb7cSEd Maste 				/* XXX: What about DW_TAG_partial_unit? */
5550cf781b2eSEd Maste 				if ((is_info && tag == DW_TAG_compile_unit) ||
5551cf781b2eSEd Maste 				    (!is_info && tag == DW_TAG_type_unit))
55522c23cb7cSEd Maste 					break;
55532c23cb7cSEd Maste 			}
5554cf781b2eSEd Maste 			if (die == NULL && is_info) {
5555cf781b2eSEd Maste 				warnx("could not find DW_TAG_compile_unit "
5556cf781b2eSEd Maste 				    "die");
5557cf781b2eSEd Maste 				continue;
5558cf781b2eSEd Maste 			} else if (die == NULL && !is_info) {
5559cf781b2eSEd Maste 				warnx("could not find DW_TAG_type_unit die");
5560cf781b2eSEd Maste 				continue;
55612c23cb7cSEd Maste 			}
55622c23cb7cSEd Maste 
5563cf781b2eSEd Maste 			if (dwarf_die_CU_offset_range(die, &cu_offset,
5564cf781b2eSEd Maste 			    &cu_length, &de) != DW_DLV_OK) {
55652c23cb7cSEd Maste 				warnx("dwarf_die_CU_offset failed: %s",
55662c23cb7cSEd Maste 				    dwarf_errmsg(de));
55672c23cb7cSEd Maste 				continue;
55682c23cb7cSEd Maste 			}
55692c23cb7cSEd Maste 
5570cf781b2eSEd Maste 			cu_length -= off_size == 4 ? 4 : 12;
5571cf781b2eSEd Maste 
5572cf781b2eSEd Maste 			sig = 0;
5573cf781b2eSEd Maste 			if (!is_info) {
5574cf781b2eSEd Maste 				p = (uint8_t *)(uintptr_t) &sig8.signature[0];
5575cf781b2eSEd Maste 				sig = re->dw_decode(&p, 8);
5576cf781b2eSEd Maste 			}
5577cf781b2eSEd Maste 
5578cf781b2eSEd Maste 			printf("\n  Type Unit @ offset 0x%jx:\n",
5579cf781b2eSEd Maste 			    (uintmax_t) cu_offset);
5580cf781b2eSEd Maste 			printf("    Length:\t\t%#jx (%d-bit)\n",
5581cf781b2eSEd Maste 			    (uintmax_t) cu_length, off_size == 4 ? 32 : 64);
55822c23cb7cSEd Maste 			printf("    Version:\t\t%u\n", version);
5583cf781b2eSEd Maste 			printf("    Abbrev Offset:\t0x%jx\n",
5584cf781b2eSEd Maste 			    (uintmax_t) aboff);
55852c23cb7cSEd Maste 			printf("    Pointer Size:\t%u\n", pointer_size);
5586cf781b2eSEd Maste 			if (!is_info) {
5587cf781b2eSEd Maste 				printf("    Signature:\t\t0x%016jx\n",
5588cf781b2eSEd Maste 				    (uintmax_t) sig);
5589cf781b2eSEd Maste 				printf("    Type Offset:\t0x%jx\n",
5590cf781b2eSEd Maste 				    (uintmax_t) typeoff);
5591cf781b2eSEd Maste 			}
55922c23cb7cSEd Maste 
55932c23cb7cSEd Maste 			dump_dwarf_die(re, die, 0);
55942c23cb7cSEd Maste 		}
55952c23cb7cSEd Maste 		if (ret == DW_DLV_ERROR)
55962c23cb7cSEd Maste 			warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
5597cf781b2eSEd Maste 		if (is_info)
5598cf781b2eSEd Maste 			break;
5599cf781b2eSEd Maste 	} while (dwarf_next_types_section(re->dbg, &de) == DW_DLV_OK);
56002c23cb7cSEd Maste }
56012c23cb7cSEd Maste 
56022c23cb7cSEd Maste static void
dump_dwarf_abbrev(struct readelf * re)56032c23cb7cSEd Maste dump_dwarf_abbrev(struct readelf *re)
56042c23cb7cSEd Maste {
56052c23cb7cSEd Maste 	Dwarf_Abbrev ab;
56062c23cb7cSEd Maste 	Dwarf_Off aboff, atoff;
56072c23cb7cSEd Maste 	Dwarf_Unsigned length, attr_count;
56082c23cb7cSEd Maste 	Dwarf_Signed flag, form;
56092c23cb7cSEd Maste 	Dwarf_Half tag, attr;
56102c23cb7cSEd Maste 	Dwarf_Error de;
56112c23cb7cSEd Maste 	const char *tag_str, *attr_str, *form_str;
5612cf781b2eSEd Maste 	char unk_tag[32], unk_attr[32], unk_form[32];
56132c23cb7cSEd Maste 	int i, j, ret;
56142c23cb7cSEd Maste 
56152c23cb7cSEd Maste 	printf("\nContents of section .debug_abbrev:\n\n");
56162c23cb7cSEd Maste 
56172c23cb7cSEd Maste 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, &aboff,
56182c23cb7cSEd Maste 	    NULL, NULL, &de)) ==  DW_DLV_OK) {
56192c23cb7cSEd Maste 		printf("  Number TAG\n");
56202c23cb7cSEd Maste 		i = 0;
56212c23cb7cSEd Maste 		while ((ret = dwarf_get_abbrev(re->dbg, aboff, &ab, &length,
56222c23cb7cSEd Maste 		    &attr_count, &de)) == DW_DLV_OK) {
56232c23cb7cSEd Maste 			if (length == 1) {
56242c23cb7cSEd Maste 				dwarf_dealloc(re->dbg, ab, DW_DLA_ABBREV);
56252c23cb7cSEd Maste 				break;
56262c23cb7cSEd Maste 			}
56272c23cb7cSEd Maste 			aboff += length;
56282c23cb7cSEd Maste 			printf("%4d", ++i);
56292c23cb7cSEd Maste 			if (dwarf_get_abbrev_tag(ab, &tag, &de) != DW_DLV_OK) {
56302c23cb7cSEd Maste 				warnx("dwarf_get_abbrev_tag failed: %s",
56312c23cb7cSEd Maste 				    dwarf_errmsg(de));
56322c23cb7cSEd Maste 				goto next_abbrev;
56332c23cb7cSEd Maste 			}
56342c23cb7cSEd Maste 			if (dwarf_get_TAG_name(tag, &tag_str) != DW_DLV_OK) {
5635cf781b2eSEd Maste 				snprintf(unk_tag, sizeof(unk_tag),
5636cf781b2eSEd Maste 				    "[Unknown Tag: %#x]", tag);
5637cf781b2eSEd Maste 				tag_str = unk_tag;
56382c23cb7cSEd Maste 			}
56392c23cb7cSEd Maste 			if (dwarf_get_abbrev_children_flag(ab, &flag, &de) !=
56402c23cb7cSEd Maste 			    DW_DLV_OK) {
56412c23cb7cSEd Maste 				warnx("dwarf_get_abbrev_children_flag failed:"
56422c23cb7cSEd Maste 				    " %s", dwarf_errmsg(de));
56432c23cb7cSEd Maste 				goto next_abbrev;
56442c23cb7cSEd Maste 			}
56452c23cb7cSEd Maste 			printf("      %s    %s\n", tag_str,
56462c23cb7cSEd Maste 			    flag ? "[has children]" : "[no children]");
56472c23cb7cSEd Maste 			for (j = 0; (Dwarf_Unsigned) j < attr_count; j++) {
56482c23cb7cSEd Maste 				if (dwarf_get_abbrev_entry(ab, (Dwarf_Signed) j,
56492c23cb7cSEd Maste 				    &attr, &form, &atoff, &de) != DW_DLV_OK) {
56502c23cb7cSEd Maste 					warnx("dwarf_get_abbrev_entry failed:"
56512c23cb7cSEd Maste 					    " %s", dwarf_errmsg(de));
56522c23cb7cSEd Maste 					continue;
56532c23cb7cSEd Maste 				}
56542c23cb7cSEd Maste 				if (dwarf_get_AT_name(attr, &attr_str) !=
56552c23cb7cSEd Maste 				    DW_DLV_OK) {
5656cf781b2eSEd Maste 					snprintf(unk_attr, sizeof(unk_attr),
5657cf781b2eSEd Maste 					    "[Unknown AT: %#x]", attr);
5658cf781b2eSEd Maste 					attr_str = unk_attr;
56592c23cb7cSEd Maste 				}
56602c23cb7cSEd Maste 				if (dwarf_get_FORM_name(form, &form_str) !=
56612c23cb7cSEd Maste 				    DW_DLV_OK) {
5662cf781b2eSEd Maste 					snprintf(unk_form, sizeof(unk_form),
5663cf781b2eSEd Maste 					    "[Unknown Form: %#x]",
5664cf781b2eSEd Maste 					    (Dwarf_Half) form);
5665cf781b2eSEd Maste 					form_str = unk_form;
56662c23cb7cSEd Maste 				}
56672c23cb7cSEd Maste 				printf("    %-18s %s\n", attr_str, form_str);
56682c23cb7cSEd Maste 			}
56692c23cb7cSEd Maste 		next_abbrev:
56702c23cb7cSEd Maste 			dwarf_dealloc(re->dbg, ab, DW_DLA_ABBREV);
56712c23cb7cSEd Maste 		}
56722c23cb7cSEd Maste 		if (ret != DW_DLV_OK)
56732c23cb7cSEd Maste 			warnx("dwarf_get_abbrev: %s", dwarf_errmsg(de));
56742c23cb7cSEd Maste 	}
56752c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
56762c23cb7cSEd Maste 		warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
56772c23cb7cSEd Maste }
56782c23cb7cSEd Maste 
56792c23cb7cSEd Maste static void
dump_dwarf_pubnames(struct readelf * re)56802c23cb7cSEd Maste dump_dwarf_pubnames(struct readelf *re)
56812c23cb7cSEd Maste {
56822c23cb7cSEd Maste 	struct section *s;
56832c23cb7cSEd Maste 	Dwarf_Off die_off;
56842c23cb7cSEd Maste 	Dwarf_Unsigned offset, length, nt_cu_offset, nt_cu_length;
56852c23cb7cSEd Maste 	Dwarf_Signed cnt;
56862c23cb7cSEd Maste 	Dwarf_Global *globs;
56872c23cb7cSEd Maste 	Dwarf_Half nt_version;
56882c23cb7cSEd Maste 	Dwarf_Error de;
56892c23cb7cSEd Maste 	Elf_Data *d;
56902c23cb7cSEd Maste 	char *glob_name;
56912c23cb7cSEd Maste 	int i, dwarf_size, elferr;
56922c23cb7cSEd Maste 
56932c23cb7cSEd Maste 	printf("\nContents of the .debug_pubnames section:\n");
56942c23cb7cSEd Maste 
56952c23cb7cSEd Maste 	s = NULL;
56962c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
56972c23cb7cSEd Maste 		s = &re->sl[i];
56982c23cb7cSEd Maste 		if (s->name != NULL && !strcmp(s->name, ".debug_pubnames"))
56992c23cb7cSEd Maste 			break;
57002c23cb7cSEd Maste 	}
57012c23cb7cSEd Maste 	if ((size_t) i >= re->shnum)
57022c23cb7cSEd Maste 		return;
57032c23cb7cSEd Maste 
57042c23cb7cSEd Maste 	(void) elf_errno();
57052c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
57062c23cb7cSEd Maste 		elferr = elf_errno();
57072c23cb7cSEd Maste 		if (elferr != 0)
57082c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
57092c23cb7cSEd Maste 		return;
57102c23cb7cSEd Maste 	}
57112c23cb7cSEd Maste 	if (d->d_size <= 0)
57122c23cb7cSEd Maste 		return;
57132c23cb7cSEd Maste 
57142c23cb7cSEd Maste 	/* Read in .debug_pubnames section table header. */
57152c23cb7cSEd Maste 	offset = 0;
57162c23cb7cSEd Maste 	length = re->dw_read(d, &offset, 4);
57172c23cb7cSEd Maste 	if (length == 0xffffffff) {
57182c23cb7cSEd Maste 		dwarf_size = 8;
57192c23cb7cSEd Maste 		length = re->dw_read(d, &offset, 8);
57202c23cb7cSEd Maste 	} else
57212c23cb7cSEd Maste 		dwarf_size = 4;
57222c23cb7cSEd Maste 
57232c23cb7cSEd Maste 	if (length > d->d_size - offset) {
57242c23cb7cSEd Maste 		warnx("invalid .dwarf_pubnames section");
57252c23cb7cSEd Maste 		return;
57262c23cb7cSEd Maste 	}
57272c23cb7cSEd Maste 
57282c23cb7cSEd Maste 	nt_version = re->dw_read(d, &offset, 2);
57292c23cb7cSEd Maste 	nt_cu_offset = re->dw_read(d, &offset, dwarf_size);
57302c23cb7cSEd Maste 	nt_cu_length = re->dw_read(d, &offset, dwarf_size);
57312c23cb7cSEd Maste 	printf("  Length:\t\t\t\t%ju\n", (uintmax_t) length);
57322c23cb7cSEd Maste 	printf("  Version:\t\t\t\t%u\n", nt_version);
57332c23cb7cSEd Maste 	printf("  Offset into .debug_info section:\t%ju\n",
57342c23cb7cSEd Maste 	    (uintmax_t) nt_cu_offset);
57352c23cb7cSEd Maste 	printf("  Size of area in .debug_info section:\t%ju\n",
57362c23cb7cSEd Maste 	    (uintmax_t) nt_cu_length);
57372c23cb7cSEd Maste 
57382c23cb7cSEd Maste 	if (dwarf_get_globals(re->dbg, &globs, &cnt, &de) != DW_DLV_OK) {
57392c23cb7cSEd Maste 		warnx("dwarf_get_globals failed: %s", dwarf_errmsg(de));
57402c23cb7cSEd Maste 		return;
57412c23cb7cSEd Maste 	}
57422c23cb7cSEd Maste 
57432c23cb7cSEd Maste 	printf("\n    Offset      Name\n");
57442c23cb7cSEd Maste 	for (i = 0; i < cnt; i++) {
57452c23cb7cSEd Maste 		if (dwarf_globname(globs[i], &glob_name, &de) != DW_DLV_OK) {
57462c23cb7cSEd Maste 			warnx("dwarf_globname failed: %s", dwarf_errmsg(de));
57472c23cb7cSEd Maste 			continue;
57482c23cb7cSEd Maste 		}
57492c23cb7cSEd Maste 		if (dwarf_global_die_offset(globs[i], &die_off, &de) !=
57502c23cb7cSEd Maste 		    DW_DLV_OK) {
57512c23cb7cSEd Maste 			warnx("dwarf_global_die_offset failed: %s",
57522c23cb7cSEd Maste 			    dwarf_errmsg(de));
57532c23cb7cSEd Maste 			continue;
57542c23cb7cSEd Maste 		}
57552c23cb7cSEd Maste 		printf("    %-11ju %s\n", (uintmax_t) die_off, glob_name);
57562c23cb7cSEd Maste 	}
57572c23cb7cSEd Maste }
57582c23cb7cSEd Maste 
57592c23cb7cSEd Maste static void
dump_dwarf_aranges(struct readelf * re)57602c23cb7cSEd Maste dump_dwarf_aranges(struct readelf *re)
57612c23cb7cSEd Maste {
57622c23cb7cSEd Maste 	struct section *s;
57632c23cb7cSEd Maste 	Dwarf_Arange *aranges;
57642c23cb7cSEd Maste 	Dwarf_Addr start;
57652c23cb7cSEd Maste 	Dwarf_Unsigned offset, length, as_cu_offset;
57662c23cb7cSEd Maste 	Dwarf_Off die_off;
57672c23cb7cSEd Maste 	Dwarf_Signed cnt;
57682c23cb7cSEd Maste 	Dwarf_Half as_version, as_addrsz, as_segsz;
57692c23cb7cSEd Maste 	Dwarf_Error de;
57702c23cb7cSEd Maste 	Elf_Data *d;
57712c23cb7cSEd Maste 	int i, dwarf_size, elferr;
57722c23cb7cSEd Maste 
57732c23cb7cSEd Maste 	printf("\nContents of section .debug_aranges:\n");
57742c23cb7cSEd Maste 
57752c23cb7cSEd Maste 	s = NULL;
57762c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
57772c23cb7cSEd Maste 		s = &re->sl[i];
57782c23cb7cSEd Maste 		if (s->name != NULL && !strcmp(s->name, ".debug_aranges"))
57792c23cb7cSEd Maste 			break;
57802c23cb7cSEd Maste 	}
57812c23cb7cSEd Maste 	if ((size_t) i >= re->shnum)
57822c23cb7cSEd Maste 		return;
57832c23cb7cSEd Maste 
57842c23cb7cSEd Maste 	(void) elf_errno();
57852c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
57862c23cb7cSEd Maste 		elferr = elf_errno();
57872c23cb7cSEd Maste 		if (elferr != 0)
57882c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
57892c23cb7cSEd Maste 		return;
57902c23cb7cSEd Maste 	}
57912c23cb7cSEd Maste 	if (d->d_size <= 0)
57922c23cb7cSEd Maste 		return;
57932c23cb7cSEd Maste 
57942c23cb7cSEd Maste 	/* Read in the .debug_aranges section table header. */
57952c23cb7cSEd Maste 	offset = 0;
57962c23cb7cSEd Maste 	length = re->dw_read(d, &offset, 4);
57972c23cb7cSEd Maste 	if (length == 0xffffffff) {
57982c23cb7cSEd Maste 		dwarf_size = 8;
57992c23cb7cSEd Maste 		length = re->dw_read(d, &offset, 8);
58002c23cb7cSEd Maste 	} else
58012c23cb7cSEd Maste 		dwarf_size = 4;
58022c23cb7cSEd Maste 
58032c23cb7cSEd Maste 	if (length > d->d_size - offset) {
58042c23cb7cSEd Maste 		warnx("invalid .dwarf_aranges section");
58052c23cb7cSEd Maste 		return;
58062c23cb7cSEd Maste 	}
58072c23cb7cSEd Maste 
58082c23cb7cSEd Maste 	as_version = re->dw_read(d, &offset, 2);
58092c23cb7cSEd Maste 	as_cu_offset = re->dw_read(d, &offset, dwarf_size);
58102c23cb7cSEd Maste 	as_addrsz = re->dw_read(d, &offset, 1);
58112c23cb7cSEd Maste 	as_segsz = re->dw_read(d, &offset, 1);
58122c23cb7cSEd Maste 
58132c23cb7cSEd Maste 	printf("  Length:\t\t\t%ju\n", (uintmax_t) length);
58142c23cb7cSEd Maste 	printf("  Version:\t\t\t%u\n", as_version);
58152c23cb7cSEd Maste 	printf("  Offset into .debug_info:\t%ju\n", (uintmax_t) as_cu_offset);
58162c23cb7cSEd Maste 	printf("  Pointer Size:\t\t\t%u\n", as_addrsz);
58172c23cb7cSEd Maste 	printf("  Segment Size:\t\t\t%u\n", as_segsz);
58182c23cb7cSEd Maste 
58192c23cb7cSEd Maste 	if (dwarf_get_aranges(re->dbg, &aranges, &cnt, &de) != DW_DLV_OK) {
58202c23cb7cSEd Maste 		warnx("dwarf_get_aranges failed: %s", dwarf_errmsg(de));
58212c23cb7cSEd Maste 		return;
58222c23cb7cSEd Maste 	}
58232c23cb7cSEd Maste 
58242c23cb7cSEd Maste 	printf("\n    Address  Length\n");
58252c23cb7cSEd Maste 	for (i = 0; i < cnt; i++) {
58262c23cb7cSEd Maste 		if (dwarf_get_arange_info(aranges[i], &start, &length,
58272c23cb7cSEd Maste 		    &die_off, &de) != DW_DLV_OK) {
58282c23cb7cSEd Maste 			warnx("dwarf_get_arange_info failed: %s",
58292c23cb7cSEd Maste 			    dwarf_errmsg(de));
58302c23cb7cSEd Maste 			continue;
58312c23cb7cSEd Maste 		}
58322c23cb7cSEd Maste 		printf("    %08jx %ju\n", (uintmax_t) start,
58332c23cb7cSEd Maste 		    (uintmax_t) length);
58342c23cb7cSEd Maste 	}
58352c23cb7cSEd Maste }
58362c23cb7cSEd Maste 
58372c23cb7cSEd Maste static void
dump_dwarf_ranges_foreach(struct readelf * re,Dwarf_Die die,Dwarf_Addr base)58382c23cb7cSEd Maste dump_dwarf_ranges_foreach(struct readelf *re, Dwarf_Die die, Dwarf_Addr base)
58392c23cb7cSEd Maste {
58402c23cb7cSEd Maste 	Dwarf_Attribute *attr_list;
58412c23cb7cSEd Maste 	Dwarf_Ranges *ranges;
58422c23cb7cSEd Maste 	Dwarf_Die ret_die;
58432c23cb7cSEd Maste 	Dwarf_Error de;
58442c23cb7cSEd Maste 	Dwarf_Addr base0;
58452c23cb7cSEd Maste 	Dwarf_Half attr;
58462c23cb7cSEd Maste 	Dwarf_Signed attr_count, cnt;
5847cec2d0b1SChristos Margiolis 	Dwarf_Unsigned bytecnt;
5848cec2d0b1SChristos Margiolis 	Dwarf_Off off;
58492c23cb7cSEd Maste 	int i, j, ret;
58502c23cb7cSEd Maste 
58512c23cb7cSEd Maste 	if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
58522c23cb7cSEd Maste 	    DW_DLV_OK) {
58532c23cb7cSEd Maste 		if (ret == DW_DLV_ERROR)
58542c23cb7cSEd Maste 			warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
58552c23cb7cSEd Maste 		goto cont_search;
58562c23cb7cSEd Maste 	}
58572c23cb7cSEd Maste 
58582c23cb7cSEd Maste 	for (i = 0; i < attr_count; i++) {
58592c23cb7cSEd Maste 		if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
58602c23cb7cSEd Maste 			warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
58612c23cb7cSEd Maste 			continue;
58622c23cb7cSEd Maste 		}
58632c23cb7cSEd Maste 		if (attr != DW_AT_ranges)
58642c23cb7cSEd Maste 			continue;
5865cec2d0b1SChristos Margiolis 		if (dwarf_global_formref(attr_list[i], &off, &de) != DW_DLV_OK) {
5866cec2d0b1SChristos Margiolis 			warnx("dwarf_global_formref failed: %s",
5867cec2d0b1SChristos Margiolis 			    dwarf_errmsg(de));
58682c23cb7cSEd Maste 			continue;
58692c23cb7cSEd Maste 		}
5870cec2d0b1SChristos Margiolis 		if (dwarf_get_ranges(re->dbg, off, &ranges, &cnt,
58712c23cb7cSEd Maste 		    &bytecnt, &de) != DW_DLV_OK)
58722c23cb7cSEd Maste 			continue;
58732c23cb7cSEd Maste 		base0 = base;
58742c23cb7cSEd Maste 		for (j = 0; j < cnt; j++) {
58752c23cb7cSEd Maste 			printf("    %08jx ", (uintmax_t) off);
58762c23cb7cSEd Maste 			if (ranges[j].dwr_type == DW_RANGES_END) {
58772c23cb7cSEd Maste 				printf("%s\n", "<End of list>");
58782c23cb7cSEd Maste 				continue;
58792c23cb7cSEd Maste 			} else if (ranges[j].dwr_type ==
58802c23cb7cSEd Maste 			    DW_RANGES_ADDRESS_SELECTION) {
58812c23cb7cSEd Maste 				base0 = ranges[j].dwr_addr2;
58822c23cb7cSEd Maste 				continue;
58832c23cb7cSEd Maste 			}
58842c23cb7cSEd Maste 			if (re->ec == ELFCLASS32)
58852c23cb7cSEd Maste 				printf("%08jx %08jx\n",
5886839529caSEd Maste 				    (uintmax_t) (ranges[j].dwr_addr1 + base0),
5887839529caSEd Maste 				    (uintmax_t) (ranges[j].dwr_addr2 + base0));
58882c23cb7cSEd Maste 			else
58892c23cb7cSEd Maste 				printf("%016jx %016jx\n",
5890839529caSEd Maste 				    (uintmax_t) (ranges[j].dwr_addr1 + base0),
5891839529caSEd Maste 				    (uintmax_t) (ranges[j].dwr_addr2 + base0));
58922c23cb7cSEd Maste 		}
58932c23cb7cSEd Maste 	}
58942c23cb7cSEd Maste 
58952c23cb7cSEd Maste cont_search:
58962c23cb7cSEd Maste 	/* Search children. */
58972c23cb7cSEd Maste 	ret = dwarf_child(die, &ret_die, &de);
58982c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
58992c23cb7cSEd Maste 		warnx("dwarf_child: %s", dwarf_errmsg(de));
59002c23cb7cSEd Maste 	else if (ret == DW_DLV_OK)
59012c23cb7cSEd Maste 		dump_dwarf_ranges_foreach(re, ret_die, base);
59022c23cb7cSEd Maste 
59032c23cb7cSEd Maste 	/* Search sibling. */
59042c23cb7cSEd Maste 	ret = dwarf_siblingof(re->dbg, die, &ret_die, &de);
59052c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
59062c23cb7cSEd Maste 		warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
59072c23cb7cSEd Maste 	else if (ret == DW_DLV_OK)
59082c23cb7cSEd Maste 		dump_dwarf_ranges_foreach(re, ret_die, base);
5909cec2d0b1SChristos Margiolis 
5910cec2d0b1SChristos Margiolis 	dwarf_dealloc(re->dbg, die, DW_DLA_DIE);
59112c23cb7cSEd Maste }
59122c23cb7cSEd Maste 
59132c23cb7cSEd Maste static void
dump_dwarf_ranges(struct readelf * re)59142c23cb7cSEd Maste dump_dwarf_ranges(struct readelf *re)
59152c23cb7cSEd Maste {
59162c23cb7cSEd Maste 	Dwarf_Ranges *ranges;
59172c23cb7cSEd Maste 	Dwarf_Die die;
59182c23cb7cSEd Maste 	Dwarf_Signed cnt;
59192c23cb7cSEd Maste 	Dwarf_Unsigned bytecnt;
59202c23cb7cSEd Maste 	Dwarf_Half tag;
59212c23cb7cSEd Maste 	Dwarf_Error de;
59222c23cb7cSEd Maste 	Dwarf_Unsigned lowpc;
59232c23cb7cSEd Maste 	int ret;
59242c23cb7cSEd Maste 
59252c23cb7cSEd Maste 	if (dwarf_get_ranges(re->dbg, 0, &ranges, &cnt, &bytecnt, &de) !=
59262c23cb7cSEd Maste 	    DW_DLV_OK)
59272c23cb7cSEd Maste 		return;
59282c23cb7cSEd Maste 
59292c23cb7cSEd Maste 	printf("Contents of the .debug_ranges section:\n\n");
59302c23cb7cSEd Maste 	if (re->ec == ELFCLASS32)
59312c23cb7cSEd Maste 		printf("    %-8s %-8s %s\n", "Offset", "Begin", "End");
59322c23cb7cSEd Maste 	else
59332c23cb7cSEd Maste 		printf("    %-8s %-16s %s\n", "Offset", "Begin", "End");
59342c23cb7cSEd Maste 
59352c23cb7cSEd Maste 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
59362c23cb7cSEd Maste 	    NULL, &de)) == DW_DLV_OK) {
59372c23cb7cSEd Maste 		die = NULL;
59382c23cb7cSEd Maste 		if (dwarf_siblingof(re->dbg, die, &die, &de) != DW_DLV_OK)
59392c23cb7cSEd Maste 			continue;
59402c23cb7cSEd Maste 		if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
59412c23cb7cSEd Maste 			warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
59422c23cb7cSEd Maste 			continue;
59432c23cb7cSEd Maste 		}
59442c23cb7cSEd Maste 		/* XXX: What about DW_TAG_partial_unit? */
59452c23cb7cSEd Maste 		lowpc = 0;
59462c23cb7cSEd Maste 		if (tag == DW_TAG_compile_unit) {
59472c23cb7cSEd Maste 			if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lowpc,
59482c23cb7cSEd Maste 			    &de) != DW_DLV_OK)
59492c23cb7cSEd Maste 				lowpc = 0;
59502c23cb7cSEd Maste 		}
59512c23cb7cSEd Maste 
59522c23cb7cSEd Maste 		dump_dwarf_ranges_foreach(re, die, (Dwarf_Addr) lowpc);
59532c23cb7cSEd Maste 	}
59542c23cb7cSEd Maste 	putchar('\n');
59552c23cb7cSEd Maste }
59562c23cb7cSEd Maste 
59572c23cb7cSEd Maste static void
dump_dwarf_macinfo(struct readelf * re)59582c23cb7cSEd Maste dump_dwarf_macinfo(struct readelf *re)
59592c23cb7cSEd Maste {
59602c23cb7cSEd Maste 	Dwarf_Unsigned offset;
59612c23cb7cSEd Maste 	Dwarf_Signed cnt;
59622c23cb7cSEd Maste 	Dwarf_Macro_Details *md;
59632c23cb7cSEd Maste 	Dwarf_Error de;
59642c23cb7cSEd Maste 	const char *mi_str;
5965cf781b2eSEd Maste 	char unk_mi[32];
59662c23cb7cSEd Maste 	int i;
59672c23cb7cSEd Maste 
59682c23cb7cSEd Maste #define	_MAX_MACINFO_ENTRY	65535
59692c23cb7cSEd Maste 
59702c23cb7cSEd Maste 	printf("\nContents of section .debug_macinfo:\n\n");
59712c23cb7cSEd Maste 
59722c23cb7cSEd Maste 	offset = 0;
59732c23cb7cSEd Maste 	while (dwarf_get_macro_details(re->dbg, offset, _MAX_MACINFO_ENTRY,
59742c23cb7cSEd Maste 	    &cnt, &md, &de) == DW_DLV_OK) {
59752c23cb7cSEd Maste 		for (i = 0; i < cnt; i++) {
59762c23cb7cSEd Maste 			offset = md[i].dmd_offset + 1;
59772c23cb7cSEd Maste 			if (md[i].dmd_type == 0)
59782c23cb7cSEd Maste 				break;
59792c23cb7cSEd Maste 			if (dwarf_get_MACINFO_name(md[i].dmd_type, &mi_str) !=
59802c23cb7cSEd Maste 			    DW_DLV_OK) {
5981cf781b2eSEd Maste 				snprintf(unk_mi, sizeof(unk_mi),
5982cf781b2eSEd Maste 				    "[Unknown MACINFO: %#x]", md[i].dmd_type);
5983cf781b2eSEd Maste 				mi_str = unk_mi;
59842c23cb7cSEd Maste 			}
59852c23cb7cSEd Maste 			printf(" %s", mi_str);
59862c23cb7cSEd Maste 			switch (md[i].dmd_type) {
59872c23cb7cSEd Maste 			case DW_MACINFO_define:
59882c23cb7cSEd Maste 			case DW_MACINFO_undef:
59892c23cb7cSEd Maste 				printf(" - lineno : %jd macro : %s\n",
59902c23cb7cSEd Maste 				    (intmax_t) md[i].dmd_lineno,
59912c23cb7cSEd Maste 				    md[i].dmd_macro);
59922c23cb7cSEd Maste 				break;
59932c23cb7cSEd Maste 			case DW_MACINFO_start_file:
59942c23cb7cSEd Maste 				printf(" - lineno : %jd filenum : %jd\n",
59952c23cb7cSEd Maste 				    (intmax_t) md[i].dmd_lineno,
59962c23cb7cSEd Maste 				    (intmax_t) md[i].dmd_fileindex);
59972c23cb7cSEd Maste 				break;
59982c23cb7cSEd Maste 			default:
59992c23cb7cSEd Maste 				putchar('\n');
60002c23cb7cSEd Maste 				break;
60012c23cb7cSEd Maste 			}
60022c23cb7cSEd Maste 		}
60032c23cb7cSEd Maste 	}
60042c23cb7cSEd Maste 
60052c23cb7cSEd Maste #undef	_MAX_MACINFO_ENTRY
60062c23cb7cSEd Maste }
60072c23cb7cSEd Maste 
60082c23cb7cSEd Maste static void
dump_dwarf_frame_inst(struct readelf * re,Dwarf_Cie cie,uint8_t * insts,Dwarf_Unsigned len,Dwarf_Unsigned caf,Dwarf_Signed daf,Dwarf_Addr pc,Dwarf_Debug dbg)6009cf781b2eSEd Maste dump_dwarf_frame_inst(struct readelf *re, Dwarf_Cie cie, uint8_t *insts,
6010cf781b2eSEd Maste     Dwarf_Unsigned len, Dwarf_Unsigned caf, Dwarf_Signed daf, Dwarf_Addr pc,
6011cf781b2eSEd Maste     Dwarf_Debug dbg)
60122c23cb7cSEd Maste {
60132c23cb7cSEd Maste 	Dwarf_Frame_Op *oplist;
60142c23cb7cSEd Maste 	Dwarf_Signed opcnt, delta;
60152c23cb7cSEd Maste 	Dwarf_Small op;
60162c23cb7cSEd Maste 	Dwarf_Error de;
60172c23cb7cSEd Maste 	const char *op_str;
6018cf781b2eSEd Maste 	char unk_op[32];
60192c23cb7cSEd Maste 	int i;
60202c23cb7cSEd Maste 
60212c23cb7cSEd Maste 	if (dwarf_expand_frame_instructions(cie, insts, len, &oplist,
60222c23cb7cSEd Maste 	    &opcnt, &de) != DW_DLV_OK) {
60232c23cb7cSEd Maste 		warnx("dwarf_expand_frame_instructions failed: %s",
60242c23cb7cSEd Maste 		    dwarf_errmsg(de));
60252c23cb7cSEd Maste 		return;
60262c23cb7cSEd Maste 	}
60272c23cb7cSEd Maste 
60282c23cb7cSEd Maste 	for (i = 0; i < opcnt; i++) {
60292c23cb7cSEd Maste 		if (oplist[i].fp_base_op != 0)
60302c23cb7cSEd Maste 			op = oplist[i].fp_base_op << 6;
60312c23cb7cSEd Maste 		else
60322c23cb7cSEd Maste 			op = oplist[i].fp_extended_op;
60332c23cb7cSEd Maste 		if (dwarf_get_CFA_name(op, &op_str) != DW_DLV_OK) {
6034cf781b2eSEd Maste 			snprintf(unk_op, sizeof(unk_op), "[Unknown CFA: %#x]",
6035cf781b2eSEd Maste 			    op);
6036cf781b2eSEd Maste 			op_str = unk_op;
60372c23cb7cSEd Maste 		}
60382c23cb7cSEd Maste 		printf("  %s", op_str);
60392c23cb7cSEd Maste 		switch (op) {
60402c23cb7cSEd Maste 		case DW_CFA_advance_loc:
60412c23cb7cSEd Maste 			delta = oplist[i].fp_offset * caf;
60422c23cb7cSEd Maste 			pc += delta;
60432c23cb7cSEd Maste 			printf(": %ju to %08jx", (uintmax_t) delta,
60442c23cb7cSEd Maste 			    (uintmax_t) pc);
60452c23cb7cSEd Maste 			break;
60462c23cb7cSEd Maste 		case DW_CFA_offset:
60472c23cb7cSEd Maste 		case DW_CFA_offset_extended:
60482c23cb7cSEd Maste 		case DW_CFA_offset_extended_sf:
60492c23cb7cSEd Maste 			delta = oplist[i].fp_offset * daf;
6050cf781b2eSEd Maste 			printf(": r%u (%s) at cfa%+jd", oplist[i].fp_register,
6051cf781b2eSEd Maste 			    dwarf_regname(re, oplist[i].fp_register),
60522c23cb7cSEd Maste 			    (intmax_t) delta);
60532c23cb7cSEd Maste 			break;
60542c23cb7cSEd Maste 		case DW_CFA_restore:
6055cf781b2eSEd Maste 			printf(": r%u (%s)", oplist[i].fp_register,
6056cf781b2eSEd Maste 			    dwarf_regname(re, oplist[i].fp_register));
60572c23cb7cSEd Maste 			break;
60582c23cb7cSEd Maste 		case DW_CFA_set_loc:
60592c23cb7cSEd Maste 			pc = oplist[i].fp_offset;
60602c23cb7cSEd Maste 			printf(": to %08jx", (uintmax_t) pc);
60612c23cb7cSEd Maste 			break;
60622c23cb7cSEd Maste 		case DW_CFA_advance_loc1:
60632c23cb7cSEd Maste 		case DW_CFA_advance_loc2:
60642c23cb7cSEd Maste 		case DW_CFA_advance_loc4:
60652c23cb7cSEd Maste 			pc += oplist[i].fp_offset;
60662c23cb7cSEd Maste 			printf(": %jd to %08jx", (intmax_t) oplist[i].fp_offset,
60672c23cb7cSEd Maste 			    (uintmax_t) pc);
60682c23cb7cSEd Maste 			break;
60692c23cb7cSEd Maste 		case DW_CFA_def_cfa:
6070cf781b2eSEd Maste 			printf(": r%u (%s) ofs %ju", oplist[i].fp_register,
6071cf781b2eSEd Maste 			    dwarf_regname(re, oplist[i].fp_register),
60722c23cb7cSEd Maste 			    (uintmax_t) oplist[i].fp_offset);
60732c23cb7cSEd Maste 			break;
60742c23cb7cSEd Maste 		case DW_CFA_def_cfa_sf:
6075cf781b2eSEd Maste 			printf(": r%u (%s) ofs %jd", oplist[i].fp_register,
6076cf781b2eSEd Maste 			    dwarf_regname(re, oplist[i].fp_register),
60772c23cb7cSEd Maste 			    (intmax_t) (oplist[i].fp_offset * daf));
60782c23cb7cSEd Maste 			break;
60792c23cb7cSEd Maste 		case DW_CFA_def_cfa_register:
6080cf781b2eSEd Maste 			printf(": r%u (%s)", oplist[i].fp_register,
6081cf781b2eSEd Maste 			    dwarf_regname(re, oplist[i].fp_register));
60822c23cb7cSEd Maste 			break;
60832c23cb7cSEd Maste 		case DW_CFA_def_cfa_offset:
60842c23cb7cSEd Maste 			printf(": %ju", (uintmax_t) oplist[i].fp_offset);
60852c23cb7cSEd Maste 			break;
60862c23cb7cSEd Maste 		case DW_CFA_def_cfa_offset_sf:
60872c23cb7cSEd Maste 			printf(": %jd", (intmax_t) (oplist[i].fp_offset * daf));
60882c23cb7cSEd Maste 			break;
60892c23cb7cSEd Maste 		default:
60902c23cb7cSEd Maste 			break;
60912c23cb7cSEd Maste 		}
60922c23cb7cSEd Maste 		putchar('\n');
60932c23cb7cSEd Maste 	}
60942c23cb7cSEd Maste 
60952c23cb7cSEd Maste 	dwarf_dealloc(dbg, oplist, DW_DLA_FRAME_BLOCK);
60962c23cb7cSEd Maste }
60972c23cb7cSEd Maste 
60982c23cb7cSEd Maste static char *
get_regoff_str(struct readelf * re,Dwarf_Half reg,Dwarf_Addr off)6099cf781b2eSEd Maste get_regoff_str(struct readelf *re, Dwarf_Half reg, Dwarf_Addr off)
61002c23cb7cSEd Maste {
61012c23cb7cSEd Maste 	static char rs[16];
61022c23cb7cSEd Maste 
61032c23cb7cSEd Maste 	if (reg == DW_FRAME_UNDEFINED_VAL || reg == DW_FRAME_REG_INITIAL_VALUE)
61042c23cb7cSEd Maste 		snprintf(rs, sizeof(rs), "%c", 'u');
61052c23cb7cSEd Maste 	else if (reg == DW_FRAME_CFA_COL)
61062c23cb7cSEd Maste 		snprintf(rs, sizeof(rs), "c%+jd", (intmax_t) off);
61072c23cb7cSEd Maste 	else
6108cf781b2eSEd Maste 		snprintf(rs, sizeof(rs), "%s%+jd", dwarf_regname(re, reg),
6109cf781b2eSEd Maste 		    (intmax_t) off);
61102c23cb7cSEd Maste 
61112c23cb7cSEd Maste 	return (rs);
61122c23cb7cSEd Maste }
61132c23cb7cSEd Maste 
61142c23cb7cSEd Maste static int
dump_dwarf_frame_regtable(struct readelf * re,Dwarf_Fde fde,Dwarf_Addr pc,Dwarf_Unsigned func_len,Dwarf_Half cie_ra)6115cf781b2eSEd Maste dump_dwarf_frame_regtable(struct readelf *re, Dwarf_Fde fde, Dwarf_Addr pc,
6116cf781b2eSEd Maste     Dwarf_Unsigned func_len, Dwarf_Half cie_ra)
61172c23cb7cSEd Maste {
61182c23cb7cSEd Maste 	Dwarf_Regtable rt;
61192c23cb7cSEd Maste 	Dwarf_Addr row_pc, end_pc, pre_pc, cur_pc;
61202c23cb7cSEd Maste 	Dwarf_Error de;
61212c23cb7cSEd Maste 	char *vec;
61222c23cb7cSEd Maste 	int i;
61232c23cb7cSEd Maste 
61242c23cb7cSEd Maste #define BIT_SET(v, n) (v[(n)>>3] |= 1U << ((n) & 7))
61252c23cb7cSEd Maste #define BIT_CLR(v, n) (v[(n)>>3] &= ~(1U << ((n) & 7)))
61262c23cb7cSEd Maste #define BIT_ISSET(v, n) (v[(n)>>3] & (1U << ((n) & 7)))
61272c23cb7cSEd Maste #define	RT(x) rt.rules[(x)]
61282c23cb7cSEd Maste 
61292c23cb7cSEd Maste 	vec = calloc((DW_REG_TABLE_SIZE + 7) / 8, 1);
61302c23cb7cSEd Maste 	if (vec == NULL)
61312c23cb7cSEd Maste 		err(EXIT_FAILURE, "calloc failed");
61322c23cb7cSEd Maste 
61332c23cb7cSEd Maste 	pre_pc = ~((Dwarf_Addr) 0);
61342c23cb7cSEd Maste 	cur_pc = pc;
61352c23cb7cSEd Maste 	end_pc = pc + func_len;
61362c23cb7cSEd Maste 	for (; cur_pc < end_pc; cur_pc++) {
61372c23cb7cSEd Maste 		if (dwarf_get_fde_info_for_all_regs(fde, cur_pc, &rt, &row_pc,
61382c23cb7cSEd Maste 		    &de) != DW_DLV_OK) {
613985642eeeSMark Johnston 			free(vec);
61402c23cb7cSEd Maste 			warnx("dwarf_get_fde_info_for_all_regs failed: %s\n",
61412c23cb7cSEd Maste 			    dwarf_errmsg(de));
61422c23cb7cSEd Maste 			return (-1);
61432c23cb7cSEd Maste 		}
61442c23cb7cSEd Maste 		if (row_pc == pre_pc)
61452c23cb7cSEd Maste 			continue;
61462c23cb7cSEd Maste 		pre_pc = row_pc;
61472c23cb7cSEd Maste 		for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
61482c23cb7cSEd Maste 			if (rt.rules[i].dw_regnum != DW_FRAME_REG_INITIAL_VALUE)
61492c23cb7cSEd Maste 				BIT_SET(vec, i);
61502c23cb7cSEd Maste 		}
61512c23cb7cSEd Maste 	}
61522c23cb7cSEd Maste 
61532c23cb7cSEd Maste 	printf("   LOC   CFA      ");
61542c23cb7cSEd Maste 	for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
61552c23cb7cSEd Maste 		if (BIT_ISSET(vec, i)) {
61562c23cb7cSEd Maste 			if ((Dwarf_Half) i == cie_ra)
61572c23cb7cSEd Maste 				printf("ra   ");
6158cf781b2eSEd Maste 			else
6159cf781b2eSEd Maste 				printf("%-5s",
6160cf781b2eSEd Maste 				    dwarf_regname(re, (unsigned int) i));
61612c23cb7cSEd Maste 		}
61622c23cb7cSEd Maste 	}
61632c23cb7cSEd Maste 	putchar('\n');
61642c23cb7cSEd Maste 
61652c23cb7cSEd Maste 	pre_pc = ~((Dwarf_Addr) 0);
61662c23cb7cSEd Maste 	cur_pc = pc;
61672c23cb7cSEd Maste 	end_pc = pc + func_len;
61682c23cb7cSEd Maste 	for (; cur_pc < end_pc; cur_pc++) {
61692c23cb7cSEd Maste 		if (dwarf_get_fde_info_for_all_regs(fde, cur_pc, &rt, &row_pc,
61702c23cb7cSEd Maste 		    &de) != DW_DLV_OK) {
6171d5e7add6SMark Johnston 			free(vec);
61722c23cb7cSEd Maste 			warnx("dwarf_get_fde_info_for_all_regs failed: %s\n",
61732c23cb7cSEd Maste 			    dwarf_errmsg(de));
61742c23cb7cSEd Maste 			return (-1);
61752c23cb7cSEd Maste 		}
61762c23cb7cSEd Maste 		if (row_pc == pre_pc)
61772c23cb7cSEd Maste 			continue;
61782c23cb7cSEd Maste 		pre_pc = row_pc;
61792c23cb7cSEd Maste 		printf("%08jx ", (uintmax_t) row_pc);
6180cf781b2eSEd Maste 		printf("%-8s ", get_regoff_str(re, RT(0).dw_regnum,
61812c23cb7cSEd Maste 		    RT(0).dw_offset));
61822c23cb7cSEd Maste 		for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
61832c23cb7cSEd Maste 			if (BIT_ISSET(vec, i)) {
6184cf781b2eSEd Maste 				printf("%-5s", get_regoff_str(re,
6185cf781b2eSEd Maste 				    RT(i).dw_regnum, RT(i).dw_offset));
61862c23cb7cSEd Maste 			}
61872c23cb7cSEd Maste 		}
61882c23cb7cSEd Maste 		putchar('\n');
61892c23cb7cSEd Maste 	}
61902c23cb7cSEd Maste 
61912c23cb7cSEd Maste 	free(vec);
61922c23cb7cSEd Maste 
61932c23cb7cSEd Maste 	return (0);
61942c23cb7cSEd Maste 
61952c23cb7cSEd Maste #undef	BIT_SET
61962c23cb7cSEd Maste #undef	BIT_CLR
61972c23cb7cSEd Maste #undef	BIT_ISSET
61982c23cb7cSEd Maste #undef	RT
61992c23cb7cSEd Maste }
62002c23cb7cSEd Maste 
62012c23cb7cSEd Maste static void
dump_dwarf_frame_section(struct readelf * re,struct section * s,int alt)62022c23cb7cSEd Maste dump_dwarf_frame_section(struct readelf *re, struct section *s, int alt)
62032c23cb7cSEd Maste {
62042c23cb7cSEd Maste 	Dwarf_Cie *cie_list, cie, pre_cie;
62052c23cb7cSEd Maste 	Dwarf_Fde *fde_list, fde;
62062c23cb7cSEd Maste 	Dwarf_Off cie_offset, fde_offset;
62072c23cb7cSEd Maste 	Dwarf_Unsigned cie_length, fde_instlen;
62082c23cb7cSEd Maste 	Dwarf_Unsigned cie_caf, cie_daf, cie_instlen, func_len, fde_length;
62092c23cb7cSEd Maste 	Dwarf_Signed cie_count, fde_count, cie_index;
62102c23cb7cSEd Maste 	Dwarf_Addr low_pc;
62112c23cb7cSEd Maste 	Dwarf_Half cie_ra;
62122c23cb7cSEd Maste 	Dwarf_Small cie_version;
62132c23cb7cSEd Maste 	Dwarf_Ptr fde_addr, fde_inst, cie_inst;
62142c23cb7cSEd Maste 	char *cie_aug, c;
6215cec2d0b1SChristos Margiolis 	int i, ret, eh_frame;
62162c23cb7cSEd Maste 	Dwarf_Error de;
62172c23cb7cSEd Maste 
62182c23cb7cSEd Maste 	printf("\nThe section %s contains:\n\n", s->name);
62192c23cb7cSEd Maste 
62202c23cb7cSEd Maste 	if (!strcmp(s->name, ".debug_frame")) {
62212c23cb7cSEd Maste 		eh_frame = 0;
62222c23cb7cSEd Maste 		if (dwarf_get_fde_list(re->dbg, &cie_list, &cie_count,
62232c23cb7cSEd Maste 		    &fde_list, &fde_count, &de) != DW_DLV_OK) {
62242c23cb7cSEd Maste 			warnx("dwarf_get_fde_list failed: %s",
62252c23cb7cSEd Maste 			    dwarf_errmsg(de));
62262c23cb7cSEd Maste 			return;
62272c23cb7cSEd Maste 		}
62282c23cb7cSEd Maste 	} else if (!strcmp(s->name, ".eh_frame")) {
62292c23cb7cSEd Maste 		eh_frame = 1;
6230cec2d0b1SChristos Margiolis 		ret = dwarf_get_fde_list_eh(re->dbg, &cie_list, &cie_count,
6231cec2d0b1SChristos Margiolis 		    &fde_list, &fde_count, &de);
6232cec2d0b1SChristos Margiolis 		if (ret != DW_DLV_OK) {
6233cec2d0b1SChristos Margiolis 			if (ret == DW_DLV_ERROR) {
62342c23cb7cSEd Maste 				warnx("dwarf_get_fde_list_eh failed: %s",
62352c23cb7cSEd Maste 				    dwarf_errmsg(de));
6236cec2d0b1SChristos Margiolis 			}
62372c23cb7cSEd Maste 			return;
62382c23cb7cSEd Maste 		}
62392c23cb7cSEd Maste 	} else
62402c23cb7cSEd Maste 		return;
62412c23cb7cSEd Maste 
62422c23cb7cSEd Maste 	pre_cie = NULL;
62432c23cb7cSEd Maste 	for (i = 0; i < fde_count; i++) {
62442c23cb7cSEd Maste 		if (dwarf_get_fde_n(fde_list, i, &fde, &de) != DW_DLV_OK) {
62452c23cb7cSEd Maste 			warnx("dwarf_get_fde_n failed: %s", dwarf_errmsg(de));
62462c23cb7cSEd Maste 			continue;
62472c23cb7cSEd Maste 		}
62482c23cb7cSEd Maste 		if (dwarf_get_cie_of_fde(fde, &cie, &de) != DW_DLV_OK) {
62492c23cb7cSEd Maste 			warnx("dwarf_get_fde_n failed: %s", dwarf_errmsg(de));
62502c23cb7cSEd Maste 			continue;
62512c23cb7cSEd Maste 		}
62522c23cb7cSEd Maste 		if (dwarf_get_fde_range(fde, &low_pc, &func_len, &fde_addr,
62532c23cb7cSEd Maste 		    &fde_length, &cie_offset, &cie_index, &fde_offset,
62542c23cb7cSEd Maste 		    &de) != DW_DLV_OK) {
62552c23cb7cSEd Maste 			warnx("dwarf_get_fde_range failed: %s",
62562c23cb7cSEd Maste 			    dwarf_errmsg(de));
62572c23cb7cSEd Maste 			continue;
62582c23cb7cSEd Maste 		}
62592c23cb7cSEd Maste 		if (dwarf_get_fde_instr_bytes(fde, &fde_inst, &fde_instlen,
62602c23cb7cSEd Maste 		    &de) != DW_DLV_OK) {
62612c23cb7cSEd Maste 			warnx("dwarf_get_fde_instr_bytes failed: %s",
62622c23cb7cSEd Maste 			    dwarf_errmsg(de));
62632c23cb7cSEd Maste 			continue;
62642c23cb7cSEd Maste 		}
62652c23cb7cSEd Maste 		if (pre_cie == NULL || cie != pre_cie) {
62662c23cb7cSEd Maste 			pre_cie = cie;
62672c23cb7cSEd Maste 			if (dwarf_get_cie_info(cie, &cie_length, &cie_version,
62682c23cb7cSEd Maste 			    &cie_aug, &cie_caf, &cie_daf, &cie_ra,
62692c23cb7cSEd Maste 			    &cie_inst, &cie_instlen, &de) != DW_DLV_OK) {
62702c23cb7cSEd Maste 				warnx("dwarf_get_cie_info failed: %s",
62712c23cb7cSEd Maste 				    dwarf_errmsg(de));
62722c23cb7cSEd Maste 				continue;
62732c23cb7cSEd Maste 			}
62742c23cb7cSEd Maste 			printf("%08jx %08jx %8.8jx CIE",
62752c23cb7cSEd Maste 			    (uintmax_t) cie_offset,
62762c23cb7cSEd Maste 			    (uintmax_t) cie_length,
62772c23cb7cSEd Maste 			    (uintmax_t) (eh_frame ? 0 : ~0U));
62782c23cb7cSEd Maste 			if (!alt) {
62792c23cb7cSEd Maste 				putchar('\n');
62802c23cb7cSEd Maste 				printf("  Version:\t\t\t%u\n", cie_version);
62812c23cb7cSEd Maste 				printf("  Augmentation:\t\t\t\"");
62822c23cb7cSEd Maste 				while ((c = *cie_aug++) != '\0')
62832c23cb7cSEd Maste 					putchar(c);
62842c23cb7cSEd Maste 				printf("\"\n");
62852c23cb7cSEd Maste 				printf("  Code alignment factor:\t%ju\n",
62862c23cb7cSEd Maste 				    (uintmax_t) cie_caf);
62872c23cb7cSEd Maste 				printf("  Data alignment factor:\t%jd\n",
62882c23cb7cSEd Maste 				    (intmax_t) cie_daf);
62892c23cb7cSEd Maste 				printf("  Return address column:\t%ju\n",
62902c23cb7cSEd Maste 				    (uintmax_t) cie_ra);
62912c23cb7cSEd Maste 				putchar('\n');
6292cf781b2eSEd Maste 				dump_dwarf_frame_inst(re, cie, cie_inst,
62932c23cb7cSEd Maste 				    cie_instlen, cie_caf, cie_daf, 0,
62942c23cb7cSEd Maste 				    re->dbg);
62952c23cb7cSEd Maste 				putchar('\n');
62962c23cb7cSEd Maste 			} else {
62972c23cb7cSEd Maste 				printf(" \"");
62982c23cb7cSEd Maste 				while ((c = *cie_aug++) != '\0')
62992c23cb7cSEd Maste 					putchar(c);
63002c23cb7cSEd Maste 				putchar('"');
63012c23cb7cSEd Maste 				printf(" cf=%ju df=%jd ra=%ju\n",
63022c23cb7cSEd Maste 				    (uintmax_t) cie_caf,
63032c23cb7cSEd Maste 				    (uintmax_t) cie_daf,
63042c23cb7cSEd Maste 				    (uintmax_t) cie_ra);
6305cf781b2eSEd Maste 				dump_dwarf_frame_regtable(re, fde, low_pc, 1,
63062c23cb7cSEd Maste 				    cie_ra);
63072c23cb7cSEd Maste 				putchar('\n');
63082c23cb7cSEd Maste 			}
63092c23cb7cSEd Maste 		}
63102c23cb7cSEd Maste 		printf("%08jx %08jx %08jx FDE cie=%08jx pc=%08jx..%08jx\n",
63112c23cb7cSEd Maste 		    (uintmax_t) fde_offset, (uintmax_t) fde_length,
63122c23cb7cSEd Maste 		    (uintmax_t) cie_offset,
63132c23cb7cSEd Maste 		    (uintmax_t) (eh_frame ? fde_offset + 4 - cie_offset :
63142c23cb7cSEd Maste 			cie_offset),
63152c23cb7cSEd Maste 		    (uintmax_t) low_pc, (uintmax_t) (low_pc + func_len));
63162c23cb7cSEd Maste 		if (!alt)
6317cf781b2eSEd Maste 			dump_dwarf_frame_inst(re, cie, fde_inst, fde_instlen,
63182c23cb7cSEd Maste 			    cie_caf, cie_daf, low_pc, re->dbg);
63192c23cb7cSEd Maste 		else
6320cf781b2eSEd Maste 			dump_dwarf_frame_regtable(re, fde, low_pc, func_len,
63212c23cb7cSEd Maste 			    cie_ra);
63222c23cb7cSEd Maste 		putchar('\n');
63232c23cb7cSEd Maste 	}
63242c23cb7cSEd Maste }
63252c23cb7cSEd Maste 
63262c23cb7cSEd Maste static void
dump_dwarf_frame(struct readelf * re,int alt)63272c23cb7cSEd Maste dump_dwarf_frame(struct readelf *re, int alt)
63282c23cb7cSEd Maste {
63292c23cb7cSEd Maste 	struct section *s;
63302c23cb7cSEd Maste 	int i;
63312c23cb7cSEd Maste 
63322c23cb7cSEd Maste 	(void) dwarf_set_frame_cfa_value(re->dbg, DW_FRAME_CFA_COL);
63332c23cb7cSEd Maste 
63342c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
63352c23cb7cSEd Maste 		s = &re->sl[i];
63362c23cb7cSEd Maste 		if (s->name != NULL && (!strcmp(s->name, ".debug_frame") ||
63372c23cb7cSEd Maste 		    !strcmp(s->name, ".eh_frame")))
63382c23cb7cSEd Maste 			dump_dwarf_frame_section(re, s, alt);
63392c23cb7cSEd Maste 	}
63402c23cb7cSEd Maste }
63412c23cb7cSEd Maste 
63422c23cb7cSEd Maste static void
dump_dwarf_str(struct readelf * re)63432c23cb7cSEd Maste dump_dwarf_str(struct readelf *re)
63442c23cb7cSEd Maste {
63452c23cb7cSEd Maste 	struct section *s;
63462c23cb7cSEd Maste 	Elf_Data *d;
63472c23cb7cSEd Maste 	unsigned char *p;
63482c23cb7cSEd Maste 	int elferr, end, i, j;
63492c23cb7cSEd Maste 
63502c23cb7cSEd Maste 	printf("\nContents of section .debug_str:\n");
63512c23cb7cSEd Maste 
63522c23cb7cSEd Maste 	s = NULL;
63532c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
63542c23cb7cSEd Maste 		s = &re->sl[i];
63552c23cb7cSEd Maste 		if (s->name != NULL && !strcmp(s->name, ".debug_str"))
63562c23cb7cSEd Maste 			break;
63572c23cb7cSEd Maste 	}
63582c23cb7cSEd Maste 	if ((size_t) i >= re->shnum)
63592c23cb7cSEd Maste 		return;
63602c23cb7cSEd Maste 
63612c23cb7cSEd Maste 	(void) elf_errno();
63622c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
63632c23cb7cSEd Maste 		elferr = elf_errno();
63642c23cb7cSEd Maste 		if (elferr != 0)
63652c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
63662c23cb7cSEd Maste 		return;
63672c23cb7cSEd Maste 	}
63682c23cb7cSEd Maste 	if (d->d_size <= 0)
63692c23cb7cSEd Maste 		return;
63702c23cb7cSEd Maste 
63712c23cb7cSEd Maste 	for (i = 0, p = d->d_buf; (size_t) i < d->d_size; i += 16) {
63722c23cb7cSEd Maste 		printf("  0x%08x", (unsigned int) i);
63732c23cb7cSEd Maste 		if ((size_t) i + 16 > d->d_size)
63742c23cb7cSEd Maste 			end = d->d_size;
63752c23cb7cSEd Maste 		else
63762c23cb7cSEd Maste 			end = i + 16;
63772c23cb7cSEd Maste 		for (j = i; j < i + 16; j++) {
63782c23cb7cSEd Maste 			if ((j - i) % 4 == 0)
63792c23cb7cSEd Maste 				putchar(' ');
63802c23cb7cSEd Maste 			if (j >= end) {
63812c23cb7cSEd Maste 				printf("  ");
63822c23cb7cSEd Maste 				continue;
63832c23cb7cSEd Maste 			}
63842c23cb7cSEd Maste 			printf("%02x", (uint8_t) p[j]);
63852c23cb7cSEd Maste 		}
63862c23cb7cSEd Maste 		putchar(' ');
63872c23cb7cSEd Maste 		for (j = i; j < end; j++) {
63882c23cb7cSEd Maste 			if (isprint(p[j]))
63892c23cb7cSEd Maste 				putchar(p[j]);
63902c23cb7cSEd Maste 			else if (p[j] == 0)
63912c23cb7cSEd Maste 				putchar('.');
63922c23cb7cSEd Maste 			else
63932c23cb7cSEd Maste 				putchar(' ');
63942c23cb7cSEd Maste 		}
63952c23cb7cSEd Maste 		putchar('\n');
63962c23cb7cSEd Maste 	}
63972c23cb7cSEd Maste }
63982c23cb7cSEd Maste 
63999817bae9SEd Maste static int
loc_at_comparator(const void * la1,const void * la2)64009817bae9SEd Maste loc_at_comparator(const void *la1, const void *la2)
64019817bae9SEd Maste {
64029817bae9SEd Maste 	const struct loc_at *left, *right;
64032c23cb7cSEd Maste 
64049817bae9SEd Maste 	left = (const struct loc_at *)la1;
64059817bae9SEd Maste 	right = (const struct loc_at *)la2;
64069817bae9SEd Maste 
64079817bae9SEd Maste 	if (left->la_off > right->la_off)
64089817bae9SEd Maste 		return (1);
64099817bae9SEd Maste 	else if (left->la_off < right->la_off)
64109817bae9SEd Maste 		return (-1);
64119817bae9SEd Maste 	else
64129817bae9SEd Maste 		return (0);
64139817bae9SEd Maste }
64142c23cb7cSEd Maste 
64152c23cb7cSEd Maste static void
search_loclist_at(struct readelf * re,Dwarf_Die die,Dwarf_Unsigned lowpc,struct loc_at ** la_list,size_t * la_list_len,size_t * la_list_cap)64169817bae9SEd Maste search_loclist_at(struct readelf *re, Dwarf_Die die, Dwarf_Unsigned lowpc,
6417cb12a690SEd Maste     struct loc_at **la_list, size_t *la_list_len, size_t *la_list_cap)
64182c23cb7cSEd Maste {
64199817bae9SEd Maste 	struct loc_at *la;
64202c23cb7cSEd Maste 	Dwarf_Attribute *attr_list;
64212c23cb7cSEd Maste 	Dwarf_Die ret_die;
64222c23cb7cSEd Maste 	Dwarf_Unsigned off;
6423cf781b2eSEd Maste 	Dwarf_Off ref;
64242c23cb7cSEd Maste 	Dwarf_Signed attr_count;
64252c23cb7cSEd Maste 	Dwarf_Half attr, form;
6426cf781b2eSEd Maste 	Dwarf_Bool is_info;
64272c23cb7cSEd Maste 	Dwarf_Error de;
64282c23cb7cSEd Maste 	int i, ret;
64292c23cb7cSEd Maste 
6430cf781b2eSEd Maste 	is_info = dwarf_get_die_infotypes_flag(die);
6431cf781b2eSEd Maste 
64322c23cb7cSEd Maste 	if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
64332c23cb7cSEd Maste 	    DW_DLV_OK) {
64342c23cb7cSEd Maste 		if (ret == DW_DLV_ERROR)
64352c23cb7cSEd Maste 			warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
64362c23cb7cSEd Maste 		goto cont_search;
64372c23cb7cSEd Maste 	}
64382c23cb7cSEd Maste 	for (i = 0; i < attr_count; i++) {
64392c23cb7cSEd Maste 		if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
64402c23cb7cSEd Maste 			warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
64412c23cb7cSEd Maste 			continue;
64422c23cb7cSEd Maste 		}
64432c23cb7cSEd Maste 		if (attr != DW_AT_location &&
64442c23cb7cSEd Maste 		    attr != DW_AT_string_length &&
64452c23cb7cSEd Maste 		    attr != DW_AT_return_addr &&
64462c23cb7cSEd Maste 		    attr != DW_AT_data_member_location &&
64472c23cb7cSEd Maste 		    attr != DW_AT_frame_base &&
64482c23cb7cSEd Maste 		    attr != DW_AT_segment &&
64492c23cb7cSEd Maste 		    attr != DW_AT_static_link &&
64502c23cb7cSEd Maste 		    attr != DW_AT_use_location &&
64512c23cb7cSEd Maste 		    attr != DW_AT_vtable_elem_location)
64522c23cb7cSEd Maste 			continue;
64532c23cb7cSEd Maste 		if (dwarf_whatform(attr_list[i], &form, &de) != DW_DLV_OK) {
64542c23cb7cSEd Maste 			warnx("dwarf_whatform failed: %s", dwarf_errmsg(de));
64552c23cb7cSEd Maste 			continue;
64562c23cb7cSEd Maste 		}
6457cf781b2eSEd Maste 		if (form == DW_FORM_data4 || form == DW_FORM_data8) {
6458cf781b2eSEd Maste 			if (dwarf_formudata(attr_list[i], &off, &de) !=
6459cf781b2eSEd Maste 			    DW_DLV_OK) {
6460cf781b2eSEd Maste 				warnx("dwarf_formudata failed: %s",
6461cf781b2eSEd Maste 				    dwarf_errmsg(de));
64622c23cb7cSEd Maste 				continue;
64632c23cb7cSEd Maste 			}
6464cf781b2eSEd Maste 		} else if (form == DW_FORM_sec_offset) {
6465cf781b2eSEd Maste 			if (dwarf_global_formref(attr_list[i], &ref, &de) !=
6466cf781b2eSEd Maste 			    DW_DLV_OK) {
6467cf781b2eSEd Maste 				warnx("dwarf_global_formref failed: %s",
6468cf781b2eSEd Maste 				    dwarf_errmsg(de));
6469cf781b2eSEd Maste 				continue;
6470cf781b2eSEd Maste 			}
6471cf781b2eSEd Maste 			off = ref;
6472cf781b2eSEd Maste 		} else
6473cf781b2eSEd Maste 			continue;
6474cf781b2eSEd Maste 
64759817bae9SEd Maste 		if (*la_list_cap == *la_list_len) {
64769817bae9SEd Maste 			*la_list = realloc(*la_list,
64779817bae9SEd Maste 			    *la_list_cap * 2 * sizeof(**la_list));
647837fa1df2SMark Johnston 			if (*la_list == NULL)
647937fa1df2SMark Johnston 				err(EXIT_FAILURE, "realloc failed");
64809817bae9SEd Maste 			*la_list_cap *= 2;
64812c23cb7cSEd Maste 		}
64829817bae9SEd Maste 		la = &((*la_list)[*la_list_len]);
64839817bae9SEd Maste 		la->la_at = attr_list[i];
64849817bae9SEd Maste 		la->la_off = off;
64859817bae9SEd Maste 		la->la_lowpc = lowpc;
64869817bae9SEd Maste 		la->la_cu_psize = re->cu_psize;
64879817bae9SEd Maste 		la->la_cu_osize = re->cu_osize;
64889817bae9SEd Maste 		la->la_cu_ver = re->cu_ver;
64899817bae9SEd Maste 		(*la_list_len)++;
64902c23cb7cSEd Maste 	}
64912c23cb7cSEd Maste 
64922c23cb7cSEd Maste cont_search:
64932c23cb7cSEd Maste 	/* Search children. */
64942c23cb7cSEd Maste 	ret = dwarf_child(die, &ret_die, &de);
64952c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
64962c23cb7cSEd Maste 		warnx("dwarf_child: %s", dwarf_errmsg(de));
64972c23cb7cSEd Maste 	else if (ret == DW_DLV_OK)
64989817bae9SEd Maste 		search_loclist_at(re, ret_die, lowpc, la_list,
64999817bae9SEd Maste 		    la_list_len, la_list_cap);
65002c23cb7cSEd Maste 
65012c23cb7cSEd Maste 	/* Search sibling. */
6502cf781b2eSEd Maste 	ret = dwarf_siblingof_b(re->dbg, die, &ret_die, is_info, &de);
65032c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
65042c23cb7cSEd Maste 		warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
65052c23cb7cSEd Maste 	else if (ret == DW_DLV_OK)
65069817bae9SEd Maste 		search_loclist_at(re, ret_die, lowpc, la_list,
65079817bae9SEd Maste 		    la_list_len, la_list_cap);
65082c23cb7cSEd Maste }
65092c23cb7cSEd Maste 
65102c23cb7cSEd Maste static void
dump_dwarf_loc(struct readelf * re,Dwarf_Loc * lr)6511cf781b2eSEd Maste dump_dwarf_loc(struct readelf *re, Dwarf_Loc *lr)
65122c23cb7cSEd Maste {
65132c23cb7cSEd Maste 	const char *op_str;
6514cf781b2eSEd Maste 	char unk_op[32];
6515cf781b2eSEd Maste 	uint8_t *b, n;
6516cf781b2eSEd Maste 	int i;
65172c23cb7cSEd Maste 
65182c23cb7cSEd Maste 	if (dwarf_get_OP_name(lr->lr_atom, &op_str) !=
65192c23cb7cSEd Maste 	    DW_DLV_OK) {
6520cf781b2eSEd Maste 		snprintf(unk_op, sizeof(unk_op),
6521cf781b2eSEd Maste 		    "[Unknown OP: %#x]", lr->lr_atom);
6522cf781b2eSEd Maste 		op_str = unk_op;
65232c23cb7cSEd Maste 	}
65242c23cb7cSEd Maste 
65252c23cb7cSEd Maste 	printf("%s", op_str);
65262c23cb7cSEd Maste 
65272c23cb7cSEd Maste 	switch (lr->lr_atom) {
65282c23cb7cSEd Maste 	case DW_OP_reg0:
65292c23cb7cSEd Maste 	case DW_OP_reg1:
65302c23cb7cSEd Maste 	case DW_OP_reg2:
65312c23cb7cSEd Maste 	case DW_OP_reg3:
65322c23cb7cSEd Maste 	case DW_OP_reg4:
65332c23cb7cSEd Maste 	case DW_OP_reg5:
65342c23cb7cSEd Maste 	case DW_OP_reg6:
65352c23cb7cSEd Maste 	case DW_OP_reg7:
65362c23cb7cSEd Maste 	case DW_OP_reg8:
65372c23cb7cSEd Maste 	case DW_OP_reg9:
65382c23cb7cSEd Maste 	case DW_OP_reg10:
65392c23cb7cSEd Maste 	case DW_OP_reg11:
65402c23cb7cSEd Maste 	case DW_OP_reg12:
65412c23cb7cSEd Maste 	case DW_OP_reg13:
65422c23cb7cSEd Maste 	case DW_OP_reg14:
65432c23cb7cSEd Maste 	case DW_OP_reg15:
65442c23cb7cSEd Maste 	case DW_OP_reg16:
65452c23cb7cSEd Maste 	case DW_OP_reg17:
65462c23cb7cSEd Maste 	case DW_OP_reg18:
65472c23cb7cSEd Maste 	case DW_OP_reg19:
65482c23cb7cSEd Maste 	case DW_OP_reg20:
65492c23cb7cSEd Maste 	case DW_OP_reg21:
65502c23cb7cSEd Maste 	case DW_OP_reg22:
65512c23cb7cSEd Maste 	case DW_OP_reg23:
65522c23cb7cSEd Maste 	case DW_OP_reg24:
65532c23cb7cSEd Maste 	case DW_OP_reg25:
65542c23cb7cSEd Maste 	case DW_OP_reg26:
65552c23cb7cSEd Maste 	case DW_OP_reg27:
65562c23cb7cSEd Maste 	case DW_OP_reg28:
65572c23cb7cSEd Maste 	case DW_OP_reg29:
65582c23cb7cSEd Maste 	case DW_OP_reg30:
65592c23cb7cSEd Maste 	case DW_OP_reg31:
6560cf781b2eSEd Maste 		printf(" (%s)", dwarf_regname(re, lr->lr_atom - DW_OP_reg0));
6561cf781b2eSEd Maste 		break;
6562cf781b2eSEd Maste 
6563cf781b2eSEd Maste 	case DW_OP_deref:
65642c23cb7cSEd Maste 	case DW_OP_lit0:
65652c23cb7cSEd Maste 	case DW_OP_lit1:
65662c23cb7cSEd Maste 	case DW_OP_lit2:
65672c23cb7cSEd Maste 	case DW_OP_lit3:
65682c23cb7cSEd Maste 	case DW_OP_lit4:
65692c23cb7cSEd Maste 	case DW_OP_lit5:
65702c23cb7cSEd Maste 	case DW_OP_lit6:
65712c23cb7cSEd Maste 	case DW_OP_lit7:
65722c23cb7cSEd Maste 	case DW_OP_lit8:
65732c23cb7cSEd Maste 	case DW_OP_lit9:
65742c23cb7cSEd Maste 	case DW_OP_lit10:
65752c23cb7cSEd Maste 	case DW_OP_lit11:
65762c23cb7cSEd Maste 	case DW_OP_lit12:
65772c23cb7cSEd Maste 	case DW_OP_lit13:
65782c23cb7cSEd Maste 	case DW_OP_lit14:
65792c23cb7cSEd Maste 	case DW_OP_lit15:
65802c23cb7cSEd Maste 	case DW_OP_lit16:
65812c23cb7cSEd Maste 	case DW_OP_lit17:
65822c23cb7cSEd Maste 	case DW_OP_lit18:
65832c23cb7cSEd Maste 	case DW_OP_lit19:
65842c23cb7cSEd Maste 	case DW_OP_lit20:
65852c23cb7cSEd Maste 	case DW_OP_lit21:
65862c23cb7cSEd Maste 	case DW_OP_lit22:
65872c23cb7cSEd Maste 	case DW_OP_lit23:
65882c23cb7cSEd Maste 	case DW_OP_lit24:
65892c23cb7cSEd Maste 	case DW_OP_lit25:
65902c23cb7cSEd Maste 	case DW_OP_lit26:
65912c23cb7cSEd Maste 	case DW_OP_lit27:
65922c23cb7cSEd Maste 	case DW_OP_lit28:
65932c23cb7cSEd Maste 	case DW_OP_lit29:
65942c23cb7cSEd Maste 	case DW_OP_lit30:
65952c23cb7cSEd Maste 	case DW_OP_lit31:
65962c23cb7cSEd Maste 	case DW_OP_dup:
65972c23cb7cSEd Maste 	case DW_OP_drop:
65982c23cb7cSEd Maste 	case DW_OP_over:
65992c23cb7cSEd Maste 	case DW_OP_swap:
66002c23cb7cSEd Maste 	case DW_OP_rot:
66012c23cb7cSEd Maste 	case DW_OP_xderef:
66022c23cb7cSEd Maste 	case DW_OP_abs:
66032c23cb7cSEd Maste 	case DW_OP_and:
66042c23cb7cSEd Maste 	case DW_OP_div:
66052c23cb7cSEd Maste 	case DW_OP_minus:
66062c23cb7cSEd Maste 	case DW_OP_mod:
66072c23cb7cSEd Maste 	case DW_OP_mul:
66082c23cb7cSEd Maste 	case DW_OP_neg:
66092c23cb7cSEd Maste 	case DW_OP_not:
66102c23cb7cSEd Maste 	case DW_OP_or:
66112c23cb7cSEd Maste 	case DW_OP_plus:
66122c23cb7cSEd Maste 	case DW_OP_shl:
66132c23cb7cSEd Maste 	case DW_OP_shr:
66142c23cb7cSEd Maste 	case DW_OP_shra:
66152c23cb7cSEd Maste 	case DW_OP_xor:
66162c23cb7cSEd Maste 	case DW_OP_eq:
66172c23cb7cSEd Maste 	case DW_OP_ge:
66182c23cb7cSEd Maste 	case DW_OP_gt:
66192c23cb7cSEd Maste 	case DW_OP_le:
66202c23cb7cSEd Maste 	case DW_OP_lt:
66212c23cb7cSEd Maste 	case DW_OP_ne:
66222c23cb7cSEd Maste 	case DW_OP_nop:
6623cf781b2eSEd Maste 	case DW_OP_push_object_address:
6624cf781b2eSEd Maste 	case DW_OP_form_tls_address:
6625cf781b2eSEd Maste 	case DW_OP_call_frame_cfa:
6626cf781b2eSEd Maste 	case DW_OP_stack_value:
6627cf781b2eSEd Maste 	case DW_OP_GNU_push_tls_address:
6628cf781b2eSEd Maste 	case DW_OP_GNU_uninit:
66292c23cb7cSEd Maste 		break;
66302c23cb7cSEd Maste 
66312c23cb7cSEd Maste 	case DW_OP_const1u:
66322c23cb7cSEd Maste 	case DW_OP_pick:
66332c23cb7cSEd Maste 	case DW_OP_deref_size:
66342c23cb7cSEd Maste 	case DW_OP_xderef_size:
66352c23cb7cSEd Maste 	case DW_OP_const2u:
66362c23cb7cSEd Maste 	case DW_OP_bra:
66372c23cb7cSEd Maste 	case DW_OP_skip:
66382c23cb7cSEd Maste 	case DW_OP_const4u:
66392c23cb7cSEd Maste 	case DW_OP_const8u:
66402c23cb7cSEd Maste 	case DW_OP_constu:
66412c23cb7cSEd Maste 	case DW_OP_plus_uconst:
66422c23cb7cSEd Maste 	case DW_OP_regx:
66432c23cb7cSEd Maste 	case DW_OP_piece:
66442c23cb7cSEd Maste 		printf(": %ju", (uintmax_t)
66452c23cb7cSEd Maste 		    lr->lr_number);
66462c23cb7cSEd Maste 		break;
66472c23cb7cSEd Maste 
6648cf781b2eSEd Maste 	case DW_OP_const1s:
6649cf781b2eSEd Maste 	case DW_OP_const2s:
6650cf781b2eSEd Maste 	case DW_OP_const4s:
6651cf781b2eSEd Maste 	case DW_OP_const8s:
66522c23cb7cSEd Maste 	case DW_OP_consts:
6653cf781b2eSEd Maste 		printf(": %jd", (intmax_t)
6654cf781b2eSEd Maste 		    lr->lr_number);
6655cf781b2eSEd Maste 		break;
6656cf781b2eSEd Maste 
66572c23cb7cSEd Maste 	case DW_OP_breg0:
66582c23cb7cSEd Maste 	case DW_OP_breg1:
66592c23cb7cSEd Maste 	case DW_OP_breg2:
66602c23cb7cSEd Maste 	case DW_OP_breg3:
66612c23cb7cSEd Maste 	case DW_OP_breg4:
66622c23cb7cSEd Maste 	case DW_OP_breg5:
66632c23cb7cSEd Maste 	case DW_OP_breg6:
66642c23cb7cSEd Maste 	case DW_OP_breg7:
66652c23cb7cSEd Maste 	case DW_OP_breg8:
66662c23cb7cSEd Maste 	case DW_OP_breg9:
66672c23cb7cSEd Maste 	case DW_OP_breg10:
66682c23cb7cSEd Maste 	case DW_OP_breg11:
66692c23cb7cSEd Maste 	case DW_OP_breg12:
66702c23cb7cSEd Maste 	case DW_OP_breg13:
66712c23cb7cSEd Maste 	case DW_OP_breg14:
66722c23cb7cSEd Maste 	case DW_OP_breg15:
66732c23cb7cSEd Maste 	case DW_OP_breg16:
66742c23cb7cSEd Maste 	case DW_OP_breg17:
66752c23cb7cSEd Maste 	case DW_OP_breg18:
66762c23cb7cSEd Maste 	case DW_OP_breg19:
66772c23cb7cSEd Maste 	case DW_OP_breg20:
66782c23cb7cSEd Maste 	case DW_OP_breg21:
66792c23cb7cSEd Maste 	case DW_OP_breg22:
66802c23cb7cSEd Maste 	case DW_OP_breg23:
66812c23cb7cSEd Maste 	case DW_OP_breg24:
66822c23cb7cSEd Maste 	case DW_OP_breg25:
66832c23cb7cSEd Maste 	case DW_OP_breg26:
66842c23cb7cSEd Maste 	case DW_OP_breg27:
66852c23cb7cSEd Maste 	case DW_OP_breg28:
66862c23cb7cSEd Maste 	case DW_OP_breg29:
66872c23cb7cSEd Maste 	case DW_OP_breg30:
66882c23cb7cSEd Maste 	case DW_OP_breg31:
6689cf781b2eSEd Maste 		printf(" (%s): %jd",
6690cf781b2eSEd Maste 		    dwarf_regname(re, lr->lr_atom - DW_OP_breg0),
6691cf781b2eSEd Maste 		    (intmax_t) lr->lr_number);
6692cf781b2eSEd Maste 		break;
6693cf781b2eSEd Maste 
66942c23cb7cSEd Maste 	case DW_OP_fbreg:
66952c23cb7cSEd Maste 		printf(": %jd", (intmax_t)
66962c23cb7cSEd Maste 		    lr->lr_number);
66972c23cb7cSEd Maste 		break;
66982c23cb7cSEd Maste 
66992c23cb7cSEd Maste 	case DW_OP_bregx:
6700cf781b2eSEd Maste 		printf(": %ju (%s) %jd",
67012c23cb7cSEd Maste 		    (uintmax_t) lr->lr_number,
6702cf781b2eSEd Maste 		    dwarf_regname(re, (unsigned int) lr->lr_number),
67032c23cb7cSEd Maste 		    (intmax_t) lr->lr_number2);
67042c23cb7cSEd Maste 		break;
67052c23cb7cSEd Maste 
67062c23cb7cSEd Maste 	case DW_OP_addr:
6707cf781b2eSEd Maste 	case DW_OP_GNU_encoded_addr:
67082c23cb7cSEd Maste 		printf(": %#jx", (uintmax_t)
67092c23cb7cSEd Maste 		    lr->lr_number);
67102c23cb7cSEd Maste 		break;
6711cf781b2eSEd Maste 
6712cf781b2eSEd Maste 	case DW_OP_GNU_implicit_pointer:
6713cf781b2eSEd Maste 		printf(": <0x%jx> %jd", (uintmax_t) lr->lr_number,
6714cf781b2eSEd Maste 		    (intmax_t) lr->lr_number2);
6715cf781b2eSEd Maste 		break;
6716cf781b2eSEd Maste 
6717cf781b2eSEd Maste 	case DW_OP_implicit_value:
6718cf781b2eSEd Maste 		printf(": %ju byte block:", (uintmax_t) lr->lr_number);
6719cf781b2eSEd Maste 		b = (uint8_t *)(uintptr_t) lr->lr_number2;
6720cf781b2eSEd Maste 		for (i = 0; (Dwarf_Unsigned) i < lr->lr_number; i++)
6721cf781b2eSEd Maste 			printf(" %x", b[i]);
6722cf781b2eSEd Maste 		break;
6723cf781b2eSEd Maste 
6724cf781b2eSEd Maste 	case DW_OP_GNU_entry_value:
6725cf781b2eSEd Maste 		printf(": (");
6726cf781b2eSEd Maste 		dump_dwarf_block(re, (uint8_t *)(uintptr_t) lr->lr_number2,
6727cf781b2eSEd Maste 		    lr->lr_number);
6728cf781b2eSEd Maste 		putchar(')');
6729cf781b2eSEd Maste 		break;
6730cf781b2eSEd Maste 
6731cf781b2eSEd Maste 	case DW_OP_GNU_const_type:
6732cf781b2eSEd Maste 		printf(": <0x%jx> ", (uintmax_t) lr->lr_number);
6733cf781b2eSEd Maste 		b = (uint8_t *)(uintptr_t) lr->lr_number2;
6734cf781b2eSEd Maste 		n = *b;
6735cf781b2eSEd Maste 		for (i = 1; (uint8_t) i < n; i++)
6736cf781b2eSEd Maste 			printf(" %x", b[i]);
6737cf781b2eSEd Maste 		break;
6738cf781b2eSEd Maste 
6739cf781b2eSEd Maste 	case DW_OP_GNU_regval_type:
6740cf781b2eSEd Maste 		printf(": %ju (%s) <0x%jx>", (uintmax_t) lr->lr_number,
6741cf781b2eSEd Maste 		    dwarf_regname(re, (unsigned int) lr->lr_number),
6742cf781b2eSEd Maste 		    (uintmax_t) lr->lr_number2);
6743cf781b2eSEd Maste 		break;
6744cf781b2eSEd Maste 
6745cf781b2eSEd Maste 	case DW_OP_GNU_convert:
6746cf781b2eSEd Maste 	case DW_OP_GNU_deref_type:
6747cf781b2eSEd Maste 	case DW_OP_GNU_parameter_ref:
6748cf781b2eSEd Maste 	case DW_OP_GNU_reinterpret:
6749cf781b2eSEd Maste 		printf(": <0x%jx>", (uintmax_t) lr->lr_number);
6750cf781b2eSEd Maste 		break;
6751cf781b2eSEd Maste 
6752cf781b2eSEd Maste 	default:
6753cf781b2eSEd Maste 		break;
67542c23cb7cSEd Maste 	}
6755cf781b2eSEd Maste }
6756cf781b2eSEd Maste 
6757cf781b2eSEd Maste static void
dump_dwarf_block(struct readelf * re,uint8_t * b,Dwarf_Unsigned len)6758cf781b2eSEd Maste dump_dwarf_block(struct readelf *re, uint8_t *b, Dwarf_Unsigned len)
6759cf781b2eSEd Maste {
6760cf781b2eSEd Maste 	Dwarf_Locdesc *llbuf;
6761cf781b2eSEd Maste 	Dwarf_Signed lcnt;
6762cf781b2eSEd Maste 	Dwarf_Error de;
6763cf781b2eSEd Maste 	int i;
6764cf781b2eSEd Maste 
6765cf781b2eSEd Maste 	if (dwarf_loclist_from_expr_b(re->dbg, b, len, re->cu_psize,
6766cf781b2eSEd Maste 	    re->cu_osize, re->cu_ver, &llbuf, &lcnt, &de) != DW_DLV_OK) {
6767cf781b2eSEd Maste 		warnx("dwarf_loclist_form_expr_b: %s", dwarf_errmsg(de));
6768cf781b2eSEd Maste 		return;
6769cf781b2eSEd Maste 	}
6770cf781b2eSEd Maste 
6771cf781b2eSEd Maste 	for (i = 0; (Dwarf_Half) i < llbuf->ld_cents; i++) {
6772cf781b2eSEd Maste 		dump_dwarf_loc(re, &llbuf->ld_s[i]);
6773cf781b2eSEd Maste 		if (i < llbuf->ld_cents - 1)
6774cf781b2eSEd Maste 			printf("; ");
6775cf781b2eSEd Maste 	}
6776cf781b2eSEd Maste 
6777cf781b2eSEd Maste 	dwarf_dealloc(re->dbg, llbuf->ld_s, DW_DLA_LOC_BLOCK);
6778cf781b2eSEd Maste 	dwarf_dealloc(re->dbg, llbuf, DW_DLA_LOCDESC);
6779cf781b2eSEd Maste }
6780cf781b2eSEd Maste 
6781cf781b2eSEd Maste static void
dump_dwarf_loclist(struct readelf * re)6782cf781b2eSEd Maste dump_dwarf_loclist(struct readelf *re)
6783cf781b2eSEd Maste {
6784cf781b2eSEd Maste 	Dwarf_Die die;
6785cf781b2eSEd Maste 	Dwarf_Locdesc **llbuf;
6786cf781b2eSEd Maste 	Dwarf_Unsigned lowpc;
6787cf781b2eSEd Maste 	Dwarf_Signed lcnt;
6788cf781b2eSEd Maste 	Dwarf_Half tag, version, pointer_size, off_size;
6789cf781b2eSEd Maste 	Dwarf_Error de;
67909817bae9SEd Maste 	struct loc_at *la_list, *left, *right, *la;
6791cb12a690SEd Maste 	size_t la_list_len, la_list_cap;
6792cb12a690SEd Maste 	unsigned int duplicates, k;
6793bee2765cSEd Maste 	int i, j, ret, has_content;
6794cf781b2eSEd Maste 
67959817bae9SEd Maste 	la_list_len = 0;
67969817bae9SEd Maste 	la_list_cap = 200;
67979817bae9SEd Maste 	if ((la_list = calloc(la_list_cap, sizeof(struct loc_at))) == NULL)
67989817bae9SEd Maste 		errx(EXIT_FAILURE, "calloc failed");
6799cf781b2eSEd Maste 	/* Search .debug_info section. */
6800cf781b2eSEd Maste 	while ((ret = dwarf_next_cu_header_b(re->dbg, NULL, &version, NULL,
6801cf781b2eSEd Maste 	    &pointer_size, &off_size, NULL, NULL, &de)) == DW_DLV_OK) {
6802cf781b2eSEd Maste 		set_cu_context(re, pointer_size, off_size, version);
6803cf781b2eSEd Maste 		die = NULL;
6804cf781b2eSEd Maste 		if (dwarf_siblingof(re->dbg, die, &die, &de) != DW_DLV_OK)
6805cf781b2eSEd Maste 			continue;
6806cf781b2eSEd Maste 		if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
6807cf781b2eSEd Maste 			warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
6808cf781b2eSEd Maste 			continue;
6809cf781b2eSEd Maste 		}
6810cf781b2eSEd Maste 		/* XXX: What about DW_TAG_partial_unit? */
6811cf781b2eSEd Maste 		lowpc = 0;
6812cf781b2eSEd Maste 		if (tag == DW_TAG_compile_unit) {
6813cf781b2eSEd Maste 			if (dwarf_attrval_unsigned(die, DW_AT_low_pc,
6814cf781b2eSEd Maste 			    &lowpc, &de) != DW_DLV_OK)
6815cf781b2eSEd Maste 				lowpc = 0;
6816cf781b2eSEd Maste 		}
6817cf781b2eSEd Maste 
6818cf781b2eSEd Maste 		/* Search attributes for reference to .debug_loc section. */
68199817bae9SEd Maste 		search_loclist_at(re, die, lowpc, &la_list,
68209817bae9SEd Maste 		    &la_list_len, &la_list_cap);
6821cf781b2eSEd Maste 	}
6822cf781b2eSEd Maste 	if (ret == DW_DLV_ERROR)
6823cf781b2eSEd Maste 		warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
6824cf781b2eSEd Maste 
6825cf781b2eSEd Maste 	/* Search .debug_types section. */
6826cf781b2eSEd Maste 	do {
6827cf781b2eSEd Maste 		while ((ret = dwarf_next_cu_header_c(re->dbg, 0, NULL,
6828cf781b2eSEd Maste 		    &version, NULL, &pointer_size, &off_size, NULL, NULL,
6829cf781b2eSEd Maste 		    NULL, NULL, &de)) == DW_DLV_OK) {
6830cf781b2eSEd Maste 			set_cu_context(re, pointer_size, off_size, version);
6831cf781b2eSEd Maste 			die = NULL;
6832cf781b2eSEd Maste 			if (dwarf_siblingof(re->dbg, die, &die, &de) !=
6833cf781b2eSEd Maste 			    DW_DLV_OK)
6834cf781b2eSEd Maste 				continue;
6835cf781b2eSEd Maste 			if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
6836cf781b2eSEd Maste 				warnx("dwarf_tag failed: %s",
6837cf781b2eSEd Maste 				    dwarf_errmsg(de));
6838cf781b2eSEd Maste 				continue;
6839cf781b2eSEd Maste 			}
6840cf781b2eSEd Maste 
6841cf781b2eSEd Maste 			lowpc = 0;
6842cf781b2eSEd Maste 			if (tag == DW_TAG_type_unit) {
6843cf781b2eSEd Maste 				if (dwarf_attrval_unsigned(die, DW_AT_low_pc,
6844cf781b2eSEd Maste 				    &lowpc, &de) != DW_DLV_OK)
6845cf781b2eSEd Maste 					lowpc = 0;
6846cf781b2eSEd Maste 			}
6847cf781b2eSEd Maste 
6848cf781b2eSEd Maste 			/*
6849cf781b2eSEd Maste 			 * Search attributes for reference to .debug_loc
6850cf781b2eSEd Maste 			 * section.
6851cf781b2eSEd Maste 			 */
68529817bae9SEd Maste 			search_loclist_at(re, die, lowpc, &la_list,
68539817bae9SEd Maste 			    &la_list_len, &la_list_cap);
6854cf781b2eSEd Maste 		}
6855cf781b2eSEd Maste 		if (ret == DW_DLV_ERROR)
6856cf781b2eSEd Maste 			warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
6857cf781b2eSEd Maste 	} while (dwarf_next_types_section(re->dbg, &de) == DW_DLV_OK);
6858cf781b2eSEd Maste 
68599817bae9SEd Maste 	if (la_list_len == 0) {
68609817bae9SEd Maste 		free(la_list);
6861cf781b2eSEd Maste 		return;
68629817bae9SEd Maste 	}
68639817bae9SEd Maste 
68649817bae9SEd Maste 	/* Sort la_list using loc_at_comparator. */
68659817bae9SEd Maste 	qsort(la_list, la_list_len, sizeof(struct loc_at), loc_at_comparator);
68669817bae9SEd Maste 
68679817bae9SEd Maste 	/* Get rid of the duplicates in la_list. */
68689817bae9SEd Maste 	duplicates = 0;
68699817bae9SEd Maste 	for (k = 1; k < la_list_len; ++k) {
68709817bae9SEd Maste 		left = &la_list[k - 1 - duplicates];
68719817bae9SEd Maste 		right = &la_list[k];
68729817bae9SEd Maste 
68739817bae9SEd Maste 		if (left->la_off == right->la_off)
68749817bae9SEd Maste 			duplicates++;
68759817bae9SEd Maste 		else
68769817bae9SEd Maste 			la_list[k - duplicates] = *right;
68779817bae9SEd Maste 	}
68789817bae9SEd Maste 	la_list_len -= duplicates;
6879cf781b2eSEd Maste 
6880bee2765cSEd Maste 	has_content = 0;
68819817bae9SEd Maste 	for (k = 0; k < la_list_len; ++k) {
68829817bae9SEd Maste 		la = &la_list[k];
6883bee2765cSEd Maste 		if ((ret = dwarf_loclist_n(la->la_at, &llbuf, &lcnt, &de)) !=
6884cf781b2eSEd Maste 		    DW_DLV_OK) {
6885bee2765cSEd Maste 			if (ret != DW_DLV_NO_ENTRY)
6886bee2765cSEd Maste 				warnx("dwarf_loclist_n failed: %s",
6887bee2765cSEd Maste 				    dwarf_errmsg(de));
6888cf781b2eSEd Maste 			continue;
6889cf781b2eSEd Maste 		}
6890bee2765cSEd Maste 		if (!has_content) {
6891bee2765cSEd Maste 			has_content = 1;
6892bee2765cSEd Maste 			printf("\nContents of section .debug_loc:\n");
6893bee2765cSEd Maste 			printf("    Offset   Begin    End      Expression\n");
6894bee2765cSEd Maste 		}
6895cf781b2eSEd Maste 		set_cu_context(re, la->la_cu_psize, la->la_cu_osize,
6896cf781b2eSEd Maste 		    la->la_cu_ver);
6897cf781b2eSEd Maste 		for (i = 0; i < lcnt; i++) {
6898839529caSEd Maste 			printf("    %8.8jx ", (uintmax_t) la->la_off);
6899cf781b2eSEd Maste 			if (llbuf[i]->ld_lopc == 0 && llbuf[i]->ld_hipc == 0) {
6900cf781b2eSEd Maste 				printf("<End of list>\n");
6901cf781b2eSEd Maste 				continue;
6902cf781b2eSEd Maste 			}
6903cf781b2eSEd Maste 
6904cf781b2eSEd Maste 			/* TODO: handle base selection entry. */
6905cf781b2eSEd Maste 
6906cf781b2eSEd Maste 			printf("%8.8jx %8.8jx ",
6907cf781b2eSEd Maste 			    (uintmax_t) (la->la_lowpc + llbuf[i]->ld_lopc),
6908cf781b2eSEd Maste 			    (uintmax_t) (la->la_lowpc + llbuf[i]->ld_hipc));
6909cf781b2eSEd Maste 
6910cf781b2eSEd Maste 			putchar('(');
6911cf781b2eSEd Maste 			for (j = 0; (Dwarf_Half) j < llbuf[i]->ld_cents; j++) {
6912cf781b2eSEd Maste 				dump_dwarf_loc(re, &llbuf[i]->ld_s[j]);
69132c23cb7cSEd Maste 				if (j < llbuf[i]->ld_cents - 1)
6914cf781b2eSEd Maste 					printf("; ");
69152c23cb7cSEd Maste 			}
69162c23cb7cSEd Maste 			putchar(')');
69172c23cb7cSEd Maste 
69182c23cb7cSEd Maste 			if (llbuf[i]->ld_lopc == llbuf[i]->ld_hipc)
69192c23cb7cSEd Maste 				printf(" (start == end)");
69202c23cb7cSEd Maste 			putchar('\n');
69212c23cb7cSEd Maste 		}
6922cf781b2eSEd Maste 		for (i = 0; i < lcnt; i++) {
6923cf781b2eSEd Maste 			dwarf_dealloc(re->dbg, llbuf[i]->ld_s,
6924cf781b2eSEd Maste 			    DW_DLA_LOC_BLOCK);
6925cf781b2eSEd Maste 			dwarf_dealloc(re->dbg, llbuf[i], DW_DLA_LOCDESC);
6926cf781b2eSEd Maste 		}
6927cf781b2eSEd Maste 		dwarf_dealloc(re->dbg, llbuf, DW_DLA_LIST);
69282c23cb7cSEd Maste 	}
6929bee2765cSEd Maste 
6930bee2765cSEd Maste 	if (!has_content)
6931bee2765cSEd Maste 		printf("\nSection '.debug_loc' has no debugging data.\n");
69329817bae9SEd Maste 
69339817bae9SEd Maste 	free(la_list);
69342c23cb7cSEd Maste }
69352c23cb7cSEd Maste 
69362c23cb7cSEd Maste /*
69372c23cb7cSEd Maste  * Retrieve a string using string table section index and the string offset.
69382c23cb7cSEd Maste  */
69392c23cb7cSEd Maste static const char*
get_string(struct readelf * re,int strtab,size_t off)69402c23cb7cSEd Maste get_string(struct readelf *re, int strtab, size_t off)
69412c23cb7cSEd Maste {
69422c23cb7cSEd Maste 	const char *name;
69432c23cb7cSEd Maste 
69442c23cb7cSEd Maste 	if ((name = elf_strptr(re->elf, strtab, off)) == NULL)
69452c23cb7cSEd Maste 		return ("");
69462c23cb7cSEd Maste 
69472c23cb7cSEd Maste 	return (name);
69482c23cb7cSEd Maste }
69492c23cb7cSEd Maste 
69502c23cb7cSEd Maste /*
69512c23cb7cSEd Maste  * Retrieve the name of a symbol using the section index of the symbol
69522c23cb7cSEd Maste  * table and the index of the symbol within that table.
69532c23cb7cSEd Maste  */
69542c23cb7cSEd Maste static const char *
get_symbol_name(struct readelf * re,int symtab,int i)69552c23cb7cSEd Maste get_symbol_name(struct readelf *re, int symtab, int i)
69562c23cb7cSEd Maste {
69572c23cb7cSEd Maste 	struct section	*s;
69582c23cb7cSEd Maste 	const char	*name;
69592c23cb7cSEd Maste 	GElf_Sym	 sym;
69602c23cb7cSEd Maste 	Elf_Data	*data;
69612c23cb7cSEd Maste 	int		 elferr;
69622c23cb7cSEd Maste 
69632c23cb7cSEd Maste 	s = &re->sl[symtab];
69642c23cb7cSEd Maste 	if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM)
69652c23cb7cSEd Maste 		return ("");
69662c23cb7cSEd Maste 	(void) elf_errno();
69672c23cb7cSEd Maste 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
69682c23cb7cSEd Maste 		elferr = elf_errno();
69692c23cb7cSEd Maste 		if (elferr != 0)
69702c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
69712c23cb7cSEd Maste 		return ("");
69722c23cb7cSEd Maste 	}
69732c23cb7cSEd Maste 	if (gelf_getsym(data, i, &sym) != &sym)
69742c23cb7cSEd Maste 		return ("");
69752c23cb7cSEd Maste 	/* Return section name for STT_SECTION symbol. */
6976b6b6f9ccSEd Maste 	if (GELF_ST_TYPE(sym.st_info) == STT_SECTION) {
6977b6b6f9ccSEd Maste 		if (sym.st_shndx < re->shnum &&
69782c23cb7cSEd Maste 		    re->sl[sym.st_shndx].name != NULL)
69792c23cb7cSEd Maste 			return (re->sl[sym.st_shndx].name);
6980b6b6f9ccSEd Maste 		return ("");
6981b6b6f9ccSEd Maste 	}
6982656f49f8SEd Maste 	if (s->link >= re->shnum ||
6983656f49f8SEd Maste 	    (name = elf_strptr(re->elf, s->link, sym.st_name)) == NULL)
69842c23cb7cSEd Maste 		return ("");
69852c23cb7cSEd Maste 
69862c23cb7cSEd Maste 	return (name);
69872c23cb7cSEd Maste }
69882c23cb7cSEd Maste 
69892c23cb7cSEd Maste static uint64_t
get_symbol_value(struct readelf * re,int symtab,int i)69902c23cb7cSEd Maste get_symbol_value(struct readelf *re, int symtab, int i)
69912c23cb7cSEd Maste {
69922c23cb7cSEd Maste 	struct section	*s;
69932c23cb7cSEd Maste 	GElf_Sym	 sym;
69942c23cb7cSEd Maste 	Elf_Data	*data;
69952c23cb7cSEd Maste 	int		 elferr;
69962c23cb7cSEd Maste 
69972c23cb7cSEd Maste 	s = &re->sl[symtab];
69982c23cb7cSEd Maste 	if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM)
69992c23cb7cSEd Maste 		return (0);
70002c23cb7cSEd Maste 	(void) elf_errno();
70012c23cb7cSEd Maste 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
70022c23cb7cSEd Maste 		elferr = elf_errno();
70032c23cb7cSEd Maste 		if (elferr != 0)
70042c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
70052c23cb7cSEd Maste 		return (0);
70062c23cb7cSEd Maste 	}
70072c23cb7cSEd Maste 	if (gelf_getsym(data, i, &sym) != &sym)
70082c23cb7cSEd Maste 		return (0);
70092c23cb7cSEd Maste 
70102c23cb7cSEd Maste 	return (sym.st_value);
70112c23cb7cSEd Maste }
70122c23cb7cSEd Maste 
7013e128bd0fSEd Maste /*
7014e128bd0fSEd Maste  * Decompress a data section if needed (using ZLIB).
7015e128bd0fSEd Maste  * Returns true if sucessful, false otherwise.
7016e128bd0fSEd Maste  */
decompress_section(struct section * s,unsigned char * compressed_data_buffer,size_t compressed_size,unsigned char ** ret_buf,size_t * ret_sz)7017e128bd0fSEd Maste static bool decompress_section(struct section *s,
7018d4fba562SBrandon Bergren     unsigned char *compressed_data_buffer, size_t compressed_size,
7019d4fba562SBrandon Bergren     unsigned char **ret_buf, size_t *ret_sz)
7020e128bd0fSEd Maste {
7021e128bd0fSEd Maste 	GElf_Shdr sh;
7022e128bd0fSEd Maste 
7023e128bd0fSEd Maste 	if (gelf_getshdr(s->scn, &sh) == NULL)
7024e128bd0fSEd Maste 		errx(EXIT_FAILURE, "gelf_getshdr() failed: %s", elf_errmsg(-1));
7025e128bd0fSEd Maste 
7026e128bd0fSEd Maste 	if (sh.sh_flags & SHF_COMPRESSED) {
7027e128bd0fSEd Maste 		int ret;
7028e128bd0fSEd Maste 		GElf_Chdr chdr;
7029e128bd0fSEd Maste 		Elf64_Xword inflated_size;
7030e128bd0fSEd Maste 		unsigned char *uncompressed_data_buffer = NULL;
7031e128bd0fSEd Maste 		Elf64_Xword uncompressed_size;
7032e128bd0fSEd Maste 		z_stream strm;
7033e128bd0fSEd Maste 
7034e128bd0fSEd Maste 		if (gelf_getchdr(s->scn, &chdr) == NULL)
7035e128bd0fSEd Maste 			errx(EXIT_FAILURE, "gelf_getchdr() failed: %s", elf_errmsg(-1));
7036e128bd0fSEd Maste 		if (chdr.ch_type != ELFCOMPRESS_ZLIB) {
7037e128bd0fSEd Maste 			warnx("unknown compression type: %d", chdr.ch_type);
7038e128bd0fSEd Maste 			return (false);
7039e128bd0fSEd Maste 		}
7040e128bd0fSEd Maste 
7041e128bd0fSEd Maste 		inflated_size = 0;
7042e128bd0fSEd Maste 		uncompressed_size = chdr.ch_size;
7043e128bd0fSEd Maste 		uncompressed_data_buffer = malloc(uncompressed_size);
7044e128bd0fSEd Maste 		compressed_data_buffer += sizeof(chdr);
7045e128bd0fSEd Maste 		compressed_size -= sizeof(chdr);
7046e128bd0fSEd Maste 
7047e128bd0fSEd Maste 		strm.zalloc = Z_NULL;
7048e128bd0fSEd Maste 		strm.zfree = Z_NULL;
7049e128bd0fSEd Maste 		strm.opaque = Z_NULL;
7050e128bd0fSEd Maste 		strm.avail_in = compressed_size;
7051e128bd0fSEd Maste 		strm.avail_out = uncompressed_size;
7052e128bd0fSEd Maste 		ret = inflateInit(&strm);
7053e128bd0fSEd Maste 
7054e128bd0fSEd Maste 		if (ret != Z_OK)
7055e128bd0fSEd Maste 			goto fail;
7056e128bd0fSEd Maste 		/*
7057e128bd0fSEd Maste 		 * The section can contain several compressed buffers,
7058e128bd0fSEd Maste 		 * so decompress in a loop until all data is inflated.
7059e128bd0fSEd Maste 		 */
7060e128bd0fSEd Maste 		while (inflated_size < compressed_size) {
7061e128bd0fSEd Maste 			strm.next_in = compressed_data_buffer + inflated_size;
7062e128bd0fSEd Maste 			strm.next_out = uncompressed_data_buffer + inflated_size;
7063e128bd0fSEd Maste 			ret = inflate(&strm, Z_FINISH);
7064e128bd0fSEd Maste 			if (ret != Z_STREAM_END)
7065e128bd0fSEd Maste 				goto fail;
7066e128bd0fSEd Maste 			inflated_size = uncompressed_size - strm.avail_out;
7067e128bd0fSEd Maste 			ret = inflateReset(&strm);
7068e128bd0fSEd Maste 			if (ret != Z_OK)
7069e128bd0fSEd Maste 				goto fail;
7070e128bd0fSEd Maste 		}
7071e128bd0fSEd Maste 		if (strm.avail_out != 0)
7072e128bd0fSEd Maste 			warnx("Warning: wrong info in compression header.");
7073e128bd0fSEd Maste 		ret = inflateEnd(&strm);
7074e128bd0fSEd Maste 		if (ret != Z_OK)
7075e128bd0fSEd Maste 			goto fail;
7076e128bd0fSEd Maste 		*ret_buf = uncompressed_data_buffer;
7077e128bd0fSEd Maste 		*ret_sz = uncompressed_size;
7078e128bd0fSEd Maste 		return (true);
7079e128bd0fSEd Maste fail:
7080e128bd0fSEd Maste 		inflateEnd(&strm);
7081e128bd0fSEd Maste 		if (strm.msg)
7082e128bd0fSEd Maste 			warnx("%s", strm.msg);
7083e128bd0fSEd Maste 		else
7084e128bd0fSEd Maste 			warnx("ZLIB error: %d", ret);
7085e128bd0fSEd Maste 		free(uncompressed_data_buffer);
7086e128bd0fSEd Maste 		return (false);
7087e128bd0fSEd Maste 	}
7088e128bd0fSEd Maste 	return (false);
7089e128bd0fSEd Maste }
7090e128bd0fSEd Maste 
70912c23cb7cSEd Maste static void
hex_dump(struct readelf * re)70922c23cb7cSEd Maste hex_dump(struct readelf *re)
70932c23cb7cSEd Maste {
70942c23cb7cSEd Maste 	struct section *s;
70952c23cb7cSEd Maste 	Elf_Data *d;
7096e128bd0fSEd Maste 	uint8_t *buf, *new_buf;
70972c23cb7cSEd Maste 	size_t sz, nbytes;
70982c23cb7cSEd Maste 	uint64_t addr;
70992c23cb7cSEd Maste 	int elferr, i, j;
71002c23cb7cSEd Maste 
71012c23cb7cSEd Maste 	for (i = 1; (size_t) i < re->shnum; i++) {
7102e128bd0fSEd Maste 		new_buf = NULL;
71032c23cb7cSEd Maste 		s = &re->sl[i];
71042c23cb7cSEd Maste 		if (find_dumpop(re, (size_t) i, s->name, HEX_DUMP, -1) == NULL)
71052c23cb7cSEd Maste 			continue;
71062c23cb7cSEd Maste 		(void) elf_errno();
7107839529caSEd Maste 		if ((d = elf_getdata(s->scn, NULL)) == NULL &&
7108839529caSEd Maste 		    (d = elf_rawdata(s->scn, NULL)) == NULL) {
71092c23cb7cSEd Maste 			elferr = elf_errno();
71102c23cb7cSEd Maste 			if (elferr != 0)
71112c23cb7cSEd Maste 				warnx("elf_getdata failed: %s",
71122c23cb7cSEd Maste 				    elf_errmsg(elferr));
71132c23cb7cSEd Maste 			continue;
71142c23cb7cSEd Maste 		}
7115839529caSEd Maste 		(void) elf_errno();
71162c23cb7cSEd Maste 		if (d->d_size <= 0 || d->d_buf == NULL) {
71172c23cb7cSEd Maste 			printf("\nSection '%s' has no data to dump.\n",
71182c23cb7cSEd Maste 			    s->name);
71192c23cb7cSEd Maste 			continue;
71202c23cb7cSEd Maste 		}
71212c23cb7cSEd Maste 		buf = d->d_buf;
71222c23cb7cSEd Maste 		sz = d->d_size;
71232c23cb7cSEd Maste 		addr = s->addr;
7124e128bd0fSEd Maste 		if (re->options & RE_Z) {
7125e128bd0fSEd Maste 			if (decompress_section(s, d->d_buf, d->d_size,
7126e128bd0fSEd Maste 			    &new_buf, &sz))
7127e128bd0fSEd Maste 				buf = new_buf;
7128e128bd0fSEd Maste 		}
71292c23cb7cSEd Maste 		printf("\nHex dump of section '%s':\n", s->name);
71302c23cb7cSEd Maste 		while (sz > 0) {
71312c23cb7cSEd Maste 			printf("  0x%8.8jx ", (uintmax_t)addr);
71322c23cb7cSEd Maste 			nbytes = sz > 16? 16 : sz;
71332c23cb7cSEd Maste 			for (j = 0; j < 16; j++) {
71342c23cb7cSEd Maste 				if ((size_t)j < nbytes)
71352c23cb7cSEd Maste 					printf("%2.2x", buf[j]);
71362c23cb7cSEd Maste 				else
71372c23cb7cSEd Maste 					printf("  ");
71382c23cb7cSEd Maste 				if ((j & 3) == 3)
71392c23cb7cSEd Maste 					printf(" ");
71402c23cb7cSEd Maste 			}
71412c23cb7cSEd Maste 			for (j = 0; (size_t)j < nbytes; j++) {
71422c23cb7cSEd Maste 				if (isprint(buf[j]))
71432c23cb7cSEd Maste 					printf("%c", buf[j]);
71442c23cb7cSEd Maste 				else
71452c23cb7cSEd Maste 					printf(".");
71462c23cb7cSEd Maste 			}
71472c23cb7cSEd Maste 			printf("\n");
71482c23cb7cSEd Maste 			buf += nbytes;
71492c23cb7cSEd Maste 			addr += nbytes;
71502c23cb7cSEd Maste 			sz -= nbytes;
71512c23cb7cSEd Maste 		}
7152e128bd0fSEd Maste 		free(new_buf);
71532c23cb7cSEd Maste 	}
71542c23cb7cSEd Maste }
71552c23cb7cSEd Maste 
71562c23cb7cSEd Maste static void
str_dump(struct readelf * re)71572c23cb7cSEd Maste str_dump(struct readelf *re)
71582c23cb7cSEd Maste {
71592c23cb7cSEd Maste 	struct section *s;
71602c23cb7cSEd Maste 	Elf_Data *d;
7161e128bd0fSEd Maste 	unsigned char *start, *end, *buf_end, *new_buf;
71622c23cb7cSEd Maste 	unsigned int len;
7163e128bd0fSEd Maste 	size_t sz;
71642c23cb7cSEd Maste 	int i, j, elferr, found;
71652c23cb7cSEd Maste 
71662c23cb7cSEd Maste 	for (i = 1; (size_t) i < re->shnum; i++) {
7167e128bd0fSEd Maste 		new_buf = NULL;
71682c23cb7cSEd Maste 		s = &re->sl[i];
71692c23cb7cSEd Maste 		if (find_dumpop(re, (size_t) i, s->name, STR_DUMP, -1) == NULL)
71702c23cb7cSEd Maste 			continue;
71712c23cb7cSEd Maste 		(void) elf_errno();
7172839529caSEd Maste 		if ((d = elf_getdata(s->scn, NULL)) == NULL &&
7173839529caSEd Maste 		    (d = elf_rawdata(s->scn, NULL)) == NULL) {
71742c23cb7cSEd Maste 			elferr = elf_errno();
71752c23cb7cSEd Maste 			if (elferr != 0)
71762c23cb7cSEd Maste 				warnx("elf_getdata failed: %s",
71772c23cb7cSEd Maste 				    elf_errmsg(elferr));
71782c23cb7cSEd Maste 			continue;
71792c23cb7cSEd Maste 		}
7180839529caSEd Maste 		(void) elf_errno();
71812c23cb7cSEd Maste 		if (d->d_size <= 0 || d->d_buf == NULL) {
71822c23cb7cSEd Maste 			printf("\nSection '%s' has no data to dump.\n",
71832c23cb7cSEd Maste 			    s->name);
71842c23cb7cSEd Maste 			continue;
71852c23cb7cSEd Maste 		}
71862c23cb7cSEd Maste 		found = 0;
7187e128bd0fSEd Maste 		start = d->d_buf;
7188e128bd0fSEd Maste 		sz = d->d_size;
7189e128bd0fSEd Maste 		if (re->options & RE_Z) {
7190e128bd0fSEd Maste 			if (decompress_section(s, d->d_buf, d->d_size,
7191e128bd0fSEd Maste 			    &new_buf, &sz))
7192e128bd0fSEd Maste 				start = new_buf;
7193e128bd0fSEd Maste 		}
7194e128bd0fSEd Maste 		buf_end = start + sz;
71952c23cb7cSEd Maste 		printf("\nString dump of section '%s':\n", s->name);
71962c23cb7cSEd Maste 		for (;;) {
71972c23cb7cSEd Maste 			while (start < buf_end && !isprint(*start))
71982c23cb7cSEd Maste 				start++;
71992c23cb7cSEd Maste 			if (start >= buf_end)
72002c23cb7cSEd Maste 				break;
72012c23cb7cSEd Maste 			end = start + 1;
72022c23cb7cSEd Maste 			while (end < buf_end && isprint(*end))
72032c23cb7cSEd Maste 				end++;
72042c23cb7cSEd Maste 			printf("  [%6lx]  ",
72052c23cb7cSEd Maste 			    (long) (start - (unsigned char *) d->d_buf));
72062c23cb7cSEd Maste 			len = end - start;
72072c23cb7cSEd Maste 			for (j = 0; (unsigned int) j < len; j++)
72082c23cb7cSEd Maste 				putchar(start[j]);
72092c23cb7cSEd Maste 			putchar('\n');
72102c23cb7cSEd Maste 			found = 1;
72112c23cb7cSEd Maste 			if (end >= buf_end)
72122c23cb7cSEd Maste 				break;
72132c23cb7cSEd Maste 			start = end + 1;
72142c23cb7cSEd Maste 		}
7215e128bd0fSEd Maste 		free(new_buf);
72162c23cb7cSEd Maste 		if (!found)
72172c23cb7cSEd Maste 			printf("  No strings found in this section.");
72182c23cb7cSEd Maste 		putchar('\n');
72192c23cb7cSEd Maste 	}
72202c23cb7cSEd Maste }
72212c23cb7cSEd Maste 
72222c23cb7cSEd Maste static void
load_sections(struct readelf * re)72232c23cb7cSEd Maste load_sections(struct readelf *re)
72242c23cb7cSEd Maste {
72252c23cb7cSEd Maste 	struct section	*s;
72262c23cb7cSEd Maste 	const char	*name;
72272c23cb7cSEd Maste 	Elf_Scn		*scn;
72282c23cb7cSEd Maste 	GElf_Shdr	 sh;
72292c23cb7cSEd Maste 	size_t		 shstrndx, ndx;
72302c23cb7cSEd Maste 	int		 elferr;
72312c23cb7cSEd Maste 
72322c23cb7cSEd Maste 	/* Allocate storage for internal section list. */
72332c23cb7cSEd Maste 	if (!elf_getshnum(re->elf, &re->shnum)) {
72342c23cb7cSEd Maste 		warnx("elf_getshnum failed: %s", elf_errmsg(-1));
72352c23cb7cSEd Maste 		return;
72362c23cb7cSEd Maste 	}
72372c23cb7cSEd Maste 	if (re->sl != NULL)
72382c23cb7cSEd Maste 		free(re->sl);
72392c23cb7cSEd Maste 	if ((re->sl = calloc(re->shnum, sizeof(*re->sl))) == NULL)
72402c23cb7cSEd Maste 		err(EXIT_FAILURE, "calloc failed");
72412c23cb7cSEd Maste 
72422c23cb7cSEd Maste 	/* Get the index of .shstrtab section. */
72432c23cb7cSEd Maste 	if (!elf_getshstrndx(re->elf, &shstrndx)) {
72442c23cb7cSEd Maste 		warnx("elf_getshstrndx failed: %s", elf_errmsg(-1));
72452c23cb7cSEd Maste 		return;
72462c23cb7cSEd Maste 	}
72472c23cb7cSEd Maste 
724871a0c925SEd Maste 	if ((scn = elf_getscn(re->elf, 0)) == NULL)
72492c23cb7cSEd Maste 		return;
72502c23cb7cSEd Maste 
72512c23cb7cSEd Maste 	(void) elf_errno();
72522c23cb7cSEd Maste 	do {
72532c23cb7cSEd Maste 		if (gelf_getshdr(scn, &sh) == NULL) {
72542c23cb7cSEd Maste 			warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
72552c23cb7cSEd Maste 			(void) elf_errno();
72562c23cb7cSEd Maste 			continue;
72572c23cb7cSEd Maste 		}
72582c23cb7cSEd Maste 		if ((name = elf_strptr(re->elf, shstrndx, sh.sh_name)) == NULL) {
72592c23cb7cSEd Maste 			(void) elf_errno();
72607dc45d65SConrad Meyer 			name = "<no-name>";
72612c23cb7cSEd Maste 		}
72622c23cb7cSEd Maste 		if ((ndx = elf_ndxscn(scn)) == SHN_UNDEF) {
72637dc45d65SConrad Meyer 			if ((elferr = elf_errno()) != 0) {
72642c23cb7cSEd Maste 				warnx("elf_ndxscn failed: %s",
72652c23cb7cSEd Maste 				    elf_errmsg(elferr));
72662c23cb7cSEd Maste 				continue;
72672c23cb7cSEd Maste 			}
72687dc45d65SConrad Meyer 		}
72692c23cb7cSEd Maste 		if (ndx >= re->shnum) {
72702c23cb7cSEd Maste 			warnx("section index of '%s' out of range", name);
72712c23cb7cSEd Maste 			continue;
72722c23cb7cSEd Maste 		}
7273656f49f8SEd Maste 		if (sh.sh_link >= re->shnum)
7274656f49f8SEd Maste 			warnx("section link %llu of '%s' out of range",
7275656f49f8SEd Maste 			    (unsigned long long)sh.sh_link, name);
72762c23cb7cSEd Maste 		s = &re->sl[ndx];
72772c23cb7cSEd Maste 		s->name = name;
72782c23cb7cSEd Maste 		s->scn = scn;
72792c23cb7cSEd Maste 		s->off = sh.sh_offset;
72802c23cb7cSEd Maste 		s->sz = sh.sh_size;
72812c23cb7cSEd Maste 		s->entsize = sh.sh_entsize;
72822c23cb7cSEd Maste 		s->align = sh.sh_addralign;
72832c23cb7cSEd Maste 		s->type = sh.sh_type;
72842c23cb7cSEd Maste 		s->flags = sh.sh_flags;
72852c23cb7cSEd Maste 		s->addr = sh.sh_addr;
72862c23cb7cSEd Maste 		s->link = sh.sh_link;
72872c23cb7cSEd Maste 		s->info = sh.sh_info;
72882c23cb7cSEd Maste 	} while ((scn = elf_nextscn(re->elf, scn)) != NULL);
72892c23cb7cSEd Maste 	elferr = elf_errno();
72902c23cb7cSEd Maste 	if (elferr != 0)
72912c23cb7cSEd Maste 		warnx("elf_nextscn failed: %s", elf_errmsg(elferr));
72922c23cb7cSEd Maste }
72932c23cb7cSEd Maste 
72942c23cb7cSEd Maste static void
unload_sections(struct readelf * re)72952c23cb7cSEd Maste unload_sections(struct readelf *re)
72962c23cb7cSEd Maste {
72972c23cb7cSEd Maste 
72982c23cb7cSEd Maste 	if (re->sl != NULL) {
72992c23cb7cSEd Maste 		free(re->sl);
73002c23cb7cSEd Maste 		re->sl = NULL;
73012c23cb7cSEd Maste 	}
73022c23cb7cSEd Maste 	re->shnum = 0;
73032c23cb7cSEd Maste 	re->vd_s = NULL;
73042c23cb7cSEd Maste 	re->vn_s = NULL;
73052c23cb7cSEd Maste 	re->vs_s = NULL;
73062c23cb7cSEd Maste 	re->vs = NULL;
73072c23cb7cSEd Maste 	re->vs_sz = 0;
73082c23cb7cSEd Maste 	if (re->ver != NULL) {
73092c23cb7cSEd Maste 		free(re->ver);
73102c23cb7cSEd Maste 		re->ver = NULL;
73112c23cb7cSEd Maste 		re->ver_sz = 0;
73122c23cb7cSEd Maste 	}
73132c23cb7cSEd Maste }
73142c23cb7cSEd Maste 
7315ea444392SEd Maste static bool
dump_elf(struct readelf * re)73162c23cb7cSEd Maste dump_elf(struct readelf *re)
73172c23cb7cSEd Maste {
73182c23cb7cSEd Maste 
73192c23cb7cSEd Maste 	/* Fetch ELF header. No need to continue if it fails. */
73202c23cb7cSEd Maste 	if (gelf_getehdr(re->elf, &re->ehdr) == NULL) {
73212c23cb7cSEd Maste 		warnx("gelf_getehdr failed: %s", elf_errmsg(-1));
7322ea444392SEd Maste 		return (false);
73232c23cb7cSEd Maste 	}
73242c23cb7cSEd Maste 	if ((re->ec = gelf_getclass(re->elf)) == ELFCLASSNONE) {
73252c23cb7cSEd Maste 		warnx("gelf_getclass failed: %s", elf_errmsg(-1));
7326ea444392SEd Maste 		return (false);
73272c23cb7cSEd Maste 	}
73282c23cb7cSEd Maste 	if (re->ehdr.e_ident[EI_DATA] == ELFDATA2MSB) {
73292c23cb7cSEd Maste 		re->dw_read = _read_msb;
73302c23cb7cSEd Maste 		re->dw_decode = _decode_msb;
73312c23cb7cSEd Maste 	} else {
73322c23cb7cSEd Maste 		re->dw_read = _read_lsb;
73332c23cb7cSEd Maste 		re->dw_decode = _decode_lsb;
73342c23cb7cSEd Maste 	}
73352c23cb7cSEd Maste 
73362c23cb7cSEd Maste 	if (re->options & ~RE_H)
73372c23cb7cSEd Maste 		load_sections(re);
73382c23cb7cSEd Maste 	if ((re->options & RE_VV) || (re->options & RE_S))
73392c23cb7cSEd Maste 		search_ver(re);
73402c23cb7cSEd Maste 	if (re->options & RE_H)
73412c23cb7cSEd Maste 		dump_ehdr(re);
73422c23cb7cSEd Maste 	if (re->options & RE_L)
73432c23cb7cSEd Maste 		dump_phdr(re);
73442c23cb7cSEd Maste 	if (re->options & RE_SS)
73452c23cb7cSEd Maste 		dump_shdr(re);
73463ef90571SEd Maste 	if (re->options & RE_G)
73473ef90571SEd Maste 		dump_section_groups(re);
73482c23cb7cSEd Maste 	if (re->options & RE_D)
73492c23cb7cSEd Maste 		dump_dynamic(re);
73502c23cb7cSEd Maste 	if (re->options & RE_R)
73512c23cb7cSEd Maste 		dump_reloc(re);
73522c23cb7cSEd Maste 	if (re->options & RE_S)
73532c23cb7cSEd Maste 		dump_symtabs(re);
73542c23cb7cSEd Maste 	if (re->options & RE_N)
73552c23cb7cSEd Maste 		dump_notes(re);
73562c23cb7cSEd Maste 	if (re->options & RE_II)
73572c23cb7cSEd Maste 		dump_hash(re);
73582c23cb7cSEd Maste 	if (re->options & RE_X)
73592c23cb7cSEd Maste 		hex_dump(re);
73602c23cb7cSEd Maste 	if (re->options & RE_P)
73612c23cb7cSEd Maste 		str_dump(re);
73622c23cb7cSEd Maste 	if (re->options & RE_VV)
73632c23cb7cSEd Maste 		dump_ver(re);
73642c23cb7cSEd Maste 	if (re->options & RE_AA)
73652c23cb7cSEd Maste 		dump_arch_specific_info(re);
73662c23cb7cSEd Maste 	if (re->options & RE_W)
73672c23cb7cSEd Maste 		dump_dwarf(re);
73682c23cb7cSEd Maste 	if (re->options & ~RE_H)
73692c23cb7cSEd Maste 		unload_sections(re);
7370ea444392SEd Maste 	return (true);
73712c23cb7cSEd Maste }
73722c23cb7cSEd Maste 
73732c23cb7cSEd Maste static void
dump_dwarf(struct readelf * re)73742c23cb7cSEd Maste dump_dwarf(struct readelf *re)
73752c23cb7cSEd Maste {
73762c23cb7cSEd Maste 	Dwarf_Error de;
7377bee2765cSEd Maste 	int error;
73782c23cb7cSEd Maste 
73792c23cb7cSEd Maste 	if (dwarf_elf_init(re->elf, DW_DLC_READ, NULL, NULL, &re->dbg, &de)) {
73802c23cb7cSEd Maste 		if ((error = dwarf_errno(de)) != DW_DLE_DEBUG_INFO_NULL)
73812c23cb7cSEd Maste 			errx(EXIT_FAILURE, "dwarf_elf_init failed: %s",
73822c23cb7cSEd Maste 			    dwarf_errmsg(de));
73832c23cb7cSEd Maste 		return;
73842c23cb7cSEd Maste 	}
73852c23cb7cSEd Maste 
73862c23cb7cSEd Maste 	if (re->dop & DW_A)
73872c23cb7cSEd Maste 		dump_dwarf_abbrev(re);
73882c23cb7cSEd Maste 	if (re->dop & DW_L)
73892c23cb7cSEd Maste 		dump_dwarf_line(re);
73902c23cb7cSEd Maste 	if (re->dop & DW_LL)
73912c23cb7cSEd Maste 		dump_dwarf_line_decoded(re);
7392cf781b2eSEd Maste 	if (re->dop & DW_I) {
7393cf781b2eSEd Maste 		dump_dwarf_info(re, 0);
7394cf781b2eSEd Maste 		dump_dwarf_info(re, 1);
7395cf781b2eSEd Maste 	}
73962c23cb7cSEd Maste 	if (re->dop & DW_P)
73972c23cb7cSEd Maste 		dump_dwarf_pubnames(re);
73982c23cb7cSEd Maste 	if (re->dop & DW_R)
73992c23cb7cSEd Maste 		dump_dwarf_aranges(re);
74002c23cb7cSEd Maste 	if (re->dop & DW_RR)
74012c23cb7cSEd Maste 		dump_dwarf_ranges(re);
74022c23cb7cSEd Maste 	if (re->dop & DW_M)
74032c23cb7cSEd Maste 		dump_dwarf_macinfo(re);
74042c23cb7cSEd Maste 	if (re->dop & DW_F)
74052c23cb7cSEd Maste 		dump_dwarf_frame(re, 0);
74062c23cb7cSEd Maste 	else if (re->dop & DW_FF)
74072c23cb7cSEd Maste 		dump_dwarf_frame(re, 1);
74082c23cb7cSEd Maste 	if (re->dop & DW_S)
74092c23cb7cSEd Maste 		dump_dwarf_str(re);
74102c23cb7cSEd Maste 	if (re->dop & DW_O)
74112c23cb7cSEd Maste 		dump_dwarf_loclist(re);
74122c23cb7cSEd Maste 
74132c23cb7cSEd Maste 	dwarf_finish(re->dbg, &de);
74142c23cb7cSEd Maste }
74152c23cb7cSEd Maste 
7416ea444392SEd Maste static bool
dump_ar(struct readelf * re,int fd)74172c23cb7cSEd Maste dump_ar(struct readelf *re, int fd)
74182c23cb7cSEd Maste {
74192c23cb7cSEd Maste 	Elf_Arsym *arsym;
74202c23cb7cSEd Maste 	Elf_Arhdr *arhdr;
74212c23cb7cSEd Maste 	Elf_Cmd cmd;
74222c23cb7cSEd Maste 	Elf *e;
74232c23cb7cSEd Maste 	size_t sz;
74242c23cb7cSEd Maste 	off_t off;
74252c23cb7cSEd Maste 	int i;
74262c23cb7cSEd Maste 
74272c23cb7cSEd Maste 	re->ar = re->elf;
74282c23cb7cSEd Maste 
74292c23cb7cSEd Maste 	if (re->options & RE_C) {
74302c23cb7cSEd Maste 		if ((arsym = elf_getarsym(re->ar, &sz)) == NULL) {
74312c23cb7cSEd Maste 			warnx("elf_getarsym() failed: %s", elf_errmsg(-1));
74322c23cb7cSEd Maste 			goto process_members;
74332c23cb7cSEd Maste 		}
74342c23cb7cSEd Maste 		printf("Index of archive %s: (%ju entries)\n", re->filename,
74352c23cb7cSEd Maste 		    (uintmax_t) sz - 1);
74362c23cb7cSEd Maste 		off = 0;
74372c23cb7cSEd Maste 		for (i = 0; (size_t) i < sz; i++) {
74382c23cb7cSEd Maste 			if (arsym[i].as_name == NULL)
74392c23cb7cSEd Maste 				break;
74402c23cb7cSEd Maste 			if (arsym[i].as_off != off) {
74412c23cb7cSEd Maste 				off = arsym[i].as_off;
74422c23cb7cSEd Maste 				if (elf_rand(re->ar, off) != off) {
74432c23cb7cSEd Maste 					warnx("elf_rand() failed: %s",
74442c23cb7cSEd Maste 					    elf_errmsg(-1));
74452c23cb7cSEd Maste 					continue;
74462c23cb7cSEd Maste 				}
74472c23cb7cSEd Maste 				if ((e = elf_begin(fd, ELF_C_READ, re->ar)) ==
74482c23cb7cSEd Maste 				    NULL) {
74492c23cb7cSEd Maste 					warnx("elf_begin() failed: %s",
74502c23cb7cSEd Maste 					    elf_errmsg(-1));
74512c23cb7cSEd Maste 					continue;
74522c23cb7cSEd Maste 				}
74532c23cb7cSEd Maste 				if ((arhdr = elf_getarhdr(e)) == NULL) {
74542c23cb7cSEd Maste 					warnx("elf_getarhdr() failed: %s",
74552c23cb7cSEd Maste 					    elf_errmsg(-1));
74562c23cb7cSEd Maste 					elf_end(e);
74572c23cb7cSEd Maste 					continue;
74582c23cb7cSEd Maste 				}
74592c23cb7cSEd Maste 				printf("Binary %s(%s) contains:\n",
74602c23cb7cSEd Maste 				    re->filename, arhdr->ar_name);
7461c37c6b38SMark Johnston 				elf_end(e);
74622c23cb7cSEd Maste 			}
74632c23cb7cSEd Maste 			printf("\t%s\n", arsym[i].as_name);
74642c23cb7cSEd Maste 		}
74652c23cb7cSEd Maste 		if (elf_rand(re->ar, SARMAG) != SARMAG) {
74662c23cb7cSEd Maste 			warnx("elf_rand() failed: %s", elf_errmsg(-1));
7467ea444392SEd Maste 			return (false);
74682c23cb7cSEd Maste 		}
74692c23cb7cSEd Maste 	}
74702c23cb7cSEd Maste 
74712c23cb7cSEd Maste process_members:
74722c23cb7cSEd Maste 
74732c23cb7cSEd Maste 	if ((re->options & ~RE_C) == 0)
7474ea444392SEd Maste 		return (true);
74752c23cb7cSEd Maste 
74762c23cb7cSEd Maste 	cmd = ELF_C_READ;
74772c23cb7cSEd Maste 	while ((re->elf = elf_begin(fd, cmd, re->ar)) != NULL) {
74782c23cb7cSEd Maste 		if ((arhdr = elf_getarhdr(re->elf)) == NULL) {
74792c23cb7cSEd Maste 			warnx("elf_getarhdr() failed: %s", elf_errmsg(-1));
74802c23cb7cSEd Maste 			goto next_member;
74812c23cb7cSEd Maste 		}
74822c23cb7cSEd Maste 		if (strcmp(arhdr->ar_name, "/") == 0 ||
74832c23cb7cSEd Maste 		    strcmp(arhdr->ar_name, "//") == 0 ||
74842c23cb7cSEd Maste 		    strcmp(arhdr->ar_name, "__.SYMDEF") == 0)
74852c23cb7cSEd Maste 			goto next_member;
74862c23cb7cSEd Maste 		printf("\nFile: %s(%s)\n", re->filename, arhdr->ar_name);
74872c23cb7cSEd Maste 		dump_elf(re);
74882c23cb7cSEd Maste 
74892c23cb7cSEd Maste 	next_member:
74902c23cb7cSEd Maste 		cmd = elf_next(re->elf);
74912c23cb7cSEd Maste 		elf_end(re->elf);
74922c23cb7cSEd Maste 	}
74932c23cb7cSEd Maste 	re->elf = re->ar;
7494ea444392SEd Maste 	return (true);
74952c23cb7cSEd Maste }
74962c23cb7cSEd Maste 
7497ea444392SEd Maste static bool
dump_object(struct readelf * re,int fd)7498802c2095SMark Johnston dump_object(struct readelf *re, int fd)
74992c23cb7cSEd Maste {
7500ea444392SEd Maste 	bool rv = false;
7501ea444392SEd Maste 
75022c23cb7cSEd Maste 	if ((re->flags & DISPLAY_FILENAME) != 0)
75032c23cb7cSEd Maste 		printf("\nFile: %s\n", re->filename);
75042c23cb7cSEd Maste 
75052c23cb7cSEd Maste 	if ((re->elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
75062c23cb7cSEd Maste 		warnx("elf_begin() failed: %s", elf_errmsg(-1));
7507d003e0d7SEd Maste 		goto done;
75082c23cb7cSEd Maste 	}
75092c23cb7cSEd Maste 
75102c23cb7cSEd Maste 	switch (elf_kind(re->elf)) {
75112c23cb7cSEd Maste 	case ELF_K_NONE:
75122c23cb7cSEd Maste 		warnx("Not an ELF file.");
7513d003e0d7SEd Maste 		goto done;
75142c23cb7cSEd Maste 	case ELF_K_ELF:
7515ea444392SEd Maste 		rv = dump_elf(re);
75162c23cb7cSEd Maste 		break;
75172c23cb7cSEd Maste 	case ELF_K_AR:
7518ea444392SEd Maste 		rv = dump_ar(re, fd);
75192c23cb7cSEd Maste 		break;
75202c23cb7cSEd Maste 	default:
75212c23cb7cSEd Maste 		warnx("Internal: libelf returned unknown elf kind.");
75222c23cb7cSEd Maste 	}
75232c23cb7cSEd Maste 
7524d003e0d7SEd Maste done:
75252c23cb7cSEd Maste 	elf_end(re->elf);
7526ea444392SEd Maste 	return (rv);
75272c23cb7cSEd Maste }
75282c23cb7cSEd Maste 
75292c23cb7cSEd Maste static void
add_dumpop(struct readelf * re,size_t si,const char * sn,int op,int t)75302c23cb7cSEd Maste add_dumpop(struct readelf *re, size_t si, const char *sn, int op, int t)
75312c23cb7cSEd Maste {
75322c23cb7cSEd Maste 	struct dumpop *d;
75332c23cb7cSEd Maste 
75342c23cb7cSEd Maste 	if ((d = find_dumpop(re, si, sn, -1, t)) == NULL) {
75352c23cb7cSEd Maste 		if ((d = calloc(1, sizeof(*d))) == NULL)
75362c23cb7cSEd Maste 			err(EXIT_FAILURE, "calloc failed");
75372c23cb7cSEd Maste 		if (t == DUMP_BY_INDEX)
75382c23cb7cSEd Maste 			d->u.si = si;
75392c23cb7cSEd Maste 		else
75402c23cb7cSEd Maste 			d->u.sn = sn;
75412c23cb7cSEd Maste 		d->type = t;
75422c23cb7cSEd Maste 		d->op = op;
75432c23cb7cSEd Maste 		STAILQ_INSERT_TAIL(&re->v_dumpop, d, dumpop_list);
75442c23cb7cSEd Maste 	} else
75452c23cb7cSEd Maste 		d->op |= op;
75462c23cb7cSEd Maste }
75472c23cb7cSEd Maste 
75482c23cb7cSEd Maste static struct dumpop *
find_dumpop(struct readelf * re,size_t si,const char * sn,int op,int t)75492c23cb7cSEd Maste find_dumpop(struct readelf *re, size_t si, const char *sn, int op, int t)
75502c23cb7cSEd Maste {
75512c23cb7cSEd Maste 	struct dumpop *d;
75522c23cb7cSEd Maste 
75532c23cb7cSEd Maste 	STAILQ_FOREACH(d, &re->v_dumpop, dumpop_list) {
75542c23cb7cSEd Maste 		if ((op == -1 || op & d->op) &&
75552c23cb7cSEd Maste 		    (t == -1 || (unsigned) t == d->type)) {
75562c23cb7cSEd Maste 			if ((d->type == DUMP_BY_INDEX && d->u.si == si) ||
75572c23cb7cSEd Maste 			    (d->type == DUMP_BY_NAME && !strcmp(d->u.sn, sn)))
75582c23cb7cSEd Maste 				return (d);
75592c23cb7cSEd Maste 		}
75602c23cb7cSEd Maste 	}
75612c23cb7cSEd Maste 
75622c23cb7cSEd Maste 	return (NULL);
75632c23cb7cSEd Maste }
75642c23cb7cSEd Maste 
75652c23cb7cSEd Maste static struct {
75662c23cb7cSEd Maste 	const char *ln;
75672c23cb7cSEd Maste 	char sn;
75682c23cb7cSEd Maste 	int value;
75692c23cb7cSEd Maste } dwarf_op[] = {
75702c23cb7cSEd Maste 	{"rawline", 'l', DW_L},
75712c23cb7cSEd Maste 	{"decodedline", 'L', DW_LL},
75722c23cb7cSEd Maste 	{"info", 'i', DW_I},
75732c23cb7cSEd Maste 	{"abbrev", 'a', DW_A},
75742c23cb7cSEd Maste 	{"pubnames", 'p', DW_P},
75752c23cb7cSEd Maste 	{"aranges", 'r', DW_R},
75762c23cb7cSEd Maste 	{"ranges", 'r', DW_R},
75772c23cb7cSEd Maste 	{"Ranges", 'R', DW_RR},
75782c23cb7cSEd Maste 	{"macro", 'm', DW_M},
75792c23cb7cSEd Maste 	{"frames", 'f', DW_F},
7580cf781b2eSEd Maste 	{"frames-interp", 'F', DW_FF},
75812c23cb7cSEd Maste 	{"str", 's', DW_S},
75822c23cb7cSEd Maste 	{"loc", 'o', DW_O},
75832c23cb7cSEd Maste 	{NULL, 0, 0}
75842c23cb7cSEd Maste };
75852c23cb7cSEd Maste 
75862c23cb7cSEd Maste static void
parse_dwarf_op_short(struct readelf * re,const char * op)75872c23cb7cSEd Maste parse_dwarf_op_short(struct readelf *re, const char *op)
75882c23cb7cSEd Maste {
75892c23cb7cSEd Maste 	int i;
75902c23cb7cSEd Maste 
75912c23cb7cSEd Maste 	if (op == NULL) {
75922c23cb7cSEd Maste 		re->dop |= DW_DEFAULT_OPTIONS;
75932c23cb7cSEd Maste 		return;
75942c23cb7cSEd Maste 	}
75952c23cb7cSEd Maste 
75962c23cb7cSEd Maste 	for (; *op != '\0'; op++) {
75972c23cb7cSEd Maste 		for (i = 0; dwarf_op[i].ln != NULL; i++) {
75982c23cb7cSEd Maste 			if (dwarf_op[i].sn == *op) {
75992c23cb7cSEd Maste 				re->dop |= dwarf_op[i].value;
76002c23cb7cSEd Maste 				break;
76012c23cb7cSEd Maste 			}
76022c23cb7cSEd Maste 		}
76032c23cb7cSEd Maste 	}
76042c23cb7cSEd Maste }
76052c23cb7cSEd Maste 
76062c23cb7cSEd Maste static void
parse_dwarf_op_long(struct readelf * re,const char * op)76072c23cb7cSEd Maste parse_dwarf_op_long(struct readelf *re, const char *op)
76082c23cb7cSEd Maste {
76092c23cb7cSEd Maste 	char *p, *token, *bp;
76102c23cb7cSEd Maste 	int i;
76112c23cb7cSEd Maste 
76122c23cb7cSEd Maste 	if (op == NULL) {
76132c23cb7cSEd Maste 		re->dop |= DW_DEFAULT_OPTIONS;
76142c23cb7cSEd Maste 		return;
76152c23cb7cSEd Maste 	}
76162c23cb7cSEd Maste 
76172c23cb7cSEd Maste 	if ((p = strdup(op)) == NULL)
76182c23cb7cSEd Maste 		err(EXIT_FAILURE, "strdup failed");
76192c23cb7cSEd Maste 	bp = p;
76202c23cb7cSEd Maste 
76212c23cb7cSEd Maste 	while ((token = strsep(&p, ",")) != NULL) {
76222c23cb7cSEd Maste 		for (i = 0; dwarf_op[i].ln != NULL; i++) {
76232c23cb7cSEd Maste 			if (!strcmp(token, dwarf_op[i].ln)) {
76242c23cb7cSEd Maste 				re->dop |= dwarf_op[i].value;
76252c23cb7cSEd Maste 				break;
76262c23cb7cSEd Maste 			}
76272c23cb7cSEd Maste 		}
76282c23cb7cSEd Maste 	}
76292c23cb7cSEd Maste 
76302c23cb7cSEd Maste 	free(bp);
76312c23cb7cSEd Maste }
76322c23cb7cSEd Maste 
76332c23cb7cSEd Maste static uint64_t
_read_lsb(Elf_Data * d,uint64_t * offsetp,int bytes_to_read)76342c23cb7cSEd Maste _read_lsb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read)
76352c23cb7cSEd Maste {
76362c23cb7cSEd Maste 	uint64_t ret;
76372c23cb7cSEd Maste 	uint8_t *src;
76382c23cb7cSEd Maste 
76392c23cb7cSEd Maste 	src = (uint8_t *) d->d_buf + *offsetp;
76402c23cb7cSEd Maste 
76412c23cb7cSEd Maste 	ret = 0;
76422c23cb7cSEd Maste 	switch (bytes_to_read) {
76432c23cb7cSEd Maste 	case 8:
76442c23cb7cSEd Maste 		ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
76452c23cb7cSEd Maste 		ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
7646839529caSEd Maste 		/* FALLTHROUGH */
76472c23cb7cSEd Maste 	case 4:
76482c23cb7cSEd Maste 		ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
7649839529caSEd Maste 		/* FALLTHROUGH */
76502c23cb7cSEd Maste 	case 2:
76512c23cb7cSEd Maste 		ret |= ((uint64_t) src[1]) << 8;
7652839529caSEd Maste 		/* FALLTHROUGH */
76532c23cb7cSEd Maste 	case 1:
76542c23cb7cSEd Maste 		ret |= src[0];
76552c23cb7cSEd Maste 		break;
76562c23cb7cSEd Maste 	default:
76572c23cb7cSEd Maste 		return (0);
76582c23cb7cSEd Maste 	}
76592c23cb7cSEd Maste 
76602c23cb7cSEd Maste 	*offsetp += bytes_to_read;
76612c23cb7cSEd Maste 
76622c23cb7cSEd Maste 	return (ret);
76632c23cb7cSEd Maste }
76642c23cb7cSEd Maste 
76652c23cb7cSEd Maste static uint64_t
_read_msb(Elf_Data * d,uint64_t * offsetp,int bytes_to_read)76662c23cb7cSEd Maste _read_msb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read)
76672c23cb7cSEd Maste {
76682c23cb7cSEd Maste 	uint64_t ret;
76692c23cb7cSEd Maste 	uint8_t *src;
76702c23cb7cSEd Maste 
76712c23cb7cSEd Maste 	src = (uint8_t *) d->d_buf + *offsetp;
76722c23cb7cSEd Maste 
76732c23cb7cSEd Maste 	switch (bytes_to_read) {
76742c23cb7cSEd Maste 	case 1:
76752c23cb7cSEd Maste 		ret = src[0];
76762c23cb7cSEd Maste 		break;
76772c23cb7cSEd Maste 	case 2:
76782c23cb7cSEd Maste 		ret = src[1] | ((uint64_t) src[0]) << 8;
76792c23cb7cSEd Maste 		break;
76802c23cb7cSEd Maste 	case 4:
76812c23cb7cSEd Maste 		ret = src[3] | ((uint64_t) src[2]) << 8;
76822c23cb7cSEd Maste 		ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24;
76832c23cb7cSEd Maste 		break;
76842c23cb7cSEd Maste 	case 8:
76852c23cb7cSEd Maste 		ret = src[7] | ((uint64_t) src[6]) << 8;
76862c23cb7cSEd Maste 		ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24;
76872c23cb7cSEd Maste 		ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40;
76882c23cb7cSEd Maste 		ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56;
76892c23cb7cSEd Maste 		break;
76902c23cb7cSEd Maste 	default:
76912c23cb7cSEd Maste 		return (0);
76922c23cb7cSEd Maste 	}
76932c23cb7cSEd Maste 
76942c23cb7cSEd Maste 	*offsetp += bytes_to_read;
76952c23cb7cSEd Maste 
76962c23cb7cSEd Maste 	return (ret);
76972c23cb7cSEd Maste }
76982c23cb7cSEd Maste 
76992c23cb7cSEd Maste static uint64_t
_decode_lsb(uint8_t ** data,int bytes_to_read)77002c23cb7cSEd Maste _decode_lsb(uint8_t **data, int bytes_to_read)
77012c23cb7cSEd Maste {
77022c23cb7cSEd Maste 	uint64_t ret;
77032c23cb7cSEd Maste 	uint8_t *src;
77042c23cb7cSEd Maste 
77052c23cb7cSEd Maste 	src = *data;
77062c23cb7cSEd Maste 
77072c23cb7cSEd Maste 	ret = 0;
77082c23cb7cSEd Maste 	switch (bytes_to_read) {
77092c23cb7cSEd Maste 	case 8:
77102c23cb7cSEd Maste 		ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
77112c23cb7cSEd Maste 		ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
7712839529caSEd Maste 		/* FALLTHROUGH */
77132c23cb7cSEd Maste 	case 4:
77142c23cb7cSEd Maste 		ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
7715839529caSEd Maste 		/* FALLTHROUGH */
77162c23cb7cSEd Maste 	case 2:
77172c23cb7cSEd Maste 		ret |= ((uint64_t) src[1]) << 8;
7718839529caSEd Maste 		/* FALLTHROUGH */
77192c23cb7cSEd Maste 	case 1:
77202c23cb7cSEd Maste 		ret |= src[0];
77212c23cb7cSEd Maste 		break;
77222c23cb7cSEd Maste 	default:
77232c23cb7cSEd Maste 		return (0);
77242c23cb7cSEd Maste 	}
77252c23cb7cSEd Maste 
77262c23cb7cSEd Maste 	*data += bytes_to_read;
77272c23cb7cSEd Maste 
77282c23cb7cSEd Maste 	return (ret);
77292c23cb7cSEd Maste }
77302c23cb7cSEd Maste 
77312c23cb7cSEd Maste static uint64_t
_decode_msb(uint8_t ** data,int bytes_to_read)77322c23cb7cSEd Maste _decode_msb(uint8_t **data, int bytes_to_read)
77332c23cb7cSEd Maste {
77342c23cb7cSEd Maste 	uint64_t ret;
77352c23cb7cSEd Maste 	uint8_t *src;
77362c23cb7cSEd Maste 
77372c23cb7cSEd Maste 	src = *data;
77382c23cb7cSEd Maste 
77392c23cb7cSEd Maste 	ret = 0;
77402c23cb7cSEd Maste 	switch (bytes_to_read) {
77412c23cb7cSEd Maste 	case 1:
77422c23cb7cSEd Maste 		ret = src[0];
77432c23cb7cSEd Maste 		break;
77442c23cb7cSEd Maste 	case 2:
77452c23cb7cSEd Maste 		ret = src[1] | ((uint64_t) src[0]) << 8;
77462c23cb7cSEd Maste 		break;
77472c23cb7cSEd Maste 	case 4:
77482c23cb7cSEd Maste 		ret = src[3] | ((uint64_t) src[2]) << 8;
77492c23cb7cSEd Maste 		ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24;
77502c23cb7cSEd Maste 		break;
77512c23cb7cSEd Maste 	case 8:
77522c23cb7cSEd Maste 		ret = src[7] | ((uint64_t) src[6]) << 8;
77532c23cb7cSEd Maste 		ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24;
77542c23cb7cSEd Maste 		ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40;
77552c23cb7cSEd Maste 		ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56;
77562c23cb7cSEd Maste 		break;
77572c23cb7cSEd Maste 	default:
77582c23cb7cSEd Maste 		return (0);
77592c23cb7cSEd Maste 		break;
77602c23cb7cSEd Maste 	}
77612c23cb7cSEd Maste 
77622c23cb7cSEd Maste 	*data += bytes_to_read;
77632c23cb7cSEd Maste 
77642c23cb7cSEd Maste 	return (ret);
77652c23cb7cSEd Maste }
77662c23cb7cSEd Maste 
77672c23cb7cSEd Maste static int64_t
_decode_sleb128(uint8_t ** dp,uint8_t * dpe)776895fd7f26SEd Maste _decode_sleb128(uint8_t **dp, uint8_t *dpe)
77692c23cb7cSEd Maste {
77702c23cb7cSEd Maste 	int64_t ret = 0;
77718f32e46dSKai Wang 	uint8_t b = 0;
77722c23cb7cSEd Maste 	int shift = 0;
77732c23cb7cSEd Maste 
77742c23cb7cSEd Maste 	uint8_t *src = *dp;
77752c23cb7cSEd Maste 
77762c23cb7cSEd Maste 	do {
777795fd7f26SEd Maste 		if (src >= dpe)
777895fd7f26SEd Maste 			break;
77792c23cb7cSEd Maste 		b = *src++;
77802c23cb7cSEd Maste 		ret |= ((b & 0x7f) << shift);
77812c23cb7cSEd Maste 		shift += 7;
77822c23cb7cSEd Maste 	} while ((b & 0x80) != 0);
77832c23cb7cSEd Maste 
77842c23cb7cSEd Maste 	if (shift < 32 && (b & 0x40) != 0)
77852c23cb7cSEd Maste 		ret |= (-1 << shift);
77862c23cb7cSEd Maste 
77872c23cb7cSEd Maste 	*dp = src;
77882c23cb7cSEd Maste 
77892c23cb7cSEd Maste 	return (ret);
77902c23cb7cSEd Maste }
77912c23cb7cSEd Maste 
77922c23cb7cSEd Maste static uint64_t
_decode_uleb128(uint8_t ** dp,uint8_t * dpe)779395fd7f26SEd Maste _decode_uleb128(uint8_t **dp, uint8_t *dpe)
77942c23cb7cSEd Maste {
77952c23cb7cSEd Maste 	uint64_t ret = 0;
77962c23cb7cSEd Maste 	uint8_t b;
77972c23cb7cSEd Maste 	int shift = 0;
77982c23cb7cSEd Maste 
77992c23cb7cSEd Maste 	uint8_t *src = *dp;
78002c23cb7cSEd Maste 
78012c23cb7cSEd Maste 	do {
780295fd7f26SEd Maste 		if (src >= dpe)
780395fd7f26SEd Maste 			break;
78042c23cb7cSEd Maste 		b = *src++;
78052c23cb7cSEd Maste 		ret |= ((b & 0x7f) << shift);
78062c23cb7cSEd Maste 		shift += 7;
78072c23cb7cSEd Maste 	} while ((b & 0x80) != 0);
78082c23cb7cSEd Maste 
78092c23cb7cSEd Maste 	*dp = src;
78102c23cb7cSEd Maste 
78112c23cb7cSEd Maste 	return (ret);
78122c23cb7cSEd Maste }
78132c23cb7cSEd Maste 
78142c23cb7cSEd Maste static void
readelf_version(void)78152c23cb7cSEd Maste readelf_version(void)
78162c23cb7cSEd Maste {
78172c23cb7cSEd Maste 	(void) printf("%s (%s)\n", ELFTC_GETPROGNAME(),
78182c23cb7cSEd Maste 	    elftc_version());
78192c23cb7cSEd Maste 	exit(EXIT_SUCCESS);
78202c23cb7cSEd Maste }
78212c23cb7cSEd Maste 
78222c23cb7cSEd Maste #define	USAGE_MESSAGE	"\
78232c23cb7cSEd Maste Usage: %s [options] file...\n\
78242c23cb7cSEd Maste   Display information about ELF objects and ar(1) archives.\n\n\
78252c23cb7cSEd Maste   Options:\n\
78262c23cb7cSEd Maste   -a | --all               Equivalent to specifying options '-dhIlrsASV'.\n\
78272c23cb7cSEd Maste   -c | --archive-index     Print the archive symbol table for archives.\n\
78282c23cb7cSEd Maste   -d | --dynamic           Print the contents of SHT_DYNAMIC sections.\n\
78292c23cb7cSEd Maste   -e | --headers           Print all headers in the object.\n\
78303ef90571SEd Maste   -g | --section-groups    Print the contents of the section groups.\n\
78312c23cb7cSEd Maste   -h | --file-header       Print the file header for the object.\n\
78322c23cb7cSEd Maste   -l | --program-headers   Print the PHDR table for the object.\n\
78332c23cb7cSEd Maste   -n | --notes             Print the contents of SHT_NOTE sections.\n\
78342c23cb7cSEd Maste   -p INDEX | --string-dump=INDEX\n\
78352c23cb7cSEd Maste                            Print the contents of section at index INDEX.\n\
78362c23cb7cSEd Maste   -r | --relocs            Print relocation information.\n\
78372c23cb7cSEd Maste   -s | --syms | --symbols  Print symbol tables.\n\
78382c23cb7cSEd Maste   -t | --section-details   Print additional information about sections.\n\
78392c23cb7cSEd Maste   -v | --version           Print a version identifier and exit.\n\
7840839529caSEd Maste   -w[afilmoprsFLR] | --debug-dump={abbrev,aranges,decodedline,frames,\n\
7841839529caSEd Maste                                frames-interp,info,loc,macro,pubnames,\n\
7842839529caSEd Maste                                ranges,Ranges,rawline,str}\n\
7843839529caSEd Maste                            Display DWARF information.\n\
78442c23cb7cSEd Maste   -x INDEX | --hex-dump=INDEX\n\
78452c23cb7cSEd Maste                            Display contents of a section as hexadecimal.\n\
7846e128bd0fSEd Maste   -z | --decompress        Decompress the contents of a section before displaying it.\n\
78472c23cb7cSEd Maste   -A | --arch-specific     (accepted, but ignored)\n\
78482c23cb7cSEd Maste   -D | --use-dynamic       Print the symbol table specified by the DT_SYMTAB\n\
78492c23cb7cSEd Maste                            entry in the \".dynamic\" section.\n\
78502c23cb7cSEd Maste   -H | --help              Print a help message.\n\
78512c23cb7cSEd Maste   -I | --histogram         Print information on bucket list lengths for \n\
78522c23cb7cSEd Maste                            hash sections.\n\
78532c23cb7cSEd Maste   -N | --full-section-name (accepted, but ignored)\n\
78542c23cb7cSEd Maste   -S | --sections | --section-headers\n\
78552c23cb7cSEd Maste                            Print information about section headers.\n\
78562c23cb7cSEd Maste   -V | --version-info      Print symbol versoning information.\n\
78572c23cb7cSEd Maste   -W | --wide              Print information without wrapping long lines.\n"
78582c23cb7cSEd Maste 
78592c23cb7cSEd Maste 
78602c23cb7cSEd Maste static void
readelf_usage(int status)7861839529caSEd Maste readelf_usage(int status)
78622c23cb7cSEd Maste {
78632c23cb7cSEd Maste 	fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());
7864839529caSEd Maste 	exit(status);
78652c23cb7cSEd Maste }
78662c23cb7cSEd Maste 
78672c23cb7cSEd Maste int
main(int argc,char ** argv)78682c23cb7cSEd Maste main(int argc, char **argv)
78692c23cb7cSEd Maste {
7870802c2095SMark Johnston 	cap_rights_t	rights;
7871802c2095SMark Johnston 	fileargs_t	*fa;
78722c23cb7cSEd Maste 	struct readelf	*re, re_storage;
78732c23cb7cSEd Maste 	unsigned long	 si;
7874ea444392SEd Maste 	int		 fd, opt, i, exit_code;
78752c23cb7cSEd Maste 	char		*ep;
78762c23cb7cSEd Maste 
78772c23cb7cSEd Maste 	re = &re_storage;
78782c23cb7cSEd Maste 	memset(re, 0, sizeof(*re));
78792c23cb7cSEd Maste 	STAILQ_INIT(&re->v_dumpop);
78802c23cb7cSEd Maste 
7881e128bd0fSEd Maste 	while ((opt = getopt_long(argc, argv, "AacDdegHhIi:lNnp:rSstuVvWw::x:z",
78822c23cb7cSEd Maste 	    longopts, NULL)) != -1) {
78832c23cb7cSEd Maste 		switch(opt) {
78842c23cb7cSEd Maste 		case '?':
7885839529caSEd Maste 			readelf_usage(EXIT_SUCCESS);
78862c23cb7cSEd Maste 			break;
78872c23cb7cSEd Maste 		case 'A':
78882c23cb7cSEd Maste 			re->options |= RE_AA;
78892c23cb7cSEd Maste 			break;
78902c23cb7cSEd Maste 		case 'a':
78913ef90571SEd Maste 			re->options |= RE_AA | RE_D | RE_G | RE_H | RE_II |
7892f161abf9SEd Maste 			    RE_L | RE_N | RE_R | RE_SS | RE_S | RE_U | RE_VV;
78932c23cb7cSEd Maste 			break;
78942c23cb7cSEd Maste 		case 'c':
78952c23cb7cSEd Maste 			re->options |= RE_C;
78962c23cb7cSEd Maste 			break;
78972c23cb7cSEd Maste 		case 'D':
78982c23cb7cSEd Maste 			re->options |= RE_DD;
78992c23cb7cSEd Maste 			break;
79002c23cb7cSEd Maste 		case 'd':
79012c23cb7cSEd Maste 			re->options |= RE_D;
79022c23cb7cSEd Maste 			break;
79032c23cb7cSEd Maste 		case 'e':
79042c23cb7cSEd Maste 			re->options |= RE_H | RE_L | RE_SS;
79052c23cb7cSEd Maste 			break;
79062c23cb7cSEd Maste 		case 'g':
79072c23cb7cSEd Maste 			re->options |= RE_G;
79082c23cb7cSEd Maste 			break;
79092c23cb7cSEd Maste 		case 'H':
7910839529caSEd Maste 			readelf_usage(EXIT_SUCCESS);
79112c23cb7cSEd Maste 			break;
79122c23cb7cSEd Maste 		case 'h':
79132c23cb7cSEd Maste 			re->options |= RE_H;
79142c23cb7cSEd Maste 			break;
79152c23cb7cSEd Maste 		case 'I':
79162c23cb7cSEd Maste 			re->options |= RE_II;
79172c23cb7cSEd Maste 			break;
79182c23cb7cSEd Maste 		case 'i':
79192c23cb7cSEd Maste 			/* Not implemented yet. */
79202c23cb7cSEd Maste 			break;
79212c23cb7cSEd Maste 		case 'l':
79222c23cb7cSEd Maste 			re->options |= RE_L;
79232c23cb7cSEd Maste 			break;
79242c23cb7cSEd Maste 		case 'N':
79252c23cb7cSEd Maste 			re->options |= RE_NN;
79262c23cb7cSEd Maste 			break;
79272c23cb7cSEd Maste 		case 'n':
79282c23cb7cSEd Maste 			re->options |= RE_N;
79292c23cb7cSEd Maste 			break;
79302c23cb7cSEd Maste 		case 'p':
79312c23cb7cSEd Maste 			re->options |= RE_P;
79322c23cb7cSEd Maste 			si = strtoul(optarg, &ep, 10);
79332c23cb7cSEd Maste 			if (*ep == '\0')
79342c23cb7cSEd Maste 				add_dumpop(re, (size_t) si, NULL, STR_DUMP,
79352c23cb7cSEd Maste 				    DUMP_BY_INDEX);
79362c23cb7cSEd Maste 			else
79372c23cb7cSEd Maste 				add_dumpop(re, 0, optarg, STR_DUMP,
79382c23cb7cSEd Maste 				    DUMP_BY_NAME);
79392c23cb7cSEd Maste 			break;
79402c23cb7cSEd Maste 		case 'r':
79412c23cb7cSEd Maste 			re->options |= RE_R;
79422c23cb7cSEd Maste 			break;
79432c23cb7cSEd Maste 		case 'S':
79442c23cb7cSEd Maste 			re->options |= RE_SS;
79452c23cb7cSEd Maste 			break;
79462c23cb7cSEd Maste 		case 's':
79472c23cb7cSEd Maste 			re->options |= RE_S;
79482c23cb7cSEd Maste 			break;
79492c23cb7cSEd Maste 		case 't':
7950718699beSMark Johnston 			re->options |= RE_SS | RE_T;
79512c23cb7cSEd Maste 			break;
79522c23cb7cSEd Maste 		case 'u':
79532c23cb7cSEd Maste 			re->options |= RE_U;
79542c23cb7cSEd Maste 			break;
79552c23cb7cSEd Maste 		case 'V':
79562c23cb7cSEd Maste 			re->options |= RE_VV;
79572c23cb7cSEd Maste 			break;
79582c23cb7cSEd Maste 		case 'v':
79592c23cb7cSEd Maste 			readelf_version();
79602c23cb7cSEd Maste 			break;
79612c23cb7cSEd Maste 		case 'W':
79622c23cb7cSEd Maste 			re->options |= RE_WW;
79632c23cb7cSEd Maste 			break;
79642c23cb7cSEd Maste 		case 'w':
79652c23cb7cSEd Maste 			re->options |= RE_W;
79662c23cb7cSEd Maste 			parse_dwarf_op_short(re, optarg);
79672c23cb7cSEd Maste 			break;
79682c23cb7cSEd Maste 		case 'x':
79692c23cb7cSEd Maste 			re->options |= RE_X;
79702c23cb7cSEd Maste 			si = strtoul(optarg, &ep, 10);
79712c23cb7cSEd Maste 			if (*ep == '\0')
79722c23cb7cSEd Maste 				add_dumpop(re, (size_t) si, NULL, HEX_DUMP,
79732c23cb7cSEd Maste 				    DUMP_BY_INDEX);
79742c23cb7cSEd Maste 			else
79752c23cb7cSEd Maste 				add_dumpop(re, 0, optarg, HEX_DUMP,
79762c23cb7cSEd Maste 				    DUMP_BY_NAME);
79772c23cb7cSEd Maste 			break;
7978e128bd0fSEd Maste 		case 'z':
7979e128bd0fSEd Maste 			re->options |= RE_Z;
7980e128bd0fSEd Maste 			break;
79812c23cb7cSEd Maste 		case OPTION_DEBUG_DUMP:
79822c23cb7cSEd Maste 			re->options |= RE_W;
79832c23cb7cSEd Maste 			parse_dwarf_op_long(re, optarg);
79842c23cb7cSEd Maste 		}
79852c23cb7cSEd Maste 	}
79862c23cb7cSEd Maste 
79872c23cb7cSEd Maste 	argv += optind;
79882c23cb7cSEd Maste 	argc -= optind;
79892c23cb7cSEd Maste 
79902c23cb7cSEd Maste 	if (argc == 0 || re->options == 0)
7991839529caSEd Maste 		readelf_usage(EXIT_FAILURE);
79922c23cb7cSEd Maste 
79932c23cb7cSEd Maste 	if (argc > 1)
79942c23cb7cSEd Maste 		re->flags |= DISPLAY_FILENAME;
79952c23cb7cSEd Maste 
79962c23cb7cSEd Maste 	if (elf_version(EV_CURRENT) == EV_NONE)
79972c23cb7cSEd Maste 		errx(EXIT_FAILURE, "ELF library initialization failed: %s",
79982c23cb7cSEd Maste 		    elf_errmsg(-1));
79992c23cb7cSEd Maste 
8000802c2095SMark Johnston 	cap_rights_init(&rights, CAP_FCNTL, CAP_FSTAT, CAP_MMAP_R, CAP_SEEK);
8001802c2095SMark Johnston 	fa = fileargs_init(argc, argv, O_RDONLY, 0, &rights, FA_OPEN);
8002802c2095SMark Johnston 	if (fa == NULL)
8003802c2095SMark Johnston 		err(1, "Unable to initialize casper fileargs");
8004802c2095SMark Johnston 
8005802c2095SMark Johnston 	caph_cache_catpages();
8006802c2095SMark Johnston 	if (caph_limit_stdio() < 0) {
8007802c2095SMark Johnston 		fileargs_free(fa);
8008802c2095SMark Johnston 		err(1, "Unable to limit stdio rights");
8009802c2095SMark Johnston 	}
8010802c2095SMark Johnston 	if (caph_enter_casper() < 0) {
8011802c2095SMark Johnston 		fileargs_free(fa);
8012802c2095SMark Johnston 		err(1, "Unable to enter capability mode");
8013802c2095SMark Johnston 	}
8014802c2095SMark Johnston 
8015ea444392SEd Maste 	exit_code = EXIT_SUCCESS;
8016b00fe64fSEd Maste 	for (i = 0; i < argc; i++) {
80172c23cb7cSEd Maste 		re->filename = argv[i];
8018802c2095SMark Johnston 		fd = fileargs_open(fa, re->filename);
801919669671SMark Johnston 		if (fd < 0) {
8020802c2095SMark Johnston 			warn("open %s failed", re->filename);
8021ea444392SEd Maste 			exit_code = EXIT_FAILURE;
802219669671SMark Johnston 		} else {
8023ea444392SEd Maste 			if (!dump_object(re, fd))
8024ea444392SEd Maste 				exit_code = EXIT_FAILURE;
802519669671SMark Johnston 			close(fd);
802619669671SMark Johnston 		}
80272c23cb7cSEd Maste 	}
80282c23cb7cSEd Maste 
8029ea444392SEd Maste 	exit(exit_code);
80302c23cb7cSEd Maste }
8031