xref: /freebsd/contrib/elftoolchain/readelf/readelf.c (revision 2f834a0b41079f9be4dc33ff877d50a5fba869d4)
1 /*-
2  * Copyright (c) 2009,2010 Kai Wang
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 #include <sys/param.h>
29 #include <sys/queue.h>
30 #include <ar.h>
31 #include <ctype.h>
32 #include <dwarf.h>
33 #include <err.h>
34 #include <fcntl.h>
35 #include <gelf.h>
36 #include <getopt.h>
37 #include <libdwarf.h>
38 #include <libelftc.h>
39 #include <libgen.h>
40 #include <stdarg.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <time.h>
45 #include <unistd.h>
46 
47 #include "_elftc.h"
48 
49 ELFTC_VCSID("$Id: readelf.c 2946 2013-05-26 08:00:11Z kaiwang27 $");
50 
51 /*
52  * readelf(1) options.
53  */
54 #define	RE_AA	0x00000001
55 #define	RE_C	0x00000002
56 #define	RE_DD	0x00000004
57 #define	RE_D	0x00000008
58 #define	RE_G	0x00000010
59 #define	RE_H	0x00000020
60 #define	RE_II	0x00000040
61 #define	RE_I	0x00000080
62 #define	RE_L	0x00000100
63 #define	RE_NN	0x00000200
64 #define	RE_N	0x00000400
65 #define	RE_P	0x00000800
66 #define	RE_R	0x00001000
67 #define	RE_SS	0x00002000
68 #define	RE_S	0x00004000
69 #define	RE_T	0x00008000
70 #define	RE_U	0x00010000
71 #define	RE_VV	0x00020000
72 #define	RE_WW	0x00040000
73 #define	RE_W	0x00080000
74 #define	RE_X	0x00100000
75 
76 /*
77  * dwarf dump options.
78  */
79 #define	DW_A	0x00000001
80 #define	DW_FF	0x00000002
81 #define	DW_F	0x00000004
82 #define	DW_I	0x00000008
83 #define	DW_LL	0x00000010
84 #define	DW_L	0x00000020
85 #define	DW_M	0x00000040
86 #define	DW_O	0x00000080
87 #define	DW_P	0x00000100
88 #define	DW_RR	0x00000200
89 #define	DW_R	0x00000400
90 #define	DW_S	0x00000800
91 
92 #define	DW_DEFAULT_OPTIONS (DW_A | DW_F | DW_I | DW_L | DW_O | DW_P | \
93 	    DW_R | DW_RR | DW_S)
94 
95 /*
96  * readelf(1) run control flags.
97  */
98 #define	DISPLAY_FILENAME	0x0001
99 
100 /*
101  * Internal data structure for sections.
102  */
103 struct section {
104 	const char	*name;		/* section name */
105 	Elf_Scn		*scn;		/* section scn */
106 	uint64_t	 off;		/* section offset */
107 	uint64_t	 sz;		/* section size */
108 	uint64_t	 entsize;	/* section entsize */
109 	uint64_t	 align;		/* section alignment */
110 	uint64_t	 type;		/* section type */
111 	uint64_t	 flags;		/* section flags */
112 	uint64_t	 addr;		/* section virtual addr */
113 	uint32_t	 link;		/* section link ndx */
114 	uint32_t	 info;		/* section info ndx */
115 };
116 
117 struct dumpop {
118 	union {
119 		size_t si;		/* section index */
120 		const char *sn;		/* section name */
121 	} u;
122 	enum {
123 		DUMP_BY_INDEX = 0,
124 		DUMP_BY_NAME
125 	} type;				/* dump type */
126 #define HEX_DUMP	0x0001
127 #define STR_DUMP	0x0002
128 	int op;				/* dump operation */
129 	STAILQ_ENTRY(dumpop) dumpop_list;
130 };
131 
132 struct symver {
133 	const char *name;
134 	int type;
135 };
136 
137 /*
138  * Structure encapsulates the global data for readelf(1).
139  */
140 struct readelf {
141 	const char	 *filename;	/* current processing file. */
142 	int		  options;	/* command line options. */
143 	int		  flags;	/* run control flags. */
144 	int		  dop;		/* dwarf dump options. */
145 	Elf		 *elf;		/* underlying ELF descriptor. */
146 	Elf		 *ar;		/* archive ELF descriptor. */
147 	Dwarf_Debug	  dbg;		/* DWARF handle. */
148 	GElf_Ehdr	  ehdr;		/* ELF header. */
149 	int		  ec;		/* ELF class. */
150 	size_t		  shnum;	/* #sections. */
151 	struct section	 *vd_s;		/* Verdef section. */
152 	struct section	 *vn_s;		/* Verneed section. */
153 	struct section	 *vs_s;		/* Versym section. */
154 	uint16_t	 *vs;		/* Versym array. */
155 	int		  vs_sz;	/* Versym array size. */
156 	struct symver	 *ver;		/* Version array. */
157 	int		  ver_sz;	/* Size of version array. */
158 	struct section	 *sl;		/* list of sections. */
159 	STAILQ_HEAD(, dumpop) v_dumpop; /* list of dump ops. */
160 	uint64_t	(*dw_read)(Elf_Data *, uint64_t *, int);
161 	uint64_t	(*dw_decode)(uint8_t **, int);
162 };
163 
164 enum options
165 {
166 	OPTION_DEBUG_DUMP
167 };
168 
169 static struct option longopts[] = {
170 	{"all", no_argument, NULL, 'a'},
171 	{"arch-specific", no_argument, NULL, 'A'},
172 	{"archive-index", no_argument, NULL, 'c'},
173 	{"debug-dump", optional_argument, NULL, OPTION_DEBUG_DUMP},
174 	{"dynamic", no_argument, NULL, 'd'},
175 	{"file-header", no_argument, NULL, 'h'},
176 	{"full-section-name", no_argument, NULL, 'N'},
177 	{"headers", no_argument, NULL, 'e'},
178 	{"help", no_argument, 0, 'H'},
179 	{"hex-dump", required_argument, NULL, 'x'},
180 	{"histogram", no_argument, NULL, 'I'},
181 	{"notes", no_argument, NULL, 'n'},
182 	{"program-headers", no_argument, NULL, 'l'},
183 	{"relocs", no_argument, NULL, 'r'},
184 	{"sections", no_argument, NULL, 'S'},
185 	{"section-headers", no_argument, NULL, 'S'},
186 	{"section-groups", no_argument, NULL, 'g'},
187 	{"section-details", no_argument, NULL, 't'},
188 	{"segments", no_argument, NULL, 'l'},
189 	{"string-dump", required_argument, NULL, 'p'},
190 	{"symbols", no_argument, NULL, 's'},
191 	{"syms", no_argument, NULL, 's'},
192 	{"unwind", no_argument, NULL, 'u'},
193 	{"use-dynamic", no_argument, NULL, 'D'},
194 	{"version-info", no_argument, 0, 'V'},
195 	{"version", no_argument, 0, 'v'},
196 	{"wide", no_argument, 0, 'W'},
197 	{NULL, 0, NULL, 0}
198 };
199 
200 struct eflags_desc {
201 	uint64_t flag;
202 	const char *desc;
203 };
204 
205 struct mips_option {
206 	uint64_t flag;
207 	const char *desc;
208 };
209 
210 static void add_dumpop(struct readelf *re, size_t si, const char *sn, int op,
211     int t);
212 static const char *aeabi_adv_simd_arch(uint64_t simd);
213 static const char *aeabi_align_needed(uint64_t an);
214 static const char *aeabi_align_preserved(uint64_t ap);
215 static const char *aeabi_arm_isa(uint64_t ai);
216 static const char *aeabi_cpu_arch(uint64_t arch);
217 static const char *aeabi_cpu_arch_profile(uint64_t pf);
218 static const char *aeabi_div(uint64_t du);
219 static const char *aeabi_enum_size(uint64_t es);
220 static const char *aeabi_fp_16bit_format(uint64_t fp16);
221 static const char *aeabi_fp_arch(uint64_t fp);
222 static const char *aeabi_fp_denormal(uint64_t fd);
223 static const char *aeabi_fp_exceptions(uint64_t fe);
224 static const char *aeabi_fp_hpext(uint64_t fh);
225 static const char *aeabi_fp_number_model(uint64_t fn);
226 static const char *aeabi_fp_optm_goal(uint64_t fog);
227 static const char *aeabi_fp_rounding(uint64_t fr);
228 static const char *aeabi_hardfp(uint64_t hfp);
229 static const char *aeabi_mpext(uint64_t mp);
230 static const char *aeabi_optm_goal(uint64_t og);
231 static const char *aeabi_pcs_config(uint64_t pcs);
232 static const char *aeabi_pcs_got(uint64_t got);
233 static const char *aeabi_pcs_r9(uint64_t r9);
234 static const char *aeabi_pcs_ro(uint64_t ro);
235 static const char *aeabi_pcs_rw(uint64_t rw);
236 static const char *aeabi_pcs_wchar_t(uint64_t wt);
237 static const char *aeabi_t2ee(uint64_t t2ee);
238 static const char *aeabi_thumb_isa(uint64_t ti);
239 static const char *aeabi_fp_user_exceptions(uint64_t fu);
240 static const char *aeabi_unaligned_access(uint64_t ua);
241 static const char *aeabi_vfp_args(uint64_t va);
242 static const char *aeabi_virtual(uint64_t vt);
243 static const char *aeabi_wmmx_arch(uint64_t wmmx);
244 static const char *aeabi_wmmx_args(uint64_t wa);
245 static const char *elf_class(unsigned int class);
246 static const char *elf_endian(unsigned int endian);
247 static const char *elf_machine(unsigned int mach);
248 static const char *elf_osabi(unsigned int abi);
249 static const char *elf_type(unsigned int type);
250 static const char *elf_ver(unsigned int ver);
251 static const char *dt_type(unsigned int mach, unsigned int dtype);
252 static void dump_ar(struct readelf *re, int);
253 static void dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe);
254 static void dump_attributes(struct readelf *re);
255 static uint8_t *dump_compatibility_tag(uint8_t *p);
256 static void dump_dwarf(struct readelf *re);
257 static void dump_eflags(struct readelf *re, uint64_t e_flags);
258 static void dump_elf(struct readelf *re);
259 static void dump_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab);
260 static void dump_dynamic(struct readelf *re);
261 static void dump_liblist(struct readelf *re);
262 static void dump_mips_attributes(struct readelf *re, uint8_t *p, uint8_t *pe);
263 static void dump_mips_odk_reginfo(struct readelf *re, uint8_t *p, size_t sz);
264 static void dump_mips_options(struct readelf *re, struct section *s);
265 static void dump_mips_option_flags(const char *name, struct mips_option *opt,
266     uint64_t info);
267 static void dump_mips_reginfo(struct readelf *re, struct section *s);
268 static void dump_mips_specific_info(struct readelf *re);
269 static void dump_notes(struct readelf *re);
270 static void dump_notes_content(struct readelf *re, const char *buf, size_t sz,
271     off_t off);
272 static void dump_svr4_hash(struct section *s);
273 static void dump_svr4_hash64(struct readelf *re, struct section *s);
274 static void dump_gnu_hash(struct readelf *re, struct section *s);
275 static void dump_hash(struct readelf *re);
276 static void dump_phdr(struct readelf *re);
277 static void dump_ppc_attributes(uint8_t *p, uint8_t *pe);
278 static void dump_symtab(struct readelf *re, int i);
279 static void dump_symtabs(struct readelf *re);
280 static uint8_t *dump_unknown_tag(uint64_t tag, uint8_t *p);
281 static void dump_ver(struct readelf *re);
282 static void dump_verdef(struct readelf *re, int dump);
283 static void dump_verneed(struct readelf *re, int dump);
284 static void dump_versym(struct readelf *re);
285 static struct dumpop *find_dumpop(struct readelf *re, size_t si, const char *sn,
286     int op, int t);
287 static const char *get_string(struct readelf *re, int strtab, size_t off);
288 static const char *get_symbol_name(struct readelf *re, int symtab, int i);
289 static uint64_t get_symbol_value(struct readelf *re, int symtab, int i);
290 static void load_sections(struct readelf *re);
291 static const char *mips_abi_fp(uint64_t fp);
292 static const char *note_type(unsigned int osabi, unsigned int et,
293     unsigned int nt);
294 static const char *option_kind(uint8_t kind);
295 static const char *phdr_type(unsigned int ptype);
296 static const char *ppc_abi_fp(uint64_t fp);
297 static const char *ppc_abi_vector(uint64_t vec);
298 static const char *r_type(unsigned int mach, unsigned int type);
299 static void readelf_usage(void);
300 static void readelf_version(void);
301 static void search_ver(struct readelf *re);
302 static const char *section_type(unsigned int mach, unsigned int stype);
303 static const char *st_bind(unsigned int sbind);
304 static const char *st_shndx(unsigned int shndx);
305 static const char *st_type(unsigned int stype);
306 static const char *st_vis(unsigned int svis);
307 static const char *top_tag(unsigned int tag);
308 static void unload_sections(struct readelf *re);
309 static uint64_t _read_lsb(Elf_Data *d, uint64_t *offsetp,
310     int bytes_to_read);
311 static uint64_t _read_msb(Elf_Data *d, uint64_t *offsetp,
312     int bytes_to_read);
313 static uint64_t _decode_lsb(uint8_t **data, int bytes_to_read);
314 static uint64_t _decode_msb(uint8_t **data, int bytes_to_read);
315 static int64_t _decode_sleb128(uint8_t **dp);
316 static uint64_t _decode_uleb128(uint8_t **dp);
317 
318 static struct eflags_desc arm_eflags_desc[] = {
319 	{EF_ARM_RELEXEC, "relocatable executable"},
320 	{EF_ARM_HASENTRY, "has entry point"},
321 	{EF_ARM_SYMSARESORTED, "sorted symbol tables"},
322 	{EF_ARM_DYNSYMSUSESEGIDX, "dynamic symbols use segment index"},
323 	{EF_ARM_MAPSYMSFIRST, "mapping symbols precede others"},
324 	{EF_ARM_BE8, "BE8"},
325 	{EF_ARM_LE8, "LE8"},
326 	{EF_ARM_INTERWORK, "interworking enabled"},
327 	{EF_ARM_APCS_26, "uses APCS/26"},
328 	{EF_ARM_APCS_FLOAT, "uses APCS/float"},
329 	{EF_ARM_PIC, "position independent"},
330 	{EF_ARM_ALIGN8, "8 bit structure alignment"},
331 	{EF_ARM_NEW_ABI, "uses new ABI"},
332 	{EF_ARM_OLD_ABI, "uses old ABI"},
333 	{EF_ARM_SOFT_FLOAT, "software FP"},
334 	{EF_ARM_VFP_FLOAT, "VFP"},
335 	{EF_ARM_MAVERICK_FLOAT, "Maverick FP"},
336 	{0, NULL}
337 };
338 
339 static struct eflags_desc mips_eflags_desc[] = {
340 	{EF_MIPS_NOREORDER, "noreorder"},
341 	{EF_MIPS_PIC, "pic"},
342 	{EF_MIPS_CPIC, "cpic"},
343 	{EF_MIPS_UCODE, "ugen_reserved"},
344 	{EF_MIPS_ABI2, "abi2"},
345 	{EF_MIPS_OPTIONS_FIRST, "odk first"},
346 	{EF_MIPS_ARCH_ASE_MDMX, "mdmx"},
347 	{EF_MIPS_ARCH_ASE_M16, "mips16"},
348 	{0, NULL}
349 };
350 
351 static struct eflags_desc powerpc_eflags_desc[] = {
352 	{EF_PPC_EMB, "emb"},
353 	{EF_PPC_RELOCATABLE, "relocatable"},
354 	{EF_PPC_RELOCATABLE_LIB, "relocatable-lib"},
355 	{0, NULL}
356 };
357 
358 static struct eflags_desc sparc_eflags_desc[] = {
359 	{EF_SPARC_32PLUS, "v8+"},
360 	{EF_SPARC_SUN_US1, "ultrasparcI"},
361 	{EF_SPARC_HAL_R1, "halr1"},
362 	{EF_SPARC_SUN_US3, "ultrasparcIII"},
363 	{0, NULL}
364 };
365 
366 static const char *
367 elf_osabi(unsigned int abi)
368 {
369 	static char s_abi[32];
370 
371 	switch(abi) {
372 	case ELFOSABI_SYSV: return "SYSV";
373 	case ELFOSABI_HPUX: return "HPUS";
374 	case ELFOSABI_NETBSD: return "NetBSD";
375 	case ELFOSABI_GNU: return "GNU";
376 	case ELFOSABI_HURD: return "HURD";
377 	case ELFOSABI_86OPEN: return "86OPEN";
378 	case ELFOSABI_SOLARIS: return "Solaris";
379 	case ELFOSABI_AIX: return "AIX";
380 	case ELFOSABI_IRIX: return "IRIX";
381 	case ELFOSABI_FREEBSD: return "FreeBSD";
382 	case ELFOSABI_TRU64: return "TRU64";
383 	case ELFOSABI_MODESTO: return "MODESTO";
384 	case ELFOSABI_OPENBSD: return "OpenBSD";
385 	case ELFOSABI_OPENVMS: return "OpenVMS";
386 	case ELFOSABI_NSK: return "NSK";
387 	case ELFOSABI_ARM: return "ARM";
388 	case ELFOSABI_STANDALONE: return "StandAlone";
389 	default:
390 		snprintf(s_abi, sizeof(s_abi), "<unknown: %#x>", abi);
391 		return (s_abi);
392 	}
393 };
394 
395 static const char *
396 elf_machine(unsigned int mach)
397 {
398 	static char s_mach[32];
399 
400 	switch (mach) {
401 	case EM_NONE: return "Unknown machine";
402 	case EM_M32: return "AT&T WE32100";
403 	case EM_SPARC: return "Sun SPARC";
404 	case EM_386: return "Intel i386";
405 	case EM_68K: return "Motorola 68000";
406 	case EM_88K: return "Motorola 88000";
407 	case EM_860: return "Intel i860";
408 	case EM_MIPS: return "MIPS R3000 Big-Endian only";
409 	case EM_S370: return "IBM System/370";
410 	case EM_MIPS_RS3_LE: return "MIPS R3000 Little-Endian";
411 	case EM_PARISC: return "HP PA-RISC";
412 	case EM_VPP500: return "Fujitsu VPP500";
413 	case EM_SPARC32PLUS: return "SPARC v8plus";
414 	case EM_960: return "Intel 80960";
415 	case EM_PPC: return "PowerPC 32-bit";
416 	case EM_PPC64: return "PowerPC 64-bit";
417 	case EM_S390: return "IBM System/390";
418 	case EM_V800: return "NEC V800";
419 	case EM_FR20: return "Fujitsu FR20";
420 	case EM_RH32: return "TRW RH-32";
421 	case EM_RCE: return "Motorola RCE";
422 	case EM_ARM: return "ARM";
423 	case EM_SH: return "Hitachi SH";
424 	case EM_SPARCV9: return "SPARC v9 64-bit";
425 	case EM_TRICORE: return "Siemens TriCore embedded processor";
426 	case EM_ARC: return "Argonaut RISC Core";
427 	case EM_H8_300: return "Hitachi H8/300";
428 	case EM_H8_300H: return "Hitachi H8/300H";
429 	case EM_H8S: return "Hitachi H8S";
430 	case EM_H8_500: return "Hitachi H8/500";
431 	case EM_IA_64: return "Intel IA-64 Processor";
432 	case EM_MIPS_X: return "Stanford MIPS-X";
433 	case EM_COLDFIRE: return "Motorola ColdFire";
434 	case EM_68HC12: return "Motorola M68HC12";
435 	case EM_MMA: return "Fujitsu MMA";
436 	case EM_PCP: return "Siemens PCP";
437 	case EM_NCPU: return "Sony nCPU";
438 	case EM_NDR1: return "Denso NDR1 microprocessor";
439 	case EM_STARCORE: return "Motorola Star*Core processor";
440 	case EM_ME16: return "Toyota ME16 processor";
441 	case EM_ST100: return "STMicroelectronics ST100 processor";
442 	case EM_TINYJ: return "Advanced Logic Corp. TinyJ processor";
443 	case EM_X86_64: return "Advanced Micro Devices x86-64";
444 	case EM_PDSP: return "Sony DSP Processor";
445 	case EM_FX66: return "Siemens FX66 microcontroller";
446 	case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 microcontroller";
447 	case EM_ST7: return "STmicroelectronics ST7 8-bit microcontroller";
448 	case EM_68HC16: return "Motorola MC68HC16 microcontroller";
449 	case EM_68HC11: return "Motorola MC68HC11 microcontroller";
450 	case EM_68HC08: return "Motorola MC68HC08 microcontroller";
451 	case EM_68HC05: return "Motorola MC68HC05 microcontroller";
452 	case EM_SVX: return "Silicon Graphics SVx";
453 	case EM_ST19: return "STMicroelectronics ST19 8-bit mc";
454 	case EM_VAX: return "Digital VAX";
455 	case EM_CRIS: return "Axis Communications 32-bit embedded processor";
456 	case EM_JAVELIN: return "Infineon Tech. 32bit embedded processor";
457 	case EM_FIREPATH: return "Element 14 64-bit DSP Processor";
458 	case EM_ZSP: return "LSI Logic 16-bit DSP Processor";
459 	case EM_MMIX: return "Donald Knuth's educational 64-bit proc";
460 	case EM_HUANY: return "Harvard University MI object files";
461 	case EM_PRISM: return "SiTera Prism";
462 	case EM_AVR: return "Atmel AVR 8-bit microcontroller";
463 	case EM_FR30: return "Fujitsu FR30";
464 	case EM_D10V: return "Mitsubishi D10V";
465 	case EM_D30V: return "Mitsubishi D30V";
466 	case EM_V850: return "NEC v850";
467 	case EM_M32R: return "Mitsubishi M32R";
468 	case EM_MN10300: return "Matsushita MN10300";
469 	case EM_MN10200: return "Matsushita MN10200";
470 	case EM_PJ: return "picoJava";
471 	case EM_OPENRISC: return "OpenRISC 32-bit embedded processor";
472 	case EM_ARC_A5: return "ARC Cores Tangent-A5";
473 	case EM_XTENSA: return "Tensilica Xtensa Architecture";
474 	case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
475 	case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
476 	case EM_NS32K: return "National Semiconductor 32000 series";
477 	case EM_TPC: return "Tenor Network TPC processor";
478 	case EM_SNP1K: return "Trebia SNP 1000 processor";
479 	case EM_ST200: return "STMicroelectronics ST200 microcontroller";
480 	case EM_IP2K: return "Ubicom IP2xxx microcontroller family";
481 	case EM_MAX: return "MAX Processor";
482 	case EM_CR: return "National Semiconductor CompactRISC microprocessor";
483 	case EM_F2MC16: return "Fujitsu F2MC16";
484 	case EM_MSP430: return "TI embedded microcontroller msp430";
485 	case EM_BLACKFIN: return "Analog Devices Blackfin (DSP) processor";
486 	case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
487 	case EM_SEP: return "Sharp embedded microprocessor";
488 	case EM_ARCA: return "Arca RISC Microprocessor";
489 	case EM_UNICORE: return "Microprocessor series from PKU-Unity Ltd";
490 	case EM_AARCH64: return "AArch64";
491 	default:
492 		snprintf(s_mach, sizeof(s_mach), "<unknown: %#x>", mach);
493 		return (s_mach);
494 	}
495 
496 }
497 
498 static const char *
499 elf_class(unsigned int class)
500 {
501 	static char s_class[32];
502 
503 	switch (class) {
504 	case ELFCLASSNONE: return "none";
505 	case ELFCLASS32: return "ELF32";
506 	case ELFCLASS64: return "ELF64";
507 	default:
508 		snprintf(s_class, sizeof(s_class), "<unknown: %#x>", class);
509 		return (s_class);
510 	}
511 }
512 
513 static const char *
514 elf_endian(unsigned int endian)
515 {
516 	static char s_endian[32];
517 
518 	switch (endian) {
519 	case ELFDATANONE: return "none";
520 	case ELFDATA2LSB: return "2's complement, little endian";
521 	case ELFDATA2MSB: return "2's complement, big endian";
522 	default:
523 		snprintf(s_endian, sizeof(s_endian), "<unknown: %#x>", endian);
524 		return (s_endian);
525 	}
526 }
527 
528 static const char *
529 elf_type(unsigned int type)
530 {
531 	static char s_type[32];
532 
533 	switch (type) {
534 	case ET_NONE: return "NONE (None)";
535 	case ET_REL: return "REL (Relocatable file)";
536 	case ET_EXEC: return "EXEC (Executable file)";
537 	case ET_DYN: return "DYN (Shared object file)";
538 	case ET_CORE: return "CORE (Core file)";
539 	default:
540 		if (type >= ET_LOPROC)
541 			snprintf(s_type, sizeof(s_type), "<proc: %#x>", type);
542 		else if (type >= ET_LOOS && type <= ET_HIOS)
543 			snprintf(s_type, sizeof(s_type), "<os: %#x>", type);
544 		else
545 			snprintf(s_type, sizeof(s_type), "<unknown: %#x>",
546 			    type);
547 		return (s_type);
548 	}
549 }
550 
551 static const char *
552 elf_ver(unsigned int ver)
553 {
554 	static char s_ver[32];
555 
556 	switch (ver) {
557 	case EV_CURRENT: return "(current)";
558 	case EV_NONE: return "(none)";
559 	default:
560 		snprintf(s_ver, sizeof(s_ver), "<unknown: %#x>",
561 		    ver);
562 		return (s_ver);
563 	}
564 }
565 
566 static const char *
567 phdr_type(unsigned int ptype)
568 {
569 	static char s_ptype[32];
570 
571 	switch (ptype) {
572 	case PT_NULL: return "NULL";
573 	case PT_LOAD: return "LOAD";
574 	case PT_DYNAMIC: return "DYNAMIC";
575 	case PT_INTERP: return "INTERP";
576 	case PT_NOTE: return "NOTE";
577 	case PT_SHLIB: return "SHLIB";
578 	case PT_PHDR: return "PHDR";
579 	case PT_TLS: return "TLS";
580 	case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
581 	case PT_GNU_STACK: return "GNU_STACK";
582 	case PT_GNU_RELRO: return "GNU_RELRO";
583 	default:
584 		if (ptype >= PT_LOPROC && ptype <= PT_HIPROC)
585 			snprintf(s_ptype, sizeof(s_ptype), "LOPROC+%#x",
586 			    ptype - PT_LOPROC);
587 		else if (ptype >= PT_LOOS && ptype <= PT_HIOS)
588 			snprintf(s_ptype, sizeof(s_ptype), "LOOS+%#x",
589 			    ptype - PT_LOOS);
590 		else
591 			snprintf(s_ptype, sizeof(s_ptype), "<unknown: %#x>",
592 			    ptype);
593 		return (s_ptype);
594 	}
595 }
596 
597 static const char *
598 section_type(unsigned int mach, unsigned int stype)
599 {
600 	static char s_stype[32];
601 
602 	if (stype >= SHT_LOPROC && stype <= SHT_HIPROC) {
603 		switch (mach) {
604 		case EM_X86_64:
605 			switch (stype) {
606 			case SHT_AMD64_UNWIND: return "AMD64_UNWIND";
607 			default:
608 				break;
609 			}
610 			break;
611 		case EM_MIPS:
612 		case EM_MIPS_RS3_LE:
613 			switch (stype) {
614 			case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
615 			case SHT_MIPS_MSYM: return "MIPS_MSYM";
616 			case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
617 			case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
618 			case SHT_MIPS_UCODE: return "MIPS_UCODE";
619 			case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
620 			case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
621 			case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
622 			case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
623 			case SHT_MIPS_RELD: return "MIPS_RELD";
624 			case SHT_MIPS_IFACE: return "MIPS_IFACE";
625 			case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
626 			case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
627 			case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
628 			case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
629 			case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
630 			case SHT_MIPS_DWARF: return "MIPS_DWARF";
631 			case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
632 			case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
633 			case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
634 			case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
635 			case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
636 			case SHT_MIPS_XLATE: return "MIPS_XLATE";
637 			case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
638 			case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
639 			case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
640 			case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
641 			case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
642 			default:
643 				break;
644 			}
645 			break;
646 		default:
647 			break;
648 		}
649 
650 		snprintf(s_stype, sizeof(s_stype), "LOPROC+%#x",
651 		    stype - SHT_LOPROC);
652 		return (s_stype);
653 	}
654 
655 	switch (stype) {
656 	case SHT_NULL: return "NULL";
657 	case SHT_PROGBITS: return "PROGBITS";
658 	case SHT_SYMTAB: return "SYMTAB";
659 	case SHT_STRTAB: return "STRTAB";
660 	case SHT_RELA: return "RELA";
661 	case SHT_HASH: return "HASH";
662 	case SHT_DYNAMIC: return "DYNAMIC";
663 	case SHT_NOTE: return "NOTE";
664 	case SHT_NOBITS: return "NOBITS";
665 	case SHT_REL: return "REL";
666 	case SHT_SHLIB: return "SHLIB";
667 	case SHT_DYNSYM: return "DYNSYM";
668 	case SHT_INIT_ARRAY: return "INIT_ARRAY";
669 	case SHT_FINI_ARRAY: return "FINI_ARRAY";
670 	case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
671 	case SHT_GROUP: return "GROUP";
672 	case SHT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
673 	case SHT_SUNW_dof: return "SUNW_dof";
674 	case SHT_SUNW_cap: return "SUNW_cap";
675 	case SHT_GNU_HASH: return "GNU_HASH";
676 	case SHT_SUNW_ANNOTATE: return "SUNW_ANNOTATE";
677 	case SHT_SUNW_DEBUGSTR: return "SUNW_DEBUGSTR";
678 	case SHT_SUNW_DEBUG: return "SUNW_DEBUG";
679 	case SHT_SUNW_move: return "SUNW_move";
680 	case SHT_SUNW_COMDAT: return "SUNW_COMDAT";
681 	case SHT_SUNW_syminfo: return "SUNW_syminfo";
682 	case SHT_SUNW_verdef: return "SUNW_verdef";
683 	case SHT_SUNW_verneed: return "SUNW_verneed";
684 	case SHT_SUNW_versym: return "SUNW_versym";
685 	default:
686 		if (stype >= SHT_LOOS && stype <= SHT_HIOS)
687 			snprintf(s_stype, sizeof(s_stype), "LOOS+%#x",
688 			    stype - SHT_LOOS);
689 		else if (stype >= SHT_LOUSER)
690 			snprintf(s_stype, sizeof(s_stype), "LOUSER+%#x",
691 			    stype - SHT_LOUSER);
692 		else
693 			snprintf(s_stype, sizeof(s_stype), "<unknown: %#x>",
694 			    stype);
695 		return (s_stype);
696 	}
697 }
698 
699 static const char *
700 dt_type(unsigned int mach, unsigned int dtype)
701 {
702 	static char s_dtype[32];
703 
704 	if (dtype >= DT_LOPROC && dtype <= DT_HIPROC) {
705 		switch (mach) {
706 		case EM_ARM:
707 			switch (dtype) {
708 			case DT_ARM_SYMTABSZ:
709 				return "ARM_SYMTABSZ";
710 			default:
711 				break;
712 			}
713 			break;
714 		case EM_MIPS:
715 		case EM_MIPS_RS3_LE:
716 			switch (dtype) {
717 			case DT_MIPS_RLD_VERSION:
718 				return "MIPS_RLD_VERSION";
719 			case DT_MIPS_TIME_STAMP:
720 				return "MIPS_TIME_STAMP";
721 			case DT_MIPS_ICHECKSUM:
722 				return "MIPS_ICHECKSUM";
723 			case DT_MIPS_IVERSION:
724 				return "MIPS_IVERSION";
725 			case DT_MIPS_FLAGS:
726 				return "MIPS_FLAGS";
727 			case DT_MIPS_BASE_ADDRESS:
728 				return "MIPS_BASE_ADDRESS";
729 			case DT_MIPS_CONFLICT:
730 				return "MIPS_CONFLICT";
731 			case DT_MIPS_LIBLIST:
732 				return "MIPS_LIBLIST";
733 			case DT_MIPS_LOCAL_GOTNO:
734 				return "MIPS_LOCAL_GOTNO";
735 			case DT_MIPS_CONFLICTNO:
736 				return "MIPS_CONFLICTNO";
737 			case DT_MIPS_LIBLISTNO:
738 				return "MIPS_LIBLISTNO";
739 			case DT_MIPS_SYMTABNO:
740 				return "MIPS_SYMTABNO";
741 			case DT_MIPS_UNREFEXTNO:
742 				return "MIPS_UNREFEXTNO";
743 			case DT_MIPS_GOTSYM:
744 				return "MIPS_GOTSYM";
745 			case DT_MIPS_HIPAGENO:
746 				return "MIPS_HIPAGENO";
747 			case DT_MIPS_RLD_MAP:
748 				return "MIPS_RLD_MAP";
749 			case DT_MIPS_DELTA_CLASS:
750 				return "MIPS_DELTA_CLASS";
751 			case DT_MIPS_DELTA_CLASS_NO:
752 				return "MIPS_DELTA_CLASS_NO";
753 			case DT_MIPS_DELTA_INSTANCE:
754 				return "MIPS_DELTA_INSTANCE";
755 			case DT_MIPS_DELTA_INSTANCE_NO:
756 				return "MIPS_DELTA_INSTANCE_NO";
757 			case DT_MIPS_DELTA_RELOC:
758 				return "MIPS_DELTA_RELOC";
759 			case DT_MIPS_DELTA_RELOC_NO:
760 				return "MIPS_DELTA_RELOC_NO";
761 			case DT_MIPS_DELTA_SYM:
762 				return "MIPS_DELTA_SYM";
763 			case DT_MIPS_DELTA_SYM_NO:
764 				return "MIPS_DELTA_SYM_NO";
765 			case DT_MIPS_DELTA_CLASSSYM:
766 				return "MIPS_DELTA_CLASSSYM";
767 			case DT_MIPS_DELTA_CLASSSYM_NO:
768 				return "MIPS_DELTA_CLASSSYM_NO";
769 			case DT_MIPS_CXX_FLAGS:
770 				return "MIPS_CXX_FLAGS";
771 			case DT_MIPS_PIXIE_INIT:
772 				return "MIPS_PIXIE_INIT";
773 			case DT_MIPS_SYMBOL_LIB:
774 				return "MIPS_SYMBOL_LIB";
775 			case DT_MIPS_LOCALPAGE_GOTIDX:
776 				return "MIPS_LOCALPAGE_GOTIDX";
777 			case DT_MIPS_LOCAL_GOTIDX:
778 				return "MIPS_LOCAL_GOTIDX";
779 			case DT_MIPS_HIDDEN_GOTIDX:
780 				return "MIPS_HIDDEN_GOTIDX";
781 			case DT_MIPS_PROTECTED_GOTIDX:
782 				return "MIPS_PROTECTED_GOTIDX";
783 			case DT_MIPS_OPTIONS:
784 				return "MIPS_OPTIONS";
785 			case DT_MIPS_INTERFACE:
786 				return "MIPS_INTERFACE";
787 			case DT_MIPS_DYNSTR_ALIGN:
788 				return "MIPS_DYNSTR_ALIGN";
789 			case DT_MIPS_INTERFACE_SIZE:
790 				return "MIPS_INTERFACE_SIZE";
791 			case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
792 				return "MIPS_RLD_TEXT_RESOLVE_ADDR";
793 			case DT_MIPS_PERF_SUFFIX:
794 				return "MIPS_PERF_SUFFIX";
795 			case DT_MIPS_COMPACT_SIZE:
796 				return "MIPS_COMPACT_SIZE";
797 			case DT_MIPS_GP_VALUE:
798 				return "MIPS_GP_VALUE";
799 			case DT_MIPS_AUX_DYNAMIC:
800 				return "MIPS_AUX_DYNAMIC";
801 			case DT_MIPS_PLTGOT:
802 				return "MIPS_PLTGOT";
803 			case DT_MIPS_RLD_OBJ_UPDATE:
804 				return "MIPS_RLD_OBJ_UPDATE";
805 			case DT_MIPS_RWPLT:
806 				return "MIPS_RWPLT";
807 			default:
808 				break;
809 			}
810 			break;
811 		case EM_SPARC:
812 		case EM_SPARC32PLUS:
813 		case EM_SPARCV9:
814 			switch (dtype) {
815 			case DT_SPARC_REGISTER:
816 				return "DT_SPARC_REGISTER";
817 			default:
818 				break;
819 			}
820 			break;
821 		default:
822 			break;
823 		}
824 		snprintf(s_dtype, sizeof(s_dtype), "<unknown: %#x>", dtype);
825 		return (s_dtype);
826 	}
827 
828 	switch (dtype) {
829 	case DT_NULL: return "NULL";
830 	case DT_NEEDED: return "NEEDED";
831 	case DT_PLTRELSZ: return "PLTRELSZ";
832 	case DT_PLTGOT: return "PLTGOT";
833 	case DT_HASH: return "HASH";
834 	case DT_STRTAB: return "STRTAB";
835 	case DT_SYMTAB: return "SYMTAB";
836 	case DT_RELA: return "RELA";
837 	case DT_RELASZ: return "RELASZ";
838 	case DT_RELAENT: return "RELAENT";
839 	case DT_STRSZ: return "STRSZ";
840 	case DT_SYMENT: return "SYMENT";
841 	case DT_INIT: return "INIT";
842 	case DT_FINI: return "FINI";
843 	case DT_SONAME: return "SONAME";
844 	case DT_RPATH: return "RPATH";
845 	case DT_SYMBOLIC: return "SYMBOLIC";
846 	case DT_REL: return "REL";
847 	case DT_RELSZ: return "RELSZ";
848 	case DT_RELENT: return "RELENT";
849 	case DT_PLTREL: return "PLTREL";
850 	case DT_DEBUG: return "DEBUG";
851 	case DT_TEXTREL: return "TEXTREL";
852 	case DT_JMPREL: return "JMPREL";
853 	case DT_BIND_NOW: return "BIND_NOW";
854 	case DT_INIT_ARRAY: return "INIT_ARRAY";
855 	case DT_FINI_ARRAY: return "FINI_ARRAY";
856 	case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
857 	case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
858 	case DT_RUNPATH: return "RUNPATH";
859 	case DT_FLAGS: return "FLAGS";
860 	case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
861 	case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
862 	case DT_MAXPOSTAGS: return "MAXPOSTAGS";
863 	case DT_SUNW_AUXILIARY: return "SUNW_AUXILIARY";
864 	case DT_SUNW_RTLDINF: return "SUNW_RTLDINF";
865 	case DT_SUNW_FILTER: return "SUNW_FILTER";
866 	case DT_SUNW_CAP: return "SUNW_CAP";
867 	case DT_CHECKSUM: return "CHECKSUM";
868 	case DT_PLTPADSZ: return "PLTPADSZ";
869 	case DT_MOVEENT: return "MOVEENT";
870 	case DT_MOVESZ: return "MOVESZ";
871 	case DT_FEATURE_1: return "FEATURE_1";
872 	case DT_POSFLAG_1: return "POSFLAG_1";
873 	case DT_SYMINSZ: return "SYMINSZ";
874 	case DT_SYMINENT: return "SYMINENT";
875 	case DT_GNU_HASH: return "GNU_HASH";
876 	case DT_GNU_CONFLICT: return "GNU_CONFLICT";
877 	case DT_GNU_LIBLIST: return "GNU_LIBLIST";
878 	case DT_CONFIG: return "CONFIG";
879 	case DT_DEPAUDIT: return "DEPAUDIT";
880 	case DT_AUDIT: return "AUDIT";
881 	case DT_PLTPAD: return "PLTPAD";
882 	case DT_MOVETAB: return "MOVETAB";
883 	case DT_SYMINFO: return "SYMINFO";
884 	case DT_VERSYM: return "VERSYM";
885 	case DT_RELACOUNT: return "RELACOUNT";
886 	case DT_RELCOUNT: return "RELCOUNT";
887 	case DT_FLAGS_1: return "FLAGS_1";
888 	case DT_VERDEF: return "VERDEF";
889 	case DT_VERDEFNUM: return "VERDEFNUM";
890 	case DT_VERNEED: return "VERNEED";
891 	case DT_VERNEEDNUM: return "VERNEEDNUM";
892 	case DT_AUXILIARY: return "AUXILIARY";
893 	case DT_USED: return "USED";
894 	case DT_FILTER: return "FILTER";
895 	case DT_GNU_PRELINKED: return "GNU_PRELINKED";
896 	case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
897 	case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
898 	default:
899 		snprintf(s_dtype, sizeof(s_dtype), "<unknown: %#x>", dtype);
900 		return (s_dtype);
901 	}
902 }
903 
904 static const char *
905 st_bind(unsigned int sbind)
906 {
907 	static char s_sbind[32];
908 
909 	switch (sbind) {
910 	case STB_LOCAL: return "LOCAL";
911 	case STB_GLOBAL: return "GLOBAL";
912 	case STB_WEAK: return "WEAK";
913 	default:
914 		if (sbind >= STB_LOOS && sbind <= STB_HIOS)
915 			return "OS";
916 		else if (sbind >= STB_LOPROC && sbind <= STB_HIPROC)
917 			return "PROC";
918 		else
919 			snprintf(s_sbind, sizeof(s_sbind), "<unknown: %#x>",
920 			    sbind);
921 		return (s_sbind);
922 	}
923 }
924 
925 static const char *
926 st_type(unsigned int stype)
927 {
928 	static char s_stype[32];
929 
930 	switch (stype) {
931 	case STT_NOTYPE: return "NOTYPE";
932 	case STT_OBJECT: return "OBJECT";
933 	case STT_FUNC: return "FUNC";
934 	case STT_SECTION: return "SECTION";
935 	case STT_FILE: return "FILE";
936 	case STT_COMMON: return "COMMON";
937 	case STT_TLS: return "TLS";
938 	default:
939 		if (stype >= STT_LOOS && stype <= STT_HIOS)
940 			snprintf(s_stype, sizeof(s_stype), "OS+%#x",
941 			    stype - STT_LOOS);
942 		else if (stype >= STT_LOPROC && stype <= STT_HIPROC)
943 			snprintf(s_stype, sizeof(s_stype), "PROC+%#x",
944 			    stype - STT_LOPROC);
945 		else
946 			snprintf(s_stype, sizeof(s_stype), "<unknown: %#x>",
947 			    stype);
948 		return (s_stype);
949 	}
950 }
951 
952 static const char *
953 st_vis(unsigned int svis)
954 {
955 	static char s_svis[32];
956 
957 	switch(svis) {
958 	case STV_DEFAULT: return "DEFAULT";
959 	case STV_INTERNAL: return "INTERNAL";
960 	case STV_HIDDEN: return "HIDDEN";
961 	case STV_PROTECTED: return "PROTECTED";
962 	default:
963 		snprintf(s_svis, sizeof(s_svis), "<unknown: %#x>", svis);
964 		return (s_svis);
965 	}
966 }
967 
968 static const char *
969 st_shndx(unsigned int shndx)
970 {
971 	static char s_shndx[32];
972 
973 	switch (shndx) {
974 	case SHN_UNDEF: return "UND";
975 	case SHN_ABS: return "ABS";
976 	case SHN_COMMON: return "COM";
977 	default:
978 		if (shndx >= SHN_LOPROC && shndx <= SHN_HIPROC)
979 			return "PRC";
980 		else if (shndx >= SHN_LOOS && shndx <= SHN_HIOS)
981 			return "OS";
982 		else
983 			snprintf(s_shndx, sizeof(s_shndx), "%u", shndx);
984 		return (s_shndx);
985 	}
986 }
987 
988 static struct {
989 	const char *ln;
990 	char sn;
991 	int value;
992 } section_flag[] = {
993 	{"WRITE", 'W', SHF_WRITE},
994 	{"ALLOC", 'A', SHF_ALLOC},
995 	{"EXEC", 'X', SHF_EXECINSTR},
996 	{"MERGE", 'M', SHF_MERGE},
997 	{"STRINGS", 'S', SHF_STRINGS},
998 	{"INFO LINK", 'I', SHF_INFO_LINK},
999 	{"OS NONCONF", 'O', SHF_OS_NONCONFORMING},
1000 	{"GROUP", 'G', SHF_GROUP},
1001 	{"TLS", 'T', SHF_TLS},
1002 	{NULL, 0, 0}
1003 };
1004 
1005 static const char *
1006 r_type(unsigned int mach, unsigned int type)
1007 {
1008 	switch(mach) {
1009 	case EM_NONE: return "";
1010 	case EM_386:
1011 		switch(type) {
1012 		case 0: return "R_386_NONE";
1013 		case 1: return "R_386_32";
1014 		case 2: return "R_386_PC32";
1015 		case 3: return "R_386_GOT32";
1016 		case 4: return "R_386_PLT32";
1017 		case 5: return "R_386_COPY";
1018 		case 6: return "R_386_GLOB_DAT";
1019 		case 7: return "R_386_JMP_SLOT";
1020 		case 8: return "R_386_RELATIVE";
1021 		case 9: return "R_386_GOTOFF";
1022 		case 10: return "R_386_GOTPC";
1023 		case 14: return "R_386_TLS_TPOFF";
1024 		case 15: return "R_386_TLS_IE";
1025 		case 16: return "R_386_TLS_GOTIE";
1026 		case 17: return "R_386_TLS_LE";
1027 		case 18: return "R_386_TLS_GD";
1028 		case 19: return "R_386_TLS_LDM";
1029 		case 24: return "R_386_TLS_GD_32";
1030 		case 25: return "R_386_TLS_GD_PUSH";
1031 		case 26: return "R_386_TLS_GD_CALL";
1032 		case 27: return "R_386_TLS_GD_POP";
1033 		case 28: return "R_386_TLS_LDM_32";
1034 		case 29: return "R_386_TLS_LDM_PUSH";
1035 		case 30: return "R_386_TLS_LDM_CALL";
1036 		case 31: return "R_386_TLS_LDM_POP";
1037 		case 32: return "R_386_TLS_LDO_32";
1038 		case 33: return "R_386_TLS_IE_32";
1039 		case 34: return "R_386_TLS_LE_32";
1040 		case 35: return "R_386_TLS_DTPMOD32";
1041 		case 36: return "R_386_TLS_DTPOFF32";
1042 		case 37: return "R_386_TLS_TPOFF32";
1043 		default: return "";
1044 		}
1045 	case EM_AARCH64:
1046 		switch(type) {
1047 		case 0: return "R_AARCH64_NONE";
1048 		case 257: return "R_AARCH64_ABS64";
1049 		case 258: return "R_AARCH64_ABS32";
1050 		case 259: return "R_AARCH64_ABS16";
1051 		case 260: return "R_AARCH64_PREL64";
1052 		case 261: return "R_AARCH64_PREL32";
1053 		case 262: return "R_AARCH64_PREL16";
1054 		case 263: return "R_AARCH64_MOVW_UABS_G0";
1055 		case 264: return "R_AARCH64_MOVW_UABS_G0_NC";
1056 		case 265: return "R_AARCH64_MOVW_UABS_G1";
1057 		case 266: return "R_AARCH64_MOVW_UABS_G1_NC";
1058 		case 267: return "R_AARCH64_MOVW_UABS_G2";
1059 		case 268: return "R_AARCH64_MOVW_UABS_G2_NC";
1060 		case 269: return "R_AARCH64_MOVW_UABS_G3";
1061 		case 270: return "R_AARCH64_MOVW_SABS_G0";
1062 		case 271: return "R_AARCH64_MOVW_SABS_G1";
1063 		case 272: return "R_AARCH64_MOVW_SABS_G2";
1064 		case 273: return "R_AARCH64_LD_PREL_LO19";
1065 		case 274: return "R_AARCH64_ADR_PREL_LO21";
1066 		case 275: return "R_AARCH64_ADR_PREL_PG_HI21";
1067 		case 276: return "R_AARCH64_ADR_PREL_PG_HI21_NC";
1068 		case 277: return "R_AARCH64_ADD_ABS_LO12_NC";
1069 		case 278: return "R_AARCH64_LDST8_ABS_LO12_NC";
1070 		case 279: return "R_AARCH64_TSTBR14";
1071 		case 280: return "R_AARCH64_CONDBR19";
1072 		case 282: return "R_AARCH64_JUMP26";
1073 		case 283: return "R_AARCH64_CALL26";
1074 		case 284: return "R_AARCH64_LDST16_ABS_LO12_NC";
1075 		case 285: return "R_AARCH64_LDST32_ABS_LO12_NC";
1076 		case 286: return "R_AARCH64_LDST64_ABS_LO12_NC";
1077 		case 287: return "R_AARCH64_MOVW_PREL_G0";
1078 		case 288: return "R_AARCH64_MOVW_PREL_G0_NC";
1079 		case 289: return "R_AARCH64_MOVW_PREL_G1";
1080 		case 290: return "R_AARCH64_MOVW_PREL_G1_NC";
1081 		case 291: return "R_AARCH64_MOVW_PREL_G2";
1082 		case 292: return "R_AARCH64_MOVW_PREL_G2_NC";
1083 		case 293: return "R_AARCH64_MOVW_PREL_G3";
1084 		case 299: return "R_AARCH64_LDST128_ABS_LO12_NC";
1085 		case 300: return "R_AARCH64_MOVW_GOTOFF_G0";
1086 		case 301: return "R_AARCH64_MOVW_GOTOFF_G0_NC";
1087 		case 302: return "R_AARCH64_MOVW_GOTOFF_G1";
1088 		case 303: return "R_AARCH64_MOVW_GOTOFF_G1_NC";
1089 		case 304: return "R_AARCH64_MOVW_GOTOFF_G2";
1090 		case 305: return "R_AARCH64_MOVW_GOTOFF_G2_NC";
1091 		case 306: return "R_AARCH64_MOVW_GOTOFF_G3";
1092 		case 307: return "R_AARCH64_GOTREL64";
1093 		case 308: return "R_AARCH64_GOTREL32";
1094 		case 309: return "R_AARCH64_GOT_LD_PREL19";
1095 		case 310: return "R_AARCH64_LD64_GOTOFF_LO15";
1096 		case 311: return "R_AARCH64_ADR_GOT_PAGE";
1097 		case 312: return "R_AARCH64_LD64_GOT_LO12_NC";
1098 		case 313: return "R_AARCH64_LD64_GOTPAGE_LO15";
1099 		case 1024: return "R_AARCH64_COPY";
1100 		case 1025: return "R_AARCH64_GLOB_DAT";
1101 		case 1026: return "R_AARCH64_JUMP_SLOT";
1102 		case 1027: return "R_AARCH64_RELATIVE";
1103 		case 1031: return "R_AARCH64_TLSDESC";
1104 		default: return "";
1105 		}
1106 	case EM_ARM:
1107 		switch(type) {
1108 		case 0: return "R_ARM_NONE";
1109 		case 1: return "R_ARM_PC24";
1110 		case 2: return "R_ARM_ABS32";
1111 		case 3: return "R_ARM_REL32";
1112 		case 4: return "R_ARM_PC13";
1113 		case 5: return "R_ARM_ABS16";
1114 		case 6: return "R_ARM_ABS12";
1115 		case 7: return "R_ARM_THM_ABS5";
1116 		case 8: return "R_ARM_ABS8";
1117 		case 9: return "R_ARM_SBREL32";
1118 		case 10: return "R_ARM_THM_PC22";
1119 		case 11: return "R_ARM_THM_PC8";
1120 		case 12: return "R_ARM_AMP_VCALL9";
1121 		case 13: return "R_ARM_SWI24";
1122 		case 14: return "R_ARM_THM_SWI8";
1123 		case 15: return "R_ARM_XPC25";
1124 		case 16: return "R_ARM_THM_XPC22";
1125 		case 20: return "R_ARM_COPY";
1126 		case 21: return "R_ARM_GLOB_DAT";
1127 		case 22: return "R_ARM_JUMP_SLOT";
1128 		case 23: return "R_ARM_RELATIVE";
1129 		case 24: return "R_ARM_GOTOFF";
1130 		case 25: return "R_ARM_GOTPC";
1131 		case 26: return "R_ARM_GOT32";
1132 		case 27: return "R_ARM_PLT32";
1133 		case 100: return "R_ARM_GNU_VTENTRY";
1134 		case 101: return "R_ARM_GNU_VTINHERIT";
1135 		case 250: return "R_ARM_RSBREL32";
1136 		case 251: return "R_ARM_THM_RPC22";
1137 		case 252: return "R_ARM_RREL32";
1138 		case 253: return "R_ARM_RABS32";
1139 		case 254: return "R_ARM_RPC24";
1140 		case 255: return "R_ARM_RBASE";
1141 		default: return "";
1142 		}
1143 	case EM_IA_64:
1144 		switch(type) {
1145 		case 0: return "R_IA_64_NONE";
1146 		case 33: return "R_IA_64_IMM14";
1147 		case 34: return "R_IA_64_IMM22";
1148 		case 35: return "R_IA_64_IMM64";
1149 		case 36: return "R_IA_64_DIR32MSB";
1150 		case 37: return "R_IA_64_DIR32LSB";
1151 		case 38: return "R_IA_64_DIR64MSB";
1152 		case 39: return "R_IA_64_DIR64LSB";
1153 		case 42: return "R_IA_64_GPREL22";
1154 		case 43: return "R_IA_64_GPREL64I";
1155 		case 44: return "R_IA_64_GPREL32MSB";
1156 		case 45: return "R_IA_64_GPREL32LSB";
1157 		case 46: return "R_IA_64_GPREL64MSB";
1158 		case 47: return "R_IA_64_GPREL64LSB";
1159 		case 50: return "R_IA_64_LTOFF22";
1160 		case 51: return "R_IA_64_LTOFF64I";
1161 		case 58: return "R_IA_64_PLTOFF22";
1162 		case 59: return "R_IA_64_PLTOFF64I";
1163 		case 62: return "R_IA_64_PLTOFF64MSB";
1164 		case 63: return "R_IA_64_PLTOFF64LSB";
1165 		case 67: return "R_IA_64_FPTR64I";
1166 		case 68: return "R_IA_64_FPTR32MSB";
1167 		case 69: return "R_IA_64_FPTR32LSB";
1168 		case 70: return "R_IA_64_FPTR64MSB";
1169 		case 71: return "R_IA_64_FPTR64LSB";
1170 		case 72: return "R_IA_64_PCREL60B";
1171 		case 73: return "R_IA_64_PCREL21B";
1172 		case 74: return "R_IA_64_PCREL21M";
1173 		case 75: return "R_IA_64_PCREL21F";
1174 		case 76: return "R_IA_64_PCREL32MSB";
1175 		case 77: return "R_IA_64_PCREL32LSB";
1176 		case 78: return "R_IA_64_PCREL64MSB";
1177 		case 79: return "R_IA_64_PCREL64LSB";
1178 		case 82: return "R_IA_64_LTOFF_FPTR22";
1179 		case 83: return "R_IA_64_LTOFF_FPTR64I";
1180 		case 84: return "R_IA_64_LTOFF_FPTR32MSB";
1181 		case 85: return "R_IA_64_LTOFF_FPTR32LSB";
1182 		case 86: return "R_IA_64_LTOFF_FPTR64MSB";
1183 		case 87: return "R_IA_64_LTOFF_FPTR64LSB";
1184 		case 92: return "R_IA_64_SEGREL32MSB";
1185 		case 93: return "R_IA_64_SEGREL32LSB";
1186 		case 94: return "R_IA_64_SEGREL64MSB";
1187 		case 95: return "R_IA_64_SEGREL64LSB";
1188 		case 100: return "R_IA_64_SECREL32MSB";
1189 		case 101: return "R_IA_64_SECREL32LSB";
1190 		case 102: return "R_IA_64_SECREL64MSB";
1191 		case 103: return "R_IA_64_SECREL64LSB";
1192 		case 108: return "R_IA_64_REL32MSB";
1193 		case 109: return "R_IA_64_REL32LSB";
1194 		case 110: return "R_IA_64_REL64MSB";
1195 		case 111: return "R_IA_64_REL64LSB";
1196 		case 116: return "R_IA_64_LTV32MSB";
1197 		case 117: return "R_IA_64_LTV32LSB";
1198 		case 118: return "R_IA_64_LTV64MSB";
1199 		case 119: return "R_IA_64_LTV64LSB";
1200 		case 121: return "R_IA_64_PCREL21BI";
1201 		case 122: return "R_IA_64_PCREL22";
1202 		case 123: return "R_IA_64_PCREL64I";
1203 		case 128: return "R_IA_64_IPLTMSB";
1204 		case 129: return "R_IA_64_IPLTLSB";
1205 		case 133: return "R_IA_64_SUB";
1206 		case 134: return "R_IA_64_LTOFF22X";
1207 		case 135: return "R_IA_64_LDXMOV";
1208 		case 145: return "R_IA_64_TPREL14";
1209 		case 146: return "R_IA_64_TPREL22";
1210 		case 147: return "R_IA_64_TPREL64I";
1211 		case 150: return "R_IA_64_TPREL64MSB";
1212 		case 151: return "R_IA_64_TPREL64LSB";
1213 		case 154: return "R_IA_64_LTOFF_TPREL22";
1214 		case 166: return "R_IA_64_DTPMOD64MSB";
1215 		case 167: return "R_IA_64_DTPMOD64LSB";
1216 		case 170: return "R_IA_64_LTOFF_DTPMOD22";
1217 		case 177: return "R_IA_64_DTPREL14";
1218 		case 178: return "R_IA_64_DTPREL22";
1219 		case 179: return "R_IA_64_DTPREL64I";
1220 		case 180: return "R_IA_64_DTPREL32MSB";
1221 		case 181: return "R_IA_64_DTPREL32LSB";
1222 		case 182: return "R_IA_64_DTPREL64MSB";
1223 		case 183: return "R_IA_64_DTPREL64LSB";
1224 		case 186: return "R_IA_64_LTOFF_DTPREL22";
1225 		default: return "";
1226 		}
1227 	case EM_MIPS:
1228 		switch(type) {
1229 		case 0: return "R_MIPS_NONE";
1230 		case 1: return "R_MIPS_16";
1231 		case 2: return "R_MIPS_32";
1232 		case 3: return "R_MIPS_REL32";
1233 		case 4: return "R_MIPS_26";
1234 		case 5: return "R_MIPS_HI16";
1235 		case 6: return "R_MIPS_LO16";
1236 		case 7: return "R_MIPS_GPREL16";
1237 		case 8: return "R_MIPS_LITERAL";
1238 		case 9: return "R_MIPS_GOT16";
1239 		case 10: return "R_MIPS_PC16";
1240 		case 11: return "R_MIPS_CALL16";
1241 		case 12: return "R_MIPS_GPREL32";
1242 		case 21: return "R_MIPS_GOTHI16";
1243 		case 22: return "R_MIPS_GOTLO16";
1244 		case 30: return "R_MIPS_CALLHI16";
1245 		case 31: return "R_MIPS_CALLLO16";
1246 		default: return "";
1247 		}
1248 	case EM_PPC:
1249 		switch(type) {
1250 		case 0: return "R_PPC_NONE";
1251 		case 1: return "R_PPC_ADDR32";
1252 		case 2: return "R_PPC_ADDR24";
1253 		case 3: return "R_PPC_ADDR16";
1254 		case 4: return "R_PPC_ADDR16_LO";
1255 		case 5: return "R_PPC_ADDR16_HI";
1256 		case 6: return "R_PPC_ADDR16_HA";
1257 		case 7: return "R_PPC_ADDR14";
1258 		case 8: return "R_PPC_ADDR14_BRTAKEN";
1259 		case 9: return "R_PPC_ADDR14_BRNTAKEN";
1260 		case 10: return "R_PPC_REL24";
1261 		case 11: return "R_PPC_REL14";
1262 		case 12: return "R_PPC_REL14_BRTAKEN";
1263 		case 13: return "R_PPC_REL14_BRNTAKEN";
1264 		case 14: return "R_PPC_GOT16";
1265 		case 15: return "R_PPC_GOT16_LO";
1266 		case 16: return "R_PPC_GOT16_HI";
1267 		case 17: return "R_PPC_GOT16_HA";
1268 		case 18: return "R_PPC_PLTREL24";
1269 		case 19: return "R_PPC_COPY";
1270 		case 20: return "R_PPC_GLOB_DAT";
1271 		case 21: return "R_PPC_JMP_SLOT";
1272 		case 22: return "R_PPC_RELATIVE";
1273 		case 23: return "R_PPC_LOCAL24PC";
1274 		case 24: return "R_PPC_UADDR32";
1275 		case 25: return "R_PPC_UADDR16";
1276 		case 26: return "R_PPC_REL32";
1277 		case 27: return "R_PPC_PLT32";
1278 		case 28: return "R_PPC_PLTREL32";
1279 		case 29: return "R_PPC_PLT16_LO";
1280 		case 30: return "R_PPC_PLT16_HI";
1281 		case 31: return "R_PPC_PLT16_HA";
1282 		case 32: return "R_PPC_SDAREL16";
1283 		case 33: return "R_PPC_SECTOFF";
1284 		case 34: return "R_PPC_SECTOFF_LO";
1285 		case 35: return "R_PPC_SECTOFF_HI";
1286 		case 36: return "R_PPC_SECTOFF_HA";
1287 		case 67: return "R_PPC_TLS";
1288 		case 68: return "R_PPC_DTPMOD32";
1289 		case 69: return "R_PPC_TPREL16";
1290 		case 70: return "R_PPC_TPREL16_LO";
1291 		case 71: return "R_PPC_TPREL16_HI";
1292 		case 72: return "R_PPC_TPREL16_HA";
1293 		case 73: return "R_PPC_TPREL32";
1294 		case 74: return "R_PPC_DTPREL16";
1295 		case 75: return "R_PPC_DTPREL16_LO";
1296 		case 76: return "R_PPC_DTPREL16_HI";
1297 		case 77: return "R_PPC_DTPREL16_HA";
1298 		case 78: return "R_PPC_DTPREL32";
1299 		case 79: return "R_PPC_GOT_TLSGD16";
1300 		case 80: return "R_PPC_GOT_TLSGD16_LO";
1301 		case 81: return "R_PPC_GOT_TLSGD16_HI";
1302 		case 82: return "R_PPC_GOT_TLSGD16_HA";
1303 		case 83: return "R_PPC_GOT_TLSLD16";
1304 		case 84: return "R_PPC_GOT_TLSLD16_LO";
1305 		case 85: return "R_PPC_GOT_TLSLD16_HI";
1306 		case 86: return "R_PPC_GOT_TLSLD16_HA";
1307 		case 87: return "R_PPC_GOT_TPREL16";
1308 		case 88: return "R_PPC_GOT_TPREL16_LO";
1309 		case 89: return "R_PPC_GOT_TPREL16_HI";
1310 		case 90: return "R_PPC_GOT_TPREL16_HA";
1311 		case 101: return "R_PPC_EMB_NADDR32";
1312 		case 102: return "R_PPC_EMB_NADDR16";
1313 		case 103: return "R_PPC_EMB_NADDR16_LO";
1314 		case 104: return "R_PPC_EMB_NADDR16_HI";
1315 		case 105: return "R_PPC_EMB_NADDR16_HA";
1316 		case 106: return "R_PPC_EMB_SDAI16";
1317 		case 107: return "R_PPC_EMB_SDA2I16";
1318 		case 108: return "R_PPC_EMB_SDA2REL";
1319 		case 109: return "R_PPC_EMB_SDA21";
1320 		case 110: return "R_PPC_EMB_MRKREF";
1321 		case 111: return "R_PPC_EMB_RELSEC16";
1322 		case 112: return "R_PPC_EMB_RELST_LO";
1323 		case 113: return "R_PPC_EMB_RELST_HI";
1324 		case 114: return "R_PPC_EMB_RELST_HA";
1325 		case 115: return "R_PPC_EMB_BIT_FLD";
1326 		case 116: return "R_PPC_EMB_RELSDA";
1327 		default: return "";
1328 		}
1329 	case EM_SPARC:
1330 	case EM_SPARCV9:
1331 		switch(type) {
1332 		case 0: return "R_SPARC_NONE";
1333 		case 1: return "R_SPARC_8";
1334 		case 2: return "R_SPARC_16";
1335 		case 3: return "R_SPARC_32";
1336 		case 4: return "R_SPARC_DISP8";
1337 		case 5: return "R_SPARC_DISP16";
1338 		case 6: return "R_SPARC_DISP32";
1339 		case 7: return "R_SPARC_WDISP30";
1340 		case 8: return "R_SPARC_WDISP22";
1341 		case 9: return "R_SPARC_HI22";
1342 		case 10: return "R_SPARC_22";
1343 		case 11: return "R_SPARC_13";
1344 		case 12: return "R_SPARC_LO10";
1345 		case 13: return "R_SPARC_GOT10";
1346 		case 14: return "R_SPARC_GOT13";
1347 		case 15: return "R_SPARC_GOT22";
1348 		case 16: return "R_SPARC_PC10";
1349 		case 17: return "R_SPARC_PC22";
1350 		case 18: return "R_SPARC_WPLT30";
1351 		case 19: return "R_SPARC_COPY";
1352 		case 20: return "R_SPARC_GLOB_DAT";
1353 		case 21: return "R_SPARC_JMP_SLOT";
1354 		case 22: return "R_SPARC_RELATIVE";
1355 		case 23: return "R_SPARC_UA32";
1356 		case 24: return "R_SPARC_PLT32";
1357 		case 25: return "R_SPARC_HIPLT22";
1358 		case 26: return "R_SPARC_LOPLT10";
1359 		case 27: return "R_SPARC_PCPLT32";
1360 		case 28: return "R_SPARC_PCPLT22";
1361 		case 29: return "R_SPARC_PCPLT10";
1362 		case 30: return "R_SPARC_10";
1363 		case 31: return "R_SPARC_11";
1364 		case 32: return "R_SPARC_64";
1365 		case 33: return "R_SPARC_OLO10";
1366 		case 34: return "R_SPARC_HH22";
1367 		case 35: return "R_SPARC_HM10";
1368 		case 36: return "R_SPARC_LM22";
1369 		case 37: return "R_SPARC_PC_HH22";
1370 		case 38: return "R_SPARC_PC_HM10";
1371 		case 39: return "R_SPARC_PC_LM22";
1372 		case 40: return "R_SPARC_WDISP16";
1373 		case 41: return "R_SPARC_WDISP19";
1374 		case 42: return "R_SPARC_GLOB_JMP";
1375 		case 43: return "R_SPARC_7";
1376 		case 44: return "R_SPARC_5";
1377 		case 45: return "R_SPARC_6";
1378 		case 46: return "R_SPARC_DISP64";
1379 		case 47: return "R_SPARC_PLT64";
1380 		case 48: return "R_SPARC_HIX22";
1381 		case 49: return "R_SPARC_LOX10";
1382 		case 50: return "R_SPARC_H44";
1383 		case 51: return "R_SPARC_M44";
1384 		case 52: return "R_SPARC_L44";
1385 		case 53: return "R_SPARC_REGISTER";
1386 		case 54: return "R_SPARC_UA64";
1387 		case 55: return "R_SPARC_UA16";
1388 		case 56: return "R_SPARC_TLS_GD_HI22";
1389 		case 57: return "R_SPARC_TLS_GD_LO10";
1390 		case 58: return "R_SPARC_TLS_GD_ADD";
1391 		case 59: return "R_SPARC_TLS_GD_CALL";
1392 		case 60: return "R_SPARC_TLS_LDM_HI22";
1393 		case 61: return "R_SPARC_TLS_LDM_LO10";
1394 		case 62: return "R_SPARC_TLS_LDM_ADD";
1395 		case 63: return "R_SPARC_TLS_LDM_CALL";
1396 		case 64: return "R_SPARC_TLS_LDO_HIX22";
1397 		case 65: return "R_SPARC_TLS_LDO_LOX10";
1398 		case 66: return "R_SPARC_TLS_LDO_ADD";
1399 		case 67: return "R_SPARC_TLS_IE_HI22";
1400 		case 68: return "R_SPARC_TLS_IE_LO10";
1401 		case 69: return "R_SPARC_TLS_IE_LD";
1402 		case 70: return "R_SPARC_TLS_IE_LDX";
1403 		case 71: return "R_SPARC_TLS_IE_ADD";
1404 		case 72: return "R_SPARC_TLS_LE_HIX22";
1405 		case 73: return "R_SPARC_TLS_LE_LOX10";
1406 		case 74: return "R_SPARC_TLS_DTPMOD32";
1407 		case 75: return "R_SPARC_TLS_DTPMOD64";
1408 		case 76: return "R_SPARC_TLS_DTPOFF32";
1409 		case 77: return "R_SPARC_TLS_DTPOFF64";
1410 		case 78: return "R_SPARC_TLS_TPOFF32";
1411 		case 79: return "R_SPARC_TLS_TPOFF64";
1412 		default: return "";
1413 		}
1414 	case EM_X86_64:
1415 		switch(type) {
1416 		case 0: return "R_X86_64_NONE";
1417 		case 1: return "R_X86_64_64";
1418 		case 2: return "R_X86_64_PC32";
1419 		case 3: return "R_X86_64_GOT32";
1420 		case 4: return "R_X86_64_PLT32";
1421 		case 5: return "R_X86_64_COPY";
1422 		case 6: return "R_X86_64_GLOB_DAT";
1423 		case 7: return "R_X86_64_JMP_SLOT";
1424 		case 8: return "R_X86_64_RELATIVE";
1425 		case 9: return "R_X86_64_GOTPCREL";
1426 		case 10: return "R_X86_64_32";
1427 		case 11: return "R_X86_64_32S";
1428 		case 12: return "R_X86_64_16";
1429 		case 13: return "R_X86_64_PC16";
1430 		case 14: return "R_X86_64_8";
1431 		case 15: return "R_X86_64_PC8";
1432 		case 16: return "R_X86_64_DTPMOD64";
1433 		case 17: return "R_X86_64_DTPOFF64";
1434 		case 18: return "R_X86_64_TPOFF64";
1435 		case 19: return "R_X86_64_TLSGD";
1436 		case 20: return "R_X86_64_TLSLD";
1437 		case 21: return "R_X86_64_DTPOFF32";
1438 		case 22: return "R_X86_64_GOTTPOFF";
1439 		case 23: return "R_X86_64_TPOFF32";
1440 		default: return "";
1441 		}
1442 	default: return "";
1443 	}
1444 }
1445 
1446 static const char *
1447 note_type(unsigned int osabi, unsigned int et, unsigned int nt)
1448 {
1449 	static char s_nt[32];
1450 
1451 	if (et == ET_CORE) {
1452 		switch (nt) {
1453 		case NT_PRSTATUS:
1454 			return "NT_PRSTATUS (Process status)";
1455 		case NT_FPREGSET:
1456 			return "NT_FPREGSET (Floating point information)";
1457 		case NT_PRPSINFO:
1458 			return "NT_PRPSINFO (Process information)";
1459 		case NT_AUXV:
1460 			return "NT_AUXV (Auxiliary vector)";
1461 		case NT_PRXFPREG:
1462 			return "NT_PRXFPREG (Linux user_xfpregs structure)";
1463 		case NT_PSTATUS:
1464 			return "NT_PSTATUS (Linux process status)";
1465 		case NT_FPREGS:
1466 			return "NT_FPREGS (Linux floating point regset)";
1467 		case NT_PSINFO:
1468 			return "NT_PSINFO (Linux process information)";
1469 		case NT_LWPSTATUS:
1470 			return "NT_LWPSTATUS (Linux lwpstatus_t type)";
1471 		case NT_LWPSINFO:
1472 			return "NT_LWPSINFO (Linux lwpinfo_t type)";
1473 		default:
1474 			snprintf(s_nt, sizeof(s_nt), "<unknown: %u>", nt);
1475 			return (s_nt);
1476 		}
1477 	} else {
1478 		switch (nt) {
1479 		case NT_ABI_TAG:
1480 			switch (osabi) {
1481 			case ELFOSABI_FREEBSD:
1482 				return "NT_FREEBSD_ABI_TAG";
1483 			case ELFOSABI_NETBSD:
1484 				return "NT_NETBSD_IDENT";
1485 			case ELFOSABI_OPENBSD:
1486 				return "NT_OPENBSD_IDENT";
1487 			default:
1488 				return "NT_GNU_ABI_TAG";
1489 			}
1490 		case NT_GNU_HWCAP:
1491 			return "NT_GNU_HWCAP (Hardware capabilities)";
1492 		case NT_GNU_BUILD_ID:
1493 			return "NT_GNU_BUILD_ID (Build id set by ld(1))";
1494 		case NT_GNU_GOLD_VERSION:
1495 			return "NT_GNU_GOLD_VERSION (GNU gold version)";
1496 		default:
1497 			snprintf(s_nt, sizeof(s_nt), "<unknown: %u>", nt);
1498 			return (s_nt);
1499 		}
1500 	}
1501 }
1502 
1503 static struct {
1504 	const char *name;
1505 	int value;
1506 } l_flag[] = {
1507 	{"EXACT_MATCH", LL_EXACT_MATCH},
1508 	{"IGNORE_INT_VER", LL_IGNORE_INT_VER},
1509 	{"REQUIRE_MINOR", LL_REQUIRE_MINOR},
1510 	{"EXPORTS", LL_EXPORTS},
1511 	{"DELAY_LOAD", LL_DELAY_LOAD},
1512 	{"DELTA", LL_DELTA},
1513 	{NULL, 0}
1514 };
1515 
1516 static struct mips_option mips_exceptions_option[] = {
1517 	{OEX_PAGE0, "PAGE0"},
1518 	{OEX_SMM, "SMM"},
1519 	{OEX_PRECISEFP, "PRECISEFP"},
1520 	{OEX_DISMISS, "DISMISS"},
1521 	{0, NULL}
1522 };
1523 
1524 static struct mips_option mips_pad_option[] = {
1525 	{OPAD_PREFIX, "PREFIX"},
1526 	{OPAD_POSTFIX, "POSTFIX"},
1527 	{OPAD_SYMBOL, "SYMBOL"},
1528 	{0, NULL}
1529 };
1530 
1531 static struct mips_option mips_hwpatch_option[] = {
1532 	{OHW_R4KEOP, "R4KEOP"},
1533 	{OHW_R8KPFETCH, "R8KPFETCH"},
1534 	{OHW_R5KEOP, "R5KEOP"},
1535 	{OHW_R5KCVTL, "R5KCVTL"},
1536 	{0, NULL}
1537 };
1538 
1539 static struct mips_option mips_hwa_option[] = {
1540 	{OHWA0_R4KEOP_CHECKED, "R4KEOP_CHECKED"},
1541 	{OHWA0_R4KEOP_CLEAN, "R4KEOP_CLEAN"},
1542 	{0, NULL}
1543 };
1544 
1545 static struct mips_option mips_hwo_option[] = {
1546 	{OHWO0_FIXADE, "FIXADE"},
1547 	{0, NULL}
1548 };
1549 
1550 static const char *
1551 option_kind(uint8_t kind)
1552 {
1553 	static char s_kind[32];
1554 
1555 	switch (kind) {
1556 	case ODK_NULL: return "NULL";
1557 	case ODK_REGINFO: return "REGINFO";
1558 	case ODK_EXCEPTIONS: return "EXCEPTIONS";
1559 	case ODK_PAD: return "PAD";
1560 	case ODK_HWPATCH: return "HWPATCH";
1561 	case ODK_FILL: return "FILL";
1562 	case ODK_TAGS: return "TAGS";
1563 	case ODK_HWAND: return "HWAND";
1564 	case ODK_HWOR: return "HWOR";
1565 	case ODK_GP_GROUP: return "GP_GROUP";
1566 	case ODK_IDENT: return "IDENT";
1567 	default:
1568 		snprintf(s_kind, sizeof(s_kind), "<unknown: %u>", kind);
1569 		return (s_kind);
1570 	}
1571 }
1572 
1573 static const char *
1574 top_tag(unsigned int tag)
1575 {
1576 	static char s_top_tag[32];
1577 
1578 	switch (tag) {
1579 	case 1: return "File Attributes";
1580 	case 2: return "Section Attributes";
1581 	case 3: return "Symbol Attributes";
1582 	default:
1583 		snprintf(s_top_tag, sizeof(s_top_tag), "Unknown tag: %u", tag);
1584 		return (s_top_tag);
1585 	}
1586 }
1587 
1588 static const char *
1589 aeabi_cpu_arch(uint64_t arch)
1590 {
1591 	static char s_cpu_arch[32];
1592 
1593 	switch (arch) {
1594 	case 0: return "Pre-V4";
1595 	case 1: return "ARM v4";
1596 	case 2: return "ARM v4T";
1597 	case 3: return "ARM v5T";
1598 	case 4: return "ARM v5TE";
1599 	case 5: return "ARM v5TEJ";
1600 	case 6: return "ARM v6";
1601 	case 7: return "ARM v6KZ";
1602 	case 8: return "ARM v6T2";
1603 	case 9: return "ARM v6K";
1604 	case 10: return "ARM v7";
1605 	case 11: return "ARM v6-M";
1606 	case 12: return "ARM v6S-M";
1607 	case 13: return "ARM v7E-M";
1608 	default:
1609 		snprintf(s_cpu_arch, sizeof(s_cpu_arch),
1610 		    "Unknown (%ju)", (uintmax_t) arch);
1611 		return (s_cpu_arch);
1612 	}
1613 }
1614 
1615 static const char *
1616 aeabi_cpu_arch_profile(uint64_t pf)
1617 {
1618 	static char s_arch_profile[32];
1619 
1620 	switch (pf) {
1621 	case 0:
1622 		return "Not applicable";
1623 	case 0x41:		/* 'A' */
1624 		return "Application Profile";
1625 	case 0x52:		/* 'R' */
1626 		return "Real-Time Profile";
1627 	case 0x4D:		/* 'M' */
1628 		return "Microcontroller Profile";
1629 	case 0x53:		/* 'S' */
1630 		return "Application or Real-Time Profile";
1631 	default:
1632 		snprintf(s_arch_profile, sizeof(s_arch_profile),
1633 		    "Unknown (%ju)\n", (uintmax_t) pf);
1634 		return (s_arch_profile);
1635 	}
1636 }
1637 
1638 static const char *
1639 aeabi_arm_isa(uint64_t ai)
1640 {
1641 	static char s_ai[32];
1642 
1643 	switch (ai) {
1644 	case 0: return "No";
1645 	case 1: return "Yes";
1646 	default:
1647 		snprintf(s_ai, sizeof(s_ai), "Unknown (%ju)\n",
1648 		    (uintmax_t) ai);
1649 		return (s_ai);
1650 	}
1651 }
1652 
1653 static const char *
1654 aeabi_thumb_isa(uint64_t ti)
1655 {
1656 	static char s_ti[32];
1657 
1658 	switch (ti) {
1659 	case 0: return "No";
1660 	case 1: return "16-bit Thumb";
1661 	case 2: return "32-bit Thumb";
1662 	default:
1663 		snprintf(s_ti, sizeof(s_ti), "Unknown (%ju)\n",
1664 		    (uintmax_t) ti);
1665 		return (s_ti);
1666 	}
1667 }
1668 
1669 static const char *
1670 aeabi_fp_arch(uint64_t fp)
1671 {
1672 	static char s_fp_arch[32];
1673 
1674 	switch (fp) {
1675 	case 0: return "No";
1676 	case 1: return "VFPv1";
1677 	case 2: return "VFPv2";
1678 	case 3: return "VFPv3";
1679 	case 4: return "VFPv3-D16";
1680 	case 5: return "VFPv4";
1681 	case 6: return "VFPv4-D16";
1682 	default:
1683 		snprintf(s_fp_arch, sizeof(s_fp_arch), "Unknown (%ju)",
1684 		    (uintmax_t) fp);
1685 		return (s_fp_arch);
1686 	}
1687 }
1688 
1689 static const char *
1690 aeabi_wmmx_arch(uint64_t wmmx)
1691 {
1692 	static char s_wmmx[32];
1693 
1694 	switch (wmmx) {
1695 	case 0: return "No";
1696 	case 1: return "WMMXv1";
1697 	case 2: return "WMMXv2";
1698 	default:
1699 		snprintf(s_wmmx, sizeof(s_wmmx), "Unknown (%ju)",
1700 		    (uintmax_t) wmmx);
1701 		return (s_wmmx);
1702 	}
1703 }
1704 
1705 static const char *
1706 aeabi_adv_simd_arch(uint64_t simd)
1707 {
1708 	static char s_simd[32];
1709 
1710 	switch (simd) {
1711 	case 0: return "No";
1712 	case 1: return "NEONv1";
1713 	case 2: return "NEONv2";
1714 	default:
1715 		snprintf(s_simd, sizeof(s_simd), "Unknown (%ju)",
1716 		    (uintmax_t) simd);
1717 		return (s_simd);
1718 	}
1719 }
1720 
1721 static const char *
1722 aeabi_pcs_config(uint64_t pcs)
1723 {
1724 	static char s_pcs[32];
1725 
1726 	switch (pcs) {
1727 	case 0: return "None";
1728 	case 1: return "Bare platform";
1729 	case 2: return "Linux";
1730 	case 3: return "Linux DSO";
1731 	case 4: return "Palm OS 2004";
1732 	case 5: return "Palm OS (future)";
1733 	case 6: return "Symbian OS 2004";
1734 	case 7: return "Symbian OS (future)";
1735 	default:
1736 		snprintf(s_pcs, sizeof(s_pcs), "Unknown (%ju)",
1737 		    (uintmax_t) pcs);
1738 		return (s_pcs);
1739 	}
1740 }
1741 
1742 static const char *
1743 aeabi_pcs_r9(uint64_t r9)
1744 {
1745 	static char s_r9[32];
1746 
1747 	switch (r9) {
1748 	case 0: return "V6";
1749 	case 1: return "SB";
1750 	case 2: return "TLS pointer";
1751 	case 3: return "Unused";
1752 	default:
1753 		snprintf(s_r9, sizeof(s_r9), "Unknown (%ju)", (uintmax_t) r9);
1754 		return (s_r9);
1755 	}
1756 }
1757 
1758 static const char *
1759 aeabi_pcs_rw(uint64_t rw)
1760 {
1761 	static char s_rw[32];
1762 
1763 	switch (rw) {
1764 	case 0: return "Absolute";
1765 	case 1: return "PC-relative";
1766 	case 2: return "SB-relative";
1767 	case 3: return "None";
1768 	default:
1769 		snprintf(s_rw, sizeof(s_rw), "Unknown (%ju)", (uintmax_t) rw);
1770 		return (s_rw);
1771 	}
1772 }
1773 
1774 static const char *
1775 aeabi_pcs_ro(uint64_t ro)
1776 {
1777 	static char s_ro[32];
1778 
1779 	switch (ro) {
1780 	case 0: return "Absolute";
1781 	case 1: return "PC-relative";
1782 	case 2: return "None";
1783 	default:
1784 		snprintf(s_ro, sizeof(s_ro), "Unknown (%ju)", (uintmax_t) ro);
1785 		return (s_ro);
1786 	}
1787 }
1788 
1789 static const char *
1790 aeabi_pcs_got(uint64_t got)
1791 {
1792 	static char s_got[32];
1793 
1794 	switch (got) {
1795 	case 0: return "None";
1796 	case 1: return "direct";
1797 	case 2: return "indirect via GOT";
1798 	default:
1799 		snprintf(s_got, sizeof(s_got), "Unknown (%ju)",
1800 		    (uintmax_t) got);
1801 		return (s_got);
1802 	}
1803 }
1804 
1805 static const char *
1806 aeabi_pcs_wchar_t(uint64_t wt)
1807 {
1808 	static char s_wt[32];
1809 
1810 	switch (wt) {
1811 	case 0: return "None";
1812 	case 2: return "wchar_t size 2";
1813 	case 4: return "wchar_t size 4";
1814 	default:
1815 		snprintf(s_wt, sizeof(s_wt), "Unknown (%ju)", (uintmax_t) wt);
1816 		return (s_wt);
1817 	}
1818 }
1819 
1820 static const char *
1821 aeabi_enum_size(uint64_t es)
1822 {
1823 	static char s_es[32];
1824 
1825 	switch (es) {
1826 	case 0: return "None";
1827 	case 1: return "smallest";
1828 	case 2: return "32-bit";
1829 	case 3: return "visible 32-bit";
1830 	default:
1831 		snprintf(s_es, sizeof(s_es), "Unknown (%ju)", (uintmax_t) es);
1832 		return (s_es);
1833 	}
1834 }
1835 
1836 static const char *
1837 aeabi_align_needed(uint64_t an)
1838 {
1839 	static char s_align_n[64];
1840 
1841 	switch (an) {
1842 	case 0: return "No";
1843 	case 1: return "8-byte align";
1844 	case 2: return "4-byte align";
1845 	case 3: return "Reserved";
1846 	default:
1847 		if (an >= 4 && an <= 12)
1848 			snprintf(s_align_n, sizeof(s_align_n), "8-byte align"
1849 			    " and up to 2^%ju-byte extended align",
1850 			    (uintmax_t) an);
1851 		else
1852 			snprintf(s_align_n, sizeof(s_align_n), "Unknown (%ju)",
1853 			    (uintmax_t) an);
1854 		return (s_align_n);
1855 	}
1856 }
1857 
1858 static const char *
1859 aeabi_align_preserved(uint64_t ap)
1860 {
1861 	static char s_align_p[128];
1862 
1863 	switch (ap) {
1864 	case 0: return "No";
1865 	case 1: return "8-byte align";
1866 	case 2: return "8-byte align and SP % 8 == 0";
1867 	case 3: return "Reserved";
1868 	default:
1869 		if (ap >= 4 && ap <= 12)
1870 			snprintf(s_align_p, sizeof(s_align_p), "8-byte align"
1871 			    " and SP %% 8 == 0 and up to 2^%ju-byte extended"
1872 			    " align", (uintmax_t) ap);
1873 		else
1874 			snprintf(s_align_p, sizeof(s_align_p), "Unknown (%ju)",
1875 			    (uintmax_t) ap);
1876 		return (s_align_p);
1877 	}
1878 }
1879 
1880 static const char *
1881 aeabi_fp_rounding(uint64_t fr)
1882 {
1883 	static char s_fp_r[32];
1884 
1885 	switch (fr) {
1886 	case 0: return "Unused";
1887 	case 1: return "Needed";
1888 	default:
1889 		snprintf(s_fp_r, sizeof(s_fp_r), "Unknown (%ju)",
1890 		    (uintmax_t) fr);
1891 		return (s_fp_r);
1892 	}
1893 }
1894 
1895 static const char *
1896 aeabi_fp_denormal(uint64_t fd)
1897 {
1898 	static char s_fp_d[32];
1899 
1900 	switch (fd) {
1901 	case 0: return "Unused";
1902 	case 1: return "Needed";
1903 	case 2: return "Sign Only";
1904 	default:
1905 		snprintf(s_fp_d, sizeof(s_fp_d), "Unknown (%ju)",
1906 		    (uintmax_t) fd);
1907 		return (s_fp_d);
1908 	}
1909 }
1910 
1911 static const char *
1912 aeabi_fp_exceptions(uint64_t fe)
1913 {
1914 	static char s_fp_e[32];
1915 
1916 	switch (fe) {
1917 	case 0: return "Unused";
1918 	case 1: return "Needed";
1919 	default:
1920 		snprintf(s_fp_e, sizeof(s_fp_e), "Unknown (%ju)",
1921 		    (uintmax_t) fe);
1922 		return (s_fp_e);
1923 	}
1924 }
1925 
1926 static const char *
1927 aeabi_fp_user_exceptions(uint64_t fu)
1928 {
1929 	static char s_fp_u[32];
1930 
1931 	switch (fu) {
1932 	case 0: return "Unused";
1933 	case 1: return "Needed";
1934 	default:
1935 		snprintf(s_fp_u, sizeof(s_fp_u), "Unknown (%ju)",
1936 		    (uintmax_t) fu);
1937 		return (s_fp_u);
1938 	}
1939 }
1940 
1941 static const char *
1942 aeabi_fp_number_model(uint64_t fn)
1943 {
1944 	static char s_fp_n[32];
1945 
1946 	switch (fn) {
1947 	case 0: return "Unused";
1948 	case 1: return "IEEE 754 normal";
1949 	case 2: return "RTABI";
1950 	case 3: return "IEEE 754";
1951 	default:
1952 		snprintf(s_fp_n, sizeof(s_fp_n), "Unknown (%ju)",
1953 		    (uintmax_t) fn);
1954 		return (s_fp_n);
1955 	}
1956 }
1957 
1958 static const char *
1959 aeabi_fp_16bit_format(uint64_t fp16)
1960 {
1961 	static char s_fp_16[64];
1962 
1963 	switch (fp16) {
1964 	case 0: return "None";
1965 	case 1: return "IEEE 754";
1966 	case 2: return "VFPv3/Advanced SIMD (alternative format)";
1967 	default:
1968 		snprintf(s_fp_16, sizeof(s_fp_16), "Unknown (%ju)",
1969 		    (uintmax_t) fp16);
1970 		return (s_fp_16);
1971 	}
1972 }
1973 
1974 static const char *
1975 aeabi_mpext(uint64_t mp)
1976 {
1977 	static char s_mp[32];
1978 
1979 	switch (mp) {
1980 	case 0: return "Not allowed";
1981 	case 1: return "Allowed";
1982 	default:
1983 		snprintf(s_mp, sizeof(s_mp), "Unknown (%ju)",
1984 		    (uintmax_t) mp);
1985 		return (s_mp);
1986 	}
1987 }
1988 
1989 static const char *
1990 aeabi_div(uint64_t du)
1991 {
1992 	static char s_du[32];
1993 
1994 	switch (du) {
1995 	case 0: return "Yes (V7-R/V7-M)";
1996 	case 1: return "No";
1997 	case 2: return "Yes (V7-A)";
1998 	default:
1999 		snprintf(s_du, sizeof(s_du), "Unknown (%ju)",
2000 		    (uintmax_t) du);
2001 		return (s_du);
2002 	}
2003 }
2004 
2005 static const char *
2006 aeabi_t2ee(uint64_t t2ee)
2007 {
2008 	static char s_t2ee[32];
2009 
2010 	switch (t2ee) {
2011 	case 0: return "Not allowed";
2012 	case 1: return "Allowed";
2013 	default:
2014 		snprintf(s_t2ee, sizeof(s_t2ee), "Unknown(%ju)",
2015 		    (uintmax_t) t2ee);
2016 		return (s_t2ee);
2017 	}
2018 
2019 }
2020 
2021 static const char *
2022 aeabi_hardfp(uint64_t hfp)
2023 {
2024 	static char s_hfp[32];
2025 
2026 	switch (hfp) {
2027 	case 0: return "Tag_FP_arch";
2028 	case 1: return "only SP";
2029 	case 2: return "only DP";
2030 	case 3: return "both SP and DP";
2031 	default:
2032 		snprintf(s_hfp, sizeof(s_hfp), "Unknown (%ju)",
2033 		    (uintmax_t) hfp);
2034 		return (s_hfp);
2035 	}
2036 }
2037 
2038 static const char *
2039 aeabi_vfp_args(uint64_t va)
2040 {
2041 	static char s_va[32];
2042 
2043 	switch (va) {
2044 	case 0: return "AAPCS (base variant)";
2045 	case 1: return "AAPCS (VFP variant)";
2046 	case 2: return "toolchain-specific";
2047 	default:
2048 		snprintf(s_va, sizeof(s_va), "Unknown (%ju)", (uintmax_t) va);
2049 		return (s_va);
2050 	}
2051 }
2052 
2053 static const char *
2054 aeabi_wmmx_args(uint64_t wa)
2055 {
2056 	static char s_wa[32];
2057 
2058 	switch (wa) {
2059 	case 0: return "AAPCS (base variant)";
2060 	case 1: return "Intel WMMX";
2061 	case 2: return "toolchain-specific";
2062 	default:
2063 		snprintf(s_wa, sizeof(s_wa), "Unknown(%ju)", (uintmax_t) wa);
2064 		return (s_wa);
2065 	}
2066 }
2067 
2068 static const char *
2069 aeabi_unaligned_access(uint64_t ua)
2070 {
2071 	static char s_ua[32];
2072 
2073 	switch (ua) {
2074 	case 0: return "Not allowed";
2075 	case 1: return "Allowed";
2076 	default:
2077 		snprintf(s_ua, sizeof(s_ua), "Unknown(%ju)", (uintmax_t) ua);
2078 		return (s_ua);
2079 	}
2080 }
2081 
2082 static const char *
2083 aeabi_fp_hpext(uint64_t fh)
2084 {
2085 	static char s_fh[32];
2086 
2087 	switch (fh) {
2088 	case 0: return "Not allowed";
2089 	case 1: return "Allowed";
2090 	default:
2091 		snprintf(s_fh, sizeof(s_fh), "Unknown(%ju)", (uintmax_t) fh);
2092 		return (s_fh);
2093 	}
2094 }
2095 
2096 static const char *
2097 aeabi_optm_goal(uint64_t og)
2098 {
2099 	static char s_og[32];
2100 
2101 	switch (og) {
2102 	case 0: return "None";
2103 	case 1: return "Speed";
2104 	case 2: return "Speed aggressive";
2105 	case 3: return "Space";
2106 	case 4: return "Space aggressive";
2107 	case 5: return "Debugging";
2108 	case 6: return "Best Debugging";
2109 	default:
2110 		snprintf(s_og, sizeof(s_og), "Unknown(%ju)", (uintmax_t) og);
2111 		return (s_og);
2112 	}
2113 }
2114 
2115 static const char *
2116 aeabi_fp_optm_goal(uint64_t fog)
2117 {
2118 	static char s_fog[32];
2119 
2120 	switch (fog) {
2121 	case 0: return "None";
2122 	case 1: return "Speed";
2123 	case 2: return "Speed aggressive";
2124 	case 3: return "Space";
2125 	case 4: return "Space aggressive";
2126 	case 5: return "Accurary";
2127 	case 6: return "Best Accurary";
2128 	default:
2129 		snprintf(s_fog, sizeof(s_fog), "Unknown(%ju)",
2130 		    (uintmax_t) fog);
2131 		return (s_fog);
2132 	}
2133 }
2134 
2135 static const char *
2136 aeabi_virtual(uint64_t vt)
2137 {
2138 	static char s_virtual[64];
2139 
2140 	switch (vt) {
2141 	case 0: return "No";
2142 	case 1: return "TrustZone";
2143 	case 2: return "Virtualization extension";
2144 	case 3: return "TrustZone and virtualization extension";
2145 	default:
2146 		snprintf(s_virtual, sizeof(s_virtual), "Unknown(%ju)",
2147 		    (uintmax_t) vt);
2148 		return (s_virtual);
2149 	}
2150 }
2151 
2152 static struct {
2153 	uint64_t tag;
2154 	const char *s_tag;
2155 	const char *(*get_desc)(uint64_t val);
2156 } aeabi_tags[] = {
2157 	{4, "Tag_CPU_raw_name", NULL},
2158 	{5, "Tag_CPU_name", NULL},
2159 	{6, "Tag_CPU_arch", aeabi_cpu_arch},
2160 	{7, "Tag_CPU_arch_profile", aeabi_cpu_arch_profile},
2161 	{8, "Tag_ARM_ISA_use", aeabi_arm_isa},
2162 	{9, "Tag_THUMB_ISA_use", aeabi_thumb_isa},
2163 	{10, "Tag_FP_arch", aeabi_fp_arch},
2164 	{11, "Tag_WMMX_arch", aeabi_wmmx_arch},
2165 	{12, "Tag_Advanced_SIMD_arch", aeabi_adv_simd_arch},
2166 	{13, "Tag_PCS_config", aeabi_pcs_config},
2167 	{14, "Tag_ABI_PCS_R9_use", aeabi_pcs_r9},
2168 	{15, "Tag_ABI_PCS_RW_data", aeabi_pcs_rw},
2169 	{16, "Tag_ABI_PCS_RO_data", aeabi_pcs_ro},
2170 	{17, "Tag_ABI_PCS_GOT_use", aeabi_pcs_got},
2171 	{18, "Tag_ABI_PCS_wchar_t", aeabi_pcs_wchar_t},
2172 	{19, "Tag_ABI_FP_rounding", aeabi_fp_rounding},
2173 	{20, "Tag_ABI_FP_denormal", aeabi_fp_denormal},
2174 	{21, "Tag_ABI_FP_exceptions", aeabi_fp_exceptions},
2175 	{22, "Tag_ABI_FP_user_exceptions", aeabi_fp_user_exceptions},
2176 	{23, "Tag_ABI_FP_number_model", aeabi_fp_number_model},
2177 	{24, "Tag_ABI_align_needed", aeabi_align_needed},
2178 	{25, "Tag_ABI_align_preserved", aeabi_align_preserved},
2179 	{26, "Tag_ABI_enum_size", aeabi_enum_size},
2180 	{27, "Tag_ABI_HardFP_use", aeabi_hardfp},
2181 	{28, "Tag_ABI_VFP_args", aeabi_vfp_args},
2182 	{29, "Tag_ABI_WMMX_args", aeabi_wmmx_args},
2183 	{30, "Tag_ABI_optimization_goals", aeabi_optm_goal},
2184 	{31, "Tag_ABI_FP_optimization_goals", aeabi_fp_optm_goal},
2185 	{32, "Tag_compatibility", NULL},
2186 	{34, "Tag_CPU_unaligned_access", aeabi_unaligned_access},
2187 	{36, "Tag_FP_HP_extension", aeabi_fp_hpext},
2188 	{38, "Tag_ABI_FP_16bit_format", aeabi_fp_16bit_format},
2189 	{42, "Tag_MPextension_use", aeabi_mpext},
2190 	{44, "Tag_DIV_use", aeabi_div},
2191 	{64, "Tag_nodefaults", NULL},
2192 	{65, "Tag_also_compatible_with", NULL},
2193 	{66, "Tag_T2EE_use", aeabi_t2ee},
2194 	{67, "Tag_conformance", NULL},
2195 	{68, "Tag_Virtualization_use", aeabi_virtual},
2196 	{70, "Tag_MPextension_use", aeabi_mpext},
2197 };
2198 
2199 static const char *
2200 mips_abi_fp(uint64_t fp)
2201 {
2202 	static char s_mips_abi_fp[64];
2203 
2204 	switch (fp) {
2205 	case 0: return "N/A";
2206 	case 1: return "Hard float (double precision)";
2207 	case 2: return "Hard float (single precision)";
2208 	case 3: return "Soft float";
2209 	case 4: return "64-bit float (-mips32r2 -mfp64)";
2210 	default:
2211 		snprintf(s_mips_abi_fp, sizeof(s_mips_abi_fp), "Unknown(%ju)",
2212 		    (uintmax_t) fp);
2213 		return (s_mips_abi_fp);
2214 	}
2215 }
2216 
2217 static const char *
2218 ppc_abi_fp(uint64_t fp)
2219 {
2220 	static char s_ppc_abi_fp[64];
2221 
2222 	switch (fp) {
2223 	case 0: return "N/A";
2224 	case 1: return "Hard float (double precision)";
2225 	case 2: return "Soft float";
2226 	case 3: return "Hard float (single precision)";
2227 	default:
2228 		snprintf(s_ppc_abi_fp, sizeof(s_ppc_abi_fp), "Unknown(%ju)",
2229 		    (uintmax_t) fp);
2230 		return (s_ppc_abi_fp);
2231 	}
2232 }
2233 
2234 static const char *
2235 ppc_abi_vector(uint64_t vec)
2236 {
2237 	static char s_vec[64];
2238 
2239 	switch (vec) {
2240 	case 0: return "N/A";
2241 	case 1: return "Generic purpose registers";
2242 	case 2: return "AltiVec registers";
2243 	case 3: return "SPE registers";
2244 	default:
2245 		snprintf(s_vec, sizeof(s_vec), "Unknown(%ju)", (uintmax_t) vec);
2246 		return (s_vec);
2247 	}
2248 }
2249 
2250 static void
2251 dump_ehdr(struct readelf *re)
2252 {
2253 	size_t		 shnum, shstrndx;
2254 	int		 i;
2255 
2256 	printf("ELF Header:\n");
2257 
2258 	/* e_ident[]. */
2259 	printf("  Magic:   ");
2260 	for (i = 0; i < EI_NIDENT; i++)
2261 		printf("%.2x ", re->ehdr.e_ident[i]);
2262 	putchar('\n');
2263 
2264 	/* EI_CLASS. */
2265 	printf("%-37s%s\n", "  Class:", elf_class(re->ehdr.e_ident[EI_CLASS]));
2266 
2267 	/* EI_DATA. */
2268 	printf("%-37s%s\n", "  Data:", elf_endian(re->ehdr.e_ident[EI_DATA]));
2269 
2270 	/* EI_VERSION. */
2271 	printf("%-37s%d %s\n", "  Version:", re->ehdr.e_ident[EI_VERSION],
2272 	    elf_ver(re->ehdr.e_ident[EI_VERSION]));
2273 
2274 	/* EI_OSABI. */
2275 	printf("%-37s%s\n", "  OS/ABI:", elf_osabi(re->ehdr.e_ident[EI_OSABI]));
2276 
2277 	/* EI_ABIVERSION. */
2278 	printf("%-37s%d\n", "  ABI Version:", re->ehdr.e_ident[EI_ABIVERSION]);
2279 
2280 	/* e_type. */
2281 	printf("%-37s%s\n", "  Type:", elf_type(re->ehdr.e_type));
2282 
2283 	/* e_machine. */
2284 	printf("%-37s%s\n", "  Machine:", elf_machine(re->ehdr.e_machine));
2285 
2286 	/* e_version. */
2287 	printf("%-37s%#x\n", "  Version:", re->ehdr.e_version);
2288 
2289 	/* e_entry. */
2290 	printf("%-37s%#jx\n", "  Entry point address:",
2291 	    (uintmax_t)re->ehdr.e_entry);
2292 
2293 	/* e_phoff. */
2294 	printf("%-37s%ju (bytes into file)\n", "  Start of program headers:",
2295 	    (uintmax_t)re->ehdr.e_phoff);
2296 
2297 	/* e_shoff. */
2298 	printf("%-37s%ju (bytes into file)\n", "  Start of section headers:",
2299 	    (uintmax_t)re->ehdr.e_shoff);
2300 
2301 	/* e_flags. */
2302 	printf("%-37s%#x", "  Flags:", re->ehdr.e_flags);
2303 	dump_eflags(re, re->ehdr.e_flags);
2304 	putchar('\n');
2305 
2306 	/* e_ehsize. */
2307 	printf("%-37s%u (bytes)\n", "  Size of this header:",
2308 	    re->ehdr.e_ehsize);
2309 
2310 	/* e_phentsize. */
2311 	printf("%-37s%u (bytes)\n", "  Size of program headers:",
2312 	    re->ehdr.e_phentsize);
2313 
2314 	/* e_phnum. */
2315 	printf("%-37s%u\n", "  Number of program headers:", re->ehdr.e_phnum);
2316 
2317 	/* e_shentsize. */
2318 	printf("%-37s%u (bytes)\n", "  Size of section headers:",
2319 	    re->ehdr.e_shentsize);
2320 
2321 	/* e_shnum. */
2322 	printf("%-37s%u", "  Number of section headers:", re->ehdr.e_shnum);
2323 	if (re->ehdr.e_shnum == SHN_UNDEF) {
2324 		/* Extended section numbering is in use. */
2325 		if (elf_getshnum(re->elf, &shnum))
2326 			printf(" (%ju)", (uintmax_t)shnum);
2327 	}
2328 	putchar('\n');
2329 
2330 	/* e_shstrndx. */
2331 	printf("%-37s%u", "  Section header string table index:",
2332 	    re->ehdr.e_shstrndx);
2333 	if (re->ehdr.e_shstrndx == SHN_XINDEX) {
2334 		/* Extended section numbering is in use. */
2335 		if (elf_getshstrndx(re->elf, &shstrndx))
2336 			printf(" (%ju)", (uintmax_t)shstrndx);
2337 	}
2338 	putchar('\n');
2339 }
2340 
2341 static void
2342 dump_eflags(struct readelf *re, uint64_t e_flags)
2343 {
2344 	struct eflags_desc *edesc;
2345 	int arm_eabi;
2346 
2347 	edesc = NULL;
2348 	switch (re->ehdr.e_machine) {
2349 	case EM_ARM:
2350 		arm_eabi = (e_flags & EF_ARM_EABIMASK) >> 24;
2351 		if (arm_eabi == 0)
2352 			printf(", GNU EABI");
2353 		else if (arm_eabi <= 5)
2354 			printf(", Version%d EABI", arm_eabi);
2355 		edesc = arm_eflags_desc;
2356 		break;
2357 	case EM_MIPS:
2358 	case EM_MIPS_RS3_LE:
2359 		switch ((e_flags & EF_MIPS_ARCH) >> 28) {
2360 		case 0:	printf(", mips1"); break;
2361 		case 1: printf(", mips2"); break;
2362 		case 2: printf(", mips3"); break;
2363 		case 3: printf(", mips4"); break;
2364 		case 4: printf(", mips5"); break;
2365 		case 5: printf(", mips32"); break;
2366 		case 6: printf(", mips64"); break;
2367 		case 7: printf(", mips32r2"); break;
2368 		case 8: printf(", mips64r2"); break;
2369 		default: break;
2370 		}
2371 		switch ((e_flags & 0x00FF0000) >> 16) {
2372 		case 0x81: printf(", 3900"); break;
2373 		case 0x82: printf(", 4010"); break;
2374 		case 0x83: printf(", 4100"); break;
2375 		case 0x85: printf(", 4650"); break;
2376 		case 0x87: printf(", 4120"); break;
2377 		case 0x88: printf(", 4111"); break;
2378 		case 0x8a: printf(", sb1"); break;
2379 		case 0x8b: printf(", octeon"); break;
2380 		case 0x8c: printf(", xlr"); break;
2381 		case 0x91: printf(", 5400"); break;
2382 		case 0x98: printf(", 5500"); break;
2383 		case 0x99: printf(", 9000"); break;
2384 		case 0xa0: printf(", loongson-2e"); break;
2385 		case 0xa1: printf(", loongson-2f"); break;
2386 		default: break;
2387 		}
2388 		switch ((e_flags & 0x0000F000) >> 12) {
2389 		case 1: printf(", o32"); break;
2390 		case 2: printf(", o64"); break;
2391 		case 3: printf(", eabi32"); break;
2392 		case 4: printf(", eabi64"); break;
2393 		default: break;
2394 		}
2395 		edesc = mips_eflags_desc;
2396 		break;
2397 	case EM_PPC:
2398 	case EM_PPC64:
2399 		edesc = powerpc_eflags_desc;
2400 		break;
2401 	case EM_SPARC:
2402 	case EM_SPARC32PLUS:
2403 	case EM_SPARCV9:
2404 		switch ((e_flags & EF_SPARCV9_MM)) {
2405 		case EF_SPARCV9_TSO: printf(", tso"); break;
2406 		case EF_SPARCV9_PSO: printf(", pso"); break;
2407 		case EF_SPARCV9_MM: printf(", rmo"); break;
2408 		default: break;
2409 		}
2410 		edesc = sparc_eflags_desc;
2411 		break;
2412 	default:
2413 		break;
2414 	}
2415 
2416 	if (edesc != NULL) {
2417 		while (edesc->desc != NULL) {
2418 			if (e_flags & edesc->flag)
2419 				printf(", %s", edesc->desc);
2420 			edesc++;
2421 		}
2422 	}
2423 }
2424 
2425 static void
2426 dump_phdr(struct readelf *re)
2427 {
2428 	const char	*rawfile;
2429 	GElf_Phdr	 phdr;
2430 	size_t		 phnum;
2431 	int		 i, j;
2432 
2433 #define	PH_HDR	"Type", "Offset", "VirtAddr", "PhysAddr", "FileSiz",	\
2434 		"MemSiz", "Flg", "Align"
2435 #define	PH_CT	phdr_type(phdr.p_type), (uintmax_t)phdr.p_offset,	\
2436 		(uintmax_t)phdr.p_vaddr, (uintmax_t)phdr.p_paddr,	\
2437 		(uintmax_t)phdr.p_filesz, (uintmax_t)phdr.p_memsz,	\
2438 		phdr.p_flags & PF_R ? 'R' : ' ',			\
2439 		phdr.p_flags & PF_W ? 'W' : ' ',			\
2440 		phdr.p_flags & PF_X ? 'E' : ' ',			\
2441 		(uintmax_t)phdr.p_align
2442 
2443 	if (elf_getphnum(re->elf, &phnum) == 0) {
2444 		warnx("elf_getphnum failed: %s", elf_errmsg(-1));
2445 		return;
2446 	}
2447 	if (phnum == 0) {
2448 		printf("\nThere are no program headers in this file.\n");
2449 		return;
2450 	}
2451 
2452 	printf("\nElf file type is %s", elf_type(re->ehdr.e_type));
2453 	printf("\nEntry point 0x%jx\n", (uintmax_t)re->ehdr.e_entry);
2454 	printf("There are %ju program headers, starting at offset %ju\n",
2455 	    (uintmax_t)phnum, (uintmax_t)re->ehdr.e_phoff);
2456 
2457 	/* Dump program headers. */
2458 	printf("\nProgram Headers:\n");
2459 	if (re->ec == ELFCLASS32)
2460 		printf("  %-15s%-9s%-11s%-11s%-8s%-8s%-4s%s\n", PH_HDR);
2461 	else if (re->options & RE_WW)
2462 		printf("  %-15s%-9s%-19s%-19s%-9s%-9s%-4s%s\n", PH_HDR);
2463 	else
2464 		printf("  %-15s%-19s%-19s%s\n                 %-19s%-20s"
2465 		    "%-7s%s\n", PH_HDR);
2466 	for (i = 0; (size_t) i < phnum; i++) {
2467 		if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
2468 			warnx("gelf_getphdr failed: %s", elf_errmsg(-1));
2469 			continue;
2470 		}
2471 		/* TODO: Add arch-specific segment type dump. */
2472 		if (re->ec == ELFCLASS32)
2473 			printf("  %-14.14s 0x%6.6jx 0x%8.8jx 0x%8.8jx "
2474 			    "0x%5.5jx 0x%5.5jx %c%c%c %#jx\n", PH_CT);
2475 		else if (re->options & RE_WW)
2476 			printf("  %-14.14s 0x%6.6jx 0x%16.16jx 0x%16.16jx "
2477 			    "0x%6.6jx 0x%6.6jx %c%c%c %#jx\n", PH_CT);
2478 		else
2479 			printf("  %-14.14s 0x%16.16jx 0x%16.16jx 0x%16.16jx\n"
2480 			    "                 0x%16.16jx 0x%16.16jx  %c%c%c"
2481 			    "    %#jx\n", PH_CT);
2482 		if (phdr.p_type == PT_INTERP) {
2483 			if ((rawfile = elf_rawfile(re->elf, NULL)) == NULL) {
2484 				warnx("elf_rawfile failed: %s", elf_errmsg(-1));
2485 				continue;
2486 			}
2487 			printf("      [Requesting program interpreter: %s]\n",
2488 				rawfile + phdr.p_offset);
2489 		}
2490 	}
2491 
2492 	/* Dump section to segment mapping. */
2493 	if (re->shnum == 0)
2494 		return;
2495 	printf("\n Section to Segment mapping:\n");
2496 	printf("  Segment Sections...\n");
2497 	for (i = 0; (size_t)i < phnum; i++) {
2498 		if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
2499 			warnx("gelf_getphdr failed: %s", elf_errmsg(-1));
2500 			continue;
2501 		}
2502 		printf("   %2.2d     ", i);
2503 		/* skip NULL section. */
2504 		for (j = 1; (size_t)j < re->shnum; j++)
2505 			if (re->sl[j].off >= phdr.p_offset &&
2506 			    re->sl[j].off + re->sl[j].sz <=
2507 			    phdr.p_offset + phdr.p_memsz)
2508 				printf("%s ", re->sl[j].name);
2509 		printf("\n");
2510 	}
2511 #undef	PH_HDR
2512 #undef	PH_CT
2513 }
2514 
2515 static char *
2516 section_flags(struct readelf *re, struct section *s)
2517 {
2518 #define BUF_SZ 256
2519 	static char	buf[BUF_SZ];
2520 	int		i, p, nb;
2521 
2522 	p = 0;
2523 	nb = re->ec == ELFCLASS32 ? 8 : 16;
2524 	if (re->options & RE_T) {
2525 		snprintf(buf, BUF_SZ, "[%*.*jx]: ", nb, nb,
2526 		    (uintmax_t)s->flags);
2527 		p += nb + 4;
2528 	}
2529 	for (i = 0; section_flag[i].ln != NULL; i++) {
2530 		if ((s->flags & section_flag[i].value) == 0)
2531 			continue;
2532 		if (re->options & RE_T) {
2533 			snprintf(&buf[p], BUF_SZ - p, "%s, ",
2534 			    section_flag[i].ln);
2535 			p += strlen(section_flag[i].ln) + 2;
2536 		} else
2537 			buf[p++] = section_flag[i].sn;
2538 	}
2539 	if (re->options & RE_T && p > nb + 4)
2540 		p -= 2;
2541 	buf[p] = '\0';
2542 
2543 	return (buf);
2544 }
2545 
2546 static void
2547 dump_shdr(struct readelf *re)
2548 {
2549 	struct section	*s;
2550 	int		 i;
2551 
2552 #define	S_HDR	"[Nr] Name", "Type", "Addr", "Off", "Size", "ES",	\
2553 		"Flg", "Lk", "Inf", "Al"
2554 #define	S_HDRL	"[Nr] Name", "Type", "Address", "Offset", "Size",	\
2555 		"EntSize", "Flags", "Link", "Info", "Align"
2556 #define	ST_HDR	"[Nr] Name", "Type", "Addr", "Off", "Size", "ES",	\
2557 		"Lk", "Inf", "Al", "Flags"
2558 #define	ST_HDRL	"[Nr] Name", "Type", "Address", "Offset", "Link",	\
2559 		"Size", "EntSize", "Info", "Align", "Flags"
2560 #define	S_CT	i, s->name, section_type(re->ehdr.e_machine, s->type),	\
2561 		(uintmax_t)s->addr, (uintmax_t)s->off, (uintmax_t)s->sz,\
2562 		(uintmax_t)s->entsize, section_flags(re, s),		\
2563 		s->link, s->info, (uintmax_t)s->align
2564 #define	ST_CT	i, s->name, section_type(re->ehdr.e_machine, s->type),  \
2565 		(uintmax_t)s->addr, (uintmax_t)s->off, (uintmax_t)s->sz,\
2566 		(uintmax_t)s->entsize, s->link, s->info,		\
2567 		(uintmax_t)s->align, section_flags(re, s)
2568 #define	ST_CTL	i, s->name, section_type(re->ehdr.e_machine, s->type),  \
2569 		(uintmax_t)s->addr, (uintmax_t)s->off, s->link,		\
2570 		(uintmax_t)s->sz, (uintmax_t)s->entsize, s->info,	\
2571 		(uintmax_t)s->align, section_flags(re, s)
2572 
2573 	if (re->shnum == 0) {
2574 		printf("\nThere are no sections in this file.\n");
2575 		return;
2576 	}
2577 	printf("There are %ju section headers, starting at offset 0x%jx:\n",
2578 	    (uintmax_t)re->shnum, (uintmax_t)re->ehdr.e_shoff);
2579 	printf("\nSection Headers:\n");
2580 	if (re->ec == ELFCLASS32) {
2581 		if (re->options & RE_T)
2582 			printf("  %s\n       %-16s%-9s%-7s%-7s%-5s%-3s%-4s%s\n"
2583 			    "%12s\n", ST_HDR);
2584 		else
2585 			printf("  %-23s%-16s%-9s%-7s%-7s%-3s%-4s%-3s%-4s%s\n",
2586 			    S_HDR);
2587 	} else if (re->options & RE_WW) {
2588 		if (re->options & RE_T)
2589 			printf("  %s\n       %-16s%-17s%-7s%-7s%-5s%-3s%-4s%s\n"
2590 			    "%12s\n", ST_HDR);
2591 		else
2592 			printf("  %-23s%-16s%-17s%-7s%-7s%-3s%-4s%-3s%-4s%s\n",
2593 			    S_HDR);
2594 	} else {
2595 		if (re->options & RE_T)
2596 			printf("  %s\n       %-18s%-17s%-18s%s\n       %-18s"
2597 			    "%-17s%-18s%s\n%12s\n", ST_HDRL);
2598 		else
2599 			printf("  %-23s%-17s%-18s%s\n       %-18s%-17s%-7s%"
2600 			    "-6s%-6s%s\n", S_HDRL);
2601 	}
2602 	for (i = 0; (size_t)i < re->shnum; i++) {
2603 		s = &re->sl[i];
2604 		if (re->ec == ELFCLASS32) {
2605 			if (re->options & RE_T)
2606 				printf("  [%2d] %s\n       %-15.15s %8.8jx"
2607 				    " %6.6jx %6.6jx %2.2jx  %2u %3u %2ju\n"
2608 				    "       %s\n", ST_CT);
2609 			else
2610 				printf("  [%2d] %-17.17s %-15.15s %8.8jx"
2611 				    " %6.6jx %6.6jx %2.2jx %3s %2u %3u %2ju\n",
2612 				    S_CT);
2613 		} else if (re->options & RE_WW) {
2614 			if (re->options & RE_T)
2615 				printf("  [%2d] %s\n       %-15.15s %16.16jx"
2616 				    " %6.6jx %6.6jx %2.2jx  %2u %3u %2ju\n"
2617 				    "       %s\n", ST_CT);
2618 			else
2619 				printf("  [%2d] %-17.17s %-15.15s %16.16jx"
2620 				    " %6.6jx %6.6jx %2.2jx %3s %2u %3u %2ju\n",
2621 				    S_CT);
2622 		} else {
2623 			if (re->options & RE_T)
2624 				printf("  [%2d] %s\n       %-15.15s  %16.16jx"
2625 				    "  %16.16jx  %u\n       %16.16jx %16.16jx"
2626 				    "  %-16u  %ju\n       %s\n", ST_CTL);
2627 			else
2628 				printf("  [%2d] %-17.17s %-15.15s  %16.16jx"
2629 				    "  %8.8jx\n       %16.16jx  %16.16jx "
2630 				    "%3s      %2u   %3u     %ju\n", S_CT);
2631 		}
2632 	}
2633 	if ((re->options & RE_T) == 0)
2634 		printf("Key to Flags:\n  W (write), A (alloc),"
2635 		    " X (execute), M (merge), S (strings)\n"
2636 		    "  I (info), L (link order), G (group), x (unknown)\n"
2637 		    "  O (extra OS processing required)"
2638 		    " o (OS specific), p (processor specific)\n");
2639 
2640 #undef	S_HDR
2641 #undef	S_HDRL
2642 #undef	ST_HDR
2643 #undef	ST_HDRL
2644 #undef	S_CT
2645 #undef	ST_CT
2646 #undef	ST_CTL
2647 }
2648 
2649 static void
2650 dump_dynamic(struct readelf *re)
2651 {
2652 	GElf_Dyn	 dyn;
2653 	Elf_Data	*d;
2654 	struct section	*s;
2655 	int		 elferr, i, is_dynamic, j, jmax, nentries;
2656 
2657 	is_dynamic = 0;
2658 
2659 	for (i = 0; (size_t)i < re->shnum; i++) {
2660 		s = &re->sl[i];
2661 		if (s->type != SHT_DYNAMIC)
2662 			continue;
2663 		(void) elf_errno();
2664 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
2665 			elferr = elf_errno();
2666 			if (elferr != 0)
2667 				warnx("elf_getdata failed: %s", elf_errmsg(-1));
2668 			continue;
2669 		}
2670 		if (d->d_size <= 0)
2671 			continue;
2672 
2673 		is_dynamic = 1;
2674 
2675 		/* Determine the actual number of table entries. */
2676 		nentries = 0;
2677 		jmax = (int) (s->sz / s->entsize);
2678 
2679 		for (j = 0; j < jmax; j++) {
2680 			if (gelf_getdyn(d, j, &dyn) != &dyn) {
2681 				warnx("gelf_getdyn failed: %s",
2682 				    elf_errmsg(-1));
2683 				continue;
2684 			}
2685 			nentries ++;
2686 			if (dyn.d_tag == DT_NULL)
2687 				break;
2688                 }
2689 
2690 		printf("\nDynamic section at offset 0x%jx", (uintmax_t)s->off);
2691 		printf(" contains %u entries:\n", nentries);
2692 
2693 		if (re->ec == ELFCLASS32)
2694 			printf("%5s%12s%28s\n", "Tag", "Type", "Name/Value");
2695 		else
2696 			printf("%5s%20s%28s\n", "Tag", "Type", "Name/Value");
2697 
2698 		for (j = 0; j < nentries; j++) {
2699 			if (gelf_getdyn(d, j, &dyn) != &dyn)
2700 				continue;
2701 			/* Dump dynamic entry type. */
2702 			if (re->ec == ELFCLASS32)
2703 				printf(" 0x%8.8jx", (uintmax_t)dyn.d_tag);
2704 			else
2705 				printf(" 0x%16.16jx", (uintmax_t)dyn.d_tag);
2706 			printf(" %-20s", dt_type(re->ehdr.e_machine,
2707 			    dyn.d_tag));
2708 			/* Dump dynamic entry value. */
2709 			dump_dyn_val(re, &dyn, s->link);
2710 		}
2711 	}
2712 
2713 	if (!is_dynamic)
2714 		printf("\nThere is no dynamic section in this file.\n");
2715 }
2716 
2717 static char *
2718 timestamp(time_t ti)
2719 {
2720 	static char ts[32];
2721 	struct tm *t;
2722 
2723 	t = gmtime(&ti);
2724 	snprintf(ts, sizeof(ts), "%04d-%02d-%02dT%02d:%02d:%02d",
2725 	    t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour,
2726 	    t->tm_min, t->tm_sec);
2727 
2728 	return (ts);
2729 }
2730 
2731 static const char *
2732 dyn_str(struct readelf *re, uint32_t stab, uint64_t d_val)
2733 {
2734 	const char *name;
2735 
2736 	if (stab == SHN_UNDEF)
2737 		name = "ERROR";
2738 	else if ((name = elf_strptr(re->elf, stab, d_val)) == NULL) {
2739 		(void) elf_errno(); /* clear error */
2740 		name = "ERROR";
2741 	}
2742 
2743 	return (name);
2744 }
2745 
2746 static void
2747 dump_arch_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab)
2748 {
2749 	const char *name;
2750 
2751 	switch (re->ehdr.e_machine) {
2752 	case EM_MIPS:
2753 	case EM_MIPS_RS3_LE:
2754 		switch (dyn->d_tag) {
2755 		case DT_MIPS_RLD_VERSION:
2756 		case DT_MIPS_LOCAL_GOTNO:
2757 		case DT_MIPS_CONFLICTNO:
2758 		case DT_MIPS_LIBLISTNO:
2759 		case DT_MIPS_SYMTABNO:
2760 		case DT_MIPS_UNREFEXTNO:
2761 		case DT_MIPS_GOTSYM:
2762 		case DT_MIPS_HIPAGENO:
2763 		case DT_MIPS_DELTA_CLASS_NO:
2764 		case DT_MIPS_DELTA_INSTANCE_NO:
2765 		case DT_MIPS_DELTA_RELOC_NO:
2766 		case DT_MIPS_DELTA_SYM_NO:
2767 		case DT_MIPS_DELTA_CLASSSYM_NO:
2768 		case DT_MIPS_LOCALPAGE_GOTIDX:
2769 		case DT_MIPS_LOCAL_GOTIDX:
2770 		case DT_MIPS_HIDDEN_GOTIDX:
2771 		case DT_MIPS_PROTECTED_GOTIDX:
2772 			printf(" %ju\n", (uintmax_t) dyn->d_un.d_val);
2773 			break;
2774 		case DT_MIPS_ICHECKSUM:
2775 		case DT_MIPS_FLAGS:
2776 		case DT_MIPS_BASE_ADDRESS:
2777 		case DT_MIPS_CONFLICT:
2778 		case DT_MIPS_LIBLIST:
2779 		case DT_MIPS_RLD_MAP:
2780 		case DT_MIPS_DELTA_CLASS:
2781 		case DT_MIPS_DELTA_INSTANCE:
2782 		case DT_MIPS_DELTA_RELOC:
2783 		case DT_MIPS_DELTA_SYM:
2784 		case DT_MIPS_DELTA_CLASSSYM:
2785 		case DT_MIPS_CXX_FLAGS:
2786 		case DT_MIPS_PIXIE_INIT:
2787 		case DT_MIPS_SYMBOL_LIB:
2788 		case DT_MIPS_OPTIONS:
2789 		case DT_MIPS_INTERFACE:
2790 		case DT_MIPS_DYNSTR_ALIGN:
2791 		case DT_MIPS_INTERFACE_SIZE:
2792 		case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
2793 		case DT_MIPS_COMPACT_SIZE:
2794 		case DT_MIPS_GP_VALUE:
2795 		case DT_MIPS_AUX_DYNAMIC:
2796 		case DT_MIPS_PLTGOT:
2797 		case DT_MIPS_RLD_OBJ_UPDATE:
2798 		case DT_MIPS_RWPLT:
2799 			printf(" 0x%jx\n", (uintmax_t) dyn->d_un.d_val);
2800 			break;
2801 		case DT_MIPS_IVERSION:
2802 		case DT_MIPS_PERF_SUFFIX:
2803 		case DT_AUXILIARY:
2804 		case DT_FILTER:
2805 			name = dyn_str(re, stab, dyn->d_un.d_val);
2806 			printf(" %s\n", name);
2807 			break;
2808 		case DT_MIPS_TIME_STAMP:
2809 			printf(" %s\n", timestamp(dyn->d_un.d_val));
2810 			break;
2811 		}
2812 		break;
2813 	default:
2814 		printf("\n");
2815 		break;
2816 	}
2817 }
2818 
2819 static void
2820 dump_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab)
2821 {
2822 	const char *name;
2823 
2824 	if (dyn->d_tag >= DT_LOPROC && dyn->d_tag <= DT_HIPROC) {
2825 		dump_arch_dyn_val(re, dyn, stab);
2826 		return;
2827 	}
2828 
2829 	/* These entry values are index into the string table. */
2830 	name = NULL;
2831 	if (dyn->d_tag == DT_NEEDED || dyn->d_tag == DT_SONAME ||
2832 	    dyn->d_tag == DT_RPATH || dyn->d_tag == DT_RUNPATH)
2833 		name = dyn_str(re, stab, dyn->d_un.d_val);
2834 
2835 	switch(dyn->d_tag) {
2836 	case DT_NULL:
2837 	case DT_PLTGOT:
2838 	case DT_HASH:
2839 	case DT_STRTAB:
2840 	case DT_SYMTAB:
2841 	case DT_RELA:
2842 	case DT_INIT:
2843 	case DT_SYMBOLIC:
2844 	case DT_REL:
2845 	case DT_DEBUG:
2846 	case DT_TEXTREL:
2847 	case DT_JMPREL:
2848 	case DT_FINI:
2849 	case DT_VERDEF:
2850 	case DT_VERNEED:
2851 	case DT_VERSYM:
2852 	case DT_GNU_HASH:
2853 	case DT_GNU_LIBLIST:
2854 	case DT_GNU_CONFLICT:
2855 		printf(" 0x%jx\n", (uintmax_t) dyn->d_un.d_val);
2856 		break;
2857 	case DT_PLTRELSZ:
2858 	case DT_RELASZ:
2859 	case DT_RELAENT:
2860 	case DT_STRSZ:
2861 	case DT_SYMENT:
2862 	case DT_RELSZ:
2863 	case DT_RELENT:
2864 	case DT_INIT_ARRAYSZ:
2865 	case DT_FINI_ARRAYSZ:
2866 	case DT_GNU_CONFLICTSZ:
2867 	case DT_GNU_LIBLISTSZ:
2868 		printf(" %ju (bytes)\n", (uintmax_t) dyn->d_un.d_val);
2869 		break;
2870  	case DT_RELACOUNT:
2871 	case DT_RELCOUNT:
2872 	case DT_VERDEFNUM:
2873 	case DT_VERNEEDNUM:
2874 		printf(" %ju\n", (uintmax_t) dyn->d_un.d_val);
2875 		break;
2876 	case DT_NEEDED:
2877 		printf(" Shared library: [%s]\n", name);
2878 		break;
2879 	case DT_SONAME:
2880 		printf(" Library soname: [%s]\n", name);
2881 		break;
2882 	case DT_RPATH:
2883 		printf(" Library rpath: [%s]\n", name);
2884 		break;
2885 	case DT_RUNPATH:
2886 		printf(" Library runpath: [%s]\n", name);
2887 		break;
2888 	case DT_PLTREL:
2889 		printf(" %s\n", dt_type(re->ehdr.e_machine, dyn->d_un.d_val));
2890 		break;
2891 	case DT_GNU_PRELINKED:
2892 		printf(" %s\n", timestamp(dyn->d_un.d_val));
2893 		break;
2894 	default:
2895 		printf("\n");
2896 	}
2897 }
2898 
2899 static void
2900 dump_rel(struct readelf *re, struct section *s, Elf_Data *d)
2901 {
2902 	GElf_Rel r;
2903 	const char *symname;
2904 	uint64_t symval;
2905 	int i, len;
2906 
2907 #define	REL_HDR "r_offset", "r_info", "r_type", "st_value", "st_name"
2908 #define	REL_CT32 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
2909 		r_type(re->ehdr.e_machine, ELF32_R_TYPE(r.r_info)), \
2910 		(uintmax_t)symval, symname
2911 #define	REL_CT64 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
2912 		r_type(re->ehdr.e_machine, ELF64_R_TYPE(r.r_info)), \
2913 		(uintmax_t)symval, symname
2914 
2915 	printf("\nRelocation section (%s):\n", s->name);
2916 	if (re->ec == ELFCLASS32)
2917 		printf("%-8s %-8s %-19s %-8s %s\n", REL_HDR);
2918 	else {
2919 		if (re->options & RE_WW)
2920 			printf("%-16s %-16s %-24s %-16s %s\n", REL_HDR);
2921 		else
2922 			printf("%-12s %-12s %-19s %-16s %s\n", REL_HDR);
2923 	}
2924 	len = d->d_size / s->entsize;
2925 	for (i = 0; i < len; i++) {
2926 		if (gelf_getrel(d, i, &r) != &r) {
2927 			warnx("gelf_getrel failed: %s", elf_errmsg(-1));
2928 			continue;
2929 		}
2930 		symname = get_symbol_name(re, s->link, GELF_R_SYM(r.r_info));
2931 		symval = get_symbol_value(re, s->link, GELF_R_SYM(r.r_info));
2932 		if (re->ec == ELFCLASS32) {
2933 			r.r_info = ELF32_R_INFO(ELF64_R_SYM(r.r_info),
2934 			    ELF64_R_TYPE(r.r_info));
2935 			printf("%8.8jx %8.8jx %-19.19s %8.8jx %s\n", REL_CT32);
2936 		} else {
2937 			if (re->options & RE_WW)
2938 				printf("%16.16jx %16.16jx %-24.24s"
2939 				    " %16.16jx %s\n", REL_CT64);
2940 			else
2941 				printf("%12.12jx %12.12jx %-19.19s"
2942 				    " %16.16jx %s\n", REL_CT64);
2943 		}
2944 	}
2945 
2946 #undef	REL_HDR
2947 #undef	REL_CT
2948 }
2949 
2950 static void
2951 dump_rela(struct readelf *re, struct section *s, Elf_Data *d)
2952 {
2953 	GElf_Rela r;
2954 	const char *symname;
2955 	uint64_t symval;
2956 	int i, len;
2957 
2958 #define	RELA_HDR "r_offset", "r_info", "r_type", "st_value", \
2959 		"st_name + r_addend"
2960 #define	RELA_CT32 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
2961 		r_type(re->ehdr.e_machine, ELF32_R_TYPE(r.r_info)), \
2962 		(uintmax_t)symval, symname
2963 #define	RELA_CT64 (uintmax_t)r.r_offset, (uintmax_t)r.r_info,	    \
2964 		r_type(re->ehdr.e_machine, ELF64_R_TYPE(r.r_info)), \
2965 		(uintmax_t)symval, symname
2966 
2967 	printf("\nRelocation section with addend (%s):\n", s->name);
2968 	if (re->ec == ELFCLASS32)
2969 		printf("%-8s %-8s %-19s %-8s %s\n", RELA_HDR);
2970 	else {
2971 		if (re->options & RE_WW)
2972 			printf("%-16s %-16s %-24s %-16s %s\n", RELA_HDR);
2973 		else
2974 			printf("%-12s %-12s %-19s %-16s %s\n", RELA_HDR);
2975 	}
2976 	len = d->d_size / s->entsize;
2977 	for (i = 0; i < len; i++) {
2978 		if (gelf_getrela(d, i, &r) != &r) {
2979 			warnx("gelf_getrel failed: %s", elf_errmsg(-1));
2980 			continue;
2981 		}
2982 		symname = get_symbol_name(re, s->link, GELF_R_SYM(r.r_info));
2983 		symval = get_symbol_value(re, s->link, GELF_R_SYM(r.r_info));
2984 		if (re->ec == ELFCLASS32) {
2985 			r.r_info = ELF32_R_INFO(ELF64_R_SYM(r.r_info),
2986 			    ELF64_R_TYPE(r.r_info));
2987 			printf("%8.8jx %8.8jx %-19.19s %8.8jx %s", RELA_CT32);
2988 			printf(" + %x\n", (uint32_t) r.r_addend);
2989 		} else {
2990 			if (re->options & RE_WW)
2991 				printf("%16.16jx %16.16jx %-24.24s"
2992 				    " %16.16jx %s", RELA_CT64);
2993 			else
2994 				printf("%12.12jx %12.12jx %-19.19s"
2995 				    " %16.16jx %s", RELA_CT64);
2996 			printf(" + %jx\n", (uintmax_t) r.r_addend);
2997 		}
2998 	}
2999 
3000 #undef	RELA_HDR
3001 #undef	RELA_CT
3002 }
3003 
3004 static void
3005 dump_reloc(struct readelf *re)
3006 {
3007 	struct section *s;
3008 	Elf_Data *d;
3009 	int i, elferr;
3010 
3011 	for (i = 0; (size_t)i < re->shnum; i++) {
3012 		s = &re->sl[i];
3013 		if (s->type == SHT_REL || s->type == SHT_RELA) {
3014 			(void) elf_errno();
3015 			if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3016 				elferr = elf_errno();
3017 				if (elferr != 0)
3018 					warnx("elf_getdata failed: %s",
3019 					    elf_errmsg(elferr));
3020 				continue;
3021 			}
3022 			if (s->type == SHT_REL)
3023 				dump_rel(re, s, d);
3024 			else
3025 				dump_rela(re, s, d);
3026 		}
3027 	}
3028 }
3029 
3030 static void
3031 dump_symtab(struct readelf *re, int i)
3032 {
3033 	struct section *s;
3034 	Elf_Data *d;
3035 	GElf_Sym sym;
3036 	const char *name;
3037 	int elferr, stab, j;
3038 
3039 	s = &re->sl[i];
3040 	stab = s->link;
3041 	(void) elf_errno();
3042 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3043 		elferr = elf_errno();
3044 		if (elferr != 0)
3045 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
3046 		return;
3047 	}
3048 	if (d->d_size <= 0)
3049 		return;
3050 	printf("Symbol table (%s)", s->name);
3051 	printf(" contains %ju entries:\n", s->sz / s->entsize);
3052 	printf("%7s%9s%14s%5s%8s%6s%9s%5s\n", "Num:", "Value", "Size", "Type",
3053 	    "Bind", "Vis", "Ndx", "Name");
3054 
3055 	for (j = 0; (uint64_t)j < s->sz / s->entsize; j++) {
3056 		if (gelf_getsym(d, j, &sym) != &sym) {
3057 			warnx("gelf_getsym failed: %s", elf_errmsg(-1));
3058 			continue;
3059 		}
3060 		printf("%6d:", j);
3061 		printf(" %16.16jx", (uintmax_t)sym.st_value);
3062 		printf(" %5ju", sym.st_size);
3063 		printf(" %-7s", st_type(GELF_ST_TYPE(sym.st_info)));
3064 		printf(" %-6s", st_bind(GELF_ST_BIND(sym.st_info)));
3065 		printf(" %-8s", st_vis(GELF_ST_VISIBILITY(sym.st_other)));
3066 		printf(" %3s", st_shndx(sym.st_shndx));
3067 		if ((name = elf_strptr(re->elf, stab, sym.st_name)) != NULL)
3068 			printf(" %s", name);
3069 		/* Append symbol version string for SHT_DYNSYM symbol table. */
3070 		if (s->type == SHT_DYNSYM && re->ver != NULL &&
3071 		    re->vs != NULL && re->vs[j] > 1) {
3072 			if (re->vs[j] & 0x8000 ||
3073 			    re->ver[re->vs[j] & 0x7fff].type == 0)
3074 				printf("@%s (%d)",
3075 				    re->ver[re->vs[j] & 0x7fff].name,
3076 				    re->vs[j] & 0x7fff);
3077 			else
3078 				printf("@@%s (%d)", re->ver[re->vs[j]].name,
3079 				    re->vs[j]);
3080 		}
3081 		putchar('\n');
3082 	}
3083 
3084 }
3085 
3086 static void
3087 dump_symtabs(struct readelf *re)
3088 {
3089 	GElf_Dyn dyn;
3090 	Elf_Data *d;
3091 	struct section *s;
3092 	uint64_t dyn_off;
3093 	int elferr, i;
3094 
3095 	/*
3096 	 * If -D is specified, only dump the symbol table specified by
3097 	 * the DT_SYMTAB entry in the .dynamic section.
3098 	 */
3099 	dyn_off = 0;
3100 	if (re->options & RE_DD) {
3101 		s = NULL;
3102 		for (i = 0; (size_t)i < re->shnum; i++)
3103 			if (re->sl[i].type == SHT_DYNAMIC) {
3104 				s = &re->sl[i];
3105 				break;
3106 			}
3107 		if (s == NULL)
3108 			return;
3109 		(void) elf_errno();
3110 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3111 			elferr = elf_errno();
3112 			if (elferr != 0)
3113 				warnx("elf_getdata failed: %s", elf_errmsg(-1));
3114 			return;
3115 		}
3116 		if (d->d_size <= 0)
3117 			return;
3118 
3119 		for (i = 0; (uint64_t)i < s->sz / s->entsize; i++) {
3120 			if (gelf_getdyn(d, i, &dyn) != &dyn) {
3121 				warnx("gelf_getdyn failed: %s", elf_errmsg(-1));
3122 				continue;
3123 			}
3124 			if (dyn.d_tag == DT_SYMTAB) {
3125 				dyn_off = dyn.d_un.d_val;
3126 				break;
3127 			}
3128 		}
3129 	}
3130 
3131 	/* Find and dump symbol tables. */
3132 	for (i = 0; (size_t)i < re->shnum; i++) {
3133 		s = &re->sl[i];
3134 		if (s->type == SHT_SYMTAB || s->type == SHT_DYNSYM) {
3135 			if (re->options & RE_DD) {
3136 				if (dyn_off == s->addr) {
3137 					dump_symtab(re, i);
3138 					break;
3139 				}
3140 			} else
3141 				dump_symtab(re, i);
3142 		}
3143 	}
3144 }
3145 
3146 static void
3147 dump_svr4_hash(struct section *s)
3148 {
3149 	Elf_Data	*d;
3150 	uint32_t	*buf;
3151 	uint32_t	 nbucket, nchain;
3152 	uint32_t	*bucket, *chain;
3153 	uint32_t	*bl, *c, maxl, total;
3154 	int		 elferr, i, j;
3155 
3156 	/* Read and parse the content of .hash section. */
3157 	(void) elf_errno();
3158 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3159 		elferr = elf_errno();
3160 		if (elferr != 0)
3161 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
3162 		return;
3163 	}
3164 	if (d->d_size < 2 * sizeof(uint32_t)) {
3165 		warnx(".hash section too small");
3166 		return;
3167 	}
3168 	buf = d->d_buf;
3169 	nbucket = buf[0];
3170 	nchain = buf[1];
3171 	if (nbucket <= 0 || nchain <= 0) {
3172 		warnx("Malformed .hash section");
3173 		return;
3174 	}
3175 	if (d->d_size != (nbucket + nchain + 2) * sizeof(uint32_t)) {
3176 		warnx("Malformed .hash section");
3177 		return;
3178 	}
3179 	bucket = &buf[2];
3180 	chain = &buf[2 + nbucket];
3181 
3182 	maxl = 0;
3183 	if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
3184 		errx(EXIT_FAILURE, "calloc failed");
3185 	for (i = 0; (uint32_t)i < nbucket; i++)
3186 		for (j = bucket[i]; j > 0 && (uint32_t)j < nchain; j = chain[j])
3187 			if (++bl[i] > maxl)
3188 				maxl = bl[i];
3189 	if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
3190 		errx(EXIT_FAILURE, "calloc failed");
3191 	for (i = 0; (uint32_t)i < nbucket; i++)
3192 		c[bl[i]]++;
3193 	printf("\nHistogram for bucket list length (total of %u buckets):\n",
3194 	    nbucket);
3195 	printf(" Length\tNumber\t\t%% of total\tCoverage\n");
3196 	total = 0;
3197 	for (i = 0; (uint32_t)i <= maxl; i++) {
3198 		total += c[i] * i;
3199 		printf("%7u\t%-10u\t(%5.1f%%)\t%5.1f%%\n", i, c[i],
3200 		    c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
3201 	}
3202 	free(c);
3203 	free(bl);
3204 }
3205 
3206 static void
3207 dump_svr4_hash64(struct readelf *re, struct section *s)
3208 {
3209 	Elf_Data	*d, dst;
3210 	uint64_t	*buf;
3211 	uint64_t	 nbucket, nchain;
3212 	uint64_t	*bucket, *chain;
3213 	uint64_t	*bl, *c, maxl, total;
3214 	int		 elferr, i, j;
3215 
3216 	/*
3217 	 * ALPHA uses 64-bit hash entries. Since libelf assumes that
3218 	 * .hash section contains only 32-bit entry, an explicit
3219 	 * gelf_xlatetom is needed here.
3220 	 */
3221 	(void) elf_errno();
3222 	if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
3223 		elferr = elf_errno();
3224 		if (elferr != 0)
3225 			warnx("elf_rawdata failed: %s",
3226 			    elf_errmsg(elferr));
3227 		return;
3228 	}
3229 	d->d_type = ELF_T_XWORD;
3230 	memcpy(&dst, d, sizeof(Elf_Data));
3231 	if (gelf_xlatetom(re->elf, &dst, d,
3232 		re->ehdr.e_ident[EI_DATA]) != &dst) {
3233 		warnx("gelf_xlatetom failed: %s", elf_errmsg(-1));
3234 		return;
3235 	}
3236 	if (dst.d_size < 2 * sizeof(uint64_t)) {
3237 		warnx(".hash section too small");
3238 		return;
3239 	}
3240 	buf = dst.d_buf;
3241 	nbucket = buf[0];
3242 	nchain = buf[1];
3243 	if (nbucket <= 0 || nchain <= 0) {
3244 		warnx("Malformed .hash section");
3245 		return;
3246 	}
3247 	if (d->d_size != (nbucket + nchain + 2) * sizeof(uint32_t)) {
3248 		warnx("Malformed .hash section");
3249 		return;
3250 	}
3251 	bucket = &buf[2];
3252 	chain = &buf[2 + nbucket];
3253 
3254 	maxl = 0;
3255 	if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
3256 		errx(EXIT_FAILURE, "calloc failed");
3257 	for (i = 0; (uint32_t)i < nbucket; i++)
3258 		for (j = bucket[i]; j > 0 && (uint32_t)j < nchain; j = chain[j])
3259 			if (++bl[i] > maxl)
3260 				maxl = bl[i];
3261 	if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
3262 		errx(EXIT_FAILURE, "calloc failed");
3263 	for (i = 0; (uint64_t)i < nbucket; i++)
3264 		c[bl[i]]++;
3265 	printf("Histogram for bucket list length (total of %ju buckets):\n",
3266 	    (uintmax_t)nbucket);
3267 	printf(" Length\tNumber\t\t%% of total\tCoverage\n");
3268 	total = 0;
3269 	for (i = 0; (uint64_t)i <= maxl; i++) {
3270 		total += c[i] * i;
3271 		printf("%7u\t%-10ju\t(%5.1f%%)\t%5.1f%%\n", i, (uintmax_t)c[i],
3272 		    c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
3273 	}
3274 	free(c);
3275 	free(bl);
3276 }
3277 
3278 static void
3279 dump_gnu_hash(struct readelf *re, struct section *s)
3280 {
3281 	struct section	*ds;
3282 	Elf_Data	*d;
3283 	uint32_t	*buf;
3284 	uint32_t	*bucket, *chain;
3285 	uint32_t	 nbucket, nchain, symndx, maskwords, shift2;
3286 	uint32_t	*bl, *c, maxl, total;
3287 	int		 elferr, dynsymcount, i, j;
3288 
3289 	(void) elf_errno();
3290 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3291 		elferr = elf_errno();
3292 		if (elferr != 0)
3293 			warnx("elf_getdata failed: %s",
3294 			    elf_errmsg(elferr));
3295 		return;
3296 	}
3297 	if (d->d_size < 4 * sizeof(uint32_t)) {
3298 		warnx(".gnu.hash section too small");
3299 		return;
3300 	}
3301 	buf = d->d_buf;
3302 	nbucket = buf[0];
3303 	symndx = buf[1];
3304 	maskwords = buf[2];
3305 	shift2 = buf[3];
3306 	buf += 4;
3307 	ds = &re->sl[s->link];
3308 	dynsymcount = ds->sz / ds->entsize;
3309 	nchain = dynsymcount - symndx;
3310 	if (d->d_size != 4 * sizeof(uint32_t) + maskwords *
3311 	    (re->ec == ELFCLASS32 ? sizeof(uint32_t) : sizeof(uint64_t)) +
3312 	    (nbucket + nchain) * sizeof(uint32_t)) {
3313 		warnx("Malformed .gnu.hash section");
3314 		return;
3315 	}
3316 	bucket = buf + (re->ec == ELFCLASS32 ? maskwords : maskwords * 2);
3317 	chain = bucket + nbucket;
3318 
3319 	maxl = 0;
3320 	if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
3321 		errx(EXIT_FAILURE, "calloc failed");
3322 	for (i = 0; (uint32_t)i < nbucket; i++)
3323 		for (j = bucket[i]; j > 0 && (uint32_t)j - symndx < nchain;
3324 		     j++) {
3325 			if (++bl[i] > maxl)
3326 				maxl = bl[i];
3327 			if (chain[j - symndx] & 1)
3328 				break;
3329 		}
3330 	if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
3331 		errx(EXIT_FAILURE, "calloc failed");
3332 	for (i = 0; (uint32_t)i < nbucket; i++)
3333 		c[bl[i]]++;
3334 	printf("Histogram for bucket list length (total of %u buckets):\n",
3335 	    nbucket);
3336 	printf(" Length\tNumber\t\t%% of total\tCoverage\n");
3337 	total = 0;
3338 	for (i = 0; (uint32_t)i <= maxl; i++) {
3339 		total += c[i] * i;
3340 		printf("%7u\t%-10u\t(%5.1f%%)\t%5.1f%%\n", i, c[i],
3341 		    c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1));
3342 	}
3343 	free(c);
3344 	free(bl);
3345 }
3346 
3347 static void
3348 dump_hash(struct readelf *re)
3349 {
3350 	struct section	*s;
3351 	int		 i;
3352 
3353 	for (i = 0; (size_t) i < re->shnum; i++) {
3354 		s = &re->sl[i];
3355 		if (s->type == SHT_HASH || s->type == SHT_GNU_HASH) {
3356 			if (s->type == SHT_GNU_HASH)
3357 				dump_gnu_hash(re, s);
3358 			else if (re->ehdr.e_machine == EM_ALPHA &&
3359 			    s->entsize == 8)
3360 				dump_svr4_hash64(re, s);
3361 			else
3362 				dump_svr4_hash(s);
3363 		}
3364 	}
3365 }
3366 
3367 static void
3368 dump_notes(struct readelf *re)
3369 {
3370 	struct section *s;
3371 	const char *rawfile;
3372 	GElf_Phdr phdr;
3373 	Elf_Data *d;
3374 	size_t phnum;
3375 	int i, elferr;
3376 
3377 	if (re->ehdr.e_type == ET_CORE) {
3378 		/*
3379 		 * Search program headers in the core file for
3380 		 * PT_NOTE entry.
3381 		 */
3382 		if (elf_getphnum(re->elf, &phnum) == 0) {
3383 			warnx("elf_getphnum failed: %s", elf_errmsg(-1));
3384 			return;
3385 		}
3386 		if (phnum == 0)
3387 			return;
3388 		if ((rawfile = elf_rawfile(re->elf, NULL)) == NULL) {
3389 			warnx("elf_rawfile failed: %s", elf_errmsg(-1));
3390 			return;
3391 		}
3392 		for (i = 0; (size_t) i < phnum; i++) {
3393 			if (gelf_getphdr(re->elf, i, &phdr) != &phdr) {
3394 				warnx("gelf_getphdr failed: %s",
3395 				    elf_errmsg(-1));
3396 				continue;
3397 			}
3398 			if (phdr.p_type == PT_NOTE)
3399 				dump_notes_content(re, rawfile + phdr.p_offset,
3400 				    phdr.p_filesz, phdr.p_offset);
3401 		}
3402 
3403 	} else {
3404 		/*
3405 		 * For objects other than core files, Search for
3406 		 * SHT_NOTE sections.
3407 		 */
3408 		for (i = 0; (size_t) i < re->shnum; i++) {
3409 			s = &re->sl[i];
3410 			if (s->type == SHT_NOTE) {
3411 				(void) elf_errno();
3412 				if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3413 					elferr = elf_errno();
3414 					if (elferr != 0)
3415 						warnx("elf_getdata failed: %s",
3416 						    elf_errmsg(elferr));
3417 					continue;
3418 				}
3419 				dump_notes_content(re, d->d_buf, d->d_size,
3420 				    s->off);
3421 			}
3422 		}
3423 	}
3424 }
3425 
3426 static void
3427 dump_notes_content(struct readelf *re, const char *buf, size_t sz, off_t off)
3428 {
3429 	Elf_Note *note;
3430 	const char *end;
3431 
3432 	printf("\nNotes at offset %#010jx with length %#010jx:\n",
3433 	    (uintmax_t) off, (uintmax_t) sz);
3434 	printf("  %-13s %-15s %s\n", "Owner", "Data size", "Description");
3435 	end = buf + sz;
3436 	while (buf < end) {
3437 		note = (Elf_Note *)(uintptr_t) buf;
3438 		printf("  %-13s %#010jx", (char *)(uintptr_t) (note + 1),
3439 		    (uintmax_t) note->n_descsz);
3440 		printf("      %s\n", note_type(re->ehdr.e_ident[EI_OSABI],
3441 		    re->ehdr.e_type, note->n_type));
3442 		buf += sizeof(Elf_Note);
3443 		if (re->ec == ELFCLASS32)
3444 			buf += roundup2(note->n_namesz, 4) +
3445 			    roundup2(note->n_descsz, 4);
3446 		else
3447 			buf += roundup2(note->n_namesz, 8) +
3448 			    roundup2(note->n_descsz, 8);
3449 	}
3450 }
3451 
3452 /*
3453  * Symbol versioning sections are the same for 32bit and 64bit
3454  * ELF objects.
3455  */
3456 #define Elf_Verdef	Elf32_Verdef
3457 #define	Elf_Verdaux	Elf32_Verdaux
3458 #define	Elf_Verneed	Elf32_Verneed
3459 #define	Elf_Vernaux	Elf32_Vernaux
3460 
3461 #define	SAVE_VERSION_NAME(x, n, t)					\
3462 	do {								\
3463 		while (x >= re->ver_sz) {				\
3464 			nv = realloc(re->ver,				\
3465 			    sizeof(*re->ver) * re->ver_sz * 2);		\
3466 			if (nv == NULL) {				\
3467 				warn("realloc failed");			\
3468 				free(re->ver);				\
3469 				return;					\
3470 			}						\
3471 			re->ver = nv;					\
3472 			for (i = re->ver_sz; i < re->ver_sz * 2; i++) {	\
3473 				re->ver[i].name = NULL;			\
3474 				re->ver[i].type = 0;			\
3475 			}						\
3476 			re->ver_sz *= 2;				\
3477 		}							\
3478 		if (x > 1) {						\
3479 			re->ver[x].name = n;				\
3480 			re->ver[x].type = t;				\
3481 		}							\
3482 	} while (0)
3483 
3484 
3485 static void
3486 dump_verdef(struct readelf *re, int dump)
3487 {
3488 	struct section *s;
3489 	struct symver *nv;
3490 	Elf_Data *d;
3491 	Elf_Verdef *vd;
3492 	Elf_Verdaux *vda;
3493 	uint8_t *buf, *end, *buf2;
3494 	const char *name;
3495 	int elferr, i, j;
3496 
3497 	if ((s = re->vd_s) == NULL)
3498 		return;
3499 
3500 	if (re->ver == NULL) {
3501 		re->ver_sz = 16;
3502 		if ((re->ver = calloc(re->ver_sz, sizeof(*re->ver))) ==
3503 		    NULL) {
3504 			warn("calloc failed");
3505 			return;
3506 		}
3507 		re->ver[0].name = "*local*";
3508 		re->ver[1].name = "*global*";
3509 	}
3510 
3511 	if (dump)
3512 		printf("\nVersion definition section (%s):\n", s->name);
3513 	(void) elf_errno();
3514 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3515 		elferr = elf_errno();
3516 		if (elferr != 0)
3517 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
3518 		return;
3519 	}
3520 	if (d->d_size == 0)
3521 		return;
3522 
3523 	buf = d->d_buf;
3524 	end = buf + d->d_size;
3525 	while (buf + sizeof(Elf_Verdef) <= end) {
3526 		vd = (Elf_Verdef *) (uintptr_t) buf;
3527 		if (dump) {
3528 			printf("  0x%4.4lx", (unsigned long)
3529 			    (buf - (uint8_t *)d->d_buf));
3530 			printf(" vd_version: %u vd_flags: %d"
3531 			    " vd_ndx: %u vd_cnt: %u", vd->vd_version,
3532 			    vd->vd_flags, vd->vd_ndx, vd->vd_cnt);
3533 		}
3534 		buf2 = buf + vd->vd_aux;
3535 		j = 0;
3536 		while (buf2 + sizeof(Elf_Verdaux) <= end && j < vd->vd_cnt) {
3537 			vda = (Elf_Verdaux *) (uintptr_t) buf2;
3538 			name = get_string(re, s->link, vda->vda_name);
3539 			if (j == 0) {
3540 				if (dump)
3541 					printf(" vda_name: %s\n", name);
3542 				SAVE_VERSION_NAME((int)vd->vd_ndx, name, 1);
3543 			} else if (dump)
3544 				printf("  0x%4.4lx parent: %s\n",
3545 				    (unsigned long) (buf2 -
3546 				    (uint8_t *)d->d_buf), name);
3547 			if (vda->vda_next == 0)
3548 				break;
3549 			buf2 += vda->vda_next;
3550 			j++;
3551 		}
3552 		if (vd->vd_next == 0)
3553 			break;
3554 		buf += vd->vd_next;
3555 	}
3556 }
3557 
3558 static void
3559 dump_verneed(struct readelf *re, int dump)
3560 {
3561 	struct section *s;
3562 	struct symver *nv;
3563 	Elf_Data *d;
3564 	Elf_Verneed *vn;
3565 	Elf_Vernaux *vna;
3566 	uint8_t *buf, *end, *buf2;
3567 	const char *name;
3568 	int elferr, i, j;
3569 
3570 	if ((s = re->vn_s) == NULL)
3571 		return;
3572 
3573 	if (re->ver == NULL) {
3574 		re->ver_sz = 16;
3575 		if ((re->ver = calloc(re->ver_sz, sizeof(*re->ver))) ==
3576 		    NULL) {
3577 			warn("calloc failed");
3578 			return;
3579 		}
3580 		re->ver[0].name = "*local*";
3581 		re->ver[1].name = "*global*";
3582 	}
3583 
3584 	if (dump)
3585 		printf("\nVersion needed section (%s):\n", s->name);
3586 	(void) elf_errno();
3587 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3588 		elferr = elf_errno();
3589 		if (elferr != 0)
3590 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
3591 		return;
3592 	}
3593 	if (d->d_size == 0)
3594 		return;
3595 
3596 	buf = d->d_buf;
3597 	end = buf + d->d_size;
3598 	while (buf + sizeof(Elf_Verneed) <= end) {
3599 		vn = (Elf_Verneed *) (uintptr_t) buf;
3600 		if (dump) {
3601 			printf("  0x%4.4lx", (unsigned long)
3602 			    (buf - (uint8_t *)d->d_buf));
3603 			printf(" vn_version: %u vn_file: %s vn_cnt: %u\n",
3604 			    vn->vn_version,
3605 			    get_string(re, s->link, vn->vn_file),
3606 			    vn->vn_cnt);
3607 		}
3608 		buf2 = buf + vn->vn_aux;
3609 		j = 0;
3610 		while (buf2 + sizeof(Elf_Vernaux) <= end && j < vn->vn_cnt) {
3611 			vna = (Elf32_Vernaux *) (uintptr_t) buf2;
3612 			if (dump)
3613 				printf("  0x%4.4lx", (unsigned long)
3614 				    (buf2 - (uint8_t *)d->d_buf));
3615 			name = get_string(re, s->link, vna->vna_name);
3616 			if (dump)
3617 				printf("   vna_name: %s vna_flags: %u"
3618 				    " vna_other: %u\n", name,
3619 				    vna->vna_flags, vna->vna_other);
3620 			SAVE_VERSION_NAME((int)vna->vna_other, name, 0);
3621 			if (vna->vna_next == 0)
3622 				break;
3623 			buf2 += vna->vna_next;
3624 			j++;
3625 		}
3626 		if (vn->vn_next == 0)
3627 			break;
3628 		buf += vn->vn_next;
3629 	}
3630 }
3631 
3632 static void
3633 dump_versym(struct readelf *re)
3634 {
3635 	int i;
3636 
3637 	if (re->vs_s == NULL || re->ver == NULL || re->vs == NULL)
3638 		return;
3639 	printf("\nVersion symbol section (%s):\n", re->vs_s->name);
3640 	for (i = 0; i < re->vs_sz; i++) {
3641 		if ((i & 3) == 0) {
3642 			if (i > 0)
3643 				putchar('\n');
3644 			printf("  %03x:", i);
3645 		}
3646 		if (re->vs[i] & 0x8000)
3647 			printf(" %3xh %-12s ", re->vs[i] & 0x7fff,
3648 			    re->ver[re->vs[i] & 0x7fff].name);
3649 		else
3650 			printf(" %3x %-12s ", re->vs[i],
3651 			    re->ver[re->vs[i]].name);
3652 	}
3653 	putchar('\n');
3654 }
3655 
3656 static void
3657 dump_ver(struct readelf *re)
3658 {
3659 
3660 	if (re->vs_s && re->ver && re->vs)
3661 		dump_versym(re);
3662 	if (re->vd_s)
3663 		dump_verdef(re, 1);
3664 	if (re->vn_s)
3665 		dump_verneed(re, 1);
3666 }
3667 
3668 static void
3669 search_ver(struct readelf *re)
3670 {
3671 	struct section *s;
3672 	Elf_Data *d;
3673 	int elferr, i;
3674 
3675 	for (i = 0; (size_t) i < re->shnum; i++) {
3676 		s = &re->sl[i];
3677 		if (s->type == SHT_SUNW_versym)
3678 			re->vs_s = s;
3679 		if (s->type == SHT_SUNW_verneed)
3680 			re->vn_s = s;
3681 		if (s->type == SHT_SUNW_verdef)
3682 			re->vd_s = s;
3683 	}
3684 	if (re->vd_s)
3685 		dump_verdef(re, 0);
3686 	if (re->vn_s)
3687 		dump_verneed(re, 0);
3688 	if (re->vs_s && re->ver != NULL) {
3689 		(void) elf_errno();
3690 		if ((d = elf_getdata(re->vs_s->scn, NULL)) == NULL) {
3691 			elferr = elf_errno();
3692 			if (elferr != 0)
3693 				warnx("elf_getdata failed: %s",
3694 				    elf_errmsg(elferr));
3695 			return;
3696 		}
3697 		if (d->d_size == 0)
3698 			return;
3699 		re->vs = d->d_buf;
3700 		re->vs_sz = d->d_size / sizeof(Elf32_Half);
3701 	}
3702 }
3703 
3704 #undef	Elf_Verdef
3705 #undef	Elf_Verdaux
3706 #undef	Elf_Verneed
3707 #undef	Elf_Vernaux
3708 #undef	SAVE_VERSION_NAME
3709 
3710 /*
3711  * Elf32_Lib and Elf64_Lib are identical.
3712  */
3713 #define	Elf_Lib		Elf32_Lib
3714 
3715 static void
3716 dump_liblist(struct readelf *re)
3717 {
3718 	struct section *s;
3719 	struct tm *t;
3720 	time_t ti;
3721 	char tbuf[20];
3722 	Elf_Data *d;
3723 	Elf_Lib *lib;
3724 	int i, j, k, elferr, first;
3725 
3726 	for (i = 0; (size_t) i < re->shnum; i++) {
3727 		s = &re->sl[i];
3728 		if (s->type != SHT_GNU_LIBLIST)
3729 			continue;
3730 		(void) elf_errno();
3731 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
3732 			elferr = elf_errno();
3733 			if (elferr != 0)
3734 				warnx("elf_getdata failed: %s",
3735 				    elf_errmsg(elferr));
3736 			continue;
3737 		}
3738 		if (d->d_size <= 0)
3739 			continue;
3740 		lib = d->d_buf;
3741 		printf("\nLibrary list section '%s' ", s->name);
3742 		printf("contains %ju entries:\n", s->sz / s->entsize);
3743 		printf("%12s%24s%18s%10s%6s\n", "Library", "Time Stamp",
3744 		    "Checksum", "Version", "Flags");
3745 		for (j = 0; (uint64_t) j < s->sz / s->entsize; j++) {
3746 			printf("%3d: ", j);
3747 			printf("%-20.20s ",
3748 			    get_string(re, s->link, lib->l_name));
3749 			ti = lib->l_time_stamp;
3750 			t = gmtime(&ti);
3751 			snprintf(tbuf, sizeof(tbuf), "%04d-%02d-%02dT%02d:%02d"
3752 			    ":%2d", t->tm_year + 1900, t->tm_mon + 1,
3753 			    t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
3754 			printf("%-19.19s ", tbuf);
3755 			printf("0x%08x ", lib->l_checksum);
3756 			printf("%-7d %#x", lib->l_version, lib->l_flags);
3757 			if (lib->l_flags != 0) {
3758 				first = 1;
3759 				putchar('(');
3760 				for (k = 0; l_flag[k].name != NULL; k++) {
3761 					if ((l_flag[k].value & lib->l_flags) ==
3762 					    0)
3763 						continue;
3764 					if (!first)
3765 						putchar(',');
3766 					else
3767 						first = 0;
3768 					printf("%s", l_flag[k].name);
3769 				}
3770 				putchar(')');
3771 			}
3772 			putchar('\n');
3773 			lib++;
3774 		}
3775 	}
3776 }
3777 
3778 #undef Elf_Lib
3779 
3780 static uint8_t *
3781 dump_unknown_tag(uint64_t tag, uint8_t *p)
3782 {
3783 	uint64_t val;
3784 
3785 	/*
3786 	 * According to ARM EABI: For tags > 32, even numbered tags have
3787 	 * a ULEB128 param and odd numbered ones have NUL-terminated
3788 	 * string param. This rule probably also applies for tags <= 32
3789 	 * if the object arch is not ARM.
3790 	 */
3791 
3792 	printf("  Tag_unknown_%ju: ", (uintmax_t) tag);
3793 
3794 	if (tag & 1) {
3795 		printf("%s\n", (char *) p);
3796 		p += strlen((char *) p) + 1;
3797 	} else {
3798 		val = _decode_uleb128(&p);
3799 		printf("%ju\n", (uintmax_t) val);
3800 	}
3801 
3802 	return (p);
3803 }
3804 
3805 static uint8_t *
3806 dump_compatibility_tag(uint8_t *p)
3807 {
3808 	uint64_t val;
3809 
3810 	val = _decode_uleb128(&p);
3811 	printf("flag = %ju, vendor = %s\n", val, p);
3812 	p += strlen((char *) p) + 1;
3813 
3814 	return (p);
3815 }
3816 
3817 static void
3818 dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe)
3819 {
3820 	uint64_t tag, val;
3821 	size_t i;
3822 	int found, desc;
3823 
3824 	(void) re;
3825 
3826 	while (p < pe) {
3827 		tag = _decode_uleb128(&p);
3828 		found = desc = 0;
3829 		for (i = 0; i < sizeof(aeabi_tags) / sizeof(aeabi_tags[0]);
3830 		     i++) {
3831 			if (tag == aeabi_tags[i].tag) {
3832 				found = 1;
3833 				printf("  %s: ", aeabi_tags[i].s_tag);
3834 				if (aeabi_tags[i].get_desc) {
3835 					desc = 1;
3836 					val = _decode_uleb128(&p);
3837 					printf("%s\n",
3838 					    aeabi_tags[i].get_desc(val));
3839 				}
3840 				break;
3841 			}
3842 			if (tag < aeabi_tags[i].tag)
3843 				break;
3844 		}
3845 		if (!found) {
3846 			p = dump_unknown_tag(tag, p);
3847 			continue;
3848 		}
3849 		if (desc)
3850 			continue;
3851 
3852 		switch (tag) {
3853 		case 4:		/* Tag_CPU_raw_name */
3854 		case 5:		/* Tag_CPU_name */
3855 		case 67:	/* Tag_conformance */
3856 			printf("%s\n", (char *) p);
3857 			p += strlen((char *) p) + 1;
3858 			break;
3859 		case 32:	/* Tag_compatibility */
3860 			p = dump_compatibility_tag(p);
3861 			break;
3862 		case 64:	/* Tag_nodefaults */
3863 			/* ignored, written as 0. */
3864 			(void) _decode_uleb128(&p);
3865 			printf("True\n");
3866 			break;
3867 		case 65:	/* Tag_also_compatible_with */
3868 			val = _decode_uleb128(&p);
3869 			/* Must be Tag_CPU_arch */
3870 			if (val != 6) {
3871 				printf("unknown\n");
3872 				break;
3873 			}
3874 			val = _decode_uleb128(&p);
3875 			printf("%s\n", aeabi_cpu_arch(val));
3876 			/* Skip NUL terminator. */
3877 			p++;
3878 			break;
3879 		default:
3880 			putchar('\n');
3881 			break;
3882 		}
3883 	}
3884 }
3885 
3886 #ifndef	Tag_GNU_MIPS_ABI_FP
3887 #define	Tag_GNU_MIPS_ABI_FP	4
3888 #endif
3889 
3890 static void
3891 dump_mips_attributes(struct readelf *re, uint8_t *p, uint8_t *pe)
3892 {
3893 	uint64_t tag, val;
3894 
3895 	(void) re;
3896 
3897 	while (p < pe) {
3898 		tag = _decode_uleb128(&p);
3899 		switch (tag) {
3900 		case Tag_GNU_MIPS_ABI_FP:
3901 			val = _decode_uleb128(&p);
3902 			printf("  Tag_GNU_MIPS_ABI_FP: %s\n", mips_abi_fp(val));
3903 			break;
3904 		case 32:	/* Tag_compatibility */
3905 			p = dump_compatibility_tag(p);
3906 			break;
3907 		default:
3908 			p = dump_unknown_tag(tag, p);
3909 			break;
3910 		}
3911 	}
3912 }
3913 
3914 #ifndef Tag_GNU_Power_ABI_FP
3915 #define	Tag_GNU_Power_ABI_FP	4
3916 #endif
3917 
3918 #ifndef Tag_GNU_Power_ABI_Vector
3919 #define	Tag_GNU_Power_ABI_Vector	8
3920 #endif
3921 
3922 static void
3923 dump_ppc_attributes(uint8_t *p, uint8_t *pe)
3924 {
3925 	uint64_t tag, val;
3926 
3927 	while (p < pe) {
3928 		tag = _decode_uleb128(&p);
3929 		switch (tag) {
3930 		case Tag_GNU_Power_ABI_FP:
3931 			val = _decode_uleb128(&p);
3932 			printf("  Tag_GNU_Power_ABI_FP: %s\n", ppc_abi_fp(val));
3933 			break;
3934 		case Tag_GNU_Power_ABI_Vector:
3935 			val = _decode_uleb128(&p);
3936 			printf("  Tag_GNU_Power_ABI_Vector: %s\n",
3937 			    ppc_abi_vector(val));
3938 			break;
3939 		case 32:	/* Tag_compatibility */
3940 			p = dump_compatibility_tag(p);
3941 			break;
3942 		default:
3943 			p = dump_unknown_tag(tag, p);
3944 			break;
3945 		}
3946 	}
3947 }
3948 
3949 static void
3950 dump_attributes(struct readelf *re)
3951 {
3952 	struct section *s;
3953 	Elf_Data *d;
3954 	uint8_t *p, *sp;
3955 	size_t len, seclen, nlen, sublen;
3956 	uint64_t val;
3957 	int tag, i, elferr;
3958 
3959 	for (i = 0; (size_t) i < re->shnum; i++) {
3960 		s = &re->sl[i];
3961 		if (s->type != SHT_GNU_ATTRIBUTES &&
3962 		    (re->ehdr.e_machine != EM_ARM || s->type != SHT_LOPROC + 3))
3963 			continue;
3964 		(void) elf_errno();
3965 		if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
3966 			elferr = elf_errno();
3967 			if (elferr != 0)
3968 				warnx("elf_rawdata failed: %s",
3969 				    elf_errmsg(elferr));
3970 			continue;
3971 		}
3972 		if (d->d_size <= 0)
3973 			continue;
3974 		p = d->d_buf;
3975 		if (*p != 'A') {
3976 			printf("Unknown Attribute Section Format: %c\n",
3977 			    (char) *p);
3978 			continue;
3979 		}
3980 		len = d->d_size - 1;
3981 		p++;
3982 		while (len > 0) {
3983 			seclen = re->dw_decode(&p, 4);
3984 			if (seclen > len) {
3985 				warnx("invalid attribute section length");
3986 				break;
3987 			}
3988 			len -= seclen;
3989 			printf("Attribute Section: %s\n", (char *) p);
3990 			nlen = strlen((char *) p) + 1;
3991 			p += nlen;
3992 			seclen -= nlen + 4;
3993 			while (seclen > 0) {
3994 				sp = p;
3995 				tag = *p++;
3996 				sublen = re->dw_decode(&p, 4);
3997 				if (sublen > seclen) {
3998 					warnx("invalid attribute sub-section"
3999 					    " length");
4000 					break;
4001 				}
4002 				seclen -= sublen;
4003 				printf("%s", top_tag(tag));
4004 				if (tag == 2 || tag == 3) {
4005 					putchar(':');
4006 					for (;;) {
4007 						val = _decode_uleb128(&p);
4008 						if (val == 0)
4009 							break;
4010 						printf(" %ju", (uintmax_t) val);
4011 					}
4012 				}
4013 				putchar('\n');
4014 				if (re->ehdr.e_machine == EM_ARM &&
4015 				    s->type == SHT_LOPROC + 3)
4016 					dump_arm_attributes(re, p, sp + sublen);
4017 				else if (re->ehdr.e_machine == EM_MIPS ||
4018 				    re->ehdr.e_machine == EM_MIPS_RS3_LE)
4019 					dump_mips_attributes(re, p,
4020 					    sp + sublen);
4021 				else if (re->ehdr.e_machine == EM_PPC)
4022 					dump_ppc_attributes(p, sp + sublen);
4023 				p = sp + sublen;
4024 			}
4025 		}
4026 	}
4027 }
4028 
4029 static void
4030 dump_mips_specific_info(struct readelf *re)
4031 {
4032 	struct section *s;
4033 	int i, options_found;
4034 
4035 	options_found = 0;
4036 	s = NULL;
4037 	for (i = 0; (size_t) i < re->shnum; i++) {
4038 		s = &re->sl[i];
4039 		if (s->name != NULL && (!strcmp(s->name, ".MIPS.options") ||
4040 		    (s->type == SHT_MIPS_OPTIONS))) {
4041 			dump_mips_options(re, s);
4042 			options_found = 1;
4043 		}
4044 	}
4045 
4046 	/*
4047 	 * According to SGI mips64 spec, .reginfo should be ignored if
4048 	 * .MIPS.options section is present.
4049 	 */
4050 	if (!options_found) {
4051 		for (i = 0; (size_t) i < re->shnum; i++) {
4052 			s = &re->sl[i];
4053 			if (s->name != NULL && (!strcmp(s->name, ".reginfo") ||
4054 			    (s->type == SHT_MIPS_REGINFO)))
4055 				dump_mips_reginfo(re, s);
4056 		}
4057 	}
4058 }
4059 
4060 static void
4061 dump_mips_reginfo(struct readelf *re, struct section *s)
4062 {
4063 	Elf_Data *d;
4064 	int elferr;
4065 
4066 	(void) elf_errno();
4067 	if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
4068 		elferr = elf_errno();
4069 		if (elferr != 0)
4070 			warnx("elf_rawdata failed: %s",
4071 			    elf_errmsg(elferr));
4072 		return;
4073 	}
4074 	if (d->d_size <= 0)
4075 		return;
4076 
4077 	printf("\nSection '%s' contains %ju entries:\n", s->name,
4078 	    s->sz / s->entsize);
4079 	dump_mips_odk_reginfo(re, d->d_buf, d->d_size);
4080 }
4081 
4082 static void
4083 dump_mips_options(struct readelf *re, struct section *s)
4084 {
4085 	Elf_Data *d;
4086 	uint32_t info;
4087 	uint16_t sndx;
4088 	uint8_t *p, *pe;
4089 	uint8_t kind, size;
4090 	int elferr;
4091 
4092 	(void) elf_errno();
4093 	if ((d = elf_rawdata(s->scn, NULL)) == NULL) {
4094 		elferr = elf_errno();
4095 		if (elferr != 0)
4096 			warnx("elf_rawdata failed: %s",
4097 			    elf_errmsg(elferr));
4098 		return;
4099 	}
4100 	if (d->d_size == 0)
4101 		return;
4102 
4103 	printf("\nSection %s contains:\n", s->name);
4104 	p = d->d_buf;
4105 	pe = p + d->d_size;
4106 	while (p < pe) {
4107 		kind = re->dw_decode(&p, 1);
4108 		size = re->dw_decode(&p, 1);
4109 		sndx = re->dw_decode(&p, 2);
4110 		info = re->dw_decode(&p, 4);
4111 		switch (kind) {
4112 		case ODK_REGINFO:
4113 			dump_mips_odk_reginfo(re, p, size - 8);
4114 			break;
4115 		case ODK_EXCEPTIONS:
4116 			printf(" EXCEPTIONS FPU_MIN: %#x\n",
4117 			    info & OEX_FPU_MIN);
4118 			printf("%11.11s FPU_MAX: %#x\n", "",
4119 			    info & OEX_FPU_MAX);
4120 			dump_mips_option_flags("", mips_exceptions_option,
4121 			    info);
4122 			break;
4123 		case ODK_PAD:
4124 			printf(" %-10.10s section: %ju\n", "OPAD",
4125 			    (uintmax_t) sndx);
4126 			dump_mips_option_flags("", mips_pad_option, info);
4127 			break;
4128 		case ODK_HWPATCH:
4129 			dump_mips_option_flags("HWPATCH", mips_hwpatch_option,
4130 			    info);
4131 			break;
4132 		case ODK_HWAND:
4133 			dump_mips_option_flags("HWAND", mips_hwa_option, info);
4134 			break;
4135 		case ODK_HWOR:
4136 			dump_mips_option_flags("HWOR", mips_hwo_option, info);
4137 			break;
4138 		case ODK_FILL:
4139 			printf(" %-10.10s %#jx\n", "FILL", (uintmax_t) info);
4140 			break;
4141 		case ODK_TAGS:
4142 			printf(" %-10.10s\n", "TAGS");
4143 			break;
4144 		case ODK_GP_GROUP:
4145 			printf(" %-10.10s GP group number: %#x\n", "GP_GROUP",
4146 			    info & 0xFFFF);
4147 			if (info & 0x10000)
4148 				printf(" %-10.10s GP group is "
4149 				    "self-contained\n", "");
4150 			break;
4151 		case ODK_IDENT:
4152 			printf(" %-10.10s default GP group number: %#x\n",
4153 			    "IDENT", info & 0xFFFF);
4154 			if (info & 0x10000)
4155 				printf(" %-10.10s default GP group is "
4156 				    "self-contained\n", "");
4157 			break;
4158 		case ODK_PAGESIZE:
4159 			printf(" %-10.10s\n", "PAGESIZE");
4160 			break;
4161 		default:
4162 			break;
4163 		}
4164 		p += size - 8;
4165 	}
4166 }
4167 
4168 static void
4169 dump_mips_option_flags(const char *name, struct mips_option *opt, uint64_t info)
4170 {
4171 	int first;
4172 
4173 	first = 1;
4174 	for (; opt->desc != NULL; opt++) {
4175 		if (info & opt->flag) {
4176 			printf(" %-10.10s %s\n", first ? name : "",
4177 			    opt->desc);
4178 			first = 0;
4179 		}
4180 	}
4181 }
4182 
4183 static void
4184 dump_mips_odk_reginfo(struct readelf *re, uint8_t *p, size_t sz)
4185 {
4186 	uint32_t ri_gprmask;
4187 	uint32_t ri_cprmask[4];
4188 	uint64_t ri_gp_value;
4189 	uint8_t *pe;
4190 	int i;
4191 
4192 	pe = p + sz;
4193 	while (p < pe) {
4194 		ri_gprmask = re->dw_decode(&p, 4);
4195 		/* Skip ri_pad padding field for mips64. */
4196 		if (re->ec == ELFCLASS64)
4197 			re->dw_decode(&p, 4);
4198 		for (i = 0; i < 4; i++)
4199 			ri_cprmask[i] = re->dw_decode(&p, 4);
4200 		if (re->ec == ELFCLASS32)
4201 			ri_gp_value = re->dw_decode(&p, 4);
4202 		else
4203 			ri_gp_value = re->dw_decode(&p, 8);
4204 		printf(" %s    ", option_kind(ODK_REGINFO));
4205 		printf("ri_gprmask:    0x%08jx\n", (uintmax_t) ri_gprmask);
4206 		for (i = 0; i < 4; i++)
4207 			printf("%11.11s ri_cprmask[%d]: 0x%08jx\n", "", i,
4208 			    (uintmax_t) ri_cprmask[i]);
4209 		printf("%12.12s", "");
4210 		printf("ri_gp_value:   %#jx\n", (uintmax_t) ri_gp_value);
4211 	}
4212 }
4213 
4214 static void
4215 dump_arch_specific_info(struct readelf *re)
4216 {
4217 
4218 	dump_liblist(re);
4219 	dump_attributes(re);
4220 
4221 	switch (re->ehdr.e_machine) {
4222 	case EM_MIPS:
4223 	case EM_MIPS_RS3_LE:
4224 		dump_mips_specific_info(re);
4225 	default:
4226 		break;
4227 	}
4228 }
4229 
4230 static void
4231 dump_dwarf_line(struct readelf *re)
4232 {
4233 	struct section *s;
4234 	Dwarf_Die die;
4235 	Dwarf_Error de;
4236 	Dwarf_Half tag, version, pointer_size;
4237 	Dwarf_Unsigned offset, endoff, length, hdrlen, dirndx, mtime, fsize;
4238 	Dwarf_Small minlen, defstmt, lrange, opbase, oplen;
4239 	Elf_Data *d;
4240 	char *pn;
4241 	uint64_t address, file, line, column, isa, opsize, udelta;
4242 	int64_t sdelta;
4243 	uint8_t *p, *pe;
4244 	int8_t lbase;
4245 	int is_stmt, basic_block, end_sequence;
4246 	int prologue_end, epilogue_begin;
4247 	int i, dwarf_size, elferr, ret;
4248 
4249 	printf("\nDump of debug contents of section .debug_line:\n");
4250 
4251 	s = NULL;
4252 	for (i = 0; (size_t) i < re->shnum; i++) {
4253 		s = &re->sl[i];
4254 		if (s->name != NULL && !strcmp(s->name, ".debug_line"))
4255 			break;
4256 	}
4257 	if ((size_t) i >= re->shnum)
4258 		return;
4259 
4260 	(void) elf_errno();
4261 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
4262 		elferr = elf_errno();
4263 		if (elferr != 0)
4264 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
4265 		return;
4266 	}
4267 	if (d->d_size <= 0)
4268 		return;
4269 
4270 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
4271 	    NULL, &de)) ==  DW_DLV_OK) {
4272 		die = NULL;
4273 		while (dwarf_siblingof(re->dbg, die, &die, &de) == DW_DLV_OK) {
4274 			if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
4275 				warnx("dwarf_tag failed: %s",
4276 				    dwarf_errmsg(de));
4277 				return;
4278 			}
4279 			/* XXX: What about DW_TAG_partial_unit? */
4280 			if (tag == DW_TAG_compile_unit)
4281 				break;
4282 		}
4283 		if (die == NULL) {
4284 			warnx("could not find DW_TAG_compile_unit die");
4285 			return;
4286 		}
4287 		if (dwarf_attrval_unsigned(die, DW_AT_stmt_list, &offset,
4288 		    &de) != DW_DLV_OK)
4289 			continue;
4290 
4291 		length = re->dw_read(d, &offset, 4);
4292 		if (length == 0xffffffff) {
4293 			dwarf_size = 8;
4294 			length = re->dw_read(d, &offset, 8);
4295 		} else
4296 			dwarf_size = 4;
4297 
4298 		if (length > d->d_size - offset) {
4299 			warnx("invalid .dwarf_line section");
4300 			continue;
4301 		}
4302 
4303 		endoff = offset + length;
4304 		version = re->dw_read(d, &offset, 2);
4305 		hdrlen = re->dw_read(d, &offset, dwarf_size);
4306 		minlen = re->dw_read(d, &offset, 1);
4307 		defstmt = re->dw_read(d, &offset, 1);
4308 		lbase = re->dw_read(d, &offset, 1);
4309 		lrange = re->dw_read(d, &offset, 1);
4310 		opbase = re->dw_read(d, &offset, 1);
4311 
4312 		printf("\n");
4313 		printf("  Length:\t\t\t%ju\n", (uintmax_t) length);
4314 		printf("  DWARF version:\t\t%u\n", version);
4315 		printf("  Prologue Length:\t\t%ju\n", (uintmax_t) hdrlen);
4316 		printf("  Minimum Instruction Length:\t%u\n", minlen);
4317 		printf("  Initial value of 'is_stmt':\t%u\n", defstmt);
4318 		printf("  Line Base:\t\t\t%d\n", lbase);
4319 		printf("  Line Range:\t\t\t%u\n", lrange);
4320 		printf("  Opcode Base:\t\t\t%u\n", opbase);
4321 		(void) dwarf_get_address_size(re->dbg, &pointer_size, &de);
4322 		printf("  (Pointer size:\t\t%u)\n", pointer_size);
4323 
4324 		printf("\n");
4325 		printf(" Opcodes:\n");
4326 		for (i = 1; i < opbase; i++) {
4327 			oplen = re->dw_read(d, &offset, 1);
4328 			printf("  Opcode %d has %u args\n", i, oplen);
4329 		}
4330 
4331 		printf("\n");
4332 		printf(" The Directory Table:\n");
4333 		p = (uint8_t *) d->d_buf + offset;
4334 		while (*p != '\0') {
4335 			printf("  %s\n", (char *) p);
4336 			p += strlen((char *) p) + 1;
4337 		}
4338 
4339 		p++;
4340 		printf("\n");
4341 		printf(" The File Name Table:\n");
4342 		printf("  Entry\tDir\tTime\tSize\tName\n");
4343 		i = 0;
4344 		while (*p != '\0') {
4345 			i++;
4346 			pn = (char *) p;
4347 			p += strlen(pn) + 1;
4348 			dirndx = _decode_uleb128(&p);
4349 			mtime = _decode_uleb128(&p);
4350 			fsize = _decode_uleb128(&p);
4351 			printf("  %d\t%ju\t%ju\t%ju\t%s\n", i,
4352 			    (uintmax_t) dirndx, (uintmax_t) mtime,
4353 			    (uintmax_t) fsize, pn);
4354 		}
4355 
4356 #define	RESET_REGISTERS						\
4357 	do {							\
4358 		address	       = 0;				\
4359 		file	       = 1;				\
4360 		line	       = 1;				\
4361 		column	       = 0;				\
4362 		is_stmt	       = defstmt;			\
4363 		basic_block    = 0;				\
4364 		end_sequence   = 0;				\
4365 		prologue_end   = 0;				\
4366 		epilogue_begin = 0;				\
4367 	} while(0)
4368 
4369 #define	LINE(x) (lbase + (((x) - opbase) % lrange))
4370 #define	ADDRESS(x) ((((x) - opbase) / lrange) * minlen)
4371 
4372 		p++;
4373 		pe = (uint8_t *) d->d_buf + endoff;
4374 		printf("\n");
4375 		printf(" Line Number Statements:\n");
4376 
4377 		RESET_REGISTERS;
4378 
4379 		while (p < pe) {
4380 
4381 			if (*p == 0) {
4382 				/*
4383 				 * Extended Opcodes.
4384 				 */
4385 				p++;
4386 				opsize = _decode_uleb128(&p);
4387 				printf("  Extended opcode %u: ", *p);
4388 				switch (*p) {
4389 				case DW_LNE_end_sequence:
4390 					p++;
4391 					end_sequence = 1;
4392 					RESET_REGISTERS;
4393 					printf("End of Sequence\n");
4394 					break;
4395 				case DW_LNE_set_address:
4396 					p++;
4397 					address = re->dw_decode(&p,
4398 					    pointer_size);
4399 					printf("set Address to %#jx\n",
4400 					    (uintmax_t) address);
4401 					break;
4402 				case DW_LNE_define_file:
4403 					p++;
4404 					pn = (char *) p;
4405 					p += strlen(pn) + 1;
4406 					dirndx = _decode_uleb128(&p);
4407 					mtime = _decode_uleb128(&p);
4408 					fsize = _decode_uleb128(&p);
4409 					printf("define new file: %s\n", pn);
4410 					break;
4411 				default:
4412 					/* Unrecognized extened opcodes. */
4413 					p += opsize;
4414 					printf("unknown opcode\n");
4415 				}
4416 			} else if (*p > 0 && *p < opbase) {
4417 				/*
4418 				 * Standard Opcodes.
4419 				 */
4420 				switch(*p++) {
4421 				case DW_LNS_copy:
4422 					basic_block = 0;
4423 					prologue_end = 0;
4424 					epilogue_begin = 0;
4425 					printf("  Copy\n");
4426 					break;
4427 				case DW_LNS_advance_pc:
4428 					udelta = _decode_uleb128(&p) *
4429 					    minlen;
4430 					address += udelta;
4431 					printf("  Advance PC by %ju to %#jx\n",
4432 					    (uintmax_t) udelta,
4433 					    (uintmax_t) address);
4434 					break;
4435 				case DW_LNS_advance_line:
4436 					sdelta = _decode_sleb128(&p);
4437 					line += sdelta;
4438 					printf("  Advance Line by %jd to %ju\n",
4439 					    (intmax_t) sdelta,
4440 					    (uintmax_t) line);
4441 					break;
4442 				case DW_LNS_set_file:
4443 					file = _decode_uleb128(&p);
4444 					printf("  Set File to %ju\n",
4445 					    (uintmax_t) file);
4446 					break;
4447 				case DW_LNS_set_column:
4448 					column = _decode_uleb128(&p);
4449 					printf("  Set Column to %ju\n",
4450 					    (uintmax_t) column);
4451 					break;
4452 				case DW_LNS_negate_stmt:
4453 					is_stmt = !is_stmt;
4454 					printf("  Set is_stmt to %d\n", is_stmt);
4455 					break;
4456 				case DW_LNS_set_basic_block:
4457 					basic_block = 1;
4458 					printf("  Set basic block flag\n");
4459 					break;
4460 				case DW_LNS_const_add_pc:
4461 					address += ADDRESS(255);
4462 					printf("  Advance PC by constant %ju"
4463 					    " to %#jx\n",
4464 					    (uintmax_t) ADDRESS(255),
4465 					    (uintmax_t) address);
4466 					break;
4467 				case DW_LNS_fixed_advance_pc:
4468 					udelta = re->dw_decode(&p, 2);
4469 					address += udelta;
4470 					printf("  Advance PC by fixed value "
4471 					    "%ju to %#jx\n",
4472 					    (uintmax_t) udelta,
4473 					    (uintmax_t) address);
4474 					break;
4475 				case DW_LNS_set_prologue_end:
4476 					prologue_end = 1;
4477 					printf("  Set prologue end flag\n");
4478 					break;
4479 				case DW_LNS_set_epilogue_begin:
4480 					epilogue_begin = 1;
4481 					printf("  Set epilogue begin flag\n");
4482 					break;
4483 				case DW_LNS_set_isa:
4484 					isa = _decode_uleb128(&p);
4485 					printf("  Set isa to %ju\n", isa);
4486 					break;
4487 				default:
4488 					/* Unrecognized extended opcodes. */
4489 					printf("  Unknown extended opcode %u\n",
4490 					    *(p - 1));
4491 					break;
4492 				}
4493 
4494 			} else {
4495 				/*
4496 				 * Special Opcodes.
4497 				 */
4498 				line += LINE(*p);
4499 				address += ADDRESS(*p);
4500 				basic_block = 0;
4501 				prologue_end = 0;
4502 				epilogue_begin = 0;
4503 				printf("  Special opcode %u: advance Address "
4504 				    "by %ju to %#jx and Line by %jd to %ju\n",
4505 				    *p - opbase, (uintmax_t) ADDRESS(*p),
4506 				    (uintmax_t) address, (intmax_t) LINE(*p),
4507 				    (uintmax_t) line);
4508 				p++;
4509 			}
4510 
4511 
4512 		}
4513 	}
4514 	if (ret == DW_DLV_ERROR)
4515 		warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
4516 
4517 #undef	RESET_REGISTERS
4518 #undef	LINE
4519 #undef	ADDRESS
4520 }
4521 
4522 static void
4523 dump_dwarf_line_decoded(struct readelf *re)
4524 {
4525 	Dwarf_Die die;
4526 	Dwarf_Line *linebuf, ln;
4527 	Dwarf_Addr lineaddr;
4528 	Dwarf_Signed linecount, srccount;
4529 	Dwarf_Unsigned lineno, fn;
4530 	Dwarf_Error de;
4531 	const char *dir, *file;
4532 	char **srcfiles;
4533 	int i, ret;
4534 
4535 	printf("Decoded dump of debug contents of section .debug_line:\n\n");
4536 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
4537 	    NULL, &de)) == DW_DLV_OK) {
4538 		if (dwarf_siblingof(re->dbg, NULL, &die, &de) != DW_DLV_OK)
4539 			continue;
4540 		if (dwarf_attrval_string(die, DW_AT_name, &file, &de) !=
4541 		    DW_DLV_OK)
4542 			file = NULL;
4543 		if (dwarf_attrval_string(die, DW_AT_comp_dir, &dir, &de) !=
4544 		    DW_DLV_OK)
4545 			dir = NULL;
4546 		printf("CU: ");
4547 		if (dir && file)
4548 			printf("%s/", dir);
4549 		if (file)
4550 			printf("%s", file);
4551 		putchar('\n');
4552 		printf("%-37s %11s   %s\n", "Filename", "Line Number",
4553 		    "Starting Address");
4554 		if (dwarf_srclines(die, &linebuf, &linecount, &de) != DW_DLV_OK)
4555 			continue;
4556 		if (dwarf_srcfiles(die, &srcfiles, &srccount, &de) != DW_DLV_OK)
4557 			continue;
4558 		for (i = 0; i < linecount; i++) {
4559 			ln = linebuf[i];
4560 			if (dwarf_line_srcfileno(ln, &fn, &de) != DW_DLV_OK)
4561 				continue;
4562 			if (dwarf_lineno(ln, &lineno, &de) != DW_DLV_OK)
4563 				continue;
4564 			if (dwarf_lineaddr(ln, &lineaddr, &de) != DW_DLV_OK)
4565 				continue;
4566 			printf("%-37s %11ju %#18jx\n",
4567 			    basename(srcfiles[fn - 1]), (uintmax_t) lineno,
4568 			    (uintmax_t) lineaddr);
4569 		}
4570 		putchar('\n');
4571 	}
4572 }
4573 
4574 static void
4575 dump_dwarf_die(struct readelf *re, Dwarf_Die die, int level)
4576 {
4577 	Dwarf_Attribute *attr_list;
4578 	Dwarf_Die ret_die;
4579 	Dwarf_Off dieoff, cuoff, culen;
4580 	Dwarf_Unsigned ate, v_udata;
4581 	Dwarf_Signed attr_count, v_sdata;
4582 	Dwarf_Off v_off;
4583 	Dwarf_Addr v_addr;
4584 	Dwarf_Half tag, attr, form;
4585 	Dwarf_Block *v_block;
4586 	Dwarf_Bool v_bool;
4587 	Dwarf_Error de;
4588 	const char *tag_str, *attr_str, *ate_str;
4589 	char *v_str;
4590 	uint8_t *b;
4591 	int i, j, abc, ret;
4592 
4593 	if (dwarf_dieoffset(die, &dieoff, &de) != DW_DLV_OK) {
4594 		warnx("dwarf_dieoffset failed: %s", dwarf_errmsg(de));
4595 		goto cont_search;
4596 	}
4597 
4598 	printf("<%d><%jx>: ", level, (uintmax_t) dieoff);
4599 
4600 	if (dwarf_die_CU_offset_range(die, &cuoff, &culen, &de) != DW_DLV_OK) {
4601 		warnx("dwarf_die_CU_offset_range failed: %s",
4602 		      dwarf_errmsg(de));
4603 		cuoff = 0;
4604 	}
4605 
4606 	abc = dwarf_die_abbrev_code(die);
4607 	if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
4608 		warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
4609 		goto cont_search;
4610 	}
4611 	if (dwarf_get_TAG_name(tag, &tag_str) != DW_DLV_OK) {
4612 		warnx("dwarf_get_TAG_name failed");
4613 		goto cont_search;
4614 	}
4615 
4616 	printf("Abbrev Number: %d (%s)\n", abc, tag_str);
4617 
4618 	if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
4619 	    DW_DLV_OK) {
4620 		if (ret == DW_DLV_ERROR)
4621 			warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
4622 		goto cont_search;
4623 	}
4624 
4625 	for (i = 0; i < attr_count; i++) {
4626 		if (dwarf_whatform(attr_list[i], &form, &de) != DW_DLV_OK) {
4627 			warnx("dwarf_whatform failed: %s", dwarf_errmsg(de));
4628 			continue;
4629 		}
4630 		if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
4631 			warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
4632 			continue;
4633 		}
4634 		if (dwarf_get_AT_name(attr, &attr_str) != DW_DLV_OK) {
4635 			warnx("dwarf_get_AT_name failed");
4636 			continue;
4637 		}
4638 		printf("     %-18s: ", attr_str);
4639 		switch (form) {
4640 		case DW_FORM_ref_addr:
4641 			if (dwarf_global_formref(attr_list[i], &v_off, &de) !=
4642 			    DW_DLV_OK) {
4643 				warnx("dwarf_global_formref failed: %s",
4644 				    dwarf_errmsg(de));
4645 				continue;
4646 			}
4647 			printf("<%jx>", (uintmax_t) v_off);
4648 			break;
4649 
4650 		case DW_FORM_ref1:
4651 		case DW_FORM_ref2:
4652 		case DW_FORM_ref4:
4653 		case DW_FORM_ref8:
4654 		case DW_FORM_ref_udata:
4655 			if (dwarf_formref(attr_list[i], &v_off, &de) !=
4656 			    DW_DLV_OK) {
4657 				warnx("dwarf_formref failed: %s",
4658 				    dwarf_errmsg(de));
4659 				continue;
4660 			}
4661 			v_off += cuoff;
4662 			printf("<%jx>", (uintmax_t) v_off);
4663 			break;
4664 
4665 		case DW_FORM_addr:
4666 			if (dwarf_formaddr(attr_list[i], &v_addr, &de) !=
4667 			    DW_DLV_OK) {
4668 				warnx("dwarf_formaddr failed: %s",
4669 				    dwarf_errmsg(de));
4670 				continue;
4671 			}
4672 			printf("%#jx", (uintmax_t) v_addr);
4673 			break;
4674 
4675 		case DW_FORM_data1:
4676 		case DW_FORM_data2:
4677 		case DW_FORM_data4:
4678 		case DW_FORM_data8:
4679 		case DW_FORM_udata:
4680 			if (dwarf_formudata(attr_list[i], &v_udata, &de) !=
4681 			    DW_DLV_OK) {
4682 				warnx("dwarf_formudata failed: %s",
4683 				    dwarf_errmsg(de));
4684 				continue;
4685 			}
4686 			printf("%ju", (uintmax_t) v_udata);
4687 			break;
4688 
4689 		case DW_FORM_sdata:
4690 			if (dwarf_formsdata(attr_list[i], &v_sdata, &de) !=
4691 			    DW_DLV_OK) {
4692 				warnx("dwarf_formudata failed: %s",
4693 				    dwarf_errmsg(de));
4694 				continue;
4695 			}
4696 			printf("%jd", (intmax_t) v_sdata);
4697 			break;
4698 
4699 		case DW_FORM_flag:
4700 			if (dwarf_formflag(attr_list[i], &v_bool, &de) !=
4701 			    DW_DLV_OK) {
4702 				warnx("dwarf_formflag failed: %s",
4703 				    dwarf_errmsg(de));
4704 				continue;
4705 			}
4706 			printf("%jd", (intmax_t) v_bool);
4707 			break;
4708 
4709 		case DW_FORM_string:
4710 		case DW_FORM_strp:
4711 			if (dwarf_formstring(attr_list[i], &v_str, &de) !=
4712 			    DW_DLV_OK) {
4713 				warnx("dwarf_formstring failed: %s",
4714 				    dwarf_errmsg(de));
4715 				continue;
4716 			}
4717 			if (form == DW_FORM_string)
4718 				printf("%s", v_str);
4719 			else
4720 				printf("(indirect string) %s", v_str);
4721 			break;
4722 
4723 		case DW_FORM_block:
4724 		case DW_FORM_block1:
4725 		case DW_FORM_block2:
4726 		case DW_FORM_block4:
4727 			if (dwarf_formblock(attr_list[i], &v_block, &de) !=
4728 			    DW_DLV_OK) {
4729 				warnx("dwarf_formblock failed: %s",
4730 				    dwarf_errmsg(de));
4731 				continue;
4732 			}
4733 			printf("%ju byte block:", v_block->bl_len);
4734 			b = v_block->bl_data;
4735 			for (j = 0; (Dwarf_Unsigned) j < v_block->bl_len; j++)
4736 				printf(" %x", b[j]);
4737 			break;
4738 		}
4739 		switch (attr) {
4740 		case DW_AT_encoding:
4741 			if (dwarf_attrval_unsigned(die, attr, &ate, &de) !=
4742 			    DW_DLV_OK)
4743 				break;
4744 			if (dwarf_get_ATE_name(ate, &ate_str) != DW_DLV_OK)
4745 				break;
4746 			printf("\t(%s)", &ate_str[strlen("DW_ATE_")]);
4747 			break;
4748 		default:
4749 			break;
4750 		}
4751 		putchar('\n');
4752 	}
4753 
4754 
4755 cont_search:
4756 	/* Search children. */
4757 	ret = dwarf_child(die, &ret_die, &de);
4758 	if (ret == DW_DLV_ERROR)
4759 		warnx("dwarf_child: %s", dwarf_errmsg(de));
4760 	else if (ret == DW_DLV_OK)
4761 		dump_dwarf_die(re, ret_die, level + 1);
4762 
4763 	/* Search sibling. */
4764 	ret = dwarf_siblingof(re->dbg, die, &ret_die, &de);
4765 	if (ret == DW_DLV_ERROR)
4766 		warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
4767 	else if (ret == DW_DLV_OK)
4768 		dump_dwarf_die(re, ret_die, level);
4769 
4770 	dwarf_dealloc(re->dbg, die, DW_DLA_DIE);
4771 }
4772 
4773 static void
4774 dump_dwarf_info(struct readelf *re)
4775 {
4776 	struct section *s;
4777 	Dwarf_Die die;
4778 	Dwarf_Error de;
4779 	Dwarf_Half tag, version, pointer_size;
4780 	Dwarf_Off cu_offset, cu_length;
4781 	Dwarf_Off aboff;
4782 	Elf_Data *d;
4783 	int i, elferr, ret;
4784 
4785 	printf("\nDump of debug contents of section .debug_info:\n");
4786 
4787 	s = NULL;
4788 	for (i = 0; (size_t) i < re->shnum; i++) {
4789 		s = &re->sl[i];
4790 		if (s->name != NULL && !strcmp(s->name, ".debug_info"))
4791 			break;
4792 	}
4793 	if ((size_t) i >= re->shnum)
4794 		return;
4795 
4796 	(void) elf_errno();
4797 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
4798 		elferr = elf_errno();
4799 		if (elferr != 0)
4800 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
4801 		return;
4802 	}
4803 	if (d->d_size <= 0)
4804 		return;
4805 
4806 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, &version, &aboff,
4807 	    &pointer_size, NULL, &de)) == DW_DLV_OK) {
4808 		die = NULL;
4809 		while (dwarf_siblingof(re->dbg, die, &die, &de) == DW_DLV_OK) {
4810 			if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
4811 				warnx("dwarf_tag failed: %s",
4812 				    dwarf_errmsg(de));
4813 				return;
4814 			}
4815 			/* XXX: What about DW_TAG_partial_unit? */
4816 			if (tag == DW_TAG_compile_unit)
4817 				break;
4818 		}
4819 		if (die == NULL) {
4820 			warnx("could not find DW_TAG_compile_unit die");
4821 			return;
4822 		}
4823 
4824 		if (dwarf_die_CU_offset_range(die, &cu_offset, &cu_length,
4825 		    &de) != DW_DLV_OK) {
4826 			warnx("dwarf_die_CU_offset failed: %s",
4827 			    dwarf_errmsg(de));
4828 			continue;
4829 		}
4830 
4831 		printf("  Compilation Unit @ %jd:\n", (intmax_t) cu_offset);
4832 		printf("    Length:\t\t%jd\n", (intmax_t) cu_length);
4833 		printf("    Version:\t\t%u\n", version);
4834 		printf("    Abbrev Offset:\t%ju\n", (uintmax_t) aboff);
4835 		printf("    Pointer Size:\t%u\n", pointer_size);
4836 
4837 		dump_dwarf_die(re, die, 0);
4838 	}
4839 	if (ret == DW_DLV_ERROR)
4840 		warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
4841 }
4842 
4843 static void
4844 dump_dwarf_abbrev(struct readelf *re)
4845 {
4846 	Dwarf_Abbrev ab;
4847 	Dwarf_Off aboff, atoff;
4848 	Dwarf_Unsigned length, attr_count;
4849 	Dwarf_Signed flag, form;
4850 	Dwarf_Half tag, attr;
4851 	Dwarf_Error de;
4852 	const char *tag_str, *attr_str, *form_str;
4853 	int i, j, ret;
4854 
4855 	printf("\nContents of section .debug_abbrev:\n\n");
4856 
4857 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, &aboff,
4858 	    NULL, NULL, &de)) ==  DW_DLV_OK) {
4859 		printf("  Number TAG\n");
4860 		i = 0;
4861 		while ((ret = dwarf_get_abbrev(re->dbg, aboff, &ab, &length,
4862 		    &attr_count, &de)) == DW_DLV_OK) {
4863 			if (length == 1) {
4864 				dwarf_dealloc(re->dbg, ab, DW_DLA_ABBREV);
4865 				break;
4866 			}
4867 			aboff += length;
4868 			printf("%4d", ++i);
4869 			if (dwarf_get_abbrev_tag(ab, &tag, &de) != DW_DLV_OK) {
4870 				warnx("dwarf_get_abbrev_tag failed: %s",
4871 				    dwarf_errmsg(de));
4872 				goto next_abbrev;
4873 			}
4874 			if (dwarf_get_TAG_name(tag, &tag_str) != DW_DLV_OK) {
4875 				warnx("dwarf_get_TAG_name failed");
4876 				goto next_abbrev;
4877 			}
4878 			if (dwarf_get_abbrev_children_flag(ab, &flag, &de) !=
4879 			    DW_DLV_OK) {
4880 				warnx("dwarf_get_abbrev_children_flag failed:"
4881 				    " %s", dwarf_errmsg(de));
4882 				goto next_abbrev;
4883 			}
4884 			printf("      %s    %s\n", tag_str,
4885 			    flag ? "[has children]" : "[no children]");
4886 			for (j = 0; (Dwarf_Unsigned) j < attr_count; j++) {
4887 				if (dwarf_get_abbrev_entry(ab, (Dwarf_Signed) j,
4888 				    &attr, &form, &atoff, &de) != DW_DLV_OK) {
4889 					warnx("dwarf_get_abbrev_entry failed:"
4890 					    " %s", dwarf_errmsg(de));
4891 					continue;
4892 				}
4893 				if (dwarf_get_AT_name(attr, &attr_str) !=
4894 				    DW_DLV_OK) {
4895 					warnx("dwarf_get_AT_name failed");
4896 					continue;
4897 				}
4898 				if (dwarf_get_FORM_name(form, &form_str) !=
4899 				    DW_DLV_OK) {
4900 					warnx("dwarf_get_FORM_name failed");
4901 					continue;
4902 				}
4903 				printf("    %-18s %s\n", attr_str, form_str);
4904 			}
4905 		next_abbrev:
4906 			dwarf_dealloc(re->dbg, ab, DW_DLA_ABBREV);
4907 		}
4908 		if (ret != DW_DLV_OK)
4909 			warnx("dwarf_get_abbrev: %s", dwarf_errmsg(de));
4910 	}
4911 	if (ret == DW_DLV_ERROR)
4912 		warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
4913 }
4914 
4915 static void
4916 dump_dwarf_pubnames(struct readelf *re)
4917 {
4918 	struct section *s;
4919 	Dwarf_Off die_off;
4920 	Dwarf_Unsigned offset, length, nt_cu_offset, nt_cu_length;
4921 	Dwarf_Signed cnt;
4922 	Dwarf_Global *globs;
4923 	Dwarf_Half nt_version;
4924 	Dwarf_Error de;
4925 	Elf_Data *d;
4926 	char *glob_name;
4927 	int i, dwarf_size, elferr;
4928 
4929 	printf("\nContents of the .debug_pubnames section:\n");
4930 
4931 	s = NULL;
4932 	for (i = 0; (size_t) i < re->shnum; i++) {
4933 		s = &re->sl[i];
4934 		if (s->name != NULL && !strcmp(s->name, ".debug_pubnames"))
4935 			break;
4936 	}
4937 	if ((size_t) i >= re->shnum)
4938 		return;
4939 
4940 	(void) elf_errno();
4941 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
4942 		elferr = elf_errno();
4943 		if (elferr != 0)
4944 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
4945 		return;
4946 	}
4947 	if (d->d_size <= 0)
4948 		return;
4949 
4950 	/* Read in .debug_pubnames section table header. */
4951 	offset = 0;
4952 	length = re->dw_read(d, &offset, 4);
4953 	if (length == 0xffffffff) {
4954 		dwarf_size = 8;
4955 		length = re->dw_read(d, &offset, 8);
4956 	} else
4957 		dwarf_size = 4;
4958 
4959 	if (length > d->d_size - offset) {
4960 		warnx("invalid .dwarf_pubnames section");
4961 		return;
4962 	}
4963 
4964 	nt_version = re->dw_read(d, &offset, 2);
4965 	nt_cu_offset = re->dw_read(d, &offset, dwarf_size);
4966 	nt_cu_length = re->dw_read(d, &offset, dwarf_size);
4967 	printf("  Length:\t\t\t\t%ju\n", (uintmax_t) length);
4968 	printf("  Version:\t\t\t\t%u\n", nt_version);
4969 	printf("  Offset into .debug_info section:\t%ju\n",
4970 	    (uintmax_t) nt_cu_offset);
4971 	printf("  Size of area in .debug_info section:\t%ju\n",
4972 	    (uintmax_t) nt_cu_length);
4973 
4974 	if (dwarf_get_globals(re->dbg, &globs, &cnt, &de) != DW_DLV_OK) {
4975 		warnx("dwarf_get_globals failed: %s", dwarf_errmsg(de));
4976 		return;
4977 	}
4978 
4979 	printf("\n    Offset      Name\n");
4980 	for (i = 0; i < cnt; i++) {
4981 		if (dwarf_globname(globs[i], &glob_name, &de) != DW_DLV_OK) {
4982 			warnx("dwarf_globname failed: %s", dwarf_errmsg(de));
4983 			continue;
4984 		}
4985 		if (dwarf_global_die_offset(globs[i], &die_off, &de) !=
4986 		    DW_DLV_OK) {
4987 			warnx("dwarf_global_die_offset failed: %s",
4988 			    dwarf_errmsg(de));
4989 			continue;
4990 		}
4991 		printf("    %-11ju %s\n", (uintmax_t) die_off, glob_name);
4992 	}
4993 }
4994 
4995 static void
4996 dump_dwarf_aranges(struct readelf *re)
4997 {
4998 	struct section *s;
4999 	Dwarf_Arange *aranges;
5000 	Dwarf_Addr start;
5001 	Dwarf_Unsigned offset, length, as_cu_offset;
5002 	Dwarf_Off die_off;
5003 	Dwarf_Signed cnt;
5004 	Dwarf_Half as_version, as_addrsz, as_segsz;
5005 	Dwarf_Error de;
5006 	Elf_Data *d;
5007 	int i, dwarf_size, elferr;
5008 
5009 	printf("\nContents of section .debug_aranges:\n");
5010 
5011 	s = NULL;
5012 	for (i = 0; (size_t) i < re->shnum; i++) {
5013 		s = &re->sl[i];
5014 		if (s->name != NULL && !strcmp(s->name, ".debug_aranges"))
5015 			break;
5016 	}
5017 	if ((size_t) i >= re->shnum)
5018 		return;
5019 
5020 	(void) elf_errno();
5021 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
5022 		elferr = elf_errno();
5023 		if (elferr != 0)
5024 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
5025 		return;
5026 	}
5027 	if (d->d_size <= 0)
5028 		return;
5029 
5030 	/* Read in the .debug_aranges section table header. */
5031 	offset = 0;
5032 	length = re->dw_read(d, &offset, 4);
5033 	if (length == 0xffffffff) {
5034 		dwarf_size = 8;
5035 		length = re->dw_read(d, &offset, 8);
5036 	} else
5037 		dwarf_size = 4;
5038 
5039 	if (length > d->d_size - offset) {
5040 		warnx("invalid .dwarf_aranges section");
5041 		return;
5042 	}
5043 
5044 	as_version = re->dw_read(d, &offset, 2);
5045 	as_cu_offset = re->dw_read(d, &offset, dwarf_size);
5046 	as_addrsz = re->dw_read(d, &offset, 1);
5047 	as_segsz = re->dw_read(d, &offset, 1);
5048 
5049 	printf("  Length:\t\t\t%ju\n", (uintmax_t) length);
5050 	printf("  Version:\t\t\t%u\n", as_version);
5051 	printf("  Offset into .debug_info:\t%ju\n", (uintmax_t) as_cu_offset);
5052 	printf("  Pointer Size:\t\t\t%u\n", as_addrsz);
5053 	printf("  Segment Size:\t\t\t%u\n", as_segsz);
5054 
5055 	if (dwarf_get_aranges(re->dbg, &aranges, &cnt, &de) != DW_DLV_OK) {
5056 		warnx("dwarf_get_aranges failed: %s", dwarf_errmsg(de));
5057 		return;
5058 	}
5059 
5060 	printf("\n    Address  Length\n");
5061 	for (i = 0; i < cnt; i++) {
5062 		if (dwarf_get_arange_info(aranges[i], &start, &length,
5063 		    &die_off, &de) != DW_DLV_OK) {
5064 			warnx("dwarf_get_arange_info failed: %s",
5065 			    dwarf_errmsg(de));
5066 			continue;
5067 		}
5068 		printf("    %08jx %ju\n", (uintmax_t) start,
5069 		    (uintmax_t) length);
5070 	}
5071 }
5072 
5073 static void
5074 dump_dwarf_ranges_foreach(struct readelf *re, Dwarf_Die die, Dwarf_Addr base)
5075 {
5076 	Dwarf_Attribute *attr_list;
5077 	Dwarf_Ranges *ranges;
5078 	Dwarf_Die ret_die;
5079 	Dwarf_Error de;
5080 	Dwarf_Addr base0;
5081 	Dwarf_Half attr;
5082 	Dwarf_Signed attr_count, cnt;
5083 	Dwarf_Unsigned off, bytecnt;
5084 	int i, j, ret;
5085 
5086 	if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
5087 	    DW_DLV_OK) {
5088 		if (ret == DW_DLV_ERROR)
5089 			warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
5090 		goto cont_search;
5091 	}
5092 
5093 	for (i = 0; i < attr_count; i++) {
5094 		if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
5095 			warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
5096 			continue;
5097 		}
5098 		if (attr != DW_AT_ranges)
5099 			continue;
5100 		if (dwarf_formudata(attr_list[i], &off, &de) != DW_DLV_OK) {
5101 			warnx("dwarf_formudata failed: %s", dwarf_errmsg(de));
5102 			continue;
5103 		}
5104 		if (dwarf_get_ranges(re->dbg, (Dwarf_Off) off, &ranges, &cnt,
5105 		    &bytecnt, &de) != DW_DLV_OK)
5106 			continue;
5107 		base0 = base;
5108 		for (j = 0; j < cnt; j++) {
5109 			printf("    %08jx ", (uintmax_t) off);
5110 			if (ranges[j].dwr_type == DW_RANGES_END) {
5111 				printf("%s\n", "<End of list>");
5112 				continue;
5113 			} else if (ranges[j].dwr_type ==
5114 			    DW_RANGES_ADDRESS_SELECTION) {
5115 				base0 = ranges[j].dwr_addr2;
5116 				continue;
5117 			}
5118 			if (re->ec == ELFCLASS32)
5119 				printf("%08jx %08jx\n",
5120 				    ranges[j].dwr_addr1 + base0,
5121 				    ranges[j].dwr_addr2 + base0);
5122 			else
5123 				printf("%016jx %016jx\n",
5124 				    ranges[j].dwr_addr1 + base0,
5125 				    ranges[j].dwr_addr2 + base0);
5126 		}
5127 	}
5128 
5129 cont_search:
5130 	/* Search children. */
5131 	ret = dwarf_child(die, &ret_die, &de);
5132 	if (ret == DW_DLV_ERROR)
5133 		warnx("dwarf_child: %s", dwarf_errmsg(de));
5134 	else if (ret == DW_DLV_OK)
5135 		dump_dwarf_ranges_foreach(re, ret_die, base);
5136 
5137 	/* Search sibling. */
5138 	ret = dwarf_siblingof(re->dbg, die, &ret_die, &de);
5139 	if (ret == DW_DLV_ERROR)
5140 		warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
5141 	else if (ret == DW_DLV_OK)
5142 		dump_dwarf_ranges_foreach(re, ret_die, base);
5143 }
5144 
5145 static void
5146 dump_dwarf_ranges(struct readelf *re)
5147 {
5148 	Dwarf_Ranges *ranges;
5149 	Dwarf_Die die;
5150 	Dwarf_Signed cnt;
5151 	Dwarf_Unsigned bytecnt;
5152 	Dwarf_Half tag;
5153 	Dwarf_Error de;
5154 	Dwarf_Unsigned lowpc;
5155 	int ret;
5156 
5157 	if (dwarf_get_ranges(re->dbg, 0, &ranges, &cnt, &bytecnt, &de) !=
5158 	    DW_DLV_OK)
5159 		return;
5160 
5161 	printf("Contents of the .debug_ranges section:\n\n");
5162 	if (re->ec == ELFCLASS32)
5163 		printf("    %-8s %-8s %s\n", "Offset", "Begin", "End");
5164 	else
5165 		printf("    %-8s %-16s %s\n", "Offset", "Begin", "End");
5166 
5167 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
5168 	    NULL, &de)) == DW_DLV_OK) {
5169 		die = NULL;
5170 		if (dwarf_siblingof(re->dbg, die, &die, &de) != DW_DLV_OK)
5171 			continue;
5172 		if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
5173 			warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
5174 			continue;
5175 		}
5176 		/* XXX: What about DW_TAG_partial_unit? */
5177 		lowpc = 0;
5178 		if (tag == DW_TAG_compile_unit) {
5179 			if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lowpc,
5180 			    &de) != DW_DLV_OK)
5181 				lowpc = 0;
5182 		}
5183 
5184 		dump_dwarf_ranges_foreach(re, die, (Dwarf_Addr) lowpc);
5185 	}
5186 	putchar('\n');
5187 }
5188 
5189 static void
5190 dump_dwarf_macinfo(struct readelf *re)
5191 {
5192 	Dwarf_Unsigned offset;
5193 	Dwarf_Signed cnt;
5194 	Dwarf_Macro_Details *md;
5195 	Dwarf_Error de;
5196 	const char *mi_str;
5197 	int i;
5198 
5199 #define	_MAX_MACINFO_ENTRY	65535
5200 
5201 	printf("\nContents of section .debug_macinfo:\n\n");
5202 
5203 	offset = 0;
5204 	while (dwarf_get_macro_details(re->dbg, offset, _MAX_MACINFO_ENTRY,
5205 	    &cnt, &md, &de) == DW_DLV_OK) {
5206 		for (i = 0; i < cnt; i++) {
5207 			offset = md[i].dmd_offset + 1;
5208 			if (md[i].dmd_type == 0)
5209 				break;
5210 			if (dwarf_get_MACINFO_name(md[i].dmd_type, &mi_str) !=
5211 			    DW_DLV_OK) {
5212 				warnx("dwarf_get_MACINFO_name failed: %s",
5213 				    dwarf_errmsg(de));
5214 				continue;
5215 			}
5216 			printf(" %s", mi_str);
5217 			switch (md[i].dmd_type) {
5218 			case DW_MACINFO_define:
5219 			case DW_MACINFO_undef:
5220 				printf(" - lineno : %jd macro : %s\n",
5221 				    (intmax_t) md[i].dmd_lineno,
5222 				    md[i].dmd_macro);
5223 				break;
5224 			case DW_MACINFO_start_file:
5225 				printf(" - lineno : %jd filenum : %jd\n",
5226 				    (intmax_t) md[i].dmd_lineno,
5227 				    (intmax_t) md[i].dmd_fileindex);
5228 				break;
5229 			default:
5230 				putchar('\n');
5231 				break;
5232 			}
5233 		}
5234 	}
5235 
5236 #undef	_MAX_MACINFO_ENTRY
5237 }
5238 
5239 static void
5240 dump_dwarf_frame_inst(Dwarf_Cie cie, uint8_t *insts, Dwarf_Unsigned len,
5241     Dwarf_Unsigned caf, Dwarf_Signed daf, Dwarf_Addr pc, Dwarf_Debug dbg)
5242 {
5243 	Dwarf_Frame_Op *oplist;
5244 	Dwarf_Signed opcnt, delta;
5245 	Dwarf_Small op;
5246 	Dwarf_Error de;
5247 	const char *op_str;
5248 	int i;
5249 
5250 	if (dwarf_expand_frame_instructions(cie, insts, len, &oplist,
5251 	    &opcnt, &de) != DW_DLV_OK) {
5252 		warnx("dwarf_expand_frame_instructions failed: %s",
5253 		    dwarf_errmsg(de));
5254 		return;
5255 	}
5256 
5257 	for (i = 0; i < opcnt; i++) {
5258 		if (oplist[i].fp_base_op != 0)
5259 			op = oplist[i].fp_base_op << 6;
5260 		else
5261 			op = oplist[i].fp_extended_op;
5262 		if (dwarf_get_CFA_name(op, &op_str) != DW_DLV_OK) {
5263 			warnx("dwarf_get_CFA_name failed: %s",
5264 			    dwarf_errmsg(de));
5265 			continue;
5266 		}
5267 		printf("  %s", op_str);
5268 		switch (op) {
5269 		case DW_CFA_advance_loc:
5270 			delta = oplist[i].fp_offset * caf;
5271 			pc += delta;
5272 			printf(": %ju to %08jx", (uintmax_t) delta,
5273 			    (uintmax_t) pc);
5274 			break;
5275 		case DW_CFA_offset:
5276 		case DW_CFA_offset_extended:
5277 		case DW_CFA_offset_extended_sf:
5278 			delta = oplist[i].fp_offset * daf;
5279 			printf(": r%u at cfa%+jd", oplist[i].fp_register,
5280 			    (intmax_t) delta);
5281 			break;
5282 		case DW_CFA_restore:
5283 			printf(": r%u", oplist[i].fp_register);
5284 			break;
5285 		case DW_CFA_set_loc:
5286 			pc = oplist[i].fp_offset;
5287 			printf(": to %08jx", (uintmax_t) pc);
5288 			break;
5289 		case DW_CFA_advance_loc1:
5290 		case DW_CFA_advance_loc2:
5291 		case DW_CFA_advance_loc4:
5292 			pc += oplist[i].fp_offset;
5293 			printf(": %jd to %08jx", (intmax_t) oplist[i].fp_offset,
5294 			    (uintmax_t) pc);
5295 			break;
5296 		case DW_CFA_def_cfa:
5297 			printf(": r%u ofs %ju", oplist[i].fp_register,
5298 			    (uintmax_t) oplist[i].fp_offset);
5299 			break;
5300 		case DW_CFA_def_cfa_sf:
5301 			printf(": r%u ofs %jd", oplist[i].fp_register,
5302 			    (intmax_t) (oplist[i].fp_offset * daf));
5303 			break;
5304 		case DW_CFA_def_cfa_register:
5305 			printf(": r%u", oplist[i].fp_register);
5306 			break;
5307 		case DW_CFA_def_cfa_offset:
5308 			printf(": %ju", (uintmax_t) oplist[i].fp_offset);
5309 			break;
5310 		case DW_CFA_def_cfa_offset_sf:
5311 			printf(": %jd", (intmax_t) (oplist[i].fp_offset * daf));
5312 			break;
5313 		default:
5314 			break;
5315 		}
5316 		putchar('\n');
5317 	}
5318 
5319 	dwarf_dealloc(dbg, oplist, DW_DLA_FRAME_BLOCK);
5320 }
5321 
5322 static char *
5323 get_regoff_str(Dwarf_Half reg, Dwarf_Addr off)
5324 {
5325 	static char rs[16];
5326 
5327 	if (reg == DW_FRAME_UNDEFINED_VAL || reg == DW_FRAME_REG_INITIAL_VALUE)
5328 		snprintf(rs, sizeof(rs), "%c", 'u');
5329 	else if (reg == DW_FRAME_CFA_COL)
5330 		snprintf(rs, sizeof(rs), "c%+jd", (intmax_t) off);
5331 	else
5332 		snprintf(rs, sizeof(rs), "r%u%+jd", reg, (intmax_t) off);
5333 
5334 	return (rs);
5335 }
5336 
5337 static int
5338 dump_dwarf_frame_regtable(Dwarf_Fde fde, Dwarf_Addr pc, Dwarf_Unsigned func_len,
5339     Dwarf_Half cie_ra)
5340 {
5341 	Dwarf_Regtable rt;
5342 	Dwarf_Addr row_pc, end_pc, pre_pc, cur_pc;
5343 	Dwarf_Error de;
5344 	char rn[16];
5345 	char *vec;
5346 	int i;
5347 
5348 #define BIT_SET(v, n) (v[(n)>>3] |= 1U << ((n) & 7))
5349 #define BIT_CLR(v, n) (v[(n)>>3] &= ~(1U << ((n) & 7)))
5350 #define BIT_ISSET(v, n) (v[(n)>>3] & (1U << ((n) & 7)))
5351 #define	RT(x) rt.rules[(x)]
5352 
5353 	vec = calloc((DW_REG_TABLE_SIZE + 7) / 8, 1);
5354 	if (vec == NULL)
5355 		err(EXIT_FAILURE, "calloc failed");
5356 
5357 	pre_pc = ~((Dwarf_Addr) 0);
5358 	cur_pc = pc;
5359 	end_pc = pc + func_len;
5360 	for (; cur_pc < end_pc; cur_pc++) {
5361 		if (dwarf_get_fde_info_for_all_regs(fde, cur_pc, &rt, &row_pc,
5362 		    &de) != DW_DLV_OK) {
5363 			warnx("dwarf_get_fde_info_for_all_regs failed: %s\n",
5364 			    dwarf_errmsg(de));
5365 			return (-1);
5366 		}
5367 		if (row_pc == pre_pc)
5368 			continue;
5369 		pre_pc = row_pc;
5370 		for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
5371 			if (rt.rules[i].dw_regnum != DW_FRAME_REG_INITIAL_VALUE)
5372 				BIT_SET(vec, i);
5373 		}
5374 	}
5375 
5376 	printf("   LOC   CFA      ");
5377 	for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
5378 		if (BIT_ISSET(vec, i)) {
5379 			if ((Dwarf_Half) i == cie_ra)
5380 				printf("ra   ");
5381 			else {
5382 				snprintf(rn, sizeof(rn), "r%d", i);
5383 				printf("%-5s", rn);
5384 			}
5385 		}
5386 	}
5387 	putchar('\n');
5388 
5389 	pre_pc = ~((Dwarf_Addr) 0);
5390 	cur_pc = pc;
5391 	end_pc = pc + func_len;
5392 	for (; cur_pc < end_pc; cur_pc++) {
5393 		if (dwarf_get_fde_info_for_all_regs(fde, cur_pc, &rt, &row_pc,
5394 		    &de) != DW_DLV_OK) {
5395 			warnx("dwarf_get_fde_info_for_all_regs failed: %s\n",
5396 			    dwarf_errmsg(de));
5397 			return (-1);
5398 		}
5399 		if (row_pc == pre_pc)
5400 			continue;
5401 		pre_pc = row_pc;
5402 		printf("%08jx ", (uintmax_t) row_pc);
5403 		printf("%-8s ", get_regoff_str(RT(0).dw_regnum,
5404 		    RT(0).dw_offset));
5405 		for (i = 1; i < DW_REG_TABLE_SIZE; i++) {
5406 			if (BIT_ISSET(vec, i)) {
5407 				printf("%-5s", get_regoff_str(RT(i).dw_regnum,
5408 				    RT(i).dw_offset));
5409 			}
5410 		}
5411 		putchar('\n');
5412 	}
5413 
5414 	free(vec);
5415 
5416 	return (0);
5417 
5418 #undef	BIT_SET
5419 #undef	BIT_CLR
5420 #undef	BIT_ISSET
5421 #undef	RT
5422 }
5423 
5424 static void
5425 dump_dwarf_frame_section(struct readelf *re, struct section *s, int alt)
5426 {
5427 	Dwarf_Cie *cie_list, cie, pre_cie;
5428 	Dwarf_Fde *fde_list, fde;
5429 	Dwarf_Off cie_offset, fde_offset;
5430 	Dwarf_Unsigned cie_length, fde_instlen;
5431 	Dwarf_Unsigned cie_caf, cie_daf, cie_instlen, func_len, fde_length;
5432 	Dwarf_Signed cie_count, fde_count, cie_index;
5433 	Dwarf_Addr low_pc;
5434 	Dwarf_Half cie_ra;
5435 	Dwarf_Small cie_version;
5436 	Dwarf_Ptr fde_addr, fde_inst, cie_inst;
5437 	char *cie_aug, c;
5438 	int i, eh_frame;
5439 	Dwarf_Error de;
5440 
5441 	printf("\nThe section %s contains:\n\n", s->name);
5442 
5443 	if (!strcmp(s->name, ".debug_frame")) {
5444 		eh_frame = 0;
5445 		if (dwarf_get_fde_list(re->dbg, &cie_list, &cie_count,
5446 		    &fde_list, &fde_count, &de) != DW_DLV_OK) {
5447 			warnx("dwarf_get_fde_list failed: %s",
5448 			    dwarf_errmsg(de));
5449 			return;
5450 		}
5451 	} else if (!strcmp(s->name, ".eh_frame")) {
5452 		eh_frame = 1;
5453 		if (dwarf_get_fde_list_eh(re->dbg, &cie_list, &cie_count,
5454 		    &fde_list, &fde_count, &de) != DW_DLV_OK) {
5455 			warnx("dwarf_get_fde_list_eh failed: %s",
5456 			    dwarf_errmsg(de));
5457 			return;
5458 		}
5459 	} else
5460 		return;
5461 
5462 	pre_cie = NULL;
5463 	for (i = 0; i < fde_count; i++) {
5464 		if (dwarf_get_fde_n(fde_list, i, &fde, &de) != DW_DLV_OK) {
5465 			warnx("dwarf_get_fde_n failed: %s", dwarf_errmsg(de));
5466 			continue;
5467 		}
5468 		if (dwarf_get_cie_of_fde(fde, &cie, &de) != DW_DLV_OK) {
5469 			warnx("dwarf_get_fde_n failed: %s", dwarf_errmsg(de));
5470 			continue;
5471 		}
5472 		if (dwarf_get_fde_range(fde, &low_pc, &func_len, &fde_addr,
5473 		    &fde_length, &cie_offset, &cie_index, &fde_offset,
5474 		    &de) != DW_DLV_OK) {
5475 			warnx("dwarf_get_fde_range failed: %s",
5476 			    dwarf_errmsg(de));
5477 			continue;
5478 		}
5479 		if (dwarf_get_fde_instr_bytes(fde, &fde_inst, &fde_instlen,
5480 		    &de) != DW_DLV_OK) {
5481 			warnx("dwarf_get_fde_instr_bytes failed: %s",
5482 			    dwarf_errmsg(de));
5483 			continue;
5484 		}
5485 		if (pre_cie == NULL || cie != pre_cie) {
5486 			pre_cie = cie;
5487 			if (dwarf_get_cie_info(cie, &cie_length, &cie_version,
5488 			    &cie_aug, &cie_caf, &cie_daf, &cie_ra,
5489 			    &cie_inst, &cie_instlen, &de) != DW_DLV_OK) {
5490 				warnx("dwarf_get_cie_info failed: %s",
5491 				    dwarf_errmsg(de));
5492 				continue;
5493 			}
5494 			printf("%08jx %08jx %8.8jx CIE",
5495 			    (uintmax_t) cie_offset,
5496 			    (uintmax_t) cie_length,
5497 			    (uintmax_t) (eh_frame ? 0 : ~0U));
5498 			if (!alt) {
5499 				putchar('\n');
5500 				printf("  Version:\t\t\t%u\n", cie_version);
5501 				printf("  Augmentation:\t\t\t\"");
5502 				while ((c = *cie_aug++) != '\0')
5503 					putchar(c);
5504 				printf("\"\n");
5505 				printf("  Code alignment factor:\t%ju\n",
5506 				    (uintmax_t) cie_caf);
5507 				printf("  Data alignment factor:\t%jd\n",
5508 				    (intmax_t) cie_daf);
5509 				printf("  Return address column:\t%ju\n",
5510 				    (uintmax_t) cie_ra);
5511 				putchar('\n');
5512 				dump_dwarf_frame_inst(cie, cie_inst,
5513 				    cie_instlen, cie_caf, cie_daf, 0,
5514 				    re->dbg);
5515 				putchar('\n');
5516 			} else {
5517 				printf(" \"");
5518 				while ((c = *cie_aug++) != '\0')
5519 					putchar(c);
5520 				putchar('"');
5521 				printf(" cf=%ju df=%jd ra=%ju\n",
5522 				    (uintmax_t) cie_caf,
5523 				    (uintmax_t) cie_daf,
5524 				    (uintmax_t) cie_ra);
5525 				dump_dwarf_frame_regtable(fde, low_pc, 1,
5526 				    cie_ra);
5527 				putchar('\n');
5528 			}
5529 		}
5530 		printf("%08jx %08jx %08jx FDE cie=%08jx pc=%08jx..%08jx\n",
5531 		    (uintmax_t) fde_offset, (uintmax_t) fde_length,
5532 		    (uintmax_t) cie_offset,
5533 		    (uintmax_t) (eh_frame ? fde_offset + 4 - cie_offset :
5534 			cie_offset),
5535 		    (uintmax_t) low_pc, (uintmax_t) (low_pc + func_len));
5536 		if (!alt)
5537 			dump_dwarf_frame_inst(cie, fde_inst, fde_instlen,
5538 			    cie_caf, cie_daf, low_pc, re->dbg);
5539 		else
5540 			dump_dwarf_frame_regtable(fde, low_pc, func_len,
5541 			    cie_ra);
5542 		putchar('\n');
5543 	}
5544 }
5545 
5546 static void
5547 dump_dwarf_frame(struct readelf *re, int alt)
5548 {
5549 	struct section *s;
5550 	int i;
5551 
5552 	(void) dwarf_set_frame_cfa_value(re->dbg, DW_FRAME_CFA_COL);
5553 
5554 	for (i = 0; (size_t) i < re->shnum; i++) {
5555 		s = &re->sl[i];
5556 		if (s->name != NULL && (!strcmp(s->name, ".debug_frame") ||
5557 		    !strcmp(s->name, ".eh_frame")))
5558 			dump_dwarf_frame_section(re, s, alt);
5559 	}
5560 }
5561 
5562 static void
5563 dump_dwarf_str(struct readelf *re)
5564 {
5565 	struct section *s;
5566 	Elf_Data *d;
5567 	unsigned char *p;
5568 	int elferr, end, i, j;
5569 
5570 	printf("\nContents of section .debug_str:\n");
5571 
5572 	s = NULL;
5573 	for (i = 0; (size_t) i < re->shnum; i++) {
5574 		s = &re->sl[i];
5575 		if (s->name != NULL && !strcmp(s->name, ".debug_str"))
5576 			break;
5577 	}
5578 	if ((size_t) i >= re->shnum)
5579 		return;
5580 
5581 	(void) elf_errno();
5582 	if ((d = elf_getdata(s->scn, NULL)) == NULL) {
5583 		elferr = elf_errno();
5584 		if (elferr != 0)
5585 			warnx("elf_getdata failed: %s", elf_errmsg(-1));
5586 		return;
5587 	}
5588 	if (d->d_size <= 0)
5589 		return;
5590 
5591 	for (i = 0, p = d->d_buf; (size_t) i < d->d_size; i += 16) {
5592 		printf("  0x%08x", (unsigned int) i);
5593 		if ((size_t) i + 16 > d->d_size)
5594 			end = d->d_size;
5595 		else
5596 			end = i + 16;
5597 		for (j = i; j < i + 16; j++) {
5598 			if ((j - i) % 4 == 0)
5599 				putchar(' ');
5600 			if (j >= end) {
5601 				printf("  ");
5602 				continue;
5603 			}
5604 			printf("%02x", (uint8_t) p[j]);
5605 		}
5606 		putchar(' ');
5607 		for (j = i; j < end; j++) {
5608 			if (isprint(p[j]))
5609 				putchar(p[j]);
5610 			else if (p[j] == 0)
5611 				putchar('.');
5612 			else
5613 				putchar(' ');
5614 		}
5615 		putchar('\n');
5616 	}
5617 }
5618 
5619 struct loc_at {
5620 	Dwarf_Attribute la_at;
5621 	Dwarf_Unsigned la_off;
5622 	Dwarf_Unsigned la_lowpc;
5623 	TAILQ_ENTRY(loc_at) la_next;
5624 };
5625 
5626 static TAILQ_HEAD(, loc_at) lalist = TAILQ_HEAD_INITIALIZER(lalist);
5627 
5628 static void
5629 search_loclist_at(struct readelf *re, Dwarf_Die die, Dwarf_Unsigned lowpc)
5630 {
5631 	Dwarf_Attribute *attr_list;
5632 	Dwarf_Die ret_die;
5633 	Dwarf_Unsigned off;
5634 	Dwarf_Signed attr_count;
5635 	Dwarf_Half attr, form;
5636 	Dwarf_Error de;
5637 	struct loc_at *la, *nla;
5638 	int i, ret;
5639 
5640 	if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) !=
5641 	    DW_DLV_OK) {
5642 		if (ret == DW_DLV_ERROR)
5643 			warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de));
5644 		goto cont_search;
5645 	}
5646 	for (i = 0; i < attr_count; i++) {
5647 		if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) {
5648 			warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de));
5649 			continue;
5650 		}
5651 		if (attr != DW_AT_location &&
5652 		    attr != DW_AT_string_length &&
5653 		    attr != DW_AT_return_addr &&
5654 		    attr != DW_AT_data_member_location &&
5655 		    attr != DW_AT_frame_base &&
5656 		    attr != DW_AT_segment &&
5657 		    attr != DW_AT_static_link &&
5658 		    attr != DW_AT_use_location &&
5659 		    attr != DW_AT_vtable_elem_location)
5660 			continue;
5661 		if (dwarf_whatform(attr_list[i], &form, &de) != DW_DLV_OK) {
5662 			warnx("dwarf_whatform failed: %s", dwarf_errmsg(de));
5663 			continue;
5664 		}
5665 		if (form != DW_FORM_data4 && form != DW_FORM_data8)
5666 			continue;
5667 		if (dwarf_formudata(attr_list[i], &off, &de) != DW_DLV_OK) {
5668 			warnx("dwarf_formudata failed: %s", dwarf_errmsg(de));
5669 			continue;
5670 		}
5671 		TAILQ_FOREACH(la, &lalist, la_next) {
5672 			if (off == la->la_off)
5673 				break;
5674 			if (off < la->la_off) {
5675 				if ((nla = malloc(sizeof(*nla))) == NULL)
5676 					err(EXIT_FAILURE, "malloc failed");
5677 				nla->la_at = attr_list[i];
5678 				nla->la_off = off;
5679 				nla->la_lowpc = lowpc;
5680 				TAILQ_INSERT_BEFORE(la, nla, la_next);
5681 				break;
5682 			}
5683 		}
5684 		if (la == NULL) {
5685 			if ((nla = malloc(sizeof(*nla))) == NULL)
5686 				err(EXIT_FAILURE, "malloc failed");
5687 			nla->la_at = attr_list[i];
5688 			nla->la_off = off;
5689 			nla->la_lowpc = lowpc;
5690 			TAILQ_INSERT_TAIL(&lalist, nla, la_next);
5691 		}
5692 	}
5693 
5694 cont_search:
5695 	/* Search children. */
5696 	ret = dwarf_child(die, &ret_die, &de);
5697 	if (ret == DW_DLV_ERROR)
5698 		warnx("dwarf_child: %s", dwarf_errmsg(de));
5699 	else if (ret == DW_DLV_OK)
5700 		search_loclist_at(re, ret_die, lowpc);
5701 
5702 	/* Search sibling. */
5703 	ret = dwarf_siblingof(re->dbg, die, &ret_die, &de);
5704 	if (ret == DW_DLV_ERROR)
5705 		warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
5706 	else if (ret == DW_DLV_OK)
5707 		search_loclist_at(re, ret_die, lowpc);
5708 }
5709 
5710 static void
5711 dump_dwarf_loclist(struct readelf *re)
5712 {
5713 	Dwarf_Die die;
5714 	Dwarf_Locdesc **llbuf;
5715 	Dwarf_Unsigned lowpc;
5716 	Dwarf_Signed lcnt;
5717 	Dwarf_Half tag;
5718 	Dwarf_Error de;
5719 	Dwarf_Loc *lr;
5720 	struct loc_at *la;
5721 	const char *op_str;
5722 	int i, j, ret;
5723 
5724 	printf("\nContents of section .debug_loc:\n");
5725 
5726 	while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL,
5727 	    NULL, &de)) == DW_DLV_OK) {
5728 		die = NULL;
5729 		if (dwarf_siblingof(re->dbg, die, &die, &de) != DW_DLV_OK)
5730 			continue;
5731 		if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
5732 			warnx("dwarf_tag failed: %s", dwarf_errmsg(de));
5733 			continue;
5734 		}
5735 		/* XXX: What about DW_TAG_partial_unit? */
5736 		lowpc = 0;
5737 		if (tag == DW_TAG_compile_unit) {
5738 			if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lowpc,
5739 			    &de) != DW_DLV_OK)
5740 				lowpc = 0;
5741 		}
5742 
5743 		/* Search attributes for reference to .debug_loc section. */
5744 		search_loclist_at(re, die, lowpc);
5745 	}
5746 	if (ret == DW_DLV_ERROR)
5747 		warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de));
5748 
5749 	if (TAILQ_EMPTY(&lalist))
5750 		return;
5751 
5752 	printf("    Offset   Begin    End      Expression\n");
5753 
5754 	TAILQ_FOREACH(la, &lalist, la_next) {
5755 		if (dwarf_loclist_n(la->la_at, &llbuf, &lcnt, &de) !=
5756 		    DW_DLV_OK) {
5757 			warnx("dwarf_loclist_n failed: %s", dwarf_errmsg(de));
5758 			continue;
5759 		}
5760 		for (i = 0; i < lcnt; i++) {
5761 			printf("    %8.8jx ", la->la_off);
5762 			if (llbuf[i]->ld_lopc == 0 && llbuf[i]->ld_hipc == 0) {
5763 				printf("<End of list>\n");
5764 				continue;
5765 			}
5766 
5767 			/* TODO: handle base selection entry. */
5768 
5769 			printf("%8.8jx %8.8jx ",
5770 			    (uintmax_t) (la->la_lowpc + llbuf[i]->ld_lopc),
5771 			    (uintmax_t) (la->la_lowpc + llbuf[i]->ld_hipc));
5772 
5773 			putchar('(');
5774 			for (j = 0; (Dwarf_Half) j < llbuf[i]->ld_cents; j++) {
5775 				lr = &llbuf[i]->ld_s[j];
5776 				if (dwarf_get_OP_name(lr->lr_atom, &op_str) !=
5777 				    DW_DLV_OK) {
5778 					warnx("dwarf_get_OP_name failed: %s",
5779 					    dwarf_errmsg(de));
5780 					continue;
5781 				}
5782 
5783 				printf("%s", op_str);
5784 
5785 				switch (lr->lr_atom) {
5786 				/* Operations with no operands. */
5787 				case DW_OP_deref:
5788 				case DW_OP_reg0:
5789 				case DW_OP_reg1:
5790 				case DW_OP_reg2:
5791 				case DW_OP_reg3:
5792 				case DW_OP_reg4:
5793 				case DW_OP_reg5:
5794 				case DW_OP_reg6:
5795 				case DW_OP_reg7:
5796 				case DW_OP_reg8:
5797 				case DW_OP_reg9:
5798 				case DW_OP_reg10:
5799 				case DW_OP_reg11:
5800 				case DW_OP_reg12:
5801 				case DW_OP_reg13:
5802 				case DW_OP_reg14:
5803 				case DW_OP_reg15:
5804 				case DW_OP_reg16:
5805 				case DW_OP_reg17:
5806 				case DW_OP_reg18:
5807 				case DW_OP_reg19:
5808 				case DW_OP_reg20:
5809 				case DW_OP_reg21:
5810 				case DW_OP_reg22:
5811 				case DW_OP_reg23:
5812 				case DW_OP_reg24:
5813 				case DW_OP_reg25:
5814 				case DW_OP_reg26:
5815 				case DW_OP_reg27:
5816 				case DW_OP_reg28:
5817 				case DW_OP_reg29:
5818 				case DW_OP_reg30:
5819 				case DW_OP_reg31:
5820 				case DW_OP_lit0:
5821 				case DW_OP_lit1:
5822 				case DW_OP_lit2:
5823 				case DW_OP_lit3:
5824 				case DW_OP_lit4:
5825 				case DW_OP_lit5:
5826 				case DW_OP_lit6:
5827 				case DW_OP_lit7:
5828 				case DW_OP_lit8:
5829 				case DW_OP_lit9:
5830 				case DW_OP_lit10:
5831 				case DW_OP_lit11:
5832 				case DW_OP_lit12:
5833 				case DW_OP_lit13:
5834 				case DW_OP_lit14:
5835 				case DW_OP_lit15:
5836 				case DW_OP_lit16:
5837 				case DW_OP_lit17:
5838 				case DW_OP_lit18:
5839 				case DW_OP_lit19:
5840 				case DW_OP_lit20:
5841 				case DW_OP_lit21:
5842 				case DW_OP_lit22:
5843 				case DW_OP_lit23:
5844 				case DW_OP_lit24:
5845 				case DW_OP_lit25:
5846 				case DW_OP_lit26:
5847 				case DW_OP_lit27:
5848 				case DW_OP_lit28:
5849 				case DW_OP_lit29:
5850 				case DW_OP_lit30:
5851 				case DW_OP_lit31:
5852 				case DW_OP_dup:
5853 				case DW_OP_drop:
5854 				case DW_OP_over:
5855 				case DW_OP_swap:
5856 				case DW_OP_rot:
5857 				case DW_OP_xderef:
5858 				case DW_OP_abs:
5859 				case DW_OP_and:
5860 				case DW_OP_div:
5861 				case DW_OP_minus:
5862 				case DW_OP_mod:
5863 				case DW_OP_mul:
5864 				case DW_OP_neg:
5865 				case DW_OP_not:
5866 				case DW_OP_or:
5867 				case DW_OP_plus:
5868 				case DW_OP_shl:
5869 				case DW_OP_shr:
5870 				case DW_OP_shra:
5871 				case DW_OP_xor:
5872 				case DW_OP_eq:
5873 				case DW_OP_ge:
5874 				case DW_OP_gt:
5875 				case DW_OP_le:
5876 				case DW_OP_lt:
5877 				case DW_OP_ne:
5878 				case DW_OP_nop:
5879 					break;
5880 
5881 				case DW_OP_const1u:
5882 				case DW_OP_const1s:
5883 				case DW_OP_pick:
5884 				case DW_OP_deref_size:
5885 				case DW_OP_xderef_size:
5886 				case DW_OP_const2u:
5887 				case DW_OP_const2s:
5888 				case DW_OP_bra:
5889 				case DW_OP_skip:
5890 				case DW_OP_const4u:
5891 				case DW_OP_const4s:
5892 				case DW_OP_const8u:
5893 				case DW_OP_const8s:
5894 				case DW_OP_constu:
5895 				case DW_OP_plus_uconst:
5896 				case DW_OP_regx:
5897 				case DW_OP_piece:
5898 					printf(": %ju", (uintmax_t)
5899 					    lr->lr_number);
5900 					break;
5901 
5902 				case DW_OP_consts:
5903 				case DW_OP_breg0:
5904 				case DW_OP_breg1:
5905 				case DW_OP_breg2:
5906 				case DW_OP_breg3:
5907 				case DW_OP_breg4:
5908 				case DW_OP_breg5:
5909 				case DW_OP_breg6:
5910 				case DW_OP_breg7:
5911 				case DW_OP_breg8:
5912 				case DW_OP_breg9:
5913 				case DW_OP_breg10:
5914 				case DW_OP_breg11:
5915 				case DW_OP_breg12:
5916 				case DW_OP_breg13:
5917 				case DW_OP_breg14:
5918 				case DW_OP_breg15:
5919 				case DW_OP_breg16:
5920 				case DW_OP_breg17:
5921 				case DW_OP_breg18:
5922 				case DW_OP_breg19:
5923 				case DW_OP_breg20:
5924 				case DW_OP_breg21:
5925 				case DW_OP_breg22:
5926 				case DW_OP_breg23:
5927 				case DW_OP_breg24:
5928 				case DW_OP_breg25:
5929 				case DW_OP_breg26:
5930 				case DW_OP_breg27:
5931 				case DW_OP_breg28:
5932 				case DW_OP_breg29:
5933 				case DW_OP_breg30:
5934 				case DW_OP_breg31:
5935 				case DW_OP_fbreg:
5936 					printf(": %jd", (intmax_t)
5937 					    lr->lr_number);
5938 					break;
5939 
5940 				case DW_OP_bregx:
5941 					printf(": %ju %jd",
5942 					    (uintmax_t) lr->lr_number,
5943 					    (intmax_t) lr->lr_number2);
5944 					break;
5945 
5946 				case DW_OP_addr:
5947 					printf(": %#jx", (uintmax_t)
5948 					    lr->lr_number);
5949 					break;
5950 				}
5951 				if (j < llbuf[i]->ld_cents - 1)
5952 					printf(", ");
5953 			}
5954 			putchar(')');
5955 
5956 			if (llbuf[i]->ld_lopc == llbuf[i]->ld_hipc)
5957 				printf(" (start == end)");
5958 			putchar('\n');
5959 		}
5960 	}
5961 }
5962 
5963 /*
5964  * Retrieve a string using string table section index and the string offset.
5965  */
5966 static const char*
5967 get_string(struct readelf *re, int strtab, size_t off)
5968 {
5969 	const char *name;
5970 
5971 	if ((name = elf_strptr(re->elf, strtab, off)) == NULL)
5972 		return ("");
5973 
5974 	return (name);
5975 }
5976 
5977 /*
5978  * Retrieve the name of a symbol using the section index of the symbol
5979  * table and the index of the symbol within that table.
5980  */
5981 static const char *
5982 get_symbol_name(struct readelf *re, int symtab, int i)
5983 {
5984 	struct section	*s;
5985 	const char	*name;
5986 	GElf_Sym	 sym;
5987 	Elf_Data	*data;
5988 	int		 elferr;
5989 
5990 	s = &re->sl[symtab];
5991 	if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM)
5992 		return ("");
5993 	(void) elf_errno();
5994 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
5995 		elferr = elf_errno();
5996 		if (elferr != 0)
5997 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
5998 		return ("");
5999 	}
6000 	if (gelf_getsym(data, i, &sym) != &sym)
6001 		return ("");
6002 	/* Return section name for STT_SECTION symbol. */
6003 	if (GELF_ST_TYPE(sym.st_info) == STT_SECTION &&
6004 	    re->sl[sym.st_shndx].name != NULL)
6005 		return (re->sl[sym.st_shndx].name);
6006 	if ((name = elf_strptr(re->elf, s->link, sym.st_name)) == NULL)
6007 		return ("");
6008 
6009 	return (name);
6010 }
6011 
6012 static uint64_t
6013 get_symbol_value(struct readelf *re, int symtab, int i)
6014 {
6015 	struct section	*s;
6016 	GElf_Sym	 sym;
6017 	Elf_Data	*data;
6018 	int		 elferr;
6019 
6020 	s = &re->sl[symtab];
6021 	if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM)
6022 		return (0);
6023 	(void) elf_errno();
6024 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
6025 		elferr = elf_errno();
6026 		if (elferr != 0)
6027 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
6028 		return (0);
6029 	}
6030 	if (gelf_getsym(data, i, &sym) != &sym)
6031 		return (0);
6032 
6033 	return (sym.st_value);
6034 }
6035 
6036 static void
6037 hex_dump(struct readelf *re)
6038 {
6039 	struct section *s;
6040 	Elf_Data *d;
6041 	uint8_t *buf;
6042 	size_t sz, nbytes;
6043 	uint64_t addr;
6044 	int elferr, i, j;
6045 
6046 	for (i = 1; (size_t) i < re->shnum; i++) {
6047 		s = &re->sl[i];
6048 		if (find_dumpop(re, (size_t) i, s->name, HEX_DUMP, -1) == NULL)
6049 			continue;
6050 		(void) elf_errno();
6051 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
6052 			elferr = elf_errno();
6053 			if (elferr != 0)
6054 				warnx("elf_getdata failed: %s",
6055 				    elf_errmsg(elferr));
6056 			continue;
6057 		}
6058 		if (d->d_size <= 0 || d->d_buf == NULL) {
6059 			printf("\nSection '%s' has no data to dump.\n",
6060 			    s->name);
6061 			continue;
6062 		}
6063 		buf = d->d_buf;
6064 		sz = d->d_size;
6065 		addr = s->addr;
6066 		printf("\nHex dump of section '%s':\n", s->name);
6067 		while (sz > 0) {
6068 			printf("  0x%8.8jx ", (uintmax_t)addr);
6069 			nbytes = sz > 16? 16 : sz;
6070 			for (j = 0; j < 16; j++) {
6071 				if ((size_t)j < nbytes)
6072 					printf("%2.2x", buf[j]);
6073 				else
6074 					printf("  ");
6075 				if ((j & 3) == 3)
6076 					printf(" ");
6077 			}
6078 			for (j = 0; (size_t)j < nbytes; j++) {
6079 				if (isprint(buf[j]))
6080 					printf("%c", buf[j]);
6081 				else
6082 					printf(".");
6083 			}
6084 			printf("\n");
6085 			buf += nbytes;
6086 			addr += nbytes;
6087 			sz -= nbytes;
6088 		}
6089 	}
6090 }
6091 
6092 static void
6093 str_dump(struct readelf *re)
6094 {
6095 	struct section *s;
6096 	Elf_Data *d;
6097 	unsigned char *start, *end, *buf_end;
6098 	unsigned int len;
6099 	int i, j, elferr, found;
6100 
6101 	for (i = 1; (size_t) i < re->shnum; i++) {
6102 		s = &re->sl[i];
6103 		if (find_dumpop(re, (size_t) i, s->name, STR_DUMP, -1) == NULL)
6104 			continue;
6105 		(void) elf_errno();
6106 		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
6107 			elferr = elf_errno();
6108 			if (elferr != 0)
6109 				warnx("elf_getdata failed: %s",
6110 				    elf_errmsg(elferr));
6111 			continue;
6112 		}
6113 		if (d->d_size <= 0 || d->d_buf == NULL) {
6114 			printf("\nSection '%s' has no data to dump.\n",
6115 			    s->name);
6116 			continue;
6117 		}
6118 		buf_end = (unsigned char *) d->d_buf + d->d_size;
6119 		start = (unsigned char *) d->d_buf;
6120 		found = 0;
6121 		printf("\nString dump of section '%s':\n", s->name);
6122 		for (;;) {
6123 			while (start < buf_end && !isprint(*start))
6124 				start++;
6125 			if (start >= buf_end)
6126 				break;
6127 			end = start + 1;
6128 			while (end < buf_end && isprint(*end))
6129 				end++;
6130 			printf("  [%6lx]  ",
6131 			    (long) (start - (unsigned char *) d->d_buf));
6132 			len = end - start;
6133 			for (j = 0; (unsigned int) j < len; j++)
6134 				putchar(start[j]);
6135 			putchar('\n');
6136 			found = 1;
6137 			if (end >= buf_end)
6138 				break;
6139 			start = end + 1;
6140 		}
6141 		if (!found)
6142 			printf("  No strings found in this section.");
6143 		putchar('\n');
6144 	}
6145 }
6146 
6147 static void
6148 load_sections(struct readelf *re)
6149 {
6150 	struct section	*s;
6151 	const char	*name;
6152 	Elf_Scn		*scn;
6153 	GElf_Shdr	 sh;
6154 	size_t		 shstrndx, ndx;
6155 	int		 elferr;
6156 
6157 	/* Allocate storage for internal section list. */
6158 	if (!elf_getshnum(re->elf, &re->shnum)) {
6159 		warnx("elf_getshnum failed: %s", elf_errmsg(-1));
6160 		return;
6161 	}
6162 	if (re->sl != NULL)
6163 		free(re->sl);
6164 	if ((re->sl = calloc(re->shnum, sizeof(*re->sl))) == NULL)
6165 		err(EXIT_FAILURE, "calloc failed");
6166 
6167 	/* Get the index of .shstrtab section. */
6168 	if (!elf_getshstrndx(re->elf, &shstrndx)) {
6169 		warnx("elf_getshstrndx failed: %s", elf_errmsg(-1));
6170 		return;
6171 	}
6172 
6173 	if ((scn = elf_getscn(re->elf, 0)) == NULL) {
6174 		warnx("elf_getscn failed: %s", elf_errmsg(-1));
6175 		return;
6176 	}
6177 
6178 	(void) elf_errno();
6179 	do {
6180 		if (gelf_getshdr(scn, &sh) == NULL) {
6181 			warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
6182 			(void) elf_errno();
6183 			continue;
6184 		}
6185 		if ((name = elf_strptr(re->elf, shstrndx, sh.sh_name)) == NULL) {
6186 			(void) elf_errno();
6187 			name = "ERROR";
6188 		}
6189 		if ((ndx = elf_ndxscn(scn)) == SHN_UNDEF) {
6190 			if ((elferr = elf_errno()) != 0)
6191 				warnx("elf_ndxscn failed: %s",
6192 				    elf_errmsg(elferr));
6193 			continue;
6194 		}
6195 		if (ndx >= re->shnum) {
6196 			warnx("section index of '%s' out of range", name);
6197 			continue;
6198 		}
6199 		s = &re->sl[ndx];
6200 		s->name = name;
6201 		s->scn = scn;
6202 		s->off = sh.sh_offset;
6203 		s->sz = sh.sh_size;
6204 		s->entsize = sh.sh_entsize;
6205 		s->align = sh.sh_addralign;
6206 		s->type = sh.sh_type;
6207 		s->flags = sh.sh_flags;
6208 		s->addr = sh.sh_addr;
6209 		s->link = sh.sh_link;
6210 		s->info = sh.sh_info;
6211 	} while ((scn = elf_nextscn(re->elf, scn)) != NULL);
6212 	elferr = elf_errno();
6213 	if (elferr != 0)
6214 		warnx("elf_nextscn failed: %s", elf_errmsg(elferr));
6215 }
6216 
6217 static void
6218 unload_sections(struct readelf *re)
6219 {
6220 
6221 	if (re->sl != NULL) {
6222 		free(re->sl);
6223 		re->sl = NULL;
6224 	}
6225 	re->shnum = 0;
6226 	re->vd_s = NULL;
6227 	re->vn_s = NULL;
6228 	re->vs_s = NULL;
6229 	re->vs = NULL;
6230 	re->vs_sz = 0;
6231 	if (re->ver != NULL) {
6232 		free(re->ver);
6233 		re->ver = NULL;
6234 		re->ver_sz = 0;
6235 	}
6236 }
6237 
6238 static void
6239 dump_elf(struct readelf *re)
6240 {
6241 
6242 	/* Fetch ELF header. No need to continue if it fails. */
6243 	if (gelf_getehdr(re->elf, &re->ehdr) == NULL) {
6244 		warnx("gelf_getehdr failed: %s", elf_errmsg(-1));
6245 		return;
6246 	}
6247 	if ((re->ec = gelf_getclass(re->elf)) == ELFCLASSNONE) {
6248 		warnx("gelf_getclass failed: %s", elf_errmsg(-1));
6249 		return;
6250 	}
6251 	if (re->ehdr.e_ident[EI_DATA] == ELFDATA2MSB) {
6252 		re->dw_read = _read_msb;
6253 		re->dw_decode = _decode_msb;
6254 	} else {
6255 		re->dw_read = _read_lsb;
6256 		re->dw_decode = _decode_lsb;
6257 	}
6258 
6259 	if (re->options & ~RE_H)
6260 		load_sections(re);
6261 	if ((re->options & RE_VV) || (re->options & RE_S))
6262 		search_ver(re);
6263 	if (re->options & RE_H)
6264 		dump_ehdr(re);
6265 	if (re->options & RE_L)
6266 		dump_phdr(re);
6267 	if (re->options & RE_SS)
6268 		dump_shdr(re);
6269 	if (re->options & RE_D)
6270 		dump_dynamic(re);
6271 	if (re->options & RE_R)
6272 		dump_reloc(re);
6273 	if (re->options & RE_S)
6274 		dump_symtabs(re);
6275 	if (re->options & RE_N)
6276 		dump_notes(re);
6277 	if (re->options & RE_II)
6278 		dump_hash(re);
6279 	if (re->options & RE_X)
6280 		hex_dump(re);
6281 	if (re->options & RE_P)
6282 		str_dump(re);
6283 	if (re->options & RE_VV)
6284 		dump_ver(re);
6285 	if (re->options & RE_AA)
6286 		dump_arch_specific_info(re);
6287 	if (re->options & RE_W)
6288 		dump_dwarf(re);
6289 	if (re->options & ~RE_H)
6290 		unload_sections(re);
6291 }
6292 
6293 static void
6294 dump_dwarf(struct readelf *re)
6295 {
6296 	int error;
6297 	Dwarf_Error de;
6298 
6299 	if (dwarf_elf_init(re->elf, DW_DLC_READ, NULL, NULL, &re->dbg, &de)) {
6300 		if ((error = dwarf_errno(de)) != DW_DLE_DEBUG_INFO_NULL)
6301 			errx(EXIT_FAILURE, "dwarf_elf_init failed: %s",
6302 			    dwarf_errmsg(de));
6303 		return;
6304 	}
6305 
6306 	if (re->dop & DW_A)
6307 		dump_dwarf_abbrev(re);
6308 	if (re->dop & DW_L)
6309 		dump_dwarf_line(re);
6310 	if (re->dop & DW_LL)
6311 		dump_dwarf_line_decoded(re);
6312 	if (re->dop & DW_I)
6313 		dump_dwarf_info(re);
6314 	if (re->dop & DW_P)
6315 		dump_dwarf_pubnames(re);
6316 	if (re->dop & DW_R)
6317 		dump_dwarf_aranges(re);
6318 	if (re->dop & DW_RR)
6319 		dump_dwarf_ranges(re);
6320 	if (re->dop & DW_M)
6321 		dump_dwarf_macinfo(re);
6322 	if (re->dop & DW_F)
6323 		dump_dwarf_frame(re, 0);
6324 	else if (re->dop & DW_FF)
6325 		dump_dwarf_frame(re, 1);
6326 	if (re->dop & DW_S)
6327 		dump_dwarf_str(re);
6328 	if (re->dop & DW_O)
6329 		dump_dwarf_loclist(re);
6330 
6331 	dwarf_finish(re->dbg, &de);
6332 }
6333 
6334 static void
6335 dump_ar(struct readelf *re, int fd)
6336 {
6337 	Elf_Arsym *arsym;
6338 	Elf_Arhdr *arhdr;
6339 	Elf_Cmd cmd;
6340 	Elf *e;
6341 	size_t sz;
6342 	off_t off;
6343 	int i;
6344 
6345 	re->ar = re->elf;
6346 
6347 	if (re->options & RE_C) {
6348 		if ((arsym = elf_getarsym(re->ar, &sz)) == NULL) {
6349 			warnx("elf_getarsym() failed: %s", elf_errmsg(-1));
6350 			goto process_members;
6351 		}
6352 		printf("Index of archive %s: (%ju entries)\n", re->filename,
6353 		    (uintmax_t) sz - 1);
6354 		off = 0;
6355 		for (i = 0; (size_t) i < sz; i++) {
6356 			if (arsym[i].as_name == NULL)
6357 				break;
6358 			if (arsym[i].as_off != off) {
6359 				off = arsym[i].as_off;
6360 				if (elf_rand(re->ar, off) != off) {
6361 					warnx("elf_rand() failed: %s",
6362 					    elf_errmsg(-1));
6363 					continue;
6364 				}
6365 				if ((e = elf_begin(fd, ELF_C_READ, re->ar)) ==
6366 				    NULL) {
6367 					warnx("elf_begin() failed: %s",
6368 					    elf_errmsg(-1));
6369 					continue;
6370 				}
6371 				if ((arhdr = elf_getarhdr(e)) == NULL) {
6372 					warnx("elf_getarhdr() failed: %s",
6373 					    elf_errmsg(-1));
6374 					elf_end(e);
6375 					continue;
6376 				}
6377 				printf("Binary %s(%s) contains:\n",
6378 				    re->filename, arhdr->ar_name);
6379 			}
6380 			printf("\t%s\n", arsym[i].as_name);
6381 		}
6382 		if (elf_rand(re->ar, SARMAG) != SARMAG) {
6383 			warnx("elf_rand() failed: %s", elf_errmsg(-1));
6384 			return;
6385 		}
6386 	}
6387 
6388 process_members:
6389 
6390 	if ((re->options & ~RE_C) == 0)
6391 		return;
6392 
6393 	cmd = ELF_C_READ;
6394 	while ((re->elf = elf_begin(fd, cmd, re->ar)) != NULL) {
6395 		if ((arhdr = elf_getarhdr(re->elf)) == NULL) {
6396 			warnx("elf_getarhdr() failed: %s", elf_errmsg(-1));
6397 			goto next_member;
6398 		}
6399 		if (strcmp(arhdr->ar_name, "/") == 0 ||
6400 		    strcmp(arhdr->ar_name, "//") == 0 ||
6401 		    strcmp(arhdr->ar_name, "__.SYMDEF") == 0)
6402 			goto next_member;
6403 		printf("\nFile: %s(%s)\n", re->filename, arhdr->ar_name);
6404 		dump_elf(re);
6405 
6406 	next_member:
6407 		cmd = elf_next(re->elf);
6408 		elf_end(re->elf);
6409 	}
6410 	re->elf = re->ar;
6411 }
6412 
6413 static void
6414 dump_object(struct readelf *re)
6415 {
6416 	int fd;
6417 
6418 	if ((fd = open(re->filename, O_RDONLY)) == -1) {
6419 		warn("open %s failed", re->filename);
6420 		return;
6421 	}
6422 
6423 	if ((re->flags & DISPLAY_FILENAME) != 0)
6424 		printf("\nFile: %s\n", re->filename);
6425 
6426 	if ((re->elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
6427 		warnx("elf_begin() failed: %s", elf_errmsg(-1));
6428 		return;
6429 	}
6430 
6431 	switch (elf_kind(re->elf)) {
6432 	case ELF_K_NONE:
6433 		warnx("Not an ELF file.");
6434 		return;
6435 	case ELF_K_ELF:
6436 		dump_elf(re);
6437 		break;
6438 	case ELF_K_AR:
6439 		dump_ar(re, fd);
6440 		break;
6441 	default:
6442 		warnx("Internal: libelf returned unknown elf kind.");
6443 		return;
6444 	}
6445 
6446 	elf_end(re->elf);
6447 }
6448 
6449 static void
6450 add_dumpop(struct readelf *re, size_t si, const char *sn, int op, int t)
6451 {
6452 	struct dumpop *d;
6453 
6454 	if ((d = find_dumpop(re, si, sn, -1, t)) == NULL) {
6455 		if ((d = calloc(1, sizeof(*d))) == NULL)
6456 			err(EXIT_FAILURE, "calloc failed");
6457 		if (t == DUMP_BY_INDEX)
6458 			d->u.si = si;
6459 		else
6460 			d->u.sn = sn;
6461 		d->type = t;
6462 		d->op = op;
6463 		STAILQ_INSERT_TAIL(&re->v_dumpop, d, dumpop_list);
6464 	} else
6465 		d->op |= op;
6466 }
6467 
6468 static struct dumpop *
6469 find_dumpop(struct readelf *re, size_t si, const char *sn, int op, int t)
6470 {
6471 	struct dumpop *d;
6472 
6473 	STAILQ_FOREACH(d, &re->v_dumpop, dumpop_list) {
6474 		if ((op == -1 || op & d->op) &&
6475 		    (t == -1 || (unsigned) t == d->type)) {
6476 			if ((d->type == DUMP_BY_INDEX && d->u.si == si) ||
6477 			    (d->type == DUMP_BY_NAME && !strcmp(d->u.sn, sn)))
6478 				return (d);
6479 		}
6480 	}
6481 
6482 	return (NULL);
6483 }
6484 
6485 static struct {
6486 	const char *ln;
6487 	char sn;
6488 	int value;
6489 } dwarf_op[] = {
6490 	{"rawline", 'l', DW_L},
6491 	{"decodedline", 'L', DW_LL},
6492 	{"info", 'i', DW_I},
6493 	{"abbrev", 'a', DW_A},
6494 	{"pubnames", 'p', DW_P},
6495 	{"aranges", 'r', DW_R},
6496 	{"ranges", 'r', DW_R},
6497 	{"Ranges", 'R', DW_RR},
6498 	{"macro", 'm', DW_M},
6499 	{"frames", 'f', DW_F},
6500 	{"", 'F', DW_FF},
6501 	{"str", 's', DW_S},
6502 	{"loc", 'o', DW_O},
6503 	{NULL, 0, 0}
6504 };
6505 
6506 static void
6507 parse_dwarf_op_short(struct readelf *re, const char *op)
6508 {
6509 	int i;
6510 
6511 	if (op == NULL) {
6512 		re->dop |= DW_DEFAULT_OPTIONS;
6513 		return;
6514 	}
6515 
6516 	for (; *op != '\0'; op++) {
6517 		for (i = 0; dwarf_op[i].ln != NULL; i++) {
6518 			if (dwarf_op[i].sn == *op) {
6519 				re->dop |= dwarf_op[i].value;
6520 				break;
6521 			}
6522 		}
6523 	}
6524 }
6525 
6526 static void
6527 parse_dwarf_op_long(struct readelf *re, const char *op)
6528 {
6529 	char *p, *token, *bp;
6530 	int i;
6531 
6532 	if (op == NULL) {
6533 		re->dop |= DW_DEFAULT_OPTIONS;
6534 		return;
6535 	}
6536 
6537 	if ((p = strdup(op)) == NULL)
6538 		err(EXIT_FAILURE, "strdup failed");
6539 	bp = p;
6540 
6541 	while ((token = strsep(&p, ",")) != NULL) {
6542 		for (i = 0; dwarf_op[i].ln != NULL; i++) {
6543 			if (!strcmp(token, dwarf_op[i].ln)) {
6544 				re->dop |= dwarf_op[i].value;
6545 				break;
6546 			}
6547 		}
6548 	}
6549 
6550 	free(bp);
6551 }
6552 
6553 static uint64_t
6554 _read_lsb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read)
6555 {
6556 	uint64_t ret;
6557 	uint8_t *src;
6558 
6559 	src = (uint8_t *) d->d_buf + *offsetp;
6560 
6561 	ret = 0;
6562 	switch (bytes_to_read) {
6563 	case 8:
6564 		ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
6565 		ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
6566 	case 4:
6567 		ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
6568 	case 2:
6569 		ret |= ((uint64_t) src[1]) << 8;
6570 	case 1:
6571 		ret |= src[0];
6572 		break;
6573 	default:
6574 		return (0);
6575 	}
6576 
6577 	*offsetp += bytes_to_read;
6578 
6579 	return (ret);
6580 }
6581 
6582 static uint64_t
6583 _read_msb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read)
6584 {
6585 	uint64_t ret;
6586 	uint8_t *src;
6587 
6588 	src = (uint8_t *) d->d_buf + *offsetp;
6589 
6590 	switch (bytes_to_read) {
6591 	case 1:
6592 		ret = src[0];
6593 		break;
6594 	case 2:
6595 		ret = src[1] | ((uint64_t) src[0]) << 8;
6596 		break;
6597 	case 4:
6598 		ret = src[3] | ((uint64_t) src[2]) << 8;
6599 		ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24;
6600 		break;
6601 	case 8:
6602 		ret = src[7] | ((uint64_t) src[6]) << 8;
6603 		ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24;
6604 		ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40;
6605 		ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56;
6606 		break;
6607 	default:
6608 		return (0);
6609 	}
6610 
6611 	*offsetp += bytes_to_read;
6612 
6613 	return (ret);
6614 }
6615 
6616 static uint64_t
6617 _decode_lsb(uint8_t **data, int bytes_to_read)
6618 {
6619 	uint64_t ret;
6620 	uint8_t *src;
6621 
6622 	src = *data;
6623 
6624 	ret = 0;
6625 	switch (bytes_to_read) {
6626 	case 8:
6627 		ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
6628 		ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
6629 	case 4:
6630 		ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
6631 	case 2:
6632 		ret |= ((uint64_t) src[1]) << 8;
6633 	case 1:
6634 		ret |= src[0];
6635 		break;
6636 	default:
6637 		return (0);
6638 	}
6639 
6640 	*data += bytes_to_read;
6641 
6642 	return (ret);
6643 }
6644 
6645 static uint64_t
6646 _decode_msb(uint8_t **data, int bytes_to_read)
6647 {
6648 	uint64_t ret;
6649 	uint8_t *src;
6650 
6651 	src = *data;
6652 
6653 	ret = 0;
6654 	switch (bytes_to_read) {
6655 	case 1:
6656 		ret = src[0];
6657 		break;
6658 	case 2:
6659 		ret = src[1] | ((uint64_t) src[0]) << 8;
6660 		break;
6661 	case 4:
6662 		ret = src[3] | ((uint64_t) src[2]) << 8;
6663 		ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24;
6664 		break;
6665 	case 8:
6666 		ret = src[7] | ((uint64_t) src[6]) << 8;
6667 		ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24;
6668 		ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40;
6669 		ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56;
6670 		break;
6671 	default:
6672 		return (0);
6673 		break;
6674 	}
6675 
6676 	*data += bytes_to_read;
6677 
6678 	return (ret);
6679 }
6680 
6681 static int64_t
6682 _decode_sleb128(uint8_t **dp)
6683 {
6684 	int64_t ret = 0;
6685 	uint8_t b;
6686 	int shift = 0;
6687 
6688 	uint8_t *src = *dp;
6689 
6690 	do {
6691 		b = *src++;
6692 		ret |= ((b & 0x7f) << shift);
6693 		shift += 7;
6694 	} while ((b & 0x80) != 0);
6695 
6696 	if (shift < 32 && (b & 0x40) != 0)
6697 		ret |= (-1 << shift);
6698 
6699 	*dp = src;
6700 
6701 	return (ret);
6702 }
6703 
6704 static uint64_t
6705 _decode_uleb128(uint8_t **dp)
6706 {
6707 	uint64_t ret = 0;
6708 	uint8_t b;
6709 	int shift = 0;
6710 
6711 	uint8_t *src = *dp;
6712 
6713 	do {
6714 		b = *src++;
6715 		ret |= ((b & 0x7f) << shift);
6716 		shift += 7;
6717 	} while ((b & 0x80) != 0);
6718 
6719 	*dp = src;
6720 
6721 	return (ret);
6722 }
6723 
6724 static void
6725 readelf_version(void)
6726 {
6727 	(void) printf("%s (%s)\n", ELFTC_GETPROGNAME(),
6728 	    elftc_version());
6729 	exit(EXIT_SUCCESS);
6730 }
6731 
6732 #define	USAGE_MESSAGE	"\
6733 Usage: %s [options] file...\n\
6734   Display information about ELF objects and ar(1) archives.\n\n\
6735   Options:\n\
6736   -a | --all               Equivalent to specifying options '-dhIlrsASV'.\n\
6737   -c | --archive-index     Print the archive symbol table for archives.\n\
6738   -d | --dynamic           Print the contents of SHT_DYNAMIC sections.\n\
6739   -e | --headers           Print all headers in the object.\n\
6740   -g | --section-groups    (accepted, but ignored)\n\
6741   -h | --file-header       Print the file header for the object.\n\
6742   -l | --program-headers   Print the PHDR table for the object.\n\
6743   -n | --notes             Print the contents of SHT_NOTE sections.\n\
6744   -p INDEX | --string-dump=INDEX\n\
6745                            Print the contents of section at index INDEX.\n\
6746   -r | --relocs            Print relocation information.\n\
6747   -s | --syms | --symbols  Print symbol tables.\n\
6748   -t | --section-details   Print additional information about sections.\n\
6749   -v | --version           Print a version identifier and exit.\n\
6750   -x INDEX | --hex-dump=INDEX\n\
6751                            Display contents of a section as hexadecimal.\n\
6752   -A | --arch-specific     (accepted, but ignored)\n\
6753   -D | --use-dynamic       Print the symbol table specified by the DT_SYMTAB\n\
6754                            entry in the \".dynamic\" section.\n\
6755   -H | --help              Print a help message.\n\
6756   -I | --histogram         Print information on bucket list lengths for \n\
6757                            hash sections.\n\
6758   -N | --full-section-name (accepted, but ignored)\n\
6759   -S | --sections | --section-headers\n\
6760                            Print information about section headers.\n\
6761   -V | --version-info      Print symbol versoning information.\n\
6762   -W | --wide              Print information without wrapping long lines.\n"
6763 
6764 
6765 static void
6766 readelf_usage(void)
6767 {
6768 	fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());
6769 	exit(EXIT_FAILURE);
6770 }
6771 
6772 int
6773 main(int argc, char **argv)
6774 {
6775 	struct readelf	*re, re_storage;
6776 	unsigned long	 si;
6777 	int		 opt, i;
6778 	char		*ep;
6779 
6780 	re = &re_storage;
6781 	memset(re, 0, sizeof(*re));
6782 	STAILQ_INIT(&re->v_dumpop);
6783 
6784 	while ((opt = getopt_long(argc, argv, "AacDdegHhIi:lNnp:rSstuVvWw::x:",
6785 	    longopts, NULL)) != -1) {
6786 		switch(opt) {
6787 		case '?':
6788 			readelf_usage();
6789 			break;
6790 		case 'A':
6791 			re->options |= RE_AA;
6792 			break;
6793 		case 'a':
6794 			re->options |= RE_AA | RE_D | RE_H | RE_II | RE_L |
6795 			    RE_R | RE_SS | RE_S | RE_VV;
6796 			break;
6797 		case 'c':
6798 			re->options |= RE_C;
6799 			break;
6800 		case 'D':
6801 			re->options |= RE_DD;
6802 			break;
6803 		case 'd':
6804 			re->options |= RE_D;
6805 			break;
6806 		case 'e':
6807 			re->options |= RE_H | RE_L | RE_SS;
6808 			break;
6809 		case 'g':
6810 			re->options |= RE_G;
6811 			break;
6812 		case 'H':
6813 			readelf_usage();
6814 			break;
6815 		case 'h':
6816 			re->options |= RE_H;
6817 			break;
6818 		case 'I':
6819 			re->options |= RE_II;
6820 			break;
6821 		case 'i':
6822 			/* Not implemented yet. */
6823 			break;
6824 		case 'l':
6825 			re->options |= RE_L;
6826 			break;
6827 		case 'N':
6828 			re->options |= RE_NN;
6829 			break;
6830 		case 'n':
6831 			re->options |= RE_N;
6832 			break;
6833 		case 'p':
6834 			re->options |= RE_P;
6835 			si = strtoul(optarg, &ep, 10);
6836 			if (*ep == '\0')
6837 				add_dumpop(re, (size_t) si, NULL, STR_DUMP,
6838 				    DUMP_BY_INDEX);
6839 			else
6840 				add_dumpop(re, 0, optarg, STR_DUMP,
6841 				    DUMP_BY_NAME);
6842 			break;
6843 		case 'r':
6844 			re->options |= RE_R;
6845 			break;
6846 		case 'S':
6847 			re->options |= RE_SS;
6848 			break;
6849 		case 's':
6850 			re->options |= RE_S;
6851 			break;
6852 		case 't':
6853 			re->options |= RE_T;
6854 			break;
6855 		case 'u':
6856 			re->options |= RE_U;
6857 			break;
6858 		case 'V':
6859 			re->options |= RE_VV;
6860 			break;
6861 		case 'v':
6862 			readelf_version();
6863 			break;
6864 		case 'W':
6865 			re->options |= RE_WW;
6866 			break;
6867 		case 'w':
6868 			re->options |= RE_W;
6869 			parse_dwarf_op_short(re, optarg);
6870 			break;
6871 		case 'x':
6872 			re->options |= RE_X;
6873 			si = strtoul(optarg, &ep, 10);
6874 			if (*ep == '\0')
6875 				add_dumpop(re, (size_t) si, NULL, HEX_DUMP,
6876 				    DUMP_BY_INDEX);
6877 			else
6878 				add_dumpop(re, 0, optarg, HEX_DUMP,
6879 				    DUMP_BY_NAME);
6880 			break;
6881 		case OPTION_DEBUG_DUMP:
6882 			re->options |= RE_W;
6883 			parse_dwarf_op_long(re, optarg);
6884 		}
6885 	}
6886 
6887 	argv += optind;
6888 	argc -= optind;
6889 
6890 	if (argc == 0 || re->options == 0)
6891 		readelf_usage();
6892 
6893 	if (argc > 1)
6894 		re->flags |= DISPLAY_FILENAME;
6895 
6896 	if (elf_version(EV_CURRENT) == EV_NONE)
6897 		errx(EXIT_FAILURE, "ELF library initialization failed: %s",
6898 		    elf_errmsg(-1));
6899 
6900 	for (i = 0; i < argc; i++)
6901 		if (argv[i] != NULL) {
6902 			re->filename = argv[i];
6903 			dump_object(re);
6904 		}
6905 
6906 	exit(EXIT_SUCCESS);
6907 }
6908