xref: /freebsd/contrib/elftoolchain/readelf/readelf.c (revision e128bd0ff979799b32ae926c3264f11804ab0977)
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>
43*e128bd0fSEd 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>
50*e128bd0fSEd 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
92*e128bd0fSEd 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},
195*e128bd0fSEd 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 
232d003e0d7SEd Maste struct mips_option {
2336b2779a0SEd Maste 	uint64_t flag;
2346b2779a0SEd Maste 	const char *desc;
2356b2779a0SEd Maste };
2366b2779a0SEd Maste 
2379817bae9SEd Maste struct loc_at {
2389817bae9SEd Maste 	Dwarf_Attribute la_at;
2399817bae9SEd Maste 	Dwarf_Unsigned la_off;
2409817bae9SEd Maste 	Dwarf_Unsigned la_lowpc;
2419817bae9SEd Maste 	Dwarf_Half la_cu_psize;
2429817bae9SEd Maste 	Dwarf_Half la_cu_osize;
2439817bae9SEd Maste 	Dwarf_Half la_cu_ver;
2449817bae9SEd Maste };
2459817bae9SEd Maste 
2462c23cb7cSEd Maste static void add_dumpop(struct readelf *re, size_t si, const char *sn, int op,
2472c23cb7cSEd Maste     int t);
2482c23cb7cSEd Maste static const char *aeabi_adv_simd_arch(uint64_t simd);
2492c23cb7cSEd Maste static const char *aeabi_align_needed(uint64_t an);
2502c23cb7cSEd Maste static const char *aeabi_align_preserved(uint64_t ap);
2512c23cb7cSEd Maste static const char *aeabi_arm_isa(uint64_t ai);
2522c23cb7cSEd Maste static const char *aeabi_cpu_arch(uint64_t arch);
2532c23cb7cSEd Maste static const char *aeabi_cpu_arch_profile(uint64_t pf);
2542c23cb7cSEd Maste static const char *aeabi_div(uint64_t du);
2552c23cb7cSEd Maste static const char *aeabi_enum_size(uint64_t es);
2562c23cb7cSEd Maste static const char *aeabi_fp_16bit_format(uint64_t fp16);
2572c23cb7cSEd Maste static const char *aeabi_fp_arch(uint64_t fp);
2582c23cb7cSEd Maste static const char *aeabi_fp_denormal(uint64_t fd);
2592c23cb7cSEd Maste static const char *aeabi_fp_exceptions(uint64_t fe);
2602c23cb7cSEd Maste static const char *aeabi_fp_hpext(uint64_t fh);
2612c23cb7cSEd Maste static const char *aeabi_fp_number_model(uint64_t fn);
2622c23cb7cSEd Maste static const char *aeabi_fp_optm_goal(uint64_t fog);
2632c23cb7cSEd Maste static const char *aeabi_fp_rounding(uint64_t fr);
2642c23cb7cSEd Maste static const char *aeabi_hardfp(uint64_t hfp);
2652c23cb7cSEd Maste static const char *aeabi_mpext(uint64_t mp);
2662c23cb7cSEd Maste static const char *aeabi_optm_goal(uint64_t og);
2672c23cb7cSEd Maste static const char *aeabi_pcs_config(uint64_t pcs);
2682c23cb7cSEd Maste static const char *aeabi_pcs_got(uint64_t got);
2692c23cb7cSEd Maste static const char *aeabi_pcs_r9(uint64_t r9);
2702c23cb7cSEd Maste static const char *aeabi_pcs_ro(uint64_t ro);
2712c23cb7cSEd Maste static const char *aeabi_pcs_rw(uint64_t rw);
2722c23cb7cSEd Maste static const char *aeabi_pcs_wchar_t(uint64_t wt);
2732c23cb7cSEd Maste static const char *aeabi_t2ee(uint64_t t2ee);
2742c23cb7cSEd Maste static const char *aeabi_thumb_isa(uint64_t ti);
2752c23cb7cSEd Maste static const char *aeabi_fp_user_exceptions(uint64_t fu);
2762c23cb7cSEd Maste static const char *aeabi_unaligned_access(uint64_t ua);
2772c23cb7cSEd Maste static const char *aeabi_vfp_args(uint64_t va);
2782c23cb7cSEd Maste static const char *aeabi_virtual(uint64_t vt);
2792c23cb7cSEd Maste static const char *aeabi_wmmx_arch(uint64_t wmmx);
2802c23cb7cSEd Maste static const char *aeabi_wmmx_args(uint64_t wa);
2812c23cb7cSEd Maste static const char *elf_class(unsigned int class);
2822c23cb7cSEd Maste static const char *elf_endian(unsigned int endian);
2832c23cb7cSEd Maste static const char *elf_machine(unsigned int mach);
2842c23cb7cSEd Maste static const char *elf_osabi(unsigned int abi);
2852c23cb7cSEd Maste static const char *elf_type(unsigned int type);
2862c23cb7cSEd Maste static const char *elf_ver(unsigned int ver);
2872c23cb7cSEd Maste static const char *dt_type(unsigned int mach, unsigned int dtype);
2882c23cb7cSEd Maste static void dump_ar(struct readelf *re, int);
2892c23cb7cSEd Maste static void dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe);
2902c23cb7cSEd Maste static void dump_attributes(struct readelf *re);
29195fd7f26SEd Maste static uint8_t *dump_compatibility_tag(uint8_t *p, uint8_t *pe);
2922c23cb7cSEd Maste static void dump_dwarf(struct readelf *re);
293cf781b2eSEd Maste static void dump_dwarf_abbrev(struct readelf *re);
294cf781b2eSEd Maste static void dump_dwarf_aranges(struct readelf *re);
295cf781b2eSEd Maste static void dump_dwarf_block(struct readelf *re, uint8_t *b,
296cf781b2eSEd Maste     Dwarf_Unsigned len);
297cf781b2eSEd Maste static void dump_dwarf_die(struct readelf *re, Dwarf_Die die, int level);
298cf781b2eSEd Maste static void dump_dwarf_frame(struct readelf *re, int alt);
299cf781b2eSEd Maste static void dump_dwarf_frame_inst(struct readelf *re, Dwarf_Cie cie,
300cf781b2eSEd Maste     uint8_t *insts, Dwarf_Unsigned len, Dwarf_Unsigned caf, Dwarf_Signed daf,
301cf781b2eSEd Maste     Dwarf_Addr pc, Dwarf_Debug dbg);
302cf781b2eSEd Maste static int dump_dwarf_frame_regtable(struct readelf *re, Dwarf_Fde fde,
303cf781b2eSEd Maste     Dwarf_Addr pc, Dwarf_Unsigned func_len, Dwarf_Half cie_ra);
304cf781b2eSEd Maste static void dump_dwarf_frame_section(struct readelf *re, struct section *s,
305cf781b2eSEd Maste     int alt);
306cf781b2eSEd Maste static void dump_dwarf_info(struct readelf *re, Dwarf_Bool is_info);
307cf781b2eSEd Maste static void dump_dwarf_macinfo(struct readelf *re);
308cf781b2eSEd Maste static void dump_dwarf_line(struct readelf *re);
309cf781b2eSEd Maste static void dump_dwarf_line_decoded(struct readelf *re);
310cf781b2eSEd Maste static void dump_dwarf_loc(struct readelf *re, Dwarf_Loc *lr);
311cf781b2eSEd Maste static void dump_dwarf_loclist(struct readelf *re);
312cf781b2eSEd Maste static void dump_dwarf_pubnames(struct readelf *re);
313cf781b2eSEd Maste static void dump_dwarf_ranges(struct readelf *re);
314cf781b2eSEd Maste static void dump_dwarf_ranges_foreach(struct readelf *re, Dwarf_Die die,
315cf781b2eSEd Maste     Dwarf_Addr base);
316cf781b2eSEd Maste static void dump_dwarf_str(struct readelf *re);
3172c23cb7cSEd Maste static void dump_eflags(struct readelf *re, uint64_t e_flags);
3182c23cb7cSEd Maste static void dump_elf(struct readelf *re);
3196b2779a0SEd Maste static void dump_flags(struct flag_desc *fd, uint64_t flags);
3202c23cb7cSEd Maste static void dump_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab);
3212c23cb7cSEd Maste static void dump_dynamic(struct readelf *re);
3222c23cb7cSEd Maste static void dump_liblist(struct readelf *re);
323e9f3f88aSEd Maste static void dump_mips_abiflags(struct readelf *re, struct section *s);
3242c23cb7cSEd Maste static void dump_mips_attributes(struct readelf *re, uint8_t *p, uint8_t *pe);
3252c23cb7cSEd Maste static void dump_mips_odk_reginfo(struct readelf *re, uint8_t *p, size_t sz);
3262c23cb7cSEd Maste static void dump_mips_options(struct readelf *re, struct section *s);
3272c23cb7cSEd Maste static void dump_mips_option_flags(const char *name, struct mips_option *opt,
3282c23cb7cSEd Maste     uint64_t info);
3292c23cb7cSEd Maste static void dump_mips_reginfo(struct readelf *re, struct section *s);
3302c23cb7cSEd Maste static void dump_mips_specific_info(struct readelf *re);
3312c23cb7cSEd Maste static void dump_notes(struct readelf *re);
3322c23cb7cSEd Maste static void dump_notes_content(struct readelf *re, const char *buf, size_t sz,
3332c23cb7cSEd Maste     off_t off);
33414a345d9SEd Maste static void dump_notes_data(struct readelf *re, const char *name,
33514a345d9SEd Maste     uint32_t type, const char *buf, size_t sz);
3362c23cb7cSEd Maste static void dump_svr4_hash(struct section *s);
3372c23cb7cSEd Maste static void dump_svr4_hash64(struct readelf *re, struct section *s);
3382c23cb7cSEd Maste static void dump_gnu_hash(struct readelf *re, struct section *s);
33914a345d9SEd Maste static void dump_gnu_property_type_0(struct readelf *re, const char *buf,
34014a345d9SEd Maste     size_t sz);
3412c23cb7cSEd Maste static void dump_hash(struct readelf *re);
3422c23cb7cSEd Maste static void dump_phdr(struct readelf *re);
3432c23cb7cSEd Maste static void dump_ppc_attributes(uint8_t *p, uint8_t *pe);
3443ef90571SEd Maste static void dump_section_groups(struct readelf *re);
3452c23cb7cSEd Maste static void dump_symtab(struct readelf *re, int i);
3462c23cb7cSEd Maste static void dump_symtabs(struct readelf *re);
34795fd7f26SEd Maste static uint8_t *dump_unknown_tag(uint64_t tag, uint8_t *p, uint8_t *pe);
3482c23cb7cSEd Maste static void dump_ver(struct readelf *re);
3492c23cb7cSEd Maste static void dump_verdef(struct readelf *re, int dump);
3502c23cb7cSEd Maste static void dump_verneed(struct readelf *re, int dump);
3512c23cb7cSEd Maste static void dump_versym(struct readelf *re);
352cf781b2eSEd Maste static const char *dwarf_reg(unsigned int mach, unsigned int reg);
353cf781b2eSEd Maste static const char *dwarf_regname(struct readelf *re, unsigned int num);
354cf781b2eSEd Maste static struct dumpop *find_dumpop(struct readelf *re, size_t si,
355cf781b2eSEd Maste     const char *sn, int op, int t);
35671edbbfdSEd Maste static int get_ent_count(struct section *s, int *ent_count);
357e9f3f88aSEd Maste static int get_mips_register_size(uint8_t flag);
358cf781b2eSEd Maste static char *get_regoff_str(struct readelf *re, Dwarf_Half reg,
359cf781b2eSEd Maste     Dwarf_Addr off);
3602c23cb7cSEd Maste static const char *get_string(struct readelf *re, int strtab, size_t off);
3612c23cb7cSEd Maste static const char *get_symbol_name(struct readelf *re, int symtab, int i);
3622c23cb7cSEd Maste static uint64_t get_symbol_value(struct readelf *re, int symtab, int i);
3632c23cb7cSEd Maste static void load_sections(struct readelf *re);
3649817bae9SEd Maste static int loc_at_comparator(const void *la1, const void *la2);
3652c23cb7cSEd Maste static const char *mips_abi_fp(uint64_t fp);
36602b08c90SEd Maste static const char *note_type(const char *note_name, unsigned int et,
3672c23cb7cSEd Maste     unsigned int nt);
36802b08c90SEd Maste static const char *note_type_freebsd(unsigned int nt);
36902b08c90SEd Maste static const char *note_type_freebsd_core(unsigned int nt);
37002b08c90SEd Maste static const char *note_type_linux_core(unsigned int nt);
37102b08c90SEd Maste static const char *note_type_gnu(unsigned int nt);
37202b08c90SEd Maste static const char *note_type_netbsd(unsigned int nt);
37302b08c90SEd Maste static const char *note_type_openbsd(unsigned int nt);
37402b08c90SEd Maste static const char *note_type_unknown(unsigned int nt);
375895f86f1SEd Maste static const char *note_type_xen(unsigned int nt);
3762c23cb7cSEd Maste static const char *option_kind(uint8_t kind);
37720136ffcSEd Maste static const char *phdr_type(unsigned int mach, unsigned int ptype);
3782c23cb7cSEd Maste static const char *ppc_abi_fp(uint64_t fp);
3792c23cb7cSEd Maste static const char *ppc_abi_vector(uint64_t vec);
380839529caSEd Maste static void readelf_usage(int status);
3812c23cb7cSEd Maste static void readelf_version(void);
382cf781b2eSEd Maste static void search_loclist_at(struct readelf *re, Dwarf_Die die,
3839817bae9SEd Maste     Dwarf_Unsigned lowpc, struct loc_at **la_list,
384cb12a690SEd Maste     size_t *la_list_len, size_t *la_list_cap);
3852c23cb7cSEd Maste static void search_ver(struct readelf *re);
3862c23cb7cSEd Maste static const char *section_type(unsigned int mach, unsigned int stype);
387cf781b2eSEd Maste static void set_cu_context(struct readelf *re, Dwarf_Half psize,
388cf781b2eSEd Maste     Dwarf_Half osize, Dwarf_Half ver);
3892c23cb7cSEd Maste static const char *st_bind(unsigned int sbind);
3902c23cb7cSEd Maste static const char *st_shndx(unsigned int shndx);
391b6b6f9ccSEd Maste static const char *st_type(unsigned int mach, unsigned int os,
392b6b6f9ccSEd Maste     unsigned int stype);
3932c23cb7cSEd Maste static const char *st_vis(unsigned int svis);
3942c23cb7cSEd Maste static const char *top_tag(unsigned int tag);
3952c23cb7cSEd Maste static void unload_sections(struct readelf *re);
3962c23cb7cSEd Maste static uint64_t _read_lsb(Elf_Data *d, uint64_t *offsetp,
3972c23cb7cSEd Maste     int bytes_to_read);
3982c23cb7cSEd Maste static uint64_t _read_msb(Elf_Data *d, uint64_t *offsetp,
3992c23cb7cSEd Maste     int bytes_to_read);
4002c23cb7cSEd Maste static uint64_t _decode_lsb(uint8_t **data, int bytes_to_read);
4012c23cb7cSEd Maste static uint64_t _decode_msb(uint8_t **data, int bytes_to_read);
40295fd7f26SEd Maste static int64_t _decode_sleb128(uint8_t **dp, uint8_t *dpe);
40395fd7f26SEd Maste static uint64_t _decode_uleb128(uint8_t **dp, uint8_t *dpe);
4042c23cb7cSEd Maste 
4052c23cb7cSEd Maste static struct eflags_desc arm_eflags_desc[] = {
4062c23cb7cSEd Maste 	{EF_ARM_RELEXEC, "relocatable executable"},
4072c23cb7cSEd Maste 	{EF_ARM_HASENTRY, "has entry point"},
4082c23cb7cSEd Maste 	{EF_ARM_SYMSARESORTED, "sorted symbol tables"},
4092c23cb7cSEd Maste 	{EF_ARM_DYNSYMSUSESEGIDX, "dynamic symbols use segment index"},
4102c23cb7cSEd Maste 	{EF_ARM_MAPSYMSFIRST, "mapping symbols precede others"},
4112c23cb7cSEd Maste 	{EF_ARM_BE8, "BE8"},
4122c23cb7cSEd Maste 	{EF_ARM_LE8, "LE8"},
4132c23cb7cSEd Maste 	{EF_ARM_INTERWORK, "interworking enabled"},
4142c23cb7cSEd Maste 	{EF_ARM_APCS_26, "uses APCS/26"},
4152c23cb7cSEd Maste 	{EF_ARM_APCS_FLOAT, "uses APCS/float"},
4162c23cb7cSEd Maste 	{EF_ARM_PIC, "position independent"},
4172c23cb7cSEd Maste 	{EF_ARM_ALIGN8, "8 bit structure alignment"},
4182c23cb7cSEd Maste 	{EF_ARM_NEW_ABI, "uses new ABI"},
4192c23cb7cSEd Maste 	{EF_ARM_OLD_ABI, "uses old ABI"},
4202c23cb7cSEd Maste 	{EF_ARM_SOFT_FLOAT, "software FP"},
4212c23cb7cSEd Maste 	{EF_ARM_VFP_FLOAT, "VFP"},
4222c23cb7cSEd Maste 	{EF_ARM_MAVERICK_FLOAT, "Maverick FP"},
4232c23cb7cSEd Maste 	{0, NULL}
4242c23cb7cSEd Maste };
4252c23cb7cSEd Maste 
4262c23cb7cSEd Maste static struct eflags_desc mips_eflags_desc[] = {
4272c23cb7cSEd Maste 	{EF_MIPS_NOREORDER, "noreorder"},
4282c23cb7cSEd Maste 	{EF_MIPS_PIC, "pic"},
4292c23cb7cSEd Maste 	{EF_MIPS_CPIC, "cpic"},
4302c23cb7cSEd Maste 	{EF_MIPS_UCODE, "ugen_reserved"},
4312c23cb7cSEd Maste 	{EF_MIPS_ABI2, "abi2"},
4322c23cb7cSEd Maste 	{EF_MIPS_OPTIONS_FIRST, "odk first"},
4332c23cb7cSEd Maste 	{EF_MIPS_ARCH_ASE_MDMX, "mdmx"},
4342c23cb7cSEd Maste 	{EF_MIPS_ARCH_ASE_M16, "mips16"},
4352c23cb7cSEd Maste 	{0, NULL}
4362c23cb7cSEd Maste };
4372c23cb7cSEd Maste 
4382c23cb7cSEd Maste static struct eflags_desc powerpc_eflags_desc[] = {
4392c23cb7cSEd Maste 	{EF_PPC_EMB, "emb"},
4402c23cb7cSEd Maste 	{EF_PPC_RELOCATABLE, "relocatable"},
4412c23cb7cSEd Maste 	{EF_PPC_RELOCATABLE_LIB, "relocatable-lib"},
4422c23cb7cSEd Maste 	{0, NULL}
4432c23cb7cSEd Maste };
4442c23cb7cSEd Maste 
445b0084180SMitchell Horne static struct eflags_desc riscv_eflags_desc[] = {
446b0084180SMitchell Horne 	{EF_RISCV_RVC, "RVC"},
447b0084180SMitchell Horne 	{EF_RISCV_RVE, "RVE"},
448b0084180SMitchell Horne 	{EF_RISCV_TSO, "TSO"},
449b0084180SMitchell Horne 	{0, NULL}
450b0084180SMitchell Horne };
451b0084180SMitchell Horne 
4522c23cb7cSEd Maste static struct eflags_desc sparc_eflags_desc[] = {
4532c23cb7cSEd Maste 	{EF_SPARC_32PLUS, "v8+"},
4542c23cb7cSEd Maste 	{EF_SPARC_SUN_US1, "ultrasparcI"},
4552c23cb7cSEd Maste 	{EF_SPARC_HAL_R1, "halr1"},
4562c23cb7cSEd Maste 	{EF_SPARC_SUN_US3, "ultrasparcIII"},
4572c23cb7cSEd Maste 	{0, NULL}
4582c23cb7cSEd Maste };
4592c23cb7cSEd Maste 
4602c23cb7cSEd Maste static const char *
4612c23cb7cSEd Maste elf_osabi(unsigned int abi)
4622c23cb7cSEd Maste {
4632c23cb7cSEd Maste 	static char s_abi[32];
4642c23cb7cSEd Maste 
4652c23cb7cSEd Maste 	switch(abi) {
466453b09caSEd Maste 	case ELFOSABI_NONE: return "NONE";
467473c31f1SEd Maste 	case ELFOSABI_HPUX: return "HPUX";
4682c23cb7cSEd Maste 	case ELFOSABI_NETBSD: return "NetBSD";
4692c23cb7cSEd Maste 	case ELFOSABI_GNU: return "GNU";
4702c23cb7cSEd Maste 	case ELFOSABI_HURD: return "HURD";
4712c23cb7cSEd Maste 	case ELFOSABI_86OPEN: return "86OPEN";
4722c23cb7cSEd Maste 	case ELFOSABI_SOLARIS: return "Solaris";
4732c23cb7cSEd Maste 	case ELFOSABI_AIX: return "AIX";
4742c23cb7cSEd Maste 	case ELFOSABI_IRIX: return "IRIX";
4752c23cb7cSEd Maste 	case ELFOSABI_FREEBSD: return "FreeBSD";
4762c23cb7cSEd Maste 	case ELFOSABI_TRU64: return "TRU64";
4772c23cb7cSEd Maste 	case ELFOSABI_MODESTO: return "MODESTO";
4782c23cb7cSEd Maste 	case ELFOSABI_OPENBSD: return "OpenBSD";
4792c23cb7cSEd Maste 	case ELFOSABI_OPENVMS: return "OpenVMS";
4802c23cb7cSEd Maste 	case ELFOSABI_NSK: return "NSK";
481b6b6f9ccSEd Maste 	case ELFOSABI_CLOUDABI: return "CloudABI";
482b6d812d2SEd Maste 	case ELFOSABI_ARM_AEABI: return "ARM EABI";
4832c23cb7cSEd Maste 	case ELFOSABI_ARM: return "ARM";
4842c23cb7cSEd Maste 	case ELFOSABI_STANDALONE: return "StandAlone";
4852c23cb7cSEd Maste 	default:
4862c23cb7cSEd Maste 		snprintf(s_abi, sizeof(s_abi), "<unknown: %#x>", abi);
4872c23cb7cSEd Maste 		return (s_abi);
4882c23cb7cSEd Maste 	}
4892c23cb7cSEd Maste };
4902c23cb7cSEd Maste 
4912c23cb7cSEd Maste static const char *
4922c23cb7cSEd Maste elf_machine(unsigned int mach)
4932c23cb7cSEd Maste {
4942c23cb7cSEd Maste 	static char s_mach[32];
4952c23cb7cSEd Maste 
4962c23cb7cSEd Maste 	switch (mach) {
4972c23cb7cSEd Maste 	case EM_NONE: return "Unknown machine";
4982c23cb7cSEd Maste 	case EM_M32: return "AT&T WE32100";
4992c23cb7cSEd Maste 	case EM_SPARC: return "Sun SPARC";
5002c23cb7cSEd Maste 	case EM_386: return "Intel i386";
5012c23cb7cSEd Maste 	case EM_68K: return "Motorola 68000";
5023ef90571SEd Maste 	case EM_IAMCU: return "Intel MCU";
5032c23cb7cSEd Maste 	case EM_88K: return "Motorola 88000";
5042c23cb7cSEd Maste 	case EM_860: return "Intel i860";
5052c23cb7cSEd Maste 	case EM_MIPS: return "MIPS R3000 Big-Endian only";
5062c23cb7cSEd Maste 	case EM_S370: return "IBM System/370";
5072c23cb7cSEd Maste 	case EM_MIPS_RS3_LE: return "MIPS R3000 Little-Endian";
5082c23cb7cSEd Maste 	case EM_PARISC: return "HP PA-RISC";
5092c23cb7cSEd Maste 	case EM_VPP500: return "Fujitsu VPP500";
5102c23cb7cSEd Maste 	case EM_SPARC32PLUS: return "SPARC v8plus";
5112c23cb7cSEd Maste 	case EM_960: return "Intel 80960";
5122c23cb7cSEd Maste 	case EM_PPC: return "PowerPC 32-bit";
5132c23cb7cSEd Maste 	case EM_PPC64: return "PowerPC 64-bit";
5142c23cb7cSEd Maste 	case EM_S390: return "IBM System/390";
5152c23cb7cSEd Maste 	case EM_V800: return "NEC V800";
5162c23cb7cSEd Maste 	case EM_FR20: return "Fujitsu FR20";
5172c23cb7cSEd Maste 	case EM_RH32: return "TRW RH-32";
5182c23cb7cSEd Maste 	case EM_RCE: return "Motorola RCE";
5192c23cb7cSEd Maste 	case EM_ARM: return "ARM";
5202c23cb7cSEd Maste 	case EM_SH: return "Hitachi SH";
5212c23cb7cSEd Maste 	case EM_SPARCV9: return "SPARC v9 64-bit";
5222c23cb7cSEd Maste 	case EM_TRICORE: return "Siemens TriCore embedded processor";
5232c23cb7cSEd Maste 	case EM_ARC: return "Argonaut RISC Core";
5242c23cb7cSEd Maste 	case EM_H8_300: return "Hitachi H8/300";
5252c23cb7cSEd Maste 	case EM_H8_300H: return "Hitachi H8/300H";
5262c23cb7cSEd Maste 	case EM_H8S: return "Hitachi H8S";
5272c23cb7cSEd Maste 	case EM_H8_500: return "Hitachi H8/500";
5282c23cb7cSEd Maste 	case EM_IA_64: return "Intel IA-64 Processor";
5292c23cb7cSEd Maste 	case EM_MIPS_X: return "Stanford MIPS-X";
5302c23cb7cSEd Maste 	case EM_COLDFIRE: return "Motorola ColdFire";
5312c23cb7cSEd Maste 	case EM_68HC12: return "Motorola M68HC12";
5322c23cb7cSEd Maste 	case EM_MMA: return "Fujitsu MMA";
5332c23cb7cSEd Maste 	case EM_PCP: return "Siemens PCP";
5342c23cb7cSEd Maste 	case EM_NCPU: return "Sony nCPU";
5352c23cb7cSEd Maste 	case EM_NDR1: return "Denso NDR1 microprocessor";
5362c23cb7cSEd Maste 	case EM_STARCORE: return "Motorola Star*Core processor";
5372c23cb7cSEd Maste 	case EM_ME16: return "Toyota ME16 processor";
5382c23cb7cSEd Maste 	case EM_ST100: return "STMicroelectronics ST100 processor";
5392c23cb7cSEd Maste 	case EM_TINYJ: return "Advanced Logic Corp. TinyJ processor";
5402c23cb7cSEd Maste 	case EM_X86_64: return "Advanced Micro Devices x86-64";
5412c23cb7cSEd Maste 	case EM_PDSP: return "Sony DSP Processor";
5422c23cb7cSEd Maste 	case EM_FX66: return "Siemens FX66 microcontroller";
5432c23cb7cSEd Maste 	case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 microcontroller";
5442c23cb7cSEd Maste 	case EM_ST7: return "STmicroelectronics ST7 8-bit microcontroller";
5452c23cb7cSEd Maste 	case EM_68HC16: return "Motorola MC68HC16 microcontroller";
5462c23cb7cSEd Maste 	case EM_68HC11: return "Motorola MC68HC11 microcontroller";
5472c23cb7cSEd Maste 	case EM_68HC08: return "Motorola MC68HC08 microcontroller";
5482c23cb7cSEd Maste 	case EM_68HC05: return "Motorola MC68HC05 microcontroller";
5492c23cb7cSEd Maste 	case EM_SVX: return "Silicon Graphics SVx";
5502c23cb7cSEd Maste 	case EM_ST19: return "STMicroelectronics ST19 8-bit mc";
5512c23cb7cSEd Maste 	case EM_VAX: return "Digital VAX";
5522c23cb7cSEd Maste 	case EM_CRIS: return "Axis Communications 32-bit embedded processor";
5532c23cb7cSEd Maste 	case EM_JAVELIN: return "Infineon Tech. 32bit embedded processor";
5542c23cb7cSEd Maste 	case EM_FIREPATH: return "Element 14 64-bit DSP Processor";
5552c23cb7cSEd Maste 	case EM_ZSP: return "LSI Logic 16-bit DSP Processor";
5562c23cb7cSEd Maste 	case EM_MMIX: return "Donald Knuth's educational 64-bit proc";
5572c23cb7cSEd Maste 	case EM_HUANY: return "Harvard University MI object files";
5582c23cb7cSEd Maste 	case EM_PRISM: return "SiTera Prism";
5592c23cb7cSEd Maste 	case EM_AVR: return "Atmel AVR 8-bit microcontroller";
5602c23cb7cSEd Maste 	case EM_FR30: return "Fujitsu FR30";
5612c23cb7cSEd Maste 	case EM_D10V: return "Mitsubishi D10V";
5622c23cb7cSEd Maste 	case EM_D30V: return "Mitsubishi D30V";
5632c23cb7cSEd Maste 	case EM_V850: return "NEC v850";
5642c23cb7cSEd Maste 	case EM_M32R: return "Mitsubishi M32R";
5652c23cb7cSEd Maste 	case EM_MN10300: return "Matsushita MN10300";
5662c23cb7cSEd Maste 	case EM_MN10200: return "Matsushita MN10200";
5672c23cb7cSEd Maste 	case EM_PJ: return "picoJava";
5682c23cb7cSEd Maste 	case EM_OPENRISC: return "OpenRISC 32-bit embedded processor";
5692c23cb7cSEd Maste 	case EM_ARC_A5: return "ARC Cores Tangent-A5";
5702c23cb7cSEd Maste 	case EM_XTENSA: return "Tensilica Xtensa Architecture";
5712c23cb7cSEd Maste 	case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
5722c23cb7cSEd Maste 	case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
5732c23cb7cSEd Maste 	case EM_NS32K: return "National Semiconductor 32000 series";
5742c23cb7cSEd Maste 	case EM_TPC: return "Tenor Network TPC processor";
5752c23cb7cSEd Maste 	case EM_SNP1K: return "Trebia SNP 1000 processor";
5762c23cb7cSEd Maste 	case EM_ST200: return "STMicroelectronics ST200 microcontroller";
5772c23cb7cSEd Maste 	case EM_IP2K: return "Ubicom IP2xxx microcontroller family";
5782c23cb7cSEd Maste 	case EM_MAX: return "MAX Processor";
5792c23cb7cSEd Maste 	case EM_CR: return "National Semiconductor CompactRISC microprocessor";
5802c23cb7cSEd Maste 	case EM_F2MC16: return "Fujitsu F2MC16";
5812c23cb7cSEd Maste 	case EM_MSP430: return "TI embedded microcontroller msp430";
5822c23cb7cSEd Maste 	case EM_BLACKFIN: return "Analog Devices Blackfin (DSP) processor";
5832c23cb7cSEd Maste 	case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
5842c23cb7cSEd Maste 	case EM_SEP: return "Sharp embedded microprocessor";
5852c23cb7cSEd Maste 	case EM_ARCA: return "Arca RISC Microprocessor";
5862c23cb7cSEd Maste 	case EM_UNICORE: return "Microprocessor series from PKU-Unity Ltd";
587b3f26809SEd Maste 	case EM_AARCH64: return "AArch64";
588119b7592SEd Maste 	case EM_RISCV: return "RISC-V";
5892c23cb7cSEd Maste 	default:
5902c23cb7cSEd Maste 		snprintf(s_mach, sizeof(s_mach), "<unknown: %#x>", mach);
5912c23cb7cSEd Maste 		return (s_mach);
5922c23cb7cSEd Maste 	}
5932c23cb7cSEd Maste 
5942c23cb7cSEd Maste }
5952c23cb7cSEd Maste 
5962c23cb7cSEd Maste static const char *
5972c23cb7cSEd Maste elf_class(unsigned int class)
5982c23cb7cSEd Maste {
5992c23cb7cSEd Maste 	static char s_class[32];
6002c23cb7cSEd Maste 
6012c23cb7cSEd Maste 	switch (class) {
6022c23cb7cSEd Maste 	case ELFCLASSNONE: return "none";
6032c23cb7cSEd Maste 	case ELFCLASS32: return "ELF32";
6042c23cb7cSEd Maste 	case ELFCLASS64: return "ELF64";
6052c23cb7cSEd Maste 	default:
6062c23cb7cSEd Maste 		snprintf(s_class, sizeof(s_class), "<unknown: %#x>", class);
6072c23cb7cSEd Maste 		return (s_class);
6082c23cb7cSEd Maste 	}
6092c23cb7cSEd Maste }
6102c23cb7cSEd Maste 
6112c23cb7cSEd Maste static const char *
6122c23cb7cSEd Maste elf_endian(unsigned int endian)
6132c23cb7cSEd Maste {
6142c23cb7cSEd Maste 	static char s_endian[32];
6152c23cb7cSEd Maste 
6162c23cb7cSEd Maste 	switch (endian) {
6172c23cb7cSEd Maste 	case ELFDATANONE: return "none";
6182c23cb7cSEd Maste 	case ELFDATA2LSB: return "2's complement, little endian";
6192c23cb7cSEd Maste 	case ELFDATA2MSB: return "2's complement, big endian";
6202c23cb7cSEd Maste 	default:
6212c23cb7cSEd Maste 		snprintf(s_endian, sizeof(s_endian), "<unknown: %#x>", endian);
6222c23cb7cSEd Maste 		return (s_endian);
6232c23cb7cSEd Maste 	}
6242c23cb7cSEd Maste }
6252c23cb7cSEd Maste 
6262c23cb7cSEd Maste static const char *
6272c23cb7cSEd Maste elf_type(unsigned int type)
6282c23cb7cSEd Maste {
6292c23cb7cSEd Maste 	static char s_type[32];
6302c23cb7cSEd Maste 
6312c23cb7cSEd Maste 	switch (type) {
6322c23cb7cSEd Maste 	case ET_NONE: return "NONE (None)";
6332c23cb7cSEd Maste 	case ET_REL: return "REL (Relocatable file)";
6342c23cb7cSEd Maste 	case ET_EXEC: return "EXEC (Executable file)";
6352c23cb7cSEd Maste 	case ET_DYN: return "DYN (Shared object file)";
6362c23cb7cSEd Maste 	case ET_CORE: return "CORE (Core file)";
6372c23cb7cSEd Maste 	default:
6382c23cb7cSEd Maste 		if (type >= ET_LOPROC)
6392c23cb7cSEd Maste 			snprintf(s_type, sizeof(s_type), "<proc: %#x>", type);
6402c23cb7cSEd Maste 		else if (type >= ET_LOOS && type <= ET_HIOS)
6412c23cb7cSEd Maste 			snprintf(s_type, sizeof(s_type), "<os: %#x>", type);
6422c23cb7cSEd Maste 		else
6432c23cb7cSEd Maste 			snprintf(s_type, sizeof(s_type), "<unknown: %#x>",
6442c23cb7cSEd Maste 			    type);
6452c23cb7cSEd Maste 		return (s_type);
6462c23cb7cSEd Maste 	}
6472c23cb7cSEd Maste }
6482c23cb7cSEd Maste 
6492c23cb7cSEd Maste static const char *
6502c23cb7cSEd Maste elf_ver(unsigned int ver)
6512c23cb7cSEd Maste {
6522c23cb7cSEd Maste 	static char s_ver[32];
6532c23cb7cSEd Maste 
6542c23cb7cSEd Maste 	switch (ver) {
6552c23cb7cSEd Maste 	case EV_CURRENT: return "(current)";
6562c23cb7cSEd Maste 	case EV_NONE: return "(none)";
6572c23cb7cSEd Maste 	default:
6582c23cb7cSEd Maste 		snprintf(s_ver, sizeof(s_ver), "<unknown: %#x>",
6592c23cb7cSEd Maste 		    ver);
6602c23cb7cSEd Maste 		return (s_ver);
6612c23cb7cSEd Maste 	}
6622c23cb7cSEd Maste }
6632c23cb7cSEd Maste 
6642c23cb7cSEd Maste static const char *
66520136ffcSEd Maste phdr_type(unsigned int mach, unsigned int ptype)
6662c23cb7cSEd Maste {
6672c23cb7cSEd Maste 	static char s_ptype[32];
6682c23cb7cSEd Maste 
66920136ffcSEd Maste 	if (ptype >= PT_LOPROC && ptype <= PT_HIPROC) {
67020136ffcSEd Maste 		switch (mach) {
67120136ffcSEd Maste 		case EM_ARM:
67220136ffcSEd Maste 			switch (ptype) {
67320136ffcSEd Maste 			case PT_ARM_ARCHEXT: return "ARM_ARCHEXT";
67420136ffcSEd Maste 			case PT_ARM_EXIDX: return "ARM_EXIDX";
67520136ffcSEd Maste 			}
67620136ffcSEd Maste 			break;
67720136ffcSEd Maste 		}
67820136ffcSEd Maste 		snprintf(s_ptype, sizeof(s_ptype), "LOPROC+%#x",
67920136ffcSEd Maste 		    ptype - PT_LOPROC);
68020136ffcSEd Maste 		return (s_ptype);
68120136ffcSEd Maste 	}
68220136ffcSEd Maste 
6832c23cb7cSEd Maste 	switch (ptype) {
6842c23cb7cSEd Maste 	case PT_NULL: return "NULL";
6852c23cb7cSEd Maste 	case PT_LOAD: return "LOAD";
6862c23cb7cSEd Maste 	case PT_DYNAMIC: return "DYNAMIC";
6872c23cb7cSEd Maste 	case PT_INTERP: return "INTERP";
6882c23cb7cSEd Maste 	case PT_NOTE: return "NOTE";
6892c23cb7cSEd Maste 	case PT_SHLIB: return "SHLIB";
6902c23cb7cSEd Maste 	case PT_PHDR: return "PHDR";
6912c23cb7cSEd Maste 	case PT_TLS: return "TLS";
6922c23cb7cSEd Maste 	case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
6932c23cb7cSEd Maste 	case PT_GNU_STACK: return "GNU_STACK";
6942c23cb7cSEd Maste 	case PT_GNU_RELRO: return "GNU_RELRO";
695ca307559SChristian S.J. Peron 	case PT_OPENBSD_RANDOMIZE: return "OPENBSD_RANDOMIZE";
696ca307559SChristian S.J. Peron 	case PT_OPENBSD_WXNEEDED: return "OPENBSD_WXNEEDED";
697ca307559SChristian S.J. Peron 	case PT_OPENBSD_BOOTDATA: return "OPENBSD_BOOTDATA";
6982c23cb7cSEd Maste 	default:
69920136ffcSEd Maste 		if (ptype >= PT_LOOS && ptype <= PT_HIOS)
7002c23cb7cSEd Maste 			snprintf(s_ptype, sizeof(s_ptype), "LOOS+%#x",
7012c23cb7cSEd Maste 			    ptype - PT_LOOS);
7022c23cb7cSEd Maste 		else
7032c23cb7cSEd Maste 			snprintf(s_ptype, sizeof(s_ptype), "<unknown: %#x>",
7042c23cb7cSEd Maste 			    ptype);
7052c23cb7cSEd Maste 		return (s_ptype);
7062c23cb7cSEd Maste 	}
7072c23cb7cSEd Maste }
7082c23cb7cSEd Maste 
7092c23cb7cSEd Maste static const char *
7102c23cb7cSEd Maste section_type(unsigned int mach, unsigned int stype)
7112c23cb7cSEd Maste {
7122c23cb7cSEd Maste 	static char s_stype[32];
7132c23cb7cSEd Maste 
7142c23cb7cSEd Maste 	if (stype >= SHT_LOPROC && stype <= SHT_HIPROC) {
7152c23cb7cSEd Maste 		switch (mach) {
71620136ffcSEd Maste 		case EM_ARM:
71720136ffcSEd Maste 			switch (stype) {
71820136ffcSEd Maste 			case SHT_ARM_EXIDX: return "ARM_EXIDX";
71920136ffcSEd Maste 			case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP";
72020136ffcSEd Maste 			case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES";
72120136ffcSEd Maste 			case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY";
72220136ffcSEd Maste 			case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION";
72320136ffcSEd Maste 			}
72420136ffcSEd Maste 			break;
7252c23cb7cSEd Maste 		case EM_X86_64:
7262c23cb7cSEd Maste 			switch (stype) {
727b6b6f9ccSEd Maste 			case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
7282c23cb7cSEd Maste 			default:
7292c23cb7cSEd Maste 				break;
7302c23cb7cSEd Maste 			}
7312c23cb7cSEd Maste 			break;
7322c23cb7cSEd Maste 		case EM_MIPS:
7332c23cb7cSEd Maste 		case EM_MIPS_RS3_LE:
7342c23cb7cSEd Maste 			switch (stype) {
7352c23cb7cSEd Maste 			case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
7362c23cb7cSEd Maste 			case SHT_MIPS_MSYM: return "MIPS_MSYM";
7372c23cb7cSEd Maste 			case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
7382c23cb7cSEd Maste 			case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
7392c23cb7cSEd Maste 			case SHT_MIPS_UCODE: return "MIPS_UCODE";
7402c23cb7cSEd Maste 			case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
7412c23cb7cSEd Maste 			case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
7422c23cb7cSEd Maste 			case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
7432c23cb7cSEd Maste 			case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
7442c23cb7cSEd Maste 			case SHT_MIPS_RELD: return "MIPS_RELD";
7452c23cb7cSEd Maste 			case SHT_MIPS_IFACE: return "MIPS_IFACE";
7462c23cb7cSEd Maste 			case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
7472c23cb7cSEd Maste 			case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
7482c23cb7cSEd Maste 			case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
7492c23cb7cSEd Maste 			case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
7502c23cb7cSEd Maste 			case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
7512c23cb7cSEd Maste 			case SHT_MIPS_DWARF: return "MIPS_DWARF";
7522c23cb7cSEd Maste 			case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
7532c23cb7cSEd Maste 			case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
7542c23cb7cSEd Maste 			case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
7552c23cb7cSEd Maste 			case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
7562c23cb7cSEd Maste 			case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
7572c23cb7cSEd Maste 			case SHT_MIPS_XLATE: return "MIPS_XLATE";
7582c23cb7cSEd Maste 			case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
7592c23cb7cSEd Maste 			case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
7602c23cb7cSEd Maste 			case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
7612c23cb7cSEd Maste 			case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
7622c23cb7cSEd Maste 			case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
763bee2765cSEd Maste 			case SHT_MIPS_ABIFLAGS: return "MIPS_ABIFLAGS";
7642c23cb7cSEd Maste 			default:
7652c23cb7cSEd Maste 				break;
7662c23cb7cSEd Maste 			}
7672c23cb7cSEd Maste 			break;
7682c23cb7cSEd Maste 		default:
7692c23cb7cSEd Maste 			break;
7702c23cb7cSEd Maste 		}
7712c23cb7cSEd Maste 
7722c23cb7cSEd Maste 		snprintf(s_stype, sizeof(s_stype), "LOPROC+%#x",
7732c23cb7cSEd Maste 		    stype - SHT_LOPROC);
7742c23cb7cSEd Maste 		return (s_stype);
7752c23cb7cSEd Maste 	}
7762c23cb7cSEd Maste 
7772c23cb7cSEd Maste 	switch (stype) {
7782c23cb7cSEd Maste 	case SHT_NULL: return "NULL";
7792c23cb7cSEd Maste 	case SHT_PROGBITS: return "PROGBITS";
7802c23cb7cSEd Maste 	case SHT_SYMTAB: return "SYMTAB";
7812c23cb7cSEd Maste 	case SHT_STRTAB: return "STRTAB";
7822c23cb7cSEd Maste 	case SHT_RELA: return "RELA";
7832c23cb7cSEd Maste 	case SHT_HASH: return "HASH";
7842c23cb7cSEd Maste 	case SHT_DYNAMIC: return "DYNAMIC";
7852c23cb7cSEd Maste 	case SHT_NOTE: return "NOTE";
7862c23cb7cSEd Maste 	case SHT_NOBITS: return "NOBITS";
7872c23cb7cSEd Maste 	case SHT_REL: return "REL";
7882c23cb7cSEd Maste 	case SHT_SHLIB: return "SHLIB";
7892c23cb7cSEd Maste 	case SHT_DYNSYM: return "DYNSYM";
7902c23cb7cSEd Maste 	case SHT_INIT_ARRAY: return "INIT_ARRAY";
7912c23cb7cSEd Maste 	case SHT_FINI_ARRAY: return "FINI_ARRAY";
7922c23cb7cSEd Maste 	case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
7932c23cb7cSEd Maste 	case SHT_GROUP: return "GROUP";
7942c23cb7cSEd Maste 	case SHT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
7952c23cb7cSEd Maste 	case SHT_SUNW_dof: return "SUNW_dof";
7962c23cb7cSEd Maste 	case SHT_SUNW_cap: return "SUNW_cap";
7972c23cb7cSEd Maste 	case SHT_GNU_HASH: return "GNU_HASH";
7982c23cb7cSEd Maste 	case SHT_SUNW_ANNOTATE: return "SUNW_ANNOTATE";
7992c23cb7cSEd Maste 	case SHT_SUNW_DEBUGSTR: return "SUNW_DEBUGSTR";
8002c23cb7cSEd Maste 	case SHT_SUNW_DEBUG: return "SUNW_DEBUG";
8012c23cb7cSEd Maste 	case SHT_SUNW_move: return "SUNW_move";
8022c23cb7cSEd Maste 	case SHT_SUNW_COMDAT: return "SUNW_COMDAT";
8032c23cb7cSEd Maste 	case SHT_SUNW_syminfo: return "SUNW_syminfo";
8042c23cb7cSEd Maste 	case SHT_SUNW_verdef: return "SUNW_verdef";
8052c23cb7cSEd Maste 	case SHT_SUNW_verneed: return "SUNW_verneed";
8062c23cb7cSEd Maste 	case SHT_SUNW_versym: return "SUNW_versym";
8072c23cb7cSEd Maste 	default:
8082c23cb7cSEd Maste 		if (stype >= SHT_LOOS && stype <= SHT_HIOS)
8092c23cb7cSEd Maste 			snprintf(s_stype, sizeof(s_stype), "LOOS+%#x",
8102c23cb7cSEd Maste 			    stype - SHT_LOOS);
8112c23cb7cSEd Maste 		else if (stype >= SHT_LOUSER)
8122c23cb7cSEd Maste 			snprintf(s_stype, sizeof(s_stype), "LOUSER+%#x",
8132c23cb7cSEd Maste 			    stype - SHT_LOUSER);
8142c23cb7cSEd Maste 		else
8152c23cb7cSEd Maste 			snprintf(s_stype, sizeof(s_stype), "<unknown: %#x>",
8162c23cb7cSEd Maste 			    stype);
8172c23cb7cSEd Maste 		return (s_stype);
8182c23cb7cSEd Maste 	}
8192c23cb7cSEd Maste }
8202c23cb7cSEd Maste 
8212c23cb7cSEd Maste static const char *
8222c23cb7cSEd Maste dt_type(unsigned int mach, unsigned int dtype)
8232c23cb7cSEd Maste {
8242c23cb7cSEd Maste 	static char s_dtype[32];
8252c23cb7cSEd Maste 
826d62ecb4eSEd Maste 	switch (dtype) {
827d62ecb4eSEd Maste 	case DT_NULL: return "NULL";
828d62ecb4eSEd Maste 	case DT_NEEDED: return "NEEDED";
829d62ecb4eSEd Maste 	case DT_PLTRELSZ: return "PLTRELSZ";
830d62ecb4eSEd Maste 	case DT_PLTGOT: return "PLTGOT";
831d62ecb4eSEd Maste 	case DT_HASH: return "HASH";
832d62ecb4eSEd Maste 	case DT_STRTAB: return "STRTAB";
833d62ecb4eSEd Maste 	case DT_SYMTAB: return "SYMTAB";
834d62ecb4eSEd Maste 	case DT_RELA: return "RELA";
835d62ecb4eSEd Maste 	case DT_RELASZ: return "RELASZ";
836d62ecb4eSEd Maste 	case DT_RELAENT: return "RELAENT";
837d62ecb4eSEd Maste 	case DT_STRSZ: return "STRSZ";
838d62ecb4eSEd Maste 	case DT_SYMENT: return "SYMENT";
839d62ecb4eSEd Maste 	case DT_INIT: return "INIT";
840d62ecb4eSEd Maste 	case DT_FINI: return "FINI";
841d62ecb4eSEd Maste 	case DT_SONAME: return "SONAME";
842d62ecb4eSEd Maste 	case DT_RPATH: return "RPATH";
843d62ecb4eSEd Maste 	case DT_SYMBOLIC: return "SYMBOLIC";
844d62ecb4eSEd Maste 	case DT_REL: return "REL";
845d62ecb4eSEd Maste 	case DT_RELSZ: return "RELSZ";
846d62ecb4eSEd Maste 	case DT_RELENT: return "RELENT";
847d62ecb4eSEd Maste 	case DT_PLTREL: return "PLTREL";
848d62ecb4eSEd Maste 	case DT_DEBUG: return "DEBUG";
849d62ecb4eSEd Maste 	case DT_TEXTREL: return "TEXTREL";
850d62ecb4eSEd Maste 	case DT_JMPREL: return "JMPREL";
851d62ecb4eSEd Maste 	case DT_BIND_NOW: return "BIND_NOW";
852d62ecb4eSEd Maste 	case DT_INIT_ARRAY: return "INIT_ARRAY";
853d62ecb4eSEd Maste 	case DT_FINI_ARRAY: return "FINI_ARRAY";
854d62ecb4eSEd Maste 	case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
855d62ecb4eSEd Maste 	case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
856d62ecb4eSEd Maste 	case DT_RUNPATH: return "RUNPATH";
857d62ecb4eSEd Maste 	case DT_FLAGS: return "FLAGS";
858d62ecb4eSEd Maste 	case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
859d62ecb4eSEd Maste 	case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
860d62ecb4eSEd Maste 	case DT_MAXPOSTAGS: return "MAXPOSTAGS";
861d62ecb4eSEd Maste 	case DT_SUNW_AUXILIARY: return "SUNW_AUXILIARY";
862d62ecb4eSEd Maste 	case DT_SUNW_RTLDINF: return "SUNW_RTLDINF";
863d62ecb4eSEd Maste 	case DT_SUNW_FILTER: return "SUNW_FILTER";
864d62ecb4eSEd Maste 	case DT_SUNW_CAP: return "SUNW_CAP";
865715d1396SEd Maste 	case DT_SUNW_ASLR: return "SUNW_ASLR";
866d62ecb4eSEd Maste 	case DT_CHECKSUM: return "CHECKSUM";
867d62ecb4eSEd Maste 	case DT_PLTPADSZ: return "PLTPADSZ";
868d62ecb4eSEd Maste 	case DT_MOVEENT: return "MOVEENT";
869d62ecb4eSEd Maste 	case DT_MOVESZ: return "MOVESZ";
870d62ecb4eSEd Maste 	case DT_FEATURE: return "FEATURE";
871d62ecb4eSEd Maste 	case DT_POSFLAG_1: return "POSFLAG_1";
872d62ecb4eSEd Maste 	case DT_SYMINSZ: return "SYMINSZ";
873d62ecb4eSEd Maste 	case DT_SYMINENT: return "SYMINENT";
874d62ecb4eSEd Maste 	case DT_GNU_HASH: return "GNU_HASH";
875d62ecb4eSEd Maste 	case DT_TLSDESC_PLT: return "DT_TLSDESC_PLT";
876d62ecb4eSEd Maste 	case DT_TLSDESC_GOT: return "DT_TLSDESC_GOT";
877d62ecb4eSEd Maste 	case DT_GNU_CONFLICT: return "GNU_CONFLICT";
878d62ecb4eSEd Maste 	case DT_GNU_LIBLIST: return "GNU_LIBLIST";
879d62ecb4eSEd Maste 	case DT_CONFIG: return "CONFIG";
880d62ecb4eSEd Maste 	case DT_DEPAUDIT: return "DEPAUDIT";
881d62ecb4eSEd Maste 	case DT_AUDIT: return "AUDIT";
882d62ecb4eSEd Maste 	case DT_PLTPAD: return "PLTPAD";
883d62ecb4eSEd Maste 	case DT_MOVETAB: return "MOVETAB";
884d62ecb4eSEd Maste 	case DT_SYMINFO: return "SYMINFO";
885d62ecb4eSEd Maste 	case DT_VERSYM: return "VERSYM";
886d62ecb4eSEd Maste 	case DT_RELACOUNT: return "RELACOUNT";
887d62ecb4eSEd Maste 	case DT_RELCOUNT: return "RELCOUNT";
888d62ecb4eSEd Maste 	case DT_FLAGS_1: return "FLAGS_1";
889d62ecb4eSEd Maste 	case DT_VERDEF: return "VERDEF";
890d62ecb4eSEd Maste 	case DT_VERDEFNUM: return "VERDEFNUM";
891d62ecb4eSEd Maste 	case DT_VERNEED: return "VERNEED";
892d62ecb4eSEd Maste 	case DT_VERNEEDNUM: return "VERNEEDNUM";
893d62ecb4eSEd Maste 	case DT_AUXILIARY: return "AUXILIARY";
894d62ecb4eSEd Maste 	case DT_USED: return "USED";
895d62ecb4eSEd Maste 	case DT_FILTER: return "FILTER";
896d62ecb4eSEd Maste 	case DT_GNU_PRELINKED: return "GNU_PRELINKED";
897d62ecb4eSEd Maste 	case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
898d62ecb4eSEd Maste 	case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
899d62ecb4eSEd Maste 	}
900d62ecb4eSEd Maste 
9012c23cb7cSEd Maste 	if (dtype >= DT_LOPROC && dtype <= DT_HIPROC) {
9022c23cb7cSEd Maste 		switch (mach) {
9032c23cb7cSEd Maste 		case EM_ARM:
9042c23cb7cSEd Maste 			switch (dtype) {
9052c23cb7cSEd Maste 			case DT_ARM_SYMTABSZ:
9062c23cb7cSEd Maste 				return "ARM_SYMTABSZ";
9072c23cb7cSEd Maste 			default:
9082c23cb7cSEd Maste 				break;
9092c23cb7cSEd Maste 			}
9102c23cb7cSEd Maste 			break;
9112c23cb7cSEd Maste 		case EM_MIPS:
9122c23cb7cSEd Maste 		case EM_MIPS_RS3_LE:
9132c23cb7cSEd Maste 			switch (dtype) {
9142c23cb7cSEd Maste 			case DT_MIPS_RLD_VERSION:
9152c23cb7cSEd Maste 				return "MIPS_RLD_VERSION";
9162c23cb7cSEd Maste 			case DT_MIPS_TIME_STAMP:
9172c23cb7cSEd Maste 				return "MIPS_TIME_STAMP";
9182c23cb7cSEd Maste 			case DT_MIPS_ICHECKSUM:
9192c23cb7cSEd Maste 				return "MIPS_ICHECKSUM";
9202c23cb7cSEd Maste 			case DT_MIPS_IVERSION:
9212c23cb7cSEd Maste 				return "MIPS_IVERSION";
9222c23cb7cSEd Maste 			case DT_MIPS_FLAGS:
9232c23cb7cSEd Maste 				return "MIPS_FLAGS";
9242c23cb7cSEd Maste 			case DT_MIPS_BASE_ADDRESS:
9252c23cb7cSEd Maste 				return "MIPS_BASE_ADDRESS";
9262c23cb7cSEd Maste 			case DT_MIPS_CONFLICT:
9272c23cb7cSEd Maste 				return "MIPS_CONFLICT";
9282c23cb7cSEd Maste 			case DT_MIPS_LIBLIST:
9292c23cb7cSEd Maste 				return "MIPS_LIBLIST";
9302c23cb7cSEd Maste 			case DT_MIPS_LOCAL_GOTNO:
9312c23cb7cSEd Maste 				return "MIPS_LOCAL_GOTNO";
9322c23cb7cSEd Maste 			case DT_MIPS_CONFLICTNO:
9332c23cb7cSEd Maste 				return "MIPS_CONFLICTNO";
9342c23cb7cSEd Maste 			case DT_MIPS_LIBLISTNO:
9352c23cb7cSEd Maste 				return "MIPS_LIBLISTNO";
9362c23cb7cSEd Maste 			case DT_MIPS_SYMTABNO:
9372c23cb7cSEd Maste 				return "MIPS_SYMTABNO";
9382c23cb7cSEd Maste 			case DT_MIPS_UNREFEXTNO:
9392c23cb7cSEd Maste 				return "MIPS_UNREFEXTNO";
9402c23cb7cSEd Maste 			case DT_MIPS_GOTSYM:
9412c23cb7cSEd Maste 				return "MIPS_GOTSYM";
9422c23cb7cSEd Maste 			case DT_MIPS_HIPAGENO:
9432c23cb7cSEd Maste 				return "MIPS_HIPAGENO";
9442c23cb7cSEd Maste 			case DT_MIPS_RLD_MAP:
9452c23cb7cSEd Maste 				return "MIPS_RLD_MAP";
9462c23cb7cSEd Maste 			case DT_MIPS_DELTA_CLASS:
9472c23cb7cSEd Maste 				return "MIPS_DELTA_CLASS";
9482c23cb7cSEd Maste 			case DT_MIPS_DELTA_CLASS_NO:
9492c23cb7cSEd Maste 				return "MIPS_DELTA_CLASS_NO";
9502c23cb7cSEd Maste 			case DT_MIPS_DELTA_INSTANCE:
9512c23cb7cSEd Maste 				return "MIPS_DELTA_INSTANCE";
9522c23cb7cSEd Maste 			case DT_MIPS_DELTA_INSTANCE_NO:
9532c23cb7cSEd Maste 				return "MIPS_DELTA_INSTANCE_NO";
9542c23cb7cSEd Maste 			case DT_MIPS_DELTA_RELOC:
9552c23cb7cSEd Maste 				return "MIPS_DELTA_RELOC";
9562c23cb7cSEd Maste 			case DT_MIPS_DELTA_RELOC_NO:
9572c23cb7cSEd Maste 				return "MIPS_DELTA_RELOC_NO";
9582c23cb7cSEd Maste 			case DT_MIPS_DELTA_SYM:
9592c23cb7cSEd Maste 				return "MIPS_DELTA_SYM";
9602c23cb7cSEd Maste 			case DT_MIPS_DELTA_SYM_NO:
9612c23cb7cSEd Maste 				return "MIPS_DELTA_SYM_NO";
9622c23cb7cSEd Maste 			case DT_MIPS_DELTA_CLASSSYM:
9632c23cb7cSEd Maste 				return "MIPS_DELTA_CLASSSYM";
9642c23cb7cSEd Maste 			case DT_MIPS_DELTA_CLASSSYM_NO:
9652c23cb7cSEd Maste 				return "MIPS_DELTA_CLASSSYM_NO";
9662c23cb7cSEd Maste 			case DT_MIPS_CXX_FLAGS:
9672c23cb7cSEd Maste 				return "MIPS_CXX_FLAGS";
9682c23cb7cSEd Maste 			case DT_MIPS_PIXIE_INIT:
9692c23cb7cSEd Maste 				return "MIPS_PIXIE_INIT";
9702c23cb7cSEd Maste 			case DT_MIPS_SYMBOL_LIB:
9712c23cb7cSEd Maste 				return "MIPS_SYMBOL_LIB";
9722c23cb7cSEd Maste 			case DT_MIPS_LOCALPAGE_GOTIDX:
9732c23cb7cSEd Maste 				return "MIPS_LOCALPAGE_GOTIDX";
9742c23cb7cSEd Maste 			case DT_MIPS_LOCAL_GOTIDX:
9752c23cb7cSEd Maste 				return "MIPS_LOCAL_GOTIDX";
9762c23cb7cSEd Maste 			case DT_MIPS_HIDDEN_GOTIDX:
9772c23cb7cSEd Maste 				return "MIPS_HIDDEN_GOTIDX";
9782c23cb7cSEd Maste 			case DT_MIPS_PROTECTED_GOTIDX:
9792c23cb7cSEd Maste 				return "MIPS_PROTECTED_GOTIDX";
9802c23cb7cSEd Maste 			case DT_MIPS_OPTIONS:
9812c23cb7cSEd Maste 				return "MIPS_OPTIONS";
9822c23cb7cSEd Maste 			case DT_MIPS_INTERFACE:
9832c23cb7cSEd Maste 				return "MIPS_INTERFACE";
9842c23cb7cSEd Maste 			case DT_MIPS_DYNSTR_ALIGN:
9852c23cb7cSEd Maste 				return "MIPS_DYNSTR_ALIGN";
9862c23cb7cSEd Maste 			case DT_MIPS_INTERFACE_SIZE:
9872c23cb7cSEd Maste 				return "MIPS_INTERFACE_SIZE";
9882c23cb7cSEd Maste 			case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
9892c23cb7cSEd Maste 				return "MIPS_RLD_TEXT_RESOLVE_ADDR";
9902c23cb7cSEd Maste 			case DT_MIPS_PERF_SUFFIX:
9912c23cb7cSEd Maste 				return "MIPS_PERF_SUFFIX";
9922c23cb7cSEd Maste 			case DT_MIPS_COMPACT_SIZE:
9932c23cb7cSEd Maste 				return "MIPS_COMPACT_SIZE";
9942c23cb7cSEd Maste 			case DT_MIPS_GP_VALUE:
9952c23cb7cSEd Maste 				return "MIPS_GP_VALUE";
9962c23cb7cSEd Maste 			case DT_MIPS_AUX_DYNAMIC:
9972c23cb7cSEd Maste 				return "MIPS_AUX_DYNAMIC";
9982c23cb7cSEd Maste 			case DT_MIPS_PLTGOT:
9992c23cb7cSEd Maste 				return "MIPS_PLTGOT";
10002c23cb7cSEd Maste 			case DT_MIPS_RLD_OBJ_UPDATE:
10012c23cb7cSEd Maste 				return "MIPS_RLD_OBJ_UPDATE";
10022c23cb7cSEd Maste 			case DT_MIPS_RWPLT:
10032c23cb7cSEd Maste 				return "MIPS_RWPLT";
10042c23cb7cSEd Maste 			default:
10052c23cb7cSEd Maste 				break;
10062c23cb7cSEd Maste 			}
10072c23cb7cSEd Maste 			break;
10082c23cb7cSEd Maste 		case EM_SPARC:
10092c23cb7cSEd Maste 		case EM_SPARC32PLUS:
10102c23cb7cSEd Maste 		case EM_SPARCV9:
10112c23cb7cSEd Maste 			switch (dtype) {
10122c23cb7cSEd Maste 			case DT_SPARC_REGISTER:
10132c23cb7cSEd Maste 				return "DT_SPARC_REGISTER";
10142c23cb7cSEd Maste 			default:
10152c23cb7cSEd Maste 				break;
10162c23cb7cSEd Maste 			}
10172c23cb7cSEd Maste 			break;
10182c23cb7cSEd Maste 		default:
10192c23cb7cSEd Maste 			break;
10202c23cb7cSEd Maste 		}
10212c23cb7cSEd Maste 	}
10222c23cb7cSEd Maste 
10232c23cb7cSEd Maste 	snprintf(s_dtype, sizeof(s_dtype), "<unknown: %#x>", dtype);
10242c23cb7cSEd Maste 	return (s_dtype);
10252c23cb7cSEd Maste }
10262c23cb7cSEd Maste 
10272c23cb7cSEd Maste static const char *
10282c23cb7cSEd Maste st_bind(unsigned int sbind)
10292c23cb7cSEd Maste {
10302c23cb7cSEd Maste 	static char s_sbind[32];
10312c23cb7cSEd Maste 
10322c23cb7cSEd Maste 	switch (sbind) {
10332c23cb7cSEd Maste 	case STB_LOCAL: return "LOCAL";
10342c23cb7cSEd Maste 	case STB_GLOBAL: return "GLOBAL";
10352c23cb7cSEd Maste 	case STB_WEAK: return "WEAK";
1036839529caSEd Maste 	case STB_GNU_UNIQUE: return "UNIQUE";
10372c23cb7cSEd Maste 	default:
10382c23cb7cSEd Maste 		if (sbind >= STB_LOOS && sbind <= STB_HIOS)
10392c23cb7cSEd Maste 			return "OS";
10402c23cb7cSEd Maste 		else if (sbind >= STB_LOPROC && sbind <= STB_HIPROC)
10412c23cb7cSEd Maste 			return "PROC";
10422c23cb7cSEd Maste 		else
10432c23cb7cSEd Maste 			snprintf(s_sbind, sizeof(s_sbind), "<unknown: %#x>",
10442c23cb7cSEd Maste 			    sbind);
10452c23cb7cSEd Maste 		return (s_sbind);
10462c23cb7cSEd Maste 	}
10472c23cb7cSEd Maste }
10482c23cb7cSEd Maste 
10492c23cb7cSEd Maste static const char *
1050b6b6f9ccSEd Maste st_type(unsigned int mach, unsigned int os, unsigned int stype)
10512c23cb7cSEd Maste {
10522c23cb7cSEd Maste 	static char s_stype[32];
10532c23cb7cSEd Maste 
10542c23cb7cSEd Maste 	switch (stype) {
10552c23cb7cSEd Maste 	case STT_NOTYPE: return "NOTYPE";
10562c23cb7cSEd Maste 	case STT_OBJECT: return "OBJECT";
10572c23cb7cSEd Maste 	case STT_FUNC: return "FUNC";
10582c23cb7cSEd Maste 	case STT_SECTION: return "SECTION";
10592c23cb7cSEd Maste 	case STT_FILE: return "FILE";
10602c23cb7cSEd Maste 	case STT_COMMON: return "COMMON";
10612c23cb7cSEd Maste 	case STT_TLS: return "TLS";
10622c23cb7cSEd Maste 	default:
1063b6b6f9ccSEd Maste 		if (stype >= STT_LOOS && stype <= STT_HIOS) {
1064b6b6f9ccSEd Maste 			if ((os == ELFOSABI_GNU || os == ELFOSABI_FREEBSD) &&
1065b6b6f9ccSEd Maste 			    stype == STT_GNU_IFUNC)
1066b6b6f9ccSEd Maste 				return "IFUNC";
10672c23cb7cSEd Maste 			snprintf(s_stype, sizeof(s_stype), "OS+%#x",
10682c23cb7cSEd Maste 			    stype - STT_LOOS);
1069b6b6f9ccSEd Maste 		} else if (stype >= STT_LOPROC && stype <= STT_HIPROC) {
1070839529caSEd Maste 			if (mach == EM_SPARCV9 && stype == STT_SPARC_REGISTER)
1071839529caSEd Maste 				return "REGISTER";
10722c23cb7cSEd Maste 			snprintf(s_stype, sizeof(s_stype), "PROC+%#x",
10732c23cb7cSEd Maste 			    stype - STT_LOPROC);
1074839529caSEd Maste 		} else
10752c23cb7cSEd Maste 			snprintf(s_stype, sizeof(s_stype), "<unknown: %#x>",
10762c23cb7cSEd Maste 			    stype);
10772c23cb7cSEd Maste 		return (s_stype);
10782c23cb7cSEd Maste 	}
10792c23cb7cSEd Maste }
10802c23cb7cSEd Maste 
10812c23cb7cSEd Maste static const char *
10822c23cb7cSEd Maste st_vis(unsigned int svis)
10832c23cb7cSEd Maste {
10842c23cb7cSEd Maste 	static char s_svis[32];
10852c23cb7cSEd Maste 
10862c23cb7cSEd Maste 	switch(svis) {
10872c23cb7cSEd Maste 	case STV_DEFAULT: return "DEFAULT";
10882c23cb7cSEd Maste 	case STV_INTERNAL: return "INTERNAL";
10892c23cb7cSEd Maste 	case STV_HIDDEN: return "HIDDEN";
10902c23cb7cSEd Maste 	case STV_PROTECTED: return "PROTECTED";
10912c23cb7cSEd Maste 	default:
10922c23cb7cSEd Maste 		snprintf(s_svis, sizeof(s_svis), "<unknown: %#x>", svis);
10932c23cb7cSEd Maste 		return (s_svis);
10942c23cb7cSEd Maste 	}
10952c23cb7cSEd Maste }
10962c23cb7cSEd Maste 
10972c23cb7cSEd Maste static const char *
10982c23cb7cSEd Maste st_shndx(unsigned int shndx)
10992c23cb7cSEd Maste {
11002c23cb7cSEd Maste 	static char s_shndx[32];
11012c23cb7cSEd Maste 
11022c23cb7cSEd Maste 	switch (shndx) {
11032c23cb7cSEd Maste 	case SHN_UNDEF: return "UND";
11042c23cb7cSEd Maste 	case SHN_ABS: return "ABS";
11052c23cb7cSEd Maste 	case SHN_COMMON: return "COM";
11062c23cb7cSEd Maste 	default:
11072c23cb7cSEd Maste 		if (shndx >= SHN_LOPROC && shndx <= SHN_HIPROC)
11082c23cb7cSEd Maste 			return "PRC";
11092c23cb7cSEd Maste 		else if (shndx >= SHN_LOOS && shndx <= SHN_HIOS)
11102c23cb7cSEd Maste 			return "OS";
11112c23cb7cSEd Maste 		else
11122c23cb7cSEd Maste 			snprintf(s_shndx, sizeof(s_shndx), "%u", shndx);
11132c23cb7cSEd Maste 		return (s_shndx);
11142c23cb7cSEd Maste 	}
11152c23cb7cSEd Maste }
11162c23cb7cSEd Maste 
11172c23cb7cSEd Maste static struct {
11182c23cb7cSEd Maste 	const char *ln;
11192c23cb7cSEd Maste 	char sn;
11202c23cb7cSEd Maste 	int value;
11212c23cb7cSEd Maste } section_flag[] = {
11222c23cb7cSEd Maste 	{"WRITE", 'W', SHF_WRITE},
11232c23cb7cSEd Maste 	{"ALLOC", 'A', SHF_ALLOC},
11242c23cb7cSEd Maste 	{"EXEC", 'X', SHF_EXECINSTR},
11252c23cb7cSEd Maste 	{"MERGE", 'M', SHF_MERGE},
11262c23cb7cSEd Maste 	{"STRINGS", 'S', SHF_STRINGS},
11272c23cb7cSEd Maste 	{"INFO LINK", 'I', SHF_INFO_LINK},
11282c23cb7cSEd Maste 	{"OS NONCONF", 'O', SHF_OS_NONCONFORMING},
11292c23cb7cSEd Maste 	{"GROUP", 'G', SHF_GROUP},
11302c23cb7cSEd Maste 	{"TLS", 'T', SHF_TLS},
1131b6b6f9ccSEd Maste 	{"COMPRESSED", 'C', SHF_COMPRESSED},
11322c23cb7cSEd Maste 	{NULL, 0, 0}
11332c23cb7cSEd Maste };
11342c23cb7cSEd Maste 
11352c23cb7cSEd Maste static const char *
113602b08c90SEd Maste note_type(const char *name, unsigned int et, unsigned int nt)
113702b08c90SEd Maste {
113871a0c925SEd Maste 	if ((strcmp(name, "CORE") == 0 || strcmp(name, "LINUX") == 0) &&
113971a0c925SEd Maste 	    et == ET_CORE)
114002b08c90SEd Maste 		return note_type_linux_core(nt);
114102b08c90SEd Maste 	else if (strcmp(name, "FreeBSD") == 0)
114202b08c90SEd Maste 		if (et == ET_CORE)
114302b08c90SEd Maste 			return note_type_freebsd_core(nt);
114402b08c90SEd Maste 		else
114502b08c90SEd Maste 			return note_type_freebsd(nt);
114602b08c90SEd Maste 	else if (strcmp(name, "GNU") == 0 && et != ET_CORE)
114702b08c90SEd Maste 		return note_type_gnu(nt);
114802b08c90SEd Maste 	else if (strcmp(name, "NetBSD") == 0 && et != ET_CORE)
114902b08c90SEd Maste 		return note_type_netbsd(nt);
115002b08c90SEd Maste 	else if (strcmp(name, "OpenBSD") == 0 && et != ET_CORE)
115102b08c90SEd Maste 		return note_type_openbsd(nt);
1152895f86f1SEd Maste 	else if (strcmp(name, "Xen") == 0 && et != ET_CORE)
1153895f86f1SEd Maste 		return note_type_xen(nt);
115402b08c90SEd Maste 	return note_type_unknown(nt);
115502b08c90SEd Maste }
115602b08c90SEd Maste 
115702b08c90SEd Maste static const char *
115802b08c90SEd Maste note_type_freebsd(unsigned int nt)
115902b08c90SEd Maste {
116002b08c90SEd Maste 	switch (nt) {
116102b08c90SEd Maste 	case 1: return "NT_FREEBSD_ABI_TAG";
116202b08c90SEd Maste 	case 2: return "NT_FREEBSD_NOINIT_TAG";
116302b08c90SEd Maste 	case 3: return "NT_FREEBSD_ARCH_TAG";
1164e74f411dSEd Maste 	case 4: return "NT_FREEBSD_FEATURE_CTL";
116502b08c90SEd Maste 	default: return (note_type_unknown(nt));
116602b08c90SEd Maste 	}
116702b08c90SEd Maste }
116802b08c90SEd Maste 
116902b08c90SEd Maste static const char *
117002b08c90SEd Maste note_type_freebsd_core(unsigned int nt)
117102b08c90SEd Maste {
117202b08c90SEd Maste 	switch (nt) {
117302b08c90SEd Maste 	case 1: return "NT_PRSTATUS";
117402b08c90SEd Maste 	case 2: return "NT_FPREGSET";
117502b08c90SEd Maste 	case 3: return "NT_PRPSINFO";
117602b08c90SEd Maste 	case 7: return "NT_THRMISC";
117702b08c90SEd Maste 	case 8: return "NT_PROCSTAT_PROC";
117802b08c90SEd Maste 	case 9: return "NT_PROCSTAT_FILES";
117902b08c90SEd Maste 	case 10: return "NT_PROCSTAT_VMMAP";
118002b08c90SEd Maste 	case 11: return "NT_PROCSTAT_GROUPS";
118102b08c90SEd Maste 	case 12: return "NT_PROCSTAT_UMASK";
118202b08c90SEd Maste 	case 13: return "NT_PROCSTAT_RLIMIT";
118302b08c90SEd Maste 	case 14: return "NT_PROCSTAT_OSREL";
118402b08c90SEd Maste 	case 15: return "NT_PROCSTAT_PSSTRINGS";
118502b08c90SEd Maste 	case 16: return "NT_PROCSTAT_AUXV";
1186369bd05bSJohn Baldwin 	case 17: return "NT_PTLWPINFO";
118712f7c1e8SJustin Hibbits 	case 0x100: return "NT_PPC_VMX (ppc Altivec registers)";
118812f7c1e8SJustin Hibbits 	case 0x102: return "NT_PPC_VSX (ppc VSX registers)";
118902b08c90SEd Maste 	case 0x202: return "NT_X86_XSTATE (x86 XSAVE extended state)";
1190369bd05bSJohn Baldwin 	case 0x400: return "NT_ARM_VFP (arm VFP registers)";
119102b08c90SEd Maste 	default: return (note_type_unknown(nt));
119202b08c90SEd Maste 	}
119302b08c90SEd Maste }
119402b08c90SEd Maste 
119502b08c90SEd Maste static const char *
119602b08c90SEd Maste note_type_linux_core(unsigned int nt)
119702b08c90SEd Maste {
119802b08c90SEd Maste 	switch (nt) {
119902b08c90SEd Maste 	case 1: return "NT_PRSTATUS (Process status)";
120002b08c90SEd Maste 	case 2: return "NT_FPREGSET (Floating point information)";
120102b08c90SEd Maste 	case 3: return "NT_PRPSINFO (Process information)";
120271a0c925SEd Maste 	case 4: return "NT_TASKSTRUCT (Task structure)";
120302b08c90SEd Maste 	case 6: return "NT_AUXV (Auxiliary vector)";
120402b08c90SEd Maste 	case 10: return "NT_PSTATUS (Linux process status)";
120502b08c90SEd Maste 	case 12: return "NT_FPREGS (Linux floating point regset)";
120602b08c90SEd Maste 	case 13: return "NT_PSINFO (Linux process information)";
120702b08c90SEd Maste 	case 16: return "NT_LWPSTATUS (Linux lwpstatus_t type)";
120802b08c90SEd Maste 	case 17: return "NT_LWPSINFO (Linux lwpinfo_t type)";
120971a0c925SEd Maste 	case 18: return "NT_WIN32PSTATUS (win32_pstatus structure)";
121071a0c925SEd Maste 	case 0x100: return "NT_PPC_VMX (ppc Altivec registers)";
121171a0c925SEd Maste 	case 0x102: return "NT_PPC_VSX (ppc VSX registers)";
121271a0c925SEd Maste 	case 0x202: return "NT_X86_XSTATE (x86 XSAVE extended state)";
121371a0c925SEd Maste 	case 0x300: return "NT_S390_HIGH_GPRS (s390 upper register halves)";
121471a0c925SEd Maste 	case 0x301: return "NT_S390_TIMER (s390 timer register)";
121571a0c925SEd Maste 	case 0x302: return "NT_S390_TODCMP (s390 TOD comparator register)";
121671a0c925SEd Maste 	case 0x303: return "NT_S390_TODPREG (s390 TOD programmable register)";
121771a0c925SEd Maste 	case 0x304: return "NT_S390_CTRS (s390 control registers)";
121871a0c925SEd Maste 	case 0x305: return "NT_S390_PREFIX (s390 prefix register)";
121971a0c925SEd Maste 	case 0x400: return "NT_ARM_VFP (arm VFP registers)";
122071a0c925SEd Maste 	case 0x46494c45UL: return "NT_FILE (mapped files)";
122171a0c925SEd Maste 	case 0x46E62B7FUL: return "NT_PRXFPREG (Linux user_xfpregs structure)";
122271a0c925SEd Maste 	case 0x53494749UL: return "NT_SIGINFO (siginfo_t data)";
122302b08c90SEd Maste 	default: return (note_type_unknown(nt));
122402b08c90SEd Maste 	}
122502b08c90SEd Maste }
122602b08c90SEd Maste 
122702b08c90SEd Maste static const char *
122802b08c90SEd Maste note_type_gnu(unsigned int nt)
122902b08c90SEd Maste {
123002b08c90SEd Maste 	switch (nt) {
123102b08c90SEd Maste 	case 1: return "NT_GNU_ABI_TAG";
123202b08c90SEd Maste 	case 2: return "NT_GNU_HWCAP (Hardware capabilities)";
123302b08c90SEd Maste 	case 3: return "NT_GNU_BUILD_ID (Build id set by ld(1))";
123402b08c90SEd Maste 	case 4: return "NT_GNU_GOLD_VERSION (GNU gold version)";
1235ce0c6340SEd Maste 	case 5: return "NT_GNU_PROPERTY_TYPE_0";
123602b08c90SEd Maste 	default: return (note_type_unknown(nt));
123702b08c90SEd Maste 	}
123802b08c90SEd Maste }
123902b08c90SEd Maste 
124002b08c90SEd Maste static const char *
124102b08c90SEd Maste note_type_netbsd(unsigned int nt)
124202b08c90SEd Maste {
124302b08c90SEd Maste 	switch (nt) {
124402b08c90SEd Maste 	case 1: return "NT_NETBSD_IDENT";
124502b08c90SEd Maste 	default: return (note_type_unknown(nt));
124602b08c90SEd Maste 	}
124702b08c90SEd Maste }
124802b08c90SEd Maste 
124902b08c90SEd Maste static const char *
125002b08c90SEd Maste note_type_openbsd(unsigned int nt)
125102b08c90SEd Maste {
125202b08c90SEd Maste 	switch (nt) {
125302b08c90SEd Maste 	case 1: return "NT_OPENBSD_IDENT";
125402b08c90SEd Maste 	default: return (note_type_unknown(nt));
125502b08c90SEd Maste 	}
125602b08c90SEd Maste }
125702b08c90SEd Maste 
125802b08c90SEd Maste static const char *
125902b08c90SEd Maste note_type_unknown(unsigned int nt)
12602c23cb7cSEd Maste {
12612c23cb7cSEd Maste 	static char s_nt[32];
12622c23cb7cSEd Maste 
126371a0c925SEd Maste 	snprintf(s_nt, sizeof(s_nt),
126471a0c925SEd Maste 	    nt >= 0x100 ? "<unknown: 0x%x>" : "<unknown: %u>", nt);
12652c23cb7cSEd Maste 	return (s_nt);
12662c23cb7cSEd Maste }
12672c23cb7cSEd Maste 
1268895f86f1SEd Maste static const char *
1269895f86f1SEd Maste note_type_xen(unsigned int nt)
1270895f86f1SEd Maste {
1271895f86f1SEd Maste 	switch (nt) {
1272895f86f1SEd Maste 	case 0: return "XEN_ELFNOTE_INFO";
1273895f86f1SEd Maste 	case 1: return "XEN_ELFNOTE_ENTRY";
1274895f86f1SEd Maste 	case 2: return "XEN_ELFNOTE_HYPERCALL_PAGE";
1275895f86f1SEd Maste 	case 3: return "XEN_ELFNOTE_VIRT_BASE";
1276895f86f1SEd Maste 	case 4: return "XEN_ELFNOTE_PADDR_OFFSET";
1277895f86f1SEd Maste 	case 5: return "XEN_ELFNOTE_XEN_VERSION";
1278895f86f1SEd Maste 	case 6: return "XEN_ELFNOTE_GUEST_OS";
1279895f86f1SEd Maste 	case 7: return "XEN_ELFNOTE_GUEST_VERSION";
1280895f86f1SEd Maste 	case 8: return "XEN_ELFNOTE_LOADER";
1281895f86f1SEd Maste 	case 9: return "XEN_ELFNOTE_PAE_MODE";
1282895f86f1SEd Maste 	case 10: return "XEN_ELFNOTE_FEATURES";
1283895f86f1SEd Maste 	case 11: return "XEN_ELFNOTE_BSD_SYMTAB";
1284895f86f1SEd Maste 	case 12: return "XEN_ELFNOTE_HV_START_LOW";
1285895f86f1SEd Maste 	case 13: return "XEN_ELFNOTE_L1_MFN_VALID";
1286895f86f1SEd Maste 	case 14: return "XEN_ELFNOTE_SUSPEND_CANCEL";
1287895f86f1SEd Maste 	case 15: return "XEN_ELFNOTE_INIT_P2M";
1288895f86f1SEd Maste 	case 16: return "XEN_ELFNOTE_MOD_START_PFN";
1289895f86f1SEd Maste 	case 17: return "XEN_ELFNOTE_SUPPORTED_FEATURES";
12904d8a9fafSEd Maste 	case 18: return "XEN_ELFNOTE_PHYS32_ENTRY";
1291895f86f1SEd Maste 	default: return (note_type_unknown(nt));
1292895f86f1SEd Maste 	}
1293895f86f1SEd Maste }
1294895f86f1SEd Maste 
12952c23cb7cSEd Maste static struct {
12962c23cb7cSEd Maste 	const char *name;
12972c23cb7cSEd Maste 	int value;
12982c23cb7cSEd Maste } l_flag[] = {
12992c23cb7cSEd Maste 	{"EXACT_MATCH", LL_EXACT_MATCH},
13002c23cb7cSEd Maste 	{"IGNORE_INT_VER", LL_IGNORE_INT_VER},
13012c23cb7cSEd Maste 	{"REQUIRE_MINOR", LL_REQUIRE_MINOR},
13022c23cb7cSEd Maste 	{"EXPORTS", LL_EXPORTS},
13032c23cb7cSEd Maste 	{"DELAY_LOAD", LL_DELAY_LOAD},
13042c23cb7cSEd Maste 	{"DELTA", LL_DELTA},
13052c23cb7cSEd Maste 	{NULL, 0}
13062c23cb7cSEd Maste };
13072c23cb7cSEd Maste 
13082c23cb7cSEd Maste static struct mips_option mips_exceptions_option[] = {
13092c23cb7cSEd Maste 	{OEX_PAGE0, "PAGE0"},
13102c23cb7cSEd Maste 	{OEX_SMM, "SMM"},
13112c23cb7cSEd Maste 	{OEX_PRECISEFP, "PRECISEFP"},
13122c23cb7cSEd Maste 	{OEX_DISMISS, "DISMISS"},
13132c23cb7cSEd Maste 	{0, NULL}
13142c23cb7cSEd Maste };
13152c23cb7cSEd Maste 
13162c23cb7cSEd Maste static struct mips_option mips_pad_option[] = {
13172c23cb7cSEd Maste 	{OPAD_PREFIX, "PREFIX"},
13182c23cb7cSEd Maste 	{OPAD_POSTFIX, "POSTFIX"},
13192c23cb7cSEd Maste 	{OPAD_SYMBOL, "SYMBOL"},
13202c23cb7cSEd Maste 	{0, NULL}
13212c23cb7cSEd Maste };
13222c23cb7cSEd Maste 
13232c23cb7cSEd Maste static struct mips_option mips_hwpatch_option[] = {
13242c23cb7cSEd Maste 	{OHW_R4KEOP, "R4KEOP"},
13252c23cb7cSEd Maste 	{OHW_R8KPFETCH, "R8KPFETCH"},
13262c23cb7cSEd Maste 	{OHW_R5KEOP, "R5KEOP"},
13272c23cb7cSEd Maste 	{OHW_R5KCVTL, "R5KCVTL"},
13282c23cb7cSEd Maste 	{0, NULL}
13292c23cb7cSEd Maste };
13302c23cb7cSEd Maste 
13312c23cb7cSEd Maste static struct mips_option mips_hwa_option[] = {
13322c23cb7cSEd Maste 	{OHWA0_R4KEOP_CHECKED, "R4KEOP_CHECKED"},
13332c23cb7cSEd Maste 	{OHWA0_R4KEOP_CLEAN, "R4KEOP_CLEAN"},
13342c23cb7cSEd Maste 	{0, NULL}
13352c23cb7cSEd Maste };
13362c23cb7cSEd Maste 
13372c23cb7cSEd Maste static struct mips_option mips_hwo_option[] = {
13382c23cb7cSEd Maste 	{OHWO0_FIXADE, "FIXADE"},
13392c23cb7cSEd Maste 	{0, NULL}
13402c23cb7cSEd Maste };
13412c23cb7cSEd Maste 
13422c23cb7cSEd Maste static const char *
13432c23cb7cSEd Maste option_kind(uint8_t kind)
13442c23cb7cSEd Maste {
13452c23cb7cSEd Maste 	static char s_kind[32];
13462c23cb7cSEd Maste 
13472c23cb7cSEd Maste 	switch (kind) {
13482c23cb7cSEd Maste 	case ODK_NULL: return "NULL";
13492c23cb7cSEd Maste 	case ODK_REGINFO: return "REGINFO";
13502c23cb7cSEd Maste 	case ODK_EXCEPTIONS: return "EXCEPTIONS";
13512c23cb7cSEd Maste 	case ODK_PAD: return "PAD";
13522c23cb7cSEd Maste 	case ODK_HWPATCH: return "HWPATCH";
13532c23cb7cSEd Maste 	case ODK_FILL: return "FILL";
13542c23cb7cSEd Maste 	case ODK_TAGS: return "TAGS";
13552c23cb7cSEd Maste 	case ODK_HWAND: return "HWAND";
13562c23cb7cSEd Maste 	case ODK_HWOR: return "HWOR";
13572c23cb7cSEd Maste 	case ODK_GP_GROUP: return "GP_GROUP";
13582c23cb7cSEd Maste 	case ODK_IDENT: return "IDENT";
13592c23cb7cSEd Maste 	default:
13602c23cb7cSEd Maste 		snprintf(s_kind, sizeof(s_kind), "<unknown: %u>", kind);
13612c23cb7cSEd Maste 		return (s_kind);
13622c23cb7cSEd Maste 	}
13632c23cb7cSEd Maste }
13642c23cb7cSEd Maste 
13652c23cb7cSEd Maste static const char *
13662c23cb7cSEd Maste top_tag(unsigned int tag)
13672c23cb7cSEd Maste {
13682c23cb7cSEd Maste 	static char s_top_tag[32];
13692c23cb7cSEd Maste 
13702c23cb7cSEd Maste 	switch (tag) {
13712c23cb7cSEd Maste 	case 1: return "File Attributes";
13722c23cb7cSEd Maste 	case 2: return "Section Attributes";
13732c23cb7cSEd Maste 	case 3: return "Symbol Attributes";
13742c23cb7cSEd Maste 	default:
13752c23cb7cSEd Maste 		snprintf(s_top_tag, sizeof(s_top_tag), "Unknown tag: %u", tag);
13762c23cb7cSEd Maste 		return (s_top_tag);
13772c23cb7cSEd Maste 	}
13782c23cb7cSEd Maste }
13792c23cb7cSEd Maste 
13802c23cb7cSEd Maste static const char *
13812c23cb7cSEd Maste aeabi_cpu_arch(uint64_t arch)
13822c23cb7cSEd Maste {
13832c23cb7cSEd Maste 	static char s_cpu_arch[32];
13842c23cb7cSEd Maste 
13852c23cb7cSEd Maste 	switch (arch) {
13862c23cb7cSEd Maste 	case 0: return "Pre-V4";
13872c23cb7cSEd Maste 	case 1: return "ARM v4";
13882c23cb7cSEd Maste 	case 2: return "ARM v4T";
13892c23cb7cSEd Maste 	case 3: return "ARM v5T";
13902c23cb7cSEd Maste 	case 4: return "ARM v5TE";
13912c23cb7cSEd Maste 	case 5: return "ARM v5TEJ";
13922c23cb7cSEd Maste 	case 6: return "ARM v6";
13932c23cb7cSEd Maste 	case 7: return "ARM v6KZ";
13942c23cb7cSEd Maste 	case 8: return "ARM v6T2";
13952c23cb7cSEd Maste 	case 9: return "ARM v6K";
13962c23cb7cSEd Maste 	case 10: return "ARM v7";
13972c23cb7cSEd Maste 	case 11: return "ARM v6-M";
13982c23cb7cSEd Maste 	case 12: return "ARM v6S-M";
13992c23cb7cSEd Maste 	case 13: return "ARM v7E-M";
14002c23cb7cSEd Maste 	default:
14012c23cb7cSEd Maste 		snprintf(s_cpu_arch, sizeof(s_cpu_arch),
14022c23cb7cSEd Maste 		    "Unknown (%ju)", (uintmax_t) arch);
14032c23cb7cSEd Maste 		return (s_cpu_arch);
14042c23cb7cSEd Maste 	}
14052c23cb7cSEd Maste }
14062c23cb7cSEd Maste 
14072c23cb7cSEd Maste static const char *
14082c23cb7cSEd Maste aeabi_cpu_arch_profile(uint64_t pf)
14092c23cb7cSEd Maste {
14102c23cb7cSEd Maste 	static char s_arch_profile[32];
14112c23cb7cSEd Maste 
14122c23cb7cSEd Maste 	switch (pf) {
14132c23cb7cSEd Maste 	case 0:
14142c23cb7cSEd Maste 		return "Not applicable";
14152c23cb7cSEd Maste 	case 0x41:		/* 'A' */
14162c23cb7cSEd Maste 		return "Application Profile";
14172c23cb7cSEd Maste 	case 0x52:		/* 'R' */
14182c23cb7cSEd Maste 		return "Real-Time Profile";
14192c23cb7cSEd Maste 	case 0x4D:		/* 'M' */
14202c23cb7cSEd Maste 		return "Microcontroller Profile";
14212c23cb7cSEd Maste 	case 0x53:		/* 'S' */
14222c23cb7cSEd Maste 		return "Application or Real-Time Profile";
14232c23cb7cSEd Maste 	default:
14242c23cb7cSEd Maste 		snprintf(s_arch_profile, sizeof(s_arch_profile),
14252c23cb7cSEd Maste 		    "Unknown (%ju)\n", (uintmax_t) pf);
14262c23cb7cSEd Maste 		return (s_arch_profile);
14272c23cb7cSEd Maste 	}
14282c23cb7cSEd Maste }
14292c23cb7cSEd Maste 
14302c23cb7cSEd Maste static const char *
14312c23cb7cSEd Maste aeabi_arm_isa(uint64_t ai)
14322c23cb7cSEd Maste {
14332c23cb7cSEd Maste 	static char s_ai[32];
14342c23cb7cSEd Maste 
14352c23cb7cSEd Maste 	switch (ai) {
14362c23cb7cSEd Maste 	case 0: return "No";
14372c23cb7cSEd Maste 	case 1: return "Yes";
14382c23cb7cSEd Maste 	default:
14392c23cb7cSEd Maste 		snprintf(s_ai, sizeof(s_ai), "Unknown (%ju)\n",
14402c23cb7cSEd Maste 		    (uintmax_t) ai);
14412c23cb7cSEd Maste 		return (s_ai);
14422c23cb7cSEd Maste 	}
14432c23cb7cSEd Maste }
14442c23cb7cSEd Maste 
14452c23cb7cSEd Maste static const char *
14462c23cb7cSEd Maste aeabi_thumb_isa(uint64_t ti)
14472c23cb7cSEd Maste {
14482c23cb7cSEd Maste 	static char s_ti[32];
14492c23cb7cSEd Maste 
14502c23cb7cSEd Maste 	switch (ti) {
14512c23cb7cSEd Maste 	case 0: return "No";
14522c23cb7cSEd Maste 	case 1: return "16-bit Thumb";
14532c23cb7cSEd Maste 	case 2: return "32-bit Thumb";
14542c23cb7cSEd Maste 	default:
14552c23cb7cSEd Maste 		snprintf(s_ti, sizeof(s_ti), "Unknown (%ju)\n",
14562c23cb7cSEd Maste 		    (uintmax_t) ti);
14572c23cb7cSEd Maste 		return (s_ti);
14582c23cb7cSEd Maste 	}
14592c23cb7cSEd Maste }
14602c23cb7cSEd Maste 
14612c23cb7cSEd Maste static const char *
14622c23cb7cSEd Maste aeabi_fp_arch(uint64_t fp)
14632c23cb7cSEd Maste {
14642c23cb7cSEd Maste 	static char s_fp_arch[32];
14652c23cb7cSEd Maste 
14662c23cb7cSEd Maste 	switch (fp) {
14672c23cb7cSEd Maste 	case 0: return "No";
14682c23cb7cSEd Maste 	case 1: return "VFPv1";
14692c23cb7cSEd Maste 	case 2: return "VFPv2";
14702c23cb7cSEd Maste 	case 3: return "VFPv3";
14712c23cb7cSEd Maste 	case 4: return "VFPv3-D16";
14722c23cb7cSEd Maste 	case 5: return "VFPv4";
14732c23cb7cSEd Maste 	case 6: return "VFPv4-D16";
14742c23cb7cSEd Maste 	default:
14752c23cb7cSEd Maste 		snprintf(s_fp_arch, sizeof(s_fp_arch), "Unknown (%ju)",
14762c23cb7cSEd Maste 		    (uintmax_t) fp);
14772c23cb7cSEd Maste 		return (s_fp_arch);
14782c23cb7cSEd Maste 	}
14792c23cb7cSEd Maste }
14802c23cb7cSEd Maste 
14812c23cb7cSEd Maste static const char *
14822c23cb7cSEd Maste aeabi_wmmx_arch(uint64_t wmmx)
14832c23cb7cSEd Maste {
14842c23cb7cSEd Maste 	static char s_wmmx[32];
14852c23cb7cSEd Maste 
14862c23cb7cSEd Maste 	switch (wmmx) {
14872c23cb7cSEd Maste 	case 0: return "No";
14882c23cb7cSEd Maste 	case 1: return "WMMXv1";
14892c23cb7cSEd Maste 	case 2: return "WMMXv2";
14902c23cb7cSEd Maste 	default:
14912c23cb7cSEd Maste 		snprintf(s_wmmx, sizeof(s_wmmx), "Unknown (%ju)",
14922c23cb7cSEd Maste 		    (uintmax_t) wmmx);
14932c23cb7cSEd Maste 		return (s_wmmx);
14942c23cb7cSEd Maste 	}
14952c23cb7cSEd Maste }
14962c23cb7cSEd Maste 
14972c23cb7cSEd Maste static const char *
14982c23cb7cSEd Maste aeabi_adv_simd_arch(uint64_t simd)
14992c23cb7cSEd Maste {
15002c23cb7cSEd Maste 	static char s_simd[32];
15012c23cb7cSEd Maste 
15022c23cb7cSEd Maste 	switch (simd) {
15032c23cb7cSEd Maste 	case 0: return "No";
15042c23cb7cSEd Maste 	case 1: return "NEONv1";
15052c23cb7cSEd Maste 	case 2: return "NEONv2";
15062c23cb7cSEd Maste 	default:
15072c23cb7cSEd Maste 		snprintf(s_simd, sizeof(s_simd), "Unknown (%ju)",
15082c23cb7cSEd Maste 		    (uintmax_t) simd);
15092c23cb7cSEd Maste 		return (s_simd);
15102c23cb7cSEd Maste 	}
15112c23cb7cSEd Maste }
15122c23cb7cSEd Maste 
15132c23cb7cSEd Maste static const char *
15142c23cb7cSEd Maste aeabi_pcs_config(uint64_t pcs)
15152c23cb7cSEd Maste {
15162c23cb7cSEd Maste 	static char s_pcs[32];
15172c23cb7cSEd Maste 
15182c23cb7cSEd Maste 	switch (pcs) {
15192c23cb7cSEd Maste 	case 0: return "None";
15202c23cb7cSEd Maste 	case 1: return "Bare platform";
15212c23cb7cSEd Maste 	case 2: return "Linux";
15222c23cb7cSEd Maste 	case 3: return "Linux DSO";
15232c23cb7cSEd Maste 	case 4: return "Palm OS 2004";
15242c23cb7cSEd Maste 	case 5: return "Palm OS (future)";
15252c23cb7cSEd Maste 	case 6: return "Symbian OS 2004";
15262c23cb7cSEd Maste 	case 7: return "Symbian OS (future)";
15272c23cb7cSEd Maste 	default:
15282c23cb7cSEd Maste 		snprintf(s_pcs, sizeof(s_pcs), "Unknown (%ju)",
15292c23cb7cSEd Maste 		    (uintmax_t) pcs);
15302c23cb7cSEd Maste 		return (s_pcs);
15312c23cb7cSEd Maste 	}
15322c23cb7cSEd Maste }
15332c23cb7cSEd Maste 
15342c23cb7cSEd Maste static const char *
15352c23cb7cSEd Maste aeabi_pcs_r9(uint64_t r9)
15362c23cb7cSEd Maste {
15372c23cb7cSEd Maste 	static char s_r9[32];
15382c23cb7cSEd Maste 
15392c23cb7cSEd Maste 	switch (r9) {
15402c23cb7cSEd Maste 	case 0: return "V6";
15412c23cb7cSEd Maste 	case 1: return "SB";
15422c23cb7cSEd Maste 	case 2: return "TLS pointer";
15432c23cb7cSEd Maste 	case 3: return "Unused";
15442c23cb7cSEd Maste 	default:
15452c23cb7cSEd Maste 		snprintf(s_r9, sizeof(s_r9), "Unknown (%ju)", (uintmax_t) r9);
15462c23cb7cSEd Maste 		return (s_r9);
15472c23cb7cSEd Maste 	}
15482c23cb7cSEd Maste }
15492c23cb7cSEd Maste 
15502c23cb7cSEd Maste static const char *
15512c23cb7cSEd Maste aeabi_pcs_rw(uint64_t rw)
15522c23cb7cSEd Maste {
15532c23cb7cSEd Maste 	static char s_rw[32];
15542c23cb7cSEd Maste 
15552c23cb7cSEd Maste 	switch (rw) {
15562c23cb7cSEd Maste 	case 0: return "Absolute";
15572c23cb7cSEd Maste 	case 1: return "PC-relative";
15582c23cb7cSEd Maste 	case 2: return "SB-relative";
15592c23cb7cSEd Maste 	case 3: return "None";
15602c23cb7cSEd Maste 	default:
15612c23cb7cSEd Maste 		snprintf(s_rw, sizeof(s_rw), "Unknown (%ju)", (uintmax_t) rw);
15622c23cb7cSEd Maste 		return (s_rw);
15632c23cb7cSEd Maste 	}
15642c23cb7cSEd Maste }
15652c23cb7cSEd Maste 
15662c23cb7cSEd Maste static const char *
15672c23cb7cSEd Maste aeabi_pcs_ro(uint64_t ro)
15682c23cb7cSEd Maste {
15692c23cb7cSEd Maste 	static char s_ro[32];
15702c23cb7cSEd Maste 
15712c23cb7cSEd Maste 	switch (ro) {
15722c23cb7cSEd Maste 	case 0: return "Absolute";
15732c23cb7cSEd Maste 	case 1: return "PC-relative";
15742c23cb7cSEd Maste 	case 2: return "None";
15752c23cb7cSEd Maste 	default:
15762c23cb7cSEd Maste 		snprintf(s_ro, sizeof(s_ro), "Unknown (%ju)", (uintmax_t) ro);
15772c23cb7cSEd Maste 		return (s_ro);
15782c23cb7cSEd Maste 	}
15792c23cb7cSEd Maste }
15802c23cb7cSEd Maste 
15812c23cb7cSEd Maste static const char *
15822c23cb7cSEd Maste aeabi_pcs_got(uint64_t got)
15832c23cb7cSEd Maste {
15842c23cb7cSEd Maste 	static char s_got[32];
15852c23cb7cSEd Maste 
15862c23cb7cSEd Maste 	switch (got) {
15872c23cb7cSEd Maste 	case 0: return "None";
15882c23cb7cSEd Maste 	case 1: return "direct";
15892c23cb7cSEd Maste 	case 2: return "indirect via GOT";
15902c23cb7cSEd Maste 	default:
15912c23cb7cSEd Maste 		snprintf(s_got, sizeof(s_got), "Unknown (%ju)",
15922c23cb7cSEd Maste 		    (uintmax_t) got);
15932c23cb7cSEd Maste 		return (s_got);
15942c23cb7cSEd Maste 	}
15952c23cb7cSEd Maste }
15962c23cb7cSEd Maste 
15972c23cb7cSEd Maste static const char *
15982c23cb7cSEd Maste aeabi_pcs_wchar_t(uint64_t wt)
15992c23cb7cSEd Maste {
16002c23cb7cSEd Maste 	static char s_wt[32];
16012c23cb7cSEd Maste 
16022c23cb7cSEd Maste 	switch (wt) {
16032c23cb7cSEd Maste 	case 0: return "None";
16042c23cb7cSEd Maste 	case 2: return "wchar_t size 2";
16052c23cb7cSEd Maste 	case 4: return "wchar_t size 4";
16062c23cb7cSEd Maste 	default:
16072c23cb7cSEd Maste 		snprintf(s_wt, sizeof(s_wt), "Unknown (%ju)", (uintmax_t) wt);
16082c23cb7cSEd Maste 		return (s_wt);
16092c23cb7cSEd Maste 	}
16102c23cb7cSEd Maste }
16112c23cb7cSEd Maste 
16122c23cb7cSEd Maste static const char *
16132c23cb7cSEd Maste aeabi_enum_size(uint64_t es)
16142c23cb7cSEd Maste {
16152c23cb7cSEd Maste 	static char s_es[32];
16162c23cb7cSEd Maste 
16172c23cb7cSEd Maste 	switch (es) {
16182c23cb7cSEd Maste 	case 0: return "None";
16192c23cb7cSEd Maste 	case 1: return "smallest";
16202c23cb7cSEd Maste 	case 2: return "32-bit";
16212c23cb7cSEd Maste 	case 3: return "visible 32-bit";
16222c23cb7cSEd Maste 	default:
16232c23cb7cSEd Maste 		snprintf(s_es, sizeof(s_es), "Unknown (%ju)", (uintmax_t) es);
16242c23cb7cSEd Maste 		return (s_es);
16252c23cb7cSEd Maste 	}
16262c23cb7cSEd Maste }
16272c23cb7cSEd Maste 
16282c23cb7cSEd Maste static const char *
16292c23cb7cSEd Maste aeabi_align_needed(uint64_t an)
16302c23cb7cSEd Maste {
16312c23cb7cSEd Maste 	static char s_align_n[64];
16322c23cb7cSEd Maste 
16332c23cb7cSEd Maste 	switch (an) {
16342c23cb7cSEd Maste 	case 0: return "No";
16352c23cb7cSEd Maste 	case 1: return "8-byte align";
16362c23cb7cSEd Maste 	case 2: return "4-byte align";
16372c23cb7cSEd Maste 	case 3: return "Reserved";
16382c23cb7cSEd Maste 	default:
16392c23cb7cSEd Maste 		if (an >= 4 && an <= 12)
16402c23cb7cSEd Maste 			snprintf(s_align_n, sizeof(s_align_n), "8-byte align"
16412c23cb7cSEd Maste 			    " and up to 2^%ju-byte extended align",
16422c23cb7cSEd Maste 			    (uintmax_t) an);
16432c23cb7cSEd Maste 		else
16442c23cb7cSEd Maste 			snprintf(s_align_n, sizeof(s_align_n), "Unknown (%ju)",
16452c23cb7cSEd Maste 			    (uintmax_t) an);
16462c23cb7cSEd Maste 		return (s_align_n);
16472c23cb7cSEd Maste 	}
16482c23cb7cSEd Maste }
16492c23cb7cSEd Maste 
16502c23cb7cSEd Maste static const char *
16512c23cb7cSEd Maste aeabi_align_preserved(uint64_t ap)
16522c23cb7cSEd Maste {
16532c23cb7cSEd Maste 	static char s_align_p[128];
16542c23cb7cSEd Maste 
16552c23cb7cSEd Maste 	switch (ap) {
16562c23cb7cSEd Maste 	case 0: return "No";
16572c23cb7cSEd Maste 	case 1: return "8-byte align";
16582c23cb7cSEd Maste 	case 2: return "8-byte align and SP % 8 == 0";
16592c23cb7cSEd Maste 	case 3: return "Reserved";
16602c23cb7cSEd Maste 	default:
16612c23cb7cSEd Maste 		if (ap >= 4 && ap <= 12)
16622c23cb7cSEd Maste 			snprintf(s_align_p, sizeof(s_align_p), "8-byte align"
16632c23cb7cSEd Maste 			    " and SP %% 8 == 0 and up to 2^%ju-byte extended"
16642c23cb7cSEd Maste 			    " align", (uintmax_t) ap);
16652c23cb7cSEd Maste 		else
16662c23cb7cSEd Maste 			snprintf(s_align_p, sizeof(s_align_p), "Unknown (%ju)",
16672c23cb7cSEd Maste 			    (uintmax_t) ap);
16682c23cb7cSEd Maste 		return (s_align_p);
16692c23cb7cSEd Maste 	}
16702c23cb7cSEd Maste }
16712c23cb7cSEd Maste 
16722c23cb7cSEd Maste static const char *
16732c23cb7cSEd Maste aeabi_fp_rounding(uint64_t fr)
16742c23cb7cSEd Maste {
16752c23cb7cSEd Maste 	static char s_fp_r[32];
16762c23cb7cSEd Maste 
16772c23cb7cSEd Maste 	switch (fr) {
16782c23cb7cSEd Maste 	case 0: return "Unused";
16792c23cb7cSEd Maste 	case 1: return "Needed";
16802c23cb7cSEd Maste 	default:
16812c23cb7cSEd Maste 		snprintf(s_fp_r, sizeof(s_fp_r), "Unknown (%ju)",
16822c23cb7cSEd Maste 		    (uintmax_t) fr);
16832c23cb7cSEd Maste 		return (s_fp_r);
16842c23cb7cSEd Maste 	}
16852c23cb7cSEd Maste }
16862c23cb7cSEd Maste 
16872c23cb7cSEd Maste static const char *
16882c23cb7cSEd Maste aeabi_fp_denormal(uint64_t fd)
16892c23cb7cSEd Maste {
16902c23cb7cSEd Maste 	static char s_fp_d[32];
16912c23cb7cSEd Maste 
16922c23cb7cSEd Maste 	switch (fd) {
16932c23cb7cSEd Maste 	case 0: return "Unused";
16942c23cb7cSEd Maste 	case 1: return "Needed";
16952c23cb7cSEd Maste 	case 2: return "Sign Only";
16962c23cb7cSEd Maste 	default:
16972c23cb7cSEd Maste 		snprintf(s_fp_d, sizeof(s_fp_d), "Unknown (%ju)",
16982c23cb7cSEd Maste 		    (uintmax_t) fd);
16992c23cb7cSEd Maste 		return (s_fp_d);
17002c23cb7cSEd Maste 	}
17012c23cb7cSEd Maste }
17022c23cb7cSEd Maste 
17032c23cb7cSEd Maste static const char *
17042c23cb7cSEd Maste aeabi_fp_exceptions(uint64_t fe)
17052c23cb7cSEd Maste {
17062c23cb7cSEd Maste 	static char s_fp_e[32];
17072c23cb7cSEd Maste 
17082c23cb7cSEd Maste 	switch (fe) {
17092c23cb7cSEd Maste 	case 0: return "Unused";
17102c23cb7cSEd Maste 	case 1: return "Needed";
17112c23cb7cSEd Maste 	default:
17122c23cb7cSEd Maste 		snprintf(s_fp_e, sizeof(s_fp_e), "Unknown (%ju)",
17132c23cb7cSEd Maste 		    (uintmax_t) fe);
17142c23cb7cSEd Maste 		return (s_fp_e);
17152c23cb7cSEd Maste 	}
17162c23cb7cSEd Maste }
17172c23cb7cSEd Maste 
17182c23cb7cSEd Maste static const char *
17192c23cb7cSEd Maste aeabi_fp_user_exceptions(uint64_t fu)
17202c23cb7cSEd Maste {
17212c23cb7cSEd Maste 	static char s_fp_u[32];
17222c23cb7cSEd Maste 
17232c23cb7cSEd Maste 	switch (fu) {
17242c23cb7cSEd Maste 	case 0: return "Unused";
17252c23cb7cSEd Maste 	case 1: return "Needed";
17262c23cb7cSEd Maste 	default:
17272c23cb7cSEd Maste 		snprintf(s_fp_u, sizeof(s_fp_u), "Unknown (%ju)",
17282c23cb7cSEd Maste 		    (uintmax_t) fu);
17292c23cb7cSEd Maste 		return (s_fp_u);
17302c23cb7cSEd Maste 	}
17312c23cb7cSEd Maste }
17322c23cb7cSEd Maste 
17332c23cb7cSEd Maste static const char *
17342c23cb7cSEd Maste aeabi_fp_number_model(uint64_t fn)
17352c23cb7cSEd Maste {
17362c23cb7cSEd Maste 	static char s_fp_n[32];
17372c23cb7cSEd Maste 
17382c23cb7cSEd Maste 	switch (fn) {
17392c23cb7cSEd Maste 	case 0: return "Unused";
17402c23cb7cSEd Maste 	case 1: return "IEEE 754 normal";
17412c23cb7cSEd Maste 	case 2: return "RTABI";
17422c23cb7cSEd Maste 	case 3: return "IEEE 754";
17432c23cb7cSEd Maste 	default:
17442c23cb7cSEd Maste 		snprintf(s_fp_n, sizeof(s_fp_n), "Unknown (%ju)",
17452c23cb7cSEd Maste 		    (uintmax_t) fn);
17462c23cb7cSEd Maste 		return (s_fp_n);
17472c23cb7cSEd Maste 	}
17482c23cb7cSEd Maste }
17492c23cb7cSEd Maste 
17502c23cb7cSEd Maste static const char *
17512c23cb7cSEd Maste aeabi_fp_16bit_format(uint64_t fp16)
17522c23cb7cSEd Maste {
17532c23cb7cSEd Maste 	static char s_fp_16[64];
17542c23cb7cSEd Maste 
17552c23cb7cSEd Maste 	switch (fp16) {
17562c23cb7cSEd Maste 	case 0: return "None";
17572c23cb7cSEd Maste 	case 1: return "IEEE 754";
17582c23cb7cSEd Maste 	case 2: return "VFPv3/Advanced SIMD (alternative format)";
17592c23cb7cSEd Maste 	default:
17602c23cb7cSEd Maste 		snprintf(s_fp_16, sizeof(s_fp_16), "Unknown (%ju)",
17612c23cb7cSEd Maste 		    (uintmax_t) fp16);
17622c23cb7cSEd Maste 		return (s_fp_16);
17632c23cb7cSEd Maste 	}
17642c23cb7cSEd Maste }
17652c23cb7cSEd Maste 
17662c23cb7cSEd Maste static const char *
17672c23cb7cSEd Maste aeabi_mpext(uint64_t mp)
17682c23cb7cSEd Maste {
17692c23cb7cSEd Maste 	static char s_mp[32];
17702c23cb7cSEd Maste 
17712c23cb7cSEd Maste 	switch (mp) {
17722c23cb7cSEd Maste 	case 0: return "Not allowed";
17732c23cb7cSEd Maste 	case 1: return "Allowed";
17742c23cb7cSEd Maste 	default:
17752c23cb7cSEd Maste 		snprintf(s_mp, sizeof(s_mp), "Unknown (%ju)",
17762c23cb7cSEd Maste 		    (uintmax_t) mp);
17772c23cb7cSEd Maste 		return (s_mp);
17782c23cb7cSEd Maste 	}
17792c23cb7cSEd Maste }
17802c23cb7cSEd Maste 
17812c23cb7cSEd Maste static const char *
17822c23cb7cSEd Maste aeabi_div(uint64_t du)
17832c23cb7cSEd Maste {
17842c23cb7cSEd Maste 	static char s_du[32];
17852c23cb7cSEd Maste 
17862c23cb7cSEd Maste 	switch (du) {
17872c23cb7cSEd Maste 	case 0: return "Yes (V7-R/V7-M)";
17882c23cb7cSEd Maste 	case 1: return "No";
17892c23cb7cSEd Maste 	case 2: return "Yes (V7-A)";
17902c23cb7cSEd Maste 	default:
17912c23cb7cSEd Maste 		snprintf(s_du, sizeof(s_du), "Unknown (%ju)",
17922c23cb7cSEd Maste 		    (uintmax_t) du);
17932c23cb7cSEd Maste 		return (s_du);
17942c23cb7cSEd Maste 	}
17952c23cb7cSEd Maste }
17962c23cb7cSEd Maste 
17972c23cb7cSEd Maste static const char *
17982c23cb7cSEd Maste aeabi_t2ee(uint64_t t2ee)
17992c23cb7cSEd Maste {
18002c23cb7cSEd Maste 	static char s_t2ee[32];
18012c23cb7cSEd Maste 
18022c23cb7cSEd Maste 	switch (t2ee) {
18032c23cb7cSEd Maste 	case 0: return "Not allowed";
18042c23cb7cSEd Maste 	case 1: return "Allowed";
18052c23cb7cSEd Maste 	default:
18062c23cb7cSEd Maste 		snprintf(s_t2ee, sizeof(s_t2ee), "Unknown(%ju)",
18072c23cb7cSEd Maste 		    (uintmax_t) t2ee);
18082c23cb7cSEd Maste 		return (s_t2ee);
18092c23cb7cSEd Maste 	}
18102c23cb7cSEd Maste 
18112c23cb7cSEd Maste }
18122c23cb7cSEd Maste 
18132c23cb7cSEd Maste static const char *
18142c23cb7cSEd Maste aeabi_hardfp(uint64_t hfp)
18152c23cb7cSEd Maste {
18162c23cb7cSEd Maste 	static char s_hfp[32];
18172c23cb7cSEd Maste 
18182c23cb7cSEd Maste 	switch (hfp) {
18192c23cb7cSEd Maste 	case 0: return "Tag_FP_arch";
18202c23cb7cSEd Maste 	case 1: return "only SP";
18212c23cb7cSEd Maste 	case 2: return "only DP";
18222c23cb7cSEd Maste 	case 3: return "both SP and DP";
18232c23cb7cSEd Maste 	default:
18242c23cb7cSEd Maste 		snprintf(s_hfp, sizeof(s_hfp), "Unknown (%ju)",
18252c23cb7cSEd Maste 		    (uintmax_t) hfp);
18262c23cb7cSEd Maste 		return (s_hfp);
18272c23cb7cSEd Maste 	}
18282c23cb7cSEd Maste }
18292c23cb7cSEd Maste 
18302c23cb7cSEd Maste static const char *
18312c23cb7cSEd Maste aeabi_vfp_args(uint64_t va)
18322c23cb7cSEd Maste {
18332c23cb7cSEd Maste 	static char s_va[32];
18342c23cb7cSEd Maste 
18352c23cb7cSEd Maste 	switch (va) {
18362c23cb7cSEd Maste 	case 0: return "AAPCS (base variant)";
18372c23cb7cSEd Maste 	case 1: return "AAPCS (VFP variant)";
18382c23cb7cSEd Maste 	case 2: return "toolchain-specific";
18392c23cb7cSEd Maste 	default:
18402c23cb7cSEd Maste 		snprintf(s_va, sizeof(s_va), "Unknown (%ju)", (uintmax_t) va);
18412c23cb7cSEd Maste 		return (s_va);
18422c23cb7cSEd Maste 	}
18432c23cb7cSEd Maste }
18442c23cb7cSEd Maste 
18452c23cb7cSEd Maste static const char *
18462c23cb7cSEd Maste aeabi_wmmx_args(uint64_t wa)
18472c23cb7cSEd Maste {
18482c23cb7cSEd Maste 	static char s_wa[32];
18492c23cb7cSEd Maste 
18502c23cb7cSEd Maste 	switch (wa) {
18512c23cb7cSEd Maste 	case 0: return "AAPCS (base variant)";
18522c23cb7cSEd Maste 	case 1: return "Intel WMMX";
18532c23cb7cSEd Maste 	case 2: return "toolchain-specific";
18542c23cb7cSEd Maste 	default:
18552c23cb7cSEd Maste 		snprintf(s_wa, sizeof(s_wa), "Unknown(%ju)", (uintmax_t) wa);
18562c23cb7cSEd Maste 		return (s_wa);
18572c23cb7cSEd Maste 	}
18582c23cb7cSEd Maste }
18592c23cb7cSEd Maste 
18602c23cb7cSEd Maste static const char *
18612c23cb7cSEd Maste aeabi_unaligned_access(uint64_t ua)
18622c23cb7cSEd Maste {
18632c23cb7cSEd Maste 	static char s_ua[32];
18642c23cb7cSEd Maste 
18652c23cb7cSEd Maste 	switch (ua) {
18662c23cb7cSEd Maste 	case 0: return "Not allowed";
18672c23cb7cSEd Maste 	case 1: return "Allowed";
18682c23cb7cSEd Maste 	default:
18692c23cb7cSEd Maste 		snprintf(s_ua, sizeof(s_ua), "Unknown(%ju)", (uintmax_t) ua);
18702c23cb7cSEd Maste 		return (s_ua);
18712c23cb7cSEd Maste 	}
18722c23cb7cSEd Maste }
18732c23cb7cSEd Maste 
18742c23cb7cSEd Maste static const char *
18752c23cb7cSEd Maste aeabi_fp_hpext(uint64_t fh)
18762c23cb7cSEd Maste {
18772c23cb7cSEd Maste 	static char s_fh[32];
18782c23cb7cSEd Maste 
18792c23cb7cSEd Maste 	switch (fh) {
18802c23cb7cSEd Maste 	case 0: return "Not allowed";
18812c23cb7cSEd Maste 	case 1: return "Allowed";
18822c23cb7cSEd Maste 	default:
18832c23cb7cSEd Maste 		snprintf(s_fh, sizeof(s_fh), "Unknown(%ju)", (uintmax_t) fh);
18842c23cb7cSEd Maste 		return (s_fh);
18852c23cb7cSEd Maste 	}
18862c23cb7cSEd Maste }
18872c23cb7cSEd Maste 
18882c23cb7cSEd Maste static const char *
18892c23cb7cSEd Maste aeabi_optm_goal(uint64_t og)
18902c23cb7cSEd Maste {
18912c23cb7cSEd Maste 	static char s_og[32];
18922c23cb7cSEd Maste 
18932c23cb7cSEd Maste 	switch (og) {
18942c23cb7cSEd Maste 	case 0: return "None";
18952c23cb7cSEd Maste 	case 1: return "Speed";
18962c23cb7cSEd Maste 	case 2: return "Speed aggressive";
18972c23cb7cSEd Maste 	case 3: return "Space";
18982c23cb7cSEd Maste 	case 4: return "Space aggressive";
18992c23cb7cSEd Maste 	case 5: return "Debugging";
19002c23cb7cSEd Maste 	case 6: return "Best Debugging";
19012c23cb7cSEd Maste 	default:
19022c23cb7cSEd Maste 		snprintf(s_og, sizeof(s_og), "Unknown(%ju)", (uintmax_t) og);
19032c23cb7cSEd Maste 		return (s_og);
19042c23cb7cSEd Maste 	}
19052c23cb7cSEd Maste }
19062c23cb7cSEd Maste 
19072c23cb7cSEd Maste static const char *
19082c23cb7cSEd Maste aeabi_fp_optm_goal(uint64_t fog)
19092c23cb7cSEd Maste {
19102c23cb7cSEd Maste 	static char s_fog[32];
19112c23cb7cSEd Maste 
19122c23cb7cSEd Maste 	switch (fog) {
19132c23cb7cSEd Maste 	case 0: return "None";
19142c23cb7cSEd Maste 	case 1: return "Speed";
19152c23cb7cSEd Maste 	case 2: return "Speed aggressive";
19162c23cb7cSEd Maste 	case 3: return "Space";
19172c23cb7cSEd Maste 	case 4: return "Space aggressive";
19182c23cb7cSEd Maste 	case 5: return "Accurary";
19192c23cb7cSEd Maste 	case 6: return "Best Accurary";
19202c23cb7cSEd Maste 	default:
19212c23cb7cSEd Maste 		snprintf(s_fog, sizeof(s_fog), "Unknown(%ju)",
19222c23cb7cSEd Maste 		    (uintmax_t) fog);
19232c23cb7cSEd Maste 		return (s_fog);
19242c23cb7cSEd Maste 	}
19252c23cb7cSEd Maste }
19262c23cb7cSEd Maste 
19272c23cb7cSEd Maste static const char *
19282c23cb7cSEd Maste aeabi_virtual(uint64_t vt)
19292c23cb7cSEd Maste {
19302c23cb7cSEd Maste 	static char s_virtual[64];
19312c23cb7cSEd Maste 
19322c23cb7cSEd Maste 	switch (vt) {
19332c23cb7cSEd Maste 	case 0: return "No";
19342c23cb7cSEd Maste 	case 1: return "TrustZone";
19352c23cb7cSEd Maste 	case 2: return "Virtualization extension";
19362c23cb7cSEd Maste 	case 3: return "TrustZone and virtualization extension";
19372c23cb7cSEd Maste 	default:
19382c23cb7cSEd Maste 		snprintf(s_virtual, sizeof(s_virtual), "Unknown(%ju)",
19392c23cb7cSEd Maste 		    (uintmax_t) vt);
19402c23cb7cSEd Maste 		return (s_virtual);
19412c23cb7cSEd Maste 	}
19422c23cb7cSEd Maste }
19432c23cb7cSEd Maste 
19442c23cb7cSEd Maste static struct {
19452c23cb7cSEd Maste 	uint64_t tag;
19462c23cb7cSEd Maste 	const char *s_tag;
19472c23cb7cSEd Maste 	const char *(*get_desc)(uint64_t val);
19482c23cb7cSEd Maste } aeabi_tags[] = {
19492c23cb7cSEd Maste 	{4, "Tag_CPU_raw_name", NULL},
19502c23cb7cSEd Maste 	{5, "Tag_CPU_name", NULL},
19512c23cb7cSEd Maste 	{6, "Tag_CPU_arch", aeabi_cpu_arch},
19522c23cb7cSEd Maste 	{7, "Tag_CPU_arch_profile", aeabi_cpu_arch_profile},
19532c23cb7cSEd Maste 	{8, "Tag_ARM_ISA_use", aeabi_arm_isa},
19542c23cb7cSEd Maste 	{9, "Tag_THUMB_ISA_use", aeabi_thumb_isa},
19552c23cb7cSEd Maste 	{10, "Tag_FP_arch", aeabi_fp_arch},
19562c23cb7cSEd Maste 	{11, "Tag_WMMX_arch", aeabi_wmmx_arch},
19572c23cb7cSEd Maste 	{12, "Tag_Advanced_SIMD_arch", aeabi_adv_simd_arch},
19582c23cb7cSEd Maste 	{13, "Tag_PCS_config", aeabi_pcs_config},
19592c23cb7cSEd Maste 	{14, "Tag_ABI_PCS_R9_use", aeabi_pcs_r9},
19602c23cb7cSEd Maste 	{15, "Tag_ABI_PCS_RW_data", aeabi_pcs_rw},
19612c23cb7cSEd Maste 	{16, "Tag_ABI_PCS_RO_data", aeabi_pcs_ro},
19622c23cb7cSEd Maste 	{17, "Tag_ABI_PCS_GOT_use", aeabi_pcs_got},
19632c23cb7cSEd Maste 	{18, "Tag_ABI_PCS_wchar_t", aeabi_pcs_wchar_t},
19642c23cb7cSEd Maste 	{19, "Tag_ABI_FP_rounding", aeabi_fp_rounding},
19652c23cb7cSEd Maste 	{20, "Tag_ABI_FP_denormal", aeabi_fp_denormal},
19662c23cb7cSEd Maste 	{21, "Tag_ABI_FP_exceptions", aeabi_fp_exceptions},
19672c23cb7cSEd Maste 	{22, "Tag_ABI_FP_user_exceptions", aeabi_fp_user_exceptions},
19682c23cb7cSEd Maste 	{23, "Tag_ABI_FP_number_model", aeabi_fp_number_model},
19692c23cb7cSEd Maste 	{24, "Tag_ABI_align_needed", aeabi_align_needed},
19702c23cb7cSEd Maste 	{25, "Tag_ABI_align_preserved", aeabi_align_preserved},
19712c23cb7cSEd Maste 	{26, "Tag_ABI_enum_size", aeabi_enum_size},
19722c23cb7cSEd Maste 	{27, "Tag_ABI_HardFP_use", aeabi_hardfp},
19732c23cb7cSEd Maste 	{28, "Tag_ABI_VFP_args", aeabi_vfp_args},
19742c23cb7cSEd Maste 	{29, "Tag_ABI_WMMX_args", aeabi_wmmx_args},
19752c23cb7cSEd Maste 	{30, "Tag_ABI_optimization_goals", aeabi_optm_goal},
19762c23cb7cSEd Maste 	{31, "Tag_ABI_FP_optimization_goals", aeabi_fp_optm_goal},
19772c23cb7cSEd Maste 	{32, "Tag_compatibility", NULL},
19782c23cb7cSEd Maste 	{34, "Tag_CPU_unaligned_access", aeabi_unaligned_access},
19792c23cb7cSEd Maste 	{36, "Tag_FP_HP_extension", aeabi_fp_hpext},
19802c23cb7cSEd Maste 	{38, "Tag_ABI_FP_16bit_format", aeabi_fp_16bit_format},
19812c23cb7cSEd Maste 	{42, "Tag_MPextension_use", aeabi_mpext},
19822c23cb7cSEd Maste 	{44, "Tag_DIV_use", aeabi_div},
19832c23cb7cSEd Maste 	{64, "Tag_nodefaults", NULL},
19842c23cb7cSEd Maste 	{65, "Tag_also_compatible_with", NULL},
19852c23cb7cSEd Maste 	{66, "Tag_T2EE_use", aeabi_t2ee},
19862c23cb7cSEd Maste 	{67, "Tag_conformance", NULL},
19872c23cb7cSEd Maste 	{68, "Tag_Virtualization_use", aeabi_virtual},
19882c23cb7cSEd Maste 	{70, "Tag_MPextension_use", aeabi_mpext},
19892c23cb7cSEd Maste };
19902c23cb7cSEd Maste 
19912c23cb7cSEd Maste static const char *
19922c23cb7cSEd Maste mips_abi_fp(uint64_t fp)
19932c23cb7cSEd Maste {
19942c23cb7cSEd Maste 	static char s_mips_abi_fp[64];
19952c23cb7cSEd Maste 
19962c23cb7cSEd Maste 	switch (fp) {
19972c23cb7cSEd Maste 	case 0: return "N/A";
19982c23cb7cSEd Maste 	case 1: return "Hard float (double precision)";
19992c23cb7cSEd Maste 	case 2: return "Hard float (single precision)";
20002c23cb7cSEd Maste 	case 3: return "Soft float";
20012c23cb7cSEd Maste 	case 4: return "64-bit float (-mips32r2 -mfp64)";
20022c23cb7cSEd Maste 	default:
20032c23cb7cSEd Maste 		snprintf(s_mips_abi_fp, sizeof(s_mips_abi_fp), "Unknown(%ju)",
20042c23cb7cSEd Maste 		    (uintmax_t) fp);
20052c23cb7cSEd Maste 		return (s_mips_abi_fp);
20062c23cb7cSEd Maste 	}
20072c23cb7cSEd Maste }
20082c23cb7cSEd Maste 
20092c23cb7cSEd Maste static const char *
20102c23cb7cSEd Maste ppc_abi_fp(uint64_t fp)
20112c23cb7cSEd Maste {
20122c23cb7cSEd Maste 	static char s_ppc_abi_fp[64];
20132c23cb7cSEd Maste 
20142c23cb7cSEd Maste 	switch (fp) {
20152c23cb7cSEd Maste 	case 0: return "N/A";
20162c23cb7cSEd Maste 	case 1: return "Hard float (double precision)";
20172c23cb7cSEd Maste 	case 2: return "Soft float";
20182c23cb7cSEd Maste 	case 3: return "Hard float (single precision)";
20192c23cb7cSEd Maste 	default:
20202c23cb7cSEd Maste 		snprintf(s_ppc_abi_fp, sizeof(s_ppc_abi_fp), "Unknown(%ju)",
20212c23cb7cSEd Maste 		    (uintmax_t) fp);
20222c23cb7cSEd Maste 		return (s_ppc_abi_fp);
20232c23cb7cSEd Maste 	}
20242c23cb7cSEd Maste }
20252c23cb7cSEd Maste 
20262c23cb7cSEd Maste static const char *
20272c23cb7cSEd Maste ppc_abi_vector(uint64_t vec)
20282c23cb7cSEd Maste {
20292c23cb7cSEd Maste 	static char s_vec[64];
20302c23cb7cSEd Maste 
20312c23cb7cSEd Maste 	switch (vec) {
20322c23cb7cSEd Maste 	case 0: return "N/A";
20332c23cb7cSEd Maste 	case 1: return "Generic purpose registers";
20342c23cb7cSEd Maste 	case 2: return "AltiVec registers";
20352c23cb7cSEd Maste 	case 3: return "SPE registers";
20362c23cb7cSEd Maste 	default:
20372c23cb7cSEd Maste 		snprintf(s_vec, sizeof(s_vec), "Unknown(%ju)", (uintmax_t) vec);
20382c23cb7cSEd Maste 		return (s_vec);
20392c23cb7cSEd Maste 	}
20402c23cb7cSEd Maste }
20412c23cb7cSEd Maste 
2042cf781b2eSEd Maste static const char *
2043cf781b2eSEd Maste dwarf_reg(unsigned int mach, unsigned int reg)
2044cf781b2eSEd Maste {
2045cf781b2eSEd Maste 
2046cf781b2eSEd Maste 	switch (mach) {
2047cf781b2eSEd Maste 	case EM_386:
20483ef90571SEd Maste 	case EM_IAMCU:
2049cf781b2eSEd Maste 		switch (reg) {
2050cf781b2eSEd Maste 		case 0: return "eax";
2051cf781b2eSEd Maste 		case 1: return "ecx";
2052cf781b2eSEd Maste 		case 2: return "edx";
2053cf781b2eSEd Maste 		case 3: return "ebx";
2054cf781b2eSEd Maste 		case 4: return "esp";
2055cf781b2eSEd Maste 		case 5: return "ebp";
2056cf781b2eSEd Maste 		case 6: return "esi";
2057cf781b2eSEd Maste 		case 7: return "edi";
2058cf781b2eSEd Maste 		case 8: return "eip";
2059cf781b2eSEd Maste 		case 9: return "eflags";
2060cf781b2eSEd Maste 		case 11: return "st0";
2061cf781b2eSEd Maste 		case 12: return "st1";
2062cf781b2eSEd Maste 		case 13: return "st2";
2063cf781b2eSEd Maste 		case 14: return "st3";
2064cf781b2eSEd Maste 		case 15: return "st4";
2065cf781b2eSEd Maste 		case 16: return "st5";
2066cf781b2eSEd Maste 		case 17: return "st6";
2067cf781b2eSEd Maste 		case 18: return "st7";
2068cf781b2eSEd Maste 		case 21: return "xmm0";
2069cf781b2eSEd Maste 		case 22: return "xmm1";
2070cf781b2eSEd Maste 		case 23: return "xmm2";
2071cf781b2eSEd Maste 		case 24: return "xmm3";
2072cf781b2eSEd Maste 		case 25: return "xmm4";
2073cf781b2eSEd Maste 		case 26: return "xmm5";
2074cf781b2eSEd Maste 		case 27: return "xmm6";
2075cf781b2eSEd Maste 		case 28: return "xmm7";
2076cf781b2eSEd Maste 		case 29: return "mm0";
2077cf781b2eSEd Maste 		case 30: return "mm1";
2078cf781b2eSEd Maste 		case 31: return "mm2";
2079cf781b2eSEd Maste 		case 32: return "mm3";
2080cf781b2eSEd Maste 		case 33: return "mm4";
2081cf781b2eSEd Maste 		case 34: return "mm5";
2082cf781b2eSEd Maste 		case 35: return "mm6";
2083cf781b2eSEd Maste 		case 36: return "mm7";
2084cf781b2eSEd Maste 		case 37: return "fcw";
2085cf781b2eSEd Maste 		case 38: return "fsw";
2086cf781b2eSEd Maste 		case 39: return "mxcsr";
2087cf781b2eSEd Maste 		case 40: return "es";
2088cf781b2eSEd Maste 		case 41: return "cs";
2089cf781b2eSEd Maste 		case 42: return "ss";
2090cf781b2eSEd Maste 		case 43: return "ds";
2091cf781b2eSEd Maste 		case 44: return "fs";
2092cf781b2eSEd Maste 		case 45: return "gs";
2093cf781b2eSEd Maste 		case 48: return "tr";
2094cf781b2eSEd Maste 		case 49: return "ldtr";
2095cf781b2eSEd Maste 		default: return (NULL);
2096cf781b2eSEd Maste 		}
20971a0c2201SMitchell Horne 	case EM_RISCV:
20981a0c2201SMitchell Horne 		switch (reg) {
20991a0c2201SMitchell Horne 		case 0: return "zero";
21001a0c2201SMitchell Horne 		case 1: return "ra";
21011a0c2201SMitchell Horne 		case 2: return "sp";
21021a0c2201SMitchell Horne 		case 3: return "gp";
21031a0c2201SMitchell Horne 		case 4: return "tp";
21041a0c2201SMitchell Horne 		case 5: return "t0";
21051a0c2201SMitchell Horne 		case 6: return "t1";
21061a0c2201SMitchell Horne 		case 7: return "t2";
21071a0c2201SMitchell Horne 		case 8: return "s0";
21081a0c2201SMitchell Horne 		case 9: return "s1";
21091a0c2201SMitchell Horne 		case 10: return "a0";
21101a0c2201SMitchell Horne 		case 11: return "a1";
21111a0c2201SMitchell Horne 		case 12: return "a2";
21121a0c2201SMitchell Horne 		case 13: return "a3";
21131a0c2201SMitchell Horne 		case 14: return "a4";
21141a0c2201SMitchell Horne 		case 15: return "a5";
21151a0c2201SMitchell Horne 		case 16: return "a6";
21161a0c2201SMitchell Horne 		case 17: return "a7";
21171a0c2201SMitchell Horne 		case 18: return "s2";
21181a0c2201SMitchell Horne 		case 19: return "s3";
21191a0c2201SMitchell Horne 		case 20: return "s4";
21201a0c2201SMitchell Horne 		case 21: return "s5";
21211a0c2201SMitchell Horne 		case 22: return "s6";
21221a0c2201SMitchell Horne 		case 23: return "s7";
21231a0c2201SMitchell Horne 		case 24: return "s8";
21241a0c2201SMitchell Horne 		case 25: return "s9";
21251a0c2201SMitchell Horne 		case 26: return "s10";
21261a0c2201SMitchell Horne 		case 27: return "s11";
21271a0c2201SMitchell Horne 		case 28: return "t3";
21281a0c2201SMitchell Horne 		case 29: return "t4";
21291a0c2201SMitchell Horne 		case 30: return "t5";
21301a0c2201SMitchell Horne 		case 31: return "t6";
21311a0c2201SMitchell Horne 		case 32: return "ft0";
21321a0c2201SMitchell Horne 		case 33: return "ft1";
21331a0c2201SMitchell Horne 		case 34: return "ft2";
21341a0c2201SMitchell Horne 		case 35: return "ft3";
21351a0c2201SMitchell Horne 		case 36: return "ft4";
21361a0c2201SMitchell Horne 		case 37: return "ft5";
21371a0c2201SMitchell Horne 		case 38: return "ft6";
21381a0c2201SMitchell Horne 		case 39: return "ft7";
21391a0c2201SMitchell Horne 		case 40: return "fs0";
21401a0c2201SMitchell Horne 		case 41: return "fs1";
21411a0c2201SMitchell Horne 		case 42: return "fa0";
21421a0c2201SMitchell Horne 		case 43: return "fa1";
21431a0c2201SMitchell Horne 		case 44: return "fa2";
21441a0c2201SMitchell Horne 		case 45: return "fa3";
21451a0c2201SMitchell Horne 		case 46: return "fa4";
21461a0c2201SMitchell Horne 		case 47: return "fa5";
21471a0c2201SMitchell Horne 		case 48: return "fa6";
21481a0c2201SMitchell Horne 		case 49: return "fa7";
21491a0c2201SMitchell Horne 		case 50: return "fs2";
21501a0c2201SMitchell Horne 		case 51: return "fs3";
21511a0c2201SMitchell Horne 		case 52: return "fs4";
21521a0c2201SMitchell Horne 		case 53: return "fs5";
21531a0c2201SMitchell Horne 		case 54: return "fs6";
21541a0c2201SMitchell Horne 		case 55: return "fs7";
21551a0c2201SMitchell Horne 		case 56: return "fs8";
21561a0c2201SMitchell Horne 		case 57: return "fs9";
21571a0c2201SMitchell Horne 		case 58: return "fs10";
21581a0c2201SMitchell Horne 		case 59: return "fs11";
21591a0c2201SMitchell Horne 		case 60: return "ft8";
21601a0c2201SMitchell Horne 		case 61: return "ft9";
21611a0c2201SMitchell Horne 		case 62: return "ft10";
21621a0c2201SMitchell Horne 		case 63: return "ft11";
21631a0c2201SMitchell Horne 		default: return (NULL);
21641a0c2201SMitchell Horne 		}
2165cf781b2eSEd Maste 	case EM_X86_64:
2166cf781b2eSEd Maste 		switch (reg) {
2167cf781b2eSEd Maste 		case 0: return "rax";
2168cf781b2eSEd Maste 		case 1: return "rdx";
2169cf781b2eSEd Maste 		case 2: return "rcx";
2170cf781b2eSEd Maste 		case 3: return "rbx";
2171cf781b2eSEd Maste 		case 4: return "rsi";
2172cf781b2eSEd Maste 		case 5: return "rdi";
2173cf781b2eSEd Maste 		case 6: return "rbp";
2174cf781b2eSEd Maste 		case 7: return "rsp";
2175cf781b2eSEd Maste 		case 16: return "rip";
2176cf781b2eSEd Maste 		case 17: return "xmm0";
2177cf781b2eSEd Maste 		case 18: return "xmm1";
2178cf781b2eSEd Maste 		case 19: return "xmm2";
2179cf781b2eSEd Maste 		case 20: return "xmm3";
2180cf781b2eSEd Maste 		case 21: return "xmm4";
2181cf781b2eSEd Maste 		case 22: return "xmm5";
2182cf781b2eSEd Maste 		case 23: return "xmm6";
2183cf781b2eSEd Maste 		case 24: return "xmm7";
2184cf781b2eSEd Maste 		case 25: return "xmm8";
2185cf781b2eSEd Maste 		case 26: return "xmm9";
2186cf781b2eSEd Maste 		case 27: return "xmm10";
2187cf781b2eSEd Maste 		case 28: return "xmm11";
2188cf781b2eSEd Maste 		case 29: return "xmm12";
2189cf781b2eSEd Maste 		case 30: return "xmm13";
2190cf781b2eSEd Maste 		case 31: return "xmm14";
2191cf781b2eSEd Maste 		case 32: return "xmm15";
2192cf781b2eSEd Maste 		case 33: return "st0";
2193cf781b2eSEd Maste 		case 34: return "st1";
2194cf781b2eSEd Maste 		case 35: return "st2";
2195cf781b2eSEd Maste 		case 36: return "st3";
2196cf781b2eSEd Maste 		case 37: return "st4";
2197cf781b2eSEd Maste 		case 38: return "st5";
2198cf781b2eSEd Maste 		case 39: return "st6";
2199cf781b2eSEd Maste 		case 40: return "st7";
2200cf781b2eSEd Maste 		case 41: return "mm0";
2201cf781b2eSEd Maste 		case 42: return "mm1";
2202cf781b2eSEd Maste 		case 43: return "mm2";
2203cf781b2eSEd Maste 		case 44: return "mm3";
2204cf781b2eSEd Maste 		case 45: return "mm4";
2205cf781b2eSEd Maste 		case 46: return "mm5";
2206cf781b2eSEd Maste 		case 47: return "mm6";
2207cf781b2eSEd Maste 		case 48: return "mm7";
2208cf781b2eSEd Maste 		case 49: return "rflags";
2209cf781b2eSEd Maste 		case 50: return "es";
2210cf781b2eSEd Maste 		case 51: return "cs";
2211cf781b2eSEd Maste 		case 52: return "ss";
2212cf781b2eSEd Maste 		case 53: return "ds";
2213cf781b2eSEd Maste 		case 54: return "fs";
2214cf781b2eSEd Maste 		case 55: return "gs";
2215cf781b2eSEd Maste 		case 58: return "fs.base";
2216cf781b2eSEd Maste 		case 59: return "gs.base";
2217cf781b2eSEd Maste 		case 62: return "tr";
2218cf781b2eSEd Maste 		case 63: return "ldtr";
2219cf781b2eSEd Maste 		case 64: return "mxcsr";
2220cf781b2eSEd Maste 		case 65: return "fcw";
2221cf781b2eSEd Maste 		case 66: return "fsw";
2222cf781b2eSEd Maste 		default: return (NULL);
2223cf781b2eSEd Maste 		}
2224cf781b2eSEd Maste 	default:
2225cf781b2eSEd Maste 		return (NULL);
2226cf781b2eSEd Maste 	}
2227cf781b2eSEd Maste }
2228cf781b2eSEd Maste 
22292c23cb7cSEd Maste static void
22302c23cb7cSEd Maste dump_ehdr(struct readelf *re)
22312c23cb7cSEd Maste {
2232714935beSConrad Meyer 	size_t		 phnum, shnum, shstrndx;
22332c23cb7cSEd Maste 	int		 i;
22342c23cb7cSEd Maste 
22352c23cb7cSEd Maste 	printf("ELF Header:\n");
22362c23cb7cSEd Maste 
22372c23cb7cSEd Maste 	/* e_ident[]. */
22382c23cb7cSEd Maste 	printf("  Magic:   ");
22392c23cb7cSEd Maste 	for (i = 0; i < EI_NIDENT; i++)
22402c23cb7cSEd Maste 		printf("%.2x ", re->ehdr.e_ident[i]);
22412c23cb7cSEd Maste 	putchar('\n');
22422c23cb7cSEd Maste 
22432c23cb7cSEd Maste 	/* EI_CLASS. */
22442c23cb7cSEd Maste 	printf("%-37s%s\n", "  Class:", elf_class(re->ehdr.e_ident[EI_CLASS]));
22452c23cb7cSEd Maste 
22462c23cb7cSEd Maste 	/* EI_DATA. */
22472c23cb7cSEd Maste 	printf("%-37s%s\n", "  Data:", elf_endian(re->ehdr.e_ident[EI_DATA]));
22482c23cb7cSEd Maste 
22492c23cb7cSEd Maste 	/* EI_VERSION. */
22502c23cb7cSEd Maste 	printf("%-37s%d %s\n", "  Version:", re->ehdr.e_ident[EI_VERSION],
22512c23cb7cSEd Maste 	    elf_ver(re->ehdr.e_ident[EI_VERSION]));
22522c23cb7cSEd Maste 
22532c23cb7cSEd Maste 	/* EI_OSABI. */
22542c23cb7cSEd Maste 	printf("%-37s%s\n", "  OS/ABI:", elf_osabi(re->ehdr.e_ident[EI_OSABI]));
22552c23cb7cSEd Maste 
22562c23cb7cSEd Maste 	/* EI_ABIVERSION. */
22572c23cb7cSEd Maste 	printf("%-37s%d\n", "  ABI Version:", re->ehdr.e_ident[EI_ABIVERSION]);
22582c23cb7cSEd Maste 
22592c23cb7cSEd Maste 	/* e_type. */
22602c23cb7cSEd Maste 	printf("%-37s%s\n", "  Type:", elf_type(re->ehdr.e_type));
22612c23cb7cSEd Maste 
22622c23cb7cSEd Maste 	/* e_machine. */
22632c23cb7cSEd Maste 	printf("%-37s%s\n", "  Machine:", elf_machine(re->ehdr.e_machine));
22642c23cb7cSEd Maste 
22652c23cb7cSEd Maste 	/* e_version. */
22662c23cb7cSEd Maste 	printf("%-37s%#x\n", "  Version:", re->ehdr.e_version);
22672c23cb7cSEd Maste 
22682c23cb7cSEd Maste 	/* e_entry. */
22692c23cb7cSEd Maste 	printf("%-37s%#jx\n", "  Entry point address:",
22702c23cb7cSEd Maste 	    (uintmax_t)re->ehdr.e_entry);
22712c23cb7cSEd Maste 
22722c23cb7cSEd Maste 	/* e_phoff. */
22732c23cb7cSEd Maste 	printf("%-37s%ju (bytes into file)\n", "  Start of program headers:",
22742c23cb7cSEd Maste 	    (uintmax_t)re->ehdr.e_phoff);
22752c23cb7cSEd Maste 
22762c23cb7cSEd Maste 	/* e_shoff. */
22772c23cb7cSEd Maste 	printf("%-37s%ju (bytes into file)\n", "  Start of section headers:",
22782c23cb7cSEd Maste 	    (uintmax_t)re->ehdr.e_shoff);
22792c23cb7cSEd Maste 
22802c23cb7cSEd Maste 	/* e_flags. */
22812c23cb7cSEd Maste 	printf("%-37s%#x", "  Flags:", re->ehdr.e_flags);
22822c23cb7cSEd Maste 	dump_eflags(re, re->ehdr.e_flags);
22832c23cb7cSEd Maste 	putchar('\n');
22842c23cb7cSEd Maste 
22852c23cb7cSEd Maste 	/* e_ehsize. */
22862c23cb7cSEd Maste 	printf("%-37s%u (bytes)\n", "  Size of this header:",
22872c23cb7cSEd Maste 	    re->ehdr.e_ehsize);
22882c23cb7cSEd Maste 
22892c23cb7cSEd Maste 	/* e_phentsize. */
22902c23cb7cSEd Maste 	printf("%-37s%u (bytes)\n", "  Size of program headers:",
22912c23cb7cSEd Maste 	    re->ehdr.e_phentsize);
22922c23cb7cSEd Maste 
22932c23cb7cSEd Maste 	/* e_phnum. */
2294714935beSConrad Meyer 	printf("%-37s%u", "  Number of program headers:", re->ehdr.e_phnum);
2295714935beSConrad Meyer 	if (re->ehdr.e_phnum == PN_XNUM) {
2296714935beSConrad Meyer 		/* Extended program header numbering is in use. */
2297714935beSConrad Meyer 		if (elf_getphnum(re->elf, &phnum))
2298714935beSConrad Meyer 			printf(" (%zu)", phnum);
2299714935beSConrad Meyer 	}
2300714935beSConrad Meyer 	putchar('\n');
23012c23cb7cSEd Maste 
23022c23cb7cSEd Maste 	/* e_shentsize. */
23032c23cb7cSEd Maste 	printf("%-37s%u (bytes)\n", "  Size of section headers:",
23042c23cb7cSEd Maste 	    re->ehdr.e_shentsize);
23052c23cb7cSEd Maste 
23062c23cb7cSEd Maste 	/* e_shnum. */
23072c23cb7cSEd Maste 	printf("%-37s%u", "  Number of section headers:", re->ehdr.e_shnum);
23082c23cb7cSEd Maste 	if (re->ehdr.e_shnum == SHN_UNDEF) {
23092c23cb7cSEd Maste 		/* Extended section numbering is in use. */
23102c23cb7cSEd Maste 		if (elf_getshnum(re->elf, &shnum))
23112c23cb7cSEd Maste 			printf(" (%ju)", (uintmax_t)shnum);
23122c23cb7cSEd Maste 	}
23132c23cb7cSEd Maste 	putchar('\n');
23142c23cb7cSEd Maste 
23152c23cb7cSEd Maste 	/* e_shstrndx. */
23162c23cb7cSEd Maste 	printf("%-37s%u", "  Section header string table index:",
23172c23cb7cSEd Maste 	    re->ehdr.e_shstrndx);
23182c23cb7cSEd Maste 	if (re->ehdr.e_shstrndx == SHN_XINDEX) {
23192c23cb7cSEd Maste 		/* Extended section numbering is in use. */
23202c23cb7cSEd Maste 		if (elf_getshstrndx(re->elf, &shstrndx))
23212c23cb7cSEd Maste 			printf(" (%ju)", (uintmax_t)shstrndx);
23222c23cb7cSEd Maste 	}
23232c23cb7cSEd Maste 	putchar('\n');
23242c23cb7cSEd Maste }
23252c23cb7cSEd Maste 
23262c23cb7cSEd Maste static void
23272c23cb7cSEd Maste dump_eflags(struct readelf *re, uint64_t e_flags)
23282c23cb7cSEd Maste {
23292c23cb7cSEd Maste 	struct eflags_desc *edesc;
23302c23cb7cSEd Maste 	int arm_eabi;
23312c23cb7cSEd Maste 
23322c23cb7cSEd Maste 	edesc = NULL;
23332c23cb7cSEd Maste 	switch (re->ehdr.e_machine) {
23342c23cb7cSEd Maste 	case EM_ARM:
23352c23cb7cSEd Maste 		arm_eabi = (e_flags & EF_ARM_EABIMASK) >> 24;
23362c23cb7cSEd Maste 		if (arm_eabi == 0)
23372c23cb7cSEd Maste 			printf(", GNU EABI");
23382c23cb7cSEd Maste 		else if (arm_eabi <= 5)
23392c23cb7cSEd Maste 			printf(", Version%d EABI", arm_eabi);
23402c23cb7cSEd Maste 		edesc = arm_eflags_desc;
23412c23cb7cSEd Maste 		break;
23422c23cb7cSEd Maste 	case EM_MIPS:
23432c23cb7cSEd Maste 	case EM_MIPS_RS3_LE:
23442c23cb7cSEd Maste 		switch ((e_flags & EF_MIPS_ARCH) >> 28) {
23452c23cb7cSEd Maste 		case 0:	printf(", mips1"); break;
23462c23cb7cSEd Maste 		case 1: printf(", mips2"); break;
23472c23cb7cSEd Maste 		case 2: printf(", mips3"); break;
23482c23cb7cSEd Maste 		case 3: printf(", mips4"); break;
23492c23cb7cSEd Maste 		case 4: printf(", mips5"); break;
23502c23cb7cSEd Maste 		case 5: printf(", mips32"); break;
23512c23cb7cSEd Maste 		case 6: printf(", mips64"); break;
23522c23cb7cSEd Maste 		case 7: printf(", mips32r2"); break;
23532c23cb7cSEd Maste 		case 8: printf(", mips64r2"); break;
23542c23cb7cSEd Maste 		default: break;
23552c23cb7cSEd Maste 		}
23562c23cb7cSEd Maste 		switch ((e_flags & 0x00FF0000) >> 16) {
23572c23cb7cSEd Maste 		case 0x81: printf(", 3900"); break;
23582c23cb7cSEd Maste 		case 0x82: printf(", 4010"); break;
23592c23cb7cSEd Maste 		case 0x83: printf(", 4100"); break;
23602c23cb7cSEd Maste 		case 0x85: printf(", 4650"); break;
23612c23cb7cSEd Maste 		case 0x87: printf(", 4120"); break;
23622c23cb7cSEd Maste 		case 0x88: printf(", 4111"); break;
23632c23cb7cSEd Maste 		case 0x8a: printf(", sb1"); break;
23642c23cb7cSEd Maste 		case 0x8b: printf(", octeon"); break;
23652c23cb7cSEd Maste 		case 0x8c: printf(", xlr"); break;
23662c23cb7cSEd Maste 		case 0x91: printf(", 5400"); break;
23672c23cb7cSEd Maste 		case 0x98: printf(", 5500"); break;
23682c23cb7cSEd Maste 		case 0x99: printf(", 9000"); break;
23692c23cb7cSEd Maste 		case 0xa0: printf(", loongson-2e"); break;
23702c23cb7cSEd Maste 		case 0xa1: printf(", loongson-2f"); break;
23712c23cb7cSEd Maste 		default: break;
23722c23cb7cSEd Maste 		}
23732c23cb7cSEd Maste 		switch ((e_flags & 0x0000F000) >> 12) {
23742c23cb7cSEd Maste 		case 1: printf(", o32"); break;
23752c23cb7cSEd Maste 		case 2: printf(", o64"); break;
23762c23cb7cSEd Maste 		case 3: printf(", eabi32"); break;
23772c23cb7cSEd Maste 		case 4: printf(", eabi64"); break;
23782c23cb7cSEd Maste 		default: break;
23792c23cb7cSEd Maste 		}
23802c23cb7cSEd Maste 		edesc = mips_eflags_desc;
23812c23cb7cSEd Maste 		break;
23822c23cb7cSEd Maste 	case EM_PPC64:
2383857e20a2SLeandro Lupori 		switch (e_flags) {
2384857e20a2SLeandro Lupori 		case 0: printf(", Unspecified or Power ELF V1 ABI"); break;
2385857e20a2SLeandro Lupori 		case 1: printf(", Power ELF V1 ABI"); break;
2386857e20a2SLeandro Lupori 		case 2: printf(", OpenPOWER ELF V2 ABI"); break;
2387857e20a2SLeandro Lupori 		default: break;
2388857e20a2SLeandro Lupori 		}
2389d003e0d7SEd Maste 		/* FALLTHROUGH */
2390857e20a2SLeandro Lupori 	case EM_PPC:
23912c23cb7cSEd Maste 		edesc = powerpc_eflags_desc;
23922c23cb7cSEd Maste 		break;
2393b0084180SMitchell Horne 	case EM_RISCV:
2394b0084180SMitchell Horne 		switch (e_flags & EF_RISCV_FLOAT_ABI_MASK) {
2395b0084180SMitchell Horne 		case EF_RISCV_FLOAT_ABI_SOFT:
2396b0084180SMitchell Horne 			printf(", soft-float ABI");
2397b0084180SMitchell Horne 			break;
2398b0084180SMitchell Horne 		case EF_RISCV_FLOAT_ABI_SINGLE:
2399b0084180SMitchell Horne 			printf(", single-float ABI");
2400b0084180SMitchell Horne 			break;
2401b0084180SMitchell Horne 		case EF_RISCV_FLOAT_ABI_DOUBLE:
2402b0084180SMitchell Horne 			printf(", double-float ABI");
2403b0084180SMitchell Horne 			break;
2404b0084180SMitchell Horne 		case EF_RISCV_FLOAT_ABI_QUAD:
2405b0084180SMitchell Horne 			printf(", quad-float ABI");
2406b0084180SMitchell Horne 			break;
2407b0084180SMitchell Horne 		}
2408b0084180SMitchell Horne 		edesc = riscv_eflags_desc;
2409b0084180SMitchell Horne 		break;
24102c23cb7cSEd Maste 	case EM_SPARC:
24112c23cb7cSEd Maste 	case EM_SPARC32PLUS:
24122c23cb7cSEd Maste 	case EM_SPARCV9:
24132c23cb7cSEd Maste 		switch ((e_flags & EF_SPARCV9_MM)) {
24142c23cb7cSEd Maste 		case EF_SPARCV9_TSO: printf(", tso"); break;
24152c23cb7cSEd Maste 		case EF_SPARCV9_PSO: printf(", pso"); break;
24162c23cb7cSEd Maste 		case EF_SPARCV9_MM: printf(", rmo"); break;
24172c23cb7cSEd Maste 		default: break;
24182c23cb7cSEd Maste 		}
24192c23cb7cSEd Maste 		edesc = sparc_eflags_desc;
24202c23cb7cSEd Maste 		break;
24212c23cb7cSEd Maste 	default:
24222c23cb7cSEd Maste 		break;
24232c23cb7cSEd Maste 	}
24242c23cb7cSEd Maste 
24252c23cb7cSEd Maste 	if (edesc != NULL) {
24262c23cb7cSEd Maste 		while (edesc->desc != NULL) {
24272c23cb7cSEd Maste 			if (e_flags & edesc->flag)
24282c23cb7cSEd Maste 				printf(", %s", edesc->desc);
24292c23cb7cSEd Maste 			edesc++;
24302c23cb7cSEd Maste 		}
24312c23cb7cSEd Maste 	}
24322c23cb7cSEd Maste }
24332c23cb7cSEd Maste 
24342c23cb7cSEd Maste static void
24352c23cb7cSEd Maste dump_phdr(struct readelf *re)
24362c23cb7cSEd Maste {
24372c23cb7cSEd Maste 	const char	*rawfile;
24382c23cb7cSEd Maste 	GElf_Phdr	 phdr;
2439b00fe64fSEd Maste 	size_t		 phnum, size;
24402c23cb7cSEd Maste 	int		 i, j;
24412c23cb7cSEd Maste 
24422c23cb7cSEd Maste #define	PH_HDR	"Type", "Offset", "VirtAddr", "PhysAddr", "FileSiz",	\
24432c23cb7cSEd Maste 		"MemSiz", "Flg", "Align"
244420136ffcSEd Maste #define	PH_CT	phdr_type(re->ehdr.e_machine, phdr.p_type),		\
244520136ffcSEd Maste 		(uintmax_t)phdr.p_offset, (uintmax_t)phdr.p_vaddr,	\
244620136ffcSEd Maste 		(uintmax_t)phdr.p_paddr, (uintmax_t)phdr.p_filesz,	\
244720136ffcSEd Maste 		(uintmax_t)phdr.p_memsz,				\
24482c23cb7cSEd Maste 		phdr.p_flags & PF_R ? 'R' : ' ',			\
24492c23cb7cSEd Maste 		phdr.p_flags & PF_W ? 'W' : ' ',			\
24502c23cb7cSEd Maste 		phdr.p_flags & PF_X ? 'E' : ' ',			\
24512c23cb7cSEd Maste 		(uintmax_t)phdr.p_align
24522c23cb7cSEd Maste 
24532c23cb7cSEd Maste 	if (elf_getphnum(re->elf, &phnum) == 0) {
24542c23cb7cSEd Maste 		warnx("elf_getphnum failed: %s", elf_errmsg(-1));
24552c23cb7cSEd Maste 		return;
24562c23cb7cSEd Maste 	}
24572c23cb7cSEd Maste 	if (phnum == 0) {
24582c23cb7cSEd Maste 		printf("\nThere are no program headers in this file.\n");
24592c23cb7cSEd Maste 		return;
24602c23cb7cSEd Maste 	}
24612c23cb7cSEd Maste 
24622c23cb7cSEd Maste 	printf("\nElf file type is %s", elf_type(re->ehdr.e_type));
24632c23cb7cSEd Maste 	printf("\nEntry point 0x%jx\n", (uintmax_t)re->ehdr.e_entry);
24642c23cb7cSEd Maste 	printf("There are %ju program headers, starting at offset %ju\n",
24652c23cb7cSEd Maste 	    (uintmax_t)phnum, (uintmax_t)re->ehdr.e_phoff);
24662c23cb7cSEd Maste 
24672c23cb7cSEd Maste 	/* Dump program headers. */
24682c23cb7cSEd Maste 	printf("\nProgram Headers:\n");
24692c23cb7cSEd Maste 	if (re->ec == ELFCLASS32)
24702c23cb7cSEd Maste 		printf("  %-15s%-9s%-11s%-11s%-8s%-8s%-4s%s\n", PH_HDR);
24712c23cb7cSEd Maste 	else if (re->options & RE_WW)
24722c23cb7cSEd Maste 		printf("  %-15s%-9s%-19s%-19s%-9s%-9s%-4s%s\n", PH_HDR);
24732c23cb7cSEd Maste 	else
24742c23cb7cSEd Maste 		printf("  %-15s%-19s%-19s%s\n                 %-19s%-20s"
24752c23cb7cSEd Maste 		    "%-7s%s\n", PH_HDR);
24762c23cb7cSEd Maste 	for (i = 0; (size_t) i < phnum; i++) {
24772c23cb7cSEd Maste 		if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
24782c23cb7cSEd Maste 			warnx("gelf_getphdr failed: %s", elf_errmsg(-1));
24792c23cb7cSEd Maste 			continue;
24802c23cb7cSEd Maste 		}
24812c23cb7cSEd Maste 		/* TODO: Add arch-specific segment type dump. */
24822c23cb7cSEd Maste 		if (re->ec == ELFCLASS32)
24832c23cb7cSEd Maste 			printf("  %-14.14s 0x%6.6jx 0x%8.8jx 0x%8.8jx "
24842c23cb7cSEd Maste 			    "0x%5.5jx 0x%5.5jx %c%c%c %#jx\n", PH_CT);
24852c23cb7cSEd Maste 		else if (re->options & RE_WW)
24862c23cb7cSEd Maste 			printf("  %-14.14s 0x%6.6jx 0x%16.16jx 0x%16.16jx "
24872c23cb7cSEd Maste 			    "0x%6.6jx 0x%6.6jx %c%c%c %#jx\n", PH_CT);
24882c23cb7cSEd Maste 		else
24892c23cb7cSEd Maste 			printf("  %-14.14s 0x%16.16jx 0x%16.16jx 0x%16.16jx\n"
24902c23cb7cSEd Maste 			    "                 0x%16.16jx 0x%16.16jx  %c%c%c"
24912c23cb7cSEd Maste 			    "    %#jx\n", PH_CT);
24922c23cb7cSEd Maste 		if (phdr.p_type == PT_INTERP) {
2493b00fe64fSEd Maste 			if ((rawfile = elf_rawfile(re->elf, &size)) == NULL) {
24942c23cb7cSEd Maste 				warnx("elf_rawfile failed: %s", elf_errmsg(-1));
24952c23cb7cSEd Maste 				continue;
24962c23cb7cSEd Maste 			}
2497b00fe64fSEd Maste 			if (phdr.p_offset >= size) {
2498b00fe64fSEd Maste 				warnx("invalid program header offset");
2499b00fe64fSEd Maste 				continue;
2500b00fe64fSEd Maste 			}
25012c23cb7cSEd Maste 			printf("      [Requesting program interpreter: %s]\n",
25022c23cb7cSEd Maste 				rawfile + phdr.p_offset);
25032c23cb7cSEd Maste 		}
25042c23cb7cSEd Maste 	}
25052c23cb7cSEd Maste 
25062c23cb7cSEd Maste 	/* Dump section to segment mapping. */
25072c23cb7cSEd Maste 	if (re->shnum == 0)
25082c23cb7cSEd Maste 		return;
25092c23cb7cSEd Maste 	printf("\n Section to Segment mapping:\n");
25102c23cb7cSEd Maste 	printf("  Segment Sections...\n");
25112c23cb7cSEd Maste 	for (i = 0; (size_t)i < phnum; i++) {
25122c23cb7cSEd Maste 		if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
25132c23cb7cSEd Maste 			warnx("gelf_getphdr failed: %s", elf_errmsg(-1));
25142c23cb7cSEd Maste 			continue;
25152c23cb7cSEd Maste 		}
25162c23cb7cSEd Maste 		printf("   %2.2d     ", i);
25172c23cb7cSEd Maste 		/* skip NULL section. */
251848d41ef0SPhil Shafer 		for (j = 1; (size_t)j < re->shnum; j++) {
251948d41ef0SPhil Shafer 			if (re->sl[j].off < phdr.p_offset)
252048d41ef0SPhil Shafer 				continue;
252148d41ef0SPhil Shafer 			if (re->sl[j].off + re->sl[j].sz >
252248d41ef0SPhil Shafer 			    phdr.p_offset + phdr.p_filesz &&
252348d41ef0SPhil Shafer 			    re->sl[j].type != SHT_NOBITS)
252448d41ef0SPhil Shafer 				continue;
252548d41ef0SPhil Shafer 			if (re->sl[j].addr < phdr.p_vaddr ||
252648d41ef0SPhil Shafer 			    re->sl[j].addr + re->sl[j].sz >
252795fd7f26SEd Maste 			    phdr.p_vaddr + phdr.p_memsz)
252848d41ef0SPhil Shafer 				continue;
252948d41ef0SPhil Shafer 			if (phdr.p_type == PT_TLS &&
253048d41ef0SPhil Shafer 			    (re->sl[j].flags & SHF_TLS) == 0)
253148d41ef0SPhil Shafer 				continue;
25322c23cb7cSEd Maste 			printf("%s ", re->sl[j].name);
253348d41ef0SPhil Shafer 		}
25342c23cb7cSEd Maste 		printf("\n");
25352c23cb7cSEd Maste 	}
25362c23cb7cSEd Maste #undef	PH_HDR
25372c23cb7cSEd Maste #undef	PH_CT
25382c23cb7cSEd Maste }
25392c23cb7cSEd Maste 
25402c23cb7cSEd Maste static char *
25412c23cb7cSEd Maste section_flags(struct readelf *re, struct section *s)
25422c23cb7cSEd Maste {
25432c23cb7cSEd Maste #define BUF_SZ 256
25442c23cb7cSEd Maste 	static char	buf[BUF_SZ];
25452c23cb7cSEd Maste 	int		i, p, nb;
25462c23cb7cSEd Maste 
25472c23cb7cSEd Maste 	p = 0;
25482c23cb7cSEd Maste 	nb = re->ec == ELFCLASS32 ? 8 : 16;
25492c23cb7cSEd Maste 	if (re->options & RE_T) {
25502c23cb7cSEd Maste 		snprintf(buf, BUF_SZ, "[%*.*jx]: ", nb, nb,
25512c23cb7cSEd Maste 		    (uintmax_t)s->flags);
25522c23cb7cSEd Maste 		p += nb + 4;
25532c23cb7cSEd Maste 	}
25542c23cb7cSEd Maste 	for (i = 0; section_flag[i].ln != NULL; i++) {
25552c23cb7cSEd Maste 		if ((s->flags & section_flag[i].value) == 0)
25562c23cb7cSEd Maste 			continue;
25572c23cb7cSEd Maste 		if (re->options & RE_T) {
25582c23cb7cSEd Maste 			snprintf(&buf[p], BUF_SZ - p, "%s, ",
25592c23cb7cSEd Maste 			    section_flag[i].ln);
25602c23cb7cSEd Maste 			p += strlen(section_flag[i].ln) + 2;
25612c23cb7cSEd Maste 		} else
25622c23cb7cSEd Maste 			buf[p++] = section_flag[i].sn;
25632c23cb7cSEd Maste 	}
25642c23cb7cSEd Maste 	if (re->options & RE_T && p > nb + 4)
25652c23cb7cSEd Maste 		p -= 2;
25662c23cb7cSEd Maste 	buf[p] = '\0';
25672c23cb7cSEd Maste 
25682c23cb7cSEd Maste 	return (buf);
25692c23cb7cSEd Maste }
25702c23cb7cSEd Maste 
25712c23cb7cSEd Maste static void
25722c23cb7cSEd Maste dump_shdr(struct readelf *re)
25732c23cb7cSEd Maste {
25742c23cb7cSEd Maste 	struct section	*s;
25752c23cb7cSEd Maste 	int		 i;
25762c23cb7cSEd Maste 
25772c23cb7cSEd Maste #define	S_HDR	"[Nr] Name", "Type", "Addr", "Off", "Size", "ES",	\
25782c23cb7cSEd Maste 		"Flg", "Lk", "Inf", "Al"
25792c23cb7cSEd Maste #define	S_HDRL	"[Nr] Name", "Type", "Address", "Offset", "Size",	\
25802c23cb7cSEd Maste 		"EntSize", "Flags", "Link", "Info", "Align"
25812c23cb7cSEd Maste #define	ST_HDR	"[Nr] Name", "Type", "Addr", "Off", "Size", "ES",	\
25822c23cb7cSEd Maste 		"Lk", "Inf", "Al", "Flags"
25832c23cb7cSEd Maste #define	ST_HDRL	"[Nr] Name", "Type", "Address", "Offset", "Link",	\
25842c23cb7cSEd Maste 		"Size", "EntSize", "Info", "Align", "Flags"
25852c23cb7cSEd Maste #define	S_CT	i, s->name, section_type(re->ehdr.e_machine, s->type),	\
25862c23cb7cSEd Maste 		(uintmax_t)s->addr, (uintmax_t)s->off, (uintmax_t)s->sz,\
25872c23cb7cSEd Maste 		(uintmax_t)s->entsize, section_flags(re, s),		\
25882c23cb7cSEd Maste 		s->link, s->info, (uintmax_t)s->align
25892c23cb7cSEd Maste #define	ST_CT	i, s->name, section_type(re->ehdr.e_machine, s->type),  \
25902c23cb7cSEd Maste 		(uintmax_t)s->addr, (uintmax_t)s->off, (uintmax_t)s->sz,\
25912c23cb7cSEd Maste 		(uintmax_t)s->entsize, s->link, s->info,		\
25922c23cb7cSEd Maste 		(uintmax_t)s->align, section_flags(re, s)
25932c23cb7cSEd Maste #define	ST_CTL	i, s->name, section_type(re->ehdr.e_machine, s->type),  \
25942c23cb7cSEd Maste 		(uintmax_t)s->addr, (uintmax_t)s->off, s->link,		\
25952c23cb7cSEd Maste 		(uintmax_t)s->sz, (uintmax_t)s->entsize, s->info,	\
25962c23cb7cSEd Maste 		(uintmax_t)s->align, section_flags(re, s)
25972c23cb7cSEd Maste 
25982c23cb7cSEd Maste 	if (re->shnum == 0) {
25992c23cb7cSEd Maste 		printf("\nThere are no sections in this file.\n");
26002c23cb7cSEd Maste 		return;
26012c23cb7cSEd Maste 	}
26022c23cb7cSEd Maste 	printf("There are %ju section headers, starting at offset 0x%jx:\n",
26032c23cb7cSEd Maste 	    (uintmax_t)re->shnum, (uintmax_t)re->ehdr.e_shoff);
26042c23cb7cSEd Maste 	printf("\nSection Headers:\n");
26052c23cb7cSEd Maste 	if (re->ec == ELFCLASS32) {
26062c23cb7cSEd Maste 		if (re->options & RE_T)
26072c23cb7cSEd Maste 			printf("  %s\n       %-16s%-9s%-7s%-7s%-5s%-3s%-4s%s\n"
26082c23cb7cSEd Maste 			    "%12s\n", ST_HDR);
26092c23cb7cSEd Maste 		else
26102c23cb7cSEd Maste 			printf("  %-23s%-16s%-9s%-7s%-7s%-3s%-4s%-3s%-4s%s\n",
26112c23cb7cSEd Maste 			    S_HDR);
26122c23cb7cSEd Maste 	} else if (re->options & RE_WW) {
26132c23cb7cSEd Maste 		if (re->options & RE_T)
26142c23cb7cSEd Maste 			printf("  %s\n       %-16s%-17s%-7s%-7s%-5s%-3s%-4s%s\n"
26152c23cb7cSEd Maste 			    "%12s\n", ST_HDR);
26162c23cb7cSEd Maste 		else
26172c23cb7cSEd Maste 			printf("  %-23s%-16s%-17s%-7s%-7s%-3s%-4s%-3s%-4s%s\n",
26182c23cb7cSEd Maste 			    S_HDR);
26192c23cb7cSEd Maste 	} else {
26202c23cb7cSEd Maste 		if (re->options & RE_T)
26212c23cb7cSEd Maste 			printf("  %s\n       %-18s%-17s%-18s%s\n       %-18s"
26222c23cb7cSEd Maste 			    "%-17s%-18s%s\n%12s\n", ST_HDRL);
26232c23cb7cSEd Maste 		else
26242c23cb7cSEd Maste 			printf("  %-23s%-17s%-18s%s\n       %-18s%-17s%-7s%"
26252c23cb7cSEd Maste 			    "-6s%-6s%s\n", S_HDRL);
26262c23cb7cSEd Maste 	}
26272c23cb7cSEd Maste 	for (i = 0; (size_t)i < re->shnum; i++) {
26282c23cb7cSEd Maste 		s = &re->sl[i];
26292c23cb7cSEd Maste 		if (re->ec == ELFCLASS32) {
26302c23cb7cSEd Maste 			if (re->options & RE_T)
26312c23cb7cSEd Maste 				printf("  [%2d] %s\n       %-15.15s %8.8jx"
26322c23cb7cSEd Maste 				    " %6.6jx %6.6jx %2.2jx  %2u %3u %2ju\n"
26332c23cb7cSEd Maste 				    "       %s\n", ST_CT);
26342c23cb7cSEd Maste 			else
26352c23cb7cSEd Maste 				printf("  [%2d] %-17.17s %-15.15s %8.8jx"
26362c23cb7cSEd Maste 				    " %6.6jx %6.6jx %2.2jx %3s %2u %3u %2ju\n",
26372c23cb7cSEd Maste 				    S_CT);
26382c23cb7cSEd Maste 		} else if (re->options & RE_WW) {
26392c23cb7cSEd Maste 			if (re->options & RE_T)
26402c23cb7cSEd Maste 				printf("  [%2d] %s\n       %-15.15s %16.16jx"
26412c23cb7cSEd Maste 				    " %6.6jx %6.6jx %2.2jx  %2u %3u %2ju\n"
26422c23cb7cSEd Maste 				    "       %s\n", ST_CT);
26432c23cb7cSEd Maste 			else
26442c23cb7cSEd Maste 				printf("  [%2d] %-17.17s %-15.15s %16.16jx"
26452c23cb7cSEd Maste 				    " %6.6jx %6.6jx %2.2jx %3s %2u %3u %2ju\n",
26462c23cb7cSEd Maste 				    S_CT);
26472c23cb7cSEd Maste 		} else {
26482c23cb7cSEd Maste 			if (re->options & RE_T)
26492c23cb7cSEd Maste 				printf("  [%2d] %s\n       %-15.15s  %16.16jx"
26502c23cb7cSEd Maste 				    "  %16.16jx  %u\n       %16.16jx %16.16jx"
26512c23cb7cSEd Maste 				    "  %-16u  %ju\n       %s\n", ST_CTL);
26522c23cb7cSEd Maste 			else
26532c23cb7cSEd Maste 				printf("  [%2d] %-17.17s %-15.15s  %16.16jx"
26542c23cb7cSEd Maste 				    "  %8.8jx\n       %16.16jx  %16.16jx "
26552c23cb7cSEd Maste 				    "%3s      %2u   %3u     %ju\n", S_CT);
26562c23cb7cSEd Maste 		}
26572c23cb7cSEd Maste 	}
26582c23cb7cSEd Maste 	if ((re->options & RE_T) == 0)
26592c23cb7cSEd Maste 		printf("Key to Flags:\n  W (write), A (alloc),"
26602c23cb7cSEd Maste 		    " X (execute), M (merge), S (strings)\n"
26612c23cb7cSEd Maste 		    "  I (info), L (link order), G (group), x (unknown)\n"
26622c23cb7cSEd Maste 		    "  O (extra OS processing required)"
26632c23cb7cSEd Maste 		    " o (OS specific), p (processor specific)\n");
26642c23cb7cSEd Maste 
26652c23cb7cSEd Maste #undef	S_HDR
26662c23cb7cSEd Maste #undef	S_HDRL
26672c23cb7cSEd Maste #undef	ST_HDR
26682c23cb7cSEd Maste #undef	ST_HDRL
26692c23cb7cSEd Maste #undef	S_CT
26702c23cb7cSEd Maste #undef	ST_CT
26712c23cb7cSEd Maste #undef	ST_CTL
26722c23cb7cSEd Maste }
26732c23cb7cSEd Maste 
267471edbbfdSEd Maste /*
267571edbbfdSEd Maste  * Return number of entries in the given section. We'd prefer ent_count be a
267671edbbfdSEd Maste  * size_t *, but libelf APIs already use int for section indices.
267771edbbfdSEd Maste  */
267871edbbfdSEd Maste static int
267971edbbfdSEd Maste get_ent_count(struct section *s, int *ent_count)
268071edbbfdSEd Maste {
268171edbbfdSEd Maste 	if (s->entsize == 0) {
268271edbbfdSEd Maste 		warnx("section %s has entry size 0", s->name);
268371edbbfdSEd Maste 		return (0);
268471edbbfdSEd Maste 	} else if (s->sz / s->entsize > INT_MAX) {
268571edbbfdSEd Maste 		warnx("section %s has invalid section count", s->name);
268671edbbfdSEd Maste 		return (0);
268771edbbfdSEd Maste 	}
268871edbbfdSEd Maste 	*ent_count = (int)(s->sz / s->entsize);
268971edbbfdSEd Maste 	return (1);
269071edbbfdSEd Maste }
269171edbbfdSEd Maste 
26922c23cb7cSEd Maste static void
26932c23cb7cSEd Maste dump_dynamic(struct readelf *re)
26942c23cb7cSEd Maste {
26952c23cb7cSEd Maste 	GElf_Dyn	 dyn;
26962c23cb7cSEd Maste 	Elf_Data	*d;
26972c23cb7cSEd Maste 	struct section	*s;
26982c23cb7cSEd Maste 	int		 elferr, i, is_dynamic, j, jmax, nentries;
26992c23cb7cSEd Maste 
27002c23cb7cSEd Maste 	is_dynamic = 0;
27012c23cb7cSEd Maste 
27022c23cb7cSEd Maste 	for (i = 0; (size_t)i < re->shnum; i++) {
27032c23cb7cSEd Maste 		s = &re->sl[i];
27042c23cb7cSEd Maste 		if (s->type != SHT_DYNAMIC)
27052c23cb7cSEd Maste 			continue;
27062c23cb7cSEd Maste 		(void) elf_errno();
27072c23cb7cSEd Maste 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
27082c23cb7cSEd Maste 			elferr = elf_errno();
27092c23cb7cSEd Maste 			if (elferr != 0)
27102c23cb7cSEd Maste 				warnx("elf_getdata failed: %s", elf_errmsg(-1));
27112c23cb7cSEd Maste 			continue;
27122c23cb7cSEd Maste 		}
27132c23cb7cSEd Maste 		if (d->d_size <= 0)
27142c23cb7cSEd Maste 			continue;
27152c23cb7cSEd Maste 
27162c23cb7cSEd Maste 		is_dynamic = 1;
27172c23cb7cSEd Maste 
27182c23cb7cSEd Maste 		/* Determine the actual number of table entries. */
27192c23cb7cSEd Maste 		nentries = 0;
272071edbbfdSEd Maste 		if (!get_ent_count(s, &jmax))
272171edbbfdSEd Maste 			continue;
27222c23cb7cSEd Maste 		for (j = 0; j < jmax; j++) {
27232c23cb7cSEd Maste 			if (gelf_getdyn(d, j, &dyn) != &dyn) {
27242c23cb7cSEd Maste 				warnx("gelf_getdyn failed: %s",
27252c23cb7cSEd Maste 				    elf_errmsg(-1));
27262c23cb7cSEd Maste 				continue;
27272c23cb7cSEd Maste 			}
27282c23cb7cSEd Maste 			nentries ++;
27292c23cb7cSEd Maste 			if (dyn.d_tag == DT_NULL)
27302c23cb7cSEd Maste 				break;
27312c23cb7cSEd Maste                 }
27322c23cb7cSEd Maste 
27332c23cb7cSEd Maste 		printf("\nDynamic section at offset 0x%jx", (uintmax_t)s->off);
27342c23cb7cSEd Maste 		printf(" contains %u entries:\n", nentries);
27352c23cb7cSEd Maste 
27362c23cb7cSEd Maste 		if (re->ec == ELFCLASS32)
27372c23cb7cSEd Maste 			printf("%5s%12s%28s\n", "Tag", "Type", "Name/Value");
27382c23cb7cSEd Maste 		else
27392c23cb7cSEd Maste 			printf("%5s%20s%28s\n", "Tag", "Type", "Name/Value");
27402c23cb7cSEd Maste 
27412c23cb7cSEd Maste 		for (j = 0; j < nentries; j++) {
27422c23cb7cSEd Maste 			if (gelf_getdyn(d, j, &dyn) != &dyn)
27432c23cb7cSEd Maste 				continue;
27442c23cb7cSEd Maste 			/* Dump dynamic entry type. */
27452c23cb7cSEd Maste 			if (re->ec == ELFCLASS32)
27462c23cb7cSEd Maste 				printf(" 0x%8.8jx", (uintmax_t)dyn.d_tag);
27472c23cb7cSEd Maste 			else
27482c23cb7cSEd Maste 				printf(" 0x%16.16jx", (uintmax_t)dyn.d_tag);
27492c23cb7cSEd Maste 			printf(" %-20s", dt_type(re->ehdr.e_machine,
27502c23cb7cSEd Maste 			    dyn.d_tag));
27512c23cb7cSEd Maste 			/* Dump dynamic entry value. */
27522c23cb7cSEd Maste 			dump_dyn_val(re, &dyn, s->link);
27532c23cb7cSEd Maste 		}
27542c23cb7cSEd Maste 	}
27552c23cb7cSEd Maste 
27562c23cb7cSEd Maste 	if (!is_dynamic)
27572c23cb7cSEd Maste 		printf("\nThere is no dynamic section in this file.\n");
27582c23cb7cSEd Maste }
27592c23cb7cSEd Maste 
27602c23cb7cSEd Maste static char *
27612c23cb7cSEd Maste timestamp(time_t ti)
27622c23cb7cSEd Maste {
27632c23cb7cSEd Maste 	static char ts[32];
27642c23cb7cSEd Maste 	struct tm *t;
27652c23cb7cSEd Maste 
27662c23cb7cSEd Maste 	t = gmtime(&ti);
27672c23cb7cSEd Maste 	snprintf(ts, sizeof(ts), "%04d-%02d-%02dT%02d:%02d:%02d",
27682c23cb7cSEd Maste 	    t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour,
27692c23cb7cSEd Maste 	    t->tm_min, t->tm_sec);
27702c23cb7cSEd Maste 
27712c23cb7cSEd Maste 	return (ts);
27722c23cb7cSEd Maste }
27732c23cb7cSEd Maste 
27742c23cb7cSEd Maste static const char *
27752c23cb7cSEd Maste dyn_str(struct readelf *re, uint32_t stab, uint64_t d_val)
27762c23cb7cSEd Maste {
27772c23cb7cSEd Maste 	const char *name;
27782c23cb7cSEd Maste 
27792c23cb7cSEd Maste 	if (stab == SHN_UNDEF)
27802c23cb7cSEd Maste 		name = "ERROR";
27812c23cb7cSEd Maste 	else if ((name = elf_strptr(re->elf, stab, d_val)) == NULL) {
27822c23cb7cSEd Maste 		(void) elf_errno(); /* clear error */
27832c23cb7cSEd Maste 		name = "ERROR";
27842c23cb7cSEd Maste 	}
27852c23cb7cSEd Maste 
27862c23cb7cSEd Maste 	return (name);
27872c23cb7cSEd Maste }
27882c23cb7cSEd Maste 
27892c23cb7cSEd Maste static void
279075826696SEd Maste dump_arch_dyn_val(struct readelf *re, GElf_Dyn *dyn)
27912c23cb7cSEd Maste {
27922c23cb7cSEd Maste 	switch (re->ehdr.e_machine) {
27932c23cb7cSEd Maste 	case EM_MIPS:
27942c23cb7cSEd Maste 	case EM_MIPS_RS3_LE:
27952c23cb7cSEd Maste 		switch (dyn->d_tag) {
27962c23cb7cSEd Maste 		case DT_MIPS_RLD_VERSION:
27972c23cb7cSEd Maste 		case DT_MIPS_LOCAL_GOTNO:
27982c23cb7cSEd Maste 		case DT_MIPS_CONFLICTNO:
27992c23cb7cSEd Maste 		case DT_MIPS_LIBLISTNO:
28002c23cb7cSEd Maste 		case DT_MIPS_SYMTABNO:
28012c23cb7cSEd Maste 		case DT_MIPS_UNREFEXTNO:
28022c23cb7cSEd Maste 		case DT_MIPS_GOTSYM:
28032c23cb7cSEd Maste 		case DT_MIPS_HIPAGENO:
28042c23cb7cSEd Maste 		case DT_MIPS_DELTA_CLASS_NO:
28052c23cb7cSEd Maste 		case DT_MIPS_DELTA_INSTANCE_NO:
28062c23cb7cSEd Maste 		case DT_MIPS_DELTA_RELOC_NO:
28072c23cb7cSEd Maste 		case DT_MIPS_DELTA_SYM_NO:
28082c23cb7cSEd Maste 		case DT_MIPS_DELTA_CLASSSYM_NO:
28092c23cb7cSEd Maste 		case DT_MIPS_LOCALPAGE_GOTIDX:
28102c23cb7cSEd Maste 		case DT_MIPS_LOCAL_GOTIDX:
28112c23cb7cSEd Maste 		case DT_MIPS_HIDDEN_GOTIDX:
28122c23cb7cSEd Maste 		case DT_MIPS_PROTECTED_GOTIDX:
28132c23cb7cSEd Maste 			printf(" %ju\n", (uintmax_t) dyn->d_un.d_val);
28142c23cb7cSEd Maste 			break;
28152c23cb7cSEd Maste 		case DT_MIPS_ICHECKSUM:
28162c23cb7cSEd Maste 		case DT_MIPS_FLAGS:
28172c23cb7cSEd Maste 		case DT_MIPS_BASE_ADDRESS:
28182c23cb7cSEd Maste 		case DT_MIPS_CONFLICT:
28192c23cb7cSEd Maste 		case DT_MIPS_LIBLIST:
28202c23cb7cSEd Maste 		case DT_MIPS_RLD_MAP:
28212c23cb7cSEd Maste 		case DT_MIPS_DELTA_CLASS:
28222c23cb7cSEd Maste 		case DT_MIPS_DELTA_INSTANCE:
28232c23cb7cSEd Maste 		case DT_MIPS_DELTA_RELOC:
28242c23cb7cSEd Maste 		case DT_MIPS_DELTA_SYM:
28252c23cb7cSEd Maste 		case DT_MIPS_DELTA_CLASSSYM:
28262c23cb7cSEd Maste 		case DT_MIPS_CXX_FLAGS:
28272c23cb7cSEd Maste 		case DT_MIPS_PIXIE_INIT:
28282c23cb7cSEd Maste 		case DT_MIPS_SYMBOL_LIB:
28292c23cb7cSEd Maste 		case DT_MIPS_OPTIONS:
28302c23cb7cSEd Maste 		case DT_MIPS_INTERFACE:
28312c23cb7cSEd Maste 		case DT_MIPS_DYNSTR_ALIGN:
28322c23cb7cSEd Maste 		case DT_MIPS_INTERFACE_SIZE:
28332c23cb7cSEd Maste 		case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
28342c23cb7cSEd Maste 		case DT_MIPS_COMPACT_SIZE:
28352c23cb7cSEd Maste 		case DT_MIPS_GP_VALUE:
28362c23cb7cSEd Maste 		case DT_MIPS_AUX_DYNAMIC:
28372c23cb7cSEd Maste 		case DT_MIPS_PLTGOT:
28382c23cb7cSEd Maste 		case DT_MIPS_RLD_OBJ_UPDATE:
28392c23cb7cSEd Maste 		case DT_MIPS_RWPLT:
28402c23cb7cSEd Maste 			printf(" 0x%jx\n", (uintmax_t) dyn->d_un.d_val);
28412c23cb7cSEd Maste 			break;
28422c23cb7cSEd Maste 		case DT_MIPS_IVERSION:
28432c23cb7cSEd Maste 		case DT_MIPS_PERF_SUFFIX:
28442c23cb7cSEd Maste 		case DT_MIPS_TIME_STAMP:
28452c23cb7cSEd Maste 			printf(" %s\n", timestamp(dyn->d_un.d_val));
28462c23cb7cSEd Maste 			break;
28479fb35c8dSJohn Baldwin 		default:
28489fb35c8dSJohn Baldwin 			printf("\n");
28499fb35c8dSJohn Baldwin 			break;
28502c23cb7cSEd Maste 		}
28512c23cb7cSEd Maste 		break;
28522c23cb7cSEd Maste 	default:
28532c23cb7cSEd Maste 		printf("\n");
28542c23cb7cSEd Maste 		break;
28552c23cb7cSEd Maste 	}
28562c23cb7cSEd Maste }
28572c23cb7cSEd Maste 
28582c23cb7cSEd Maste static void
28596b2779a0SEd Maste dump_flags(struct flag_desc *desc, uint64_t val)
28606b2779a0SEd Maste {
28616b2779a0SEd Maste 	struct flag_desc *fd;
28626b2779a0SEd Maste 
28636b2779a0SEd Maste 	for (fd = desc; fd->flag != 0; fd++) {
28646b2779a0SEd Maste 		if (val & fd->flag) {
28656b2779a0SEd Maste 			val &= ~fd->flag;
28666b2779a0SEd Maste 			printf(" %s", fd->desc);
28676b2779a0SEd Maste 		}
28686b2779a0SEd Maste 	}
28696b2779a0SEd Maste 	if (val != 0)
28706b2779a0SEd Maste 		printf(" unknown (0x%jx)", (uintmax_t)val);
287177f552e2SEd Maste 	printf("\n");
28726b2779a0SEd Maste }
28736b2779a0SEd Maste 
28746b2779a0SEd Maste static struct flag_desc dt_flags[] = {
28756b2779a0SEd Maste 	{ DF_ORIGIN,		"ORIGIN" },
28766b2779a0SEd Maste 	{ DF_SYMBOLIC,		"SYMBOLIC" },
28776b2779a0SEd Maste 	{ DF_TEXTREL,		"TEXTREL" },
28786b2779a0SEd Maste 	{ DF_BIND_NOW,		"BIND_NOW" },
28796b2779a0SEd Maste 	{ DF_STATIC_TLS,	"STATIC_TLS" },
28806b2779a0SEd Maste 	{ 0, NULL }
28816b2779a0SEd Maste };
28826b2779a0SEd Maste 
28836b2779a0SEd Maste static struct flag_desc dt_flags_1[] = {
28846b2779a0SEd Maste 	{ DF_1_BIND_NOW,	"NOW" },
28856b2779a0SEd Maste 	{ DF_1_GLOBAL,		"GLOBAL" },
28866b2779a0SEd Maste 	{ 0x4,			"GROUP" },
28876b2779a0SEd Maste 	{ DF_1_NODELETE,	"NODELETE" },
28886b2779a0SEd Maste 	{ DF_1_LOADFLTR,	"LOADFLTR" },
28896b2779a0SEd Maste 	{ 0x20,			"INITFIRST" },
28906b2779a0SEd Maste 	{ DF_1_NOOPEN,		"NOOPEN" },
28916b2779a0SEd Maste 	{ DF_1_ORIGIN,		"ORIGIN" },
28926b2779a0SEd Maste 	{ 0x100,		"DIRECT" },
28936b2779a0SEd Maste 	{ DF_1_INTERPOSE,	"INTERPOSE" },
28946b2779a0SEd Maste 	{ DF_1_NODEFLIB,	"NODEFLIB" },
28956b2779a0SEd Maste 	{ 0x1000,		"NODUMP" },
28966b2779a0SEd Maste 	{ 0x2000,		"CONFALT" },
28976b2779a0SEd Maste 	{ 0x4000,		"ENDFILTEE" },
28986b2779a0SEd Maste 	{ 0x8000,		"DISPRELDNE" },
28996b2779a0SEd Maste 	{ 0x10000,		"DISPRELPND" },
29006b2779a0SEd Maste 	{ 0x20000,		"NODIRECT" },
29016b2779a0SEd Maste 	{ 0x40000,		"IGNMULDEF" },
29026b2779a0SEd Maste 	{ 0x80000,		"NOKSYMS" },
29036b2779a0SEd Maste 	{ 0x100000,		"NOHDR" },
29046b2779a0SEd Maste 	{ 0x200000,		"EDITED" },
29056b2779a0SEd Maste 	{ 0x400000,		"NORELOC" },
29066b2779a0SEd Maste 	{ 0x800000,		"SYMINTPOSE" },
29076b2779a0SEd Maste 	{ 0x1000000,		"GLOBAUDIT" },
2908796bf313SEd Maste 	{ 0x02000000,		"SINGLETON" },
2909796bf313SEd Maste 	{ 0x04000000,		"STUB" },
2910796bf313SEd Maste 	{ DF_1_PIE,		"PIE" },
29116b2779a0SEd Maste 	{ 0, NULL }
29126b2779a0SEd Maste };
29136b2779a0SEd Maste 
29146b2779a0SEd Maste static void
29152c23cb7cSEd Maste dump_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab)
29162c23cb7cSEd Maste {
29172c23cb7cSEd Maste 	const char *name;
29182c23cb7cSEd Maste 
291975826696SEd Maste 	if (dyn->d_tag >= DT_LOPROC && dyn->d_tag <= DT_HIPROC &&
292075826696SEd Maste 	    dyn->d_tag != DT_AUXILIARY && dyn->d_tag != DT_FILTER) {
292175826696SEd Maste 		dump_arch_dyn_val(re, dyn);
29222c23cb7cSEd Maste 		return;
29232c23cb7cSEd Maste 	}
29242c23cb7cSEd Maste 
29252c23cb7cSEd Maste 	/* These entry values are index into the string table. */
29262c23cb7cSEd Maste 	name = NULL;
292775826696SEd Maste 	if (dyn->d_tag == DT_AUXILIARY || dyn->d_tag == DT_FILTER ||
292875826696SEd Maste 	    dyn->d_tag == DT_NEEDED || dyn->d_tag == DT_SONAME ||
29292c23cb7cSEd Maste 	    dyn->d_tag == DT_RPATH || dyn->d_tag == DT_RUNPATH)
29302c23cb7cSEd Maste 		name = dyn_str(re, stab, dyn->d_un.d_val);
29312c23cb7cSEd Maste 
29322c23cb7cSEd Maste 	switch(dyn->d_tag) {
29332c23cb7cSEd Maste 	case DT_NULL:
29342c23cb7cSEd Maste 	case DT_PLTGOT:
29352c23cb7cSEd Maste 	case DT_HASH:
29362c23cb7cSEd Maste 	case DT_STRTAB:
29372c23cb7cSEd Maste 	case DT_SYMTAB:
29382c23cb7cSEd Maste 	case DT_RELA:
29392c23cb7cSEd Maste 	case DT_INIT:
29402c23cb7cSEd Maste 	case DT_SYMBOLIC:
29412c23cb7cSEd Maste 	case DT_REL:
29422c23cb7cSEd Maste 	case DT_DEBUG:
29432c23cb7cSEd Maste 	case DT_TEXTREL:
29442c23cb7cSEd Maste 	case DT_JMPREL:
29452c23cb7cSEd Maste 	case DT_FINI:
29462c23cb7cSEd Maste 	case DT_VERDEF:
29472c23cb7cSEd Maste 	case DT_VERNEED:
29482c23cb7cSEd Maste 	case DT_VERSYM:
29492c23cb7cSEd Maste 	case DT_GNU_HASH:
29502c23cb7cSEd Maste 	case DT_GNU_LIBLIST:
29512c23cb7cSEd Maste 	case DT_GNU_CONFLICT:
29522c23cb7cSEd Maste 		printf(" 0x%jx\n", (uintmax_t) dyn->d_un.d_val);
29532c23cb7cSEd Maste 		break;
29542c23cb7cSEd Maste 	case DT_PLTRELSZ:
29552c23cb7cSEd Maste 	case DT_RELASZ:
29562c23cb7cSEd Maste 	case DT_RELAENT:
29572c23cb7cSEd Maste 	case DT_STRSZ:
29582c23cb7cSEd Maste 	case DT_SYMENT:
29592c23cb7cSEd Maste 	case DT_RELSZ:
29602c23cb7cSEd Maste 	case DT_RELENT:
29617003cfb3SEd Maste 	case DT_PREINIT_ARRAYSZ:
29622c23cb7cSEd Maste 	case DT_INIT_ARRAYSZ:
29632c23cb7cSEd Maste 	case DT_FINI_ARRAYSZ:
29642c23cb7cSEd Maste 	case DT_GNU_CONFLICTSZ:
29652c23cb7cSEd Maste 	case DT_GNU_LIBLISTSZ:
29662c23cb7cSEd Maste 		printf(" %ju (bytes)\n", (uintmax_t) dyn->d_un.d_val);
29672c23cb7cSEd Maste 		break;
29682c23cb7cSEd Maste  	case DT_RELACOUNT:
29692c23cb7cSEd Maste 	case DT_RELCOUNT:
29702c23cb7cSEd Maste 	case DT_VERDEFNUM:
29712c23cb7cSEd Maste 	case DT_VERNEEDNUM:
29722c23cb7cSEd Maste 		printf(" %ju\n", (uintmax_t) dyn->d_un.d_val);
29732c23cb7cSEd Maste 		break;
297475826696SEd Maste 	case DT_AUXILIARY:
297575826696SEd Maste 		printf(" Auxiliary library: [%s]\n", name);
297675826696SEd Maste 		break;
297775826696SEd Maste 	case DT_FILTER:
297875826696SEd Maste 		printf(" Filter library: [%s]\n", name);
297975826696SEd Maste 		break;
29802c23cb7cSEd Maste 	case DT_NEEDED:
29812c23cb7cSEd Maste 		printf(" Shared library: [%s]\n", name);
29822c23cb7cSEd Maste 		break;
29832c23cb7cSEd Maste 	case DT_SONAME:
29842c23cb7cSEd Maste 		printf(" Library soname: [%s]\n", name);
29852c23cb7cSEd Maste 		break;
29862c23cb7cSEd Maste 	case DT_RPATH:
29872c23cb7cSEd Maste 		printf(" Library rpath: [%s]\n", name);
29882c23cb7cSEd Maste 		break;
29892c23cb7cSEd Maste 	case DT_RUNPATH:
29902c23cb7cSEd Maste 		printf(" Library runpath: [%s]\n", name);
29912c23cb7cSEd Maste 		break;
29922c23cb7cSEd Maste 	case DT_PLTREL:
29932c23cb7cSEd Maste 		printf(" %s\n", dt_type(re->ehdr.e_machine, dyn->d_un.d_val));
29942c23cb7cSEd Maste 		break;
29952c23cb7cSEd Maste 	case DT_GNU_PRELINKED:
29962c23cb7cSEd Maste 		printf(" %s\n", timestamp(dyn->d_un.d_val));
29972c23cb7cSEd Maste 		break;
299887a8583bSEd Maste 	case DT_FLAGS:
29996b2779a0SEd Maste 		dump_flags(dt_flags, dyn->d_un.d_val);
300087a8583bSEd Maste 		break;
300187a8583bSEd Maste 	case DT_FLAGS_1:
30026b2779a0SEd Maste 		dump_flags(dt_flags_1, dyn->d_un.d_val);
300387a8583bSEd Maste 		break;
30042c23cb7cSEd Maste 	default:
30052c23cb7cSEd Maste 		printf("\n");
30062c23cb7cSEd Maste 	}
30072c23cb7cSEd Maste }
30082c23cb7cSEd Maste 
30092c23cb7cSEd Maste static void
30102c23cb7cSEd Maste dump_rel(struct readelf *re, struct section *s, Elf_Data *d)
30112c23cb7cSEd Maste {
30122c23cb7cSEd Maste 	GElf_Rel r;
30132c23cb7cSEd Maste 	const char *symname;
30142c23cb7cSEd Maste 	uint64_t symval;
30152c23cb7cSEd Maste 	int i, len;
3016b6d812d2SEd Maste 	uint32_t type;
3017b6d812d2SEd Maste 	uint8_t type2, type3;
30182c23cb7cSEd Maste 
3019656f49f8SEd Maste 	if (s->link >= re->shnum)
3020656f49f8SEd Maste 		return;
3021656f49f8SEd Maste 
30222c23cb7cSEd Maste #define	REL_HDR "r_offset", "r_info", "r_type", "st_value", "st_name"
30232c23cb7cSEd Maste #define	REL_CT32 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
3024b6b6f9ccSEd Maste 		elftc_reloc_type_str(re->ehdr.e_machine,	    \
3025b6b6f9ccSEd Maste 		ELF32_R_TYPE(r.r_info)), (uintmax_t)symval, symname
30262c23cb7cSEd Maste #define	REL_CT64 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
3027b6d812d2SEd Maste 		elftc_reloc_type_str(re->ehdr.e_machine, type),	    \
3028b6d812d2SEd Maste 		(uintmax_t)symval, symname
30292c23cb7cSEd Maste 
30302c23cb7cSEd Maste 	printf("\nRelocation section (%s):\n", s->name);
30312c23cb7cSEd Maste 	if (re->ec == ELFCLASS32)
30322c23cb7cSEd Maste 		printf("%-8s %-8s %-19s %-8s %s\n", REL_HDR);
30332c23cb7cSEd Maste 	else {
30342c23cb7cSEd Maste 		if (re->options & RE_WW)
30352c23cb7cSEd Maste 			printf("%-16s %-16s %-24s %-16s %s\n", REL_HDR);
30362c23cb7cSEd Maste 		else
30372c23cb7cSEd Maste 			printf("%-12s %-12s %-19s %-16s %s\n", REL_HDR);
30382c23cb7cSEd Maste 	}
303971edbbfdSEd Maste 	assert(d->d_size == s->sz);
304071edbbfdSEd Maste 	if (!get_ent_count(s, &len))
304171edbbfdSEd Maste 		return;
30422c23cb7cSEd Maste 	for (i = 0; i < len; i++) {
30432c23cb7cSEd Maste 		if (gelf_getrel(d, i, &r) != &r) {
30442c23cb7cSEd Maste 			warnx("gelf_getrel failed: %s", elf_errmsg(-1));
30452c23cb7cSEd Maste 			continue;
30462c23cb7cSEd Maste 		}
30472c23cb7cSEd Maste 		symname = get_symbol_name(re, s->link, GELF_R_SYM(r.r_info));
30482c23cb7cSEd Maste 		symval = get_symbol_value(re, s->link, GELF_R_SYM(r.r_info));
30492c23cb7cSEd Maste 		if (re->ec == ELFCLASS32) {
30502c23cb7cSEd Maste 			r.r_info = ELF32_R_INFO(ELF64_R_SYM(r.r_info),
30512c23cb7cSEd Maste 			    ELF64_R_TYPE(r.r_info));
30522c23cb7cSEd Maste 			printf("%8.8jx %8.8jx %-19.19s %8.8jx %s\n", REL_CT32);
30532c23cb7cSEd Maste 		} else {
3054b6d812d2SEd Maste 			type = ELF64_R_TYPE(r.r_info);
3055b6d812d2SEd Maste 			if (re->ehdr.e_machine == EM_MIPS) {
3056b6d812d2SEd Maste 				type2 = (type >> 8) & 0xFF;
3057b6d812d2SEd Maste 				type3 = (type >> 16) & 0xFF;
3058b6d812d2SEd Maste 				type = type & 0xFF;
3059d0bd2dadSEd Maste 			} else {
3060d0bd2dadSEd Maste 				type2 = type3 = 0;
3061b6d812d2SEd Maste 			}
30622c23cb7cSEd Maste 			if (re->options & RE_WW)
30632c23cb7cSEd Maste 				printf("%16.16jx %16.16jx %-24.24s"
30642c23cb7cSEd Maste 				    " %16.16jx %s\n", REL_CT64);
30652c23cb7cSEd Maste 			else
30662c23cb7cSEd Maste 				printf("%12.12jx %12.12jx %-19.19s"
30672c23cb7cSEd Maste 				    " %16.16jx %s\n", REL_CT64);
3068b6d812d2SEd Maste 			if (re->ehdr.e_machine == EM_MIPS) {
3069b6d812d2SEd Maste 				if (re->options & RE_WW) {
3070b6d812d2SEd Maste 					printf("%32s: %s\n", "Type2",
3071b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
3072b6d812d2SEd Maste 					    type2));
3073b6d812d2SEd Maste 					printf("%32s: %s\n", "Type3",
3074b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
3075b6d812d2SEd Maste 					    type3));
3076b6d812d2SEd Maste 				} else {
3077b6d812d2SEd Maste 					printf("%24s: %s\n", "Type2",
3078b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
3079b6d812d2SEd Maste 					    type2));
3080b6d812d2SEd Maste 					printf("%24s: %s\n", "Type3",
3081b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
3082b6d812d2SEd Maste 					    type3));
3083b6d812d2SEd Maste 				}
3084b6d812d2SEd Maste 			}
30852c23cb7cSEd Maste 		}
30862c23cb7cSEd Maste 	}
30872c23cb7cSEd Maste 
30882c23cb7cSEd Maste #undef	REL_HDR
30892c23cb7cSEd Maste #undef	REL_CT
30902c23cb7cSEd Maste }
30912c23cb7cSEd Maste 
30922c23cb7cSEd Maste static void
30932c23cb7cSEd Maste dump_rela(struct readelf *re, struct section *s, Elf_Data *d)
30942c23cb7cSEd Maste {
30952c23cb7cSEd Maste 	GElf_Rela r;
30962c23cb7cSEd Maste 	const char *symname;
30972c23cb7cSEd Maste 	uint64_t symval;
30982c23cb7cSEd Maste 	int i, len;
3099b6d812d2SEd Maste 	uint32_t type;
3100b6d812d2SEd Maste 	uint8_t type2, type3;
31012c23cb7cSEd Maste 
3102656f49f8SEd Maste 	if (s->link >= re->shnum)
3103656f49f8SEd Maste 		return;
3104656f49f8SEd Maste 
31052c23cb7cSEd Maste #define	RELA_HDR "r_offset", "r_info", "r_type", "st_value", \
31062c23cb7cSEd Maste 		"st_name + r_addend"
31072c23cb7cSEd Maste #define	RELA_CT32 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
3108b6b6f9ccSEd Maste 		elftc_reloc_type_str(re->ehdr.e_machine,	    \
3109b6b6f9ccSEd Maste 		ELF32_R_TYPE(r.r_info)), (uintmax_t)symval, symname
31102c23cb7cSEd Maste #define	RELA_CT64 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
3111b6d812d2SEd Maste 		elftc_reloc_type_str(re->ehdr.e_machine, type),	    \
3112b6d812d2SEd Maste 		(uintmax_t)symval, symname
31132c23cb7cSEd Maste 
31142c23cb7cSEd Maste 	printf("\nRelocation section with addend (%s):\n", s->name);
31152c23cb7cSEd Maste 	if (re->ec == ELFCLASS32)
31162c23cb7cSEd Maste 		printf("%-8s %-8s %-19s %-8s %s\n", RELA_HDR);
31172c23cb7cSEd Maste 	else {
31182c23cb7cSEd Maste 		if (re->options & RE_WW)
31192c23cb7cSEd Maste 			printf("%-16s %-16s %-24s %-16s %s\n", RELA_HDR);
31202c23cb7cSEd Maste 		else
31212c23cb7cSEd Maste 			printf("%-12s %-12s %-19s %-16s %s\n", RELA_HDR);
31222c23cb7cSEd Maste 	}
312371edbbfdSEd Maste 	assert(d->d_size == s->sz);
312471edbbfdSEd Maste 	if (!get_ent_count(s, &len))
312571edbbfdSEd Maste 		return;
31262c23cb7cSEd Maste 	for (i = 0; i < len; i++) {
31272c23cb7cSEd Maste 		if (gelf_getrela(d, i, &r) != &r) {
31282c23cb7cSEd Maste 			warnx("gelf_getrel failed: %s", elf_errmsg(-1));
31292c23cb7cSEd Maste 			continue;
31302c23cb7cSEd Maste 		}
31312c23cb7cSEd Maste 		symname = get_symbol_name(re, s->link, GELF_R_SYM(r.r_info));
31322c23cb7cSEd Maste 		symval = get_symbol_value(re, s->link, GELF_R_SYM(r.r_info));
31332c23cb7cSEd Maste 		if (re->ec == ELFCLASS32) {
31342c23cb7cSEd Maste 			r.r_info = ELF32_R_INFO(ELF64_R_SYM(r.r_info),
31352c23cb7cSEd Maste 			    ELF64_R_TYPE(r.r_info));
31362c23cb7cSEd Maste 			printf("%8.8jx %8.8jx %-19.19s %8.8jx %s", RELA_CT32);
31372c23cb7cSEd Maste 			printf(" + %x\n", (uint32_t) r.r_addend);
31382c23cb7cSEd Maste 		} else {
3139b6d812d2SEd Maste 			type = ELF64_R_TYPE(r.r_info);
3140b6d812d2SEd Maste 			if (re->ehdr.e_machine == EM_MIPS) {
3141b6d812d2SEd Maste 				type2 = (type >> 8) & 0xFF;
3142b6d812d2SEd Maste 				type3 = (type >> 16) & 0xFF;
3143b6d812d2SEd Maste 				type = type & 0xFF;
3144d0bd2dadSEd Maste 			} else {
3145d0bd2dadSEd Maste 				type2 = type3 = 0;
3146b6d812d2SEd Maste 			}
31472c23cb7cSEd Maste 			if (re->options & RE_WW)
31482c23cb7cSEd Maste 				printf("%16.16jx %16.16jx %-24.24s"
31492c23cb7cSEd Maste 				    " %16.16jx %s", RELA_CT64);
31502c23cb7cSEd Maste 			else
31512c23cb7cSEd Maste 				printf("%12.12jx %12.12jx %-19.19s"
31522c23cb7cSEd Maste 				    " %16.16jx %s", RELA_CT64);
31532c23cb7cSEd Maste 			printf(" + %jx\n", (uintmax_t) r.r_addend);
3154b6d812d2SEd Maste 			if (re->ehdr.e_machine == EM_MIPS) {
3155b6d812d2SEd Maste 				if (re->options & RE_WW) {
3156b6d812d2SEd Maste 					printf("%32s: %s\n", "Type2",
3157b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
3158b6d812d2SEd Maste 					    type2));
3159b6d812d2SEd Maste 					printf("%32s: %s\n", "Type3",
3160b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
3161b6d812d2SEd Maste 					    type3));
3162b6d812d2SEd Maste 				} else {
3163b6d812d2SEd Maste 					printf("%24s: %s\n", "Type2",
3164b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
3165b6d812d2SEd Maste 					    type2));
3166b6d812d2SEd Maste 					printf("%24s: %s\n", "Type3",
3167b6d812d2SEd Maste 					    elftc_reloc_type_str(EM_MIPS,
3168b6d812d2SEd Maste 					    type3));
3169b6d812d2SEd Maste 				}
3170b6d812d2SEd Maste 			}
31712c23cb7cSEd Maste 		}
31722c23cb7cSEd Maste 	}
31732c23cb7cSEd Maste 
31742c23cb7cSEd Maste #undef	RELA_HDR
31752c23cb7cSEd Maste #undef	RELA_CT
31762c23cb7cSEd Maste }
31772c23cb7cSEd Maste 
31782c23cb7cSEd Maste static void
31792c23cb7cSEd Maste dump_reloc(struct readelf *re)
31802c23cb7cSEd Maste {
31812c23cb7cSEd Maste 	struct section *s;
31822c23cb7cSEd Maste 	Elf_Data *d;
31832c23cb7cSEd Maste 	int i, elferr;
31842c23cb7cSEd Maste 
31852c23cb7cSEd Maste 	for (i = 0; (size_t)i < re->shnum; i++) {
31862c23cb7cSEd Maste 		s = &re->sl[i];
31872c23cb7cSEd Maste 		if (s->type == SHT_REL || s->type == SHT_RELA) {
31882c23cb7cSEd Maste 			(void) elf_errno();
31892c23cb7cSEd Maste 			if ((d = elf_getdata(s->scn, NULL)) == NULL) {
31902c23cb7cSEd Maste 				elferr = elf_errno();
31912c23cb7cSEd Maste 				if (elferr != 0)
31922c23cb7cSEd Maste 					warnx("elf_getdata failed: %s",
31932c23cb7cSEd Maste 					    elf_errmsg(elferr));
31942c23cb7cSEd Maste 				continue;
31952c23cb7cSEd Maste 			}
31962c23cb7cSEd Maste 			if (s->type == SHT_REL)
31972c23cb7cSEd Maste 				dump_rel(re, s, d);
31982c23cb7cSEd Maste 			else
31992c23cb7cSEd Maste 				dump_rela(re, s, d);
32002c23cb7cSEd Maste 		}
32012c23cb7cSEd Maste 	}
32022c23cb7cSEd Maste }
32032c23cb7cSEd Maste 
32042c23cb7cSEd Maste static void
32052c23cb7cSEd Maste dump_symtab(struct readelf *re, int i)
32062c23cb7cSEd Maste {
32072c23cb7cSEd Maste 	struct section *s;
32082c23cb7cSEd Maste 	Elf_Data *d;
32092c23cb7cSEd Maste 	GElf_Sym sym;
32102c23cb7cSEd Maste 	const char *name;
3211656f49f8SEd Maste 	uint32_t stab;
3212656f49f8SEd Maste 	int elferr, j, len;
3213656f49f8SEd Maste 	uint16_t vs;
32142c23cb7cSEd Maste 
32152c23cb7cSEd Maste 	s = &re->sl[i];
3216656f49f8SEd Maste 	if (s->link >= re->shnum)
3217656f49f8SEd Maste 		return;
32182c23cb7cSEd Maste 	stab = s->link;
32192c23cb7cSEd Maste 	(void) elf_errno();
32202c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
32212c23cb7cSEd Maste 		elferr = elf_errno();
32222c23cb7cSEd Maste 		if (elferr != 0)
32232c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
32242c23cb7cSEd Maste 		return;
32252c23cb7cSEd Maste 	}
32262c23cb7cSEd Maste 	if (d->d_size <= 0)
32272c23cb7cSEd Maste 		return;
322871edbbfdSEd Maste 	if (!get_ent_count(s, &len))
322971edbbfdSEd Maste 		return;
32302c23cb7cSEd Maste 	printf("Symbol table (%s)", s->name);
323171edbbfdSEd Maste 	printf(" contains %d entries:\n", len);
32322c23cb7cSEd Maste 	printf("%7s%9s%14s%5s%8s%6s%9s%5s\n", "Num:", "Value", "Size", "Type",
32332c23cb7cSEd Maste 	    "Bind", "Vis", "Ndx", "Name");
32342c23cb7cSEd Maste 
323571edbbfdSEd Maste 	for (j = 0; j < len; j++) {
32362c23cb7cSEd Maste 		if (gelf_getsym(d, j, &sym) != &sym) {
32372c23cb7cSEd Maste 			warnx("gelf_getsym failed: %s", elf_errmsg(-1));
32382c23cb7cSEd Maste 			continue;
32392c23cb7cSEd Maste 		}
32402c23cb7cSEd Maste 		printf("%6d:", j);
32412c23cb7cSEd Maste 		printf(" %16.16jx", (uintmax_t) sym.st_value);
3242839529caSEd Maste 		printf(" %5ju", (uintmax_t) sym.st_size);
3243839529caSEd Maste 		printf(" %-7s", st_type(re->ehdr.e_machine,
3244b6b6f9ccSEd Maste 		    re->ehdr.e_ident[EI_OSABI], GELF_ST_TYPE(sym.st_info)));
32452c23cb7cSEd Maste 		printf(" %-6s", st_bind(GELF_ST_BIND(sym.st_info)));
32462c23cb7cSEd Maste 		printf(" %-8s", st_vis(GELF_ST_VISIBILITY(sym.st_other)));
32472c23cb7cSEd Maste 		printf(" %3s", st_shndx(sym.st_shndx));
32482c23cb7cSEd Maste 		if ((name = elf_strptr(re->elf, stab, sym.st_name)) != NULL)
32492c23cb7cSEd Maste 			printf(" %s", name);
32502c23cb7cSEd Maste 		/* Append symbol version string for SHT_DYNSYM symbol table. */
32512c23cb7cSEd Maste 		if (s->type == SHT_DYNSYM && re->ver != NULL &&
32522c23cb7cSEd Maste 		    re->vs != NULL && re->vs[j] > 1) {
3253656f49f8SEd Maste 			vs = re->vs[j] & VERSYM_VERSION;
3254656f49f8SEd Maste 			if (vs >= re->ver_sz || re->ver[vs].name == NULL) {
3255656f49f8SEd Maste 				warnx("invalid versym version index %u", vs);
3256656f49f8SEd Maste 				break;
3257656f49f8SEd Maste 			}
3258656f49f8SEd Maste 			if (re->vs[j] & VERSYM_HIDDEN || re->ver[vs].type == 0)
3259656f49f8SEd Maste 				printf("@%s (%d)", re->ver[vs].name, vs);
32602c23cb7cSEd Maste 			else
3261656f49f8SEd Maste 				printf("@@%s (%d)", re->ver[vs].name, vs);
32622c23cb7cSEd Maste 		}
32632c23cb7cSEd Maste 		putchar('\n');
32642c23cb7cSEd Maste 	}
32652c23cb7cSEd Maste 
32662c23cb7cSEd Maste }
32672c23cb7cSEd Maste 
32682c23cb7cSEd Maste static void
32692c23cb7cSEd Maste dump_symtabs(struct readelf *re)
32702c23cb7cSEd Maste {
32712c23cb7cSEd Maste 	GElf_Dyn dyn;
32722c23cb7cSEd Maste 	Elf_Data *d;
32732c23cb7cSEd Maste 	struct section *s;
32742c23cb7cSEd Maste 	uint64_t dyn_off;
327571edbbfdSEd Maste 	int elferr, i, len;
32762c23cb7cSEd Maste 
32772c23cb7cSEd Maste 	/*
32782c23cb7cSEd Maste 	 * If -D is specified, only dump the symbol table specified by
32792c23cb7cSEd Maste 	 * the DT_SYMTAB entry in the .dynamic section.
32802c23cb7cSEd Maste 	 */
32812c23cb7cSEd Maste 	dyn_off = 0;
32822c23cb7cSEd Maste 	if (re->options & RE_DD) {
32832c23cb7cSEd Maste 		s = NULL;
32842c23cb7cSEd Maste 		for (i = 0; (size_t)i < re->shnum; i++)
32852c23cb7cSEd Maste 			if (re->sl[i].type == SHT_DYNAMIC) {
32862c23cb7cSEd Maste 				s = &re->sl[i];
32872c23cb7cSEd Maste 				break;
32882c23cb7cSEd Maste 			}
32892c23cb7cSEd Maste 		if (s == NULL)
32902c23cb7cSEd Maste 			return;
32912c23cb7cSEd Maste 		(void) elf_errno();
32922c23cb7cSEd Maste 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
32932c23cb7cSEd Maste 			elferr = elf_errno();
32942c23cb7cSEd Maste 			if (elferr != 0)
32952c23cb7cSEd Maste 				warnx("elf_getdata failed: %s", elf_errmsg(-1));
32962c23cb7cSEd Maste 			return;
32972c23cb7cSEd Maste 		}
32982c23cb7cSEd Maste 		if (d->d_size <= 0)
32992c23cb7cSEd Maste 			return;
330071edbbfdSEd Maste 		if (!get_ent_count(s, &len))
330171edbbfdSEd Maste 			return;
33022c23cb7cSEd Maste 
330371edbbfdSEd Maste 		for (i = 0; i < len; i++) {
33042c23cb7cSEd Maste 			if (gelf_getdyn(d, i, &dyn) != &dyn) {
33052c23cb7cSEd Maste 				warnx("gelf_getdyn failed: %s", elf_errmsg(-1));
33062c23cb7cSEd Maste 				continue;
33072c23cb7cSEd Maste 			}
33082c23cb7cSEd Maste 			if (dyn.d_tag == DT_SYMTAB) {
33092c23cb7cSEd Maste 				dyn_off = dyn.d_un.d_val;
33102c23cb7cSEd Maste 				break;
33112c23cb7cSEd Maste 			}
33122c23cb7cSEd Maste 		}
33132c23cb7cSEd Maste 	}
33142c23cb7cSEd Maste 
33152c23cb7cSEd Maste 	/* Find and dump symbol tables. */
33162c23cb7cSEd Maste 	for (i = 0; (size_t)i < re->shnum; i++) {
33172c23cb7cSEd Maste 		s = &re->sl[i];
33182c23cb7cSEd Maste 		if (s->type == SHT_SYMTAB || s->type == SHT_DYNSYM) {
33192c23cb7cSEd Maste 			if (re->options & RE_DD) {
33202c23cb7cSEd Maste 				if (dyn_off == s->addr) {
33212c23cb7cSEd Maste 					dump_symtab(re, i);
33222c23cb7cSEd Maste 					break;
33232c23cb7cSEd Maste 				}
33242c23cb7cSEd Maste 			} else
33252c23cb7cSEd Maste 				dump_symtab(re, i);
33262c23cb7cSEd Maste 		}
33272c23cb7cSEd Maste 	}
33282c23cb7cSEd Maste }
33292c23cb7cSEd Maste 
33302c23cb7cSEd Maste static void
33312c23cb7cSEd Maste dump_svr4_hash(struct section *s)
33322c23cb7cSEd Maste {
33332c23cb7cSEd Maste 	Elf_Data	*d;
33342c23cb7cSEd Maste 	uint32_t	*buf;
33352c23cb7cSEd Maste 	uint32_t	 nbucket, nchain;
33362c23cb7cSEd Maste 	uint32_t	*bucket, *chain;
33372c23cb7cSEd Maste 	uint32_t	*bl, *c, maxl, total;
33382c23cb7cSEd Maste 	int		 elferr, i, j;
33392c23cb7cSEd Maste 
33402c23cb7cSEd Maste 	/* Read and parse the content of .hash section. */
33412c23cb7cSEd Maste 	(void) elf_errno();
33422c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
33432c23cb7cSEd Maste 		elferr = elf_errno();
33442c23cb7cSEd Maste 		if (elferr != 0)
33452c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
33462c23cb7cSEd Maste 		return;
33472c23cb7cSEd Maste 	}
33482c23cb7cSEd Maste 	if (d->d_size < 2 * sizeof(uint32_t)) {
33492c23cb7cSEd Maste 		warnx(".hash section too small");
33502c23cb7cSEd Maste 		return;
33512c23cb7cSEd Maste 	}
33522c23cb7cSEd Maste 	buf = d->d_buf;
33532c23cb7cSEd Maste 	nbucket = buf[0];
33542c23cb7cSEd Maste 	nchain = buf[1];
33552c23cb7cSEd Maste 	if (nbucket <= 0 || nchain <= 0) {
33562c23cb7cSEd Maste 		warnx("Malformed .hash section");
33572c23cb7cSEd Maste 		return;
33582c23cb7cSEd Maste 	}
33592c23cb7cSEd Maste 	if (d->d_size != (nbucket + nchain + 2) * sizeof(uint32_t)) {
33602c23cb7cSEd Maste 		warnx("Malformed .hash section");
33612c23cb7cSEd Maste 		return;
33622c23cb7cSEd Maste 	}
33632c23cb7cSEd Maste 	bucket = &buf[2];
33642c23cb7cSEd Maste 	chain = &buf[2 + nbucket];
33652c23cb7cSEd Maste 
33662c23cb7cSEd Maste 	maxl = 0;
33672c23cb7cSEd Maste 	if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
33682c23cb7cSEd Maste 		errx(EXIT_FAILURE, "calloc failed");
33692c23cb7cSEd Maste 	for (i = 0; (uint32_t)i < nbucket; i++)
33702c23cb7cSEd Maste 		for (j = bucket[i]; j > 0 && (uint32_t)j < nchain; j = chain[j])
33712c23cb7cSEd Maste 			if (++bl[i] > maxl)
33722c23cb7cSEd Maste 				maxl = bl[i];
33732c23cb7cSEd Maste 	if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
33742c23cb7cSEd Maste 		errx(EXIT_FAILURE, "calloc failed");
33752c23cb7cSEd Maste 	for (i = 0; (uint32_t)i < nbucket; i++)
33762c23cb7cSEd Maste 		c[bl[i]]++;
33772c23cb7cSEd Maste 	printf("\nHistogram for bucket list length (total of %u buckets):\n",
33782c23cb7cSEd Maste 	    nbucket);
33792c23cb7cSEd Maste 	printf(" Length\tNumber\t\t%% of total\tCoverage\n");
33802c23cb7cSEd Maste 	total = 0;
33812c23cb7cSEd Maste 	for (i = 0; (uint32_t)i <= maxl; i++) {
33822c23cb7cSEd Maste 		total += c[i] * i;
33832c23cb7cSEd Maste 		printf("%7u\t%-10u\t(%5.1f%%)\t%5.1f%%\n", i, c[i],
33842c23cb7cSEd Maste 		    c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
33852c23cb7cSEd Maste 	}
33862c23cb7cSEd Maste 	free(c);
33872c23cb7cSEd Maste 	free(bl);
33882c23cb7cSEd Maste }
33892c23cb7cSEd Maste 
33902c23cb7cSEd Maste static void
33912c23cb7cSEd Maste dump_svr4_hash64(struct readelf *re, struct section *s)
33922c23cb7cSEd Maste {
33932c23cb7cSEd Maste 	Elf_Data	*d, dst;
33942c23cb7cSEd Maste 	uint64_t	*buf;
33952c23cb7cSEd Maste 	uint64_t	 nbucket, nchain;
33962c23cb7cSEd Maste 	uint64_t	*bucket, *chain;
33972c23cb7cSEd Maste 	uint64_t	*bl, *c, maxl, total;
33982c23cb7cSEd Maste 	int		 elferr, i, j;
33992c23cb7cSEd Maste 
34002c23cb7cSEd Maste 	/*
34012c23cb7cSEd Maste 	 * ALPHA uses 64-bit hash entries. Since libelf assumes that
34022c23cb7cSEd Maste 	 * .hash section contains only 32-bit entry, an explicit
34032c23cb7cSEd Maste 	 * gelf_xlatetom is needed here.
34042c23cb7cSEd Maste 	 */
34052c23cb7cSEd Maste 	(void) elf_errno();
34062c23cb7cSEd Maste 	if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
34072c23cb7cSEd Maste 		elferr = elf_errno();
34082c23cb7cSEd Maste 		if (elferr != 0)
34092c23cb7cSEd Maste 			warnx("elf_rawdata failed: %s",
34102c23cb7cSEd Maste 			    elf_errmsg(elferr));
34112c23cb7cSEd Maste 		return;
34122c23cb7cSEd Maste 	}
34132c23cb7cSEd Maste 	d->d_type = ELF_T_XWORD;
34142c23cb7cSEd Maste 	memcpy(&dst, d, sizeof(Elf_Data));
34152c23cb7cSEd Maste 	if (gelf_xlatetom(re->elf, &dst, d,
34162c23cb7cSEd Maste 		re->ehdr.e_ident[EI_DATA]) != &dst) {
34172c23cb7cSEd Maste 		warnx("gelf_xlatetom failed: %s", elf_errmsg(-1));
34182c23cb7cSEd Maste 		return;
34192c23cb7cSEd Maste 	}
34202c23cb7cSEd Maste 	if (dst.d_size < 2 * sizeof(uint64_t)) {
34212c23cb7cSEd Maste 		warnx(".hash section too small");
34222c23cb7cSEd Maste 		return;
34232c23cb7cSEd Maste 	}
34242c23cb7cSEd Maste 	buf = dst.d_buf;
34252c23cb7cSEd Maste 	nbucket = buf[0];
34262c23cb7cSEd Maste 	nchain = buf[1];
34272c23cb7cSEd Maste 	if (nbucket <= 0 || nchain <= 0) {
34282c23cb7cSEd Maste 		warnx("Malformed .hash section");
34292c23cb7cSEd Maste 		return;
34302c23cb7cSEd Maste 	}
34312c23cb7cSEd Maste 	if (d->d_size != (nbucket + nchain + 2) * sizeof(uint32_t)) {
34322c23cb7cSEd Maste 		warnx("Malformed .hash section");
34332c23cb7cSEd Maste 		return;
34342c23cb7cSEd Maste 	}
34352c23cb7cSEd Maste 	bucket = &buf[2];
34362c23cb7cSEd Maste 	chain = &buf[2 + nbucket];
34372c23cb7cSEd Maste 
34382c23cb7cSEd Maste 	maxl = 0;
34392c23cb7cSEd Maste 	if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
34402c23cb7cSEd Maste 		errx(EXIT_FAILURE, "calloc failed");
34412c23cb7cSEd Maste 	for (i = 0; (uint32_t)i < nbucket; i++)
34422c23cb7cSEd Maste 		for (j = bucket[i]; j > 0 && (uint32_t)j < nchain; j = chain[j])
34432c23cb7cSEd Maste 			if (++bl[i] > maxl)
34442c23cb7cSEd Maste 				maxl = bl[i];
34452c23cb7cSEd Maste 	if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
34462c23cb7cSEd Maste 		errx(EXIT_FAILURE, "calloc failed");
34472c23cb7cSEd Maste 	for (i = 0; (uint64_t)i < nbucket; i++)
34482c23cb7cSEd Maste 		c[bl[i]]++;
34492c23cb7cSEd Maste 	printf("Histogram for bucket list length (total of %ju buckets):\n",
34502c23cb7cSEd Maste 	    (uintmax_t)nbucket);
34512c23cb7cSEd Maste 	printf(" Length\tNumber\t\t%% of total\tCoverage\n");
34522c23cb7cSEd Maste 	total = 0;
34532c23cb7cSEd Maste 	for (i = 0; (uint64_t)i <= maxl; i++) {
34542c23cb7cSEd Maste 		total += c[i] * i;
34552c23cb7cSEd Maste 		printf("%7u\t%-10ju\t(%5.1f%%)\t%5.1f%%\n", i, (uintmax_t)c[i],
34562c23cb7cSEd Maste 		    c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
34572c23cb7cSEd Maste 	}
34582c23cb7cSEd Maste 	free(c);
34592c23cb7cSEd Maste 	free(bl);
34602c23cb7cSEd Maste }
34612c23cb7cSEd Maste 
34622c23cb7cSEd Maste static void
34632c23cb7cSEd Maste dump_gnu_hash(struct readelf *re, struct section *s)
34642c23cb7cSEd Maste {
34652c23cb7cSEd Maste 	struct section	*ds;
34662c23cb7cSEd Maste 	Elf_Data	*d;
34672c23cb7cSEd Maste 	uint32_t	*buf;
34682c23cb7cSEd Maste 	uint32_t	*bucket, *chain;
3469cf781b2eSEd Maste 	uint32_t	 nbucket, nchain, symndx, maskwords;
34702c23cb7cSEd Maste 	uint32_t	*bl, *c, maxl, total;
34712c23cb7cSEd Maste 	int		 elferr, dynsymcount, i, j;
34722c23cb7cSEd Maste 
34732c23cb7cSEd Maste 	(void) elf_errno();
34742c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
34752c23cb7cSEd Maste 		elferr = elf_errno();
34762c23cb7cSEd Maste 		if (elferr != 0)
34772c23cb7cSEd Maste 			warnx("elf_getdata failed: %s",
34782c23cb7cSEd Maste 			    elf_errmsg(elferr));
34792c23cb7cSEd Maste 		return;
34802c23cb7cSEd Maste 	}
34812c23cb7cSEd Maste 	if (d->d_size < 4 * sizeof(uint32_t)) {
34822c23cb7cSEd Maste 		warnx(".gnu.hash section too small");
34832c23cb7cSEd Maste 		return;
34842c23cb7cSEd Maste 	}
34852c23cb7cSEd Maste 	buf = d->d_buf;
34862c23cb7cSEd Maste 	nbucket = buf[0];
34872c23cb7cSEd Maste 	symndx = buf[1];
34882c23cb7cSEd Maste 	maskwords = buf[2];
34892c23cb7cSEd Maste 	buf += 4;
3490656f49f8SEd Maste 	if (s->link >= re->shnum)
3491656f49f8SEd Maste 		return;
34922c23cb7cSEd Maste 	ds = &re->sl[s->link];
349371edbbfdSEd Maste 	if (!get_ent_count(ds, &dynsymcount))
349471edbbfdSEd Maste 		return;
3495b6b6f9ccSEd Maste 	if (symndx >= (uint32_t)dynsymcount) {
3496b6b6f9ccSEd Maste 		warnx("Malformed .gnu.hash section (symndx out of range)");
3497b6b6f9ccSEd Maste 		return;
3498b6b6f9ccSEd Maste 	}
34992c23cb7cSEd Maste 	nchain = dynsymcount - symndx;
35002c23cb7cSEd Maste 	if (d->d_size != 4 * sizeof(uint32_t) + maskwords *
35012c23cb7cSEd Maste 	    (re->ec == ELFCLASS32 ? sizeof(uint32_t) : sizeof(uint64_t)) +
35022c23cb7cSEd Maste 	    (nbucket + nchain) * sizeof(uint32_t)) {
35032c23cb7cSEd Maste 		warnx("Malformed .gnu.hash section");
35042c23cb7cSEd Maste 		return;
35052c23cb7cSEd Maste 	}
35062c23cb7cSEd Maste 	bucket = buf + (re->ec == ELFCLASS32 ? maskwords : maskwords * 2);
35072c23cb7cSEd Maste 	chain = bucket + nbucket;
35082c23cb7cSEd Maste 
35092c23cb7cSEd Maste 	maxl = 0;
35102c23cb7cSEd Maste 	if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
35112c23cb7cSEd Maste 		errx(EXIT_FAILURE, "calloc failed");
35122c23cb7cSEd Maste 	for (i = 0; (uint32_t)i < nbucket; i++)
35132c23cb7cSEd Maste 		for (j = bucket[i]; j > 0 && (uint32_t)j - symndx < nchain;
35142c23cb7cSEd Maste 		     j++) {
35152c23cb7cSEd Maste 			if (++bl[i] > maxl)
35162c23cb7cSEd Maste 				maxl = bl[i];
35172c23cb7cSEd Maste 			if (chain[j - symndx] & 1)
35182c23cb7cSEd Maste 				break;
35192c23cb7cSEd Maste 		}
35202c23cb7cSEd Maste 	if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
35212c23cb7cSEd Maste 		errx(EXIT_FAILURE, "calloc failed");
35222c23cb7cSEd Maste 	for (i = 0; (uint32_t)i < nbucket; i++)
35232c23cb7cSEd Maste 		c[bl[i]]++;
35242c23cb7cSEd Maste 	printf("Histogram for bucket list length (total of %u buckets):\n",
35252c23cb7cSEd Maste 	    nbucket);
35262c23cb7cSEd Maste 	printf(" Length\tNumber\t\t%% of total\tCoverage\n");
35272c23cb7cSEd Maste 	total = 0;
35282c23cb7cSEd Maste 	for (i = 0; (uint32_t)i <= maxl; i++) {
35292c23cb7cSEd Maste 		total += c[i] * i;
35302c23cb7cSEd Maste 		printf("%7u\t%-10u\t(%5.1f%%)\t%5.1f%%\n", i, c[i],
35312c23cb7cSEd Maste 		    c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
35322c23cb7cSEd Maste 	}
35332c23cb7cSEd Maste 	free(c);
35342c23cb7cSEd Maste 	free(bl);
35352c23cb7cSEd Maste }
35362c23cb7cSEd Maste 
353714a345d9SEd Maste static struct flag_desc gnu_property_x86_feature_1_and_bits[] = {
353814a345d9SEd Maste 	{ GNU_PROPERTY_X86_FEATURE_1_IBT,	"IBT" },
353914a345d9SEd Maste 	{ GNU_PROPERTY_X86_FEATURE_1_SHSTK,	"SHSTK" },
354014a345d9SEd Maste 	{ 0, NULL }
354114a345d9SEd Maste };
354214a345d9SEd Maste 
354314a345d9SEd Maste static void
354414a345d9SEd Maste dump_gnu_property_type_0(struct readelf *re, const char *buf, size_t sz)
354514a345d9SEd Maste {
354614a345d9SEd Maste 	size_t i;
354714a345d9SEd Maste 	uint32_t type, prop_sz;
354814a345d9SEd Maste 
354914a345d9SEd Maste 	printf("      Properties: ");
355014a345d9SEd Maste 	while (sz > 0) {
355114a345d9SEd Maste 		if (sz < 8)
355214a345d9SEd Maste 			goto bad;
355314a345d9SEd Maste 
355414a345d9SEd Maste 		type = *(const uint32_t *)(const void *)buf;
355514a345d9SEd Maste 		prop_sz = *(const uint32_t *)(const void *)(buf + 4);
355614a345d9SEd Maste 		buf += 8;
355714a345d9SEd Maste 		sz -= 8;
355814a345d9SEd Maste 
355914a345d9SEd Maste 		if (prop_sz > sz)
356014a345d9SEd Maste 			goto bad;
356114a345d9SEd Maste 
356214a345d9SEd Maste 		if (type >= GNU_PROPERTY_LOPROC &&
356314a345d9SEd Maste 		    type <= GNU_PROPERTY_HIPROC) {
356414a345d9SEd Maste 			if (re->ehdr.e_machine != EM_X86_64) {
356514a345d9SEd Maste 				printf("machine type %x unknown\n",
356614a345d9SEd Maste 				    re->ehdr.e_machine);
356714a345d9SEd Maste 				goto unknown;
356814a345d9SEd Maste 			}
356914a345d9SEd Maste 			switch (type) {
357014a345d9SEd Maste 			case GNU_PROPERTY_X86_FEATURE_1_AND:
357114a345d9SEd Maste 				printf("x86 features:");
357214a345d9SEd Maste 				if (prop_sz != 4)
357314a345d9SEd Maste 					goto bad;
357414a345d9SEd Maste 				dump_flags(gnu_property_x86_feature_1_and_bits,
357514a345d9SEd Maste 				    *(const uint32_t *)(const void *)buf);
357614a345d9SEd Maste 				break;
357714a345d9SEd Maste 			}
357814a345d9SEd Maste 		}
357914a345d9SEd Maste 
358014a345d9SEd Maste 		buf += roundup2(prop_sz, 8);
358114a345d9SEd Maste 		sz -= roundup2(prop_sz, 8);
358214a345d9SEd Maste 	}
358314a345d9SEd Maste 	return;
358414a345d9SEd Maste bad:
358514a345d9SEd Maste 	printf("corrupt GNU property\n");
358614a345d9SEd Maste unknown:
358714a345d9SEd Maste 	printf("remaining description data:");
358814a345d9SEd Maste 	for (i = 0; i < sz; i++)
358914a345d9SEd Maste 		printf(" %02x", (unsigned char)buf[i]);
359014a345d9SEd Maste 	printf("\n");
359114a345d9SEd Maste }
359214a345d9SEd Maste 
35932c23cb7cSEd Maste static void
35942c23cb7cSEd Maste dump_hash(struct readelf *re)
35952c23cb7cSEd Maste {
35962c23cb7cSEd Maste 	struct section	*s;
35972c23cb7cSEd Maste 	int		 i;
35982c23cb7cSEd Maste 
35992c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
36002c23cb7cSEd Maste 		s = &re->sl[i];
36012c23cb7cSEd Maste 		if (s->type == SHT_HASH || s->type == SHT_GNU_HASH) {
36022c23cb7cSEd Maste 			if (s->type == SHT_GNU_HASH)
36032c23cb7cSEd Maste 				dump_gnu_hash(re, s);
36042c23cb7cSEd Maste 			else if (re->ehdr.e_machine == EM_ALPHA &&
36052c23cb7cSEd Maste 			    s->entsize == 8)
36062c23cb7cSEd Maste 				dump_svr4_hash64(re, s);
36072c23cb7cSEd Maste 			else
36082c23cb7cSEd Maste 				dump_svr4_hash(s);
36092c23cb7cSEd Maste 		}
36102c23cb7cSEd Maste 	}
36112c23cb7cSEd Maste }
36122c23cb7cSEd Maste 
36132c23cb7cSEd Maste static void
36142c23cb7cSEd Maste dump_notes(struct readelf *re)
36152c23cb7cSEd Maste {
36162c23cb7cSEd Maste 	struct section *s;
36172c23cb7cSEd Maste 	const char *rawfile;
36182c23cb7cSEd Maste 	GElf_Phdr phdr;
36192c23cb7cSEd Maste 	Elf_Data *d;
3620b6b6f9ccSEd Maste 	size_t filesize, phnum;
36212c23cb7cSEd Maste 	int i, elferr;
36222c23cb7cSEd Maste 
36232c23cb7cSEd Maste 	if (re->ehdr.e_type == ET_CORE) {
36242c23cb7cSEd Maste 		/*
36252c23cb7cSEd Maste 		 * Search program headers in the core file for
36262c23cb7cSEd Maste 		 * PT_NOTE entry.
36272c23cb7cSEd Maste 		 */
36282c23cb7cSEd Maste 		if (elf_getphnum(re->elf, &phnum) == 0) {
36292c23cb7cSEd Maste 			warnx("elf_getphnum failed: %s", elf_errmsg(-1));
36302c23cb7cSEd Maste 			return;
36312c23cb7cSEd Maste 		}
36322c23cb7cSEd Maste 		if (phnum == 0)
36332c23cb7cSEd Maste 			return;
3634b6b6f9ccSEd Maste 		if ((rawfile = elf_rawfile(re->elf, &filesize)) == NULL) {
36352c23cb7cSEd Maste 			warnx("elf_rawfile failed: %s", elf_errmsg(-1));
36362c23cb7cSEd Maste 			return;
36372c23cb7cSEd Maste 		}
36382c23cb7cSEd Maste 		for (i = 0; (size_t) i < phnum; i++) {
36392c23cb7cSEd Maste 			if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
36402c23cb7cSEd Maste 				warnx("gelf_getphdr failed: %s",
36412c23cb7cSEd Maste 				    elf_errmsg(-1));
36422c23cb7cSEd Maste 				continue;
36432c23cb7cSEd Maste 			}
3644b6b6f9ccSEd Maste 			if (phdr.p_type == PT_NOTE) {
3645b6b6f9ccSEd Maste 				if (phdr.p_offset >= filesize ||
3646b6b6f9ccSEd Maste 				    phdr.p_filesz > filesize - phdr.p_offset) {
3647b6b6f9ccSEd Maste 					warnx("invalid PHDR offset");
3648b6b6f9ccSEd Maste 					continue;
3649b6b6f9ccSEd Maste 				}
36502c23cb7cSEd Maste 				dump_notes_content(re, rawfile + phdr.p_offset,
36512c23cb7cSEd Maste 				    phdr.p_filesz, phdr.p_offset);
36522c23cb7cSEd Maste 			}
3653b6b6f9ccSEd Maste 		}
36542c23cb7cSEd Maste 
36552c23cb7cSEd Maste 	} else {
36562c23cb7cSEd Maste 		/*
36572c23cb7cSEd Maste 		 * For objects other than core files, Search for
36582c23cb7cSEd Maste 		 * SHT_NOTE sections.
36592c23cb7cSEd Maste 		 */
36602c23cb7cSEd Maste 		for (i = 0; (size_t) i < re->shnum; i++) {
36612c23cb7cSEd Maste 			s = &re->sl[i];
36622c23cb7cSEd Maste 			if (s->type == SHT_NOTE) {
36632c23cb7cSEd Maste 				(void) elf_errno();
36642c23cb7cSEd Maste 				if ((d = elf_getdata(s->scn, NULL)) == NULL) {
36652c23cb7cSEd Maste 					elferr = elf_errno();
36662c23cb7cSEd Maste 					if (elferr != 0)
36672c23cb7cSEd Maste 						warnx("elf_getdata failed: %s",
36682c23cb7cSEd Maste 						    elf_errmsg(elferr));
36692c23cb7cSEd Maste 					continue;
36702c23cb7cSEd Maste 				}
36712c23cb7cSEd Maste 				dump_notes_content(re, d->d_buf, d->d_size,
36722c23cb7cSEd Maste 				    s->off);
36732c23cb7cSEd Maste 			}
36742c23cb7cSEd Maste 		}
36752c23cb7cSEd Maste 	}
36762c23cb7cSEd Maste }
36772c23cb7cSEd Maste 
36783f0e38d7SEd Maste static struct flag_desc note_feature_ctl_flags[] = {
36793f0e38d7SEd Maste 	{ NT_FREEBSD_FCTL_ASLR_DISABLE,		"ASLR_DISABLE" },
36808e7e7da5SEd Maste 	{ NT_FREEBSD_FCTL_PROTMAX_DISABLE,	"PROTMAX_DISABLE" },
36818e7e7da5SEd Maste 	{ NT_FREEBSD_FCTL_STKGAP_DISABLE,	"STKGAP_DISABLE" },
3682d06e23f9SEd Maste 	{ NT_FREEBSD_FCTL_WXNEEDED,		"WXNEEDED" },
36833f0e38d7SEd Maste 	{ 0, NULL }
36843f0e38d7SEd Maste };
36853f0e38d7SEd Maste 
3686e982f6f9SEd Maste static bool
3687ff0f134bSEd Maste dump_note_string(const char *description, const char *s, size_t len)
3688ff0f134bSEd Maste {
3689ff0f134bSEd Maste 	size_t i;
3690ff0f134bSEd Maste 
3691ff0f134bSEd Maste 	if (len == 0 || s[--len] != '\0') {
3692e982f6f9SEd Maste 		return (false);
3693ff0f134bSEd Maste 	} else {
3694e982f6f9SEd Maste 		for (i = 0; i < len; i++)
3695e982f6f9SEd Maste 			if (!isprint(s[i]))
3696e982f6f9SEd Maste 				return (false);
3697ff0f134bSEd Maste 	}
3698ff0f134bSEd Maste 
3699ff0f134bSEd Maste 	printf("   %s: %s\n", description, s);
3700e982f6f9SEd Maste 	return (true);
3701ff0f134bSEd Maste }
3702e982f6f9SEd Maste 
3703e982f6f9SEd Maste struct note_desc {
3704e982f6f9SEd Maste 	uint32_t type;
3705e982f6f9SEd Maste 	const char *description;
3706e982f6f9SEd Maste 	bool (*fp)(const char *, const char *, size_t);
3707e982f6f9SEd Maste };
3708e982f6f9SEd Maste 
3709e982f6f9SEd Maste static struct note_desc xen_notes[] = {
3710e982f6f9SEd Maste 	{ 5, "Xen version", dump_note_string },
3711e982f6f9SEd Maste 	{ 6, "Guest OS", dump_note_string },
3712e982f6f9SEd Maste 	{ 7, "Guest version", dump_note_string },
3713e982f6f9SEd Maste 	{ 8, "Loader", dump_note_string },
3714e982f6f9SEd Maste 	{ 9, "PAE mode", dump_note_string },
3715e982f6f9SEd Maste 	{ 10, "Features", dump_note_string },
3716e982f6f9SEd Maste 	{ 11, "BSD symtab", dump_note_string },
3717e982f6f9SEd Maste 	{ 0, NULL, NULL }
3718e982f6f9SEd Maste };
3719ff0f134bSEd Maste 
3720ff0f134bSEd Maste static void
372114a345d9SEd Maste dump_notes_data(struct readelf *re, const char *name, uint32_t type,
372214a345d9SEd Maste     const char *buf, size_t sz)
37233f0e38d7SEd Maste {
3724e982f6f9SEd Maste 	struct note_desc *nd;
37253f0e38d7SEd Maste 	size_t i;
37263f0e38d7SEd Maste 	const uint32_t *ubuf;
37273f0e38d7SEd Maste 
37283f0e38d7SEd Maste 	/* Note data is at least 4-byte aligned. */
37293f0e38d7SEd Maste 	if (((uintptr_t)buf & 3) != 0) {
37303f0e38d7SEd Maste 		warnx("bad note data alignment");
37313f0e38d7SEd Maste 		goto unknown;
37323f0e38d7SEd Maste 	}
37333f0e38d7SEd Maste 	ubuf = (const uint32_t *)(const void *)buf;
37343f0e38d7SEd Maste 
37353f0e38d7SEd Maste 	if (strcmp(name, "FreeBSD") == 0) {
37363f0e38d7SEd Maste 		switch (type) {
37373f0e38d7SEd Maste 		case NT_FREEBSD_ABI_TAG:
37383f0e38d7SEd Maste 			if (sz != 4)
37393f0e38d7SEd Maste 				goto unknown;
37403f0e38d7SEd Maste 			printf("   ABI tag: %u\n", ubuf[0]);
37413f0e38d7SEd Maste 			return;
37423f0e38d7SEd Maste 		/* NT_FREEBSD_NOINIT_TAG carries no data, treat as unknown. */
37433f0e38d7SEd Maste 		case NT_FREEBSD_ARCH_TAG:
37443f0e38d7SEd Maste 			if (sz != 4)
37453f0e38d7SEd Maste 				goto unknown;
37463f0e38d7SEd Maste 			printf("   Arch tag: %x\n", ubuf[0]);
37473f0e38d7SEd Maste 			return;
37483f0e38d7SEd Maste 		case NT_FREEBSD_FEATURE_CTL:
37493f0e38d7SEd Maste 			if (sz != 4)
37503f0e38d7SEd Maste 				goto unknown;
37513f0e38d7SEd Maste 			printf("   Features:");
37523f0e38d7SEd Maste 			dump_flags(note_feature_ctl_flags, ubuf[0]);
37533f0e38d7SEd Maste 			return;
37543f0e38d7SEd Maste 		}
375514a345d9SEd Maste 	} else if (strcmp(name, "GNU") == 0) {
375614a345d9SEd Maste 		switch (type) {
375714a345d9SEd Maste 		case NT_GNU_PROPERTY_TYPE_0:
375814a345d9SEd Maste 			dump_gnu_property_type_0(re, buf, sz);
375914a345d9SEd Maste 			return;
37606c37d603SEd Maste 		case NT_GNU_BUILD_ID:
37616c37d603SEd Maste 			printf("   Build ID: ");
37626c37d603SEd Maste 			for (i = 0; i < sz; i++)
37636c37d603SEd Maste 				printf("%02x", (unsigned char)buf[i]);
37646c37d603SEd Maste 			printf("\n");
37656c37d603SEd Maste 			return;
376614a345d9SEd Maste 		}
3767ff0f134bSEd Maste 	} else if (strcmp(name, "Xen") == 0) {
3768e982f6f9SEd Maste 		for (nd = xen_notes; nd->description != NULL; nd++) {
3769e982f6f9SEd Maste 			if (nd->type == type) {
3770e982f6f9SEd Maste 				if (nd->fp(nd->description, buf, sz))
3771ff0f134bSEd Maste 					return;
3772e982f6f9SEd Maste 				else
3773e982f6f9SEd Maste 					break;
3774e982f6f9SEd Maste 			}
3775ff0f134bSEd Maste 		}
37763f0e38d7SEd Maste 	}
37773f0e38d7SEd Maste unknown:
37783f0e38d7SEd Maste 	printf("   description data:");
37793f0e38d7SEd Maste 	for (i = 0; i < sz; i++)
37803f0e38d7SEd Maste 		printf(" %02x", (unsigned char)buf[i]);
37813f0e38d7SEd Maste 	printf("\n");
37823f0e38d7SEd Maste }
37833f0e38d7SEd Maste 
37842c23cb7cSEd Maste static void
37852c23cb7cSEd Maste dump_notes_content(struct readelf *re, const char *buf, size_t sz, off_t off)
37862c23cb7cSEd Maste {
37872c23cb7cSEd Maste 	Elf_Note *note;
378802b08c90SEd Maste 	const char *end, *name;
378989839cadSEd Maste 	uint32_t namesz, descsz;
37902c23cb7cSEd Maste 
37912c23cb7cSEd Maste 	printf("\nNotes at offset %#010jx with length %#010jx:\n",
37922c23cb7cSEd Maste 	    (uintmax_t) off, (uintmax_t) sz);
37932c23cb7cSEd Maste 	printf("  %-13s %-15s %s\n", "Owner", "Data size", "Description");
37942c23cb7cSEd Maste 	end = buf + sz;
37952c23cb7cSEd Maste 	while (buf < end) {
379602b08c90SEd Maste 		if (buf + sizeof(*note) > end) {
379702b08c90SEd Maste 			warnx("invalid note header");
379802b08c90SEd Maste 			return;
379902b08c90SEd Maste 		}
38002c23cb7cSEd Maste 		note = (Elf_Note *)(uintptr_t) buf;
380189839cadSEd Maste 		namesz = roundup2(note->n_namesz, 4);
380289839cadSEd Maste 		descsz = roundup2(note->n_descsz, 4);
380389839cadSEd Maste 		if (namesz < note->n_namesz || descsz < note->n_descsz ||
380489839cadSEd Maste 		    buf + namesz + descsz > end) {
380589839cadSEd Maste 			warnx("invalid note header");
3806721ac29cSEd Maste 			return;
3807721ac29cSEd Maste 		}
380889839cadSEd Maste 		buf += sizeof(Elf_Note);
3809675f752cSEd Maste 		name = buf;
381089839cadSEd Maste 		buf += namesz;
381102b08c90SEd Maste 		/*
381202b08c90SEd Maste 		 * The name field is required to be nul-terminated, and
381302b08c90SEd Maste 		 * n_namesz includes the terminating nul in observed
381402b08c90SEd Maste 		 * implementations (contrary to the ELF-64 spec). A special
381502b08c90SEd Maste 		 * case is needed for cores generated by some older Linux
381602b08c90SEd Maste 		 * versions, which write a note named "CORE" without a nul
381702b08c90SEd Maste 		 * terminator and n_namesz = 4.
381802b08c90SEd Maste 		 */
381902b08c90SEd Maste 		if (note->n_namesz == 0)
382002b08c90SEd Maste 			name = "";
382102b08c90SEd Maste 		else if (note->n_namesz == 4 && strncmp(name, "CORE", 4) == 0)
382202b08c90SEd Maste 			name = "CORE";
382302b08c90SEd Maste 		else if (strnlen(name, note->n_namesz) >= note->n_namesz)
382402b08c90SEd Maste 			name = "<invalid>";
382502b08c90SEd Maste 		printf("  %-13s %#010jx", name, (uintmax_t) note->n_descsz);
382602b08c90SEd Maste 		printf("      %s\n", note_type(name, re->ehdr.e_type,
382702b08c90SEd Maste 		    note->n_type));
382814a345d9SEd Maste 		dump_notes_data(re, name, note->n_type, buf, note->n_descsz);
382989839cadSEd Maste 		buf += descsz;
38302c23cb7cSEd Maste 	}
38312c23cb7cSEd Maste }
38322c23cb7cSEd Maste 
38332c23cb7cSEd Maste /*
38342c23cb7cSEd Maste  * Symbol versioning sections are the same for 32bit and 64bit
38352c23cb7cSEd Maste  * ELF objects.
38362c23cb7cSEd Maste  */
38372c23cb7cSEd Maste #define Elf_Verdef	Elf32_Verdef
38382c23cb7cSEd Maste #define	Elf_Verdaux	Elf32_Verdaux
38392c23cb7cSEd Maste #define	Elf_Verneed	Elf32_Verneed
38402c23cb7cSEd Maste #define	Elf_Vernaux	Elf32_Vernaux
38412c23cb7cSEd Maste 
38422c23cb7cSEd Maste #define	SAVE_VERSION_NAME(x, n, t)					\
38432c23cb7cSEd Maste 	do {								\
38442c23cb7cSEd Maste 		while (x >= re->ver_sz) {				\
38452c23cb7cSEd Maste 			nv = realloc(re->ver,				\
38462c23cb7cSEd Maste 			    sizeof(*re->ver) * re->ver_sz * 2);		\
38472c23cb7cSEd Maste 			if (nv == NULL) {				\
38482c23cb7cSEd Maste 				warn("realloc failed");			\
38492c23cb7cSEd Maste 				free(re->ver);				\
38502c23cb7cSEd Maste 				return;					\
38512c23cb7cSEd Maste 			}						\
38522c23cb7cSEd Maste 			re->ver = nv;					\
38532c23cb7cSEd Maste 			for (i = re->ver_sz; i < re->ver_sz * 2; i++) {	\
38542c23cb7cSEd Maste 				re->ver[i].name = NULL;			\
38552c23cb7cSEd Maste 				re->ver[i].type = 0;			\
38562c23cb7cSEd Maste 			}						\
38572c23cb7cSEd Maste 			re->ver_sz *= 2;				\
38582c23cb7cSEd Maste 		}							\
38592c23cb7cSEd Maste 		if (x > 1) {						\
38602c23cb7cSEd Maste 			re->ver[x].name = n;				\
38612c23cb7cSEd Maste 			re->ver[x].type = t;				\
38622c23cb7cSEd Maste 		}							\
38632c23cb7cSEd Maste 	} while (0)
38642c23cb7cSEd Maste 
38652c23cb7cSEd Maste 
38662c23cb7cSEd Maste static void
38672c23cb7cSEd Maste dump_verdef(struct readelf *re, int dump)
38682c23cb7cSEd Maste {
38692c23cb7cSEd Maste 	struct section *s;
38702c23cb7cSEd Maste 	struct symver *nv;
38712c23cb7cSEd Maste 	Elf_Data *d;
38722c23cb7cSEd Maste 	Elf_Verdef *vd;
38732c23cb7cSEd Maste 	Elf_Verdaux *vda;
38742c23cb7cSEd Maste 	uint8_t *buf, *end, *buf2;
38752c23cb7cSEd Maste 	const char *name;
38762c23cb7cSEd Maste 	int elferr, i, j;
38772c23cb7cSEd Maste 
38782c23cb7cSEd Maste 	if ((s = re->vd_s) == NULL)
38792c23cb7cSEd Maste 		return;
3880656f49f8SEd Maste 	if (s->link >= re->shnum)
3881656f49f8SEd Maste 		return;
38822c23cb7cSEd Maste 
38832c23cb7cSEd Maste 	if (re->ver == NULL) {
38842c23cb7cSEd Maste 		re->ver_sz = 16;
38852c23cb7cSEd Maste 		if ((re->ver = calloc(re->ver_sz, sizeof(*re->ver))) ==
38862c23cb7cSEd Maste 		    NULL) {
38872c23cb7cSEd Maste 			warn("calloc failed");
38882c23cb7cSEd Maste 			return;
38892c23cb7cSEd Maste 		}
38902c23cb7cSEd Maste 		re->ver[0].name = "*local*";
38912c23cb7cSEd Maste 		re->ver[1].name = "*global*";
38922c23cb7cSEd Maste 	}
38932c23cb7cSEd Maste 
38942c23cb7cSEd Maste 	if (dump)
38952c23cb7cSEd Maste 		printf("\nVersion definition section (%s):\n", s->name);
38962c23cb7cSEd Maste 	(void) elf_errno();
38972c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
38982c23cb7cSEd Maste 		elferr = elf_errno();
38992c23cb7cSEd Maste 		if (elferr != 0)
39002c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
39012c23cb7cSEd Maste 		return;
39022c23cb7cSEd Maste 	}
39032c23cb7cSEd Maste 	if (d->d_size == 0)
39042c23cb7cSEd Maste 		return;
39052c23cb7cSEd Maste 
39062c23cb7cSEd Maste 	buf = d->d_buf;
39072c23cb7cSEd Maste 	end = buf + d->d_size;
39082c23cb7cSEd Maste 	while (buf + sizeof(Elf_Verdef) <= end) {
39092c23cb7cSEd Maste 		vd = (Elf_Verdef *) (uintptr_t) buf;
39102c23cb7cSEd Maste 		if (dump) {
39112c23cb7cSEd Maste 			printf("  0x%4.4lx", (unsigned long)
39122c23cb7cSEd Maste 			    (buf - (uint8_t *)d->d_buf));
39132c23cb7cSEd Maste 			printf(" vd_version: %u vd_flags: %d"
39142c23cb7cSEd Maste 			    " vd_ndx: %u vd_cnt: %u", vd->vd_version,
39152c23cb7cSEd Maste 			    vd->vd_flags, vd->vd_ndx, vd->vd_cnt);
39162c23cb7cSEd Maste 		}
39172c23cb7cSEd Maste 		buf2 = buf + vd->vd_aux;
39182c23cb7cSEd Maste 		j = 0;
39192c23cb7cSEd Maste 		while (buf2 + sizeof(Elf_Verdaux) <= end && j < vd->vd_cnt) {
39202c23cb7cSEd Maste 			vda = (Elf_Verdaux *) (uintptr_t) buf2;
39212c23cb7cSEd Maste 			name = get_string(re, s->link, vda->vda_name);
39222c23cb7cSEd Maste 			if (j == 0) {
39232c23cb7cSEd Maste 				if (dump)
39242c23cb7cSEd Maste 					printf(" vda_name: %s\n", name);
39252c23cb7cSEd Maste 				SAVE_VERSION_NAME((int)vd->vd_ndx, name, 1);
39262c23cb7cSEd Maste 			} else if (dump)
39272c23cb7cSEd Maste 				printf("  0x%4.4lx parent: %s\n",
39282c23cb7cSEd Maste 				    (unsigned long) (buf2 -
39292c23cb7cSEd Maste 				    (uint8_t *)d->d_buf), name);
39302c23cb7cSEd Maste 			if (vda->vda_next == 0)
39312c23cb7cSEd Maste 				break;
39322c23cb7cSEd Maste 			buf2 += vda->vda_next;
39332c23cb7cSEd Maste 			j++;
39342c23cb7cSEd Maste 		}
39352c23cb7cSEd Maste 		if (vd->vd_next == 0)
39362c23cb7cSEd Maste 			break;
39372c23cb7cSEd Maste 		buf += vd->vd_next;
39382c23cb7cSEd Maste 	}
39392c23cb7cSEd Maste }
39402c23cb7cSEd Maste 
39412c23cb7cSEd Maste static void
39422c23cb7cSEd Maste dump_verneed(struct readelf *re, int dump)
39432c23cb7cSEd Maste {
39442c23cb7cSEd Maste 	struct section *s;
39452c23cb7cSEd Maste 	struct symver *nv;
39462c23cb7cSEd Maste 	Elf_Data *d;
39472c23cb7cSEd Maste 	Elf_Verneed *vn;
39482c23cb7cSEd Maste 	Elf_Vernaux *vna;
39492c23cb7cSEd Maste 	uint8_t *buf, *end, *buf2;
39502c23cb7cSEd Maste 	const char *name;
39512c23cb7cSEd Maste 	int elferr, i, j;
39522c23cb7cSEd Maste 
39532c23cb7cSEd Maste 	if ((s = re->vn_s) == NULL)
39542c23cb7cSEd Maste 		return;
3955656f49f8SEd Maste 	if (s->link >= re->shnum)
3956656f49f8SEd Maste 		return;
39572c23cb7cSEd Maste 
39582c23cb7cSEd Maste 	if (re->ver == NULL) {
39592c23cb7cSEd Maste 		re->ver_sz = 16;
39602c23cb7cSEd Maste 		if ((re->ver = calloc(re->ver_sz, sizeof(*re->ver))) ==
39612c23cb7cSEd Maste 		    NULL) {
39622c23cb7cSEd Maste 			warn("calloc failed");
39632c23cb7cSEd Maste 			return;
39642c23cb7cSEd Maste 		}
39652c23cb7cSEd Maste 		re->ver[0].name = "*local*";
39662c23cb7cSEd Maste 		re->ver[1].name = "*global*";
39672c23cb7cSEd Maste 	}
39682c23cb7cSEd Maste 
39692c23cb7cSEd Maste 	if (dump)
39702c23cb7cSEd Maste 		printf("\nVersion needed section (%s):\n", s->name);
39712c23cb7cSEd Maste 	(void) elf_errno();
39722c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
39732c23cb7cSEd Maste 		elferr = elf_errno();
39742c23cb7cSEd Maste 		if (elferr != 0)
39752c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
39762c23cb7cSEd Maste 		return;
39772c23cb7cSEd Maste 	}
39782c23cb7cSEd Maste 	if (d->d_size == 0)
39792c23cb7cSEd Maste 		return;
39802c23cb7cSEd Maste 
39812c23cb7cSEd Maste 	buf = d->d_buf;
39822c23cb7cSEd Maste 	end = buf + d->d_size;
39832c23cb7cSEd Maste 	while (buf + sizeof(Elf_Verneed) <= end) {
39842c23cb7cSEd Maste 		vn = (Elf_Verneed *) (uintptr_t) buf;
39852c23cb7cSEd Maste 		if (dump) {
39862c23cb7cSEd Maste 			printf("  0x%4.4lx", (unsigned long)
39872c23cb7cSEd Maste 			    (buf - (uint8_t *)d->d_buf));
39882c23cb7cSEd Maste 			printf(" vn_version: %u vn_file: %s vn_cnt: %u\n",
39892c23cb7cSEd Maste 			    vn->vn_version,
39902c23cb7cSEd Maste 			    get_string(re, s->link, vn->vn_file),
39912c23cb7cSEd Maste 			    vn->vn_cnt);
39922c23cb7cSEd Maste 		}
39932c23cb7cSEd Maste 		buf2 = buf + vn->vn_aux;
39942c23cb7cSEd Maste 		j = 0;
39952c23cb7cSEd Maste 		while (buf2 + sizeof(Elf_Vernaux) <= end && j < vn->vn_cnt) {
39962c23cb7cSEd Maste 			vna = (Elf32_Vernaux *) (uintptr_t) buf2;
39972c23cb7cSEd Maste 			if (dump)
39982c23cb7cSEd Maste 				printf("  0x%4.4lx", (unsigned long)
39992c23cb7cSEd Maste 				    (buf2 - (uint8_t *)d->d_buf));
40002c23cb7cSEd Maste 			name = get_string(re, s->link, vna->vna_name);
40012c23cb7cSEd Maste 			if (dump)
40022c23cb7cSEd Maste 				printf("   vna_name: %s vna_flags: %u"
40032c23cb7cSEd Maste 				    " vna_other: %u\n", name,
40042c23cb7cSEd Maste 				    vna->vna_flags, vna->vna_other);
40052c23cb7cSEd Maste 			SAVE_VERSION_NAME((int)vna->vna_other, name, 0);
40062c23cb7cSEd Maste 			if (vna->vna_next == 0)
40072c23cb7cSEd Maste 				break;
40082c23cb7cSEd Maste 			buf2 += vna->vna_next;
40092c23cb7cSEd Maste 			j++;
40102c23cb7cSEd Maste 		}
40112c23cb7cSEd Maste 		if (vn->vn_next == 0)
40122c23cb7cSEd Maste 			break;
40132c23cb7cSEd Maste 		buf += vn->vn_next;
40142c23cb7cSEd Maste 	}
40152c23cb7cSEd Maste }
40162c23cb7cSEd Maste 
40172c23cb7cSEd Maste static void
40182c23cb7cSEd Maste dump_versym(struct readelf *re)
40192c23cb7cSEd Maste {
40202c23cb7cSEd Maste 	int i;
4021656f49f8SEd Maste 	uint16_t vs;
40222c23cb7cSEd Maste 
40232c23cb7cSEd Maste 	if (re->vs_s == NULL || re->ver == NULL || re->vs == NULL)
40242c23cb7cSEd Maste 		return;
40252c23cb7cSEd Maste 	printf("\nVersion symbol section (%s):\n", re->vs_s->name);
40262c23cb7cSEd Maste 	for (i = 0; i < re->vs_sz; i++) {
40272c23cb7cSEd Maste 		if ((i & 3) == 0) {
40282c23cb7cSEd Maste 			if (i > 0)
40292c23cb7cSEd Maste 				putchar('\n');
40302c23cb7cSEd Maste 			printf("  %03x:", i);
40312c23cb7cSEd Maste 		}
4032656f49f8SEd Maste 		vs = re->vs[i] & VERSYM_VERSION;
4033656f49f8SEd Maste 		if (vs >= re->ver_sz || re->ver[vs].name == NULL) {
4034656f49f8SEd Maste 			warnx("invalid versym version index %u", re->vs[i]);
4035656f49f8SEd Maste 			break;
4036656f49f8SEd Maste 		}
4037656f49f8SEd Maste 		if (re->vs[i] & VERSYM_HIDDEN)
4038656f49f8SEd Maste 			printf(" %3xh %-12s ", vs,
4039656f49f8SEd Maste 			    re->ver[re->vs[i] & VERSYM_VERSION].name);
40402c23cb7cSEd Maste 		else
4041656f49f8SEd Maste 			printf(" %3x %-12s ", vs, re->ver[re->vs[i]].name);
40422c23cb7cSEd Maste 	}
40432c23cb7cSEd Maste 	putchar('\n');
40442c23cb7cSEd Maste }
40452c23cb7cSEd Maste 
40462c23cb7cSEd Maste static void
40472c23cb7cSEd Maste dump_ver(struct readelf *re)
40482c23cb7cSEd Maste {
40492c23cb7cSEd Maste 
40502c23cb7cSEd Maste 	if (re->vs_s && re->ver && re->vs)
40512c23cb7cSEd Maste 		dump_versym(re);
40522c23cb7cSEd Maste 	if (re->vd_s)
40532c23cb7cSEd Maste 		dump_verdef(re, 1);
40542c23cb7cSEd Maste 	if (re->vn_s)
40552c23cb7cSEd Maste 		dump_verneed(re, 1);
40562c23cb7cSEd Maste }
40572c23cb7cSEd Maste 
40582c23cb7cSEd Maste static void
40592c23cb7cSEd Maste search_ver(struct readelf *re)
40602c23cb7cSEd Maste {
40612c23cb7cSEd Maste 	struct section *s;
40622c23cb7cSEd Maste 	Elf_Data *d;
40632c23cb7cSEd Maste 	int elferr, i;
40642c23cb7cSEd Maste 
40652c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
40662c23cb7cSEd Maste 		s = &re->sl[i];
40672c23cb7cSEd Maste 		if (s->type == SHT_SUNW_versym)
40682c23cb7cSEd Maste 			re->vs_s = s;
40692c23cb7cSEd Maste 		if (s->type == SHT_SUNW_verneed)
40702c23cb7cSEd Maste 			re->vn_s = s;
40712c23cb7cSEd Maste 		if (s->type == SHT_SUNW_verdef)
40722c23cb7cSEd Maste 			re->vd_s = s;
40732c23cb7cSEd Maste 	}
40742c23cb7cSEd Maste 	if (re->vd_s)
40752c23cb7cSEd Maste 		dump_verdef(re, 0);
40762c23cb7cSEd Maste 	if (re->vn_s)
40772c23cb7cSEd Maste 		dump_verneed(re, 0);
40782c23cb7cSEd Maste 	if (re->vs_s && re->ver != NULL) {
40792c23cb7cSEd Maste 		(void) elf_errno();
40802c23cb7cSEd Maste 		if ((d = elf_getdata(re->vs_s->scn, NULL)) == NULL) {
40812c23cb7cSEd Maste 			elferr = elf_errno();
40822c23cb7cSEd Maste 			if (elferr != 0)
40832c23cb7cSEd Maste 				warnx("elf_getdata failed: %s",
40842c23cb7cSEd Maste 				    elf_errmsg(elferr));
40852c23cb7cSEd Maste 			return;
40862c23cb7cSEd Maste 		}
40872c23cb7cSEd Maste 		if (d->d_size == 0)
40882c23cb7cSEd Maste 			return;
40892c23cb7cSEd Maste 		re->vs = d->d_buf;
40902c23cb7cSEd Maste 		re->vs_sz = d->d_size / sizeof(Elf32_Half);
40912c23cb7cSEd Maste 	}
40922c23cb7cSEd Maste }
40932c23cb7cSEd Maste 
40942c23cb7cSEd Maste #undef	Elf_Verdef
40952c23cb7cSEd Maste #undef	Elf_Verdaux
40962c23cb7cSEd Maste #undef	Elf_Verneed
40972c23cb7cSEd Maste #undef	Elf_Vernaux
40982c23cb7cSEd Maste #undef	SAVE_VERSION_NAME
40992c23cb7cSEd Maste 
41002c23cb7cSEd Maste /*
41012c23cb7cSEd Maste  * Elf32_Lib and Elf64_Lib are identical.
41022c23cb7cSEd Maste  */
41032c23cb7cSEd Maste #define	Elf_Lib		Elf32_Lib
41042c23cb7cSEd Maste 
41052c23cb7cSEd Maste static void
41062c23cb7cSEd Maste dump_liblist(struct readelf *re)
41072c23cb7cSEd Maste {
41082c23cb7cSEd Maste 	struct section *s;
41092c23cb7cSEd Maste 	struct tm *t;
41102c23cb7cSEd Maste 	time_t ti;
41112c23cb7cSEd Maste 	char tbuf[20];
41122c23cb7cSEd Maste 	Elf_Data *d;
41132c23cb7cSEd Maste 	Elf_Lib *lib;
411471edbbfdSEd Maste 	int i, j, k, elferr, first, len;
41152c23cb7cSEd Maste 
41162c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
41172c23cb7cSEd Maste 		s = &re->sl[i];
41182c23cb7cSEd Maste 		if (s->type != SHT_GNU_LIBLIST)
41192c23cb7cSEd Maste 			continue;
4120656f49f8SEd Maste 		if (s->link >= re->shnum)
4121656f49f8SEd Maste 			continue;
41222c23cb7cSEd Maste 		(void) elf_errno();
41232c23cb7cSEd Maste 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
41242c23cb7cSEd Maste 			elferr = elf_errno();
41252c23cb7cSEd Maste 			if (elferr != 0)
41262c23cb7cSEd Maste 				warnx("elf_getdata failed: %s",
41272c23cb7cSEd Maste 				    elf_errmsg(elferr));
41282c23cb7cSEd Maste 			continue;
41292c23cb7cSEd Maste 		}
41302c23cb7cSEd Maste 		if (d->d_size <= 0)
41312c23cb7cSEd Maste 			continue;
41322c23cb7cSEd Maste 		lib = d->d_buf;
413371edbbfdSEd Maste 		if (!get_ent_count(s, &len))
413471edbbfdSEd Maste 			continue;
41352c23cb7cSEd Maste 		printf("\nLibrary list section '%s' ", s->name);
413671edbbfdSEd Maste 		printf("contains %d entries:\n", len);
41372c23cb7cSEd Maste 		printf("%12s%24s%18s%10s%6s\n", "Library", "Time Stamp",
41382c23cb7cSEd Maste 		    "Checksum", "Version", "Flags");
41392c23cb7cSEd Maste 		for (j = 0; (uint64_t) j < s->sz / s->entsize; j++) {
41402c23cb7cSEd Maste 			printf("%3d: ", j);
41412c23cb7cSEd Maste 			printf("%-20.20s ",
41422c23cb7cSEd Maste 			    get_string(re, s->link, lib->l_name));
41432c23cb7cSEd Maste 			ti = lib->l_time_stamp;
41442c23cb7cSEd Maste 			t = gmtime(&ti);
41452c23cb7cSEd Maste 			snprintf(tbuf, sizeof(tbuf), "%04d-%02d-%02dT%02d:%02d"
41462c23cb7cSEd Maste 			    ":%2d", t->tm_year + 1900, t->tm_mon + 1,
41472c23cb7cSEd Maste 			    t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
41482c23cb7cSEd Maste 			printf("%-19.19s ", tbuf);
41492c23cb7cSEd Maste 			printf("0x%08x ", lib->l_checksum);
41502c23cb7cSEd Maste 			printf("%-7d %#x", lib->l_version, lib->l_flags);
41512c23cb7cSEd Maste 			if (lib->l_flags != 0) {
41522c23cb7cSEd Maste 				first = 1;
41532c23cb7cSEd Maste 				putchar('(');
41542c23cb7cSEd Maste 				for (k = 0; l_flag[k].name != NULL; k++) {
41552c23cb7cSEd Maste 					if ((l_flag[k].value & lib->l_flags) ==
41562c23cb7cSEd Maste 					    0)
41572c23cb7cSEd Maste 						continue;
41582c23cb7cSEd Maste 					if (!first)
41592c23cb7cSEd Maste 						putchar(',');
41602c23cb7cSEd Maste 					else
41612c23cb7cSEd Maste 						first = 0;
41622c23cb7cSEd Maste 					printf("%s", l_flag[k].name);
41632c23cb7cSEd Maste 				}
41642c23cb7cSEd Maste 				putchar(')');
41652c23cb7cSEd Maste 			}
41662c23cb7cSEd Maste 			putchar('\n');
41672c23cb7cSEd Maste 			lib++;
41682c23cb7cSEd Maste 		}
41692c23cb7cSEd Maste 	}
41702c23cb7cSEd Maste }
41712c23cb7cSEd Maste 
41722c23cb7cSEd Maste #undef Elf_Lib
41732c23cb7cSEd Maste 
41743ef90571SEd Maste static void
41753ef90571SEd Maste dump_section_groups(struct readelf *re)
41763ef90571SEd Maste {
41773ef90571SEd Maste 	struct section *s;
41783ef90571SEd Maste 	const char *symname;
41793ef90571SEd Maste 	Elf_Data *d;
41803ef90571SEd Maste 	uint32_t *w;
41813ef90571SEd Maste 	int i, j, elferr;
41823ef90571SEd Maste 	size_t n;
41833ef90571SEd Maste 
41843ef90571SEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
41853ef90571SEd Maste 		s = &re->sl[i];
41863ef90571SEd Maste 		if (s->type != SHT_GROUP)
41873ef90571SEd Maste 			continue;
4188656f49f8SEd Maste 		if (s->link >= re->shnum)
4189656f49f8SEd Maste 			continue;
41903ef90571SEd Maste 		(void) elf_errno();
41913ef90571SEd Maste 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
41923ef90571SEd Maste 			elferr = elf_errno();
41933ef90571SEd Maste 			if (elferr != 0)
41943ef90571SEd Maste 				warnx("elf_getdata failed: %s",
41953ef90571SEd Maste 				    elf_errmsg(elferr));
41963ef90571SEd Maste 			continue;
41973ef90571SEd Maste 		}
41983ef90571SEd Maste 		if (d->d_size <= 0)
41993ef90571SEd Maste 			continue;
42003ef90571SEd Maste 
42013ef90571SEd Maste 		w = d->d_buf;
42023ef90571SEd Maste 
42033ef90571SEd Maste 		/* We only support COMDAT section. */
42043ef90571SEd Maste #ifndef GRP_COMDAT
42053ef90571SEd Maste #define	GRP_COMDAT 0x1
42063ef90571SEd Maste #endif
42073ef90571SEd Maste 		if ((*w++ & GRP_COMDAT) == 0)
42083ef90571SEd Maste 			return;
42093ef90571SEd Maste 
42103ef90571SEd Maste 		if (s->entsize == 0)
42113ef90571SEd Maste 			s->entsize = 4;
42123ef90571SEd Maste 
42133ef90571SEd Maste 		symname = get_symbol_name(re, s->link, s->info);
42143ef90571SEd Maste 		n = s->sz / s->entsize;
42153ef90571SEd Maste 		if (n-- < 1)
42163ef90571SEd Maste 			return;
42173ef90571SEd Maste 
42183ef90571SEd Maste 		printf("\nCOMDAT group section [%5d] `%s' [%s] contains %ju"
42193ef90571SEd Maste 		    " sections:\n", i, s->name, symname, (uintmax_t)n);
42203ef90571SEd Maste 		printf("   %-10.10s %s\n", "[Index]", "Name");
42213ef90571SEd Maste 		for (j = 0; (size_t) j < n; j++, w++) {
42223ef90571SEd Maste 			if (*w >= re->shnum) {
42233ef90571SEd Maste 				warnx("invalid section index: %u", *w);
42243ef90571SEd Maste 				continue;
42253ef90571SEd Maste 			}
42263ef90571SEd Maste 			printf("   [%5u]   %s\n", *w, re->sl[*w].name);
42273ef90571SEd Maste 		}
42283ef90571SEd Maste 	}
42293ef90571SEd Maste }
42303ef90571SEd Maste 
42312c23cb7cSEd Maste static uint8_t *
423295fd7f26SEd Maste dump_unknown_tag(uint64_t tag, uint8_t *p, uint8_t *pe)
42332c23cb7cSEd Maste {
42342c23cb7cSEd Maste 	uint64_t val;
42352c23cb7cSEd Maste 
42362c23cb7cSEd Maste 	/*
42372c23cb7cSEd Maste 	 * According to ARM EABI: For tags > 32, even numbered tags have
42382c23cb7cSEd Maste 	 * a ULEB128 param and odd numbered ones have NUL-terminated
42392c23cb7cSEd Maste 	 * string param. This rule probably also applies for tags <= 32
42402c23cb7cSEd Maste 	 * if the object arch is not ARM.
42412c23cb7cSEd Maste 	 */
42422c23cb7cSEd Maste 
42432c23cb7cSEd Maste 	printf("  Tag_unknown_%ju: ", (uintmax_t) tag);
42442c23cb7cSEd Maste 
42452c23cb7cSEd Maste 	if (tag & 1) {
42462c23cb7cSEd Maste 		printf("%s\n", (char *) p);
42472c23cb7cSEd Maste 		p += strlen((char *) p) + 1;
42482c23cb7cSEd Maste 	} else {
424995fd7f26SEd Maste 		val = _decode_uleb128(&p, pe);
42502c23cb7cSEd Maste 		printf("%ju\n", (uintmax_t) val);
42512c23cb7cSEd Maste 	}
42522c23cb7cSEd Maste 
42532c23cb7cSEd Maste 	return (p);
42542c23cb7cSEd Maste }
42552c23cb7cSEd Maste 
42562c23cb7cSEd Maste static uint8_t *
425795fd7f26SEd Maste dump_compatibility_tag(uint8_t *p, uint8_t *pe)
42582c23cb7cSEd Maste {
42592c23cb7cSEd Maste 	uint64_t val;
42602c23cb7cSEd Maste 
426195fd7f26SEd Maste 	val = _decode_uleb128(&p, pe);
4262839529caSEd Maste 	printf("flag = %ju, vendor = %s\n", (uintmax_t) val, p);
42632c23cb7cSEd Maste 	p += strlen((char *) p) + 1;
42642c23cb7cSEd Maste 
42652c23cb7cSEd Maste 	return (p);
42662c23cb7cSEd Maste }
42672c23cb7cSEd Maste 
42682c23cb7cSEd Maste static void
42692c23cb7cSEd Maste dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe)
42702c23cb7cSEd Maste {
42712c23cb7cSEd Maste 	uint64_t tag, val;
42722c23cb7cSEd Maste 	size_t i;
42732c23cb7cSEd Maste 	int found, desc;
42742c23cb7cSEd Maste 
42752c23cb7cSEd Maste 	(void) re;
42762c23cb7cSEd Maste 
42772c23cb7cSEd Maste 	while (p < pe) {
427895fd7f26SEd Maste 		tag = _decode_uleb128(&p, pe);
42792c23cb7cSEd Maste 		found = desc = 0;
42802c23cb7cSEd Maste 		for (i = 0; i < sizeof(aeabi_tags) / sizeof(aeabi_tags[0]);
42812c23cb7cSEd Maste 		     i++) {
42822c23cb7cSEd Maste 			if (tag == aeabi_tags[i].tag) {
42832c23cb7cSEd Maste 				found = 1;
42842c23cb7cSEd Maste 				printf("  %s: ", aeabi_tags[i].s_tag);
42852c23cb7cSEd Maste 				if (aeabi_tags[i].get_desc) {
42862c23cb7cSEd Maste 					desc = 1;
428795fd7f26SEd Maste 					val = _decode_uleb128(&p, pe);
42882c23cb7cSEd Maste 					printf("%s\n",
42892c23cb7cSEd Maste 					    aeabi_tags[i].get_desc(val));
42902c23cb7cSEd Maste 				}
42912c23cb7cSEd Maste 				break;
42922c23cb7cSEd Maste 			}
42932c23cb7cSEd Maste 			if (tag < aeabi_tags[i].tag)
42942c23cb7cSEd Maste 				break;
42952c23cb7cSEd Maste 		}
42962c23cb7cSEd Maste 		if (!found) {
429795fd7f26SEd Maste 			p = dump_unknown_tag(tag, p, pe);
42982c23cb7cSEd Maste 			continue;
42992c23cb7cSEd Maste 		}
43002c23cb7cSEd Maste 		if (desc)
43012c23cb7cSEd Maste 			continue;
43022c23cb7cSEd Maste 
43032c23cb7cSEd Maste 		switch (tag) {
43042c23cb7cSEd Maste 		case 4:		/* Tag_CPU_raw_name */
43052c23cb7cSEd Maste 		case 5:		/* Tag_CPU_name */
43062c23cb7cSEd Maste 		case 67:	/* Tag_conformance */
43072c23cb7cSEd Maste 			printf("%s\n", (char *) p);
43082c23cb7cSEd Maste 			p += strlen((char *) p) + 1;
43092c23cb7cSEd Maste 			break;
43102c23cb7cSEd Maste 		case 32:	/* Tag_compatibility */
431195fd7f26SEd Maste 			p = dump_compatibility_tag(p, pe);
43122c23cb7cSEd Maste 			break;
43132c23cb7cSEd Maste 		case 64:	/* Tag_nodefaults */
43142c23cb7cSEd Maste 			/* ignored, written as 0. */
431595fd7f26SEd Maste 			(void) _decode_uleb128(&p, pe);
43162c23cb7cSEd Maste 			printf("True\n");
43172c23cb7cSEd Maste 			break;
43182c23cb7cSEd Maste 		case 65:	/* Tag_also_compatible_with */
431995fd7f26SEd Maste 			val = _decode_uleb128(&p, pe);
43202c23cb7cSEd Maste 			/* Must be Tag_CPU_arch */
43212c23cb7cSEd Maste 			if (val != 6) {
43222c23cb7cSEd Maste 				printf("unknown\n");
43232c23cb7cSEd Maste 				break;
43242c23cb7cSEd Maste 			}
432595fd7f26SEd Maste 			val = _decode_uleb128(&p, pe);
43262c23cb7cSEd Maste 			printf("%s\n", aeabi_cpu_arch(val));
43272c23cb7cSEd Maste 			/* Skip NUL terminator. */
43282c23cb7cSEd Maste 			p++;
43292c23cb7cSEd Maste 			break;
43302c23cb7cSEd Maste 		default:
43312c23cb7cSEd Maste 			putchar('\n');
43322c23cb7cSEd Maste 			break;
43332c23cb7cSEd Maste 		}
43342c23cb7cSEd Maste 	}
43352c23cb7cSEd Maste }
43362c23cb7cSEd Maste 
43372c23cb7cSEd Maste #ifndef	Tag_GNU_MIPS_ABI_FP
43382c23cb7cSEd Maste #define	Tag_GNU_MIPS_ABI_FP	4
43392c23cb7cSEd Maste #endif
43402c23cb7cSEd Maste 
43412c23cb7cSEd Maste static void
43422c23cb7cSEd Maste dump_mips_attributes(struct readelf *re, uint8_t *p, uint8_t *pe)
43432c23cb7cSEd Maste {
43442c23cb7cSEd Maste 	uint64_t tag, val;
43452c23cb7cSEd Maste 
43462c23cb7cSEd Maste 	(void) re;
43472c23cb7cSEd Maste 
43482c23cb7cSEd Maste 	while (p < pe) {
434995fd7f26SEd Maste 		tag = _decode_uleb128(&p, pe);
43502c23cb7cSEd Maste 		switch (tag) {
43512c23cb7cSEd Maste 		case Tag_GNU_MIPS_ABI_FP:
435295fd7f26SEd Maste 			val = _decode_uleb128(&p, pe);
43532c23cb7cSEd Maste 			printf("  Tag_GNU_MIPS_ABI_FP: %s\n", mips_abi_fp(val));
43542c23cb7cSEd Maste 			break;
43552c23cb7cSEd Maste 		case 32:	/* Tag_compatibility */
435695fd7f26SEd Maste 			p = dump_compatibility_tag(p, pe);
43572c23cb7cSEd Maste 			break;
43582c23cb7cSEd Maste 		default:
435995fd7f26SEd Maste 			p = dump_unknown_tag(tag, p, pe);
43602c23cb7cSEd Maste 			break;
43612c23cb7cSEd Maste 		}
43622c23cb7cSEd Maste 	}
43632c23cb7cSEd Maste }
43642c23cb7cSEd Maste 
43652c23cb7cSEd Maste #ifndef Tag_GNU_Power_ABI_FP
43662c23cb7cSEd Maste #define	Tag_GNU_Power_ABI_FP	4
43672c23cb7cSEd Maste #endif
43682c23cb7cSEd Maste 
43692c23cb7cSEd Maste #ifndef Tag_GNU_Power_ABI_Vector
43702c23cb7cSEd Maste #define	Tag_GNU_Power_ABI_Vector	8
43712c23cb7cSEd Maste #endif
43722c23cb7cSEd Maste 
43732c23cb7cSEd Maste static void
43742c23cb7cSEd Maste dump_ppc_attributes(uint8_t *p, uint8_t *pe)
43752c23cb7cSEd Maste {
43762c23cb7cSEd Maste 	uint64_t tag, val;
43772c23cb7cSEd Maste 
43782c23cb7cSEd Maste 	while (p < pe) {
437995fd7f26SEd Maste 		tag = _decode_uleb128(&p, pe);
43802c23cb7cSEd Maste 		switch (tag) {
43812c23cb7cSEd Maste 		case Tag_GNU_Power_ABI_FP:
438295fd7f26SEd Maste 			val = _decode_uleb128(&p, pe);
43832c23cb7cSEd Maste 			printf("  Tag_GNU_Power_ABI_FP: %s\n", ppc_abi_fp(val));
43842c23cb7cSEd Maste 			break;
43852c23cb7cSEd Maste 		case Tag_GNU_Power_ABI_Vector:
438695fd7f26SEd Maste 			val = _decode_uleb128(&p, pe);
43872c23cb7cSEd Maste 			printf("  Tag_GNU_Power_ABI_Vector: %s\n",
43882c23cb7cSEd Maste 			    ppc_abi_vector(val));
43892c23cb7cSEd Maste 			break;
43902c23cb7cSEd Maste 		case 32:	/* Tag_compatibility */
439195fd7f26SEd Maste 			p = dump_compatibility_tag(p, pe);
43922c23cb7cSEd Maste 			break;
43932c23cb7cSEd Maste 		default:
439495fd7f26SEd Maste 			p = dump_unknown_tag(tag, p, pe);
43952c23cb7cSEd Maste 			break;
43962c23cb7cSEd Maste 		}
43972c23cb7cSEd Maste 	}
43982c23cb7cSEd Maste }
43992c23cb7cSEd Maste 
44002c23cb7cSEd Maste static void
44012c23cb7cSEd Maste dump_attributes(struct readelf *re)
44022c23cb7cSEd Maste {
44032c23cb7cSEd Maste 	struct section *s;
44042c23cb7cSEd Maste 	Elf_Data *d;
440595fd7f26SEd Maste 	uint8_t *p, *pe, *sp;
44062c23cb7cSEd Maste 	size_t len, seclen, nlen, sublen;
44072c23cb7cSEd Maste 	uint64_t val;
44082c23cb7cSEd Maste 	int tag, i, elferr;
44092c23cb7cSEd Maste 
44102c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
44112c23cb7cSEd Maste 		s = &re->sl[i];
44122c23cb7cSEd Maste 		if (s->type != SHT_GNU_ATTRIBUTES &&
44132c23cb7cSEd Maste 		    (re->ehdr.e_machine != EM_ARM || s->type != SHT_LOPROC + 3))
44142c23cb7cSEd Maste 			continue;
44152c23cb7cSEd Maste 		(void) elf_errno();
44162c23cb7cSEd Maste 		if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
44172c23cb7cSEd Maste 			elferr = elf_errno();
44182c23cb7cSEd Maste 			if (elferr != 0)
44192c23cb7cSEd Maste 				warnx("elf_rawdata failed: %s",
44202c23cb7cSEd Maste 				    elf_errmsg(elferr));
44212c23cb7cSEd Maste 			continue;
44222c23cb7cSEd Maste 		}
44232c23cb7cSEd Maste 		if (d->d_size <= 0)
44242c23cb7cSEd Maste 			continue;
44252c23cb7cSEd Maste 		p = d->d_buf;
442695fd7f26SEd Maste 		pe = p + d->d_size;
44272c23cb7cSEd Maste 		if (*p != 'A') {
44282c23cb7cSEd Maste 			printf("Unknown Attribute Section Format: %c\n",
44292c23cb7cSEd Maste 			    (char) *p);
44302c23cb7cSEd Maste 			continue;
44312c23cb7cSEd Maste 		}
44322c23cb7cSEd Maste 		len = d->d_size - 1;
44332c23cb7cSEd Maste 		p++;
44342c23cb7cSEd Maste 		while (len > 0) {
443571a0c925SEd Maste 			if (len < 4) {
443671a0c925SEd Maste 				warnx("truncated attribute section length");
443795fd7f26SEd Maste 				return;
443871a0c925SEd Maste 			}
44392c23cb7cSEd Maste 			seclen = re->dw_decode(&p, 4);
44402c23cb7cSEd Maste 			if (seclen > len) {
44412c23cb7cSEd Maste 				warnx("invalid attribute section length");
444295fd7f26SEd Maste 				return;
44432c23cb7cSEd Maste 			}
44442c23cb7cSEd Maste 			len -= seclen;
44452c23cb7cSEd Maste 			nlen = strlen((char *) p) + 1;
444671a0c925SEd Maste 			if (nlen + 4 > seclen) {
444771a0c925SEd Maste 				warnx("invalid attribute section name");
444895fd7f26SEd Maste 				return;
444971a0c925SEd Maste 			}
445071a0c925SEd Maste 			printf("Attribute Section: %s\n", (char *) p);
44512c23cb7cSEd Maste 			p += nlen;
44522c23cb7cSEd Maste 			seclen -= nlen + 4;
44532c23cb7cSEd Maste 			while (seclen > 0) {
44542c23cb7cSEd Maste 				sp = p;
44552c23cb7cSEd Maste 				tag = *p++;
44562c23cb7cSEd Maste 				sublen = re->dw_decode(&p, 4);
44572c23cb7cSEd Maste 				if (sublen > seclen) {
44582c23cb7cSEd Maste 					warnx("invalid attribute sub-section"
44592c23cb7cSEd Maste 					    " length");
446095fd7f26SEd Maste 					return;
44612c23cb7cSEd Maste 				}
44622c23cb7cSEd Maste 				seclen -= sublen;
44632c23cb7cSEd Maste 				printf("%s", top_tag(tag));
44642c23cb7cSEd Maste 				if (tag == 2 || tag == 3) {
44652c23cb7cSEd Maste 					putchar(':');
44662c23cb7cSEd Maste 					for (;;) {
446795fd7f26SEd Maste 						val = _decode_uleb128(&p, pe);
44682c23cb7cSEd Maste 						if (val == 0)
44692c23cb7cSEd Maste 							break;
44702c23cb7cSEd Maste 						printf(" %ju", (uintmax_t) val);
44712c23cb7cSEd Maste 					}
44722c23cb7cSEd Maste 				}
44732c23cb7cSEd Maste 				putchar('\n');
44742c23cb7cSEd Maste 				if (re->ehdr.e_machine == EM_ARM &&
44752c23cb7cSEd Maste 				    s->type == SHT_LOPROC + 3)
44762c23cb7cSEd Maste 					dump_arm_attributes(re, p, sp + sublen);
44772c23cb7cSEd Maste 				else if (re->ehdr.e_machine == EM_MIPS ||
44782c23cb7cSEd Maste 				    re->ehdr.e_machine == EM_MIPS_RS3_LE)
44792c23cb7cSEd Maste 					dump_mips_attributes(re, p,
44802c23cb7cSEd Maste 					    sp + sublen);
44812c23cb7cSEd Maste 				else if (re->ehdr.e_machine == EM_PPC)
44822c23cb7cSEd Maste 					dump_ppc_attributes(p, sp + sublen);
44832c23cb7cSEd Maste 				p = sp + sublen;
44842c23cb7cSEd Maste 			}
44852c23cb7cSEd Maste 		}
44862c23cb7cSEd Maste 	}
44872c23cb7cSEd Maste }
44882c23cb7cSEd Maste 
44892c23cb7cSEd Maste static void
44902c23cb7cSEd Maste dump_mips_specific_info(struct readelf *re)
44912c23cb7cSEd Maste {
44922c23cb7cSEd Maste 	struct section *s;
4493bee2765cSEd Maste 	int i;
44942c23cb7cSEd Maste 
44952c23cb7cSEd Maste 	s = NULL;
44962c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
44972c23cb7cSEd Maste 		s = &re->sl[i];
44982c23cb7cSEd Maste 		if (s->name != NULL && (!strcmp(s->name, ".MIPS.options") ||
44992c23cb7cSEd Maste 		    (s->type == SHT_MIPS_OPTIONS))) {
45002c23cb7cSEd Maste 			dump_mips_options(re, s);
45012c23cb7cSEd Maste 		}
45022c23cb7cSEd Maste 	}
45032c23cb7cSEd Maste 
4504e9f3f88aSEd Maste 	if (s->name != NULL && (!strcmp(s->name, ".MIPS.abiflags") ||
4505e9f3f88aSEd Maste 	    (s->type == SHT_MIPS_ABIFLAGS)))
4506e9f3f88aSEd Maste 		dump_mips_abiflags(re, s);
4507e9f3f88aSEd Maste 
45082c23cb7cSEd Maste 	/*
4509bee2765cSEd Maste 	 * Dump .reginfo if present (although it will be ignored by an OS if a
4510bee2765cSEd Maste 	 * .MIPS.options section is present, according to SGI mips64 spec).
45112c23cb7cSEd Maste 	 */
45122c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
45132c23cb7cSEd Maste 		s = &re->sl[i];
45142c23cb7cSEd Maste 		if (s->name != NULL && (!strcmp(s->name, ".reginfo") ||
45152c23cb7cSEd Maste 		    (s->type == SHT_MIPS_REGINFO)))
45162c23cb7cSEd Maste 			dump_mips_reginfo(re, s);
45172c23cb7cSEd Maste 	}
45182c23cb7cSEd Maste }
45192c23cb7cSEd Maste 
45202c23cb7cSEd Maste static void
4521e9f3f88aSEd Maste dump_mips_abiflags(struct readelf *re, struct section *s)
4522e9f3f88aSEd Maste {
4523e9f3f88aSEd Maste 	Elf_Data *d;
4524e9f3f88aSEd Maste 	uint8_t *p;
4525e9f3f88aSEd Maste 	int elferr;
4526e9f3f88aSEd Maste 	uint32_t isa_ext, ases, flags1, flags2;
4527e9f3f88aSEd Maste 	uint16_t version;
4528e9f3f88aSEd Maste 	uint8_t isa_level, isa_rev, gpr_size, cpr1_size, cpr2_size, fp_abi;
4529e9f3f88aSEd Maste 
4530e9f3f88aSEd Maste 	if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
4531e9f3f88aSEd Maste 		elferr = elf_errno();
4532e9f3f88aSEd Maste 		if (elferr != 0)
4533e9f3f88aSEd Maste 			warnx("elf_rawdata failed: %s",
4534e9f3f88aSEd Maste 			    elf_errmsg(elferr));
4535e9f3f88aSEd Maste 		return;
4536e9f3f88aSEd Maste 	}
4537e9f3f88aSEd Maste 	if (d->d_size != 24) {
4538e9f3f88aSEd Maste 		warnx("invalid MIPS abiflags section size");
4539e9f3f88aSEd Maste 		return;
4540e9f3f88aSEd Maste 	}
4541e9f3f88aSEd Maste 
4542e9f3f88aSEd Maste 	p = d->d_buf;
4543e9f3f88aSEd Maste 	version = re->dw_decode(&p, 2);
4544e9f3f88aSEd Maste 	printf("MIPS ABI Flags Version: %u", version);
4545e9f3f88aSEd Maste 	if (version != 0) {
4546e9f3f88aSEd Maste 		printf(" (unknown)\n\n");
4547e9f3f88aSEd Maste 		return;
4548e9f3f88aSEd Maste 	}
4549e9f3f88aSEd Maste 	printf("\n\n");
4550e9f3f88aSEd Maste 
4551e9f3f88aSEd Maste 	isa_level = re->dw_decode(&p, 1);
4552e9f3f88aSEd Maste 	isa_rev = re->dw_decode(&p, 1);
4553e9f3f88aSEd Maste 	gpr_size = re->dw_decode(&p, 1);
4554e9f3f88aSEd Maste 	cpr1_size = re->dw_decode(&p, 1);
4555e9f3f88aSEd Maste 	cpr2_size = re->dw_decode(&p, 1);
4556e9f3f88aSEd Maste 	fp_abi = re->dw_decode(&p, 1);
4557e9f3f88aSEd Maste 	isa_ext = re->dw_decode(&p, 4);
4558e9f3f88aSEd Maste 	ases = re->dw_decode(&p, 4);
4559e9f3f88aSEd Maste 	flags1 = re->dw_decode(&p, 4);
4560e9f3f88aSEd Maste 	flags2 = re->dw_decode(&p, 4);
4561e9f3f88aSEd Maste 
4562e9f3f88aSEd Maste 	printf("ISA: ");
4563e9f3f88aSEd Maste 	if (isa_rev <= 1)
4564e9f3f88aSEd Maste 		printf("MIPS%u\n", isa_level);
4565e9f3f88aSEd Maste 	else
4566e9f3f88aSEd Maste 		printf("MIPS%ur%u\n", isa_level, isa_rev);
4567e9f3f88aSEd Maste 	printf("GPR size: %d\n", get_mips_register_size(gpr_size));
4568e9f3f88aSEd Maste 	printf("CPR1 size: %d\n", get_mips_register_size(cpr1_size));
4569e9f3f88aSEd Maste 	printf("CPR2 size: %d\n", get_mips_register_size(cpr2_size));
4570e9f3f88aSEd Maste 	printf("FP ABI: ");
4571e9f3f88aSEd Maste 	switch (fp_abi) {
4572e9f3f88aSEd Maste 	case 3:
4573e9f3f88aSEd Maste 		printf("Soft float");
4574e9f3f88aSEd Maste 		break;
4575e9f3f88aSEd Maste 	default:
4576e9f3f88aSEd Maste 		printf("%u", fp_abi);
4577e9f3f88aSEd Maste 		break;
4578e9f3f88aSEd Maste 	}
4579e9f3f88aSEd Maste 	printf("\nISA Extension: %u\n", isa_ext);
4580e9f3f88aSEd Maste 	printf("ASEs: %u\n", ases);
4581e9f3f88aSEd Maste 	printf("FLAGS 1: %08x\n", flags1);
4582e9f3f88aSEd Maste 	printf("FLAGS 2: %08x\n", flags2);
4583e9f3f88aSEd Maste }
4584e9f3f88aSEd Maste 
4585e9f3f88aSEd Maste static int
4586e9f3f88aSEd Maste get_mips_register_size(uint8_t flag)
4587e9f3f88aSEd Maste {
4588e9f3f88aSEd Maste 	switch (flag) {
4589e9f3f88aSEd Maste 	case 0: return 0;
4590e9f3f88aSEd Maste 	case 1: return 32;
4591e9f3f88aSEd Maste 	case 2: return 64;
4592e9f3f88aSEd Maste 	case 3: return 128;
4593e9f3f88aSEd Maste 	default: return -1;
4594e9f3f88aSEd Maste 	}
4595e9f3f88aSEd Maste }
4596e9f3f88aSEd Maste static void
45972c23cb7cSEd Maste dump_mips_reginfo(struct readelf *re, struct section *s)
45982c23cb7cSEd Maste {
45992c23cb7cSEd Maste 	Elf_Data *d;
460071edbbfdSEd Maste 	int elferr, len;
46012c23cb7cSEd Maste 
46022c23cb7cSEd Maste 	(void) elf_errno();
46032c23cb7cSEd Maste 	if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
46042c23cb7cSEd Maste 		elferr = elf_errno();
46052c23cb7cSEd Maste 		if (elferr != 0)
46062c23cb7cSEd Maste 			warnx("elf_rawdata failed: %s",
46072c23cb7cSEd Maste 			    elf_errmsg(elferr));
46082c23cb7cSEd Maste 		return;
46092c23cb7cSEd Maste 	}
46102c23cb7cSEd Maste 	if (d->d_size <= 0)
46112c23cb7cSEd Maste 		return;
461271edbbfdSEd Maste 	if (!get_ent_count(s, &len))
461371edbbfdSEd Maste 		return;
46142c23cb7cSEd Maste 
461571edbbfdSEd Maste 	printf("\nSection '%s' contains %d entries:\n", s->name, len);
46162c23cb7cSEd Maste 	dump_mips_odk_reginfo(re, d->d_buf, d->d_size);
46172c23cb7cSEd Maste }
46182c23cb7cSEd Maste 
46192c23cb7cSEd Maste static void
46202c23cb7cSEd Maste dump_mips_options(struct readelf *re, struct section *s)
46212c23cb7cSEd Maste {
46222c23cb7cSEd Maste 	Elf_Data *d;
46232c23cb7cSEd Maste 	uint32_t info;
46242c23cb7cSEd Maste 	uint16_t sndx;
46252c23cb7cSEd Maste 	uint8_t *p, *pe;
46262c23cb7cSEd Maste 	uint8_t kind, size;
46272c23cb7cSEd Maste 	int elferr;
46282c23cb7cSEd Maste 
46292c23cb7cSEd Maste 	(void) elf_errno();
46302c23cb7cSEd Maste 	if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
46312c23cb7cSEd Maste 		elferr = elf_errno();
46322c23cb7cSEd Maste 		if (elferr != 0)
46332c23cb7cSEd Maste 			warnx("elf_rawdata failed: %s",
46342c23cb7cSEd Maste 			    elf_errmsg(elferr));
46352c23cb7cSEd Maste 		return;
46362c23cb7cSEd Maste 	}
46372c23cb7cSEd Maste 	if (d->d_size == 0)
46382c23cb7cSEd Maste 		return;
46392c23cb7cSEd Maste 
46402c23cb7cSEd Maste 	printf("\nSection %s contains:\n", s->name);
46412c23cb7cSEd Maste 	p = d->d_buf;
46422c23cb7cSEd Maste 	pe = p + d->d_size;
46432c23cb7cSEd Maste 	while (p < pe) {
4644b00fe64fSEd Maste 		if (pe - p < 8) {
4645b00fe64fSEd Maste 			warnx("Truncated MIPS option header");
4646b00fe64fSEd Maste 			return;
4647b00fe64fSEd Maste 		}
46482c23cb7cSEd Maste 		kind = re->dw_decode(&p, 1);
46492c23cb7cSEd Maste 		size = re->dw_decode(&p, 1);
46502c23cb7cSEd Maste 		sndx = re->dw_decode(&p, 2);
46512c23cb7cSEd Maste 		info = re->dw_decode(&p, 4);
4652b00fe64fSEd Maste 		if (size < 8 || size - 8 > pe - p) {
4653b00fe64fSEd Maste 			warnx("Malformed MIPS option header");
4654b00fe64fSEd Maste 			return;
4655b00fe64fSEd Maste 		}
4656b00fe64fSEd Maste 		size -= 8;
46572c23cb7cSEd Maste 		switch (kind) {
46582c23cb7cSEd Maste 		case ODK_REGINFO:
4659b00fe64fSEd Maste 			dump_mips_odk_reginfo(re, p, size);
46602c23cb7cSEd Maste 			break;
46612c23cb7cSEd Maste 		case ODK_EXCEPTIONS:
46622c23cb7cSEd Maste 			printf(" EXCEPTIONS FPU_MIN: %#x\n",
46632c23cb7cSEd Maste 			    info & OEX_FPU_MIN);
46642c23cb7cSEd Maste 			printf("%11.11s FPU_MAX: %#x\n", "",
46652c23cb7cSEd Maste 			    info & OEX_FPU_MAX);
46662c23cb7cSEd Maste 			dump_mips_option_flags("", mips_exceptions_option,
46672c23cb7cSEd Maste 			    info);
46682c23cb7cSEd Maste 			break;
46692c23cb7cSEd Maste 		case ODK_PAD:
46702c23cb7cSEd Maste 			printf(" %-10.10s section: %ju\n", "OPAD",
46712c23cb7cSEd Maste 			    (uintmax_t) sndx);
46722c23cb7cSEd Maste 			dump_mips_option_flags("", mips_pad_option, info);
46732c23cb7cSEd Maste 			break;
46742c23cb7cSEd Maste 		case ODK_HWPATCH:
46752c23cb7cSEd Maste 			dump_mips_option_flags("HWPATCH", mips_hwpatch_option,
46762c23cb7cSEd Maste 			    info);
46772c23cb7cSEd Maste 			break;
46782c23cb7cSEd Maste 		case ODK_HWAND:
46792c23cb7cSEd Maste 			dump_mips_option_flags("HWAND", mips_hwa_option, info);
46802c23cb7cSEd Maste 			break;
46812c23cb7cSEd Maste 		case ODK_HWOR:
46822c23cb7cSEd Maste 			dump_mips_option_flags("HWOR", mips_hwo_option, info);
46832c23cb7cSEd Maste 			break;
46842c23cb7cSEd Maste 		case ODK_FILL:
46852c23cb7cSEd Maste 			printf(" %-10.10s %#jx\n", "FILL", (uintmax_t) info);
46862c23cb7cSEd Maste 			break;
46872c23cb7cSEd Maste 		case ODK_TAGS:
46882c23cb7cSEd Maste 			printf(" %-10.10s\n", "TAGS");
46892c23cb7cSEd Maste 			break;
46902c23cb7cSEd Maste 		case ODK_GP_GROUP:
46912c23cb7cSEd Maste 			printf(" %-10.10s GP group number: %#x\n", "GP_GROUP",
46922c23cb7cSEd Maste 			    info & 0xFFFF);
46932c23cb7cSEd Maste 			if (info & 0x10000)
46942c23cb7cSEd Maste 				printf(" %-10.10s GP group is "
46952c23cb7cSEd Maste 				    "self-contained\n", "");
46962c23cb7cSEd Maste 			break;
46972c23cb7cSEd Maste 		case ODK_IDENT:
46982c23cb7cSEd Maste 			printf(" %-10.10s default GP group number: %#x\n",
46992c23cb7cSEd Maste 			    "IDENT", info & 0xFFFF);
47002c23cb7cSEd Maste 			if (info & 0x10000)
47012c23cb7cSEd Maste 				printf(" %-10.10s default GP group is "
47022c23cb7cSEd Maste 				    "self-contained\n", "");
47032c23cb7cSEd Maste 			break;
47042c23cb7cSEd Maste 		case ODK_PAGESIZE:
47052c23cb7cSEd Maste 			printf(" %-10.10s\n", "PAGESIZE");
47062c23cb7cSEd Maste 			break;
47072c23cb7cSEd Maste 		default:
47082c23cb7cSEd Maste 			break;
47092c23cb7cSEd Maste 		}
4710b00fe64fSEd Maste 		p += size;
47112c23cb7cSEd Maste 	}
47122c23cb7cSEd Maste }
47132c23cb7cSEd Maste 
47142c23cb7cSEd Maste static void
47152c23cb7cSEd Maste dump_mips_option_flags(const char *name, struct mips_option *opt, uint64_t info)
47162c23cb7cSEd Maste {
47172c23cb7cSEd Maste 	int first;
47182c23cb7cSEd Maste 
47192c23cb7cSEd Maste 	first = 1;
47202c23cb7cSEd Maste 	for (; opt->desc != NULL; opt++) {
47212c23cb7cSEd Maste 		if (info & opt->flag) {
47222c23cb7cSEd Maste 			printf(" %-10.10s %s\n", first ? name : "",
47232c23cb7cSEd Maste 			    opt->desc);
47242c23cb7cSEd Maste 			first = 0;
47252c23cb7cSEd Maste 		}
47262c23cb7cSEd Maste 	}
47272c23cb7cSEd Maste }
47282c23cb7cSEd Maste 
47292c23cb7cSEd Maste static void
47302c23cb7cSEd Maste dump_mips_odk_reginfo(struct readelf *re, uint8_t *p, size_t sz)
47312c23cb7cSEd Maste {
47322c23cb7cSEd Maste 	uint32_t ri_gprmask;
47332c23cb7cSEd Maste 	uint32_t ri_cprmask[4];
47342c23cb7cSEd Maste 	uint64_t ri_gp_value;
47352c23cb7cSEd Maste 	uint8_t *pe;
47362c23cb7cSEd Maste 	int i;
47372c23cb7cSEd Maste 
47382c23cb7cSEd Maste 	pe = p + sz;
47392c23cb7cSEd Maste 	while (p < pe) {
47402c23cb7cSEd Maste 		ri_gprmask = re->dw_decode(&p, 4);
47412c23cb7cSEd Maste 		/* Skip ri_pad padding field for mips64. */
47422c23cb7cSEd Maste 		if (re->ec == ELFCLASS64)
47432c23cb7cSEd Maste 			re->dw_decode(&p, 4);
47442c23cb7cSEd Maste 		for (i = 0; i < 4; i++)
47452c23cb7cSEd Maste 			ri_cprmask[i] = re->dw_decode(&p, 4);
47462c23cb7cSEd Maste 		if (re->ec == ELFCLASS32)
47472c23cb7cSEd Maste 			ri_gp_value = re->dw_decode(&p, 4);
47482c23cb7cSEd Maste 		else
47492c23cb7cSEd Maste 			ri_gp_value = re->dw_decode(&p, 8);
47502c23cb7cSEd Maste 		printf(" %s    ", option_kind(ODK_REGINFO));
47512c23cb7cSEd Maste 		printf("ri_gprmask:    0x%08jx\n", (uintmax_t) ri_gprmask);
47522c23cb7cSEd Maste 		for (i = 0; i < 4; i++)
47532c23cb7cSEd Maste 			printf("%11.11s ri_cprmask[%d]: 0x%08jx\n", "", i,
47542c23cb7cSEd Maste 			    (uintmax_t) ri_cprmask[i]);
47552c23cb7cSEd Maste 		printf("%12.12s", "");
47562c23cb7cSEd Maste 		printf("ri_gp_value:   %#jx\n", (uintmax_t) ri_gp_value);
47572c23cb7cSEd Maste 	}
47582c23cb7cSEd Maste }
47592c23cb7cSEd Maste 
47602c23cb7cSEd Maste static void
47612c23cb7cSEd Maste dump_arch_specific_info(struct readelf *re)
47622c23cb7cSEd Maste {
47632c23cb7cSEd Maste 
47642c23cb7cSEd Maste 	dump_liblist(re);
47652c23cb7cSEd Maste 	dump_attributes(re);
47662c23cb7cSEd Maste 
47672c23cb7cSEd Maste 	switch (re->ehdr.e_machine) {
47682c23cb7cSEd Maste 	case EM_MIPS:
47692c23cb7cSEd Maste 	case EM_MIPS_RS3_LE:
47702c23cb7cSEd Maste 		dump_mips_specific_info(re);
47712c23cb7cSEd Maste 	default:
47722c23cb7cSEd Maste 		break;
47732c23cb7cSEd Maste 	}
47742c23cb7cSEd Maste }
47752c23cb7cSEd Maste 
4776cf781b2eSEd Maste static const char *
4777cf781b2eSEd Maste dwarf_regname(struct readelf *re, unsigned int num)
4778cf781b2eSEd Maste {
4779cf781b2eSEd Maste 	static char rx[32];
4780cf781b2eSEd Maste 	const char *rn;
4781cf781b2eSEd Maste 
4782cf781b2eSEd Maste 	if ((rn = dwarf_reg(re->ehdr.e_machine, num)) != NULL)
4783cf781b2eSEd Maste 		return (rn);
4784cf781b2eSEd Maste 
4785cf781b2eSEd Maste 	snprintf(rx, sizeof(rx), "r%u", num);
4786cf781b2eSEd Maste 
4787cf781b2eSEd Maste 	return (rx);
4788cf781b2eSEd Maste }
4789cf781b2eSEd Maste 
47902c23cb7cSEd Maste static void
47912c23cb7cSEd Maste dump_dwarf_line(struct readelf *re)
47922c23cb7cSEd Maste {
47932c23cb7cSEd Maste 	struct section *s;
47942c23cb7cSEd Maste 	Dwarf_Die die;
47952c23cb7cSEd Maste 	Dwarf_Error de;
47962c23cb7cSEd Maste 	Dwarf_Half tag, version, pointer_size;
47972c23cb7cSEd Maste 	Dwarf_Unsigned offset, endoff, length, hdrlen, dirndx, mtime, fsize;
47982c23cb7cSEd Maste 	Dwarf_Small minlen, defstmt, lrange, opbase, oplen;
47992c23cb7cSEd Maste 	Elf_Data *d;
48002c23cb7cSEd Maste 	char *pn;
48012c23cb7cSEd Maste 	uint64_t address, file, line, column, isa, opsize, udelta;
48022c23cb7cSEd Maste 	int64_t sdelta;
48032c23cb7cSEd Maste 	uint8_t *p, *pe;
48042c23cb7cSEd Maste 	int8_t lbase;
4805cf781b2eSEd Maste 	int i, is_stmt, dwarf_size, elferr, ret;
48062c23cb7cSEd Maste 
48072c23cb7cSEd Maste 	printf("\nDump of debug contents of section .debug_line:\n");
48082c23cb7cSEd Maste 
48092c23cb7cSEd Maste 	s = NULL;
48102c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
48112c23cb7cSEd Maste 		s = &re->sl[i];
48122c23cb7cSEd Maste 		if (s->name != NULL && !strcmp(s->name, ".debug_line"))
48132c23cb7cSEd Maste 			break;
48142c23cb7cSEd Maste 	}
48152c23cb7cSEd Maste 	if ((size_t) i >= re->shnum)
48162c23cb7cSEd Maste 		return;
48172c23cb7cSEd Maste 
48182c23cb7cSEd Maste 	(void) elf_errno();
48192c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
48202c23cb7cSEd Maste 		elferr = elf_errno();
48212c23cb7cSEd Maste 		if (elferr != 0)
48222c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
48232c23cb7cSEd Maste 		return;
48242c23cb7cSEd Maste 	}
48252c23cb7cSEd Maste 	if (d->d_size <= 0)
48262c23cb7cSEd Maste 		return;
48272c23cb7cSEd Maste 
48282c23cb7cSEd Maste 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
48292c23cb7cSEd Maste 	    NULL, &de)) ==  DW_DLV_OK) {
48302c23cb7cSEd Maste 		die = NULL;
48312c23cb7cSEd Maste 		while (dwarf_siblingof(re->dbg, die, &die, &de) == DW_DLV_OK) {
48322c23cb7cSEd Maste 			if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
48332c23cb7cSEd Maste 				warnx("dwarf_tag failed: %s",
48342c23cb7cSEd Maste 				    dwarf_errmsg(de));
48352c23cb7cSEd Maste 				return;
48362c23cb7cSEd Maste 			}
48372c23cb7cSEd Maste 			/* XXX: What about DW_TAG_partial_unit? */
48382c23cb7cSEd Maste 			if (tag == DW_TAG_compile_unit)
48392c23cb7cSEd Maste 				break;
48402c23cb7cSEd Maste 		}
48412c23cb7cSEd Maste 		if (die == NULL) {
48422c23cb7cSEd Maste 			warnx("could not find DW_TAG_compile_unit die");
48432c23cb7cSEd Maste 			return;
48442c23cb7cSEd Maste 		}
48452c23cb7cSEd Maste 		if (dwarf_attrval_unsigned(die, DW_AT_stmt_list, &offset,
48462c23cb7cSEd Maste 		    &de) != DW_DLV_OK)
48472c23cb7cSEd Maste 			continue;
48482c23cb7cSEd Maste 
48492c23cb7cSEd Maste 		length = re->dw_read(d, &offset, 4);
48502c23cb7cSEd Maste 		if (length == 0xffffffff) {
48512c23cb7cSEd Maste 			dwarf_size = 8;
48522c23cb7cSEd Maste 			length = re->dw_read(d, &offset, 8);
48532c23cb7cSEd Maste 		} else
48542c23cb7cSEd Maste 			dwarf_size = 4;
48552c23cb7cSEd Maste 
48562c23cb7cSEd Maste 		if (length > d->d_size - offset) {
48572c23cb7cSEd Maste 			warnx("invalid .dwarf_line section");
48582c23cb7cSEd Maste 			continue;
48592c23cb7cSEd Maste 		}
48602c23cb7cSEd Maste 
48612c23cb7cSEd Maste 		endoff = offset + length;
48628f32e46dSKai Wang 		pe = (uint8_t *) d->d_buf + endoff;
48632c23cb7cSEd Maste 		version = re->dw_read(d, &offset, 2);
48642c23cb7cSEd Maste 		hdrlen = re->dw_read(d, &offset, dwarf_size);
48652c23cb7cSEd Maste 		minlen = re->dw_read(d, &offset, 1);
48662c23cb7cSEd Maste 		defstmt = re->dw_read(d, &offset, 1);
48672c23cb7cSEd Maste 		lbase = re->dw_read(d, &offset, 1);
48682c23cb7cSEd Maste 		lrange = re->dw_read(d, &offset, 1);
48692c23cb7cSEd Maste 		opbase = re->dw_read(d, &offset, 1);
48702c23cb7cSEd Maste 
48712c23cb7cSEd Maste 		printf("\n");
48722c23cb7cSEd Maste 		printf("  Length:\t\t\t%ju\n", (uintmax_t) length);
48732c23cb7cSEd Maste 		printf("  DWARF version:\t\t%u\n", version);
48742c23cb7cSEd Maste 		printf("  Prologue Length:\t\t%ju\n", (uintmax_t) hdrlen);
48752c23cb7cSEd Maste 		printf("  Minimum Instruction Length:\t%u\n", minlen);
48762c23cb7cSEd Maste 		printf("  Initial value of 'is_stmt':\t%u\n", defstmt);
48772c23cb7cSEd Maste 		printf("  Line Base:\t\t\t%d\n", lbase);
48782c23cb7cSEd Maste 		printf("  Line Range:\t\t\t%u\n", lrange);
48792c23cb7cSEd Maste 		printf("  Opcode Base:\t\t\t%u\n", opbase);
48802c23cb7cSEd Maste 		(void) dwarf_get_address_size(re->dbg, &pointer_size, &de);
48812c23cb7cSEd Maste 		printf("  (Pointer size:\t\t%u)\n", pointer_size);
48822c23cb7cSEd Maste 
48832c23cb7cSEd Maste 		printf("\n");
48842c23cb7cSEd Maste 		printf(" Opcodes:\n");
48852c23cb7cSEd Maste 		for (i = 1; i < opbase; i++) {
48862c23cb7cSEd Maste 			oplen = re->dw_read(d, &offset, 1);
48872c23cb7cSEd Maste 			printf("  Opcode %d has %u args\n", i, oplen);
48882c23cb7cSEd Maste 		}
48892c23cb7cSEd Maste 
48902c23cb7cSEd Maste 		printf("\n");
48912c23cb7cSEd Maste 		printf(" The Directory Table:\n");
48922c23cb7cSEd Maste 		p = (uint8_t *) d->d_buf + offset;
48932c23cb7cSEd Maste 		while (*p != '\0') {
48942c23cb7cSEd Maste 			printf("  %s\n", (char *) p);
48952c23cb7cSEd Maste 			p += strlen((char *) p) + 1;
48962c23cb7cSEd Maste 		}
48972c23cb7cSEd Maste 
48982c23cb7cSEd Maste 		p++;
48992c23cb7cSEd Maste 		printf("\n");
49002c23cb7cSEd Maste 		printf(" The File Name Table:\n");
49012c23cb7cSEd Maste 		printf("  Entry\tDir\tTime\tSize\tName\n");
49022c23cb7cSEd Maste 		i = 0;
49032c23cb7cSEd Maste 		while (*p != '\0') {
49042c23cb7cSEd Maste 			i++;
49052c23cb7cSEd Maste 			pn = (char *) p;
49062c23cb7cSEd Maste 			p += strlen(pn) + 1;
490795fd7f26SEd Maste 			dirndx = _decode_uleb128(&p, pe);
490895fd7f26SEd Maste 			mtime = _decode_uleb128(&p, pe);
490995fd7f26SEd Maste 			fsize = _decode_uleb128(&p, pe);
49102c23cb7cSEd Maste 			printf("  %d\t%ju\t%ju\t%ju\t%s\n", i,
49112c23cb7cSEd Maste 			    (uintmax_t) dirndx, (uintmax_t) mtime,
49122c23cb7cSEd Maste 			    (uintmax_t) fsize, pn);
49132c23cb7cSEd Maste 		}
49142c23cb7cSEd Maste 
49152c23cb7cSEd Maste #define	RESET_REGISTERS						\
49162c23cb7cSEd Maste 	do {							\
49172c23cb7cSEd Maste 		address	       = 0;				\
49182c23cb7cSEd Maste 		file	       = 1;				\
49192c23cb7cSEd Maste 		line	       = 1;				\
49202c23cb7cSEd Maste 		column	       = 0;				\
49212c23cb7cSEd Maste 		is_stmt	       = defstmt;			\
49222c23cb7cSEd Maste 	} while(0)
49232c23cb7cSEd Maste 
49242c23cb7cSEd Maste #define	LINE(x) (lbase + (((x) - opbase) % lrange))
49252c23cb7cSEd Maste #define	ADDRESS(x) ((((x) - opbase) / lrange) * minlen)
49262c23cb7cSEd Maste 
49272c23cb7cSEd Maste 		p++;
49282c23cb7cSEd Maste 		printf("\n");
49292c23cb7cSEd Maste 		printf(" Line Number Statements:\n");
49302c23cb7cSEd Maste 
49312c23cb7cSEd Maste 		RESET_REGISTERS;
49322c23cb7cSEd Maste 
49332c23cb7cSEd Maste 		while (p < pe) {
49342c23cb7cSEd Maste 
49352c23cb7cSEd Maste 			if (*p == 0) {
49362c23cb7cSEd Maste 				/*
49372c23cb7cSEd Maste 				 * Extended Opcodes.
49382c23cb7cSEd Maste 				 */
49392c23cb7cSEd Maste 				p++;
494095fd7f26SEd Maste 				opsize = _decode_uleb128(&p, pe);
49412c23cb7cSEd Maste 				printf("  Extended opcode %u: ", *p);
49422c23cb7cSEd Maste 				switch (*p) {
49432c23cb7cSEd Maste 				case DW_LNE_end_sequence:
49442c23cb7cSEd Maste 					p++;
49452c23cb7cSEd Maste 					RESET_REGISTERS;
49462c23cb7cSEd Maste 					printf("End of Sequence\n");
49472c23cb7cSEd Maste 					break;
49482c23cb7cSEd Maste 				case DW_LNE_set_address:
49492c23cb7cSEd Maste 					p++;
49502c23cb7cSEd Maste 					address = re->dw_decode(&p,
49512c23cb7cSEd Maste 					    pointer_size);
49522c23cb7cSEd Maste 					printf("set Address to %#jx\n",
49532c23cb7cSEd Maste 					    (uintmax_t) address);
49542c23cb7cSEd Maste 					break;
49552c23cb7cSEd Maste 				case DW_LNE_define_file:
49562c23cb7cSEd Maste 					p++;
49572c23cb7cSEd Maste 					pn = (char *) p;
49582c23cb7cSEd Maste 					p += strlen(pn) + 1;
495995fd7f26SEd Maste 					dirndx = _decode_uleb128(&p, pe);
496095fd7f26SEd Maste 					mtime = _decode_uleb128(&p, pe);
496195fd7f26SEd Maste 					fsize = _decode_uleb128(&p, pe);
49622c23cb7cSEd Maste 					printf("define new file: %s\n", pn);
49632c23cb7cSEd Maste 					break;
49642c23cb7cSEd Maste 				default:
49652c23cb7cSEd Maste 					/* Unrecognized extened opcodes. */
49662c23cb7cSEd Maste 					p += opsize;
49672c23cb7cSEd Maste 					printf("unknown opcode\n");
49682c23cb7cSEd Maste 				}
49692c23cb7cSEd Maste 			} else if (*p > 0 && *p < opbase) {
49702c23cb7cSEd Maste 				/*
49712c23cb7cSEd Maste 				 * Standard Opcodes.
49722c23cb7cSEd Maste 				 */
49732c23cb7cSEd Maste 				switch(*p++) {
49742c23cb7cSEd Maste 				case DW_LNS_copy:
49752c23cb7cSEd Maste 					printf("  Copy\n");
49762c23cb7cSEd Maste 					break;
49772c23cb7cSEd Maste 				case DW_LNS_advance_pc:
497895fd7f26SEd Maste 					udelta = _decode_uleb128(&p, pe) *
49792c23cb7cSEd Maste 					    minlen;
49802c23cb7cSEd Maste 					address += udelta;
49812c23cb7cSEd Maste 					printf("  Advance PC by %ju to %#jx\n",
49822c23cb7cSEd Maste 					    (uintmax_t) udelta,
49832c23cb7cSEd Maste 					    (uintmax_t) address);
49842c23cb7cSEd Maste 					break;
49852c23cb7cSEd Maste 				case DW_LNS_advance_line:
498695fd7f26SEd Maste 					sdelta = _decode_sleb128(&p, pe);
49872c23cb7cSEd Maste 					line += sdelta;
49882c23cb7cSEd Maste 					printf("  Advance Line by %jd to %ju\n",
49892c23cb7cSEd Maste 					    (intmax_t) sdelta,
49902c23cb7cSEd Maste 					    (uintmax_t) line);
49912c23cb7cSEd Maste 					break;
49922c23cb7cSEd Maste 				case DW_LNS_set_file:
499395fd7f26SEd Maste 					file = _decode_uleb128(&p, pe);
49942c23cb7cSEd Maste 					printf("  Set File to %ju\n",
49952c23cb7cSEd Maste 					    (uintmax_t) file);
49962c23cb7cSEd Maste 					break;
49972c23cb7cSEd Maste 				case DW_LNS_set_column:
499895fd7f26SEd Maste 					column = _decode_uleb128(&p, pe);
49992c23cb7cSEd Maste 					printf("  Set Column to %ju\n",
50002c23cb7cSEd Maste 					    (uintmax_t) column);
50012c23cb7cSEd Maste 					break;
50022c23cb7cSEd Maste 				case DW_LNS_negate_stmt:
50032c23cb7cSEd Maste 					is_stmt = !is_stmt;
50042c23cb7cSEd Maste 					printf("  Set is_stmt to %d\n", is_stmt);
50052c23cb7cSEd Maste 					break;
50062c23cb7cSEd Maste 				case DW_LNS_set_basic_block:
50072c23cb7cSEd Maste 					printf("  Set basic block flag\n");
50082c23cb7cSEd Maste 					break;
50092c23cb7cSEd Maste 				case DW_LNS_const_add_pc:
50102c23cb7cSEd Maste 					address += ADDRESS(255);
50112c23cb7cSEd Maste 					printf("  Advance PC by constant %ju"
50122c23cb7cSEd Maste 					    " to %#jx\n",
50132c23cb7cSEd Maste 					    (uintmax_t) ADDRESS(255),
50142c23cb7cSEd Maste 					    (uintmax_t) address);
50152c23cb7cSEd Maste 					break;
50162c23cb7cSEd Maste 				case DW_LNS_fixed_advance_pc:
50172c23cb7cSEd Maste 					udelta = re->dw_decode(&p, 2);
50182c23cb7cSEd Maste 					address += udelta;
50192c23cb7cSEd Maste 					printf("  Advance PC by fixed value "
50202c23cb7cSEd Maste 					    "%ju to %#jx\n",
50212c23cb7cSEd Maste 					    (uintmax_t) udelta,
50222c23cb7cSEd Maste 					    (uintmax_t) address);
50232c23cb7cSEd Maste 					break;
50242c23cb7cSEd Maste 				case DW_LNS_set_prologue_end:
50252c23cb7cSEd Maste 					printf("  Set prologue end flag\n");
50262c23cb7cSEd Maste 					break;
50272c23cb7cSEd Maste 				case DW_LNS_set_epilogue_begin:
50282c23cb7cSEd Maste 					printf("  Set epilogue begin flag\n");
50292c23cb7cSEd Maste 					break;
50302c23cb7cSEd Maste 				case DW_LNS_set_isa:
503195fd7f26SEd Maste 					isa = _decode_uleb128(&p, pe);
5032839529caSEd Maste 					printf("  Set isa to %ju\n",
5033839529caSEd Maste 					    (uintmax_t) isa);
50342c23cb7cSEd Maste 					break;
50352c23cb7cSEd Maste 				default:
50362c23cb7cSEd Maste 					/* Unrecognized extended opcodes. */
50372c23cb7cSEd Maste 					printf("  Unknown extended opcode %u\n",
50382c23cb7cSEd Maste 					    *(p - 1));
50392c23cb7cSEd Maste 					break;
50402c23cb7cSEd Maste 				}
50412c23cb7cSEd Maste 
50422c23cb7cSEd Maste 			} else {
50432c23cb7cSEd Maste 				/*
50442c23cb7cSEd Maste 				 * Special Opcodes.
50452c23cb7cSEd Maste 				 */
50462c23cb7cSEd Maste 				line += LINE(*p);
50472c23cb7cSEd Maste 				address += ADDRESS(*p);
50482c23cb7cSEd Maste 				printf("  Special opcode %u: advance Address "
50492c23cb7cSEd Maste 				    "by %ju to %#jx and Line by %jd to %ju\n",
50502c23cb7cSEd Maste 				    *p - opbase, (uintmax_t) ADDRESS(*p),
50512c23cb7cSEd Maste 				    (uintmax_t) address, (intmax_t) LINE(*p),
50522c23cb7cSEd Maste 				    (uintmax_t) line);
50532c23cb7cSEd Maste 				p++;
50542c23cb7cSEd Maste 			}
50552c23cb7cSEd Maste 
50562c23cb7cSEd Maste 
50572c23cb7cSEd Maste 		}
50582c23cb7cSEd Maste 	}
50592c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
50602c23cb7cSEd Maste 		warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
50612c23cb7cSEd Maste 
50622c23cb7cSEd Maste #undef	RESET_REGISTERS
50632c23cb7cSEd Maste #undef	LINE
50642c23cb7cSEd Maste #undef	ADDRESS
50652c23cb7cSEd Maste }
50662c23cb7cSEd Maste 
50672c23cb7cSEd Maste static void
50682c23cb7cSEd Maste dump_dwarf_line_decoded(struct readelf *re)
50692c23cb7cSEd Maste {
50702c23cb7cSEd Maste 	Dwarf_Die die;
50712c23cb7cSEd Maste 	Dwarf_Line *linebuf, ln;
50722c23cb7cSEd Maste 	Dwarf_Addr lineaddr;
50732c23cb7cSEd Maste 	Dwarf_Signed linecount, srccount;
50742c23cb7cSEd Maste 	Dwarf_Unsigned lineno, fn;
50752c23cb7cSEd Maste 	Dwarf_Error de;
50762c23cb7cSEd Maste 	const char *dir, *file;
50772c23cb7cSEd Maste 	char **srcfiles;
50782c23cb7cSEd Maste 	int i, ret;
50792c23cb7cSEd Maste 
50802c23cb7cSEd Maste 	printf("Decoded dump of debug contents of section .debug_line:\n\n");
50812c23cb7cSEd Maste 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
50822c23cb7cSEd Maste 	    NULL, &de)) == DW_DLV_OK) {
50832c23cb7cSEd Maste 		if (dwarf_siblingof(re->dbg, NULL, &die, &de) != DW_DLV_OK)
50842c23cb7cSEd Maste 			continue;
50852c23cb7cSEd Maste 		if (dwarf_attrval_string(die, DW_AT_name, &file, &de) !=
50862c23cb7cSEd Maste 		    DW_DLV_OK)
50872c23cb7cSEd Maste 			file = NULL;
50882c23cb7cSEd Maste 		if (dwarf_attrval_string(die, DW_AT_comp_dir, &dir, &de) !=
50892c23cb7cSEd Maste 		    DW_DLV_OK)
50902c23cb7cSEd Maste 			dir = NULL;
50912c23cb7cSEd Maste 		printf("CU: ");
509265caaa0eSMaxim Sobolev 		if (dir && file && file[0] != '/')
50932c23cb7cSEd Maste 			printf("%s/", dir);
50942c23cb7cSEd Maste 		if (file)
50952c23cb7cSEd Maste 			printf("%s", file);
50962c23cb7cSEd Maste 		putchar('\n');
50972c23cb7cSEd Maste 		printf("%-37s %11s   %s\n", "Filename", "Line Number",
50982c23cb7cSEd Maste 		    "Starting Address");
50992c23cb7cSEd Maste 		if (dwarf_srclines(die, &linebuf, &linecount, &de) != DW_DLV_OK)
51002c23cb7cSEd Maste 			continue;
51012c23cb7cSEd Maste 		if (dwarf_srcfiles(die, &srcfiles, &srccount, &de) != DW_DLV_OK)
51022c23cb7cSEd Maste 			continue;
51032c23cb7cSEd Maste 		for (i = 0; i < linecount; i++) {
51042c23cb7cSEd Maste 			ln = linebuf[i];
51052c23cb7cSEd Maste 			if (dwarf_line_srcfileno(ln, &fn, &de) != DW_DLV_OK)
51062c23cb7cSEd Maste 				continue;
51072c23cb7cSEd Maste 			if (dwarf_lineno(ln, &lineno, &de) != DW_DLV_OK)
51082c23cb7cSEd Maste 				continue;
51092c23cb7cSEd Maste 			if (dwarf_lineaddr(ln, &lineaddr, &de) != DW_DLV_OK)
51102c23cb7cSEd Maste 				continue;
51112c23cb7cSEd Maste 			printf("%-37s %11ju %#18jx\n",
51122c23cb7cSEd Maste 			    basename(srcfiles[fn - 1]), (uintmax_t) lineno,
51132c23cb7cSEd Maste 			    (uintmax_t) lineaddr);
51142c23cb7cSEd Maste 		}
51152c23cb7cSEd Maste 		putchar('\n');
51162c23cb7cSEd Maste 	}
51172c23cb7cSEd Maste }
51182c23cb7cSEd Maste 
51192c23cb7cSEd Maste static void
51202c23cb7cSEd Maste dump_dwarf_die(struct readelf *re, Dwarf_Die die, int level)
51212c23cb7cSEd Maste {
51222c23cb7cSEd Maste 	Dwarf_Attribute *attr_list;
51232c23cb7cSEd Maste 	Dwarf_Die ret_die;
5124cf781b2eSEd Maste 	Dwarf_Off dieoff, cuoff, culen, attroff;
5125cf781b2eSEd Maste 	Dwarf_Unsigned ate, lang, v_udata, v_sig;
51262c23cb7cSEd Maste 	Dwarf_Signed attr_count, v_sdata;
51272c23cb7cSEd Maste 	Dwarf_Off v_off;
51282c23cb7cSEd Maste 	Dwarf_Addr v_addr;
51292c23cb7cSEd Maste 	Dwarf_Half tag, attr, form;
51302c23cb7cSEd Maste 	Dwarf_Block *v_block;
5131cf781b2eSEd Maste 	Dwarf_Bool v_bool, is_info;
5132cf781b2eSEd Maste 	Dwarf_Sig8 v_sig8;
51332c23cb7cSEd Maste 	Dwarf_Error de;
5134cf781b2eSEd Maste 	Dwarf_Ptr v_expr;
5135cf781b2eSEd Maste 	const char *tag_str, *attr_str, *ate_str, *lang_str;
5136cf781b2eSEd Maste 	char unk_tag[32], unk_attr[32];
51372c23cb7cSEd Maste 	char *v_str;
5138cf781b2eSEd Maste 	uint8_t *b, *p;
51392c23cb7cSEd Maste 	int i, j, abc, ret;
51402c23cb7cSEd Maste 
51412c23cb7cSEd Maste 	if (dwarf_dieoffset(die, &dieoff, &de) != DW_DLV_OK) {
51422c23cb7cSEd Maste 		warnx("dwarf_dieoffset failed: %s", dwarf_errmsg(de));
51432c23cb7cSEd Maste 		goto cont_search;
51442c23cb7cSEd Maste 	}
51452c23cb7cSEd Maste 
51462c23cb7cSEd Maste 	printf(" <%d><%jx>: ", level, (uintmax_t) dieoff);
51472c23cb7cSEd Maste 
51482c23cb7cSEd Maste 	if (dwarf_die_CU_offset_range(die, &cuoff, &culen, &de) != DW_DLV_OK) {
51492c23cb7cSEd Maste 		warnx("dwarf_die_CU_offset_range failed: %s",
51502c23cb7cSEd Maste 		      dwarf_errmsg(de));
51512c23cb7cSEd Maste 		cuoff = 0;
51522c23cb7cSEd Maste 	}
51532c23cb7cSEd Maste 
51542c23cb7cSEd Maste 	abc = dwarf_die_abbrev_code(die);
51552c23cb7cSEd Maste 	if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
51562c23cb7cSEd Maste 		warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
51572c23cb7cSEd Maste 		goto cont_search;
51582c23cb7cSEd Maste 	}
51592c23cb7cSEd Maste 	if (dwarf_get_TAG_name(tag, &tag_str) != DW_DLV_OK) {
5160cf781b2eSEd Maste 		snprintf(unk_tag, sizeof(unk_tag), "[Unknown Tag: %#x]", tag);
5161cf781b2eSEd Maste 		tag_str = unk_tag;
51622c23cb7cSEd Maste 	}
51632c23cb7cSEd Maste 
51642c23cb7cSEd Maste 	printf("Abbrev Number: %d (%s)\n", abc, tag_str);
51652c23cb7cSEd Maste 
51662c23cb7cSEd Maste 	if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
51672c23cb7cSEd Maste 	    DW_DLV_OK) {
51682c23cb7cSEd Maste 		if (ret == DW_DLV_ERROR)
51692c23cb7cSEd Maste 			warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
51702c23cb7cSEd Maste 		goto cont_search;
51712c23cb7cSEd Maste 	}
51722c23cb7cSEd Maste 
51732c23cb7cSEd Maste 	for (i = 0; i < attr_count; i++) {
51742c23cb7cSEd Maste 		if (dwarf_whatform(attr_list[i], &form, &de) != DW_DLV_OK) {
51752c23cb7cSEd Maste 			warnx("dwarf_whatform failed: %s", dwarf_errmsg(de));
51762c23cb7cSEd Maste 			continue;
51772c23cb7cSEd Maste 		}
51782c23cb7cSEd Maste 		if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
51792c23cb7cSEd Maste 			warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
51802c23cb7cSEd Maste 			continue;
51812c23cb7cSEd Maste 		}
51822c23cb7cSEd Maste 		if (dwarf_get_AT_name(attr, &attr_str) != DW_DLV_OK) {
5183cf781b2eSEd Maste 			snprintf(unk_attr, sizeof(unk_attr),
5184cf781b2eSEd Maste 			    "[Unknown AT: %#x]", attr);
5185cf781b2eSEd Maste 			attr_str = unk_attr;
51862c23cb7cSEd Maste 		}
5187cf781b2eSEd Maste 		if (dwarf_attroffset(attr_list[i], &attroff, &de) !=
5188cf781b2eSEd Maste 		    DW_DLV_OK) {
5189cf781b2eSEd Maste 			warnx("dwarf_attroffset failed: %s", dwarf_errmsg(de));
5190cf781b2eSEd Maste 			attroff = 0;
5191cf781b2eSEd Maste 		}
5192cf781b2eSEd Maste 		printf("    <%jx>   %-18s: ", (uintmax_t) attroff, attr_str);
51932c23cb7cSEd Maste 		switch (form) {
51942c23cb7cSEd Maste 		case DW_FORM_ref_addr:
5195cf781b2eSEd Maste 		case DW_FORM_sec_offset:
51962c23cb7cSEd Maste 			if (dwarf_global_formref(attr_list[i], &v_off, &de) !=
51972c23cb7cSEd Maste 			    DW_DLV_OK) {
51982c23cb7cSEd Maste 				warnx("dwarf_global_formref failed: %s",
51992c23cb7cSEd Maste 				    dwarf_errmsg(de));
52002c23cb7cSEd Maste 				continue;
52012c23cb7cSEd Maste 			}
5202cf781b2eSEd Maste 			if (form == DW_FORM_ref_addr)
5203cf781b2eSEd Maste 				printf("<0x%jx>", (uintmax_t) v_off);
5204cf781b2eSEd Maste 			else
5205cf781b2eSEd Maste 				printf("0x%jx", (uintmax_t) v_off);
52062c23cb7cSEd Maste 			break;
52072c23cb7cSEd Maste 
52082c23cb7cSEd Maste 		case DW_FORM_ref1:
52092c23cb7cSEd Maste 		case DW_FORM_ref2:
52102c23cb7cSEd Maste 		case DW_FORM_ref4:
52112c23cb7cSEd Maste 		case DW_FORM_ref8:
52122c23cb7cSEd Maste 		case DW_FORM_ref_udata:
52132c23cb7cSEd Maste 			if (dwarf_formref(attr_list[i], &v_off, &de) !=
52142c23cb7cSEd Maste 			    DW_DLV_OK) {
52152c23cb7cSEd Maste 				warnx("dwarf_formref failed: %s",
52162c23cb7cSEd Maste 				    dwarf_errmsg(de));
52172c23cb7cSEd Maste 				continue;
52182c23cb7cSEd Maste 			}
52192c23cb7cSEd Maste 			v_off += cuoff;
5220cf781b2eSEd Maste 			printf("<0x%jx>", (uintmax_t) v_off);
52212c23cb7cSEd Maste 			break;
52222c23cb7cSEd Maste 
52232c23cb7cSEd Maste 		case DW_FORM_addr:
52242c23cb7cSEd Maste 			if (dwarf_formaddr(attr_list[i], &v_addr, &de) !=
52252c23cb7cSEd Maste 			    DW_DLV_OK) {
52262c23cb7cSEd Maste 				warnx("dwarf_formaddr failed: %s",
52272c23cb7cSEd Maste 				    dwarf_errmsg(de));
52282c23cb7cSEd Maste 				continue;
52292c23cb7cSEd Maste 			}
52302c23cb7cSEd Maste 			printf("%#jx", (uintmax_t) v_addr);
52312c23cb7cSEd Maste 			break;
52322c23cb7cSEd Maste 
52332c23cb7cSEd Maste 		case DW_FORM_data1:
52342c23cb7cSEd Maste 		case DW_FORM_data2:
52352c23cb7cSEd Maste 		case DW_FORM_data4:
52362c23cb7cSEd Maste 		case DW_FORM_data8:
52372c23cb7cSEd Maste 		case DW_FORM_udata:
52382c23cb7cSEd Maste 			if (dwarf_formudata(attr_list[i], &v_udata, &de) !=
52392c23cb7cSEd Maste 			    DW_DLV_OK) {
52402c23cb7cSEd Maste 				warnx("dwarf_formudata failed: %s",
52412c23cb7cSEd Maste 				    dwarf_errmsg(de));
52422c23cb7cSEd Maste 				continue;
52432c23cb7cSEd Maste 			}
5244cf781b2eSEd Maste 			if (attr == DW_AT_high_pc)
5245cf781b2eSEd Maste 				printf("0x%jx", (uintmax_t) v_udata);
5246cf781b2eSEd Maste 			else
52472c23cb7cSEd Maste 				printf("%ju", (uintmax_t) v_udata);
52482c23cb7cSEd Maste 			break;
52492c23cb7cSEd Maste 
52502c23cb7cSEd Maste 		case DW_FORM_sdata:
52512c23cb7cSEd Maste 			if (dwarf_formsdata(attr_list[i], &v_sdata, &de) !=
52522c23cb7cSEd Maste 			    DW_DLV_OK) {
52532c23cb7cSEd Maste 				warnx("dwarf_formudata failed: %s",
52542c23cb7cSEd Maste 				    dwarf_errmsg(de));
52552c23cb7cSEd Maste 				continue;
52562c23cb7cSEd Maste 			}
52572c23cb7cSEd Maste 			printf("%jd", (intmax_t) v_sdata);
52582c23cb7cSEd Maste 			break;
52592c23cb7cSEd Maste 
52602c23cb7cSEd Maste 		case DW_FORM_flag:
52612c23cb7cSEd Maste 			if (dwarf_formflag(attr_list[i], &v_bool, &de) !=
52622c23cb7cSEd Maste 			    DW_DLV_OK) {
52632c23cb7cSEd Maste 				warnx("dwarf_formflag failed: %s",
52642c23cb7cSEd Maste 				    dwarf_errmsg(de));
52652c23cb7cSEd Maste 				continue;
52662c23cb7cSEd Maste 			}
52672c23cb7cSEd Maste 			printf("%jd", (intmax_t) v_bool);
52682c23cb7cSEd Maste 			break;
52692c23cb7cSEd Maste 
5270cf781b2eSEd Maste 		case DW_FORM_flag_present:
5271cf781b2eSEd Maste 			putchar('1');
5272cf781b2eSEd Maste 			break;
5273cf781b2eSEd Maste 
52742c23cb7cSEd Maste 		case DW_FORM_string:
52752c23cb7cSEd Maste 		case DW_FORM_strp:
52762c23cb7cSEd Maste 			if (dwarf_formstring(attr_list[i], &v_str, &de) !=
52772c23cb7cSEd Maste 			    DW_DLV_OK) {
52782c23cb7cSEd Maste 				warnx("dwarf_formstring failed: %s",
52792c23cb7cSEd Maste 				    dwarf_errmsg(de));
52802c23cb7cSEd Maste 				continue;
52812c23cb7cSEd Maste 			}
52822c23cb7cSEd Maste 			if (form == DW_FORM_string)
52832c23cb7cSEd Maste 				printf("%s", v_str);
52842c23cb7cSEd Maste 			else
52852c23cb7cSEd Maste 				printf("(indirect string) %s", v_str);
52862c23cb7cSEd Maste 			break;
52872c23cb7cSEd Maste 
52882c23cb7cSEd Maste 		case DW_FORM_block:
52892c23cb7cSEd Maste 		case DW_FORM_block1:
52902c23cb7cSEd Maste 		case DW_FORM_block2:
52912c23cb7cSEd Maste 		case DW_FORM_block4:
52922c23cb7cSEd Maste 			if (dwarf_formblock(attr_list[i], &v_block, &de) !=
52932c23cb7cSEd Maste 			    DW_DLV_OK) {
52942c23cb7cSEd Maste 				warnx("dwarf_formblock failed: %s",
52952c23cb7cSEd Maste 				    dwarf_errmsg(de));
52962c23cb7cSEd Maste 				continue;
52972c23cb7cSEd Maste 			}
5298cf781b2eSEd Maste 			printf("%ju byte block:", (uintmax_t) v_block->bl_len);
52992c23cb7cSEd Maste 			b = v_block->bl_data;
53002c23cb7cSEd Maste 			for (j = 0; (Dwarf_Unsigned) j < v_block->bl_len; j++)
53012c23cb7cSEd Maste 				printf(" %x", b[j]);
5302cf781b2eSEd Maste 			printf("\t(");
5303cf781b2eSEd Maste 			dump_dwarf_block(re, v_block->bl_data, v_block->bl_len);
5304cf781b2eSEd Maste 			putchar(')');
53052c23cb7cSEd Maste 			break;
5306cf781b2eSEd Maste 
5307cf781b2eSEd Maste 		case DW_FORM_exprloc:
5308cf781b2eSEd Maste 			if (dwarf_formexprloc(attr_list[i], &v_udata, &v_expr,
5309cf781b2eSEd Maste 			    &de) != DW_DLV_OK) {
5310cf781b2eSEd Maste 				warnx("dwarf_formexprloc failed: %s",
5311cf781b2eSEd Maste 				    dwarf_errmsg(de));
5312cf781b2eSEd Maste 				continue;
5313cf781b2eSEd Maste 			}
5314cf781b2eSEd Maste 			printf("%ju byte block:", (uintmax_t) v_udata);
5315cf781b2eSEd Maste 			b = v_expr;
5316cf781b2eSEd Maste 			for (j = 0; (Dwarf_Unsigned) j < v_udata; j++)
5317cf781b2eSEd Maste 				printf(" %x", b[j]);
5318cf781b2eSEd Maste 			printf("\t(");
5319cf781b2eSEd Maste 			dump_dwarf_block(re, v_expr, v_udata);
5320cf781b2eSEd Maste 			putchar(')');
5321cf781b2eSEd Maste 			break;
5322cf781b2eSEd Maste 
5323cf781b2eSEd Maste 		case DW_FORM_ref_sig8:
5324cf781b2eSEd Maste 			if (dwarf_formsig8(attr_list[i], &v_sig8, &de) !=
5325cf781b2eSEd Maste 			    DW_DLV_OK) {
5326cf781b2eSEd Maste 				warnx("dwarf_formsig8 failed: %s",
5327cf781b2eSEd Maste 				    dwarf_errmsg(de));
5328cf781b2eSEd Maste 				continue;
5329cf781b2eSEd Maste 			}
5330cf781b2eSEd Maste 			p = (uint8_t *)(uintptr_t) &v_sig8.signature[0];
5331cf781b2eSEd Maste 			v_sig = re->dw_decode(&p, 8);
5332cf781b2eSEd Maste 			printf("signature: 0x%jx", (uintmax_t) v_sig);
53332c23cb7cSEd Maste 		}
53342c23cb7cSEd Maste 		switch (attr) {
53352c23cb7cSEd Maste 		case DW_AT_encoding:
53362c23cb7cSEd Maste 			if (dwarf_attrval_unsigned(die, attr, &ate, &de) !=
53372c23cb7cSEd Maste 			    DW_DLV_OK)
53382c23cb7cSEd Maste 				break;
53392c23cb7cSEd Maste 			if (dwarf_get_ATE_name(ate, &ate_str) != DW_DLV_OK)
5340cf781b2eSEd Maste 				ate_str = "DW_ATE_UNKNOWN";
53412c23cb7cSEd Maste 			printf("\t(%s)", &ate_str[strlen("DW_ATE_")]);
53422c23cb7cSEd Maste 			break;
5343cf781b2eSEd Maste 
5344cf781b2eSEd Maste 		case DW_AT_language:
5345cf781b2eSEd Maste 			if (dwarf_attrval_unsigned(die, attr, &lang, &de) !=
5346cf781b2eSEd Maste 			    DW_DLV_OK)
5347cf781b2eSEd Maste 				break;
5348cf781b2eSEd Maste 			if (dwarf_get_LANG_name(lang, &lang_str) != DW_DLV_OK)
5349cf781b2eSEd Maste 				break;
5350cf781b2eSEd Maste 			printf("\t(%s)", &lang_str[strlen("DW_LANG_")]);
5351cf781b2eSEd Maste 			break;
5352cf781b2eSEd Maste 
5353cf781b2eSEd Maste 		case DW_AT_location:
5354cf781b2eSEd Maste 		case DW_AT_string_length:
5355cf781b2eSEd Maste 		case DW_AT_return_addr:
5356cf781b2eSEd Maste 		case DW_AT_data_member_location:
5357cf781b2eSEd Maste 		case DW_AT_frame_base:
5358cf781b2eSEd Maste 		case DW_AT_segment:
5359cf781b2eSEd Maste 		case DW_AT_static_link:
5360cf781b2eSEd Maste 		case DW_AT_use_location:
5361cf781b2eSEd Maste 		case DW_AT_vtable_elem_location:
5362cf781b2eSEd Maste 			switch (form) {
5363cf781b2eSEd Maste 			case DW_FORM_data4:
5364cf781b2eSEd Maste 			case DW_FORM_data8:
5365cf781b2eSEd Maste 			case DW_FORM_sec_offset:
5366cf781b2eSEd Maste 				printf("\t(location list)");
5367cf781b2eSEd Maste 				break;
5368cf781b2eSEd Maste 			default:
5369cf781b2eSEd Maste 				break;
5370cf781b2eSEd Maste 			}
5371cf781b2eSEd Maste 
53722c23cb7cSEd Maste 		default:
53732c23cb7cSEd Maste 			break;
53742c23cb7cSEd Maste 		}
53752c23cb7cSEd Maste 		putchar('\n');
53762c23cb7cSEd Maste 	}
53772c23cb7cSEd Maste 
53782c23cb7cSEd Maste 
53792c23cb7cSEd Maste cont_search:
53802c23cb7cSEd Maste 	/* Search children. */
53812c23cb7cSEd Maste 	ret = dwarf_child(die, &ret_die, &de);
53822c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
53832c23cb7cSEd Maste 		warnx("dwarf_child: %s", dwarf_errmsg(de));
53842c23cb7cSEd Maste 	else if (ret == DW_DLV_OK)
53852c23cb7cSEd Maste 		dump_dwarf_die(re, ret_die, level + 1);
53862c23cb7cSEd Maste 
53872c23cb7cSEd Maste 	/* Search sibling. */
5388cf781b2eSEd Maste 	is_info = dwarf_get_die_infotypes_flag(die);
5389cf781b2eSEd Maste 	ret = dwarf_siblingof_b(re->dbg, die, &ret_die, is_info, &de);
53902c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
53912c23cb7cSEd Maste 		warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
53922c23cb7cSEd Maste 	else if (ret == DW_DLV_OK)
53932c23cb7cSEd Maste 		dump_dwarf_die(re, ret_die, level);
53942c23cb7cSEd Maste 
53952c23cb7cSEd Maste 	dwarf_dealloc(re->dbg, die, DW_DLA_DIE);
53962c23cb7cSEd Maste }
53972c23cb7cSEd Maste 
53982c23cb7cSEd Maste static void
5399cf781b2eSEd Maste set_cu_context(struct readelf *re, Dwarf_Half psize, Dwarf_Half osize,
5400cf781b2eSEd Maste     Dwarf_Half ver)
5401cf781b2eSEd Maste {
5402cf781b2eSEd Maste 
5403cf781b2eSEd Maste 	re->cu_psize = psize;
5404cf781b2eSEd Maste 	re->cu_osize = osize;
5405cf781b2eSEd Maste 	re->cu_ver = ver;
5406cf781b2eSEd Maste }
5407cf781b2eSEd Maste 
5408cf781b2eSEd Maste static void
5409cf781b2eSEd Maste dump_dwarf_info(struct readelf *re, Dwarf_Bool is_info)
54102c23cb7cSEd Maste {
54112c23cb7cSEd Maste 	struct section *s;
54122c23cb7cSEd Maste 	Dwarf_Die die;
54132c23cb7cSEd Maste 	Dwarf_Error de;
5414cf781b2eSEd Maste 	Dwarf_Half tag, version, pointer_size, off_size;
54152c23cb7cSEd Maste 	Dwarf_Off cu_offset, cu_length;
54162c23cb7cSEd Maste 	Dwarf_Off aboff;
5417cf781b2eSEd Maste 	Dwarf_Unsigned typeoff;
5418cf781b2eSEd Maste 	Dwarf_Sig8 sig8;
5419cf781b2eSEd Maste 	Dwarf_Unsigned sig;
5420cf781b2eSEd Maste 	uint8_t *p;
5421cf781b2eSEd Maste 	const char *sn;
5422cf781b2eSEd Maste 	int i, ret;
54232c23cb7cSEd Maste 
5424cf781b2eSEd Maste 	sn = is_info ? ".debug_info" : ".debug_types";
54252c23cb7cSEd Maste 
54262c23cb7cSEd Maste 	s = NULL;
54272c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
54282c23cb7cSEd Maste 		s = &re->sl[i];
5429cf781b2eSEd Maste 		if (s->name != NULL && !strcmp(s->name, sn))
54302c23cb7cSEd Maste 			break;
54312c23cb7cSEd Maste 	}
54322c23cb7cSEd Maste 	if ((size_t) i >= re->shnum)
54332c23cb7cSEd Maste 		return;
54342c23cb7cSEd Maste 
5435cf781b2eSEd Maste 	do {
5436cf781b2eSEd Maste 		printf("\nDump of debug contents of section %s:\n", sn);
54372c23cb7cSEd Maste 
5438cf781b2eSEd Maste 		while ((ret = dwarf_next_cu_header_c(re->dbg, is_info, NULL,
5439cf781b2eSEd Maste 		    &version, &aboff, &pointer_size, &off_size, NULL, &sig8,
5440cf781b2eSEd Maste 		    &typeoff, NULL, &de)) == DW_DLV_OK) {
5441cf781b2eSEd Maste 			set_cu_context(re, pointer_size, off_size, version);
54422c23cb7cSEd Maste 			die = NULL;
5443cf781b2eSEd Maste 			while (dwarf_siblingof_b(re->dbg, die, &die, is_info,
5444cf781b2eSEd Maste 			    &de) == DW_DLV_OK) {
54452c23cb7cSEd Maste 				if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
54462c23cb7cSEd Maste 					warnx("dwarf_tag failed: %s",
54472c23cb7cSEd Maste 					    dwarf_errmsg(de));
5448cf781b2eSEd Maste 					continue;
54492c23cb7cSEd Maste 				}
54502c23cb7cSEd Maste 				/* XXX: What about DW_TAG_partial_unit? */
5451cf781b2eSEd Maste 				if ((is_info && tag == DW_TAG_compile_unit) ||
5452cf781b2eSEd Maste 				    (!is_info && tag == DW_TAG_type_unit))
54532c23cb7cSEd Maste 					break;
54542c23cb7cSEd Maste 			}
5455cf781b2eSEd Maste 			if (die == NULL && is_info) {
5456cf781b2eSEd Maste 				warnx("could not find DW_TAG_compile_unit "
5457cf781b2eSEd Maste 				    "die");
5458cf781b2eSEd Maste 				continue;
5459cf781b2eSEd Maste 			} else if (die == NULL && !is_info) {
5460cf781b2eSEd Maste 				warnx("could not find DW_TAG_type_unit die");
5461cf781b2eSEd Maste 				continue;
54622c23cb7cSEd Maste 			}
54632c23cb7cSEd Maste 
5464cf781b2eSEd Maste 			if (dwarf_die_CU_offset_range(die, &cu_offset,
5465cf781b2eSEd Maste 			    &cu_length, &de) != DW_DLV_OK) {
54662c23cb7cSEd Maste 				warnx("dwarf_die_CU_offset failed: %s",
54672c23cb7cSEd Maste 				    dwarf_errmsg(de));
54682c23cb7cSEd Maste 				continue;
54692c23cb7cSEd Maste 			}
54702c23cb7cSEd Maste 
5471cf781b2eSEd Maste 			cu_length -= off_size == 4 ? 4 : 12;
5472cf781b2eSEd Maste 
5473cf781b2eSEd Maste 			sig = 0;
5474cf781b2eSEd Maste 			if (!is_info) {
5475cf781b2eSEd Maste 				p = (uint8_t *)(uintptr_t) &sig8.signature[0];
5476cf781b2eSEd Maste 				sig = re->dw_decode(&p, 8);
5477cf781b2eSEd Maste 			}
5478cf781b2eSEd Maste 
5479cf781b2eSEd Maste 			printf("\n  Type Unit @ offset 0x%jx:\n",
5480cf781b2eSEd Maste 			    (uintmax_t) cu_offset);
5481cf781b2eSEd Maste 			printf("    Length:\t\t%#jx (%d-bit)\n",
5482cf781b2eSEd Maste 			    (uintmax_t) cu_length, off_size == 4 ? 32 : 64);
54832c23cb7cSEd Maste 			printf("    Version:\t\t%u\n", version);
5484cf781b2eSEd Maste 			printf("    Abbrev Offset:\t0x%jx\n",
5485cf781b2eSEd Maste 			    (uintmax_t) aboff);
54862c23cb7cSEd Maste 			printf("    Pointer Size:\t%u\n", pointer_size);
5487cf781b2eSEd Maste 			if (!is_info) {
5488cf781b2eSEd Maste 				printf("    Signature:\t\t0x%016jx\n",
5489cf781b2eSEd Maste 				    (uintmax_t) sig);
5490cf781b2eSEd Maste 				printf("    Type Offset:\t0x%jx\n",
5491cf781b2eSEd Maste 				    (uintmax_t) typeoff);
5492cf781b2eSEd Maste 			}
54932c23cb7cSEd Maste 
54942c23cb7cSEd Maste 			dump_dwarf_die(re, die, 0);
54952c23cb7cSEd Maste 		}
54962c23cb7cSEd Maste 		if (ret == DW_DLV_ERROR)
54972c23cb7cSEd Maste 			warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
5498cf781b2eSEd Maste 		if (is_info)
5499cf781b2eSEd Maste 			break;
5500cf781b2eSEd Maste 	} while (dwarf_next_types_section(re->dbg, &de) == DW_DLV_OK);
55012c23cb7cSEd Maste }
55022c23cb7cSEd Maste 
55032c23cb7cSEd Maste static void
55042c23cb7cSEd Maste dump_dwarf_abbrev(struct readelf *re)
55052c23cb7cSEd Maste {
55062c23cb7cSEd Maste 	Dwarf_Abbrev ab;
55072c23cb7cSEd Maste 	Dwarf_Off aboff, atoff;
55082c23cb7cSEd Maste 	Dwarf_Unsigned length, attr_count;
55092c23cb7cSEd Maste 	Dwarf_Signed flag, form;
55102c23cb7cSEd Maste 	Dwarf_Half tag, attr;
55112c23cb7cSEd Maste 	Dwarf_Error de;
55122c23cb7cSEd Maste 	const char *tag_str, *attr_str, *form_str;
5513cf781b2eSEd Maste 	char unk_tag[32], unk_attr[32], unk_form[32];
55142c23cb7cSEd Maste 	int i, j, ret;
55152c23cb7cSEd Maste 
55162c23cb7cSEd Maste 	printf("\nContents of section .debug_abbrev:\n\n");
55172c23cb7cSEd Maste 
55182c23cb7cSEd Maste 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, &aboff,
55192c23cb7cSEd Maste 	    NULL, NULL, &de)) ==  DW_DLV_OK) {
55202c23cb7cSEd Maste 		printf("  Number TAG\n");
55212c23cb7cSEd Maste 		i = 0;
55222c23cb7cSEd Maste 		while ((ret = dwarf_get_abbrev(re->dbg, aboff, &ab, &length,
55232c23cb7cSEd Maste 		    &attr_count, &de)) == DW_DLV_OK) {
55242c23cb7cSEd Maste 			if (length == 1) {
55252c23cb7cSEd Maste 				dwarf_dealloc(re->dbg, ab, DW_DLA_ABBREV);
55262c23cb7cSEd Maste 				break;
55272c23cb7cSEd Maste 			}
55282c23cb7cSEd Maste 			aboff += length;
55292c23cb7cSEd Maste 			printf("%4d", ++i);
55302c23cb7cSEd Maste 			if (dwarf_get_abbrev_tag(ab, &tag, &de) != DW_DLV_OK) {
55312c23cb7cSEd Maste 				warnx("dwarf_get_abbrev_tag failed: %s",
55322c23cb7cSEd Maste 				    dwarf_errmsg(de));
55332c23cb7cSEd Maste 				goto next_abbrev;
55342c23cb7cSEd Maste 			}
55352c23cb7cSEd Maste 			if (dwarf_get_TAG_name(tag, &tag_str) != DW_DLV_OK) {
5536cf781b2eSEd Maste 				snprintf(unk_tag, sizeof(unk_tag),
5537cf781b2eSEd Maste 				    "[Unknown Tag: %#x]", tag);
5538cf781b2eSEd Maste 				tag_str = unk_tag;
55392c23cb7cSEd Maste 			}
55402c23cb7cSEd Maste 			if (dwarf_get_abbrev_children_flag(ab, &flag, &de) !=
55412c23cb7cSEd Maste 			    DW_DLV_OK) {
55422c23cb7cSEd Maste 				warnx("dwarf_get_abbrev_children_flag failed:"
55432c23cb7cSEd Maste 				    " %s", dwarf_errmsg(de));
55442c23cb7cSEd Maste 				goto next_abbrev;
55452c23cb7cSEd Maste 			}
55462c23cb7cSEd Maste 			printf("      %s    %s\n", tag_str,
55472c23cb7cSEd Maste 			    flag ? "[has children]" : "[no children]");
55482c23cb7cSEd Maste 			for (j = 0; (Dwarf_Unsigned) j < attr_count; j++) {
55492c23cb7cSEd Maste 				if (dwarf_get_abbrev_entry(ab, (Dwarf_Signed) j,
55502c23cb7cSEd Maste 				    &attr, &form, &atoff, &de) != DW_DLV_OK) {
55512c23cb7cSEd Maste 					warnx("dwarf_get_abbrev_entry failed:"
55522c23cb7cSEd Maste 					    " %s", dwarf_errmsg(de));
55532c23cb7cSEd Maste 					continue;
55542c23cb7cSEd Maste 				}
55552c23cb7cSEd Maste 				if (dwarf_get_AT_name(attr, &attr_str) !=
55562c23cb7cSEd Maste 				    DW_DLV_OK) {
5557cf781b2eSEd Maste 					snprintf(unk_attr, sizeof(unk_attr),
5558cf781b2eSEd Maste 					    "[Unknown AT: %#x]", attr);
5559cf781b2eSEd Maste 					attr_str = unk_attr;
55602c23cb7cSEd Maste 				}
55612c23cb7cSEd Maste 				if (dwarf_get_FORM_name(form, &form_str) !=
55622c23cb7cSEd Maste 				    DW_DLV_OK) {
5563cf781b2eSEd Maste 					snprintf(unk_form, sizeof(unk_form),
5564cf781b2eSEd Maste 					    "[Unknown Form: %#x]",
5565cf781b2eSEd Maste 					    (Dwarf_Half) form);
5566cf781b2eSEd Maste 					form_str = unk_form;
55672c23cb7cSEd Maste 				}
55682c23cb7cSEd Maste 				printf("    %-18s %s\n", attr_str, form_str);
55692c23cb7cSEd Maste 			}
55702c23cb7cSEd Maste 		next_abbrev:
55712c23cb7cSEd Maste 			dwarf_dealloc(re->dbg, ab, DW_DLA_ABBREV);
55722c23cb7cSEd Maste 		}
55732c23cb7cSEd Maste 		if (ret != DW_DLV_OK)
55742c23cb7cSEd Maste 			warnx("dwarf_get_abbrev: %s", dwarf_errmsg(de));
55752c23cb7cSEd Maste 	}
55762c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
55772c23cb7cSEd Maste 		warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
55782c23cb7cSEd Maste }
55792c23cb7cSEd Maste 
55802c23cb7cSEd Maste static void
55812c23cb7cSEd Maste dump_dwarf_pubnames(struct readelf *re)
55822c23cb7cSEd Maste {
55832c23cb7cSEd Maste 	struct section *s;
55842c23cb7cSEd Maste 	Dwarf_Off die_off;
55852c23cb7cSEd Maste 	Dwarf_Unsigned offset, length, nt_cu_offset, nt_cu_length;
55862c23cb7cSEd Maste 	Dwarf_Signed cnt;
55872c23cb7cSEd Maste 	Dwarf_Global *globs;
55882c23cb7cSEd Maste 	Dwarf_Half nt_version;
55892c23cb7cSEd Maste 	Dwarf_Error de;
55902c23cb7cSEd Maste 	Elf_Data *d;
55912c23cb7cSEd Maste 	char *glob_name;
55922c23cb7cSEd Maste 	int i, dwarf_size, elferr;
55932c23cb7cSEd Maste 
55942c23cb7cSEd Maste 	printf("\nContents of the .debug_pubnames section:\n");
55952c23cb7cSEd Maste 
55962c23cb7cSEd Maste 	s = NULL;
55972c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
55982c23cb7cSEd Maste 		s = &re->sl[i];
55992c23cb7cSEd Maste 		if (s->name != NULL && !strcmp(s->name, ".debug_pubnames"))
56002c23cb7cSEd Maste 			break;
56012c23cb7cSEd Maste 	}
56022c23cb7cSEd Maste 	if ((size_t) i >= re->shnum)
56032c23cb7cSEd Maste 		return;
56042c23cb7cSEd Maste 
56052c23cb7cSEd Maste 	(void) elf_errno();
56062c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
56072c23cb7cSEd Maste 		elferr = elf_errno();
56082c23cb7cSEd Maste 		if (elferr != 0)
56092c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
56102c23cb7cSEd Maste 		return;
56112c23cb7cSEd Maste 	}
56122c23cb7cSEd Maste 	if (d->d_size <= 0)
56132c23cb7cSEd Maste 		return;
56142c23cb7cSEd Maste 
56152c23cb7cSEd Maste 	/* Read in .debug_pubnames section table header. */
56162c23cb7cSEd Maste 	offset = 0;
56172c23cb7cSEd Maste 	length = re->dw_read(d, &offset, 4);
56182c23cb7cSEd Maste 	if (length == 0xffffffff) {
56192c23cb7cSEd Maste 		dwarf_size = 8;
56202c23cb7cSEd Maste 		length = re->dw_read(d, &offset, 8);
56212c23cb7cSEd Maste 	} else
56222c23cb7cSEd Maste 		dwarf_size = 4;
56232c23cb7cSEd Maste 
56242c23cb7cSEd Maste 	if (length > d->d_size - offset) {
56252c23cb7cSEd Maste 		warnx("invalid .dwarf_pubnames section");
56262c23cb7cSEd Maste 		return;
56272c23cb7cSEd Maste 	}
56282c23cb7cSEd Maste 
56292c23cb7cSEd Maste 	nt_version = re->dw_read(d, &offset, 2);
56302c23cb7cSEd Maste 	nt_cu_offset = re->dw_read(d, &offset, dwarf_size);
56312c23cb7cSEd Maste 	nt_cu_length = re->dw_read(d, &offset, dwarf_size);
56322c23cb7cSEd Maste 	printf("  Length:\t\t\t\t%ju\n", (uintmax_t) length);
56332c23cb7cSEd Maste 	printf("  Version:\t\t\t\t%u\n", nt_version);
56342c23cb7cSEd Maste 	printf("  Offset into .debug_info section:\t%ju\n",
56352c23cb7cSEd Maste 	    (uintmax_t) nt_cu_offset);
56362c23cb7cSEd Maste 	printf("  Size of area in .debug_info section:\t%ju\n",
56372c23cb7cSEd Maste 	    (uintmax_t) nt_cu_length);
56382c23cb7cSEd Maste 
56392c23cb7cSEd Maste 	if (dwarf_get_globals(re->dbg, &globs, &cnt, &de) != DW_DLV_OK) {
56402c23cb7cSEd Maste 		warnx("dwarf_get_globals failed: %s", dwarf_errmsg(de));
56412c23cb7cSEd Maste 		return;
56422c23cb7cSEd Maste 	}
56432c23cb7cSEd Maste 
56442c23cb7cSEd Maste 	printf("\n    Offset      Name\n");
56452c23cb7cSEd Maste 	for (i = 0; i < cnt; i++) {
56462c23cb7cSEd Maste 		if (dwarf_globname(globs[i], &glob_name, &de) != DW_DLV_OK) {
56472c23cb7cSEd Maste 			warnx("dwarf_globname failed: %s", dwarf_errmsg(de));
56482c23cb7cSEd Maste 			continue;
56492c23cb7cSEd Maste 		}
56502c23cb7cSEd Maste 		if (dwarf_global_die_offset(globs[i], &die_off, &de) !=
56512c23cb7cSEd Maste 		    DW_DLV_OK) {
56522c23cb7cSEd Maste 			warnx("dwarf_global_die_offset failed: %s",
56532c23cb7cSEd Maste 			    dwarf_errmsg(de));
56542c23cb7cSEd Maste 			continue;
56552c23cb7cSEd Maste 		}
56562c23cb7cSEd Maste 		printf("    %-11ju %s\n", (uintmax_t) die_off, glob_name);
56572c23cb7cSEd Maste 	}
56582c23cb7cSEd Maste }
56592c23cb7cSEd Maste 
56602c23cb7cSEd Maste static void
56612c23cb7cSEd Maste dump_dwarf_aranges(struct readelf *re)
56622c23cb7cSEd Maste {
56632c23cb7cSEd Maste 	struct section *s;
56642c23cb7cSEd Maste 	Dwarf_Arange *aranges;
56652c23cb7cSEd Maste 	Dwarf_Addr start;
56662c23cb7cSEd Maste 	Dwarf_Unsigned offset, length, as_cu_offset;
56672c23cb7cSEd Maste 	Dwarf_Off die_off;
56682c23cb7cSEd Maste 	Dwarf_Signed cnt;
56692c23cb7cSEd Maste 	Dwarf_Half as_version, as_addrsz, as_segsz;
56702c23cb7cSEd Maste 	Dwarf_Error de;
56712c23cb7cSEd Maste 	Elf_Data *d;
56722c23cb7cSEd Maste 	int i, dwarf_size, elferr;
56732c23cb7cSEd Maste 
56742c23cb7cSEd Maste 	printf("\nContents of section .debug_aranges:\n");
56752c23cb7cSEd Maste 
56762c23cb7cSEd Maste 	s = NULL;
56772c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
56782c23cb7cSEd Maste 		s = &re->sl[i];
56792c23cb7cSEd Maste 		if (s->name != NULL && !strcmp(s->name, ".debug_aranges"))
56802c23cb7cSEd Maste 			break;
56812c23cb7cSEd Maste 	}
56822c23cb7cSEd Maste 	if ((size_t) i >= re->shnum)
56832c23cb7cSEd Maste 		return;
56842c23cb7cSEd Maste 
56852c23cb7cSEd Maste 	(void) elf_errno();
56862c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
56872c23cb7cSEd Maste 		elferr = elf_errno();
56882c23cb7cSEd Maste 		if (elferr != 0)
56892c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
56902c23cb7cSEd Maste 		return;
56912c23cb7cSEd Maste 	}
56922c23cb7cSEd Maste 	if (d->d_size <= 0)
56932c23cb7cSEd Maste 		return;
56942c23cb7cSEd Maste 
56952c23cb7cSEd Maste 	/* Read in the .debug_aranges section table header. */
56962c23cb7cSEd Maste 	offset = 0;
56972c23cb7cSEd Maste 	length = re->dw_read(d, &offset, 4);
56982c23cb7cSEd Maste 	if (length == 0xffffffff) {
56992c23cb7cSEd Maste 		dwarf_size = 8;
57002c23cb7cSEd Maste 		length = re->dw_read(d, &offset, 8);
57012c23cb7cSEd Maste 	} else
57022c23cb7cSEd Maste 		dwarf_size = 4;
57032c23cb7cSEd Maste 
57042c23cb7cSEd Maste 	if (length > d->d_size - offset) {
57052c23cb7cSEd Maste 		warnx("invalid .dwarf_aranges section");
57062c23cb7cSEd Maste 		return;
57072c23cb7cSEd Maste 	}
57082c23cb7cSEd Maste 
57092c23cb7cSEd Maste 	as_version = re->dw_read(d, &offset, 2);
57102c23cb7cSEd Maste 	as_cu_offset = re->dw_read(d, &offset, dwarf_size);
57112c23cb7cSEd Maste 	as_addrsz = re->dw_read(d, &offset, 1);
57122c23cb7cSEd Maste 	as_segsz = re->dw_read(d, &offset, 1);
57132c23cb7cSEd Maste 
57142c23cb7cSEd Maste 	printf("  Length:\t\t\t%ju\n", (uintmax_t) length);
57152c23cb7cSEd Maste 	printf("  Version:\t\t\t%u\n", as_version);
57162c23cb7cSEd Maste 	printf("  Offset into .debug_info:\t%ju\n", (uintmax_t) as_cu_offset);
57172c23cb7cSEd Maste 	printf("  Pointer Size:\t\t\t%u\n", as_addrsz);
57182c23cb7cSEd Maste 	printf("  Segment Size:\t\t\t%u\n", as_segsz);
57192c23cb7cSEd Maste 
57202c23cb7cSEd Maste 	if (dwarf_get_aranges(re->dbg, &aranges, &cnt, &de) != DW_DLV_OK) {
57212c23cb7cSEd Maste 		warnx("dwarf_get_aranges failed: %s", dwarf_errmsg(de));
57222c23cb7cSEd Maste 		return;
57232c23cb7cSEd Maste 	}
57242c23cb7cSEd Maste 
57252c23cb7cSEd Maste 	printf("\n    Address  Length\n");
57262c23cb7cSEd Maste 	for (i = 0; i < cnt; i++) {
57272c23cb7cSEd Maste 		if (dwarf_get_arange_info(aranges[i], &start, &length,
57282c23cb7cSEd Maste 		    &die_off, &de) != DW_DLV_OK) {
57292c23cb7cSEd Maste 			warnx("dwarf_get_arange_info failed: %s",
57302c23cb7cSEd Maste 			    dwarf_errmsg(de));
57312c23cb7cSEd Maste 			continue;
57322c23cb7cSEd Maste 		}
57332c23cb7cSEd Maste 		printf("    %08jx %ju\n", (uintmax_t) start,
57342c23cb7cSEd Maste 		    (uintmax_t) length);
57352c23cb7cSEd Maste 	}
57362c23cb7cSEd Maste }
57372c23cb7cSEd Maste 
57382c23cb7cSEd Maste static void
57392c23cb7cSEd Maste dump_dwarf_ranges_foreach(struct readelf *re, Dwarf_Die die, Dwarf_Addr base)
57402c23cb7cSEd Maste {
57412c23cb7cSEd Maste 	Dwarf_Attribute *attr_list;
57422c23cb7cSEd Maste 	Dwarf_Ranges *ranges;
57432c23cb7cSEd Maste 	Dwarf_Die ret_die;
57442c23cb7cSEd Maste 	Dwarf_Error de;
57452c23cb7cSEd Maste 	Dwarf_Addr base0;
57462c23cb7cSEd Maste 	Dwarf_Half attr;
57472c23cb7cSEd Maste 	Dwarf_Signed attr_count, cnt;
57482c23cb7cSEd Maste 	Dwarf_Unsigned off, bytecnt;
57492c23cb7cSEd Maste 	int i, j, ret;
57502c23cb7cSEd Maste 
57512c23cb7cSEd Maste 	if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
57522c23cb7cSEd Maste 	    DW_DLV_OK) {
57532c23cb7cSEd Maste 		if (ret == DW_DLV_ERROR)
57542c23cb7cSEd Maste 			warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
57552c23cb7cSEd Maste 		goto cont_search;
57562c23cb7cSEd Maste 	}
57572c23cb7cSEd Maste 
57582c23cb7cSEd Maste 	for (i = 0; i < attr_count; i++) {
57592c23cb7cSEd Maste 		if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
57602c23cb7cSEd Maste 			warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
57612c23cb7cSEd Maste 			continue;
57622c23cb7cSEd Maste 		}
57632c23cb7cSEd Maste 		if (attr != DW_AT_ranges)
57642c23cb7cSEd Maste 			continue;
57652c23cb7cSEd Maste 		if (dwarf_formudata(attr_list[i], &off, &de) != DW_DLV_OK) {
57662c23cb7cSEd Maste 			warnx("dwarf_formudata failed: %s", dwarf_errmsg(de));
57672c23cb7cSEd Maste 			continue;
57682c23cb7cSEd Maste 		}
57692c23cb7cSEd Maste 		if (dwarf_get_ranges(re->dbg, (Dwarf_Off) off, &ranges, &cnt,
57702c23cb7cSEd Maste 		    &bytecnt, &de) != DW_DLV_OK)
57712c23cb7cSEd Maste 			continue;
57722c23cb7cSEd Maste 		base0 = base;
57732c23cb7cSEd Maste 		for (j = 0; j < cnt; j++) {
57742c23cb7cSEd Maste 			printf("    %08jx ", (uintmax_t) off);
57752c23cb7cSEd Maste 			if (ranges[j].dwr_type == DW_RANGES_END) {
57762c23cb7cSEd Maste 				printf("%s\n", "<End of list>");
57772c23cb7cSEd Maste 				continue;
57782c23cb7cSEd Maste 			} else if (ranges[j].dwr_type ==
57792c23cb7cSEd Maste 			    DW_RANGES_ADDRESS_SELECTION) {
57802c23cb7cSEd Maste 				base0 = ranges[j].dwr_addr2;
57812c23cb7cSEd Maste 				continue;
57822c23cb7cSEd Maste 			}
57832c23cb7cSEd Maste 			if (re->ec == ELFCLASS32)
57842c23cb7cSEd Maste 				printf("%08jx %08jx\n",
5785839529caSEd Maste 				    (uintmax_t) (ranges[j].dwr_addr1 + base0),
5786839529caSEd Maste 				    (uintmax_t) (ranges[j].dwr_addr2 + base0));
57872c23cb7cSEd Maste 			else
57882c23cb7cSEd Maste 				printf("%016jx %016jx\n",
5789839529caSEd Maste 				    (uintmax_t) (ranges[j].dwr_addr1 + base0),
5790839529caSEd Maste 				    (uintmax_t) (ranges[j].dwr_addr2 + base0));
57912c23cb7cSEd Maste 		}
57922c23cb7cSEd Maste 	}
57932c23cb7cSEd Maste 
57942c23cb7cSEd Maste cont_search:
57952c23cb7cSEd Maste 	/* Search children. */
57962c23cb7cSEd Maste 	ret = dwarf_child(die, &ret_die, &de);
57972c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
57982c23cb7cSEd Maste 		warnx("dwarf_child: %s", dwarf_errmsg(de));
57992c23cb7cSEd Maste 	else if (ret == DW_DLV_OK)
58002c23cb7cSEd Maste 		dump_dwarf_ranges_foreach(re, ret_die, base);
58012c23cb7cSEd Maste 
58022c23cb7cSEd Maste 	/* Search sibling. */
58032c23cb7cSEd Maste 	ret = dwarf_siblingof(re->dbg, die, &ret_die, &de);
58042c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
58052c23cb7cSEd Maste 		warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
58062c23cb7cSEd Maste 	else if (ret == DW_DLV_OK)
58072c23cb7cSEd Maste 		dump_dwarf_ranges_foreach(re, ret_die, base);
58082c23cb7cSEd Maste }
58092c23cb7cSEd Maste 
58102c23cb7cSEd Maste static void
58112c23cb7cSEd Maste dump_dwarf_ranges(struct readelf *re)
58122c23cb7cSEd Maste {
58132c23cb7cSEd Maste 	Dwarf_Ranges *ranges;
58142c23cb7cSEd Maste 	Dwarf_Die die;
58152c23cb7cSEd Maste 	Dwarf_Signed cnt;
58162c23cb7cSEd Maste 	Dwarf_Unsigned bytecnt;
58172c23cb7cSEd Maste 	Dwarf_Half tag;
58182c23cb7cSEd Maste 	Dwarf_Error de;
58192c23cb7cSEd Maste 	Dwarf_Unsigned lowpc;
58202c23cb7cSEd Maste 	int ret;
58212c23cb7cSEd Maste 
58222c23cb7cSEd Maste 	if (dwarf_get_ranges(re->dbg, 0, &ranges, &cnt, &bytecnt, &de) !=
58232c23cb7cSEd Maste 	    DW_DLV_OK)
58242c23cb7cSEd Maste 		return;
58252c23cb7cSEd Maste 
58262c23cb7cSEd Maste 	printf("Contents of the .debug_ranges section:\n\n");
58272c23cb7cSEd Maste 	if (re->ec == ELFCLASS32)
58282c23cb7cSEd Maste 		printf("    %-8s %-8s %s\n", "Offset", "Begin", "End");
58292c23cb7cSEd Maste 	else
58302c23cb7cSEd Maste 		printf("    %-8s %-16s %s\n", "Offset", "Begin", "End");
58312c23cb7cSEd Maste 
58322c23cb7cSEd Maste 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
58332c23cb7cSEd Maste 	    NULL, &de)) == DW_DLV_OK) {
58342c23cb7cSEd Maste 		die = NULL;
58352c23cb7cSEd Maste 		if (dwarf_siblingof(re->dbg, die, &die, &de) != DW_DLV_OK)
58362c23cb7cSEd Maste 			continue;
58372c23cb7cSEd Maste 		if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
58382c23cb7cSEd Maste 			warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
58392c23cb7cSEd Maste 			continue;
58402c23cb7cSEd Maste 		}
58412c23cb7cSEd Maste 		/* XXX: What about DW_TAG_partial_unit? */
58422c23cb7cSEd Maste 		lowpc = 0;
58432c23cb7cSEd Maste 		if (tag == DW_TAG_compile_unit) {
58442c23cb7cSEd Maste 			if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lowpc,
58452c23cb7cSEd Maste 			    &de) != DW_DLV_OK)
58462c23cb7cSEd Maste 				lowpc = 0;
58472c23cb7cSEd Maste 		}
58482c23cb7cSEd Maste 
58492c23cb7cSEd Maste 		dump_dwarf_ranges_foreach(re, die, (Dwarf_Addr) lowpc);
58502c23cb7cSEd Maste 	}
58512c23cb7cSEd Maste 	putchar('\n');
58522c23cb7cSEd Maste }
58532c23cb7cSEd Maste 
58542c23cb7cSEd Maste static void
58552c23cb7cSEd Maste dump_dwarf_macinfo(struct readelf *re)
58562c23cb7cSEd Maste {
58572c23cb7cSEd Maste 	Dwarf_Unsigned offset;
58582c23cb7cSEd Maste 	Dwarf_Signed cnt;
58592c23cb7cSEd Maste 	Dwarf_Macro_Details *md;
58602c23cb7cSEd Maste 	Dwarf_Error de;
58612c23cb7cSEd Maste 	const char *mi_str;
5862cf781b2eSEd Maste 	char unk_mi[32];
58632c23cb7cSEd Maste 	int i;
58642c23cb7cSEd Maste 
58652c23cb7cSEd Maste #define	_MAX_MACINFO_ENTRY	65535
58662c23cb7cSEd Maste 
58672c23cb7cSEd Maste 	printf("\nContents of section .debug_macinfo:\n\n");
58682c23cb7cSEd Maste 
58692c23cb7cSEd Maste 	offset = 0;
58702c23cb7cSEd Maste 	while (dwarf_get_macro_details(re->dbg, offset, _MAX_MACINFO_ENTRY,
58712c23cb7cSEd Maste 	    &cnt, &md, &de) == DW_DLV_OK) {
58722c23cb7cSEd Maste 		for (i = 0; i < cnt; i++) {
58732c23cb7cSEd Maste 			offset = md[i].dmd_offset + 1;
58742c23cb7cSEd Maste 			if (md[i].dmd_type == 0)
58752c23cb7cSEd Maste 				break;
58762c23cb7cSEd Maste 			if (dwarf_get_MACINFO_name(md[i].dmd_type, &mi_str) !=
58772c23cb7cSEd Maste 			    DW_DLV_OK) {
5878cf781b2eSEd Maste 				snprintf(unk_mi, sizeof(unk_mi),
5879cf781b2eSEd Maste 				    "[Unknown MACINFO: %#x]", md[i].dmd_type);
5880cf781b2eSEd Maste 				mi_str = unk_mi;
58812c23cb7cSEd Maste 			}
58822c23cb7cSEd Maste 			printf(" %s", mi_str);
58832c23cb7cSEd Maste 			switch (md[i].dmd_type) {
58842c23cb7cSEd Maste 			case DW_MACINFO_define:
58852c23cb7cSEd Maste 			case DW_MACINFO_undef:
58862c23cb7cSEd Maste 				printf(" - lineno : %jd macro : %s\n",
58872c23cb7cSEd Maste 				    (intmax_t) md[i].dmd_lineno,
58882c23cb7cSEd Maste 				    md[i].dmd_macro);
58892c23cb7cSEd Maste 				break;
58902c23cb7cSEd Maste 			case DW_MACINFO_start_file:
58912c23cb7cSEd Maste 				printf(" - lineno : %jd filenum : %jd\n",
58922c23cb7cSEd Maste 				    (intmax_t) md[i].dmd_lineno,
58932c23cb7cSEd Maste 				    (intmax_t) md[i].dmd_fileindex);
58942c23cb7cSEd Maste 				break;
58952c23cb7cSEd Maste 			default:
58962c23cb7cSEd Maste 				putchar('\n');
58972c23cb7cSEd Maste 				break;
58982c23cb7cSEd Maste 			}
58992c23cb7cSEd Maste 		}
59002c23cb7cSEd Maste 	}
59012c23cb7cSEd Maste 
59022c23cb7cSEd Maste #undef	_MAX_MACINFO_ENTRY
59032c23cb7cSEd Maste }
59042c23cb7cSEd Maste 
59052c23cb7cSEd Maste static void
5906cf781b2eSEd Maste dump_dwarf_frame_inst(struct readelf *re, Dwarf_Cie cie, uint8_t *insts,
5907cf781b2eSEd Maste     Dwarf_Unsigned len, Dwarf_Unsigned caf, Dwarf_Signed daf, Dwarf_Addr pc,
5908cf781b2eSEd Maste     Dwarf_Debug dbg)
59092c23cb7cSEd Maste {
59102c23cb7cSEd Maste 	Dwarf_Frame_Op *oplist;
59112c23cb7cSEd Maste 	Dwarf_Signed opcnt, delta;
59122c23cb7cSEd Maste 	Dwarf_Small op;
59132c23cb7cSEd Maste 	Dwarf_Error de;
59142c23cb7cSEd Maste 	const char *op_str;
5915cf781b2eSEd Maste 	char unk_op[32];
59162c23cb7cSEd Maste 	int i;
59172c23cb7cSEd Maste 
59182c23cb7cSEd Maste 	if (dwarf_expand_frame_instructions(cie, insts, len, &oplist,
59192c23cb7cSEd Maste 	    &opcnt, &de) != DW_DLV_OK) {
59202c23cb7cSEd Maste 		warnx("dwarf_expand_frame_instructions failed: %s",
59212c23cb7cSEd Maste 		    dwarf_errmsg(de));
59222c23cb7cSEd Maste 		return;
59232c23cb7cSEd Maste 	}
59242c23cb7cSEd Maste 
59252c23cb7cSEd Maste 	for (i = 0; i < opcnt; i++) {
59262c23cb7cSEd Maste 		if (oplist[i].fp_base_op != 0)
59272c23cb7cSEd Maste 			op = oplist[i].fp_base_op << 6;
59282c23cb7cSEd Maste 		else
59292c23cb7cSEd Maste 			op = oplist[i].fp_extended_op;
59302c23cb7cSEd Maste 		if (dwarf_get_CFA_name(op, &op_str) != DW_DLV_OK) {
5931cf781b2eSEd Maste 			snprintf(unk_op, sizeof(unk_op), "[Unknown CFA: %#x]",
5932cf781b2eSEd Maste 			    op);
5933cf781b2eSEd Maste 			op_str = unk_op;
59342c23cb7cSEd Maste 		}
59352c23cb7cSEd Maste 		printf("  %s", op_str);
59362c23cb7cSEd Maste 		switch (op) {
59372c23cb7cSEd Maste 		case DW_CFA_advance_loc:
59382c23cb7cSEd Maste 			delta = oplist[i].fp_offset * caf;
59392c23cb7cSEd Maste 			pc += delta;
59402c23cb7cSEd Maste 			printf(": %ju to %08jx", (uintmax_t) delta,
59412c23cb7cSEd Maste 			    (uintmax_t) pc);
59422c23cb7cSEd Maste 			break;
59432c23cb7cSEd Maste 		case DW_CFA_offset:
59442c23cb7cSEd Maste 		case DW_CFA_offset_extended:
59452c23cb7cSEd Maste 		case DW_CFA_offset_extended_sf:
59462c23cb7cSEd Maste 			delta = oplist[i].fp_offset * daf;
5947cf781b2eSEd Maste 			printf(": r%u (%s) at cfa%+jd", oplist[i].fp_register,
5948cf781b2eSEd Maste 			    dwarf_regname(re, oplist[i].fp_register),
59492c23cb7cSEd Maste 			    (intmax_t) delta);
59502c23cb7cSEd Maste 			break;
59512c23cb7cSEd Maste 		case DW_CFA_restore:
5952cf781b2eSEd Maste 			printf(": r%u (%s)", oplist[i].fp_register,
5953cf781b2eSEd Maste 			    dwarf_regname(re, oplist[i].fp_register));
59542c23cb7cSEd Maste 			break;
59552c23cb7cSEd Maste 		case DW_CFA_set_loc:
59562c23cb7cSEd Maste 			pc = oplist[i].fp_offset;
59572c23cb7cSEd Maste 			printf(": to %08jx", (uintmax_t) pc);
59582c23cb7cSEd Maste 			break;
59592c23cb7cSEd Maste 		case DW_CFA_advance_loc1:
59602c23cb7cSEd Maste 		case DW_CFA_advance_loc2:
59612c23cb7cSEd Maste 		case DW_CFA_advance_loc4:
59622c23cb7cSEd Maste 			pc += oplist[i].fp_offset;
59632c23cb7cSEd Maste 			printf(": %jd to %08jx", (intmax_t) oplist[i].fp_offset,
59642c23cb7cSEd Maste 			    (uintmax_t) pc);
59652c23cb7cSEd Maste 			break;
59662c23cb7cSEd Maste 		case DW_CFA_def_cfa:
5967cf781b2eSEd Maste 			printf(": r%u (%s) ofs %ju", oplist[i].fp_register,
5968cf781b2eSEd Maste 			    dwarf_regname(re, oplist[i].fp_register),
59692c23cb7cSEd Maste 			    (uintmax_t) oplist[i].fp_offset);
59702c23cb7cSEd Maste 			break;
59712c23cb7cSEd Maste 		case DW_CFA_def_cfa_sf:
5972cf781b2eSEd Maste 			printf(": r%u (%s) ofs %jd", oplist[i].fp_register,
5973cf781b2eSEd Maste 			    dwarf_regname(re, oplist[i].fp_register),
59742c23cb7cSEd Maste 			    (intmax_t) (oplist[i].fp_offset * daf));
59752c23cb7cSEd Maste 			break;
59762c23cb7cSEd Maste 		case DW_CFA_def_cfa_register:
5977cf781b2eSEd Maste 			printf(": r%u (%s)", oplist[i].fp_register,
5978cf781b2eSEd Maste 			    dwarf_regname(re, oplist[i].fp_register));
59792c23cb7cSEd Maste 			break;
59802c23cb7cSEd Maste 		case DW_CFA_def_cfa_offset:
59812c23cb7cSEd Maste 			printf(": %ju", (uintmax_t) oplist[i].fp_offset);
59822c23cb7cSEd Maste 			break;
59832c23cb7cSEd Maste 		case DW_CFA_def_cfa_offset_sf:
59842c23cb7cSEd Maste 			printf(": %jd", (intmax_t) (oplist[i].fp_offset * daf));
59852c23cb7cSEd Maste 			break;
59862c23cb7cSEd Maste 		default:
59872c23cb7cSEd Maste 			break;
59882c23cb7cSEd Maste 		}
59892c23cb7cSEd Maste 		putchar('\n');
59902c23cb7cSEd Maste 	}
59912c23cb7cSEd Maste 
59922c23cb7cSEd Maste 	dwarf_dealloc(dbg, oplist, DW_DLA_FRAME_BLOCK);
59932c23cb7cSEd Maste }
59942c23cb7cSEd Maste 
59952c23cb7cSEd Maste static char *
5996cf781b2eSEd Maste get_regoff_str(struct readelf *re, Dwarf_Half reg, Dwarf_Addr off)
59972c23cb7cSEd Maste {
59982c23cb7cSEd Maste 	static char rs[16];
59992c23cb7cSEd Maste 
60002c23cb7cSEd Maste 	if (reg == DW_FRAME_UNDEFINED_VAL || reg == DW_FRAME_REG_INITIAL_VALUE)
60012c23cb7cSEd Maste 		snprintf(rs, sizeof(rs), "%c", 'u');
60022c23cb7cSEd Maste 	else if (reg == DW_FRAME_CFA_COL)
60032c23cb7cSEd Maste 		snprintf(rs, sizeof(rs), "c%+jd", (intmax_t) off);
60042c23cb7cSEd Maste 	else
6005cf781b2eSEd Maste 		snprintf(rs, sizeof(rs), "%s%+jd", dwarf_regname(re, reg),
6006cf781b2eSEd Maste 		    (intmax_t) off);
60072c23cb7cSEd Maste 
60082c23cb7cSEd Maste 	return (rs);
60092c23cb7cSEd Maste }
60102c23cb7cSEd Maste 
60112c23cb7cSEd Maste static int
6012cf781b2eSEd Maste dump_dwarf_frame_regtable(struct readelf *re, Dwarf_Fde fde, Dwarf_Addr pc,
6013cf781b2eSEd Maste     Dwarf_Unsigned func_len, Dwarf_Half cie_ra)
60142c23cb7cSEd Maste {
60152c23cb7cSEd Maste 	Dwarf_Regtable rt;
60162c23cb7cSEd Maste 	Dwarf_Addr row_pc, end_pc, pre_pc, cur_pc;
60172c23cb7cSEd Maste 	Dwarf_Error de;
60182c23cb7cSEd Maste 	char *vec;
60192c23cb7cSEd Maste 	int i;
60202c23cb7cSEd Maste 
60212c23cb7cSEd Maste #define BIT_SET(v, n) (v[(n)>>3] |= 1U << ((n) & 7))
60222c23cb7cSEd Maste #define BIT_CLR(v, n) (v[(n)>>3] &= ~(1U << ((n) & 7)))
60232c23cb7cSEd Maste #define BIT_ISSET(v, n) (v[(n)>>3] & (1U << ((n) & 7)))
60242c23cb7cSEd Maste #define	RT(x) rt.rules[(x)]
60252c23cb7cSEd Maste 
60262c23cb7cSEd Maste 	vec = calloc((DW_REG_TABLE_SIZE + 7) / 8, 1);
60272c23cb7cSEd Maste 	if (vec == NULL)
60282c23cb7cSEd Maste 		err(EXIT_FAILURE, "calloc failed");
60292c23cb7cSEd Maste 
60302c23cb7cSEd Maste 	pre_pc = ~((Dwarf_Addr) 0);
60312c23cb7cSEd Maste 	cur_pc = pc;
60322c23cb7cSEd Maste 	end_pc = pc + func_len;
60332c23cb7cSEd Maste 	for (; cur_pc < end_pc; cur_pc++) {
60342c23cb7cSEd Maste 		if (dwarf_get_fde_info_for_all_regs(fde, cur_pc, &rt, &row_pc,
60352c23cb7cSEd Maste 		    &de) != DW_DLV_OK) {
603685642eeeSMark Johnston 			free(vec);
60372c23cb7cSEd Maste 			warnx("dwarf_get_fde_info_for_all_regs failed: %s\n",
60382c23cb7cSEd Maste 			    dwarf_errmsg(de));
60392c23cb7cSEd Maste 			return (-1);
60402c23cb7cSEd Maste 		}
60412c23cb7cSEd Maste 		if (row_pc == pre_pc)
60422c23cb7cSEd Maste 			continue;
60432c23cb7cSEd Maste 		pre_pc = row_pc;
60442c23cb7cSEd Maste 		for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
60452c23cb7cSEd Maste 			if (rt.rules[i].dw_regnum != DW_FRAME_REG_INITIAL_VALUE)
60462c23cb7cSEd Maste 				BIT_SET(vec, i);
60472c23cb7cSEd Maste 		}
60482c23cb7cSEd Maste 	}
60492c23cb7cSEd Maste 
60502c23cb7cSEd Maste 	printf("   LOC   CFA      ");
60512c23cb7cSEd Maste 	for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
60522c23cb7cSEd Maste 		if (BIT_ISSET(vec, i)) {
60532c23cb7cSEd Maste 			if ((Dwarf_Half) i == cie_ra)
60542c23cb7cSEd Maste 				printf("ra   ");
6055cf781b2eSEd Maste 			else
6056cf781b2eSEd Maste 				printf("%-5s",
6057cf781b2eSEd Maste 				    dwarf_regname(re, (unsigned int) i));
60582c23cb7cSEd Maste 		}
60592c23cb7cSEd Maste 	}
60602c23cb7cSEd Maste 	putchar('\n');
60612c23cb7cSEd Maste 
60622c23cb7cSEd Maste 	pre_pc = ~((Dwarf_Addr) 0);
60632c23cb7cSEd Maste 	cur_pc = pc;
60642c23cb7cSEd Maste 	end_pc = pc + func_len;
60652c23cb7cSEd Maste 	for (; cur_pc < end_pc; cur_pc++) {
60662c23cb7cSEd Maste 		if (dwarf_get_fde_info_for_all_regs(fde, cur_pc, &rt, &row_pc,
60672c23cb7cSEd Maste 		    &de) != DW_DLV_OK) {
6068d5e7add6SMark Johnston 			free(vec);
60692c23cb7cSEd Maste 			warnx("dwarf_get_fde_info_for_all_regs failed: %s\n",
60702c23cb7cSEd Maste 			    dwarf_errmsg(de));
60712c23cb7cSEd Maste 			return (-1);
60722c23cb7cSEd Maste 		}
60732c23cb7cSEd Maste 		if (row_pc == pre_pc)
60742c23cb7cSEd Maste 			continue;
60752c23cb7cSEd Maste 		pre_pc = row_pc;
60762c23cb7cSEd Maste 		printf("%08jx ", (uintmax_t) row_pc);
6077cf781b2eSEd Maste 		printf("%-8s ", get_regoff_str(re, RT(0).dw_regnum,
60782c23cb7cSEd Maste 		    RT(0).dw_offset));
60792c23cb7cSEd Maste 		for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
60802c23cb7cSEd Maste 			if (BIT_ISSET(vec, i)) {
6081cf781b2eSEd Maste 				printf("%-5s", get_regoff_str(re,
6082cf781b2eSEd Maste 				    RT(i).dw_regnum, RT(i).dw_offset));
60832c23cb7cSEd Maste 			}
60842c23cb7cSEd Maste 		}
60852c23cb7cSEd Maste 		putchar('\n');
60862c23cb7cSEd Maste 	}
60872c23cb7cSEd Maste 
60882c23cb7cSEd Maste 	free(vec);
60892c23cb7cSEd Maste 
60902c23cb7cSEd Maste 	return (0);
60912c23cb7cSEd Maste 
60922c23cb7cSEd Maste #undef	BIT_SET
60932c23cb7cSEd Maste #undef	BIT_CLR
60942c23cb7cSEd Maste #undef	BIT_ISSET
60952c23cb7cSEd Maste #undef	RT
60962c23cb7cSEd Maste }
60972c23cb7cSEd Maste 
60982c23cb7cSEd Maste static void
60992c23cb7cSEd Maste dump_dwarf_frame_section(struct readelf *re, struct section *s, int alt)
61002c23cb7cSEd Maste {
61012c23cb7cSEd Maste 	Dwarf_Cie *cie_list, cie, pre_cie;
61022c23cb7cSEd Maste 	Dwarf_Fde *fde_list, fde;
61032c23cb7cSEd Maste 	Dwarf_Off cie_offset, fde_offset;
61042c23cb7cSEd Maste 	Dwarf_Unsigned cie_length, fde_instlen;
61052c23cb7cSEd Maste 	Dwarf_Unsigned cie_caf, cie_daf, cie_instlen, func_len, fde_length;
61062c23cb7cSEd Maste 	Dwarf_Signed cie_count, fde_count, cie_index;
61072c23cb7cSEd Maste 	Dwarf_Addr low_pc;
61082c23cb7cSEd Maste 	Dwarf_Half cie_ra;
61092c23cb7cSEd Maste 	Dwarf_Small cie_version;
61102c23cb7cSEd Maste 	Dwarf_Ptr fde_addr, fde_inst, cie_inst;
61112c23cb7cSEd Maste 	char *cie_aug, c;
61122c23cb7cSEd Maste 	int i, eh_frame;
61132c23cb7cSEd Maste 	Dwarf_Error de;
61142c23cb7cSEd Maste 
61152c23cb7cSEd Maste 	printf("\nThe section %s contains:\n\n", s->name);
61162c23cb7cSEd Maste 
61172c23cb7cSEd Maste 	if (!strcmp(s->name, ".debug_frame")) {
61182c23cb7cSEd Maste 		eh_frame = 0;
61192c23cb7cSEd Maste 		if (dwarf_get_fde_list(re->dbg, &cie_list, &cie_count,
61202c23cb7cSEd Maste 		    &fde_list, &fde_count, &de) != DW_DLV_OK) {
61212c23cb7cSEd Maste 			warnx("dwarf_get_fde_list failed: %s",
61222c23cb7cSEd Maste 			    dwarf_errmsg(de));
61232c23cb7cSEd Maste 			return;
61242c23cb7cSEd Maste 		}
61252c23cb7cSEd Maste 	} else if (!strcmp(s->name, ".eh_frame")) {
61262c23cb7cSEd Maste 		eh_frame = 1;
61272c23cb7cSEd Maste 		if (dwarf_get_fde_list_eh(re->dbg, &cie_list, &cie_count,
61282c23cb7cSEd Maste 		    &fde_list, &fde_count, &de) != DW_DLV_OK) {
61292c23cb7cSEd Maste 			warnx("dwarf_get_fde_list_eh failed: %s",
61302c23cb7cSEd Maste 			    dwarf_errmsg(de));
61312c23cb7cSEd Maste 			return;
61322c23cb7cSEd Maste 		}
61332c23cb7cSEd Maste 	} else
61342c23cb7cSEd Maste 		return;
61352c23cb7cSEd Maste 
61362c23cb7cSEd Maste 	pre_cie = NULL;
61372c23cb7cSEd Maste 	for (i = 0; i < fde_count; i++) {
61382c23cb7cSEd Maste 		if (dwarf_get_fde_n(fde_list, i, &fde, &de) != DW_DLV_OK) {
61392c23cb7cSEd Maste 			warnx("dwarf_get_fde_n failed: %s", dwarf_errmsg(de));
61402c23cb7cSEd Maste 			continue;
61412c23cb7cSEd Maste 		}
61422c23cb7cSEd Maste 		if (dwarf_get_cie_of_fde(fde, &cie, &de) != DW_DLV_OK) {
61432c23cb7cSEd Maste 			warnx("dwarf_get_fde_n failed: %s", dwarf_errmsg(de));
61442c23cb7cSEd Maste 			continue;
61452c23cb7cSEd Maste 		}
61462c23cb7cSEd Maste 		if (dwarf_get_fde_range(fde, &low_pc, &func_len, &fde_addr,
61472c23cb7cSEd Maste 		    &fde_length, &cie_offset, &cie_index, &fde_offset,
61482c23cb7cSEd Maste 		    &de) != DW_DLV_OK) {
61492c23cb7cSEd Maste 			warnx("dwarf_get_fde_range failed: %s",
61502c23cb7cSEd Maste 			    dwarf_errmsg(de));
61512c23cb7cSEd Maste 			continue;
61522c23cb7cSEd Maste 		}
61532c23cb7cSEd Maste 		if (dwarf_get_fde_instr_bytes(fde, &fde_inst, &fde_instlen,
61542c23cb7cSEd Maste 		    &de) != DW_DLV_OK) {
61552c23cb7cSEd Maste 			warnx("dwarf_get_fde_instr_bytes failed: %s",
61562c23cb7cSEd Maste 			    dwarf_errmsg(de));
61572c23cb7cSEd Maste 			continue;
61582c23cb7cSEd Maste 		}
61592c23cb7cSEd Maste 		if (pre_cie == NULL || cie != pre_cie) {
61602c23cb7cSEd Maste 			pre_cie = cie;
61612c23cb7cSEd Maste 			if (dwarf_get_cie_info(cie, &cie_length, &cie_version,
61622c23cb7cSEd Maste 			    &cie_aug, &cie_caf, &cie_daf, &cie_ra,
61632c23cb7cSEd Maste 			    &cie_inst, &cie_instlen, &de) != DW_DLV_OK) {
61642c23cb7cSEd Maste 				warnx("dwarf_get_cie_info failed: %s",
61652c23cb7cSEd Maste 				    dwarf_errmsg(de));
61662c23cb7cSEd Maste 				continue;
61672c23cb7cSEd Maste 			}
61682c23cb7cSEd Maste 			printf("%08jx %08jx %8.8jx CIE",
61692c23cb7cSEd Maste 			    (uintmax_t) cie_offset,
61702c23cb7cSEd Maste 			    (uintmax_t) cie_length,
61712c23cb7cSEd Maste 			    (uintmax_t) (eh_frame ? 0 : ~0U));
61722c23cb7cSEd Maste 			if (!alt) {
61732c23cb7cSEd Maste 				putchar('\n');
61742c23cb7cSEd Maste 				printf("  Version:\t\t\t%u\n", cie_version);
61752c23cb7cSEd Maste 				printf("  Augmentation:\t\t\t\"");
61762c23cb7cSEd Maste 				while ((c = *cie_aug++) != '\0')
61772c23cb7cSEd Maste 					putchar(c);
61782c23cb7cSEd Maste 				printf("\"\n");
61792c23cb7cSEd Maste 				printf("  Code alignment factor:\t%ju\n",
61802c23cb7cSEd Maste 				    (uintmax_t) cie_caf);
61812c23cb7cSEd Maste 				printf("  Data alignment factor:\t%jd\n",
61822c23cb7cSEd Maste 				    (intmax_t) cie_daf);
61832c23cb7cSEd Maste 				printf("  Return address column:\t%ju\n",
61842c23cb7cSEd Maste 				    (uintmax_t) cie_ra);
61852c23cb7cSEd Maste 				putchar('\n');
6186cf781b2eSEd Maste 				dump_dwarf_frame_inst(re, cie, cie_inst,
61872c23cb7cSEd Maste 				    cie_instlen, cie_caf, cie_daf, 0,
61882c23cb7cSEd Maste 				    re->dbg);
61892c23cb7cSEd Maste 				putchar('\n');
61902c23cb7cSEd Maste 			} else {
61912c23cb7cSEd Maste 				printf(" \"");
61922c23cb7cSEd Maste 				while ((c = *cie_aug++) != '\0')
61932c23cb7cSEd Maste 					putchar(c);
61942c23cb7cSEd Maste 				putchar('"');
61952c23cb7cSEd Maste 				printf(" cf=%ju df=%jd ra=%ju\n",
61962c23cb7cSEd Maste 				    (uintmax_t) cie_caf,
61972c23cb7cSEd Maste 				    (uintmax_t) cie_daf,
61982c23cb7cSEd Maste 				    (uintmax_t) cie_ra);
6199cf781b2eSEd Maste 				dump_dwarf_frame_regtable(re, fde, low_pc, 1,
62002c23cb7cSEd Maste 				    cie_ra);
62012c23cb7cSEd Maste 				putchar('\n');
62022c23cb7cSEd Maste 			}
62032c23cb7cSEd Maste 		}
62042c23cb7cSEd Maste 		printf("%08jx %08jx %08jx FDE cie=%08jx pc=%08jx..%08jx\n",
62052c23cb7cSEd Maste 		    (uintmax_t) fde_offset, (uintmax_t) fde_length,
62062c23cb7cSEd Maste 		    (uintmax_t) cie_offset,
62072c23cb7cSEd Maste 		    (uintmax_t) (eh_frame ? fde_offset + 4 - cie_offset :
62082c23cb7cSEd Maste 			cie_offset),
62092c23cb7cSEd Maste 		    (uintmax_t) low_pc, (uintmax_t) (low_pc + func_len));
62102c23cb7cSEd Maste 		if (!alt)
6211cf781b2eSEd Maste 			dump_dwarf_frame_inst(re, cie, fde_inst, fde_instlen,
62122c23cb7cSEd Maste 			    cie_caf, cie_daf, low_pc, re->dbg);
62132c23cb7cSEd Maste 		else
6214cf781b2eSEd Maste 			dump_dwarf_frame_regtable(re, fde, low_pc, func_len,
62152c23cb7cSEd Maste 			    cie_ra);
62162c23cb7cSEd Maste 		putchar('\n');
62172c23cb7cSEd Maste 	}
62182c23cb7cSEd Maste }
62192c23cb7cSEd Maste 
62202c23cb7cSEd Maste static void
62212c23cb7cSEd Maste dump_dwarf_frame(struct readelf *re, int alt)
62222c23cb7cSEd Maste {
62232c23cb7cSEd Maste 	struct section *s;
62242c23cb7cSEd Maste 	int i;
62252c23cb7cSEd Maste 
62262c23cb7cSEd Maste 	(void) dwarf_set_frame_cfa_value(re->dbg, DW_FRAME_CFA_COL);
62272c23cb7cSEd Maste 
62282c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
62292c23cb7cSEd Maste 		s = &re->sl[i];
62302c23cb7cSEd Maste 		if (s->name != NULL && (!strcmp(s->name, ".debug_frame") ||
62312c23cb7cSEd Maste 		    !strcmp(s->name, ".eh_frame")))
62322c23cb7cSEd Maste 			dump_dwarf_frame_section(re, s, alt);
62332c23cb7cSEd Maste 	}
62342c23cb7cSEd Maste }
62352c23cb7cSEd Maste 
62362c23cb7cSEd Maste static void
62372c23cb7cSEd Maste dump_dwarf_str(struct readelf *re)
62382c23cb7cSEd Maste {
62392c23cb7cSEd Maste 	struct section *s;
62402c23cb7cSEd Maste 	Elf_Data *d;
62412c23cb7cSEd Maste 	unsigned char *p;
62422c23cb7cSEd Maste 	int elferr, end, i, j;
62432c23cb7cSEd Maste 
62442c23cb7cSEd Maste 	printf("\nContents of section .debug_str:\n");
62452c23cb7cSEd Maste 
62462c23cb7cSEd Maste 	s = NULL;
62472c23cb7cSEd Maste 	for (i = 0; (size_t) i < re->shnum; i++) {
62482c23cb7cSEd Maste 		s = &re->sl[i];
62492c23cb7cSEd Maste 		if (s->name != NULL && !strcmp(s->name, ".debug_str"))
62502c23cb7cSEd Maste 			break;
62512c23cb7cSEd Maste 	}
62522c23cb7cSEd Maste 	if ((size_t) i >= re->shnum)
62532c23cb7cSEd Maste 		return;
62542c23cb7cSEd Maste 
62552c23cb7cSEd Maste 	(void) elf_errno();
62562c23cb7cSEd Maste 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
62572c23cb7cSEd Maste 		elferr = elf_errno();
62582c23cb7cSEd Maste 		if (elferr != 0)
62592c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
62602c23cb7cSEd Maste 		return;
62612c23cb7cSEd Maste 	}
62622c23cb7cSEd Maste 	if (d->d_size <= 0)
62632c23cb7cSEd Maste 		return;
62642c23cb7cSEd Maste 
62652c23cb7cSEd Maste 	for (i = 0, p = d->d_buf; (size_t) i < d->d_size; i += 16) {
62662c23cb7cSEd Maste 		printf("  0x%08x", (unsigned int) i);
62672c23cb7cSEd Maste 		if ((size_t) i + 16 > d->d_size)
62682c23cb7cSEd Maste 			end = d->d_size;
62692c23cb7cSEd Maste 		else
62702c23cb7cSEd Maste 			end = i + 16;
62712c23cb7cSEd Maste 		for (j = i; j < i + 16; j++) {
62722c23cb7cSEd Maste 			if ((j - i) % 4 == 0)
62732c23cb7cSEd Maste 				putchar(' ');
62742c23cb7cSEd Maste 			if (j >= end) {
62752c23cb7cSEd Maste 				printf("  ");
62762c23cb7cSEd Maste 				continue;
62772c23cb7cSEd Maste 			}
62782c23cb7cSEd Maste 			printf("%02x", (uint8_t) p[j]);
62792c23cb7cSEd Maste 		}
62802c23cb7cSEd Maste 		putchar(' ');
62812c23cb7cSEd Maste 		for (j = i; j < end; j++) {
62822c23cb7cSEd Maste 			if (isprint(p[j]))
62832c23cb7cSEd Maste 				putchar(p[j]);
62842c23cb7cSEd Maste 			else if (p[j] == 0)
62852c23cb7cSEd Maste 				putchar('.');
62862c23cb7cSEd Maste 			else
62872c23cb7cSEd Maste 				putchar(' ');
62882c23cb7cSEd Maste 		}
62892c23cb7cSEd Maste 		putchar('\n');
62902c23cb7cSEd Maste 	}
62912c23cb7cSEd Maste }
62922c23cb7cSEd Maste 
62939817bae9SEd Maste static int
62949817bae9SEd Maste loc_at_comparator(const void *la1, const void *la2)
62959817bae9SEd Maste {
62969817bae9SEd Maste 	const struct loc_at *left, *right;
62972c23cb7cSEd Maste 
62989817bae9SEd Maste 	left = (const struct loc_at *)la1;
62999817bae9SEd Maste 	right = (const struct loc_at *)la2;
63009817bae9SEd Maste 
63019817bae9SEd Maste 	if (left->la_off > right->la_off)
63029817bae9SEd Maste 		return (1);
63039817bae9SEd Maste 	else if (left->la_off < right->la_off)
63049817bae9SEd Maste 		return (-1);
63059817bae9SEd Maste 	else
63069817bae9SEd Maste 		return (0);
63079817bae9SEd Maste }
63082c23cb7cSEd Maste 
63092c23cb7cSEd Maste static void
63109817bae9SEd Maste search_loclist_at(struct readelf *re, Dwarf_Die die, Dwarf_Unsigned lowpc,
6311cb12a690SEd Maste     struct loc_at **la_list, size_t *la_list_len, size_t *la_list_cap)
63122c23cb7cSEd Maste {
63139817bae9SEd Maste 	struct loc_at *la;
63142c23cb7cSEd Maste 	Dwarf_Attribute *attr_list;
63152c23cb7cSEd Maste 	Dwarf_Die ret_die;
63162c23cb7cSEd Maste 	Dwarf_Unsigned off;
6317cf781b2eSEd Maste 	Dwarf_Off ref;
63182c23cb7cSEd Maste 	Dwarf_Signed attr_count;
63192c23cb7cSEd Maste 	Dwarf_Half attr, form;
6320cf781b2eSEd Maste 	Dwarf_Bool is_info;
63212c23cb7cSEd Maste 	Dwarf_Error de;
63222c23cb7cSEd Maste 	int i, ret;
63232c23cb7cSEd Maste 
6324cf781b2eSEd Maste 	is_info = dwarf_get_die_infotypes_flag(die);
6325cf781b2eSEd Maste 
63262c23cb7cSEd Maste 	if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
63272c23cb7cSEd Maste 	    DW_DLV_OK) {
63282c23cb7cSEd Maste 		if (ret == DW_DLV_ERROR)
63292c23cb7cSEd Maste 			warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
63302c23cb7cSEd Maste 		goto cont_search;
63312c23cb7cSEd Maste 	}
63322c23cb7cSEd Maste 	for (i = 0; i < attr_count; i++) {
63332c23cb7cSEd Maste 		if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
63342c23cb7cSEd Maste 			warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
63352c23cb7cSEd Maste 			continue;
63362c23cb7cSEd Maste 		}
63372c23cb7cSEd Maste 		if (attr != DW_AT_location &&
63382c23cb7cSEd Maste 		    attr != DW_AT_string_length &&
63392c23cb7cSEd Maste 		    attr != DW_AT_return_addr &&
63402c23cb7cSEd Maste 		    attr != DW_AT_data_member_location &&
63412c23cb7cSEd Maste 		    attr != DW_AT_frame_base &&
63422c23cb7cSEd Maste 		    attr != DW_AT_segment &&
63432c23cb7cSEd Maste 		    attr != DW_AT_static_link &&
63442c23cb7cSEd Maste 		    attr != DW_AT_use_location &&
63452c23cb7cSEd Maste 		    attr != DW_AT_vtable_elem_location)
63462c23cb7cSEd Maste 			continue;
63472c23cb7cSEd Maste 		if (dwarf_whatform(attr_list[i], &form, &de) != DW_DLV_OK) {
63482c23cb7cSEd Maste 			warnx("dwarf_whatform failed: %s", dwarf_errmsg(de));
63492c23cb7cSEd Maste 			continue;
63502c23cb7cSEd Maste 		}
6351cf781b2eSEd Maste 		if (form == DW_FORM_data4 || form == DW_FORM_data8) {
6352cf781b2eSEd Maste 			if (dwarf_formudata(attr_list[i], &off, &de) !=
6353cf781b2eSEd Maste 			    DW_DLV_OK) {
6354cf781b2eSEd Maste 				warnx("dwarf_formudata failed: %s",
6355cf781b2eSEd Maste 				    dwarf_errmsg(de));
63562c23cb7cSEd Maste 				continue;
63572c23cb7cSEd Maste 			}
6358cf781b2eSEd Maste 		} else if (form == DW_FORM_sec_offset) {
6359cf781b2eSEd Maste 			if (dwarf_global_formref(attr_list[i], &ref, &de) !=
6360cf781b2eSEd Maste 			    DW_DLV_OK) {
6361cf781b2eSEd Maste 				warnx("dwarf_global_formref failed: %s",
6362cf781b2eSEd Maste 				    dwarf_errmsg(de));
6363cf781b2eSEd Maste 				continue;
6364cf781b2eSEd Maste 			}
6365cf781b2eSEd Maste 			off = ref;
6366cf781b2eSEd Maste 		} else
6367cf781b2eSEd Maste 			continue;
6368cf781b2eSEd Maste 
63699817bae9SEd Maste 		if (*la_list_cap == *la_list_len) {
63709817bae9SEd Maste 			*la_list = realloc(*la_list,
63719817bae9SEd Maste 			    *la_list_cap * 2 * sizeof(**la_list));
637237fa1df2SMark Johnston 			if (*la_list == NULL)
637337fa1df2SMark Johnston 				err(EXIT_FAILURE, "realloc failed");
63749817bae9SEd Maste 			*la_list_cap *= 2;
63752c23cb7cSEd Maste 		}
63769817bae9SEd Maste 		la = &((*la_list)[*la_list_len]);
63779817bae9SEd Maste 		la->la_at = attr_list[i];
63789817bae9SEd Maste 		la->la_off = off;
63799817bae9SEd Maste 		la->la_lowpc = lowpc;
63809817bae9SEd Maste 		la->la_cu_psize = re->cu_psize;
63819817bae9SEd Maste 		la->la_cu_osize = re->cu_osize;
63829817bae9SEd Maste 		la->la_cu_ver = re->cu_ver;
63839817bae9SEd Maste 		(*la_list_len)++;
63842c23cb7cSEd Maste 	}
63852c23cb7cSEd Maste 
63862c23cb7cSEd Maste cont_search:
63872c23cb7cSEd Maste 	/* Search children. */
63882c23cb7cSEd Maste 	ret = dwarf_child(die, &ret_die, &de);
63892c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
63902c23cb7cSEd Maste 		warnx("dwarf_child: %s", dwarf_errmsg(de));
63912c23cb7cSEd Maste 	else if (ret == DW_DLV_OK)
63929817bae9SEd Maste 		search_loclist_at(re, ret_die, lowpc, la_list,
63939817bae9SEd Maste 		    la_list_len, la_list_cap);
63942c23cb7cSEd Maste 
63952c23cb7cSEd Maste 	/* Search sibling. */
6396cf781b2eSEd Maste 	ret = dwarf_siblingof_b(re->dbg, die, &ret_die, is_info, &de);
63972c23cb7cSEd Maste 	if (ret == DW_DLV_ERROR)
63982c23cb7cSEd Maste 		warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
63992c23cb7cSEd Maste 	else if (ret == DW_DLV_OK)
64009817bae9SEd Maste 		search_loclist_at(re, ret_die, lowpc, la_list,
64019817bae9SEd Maste 		    la_list_len, la_list_cap);
64022c23cb7cSEd Maste }
64032c23cb7cSEd Maste 
64042c23cb7cSEd Maste static void
6405cf781b2eSEd Maste dump_dwarf_loc(struct readelf *re, Dwarf_Loc *lr)
64062c23cb7cSEd Maste {
64072c23cb7cSEd Maste 	const char *op_str;
6408cf781b2eSEd Maste 	char unk_op[32];
6409cf781b2eSEd Maste 	uint8_t *b, n;
6410cf781b2eSEd Maste 	int i;
64112c23cb7cSEd Maste 
64122c23cb7cSEd Maste 	if (dwarf_get_OP_name(lr->lr_atom, &op_str) !=
64132c23cb7cSEd Maste 	    DW_DLV_OK) {
6414cf781b2eSEd Maste 		snprintf(unk_op, sizeof(unk_op),
6415cf781b2eSEd Maste 		    "[Unknown OP: %#x]", lr->lr_atom);
6416cf781b2eSEd Maste 		op_str = unk_op;
64172c23cb7cSEd Maste 	}
64182c23cb7cSEd Maste 
64192c23cb7cSEd Maste 	printf("%s", op_str);
64202c23cb7cSEd Maste 
64212c23cb7cSEd Maste 	switch (lr->lr_atom) {
64222c23cb7cSEd Maste 	case DW_OP_reg0:
64232c23cb7cSEd Maste 	case DW_OP_reg1:
64242c23cb7cSEd Maste 	case DW_OP_reg2:
64252c23cb7cSEd Maste 	case DW_OP_reg3:
64262c23cb7cSEd Maste 	case DW_OP_reg4:
64272c23cb7cSEd Maste 	case DW_OP_reg5:
64282c23cb7cSEd Maste 	case DW_OP_reg6:
64292c23cb7cSEd Maste 	case DW_OP_reg7:
64302c23cb7cSEd Maste 	case DW_OP_reg8:
64312c23cb7cSEd Maste 	case DW_OP_reg9:
64322c23cb7cSEd Maste 	case DW_OP_reg10:
64332c23cb7cSEd Maste 	case DW_OP_reg11:
64342c23cb7cSEd Maste 	case DW_OP_reg12:
64352c23cb7cSEd Maste 	case DW_OP_reg13:
64362c23cb7cSEd Maste 	case DW_OP_reg14:
64372c23cb7cSEd Maste 	case DW_OP_reg15:
64382c23cb7cSEd Maste 	case DW_OP_reg16:
64392c23cb7cSEd Maste 	case DW_OP_reg17:
64402c23cb7cSEd Maste 	case DW_OP_reg18:
64412c23cb7cSEd Maste 	case DW_OP_reg19:
64422c23cb7cSEd Maste 	case DW_OP_reg20:
64432c23cb7cSEd Maste 	case DW_OP_reg21:
64442c23cb7cSEd Maste 	case DW_OP_reg22:
64452c23cb7cSEd Maste 	case DW_OP_reg23:
64462c23cb7cSEd Maste 	case DW_OP_reg24:
64472c23cb7cSEd Maste 	case DW_OP_reg25:
64482c23cb7cSEd Maste 	case DW_OP_reg26:
64492c23cb7cSEd Maste 	case DW_OP_reg27:
64502c23cb7cSEd Maste 	case DW_OP_reg28:
64512c23cb7cSEd Maste 	case DW_OP_reg29:
64522c23cb7cSEd Maste 	case DW_OP_reg30:
64532c23cb7cSEd Maste 	case DW_OP_reg31:
6454cf781b2eSEd Maste 		printf(" (%s)", dwarf_regname(re, lr->lr_atom - DW_OP_reg0));
6455cf781b2eSEd Maste 		break;
6456cf781b2eSEd Maste 
6457cf781b2eSEd Maste 	case DW_OP_deref:
64582c23cb7cSEd Maste 	case DW_OP_lit0:
64592c23cb7cSEd Maste 	case DW_OP_lit1:
64602c23cb7cSEd Maste 	case DW_OP_lit2:
64612c23cb7cSEd Maste 	case DW_OP_lit3:
64622c23cb7cSEd Maste 	case DW_OP_lit4:
64632c23cb7cSEd Maste 	case DW_OP_lit5:
64642c23cb7cSEd Maste 	case DW_OP_lit6:
64652c23cb7cSEd Maste 	case DW_OP_lit7:
64662c23cb7cSEd Maste 	case DW_OP_lit8:
64672c23cb7cSEd Maste 	case DW_OP_lit9:
64682c23cb7cSEd Maste 	case DW_OP_lit10:
64692c23cb7cSEd Maste 	case DW_OP_lit11:
64702c23cb7cSEd Maste 	case DW_OP_lit12:
64712c23cb7cSEd Maste 	case DW_OP_lit13:
64722c23cb7cSEd Maste 	case DW_OP_lit14:
64732c23cb7cSEd Maste 	case DW_OP_lit15:
64742c23cb7cSEd Maste 	case DW_OP_lit16:
64752c23cb7cSEd Maste 	case DW_OP_lit17:
64762c23cb7cSEd Maste 	case DW_OP_lit18:
64772c23cb7cSEd Maste 	case DW_OP_lit19:
64782c23cb7cSEd Maste 	case DW_OP_lit20:
64792c23cb7cSEd Maste 	case DW_OP_lit21:
64802c23cb7cSEd Maste 	case DW_OP_lit22:
64812c23cb7cSEd Maste 	case DW_OP_lit23:
64822c23cb7cSEd Maste 	case DW_OP_lit24:
64832c23cb7cSEd Maste 	case DW_OP_lit25:
64842c23cb7cSEd Maste 	case DW_OP_lit26:
64852c23cb7cSEd Maste 	case DW_OP_lit27:
64862c23cb7cSEd Maste 	case DW_OP_lit28:
64872c23cb7cSEd Maste 	case DW_OP_lit29:
64882c23cb7cSEd Maste 	case DW_OP_lit30:
64892c23cb7cSEd Maste 	case DW_OP_lit31:
64902c23cb7cSEd Maste 	case DW_OP_dup:
64912c23cb7cSEd Maste 	case DW_OP_drop:
64922c23cb7cSEd Maste 	case DW_OP_over:
64932c23cb7cSEd Maste 	case DW_OP_swap:
64942c23cb7cSEd Maste 	case DW_OP_rot:
64952c23cb7cSEd Maste 	case DW_OP_xderef:
64962c23cb7cSEd Maste 	case DW_OP_abs:
64972c23cb7cSEd Maste 	case DW_OP_and:
64982c23cb7cSEd Maste 	case DW_OP_div:
64992c23cb7cSEd Maste 	case DW_OP_minus:
65002c23cb7cSEd Maste 	case DW_OP_mod:
65012c23cb7cSEd Maste 	case DW_OP_mul:
65022c23cb7cSEd Maste 	case DW_OP_neg:
65032c23cb7cSEd Maste 	case DW_OP_not:
65042c23cb7cSEd Maste 	case DW_OP_or:
65052c23cb7cSEd Maste 	case DW_OP_plus:
65062c23cb7cSEd Maste 	case DW_OP_shl:
65072c23cb7cSEd Maste 	case DW_OP_shr:
65082c23cb7cSEd Maste 	case DW_OP_shra:
65092c23cb7cSEd Maste 	case DW_OP_xor:
65102c23cb7cSEd Maste 	case DW_OP_eq:
65112c23cb7cSEd Maste 	case DW_OP_ge:
65122c23cb7cSEd Maste 	case DW_OP_gt:
65132c23cb7cSEd Maste 	case DW_OP_le:
65142c23cb7cSEd Maste 	case DW_OP_lt:
65152c23cb7cSEd Maste 	case DW_OP_ne:
65162c23cb7cSEd Maste 	case DW_OP_nop:
6517cf781b2eSEd Maste 	case DW_OP_push_object_address:
6518cf781b2eSEd Maste 	case DW_OP_form_tls_address:
6519cf781b2eSEd Maste 	case DW_OP_call_frame_cfa:
6520cf781b2eSEd Maste 	case DW_OP_stack_value:
6521cf781b2eSEd Maste 	case DW_OP_GNU_push_tls_address:
6522cf781b2eSEd Maste 	case DW_OP_GNU_uninit:
65232c23cb7cSEd Maste 		break;
65242c23cb7cSEd Maste 
65252c23cb7cSEd Maste 	case DW_OP_const1u:
65262c23cb7cSEd Maste 	case DW_OP_pick:
65272c23cb7cSEd Maste 	case DW_OP_deref_size:
65282c23cb7cSEd Maste 	case DW_OP_xderef_size:
65292c23cb7cSEd Maste 	case DW_OP_const2u:
65302c23cb7cSEd Maste 	case DW_OP_bra:
65312c23cb7cSEd Maste 	case DW_OP_skip:
65322c23cb7cSEd Maste 	case DW_OP_const4u:
65332c23cb7cSEd Maste 	case DW_OP_const8u:
65342c23cb7cSEd Maste 	case DW_OP_constu:
65352c23cb7cSEd Maste 	case DW_OP_plus_uconst:
65362c23cb7cSEd Maste 	case DW_OP_regx:
65372c23cb7cSEd Maste 	case DW_OP_piece:
65382c23cb7cSEd Maste 		printf(": %ju", (uintmax_t)
65392c23cb7cSEd Maste 		    lr->lr_number);
65402c23cb7cSEd Maste 		break;
65412c23cb7cSEd Maste 
6542cf781b2eSEd Maste 	case DW_OP_const1s:
6543cf781b2eSEd Maste 	case DW_OP_const2s:
6544cf781b2eSEd Maste 	case DW_OP_const4s:
6545cf781b2eSEd Maste 	case DW_OP_const8s:
65462c23cb7cSEd Maste 	case DW_OP_consts:
6547cf781b2eSEd Maste 		printf(": %jd", (intmax_t)
6548cf781b2eSEd Maste 		    lr->lr_number);
6549cf781b2eSEd Maste 		break;
6550cf781b2eSEd Maste 
65512c23cb7cSEd Maste 	case DW_OP_breg0:
65522c23cb7cSEd Maste 	case DW_OP_breg1:
65532c23cb7cSEd Maste 	case DW_OP_breg2:
65542c23cb7cSEd Maste 	case DW_OP_breg3:
65552c23cb7cSEd Maste 	case DW_OP_breg4:
65562c23cb7cSEd Maste 	case DW_OP_breg5:
65572c23cb7cSEd Maste 	case DW_OP_breg6:
65582c23cb7cSEd Maste 	case DW_OP_breg7:
65592c23cb7cSEd Maste 	case DW_OP_breg8:
65602c23cb7cSEd Maste 	case DW_OP_breg9:
65612c23cb7cSEd Maste 	case DW_OP_breg10:
65622c23cb7cSEd Maste 	case DW_OP_breg11:
65632c23cb7cSEd Maste 	case DW_OP_breg12:
65642c23cb7cSEd Maste 	case DW_OP_breg13:
65652c23cb7cSEd Maste 	case DW_OP_breg14:
65662c23cb7cSEd Maste 	case DW_OP_breg15:
65672c23cb7cSEd Maste 	case DW_OP_breg16:
65682c23cb7cSEd Maste 	case DW_OP_breg17:
65692c23cb7cSEd Maste 	case DW_OP_breg18:
65702c23cb7cSEd Maste 	case DW_OP_breg19:
65712c23cb7cSEd Maste 	case DW_OP_breg20:
65722c23cb7cSEd Maste 	case DW_OP_breg21:
65732c23cb7cSEd Maste 	case DW_OP_breg22:
65742c23cb7cSEd Maste 	case DW_OP_breg23:
65752c23cb7cSEd Maste 	case DW_OP_breg24:
65762c23cb7cSEd Maste 	case DW_OP_breg25:
65772c23cb7cSEd Maste 	case DW_OP_breg26:
65782c23cb7cSEd Maste 	case DW_OP_breg27:
65792c23cb7cSEd Maste 	case DW_OP_breg28:
65802c23cb7cSEd Maste 	case DW_OP_breg29:
65812c23cb7cSEd Maste 	case DW_OP_breg30:
65822c23cb7cSEd Maste 	case DW_OP_breg31:
6583cf781b2eSEd Maste 		printf(" (%s): %jd",
6584cf781b2eSEd Maste 		    dwarf_regname(re, lr->lr_atom - DW_OP_breg0),
6585cf781b2eSEd Maste 		    (intmax_t) lr->lr_number);
6586cf781b2eSEd Maste 		break;
6587cf781b2eSEd Maste 
65882c23cb7cSEd Maste 	case DW_OP_fbreg:
65892c23cb7cSEd Maste 		printf(": %jd", (intmax_t)
65902c23cb7cSEd Maste 		    lr->lr_number);
65912c23cb7cSEd Maste 		break;
65922c23cb7cSEd Maste 
65932c23cb7cSEd Maste 	case DW_OP_bregx:
6594cf781b2eSEd Maste 		printf(": %ju (%s) %jd",
65952c23cb7cSEd Maste 		    (uintmax_t) lr->lr_number,
6596cf781b2eSEd Maste 		    dwarf_regname(re, (unsigned int) lr->lr_number),
65972c23cb7cSEd Maste 		    (intmax_t) lr->lr_number2);
65982c23cb7cSEd Maste 		break;
65992c23cb7cSEd Maste 
66002c23cb7cSEd Maste 	case DW_OP_addr:
6601cf781b2eSEd Maste 	case DW_OP_GNU_encoded_addr:
66022c23cb7cSEd Maste 		printf(": %#jx", (uintmax_t)
66032c23cb7cSEd Maste 		    lr->lr_number);
66042c23cb7cSEd Maste 		break;
6605cf781b2eSEd Maste 
6606cf781b2eSEd Maste 	case DW_OP_GNU_implicit_pointer:
6607cf781b2eSEd Maste 		printf(": <0x%jx> %jd", (uintmax_t) lr->lr_number,
6608cf781b2eSEd Maste 		    (intmax_t) lr->lr_number2);
6609cf781b2eSEd Maste 		break;
6610cf781b2eSEd Maste 
6611cf781b2eSEd Maste 	case DW_OP_implicit_value:
6612cf781b2eSEd Maste 		printf(": %ju byte block:", (uintmax_t) lr->lr_number);
6613cf781b2eSEd Maste 		b = (uint8_t *)(uintptr_t) lr->lr_number2;
6614cf781b2eSEd Maste 		for (i = 0; (Dwarf_Unsigned) i < lr->lr_number; i++)
6615cf781b2eSEd Maste 			printf(" %x", b[i]);
6616cf781b2eSEd Maste 		break;
6617cf781b2eSEd Maste 
6618cf781b2eSEd Maste 	case DW_OP_GNU_entry_value:
6619cf781b2eSEd Maste 		printf(": (");
6620cf781b2eSEd Maste 		dump_dwarf_block(re, (uint8_t *)(uintptr_t) lr->lr_number2,
6621cf781b2eSEd Maste 		    lr->lr_number);
6622cf781b2eSEd Maste 		putchar(')');
6623cf781b2eSEd Maste 		break;
6624cf781b2eSEd Maste 
6625cf781b2eSEd Maste 	case DW_OP_GNU_const_type:
6626cf781b2eSEd Maste 		printf(": <0x%jx> ", (uintmax_t) lr->lr_number);
6627cf781b2eSEd Maste 		b = (uint8_t *)(uintptr_t) lr->lr_number2;
6628cf781b2eSEd Maste 		n = *b;
6629cf781b2eSEd Maste 		for (i = 1; (uint8_t) i < n; i++)
6630cf781b2eSEd Maste 			printf(" %x", b[i]);
6631cf781b2eSEd Maste 		break;
6632cf781b2eSEd Maste 
6633cf781b2eSEd Maste 	case DW_OP_GNU_regval_type:
6634cf781b2eSEd Maste 		printf(": %ju (%s) <0x%jx>", (uintmax_t) lr->lr_number,
6635cf781b2eSEd Maste 		    dwarf_regname(re, (unsigned int) lr->lr_number),
6636cf781b2eSEd Maste 		    (uintmax_t) lr->lr_number2);
6637cf781b2eSEd Maste 		break;
6638cf781b2eSEd Maste 
6639cf781b2eSEd Maste 	case DW_OP_GNU_convert:
6640cf781b2eSEd Maste 	case DW_OP_GNU_deref_type:
6641cf781b2eSEd Maste 	case DW_OP_GNU_parameter_ref:
6642cf781b2eSEd Maste 	case DW_OP_GNU_reinterpret:
6643cf781b2eSEd Maste 		printf(": <0x%jx>", (uintmax_t) lr->lr_number);
6644cf781b2eSEd Maste 		break;
6645cf781b2eSEd Maste 
6646cf781b2eSEd Maste 	default:
6647cf781b2eSEd Maste 		break;
66482c23cb7cSEd Maste 	}
6649cf781b2eSEd Maste }
6650cf781b2eSEd Maste 
6651cf781b2eSEd Maste static void
6652cf781b2eSEd Maste dump_dwarf_block(struct readelf *re, uint8_t *b, Dwarf_Unsigned len)
6653cf781b2eSEd Maste {
6654cf781b2eSEd Maste 	Dwarf_Locdesc *llbuf;
6655cf781b2eSEd Maste 	Dwarf_Signed lcnt;
6656cf781b2eSEd Maste 	Dwarf_Error de;
6657cf781b2eSEd Maste 	int i;
6658cf781b2eSEd Maste 
6659cf781b2eSEd Maste 	if (dwarf_loclist_from_expr_b(re->dbg, b, len, re->cu_psize,
6660cf781b2eSEd Maste 	    re->cu_osize, re->cu_ver, &llbuf, &lcnt, &de) != DW_DLV_OK) {
6661cf781b2eSEd Maste 		warnx("dwarf_loclist_form_expr_b: %s", dwarf_errmsg(de));
6662cf781b2eSEd Maste 		return;
6663cf781b2eSEd Maste 	}
6664cf781b2eSEd Maste 
6665cf781b2eSEd Maste 	for (i = 0; (Dwarf_Half) i < llbuf->ld_cents; i++) {
6666cf781b2eSEd Maste 		dump_dwarf_loc(re, &llbuf->ld_s[i]);
6667cf781b2eSEd Maste 		if (i < llbuf->ld_cents - 1)
6668cf781b2eSEd Maste 			printf("; ");
6669cf781b2eSEd Maste 	}
6670cf781b2eSEd Maste 
6671cf781b2eSEd Maste 	dwarf_dealloc(re->dbg, llbuf->ld_s, DW_DLA_LOC_BLOCK);
6672cf781b2eSEd Maste 	dwarf_dealloc(re->dbg, llbuf, DW_DLA_LOCDESC);
6673cf781b2eSEd Maste }
6674cf781b2eSEd Maste 
6675cf781b2eSEd Maste static void
6676cf781b2eSEd Maste dump_dwarf_loclist(struct readelf *re)
6677cf781b2eSEd Maste {
6678cf781b2eSEd Maste 	Dwarf_Die die;
6679cf781b2eSEd Maste 	Dwarf_Locdesc **llbuf;
6680cf781b2eSEd Maste 	Dwarf_Unsigned lowpc;
6681cf781b2eSEd Maste 	Dwarf_Signed lcnt;
6682cf781b2eSEd Maste 	Dwarf_Half tag, version, pointer_size, off_size;
6683cf781b2eSEd Maste 	Dwarf_Error de;
66849817bae9SEd Maste 	struct loc_at *la_list, *left, *right, *la;
6685cb12a690SEd Maste 	size_t la_list_len, la_list_cap;
6686cb12a690SEd Maste 	unsigned int duplicates, k;
6687bee2765cSEd Maste 	int i, j, ret, has_content;
6688cf781b2eSEd Maste 
66899817bae9SEd Maste 	la_list_len = 0;
66909817bae9SEd Maste 	la_list_cap = 200;
66919817bae9SEd Maste 	if ((la_list = calloc(la_list_cap, sizeof(struct loc_at))) == NULL)
66929817bae9SEd Maste 		errx(EXIT_FAILURE, "calloc failed");
6693cf781b2eSEd Maste 	/* Search .debug_info section. */
6694cf781b2eSEd Maste 	while ((ret = dwarf_next_cu_header_b(re->dbg, NULL, &version, NULL,
6695cf781b2eSEd Maste 	    &pointer_size, &off_size, NULL, NULL, &de)) == DW_DLV_OK) {
6696cf781b2eSEd Maste 		set_cu_context(re, pointer_size, off_size, version);
6697cf781b2eSEd Maste 		die = NULL;
6698cf781b2eSEd Maste 		if (dwarf_siblingof(re->dbg, die, &die, &de) != DW_DLV_OK)
6699cf781b2eSEd Maste 			continue;
6700cf781b2eSEd Maste 		if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
6701cf781b2eSEd Maste 			warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
6702cf781b2eSEd Maste 			continue;
6703cf781b2eSEd Maste 		}
6704cf781b2eSEd Maste 		/* XXX: What about DW_TAG_partial_unit? */
6705cf781b2eSEd Maste 		lowpc = 0;
6706cf781b2eSEd Maste 		if (tag == DW_TAG_compile_unit) {
6707cf781b2eSEd Maste 			if (dwarf_attrval_unsigned(die, DW_AT_low_pc,
6708cf781b2eSEd Maste 			    &lowpc, &de) != DW_DLV_OK)
6709cf781b2eSEd Maste 				lowpc = 0;
6710cf781b2eSEd Maste 		}
6711cf781b2eSEd Maste 
6712cf781b2eSEd Maste 		/* Search attributes for reference to .debug_loc section. */
67139817bae9SEd Maste 		search_loclist_at(re, die, lowpc, &la_list,
67149817bae9SEd Maste 		    &la_list_len, &la_list_cap);
6715cf781b2eSEd Maste 	}
6716cf781b2eSEd Maste 	if (ret == DW_DLV_ERROR)
6717cf781b2eSEd Maste 		warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
6718cf781b2eSEd Maste 
6719cf781b2eSEd Maste 	/* Search .debug_types section. */
6720cf781b2eSEd Maste 	do {
6721cf781b2eSEd Maste 		while ((ret = dwarf_next_cu_header_c(re->dbg, 0, NULL,
6722cf781b2eSEd Maste 		    &version, NULL, &pointer_size, &off_size, NULL, NULL,
6723cf781b2eSEd Maste 		    NULL, NULL, &de)) == DW_DLV_OK) {
6724cf781b2eSEd Maste 			set_cu_context(re, pointer_size, off_size, version);
6725cf781b2eSEd Maste 			die = NULL;
6726cf781b2eSEd Maste 			if (dwarf_siblingof(re->dbg, die, &die, &de) !=
6727cf781b2eSEd Maste 			    DW_DLV_OK)
6728cf781b2eSEd Maste 				continue;
6729cf781b2eSEd Maste 			if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
6730cf781b2eSEd Maste 				warnx("dwarf_tag failed: %s",
6731cf781b2eSEd Maste 				    dwarf_errmsg(de));
6732cf781b2eSEd Maste 				continue;
6733cf781b2eSEd Maste 			}
6734cf781b2eSEd Maste 
6735cf781b2eSEd Maste 			lowpc = 0;
6736cf781b2eSEd Maste 			if (tag == DW_TAG_type_unit) {
6737cf781b2eSEd Maste 				if (dwarf_attrval_unsigned(die, DW_AT_low_pc,
6738cf781b2eSEd Maste 				    &lowpc, &de) != DW_DLV_OK)
6739cf781b2eSEd Maste 					lowpc = 0;
6740cf781b2eSEd Maste 			}
6741cf781b2eSEd Maste 
6742cf781b2eSEd Maste 			/*
6743cf781b2eSEd Maste 			 * Search attributes for reference to .debug_loc
6744cf781b2eSEd Maste 			 * section.
6745cf781b2eSEd Maste 			 */
67469817bae9SEd Maste 			search_loclist_at(re, die, lowpc, &la_list,
67479817bae9SEd Maste 			    &la_list_len, &la_list_cap);
6748cf781b2eSEd Maste 		}
6749cf781b2eSEd Maste 		if (ret == DW_DLV_ERROR)
6750cf781b2eSEd Maste 			warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
6751cf781b2eSEd Maste 	} while (dwarf_next_types_section(re->dbg, &de) == DW_DLV_OK);
6752cf781b2eSEd Maste 
67539817bae9SEd Maste 	if (la_list_len == 0) {
67549817bae9SEd Maste 		free(la_list);
6755cf781b2eSEd Maste 		return;
67569817bae9SEd Maste 	}
67579817bae9SEd Maste 
67589817bae9SEd Maste 	/* Sort la_list using loc_at_comparator. */
67599817bae9SEd Maste 	qsort(la_list, la_list_len, sizeof(struct loc_at), loc_at_comparator);
67609817bae9SEd Maste 
67619817bae9SEd Maste 	/* Get rid of the duplicates in la_list. */
67629817bae9SEd Maste 	duplicates = 0;
67639817bae9SEd Maste 	for (k = 1; k < la_list_len; ++k) {
67649817bae9SEd Maste 		left = &la_list[k - 1 - duplicates];
67659817bae9SEd Maste 		right = &la_list[k];
67669817bae9SEd Maste 
67679817bae9SEd Maste 		if (left->la_off == right->la_off)
67689817bae9SEd Maste 			duplicates++;
67699817bae9SEd Maste 		else
67709817bae9SEd Maste 			la_list[k - duplicates] = *right;
67719817bae9SEd Maste 	}
67729817bae9SEd Maste 	la_list_len -= duplicates;
6773cf781b2eSEd Maste 
6774bee2765cSEd Maste 	has_content = 0;
67759817bae9SEd Maste 	for (k = 0; k < la_list_len; ++k) {
67769817bae9SEd Maste 		la = &la_list[k];
6777bee2765cSEd Maste 		if ((ret = dwarf_loclist_n(la->la_at, &llbuf, &lcnt, &de)) !=
6778cf781b2eSEd Maste 		    DW_DLV_OK) {
6779bee2765cSEd Maste 			if (ret != DW_DLV_NO_ENTRY)
6780bee2765cSEd Maste 				warnx("dwarf_loclist_n failed: %s",
6781bee2765cSEd Maste 				    dwarf_errmsg(de));
6782cf781b2eSEd Maste 			continue;
6783cf781b2eSEd Maste 		}
6784bee2765cSEd Maste 		if (!has_content) {
6785bee2765cSEd Maste 			has_content = 1;
6786bee2765cSEd Maste 			printf("\nContents of section .debug_loc:\n");
6787bee2765cSEd Maste 			printf("    Offset   Begin    End      Expression\n");
6788bee2765cSEd Maste 		}
6789cf781b2eSEd Maste 		set_cu_context(re, la->la_cu_psize, la->la_cu_osize,
6790cf781b2eSEd Maste 		    la->la_cu_ver);
6791cf781b2eSEd Maste 		for (i = 0; i < lcnt; i++) {
6792839529caSEd Maste 			printf("    %8.8jx ", (uintmax_t) la->la_off);
6793cf781b2eSEd Maste 			if (llbuf[i]->ld_lopc == 0 && llbuf[i]->ld_hipc == 0) {
6794cf781b2eSEd Maste 				printf("<End of list>\n");
6795cf781b2eSEd Maste 				continue;
6796cf781b2eSEd Maste 			}
6797cf781b2eSEd Maste 
6798cf781b2eSEd Maste 			/* TODO: handle base selection entry. */
6799cf781b2eSEd Maste 
6800cf781b2eSEd Maste 			printf("%8.8jx %8.8jx ",
6801cf781b2eSEd Maste 			    (uintmax_t) (la->la_lowpc + llbuf[i]->ld_lopc),
6802cf781b2eSEd Maste 			    (uintmax_t) (la->la_lowpc + llbuf[i]->ld_hipc));
6803cf781b2eSEd Maste 
6804cf781b2eSEd Maste 			putchar('(');
6805cf781b2eSEd Maste 			for (j = 0; (Dwarf_Half) j < llbuf[i]->ld_cents; j++) {
6806cf781b2eSEd Maste 				dump_dwarf_loc(re, &llbuf[i]->ld_s[j]);
68072c23cb7cSEd Maste 				if (j < llbuf[i]->ld_cents - 1)
6808cf781b2eSEd Maste 					printf("; ");
68092c23cb7cSEd Maste 			}
68102c23cb7cSEd Maste 			putchar(')');
68112c23cb7cSEd Maste 
68122c23cb7cSEd Maste 			if (llbuf[i]->ld_lopc == llbuf[i]->ld_hipc)
68132c23cb7cSEd Maste 				printf(" (start == end)");
68142c23cb7cSEd Maste 			putchar('\n');
68152c23cb7cSEd Maste 		}
6816cf781b2eSEd Maste 		for (i = 0; i < lcnt; i++) {
6817cf781b2eSEd Maste 			dwarf_dealloc(re->dbg, llbuf[i]->ld_s,
6818cf781b2eSEd Maste 			    DW_DLA_LOC_BLOCK);
6819cf781b2eSEd Maste 			dwarf_dealloc(re->dbg, llbuf[i], DW_DLA_LOCDESC);
6820cf781b2eSEd Maste 		}
6821cf781b2eSEd Maste 		dwarf_dealloc(re->dbg, llbuf, DW_DLA_LIST);
68222c23cb7cSEd Maste 	}
6823bee2765cSEd Maste 
6824bee2765cSEd Maste 	if (!has_content)
6825bee2765cSEd Maste 		printf("\nSection '.debug_loc' has no debugging data.\n");
68269817bae9SEd Maste 
68279817bae9SEd Maste 	free(la_list);
68282c23cb7cSEd Maste }
68292c23cb7cSEd Maste 
68302c23cb7cSEd Maste /*
68312c23cb7cSEd Maste  * Retrieve a string using string table section index and the string offset.
68322c23cb7cSEd Maste  */
68332c23cb7cSEd Maste static const char*
68342c23cb7cSEd Maste get_string(struct readelf *re, int strtab, size_t off)
68352c23cb7cSEd Maste {
68362c23cb7cSEd Maste 	const char *name;
68372c23cb7cSEd Maste 
68382c23cb7cSEd Maste 	if ((name = elf_strptr(re->elf, strtab, off)) == NULL)
68392c23cb7cSEd Maste 		return ("");
68402c23cb7cSEd Maste 
68412c23cb7cSEd Maste 	return (name);
68422c23cb7cSEd Maste }
68432c23cb7cSEd Maste 
68442c23cb7cSEd Maste /*
68452c23cb7cSEd Maste  * Retrieve the name of a symbol using the section index of the symbol
68462c23cb7cSEd Maste  * table and the index of the symbol within that table.
68472c23cb7cSEd Maste  */
68482c23cb7cSEd Maste static const char *
68492c23cb7cSEd Maste get_symbol_name(struct readelf *re, int symtab, int i)
68502c23cb7cSEd Maste {
68512c23cb7cSEd Maste 	struct section	*s;
68522c23cb7cSEd Maste 	const char	*name;
68532c23cb7cSEd Maste 	GElf_Sym	 sym;
68542c23cb7cSEd Maste 	Elf_Data	*data;
68552c23cb7cSEd Maste 	int		 elferr;
68562c23cb7cSEd Maste 
68572c23cb7cSEd Maste 	s = &re->sl[symtab];
68582c23cb7cSEd Maste 	if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM)
68592c23cb7cSEd Maste 		return ("");
68602c23cb7cSEd Maste 	(void) elf_errno();
68612c23cb7cSEd Maste 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
68622c23cb7cSEd Maste 		elferr = elf_errno();
68632c23cb7cSEd Maste 		if (elferr != 0)
68642c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
68652c23cb7cSEd Maste 		return ("");
68662c23cb7cSEd Maste 	}
68672c23cb7cSEd Maste 	if (gelf_getsym(data, i, &sym) != &sym)
68682c23cb7cSEd Maste 		return ("");
68692c23cb7cSEd Maste 	/* Return section name for STT_SECTION symbol. */
6870b6b6f9ccSEd Maste 	if (GELF_ST_TYPE(sym.st_info) == STT_SECTION) {
6871b6b6f9ccSEd Maste 		if (sym.st_shndx < re->shnum &&
68722c23cb7cSEd Maste 		    re->sl[sym.st_shndx].name != NULL)
68732c23cb7cSEd Maste 			return (re->sl[sym.st_shndx].name);
6874b6b6f9ccSEd Maste 		return ("");
6875b6b6f9ccSEd Maste 	}
6876656f49f8SEd Maste 	if (s->link >= re->shnum ||
6877656f49f8SEd Maste 	    (name = elf_strptr(re->elf, s->link, sym.st_name)) == NULL)
68782c23cb7cSEd Maste 		return ("");
68792c23cb7cSEd Maste 
68802c23cb7cSEd Maste 	return (name);
68812c23cb7cSEd Maste }
68822c23cb7cSEd Maste 
68832c23cb7cSEd Maste static uint64_t
68842c23cb7cSEd Maste get_symbol_value(struct readelf *re, int symtab, int i)
68852c23cb7cSEd Maste {
68862c23cb7cSEd Maste 	struct section	*s;
68872c23cb7cSEd Maste 	GElf_Sym	 sym;
68882c23cb7cSEd Maste 	Elf_Data	*data;
68892c23cb7cSEd Maste 	int		 elferr;
68902c23cb7cSEd Maste 
68912c23cb7cSEd Maste 	s = &re->sl[symtab];
68922c23cb7cSEd Maste 	if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM)
68932c23cb7cSEd Maste 		return (0);
68942c23cb7cSEd Maste 	(void) elf_errno();
68952c23cb7cSEd Maste 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
68962c23cb7cSEd Maste 		elferr = elf_errno();
68972c23cb7cSEd Maste 		if (elferr != 0)
68982c23cb7cSEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
68992c23cb7cSEd Maste 		return (0);
69002c23cb7cSEd Maste 	}
69012c23cb7cSEd Maste 	if (gelf_getsym(data, i, &sym) != &sym)
69022c23cb7cSEd Maste 		return (0);
69032c23cb7cSEd Maste 
69042c23cb7cSEd Maste 	return (sym.st_value);
69052c23cb7cSEd Maste }
69062c23cb7cSEd Maste 
6907*e128bd0fSEd Maste /*
6908*e128bd0fSEd Maste  * Decompress a data section if needed (using ZLIB).
6909*e128bd0fSEd Maste  * Returns true if sucessful, false otherwise.
6910*e128bd0fSEd Maste  */
6911*e128bd0fSEd Maste static bool decompress_section(struct section *s,
6912*e128bd0fSEd Maste     unsigned char *compressed_data_buffer, uint64_t compressed_size,
6913*e128bd0fSEd Maste     unsigned char **ret_buf, uint64_t *ret_sz)
6914*e128bd0fSEd Maste {
6915*e128bd0fSEd Maste 	GElf_Shdr sh;
6916*e128bd0fSEd Maste 
6917*e128bd0fSEd Maste 	if (gelf_getshdr(s->scn, &sh) == NULL)
6918*e128bd0fSEd Maste 		errx(EXIT_FAILURE, "gelf_getshdr() failed: %s", elf_errmsg(-1));
6919*e128bd0fSEd Maste 
6920*e128bd0fSEd Maste 	if (sh.sh_flags & SHF_COMPRESSED) {
6921*e128bd0fSEd Maste 		int ret;
6922*e128bd0fSEd Maste 		GElf_Chdr chdr;
6923*e128bd0fSEd Maste 		Elf64_Xword inflated_size;
6924*e128bd0fSEd Maste 		unsigned char *uncompressed_data_buffer = NULL;
6925*e128bd0fSEd Maste 		Elf64_Xword uncompressed_size;
6926*e128bd0fSEd Maste 		z_stream strm;
6927*e128bd0fSEd Maste 
6928*e128bd0fSEd Maste 		if (gelf_getchdr(s->scn, &chdr) == NULL)
6929*e128bd0fSEd Maste 			errx(EXIT_FAILURE, "gelf_getchdr() failed: %s", elf_errmsg(-1));
6930*e128bd0fSEd Maste 		if (chdr.ch_type != ELFCOMPRESS_ZLIB) {
6931*e128bd0fSEd Maste 			warnx("unknown compression type: %d", chdr.ch_type);
6932*e128bd0fSEd Maste 			return (false);
6933*e128bd0fSEd Maste 		}
6934*e128bd0fSEd Maste 
6935*e128bd0fSEd Maste 		inflated_size = 0;
6936*e128bd0fSEd Maste 		uncompressed_size = chdr.ch_size;
6937*e128bd0fSEd Maste 		uncompressed_data_buffer = malloc(uncompressed_size);
6938*e128bd0fSEd Maste 		compressed_data_buffer += sizeof(chdr);
6939*e128bd0fSEd Maste 		compressed_size -= sizeof(chdr);
6940*e128bd0fSEd Maste 
6941*e128bd0fSEd Maste 		strm.zalloc = Z_NULL;
6942*e128bd0fSEd Maste 		strm.zfree = Z_NULL;
6943*e128bd0fSEd Maste 		strm.opaque = Z_NULL;
6944*e128bd0fSEd Maste 		strm.avail_in = compressed_size;
6945*e128bd0fSEd Maste 		strm.avail_out = uncompressed_size;
6946*e128bd0fSEd Maste 		ret = inflateInit(&strm);
6947*e128bd0fSEd Maste 
6948*e128bd0fSEd Maste 		if (ret != Z_OK)
6949*e128bd0fSEd Maste 			goto fail;
6950*e128bd0fSEd Maste 		/*
6951*e128bd0fSEd Maste 		 * The section can contain several compressed buffers,
6952*e128bd0fSEd Maste 		 * so decompress in a loop until all data is inflated.
6953*e128bd0fSEd Maste 		 */
6954*e128bd0fSEd Maste 		while (inflated_size < compressed_size) {
6955*e128bd0fSEd Maste 			strm.next_in = compressed_data_buffer + inflated_size;
6956*e128bd0fSEd Maste 			strm.next_out = uncompressed_data_buffer + inflated_size;
6957*e128bd0fSEd Maste 			ret = inflate(&strm, Z_FINISH);
6958*e128bd0fSEd Maste 			if (ret != Z_STREAM_END)
6959*e128bd0fSEd Maste 				goto fail;
6960*e128bd0fSEd Maste 			inflated_size = uncompressed_size - strm.avail_out;
6961*e128bd0fSEd Maste 			ret = inflateReset(&strm);
6962*e128bd0fSEd Maste 			if (ret != Z_OK)
6963*e128bd0fSEd Maste 				goto fail;
6964*e128bd0fSEd Maste 		}
6965*e128bd0fSEd Maste 		if (strm.avail_out != 0)
6966*e128bd0fSEd Maste 			warnx("Warning: wrong info in compression header.");
6967*e128bd0fSEd Maste 		ret = inflateEnd(&strm);
6968*e128bd0fSEd Maste 		if (ret != Z_OK)
6969*e128bd0fSEd Maste 			goto fail;
6970*e128bd0fSEd Maste 		*ret_buf = uncompressed_data_buffer;
6971*e128bd0fSEd Maste 		*ret_sz = uncompressed_size;
6972*e128bd0fSEd Maste 		return (true);
6973*e128bd0fSEd Maste fail:
6974*e128bd0fSEd Maste 		inflateEnd(&strm);
6975*e128bd0fSEd Maste 		if (strm.msg)
6976*e128bd0fSEd Maste 			warnx("%s", strm.msg);
6977*e128bd0fSEd Maste 		else
6978*e128bd0fSEd Maste 			warnx("ZLIB error: %d", ret);
6979*e128bd0fSEd Maste 		free(uncompressed_data_buffer);
6980*e128bd0fSEd Maste 		return (false);
6981*e128bd0fSEd Maste 	}
6982*e128bd0fSEd Maste 	return (false);
6983*e128bd0fSEd Maste }
6984*e128bd0fSEd Maste 
69852c23cb7cSEd Maste static void
69862c23cb7cSEd Maste hex_dump(struct readelf *re)
69872c23cb7cSEd Maste {
69882c23cb7cSEd Maste 	struct section *s;
69892c23cb7cSEd Maste 	Elf_Data *d;
6990*e128bd0fSEd Maste 	uint8_t *buf, *new_buf;
69912c23cb7cSEd Maste 	size_t sz, nbytes;
69922c23cb7cSEd Maste 	uint64_t addr;
69932c23cb7cSEd Maste 	int elferr, i, j;
69942c23cb7cSEd Maste 
69952c23cb7cSEd Maste 	for (i = 1; (size_t) i < re->shnum; i++) {
6996*e128bd0fSEd Maste 		new_buf = NULL;
69972c23cb7cSEd Maste 		s = &re->sl[i];
69982c23cb7cSEd Maste 		if (find_dumpop(re, (size_t) i, s->name, HEX_DUMP, -1) == NULL)
69992c23cb7cSEd Maste 			continue;
70002c23cb7cSEd Maste 		(void) elf_errno();
7001839529caSEd Maste 		if ((d = elf_getdata(s->scn, NULL)) == NULL &&
7002839529caSEd Maste 		    (d = elf_rawdata(s->scn, NULL)) == NULL) {
70032c23cb7cSEd Maste 			elferr = elf_errno();
70042c23cb7cSEd Maste 			if (elferr != 0)
70052c23cb7cSEd Maste 				warnx("elf_getdata failed: %s",
70062c23cb7cSEd Maste 				    elf_errmsg(elferr));
70072c23cb7cSEd Maste 			continue;
70082c23cb7cSEd Maste 		}
7009839529caSEd Maste 		(void) elf_errno();
70102c23cb7cSEd Maste 		if (d->d_size <= 0 || d->d_buf == NULL) {
70112c23cb7cSEd Maste 			printf("\nSection '%s' has no data to dump.\n",
70122c23cb7cSEd Maste 			    s->name);
70132c23cb7cSEd Maste 			continue;
70142c23cb7cSEd Maste 		}
70152c23cb7cSEd Maste 		buf = d->d_buf;
70162c23cb7cSEd Maste 		sz = d->d_size;
70172c23cb7cSEd Maste 		addr = s->addr;
7018*e128bd0fSEd Maste 		if (re->options & RE_Z) {
7019*e128bd0fSEd Maste 			if (decompress_section(s, d->d_buf, d->d_size,
7020*e128bd0fSEd Maste 			    &new_buf, &sz))
7021*e128bd0fSEd Maste 				buf = new_buf;
7022*e128bd0fSEd Maste 		}
70232c23cb7cSEd Maste 		printf("\nHex dump of section '%s':\n", s->name);
70242c23cb7cSEd Maste 		while (sz > 0) {
70252c23cb7cSEd Maste 			printf("  0x%8.8jx ", (uintmax_t)addr);
70262c23cb7cSEd Maste 			nbytes = sz > 16? 16 : sz;
70272c23cb7cSEd Maste 			for (j = 0; j < 16; j++) {
70282c23cb7cSEd Maste 				if ((size_t)j < nbytes)
70292c23cb7cSEd Maste 					printf("%2.2x", buf[j]);
70302c23cb7cSEd Maste 				else
70312c23cb7cSEd Maste 					printf("  ");
70322c23cb7cSEd Maste 				if ((j & 3) == 3)
70332c23cb7cSEd Maste 					printf(" ");
70342c23cb7cSEd Maste 			}
70352c23cb7cSEd Maste 			for (j = 0; (size_t)j < nbytes; j++) {
70362c23cb7cSEd Maste 				if (isprint(buf[j]))
70372c23cb7cSEd Maste 					printf("%c", buf[j]);
70382c23cb7cSEd Maste 				else
70392c23cb7cSEd Maste 					printf(".");
70402c23cb7cSEd Maste 			}
70412c23cb7cSEd Maste 			printf("\n");
70422c23cb7cSEd Maste 			buf += nbytes;
70432c23cb7cSEd Maste 			addr += nbytes;
70442c23cb7cSEd Maste 			sz -= nbytes;
70452c23cb7cSEd Maste 		}
7046*e128bd0fSEd Maste 		free(new_buf);
70472c23cb7cSEd Maste 	}
70482c23cb7cSEd Maste }
70492c23cb7cSEd Maste 
70502c23cb7cSEd Maste static void
70512c23cb7cSEd Maste str_dump(struct readelf *re)
70522c23cb7cSEd Maste {
70532c23cb7cSEd Maste 	struct section *s;
70542c23cb7cSEd Maste 	Elf_Data *d;
7055*e128bd0fSEd Maste 	unsigned char *start, *end, *buf_end, *new_buf;
70562c23cb7cSEd Maste 	unsigned int len;
7057*e128bd0fSEd Maste 	size_t sz;
70582c23cb7cSEd Maste 	int i, j, elferr, found;
70592c23cb7cSEd Maste 
70602c23cb7cSEd Maste 	for (i = 1; (size_t) i < re->shnum; i++) {
7061*e128bd0fSEd Maste 		new_buf = NULL;
70622c23cb7cSEd Maste 		s = &re->sl[i];
70632c23cb7cSEd Maste 		if (find_dumpop(re, (size_t) i, s->name, STR_DUMP, -1) == NULL)
70642c23cb7cSEd Maste 			continue;
70652c23cb7cSEd Maste 		(void) elf_errno();
7066839529caSEd Maste 		if ((d = elf_getdata(s->scn, NULL)) == NULL &&
7067839529caSEd Maste 		    (d = elf_rawdata(s->scn, NULL)) == NULL) {
70682c23cb7cSEd Maste 			elferr = elf_errno();
70692c23cb7cSEd Maste 			if (elferr != 0)
70702c23cb7cSEd Maste 				warnx("elf_getdata failed: %s",
70712c23cb7cSEd Maste 				    elf_errmsg(elferr));
70722c23cb7cSEd Maste 			continue;
70732c23cb7cSEd Maste 		}
7074839529caSEd Maste 		(void) elf_errno();
70752c23cb7cSEd Maste 		if (d->d_size <= 0 || d->d_buf == NULL) {
70762c23cb7cSEd Maste 			printf("\nSection '%s' has no data to dump.\n",
70772c23cb7cSEd Maste 			    s->name);
70782c23cb7cSEd Maste 			continue;
70792c23cb7cSEd Maste 		}
70802c23cb7cSEd Maste 		found = 0;
7081*e128bd0fSEd Maste 		start = d->d_buf;
7082*e128bd0fSEd Maste 		sz = d->d_size;
7083*e128bd0fSEd Maste 		if (re->options & RE_Z) {
7084*e128bd0fSEd Maste 			if (decompress_section(s, d->d_buf, d->d_size,
7085*e128bd0fSEd Maste 			    &new_buf, &sz))
7086*e128bd0fSEd Maste 				start = new_buf;
7087*e128bd0fSEd Maste 		}
7088*e128bd0fSEd Maste 		buf_end = start + sz;
70892c23cb7cSEd Maste 		printf("\nString dump of section '%s':\n", s->name);
70902c23cb7cSEd Maste 		for (;;) {
70912c23cb7cSEd Maste 			while (start < buf_end && !isprint(*start))
70922c23cb7cSEd Maste 				start++;
70932c23cb7cSEd Maste 			if (start >= buf_end)
70942c23cb7cSEd Maste 				break;
70952c23cb7cSEd Maste 			end = start + 1;
70962c23cb7cSEd Maste 			while (end < buf_end && isprint(*end))
70972c23cb7cSEd Maste 				end++;
70982c23cb7cSEd Maste 			printf("  [%6lx]  ",
70992c23cb7cSEd Maste 			    (long) (start - (unsigned char *) d->d_buf));
71002c23cb7cSEd Maste 			len = end - start;
71012c23cb7cSEd Maste 			for (j = 0; (unsigned int) j < len; j++)
71022c23cb7cSEd Maste 				putchar(start[j]);
71032c23cb7cSEd Maste 			putchar('\n');
71042c23cb7cSEd Maste 			found = 1;
71052c23cb7cSEd Maste 			if (end >= buf_end)
71062c23cb7cSEd Maste 				break;
71072c23cb7cSEd Maste 			start = end + 1;
71082c23cb7cSEd Maste 		}
7109*e128bd0fSEd Maste 		free(new_buf);
71102c23cb7cSEd Maste 		if (!found)
71112c23cb7cSEd Maste 			printf("  No strings found in this section.");
71122c23cb7cSEd Maste 		putchar('\n');
71132c23cb7cSEd Maste 	}
71142c23cb7cSEd Maste }
71152c23cb7cSEd Maste 
71162c23cb7cSEd Maste static void
71172c23cb7cSEd Maste load_sections(struct readelf *re)
71182c23cb7cSEd Maste {
71192c23cb7cSEd Maste 	struct section	*s;
71202c23cb7cSEd Maste 	const char	*name;
71212c23cb7cSEd Maste 	Elf_Scn		*scn;
71222c23cb7cSEd Maste 	GElf_Shdr	 sh;
71232c23cb7cSEd Maste 	size_t		 shstrndx, ndx;
71242c23cb7cSEd Maste 	int		 elferr;
71252c23cb7cSEd Maste 
71262c23cb7cSEd Maste 	/* Allocate storage for internal section list. */
71272c23cb7cSEd Maste 	if (!elf_getshnum(re->elf, &re->shnum)) {
71282c23cb7cSEd Maste 		warnx("elf_getshnum failed: %s", elf_errmsg(-1));
71292c23cb7cSEd Maste 		return;
71302c23cb7cSEd Maste 	}
71312c23cb7cSEd Maste 	if (re->sl != NULL)
71322c23cb7cSEd Maste 		free(re->sl);
71332c23cb7cSEd Maste 	if ((re->sl = calloc(re->shnum, sizeof(*re->sl))) == NULL)
71342c23cb7cSEd Maste 		err(EXIT_FAILURE, "calloc failed");
71352c23cb7cSEd Maste 
71362c23cb7cSEd Maste 	/* Get the index of .shstrtab section. */
71372c23cb7cSEd Maste 	if (!elf_getshstrndx(re->elf, &shstrndx)) {
71382c23cb7cSEd Maste 		warnx("elf_getshstrndx failed: %s", elf_errmsg(-1));
71392c23cb7cSEd Maste 		return;
71402c23cb7cSEd Maste 	}
71412c23cb7cSEd Maste 
714271a0c925SEd Maste 	if ((scn = elf_getscn(re->elf, 0)) == NULL)
71432c23cb7cSEd Maste 		return;
71442c23cb7cSEd Maste 
71452c23cb7cSEd Maste 	(void) elf_errno();
71462c23cb7cSEd Maste 	do {
71472c23cb7cSEd Maste 		if (gelf_getshdr(scn, &sh) == NULL) {
71482c23cb7cSEd Maste 			warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
71492c23cb7cSEd Maste 			(void) elf_errno();
71502c23cb7cSEd Maste 			continue;
71512c23cb7cSEd Maste 		}
71522c23cb7cSEd Maste 		if ((name = elf_strptr(re->elf, shstrndx, sh.sh_name)) == NULL) {
71532c23cb7cSEd Maste 			(void) elf_errno();
71547dc45d65SConrad Meyer 			name = "<no-name>";
71552c23cb7cSEd Maste 		}
71562c23cb7cSEd Maste 		if ((ndx = elf_ndxscn(scn)) == SHN_UNDEF) {
71577dc45d65SConrad Meyer 			if ((elferr = elf_errno()) != 0) {
71582c23cb7cSEd Maste 				warnx("elf_ndxscn failed: %s",
71592c23cb7cSEd Maste 				    elf_errmsg(elferr));
71602c23cb7cSEd Maste 				continue;
71612c23cb7cSEd Maste 			}
71627dc45d65SConrad Meyer 		}
71632c23cb7cSEd Maste 		if (ndx >= re->shnum) {
71642c23cb7cSEd Maste 			warnx("section index of '%s' out of range", name);
71652c23cb7cSEd Maste 			continue;
71662c23cb7cSEd Maste 		}
7167656f49f8SEd Maste 		if (sh.sh_link >= re->shnum)
7168656f49f8SEd Maste 			warnx("section link %llu of '%s' out of range",
7169656f49f8SEd Maste 			    (unsigned long long)sh.sh_link, name);
71702c23cb7cSEd Maste 		s = &re->sl[ndx];
71712c23cb7cSEd Maste 		s->name = name;
71722c23cb7cSEd Maste 		s->scn = scn;
71732c23cb7cSEd Maste 		s->off = sh.sh_offset;
71742c23cb7cSEd Maste 		s->sz = sh.sh_size;
71752c23cb7cSEd Maste 		s->entsize = sh.sh_entsize;
71762c23cb7cSEd Maste 		s->align = sh.sh_addralign;
71772c23cb7cSEd Maste 		s->type = sh.sh_type;
71782c23cb7cSEd Maste 		s->flags = sh.sh_flags;
71792c23cb7cSEd Maste 		s->addr = sh.sh_addr;
71802c23cb7cSEd Maste 		s->link = sh.sh_link;
71812c23cb7cSEd Maste 		s->info = sh.sh_info;
71822c23cb7cSEd Maste 	} while ((scn = elf_nextscn(re->elf, scn)) != NULL);
71832c23cb7cSEd Maste 	elferr = elf_errno();
71842c23cb7cSEd Maste 	if (elferr != 0)
71852c23cb7cSEd Maste 		warnx("elf_nextscn failed: %s", elf_errmsg(elferr));
71862c23cb7cSEd Maste }
71872c23cb7cSEd Maste 
71882c23cb7cSEd Maste static void
71892c23cb7cSEd Maste unload_sections(struct readelf *re)
71902c23cb7cSEd Maste {
71912c23cb7cSEd Maste 
71922c23cb7cSEd Maste 	if (re->sl != NULL) {
71932c23cb7cSEd Maste 		free(re->sl);
71942c23cb7cSEd Maste 		re->sl = NULL;
71952c23cb7cSEd Maste 	}
71962c23cb7cSEd Maste 	re->shnum = 0;
71972c23cb7cSEd Maste 	re->vd_s = NULL;
71982c23cb7cSEd Maste 	re->vn_s = NULL;
71992c23cb7cSEd Maste 	re->vs_s = NULL;
72002c23cb7cSEd Maste 	re->vs = NULL;
72012c23cb7cSEd Maste 	re->vs_sz = 0;
72022c23cb7cSEd Maste 	if (re->ver != NULL) {
72032c23cb7cSEd Maste 		free(re->ver);
72042c23cb7cSEd Maste 		re->ver = NULL;
72052c23cb7cSEd Maste 		re->ver_sz = 0;
72062c23cb7cSEd Maste 	}
72072c23cb7cSEd Maste }
72082c23cb7cSEd Maste 
72092c23cb7cSEd Maste static void
72102c23cb7cSEd Maste dump_elf(struct readelf *re)
72112c23cb7cSEd Maste {
72122c23cb7cSEd Maste 
72132c23cb7cSEd Maste 	/* Fetch ELF header. No need to continue if it fails. */
72142c23cb7cSEd Maste 	if (gelf_getehdr(re->elf, &re->ehdr) == NULL) {
72152c23cb7cSEd Maste 		warnx("gelf_getehdr failed: %s", elf_errmsg(-1));
72162c23cb7cSEd Maste 		return;
72172c23cb7cSEd Maste 	}
72182c23cb7cSEd Maste 	if ((re->ec = gelf_getclass(re->elf)) == ELFCLASSNONE) {
72192c23cb7cSEd Maste 		warnx("gelf_getclass failed: %s", elf_errmsg(-1));
72202c23cb7cSEd Maste 		return;
72212c23cb7cSEd Maste 	}
72222c23cb7cSEd Maste 	if (re->ehdr.e_ident[EI_DATA] == ELFDATA2MSB) {
72232c23cb7cSEd Maste 		re->dw_read = _read_msb;
72242c23cb7cSEd Maste 		re->dw_decode = _decode_msb;
72252c23cb7cSEd Maste 	} else {
72262c23cb7cSEd Maste 		re->dw_read = _read_lsb;
72272c23cb7cSEd Maste 		re->dw_decode = _decode_lsb;
72282c23cb7cSEd Maste 	}
72292c23cb7cSEd Maste 
72302c23cb7cSEd Maste 	if (re->options & ~RE_H)
72312c23cb7cSEd Maste 		load_sections(re);
72322c23cb7cSEd Maste 	if ((re->options & RE_VV) || (re->options & RE_S))
72332c23cb7cSEd Maste 		search_ver(re);
72342c23cb7cSEd Maste 	if (re->options & RE_H)
72352c23cb7cSEd Maste 		dump_ehdr(re);
72362c23cb7cSEd Maste 	if (re->options & RE_L)
72372c23cb7cSEd Maste 		dump_phdr(re);
72382c23cb7cSEd Maste 	if (re->options & RE_SS)
72392c23cb7cSEd Maste 		dump_shdr(re);
72403ef90571SEd Maste 	if (re->options & RE_G)
72413ef90571SEd Maste 		dump_section_groups(re);
72422c23cb7cSEd Maste 	if (re->options & RE_D)
72432c23cb7cSEd Maste 		dump_dynamic(re);
72442c23cb7cSEd Maste 	if (re->options & RE_R)
72452c23cb7cSEd Maste 		dump_reloc(re);
72462c23cb7cSEd Maste 	if (re->options & RE_S)
72472c23cb7cSEd Maste 		dump_symtabs(re);
72482c23cb7cSEd Maste 	if (re->options & RE_N)
72492c23cb7cSEd Maste 		dump_notes(re);
72502c23cb7cSEd Maste 	if (re->options & RE_II)
72512c23cb7cSEd Maste 		dump_hash(re);
72522c23cb7cSEd Maste 	if (re->options & RE_X)
72532c23cb7cSEd Maste 		hex_dump(re);
72542c23cb7cSEd Maste 	if (re->options & RE_P)
72552c23cb7cSEd Maste 		str_dump(re);
72562c23cb7cSEd Maste 	if (re->options & RE_VV)
72572c23cb7cSEd Maste 		dump_ver(re);
72582c23cb7cSEd Maste 	if (re->options & RE_AA)
72592c23cb7cSEd Maste 		dump_arch_specific_info(re);
72602c23cb7cSEd Maste 	if (re->options & RE_W)
72612c23cb7cSEd Maste 		dump_dwarf(re);
72622c23cb7cSEd Maste 	if (re->options & ~RE_H)
72632c23cb7cSEd Maste 		unload_sections(re);
72642c23cb7cSEd Maste }
72652c23cb7cSEd Maste 
72662c23cb7cSEd Maste static void
72672c23cb7cSEd Maste dump_dwarf(struct readelf *re)
72682c23cb7cSEd Maste {
72692c23cb7cSEd Maste 	Dwarf_Error de;
7270bee2765cSEd Maste 	int error;
72712c23cb7cSEd Maste 
72722c23cb7cSEd Maste 	if (dwarf_elf_init(re->elf, DW_DLC_READ, NULL, NULL, &re->dbg, &de)) {
72732c23cb7cSEd Maste 		if ((error = dwarf_errno(de)) != DW_DLE_DEBUG_INFO_NULL)
72742c23cb7cSEd Maste 			errx(EXIT_FAILURE, "dwarf_elf_init failed: %s",
72752c23cb7cSEd Maste 			    dwarf_errmsg(de));
72762c23cb7cSEd Maste 		return;
72772c23cb7cSEd Maste 	}
72782c23cb7cSEd Maste 
72792c23cb7cSEd Maste 	if (re->dop & DW_A)
72802c23cb7cSEd Maste 		dump_dwarf_abbrev(re);
72812c23cb7cSEd Maste 	if (re->dop & DW_L)
72822c23cb7cSEd Maste 		dump_dwarf_line(re);
72832c23cb7cSEd Maste 	if (re->dop & DW_LL)
72842c23cb7cSEd Maste 		dump_dwarf_line_decoded(re);
7285cf781b2eSEd Maste 	if (re->dop & DW_I) {
7286cf781b2eSEd Maste 		dump_dwarf_info(re, 0);
7287cf781b2eSEd Maste 		dump_dwarf_info(re, 1);
7288cf781b2eSEd Maste 	}
72892c23cb7cSEd Maste 	if (re->dop & DW_P)
72902c23cb7cSEd Maste 		dump_dwarf_pubnames(re);
72912c23cb7cSEd Maste 	if (re->dop & DW_R)
72922c23cb7cSEd Maste 		dump_dwarf_aranges(re);
72932c23cb7cSEd Maste 	if (re->dop & DW_RR)
72942c23cb7cSEd Maste 		dump_dwarf_ranges(re);
72952c23cb7cSEd Maste 	if (re->dop & DW_M)
72962c23cb7cSEd Maste 		dump_dwarf_macinfo(re);
72972c23cb7cSEd Maste 	if (re->dop & DW_F)
72982c23cb7cSEd Maste 		dump_dwarf_frame(re, 0);
72992c23cb7cSEd Maste 	else if (re->dop & DW_FF)
73002c23cb7cSEd Maste 		dump_dwarf_frame(re, 1);
73012c23cb7cSEd Maste 	if (re->dop & DW_S)
73022c23cb7cSEd Maste 		dump_dwarf_str(re);
73032c23cb7cSEd Maste 	if (re->dop & DW_O)
73042c23cb7cSEd Maste 		dump_dwarf_loclist(re);
73052c23cb7cSEd Maste 
73062c23cb7cSEd Maste 	dwarf_finish(re->dbg, &de);
73072c23cb7cSEd Maste }
73082c23cb7cSEd Maste 
73092c23cb7cSEd Maste static void
73102c23cb7cSEd Maste dump_ar(struct readelf *re, int fd)
73112c23cb7cSEd Maste {
73122c23cb7cSEd Maste 	Elf_Arsym *arsym;
73132c23cb7cSEd Maste 	Elf_Arhdr *arhdr;
73142c23cb7cSEd Maste 	Elf_Cmd cmd;
73152c23cb7cSEd Maste 	Elf *e;
73162c23cb7cSEd Maste 	size_t sz;
73172c23cb7cSEd Maste 	off_t off;
73182c23cb7cSEd Maste 	int i;
73192c23cb7cSEd Maste 
73202c23cb7cSEd Maste 	re->ar = re->elf;
73212c23cb7cSEd Maste 
73222c23cb7cSEd Maste 	if (re->options & RE_C) {
73232c23cb7cSEd Maste 		if ((arsym = elf_getarsym(re->ar, &sz)) == NULL) {
73242c23cb7cSEd Maste 			warnx("elf_getarsym() failed: %s", elf_errmsg(-1));
73252c23cb7cSEd Maste 			goto process_members;
73262c23cb7cSEd Maste 		}
73272c23cb7cSEd Maste 		printf("Index of archive %s: (%ju entries)\n", re->filename,
73282c23cb7cSEd Maste 		    (uintmax_t) sz - 1);
73292c23cb7cSEd Maste 		off = 0;
73302c23cb7cSEd Maste 		for (i = 0; (size_t) i < sz; i++) {
73312c23cb7cSEd Maste 			if (arsym[i].as_name == NULL)
73322c23cb7cSEd Maste 				break;
73332c23cb7cSEd Maste 			if (arsym[i].as_off != off) {
73342c23cb7cSEd Maste 				off = arsym[i].as_off;
73352c23cb7cSEd Maste 				if (elf_rand(re->ar, off) != off) {
73362c23cb7cSEd Maste 					warnx("elf_rand() failed: %s",
73372c23cb7cSEd Maste 					    elf_errmsg(-1));
73382c23cb7cSEd Maste 					continue;
73392c23cb7cSEd Maste 				}
73402c23cb7cSEd Maste 				if ((e = elf_begin(fd, ELF_C_READ, re->ar)) ==
73412c23cb7cSEd Maste 				    NULL) {
73422c23cb7cSEd Maste 					warnx("elf_begin() failed: %s",
73432c23cb7cSEd Maste 					    elf_errmsg(-1));
73442c23cb7cSEd Maste 					continue;
73452c23cb7cSEd Maste 				}
73462c23cb7cSEd Maste 				if ((arhdr = elf_getarhdr(e)) == NULL) {
73472c23cb7cSEd Maste 					warnx("elf_getarhdr() failed: %s",
73482c23cb7cSEd Maste 					    elf_errmsg(-1));
73492c23cb7cSEd Maste 					elf_end(e);
73502c23cb7cSEd Maste 					continue;
73512c23cb7cSEd Maste 				}
73522c23cb7cSEd Maste 				printf("Binary %s(%s) contains:\n",
73532c23cb7cSEd Maste 				    re->filename, arhdr->ar_name);
7354c37c6b38SMark Johnston 				elf_end(e);
73552c23cb7cSEd Maste 			}
73562c23cb7cSEd Maste 			printf("\t%s\n", arsym[i].as_name);
73572c23cb7cSEd Maste 		}
73582c23cb7cSEd Maste 		if (elf_rand(re->ar, SARMAG) != SARMAG) {
73592c23cb7cSEd Maste 			warnx("elf_rand() failed: %s", elf_errmsg(-1));
73602c23cb7cSEd Maste 			return;
73612c23cb7cSEd Maste 		}
73622c23cb7cSEd Maste 	}
73632c23cb7cSEd Maste 
73642c23cb7cSEd Maste process_members:
73652c23cb7cSEd Maste 
73662c23cb7cSEd Maste 	if ((re->options & ~RE_C) == 0)
73672c23cb7cSEd Maste 		return;
73682c23cb7cSEd Maste 
73692c23cb7cSEd Maste 	cmd = ELF_C_READ;
73702c23cb7cSEd Maste 	while ((re->elf = elf_begin(fd, cmd, re->ar)) != NULL) {
73712c23cb7cSEd Maste 		if ((arhdr = elf_getarhdr(re->elf)) == NULL) {
73722c23cb7cSEd Maste 			warnx("elf_getarhdr() failed: %s", elf_errmsg(-1));
73732c23cb7cSEd Maste 			goto next_member;
73742c23cb7cSEd Maste 		}
73752c23cb7cSEd Maste 		if (strcmp(arhdr->ar_name, "/") == 0 ||
73762c23cb7cSEd Maste 		    strcmp(arhdr->ar_name, "//") == 0 ||
73772c23cb7cSEd Maste 		    strcmp(arhdr->ar_name, "__.SYMDEF") == 0)
73782c23cb7cSEd Maste 			goto next_member;
73792c23cb7cSEd Maste 		printf("\nFile: %s(%s)\n", re->filename, arhdr->ar_name);
73802c23cb7cSEd Maste 		dump_elf(re);
73812c23cb7cSEd Maste 
73822c23cb7cSEd Maste 	next_member:
73832c23cb7cSEd Maste 		cmd = elf_next(re->elf);
73842c23cb7cSEd Maste 		elf_end(re->elf);
73852c23cb7cSEd Maste 	}
73862c23cb7cSEd Maste 	re->elf = re->ar;
73872c23cb7cSEd Maste }
73882c23cb7cSEd Maste 
73892c23cb7cSEd Maste static void
7390802c2095SMark Johnston dump_object(struct readelf *re, int fd)
73912c23cb7cSEd Maste {
73922c23cb7cSEd Maste 	if ((re->flags & DISPLAY_FILENAME) != 0)
73932c23cb7cSEd Maste 		printf("\nFile: %s\n", re->filename);
73942c23cb7cSEd Maste 
73952c23cb7cSEd Maste 	if ((re->elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
73962c23cb7cSEd Maste 		warnx("elf_begin() failed: %s", elf_errmsg(-1));
7397d003e0d7SEd Maste 		goto done;
73982c23cb7cSEd Maste 	}
73992c23cb7cSEd Maste 
74002c23cb7cSEd Maste 	switch (elf_kind(re->elf)) {
74012c23cb7cSEd Maste 	case ELF_K_NONE:
74022c23cb7cSEd Maste 		warnx("Not an ELF file.");
7403d003e0d7SEd Maste 		goto done;
74042c23cb7cSEd Maste 	case ELF_K_ELF:
74052c23cb7cSEd Maste 		dump_elf(re);
74062c23cb7cSEd Maste 		break;
74072c23cb7cSEd Maste 	case ELF_K_AR:
74082c23cb7cSEd Maste 		dump_ar(re, fd);
74092c23cb7cSEd Maste 		break;
74102c23cb7cSEd Maste 	default:
74112c23cb7cSEd Maste 		warnx("Internal: libelf returned unknown elf kind.");
74122c23cb7cSEd Maste 	}
74132c23cb7cSEd Maste 
7414d003e0d7SEd Maste done:
74152c23cb7cSEd Maste 	elf_end(re->elf);
74162c23cb7cSEd Maste }
74172c23cb7cSEd Maste 
74182c23cb7cSEd Maste static void
74192c23cb7cSEd Maste add_dumpop(struct readelf *re, size_t si, const char *sn, int op, int t)
74202c23cb7cSEd Maste {
74212c23cb7cSEd Maste 	struct dumpop *d;
74222c23cb7cSEd Maste 
74232c23cb7cSEd Maste 	if ((d = find_dumpop(re, si, sn, -1, t)) == NULL) {
74242c23cb7cSEd Maste 		if ((d = calloc(1, sizeof(*d))) == NULL)
74252c23cb7cSEd Maste 			err(EXIT_FAILURE, "calloc failed");
74262c23cb7cSEd Maste 		if (t == DUMP_BY_INDEX)
74272c23cb7cSEd Maste 			d->u.si = si;
74282c23cb7cSEd Maste 		else
74292c23cb7cSEd Maste 			d->u.sn = sn;
74302c23cb7cSEd Maste 		d->type = t;
74312c23cb7cSEd Maste 		d->op = op;
74322c23cb7cSEd Maste 		STAILQ_INSERT_TAIL(&re->v_dumpop, d, dumpop_list);
74332c23cb7cSEd Maste 	} else
74342c23cb7cSEd Maste 		d->op |= op;
74352c23cb7cSEd Maste }
74362c23cb7cSEd Maste 
74372c23cb7cSEd Maste static struct dumpop *
74382c23cb7cSEd Maste find_dumpop(struct readelf *re, size_t si, const char *sn, int op, int t)
74392c23cb7cSEd Maste {
74402c23cb7cSEd Maste 	struct dumpop *d;
74412c23cb7cSEd Maste 
74422c23cb7cSEd Maste 	STAILQ_FOREACH(d, &re->v_dumpop, dumpop_list) {
74432c23cb7cSEd Maste 		if ((op == -1 || op & d->op) &&
74442c23cb7cSEd Maste 		    (t == -1 || (unsigned) t == d->type)) {
74452c23cb7cSEd Maste 			if ((d->type == DUMP_BY_INDEX && d->u.si == si) ||
74462c23cb7cSEd Maste 			    (d->type == DUMP_BY_NAME && !strcmp(d->u.sn, sn)))
74472c23cb7cSEd Maste 				return (d);
74482c23cb7cSEd Maste 		}
74492c23cb7cSEd Maste 	}
74502c23cb7cSEd Maste 
74512c23cb7cSEd Maste 	return (NULL);
74522c23cb7cSEd Maste }
74532c23cb7cSEd Maste 
74542c23cb7cSEd Maste static struct {
74552c23cb7cSEd Maste 	const char *ln;
74562c23cb7cSEd Maste 	char sn;
74572c23cb7cSEd Maste 	int value;
74582c23cb7cSEd Maste } dwarf_op[] = {
74592c23cb7cSEd Maste 	{"rawline", 'l', DW_L},
74602c23cb7cSEd Maste 	{"decodedline", 'L', DW_LL},
74612c23cb7cSEd Maste 	{"info", 'i', DW_I},
74622c23cb7cSEd Maste 	{"abbrev", 'a', DW_A},
74632c23cb7cSEd Maste 	{"pubnames", 'p', DW_P},
74642c23cb7cSEd Maste 	{"aranges", 'r', DW_R},
74652c23cb7cSEd Maste 	{"ranges", 'r', DW_R},
74662c23cb7cSEd Maste 	{"Ranges", 'R', DW_RR},
74672c23cb7cSEd Maste 	{"macro", 'm', DW_M},
74682c23cb7cSEd Maste 	{"frames", 'f', DW_F},
7469cf781b2eSEd Maste 	{"frames-interp", 'F', DW_FF},
74702c23cb7cSEd Maste 	{"str", 's', DW_S},
74712c23cb7cSEd Maste 	{"loc", 'o', DW_O},
74722c23cb7cSEd Maste 	{NULL, 0, 0}
74732c23cb7cSEd Maste };
74742c23cb7cSEd Maste 
74752c23cb7cSEd Maste static void
74762c23cb7cSEd Maste parse_dwarf_op_short(struct readelf *re, const char *op)
74772c23cb7cSEd Maste {
74782c23cb7cSEd Maste 	int i;
74792c23cb7cSEd Maste 
74802c23cb7cSEd Maste 	if (op == NULL) {
74812c23cb7cSEd Maste 		re->dop |= DW_DEFAULT_OPTIONS;
74822c23cb7cSEd Maste 		return;
74832c23cb7cSEd Maste 	}
74842c23cb7cSEd Maste 
74852c23cb7cSEd Maste 	for (; *op != '\0'; op++) {
74862c23cb7cSEd Maste 		for (i = 0; dwarf_op[i].ln != NULL; i++) {
74872c23cb7cSEd Maste 			if (dwarf_op[i].sn == *op) {
74882c23cb7cSEd Maste 				re->dop |= dwarf_op[i].value;
74892c23cb7cSEd Maste 				break;
74902c23cb7cSEd Maste 			}
74912c23cb7cSEd Maste 		}
74922c23cb7cSEd Maste 	}
74932c23cb7cSEd Maste }
74942c23cb7cSEd Maste 
74952c23cb7cSEd Maste static void
74962c23cb7cSEd Maste parse_dwarf_op_long(struct readelf *re, const char *op)
74972c23cb7cSEd Maste {
74982c23cb7cSEd Maste 	char *p, *token, *bp;
74992c23cb7cSEd Maste 	int i;
75002c23cb7cSEd Maste 
75012c23cb7cSEd Maste 	if (op == NULL) {
75022c23cb7cSEd Maste 		re->dop |= DW_DEFAULT_OPTIONS;
75032c23cb7cSEd Maste 		return;
75042c23cb7cSEd Maste 	}
75052c23cb7cSEd Maste 
75062c23cb7cSEd Maste 	if ((p = strdup(op)) == NULL)
75072c23cb7cSEd Maste 		err(EXIT_FAILURE, "strdup failed");
75082c23cb7cSEd Maste 	bp = p;
75092c23cb7cSEd Maste 
75102c23cb7cSEd Maste 	while ((token = strsep(&p, ",")) != NULL) {
75112c23cb7cSEd Maste 		for (i = 0; dwarf_op[i].ln != NULL; i++) {
75122c23cb7cSEd Maste 			if (!strcmp(token, dwarf_op[i].ln)) {
75132c23cb7cSEd Maste 				re->dop |= dwarf_op[i].value;
75142c23cb7cSEd Maste 				break;
75152c23cb7cSEd Maste 			}
75162c23cb7cSEd Maste 		}
75172c23cb7cSEd Maste 	}
75182c23cb7cSEd Maste 
75192c23cb7cSEd Maste 	free(bp);
75202c23cb7cSEd Maste }
75212c23cb7cSEd Maste 
75222c23cb7cSEd Maste static uint64_t
75232c23cb7cSEd Maste _read_lsb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read)
75242c23cb7cSEd Maste {
75252c23cb7cSEd Maste 	uint64_t ret;
75262c23cb7cSEd Maste 	uint8_t *src;
75272c23cb7cSEd Maste 
75282c23cb7cSEd Maste 	src = (uint8_t *) d->d_buf + *offsetp;
75292c23cb7cSEd Maste 
75302c23cb7cSEd Maste 	ret = 0;
75312c23cb7cSEd Maste 	switch (bytes_to_read) {
75322c23cb7cSEd Maste 	case 8:
75332c23cb7cSEd Maste 		ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
75342c23cb7cSEd Maste 		ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
7535839529caSEd Maste 		/* FALLTHROUGH */
75362c23cb7cSEd Maste 	case 4:
75372c23cb7cSEd Maste 		ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
7538839529caSEd Maste 		/* FALLTHROUGH */
75392c23cb7cSEd Maste 	case 2:
75402c23cb7cSEd Maste 		ret |= ((uint64_t) src[1]) << 8;
7541839529caSEd Maste 		/* FALLTHROUGH */
75422c23cb7cSEd Maste 	case 1:
75432c23cb7cSEd Maste 		ret |= src[0];
75442c23cb7cSEd Maste 		break;
75452c23cb7cSEd Maste 	default:
75462c23cb7cSEd Maste 		return (0);
75472c23cb7cSEd Maste 	}
75482c23cb7cSEd Maste 
75492c23cb7cSEd Maste 	*offsetp += bytes_to_read;
75502c23cb7cSEd Maste 
75512c23cb7cSEd Maste 	return (ret);
75522c23cb7cSEd Maste }
75532c23cb7cSEd Maste 
75542c23cb7cSEd Maste static uint64_t
75552c23cb7cSEd Maste _read_msb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read)
75562c23cb7cSEd Maste {
75572c23cb7cSEd Maste 	uint64_t ret;
75582c23cb7cSEd Maste 	uint8_t *src;
75592c23cb7cSEd Maste 
75602c23cb7cSEd Maste 	src = (uint8_t *) d->d_buf + *offsetp;
75612c23cb7cSEd Maste 
75622c23cb7cSEd Maste 	switch (bytes_to_read) {
75632c23cb7cSEd Maste 	case 1:
75642c23cb7cSEd Maste 		ret = src[0];
75652c23cb7cSEd Maste 		break;
75662c23cb7cSEd Maste 	case 2:
75672c23cb7cSEd Maste 		ret = src[1] | ((uint64_t) src[0]) << 8;
75682c23cb7cSEd Maste 		break;
75692c23cb7cSEd Maste 	case 4:
75702c23cb7cSEd Maste 		ret = src[3] | ((uint64_t) src[2]) << 8;
75712c23cb7cSEd Maste 		ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24;
75722c23cb7cSEd Maste 		break;
75732c23cb7cSEd Maste 	case 8:
75742c23cb7cSEd Maste 		ret = src[7] | ((uint64_t) src[6]) << 8;
75752c23cb7cSEd Maste 		ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24;
75762c23cb7cSEd Maste 		ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40;
75772c23cb7cSEd Maste 		ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56;
75782c23cb7cSEd Maste 		break;
75792c23cb7cSEd Maste 	default:
75802c23cb7cSEd Maste 		return (0);
75812c23cb7cSEd Maste 	}
75822c23cb7cSEd Maste 
75832c23cb7cSEd Maste 	*offsetp += bytes_to_read;
75842c23cb7cSEd Maste 
75852c23cb7cSEd Maste 	return (ret);
75862c23cb7cSEd Maste }
75872c23cb7cSEd Maste 
75882c23cb7cSEd Maste static uint64_t
75892c23cb7cSEd Maste _decode_lsb(uint8_t **data, int bytes_to_read)
75902c23cb7cSEd Maste {
75912c23cb7cSEd Maste 	uint64_t ret;
75922c23cb7cSEd Maste 	uint8_t *src;
75932c23cb7cSEd Maste 
75942c23cb7cSEd Maste 	src = *data;
75952c23cb7cSEd Maste 
75962c23cb7cSEd Maste 	ret = 0;
75972c23cb7cSEd Maste 	switch (bytes_to_read) {
75982c23cb7cSEd Maste 	case 8:
75992c23cb7cSEd Maste 		ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
76002c23cb7cSEd Maste 		ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
7601839529caSEd Maste 		/* FALLTHROUGH */
76022c23cb7cSEd Maste 	case 4:
76032c23cb7cSEd Maste 		ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
7604839529caSEd Maste 		/* FALLTHROUGH */
76052c23cb7cSEd Maste 	case 2:
76062c23cb7cSEd Maste 		ret |= ((uint64_t) src[1]) << 8;
7607839529caSEd Maste 		/* FALLTHROUGH */
76082c23cb7cSEd Maste 	case 1:
76092c23cb7cSEd Maste 		ret |= src[0];
76102c23cb7cSEd Maste 		break;
76112c23cb7cSEd Maste 	default:
76122c23cb7cSEd Maste 		return (0);
76132c23cb7cSEd Maste 	}
76142c23cb7cSEd Maste 
76152c23cb7cSEd Maste 	*data += bytes_to_read;
76162c23cb7cSEd Maste 
76172c23cb7cSEd Maste 	return (ret);
76182c23cb7cSEd Maste }
76192c23cb7cSEd Maste 
76202c23cb7cSEd Maste static uint64_t
76212c23cb7cSEd Maste _decode_msb(uint8_t **data, int bytes_to_read)
76222c23cb7cSEd Maste {
76232c23cb7cSEd Maste 	uint64_t ret;
76242c23cb7cSEd Maste 	uint8_t *src;
76252c23cb7cSEd Maste 
76262c23cb7cSEd Maste 	src = *data;
76272c23cb7cSEd Maste 
76282c23cb7cSEd Maste 	ret = 0;
76292c23cb7cSEd Maste 	switch (bytes_to_read) {
76302c23cb7cSEd Maste 	case 1:
76312c23cb7cSEd Maste 		ret = src[0];
76322c23cb7cSEd Maste 		break;
76332c23cb7cSEd Maste 	case 2:
76342c23cb7cSEd Maste 		ret = src[1] | ((uint64_t) src[0]) << 8;
76352c23cb7cSEd Maste 		break;
76362c23cb7cSEd Maste 	case 4:
76372c23cb7cSEd Maste 		ret = src[3] | ((uint64_t) src[2]) << 8;
76382c23cb7cSEd Maste 		ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24;
76392c23cb7cSEd Maste 		break;
76402c23cb7cSEd Maste 	case 8:
76412c23cb7cSEd Maste 		ret = src[7] | ((uint64_t) src[6]) << 8;
76422c23cb7cSEd Maste 		ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24;
76432c23cb7cSEd Maste 		ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40;
76442c23cb7cSEd Maste 		ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56;
76452c23cb7cSEd Maste 		break;
76462c23cb7cSEd Maste 	default:
76472c23cb7cSEd Maste 		return (0);
76482c23cb7cSEd Maste 		break;
76492c23cb7cSEd Maste 	}
76502c23cb7cSEd Maste 
76512c23cb7cSEd Maste 	*data += bytes_to_read;
76522c23cb7cSEd Maste 
76532c23cb7cSEd Maste 	return (ret);
76542c23cb7cSEd Maste }
76552c23cb7cSEd Maste 
76562c23cb7cSEd Maste static int64_t
765795fd7f26SEd Maste _decode_sleb128(uint8_t **dp, uint8_t *dpe)
76582c23cb7cSEd Maste {
76592c23cb7cSEd Maste 	int64_t ret = 0;
76608f32e46dSKai Wang 	uint8_t b = 0;
76612c23cb7cSEd Maste 	int shift = 0;
76622c23cb7cSEd Maste 
76632c23cb7cSEd Maste 	uint8_t *src = *dp;
76642c23cb7cSEd Maste 
76652c23cb7cSEd Maste 	do {
766695fd7f26SEd Maste 		if (src >= dpe)
766795fd7f26SEd Maste 			break;
76682c23cb7cSEd Maste 		b = *src++;
76692c23cb7cSEd Maste 		ret |= ((b & 0x7f) << shift);
76702c23cb7cSEd Maste 		shift += 7;
76712c23cb7cSEd Maste 	} while ((b & 0x80) != 0);
76722c23cb7cSEd Maste 
76732c23cb7cSEd Maste 	if (shift < 32 && (b & 0x40) != 0)
76742c23cb7cSEd Maste 		ret |= (-1 << shift);
76752c23cb7cSEd Maste 
76762c23cb7cSEd Maste 	*dp = src;
76772c23cb7cSEd Maste 
76782c23cb7cSEd Maste 	return (ret);
76792c23cb7cSEd Maste }
76802c23cb7cSEd Maste 
76812c23cb7cSEd Maste static uint64_t
768295fd7f26SEd Maste _decode_uleb128(uint8_t **dp, uint8_t *dpe)
76832c23cb7cSEd Maste {
76842c23cb7cSEd Maste 	uint64_t ret = 0;
76852c23cb7cSEd Maste 	uint8_t b;
76862c23cb7cSEd Maste 	int shift = 0;
76872c23cb7cSEd Maste 
76882c23cb7cSEd Maste 	uint8_t *src = *dp;
76892c23cb7cSEd Maste 
76902c23cb7cSEd Maste 	do {
769195fd7f26SEd Maste 		if (src >= dpe)
769295fd7f26SEd Maste 			break;
76932c23cb7cSEd Maste 		b = *src++;
76942c23cb7cSEd Maste 		ret |= ((b & 0x7f) << shift);
76952c23cb7cSEd Maste 		shift += 7;
76962c23cb7cSEd Maste 	} while ((b & 0x80) != 0);
76972c23cb7cSEd Maste 
76982c23cb7cSEd Maste 	*dp = src;
76992c23cb7cSEd Maste 
77002c23cb7cSEd Maste 	return (ret);
77012c23cb7cSEd Maste }
77022c23cb7cSEd Maste 
77032c23cb7cSEd Maste static void
77042c23cb7cSEd Maste readelf_version(void)
77052c23cb7cSEd Maste {
77062c23cb7cSEd Maste 	(void) printf("%s (%s)\n", ELFTC_GETPROGNAME(),
77072c23cb7cSEd Maste 	    elftc_version());
77082c23cb7cSEd Maste 	exit(EXIT_SUCCESS);
77092c23cb7cSEd Maste }
77102c23cb7cSEd Maste 
77112c23cb7cSEd Maste #define	USAGE_MESSAGE	"\
77122c23cb7cSEd Maste Usage: %s [options] file...\n\
77132c23cb7cSEd Maste   Display information about ELF objects and ar(1) archives.\n\n\
77142c23cb7cSEd Maste   Options:\n\
77152c23cb7cSEd Maste   -a | --all               Equivalent to specifying options '-dhIlrsASV'.\n\
77162c23cb7cSEd Maste   -c | --archive-index     Print the archive symbol table for archives.\n\
77172c23cb7cSEd Maste   -d | --dynamic           Print the contents of SHT_DYNAMIC sections.\n\
77182c23cb7cSEd Maste   -e | --headers           Print all headers in the object.\n\
77193ef90571SEd Maste   -g | --section-groups    Print the contents of the section groups.\n\
77202c23cb7cSEd Maste   -h | --file-header       Print the file header for the object.\n\
77212c23cb7cSEd Maste   -l | --program-headers   Print the PHDR table for the object.\n\
77222c23cb7cSEd Maste   -n | --notes             Print the contents of SHT_NOTE sections.\n\
77232c23cb7cSEd Maste   -p INDEX | --string-dump=INDEX\n\
77242c23cb7cSEd Maste                            Print the contents of section at index INDEX.\n\
77252c23cb7cSEd Maste   -r | --relocs            Print relocation information.\n\
77262c23cb7cSEd Maste   -s | --syms | --symbols  Print symbol tables.\n\
77272c23cb7cSEd Maste   -t | --section-details   Print additional information about sections.\n\
77282c23cb7cSEd Maste   -v | --version           Print a version identifier and exit.\n\
7729839529caSEd Maste   -w[afilmoprsFLR] | --debug-dump={abbrev,aranges,decodedline,frames,\n\
7730839529caSEd Maste                                frames-interp,info,loc,macro,pubnames,\n\
7731839529caSEd Maste                                ranges,Ranges,rawline,str}\n\
7732839529caSEd Maste                            Display DWARF information.\n\
77332c23cb7cSEd Maste   -x INDEX | --hex-dump=INDEX\n\
77342c23cb7cSEd Maste                            Display contents of a section as hexadecimal.\n\
7735*e128bd0fSEd Maste   -z | --decompress        Decompress the contents of a section before displaying it.\n\
77362c23cb7cSEd Maste   -A | --arch-specific     (accepted, but ignored)\n\
77372c23cb7cSEd Maste   -D | --use-dynamic       Print the symbol table specified by the DT_SYMTAB\n\
77382c23cb7cSEd Maste                            entry in the \".dynamic\" section.\n\
77392c23cb7cSEd Maste   -H | --help              Print a help message.\n\
77402c23cb7cSEd Maste   -I | --histogram         Print information on bucket list lengths for \n\
77412c23cb7cSEd Maste                            hash sections.\n\
77422c23cb7cSEd Maste   -N | --full-section-name (accepted, but ignored)\n\
77432c23cb7cSEd Maste   -S | --sections | --section-headers\n\
77442c23cb7cSEd Maste                            Print information about section headers.\n\
77452c23cb7cSEd Maste   -V | --version-info      Print symbol versoning information.\n\
77462c23cb7cSEd Maste   -W | --wide              Print information without wrapping long lines.\n"
77472c23cb7cSEd Maste 
77482c23cb7cSEd Maste 
77492c23cb7cSEd Maste static void
7750839529caSEd Maste readelf_usage(int status)
77512c23cb7cSEd Maste {
77522c23cb7cSEd Maste 	fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());
7753839529caSEd Maste 	exit(status);
77542c23cb7cSEd Maste }
77552c23cb7cSEd Maste 
77562c23cb7cSEd Maste int
77572c23cb7cSEd Maste main(int argc, char **argv)
77582c23cb7cSEd Maste {
7759802c2095SMark Johnston 	cap_rights_t	rights;
7760802c2095SMark Johnston 	fileargs_t	*fa;
77612c23cb7cSEd Maste 	struct readelf	*re, re_storage;
77622c23cb7cSEd Maste 	unsigned long	 si;
7763802c2095SMark Johnston 	int		 fd, opt, i;
77642c23cb7cSEd Maste 	char		*ep;
77652c23cb7cSEd Maste 
77662c23cb7cSEd Maste 	re = &re_storage;
77672c23cb7cSEd Maste 	memset(re, 0, sizeof(*re));
77682c23cb7cSEd Maste 	STAILQ_INIT(&re->v_dumpop);
77692c23cb7cSEd Maste 
7770*e128bd0fSEd Maste 	while ((opt = getopt_long(argc, argv, "AacDdegHhIi:lNnp:rSstuVvWw::x:z",
77712c23cb7cSEd Maste 	    longopts, NULL)) != -1) {
77722c23cb7cSEd Maste 		switch(opt) {
77732c23cb7cSEd Maste 		case '?':
7774839529caSEd Maste 			readelf_usage(EXIT_SUCCESS);
77752c23cb7cSEd Maste 			break;
77762c23cb7cSEd Maste 		case 'A':
77772c23cb7cSEd Maste 			re->options |= RE_AA;
77782c23cb7cSEd Maste 			break;
77792c23cb7cSEd Maste 		case 'a':
77803ef90571SEd Maste 			re->options |= RE_AA | RE_D | RE_G | RE_H | RE_II |
77813ef90571SEd Maste 			    RE_L | RE_R | RE_SS | RE_S | RE_VV;
77822c23cb7cSEd Maste 			break;
77832c23cb7cSEd Maste 		case 'c':
77842c23cb7cSEd Maste 			re->options |= RE_C;
77852c23cb7cSEd Maste 			break;
77862c23cb7cSEd Maste 		case 'D':
77872c23cb7cSEd Maste 			re->options |= RE_DD;
77882c23cb7cSEd Maste 			break;
77892c23cb7cSEd Maste 		case 'd':
77902c23cb7cSEd Maste 			re->options |= RE_D;
77912c23cb7cSEd Maste 			break;
77922c23cb7cSEd Maste 		case 'e':
77932c23cb7cSEd Maste 			re->options |= RE_H | RE_L | RE_SS;
77942c23cb7cSEd Maste 			break;
77952c23cb7cSEd Maste 		case 'g':
77962c23cb7cSEd Maste 			re->options |= RE_G;
77972c23cb7cSEd Maste 			break;
77982c23cb7cSEd Maste 		case 'H':
7799839529caSEd Maste 			readelf_usage(EXIT_SUCCESS);
78002c23cb7cSEd Maste 			break;
78012c23cb7cSEd Maste 		case 'h':
78022c23cb7cSEd Maste 			re->options |= RE_H;
78032c23cb7cSEd Maste 			break;
78042c23cb7cSEd Maste 		case 'I':
78052c23cb7cSEd Maste 			re->options |= RE_II;
78062c23cb7cSEd Maste 			break;
78072c23cb7cSEd Maste 		case 'i':
78082c23cb7cSEd Maste 			/* Not implemented yet. */
78092c23cb7cSEd Maste 			break;
78102c23cb7cSEd Maste 		case 'l':
78112c23cb7cSEd Maste 			re->options |= RE_L;
78122c23cb7cSEd Maste 			break;
78132c23cb7cSEd Maste 		case 'N':
78142c23cb7cSEd Maste 			re->options |= RE_NN;
78152c23cb7cSEd Maste 			break;
78162c23cb7cSEd Maste 		case 'n':
78172c23cb7cSEd Maste 			re->options |= RE_N;
78182c23cb7cSEd Maste 			break;
78192c23cb7cSEd Maste 		case 'p':
78202c23cb7cSEd Maste 			re->options |= RE_P;
78212c23cb7cSEd Maste 			si = strtoul(optarg, &ep, 10);
78222c23cb7cSEd Maste 			if (*ep == '\0')
78232c23cb7cSEd Maste 				add_dumpop(re, (size_t) si, NULL, STR_DUMP,
78242c23cb7cSEd Maste 				    DUMP_BY_INDEX);
78252c23cb7cSEd Maste 			else
78262c23cb7cSEd Maste 				add_dumpop(re, 0, optarg, STR_DUMP,
78272c23cb7cSEd Maste 				    DUMP_BY_NAME);
78282c23cb7cSEd Maste 			break;
78292c23cb7cSEd Maste 		case 'r':
78302c23cb7cSEd Maste 			re->options |= RE_R;
78312c23cb7cSEd Maste 			break;
78322c23cb7cSEd Maste 		case 'S':
78332c23cb7cSEd Maste 			re->options |= RE_SS;
78342c23cb7cSEd Maste 			break;
78352c23cb7cSEd Maste 		case 's':
78362c23cb7cSEd Maste 			re->options |= RE_S;
78372c23cb7cSEd Maste 			break;
78382c23cb7cSEd Maste 		case 't':
7839718699beSMark Johnston 			re->options |= RE_SS | RE_T;
78402c23cb7cSEd Maste 			break;
78412c23cb7cSEd Maste 		case 'u':
78422c23cb7cSEd Maste 			re->options |= RE_U;
78432c23cb7cSEd Maste 			break;
78442c23cb7cSEd Maste 		case 'V':
78452c23cb7cSEd Maste 			re->options |= RE_VV;
78462c23cb7cSEd Maste 			break;
78472c23cb7cSEd Maste 		case 'v':
78482c23cb7cSEd Maste 			readelf_version();
78492c23cb7cSEd Maste 			break;
78502c23cb7cSEd Maste 		case 'W':
78512c23cb7cSEd Maste 			re->options |= RE_WW;
78522c23cb7cSEd Maste 			break;
78532c23cb7cSEd Maste 		case 'w':
78542c23cb7cSEd Maste 			re->options |= RE_W;
78552c23cb7cSEd Maste 			parse_dwarf_op_short(re, optarg);
78562c23cb7cSEd Maste 			break;
78572c23cb7cSEd Maste 		case 'x':
78582c23cb7cSEd Maste 			re->options |= RE_X;
78592c23cb7cSEd Maste 			si = strtoul(optarg, &ep, 10);
78602c23cb7cSEd Maste 			if (*ep == '\0')
78612c23cb7cSEd Maste 				add_dumpop(re, (size_t) si, NULL, HEX_DUMP,
78622c23cb7cSEd Maste 				    DUMP_BY_INDEX);
78632c23cb7cSEd Maste 			else
78642c23cb7cSEd Maste 				add_dumpop(re, 0, optarg, HEX_DUMP,
78652c23cb7cSEd Maste 				    DUMP_BY_NAME);
78662c23cb7cSEd Maste 			break;
7867*e128bd0fSEd Maste 		case 'z':
7868*e128bd0fSEd Maste 			re->options |= RE_Z;
7869*e128bd0fSEd Maste 			break;
78702c23cb7cSEd Maste 		case OPTION_DEBUG_DUMP:
78712c23cb7cSEd Maste 			re->options |= RE_W;
78722c23cb7cSEd Maste 			parse_dwarf_op_long(re, optarg);
78732c23cb7cSEd Maste 		}
78742c23cb7cSEd Maste 	}
78752c23cb7cSEd Maste 
78762c23cb7cSEd Maste 	argv += optind;
78772c23cb7cSEd Maste 	argc -= optind;
78782c23cb7cSEd Maste 
78792c23cb7cSEd Maste 	if (argc == 0 || re->options == 0)
7880839529caSEd Maste 		readelf_usage(EXIT_FAILURE);
78812c23cb7cSEd Maste 
78822c23cb7cSEd Maste 	if (argc > 1)
78832c23cb7cSEd Maste 		re->flags |= DISPLAY_FILENAME;
78842c23cb7cSEd Maste 
78852c23cb7cSEd Maste 	if (elf_version(EV_CURRENT) == EV_NONE)
78862c23cb7cSEd Maste 		errx(EXIT_FAILURE, "ELF library initialization failed: %s",
78872c23cb7cSEd Maste 		    elf_errmsg(-1));
78882c23cb7cSEd Maste 
7889802c2095SMark Johnston 	cap_rights_init(&rights, CAP_FCNTL, CAP_FSTAT, CAP_MMAP_R, CAP_SEEK);
7890802c2095SMark Johnston 	fa = fileargs_init(argc, argv, O_RDONLY, 0, &rights, FA_OPEN);
7891802c2095SMark Johnston 	if (fa == NULL)
7892802c2095SMark Johnston 		err(1, "Unable to initialize casper fileargs");
7893802c2095SMark Johnston 
7894802c2095SMark Johnston 	caph_cache_catpages();
7895802c2095SMark Johnston 	if (caph_limit_stdio() < 0) {
7896802c2095SMark Johnston 		fileargs_free(fa);
7897802c2095SMark Johnston 		err(1, "Unable to limit stdio rights");
7898802c2095SMark Johnston 	}
7899802c2095SMark Johnston 	if (caph_enter_casper() < 0) {
7900802c2095SMark Johnston 		fileargs_free(fa);
7901802c2095SMark Johnston 		err(1, "Unable to enter capability mode");
7902802c2095SMark Johnston 	}
7903802c2095SMark Johnston 
7904b00fe64fSEd Maste 	for (i = 0; i < argc; i++) {
79052c23cb7cSEd Maste 		re->filename = argv[i];
7906802c2095SMark Johnston 		fd = fileargs_open(fa, re->filename);
790719669671SMark Johnston 		if (fd < 0) {
7908802c2095SMark Johnston 			warn("open %s failed", re->filename);
790919669671SMark Johnston 		} else {
7910802c2095SMark Johnston 			dump_object(re, fd);
791119669671SMark Johnston 			close(fd);
791219669671SMark Johnston 		}
79132c23cb7cSEd Maste 	}
79142c23cb7cSEd Maste 
79152c23cb7cSEd Maste 	exit(EXIT_SUCCESS);
79162c23cb7cSEd Maste }
7917