xref: /freebsd/contrib/elftoolchain/elfdump/elfdump.c (revision ee5cf11617a9b7f034d95c639bd4d27d1f09e848)
1 /*-
2  * Copyright (c) 2007-2012 Kai Wang
3  * Copyright (c) 2003 David O'Brien.  All rights reserved.
4  * Copyright (c) 2001 Jake Burkholder
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/queue.h>
31 #include <sys/stat.h>
32 
33 #include <ar.h>
34 #include <assert.h>
35 #include <err.h>
36 #include <fcntl.h>
37 #include <gelf.h>
38 #include <getopt.h>
39 #include <libelftc.h>
40 #include <inttypes.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 
46 #ifdef USE_LIBARCHIVE_AR
47 #include <archive.h>
48 #include <archive_entry.h>
49 #endif
50 
51 #include "_elftc.h"
52 
53 ELFTC_VCSID("$Id: elfdump.c 3474 2016-05-17 20:44:53Z emaste $");
54 
55 /* Backwards compatability for older FreeBSD releases. */
56 #ifndef EM_IAMCU
57 #define EM_IAMCU 6
58 #endif
59 #ifndef EM_RISCV
60 #define EM_RISCV 243
61 #endif
62 
63 #if defined(ELFTC_NEED_ELF_NOTE_DEFINITION)
64 #include "native-elf-format.h"
65 #if ELFTC_CLASS == ELFCLASS32
66 typedef Elf32_Nhdr	Elf_Note;
67 #else
68 typedef Elf64_Nhdr	Elf_Note;
69 #endif
70 #endif
71 
72 /* elfdump(1) options. */
73 #define	ED_DYN		(1<<0)
74 #define	ED_EHDR		(1<<1)
75 #define	ED_GOT		(1<<2)
76 #define	ED_HASH		(1<<3)
77 #define	ED_INTERP	(1<<4)
78 #define	ED_NOTE		(1<<5)
79 #define	ED_PHDR		(1<<6)
80 #define	ED_REL		(1<<7)
81 #define	ED_SHDR		(1<<8)
82 #define	ED_SYMTAB	(1<<9)
83 #define	ED_SYMVER	(1<<10)
84 #define	ED_CHECKSUM	(1<<11)
85 #define	ED_ALL		((1<<12)-1)
86 
87 /* elfdump(1) run control flags. */
88 #define	SOLARIS_FMT		(1<<0)
89 #define	PRINT_FILENAME		(1<<1)
90 #define	PRINT_ARSYM		(1<<2)
91 #define	ONLY_ARSYM		(1<<3)
92 
93 /* Convenient print macro. */
94 #define	PRT(...)	fprintf(ed->out, __VA_ARGS__)
95 
96 /* Internal data structure for sections. */
97 struct section {
98 	const char	*name;		/* section name */
99 	Elf_Scn		*scn;		/* section scn */
100 	uint64_t	 off;		/* section offset */
101 	uint64_t	 sz;		/* section size */
102 	uint64_t	 entsize;	/* section entsize */
103 	uint64_t	 align;		/* section alignment */
104 	uint64_t	 type;		/* section type */
105 	uint64_t	 flags;		/* section flags */
106 	uint64_t	 addr;		/* section virtual addr */
107 	uint32_t	 link;		/* section link ndx */
108 	uint32_t	 info;		/* section info ndx */
109 };
110 
111 struct spec_name {
112 	const char	*name;
113 	STAILQ_ENTRY(spec_name)	sn_list;
114 };
115 
116 /* Structure encapsulates the global data for readelf(1). */
117 struct elfdump {
118 	FILE		*out;		/* output redirection. */
119 	const char	*filename;	/* current processing file. */
120 	const char	*archive;	/* archive name */
121 	int		 options;	/* command line options. */
122 	int		 flags;		/* run control flags. */
123 	Elf		*elf;		/* underlying ELF descriptor. */
124 #ifndef USE_LIBARCHIVE_AR
125 	Elf		*ar;		/* ar(1) archive descriptor. */
126 #endif
127 	GElf_Ehdr	 ehdr;		/* ELF header. */
128 	int		 ec;		/* ELF class. */
129 	size_t		 shnum;		/* #sections. */
130 	struct section	*sl;		/* list of sections. */
131 	STAILQ_HEAD(, spec_name) snl;	/* list of names specified by -N. */
132 };
133 
134 /* Relocation entry. */
135 struct rel_entry {
136 	union {
137 		GElf_Rel rel;
138 		GElf_Rela rela;
139 	} u_r;
140 	const char *symn;
141 	uint32_t type;
142 };
143 
144 #if defined(ELFTC_NEED_BYTEORDER_EXTENSIONS)
145 static __inline uint32_t
146 be32dec(const void *pp)
147 {
148 	unsigned char const *p = (unsigned char const *)pp;
149 
150 	return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
151 }
152 
153 static __inline uint32_t
154 le32dec(const void *pp)
155 {
156 	unsigned char const *p = (unsigned char const *)pp;
157 
158 	return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
159 }
160 #endif
161 
162 /* http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#tag_encodings */
163 static const char *
164 d_tags(uint64_t tag)
165 {
166 	static char unknown_buf[64];
167 
168 	switch (tag) {
169 	case DT_NULL:		return "DT_NULL";
170 	case DT_NEEDED:		return "DT_NEEDED";
171 	case DT_PLTRELSZ:	return "DT_PLTRELSZ";
172 	case DT_PLTGOT:		return "DT_PLTGOT";
173 	case DT_HASH:		return "DT_HASH";
174 	case DT_STRTAB:		return "DT_STRTAB";
175 	case DT_SYMTAB:		return "DT_SYMTAB";
176 	case DT_RELA:		return "DT_RELA";
177 	case DT_RELASZ:		return "DT_RELASZ";
178 	case DT_RELAENT:	return "DT_RELAENT";
179 	case DT_STRSZ:		return "DT_STRSZ";
180 	case DT_SYMENT:		return "DT_SYMENT";
181 	case DT_INIT:		return "DT_INIT";
182 	case DT_FINI:		return "DT_FINI";
183 	case DT_SONAME:		return "DT_SONAME";
184 	case DT_RPATH:		return "DT_RPATH";
185 	case DT_SYMBOLIC:	return "DT_SYMBOLIC";
186 	case DT_REL:		return "DT_REL";
187 	case DT_RELSZ:		return "DT_RELSZ";
188 	case DT_RELENT:		return "DT_RELENT";
189 	case DT_PLTREL:		return "DT_PLTREL";
190 	case DT_DEBUG:		return "DT_DEBUG";
191 	case DT_TEXTREL:	return "DT_TEXTREL";
192 	case DT_JMPREL:		return "DT_JMPREL";
193 	case DT_BIND_NOW:	return "DT_BIND_NOW";
194 	case DT_INIT_ARRAY:	return "DT_INIT_ARRAY";
195 	case DT_FINI_ARRAY:	return "DT_FINI_ARRAY";
196 	case DT_INIT_ARRAYSZ:	return "DT_INIT_ARRAYSZ";
197 	case DT_FINI_ARRAYSZ:	return "DT_FINI_ARRAYSZ";
198 	case DT_RUNPATH:	return "DT_RUNPATH";
199 	case DT_FLAGS:		return "DT_FLAGS";
200 	case DT_PREINIT_ARRAY:	return "DT_PREINIT_ARRAY"; /* XXX DT_ENCODING */
201 	case DT_PREINIT_ARRAYSZ:return "DT_PREINIT_ARRAYSZ";
202 	/* 0x6000000D - 0x6ffff000 operating system-specific semantics */
203 	case 0x6ffffdf5:	return "DT_GNU_PRELINKED";
204 	case 0x6ffffdf6:	return "DT_GNU_CONFLICTSZ";
205 	case 0x6ffffdf7:	return "DT_GNU_LIBLISTSZ";
206 	case 0x6ffffdf8:	return "DT_SUNW_CHECKSUM";
207 	case DT_PLTPADSZ:	return "DT_PLTPADSZ";
208 	case DT_MOVEENT:	return "DT_MOVEENT";
209 	case DT_MOVESZ:		return "DT_MOVESZ";
210 	case 0x6ffffdfc:	return "DT_FEATURE";
211 	case DT_POSFLAG_1:	return "DT_POSFLAG_1";
212 	case DT_SYMINSZ:	return "DT_SYMINSZ";
213 	case DT_SYMINENT:	return "DT_SYMINENT (DT_VALRNGHI)";
214 	case DT_ADDRRNGLO:	return "DT_ADDRRNGLO";
215 	case DT_GNU_HASH:	return "DT_GNU_HASH";
216 	case 0x6ffffef8:	return "DT_GNU_CONFLICT";
217 	case 0x6ffffef9:	return "DT_GNU_LIBLIST";
218 	case 0x6ffffefa:	return "DT_CONFIG";
219 	case 0x6ffffefb:	return "DT_DEPAUDIT";
220 	case 0x6ffffefc:	return "DT_AUDIT";
221 	case 0x6ffffefd:	return "DT_PLTPAD";
222 	case 0x6ffffefe:	return "DT_MOVETAB";
223 	case DT_SYMINFO:	return "DT_SYMINFO (DT_ADDRRNGHI)";
224 	case DT_RELACOUNT:	return "DT_RELACOUNT";
225 	case DT_RELCOUNT:	return "DT_RELCOUNT";
226 	case DT_FLAGS_1:	return "DT_FLAGS_1";
227 	case DT_VERDEF:		return "DT_VERDEF";
228 	case DT_VERDEFNUM:	return "DT_VERDEFNUM";
229 	case DT_VERNEED:	return "DT_VERNEED";
230 	case DT_VERNEEDNUM:	return "DT_VERNEEDNUM";
231 	case 0x6ffffff0:	return "DT_GNU_VERSYM";
232 	/* 0x70000000 - 0x7fffffff processor-specific semantics */
233 	case 0x70000000:	return "DT_IA_64_PLT_RESERVE";
234 	case 0x7ffffffd:	return "DT_SUNW_AUXILIARY";
235 	case 0x7ffffffe:	return "DT_SUNW_USED";
236 	case 0x7fffffff:	return "DT_SUNW_FILTER";
237 	}
238 
239 	snprintf(unknown_buf, sizeof(unknown_buf),
240 		"<unknown: %#llx>", (unsigned long long)tag);
241 	return (unknown_buf);
242 }
243 
244 static const char *
245 e_machines(unsigned int mach)
246 {
247 	static char machdesc[64];
248 
249 	switch (mach) {
250 	case EM_NONE:	return "EM_NONE";
251 	case EM_M32:	return "EM_M32";
252 	case EM_SPARC:	return "EM_SPARC";
253 	case EM_386:	return "EM_386";
254 	case EM_68K:	return "EM_68K";
255 	case EM_88K:	return "EM_88K";
256 	case EM_IAMCU:	return "EM_IAMCU";
257 	case EM_860:	return "EM_860";
258 	case EM_MIPS:	return "EM_MIPS";
259 	case EM_PPC:	return "EM_PPC";
260 	case EM_PPC64:	return "EM_PPC64";
261 	case EM_ARM:	return "EM_ARM";
262 	case EM_ALPHA:	return "EM_ALPHA (legacy)";
263 	case EM_SPARCV9:return "EM_SPARCV9";
264 	case EM_IA_64:	return "EM_IA_64";
265 	case EM_X86_64:	return "EM_X86_64";
266 	case EM_AARCH64:return "EM_AARCH64";
267 	case EM_RISCV:	return "EM_RISCV";
268 	}
269 	snprintf(machdesc, sizeof(machdesc),
270 	    "(unknown machine) -- type 0x%x", mach);
271 	return (machdesc);
272 }
273 
274 static const char *
275 elf_type_str(unsigned int type)
276 {
277 	static char s_type[32];
278 
279 	switch (type)
280 	{
281 	case ET_NONE:	return "ET_NONE";
282 	case ET_REL:	return "ET_REL";
283 	case ET_EXEC:	return "ET_EXEC";
284 	case ET_DYN:	return "ET_DYN";
285 	case ET_CORE:	return "ET_CORE";
286 	}
287 	if (type >= ET_LOPROC)
288 		snprintf(s_type, sizeof(s_type), "<proc: %#x>", type);
289 	else if (type >= ET_LOOS && type <= ET_HIOS)
290 		snprintf(s_type, sizeof(s_type), "<os: %#x>", type);
291 	else
292 		snprintf(s_type, sizeof(s_type), "<unknown: %#x", type);
293 	return (s_type);
294 }
295 
296 static const char *
297 elf_version_str(unsigned int ver)
298 {
299 	static char s_ver[32];
300 
301 	switch (ver) {
302 	case EV_NONE:		return "EV_NONE";
303 	case EV_CURRENT:	return "EV_CURRENT";
304 	}
305 	snprintf(s_ver, sizeof(s_ver), "<unknown: %#x>", ver);
306 	return (s_ver);
307 }
308 
309 static const char *
310 elf_class_str(unsigned int class)
311 {
312 	static char s_class[32];
313 
314 	switch (class) {
315 	case ELFCLASSNONE:	return "ELFCLASSNONE";
316 	case ELFCLASS32:	return "ELFCLASS32";
317 	case ELFCLASS64:	return "ELFCLASS64";
318 	}
319 	snprintf(s_class, sizeof(s_class), "<unknown: %#x>", class);
320 	return (s_class);
321 }
322 
323 static const char *
324 elf_data_str(unsigned int data)
325 {
326 	static char s_data[32];
327 
328 	switch (data) {
329 	case ELFDATANONE:	return "ELFDATANONE";
330 	case ELFDATA2LSB:	return "ELFDATA2LSB";
331 	case ELFDATA2MSB:	return "ELFDATA2MSB";
332 	}
333 	snprintf(s_data, sizeof(s_data), "<unknown: %#x>", data);
334 	return (s_data);
335 }
336 
337 static const char *ei_abis[256] = {
338 	"ELFOSABI_NONE", "ELFOSABI_HPUX", "ELFOSABI_NETBSD", "ELFOSABI_LINUX",
339 	"ELFOSABI_HURD", "ELFOSABI_86OPEN", "ELFOSABI_SOLARIS", "ELFOSABI_AIX",
340 	"ELFOSABI_IRIX", "ELFOSABI_FREEBSD", "ELFOSABI_TRU64",
341 	"ELFOSABI_MODESTO", "ELFOSABI_OPENBSD",
342 	[17] = "ELFOSABI_CLOUDABI",
343 	[255] = "ELFOSABI_STANDALONE"
344 };
345 
346 static const char *
347 elf_phdr_type_str(unsigned int type)
348 {
349 	static char s_type[32];
350 
351 	switch (type) {
352 	case PT_NULL:		return "PT_NULL";
353 	case PT_LOAD:		return "PT_LOAD";
354 	case PT_DYNAMIC:	return "PT_DYNAMIC";
355 	case PT_INTERP:		return "PT_INTERP";
356 	case PT_NOTE:		return "PT_NOTE";
357 	case PT_SHLIB:		return "PT_SHLIB";
358 	case PT_PHDR:		return "PT_PHDR";
359 	case PT_TLS:		return "PT_TLS";
360 	case PT_GNU_EH_FRAME:	return "PT_GNU_EH_FRAME";
361 	case PT_GNU_STACK:	return "PT_GNU_STACK";
362 	case PT_GNU_RELRO:	return "PT_GNU_RELRO";
363 	}
364 	snprintf(s_type, sizeof(s_type), "<unknown: %#x>", type);
365 	return (s_type);
366 }
367 
368 static const char *p_flags[] = {
369 	"", "PF_X", "PF_W", "PF_X|PF_W", "PF_R", "PF_X|PF_R", "PF_W|PF_R",
370 	"PF_X|PF_W|PF_R"
371 };
372 
373 static const char *
374 sh_name(struct elfdump *ed, int ndx)
375 {
376 	static char num[10];
377 
378 	switch (ndx) {
379 	case SHN_UNDEF: return "UNDEF";
380 	case SHN_ABS: return "ABS";
381 	case SHN_COMMON: return "COMMON";
382 	default:
383 		if ((uint64_t)ndx < ed->shnum)
384 			return (ed->sl[ndx].name);
385 		else {
386 			snprintf(num, sizeof(num), "%d", ndx);
387 			return (num);
388 		}
389 	}
390 }
391 
392 /* http://www.sco.com/developers/gabi/latest/ch4.sheader.html#sh_type */
393 static const char *
394 sh_types(uint64_t mach, uint64_t sht) {
395 	static char unknown_buf[64];
396 
397 	if (sht < 0x60000000) {
398 		switch (sht) {
399 		case SHT_NULL:		return "SHT_NULL";
400 		case SHT_PROGBITS:	return "SHT_PROGBITS";
401 		case SHT_SYMTAB:	return "SHT_SYMTAB";
402 		case SHT_STRTAB:	return "SHT_STRTAB";
403 		case SHT_RELA:		return "SHT_RELA";
404 		case SHT_HASH:		return "SHT_HASH";
405 		case SHT_DYNAMIC:	return "SHT_DYNAMIC";
406 		case SHT_NOTE:		return "SHT_NOTE";
407 		case SHT_NOBITS:	return "SHT_NOBITS";
408 		case SHT_REL:		return "SHT_REL";
409 		case SHT_SHLIB:		return "SHT_SHLIB";
410 		case SHT_DYNSYM:	return "SHT_DYNSYM";
411 		case SHT_INIT_ARRAY:	return "SHT_INIT_ARRAY";
412 		case SHT_FINI_ARRAY:	return "SHT_FINI_ARRAY";
413 		case SHT_PREINIT_ARRAY:	return "SHT_PREINIT_ARRAY";
414 		case SHT_GROUP:		return "SHT_GROUP";
415 		case SHT_SYMTAB_SHNDX:	return "SHT_SYMTAB_SHNDX";
416 		}
417 	} else if (sht < 0x70000000) {
418 		/* 0x60000000-0x6fffffff operating system-specific semantics */
419 		switch (sht) {
420 		case 0x6ffffff0:	return "XXX:VERSYM";
421 		case SHT_SUNW_dof:	return "SHT_SUNW_dof";
422 		case SHT_GNU_HASH:	return "SHT_GNU_HASH";
423 		case 0x6ffffff7:	return "SHT_GNU_LIBLIST";
424 		case 0x6ffffffc:	return "XXX:VERDEF";
425 		case SHT_SUNW_verdef:	return "SHT_SUNW(GNU)_verdef";
426 		case SHT_SUNW_verneed:	return "SHT_SUNW(GNU)_verneed";
427 		case SHT_SUNW_versym:	return "SHT_SUNW(GNU)_versym";
428 		}
429 	} else if (sht < 0x80000000) {
430 		/* 0x70000000 - 0x7fffffff processor-specific semantics */
431 		switch (mach) {
432 		case EM_ARM:
433 			switch (sht) {
434 			case SHT_ARM_EXIDX: return "SHT_ARM_EXIDX";
435 			case SHT_ARM_PREEMPTMAP: return "SHT_ARM_PREEMPTMAP";
436 			case SHT_ARM_ATTRIBUTES: return "SHT_ARM_ATTRIBUTES";
437 			case SHT_ARM_DEBUGOVERLAY:
438 			    return "SHT_ARM_DEBUGOVERLAY";
439 			case SHT_ARM_OVERLAYSECTION:
440 			    return "SHT_ARM_OVERLAYSECTION";
441 			}
442 			break;
443 		case EM_IA_64:
444 			switch (sht) {
445 			case 0x70000000: return "SHT_IA_64_EXT";
446 			case 0x70000001: return "SHT_IA_64_UNWIND";
447 			}
448 			break;
449 		case EM_MIPS:
450 			switch (sht) {
451 			case SHT_MIPS_REGINFO: return "SHT_MIPS_REGINFO";
452 			case SHT_MIPS_OPTIONS: return "SHT_MIPS_OPTIONS";
453 			case SHT_MIPS_ABIFLAGS: return "SHT_MIPS_ABIFLAGS";
454 			}
455 			break;
456 		}
457 		switch (sht) {
458 		case 0x7ffffffd: return "XXX:AUXILIARY";
459 		case 0x7fffffff: return "XXX:FILTER";
460 		}
461 	}
462 	/* 0x80000000 - 0xffffffff application programs */
463 
464 	snprintf(unknown_buf, sizeof(unknown_buf),
465 		"<unknown: %#llx>", (unsigned long long)sht);
466 	return (unknown_buf);
467 }
468 
469 /*
470  * Define known section flags. These flags are defined in the order
471  * they are to be printed out.
472  */
473 #define	DEFINE_SHFLAGS()			\
474 	DEFINE_SHF(WRITE)			\
475 	DEFINE_SHF(ALLOC)			\
476 	DEFINE_SHF(EXECINSTR)			\
477 	DEFINE_SHF(MERGE)			\
478 	DEFINE_SHF(STRINGS)			\
479 	DEFINE_SHF(INFO_LINK)			\
480 	DEFINE_SHF(LINK_ORDER)			\
481 	DEFINE_SHF(OS_NONCONFORMING)		\
482 	DEFINE_SHF(GROUP)			\
483 	DEFINE_SHF(TLS)				\
484 	DEFINE_SHF(COMPRESSED)
485 
486 #undef	DEFINE_SHF
487 #define	DEFINE_SHF(F) "SHF_" #F "|"
488 #define ALLSHFLAGS	DEFINE_SHFLAGS()
489 
490 static const char *
491 sh_flags(uint64_t shf)
492 {
493 	static char	flg[sizeof(ALLSHFLAGS)+1];
494 
495 	flg[0] = '\0';
496 
497 #undef	DEFINE_SHF
498 #define	DEFINE_SHF(N)				\
499 	if (shf & SHF_##N)			\
500 		strcat(flg, "SHF_" #N "|");	\
501 
502 	DEFINE_SHFLAGS()
503 
504 	flg[strlen(flg) - 1] = '\0'; /* Remove the trailing "|". */
505 
506 	return (flg);
507 }
508 
509 static const char *
510 st_type(unsigned int mach, unsigned int type)
511 {
512 	static char s_type[32];
513 
514 	switch (type) {
515 	case STT_NOTYPE: return "STT_NOTYPE";
516 	case STT_OBJECT: return "STT_OBJECT";
517 	case STT_FUNC: return "STT_FUNC";
518 	case STT_SECTION: return "STT_SECTION";
519 	case STT_FILE: return "STT_FILE";
520 	case STT_COMMON: return "STT_COMMON";
521 	case STT_TLS: return "STT_TLS";
522 	case 13:
523 		if (mach == EM_SPARCV9)
524 			return "STT_SPARC_REGISTER";
525 		break;
526 	}
527 	snprintf(s_type, sizeof(s_type), "<unknown: %#x>", type);
528 	return (s_type);
529 }
530 
531 static const char *
532 st_type_S(unsigned int type)
533 {
534 	static char s_type[32];
535 
536 	switch (type) {
537 	case STT_NOTYPE: return "NOTY";
538 	case STT_OBJECT: return "OBJT";
539 	case STT_FUNC: return "FUNC";
540 	case STT_SECTION: return "SECT";
541 	case STT_FILE: return "FILE";
542 	}
543 	snprintf(s_type, sizeof(s_type), "<unknown: %#x>", type);
544 	return (s_type);
545 }
546 
547 static const char *
548 st_bindings(unsigned int sbind)
549 {
550 	static char s_sbind[32];
551 
552 	switch (sbind) {
553 	case STB_LOCAL: return "STB_LOCAL";
554 	case STB_GLOBAL: return "STB_GLOBAL";
555 	case STB_WEAK: return "STB_WEAK";
556 	case STB_GNU_UNIQUE: return "STB_GNU_UNIQUE";
557 	default:
558 		if (sbind >= STB_LOOS && sbind <= STB_HIOS)
559 			return "OS";
560 		else if (sbind >= STB_LOPROC && sbind <= STB_HIPROC)
561 			return "PROC";
562 		else
563 			snprintf(s_sbind, sizeof(s_sbind), "<unknown: %#x>",
564 			    sbind);
565 		return (s_sbind);
566 	}
567 }
568 
569 static const char *
570 st_bindings_S(unsigned int sbind)
571 {
572 	static char s_sbind[32];
573 
574 	switch (sbind) {
575 	case STB_LOCAL: return "LOCL";
576 	case STB_GLOBAL: return "GLOB";
577 	case STB_WEAK: return "WEAK";
578 	case STB_GNU_UNIQUE: return "UNIQ";
579 	default:
580 		if (sbind >= STB_LOOS && sbind <= STB_HIOS)
581 			return "OS";
582 		else if (sbind >= STB_LOPROC && sbind <= STB_HIPROC)
583 			return "PROC";
584 		else
585 			snprintf(s_sbind, sizeof(s_sbind), "<%#x>",
586 			    sbind);
587 		return (s_sbind);
588 	}
589 }
590 
591 static unsigned char st_others[] = {
592 	'D', 'I', 'H', 'P'
593 };
594 
595 static void	add_name(struct elfdump *ed, const char *name);
596 static void	elf_print_object(struct elfdump *ed);
597 static void	elf_print_elf(struct elfdump *ed);
598 static void	elf_print_ehdr(struct elfdump *ed);
599 static void	elf_print_phdr(struct elfdump *ed);
600 static void	elf_print_shdr(struct elfdump *ed);
601 static void	elf_print_symtab(struct elfdump *ed, int i);
602 static void	elf_print_symtabs(struct elfdump *ed);
603 static void	elf_print_symver(struct elfdump *ed);
604 static void	elf_print_verdef(struct elfdump *ed, struct section *s);
605 static void	elf_print_verneed(struct elfdump *ed, struct section *s);
606 static void	elf_print_interp(struct elfdump *ed);
607 static void	elf_print_dynamic(struct elfdump *ed);
608 static void	elf_print_rel_entry(struct elfdump *ed, struct section *s,
609     int j, struct rel_entry *r);
610 static void	elf_print_rela(struct elfdump *ed, struct section *s,
611     Elf_Data *data);
612 static void	elf_print_rel(struct elfdump *ed, struct section *s,
613     Elf_Data *data);
614 static void	elf_print_reloc(struct elfdump *ed);
615 static void	elf_print_got(struct elfdump *ed);
616 static void	elf_print_got_section(struct elfdump *ed, struct section *s);
617 static void	elf_print_note(struct elfdump *ed);
618 static void	elf_print_svr4_hash(struct elfdump *ed, struct section *s);
619 static void	elf_print_svr4_hash64(struct elfdump *ed, struct section *s);
620 static void	elf_print_gnu_hash(struct elfdump *ed, struct section *s);
621 static void	elf_print_hash(struct elfdump *ed);
622 static void	elf_print_checksum(struct elfdump *ed);
623 static void	find_gotrel(struct elfdump *ed, struct section *gs,
624     struct rel_entry *got);
625 static struct spec_name	*find_name(struct elfdump *ed, const char *name);
626 static int	get_ent_count(const struct section *s, int *ent_count);
627 static const char *get_symbol_name(struct elfdump *ed, uint32_t symtab, int i);
628 static const char *get_string(struct elfdump *ed, int strtab, size_t off);
629 static void	get_versym(struct elfdump *ed, int i, uint16_t **vs, int *nvs);
630 static void	load_sections(struct elfdump *ed);
631 static void	unload_sections(struct elfdump *ed);
632 static void	usage(void);
633 #ifdef	USE_LIBARCHIVE_AR
634 static int	ac_detect_ar(int fd);
635 static void	ac_print_ar(struct elfdump *ed, int fd);
636 #else
637 static void	elf_print_ar(struct elfdump *ed, int fd);
638 #endif	/* USE_LIBARCHIVE_AR */
639 
640 static struct option elfdump_longopts[] =
641 {
642 	{ "help",	no_argument,	NULL,	'H' },
643 	{ "version",	no_argument,	NULL,	'V' },
644 	{ NULL,		0,		NULL,	0   }
645 };
646 
647 int
648 main(int ac, char **av)
649 {
650 	struct elfdump		*ed, ed_storage;
651 	struct spec_name	*sn;
652 	int			 ch, i;
653 
654 	ed = &ed_storage;
655 	memset(ed, 0, sizeof(*ed));
656 	STAILQ_INIT(&ed->snl);
657 	ed->out = stdout;
658 	while ((ch = getopt_long(ac, av, "acdeiGHhknN:prsSvVw:",
659 		elfdump_longopts, NULL)) != -1)
660 		switch (ch) {
661 		case 'a':
662 			ed->options = ED_ALL;
663 			break;
664 		case 'c':
665 			ed->options |= ED_SHDR;
666 			break;
667 		case 'd':
668 			ed->options |= ED_DYN;
669 			break;
670 		case 'e':
671 			ed->options |= ED_EHDR;
672 			break;
673 		case 'i':
674 			ed->options |= ED_INTERP;
675 			break;
676 		case 'G':
677 			ed->options |= ED_GOT;
678 			break;
679 		case 'h':
680 			ed->options |= ED_HASH;
681 			break;
682 		case 'k':
683 			ed->options |= ED_CHECKSUM;
684 			break;
685 		case 'n':
686 			ed->options |= ED_NOTE;
687 			break;
688 		case 'N':
689 			add_name(ed, optarg);
690 			break;
691 		case 'p':
692 			ed->options |= ED_PHDR;
693 			break;
694 		case 'r':
695 			ed->options |= ED_REL;
696 			break;
697 		case 's':
698 			ed->options |= ED_SYMTAB;
699 			break;
700 		case 'S':
701 			ed->flags |= SOLARIS_FMT;
702 			break;
703 		case 'v':
704 			ed->options |= ED_SYMVER;
705 			break;
706 		case 'V':
707 			(void) printf("%s (%s)\n", ELFTC_GETPROGNAME(),
708 			    elftc_version());
709 			exit(EXIT_SUCCESS);
710 			break;
711 		case 'w':
712 			if ((ed->out = fopen(optarg, "w")) == NULL)
713 				err(EXIT_FAILURE, "%s", optarg);
714 			break;
715 		case '?':
716 		case 'H':
717 		default:
718 			usage();
719 		}
720 
721 	ac -= optind;
722 	av += optind;
723 
724 	if (ed->options == 0)
725 		ed->options = ED_ALL;
726 	sn = NULL;
727 	if (ed->options & ED_SYMTAB &&
728 	    (STAILQ_EMPTY(&ed->snl) || (sn = find_name(ed, "ARSYM")) != NULL)) {
729 		ed->flags |= PRINT_ARSYM;
730 		if (sn != NULL) {
731 			STAILQ_REMOVE(&ed->snl, sn, spec_name, sn_list);
732 			if (STAILQ_EMPTY(&ed->snl))
733 				ed->flags |= ONLY_ARSYM;
734 		}
735 	}
736 	if (ac == 0)
737 		usage();
738 	if (ac > 1)
739 		ed->flags |= PRINT_FILENAME;
740 	if (elf_version(EV_CURRENT) == EV_NONE)
741 		errx(EXIT_FAILURE, "ELF library initialization failed: %s",
742 		    elf_errmsg(-1));
743 
744 	for (i = 0; i < ac; i++) {
745 		ed->filename = av[i];
746 		ed->archive = NULL;
747 		elf_print_object(ed);
748 	}
749 
750 	exit(EXIT_SUCCESS);
751 }
752 
753 #ifdef USE_LIBARCHIVE_AR
754 
755 /* Archive symbol table entry. */
756 struct arsym_entry {
757 	char *sym_name;
758 	size_t off;
759 };
760 
761 /*
762  * Convenient wrapper for general libarchive error handling.
763  */
764 #define	AC(CALL) do {							\
765 	if ((CALL)) {							\
766 		warnx("%s", archive_error_string(a));			\
767 		return;							\
768 	}								\
769 } while (0)
770 
771 /*
772  * Detect an ar(1) archive using libarchive(3).
773  */
774 static int
775 ac_detect_ar(int fd)
776 {
777 	struct archive		*a;
778 	struct archive_entry	*entry;
779 	int			 r;
780 
781 	r = -1;
782 	if ((a = archive_read_new()) == NULL)
783 		return (0);
784 	archive_read_support_format_ar(a);
785 	if (archive_read_open_fd(a, fd, 10240) == ARCHIVE_OK)
786 		r = archive_read_next_header(a, &entry);
787 	archive_read_close(a);
788 	archive_read_free(a);
789 
790 	return (r == ARCHIVE_OK);
791 }
792 
793 /*
794  * Dump an ar(1) archive using libarchive(3).
795  */
796 static void
797 ac_print_ar(struct elfdump *ed, int fd)
798 {
799 	struct archive		*a;
800 	struct archive_entry	*entry;
801 	struct arsym_entry	*arsym;
802 	const char		*name;
803 	char			 idx[10], *b;
804 	void			*buff;
805 	size_t			 size;
806 	uint32_t		 cnt, i;
807 	int			 r;
808 
809 	if (lseek(fd, 0, SEEK_SET) == -1)
810 		err(EXIT_FAILURE, "lseek failed");
811 	if ((a = archive_read_new()) == NULL)
812 		errx(EXIT_FAILURE, "%s", archive_error_string(a));
813 	archive_read_support_format_ar(a);
814 	AC(archive_read_open_fd(a, fd, 10240));
815 	for(;;) {
816 		r = archive_read_next_header(a, &entry);
817 		if (r == ARCHIVE_FATAL)
818 			errx(EXIT_FAILURE, "%s", archive_error_string(a));
819 		if (r == ARCHIVE_EOF)
820 			break;
821 		if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY)
822 			warnx("%s", archive_error_string(a));
823 		if (r == ARCHIVE_RETRY)
824 			continue;
825 		name = archive_entry_pathname(entry);
826 		size = archive_entry_size(entry);
827 		if (size == 0)
828 			continue;
829 		if ((buff = malloc(size)) == NULL) {
830 			warn("malloc failed");
831 			continue;
832 		}
833 		if (archive_read_data(a, buff, size) != (ssize_t)size) {
834 			warnx("%s", archive_error_string(a));
835 			free(buff);
836 			continue;
837 		}
838 
839 		/*
840 		 * Note that when processing arsym via libarchive, there is
841 		 * no way to tell which member a certain symbol belongs to,
842 		 * since we can not just "lseek" to a member offset and read
843 		 * the member header.
844 		 */
845 		if (!strcmp(name, "/") && ed->flags & PRINT_ARSYM) {
846 			b = buff;
847 			cnt = be32dec(b);
848 			if (cnt == 0) {
849 				free(buff);
850 				continue;
851 			}
852 			arsym = calloc(cnt, sizeof(*arsym));
853 			if (arsym == NULL)
854 				err(EXIT_FAILURE, "calloc failed");
855 			b += sizeof(uint32_t);
856 			for (i = 0; i < cnt; i++) {
857 				arsym[i].off = be32dec(b);
858 				b += sizeof(uint32_t);
859 			}
860 			for (i = 0; i < cnt; i++) {
861 				arsym[i].sym_name = b;
862 				b += strlen(b) + 1;
863 			}
864 			if (ed->flags & SOLARIS_FMT) {
865 				PRT("\nSymbol Table: (archive)\n");
866 				PRT("     index    offset    symbol\n");
867 			} else
868 				PRT("\nsymbol table (archive):\n");
869 			for (i = 0; i < cnt; i++) {
870 				if (ed->flags & SOLARIS_FMT) {
871 					snprintf(idx, sizeof(idx), "[%d]", i);
872 					PRT("%10s  ", idx);
873 					PRT("0x%8.8jx  ",
874 					    (uintmax_t)arsym[i].off);
875 					PRT("%s\n", arsym[i].sym_name);
876 				} else {
877 					PRT("\nentry: %d\n", i);
878 					PRT("\toffset: %#jx\n",
879 					    (uintmax_t)arsym[i].off);
880 					PRT("\tsymbol: %s\n",
881 					    arsym[i].sym_name);
882 				}
883 			}
884 			free(arsym);
885 			free(buff);
886 			/* No need to continue if we only dump ARSYM. */
887 			if (ed->flags & ONLY_ARSYM) {
888 				AC(archive_read_close(a));
889 				AC(archive_read_free(a));
890 				return;
891 			}
892 			continue;
893 		}
894 		if ((ed->elf = elf_memory(buff, size)) == NULL) {
895 			warnx("elf_memroy() failed: %s",
896 			      elf_errmsg(-1));
897 			free(buff);
898 			continue;
899 		}
900 		/* Skip non-ELF member. */
901 		if (elf_kind(ed->elf) == ELF_K_ELF) {
902 			printf("\n%s(%s):\n", ed->archive, name);
903 			elf_print_elf(ed);
904 		}
905 		elf_end(ed->elf);
906 		free(buff);
907 	}
908 	AC(archive_read_close(a));
909 	AC(archive_read_free(a));
910 }
911 
912 #else  /* USE_LIBARCHIVE_AR */
913 
914 /*
915  * Dump an ar(1) archive.
916  */
917 static void
918 elf_print_ar(struct elfdump *ed, int fd)
919 {
920 	Elf		*e;
921 	Elf_Arhdr	*arh;
922 	Elf_Arsym	*arsym;
923 	Elf_Cmd		 cmd;
924 	char		 idx[10];
925 	size_t		 cnt, i;
926 
927 	ed->ar = ed->elf;
928 
929 	if (ed->flags & PRINT_ARSYM) {
930 		cnt = 0;
931 		if ((arsym = elf_getarsym(ed->ar, &cnt)) == NULL) {
932 			warnx("elf_getarsym failed: %s", elf_errmsg(-1));
933 			goto print_members;
934 		}
935 		if (cnt == 0)
936 			goto print_members;
937 		if (ed->flags & SOLARIS_FMT) {
938 			PRT("\nSymbol Table: (archive)\n");
939 			PRT("     index    offset    member name and symbol\n");
940 		} else
941 			PRT("\nsymbol table (archive):\n");
942 		for (i = 0; i < cnt - 1; i++) {
943 			if (elf_rand(ed->ar, arsym[i].as_off) !=
944 			    arsym[i].as_off) {
945 				warnx("elf_rand failed: %s", elf_errmsg(-1));
946 				break;
947 			}
948 			if ((e = elf_begin(fd, ELF_C_READ, ed->ar)) == NULL) {
949 				warnx("elf_begin failed: %s", elf_errmsg(-1));
950 				break;
951 			}
952 			if ((arh = elf_getarhdr(e)) == NULL) {
953 				warnx("elf_getarhdr failed: %s",
954 				    elf_errmsg(-1));
955 				break;
956 			}
957 			if (ed->flags & SOLARIS_FMT) {
958 				snprintf(idx, sizeof(idx), "[%zu]", i);
959 				PRT("%10s  ", idx);
960 				PRT("0x%8.8jx  ",
961 				    (uintmax_t)arsym[i].as_off);
962 				PRT("(%s):%s\n", arh->ar_name,
963 				    arsym[i].as_name);
964 			} else {
965 				PRT("\nentry: %zu\n", i);
966 				PRT("\toffset: %#jx\n",
967 				    (uintmax_t)arsym[i].as_off);
968 				PRT("\tmember: %s\n", arh->ar_name);
969 				PRT("\tsymbol: %s\n", arsym[i].as_name);
970 			}
971 			elf_end(e);
972 		}
973 
974 		/* No need to continue if we only dump ARSYM. */
975 		if (ed->flags & ONLY_ARSYM)
976 			return;
977 	}
978 
979 print_members:
980 
981 	/* Rewind the archive. */
982 	if (elf_rand(ed->ar, SARMAG) != SARMAG) {
983 		warnx("elf_rand failed: %s", elf_errmsg(-1));
984 		return;
985 	}
986 
987 	/* Dump each member of the archive. */
988 	cmd = ELF_C_READ;
989 	while ((ed->elf = elf_begin(fd, cmd, ed->ar)) != NULL) {
990 		/* Skip non-ELF member. */
991 		if (elf_kind(ed->elf) == ELF_K_ELF) {
992 			if ((arh = elf_getarhdr(ed->elf)) == NULL) {
993 				warnx("elf_getarhdr failed: %s",
994 				    elf_errmsg(-1));
995 				break;
996 			}
997 			printf("\n%s(%s):\n", ed->archive, arh->ar_name);
998 			elf_print_elf(ed);
999 		}
1000 		cmd = elf_next(ed->elf);
1001 		elf_end(ed->elf);
1002 	}
1003 }
1004 
1005 #endif	/* USE_LIBARCHIVE_AR */
1006 
1007 /*
1008  * Dump an object. (ELF object or ar(1) archive)
1009  */
1010 static void
1011 elf_print_object(struct elfdump *ed)
1012 {
1013 	int fd;
1014 
1015 	if ((fd = open(ed->filename, O_RDONLY)) == -1) {
1016 		warn("open %s failed", ed->filename);
1017 		return;
1018 	}
1019 
1020 #ifdef	USE_LIBARCHIVE_AR
1021 	if (ac_detect_ar(fd)) {
1022 		ed->archive = ed->filename;
1023 		ac_print_ar(ed, fd);
1024 		return;
1025 	}
1026 #endif	/* USE_LIBARCHIVE_AR */
1027 
1028 	if ((ed->elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
1029 		warnx("elf_begin() failed: %s", elf_errmsg(-1));
1030 		return;
1031 	}
1032 
1033 	switch (elf_kind(ed->elf)) {
1034 	case ELF_K_NONE:
1035 		warnx("Not an ELF file.");
1036 		return;
1037 	case ELF_K_ELF:
1038 		if (ed->flags & PRINT_FILENAME)
1039 			printf("\n%s:\n", ed->filename);
1040 		elf_print_elf(ed);
1041 		break;
1042 	case ELF_K_AR:
1043 #ifndef	USE_LIBARCHIVE_AR
1044 		ed->archive = ed->filename;
1045 		elf_print_ar(ed, fd);
1046 #endif
1047 		break;
1048 	default:
1049 		warnx("Internal: libelf returned unknown elf kind.");
1050 		return;
1051 	}
1052 
1053 	elf_end(ed->elf);
1054 }
1055 
1056 /*
1057  * Dump an ELF object.
1058  */
1059 static void
1060 elf_print_elf(struct elfdump *ed)
1061 {
1062 
1063 	if (gelf_getehdr(ed->elf, &ed->ehdr) == NULL) {
1064 		warnx("gelf_getehdr failed: %s", elf_errmsg(-1));
1065 		return;
1066 	}
1067 	if ((ed->ec = gelf_getclass(ed->elf)) == ELFCLASSNONE) {
1068 		warnx("gelf_getclass failed: %s", elf_errmsg(-1));
1069 		return;
1070 	}
1071 
1072 	if (ed->options & (ED_SHDR | ED_DYN | ED_REL | ED_GOT | ED_SYMTAB |
1073 	    ED_SYMVER | ED_NOTE | ED_HASH))
1074 		load_sections(ed);
1075 
1076 	if (ed->options & ED_EHDR)
1077 		elf_print_ehdr(ed);
1078 	if (ed->options & ED_PHDR)
1079 		elf_print_phdr(ed);
1080 	if (ed->options & ED_INTERP)
1081 		elf_print_interp(ed);
1082 	if (ed->options & ED_SHDR)
1083 		elf_print_shdr(ed);
1084 	if (ed->options & ED_DYN)
1085 		elf_print_dynamic(ed);
1086 	if (ed->options & ED_REL)
1087 		elf_print_reloc(ed);
1088 	if (ed->options & ED_GOT)
1089 		elf_print_got(ed);
1090 	if (ed->options & ED_SYMTAB)
1091 		elf_print_symtabs(ed);
1092 	if (ed->options & ED_SYMVER)
1093 		elf_print_symver(ed);
1094 	if (ed->options & ED_NOTE)
1095 		elf_print_note(ed);
1096 	if (ed->options & ED_HASH)
1097 		elf_print_hash(ed);
1098 	if (ed->options & ED_CHECKSUM)
1099 		elf_print_checksum(ed);
1100 
1101 	unload_sections(ed);
1102 }
1103 
1104 /*
1105  * Read the section headers from ELF object and store them in the
1106  * internal cache.
1107  */
1108 static void
1109 load_sections(struct elfdump *ed)
1110 {
1111 	struct section	*s;
1112 	const char	*name;
1113 	Elf_Scn		*scn;
1114 	GElf_Shdr	 sh;
1115 	size_t		 shstrndx, ndx;
1116 	int		 elferr;
1117 
1118 	assert(ed->sl == NULL);
1119 
1120 	if (!elf_getshnum(ed->elf, &ed->shnum)) {
1121 		warnx("elf_getshnum failed: %s", elf_errmsg(-1));
1122 		return;
1123 	}
1124 	if (ed->shnum == 0)
1125 		return;
1126 	if ((ed->sl = calloc(ed->shnum, sizeof(*ed->sl))) == NULL)
1127 		err(EXIT_FAILURE, "calloc failed");
1128 	if (!elf_getshstrndx(ed->elf, &shstrndx)) {
1129 		warnx("elf_getshstrndx failed: %s", elf_errmsg(-1));
1130 		return;
1131 	}
1132 	if ((scn = elf_getscn(ed->elf, 0)) == NULL) {
1133 		warnx("elf_getscn failed: %s", elf_errmsg(-1));
1134 		return;
1135 	}
1136 	(void) elf_errno();
1137 	do {
1138 		if (gelf_getshdr(scn, &sh) == NULL) {
1139 			warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
1140 			(void) elf_errno();
1141 			continue;
1142 		}
1143 		if ((name = elf_strptr(ed->elf, shstrndx, sh.sh_name)) == NULL) {
1144 			(void) elf_errno();
1145 			name = "ERROR";
1146 		}
1147 		if ((ndx = elf_ndxscn(scn)) == SHN_UNDEF)
1148 			if ((elferr = elf_errno()) != 0) {
1149 				warnx("elf_ndxscn failed: %s",
1150 				    elf_errmsg(elferr));
1151 				continue;
1152 			}
1153 		if (ndx >= ed->shnum) {
1154 			warnx("section index of '%s' out of range", name);
1155 			continue;
1156 		}
1157 		s = &ed->sl[ndx];
1158 		s->name = name;
1159 		s->scn = scn;
1160 		s->off = sh.sh_offset;
1161 		s->sz = sh.sh_size;
1162 		s->entsize = sh.sh_entsize;
1163 		s->align = sh.sh_addralign;
1164 		s->type = sh.sh_type;
1165 		s->flags = sh.sh_flags;
1166 		s->addr = sh.sh_addr;
1167 		s->link = sh.sh_link;
1168 		s->info = sh.sh_info;
1169 	} while ((scn = elf_nextscn(ed->elf, scn)) != NULL);
1170 	elferr = elf_errno();
1171 	if (elferr != 0)
1172 		warnx("elf_nextscn failed: %s", elf_errmsg(elferr));
1173 }
1174 
1175 /*
1176  * Release section related resources.
1177  */
1178 static void
1179 unload_sections(struct elfdump *ed)
1180 {
1181 	if (ed->sl != NULL) {
1182 		free(ed->sl);
1183 		ed->sl = NULL;
1184 	}
1185 }
1186 
1187 /*
1188  * Add a name to the '-N' name list.
1189  */
1190 static void
1191 add_name(struct elfdump *ed, const char *name)
1192 {
1193 	struct spec_name *sn;
1194 
1195 	if (find_name(ed, name))
1196 		return;
1197 	if ((sn = malloc(sizeof(*sn))) == NULL) {
1198 		warn("malloc failed");
1199 		return;
1200 	}
1201 	sn->name = name;
1202 	STAILQ_INSERT_TAIL(&ed->snl, sn, sn_list);
1203 }
1204 
1205 /*
1206  * Lookup a name in the '-N' name list.
1207  */
1208 static struct spec_name *
1209 find_name(struct elfdump *ed, const char *name)
1210 {
1211 	struct spec_name *sn;
1212 
1213 	STAILQ_FOREACH(sn, &ed->snl, sn_list) {
1214 		if (!strcmp(sn->name, name))
1215 			return (sn);
1216 	}
1217 
1218 	return (NULL);
1219 }
1220 
1221 /*
1222  * Retrieve the name of a symbol using the section index of the symbol
1223  * table and the index of the symbol within that table.
1224  */
1225 static const char *
1226 get_symbol_name(struct elfdump *ed, uint32_t symtab, int i)
1227 {
1228 	static char	 sname[64];
1229 	struct section	*s;
1230 	const char	*name;
1231 	GElf_Sym	 sym;
1232 	Elf_Data	*data;
1233 	int		 elferr;
1234 
1235 	if (symtab >= ed->shnum)
1236 		return ("");
1237 	s = &ed->sl[symtab];
1238 	if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM)
1239 		return ("");
1240 	(void) elf_errno();
1241 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
1242 		elferr = elf_errno();
1243 		if (elferr != 0)
1244 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
1245 		return ("");
1246 	}
1247 	if (gelf_getsym(data, i, &sym) != &sym)
1248 		return ("");
1249 	if (GELF_ST_TYPE(sym.st_info) == STT_SECTION) {
1250 		if (sym.st_shndx < ed->shnum) {
1251 			snprintf(sname, sizeof(sname), "%s (section)",
1252 			    ed->sl[sym.st_shndx].name);
1253 			return (sname);
1254 		} else
1255 			return ("");
1256 	}
1257 	if ((name = elf_strptr(ed->elf, s->link, sym.st_name)) == NULL)
1258 		return ("");
1259 
1260 	return (name);
1261 }
1262 
1263 /*
1264  * Retrieve a string using string table section index and the string offset.
1265  */
1266 static const char*
1267 get_string(struct elfdump *ed, int strtab, size_t off)
1268 {
1269 	const char *name;
1270 
1271 	if ((name = elf_strptr(ed->elf, strtab, off)) == NULL)
1272 		return ("");
1273 
1274 	return (name);
1275 }
1276 
1277 /*
1278  * Dump the ELF Executable Header.
1279  */
1280 static void
1281 elf_print_ehdr(struct elfdump *ed)
1282 {
1283 
1284 	if (!STAILQ_EMPTY(&ed->snl))
1285 		return;
1286 
1287 	if (ed->flags & SOLARIS_FMT) {
1288 		PRT("\nELF Header\n");
1289 		PRT("  ei_magic:   { %#x, %c, %c, %c }\n",
1290 		    ed->ehdr.e_ident[0], ed->ehdr.e_ident[1],
1291 		    ed->ehdr.e_ident[2], ed->ehdr.e_ident[3]);
1292 		PRT("  ei_class:   %-18s",
1293 		    elf_class_str(ed->ehdr.e_ident[EI_CLASS]));
1294 		PRT("  ei_data:      %s\n",
1295 		    elf_data_str(ed->ehdr.e_ident[EI_DATA]));
1296 		PRT("  e_machine:  %-18s", e_machines(ed->ehdr.e_machine));
1297 		PRT("  e_version:    %s\n",
1298 		    elf_version_str(ed->ehdr.e_version));
1299 		PRT("  e_type:     %s\n", elf_type_str(ed->ehdr.e_type));
1300 		PRT("  e_flags:    %18d\n", ed->ehdr.e_flags);
1301 		PRT("  e_entry:    %#18jx", (uintmax_t)ed->ehdr.e_entry);
1302 		PRT("  e_ehsize: %6d", ed->ehdr.e_ehsize);
1303 		PRT("  e_shstrndx:%5d\n", ed->ehdr.e_shstrndx);
1304 		PRT("  e_shoff:    %#18jx", (uintmax_t)ed->ehdr.e_shoff);
1305 		PRT("  e_shentsize: %3d", ed->ehdr.e_shentsize);
1306 		PRT("  e_shnum:   %5d\n", ed->ehdr.e_shnum);
1307 		PRT("  e_phoff:    %#18jx", (uintmax_t)ed->ehdr.e_phoff);
1308 		PRT("  e_phentsize: %3d", ed->ehdr.e_phentsize);
1309 		PRT("  e_phnum:   %5d\n", ed->ehdr.e_phnum);
1310 	} else {
1311 		PRT("\nelf header:\n");
1312 		PRT("\n");
1313 		PRT("\te_ident: %s %s %s\n",
1314 		    elf_class_str(ed->ehdr.e_ident[EI_CLASS]),
1315 		    elf_data_str(ed->ehdr.e_ident[EI_DATA]),
1316 		    ei_abis[ed->ehdr.e_ident[EI_OSABI]]);
1317 		PRT("\te_type: %s\n", elf_type_str(ed->ehdr.e_type));
1318 		PRT("\te_machine: %s\n", e_machines(ed->ehdr.e_machine));
1319 		PRT("\te_version: %s\n", elf_version_str(ed->ehdr.e_version));
1320 		PRT("\te_entry: %#jx\n", (uintmax_t)ed->ehdr.e_entry);
1321 		PRT("\te_phoff: %ju\n", (uintmax_t)ed->ehdr.e_phoff);
1322 		PRT("\te_shoff: %ju\n", (uintmax_t) ed->ehdr.e_shoff);
1323 		PRT("\te_flags: %u\n", ed->ehdr.e_flags);
1324 		PRT("\te_ehsize: %u\n", ed->ehdr.e_ehsize);
1325 		PRT("\te_phentsize: %u\n", ed->ehdr.e_phentsize);
1326 		PRT("\te_phnum: %u\n", ed->ehdr.e_phnum);
1327 		PRT("\te_shentsize: %u\n", ed->ehdr.e_shentsize);
1328 		PRT("\te_shnum: %u\n", ed->ehdr.e_shnum);
1329 		PRT("\te_shstrndx: %u\n", ed->ehdr.e_shstrndx);
1330 	}
1331 }
1332 
1333 /*
1334  * Dump the ELF Program Header Table.
1335  */
1336 static void
1337 elf_print_phdr(struct elfdump *ed)
1338 {
1339 	GElf_Phdr	 ph;
1340 	size_t		 phnum, i;
1341 	int		 header;
1342 
1343 	if (elf_getphnum(ed->elf, &phnum) == 0) {
1344 		warnx("elf_getphnum failed: %s", elf_errmsg(-1));
1345 		return;
1346 	}
1347 	header = 0;
1348 	for (i = 0; i < phnum; i++) {
1349 		if (gelf_getphdr(ed->elf, i, &ph) != &ph) {
1350 			warnx("elf_getphdr failed: %s", elf_errmsg(-1));
1351 			continue;
1352 		}
1353 		if (!STAILQ_EMPTY(&ed->snl) &&
1354 		    find_name(ed, elf_phdr_type_str(ph.p_type)) == NULL)
1355 			continue;
1356 		if (ed->flags & SOLARIS_FMT) {
1357 			PRT("\nProgram Header[%zu]:\n", i);
1358 			PRT("    p_vaddr:      %#-14jx", (uintmax_t)ph.p_vaddr);
1359 			PRT("  p_flags:    [ %s ]\n",
1360 			    p_flags[ph.p_flags & 0x7]);
1361 			PRT("    p_paddr:      %#-14jx", (uintmax_t)ph.p_paddr);
1362 			PRT("  p_type:     [ %s ]\n",
1363 			    elf_phdr_type_str(ph.p_type));
1364 			PRT("    p_filesz:     %#-14jx",
1365 			    (uintmax_t)ph.p_filesz);
1366 			PRT("  p_memsz:    %#jx\n", (uintmax_t)ph.p_memsz);
1367 			PRT("    p_offset:     %#-14jx",
1368 			    (uintmax_t)ph.p_offset);
1369 			PRT("  p_align:    %#jx\n", (uintmax_t)ph.p_align);
1370 		} else {
1371 			if (!header) {
1372 				PRT("\nprogram header:\n");
1373 				header = 1;
1374 			}
1375 			PRT("\n");
1376 			PRT("entry: %zu\n", i);
1377 			PRT("\tp_type: %s\n", elf_phdr_type_str(ph.p_type));
1378 			PRT("\tp_offset: %ju\n", (uintmax_t)ph.p_offset);
1379 			PRT("\tp_vaddr: %#jx\n", (uintmax_t)ph.p_vaddr);
1380 			PRT("\tp_paddr: %#jx\n", (uintmax_t)ph.p_paddr);
1381 			PRT("\tp_filesz: %ju\n", (uintmax_t)ph.p_filesz);
1382 			PRT("\tp_memsz: %ju\n", (uintmax_t)ph.p_memsz);
1383 			PRT("\tp_flags: %s\n", p_flags[ph.p_flags & 0x7]);
1384 			PRT("\tp_align: %ju\n", (uintmax_t)ph.p_align);
1385 		}
1386 	}
1387 }
1388 
1389 /*
1390  * Dump the ELF Section Header Table.
1391  */
1392 static void
1393 elf_print_shdr(struct elfdump *ed)
1394 {
1395 	struct section *s;
1396 	size_t i;
1397 
1398 	if (!STAILQ_EMPTY(&ed->snl))
1399 		return;
1400 
1401 	if ((ed->flags & SOLARIS_FMT) == 0)
1402 		PRT("\nsection header:\n");
1403 	for (i = 0; i < ed->shnum; i++) {
1404 		s = &ed->sl[i];
1405 		if (ed->flags & SOLARIS_FMT) {
1406 			if (i == 0)
1407 				continue;
1408 			PRT("\nSection Header[%zu]:", i);
1409 			PRT("  sh_name: %s\n", s->name);
1410 			PRT("    sh_addr:      %#-14jx", (uintmax_t)s->addr);
1411 			if (s->flags != 0)
1412 				PRT("  sh_flags:   [ %s ]\n", sh_flags(s->flags));
1413 			else
1414 				PRT("  sh_flags:   0\n");
1415 			PRT("    sh_size:      %#-14jx", (uintmax_t)s->sz);
1416 			PRT("  sh_type:    [ %s ]\n",
1417 			    sh_types(ed->ehdr.e_machine, s->type));
1418 			PRT("    sh_offset:    %#-14jx", (uintmax_t)s->off);
1419 			PRT("  sh_entsize: %#jx\n", (uintmax_t)s->entsize);
1420 			PRT("    sh_link:      %-14u", s->link);
1421 			PRT("  sh_info:    %u\n", s->info);
1422 			PRT("    sh_addralign: %#jx\n", (uintmax_t)s->align);
1423 		} else {
1424 			PRT("\n");
1425 			PRT("entry: %ju\n", (uintmax_t)i);
1426 			PRT("\tsh_name: %s\n", s->name);
1427 			PRT("\tsh_type: %s\n",
1428 			    sh_types(ed->ehdr.e_machine, s->type));
1429 			PRT("\tsh_flags: %s\n", sh_flags(s->flags));
1430 			PRT("\tsh_addr: %#jx\n", (uintmax_t)s->addr);
1431 			PRT("\tsh_offset: %ju\n", (uintmax_t)s->off);
1432 			PRT("\tsh_size: %ju\n", (uintmax_t)s->sz);
1433 			PRT("\tsh_link: %u\n", s->link);
1434 			PRT("\tsh_info: %u\n", s->info);
1435 			PRT("\tsh_addralign: %ju\n", (uintmax_t)s->align);
1436 			PRT("\tsh_entsize: %ju\n", (uintmax_t)s->entsize);
1437 		}
1438 	}
1439 }
1440 
1441 /*
1442  * Return number of entries in the given section. We'd prefer ent_count be a
1443  * size_t, but libelf APIs already use int for section indices.
1444  */
1445 static int
1446 get_ent_count(const struct section *s, int *ent_count)
1447 {
1448 	if (s->entsize == 0) {
1449 		warnx("section %s has entry size 0", s->name);
1450 		return (0);
1451 	} else if (s->sz / s->entsize > INT_MAX) {
1452 		warnx("section %s has invalid section count", s->name);
1453 		return (0);
1454 	}
1455 	*ent_count = (int)(s->sz / s->entsize);
1456 	return (1);
1457 }
1458 
1459 /*
1460  * Retrieve the content of the corresponding SHT_SUNW_versym section for
1461  * a symbol table section.
1462  */
1463 static void
1464 get_versym(struct elfdump *ed, int i, uint16_t **vs, int *nvs)
1465 {
1466 	struct section	*s;
1467 	Elf_Data	*data;
1468 	size_t		 j;
1469 	int		 elferr;
1470 
1471 	s = NULL;
1472 	for (j = 0; j < ed->shnum; j++) {
1473 		s = &ed->sl[j];
1474 		if (s->type == SHT_SUNW_versym && s->link == (uint32_t)i)
1475 			break;
1476 	}
1477 	if (j >= ed->shnum) {
1478 		*vs = NULL;
1479 		return;
1480 	}
1481 	(void) elf_errno();
1482 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
1483 		elferr = elf_errno();
1484 		if (elferr != 0)
1485 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
1486 		*vs = NULL;
1487 		return;
1488 	}
1489 
1490 	*vs = data->d_buf;
1491 	assert(data->d_size == s->sz);
1492 	if (!get_ent_count(s, nvs))
1493 		*nvs = 0;
1494 }
1495 
1496 /*
1497  * Dump the symbol table section.
1498  */
1499 static void
1500 elf_print_symtab(struct elfdump *ed, int i)
1501 {
1502 	struct section	*s;
1503 	const char	*name;
1504 	uint16_t	*vs;
1505 	char		 idx[10];
1506 	Elf_Data	*data;
1507 	GElf_Sym	 sym;
1508 	int		 len, j, elferr, nvs;
1509 
1510 	s = &ed->sl[i];
1511 	if (ed->flags & SOLARIS_FMT)
1512 		PRT("\nSymbol Table Section:  %s\n", s->name);
1513 	else
1514 		PRT("\nsymbol table (%s):\n", s->name);
1515 	(void) elf_errno();
1516 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
1517 		elferr = elf_errno();
1518 		if (elferr != 0)
1519 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
1520 		return;
1521 	}
1522 	vs = NULL;
1523 	nvs = 0;
1524 	assert(data->d_size == s->sz);
1525 	if (!get_ent_count(s, &len))
1526 		return;
1527 	if (ed->flags & SOLARIS_FMT) {
1528 		if (ed->ec == ELFCLASS32)
1529 			PRT("     index    value       ");
1530 		else
1531 			PRT("     index        value           ");
1532 		PRT("size     type bind oth ver shndx       name\n");
1533 		get_versym(ed, i, &vs, &nvs);
1534 		if (vs != NULL && nvs != len) {
1535 			warnx("#symbol not equal to #versym");
1536 			vs = NULL;
1537 		}
1538 	}
1539 	for (j = 0; j < len; j++) {
1540 		if (gelf_getsym(data, j, &sym) != &sym) {
1541 			warnx("gelf_getsym failed: %s", elf_errmsg(-1));
1542 			continue;
1543 		}
1544 		name = get_string(ed, s->link, sym.st_name);
1545 		if (ed->flags & SOLARIS_FMT) {
1546 			snprintf(idx, sizeof(idx), "[%d]", j);
1547 			if (ed->ec == ELFCLASS32)
1548 				PRT("%10s  ", idx);
1549 			else
1550 				PRT("%10s      ", idx);
1551 			PRT("0x%8.8jx ", (uintmax_t)sym.st_value);
1552 			if (ed->ec == ELFCLASS32)
1553 				PRT("0x%8.8jx  ", (uintmax_t)sym.st_size);
1554 			else
1555 				PRT("0x%12.12jx  ", (uintmax_t)sym.st_size);
1556 			PRT("%s ", st_type_S(GELF_ST_TYPE(sym.st_info)));
1557 			PRT("%s  ", st_bindings_S(GELF_ST_BIND(sym.st_info)));
1558 			PRT("%c  ", st_others[sym.st_other]);
1559 			PRT("%3u ", (vs == NULL ? 0 : vs[j]));
1560 			PRT("%-11.11s ", sh_name(ed, sym.st_shndx));
1561 			PRT("%s\n", name);
1562 		} else {
1563 			PRT("\nentry: %d\n", j);
1564 			PRT("\tst_name: %s\n", name);
1565 			PRT("\tst_value: %#jx\n", (uintmax_t)sym.st_value);
1566 			PRT("\tst_size: %ju\n", (uintmax_t)sym.st_size);
1567 			PRT("\tst_info: %s %s\n",
1568 			    st_type(ed->ehdr.e_machine,
1569 			    GELF_ST_TYPE(sym.st_info)),
1570 			    st_bindings(GELF_ST_BIND(sym.st_info)));
1571 			PRT("\tst_shndx: %ju\n", (uintmax_t)sym.st_shndx);
1572 		}
1573 	}
1574 }
1575 
1576 /*
1577  * Dump the symbol tables. (.dynsym and .symtab)
1578  */
1579 static void
1580 elf_print_symtabs(struct elfdump *ed)
1581 {
1582 	size_t i;
1583 
1584 	for (i = 0; i < ed->shnum; i++)
1585 		if ((ed->sl[i].type == SHT_SYMTAB ||
1586 		    ed->sl[i].type == SHT_DYNSYM) &&
1587 		    (STAILQ_EMPTY(&ed->snl) || find_name(ed, ed->sl[i].name)))
1588 			elf_print_symtab(ed, i);
1589 }
1590 
1591 /*
1592  * Dump the content of .dynamic section.
1593  */
1594 static void
1595 elf_print_dynamic(struct elfdump *ed)
1596 {
1597 	struct section	*s;
1598 	const char	*name;
1599 	char		 idx[10];
1600 	Elf_Data	*data;
1601 	GElf_Dyn	 dyn;
1602 	int		 elferr, i, len;
1603 
1604 	s = NULL;
1605 	for (i = 0; (size_t)i < ed->shnum; i++) {
1606 		s = &ed->sl[i];
1607 		if (s->type == SHT_DYNAMIC &&
1608 		    (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name)))
1609 			break;
1610 	}
1611 	if ((size_t)i >= ed->shnum)
1612 		return;
1613 
1614 	if (ed->flags & SOLARIS_FMT) {
1615 		PRT("Dynamic Section:  %s\n", s->name);
1616 		PRT("     index  tag               value\n");
1617 	} else
1618 		PRT("\ndynamic:\n");
1619 	(void) elf_errno();
1620 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
1621 		elferr = elf_errno();
1622 		if (elferr != 0)
1623 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
1624 		return;
1625 	}
1626 	assert(data->d_size == s->sz);
1627 	if (!get_ent_count(s, &len))
1628 		return;
1629 	for (i = 0; i < len; i++) {
1630 		if (gelf_getdyn(data, i, &dyn) != &dyn) {
1631 			warnx("gelf_getdyn failed: %s", elf_errmsg(-1));
1632 			continue;
1633 		}
1634 
1635 		if (ed->flags & SOLARIS_FMT) {
1636 			snprintf(idx, sizeof(idx), "[%d]", i);
1637 			PRT("%10s  %-16s ", idx, d_tags(dyn.d_tag));
1638 		} else {
1639 			PRT("\n");
1640 			PRT("entry: %d\n", i);
1641 			PRT("\td_tag: %s\n", d_tags(dyn.d_tag));
1642 		}
1643 		switch(dyn.d_tag) {
1644 		case DT_NEEDED:
1645 		case DT_SONAME:
1646 		case DT_RPATH:
1647 		case DT_RUNPATH:
1648 			if ((name = elf_strptr(ed->elf, s->link,
1649 				    dyn.d_un.d_val)) == NULL)
1650 				name = "";
1651 			if (ed->flags & SOLARIS_FMT)
1652 				PRT("%#-16jx %s\n", (uintmax_t)dyn.d_un.d_val,
1653 				    name);
1654 			else
1655 				PRT("\td_val: %s\n", name);
1656 			break;
1657 		case DT_PLTRELSZ:
1658 		case DT_RELA:
1659 		case DT_RELASZ:
1660 		case DT_RELAENT:
1661 		case DT_RELACOUNT:
1662 		case DT_STRSZ:
1663 		case DT_SYMENT:
1664 		case DT_RELSZ:
1665 		case DT_RELENT:
1666 		case DT_PLTREL:
1667 		case DT_VERDEF:
1668 		case DT_VERDEFNUM:
1669 		case DT_VERNEED:
1670 		case DT_VERNEEDNUM:
1671 		case DT_VERSYM:
1672 			if (ed->flags & SOLARIS_FMT)
1673 				PRT("%#jx\n", (uintmax_t)dyn.d_un.d_val);
1674 			else
1675 				PRT("\td_val: %ju\n",
1676 				    (uintmax_t)dyn.d_un.d_val);
1677 			break;
1678 		case DT_PLTGOT:
1679 		case DT_HASH:
1680 		case DT_GNU_HASH:
1681 		case DT_STRTAB:
1682 		case DT_SYMTAB:
1683 		case DT_INIT:
1684 		case DT_FINI:
1685 		case DT_REL:
1686 		case DT_JMPREL:
1687 		case DT_DEBUG:
1688 			if (ed->flags & SOLARIS_FMT)
1689 				PRT("%#jx\n", (uintmax_t)dyn.d_un.d_ptr);
1690 			else
1691 				PRT("\td_ptr: %#jx\n",
1692 				    (uintmax_t)dyn.d_un.d_ptr);
1693 			break;
1694 		case DT_NULL:
1695 		case DT_SYMBOLIC:
1696 		case DT_TEXTREL:
1697 		default:
1698 			if (ed->flags & SOLARIS_FMT)
1699 				PRT("\n");
1700 			break;
1701 		}
1702 	}
1703 }
1704 
1705 /*
1706  * Dump a .rel/.rela section entry.
1707  */
1708 static void
1709 elf_print_rel_entry(struct elfdump *ed, struct section *s, int j,
1710     struct rel_entry *r)
1711 {
1712 
1713 	if (ed->flags & SOLARIS_FMT) {
1714 		PRT("        %-23s ", elftc_reloc_type_str(ed->ehdr.e_machine,
1715 			GELF_R_TYPE(r->u_r.rel.r_info)));
1716 		PRT("%#12jx ", (uintmax_t)r->u_r.rel.r_offset);
1717 		if (r->type == SHT_RELA)
1718 			PRT("%10jd  ", (intmax_t)r->u_r.rela.r_addend);
1719 		else
1720 			PRT("    ");
1721 		PRT("%-14s ", s->name);
1722 		PRT("%s\n", r->symn);
1723 	} else {
1724 		PRT("\n");
1725 		PRT("entry: %d\n", j);
1726 		PRT("\tr_offset: %#jx\n", (uintmax_t)r->u_r.rel.r_offset);
1727 		if (ed->ec == ELFCLASS32)
1728 			PRT("\tr_info: %#jx\n", (uintmax_t)
1729 			    ELF32_R_INFO(ELF64_R_SYM(r->u_r.rel.r_info),
1730 			    ELF64_R_TYPE(r->u_r.rel.r_info)));
1731 		else
1732 			PRT("\tr_info: %#jx\n", (uintmax_t)r->u_r.rel.r_info);
1733 		if (r->type == SHT_RELA)
1734 			PRT("\tr_addend: %jd\n",
1735 			    (intmax_t)r->u_r.rela.r_addend);
1736 	}
1737 }
1738 
1739 /*
1740  * Dump a relocation section of type SHT_RELA.
1741  */
1742 static void
1743 elf_print_rela(struct elfdump *ed, struct section *s, Elf_Data *data)
1744 {
1745 	struct rel_entry	r;
1746 	int			j, len;
1747 
1748 	if (ed->flags & SOLARIS_FMT) {
1749 		PRT("\nRelocation Section:  %s\n", s->name);
1750 		PRT("        type                          offset     "
1751 		    "addend  section        with respect to\n");
1752 	} else
1753 		PRT("\nrelocation with addend (%s):\n", s->name);
1754 	r.type = SHT_RELA;
1755 	assert(data->d_size == s->sz);
1756 	if (!get_ent_count(s, &len))
1757 		return;
1758 	for (j = 0; j < len; j++) {
1759 		if (gelf_getrela(data, j, &r.u_r.rela) != &r.u_r.rela) {
1760 			warnx("gelf_getrela failed: %s",
1761 			    elf_errmsg(-1));
1762 			continue;
1763 		}
1764 		r.symn = get_symbol_name(ed, s->link,
1765 		    GELF_R_SYM(r.u_r.rela.r_info));
1766 		elf_print_rel_entry(ed, s, j, &r);
1767 	}
1768 }
1769 
1770 /*
1771  * Dump a relocation section of type SHT_REL.
1772  */
1773 static void
1774 elf_print_rel(struct elfdump *ed, struct section *s, Elf_Data *data)
1775 {
1776 	struct rel_entry	r;
1777 	int			j, len;
1778 
1779 	if (ed->flags & SOLARIS_FMT) {
1780 		PRT("\nRelocation Section:  %s\n", s->name);
1781 		PRT("        type                          offset     "
1782 		    "section        with respect to\n");
1783 	} else
1784 		PRT("\nrelocation (%s):\n", s->name);
1785 	r.type = SHT_REL;
1786 	assert(data->d_size == s->sz);
1787 	if (!get_ent_count(s, &len))
1788 		return;
1789 	for (j = 0; j < len; j++) {
1790 		if (gelf_getrel(data, j, &r.u_r.rel) != &r.u_r.rel) {
1791 			warnx("gelf_getrel failed: %s", elf_errmsg(-1));
1792 			continue;
1793 		}
1794 		r.symn = get_symbol_name(ed, s->link,
1795 		    GELF_R_SYM(r.u_r.rel.r_info));
1796 		elf_print_rel_entry(ed, s, j, &r);
1797 	}
1798 }
1799 
1800 /*
1801  * Dump relocation sections.
1802  */
1803 static void
1804 elf_print_reloc(struct elfdump *ed)
1805 {
1806 	struct section	*s;
1807 	Elf_Data	*data;
1808 	size_t		 i;
1809 	int		 elferr;
1810 
1811 	for (i = 0; i < ed->shnum; i++) {
1812 		s = &ed->sl[i];
1813 		if ((s->type == SHT_REL || s->type == SHT_RELA) &&
1814 		    (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name))) {
1815 			(void) elf_errno();
1816 			if ((data = elf_getdata(s->scn, NULL)) == NULL) {
1817 				elferr = elf_errno();
1818 				if (elferr != 0)
1819 					warnx("elf_getdata failed: %s",
1820 					    elf_errmsg(elferr));
1821 				continue;
1822 			}
1823 			if (s->type == SHT_REL)
1824 				elf_print_rel(ed, s, data);
1825 			else
1826 				elf_print_rela(ed, s, data);
1827 		}
1828 	}
1829 }
1830 
1831 /*
1832  * Dump the content of PT_INTERP segment.
1833  */
1834 static void
1835 elf_print_interp(struct elfdump *ed)
1836 {
1837 	const char *s;
1838 	GElf_Phdr phdr;
1839 	size_t filesize, i, phnum;
1840 
1841 	if (!STAILQ_EMPTY(&ed->snl) && find_name(ed, "PT_INTERP") == NULL)
1842 		return;
1843 
1844 	if ((s = elf_rawfile(ed->elf, &filesize)) == NULL) {
1845 		warnx("elf_rawfile failed: %s", elf_errmsg(-1));
1846 		return;
1847 	}
1848 	if (!elf_getphnum(ed->elf, &phnum)) {
1849 		warnx("elf_getphnum failed: %s", elf_errmsg(-1));
1850 		return;
1851 	}
1852 	for (i = 0; i < phnum; i++) {
1853 		if (gelf_getphdr(ed->elf, i, &phdr) != &phdr) {
1854 			warnx("elf_getphdr failed: %s", elf_errmsg(-1));
1855 			continue;
1856 		}
1857 		if (phdr.p_type == PT_INTERP) {
1858 			if (phdr.p_offset >= filesize) {
1859 				warnx("invalid phdr offset");
1860 				continue;
1861 			}
1862 			PRT("\ninterp:\n");
1863 			PRT("\t%s\n", s + phdr.p_offset);
1864 		}
1865 	}
1866 }
1867 
1868 /*
1869  * Search the relocation sections for entries referring to the .got section.
1870  */
1871 static void
1872 find_gotrel(struct elfdump *ed, struct section *gs, struct rel_entry *got)
1873 {
1874 	struct section		*s;
1875 	struct rel_entry	 r;
1876 	Elf_Data		*data;
1877 	size_t			 i;
1878 	int			 elferr, j, k, len;
1879 
1880 	for(i = 0; i < ed->shnum; i++) {
1881 		s = &ed->sl[i];
1882 		if (s->type != SHT_REL && s->type != SHT_RELA)
1883 			continue;
1884 		(void) elf_errno();
1885 		if ((data = elf_getdata(s->scn, NULL)) == NULL) {
1886 			elferr = elf_errno();
1887 			if (elferr != 0)
1888 				warnx("elf_getdata failed: %s",
1889 				    elf_errmsg(elferr));
1890 			return;
1891 		}
1892 		memset(&r, 0, sizeof(struct rel_entry));
1893 		r.type = s->type;
1894 		assert(data->d_size == s->sz);
1895 		if (!get_ent_count(s, &len))
1896 			return;
1897 		for (j = 0; j < len; j++) {
1898 			if (s->type == SHT_REL) {
1899 				if (gelf_getrel(data, j, &r.u_r.rel) !=
1900 				    &r.u_r.rel) {
1901 					warnx("gelf_getrel failed: %s",
1902 					    elf_errmsg(-1));
1903 					continue;
1904 				}
1905 			} else {
1906 				if (gelf_getrela(data, j, &r.u_r.rela) !=
1907 				    &r.u_r.rela) {
1908 					warnx("gelf_getrel failed: %s",
1909 					    elf_errmsg(-1));
1910 					continue;
1911 				}
1912 			}
1913 			if (r.u_r.rel.r_offset >= gs->addr &&
1914 			    r.u_r.rel.r_offset < gs->addr + gs->sz) {
1915 				r.symn = get_symbol_name(ed, s->link,
1916 				    GELF_R_SYM(r.u_r.rel.r_info));
1917 				k = (r.u_r.rel.r_offset - gs->addr) /
1918 				    gs->entsize;
1919 				memcpy(&got[k], &r, sizeof(struct rel_entry));
1920 			}
1921 		}
1922 	}
1923 }
1924 
1925 static void
1926 elf_print_got_section(struct elfdump *ed, struct section *s)
1927 {
1928 	struct rel_entry	*got;
1929 	Elf_Data		*data, dst;
1930 	int			 elferr, i, len;
1931 
1932 	if (s->entsize == 0) {
1933 		/* XXX IA64 GOT section generated by gcc has entry size 0. */
1934 		if (s->align != 0)
1935 			s->entsize = s->align;
1936 		else
1937 			return;
1938 	}
1939 
1940 	if (!get_ent_count(s, &len))
1941 		return;
1942 	if (ed->flags & SOLARIS_FMT)
1943 		PRT("\nGlobal Offset Table Section:  %s  (%d entries)\n",
1944 		    s->name, len);
1945 	else
1946 		PRT("\nglobal offset table: %s\n", s->name);
1947 	(void) elf_errno();
1948 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
1949 		elferr = elf_errno();
1950 		if (elferr != 0)
1951 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
1952 		return;
1953 	}
1954 
1955 	/*
1956 	 * GOT section has section type SHT_PROGBITS, thus libelf treats it as
1957 	 * byte stream and will not perform any translation on it. As a result,
1958 	 * an exlicit call to gelf_xlatetom is needed here. Depends on arch,
1959 	 * GOT section should be translated to either WORD or XWORD.
1960 	 */
1961 	if (ed->ec == ELFCLASS32)
1962 		data->d_type = ELF_T_WORD;
1963 	else
1964 		data->d_type = ELF_T_XWORD;
1965 	memcpy(&dst, data, sizeof(Elf_Data));
1966 	if (gelf_xlatetom(ed->elf, &dst, data, ed->ehdr.e_ident[EI_DATA]) !=
1967 	    &dst) {
1968 		warnx("gelf_xlatetom failed: %s", elf_errmsg(-1));
1969 		return;
1970 	}
1971 	assert(dst.d_size == s->sz);
1972 	if (ed->flags & SOLARIS_FMT) {
1973 		/*
1974 		 * In verbose/Solaris mode, we search the relocation sections
1975 		 * and try to find the corresponding reloc entry for each GOT
1976 		 * section entry.
1977 		 */
1978 		if ((got = calloc(len, sizeof(struct rel_entry))) == NULL)
1979 			err(EXIT_FAILURE, "calloc failed");
1980 		find_gotrel(ed, s, got);
1981 		if (ed->ec == ELFCLASS32) {
1982 			PRT(" ndx     addr      value    reloc              ");
1983 			PRT("addend   symbol\n");
1984 		} else {
1985 			PRT(" ndx     addr              value             ");
1986 			PRT("reloc              addend       symbol\n");
1987 		}
1988 		for(i = 0; i < len; i++) {
1989 			PRT("[%5.5d]  ", i);
1990 			if (ed->ec == ELFCLASS32) {
1991 				PRT("%-8.8jx  ",
1992 				    (uintmax_t) (s->addr + i * s->entsize));
1993 				PRT("%-8.8x ", *((uint32_t *)dst.d_buf + i));
1994 			} else {
1995 				PRT("%-16.16jx  ",
1996 				    (uintmax_t) (s->addr + i * s->entsize));
1997 				PRT("%-16.16jx  ",
1998 				    (uintmax_t) *((uint64_t *)dst.d_buf + i));
1999 			}
2000 			PRT("%-18s ", elftc_reloc_type_str(ed->ehdr.e_machine,
2001 				GELF_R_TYPE(got[i].u_r.rel.r_info)));
2002 			if (ed->ec == ELFCLASS32)
2003 				PRT("%-8.8jd ",
2004 				    (intmax_t)got[i].u_r.rela.r_addend);
2005 			else
2006 				PRT("%-12.12jd ",
2007 				    (intmax_t)got[i].u_r.rela.r_addend);
2008 			if (got[i].symn == NULL)
2009 				got[i].symn = "";
2010 			PRT("%s\n", got[i].symn);
2011 		}
2012 		free(got);
2013 	} else {
2014 		for(i = 0; i < len; i++) {
2015 			PRT("\nentry: %d\n", i);
2016 			if (ed->ec == ELFCLASS32)
2017 				PRT("\t%#x\n", *((uint32_t *)dst.d_buf + i));
2018 			else
2019 				PRT("\t%#jx\n",
2020 				    (uintmax_t) *((uint64_t *)dst.d_buf + i));
2021 		}
2022 	}
2023 }
2024 
2025 /*
2026  * Dump the content of Global Offset Table section.
2027  */
2028 static void
2029 elf_print_got(struct elfdump *ed)
2030 {
2031 	struct section	*s;
2032 	size_t		 i;
2033 
2034 	if (!STAILQ_EMPTY(&ed->snl))
2035 		return;
2036 
2037 	s = NULL;
2038 	for (i = 0; i < ed->shnum; i++) {
2039 		s = &ed->sl[i];
2040 		if (s->name && !strncmp(s->name, ".got", 4) &&
2041 		    (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name)))
2042 			elf_print_got_section(ed, s);
2043 	}
2044 }
2045 
2046 /*
2047  * Dump the content of .note.ABI-tag section.
2048  */
2049 static void
2050 elf_print_note(struct elfdump *ed)
2051 {
2052 	struct section	*s;
2053 	Elf_Data        *data;
2054 	Elf_Note	*en;
2055 	uint32_t	 namesz;
2056 	uint32_t	 descsz;
2057 	uint32_t	 desc;
2058 	size_t		 count;
2059 	int		 elferr, i;
2060 	uint8_t		*src;
2061 	char		 idx[10];
2062 
2063 	s = NULL;
2064 	for (i = 0; (size_t)i < ed->shnum; i++) {
2065 		s = &ed->sl[i];
2066 		if (s->type == SHT_NOTE && s->name &&
2067 		    !strcmp(s->name, ".note.ABI-tag") &&
2068 		    (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name)))
2069 			break;
2070 	}
2071 	if ((size_t)i >= ed->shnum)
2072 		return;
2073 	if (ed->flags & SOLARIS_FMT)
2074 		PRT("\nNote Section:  %s\n", s->name);
2075 	else
2076 		PRT("\nnote (%s):\n", s->name);
2077 	(void) elf_errno();
2078 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
2079 		elferr = elf_errno();
2080 		if (elferr != 0)
2081 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
2082 		return;
2083 	}
2084 	src = data->d_buf;
2085 	count = data->d_size;
2086 	while (count > sizeof(Elf_Note)) {
2087 		en = (Elf_Note *) (uintptr_t) src;
2088 		namesz = en->n_namesz;
2089 		descsz = en->n_descsz;
2090 		src += sizeof(Elf_Note);
2091 		count -= sizeof(Elf_Note);
2092 		if (roundup2(namesz, 4) + roundup2(descsz, 4) > count) {
2093 			warnx("truncated note section");
2094 			return;
2095 		}
2096 		if (ed->flags & SOLARIS_FMT) {
2097 			PRT("\n    type   %#x\n", en->n_type);
2098 			PRT("    namesz %#x:\n", en->n_namesz);
2099 			PRT("%s\n", src);
2100 		} else
2101 			PRT("\t%s ", src);
2102 		src += roundup2(namesz, 4);
2103 		count -= roundup2(namesz, 4);
2104 
2105 		/*
2106 		 * Note that we dump the whole desc part if we're in
2107 		 * "Solaris mode", while in the normal mode, we only look
2108 		 * at the first 4 bytes (a 32bit word) of the desc, i.e,
2109 		 * we assume that it's always a FreeBSD version number.
2110 		 */
2111 		if (ed->flags & SOLARIS_FMT) {
2112 			PRT("    descsz %#x:", en->n_descsz);
2113 			for (i = 0; (uint32_t)i < descsz; i++) {
2114 				if ((i & 0xF) == 0) {
2115 					snprintf(idx, sizeof(idx), "desc[%d]",
2116 					    i);
2117 					PRT("\n      %-9s", idx);
2118 				} else if ((i & 0x3) == 0)
2119 					PRT("  ");
2120 				PRT(" %2.2x", src[i]);
2121 			}
2122 			PRT("\n");
2123 		} else {
2124 			if (ed->ehdr.e_ident[EI_DATA] == ELFDATA2MSB)
2125 				desc = be32dec(src);
2126 			else
2127 				desc = le32dec(src);
2128 			PRT("%d\n", desc);
2129 		}
2130 		src += roundup2(descsz, 4);
2131 		count -= roundup2(descsz, 4);
2132 	}
2133 }
2134 
2135 /*
2136  * Dump a hash table.
2137  */
2138 static void
2139 elf_print_svr4_hash(struct elfdump *ed, struct section *s)
2140 {
2141 	Elf_Data	*data;
2142 	uint32_t	*buf;
2143 	uint32_t	*bucket, *chain;
2144 	uint32_t	 nbucket, nchain;
2145 	uint32_t	*bl, *c, maxl, total;
2146 	uint32_t	 i, j;
2147 	int		 first, elferr;
2148 	char		 idx[10];
2149 
2150 	if (ed->flags & SOLARIS_FMT)
2151 		PRT("\nHash Section:  %s\n", s->name);
2152 	else
2153 		PRT("\nhash table (%s):\n", s->name);
2154 	(void) elf_errno();
2155 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
2156 		elferr = elf_errno();
2157 		if (elferr != 0)
2158 			warnx("elf_getdata failed: %s",
2159 			    elf_errmsg(elferr));
2160 		return;
2161 	}
2162 	if (data->d_size < 2 * sizeof(uint32_t)) {
2163 		warnx(".hash section too small");
2164 		return;
2165 	}
2166 	buf = data->d_buf;
2167 	nbucket = buf[0];
2168 	nchain = buf[1];
2169 	if (nbucket <= 0 || nchain <= 0) {
2170 		warnx("Malformed .hash section");
2171 		return;
2172 	}
2173 	if (data->d_size !=
2174 	    ((uint64_t)nbucket + (uint64_t)nchain + 2) * sizeof(uint32_t)) {
2175 		warnx("Malformed .hash section");
2176 		return;
2177 	}
2178 	bucket = &buf[2];
2179 	chain = &buf[2 + nbucket];
2180 
2181 	if (ed->flags & SOLARIS_FMT) {
2182 		maxl = 0;
2183 		if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
2184 			err(EXIT_FAILURE, "calloc failed");
2185 		for (i = 0; i < nbucket; i++)
2186 			for (j = bucket[i]; j > 0 && j < nchain; j = chain[j])
2187 				if (++bl[i] > maxl)
2188 					maxl = bl[i];
2189 		if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
2190 			err(EXIT_FAILURE, "calloc failed");
2191 		for (i = 0; i < nbucket; i++)
2192 			c[bl[i]]++;
2193 		PRT("    bucket    symndx    name\n");
2194 		for (i = 0; i < nbucket; i++) {
2195 			first = 1;
2196 			for (j = bucket[i]; j > 0 && j < nchain; j = chain[j]) {
2197 				if (first) {
2198 					PRT("%10d  ", i);
2199 					first = 0;
2200 				} else
2201 					PRT("            ");
2202 				snprintf(idx, sizeof(idx), "[%d]", j);
2203 				PRT("%-10s  ", idx);
2204 				PRT("%s\n", get_symbol_name(ed, s->link, j));
2205 			}
2206 		}
2207 		PRT("\n");
2208 		total = 0;
2209 		for (i = 0; i <= maxl; i++) {
2210 			total += c[i] * i;
2211 			PRT("%10u  buckets contain %8d symbols\n", c[i], i);
2212 		}
2213 		PRT("%10u  buckets         %8u symbols (globals)\n", nbucket,
2214 		    total);
2215 	} else {
2216 		PRT("\nnbucket: %u\n", nbucket);
2217 		PRT("nchain: %u\n\n", nchain);
2218 		for (i = 0; i < nbucket; i++)
2219 			PRT("bucket[%d]:\n\t%u\n\n", i, bucket[i]);
2220 		for (i = 0; i < nchain; i++)
2221 			PRT("chain[%d]:\n\t%u\n\n", i, chain[i]);
2222 	}
2223 }
2224 
2225 /*
2226  * Dump a 64bit hash table.
2227  */
2228 static void
2229 elf_print_svr4_hash64(struct elfdump *ed, struct section *s)
2230 {
2231 	Elf_Data	*data, dst;
2232 	uint64_t	*buf;
2233 	uint64_t	*bucket, *chain;
2234 	uint64_t	 nbucket, nchain;
2235 	uint64_t	*bl, *c, maxl, total;
2236 	uint64_t	 i, j;
2237 	int		 elferr, first;
2238 	char		 idx[10];
2239 
2240 	if (ed->flags & SOLARIS_FMT)
2241 		PRT("\nHash Section:  %s\n", s->name);
2242 	else
2243 		PRT("\nhash table (%s):\n", s->name);
2244 
2245 	/*
2246 	 * ALPHA uses 64-bit hash entries. Since libelf assumes that
2247 	 * .hash section contains only 32-bit entry, an explicit
2248 	 * gelf_xlatetom is needed here.
2249 	 */
2250 	(void) elf_errno();
2251 	if ((data = elf_rawdata(s->scn, NULL)) == NULL) {
2252 		elferr = elf_errno();
2253 		if (elferr != 0)
2254 			warnx("elf_rawdata failed: %s",
2255 			    elf_errmsg(elferr));
2256 		return;
2257 	}
2258 	data->d_type = ELF_T_XWORD;
2259 	memcpy(&dst, data, sizeof(Elf_Data));
2260 	if (gelf_xlatetom(ed->elf, &dst, data,
2261 		ed->ehdr.e_ident[EI_DATA]) != &dst) {
2262 		warnx("gelf_xlatetom failed: %s", elf_errmsg(-1));
2263 		return;
2264 	}
2265 	if (dst.d_size < 2 * sizeof(uint64_t)) {
2266 		warnx(".hash section too small");
2267 		return;
2268 	}
2269 	buf = dst.d_buf;
2270 	nbucket = buf[0];
2271 	nchain = buf[1];
2272 	if (nbucket <= 0 || nchain <= 0) {
2273 		warnx("Malformed .hash section");
2274 		return;
2275 	}
2276 	if (dst.d_size != (nbucket + nchain + 2) * sizeof(uint64_t)) {
2277 		warnx("Malformed .hash section");
2278 		return;
2279 	}
2280 	bucket = &buf[2];
2281 	chain = &buf[2 + nbucket];
2282 
2283 	if (ed->flags & SOLARIS_FMT) {
2284 		maxl = 0;
2285 		if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
2286 			err(EXIT_FAILURE, "calloc failed");
2287 		for (i = 0; i < nbucket; i++)
2288 			for (j = bucket[i]; j > 0 && j < nchain; j = chain[j])
2289 				if (++bl[i] > maxl)
2290 					maxl = bl[i];
2291 		if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
2292 			err(EXIT_FAILURE, "calloc failed");
2293 		for (i = 0; i < nbucket; i++)
2294 			c[bl[i]]++;
2295 		PRT("    bucket    symndx    name\n");
2296 		for (i = 0; i < nbucket; i++) {
2297 			first = 1;
2298 			for (j = bucket[i]; j > 0 && j < nchain; j = chain[j]) {
2299 				if (first) {
2300 					PRT("%10zu  ", i);
2301 					first = 0;
2302 				} else
2303 					PRT("            ");
2304 				snprintf(idx, sizeof(idx), "[%zu]", (size_t)j);
2305 				PRT("%-10s  ", idx);
2306 				PRT("%s\n", get_symbol_name(ed, s->link, j));
2307 			}
2308 		}
2309 		PRT("\n");
2310 		total = 0;
2311 		for (i = 0; i <= maxl; i++) {
2312 			total += c[i] * i;
2313 			PRT("%10ju  buckets contain %8zu symbols\n",
2314 			    (uintmax_t)c[i], i);
2315 		}
2316 		PRT("%10ju  buckets         %8ju symbols (globals)\n",
2317 		    (uintmax_t)nbucket, (uintmax_t)total);
2318 	} else {
2319 		PRT("\nnbucket: %ju\n", (uintmax_t)nbucket);
2320 		PRT("nchain: %ju\n\n", (uintmax_t)nchain);
2321 		for (i = 0; i < nbucket; i++)
2322 			PRT("bucket[%zu]:\n\t%ju\n\n", i, (uintmax_t)bucket[i]);
2323 		for (i = 0; i < nchain; i++)
2324 			PRT("chain[%zu]:\n\t%ju\n\n", i, (uintmax_t)chain[i]);
2325 	}
2326 
2327 }
2328 
2329 /*
2330  * Dump a GNU hash table.
2331  */
2332 static void
2333 elf_print_gnu_hash(struct elfdump *ed, struct section *s)
2334 {
2335 	struct section	*ds;
2336 	Elf_Data	*data;
2337 	uint32_t	*buf;
2338 	uint32_t	*bucket, *chain;
2339 	uint32_t	 nbucket, nchain, symndx, maskwords, shift2;
2340 	uint32_t	*bl, *c, maxl, total;
2341 	uint32_t	 i, j;
2342 	int		 first, elferr, dynsymcount;
2343 	char		 idx[10];
2344 
2345 	if (ed->flags & SOLARIS_FMT)
2346 		PRT("\nGNU Hash Section:  %s\n", s->name);
2347 	else
2348 		PRT("\ngnu hash table (%s):\n", s->name);
2349 	(void) elf_errno();
2350 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
2351 		elferr = elf_errno();
2352 		if (elferr != 0)
2353 			warnx("elf_getdata failed: %s",
2354 			    elf_errmsg(elferr));
2355 		return;
2356 	}
2357 	if (data->d_size < 4 * sizeof(uint32_t)) {
2358 		warnx(".gnu.hash section too small");
2359 		return;
2360 	}
2361 	buf = data->d_buf;
2362 	nbucket = buf[0];
2363 	symndx = buf[1];
2364 	maskwords = buf[2];
2365 	shift2 = buf[3];
2366 	buf += 4;
2367 	if (s->link >= ed->shnum) {
2368 		warnx("Malformed .gnu.hash section");
2369 		return;
2370 	}
2371 	ds = &ed->sl[s->link];
2372 	if (!get_ent_count(ds, &dynsymcount))
2373 		return;
2374 	if (symndx >= (uint32_t)dynsymcount) {
2375 		warnx("Malformed .gnu.hash section");
2376 		return;
2377 	}
2378 	nchain = dynsymcount - symndx;
2379 	if (data->d_size != 4 * sizeof(uint32_t) + maskwords *
2380 	    (ed->ec == ELFCLASS32 ? sizeof(uint32_t) : sizeof(uint64_t)) +
2381 	    ((uint64_t)nbucket + (uint64_t)nchain) * sizeof(uint32_t)) {
2382 		warnx("Malformed .gnu.hash section");
2383 		return;
2384 	}
2385 	bucket = buf + (ed->ec == ELFCLASS32 ? maskwords : maskwords * 2);
2386 	chain = bucket + nbucket;
2387 
2388 	if (ed->flags & SOLARIS_FMT) {
2389 		maxl = 0;
2390 		if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
2391 			err(EXIT_FAILURE, "calloc failed");
2392 		for (i = 0; i < nbucket; i++)
2393 			for (j = bucket[i]; j > 0 && j - symndx < nchain; j++) {
2394 				if (++bl[i] > maxl)
2395 					maxl = bl[i];
2396 				if (chain[j - symndx] & 1)
2397 					break;
2398 			}
2399 		if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
2400 			err(EXIT_FAILURE, "calloc failed");
2401 		for (i = 0; i < nbucket; i++)
2402 			c[bl[i]]++;
2403 		PRT("    bucket    symndx    name\n");
2404 		for (i = 0; i < nbucket; i++) {
2405 			first = 1;
2406 			for (j = bucket[i]; j > 0 && j - symndx < nchain; j++) {
2407 				if (first) {
2408 					PRT("%10d  ", i);
2409 					first = 0;
2410 				} else
2411 					PRT("            ");
2412 				snprintf(idx, sizeof(idx), "[%d]", j );
2413 				PRT("%-10s  ", idx);
2414 				PRT("%s\n", get_symbol_name(ed, s->link, j));
2415 				if (chain[j - symndx] & 1)
2416 					break;
2417 			}
2418 		}
2419 		PRT("\n");
2420 		total = 0;
2421 		for (i = 0; i <= maxl; i++) {
2422 			total += c[i] * i;
2423 			PRT("%10u  buckets contain %8d symbols\n", c[i], i);
2424 		}
2425 		PRT("%10u  buckets         %8u symbols (globals)\n", nbucket,
2426 		    total);
2427 	} else {
2428 		PRT("\nnbucket: %u\n", nbucket);
2429 		PRT("symndx: %u\n", symndx);
2430 		PRT("maskwords: %u\n", maskwords);
2431 		PRT("shift2: %u\n", shift2);
2432 		PRT("nchain: %u\n\n", nchain);
2433 		for (i = 0; i < nbucket; i++)
2434 			PRT("bucket[%d]:\n\t%u\n\n", i, bucket[i]);
2435 		for (i = 0; i < nchain; i++)
2436 			PRT("chain[%d]:\n\t%u\n\n", i, chain[i]);
2437 	}
2438 }
2439 
2440 /*
2441  * Dump hash tables.
2442  */
2443 static void
2444 elf_print_hash(struct elfdump *ed)
2445 {
2446 	struct section	*s;
2447 	size_t		 i;
2448 
2449 	for (i = 0; i < ed->shnum; i++) {
2450 		s = &ed->sl[i];
2451 		if ((s->type == SHT_HASH || s->type == SHT_GNU_HASH) &&
2452 		    (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name))) {
2453 			if (s->type == SHT_GNU_HASH)
2454 				elf_print_gnu_hash(ed, s);
2455 			else if (ed->ehdr.e_machine == EM_ALPHA &&
2456 			    s->entsize == 8)
2457 				elf_print_svr4_hash64(ed, s);
2458 			else
2459 				elf_print_svr4_hash(ed, s);
2460 		}
2461 	}
2462 }
2463 
2464 /*
2465  * Dump the content of a Version Definition(SHT_SUNW_Verdef) Section.
2466  */
2467 static void
2468 elf_print_verdef(struct elfdump *ed, struct section *s)
2469 {
2470 	Elf_Data	*data;
2471 	Elf32_Verdef	*vd;
2472 	Elf32_Verdaux	*vda;
2473 	const char 	*str;
2474 	char		 idx[10];
2475 	uint8_t		*buf, *end, *buf2;
2476 	int		 i, j, elferr, count;
2477 
2478 	if (ed->flags & SOLARIS_FMT)
2479 		PRT("Version Definition Section:  %s\n", s->name);
2480 	else
2481 		PRT("\nversion definition section (%s):\n", s->name);
2482 	(void) elf_errno();
2483 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
2484 		elferr = elf_errno();
2485 		if (elferr != 0)
2486 			warnx("elf_getdata failed: %s",
2487 			    elf_errmsg(elferr));
2488 		return;
2489 	}
2490 	buf = data->d_buf;
2491 	end = buf + data->d_size;
2492 	i = 0;
2493 	if (ed->flags & SOLARIS_FMT)
2494 		PRT("     index  version                     dependency\n");
2495 	while (buf + sizeof(Elf32_Verdef) <= end) {
2496 		vd = (Elf32_Verdef *) (uintptr_t) buf;
2497 		if (ed->flags & SOLARIS_FMT) {
2498 			snprintf(idx, sizeof(idx), "[%d]", vd->vd_ndx);
2499 			PRT("%10s  ", idx);
2500 		} else {
2501 			PRT("\nentry: %d\n", i++);
2502 			PRT("\tvd_version: %u\n", vd->vd_version);
2503 			PRT("\tvd_flags: %u\n", vd->vd_flags);
2504 			PRT("\tvd_ndx: %u\n", vd->vd_ndx);
2505 			PRT("\tvd_cnt: %u\n", vd->vd_cnt);
2506 			PRT("\tvd_hash: %u\n", vd->vd_hash);
2507 			PRT("\tvd_aux: %u\n", vd->vd_aux);
2508 			PRT("\tvd_next: %u\n\n", vd->vd_next);
2509 		}
2510 		buf2 = buf + vd->vd_aux;
2511 		j = 0;
2512 		count = 0;
2513 		while (buf2 + sizeof(Elf32_Verdaux) <= end && j < vd->vd_cnt) {
2514 			vda = (Elf32_Verdaux *) (uintptr_t) buf2;
2515 			str = get_string(ed, s->link, vda->vda_name);
2516 			if (ed->flags & SOLARIS_FMT) {
2517 				if (count == 0)
2518 					PRT("%-26.26s", str);
2519 				else if (count == 1)
2520 					PRT("  %-20.20s", str);
2521 				else {
2522 					PRT("\n%40.40s", "");
2523 					PRT("%s", str);
2524 				}
2525 			} else {
2526 				PRT("\t\tvda: %d\n", j++);
2527 				PRT("\t\t\tvda_name: %s\n", str);
2528 				PRT("\t\t\tvda_next: %u\n", vda->vda_next);
2529 			}
2530 			if (vda->vda_next == 0) {
2531 				if (ed->flags & SOLARIS_FMT) {
2532 					if (vd->vd_flags & VER_FLG_BASE) {
2533 						if (count == 0)
2534 							PRT("%-20.20s", "");
2535 						PRT("%s", "[ BASE ]");
2536 					}
2537 					PRT("\n");
2538 				}
2539 				break;
2540 			}
2541 			if (ed->flags & SOLARIS_FMT)
2542 				count++;
2543 			buf2 += vda->vda_next;
2544 		}
2545 		if (vd->vd_next == 0)
2546 			break;
2547 		buf += vd->vd_next;
2548 	}
2549 }
2550 
2551 /*
2552  * Dump the content of a Version Needed(SHT_SUNW_Verneed) Section.
2553  */
2554 static void
2555 elf_print_verneed(struct elfdump *ed, struct section *s)
2556 {
2557 	Elf_Data	*data;
2558 	Elf32_Verneed	*vn;
2559 	Elf32_Vernaux	*vna;
2560 	uint8_t		*buf, *end, *buf2;
2561 	int		 i, j, elferr, first;
2562 
2563 	if (ed->flags & SOLARIS_FMT)
2564 		PRT("\nVersion Needed Section:  %s\n", s->name);
2565 	else
2566 		PRT("\nversion need section (%s):\n", s->name);
2567 	(void) elf_errno();
2568 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
2569 		elferr = elf_errno();
2570 		if (elferr != 0)
2571 			warnx("elf_getdata failed: %s",
2572 			    elf_errmsg(elferr));
2573 		return;
2574 	}
2575 	buf = data->d_buf;
2576 	end = buf + data->d_size;
2577 	if (ed->flags & SOLARIS_FMT)
2578 		PRT("            file                        version\n");
2579 	i = 0;
2580 	while (buf + sizeof(Elf32_Verneed) <= end) {
2581 		vn = (Elf32_Verneed *) (uintptr_t) buf;
2582 		if (ed->flags & SOLARIS_FMT)
2583 			PRT("            %-26.26s  ",
2584 			    get_string(ed, s->link, vn->vn_file));
2585 		else {
2586 			PRT("\nentry: %d\n", i++);
2587 			PRT("\tvn_version: %u\n", vn->vn_version);
2588 			PRT("\tvn_cnt: %u\n", vn->vn_cnt);
2589 			PRT("\tvn_file: %s\n",
2590 			    get_string(ed, s->link, vn->vn_file));
2591 			PRT("\tvn_aux: %u\n", vn->vn_aux);
2592 			PRT("\tvn_next: %u\n\n", vn->vn_next);
2593 		}
2594 		buf2 = buf + vn->vn_aux;
2595 		j = 0;
2596 		first = 1;
2597 		while (buf2 + sizeof(Elf32_Vernaux) <= end && j < vn->vn_cnt) {
2598 			vna = (Elf32_Vernaux *) (uintptr_t) buf2;
2599 			if (ed->flags & SOLARIS_FMT) {
2600 				if (!first)
2601 					PRT("%40.40s", "");
2602 				else
2603 					first = 0;
2604 				PRT("%s\n", get_string(ed, s->link,
2605 				    vna->vna_name));
2606 			} else {
2607 				PRT("\t\tvna: %d\n", j++);
2608 				PRT("\t\t\tvna_hash: %u\n", vna->vna_hash);
2609 				PRT("\t\t\tvna_flags: %u\n", vna->vna_flags);
2610 				PRT("\t\t\tvna_other: %u\n", vna->vna_other);
2611 				PRT("\t\t\tvna_name: %s\n",
2612 				    get_string(ed, s->link, vna->vna_name));
2613 				PRT("\t\t\tvna_next: %u\n", vna->vna_next);
2614 			}
2615 			if (vna->vna_next == 0)
2616 				break;
2617 			buf2 += vna->vna_next;
2618 		}
2619 		if (vn->vn_next == 0)
2620 			break;
2621 		buf += vn->vn_next;
2622 	}
2623 }
2624 
2625 /*
2626  * Dump the symbol-versioning sections.
2627  */
2628 static void
2629 elf_print_symver(struct elfdump *ed)
2630 {
2631 	struct section	*s;
2632 	size_t		 i;
2633 
2634 	for (i = 0; i < ed->shnum; i++) {
2635 		s = &ed->sl[i];
2636 		if (!STAILQ_EMPTY(&ed->snl) && !find_name(ed, s->name))
2637 			continue;
2638 		if (s->type == SHT_SUNW_verdef)
2639 			elf_print_verdef(ed, s);
2640 		if (s->type == SHT_SUNW_verneed)
2641 			elf_print_verneed(ed, s);
2642 	}
2643 }
2644 
2645 /*
2646  * Dump the ELF checksum. See gelf_checksum(3) for details.
2647  */
2648 static void
2649 elf_print_checksum(struct elfdump *ed)
2650 {
2651 
2652 	if (!STAILQ_EMPTY(&ed->snl))
2653 		return;
2654 
2655 	PRT("\nelf checksum: %#lx\n", gelf_checksum(ed->elf));
2656 }
2657 
2658 #define	USAGE_MESSAGE	"\
2659 Usage: %s [options] file...\n\
2660   Display information about ELF objects and ar(1) archives.\n\n\
2661   Options:\n\
2662   -a                        Show all information.\n\
2663   -c                        Show shared headers.\n\
2664   -d                        Show dynamic symbols.\n\
2665   -e                        Show the ELF header.\n\
2666   -G                        Show the GOT.\n\
2667   -H | --help               Show a usage message and exit.\n\
2668   -h                        Show hash values.\n\
2669   -i                        Show the dynamic interpreter.\n\
2670   -k                        Show the ELF checksum.\n\
2671   -n                        Show the contents of note sections.\n\
2672   -N NAME                   Show the section named \"NAME\".\n\
2673   -p                        Show the program header.\n\
2674   -r                        Show relocations.\n\
2675   -s                        Show the symbol table.\n\
2676   -S                        Use the Solaris elfdump format.\n\
2677   -v                        Show symbol-versioning information.\n\
2678   -V | --version            Print a version identifier and exit.\n\
2679   -w FILE                   Write output to \"FILE\".\n"
2680 
2681 static void
2682 usage(void)
2683 {
2684 	fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());
2685 	exit(EXIT_FAILURE);
2686 }
2687