xref: /freebsd/contrib/elftoolchain/elfdump/elfdump.c (revision d003e0d7fe0d3a9b4b2c5835bb3f0f6faf3ab538)
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*d003e0d7SEd Maste ELFTC_VCSID("$Id: elfdump.c 3762 2019-06-28 21:06:24Z emaste $");
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
be32dec(const void * pp)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
le32dec(const void * pp)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 *
d_tags(uint64_t tag)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 *
e_machines(unsigned int mach)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 *
elf_type_str(unsigned int type)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 *
elf_version_str(unsigned int ver)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 *
elf_class_str(unsigned int class)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 *
elf_data_str(unsigned int data)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 *
elf_phdr_type_str(unsigned int type)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";
357ca307559SChristian S.J. Peron 	case PT_OPENBSD_RANDOMIZE:	return "PT_OPENBSD_RANDOMIZE";
358ca307559SChristian S.J. Peron 	case PT_OPENBSD_WXNEEDED:	return "PT_OPENBSD_WXNEEDED";
359ca307559SChristian S.J. Peron 	case PT_OPENBSD_BOOTDATA:	return "PT_OPENBSD_BOOTDATA";
360b6b6f9ccSEd Maste 	}
361b6b6f9ccSEd Maste 	snprintf(s_type, sizeof(s_type), "<unknown: %#x>", type);
362b6b6f9ccSEd Maste 	return (s_type);
363b6b6f9ccSEd Maste }
3643fe401a5SEd Maste 
3653fe401a5SEd Maste static const char *p_flags[] = {
3663fe401a5SEd Maste 	"", "PF_X", "PF_W", "PF_X|PF_W", "PF_R", "PF_X|PF_R", "PF_W|PF_R",
3673fe401a5SEd Maste 	"PF_X|PF_W|PF_R"
3683fe401a5SEd Maste };
3693fe401a5SEd Maste 
3703fe401a5SEd Maste static const char *
sh_name(struct elfdump * ed,int ndx)3713fe401a5SEd Maste sh_name(struct elfdump *ed, int ndx)
3723fe401a5SEd Maste {
3733fe401a5SEd Maste 	static char num[10];
3743fe401a5SEd Maste 
3753fe401a5SEd Maste 	switch (ndx) {
3763fe401a5SEd Maste 	case SHN_UNDEF: return "UNDEF";
3773fe401a5SEd Maste 	case SHN_ABS: return "ABS";
3783fe401a5SEd Maste 	case SHN_COMMON: return "COMMON";
3793fe401a5SEd Maste 	default:
3803fe401a5SEd Maste 		if ((uint64_t)ndx < ed->shnum)
3813fe401a5SEd Maste 			return (ed->sl[ndx].name);
3823fe401a5SEd Maste 		else {
3833fe401a5SEd Maste 			snprintf(num, sizeof(num), "%d", ndx);
3843fe401a5SEd Maste 			return (num);
3853fe401a5SEd Maste 		}
3863fe401a5SEd Maste 	}
3873fe401a5SEd Maste }
3883fe401a5SEd Maste 
3893fe401a5SEd Maste /* http://www.sco.com/developers/gabi/latest/ch4.sheader.html#sh_type */
3903fe401a5SEd Maste static const char *
sh_types(uint64_t mach,uint64_t sht)391839529caSEd Maste sh_types(uint64_t mach, uint64_t sht) {
392839529caSEd Maste 	static char unknown_buf[64];
393839529caSEd Maste 
394839529caSEd Maste 	if (sht < 0x60000000) {
3953fe401a5SEd Maste 		switch (sht) {
396839529caSEd Maste 		case SHT_NULL:		return "SHT_NULL";
397839529caSEd Maste 		case SHT_PROGBITS:	return "SHT_PROGBITS";
398839529caSEd Maste 		case SHT_SYMTAB:	return "SHT_SYMTAB";
399839529caSEd Maste 		case SHT_STRTAB:	return "SHT_STRTAB";
400839529caSEd Maste 		case SHT_RELA:		return "SHT_RELA";
401839529caSEd Maste 		case SHT_HASH:		return "SHT_HASH";
402839529caSEd Maste 		case SHT_DYNAMIC:	return "SHT_DYNAMIC";
403839529caSEd Maste 		case SHT_NOTE:		return "SHT_NOTE";
404839529caSEd Maste 		case SHT_NOBITS:	return "SHT_NOBITS";
405839529caSEd Maste 		case SHT_REL:		return "SHT_REL";
406839529caSEd Maste 		case SHT_SHLIB:		return "SHT_SHLIB";
407839529caSEd Maste 		case SHT_DYNSYM:	return "SHT_DYNSYM";
408839529caSEd Maste 		case SHT_INIT_ARRAY:	return "SHT_INIT_ARRAY";
409839529caSEd Maste 		case SHT_FINI_ARRAY:	return "SHT_FINI_ARRAY";
410839529caSEd Maste 		case SHT_PREINIT_ARRAY:	return "SHT_PREINIT_ARRAY";
411839529caSEd Maste 		case SHT_GROUP:		return "SHT_GROUP";
412839529caSEd Maste 		case SHT_SYMTAB_SHNDX:	return "SHT_SYMTAB_SHNDX";
413839529caSEd Maste 		}
414839529caSEd Maste 	} else if (sht < 0x70000000) {
4153fe401a5SEd Maste 		/* 0x60000000-0x6fffffff operating system-specific semantics */
416839529caSEd Maste 		switch (sht) {
4173fe401a5SEd Maste 		case 0x6ffffff0:	return "XXX:VERSYM";
418839529caSEd Maste 		case SHT_SUNW_dof:	return "SHT_SUNW_dof";
419839529caSEd Maste 		case SHT_GNU_HASH:	return "SHT_GNU_HASH";
4203fe401a5SEd Maste 		case 0x6ffffff7:	return "SHT_GNU_LIBLIST";
4213fe401a5SEd Maste 		case 0x6ffffffc:	return "XXX:VERDEF";
422839529caSEd Maste 		case SHT_SUNW_verdef:	return "SHT_SUNW(GNU)_verdef";
423839529caSEd Maste 		case SHT_SUNW_verneed:	return "SHT_SUNW(GNU)_verneed";
424839529caSEd Maste 		case SHT_SUNW_versym:	return "SHT_SUNW(GNU)_versym";
425839529caSEd Maste 		}
426839529caSEd Maste 	} else if (sht < 0x80000000) {
4273fe401a5SEd Maste 		/* 0x70000000 - 0x7fffffff processor-specific semantics */
428839529caSEd Maste 		switch (mach) {
429839529caSEd Maste 		case EM_ARM:
430839529caSEd Maste 			switch (sht) {
431839529caSEd Maste 			case SHT_ARM_EXIDX: return "SHT_ARM_EXIDX";
432839529caSEd Maste 			case SHT_ARM_PREEMPTMAP: return "SHT_ARM_PREEMPTMAP";
433839529caSEd Maste 			case SHT_ARM_ATTRIBUTES: return "SHT_ARM_ATTRIBUTES";
434839529caSEd Maste 			case SHT_ARM_DEBUGOVERLAY:
435839529caSEd Maste 			    return "SHT_ARM_DEBUGOVERLAY";
436839529caSEd Maste 			case SHT_ARM_OVERLAYSECTION:
437839529caSEd Maste 			    return "SHT_ARM_OVERLAYSECTION";
438839529caSEd Maste 			}
439839529caSEd Maste 			break;
440839529caSEd Maste 		case EM_IA_64:
441839529caSEd Maste 			switch (sht) {
4423fe401a5SEd Maste 			case 0x70000000: return "SHT_IA_64_EXT";
4433fe401a5SEd Maste 			case 0x70000001: return "SHT_IA_64_UNWIND";
444839529caSEd Maste 			}
445839529caSEd Maste 			break;
446839529caSEd Maste 		case EM_MIPS:
447839529caSEd Maste 			switch (sht) {
448839529caSEd Maste 			case SHT_MIPS_REGINFO: return "SHT_MIPS_REGINFO";
449839529caSEd Maste 			case SHT_MIPS_OPTIONS: return "SHT_MIPS_OPTIONS";
450839529caSEd Maste 			case SHT_MIPS_ABIFLAGS: return "SHT_MIPS_ABIFLAGS";
451839529caSEd Maste 			}
452839529caSEd Maste 			break;
453839529caSEd Maste 		}
454839529caSEd Maste 		switch (sht) {
4553fe401a5SEd Maste 		case 0x7ffffffd: return "XXX:AUXILIARY";
4563fe401a5SEd Maste 		case 0x7fffffff: return "XXX:FILTER";
4573fe401a5SEd Maste 		}
4583fe401a5SEd Maste 	}
459839529caSEd Maste 	/* 0x80000000 - 0xffffffff application programs */
460839529caSEd Maste 
461839529caSEd Maste 	snprintf(unknown_buf, sizeof(unknown_buf),
462839529caSEd Maste 		"<unknown: %#llx>", (unsigned long long)sht);
463839529caSEd Maste 	return (unknown_buf);
464839529caSEd Maste }
4653fe401a5SEd Maste 
4663fe401a5SEd Maste /*
4673fe401a5SEd Maste  * Define known section flags. These flags are defined in the order
4683fe401a5SEd Maste  * they are to be printed out.
4693fe401a5SEd Maste  */
4703fe401a5SEd Maste #define	DEFINE_SHFLAGS()			\
4713fe401a5SEd Maste 	DEFINE_SHF(WRITE)			\
4723fe401a5SEd Maste 	DEFINE_SHF(ALLOC)			\
4733fe401a5SEd Maste 	DEFINE_SHF(EXECINSTR)			\
4743fe401a5SEd Maste 	DEFINE_SHF(MERGE)			\
4753fe401a5SEd Maste 	DEFINE_SHF(STRINGS)			\
4763fe401a5SEd Maste 	DEFINE_SHF(INFO_LINK)			\
4773fe401a5SEd Maste 	DEFINE_SHF(LINK_ORDER)			\
4783fe401a5SEd Maste 	DEFINE_SHF(OS_NONCONFORMING)		\
4793fe401a5SEd Maste 	DEFINE_SHF(GROUP)			\
480b6b6f9ccSEd Maste 	DEFINE_SHF(TLS)				\
481b6b6f9ccSEd Maste 	DEFINE_SHF(COMPRESSED)
4823fe401a5SEd Maste 
4833fe401a5SEd Maste #undef	DEFINE_SHF
4843fe401a5SEd Maste #define	DEFINE_SHF(F) "SHF_" #F "|"
4853fe401a5SEd Maste #define ALLSHFLAGS	DEFINE_SHFLAGS()
4863fe401a5SEd Maste 
4873fe401a5SEd Maste static const char *
sh_flags(uint64_t shf)4883fe401a5SEd Maste sh_flags(uint64_t shf)
4893fe401a5SEd Maste {
4903fe401a5SEd Maste 	static char	flg[sizeof(ALLSHFLAGS)+1];
4913fe401a5SEd Maste 
4923fe401a5SEd Maste 	flg[0] = '\0';
4933fe401a5SEd Maste 
4943fe401a5SEd Maste #undef	DEFINE_SHF
4953fe401a5SEd Maste #define	DEFINE_SHF(N)				\
4963fe401a5SEd Maste 	if (shf & SHF_##N)			\
4973fe401a5SEd Maste 		strcat(flg, "SHF_" #N "|");	\
4983fe401a5SEd Maste 
4993fe401a5SEd Maste 	DEFINE_SHFLAGS()
5003fe401a5SEd Maste 
5013fe401a5SEd Maste 	flg[strlen(flg) - 1] = '\0'; /* Remove the trailing "|". */
5023fe401a5SEd Maste 
5033fe401a5SEd Maste 	return (flg);
5043fe401a5SEd Maste }
5053fe401a5SEd Maste 
506839529caSEd Maste static const char *
st_type(unsigned int mach,unsigned int type)507839529caSEd Maste st_type(unsigned int mach, unsigned int type)
508839529caSEd Maste {
509839529caSEd Maste 	static char s_type[32];
5103fe401a5SEd Maste 
511839529caSEd Maste 	switch (type) {
512839529caSEd Maste 	case STT_NOTYPE: return "STT_NOTYPE";
513839529caSEd Maste 	case STT_OBJECT: return "STT_OBJECT";
514839529caSEd Maste 	case STT_FUNC: return "STT_FUNC";
515839529caSEd Maste 	case STT_SECTION: return "STT_SECTION";
516839529caSEd Maste 	case STT_FILE: return "STT_FILE";
517839529caSEd Maste 	case STT_COMMON: return "STT_COMMON";
518839529caSEd Maste 	case STT_TLS: return "STT_TLS";
519839529caSEd Maste 	case 13:
520839529caSEd Maste 		if (mach == EM_SPARCV9)
521839529caSEd Maste 			return "STT_SPARC_REGISTER";
522839529caSEd Maste 		break;
523839529caSEd Maste 	}
524839529caSEd Maste 	snprintf(s_type, sizeof(s_type), "<unknown: %#x>", type);
525839529caSEd Maste 	return (s_type);
526839529caSEd Maste }
5273fe401a5SEd Maste 
528839529caSEd Maste static const char *
st_type_S(unsigned int type)529839529caSEd Maste st_type_S(unsigned int type)
530839529caSEd Maste {
531839529caSEd Maste 	static char s_type[32];
5323fe401a5SEd Maste 
533839529caSEd Maste 	switch (type) {
534839529caSEd Maste 	case STT_NOTYPE: return "NOTY";
535839529caSEd Maste 	case STT_OBJECT: return "OBJT";
536839529caSEd Maste 	case STT_FUNC: return "FUNC";
537839529caSEd Maste 	case STT_SECTION: return "SECT";
538839529caSEd Maste 	case STT_FILE: return "FILE";
539839529caSEd Maste 	}
540839529caSEd Maste 	snprintf(s_type, sizeof(s_type), "<unknown: %#x>", type);
541839529caSEd Maste 	return (s_type);
542839529caSEd Maste }
543839529caSEd Maste 
544839529caSEd Maste static const char *
st_bindings(unsigned int sbind)545839529caSEd Maste st_bindings(unsigned int sbind)
546839529caSEd Maste {
547839529caSEd Maste 	static char s_sbind[32];
548839529caSEd Maste 
549839529caSEd Maste 	switch (sbind) {
550839529caSEd Maste 	case STB_LOCAL: return "STB_LOCAL";
551839529caSEd Maste 	case STB_GLOBAL: return "STB_GLOBAL";
552839529caSEd Maste 	case STB_WEAK: return "STB_WEAK";
553839529caSEd Maste 	case STB_GNU_UNIQUE: return "STB_GNU_UNIQUE";
554839529caSEd Maste 	default:
555839529caSEd Maste 		if (sbind >= STB_LOOS && sbind <= STB_HIOS)
556839529caSEd Maste 			return "OS";
557839529caSEd Maste 		else if (sbind >= STB_LOPROC && sbind <= STB_HIPROC)
558839529caSEd Maste 			return "PROC";
559839529caSEd Maste 		else
560839529caSEd Maste 			snprintf(s_sbind, sizeof(s_sbind), "<unknown: %#x>",
561839529caSEd Maste 			    sbind);
562839529caSEd Maste 		return (s_sbind);
563839529caSEd Maste 	}
564839529caSEd Maste }
565839529caSEd Maste 
566839529caSEd Maste static const char *
st_bindings_S(unsigned int sbind)567839529caSEd Maste st_bindings_S(unsigned int sbind)
568839529caSEd Maste {
569839529caSEd Maste 	static char s_sbind[32];
570839529caSEd Maste 
571839529caSEd Maste 	switch (sbind) {
572839529caSEd Maste 	case STB_LOCAL: return "LOCL";
573839529caSEd Maste 	case STB_GLOBAL: return "GLOB";
574839529caSEd Maste 	case STB_WEAK: return "WEAK";
575839529caSEd Maste 	case STB_GNU_UNIQUE: return "UNIQ";
576839529caSEd Maste 	default:
577839529caSEd Maste 		if (sbind >= STB_LOOS && sbind <= STB_HIOS)
578839529caSEd Maste 			return "OS";
579839529caSEd Maste 		else if (sbind >= STB_LOPROC && sbind <= STB_HIPROC)
580839529caSEd Maste 			return "PROC";
581839529caSEd Maste 		else
582839529caSEd Maste 			snprintf(s_sbind, sizeof(s_sbind), "<%#x>",
583839529caSEd Maste 			    sbind);
584839529caSEd Maste 		return (s_sbind);
585839529caSEd Maste 	}
586839529caSEd Maste }
5873fe401a5SEd Maste 
5883fe401a5SEd Maste static unsigned char st_others[] = {
5893fe401a5SEd Maste 	'D', 'I', 'H', 'P'
5903fe401a5SEd Maste };
5913fe401a5SEd Maste 
5923fe401a5SEd Maste static void	add_name(struct elfdump *ed, const char *name);
5933fe401a5SEd Maste static void	elf_print_object(struct elfdump *ed);
5943fe401a5SEd Maste static void	elf_print_elf(struct elfdump *ed);
5953fe401a5SEd Maste static void	elf_print_ehdr(struct elfdump *ed);
5963fe401a5SEd Maste static void	elf_print_phdr(struct elfdump *ed);
5973fe401a5SEd Maste static void	elf_print_shdr(struct elfdump *ed);
5983fe401a5SEd Maste static void	elf_print_symtab(struct elfdump *ed, int i);
5993fe401a5SEd Maste static void	elf_print_symtabs(struct elfdump *ed);
6003fe401a5SEd Maste static void	elf_print_symver(struct elfdump *ed);
6013fe401a5SEd Maste static void	elf_print_verdef(struct elfdump *ed, struct section *s);
6023fe401a5SEd Maste static void	elf_print_verneed(struct elfdump *ed, struct section *s);
6033fe401a5SEd Maste static void	elf_print_interp(struct elfdump *ed);
6043fe401a5SEd Maste static void	elf_print_dynamic(struct elfdump *ed);
6053fe401a5SEd Maste static void	elf_print_rel_entry(struct elfdump *ed, struct section *s,
6063fe401a5SEd Maste     int j, struct rel_entry *r);
6073fe401a5SEd Maste static void	elf_print_rela(struct elfdump *ed, struct section *s,
6083fe401a5SEd Maste     Elf_Data *data);
6093fe401a5SEd Maste static void	elf_print_rel(struct elfdump *ed, struct section *s,
6103fe401a5SEd Maste     Elf_Data *data);
6113fe401a5SEd Maste static void	elf_print_reloc(struct elfdump *ed);
6123fe401a5SEd Maste static void	elf_print_got(struct elfdump *ed);
6133fe401a5SEd Maste static void	elf_print_got_section(struct elfdump *ed, struct section *s);
6143fe401a5SEd Maste static void	elf_print_note(struct elfdump *ed);
6153fe401a5SEd Maste static void	elf_print_svr4_hash(struct elfdump *ed, struct section *s);
6163fe401a5SEd Maste static void	elf_print_svr4_hash64(struct elfdump *ed, struct section *s);
6173fe401a5SEd Maste static void	elf_print_gnu_hash(struct elfdump *ed, struct section *s);
6183fe401a5SEd Maste static void	elf_print_hash(struct elfdump *ed);
6193fe401a5SEd Maste static void	elf_print_checksum(struct elfdump *ed);
6203fe401a5SEd Maste static void	find_gotrel(struct elfdump *ed, struct section *gs,
6213fe401a5SEd Maste     struct rel_entry *got);
6223fe401a5SEd Maste static struct spec_name	*find_name(struct elfdump *ed, const char *name);
623656f49f8SEd Maste static int	get_ent_count(const struct section *s, int *ent_count);
624b6b6f9ccSEd Maste static const char *get_symbol_name(struct elfdump *ed, uint32_t symtab, int i);
6253fe401a5SEd Maste static const char *get_string(struct elfdump *ed, int strtab, size_t off);
6263fe401a5SEd Maste static void	get_versym(struct elfdump *ed, int i, uint16_t **vs, int *nvs);
6273fe401a5SEd Maste static void	load_sections(struct elfdump *ed);
6283fe401a5SEd Maste static void	unload_sections(struct elfdump *ed);
6293fe401a5SEd Maste static void	usage(void);
6303fe401a5SEd Maste #ifdef	USE_LIBARCHIVE_AR
6313fe401a5SEd Maste static int	ac_detect_ar(int fd);
6323fe401a5SEd Maste static void	ac_print_ar(struct elfdump *ed, int fd);
6333fe401a5SEd Maste #else
6343fe401a5SEd Maste static void	elf_print_ar(struct elfdump *ed, int fd);
6353fe401a5SEd Maste #endif	/* USE_LIBARCHIVE_AR */
6363fe401a5SEd Maste 
6373fe401a5SEd Maste static struct option elfdump_longopts[] =
6383fe401a5SEd Maste {
6393fe401a5SEd Maste 	{ "help",	no_argument,	NULL,	'H' },
6403fe401a5SEd Maste 	{ "version",	no_argument,	NULL,	'V' },
6413fe401a5SEd Maste 	{ NULL,		0,		NULL,	0   }
6423fe401a5SEd Maste };
6433fe401a5SEd Maste 
6443fe401a5SEd Maste int
main(int ac,char ** av)6453fe401a5SEd Maste main(int ac, char **av)
6463fe401a5SEd Maste {
6473fe401a5SEd Maste 	struct elfdump		*ed, ed_storage;
6483fe401a5SEd Maste 	struct spec_name	*sn;
6493fe401a5SEd Maste 	int			 ch, i;
6503fe401a5SEd Maste 
6513fe401a5SEd Maste 	ed = &ed_storage;
6523fe401a5SEd Maste 	memset(ed, 0, sizeof(*ed));
6533fe401a5SEd Maste 	STAILQ_INIT(&ed->snl);
6543fe401a5SEd Maste 	ed->out = stdout;
6553fe401a5SEd Maste 	while ((ch = getopt_long(ac, av, "acdeiGHhknN:prsSvVw:",
6563fe401a5SEd Maste 		elfdump_longopts, NULL)) != -1)
6573fe401a5SEd Maste 		switch (ch) {
6583fe401a5SEd Maste 		case 'a':
6593fe401a5SEd Maste 			ed->options = ED_ALL;
6603fe401a5SEd Maste 			break;
6613fe401a5SEd Maste 		case 'c':
6623fe401a5SEd Maste 			ed->options |= ED_SHDR;
6633fe401a5SEd Maste 			break;
6643fe401a5SEd Maste 		case 'd':
6653fe401a5SEd Maste 			ed->options |= ED_DYN;
6663fe401a5SEd Maste 			break;
6673fe401a5SEd Maste 		case 'e':
6683fe401a5SEd Maste 			ed->options |= ED_EHDR;
6693fe401a5SEd Maste 			break;
6703fe401a5SEd Maste 		case 'i':
6713fe401a5SEd Maste 			ed->options |= ED_INTERP;
6723fe401a5SEd Maste 			break;
6733fe401a5SEd Maste 		case 'G':
6743fe401a5SEd Maste 			ed->options |= ED_GOT;
6753fe401a5SEd Maste 			break;
6763fe401a5SEd Maste 		case 'h':
6773fe401a5SEd Maste 			ed->options |= ED_HASH;
6783fe401a5SEd Maste 			break;
6793fe401a5SEd Maste 		case 'k':
6803fe401a5SEd Maste 			ed->options |= ED_CHECKSUM;
6813fe401a5SEd Maste 			break;
6823fe401a5SEd Maste 		case 'n':
6833fe401a5SEd Maste 			ed->options |= ED_NOTE;
6843fe401a5SEd Maste 			break;
6853fe401a5SEd Maste 		case 'N':
6863fe401a5SEd Maste 			add_name(ed, optarg);
6873fe401a5SEd Maste 			break;
6883fe401a5SEd Maste 		case 'p':
6893fe401a5SEd Maste 			ed->options |= ED_PHDR;
6903fe401a5SEd Maste 			break;
6913fe401a5SEd Maste 		case 'r':
6923fe401a5SEd Maste 			ed->options |= ED_REL;
6933fe401a5SEd Maste 			break;
6943fe401a5SEd Maste 		case 's':
6953fe401a5SEd Maste 			ed->options |= ED_SYMTAB;
6963fe401a5SEd Maste 			break;
6973fe401a5SEd Maste 		case 'S':
6983fe401a5SEd Maste 			ed->flags |= SOLARIS_FMT;
6993fe401a5SEd Maste 			break;
7003fe401a5SEd Maste 		case 'v':
7013fe401a5SEd Maste 			ed->options |= ED_SYMVER;
7023fe401a5SEd Maste 			break;
7033fe401a5SEd Maste 		case 'V':
7043fe401a5SEd Maste 			(void) printf("%s (%s)\n", ELFTC_GETPROGNAME(),
7053fe401a5SEd Maste 			    elftc_version());
7063fe401a5SEd Maste 			exit(EXIT_SUCCESS);
7073fe401a5SEd Maste 			break;
7083fe401a5SEd Maste 		case 'w':
7093fe401a5SEd Maste 			if ((ed->out = fopen(optarg, "w")) == NULL)
7103fe401a5SEd Maste 				err(EXIT_FAILURE, "%s", optarg);
7113fe401a5SEd Maste 			break;
7123fe401a5SEd Maste 		case '?':
7133fe401a5SEd Maste 		case 'H':
7143fe401a5SEd Maste 		default:
7153fe401a5SEd Maste 			usage();
7163fe401a5SEd Maste 		}
7173fe401a5SEd Maste 
7183fe401a5SEd Maste 	ac -= optind;
7193fe401a5SEd Maste 	av += optind;
7203fe401a5SEd Maste 
7213fe401a5SEd Maste 	if (ed->options == 0)
7223fe401a5SEd Maste 		ed->options = ED_ALL;
7233fe401a5SEd Maste 	sn = NULL;
7243fe401a5SEd Maste 	if (ed->options & ED_SYMTAB &&
7253fe401a5SEd Maste 	    (STAILQ_EMPTY(&ed->snl) || (sn = find_name(ed, "ARSYM")) != NULL)) {
7263fe401a5SEd Maste 		ed->flags |= PRINT_ARSYM;
7273fe401a5SEd Maste 		if (sn != NULL) {
7283fe401a5SEd Maste 			STAILQ_REMOVE(&ed->snl, sn, spec_name, sn_list);
7293fe401a5SEd Maste 			if (STAILQ_EMPTY(&ed->snl))
7303fe401a5SEd Maste 				ed->flags |= ONLY_ARSYM;
7313fe401a5SEd Maste 		}
7323fe401a5SEd Maste 	}
7333fe401a5SEd Maste 	if (ac == 0)
7343fe401a5SEd Maste 		usage();
7353fe401a5SEd Maste 	if (ac > 1)
7363fe401a5SEd Maste 		ed->flags |= PRINT_FILENAME;
7373fe401a5SEd Maste 	if (elf_version(EV_CURRENT) == EV_NONE)
7383fe401a5SEd Maste 		errx(EXIT_FAILURE, "ELF library initialization failed: %s",
7393fe401a5SEd Maste 		    elf_errmsg(-1));
7403fe401a5SEd Maste 
7413fe401a5SEd Maste 	for (i = 0; i < ac; i++) {
7423fe401a5SEd Maste 		ed->filename = av[i];
7433fe401a5SEd Maste 		ed->archive = NULL;
7443fe401a5SEd Maste 		elf_print_object(ed);
7453fe401a5SEd Maste 	}
7463fe401a5SEd Maste 
7473fe401a5SEd Maste 	exit(EXIT_SUCCESS);
7483fe401a5SEd Maste }
7493fe401a5SEd Maste 
7503fe401a5SEd Maste #ifdef USE_LIBARCHIVE_AR
7513fe401a5SEd Maste 
7523fe401a5SEd Maste /* Archive symbol table entry. */
7533fe401a5SEd Maste struct arsym_entry {
7543fe401a5SEd Maste 	char *sym_name;
7553fe401a5SEd Maste 	size_t off;
7563fe401a5SEd Maste };
7573fe401a5SEd Maste 
7583fe401a5SEd Maste /*
7593fe401a5SEd Maste  * Convenient wrapper for general libarchive error handling.
7603fe401a5SEd Maste  */
7613fe401a5SEd Maste #define	AC(CALL) do {							\
7623fe401a5SEd Maste 	if ((CALL)) {							\
7633fe401a5SEd Maste 		warnx("%s", archive_error_string(a));			\
7643fe401a5SEd Maste 		return;							\
7653fe401a5SEd Maste 	}								\
7663fe401a5SEd Maste } while (0)
7673fe401a5SEd Maste 
7683fe401a5SEd Maste /*
7693fe401a5SEd Maste  * Detect an ar(1) archive using libarchive(3).
7703fe401a5SEd Maste  */
7713fe401a5SEd Maste static int
ac_detect_ar(int fd)7723fe401a5SEd Maste ac_detect_ar(int fd)
7733fe401a5SEd Maste {
7743fe401a5SEd Maste 	struct archive		*a;
7753fe401a5SEd Maste 	struct archive_entry	*entry;
7763fe401a5SEd Maste 	int			 r;
7773fe401a5SEd Maste 
7783fe401a5SEd Maste 	r = -1;
7793fe401a5SEd Maste 	if ((a = archive_read_new()) == NULL)
7803fe401a5SEd Maste 		return (0);
7813fe401a5SEd Maste 	archive_read_support_format_ar(a);
7823fe401a5SEd Maste 	if (archive_read_open_fd(a, fd, 10240) == ARCHIVE_OK)
7833fe401a5SEd Maste 		r = archive_read_next_header(a, &entry);
7843fe401a5SEd Maste 	archive_read_close(a);
7853fe401a5SEd Maste 	archive_read_free(a);
7863fe401a5SEd Maste 
7873fe401a5SEd Maste 	return (r == ARCHIVE_OK);
7883fe401a5SEd Maste }
7893fe401a5SEd Maste 
7903fe401a5SEd Maste /*
7913fe401a5SEd Maste  * Dump an ar(1) archive using libarchive(3).
7923fe401a5SEd Maste  */
7933fe401a5SEd Maste static void
ac_print_ar(struct elfdump * ed,int fd)7943fe401a5SEd Maste ac_print_ar(struct elfdump *ed, int fd)
7953fe401a5SEd Maste {
7963fe401a5SEd Maste 	struct archive		*a;
7973fe401a5SEd Maste 	struct archive_entry	*entry;
7983fe401a5SEd Maste 	struct arsym_entry	*arsym;
7993fe401a5SEd Maste 	const char		*name;
8003fe401a5SEd Maste 	char			 idx[10], *b;
8013fe401a5SEd Maste 	void			*buff;
8023fe401a5SEd Maste 	size_t			 size;
803b6b6f9ccSEd Maste 	uint32_t		 cnt, i;
804b6b6f9ccSEd Maste 	int			 r;
8053fe401a5SEd Maste 
8063fe401a5SEd Maste 	if (lseek(fd, 0, SEEK_SET) == -1)
8073fe401a5SEd Maste 		err(EXIT_FAILURE, "lseek failed");
8083fe401a5SEd Maste 	if ((a = archive_read_new()) == NULL)
8093fe401a5SEd Maste 		errx(EXIT_FAILURE, "%s", archive_error_string(a));
8103fe401a5SEd Maste 	archive_read_support_format_ar(a);
8113fe401a5SEd Maste 	AC(archive_read_open_fd(a, fd, 10240));
8123fe401a5SEd Maste 	for(;;) {
8133fe401a5SEd Maste 		r = archive_read_next_header(a, &entry);
8143fe401a5SEd Maste 		if (r == ARCHIVE_FATAL)
8153fe401a5SEd Maste 			errx(EXIT_FAILURE, "%s", archive_error_string(a));
8163fe401a5SEd Maste 		if (r == ARCHIVE_EOF)
8173fe401a5SEd Maste 			break;
8183fe401a5SEd Maste 		if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY)
8193fe401a5SEd Maste 			warnx("%s", archive_error_string(a));
8203fe401a5SEd Maste 		if (r == ARCHIVE_RETRY)
8213fe401a5SEd Maste 			continue;
8223fe401a5SEd Maste 		name = archive_entry_pathname(entry);
8233fe401a5SEd Maste 		size = archive_entry_size(entry);
8243fe401a5SEd Maste 		if (size == 0)
8253fe401a5SEd Maste 			continue;
8263fe401a5SEd Maste 		if ((buff = malloc(size)) == NULL) {
8273fe401a5SEd Maste 			warn("malloc failed");
8283fe401a5SEd Maste 			continue;
8293fe401a5SEd Maste 		}
8303fe401a5SEd Maste 		if (archive_read_data(a, buff, size) != (ssize_t)size) {
8313fe401a5SEd Maste 			warnx("%s", archive_error_string(a));
8323fe401a5SEd Maste 			free(buff);
8333fe401a5SEd Maste 			continue;
8343fe401a5SEd Maste 		}
8353fe401a5SEd Maste 
8363fe401a5SEd Maste 		/*
8373fe401a5SEd Maste 		 * Note that when processing arsym via libarchive, there is
8383fe401a5SEd Maste 		 * no way to tell which member a certain symbol belongs to,
8393fe401a5SEd Maste 		 * since we can not just "lseek" to a member offset and read
8403fe401a5SEd Maste 		 * the member header.
8413fe401a5SEd Maste 		 */
8423fe401a5SEd Maste 		if (!strcmp(name, "/") && ed->flags & PRINT_ARSYM) {
8433fe401a5SEd Maste 			b = buff;
8443fe401a5SEd Maste 			cnt = be32dec(b);
8453fe401a5SEd Maste 			if (cnt == 0) {
8463fe401a5SEd Maste 				free(buff);
8473fe401a5SEd Maste 				continue;
8483fe401a5SEd Maste 			}
8493fe401a5SEd Maste 			arsym = calloc(cnt, sizeof(*arsym));
8503fe401a5SEd Maste 			if (arsym == NULL)
8513fe401a5SEd Maste 				err(EXIT_FAILURE, "calloc failed");
8523fe401a5SEd Maste 			b += sizeof(uint32_t);
853b6b6f9ccSEd Maste 			for (i = 0; i < cnt; i++) {
8543fe401a5SEd Maste 				arsym[i].off = be32dec(b);
8553fe401a5SEd Maste 				b += sizeof(uint32_t);
8563fe401a5SEd Maste 			}
857b6b6f9ccSEd Maste 			for (i = 0; i < cnt; i++) {
8583fe401a5SEd Maste 				arsym[i].sym_name = b;
8593fe401a5SEd Maste 				b += strlen(b) + 1;
8603fe401a5SEd Maste 			}
8613fe401a5SEd Maste 			if (ed->flags & SOLARIS_FMT) {
8623fe401a5SEd Maste 				PRT("\nSymbol Table: (archive)\n");
8633fe401a5SEd Maste 				PRT("     index    offset    symbol\n");
8643fe401a5SEd Maste 			} else
8653fe401a5SEd Maste 				PRT("\nsymbol table (archive):\n");
866b6b6f9ccSEd Maste 			for (i = 0; i < cnt; i++) {
8673fe401a5SEd Maste 				if (ed->flags & SOLARIS_FMT) {
8683fe401a5SEd Maste 					snprintf(idx, sizeof(idx), "[%d]", i);
8693fe401a5SEd Maste 					PRT("%10s  ", idx);
8703fe401a5SEd Maste 					PRT("0x%8.8jx  ",
8713fe401a5SEd Maste 					    (uintmax_t)arsym[i].off);
8723fe401a5SEd Maste 					PRT("%s\n", arsym[i].sym_name);
8733fe401a5SEd Maste 				} else {
8743fe401a5SEd Maste 					PRT("\nentry: %d\n", i);
8753fe401a5SEd Maste 					PRT("\toffset: %#jx\n",
8763fe401a5SEd Maste 					    (uintmax_t)arsym[i].off);
8773fe401a5SEd Maste 					PRT("\tsymbol: %s\n",
8783fe401a5SEd Maste 					    arsym[i].sym_name);
8793fe401a5SEd Maste 				}
8803fe401a5SEd Maste 			}
8813fe401a5SEd Maste 			free(arsym);
8823fe401a5SEd Maste 			free(buff);
8833fe401a5SEd Maste 			/* No need to continue if we only dump ARSYM. */
8843fe401a5SEd Maste 			if (ed->flags & ONLY_ARSYM) {
8853fe401a5SEd Maste 				AC(archive_read_close(a));
8863fe401a5SEd Maste 				AC(archive_read_free(a));
8873fe401a5SEd Maste 				return;
8883fe401a5SEd Maste 			}
8893fe401a5SEd Maste 			continue;
8903fe401a5SEd Maste 		}
8913fe401a5SEd Maste 		if ((ed->elf = elf_memory(buff, size)) == NULL) {
8923fe401a5SEd Maste 			warnx("elf_memroy() failed: %s",
8933fe401a5SEd Maste 			      elf_errmsg(-1));
8943fe401a5SEd Maste 			free(buff);
8953fe401a5SEd Maste 			continue;
8963fe401a5SEd Maste 		}
8973fe401a5SEd Maste 		/* Skip non-ELF member. */
8983fe401a5SEd Maste 		if (elf_kind(ed->elf) == ELF_K_ELF) {
8993fe401a5SEd Maste 			printf("\n%s(%s):\n", ed->archive, name);
9003fe401a5SEd Maste 			elf_print_elf(ed);
9013fe401a5SEd Maste 		}
9023fe401a5SEd Maste 		elf_end(ed->elf);
9033fe401a5SEd Maste 		free(buff);
9043fe401a5SEd Maste 	}
9053fe401a5SEd Maste 	AC(archive_read_close(a));
9063fe401a5SEd Maste 	AC(archive_read_free(a));
9073fe401a5SEd Maste }
9083fe401a5SEd Maste 
9093fe401a5SEd Maste #else  /* USE_LIBARCHIVE_AR */
9103fe401a5SEd Maste 
9113fe401a5SEd Maste /*
9123fe401a5SEd Maste  * Dump an ar(1) archive.
9133fe401a5SEd Maste  */
9143fe401a5SEd Maste static void
elf_print_ar(struct elfdump * ed,int fd)9153fe401a5SEd Maste elf_print_ar(struct elfdump *ed, int fd)
9163fe401a5SEd Maste {
9173fe401a5SEd Maste 	Elf		*e;
9183fe401a5SEd Maste 	Elf_Arhdr	*arh;
9193fe401a5SEd Maste 	Elf_Arsym	*arsym;
9203fe401a5SEd Maste 	Elf_Cmd		 cmd;
921715d1396SEd Maste 	char		 idx[21];
922b6b6f9ccSEd Maste 	size_t		 cnt, i;
9233fe401a5SEd Maste 
9243fe401a5SEd Maste 	ed->ar = ed->elf;
9253fe401a5SEd Maste 
9263fe401a5SEd Maste 	if (ed->flags & PRINT_ARSYM) {
9273fe401a5SEd Maste 		cnt = 0;
9283fe401a5SEd Maste 		if ((arsym = elf_getarsym(ed->ar, &cnt)) == NULL) {
9293fe401a5SEd Maste 			warnx("elf_getarsym failed: %s", elf_errmsg(-1));
9303fe401a5SEd Maste 			goto print_members;
9313fe401a5SEd Maste 		}
9323fe401a5SEd Maste 		if (cnt == 0)
9333fe401a5SEd Maste 			goto print_members;
9343fe401a5SEd Maste 		if (ed->flags & SOLARIS_FMT) {
9353fe401a5SEd Maste 			PRT("\nSymbol Table: (archive)\n");
9363fe401a5SEd Maste 			PRT("     index    offset    member name and symbol\n");
9373fe401a5SEd Maste 		} else
9383fe401a5SEd Maste 			PRT("\nsymbol table (archive):\n");
939b6b6f9ccSEd Maste 		for (i = 0; i < cnt - 1; i++) {
9403fe401a5SEd Maste 			if (elf_rand(ed->ar, arsym[i].as_off) !=
9413fe401a5SEd Maste 			    arsym[i].as_off) {
9423fe401a5SEd Maste 				warnx("elf_rand failed: %s", elf_errmsg(-1));
9433fe401a5SEd Maste 				break;
9443fe401a5SEd Maste 			}
9453fe401a5SEd Maste 			if ((e = elf_begin(fd, ELF_C_READ, ed->ar)) == NULL) {
9463fe401a5SEd Maste 				warnx("elf_begin failed: %s", elf_errmsg(-1));
9473fe401a5SEd Maste 				break;
9483fe401a5SEd Maste 			}
9493fe401a5SEd Maste 			if ((arh = elf_getarhdr(e)) == NULL) {
9503fe401a5SEd Maste 				warnx("elf_getarhdr failed: %s",
9513fe401a5SEd Maste 				    elf_errmsg(-1));
9523fe401a5SEd Maste 				break;
9533fe401a5SEd Maste 			}
9543fe401a5SEd Maste 			if (ed->flags & SOLARIS_FMT) {
955b6b6f9ccSEd Maste 				snprintf(idx, sizeof(idx), "[%zu]", i);
9563fe401a5SEd Maste 				PRT("%10s  ", idx);
9573fe401a5SEd Maste 				PRT("0x%8.8jx  ",
9583fe401a5SEd Maste 				    (uintmax_t)arsym[i].as_off);
9593fe401a5SEd Maste 				PRT("(%s):%s\n", arh->ar_name,
9603fe401a5SEd Maste 				    arsym[i].as_name);
9613fe401a5SEd Maste 			} else {
962b6b6f9ccSEd Maste 				PRT("\nentry: %zu\n", i);
9633fe401a5SEd Maste 				PRT("\toffset: %#jx\n",
9643fe401a5SEd Maste 				    (uintmax_t)arsym[i].as_off);
9653fe401a5SEd Maste 				PRT("\tmember: %s\n", arh->ar_name);
9663fe401a5SEd Maste 				PRT("\tsymbol: %s\n", arsym[i].as_name);
9673fe401a5SEd Maste 			}
9683fe401a5SEd Maste 			elf_end(e);
9693fe401a5SEd Maste 		}
9703fe401a5SEd Maste 
9713fe401a5SEd Maste 		/* No need to continue if we only dump ARSYM. */
9723fe401a5SEd Maste 		if (ed->flags & ONLY_ARSYM)
9733fe401a5SEd Maste 			return;
9743fe401a5SEd Maste 	}
9753fe401a5SEd Maste 
9763fe401a5SEd Maste print_members:
9773fe401a5SEd Maste 
9783fe401a5SEd Maste 	/* Rewind the archive. */
9793fe401a5SEd Maste 	if (elf_rand(ed->ar, SARMAG) != SARMAG) {
9803fe401a5SEd Maste 		warnx("elf_rand failed: %s", elf_errmsg(-1));
9813fe401a5SEd Maste 		return;
9823fe401a5SEd Maste 	}
9833fe401a5SEd Maste 
9843fe401a5SEd Maste 	/* Dump each member of the archive. */
9853fe401a5SEd Maste 	cmd = ELF_C_READ;
9863fe401a5SEd Maste 	while ((ed->elf = elf_begin(fd, cmd, ed->ar)) != NULL) {
9873fe401a5SEd Maste 		/* Skip non-ELF member. */
9883fe401a5SEd Maste 		if (elf_kind(ed->elf) == ELF_K_ELF) {
9893fe401a5SEd Maste 			if ((arh = elf_getarhdr(ed->elf)) == NULL) {
9903fe401a5SEd Maste 				warnx("elf_getarhdr failed: %s",
9913fe401a5SEd Maste 				    elf_errmsg(-1));
9923fe401a5SEd Maste 				break;
9933fe401a5SEd Maste 			}
9943fe401a5SEd Maste 			printf("\n%s(%s):\n", ed->archive, arh->ar_name);
9953fe401a5SEd Maste 			elf_print_elf(ed);
9963fe401a5SEd Maste 		}
9973fe401a5SEd Maste 		cmd = elf_next(ed->elf);
9983fe401a5SEd Maste 		elf_end(ed->elf);
9993fe401a5SEd Maste 	}
10003fe401a5SEd Maste }
10013fe401a5SEd Maste 
10023fe401a5SEd Maste #endif	/* USE_LIBARCHIVE_AR */
10033fe401a5SEd Maste 
10043fe401a5SEd Maste /*
10053fe401a5SEd Maste  * Dump an object. (ELF object or ar(1) archive)
10063fe401a5SEd Maste  */
10073fe401a5SEd Maste static void
elf_print_object(struct elfdump * ed)10083fe401a5SEd Maste elf_print_object(struct elfdump *ed)
10093fe401a5SEd Maste {
10103fe401a5SEd Maste 	int fd;
10113fe401a5SEd Maste 
10123fe401a5SEd Maste 	if ((fd = open(ed->filename, O_RDONLY)) == -1) {
10133fe401a5SEd Maste 		warn("open %s failed", ed->filename);
10143fe401a5SEd Maste 		return;
10153fe401a5SEd Maste 	}
10163fe401a5SEd Maste 
10173fe401a5SEd Maste #ifdef	USE_LIBARCHIVE_AR
10183fe401a5SEd Maste 	if (ac_detect_ar(fd)) {
10193fe401a5SEd Maste 		ed->archive = ed->filename;
10203fe401a5SEd Maste 		ac_print_ar(ed, fd);
10213fe401a5SEd Maste 		return;
10223fe401a5SEd Maste 	}
10233fe401a5SEd Maste #endif	/* USE_LIBARCHIVE_AR */
10243fe401a5SEd Maste 
10253fe401a5SEd Maste 	if ((ed->elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
10263fe401a5SEd Maste 		warnx("elf_begin() failed: %s", elf_errmsg(-1));
10273fe401a5SEd Maste 		return;
10283fe401a5SEd Maste 	}
10293fe401a5SEd Maste 
10303fe401a5SEd Maste 	switch (elf_kind(ed->elf)) {
10313fe401a5SEd Maste 	case ELF_K_NONE:
10323fe401a5SEd Maste 		warnx("Not an ELF file.");
10333fe401a5SEd Maste 		return;
10343fe401a5SEd Maste 	case ELF_K_ELF:
10353fe401a5SEd Maste 		if (ed->flags & PRINT_FILENAME)
10363fe401a5SEd Maste 			printf("\n%s:\n", ed->filename);
10373fe401a5SEd Maste 		elf_print_elf(ed);
10383fe401a5SEd Maste 		break;
10393fe401a5SEd Maste 	case ELF_K_AR:
10403fe401a5SEd Maste #ifndef	USE_LIBARCHIVE_AR
10413fe401a5SEd Maste 		ed->archive = ed->filename;
10423fe401a5SEd Maste 		elf_print_ar(ed, fd);
10433fe401a5SEd Maste #endif
10443fe401a5SEd Maste 		break;
10453fe401a5SEd Maste 	default:
10463fe401a5SEd Maste 		warnx("Internal: libelf returned unknown elf kind.");
10473fe401a5SEd Maste 		return;
10483fe401a5SEd Maste 	}
10493fe401a5SEd Maste 
10503fe401a5SEd Maste 	elf_end(ed->elf);
10513fe401a5SEd Maste }
10523fe401a5SEd Maste 
10533fe401a5SEd Maste /*
10543fe401a5SEd Maste  * Dump an ELF object.
10553fe401a5SEd Maste  */
10563fe401a5SEd Maste static void
elf_print_elf(struct elfdump * ed)10573fe401a5SEd Maste elf_print_elf(struct elfdump *ed)
10583fe401a5SEd Maste {
10593fe401a5SEd Maste 
10603fe401a5SEd Maste 	if (gelf_getehdr(ed->elf, &ed->ehdr) == NULL) {
10613fe401a5SEd Maste 		warnx("gelf_getehdr failed: %s", elf_errmsg(-1));
10623fe401a5SEd Maste 		return;
10633fe401a5SEd Maste 	}
10643fe401a5SEd Maste 	if ((ed->ec = gelf_getclass(ed->elf)) == ELFCLASSNONE) {
10653fe401a5SEd Maste 		warnx("gelf_getclass failed: %s", elf_errmsg(-1));
10663fe401a5SEd Maste 		return;
10673fe401a5SEd Maste 	}
10683fe401a5SEd Maste 
10693fe401a5SEd Maste 	if (ed->options & (ED_SHDR | ED_DYN | ED_REL | ED_GOT | ED_SYMTAB |
10703fe401a5SEd Maste 	    ED_SYMVER | ED_NOTE | ED_HASH))
10713fe401a5SEd Maste 		load_sections(ed);
10723fe401a5SEd Maste 
10733fe401a5SEd Maste 	if (ed->options & ED_EHDR)
10743fe401a5SEd Maste 		elf_print_ehdr(ed);
10753fe401a5SEd Maste 	if (ed->options & ED_PHDR)
10763fe401a5SEd Maste 		elf_print_phdr(ed);
10773fe401a5SEd Maste 	if (ed->options & ED_INTERP)
10783fe401a5SEd Maste 		elf_print_interp(ed);
10793fe401a5SEd Maste 	if (ed->options & ED_SHDR)
10803fe401a5SEd Maste 		elf_print_shdr(ed);
10813fe401a5SEd Maste 	if (ed->options & ED_DYN)
10823fe401a5SEd Maste 		elf_print_dynamic(ed);
10833fe401a5SEd Maste 	if (ed->options & ED_REL)
10843fe401a5SEd Maste 		elf_print_reloc(ed);
10853fe401a5SEd Maste 	if (ed->options & ED_GOT)
10863fe401a5SEd Maste 		elf_print_got(ed);
10873fe401a5SEd Maste 	if (ed->options & ED_SYMTAB)
10883fe401a5SEd Maste 		elf_print_symtabs(ed);
10893fe401a5SEd Maste 	if (ed->options & ED_SYMVER)
10903fe401a5SEd Maste 		elf_print_symver(ed);
10913fe401a5SEd Maste 	if (ed->options & ED_NOTE)
10923fe401a5SEd Maste 		elf_print_note(ed);
10933fe401a5SEd Maste 	if (ed->options & ED_HASH)
10943fe401a5SEd Maste 		elf_print_hash(ed);
10953fe401a5SEd Maste 	if (ed->options & ED_CHECKSUM)
10963fe401a5SEd Maste 		elf_print_checksum(ed);
10973fe401a5SEd Maste 
10983fe401a5SEd Maste 	unload_sections(ed);
10993fe401a5SEd Maste }
11003fe401a5SEd Maste 
11013fe401a5SEd Maste /*
11023fe401a5SEd Maste  * Read the section headers from ELF object and store them in the
11033fe401a5SEd Maste  * internal cache.
11043fe401a5SEd Maste  */
11053fe401a5SEd Maste static void
load_sections(struct elfdump * ed)11063fe401a5SEd Maste load_sections(struct elfdump *ed)
11073fe401a5SEd Maste {
11083fe401a5SEd Maste 	struct section	*s;
11093fe401a5SEd Maste 	const char	*name;
11103fe401a5SEd Maste 	Elf_Scn		*scn;
11113fe401a5SEd Maste 	GElf_Shdr	 sh;
11123fe401a5SEd Maste 	size_t		 shstrndx, ndx;
11133fe401a5SEd Maste 	int		 elferr;
11143fe401a5SEd Maste 
11153fe401a5SEd Maste 	assert(ed->sl == NULL);
11163fe401a5SEd Maste 
11173fe401a5SEd Maste 	if (!elf_getshnum(ed->elf, &ed->shnum)) {
11183fe401a5SEd Maste 		warnx("elf_getshnum failed: %s", elf_errmsg(-1));
11193fe401a5SEd Maste 		return;
11203fe401a5SEd Maste 	}
11213fe401a5SEd Maste 	if (ed->shnum == 0)
11223fe401a5SEd Maste 		return;
11233fe401a5SEd Maste 	if ((ed->sl = calloc(ed->shnum, sizeof(*ed->sl))) == NULL)
11243fe401a5SEd Maste 		err(EXIT_FAILURE, "calloc failed");
11253fe401a5SEd Maste 	if (!elf_getshstrndx(ed->elf, &shstrndx)) {
11263fe401a5SEd Maste 		warnx("elf_getshstrndx failed: %s", elf_errmsg(-1));
11273fe401a5SEd Maste 		return;
11283fe401a5SEd Maste 	}
11293fe401a5SEd Maste 	if ((scn = elf_getscn(ed->elf, 0)) == NULL) {
11303fe401a5SEd Maste 		warnx("elf_getscn failed: %s", elf_errmsg(-1));
11313fe401a5SEd Maste 		return;
11323fe401a5SEd Maste 	}
11333fe401a5SEd Maste 	(void) elf_errno();
11343fe401a5SEd Maste 	do {
11353fe401a5SEd Maste 		if (gelf_getshdr(scn, &sh) == NULL) {
11363fe401a5SEd Maste 			warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
11373fe401a5SEd Maste 			(void) elf_errno();
11383fe401a5SEd Maste 			continue;
11393fe401a5SEd Maste 		}
11403fe401a5SEd Maste 		if ((name = elf_strptr(ed->elf, shstrndx, sh.sh_name)) == NULL) {
11413fe401a5SEd Maste 			(void) elf_errno();
11423fe401a5SEd Maste 			name = "ERROR";
11433fe401a5SEd Maste 		}
11443fe401a5SEd Maste 		if ((ndx = elf_ndxscn(scn)) == SHN_UNDEF)
11453fe401a5SEd Maste 			if ((elferr = elf_errno()) != 0) {
11463fe401a5SEd Maste 				warnx("elf_ndxscn failed: %s",
11473fe401a5SEd Maste 				    elf_errmsg(elferr));
11483fe401a5SEd Maste 				continue;
11493fe401a5SEd Maste 			}
11503fe401a5SEd Maste 		if (ndx >= ed->shnum) {
11513fe401a5SEd Maste 			warnx("section index of '%s' out of range", name);
11523fe401a5SEd Maste 			continue;
11533fe401a5SEd Maste 		}
11543fe401a5SEd Maste 		s = &ed->sl[ndx];
11553fe401a5SEd Maste 		s->name = name;
11563fe401a5SEd Maste 		s->scn = scn;
11573fe401a5SEd Maste 		s->off = sh.sh_offset;
11583fe401a5SEd Maste 		s->sz = sh.sh_size;
11593fe401a5SEd Maste 		s->entsize = sh.sh_entsize;
11603fe401a5SEd Maste 		s->align = sh.sh_addralign;
11613fe401a5SEd Maste 		s->type = sh.sh_type;
11623fe401a5SEd Maste 		s->flags = sh.sh_flags;
11633fe401a5SEd Maste 		s->addr = sh.sh_addr;
11643fe401a5SEd Maste 		s->link = sh.sh_link;
11653fe401a5SEd Maste 		s->info = sh.sh_info;
11663fe401a5SEd Maste 	} while ((scn = elf_nextscn(ed->elf, scn)) != NULL);
11673fe401a5SEd Maste 	elferr = elf_errno();
11683fe401a5SEd Maste 	if (elferr != 0)
11693fe401a5SEd Maste 		warnx("elf_nextscn failed: %s", elf_errmsg(elferr));
11703fe401a5SEd Maste }
11713fe401a5SEd Maste 
11723fe401a5SEd Maste /*
11733fe401a5SEd Maste  * Release section related resources.
11743fe401a5SEd Maste  */
11753fe401a5SEd Maste static void
unload_sections(struct elfdump * ed)11763fe401a5SEd Maste unload_sections(struct elfdump *ed)
11773fe401a5SEd Maste {
11783fe401a5SEd Maste 	if (ed->sl != NULL) {
11793fe401a5SEd Maste 		free(ed->sl);
11803fe401a5SEd Maste 		ed->sl = NULL;
11813fe401a5SEd Maste 	}
11823fe401a5SEd Maste }
11833fe401a5SEd Maste 
11843fe401a5SEd Maste /*
11853fe401a5SEd Maste  * Add a name to the '-N' name list.
11863fe401a5SEd Maste  */
11873fe401a5SEd Maste static void
add_name(struct elfdump * ed,const char * name)11883fe401a5SEd Maste add_name(struct elfdump *ed, const char *name)
11893fe401a5SEd Maste {
11903fe401a5SEd Maste 	struct spec_name *sn;
11913fe401a5SEd Maste 
11923fe401a5SEd Maste 	if (find_name(ed, name))
11933fe401a5SEd Maste 		return;
11943fe401a5SEd Maste 	if ((sn = malloc(sizeof(*sn))) == NULL) {
11953fe401a5SEd Maste 		warn("malloc failed");
11963fe401a5SEd Maste 		return;
11973fe401a5SEd Maste 	}
11983fe401a5SEd Maste 	sn->name = name;
11993fe401a5SEd Maste 	STAILQ_INSERT_TAIL(&ed->snl, sn, sn_list);
12003fe401a5SEd Maste }
12013fe401a5SEd Maste 
12023fe401a5SEd Maste /*
12033fe401a5SEd Maste  * Lookup a name in the '-N' name list.
12043fe401a5SEd Maste  */
12053fe401a5SEd Maste static struct spec_name *
find_name(struct elfdump * ed,const char * name)12063fe401a5SEd Maste find_name(struct elfdump *ed, const char *name)
12073fe401a5SEd Maste {
12083fe401a5SEd Maste 	struct spec_name *sn;
12093fe401a5SEd Maste 
12103fe401a5SEd Maste 	STAILQ_FOREACH(sn, &ed->snl, sn_list) {
12113fe401a5SEd Maste 		if (!strcmp(sn->name, name))
12123fe401a5SEd Maste 			return (sn);
12133fe401a5SEd Maste 	}
12143fe401a5SEd Maste 
12153fe401a5SEd Maste 	return (NULL);
12163fe401a5SEd Maste }
12173fe401a5SEd Maste 
12183fe401a5SEd Maste /*
12193fe401a5SEd Maste  * Retrieve the name of a symbol using the section index of the symbol
12203fe401a5SEd Maste  * table and the index of the symbol within that table.
12213fe401a5SEd Maste  */
12223fe401a5SEd Maste static const char *
get_symbol_name(struct elfdump * ed,uint32_t symtab,int i)1223b6b6f9ccSEd Maste get_symbol_name(struct elfdump *ed, uint32_t symtab, int i)
12243fe401a5SEd Maste {
12253fe401a5SEd Maste 	static char	 sname[64];
12263fe401a5SEd Maste 	struct section	*s;
12273fe401a5SEd Maste 	const char	*name;
12283fe401a5SEd Maste 	GElf_Sym	 sym;
12293fe401a5SEd Maste 	Elf_Data	*data;
12303fe401a5SEd Maste 	int		 elferr;
12313fe401a5SEd Maste 
1232b6b6f9ccSEd Maste 	if (symtab >= ed->shnum)
1233b6b6f9ccSEd Maste 		return ("");
12343fe401a5SEd Maste 	s = &ed->sl[symtab];
12353fe401a5SEd Maste 	if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM)
12363fe401a5SEd Maste 		return ("");
12373fe401a5SEd Maste 	(void) elf_errno();
12383fe401a5SEd Maste 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
12393fe401a5SEd Maste 		elferr = elf_errno();
12403fe401a5SEd Maste 		if (elferr != 0)
12413fe401a5SEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
12423fe401a5SEd Maste 		return ("");
12433fe401a5SEd Maste 	}
12443fe401a5SEd Maste 	if (gelf_getsym(data, i, &sym) != &sym)
12453fe401a5SEd Maste 		return ("");
12463fe401a5SEd Maste 	if (GELF_ST_TYPE(sym.st_info) == STT_SECTION) {
12473fe401a5SEd Maste 		if (sym.st_shndx < ed->shnum) {
12483fe401a5SEd Maste 			snprintf(sname, sizeof(sname), "%s (section)",
12493fe401a5SEd Maste 			    ed->sl[sym.st_shndx].name);
12503fe401a5SEd Maste 			return (sname);
12513fe401a5SEd Maste 		} else
12523fe401a5SEd Maste 			return ("");
12533fe401a5SEd Maste 	}
12543fe401a5SEd Maste 	if ((name = elf_strptr(ed->elf, s->link, sym.st_name)) == NULL)
12553fe401a5SEd Maste 		return ("");
12563fe401a5SEd Maste 
12573fe401a5SEd Maste 	return (name);
12583fe401a5SEd Maste }
12593fe401a5SEd Maste 
12603fe401a5SEd Maste /*
12613fe401a5SEd Maste  * Retrieve a string using string table section index and the string offset.
12623fe401a5SEd Maste  */
12633fe401a5SEd Maste static const char*
get_string(struct elfdump * ed,int strtab,size_t off)12643fe401a5SEd Maste get_string(struct elfdump *ed, int strtab, size_t off)
12653fe401a5SEd Maste {
12663fe401a5SEd Maste 	const char *name;
12673fe401a5SEd Maste 
12683fe401a5SEd Maste 	if ((name = elf_strptr(ed->elf, strtab, off)) == NULL)
12693fe401a5SEd Maste 		return ("");
12703fe401a5SEd Maste 
12713fe401a5SEd Maste 	return (name);
12723fe401a5SEd Maste }
12733fe401a5SEd Maste 
12743fe401a5SEd Maste /*
12753fe401a5SEd Maste  * Dump the ELF Executable Header.
12763fe401a5SEd Maste  */
12773fe401a5SEd Maste static void
elf_print_ehdr(struct elfdump * ed)12783fe401a5SEd Maste elf_print_ehdr(struct elfdump *ed)
12793fe401a5SEd Maste {
12803fe401a5SEd Maste 
12813fe401a5SEd Maste 	if (!STAILQ_EMPTY(&ed->snl))
12823fe401a5SEd Maste 		return;
12833fe401a5SEd Maste 
12843fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT) {
12853fe401a5SEd Maste 		PRT("\nELF Header\n");
12863fe401a5SEd Maste 		PRT("  ei_magic:   { %#x, %c, %c, %c }\n",
12873fe401a5SEd Maste 		    ed->ehdr.e_ident[0], ed->ehdr.e_ident[1],
12883fe401a5SEd Maste 		    ed->ehdr.e_ident[2], ed->ehdr.e_ident[3]);
12893fe401a5SEd Maste 		PRT("  ei_class:   %-18s",
1290b6b6f9ccSEd Maste 		    elf_class_str(ed->ehdr.e_ident[EI_CLASS]));
1291b6b6f9ccSEd Maste 		PRT("  ei_data:      %s\n",
1292b6b6f9ccSEd Maste 		    elf_data_str(ed->ehdr.e_ident[EI_DATA]));
12933fe401a5SEd Maste 		PRT("  e_machine:  %-18s", e_machines(ed->ehdr.e_machine));
1294b6b6f9ccSEd Maste 		PRT("  e_version:    %s\n",
1295b6b6f9ccSEd Maste 		    elf_version_str(ed->ehdr.e_version));
1296b6b6f9ccSEd Maste 		PRT("  e_type:     %s\n", elf_type_str(ed->ehdr.e_type));
12973fe401a5SEd Maste 		PRT("  e_flags:    %18d\n", ed->ehdr.e_flags);
12983fe401a5SEd Maste 		PRT("  e_entry:    %#18jx", (uintmax_t)ed->ehdr.e_entry);
12993fe401a5SEd Maste 		PRT("  e_ehsize: %6d", ed->ehdr.e_ehsize);
13003fe401a5SEd Maste 		PRT("  e_shstrndx:%5d\n", ed->ehdr.e_shstrndx);
13013fe401a5SEd Maste 		PRT("  e_shoff:    %#18jx", (uintmax_t)ed->ehdr.e_shoff);
13023fe401a5SEd Maste 		PRT("  e_shentsize: %3d", ed->ehdr.e_shentsize);
13033fe401a5SEd Maste 		PRT("  e_shnum:   %5d\n", ed->ehdr.e_shnum);
13043fe401a5SEd Maste 		PRT("  e_phoff:    %#18jx", (uintmax_t)ed->ehdr.e_phoff);
13053fe401a5SEd Maste 		PRT("  e_phentsize: %3d", ed->ehdr.e_phentsize);
13063fe401a5SEd Maste 		PRT("  e_phnum:   %5d\n", ed->ehdr.e_phnum);
13073fe401a5SEd Maste 	} else {
13083fe401a5SEd Maste 		PRT("\nelf header:\n");
13093fe401a5SEd Maste 		PRT("\n");
13103fe401a5SEd Maste 		PRT("\te_ident: %s %s %s\n",
1311b6b6f9ccSEd Maste 		    elf_class_str(ed->ehdr.e_ident[EI_CLASS]),
1312b6b6f9ccSEd Maste 		    elf_data_str(ed->ehdr.e_ident[EI_DATA]),
13133fe401a5SEd Maste 		    ei_abis[ed->ehdr.e_ident[EI_OSABI]]);
1314b6b6f9ccSEd Maste 		PRT("\te_type: %s\n", elf_type_str(ed->ehdr.e_type));
13153fe401a5SEd Maste 		PRT("\te_machine: %s\n", e_machines(ed->ehdr.e_machine));
1316b6b6f9ccSEd Maste 		PRT("\te_version: %s\n", elf_version_str(ed->ehdr.e_version));
13173fe401a5SEd Maste 		PRT("\te_entry: %#jx\n", (uintmax_t)ed->ehdr.e_entry);
13183fe401a5SEd Maste 		PRT("\te_phoff: %ju\n", (uintmax_t)ed->ehdr.e_phoff);
13193fe401a5SEd Maste 		PRT("\te_shoff: %ju\n", (uintmax_t) ed->ehdr.e_shoff);
13203fe401a5SEd Maste 		PRT("\te_flags: %u\n", ed->ehdr.e_flags);
13213fe401a5SEd Maste 		PRT("\te_ehsize: %u\n", ed->ehdr.e_ehsize);
13223fe401a5SEd Maste 		PRT("\te_phentsize: %u\n", ed->ehdr.e_phentsize);
13233fe401a5SEd Maste 		PRT("\te_phnum: %u\n", ed->ehdr.e_phnum);
13243fe401a5SEd Maste 		PRT("\te_shentsize: %u\n", ed->ehdr.e_shentsize);
13253fe401a5SEd Maste 		PRT("\te_shnum: %u\n", ed->ehdr.e_shnum);
13263fe401a5SEd Maste 		PRT("\te_shstrndx: %u\n", ed->ehdr.e_shstrndx);
13273fe401a5SEd Maste 	}
13283fe401a5SEd Maste }
13293fe401a5SEd Maste 
13303fe401a5SEd Maste /*
13313fe401a5SEd Maste  * Dump the ELF Program Header Table.
13323fe401a5SEd Maste  */
13333fe401a5SEd Maste static void
elf_print_phdr(struct elfdump * ed)13343fe401a5SEd Maste elf_print_phdr(struct elfdump *ed)
13353fe401a5SEd Maste {
13363fe401a5SEd Maste 	GElf_Phdr	 ph;
1337b6b6f9ccSEd Maste 	size_t		 phnum, i;
1338b6b6f9ccSEd Maste 	int		 header;
13393fe401a5SEd Maste 
13403fe401a5SEd Maste 	if (elf_getphnum(ed->elf, &phnum) == 0) {
13413fe401a5SEd Maste 		warnx("elf_getphnum failed: %s", elf_errmsg(-1));
13423fe401a5SEd Maste 		return;
13433fe401a5SEd Maste 	}
13443fe401a5SEd Maste 	header = 0;
1345b6b6f9ccSEd Maste 	for (i = 0; i < phnum; i++) {
13463fe401a5SEd Maste 		if (gelf_getphdr(ed->elf, i, &ph) != &ph) {
13473fe401a5SEd Maste 			warnx("elf_getphdr failed: %s", elf_errmsg(-1));
13483fe401a5SEd Maste 			continue;
13493fe401a5SEd Maste 		}
13503fe401a5SEd Maste 		if (!STAILQ_EMPTY(&ed->snl) &&
1351b6b6f9ccSEd Maste 		    find_name(ed, elf_phdr_type_str(ph.p_type)) == NULL)
13523fe401a5SEd Maste 			continue;
13533fe401a5SEd Maste 		if (ed->flags & SOLARIS_FMT) {
1354b6b6f9ccSEd Maste 			PRT("\nProgram Header[%zu]:\n", i);
13553fe401a5SEd Maste 			PRT("    p_vaddr:      %#-14jx", (uintmax_t)ph.p_vaddr);
1356b6b6f9ccSEd Maste 			PRT("  p_flags:    [ %s ]\n",
1357b6b6f9ccSEd Maste 			    p_flags[ph.p_flags & 0x7]);
13583fe401a5SEd Maste 			PRT("    p_paddr:      %#-14jx", (uintmax_t)ph.p_paddr);
1359b6b6f9ccSEd Maste 			PRT("  p_type:     [ %s ]\n",
1360b6b6f9ccSEd Maste 			    elf_phdr_type_str(ph.p_type));
13613fe401a5SEd Maste 			PRT("    p_filesz:     %#-14jx",
13623fe401a5SEd Maste 			    (uintmax_t)ph.p_filesz);
13633fe401a5SEd Maste 			PRT("  p_memsz:    %#jx\n", (uintmax_t)ph.p_memsz);
13643fe401a5SEd Maste 			PRT("    p_offset:     %#-14jx",
13653fe401a5SEd Maste 			    (uintmax_t)ph.p_offset);
13663fe401a5SEd Maste 			PRT("  p_align:    %#jx\n", (uintmax_t)ph.p_align);
13673fe401a5SEd Maste 		} else {
13683fe401a5SEd Maste 			if (!header) {
13693fe401a5SEd Maste 				PRT("\nprogram header:\n");
13703fe401a5SEd Maste 				header = 1;
13713fe401a5SEd Maste 			}
13723fe401a5SEd Maste 			PRT("\n");
1373b6b6f9ccSEd Maste 			PRT("entry: %zu\n", i);
1374b6b6f9ccSEd Maste 			PRT("\tp_type: %s\n", elf_phdr_type_str(ph.p_type));
13753fe401a5SEd Maste 			PRT("\tp_offset: %ju\n", (uintmax_t)ph.p_offset);
13763fe401a5SEd Maste 			PRT("\tp_vaddr: %#jx\n", (uintmax_t)ph.p_vaddr);
13773fe401a5SEd Maste 			PRT("\tp_paddr: %#jx\n", (uintmax_t)ph.p_paddr);
13783fe401a5SEd Maste 			PRT("\tp_filesz: %ju\n", (uintmax_t)ph.p_filesz);
13793fe401a5SEd Maste 			PRT("\tp_memsz: %ju\n", (uintmax_t)ph.p_memsz);
1380b6b6f9ccSEd Maste 			PRT("\tp_flags: %s\n", p_flags[ph.p_flags & 0x7]);
13813fe401a5SEd Maste 			PRT("\tp_align: %ju\n", (uintmax_t)ph.p_align);
13823fe401a5SEd Maste 		}
13833fe401a5SEd Maste 	}
13843fe401a5SEd Maste }
13853fe401a5SEd Maste 
13863fe401a5SEd Maste /*
13873fe401a5SEd Maste  * Dump the ELF Section Header Table.
13883fe401a5SEd Maste  */
13893fe401a5SEd Maste static void
elf_print_shdr(struct elfdump * ed)13903fe401a5SEd Maste elf_print_shdr(struct elfdump *ed)
13913fe401a5SEd Maste {
13923fe401a5SEd Maste 	struct section *s;
1393b6b6f9ccSEd Maste 	size_t i;
13943fe401a5SEd Maste 
13953fe401a5SEd Maste 	if (!STAILQ_EMPTY(&ed->snl))
13963fe401a5SEd Maste 		return;
13973fe401a5SEd Maste 
13983fe401a5SEd Maste 	if ((ed->flags & SOLARIS_FMT) == 0)
13993fe401a5SEd Maste 		PRT("\nsection header:\n");
1400b6b6f9ccSEd Maste 	for (i = 0; i < ed->shnum; i++) {
14013fe401a5SEd Maste 		s = &ed->sl[i];
14023fe401a5SEd Maste 		if (ed->flags & SOLARIS_FMT) {
14033fe401a5SEd Maste 			if (i == 0)
14043fe401a5SEd Maste 				continue;
1405b6b6f9ccSEd Maste 			PRT("\nSection Header[%zu]:", i);
14063fe401a5SEd Maste 			PRT("  sh_name: %s\n", s->name);
14073fe401a5SEd Maste 			PRT("    sh_addr:      %#-14jx", (uintmax_t)s->addr);
14083fe401a5SEd Maste 			if (s->flags != 0)
14093fe401a5SEd Maste 				PRT("  sh_flags:   [ %s ]\n", sh_flags(s->flags));
14103fe401a5SEd Maste 			else
14113fe401a5SEd Maste 				PRT("  sh_flags:   0\n");
14123fe401a5SEd Maste 			PRT("    sh_size:      %#-14jx", (uintmax_t)s->sz);
1413839529caSEd Maste 			PRT("  sh_type:    [ %s ]\n",
1414839529caSEd Maste 			    sh_types(ed->ehdr.e_machine, s->type));
14153fe401a5SEd Maste 			PRT("    sh_offset:    %#-14jx", (uintmax_t)s->off);
14163fe401a5SEd Maste 			PRT("  sh_entsize: %#jx\n", (uintmax_t)s->entsize);
14173fe401a5SEd Maste 			PRT("    sh_link:      %-14u", s->link);
14183fe401a5SEd Maste 			PRT("  sh_info:    %u\n", s->info);
14193fe401a5SEd Maste 			PRT("    sh_addralign: %#jx\n", (uintmax_t)s->align);
14203fe401a5SEd Maste 		} else {
14213fe401a5SEd Maste 			PRT("\n");
14223fe401a5SEd Maste 			PRT("entry: %ju\n", (uintmax_t)i);
14233fe401a5SEd Maste 			PRT("\tsh_name: %s\n", s->name);
1424839529caSEd Maste 			PRT("\tsh_type: %s\n",
1425839529caSEd Maste 			    sh_types(ed->ehdr.e_machine, s->type));
14263fe401a5SEd Maste 			PRT("\tsh_flags: %s\n", sh_flags(s->flags));
14273fe401a5SEd Maste 			PRT("\tsh_addr: %#jx\n", (uintmax_t)s->addr);
14283fe401a5SEd Maste 			PRT("\tsh_offset: %ju\n", (uintmax_t)s->off);
14293fe401a5SEd Maste 			PRT("\tsh_size: %ju\n", (uintmax_t)s->sz);
14303fe401a5SEd Maste 			PRT("\tsh_link: %u\n", s->link);
14313fe401a5SEd Maste 			PRT("\tsh_info: %u\n", s->info);
14323fe401a5SEd Maste 			PRT("\tsh_addralign: %ju\n", (uintmax_t)s->align);
14333fe401a5SEd Maste 			PRT("\tsh_entsize: %ju\n", (uintmax_t)s->entsize);
14343fe401a5SEd Maste 		}
14353fe401a5SEd Maste 	}
14363fe401a5SEd Maste }
14373fe401a5SEd Maste 
14383fe401a5SEd Maste /*
1439656f49f8SEd Maste  * Return number of entries in the given section. We'd prefer ent_count be a
1440656f49f8SEd Maste  * size_t, but libelf APIs already use int for section indices.
1441656f49f8SEd Maste  */
1442656f49f8SEd Maste static int
get_ent_count(const struct section * s,int * ent_count)1443656f49f8SEd Maste get_ent_count(const struct section *s, int *ent_count)
1444656f49f8SEd Maste {
1445656f49f8SEd Maste 	if (s->entsize == 0) {
1446656f49f8SEd Maste 		warnx("section %s has entry size 0", s->name);
1447656f49f8SEd Maste 		return (0);
1448656f49f8SEd Maste 	} else if (s->sz / s->entsize > INT_MAX) {
1449656f49f8SEd Maste 		warnx("section %s has invalid section count", s->name);
1450656f49f8SEd Maste 		return (0);
1451656f49f8SEd Maste 	}
1452656f49f8SEd Maste 	*ent_count = (int)(s->sz / s->entsize);
1453656f49f8SEd Maste 	return (1);
1454656f49f8SEd Maste }
1455656f49f8SEd Maste 
1456656f49f8SEd Maste /*
14573fe401a5SEd Maste  * Retrieve the content of the corresponding SHT_SUNW_versym section for
14583fe401a5SEd Maste  * a symbol table section.
14593fe401a5SEd Maste  */
14603fe401a5SEd Maste static void
get_versym(struct elfdump * ed,int i,uint16_t ** vs,int * nvs)14613fe401a5SEd Maste get_versym(struct elfdump *ed, int i, uint16_t **vs, int *nvs)
14623fe401a5SEd Maste {
14633fe401a5SEd Maste 	struct section	*s;
14643fe401a5SEd Maste 	Elf_Data	*data;
1465b6b6f9ccSEd Maste 	size_t		 j;
1466b6b6f9ccSEd Maste 	int		 elferr;
14673fe401a5SEd Maste 
14683fe401a5SEd Maste 	s = NULL;
1469b6b6f9ccSEd Maste 	for (j = 0; j < ed->shnum; j++) {
14703fe401a5SEd Maste 		s = &ed->sl[j];
14713fe401a5SEd Maste 		if (s->type == SHT_SUNW_versym && s->link == (uint32_t)i)
14723fe401a5SEd Maste 			break;
14733fe401a5SEd Maste 	}
1474b6b6f9ccSEd Maste 	if (j >= ed->shnum) {
14753fe401a5SEd Maste 		*vs = NULL;
14763fe401a5SEd Maste 		return;
14773fe401a5SEd Maste 	}
14783fe401a5SEd Maste 	(void) elf_errno();
14793fe401a5SEd Maste 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
14803fe401a5SEd Maste 		elferr = elf_errno();
14813fe401a5SEd Maste 		if (elferr != 0)
14823fe401a5SEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
14833fe401a5SEd Maste 		*vs = NULL;
14843fe401a5SEd Maste 		return;
14853fe401a5SEd Maste 	}
14863fe401a5SEd Maste 
14873fe401a5SEd Maste 	*vs = data->d_buf;
1488656f49f8SEd Maste 	assert(data->d_size == s->sz);
1489656f49f8SEd Maste 	if (!get_ent_count(s, nvs))
1490656f49f8SEd Maste 		*nvs = 0;
14913fe401a5SEd Maste }
14923fe401a5SEd Maste 
14933fe401a5SEd Maste /*
14943fe401a5SEd Maste  * Dump the symbol table section.
14953fe401a5SEd Maste  */
14963fe401a5SEd Maste static void
elf_print_symtab(struct elfdump * ed,int i)14973fe401a5SEd Maste elf_print_symtab(struct elfdump *ed, int i)
14983fe401a5SEd Maste {
14993fe401a5SEd Maste 	struct section	*s;
15003fe401a5SEd Maste 	const char	*name;
15013fe401a5SEd Maste 	uint16_t	*vs;
1502715d1396SEd Maste 	char		 idx[13];
15033fe401a5SEd Maste 	Elf_Data	*data;
15043fe401a5SEd Maste 	GElf_Sym	 sym;
15053fe401a5SEd Maste 	int		 len, j, elferr, nvs;
15063fe401a5SEd Maste 
15073fe401a5SEd Maste 	s = &ed->sl[i];
15083fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT)
15093fe401a5SEd Maste 		PRT("\nSymbol Table Section:  %s\n", s->name);
15103fe401a5SEd Maste 	else
15113fe401a5SEd Maste 		PRT("\nsymbol table (%s):\n", s->name);
15123fe401a5SEd Maste 	(void) elf_errno();
15133fe401a5SEd Maste 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
15143fe401a5SEd Maste 		elferr = elf_errno();
15153fe401a5SEd Maste 		if (elferr != 0)
15163fe401a5SEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
15173fe401a5SEd Maste 		return;
15183fe401a5SEd Maste 	}
15193fe401a5SEd Maste 	vs = NULL;
15203fe401a5SEd Maste 	nvs = 0;
1521656f49f8SEd Maste 	assert(data->d_size == s->sz);
1522656f49f8SEd Maste 	if (!get_ent_count(s, &len))
1523656f49f8SEd Maste 		return;
15243fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT) {
15253fe401a5SEd Maste 		if (ed->ec == ELFCLASS32)
15263fe401a5SEd Maste 			PRT("     index    value       ");
15273fe401a5SEd Maste 		else
15283fe401a5SEd Maste 			PRT("     index        value           ");
15293fe401a5SEd Maste 		PRT("size     type bind oth ver shndx       name\n");
15303fe401a5SEd Maste 		get_versym(ed, i, &vs, &nvs);
15313fe401a5SEd Maste 		if (vs != NULL && nvs != len) {
15323fe401a5SEd Maste 			warnx("#symbol not equal to #versym");
15333fe401a5SEd Maste 			vs = NULL;
15343fe401a5SEd Maste 		}
15353fe401a5SEd Maste 	}
15363fe401a5SEd Maste 	for (j = 0; j < len; j++) {
15373fe401a5SEd Maste 		if (gelf_getsym(data, j, &sym) != &sym) {
15383fe401a5SEd Maste 			warnx("gelf_getsym failed: %s", elf_errmsg(-1));
15393fe401a5SEd Maste 			continue;
15403fe401a5SEd Maste 		}
15413fe401a5SEd Maste 		name = get_string(ed, s->link, sym.st_name);
15423fe401a5SEd Maste 		if (ed->flags & SOLARIS_FMT) {
15433fe401a5SEd Maste 			snprintf(idx, sizeof(idx), "[%d]", j);
15443fe401a5SEd Maste 			if (ed->ec == ELFCLASS32)
15453fe401a5SEd Maste 				PRT("%10s  ", idx);
15463fe401a5SEd Maste 			else
15473fe401a5SEd Maste 				PRT("%10s      ", idx);
15483fe401a5SEd Maste 			PRT("0x%8.8jx ", (uintmax_t)sym.st_value);
15493fe401a5SEd Maste 			if (ed->ec == ELFCLASS32)
15503fe401a5SEd Maste 				PRT("0x%8.8jx  ", (uintmax_t)sym.st_size);
15513fe401a5SEd Maste 			else
15523fe401a5SEd Maste 				PRT("0x%12.12jx  ", (uintmax_t)sym.st_size);
1553839529caSEd Maste 			PRT("%s ", st_type_S(GELF_ST_TYPE(sym.st_info)));
1554839529caSEd Maste 			PRT("%s  ", st_bindings_S(GELF_ST_BIND(sym.st_info)));
15553fe401a5SEd Maste 			PRT("%c  ", st_others[sym.st_other]);
15563fe401a5SEd Maste 			PRT("%3u ", (vs == NULL ? 0 : vs[j]));
15573fe401a5SEd Maste 			PRT("%-11.11s ", sh_name(ed, sym.st_shndx));
15583fe401a5SEd Maste 			PRT("%s\n", name);
15593fe401a5SEd Maste 		} else {
15603fe401a5SEd Maste 			PRT("\nentry: %d\n", j);
15613fe401a5SEd Maste 			PRT("\tst_name: %s\n", name);
15623fe401a5SEd Maste 			PRT("\tst_value: %#jx\n", (uintmax_t)sym.st_value);
15633fe401a5SEd Maste 			PRT("\tst_size: %ju\n", (uintmax_t)sym.st_size);
15643fe401a5SEd Maste 			PRT("\tst_info: %s %s\n",
1565839529caSEd Maste 			    st_type(ed->ehdr.e_machine,
1566839529caSEd Maste 			    GELF_ST_TYPE(sym.st_info)),
1567839529caSEd Maste 			    st_bindings(GELF_ST_BIND(sym.st_info)));
15683fe401a5SEd Maste 			PRT("\tst_shndx: %ju\n", (uintmax_t)sym.st_shndx);
15693fe401a5SEd Maste 		}
15703fe401a5SEd Maste 	}
15713fe401a5SEd Maste }
15723fe401a5SEd Maste 
15733fe401a5SEd Maste /*
15743fe401a5SEd Maste  * Dump the symbol tables. (.dynsym and .symtab)
15753fe401a5SEd Maste  */
15763fe401a5SEd Maste static void
elf_print_symtabs(struct elfdump * ed)15773fe401a5SEd Maste elf_print_symtabs(struct elfdump *ed)
15783fe401a5SEd Maste {
1579b6b6f9ccSEd Maste 	size_t i;
15803fe401a5SEd Maste 
1581b6b6f9ccSEd Maste 	for (i = 0; i < ed->shnum; i++)
15823fe401a5SEd Maste 		if ((ed->sl[i].type == SHT_SYMTAB ||
15833fe401a5SEd Maste 		    ed->sl[i].type == SHT_DYNSYM) &&
15843fe401a5SEd Maste 		    (STAILQ_EMPTY(&ed->snl) || find_name(ed, ed->sl[i].name)))
15853fe401a5SEd Maste 			elf_print_symtab(ed, i);
15863fe401a5SEd Maste }
15873fe401a5SEd Maste 
15883fe401a5SEd Maste /*
15893fe401a5SEd Maste  * Dump the content of .dynamic section.
15903fe401a5SEd Maste  */
15913fe401a5SEd Maste static void
elf_print_dynamic(struct elfdump * ed)15923fe401a5SEd Maste elf_print_dynamic(struct elfdump *ed)
15933fe401a5SEd Maste {
15943fe401a5SEd Maste 	struct section	*s;
15953fe401a5SEd Maste 	const char	*name;
1596715d1396SEd Maste 	char		 idx[13];
15973fe401a5SEd Maste 	Elf_Data	*data;
15983fe401a5SEd Maste 	GElf_Dyn	 dyn;
15993fe401a5SEd Maste 	int		 elferr, i, len;
16003fe401a5SEd Maste 
16013fe401a5SEd Maste 	s = NULL;
16023fe401a5SEd Maste 	for (i = 0; (size_t)i < ed->shnum; i++) {
16033fe401a5SEd Maste 		s = &ed->sl[i];
16043fe401a5SEd Maste 		if (s->type == SHT_DYNAMIC &&
16053fe401a5SEd Maste 		    (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name)))
16063fe401a5SEd Maste 			break;
16073fe401a5SEd Maste 	}
16083fe401a5SEd Maste 	if ((size_t)i >= ed->shnum)
16093fe401a5SEd Maste 		return;
16103fe401a5SEd Maste 
16113fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT) {
16123fe401a5SEd Maste 		PRT("Dynamic Section:  %s\n", s->name);
16133fe401a5SEd Maste 		PRT("     index  tag               value\n");
16143fe401a5SEd Maste 	} else
16153fe401a5SEd Maste 		PRT("\ndynamic:\n");
16163fe401a5SEd Maste 	(void) elf_errno();
16173fe401a5SEd Maste 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
16183fe401a5SEd Maste 		elferr = elf_errno();
16193fe401a5SEd Maste 		if (elferr != 0)
16203fe401a5SEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
16213fe401a5SEd Maste 		return;
16223fe401a5SEd Maste 	}
1623656f49f8SEd Maste 	assert(data->d_size == s->sz);
1624656f49f8SEd Maste 	if (!get_ent_count(s, &len))
1625656f49f8SEd Maste 		return;
16263fe401a5SEd Maste 	for (i = 0; i < len; i++) {
16273fe401a5SEd Maste 		if (gelf_getdyn(data, i, &dyn) != &dyn) {
16283fe401a5SEd Maste 			warnx("gelf_getdyn failed: %s", elf_errmsg(-1));
16293fe401a5SEd Maste 			continue;
16303fe401a5SEd Maste 		}
16313fe401a5SEd Maste 
16323fe401a5SEd Maste 		if (ed->flags & SOLARIS_FMT) {
16333fe401a5SEd Maste 			snprintf(idx, sizeof(idx), "[%d]", i);
16343fe401a5SEd Maste 			PRT("%10s  %-16s ", idx, d_tags(dyn.d_tag));
16353fe401a5SEd Maste 		} else {
16363fe401a5SEd Maste 			PRT("\n");
16373fe401a5SEd Maste 			PRT("entry: %d\n", i);
16383fe401a5SEd Maste 			PRT("\td_tag: %s\n", d_tags(dyn.d_tag));
16393fe401a5SEd Maste 		}
16403fe401a5SEd Maste 		switch(dyn.d_tag) {
16413fe401a5SEd Maste 		case DT_NEEDED:
16423fe401a5SEd Maste 		case DT_SONAME:
16433fe401a5SEd Maste 		case DT_RPATH:
1644b6b6f9ccSEd Maste 		case DT_RUNPATH:
16453fe401a5SEd Maste 			if ((name = elf_strptr(ed->elf, s->link,
16463fe401a5SEd Maste 				    dyn.d_un.d_val)) == NULL)
16473fe401a5SEd Maste 				name = "";
16483fe401a5SEd Maste 			if (ed->flags & SOLARIS_FMT)
16493fe401a5SEd Maste 				PRT("%#-16jx %s\n", (uintmax_t)dyn.d_un.d_val,
16503fe401a5SEd Maste 				    name);
16513fe401a5SEd Maste 			else
16523fe401a5SEd Maste 				PRT("\td_val: %s\n", name);
16533fe401a5SEd Maste 			break;
16543fe401a5SEd Maste 		case DT_PLTRELSZ:
16553fe401a5SEd Maste 		case DT_RELA:
16563fe401a5SEd Maste 		case DT_RELASZ:
16573fe401a5SEd Maste 		case DT_RELAENT:
16583fe401a5SEd Maste 		case DT_RELACOUNT:
16593fe401a5SEd Maste 		case DT_STRSZ:
16603fe401a5SEd Maste 		case DT_SYMENT:
16613fe401a5SEd Maste 		case DT_RELSZ:
16623fe401a5SEd Maste 		case DT_RELENT:
16633fe401a5SEd Maste 		case DT_PLTREL:
16643fe401a5SEd Maste 		case DT_VERDEF:
16653fe401a5SEd Maste 		case DT_VERDEFNUM:
16663fe401a5SEd Maste 		case DT_VERNEED:
16673fe401a5SEd Maste 		case DT_VERNEEDNUM:
16683fe401a5SEd Maste 		case DT_VERSYM:
16693fe401a5SEd Maste 			if (ed->flags & SOLARIS_FMT)
16703fe401a5SEd Maste 				PRT("%#jx\n", (uintmax_t)dyn.d_un.d_val);
16713fe401a5SEd Maste 			else
16723fe401a5SEd Maste 				PRT("\td_val: %ju\n",
16733fe401a5SEd Maste 				    (uintmax_t)dyn.d_un.d_val);
16743fe401a5SEd Maste 			break;
16753fe401a5SEd Maste 		case DT_PLTGOT:
16763fe401a5SEd Maste 		case DT_HASH:
16773fe401a5SEd Maste 		case DT_GNU_HASH:
16783fe401a5SEd Maste 		case DT_STRTAB:
16793fe401a5SEd Maste 		case DT_SYMTAB:
16803fe401a5SEd Maste 		case DT_INIT:
16813fe401a5SEd Maste 		case DT_FINI:
16823fe401a5SEd Maste 		case DT_REL:
16833fe401a5SEd Maste 		case DT_JMPREL:
16843fe401a5SEd Maste 		case DT_DEBUG:
16853fe401a5SEd Maste 			if (ed->flags & SOLARIS_FMT)
16863fe401a5SEd Maste 				PRT("%#jx\n", (uintmax_t)dyn.d_un.d_ptr);
16873fe401a5SEd Maste 			else
16883fe401a5SEd Maste 				PRT("\td_ptr: %#jx\n",
16893fe401a5SEd Maste 				    (uintmax_t)dyn.d_un.d_ptr);
16903fe401a5SEd Maste 			break;
16913fe401a5SEd Maste 		case DT_NULL:
16923fe401a5SEd Maste 		case DT_SYMBOLIC:
16933fe401a5SEd Maste 		case DT_TEXTREL:
16943fe401a5SEd Maste 		default:
16953fe401a5SEd Maste 			if (ed->flags & SOLARIS_FMT)
16963fe401a5SEd Maste 				PRT("\n");
16973fe401a5SEd Maste 			break;
16983fe401a5SEd Maste 		}
16993fe401a5SEd Maste 	}
17003fe401a5SEd Maste }
17013fe401a5SEd Maste 
17023fe401a5SEd Maste /*
17033fe401a5SEd Maste  * Dump a .rel/.rela section entry.
17043fe401a5SEd Maste  */
17053fe401a5SEd Maste static void
elf_print_rel_entry(struct elfdump * ed,struct section * s,int j,struct rel_entry * r)17063fe401a5SEd Maste elf_print_rel_entry(struct elfdump *ed, struct section *s, int j,
17073fe401a5SEd Maste     struct rel_entry *r)
17083fe401a5SEd Maste {
17093fe401a5SEd Maste 
17103fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT) {
1711b6b6f9ccSEd Maste 		PRT("        %-23s ", elftc_reloc_type_str(ed->ehdr.e_machine,
17123fe401a5SEd Maste 			GELF_R_TYPE(r->u_r.rel.r_info)));
17133fe401a5SEd Maste 		PRT("%#12jx ", (uintmax_t)r->u_r.rel.r_offset);
17143fe401a5SEd Maste 		if (r->type == SHT_RELA)
17153fe401a5SEd Maste 			PRT("%10jd  ", (intmax_t)r->u_r.rela.r_addend);
17163fe401a5SEd Maste 		else
17173fe401a5SEd Maste 			PRT("    ");
17183fe401a5SEd Maste 		PRT("%-14s ", s->name);
17193fe401a5SEd Maste 		PRT("%s\n", r->symn);
17203fe401a5SEd Maste 	} else {
17213fe401a5SEd Maste 		PRT("\n");
17223fe401a5SEd Maste 		PRT("entry: %d\n", j);
17233fe401a5SEd Maste 		PRT("\tr_offset: %#jx\n", (uintmax_t)r->u_r.rel.r_offset);
17243fe401a5SEd Maste 		if (ed->ec == ELFCLASS32)
17253fe401a5SEd Maste 			PRT("\tr_info: %#jx\n", (uintmax_t)
17263fe401a5SEd Maste 			    ELF32_R_INFO(ELF64_R_SYM(r->u_r.rel.r_info),
17273fe401a5SEd Maste 			    ELF64_R_TYPE(r->u_r.rel.r_info)));
17283fe401a5SEd Maste 		else
17293fe401a5SEd Maste 			PRT("\tr_info: %#jx\n", (uintmax_t)r->u_r.rel.r_info);
17303fe401a5SEd Maste 		if (r->type == SHT_RELA)
17313fe401a5SEd Maste 			PRT("\tr_addend: %jd\n",
17323fe401a5SEd Maste 			    (intmax_t)r->u_r.rela.r_addend);
17333fe401a5SEd Maste 	}
17343fe401a5SEd Maste }
17353fe401a5SEd Maste 
17363fe401a5SEd Maste /*
17373fe401a5SEd Maste  * Dump a relocation section of type SHT_RELA.
17383fe401a5SEd Maste  */
17393fe401a5SEd Maste static void
elf_print_rela(struct elfdump * ed,struct section * s,Elf_Data * data)17403fe401a5SEd Maste elf_print_rela(struct elfdump *ed, struct section *s, Elf_Data *data)
17413fe401a5SEd Maste {
17423fe401a5SEd Maste 	struct rel_entry	r;
17433fe401a5SEd Maste 	int			j, len;
17443fe401a5SEd Maste 
17453fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT) {
17463fe401a5SEd Maste 		PRT("\nRelocation Section:  %s\n", s->name);
17473fe401a5SEd Maste 		PRT("        type                          offset     "
17483fe401a5SEd Maste 		    "addend  section        with respect to\n");
17493fe401a5SEd Maste 	} else
17503fe401a5SEd Maste 		PRT("\nrelocation with addend (%s):\n", s->name);
17513fe401a5SEd Maste 	r.type = SHT_RELA;
1752656f49f8SEd Maste 	assert(data->d_size == s->sz);
1753656f49f8SEd Maste 	if (!get_ent_count(s, &len))
1754656f49f8SEd Maste 		return;
17553fe401a5SEd Maste 	for (j = 0; j < len; j++) {
17563fe401a5SEd Maste 		if (gelf_getrela(data, j, &r.u_r.rela) != &r.u_r.rela) {
17573fe401a5SEd Maste 			warnx("gelf_getrela failed: %s",
17583fe401a5SEd Maste 			    elf_errmsg(-1));
17593fe401a5SEd Maste 			continue;
17603fe401a5SEd Maste 		}
17613fe401a5SEd Maste 		r.symn = get_symbol_name(ed, s->link,
17623fe401a5SEd Maste 		    GELF_R_SYM(r.u_r.rela.r_info));
17633fe401a5SEd Maste 		elf_print_rel_entry(ed, s, j, &r);
17643fe401a5SEd Maste 	}
17653fe401a5SEd Maste }
17663fe401a5SEd Maste 
17673fe401a5SEd Maste /*
17683fe401a5SEd Maste  * Dump a relocation section of type SHT_REL.
17693fe401a5SEd Maste  */
17703fe401a5SEd Maste static void
elf_print_rel(struct elfdump * ed,struct section * s,Elf_Data * data)17713fe401a5SEd Maste elf_print_rel(struct elfdump *ed, struct section *s, Elf_Data *data)
17723fe401a5SEd Maste {
17733fe401a5SEd Maste 	struct rel_entry	r;
17743fe401a5SEd Maste 	int			j, len;
17753fe401a5SEd Maste 
17763fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT) {
17773fe401a5SEd Maste 		PRT("\nRelocation Section:  %s\n", s->name);
17783fe401a5SEd Maste 		PRT("        type                          offset     "
17793fe401a5SEd Maste 		    "section        with respect to\n");
17803fe401a5SEd Maste 	} else
17813fe401a5SEd Maste 		PRT("\nrelocation (%s):\n", s->name);
17823fe401a5SEd Maste 	r.type = SHT_REL;
1783656f49f8SEd Maste 	assert(data->d_size == s->sz);
1784656f49f8SEd Maste 	if (!get_ent_count(s, &len))
1785656f49f8SEd Maste 		return;
17863fe401a5SEd Maste 	for (j = 0; j < len; j++) {
17873fe401a5SEd Maste 		if (gelf_getrel(data, j, &r.u_r.rel) != &r.u_r.rel) {
17883fe401a5SEd Maste 			warnx("gelf_getrel failed: %s", elf_errmsg(-1));
17893fe401a5SEd Maste 			continue;
17903fe401a5SEd Maste 		}
17913fe401a5SEd Maste 		r.symn = get_symbol_name(ed, s->link,
17923fe401a5SEd Maste 		    GELF_R_SYM(r.u_r.rel.r_info));
17933fe401a5SEd Maste 		elf_print_rel_entry(ed, s, j, &r);
17943fe401a5SEd Maste 	}
17953fe401a5SEd Maste }
17963fe401a5SEd Maste 
17973fe401a5SEd Maste /*
17983fe401a5SEd Maste  * Dump relocation sections.
17993fe401a5SEd Maste  */
18003fe401a5SEd Maste static void
elf_print_reloc(struct elfdump * ed)18013fe401a5SEd Maste elf_print_reloc(struct elfdump *ed)
18023fe401a5SEd Maste {
18033fe401a5SEd Maste 	struct section	*s;
18043fe401a5SEd Maste 	Elf_Data	*data;
1805b6b6f9ccSEd Maste 	size_t		 i;
1806b6b6f9ccSEd Maste 	int		 elferr;
18073fe401a5SEd Maste 
1808b6b6f9ccSEd Maste 	for (i = 0; i < ed->shnum; i++) {
18093fe401a5SEd Maste 		s = &ed->sl[i];
18103fe401a5SEd Maste 		if ((s->type == SHT_REL || s->type == SHT_RELA) &&
18113fe401a5SEd Maste 		    (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name))) {
18123fe401a5SEd Maste 			(void) elf_errno();
18133fe401a5SEd Maste 			if ((data = elf_getdata(s->scn, NULL)) == NULL) {
18143fe401a5SEd Maste 				elferr = elf_errno();
18153fe401a5SEd Maste 				if (elferr != 0)
18163fe401a5SEd Maste 					warnx("elf_getdata failed: %s",
18173fe401a5SEd Maste 					    elf_errmsg(elferr));
18183fe401a5SEd Maste 				continue;
18193fe401a5SEd Maste 			}
18203fe401a5SEd Maste 			if (s->type == SHT_REL)
18213fe401a5SEd Maste 				elf_print_rel(ed, s, data);
18223fe401a5SEd Maste 			else
18233fe401a5SEd Maste 				elf_print_rela(ed, s, data);
18243fe401a5SEd Maste 		}
18253fe401a5SEd Maste 	}
18263fe401a5SEd Maste }
18273fe401a5SEd Maste 
18283fe401a5SEd Maste /*
18293fe401a5SEd Maste  * Dump the content of PT_INTERP segment.
18303fe401a5SEd Maste  */
18313fe401a5SEd Maste static void
elf_print_interp(struct elfdump * ed)18323fe401a5SEd Maste elf_print_interp(struct elfdump *ed)
18333fe401a5SEd Maste {
18343fe401a5SEd Maste 	const char *s;
18353fe401a5SEd Maste 	GElf_Phdr phdr;
1836b6b6f9ccSEd Maste 	size_t filesize, i, phnum;
18373fe401a5SEd Maste 
18383fe401a5SEd Maste 	if (!STAILQ_EMPTY(&ed->snl) && find_name(ed, "PT_INTERP") == NULL)
18393fe401a5SEd Maste 		return;
18403fe401a5SEd Maste 
1841b6b6f9ccSEd Maste 	if ((s = elf_rawfile(ed->elf, &filesize)) == NULL) {
18423fe401a5SEd Maste 		warnx("elf_rawfile failed: %s", elf_errmsg(-1));
18433fe401a5SEd Maste 		return;
18443fe401a5SEd Maste 	}
18453fe401a5SEd Maste 	if (!elf_getphnum(ed->elf, &phnum)) {
18463fe401a5SEd Maste 		warnx("elf_getphnum failed: %s", elf_errmsg(-1));
18473fe401a5SEd Maste 		return;
18483fe401a5SEd Maste 	}
1849b6b6f9ccSEd Maste 	for (i = 0; i < phnum; i++) {
18503fe401a5SEd Maste 		if (gelf_getphdr(ed->elf, i, &phdr) != &phdr) {
18513fe401a5SEd Maste 			warnx("elf_getphdr failed: %s", elf_errmsg(-1));
18523fe401a5SEd Maste 			continue;
18533fe401a5SEd Maste 		}
18543fe401a5SEd Maste 		if (phdr.p_type == PT_INTERP) {
1855b6b6f9ccSEd Maste 			if (phdr.p_offset >= filesize) {
1856b6b6f9ccSEd Maste 				warnx("invalid phdr offset");
1857b6b6f9ccSEd Maste 				continue;
1858b6b6f9ccSEd Maste 			}
18593fe401a5SEd Maste 			PRT("\ninterp:\n");
18603fe401a5SEd Maste 			PRT("\t%s\n", s + phdr.p_offset);
18613fe401a5SEd Maste 		}
18623fe401a5SEd Maste 	}
18633fe401a5SEd Maste }
18643fe401a5SEd Maste 
18653fe401a5SEd Maste /*
1866b6b6f9ccSEd Maste  * Search the relocation sections for entries referring to the .got section.
18673fe401a5SEd Maste  */
18683fe401a5SEd Maste static void
find_gotrel(struct elfdump * ed,struct section * gs,struct rel_entry * got)18693fe401a5SEd Maste find_gotrel(struct elfdump *ed, struct section *gs, struct rel_entry *got)
18703fe401a5SEd Maste {
18713fe401a5SEd Maste 	struct section		*s;
18723fe401a5SEd Maste 	struct rel_entry	 r;
18733fe401a5SEd Maste 	Elf_Data		*data;
1874b6b6f9ccSEd Maste 	size_t			 i;
1875b6b6f9ccSEd Maste 	int			 elferr, j, k, len;
18763fe401a5SEd Maste 
1877b6b6f9ccSEd Maste 	for(i = 0; i < ed->shnum; i++) {
18783fe401a5SEd Maste 		s = &ed->sl[i];
18793fe401a5SEd Maste 		if (s->type != SHT_REL && s->type != SHT_RELA)
18803fe401a5SEd Maste 			continue;
18813fe401a5SEd Maste 		(void) elf_errno();
18823fe401a5SEd Maste 		if ((data = elf_getdata(s->scn, NULL)) == NULL) {
18833fe401a5SEd Maste 			elferr = elf_errno();
18843fe401a5SEd Maste 			if (elferr != 0)
18853fe401a5SEd Maste 				warnx("elf_getdata failed: %s",
18863fe401a5SEd Maste 				    elf_errmsg(elferr));
18873fe401a5SEd Maste 			return;
18883fe401a5SEd Maste 		}
18893fe401a5SEd Maste 		memset(&r, 0, sizeof(struct rel_entry));
18903fe401a5SEd Maste 		r.type = s->type;
1891656f49f8SEd Maste 		assert(data->d_size == s->sz);
1892656f49f8SEd Maste 		if (!get_ent_count(s, &len))
1893656f49f8SEd Maste 			return;
18943fe401a5SEd Maste 		for (j = 0; j < len; j++) {
18953fe401a5SEd Maste 			if (s->type == SHT_REL) {
18963fe401a5SEd Maste 				if (gelf_getrel(data, j, &r.u_r.rel) !=
18973fe401a5SEd Maste 				    &r.u_r.rel) {
18983fe401a5SEd Maste 					warnx("gelf_getrel failed: %s",
18993fe401a5SEd Maste 					    elf_errmsg(-1));
19003fe401a5SEd Maste 					continue;
19013fe401a5SEd Maste 				}
19023fe401a5SEd Maste 			} else {
19033fe401a5SEd Maste 				if (gelf_getrela(data, j, &r.u_r.rela) !=
19043fe401a5SEd Maste 				    &r.u_r.rela) {
19053fe401a5SEd Maste 					warnx("gelf_getrel failed: %s",
19063fe401a5SEd Maste 					    elf_errmsg(-1));
19073fe401a5SEd Maste 					continue;
19083fe401a5SEd Maste 				}
19093fe401a5SEd Maste 			}
19103fe401a5SEd Maste 			if (r.u_r.rel.r_offset >= gs->addr &&
19113fe401a5SEd Maste 			    r.u_r.rel.r_offset < gs->addr + gs->sz) {
19123fe401a5SEd Maste 				r.symn = get_symbol_name(ed, s->link,
19133fe401a5SEd Maste 				    GELF_R_SYM(r.u_r.rel.r_info));
19143fe401a5SEd Maste 				k = (r.u_r.rel.r_offset - gs->addr) /
19153fe401a5SEd Maste 				    gs->entsize;
19163fe401a5SEd Maste 				memcpy(&got[k], &r, sizeof(struct rel_entry));
19173fe401a5SEd Maste 			}
19183fe401a5SEd Maste 		}
19193fe401a5SEd Maste 	}
19203fe401a5SEd Maste }
19213fe401a5SEd Maste 
19223fe401a5SEd Maste static void
elf_print_got_section(struct elfdump * ed,struct section * s)19233fe401a5SEd Maste elf_print_got_section(struct elfdump *ed, struct section *s)
19243fe401a5SEd Maste {
19253fe401a5SEd Maste 	struct rel_entry	*got;
19263fe401a5SEd Maste 	Elf_Data		*data, dst;
19273fe401a5SEd Maste 	int			 elferr, i, len;
19283fe401a5SEd Maste 
19293fe401a5SEd Maste 	if (s->entsize == 0) {
19303fe401a5SEd Maste 		/* XXX IA64 GOT section generated by gcc has entry size 0. */
19313fe401a5SEd Maste 		if (s->align != 0)
19323fe401a5SEd Maste 			s->entsize = s->align;
19333fe401a5SEd Maste 		else
19343fe401a5SEd Maste 			return;
19353fe401a5SEd Maste 	}
19363fe401a5SEd Maste 
1937656f49f8SEd Maste 	if (!get_ent_count(s, &len))
1938656f49f8SEd Maste 		return;
19393fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT)
1940656f49f8SEd Maste 		PRT("\nGlobal Offset Table Section:  %s  (%d entries)\n",
1941656f49f8SEd Maste 		    s->name, len);
19423fe401a5SEd Maste 	else
19433fe401a5SEd Maste 		PRT("\nglobal offset table: %s\n", s->name);
19443fe401a5SEd Maste 	(void) elf_errno();
19453fe401a5SEd Maste 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
19463fe401a5SEd Maste 		elferr = elf_errno();
19473fe401a5SEd Maste 		if (elferr != 0)
19483fe401a5SEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
19493fe401a5SEd Maste 		return;
19503fe401a5SEd Maste 	}
19513fe401a5SEd Maste 
19523fe401a5SEd Maste 	/*
19533fe401a5SEd Maste 	 * GOT section has section type SHT_PROGBITS, thus libelf treats it as
1954b6b6f9ccSEd Maste 	 * byte stream and will not perform any translation on it. As a result,
19553fe401a5SEd Maste 	 * an exlicit call to gelf_xlatetom is needed here. Depends on arch,
19563fe401a5SEd Maste 	 * GOT section should be translated to either WORD or XWORD.
19573fe401a5SEd Maste 	 */
19583fe401a5SEd Maste 	if (ed->ec == ELFCLASS32)
19593fe401a5SEd Maste 		data->d_type = ELF_T_WORD;
19603fe401a5SEd Maste 	else
19613fe401a5SEd Maste 		data->d_type = ELF_T_XWORD;
19623fe401a5SEd Maste 	memcpy(&dst, data, sizeof(Elf_Data));
19633fe401a5SEd Maste 	if (gelf_xlatetom(ed->elf, &dst, data, ed->ehdr.e_ident[EI_DATA]) !=
19643fe401a5SEd Maste 	    &dst) {
19653fe401a5SEd Maste 		warnx("gelf_xlatetom failed: %s", elf_errmsg(-1));
19663fe401a5SEd Maste 		return;
19673fe401a5SEd Maste 	}
1968656f49f8SEd Maste 	assert(dst.d_size == s->sz);
19693fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT) {
19703fe401a5SEd Maste 		/*
19713fe401a5SEd Maste 		 * In verbose/Solaris mode, we search the relocation sections
19723fe401a5SEd Maste 		 * and try to find the corresponding reloc entry for each GOT
19733fe401a5SEd Maste 		 * section entry.
19743fe401a5SEd Maste 		 */
19753fe401a5SEd Maste 		if ((got = calloc(len, sizeof(struct rel_entry))) == NULL)
19763fe401a5SEd Maste 			err(EXIT_FAILURE, "calloc failed");
19773fe401a5SEd Maste 		find_gotrel(ed, s, got);
19783fe401a5SEd Maste 		if (ed->ec == ELFCLASS32) {
19793fe401a5SEd Maste 			PRT(" ndx     addr      value    reloc              ");
19803fe401a5SEd Maste 			PRT("addend   symbol\n");
19813fe401a5SEd Maste 		} else {
19823fe401a5SEd Maste 			PRT(" ndx     addr              value             ");
19833fe401a5SEd Maste 			PRT("reloc              addend       symbol\n");
19843fe401a5SEd Maste 		}
19853fe401a5SEd Maste 		for(i = 0; i < len; i++) {
19863fe401a5SEd Maste 			PRT("[%5.5d]  ", i);
19873fe401a5SEd Maste 			if (ed->ec == ELFCLASS32) {
1988839529caSEd Maste 				PRT("%-8.8jx  ",
1989839529caSEd Maste 				    (uintmax_t) (s->addr + i * s->entsize));
19903fe401a5SEd Maste 				PRT("%-8.8x ", *((uint32_t *)dst.d_buf + i));
19913fe401a5SEd Maste 			} else {
1992839529caSEd Maste 				PRT("%-16.16jx  ",
1993839529caSEd Maste 				    (uintmax_t) (s->addr + i * s->entsize));
1994839529caSEd Maste 				PRT("%-16.16jx  ",
1995839529caSEd Maste 				    (uintmax_t) *((uint64_t *)dst.d_buf + i));
19963fe401a5SEd Maste 			}
1997b6b6f9ccSEd Maste 			PRT("%-18s ", elftc_reloc_type_str(ed->ehdr.e_machine,
19983fe401a5SEd Maste 				GELF_R_TYPE(got[i].u_r.rel.r_info)));
19993fe401a5SEd Maste 			if (ed->ec == ELFCLASS32)
20003fe401a5SEd Maste 				PRT("%-8.8jd ",
20013fe401a5SEd Maste 				    (intmax_t)got[i].u_r.rela.r_addend);
20023fe401a5SEd Maste 			else
20033fe401a5SEd Maste 				PRT("%-12.12jd ",
20043fe401a5SEd Maste 				    (intmax_t)got[i].u_r.rela.r_addend);
20053fe401a5SEd Maste 			if (got[i].symn == NULL)
20063fe401a5SEd Maste 				got[i].symn = "";
20073fe401a5SEd Maste 			PRT("%s\n", got[i].symn);
20083fe401a5SEd Maste 		}
20093fe401a5SEd Maste 		free(got);
20103fe401a5SEd Maste 	} else {
20113fe401a5SEd Maste 		for(i = 0; i < len; i++) {
20123fe401a5SEd Maste 			PRT("\nentry: %d\n", i);
20133fe401a5SEd Maste 			if (ed->ec == ELFCLASS32)
20143fe401a5SEd Maste 				PRT("\t%#x\n", *((uint32_t *)dst.d_buf + i));
20153fe401a5SEd Maste 			else
2016839529caSEd Maste 				PRT("\t%#jx\n",
2017839529caSEd Maste 				    (uintmax_t) *((uint64_t *)dst.d_buf + i));
20183fe401a5SEd Maste 		}
20193fe401a5SEd Maste 	}
20203fe401a5SEd Maste }
20213fe401a5SEd Maste 
20223fe401a5SEd Maste /*
20233fe401a5SEd Maste  * Dump the content of Global Offset Table section.
20243fe401a5SEd Maste  */
20253fe401a5SEd Maste static void
elf_print_got(struct elfdump * ed)20263fe401a5SEd Maste elf_print_got(struct elfdump *ed)
20273fe401a5SEd Maste {
20283fe401a5SEd Maste 	struct section	*s;
2029b6b6f9ccSEd Maste 	size_t		 i;
20303fe401a5SEd Maste 
20313fe401a5SEd Maste 	if (!STAILQ_EMPTY(&ed->snl))
20323fe401a5SEd Maste 		return;
20333fe401a5SEd Maste 
20343fe401a5SEd Maste 	s = NULL;
2035b6b6f9ccSEd Maste 	for (i = 0; i < ed->shnum; i++) {
20363fe401a5SEd Maste 		s = &ed->sl[i];
20373fe401a5SEd Maste 		if (s->name && !strncmp(s->name, ".got", 4) &&
20383fe401a5SEd Maste 		    (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name)))
20393fe401a5SEd Maste 			elf_print_got_section(ed, s);
20403fe401a5SEd Maste 	}
20413fe401a5SEd Maste }
20423fe401a5SEd Maste 
20433fe401a5SEd Maste /*
20443fe401a5SEd Maste  * Dump the content of .note.ABI-tag section.
20453fe401a5SEd Maste  */
20463fe401a5SEd Maste static void
elf_print_note(struct elfdump * ed)20473fe401a5SEd Maste elf_print_note(struct elfdump *ed)
20483fe401a5SEd Maste {
20493fe401a5SEd Maste 	struct section	*s;
20503fe401a5SEd Maste 	Elf_Data        *data;
20513fe401a5SEd Maste 	Elf_Note	*en;
20523fe401a5SEd Maste 	uint32_t	 namesz;
20533fe401a5SEd Maste 	uint32_t	 descsz;
20543fe401a5SEd Maste 	uint32_t	 desc;
20553fe401a5SEd Maste 	size_t		 count;
20563fe401a5SEd Maste 	int		 elferr, i;
2057b6b6f9ccSEd Maste 	uint8_t		*src;
2058715d1396SEd Maste 	char		 idx[17];
20593fe401a5SEd Maste 
20603fe401a5SEd Maste 	s = NULL;
20613fe401a5SEd Maste 	for (i = 0; (size_t)i < ed->shnum; i++) {
20623fe401a5SEd Maste 		s = &ed->sl[i];
20633fe401a5SEd Maste 		if (s->type == SHT_NOTE && s->name &&
20643fe401a5SEd Maste 		    !strcmp(s->name, ".note.ABI-tag") &&
20653fe401a5SEd Maste 		    (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name)))
20663fe401a5SEd Maste 			break;
20673fe401a5SEd Maste 	}
20683fe401a5SEd Maste 	if ((size_t)i >= ed->shnum)
20693fe401a5SEd Maste 		return;
20703fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT)
20713fe401a5SEd Maste 		PRT("\nNote Section:  %s\n", s->name);
20723fe401a5SEd Maste 	else
20733fe401a5SEd Maste 		PRT("\nnote (%s):\n", s->name);
20743fe401a5SEd Maste 	(void) elf_errno();
20753fe401a5SEd Maste 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
20763fe401a5SEd Maste 		elferr = elf_errno();
20773fe401a5SEd Maste 		if (elferr != 0)
20783fe401a5SEd Maste 			warnx("elf_getdata failed: %s", elf_errmsg(elferr));
20793fe401a5SEd Maste 		return;
20803fe401a5SEd Maste 	}
20813fe401a5SEd Maste 	src = data->d_buf;
20823fe401a5SEd Maste 	count = data->d_size;
20833fe401a5SEd Maste 	while (count > sizeof(Elf_Note)) {
20843fe401a5SEd Maste 		en = (Elf_Note *) (uintptr_t) src;
20853fe401a5SEd Maste 		namesz = en->n_namesz;
20863fe401a5SEd Maste 		descsz = en->n_descsz;
20873fe401a5SEd Maste 		src += sizeof(Elf_Note);
20883fe401a5SEd Maste 		count -= sizeof(Elf_Note);
2089b6b6f9ccSEd Maste 		if (roundup2(namesz, 4) + roundup2(descsz, 4) > count) {
2090b6b6f9ccSEd Maste 			warnx("truncated note section");
2091b6b6f9ccSEd Maste 			return;
2092b6b6f9ccSEd Maste 		}
20933fe401a5SEd Maste 		if (ed->flags & SOLARIS_FMT) {
20943fe401a5SEd Maste 			PRT("\n    type   %#x\n", en->n_type);
20953fe401a5SEd Maste 			PRT("    namesz %#x:\n", en->n_namesz);
20963fe401a5SEd Maste 			PRT("%s\n", src);
20973fe401a5SEd Maste 		} else
20983fe401a5SEd Maste 			PRT("\t%s ", src);
20993fe401a5SEd Maste 		src += roundup2(namesz, 4);
21003fe401a5SEd Maste 		count -= roundup2(namesz, 4);
21013fe401a5SEd Maste 
21023fe401a5SEd Maste 		/*
21033fe401a5SEd Maste 		 * Note that we dump the whole desc part if we're in
21043fe401a5SEd Maste 		 * "Solaris mode", while in the normal mode, we only look
21053fe401a5SEd Maste 		 * at the first 4 bytes (a 32bit word) of the desc, i.e,
21063fe401a5SEd Maste 		 * we assume that it's always a FreeBSD version number.
21073fe401a5SEd Maste 		 */
21083fe401a5SEd Maste 		if (ed->flags & SOLARIS_FMT) {
21093fe401a5SEd Maste 			PRT("    descsz %#x:", en->n_descsz);
21103fe401a5SEd Maste 			for (i = 0; (uint32_t)i < descsz; i++) {
21113fe401a5SEd Maste 				if ((i & 0xF) == 0) {
21123fe401a5SEd Maste 					snprintf(idx, sizeof(idx), "desc[%d]",
21133fe401a5SEd Maste 					    i);
21143fe401a5SEd Maste 					PRT("\n      %-9s", idx);
21153fe401a5SEd Maste 				} else if ((i & 0x3) == 0)
21163fe401a5SEd Maste 					PRT("  ");
21173fe401a5SEd Maste 				PRT(" %2.2x", src[i]);
21183fe401a5SEd Maste 			}
21193fe401a5SEd Maste 			PRT("\n");
21203fe401a5SEd Maste 		} else {
21213fe401a5SEd Maste 			if (ed->ehdr.e_ident[EI_DATA] == ELFDATA2MSB)
21223fe401a5SEd Maste 				desc = be32dec(src);
21233fe401a5SEd Maste 			else
21243fe401a5SEd Maste 				desc = le32dec(src);
21253fe401a5SEd Maste 			PRT("%d\n", desc);
21263fe401a5SEd Maste 		}
21273fe401a5SEd Maste 		src += roundup2(descsz, 4);
21283fe401a5SEd Maste 		count -= roundup2(descsz, 4);
21293fe401a5SEd Maste 	}
21303fe401a5SEd Maste }
21313fe401a5SEd Maste 
21323fe401a5SEd Maste /*
21333fe401a5SEd Maste  * Dump a hash table.
21343fe401a5SEd Maste  */
21353fe401a5SEd Maste static void
elf_print_svr4_hash(struct elfdump * ed,struct section * s)21363fe401a5SEd Maste elf_print_svr4_hash(struct elfdump *ed, struct section *s)
21373fe401a5SEd Maste {
21383fe401a5SEd Maste 	Elf_Data	*data;
21393fe401a5SEd Maste 	uint32_t	*buf;
21403fe401a5SEd Maste 	uint32_t	*bucket, *chain;
21413fe401a5SEd Maste 	uint32_t	 nbucket, nchain;
21423fe401a5SEd Maste 	uint32_t	*bl, *c, maxl, total;
2143b6b6f9ccSEd Maste 	uint32_t	 i, j;
2144b6b6f9ccSEd Maste 	int		 first, elferr;
21453fe401a5SEd Maste 	char		 idx[10];
21463fe401a5SEd Maste 
21473fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT)
21483fe401a5SEd Maste 		PRT("\nHash Section:  %s\n", s->name);
21493fe401a5SEd Maste 	else
21503fe401a5SEd Maste 		PRT("\nhash table (%s):\n", s->name);
21513fe401a5SEd Maste 	(void) elf_errno();
21523fe401a5SEd Maste 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
21533fe401a5SEd Maste 		elferr = elf_errno();
21543fe401a5SEd Maste 		if (elferr != 0)
21553fe401a5SEd Maste 			warnx("elf_getdata failed: %s",
21563fe401a5SEd Maste 			    elf_errmsg(elferr));
21573fe401a5SEd Maste 		return;
21583fe401a5SEd Maste 	}
21593fe401a5SEd Maste 	if (data->d_size < 2 * sizeof(uint32_t)) {
21603fe401a5SEd Maste 		warnx(".hash section too small");
21613fe401a5SEd Maste 		return;
21623fe401a5SEd Maste 	}
21633fe401a5SEd Maste 	buf = data->d_buf;
21643fe401a5SEd Maste 	nbucket = buf[0];
21653fe401a5SEd Maste 	nchain = buf[1];
21663fe401a5SEd Maste 	if (nbucket <= 0 || nchain <= 0) {
21673fe401a5SEd Maste 		warnx("Malformed .hash section");
21683fe401a5SEd Maste 		return;
21693fe401a5SEd Maste 	}
2170b6b6f9ccSEd Maste 	if (data->d_size !=
2171b6b6f9ccSEd Maste 	    ((uint64_t)nbucket + (uint64_t)nchain + 2) * sizeof(uint32_t)) {
21723fe401a5SEd Maste 		warnx("Malformed .hash section");
21733fe401a5SEd Maste 		return;
21743fe401a5SEd Maste 	}
21753fe401a5SEd Maste 	bucket = &buf[2];
21763fe401a5SEd Maste 	chain = &buf[2 + nbucket];
21773fe401a5SEd Maste 
21783fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT) {
21793fe401a5SEd Maste 		maxl = 0;
21803fe401a5SEd Maste 		if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
21813fe401a5SEd Maste 			err(EXIT_FAILURE, "calloc failed");
2182b6b6f9ccSEd Maste 		for (i = 0; i < nbucket; i++)
2183b6b6f9ccSEd Maste 			for (j = bucket[i]; j > 0 && j < nchain; j = chain[j])
21843fe401a5SEd Maste 				if (++bl[i] > maxl)
21853fe401a5SEd Maste 					maxl = bl[i];
21863fe401a5SEd Maste 		if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
21873fe401a5SEd Maste 			err(EXIT_FAILURE, "calloc failed");
2188b6b6f9ccSEd Maste 		for (i = 0; i < nbucket; i++)
21893fe401a5SEd Maste 			c[bl[i]]++;
21903fe401a5SEd Maste 		PRT("    bucket    symndx    name\n");
2191b6b6f9ccSEd Maste 		for (i = 0; i < nbucket; i++) {
21923fe401a5SEd Maste 			first = 1;
2193b6b6f9ccSEd Maste 			for (j = bucket[i]; j > 0 && j < nchain; j = chain[j]) {
21943fe401a5SEd Maste 				if (first) {
21953fe401a5SEd Maste 					PRT("%10d  ", i);
21963fe401a5SEd Maste 					first = 0;
21973fe401a5SEd Maste 				} else
21983fe401a5SEd Maste 					PRT("            ");
21993fe401a5SEd Maste 				snprintf(idx, sizeof(idx), "[%d]", j);
22003fe401a5SEd Maste 				PRT("%-10s  ", idx);
22013fe401a5SEd Maste 				PRT("%s\n", get_symbol_name(ed, s->link, j));
22023fe401a5SEd Maste 			}
22033fe401a5SEd Maste 		}
22043fe401a5SEd Maste 		PRT("\n");
22053fe401a5SEd Maste 		total = 0;
2206b6b6f9ccSEd Maste 		for (i = 0; i <= maxl; i++) {
22073fe401a5SEd Maste 			total += c[i] * i;
22083fe401a5SEd Maste 			PRT("%10u  buckets contain %8d symbols\n", c[i], i);
22093fe401a5SEd Maste 		}
22103fe401a5SEd Maste 		PRT("%10u  buckets         %8u symbols (globals)\n", nbucket,
22113fe401a5SEd Maste 		    total);
22123fe401a5SEd Maste 	} else {
22133fe401a5SEd Maste 		PRT("\nnbucket: %u\n", nbucket);
22143fe401a5SEd Maste 		PRT("nchain: %u\n\n", nchain);
2215b6b6f9ccSEd Maste 		for (i = 0; i < nbucket; i++)
22163fe401a5SEd Maste 			PRT("bucket[%d]:\n\t%u\n\n", i, bucket[i]);
2217b6b6f9ccSEd Maste 		for (i = 0; i < nchain; i++)
22183fe401a5SEd Maste 			PRT("chain[%d]:\n\t%u\n\n", i, chain[i]);
22193fe401a5SEd Maste 	}
22203fe401a5SEd Maste }
22213fe401a5SEd Maste 
22223fe401a5SEd Maste /*
22233fe401a5SEd Maste  * Dump a 64bit hash table.
22243fe401a5SEd Maste  */
22253fe401a5SEd Maste static void
elf_print_svr4_hash64(struct elfdump * ed,struct section * s)22263fe401a5SEd Maste elf_print_svr4_hash64(struct elfdump *ed, struct section *s)
22273fe401a5SEd Maste {
22283fe401a5SEd Maste 	Elf_Data	*data, dst;
22293fe401a5SEd Maste 	uint64_t	*buf;
22303fe401a5SEd Maste 	uint64_t	*bucket, *chain;
22313fe401a5SEd Maste 	uint64_t	 nbucket, nchain;
223218f4c9dbSEd Maste 	uint64_t	*bl, *c, j, maxl, total;
223318f4c9dbSEd Maste 	size_t		 i;
2234b6b6f9ccSEd Maste 	int		 elferr, first;
22353fe401a5SEd Maste 	char		 idx[10];
22363fe401a5SEd Maste 
22373fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT)
22383fe401a5SEd Maste 		PRT("\nHash Section:  %s\n", s->name);
22393fe401a5SEd Maste 	else
22403fe401a5SEd Maste 		PRT("\nhash table (%s):\n", s->name);
22413fe401a5SEd Maste 
22423fe401a5SEd Maste 	/*
22433fe401a5SEd Maste 	 * ALPHA uses 64-bit hash entries. Since libelf assumes that
22443fe401a5SEd Maste 	 * .hash section contains only 32-bit entry, an explicit
22453fe401a5SEd Maste 	 * gelf_xlatetom is needed here.
22463fe401a5SEd Maste 	 */
22473fe401a5SEd Maste 	(void) elf_errno();
22483fe401a5SEd Maste 	if ((data = elf_rawdata(s->scn, NULL)) == NULL) {
22493fe401a5SEd Maste 		elferr = elf_errno();
22503fe401a5SEd Maste 		if (elferr != 0)
22513fe401a5SEd Maste 			warnx("elf_rawdata failed: %s",
22523fe401a5SEd Maste 			    elf_errmsg(elferr));
22533fe401a5SEd Maste 		return;
22543fe401a5SEd Maste 	}
22553fe401a5SEd Maste 	data->d_type = ELF_T_XWORD;
22563fe401a5SEd Maste 	memcpy(&dst, data, sizeof(Elf_Data));
22573fe401a5SEd Maste 	if (gelf_xlatetom(ed->elf, &dst, data,
22583fe401a5SEd Maste 		ed->ehdr.e_ident[EI_DATA]) != &dst) {
22593fe401a5SEd Maste 		warnx("gelf_xlatetom failed: %s", elf_errmsg(-1));
22603fe401a5SEd Maste 		return;
22613fe401a5SEd Maste 	}
22623fe401a5SEd Maste 	if (dst.d_size < 2 * sizeof(uint64_t)) {
22633fe401a5SEd Maste 		warnx(".hash section too small");
22643fe401a5SEd Maste 		return;
22653fe401a5SEd Maste 	}
22663fe401a5SEd Maste 	buf = dst.d_buf;
22673fe401a5SEd Maste 	nbucket = buf[0];
22683fe401a5SEd Maste 	nchain = buf[1];
22693fe401a5SEd Maste 	if (nbucket <= 0 || nchain <= 0) {
22703fe401a5SEd Maste 		warnx("Malformed .hash section");
22713fe401a5SEd Maste 		return;
22723fe401a5SEd Maste 	}
22733fe401a5SEd Maste 	if (dst.d_size != (nbucket + nchain + 2) * sizeof(uint64_t)) {
22743fe401a5SEd Maste 		warnx("Malformed .hash section");
22753fe401a5SEd Maste 		return;
22763fe401a5SEd Maste 	}
22773fe401a5SEd Maste 	bucket = &buf[2];
22783fe401a5SEd Maste 	chain = &buf[2 + nbucket];
22793fe401a5SEd Maste 
22803fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT) {
22813fe401a5SEd Maste 		maxl = 0;
22823fe401a5SEd Maste 		if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
22833fe401a5SEd Maste 			err(EXIT_FAILURE, "calloc failed");
2284b6b6f9ccSEd Maste 		for (i = 0; i < nbucket; i++)
2285b6b6f9ccSEd Maste 			for (j = bucket[i]; j > 0 && j < nchain; j = chain[j])
22863fe401a5SEd Maste 				if (++bl[i] > maxl)
22873fe401a5SEd Maste 					maxl = bl[i];
22883fe401a5SEd Maste 		if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
22893fe401a5SEd Maste 			err(EXIT_FAILURE, "calloc failed");
2290b6b6f9ccSEd Maste 		for (i = 0; i < nbucket; i++)
22913fe401a5SEd Maste 			c[bl[i]]++;
22923fe401a5SEd Maste 		PRT("    bucket    symndx    name\n");
2293b6b6f9ccSEd Maste 		for (i = 0; i < nbucket; i++) {
22943fe401a5SEd Maste 			first = 1;
2295b6b6f9ccSEd Maste 			for (j = bucket[i]; j > 0 && j < nchain; j = chain[j]) {
22963fe401a5SEd Maste 				if (first) {
2297b6b6f9ccSEd Maste 					PRT("%10zu  ", i);
22983fe401a5SEd Maste 					first = 0;
22993fe401a5SEd Maste 				} else
23003fe401a5SEd Maste 					PRT("            ");
2301b6b6f9ccSEd Maste 				snprintf(idx, sizeof(idx), "[%zu]", (size_t)j);
23023fe401a5SEd Maste 				PRT("%-10s  ", idx);
23033fe401a5SEd Maste 				PRT("%s\n", get_symbol_name(ed, s->link, j));
23043fe401a5SEd Maste 			}
23053fe401a5SEd Maste 		}
23063fe401a5SEd Maste 		PRT("\n");
23073fe401a5SEd Maste 		total = 0;
2308b6b6f9ccSEd Maste 		for (i = 0; i <= maxl; i++) {
23093fe401a5SEd Maste 			total += c[i] * i;
2310b6b6f9ccSEd Maste 			PRT("%10ju  buckets contain %8zu symbols\n",
23113fe401a5SEd Maste 			    (uintmax_t)c[i], i);
23123fe401a5SEd Maste 		}
23133fe401a5SEd Maste 		PRT("%10ju  buckets         %8ju symbols (globals)\n",
23143fe401a5SEd Maste 		    (uintmax_t)nbucket, (uintmax_t)total);
23153fe401a5SEd Maste 	} else {
23163fe401a5SEd Maste 		PRT("\nnbucket: %ju\n", (uintmax_t)nbucket);
23173fe401a5SEd Maste 		PRT("nchain: %ju\n\n", (uintmax_t)nchain);
2318b6b6f9ccSEd Maste 		for (i = 0; i < nbucket; i++)
2319b6b6f9ccSEd Maste 			PRT("bucket[%zu]:\n\t%ju\n\n", i, (uintmax_t)bucket[i]);
2320b6b6f9ccSEd Maste 		for (i = 0; i < nchain; i++)
2321b6b6f9ccSEd Maste 			PRT("chain[%zu]:\n\t%ju\n\n", i, (uintmax_t)chain[i]);
23223fe401a5SEd Maste 	}
23233fe401a5SEd Maste 
23243fe401a5SEd Maste }
23253fe401a5SEd Maste 
23263fe401a5SEd Maste /*
23273fe401a5SEd Maste  * Dump a GNU hash table.
23283fe401a5SEd Maste  */
23293fe401a5SEd Maste static void
elf_print_gnu_hash(struct elfdump * ed,struct section * s)23303fe401a5SEd Maste elf_print_gnu_hash(struct elfdump *ed, struct section *s)
23313fe401a5SEd Maste {
23323fe401a5SEd Maste 	struct section	*ds;
23333fe401a5SEd Maste 	Elf_Data	*data;
23343fe401a5SEd Maste 	uint32_t	*buf;
23353fe401a5SEd Maste 	uint32_t	*bucket, *chain;
23363fe401a5SEd Maste 	uint32_t	 nbucket, nchain, symndx, maskwords, shift2;
23373fe401a5SEd Maste 	uint32_t	*bl, *c, maxl, total;
2338b6b6f9ccSEd Maste 	uint32_t	 i, j;
2339b6b6f9ccSEd Maste 	int		 first, elferr, dynsymcount;
23403fe401a5SEd Maste 	char		 idx[10];
23413fe401a5SEd Maste 
23423fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT)
23433fe401a5SEd Maste 		PRT("\nGNU Hash Section:  %s\n", s->name);
23443fe401a5SEd Maste 	else
23453fe401a5SEd Maste 		PRT("\ngnu hash table (%s):\n", s->name);
23463fe401a5SEd Maste 	(void) elf_errno();
23473fe401a5SEd Maste 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
23483fe401a5SEd Maste 		elferr = elf_errno();
23493fe401a5SEd Maste 		if (elferr != 0)
23503fe401a5SEd Maste 			warnx("elf_getdata failed: %s",
23513fe401a5SEd Maste 			    elf_errmsg(elferr));
23523fe401a5SEd Maste 		return;
23533fe401a5SEd Maste 	}
23543fe401a5SEd Maste 	if (data->d_size < 4 * sizeof(uint32_t)) {
23553fe401a5SEd Maste 		warnx(".gnu.hash section too small");
23563fe401a5SEd Maste 		return;
23573fe401a5SEd Maste 	}
23583fe401a5SEd Maste 	buf = data->d_buf;
23593fe401a5SEd Maste 	nbucket = buf[0];
23603fe401a5SEd Maste 	symndx = buf[1];
23613fe401a5SEd Maste 	maskwords = buf[2];
23623fe401a5SEd Maste 	shift2 = buf[3];
23633fe401a5SEd Maste 	buf += 4;
2364b6b6f9ccSEd Maste 	if (s->link >= ed->shnum) {
2365b6b6f9ccSEd Maste 		warnx("Malformed .gnu.hash section");
2366b6b6f9ccSEd Maste 		return;
2367b6b6f9ccSEd Maste 	}
23683fe401a5SEd Maste 	ds = &ed->sl[s->link];
2369656f49f8SEd Maste 	if (!get_ent_count(ds, &dynsymcount))
2370656f49f8SEd Maste 		return;
2371b6b6f9ccSEd Maste 	if (symndx >= (uint32_t)dynsymcount) {
2372b6b6f9ccSEd Maste 		warnx("Malformed .gnu.hash section");
2373b6b6f9ccSEd Maste 		return;
2374b6b6f9ccSEd Maste 	}
23753fe401a5SEd Maste 	nchain = dynsymcount - symndx;
23763fe401a5SEd Maste 	if (data->d_size != 4 * sizeof(uint32_t) + maskwords *
23773fe401a5SEd Maste 	    (ed->ec == ELFCLASS32 ? sizeof(uint32_t) : sizeof(uint64_t)) +
2378b6b6f9ccSEd Maste 	    ((uint64_t)nbucket + (uint64_t)nchain) * sizeof(uint32_t)) {
23793fe401a5SEd Maste 		warnx("Malformed .gnu.hash section");
23803fe401a5SEd Maste 		return;
23813fe401a5SEd Maste 	}
23823fe401a5SEd Maste 	bucket = buf + (ed->ec == ELFCLASS32 ? maskwords : maskwords * 2);
23833fe401a5SEd Maste 	chain = bucket + nbucket;
23843fe401a5SEd Maste 
23853fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT) {
23863fe401a5SEd Maste 		maxl = 0;
23873fe401a5SEd Maste 		if ((bl = calloc(nbucket, sizeof(*bl))) == NULL)
23883fe401a5SEd Maste 			err(EXIT_FAILURE, "calloc failed");
2389b6b6f9ccSEd Maste 		for (i = 0; i < nbucket; i++)
2390b6b6f9ccSEd Maste 			for (j = bucket[i]; j > 0 && j - symndx < nchain; j++) {
23913fe401a5SEd Maste 				if (++bl[i] > maxl)
23923fe401a5SEd Maste 					maxl = bl[i];
23933fe401a5SEd Maste 				if (chain[j - symndx] & 1)
23943fe401a5SEd Maste 					break;
23953fe401a5SEd Maste 			}
23963fe401a5SEd Maste 		if ((c = calloc(maxl + 1, sizeof(*c))) == NULL)
23973fe401a5SEd Maste 			err(EXIT_FAILURE, "calloc failed");
2398b6b6f9ccSEd Maste 		for (i = 0; i < nbucket; i++)
23993fe401a5SEd Maste 			c[bl[i]]++;
24003fe401a5SEd Maste 		PRT("    bucket    symndx    name\n");
2401b6b6f9ccSEd Maste 		for (i = 0; i < nbucket; i++) {
24023fe401a5SEd Maste 			first = 1;
2403b6b6f9ccSEd Maste 			for (j = bucket[i]; j > 0 && j - symndx < nchain; j++) {
24043fe401a5SEd Maste 				if (first) {
24053fe401a5SEd Maste 					PRT("%10d  ", i);
24063fe401a5SEd Maste 					first = 0;
24073fe401a5SEd Maste 				} else
24083fe401a5SEd Maste 					PRT("            ");
24093fe401a5SEd Maste 				snprintf(idx, sizeof(idx), "[%d]", j );
24103fe401a5SEd Maste 				PRT("%-10s  ", idx);
24113fe401a5SEd Maste 				PRT("%s\n", get_symbol_name(ed, s->link, j));
24123fe401a5SEd Maste 				if (chain[j - symndx] & 1)
24133fe401a5SEd Maste 					break;
24143fe401a5SEd Maste 			}
24153fe401a5SEd Maste 		}
24163fe401a5SEd Maste 		PRT("\n");
24173fe401a5SEd Maste 		total = 0;
2418b6b6f9ccSEd Maste 		for (i = 0; i <= maxl; i++) {
24193fe401a5SEd Maste 			total += c[i] * i;
24203fe401a5SEd Maste 			PRT("%10u  buckets contain %8d symbols\n", c[i], i);
24213fe401a5SEd Maste 		}
24223fe401a5SEd Maste 		PRT("%10u  buckets         %8u symbols (globals)\n", nbucket,
24233fe401a5SEd Maste 		    total);
24243fe401a5SEd Maste 	} else {
24253fe401a5SEd Maste 		PRT("\nnbucket: %u\n", nbucket);
24263fe401a5SEd Maste 		PRT("symndx: %u\n", symndx);
24273fe401a5SEd Maste 		PRT("maskwords: %u\n", maskwords);
24283fe401a5SEd Maste 		PRT("shift2: %u\n", shift2);
24293fe401a5SEd Maste 		PRT("nchain: %u\n\n", nchain);
2430b6b6f9ccSEd Maste 		for (i = 0; i < nbucket; i++)
24313fe401a5SEd Maste 			PRT("bucket[%d]:\n\t%u\n\n", i, bucket[i]);
2432b6b6f9ccSEd Maste 		for (i = 0; i < nchain; i++)
24333fe401a5SEd Maste 			PRT("chain[%d]:\n\t%u\n\n", i, chain[i]);
24343fe401a5SEd Maste 	}
24353fe401a5SEd Maste }
24363fe401a5SEd Maste 
24373fe401a5SEd Maste /*
24383fe401a5SEd Maste  * Dump hash tables.
24393fe401a5SEd Maste  */
24403fe401a5SEd Maste static void
elf_print_hash(struct elfdump * ed)24413fe401a5SEd Maste elf_print_hash(struct elfdump *ed)
24423fe401a5SEd Maste {
24433fe401a5SEd Maste 	struct section	*s;
2444b6b6f9ccSEd Maste 	size_t		 i;
24453fe401a5SEd Maste 
2446b6b6f9ccSEd Maste 	for (i = 0; i < ed->shnum; i++) {
24473fe401a5SEd Maste 		s = &ed->sl[i];
24483fe401a5SEd Maste 		if ((s->type == SHT_HASH || s->type == SHT_GNU_HASH) &&
24493fe401a5SEd Maste 		    (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name))) {
24503fe401a5SEd Maste 			if (s->type == SHT_GNU_HASH)
24513fe401a5SEd Maste 				elf_print_gnu_hash(ed, s);
24523fe401a5SEd Maste 			else if (ed->ehdr.e_machine == EM_ALPHA &&
24533fe401a5SEd Maste 			    s->entsize == 8)
24543fe401a5SEd Maste 				elf_print_svr4_hash64(ed, s);
24553fe401a5SEd Maste 			else
24563fe401a5SEd Maste 				elf_print_svr4_hash(ed, s);
24573fe401a5SEd Maste 		}
24583fe401a5SEd Maste 	}
24593fe401a5SEd Maste }
24603fe401a5SEd Maste 
24613fe401a5SEd Maste /*
24623fe401a5SEd Maste  * Dump the content of a Version Definition(SHT_SUNW_Verdef) Section.
24633fe401a5SEd Maste  */
24643fe401a5SEd Maste static void
elf_print_verdef(struct elfdump * ed,struct section * s)24653fe401a5SEd Maste elf_print_verdef(struct elfdump *ed, struct section *s)
24663fe401a5SEd Maste {
24673fe401a5SEd Maste 	Elf_Data	*data;
24683fe401a5SEd Maste 	Elf32_Verdef	*vd;
24693fe401a5SEd Maste 	Elf32_Verdaux	*vda;
24703fe401a5SEd Maste 	const char 	*str;
24713fe401a5SEd Maste 	char		 idx[10];
24723fe401a5SEd Maste 	uint8_t		*buf, *end, *buf2;
24733fe401a5SEd Maste 	int		 i, j, elferr, count;
24743fe401a5SEd Maste 
24753fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT)
24763fe401a5SEd Maste 		PRT("Version Definition Section:  %s\n", s->name);
24773fe401a5SEd Maste 	else
24783fe401a5SEd Maste 		PRT("\nversion definition section (%s):\n", s->name);
24793fe401a5SEd Maste 	(void) elf_errno();
24803fe401a5SEd Maste 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
24813fe401a5SEd Maste 		elferr = elf_errno();
24823fe401a5SEd Maste 		if (elferr != 0)
24833fe401a5SEd Maste 			warnx("elf_getdata failed: %s",
24843fe401a5SEd Maste 			    elf_errmsg(elferr));
24853fe401a5SEd Maste 		return;
24863fe401a5SEd Maste 	}
24873fe401a5SEd Maste 	buf = data->d_buf;
24883fe401a5SEd Maste 	end = buf + data->d_size;
24893fe401a5SEd Maste 	i = 0;
24903fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT)
24913fe401a5SEd Maste 		PRT("     index  version                     dependency\n");
24923fe401a5SEd Maste 	while (buf + sizeof(Elf32_Verdef) <= end) {
24933fe401a5SEd Maste 		vd = (Elf32_Verdef *) (uintptr_t) buf;
24943fe401a5SEd Maste 		if (ed->flags & SOLARIS_FMT) {
24953fe401a5SEd Maste 			snprintf(idx, sizeof(idx), "[%d]", vd->vd_ndx);
24963fe401a5SEd Maste 			PRT("%10s  ", idx);
24973fe401a5SEd Maste 		} else {
24983fe401a5SEd Maste 			PRT("\nentry: %d\n", i++);
24993fe401a5SEd Maste 			PRT("\tvd_version: %u\n", vd->vd_version);
25003fe401a5SEd Maste 			PRT("\tvd_flags: %u\n", vd->vd_flags);
25013fe401a5SEd Maste 			PRT("\tvd_ndx: %u\n", vd->vd_ndx);
25023fe401a5SEd Maste 			PRT("\tvd_cnt: %u\n", vd->vd_cnt);
25033fe401a5SEd Maste 			PRT("\tvd_hash: %u\n", vd->vd_hash);
25043fe401a5SEd Maste 			PRT("\tvd_aux: %u\n", vd->vd_aux);
25053fe401a5SEd Maste 			PRT("\tvd_next: %u\n\n", vd->vd_next);
25063fe401a5SEd Maste 		}
25073fe401a5SEd Maste 		buf2 = buf + vd->vd_aux;
25083fe401a5SEd Maste 		j = 0;
25093fe401a5SEd Maste 		count = 0;
25103fe401a5SEd Maste 		while (buf2 + sizeof(Elf32_Verdaux) <= end && j < vd->vd_cnt) {
25113fe401a5SEd Maste 			vda = (Elf32_Verdaux *) (uintptr_t) buf2;
25123fe401a5SEd Maste 			str = get_string(ed, s->link, vda->vda_name);
25133fe401a5SEd Maste 			if (ed->flags & SOLARIS_FMT) {
25143fe401a5SEd Maste 				if (count == 0)
25153fe401a5SEd Maste 					PRT("%-26.26s", str);
25163fe401a5SEd Maste 				else if (count == 1)
25173fe401a5SEd Maste 					PRT("  %-20.20s", str);
25183fe401a5SEd Maste 				else {
25193fe401a5SEd Maste 					PRT("\n%40.40s", "");
25203fe401a5SEd Maste 					PRT("%s", str);
25213fe401a5SEd Maste 				}
25223fe401a5SEd Maste 			} else {
25233fe401a5SEd Maste 				PRT("\t\tvda: %d\n", j++);
25243fe401a5SEd Maste 				PRT("\t\t\tvda_name: %s\n", str);
25253fe401a5SEd Maste 				PRT("\t\t\tvda_next: %u\n", vda->vda_next);
25263fe401a5SEd Maste 			}
25273fe401a5SEd Maste 			if (vda->vda_next == 0) {
25283fe401a5SEd Maste 				if (ed->flags & SOLARIS_FMT) {
25293fe401a5SEd Maste 					if (vd->vd_flags & VER_FLG_BASE) {
25303fe401a5SEd Maste 						if (count == 0)
25313fe401a5SEd Maste 							PRT("%-20.20s", "");
25323fe401a5SEd Maste 						PRT("%s", "[ BASE ]");
25333fe401a5SEd Maste 					}
25343fe401a5SEd Maste 					PRT("\n");
25353fe401a5SEd Maste 				}
25363fe401a5SEd Maste 				break;
25373fe401a5SEd Maste 			}
25383fe401a5SEd Maste 			if (ed->flags & SOLARIS_FMT)
25393fe401a5SEd Maste 				count++;
25403fe401a5SEd Maste 			buf2 += vda->vda_next;
25413fe401a5SEd Maste 		}
25423fe401a5SEd Maste 		if (vd->vd_next == 0)
25433fe401a5SEd Maste 			break;
25443fe401a5SEd Maste 		buf += vd->vd_next;
25453fe401a5SEd Maste 	}
25463fe401a5SEd Maste }
25473fe401a5SEd Maste 
25483fe401a5SEd Maste /*
25493fe401a5SEd Maste  * Dump the content of a Version Needed(SHT_SUNW_Verneed) Section.
25503fe401a5SEd Maste  */
25513fe401a5SEd Maste static void
elf_print_verneed(struct elfdump * ed,struct section * s)25523fe401a5SEd Maste elf_print_verneed(struct elfdump *ed, struct section *s)
25533fe401a5SEd Maste {
25543fe401a5SEd Maste 	Elf_Data	*data;
25553fe401a5SEd Maste 	Elf32_Verneed	*vn;
25563fe401a5SEd Maste 	Elf32_Vernaux	*vna;
25573fe401a5SEd Maste 	uint8_t		*buf, *end, *buf2;
25583fe401a5SEd Maste 	int		 i, j, elferr, first;
25593fe401a5SEd Maste 
25603fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT)
25613fe401a5SEd Maste 		PRT("\nVersion Needed Section:  %s\n", s->name);
25623fe401a5SEd Maste 	else
25633fe401a5SEd Maste 		PRT("\nversion need section (%s):\n", s->name);
25643fe401a5SEd Maste 	(void) elf_errno();
25653fe401a5SEd Maste 	if ((data = elf_getdata(s->scn, NULL)) == NULL) {
25663fe401a5SEd Maste 		elferr = elf_errno();
25673fe401a5SEd Maste 		if (elferr != 0)
25683fe401a5SEd Maste 			warnx("elf_getdata failed: %s",
25693fe401a5SEd Maste 			    elf_errmsg(elferr));
25703fe401a5SEd Maste 		return;
25713fe401a5SEd Maste 	}
25723fe401a5SEd Maste 	buf = data->d_buf;
25733fe401a5SEd Maste 	end = buf + data->d_size;
25743fe401a5SEd Maste 	if (ed->flags & SOLARIS_FMT)
25753fe401a5SEd Maste 		PRT("            file                        version\n");
25763fe401a5SEd Maste 	i = 0;
25773fe401a5SEd Maste 	while (buf + sizeof(Elf32_Verneed) <= end) {
25783fe401a5SEd Maste 		vn = (Elf32_Verneed *) (uintptr_t) buf;
25793fe401a5SEd Maste 		if (ed->flags & SOLARIS_FMT)
25803fe401a5SEd Maste 			PRT("            %-26.26s  ",
25813fe401a5SEd Maste 			    get_string(ed, s->link, vn->vn_file));
25823fe401a5SEd Maste 		else {
25833fe401a5SEd Maste 			PRT("\nentry: %d\n", i++);
25843fe401a5SEd Maste 			PRT("\tvn_version: %u\n", vn->vn_version);
25853fe401a5SEd Maste 			PRT("\tvn_cnt: %u\n", vn->vn_cnt);
25863fe401a5SEd Maste 			PRT("\tvn_file: %s\n",
25873fe401a5SEd Maste 			    get_string(ed, s->link, vn->vn_file));
25883fe401a5SEd Maste 			PRT("\tvn_aux: %u\n", vn->vn_aux);
25893fe401a5SEd Maste 			PRT("\tvn_next: %u\n\n", vn->vn_next);
25903fe401a5SEd Maste 		}
25913fe401a5SEd Maste 		buf2 = buf + vn->vn_aux;
25923fe401a5SEd Maste 		j = 0;
25933fe401a5SEd Maste 		first = 1;
25943fe401a5SEd Maste 		while (buf2 + sizeof(Elf32_Vernaux) <= end && j < vn->vn_cnt) {
25953fe401a5SEd Maste 			vna = (Elf32_Vernaux *) (uintptr_t) buf2;
25963fe401a5SEd Maste 			if (ed->flags & SOLARIS_FMT) {
25973fe401a5SEd Maste 				if (!first)
25983fe401a5SEd Maste 					PRT("%40.40s", "");
25993fe401a5SEd Maste 				else
26003fe401a5SEd Maste 					first = 0;
26013fe401a5SEd Maste 				PRT("%s\n", get_string(ed, s->link,
26023fe401a5SEd Maste 				    vna->vna_name));
26033fe401a5SEd Maste 			} else {
26043fe401a5SEd Maste 				PRT("\t\tvna: %d\n", j++);
26053fe401a5SEd Maste 				PRT("\t\t\tvna_hash: %u\n", vna->vna_hash);
26063fe401a5SEd Maste 				PRT("\t\t\tvna_flags: %u\n", vna->vna_flags);
26073fe401a5SEd Maste 				PRT("\t\t\tvna_other: %u\n", vna->vna_other);
26083fe401a5SEd Maste 				PRT("\t\t\tvna_name: %s\n",
26093fe401a5SEd Maste 				    get_string(ed, s->link, vna->vna_name));
26103fe401a5SEd Maste 				PRT("\t\t\tvna_next: %u\n", vna->vna_next);
26113fe401a5SEd Maste 			}
26123fe401a5SEd Maste 			if (vna->vna_next == 0)
26133fe401a5SEd Maste 				break;
26143fe401a5SEd Maste 			buf2 += vna->vna_next;
26153fe401a5SEd Maste 		}
26163fe401a5SEd Maste 		if (vn->vn_next == 0)
26173fe401a5SEd Maste 			break;
26183fe401a5SEd Maste 		buf += vn->vn_next;
26193fe401a5SEd Maste 	}
26203fe401a5SEd Maste }
26213fe401a5SEd Maste 
26223fe401a5SEd Maste /*
26233fe401a5SEd Maste  * Dump the symbol-versioning sections.
26243fe401a5SEd Maste  */
26253fe401a5SEd Maste static void
elf_print_symver(struct elfdump * ed)26263fe401a5SEd Maste elf_print_symver(struct elfdump *ed)
26273fe401a5SEd Maste {
26283fe401a5SEd Maste 	struct section	*s;
2629b6b6f9ccSEd Maste 	size_t		 i;
26303fe401a5SEd Maste 
2631b6b6f9ccSEd Maste 	for (i = 0; i < ed->shnum; i++) {
26323fe401a5SEd Maste 		s = &ed->sl[i];
26333fe401a5SEd Maste 		if (!STAILQ_EMPTY(&ed->snl) && !find_name(ed, s->name))
26343fe401a5SEd Maste 			continue;
26353fe401a5SEd Maste 		if (s->type == SHT_SUNW_verdef)
26363fe401a5SEd Maste 			elf_print_verdef(ed, s);
26373fe401a5SEd Maste 		if (s->type == SHT_SUNW_verneed)
26383fe401a5SEd Maste 			elf_print_verneed(ed, s);
26393fe401a5SEd Maste 	}
26403fe401a5SEd Maste }
26413fe401a5SEd Maste 
26423fe401a5SEd Maste /*
26433fe401a5SEd Maste  * Dump the ELF checksum. See gelf_checksum(3) for details.
26443fe401a5SEd Maste  */
26453fe401a5SEd Maste static void
elf_print_checksum(struct elfdump * ed)26463fe401a5SEd Maste elf_print_checksum(struct elfdump *ed)
26473fe401a5SEd Maste {
26483fe401a5SEd Maste 
26493fe401a5SEd Maste 	if (!STAILQ_EMPTY(&ed->snl))
26503fe401a5SEd Maste 		return;
26513fe401a5SEd Maste 
26523fe401a5SEd Maste 	PRT("\nelf checksum: %#lx\n", gelf_checksum(ed->elf));
26533fe401a5SEd Maste }
26543fe401a5SEd Maste 
26553fe401a5SEd Maste #define	USAGE_MESSAGE	"\
26563fe401a5SEd Maste Usage: %s [options] file...\n\
26573fe401a5SEd Maste   Display information about ELF objects and ar(1) archives.\n\n\
26583fe401a5SEd Maste   Options:\n\
26593fe401a5SEd Maste   -a                        Show all information.\n\
26603fe401a5SEd Maste   -c                        Show shared headers.\n\
26613fe401a5SEd Maste   -d                        Show dynamic symbols.\n\
26623fe401a5SEd Maste   -e                        Show the ELF header.\n\
26633fe401a5SEd Maste   -G                        Show the GOT.\n\
26643fe401a5SEd Maste   -H | --help               Show a usage message and exit.\n\
26653fe401a5SEd Maste   -h                        Show hash values.\n\
26663fe401a5SEd Maste   -i                        Show the dynamic interpreter.\n\
26673fe401a5SEd Maste   -k                        Show the ELF checksum.\n\
26683fe401a5SEd Maste   -n                        Show the contents of note sections.\n\
26693fe401a5SEd Maste   -N NAME                   Show the section named \"NAME\".\n\
26703fe401a5SEd Maste   -p                        Show the program header.\n\
26713fe401a5SEd Maste   -r                        Show relocations.\n\
26723fe401a5SEd Maste   -s                        Show the symbol table.\n\
26733fe401a5SEd Maste   -S                        Use the Solaris elfdump format.\n\
26743fe401a5SEd Maste   -v                        Show symbol-versioning information.\n\
26753fe401a5SEd Maste   -V | --version            Print a version identifier and exit.\n\
26763fe401a5SEd Maste   -w FILE                   Write output to \"FILE\".\n"
26773fe401a5SEd Maste 
26783fe401a5SEd Maste static void
usage(void)26793fe401a5SEd Maste usage(void)
26803fe401a5SEd Maste {
26813fe401a5SEd Maste 	fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());
26823fe401a5SEd Maste 	exit(EXIT_FAILURE);
26833fe401a5SEd Maste }
2684