xref: /freebsd/contrib/elftoolchain/elfdump/elfdump.c (revision 715d1396d6233158b94e85414b57520ee3e536cc)
13fe401a5SEd Maste /*-
23fe401a5SEd Maste  * Copyright (c) 2007-2012 Kai Wang
33fe401a5SEd Maste  * Copyright (c) 2003 David O'Brien.  All rights reserved.
43fe401a5SEd Maste  * Copyright (c) 2001 Jake Burkholder
53fe401a5SEd Maste  * All rights reserved.
63fe401a5SEd Maste  *
73fe401a5SEd Maste  * Redistribution and use in source and binary forms, with or without
83fe401a5SEd Maste  * modification, are permitted provided that the following conditions
93fe401a5SEd Maste  * are met:
103fe401a5SEd Maste  * 1. Redistributions of source code must retain the above copyright
113fe401a5SEd Maste  *    notice, this list of conditions and the following disclaimer.
123fe401a5SEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
133fe401a5SEd Maste  *    notice, this list of conditions and the following disclaimer in the
143fe401a5SEd Maste  *    documentation and/or other materials provided with the distribution.
153fe401a5SEd Maste  *
163fe401a5SEd Maste  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
173fe401a5SEd Maste  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
183fe401a5SEd Maste  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
193fe401a5SEd Maste  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
203fe401a5SEd Maste  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
213fe401a5SEd Maste  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
223fe401a5SEd Maste  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
233fe401a5SEd Maste  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
243fe401a5SEd Maste  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
253fe401a5SEd Maste  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
263fe401a5SEd Maste  * SUCH DAMAGE.
273fe401a5SEd Maste  */
283fe401a5SEd Maste 
293fe401a5SEd Maste #include <sys/param.h>
303fe401a5SEd Maste #include <sys/queue.h>
313fe401a5SEd Maste #include <sys/stat.h>
323fe401a5SEd Maste 
333fe401a5SEd Maste #include <ar.h>
343fe401a5SEd Maste #include <assert.h>
353fe401a5SEd Maste #include <err.h>
363fe401a5SEd Maste #include <fcntl.h>
373fe401a5SEd Maste #include <gelf.h>
383fe401a5SEd Maste #include <getopt.h>
393fe401a5SEd Maste #include <libelftc.h>
403fe401a5SEd Maste #include <inttypes.h>
413fe401a5SEd Maste #include <stdio.h>
423fe401a5SEd Maste #include <stdlib.h>
433fe401a5SEd Maste #include <string.h>
443fe401a5SEd Maste #include <unistd.h>
453fe401a5SEd Maste 
463fe401a5SEd Maste #ifdef USE_LIBARCHIVE_AR
473fe401a5SEd Maste #include <archive.h>
483fe401a5SEd Maste #include <archive_entry.h>
493fe401a5SEd Maste #endif
503fe401a5SEd Maste 
513fe401a5SEd Maste #include "_elftc.h"
523fe401a5SEd Maste 
53*715d1396SEd Maste ELFTC_VCSID("$Id: elfdump.c 3584 2017-11-05 20:51:43Z jkoshy $");
543fe401a5SEd Maste 
553fe401a5SEd Maste #if defined(ELFTC_NEED_ELF_NOTE_DEFINITION)
563fe401a5SEd Maste #include "native-elf-format.h"
573fe401a5SEd Maste #if ELFTC_CLASS == ELFCLASS32
583fe401a5SEd Maste typedef Elf32_Nhdr	Elf_Note;
593fe401a5SEd Maste #else
603fe401a5SEd Maste typedef Elf64_Nhdr	Elf_Note;
613fe401a5SEd Maste #endif
623fe401a5SEd Maste #endif
633fe401a5SEd Maste 
643fe401a5SEd Maste /* elfdump(1) options. */
653fe401a5SEd Maste #define	ED_DYN		(1<<0)
663fe401a5SEd Maste #define	ED_EHDR		(1<<1)
673fe401a5SEd Maste #define	ED_GOT		(1<<2)
683fe401a5SEd Maste #define	ED_HASH		(1<<3)
693fe401a5SEd Maste #define	ED_INTERP	(1<<4)
703fe401a5SEd Maste #define	ED_NOTE		(1<<5)
713fe401a5SEd Maste #define	ED_PHDR		(1<<6)
723fe401a5SEd Maste #define	ED_REL		(1<<7)
733fe401a5SEd Maste #define	ED_SHDR		(1<<8)
743fe401a5SEd Maste #define	ED_SYMTAB	(1<<9)
753fe401a5SEd Maste #define	ED_SYMVER	(1<<10)
763fe401a5SEd Maste #define	ED_CHECKSUM	(1<<11)
773fe401a5SEd Maste #define	ED_ALL		((1<<12)-1)
783fe401a5SEd Maste 
793fe401a5SEd Maste /* elfdump(1) run control flags. */
803fe401a5SEd Maste #define	SOLARIS_FMT		(1<<0)
813fe401a5SEd Maste #define	PRINT_FILENAME		(1<<1)
823fe401a5SEd Maste #define	PRINT_ARSYM		(1<<2)
833fe401a5SEd Maste #define	ONLY_ARSYM		(1<<3)
843fe401a5SEd Maste 
853fe401a5SEd Maste /* Convenient print macro. */
863fe401a5SEd Maste #define	PRT(...)	fprintf(ed->out, __VA_ARGS__)
873fe401a5SEd Maste 
883fe401a5SEd Maste /* Internal data structure for sections. */
893fe401a5SEd Maste struct section {
903fe401a5SEd Maste 	const char	*name;		/* section name */
913fe401a5SEd Maste 	Elf_Scn		*scn;		/* section scn */
923fe401a5SEd Maste 	uint64_t	 off;		/* section offset */
933fe401a5SEd Maste 	uint64_t	 sz;		/* section size */
943fe401a5SEd Maste 	uint64_t	 entsize;	/* section entsize */
953fe401a5SEd Maste 	uint64_t	 align;		/* section alignment */
963fe401a5SEd Maste 	uint64_t	 type;		/* section type */
973fe401a5SEd Maste 	uint64_t	 flags;		/* section flags */
983fe401a5SEd Maste 	uint64_t	 addr;		/* section virtual addr */
993fe401a5SEd Maste 	uint32_t	 link;		/* section link ndx */
1003fe401a5SEd Maste 	uint32_t	 info;		/* section info ndx */
1013fe401a5SEd Maste };
1023fe401a5SEd Maste 
1033fe401a5SEd Maste struct spec_name {
1043fe401a5SEd Maste 	const char	*name;
1053fe401a5SEd Maste 	STAILQ_ENTRY(spec_name)	sn_list;
1063fe401a5SEd Maste };
1073fe401a5SEd Maste 
1083fe401a5SEd Maste /* Structure encapsulates the global data for readelf(1). */
1093fe401a5SEd Maste struct elfdump {
1103fe401a5SEd Maste 	FILE		*out;		/* output redirection. */
1113fe401a5SEd Maste 	const char	*filename;	/* current processing file. */
1123fe401a5SEd Maste 	const char	*archive;	/* archive name */
1133fe401a5SEd Maste 	int		 options;	/* command line options. */
1143fe401a5SEd Maste 	int		 flags;		/* run control flags. */
1153fe401a5SEd Maste 	Elf		*elf;		/* underlying ELF descriptor. */
1163fe401a5SEd Maste #ifndef USE_LIBARCHIVE_AR
1173fe401a5SEd Maste 	Elf		*ar;		/* ar(1) archive descriptor. */
1183fe401a5SEd Maste #endif
1193fe401a5SEd Maste 	GElf_Ehdr	 ehdr;		/* ELF header. */
1203fe401a5SEd Maste 	int		 ec;		/* ELF class. */
1213fe401a5SEd Maste 	size_t		 shnum;		/* #sections. */
1223fe401a5SEd Maste 	struct section	*sl;		/* list of sections. */
1233fe401a5SEd Maste 	STAILQ_HEAD(, spec_name) snl;	/* list of names specified by -N. */
1243fe401a5SEd Maste };
1253fe401a5SEd Maste 
1263fe401a5SEd Maste /* Relocation entry. */
1273fe401a5SEd Maste struct rel_entry {
1283fe401a5SEd Maste 	union {
1293fe401a5SEd Maste 		GElf_Rel rel;
1303fe401a5SEd Maste 		GElf_Rela rela;
1313fe401a5SEd Maste 	} u_r;
1323fe401a5SEd Maste 	const char *symn;
1333fe401a5SEd Maste 	uint32_t type;
1343fe401a5SEd Maste };
1353fe401a5SEd Maste 
1363fe401a5SEd Maste #if defined(ELFTC_NEED_BYTEORDER_EXTENSIONS)
1373fe401a5SEd Maste static __inline uint32_t
1383fe401a5SEd Maste be32dec(const void *pp)
1393fe401a5SEd Maste {
1403fe401a5SEd Maste 	unsigned char const *p = (unsigned char const *)pp;
1413fe401a5SEd Maste 
1423fe401a5SEd Maste 	return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
1433fe401a5SEd Maste }
1443fe401a5SEd Maste 
1453fe401a5SEd Maste static __inline uint32_t
1463fe401a5SEd Maste le32dec(const void *pp)
1473fe401a5SEd Maste {
1483fe401a5SEd Maste 	unsigned char const *p = (unsigned char const *)pp;
1493fe401a5SEd Maste 
1503fe401a5SEd Maste 	return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
1513fe401a5SEd Maste }
1523fe401a5SEd Maste #endif
1533fe401a5SEd Maste 
1543fe401a5SEd Maste /* http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#tag_encodings */
1553fe401a5SEd Maste static const char *
1563fe401a5SEd Maste d_tags(uint64_t tag)
1573fe401a5SEd Maste {
158839529caSEd Maste 	static char unknown_buf[64];
159839529caSEd Maste 
1603fe401a5SEd Maste 	switch (tag) {
161839529caSEd Maste 	case DT_NULL:		return "DT_NULL";
162839529caSEd Maste 	case DT_NEEDED:		return "DT_NEEDED";
163839529caSEd Maste 	case DT_PLTRELSZ:	return "DT_PLTRELSZ";
164839529caSEd Maste 	case DT_PLTGOT:		return "DT_PLTGOT";
165839529caSEd Maste 	case DT_HASH:		return "DT_HASH";
166839529caSEd Maste 	case DT_STRTAB:		return "DT_STRTAB";
167839529caSEd Maste 	case DT_SYMTAB:		return "DT_SYMTAB";
168839529caSEd Maste 	case DT_RELA:		return "DT_RELA";
169839529caSEd Maste 	case DT_RELASZ:		return "DT_RELASZ";
170839529caSEd Maste 	case DT_RELAENT:	return "DT_RELAENT";
171839529caSEd Maste 	case DT_STRSZ:		return "DT_STRSZ";
172839529caSEd Maste 	case DT_SYMENT:		return "DT_SYMENT";
173839529caSEd Maste 	case DT_INIT:		return "DT_INIT";
174839529caSEd Maste 	case DT_FINI:		return "DT_FINI";
175839529caSEd Maste 	case DT_SONAME:		return "DT_SONAME";
176839529caSEd Maste 	case DT_RPATH:		return "DT_RPATH";
177839529caSEd Maste 	case DT_SYMBOLIC:	return "DT_SYMBOLIC";
178839529caSEd Maste 	case DT_REL:		return "DT_REL";
179839529caSEd Maste 	case DT_RELSZ:		return "DT_RELSZ";
180839529caSEd Maste 	case DT_RELENT:		return "DT_RELENT";
181839529caSEd Maste 	case DT_PLTREL:		return "DT_PLTREL";
182839529caSEd Maste 	case DT_DEBUG:		return "DT_DEBUG";
183839529caSEd Maste 	case DT_TEXTREL:	return "DT_TEXTREL";
184839529caSEd Maste 	case DT_JMPREL:		return "DT_JMPREL";
185839529caSEd Maste 	case DT_BIND_NOW:	return "DT_BIND_NOW";
186839529caSEd Maste 	case DT_INIT_ARRAY:	return "DT_INIT_ARRAY";
187839529caSEd Maste 	case DT_FINI_ARRAY:	return "DT_FINI_ARRAY";
188839529caSEd Maste 	case DT_INIT_ARRAYSZ:	return "DT_INIT_ARRAYSZ";
189839529caSEd Maste 	case DT_FINI_ARRAYSZ:	return "DT_FINI_ARRAYSZ";
190839529caSEd Maste 	case DT_RUNPATH:	return "DT_RUNPATH";
191839529caSEd Maste 	case DT_FLAGS:		return "DT_FLAGS";
192839529caSEd Maste 	case DT_PREINIT_ARRAY:	return "DT_PREINIT_ARRAY"; /* XXX DT_ENCODING */
193839529caSEd Maste 	case DT_PREINIT_ARRAYSZ:return "DT_PREINIT_ARRAYSZ";
1943fe401a5SEd Maste 	/* 0x6000000D - 0x6ffff000 operating system-specific semantics */
1953fe401a5SEd Maste 	case 0x6ffffdf5:	return "DT_GNU_PRELINKED";
1963fe401a5SEd Maste 	case 0x6ffffdf6:	return "DT_GNU_CONFLICTSZ";
1973fe401a5SEd Maste 	case 0x6ffffdf7:	return "DT_GNU_LIBLISTSZ";
1983fe401a5SEd Maste 	case 0x6ffffdf8:	return "DT_SUNW_CHECKSUM";
199839529caSEd Maste 	case DT_PLTPADSZ:	return "DT_PLTPADSZ";
200839529caSEd Maste 	case DT_MOVEENT:	return "DT_MOVEENT";
201839529caSEd Maste 	case DT_MOVESZ:		return "DT_MOVESZ";
2023fe401a5SEd Maste 	case 0x6ffffdfc:	return "DT_FEATURE";
203839529caSEd Maste 	case DT_POSFLAG_1:	return "DT_POSFLAG_1";
204839529caSEd Maste 	case DT_SYMINSZ:	return "DT_SYMINSZ";
205839529caSEd Maste 	case DT_SYMINENT:	return "DT_SYMINENT (DT_VALRNGHI)";
206839529caSEd Maste 	case DT_ADDRRNGLO:	return "DT_ADDRRNGLO";
207839529caSEd Maste 	case DT_GNU_HASH:	return "DT_GNU_HASH";
2083fe401a5SEd Maste 	case 0x6ffffef8:	return "DT_GNU_CONFLICT";
2093fe401a5SEd Maste 	case 0x6ffffef9:	return "DT_GNU_LIBLIST";
210656f49f8SEd Maste 	case 0x6ffffefa:	return "DT_CONFIG";
211656f49f8SEd Maste 	case 0x6ffffefb:	return "DT_DEPAUDIT";
212656f49f8SEd Maste 	case 0x6ffffefc:	return "DT_AUDIT";
213656f49f8SEd Maste 	case 0x6ffffefd:	return "DT_PLTPAD";
214656f49f8SEd Maste 	case 0x6ffffefe:	return "DT_MOVETAB";
215839529caSEd Maste 	case DT_SYMINFO:	return "DT_SYMINFO (DT_ADDRRNGHI)";
216839529caSEd Maste 	case DT_RELACOUNT:	return "DT_RELACOUNT";
217839529caSEd Maste 	case DT_RELCOUNT:	return "DT_RELCOUNT";
218839529caSEd Maste 	case DT_FLAGS_1:	return "DT_FLAGS_1";
219839529caSEd Maste 	case DT_VERDEF:		return "DT_VERDEF";
220839529caSEd Maste 	case DT_VERDEFNUM:	return "DT_VERDEFNUM";
221839529caSEd Maste 	case DT_VERNEED:	return "DT_VERNEED";
222839529caSEd Maste 	case DT_VERNEEDNUM:	return "DT_VERNEEDNUM";
2233fe401a5SEd Maste 	case 0x6ffffff0:	return "DT_GNU_VERSYM";
2243fe401a5SEd Maste 	/* 0x70000000 - 0x7fffffff processor-specific semantics */
2253fe401a5SEd Maste 	case 0x70000000:	return "DT_IA_64_PLT_RESERVE";
226bee2765cSEd Maste 	case DT_AUXILIARY:	return "DT_AUXILIARY";
227bee2765cSEd Maste 	case DT_USED:		return "DT_USED";
228bee2765cSEd Maste 	case DT_FILTER:		return "DT_FILTER";
2293fe401a5SEd Maste 	}
230839529caSEd Maste 
231839529caSEd Maste 	snprintf(unknown_buf, sizeof(unknown_buf),
232839529caSEd Maste 		"<unknown: %#llx>", (unsigned long long)tag);
233839529caSEd Maste 	return (unknown_buf);
2343fe401a5SEd Maste }
2353fe401a5SEd Maste 
2363fe401a5SEd Maste static const char *
2373fe401a5SEd Maste e_machines(unsigned int mach)
2383fe401a5SEd Maste {
2393fe401a5SEd Maste 	static char machdesc[64];
2403fe401a5SEd Maste 
2413fe401a5SEd Maste 	switch (mach) {
2423fe401a5SEd Maste 	case EM_NONE:	return "EM_NONE";
2433fe401a5SEd Maste 	case EM_M32:	return "EM_M32";
2443fe401a5SEd Maste 	case EM_SPARC:	return "EM_SPARC";
2453fe401a5SEd Maste 	case EM_386:	return "EM_386";
2463fe401a5SEd Maste 	case EM_68K:	return "EM_68K";
2473fe401a5SEd Maste 	case EM_88K:	return "EM_88K";
2483fe401a5SEd Maste 	case EM_IAMCU:	return "EM_IAMCU";
2493fe401a5SEd Maste 	case EM_860:	return "EM_860";
2503fe401a5SEd Maste 	case EM_MIPS:	return "EM_MIPS";
2513fe401a5SEd Maste 	case EM_PPC:	return "EM_PPC";
252656f49f8SEd Maste 	case EM_PPC64:	return "EM_PPC64";
2533fe401a5SEd Maste 	case EM_ARM:	return "EM_ARM";
2543fe401a5SEd Maste 	case EM_ALPHA:	return "EM_ALPHA (legacy)";
2553fe401a5SEd Maste 	case EM_SPARCV9:return "EM_SPARCV9";
2563fe401a5SEd Maste 	case EM_IA_64:	return "EM_IA_64";
2573fe401a5SEd Maste 	case EM_X86_64:	return "EM_X86_64";
258656f49f8SEd Maste 	case EM_AARCH64:return "EM_AARCH64";
259656f49f8SEd Maste 	case EM_RISCV:	return "EM_RISCV";
2603fe401a5SEd Maste 	}
2613fe401a5SEd Maste 	snprintf(machdesc, sizeof(machdesc),
2623fe401a5SEd Maste 	    "(unknown machine) -- type 0x%x", mach);
2633fe401a5SEd Maste 	return (machdesc);
2643fe401a5SEd Maste }
2653fe401a5SEd Maste 
266b6b6f9ccSEd Maste static const char *
267b6b6f9ccSEd Maste elf_type_str(unsigned int type)
268b6b6f9ccSEd Maste {
269b6b6f9ccSEd Maste 	static char s_type[32];
2703fe401a5SEd Maste 
271b6b6f9ccSEd Maste 	switch (type)
272b6b6f9ccSEd Maste 	{
273b6b6f9ccSEd Maste 	case ET_NONE:	return "ET_NONE";
274b6b6f9ccSEd Maste 	case ET_REL:	return "ET_REL";
275b6b6f9ccSEd Maste 	case ET_EXEC:	return "ET_EXEC";
276b6b6f9ccSEd Maste 	case ET_DYN:	return "ET_DYN";
277b6b6f9ccSEd Maste 	case ET_CORE:	return "ET_CORE";
278b6b6f9ccSEd Maste 	}
279b6b6f9ccSEd Maste 	if (type >= ET_LOPROC)
280b6b6f9ccSEd Maste 		snprintf(s_type, sizeof(s_type), "<proc: %#x>", type);
281b6b6f9ccSEd Maste 	else if (type >= ET_LOOS && type <= ET_HIOS)
282b6b6f9ccSEd Maste 		snprintf(s_type, sizeof(s_type), "<os: %#x>", type);
283b6b6f9ccSEd Maste 	else
284b6b6f9ccSEd Maste 		snprintf(s_type, sizeof(s_type), "<unknown: %#x", type);
285b6b6f9ccSEd Maste 	return (s_type);
286b6b6f9ccSEd Maste }
2873fe401a5SEd Maste 
288b6b6f9ccSEd Maste static const char *
289b6b6f9ccSEd Maste elf_version_str(unsigned int ver)
290b6b6f9ccSEd Maste {
291b6b6f9ccSEd Maste 	static char s_ver[32];
2923fe401a5SEd Maste 
293b6b6f9ccSEd Maste 	switch (ver) {
294b6b6f9ccSEd Maste 	case EV_NONE:		return "EV_NONE";
295b6b6f9ccSEd Maste 	case EV_CURRENT:	return "EV_CURRENT";
296b6b6f9ccSEd Maste 	}
297b6b6f9ccSEd Maste 	snprintf(s_ver, sizeof(s_ver), "<unknown: %#x>", ver);
298b6b6f9ccSEd Maste 	return (s_ver);
299b6b6f9ccSEd Maste }
300b6b6f9ccSEd Maste 
301b6b6f9ccSEd Maste static const char *
302b6b6f9ccSEd Maste elf_class_str(unsigned int class)
303b6b6f9ccSEd Maste {
304b6b6f9ccSEd Maste 	static char s_class[32];
305b6b6f9ccSEd Maste 
306b6b6f9ccSEd Maste 	switch (class) {
307b6b6f9ccSEd Maste 	case ELFCLASSNONE:	return "ELFCLASSNONE";
308b6b6f9ccSEd Maste 	case ELFCLASS32:	return "ELFCLASS32";
309b6b6f9ccSEd Maste 	case ELFCLASS64:	return "ELFCLASS64";
310b6b6f9ccSEd Maste 	}
311b6b6f9ccSEd Maste 	snprintf(s_class, sizeof(s_class), "<unknown: %#x>", class);
312b6b6f9ccSEd Maste 	return (s_class);
313b6b6f9ccSEd Maste }
314b6b6f9ccSEd Maste 
315b6b6f9ccSEd Maste static const char *
316b6b6f9ccSEd Maste elf_data_str(unsigned int data)
317b6b6f9ccSEd Maste {
318b6b6f9ccSEd Maste 	static char s_data[32];
319b6b6f9ccSEd Maste 
320b6b6f9ccSEd Maste 	switch (data) {
321b6b6f9ccSEd Maste 	case ELFDATANONE:	return "ELFDATANONE";
322b6b6f9ccSEd Maste 	case ELFDATA2LSB:	return "ELFDATA2LSB";
323b6b6f9ccSEd Maste 	case ELFDATA2MSB:	return "ELFDATA2MSB";
324b6b6f9ccSEd Maste 	}
325b6b6f9ccSEd Maste 	snprintf(s_data, sizeof(s_data), "<unknown: %#x>", data);
326b6b6f9ccSEd Maste 	return (s_data);
327b6b6f9ccSEd Maste }
3283fe401a5SEd Maste 
329656f49f8SEd Maste static const char *ei_abis[256] = {
330453b09caSEd Maste 	"ELFOSABI_NONE", "ELFOSABI_HPUX", "ELFOSABI_NETBSD", "ELFOSABI_LINUX",
331656f49f8SEd Maste 	"ELFOSABI_HURD", "ELFOSABI_86OPEN", "ELFOSABI_SOLARIS", "ELFOSABI_AIX",
332656f49f8SEd Maste 	"ELFOSABI_IRIX", "ELFOSABI_FREEBSD", "ELFOSABI_TRU64",
333656f49f8SEd Maste 	"ELFOSABI_MODESTO", "ELFOSABI_OPENBSD",
334b6b6f9ccSEd Maste 	[17] = "ELFOSABI_CLOUDABI",
335b6d812d2SEd Maste 	[64] = "ELFOSABI_ARM_AEABI",
336b6d812d2SEd Maste 	[97] = "ELFOSABI_ARM",
337656f49f8SEd Maste 	[255] = "ELFOSABI_STANDALONE"
3383fe401a5SEd Maste };
3393fe401a5SEd Maste 
340b6b6f9ccSEd Maste static const char *
341b6b6f9ccSEd Maste elf_phdr_type_str(unsigned int type)
342b6b6f9ccSEd Maste {
343b6b6f9ccSEd Maste 	static char s_type[32];
344b6b6f9ccSEd Maste 
345b6b6f9ccSEd Maste 	switch (type) {
346b6b6f9ccSEd Maste 	case PT_NULL:		return "PT_NULL";
347b6b6f9ccSEd Maste 	case PT_LOAD:		return "PT_LOAD";
348b6b6f9ccSEd Maste 	case PT_DYNAMIC:	return "PT_DYNAMIC";
349b6b6f9ccSEd Maste 	case PT_INTERP:		return "PT_INTERP";
350b6b6f9ccSEd Maste 	case PT_NOTE:		return "PT_NOTE";
351b6b6f9ccSEd Maste 	case PT_SHLIB:		return "PT_SHLIB";
352b6b6f9ccSEd Maste 	case PT_PHDR:		return "PT_PHDR";
353b6b6f9ccSEd Maste 	case PT_TLS:		return "PT_TLS";
354b6b6f9ccSEd Maste 	case PT_GNU_EH_FRAME:	return "PT_GNU_EH_FRAME";
355b6b6f9ccSEd Maste 	case PT_GNU_STACK:	return "PT_GNU_STACK";
356b6b6f9ccSEd Maste 	case PT_GNU_RELRO:	return "PT_GNU_RELRO";
357b6b6f9ccSEd Maste 	}
358b6b6f9ccSEd Maste 	snprintf(s_type, sizeof(s_type), "<unknown: %#x>", type);
359b6b6f9ccSEd Maste 	return (s_type);
360b6b6f9ccSEd Maste }
3613fe401a5SEd Maste 
3623fe401a5SEd Maste static const char *p_flags[] = {
3633fe401a5SEd Maste 	"", "PF_X", "PF_W", "PF_X|PF_W", "PF_R", "PF_X|PF_R", "PF_W|PF_R",
3643fe401a5SEd Maste 	"PF_X|PF_W|PF_R"
3653fe401a5SEd Maste };
3663fe401a5SEd Maste 
3673fe401a5SEd Maste static const char *
3683fe401a5SEd Maste sh_name(struct elfdump *ed, int ndx)
3693fe401a5SEd Maste {
3703fe401a5SEd Maste 	static char num[10];
3713fe401a5SEd Maste 
3723fe401a5SEd Maste 	switch (ndx) {
3733fe401a5SEd Maste 	case SHN_UNDEF: return "UNDEF";
3743fe401a5SEd Maste 	case SHN_ABS: return "ABS";
3753fe401a5SEd Maste 	case SHN_COMMON: return "COMMON";
3763fe401a5SEd Maste 	default:
3773fe401a5SEd Maste 		if ((uint64_t)ndx < ed->shnum)
3783fe401a5SEd Maste 			return (ed->sl[ndx].name);
3793fe401a5SEd Maste 		else {
3803fe401a5SEd Maste 			snprintf(num, sizeof(num), "%d", ndx);
3813fe401a5SEd Maste 			return (num);
3823fe401a5SEd Maste 		}
3833fe401a5SEd Maste 	}
3843fe401a5SEd Maste }
3853fe401a5SEd Maste 
3863fe401a5SEd Maste /* http://www.sco.com/developers/gabi/latest/ch4.sheader.html#sh_type */
3873fe401a5SEd Maste static const char *
388839529caSEd Maste sh_types(uint64_t mach, uint64_t sht) {
389839529caSEd Maste 	static char unknown_buf[64];
390839529caSEd Maste 
391839529caSEd Maste 	if (sht < 0x60000000) {
3923fe401a5SEd Maste 		switch (sht) {
393839529caSEd Maste 		case SHT_NULL:		return "SHT_NULL";
394839529caSEd Maste 		case SHT_PROGBITS:	return "SHT_PROGBITS";
395839529caSEd Maste 		case SHT_SYMTAB:	return "SHT_SYMTAB";
396839529caSEd Maste 		case SHT_STRTAB:	return "SHT_STRTAB";
397839529caSEd Maste 		case SHT_RELA:		return "SHT_RELA";
398839529caSEd Maste 		case SHT_HASH:		return "SHT_HASH";
399839529caSEd Maste 		case SHT_DYNAMIC:	return "SHT_DYNAMIC";
400839529caSEd Maste 		case SHT_NOTE:		return "SHT_NOTE";
401839529caSEd Maste 		case SHT_NOBITS:	return "SHT_NOBITS";
402839529caSEd Maste 		case SHT_REL:		return "SHT_REL";
403839529caSEd Maste 		case SHT_SHLIB:		return "SHT_SHLIB";
404839529caSEd Maste 		case SHT_DYNSYM:	return "SHT_DYNSYM";
405839529caSEd Maste 		case SHT_INIT_ARRAY:	return "SHT_INIT_ARRAY";
406839529caSEd Maste 		case SHT_FINI_ARRAY:	return "SHT_FINI_ARRAY";
407839529caSEd Maste 		case SHT_PREINIT_ARRAY:	return "SHT_PREINIT_ARRAY";
408839529caSEd Maste 		case SHT_GROUP:		return "SHT_GROUP";
409839529caSEd Maste 		case SHT_SYMTAB_SHNDX:	return "SHT_SYMTAB_SHNDX";
410839529caSEd Maste 		}
411839529caSEd Maste 	} else if (sht < 0x70000000) {
4123fe401a5SEd Maste 		/* 0x60000000-0x6fffffff operating system-specific semantics */
413839529caSEd Maste 		switch (sht) {
4143fe401a5SEd Maste 		case 0x6ffffff0:	return "XXX:VERSYM";
415839529caSEd Maste 		case SHT_SUNW_dof:	return "SHT_SUNW_dof";
416839529caSEd Maste 		case SHT_GNU_HASH:	return "SHT_GNU_HASH";
4173fe401a5SEd Maste 		case 0x6ffffff7:	return "SHT_GNU_LIBLIST";
4183fe401a5SEd Maste 		case 0x6ffffffc:	return "XXX:VERDEF";
419839529caSEd Maste 		case SHT_SUNW_verdef:	return "SHT_SUNW(GNU)_verdef";
420839529caSEd Maste 		case SHT_SUNW_verneed:	return "SHT_SUNW(GNU)_verneed";
421839529caSEd Maste 		case SHT_SUNW_versym:	return "SHT_SUNW(GNU)_versym";
422839529caSEd Maste 		}
423839529caSEd Maste 	} else if (sht < 0x80000000) {
4243fe401a5SEd Maste 		/* 0x70000000 - 0x7fffffff processor-specific semantics */
425839529caSEd Maste 		switch (mach) {
426839529caSEd Maste 		case EM_ARM:
427839529caSEd Maste 			switch (sht) {
428839529caSEd Maste 			case SHT_ARM_EXIDX: return "SHT_ARM_EXIDX";
429839529caSEd Maste 			case SHT_ARM_PREEMPTMAP: return "SHT_ARM_PREEMPTMAP";
430839529caSEd Maste 			case SHT_ARM_ATTRIBUTES: return "SHT_ARM_ATTRIBUTES";
431839529caSEd Maste 			case SHT_ARM_DEBUGOVERLAY:
432839529caSEd Maste 			    return "SHT_ARM_DEBUGOVERLAY";
433839529caSEd Maste 			case SHT_ARM_OVERLAYSECTION:
434839529caSEd Maste 			    return "SHT_ARM_OVERLAYSECTION";
435839529caSEd Maste 			}
436839529caSEd Maste 			break;
437839529caSEd Maste 		case EM_IA_64:
438839529caSEd Maste 			switch (sht) {
4393fe401a5SEd Maste 			case 0x70000000: return "SHT_IA_64_EXT";
4403fe401a5SEd Maste 			case 0x70000001: return "SHT_IA_64_UNWIND";
441839529caSEd Maste 			}
442839529caSEd Maste 			break;
443839529caSEd Maste 		case EM_MIPS:
444839529caSEd Maste 			switch (sht) {
445839529caSEd Maste 			case SHT_MIPS_REGINFO: return "SHT_MIPS_REGINFO";
446839529caSEd Maste 			case SHT_MIPS_OPTIONS: return "SHT_MIPS_OPTIONS";
447839529caSEd Maste 			case SHT_MIPS_ABIFLAGS: return "SHT_MIPS_ABIFLAGS";
448839529caSEd Maste 			}
449839529caSEd Maste 			break;
450839529caSEd Maste 		}
451839529caSEd Maste 		switch (sht) {
4523fe401a5SEd Maste 		case 0x7ffffffd: return "XXX:AUXILIARY";
4533fe401a5SEd Maste 		case 0x7fffffff: return "XXX:FILTER";
4543fe401a5SEd Maste 		}
4553fe401a5SEd Maste 	}
456839529caSEd Maste 	/* 0x80000000 - 0xffffffff application programs */
457839529caSEd Maste 
458839529caSEd Maste 	snprintf(unknown_buf, sizeof(unknown_buf),
459839529caSEd Maste 		"<unknown: %#llx>", (unsigned long long)sht);
460839529caSEd Maste 	return (unknown_buf);
461839529caSEd Maste }
4623fe401a5SEd Maste 
4633fe401a5SEd Maste /*
4643fe401a5SEd Maste  * Define known section flags. These flags are defined in the order
4653fe401a5SEd Maste  * they are to be printed out.
4663fe401a5SEd Maste  */
4673fe401a5SEd Maste #define	DEFINE_SHFLAGS()			\
4683fe401a5SEd Maste 	DEFINE_SHF(WRITE)			\
4693fe401a5SEd Maste 	DEFINE_SHF(ALLOC)			\
4703fe401a5SEd Maste 	DEFINE_SHF(EXECINSTR)			\
4713fe401a5SEd Maste 	DEFINE_SHF(MERGE)			\
4723fe401a5SEd Maste 	DEFINE_SHF(STRINGS)			\
4733fe401a5SEd Maste 	DEFINE_SHF(INFO_LINK)			\
4743fe401a5SEd Maste 	DEFINE_SHF(LINK_ORDER)			\
4753fe401a5SEd Maste 	DEFINE_SHF(OS_NONCONFORMING)		\
4763fe401a5SEd Maste 	DEFINE_SHF(GROUP)			\
477b6b6f9ccSEd Maste 	DEFINE_SHF(TLS)				\
478b6b6f9ccSEd Maste 	DEFINE_SHF(COMPRESSED)
4793fe401a5SEd Maste 
4803fe401a5SEd Maste #undef	DEFINE_SHF
4813fe401a5SEd Maste #define	DEFINE_SHF(F) "SHF_" #F "|"
4823fe401a5SEd Maste #define ALLSHFLAGS	DEFINE_SHFLAGS()
4833fe401a5SEd Maste 
4843fe401a5SEd Maste static const char *
4853fe401a5SEd Maste sh_flags(uint64_t shf)
4863fe401a5SEd Maste {
4873fe401a5SEd Maste 	static char	flg[sizeof(ALLSHFLAGS)+1];
4883fe401a5SEd Maste 
4893fe401a5SEd Maste 	flg[0] = '\0';
4903fe401a5SEd Maste 
4913fe401a5SEd Maste #undef	DEFINE_SHF
4923fe401a5SEd Maste #define	DEFINE_SHF(N)				\
4933fe401a5SEd Maste 	if (shf & SHF_##N)			\
4943fe401a5SEd Maste 		strcat(flg, "SHF_" #N "|");	\
4953fe401a5SEd Maste 
4963fe401a5SEd Maste 	DEFINE_SHFLAGS()
4973fe401a5SEd Maste 
4983fe401a5SEd Maste 	flg[strlen(flg) - 1] = '\0'; /* Remove the trailing "|". */
4993fe401a5SEd Maste 
5003fe401a5SEd Maste 	return (flg);
5013fe401a5SEd Maste }
5023fe401a5SEd Maste 
503839529caSEd Maste static const char *
504839529caSEd Maste st_type(unsigned int mach, unsigned int type)
505839529caSEd Maste {
506839529caSEd Maste 	static char s_type[32];
5073fe401a5SEd Maste 
508839529caSEd Maste 	switch (type) {
509839529caSEd Maste 	case STT_NOTYPE: return "STT_NOTYPE";
510839529caSEd Maste 	case STT_OBJECT: return "STT_OBJECT";
511839529caSEd Maste 	case STT_FUNC: return "STT_FUNC";
512839529caSEd Maste 	case STT_SECTION: return "STT_SECTION";
513839529caSEd Maste 	case STT_FILE: return "STT_FILE";
514839529caSEd Maste 	case STT_COMMON: return "STT_COMMON";
515839529caSEd Maste 	case STT_TLS: return "STT_TLS";
516839529caSEd Maste 	case 13:
517839529caSEd Maste 		if (mach == EM_SPARCV9)
518839529caSEd Maste 			return "STT_SPARC_REGISTER";
519839529caSEd Maste 		break;
520839529caSEd Maste 	}
521839529caSEd Maste 	snprintf(s_type, sizeof(s_type), "<unknown: %#x>", type);
522839529caSEd Maste 	return (s_type);
523839529caSEd Maste }
5243fe401a5SEd Maste 
525839529caSEd Maste static const char *
526839529caSEd Maste st_type_S(unsigned int type)
527839529caSEd Maste {
528839529caSEd Maste 	static char s_type[32];
5293fe401a5SEd Maste 
530839529caSEd Maste 	switch (type) {
531839529caSEd Maste 	case STT_NOTYPE: return "NOTY";
532839529caSEd Maste 	case STT_OBJECT: return "OBJT";
533839529caSEd Maste 	case STT_FUNC: return "FUNC";
534839529caSEd Maste 	case STT_SECTION: return "SECT";
535839529caSEd Maste 	case STT_FILE: return "FILE";
536839529caSEd Maste 	}
537839529caSEd Maste 	snprintf(s_type, sizeof(s_type), "<unknown: %#x>", type);
538839529caSEd Maste 	return (s_type);
539839529caSEd Maste }
540839529caSEd Maste 
541839529caSEd Maste static const char *
542839529caSEd Maste st_bindings(unsigned int sbind)
543839529caSEd Maste {
544839529caSEd Maste 	static char s_sbind[32];
545839529caSEd Maste 
546839529caSEd Maste 	switch (sbind) {
547839529caSEd Maste 	case STB_LOCAL: return "STB_LOCAL";
548839529caSEd Maste 	case STB_GLOBAL: return "STB_GLOBAL";
549839529caSEd Maste 	case STB_WEAK: return "STB_WEAK";
550839529caSEd Maste 	case STB_GNU_UNIQUE: return "STB_GNU_UNIQUE";
551839529caSEd Maste 	default:
552839529caSEd Maste 		if (sbind >= STB_LOOS && sbind <= STB_HIOS)
553839529caSEd Maste 			return "OS";
554839529caSEd Maste 		else if (sbind >= STB_LOPROC && sbind <= STB_HIPROC)
555839529caSEd Maste 			return "PROC";
556839529caSEd Maste 		else
557839529caSEd Maste 			snprintf(s_sbind, sizeof(s_sbind), "<unknown: %#x>",
558839529caSEd Maste 			    sbind);
559839529caSEd Maste 		return (s_sbind);
560839529caSEd Maste 	}
561839529caSEd Maste }
562839529caSEd Maste 
563839529caSEd Maste static const char *
564839529caSEd Maste st_bindings_S(unsigned int sbind)
565839529caSEd Maste {
566839529caSEd Maste 	static char s_sbind[32];
567839529caSEd Maste 
568839529caSEd Maste 	switch (sbind) {
569839529caSEd Maste 	case STB_LOCAL: return "LOCL";
570839529caSEd Maste 	case STB_GLOBAL: return "GLOB";
571839529caSEd Maste 	case STB_WEAK: return "WEAK";
572839529caSEd Maste 	case STB_GNU_UNIQUE: return "UNIQ";
573839529caSEd Maste 	default:
574839529caSEd Maste 		if (sbind >= STB_LOOS && sbind <= STB_HIOS)
575839529caSEd Maste 			return "OS";
576839529caSEd Maste 		else if (sbind >= STB_LOPROC && sbind <= STB_HIPROC)
577839529caSEd Maste 			return "PROC";
578839529caSEd Maste 		else
579839529caSEd Maste 			snprintf(s_sbind, sizeof(s_sbind), "<%#x>",
580839529caSEd Maste 			    sbind);
581839529caSEd Maste 		return (s_sbind);
582839529caSEd Maste 	}
583839529caSEd Maste }
5843fe401a5SEd Maste 
5853fe401a5SEd Maste static unsigned char st_others[] = {
5863fe401a5SEd Maste 	'D', 'I', 'H', 'P'
5873fe401a5SEd Maste };
5883fe401a5SEd Maste 
5893fe401a5SEd Maste static void	add_name(struct elfdump *ed, const char *name);
5903fe401a5SEd Maste static void	elf_print_object(struct elfdump *ed);
5913fe401a5SEd Maste static void	elf_print_elf(struct elfdump *ed);
5923fe401a5SEd Maste static void	elf_print_ehdr(struct elfdump *ed);
5933fe401a5SEd Maste static void	elf_print_phdr(struct elfdump *ed);
5943fe401a5SEd Maste static void	elf_print_shdr(struct elfdump *ed);
5953fe401a5SEd Maste static void	elf_print_symtab(struct elfdump *ed, int i);
5963fe401a5SEd Maste static void	elf_print_symtabs(struct elfdump *ed);
5973fe401a5SEd Maste static void	elf_print_symver(struct elfdump *ed);
5983fe401a5SEd Maste static void	elf_print_verdef(struct elfdump *ed, struct section *s);
5993fe401a5SEd Maste static void	elf_print_verneed(struct elfdump *ed, struct section *s);
6003fe401a5SEd Maste static void	elf_print_interp(struct elfdump *ed);
6013fe401a5SEd Maste static void	elf_print_dynamic(struct elfdump *ed);
6023fe401a5SEd Maste static void	elf_print_rel_entry(struct elfdump *ed, struct section *s,
6033fe401a5SEd Maste     int j, struct rel_entry *r);
6043fe401a5SEd Maste static void	elf_print_rela(struct elfdump *ed, struct section *s,
6053fe401a5SEd Maste     Elf_Data *data);
6063fe401a5SEd Maste static void	elf_print_rel(struct elfdump *ed, struct section *s,
6073fe401a5SEd Maste     Elf_Data *data);
6083fe401a5SEd Maste static void	elf_print_reloc(struct elfdump *ed);
6093fe401a5SEd Maste static void	elf_print_got(struct elfdump *ed);
6103fe401a5SEd Maste static void	elf_print_got_section(struct elfdump *ed, struct section *s);
6113fe401a5SEd Maste static void	elf_print_note(struct elfdump *ed);
6123fe401a5SEd Maste static void	elf_print_svr4_hash(struct elfdump *ed, struct section *s);
6133fe401a5SEd Maste static void	elf_print_svr4_hash64(struct elfdump *ed, struct section *s);
6143fe401a5SEd Maste static void	elf_print_gnu_hash(struct elfdump *ed, struct section *s);
6153fe401a5SEd Maste static void	elf_print_hash(struct elfdump *ed);
6163fe401a5SEd Maste static void	elf_print_checksum(struct elfdump *ed);
6173fe401a5SEd Maste static void	find_gotrel(struct elfdump *ed, struct section *gs,
6183fe401a5SEd Maste     struct rel_entry *got);
6193fe401a5SEd Maste static struct spec_name	*find_name(struct elfdump *ed, const char *name);
620656f49f8SEd Maste static int	get_ent_count(const struct section *s, int *ent_count);
621b6b6f9ccSEd Maste static const char *get_symbol_name(struct elfdump *ed, uint32_t symtab, int i);
6223fe401a5SEd Maste static const char *get_string(struct elfdump *ed, int strtab, size_t off);
6233fe401a5SEd Maste static void	get_versym(struct elfdump *ed, int i, uint16_t **vs, int *nvs);
6243fe401a5SEd Maste static void	load_sections(struct elfdump *ed);
6253fe401a5SEd Maste static void	unload_sections(struct elfdump *ed);
6263fe401a5SEd Maste static void	usage(void);
6273fe401a5SEd Maste #ifdef	USE_LIBARCHIVE_AR
6283fe401a5SEd Maste static int	ac_detect_ar(int fd);
6293fe401a5SEd Maste static void	ac_print_ar(struct elfdump *ed, int fd);
6303fe401a5SEd Maste #else
6313fe401a5SEd Maste static void	elf_print_ar(struct elfdump *ed, int fd);
6323fe401a5SEd Maste #endif	/* USE_LIBARCHIVE_AR */
6333fe401a5SEd Maste 
6343fe401a5SEd Maste static struct option elfdump_longopts[] =
6353fe401a5SEd Maste {
6363fe401a5SEd Maste 	{ "help",	no_argument,	NULL,	'H' },
6373fe401a5SEd Maste 	{ "version",	no_argument,	NULL,	'V' },
6383fe401a5SEd Maste 	{ NULL,		0,		NULL,	0   }
6393fe401a5SEd Maste };
6403fe401a5SEd Maste 
6413fe401a5SEd Maste int
6423fe401a5SEd Maste main(int ac, char **av)
6433fe401a5SEd Maste {
6443fe401a5SEd Maste 	struct elfdump		*ed, ed_storage;
6453fe401a5SEd Maste 	struct spec_name	*sn;
6463fe401a5SEd Maste 	int			 ch, i;
6473fe401a5SEd Maste 
6483fe401a5SEd Maste 	ed = &ed_storage;
6493fe401a5SEd Maste 	memset(ed, 0, sizeof(*ed));
6503fe401a5SEd Maste 	STAILQ_INIT(&ed->snl);
6513fe401a5SEd Maste 	ed->out = stdout;
6523fe401a5SEd Maste 	while ((ch = getopt_long(ac, av, "acdeiGHhknN:prsSvVw:",
6533fe401a5SEd Maste 		elfdump_longopts, NULL)) != -1)
6543fe401a5SEd Maste 		switch (ch) {
6553fe401a5SEd Maste 		case 'a':
6563fe401a5SEd Maste 			ed->options = ED_ALL;
6573fe401a5SEd Maste 			break;
6583fe401a5SEd Maste 		case 'c':
6593fe401a5SEd Maste 			ed->options |= ED_SHDR;
6603fe401a5SEd Maste 			break;
6613fe401a5SEd Maste 		case 'd':
6623fe401a5SEd Maste 			ed->options |= ED_DYN;
6633fe401a5SEd Maste 			break;
6643fe401a5SEd Maste 		case 'e':
6653fe401a5SEd Maste 			ed->options |= ED_EHDR;
6663fe401a5SEd Maste 			break;
6673fe401a5SEd Maste 		case 'i':
6683fe401a5SEd Maste 			ed->options |= ED_INTERP;
6693fe401a5SEd Maste 			break;
6703fe401a5SEd Maste 		case 'G':
6713fe401a5SEd Maste 			ed->options |= ED_GOT;
6723fe401a5SEd Maste 			break;
6733fe401a5SEd Maste 		case 'h':
6743fe401a5SEd Maste 			ed->options |= ED_HASH;
6753fe401a5SEd Maste 			break;
6763fe401a5SEd Maste 		case 'k':
6773fe401a5SEd Maste 			ed->options |= ED_CHECKSUM;
6783fe401a5SEd Maste 			break;
6793fe401a5SEd Maste 		case 'n':
6803fe401a5SEd Maste 			ed->options |= ED_NOTE;
6813fe401a5SEd Maste 			break;
6823fe401a5SEd Maste 		case 'N':
6833fe401a5SEd Maste 			add_name(ed, optarg);
6843fe401a5SEd Maste 			break;
6853fe401a5SEd Maste 		case 'p':
6863fe401a5SEd Maste 			ed->options |= ED_PHDR;
6873fe401a5SEd Maste 			break;
6883fe401a5SEd Maste 		case 'r':
6893fe401a5SEd Maste 			ed->options |= ED_REL;
6903fe401a5SEd Maste 			break;
6913fe401a5SEd Maste 		case 's':
6923fe401a5SEd Maste 			ed->options |= ED_SYMTAB;
6933fe401a5SEd Maste 			break;
6943fe401a5SEd Maste 		case 'S':
6953fe401a5SEd Maste 			ed->flags |= SOLARIS_FMT;
6963fe401a5SEd Maste 			break;
6973fe401a5SEd Maste 		case 'v':
6983fe401a5SEd Maste 			ed->options |= ED_SYMVER;
6993fe401a5SEd Maste 			break;
7003fe401a5SEd Maste 		case 'V':
7013fe401a5SEd Maste 			(void) printf("%s (%s)\n", ELFTC_GETPROGNAME(),
7023fe401a5SEd Maste 			    elftc_version());
7033fe401a5SEd Maste 			exit(EXIT_SUCCESS);
7043fe401a5SEd Maste 			break;
7053fe401a5SEd Maste 		case 'w':
7063fe401a5SEd Maste 			if ((ed->out = fopen(optarg, "w")) == NULL)
7073fe401a5SEd Maste 				err(EXIT_FAILURE, "%s", optarg);
7083fe401a5SEd Maste 			break;
7093fe401a5SEd Maste 		case '?':
7103fe401a5SEd Maste 		case 'H':
7113fe401a5SEd Maste 		default:
7123fe401a5SEd Maste 			usage();
7133fe401a5SEd Maste 		}
7143fe401a5SEd Maste 
7153fe401a5SEd Maste 	ac -= optind;
7163fe401a5SEd Maste 	av += optind;
7173fe401a5SEd Maste 
7183fe401a5SEd Maste 	if (ed->options == 0)
7193fe401a5SEd Maste 		ed->options = ED_ALL;
7203fe401a5SEd Maste 	sn = NULL;
7213fe401a5SEd Maste 	if (ed->options & ED_SYMTAB &&
7223fe401a5SEd Maste 	    (STAILQ_EMPTY(&ed->snl) || (sn = find_name(ed, "ARSYM")) != NULL)) {
7233fe401a5SEd Maste 		ed->flags |= PRINT_ARSYM;
7243fe401a5SEd Maste 		if (sn != NULL) {
7253fe401a5SEd Maste 			STAILQ_REMOVE(&ed->snl, sn, spec_name, sn_list);
7263fe401a5SEd Maste 			if (STAILQ_EMPTY(&ed->snl))
7273fe401a5SEd Maste 				ed->flags |= ONLY_ARSYM;
7283fe401a5SEd Maste 		}
7293fe401a5SEd Maste 	}
7303fe401a5SEd Maste 	if (ac == 0)
7313fe401a5SEd Maste 		usage();
7323fe401a5SEd Maste 	if (ac > 1)
7333fe401a5SEd Maste 		ed->flags |= PRINT_FILENAME;
7343fe401a5SEd Maste 	if (elf_version(EV_CURRENT) == EV_NONE)
7353fe401a5SEd Maste 		errx(EXIT_FAILURE, "ELF library initialization failed: %s",
7363fe401a5SEd Maste 		    elf_errmsg(-1));
7373fe401a5SEd Maste 
7383fe401a5SEd Maste 	for (i = 0; i < ac; i++) {
7393fe401a5SEd Maste 		ed->filename = av[i];
7403fe401a5SEd Maste 		ed->archive = NULL;
7413fe401a5SEd Maste 		elf_print_object(ed);
7423fe401a5SEd Maste 	}
7433fe401a5SEd Maste 
7443fe401a5SEd Maste 	exit(EXIT_SUCCESS);
7453fe401a5SEd Maste }
7463fe401a5SEd Maste 
7473fe401a5SEd Maste #ifdef USE_LIBARCHIVE_AR
7483fe401a5SEd Maste 
7493fe401a5SEd Maste /* Archive symbol table entry. */
7503fe401a5SEd Maste struct arsym_entry {
7513fe401a5SEd Maste 	char *sym_name;
7523fe401a5SEd Maste 	size_t off;
7533fe401a5SEd Maste };
7543fe401a5SEd Maste 
7553fe401a5SEd Maste /*
7563fe401a5SEd Maste  * Convenient wrapper for general libarchive error handling.
7573fe401a5SEd Maste  */
7583fe401a5SEd Maste #define	AC(CALL) do {							\
7593fe401a5SEd Maste 	if ((CALL)) {							\
7603fe401a5SEd Maste 		warnx("%s", archive_error_string(a));			\
7613fe401a5SEd Maste 		return;							\
7623fe401a5SEd Maste 	}								\
7633fe401a5SEd Maste } while (0)
7643fe401a5SEd Maste 
7653fe401a5SEd Maste /*
7663fe401a5SEd Maste  * Detect an ar(1) archive using libarchive(3).
7673fe401a5SEd Maste  */
7683fe401a5SEd Maste static int
7693fe401a5SEd Maste ac_detect_ar(int fd)
7703fe401a5SEd Maste {
7713fe401a5SEd Maste 	struct archive		*a;
7723fe401a5SEd Maste 	struct archive_entry	*entry;
7733fe401a5SEd Maste 	int			 r;
7743fe401a5SEd Maste 
7753fe401a5SEd Maste 	r = -1;
7763fe401a5SEd Maste 	if ((a = archive_read_new()) == NULL)
7773fe401a5SEd Maste 		return (0);
7783fe401a5SEd Maste 	archive_read_support_format_ar(a);
7793fe401a5SEd Maste 	if (archive_read_open_fd(a, fd, 10240) == ARCHIVE_OK)
7803fe401a5SEd Maste 		r = archive_read_next_header(a, &entry);
7813fe401a5SEd Maste 	archive_read_close(a);
7823fe401a5SEd Maste 	archive_read_free(a);
7833fe401a5SEd Maste 
7843fe401a5SEd Maste 	return (r == ARCHIVE_OK);
7853fe401a5SEd Maste }
7863fe401a5SEd Maste 
7873fe401a5SEd Maste /*
7883fe401a5SEd Maste  * Dump an ar(1) archive using libarchive(3).
7893fe401a5SEd Maste  */
7903fe401a5SEd Maste static void
7913fe401a5SEd Maste ac_print_ar(struct elfdump *ed, int fd)
7923fe401a5SEd Maste {
7933fe401a5SEd Maste 	struct archive		*a;
7943fe401a5SEd Maste 	struct archive_entry	*entry;
7953fe401a5SEd Maste 	struct arsym_entry	*arsym;
7963fe401a5SEd Maste 	const char		*name;
7973fe401a5SEd Maste 	char			 idx[10], *b;
7983fe401a5SEd Maste 	void			*buff;
7993fe401a5SEd Maste 	size_t			 size;
800b6b6f9ccSEd Maste 	uint32_t		 cnt, i;
801b6b6f9ccSEd Maste 	int			 r;
8023fe401a5SEd Maste 
8033fe401a5SEd Maste 	if (lseek(fd, 0, SEEK_SET) == -1)
8043fe401a5SEd Maste 		err(EXIT_FAILURE, "lseek failed");
8053fe401a5SEd Maste 	if ((a = archive_read_new()) == NULL)
8063fe401a5SEd Maste 		errx(EXIT_FAILURE, "%s", archive_error_string(a));
8073fe401a5SEd Maste 	archive_read_support_format_ar(a);
8083fe401a5SEd Maste 	AC(archive_read_open_fd(a, fd, 10240));
8093fe401a5SEd Maste 	for(;;) {
8103fe401a5SEd Maste 		r = archive_read_next_header(a, &entry);
8113fe401a5SEd Maste 		if (r == ARCHIVE_FATAL)
8123fe401a5SEd Maste 			errx(EXIT_FAILURE, "%s", archive_error_string(a));
8133fe401a5SEd Maste 		if (r == ARCHIVE_EOF)
8143fe401a5SEd Maste 			break;
8153fe401a5SEd Maste 		if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY)
8163fe401a5SEd Maste 			warnx("%s", archive_error_string(a));
8173fe401a5SEd Maste 		if (r == ARCHIVE_RETRY)
8183fe401a5SEd Maste 			continue;
8193fe401a5SEd Maste 		name = archive_entry_pathname(entry);
8203fe401a5SEd Maste 		size = archive_entry_size(entry);
8213fe401a5SEd Maste 		if (size == 0)
8223fe401a5SEd Maste 			continue;
8233fe401a5SEd Maste 		if ((buff = malloc(size)) == NULL) {
8243fe401a5SEd Maste 			warn("malloc failed");
8253fe401a5SEd Maste 			continue;
8263fe401a5SEd Maste 		}
8273fe401a5SEd Maste 		if (archive_read_data(a, buff, size) != (ssize_t)size) {
8283fe401a5SEd Maste 			warnx("%s", archive_error_string(a));
8293fe401a5SEd Maste 			free(buff);
8303fe401a5SEd Maste 			continue;
8313fe401a5SEd Maste 		}
8323fe401a5SEd Maste 
8333fe401a5SEd Maste 		/*
8343fe401a5SEd Maste 		 * Note that when processing arsym via libarchive, there is
8353fe401a5SEd Maste 		 * no way to tell which member a certain symbol belongs to,
8363fe401a5SEd Maste 		 * since we can not just "lseek" to a member offset and read
8373fe401a5SEd Maste 		 * the member header.
8383fe401a5SEd Maste 		 */
8393fe401a5SEd Maste 		if (!strcmp(name, "/") && ed->flags & PRINT_ARSYM) {
8403fe401a5SEd Maste 			b = buff;
8413fe401a5SEd Maste 			cnt = be32dec(b);
8423fe401a5SEd Maste 			if (cnt == 0) {
8433fe401a5SEd Maste 				free(buff);
8443fe401a5SEd Maste 				continue;
8453fe401a5SEd Maste 			}
8463fe401a5SEd Maste 			arsym = calloc(cnt, sizeof(*arsym));
8473fe401a5SEd Maste 			if (arsym == NULL)
8483fe401a5SEd Maste 				err(EXIT_FAILURE, "calloc failed");
8493fe401a5SEd Maste 			b += sizeof(uint32_t);
850b6b6f9ccSEd Maste 			for (i = 0; i < cnt; i++) {
8513fe401a5SEd Maste 				arsym[i].off = be32dec(b);
8523fe401a5SEd Maste 				b += sizeof(uint32_t);
8533fe401a5SEd Maste 			}
854b6b6f9ccSEd Maste 			for (i = 0; i < cnt; i++) {
8553fe401a5SEd Maste 				arsym[i].sym_name = b;
8563fe401a5SEd Maste 				b += strlen(b) + 1;
8573fe401a5SEd Maste 			}
8583fe401a5SEd Maste 			if (ed->flags & SOLARIS_FMT) {
8593fe401a5SEd Maste 				PRT("\nSymbol Table: (archive)\n");
8603fe401a5SEd Maste 				PRT("     index    offset    symbol\n");
8613fe401a5SEd Maste 			} else
8623fe401a5SEd Maste 				PRT("\nsymbol table (archive):\n");
863b6b6f9ccSEd Maste 			for (i = 0; i < cnt; i++) {
8643fe401a5SEd Maste 				if (ed->flags & SOLARIS_FMT) {
8653fe401a5SEd Maste 					snprintf(idx, sizeof(idx), "[%d]", i);
8663fe401a5SEd Maste 					PRT("%10s  ", idx);
8673fe401a5SEd Maste 					PRT("0x%8.8jx  ",
8683fe401a5SEd Maste 					    (uintmax_t)arsym[i].off);
8693fe401a5SEd Maste 					PRT("%s\n", arsym[i].sym_name);
8703fe401a5SEd Maste 				} else {
8713fe401a5SEd Maste 					PRT("\nentry: %d\n", i);
8723fe401a5SEd Maste 					PRT("\toffset: %#jx\n",
8733fe401a5SEd Maste 					    (uintmax_t)arsym[i].off);
8743fe401a5SEd Maste 					PRT("\tsymbol: %s\n",
8753fe401a5SEd Maste 					    arsym[i].sym_name);
8763fe401a5SEd Maste 				}
8773fe401a5SEd Maste 			}
8783fe401a5SEd Maste 			free(arsym);
8793fe401a5SEd Maste 			free(buff);
8803fe401a5SEd Maste 			/* No need to continue if we only dump ARSYM. */
8813fe401a5SEd Maste 			if (ed->flags & ONLY_ARSYM) {
8823fe401a5SEd Maste 				AC(archive_read_close(a));
8833fe401a5SEd Maste 				AC(archive_read_free(a));
8843fe401a5SEd Maste 				return;
8853fe401a5SEd Maste 			}
8863fe401a5SEd Maste 			continue;
8873fe401a5SEd Maste 		}
8883fe401a5SEd Maste 		if ((ed->elf = elf_memory(buff, size)) == NULL) {
8893fe401a5SEd Maste 			warnx("elf_memroy() failed: %s",
8903fe401a5SEd Maste 			      elf_errmsg(-1));
8913fe401a5SEd Maste 			free(buff);
8923fe401a5SEd Maste 			continue;
8933fe401a5SEd Maste 		}
8943fe401a5SEd Maste 		/* Skip non-ELF member. */
8953fe401a5SEd Maste 		if (elf_kind(ed->elf) == ELF_K_ELF) {
8963fe401a5SEd Maste 			printf("\n%s(%s):\n", ed->archive, name);
8973fe401a5SEd Maste 			elf_print_elf(ed);
8983fe401a5SEd Maste 		}
8993fe401a5SEd Maste 		elf_end(ed->elf);
9003fe401a5SEd Maste 		free(buff);
9013fe401a5SEd Maste 	}
9023fe401a5SEd Maste 	AC(archive_read_close(a));
9033fe401a5SEd Maste 	AC(archive_read_free(a));
9043fe401a5SEd Maste }
9053fe401a5SEd Maste 
9063fe401a5SEd Maste #else  /* USE_LIBARCHIVE_AR */
9073fe401a5SEd Maste 
9083fe401a5SEd Maste /*
9093fe401a5SEd Maste  * Dump an ar(1) archive.
9103fe401a5SEd Maste  */
9113fe401a5SEd Maste static void
9123fe401a5SEd Maste elf_print_ar(struct elfdump *ed, int fd)
9133fe401a5SEd Maste {
9143fe401a5SEd Maste 	Elf		*e;
9153fe401a5SEd Maste 	Elf_Arhdr	*arh;
9163fe401a5SEd Maste 	Elf_Arsym	*arsym;
9173fe401a5SEd Maste 	Elf_Cmd		 cmd;
918*715d1396SEd Maste 	char		 idx[21];
919b6b6f9ccSEd Maste 	size_t		 cnt, i;
9203fe401a5SEd Maste 
9213fe401a5SEd Maste 	ed->ar = ed->elf;
9223fe401a5SEd Maste 
9233fe401a5SEd Maste 	if (ed->flags & PRINT_ARSYM) {
9243fe401a5SEd Maste 		cnt = 0;
9253fe401a5SEd Maste 		if ((arsym = elf_getarsym(ed->ar, &cnt)) == NULL) {
9263fe401a5SEd Maste 			warnx("elf_getarsym failed: %s", elf_errmsg(-1));
9273fe401a5SEd Maste 			goto print_members;
9283fe401a5SEd Maste 		}
9293fe401a5SEd Maste 		if (cnt == 0)
9303fe401a5SEd Maste 			goto print_members;
9313fe401a5SEd Maste 		if (ed->flags & SOLARIS_FMT) {
9323fe401a5SEd Maste 			PRT("\nSymbol Table: (archive)\n");
9333fe401a5SEd Maste 			PRT("     index    offset    member name and symbol\n");
9343fe401a5SEd Maste 		} else
9353fe401a5SEd Maste 			PRT("\nsymbol table (archive):\n");
936b6b6f9ccSEd Maste 		for (i = 0; i < cnt - 1; i++) {
9373fe401a5SEd Maste 			if (elf_rand(ed->ar, arsym[i].as_off) !=
9383fe401a5SEd Maste 			    arsym[i].as_off) {
9393fe401a5SEd Maste 				warnx("elf_rand failed: %s", elf_errmsg(-1));
9403fe401a5SEd Maste 				break;
9413fe401a5SEd Maste 			}
9423fe401a5SEd Maste 			if ((e = elf_begin(fd, ELF_C_READ, ed->ar)) == NULL) {
9433fe401a5SEd Maste 				warnx("elf_begin failed: %s", elf_errmsg(-1));
9443fe401a5SEd Maste 				break;
9453fe401a5SEd Maste 			}
9463fe401a5SEd Maste 			if ((arh = elf_getarhdr(e)) == NULL) {
9473fe401a5SEd Maste 				warnx("elf_getarhdr failed: %s",
9483fe401a5SEd Maste 				    elf_errmsg(-1));
9493fe401a5SEd Maste 				break;
9503fe401a5SEd Maste 			}
9513fe401a5SEd Maste 			if (ed->flags & SOLARIS_FMT) {
952b6b6f9ccSEd Maste 				snprintf(idx, sizeof(idx), "[%zu]", i);
9533fe401a5SEd Maste 				PRT("%10s  ", idx);
9543fe401a5SEd Maste 				PRT("0x%8.8jx  ",
9553fe401a5SEd Maste 				    (uintmax_t)arsym[i].as_off);
9563fe401a5SEd Maste 				PRT("(%s):%s\n", arh->ar_name,
9573fe401a5SEd Maste 				    arsym[i].as_name);
9583fe401a5SEd Maste 			} else {
959b6b6f9ccSEd Maste 				PRT("\nentry: %zu\n", i);
9603fe401a5SEd Maste 				PRT("\toffset: %#jx\n",
9613fe401a5SEd Maste 				    (uintmax_t)arsym[i].as_off);
9623fe401a5SEd Maste 				PRT("\tmember: %s\n", arh->ar_name);
9633fe401a5SEd Maste 				PRT("\tsymbol: %s\n", arsym[i].as_name);
9643fe401a5SEd Maste 			}
9653fe401a5SEd Maste 			elf_end(e);
9663fe401a5SEd Maste 		}
9673fe401a5SEd Maste 
9683fe401a5SEd Maste 		/* No need to continue if we only dump ARSYM. */
9693fe401a5SEd Maste 		if (ed->flags & ONLY_ARSYM)
9703fe401a5SEd Maste 			return;
9713fe401a5SEd Maste 	}
9723fe401a5SEd Maste 
9733fe401a5SEd Maste print_members:
9743fe401a5SEd Maste 
9753fe401a5SEd Maste 	/* Rewind the archive. */
9763fe401a5SEd Maste 	if (elf_rand(ed->ar, SARMAG) != SARMAG) {
9773fe401a5SEd Maste 		warnx("elf_rand failed: %s", elf_errmsg(-1));
9783fe401a5SEd Maste 		return;
9793fe401a5SEd Maste 	}
9803fe401a5SEd Maste 
9813fe401a5SEd Maste 	/* Dump each member of the archive. */
9823fe401a5SEd Maste 	cmd = ELF_C_READ;
9833fe401a5SEd Maste 	while ((ed->elf = elf_begin(fd, cmd, ed->ar)) != NULL) {
9843fe401a5SEd Maste 		/* Skip non-ELF member. */
9853fe401a5SEd Maste 		if (elf_kind(ed->elf) == ELF_K_ELF) {
9863fe401a5SEd Maste 			if ((arh = elf_getarhdr(ed->elf)) == NULL) {
9873fe401a5SEd Maste 				warnx("elf_getarhdr failed: %s",
9883fe401a5SEd Maste 				    elf_errmsg(-1));
9893fe401a5SEd Maste 				break;
9903fe401a5SEd Maste 			}
9913fe401a5SEd Maste 			printf("\n%s(%s):\n", ed->archive, arh->ar_name);
9923fe401a5SEd Maste 			elf_print_elf(ed);
9933fe401a5SEd Maste 		}
9943fe401a5SEd Maste 		cmd = elf_next(ed->elf);
9953fe401a5SEd Maste 		elf_end(ed->elf);
9963fe401a5SEd Maste 	}
9973fe401a5SEd Maste }
9983fe401a5SEd Maste 
9993fe401a5SEd Maste #endif	/* USE_LIBARCHIVE_AR */
10003fe401a5SEd Maste 
10013fe401a5SEd Maste /*
10023fe401a5SEd Maste  * Dump an object. (ELF object or ar(1) archive)
10033fe401a5SEd Maste  */
10043fe401a5SEd Maste static void
10053fe401a5SEd Maste elf_print_object(struct elfdump *ed)
10063fe401a5SEd Maste {
10073fe401a5SEd Maste 	int fd;
10083fe401a5SEd Maste 
10093fe401a5SEd Maste 	if ((fd = open(ed->filename, O_RDONLY)) == -1) {
10103fe401a5SEd Maste 		warn("open %s failed", ed->filename);
10113fe401a5SEd Maste 		return;
10123fe401a5SEd Maste 	}
10133fe401a5SEd Maste 
10143fe401a5SEd Maste #ifdef	USE_LIBARCHIVE_AR
10153fe401a5SEd Maste 	if (ac_detect_ar(fd)) {
10163fe401a5SEd Maste 		ed->archive = ed->filename;
10173fe401a5SEd Maste 		ac_print_ar(ed, fd);
10183fe401a5SEd Maste 		return;
10193fe401a5SEd Maste 	}
10203fe401a5SEd Maste #endif	/* USE_LIBARCHIVE_AR */
10213fe401a5SEd Maste 
10223fe401a5SEd Maste 	if ((ed->elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
10233fe401a5SEd Maste 		warnx("elf_begin() failed: %s", elf_errmsg(-1));
10243fe401a5SEd Maste 		return;
10253fe401a5SEd Maste 	}
10263fe401a5SEd Maste 
10273fe401a5SEd Maste 	switch (elf_kind(ed->elf)) {
10283fe401a5SEd Maste 	case ELF_K_NONE:
10293fe401a5SEd Maste 		warnx("Not an ELF file.");
10303fe401a5SEd Maste 		return;
10313fe401a5SEd Maste 	case ELF_K_ELF:
10323fe401a5SEd Maste 		if (ed->flags & PRINT_FILENAME)
10333fe401a5SEd Maste 			printf("\n%s:\n", ed->filename);
10343fe401a5SEd Maste 		elf_print_elf(ed);
10353fe401a5SEd Maste 		break;
10363fe401a5SEd Maste 	case ELF_K_AR:
10373fe401a5SEd Maste #ifndef	USE_LIBARCHIVE_AR
10383fe401a5SEd Maste 		ed->archive = ed->filename;
10393fe401a5SEd Maste 		elf_print_ar(ed, fd);
10403fe401a5SEd Maste #endif
10413fe401a5SEd Maste 		break;
10423fe401a5SEd Maste 	default:
10433fe401a5SEd Maste 		warnx("Internal: libelf returned unknown elf kind.");
10443fe401a5SEd Maste 		return;
10453fe401a5SEd Maste 	}
10463fe401a5SEd Maste 
10473fe401a5SEd Maste 	elf_end(ed->elf);
10483fe401a5SEd Maste }
10493fe401a5SEd Maste 
10503fe401a5SEd Maste /*
10513fe401a5SEd Maste  * Dump an ELF object.
10523fe401a5SEd Maste  */
10533fe401a5SEd Maste static void
10543fe401a5SEd Maste elf_print_elf(struct elfdump *ed)
10553fe401a5SEd Maste {
10563fe401a5SEd Maste 
10573fe401a5SEd Maste 	if (gelf_getehdr(ed->elf, &ed->ehdr) == NULL) {
10583fe401a5SEd Maste 		warnx("gelf_getehdr failed: %s", elf_errmsg(-1));
10593fe401a5SEd Maste 		return;
10603fe401a5SEd Maste 	}
10613fe401a5SEd Maste 	if ((ed->ec = gelf_getclass(ed->elf)) == ELFCLASSNONE) {
10623fe401a5SEd Maste 		warnx("gelf_getclass failed: %s", elf_errmsg(-1));
10633fe401a5SEd Maste 		return;
10643fe401a5SEd Maste 	}
10653fe401a5SEd Maste 
10663fe401a5SEd Maste 	if (ed->options & (ED_SHDR | ED_DYN | ED_REL | ED_GOT | ED_SYMTAB |
10673fe401a5SEd Maste 	    ED_SYMVER | ED_NOTE | ED_HASH))
10683fe401a5SEd Maste 		load_sections(ed);
10693fe401a5SEd Maste 
10703fe401a5SEd Maste 	if (ed->options & ED_EHDR)
10713fe401a5SEd Maste 		elf_print_ehdr(ed);
10723fe401a5SEd Maste 	if (ed->options & ED_PHDR)
10733fe401a5SEd Maste 		elf_print_phdr(ed);
10743fe401a5SEd Maste 	if (ed->options & ED_INTERP)
10753fe401a5SEd Maste 		elf_print_interp(ed);
10763fe401a5SEd Maste 	if (ed->options & ED_SHDR)
10773fe401a5SEd Maste 		elf_print_shdr(ed);
10783fe401a5SEd Maste 	if (ed->options & ED_DYN)
10793fe401a5SEd Maste 		elf_print_dynamic(ed);
10803fe401a5SEd Maste 	if (ed->options & ED_REL)
10813fe401a5SEd Maste 		elf_print_reloc(ed);
10823fe401a5SEd Maste 	if (ed->options & ED_GOT)
10833fe401a5SEd Maste 		elf_print_got(ed);
10843fe401a5SEd Maste 	if (ed->options & ED_SYMTAB)
10853fe401a5SEd Maste 		elf_print_symtabs(ed);
10863fe401a5SEd Maste 	if (ed->options & ED_SYMVER)
10873fe401a5SEd Maste 		elf_print_symver(ed);
10883fe401a5SEd Maste 	if (ed->options & ED_NOTE)
10893fe401a5SEd Maste 		elf_print_note(ed);
10903fe401a5SEd Maste 	if (ed->options & ED_HASH)
10913fe401a5SEd Maste 		elf_print_hash(ed);
10923fe401a5SEd Maste 	if (ed->options & ED_CHECKSUM)
10933fe401a5SEd Maste 		elf_print_checksum(ed);
10943fe401a5SEd Maste 
10953fe401a5SEd Maste 	unload_sections(ed);
10963fe401a5SEd Maste }
10973fe401a5SEd Maste 
10983fe401a5SEd Maste /*
10993fe401a5SEd Maste  * Read the section headers from ELF object and store them in the
11003fe401a5SEd Maste  * internal cache.
11013fe401a5SEd Maste  */
11023fe401a5SEd Maste static void
11033fe401a5SEd Maste load_sections(struct elfdump *ed)
11043fe401a5SEd Maste {
11053fe401a5SEd Maste 	struct section	*s;
11063fe401a5SEd Maste 	const char	*name;
11073fe401a5SEd Maste 	Elf_Scn		*scn;
11083fe401a5SEd Maste 	GElf_Shdr	 sh;
11093fe401a5SEd Maste 	size_t		 shstrndx, ndx;
11103fe401a5SEd Maste 	int		 elferr;
11113fe401a5SEd Maste 
11123fe401a5SEd Maste 	assert(ed->sl == NULL);
11133fe401a5SEd Maste 
11143fe401a5SEd Maste 	if (!elf_getshnum(ed->elf, &ed->shnum)) {
11153fe401a5SEd Maste 		warnx("elf_getshnum failed: %s", elf_errmsg(-1));
11163fe401a5SEd Maste 		return;
11173fe401a5SEd Maste 	}
11183fe401a5SEd Maste 	if (ed->shnum == 0)
11193fe401a5SEd Maste 		return;
11203fe401a5SEd Maste 	if ((ed->sl = calloc(ed->shnum, sizeof(*ed->sl))) == NULL)
11213fe401a5SEd Maste 		err(EXIT_FAILURE, "calloc failed");
11223fe401a5SEd Maste 	if (!elf_getshstrndx(ed->elf, &shstrndx)) {
11233fe401a5SEd Maste 		warnx("elf_getshstrndx failed: %s", elf_errmsg(-1));
11243fe401a5SEd Maste 		return;
11253fe401a5SEd Maste 	}
11263fe401a5SEd Maste 	if ((scn = elf_getscn(ed->elf, 0)) == NULL) {
11273fe401a5SEd Maste 		warnx("elf_getscn failed: %s", elf_errmsg(-1));
11283fe401a5SEd Maste 		return;
11293fe401a5SEd Maste 	}
11303fe401a5SEd Maste 	(void) elf_errno();
11313fe401a5SEd Maste 	do {
11323fe401a5SEd Maste 		if (gelf_getshdr(scn, &sh) == NULL) {
11333fe401a5SEd Maste 			warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
11343fe401a5SEd Maste 			(void) elf_errno();
11353fe401a5SEd Maste 			continue;
11363fe401a5SEd Maste 		}
11373fe401a5SEd Maste 		if ((name = elf_strptr(ed->elf, shstrndx, sh.sh_name)) == NULL) {
11383fe401a5SEd Maste 			(void) elf_errno();
11393fe401a5SEd Maste 			name = "ERROR";
11403fe401a5SEd Maste 		}
11413fe401a5SEd Maste 		if ((ndx = elf_ndxscn(scn)) == SHN_UNDEF)
11423fe401a5SEd Maste 			if ((elferr = elf_errno()) != 0) {
11433fe401a5SEd Maste 				warnx("elf_ndxscn failed: %s",
11443fe401a5SEd Maste 				    elf_errmsg(elferr));
11453fe401a5SEd Maste 				continue;
11463fe401a5SEd Maste 			}
11473fe401a5SEd Maste 		if (ndx >= ed->shnum) {
11483fe401a5SEd Maste 			warnx("section index of '%s' out of range", name);
11493fe401a5SEd Maste 			continue;
11503fe401a5SEd Maste 		}
11513fe401a5SEd Maste 		s = &ed->sl[ndx];
11523fe401a5SEd Maste 		s->name = name;
11533fe401a5SEd Maste 		s->scn = scn;
11543fe401a5SEd Maste 		s->off = sh.sh_offset;
11553fe401a5SEd Maste 		s->sz = sh.sh_size;
11563fe401a5SEd Maste 		s->entsize = sh.sh_entsize;
11573fe401a5SEd Maste 		s->align = sh.sh_addralign;
11583fe401a5SEd Maste 		s->type = sh.sh_type;
11593fe401a5SEd Maste 		s->flags = sh.sh_flags;
11603fe401a5SEd Maste 		s->addr = sh.sh_addr;
11613fe401a5SEd Maste 		s->link = sh.sh_link;
11623fe401a5SEd Maste 		s->info = sh.sh_info;
11633fe401a5SEd Maste 	} while ((scn = elf_nextscn(ed->elf, scn)) != NULL);
11643fe401a5SEd Maste 	elferr = elf_errno();
11653fe401a5SEd Maste 	if (elferr != 0)
11663fe401a5SEd Maste 		warnx("elf_nextscn failed: %s", elf_errmsg(elferr));
11673fe401a5SEd Maste }
11683fe401a5SEd Maste 
11693fe401a5SEd Maste /*
11703fe401a5SEd Maste  * Release section related resources.
11713fe401a5SEd Maste  */
11723fe401a5SEd Maste static void
11733fe401a5SEd Maste unload_sections(struct elfdump *ed)
11743fe401a5SEd Maste {
11753fe401a5SEd Maste 	if (ed->sl != NULL) {
11763fe401a5SEd Maste 		free(ed->sl);
11773fe401a5SEd Maste 		ed->sl = NULL;
11783fe401a5SEd Maste 	}
11793fe401a5SEd Maste }
11803fe401a5SEd Maste 
11813fe401a5SEd Maste /*
11823fe401a5SEd Maste  * Add a name to the '-N' name list.
11833fe401a5SEd Maste  */
11843fe401a5SEd Maste static void
11853fe401a5SEd Maste add_name(struct elfdump *ed, const char *name)
11863fe401a5SEd Maste {
11873fe401a5SEd Maste 	struct spec_name *sn;
11883fe401a5SEd Maste 
11893fe401a5SEd Maste 	if (find_name(ed, name))
11903fe401a5SEd Maste 		return;
11913fe401a5SEd Maste 	if ((sn = malloc(sizeof(*sn))) == NULL) {
11923fe401a5SEd Maste 		warn("malloc failed");
11933fe401a5SEd Maste 		return;
11943fe401a5SEd Maste 	}
11953fe401a5SEd Maste 	sn->name = name;
11963fe401a5SEd Maste 	STAILQ_INSERT_TAIL(&ed->snl, sn, sn_list);
11973fe401a5SEd Maste }
11983fe401a5SEd Maste 
11993fe401a5SEd Maste /*
12003fe401a5SEd Maste  * Lookup a name in the '-N' name list.
12013fe401a5SEd Maste  */
12023fe401a5SEd Maste static struct spec_name *
12033fe401a5SEd Maste find_name(struct elfdump *ed, const char *name)
12043fe401a5SEd Maste {
12053fe401a5SEd Maste 	struct spec_name *sn;
12063fe401a5SEd Maste 
12073fe401a5SEd Maste 	STAILQ_FOREACH(sn, &ed->snl, sn_list) {
12083fe401a5SEd Maste 		if (!strcmp(sn->name, name))
12093fe401a5SEd Maste 			return (sn);
12103fe401a5SEd Maste 	}
12113fe401a5SEd Maste 
12123fe401a5SEd Maste 	return (NULL);
12133fe401a5SEd Maste }
12143fe401a5SEd Maste 
12153fe401a5SEd Maste /*
12163fe401a5SEd Maste  * Retrieve the name of a symbol using the section index of the symbol
12173fe401a5SEd Maste  * table and the index of the symbol within that table.
12183fe401a5SEd Maste  */
12193fe401a5SEd Maste static const char *
1220b6b6f9ccSEd Maste get_symbol_name(struct elfdump *ed, uint32_t symtab, int i)
12213fe401a5SEd Maste {
12223fe401a5SEd Maste 	static char	 sname[64];
12233fe401a5SEd Maste 	struct section	*s;
12243fe401a5SEd Maste 	const char	*name;
12253fe401a5SEd Maste 	GElf_Sym	 sym;
12263fe401a5SEd Maste 	Elf_Data	*data;
12273fe401a5SEd Maste 	int		 elferr;
12283fe401a5SEd Maste 
1229b6b6f9ccSEd Maste 	if (symtab >= ed->shnum)
1230b6b6f9ccSEd Maste 		return ("");
12313fe401a5SEd Maste 	s = &ed->sl[symtab];
12323fe401a5SEd Maste 	if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM)
12333fe401a5SEd Maste 		return ("");
12343fe401a5SEd Maste 	(void) elf_errno();
12353fe401a5SEd Maste 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
12363fe401a5SEd Maste 		elferr = elf_errno();
12373fe401a5SEd Maste 		if (elferr != 0)
12383fe401a5SEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
12393fe401a5SEd Maste 		return ("");
12403fe401a5SEd Maste 	}
12413fe401a5SEd Maste 	if (gelf_getsym(data, i, &sym) != &sym)
12423fe401a5SEd Maste 		return ("");
12433fe401a5SEd Maste 	if (GELF_ST_TYPE(sym.st_info) == STT_SECTION) {
12443fe401a5SEd Maste 		if (sym.st_shndx < ed->shnum) {
12453fe401a5SEd Maste 			snprintf(sname, sizeof(sname), "%s (section)",
12463fe401a5SEd Maste 			    ed->sl[sym.st_shndx].name);
12473fe401a5SEd Maste 			return (sname);
12483fe401a5SEd Maste 		} else
12493fe401a5SEd Maste 			return ("");
12503fe401a5SEd Maste 	}
12513fe401a5SEd Maste 	if ((name = elf_strptr(ed->elf, s->link, sym.st_name)) == NULL)
12523fe401a5SEd Maste 		return ("");
12533fe401a5SEd Maste 
12543fe401a5SEd Maste 	return (name);
12553fe401a5SEd Maste }
12563fe401a5SEd Maste 
12573fe401a5SEd Maste /*
12583fe401a5SEd Maste  * Retrieve a string using string table section index and the string offset.
12593fe401a5SEd Maste  */
12603fe401a5SEd Maste static const char*
12613fe401a5SEd Maste get_string(struct elfdump *ed, int strtab, size_t off)
12623fe401a5SEd Maste {
12633fe401a5SEd Maste 	const char *name;
12643fe401a5SEd Maste 
12653fe401a5SEd Maste 	if ((name = elf_strptr(ed->elf, strtab, off)) == NULL)
12663fe401a5SEd Maste 		return ("");
12673fe401a5SEd Maste 
12683fe401a5SEd Maste 	return (name);
12693fe401a5SEd Maste }
12703fe401a5SEd Maste 
12713fe401a5SEd Maste /*
12723fe401a5SEd Maste  * Dump the ELF Executable Header.
12733fe401a5SEd Maste  */
12743fe401a5SEd Maste static void
12753fe401a5SEd Maste elf_print_ehdr(struct elfdump *ed)
12763fe401a5SEd Maste {
12773fe401a5SEd Maste 
12783fe401a5SEd Maste 	if (!STAILQ_EMPTY(&ed->snl))
12793fe401a5SEd Maste 		return;
12803fe401a5SEd Maste 
12813fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT) {
12823fe401a5SEd Maste 		PRT("\nELF Header\n");
12833fe401a5SEd Maste 		PRT("  ei_magic:   { %#x, %c, %c, %c }\n",
12843fe401a5SEd Maste 		    ed->ehdr.e_ident[0], ed->ehdr.e_ident[1],
12853fe401a5SEd Maste 		    ed->ehdr.e_ident[2], ed->ehdr.e_ident[3]);
12863fe401a5SEd Maste 		PRT("  ei_class:   %-18s",
1287b6b6f9ccSEd Maste 		    elf_class_str(ed->ehdr.e_ident[EI_CLASS]));
1288b6b6f9ccSEd Maste 		PRT("  ei_data:      %s\n",
1289b6b6f9ccSEd Maste 		    elf_data_str(ed->ehdr.e_ident[EI_DATA]));
12903fe401a5SEd Maste 		PRT("  e_machine:  %-18s", e_machines(ed->ehdr.e_machine));
1291b6b6f9ccSEd Maste 		PRT("  e_version:    %s\n",
1292b6b6f9ccSEd Maste 		    elf_version_str(ed->ehdr.e_version));
1293b6b6f9ccSEd Maste 		PRT("  e_type:     %s\n", elf_type_str(ed->ehdr.e_type));
12943fe401a5SEd Maste 		PRT("  e_flags:    %18d\n", ed->ehdr.e_flags);
12953fe401a5SEd Maste 		PRT("  e_entry:    %#18jx", (uintmax_t)ed->ehdr.e_entry);
12963fe401a5SEd Maste 		PRT("  e_ehsize: %6d", ed->ehdr.e_ehsize);
12973fe401a5SEd Maste 		PRT("  e_shstrndx:%5d\n", ed->ehdr.e_shstrndx);
12983fe401a5SEd Maste 		PRT("  e_shoff:    %#18jx", (uintmax_t)ed->ehdr.e_shoff);
12993fe401a5SEd Maste 		PRT("  e_shentsize: %3d", ed->ehdr.e_shentsize);
13003fe401a5SEd Maste 		PRT("  e_shnum:   %5d\n", ed->ehdr.e_shnum);
13013fe401a5SEd Maste 		PRT("  e_phoff:    %#18jx", (uintmax_t)ed->ehdr.e_phoff);
13023fe401a5SEd Maste 		PRT("  e_phentsize: %3d", ed->ehdr.e_phentsize);
13033fe401a5SEd Maste 		PRT("  e_phnum:   %5d\n", ed->ehdr.e_phnum);
13043fe401a5SEd Maste 	} else {
13053fe401a5SEd Maste 		PRT("\nelf header:\n");
13063fe401a5SEd Maste 		PRT("\n");
13073fe401a5SEd Maste 		PRT("\te_ident: %s %s %s\n",
1308b6b6f9ccSEd Maste 		    elf_class_str(ed->ehdr.e_ident[EI_CLASS]),
1309b6b6f9ccSEd Maste 		    elf_data_str(ed->ehdr.e_ident[EI_DATA]),
13103fe401a5SEd Maste 		    ei_abis[ed->ehdr.e_ident[EI_OSABI]]);
1311b6b6f9ccSEd Maste 		PRT("\te_type: %s\n", elf_type_str(ed->ehdr.e_type));
13123fe401a5SEd Maste 		PRT("\te_machine: %s\n", e_machines(ed->ehdr.e_machine));
1313b6b6f9ccSEd Maste 		PRT("\te_version: %s\n", elf_version_str(ed->ehdr.e_version));
13143fe401a5SEd Maste 		PRT("\te_entry: %#jx\n", (uintmax_t)ed->ehdr.e_entry);
13153fe401a5SEd Maste 		PRT("\te_phoff: %ju\n", (uintmax_t)ed->ehdr.e_phoff);
13163fe401a5SEd Maste 		PRT("\te_shoff: %ju\n", (uintmax_t) ed->ehdr.e_shoff);
13173fe401a5SEd Maste 		PRT("\te_flags: %u\n", ed->ehdr.e_flags);
13183fe401a5SEd Maste 		PRT("\te_ehsize: %u\n", ed->ehdr.e_ehsize);
13193fe401a5SEd Maste 		PRT("\te_phentsize: %u\n", ed->ehdr.e_phentsize);
13203fe401a5SEd Maste 		PRT("\te_phnum: %u\n", ed->ehdr.e_phnum);
13213fe401a5SEd Maste 		PRT("\te_shentsize: %u\n", ed->ehdr.e_shentsize);
13223fe401a5SEd Maste 		PRT("\te_shnum: %u\n", ed->ehdr.e_shnum);
13233fe401a5SEd Maste 		PRT("\te_shstrndx: %u\n", ed->ehdr.e_shstrndx);
13243fe401a5SEd Maste 	}
13253fe401a5SEd Maste }
13263fe401a5SEd Maste 
13273fe401a5SEd Maste /*
13283fe401a5SEd Maste  * Dump the ELF Program Header Table.
13293fe401a5SEd Maste  */
13303fe401a5SEd Maste static void
13313fe401a5SEd Maste elf_print_phdr(struct elfdump *ed)
13323fe401a5SEd Maste {
13333fe401a5SEd Maste 	GElf_Phdr	 ph;
1334b6b6f9ccSEd Maste 	size_t		 phnum, i;
1335b6b6f9ccSEd Maste 	int		 header;
13363fe401a5SEd Maste 
13373fe401a5SEd Maste 	if (elf_getphnum(ed->elf, &phnum) == 0) {
13383fe401a5SEd Maste 		warnx("elf_getphnum failed: %s", elf_errmsg(-1));
13393fe401a5SEd Maste 		return;
13403fe401a5SEd Maste 	}
13413fe401a5SEd Maste 	header = 0;
1342b6b6f9ccSEd Maste 	for (i = 0; i < phnum; i++) {
13433fe401a5SEd Maste 		if (gelf_getphdr(ed->elf, i, &ph) != &ph) {
13443fe401a5SEd Maste 			warnx("elf_getphdr failed: %s", elf_errmsg(-1));
13453fe401a5SEd Maste 			continue;
13463fe401a5SEd Maste 		}
13473fe401a5SEd Maste 		if (!STAILQ_EMPTY(&ed->snl) &&
1348b6b6f9ccSEd Maste 		    find_name(ed, elf_phdr_type_str(ph.p_type)) == NULL)
13493fe401a5SEd Maste 			continue;
13503fe401a5SEd Maste 		if (ed->flags & SOLARIS_FMT) {
1351b6b6f9ccSEd Maste 			PRT("\nProgram Header[%zu]:\n", i);
13523fe401a5SEd Maste 			PRT("    p_vaddr:      %#-14jx", (uintmax_t)ph.p_vaddr);
1353b6b6f9ccSEd Maste 			PRT("  p_flags:    [ %s ]\n",
1354b6b6f9ccSEd Maste 			    p_flags[ph.p_flags & 0x7]);
13553fe401a5SEd Maste 			PRT("    p_paddr:      %#-14jx", (uintmax_t)ph.p_paddr);
1356b6b6f9ccSEd Maste 			PRT("  p_type:     [ %s ]\n",
1357b6b6f9ccSEd Maste 			    elf_phdr_type_str(ph.p_type));
13583fe401a5SEd Maste 			PRT("    p_filesz:     %#-14jx",
13593fe401a5SEd Maste 			    (uintmax_t)ph.p_filesz);
13603fe401a5SEd Maste 			PRT("  p_memsz:    %#jx\n", (uintmax_t)ph.p_memsz);
13613fe401a5SEd Maste 			PRT("    p_offset:     %#-14jx",
13623fe401a5SEd Maste 			    (uintmax_t)ph.p_offset);
13633fe401a5SEd Maste 			PRT("  p_align:    %#jx\n", (uintmax_t)ph.p_align);
13643fe401a5SEd Maste 		} else {
13653fe401a5SEd Maste 			if (!header) {
13663fe401a5SEd Maste 				PRT("\nprogram header:\n");
13673fe401a5SEd Maste 				header = 1;
13683fe401a5SEd Maste 			}
13693fe401a5SEd Maste 			PRT("\n");
1370b6b6f9ccSEd Maste 			PRT("entry: %zu\n", i);
1371b6b6f9ccSEd Maste 			PRT("\tp_type: %s\n", elf_phdr_type_str(ph.p_type));
13723fe401a5SEd Maste 			PRT("\tp_offset: %ju\n", (uintmax_t)ph.p_offset);
13733fe401a5SEd Maste 			PRT("\tp_vaddr: %#jx\n", (uintmax_t)ph.p_vaddr);
13743fe401a5SEd Maste 			PRT("\tp_paddr: %#jx\n", (uintmax_t)ph.p_paddr);
13753fe401a5SEd Maste 			PRT("\tp_filesz: %ju\n", (uintmax_t)ph.p_filesz);
13763fe401a5SEd Maste 			PRT("\tp_memsz: %ju\n", (uintmax_t)ph.p_memsz);
1377b6b6f9ccSEd Maste 			PRT("\tp_flags: %s\n", p_flags[ph.p_flags & 0x7]);
13783fe401a5SEd Maste 			PRT("\tp_align: %ju\n", (uintmax_t)ph.p_align);
13793fe401a5SEd Maste 		}
13803fe401a5SEd Maste 	}
13813fe401a5SEd Maste }
13823fe401a5SEd Maste 
13833fe401a5SEd Maste /*
13843fe401a5SEd Maste  * Dump the ELF Section Header Table.
13853fe401a5SEd Maste  */
13863fe401a5SEd Maste static void
13873fe401a5SEd Maste elf_print_shdr(struct elfdump *ed)
13883fe401a5SEd Maste {
13893fe401a5SEd Maste 	struct section *s;
1390b6b6f9ccSEd Maste 	size_t i;
13913fe401a5SEd Maste 
13923fe401a5SEd Maste 	if (!STAILQ_EMPTY(&ed->snl))
13933fe401a5SEd Maste 		return;
13943fe401a5SEd Maste 
13953fe401a5SEd Maste 	if ((ed->flags & SOLARIS_FMT) == 0)
13963fe401a5SEd Maste 		PRT("\nsection header:\n");
1397b6b6f9ccSEd Maste 	for (i = 0; i < ed->shnum; i++) {
13983fe401a5SEd Maste 		s = &ed->sl[i];
13993fe401a5SEd Maste 		if (ed->flags & SOLARIS_FMT) {
14003fe401a5SEd Maste 			if (i == 0)
14013fe401a5SEd Maste 				continue;
1402b6b6f9ccSEd Maste 			PRT("\nSection Header[%zu]:", i);
14033fe401a5SEd Maste 			PRT("  sh_name: %s\n", s->name);
14043fe401a5SEd Maste 			PRT("    sh_addr:      %#-14jx", (uintmax_t)s->addr);
14053fe401a5SEd Maste 			if (s->flags != 0)
14063fe401a5SEd Maste 				PRT("  sh_flags:   [ %s ]\n", sh_flags(s->flags));
14073fe401a5SEd Maste 			else
14083fe401a5SEd Maste 				PRT("  sh_flags:   0\n");
14093fe401a5SEd Maste 			PRT("    sh_size:      %#-14jx", (uintmax_t)s->sz);
1410839529caSEd Maste 			PRT("  sh_type:    [ %s ]\n",
1411839529caSEd Maste 			    sh_types(ed->ehdr.e_machine, s->type));
14123fe401a5SEd Maste 			PRT("    sh_offset:    %#-14jx", (uintmax_t)s->off);
14133fe401a5SEd Maste 			PRT("  sh_entsize: %#jx\n", (uintmax_t)s->entsize);
14143fe401a5SEd Maste 			PRT("    sh_link:      %-14u", s->link);
14153fe401a5SEd Maste 			PRT("  sh_info:    %u\n", s->info);
14163fe401a5SEd Maste 			PRT("    sh_addralign: %#jx\n", (uintmax_t)s->align);
14173fe401a5SEd Maste 		} else {
14183fe401a5SEd Maste 			PRT("\n");
14193fe401a5SEd Maste 			PRT("entry: %ju\n", (uintmax_t)i);
14203fe401a5SEd Maste 			PRT("\tsh_name: %s\n", s->name);
1421839529caSEd Maste 			PRT("\tsh_type: %s\n",
1422839529caSEd Maste 			    sh_types(ed->ehdr.e_machine, s->type));
14233fe401a5SEd Maste 			PRT("\tsh_flags: %s\n", sh_flags(s->flags));
14243fe401a5SEd Maste 			PRT("\tsh_addr: %#jx\n", (uintmax_t)s->addr);
14253fe401a5SEd Maste 			PRT("\tsh_offset: %ju\n", (uintmax_t)s->off);
14263fe401a5SEd Maste 			PRT("\tsh_size: %ju\n", (uintmax_t)s->sz);
14273fe401a5SEd Maste 			PRT("\tsh_link: %u\n", s->link);
14283fe401a5SEd Maste 			PRT("\tsh_info: %u\n", s->info);
14293fe401a5SEd Maste 			PRT("\tsh_addralign: %ju\n", (uintmax_t)s->align);
14303fe401a5SEd Maste 			PRT("\tsh_entsize: %ju\n", (uintmax_t)s->entsize);
14313fe401a5SEd Maste 		}
14323fe401a5SEd Maste 	}
14333fe401a5SEd Maste }
14343fe401a5SEd Maste 
14353fe401a5SEd Maste /*
1436656f49f8SEd Maste  * Return number of entries in the given section. We'd prefer ent_count be a
1437656f49f8SEd Maste  * size_t, but libelf APIs already use int for section indices.
1438656f49f8SEd Maste  */
1439656f49f8SEd Maste static int
1440656f49f8SEd Maste get_ent_count(const struct section *s, int *ent_count)
1441656f49f8SEd Maste {
1442656f49f8SEd Maste 	if (s->entsize == 0) {
1443656f49f8SEd Maste 		warnx("section %s has entry size 0", s->name);
1444656f49f8SEd Maste 		return (0);
1445656f49f8SEd Maste 	} else if (s->sz / s->entsize > INT_MAX) {
1446656f49f8SEd Maste 		warnx("section %s has invalid section count", s->name);
1447656f49f8SEd Maste 		return (0);
1448656f49f8SEd Maste 	}
1449656f49f8SEd Maste 	*ent_count = (int)(s->sz / s->entsize);
1450656f49f8SEd Maste 	return (1);
1451656f49f8SEd Maste }
1452656f49f8SEd Maste 
1453656f49f8SEd Maste /*
14543fe401a5SEd Maste  * Retrieve the content of the corresponding SHT_SUNW_versym section for
14553fe401a5SEd Maste  * a symbol table section.
14563fe401a5SEd Maste  */
14573fe401a5SEd Maste static void
14583fe401a5SEd Maste get_versym(struct elfdump *ed, int i, uint16_t **vs, int *nvs)
14593fe401a5SEd Maste {
14603fe401a5SEd Maste 	struct section	*s;
14613fe401a5SEd Maste 	Elf_Data	*data;
1462b6b6f9ccSEd Maste 	size_t		 j;
1463b6b6f9ccSEd Maste 	int		 elferr;
14643fe401a5SEd Maste 
14653fe401a5SEd Maste 	s = NULL;
1466b6b6f9ccSEd Maste 	for (j = 0; j < ed->shnum; j++) {
14673fe401a5SEd Maste 		s = &ed->sl[j];
14683fe401a5SEd Maste 		if (s->type == SHT_SUNW_versym && s->link == (uint32_t)i)
14693fe401a5SEd Maste 			break;
14703fe401a5SEd Maste 	}
1471b6b6f9ccSEd Maste 	if (j >= ed->shnum) {
14723fe401a5SEd Maste 		*vs = NULL;
14733fe401a5SEd Maste 		return;
14743fe401a5SEd Maste 	}
14753fe401a5SEd Maste 	(void) elf_errno();
14763fe401a5SEd Maste 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
14773fe401a5SEd Maste 		elferr = elf_errno();
14783fe401a5SEd Maste 		if (elferr != 0)
14793fe401a5SEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
14803fe401a5SEd Maste 		*vs = NULL;
14813fe401a5SEd Maste 		return;
14823fe401a5SEd Maste 	}
14833fe401a5SEd Maste 
14843fe401a5SEd Maste 	*vs = data->d_buf;
1485656f49f8SEd Maste 	assert(data->d_size == s->sz);
1486656f49f8SEd Maste 	if (!get_ent_count(s, nvs))
1487656f49f8SEd Maste 		*nvs = 0;
14883fe401a5SEd Maste }
14893fe401a5SEd Maste 
14903fe401a5SEd Maste /*
14913fe401a5SEd Maste  * Dump the symbol table section.
14923fe401a5SEd Maste  */
14933fe401a5SEd Maste static void
14943fe401a5SEd Maste elf_print_symtab(struct elfdump *ed, int i)
14953fe401a5SEd Maste {
14963fe401a5SEd Maste 	struct section	*s;
14973fe401a5SEd Maste 	const char	*name;
14983fe401a5SEd Maste 	uint16_t	*vs;
1499*715d1396SEd Maste 	char		 idx[13];
15003fe401a5SEd Maste 	Elf_Data	*data;
15013fe401a5SEd Maste 	GElf_Sym	 sym;
15023fe401a5SEd Maste 	int		 len, j, elferr, nvs;
15033fe401a5SEd Maste 
15043fe401a5SEd Maste 	s = &ed->sl[i];
15053fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT)
15063fe401a5SEd Maste 		PRT("\nSymbol Table Section:  %s\n", s->name);
15073fe401a5SEd Maste 	else
15083fe401a5SEd Maste 		PRT("\nsymbol table (%s):\n", s->name);
15093fe401a5SEd Maste 	(void) elf_errno();
15103fe401a5SEd Maste 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
15113fe401a5SEd Maste 		elferr = elf_errno();
15123fe401a5SEd Maste 		if (elferr != 0)
15133fe401a5SEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
15143fe401a5SEd Maste 		return;
15153fe401a5SEd Maste 	}
15163fe401a5SEd Maste 	vs = NULL;
15173fe401a5SEd Maste 	nvs = 0;
1518656f49f8SEd Maste 	assert(data->d_size == s->sz);
1519656f49f8SEd Maste 	if (!get_ent_count(s, &len))
1520656f49f8SEd Maste 		return;
15213fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT) {
15223fe401a5SEd Maste 		if (ed->ec == ELFCLASS32)
15233fe401a5SEd Maste 			PRT("     index    value       ");
15243fe401a5SEd Maste 		else
15253fe401a5SEd Maste 			PRT("     index        value           ");
15263fe401a5SEd Maste 		PRT("size     type bind oth ver shndx       name\n");
15273fe401a5SEd Maste 		get_versym(ed, i, &vs, &nvs);
15283fe401a5SEd Maste 		if (vs != NULL && nvs != len) {
15293fe401a5SEd Maste 			warnx("#symbol not equal to #versym");
15303fe401a5SEd Maste 			vs = NULL;
15313fe401a5SEd Maste 		}
15323fe401a5SEd Maste 	}
15333fe401a5SEd Maste 	for (j = 0; j < len; j++) {
15343fe401a5SEd Maste 		if (gelf_getsym(data, j, &sym) != &sym) {
15353fe401a5SEd Maste 			warnx("gelf_getsym failed: %s", elf_errmsg(-1));
15363fe401a5SEd Maste 			continue;
15373fe401a5SEd Maste 		}
15383fe401a5SEd Maste 		name = get_string(ed, s->link, sym.st_name);
15393fe401a5SEd Maste 		if (ed->flags & SOLARIS_FMT) {
15403fe401a5SEd Maste 			snprintf(idx, sizeof(idx), "[%d]", j);
15413fe401a5SEd Maste 			if (ed->ec == ELFCLASS32)
15423fe401a5SEd Maste 				PRT("%10s  ", idx);
15433fe401a5SEd Maste 			else
15443fe401a5SEd Maste 				PRT("%10s      ", idx);
15453fe401a5SEd Maste 			PRT("0x%8.8jx ", (uintmax_t)sym.st_value);
15463fe401a5SEd Maste 			if (ed->ec == ELFCLASS32)
15473fe401a5SEd Maste 				PRT("0x%8.8jx  ", (uintmax_t)sym.st_size);
15483fe401a5SEd Maste 			else
15493fe401a5SEd Maste 				PRT("0x%12.12jx  ", (uintmax_t)sym.st_size);
1550839529caSEd Maste 			PRT("%s ", st_type_S(GELF_ST_TYPE(sym.st_info)));
1551839529caSEd Maste 			PRT("%s  ", st_bindings_S(GELF_ST_BIND(sym.st_info)));
15523fe401a5SEd Maste 			PRT("%c  ", st_others[sym.st_other]);
15533fe401a5SEd Maste 			PRT("%3u ", (vs == NULL ? 0 : vs[j]));
15543fe401a5SEd Maste 			PRT("%-11.11s ", sh_name(ed, sym.st_shndx));
15553fe401a5SEd Maste 			PRT("%s\n", name);
15563fe401a5SEd Maste 		} else {
15573fe401a5SEd Maste 			PRT("\nentry: %d\n", j);
15583fe401a5SEd Maste 			PRT("\tst_name: %s\n", name);
15593fe401a5SEd Maste 			PRT("\tst_value: %#jx\n", (uintmax_t)sym.st_value);
15603fe401a5SEd Maste 			PRT("\tst_size: %ju\n", (uintmax_t)sym.st_size);
15613fe401a5SEd Maste 			PRT("\tst_info: %s %s\n",
1562839529caSEd Maste 			    st_type(ed->ehdr.e_machine,
1563839529caSEd Maste 			    GELF_ST_TYPE(sym.st_info)),
1564839529caSEd Maste 			    st_bindings(GELF_ST_BIND(sym.st_info)));
15653fe401a5SEd Maste 			PRT("\tst_shndx: %ju\n", (uintmax_t)sym.st_shndx);
15663fe401a5SEd Maste 		}
15673fe401a5SEd Maste 	}
15683fe401a5SEd Maste }
15693fe401a5SEd Maste 
15703fe401a5SEd Maste /*
15713fe401a5SEd Maste  * Dump the symbol tables. (.dynsym and .symtab)
15723fe401a5SEd Maste  */
15733fe401a5SEd Maste static void
15743fe401a5SEd Maste elf_print_symtabs(struct elfdump *ed)
15753fe401a5SEd Maste {
1576b6b6f9ccSEd Maste 	size_t i;
15773fe401a5SEd Maste 
1578b6b6f9ccSEd Maste 	for (i = 0; i < ed->shnum; i++)
15793fe401a5SEd Maste 		if ((ed->sl[i].type == SHT_SYMTAB ||
15803fe401a5SEd Maste 		    ed->sl[i].type == SHT_DYNSYM) &&
15813fe401a5SEd Maste 		    (STAILQ_EMPTY(&ed->snl) || find_name(ed, ed->sl[i].name)))
15823fe401a5SEd Maste 			elf_print_symtab(ed, i);
15833fe401a5SEd Maste }
15843fe401a5SEd Maste 
15853fe401a5SEd Maste /*
15863fe401a5SEd Maste  * Dump the content of .dynamic section.
15873fe401a5SEd Maste  */
15883fe401a5SEd Maste static void
15893fe401a5SEd Maste elf_print_dynamic(struct elfdump *ed)
15903fe401a5SEd Maste {
15913fe401a5SEd Maste 	struct section	*s;
15923fe401a5SEd Maste 	const char	*name;
1593*715d1396SEd Maste 	char		 idx[13];
15943fe401a5SEd Maste 	Elf_Data	*data;
15953fe401a5SEd Maste 	GElf_Dyn	 dyn;
15963fe401a5SEd Maste 	int		 elferr, i, len;
15973fe401a5SEd Maste 
15983fe401a5SEd Maste 	s = NULL;
15993fe401a5SEd Maste 	for (i = 0; (size_t)i < ed->shnum; i++) {
16003fe401a5SEd Maste 		s = &ed->sl[i];
16013fe401a5SEd Maste 		if (s->type == SHT_DYNAMIC &&
16023fe401a5SEd Maste 		    (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name)))
16033fe401a5SEd Maste 			break;
16043fe401a5SEd Maste 	}
16053fe401a5SEd Maste 	if ((size_t)i >= ed->shnum)
16063fe401a5SEd Maste 		return;
16073fe401a5SEd Maste 
16083fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT) {
16093fe401a5SEd Maste 		PRT("Dynamic Section:  %s\n", s->name);
16103fe401a5SEd Maste 		PRT("     index  tag               value\n");
16113fe401a5SEd Maste 	} else
16123fe401a5SEd Maste 		PRT("\ndynamic:\n");
16133fe401a5SEd Maste 	(void) elf_errno();
16143fe401a5SEd Maste 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
16153fe401a5SEd Maste 		elferr = elf_errno();
16163fe401a5SEd Maste 		if (elferr != 0)
16173fe401a5SEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
16183fe401a5SEd Maste 		return;
16193fe401a5SEd Maste 	}
1620656f49f8SEd Maste 	assert(data->d_size == s->sz);
1621656f49f8SEd Maste 	if (!get_ent_count(s, &len))
1622656f49f8SEd Maste 		return;
16233fe401a5SEd Maste 	for (i = 0; i < len; i++) {
16243fe401a5SEd Maste 		if (gelf_getdyn(data, i, &dyn) != &dyn) {
16253fe401a5SEd Maste 			warnx("gelf_getdyn failed: %s", elf_errmsg(-1));
16263fe401a5SEd Maste 			continue;
16273fe401a5SEd Maste 		}
16283fe401a5SEd Maste 
16293fe401a5SEd Maste 		if (ed->flags & SOLARIS_FMT) {
16303fe401a5SEd Maste 			snprintf(idx, sizeof(idx), "[%d]", i);
16313fe401a5SEd Maste 			PRT("%10s  %-16s ", idx, d_tags(dyn.d_tag));
16323fe401a5SEd Maste 		} else {
16333fe401a5SEd Maste 			PRT("\n");
16343fe401a5SEd Maste 			PRT("entry: %d\n", i);
16353fe401a5SEd Maste 			PRT("\td_tag: %s\n", d_tags(dyn.d_tag));
16363fe401a5SEd Maste 		}
16373fe401a5SEd Maste 		switch(dyn.d_tag) {
16383fe401a5SEd Maste 		case DT_NEEDED:
16393fe401a5SEd Maste 		case DT_SONAME:
16403fe401a5SEd Maste 		case DT_RPATH:
1641b6b6f9ccSEd Maste 		case DT_RUNPATH:
16423fe401a5SEd Maste 			if ((name = elf_strptr(ed->elf, s->link,
16433fe401a5SEd Maste 				    dyn.d_un.d_val)) == NULL)
16443fe401a5SEd Maste 				name = "";
16453fe401a5SEd Maste 			if (ed->flags & SOLARIS_FMT)
16463fe401a5SEd Maste 				PRT("%#-16jx %s\n", (uintmax_t)dyn.d_un.d_val,
16473fe401a5SEd Maste 				    name);
16483fe401a5SEd Maste 			else
16493fe401a5SEd Maste 				PRT("\td_val: %s\n", name);
16503fe401a5SEd Maste 			break;
16513fe401a5SEd Maste 		case DT_PLTRELSZ:
16523fe401a5SEd Maste 		case DT_RELA:
16533fe401a5SEd Maste 		case DT_RELASZ:
16543fe401a5SEd Maste 		case DT_RELAENT:
16553fe401a5SEd Maste 		case DT_RELACOUNT:
16563fe401a5SEd Maste 		case DT_STRSZ:
16573fe401a5SEd Maste 		case DT_SYMENT:
16583fe401a5SEd Maste 		case DT_RELSZ:
16593fe401a5SEd Maste 		case DT_RELENT:
16603fe401a5SEd Maste 		case DT_PLTREL:
16613fe401a5SEd Maste 		case DT_VERDEF:
16623fe401a5SEd Maste 		case DT_VERDEFNUM:
16633fe401a5SEd Maste 		case DT_VERNEED:
16643fe401a5SEd Maste 		case DT_VERNEEDNUM:
16653fe401a5SEd Maste 		case DT_VERSYM:
16663fe401a5SEd Maste 			if (ed->flags & SOLARIS_FMT)
16673fe401a5SEd Maste 				PRT("%#jx\n", (uintmax_t)dyn.d_un.d_val);
16683fe401a5SEd Maste 			else
16693fe401a5SEd Maste 				PRT("\td_val: %ju\n",
16703fe401a5SEd Maste 				    (uintmax_t)dyn.d_un.d_val);
16713fe401a5SEd Maste 			break;
16723fe401a5SEd Maste 		case DT_PLTGOT:
16733fe401a5SEd Maste 		case DT_HASH:
16743fe401a5SEd Maste 		case DT_GNU_HASH:
16753fe401a5SEd Maste 		case DT_STRTAB:
16763fe401a5SEd Maste 		case DT_SYMTAB:
16773fe401a5SEd Maste 		case DT_INIT:
16783fe401a5SEd Maste 		case DT_FINI:
16793fe401a5SEd Maste 		case DT_REL:
16803fe401a5SEd Maste 		case DT_JMPREL:
16813fe401a5SEd Maste 		case DT_DEBUG:
16823fe401a5SEd Maste 			if (ed->flags & SOLARIS_FMT)
16833fe401a5SEd Maste 				PRT("%#jx\n", (uintmax_t)dyn.d_un.d_ptr);
16843fe401a5SEd Maste 			else
16853fe401a5SEd Maste 				PRT("\td_ptr: %#jx\n",
16863fe401a5SEd Maste 				    (uintmax_t)dyn.d_un.d_ptr);
16873fe401a5SEd Maste 			break;
16883fe401a5SEd Maste 		case DT_NULL:
16893fe401a5SEd Maste 		case DT_SYMBOLIC:
16903fe401a5SEd Maste 		case DT_TEXTREL:
16913fe401a5SEd Maste 		default:
16923fe401a5SEd Maste 			if (ed->flags & SOLARIS_FMT)
16933fe401a5SEd Maste 				PRT("\n");
16943fe401a5SEd Maste 			break;
16953fe401a5SEd Maste 		}
16963fe401a5SEd Maste 	}
16973fe401a5SEd Maste }
16983fe401a5SEd Maste 
16993fe401a5SEd Maste /*
17003fe401a5SEd Maste  * Dump a .rel/.rela section entry.
17013fe401a5SEd Maste  */
17023fe401a5SEd Maste static void
17033fe401a5SEd Maste elf_print_rel_entry(struct elfdump *ed, struct section *s, int j,
17043fe401a5SEd Maste     struct rel_entry *r)
17053fe401a5SEd Maste {
17063fe401a5SEd Maste 
17073fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT) {
1708b6b6f9ccSEd Maste 		PRT("        %-23s ", elftc_reloc_type_str(ed->ehdr.e_machine,
17093fe401a5SEd Maste 			GELF_R_TYPE(r->u_r.rel.r_info)));
17103fe401a5SEd Maste 		PRT("%#12jx ", (uintmax_t)r->u_r.rel.r_offset);
17113fe401a5SEd Maste 		if (r->type == SHT_RELA)
17123fe401a5SEd Maste 			PRT("%10jd  ", (intmax_t)r->u_r.rela.r_addend);
17133fe401a5SEd Maste 		else
17143fe401a5SEd Maste 			PRT("    ");
17153fe401a5SEd Maste 		PRT("%-14s ", s->name);
17163fe401a5SEd Maste 		PRT("%s\n", r->symn);
17173fe401a5SEd Maste 	} else {
17183fe401a5SEd Maste 		PRT("\n");
17193fe401a5SEd Maste 		PRT("entry: %d\n", j);
17203fe401a5SEd Maste 		PRT("\tr_offset: %#jx\n", (uintmax_t)r->u_r.rel.r_offset);
17213fe401a5SEd Maste 		if (ed->ec == ELFCLASS32)
17223fe401a5SEd Maste 			PRT("\tr_info: %#jx\n", (uintmax_t)
17233fe401a5SEd Maste 			    ELF32_R_INFO(ELF64_R_SYM(r->u_r.rel.r_info),
17243fe401a5SEd Maste 			    ELF64_R_TYPE(r->u_r.rel.r_info)));
17253fe401a5SEd Maste 		else
17263fe401a5SEd Maste 			PRT("\tr_info: %#jx\n", (uintmax_t)r->u_r.rel.r_info);
17273fe401a5SEd Maste 		if (r->type == SHT_RELA)
17283fe401a5SEd Maste 			PRT("\tr_addend: %jd\n",
17293fe401a5SEd Maste 			    (intmax_t)r->u_r.rela.r_addend);
17303fe401a5SEd Maste 	}
17313fe401a5SEd Maste }
17323fe401a5SEd Maste 
17333fe401a5SEd Maste /*
17343fe401a5SEd Maste  * Dump a relocation section of type SHT_RELA.
17353fe401a5SEd Maste  */
17363fe401a5SEd Maste static void
17373fe401a5SEd Maste elf_print_rela(struct elfdump *ed, struct section *s, Elf_Data *data)
17383fe401a5SEd Maste {
17393fe401a5SEd Maste 	struct rel_entry	r;
17403fe401a5SEd Maste 	int			j, len;
17413fe401a5SEd Maste 
17423fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT) {
17433fe401a5SEd Maste 		PRT("\nRelocation Section:  %s\n", s->name);
17443fe401a5SEd Maste 		PRT("        type                          offset     "
17453fe401a5SEd Maste 		    "addend  section        with respect to\n");
17463fe401a5SEd Maste 	} else
17473fe401a5SEd Maste 		PRT("\nrelocation with addend (%s):\n", s->name);
17483fe401a5SEd Maste 	r.type = SHT_RELA;
1749656f49f8SEd Maste 	assert(data->d_size == s->sz);
1750656f49f8SEd Maste 	if (!get_ent_count(s, &len))
1751656f49f8SEd Maste 		return;
17523fe401a5SEd Maste 	for (j = 0; j < len; j++) {
17533fe401a5SEd Maste 		if (gelf_getrela(data, j, &r.u_r.rela) != &r.u_r.rela) {
17543fe401a5SEd Maste 			warnx("gelf_getrela failed: %s",
17553fe401a5SEd Maste 			    elf_errmsg(-1));
17563fe401a5SEd Maste 			continue;
17573fe401a5SEd Maste 		}
17583fe401a5SEd Maste 		r.symn = get_symbol_name(ed, s->link,
17593fe401a5SEd Maste 		    GELF_R_SYM(r.u_r.rela.r_info));
17603fe401a5SEd Maste 		elf_print_rel_entry(ed, s, j, &r);
17613fe401a5SEd Maste 	}
17623fe401a5SEd Maste }
17633fe401a5SEd Maste 
17643fe401a5SEd Maste /*
17653fe401a5SEd Maste  * Dump a relocation section of type SHT_REL.
17663fe401a5SEd Maste  */
17673fe401a5SEd Maste static void
17683fe401a5SEd Maste elf_print_rel(struct elfdump *ed, struct section *s, Elf_Data *data)
17693fe401a5SEd Maste {
17703fe401a5SEd Maste 	struct rel_entry	r;
17713fe401a5SEd Maste 	int			j, len;
17723fe401a5SEd Maste 
17733fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT) {
17743fe401a5SEd Maste 		PRT("\nRelocation Section:  %s\n", s->name);
17753fe401a5SEd Maste 		PRT("        type                          offset     "
17763fe401a5SEd Maste 		    "section        with respect to\n");
17773fe401a5SEd Maste 	} else
17783fe401a5SEd Maste 		PRT("\nrelocation (%s):\n", s->name);
17793fe401a5SEd Maste 	r.type = SHT_REL;
1780656f49f8SEd Maste 	assert(data->d_size == s->sz);
1781656f49f8SEd Maste 	if (!get_ent_count(s, &len))
1782656f49f8SEd Maste 		return;
17833fe401a5SEd Maste 	for (j = 0; j < len; j++) {
17843fe401a5SEd Maste 		if (gelf_getrel(data, j, &r.u_r.rel) != &r.u_r.rel) {
17853fe401a5SEd Maste 			warnx("gelf_getrel failed: %s", elf_errmsg(-1));
17863fe401a5SEd Maste 			continue;
17873fe401a5SEd Maste 		}
17883fe401a5SEd Maste 		r.symn = get_symbol_name(ed, s->link,
17893fe401a5SEd Maste 		    GELF_R_SYM(r.u_r.rel.r_info));
17903fe401a5SEd Maste 		elf_print_rel_entry(ed, s, j, &r);
17913fe401a5SEd Maste 	}
17923fe401a5SEd Maste }
17933fe401a5SEd Maste 
17943fe401a5SEd Maste /*
17953fe401a5SEd Maste  * Dump relocation sections.
17963fe401a5SEd Maste  */
17973fe401a5SEd Maste static void
17983fe401a5SEd Maste elf_print_reloc(struct elfdump *ed)
17993fe401a5SEd Maste {
18003fe401a5SEd Maste 	struct section	*s;
18013fe401a5SEd Maste 	Elf_Data	*data;
1802b6b6f9ccSEd Maste 	size_t		 i;
1803b6b6f9ccSEd Maste 	int		 elferr;
18043fe401a5SEd Maste 
1805b6b6f9ccSEd Maste 	for (i = 0; i < ed->shnum; i++) {
18063fe401a5SEd Maste 		s = &ed->sl[i];
18073fe401a5SEd Maste 		if ((s->type == SHT_REL || s->type == SHT_RELA) &&
18083fe401a5SEd Maste 		    (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name))) {
18093fe401a5SEd Maste 			(void) elf_errno();
18103fe401a5SEd Maste 			if ((data = elf_getdata(s->scn, NULL)) == NULL) {
18113fe401a5SEd Maste 				elferr = elf_errno();
18123fe401a5SEd Maste 				if (elferr != 0)
18133fe401a5SEd Maste 					warnx("elf_getdata failed: %s",
18143fe401a5SEd Maste 					    elf_errmsg(elferr));
18153fe401a5SEd Maste 				continue;
18163fe401a5SEd Maste 			}
18173fe401a5SEd Maste 			if (s->type == SHT_REL)
18183fe401a5SEd Maste 				elf_print_rel(ed, s, data);
18193fe401a5SEd Maste 			else
18203fe401a5SEd Maste 				elf_print_rela(ed, s, data);
18213fe401a5SEd Maste 		}
18223fe401a5SEd Maste 	}
18233fe401a5SEd Maste }
18243fe401a5SEd Maste 
18253fe401a5SEd Maste /*
18263fe401a5SEd Maste  * Dump the content of PT_INTERP segment.
18273fe401a5SEd Maste  */
18283fe401a5SEd Maste static void
18293fe401a5SEd Maste elf_print_interp(struct elfdump *ed)
18303fe401a5SEd Maste {
18313fe401a5SEd Maste 	const char *s;
18323fe401a5SEd Maste 	GElf_Phdr phdr;
1833b6b6f9ccSEd Maste 	size_t filesize, i, phnum;
18343fe401a5SEd Maste 
18353fe401a5SEd Maste 	if (!STAILQ_EMPTY(&ed->snl) && find_name(ed, "PT_INTERP") == NULL)
18363fe401a5SEd Maste 		return;
18373fe401a5SEd Maste 
1838b6b6f9ccSEd Maste 	if ((s = elf_rawfile(ed->elf, &filesize)) == NULL) {
18393fe401a5SEd Maste 		warnx("elf_rawfile failed: %s", elf_errmsg(-1));
18403fe401a5SEd Maste 		return;
18413fe401a5SEd Maste 	}
18423fe401a5SEd Maste 	if (!elf_getphnum(ed->elf, &phnum)) {
18433fe401a5SEd Maste 		warnx("elf_getphnum failed: %s", elf_errmsg(-1));
18443fe401a5SEd Maste 		return;
18453fe401a5SEd Maste 	}
1846b6b6f9ccSEd Maste 	for (i = 0; i < phnum; i++) {
18473fe401a5SEd Maste 		if (gelf_getphdr(ed->elf, i, &phdr) != &phdr) {
18483fe401a5SEd Maste 			warnx("elf_getphdr failed: %s", elf_errmsg(-1));
18493fe401a5SEd Maste 			continue;
18503fe401a5SEd Maste 		}
18513fe401a5SEd Maste 		if (phdr.p_type == PT_INTERP) {
1852b6b6f9ccSEd Maste 			if (phdr.p_offset >= filesize) {
1853b6b6f9ccSEd Maste 				warnx("invalid phdr offset");
1854b6b6f9ccSEd Maste 				continue;
1855b6b6f9ccSEd Maste 			}
18563fe401a5SEd Maste 			PRT("\ninterp:\n");
18573fe401a5SEd Maste 			PRT("\t%s\n", s + phdr.p_offset);
18583fe401a5SEd Maste 		}
18593fe401a5SEd Maste 	}
18603fe401a5SEd Maste }
18613fe401a5SEd Maste 
18623fe401a5SEd Maste /*
1863b6b6f9ccSEd Maste  * Search the relocation sections for entries referring to the .got section.
18643fe401a5SEd Maste  */
18653fe401a5SEd Maste static void
18663fe401a5SEd Maste find_gotrel(struct elfdump *ed, struct section *gs, struct rel_entry *got)
18673fe401a5SEd Maste {
18683fe401a5SEd Maste 	struct section		*s;
18693fe401a5SEd Maste 	struct rel_entry	 r;
18703fe401a5SEd Maste 	Elf_Data		*data;
1871b6b6f9ccSEd Maste 	size_t			 i;
1872b6b6f9ccSEd Maste 	int			 elferr, j, k, len;
18733fe401a5SEd Maste 
1874b6b6f9ccSEd Maste 	for(i = 0; i < ed->shnum; i++) {
18753fe401a5SEd Maste 		s = &ed->sl[i];
18763fe401a5SEd Maste 		if (s->type != SHT_REL && s->type != SHT_RELA)
18773fe401a5SEd Maste 			continue;
18783fe401a5SEd Maste 		(void) elf_errno();
18793fe401a5SEd Maste 		if ((data = elf_getdata(s->scn, NULL)) == NULL) {
18803fe401a5SEd Maste 			elferr = elf_errno();
18813fe401a5SEd Maste 			if (elferr != 0)
18823fe401a5SEd Maste 				warnx("elf_getdata failed: %s",
18833fe401a5SEd Maste 				    elf_errmsg(elferr));
18843fe401a5SEd Maste 			return;
18853fe401a5SEd Maste 		}
18863fe401a5SEd Maste 		memset(&r, 0, sizeof(struct rel_entry));
18873fe401a5SEd Maste 		r.type = s->type;
1888656f49f8SEd Maste 		assert(data->d_size == s->sz);
1889656f49f8SEd Maste 		if (!get_ent_count(s, &len))
1890656f49f8SEd Maste 			return;
18913fe401a5SEd Maste 		for (j = 0; j < len; j++) {
18923fe401a5SEd Maste 			if (s->type == SHT_REL) {
18933fe401a5SEd Maste 				if (gelf_getrel(data, j, &r.u_r.rel) !=
18943fe401a5SEd Maste 				    &r.u_r.rel) {
18953fe401a5SEd Maste 					warnx("gelf_getrel failed: %s",
18963fe401a5SEd Maste 					    elf_errmsg(-1));
18973fe401a5SEd Maste 					continue;
18983fe401a5SEd Maste 				}
18993fe401a5SEd Maste 			} else {
19003fe401a5SEd Maste 				if (gelf_getrela(data, j, &r.u_r.rela) !=
19013fe401a5SEd Maste 				    &r.u_r.rela) {
19023fe401a5SEd Maste 					warnx("gelf_getrel failed: %s",
19033fe401a5SEd Maste 					    elf_errmsg(-1));
19043fe401a5SEd Maste 					continue;
19053fe401a5SEd Maste 				}
19063fe401a5SEd Maste 			}
19073fe401a5SEd Maste 			if (r.u_r.rel.r_offset >= gs->addr &&
19083fe401a5SEd Maste 			    r.u_r.rel.r_offset < gs->addr + gs->sz) {
19093fe401a5SEd Maste 				r.symn = get_symbol_name(ed, s->link,
19103fe401a5SEd Maste 				    GELF_R_SYM(r.u_r.rel.r_info));
19113fe401a5SEd Maste 				k = (r.u_r.rel.r_offset - gs->addr) /
19123fe401a5SEd Maste 				    gs->entsize;
19133fe401a5SEd Maste 				memcpy(&got[k], &r, sizeof(struct rel_entry));
19143fe401a5SEd Maste 			}
19153fe401a5SEd Maste 		}
19163fe401a5SEd Maste 	}
19173fe401a5SEd Maste }
19183fe401a5SEd Maste 
19193fe401a5SEd Maste static void
19203fe401a5SEd Maste elf_print_got_section(struct elfdump *ed, struct section *s)
19213fe401a5SEd Maste {
19223fe401a5SEd Maste 	struct rel_entry	*got;
19233fe401a5SEd Maste 	Elf_Data		*data, dst;
19243fe401a5SEd Maste 	int			 elferr, i, len;
19253fe401a5SEd Maste 
19263fe401a5SEd Maste 	if (s->entsize == 0) {
19273fe401a5SEd Maste 		/* XXX IA64 GOT section generated by gcc has entry size 0. */
19283fe401a5SEd Maste 		if (s->align != 0)
19293fe401a5SEd Maste 			s->entsize = s->align;
19303fe401a5SEd Maste 		else
19313fe401a5SEd Maste 			return;
19323fe401a5SEd Maste 	}
19333fe401a5SEd Maste 
1934656f49f8SEd Maste 	if (!get_ent_count(s, &len))
1935656f49f8SEd Maste 		return;
19363fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT)
1937656f49f8SEd Maste 		PRT("\nGlobal Offset Table Section:  %s  (%d entries)\n",
1938656f49f8SEd Maste 		    s->name, len);
19393fe401a5SEd Maste 	else
19403fe401a5SEd Maste 		PRT("\nglobal offset table: %s\n", s->name);
19413fe401a5SEd Maste 	(void) elf_errno();
19423fe401a5SEd Maste 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
19433fe401a5SEd Maste 		elferr = elf_errno();
19443fe401a5SEd Maste 		if (elferr != 0)
19453fe401a5SEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
19463fe401a5SEd Maste 		return;
19473fe401a5SEd Maste 	}
19483fe401a5SEd Maste 
19493fe401a5SEd Maste 	/*
19503fe401a5SEd Maste 	 * GOT section has section type SHT_PROGBITS, thus libelf treats it as
1951b6b6f9ccSEd Maste 	 * byte stream and will not perform any translation on it. As a result,
19523fe401a5SEd Maste 	 * an exlicit call to gelf_xlatetom is needed here. Depends on arch,
19533fe401a5SEd Maste 	 * GOT section should be translated to either WORD or XWORD.
19543fe401a5SEd Maste 	 */
19553fe401a5SEd Maste 	if (ed->ec == ELFCLASS32)
19563fe401a5SEd Maste 		data->d_type = ELF_T_WORD;
19573fe401a5SEd Maste 	else
19583fe401a5SEd Maste 		data->d_type = ELF_T_XWORD;
19593fe401a5SEd Maste 	memcpy(&dst, data, sizeof(Elf_Data));
19603fe401a5SEd Maste 	if (gelf_xlatetom(ed->elf, &dst, data, ed->ehdr.e_ident[EI_DATA]) !=
19613fe401a5SEd Maste 	    &dst) {
19623fe401a5SEd Maste 		warnx("gelf_xlatetom failed: %s", elf_errmsg(-1));
19633fe401a5SEd Maste 		return;
19643fe401a5SEd Maste 	}
1965656f49f8SEd Maste 	assert(dst.d_size == s->sz);
19663fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT) {
19673fe401a5SEd Maste 		/*
19683fe401a5SEd Maste 		 * In verbose/Solaris mode, we search the relocation sections
19693fe401a5SEd Maste 		 * and try to find the corresponding reloc entry for each GOT
19703fe401a5SEd Maste 		 * section entry.
19713fe401a5SEd Maste 		 */
19723fe401a5SEd Maste 		if ((got = calloc(len, sizeof(struct rel_entry))) == NULL)
19733fe401a5SEd Maste 			err(EXIT_FAILURE, "calloc failed");
19743fe401a5SEd Maste 		find_gotrel(ed, s, got);
19753fe401a5SEd Maste 		if (ed->ec == ELFCLASS32) {
19763fe401a5SEd Maste 			PRT(" ndx     addr      value    reloc              ");
19773fe401a5SEd Maste 			PRT("addend   symbol\n");
19783fe401a5SEd Maste 		} else {
19793fe401a5SEd Maste 			PRT(" ndx     addr              value             ");
19803fe401a5SEd Maste 			PRT("reloc              addend       symbol\n");
19813fe401a5SEd Maste 		}
19823fe401a5SEd Maste 		for(i = 0; i < len; i++) {
19833fe401a5SEd Maste 			PRT("[%5.5d]  ", i);
19843fe401a5SEd Maste 			if (ed->ec == ELFCLASS32) {
1985839529caSEd Maste 				PRT("%-8.8jx  ",
1986839529caSEd Maste 				    (uintmax_t) (s->addr + i * s->entsize));
19873fe401a5SEd Maste 				PRT("%-8.8x ", *((uint32_t *)dst.d_buf + i));
19883fe401a5SEd Maste 			} else {
1989839529caSEd Maste 				PRT("%-16.16jx  ",
1990839529caSEd Maste 				    (uintmax_t) (s->addr + i * s->entsize));
1991839529caSEd Maste 				PRT("%-16.16jx  ",
1992839529caSEd Maste 				    (uintmax_t) *((uint64_t *)dst.d_buf + i));
19933fe401a5SEd Maste 			}
1994b6b6f9ccSEd Maste 			PRT("%-18s ", elftc_reloc_type_str(ed->ehdr.e_machine,
19953fe401a5SEd Maste 				GELF_R_TYPE(got[i].u_r.rel.r_info)));
19963fe401a5SEd Maste 			if (ed->ec == ELFCLASS32)
19973fe401a5SEd Maste 				PRT("%-8.8jd ",
19983fe401a5SEd Maste 				    (intmax_t)got[i].u_r.rela.r_addend);
19993fe401a5SEd Maste 			else
20003fe401a5SEd Maste 				PRT("%-12.12jd ",
20013fe401a5SEd Maste 				    (intmax_t)got[i].u_r.rela.r_addend);
20023fe401a5SEd Maste 			if (got[i].symn == NULL)
20033fe401a5SEd Maste 				got[i].symn = "";
20043fe401a5SEd Maste 			PRT("%s\n", got[i].symn);
20053fe401a5SEd Maste 		}
20063fe401a5SEd Maste 		free(got);
20073fe401a5SEd Maste 	} else {
20083fe401a5SEd Maste 		for(i = 0; i < len; i++) {
20093fe401a5SEd Maste 			PRT("\nentry: %d\n", i);
20103fe401a5SEd Maste 			if (ed->ec == ELFCLASS32)
20113fe401a5SEd Maste 				PRT("\t%#x\n", *((uint32_t *)dst.d_buf + i));
20123fe401a5SEd Maste 			else
2013839529caSEd Maste 				PRT("\t%#jx\n",
2014839529caSEd Maste 				    (uintmax_t) *((uint64_t *)dst.d_buf + i));
20153fe401a5SEd Maste 		}
20163fe401a5SEd Maste 	}
20173fe401a5SEd Maste }
20183fe401a5SEd Maste 
20193fe401a5SEd Maste /*
20203fe401a5SEd Maste  * Dump the content of Global Offset Table section.
20213fe401a5SEd Maste  */
20223fe401a5SEd Maste static void
20233fe401a5SEd Maste elf_print_got(struct elfdump *ed)
20243fe401a5SEd Maste {
20253fe401a5SEd Maste 	struct section	*s;
2026b6b6f9ccSEd Maste 	size_t		 i;
20273fe401a5SEd Maste 
20283fe401a5SEd Maste 	if (!STAILQ_EMPTY(&ed->snl))
20293fe401a5SEd Maste 		return;
20303fe401a5SEd Maste 
20313fe401a5SEd Maste 	s = NULL;
2032b6b6f9ccSEd Maste 	for (i = 0; i < ed->shnum; i++) {
20333fe401a5SEd Maste 		s = &ed->sl[i];
20343fe401a5SEd Maste 		if (s->name && !strncmp(s->name, ".got", 4) &&
20353fe401a5SEd Maste 		    (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name)))
20363fe401a5SEd Maste 			elf_print_got_section(ed, s);
20373fe401a5SEd Maste 	}
20383fe401a5SEd Maste }
20393fe401a5SEd Maste 
20403fe401a5SEd Maste /*
20413fe401a5SEd Maste  * Dump the content of .note.ABI-tag section.
20423fe401a5SEd Maste  */
20433fe401a5SEd Maste static void
20443fe401a5SEd Maste elf_print_note(struct elfdump *ed)
20453fe401a5SEd Maste {
20463fe401a5SEd Maste 	struct section	*s;
20473fe401a5SEd Maste 	Elf_Data        *data;
20483fe401a5SEd Maste 	Elf_Note	*en;
20493fe401a5SEd Maste 	uint32_t	 namesz;
20503fe401a5SEd Maste 	uint32_t	 descsz;
20513fe401a5SEd Maste 	uint32_t	 desc;
20523fe401a5SEd Maste 	size_t		 count;
20533fe401a5SEd Maste 	int		 elferr, i;
2054b6b6f9ccSEd Maste 	uint8_t		*src;
2055*715d1396SEd Maste 	char		 idx[17];
20563fe401a5SEd Maste 
20573fe401a5SEd Maste 	s = NULL;
20583fe401a5SEd Maste 	for (i = 0; (size_t)i < ed->shnum; i++) {
20593fe401a5SEd Maste 		s = &ed->sl[i];
20603fe401a5SEd Maste 		if (s->type == SHT_NOTE && s->name &&
20613fe401a5SEd Maste 		    !strcmp(s->name, ".note.ABI-tag") &&
20623fe401a5SEd Maste 		    (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name)))
20633fe401a5SEd Maste 			break;
20643fe401a5SEd Maste 	}
20653fe401a5SEd Maste 	if ((size_t)i >= ed->shnum)
20663fe401a5SEd Maste 		return;
20673fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT)
20683fe401a5SEd Maste 		PRT("\nNote Section:  %s\n", s->name);
20693fe401a5SEd Maste 	else
20703fe401a5SEd Maste 		PRT("\nnote (%s):\n", s->name);
20713fe401a5SEd Maste 	(void) elf_errno();
20723fe401a5SEd Maste 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
20733fe401a5SEd Maste 		elferr = elf_errno();
20743fe401a5SEd Maste 		if (elferr != 0)
20753fe401a5SEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
20763fe401a5SEd Maste 		return;
20773fe401a5SEd Maste 	}
20783fe401a5SEd Maste 	src = data->d_buf;
20793fe401a5SEd Maste 	count = data->d_size;
20803fe401a5SEd Maste 	while (count > sizeof(Elf_Note)) {
20813fe401a5SEd Maste 		en = (Elf_Note *) (uintptr_t) src;
20823fe401a5SEd Maste 		namesz = en->n_namesz;
20833fe401a5SEd Maste 		descsz = en->n_descsz;
20843fe401a5SEd Maste 		src += sizeof(Elf_Note);
20853fe401a5SEd Maste 		count -= sizeof(Elf_Note);
2086b6b6f9ccSEd Maste 		if (roundup2(namesz, 4) + roundup2(descsz, 4) > count) {
2087b6b6f9ccSEd Maste 			warnx("truncated note section");
2088b6b6f9ccSEd Maste 			return;
2089b6b6f9ccSEd Maste 		}
20903fe401a5SEd Maste 		if (ed->flags & SOLARIS_FMT) {
20913fe401a5SEd Maste 			PRT("\n    type   %#x\n", en->n_type);
20923fe401a5SEd Maste 			PRT("    namesz %#x:\n", en->n_namesz);
20933fe401a5SEd Maste 			PRT("%s\n", src);
20943fe401a5SEd Maste 		} else
20953fe401a5SEd Maste 			PRT("\t%s ", src);
20963fe401a5SEd Maste 		src += roundup2(namesz, 4);
20973fe401a5SEd Maste 		count -= roundup2(namesz, 4);
20983fe401a5SEd Maste 
20993fe401a5SEd Maste 		/*
21003fe401a5SEd Maste 		 * Note that we dump the whole desc part if we're in
21013fe401a5SEd Maste 		 * "Solaris mode", while in the normal mode, we only look
21023fe401a5SEd Maste 		 * at the first 4 bytes (a 32bit word) of the desc, i.e,
21033fe401a5SEd Maste 		 * we assume that it's always a FreeBSD version number.
21043fe401a5SEd Maste 		 */
21053fe401a5SEd Maste 		if (ed->flags & SOLARIS_FMT) {
21063fe401a5SEd Maste 			PRT("    descsz %#x:", en->n_descsz);
21073fe401a5SEd Maste 			for (i = 0; (uint32_t)i < descsz; i++) {
21083fe401a5SEd Maste 				if ((i & 0xF) == 0) {
21093fe401a5SEd Maste 					snprintf(idx, sizeof(idx), "desc[%d]",
21103fe401a5SEd Maste 					    i);
21113fe401a5SEd Maste 					PRT("\n      %-9s", idx);
21123fe401a5SEd Maste 				} else if ((i & 0x3) == 0)
21133fe401a5SEd Maste 					PRT("  ");
21143fe401a5SEd Maste 				PRT(" %2.2x", src[i]);
21153fe401a5SEd Maste 			}
21163fe401a5SEd Maste 			PRT("\n");
21173fe401a5SEd Maste 		} else {
21183fe401a5SEd Maste 			if (ed->ehdr.e_ident[EI_DATA] == ELFDATA2MSB)
21193fe401a5SEd Maste 				desc = be32dec(src);
21203fe401a5SEd Maste 			else
21213fe401a5SEd Maste 				desc = le32dec(src);
21223fe401a5SEd Maste 			PRT("%d\n", desc);
21233fe401a5SEd Maste 		}
21243fe401a5SEd Maste 		src += roundup2(descsz, 4);
21253fe401a5SEd Maste 		count -= roundup2(descsz, 4);
21263fe401a5SEd Maste 	}
21273fe401a5SEd Maste }
21283fe401a5SEd Maste 
21293fe401a5SEd Maste /*
21303fe401a5SEd Maste  * Dump a hash table.
21313fe401a5SEd Maste  */
21323fe401a5SEd Maste static void
21333fe401a5SEd Maste elf_print_svr4_hash(struct elfdump *ed, struct section *s)
21343fe401a5SEd Maste {
21353fe401a5SEd Maste 	Elf_Data	*data;
21363fe401a5SEd Maste 	uint32_t	*buf;
21373fe401a5SEd Maste 	uint32_t	*bucket, *chain;
21383fe401a5SEd Maste 	uint32_t	 nbucket, nchain;
21393fe401a5SEd Maste 	uint32_t	*bl, *c, maxl, total;
2140b6b6f9ccSEd Maste 	uint32_t	 i, j;
2141b6b6f9ccSEd Maste 	int		 first, elferr;
21423fe401a5SEd Maste 	char		 idx[10];
21433fe401a5SEd Maste 
21443fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT)
21453fe401a5SEd Maste 		PRT("\nHash Section:  %s\n", s->name);
21463fe401a5SEd Maste 	else
21473fe401a5SEd Maste 		PRT("\nhash table (%s):\n", s->name);
21483fe401a5SEd Maste 	(void) elf_errno();
21493fe401a5SEd Maste 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
21503fe401a5SEd Maste 		elferr = elf_errno();
21513fe401a5SEd Maste 		if (elferr != 0)
21523fe401a5SEd Maste 			warnx("elf_getdata failed: %s",
21533fe401a5SEd Maste 			    elf_errmsg(elferr));
21543fe401a5SEd Maste 		return;
21553fe401a5SEd Maste 	}
21563fe401a5SEd Maste 	if (data->d_size < 2 * sizeof(uint32_t)) {
21573fe401a5SEd Maste 		warnx(".hash section too small");
21583fe401a5SEd Maste 		return;
21593fe401a5SEd Maste 	}
21603fe401a5SEd Maste 	buf = data->d_buf;
21613fe401a5SEd Maste 	nbucket = buf[0];
21623fe401a5SEd Maste 	nchain = buf[1];
21633fe401a5SEd Maste 	if (nbucket <= 0 || nchain <= 0) {
21643fe401a5SEd Maste 		warnx("Malformed .hash section");
21653fe401a5SEd Maste 		return;
21663fe401a5SEd Maste 	}
2167b6b6f9ccSEd Maste 	if (data->d_size !=
2168b6b6f9ccSEd Maste 	    ((uint64_t)nbucket + (uint64_t)nchain + 2) * sizeof(uint32_t)) {
21693fe401a5SEd Maste 		warnx("Malformed .hash section");
21703fe401a5SEd Maste 		return;
21713fe401a5SEd Maste 	}
21723fe401a5SEd Maste 	bucket = &buf[2];
21733fe401a5SEd Maste 	chain = &buf[2 + nbucket];
21743fe401a5SEd Maste 
21753fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT) {
21763fe401a5SEd Maste 		maxl = 0;
21773fe401a5SEd Maste 		if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
21783fe401a5SEd Maste 			err(EXIT_FAILURE, "calloc failed");
2179b6b6f9ccSEd Maste 		for (i = 0; i < nbucket; i++)
2180b6b6f9ccSEd Maste 			for (j = bucket[i]; j > 0 && j < nchain; j = chain[j])
21813fe401a5SEd Maste 				if (++bl[i] > maxl)
21823fe401a5SEd Maste 					maxl = bl[i];
21833fe401a5SEd Maste 		if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
21843fe401a5SEd Maste 			err(EXIT_FAILURE, "calloc failed");
2185b6b6f9ccSEd Maste 		for (i = 0; i < nbucket; i++)
21863fe401a5SEd Maste 			c[bl[i]]++;
21873fe401a5SEd Maste 		PRT("    bucket    symndx    name\n");
2188b6b6f9ccSEd Maste 		for (i = 0; i < nbucket; i++) {
21893fe401a5SEd Maste 			first = 1;
2190b6b6f9ccSEd Maste 			for (j = bucket[i]; j > 0 && j < nchain; j = chain[j]) {
21913fe401a5SEd Maste 				if (first) {
21923fe401a5SEd Maste 					PRT("%10d  ", i);
21933fe401a5SEd Maste 					first = 0;
21943fe401a5SEd Maste 				} else
21953fe401a5SEd Maste 					PRT("            ");
21963fe401a5SEd Maste 				snprintf(idx, sizeof(idx), "[%d]", j);
21973fe401a5SEd Maste 				PRT("%-10s  ", idx);
21983fe401a5SEd Maste 				PRT("%s\n", get_symbol_name(ed, s->link, j));
21993fe401a5SEd Maste 			}
22003fe401a5SEd Maste 		}
22013fe401a5SEd Maste 		PRT("\n");
22023fe401a5SEd Maste 		total = 0;
2203b6b6f9ccSEd Maste 		for (i = 0; i <= maxl; i++) {
22043fe401a5SEd Maste 			total += c[i] * i;
22053fe401a5SEd Maste 			PRT("%10u  buckets contain %8d symbols\n", c[i], i);
22063fe401a5SEd Maste 		}
22073fe401a5SEd Maste 		PRT("%10u  buckets         %8u symbols (globals)\n", nbucket,
22083fe401a5SEd Maste 		    total);
22093fe401a5SEd Maste 	} else {
22103fe401a5SEd Maste 		PRT("\nnbucket: %u\n", nbucket);
22113fe401a5SEd Maste 		PRT("nchain: %u\n\n", nchain);
2212b6b6f9ccSEd Maste 		for (i = 0; i < nbucket; i++)
22133fe401a5SEd Maste 			PRT("bucket[%d]:\n\t%u\n\n", i, bucket[i]);
2214b6b6f9ccSEd Maste 		for (i = 0; i < nchain; i++)
22153fe401a5SEd Maste 			PRT("chain[%d]:\n\t%u\n\n", i, chain[i]);
22163fe401a5SEd Maste 	}
22173fe401a5SEd Maste }
22183fe401a5SEd Maste 
22193fe401a5SEd Maste /*
22203fe401a5SEd Maste  * Dump a 64bit hash table.
22213fe401a5SEd Maste  */
22223fe401a5SEd Maste static void
22233fe401a5SEd Maste elf_print_svr4_hash64(struct elfdump *ed, struct section *s)
22243fe401a5SEd Maste {
22253fe401a5SEd Maste 	Elf_Data	*data, dst;
22263fe401a5SEd Maste 	uint64_t	*buf;
22273fe401a5SEd Maste 	uint64_t	*bucket, *chain;
22283fe401a5SEd Maste 	uint64_t	 nbucket, nchain;
222918f4c9dbSEd Maste 	uint64_t	*bl, *c, j, maxl, total;
223018f4c9dbSEd Maste 	size_t		 i;
2231b6b6f9ccSEd Maste 	int		 elferr, first;
22323fe401a5SEd Maste 	char		 idx[10];
22333fe401a5SEd Maste 
22343fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT)
22353fe401a5SEd Maste 		PRT("\nHash Section:  %s\n", s->name);
22363fe401a5SEd Maste 	else
22373fe401a5SEd Maste 		PRT("\nhash table (%s):\n", s->name);
22383fe401a5SEd Maste 
22393fe401a5SEd Maste 	/*
22403fe401a5SEd Maste 	 * ALPHA uses 64-bit hash entries. Since libelf assumes that
22413fe401a5SEd Maste 	 * .hash section contains only 32-bit entry, an explicit
22423fe401a5SEd Maste 	 * gelf_xlatetom is needed here.
22433fe401a5SEd Maste 	 */
22443fe401a5SEd Maste 	(void) elf_errno();
22453fe401a5SEd Maste 	if ((data = elf_rawdata(s->scn, NULL)) == NULL) {
22463fe401a5SEd Maste 		elferr = elf_errno();
22473fe401a5SEd Maste 		if (elferr != 0)
22483fe401a5SEd Maste 			warnx("elf_rawdata failed: %s",
22493fe401a5SEd Maste 			    elf_errmsg(elferr));
22503fe401a5SEd Maste 		return;
22513fe401a5SEd Maste 	}
22523fe401a5SEd Maste 	data->d_type = ELF_T_XWORD;
22533fe401a5SEd Maste 	memcpy(&dst, data, sizeof(Elf_Data));
22543fe401a5SEd Maste 	if (gelf_xlatetom(ed->elf, &dst, data,
22553fe401a5SEd Maste 		ed->ehdr.e_ident[EI_DATA]) != &dst) {
22563fe401a5SEd Maste 		warnx("gelf_xlatetom failed: %s", elf_errmsg(-1));
22573fe401a5SEd Maste 		return;
22583fe401a5SEd Maste 	}
22593fe401a5SEd Maste 	if (dst.d_size < 2 * sizeof(uint64_t)) {
22603fe401a5SEd Maste 		warnx(".hash section too small");
22613fe401a5SEd Maste 		return;
22623fe401a5SEd Maste 	}
22633fe401a5SEd Maste 	buf = dst.d_buf;
22643fe401a5SEd Maste 	nbucket = buf[0];
22653fe401a5SEd Maste 	nchain = buf[1];
22663fe401a5SEd Maste 	if (nbucket <= 0 || nchain <= 0) {
22673fe401a5SEd Maste 		warnx("Malformed .hash section");
22683fe401a5SEd Maste 		return;
22693fe401a5SEd Maste 	}
22703fe401a5SEd Maste 	if (dst.d_size != (nbucket + nchain + 2) * sizeof(uint64_t)) {
22713fe401a5SEd Maste 		warnx("Malformed .hash section");
22723fe401a5SEd Maste 		return;
22733fe401a5SEd Maste 	}
22743fe401a5SEd Maste 	bucket = &buf[2];
22753fe401a5SEd Maste 	chain = &buf[2 + nbucket];
22763fe401a5SEd Maste 
22773fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT) {
22783fe401a5SEd Maste 		maxl = 0;
22793fe401a5SEd Maste 		if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
22803fe401a5SEd Maste 			err(EXIT_FAILURE, "calloc failed");
2281b6b6f9ccSEd Maste 		for (i = 0; i < nbucket; i++)
2282b6b6f9ccSEd Maste 			for (j = bucket[i]; j > 0 && j < nchain; j = chain[j])
22833fe401a5SEd Maste 				if (++bl[i] > maxl)
22843fe401a5SEd Maste 					maxl = bl[i];
22853fe401a5SEd Maste 		if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
22863fe401a5SEd Maste 			err(EXIT_FAILURE, "calloc failed");
2287b6b6f9ccSEd Maste 		for (i = 0; i < nbucket; i++)
22883fe401a5SEd Maste 			c[bl[i]]++;
22893fe401a5SEd Maste 		PRT("    bucket    symndx    name\n");
2290b6b6f9ccSEd Maste 		for (i = 0; i < nbucket; i++) {
22913fe401a5SEd Maste 			first = 1;
2292b6b6f9ccSEd Maste 			for (j = bucket[i]; j > 0 && j < nchain; j = chain[j]) {
22933fe401a5SEd Maste 				if (first) {
2294b6b6f9ccSEd Maste 					PRT("%10zu  ", i);
22953fe401a5SEd Maste 					first = 0;
22963fe401a5SEd Maste 				} else
22973fe401a5SEd Maste 					PRT("            ");
2298b6b6f9ccSEd Maste 				snprintf(idx, sizeof(idx), "[%zu]", (size_t)j);
22993fe401a5SEd Maste 				PRT("%-10s  ", idx);
23003fe401a5SEd Maste 				PRT("%s\n", get_symbol_name(ed, s->link, j));
23013fe401a5SEd Maste 			}
23023fe401a5SEd Maste 		}
23033fe401a5SEd Maste 		PRT("\n");
23043fe401a5SEd Maste 		total = 0;
2305b6b6f9ccSEd Maste 		for (i = 0; i <= maxl; i++) {
23063fe401a5SEd Maste 			total += c[i] * i;
2307b6b6f9ccSEd Maste 			PRT("%10ju  buckets contain %8zu symbols\n",
23083fe401a5SEd Maste 			    (uintmax_t)c[i], i);
23093fe401a5SEd Maste 		}
23103fe401a5SEd Maste 		PRT("%10ju  buckets         %8ju symbols (globals)\n",
23113fe401a5SEd Maste 		    (uintmax_t)nbucket, (uintmax_t)total);
23123fe401a5SEd Maste 	} else {
23133fe401a5SEd Maste 		PRT("\nnbucket: %ju\n", (uintmax_t)nbucket);
23143fe401a5SEd Maste 		PRT("nchain: %ju\n\n", (uintmax_t)nchain);
2315b6b6f9ccSEd Maste 		for (i = 0; i < nbucket; i++)
2316b6b6f9ccSEd Maste 			PRT("bucket[%zu]:\n\t%ju\n\n", i, (uintmax_t)bucket[i]);
2317b6b6f9ccSEd Maste 		for (i = 0; i < nchain; i++)
2318b6b6f9ccSEd Maste 			PRT("chain[%zu]:\n\t%ju\n\n", i, (uintmax_t)chain[i]);
23193fe401a5SEd Maste 	}
23203fe401a5SEd Maste 
23213fe401a5SEd Maste }
23223fe401a5SEd Maste 
23233fe401a5SEd Maste /*
23243fe401a5SEd Maste  * Dump a GNU hash table.
23253fe401a5SEd Maste  */
23263fe401a5SEd Maste static void
23273fe401a5SEd Maste elf_print_gnu_hash(struct elfdump *ed, struct section *s)
23283fe401a5SEd Maste {
23293fe401a5SEd Maste 	struct section	*ds;
23303fe401a5SEd Maste 	Elf_Data	*data;
23313fe401a5SEd Maste 	uint32_t	*buf;
23323fe401a5SEd Maste 	uint32_t	*bucket, *chain;
23333fe401a5SEd Maste 	uint32_t	 nbucket, nchain, symndx, maskwords, shift2;
23343fe401a5SEd Maste 	uint32_t	*bl, *c, maxl, total;
2335b6b6f9ccSEd Maste 	uint32_t	 i, j;
2336b6b6f9ccSEd Maste 	int		 first, elferr, dynsymcount;
23373fe401a5SEd Maste 	char		 idx[10];
23383fe401a5SEd Maste 
23393fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT)
23403fe401a5SEd Maste 		PRT("\nGNU Hash Section:  %s\n", s->name);
23413fe401a5SEd Maste 	else
23423fe401a5SEd Maste 		PRT("\ngnu hash table (%s):\n", s->name);
23433fe401a5SEd Maste 	(void) elf_errno();
23443fe401a5SEd Maste 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
23453fe401a5SEd Maste 		elferr = elf_errno();
23463fe401a5SEd Maste 		if (elferr != 0)
23473fe401a5SEd Maste 			warnx("elf_getdata failed: %s",
23483fe401a5SEd Maste 			    elf_errmsg(elferr));
23493fe401a5SEd Maste 		return;
23503fe401a5SEd Maste 	}
23513fe401a5SEd Maste 	if (data->d_size < 4 * sizeof(uint32_t)) {
23523fe401a5SEd Maste 		warnx(".gnu.hash section too small");
23533fe401a5SEd Maste 		return;
23543fe401a5SEd Maste 	}
23553fe401a5SEd Maste 	buf = data->d_buf;
23563fe401a5SEd Maste 	nbucket = buf[0];
23573fe401a5SEd Maste 	symndx = buf[1];
23583fe401a5SEd Maste 	maskwords = buf[2];
23593fe401a5SEd Maste 	shift2 = buf[3];
23603fe401a5SEd Maste 	buf += 4;
2361b6b6f9ccSEd Maste 	if (s->link >= ed->shnum) {
2362b6b6f9ccSEd Maste 		warnx("Malformed .gnu.hash section");
2363b6b6f9ccSEd Maste 		return;
2364b6b6f9ccSEd Maste 	}
23653fe401a5SEd Maste 	ds = &ed->sl[s->link];
2366656f49f8SEd Maste 	if (!get_ent_count(ds, &dynsymcount))
2367656f49f8SEd Maste 		return;
2368b6b6f9ccSEd Maste 	if (symndx >= (uint32_t)dynsymcount) {
2369b6b6f9ccSEd Maste 		warnx("Malformed .gnu.hash section");
2370b6b6f9ccSEd Maste 		return;
2371b6b6f9ccSEd Maste 	}
23723fe401a5SEd Maste 	nchain = dynsymcount - symndx;
23733fe401a5SEd Maste 	if (data->d_size != 4 * sizeof(uint32_t) + maskwords *
23743fe401a5SEd Maste 	    (ed->ec == ELFCLASS32 ? sizeof(uint32_t) : sizeof(uint64_t)) +
2375b6b6f9ccSEd Maste 	    ((uint64_t)nbucket + (uint64_t)nchain) * sizeof(uint32_t)) {
23763fe401a5SEd Maste 		warnx("Malformed .gnu.hash section");
23773fe401a5SEd Maste 		return;
23783fe401a5SEd Maste 	}
23793fe401a5SEd Maste 	bucket = buf + (ed->ec == ELFCLASS32 ? maskwords : maskwords * 2);
23803fe401a5SEd Maste 	chain = bucket + nbucket;
23813fe401a5SEd Maste 
23823fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT) {
23833fe401a5SEd Maste 		maxl = 0;
23843fe401a5SEd Maste 		if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
23853fe401a5SEd Maste 			err(EXIT_FAILURE, "calloc failed");
2386b6b6f9ccSEd Maste 		for (i = 0; i < nbucket; i++)
2387b6b6f9ccSEd Maste 			for (j = bucket[i]; j > 0 && j - symndx < nchain; j++) {
23883fe401a5SEd Maste 				if (++bl[i] > maxl)
23893fe401a5SEd Maste 					maxl = bl[i];
23903fe401a5SEd Maste 				if (chain[j - symndx] & 1)
23913fe401a5SEd Maste 					break;
23923fe401a5SEd Maste 			}
23933fe401a5SEd Maste 		if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
23943fe401a5SEd Maste 			err(EXIT_FAILURE, "calloc failed");
2395b6b6f9ccSEd Maste 		for (i = 0; i < nbucket; i++)
23963fe401a5SEd Maste 			c[bl[i]]++;
23973fe401a5SEd Maste 		PRT("    bucket    symndx    name\n");
2398b6b6f9ccSEd Maste 		for (i = 0; i < nbucket; i++) {
23993fe401a5SEd Maste 			first = 1;
2400b6b6f9ccSEd Maste 			for (j = bucket[i]; j > 0 && j - symndx < nchain; j++) {
24013fe401a5SEd Maste 				if (first) {
24023fe401a5SEd Maste 					PRT("%10d  ", i);
24033fe401a5SEd Maste 					first = 0;
24043fe401a5SEd Maste 				} else
24053fe401a5SEd Maste 					PRT("            ");
24063fe401a5SEd Maste 				snprintf(idx, sizeof(idx), "[%d]", j );
24073fe401a5SEd Maste 				PRT("%-10s  ", idx);
24083fe401a5SEd Maste 				PRT("%s\n", get_symbol_name(ed, s->link, j));
24093fe401a5SEd Maste 				if (chain[j - symndx] & 1)
24103fe401a5SEd Maste 					break;
24113fe401a5SEd Maste 			}
24123fe401a5SEd Maste 		}
24133fe401a5SEd Maste 		PRT("\n");
24143fe401a5SEd Maste 		total = 0;
2415b6b6f9ccSEd Maste 		for (i = 0; i <= maxl; i++) {
24163fe401a5SEd Maste 			total += c[i] * i;
24173fe401a5SEd Maste 			PRT("%10u  buckets contain %8d symbols\n", c[i], i);
24183fe401a5SEd Maste 		}
24193fe401a5SEd Maste 		PRT("%10u  buckets         %8u symbols (globals)\n", nbucket,
24203fe401a5SEd Maste 		    total);
24213fe401a5SEd Maste 	} else {
24223fe401a5SEd Maste 		PRT("\nnbucket: %u\n", nbucket);
24233fe401a5SEd Maste 		PRT("symndx: %u\n", symndx);
24243fe401a5SEd Maste 		PRT("maskwords: %u\n", maskwords);
24253fe401a5SEd Maste 		PRT("shift2: %u\n", shift2);
24263fe401a5SEd Maste 		PRT("nchain: %u\n\n", nchain);
2427b6b6f9ccSEd Maste 		for (i = 0; i < nbucket; i++)
24283fe401a5SEd Maste 			PRT("bucket[%d]:\n\t%u\n\n", i, bucket[i]);
2429b6b6f9ccSEd Maste 		for (i = 0; i < nchain; i++)
24303fe401a5SEd Maste 			PRT("chain[%d]:\n\t%u\n\n", i, chain[i]);
24313fe401a5SEd Maste 	}
24323fe401a5SEd Maste }
24333fe401a5SEd Maste 
24343fe401a5SEd Maste /*
24353fe401a5SEd Maste  * Dump hash tables.
24363fe401a5SEd Maste  */
24373fe401a5SEd Maste static void
24383fe401a5SEd Maste elf_print_hash(struct elfdump *ed)
24393fe401a5SEd Maste {
24403fe401a5SEd Maste 	struct section	*s;
2441b6b6f9ccSEd Maste 	size_t		 i;
24423fe401a5SEd Maste 
2443b6b6f9ccSEd Maste 	for (i = 0; i < ed->shnum; i++) {
24443fe401a5SEd Maste 		s = &ed->sl[i];
24453fe401a5SEd Maste 		if ((s->type == SHT_HASH || s->type == SHT_GNU_HASH) &&
24463fe401a5SEd Maste 		    (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name))) {
24473fe401a5SEd Maste 			if (s->type == SHT_GNU_HASH)
24483fe401a5SEd Maste 				elf_print_gnu_hash(ed, s);
24493fe401a5SEd Maste 			else if (ed->ehdr.e_machine == EM_ALPHA &&
24503fe401a5SEd Maste 			    s->entsize == 8)
24513fe401a5SEd Maste 				elf_print_svr4_hash64(ed, s);
24523fe401a5SEd Maste 			else
24533fe401a5SEd Maste 				elf_print_svr4_hash(ed, s);
24543fe401a5SEd Maste 		}
24553fe401a5SEd Maste 	}
24563fe401a5SEd Maste }
24573fe401a5SEd Maste 
24583fe401a5SEd Maste /*
24593fe401a5SEd Maste  * Dump the content of a Version Definition(SHT_SUNW_Verdef) Section.
24603fe401a5SEd Maste  */
24613fe401a5SEd Maste static void
24623fe401a5SEd Maste elf_print_verdef(struct elfdump *ed, struct section *s)
24633fe401a5SEd Maste {
24643fe401a5SEd Maste 	Elf_Data	*data;
24653fe401a5SEd Maste 	Elf32_Verdef	*vd;
24663fe401a5SEd Maste 	Elf32_Verdaux	*vda;
24673fe401a5SEd Maste 	const char 	*str;
24683fe401a5SEd Maste 	char		 idx[10];
24693fe401a5SEd Maste 	uint8_t		*buf, *end, *buf2;
24703fe401a5SEd Maste 	int		 i, j, elferr, count;
24713fe401a5SEd Maste 
24723fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT)
24733fe401a5SEd Maste 		PRT("Version Definition Section:  %s\n", s->name);
24743fe401a5SEd Maste 	else
24753fe401a5SEd Maste 		PRT("\nversion definition section (%s):\n", s->name);
24763fe401a5SEd Maste 	(void) elf_errno();
24773fe401a5SEd Maste 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
24783fe401a5SEd Maste 		elferr = elf_errno();
24793fe401a5SEd Maste 		if (elferr != 0)
24803fe401a5SEd Maste 			warnx("elf_getdata failed: %s",
24813fe401a5SEd Maste 			    elf_errmsg(elferr));
24823fe401a5SEd Maste 		return;
24833fe401a5SEd Maste 	}
24843fe401a5SEd Maste 	buf = data->d_buf;
24853fe401a5SEd Maste 	end = buf + data->d_size;
24863fe401a5SEd Maste 	i = 0;
24873fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT)
24883fe401a5SEd Maste 		PRT("     index  version                     dependency\n");
24893fe401a5SEd Maste 	while (buf + sizeof(Elf32_Verdef) <= end) {
24903fe401a5SEd Maste 		vd = (Elf32_Verdef *) (uintptr_t) buf;
24913fe401a5SEd Maste 		if (ed->flags & SOLARIS_FMT) {
24923fe401a5SEd Maste 			snprintf(idx, sizeof(idx), "[%d]", vd->vd_ndx);
24933fe401a5SEd Maste 			PRT("%10s  ", idx);
24943fe401a5SEd Maste 		} else {
24953fe401a5SEd Maste 			PRT("\nentry: %d\n", i++);
24963fe401a5SEd Maste 			PRT("\tvd_version: %u\n", vd->vd_version);
24973fe401a5SEd Maste 			PRT("\tvd_flags: %u\n", vd->vd_flags);
24983fe401a5SEd Maste 			PRT("\tvd_ndx: %u\n", vd->vd_ndx);
24993fe401a5SEd Maste 			PRT("\tvd_cnt: %u\n", vd->vd_cnt);
25003fe401a5SEd Maste 			PRT("\tvd_hash: %u\n", vd->vd_hash);
25013fe401a5SEd Maste 			PRT("\tvd_aux: %u\n", vd->vd_aux);
25023fe401a5SEd Maste 			PRT("\tvd_next: %u\n\n", vd->vd_next);
25033fe401a5SEd Maste 		}
25043fe401a5SEd Maste 		buf2 = buf + vd->vd_aux;
25053fe401a5SEd Maste 		j = 0;
25063fe401a5SEd Maste 		count = 0;
25073fe401a5SEd Maste 		while (buf2 + sizeof(Elf32_Verdaux) <= end && j < vd->vd_cnt) {
25083fe401a5SEd Maste 			vda = (Elf32_Verdaux *) (uintptr_t) buf2;
25093fe401a5SEd Maste 			str = get_string(ed, s->link, vda->vda_name);
25103fe401a5SEd Maste 			if (ed->flags & SOLARIS_FMT) {
25113fe401a5SEd Maste 				if (count == 0)
25123fe401a5SEd Maste 					PRT("%-26.26s", str);
25133fe401a5SEd Maste 				else if (count == 1)
25143fe401a5SEd Maste 					PRT("  %-20.20s", str);
25153fe401a5SEd Maste 				else {
25163fe401a5SEd Maste 					PRT("\n%40.40s", "");
25173fe401a5SEd Maste 					PRT("%s", str);
25183fe401a5SEd Maste 				}
25193fe401a5SEd Maste 			} else {
25203fe401a5SEd Maste 				PRT("\t\tvda: %d\n", j++);
25213fe401a5SEd Maste 				PRT("\t\t\tvda_name: %s\n", str);
25223fe401a5SEd Maste 				PRT("\t\t\tvda_next: %u\n", vda->vda_next);
25233fe401a5SEd Maste 			}
25243fe401a5SEd Maste 			if (vda->vda_next == 0) {
25253fe401a5SEd Maste 				if (ed->flags & SOLARIS_FMT) {
25263fe401a5SEd Maste 					if (vd->vd_flags & VER_FLG_BASE) {
25273fe401a5SEd Maste 						if (count == 0)
25283fe401a5SEd Maste 							PRT("%-20.20s", "");
25293fe401a5SEd Maste 						PRT("%s", "[ BASE ]");
25303fe401a5SEd Maste 					}
25313fe401a5SEd Maste 					PRT("\n");
25323fe401a5SEd Maste 				}
25333fe401a5SEd Maste 				break;
25343fe401a5SEd Maste 			}
25353fe401a5SEd Maste 			if (ed->flags & SOLARIS_FMT)
25363fe401a5SEd Maste 				count++;
25373fe401a5SEd Maste 			buf2 += vda->vda_next;
25383fe401a5SEd Maste 		}
25393fe401a5SEd Maste 		if (vd->vd_next == 0)
25403fe401a5SEd Maste 			break;
25413fe401a5SEd Maste 		buf += vd->vd_next;
25423fe401a5SEd Maste 	}
25433fe401a5SEd Maste }
25443fe401a5SEd Maste 
25453fe401a5SEd Maste /*
25463fe401a5SEd Maste  * Dump the content of a Version Needed(SHT_SUNW_Verneed) Section.
25473fe401a5SEd Maste  */
25483fe401a5SEd Maste static void
25493fe401a5SEd Maste elf_print_verneed(struct elfdump *ed, struct section *s)
25503fe401a5SEd Maste {
25513fe401a5SEd Maste 	Elf_Data	*data;
25523fe401a5SEd Maste 	Elf32_Verneed	*vn;
25533fe401a5SEd Maste 	Elf32_Vernaux	*vna;
25543fe401a5SEd Maste 	uint8_t		*buf, *end, *buf2;
25553fe401a5SEd Maste 	int		 i, j, elferr, first;
25563fe401a5SEd Maste 
25573fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT)
25583fe401a5SEd Maste 		PRT("\nVersion Needed Section:  %s\n", s->name);
25593fe401a5SEd Maste 	else
25603fe401a5SEd Maste 		PRT("\nversion need section (%s):\n", s->name);
25613fe401a5SEd Maste 	(void) elf_errno();
25623fe401a5SEd Maste 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
25633fe401a5SEd Maste 		elferr = elf_errno();
25643fe401a5SEd Maste 		if (elferr != 0)
25653fe401a5SEd Maste 			warnx("elf_getdata failed: %s",
25663fe401a5SEd Maste 			    elf_errmsg(elferr));
25673fe401a5SEd Maste 		return;
25683fe401a5SEd Maste 	}
25693fe401a5SEd Maste 	buf = data->d_buf;
25703fe401a5SEd Maste 	end = buf + data->d_size;
25713fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT)
25723fe401a5SEd Maste 		PRT("            file                        version\n");
25733fe401a5SEd Maste 	i = 0;
25743fe401a5SEd Maste 	while (buf + sizeof(Elf32_Verneed) <= end) {
25753fe401a5SEd Maste 		vn = (Elf32_Verneed *) (uintptr_t) buf;
25763fe401a5SEd Maste 		if (ed->flags & SOLARIS_FMT)
25773fe401a5SEd Maste 			PRT("            %-26.26s  ",
25783fe401a5SEd Maste 			    get_string(ed, s->link, vn->vn_file));
25793fe401a5SEd Maste 		else {
25803fe401a5SEd Maste 			PRT("\nentry: %d\n", i++);
25813fe401a5SEd Maste 			PRT("\tvn_version: %u\n", vn->vn_version);
25823fe401a5SEd Maste 			PRT("\tvn_cnt: %u\n", vn->vn_cnt);
25833fe401a5SEd Maste 			PRT("\tvn_file: %s\n",
25843fe401a5SEd Maste 			    get_string(ed, s->link, vn->vn_file));
25853fe401a5SEd Maste 			PRT("\tvn_aux: %u\n", vn->vn_aux);
25863fe401a5SEd Maste 			PRT("\tvn_next: %u\n\n", vn->vn_next);
25873fe401a5SEd Maste 		}
25883fe401a5SEd Maste 		buf2 = buf + vn->vn_aux;
25893fe401a5SEd Maste 		j = 0;
25903fe401a5SEd Maste 		first = 1;
25913fe401a5SEd Maste 		while (buf2 + sizeof(Elf32_Vernaux) <= end && j < vn->vn_cnt) {
25923fe401a5SEd Maste 			vna = (Elf32_Vernaux *) (uintptr_t) buf2;
25933fe401a5SEd Maste 			if (ed->flags & SOLARIS_FMT) {
25943fe401a5SEd Maste 				if (!first)
25953fe401a5SEd Maste 					PRT("%40.40s", "");
25963fe401a5SEd Maste 				else
25973fe401a5SEd Maste 					first = 0;
25983fe401a5SEd Maste 				PRT("%s\n", get_string(ed, s->link,
25993fe401a5SEd Maste 				    vna->vna_name));
26003fe401a5SEd Maste 			} else {
26013fe401a5SEd Maste 				PRT("\t\tvna: %d\n", j++);
26023fe401a5SEd Maste 				PRT("\t\t\tvna_hash: %u\n", vna->vna_hash);
26033fe401a5SEd Maste 				PRT("\t\t\tvna_flags: %u\n", vna->vna_flags);
26043fe401a5SEd Maste 				PRT("\t\t\tvna_other: %u\n", vna->vna_other);
26053fe401a5SEd Maste 				PRT("\t\t\tvna_name: %s\n",
26063fe401a5SEd Maste 				    get_string(ed, s->link, vna->vna_name));
26073fe401a5SEd Maste 				PRT("\t\t\tvna_next: %u\n", vna->vna_next);
26083fe401a5SEd Maste 			}
26093fe401a5SEd Maste 			if (vna->vna_next == 0)
26103fe401a5SEd Maste 				break;
26113fe401a5SEd Maste 			buf2 += vna->vna_next;
26123fe401a5SEd Maste 		}
26133fe401a5SEd Maste 		if (vn->vn_next == 0)
26143fe401a5SEd Maste 			break;
26153fe401a5SEd Maste 		buf += vn->vn_next;
26163fe401a5SEd Maste 	}
26173fe401a5SEd Maste }
26183fe401a5SEd Maste 
26193fe401a5SEd Maste /*
26203fe401a5SEd Maste  * Dump the symbol-versioning sections.
26213fe401a5SEd Maste  */
26223fe401a5SEd Maste static void
26233fe401a5SEd Maste elf_print_symver(struct elfdump *ed)
26243fe401a5SEd Maste {
26253fe401a5SEd Maste 	struct section	*s;
2626b6b6f9ccSEd Maste 	size_t		 i;
26273fe401a5SEd Maste 
2628b6b6f9ccSEd Maste 	for (i = 0; i < ed->shnum; i++) {
26293fe401a5SEd Maste 		s = &ed->sl[i];
26303fe401a5SEd Maste 		if (!STAILQ_EMPTY(&ed->snl) && !find_name(ed, s->name))
26313fe401a5SEd Maste 			continue;
26323fe401a5SEd Maste 		if (s->type == SHT_SUNW_verdef)
26333fe401a5SEd Maste 			elf_print_verdef(ed, s);
26343fe401a5SEd Maste 		if (s->type == SHT_SUNW_verneed)
26353fe401a5SEd Maste 			elf_print_verneed(ed, s);
26363fe401a5SEd Maste 	}
26373fe401a5SEd Maste }
26383fe401a5SEd Maste 
26393fe401a5SEd Maste /*
26403fe401a5SEd Maste  * Dump the ELF checksum. See gelf_checksum(3) for details.
26413fe401a5SEd Maste  */
26423fe401a5SEd Maste static void
26433fe401a5SEd Maste elf_print_checksum(struct elfdump *ed)
26443fe401a5SEd Maste {
26453fe401a5SEd Maste 
26463fe401a5SEd Maste 	if (!STAILQ_EMPTY(&ed->snl))
26473fe401a5SEd Maste 		return;
26483fe401a5SEd Maste 
26493fe401a5SEd Maste 	PRT("\nelf checksum: %#lx\n", gelf_checksum(ed->elf));
26503fe401a5SEd Maste }
26513fe401a5SEd Maste 
26523fe401a5SEd Maste #define	USAGE_MESSAGE	"\
26533fe401a5SEd Maste Usage: %s [options] file...\n\
26543fe401a5SEd Maste   Display information about ELF objects and ar(1) archives.\n\n\
26553fe401a5SEd Maste   Options:\n\
26563fe401a5SEd Maste   -a                        Show all information.\n\
26573fe401a5SEd Maste   -c                        Show shared headers.\n\
26583fe401a5SEd Maste   -d                        Show dynamic symbols.\n\
26593fe401a5SEd Maste   -e                        Show the ELF header.\n\
26603fe401a5SEd Maste   -G                        Show the GOT.\n\
26613fe401a5SEd Maste   -H | --help               Show a usage message and exit.\n\
26623fe401a5SEd Maste   -h                        Show hash values.\n\
26633fe401a5SEd Maste   -i                        Show the dynamic interpreter.\n\
26643fe401a5SEd Maste   -k                        Show the ELF checksum.\n\
26653fe401a5SEd Maste   -n                        Show the contents of note sections.\n\
26663fe401a5SEd Maste   -N NAME                   Show the section named \"NAME\".\n\
26673fe401a5SEd Maste   -p                        Show the program header.\n\
26683fe401a5SEd Maste   -r                        Show relocations.\n\
26693fe401a5SEd Maste   -s                        Show the symbol table.\n\
26703fe401a5SEd Maste   -S                        Use the Solaris elfdump format.\n\
26713fe401a5SEd Maste   -v                        Show symbol-versioning information.\n\
26723fe401a5SEd Maste   -V | --version            Print a version identifier and exit.\n\
26733fe401a5SEd Maste   -w FILE                   Write output to \"FILE\".\n"
26743fe401a5SEd Maste 
26753fe401a5SEd Maste static void
26763fe401a5SEd Maste usage(void)
26773fe401a5SEd Maste {
26783fe401a5SEd Maste 	fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());
26793fe401a5SEd Maste 	exit(EXIT_FAILURE);
26803fe401a5SEd Maste }
2681