xref: /onnv-gate/usr/src/cmd/sgs/dump/common/dump.c (revision 1698)
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  *
261618Srie  * Copyright 2006 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 
320Sstevel@tonic-gate #include <stdio.h>
330Sstevel@tonic-gate #include <stdlib.h>
340Sstevel@tonic-gate #include <locale.h>
350Sstevel@tonic-gate #include <unistd.h>
360Sstevel@tonic-gate #include <libelf.h>
370Sstevel@tonic-gate #include <link.h>
380Sstevel@tonic-gate #include <sys/elf_M32.h>
390Sstevel@tonic-gate #include <sys/elf_386.h>
400Sstevel@tonic-gate #include <sys/elf_SPARC.h>
411618Srie #include <sys/elf_amd64.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 
540Sstevel@tonic-gate const char *UNKNOWN = "<unknown>";
550Sstevel@tonic-gate 
560Sstevel@tonic-gate static SCNTAB *p_symtab, *p_head_scns, *p_dynsym;
570Sstevel@tonic-gate 
580Sstevel@tonic-gate static int
590Sstevel@tonic-gate 	x_flag = 0,	/* option requires section header table */
600Sstevel@tonic-gate 	z_flag = 0,	/* process files within an archive */
610Sstevel@tonic-gate 	rn_flag = 0;	/* dump named relocation information */
620Sstevel@tonic-gate 
630Sstevel@tonic-gate static int
640Sstevel@tonic-gate 	/* flags: ?_flag corresponds to ? option */
650Sstevel@tonic-gate 	a_flag = 0,	/* dump archive header of each member of archive */
660Sstevel@tonic-gate 	g_flag = 0,	/* dump archive symbol table */
670Sstevel@tonic-gate 	c_flag = 0,	/* dump the string table */
680Sstevel@tonic-gate 	d_flag = 0,	/* dump range of sections */
690Sstevel@tonic-gate 	f_flag = 0,	/* dump each file header */
700Sstevel@tonic-gate 	h_flag = 0,	/* dump section headers */
710Sstevel@tonic-gate 	n_flag = 0,	/* dump named section */
720Sstevel@tonic-gate 	o_flag = 0,	/* dump each program execution header */
730Sstevel@tonic-gate 	r_flag = 0,	/* dump relocation information */
740Sstevel@tonic-gate 	s_flag = 0,	/* dump section contents */
750Sstevel@tonic-gate 	t_flag = 0,	/* dump symbol table entries */
760Sstevel@tonic-gate 	C_flag = 0,	/* dump decoded C++ symbol names */
770Sstevel@tonic-gate 	L_flag = 0,	/* dump dynamic linking information */
780Sstevel@tonic-gate 	T_flag = 0,	/* dump symbol table range */
790Sstevel@tonic-gate 	V_flag = 0;	/* dump version information */
800Sstevel@tonic-gate 
810Sstevel@tonic-gate int	p_flag = 0,	/* suppress printing of headings */
820Sstevel@tonic-gate 	v_flag = 0;	/* print information in verbose form */
830Sstevel@tonic-gate 
840Sstevel@tonic-gate static int
850Sstevel@tonic-gate 	d_low = 0,	/* range for use with -d */
860Sstevel@tonic-gate 	d_hi = 0,
870Sstevel@tonic-gate 	d_num = 0;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate static int
900Sstevel@tonic-gate 	T_low = 0,	/* range for use with -T */
910Sstevel@tonic-gate 	T_hi = 0,
920Sstevel@tonic-gate 	T_num = 0;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate static char *name = NULL; /* for use with -n option */
950Sstevel@tonic-gate char *prog_name;
960Sstevel@tonic-gate static int errflag = 0;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate static struct stab_list_s {
990Sstevel@tonic-gate 	struct stab_list_s *next;
1000Sstevel@tonic-gate 	char *strings;
1010Sstevel@tonic-gate 	size_t size;
1020Sstevel@tonic-gate } *StringTableList = (void *)0;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate extern void ar_sym_read();
1050Sstevel@tonic-gate extern void dump_exec_header();
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate /*
1090Sstevel@tonic-gate  * Get the section descriptor and set the size of the
1100Sstevel@tonic-gate  * data returned.  Data is byte-order converted.
1110Sstevel@tonic-gate  */
1120Sstevel@tonic-gate void *
1130Sstevel@tonic-gate get_scndata(Elf_Scn *fd_scn, size_t *size)
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate 	Elf_Data *p_data;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	p_data = 0;
1180Sstevel@tonic-gate 	if ((p_data = elf_getdata(fd_scn, p_data)) == 0 ||
1190Sstevel@tonic-gate 	    p_data->d_size == 0) {
1200Sstevel@tonic-gate 		return (NULL);
1210Sstevel@tonic-gate 	}
1220Sstevel@tonic-gate 	*size = p_data->d_size;
1230Sstevel@tonic-gate 	return (p_data->d_buf);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate /*
1270Sstevel@tonic-gate  * Get the section descriptor and set the size of the
1280Sstevel@tonic-gate  * data returned.  Data is raw (i.e., not byte-order converted).
1290Sstevel@tonic-gate  */
1300Sstevel@tonic-gate static void *
1310Sstevel@tonic-gate get_rawscn(Elf_Scn *fd_scn, size_t *size)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate 	Elf_Data *p_data;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	p_data = 0;
1360Sstevel@tonic-gate 	if ((p_data = elf_rawdata(fd_scn, p_data)) == 0 ||
1370Sstevel@tonic-gate 	    p_data->d_size == 0) {
1380Sstevel@tonic-gate 		return (NULL);
1390Sstevel@tonic-gate 	}
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	*size = p_data->d_size;
1420Sstevel@tonic-gate 	return (p_data->d_buf);
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate /*
1460Sstevel@tonic-gate  * Print out a usage message in short form when program is invoked
1470Sstevel@tonic-gate  * with insufficient or no arguments, and in long form when given
1480Sstevel@tonic-gate  * either a ? or an invalid option.
1490Sstevel@tonic-gate  */
1500Sstevel@tonic-gate static void
1510Sstevel@tonic-gate usage()
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate 	(void) fprintf(stderr,
1540Sstevel@tonic-gate 	"Usage: %s [-%s] file(s) ...\n", prog_name, OPTSTR);
1550Sstevel@tonic-gate 	if (errflag) {
1560Sstevel@tonic-gate 		(void) fprintf(stderr,
1570Sstevel@tonic-gate 		"\t\t[-a dump archive header of each member of archive]\n\
1580Sstevel@tonic-gate 		[-g dump archive global symbol table]\n\
1590Sstevel@tonic-gate 		[-c dump the string table]\n\
1600Sstevel@tonic-gate 		[-d dump range of sections]\n\
1610Sstevel@tonic-gate 		[-f dump each file header]\n\
1620Sstevel@tonic-gate 		[-h dump section headers]\n\
1630Sstevel@tonic-gate 		[-n dump named section]\n\
1640Sstevel@tonic-gate 		[-o dump each program execution header]\n\
1650Sstevel@tonic-gate 		[-p suppress printing of headings]\n\
1660Sstevel@tonic-gate 		[-r dump relocation information]\n\
1670Sstevel@tonic-gate 		[-s dump section contents]\n\
1680Sstevel@tonic-gate 		[-t dump symbol table entries]\n\
1690Sstevel@tonic-gate 		[-v print information in verbose form]\n\
1700Sstevel@tonic-gate 		[-C dump decoded C++ symbol names]\n\
1710Sstevel@tonic-gate 		[-L dump the .dynamic structure]\n\
1720Sstevel@tonic-gate 		[-T dump symbol table range]\n\
1730Sstevel@tonic-gate 		[-V dump version information]\n");
1740Sstevel@tonic-gate 	}
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate /*
1780Sstevel@tonic-gate  * Set a range.  Input is a character string, a lower
1790Sstevel@tonic-gate  * bound and an upper bound.  This function converts
1800Sstevel@tonic-gate  * a character string into its correct integer values,
1810Sstevel@tonic-gate  * setting the first value as the lower bound, and
1820Sstevel@tonic-gate  * the second value as the upper bound.  If more values
1830Sstevel@tonic-gate  * are given they are ignored with a warning.
1840Sstevel@tonic-gate  */
1850Sstevel@tonic-gate static void
1860Sstevel@tonic-gate set_range(char *s, int  *low, int  *high)
1870Sstevel@tonic-gate {
1880Sstevel@tonic-gate 	char *w;
1890Sstevel@tonic-gate 	char *lasts;
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	while ((w = strtok_r(s, ",", &lasts)) != NULL) {
1920Sstevel@tonic-gate 		if (!(*low))
1930Sstevel@tonic-gate 			/* LINTED */
1940Sstevel@tonic-gate 			*low = (int)atol(w);
1950Sstevel@tonic-gate 		else
1960Sstevel@tonic-gate 			if (!(*high))
1970Sstevel@tonic-gate 				/* LINTED */
1980Sstevel@tonic-gate 				*high = (int)atol(w);
1990Sstevel@tonic-gate 			else {
2000Sstevel@tonic-gate 				(void) fprintf(stderr,
2010Sstevel@tonic-gate 					"%s: too many arguments - %s ignored\n",
2020Sstevel@tonic-gate 					prog_name, w);
2030Sstevel@tonic-gate 				return;
2040Sstevel@tonic-gate 			}
2050Sstevel@tonic-gate 		s = NULL;
2060Sstevel@tonic-gate 	} /* end while */
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate /*
2110Sstevel@tonic-gate  * Print static shared library information.
2120Sstevel@tonic-gate  */
2130Sstevel@tonic-gate static void
2140Sstevel@tonic-gate print_static(SCNTAB *l_scns, char *filename)
2150Sstevel@tonic-gate {
2160Sstevel@tonic-gate 	size_t section_size;
2170Sstevel@tonic-gate 	unsigned char *strtab;
2180Sstevel@tonic-gate 	unsigned char *path, buf[1024];
2190Sstevel@tonic-gate 	unsigned long *temp;
2200Sstevel@tonic-gate 	unsigned long total, topath;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	(void) printf("\n  **** STATIC SHARED LIBRARY INFORMATION ****\n");
2230Sstevel@tonic-gate 	(void) printf("\n%s:\n", filename);
2240Sstevel@tonic-gate 	(void) printf("\t");
2250Sstevel@tonic-gate 	section_size  = 0;
2260Sstevel@tonic-gate 	if ((strtab = (unsigned char *)
2270Sstevel@tonic-gate 	    get_scndata(l_scns->p_sd, &section_size)) == NULL) {
2280Sstevel@tonic-gate 		return;
2290Sstevel@tonic-gate 	}
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	while (section_size != 0) {
2320Sstevel@tonic-gate 		/* LINTED */
2330Sstevel@tonic-gate 		temp = (unsigned long *)strtab;
2340Sstevel@tonic-gate 		total = temp[0];
2350Sstevel@tonic-gate 		topath = temp[1];
2360Sstevel@tonic-gate 		path = strtab + (topath*sizeof (long));
2370Sstevel@tonic-gate 		(void) strncpy((char *)buf, (char *)path,
2380Sstevel@tonic-gate 			(total - topath)*sizeof (long));
2390Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\n", buf);
2400Sstevel@tonic-gate 		strtab += total*sizeof (long);
2410Sstevel@tonic-gate 		section_size -= (total*sizeof (long));
2420Sstevel@tonic-gate 	}
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate /*
2460Sstevel@tonic-gate  * Print raw data in hexidecimal.  Input is the section data to
2470Sstevel@tonic-gate  * be printed out and the size of the data.  Output is relative
2480Sstevel@tonic-gate  * to a table lookup in dumpmap.h.
2490Sstevel@tonic-gate  */
2500Sstevel@tonic-gate static void
2510Sstevel@tonic-gate print_rawdata(unsigned char *p_sec, size_t size)
2520Sstevel@tonic-gate {
2530Sstevel@tonic-gate 	size_t   j;
2540Sstevel@tonic-gate 	size_t   count;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	count = 1;
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	(void) printf("\t");
2590Sstevel@tonic-gate 	for (j = size/sizeof (short); j != 0; --j, ++count) {
2600Sstevel@tonic-gate 		(void) printf("%.2x %.2x ", p_sec[0], p_sec[1]);
2610Sstevel@tonic-gate 		p_sec += 2;
2620Sstevel@tonic-gate 		if (count == 12) {
2630Sstevel@tonic-gate 			(void) printf("\n\t");
2640Sstevel@tonic-gate 			count = 0;
2650Sstevel@tonic-gate 		}
2660Sstevel@tonic-gate 	}
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	/*
2690Sstevel@tonic-gate 	 * take care of last byte if odd byte section
2700Sstevel@tonic-gate 	 */
2710Sstevel@tonic-gate 	if ((size & 0x1L) == 1L)
2720Sstevel@tonic-gate 		(void) printf("%.2x", *p_sec);
2730Sstevel@tonic-gate 	(void) printf("\n");
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate /*
2790Sstevel@tonic-gate  * Print relocation data of type SHT_RELA
2800Sstevel@tonic-gate  * If d_flag, print data corresponding only to
2810Sstevel@tonic-gate  * the section or range of sections specified.
2820Sstevel@tonic-gate  * If n_flag, print data corresponding only to
2830Sstevel@tonic-gate  * the named section.
2840Sstevel@tonic-gate  */
2850Sstevel@tonic-gate static void
2860Sstevel@tonic-gate print_rela(Elf *elf_file, SCNTAB *p_scns, Elf_Data *rdata, Elf_Data *sym_data,
2870Sstevel@tonic-gate 	GElf_Ehdr * p_ehdr, size_t reloc_size, size_t sym_size, char *filename,
2880Sstevel@tonic-gate 	SCNTAB *reloc_symtab)
2890Sstevel@tonic-gate {
2900Sstevel@tonic-gate 	GElf_Rela rela;
2910Sstevel@tonic-gate 	GElf_Sym sym;
2920Sstevel@tonic-gate 	size_t no_entries;
2930Sstevel@tonic-gate 	size_t rel_entsize;
2940Sstevel@tonic-gate 	size_t no_syms;
2950Sstevel@tonic-gate 	int type, symid;
2960Sstevel@tonic-gate 	static int n_title = 0;
2970Sstevel@tonic-gate 	int ndx = 0;
2980Sstevel@tonic-gate 	char *sym_name;
2990Sstevel@tonic-gate 	int adj = 0;
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	if (gelf_getclass(elf_file) == ELFCLASS64)
3021618Srie 		adj = 8;
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	rel_entsize = p_scns->p_shdr.sh_entsize;
3050Sstevel@tonic-gate 	if ((rel_entsize == 0) ||
3060Sstevel@tonic-gate 	    (rel_entsize > p_scns->p_shdr.sh_size)) {
3070Sstevel@tonic-gate 		rel_entsize = gelf_fsize(elf_file, ELF_T_RELA, 1,
3080Sstevel@tonic-gate 		    EV_CURRENT);
3090Sstevel@tonic-gate 	}
3100Sstevel@tonic-gate 	no_entries = reloc_size / rel_entsize;
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	no_syms = sym_size / gelf_fsize(elf_file, ELF_T_SYM, 1, EV_CURRENT);
3130Sstevel@tonic-gate 	while (no_entries--) {
3140Sstevel@tonic-gate 		(void) gelf_getrela(rdata, ndx, &rela);
3150Sstevel@tonic-gate 		/* LINTED */
3160Sstevel@tonic-gate 		type = (int)GELF_R_TYPE(rela.r_info);
3170Sstevel@tonic-gate 		/* LINTED */
3180Sstevel@tonic-gate 		symid = (int)GELF_R_SYM(rela.r_info);
3190Sstevel@tonic-gate 		/* LINTED */
3200Sstevel@tonic-gate 		if ((symid > (no_syms - 1)) || (symid < 0)) {
3210Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s: invalid symbol table "
3220Sstevel@tonic-gate 			    "offset - %d - in %s\n", prog_name, filename,
3230Sstevel@tonic-gate 			    symid, p_scns->scn_name);
3240Sstevel@tonic-gate 			ndx++;
3250Sstevel@tonic-gate 			continue;
3260Sstevel@tonic-gate 		}
3270Sstevel@tonic-gate 		(void) gelf_getsym(sym_data, symid, &sym);
3280Sstevel@tonic-gate 		sym_name = (char *)elf_strptr(elf_file,
3290Sstevel@tonic-gate 			reloc_symtab->p_shdr.sh_link, sym.st_name);
3300Sstevel@tonic-gate 		if (sym_name == NULL)
3310Sstevel@tonic-gate 			sym_name = (char *)UNKNOWN;
3320Sstevel@tonic-gate 		if (r_flag && rn_flag) {
3330Sstevel@tonic-gate 			if (strcmp(name, p_scns->scn_name) != 0) {
3340Sstevel@tonic-gate 				ndx++;
3350Sstevel@tonic-gate 				continue;
3360Sstevel@tonic-gate 			}
3370Sstevel@tonic-gate 			if (!n_title) {
3380Sstevel@tonic-gate 				(void) printf("\n%s:\n", p_scns->scn_name);
3390Sstevel@tonic-gate 				(void) printf("%-*s%-*s%-*s%s\n\n",
3400Sstevel@tonic-gate 				    12 + adj, "Offset", 22, "Symndx",
3410Sstevel@tonic-gate 				    16, "Type", "Addend");
3420Sstevel@tonic-gate 				n_title = 1;
3430Sstevel@tonic-gate 			}
3440Sstevel@tonic-gate 		}
3450Sstevel@tonic-gate 		if (d_flag) {
3460Sstevel@tonic-gate 			if (!d_hi)
3470Sstevel@tonic-gate 				d_hi = d_low;
3480Sstevel@tonic-gate 			if ((symid < d_low) || (symid > d_hi)) {
3490Sstevel@tonic-gate 				ndx++;
3500Sstevel@tonic-gate 				continue;
3510Sstevel@tonic-gate 			}
3520Sstevel@tonic-gate 		}
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 		(void) printf("%-#*llx", 12 + adj, EC_XWORD(rela.r_offset));
3550Sstevel@tonic-gate 		if (!v_flag) {
3560Sstevel@tonic-gate 			(void) printf("%-22d%-18d", symid, type);
3570Sstevel@tonic-gate 		} else {
3580Sstevel@tonic-gate 			if (strlen(sym_name)) {
3590Sstevel@tonic-gate 				size_t len = strlen(sym_name) + 1;
3600Sstevel@tonic-gate 				char tmpstr[10];
3610Sstevel@tonic-gate 				if (len > 22) {
3620Sstevel@tonic-gate 					(void) sprintf(tmpstr, "%%-%ds",
3630Sstevel@tonic-gate 						/* LINTED */
3640Sstevel@tonic-gate 						(int)len);
3650Sstevel@tonic-gate 					(void) printf(tmpstr, sym_name);
3660Sstevel@tonic-gate 				} else
3670Sstevel@tonic-gate 					(void) printf("%-22s", sym_name);
3680Sstevel@tonic-gate 			} else
3690Sstevel@tonic-gate 				(void) printf("%-22d", symid);
3700Sstevel@tonic-gate 			print_reloc_type(p_ehdr->e_machine, type);
3710Sstevel@tonic-gate 		}
3720Sstevel@tonic-gate 		(void) printf("%lld\n", EC_SXWORD(rela.r_addend));
3730Sstevel@tonic-gate 		ndx++;
3740Sstevel@tonic-gate 	}
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate /*
3780Sstevel@tonic-gate  * Print relocation data of type SHT_REL.
3790Sstevel@tonic-gate  * If d_flag, print data corresponding only to
3800Sstevel@tonic-gate  * the section or range of sections specified.
3810Sstevel@tonic-gate  * If n_flag, print data corresponding only to
3820Sstevel@tonic-gate  * the named section.
3830Sstevel@tonic-gate  */
3840Sstevel@tonic-gate static void
3850Sstevel@tonic-gate print_rel(Elf *elf_file, SCNTAB *p_scns, Elf_Data *rdata, Elf_Data *sym_data,
3860Sstevel@tonic-gate 	GElf_Ehdr *p_ehdr, size_t reloc_size, size_t sym_size, char *filename,
3870Sstevel@tonic-gate 	SCNTAB *reloc_symtab)
3880Sstevel@tonic-gate {
3890Sstevel@tonic-gate 	GElf_Rel rel;
3900Sstevel@tonic-gate 	GElf_Sym sym;
3910Sstevel@tonic-gate 	size_t no_entries;
3920Sstevel@tonic-gate 	size_t rel_entsize;
3930Sstevel@tonic-gate 	int type, symid;
3940Sstevel@tonic-gate 	size_t no_syms;
3950Sstevel@tonic-gate 	static int n_title = 0;
3960Sstevel@tonic-gate 	int ndx = 0;
3970Sstevel@tonic-gate 	char *sym_name;
3980Sstevel@tonic-gate 	int adj = 0;
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	if (gelf_getclass(elf_file) == ELFCLASS64)
4011618Srie 		adj = 8;
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	rel_entsize = p_scns->p_shdr.sh_entsize;
4040Sstevel@tonic-gate 	if ((rel_entsize == 0) ||
4050Sstevel@tonic-gate 	    (rel_entsize > p_scns->p_shdr.sh_size)) {
4060Sstevel@tonic-gate 		rel_entsize = gelf_fsize(elf_file, ELF_T_REL, 1,
4070Sstevel@tonic-gate 		    EV_CURRENT);
4080Sstevel@tonic-gate 	}
4090Sstevel@tonic-gate 	no_entries = reloc_size / rel_entsize;
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	no_syms = sym_size / gelf_fsize(elf_file, ELF_T_SYM, 1, EV_CURRENT);
4120Sstevel@tonic-gate 	while (no_entries--) {
4130Sstevel@tonic-gate 		(void) gelf_getrel(rdata, ndx, &rel);
4140Sstevel@tonic-gate 		/* LINTED */
4150Sstevel@tonic-gate 		type = (int)GELF_R_TYPE(rel.r_info);
4160Sstevel@tonic-gate 		/* LINTED */
4170Sstevel@tonic-gate 		symid = (int)GELF_R_SYM(rel.r_info);
4180Sstevel@tonic-gate 		/* LINTED */
4190Sstevel@tonic-gate 		if ((symid > (no_syms - 1)) || (symid < 0)) {
4200Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s: invalid symbol table "
4210Sstevel@tonic-gate 			    "offset - %d - in %s\n", prog_name, filename,
4220Sstevel@tonic-gate 			    symid, p_scns->scn_name);
4230Sstevel@tonic-gate 			ndx++;
4240Sstevel@tonic-gate 			continue;
4250Sstevel@tonic-gate 		}
4260Sstevel@tonic-gate 		(void) gelf_getsym(sym_data, symid, &sym);
4270Sstevel@tonic-gate 		sym_name = (char *)elf_strptr(elf_file,
4280Sstevel@tonic-gate 			reloc_symtab->p_shdr.sh_link, sym.st_name);
4290Sstevel@tonic-gate 		if (sym_name == NULL)
4300Sstevel@tonic-gate 			sym_name = (char *)UNKNOWN;
4310Sstevel@tonic-gate 		if (r_flag && rn_flag) {
4320Sstevel@tonic-gate 			if (strcmp(name, p_scns->scn_name) != 0) {
4330Sstevel@tonic-gate 				ndx++;
4340Sstevel@tonic-gate 				continue;
4350Sstevel@tonic-gate 			}
4360Sstevel@tonic-gate 			if (!n_title) {
4370Sstevel@tonic-gate 				(void) printf("\n%s:\n", p_scns->scn_name);
4380Sstevel@tonic-gate 				(void) printf("%-*s%-*s%s\n\n",
4390Sstevel@tonic-gate 				    12 + adj, "Offset", 20, "Symndx", "Type");
4400Sstevel@tonic-gate 				n_title = 1;
4410Sstevel@tonic-gate 			}
4420Sstevel@tonic-gate 		}
4430Sstevel@tonic-gate 		if (d_flag) {
4440Sstevel@tonic-gate 			if (!d_hi)
4450Sstevel@tonic-gate 				d_hi = d_low;
4460Sstevel@tonic-gate 			if ((symid < d_low) || (symid > d_hi)) {
4470Sstevel@tonic-gate 				ndx++;
4480Sstevel@tonic-gate 				continue;
4490Sstevel@tonic-gate 			}
4500Sstevel@tonic-gate 		}
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 		(void) printf("%-#*llx", 12 + adj, EC_ADDR(rel.r_offset));
4530Sstevel@tonic-gate 		if (!v_flag) {
4540Sstevel@tonic-gate 			(void) printf("%-20d%-18d", symid, type);
4550Sstevel@tonic-gate 		} else {
4560Sstevel@tonic-gate 			if (strlen(sym_name))
4570Sstevel@tonic-gate 				(void) printf("%-20s", sym_name);
4580Sstevel@tonic-gate 			else
4590Sstevel@tonic-gate 				(void) printf("%-20d", sym.st_name);
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 			print_reloc_type(p_ehdr->e_machine, type);
4620Sstevel@tonic-gate 		}
4630Sstevel@tonic-gate 		(void) printf("\n");
4640Sstevel@tonic-gate 		ndx++;
4650Sstevel@tonic-gate 	}
4660Sstevel@tonic-gate }
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate /* demangle C++ names */
4690Sstevel@tonic-gate static char *
4700Sstevel@tonic-gate demangled_name(char *s)
4710Sstevel@tonic-gate {
4721618Srie 	static char	*buf = NULL;
4731618Srie 	char		*dn;
4741618Srie 	size_t		len;
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	dn = sgs_demangle(s);
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	/*
4790Sstevel@tonic-gate 	 * If not demangled, just return the symbol name
4800Sstevel@tonic-gate 	 */
4810Sstevel@tonic-gate 	if (strcmp(s, dn) == 0)
4820Sstevel@tonic-gate 		return (s);
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	/*
4850Sstevel@tonic-gate 	 * Demangled. Format it
4860Sstevel@tonic-gate 	 */
4870Sstevel@tonic-gate 	if (buf != NULL)
4880Sstevel@tonic-gate 		free(buf);
4890Sstevel@tonic-gate 
4901618Srie 	len = strlen(dn) + strlen(s) + 4;
4911618Srie 	if ((buf = malloc(len)) == NULL)
4920Sstevel@tonic-gate 		return (s);
4930Sstevel@tonic-gate 
4941618Srie 	(void) snprintf(buf, len, "%s\t[%s]", dn, s);
4950Sstevel@tonic-gate 	return (buf);
4960Sstevel@tonic-gate }
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate /*
4990Sstevel@tonic-gate  * Print the symbol table.  Input is an ELF file descriptor, a
5000Sstevel@tonic-gate  * pointer to the symbol table SCNTAB structure,
5010Sstevel@tonic-gate  * the number of symbols, a range of symbols to print,
5020Sstevel@tonic-gate  * an index which is the number of the
5030Sstevel@tonic-gate  * section in the file, and the filename.  The number of sections,
5040Sstevel@tonic-gate  * the range, and the index are set in
5050Sstevel@tonic-gate  * dump_symbol_table, depending on whether -n or -T were set.
5060Sstevel@tonic-gate  */
5070Sstevel@tonic-gate static void
5080Sstevel@tonic-gate print_symtab(Elf *elf_file, SCNTAB *p_symtab, Elf_Data *sym_data,
5090Sstevel@tonic-gate 	long range, int index)
5100Sstevel@tonic-gate {
5110Sstevel@tonic-gate 	GElf_Sym sym;
5120Sstevel@tonic-gate 	int adj = 0;		/* field adjustment for elf64 */
5130Sstevel@tonic-gate 	Elf32_Word	*symshndx = 0;
5140Sstevel@tonic-gate 	unsigned int	nosymshndx = 0;
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	if (gelf_getclass(elf_file) == ELFCLASS64)
5171618Srie 		adj = 8;
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	while (range > 0) {
5200Sstevel@tonic-gate 		char		*sym_name = (char *)0;
5210Sstevel@tonic-gate 		int		type, bind;
5220Sstevel@tonic-gate 		int		specsec;
5230Sstevel@tonic-gate 		unsigned int	shndx;
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 		(void) gelf_getsym(sym_data, index, &sym);
5260Sstevel@tonic-gate 		type = (int)GELF_ST_TYPE(sym.st_info);
5270Sstevel@tonic-gate 		bind = (int)GELF_ST_BIND(sym.st_info);
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 		if ((sym.st_shndx == SHN_XINDEX) &&
5300Sstevel@tonic-gate 		    (symshndx == 0) && (nosymshndx == 0)) {
5310Sstevel@tonic-gate 			Elf_Scn		*_scn;
5320Sstevel@tonic-gate 			GElf_Shdr	_shdr;
5330Sstevel@tonic-gate 			size_t		symscnndx;
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 			symscnndx = elf_ndxscn(p_symtab->p_sd);
5360Sstevel@tonic-gate 			_scn = 0;
5370Sstevel@tonic-gate 			while ((_scn = elf_nextscn(elf_file, _scn)) != 0) {
5380Sstevel@tonic-gate 				if (gelf_getshdr(_scn, &_shdr) == 0)
5390Sstevel@tonic-gate 					break;
5400Sstevel@tonic-gate 				if ((_shdr.sh_type == SHT_SYMTAB_SHNDX) &&
5410Sstevel@tonic-gate 				    /* LINTED */
5420Sstevel@tonic-gate 				    (_shdr.sh_link == (GElf_Word)symscnndx)) {
5430Sstevel@tonic-gate 					Elf_Data	*_data;
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 					if ((_data = elf_getdata(_scn, 0)) == 0)
5460Sstevel@tonic-gate 						continue;
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 					symshndx = (Elf32_Word *)_data->d_buf;
5490Sstevel@tonic-gate 					nosymshndx = 0;
5500Sstevel@tonic-gate 					break;
5510Sstevel@tonic-gate 				}
5520Sstevel@tonic-gate 			}
5530Sstevel@tonic-gate 			nosymshndx = 1;
5540Sstevel@tonic-gate 		}
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 		if ((symshndx) && (sym.st_shndx == SHN_XINDEX)) {
5570Sstevel@tonic-gate 			shndx = symshndx[index];
5580Sstevel@tonic-gate 			specsec = 0;
5590Sstevel@tonic-gate 		} else {
5600Sstevel@tonic-gate 			shndx = sym.st_shndx;
5610Sstevel@tonic-gate 			if ((sym.st_shndx == SHN_UNDEF) ||
5620Sstevel@tonic-gate 			    (sym.st_shndx >= SHN_LORESERVE))
5630Sstevel@tonic-gate 				specsec = 1;
5640Sstevel@tonic-gate 			else
5650Sstevel@tonic-gate 				specsec = 0;
5660Sstevel@tonic-gate 		}
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 		(void) printf("[%d]\t ", index++);
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 		if (v_flag && (type == STT_SPARC_REGISTER)) {
5720Sstevel@tonic-gate 			/*
5730Sstevel@tonic-gate 			 *  The strings "REG_G1" through "REG_G7" are intended
5740Sstevel@tonic-gate 			 *  to be consistent with output from elfdump(1).
5750Sstevel@tonic-gate 			 */
5760Sstevel@tonic-gate 			switch (sym.st_value) {
5770Sstevel@tonic-gate 			case STO_SPARC_REGISTER_G1:
5780Sstevel@tonic-gate 				(void) printf("%-*s", 12 + adj, "REG_G1");
5790Sstevel@tonic-gate 				break;
5800Sstevel@tonic-gate 			case STO_SPARC_REGISTER_G2:
5810Sstevel@tonic-gate 				(void) printf("%-*s", 12 + adj, "REG_G2");
5820Sstevel@tonic-gate 				break;
5830Sstevel@tonic-gate 			case STO_SPARC_REGISTER_G3:
5840Sstevel@tonic-gate 				(void) printf("%-*s", 12 + adj, "REG_G3");
5850Sstevel@tonic-gate 				break;
5860Sstevel@tonic-gate 			case STO_SPARC_REGISTER_G4:
5870Sstevel@tonic-gate 				(void) printf("%-*s", 12 + adj, "REG_G4");
5880Sstevel@tonic-gate 				break;
5890Sstevel@tonic-gate 			case STO_SPARC_REGISTER_G5:
5900Sstevel@tonic-gate 				(void) printf("%-*s", 12 + adj, "REG_G5");
5910Sstevel@tonic-gate 				break;
5920Sstevel@tonic-gate 			case STO_SPARC_REGISTER_G6:
5930Sstevel@tonic-gate 				(void) printf("%-*s", 12 + adj, "REG_G6");
5940Sstevel@tonic-gate 				break;
5950Sstevel@tonic-gate 			case STO_SPARC_REGISTER_G7:
5960Sstevel@tonic-gate 				(void) printf("%-*s", 12 + adj, "REG_G7");
5970Sstevel@tonic-gate 				break;
5980Sstevel@tonic-gate 			default:
5990Sstevel@tonic-gate 				(void) printf("0x%-*llx", 10 + adj,
6000Sstevel@tonic-gate 				    EC_ADDR(sym.st_value));
6010Sstevel@tonic-gate 			}
6020Sstevel@tonic-gate 		} else
6030Sstevel@tonic-gate 			(void) printf("0x%-*llx", 10 + adj,
6040Sstevel@tonic-gate 			    EC_ADDR(sym.st_value));
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 		(void) printf("%-*lld", 9 + adj, EC_XWORD(sym.st_size));
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 		if (!v_flag) {
6090Sstevel@tonic-gate 			(void) printf("%d\t\t%d\t%d\t%#x\t",
6100Sstevel@tonic-gate 			    type, bind, (int)sym.st_other, (int)shndx);
6110Sstevel@tonic-gate 		} else {
6120Sstevel@tonic-gate 			switch (type) {
6130Sstevel@tonic-gate 			case STT_NOTYPE:
6140Sstevel@tonic-gate 				(void) printf("%s\t", "NOTY");
6150Sstevel@tonic-gate 				break;
6160Sstevel@tonic-gate 			case STT_OBJECT:
6170Sstevel@tonic-gate 				(void) printf("%s\t", "OBJT");
6180Sstevel@tonic-gate 				break;
6190Sstevel@tonic-gate 			case STT_FUNC:
6200Sstevel@tonic-gate 				(void) printf("%s\t", "FUNC");
6210Sstevel@tonic-gate 				break;
6220Sstevel@tonic-gate 			case STT_SECTION:
6230Sstevel@tonic-gate 				(void) printf("%s\t", "SECT");
6240Sstevel@tonic-gate 				break;
6250Sstevel@tonic-gate 			case STT_FILE:
6260Sstevel@tonic-gate 				(void) printf("%s\t", "FILE");
6270Sstevel@tonic-gate 				break;
6280Sstevel@tonic-gate 			case STT_SPARC_REGISTER:
6290Sstevel@tonic-gate 				(void) printf("%s\t", "REGI");
6300Sstevel@tonic-gate 				break;
6310Sstevel@tonic-gate 			case STT_COMMON:
6320Sstevel@tonic-gate 				(void) printf("%s\t", "COMM");
6330Sstevel@tonic-gate 				break;
6340Sstevel@tonic-gate 			case STT_TLS:
6350Sstevel@tonic-gate 				(void) printf("%s\t", "TLS ");
6360Sstevel@tonic-gate 				break;
6370Sstevel@tonic-gate 			default:
6380Sstevel@tonic-gate 				(void) printf("%d\t", type);
6390Sstevel@tonic-gate 			}
6400Sstevel@tonic-gate 			switch (bind) {
6410Sstevel@tonic-gate 			case STB_LOCAL:
6420Sstevel@tonic-gate 				(void) printf("LOCL");
6430Sstevel@tonic-gate 				break;
6440Sstevel@tonic-gate 			case STB_GLOBAL:
6450Sstevel@tonic-gate 				(void) printf("GLOB");
6460Sstevel@tonic-gate 				break;
6470Sstevel@tonic-gate 			case STB_WEAK:
6480Sstevel@tonic-gate 				(void) printf("WEAK");
6490Sstevel@tonic-gate 				break;
6500Sstevel@tonic-gate 			default:
6510Sstevel@tonic-gate 				(void) printf("%d", bind);
6520Sstevel@tonic-gate 			}
6530Sstevel@tonic-gate 			(void) printf("\t  %d\t", EC_WORD(sym.st_other));
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 			if (specsec) {
6560Sstevel@tonic-gate 				switch (shndx) {
6570Sstevel@tonic-gate 				case SHN_UNDEF:
6580Sstevel@tonic-gate 					(void) printf("UNDEF");
6590Sstevel@tonic-gate 					break;
6600Sstevel@tonic-gate 				case SHN_ABS:
6610Sstevel@tonic-gate 					(void) printf("ABS");
6620Sstevel@tonic-gate 					break;
6630Sstevel@tonic-gate 				case SHN_COMMON:
6640Sstevel@tonic-gate 					(void) printf("COMMON");
6650Sstevel@tonic-gate 					break;
6660Sstevel@tonic-gate 				case SHN_XINDEX:
6670Sstevel@tonic-gate 					(void) printf("XINDEX");
6680Sstevel@tonic-gate 					break;
6690Sstevel@tonic-gate 				default:
6700Sstevel@tonic-gate 					(void) printf("%d", EC_WORD(shndx));
6710Sstevel@tonic-gate 				}
6720Sstevel@tonic-gate 			} else
6730Sstevel@tonic-gate 				(void) printf("%d", EC_WORD(shndx));
6740Sstevel@tonic-gate 			(void) printf("\t");
6750Sstevel@tonic-gate 		}
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 		/* support machines where NULL-deref causes core dump */
6780Sstevel@tonic-gate 		if (sym.st_name == 0)
6790Sstevel@tonic-gate 			sym_name = (char *)UNKNOWN;
6800Sstevel@tonic-gate 		else
6810Sstevel@tonic-gate 			if (C_flag)
6820Sstevel@tonic-gate 				sym_name = demangled_name(
6830Sstevel@tonic-gate 					(char *)elf_strptr(elf_file,
6840Sstevel@tonic-gate 					p_symtab->p_shdr.sh_link,
6850Sstevel@tonic-gate 					sym.st_name));
6860Sstevel@tonic-gate 		else
6870Sstevel@tonic-gate 			sym_name = (char *)elf_strptr(elf_file,
6880Sstevel@tonic-gate 				p_symtab->p_shdr.sh_link,
6890Sstevel@tonic-gate 				sym.st_name);
6900Sstevel@tonic-gate 		if (sym_name == NULL)
6910Sstevel@tonic-gate 			sym_name = (char *)UNKNOWN;
6920Sstevel@tonic-gate 		(void) printf("%s\n", sym_name);
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 		range--;
6950Sstevel@tonic-gate 	}	/* end while */
6960Sstevel@tonic-gate }
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate /*
6990Sstevel@tonic-gate  * Print the section header table.  Input is the SCNTAB structure,
7000Sstevel@tonic-gate  * the number of sections, an index which is the number of the
7010Sstevel@tonic-gate  * section in the file, and the filename.  The values of the SCNTAB
7020Sstevel@tonic-gate  * structure, the number of sections, and the index are set in
7030Sstevel@tonic-gate  * dump_shdr depending on whether the -n or -d modifiers were set.
7040Sstevel@tonic-gate  */
7050Sstevel@tonic-gate static void
7060Sstevel@tonic-gate print_shdr(Elf *elf_file, SCNTAB *s, int num_scns, int index)
7070Sstevel@tonic-gate {
7080Sstevel@tonic-gate 	SCNTAB *p;
7090Sstevel@tonic-gate 	int num;
7100Sstevel@tonic-gate 	int field;
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 	if (gelf_getclass(elf_file) == ELFCLASS64)
7131618Srie 		field = 21;
7140Sstevel@tonic-gate 	else
7150Sstevel@tonic-gate 		field = 13;
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 	p = s;
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	for (num = 0; num < num_scns; num++, p++) {
7200Sstevel@tonic-gate 		(void) printf("[%d]\t", index++);
7210Sstevel@tonic-gate 		if (!v_flag) {
7220Sstevel@tonic-gate 			(void) printf("%u\t%llu\t",
7230Sstevel@tonic-gate 			EC_WORD(p->p_shdr.sh_type),
7240Sstevel@tonic-gate 			EC_XWORD(p->p_shdr.sh_flags));
7250Sstevel@tonic-gate 		} else {
7260Sstevel@tonic-gate 			switch (p->p_shdr.sh_type) {
7270Sstevel@tonic-gate 			case SHT_NULL:
7280Sstevel@tonic-gate 				(void) printf("NULL");
7290Sstevel@tonic-gate 				break;
7300Sstevel@tonic-gate 			case SHT_PROGBITS:
7310Sstevel@tonic-gate 				(void) printf("PBIT");
7320Sstevel@tonic-gate 				break;
7330Sstevel@tonic-gate 			case SHT_SYMTAB:
7340Sstevel@tonic-gate 				(void) printf("SYMT");
7350Sstevel@tonic-gate 				break;
7360Sstevel@tonic-gate 			case SHT_STRTAB:
7370Sstevel@tonic-gate 				(void) printf("STRT");
7380Sstevel@tonic-gate 				break;
7390Sstevel@tonic-gate 			case SHT_RELA:
7400Sstevel@tonic-gate 				(void) printf("RELA");
7410Sstevel@tonic-gate 				break;
7420Sstevel@tonic-gate 			case SHT_HASH:
7430Sstevel@tonic-gate 				(void) printf("HASH");
7440Sstevel@tonic-gate 				break;
7450Sstevel@tonic-gate 			case SHT_DYNAMIC:
7460Sstevel@tonic-gate 				(void) printf("DYNM");
7470Sstevel@tonic-gate 				break;
7480Sstevel@tonic-gate 			case SHT_NOTE:
7490Sstevel@tonic-gate 				(void) printf("NOTE");
7500Sstevel@tonic-gate 				break;
7510Sstevel@tonic-gate 			case SHT_NOBITS:
7520Sstevel@tonic-gate 				(void) printf("NOBI");
7530Sstevel@tonic-gate 				break;
7540Sstevel@tonic-gate 			case SHT_REL:
7550Sstevel@tonic-gate 				(void) printf("REL ");
7560Sstevel@tonic-gate 				break;
7570Sstevel@tonic-gate 			case SHT_DYNSYM:
7580Sstevel@tonic-gate 				(void) printf("DYNS");
7590Sstevel@tonic-gate 				break;
7600Sstevel@tonic-gate 			case ((GElf_Word) SHT_LOUSER):
7610Sstevel@tonic-gate 				(void) printf("LUSR");
7620Sstevel@tonic-gate 				break;
7630Sstevel@tonic-gate 			case ((GElf_Word) SHT_HIUSER):
7640Sstevel@tonic-gate 				(void) printf("HUSR");
7650Sstevel@tonic-gate 				break;
7660Sstevel@tonic-gate 			case SHT_SHLIB:
7670Sstevel@tonic-gate 				(void) printf("SHLB");
7680Sstevel@tonic-gate 				break;
7690Sstevel@tonic-gate 			case SHT_SUNW_SIGNATURE:
7700Sstevel@tonic-gate 				(void) printf("SIGN");
7710Sstevel@tonic-gate 				break;
7720Sstevel@tonic-gate 			case SHT_SUNW_ANNOTATE:
7730Sstevel@tonic-gate 				(void) printf("ANOT");
7740Sstevel@tonic-gate 				break;
7750Sstevel@tonic-gate 			case SHT_SUNW_DEBUGSTR:
7760Sstevel@tonic-gate 				(void) printf("DBGS");
7770Sstevel@tonic-gate 				break;
7780Sstevel@tonic-gate 			case SHT_SUNW_DEBUG:
7790Sstevel@tonic-gate 				(void) printf("DBG ");
7800Sstevel@tonic-gate 				break;
7810Sstevel@tonic-gate 			case SHT_SUNW_move:
7820Sstevel@tonic-gate 				(void) printf("MOVE");
7830Sstevel@tonic-gate 				break;
7840Sstevel@tonic-gate 			case SHT_SUNW_verdef:
7850Sstevel@tonic-gate 				(void) printf("VERD");
7860Sstevel@tonic-gate 				break;
7870Sstevel@tonic-gate 			case SHT_SUNW_verneed:
7880Sstevel@tonic-gate 				(void) printf("VERN");
7890Sstevel@tonic-gate 				break;
7900Sstevel@tonic-gate 			case SHT_SUNW_versym:
7910Sstevel@tonic-gate 				(void) printf("VERS");
7920Sstevel@tonic-gate 				break;
7930Sstevel@tonic-gate 			case SHT_SUNW_syminfo:
7940Sstevel@tonic-gate 				(void) printf("SYMI");
7950Sstevel@tonic-gate 				break;
7960Sstevel@tonic-gate 			case SHT_SUNW_COMDAT:
7970Sstevel@tonic-gate 				(void) printf("COMD");
7980Sstevel@tonic-gate 				break;
7991618Srie 			case SHT_AMD64_UNWIND:
8001618Srie 				(void) printf("UNWD");
8011618Srie 				break;
8021618Srie 			case SHT_SPARC_GOTDATA:
8031618Srie 				(void) printf("GOTD");
8041618Srie 				break;
8050Sstevel@tonic-gate 			default:
8060Sstevel@tonic-gate 				(void) printf("%u", EC_WORD(p->p_shdr.sh_type));
8070Sstevel@tonic-gate 				break;
8080Sstevel@tonic-gate 			}
8090Sstevel@tonic-gate 			(void) printf("    ");
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 			if (p->p_shdr.sh_flags & SHF_WRITE)
8120Sstevel@tonic-gate 				(void) printf("W");
8130Sstevel@tonic-gate 			else
8140Sstevel@tonic-gate 				(void) printf("-");
8150Sstevel@tonic-gate 			if (p->p_shdr.sh_flags & SHF_ALLOC)
8160Sstevel@tonic-gate 				(void) printf("A");
8170Sstevel@tonic-gate 			else
8180Sstevel@tonic-gate 				(void) printf("-");
8190Sstevel@tonic-gate 			if (p->p_shdr.sh_flags & SHF_EXECINSTR)
8200Sstevel@tonic-gate 				(void) printf("I");
8210Sstevel@tonic-gate 			else
8220Sstevel@tonic-gate 				(void) printf("-");
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 			if (p->p_shdr.sh_flags & SHF_ORDERED)
8250Sstevel@tonic-gate 				(void) printf("O");
8260Sstevel@tonic-gate 			if (p->p_shdr.sh_flags & SHF_EXCLUDE)
8270Sstevel@tonic-gate 				(void) printf("E");
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 			(void) printf("\t");
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 		}
8320Sstevel@tonic-gate 		(void) printf("%-#*llx%-#*llx%-#*llx%s%s\n",
8330Sstevel@tonic-gate 			field, EC_ADDR(p->p_shdr.sh_addr),
8340Sstevel@tonic-gate 			field, EC_OFF(p->p_shdr.sh_offset),
8350Sstevel@tonic-gate 			field, EC_XWORD(p->p_shdr.sh_size),
8360Sstevel@tonic-gate 			/* compatibility:  tab for elf32 */
8370Sstevel@tonic-gate 			(field == 13) ? "\t" : " ", p->scn_name);
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 		(void) printf("\t%u\t%u\t%-#*llx%-#*llx\n\n",
8400Sstevel@tonic-gate 			EC_WORD(p->p_shdr.sh_link),
8410Sstevel@tonic-gate 			EC_WORD(p->p_shdr.sh_info),
8420Sstevel@tonic-gate 			field, EC_XWORD(p->p_shdr.sh_addralign),
8430Sstevel@tonic-gate 			field, EC_XWORD(p->p_shdr.sh_entsize));
8440Sstevel@tonic-gate 	}
8450Sstevel@tonic-gate }
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate /*
8480Sstevel@tonic-gate  * Check that a range of numbers is valid.  Input is
8490Sstevel@tonic-gate  * a lower bound, an upper bound, a boundary condition,
8500Sstevel@tonic-gate  * and the filename.  Negative numbers and numbers greater
8510Sstevel@tonic-gate  * than the bound are invalid.  low must be smaller than hi.
8520Sstevel@tonic-gate  * The returned integer is the number of items in the
8530Sstevel@tonic-gate  * range if it is valid and -1 otherwise.
8540Sstevel@tonic-gate  */
8550Sstevel@tonic-gate static int
8560Sstevel@tonic-gate check_range(int low, int hi, size_t bound, char *filename)
8570Sstevel@tonic-gate {
8580Sstevel@tonic-gate 	if (((size_t)low > bound) || (low <= 0)) {
8590Sstevel@tonic-gate 		(void) fprintf(stderr,
8600Sstevel@tonic-gate 			"%s: %s: number out of range, %d\n",
8610Sstevel@tonic-gate 			prog_name, filename, low);
8620Sstevel@tonic-gate 		return (-1);
8630Sstevel@tonic-gate 	}
8640Sstevel@tonic-gate 	if (((size_t)hi > bound) || (hi < 0)) {
8650Sstevel@tonic-gate 		(void) fprintf(stderr,
8660Sstevel@tonic-gate 			"%s: %s: number out of range, %d\n",
8670Sstevel@tonic-gate 			prog_name, filename, hi);
8680Sstevel@tonic-gate 			return (-1);
8690Sstevel@tonic-gate 	}
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 	if (hi && (low > hi)) {
8720Sstevel@tonic-gate 		(void) fprintf(stderr,
8730Sstevel@tonic-gate 			"%s: %s: invalid range, %d,%d\n",
8740Sstevel@tonic-gate 			prog_name, filename, low, hi);
8750Sstevel@tonic-gate 		return (-1);
8760Sstevel@tonic-gate 	}
8770Sstevel@tonic-gate 	if (hi)
8780Sstevel@tonic-gate 		return (hi - low + 1);
8790Sstevel@tonic-gate 	else
8800Sstevel@tonic-gate 		return (1);
8810Sstevel@tonic-gate }
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate /*
8840Sstevel@tonic-gate  * Print relocation information.  Since this information is
8850Sstevel@tonic-gate  * machine dependent, new sections must be added for each machine
8860Sstevel@tonic-gate  * that is supported.  Input is an ELF file descriptor, the ELF header,
8870Sstevel@tonic-gate  * the SCNTAB structure, the number of sections, and a filename.
8880Sstevel@tonic-gate  * Set up necessary information to print relocation information
8890Sstevel@tonic-gate  * and call the appropriate print function depending on the
8900Sstevel@tonic-gate  * type of relocation information.  If the symbol table is
8910Sstevel@tonic-gate  * absent, no relocation data is processed.  Input is an
8920Sstevel@tonic-gate  * ELF file descriptor, the ELF header, the SCNTAB structure,
8930Sstevel@tonic-gate  * and the filename.  Set range of d_flag and name if n_flag.
8940Sstevel@tonic-gate  */
8950Sstevel@tonic-gate static void
8960Sstevel@tonic-gate dump_reloc_table(Elf *elf_file, GElf_Ehdr *p_ehdr,
8970Sstevel@tonic-gate 	SCNTAB *p_scns, int num_scns, char *filename)
8980Sstevel@tonic-gate {
8990Sstevel@tonic-gate 	Elf_Data *rel_data;
9000Sstevel@tonic-gate 	Elf_Data *sym_data;
9010Sstevel@tonic-gate 	size_t    sym_size;
9020Sstevel@tonic-gate 	size_t    reloc_size;
9030Sstevel@tonic-gate 	SCNTAB *reloc_symtab;
9040Sstevel@tonic-gate 	SCNTAB *head_scns;
9050Sstevel@tonic-gate 	int r_title = 0;
9060Sstevel@tonic-gate 	int adj = 0;
9070Sstevel@tonic-gate 	size_t shnum;
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 	if (gelf_getclass(elf_file) == ELFCLASS64)
9101618Srie 		adj = 8;
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	if ((!p_flag) && (!r_title)) {
9130Sstevel@tonic-gate 		(void) printf("\n    **** RELOCATION INFORMATION ****\n");
9140Sstevel@tonic-gate 		r_title = 1;
9150Sstevel@tonic-gate 	}
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	while (num_scns-- > 0) {
9180Sstevel@tonic-gate 		if ((p_scns->p_shdr.sh_type != SHT_RELA) &&
9190Sstevel@tonic-gate 		    (p_scns->p_shdr.sh_type != SHT_REL)) {
9200Sstevel@tonic-gate 			p_scns++;
9210Sstevel@tonic-gate 			continue;
9220Sstevel@tonic-gate 		}
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 	head_scns = p_head_scns;
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 	if (elf_getshnum(elf_file, &shnum) == 0) {
9270Sstevel@tonic-gate 		(void) fprintf(stderr,
9280Sstevel@tonic-gate 			"%s: %s: elf_getshnum failed: %s\n",
9290Sstevel@tonic-gate 			prog_name, filename, elf_errmsg(-1));
9300Sstevel@tonic-gate 		return;
9310Sstevel@tonic-gate 	}
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 	if ((p_scns->p_shdr.sh_link == 0) ||
9340Sstevel@tonic-gate 	    /* LINTED */
9350Sstevel@tonic-gate 	    (p_scns->p_shdr.sh_link >= (GElf_Word)shnum)) {
9360Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: invalid sh_link field: "
9370Sstevel@tonic-gate 			"section #: %d sh_link: %d\n",
9380Sstevel@tonic-gate 			/* LINTED */
9390Sstevel@tonic-gate 			prog_name, filename, (int)elf_ndxscn(p_scns->p_sd),
9400Sstevel@tonic-gate 			(int)p_scns->p_shdr.sh_link);
9410Sstevel@tonic-gate 		return;
9420Sstevel@tonic-gate 	}
9430Sstevel@tonic-gate 	head_scns += (p_scns->p_shdr.sh_link -1);
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 	if (head_scns->p_shdr.sh_type == SHT_SYMTAB) {
9460Sstevel@tonic-gate 		reloc_symtab = p_symtab;
9470Sstevel@tonic-gate 	} else if (head_scns->p_shdr.sh_type  == SHT_DYNSYM) {
9480Sstevel@tonic-gate 		reloc_symtab = p_dynsym;
9490Sstevel@tonic-gate 	} else {
9500Sstevel@tonic-gate 		(void) fprintf(stderr,
9510Sstevel@tonic-gate "%s: %s: could not get symbol table\n", prog_name, filename);
9520Sstevel@tonic-gate 		return;
9530Sstevel@tonic-gate 	}
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	sym_data = NULL;
9560Sstevel@tonic-gate 	sym_size = 0;
9570Sstevel@tonic-gate 	reloc_size = 0;
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 	if ((sym_data = elf_getdata(reloc_symtab->p_sd, NULL)) == NULL) {
9600Sstevel@tonic-gate 		(void) fprintf(stderr,
9610Sstevel@tonic-gate 		"%s: %s: no symbol table data\n", prog_name, filename);
9620Sstevel@tonic-gate 		return;
9630Sstevel@tonic-gate 	}
9640Sstevel@tonic-gate 	sym_size = sym_data->d_size;
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 	if (p_scns == NULL) {
9670Sstevel@tonic-gate 		(void) fprintf(stderr,
9680Sstevel@tonic-gate 		"%s: %s: no section table data\n", prog_name, filename);
9690Sstevel@tonic-gate 		return;
9700Sstevel@tonic-gate 	}
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 	if (p_scns->p_shdr.sh_type == SHT_RELA) {
9730Sstevel@tonic-gate 		if (!n_flag && r_flag)
9740Sstevel@tonic-gate 			(void) printf("\n%s:\n", p_scns->scn_name);
9750Sstevel@tonic-gate 		if (!p_flag && (!n_flag && r_flag))
9760Sstevel@tonic-gate 			(void) printf("%-*s%-*s%-*s%s\n\n",
9770Sstevel@tonic-gate 			    12 + adj, "Offset", 22, "Symndx",
9780Sstevel@tonic-gate 			    18, "Type", "Addend");
9790Sstevel@tonic-gate 		if ((rel_data = elf_getdata(p_scns->p_sd, NULL)) == NULL) {
9800Sstevel@tonic-gate 			(void) fprintf(stderr,
9810Sstevel@tonic-gate "%s: %s: no relocation information\n", prog_name, filename);
9820Sstevel@tonic-gate 			return;
9830Sstevel@tonic-gate 		}
9840Sstevel@tonic-gate 		reloc_size = rel_data->d_size;
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 		if (n_flag) {
9870Sstevel@tonic-gate 			rn_flag = 1;
9880Sstevel@tonic-gate 			print_rela(elf_file, p_scns, rel_data, sym_data, p_ehdr,
9890Sstevel@tonic-gate 				reloc_size, sym_size, filename, reloc_symtab);
9900Sstevel@tonic-gate 		}
9910Sstevel@tonic-gate 		if (d_flag) {
9920Sstevel@tonic-gate 			rn_flag = 0;
9930Sstevel@tonic-gate 			print_rela(elf_file, p_scns, rel_data, sym_data, p_ehdr,
9940Sstevel@tonic-gate 				reloc_size, sym_size, filename, reloc_symtab);
9950Sstevel@tonic-gate 		}
9960Sstevel@tonic-gate 		if (!n_flag && !d_flag)
9970Sstevel@tonic-gate 			print_rela(elf_file, p_scns, rel_data, sym_data, p_ehdr,
9980Sstevel@tonic-gate 				reloc_size, sym_size, filename, reloc_symtab);
9990Sstevel@tonic-gate 	} else {
10000Sstevel@tonic-gate 		if (p_scns->p_shdr.sh_type == SHT_REL) {
10010Sstevel@tonic-gate 			if (!n_flag && r_flag)
10020Sstevel@tonic-gate 				(void) printf("\n%s:\n", p_scns->scn_name);
10030Sstevel@tonic-gate 			if (!p_flag && (!n_flag && r_flag)) {
10040Sstevel@tonic-gate 				(void) printf("%-*s%-*s%s\n\n",
10050Sstevel@tonic-gate 				    12 + adj, "Offset", 20, "Symndx", "Type");
10060Sstevel@tonic-gate 			}
10070Sstevel@tonic-gate 			if ((rel_data = elf_getdata(p_scns->p_sd, NULL))
10080Sstevel@tonic-gate 			    == NULL) {
10090Sstevel@tonic-gate 				(void) fprintf(stderr,
10100Sstevel@tonic-gate "%s: %s: no relocation information\n", prog_name, filename);
10110Sstevel@tonic-gate 				return;
10120Sstevel@tonic-gate 			}
10130Sstevel@tonic-gate 			reloc_size = rel_data->d_size;
10140Sstevel@tonic-gate 			if (n_flag) {
10150Sstevel@tonic-gate 				rn_flag = 1;
10160Sstevel@tonic-gate 				print_rel(elf_file, p_scns, rel_data, sym_data,
10170Sstevel@tonic-gate 					p_ehdr, reloc_size, sym_size,
10180Sstevel@tonic-gate 					filename, reloc_symtab);
10190Sstevel@tonic-gate 			}
10200Sstevel@tonic-gate 			if (d_flag) {
10210Sstevel@tonic-gate 				rn_flag = 0;
10220Sstevel@tonic-gate 				print_rel(elf_file, p_scns, rel_data, sym_data,
10230Sstevel@tonic-gate 					p_ehdr, reloc_size, sym_size,
10240Sstevel@tonic-gate 					filename, reloc_symtab);
10250Sstevel@tonic-gate 			}
10260Sstevel@tonic-gate 			if (!n_flag && !d_flag)
10270Sstevel@tonic-gate 				print_rel(elf_file, p_scns, rel_data, sym_data,
10280Sstevel@tonic-gate 					p_ehdr, reloc_size, sym_size,
10290Sstevel@tonic-gate 					filename, reloc_symtab);
10300Sstevel@tonic-gate 		}
10310Sstevel@tonic-gate 	}
10320Sstevel@tonic-gate 	p_scns++;
10330Sstevel@tonic-gate 	}
10340Sstevel@tonic-gate }
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate /*
10370Sstevel@tonic-gate  * Print out the string tables.  Input is an opened ELF file,
10380Sstevel@tonic-gate  * the SCNTAB structure, the number of sections, and the filename.
10390Sstevel@tonic-gate  * Since there can be more than one string table, all sections are
10400Sstevel@tonic-gate  * examined and any with the correct type are printed out.
10410Sstevel@tonic-gate  */
10420Sstevel@tonic-gate static void
10430Sstevel@tonic-gate dump_string_table(SCNTAB *s, int num_scns)
10440Sstevel@tonic-gate {
10450Sstevel@tonic-gate 	size_t section_size;
10460Sstevel@tonic-gate 	unsigned char *strtab;
10470Sstevel@tonic-gate 	int beg_of_string;
10480Sstevel@tonic-gate 	int counter = 0;
10490Sstevel@tonic-gate 	int str_off;
10500Sstevel@tonic-gate 	int i;
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	if (!p_flag) {
10530Sstevel@tonic-gate 		(void) printf("\n     **** STRING TABLE INFORMATION ****\n");
10540Sstevel@tonic-gate 	}
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 	for (i = 0; i < num_scns; i++, s++) {
10570Sstevel@tonic-gate 		if (s->p_shdr.sh_type != SHT_STRTAB)
10580Sstevel@tonic-gate 			continue;
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 		str_off = 0;
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 		if (!p_flag) {
10630Sstevel@tonic-gate 			(void) printf("\n%s:\n", s->scn_name);
10640Sstevel@tonic-gate 			(void) printf("   <offset>  \tName\n");
10650Sstevel@tonic-gate 		}
10660Sstevel@tonic-gate 		section_size = 0;
10670Sstevel@tonic-gate 		if ((strtab = (unsigned char *)
10680Sstevel@tonic-gate 		    get_scndata(s->p_sd, &section_size)) == NULL) {
10690Sstevel@tonic-gate 			continue;
10700Sstevel@tonic-gate 		}
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 		if (section_size != 0) {
10730Sstevel@tonic-gate 			(void) printf("   <%d>  \t", str_off);
10740Sstevel@tonic-gate 			beg_of_string = 0;
10750Sstevel@tonic-gate 			while (section_size--) {
10760Sstevel@tonic-gate 				unsigned char c = *strtab++;
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 				if (beg_of_string) {
10790Sstevel@tonic-gate 					(void) printf("   <%d>  \t", str_off);
10800Sstevel@tonic-gate 					counter++;
10810Sstevel@tonic-gate 					beg_of_string = 0;
10820Sstevel@tonic-gate 				}
10830Sstevel@tonic-gate 				str_off++;
10840Sstevel@tonic-gate 				switch (c) {
10850Sstevel@tonic-gate 				case '\0':
10860Sstevel@tonic-gate 					(void) printf("\n");
10870Sstevel@tonic-gate 					beg_of_string = 1;
10880Sstevel@tonic-gate 					break;
10890Sstevel@tonic-gate 				default:
10900Sstevel@tonic-gate 					(void) putchar(c);
10910Sstevel@tonic-gate 				}
10920Sstevel@tonic-gate 			}
10930Sstevel@tonic-gate 		}
10940Sstevel@tonic-gate 	}
10950Sstevel@tonic-gate 	(void) printf("\n");
10960Sstevel@tonic-gate }
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate /*
10990Sstevel@tonic-gate  * Print the symbol table.  This function does not print the contents
11000Sstevel@tonic-gate  * of the symbol table but sets up the parameters and then calls
11010Sstevel@tonic-gate  * print_symtab to print the symbols.  Calling another function to print
11020Sstevel@tonic-gate  * the symbols allows both -T and -n to work correctly
11030Sstevel@tonic-gate  * simultaneously.  Input is an opened ELF file, a pointer to the
11040Sstevel@tonic-gate  * symbol table SCNTAB structure, and the filename.
11050Sstevel@tonic-gate  * Set the range of symbols to print if T_flag, and set
11060Sstevel@tonic-gate  * name of symbol to print if n_flag.
11070Sstevel@tonic-gate  */
11080Sstevel@tonic-gate static void
11090Sstevel@tonic-gate dump_symbol_table(Elf *elf_file, SCNTAB *p_symtab, char *filename)
11100Sstevel@tonic-gate {
11110Sstevel@tonic-gate 	Elf_Data  *sym_data;
11120Sstevel@tonic-gate 	GElf_Sym  T_range, n_range;	/* for use with -T and -n */
11130Sstevel@tonic-gate 	size_t count = 0;
11140Sstevel@tonic-gate 	size_t sym_size;
11150Sstevel@tonic-gate 	int index = 1;
11160Sstevel@tonic-gate 	int found_it = 0;
11170Sstevel@tonic-gate 	int i;
11180Sstevel@tonic-gate 	int adj = 0;			/*  field adjustment for elf64 */
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 	if (gelf_getclass(elf_file) == ELFCLASS64)
11211618Srie 		adj = 8;
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate 	if (p_symtab == NULL) {
11240Sstevel@tonic-gate 		(void) fprintf(stderr,
11250Sstevel@tonic-gate 		"%s: %s: could not get symbol table\n", prog_name, filename);
11260Sstevel@tonic-gate 		return;
11270Sstevel@tonic-gate 	}
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 	/* get symbol table data */
11300Sstevel@tonic-gate 	sym_data = NULL;
11310Sstevel@tonic-gate 	sym_size = 0;
11320Sstevel@tonic-gate 	if ((sym_data =
11330Sstevel@tonic-gate 	    elf_getdata(p_symtab->p_sd, NULL)) == NULL) {
11340Sstevel@tonic-gate 		(void) printf("\n%s:\n", p_symtab->scn_name);
11350Sstevel@tonic-gate 		(void) printf("No symbol table data\n");
11360Sstevel@tonic-gate 		return;
11370Sstevel@tonic-gate 	}
11380Sstevel@tonic-gate 	sym_size = sym_data->d_size;
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate 	count = sym_size / p_symtab->p_shdr.sh_entsize;
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 	if (n_flag && t_flag && !T_flag) {
11430Sstevel@tonic-gate 		/* LINTED */
11440Sstevel@tonic-gate 		for (i = 1; i < count; i++) {
11450Sstevel@tonic-gate 			(void) gelf_getsym(sym_data, i, &n_range);
11460Sstevel@tonic-gate 			if (strcmp(name, (char *)
11470Sstevel@tonic-gate 			    elf_strptr(elf_file,
11480Sstevel@tonic-gate 			    p_symtab->p_shdr.sh_link,
11490Sstevel@tonic-gate 			    n_range.st_name)) != 0) {
11500Sstevel@tonic-gate 				continue;
11510Sstevel@tonic-gate 			} else {
11520Sstevel@tonic-gate 				found_it = 1;
11530Sstevel@tonic-gate 				if (!p_flag) {
11540Sstevel@tonic-gate 					(void) printf(
11550Sstevel@tonic-gate "\n              ***** SYMBOL TABLE INFORMATION *****\n");
11560Sstevel@tonic-gate 					(void) printf(
11570Sstevel@tonic-gate "[Index]  %-*s%-*sType\tBind\tOther\tShndx\tName",
11580Sstevel@tonic-gate 			    12 + adj, "Value", 9 + adj, "Size");
11590Sstevel@tonic-gate 				}
11600Sstevel@tonic-gate 				(void) printf("\n%s:\n", p_symtab->scn_name);
11610Sstevel@tonic-gate 				print_symtab(elf_file, p_symtab, sym_data,
11620Sstevel@tonic-gate 				    1, i);
11630Sstevel@tonic-gate 			}
11640Sstevel@tonic-gate 		}   /* end for */
11650Sstevel@tonic-gate 		if (!found_it) {
11660Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s: %s not found\n",
11670Sstevel@tonic-gate 			prog_name, filename, name);
11680Sstevel@tonic-gate 		}
11690Sstevel@tonic-gate 	} else if (T_flag) {
11700Sstevel@tonic-gate 		T_num = check_range(T_low, T_hi, count, filename);
11710Sstevel@tonic-gate 		if (T_num < 0)
11720Sstevel@tonic-gate 			return;
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 		(void) gelf_getsym(sym_data, T_low-1, &T_range);
11750Sstevel@tonic-gate 		index = T_low;
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 		if (!p_flag) {
11780Sstevel@tonic-gate 			(void) printf(
11790Sstevel@tonic-gate "\n              ***** SYMBOL TABLE INFORMATION *****\n");
11800Sstevel@tonic-gate 			(void) printf(
11810Sstevel@tonic-gate "[Index]  %-*s%-*sType\tBind\tOther\tShndx\tName",
11820Sstevel@tonic-gate 			    12 + adj, "Value", 9 + adj, "Size");
11830Sstevel@tonic-gate 		}
11840Sstevel@tonic-gate 		(void) printf("\n%s:\n", p_symtab->scn_name);
11850Sstevel@tonic-gate 		print_symtab(elf_file, p_symtab, sym_data, T_num, index);
11860Sstevel@tonic-gate 	} else {
11870Sstevel@tonic-gate 		if (!p_flag) {
11880Sstevel@tonic-gate 			(void) printf(
11890Sstevel@tonic-gate "\n              ***** SYMBOL TABLE INFORMATION *****\n");
11900Sstevel@tonic-gate 			(void) printf(
11910Sstevel@tonic-gate "[Index]  %-*s%-*sType\tBind\tOther\tShndx\tName",
11920Sstevel@tonic-gate 			    12 + adj, "Value", 9 + adj, "Size");
11930Sstevel@tonic-gate 		}
11940Sstevel@tonic-gate 		(void) printf("\n%s:\n", p_symtab->scn_name);
11950Sstevel@tonic-gate 		print_symtab(elf_file, p_symtab, sym_data, count-1, 1);
11960Sstevel@tonic-gate 	}
11970Sstevel@tonic-gate }
11980Sstevel@tonic-gate 
1199*1698Sab196087 
1200*1698Sab196087 /*
1201*1698Sab196087  * The items in a dynamic section are displayed using a small set
1202*1698Sab196087  * of standard output styles. To reduce code size and promote
1203*1698Sab196087  * consistency, the following family of functions, all named
1204*1698Sab196087  * with the prefix "pdynitem_", are used by dump_dynamic().
1205*1698Sab196087  *
1206*1698Sab196087  * entry:
1207*1698Sab196087  *	name - Name of element
1208*1698Sab196087  *	p_dyn - Pointer to struct describing element
1209*1698Sab196087  *	elf_file, link - Used to look up a string from the string table
1210*1698Sab196087  *		in the ELF file.
1211*1698Sab196087  *	bitdesc - Pointer to NULL terminated array of PDYNITEM_BITDESC
1212*1698Sab196087  *		structs describing the allowed bit values.
1213*1698Sab196087  */
1214*1698Sab196087 #define	Fmttag		"%-15.15s "
1215*1698Sab196087 #define	Fmtptr		"%#llx"
1216*1698Sab196087 
1217*1698Sab196087 void
1218*1698Sab196087 pdynitem_addr(const char *name, GElf_Dyn *p_dyn)
1219*1698Sab196087 {
1220*1698Sab196087 	(void) printf(Fmttag, name);
1221*1698Sab196087 	(void) printf(Fmtptr, EC_ADDR(p_dyn->d_un.d_ptr));
1222*1698Sab196087 }
1223*1698Sab196087 
1224*1698Sab196087 void
1225*1698Sab196087 pdynitem_val(const char *name, GElf_Dyn *p_dyn)
1226*1698Sab196087 {
1227*1698Sab196087 	(void) printf(Fmttag, name);
1228*1698Sab196087 	(void) printf(Fmtptr, EC_XWORD(p_dyn->d_un.d_val));
1229*1698Sab196087 }
1230*1698Sab196087 
1231*1698Sab196087 void
1232*1698Sab196087 pdynitem_depval(const char *name, GElf_Dyn *p_dyn)
1233*1698Sab196087 {
1234*1698Sab196087 	(void) printf(Fmttag, name);
1235*1698Sab196087 	(void) printf(Fmtptr "  (deprecated value)",
1236*1698Sab196087 			EC_XWORD(p_dyn->d_un.d_val));
1237*1698Sab196087 }
1238*1698Sab196087 
1239*1698Sab196087 void
1240*1698Sab196087 pdynitem_ign(const char *name)
1241*1698Sab196087 {
1242*1698Sab196087 	(void) printf(Fmttag "(ignored)", name);
1243*1698Sab196087 }
1244*1698Sab196087 
1245*1698Sab196087 void
1246*1698Sab196087 pdynitem_str(const char *name, GElf_Dyn *p_dyn, Elf *elf_file, GElf_Word link)
1247*1698Sab196087 {
1248*1698Sab196087 	char *str;
1249*1698Sab196087 
1250*1698Sab196087 	(void) printf(Fmttag, name);
1251*1698Sab196087 	if (v_flag) {	/* Look up the string */
1252*1698Sab196087 		str = (char *)elf_strptr(elf_file, link, p_dyn->d_un.d_ptr);
1253*1698Sab196087 		if (!(str && *str))
1254*1698Sab196087 			str = (char *)UNKNOWN;
1255*1698Sab196087 		(void) printf("%s", str);
1256*1698Sab196087 	} else {	/* Show the address */
1257*1698Sab196087 		(void) printf(Fmtptr, EC_ADDR(p_dyn->d_un.d_ptr));
1258*1698Sab196087 	}
1259*1698Sab196087 }
1260*1698Sab196087 
1261*1698Sab196087 /*
1262*1698Sab196087  * Arrays of this type are used for the values argument to
1263*1698Sab196087  * pdynitem_bitmask(). It maps a bit value to a human readable
1264*1698Sab196087  * name string. The final element of such an array must always
1265*1698Sab196087  * be NULL.
1266*1698Sab196087  */
1267*1698Sab196087 typedef struct {
1268*1698Sab196087 	int bit;		/* Bit Value */
1269*1698Sab196087 	const char *name;	/* Name corresponding to bit */
1270*1698Sab196087 } PDYNITEM_BITDESC;
1271*1698Sab196087 
1272*1698Sab196087 void
1273*1698Sab196087 pdynitem_bitmask(const char *name, GElf_Dyn *p_dyn, PDYNITEM_BITDESC *bitdesc)
1274*1698Sab196087 {
1275*1698Sab196087 	char buf[512];
1276*1698Sab196087 
1277*1698Sab196087 	(void) printf(Fmttag, name);
1278*1698Sab196087 	if (v_flag) {
1279*1698Sab196087 		buf[0] = '\0';
1280*1698Sab196087 		for (; bitdesc->bit; bitdesc++) {
1281*1698Sab196087 			if (p_dyn->d_un.d_val &	bitdesc->bit)
1282*1698Sab196087 				(void) strlcat(buf, bitdesc->name,
1283*1698Sab196087 					sizeof (buf));
1284*1698Sab196087 		}
1285*1698Sab196087 		if (buf[0]) {
1286*1698Sab196087 			(void) printf("%s", buf);
1287*1698Sab196087 			return;
1288*1698Sab196087 		}
1289*1698Sab196087 	}
1290*1698Sab196087 
1291*1698Sab196087 	/* We don't have a string, or one is not requested. Show address */
1292*1698Sab196087 	(void) printf(Fmtptr, EC_ADDR(p_dyn->d_un.d_ptr));
1293*1698Sab196087 
1294*1698Sab196087 }
1295*1698Sab196087 
1296*1698Sab196087 #undef Fmttag
1297*1698Sab196087 #undef Fmtptr
1298*1698Sab196087 
12990Sstevel@tonic-gate /*
13000Sstevel@tonic-gate  * Print dynamic linking information.  Input is an ELF
13010Sstevel@tonic-gate  * file descriptor, the SCNTAB structure, the number of
13020Sstevel@tonic-gate  * sections, and the filename.
13030Sstevel@tonic-gate  */
13040Sstevel@tonic-gate static void
13050Sstevel@tonic-gate dump_dynamic(Elf *elf_file, SCNTAB *p_scns, int num_scns, char *filename)
13060Sstevel@tonic-gate {
1307*1698Sab196087 	/*
1308*1698Sab196087 	 * Map dynamic bit fields to readable strings. These tables
1309*1698Sab196087 	 * must be kept in sync with the definitions in <link.h>.
1310*1698Sab196087 	 *
1311*1698Sab196087 	 * The string displayed is always the same as the name used
1312*1698Sab196087 	 * for the constant, with the prefix removed. The BITDESC macro
1313*1698Sab196087 	 * generate both PDYNITEM_BITDESC fields from the base name.
1314*1698Sab196087 	 */
1315*1698Sab196087 #define	BITDESC(prefix, item) { prefix ## item, #item " " }
1316*1698Sab196087 
1317*1698Sab196087 	static PDYNITEM_BITDESC dyn_dt_flags[] = {	/* DT_FLAGS */
1318*1698Sab196087 		BITDESC(DF_, ORIGIN),
1319*1698Sab196087 		BITDESC(DF_, SYMBOLIC),
1320*1698Sab196087 		BITDESC(DF_, TEXTREL),
1321*1698Sab196087 		BITDESC(DF_, BIND_NOW),
1322*1698Sab196087 		BITDESC(DF_, STATIC_TLS),
1323*1698Sab196087 		{ 0 }
1324*1698Sab196087 	};
1325*1698Sab196087 
1326*1698Sab196087 	static PDYNITEM_BITDESC dyn_dt_flags_1[] = {	/* DT_FLAGS_1 */
1327*1698Sab196087 		BITDESC(DF_1_, NOW),
1328*1698Sab196087 		BITDESC(DF_1_, GLOBAL),
1329*1698Sab196087 		BITDESC(DF_1_, GROUP),
1330*1698Sab196087 		BITDESC(DF_1_, NODELETE),
1331*1698Sab196087 		BITDESC(DF_1_, LOADFLTR),
1332*1698Sab196087 		BITDESC(DF_1_, INITFIRST),
1333*1698Sab196087 		BITDESC(DF_1_, NOOPEN),
1334*1698Sab196087 		BITDESC(DF_1_, ORIGIN),
1335*1698Sab196087 		BITDESC(DF_1_, DIRECT),
1336*1698Sab196087 		BITDESC(DF_1_, TRANS),
1337*1698Sab196087 		BITDESC(DF_1_, INTERPOSE),
1338*1698Sab196087 		BITDESC(DF_1_, NODEFLIB),
1339*1698Sab196087 		BITDESC(DF_1_, NODUMP),
1340*1698Sab196087 		BITDESC(DF_1_, CONFALT),
1341*1698Sab196087 		BITDESC(DF_1_, ENDFILTEE),
1342*1698Sab196087 		BITDESC(DF_1_, DISPRELDNE),
1343*1698Sab196087 		BITDESC(DF_1_, DISPRELPND),
1344*1698Sab196087 		BITDESC(DF_1_, NODIRECT),
1345*1698Sab196087 		BITDESC(DF_1_, IGNMULDEF),
1346*1698Sab196087 		BITDESC(DF_1_, NOKSYMS),
1347*1698Sab196087 		BITDESC(DF_1_, NOHDR),
1348*1698Sab196087 		BITDESC(DF_1_, NORELOC),
1349*1698Sab196087 		{ 0 }
1350*1698Sab196087 	};
1351*1698Sab196087 
1352*1698Sab196087 	static PDYNITEM_BITDESC dyn_dt_feature_1[] = {	/* DT_FEATURE_1 */
1353*1698Sab196087 		BITDESC(DTF_1_, PARINIT),
1354*1698Sab196087 		BITDESC(DTF_1_, CONFEXP),
1355*1698Sab196087 		{ 0 }
1356*1698Sab196087 	};
1357*1698Sab196087 
1358*1698Sab196087 	static PDYNITEM_BITDESC dyn_dt_posflag_1[] = {	/* DT_POSFLAG_1 */
1359*1698Sab196087 		BITDESC(DF_P1_, LAZYLOAD),
1360*1698Sab196087 		BITDESC(DF_P1_, GROUPPERM),
1361*1698Sab196087 		{ 0 }
1362*1698Sab196087 	};
1363*1698Sab196087 
1364*1698Sab196087 #undef	BITDESC
1365*1698Sab196087 
13660Sstevel@tonic-gate 	Elf_Data	*dyn_data;
13670Sstevel@tonic-gate 	GElf_Dyn	p_dyn;
13680Sstevel@tonic-gate 	GElf_Phdr	p_phdr;
13690Sstevel@tonic-gate 	GElf_Ehdr	p_ehdr;
13700Sstevel@tonic-gate 	int		index = 1;
13710Sstevel@tonic-gate 	int		lib_scns = num_scns;
13720Sstevel@tonic-gate 	SCNTAB		*l_scns = p_scns;
13730Sstevel@tonic-gate 	int		header_num = 0;
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 	if (!p_flag)
13760Sstevel@tonic-gate 		(void) printf("\n  **** DYNAMIC SECTION INFORMATION ****\n");
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate 	for (; num_scns > 0; num_scns--, p_scns++) {
13790Sstevel@tonic-gate 		GElf_Word	link;
13800Sstevel@tonic-gate 		int		ii;
13810Sstevel@tonic-gate 
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate 		if (p_scns->p_shdr.sh_type != SHT_DYNAMIC)
13840Sstevel@tonic-gate 			continue;
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 		if (!p_flag) {
13870Sstevel@tonic-gate 			(void) printf("%s:\n", p_scns->scn_name);
13880Sstevel@tonic-gate 			(void) printf("[INDEX]\tTag         Value\n");
13890Sstevel@tonic-gate 		}
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate 		if ((dyn_data = elf_getdata(p_scns->p_sd, NULL)) == 0) {
13920Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s: no data in "
13930Sstevel@tonic-gate 			    "%s section\n", prog_name, filename,
13940Sstevel@tonic-gate 			    p_scns->scn_name);
13950Sstevel@tonic-gate 			return;
13960Sstevel@tonic-gate 		}
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate 		link = p_scns->p_shdr.sh_link;
13990Sstevel@tonic-gate 		ii = 0;
14000Sstevel@tonic-gate 
14010Sstevel@tonic-gate 		(void) gelf_getdyn(dyn_data, ii++, &p_dyn);
14020Sstevel@tonic-gate 		while (p_dyn.d_tag != DT_NULL) {
14030Sstevel@tonic-gate 			(void) printf("[%d]\t", index++);
14040Sstevel@tonic-gate 
1405*1698Sab196087 			/*
1406*1698Sab196087 			 * It would be nice to use a table driven loop
1407*1698Sab196087 			 * here, but the address space is too sparse
1408*1698Sab196087 			 * and irregular. A switch is simple and robust.
1409*1698Sab196087 			 */
14100Sstevel@tonic-gate 			switch (p_dyn.d_tag) {
14110Sstevel@tonic-gate 			/*
14120Sstevel@tonic-gate 			 * Start of generic flags.
14130Sstevel@tonic-gate 			 */
14140Sstevel@tonic-gate 			case (DT_NEEDED):
1415*1698Sab196087 				pdynitem_str("NEEDED", &p_dyn, elf_file, link);
14160Sstevel@tonic-gate 				break;
14170Sstevel@tonic-gate 			case (DT_PLTRELSZ):
1418*1698Sab196087 				pdynitem_val("PLTSZ", &p_dyn);
14190Sstevel@tonic-gate 				break;
14200Sstevel@tonic-gate 			case (DT_PLTGOT):
1421*1698Sab196087 				pdynitem_addr("PLTGOT", &p_dyn);
14220Sstevel@tonic-gate 				break;
14230Sstevel@tonic-gate 			case (DT_HASH):
1424*1698Sab196087 				pdynitem_addr("HASH", &p_dyn);
14250Sstevel@tonic-gate 				break;
14260Sstevel@tonic-gate 			case (DT_STRTAB):
1427*1698Sab196087 				pdynitem_addr("STRTAB", &p_dyn);
14280Sstevel@tonic-gate 				break;
14290Sstevel@tonic-gate 			case (DT_SYMTAB):
1430*1698Sab196087 				pdynitem_addr("SYMTAB", &p_dyn);
14310Sstevel@tonic-gate 				break;
14320Sstevel@tonic-gate 			case (DT_RELA):
1433*1698Sab196087 				pdynitem_addr("RELA", &p_dyn);
14340Sstevel@tonic-gate 				break;
14350Sstevel@tonic-gate 			case (DT_RELASZ):
1436*1698Sab196087 				pdynitem_val("RELASZ", &p_dyn);
14370Sstevel@tonic-gate 				break;
14380Sstevel@tonic-gate 			case (DT_RELAENT):
1439*1698Sab196087 				pdynitem_val("RELAENT", &p_dyn);
14400Sstevel@tonic-gate 				break;
14410Sstevel@tonic-gate 			case (DT_STRSZ):
1442*1698Sab196087 				pdynitem_val("STRSZ", &p_dyn);
14430Sstevel@tonic-gate 				break;
14440Sstevel@tonic-gate 			case (DT_SYMENT):
1445*1698Sab196087 				pdynitem_val("SYMENT", &p_dyn);
14460Sstevel@tonic-gate 				break;
14470Sstevel@tonic-gate 			case (DT_INIT):
1448*1698Sab196087 				pdynitem_addr("INIT", &p_dyn);
14490Sstevel@tonic-gate 				break;
14500Sstevel@tonic-gate 			case (DT_FINI):
1451*1698Sab196087 				pdynitem_addr("FINI", &p_dyn);
14520Sstevel@tonic-gate 				break;
14530Sstevel@tonic-gate 			case (DT_SONAME):
1454*1698Sab196087 				pdynitem_str("SONAME", &p_dyn, elf_file, link);
14550Sstevel@tonic-gate 				break;
14560Sstevel@tonic-gate 			case (DT_RPATH):
1457*1698Sab196087 				pdynitem_str("RPATH", &p_dyn, elf_file, link);
14580Sstevel@tonic-gate 				break;
14590Sstevel@tonic-gate 			case (DT_SYMBOLIC):
1460*1698Sab196087 				pdynitem_ign("SYMB");
14610Sstevel@tonic-gate 				break;
14620Sstevel@tonic-gate 			case (DT_REL):
1463*1698Sab196087 				pdynitem_addr("REL", &p_dyn);
14640Sstevel@tonic-gate 				break;
14650Sstevel@tonic-gate 			case (DT_RELSZ):
1466*1698Sab196087 				pdynitem_val("RELSZ", &p_dyn);
14670Sstevel@tonic-gate 				break;
14680Sstevel@tonic-gate 			case (DT_RELENT):
1469*1698Sab196087 				pdynitem_val("RELENT", &p_dyn);
14700Sstevel@tonic-gate 				break;
14710Sstevel@tonic-gate 			case (DT_PLTREL):
1472*1698Sab196087 				pdynitem_val("PLTREL", &p_dyn);
14730Sstevel@tonic-gate 				break;
14740Sstevel@tonic-gate 			case (DT_DEBUG):
1475*1698Sab196087 				pdynitem_addr("DEBUG", &p_dyn);
14760Sstevel@tonic-gate 				break;
14770Sstevel@tonic-gate 			case (DT_TEXTREL):
1478*1698Sab196087 				pdynitem_addr("TEXTREL", &p_dyn);
14790Sstevel@tonic-gate 				break;
14800Sstevel@tonic-gate 			case (DT_JMPREL):
1481*1698Sab196087 				pdynitem_addr("JMPREL", &p_dyn);
14820Sstevel@tonic-gate 				break;
14830Sstevel@tonic-gate 			case (DT_BIND_NOW):
1484*1698Sab196087 				pdynitem_val("BIND_NOW", &p_dyn);
14850Sstevel@tonic-gate 				break;
14860Sstevel@tonic-gate 			case (DT_INIT_ARRAY):
1487*1698Sab196087 				pdynitem_addr("INIT_ARRAY", &p_dyn);
14880Sstevel@tonic-gate 				break;
14890Sstevel@tonic-gate 			case (DT_FINI_ARRAY):
1490*1698Sab196087 				pdynitem_addr("FINI_ARRAY", &p_dyn);
14910Sstevel@tonic-gate 				break;
14920Sstevel@tonic-gate 			case (DT_INIT_ARRAYSZ):
1493*1698Sab196087 				pdynitem_addr("INIT_ARRAYSZ", &p_dyn);
14940Sstevel@tonic-gate 				break;
14950Sstevel@tonic-gate 			case (DT_FINI_ARRAYSZ):
1496*1698Sab196087 				pdynitem_addr("FINI_ARRAYSZ", &p_dyn);
14970Sstevel@tonic-gate 				break;
14980Sstevel@tonic-gate 			case (DT_RUNPATH):
1499*1698Sab196087 				pdynitem_str("RUNPATH", &p_dyn,
1500*1698Sab196087 					elf_file, link);
15010Sstevel@tonic-gate 				break;
15020Sstevel@tonic-gate 			case (DT_FLAGS):
1503*1698Sab196087 				pdynitem_bitmask("FLAGS", &p_dyn,
1504*1698Sab196087 					dyn_dt_flags);
15050Sstevel@tonic-gate 				break;
1506*1698Sab196087 
15070Sstevel@tonic-gate 			case (DT_PREINIT_ARRAY):
1508*1698Sab196087 				pdynitem_addr("PRINIT_ARRAY", &p_dyn);
15090Sstevel@tonic-gate 				break;
15100Sstevel@tonic-gate 			case (DT_PREINIT_ARRAYSZ):
1511*1698Sab196087 				pdynitem_addr("PRINIT_ARRAYSZ", &p_dyn);
15120Sstevel@tonic-gate 				break;
15130Sstevel@tonic-gate 			/*
15140Sstevel@tonic-gate 			 * DT_LOOS - DT_HIOS range.
15150Sstevel@tonic-gate 			 */
15160Sstevel@tonic-gate 			case (DT_SUNW_AUXILIARY):
1517*1698Sab196087 				pdynitem_str("SUNW_AUXILIARY", &p_dyn,
1518*1698Sab196087 					elf_file, link);
15190Sstevel@tonic-gate 				break;
15200Sstevel@tonic-gate 			case (DT_SUNW_RTLDINF):
1521*1698Sab196087 				pdynitem_addr("SUNW_RTLDINF", &p_dyn);
15220Sstevel@tonic-gate 				break;
15230Sstevel@tonic-gate 			case (DT_SUNW_FILTER):
1524*1698Sab196087 				pdynitem_str("SUNW_FILTER", &p_dyn,
1525*1698Sab196087 					elf_file, link);
15260Sstevel@tonic-gate 				break;
15270Sstevel@tonic-gate 			case (DT_SUNW_CAP):
1528*1698Sab196087 				pdynitem_addr("SUNW_CAP", &p_dyn);
15290Sstevel@tonic-gate 				break;
15300Sstevel@tonic-gate 
15310Sstevel@tonic-gate 			/*
15320Sstevel@tonic-gate 			 * SUNW: DT_VALRNGLO - DT_VALRNGHI range.
15330Sstevel@tonic-gate 			 */
15340Sstevel@tonic-gate 			case (DT_CHECKSUM):
1535*1698Sab196087 				pdynitem_val("CHECKSUM", &p_dyn);
15360Sstevel@tonic-gate 				break;
15370Sstevel@tonic-gate 			case (DT_PLTPADSZ):
1538*1698Sab196087 				pdynitem_val("PLTPADSZ", &p_dyn);
15390Sstevel@tonic-gate 				break;
15400Sstevel@tonic-gate 			case (DT_MOVEENT):
1541*1698Sab196087 				pdynitem_val("MOVEENT", &p_dyn);
15420Sstevel@tonic-gate 				break;
15430Sstevel@tonic-gate 			case (DT_MOVESZ):
1544*1698Sab196087 				pdynitem_val("MOVESZ", &p_dyn);
15450Sstevel@tonic-gate 				break;
15460Sstevel@tonic-gate 			case (DT_FEATURE_1):
1547*1698Sab196087 				pdynitem_bitmask("FEATURE_1", &p_dyn,
1548*1698Sab196087 					dyn_dt_feature_1);
15490Sstevel@tonic-gate 				break;
15500Sstevel@tonic-gate 			case (DT_POSFLAG_1):
1551*1698Sab196087 				pdynitem_bitmask("POSFLAG_1", &p_dyn,
1552*1698Sab196087 					dyn_dt_posflag_1);
15530Sstevel@tonic-gate 				break;
15540Sstevel@tonic-gate 			case (DT_SYMINSZ):
1555*1698Sab196087 				pdynitem_val("SYMINSZ", &p_dyn);
15560Sstevel@tonic-gate 				break;
15570Sstevel@tonic-gate 			case (DT_SYMINENT):
1558*1698Sab196087 				pdynitem_val("SYMINENT", &p_dyn);
15590Sstevel@tonic-gate 				break;
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate 			/*
15620Sstevel@tonic-gate 			 * SUNW: DT_ADDRRNGLO - DT_ADDRRNGHI range.
15630Sstevel@tonic-gate 			 */
15640Sstevel@tonic-gate 			case (DT_CONFIG):
1565*1698Sab196087 				pdynitem_str("CONFIG", &p_dyn, elf_file, link);
15660Sstevel@tonic-gate 				break;
15670Sstevel@tonic-gate 			case (DT_DEPAUDIT):
1568*1698Sab196087 				pdynitem_str("DEPAUDIT", &p_dyn,
1569*1698Sab196087 					elf_file, link);
15700Sstevel@tonic-gate 				break;
15710Sstevel@tonic-gate 			case (DT_AUDIT):
1572*1698Sab196087 				pdynitem_str("AUDIT", &p_dyn, elf_file, link);
15730Sstevel@tonic-gate 				break;
15740Sstevel@tonic-gate 			case (DT_PLTPAD):
1575*1698Sab196087 				pdynitem_addr("PLTPAD", &p_dyn);
15760Sstevel@tonic-gate 				break;
15770Sstevel@tonic-gate 			case (DT_MOVETAB):
1578*1698Sab196087 				pdynitem_addr("MOVETAB", &p_dyn);
15790Sstevel@tonic-gate 				break;
15800Sstevel@tonic-gate 			case (DT_SYMINFO):
1581*1698Sab196087 				pdynitem_addr("SYMINFO", &p_dyn);
15820Sstevel@tonic-gate 				break;
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate 			/*
15850Sstevel@tonic-gate 			 * SUNW: generic range.
15860Sstevel@tonic-gate 			 */
15870Sstevel@tonic-gate 			case (DT_RELACOUNT):
1588*1698Sab196087 				pdynitem_addr("RELACOUNT", &p_dyn);
15890Sstevel@tonic-gate 				break;
15900Sstevel@tonic-gate 			case (DT_RELCOUNT):
1591*1698Sab196087 				pdynitem_addr("RELCOUNT", &p_dyn);
15920Sstevel@tonic-gate 				break;
15930Sstevel@tonic-gate 			case (DT_FLAGS_1):
1594*1698Sab196087 				pdynitem_bitmask("FLAGS_1", &p_dyn,
1595*1698Sab196087 					dyn_dt_flags_1);
15960Sstevel@tonic-gate 				break;
15970Sstevel@tonic-gate 			case (DT_VERSYM):
1598*1698Sab196087 				pdynitem_addr("VERSYM", &p_dyn);
15990Sstevel@tonic-gate 				break;
16000Sstevel@tonic-gate 			case (DT_VERDEF):
1601*1698Sab196087 				pdynitem_addr("VERDEF", &p_dyn);
16020Sstevel@tonic-gate 				break;
16030Sstevel@tonic-gate 			case (DT_VERDEFNUM):
1604*1698Sab196087 				pdynitem_addr("VERDEFNUM", &p_dyn);
16050Sstevel@tonic-gate 				break;
16060Sstevel@tonic-gate 			case (DT_VERNEED):
1607*1698Sab196087 				pdynitem_addr("VERNEED", &p_dyn);
16080Sstevel@tonic-gate 				break;
16090Sstevel@tonic-gate 			case (DT_VERNEEDNUM):
1610*1698Sab196087 				pdynitem_val("VERNEEDNUM", &p_dyn);
16110Sstevel@tonic-gate 				break;
16120Sstevel@tonic-gate 			case (DT_AUXILIARY):
1613*1698Sab196087 				pdynitem_str("AUXILIARY", &p_dyn,
1614*1698Sab196087 					elf_file, link);
16150Sstevel@tonic-gate 				break;
16160Sstevel@tonic-gate 			case (DT_USED):
1617*1698Sab196087 				pdynitem_str("USED", &p_dyn, elf_file, link);
16180Sstevel@tonic-gate 				break;
16190Sstevel@tonic-gate 			case (DT_FILTER):
1620*1698Sab196087 				pdynitem_str("FILTER", &p_dyn, elf_file, link);
16210Sstevel@tonic-gate 				break;
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate 			/*
16240Sstevel@tonic-gate 			 * SUNW: machine specific range.
16250Sstevel@tonic-gate 			 */
16260Sstevel@tonic-gate 			case (DT_SPARC_REGISTER):
1627*1698Sab196087 				pdynitem_val("REGISTER", &p_dyn);
16280Sstevel@tonic-gate 				break;
16290Sstevel@tonic-gate 			case (DT_DEPRECATED_SPARC_REGISTER):
1630*1698Sab196087 				pdynitem_depval("REGISTER", &p_dyn);
16310Sstevel@tonic-gate 				break;
16320Sstevel@tonic-gate 			default:
16330Sstevel@tonic-gate 				(void) printf("%lld", EC_XWORD(p_dyn.d_tag));
16340Sstevel@tonic-gate 				break;
16350Sstevel@tonic-gate 			}
16360Sstevel@tonic-gate 			(void) printf("\n");
16370Sstevel@tonic-gate 			(void) gelf_getdyn(dyn_data, ii++, &p_dyn);
16380Sstevel@tonic-gate 		}
16390Sstevel@tonic-gate 	}
16400Sstevel@tonic-gate 
16410Sstevel@tonic-gate 	/*
16420Sstevel@tonic-gate 	 * Check for existence of static shared library information.
16430Sstevel@tonic-gate 	 */
16440Sstevel@tonic-gate 	(void) gelf_getehdr(elf_file, &p_ehdr);
16450Sstevel@tonic-gate 	while (header_num < p_ehdr.e_phnum) {
16460Sstevel@tonic-gate 		(void) gelf_getphdr(elf_file, header_num, &p_phdr);
16470Sstevel@tonic-gate 		if (p_phdr.p_type == PT_SHLIB) {
16480Sstevel@tonic-gate 			while (--lib_scns > 0) {
16490Sstevel@tonic-gate 				if (strcmp(l_scns->scn_name, ".lib") == 0) {
16500Sstevel@tonic-gate 					print_static(l_scns, filename);
16510Sstevel@tonic-gate 				}
16520Sstevel@tonic-gate 				l_scns++;
16530Sstevel@tonic-gate 			}
16540Sstevel@tonic-gate 		}
16550Sstevel@tonic-gate 		header_num++;
16560Sstevel@tonic-gate 	}
16570Sstevel@tonic-gate }
16580Sstevel@tonic-gate 
16590Sstevel@tonic-gate /*
16600Sstevel@tonic-gate  * Print the ELF header.  Input is an ELF file descriptor
16610Sstevel@tonic-gate  * and the filename.  If f_flag is set, the ELF header is
16620Sstevel@tonic-gate  * printed to stdout, otherwise the function returns after
16630Sstevel@tonic-gate  * setting the pointer to the ELF header.  Any values which
16640Sstevel@tonic-gate  * are not known are printed in decimal.  Fields must be updated
16650Sstevel@tonic-gate  * as new values are added.
16660Sstevel@tonic-gate  */
16670Sstevel@tonic-gate static GElf_Ehdr *
16680Sstevel@tonic-gate dump_elf_header(Elf *elf_file, char *filename, GElf_Ehdr * elf_head_p)
16690Sstevel@tonic-gate {
16700Sstevel@tonic-gate 	int class, data;
16710Sstevel@tonic-gate 	int field;
16720Sstevel@tonic-gate 
16730Sstevel@tonic-gate 	if (gelf_getehdr(elf_file, elf_head_p) == NULL) {
16741618Srie 		(void) fprintf(stderr, "%s: %s: %s\n", prog_name, filename,
16751618Srie 		    elf_errmsg(-1));
16760Sstevel@tonic-gate 		return (NULL);
16770Sstevel@tonic-gate 	}
16780Sstevel@tonic-gate 
16790Sstevel@tonic-gate 	class = (int)elf_head_p->e_ident[4];
16800Sstevel@tonic-gate 
16810Sstevel@tonic-gate 	if (class == ELFCLASS64)
16821618Srie 		field = 21;
16830Sstevel@tonic-gate 	else
16840Sstevel@tonic-gate 		field = 13;
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 	if (!f_flag)
16870Sstevel@tonic-gate 		return (elf_head_p);
16880Sstevel@tonic-gate 
16890Sstevel@tonic-gate 	if (!p_flag) {
16901618Srie 		(void) printf("\n                    **** ELF HEADER ****\n");
16911618Srie 		(void) printf("%-*s%-11s%-*sMachine     Version\n",
16921618Srie 		    field, "Class", "Data", field, "Type");
16931618Srie 		(void) printf("%-*s%-11s%-*sFlags       Ehsize\n",
16941618Srie 		    field, "Entry", "Phoff", field, "Shoff");
16951618Srie 		(void) printf("%-*s%-11s%-*sShnum       Shstrndx\n\n",
16961618Srie 		    field, "Phentsize", "Phnum", field, "Shentsz");
16970Sstevel@tonic-gate 	}
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate 	if (!v_flag) {
17000Sstevel@tonic-gate 		(void) printf("%-*d%-11d%-*d%-12d%d\n",
17010Sstevel@tonic-gate 			field, elf_head_p->e_ident[4],
17020Sstevel@tonic-gate 			elf_head_p->e_ident[5],
17030Sstevel@tonic-gate 			field, (int)elf_head_p->e_type,
17040Sstevel@tonic-gate 			(int)elf_head_p->e_machine,
17050Sstevel@tonic-gate 			elf_head_p->e_version);
17060Sstevel@tonic-gate 	} else {
17070Sstevel@tonic-gate 		data = elf_head_p->e_ident[5];
17080Sstevel@tonic-gate 
17090Sstevel@tonic-gate 		switch (class) {
17100Sstevel@tonic-gate 		case ELFCLASSNONE:
17110Sstevel@tonic-gate 			(void) printf("%-*s", field, "None");
17120Sstevel@tonic-gate 			break;
17130Sstevel@tonic-gate 		case ELFCLASS32:
17140Sstevel@tonic-gate 			(void) printf("%-*s", field, "32-bit");
17150Sstevel@tonic-gate 			break;
17160Sstevel@tonic-gate 		case ELFCLASS64:
17170Sstevel@tonic-gate 			(void) printf("%-*s", field, "64-bit");
17180Sstevel@tonic-gate 			break;
17190Sstevel@tonic-gate 		default:
17200Sstevel@tonic-gate 			(void) printf("%-*d", field, class);
17210Sstevel@tonic-gate 			break;
17220Sstevel@tonic-gate 		}
17230Sstevel@tonic-gate 		switch (data) {
17240Sstevel@tonic-gate 		case ELFDATANONE:
17250Sstevel@tonic-gate 			(void) printf("%-11s", "None   ");
17260Sstevel@tonic-gate 			break;
17270Sstevel@tonic-gate 		case ELFDATA2LSB:
17280Sstevel@tonic-gate 			(void) printf("%-11s", "2LSB   ");
17290Sstevel@tonic-gate 			break;
17300Sstevel@tonic-gate 		case ELFDATA2MSB:
17310Sstevel@tonic-gate 			(void) printf("%-11s", "2MSB   ");
17320Sstevel@tonic-gate 			break;
17330Sstevel@tonic-gate 		default:
17340Sstevel@tonic-gate 			(void) printf("%-11d", data);
17350Sstevel@tonic-gate 			break;
17360Sstevel@tonic-gate 		}
17370Sstevel@tonic-gate 
17380Sstevel@tonic-gate 		switch (elf_head_p->e_type) {
17390Sstevel@tonic-gate 		case ET_NONE:
17400Sstevel@tonic-gate 			(void) printf("%-*s", field, "None");
17410Sstevel@tonic-gate 			break;
17420Sstevel@tonic-gate 		case ET_REL:
17430Sstevel@tonic-gate 			(void) printf("%-*s", field, "Reloc");
17440Sstevel@tonic-gate 			break;
17450Sstevel@tonic-gate 		case ET_EXEC:
17460Sstevel@tonic-gate 			(void) printf("%-*s", field, "Exec");
17470Sstevel@tonic-gate 			break;
17480Sstevel@tonic-gate 		case ET_DYN:
17490Sstevel@tonic-gate 			(void) printf("%-*s", field, "Dyn");
17500Sstevel@tonic-gate 			break;
17510Sstevel@tonic-gate 		case ET_CORE:
17520Sstevel@tonic-gate 			(void) printf("%-*s", field, "Core");
17530Sstevel@tonic-gate 			break;
17540Sstevel@tonic-gate 		default:
17550Sstevel@tonic-gate 			(void) printf("%-*d", field,
17560Sstevel@tonic-gate 				EC_WORD(elf_head_p->e_type));
17570Sstevel@tonic-gate 			break;
17580Sstevel@tonic-gate 		}
17590Sstevel@tonic-gate 		switch (elf_head_p->e_machine) {
17600Sstevel@tonic-gate 		case EM_NONE:
17610Sstevel@tonic-gate 			(void) printf("%-12s", "No mach");
17620Sstevel@tonic-gate 			break;
17630Sstevel@tonic-gate 		case EM_M32:
17640Sstevel@tonic-gate 			(void) printf("%-12s", "WE32100");
17650Sstevel@tonic-gate 			break;
17660Sstevel@tonic-gate 		case EM_SPARC:
17670Sstevel@tonic-gate 			(void) printf("%-12s", "SPARC");
17680Sstevel@tonic-gate 			break;
17690Sstevel@tonic-gate 		case EM_SPARCV9:
17700Sstevel@tonic-gate 			(void) printf("%-12s", "SPARCV9");
17710Sstevel@tonic-gate 			break;
17720Sstevel@tonic-gate 		case EM_386:
17730Sstevel@tonic-gate 			(void) printf("%-12s", "80386");
17740Sstevel@tonic-gate 			break;
17750Sstevel@tonic-gate 		case EM_68K:
17760Sstevel@tonic-gate 			(void) printf("%-12s", "68000");
17770Sstevel@tonic-gate 			break;
17780Sstevel@tonic-gate 		case EM_88K:
17790Sstevel@tonic-gate 			(void) printf("%-12s", "88000");
17800Sstevel@tonic-gate 			break;
17810Sstevel@tonic-gate 		case EM_486:
17820Sstevel@tonic-gate 			(void) printf("%-12s", "80486");
17830Sstevel@tonic-gate 			break;
17840Sstevel@tonic-gate 		case EM_860:
17850Sstevel@tonic-gate 			(void) printf("%-12s", "i860");
17860Sstevel@tonic-gate 			break;
17870Sstevel@tonic-gate 		case EM_MIPS:
17880Sstevel@tonic-gate 			(void) printf("%-12s", "RS3000_BE");
17890Sstevel@tonic-gate 			break;
17900Sstevel@tonic-gate 		case EM_MIPS_RS3_LE:
17910Sstevel@tonic-gate 			(void) printf("%-12s", "RS3000_LE");
17920Sstevel@tonic-gate 			break;
17930Sstevel@tonic-gate 		case EM_RS6000:
17940Sstevel@tonic-gate 			(void) printf("%-12s", "RS6000");
17950Sstevel@tonic-gate 			break;
17960Sstevel@tonic-gate 		case EM_PA_RISC:
17970Sstevel@tonic-gate 			(void) printf("%-12s", "PA_RISC");
17980Sstevel@tonic-gate 			break;
17990Sstevel@tonic-gate 		case EM_nCUBE:
18000Sstevel@tonic-gate 			(void) printf("%-12s", "nCUBE");
18010Sstevel@tonic-gate 			break;
18020Sstevel@tonic-gate 		case EM_VPP500:
18030Sstevel@tonic-gate 			(void) printf("%-12s", "VPP500");
18040Sstevel@tonic-gate 			break;
18050Sstevel@tonic-gate 		case EM_SPARC32PLUS:
18060Sstevel@tonic-gate 			(void) printf("%-12s", "SPARC32PLUS");
18070Sstevel@tonic-gate 			break;
18080Sstevel@tonic-gate 		case EM_PPC:
18090Sstevel@tonic-gate 			(void) printf("%-12s", "PowerPC");
18100Sstevel@tonic-gate 			break;
18110Sstevel@tonic-gate 		case EM_IA_64:
18120Sstevel@tonic-gate 			(void) printf("%-12s", "IA64");
18130Sstevel@tonic-gate 			break;
18140Sstevel@tonic-gate 		default:
18150Sstevel@tonic-gate 			(void) printf("%-12d", EC_WORD(elf_head_p->e_machine));
18160Sstevel@tonic-gate 		}
18170Sstevel@tonic-gate 		switch (elf_head_p->e_version) {
18180Sstevel@tonic-gate 		case EV_NONE:
18190Sstevel@tonic-gate 			(void) printf("Invalid\n");
18200Sstevel@tonic-gate 			break;
18210Sstevel@tonic-gate 		case EV_CURRENT:
18220Sstevel@tonic-gate 			(void) printf("Current\n");
18230Sstevel@tonic-gate 			break;
18240Sstevel@tonic-gate 		default:
18250Sstevel@tonic-gate 			(void) printf("%d\n", elf_head_p->e_version);
18260Sstevel@tonic-gate 		}
18270Sstevel@tonic-gate 	}
18280Sstevel@tonic-gate 	(void) printf("%-#*llx%-#11llx%-#*llx%-#12x%#x\n",
18290Sstevel@tonic-gate 		field, EC_ADDR(elf_head_p->e_entry),
18300Sstevel@tonic-gate 		EC_OFF(elf_head_p->e_phoff),
18310Sstevel@tonic-gate 		field, EC_OFF(elf_head_p->e_shoff),
18320Sstevel@tonic-gate 		EC_WORD(elf_head_p->e_flags),
18330Sstevel@tonic-gate 		EC_WORD(elf_head_p->e_ehsize));
18340Sstevel@tonic-gate 	if (!v_flag || (elf_head_p->e_shstrndx != SHN_XINDEX)) {
18350Sstevel@tonic-gate 		(void) printf("%-#*x%-11u%-#*x%-12u%u\n",
18360Sstevel@tonic-gate 			field, EC_WORD(elf_head_p->e_phentsize),
18370Sstevel@tonic-gate 			EC_WORD(elf_head_p->e_phnum),
18380Sstevel@tonic-gate 			field, EC_WORD(elf_head_p->e_shentsize),
18390Sstevel@tonic-gate 			EC_WORD(elf_head_p->e_shnum),
18400Sstevel@tonic-gate 			EC_WORD(elf_head_p->e_shstrndx));
18410Sstevel@tonic-gate 	} else {
18420Sstevel@tonic-gate 		(void) printf("%-#*x%-11u%-#*x%-12uXINDEX\n",
18430Sstevel@tonic-gate 			field, EC_WORD(elf_head_p->e_phentsize),
18440Sstevel@tonic-gate 			EC_WORD(elf_head_p->e_phnum),
18450Sstevel@tonic-gate 			field, EC_WORD(elf_head_p->e_shentsize),
18460Sstevel@tonic-gate 			EC_WORD(elf_head_p->e_shnum));
18470Sstevel@tonic-gate 	}
18480Sstevel@tonic-gate 	if ((elf_head_p->e_shnum == 0) && (elf_head_p->e_shoff > 0)) {
18490Sstevel@tonic-gate 		Elf_Scn		*scn;
18500Sstevel@tonic-gate 		GElf_Shdr	shdr0;
18510Sstevel@tonic-gate 		int		field;
18520Sstevel@tonic-gate 
18530Sstevel@tonic-gate 		if (gelf_getclass(elf_file) == ELFCLASS64)
18541618Srie 			field = 21;
18550Sstevel@tonic-gate 		else
18561618Srie 			field = 13;
18570Sstevel@tonic-gate 		if (!p_flag) {
18580Sstevel@tonic-gate 			(void) printf("\n	   **** SECTION HEADER[0] "
18590Sstevel@tonic-gate 			    "{Elf Extensions} ****\n");
18600Sstevel@tonic-gate 			(void) printf(
18610Sstevel@tonic-gate 			    "[No]\tType\tFlags\t%-*s %-*s%-*s%sName\n",
18620Sstevel@tonic-gate 			    field, "Addr", field, "Offset", field,
18630Sstevel@tonic-gate 			    "Size(shnum)",
18640Sstevel@tonic-gate 			    /* compatibility:  tab for elf32 */
18651618Srie 			    (field == 13) ? "\t" : "  ");
18660Sstevel@tonic-gate 			(void) printf("\tLn(strndx) Info\t%-*s Entsize\n",
18670Sstevel@tonic-gate 			    field, "Adralgn");
18680Sstevel@tonic-gate 		}
18690Sstevel@tonic-gate 		if ((scn = elf_getscn(elf_file, 0)) == NULL) {
18700Sstevel@tonic-gate 			(void) fprintf(stderr,
18710Sstevel@tonic-gate 				"%s: %s: elf_getscn failed: %s\n",
18720Sstevel@tonic-gate 				prog_name, filename, elf_errmsg(-1));
18730Sstevel@tonic-gate 			return (NULL);
18740Sstevel@tonic-gate 		}
18750Sstevel@tonic-gate 		if (gelf_getshdr(scn, &shdr0) == 0) {
18760Sstevel@tonic-gate 			(void) fprintf(stderr,
18770Sstevel@tonic-gate 				"%s: %s: gelf_getshdr: %s\n",
18780Sstevel@tonic-gate 				prog_name, filename, elf_errmsg(-1));
18790Sstevel@tonic-gate 			return (NULL);
18800Sstevel@tonic-gate 		}
18810Sstevel@tonic-gate 		(void) printf("[0]\t%u\t%llu\t", EC_WORD(shdr0.sh_type),
18820Sstevel@tonic-gate 			EC_XWORD(shdr0.sh_flags));
18830Sstevel@tonic-gate 
18840Sstevel@tonic-gate 		/*
18850Sstevel@tonic-gate 		 * LINTED - field and EC_XWORD cause -#*llu complaints that
18860Sstevel@tonic-gate 		 * even this comment can't shutup.
18870Sstevel@tonic-gate 		 */
18880Sstevel@tonic-gate 		(void) printf("%-#*llx %-#*llx%-#*llu%s%-#*u\n",
18890Sstevel@tonic-gate 			field, EC_ADDR(shdr0.sh_addr),
18900Sstevel@tonic-gate 			field, EC_OFF(shdr0.sh_offset),
18910Sstevel@tonic-gate 			field, EC_XWORD(shdr0.sh_size),
18920Sstevel@tonic-gate 			/* compatibility:  tab for elf32 */
18931618Srie 			((field == 13) ? "\t" : "  "),
18940Sstevel@tonic-gate 			field, EC_WORD(shdr0.sh_name));
18950Sstevel@tonic-gate 
18960Sstevel@tonic-gate 		(void) printf("\t%u\t%u\t%-#*llx %-#*llx\n",
18970Sstevel@tonic-gate 			EC_WORD(shdr0.sh_link),
18980Sstevel@tonic-gate 			EC_WORD(shdr0.sh_info),
18990Sstevel@tonic-gate 			field, EC_XWORD(shdr0.sh_addralign),
19000Sstevel@tonic-gate 			field, EC_XWORD(shdr0.sh_entsize));
19010Sstevel@tonic-gate 	}
19020Sstevel@tonic-gate 	(void) printf("\n");
19030Sstevel@tonic-gate 
19040Sstevel@tonic-gate 	return (elf_head_p);
19050Sstevel@tonic-gate }
19060Sstevel@tonic-gate 
19070Sstevel@tonic-gate /*
19080Sstevel@tonic-gate  * Print section contents.  Input is an ELF file descriptor,
19090Sstevel@tonic-gate  * the ELF header, the SCNTAB structure,
19100Sstevel@tonic-gate  * the number of symbols, and the filename.
19110Sstevel@tonic-gate  * The number of sections,
19120Sstevel@tonic-gate  * and the offset into the SCNTAB structure will be
19130Sstevel@tonic-gate  * set in dump_section if d_flag or n_flag are set.
19140Sstevel@tonic-gate  * If v_flag is set, sections which can be interpreted will
19150Sstevel@tonic-gate  * be interpreted, otherwise raw data will be output in hexidecimal.
19160Sstevel@tonic-gate  */
19170Sstevel@tonic-gate static void
19180Sstevel@tonic-gate print_section(Elf *elf_file,
19190Sstevel@tonic-gate 	GElf_Ehdr *p_ehdr, SCNTAB *p, int num_scns, char *filename)
19200Sstevel@tonic-gate {
19210Sstevel@tonic-gate 	unsigned char    *p_sec;
19220Sstevel@tonic-gate 	int	i;
19230Sstevel@tonic-gate 	size_t	size;
19240Sstevel@tonic-gate 
19250Sstevel@tonic-gate 	for (i = 0; i < num_scns; i++, p++) {
19260Sstevel@tonic-gate 		GElf_Shdr shdr;
19270Sstevel@tonic-gate 
19280Sstevel@tonic-gate 		size = 0;
19290Sstevel@tonic-gate 		if (s_flag && !v_flag)
19300Sstevel@tonic-gate 			p_sec = (unsigned char *)get_rawscn(p->p_sd, &size);
19310Sstevel@tonic-gate 		else
19320Sstevel@tonic-gate 			p_sec = (unsigned char *)get_scndata(p->p_sd, &size);
19330Sstevel@tonic-gate 
19340Sstevel@tonic-gate 		if ((gelf_getshdr(p->p_sd, &shdr) != NULL) &&
19350Sstevel@tonic-gate 		    (shdr.sh_type == SHT_NOBITS)) {
19360Sstevel@tonic-gate 			continue;
19370Sstevel@tonic-gate 		}
19380Sstevel@tonic-gate 		if (s_flag && !v_flag) {
19390Sstevel@tonic-gate 			(void) printf("\n%s:\n", p->scn_name);
19400Sstevel@tonic-gate 			print_rawdata(p_sec, size);
19410Sstevel@tonic-gate 			continue;
19420Sstevel@tonic-gate 		}
19430Sstevel@tonic-gate 		if (shdr.sh_type == SHT_SYMTAB) {
19440Sstevel@tonic-gate 			dump_symbol_table(elf_file, p, filename);
19450Sstevel@tonic-gate 			continue;
19460Sstevel@tonic-gate 		}
19470Sstevel@tonic-gate 		if (shdr.sh_type == SHT_DYNSYM) {
19480Sstevel@tonic-gate 			dump_symbol_table(elf_file, p, filename);
19490Sstevel@tonic-gate 			continue;
19500Sstevel@tonic-gate 		}
19510Sstevel@tonic-gate 		if (shdr.sh_type == SHT_STRTAB) {
19520Sstevel@tonic-gate 			dump_string_table(p, 1);
19530Sstevel@tonic-gate 			continue;
19540Sstevel@tonic-gate 		}
19550Sstevel@tonic-gate 		if (shdr.sh_type == SHT_RELA) {
19560Sstevel@tonic-gate 			dump_reloc_table(elf_file, p_ehdr, p, 1, filename);
19570Sstevel@tonic-gate 			continue;
19580Sstevel@tonic-gate 		}
19590Sstevel@tonic-gate 		if (shdr.sh_type == SHT_REL) {
19600Sstevel@tonic-gate 			dump_reloc_table(elf_file, p_ehdr, p, 1, filename);
19610Sstevel@tonic-gate 			continue;
19620Sstevel@tonic-gate 		}
19630Sstevel@tonic-gate 		if (shdr.sh_type == SHT_DYNAMIC) {
19640Sstevel@tonic-gate 			dump_dynamic(elf_file, p, 1, filename);
19650Sstevel@tonic-gate 			continue;
19660Sstevel@tonic-gate 		}
19670Sstevel@tonic-gate 
19680Sstevel@tonic-gate 		(void) printf("\n%s:\n", p->scn_name);
19690Sstevel@tonic-gate 		print_rawdata(p_sec, size);
19700Sstevel@tonic-gate 	}
19710Sstevel@tonic-gate 	(void) printf("\n");
19720Sstevel@tonic-gate }
19730Sstevel@tonic-gate 
19740Sstevel@tonic-gate /*
19750Sstevel@tonic-gate  * Print section contents. This function does not print the contents
19760Sstevel@tonic-gate  * of the sections but sets up the parameters and then calls
19770Sstevel@tonic-gate  * print_section to print the contents.  Calling another function to print
19780Sstevel@tonic-gate  * the contents allows both -d and -n to work correctly
19790Sstevel@tonic-gate  * simultaneously. Input is an ELF file descriptor, the ELF header,
19800Sstevel@tonic-gate  * the SCNTAB structure, the number of sections, and the filename.
19810Sstevel@tonic-gate  * Set the range of sections if d_flag, and set section name if
19820Sstevel@tonic-gate  * n_flag.
19830Sstevel@tonic-gate  */
19840Sstevel@tonic-gate static void
19850Sstevel@tonic-gate dump_section(Elf *elf_file,
19860Sstevel@tonic-gate 	GElf_Ehdr *p_ehdr, SCNTAB *s, int num_scns, char *filename)
19870Sstevel@tonic-gate {
19880Sstevel@tonic-gate 	SCNTAB *n_range, *d_range; /* for use with -n and -d modifiers */
19890Sstevel@tonic-gate 	int i;
19900Sstevel@tonic-gate 	int found_it = 0;  /* for use with -n section_name */
19910Sstevel@tonic-gate 
19920Sstevel@tonic-gate 	if (n_flag) {
19930Sstevel@tonic-gate 		n_range = s;
19940Sstevel@tonic-gate 
19950Sstevel@tonic-gate 		for (i = 0; i < num_scns; i++, n_range++) {
19960Sstevel@tonic-gate 			if ((strcmp(name, n_range->scn_name)) != 0)
19970Sstevel@tonic-gate 				continue;
19980Sstevel@tonic-gate 			else {
19990Sstevel@tonic-gate 				found_it = 1;
20000Sstevel@tonic-gate 				print_section(elf_file, p_ehdr,
20010Sstevel@tonic-gate 					n_range, 1, filename);
20020Sstevel@tonic-gate 			}
20030Sstevel@tonic-gate 		}
20040Sstevel@tonic-gate 
20050Sstevel@tonic-gate 		if (!found_it) {
20060Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s: %s not found\n",
20070Sstevel@tonic-gate 				prog_name, filename, name);
20080Sstevel@tonic-gate 		}
20090Sstevel@tonic-gate 	} /* end n_flag */
20100Sstevel@tonic-gate 
20110Sstevel@tonic-gate 	if (d_flag) {
20120Sstevel@tonic-gate 		d_range = s;
20130Sstevel@tonic-gate 		d_num = check_range(d_low, d_hi, num_scns, filename);
20140Sstevel@tonic-gate 		if (d_num < 0)
20150Sstevel@tonic-gate 			return;
20160Sstevel@tonic-gate 		d_range += d_low - 1;
20170Sstevel@tonic-gate 
20180Sstevel@tonic-gate 		print_section(elf_file, p_ehdr, d_range, d_num, filename);
20190Sstevel@tonic-gate 	}	/* end d_flag */
20200Sstevel@tonic-gate 
20210Sstevel@tonic-gate 	if (!n_flag && !d_flag)
20220Sstevel@tonic-gate 		print_section(elf_file, p_ehdr, s, num_scns, filename);
20230Sstevel@tonic-gate }
20240Sstevel@tonic-gate 
20250Sstevel@tonic-gate /*
20260Sstevel@tonic-gate  * Print the section header table. This function does not print the contents
20270Sstevel@tonic-gate  * of the section headers but sets up the parameters and then calls
20280Sstevel@tonic-gate  * print_shdr to print the contents.  Calling another function to print
20290Sstevel@tonic-gate  * the contents allows both -d and -n to work correctly
20300Sstevel@tonic-gate  * simultaneously.  Input is the SCNTAB structure,
20310Sstevel@tonic-gate  * the number of sections from the ELF header, and the filename.
20320Sstevel@tonic-gate  * Set the range of section headers to print if d_flag, and set
20330Sstevel@tonic-gate  * name of section header to print if n_flag.
20340Sstevel@tonic-gate  */
20350Sstevel@tonic-gate static void
20360Sstevel@tonic-gate dump_shdr(Elf *elf_file, SCNTAB *s, int num_scns, char *filename)
20370Sstevel@tonic-gate {
20380Sstevel@tonic-gate 
20390Sstevel@tonic-gate 	SCNTAB *n_range, *d_range;	/* for use with -n and -d modifiers */
20400Sstevel@tonic-gate 	int field;
20410Sstevel@tonic-gate 	int i;
20420Sstevel@tonic-gate 	int found_it = 0;  /* for use with -n section_name */
20430Sstevel@tonic-gate 
20440Sstevel@tonic-gate 	if (gelf_getclass(elf_file) == ELFCLASS64)
20451618Srie 		field = 21;
20460Sstevel@tonic-gate 	else
20471618Srie 		field = 13;
20480Sstevel@tonic-gate 
20490Sstevel@tonic-gate 	if (!p_flag) {
20500Sstevel@tonic-gate 		(void) printf("\n	   **** SECTION HEADER TABLE ****\n");
20510Sstevel@tonic-gate 		(void) printf("[No]\tType\tFlags\t%-*s %-*s %-*s%sName\n",
20520Sstevel@tonic-gate 		    field, "Addr", field, "Offset", field, "Size",
20530Sstevel@tonic-gate 		    /* compatibility:  tab for elf32 */
20541618Srie 		    (field == 13) ? "\t" : "  ");
20550Sstevel@tonic-gate 		(void) printf("\tLink\tInfo\t%-*s Entsize\n\n",
20560Sstevel@tonic-gate 		    field, "Adralgn");
20570Sstevel@tonic-gate 	}
20580Sstevel@tonic-gate 
20590Sstevel@tonic-gate 	if (n_flag) {
20600Sstevel@tonic-gate 		n_range = s;
20610Sstevel@tonic-gate 
20620Sstevel@tonic-gate 		for (i = 1; i <= num_scns; i++, n_range++) {
20630Sstevel@tonic-gate 			if ((strcmp(name, n_range->scn_name)) != 0)
20640Sstevel@tonic-gate 				continue;
20650Sstevel@tonic-gate 			else {
20660Sstevel@tonic-gate 				found_it = 1;
20670Sstevel@tonic-gate 				print_shdr(elf_file, n_range, 1, i);
20680Sstevel@tonic-gate 			}
20690Sstevel@tonic-gate 		}
20700Sstevel@tonic-gate 
20710Sstevel@tonic-gate 		if (!found_it) {
20720Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s: %s not found\n",
20730Sstevel@tonic-gate 				prog_name, filename, name);
20740Sstevel@tonic-gate 		}
20750Sstevel@tonic-gate 	} /* end n_flag */
20760Sstevel@tonic-gate 
20770Sstevel@tonic-gate 	if (d_flag) {
20780Sstevel@tonic-gate 		d_range = s;
20790Sstevel@tonic-gate 		d_num = check_range(d_low, d_hi, num_scns, filename);
20800Sstevel@tonic-gate 		if (d_num < 0)
20810Sstevel@tonic-gate 			return;
20820Sstevel@tonic-gate 		d_range += d_low - 1;
20830Sstevel@tonic-gate 
20840Sstevel@tonic-gate 		print_shdr(elf_file, d_range, d_num, d_low);
20850Sstevel@tonic-gate 	}	/* end d_flag */
20860Sstevel@tonic-gate 
20870Sstevel@tonic-gate 	if (!n_flag && !d_flag)
20880Sstevel@tonic-gate 		print_shdr(elf_file, s, num_scns, 1);
20890Sstevel@tonic-gate }
20900Sstevel@tonic-gate 
20910Sstevel@tonic-gate /*
20920Sstevel@tonic-gate  * Process all of the command line options (except
20930Sstevel@tonic-gate  * for -a, -g, -f, and -o).  All of the options processed
20940Sstevel@tonic-gate  * by this function require the presence of the section
20950Sstevel@tonic-gate  * header table and will not be processed if it is not present.
20960Sstevel@tonic-gate  * Set up a buffer containing section name, section header,
20970Sstevel@tonic-gate  * and section descriptor for each section in the file.  This
20980Sstevel@tonic-gate  * structure is used to avoid duplicate calls to libelf functions.
20990Sstevel@tonic-gate  * Structure members for the symbol table, the debugging information,
21000Sstevel@tonic-gate  * and the line number information are global.  All of the
21010Sstevel@tonic-gate  * rest are local.
21020Sstevel@tonic-gate  */
21030Sstevel@tonic-gate static void
21040Sstevel@tonic-gate dump_section_table(Elf *elf_file, GElf_Ehdr *elf_head_p, char *filename)
21050Sstevel@tonic-gate {
21060Sstevel@tonic-gate 
21070Sstevel@tonic-gate 	static SCNTAB	*buffer, *p_scns;
21080Sstevel@tonic-gate 	Elf_Scn		*scn = 0;
21090Sstevel@tonic-gate 	char		*s_name = NULL;
21100Sstevel@tonic-gate 	int		found = 0;
21110Sstevel@tonic-gate 	unsigned int	num_scns;
21120Sstevel@tonic-gate 	size_t		shstrndx;
21130Sstevel@tonic-gate 	size_t		shnum;
21140Sstevel@tonic-gate 
21150Sstevel@tonic-gate 
21160Sstevel@tonic-gate 	if (elf_getshnum(elf_file, &shnum) == 0) {
21170Sstevel@tonic-gate 		(void) fprintf(stderr,
21180Sstevel@tonic-gate 			"%s: %s: elf_getshnum failed: %s\n",
21190Sstevel@tonic-gate 			prog_name, filename, elf_errmsg(-1));
21200Sstevel@tonic-gate 		return;
21210Sstevel@tonic-gate 	}
21220Sstevel@tonic-gate 	if (elf_getshstrndx(elf_file, &shstrndx) == 0) {
21230Sstevel@tonic-gate 		(void) fprintf(stderr,
21240Sstevel@tonic-gate 			"%s: %s: elf_getshstrndx failed: %s\n",
21250Sstevel@tonic-gate 			prog_name, filename, elf_errmsg(-1));
21260Sstevel@tonic-gate 		return;
21270Sstevel@tonic-gate 	}
21280Sstevel@tonic-gate 
21290Sstevel@tonic-gate 	if ((buffer = calloc(shnum, sizeof (SCNTAB))) == NULL) {
21300Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: cannot calloc space\n",
21310Sstevel@tonic-gate 			prog_name, filename);
21320Sstevel@tonic-gate 		return;
21330Sstevel@tonic-gate 	}
21340Sstevel@tonic-gate 	/* LINTED */
21350Sstevel@tonic-gate 	num_scns = (int)shnum - 1;
21360Sstevel@tonic-gate 
21370Sstevel@tonic-gate 	p_symtab = (SCNTAB *)0;
21380Sstevel@tonic-gate 	p_dynsym = (SCNTAB *)0;
21390Sstevel@tonic-gate 	p_scns = buffer;
21400Sstevel@tonic-gate 	p_head_scns = buffer;
21410Sstevel@tonic-gate 
21420Sstevel@tonic-gate 	while ((scn = elf_nextscn(elf_file, scn)) != 0) {
21430Sstevel@tonic-gate 		if ((gelf_getshdr(scn, &buffer->p_shdr)) == 0) {
21440Sstevel@tonic-gate 			(void) fprintf(stderr,
21450Sstevel@tonic-gate 			"%s: %s: %s\n", prog_name, filename, elf_errmsg(-1));
21460Sstevel@tonic-gate 			return;
21470Sstevel@tonic-gate 		}
21480Sstevel@tonic-gate 		s_name = (char *)elf_strptr(elf_file,
21490Sstevel@tonic-gate 			shstrndx, buffer->p_shdr.sh_name);
21500Sstevel@tonic-gate 		buffer->scn_name = s_name ? s_name : (char *)UNKNOWN;
21510Sstevel@tonic-gate 		buffer->p_sd   =  scn;
21520Sstevel@tonic-gate 
21530Sstevel@tonic-gate 		if (buffer->p_shdr.sh_type == SHT_SYMTAB) {
21540Sstevel@tonic-gate 			found += 1;
21550Sstevel@tonic-gate 			p_symtab = buffer;
21560Sstevel@tonic-gate 		}
21570Sstevel@tonic-gate 		if (buffer->p_shdr.sh_type == SHT_DYNSYM)
21580Sstevel@tonic-gate 			p_dynsym = buffer;
21590Sstevel@tonic-gate 		buffer++;
21600Sstevel@tonic-gate 	}
21610Sstevel@tonic-gate 
21620Sstevel@tonic-gate 	/*
21630Sstevel@tonic-gate 	 * These functions depend upon the presence of the section header table
21640Sstevel@tonic-gate 	 * and will not be invoked in its absence
21650Sstevel@tonic-gate 	 */
21660Sstevel@tonic-gate 	if (h_flag) {
21670Sstevel@tonic-gate 		dump_shdr(elf_file, p_scns, num_scns, filename);
21680Sstevel@tonic-gate 	}
21690Sstevel@tonic-gate 	if (p_symtab && (t_flag || T_flag)) {
21700Sstevel@tonic-gate 		dump_symbol_table(elf_file, p_symtab, filename);
21710Sstevel@tonic-gate 	}
21720Sstevel@tonic-gate 	if (c_flag) {
21730Sstevel@tonic-gate 		dump_string_table(p_scns, num_scns);
21740Sstevel@tonic-gate 	}
21750Sstevel@tonic-gate 	if (r_flag) {
21760Sstevel@tonic-gate 		dump_reloc_table(elf_file, elf_head_p,
21770Sstevel@tonic-gate 			p_scns, num_scns, filename);
21780Sstevel@tonic-gate 	}
21790Sstevel@tonic-gate 	if (L_flag) {
21800Sstevel@tonic-gate 		dump_dynamic(elf_file, p_scns, num_scns, filename);
21810Sstevel@tonic-gate 	}
21820Sstevel@tonic-gate 	if (s_flag) {
21830Sstevel@tonic-gate 		dump_section(elf_file, elf_head_p, p_scns,
21840Sstevel@tonic-gate 			num_scns, filename);
21850Sstevel@tonic-gate 	}
21860Sstevel@tonic-gate }
21870Sstevel@tonic-gate 
21880Sstevel@tonic-gate /*
21890Sstevel@tonic-gate  * Load the archive string table(s) (for extended-length strings)
21900Sstevel@tonic-gate  * into an in-core table/list
21910Sstevel@tonic-gate  */
21920Sstevel@tonic-gate static struct stab_list_s *
21930Sstevel@tonic-gate load_arstring_table(struct stab_list_s *STabList,
21940Sstevel@tonic-gate 	int fd, Elf *elf_file, Elf_Arhdr *p_ar, char *filename)
21950Sstevel@tonic-gate {
21960Sstevel@tonic-gate 	off_t here;
21970Sstevel@tonic-gate 	struct stab_list_s *STL_entry, *STL_next;
21980Sstevel@tonic-gate 
21990Sstevel@tonic-gate 	if (p_ar) {
22000Sstevel@tonic-gate 		STL_entry = malloc(sizeof (struct stab_list_s));
22010Sstevel@tonic-gate 		STL_entry->next    = 0;
22020Sstevel@tonic-gate 		STL_entry->strings = 0;
22030Sstevel@tonic-gate 		STL_entry->size    = 0;
22040Sstevel@tonic-gate 
22050Sstevel@tonic-gate 		if (!STabList)
22060Sstevel@tonic-gate 			STabList = STL_entry;
22070Sstevel@tonic-gate 		else {
22080Sstevel@tonic-gate 			STL_next = STabList;
22090Sstevel@tonic-gate 			while (STL_next->next != (void *)0)
22100Sstevel@tonic-gate 				STL_next = STL_next->next;
22110Sstevel@tonic-gate 			STL_next->next = STL_entry;
22120Sstevel@tonic-gate 		}
22130Sstevel@tonic-gate 
22140Sstevel@tonic-gate 		STL_entry->size    = p_ar->ar_size;
22150Sstevel@tonic-gate 		STL_entry->strings = malloc(p_ar->ar_size);
22160Sstevel@tonic-gate 		here = elf_getbase(elf_file);
22170Sstevel@tonic-gate 		if ((lseek(fd, here, 0)) != here) {
22180Sstevel@tonic-gate 			(void) fprintf(stderr,
22190Sstevel@tonic-gate 			"%s: %s: could not lseek\n", prog_name, filename);
22200Sstevel@tonic-gate 		}
22210Sstevel@tonic-gate 
22220Sstevel@tonic-gate 		if ((read(fd, STL_entry->strings, p_ar->ar_size)) == -1) {
22230Sstevel@tonic-gate 			(void) fprintf(stderr,
22240Sstevel@tonic-gate 			"%s: %s: could not read\n", prog_name, filename);
22250Sstevel@tonic-gate 		}
22260Sstevel@tonic-gate 	}
22270Sstevel@tonic-gate 	return (STabList);
22280Sstevel@tonic-gate }
22290Sstevel@tonic-gate 
22300Sstevel@tonic-gate /*
22310Sstevel@tonic-gate  * Print the archive header for each member of an archive.
22320Sstevel@tonic-gate  * Also call ar_sym_read to print the symbols in the
22330Sstevel@tonic-gate  * archive symbol table if g_flag.  Input is a file descriptor,
22340Sstevel@tonic-gate  * an ELF file descriptor, and the filename.  Putting the call
22350Sstevel@tonic-gate  * to dump the archive symbol table in this function is more
22360Sstevel@tonic-gate  * efficient since it is necessary to examine the archive member
22370Sstevel@tonic-gate  * name in the archive header to determine which member is the
22380Sstevel@tonic-gate  * symbol table.
22390Sstevel@tonic-gate  */
22400Sstevel@tonic-gate static void
22410Sstevel@tonic-gate dump_ar_hdr(int fd, Elf *elf_file, char *filename)
22420Sstevel@tonic-gate {
22430Sstevel@tonic-gate 	extern int v_flag, g_flag, a_flag, p_flag;
22440Sstevel@tonic-gate 	Elf_Arhdr  *p_ar;
22450Sstevel@tonic-gate 	Elf *arf;
22460Sstevel@tonic-gate 	Elf_Cmd cmd;
22470Sstevel@tonic-gate 	int title = 0;
22480Sstevel@tonic-gate 	int err = 0;
22490Sstevel@tonic-gate 
22500Sstevel@tonic-gate 	char buf[DATESIZE];
22510Sstevel@tonic-gate 
22520Sstevel@tonic-gate 	cmd = ELF_C_READ;
22530Sstevel@tonic-gate 	while ((arf = elf_begin(fd, cmd, elf_file)) != 0) {
22540Sstevel@tonic-gate 		p_ar = elf_getarhdr(arf);
22550Sstevel@tonic-gate 		if (p_ar == NULL) {
22560Sstevel@tonic-gate 			(void) fprintf(stderr,
22570Sstevel@tonic-gate 			"%s: %s: %s\n", prog_name, filename, elf_errmsg(-1));
22580Sstevel@tonic-gate 			continue;
22590Sstevel@tonic-gate 		}
22600Sstevel@tonic-gate 		if (strcmp(p_ar->ar_name, "/") == 0) {
22610Sstevel@tonic-gate 			if (g_flag)
22620Sstevel@tonic-gate 				ar_sym_read(elf_file, filename);
22630Sstevel@tonic-gate 		} else if (strcmp(p_ar->ar_name, "//") == 0) {
22640Sstevel@tonic-gate 			StringTableList = load_arstring_table(
22650Sstevel@tonic-gate 				StringTableList, fd, arf, p_ar,
22660Sstevel@tonic-gate 				filename);
22670Sstevel@tonic-gate 			cmd = elf_next(arf);
22680Sstevel@tonic-gate 			(void) elf_end(arf);
22690Sstevel@tonic-gate 			continue;
22700Sstevel@tonic-gate 		} else {
22710Sstevel@tonic-gate 			if (a_flag) {
22720Sstevel@tonic-gate 				(void) printf("%s[%s]:\n", filename,
22730Sstevel@tonic-gate 					p_ar->ar_name);
22740Sstevel@tonic-gate 				if (!p_flag && title == 0) {
22750Sstevel@tonic-gate 					if (!v_flag)
22760Sstevel@tonic-gate 						(void) printf(
22770Sstevel@tonic-gate "\n\n\t\t\t***ARCHIVE HEADER***"
22780Sstevel@tonic-gate "\n	Date          Uid     Gid    Mode      Size	 Member Name\n\n");
22790Sstevel@tonic-gate 					else
22800Sstevel@tonic-gate 						(void) printf(
22810Sstevel@tonic-gate "\n\n\t\t\t***ARCHIVE HEADER***"
22820Sstevel@tonic-gate "\n	Date                   Uid    Gid   Mode     Size     Member Name\n\n");
22830Sstevel@tonic-gate 					title = 1;
22840Sstevel@tonic-gate 				}
22850Sstevel@tonic-gate 				if (!v_flag) {
22860Sstevel@tonic-gate 					(void) printf(
22870Sstevel@tonic-gate "\t0x%.8lx  %6d  %6d  0%.6ho  0x%.8lx  %-s\n\n",
22880Sstevel@tonic-gate 						p_ar->ar_date,
22890Sstevel@tonic-gate 						(int)p_ar->ar_uid,
22900Sstevel@tonic-gate 						(int)p_ar->ar_gid,
22910Sstevel@tonic-gate 						(int)p_ar->ar_mode,
22920Sstevel@tonic-gate 						p_ar->ar_size,
22930Sstevel@tonic-gate 						p_ar->ar_name);
22940Sstevel@tonic-gate 				} else {
22950Sstevel@tonic-gate 					if ((strftime(buf, DATESIZE,
22960Sstevel@tonic-gate 					    "%b %d %H:%M:%S %Y",
22970Sstevel@tonic-gate 					    localtime(
22980Sstevel@tonic-gate 					    &(p_ar->ar_date)))) == 0) {
22990Sstevel@tonic-gate 						(void) fprintf(stderr,
23000Sstevel@tonic-gate "%s: %s: don't have enough space to store the date\n", prog_name, filename);
23010Sstevel@tonic-gate 						exit(1);
23020Sstevel@tonic-gate 					}
23030Sstevel@tonic-gate 					(void) printf(
23040Sstevel@tonic-gate 					"\t%s %6d %6d 0%.6ho 0x%.8lx %-s\n\n",
23050Sstevel@tonic-gate 						buf,
23060Sstevel@tonic-gate 						(int)p_ar->ar_uid,
23070Sstevel@tonic-gate 						(int)p_ar->ar_gid,
23080Sstevel@tonic-gate 						(int)p_ar->ar_mode,
23090Sstevel@tonic-gate 						p_ar->ar_size,
23100Sstevel@tonic-gate 						p_ar->ar_name);
23110Sstevel@tonic-gate 				}
23120Sstevel@tonic-gate 			}
23130Sstevel@tonic-gate 		}
23140Sstevel@tonic-gate 		cmd = elf_next(arf);
23150Sstevel@tonic-gate 		(void) elf_end(arf);
23160Sstevel@tonic-gate 	} /* end while */
23170Sstevel@tonic-gate 
23180Sstevel@tonic-gate 	err = elf_errno();
23190Sstevel@tonic-gate 	if (err != 0) {
23200Sstevel@tonic-gate 		(void) fprintf(stderr,
23210Sstevel@tonic-gate 		"%s: %s: %s\n", prog_name, filename, elf_errmsg(err));
23220Sstevel@tonic-gate 	}
23230Sstevel@tonic-gate }
23240Sstevel@tonic-gate 
23250Sstevel@tonic-gate /*
23260Sstevel@tonic-gate  * Process member files of an archive.  This function provides
23270Sstevel@tonic-gate  * a loop through an archive equivalent the processing of
23280Sstevel@tonic-gate  * each_file for individual object files.
23290Sstevel@tonic-gate  */
23300Sstevel@tonic-gate static void
23310Sstevel@tonic-gate dump_ar_files(int fd, Elf *elf_file, char *filename)
23320Sstevel@tonic-gate {
23330Sstevel@tonic-gate 	Elf_Arhdr  *p_ar;
23340Sstevel@tonic-gate 	Elf *arf;
23350Sstevel@tonic-gate 	Elf_Cmd cmd;
23360Sstevel@tonic-gate 	Elf_Kind file_type;
23370Sstevel@tonic-gate 	GElf_Ehdr elf_head;
23380Sstevel@tonic-gate 	char *fullname;
23390Sstevel@tonic-gate 
23400Sstevel@tonic-gate 	cmd = ELF_C_READ;
23410Sstevel@tonic-gate 	while ((arf = elf_begin(fd, cmd, elf_file)) != 0) {
23421618Srie 		size_t	len;
23431618Srie 
23440Sstevel@tonic-gate 		p_ar = elf_getarhdr(arf);
23450Sstevel@tonic-gate 		if (p_ar == NULL) {
23460Sstevel@tonic-gate 			(void) fprintf(stderr,
23470Sstevel@tonic-gate 				"%s: %s: %s\n",
23480Sstevel@tonic-gate 				prog_name, filename, elf_errmsg(-1));
23490Sstevel@tonic-gate 			return;
23500Sstevel@tonic-gate 		}
23510Sstevel@tonic-gate 		if ((strcmp(p_ar->ar_name, "/") == 0) ||
23520Sstevel@tonic-gate 			(strcmp(p_ar->ar_name, "//") == 0)) {
23530Sstevel@tonic-gate 			cmd = elf_next(arf);
23540Sstevel@tonic-gate 			(void) elf_end(arf);
23550Sstevel@tonic-gate 			continue;
23560Sstevel@tonic-gate 		}
23570Sstevel@tonic-gate 
23581618Srie 		len = strlen(filename) + strlen(p_ar->ar_name) + 3;
23591618Srie 		if ((fullname = malloc(len)) == NULL)
23601618Srie 			return;
23611618Srie 		(void) snprintf(fullname, len, "%s[%s]", filename,
23621618Srie 		    p_ar->ar_name);
23630Sstevel@tonic-gate 		(void) printf("\n%s:\n", fullname);
23640Sstevel@tonic-gate 		file_type = elf_kind(arf);
23650Sstevel@tonic-gate 		if (file_type == ELF_K_ELF) {
23660Sstevel@tonic-gate 			if (dump_elf_header(arf, fullname, &elf_head) == NULL)
23670Sstevel@tonic-gate 				return;
23680Sstevel@tonic-gate 			if (o_flag)
23690Sstevel@tonic-gate 				dump_exec_header(arf,
23700Sstevel@tonic-gate 					(unsigned)elf_head.e_phnum, fullname);
23710Sstevel@tonic-gate 			if (x_flag)
23720Sstevel@tonic-gate 				dump_section_table(arf, &elf_head, fullname);
23730Sstevel@tonic-gate 		} else {
23740Sstevel@tonic-gate 			(void) fprintf(stderr,
23750Sstevel@tonic-gate 				"%s: %s: invalid file type\n",
23760Sstevel@tonic-gate 				prog_name, fullname);
23770Sstevel@tonic-gate 			cmd = elf_next(arf);
23780Sstevel@tonic-gate 			(void) elf_end(arf);
23790Sstevel@tonic-gate 			continue;
23800Sstevel@tonic-gate 		}
23810Sstevel@tonic-gate 
23820Sstevel@tonic-gate 		cmd = elf_next(arf);
23830Sstevel@tonic-gate 		(void) elf_end(arf);
23840Sstevel@tonic-gate 	} /* end while */
23850Sstevel@tonic-gate }
23860Sstevel@tonic-gate 
23870Sstevel@tonic-gate /*
23880Sstevel@tonic-gate  * Takes a filename as input.  Test first for a valid version
23890Sstevel@tonic-gate  * of libelf.a and exit on error.  Process each valid file
23900Sstevel@tonic-gate  * or archive given as input on the command line.  Check
23910Sstevel@tonic-gate  * for file type.  If it is an archive, process the archive-
23920Sstevel@tonic-gate  * specific options first, then files within the archive.
23930Sstevel@tonic-gate  * If it is an ELF object file, process it; otherwise
23940Sstevel@tonic-gate  * warn that it is an invalid file type.
23950Sstevel@tonic-gate  * All options except the archive-specific and program
23960Sstevel@tonic-gate  * execution header are processed in the function, dump_section_table.
23970Sstevel@tonic-gate  */
23980Sstevel@tonic-gate static void
23990Sstevel@tonic-gate each_file(char *filename)
24000Sstevel@tonic-gate {
24010Sstevel@tonic-gate 	Elf *elf_file;
24020Sstevel@tonic-gate 	GElf_Ehdr elf_head;
24030Sstevel@tonic-gate 	int fd;
24040Sstevel@tonic-gate 	Elf_Kind   file_type;
24050Sstevel@tonic-gate 
24060Sstevel@tonic-gate 	struct stat buf;
24070Sstevel@tonic-gate 
24080Sstevel@tonic-gate 	Elf_Cmd cmd;
24090Sstevel@tonic-gate 	errno = 0;
24100Sstevel@tonic-gate 
24110Sstevel@tonic-gate 	if (stat(filename, &buf) == -1) {
24121618Srie 		int	err = errno;
24131618Srie 		(void) fprintf(stderr, "%s: %s: %s", prog_name, filename,
24141618Srie 		    strerror(err));
24150Sstevel@tonic-gate 		return;
24160Sstevel@tonic-gate 	}
24170Sstevel@tonic-gate 
24180Sstevel@tonic-gate 	if ((fd = open((filename), O_RDONLY)) == -1) {
24191618Srie 		(void) fprintf(stderr, "%s: %s: cannot read\n", prog_name,
24201618Srie 		    filename);
24210Sstevel@tonic-gate 		return;
24220Sstevel@tonic-gate 	}
24230Sstevel@tonic-gate 	cmd = ELF_C_READ;
24240Sstevel@tonic-gate 	if ((elf_file = elf_begin(fd, cmd, (Elf *)0)) == NULL) {
24251618Srie 		(void) fprintf(stderr, "%s: %s: %s\n", prog_name, filename,
24261618Srie 		    elf_errmsg(-1));
24270Sstevel@tonic-gate 		return;
24280Sstevel@tonic-gate 	}
24290Sstevel@tonic-gate 
24300Sstevel@tonic-gate 	file_type = elf_kind(elf_file);
24310Sstevel@tonic-gate 	if (file_type == ELF_K_AR) {
24320Sstevel@tonic-gate 		if (a_flag || g_flag) {
24330Sstevel@tonic-gate 			dump_ar_hdr(fd, elf_file, filename);
24340Sstevel@tonic-gate 			elf_file = elf_begin(fd, cmd, (Elf *)0);
24350Sstevel@tonic-gate 		}
24360Sstevel@tonic-gate 		if (z_flag)
24370Sstevel@tonic-gate 			dump_ar_files(fd, elf_file, filename);
24380Sstevel@tonic-gate 	} else {
24390Sstevel@tonic-gate 		if (file_type == ELF_K_ELF) {
24400Sstevel@tonic-gate 			(void) printf("\n%s:\n", filename);
24411618Srie 			if (dump_elf_header(elf_file, filename, &elf_head)) {
24421618Srie 				if (o_flag)
24431618Srie 					dump_exec_header(elf_file,
24441618Srie 					    (unsigned)elf_head.e_phnum,
24451618Srie 					    filename);
24461618Srie 				if (x_flag)
24471618Srie 					dump_section_table(elf_file,
24481618Srie 					    &elf_head, filename);
24490Sstevel@tonic-gate 			}
24500Sstevel@tonic-gate 		} else {
24510Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s: invalid file type\n",
24521618Srie 			    prog_name, filename);
24530Sstevel@tonic-gate 		}
24540Sstevel@tonic-gate 	}
24550Sstevel@tonic-gate 	(void) elf_end(elf_file);
24560Sstevel@tonic-gate 	(void) close(fd);
24570Sstevel@tonic-gate }
24580Sstevel@tonic-gate 
24590Sstevel@tonic-gate /*
24600Sstevel@tonic-gate  * Sets up flags for command line options given and then
24610Sstevel@tonic-gate  * calls each_file() to process each file.
24620Sstevel@tonic-gate  */
24630Sstevel@tonic-gate int
24640Sstevel@tonic-gate main(int argc, char *argv[], char *envp[])
24650Sstevel@tonic-gate {
24660Sstevel@tonic-gate 	char *optstr = OPTSTR; /* option string used by getopt() */
24670Sstevel@tonic-gate 	int optchar;
24680Sstevel@tonic-gate 
24690Sstevel@tonic-gate 	/*
24700Sstevel@tonic-gate 	 * Check for a binary that better fits this architecture.
24710Sstevel@tonic-gate 	 */
24720Sstevel@tonic-gate 	conv_check_native(argv, envp);
24730Sstevel@tonic-gate 
24740Sstevel@tonic-gate 	prog_name = argv[0];
24750Sstevel@tonic-gate 
24760Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
24770Sstevel@tonic-gate 	while ((optchar = getopt(argc, argv, optstr)) != -1) {
24780Sstevel@tonic-gate 		switch (optchar) {
24790Sstevel@tonic-gate 		case 'a':
24800Sstevel@tonic-gate 			a_flag = 1;
24810Sstevel@tonic-gate 			x_flag = 1;
24820Sstevel@tonic-gate 			break;
24830Sstevel@tonic-gate 		case 'g':
24840Sstevel@tonic-gate 			g_flag = 1;
24850Sstevel@tonic-gate 			x_flag = 1;
24860Sstevel@tonic-gate 			break;
24870Sstevel@tonic-gate 		case 'v':
24880Sstevel@tonic-gate 			v_flag = 1;
24890Sstevel@tonic-gate 			break;
24900Sstevel@tonic-gate 		case 'p':
24910Sstevel@tonic-gate 			p_flag = 1;
24920Sstevel@tonic-gate 			break;
24930Sstevel@tonic-gate 		case 'f':
24940Sstevel@tonic-gate 			f_flag = 1;
24950Sstevel@tonic-gate 			z_flag = 1;
24960Sstevel@tonic-gate 			break;
24970Sstevel@tonic-gate 		case 'o':
24980Sstevel@tonic-gate 			o_flag = 1;
24990Sstevel@tonic-gate 			z_flag = 1;
25000Sstevel@tonic-gate 			break;
25010Sstevel@tonic-gate 		case 'h':
25020Sstevel@tonic-gate 			h_flag = 1;
25030Sstevel@tonic-gate 			x_flag = 1;
25040Sstevel@tonic-gate 			z_flag = 1;
25050Sstevel@tonic-gate 			break;
25060Sstevel@tonic-gate 		case 's':
25070Sstevel@tonic-gate 			s_flag = 1;
25080Sstevel@tonic-gate 			x_flag = 1;
25090Sstevel@tonic-gate 			z_flag = 1;
25100Sstevel@tonic-gate 			break;
25110Sstevel@tonic-gate 		case 'd':
25120Sstevel@tonic-gate 			d_flag = 1;
25130Sstevel@tonic-gate 			x_flag = 1;
25140Sstevel@tonic-gate 			z_flag = 1;
25150Sstevel@tonic-gate 			set_range(optarg, &d_low, &d_hi);
25160Sstevel@tonic-gate 			break;
25170Sstevel@tonic-gate 		case 'n':
25180Sstevel@tonic-gate 			n_flag++;
25190Sstevel@tonic-gate 			x_flag = 1;
25200Sstevel@tonic-gate 			z_flag = 1;
25210Sstevel@tonic-gate 			name = optarg;
25220Sstevel@tonic-gate 			break;
25230Sstevel@tonic-gate 		case 'r':
25240Sstevel@tonic-gate 			r_flag = 1;
25250Sstevel@tonic-gate 			x_flag = 1;
25260Sstevel@tonic-gate 			z_flag = 1;
25270Sstevel@tonic-gate 			break;
25280Sstevel@tonic-gate 		case 't':
25290Sstevel@tonic-gate 			t_flag = 1;
25300Sstevel@tonic-gate 			x_flag = 1;
25310Sstevel@tonic-gate 			z_flag = 1;
25320Sstevel@tonic-gate 			break;
25330Sstevel@tonic-gate 		case 'C':
25340Sstevel@tonic-gate 			C_flag = 1;
25350Sstevel@tonic-gate 			t_flag = 1;
25360Sstevel@tonic-gate 			x_flag = 1;
25370Sstevel@tonic-gate 			z_flag = 1;
25380Sstevel@tonic-gate 			break;
25390Sstevel@tonic-gate 		case 'T':
25400Sstevel@tonic-gate 			T_flag = 1;
25410Sstevel@tonic-gate 			x_flag = 1;
25420Sstevel@tonic-gate 			z_flag = 1;
25430Sstevel@tonic-gate 			set_range(optarg, &T_low, &T_hi);
25440Sstevel@tonic-gate 			break;
25450Sstevel@tonic-gate 		case 'c':
25460Sstevel@tonic-gate 			c_flag = 1;
25470Sstevel@tonic-gate 			x_flag = 1;
25480Sstevel@tonic-gate 			z_flag = 1;
25490Sstevel@tonic-gate 			break;
25500Sstevel@tonic-gate 		case 'L':
25510Sstevel@tonic-gate 			L_flag = 1;
25520Sstevel@tonic-gate 			x_flag = 1;
25530Sstevel@tonic-gate 			z_flag = 1;
25540Sstevel@tonic-gate 			break;
25550Sstevel@tonic-gate 		case 'V':
25560Sstevel@tonic-gate 			V_flag = 1;
25570Sstevel@tonic-gate 			(void) fprintf(stderr, "dump: %s %s\n",
25580Sstevel@tonic-gate 			    (const char *)SGU_PKG,
25590Sstevel@tonic-gate 			    (const char *)SGU_REL);
25600Sstevel@tonic-gate 			break;
25610Sstevel@tonic-gate 		case '?':
25620Sstevel@tonic-gate 			errflag += 1;
25630Sstevel@tonic-gate 			break;
25640Sstevel@tonic-gate 		default:
25650Sstevel@tonic-gate 			break;
25660Sstevel@tonic-gate 		}
25670Sstevel@tonic-gate 	}
25680Sstevel@tonic-gate 
25690Sstevel@tonic-gate 	if (errflag || (optind >= argc) || (!z_flag && !x_flag)) {
25700Sstevel@tonic-gate 		if (!(V_flag && (argc == 2))) {
25710Sstevel@tonic-gate 			usage();
25720Sstevel@tonic-gate 			exit(269);
25730Sstevel@tonic-gate 		}
25740Sstevel@tonic-gate 	}
25750Sstevel@tonic-gate 
25761618Srie 	if (elf_version(EV_CURRENT) == EV_NONE) {
25771618Srie 		(void) fprintf(stderr, "%s: libelf is out of date\n",
25781618Srie 		    prog_name);
25791618Srie 		exit(101);
25801618Srie 	}
25811618Srie 
25820Sstevel@tonic-gate 	while (optind < argc) {
25830Sstevel@tonic-gate 		each_file(argv[optind]);
25840Sstevel@tonic-gate 		optind++;
25850Sstevel@tonic-gate 	}
25860Sstevel@tonic-gate 	return (0);
25870Sstevel@tonic-gate }
2588