xref: /onnv-gate/usr/src/cmd/sgs/dump/common/dump.c (revision 4734)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51618Srie  * Common Development and Distribution License (the "License").
61618Srie  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211618Srie 
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
240Sstevel@tonic-gate  *	  All Rights Reserved
250Sstevel@tonic-gate  *
263492Sab196087  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
270Sstevel@tonic-gate  * Use is subject to license terms.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
321976Sab196087 /* Get definitions for the relocation types supported. */
331976Sab196087 #define	ELF_TARGET_ALL
341976Sab196087 
350Sstevel@tonic-gate #include <stdio.h>
360Sstevel@tonic-gate #include <stdlib.h>
370Sstevel@tonic-gate #include <locale.h>
380Sstevel@tonic-gate #include <unistd.h>
390Sstevel@tonic-gate #include <libelf.h>
400Sstevel@tonic-gate #include <link.h>
411976Sab196087 #include <sys/elf.h>
420Sstevel@tonic-gate #include <sys/machelf.h>
430Sstevel@tonic-gate #include <fcntl.h>
440Sstevel@tonic-gate #include <sys/stat.h>
450Sstevel@tonic-gate #include <errno.h>
460Sstevel@tonic-gate #include <string.h>
470Sstevel@tonic-gate #include "sgs.h"
480Sstevel@tonic-gate #include "conv.h"
490Sstevel@tonic-gate #include "dump.h"
500Sstevel@tonic-gate 
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #define	OPTSTR	"agcd:fhn:oprstvCLT:V?"		/* option string for getopt() */
530Sstevel@tonic-gate 
541976Sab196087 /*
551976Sab196087  * DUMP_CONVFMT defines the libconv formatting options we want to use:
561976Sab196087  *	- Unknown items to be printed as integers using decimal formatting
571976Sab196087  *	- The "Dump Style" versions of strings.
581976Sab196087  */
591976Sab196087 #define	DUMP_CONVFMT (CONV_FMT_DECIMAL|CONV_FMT_ALTDUMP)
601976Sab196087 
610Sstevel@tonic-gate const char *UNKNOWN = "<unknown>";
620Sstevel@tonic-gate 
630Sstevel@tonic-gate static SCNTAB *p_symtab, *p_head_scns, *p_dynsym;
640Sstevel@tonic-gate 
650Sstevel@tonic-gate static int
660Sstevel@tonic-gate 	x_flag = 0,	/* option requires section header table */
670Sstevel@tonic-gate 	z_flag = 0,	/* process files within an archive */
680Sstevel@tonic-gate 	rn_flag = 0;	/* dump named relocation information */
690Sstevel@tonic-gate 
700Sstevel@tonic-gate static int
710Sstevel@tonic-gate 	/* flags: ?_flag corresponds to ? option */
720Sstevel@tonic-gate 	a_flag = 0,	/* dump archive header of each member of archive */
730Sstevel@tonic-gate 	g_flag = 0,	/* dump archive symbol table */
740Sstevel@tonic-gate 	c_flag = 0,	/* dump the string table */
750Sstevel@tonic-gate 	d_flag = 0,	/* dump range of sections */
760Sstevel@tonic-gate 	f_flag = 0,	/* dump each file header */
770Sstevel@tonic-gate 	h_flag = 0,	/* dump section headers */
780Sstevel@tonic-gate 	n_flag = 0,	/* dump named section */
790Sstevel@tonic-gate 	o_flag = 0,	/* dump each program execution header */
800Sstevel@tonic-gate 	r_flag = 0,	/* dump relocation information */
810Sstevel@tonic-gate 	s_flag = 0,	/* dump section contents */
820Sstevel@tonic-gate 	t_flag = 0,	/* dump symbol table entries */
830Sstevel@tonic-gate 	C_flag = 0,	/* dump decoded C++ symbol names */
840Sstevel@tonic-gate 	L_flag = 0,	/* dump dynamic linking information */
850Sstevel@tonic-gate 	T_flag = 0,	/* dump symbol table range */
860Sstevel@tonic-gate 	V_flag = 0;	/* dump version information */
870Sstevel@tonic-gate 
880Sstevel@tonic-gate int	p_flag = 0,	/* suppress printing of headings */
890Sstevel@tonic-gate 	v_flag = 0;	/* print information in verbose form */
900Sstevel@tonic-gate 
910Sstevel@tonic-gate static int
920Sstevel@tonic-gate 	d_low = 0,	/* range for use with -d */
930Sstevel@tonic-gate 	d_hi = 0,
940Sstevel@tonic-gate 	d_num = 0;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate static int
970Sstevel@tonic-gate 	T_low = 0,	/* range for use with -T */
980Sstevel@tonic-gate 	T_hi = 0,
990Sstevel@tonic-gate 	T_num = 0;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate static char *name = NULL; /* for use with -n option */
1020Sstevel@tonic-gate char *prog_name;
1030Sstevel@tonic-gate static int errflag = 0;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate static struct stab_list_s {
1060Sstevel@tonic-gate 	struct stab_list_s *next;
1070Sstevel@tonic-gate 	char *strings;
1080Sstevel@tonic-gate 	size_t size;
1090Sstevel@tonic-gate } *StringTableList = (void *)0;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate extern void ar_sym_read();
1120Sstevel@tonic-gate extern void dump_exec_header();
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate /*
1160Sstevel@tonic-gate  * Get the section descriptor and set the size of the
1170Sstevel@tonic-gate  * data returned.  Data is byte-order converted.
1180Sstevel@tonic-gate  */
1190Sstevel@tonic-gate void *
1200Sstevel@tonic-gate get_scndata(Elf_Scn *fd_scn, size_t *size)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate 	Elf_Data *p_data;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	p_data = 0;
1250Sstevel@tonic-gate 	if ((p_data = elf_getdata(fd_scn, p_data)) == 0 ||
1260Sstevel@tonic-gate 	    p_data->d_size == 0) {
1270Sstevel@tonic-gate 		return (NULL);
1280Sstevel@tonic-gate 	}
1290Sstevel@tonic-gate 	*size = p_data->d_size;
1300Sstevel@tonic-gate 	return (p_data->d_buf);
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate /*
1340Sstevel@tonic-gate  * Get the section descriptor and set the size of the
1350Sstevel@tonic-gate  * data returned.  Data is raw (i.e., not byte-order converted).
1360Sstevel@tonic-gate  */
1370Sstevel@tonic-gate static void *
1380Sstevel@tonic-gate get_rawscn(Elf_Scn *fd_scn, size_t *size)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate 	Elf_Data *p_data;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	p_data = 0;
1430Sstevel@tonic-gate 	if ((p_data = elf_rawdata(fd_scn, p_data)) == 0 ||
1440Sstevel@tonic-gate 	    p_data->d_size == 0) {
1450Sstevel@tonic-gate 		return (NULL);
1460Sstevel@tonic-gate 	}
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	*size = p_data->d_size;
1490Sstevel@tonic-gate 	return (p_data->d_buf);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate /*
1530Sstevel@tonic-gate  * Print out a usage message in short form when program is invoked
1540Sstevel@tonic-gate  * with insufficient or no arguments, and in long form when given
1550Sstevel@tonic-gate  * either a ? or an invalid option.
1560Sstevel@tonic-gate  */
1570Sstevel@tonic-gate static void
1580Sstevel@tonic-gate usage()
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate 	(void) fprintf(stderr,
1610Sstevel@tonic-gate 	"Usage: %s [-%s] file(s) ...\n", prog_name, OPTSTR);
1620Sstevel@tonic-gate 	if (errflag) {
1630Sstevel@tonic-gate 		(void) fprintf(stderr,
1640Sstevel@tonic-gate 		"\t\t[-a dump archive header of each member of archive]\n\
1650Sstevel@tonic-gate 		[-g dump archive global symbol table]\n\
1660Sstevel@tonic-gate 		[-c dump the string table]\n\
1670Sstevel@tonic-gate 		[-d dump range of sections]\n\
1680Sstevel@tonic-gate 		[-f dump each file header]\n\
1690Sstevel@tonic-gate 		[-h dump section headers]\n\
1700Sstevel@tonic-gate 		[-n dump named section]\n\
1710Sstevel@tonic-gate 		[-o dump each program execution header]\n\
1720Sstevel@tonic-gate 		[-p suppress printing of headings]\n\
1730Sstevel@tonic-gate 		[-r dump relocation information]\n\
1740Sstevel@tonic-gate 		[-s dump section contents]\n\
1750Sstevel@tonic-gate 		[-t dump symbol table entries]\n\
1760Sstevel@tonic-gate 		[-v print information in verbose form]\n\
1770Sstevel@tonic-gate 		[-C dump decoded C++ symbol names]\n\
1780Sstevel@tonic-gate 		[-L dump the .dynamic structure]\n\
1790Sstevel@tonic-gate 		[-T dump symbol table range]\n\
1800Sstevel@tonic-gate 		[-V dump version information]\n");
1810Sstevel@tonic-gate 	}
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate /*
1850Sstevel@tonic-gate  * Set a range.  Input is a character string, a lower
1860Sstevel@tonic-gate  * bound and an upper bound.  This function converts
1870Sstevel@tonic-gate  * a character string into its correct integer values,
1880Sstevel@tonic-gate  * setting the first value as the lower bound, and
1890Sstevel@tonic-gate  * the second value as the upper bound.  If more values
1900Sstevel@tonic-gate  * are given they are ignored with a warning.
1910Sstevel@tonic-gate  */
1920Sstevel@tonic-gate static void
1930Sstevel@tonic-gate set_range(char *s, int  *low, int  *high)
1940Sstevel@tonic-gate {
1950Sstevel@tonic-gate 	char *w;
1960Sstevel@tonic-gate 	char *lasts;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	while ((w = strtok_r(s, ",", &lasts)) != NULL) {
1990Sstevel@tonic-gate 		if (!(*low))
2000Sstevel@tonic-gate 			/* LINTED */
2010Sstevel@tonic-gate 			*low = (int)atol(w);
2020Sstevel@tonic-gate 		else
2030Sstevel@tonic-gate 			if (!(*high))
2040Sstevel@tonic-gate 				/* LINTED */
2050Sstevel@tonic-gate 				*high = (int)atol(w);
2060Sstevel@tonic-gate 			else {
2070Sstevel@tonic-gate 				(void) fprintf(stderr,
208*4734Sab196087 				    "%s: too many arguments - %s ignored\n",
209*4734Sab196087 				    prog_name, w);
2100Sstevel@tonic-gate 				return;
2110Sstevel@tonic-gate 			}
2120Sstevel@tonic-gate 		s = NULL;
2130Sstevel@tonic-gate 	} /* end while */
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate /*
2180Sstevel@tonic-gate  * Print static shared library information.
2190Sstevel@tonic-gate  */
2200Sstevel@tonic-gate static void
2210Sstevel@tonic-gate print_static(SCNTAB *l_scns, char *filename)
2220Sstevel@tonic-gate {
2230Sstevel@tonic-gate 	size_t section_size;
2240Sstevel@tonic-gate 	unsigned char *strtab;
2250Sstevel@tonic-gate 	unsigned char *path, buf[1024];
2260Sstevel@tonic-gate 	unsigned long *temp;
2270Sstevel@tonic-gate 	unsigned long total, topath;
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	(void) printf("\n  **** STATIC SHARED LIBRARY INFORMATION ****\n");
2300Sstevel@tonic-gate 	(void) printf("\n%s:\n", filename);
2310Sstevel@tonic-gate 	(void) printf("\t");
2320Sstevel@tonic-gate 	section_size  = 0;
2330Sstevel@tonic-gate 	if ((strtab = (unsigned char *)
2340Sstevel@tonic-gate 	    get_scndata(l_scns->p_sd, &section_size)) == NULL) {
2350Sstevel@tonic-gate 		return;
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	while (section_size != 0) {
2390Sstevel@tonic-gate 		/* LINTED */
2400Sstevel@tonic-gate 		temp = (unsigned long *)strtab;
2410Sstevel@tonic-gate 		total = temp[0];
2420Sstevel@tonic-gate 		topath = temp[1];
2430Sstevel@tonic-gate 		path = strtab + (topath*sizeof (long));
2440Sstevel@tonic-gate 		(void) strncpy((char *)buf, (char *)path,
245*4734Sab196087 		    (total - topath)*sizeof (long));
2460Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\n", buf);
2470Sstevel@tonic-gate 		strtab += total*sizeof (long);
2480Sstevel@tonic-gate 		section_size -= (total*sizeof (long));
2490Sstevel@tonic-gate 	}
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate /*
2530Sstevel@tonic-gate  * Print raw data in hexidecimal.  Input is the section data to
2540Sstevel@tonic-gate  * be printed out and the size of the data.  Output is relative
2550Sstevel@tonic-gate  * to a table lookup in dumpmap.h.
2560Sstevel@tonic-gate  */
2570Sstevel@tonic-gate static void
2580Sstevel@tonic-gate print_rawdata(unsigned char *p_sec, size_t size)
2590Sstevel@tonic-gate {
2600Sstevel@tonic-gate 	size_t   j;
2610Sstevel@tonic-gate 	size_t   count;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	count = 1;
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	(void) printf("\t");
2660Sstevel@tonic-gate 	for (j = size/sizeof (short); j != 0; --j, ++count) {
2670Sstevel@tonic-gate 		(void) printf("%.2x %.2x ", p_sec[0], p_sec[1]);
2680Sstevel@tonic-gate 		p_sec += 2;
2690Sstevel@tonic-gate 		if (count == 12) {
2700Sstevel@tonic-gate 			(void) printf("\n\t");
2710Sstevel@tonic-gate 			count = 0;
2720Sstevel@tonic-gate 		}
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	/*
2760Sstevel@tonic-gate 	 * take care of last byte if odd byte section
2770Sstevel@tonic-gate 	 */
2780Sstevel@tonic-gate 	if ((size & 0x1L) == 1L)
2790Sstevel@tonic-gate 		(void) printf("%.2x", *p_sec);
2800Sstevel@tonic-gate 	(void) printf("\n");
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate /*
2860Sstevel@tonic-gate  * Print relocation data of type SHT_RELA
2870Sstevel@tonic-gate  * If d_flag, print data corresponding only to
2880Sstevel@tonic-gate  * the section or range of sections specified.
2890Sstevel@tonic-gate  * If n_flag, print data corresponding only to
2900Sstevel@tonic-gate  * the named section.
2910Sstevel@tonic-gate  */
2920Sstevel@tonic-gate static void
2930Sstevel@tonic-gate print_rela(Elf *elf_file, SCNTAB *p_scns, Elf_Data *rdata, Elf_Data *sym_data,
2940Sstevel@tonic-gate 	GElf_Ehdr * p_ehdr, size_t reloc_size, size_t sym_size, char *filename,
2950Sstevel@tonic-gate 	SCNTAB *reloc_symtab)
2960Sstevel@tonic-gate {
2970Sstevel@tonic-gate 	GElf_Rela rela;
2980Sstevel@tonic-gate 	GElf_Sym sym;
2990Sstevel@tonic-gate 	size_t no_entries;
3000Sstevel@tonic-gate 	size_t rel_entsize;
3010Sstevel@tonic-gate 	size_t no_syms;
3020Sstevel@tonic-gate 	int type, symid;
3030Sstevel@tonic-gate 	static int n_title = 0;
3040Sstevel@tonic-gate 	int ndx = 0;
3050Sstevel@tonic-gate 	char *sym_name;
3060Sstevel@tonic-gate 	int adj = 0;
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	if (gelf_getclass(elf_file) == ELFCLASS64)
3091618Srie 		adj = 8;
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	rel_entsize = p_scns->p_shdr.sh_entsize;
3120Sstevel@tonic-gate 	if ((rel_entsize == 0) ||
3130Sstevel@tonic-gate 	    (rel_entsize > p_scns->p_shdr.sh_size)) {
3140Sstevel@tonic-gate 		rel_entsize = gelf_fsize(elf_file, ELF_T_RELA, 1,
3150Sstevel@tonic-gate 		    EV_CURRENT);
3160Sstevel@tonic-gate 	}
3170Sstevel@tonic-gate 	no_entries = reloc_size / rel_entsize;
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	no_syms = sym_size / gelf_fsize(elf_file, ELF_T_SYM, 1, EV_CURRENT);
3200Sstevel@tonic-gate 	while (no_entries--) {
3210Sstevel@tonic-gate 		(void) gelf_getrela(rdata, ndx, &rela);
3220Sstevel@tonic-gate 		/* LINTED */
3230Sstevel@tonic-gate 		type = (int)GELF_R_TYPE(rela.r_info);
3240Sstevel@tonic-gate 		/* LINTED */
3250Sstevel@tonic-gate 		symid = (int)GELF_R_SYM(rela.r_info);
3260Sstevel@tonic-gate 		/* LINTED */
3270Sstevel@tonic-gate 		if ((symid > (no_syms - 1)) || (symid < 0)) {
3280Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s: invalid symbol table "
3290Sstevel@tonic-gate 			    "offset - %d - in %s\n", prog_name, filename,
3300Sstevel@tonic-gate 			    symid, p_scns->scn_name);
3310Sstevel@tonic-gate 			ndx++;
3320Sstevel@tonic-gate 			continue;
3330Sstevel@tonic-gate 		}
3340Sstevel@tonic-gate 		(void) gelf_getsym(sym_data, symid, &sym);
3350Sstevel@tonic-gate 		sym_name = (char *)elf_strptr(elf_file,
336*4734Sab196087 		    reloc_symtab->p_shdr.sh_link, sym.st_name);
3370Sstevel@tonic-gate 		if (sym_name == NULL)
3380Sstevel@tonic-gate 			sym_name = (char *)UNKNOWN;
3390Sstevel@tonic-gate 		if (r_flag && rn_flag) {
3400Sstevel@tonic-gate 			if (strcmp(name, p_scns->scn_name) != 0) {
3410Sstevel@tonic-gate 				ndx++;
3420Sstevel@tonic-gate 				continue;
3430Sstevel@tonic-gate 			}
3440Sstevel@tonic-gate 			if (!n_title) {
3450Sstevel@tonic-gate 				(void) printf("\n%s:\n", p_scns->scn_name);
3460Sstevel@tonic-gate 				(void) printf("%-*s%-*s%-*s%s\n\n",
3470Sstevel@tonic-gate 				    12 + adj, "Offset", 22, "Symndx",
3480Sstevel@tonic-gate 				    16, "Type", "Addend");
3490Sstevel@tonic-gate 				n_title = 1;
3500Sstevel@tonic-gate 			}
3510Sstevel@tonic-gate 		}
3520Sstevel@tonic-gate 		if (d_flag) {
3530Sstevel@tonic-gate 			if (!d_hi)
3540Sstevel@tonic-gate 				d_hi = d_low;
3550Sstevel@tonic-gate 			if ((symid < d_low) || (symid > d_hi)) {
3560Sstevel@tonic-gate 				ndx++;
3570Sstevel@tonic-gate 				continue;
3580Sstevel@tonic-gate 			}
3590Sstevel@tonic-gate 		}
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 		(void) printf("%-#*llx", 12 + adj, EC_XWORD(rela.r_offset));
3620Sstevel@tonic-gate 		if (!v_flag) {
3630Sstevel@tonic-gate 			(void) printf("%-22d%-18d", symid, type);
3640Sstevel@tonic-gate 		} else {
365*4734Sab196087 			Conv_inv_buf_t	inv_buf;
366*4734Sab196087 
3670Sstevel@tonic-gate 			if (strlen(sym_name)) {
3680Sstevel@tonic-gate 				size_t len = strlen(sym_name) + 1;
3690Sstevel@tonic-gate 				char tmpstr[10];
3700Sstevel@tonic-gate 				if (len > 22) {
3710Sstevel@tonic-gate 					(void) sprintf(tmpstr, "%%-%ds",
372*4734Sab196087 					    /* LINTED */
373*4734Sab196087 					    (int)len);
3742352Sab196087 					/*LINTED: E_SEC_PRINTF_VAR_FMT*/
3750Sstevel@tonic-gate 					(void) printf(tmpstr, sym_name);
3760Sstevel@tonic-gate 				} else
3770Sstevel@tonic-gate 					(void) printf("%-22s", sym_name);
3781976Sab196087 			} else {
3790Sstevel@tonic-gate 				(void) printf("%-22d", symid);
3801976Sab196087 			}
3811976Sab196087 			(void) printf("%-20s",
382*4734Sab196087 			    conv_reloc_type(p_ehdr->e_machine,
383*4734Sab196087 			    type, DUMP_CONVFMT, &inv_buf));
3840Sstevel@tonic-gate 		}
3850Sstevel@tonic-gate 		(void) printf("%lld\n", EC_SXWORD(rela.r_addend));
3860Sstevel@tonic-gate 		ndx++;
3870Sstevel@tonic-gate 	}
3880Sstevel@tonic-gate }
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate /*
3910Sstevel@tonic-gate  * Print relocation data of type SHT_REL.
3920Sstevel@tonic-gate  * If d_flag, print data corresponding only to
3930Sstevel@tonic-gate  * the section or range of sections specified.
3940Sstevel@tonic-gate  * If n_flag, print data corresponding only to
3950Sstevel@tonic-gate  * the named section.
3960Sstevel@tonic-gate  */
3970Sstevel@tonic-gate static void
3980Sstevel@tonic-gate print_rel(Elf *elf_file, SCNTAB *p_scns, Elf_Data *rdata, Elf_Data *sym_data,
3990Sstevel@tonic-gate 	GElf_Ehdr *p_ehdr, size_t reloc_size, size_t sym_size, char *filename,
4000Sstevel@tonic-gate 	SCNTAB *reloc_symtab)
4010Sstevel@tonic-gate {
4020Sstevel@tonic-gate 	GElf_Rel rel;
4030Sstevel@tonic-gate 	GElf_Sym sym;
4040Sstevel@tonic-gate 	size_t no_entries;
4050Sstevel@tonic-gate 	size_t rel_entsize;
4060Sstevel@tonic-gate 	int type, symid;
4070Sstevel@tonic-gate 	size_t no_syms;
4080Sstevel@tonic-gate 	static int n_title = 0;
4090Sstevel@tonic-gate 	int ndx = 0;
4100Sstevel@tonic-gate 	char *sym_name;
4110Sstevel@tonic-gate 	int adj = 0;
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	if (gelf_getclass(elf_file) == ELFCLASS64)
4141618Srie 		adj = 8;
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	rel_entsize = p_scns->p_shdr.sh_entsize;
4170Sstevel@tonic-gate 	if ((rel_entsize == 0) ||
4180Sstevel@tonic-gate 	    (rel_entsize > p_scns->p_shdr.sh_size)) {
4190Sstevel@tonic-gate 		rel_entsize = gelf_fsize(elf_file, ELF_T_REL, 1,
4200Sstevel@tonic-gate 		    EV_CURRENT);
4210Sstevel@tonic-gate 	}
4220Sstevel@tonic-gate 	no_entries = reloc_size / rel_entsize;
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	no_syms = sym_size / gelf_fsize(elf_file, ELF_T_SYM, 1, EV_CURRENT);
4250Sstevel@tonic-gate 	while (no_entries--) {
4260Sstevel@tonic-gate 		(void) gelf_getrel(rdata, ndx, &rel);
4270Sstevel@tonic-gate 		/* LINTED */
4280Sstevel@tonic-gate 		type = (int)GELF_R_TYPE(rel.r_info);
4290Sstevel@tonic-gate 		/* LINTED */
4300Sstevel@tonic-gate 		symid = (int)GELF_R_SYM(rel.r_info);
4310Sstevel@tonic-gate 		/* LINTED */
4320Sstevel@tonic-gate 		if ((symid > (no_syms - 1)) || (symid < 0)) {
4330Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s: invalid symbol table "
4340Sstevel@tonic-gate 			    "offset - %d - in %s\n", prog_name, filename,
4350Sstevel@tonic-gate 			    symid, p_scns->scn_name);
4360Sstevel@tonic-gate 			ndx++;
4370Sstevel@tonic-gate 			continue;
4380Sstevel@tonic-gate 		}
4390Sstevel@tonic-gate 		(void) gelf_getsym(sym_data, symid, &sym);
4400Sstevel@tonic-gate 		sym_name = (char *)elf_strptr(elf_file,
441*4734Sab196087 		    reloc_symtab->p_shdr.sh_link, sym.st_name);
4420Sstevel@tonic-gate 		if (sym_name == NULL)
4430Sstevel@tonic-gate 			sym_name = (char *)UNKNOWN;
4440Sstevel@tonic-gate 		if (r_flag && rn_flag) {
4450Sstevel@tonic-gate 			if (strcmp(name, p_scns->scn_name) != 0) {
4460Sstevel@tonic-gate 				ndx++;
4470Sstevel@tonic-gate 				continue;
4480Sstevel@tonic-gate 			}
4490Sstevel@tonic-gate 			if (!n_title) {
4500Sstevel@tonic-gate 				(void) printf("\n%s:\n", p_scns->scn_name);
4510Sstevel@tonic-gate 				(void) printf("%-*s%-*s%s\n\n",
4520Sstevel@tonic-gate 				    12 + adj, "Offset", 20, "Symndx", "Type");
4530Sstevel@tonic-gate 				n_title = 1;
4540Sstevel@tonic-gate 			}
4550Sstevel@tonic-gate 		}
4560Sstevel@tonic-gate 		if (d_flag) {
4570Sstevel@tonic-gate 			if (!d_hi)
4580Sstevel@tonic-gate 				d_hi = d_low;
4590Sstevel@tonic-gate 			if ((symid < d_low) || (symid > d_hi)) {
4600Sstevel@tonic-gate 				ndx++;
4610Sstevel@tonic-gate 				continue;
4620Sstevel@tonic-gate 			}
4630Sstevel@tonic-gate 		}
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 		(void) printf("%-#*llx", 12 + adj, EC_ADDR(rel.r_offset));
4660Sstevel@tonic-gate 		if (!v_flag) {
4670Sstevel@tonic-gate 			(void) printf("%-20d%-18d", symid, type);
4680Sstevel@tonic-gate 		} else {
469*4734Sab196087 			Conv_inv_buf_t	inv_buf;
470*4734Sab196087 
4710Sstevel@tonic-gate 			if (strlen(sym_name))
4720Sstevel@tonic-gate 				(void) printf("%-20s", sym_name);
4731976Sab196087 			else {
4740Sstevel@tonic-gate 				(void) printf("%-20d", sym.st_name);
4751976Sab196087 			}
4761976Sab196087 			(void) printf("%-20s",
477*4734Sab196087 			    conv_reloc_type(p_ehdr->e_machine,
478*4734Sab196087 			    type, DUMP_CONVFMT, &inv_buf));
4790Sstevel@tonic-gate 		}
4800Sstevel@tonic-gate 		(void) printf("\n");
4810Sstevel@tonic-gate 		ndx++;
4820Sstevel@tonic-gate 	}
4830Sstevel@tonic-gate }
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate /* demangle C++ names */
4860Sstevel@tonic-gate static char *
4870Sstevel@tonic-gate demangled_name(char *s)
4880Sstevel@tonic-gate {
4891618Srie 	static char	*buf = NULL;
4901618Srie 	char		*dn;
4911618Srie 	size_t		len;
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	dn = sgs_demangle(s);
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	/*
4960Sstevel@tonic-gate 	 * If not demangled, just return the symbol name
4970Sstevel@tonic-gate 	 */
4980Sstevel@tonic-gate 	if (strcmp(s, dn) == 0)
4990Sstevel@tonic-gate 		return (s);
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	/*
5020Sstevel@tonic-gate 	 * Demangled. Format it
5030Sstevel@tonic-gate 	 */
5040Sstevel@tonic-gate 	if (buf != NULL)
5050Sstevel@tonic-gate 		free(buf);
5060Sstevel@tonic-gate 
5071618Srie 	len = strlen(dn) + strlen(s) + 4;
5081618Srie 	if ((buf = malloc(len)) == NULL)
5090Sstevel@tonic-gate 		return (s);
5100Sstevel@tonic-gate 
5111618Srie 	(void) snprintf(buf, len, "%s\t[%s]", dn, s);
5120Sstevel@tonic-gate 	return (buf);
5130Sstevel@tonic-gate }
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate /*
5160Sstevel@tonic-gate  * Print the symbol table.  Input is an ELF file descriptor, a
5170Sstevel@tonic-gate  * pointer to the symbol table SCNTAB structure,
5180Sstevel@tonic-gate  * the number of symbols, a range of symbols to print,
5190Sstevel@tonic-gate  * an index which is the number of the
5200Sstevel@tonic-gate  * section in the file, and the filename.  The number of sections,
5210Sstevel@tonic-gate  * the range, and the index are set in
5220Sstevel@tonic-gate  * dump_symbol_table, depending on whether -n or -T were set.
5230Sstevel@tonic-gate  */
5240Sstevel@tonic-gate static void
5250Sstevel@tonic-gate print_symtab(Elf *elf_file, SCNTAB *p_symtab, Elf_Data *sym_data,
5260Sstevel@tonic-gate 	long range, int index)
5270Sstevel@tonic-gate {
5280Sstevel@tonic-gate 	GElf_Sym sym;
5290Sstevel@tonic-gate 	int adj = 0;		/* field adjustment for elf64 */
5300Sstevel@tonic-gate 	Elf32_Word	*symshndx = 0;
5310Sstevel@tonic-gate 	unsigned int	nosymshndx = 0;
532*4734Sab196087 	Conv_inv_buf_t	inv_buf;
533*4734Sab196087 
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	if (gelf_getclass(elf_file) == ELFCLASS64)
5361618Srie 		adj = 8;
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	while (range > 0) {
5390Sstevel@tonic-gate 		char		*sym_name = (char *)0;
5400Sstevel@tonic-gate 		int		type, bind;
5410Sstevel@tonic-gate 		int		specsec;
5420Sstevel@tonic-gate 		unsigned int	shndx;
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 		(void) gelf_getsym(sym_data, index, &sym);
5450Sstevel@tonic-gate 		type = (int)GELF_ST_TYPE(sym.st_info);
5460Sstevel@tonic-gate 		bind = (int)GELF_ST_BIND(sym.st_info);
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 		if ((sym.st_shndx == SHN_XINDEX) &&
5490Sstevel@tonic-gate 		    (symshndx == 0) && (nosymshndx == 0)) {
5500Sstevel@tonic-gate 			Elf_Scn		*_scn;
5510Sstevel@tonic-gate 			GElf_Shdr	_shdr;
5520Sstevel@tonic-gate 			size_t		symscnndx;
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 			symscnndx = elf_ndxscn(p_symtab->p_sd);
5550Sstevel@tonic-gate 			_scn = 0;
5560Sstevel@tonic-gate 			while ((_scn = elf_nextscn(elf_file, _scn)) != 0) {
5570Sstevel@tonic-gate 				if (gelf_getshdr(_scn, &_shdr) == 0)
5580Sstevel@tonic-gate 					break;
5590Sstevel@tonic-gate 				if ((_shdr.sh_type == SHT_SYMTAB_SHNDX) &&
5600Sstevel@tonic-gate 				    /* LINTED */
5610Sstevel@tonic-gate 				    (_shdr.sh_link == (GElf_Word)symscnndx)) {
5620Sstevel@tonic-gate 					Elf_Data	*_data;
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 					if ((_data = elf_getdata(_scn, 0)) == 0)
5650Sstevel@tonic-gate 						continue;
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 					symshndx = (Elf32_Word *)_data->d_buf;
5680Sstevel@tonic-gate 					nosymshndx = 0;
5690Sstevel@tonic-gate 					break;
5700Sstevel@tonic-gate 				}
5710Sstevel@tonic-gate 			}
5720Sstevel@tonic-gate 			nosymshndx = 1;
5730Sstevel@tonic-gate 		}
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 		if ((symshndx) && (sym.st_shndx == SHN_XINDEX)) {
5760Sstevel@tonic-gate 			shndx = symshndx[index];
5770Sstevel@tonic-gate 			specsec = 0;
5780Sstevel@tonic-gate 		} else {
5790Sstevel@tonic-gate 			shndx = sym.st_shndx;
5800Sstevel@tonic-gate 			if ((sym.st_shndx == SHN_UNDEF) ||
5810Sstevel@tonic-gate 			    (sym.st_shndx >= SHN_LORESERVE))
5820Sstevel@tonic-gate 				specsec = 1;
5830Sstevel@tonic-gate 			else
5840Sstevel@tonic-gate 				specsec = 0;
5850Sstevel@tonic-gate 		}
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 		(void) printf("[%d]\t ", index++);
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 		if (v_flag && (type == STT_SPARC_REGISTER)) {
5910Sstevel@tonic-gate 			/*
5920Sstevel@tonic-gate 			 *  The strings "REG_G1" through "REG_G7" are intended
5930Sstevel@tonic-gate 			 *  to be consistent with output from elfdump(1).
5940Sstevel@tonic-gate 			 */
5951976Sab196087 			(void) printf("%-*s", 12 + adj,
596*4734Sab196087 			    conv_sym_SPARC_value(sym.st_value,
597*4734Sab196087 			    DUMP_CONVFMT, &inv_buf));
5981976Sab196087 		} else {
5990Sstevel@tonic-gate 			(void) printf("0x%-*llx", 10 + adj,
6000Sstevel@tonic-gate 			    EC_ADDR(sym.st_value));
6011976Sab196087 		}
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 		(void) printf("%-*lld", 9 + adj, EC_XWORD(sym.st_size));
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 		if (!v_flag) {
6060Sstevel@tonic-gate 			(void) printf("%d\t\t%d\t%d\t%#x\t",
6070Sstevel@tonic-gate 			    type, bind, (int)sym.st_other, (int)shndx);
6080Sstevel@tonic-gate 		} else {
6091976Sab196087 			GElf_Ehdr p_ehdr;
6101976Sab196087 			(void) gelf_getehdr(elf_file, &p_ehdr);
6111976Sab196087 			(void) printf("%s\t",
612*4734Sab196087 			    conv_sym_info_type(p_ehdr.e_machine, type,
613*4734Sab196087 			    DUMP_CONVFMT, &inv_buf));
6141976Sab196087 			(void) printf("%s",
615*4734Sab196087 			    conv_sym_info_bind(bind, DUMP_CONVFMT, &inv_buf));
6160Sstevel@tonic-gate 			(void) printf("\t  %d\t", EC_WORD(sym.st_other));
6170Sstevel@tonic-gate 
618*4734Sab196087 			if (specsec)
619*4734Sab196087 				(void) printf("%s",
620*4734Sab196087 				    conv_sym_shndx(shndx, &inv_buf));
621*4734Sab196087 			else
6220Sstevel@tonic-gate 				(void) printf("%d", EC_WORD(shndx));
6230Sstevel@tonic-gate 			(void) printf("\t");
6240Sstevel@tonic-gate 		}
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 		/* support machines where NULL-deref causes core dump */
6270Sstevel@tonic-gate 		if (sym.st_name == 0)
6280Sstevel@tonic-gate 			sym_name = (char *)UNKNOWN;
6290Sstevel@tonic-gate 		else
6300Sstevel@tonic-gate 			if (C_flag)
6310Sstevel@tonic-gate 				sym_name = demangled_name(
632*4734Sab196087 				    (char *)elf_strptr(elf_file,
633*4734Sab196087 				    p_symtab->p_shdr.sh_link,
634*4734Sab196087 				    sym.st_name));
6350Sstevel@tonic-gate 		else
6360Sstevel@tonic-gate 			sym_name = (char *)elf_strptr(elf_file,
637*4734Sab196087 			    p_symtab->p_shdr.sh_link, sym.st_name);
6380Sstevel@tonic-gate 		if (sym_name == NULL)
6390Sstevel@tonic-gate 			sym_name = (char *)UNKNOWN;
6400Sstevel@tonic-gate 		(void) printf("%s\n", sym_name);
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 		range--;
6430Sstevel@tonic-gate 	}	/* end while */
6440Sstevel@tonic-gate }
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate /*
6470Sstevel@tonic-gate  * Print the section header table.  Input is the SCNTAB structure,
6480Sstevel@tonic-gate  * the number of sections, an index which is the number of the
6490Sstevel@tonic-gate  * section in the file, and the filename.  The values of the SCNTAB
6500Sstevel@tonic-gate  * structure, the number of sections, and the index are set in
6510Sstevel@tonic-gate  * dump_shdr depending on whether the -n or -d modifiers were set.
6520Sstevel@tonic-gate  */
6530Sstevel@tonic-gate static void
6540Sstevel@tonic-gate print_shdr(Elf *elf_file, SCNTAB *s, int num_scns, int index)
6550Sstevel@tonic-gate {
6560Sstevel@tonic-gate 	SCNTAB *p;
6570Sstevel@tonic-gate 	int num;
6580Sstevel@tonic-gate 	int field;
6591976Sab196087 	GElf_Ehdr p_ehdr;
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	if (gelf_getclass(elf_file) == ELFCLASS64)
6621618Srie 		field = 21;
6630Sstevel@tonic-gate 	else
6640Sstevel@tonic-gate 		field = 13;
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	p = s;
6671976Sab196087 	(void) gelf_getehdr(elf_file, &p_ehdr);
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	for (num = 0; num < num_scns; num++, p++) {
6700Sstevel@tonic-gate 		(void) printf("[%d]\t", index++);
6710Sstevel@tonic-gate 		if (!v_flag) {
6720Sstevel@tonic-gate 			(void) printf("%u\t%llu\t",
673*4734Sab196087 			    EC_WORD(p->p_shdr.sh_type),
674*4734Sab196087 			    EC_XWORD(p->p_shdr.sh_flags));
6750Sstevel@tonic-gate 		} else {
676*4734Sab196087 			Conv_inv_buf_t inv_buf;
677*4734Sab196087 
6782352Sab196087 			/*LINTED: E_SEC_PRINTF_VAR_FMT*/
6791976Sab196087 			(void) printf(conv_sec_type(p_ehdr.e_machine,
680*4734Sab196087 			    p->p_shdr.sh_type, DUMP_CONVFMT, &inv_buf));
6810Sstevel@tonic-gate 			(void) printf("    ");
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 			if (p->p_shdr.sh_flags & SHF_WRITE)
6840Sstevel@tonic-gate 				(void) printf("W");
6850Sstevel@tonic-gate 			else
6860Sstevel@tonic-gate 				(void) printf("-");
6870Sstevel@tonic-gate 			if (p->p_shdr.sh_flags & SHF_ALLOC)
6880Sstevel@tonic-gate 				(void) printf("A");
6890Sstevel@tonic-gate 			else
6900Sstevel@tonic-gate 				(void) printf("-");
6910Sstevel@tonic-gate 			if (p->p_shdr.sh_flags & SHF_EXECINSTR)
6920Sstevel@tonic-gate 				(void) printf("I");
6930Sstevel@tonic-gate 			else
6940Sstevel@tonic-gate 				(void) printf("-");
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 			if (p->p_shdr.sh_flags & SHF_ORDERED)
6970Sstevel@tonic-gate 				(void) printf("O");
6980Sstevel@tonic-gate 			if (p->p_shdr.sh_flags & SHF_EXCLUDE)
6990Sstevel@tonic-gate 				(void) printf("E");
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 			(void) printf("\t");
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 		}
7040Sstevel@tonic-gate 		(void) printf("%-#*llx%-#*llx%-#*llx%s%s\n",
705*4734Sab196087 		    field, EC_ADDR(p->p_shdr.sh_addr),
706*4734Sab196087 		    field, EC_OFF(p->p_shdr.sh_offset),
707*4734Sab196087 		    field, EC_XWORD(p->p_shdr.sh_size),
708*4734Sab196087 		    /* compatibility:  tab for elf32 */
709*4734Sab196087 		    (field == 13) ? "\t" : " ", p->scn_name);
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 		(void) printf("\t%u\t%u\t%-#*llx%-#*llx\n\n",
712*4734Sab196087 		    EC_WORD(p->p_shdr.sh_link),
713*4734Sab196087 		    EC_WORD(p->p_shdr.sh_info),
714*4734Sab196087 		    field, EC_XWORD(p->p_shdr.sh_addralign),
715*4734Sab196087 		    field, EC_XWORD(p->p_shdr.sh_entsize));
7160Sstevel@tonic-gate 	}
7170Sstevel@tonic-gate }
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate /*
7200Sstevel@tonic-gate  * Check that a range of numbers is valid.  Input is
7210Sstevel@tonic-gate  * a lower bound, an upper bound, a boundary condition,
7220Sstevel@tonic-gate  * and the filename.  Negative numbers and numbers greater
7230Sstevel@tonic-gate  * than the bound are invalid.  low must be smaller than hi.
7240Sstevel@tonic-gate  * The returned integer is the number of items in the
7250Sstevel@tonic-gate  * range if it is valid and -1 otherwise.
7260Sstevel@tonic-gate  */
7270Sstevel@tonic-gate static int
7280Sstevel@tonic-gate check_range(int low, int hi, size_t bound, char *filename)
7290Sstevel@tonic-gate {
7300Sstevel@tonic-gate 	if (((size_t)low > bound) || (low <= 0)) {
7310Sstevel@tonic-gate 		(void) fprintf(stderr,
732*4734Sab196087 		    "%s: %s: number out of range, %d\n",
733*4734Sab196087 		    prog_name, filename, low);
7340Sstevel@tonic-gate 		return (-1);
7350Sstevel@tonic-gate 	}
7360Sstevel@tonic-gate 	if (((size_t)hi > bound) || (hi < 0)) {
7370Sstevel@tonic-gate 		(void) fprintf(stderr,
738*4734Sab196087 		    "%s: %s: number out of range, %d\n",
739*4734Sab196087 		    prog_name, filename, hi);
740*4734Sab196087 		return (-1);
7410Sstevel@tonic-gate 	}
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	if (hi && (low > hi)) {
7440Sstevel@tonic-gate 		(void) fprintf(stderr,
745*4734Sab196087 		    "%s: %s: invalid range, %d,%d\n",
746*4734Sab196087 		    prog_name, filename, low, hi);
7470Sstevel@tonic-gate 		return (-1);
7480Sstevel@tonic-gate 	}
7490Sstevel@tonic-gate 	if (hi)
7500Sstevel@tonic-gate 		return (hi - low + 1);
7510Sstevel@tonic-gate 	else
7520Sstevel@tonic-gate 		return (1);
7530Sstevel@tonic-gate }
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate /*
7560Sstevel@tonic-gate  * Print relocation information.  Since this information is
7570Sstevel@tonic-gate  * machine dependent, new sections must be added for each machine
7580Sstevel@tonic-gate  * that is supported.  Input is an ELF file descriptor, the ELF header,
7590Sstevel@tonic-gate  * the SCNTAB structure, the number of sections, and a filename.
7600Sstevel@tonic-gate  * Set up necessary information to print relocation information
7610Sstevel@tonic-gate  * and call the appropriate print function depending on the
7620Sstevel@tonic-gate  * type of relocation information.  If the symbol table is
7630Sstevel@tonic-gate  * absent, no relocation data is processed.  Input is an
7640Sstevel@tonic-gate  * ELF file descriptor, the ELF header, the SCNTAB structure,
7650Sstevel@tonic-gate  * and the filename.  Set range of d_flag and name if n_flag.
7660Sstevel@tonic-gate  */
7670Sstevel@tonic-gate static void
7680Sstevel@tonic-gate dump_reloc_table(Elf *elf_file, GElf_Ehdr *p_ehdr,
7690Sstevel@tonic-gate 	SCNTAB *p_scns, int num_scns, char *filename)
7700Sstevel@tonic-gate {
7710Sstevel@tonic-gate 	Elf_Data *rel_data;
7720Sstevel@tonic-gate 	Elf_Data *sym_data;
7730Sstevel@tonic-gate 	size_t    sym_size;
7740Sstevel@tonic-gate 	size_t    reloc_size;
7750Sstevel@tonic-gate 	SCNTAB *reloc_symtab;
7760Sstevel@tonic-gate 	SCNTAB *head_scns;
7770Sstevel@tonic-gate 	int r_title = 0;
7780Sstevel@tonic-gate 	int adj = 0;
7790Sstevel@tonic-gate 	size_t shnum;
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	if (gelf_getclass(elf_file) == ELFCLASS64)
7821618Srie 		adj = 8;
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	if ((!p_flag) && (!r_title)) {
7850Sstevel@tonic-gate 		(void) printf("\n    **** RELOCATION INFORMATION ****\n");
7860Sstevel@tonic-gate 		r_title = 1;
7870Sstevel@tonic-gate 	}
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	while (num_scns-- > 0) {
7900Sstevel@tonic-gate 		if ((p_scns->p_shdr.sh_type != SHT_RELA) &&
7910Sstevel@tonic-gate 		    (p_scns->p_shdr.sh_type != SHT_REL)) {
7920Sstevel@tonic-gate 			p_scns++;
7930Sstevel@tonic-gate 			continue;
7940Sstevel@tonic-gate 		}
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	head_scns = p_head_scns;
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	if (elf_getshnum(elf_file, &shnum) == 0) {
7990Sstevel@tonic-gate 		(void) fprintf(stderr,
800*4734Sab196087 		    "%s: %s: elf_getshnum failed: %s\n",
801*4734Sab196087 		    prog_name, filename, elf_errmsg(-1));
8020Sstevel@tonic-gate 		return;
8030Sstevel@tonic-gate 	}
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	if ((p_scns->p_shdr.sh_link == 0) ||
8060Sstevel@tonic-gate 	    /* LINTED */
8070Sstevel@tonic-gate 	    (p_scns->p_shdr.sh_link >= (GElf_Word)shnum)) {
8080Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: invalid sh_link field: "
809*4734Sab196087 		    "section #: %d sh_link: %d\n",
810*4734Sab196087 		    /* LINTED */
811*4734Sab196087 		    prog_name, filename, (int)elf_ndxscn(p_scns->p_sd),
812*4734Sab196087 		    (int)p_scns->p_shdr.sh_link);
8130Sstevel@tonic-gate 		return;
8140Sstevel@tonic-gate 	}
8150Sstevel@tonic-gate 	head_scns += (p_scns->p_shdr.sh_link -1);
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	if (head_scns->p_shdr.sh_type == SHT_SYMTAB) {
8180Sstevel@tonic-gate 		reloc_symtab = p_symtab;
8190Sstevel@tonic-gate 	} else if (head_scns->p_shdr.sh_type  == SHT_DYNSYM) {
8200Sstevel@tonic-gate 		reloc_symtab = p_dynsym;
8210Sstevel@tonic-gate 	} else {
8220Sstevel@tonic-gate 		(void) fprintf(stderr,
8230Sstevel@tonic-gate "%s: %s: could not get symbol table\n", prog_name, filename);
8240Sstevel@tonic-gate 		return;
8250Sstevel@tonic-gate 	}
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	sym_data = NULL;
8280Sstevel@tonic-gate 	sym_size = 0;
8290Sstevel@tonic-gate 	reloc_size = 0;
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	if ((sym_data = elf_getdata(reloc_symtab->p_sd, NULL)) == NULL) {
8320Sstevel@tonic-gate 		(void) fprintf(stderr,
8330Sstevel@tonic-gate 		"%s: %s: no symbol table data\n", prog_name, filename);
8340Sstevel@tonic-gate 		return;
8350Sstevel@tonic-gate 	}
8360Sstevel@tonic-gate 	sym_size = sym_data->d_size;
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 	if (p_scns == NULL) {
8390Sstevel@tonic-gate 		(void) fprintf(stderr,
8400Sstevel@tonic-gate 		"%s: %s: no section table data\n", prog_name, filename);
8410Sstevel@tonic-gate 		return;
8420Sstevel@tonic-gate 	}
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	if (p_scns->p_shdr.sh_type == SHT_RELA) {
8450Sstevel@tonic-gate 		if (!n_flag && r_flag)
8460Sstevel@tonic-gate 			(void) printf("\n%s:\n", p_scns->scn_name);
8470Sstevel@tonic-gate 		if (!p_flag && (!n_flag && r_flag))
8480Sstevel@tonic-gate 			(void) printf("%-*s%-*s%-*s%s\n\n",
8490Sstevel@tonic-gate 			    12 + adj, "Offset", 22, "Symndx",
8500Sstevel@tonic-gate 			    18, "Type", "Addend");
8510Sstevel@tonic-gate 		if ((rel_data = elf_getdata(p_scns->p_sd, NULL)) == NULL) {
8520Sstevel@tonic-gate 			(void) fprintf(stderr,
8530Sstevel@tonic-gate "%s: %s: no relocation information\n", prog_name, filename);
8540Sstevel@tonic-gate 			return;
8550Sstevel@tonic-gate 		}
8560Sstevel@tonic-gate 		reloc_size = rel_data->d_size;
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 		if (n_flag) {
8590Sstevel@tonic-gate 			rn_flag = 1;
8600Sstevel@tonic-gate 			print_rela(elf_file, p_scns, rel_data, sym_data, p_ehdr,
861*4734Sab196087 			    reloc_size, sym_size, filename, reloc_symtab);
8620Sstevel@tonic-gate 		}
8630Sstevel@tonic-gate 		if (d_flag) {
8640Sstevel@tonic-gate 			rn_flag = 0;
8650Sstevel@tonic-gate 			print_rela(elf_file, p_scns, rel_data, sym_data, p_ehdr,
866*4734Sab196087 			    reloc_size, sym_size, filename, reloc_symtab);
8670Sstevel@tonic-gate 		}
8680Sstevel@tonic-gate 		if (!n_flag && !d_flag)
8690Sstevel@tonic-gate 			print_rela(elf_file, p_scns, rel_data, sym_data, p_ehdr,
870*4734Sab196087 			    reloc_size, sym_size, filename, reloc_symtab);
8710Sstevel@tonic-gate 	} else {
8720Sstevel@tonic-gate 		if (p_scns->p_shdr.sh_type == SHT_REL) {
8730Sstevel@tonic-gate 			if (!n_flag && r_flag)
8740Sstevel@tonic-gate 				(void) printf("\n%s:\n", p_scns->scn_name);
8750Sstevel@tonic-gate 			if (!p_flag && (!n_flag && r_flag)) {
8760Sstevel@tonic-gate 				(void) printf("%-*s%-*s%s\n\n",
8770Sstevel@tonic-gate 				    12 + adj, "Offset", 20, "Symndx", "Type");
8780Sstevel@tonic-gate 			}
8790Sstevel@tonic-gate 			if ((rel_data = elf_getdata(p_scns->p_sd, NULL))
8800Sstevel@tonic-gate 			    == NULL) {
8810Sstevel@tonic-gate 				(void) fprintf(stderr,
8820Sstevel@tonic-gate "%s: %s: no relocation information\n", prog_name, filename);
8830Sstevel@tonic-gate 				return;
8840Sstevel@tonic-gate 			}
8850Sstevel@tonic-gate 			reloc_size = rel_data->d_size;
8860Sstevel@tonic-gate 			if (n_flag) {
8870Sstevel@tonic-gate 				rn_flag = 1;
8880Sstevel@tonic-gate 				print_rel(elf_file, p_scns, rel_data, sym_data,
889*4734Sab196087 				    p_ehdr, reloc_size, sym_size,
890*4734Sab196087 				    filename, reloc_symtab);
8910Sstevel@tonic-gate 			}
8920Sstevel@tonic-gate 			if (d_flag) {
8930Sstevel@tonic-gate 				rn_flag = 0;
8940Sstevel@tonic-gate 				print_rel(elf_file, p_scns, rel_data, sym_data,
895*4734Sab196087 				    p_ehdr, reloc_size, sym_size,
896*4734Sab196087 				    filename, reloc_symtab);
8970Sstevel@tonic-gate 			}
8980Sstevel@tonic-gate 			if (!n_flag && !d_flag)
8990Sstevel@tonic-gate 				print_rel(elf_file, p_scns, rel_data, sym_data,
900*4734Sab196087 				    p_ehdr, reloc_size, sym_size,
901*4734Sab196087 				    filename, reloc_symtab);
9020Sstevel@tonic-gate 		}
9030Sstevel@tonic-gate 	}
9040Sstevel@tonic-gate 	p_scns++;
9050Sstevel@tonic-gate 	}
9060Sstevel@tonic-gate }
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate /*
9090Sstevel@tonic-gate  * Print out the string tables.  Input is an opened ELF file,
9100Sstevel@tonic-gate  * the SCNTAB structure, the number of sections, and the filename.
9110Sstevel@tonic-gate  * Since there can be more than one string table, all sections are
9120Sstevel@tonic-gate  * examined and any with the correct type are printed out.
9130Sstevel@tonic-gate  */
9140Sstevel@tonic-gate static void
9150Sstevel@tonic-gate dump_string_table(SCNTAB *s, int num_scns)
9160Sstevel@tonic-gate {
9170Sstevel@tonic-gate 	size_t section_size;
9180Sstevel@tonic-gate 	unsigned char *strtab;
9190Sstevel@tonic-gate 	int beg_of_string;
9200Sstevel@tonic-gate 	int counter = 0;
9210Sstevel@tonic-gate 	int str_off;
9220Sstevel@tonic-gate 	int i;
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 	if (!p_flag) {
9250Sstevel@tonic-gate 		(void) printf("\n     **** STRING TABLE INFORMATION ****\n");
9260Sstevel@tonic-gate 	}
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	for (i = 0; i < num_scns; i++, s++) {
9290Sstevel@tonic-gate 		if (s->p_shdr.sh_type != SHT_STRTAB)
9300Sstevel@tonic-gate 			continue;
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 		str_off = 0;
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 		if (!p_flag) {
9350Sstevel@tonic-gate 			(void) printf("\n%s:\n", s->scn_name);
9360Sstevel@tonic-gate 			(void) printf("   <offset>  \tName\n");
9370Sstevel@tonic-gate 		}
9380Sstevel@tonic-gate 		section_size = 0;
9390Sstevel@tonic-gate 		if ((strtab = (unsigned char *)
9400Sstevel@tonic-gate 		    get_scndata(s->p_sd, &section_size)) == NULL) {
9410Sstevel@tonic-gate 			continue;
9420Sstevel@tonic-gate 		}
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 		if (section_size != 0) {
9450Sstevel@tonic-gate 			(void) printf("   <%d>  \t", str_off);
9460Sstevel@tonic-gate 			beg_of_string = 0;
9470Sstevel@tonic-gate 			while (section_size--) {
9480Sstevel@tonic-gate 				unsigned char c = *strtab++;
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 				if (beg_of_string) {
9510Sstevel@tonic-gate 					(void) printf("   <%d>  \t", str_off);
9520Sstevel@tonic-gate 					counter++;
9530Sstevel@tonic-gate 					beg_of_string = 0;
9540Sstevel@tonic-gate 				}
9550Sstevel@tonic-gate 				str_off++;
9560Sstevel@tonic-gate 				switch (c) {
9570Sstevel@tonic-gate 				case '\0':
9580Sstevel@tonic-gate 					(void) printf("\n");
9590Sstevel@tonic-gate 					beg_of_string = 1;
9600Sstevel@tonic-gate 					break;
9610Sstevel@tonic-gate 				default:
9620Sstevel@tonic-gate 					(void) putchar(c);
9630Sstevel@tonic-gate 				}
9640Sstevel@tonic-gate 			}
9650Sstevel@tonic-gate 		}
9660Sstevel@tonic-gate 	}
9670Sstevel@tonic-gate 	(void) printf("\n");
9680Sstevel@tonic-gate }
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate /*
9710Sstevel@tonic-gate  * Print the symbol table.  This function does not print the contents
9720Sstevel@tonic-gate  * of the symbol table but sets up the parameters and then calls
9730Sstevel@tonic-gate  * print_symtab to print the symbols.  Calling another function to print
9740Sstevel@tonic-gate  * the symbols allows both -T and -n to work correctly
9750Sstevel@tonic-gate  * simultaneously.  Input is an opened ELF file, a pointer to the
9760Sstevel@tonic-gate  * symbol table SCNTAB structure, and the filename.
9770Sstevel@tonic-gate  * Set the range of symbols to print if T_flag, and set
9780Sstevel@tonic-gate  * name of symbol to print if n_flag.
9790Sstevel@tonic-gate  */
9800Sstevel@tonic-gate static void
9810Sstevel@tonic-gate dump_symbol_table(Elf *elf_file, SCNTAB *p_symtab, char *filename)
9820Sstevel@tonic-gate {
9830Sstevel@tonic-gate 	Elf_Data  *sym_data;
9840Sstevel@tonic-gate 	GElf_Sym  T_range, n_range;	/* for use with -T and -n */
9850Sstevel@tonic-gate 	size_t count = 0;
9860Sstevel@tonic-gate 	size_t sym_size;
9870Sstevel@tonic-gate 	int index = 1;
9880Sstevel@tonic-gate 	int found_it = 0;
9890Sstevel@tonic-gate 	int i;
9900Sstevel@tonic-gate 	int adj = 0;			/*  field adjustment for elf64 */
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate 	if (gelf_getclass(elf_file) == ELFCLASS64)
9931618Srie 		adj = 8;
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 	if (p_symtab == NULL) {
9960Sstevel@tonic-gate 		(void) fprintf(stderr,
9970Sstevel@tonic-gate 		"%s: %s: could not get symbol table\n", prog_name, filename);
9980Sstevel@tonic-gate 		return;
9990Sstevel@tonic-gate 	}
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 	/* get symbol table data */
10020Sstevel@tonic-gate 	sym_data = NULL;
10030Sstevel@tonic-gate 	sym_size = 0;
10040Sstevel@tonic-gate 	if ((sym_data =
10050Sstevel@tonic-gate 	    elf_getdata(p_symtab->p_sd, NULL)) == NULL) {
10060Sstevel@tonic-gate 		(void) printf("\n%s:\n", p_symtab->scn_name);
10070Sstevel@tonic-gate 		(void) printf("No symbol table data\n");
10080Sstevel@tonic-gate 		return;
10090Sstevel@tonic-gate 	}
10100Sstevel@tonic-gate 	sym_size = sym_data->d_size;
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate 	count = sym_size / p_symtab->p_shdr.sh_entsize;
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 	if (n_flag && t_flag && !T_flag) {
10150Sstevel@tonic-gate 		/* LINTED */
10160Sstevel@tonic-gate 		for (i = 1; i < count; i++) {
10170Sstevel@tonic-gate 			(void) gelf_getsym(sym_data, i, &n_range);
10180Sstevel@tonic-gate 			if (strcmp(name, (char *)
10190Sstevel@tonic-gate 			    elf_strptr(elf_file,
10200Sstevel@tonic-gate 			    p_symtab->p_shdr.sh_link,
10210Sstevel@tonic-gate 			    n_range.st_name)) != 0) {
10220Sstevel@tonic-gate 				continue;
10230Sstevel@tonic-gate 			} else {
10240Sstevel@tonic-gate 				found_it = 1;
10250Sstevel@tonic-gate 				if (!p_flag) {
10260Sstevel@tonic-gate 					(void) printf(
10270Sstevel@tonic-gate "\n              ***** SYMBOL TABLE INFORMATION *****\n");
10280Sstevel@tonic-gate 					(void) printf(
10290Sstevel@tonic-gate "[Index]  %-*s%-*sType\tBind\tOther\tShndx\tName",
1030*4734Sab196087 					    12 + adj, "Value", 9 + adj, "Size");
10310Sstevel@tonic-gate 				}
10320Sstevel@tonic-gate 				(void) printf("\n%s:\n", p_symtab->scn_name);
10330Sstevel@tonic-gate 				print_symtab(elf_file, p_symtab, sym_data,
10340Sstevel@tonic-gate 				    1, i);
10350Sstevel@tonic-gate 			}
10360Sstevel@tonic-gate 		}   /* end for */
10370Sstevel@tonic-gate 		if (!found_it) {
10380Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s: %s not found\n",
1039*4734Sab196087 			    prog_name, filename, name);
10400Sstevel@tonic-gate 		}
10410Sstevel@tonic-gate 	} else if (T_flag) {
10420Sstevel@tonic-gate 		T_num = check_range(T_low, T_hi, count, filename);
10430Sstevel@tonic-gate 		if (T_num < 0)
10440Sstevel@tonic-gate 			return;
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 		(void) gelf_getsym(sym_data, T_low-1, &T_range);
10470Sstevel@tonic-gate 		index = T_low;
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 		if (!p_flag) {
10500Sstevel@tonic-gate 			(void) printf(
10510Sstevel@tonic-gate "\n              ***** SYMBOL TABLE INFORMATION *****\n");
10520Sstevel@tonic-gate 			(void) printf(
10530Sstevel@tonic-gate "[Index]  %-*s%-*sType\tBind\tOther\tShndx\tName",
10540Sstevel@tonic-gate 			    12 + adj, "Value", 9 + adj, "Size");
10550Sstevel@tonic-gate 		}
10560Sstevel@tonic-gate 		(void) printf("\n%s:\n", p_symtab->scn_name);
10570Sstevel@tonic-gate 		print_symtab(elf_file, p_symtab, sym_data, T_num, index);
10580Sstevel@tonic-gate 	} else {
10590Sstevel@tonic-gate 		if (!p_flag) {
10600Sstevel@tonic-gate 			(void) printf(
10610Sstevel@tonic-gate "\n              ***** SYMBOL TABLE INFORMATION *****\n");
10620Sstevel@tonic-gate 			(void) printf(
10630Sstevel@tonic-gate "[Index]  %-*s%-*sType\tBind\tOther\tShndx\tName",
10640Sstevel@tonic-gate 			    12 + adj, "Value", 9 + adj, "Size");
10650Sstevel@tonic-gate 		}
10660Sstevel@tonic-gate 		(void) printf("\n%s:\n", p_symtab->scn_name);
10670Sstevel@tonic-gate 		print_symtab(elf_file, p_symtab, sym_data, count-1, 1);
10680Sstevel@tonic-gate 	}
10690Sstevel@tonic-gate }
10700Sstevel@tonic-gate 
10711698Sab196087 
10721698Sab196087 /*
10730Sstevel@tonic-gate  * Print dynamic linking information.  Input is an ELF
10740Sstevel@tonic-gate  * file descriptor, the SCNTAB structure, the number of
10750Sstevel@tonic-gate  * sections, and the filename.
10760Sstevel@tonic-gate  */
10770Sstevel@tonic-gate static void
10780Sstevel@tonic-gate dump_dynamic(Elf *elf_file, SCNTAB *p_scns, int num_scns, char *filename)
10790Sstevel@tonic-gate {
10802352Sab196087 #define	pdyn_Fmtptr	"%#llx"
10811698Sab196087 
10820Sstevel@tonic-gate 	Elf_Data	*dyn_data;
10830Sstevel@tonic-gate 	GElf_Dyn	p_dyn;
10840Sstevel@tonic-gate 	GElf_Phdr	p_phdr;
10850Sstevel@tonic-gate 	GElf_Ehdr	p_ehdr;
10860Sstevel@tonic-gate 	int		index = 1;
10870Sstevel@tonic-gate 	int		lib_scns = num_scns;
10880Sstevel@tonic-gate 	SCNTAB		*l_scns = p_scns;
10890Sstevel@tonic-gate 	int		header_num = 0;
10902352Sab196087 	const char	*str;
10912352Sab196087 
10922352Sab196087 	(void) gelf_getehdr(elf_file, &p_ehdr);
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 	if (!p_flag)
10950Sstevel@tonic-gate 		(void) printf("\n  **** DYNAMIC SECTION INFORMATION ****\n");
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 	for (; num_scns > 0; num_scns--, p_scns++) {
10980Sstevel@tonic-gate 		GElf_Word	link;
10990Sstevel@tonic-gate 		int		ii;
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 		if (p_scns->p_shdr.sh_type != SHT_DYNAMIC)
11030Sstevel@tonic-gate 			continue;
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate 		if (!p_flag) {
11060Sstevel@tonic-gate 			(void) printf("%s:\n", p_scns->scn_name);
11070Sstevel@tonic-gate 			(void) printf("[INDEX]\tTag         Value\n");
11080Sstevel@tonic-gate 		}
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 		if ((dyn_data = elf_getdata(p_scns->p_sd, NULL)) == 0) {
11110Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s: no data in "
11120Sstevel@tonic-gate 			    "%s section\n", prog_name, filename,
11130Sstevel@tonic-gate 			    p_scns->scn_name);
11140Sstevel@tonic-gate 			return;
11150Sstevel@tonic-gate 		}
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 		link = p_scns->p_shdr.sh_link;
11180Sstevel@tonic-gate 		ii = 0;
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 		(void) gelf_getdyn(dyn_data, ii++, &p_dyn);
11210Sstevel@tonic-gate 		while (p_dyn.d_tag != DT_NULL) {
1122*4734Sab196087 			union {
1123*4734Sab196087 				Conv_inv_buf_t		inv;
1124*4734Sab196087 				Conv_dyn_flag_buf_t	dyn_flag;
1125*4734Sab196087 				Conv_dyn_flag1_buf_t	dyn_flag1;
1126*4734Sab196087 				Conv_dyn_feature1_buf_t	dyn_feature1;
1127*4734Sab196087 				Conv_dyn_posflag1_buf_t	dyn_posflag1;
1128*4734Sab196087 			} conv_buf;
1129*4734Sab196087 
11301976Sab196087 			(void) printf("[%d]\t%-15.15s ", index++,
1131*4734Sab196087 			    conv_dyn_tag(p_dyn.d_tag, p_ehdr.e_machine,
1132*4734Sab196087 			    DUMP_CONVFMT, &conv_buf.inv));
11330Sstevel@tonic-gate 
11341698Sab196087 			/*
11351698Sab196087 			 * It would be nice to use a table driven loop
11361698Sab196087 			 * here, but the address space is too sparse
11371698Sab196087 			 * and irregular. A switch is simple and robust.
11381698Sab196087 			 */
11390Sstevel@tonic-gate 			switch (p_dyn.d_tag) {
11400Sstevel@tonic-gate 			/*
11411976Sab196087 			 * Items with an address value
11420Sstevel@tonic-gate 			 */
11431976Sab196087 			case DT_PLTGOT:
11441976Sab196087 			case DT_HASH:
11451976Sab196087 			case DT_STRTAB:
11461976Sab196087 			case DT_RELA:
11471976Sab196087 			case DT_SYMTAB:
11481976Sab196087 			case DT_INIT:
11491976Sab196087 			case DT_FINI:
11501976Sab196087 			case DT_REL:
11511976Sab196087 			case DT_DEBUG:
11521976Sab196087 			case DT_TEXTREL:
11531976Sab196087 			case DT_JMPREL:
11541976Sab196087 			case DT_INIT_ARRAY:
11551976Sab196087 			case DT_FINI_ARRAY:
11561976Sab196087 			case DT_INIT_ARRAYSZ:
11571976Sab196087 			case DT_FINI_ARRAYSZ:
11581976Sab196087 			case DT_PREINIT_ARRAY:
11591976Sab196087 			case DT_PREINIT_ARRAYSZ:
11601976Sab196087 			case DT_SUNW_RTLDINF:
11611976Sab196087 			case DT_SUNW_CAP:
11622766Sab196087 			case DT_SUNW_SYMTAB:
11633492Sab196087 			case DT_SUNW_SYMSORT:
11643492Sab196087 			case DT_SUNW_TLSSORT:
11651976Sab196087 			case DT_PLTPAD:
11661976Sab196087 			case DT_MOVETAB:
11671976Sab196087 			case DT_SYMINFO:
11681976Sab196087 			case DT_RELACOUNT:
11691976Sab196087 			case DT_RELCOUNT:
11701976Sab196087 			case DT_VERSYM:
11711976Sab196087 			case DT_VERDEF:
11721976Sab196087 			case DT_VERDEFNUM:
11731976Sab196087 			case DT_VERNEED:
11741976Sab196087 				(void) printf(pdyn_Fmtptr,
1175*4734Sab196087 				    EC_ADDR(p_dyn.d_un.d_ptr));
11760Sstevel@tonic-gate 				break;
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 			/*
11791976Sab196087 			 * Items with a string value
11800Sstevel@tonic-gate 			 */
11811976Sab196087 			case DT_NEEDED:
11821976Sab196087 			case DT_SONAME:
11831976Sab196087 			case DT_RPATH:
11841976Sab196087 			case DT_RUNPATH:
11851976Sab196087 			case DT_SUNW_AUXILIARY:
11861976Sab196087 			case DT_SUNW_FILTER:
11871976Sab196087 			case DT_CONFIG:
11881976Sab196087 			case DT_DEPAUDIT:
11891976Sab196087 			case DT_AUDIT:
11901976Sab196087 			case DT_AUXILIARY:
11911976Sab196087 			case DT_USED:
11921976Sab196087 			case DT_FILTER:
11931976Sab196087 				if (v_flag) {	/* Look up the string */
11941976Sab196087 					str = (char *)elf_strptr(elf_file, link,
1195*4734Sab196087 					    p_dyn.d_un.d_ptr);
11961976Sab196087 					if (!(str && *str))
11971976Sab196087 						str = (char *)UNKNOWN;
11981976Sab196087 					(void) printf("%s", str);
11991976Sab196087 				} else {	/* Show the address */
12001976Sab196087 					(void) printf(pdyn_Fmtptr,
1201*4734Sab196087 					    EC_ADDR(p_dyn.d_un.d_ptr));
12021976Sab196087 				}
12030Sstevel@tonic-gate 				break;
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 			/*
12061976Sab196087 			 * Items with a literal value
12070Sstevel@tonic-gate 			 */
12081976Sab196087 			case DT_PLTRELSZ:
12091976Sab196087 			case DT_RELASZ:
12101976Sab196087 			case DT_RELAENT:
12111976Sab196087 			case DT_STRSZ:
12121976Sab196087 			case DT_SYMENT:
12131976Sab196087 			case DT_RELSZ:
12141976Sab196087 			case DT_RELENT:
12151976Sab196087 			case DT_PLTREL:
12161976Sab196087 			case DT_BIND_NOW:
12171976Sab196087 			case DT_CHECKSUM:
12181976Sab196087 			case DT_PLTPADSZ:
12191976Sab196087 			case DT_MOVEENT:
12201976Sab196087 			case DT_MOVESZ:
12211976Sab196087 			case DT_SYMINSZ:
12221976Sab196087 			case DT_SYMINENT:
12231976Sab196087 			case DT_VERNEEDNUM:
12241976Sab196087 			case DT_SPARC_REGISTER:
12252766Sab196087 			case DT_SUNW_SYMSZ:
12263492Sab196087 			case DT_SUNW_SORTENT:
12273492Sab196087 			case DT_SUNW_SYMSORTSZ:
12283492Sab196087 			case DT_SUNW_TLSSORTSZ:
12293850Sab196087 			case DT_SUNW_STRPAD:
12301976Sab196087 				(void) printf(pdyn_Fmtptr,
1231*4734Sab196087 				    EC_XWORD(p_dyn.d_un.d_val));
12320Sstevel@tonic-gate 				break;
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 			/*
12351976Sab196087 			 * Items that are bitmasks
12360Sstevel@tonic-gate 			 */
12371976Sab196087 			case DT_FLAGS:
12381976Sab196087 			case DT_FEATURE_1:
12391976Sab196087 			case DT_POSFLAG_1:
12401976Sab196087 			case DT_FLAGS_1:
12412352Sab196087 				str = NULL;
12422352Sab196087 				if (v_flag) {
12432352Sab196087 					switch (p_dyn.d_tag) {
12442352Sab196087 					case DT_FLAGS:
12452352Sab196087 						str = conv_dyn_flag(
1246*4734Sab196087 						    p_dyn.d_un.d_val,
1247*4734Sab196087 						    DUMP_CONVFMT,
1248*4734Sab196087 						    &conv_buf.dyn_flag);
1249*4734Sab196087 						break;
12502352Sab196087 					case DT_FEATURE_1:
12512352Sab196087 						str = conv_dyn_feature1(
1252*4734Sab196087 						    p_dyn.d_un.d_val,
1253*4734Sab196087 						    DUMP_CONVFMT,
1254*4734Sab196087 						    &conv_buf.dyn_feature1);
12552352Sab196087 						break;
12562352Sab196087 					case DT_POSFLAG_1:
12572352Sab196087 						str = conv_dyn_posflag1(
1258*4734Sab196087 						    p_dyn.d_un.d_val,
1259*4734Sab196087 						    DUMP_CONVFMT,
1260*4734Sab196087 						    &conv_buf.dyn_posflag1);
12612352Sab196087 						break;
12622352Sab196087 					case DT_FLAGS_1:
12632352Sab196087 						str = conv_dyn_flag1(
1264*4734Sab196087 						    p_dyn.d_un.d_val,
1265*4734Sab196087 						    &conv_buf.dyn_flag1);
12662352Sab196087 						break;
12672352Sab196087 					}
12682352Sab196087 				}
12692352Sab196087 				if (str) {	/* Show as string */
12702352Sab196087 					(void) printf("%s", str);
12712352Sab196087 				} else {	/* Numeric form */
12722352Sab196087 					(void) printf(pdyn_Fmtptr,
1273*4734Sab196087 					    EC_ADDR(p_dyn.d_un.d_ptr));
12742352Sab196087 				}
12750Sstevel@tonic-gate 				break;
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 			/*
12781976Sab196087 			 * Depreciated items with a literal value
12790Sstevel@tonic-gate 			 */
12801976Sab196087 			case DT_DEPRECATED_SPARC_REGISTER:
12811976Sab196087 				(void) printf(pdyn_Fmtptr
1282*4734Sab196087 				    "  (deprecated value)",
1283*4734Sab196087 				    EC_XWORD(p_dyn.d_un.d_val));
12840Sstevel@tonic-gate 				break;
12851976Sab196087 
12861976Sab196087 			/* Ignored items */
12871976Sab196087 			case DT_SYMBOLIC:
12881976Sab196087 				(void) printf("(ignored)");
12890Sstevel@tonic-gate 				break;
12900Sstevel@tonic-gate 			}
12910Sstevel@tonic-gate 			(void) printf("\n");
12920Sstevel@tonic-gate 			(void) gelf_getdyn(dyn_data, ii++, &p_dyn);
12930Sstevel@tonic-gate 		}
12940Sstevel@tonic-gate 	}
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate 	/*
12970Sstevel@tonic-gate 	 * Check for existence of static shared library information.
12980Sstevel@tonic-gate 	 */
12990Sstevel@tonic-gate 	while (header_num < p_ehdr.e_phnum) {
13000Sstevel@tonic-gate 		(void) gelf_getphdr(elf_file, header_num, &p_phdr);
13010Sstevel@tonic-gate 		if (p_phdr.p_type == PT_SHLIB) {
13020Sstevel@tonic-gate 			while (--lib_scns > 0) {
13030Sstevel@tonic-gate 				if (strcmp(l_scns->scn_name, ".lib") == 0) {
13040Sstevel@tonic-gate 					print_static(l_scns, filename);
13050Sstevel@tonic-gate 				}
13060Sstevel@tonic-gate 				l_scns++;
13070Sstevel@tonic-gate 			}
13080Sstevel@tonic-gate 		}
13090Sstevel@tonic-gate 		header_num++;
13100Sstevel@tonic-gate 	}
13112352Sab196087 #undef	pdyn_Fmtptr
13120Sstevel@tonic-gate }
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate /*
13150Sstevel@tonic-gate  * Print the ELF header.  Input is an ELF file descriptor
13160Sstevel@tonic-gate  * and the filename.  If f_flag is set, the ELF header is
13170Sstevel@tonic-gate  * printed to stdout, otherwise the function returns after
13180Sstevel@tonic-gate  * setting the pointer to the ELF header.  Any values which
13190Sstevel@tonic-gate  * are not known are printed in decimal.  Fields must be updated
13200Sstevel@tonic-gate  * as new values are added.
13210Sstevel@tonic-gate  */
13220Sstevel@tonic-gate static GElf_Ehdr *
13230Sstevel@tonic-gate dump_elf_header(Elf *elf_file, char *filename, GElf_Ehdr * elf_head_p)
13240Sstevel@tonic-gate {
13251976Sab196087 	int class;
13260Sstevel@tonic-gate 	int field;
13270Sstevel@tonic-gate 
13280Sstevel@tonic-gate 	if (gelf_getehdr(elf_file, elf_head_p) == NULL) {
13291618Srie 		(void) fprintf(stderr, "%s: %s: %s\n", prog_name, filename,
13301618Srie 		    elf_errmsg(-1));
13310Sstevel@tonic-gate 		return (NULL);
13320Sstevel@tonic-gate 	}
13330Sstevel@tonic-gate 
13340Sstevel@tonic-gate 	class = (int)elf_head_p->e_ident[4];
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 	if (class == ELFCLASS64)
13371618Srie 		field = 21;
13380Sstevel@tonic-gate 	else
13390Sstevel@tonic-gate 		field = 13;
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate 	if (!f_flag)
13420Sstevel@tonic-gate 		return (elf_head_p);
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 	if (!p_flag) {
13451618Srie 		(void) printf("\n                    **** ELF HEADER ****\n");
13461618Srie 		(void) printf("%-*s%-11s%-*sMachine     Version\n",
13471618Srie 		    field, "Class", "Data", field, "Type");
13481618Srie 		(void) printf("%-*s%-11s%-*sFlags       Ehsize\n",
13491618Srie 		    field, "Entry", "Phoff", field, "Shoff");
13501618Srie 		(void) printf("%-*s%-11s%-*sShnum       Shstrndx\n\n",
13511618Srie 		    field, "Phentsize", "Phnum", field, "Shentsz");
13520Sstevel@tonic-gate 	}
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate 	if (!v_flag) {
13550Sstevel@tonic-gate 		(void) printf("%-*d%-11d%-*d%-12d%d\n",
1356*4734Sab196087 		    field, elf_head_p->e_ident[4], elf_head_p->e_ident[5],
1357*4734Sab196087 		    field, (int)elf_head_p->e_type, (int)elf_head_p->e_machine,
1358*4734Sab196087 		    elf_head_p->e_version);
13590Sstevel@tonic-gate 	} else {
1360*4734Sab196087 		Conv_inv_buf_t	inv_buf;
1361*4734Sab196087 
13621976Sab196087 		(void) printf("%-*s", field,
1363*4734Sab196087 		    conv_ehdr_class(class, DUMP_CONVFMT, &inv_buf));
13641976Sab196087 		(void) printf("%-11s",
1365*4734Sab196087 		    conv_ehdr_data(elf_head_p->e_ident[5], DUMP_CONVFMT,
1366*4734Sab196087 		    &inv_buf));
13671976Sab196087 		(void) printf("%-*s", field,
1368*4734Sab196087 		    conv_ehdr_type(elf_head_p->e_type, DUMP_CONVFMT, &inv_buf));
13691976Sab196087 		(void) printf("%-12s",
1370*4734Sab196087 		    conv_ehdr_mach(elf_head_p->e_machine, DUMP_CONVFMT,
1371*4734Sab196087 		    &inv_buf));
13721976Sab196087 		(void) printf("%s\n",
1373*4734Sab196087 		    conv_ehdr_vers(elf_head_p->e_version, DUMP_CONVFMT,
1374*4734Sab196087 		    &inv_buf));
13750Sstevel@tonic-gate 	}
13760Sstevel@tonic-gate 	(void) printf("%-#*llx%-#11llx%-#*llx%-#12x%#x\n",
1377*4734Sab196087 	    field, EC_ADDR(elf_head_p->e_entry), EC_OFF(elf_head_p->e_phoff),
1378*4734Sab196087 	    field, EC_OFF(elf_head_p->e_shoff), EC_WORD(elf_head_p->e_flags),
1379*4734Sab196087 	    EC_WORD(elf_head_p->e_ehsize));
13800Sstevel@tonic-gate 	if (!v_flag || (elf_head_p->e_shstrndx != SHN_XINDEX)) {
13810Sstevel@tonic-gate 		(void) printf("%-#*x%-11u%-#*x%-12u%u\n",
1382*4734Sab196087 		    field, EC_WORD(elf_head_p->e_phentsize),
1383*4734Sab196087 		    EC_WORD(elf_head_p->e_phnum),
1384*4734Sab196087 		    field, EC_WORD(elf_head_p->e_shentsize),
1385*4734Sab196087 		    EC_WORD(elf_head_p->e_shnum),
1386*4734Sab196087 		    EC_WORD(elf_head_p->e_shstrndx));
13870Sstevel@tonic-gate 	} else {
13880Sstevel@tonic-gate 		(void) printf("%-#*x%-11u%-#*x%-12uXINDEX\n",
1389*4734Sab196087 		    field, EC_WORD(elf_head_p->e_phentsize),
1390*4734Sab196087 		    EC_WORD(elf_head_p->e_phnum),
1391*4734Sab196087 		    field, EC_WORD(elf_head_p->e_shentsize),
1392*4734Sab196087 		    EC_WORD(elf_head_p->e_shnum));
13930Sstevel@tonic-gate 	}
13940Sstevel@tonic-gate 	if ((elf_head_p->e_shnum == 0) && (elf_head_p->e_shoff > 0)) {
13950Sstevel@tonic-gate 		Elf_Scn		*scn;
13960Sstevel@tonic-gate 		GElf_Shdr	shdr0;
13970Sstevel@tonic-gate 		int		field;
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate 		if (gelf_getclass(elf_file) == ELFCLASS64)
14001618Srie 			field = 21;
14010Sstevel@tonic-gate 		else
14021618Srie 			field = 13;
14030Sstevel@tonic-gate 		if (!p_flag) {
14040Sstevel@tonic-gate 			(void) printf("\n	   **** SECTION HEADER[0] "
14050Sstevel@tonic-gate 			    "{Elf Extensions} ****\n");
14060Sstevel@tonic-gate 			(void) printf(
14070Sstevel@tonic-gate 			    "[No]\tType\tFlags\t%-*s %-*s%-*s%sName\n",
14080Sstevel@tonic-gate 			    field, "Addr", field, "Offset", field,
14090Sstevel@tonic-gate 			    "Size(shnum)",
14100Sstevel@tonic-gate 			    /* compatibility:  tab for elf32 */
14111618Srie 			    (field == 13) ? "\t" : "  ");
14120Sstevel@tonic-gate 			(void) printf("\tLn(strndx) Info\t%-*s Entsize\n",
14130Sstevel@tonic-gate 			    field, "Adralgn");
14140Sstevel@tonic-gate 		}
14150Sstevel@tonic-gate 		if ((scn = elf_getscn(elf_file, 0)) == NULL) {
14160Sstevel@tonic-gate 			(void) fprintf(stderr,
1417*4734Sab196087 			    "%s: %s: elf_getscn failed: %s\n",
1418*4734Sab196087 			    prog_name, filename, elf_errmsg(-1));
14190Sstevel@tonic-gate 			return (NULL);
14200Sstevel@tonic-gate 		}
14210Sstevel@tonic-gate 		if (gelf_getshdr(scn, &shdr0) == 0) {
14220Sstevel@tonic-gate 			(void) fprintf(stderr,
1423*4734Sab196087 			    "%s: %s: gelf_getshdr: %s\n",
1424*4734Sab196087 			    prog_name, filename, elf_errmsg(-1));
14250Sstevel@tonic-gate 			return (NULL);
14260Sstevel@tonic-gate 		}
14270Sstevel@tonic-gate 		(void) printf("[0]\t%u\t%llu\t", EC_WORD(shdr0.sh_type),
1428*4734Sab196087 		    EC_XWORD(shdr0.sh_flags));
14290Sstevel@tonic-gate 
14302352Sab196087 		(void) printf("%-#*llx %-#*llx%-*llu%s%-*u\n",
1431*4734Sab196087 		    field, EC_ADDR(shdr0.sh_addr),
1432*4734Sab196087 		    field, EC_OFF(shdr0.sh_offset),
1433*4734Sab196087 		    field, EC_XWORD(shdr0.sh_size),
1434*4734Sab196087 		    /* compatibility:  tab for elf32 */
1435*4734Sab196087 		    ((field == 13) ? "\t" : "  "),
1436*4734Sab196087 		    field, EC_WORD(shdr0.sh_name));
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate 		(void) printf("\t%u\t%u\t%-#*llx %-#*llx\n",
1439*4734Sab196087 		    EC_WORD(shdr0.sh_link),
1440*4734Sab196087 		    EC_WORD(shdr0.sh_info),
1441*4734Sab196087 		    field, EC_XWORD(shdr0.sh_addralign),
1442*4734Sab196087 		    field, EC_XWORD(shdr0.sh_entsize));
14430Sstevel@tonic-gate 	}
14440Sstevel@tonic-gate 	(void) printf("\n");
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate 	return (elf_head_p);
14470Sstevel@tonic-gate }
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate /*
14500Sstevel@tonic-gate  * Print section contents.  Input is an ELF file descriptor,
14510Sstevel@tonic-gate  * the ELF header, the SCNTAB structure,
14520Sstevel@tonic-gate  * the number of symbols, and the filename.
14530Sstevel@tonic-gate  * The number of sections,
14540Sstevel@tonic-gate  * and the offset into the SCNTAB structure will be
14550Sstevel@tonic-gate  * set in dump_section if d_flag or n_flag are set.
14560Sstevel@tonic-gate  * If v_flag is set, sections which can be interpreted will
14570Sstevel@tonic-gate  * be interpreted, otherwise raw data will be output in hexidecimal.
14580Sstevel@tonic-gate  */
14590Sstevel@tonic-gate static void
14600Sstevel@tonic-gate print_section(Elf *elf_file,
14610Sstevel@tonic-gate 	GElf_Ehdr *p_ehdr, SCNTAB *p, int num_scns, char *filename)
14620Sstevel@tonic-gate {
14630Sstevel@tonic-gate 	unsigned char    *p_sec;
14640Sstevel@tonic-gate 	int	i;
14650Sstevel@tonic-gate 	size_t	size;
14660Sstevel@tonic-gate 
14670Sstevel@tonic-gate 	for (i = 0; i < num_scns; i++, p++) {
14680Sstevel@tonic-gate 		GElf_Shdr shdr;
14690Sstevel@tonic-gate 
14700Sstevel@tonic-gate 		size = 0;
14710Sstevel@tonic-gate 		if (s_flag && !v_flag)
14720Sstevel@tonic-gate 			p_sec = (unsigned char *)get_rawscn(p->p_sd, &size);
14730Sstevel@tonic-gate 		else
14740Sstevel@tonic-gate 			p_sec = (unsigned char *)get_scndata(p->p_sd, &size);
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 		if ((gelf_getshdr(p->p_sd, &shdr) != NULL) &&
14770Sstevel@tonic-gate 		    (shdr.sh_type == SHT_NOBITS)) {
14780Sstevel@tonic-gate 			continue;
14790Sstevel@tonic-gate 		}
14800Sstevel@tonic-gate 		if (s_flag && !v_flag) {
14810Sstevel@tonic-gate 			(void) printf("\n%s:\n", p->scn_name);
14820Sstevel@tonic-gate 			print_rawdata(p_sec, size);
14830Sstevel@tonic-gate 			continue;
14840Sstevel@tonic-gate 		}
14850Sstevel@tonic-gate 		if (shdr.sh_type == SHT_SYMTAB) {
14860Sstevel@tonic-gate 			dump_symbol_table(elf_file, p, filename);
14870Sstevel@tonic-gate 			continue;
14880Sstevel@tonic-gate 		}
14890Sstevel@tonic-gate 		if (shdr.sh_type == SHT_DYNSYM) {
14900Sstevel@tonic-gate 			dump_symbol_table(elf_file, p, filename);
14910Sstevel@tonic-gate 			continue;
14920Sstevel@tonic-gate 		}
14930Sstevel@tonic-gate 		if (shdr.sh_type == SHT_STRTAB) {
14940Sstevel@tonic-gate 			dump_string_table(p, 1);
14950Sstevel@tonic-gate 			continue;
14960Sstevel@tonic-gate 		}
14970Sstevel@tonic-gate 		if (shdr.sh_type == SHT_RELA) {
14980Sstevel@tonic-gate 			dump_reloc_table(elf_file, p_ehdr, p, 1, filename);
14990Sstevel@tonic-gate 			continue;
15000Sstevel@tonic-gate 		}
15010Sstevel@tonic-gate 		if (shdr.sh_type == SHT_REL) {
15020Sstevel@tonic-gate 			dump_reloc_table(elf_file, p_ehdr, p, 1, filename);
15030Sstevel@tonic-gate 			continue;
15040Sstevel@tonic-gate 		}
15050Sstevel@tonic-gate 		if (shdr.sh_type == SHT_DYNAMIC) {
15060Sstevel@tonic-gate 			dump_dynamic(elf_file, p, 1, filename);
15070Sstevel@tonic-gate 			continue;
15080Sstevel@tonic-gate 		}
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate 		(void) printf("\n%s:\n", p->scn_name);
15110Sstevel@tonic-gate 		print_rawdata(p_sec, size);
15120Sstevel@tonic-gate 	}
15130Sstevel@tonic-gate 	(void) printf("\n");
15140Sstevel@tonic-gate }
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate /*
15170Sstevel@tonic-gate  * Print section contents. This function does not print the contents
15180Sstevel@tonic-gate  * of the sections but sets up the parameters and then calls
15190Sstevel@tonic-gate  * print_section to print the contents.  Calling another function to print
15200Sstevel@tonic-gate  * the contents allows both -d and -n to work correctly
15210Sstevel@tonic-gate  * simultaneously. Input is an ELF file descriptor, the ELF header,
15220Sstevel@tonic-gate  * the SCNTAB structure, the number of sections, and the filename.
15230Sstevel@tonic-gate  * Set the range of sections if d_flag, and set section name if
15240Sstevel@tonic-gate  * n_flag.
15250Sstevel@tonic-gate  */
15260Sstevel@tonic-gate static void
15270Sstevel@tonic-gate dump_section(Elf *elf_file,
15280Sstevel@tonic-gate 	GElf_Ehdr *p_ehdr, SCNTAB *s, int num_scns, char *filename)
15290Sstevel@tonic-gate {
15300Sstevel@tonic-gate 	SCNTAB *n_range, *d_range; /* for use with -n and -d modifiers */
15310Sstevel@tonic-gate 	int i;
15320Sstevel@tonic-gate 	int found_it = 0;  /* for use with -n section_name */
15330Sstevel@tonic-gate 
15340Sstevel@tonic-gate 	if (n_flag) {
15350Sstevel@tonic-gate 		n_range = s;
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate 		for (i = 0; i < num_scns; i++, n_range++) {
15380Sstevel@tonic-gate 			if ((strcmp(name, n_range->scn_name)) != 0)
15390Sstevel@tonic-gate 				continue;
15400Sstevel@tonic-gate 			else {
15410Sstevel@tonic-gate 				found_it = 1;
15420Sstevel@tonic-gate 				print_section(elf_file, p_ehdr,
1543*4734Sab196087 				    n_range, 1, filename);
15440Sstevel@tonic-gate 			}
15450Sstevel@tonic-gate 		}
15460Sstevel@tonic-gate 
15470Sstevel@tonic-gate 		if (!found_it) {
15480Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s: %s not found\n",
1549*4734Sab196087 			    prog_name, filename, name);
15500Sstevel@tonic-gate 		}
15510Sstevel@tonic-gate 	} /* end n_flag */
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate 	if (d_flag) {
15540Sstevel@tonic-gate 		d_range = s;
15550Sstevel@tonic-gate 		d_num = check_range(d_low, d_hi, num_scns, filename);
15560Sstevel@tonic-gate 		if (d_num < 0)
15570Sstevel@tonic-gate 			return;
15580Sstevel@tonic-gate 		d_range += d_low - 1;
15590Sstevel@tonic-gate 
15600Sstevel@tonic-gate 		print_section(elf_file, p_ehdr, d_range, d_num, filename);
15610Sstevel@tonic-gate 	}	/* end d_flag */
15620Sstevel@tonic-gate 
15630Sstevel@tonic-gate 	if (!n_flag && !d_flag)
15640Sstevel@tonic-gate 		print_section(elf_file, p_ehdr, s, num_scns, filename);
15650Sstevel@tonic-gate }
15660Sstevel@tonic-gate 
15670Sstevel@tonic-gate /*
15680Sstevel@tonic-gate  * Print the section header table. This function does not print the contents
15690Sstevel@tonic-gate  * of the section headers but sets up the parameters and then calls
15700Sstevel@tonic-gate  * print_shdr to print the contents.  Calling another function to print
15710Sstevel@tonic-gate  * the contents allows both -d and -n to work correctly
15720Sstevel@tonic-gate  * simultaneously.  Input is the SCNTAB structure,
15730Sstevel@tonic-gate  * the number of sections from the ELF header, and the filename.
15740Sstevel@tonic-gate  * Set the range of section headers to print if d_flag, and set
15750Sstevel@tonic-gate  * name of section header to print if n_flag.
15760Sstevel@tonic-gate  */
15770Sstevel@tonic-gate static void
15780Sstevel@tonic-gate dump_shdr(Elf *elf_file, SCNTAB *s, int num_scns, char *filename)
15790Sstevel@tonic-gate {
15800Sstevel@tonic-gate 
15810Sstevel@tonic-gate 	SCNTAB *n_range, *d_range;	/* for use with -n and -d modifiers */
15820Sstevel@tonic-gate 	int field;
15830Sstevel@tonic-gate 	int i;
15840Sstevel@tonic-gate 	int found_it = 0;  /* for use with -n section_name */
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate 	if (gelf_getclass(elf_file) == ELFCLASS64)
15871618Srie 		field = 21;
15880Sstevel@tonic-gate 	else
15891618Srie 		field = 13;
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate 	if (!p_flag) {
15920Sstevel@tonic-gate 		(void) printf("\n	   **** SECTION HEADER TABLE ****\n");
15930Sstevel@tonic-gate 		(void) printf("[No]\tType\tFlags\t%-*s %-*s %-*s%sName\n",
15940Sstevel@tonic-gate 		    field, "Addr", field, "Offset", field, "Size",
15950Sstevel@tonic-gate 		    /* compatibility:  tab for elf32 */
15961618Srie 		    (field == 13) ? "\t" : "  ");
15970Sstevel@tonic-gate 		(void) printf("\tLink\tInfo\t%-*s Entsize\n\n",
15980Sstevel@tonic-gate 		    field, "Adralgn");
15990Sstevel@tonic-gate 	}
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate 	if (n_flag) {
16020Sstevel@tonic-gate 		n_range = s;
16030Sstevel@tonic-gate 
16040Sstevel@tonic-gate 		for (i = 1; i <= num_scns; i++, n_range++) {
16050Sstevel@tonic-gate 			if ((strcmp(name, n_range->scn_name)) != 0)
16060Sstevel@tonic-gate 				continue;
16070Sstevel@tonic-gate 			else {
16080Sstevel@tonic-gate 				found_it = 1;
16090Sstevel@tonic-gate 				print_shdr(elf_file, n_range, 1, i);
16100Sstevel@tonic-gate 			}
16110Sstevel@tonic-gate 		}
16120Sstevel@tonic-gate 
16130Sstevel@tonic-gate 		if (!found_it) {
16140Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s: %s not found\n",
1615*4734Sab196087 			    prog_name, filename, name);
16160Sstevel@tonic-gate 		}
16170Sstevel@tonic-gate 	} /* end n_flag */
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 	if (d_flag) {
16200Sstevel@tonic-gate 		d_range = s;
16210Sstevel@tonic-gate 		d_num = check_range(d_low, d_hi, num_scns, filename);
16220Sstevel@tonic-gate 		if (d_num < 0)
16230Sstevel@tonic-gate 			return;
16240Sstevel@tonic-gate 		d_range += d_low - 1;
16250Sstevel@tonic-gate 
16260Sstevel@tonic-gate 		print_shdr(elf_file, d_range, d_num, d_low);
16270Sstevel@tonic-gate 	}	/* end d_flag */
16280Sstevel@tonic-gate 
16290Sstevel@tonic-gate 	if (!n_flag && !d_flag)
16300Sstevel@tonic-gate 		print_shdr(elf_file, s, num_scns, 1);
16310Sstevel@tonic-gate }
16320Sstevel@tonic-gate 
16330Sstevel@tonic-gate /*
16340Sstevel@tonic-gate  * Process all of the command line options (except
16350Sstevel@tonic-gate  * for -a, -g, -f, and -o).  All of the options processed
16360Sstevel@tonic-gate  * by this function require the presence of the section
16370Sstevel@tonic-gate  * header table and will not be processed if it is not present.
16380Sstevel@tonic-gate  * Set up a buffer containing section name, section header,
16390Sstevel@tonic-gate  * and section descriptor for each section in the file.  This
16400Sstevel@tonic-gate  * structure is used to avoid duplicate calls to libelf functions.
16410Sstevel@tonic-gate  * Structure members for the symbol table, the debugging information,
16420Sstevel@tonic-gate  * and the line number information are global.  All of the
16430Sstevel@tonic-gate  * rest are local.
16440Sstevel@tonic-gate  */
16450Sstevel@tonic-gate static void
16460Sstevel@tonic-gate dump_section_table(Elf *elf_file, GElf_Ehdr *elf_head_p, char *filename)
16470Sstevel@tonic-gate {
16480Sstevel@tonic-gate 
16490Sstevel@tonic-gate 	static SCNTAB	*buffer, *p_scns;
16500Sstevel@tonic-gate 	Elf_Scn		*scn = 0;
16510Sstevel@tonic-gate 	char		*s_name = NULL;
16520Sstevel@tonic-gate 	int		found = 0;
16530Sstevel@tonic-gate 	unsigned int	num_scns;
16540Sstevel@tonic-gate 	size_t		shstrndx;
16550Sstevel@tonic-gate 	size_t		shnum;
16560Sstevel@tonic-gate 
16570Sstevel@tonic-gate 
16580Sstevel@tonic-gate 	if (elf_getshnum(elf_file, &shnum) == 0) {
16590Sstevel@tonic-gate 		(void) fprintf(stderr,
1660*4734Sab196087 		    "%s: %s: elf_getshnum failed: %s\n",
1661*4734Sab196087 		    prog_name, filename, elf_errmsg(-1));
16620Sstevel@tonic-gate 		return;
16630Sstevel@tonic-gate 	}
16640Sstevel@tonic-gate 	if (elf_getshstrndx(elf_file, &shstrndx) == 0) {
16650Sstevel@tonic-gate 		(void) fprintf(stderr,
1666*4734Sab196087 		    "%s: %s: elf_getshstrndx failed: %s\n",
1667*4734Sab196087 		    prog_name, filename, elf_errmsg(-1));
16680Sstevel@tonic-gate 		return;
16690Sstevel@tonic-gate 	}
16700Sstevel@tonic-gate 
16710Sstevel@tonic-gate 	if ((buffer = calloc(shnum, sizeof (SCNTAB))) == NULL) {
16720Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: cannot calloc space\n",
1673*4734Sab196087 		    prog_name, filename);
16740Sstevel@tonic-gate 		return;
16750Sstevel@tonic-gate 	}
16760Sstevel@tonic-gate 	/* LINTED */
16770Sstevel@tonic-gate 	num_scns = (int)shnum - 1;
16780Sstevel@tonic-gate 
16790Sstevel@tonic-gate 	p_symtab = (SCNTAB *)0;
16800Sstevel@tonic-gate 	p_dynsym = (SCNTAB *)0;
16810Sstevel@tonic-gate 	p_scns = buffer;
16820Sstevel@tonic-gate 	p_head_scns = buffer;
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate 	while ((scn = elf_nextscn(elf_file, scn)) != 0) {
16850Sstevel@tonic-gate 		if ((gelf_getshdr(scn, &buffer->p_shdr)) == 0) {
16860Sstevel@tonic-gate 			(void) fprintf(stderr,
1687*4734Sab196087 			    "%s: %s: %s\n", prog_name, filename,
1688*4734Sab196087 			    elf_errmsg(-1));
16890Sstevel@tonic-gate 			return;
16900Sstevel@tonic-gate 		}
1691*4734Sab196087 		s_name = (char *)
1692*4734Sab196087 		    elf_strptr(elf_file, shstrndx, buffer->p_shdr.sh_name);
16930Sstevel@tonic-gate 		buffer->scn_name = s_name ? s_name : (char *)UNKNOWN;
16940Sstevel@tonic-gate 		buffer->p_sd   =  scn;
16950Sstevel@tonic-gate 
16960Sstevel@tonic-gate 		if (buffer->p_shdr.sh_type == SHT_SYMTAB) {
16970Sstevel@tonic-gate 			found += 1;
16980Sstevel@tonic-gate 			p_symtab = buffer;
16990Sstevel@tonic-gate 		}
17000Sstevel@tonic-gate 		if (buffer->p_shdr.sh_type == SHT_DYNSYM)
17010Sstevel@tonic-gate 			p_dynsym = buffer;
17020Sstevel@tonic-gate 		buffer++;
17030Sstevel@tonic-gate 	}
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate 	/*
17060Sstevel@tonic-gate 	 * These functions depend upon the presence of the section header table
17070Sstevel@tonic-gate 	 * and will not be invoked in its absence
17080Sstevel@tonic-gate 	 */
17090Sstevel@tonic-gate 	if (h_flag) {
17100Sstevel@tonic-gate 		dump_shdr(elf_file, p_scns, num_scns, filename);
17110Sstevel@tonic-gate 	}
17120Sstevel@tonic-gate 	if (p_symtab && (t_flag || T_flag)) {
17130Sstevel@tonic-gate 		dump_symbol_table(elf_file, p_symtab, filename);
17140Sstevel@tonic-gate 	}
17150Sstevel@tonic-gate 	if (c_flag) {
17160Sstevel@tonic-gate 		dump_string_table(p_scns, num_scns);
17170Sstevel@tonic-gate 	}
17180Sstevel@tonic-gate 	if (r_flag) {
17190Sstevel@tonic-gate 		dump_reloc_table(elf_file, elf_head_p,
1720*4734Sab196087 		    p_scns, num_scns, filename);
17210Sstevel@tonic-gate 	}
17220Sstevel@tonic-gate 	if (L_flag) {
17230Sstevel@tonic-gate 		dump_dynamic(elf_file, p_scns, num_scns, filename);
17240Sstevel@tonic-gate 	}
17250Sstevel@tonic-gate 	if (s_flag) {
17260Sstevel@tonic-gate 		dump_section(elf_file, elf_head_p, p_scns,
1727*4734Sab196087 		    num_scns, filename);
17280Sstevel@tonic-gate 	}
17290Sstevel@tonic-gate }
17300Sstevel@tonic-gate 
17310Sstevel@tonic-gate /*
17320Sstevel@tonic-gate  * Load the archive string table(s) (for extended-length strings)
17330Sstevel@tonic-gate  * into an in-core table/list
17340Sstevel@tonic-gate  */
17350Sstevel@tonic-gate static struct stab_list_s *
17360Sstevel@tonic-gate load_arstring_table(struct stab_list_s *STabList,
17370Sstevel@tonic-gate 	int fd, Elf *elf_file, Elf_Arhdr *p_ar, char *filename)
17380Sstevel@tonic-gate {
17390Sstevel@tonic-gate 	off_t here;
17400Sstevel@tonic-gate 	struct stab_list_s *STL_entry, *STL_next;
17410Sstevel@tonic-gate 
17420Sstevel@tonic-gate 	if (p_ar) {
17430Sstevel@tonic-gate 		STL_entry = malloc(sizeof (struct stab_list_s));
17440Sstevel@tonic-gate 		STL_entry->next    = 0;
17450Sstevel@tonic-gate 		STL_entry->strings = 0;
17460Sstevel@tonic-gate 		STL_entry->size    = 0;
17470Sstevel@tonic-gate 
17480Sstevel@tonic-gate 		if (!STabList)
17490Sstevel@tonic-gate 			STabList = STL_entry;
17500Sstevel@tonic-gate 		else {
17510Sstevel@tonic-gate 			STL_next = STabList;
17520Sstevel@tonic-gate 			while (STL_next->next != (void *)0)
17530Sstevel@tonic-gate 				STL_next = STL_next->next;
17540Sstevel@tonic-gate 			STL_next->next = STL_entry;
17550Sstevel@tonic-gate 		}
17560Sstevel@tonic-gate 
17570Sstevel@tonic-gate 		STL_entry->size    = p_ar->ar_size;
17580Sstevel@tonic-gate 		STL_entry->strings = malloc(p_ar->ar_size);
17590Sstevel@tonic-gate 		here = elf_getbase(elf_file);
17600Sstevel@tonic-gate 		if ((lseek(fd, here, 0)) != here) {
17610Sstevel@tonic-gate 			(void) fprintf(stderr,
1762*4734Sab196087 			    "%s: %s: could not lseek\n", prog_name, filename);
17630Sstevel@tonic-gate 		}
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate 		if ((read(fd, STL_entry->strings, p_ar->ar_size)) == -1) {
17660Sstevel@tonic-gate 			(void) fprintf(stderr,
1767*4734Sab196087 			    "%s: %s: could not read\n", prog_name, filename);
17680Sstevel@tonic-gate 		}
17690Sstevel@tonic-gate 	}
17700Sstevel@tonic-gate 	return (STabList);
17710Sstevel@tonic-gate }
17720Sstevel@tonic-gate 
17730Sstevel@tonic-gate /*
17740Sstevel@tonic-gate  * Print the archive header for each member of an archive.
17750Sstevel@tonic-gate  * Also call ar_sym_read to print the symbols in the
17760Sstevel@tonic-gate  * archive symbol table if g_flag.  Input is a file descriptor,
17770Sstevel@tonic-gate  * an ELF file descriptor, and the filename.  Putting the call
17780Sstevel@tonic-gate  * to dump the archive symbol table in this function is more
17790Sstevel@tonic-gate  * efficient since it is necessary to examine the archive member
17800Sstevel@tonic-gate  * name in the archive header to determine which member is the
17810Sstevel@tonic-gate  * symbol table.
17820Sstevel@tonic-gate  */
17830Sstevel@tonic-gate static void
17840Sstevel@tonic-gate dump_ar_hdr(int fd, Elf *elf_file, char *filename)
17850Sstevel@tonic-gate {
17860Sstevel@tonic-gate 	extern int v_flag, g_flag, a_flag, p_flag;
17870Sstevel@tonic-gate 	Elf_Arhdr  *p_ar;
17880Sstevel@tonic-gate 	Elf *arf;
17890Sstevel@tonic-gate 	Elf_Cmd cmd;
17900Sstevel@tonic-gate 	int title = 0;
17910Sstevel@tonic-gate 	int err = 0;
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate 	char buf[DATESIZE];
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate 	cmd = ELF_C_READ;
17960Sstevel@tonic-gate 	while ((arf = elf_begin(fd, cmd, elf_file)) != 0) {
17970Sstevel@tonic-gate 		p_ar = elf_getarhdr(arf);
17980Sstevel@tonic-gate 		if (p_ar == NULL) {
17990Sstevel@tonic-gate 			(void) fprintf(stderr,
1800*4734Sab196087 			    "%s: %s: %s\n", prog_name, filename,
1801*4734Sab196087 			    elf_errmsg(-1));
18020Sstevel@tonic-gate 			continue;
18030Sstevel@tonic-gate 		}
18040Sstevel@tonic-gate 		if (strcmp(p_ar->ar_name, "/") == 0) {
18050Sstevel@tonic-gate 			if (g_flag)
18060Sstevel@tonic-gate 				ar_sym_read(elf_file, filename);
18070Sstevel@tonic-gate 		} else if (strcmp(p_ar->ar_name, "//") == 0) {
18080Sstevel@tonic-gate 			StringTableList = load_arstring_table(
1809*4734Sab196087 			    StringTableList, fd, arf, p_ar, filename);
18100Sstevel@tonic-gate 			cmd = elf_next(arf);
18110Sstevel@tonic-gate 			(void) elf_end(arf);
18120Sstevel@tonic-gate 			continue;
18130Sstevel@tonic-gate 		} else {
18140Sstevel@tonic-gate 			if (a_flag) {
18150Sstevel@tonic-gate 				(void) printf("%s[%s]:\n", filename,
1816*4734Sab196087 				    p_ar->ar_name);
18170Sstevel@tonic-gate 				if (!p_flag && title == 0) {
18180Sstevel@tonic-gate 					if (!v_flag)
18190Sstevel@tonic-gate 						(void) printf(
18200Sstevel@tonic-gate "\n\n\t\t\t***ARCHIVE HEADER***"
18210Sstevel@tonic-gate "\n	Date          Uid     Gid    Mode      Size	 Member Name\n\n");
18220Sstevel@tonic-gate 					else
18230Sstevel@tonic-gate 						(void) printf(
18240Sstevel@tonic-gate "\n\n\t\t\t***ARCHIVE HEADER***"
18250Sstevel@tonic-gate "\n	Date                   Uid    Gid   Mode     Size     Member Name\n\n");
18260Sstevel@tonic-gate 					title = 1;
18270Sstevel@tonic-gate 				}
18280Sstevel@tonic-gate 				if (!v_flag) {
18290Sstevel@tonic-gate 					(void) printf(
18300Sstevel@tonic-gate "\t0x%.8lx  %6d  %6d  0%.6ho  0x%.8lx  %-s\n\n",
1831*4734Sab196087 					    p_ar->ar_date, (int)p_ar->ar_uid,
1832*4734Sab196087 					    (int)p_ar->ar_gid,
1833*4734Sab196087 					    (int)p_ar->ar_mode,
1834*4734Sab196087 					    p_ar->ar_size, p_ar->ar_name);
18350Sstevel@tonic-gate 				} else {
18360Sstevel@tonic-gate 					if ((strftime(buf, DATESIZE,
18370Sstevel@tonic-gate 					    "%b %d %H:%M:%S %Y",
18380Sstevel@tonic-gate 					    localtime(
18390Sstevel@tonic-gate 					    &(p_ar->ar_date)))) == 0) {
18400Sstevel@tonic-gate 						(void) fprintf(stderr,
18410Sstevel@tonic-gate "%s: %s: don't have enough space to store the date\n", prog_name, filename);
18420Sstevel@tonic-gate 						exit(1);
18430Sstevel@tonic-gate 					}
18440Sstevel@tonic-gate 					(void) printf(
1845*4734Sab196087 "\t%s %6d %6d 0%.6ho 0x%.8lx %-s\n\n",
1846*4734Sab196087 					    buf, (int)p_ar->ar_uid,
1847*4734Sab196087 					    (int)p_ar->ar_gid,
1848*4734Sab196087 					    (int)p_ar->ar_mode,
1849*4734Sab196087 					    p_ar->ar_size, p_ar->ar_name);
18500Sstevel@tonic-gate 				}
18510Sstevel@tonic-gate 			}
18520Sstevel@tonic-gate 		}
18530Sstevel@tonic-gate 		cmd = elf_next(arf);
18540Sstevel@tonic-gate 		(void) elf_end(arf);
18550Sstevel@tonic-gate 	} /* end while */
18560Sstevel@tonic-gate 
18570Sstevel@tonic-gate 	err = elf_errno();
18580Sstevel@tonic-gate 	if (err != 0) {
18590Sstevel@tonic-gate 		(void) fprintf(stderr,
1860*4734Sab196087 		    "%s: %s: %s\n", prog_name, filename, elf_errmsg(err));
18610Sstevel@tonic-gate 	}
18620Sstevel@tonic-gate }
18630Sstevel@tonic-gate 
18640Sstevel@tonic-gate /*
18650Sstevel@tonic-gate  * Process member files of an archive.  This function provides
18660Sstevel@tonic-gate  * a loop through an archive equivalent the processing of
18670Sstevel@tonic-gate  * each_file for individual object files.
18680Sstevel@tonic-gate  */
18690Sstevel@tonic-gate static void
18700Sstevel@tonic-gate dump_ar_files(int fd, Elf *elf_file, char *filename)
18710Sstevel@tonic-gate {
18720Sstevel@tonic-gate 	Elf_Arhdr  *p_ar;
18730Sstevel@tonic-gate 	Elf *arf;
18740Sstevel@tonic-gate 	Elf_Cmd cmd;
18750Sstevel@tonic-gate 	Elf_Kind file_type;
18760Sstevel@tonic-gate 	GElf_Ehdr elf_head;
18770Sstevel@tonic-gate 	char *fullname;
18780Sstevel@tonic-gate 
18790Sstevel@tonic-gate 	cmd = ELF_C_READ;
18800Sstevel@tonic-gate 	while ((arf = elf_begin(fd, cmd, elf_file)) != 0) {
18811618Srie 		size_t	len;
18821618Srie 
18830Sstevel@tonic-gate 		p_ar = elf_getarhdr(arf);
18840Sstevel@tonic-gate 		if (p_ar == NULL) {
1885*4734Sab196087 			(void) fprintf(stderr, "%s: %s: %s\n",
1886*4734Sab196087 			    prog_name, filename, elf_errmsg(-1));
18870Sstevel@tonic-gate 			return;
18880Sstevel@tonic-gate 		}
18890Sstevel@tonic-gate 		if ((strcmp(p_ar->ar_name, "/") == 0) ||
1890*4734Sab196087 		    (strcmp(p_ar->ar_name, "//") == 0)) {
18910Sstevel@tonic-gate 			cmd = elf_next(arf);
18920Sstevel@tonic-gate 			(void) elf_end(arf);
18930Sstevel@tonic-gate 			continue;
18940Sstevel@tonic-gate 		}
18950Sstevel@tonic-gate 
18961618Srie 		len = strlen(filename) + strlen(p_ar->ar_name) + 3;
18971618Srie 		if ((fullname = malloc(len)) == NULL)
18981618Srie 			return;
18991618Srie 		(void) snprintf(fullname, len, "%s[%s]", filename,
19001618Srie 		    p_ar->ar_name);
19010Sstevel@tonic-gate 		(void) printf("\n%s:\n", fullname);
19020Sstevel@tonic-gate 		file_type = elf_kind(arf);
19030Sstevel@tonic-gate 		if (file_type == ELF_K_ELF) {
19040Sstevel@tonic-gate 			if (dump_elf_header(arf, fullname, &elf_head) == NULL)
19050Sstevel@tonic-gate 				return;
19060Sstevel@tonic-gate 			if (o_flag)
19070Sstevel@tonic-gate 				dump_exec_header(arf,
1908*4734Sab196087 				    (unsigned)elf_head.e_phnum, fullname);
19090Sstevel@tonic-gate 			if (x_flag)
19100Sstevel@tonic-gate 				dump_section_table(arf, &elf_head, fullname);
19110Sstevel@tonic-gate 		} else {
1912*4734Sab196087 			(void) fprintf(stderr, "%s: %s: invalid file type\n",
1913*4734Sab196087 			    prog_name, fullname);
19140Sstevel@tonic-gate 			cmd = elf_next(arf);
19150Sstevel@tonic-gate 			(void) elf_end(arf);
19160Sstevel@tonic-gate 			continue;
19170Sstevel@tonic-gate 		}
19180Sstevel@tonic-gate 
19190Sstevel@tonic-gate 		cmd = elf_next(arf);
19200Sstevel@tonic-gate 		(void) elf_end(arf);
19210Sstevel@tonic-gate 	} /* end while */
19220Sstevel@tonic-gate }
19230Sstevel@tonic-gate 
19240Sstevel@tonic-gate /*
19250Sstevel@tonic-gate  * Takes a filename as input.  Test first for a valid version
19260Sstevel@tonic-gate  * of libelf.a and exit on error.  Process each valid file
19270Sstevel@tonic-gate  * or archive given as input on the command line.  Check
19280Sstevel@tonic-gate  * for file type.  If it is an archive, process the archive-
19290Sstevel@tonic-gate  * specific options first, then files within the archive.
19300Sstevel@tonic-gate  * If it is an ELF object file, process it; otherwise
19310Sstevel@tonic-gate  * warn that it is an invalid file type.
19320Sstevel@tonic-gate  * All options except the archive-specific and program
19330Sstevel@tonic-gate  * execution header are processed in the function, dump_section_table.
19340Sstevel@tonic-gate  */
19350Sstevel@tonic-gate static void
19360Sstevel@tonic-gate each_file(char *filename)
19370Sstevel@tonic-gate {
19380Sstevel@tonic-gate 	Elf *elf_file;
19390Sstevel@tonic-gate 	GElf_Ehdr elf_head;
19400Sstevel@tonic-gate 	int fd;
19410Sstevel@tonic-gate 	Elf_Kind   file_type;
19420Sstevel@tonic-gate 
19430Sstevel@tonic-gate 	struct stat buf;
19440Sstevel@tonic-gate 
19450Sstevel@tonic-gate 	Elf_Cmd cmd;
19460Sstevel@tonic-gate 	errno = 0;
19470Sstevel@tonic-gate 
19480Sstevel@tonic-gate 	if (stat(filename, &buf) == -1) {
19491618Srie 		int	err = errno;
19501618Srie 		(void) fprintf(stderr, "%s: %s: %s", prog_name, filename,
19511618Srie 		    strerror(err));
19520Sstevel@tonic-gate 		return;
19530Sstevel@tonic-gate 	}
19540Sstevel@tonic-gate 
19550Sstevel@tonic-gate 	if ((fd = open((filename), O_RDONLY)) == -1) {
19561618Srie 		(void) fprintf(stderr, "%s: %s: cannot read\n", prog_name,
19571618Srie 		    filename);
19580Sstevel@tonic-gate 		return;
19590Sstevel@tonic-gate 	}
19600Sstevel@tonic-gate 	cmd = ELF_C_READ;
19610Sstevel@tonic-gate 	if ((elf_file = elf_begin(fd, cmd, (Elf *)0)) == NULL) {
19621618Srie 		(void) fprintf(stderr, "%s: %s: %s\n", prog_name, filename,
19631618Srie 		    elf_errmsg(-1));
19640Sstevel@tonic-gate 		return;
19650Sstevel@tonic-gate 	}
19660Sstevel@tonic-gate 
19670Sstevel@tonic-gate 	file_type = elf_kind(elf_file);
19680Sstevel@tonic-gate 	if (file_type == ELF_K_AR) {
19690Sstevel@tonic-gate 		if (a_flag || g_flag) {
19700Sstevel@tonic-gate 			dump_ar_hdr(fd, elf_file, filename);
19710Sstevel@tonic-gate 			elf_file = elf_begin(fd, cmd, (Elf *)0);
19720Sstevel@tonic-gate 		}
19730Sstevel@tonic-gate 		if (z_flag)
19740Sstevel@tonic-gate 			dump_ar_files(fd, elf_file, filename);
19750Sstevel@tonic-gate 	} else {
19760Sstevel@tonic-gate 		if (file_type == ELF_K_ELF) {
19770Sstevel@tonic-gate 			(void) printf("\n%s:\n", filename);
19781618Srie 			if (dump_elf_header(elf_file, filename, &elf_head)) {
19791618Srie 				if (o_flag)
19801618Srie 					dump_exec_header(elf_file,
19811618Srie 					    (unsigned)elf_head.e_phnum,
19821618Srie 					    filename);
19831618Srie 				if (x_flag)
19841618Srie 					dump_section_table(elf_file,
19851618Srie 					    &elf_head, filename);
19860Sstevel@tonic-gate 			}
19870Sstevel@tonic-gate 		} else {
19880Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s: invalid file type\n",
19891618Srie 			    prog_name, filename);
19900Sstevel@tonic-gate 		}
19910Sstevel@tonic-gate 	}
19920Sstevel@tonic-gate 	(void) elf_end(elf_file);
19930Sstevel@tonic-gate 	(void) close(fd);
19940Sstevel@tonic-gate }
19950Sstevel@tonic-gate 
19960Sstevel@tonic-gate /*
19970Sstevel@tonic-gate  * Sets up flags for command line options given and then
19980Sstevel@tonic-gate  * calls each_file() to process each file.
19990Sstevel@tonic-gate  */
20000Sstevel@tonic-gate int
20010Sstevel@tonic-gate main(int argc, char *argv[], char *envp[])
20020Sstevel@tonic-gate {
20030Sstevel@tonic-gate 	char *optstr = OPTSTR; /* option string used by getopt() */
20040Sstevel@tonic-gate 	int optchar;
20050Sstevel@tonic-gate 
20060Sstevel@tonic-gate 	/*
20070Sstevel@tonic-gate 	 * Check for a binary that better fits this architecture.
20080Sstevel@tonic-gate 	 */
20092647Srie 	(void) conv_check_native(argv, envp);
20100Sstevel@tonic-gate 
20110Sstevel@tonic-gate 	prog_name = argv[0];
20120Sstevel@tonic-gate 
20130Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
20140Sstevel@tonic-gate 	while ((optchar = getopt(argc, argv, optstr)) != -1) {
20150Sstevel@tonic-gate 		switch (optchar) {
20160Sstevel@tonic-gate 		case 'a':
20170Sstevel@tonic-gate 			a_flag = 1;
20180Sstevel@tonic-gate 			x_flag = 1;
20190Sstevel@tonic-gate 			break;
20200Sstevel@tonic-gate 		case 'g':
20210Sstevel@tonic-gate 			g_flag = 1;
20220Sstevel@tonic-gate 			x_flag = 1;
20230Sstevel@tonic-gate 			break;
20240Sstevel@tonic-gate 		case 'v':
20250Sstevel@tonic-gate 			v_flag = 1;
20260Sstevel@tonic-gate 			break;
20270Sstevel@tonic-gate 		case 'p':
20280Sstevel@tonic-gate 			p_flag = 1;
20290Sstevel@tonic-gate 			break;
20300Sstevel@tonic-gate 		case 'f':
20310Sstevel@tonic-gate 			f_flag = 1;
20320Sstevel@tonic-gate 			z_flag = 1;
20330Sstevel@tonic-gate 			break;
20340Sstevel@tonic-gate 		case 'o':
20350Sstevel@tonic-gate 			o_flag = 1;
20360Sstevel@tonic-gate 			z_flag = 1;
20370Sstevel@tonic-gate 			break;
20380Sstevel@tonic-gate 		case 'h':
20390Sstevel@tonic-gate 			h_flag = 1;
20400Sstevel@tonic-gate 			x_flag = 1;
20410Sstevel@tonic-gate 			z_flag = 1;
20420Sstevel@tonic-gate 			break;
20430Sstevel@tonic-gate 		case 's':
20440Sstevel@tonic-gate 			s_flag = 1;
20450Sstevel@tonic-gate 			x_flag = 1;
20460Sstevel@tonic-gate 			z_flag = 1;
20470Sstevel@tonic-gate 			break;
20480Sstevel@tonic-gate 		case 'd':
20490Sstevel@tonic-gate 			d_flag = 1;
20500Sstevel@tonic-gate 			x_flag = 1;
20510Sstevel@tonic-gate 			z_flag = 1;
20520Sstevel@tonic-gate 			set_range(optarg, &d_low, &d_hi);
20530Sstevel@tonic-gate 			break;
20540Sstevel@tonic-gate 		case 'n':
20550Sstevel@tonic-gate 			n_flag++;
20560Sstevel@tonic-gate 			x_flag = 1;
20570Sstevel@tonic-gate 			z_flag = 1;
20580Sstevel@tonic-gate 			name = optarg;
20590Sstevel@tonic-gate 			break;
20600Sstevel@tonic-gate 		case 'r':
20610Sstevel@tonic-gate 			r_flag = 1;
20620Sstevel@tonic-gate 			x_flag = 1;
20630Sstevel@tonic-gate 			z_flag = 1;
20640Sstevel@tonic-gate 			break;
20650Sstevel@tonic-gate 		case 't':
20660Sstevel@tonic-gate 			t_flag = 1;
20670Sstevel@tonic-gate 			x_flag = 1;
20680Sstevel@tonic-gate 			z_flag = 1;
20690Sstevel@tonic-gate 			break;
20700Sstevel@tonic-gate 		case 'C':
20710Sstevel@tonic-gate 			C_flag = 1;
20720Sstevel@tonic-gate 			t_flag = 1;
20730Sstevel@tonic-gate 			x_flag = 1;
20740Sstevel@tonic-gate 			z_flag = 1;
20750Sstevel@tonic-gate 			break;
20760Sstevel@tonic-gate 		case 'T':
20770Sstevel@tonic-gate 			T_flag = 1;
20780Sstevel@tonic-gate 			x_flag = 1;
20790Sstevel@tonic-gate 			z_flag = 1;
20800Sstevel@tonic-gate 			set_range(optarg, &T_low, &T_hi);
20810Sstevel@tonic-gate 			break;
20820Sstevel@tonic-gate 		case 'c':
20830Sstevel@tonic-gate 			c_flag = 1;
20840Sstevel@tonic-gate 			x_flag = 1;
20850Sstevel@tonic-gate 			z_flag = 1;
20860Sstevel@tonic-gate 			break;
20870Sstevel@tonic-gate 		case 'L':
20880Sstevel@tonic-gate 			L_flag = 1;
20890Sstevel@tonic-gate 			x_flag = 1;
20900Sstevel@tonic-gate 			z_flag = 1;
20910Sstevel@tonic-gate 			break;
20920Sstevel@tonic-gate 		case 'V':
20930Sstevel@tonic-gate 			V_flag = 1;
20940Sstevel@tonic-gate 			(void) fprintf(stderr, "dump: %s %s\n",
20950Sstevel@tonic-gate 			    (const char *)SGU_PKG,
20960Sstevel@tonic-gate 			    (const char *)SGU_REL);
20970Sstevel@tonic-gate 			break;
20980Sstevel@tonic-gate 		case '?':
20990Sstevel@tonic-gate 			errflag += 1;
21000Sstevel@tonic-gate 			break;
21010Sstevel@tonic-gate 		default:
21020Sstevel@tonic-gate 			break;
21030Sstevel@tonic-gate 		}
21040Sstevel@tonic-gate 	}
21050Sstevel@tonic-gate 
21060Sstevel@tonic-gate 	if (errflag || (optind >= argc) || (!z_flag && !x_flag)) {
21070Sstevel@tonic-gate 		if (!(V_flag && (argc == 2))) {
21080Sstevel@tonic-gate 			usage();
21090Sstevel@tonic-gate 			exit(269);
21100Sstevel@tonic-gate 		}
21110Sstevel@tonic-gate 	}
21120Sstevel@tonic-gate 
21131618Srie 	if (elf_version(EV_CURRENT) == EV_NONE) {
21141618Srie 		(void) fprintf(stderr, "%s: libelf is out of date\n",
21151618Srie 		    prog_name);
21161618Srie 		exit(101);
21171618Srie 	}
21181618Srie 
21190Sstevel@tonic-gate 	while (optind < argc) {
21200Sstevel@tonic-gate 		each_file(argv[optind]);
21210Sstevel@tonic-gate 		optind++;
21220Sstevel@tonic-gate 	}
21230Sstevel@tonic-gate 	return (0);
21240Sstevel@tonic-gate }
2125