xref: /titanic_51/usr/src/cmd/sgs/dump/common/dump.c (revision ba7866cd2cbdf574f47d4e38a1301b90744dd677)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55aefb655Srie  * Common Development and Distribution License (the "License").
65aefb655Srie  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
215aefb655Srie 
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
247c478bd9Sstevel@tonic-gate  *	  All Rights Reserved
257c478bd9Sstevel@tonic-gate  *
26*ba7866cdSAli Bahrami  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
29c13de8f6Sab196087 /* Get definitions for the relocation types supported. */
30c13de8f6Sab196087 #define	ELF_TARGET_ALL
31c13de8f6Sab196087 
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <locale.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367c478bd9Sstevel@tonic-gate #include <libelf.h>
37ba2be530Sab196087 #include <sys/link.h>
38c13de8f6Sab196087 #include <sys/elf.h>
397c478bd9Sstevel@tonic-gate #include <sys/machelf.h>
407c478bd9Sstevel@tonic-gate #include <fcntl.h>
417c478bd9Sstevel@tonic-gate #include <sys/stat.h>
427c478bd9Sstevel@tonic-gate #include <errno.h>
437c478bd9Sstevel@tonic-gate #include <string.h>
447c478bd9Sstevel@tonic-gate #include "sgs.h"
457c478bd9Sstevel@tonic-gate #include "conv.h"
467c478bd9Sstevel@tonic-gate #include "dump.h"
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #define	OPTSTR	"agcd:fhn:oprstvCLT:V?"		/* option string for getopt() */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate const char *UNKNOWN = "<unknown>";
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate static SCNTAB *p_symtab, *p_head_scns, *p_dynsym;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static int
567c478bd9Sstevel@tonic-gate 	x_flag = 0,	/* option requires section header table */
577c478bd9Sstevel@tonic-gate 	z_flag = 0,	/* process files within an archive */
587c478bd9Sstevel@tonic-gate 	rn_flag = 0;	/* dump named relocation information */
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate static int
617c478bd9Sstevel@tonic-gate 	/* flags: ?_flag corresponds to ? option */
627c478bd9Sstevel@tonic-gate 	a_flag = 0,	/* dump archive header of each member of archive */
637c478bd9Sstevel@tonic-gate 	g_flag = 0,	/* dump archive symbol table */
647c478bd9Sstevel@tonic-gate 	c_flag = 0,	/* dump the string table */
657c478bd9Sstevel@tonic-gate 	d_flag = 0,	/* dump range of sections */
667c478bd9Sstevel@tonic-gate 	f_flag = 0,	/* dump each file header */
677c478bd9Sstevel@tonic-gate 	h_flag = 0,	/* dump section headers */
687c478bd9Sstevel@tonic-gate 	n_flag = 0,	/* dump named section */
697c478bd9Sstevel@tonic-gate 	o_flag = 0,	/* dump each program execution header */
707c478bd9Sstevel@tonic-gate 	r_flag = 0,	/* dump relocation information */
717c478bd9Sstevel@tonic-gate 	s_flag = 0,	/* dump section contents */
727c478bd9Sstevel@tonic-gate 	t_flag = 0,	/* dump symbol table entries */
737c478bd9Sstevel@tonic-gate 	C_flag = 0,	/* dump decoded C++ symbol names */
747c478bd9Sstevel@tonic-gate 	L_flag = 0,	/* dump dynamic linking information */
757c478bd9Sstevel@tonic-gate 	T_flag = 0,	/* dump symbol table range */
767c478bd9Sstevel@tonic-gate 	V_flag = 0;	/* dump version information */
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate int	p_flag = 0,	/* suppress printing of headings */
797c478bd9Sstevel@tonic-gate 	v_flag = 0;	/* print information in verbose form */
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate static int
827c478bd9Sstevel@tonic-gate 	d_low = 0,	/* range for use with -d */
837c478bd9Sstevel@tonic-gate 	d_hi = 0,
847c478bd9Sstevel@tonic-gate 	d_num = 0;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate static int
877c478bd9Sstevel@tonic-gate 	T_low = 0,	/* range for use with -T */
887c478bd9Sstevel@tonic-gate 	T_hi = 0,
897c478bd9Sstevel@tonic-gate 	T_num = 0;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate static char *name = NULL; /* for use with -n option */
927c478bd9Sstevel@tonic-gate char *prog_name;
937c478bd9Sstevel@tonic-gate static int errflag = 0;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate static struct stab_list_s {
967c478bd9Sstevel@tonic-gate 	struct stab_list_s *next;
977c478bd9Sstevel@tonic-gate 	char *strings;
987c478bd9Sstevel@tonic-gate 	size_t size;
997c478bd9Sstevel@tonic-gate } *StringTableList = (void *)0;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate extern void ar_sym_read();
1027c478bd9Sstevel@tonic-gate extern void dump_exec_header();
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * Get the section descriptor and set the size of the
1077c478bd9Sstevel@tonic-gate  * data returned.  Data is byte-order converted.
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate void *
1107c478bd9Sstevel@tonic-gate get_scndata(Elf_Scn *fd_scn, size_t *size)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate 	Elf_Data *p_data;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	p_data = 0;
1157c478bd9Sstevel@tonic-gate 	if ((p_data = elf_getdata(fd_scn, p_data)) == 0 ||
1167c478bd9Sstevel@tonic-gate 	    p_data->d_size == 0) {
1177c478bd9Sstevel@tonic-gate 		return (NULL);
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 	*size = p_data->d_size;
1207c478bd9Sstevel@tonic-gate 	return (p_data->d_buf);
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate  * Get the section descriptor and set the size of the
1257c478bd9Sstevel@tonic-gate  * data returned.  Data is raw (i.e., not byte-order converted).
1267c478bd9Sstevel@tonic-gate  */
1277c478bd9Sstevel@tonic-gate static void *
1287c478bd9Sstevel@tonic-gate get_rawscn(Elf_Scn *fd_scn, size_t *size)
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate 	Elf_Data *p_data;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	p_data = 0;
1337c478bd9Sstevel@tonic-gate 	if ((p_data = elf_rawdata(fd_scn, p_data)) == 0 ||
1347c478bd9Sstevel@tonic-gate 	    p_data->d_size == 0) {
1357c478bd9Sstevel@tonic-gate 		return (NULL);
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	*size = p_data->d_size;
1397c478bd9Sstevel@tonic-gate 	return (p_data->d_buf);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate  * Print out a usage message in short form when program is invoked
1447c478bd9Sstevel@tonic-gate  * with insufficient or no arguments, and in long form when given
1457c478bd9Sstevel@tonic-gate  * either a ? or an invalid option.
1467c478bd9Sstevel@tonic-gate  */
1477c478bd9Sstevel@tonic-gate static void
1487c478bd9Sstevel@tonic-gate usage()
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
1517c478bd9Sstevel@tonic-gate 	"Usage: %s [-%s] file(s) ...\n", prog_name, OPTSTR);
1527c478bd9Sstevel@tonic-gate 	if (errflag) {
1537c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1547c478bd9Sstevel@tonic-gate 		"\t\t[-a dump archive header of each member of archive]\n\
1557c478bd9Sstevel@tonic-gate 		[-g dump archive global symbol table]\n\
1567c478bd9Sstevel@tonic-gate 		[-c dump the string table]\n\
1577c478bd9Sstevel@tonic-gate 		[-d dump range of sections]\n\
1587c478bd9Sstevel@tonic-gate 		[-f dump each file header]\n\
1597c478bd9Sstevel@tonic-gate 		[-h dump section headers]\n\
1607c478bd9Sstevel@tonic-gate 		[-n dump named section]\n\
1617c478bd9Sstevel@tonic-gate 		[-o dump each program execution header]\n\
1627c478bd9Sstevel@tonic-gate 		[-p suppress printing of headings]\n\
1637c478bd9Sstevel@tonic-gate 		[-r dump relocation information]\n\
1647c478bd9Sstevel@tonic-gate 		[-s dump section contents]\n\
1657c478bd9Sstevel@tonic-gate 		[-t dump symbol table entries]\n\
1667c478bd9Sstevel@tonic-gate 		[-v print information in verbose form]\n\
1677c478bd9Sstevel@tonic-gate 		[-C dump decoded C++ symbol names]\n\
1687c478bd9Sstevel@tonic-gate 		[-L dump the .dynamic structure]\n\
1697c478bd9Sstevel@tonic-gate 		[-T dump symbol table range]\n\
1707c478bd9Sstevel@tonic-gate 		[-V dump version information]\n");
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate /*
1757c478bd9Sstevel@tonic-gate  * Set a range.  Input is a character string, a lower
1767c478bd9Sstevel@tonic-gate  * bound and an upper bound.  This function converts
1777c478bd9Sstevel@tonic-gate  * a character string into its correct integer values,
1787c478bd9Sstevel@tonic-gate  * setting the first value as the lower bound, and
1797c478bd9Sstevel@tonic-gate  * the second value as the upper bound.  If more values
1807c478bd9Sstevel@tonic-gate  * are given they are ignored with a warning.
1817c478bd9Sstevel@tonic-gate  */
1827c478bd9Sstevel@tonic-gate static void
1837c478bd9Sstevel@tonic-gate set_range(char *s, int  *low, int  *high)
1847c478bd9Sstevel@tonic-gate {
1857c478bd9Sstevel@tonic-gate 	char *w;
1867c478bd9Sstevel@tonic-gate 	char *lasts;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	while ((w = strtok_r(s, ",", &lasts)) != NULL) {
1897c478bd9Sstevel@tonic-gate 		if (!(*low))
1907c478bd9Sstevel@tonic-gate 			/* LINTED */
1917c478bd9Sstevel@tonic-gate 			*low = (int)atol(w);
1927c478bd9Sstevel@tonic-gate 		else
1937c478bd9Sstevel@tonic-gate 			if (!(*high))
1947c478bd9Sstevel@tonic-gate 				/* LINTED */
1957c478bd9Sstevel@tonic-gate 				*high = (int)atol(w);
1967c478bd9Sstevel@tonic-gate 			else {
1977c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1987c478bd9Sstevel@tonic-gate 				    "%s: too many arguments - %s ignored\n",
1997c478bd9Sstevel@tonic-gate 				    prog_name, w);
2007c478bd9Sstevel@tonic-gate 				return;
2017c478bd9Sstevel@tonic-gate 			}
2027c478bd9Sstevel@tonic-gate 		s = NULL;
2037c478bd9Sstevel@tonic-gate 	} /* end while */
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate /*
2087c478bd9Sstevel@tonic-gate  * Print static shared library information.
2097c478bd9Sstevel@tonic-gate  */
2107c478bd9Sstevel@tonic-gate static void
2117c478bd9Sstevel@tonic-gate print_static(SCNTAB *l_scns, char *filename)
2127c478bd9Sstevel@tonic-gate {
2137c478bd9Sstevel@tonic-gate 	size_t section_size;
2147c478bd9Sstevel@tonic-gate 	unsigned char *strtab;
2157c478bd9Sstevel@tonic-gate 	unsigned char *path, buf[1024];
2167c478bd9Sstevel@tonic-gate 	unsigned long *temp;
2177c478bd9Sstevel@tonic-gate 	unsigned long total, topath;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	(void) printf("\n  **** STATIC SHARED LIBRARY INFORMATION ****\n");
2207c478bd9Sstevel@tonic-gate 	(void) printf("\n%s:\n", filename);
2217c478bd9Sstevel@tonic-gate 	(void) printf("\t");
2227c478bd9Sstevel@tonic-gate 	section_size  = 0;
2237c478bd9Sstevel@tonic-gate 	if ((strtab = (unsigned char *)
2247c478bd9Sstevel@tonic-gate 	    get_scndata(l_scns->p_sd, &section_size)) == NULL) {
2257c478bd9Sstevel@tonic-gate 		return;
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	while (section_size != 0) {
2297c478bd9Sstevel@tonic-gate 		/* LINTED */
2307c478bd9Sstevel@tonic-gate 		temp = (unsigned long *)strtab;
2317c478bd9Sstevel@tonic-gate 		total = temp[0];
2327c478bd9Sstevel@tonic-gate 		topath = temp[1];
2337c478bd9Sstevel@tonic-gate 		path = strtab + (topath*sizeof (long));
2347c478bd9Sstevel@tonic-gate 		(void) strncpy((char *)buf, (char *)path,
2357c478bd9Sstevel@tonic-gate 		    (total - topath)*sizeof (long));
2367c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\n", buf);
2377c478bd9Sstevel@tonic-gate 		strtab += total*sizeof (long);
2387c478bd9Sstevel@tonic-gate 		section_size -= (total*sizeof (long));
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate /*
2437c478bd9Sstevel@tonic-gate  * Print raw data in hexidecimal.  Input is the section data to
2447c478bd9Sstevel@tonic-gate  * be printed out and the size of the data.  Output is relative
2457c478bd9Sstevel@tonic-gate  * to a table lookup in dumpmap.h.
2467c478bd9Sstevel@tonic-gate  */
2477c478bd9Sstevel@tonic-gate static void
2487c478bd9Sstevel@tonic-gate print_rawdata(unsigned char *p_sec, size_t size)
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate 	size_t   j;
2517c478bd9Sstevel@tonic-gate 	size_t   count;
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	count = 1;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	(void) printf("\t");
2567c478bd9Sstevel@tonic-gate 	for (j = size/sizeof (short); j != 0; --j, ++count) {
2577c478bd9Sstevel@tonic-gate 		(void) printf("%.2x %.2x ", p_sec[0], p_sec[1]);
2587c478bd9Sstevel@tonic-gate 		p_sec += 2;
2597c478bd9Sstevel@tonic-gate 		if (count == 12) {
2607c478bd9Sstevel@tonic-gate 			(void) printf("\n\t");
2617c478bd9Sstevel@tonic-gate 			count = 0;
2627c478bd9Sstevel@tonic-gate 		}
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	/*
2667c478bd9Sstevel@tonic-gate 	 * take care of last byte if odd byte section
2677c478bd9Sstevel@tonic-gate 	 */
2687c478bd9Sstevel@tonic-gate 	if ((size & 0x1L) == 1L)
2697c478bd9Sstevel@tonic-gate 		(void) printf("%.2x", *p_sec);
2707c478bd9Sstevel@tonic-gate 	(void) printf("\n");
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate /*
2767c478bd9Sstevel@tonic-gate  * Print relocation data of type SHT_RELA
2777c478bd9Sstevel@tonic-gate  * If d_flag, print data corresponding only to
2787c478bd9Sstevel@tonic-gate  * the section or range of sections specified.
2797c478bd9Sstevel@tonic-gate  * If n_flag, print data corresponding only to
2807c478bd9Sstevel@tonic-gate  * the named section.
2817c478bd9Sstevel@tonic-gate  */
2827c478bd9Sstevel@tonic-gate static void
2837c478bd9Sstevel@tonic-gate print_rela(Elf *elf_file, SCNTAB *p_scns, Elf_Data *rdata, Elf_Data *sym_data,
2847c478bd9Sstevel@tonic-gate 	GElf_Ehdr * p_ehdr, size_t reloc_size, size_t sym_size, char *filename,
2857c478bd9Sstevel@tonic-gate 	SCNTAB *reloc_symtab)
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate 	GElf_Rela rela;
2887c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
2897c478bd9Sstevel@tonic-gate 	size_t no_entries;
2907c478bd9Sstevel@tonic-gate 	size_t rel_entsize;
2917c478bd9Sstevel@tonic-gate 	size_t no_syms;
2927c478bd9Sstevel@tonic-gate 	int type, symid;
2937c478bd9Sstevel@tonic-gate 	static int n_title = 0;
2947c478bd9Sstevel@tonic-gate 	int ndx = 0;
2957c478bd9Sstevel@tonic-gate 	char *sym_name;
2967c478bd9Sstevel@tonic-gate 	int adj = 0;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	if (gelf_getclass(elf_file) == ELFCLASS64)
2995aefb655Srie 		adj = 8;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	rel_entsize = p_scns->p_shdr.sh_entsize;
3027c478bd9Sstevel@tonic-gate 	if ((rel_entsize == 0) ||
3037c478bd9Sstevel@tonic-gate 	    (rel_entsize > p_scns->p_shdr.sh_size)) {
3047c478bd9Sstevel@tonic-gate 		rel_entsize = gelf_fsize(elf_file, ELF_T_RELA, 1,
3057c478bd9Sstevel@tonic-gate 		    EV_CURRENT);
3067c478bd9Sstevel@tonic-gate 	}
3077c478bd9Sstevel@tonic-gate 	no_entries = reloc_size / rel_entsize;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	no_syms = sym_size / gelf_fsize(elf_file, ELF_T_SYM, 1, EV_CURRENT);
3107c478bd9Sstevel@tonic-gate 	while (no_entries--) {
3117c478bd9Sstevel@tonic-gate 		(void) gelf_getrela(rdata, ndx, &rela);
3127c478bd9Sstevel@tonic-gate 		/* LINTED */
3137c478bd9Sstevel@tonic-gate 		type = (int)GELF_R_TYPE(rela.r_info);
3147c478bd9Sstevel@tonic-gate 		/* LINTED */
3157c478bd9Sstevel@tonic-gate 		symid = (int)GELF_R_SYM(rela.r_info);
3167c478bd9Sstevel@tonic-gate 		/* LINTED */
3177c478bd9Sstevel@tonic-gate 		if ((symid > (no_syms - 1)) || (symid < 0)) {
3187c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s: invalid symbol table "
3197c478bd9Sstevel@tonic-gate 			    "offset - %d - in %s\n", prog_name, filename,
3207c478bd9Sstevel@tonic-gate 			    symid, p_scns->scn_name);
3217c478bd9Sstevel@tonic-gate 			ndx++;
3227c478bd9Sstevel@tonic-gate 			continue;
3237c478bd9Sstevel@tonic-gate 		}
3247c478bd9Sstevel@tonic-gate 		(void) gelf_getsym(sym_data, symid, &sym);
3257c478bd9Sstevel@tonic-gate 		sym_name = (char *)elf_strptr(elf_file,
3267c478bd9Sstevel@tonic-gate 		    reloc_symtab->p_shdr.sh_link, sym.st_name);
3277c478bd9Sstevel@tonic-gate 		if (sym_name == NULL)
3287c478bd9Sstevel@tonic-gate 			sym_name = (char *)UNKNOWN;
3297c478bd9Sstevel@tonic-gate 		if (r_flag && rn_flag) {
3307c478bd9Sstevel@tonic-gate 			if (strcmp(name, p_scns->scn_name) != 0) {
3317c478bd9Sstevel@tonic-gate 				ndx++;
3327c478bd9Sstevel@tonic-gate 				continue;
3337c478bd9Sstevel@tonic-gate 			}
3347c478bd9Sstevel@tonic-gate 			if (!n_title) {
3357c478bd9Sstevel@tonic-gate 				(void) printf("\n%s:\n", p_scns->scn_name);
3367c478bd9Sstevel@tonic-gate 				(void) printf("%-*s%-*s%-*s%s\n\n",
3377c478bd9Sstevel@tonic-gate 				    12 + adj, "Offset", 22, "Symndx",
3387c478bd9Sstevel@tonic-gate 				    16, "Type", "Addend");
3397c478bd9Sstevel@tonic-gate 				n_title = 1;
3407c478bd9Sstevel@tonic-gate 			}
3417c478bd9Sstevel@tonic-gate 		}
3427c478bd9Sstevel@tonic-gate 		if (d_flag) {
3437c478bd9Sstevel@tonic-gate 			if (!d_hi)
3447c478bd9Sstevel@tonic-gate 				d_hi = d_low;
3457c478bd9Sstevel@tonic-gate 			if ((symid < d_low) || (symid > d_hi)) {
3467c478bd9Sstevel@tonic-gate 				ndx++;
3477c478bd9Sstevel@tonic-gate 				continue;
3487c478bd9Sstevel@tonic-gate 			}
3497c478bd9Sstevel@tonic-gate 		}
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 		(void) printf("%-#*llx", 12 + adj, EC_XWORD(rela.r_offset));
3527c478bd9Sstevel@tonic-gate 		if (!v_flag) {
3537c478bd9Sstevel@tonic-gate 			(void) printf("%-22d%-18d", symid, type);
3547c478bd9Sstevel@tonic-gate 		} else {
355de777a60Sab196087 			Conv_inv_buf_t	inv_buf;
356de777a60Sab196087 
3577c478bd9Sstevel@tonic-gate 			if (strlen(sym_name)) {
3587c478bd9Sstevel@tonic-gate 				size_t len = strlen(sym_name) + 1;
3597c478bd9Sstevel@tonic-gate 				char tmpstr[10];
3607c478bd9Sstevel@tonic-gate 				if (len > 22) {
3617c478bd9Sstevel@tonic-gate 					(void) sprintf(tmpstr, "%%-%ds",
3627c478bd9Sstevel@tonic-gate 					    /* LINTED */
3637c478bd9Sstevel@tonic-gate 					    (int)len);
364ba4e3c84Sab196087 					/*LINTED: E_SEC_PRINTF_VAR_FMT*/
3657c478bd9Sstevel@tonic-gate 					(void) printf(tmpstr, sym_name);
3667c478bd9Sstevel@tonic-gate 				} else
3677c478bd9Sstevel@tonic-gate 					(void) printf("%-22s", sym_name);
368c13de8f6Sab196087 			} else {
3697c478bd9Sstevel@tonic-gate 				(void) printf("%-22d", symid);
370c13de8f6Sab196087 			}
371c13de8f6Sab196087 			(void) printf("%-20s",
372c13de8f6Sab196087 			    conv_reloc_type(p_ehdr->e_machine,
373de777a60Sab196087 			    type, DUMP_CONVFMT, &inv_buf));
3747c478bd9Sstevel@tonic-gate 		}
3757c478bd9Sstevel@tonic-gate 		(void) printf("%lld\n", EC_SXWORD(rela.r_addend));
3767c478bd9Sstevel@tonic-gate 		ndx++;
3777c478bd9Sstevel@tonic-gate 	}
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate /*
3817c478bd9Sstevel@tonic-gate  * Print relocation data of type SHT_REL.
3827c478bd9Sstevel@tonic-gate  * If d_flag, print data corresponding only to
3837c478bd9Sstevel@tonic-gate  * the section or range of sections specified.
3847c478bd9Sstevel@tonic-gate  * If n_flag, print data corresponding only to
3857c478bd9Sstevel@tonic-gate  * the named section.
3867c478bd9Sstevel@tonic-gate  */
3877c478bd9Sstevel@tonic-gate static void
3887c478bd9Sstevel@tonic-gate print_rel(Elf *elf_file, SCNTAB *p_scns, Elf_Data *rdata, Elf_Data *sym_data,
3897c478bd9Sstevel@tonic-gate 	GElf_Ehdr *p_ehdr, size_t reloc_size, size_t sym_size, char *filename,
3907c478bd9Sstevel@tonic-gate 	SCNTAB *reloc_symtab)
3917c478bd9Sstevel@tonic-gate {
3927c478bd9Sstevel@tonic-gate 	GElf_Rel rel;
3937c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
3947c478bd9Sstevel@tonic-gate 	size_t no_entries;
3957c478bd9Sstevel@tonic-gate 	size_t rel_entsize;
3967c478bd9Sstevel@tonic-gate 	int type, symid;
3977c478bd9Sstevel@tonic-gate 	size_t no_syms;
3987c478bd9Sstevel@tonic-gate 	static int n_title = 0;
3997c478bd9Sstevel@tonic-gate 	int ndx = 0;
4007c478bd9Sstevel@tonic-gate 	char *sym_name;
4017c478bd9Sstevel@tonic-gate 	int adj = 0;
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	if (gelf_getclass(elf_file) == ELFCLASS64)
4045aefb655Srie 		adj = 8;
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	rel_entsize = p_scns->p_shdr.sh_entsize;
4077c478bd9Sstevel@tonic-gate 	if ((rel_entsize == 0) ||
4087c478bd9Sstevel@tonic-gate 	    (rel_entsize > p_scns->p_shdr.sh_size)) {
4097c478bd9Sstevel@tonic-gate 		rel_entsize = gelf_fsize(elf_file, ELF_T_REL, 1,
4107c478bd9Sstevel@tonic-gate 		    EV_CURRENT);
4117c478bd9Sstevel@tonic-gate 	}
4127c478bd9Sstevel@tonic-gate 	no_entries = reloc_size / rel_entsize;
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	no_syms = sym_size / gelf_fsize(elf_file, ELF_T_SYM, 1, EV_CURRENT);
4157c478bd9Sstevel@tonic-gate 	while (no_entries--) {
4167c478bd9Sstevel@tonic-gate 		(void) gelf_getrel(rdata, ndx, &rel);
4177c478bd9Sstevel@tonic-gate 		/* LINTED */
4187c478bd9Sstevel@tonic-gate 		type = (int)GELF_R_TYPE(rel.r_info);
4197c478bd9Sstevel@tonic-gate 		/* LINTED */
4207c478bd9Sstevel@tonic-gate 		symid = (int)GELF_R_SYM(rel.r_info);
4217c478bd9Sstevel@tonic-gate 		/* LINTED */
4227c478bd9Sstevel@tonic-gate 		if ((symid > (no_syms - 1)) || (symid < 0)) {
4237c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s: invalid symbol table "
4247c478bd9Sstevel@tonic-gate 			    "offset - %d - in %s\n", prog_name, filename,
4257c478bd9Sstevel@tonic-gate 			    symid, p_scns->scn_name);
4267c478bd9Sstevel@tonic-gate 			ndx++;
4277c478bd9Sstevel@tonic-gate 			continue;
4287c478bd9Sstevel@tonic-gate 		}
4297c478bd9Sstevel@tonic-gate 		(void) gelf_getsym(sym_data, symid, &sym);
4307c478bd9Sstevel@tonic-gate 		sym_name = (char *)elf_strptr(elf_file,
4317c478bd9Sstevel@tonic-gate 		    reloc_symtab->p_shdr.sh_link, sym.st_name);
4327c478bd9Sstevel@tonic-gate 		if (sym_name == NULL)
4337c478bd9Sstevel@tonic-gate 			sym_name = (char *)UNKNOWN;
4347c478bd9Sstevel@tonic-gate 		if (r_flag && rn_flag) {
4357c478bd9Sstevel@tonic-gate 			if (strcmp(name, p_scns->scn_name) != 0) {
4367c478bd9Sstevel@tonic-gate 				ndx++;
4377c478bd9Sstevel@tonic-gate 				continue;
4387c478bd9Sstevel@tonic-gate 			}
4397c478bd9Sstevel@tonic-gate 			if (!n_title) {
4407c478bd9Sstevel@tonic-gate 				(void) printf("\n%s:\n", p_scns->scn_name);
4417c478bd9Sstevel@tonic-gate 				(void) printf("%-*s%-*s%s\n\n",
4427c478bd9Sstevel@tonic-gate 				    12 + adj, "Offset", 20, "Symndx", "Type");
4437c478bd9Sstevel@tonic-gate 				n_title = 1;
4447c478bd9Sstevel@tonic-gate 			}
4457c478bd9Sstevel@tonic-gate 		}
4467c478bd9Sstevel@tonic-gate 		if (d_flag) {
4477c478bd9Sstevel@tonic-gate 			if (!d_hi)
4487c478bd9Sstevel@tonic-gate 				d_hi = d_low;
4497c478bd9Sstevel@tonic-gate 			if ((symid < d_low) || (symid > d_hi)) {
4507c478bd9Sstevel@tonic-gate 				ndx++;
4517c478bd9Sstevel@tonic-gate 				continue;
4527c478bd9Sstevel@tonic-gate 			}
4537c478bd9Sstevel@tonic-gate 		}
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 		(void) printf("%-#*llx", 12 + adj, EC_ADDR(rel.r_offset));
4567c478bd9Sstevel@tonic-gate 		if (!v_flag) {
4577c478bd9Sstevel@tonic-gate 			(void) printf("%-20d%-18d", symid, type);
4587c478bd9Sstevel@tonic-gate 		} else {
459de777a60Sab196087 			Conv_inv_buf_t	inv_buf;
460de777a60Sab196087 
4617c478bd9Sstevel@tonic-gate 			if (strlen(sym_name))
4627c478bd9Sstevel@tonic-gate 				(void) printf("%-20s", sym_name);
463c13de8f6Sab196087 			else {
4647c478bd9Sstevel@tonic-gate 				(void) printf("%-20d", sym.st_name);
465c13de8f6Sab196087 			}
466c13de8f6Sab196087 			(void) printf("%-20s",
467c13de8f6Sab196087 			    conv_reloc_type(p_ehdr->e_machine,
468de777a60Sab196087 			    type, DUMP_CONVFMT, &inv_buf));
4697c478bd9Sstevel@tonic-gate 		}
4707c478bd9Sstevel@tonic-gate 		(void) printf("\n");
4717c478bd9Sstevel@tonic-gate 		ndx++;
4727c478bd9Sstevel@tonic-gate 	}
4737c478bd9Sstevel@tonic-gate }
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate /* demangle C++ names */
4767c478bd9Sstevel@tonic-gate static char *
4777c478bd9Sstevel@tonic-gate demangled_name(char *s)
4787c478bd9Sstevel@tonic-gate {
4795aefb655Srie 	static char	*buf = NULL;
480b9bd317cSab196087 	const char	*dn;
4815aefb655Srie 	size_t		len;
4827c478bd9Sstevel@tonic-gate 
483b9bd317cSab196087 	dn = conv_demangle_name(s);
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	/*
4867c478bd9Sstevel@tonic-gate 	 * If not demangled, just return the symbol name
4877c478bd9Sstevel@tonic-gate 	 */
4887c478bd9Sstevel@tonic-gate 	if (strcmp(s, dn) == 0)
4897c478bd9Sstevel@tonic-gate 		return (s);
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	/*
4927c478bd9Sstevel@tonic-gate 	 * Demangled. Format it
4937c478bd9Sstevel@tonic-gate 	 */
4947c478bd9Sstevel@tonic-gate 	if (buf != NULL)
4957c478bd9Sstevel@tonic-gate 		free(buf);
4967c478bd9Sstevel@tonic-gate 
4975aefb655Srie 	len = strlen(dn) + strlen(s) + 4;
4985aefb655Srie 	if ((buf = malloc(len)) == NULL)
4997c478bd9Sstevel@tonic-gate 		return (s);
5007c478bd9Sstevel@tonic-gate 
5015aefb655Srie 	(void) snprintf(buf, len, "%s\t[%s]", dn, s);
5027c478bd9Sstevel@tonic-gate 	return (buf);
5037c478bd9Sstevel@tonic-gate }
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate /*
5067c478bd9Sstevel@tonic-gate  * Print the symbol table.  Input is an ELF file descriptor, a
5077c478bd9Sstevel@tonic-gate  * pointer to the symbol table SCNTAB structure,
5087c478bd9Sstevel@tonic-gate  * the number of symbols, a range of symbols to print,
5097c478bd9Sstevel@tonic-gate  * an index which is the number of the
5107c478bd9Sstevel@tonic-gate  * section in the file, and the filename.  The number of sections,
5117c478bd9Sstevel@tonic-gate  * the range, and the index are set in
5127c478bd9Sstevel@tonic-gate  * dump_symbol_table, depending on whether -n or -T were set.
5137c478bd9Sstevel@tonic-gate  */
5147c478bd9Sstevel@tonic-gate static void
5157c478bd9Sstevel@tonic-gate print_symtab(Elf *elf_file, SCNTAB *p_symtab, Elf_Data *sym_data,
5167c478bd9Sstevel@tonic-gate 	long range, int index)
5177c478bd9Sstevel@tonic-gate {
5187c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
5197c478bd9Sstevel@tonic-gate 	int adj = 0;		/* field adjustment for elf64 */
5207c478bd9Sstevel@tonic-gate 	Elf32_Word	*symshndx = 0;
5217c478bd9Sstevel@tonic-gate 	unsigned int	nosymshndx = 0;
522de777a60Sab196087 	Conv_inv_buf_t	inv_buf;
523de777a60Sab196087 
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	if (gelf_getclass(elf_file) == ELFCLASS64)
5265aefb655Srie 		adj = 8;
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	while (range > 0) {
5297c478bd9Sstevel@tonic-gate 		char		*sym_name = (char *)0;
5307c478bd9Sstevel@tonic-gate 		int		type, bind;
5317c478bd9Sstevel@tonic-gate 		int		specsec;
5327c478bd9Sstevel@tonic-gate 		unsigned int	shndx;
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 		(void) gelf_getsym(sym_data, index, &sym);
5357c478bd9Sstevel@tonic-gate 		type = (int)GELF_ST_TYPE(sym.st_info);
5367c478bd9Sstevel@tonic-gate 		bind = (int)GELF_ST_BIND(sym.st_info);
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 		if ((sym.st_shndx == SHN_XINDEX) &&
5397c478bd9Sstevel@tonic-gate 		    (symshndx == 0) && (nosymshndx == 0)) {
5407c478bd9Sstevel@tonic-gate 			Elf_Scn		*_scn;
5417c478bd9Sstevel@tonic-gate 			GElf_Shdr	_shdr;
5427c478bd9Sstevel@tonic-gate 			size_t		symscnndx;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 			symscnndx = elf_ndxscn(p_symtab->p_sd);
5457c478bd9Sstevel@tonic-gate 			_scn = 0;
5467c478bd9Sstevel@tonic-gate 			while ((_scn = elf_nextscn(elf_file, _scn)) != 0) {
5477c478bd9Sstevel@tonic-gate 				if (gelf_getshdr(_scn, &_shdr) == 0)
5487c478bd9Sstevel@tonic-gate 					break;
5497c478bd9Sstevel@tonic-gate 				if ((_shdr.sh_type == SHT_SYMTAB_SHNDX) &&
5507c478bd9Sstevel@tonic-gate 				    /* LINTED */
5517c478bd9Sstevel@tonic-gate 				    (_shdr.sh_link == (GElf_Word)symscnndx)) {
5527c478bd9Sstevel@tonic-gate 					Elf_Data	*_data;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 					if ((_data = elf_getdata(_scn, 0)) == 0)
5557c478bd9Sstevel@tonic-gate 						continue;
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 					symshndx = (Elf32_Word *)_data->d_buf;
5587c478bd9Sstevel@tonic-gate 					nosymshndx = 0;
5597c478bd9Sstevel@tonic-gate 					break;
5607c478bd9Sstevel@tonic-gate 				}
5617c478bd9Sstevel@tonic-gate 			}
5627c478bd9Sstevel@tonic-gate 			nosymshndx = 1;
5637c478bd9Sstevel@tonic-gate 		}
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 		if ((symshndx) && (sym.st_shndx == SHN_XINDEX)) {
5667c478bd9Sstevel@tonic-gate 			shndx = symshndx[index];
5677c478bd9Sstevel@tonic-gate 			specsec = 0;
5687c478bd9Sstevel@tonic-gate 		} else {
5697c478bd9Sstevel@tonic-gate 			shndx = sym.st_shndx;
5707c478bd9Sstevel@tonic-gate 			if ((sym.st_shndx == SHN_UNDEF) ||
5717c478bd9Sstevel@tonic-gate 			    (sym.st_shndx >= SHN_LORESERVE))
5727c478bd9Sstevel@tonic-gate 				specsec = 1;
5737c478bd9Sstevel@tonic-gate 			else
5747c478bd9Sstevel@tonic-gate 				specsec = 0;
5757c478bd9Sstevel@tonic-gate 		}
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 		(void) printf("[%d]\t ", index++);
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 		if (v_flag && (type == STT_SPARC_REGISTER)) {
5817c478bd9Sstevel@tonic-gate 			/*
5827c478bd9Sstevel@tonic-gate 			 *  The strings "REG_G1" through "REG_G7" are intended
5837c478bd9Sstevel@tonic-gate 			 *  to be consistent with output from elfdump(1).
5847c478bd9Sstevel@tonic-gate 			 */
585c13de8f6Sab196087 			(void) printf("%-*s", 12 + adj,
586de777a60Sab196087 			    conv_sym_SPARC_value(sym.st_value,
587de777a60Sab196087 			    DUMP_CONVFMT, &inv_buf));
588c13de8f6Sab196087 		} else {
5897c478bd9Sstevel@tonic-gate 			(void) printf("0x%-*llx", 10 + adj,
5907c478bd9Sstevel@tonic-gate 			    EC_ADDR(sym.st_value));
5917c478bd9Sstevel@tonic-gate 		}
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 		(void) printf("%-*lld", 9 + adj, EC_XWORD(sym.st_size));
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 		if (!v_flag) {
5967c478bd9Sstevel@tonic-gate 			(void) printf("%d\t\t%d\t%d\t%#x\t",
5977c478bd9Sstevel@tonic-gate 			    type, bind, (int)sym.st_other, (int)shndx);
5987c478bd9Sstevel@tonic-gate 		} else {
599c13de8f6Sab196087 			GElf_Ehdr p_ehdr;
600c13de8f6Sab196087 			(void) gelf_getehdr(elf_file, &p_ehdr);
601c13de8f6Sab196087 			(void) printf("%s\t",
602c13de8f6Sab196087 			    conv_sym_info_type(p_ehdr.e_machine, type,
603de777a60Sab196087 			    DUMP_CONVFMT, &inv_buf));
604c13de8f6Sab196087 			(void) printf("%s",
605de777a60Sab196087 			    conv_sym_info_bind(bind, DUMP_CONVFMT, &inv_buf));
6067c478bd9Sstevel@tonic-gate 			(void) printf("\t  %d\t", EC_WORD(sym.st_other));
6077c478bd9Sstevel@tonic-gate 
608de777a60Sab196087 			if (specsec)
609de777a60Sab196087 				(void) printf("%s",
6104f680cc6SAli Bahrami 				    conv_sym_shndx(p_ehdr.e_ident[EI_OSABI],
6114f680cc6SAli Bahrami 				    p_ehdr.e_machine, shndx,
6124f680cc6SAli Bahrami 				    CONV_FMT_DECIMAL, &inv_buf));
613de777a60Sab196087 			else
6147c478bd9Sstevel@tonic-gate 				(void) printf("%d", EC_WORD(shndx));
6157c478bd9Sstevel@tonic-gate 			(void) printf("\t");
6167c478bd9Sstevel@tonic-gate 		}
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 		/* support machines where NULL-deref causes core dump */
6197c478bd9Sstevel@tonic-gate 		if (sym.st_name == 0)
6207c478bd9Sstevel@tonic-gate 			sym_name = (char *)UNKNOWN;
6217c478bd9Sstevel@tonic-gate 		else
6227c478bd9Sstevel@tonic-gate 			if (C_flag)
6237c478bd9Sstevel@tonic-gate 				sym_name = demangled_name(
6247c478bd9Sstevel@tonic-gate 				    (char *)elf_strptr(elf_file,
6257c478bd9Sstevel@tonic-gate 				    p_symtab->p_shdr.sh_link,
6267c478bd9Sstevel@tonic-gate 				    sym.st_name));
6277c478bd9Sstevel@tonic-gate 		else
6287c478bd9Sstevel@tonic-gate 			sym_name = (char *)elf_strptr(elf_file,
629de777a60Sab196087 			    p_symtab->p_shdr.sh_link, sym.st_name);
6307c478bd9Sstevel@tonic-gate 		if (sym_name == NULL)
6317c478bd9Sstevel@tonic-gate 			sym_name = (char *)UNKNOWN;
6327c478bd9Sstevel@tonic-gate 		(void) printf("%s\n", sym_name);
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 		range--;
6357c478bd9Sstevel@tonic-gate 	}	/* end while */
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate /*
6397c478bd9Sstevel@tonic-gate  * Print the section header table.  Input is the SCNTAB structure,
6407c478bd9Sstevel@tonic-gate  * the number of sections, an index which is the number of the
6417c478bd9Sstevel@tonic-gate  * section in the file, and the filename.  The values of the SCNTAB
6427c478bd9Sstevel@tonic-gate  * structure, the number of sections, and the index are set in
6437c478bd9Sstevel@tonic-gate  * dump_shdr depending on whether the -n or -d modifiers were set.
6447c478bd9Sstevel@tonic-gate  */
6457c478bd9Sstevel@tonic-gate static void
6467c478bd9Sstevel@tonic-gate print_shdr(Elf *elf_file, SCNTAB *s, int num_scns, int index)
6477c478bd9Sstevel@tonic-gate {
6487c478bd9Sstevel@tonic-gate 	SCNTAB *p;
6497c478bd9Sstevel@tonic-gate 	int num;
6507c478bd9Sstevel@tonic-gate 	int field;
651c13de8f6Sab196087 	GElf_Ehdr p_ehdr;
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	if (gelf_getclass(elf_file) == ELFCLASS64)
6545aefb655Srie 		field = 21;
6557c478bd9Sstevel@tonic-gate 	else
6567c478bd9Sstevel@tonic-gate 		field = 13;
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	p = s;
659c13de8f6Sab196087 	(void) gelf_getehdr(elf_file, &p_ehdr);
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	for (num = 0; num < num_scns; num++, p++) {
6627c478bd9Sstevel@tonic-gate 		(void) printf("[%d]\t", index++);
6637c478bd9Sstevel@tonic-gate 		if (!v_flag) {
6647c478bd9Sstevel@tonic-gate 			(void) printf("%u\t%llu\t",
6657c478bd9Sstevel@tonic-gate 			    EC_WORD(p->p_shdr.sh_type),
6667c478bd9Sstevel@tonic-gate 			    EC_XWORD(p->p_shdr.sh_flags));
6677c478bd9Sstevel@tonic-gate 		} else {
668de777a60Sab196087 			Conv_inv_buf_t inv_buf;
669de777a60Sab196087 
670ba4e3c84Sab196087 			/*LINTED: E_SEC_PRINTF_VAR_FMT*/
6714f680cc6SAli Bahrami 			(void) printf(conv_sec_type(
6724f680cc6SAli Bahrami 			    p_ehdr.e_ident[EI_OSABI], p_ehdr.e_machine,
673de777a60Sab196087 			    p->p_shdr.sh_type, DUMP_CONVFMT, &inv_buf));
6747c478bd9Sstevel@tonic-gate 			(void) printf("    ");
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 			if (p->p_shdr.sh_flags & SHF_WRITE)
6777c478bd9Sstevel@tonic-gate 				(void) printf("W");
6787c478bd9Sstevel@tonic-gate 			else
6797c478bd9Sstevel@tonic-gate 				(void) printf("-");
6807c478bd9Sstevel@tonic-gate 			if (p->p_shdr.sh_flags & SHF_ALLOC)
6817c478bd9Sstevel@tonic-gate 				(void) printf("A");
6827c478bd9Sstevel@tonic-gate 			else
6837c478bd9Sstevel@tonic-gate 				(void) printf("-");
6847c478bd9Sstevel@tonic-gate 			if (p->p_shdr.sh_flags & SHF_EXECINSTR)
6857c478bd9Sstevel@tonic-gate 				(void) printf("I");
6867c478bd9Sstevel@tonic-gate 			else
6877c478bd9Sstevel@tonic-gate 				(void) printf("-");
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 			if (p->p_shdr.sh_flags & SHF_ORDERED)
6907c478bd9Sstevel@tonic-gate 				(void) printf("O");
6917c478bd9Sstevel@tonic-gate 			if (p->p_shdr.sh_flags & SHF_EXCLUDE)
6927c478bd9Sstevel@tonic-gate 				(void) printf("E");
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 			(void) printf("\t");
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 		}
6977c478bd9Sstevel@tonic-gate 		(void) printf("%-#*llx%-#*llx%-#*llx%s%s\n",
6987c478bd9Sstevel@tonic-gate 		    field, EC_ADDR(p->p_shdr.sh_addr),
6997c478bd9Sstevel@tonic-gate 		    field, EC_OFF(p->p_shdr.sh_offset),
7007c478bd9Sstevel@tonic-gate 		    field, EC_XWORD(p->p_shdr.sh_size),
7017c478bd9Sstevel@tonic-gate 		    /* compatibility:  tab for elf32 */
7027c478bd9Sstevel@tonic-gate 		    (field == 13) ? "\t" : " ", p->scn_name);
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 		(void) printf("\t%u\t%u\t%-#*llx%-#*llx\n\n",
7057c478bd9Sstevel@tonic-gate 		    EC_WORD(p->p_shdr.sh_link),
7067c478bd9Sstevel@tonic-gate 		    EC_WORD(p->p_shdr.sh_info),
7077c478bd9Sstevel@tonic-gate 		    field, EC_XWORD(p->p_shdr.sh_addralign),
7087c478bd9Sstevel@tonic-gate 		    field, EC_XWORD(p->p_shdr.sh_entsize));
7097c478bd9Sstevel@tonic-gate 	}
7107c478bd9Sstevel@tonic-gate }
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate /*
7137c478bd9Sstevel@tonic-gate  * Check that a range of numbers is valid.  Input is
7147c478bd9Sstevel@tonic-gate  * a lower bound, an upper bound, a boundary condition,
7157c478bd9Sstevel@tonic-gate  * and the filename.  Negative numbers and numbers greater
7167c478bd9Sstevel@tonic-gate  * than the bound are invalid.  low must be smaller than hi.
7177c478bd9Sstevel@tonic-gate  * The returned integer is the number of items in the
7187c478bd9Sstevel@tonic-gate  * range if it is valid and -1 otherwise.
7197c478bd9Sstevel@tonic-gate  */
7207c478bd9Sstevel@tonic-gate static int
7217c478bd9Sstevel@tonic-gate check_range(int low, int hi, size_t bound, char *filename)
7227c478bd9Sstevel@tonic-gate {
7237c478bd9Sstevel@tonic-gate 	if (((size_t)low > bound) || (low <= 0)) {
7247c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
7257c478bd9Sstevel@tonic-gate 		    "%s: %s: number out of range, %d\n",
7267c478bd9Sstevel@tonic-gate 		    prog_name, filename, low);
7277c478bd9Sstevel@tonic-gate 		return (-1);
7287c478bd9Sstevel@tonic-gate 	}
7297c478bd9Sstevel@tonic-gate 	if (((size_t)hi > bound) || (hi < 0)) {
7307c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
7317c478bd9Sstevel@tonic-gate 		    "%s: %s: number out of range, %d\n",
7327c478bd9Sstevel@tonic-gate 		    prog_name, filename, hi);
7337c478bd9Sstevel@tonic-gate 		return (-1);
7347c478bd9Sstevel@tonic-gate 	}
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	if (hi && (low > hi)) {
7377c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
7387c478bd9Sstevel@tonic-gate 		    "%s: %s: invalid range, %d,%d\n",
7397c478bd9Sstevel@tonic-gate 		    prog_name, filename, low, hi);
7407c478bd9Sstevel@tonic-gate 		return (-1);
7417c478bd9Sstevel@tonic-gate 	}
7427c478bd9Sstevel@tonic-gate 	if (hi)
7437c478bd9Sstevel@tonic-gate 		return (hi - low + 1);
7447c478bd9Sstevel@tonic-gate 	else
7457c478bd9Sstevel@tonic-gate 		return (1);
7467c478bd9Sstevel@tonic-gate }
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate /*
7497c478bd9Sstevel@tonic-gate  * Print relocation information.  Since this information is
7507c478bd9Sstevel@tonic-gate  * machine dependent, new sections must be added for each machine
7517c478bd9Sstevel@tonic-gate  * that is supported.  Input is an ELF file descriptor, the ELF header,
7527c478bd9Sstevel@tonic-gate  * the SCNTAB structure, the number of sections, and a filename.
7537c478bd9Sstevel@tonic-gate  * Set up necessary information to print relocation information
7547c478bd9Sstevel@tonic-gate  * and call the appropriate print function depending on the
7557c478bd9Sstevel@tonic-gate  * type of relocation information.  If the symbol table is
7567c478bd9Sstevel@tonic-gate  * absent, no relocation data is processed.  Input is an
7577c478bd9Sstevel@tonic-gate  * ELF file descriptor, the ELF header, the SCNTAB structure,
7587c478bd9Sstevel@tonic-gate  * and the filename.  Set range of d_flag and name if n_flag.
7597c478bd9Sstevel@tonic-gate  */
7607c478bd9Sstevel@tonic-gate static void
7617c478bd9Sstevel@tonic-gate dump_reloc_table(Elf *elf_file, GElf_Ehdr *p_ehdr,
7627c478bd9Sstevel@tonic-gate 	SCNTAB *p_scns, int num_scns, char *filename)
7637c478bd9Sstevel@tonic-gate {
7647c478bd9Sstevel@tonic-gate 	Elf_Data *rel_data;
7657c478bd9Sstevel@tonic-gate 	Elf_Data *sym_data;
7667c478bd9Sstevel@tonic-gate 	size_t    sym_size;
7677c478bd9Sstevel@tonic-gate 	size_t    reloc_size;
7687c478bd9Sstevel@tonic-gate 	SCNTAB *reloc_symtab;
7697c478bd9Sstevel@tonic-gate 	SCNTAB *head_scns;
7707c478bd9Sstevel@tonic-gate 	int r_title = 0;
7717c478bd9Sstevel@tonic-gate 	int adj = 0;
7727c478bd9Sstevel@tonic-gate 	size_t shnum;
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	if (gelf_getclass(elf_file) == ELFCLASS64)
7755aefb655Srie 		adj = 8;
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 	if ((!p_flag) && (!r_title)) {
7787c478bd9Sstevel@tonic-gate 		(void) printf("\n    **** RELOCATION INFORMATION ****\n");
7797c478bd9Sstevel@tonic-gate 		r_title = 1;
7807c478bd9Sstevel@tonic-gate 	}
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	while (num_scns-- > 0) {
7837c478bd9Sstevel@tonic-gate 		if ((p_scns->p_shdr.sh_type != SHT_RELA) &&
7847c478bd9Sstevel@tonic-gate 		    (p_scns->p_shdr.sh_type != SHT_REL)) {
7857c478bd9Sstevel@tonic-gate 			p_scns++;
7867c478bd9Sstevel@tonic-gate 			continue;
7877c478bd9Sstevel@tonic-gate 		}
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	head_scns = p_head_scns;
7907c478bd9Sstevel@tonic-gate 
79162b628a6SAli Bahrami 	if (elf_getshdrnum(elf_file, &shnum) == -1) {
7927c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
79362b628a6SAli Bahrami 		    "%s: %s: elf_getshdrnum failed: %s\n",
7947c478bd9Sstevel@tonic-gate 		    prog_name, filename, elf_errmsg(-1));
7957c478bd9Sstevel@tonic-gate 		return;
7967c478bd9Sstevel@tonic-gate 	}
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	if ((p_scns->p_shdr.sh_link == 0) ||
7997c478bd9Sstevel@tonic-gate 	    /* LINTED */
8007c478bd9Sstevel@tonic-gate 	    (p_scns->p_shdr.sh_link >= (GElf_Word)shnum)) {
8017c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: invalid sh_link field: "
8027c478bd9Sstevel@tonic-gate 		    "section #: %d sh_link: %d\n",
8037c478bd9Sstevel@tonic-gate 		    /* LINTED */
8047c478bd9Sstevel@tonic-gate 		    prog_name, filename, (int)elf_ndxscn(p_scns->p_sd),
8057c478bd9Sstevel@tonic-gate 		    (int)p_scns->p_shdr.sh_link);
8067c478bd9Sstevel@tonic-gate 		return;
8077c478bd9Sstevel@tonic-gate 	}
8087c478bd9Sstevel@tonic-gate 	head_scns += (p_scns->p_shdr.sh_link -1);
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 	if (head_scns->p_shdr.sh_type == SHT_SYMTAB) {
8117c478bd9Sstevel@tonic-gate 		reloc_symtab = p_symtab;
8127c478bd9Sstevel@tonic-gate 	} else if (head_scns->p_shdr.sh_type  == SHT_DYNSYM) {
8137c478bd9Sstevel@tonic-gate 		reloc_symtab = p_dynsym;
8147c478bd9Sstevel@tonic-gate 	} else {
8157c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
8167c478bd9Sstevel@tonic-gate "%s: %s: could not get symbol table\n", prog_name, filename);
8177c478bd9Sstevel@tonic-gate 		return;
8187c478bd9Sstevel@tonic-gate 	}
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	sym_data = NULL;
8217c478bd9Sstevel@tonic-gate 	sym_size = 0;
8227c478bd9Sstevel@tonic-gate 	reloc_size = 0;
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	if ((sym_data = elf_getdata(reloc_symtab->p_sd, NULL)) == NULL) {
8257c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
8267c478bd9Sstevel@tonic-gate 		"%s: %s: no symbol table data\n", prog_name, filename);
8277c478bd9Sstevel@tonic-gate 		return;
8287c478bd9Sstevel@tonic-gate 	}
8297c478bd9Sstevel@tonic-gate 	sym_size = sym_data->d_size;
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 	if (p_scns == NULL) {
8327c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
8337c478bd9Sstevel@tonic-gate 		"%s: %s: no section table data\n", prog_name, filename);
8347c478bd9Sstevel@tonic-gate 		return;
8357c478bd9Sstevel@tonic-gate 	}
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 	if (p_scns->p_shdr.sh_type == SHT_RELA) {
8387c478bd9Sstevel@tonic-gate 		if (!n_flag && r_flag)
8397c478bd9Sstevel@tonic-gate 			(void) printf("\n%s:\n", p_scns->scn_name);
8407c478bd9Sstevel@tonic-gate 		if (!p_flag && (!n_flag && r_flag))
8417c478bd9Sstevel@tonic-gate 			(void) printf("%-*s%-*s%-*s%s\n\n",
8427c478bd9Sstevel@tonic-gate 			    12 + adj, "Offset", 22, "Symndx",
8437c478bd9Sstevel@tonic-gate 			    18, "Type", "Addend");
8447c478bd9Sstevel@tonic-gate 		if ((rel_data = elf_getdata(p_scns->p_sd, NULL)) == NULL) {
8457c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
8467c478bd9Sstevel@tonic-gate "%s: %s: no relocation information\n", prog_name, filename);
8477c478bd9Sstevel@tonic-gate 			return;
8487c478bd9Sstevel@tonic-gate 		}
8497c478bd9Sstevel@tonic-gate 		reloc_size = rel_data->d_size;
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 		if (n_flag) {
8527c478bd9Sstevel@tonic-gate 			rn_flag = 1;
8537c478bd9Sstevel@tonic-gate 			print_rela(elf_file, p_scns, rel_data, sym_data, p_ehdr,
8547c478bd9Sstevel@tonic-gate 			    reloc_size, sym_size, filename, reloc_symtab);
8557c478bd9Sstevel@tonic-gate 		}
8567c478bd9Sstevel@tonic-gate 		if (d_flag) {
8577c478bd9Sstevel@tonic-gate 			rn_flag = 0;
8587c478bd9Sstevel@tonic-gate 			print_rela(elf_file, p_scns, rel_data, sym_data, p_ehdr,
8597c478bd9Sstevel@tonic-gate 			    reloc_size, sym_size, filename, reloc_symtab);
8607c478bd9Sstevel@tonic-gate 		}
8617c478bd9Sstevel@tonic-gate 		if (!n_flag && !d_flag)
8627c478bd9Sstevel@tonic-gate 			print_rela(elf_file, p_scns, rel_data, sym_data, p_ehdr,
8637c478bd9Sstevel@tonic-gate 			    reloc_size, sym_size, filename, reloc_symtab);
8647c478bd9Sstevel@tonic-gate 	} else {
8657c478bd9Sstevel@tonic-gate 		if (p_scns->p_shdr.sh_type == SHT_REL) {
8667c478bd9Sstevel@tonic-gate 			if (!n_flag && r_flag)
8677c478bd9Sstevel@tonic-gate 				(void) printf("\n%s:\n", p_scns->scn_name);
8687c478bd9Sstevel@tonic-gate 			if (!p_flag && (!n_flag && r_flag)) {
8697c478bd9Sstevel@tonic-gate 				(void) printf("%-*s%-*s%s\n\n",
8707c478bd9Sstevel@tonic-gate 				    12 + adj, "Offset", 20, "Symndx", "Type");
8717c478bd9Sstevel@tonic-gate 			}
8727c478bd9Sstevel@tonic-gate 			if ((rel_data = elf_getdata(p_scns->p_sd, NULL))
8737c478bd9Sstevel@tonic-gate 			    == NULL) {
8747c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
8757c478bd9Sstevel@tonic-gate "%s: %s: no relocation information\n", prog_name, filename);
8767c478bd9Sstevel@tonic-gate 				return;
8777c478bd9Sstevel@tonic-gate 			}
8787c478bd9Sstevel@tonic-gate 			reloc_size = rel_data->d_size;
8797c478bd9Sstevel@tonic-gate 			if (n_flag) {
8807c478bd9Sstevel@tonic-gate 				rn_flag = 1;
8817c478bd9Sstevel@tonic-gate 				print_rel(elf_file, p_scns, rel_data, sym_data,
8827c478bd9Sstevel@tonic-gate 				    p_ehdr, reloc_size, sym_size,
8837c478bd9Sstevel@tonic-gate 				    filename, reloc_symtab);
8847c478bd9Sstevel@tonic-gate 			}
8857c478bd9Sstevel@tonic-gate 			if (d_flag) {
8867c478bd9Sstevel@tonic-gate 				rn_flag = 0;
8877c478bd9Sstevel@tonic-gate 				print_rel(elf_file, p_scns, rel_data, sym_data,
8887c478bd9Sstevel@tonic-gate 				    p_ehdr, reloc_size, sym_size,
8897c478bd9Sstevel@tonic-gate 				    filename, reloc_symtab);
8907c478bd9Sstevel@tonic-gate 			}
8917c478bd9Sstevel@tonic-gate 			if (!n_flag && !d_flag)
8927c478bd9Sstevel@tonic-gate 				print_rel(elf_file, p_scns, rel_data, sym_data,
8937c478bd9Sstevel@tonic-gate 				    p_ehdr, reloc_size, sym_size,
8947c478bd9Sstevel@tonic-gate 				    filename, reloc_symtab);
8957c478bd9Sstevel@tonic-gate 		}
8967c478bd9Sstevel@tonic-gate 	}
8977c478bd9Sstevel@tonic-gate 	p_scns++;
8987c478bd9Sstevel@tonic-gate 	}
8997c478bd9Sstevel@tonic-gate }
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate /*
9027c478bd9Sstevel@tonic-gate  * Print out the string tables.  Input is an opened ELF file,
9037c478bd9Sstevel@tonic-gate  * the SCNTAB structure, the number of sections, and the filename.
9047c478bd9Sstevel@tonic-gate  * Since there can be more than one string table, all sections are
9057c478bd9Sstevel@tonic-gate  * examined and any with the correct type are printed out.
9067c478bd9Sstevel@tonic-gate  */
9077c478bd9Sstevel@tonic-gate static void
9087c478bd9Sstevel@tonic-gate dump_string_table(SCNTAB *s, int num_scns)
9097c478bd9Sstevel@tonic-gate {
9107c478bd9Sstevel@tonic-gate 	size_t section_size;
9117c478bd9Sstevel@tonic-gate 	unsigned char *strtab;
9127c478bd9Sstevel@tonic-gate 	int beg_of_string;
9137c478bd9Sstevel@tonic-gate 	int counter = 0;
9147c478bd9Sstevel@tonic-gate 	int str_off;
9157c478bd9Sstevel@tonic-gate 	int i;
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	if (!p_flag) {
9187c478bd9Sstevel@tonic-gate 		(void) printf("\n     **** STRING TABLE INFORMATION ****\n");
9197c478bd9Sstevel@tonic-gate 	}
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_scns; i++, s++) {
9227c478bd9Sstevel@tonic-gate 		if (s->p_shdr.sh_type != SHT_STRTAB)
9237c478bd9Sstevel@tonic-gate 			continue;
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 		str_off = 0;
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 		if (!p_flag) {
9287c478bd9Sstevel@tonic-gate 			(void) printf("\n%s:\n", s->scn_name);
9297c478bd9Sstevel@tonic-gate 			(void) printf("   <offset>  \tName\n");
9307c478bd9Sstevel@tonic-gate 		}
9317c478bd9Sstevel@tonic-gate 		section_size = 0;
9327c478bd9Sstevel@tonic-gate 		if ((strtab = (unsigned char *)
9337c478bd9Sstevel@tonic-gate 		    get_scndata(s->p_sd, &section_size)) == NULL) {
9347c478bd9Sstevel@tonic-gate 			continue;
9357c478bd9Sstevel@tonic-gate 		}
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 		if (section_size != 0) {
9387c478bd9Sstevel@tonic-gate 			(void) printf("   <%d>  \t", str_off);
9397c478bd9Sstevel@tonic-gate 			beg_of_string = 0;
9407c478bd9Sstevel@tonic-gate 			while (section_size--) {
9417c478bd9Sstevel@tonic-gate 				unsigned char c = *strtab++;
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 				if (beg_of_string) {
9447c478bd9Sstevel@tonic-gate 					(void) printf("   <%d>  \t", str_off);
9457c478bd9Sstevel@tonic-gate 					counter++;
9467c478bd9Sstevel@tonic-gate 					beg_of_string = 0;
9477c478bd9Sstevel@tonic-gate 				}
9487c478bd9Sstevel@tonic-gate 				str_off++;
9497c478bd9Sstevel@tonic-gate 				switch (c) {
9507c478bd9Sstevel@tonic-gate 				case '\0':
9517c478bd9Sstevel@tonic-gate 					(void) printf("\n");
9527c478bd9Sstevel@tonic-gate 					beg_of_string = 1;
9537c478bd9Sstevel@tonic-gate 					break;
9547c478bd9Sstevel@tonic-gate 				default:
9557c478bd9Sstevel@tonic-gate 					(void) putchar(c);
9567c478bd9Sstevel@tonic-gate 				}
9577c478bd9Sstevel@tonic-gate 			}
9587c478bd9Sstevel@tonic-gate 		}
9597c478bd9Sstevel@tonic-gate 	}
9607c478bd9Sstevel@tonic-gate 	(void) printf("\n");
9617c478bd9Sstevel@tonic-gate }
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate /*
9647c478bd9Sstevel@tonic-gate  * Print the symbol table.  This function does not print the contents
9657c478bd9Sstevel@tonic-gate  * of the symbol table but sets up the parameters and then calls
9667c478bd9Sstevel@tonic-gate  * print_symtab to print the symbols.  Calling another function to print
9677c478bd9Sstevel@tonic-gate  * the symbols allows both -T and -n to work correctly
9687c478bd9Sstevel@tonic-gate  * simultaneously.  Input is an opened ELF file, a pointer to the
9697c478bd9Sstevel@tonic-gate  * symbol table SCNTAB structure, and the filename.
9707c478bd9Sstevel@tonic-gate  * Set the range of symbols to print if T_flag, and set
9717c478bd9Sstevel@tonic-gate  * name of symbol to print if n_flag.
9727c478bd9Sstevel@tonic-gate  */
9737c478bd9Sstevel@tonic-gate static void
9747c478bd9Sstevel@tonic-gate dump_symbol_table(Elf *elf_file, SCNTAB *p_symtab, char *filename)
9757c478bd9Sstevel@tonic-gate {
9767c478bd9Sstevel@tonic-gate 	Elf_Data  *sym_data;
9777c478bd9Sstevel@tonic-gate 	GElf_Sym  T_range, n_range;	/* for use with -T and -n */
9787c478bd9Sstevel@tonic-gate 	size_t count = 0;
9797c478bd9Sstevel@tonic-gate 	size_t sym_size;
9807c478bd9Sstevel@tonic-gate 	int index = 1;
9817c478bd9Sstevel@tonic-gate 	int found_it = 0;
9827c478bd9Sstevel@tonic-gate 	int i;
9837c478bd9Sstevel@tonic-gate 	int adj = 0;			/*  field adjustment for elf64 */
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 	if (gelf_getclass(elf_file) == ELFCLASS64)
9865aefb655Srie 		adj = 8;
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 	if (p_symtab == NULL) {
9897c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
9907c478bd9Sstevel@tonic-gate 		"%s: %s: could not get symbol table\n", prog_name, filename);
9917c478bd9Sstevel@tonic-gate 		return;
9927c478bd9Sstevel@tonic-gate 	}
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 	/* get symbol table data */
9957c478bd9Sstevel@tonic-gate 	sym_data = NULL;
9967c478bd9Sstevel@tonic-gate 	sym_size = 0;
9977c478bd9Sstevel@tonic-gate 	if ((sym_data =
9987c478bd9Sstevel@tonic-gate 	    elf_getdata(p_symtab->p_sd, NULL)) == NULL) {
9997c478bd9Sstevel@tonic-gate 		(void) printf("\n%s:\n", p_symtab->scn_name);
10007c478bd9Sstevel@tonic-gate 		(void) printf("No symbol table data\n");
10017c478bd9Sstevel@tonic-gate 		return;
10027c478bd9Sstevel@tonic-gate 	}
10037c478bd9Sstevel@tonic-gate 	sym_size = sym_data->d_size;
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	count = sym_size / p_symtab->p_shdr.sh_entsize;
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 	if (n_flag && t_flag && !T_flag) {
10087c478bd9Sstevel@tonic-gate 		/* LINTED */
10097c478bd9Sstevel@tonic-gate 		for (i = 1; i < count; i++) {
10107c478bd9Sstevel@tonic-gate 			(void) gelf_getsym(sym_data, i, &n_range);
10117c478bd9Sstevel@tonic-gate 			if (strcmp(name, (char *)
10127c478bd9Sstevel@tonic-gate 			    elf_strptr(elf_file,
10137c478bd9Sstevel@tonic-gate 			    p_symtab->p_shdr.sh_link,
10147c478bd9Sstevel@tonic-gate 			    n_range.st_name)) != 0) {
10157c478bd9Sstevel@tonic-gate 				continue;
10167c478bd9Sstevel@tonic-gate 			} else {
10177c478bd9Sstevel@tonic-gate 				found_it = 1;
10187c478bd9Sstevel@tonic-gate 				if (!p_flag) {
10197c478bd9Sstevel@tonic-gate 					(void) printf(
10207c478bd9Sstevel@tonic-gate "\n              ***** SYMBOL TABLE INFORMATION *****\n");
10217c478bd9Sstevel@tonic-gate 					(void) printf(
10227c478bd9Sstevel@tonic-gate "[Index]  %-*s%-*sType\tBind\tOther\tShndx\tName",
10237c478bd9Sstevel@tonic-gate 					    12 + adj, "Value", 9 + adj, "Size");
10247c478bd9Sstevel@tonic-gate 				}
10257c478bd9Sstevel@tonic-gate 				(void) printf("\n%s:\n", p_symtab->scn_name);
10267c478bd9Sstevel@tonic-gate 				print_symtab(elf_file, p_symtab, sym_data,
10277c478bd9Sstevel@tonic-gate 				    1, i);
10287c478bd9Sstevel@tonic-gate 			}
10297c478bd9Sstevel@tonic-gate 		}   /* end for */
10307c478bd9Sstevel@tonic-gate 		if (!found_it) {
10317c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s: %s not found\n",
10327c478bd9Sstevel@tonic-gate 			    prog_name, filename, name);
10337c478bd9Sstevel@tonic-gate 		}
10347c478bd9Sstevel@tonic-gate 	} else if (T_flag) {
10357c478bd9Sstevel@tonic-gate 		T_num = check_range(T_low, T_hi, count, filename);
10367c478bd9Sstevel@tonic-gate 		if (T_num < 0)
10377c478bd9Sstevel@tonic-gate 			return;
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 		(void) gelf_getsym(sym_data, T_low-1, &T_range);
10407c478bd9Sstevel@tonic-gate 		index = T_low;
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 		if (!p_flag) {
10437c478bd9Sstevel@tonic-gate 			(void) printf(
10447c478bd9Sstevel@tonic-gate "\n              ***** SYMBOL TABLE INFORMATION *****\n");
10457c478bd9Sstevel@tonic-gate 			(void) printf(
10467c478bd9Sstevel@tonic-gate "[Index]  %-*s%-*sType\tBind\tOther\tShndx\tName",
10477c478bd9Sstevel@tonic-gate 			    12 + adj, "Value", 9 + adj, "Size");
10487c478bd9Sstevel@tonic-gate 		}
10497c478bd9Sstevel@tonic-gate 		(void) printf("\n%s:\n", p_symtab->scn_name);
10507c478bd9Sstevel@tonic-gate 		print_symtab(elf_file, p_symtab, sym_data, T_num, index);
10517c478bd9Sstevel@tonic-gate 	} else {
10527c478bd9Sstevel@tonic-gate 		if (!p_flag) {
10537c478bd9Sstevel@tonic-gate 			(void) printf(
10547c478bd9Sstevel@tonic-gate "\n              ***** SYMBOL TABLE INFORMATION *****\n");
10557c478bd9Sstevel@tonic-gate 			(void) printf(
10567c478bd9Sstevel@tonic-gate "[Index]  %-*s%-*sType\tBind\tOther\tShndx\tName",
10577c478bd9Sstevel@tonic-gate 			    12 + adj, "Value", 9 + adj, "Size");
10587c478bd9Sstevel@tonic-gate 		}
10597c478bd9Sstevel@tonic-gate 		(void) printf("\n%s:\n", p_symtab->scn_name);
10607c478bd9Sstevel@tonic-gate 		print_symtab(elf_file, p_symtab, sym_data, count-1, 1);
10617c478bd9Sstevel@tonic-gate 	}
10627c478bd9Sstevel@tonic-gate }
10637c478bd9Sstevel@tonic-gate 
10644899432aSab196087 
10654899432aSab196087 /*
10667c478bd9Sstevel@tonic-gate  * Print dynamic linking information.  Input is an ELF
10677c478bd9Sstevel@tonic-gate  * file descriptor, the SCNTAB structure, the number of
10687c478bd9Sstevel@tonic-gate  * sections, and the filename.
10697c478bd9Sstevel@tonic-gate  */
10707c478bd9Sstevel@tonic-gate static void
10717c478bd9Sstevel@tonic-gate dump_dynamic(Elf *elf_file, SCNTAB *p_scns, int num_scns, char *filename)
10727c478bd9Sstevel@tonic-gate {
1073ba4e3c84Sab196087 #define	pdyn_Fmtptr	"%#llx"
10744899432aSab196087 
10757c478bd9Sstevel@tonic-gate 	Elf_Data	*dyn_data;
10767c478bd9Sstevel@tonic-gate 	GElf_Dyn	p_dyn;
10777c478bd9Sstevel@tonic-gate 	GElf_Phdr	p_phdr;
10787c478bd9Sstevel@tonic-gate 	GElf_Ehdr	p_ehdr;
10797c478bd9Sstevel@tonic-gate 	int		index = 1;
10807c478bd9Sstevel@tonic-gate 	int		lib_scns = num_scns;
10817c478bd9Sstevel@tonic-gate 	SCNTAB		*l_scns = p_scns;
10827c478bd9Sstevel@tonic-gate 	int		header_num = 0;
1083ba4e3c84Sab196087 	const char	*str;
1084ba4e3c84Sab196087 
1085ba4e3c84Sab196087 	(void) gelf_getehdr(elf_file, &p_ehdr);
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	if (!p_flag)
10887c478bd9Sstevel@tonic-gate 		(void) printf("\n  **** DYNAMIC SECTION INFORMATION ****\n");
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	for (; num_scns > 0; num_scns--, p_scns++) {
10917c478bd9Sstevel@tonic-gate 		GElf_Word	link;
10927c478bd9Sstevel@tonic-gate 		int		ii;
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 		if (p_scns->p_shdr.sh_type != SHT_DYNAMIC)
10967c478bd9Sstevel@tonic-gate 			continue;
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 		if (!p_flag) {
10997c478bd9Sstevel@tonic-gate 			(void) printf("%s:\n", p_scns->scn_name);
11007c478bd9Sstevel@tonic-gate 			(void) printf("[INDEX]\tTag         Value\n");
11017c478bd9Sstevel@tonic-gate 		}
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 		if ((dyn_data = elf_getdata(p_scns->p_sd, NULL)) == 0) {
11047c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s: no data in "
11057c478bd9Sstevel@tonic-gate 			    "%s section\n", prog_name, filename,
11067c478bd9Sstevel@tonic-gate 			    p_scns->scn_name);
11077c478bd9Sstevel@tonic-gate 			return;
11087c478bd9Sstevel@tonic-gate 		}
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 		link = p_scns->p_shdr.sh_link;
11117c478bd9Sstevel@tonic-gate 		ii = 0;
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 		(void) gelf_getdyn(dyn_data, ii++, &p_dyn);
11147c478bd9Sstevel@tonic-gate 		while (p_dyn.d_tag != DT_NULL) {
1115de777a60Sab196087 			union {
1116de777a60Sab196087 				Conv_inv_buf_t		inv;
1117de777a60Sab196087 				Conv_dyn_flag_buf_t	dyn_flag;
1118de777a60Sab196087 				Conv_dyn_flag1_buf_t	dyn_flag1;
1119de777a60Sab196087 				Conv_dyn_feature1_buf_t	dyn_feature1;
1120de777a60Sab196087 				Conv_dyn_posflag1_buf_t	dyn_posflag1;
1121de777a60Sab196087 			} conv_buf;
1122de777a60Sab196087 
1123c13de8f6Sab196087 			(void) printf("[%d]\t%-15.15s ", index++,
11244f680cc6SAli Bahrami 			    conv_dyn_tag(p_dyn.d_tag,
11254f680cc6SAli Bahrami 			    p_ehdr.e_ident[EI_OSABI], p_ehdr.e_machine,
1126de777a60Sab196087 			    DUMP_CONVFMT, &conv_buf.inv));
11277c478bd9Sstevel@tonic-gate 
11284899432aSab196087 			/*
11294899432aSab196087 			 * It would be nice to use a table driven loop
11304899432aSab196087 			 * here, but the address space is too sparse
11314899432aSab196087 			 * and irregular. A switch is simple and robust.
11324899432aSab196087 			 */
11337c478bd9Sstevel@tonic-gate 			switch (p_dyn.d_tag) {
11347c478bd9Sstevel@tonic-gate 			/*
1135c13de8f6Sab196087 			 * Items with an address value
11367c478bd9Sstevel@tonic-gate 			 */
1137c13de8f6Sab196087 			case DT_PLTGOT:
1138c13de8f6Sab196087 			case DT_HASH:
1139c13de8f6Sab196087 			case DT_STRTAB:
1140c13de8f6Sab196087 			case DT_RELA:
1141c13de8f6Sab196087 			case DT_SYMTAB:
1142c13de8f6Sab196087 			case DT_INIT:
1143c13de8f6Sab196087 			case DT_FINI:
1144c13de8f6Sab196087 			case DT_REL:
1145c13de8f6Sab196087 			case DT_DEBUG:
1146c13de8f6Sab196087 			case DT_TEXTREL:
1147c13de8f6Sab196087 			case DT_JMPREL:
1148c13de8f6Sab196087 			case DT_INIT_ARRAY:
1149c13de8f6Sab196087 			case DT_FINI_ARRAY:
1150c13de8f6Sab196087 			case DT_INIT_ARRAYSZ:
1151c13de8f6Sab196087 			case DT_FINI_ARRAYSZ:
1152c13de8f6Sab196087 			case DT_PREINIT_ARRAY:
1153c13de8f6Sab196087 			case DT_PREINIT_ARRAYSZ:
1154c13de8f6Sab196087 			case DT_SUNW_RTLDINF:
1155c13de8f6Sab196087 			case DT_SUNW_CAP:
115608278a5eSRod Evans 			case DT_SUNW_CAPINFO:
115708278a5eSRod Evans 			case DT_SUNW_CAPCHAIN:
11589039eeafSab196087 			case DT_SUNW_SYMTAB:
1159d579eb63Sab196087 			case DT_SUNW_SYMSORT:
1160d579eb63Sab196087 			case DT_SUNW_TLSSORT:
1161c13de8f6Sab196087 			case DT_PLTPAD:
1162c13de8f6Sab196087 			case DT_MOVETAB:
1163c13de8f6Sab196087 			case DT_SYMINFO:
1164c13de8f6Sab196087 			case DT_RELACOUNT:
1165c13de8f6Sab196087 			case DT_RELCOUNT:
1166c13de8f6Sab196087 			case DT_VERSYM:
1167c13de8f6Sab196087 			case DT_VERDEF:
1168c13de8f6Sab196087 			case DT_VERDEFNUM:
1169c13de8f6Sab196087 			case DT_VERNEED:
1170c13de8f6Sab196087 				(void) printf(pdyn_Fmtptr,
1171c13de8f6Sab196087 				    EC_ADDR(p_dyn.d_un.d_ptr));
11727c478bd9Sstevel@tonic-gate 				break;
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 			/*
1175c13de8f6Sab196087 			 * Items with a string value
11767c478bd9Sstevel@tonic-gate 			 */
1177c13de8f6Sab196087 			case DT_NEEDED:
1178c13de8f6Sab196087 			case DT_SONAME:
1179c13de8f6Sab196087 			case DT_RPATH:
1180c13de8f6Sab196087 			case DT_RUNPATH:
1181c13de8f6Sab196087 			case DT_SUNW_AUXILIARY:
1182c13de8f6Sab196087 			case DT_SUNW_FILTER:
1183c13de8f6Sab196087 			case DT_CONFIG:
1184c13de8f6Sab196087 			case DT_DEPAUDIT:
1185c13de8f6Sab196087 			case DT_AUDIT:
1186c13de8f6Sab196087 			case DT_AUXILIARY:
1187c13de8f6Sab196087 			case DT_USED:
1188c13de8f6Sab196087 			case DT_FILTER:
1189c13de8f6Sab196087 				if (v_flag) {	/* Look up the string */
1190c13de8f6Sab196087 					str = (char *)elf_strptr(elf_file, link,
1191c13de8f6Sab196087 					    p_dyn.d_un.d_ptr);
1192c13de8f6Sab196087 					if (!(str && *str))
1193c13de8f6Sab196087 						str = (char *)UNKNOWN;
1194c13de8f6Sab196087 					(void) printf("%s", str);
1195c13de8f6Sab196087 				} else {	/* Show the address */
1196c13de8f6Sab196087 					(void) printf(pdyn_Fmtptr,
1197c13de8f6Sab196087 					    EC_ADDR(p_dyn.d_un.d_ptr));
1198c13de8f6Sab196087 				}
11997c478bd9Sstevel@tonic-gate 				break;
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate 			/*
1202c13de8f6Sab196087 			 * Items with a literal value
12037c478bd9Sstevel@tonic-gate 			 */
1204c13de8f6Sab196087 			case DT_PLTRELSZ:
1205c13de8f6Sab196087 			case DT_RELASZ:
1206c13de8f6Sab196087 			case DT_RELAENT:
1207c13de8f6Sab196087 			case DT_STRSZ:
1208c13de8f6Sab196087 			case DT_SYMENT:
1209c13de8f6Sab196087 			case DT_RELSZ:
1210c13de8f6Sab196087 			case DT_RELENT:
1211c13de8f6Sab196087 			case DT_PLTREL:
1212c13de8f6Sab196087 			case DT_BIND_NOW:
1213c13de8f6Sab196087 			case DT_CHECKSUM:
1214c13de8f6Sab196087 			case DT_PLTPADSZ:
1215c13de8f6Sab196087 			case DT_MOVEENT:
1216c13de8f6Sab196087 			case DT_MOVESZ:
1217c13de8f6Sab196087 			case DT_SYMINSZ:
1218c13de8f6Sab196087 			case DT_SYMINENT:
1219c13de8f6Sab196087 			case DT_VERNEEDNUM:
1220c13de8f6Sab196087 			case DT_SPARC_REGISTER:
12219039eeafSab196087 			case DT_SUNW_SYMSZ:
1222d579eb63Sab196087 			case DT_SUNW_SORTENT:
1223d579eb63Sab196087 			case DT_SUNW_SYMSORTSZ:
1224d579eb63Sab196087 			case DT_SUNW_TLSSORTSZ:
12253244bcaaSab196087 			case DT_SUNW_STRPAD:
122608278a5eSRod Evans 			case DT_SUNW_CAPCHAINENT:
122708278a5eSRod Evans 			case DT_SUNW_CAPCHAINSZ:
1228c13de8f6Sab196087 				(void) printf(pdyn_Fmtptr,
1229c13de8f6Sab196087 				    EC_XWORD(p_dyn.d_un.d_val));
12307c478bd9Sstevel@tonic-gate 				break;
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 			/*
1233ba2be530Sab196087 			 * Integer items that are bitmasks, or which
1234ba2be530Sab196087 			 * can be otherwise formatted in symbolic form.
12357c478bd9Sstevel@tonic-gate 			 */
1236c13de8f6Sab196087 			case DT_FLAGS:
1237ba4e3c84Sab196087 			case DT_FEATURE_1:
1238ba4e3c84Sab196087 			case DT_POSFLAG_1:
1239ba4e3c84Sab196087 			case DT_FLAGS_1:
1240ba2be530Sab196087 			case DT_SUNW_LDMACH:
1241ba4e3c84Sab196087 				str = NULL;
1242ba4e3c84Sab196087 				if (v_flag) {
1243ba4e3c84Sab196087 					switch (p_dyn.d_tag) {
1244ba4e3c84Sab196087 					case DT_FLAGS:
1245ba4e3c84Sab196087 						str = conv_dyn_flag(
1246ba4e3c84Sab196087 						    p_dyn.d_un.d_val,
1247de777a60Sab196087 						    DUMP_CONVFMT,
1248de777a60Sab196087 						    &conv_buf.dyn_flag);
12497c478bd9Sstevel@tonic-gate 						break;
1250c13de8f6Sab196087 					case DT_FEATURE_1:
1251ba4e3c84Sab196087 						str = conv_dyn_feature1(
1252ba4e3c84Sab196087 						    p_dyn.d_un.d_val,
1253de777a60Sab196087 						    DUMP_CONVFMT,
1254de777a60Sab196087 						    &conv_buf.dyn_feature1);
12557c478bd9Sstevel@tonic-gate 						break;
1256c13de8f6Sab196087 					case DT_POSFLAG_1:
1257ba4e3c84Sab196087 						str = conv_dyn_posflag1(
1258ba4e3c84Sab196087 						    p_dyn.d_un.d_val,
1259de777a60Sab196087 						    DUMP_CONVFMT,
1260de777a60Sab196087 						    &conv_buf.dyn_posflag1);
12617c478bd9Sstevel@tonic-gate 						break;
1262c13de8f6Sab196087 					case DT_FLAGS_1:
1263ba4e3c84Sab196087 						str = conv_dyn_flag1(
1264d29b2c44Sab196087 						    p_dyn.d_un.d_val, 0,
1265de777a60Sab196087 						    &conv_buf.dyn_flag1);
1266ba4e3c84Sab196087 						break;
1267ba2be530Sab196087 					case DT_SUNW_LDMACH:
1268ba2be530Sab196087 						str = conv_ehdr_mach(
1269ba2be530Sab196087 						    p_dyn.d_un.d_val, 0,
1270ba2be530Sab196087 						    &conv_buf.inv);
1271ba2be530Sab196087 						break;
1272ba4e3c84Sab196087 					}
1273ba4e3c84Sab196087 				}
1274ba4e3c84Sab196087 				if (str) {	/* Show as string */
1275ba4e3c84Sab196087 					(void) printf("%s", str);
1276ba4e3c84Sab196087 				} else {	/* Numeric form */
1277ba4e3c84Sab196087 					(void) printf(pdyn_Fmtptr,
1278ba4e3c84Sab196087 					    EC_ADDR(p_dyn.d_un.d_ptr));
1279ba4e3c84Sab196087 				}
12807c478bd9Sstevel@tonic-gate 				break;
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 			/*
1283c13de8f6Sab196087 			 * Depreciated items with a literal value
12847c478bd9Sstevel@tonic-gate 			 */
1285c13de8f6Sab196087 			case DT_DEPRECATED_SPARC_REGISTER:
1286c13de8f6Sab196087 				(void) printf(pdyn_Fmtptr
1287c13de8f6Sab196087 				    "  (deprecated value)",
1288c13de8f6Sab196087 				    EC_XWORD(p_dyn.d_un.d_val));
12897c478bd9Sstevel@tonic-gate 				break;
1290c13de8f6Sab196087 
1291c13de8f6Sab196087 			/* Ignored items */
1292c13de8f6Sab196087 			case DT_SYMBOLIC:
1293c13de8f6Sab196087 				(void) printf("(ignored)");
12947c478bd9Sstevel@tonic-gate 				break;
12957c478bd9Sstevel@tonic-gate 			}
12967c478bd9Sstevel@tonic-gate 			(void) printf("\n");
12977c478bd9Sstevel@tonic-gate 			(void) gelf_getdyn(dyn_data, ii++, &p_dyn);
12987c478bd9Sstevel@tonic-gate 		}
12997c478bd9Sstevel@tonic-gate 	}
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate 	/*
13027c478bd9Sstevel@tonic-gate 	 * Check for existence of static shared library information.
13037c478bd9Sstevel@tonic-gate 	 */
13047c478bd9Sstevel@tonic-gate 	while (header_num < p_ehdr.e_phnum) {
13057c478bd9Sstevel@tonic-gate 		(void) gelf_getphdr(elf_file, header_num, &p_phdr);
13067c478bd9Sstevel@tonic-gate 		if (p_phdr.p_type == PT_SHLIB) {
13077c478bd9Sstevel@tonic-gate 			while (--lib_scns > 0) {
13087c478bd9Sstevel@tonic-gate 				if (strcmp(l_scns->scn_name, ".lib") == 0) {
13097c478bd9Sstevel@tonic-gate 					print_static(l_scns, filename);
13107c478bd9Sstevel@tonic-gate 				}
13117c478bd9Sstevel@tonic-gate 				l_scns++;
13127c478bd9Sstevel@tonic-gate 			}
13137c478bd9Sstevel@tonic-gate 		}
13147c478bd9Sstevel@tonic-gate 		header_num++;
13157c478bd9Sstevel@tonic-gate 	}
1316ba4e3c84Sab196087 #undef	pdyn_Fmtptr
13177c478bd9Sstevel@tonic-gate }
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate /*
13207c478bd9Sstevel@tonic-gate  * Print the ELF header.  Input is an ELF file descriptor
13217c478bd9Sstevel@tonic-gate  * and the filename.  If f_flag is set, the ELF header is
13227c478bd9Sstevel@tonic-gate  * printed to stdout, otherwise the function returns after
13237c478bd9Sstevel@tonic-gate  * setting the pointer to the ELF header.  Any values which
13247c478bd9Sstevel@tonic-gate  * are not known are printed in decimal.  Fields must be updated
13257c478bd9Sstevel@tonic-gate  * as new values are added.
13267c478bd9Sstevel@tonic-gate  */
13277c478bd9Sstevel@tonic-gate static GElf_Ehdr *
13287c478bd9Sstevel@tonic-gate dump_elf_header(Elf *elf_file, char *filename, GElf_Ehdr * elf_head_p)
13297c478bd9Sstevel@tonic-gate {
1330c13de8f6Sab196087 	int class;
13317c478bd9Sstevel@tonic-gate 	int field;
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 	if (gelf_getehdr(elf_file, elf_head_p) == NULL) {
13345aefb655Srie 		(void) fprintf(stderr, "%s: %s: %s\n", prog_name, filename,
13355aefb655Srie 		    elf_errmsg(-1));
13367c478bd9Sstevel@tonic-gate 		return (NULL);
13377c478bd9Sstevel@tonic-gate 	}
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate 	class = (int)elf_head_p->e_ident[4];
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate 	if (class == ELFCLASS64)
13425aefb655Srie 		field = 21;
13437c478bd9Sstevel@tonic-gate 	else
13447c478bd9Sstevel@tonic-gate 		field = 13;
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 	if (!f_flag)
13477c478bd9Sstevel@tonic-gate 		return (elf_head_p);
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate 	if (!p_flag) {
13505aefb655Srie 		(void) printf("\n                    **** ELF HEADER ****\n");
13517c478bd9Sstevel@tonic-gate 		(void) printf("%-*s%-11s%-*sMachine     Version\n",
13527c478bd9Sstevel@tonic-gate 		    field, "Class", "Data", field, "Type");
13537c478bd9Sstevel@tonic-gate 		(void) printf("%-*s%-11s%-*sFlags       Ehsize\n",
13547c478bd9Sstevel@tonic-gate 		    field, "Entry", "Phoff", field, "Shoff");
13557c478bd9Sstevel@tonic-gate 		(void) printf("%-*s%-11s%-*sShnum       Shstrndx\n\n",
13567c478bd9Sstevel@tonic-gate 		    field, "Phentsize", "Phnum", field, "Shentsz");
13577c478bd9Sstevel@tonic-gate 	}
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 	if (!v_flag) {
13607c478bd9Sstevel@tonic-gate 		(void) printf("%-*d%-11d%-*d%-12d%d\n",
1361de777a60Sab196087 		    field, elf_head_p->e_ident[4], elf_head_p->e_ident[5],
1362de777a60Sab196087 		    field, (int)elf_head_p->e_type, (int)elf_head_p->e_machine,
13637c478bd9Sstevel@tonic-gate 		    elf_head_p->e_version);
13647c478bd9Sstevel@tonic-gate 	} else {
1365de777a60Sab196087 		Conv_inv_buf_t	inv_buf;
1366de777a60Sab196087 
1367c13de8f6Sab196087 		(void) printf("%-*s", field,
1368de777a60Sab196087 		    conv_ehdr_class(class, DUMP_CONVFMT, &inv_buf));
1369c13de8f6Sab196087 		(void) printf("%-11s",
1370de777a60Sab196087 		    conv_ehdr_data(elf_head_p->e_ident[5], DUMP_CONVFMT,
1371de777a60Sab196087 		    &inv_buf));
1372c13de8f6Sab196087 		(void) printf("%-*s", field,
13734f680cc6SAli Bahrami 		    conv_ehdr_type(elf_head_p->e_ident[EI_OSABI],
13744f680cc6SAli Bahrami 		    elf_head_p->e_type, DUMP_CONVFMT, &inv_buf));
1375c13de8f6Sab196087 		(void) printf("%-12s",
1376de777a60Sab196087 		    conv_ehdr_mach(elf_head_p->e_machine, DUMP_CONVFMT,
1377de777a60Sab196087 		    &inv_buf));
1378c13de8f6Sab196087 		(void) printf("%s\n",
1379de777a60Sab196087 		    conv_ehdr_vers(elf_head_p->e_version, DUMP_CONVFMT,
1380de777a60Sab196087 		    &inv_buf));
13817c478bd9Sstevel@tonic-gate 	}
13827c478bd9Sstevel@tonic-gate 	(void) printf("%-#*llx%-#11llx%-#*llx%-#12x%#x\n",
1383de777a60Sab196087 	    field, EC_ADDR(elf_head_p->e_entry), EC_OFF(elf_head_p->e_phoff),
1384de777a60Sab196087 	    field, EC_OFF(elf_head_p->e_shoff), EC_WORD(elf_head_p->e_flags),
13857c478bd9Sstevel@tonic-gate 	    EC_WORD(elf_head_p->e_ehsize));
13867c478bd9Sstevel@tonic-gate 	if (!v_flag || (elf_head_p->e_shstrndx != SHN_XINDEX)) {
13877c478bd9Sstevel@tonic-gate 		(void) printf("%-#*x%-11u%-#*x%-12u%u\n",
13887c478bd9Sstevel@tonic-gate 		    field, EC_WORD(elf_head_p->e_phentsize),
13897c478bd9Sstevel@tonic-gate 		    EC_WORD(elf_head_p->e_phnum),
13907c478bd9Sstevel@tonic-gate 		    field, EC_WORD(elf_head_p->e_shentsize),
13917c478bd9Sstevel@tonic-gate 		    EC_WORD(elf_head_p->e_shnum),
13927c478bd9Sstevel@tonic-gate 		    EC_WORD(elf_head_p->e_shstrndx));
13937c478bd9Sstevel@tonic-gate 	} else {
13947c478bd9Sstevel@tonic-gate 		(void) printf("%-#*x%-11u%-#*x%-12uXINDEX\n",
13957c478bd9Sstevel@tonic-gate 		    field, EC_WORD(elf_head_p->e_phentsize),
13967c478bd9Sstevel@tonic-gate 		    EC_WORD(elf_head_p->e_phnum),
13977c478bd9Sstevel@tonic-gate 		    field, EC_WORD(elf_head_p->e_shentsize),
13987c478bd9Sstevel@tonic-gate 		    EC_WORD(elf_head_p->e_shnum));
13997c478bd9Sstevel@tonic-gate 	}
14007c478bd9Sstevel@tonic-gate 	if ((elf_head_p->e_shnum == 0) && (elf_head_p->e_shoff > 0)) {
14017c478bd9Sstevel@tonic-gate 		Elf_Scn		*scn;
14027c478bd9Sstevel@tonic-gate 		GElf_Shdr	shdr0;
14037c478bd9Sstevel@tonic-gate 		int		field;
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 		if (gelf_getclass(elf_file) == ELFCLASS64)
14065aefb655Srie 			field = 21;
14077c478bd9Sstevel@tonic-gate 		else
14085aefb655Srie 			field = 13;
14097c478bd9Sstevel@tonic-gate 		if (!p_flag) {
14107c478bd9Sstevel@tonic-gate 			(void) printf("\n	   **** SECTION HEADER[0] "
14117c478bd9Sstevel@tonic-gate 			    "{Elf Extensions} ****\n");
14127c478bd9Sstevel@tonic-gate 			(void) printf(
14137c478bd9Sstevel@tonic-gate 			    "[No]\tType\tFlags\t%-*s %-*s%-*s%sName\n",
14147c478bd9Sstevel@tonic-gate 			    field, "Addr", field, "Offset", field,
14157c478bd9Sstevel@tonic-gate 			    "Size(shnum)",
14167c478bd9Sstevel@tonic-gate 			    /* compatibility:  tab for elf32 */
14175aefb655Srie 			    (field == 13) ? "\t" : "  ");
14187c478bd9Sstevel@tonic-gate 			(void) printf("\tLn(strndx) Info\t%-*s Entsize\n",
14197c478bd9Sstevel@tonic-gate 			    field, "Adralgn");
14207c478bd9Sstevel@tonic-gate 		}
14217c478bd9Sstevel@tonic-gate 		if ((scn = elf_getscn(elf_file, 0)) == NULL) {
14227c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
14237c478bd9Sstevel@tonic-gate 			    "%s: %s: elf_getscn failed: %s\n",
14247c478bd9Sstevel@tonic-gate 			    prog_name, filename, elf_errmsg(-1));
14257c478bd9Sstevel@tonic-gate 			return (NULL);
14267c478bd9Sstevel@tonic-gate 		}
14277c478bd9Sstevel@tonic-gate 		if (gelf_getshdr(scn, &shdr0) == 0) {
14287c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
14297c478bd9Sstevel@tonic-gate 			    "%s: %s: gelf_getshdr: %s\n",
14307c478bd9Sstevel@tonic-gate 			    prog_name, filename, elf_errmsg(-1));
14317c478bd9Sstevel@tonic-gate 			return (NULL);
14327c478bd9Sstevel@tonic-gate 		}
14337c478bd9Sstevel@tonic-gate 		(void) printf("[0]\t%u\t%llu\t", EC_WORD(shdr0.sh_type),
14347c478bd9Sstevel@tonic-gate 		    EC_XWORD(shdr0.sh_flags));
14357c478bd9Sstevel@tonic-gate 
1436ba4e3c84Sab196087 		(void) printf("%-#*llx %-#*llx%-*llu%s%-*u\n",
14377c478bd9Sstevel@tonic-gate 		    field, EC_ADDR(shdr0.sh_addr),
14387c478bd9Sstevel@tonic-gate 		    field, EC_OFF(shdr0.sh_offset),
14397c478bd9Sstevel@tonic-gate 		    field, EC_XWORD(shdr0.sh_size),
14407c478bd9Sstevel@tonic-gate 		    /* compatibility:  tab for elf32 */
14415aefb655Srie 		    ((field == 13) ? "\t" : "  "),
14427c478bd9Sstevel@tonic-gate 		    field, EC_WORD(shdr0.sh_name));
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate 		(void) printf("\t%u\t%u\t%-#*llx %-#*llx\n",
14457c478bd9Sstevel@tonic-gate 		    EC_WORD(shdr0.sh_link),
14467c478bd9Sstevel@tonic-gate 		    EC_WORD(shdr0.sh_info),
14477c478bd9Sstevel@tonic-gate 		    field, EC_XWORD(shdr0.sh_addralign),
14487c478bd9Sstevel@tonic-gate 		    field, EC_XWORD(shdr0.sh_entsize));
14497c478bd9Sstevel@tonic-gate 	}
14507c478bd9Sstevel@tonic-gate 	(void) printf("\n");
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate 	return (elf_head_p);
14537c478bd9Sstevel@tonic-gate }
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate /*
14567c478bd9Sstevel@tonic-gate  * Print section contents.  Input is an ELF file descriptor,
14577c478bd9Sstevel@tonic-gate  * the ELF header, the SCNTAB structure,
14587c478bd9Sstevel@tonic-gate  * the number of symbols, and the filename.
14597c478bd9Sstevel@tonic-gate  * The number of sections,
14607c478bd9Sstevel@tonic-gate  * and the offset into the SCNTAB structure will be
14617c478bd9Sstevel@tonic-gate  * set in dump_section if d_flag or n_flag are set.
14627c478bd9Sstevel@tonic-gate  * If v_flag is set, sections which can be interpreted will
14637c478bd9Sstevel@tonic-gate  * be interpreted, otherwise raw data will be output in hexidecimal.
14647c478bd9Sstevel@tonic-gate  */
14657c478bd9Sstevel@tonic-gate static void
14667c478bd9Sstevel@tonic-gate print_section(Elf *elf_file,
14677c478bd9Sstevel@tonic-gate 	GElf_Ehdr *p_ehdr, SCNTAB *p, int num_scns, char *filename)
14687c478bd9Sstevel@tonic-gate {
14697c478bd9Sstevel@tonic-gate 	unsigned char    *p_sec;
14707c478bd9Sstevel@tonic-gate 	int	i;
14717c478bd9Sstevel@tonic-gate 	size_t	size;
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_scns; i++, p++) {
14747c478bd9Sstevel@tonic-gate 		GElf_Shdr shdr;
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 		size = 0;
14777c478bd9Sstevel@tonic-gate 		if (s_flag && !v_flag)
14787c478bd9Sstevel@tonic-gate 			p_sec = (unsigned char *)get_rawscn(p->p_sd, &size);
14797c478bd9Sstevel@tonic-gate 		else
14807c478bd9Sstevel@tonic-gate 			p_sec = (unsigned char *)get_scndata(p->p_sd, &size);
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate 		if ((gelf_getshdr(p->p_sd, &shdr) != NULL) &&
14837c478bd9Sstevel@tonic-gate 		    (shdr.sh_type == SHT_NOBITS)) {
14847c478bd9Sstevel@tonic-gate 			continue;
14857c478bd9Sstevel@tonic-gate 		}
14867c478bd9Sstevel@tonic-gate 		if (s_flag && !v_flag) {
14877c478bd9Sstevel@tonic-gate 			(void) printf("\n%s:\n", p->scn_name);
14887c478bd9Sstevel@tonic-gate 			print_rawdata(p_sec, size);
14897c478bd9Sstevel@tonic-gate 			continue;
14907c478bd9Sstevel@tonic-gate 		}
14917c478bd9Sstevel@tonic-gate 		if (shdr.sh_type == SHT_SYMTAB) {
14927c478bd9Sstevel@tonic-gate 			dump_symbol_table(elf_file, p, filename);
14937c478bd9Sstevel@tonic-gate 			continue;
14947c478bd9Sstevel@tonic-gate 		}
14957c478bd9Sstevel@tonic-gate 		if (shdr.sh_type == SHT_DYNSYM) {
14967c478bd9Sstevel@tonic-gate 			dump_symbol_table(elf_file, p, filename);
14977c478bd9Sstevel@tonic-gate 			continue;
14987c478bd9Sstevel@tonic-gate 		}
14997c478bd9Sstevel@tonic-gate 		if (shdr.sh_type == SHT_STRTAB) {
15007c478bd9Sstevel@tonic-gate 			dump_string_table(p, 1);
15017c478bd9Sstevel@tonic-gate 			continue;
15027c478bd9Sstevel@tonic-gate 		}
15037c478bd9Sstevel@tonic-gate 		if (shdr.sh_type == SHT_RELA) {
15047c478bd9Sstevel@tonic-gate 			dump_reloc_table(elf_file, p_ehdr, p, 1, filename);
15057c478bd9Sstevel@tonic-gate 			continue;
15067c478bd9Sstevel@tonic-gate 		}
15077c478bd9Sstevel@tonic-gate 		if (shdr.sh_type == SHT_REL) {
15087c478bd9Sstevel@tonic-gate 			dump_reloc_table(elf_file, p_ehdr, p, 1, filename);
15097c478bd9Sstevel@tonic-gate 			continue;
15107c478bd9Sstevel@tonic-gate 		}
15117c478bd9Sstevel@tonic-gate 		if (shdr.sh_type == SHT_DYNAMIC) {
15127c478bd9Sstevel@tonic-gate 			dump_dynamic(elf_file, p, 1, filename);
15137c478bd9Sstevel@tonic-gate 			continue;
15147c478bd9Sstevel@tonic-gate 		}
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 		(void) printf("\n%s:\n", p->scn_name);
15177c478bd9Sstevel@tonic-gate 		print_rawdata(p_sec, size);
15187c478bd9Sstevel@tonic-gate 	}
15197c478bd9Sstevel@tonic-gate 	(void) printf("\n");
15207c478bd9Sstevel@tonic-gate }
15217c478bd9Sstevel@tonic-gate 
15227c478bd9Sstevel@tonic-gate /*
15237c478bd9Sstevel@tonic-gate  * Print section contents. This function does not print the contents
15247c478bd9Sstevel@tonic-gate  * of the sections but sets up the parameters and then calls
15257c478bd9Sstevel@tonic-gate  * print_section to print the contents.  Calling another function to print
15267c478bd9Sstevel@tonic-gate  * the contents allows both -d and -n to work correctly
15277c478bd9Sstevel@tonic-gate  * simultaneously. Input is an ELF file descriptor, the ELF header,
15287c478bd9Sstevel@tonic-gate  * the SCNTAB structure, the number of sections, and the filename.
15297c478bd9Sstevel@tonic-gate  * Set the range of sections if d_flag, and set section name if
15307c478bd9Sstevel@tonic-gate  * n_flag.
15317c478bd9Sstevel@tonic-gate  */
15327c478bd9Sstevel@tonic-gate static void
15337c478bd9Sstevel@tonic-gate dump_section(Elf *elf_file,
15347c478bd9Sstevel@tonic-gate 	GElf_Ehdr *p_ehdr, SCNTAB *s, int num_scns, char *filename)
15357c478bd9Sstevel@tonic-gate {
15367c478bd9Sstevel@tonic-gate 	SCNTAB *n_range, *d_range; /* for use with -n and -d modifiers */
15377c478bd9Sstevel@tonic-gate 	int i;
15387c478bd9Sstevel@tonic-gate 	int found_it = 0;  /* for use with -n section_name */
15397c478bd9Sstevel@tonic-gate 
15407c478bd9Sstevel@tonic-gate 	if (n_flag) {
15417c478bd9Sstevel@tonic-gate 		n_range = s;
15427c478bd9Sstevel@tonic-gate 
15437c478bd9Sstevel@tonic-gate 		for (i = 0; i < num_scns; i++, n_range++) {
15447c478bd9Sstevel@tonic-gate 			if ((strcmp(name, n_range->scn_name)) != 0)
15457c478bd9Sstevel@tonic-gate 				continue;
15467c478bd9Sstevel@tonic-gate 			else {
15477c478bd9Sstevel@tonic-gate 				found_it = 1;
15487c478bd9Sstevel@tonic-gate 				print_section(elf_file, p_ehdr,
15497c478bd9Sstevel@tonic-gate 				    n_range, 1, filename);
15507c478bd9Sstevel@tonic-gate 			}
15517c478bd9Sstevel@tonic-gate 		}
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate 		if (!found_it) {
15547c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s: %s not found\n",
15557c478bd9Sstevel@tonic-gate 			    prog_name, filename, name);
15567c478bd9Sstevel@tonic-gate 		}
15577c478bd9Sstevel@tonic-gate 	} /* end n_flag */
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 	if (d_flag) {
15607c478bd9Sstevel@tonic-gate 		d_range = s;
15617c478bd9Sstevel@tonic-gate 		d_num = check_range(d_low, d_hi, num_scns, filename);
15627c478bd9Sstevel@tonic-gate 		if (d_num < 0)
15637c478bd9Sstevel@tonic-gate 			return;
15647c478bd9Sstevel@tonic-gate 		d_range += d_low - 1;
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 		print_section(elf_file, p_ehdr, d_range, d_num, filename);
15677c478bd9Sstevel@tonic-gate 	}	/* end d_flag */
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate 	if (!n_flag && !d_flag)
15707c478bd9Sstevel@tonic-gate 		print_section(elf_file, p_ehdr, s, num_scns, filename);
15717c478bd9Sstevel@tonic-gate }
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate /*
15747c478bd9Sstevel@tonic-gate  * Print the section header table. This function does not print the contents
15757c478bd9Sstevel@tonic-gate  * of the section headers but sets up the parameters and then calls
15767c478bd9Sstevel@tonic-gate  * print_shdr to print the contents.  Calling another function to print
15777c478bd9Sstevel@tonic-gate  * the contents allows both -d and -n to work correctly
15787c478bd9Sstevel@tonic-gate  * simultaneously.  Input is the SCNTAB structure,
15797c478bd9Sstevel@tonic-gate  * the number of sections from the ELF header, and the filename.
15807c478bd9Sstevel@tonic-gate  * Set the range of section headers to print if d_flag, and set
15817c478bd9Sstevel@tonic-gate  * name of section header to print if n_flag.
15827c478bd9Sstevel@tonic-gate  */
15837c478bd9Sstevel@tonic-gate static void
15847c478bd9Sstevel@tonic-gate dump_shdr(Elf *elf_file, SCNTAB *s, int num_scns, char *filename)
15857c478bd9Sstevel@tonic-gate {
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 	SCNTAB *n_range, *d_range;	/* for use with -n and -d modifiers */
15887c478bd9Sstevel@tonic-gate 	int field;
15897c478bd9Sstevel@tonic-gate 	int i;
15907c478bd9Sstevel@tonic-gate 	int found_it = 0;  /* for use with -n section_name */
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 	if (gelf_getclass(elf_file) == ELFCLASS64)
15935aefb655Srie 		field = 21;
15947c478bd9Sstevel@tonic-gate 	else
15955aefb655Srie 		field = 13;
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate 	if (!p_flag) {
15987c478bd9Sstevel@tonic-gate 		(void) printf("\n	   **** SECTION HEADER TABLE ****\n");
15997c478bd9Sstevel@tonic-gate 		(void) printf("[No]\tType\tFlags\t%-*s %-*s %-*s%sName\n",
16007c478bd9Sstevel@tonic-gate 		    field, "Addr", field, "Offset", field, "Size",
16017c478bd9Sstevel@tonic-gate 		    /* compatibility:  tab for elf32 */
16025aefb655Srie 		    (field == 13) ? "\t" : "  ");
16037c478bd9Sstevel@tonic-gate 		(void) printf("\tLink\tInfo\t%-*s Entsize\n\n",
16047c478bd9Sstevel@tonic-gate 		    field, "Adralgn");
16057c478bd9Sstevel@tonic-gate 	}
16067c478bd9Sstevel@tonic-gate 
16077c478bd9Sstevel@tonic-gate 	if (n_flag) {
16087c478bd9Sstevel@tonic-gate 		n_range = s;
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate 		for (i = 1; i <= num_scns; i++, n_range++) {
16117c478bd9Sstevel@tonic-gate 			if ((strcmp(name, n_range->scn_name)) != 0)
16127c478bd9Sstevel@tonic-gate 				continue;
16137c478bd9Sstevel@tonic-gate 			else {
16147c478bd9Sstevel@tonic-gate 				found_it = 1;
16157c478bd9Sstevel@tonic-gate 				print_shdr(elf_file, n_range, 1, i);
16167c478bd9Sstevel@tonic-gate 			}
16177c478bd9Sstevel@tonic-gate 		}
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 		if (!found_it) {
16207c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s: %s not found\n",
16217c478bd9Sstevel@tonic-gate 			    prog_name, filename, name);
16227c478bd9Sstevel@tonic-gate 		}
16237c478bd9Sstevel@tonic-gate 	} /* end n_flag */
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate 	if (d_flag) {
16267c478bd9Sstevel@tonic-gate 		d_range = s;
16277c478bd9Sstevel@tonic-gate 		d_num = check_range(d_low, d_hi, num_scns, filename);
16287c478bd9Sstevel@tonic-gate 		if (d_num < 0)
16297c478bd9Sstevel@tonic-gate 			return;
16307c478bd9Sstevel@tonic-gate 		d_range += d_low - 1;
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate 		print_shdr(elf_file, d_range, d_num, d_low);
16337c478bd9Sstevel@tonic-gate 	}	/* end d_flag */
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate 	if (!n_flag && !d_flag)
16367c478bd9Sstevel@tonic-gate 		print_shdr(elf_file, s, num_scns, 1);
16377c478bd9Sstevel@tonic-gate }
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate /*
16407c478bd9Sstevel@tonic-gate  * Process all of the command line options (except
16417c478bd9Sstevel@tonic-gate  * for -a, -g, -f, and -o).  All of the options processed
16427c478bd9Sstevel@tonic-gate  * by this function require the presence of the section
16437c478bd9Sstevel@tonic-gate  * header table and will not be processed if it is not present.
16447c478bd9Sstevel@tonic-gate  * Set up a buffer containing section name, section header,
16457c478bd9Sstevel@tonic-gate  * and section descriptor for each section in the file.  This
16467c478bd9Sstevel@tonic-gate  * structure is used to avoid duplicate calls to libelf functions.
16477c478bd9Sstevel@tonic-gate  * Structure members for the symbol table, the debugging information,
16487c478bd9Sstevel@tonic-gate  * and the line number information are global.  All of the
16497c478bd9Sstevel@tonic-gate  * rest are local.
16507c478bd9Sstevel@tonic-gate  */
16517c478bd9Sstevel@tonic-gate static void
16527c478bd9Sstevel@tonic-gate dump_section_table(Elf *elf_file, GElf_Ehdr *elf_head_p, char *filename)
16537c478bd9Sstevel@tonic-gate {
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 	static SCNTAB	*buffer, *p_scns;
16567c478bd9Sstevel@tonic-gate 	Elf_Scn		*scn = 0;
16577c478bd9Sstevel@tonic-gate 	char		*s_name = NULL;
16587c478bd9Sstevel@tonic-gate 	int		found = 0;
16597c478bd9Sstevel@tonic-gate 	unsigned int	num_scns;
16607c478bd9Sstevel@tonic-gate 	size_t		shstrndx;
16617c478bd9Sstevel@tonic-gate 	size_t		shnum;
16627c478bd9Sstevel@tonic-gate 
16637c478bd9Sstevel@tonic-gate 
166462b628a6SAli Bahrami 	if (elf_getshdrnum(elf_file, &shnum) == -1) {
16657c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
166662b628a6SAli Bahrami 		    "%s: %s: elf_getshdrnum failed: %s\n",
16677c478bd9Sstevel@tonic-gate 		    prog_name, filename, elf_errmsg(-1));
16687c478bd9Sstevel@tonic-gate 		return;
16697c478bd9Sstevel@tonic-gate 	}
167062b628a6SAli Bahrami 	if (elf_getshdrstrndx(elf_file, &shstrndx) == -1) {
16717c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
167262b628a6SAli Bahrami 		    "%s: %s: elf_getshdrstrndx failed: %s\n",
16737c478bd9Sstevel@tonic-gate 		    prog_name, filename, elf_errmsg(-1));
16747c478bd9Sstevel@tonic-gate 		return;
16757c478bd9Sstevel@tonic-gate 	}
16767c478bd9Sstevel@tonic-gate 
16777c478bd9Sstevel@tonic-gate 	if ((buffer = calloc(shnum, sizeof (SCNTAB))) == NULL) {
16787c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: cannot calloc space\n",
16797c478bd9Sstevel@tonic-gate 		    prog_name, filename);
16807c478bd9Sstevel@tonic-gate 		return;
16817c478bd9Sstevel@tonic-gate 	}
16827c478bd9Sstevel@tonic-gate 	/* LINTED */
16837c478bd9Sstevel@tonic-gate 	num_scns = (int)shnum - 1;
16847c478bd9Sstevel@tonic-gate 
16857c478bd9Sstevel@tonic-gate 	p_symtab = (SCNTAB *)0;
16867c478bd9Sstevel@tonic-gate 	p_dynsym = (SCNTAB *)0;
16877c478bd9Sstevel@tonic-gate 	p_scns = buffer;
16887c478bd9Sstevel@tonic-gate 	p_head_scns = buffer;
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate 	while ((scn = elf_nextscn(elf_file, scn)) != 0) {
16917c478bd9Sstevel@tonic-gate 		if ((gelf_getshdr(scn, &buffer->p_shdr)) == 0) {
16927c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1693de777a60Sab196087 			    "%s: %s: %s\n", prog_name, filename,
1694de777a60Sab196087 			    elf_errmsg(-1));
16957c478bd9Sstevel@tonic-gate 			return;
16967c478bd9Sstevel@tonic-gate 		}
1697de777a60Sab196087 		s_name = (char *)
1698de777a60Sab196087 		    elf_strptr(elf_file, shstrndx, buffer->p_shdr.sh_name);
16997c478bd9Sstevel@tonic-gate 		buffer->scn_name = s_name ? s_name : (char *)UNKNOWN;
17007c478bd9Sstevel@tonic-gate 		buffer->p_sd   =  scn;
17017c478bd9Sstevel@tonic-gate 
17027c478bd9Sstevel@tonic-gate 		if (buffer->p_shdr.sh_type == SHT_SYMTAB) {
17037c478bd9Sstevel@tonic-gate 			found += 1;
17047c478bd9Sstevel@tonic-gate 			p_symtab = buffer;
17057c478bd9Sstevel@tonic-gate 		}
17067c478bd9Sstevel@tonic-gate 		if (buffer->p_shdr.sh_type == SHT_DYNSYM)
17077c478bd9Sstevel@tonic-gate 			p_dynsym = buffer;
17087c478bd9Sstevel@tonic-gate 		buffer++;
17097c478bd9Sstevel@tonic-gate 	}
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate 	/*
17127c478bd9Sstevel@tonic-gate 	 * These functions depend upon the presence of the section header table
17137c478bd9Sstevel@tonic-gate 	 * and will not be invoked in its absence
17147c478bd9Sstevel@tonic-gate 	 */
17157c478bd9Sstevel@tonic-gate 	if (h_flag) {
17167c478bd9Sstevel@tonic-gate 		dump_shdr(elf_file, p_scns, num_scns, filename);
17177c478bd9Sstevel@tonic-gate 	}
17187c478bd9Sstevel@tonic-gate 	if (p_symtab && (t_flag || T_flag)) {
17197c478bd9Sstevel@tonic-gate 		dump_symbol_table(elf_file, p_symtab, filename);
17207c478bd9Sstevel@tonic-gate 	}
17217c478bd9Sstevel@tonic-gate 	if (c_flag) {
17227c478bd9Sstevel@tonic-gate 		dump_string_table(p_scns, num_scns);
17237c478bd9Sstevel@tonic-gate 	}
17247c478bd9Sstevel@tonic-gate 	if (r_flag) {
17257c478bd9Sstevel@tonic-gate 		dump_reloc_table(elf_file, elf_head_p,
17267c478bd9Sstevel@tonic-gate 		    p_scns, num_scns, filename);
17277c478bd9Sstevel@tonic-gate 	}
17287c478bd9Sstevel@tonic-gate 	if (L_flag) {
17297c478bd9Sstevel@tonic-gate 		dump_dynamic(elf_file, p_scns, num_scns, filename);
17307c478bd9Sstevel@tonic-gate 	}
17317c478bd9Sstevel@tonic-gate 	if (s_flag) {
17327c478bd9Sstevel@tonic-gate 		dump_section(elf_file, elf_head_p, p_scns,
17337c478bd9Sstevel@tonic-gate 		    num_scns, filename);
17347c478bd9Sstevel@tonic-gate 	}
17357c478bd9Sstevel@tonic-gate }
17367c478bd9Sstevel@tonic-gate 
17377c478bd9Sstevel@tonic-gate /*
17387c478bd9Sstevel@tonic-gate  * Load the archive string table(s) (for extended-length strings)
17397c478bd9Sstevel@tonic-gate  * into an in-core table/list
17407c478bd9Sstevel@tonic-gate  */
17417c478bd9Sstevel@tonic-gate static struct stab_list_s *
17427c478bd9Sstevel@tonic-gate load_arstring_table(struct stab_list_s *STabList,
17437c478bd9Sstevel@tonic-gate 	int fd, Elf *elf_file, Elf_Arhdr *p_ar, char *filename)
17447c478bd9Sstevel@tonic-gate {
17457c478bd9Sstevel@tonic-gate 	off_t here;
17467c478bd9Sstevel@tonic-gate 	struct stab_list_s *STL_entry, *STL_next;
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 	if (p_ar) {
17497c478bd9Sstevel@tonic-gate 		STL_entry = malloc(sizeof (struct stab_list_s));
17507c478bd9Sstevel@tonic-gate 		STL_entry->next    = 0;
17517c478bd9Sstevel@tonic-gate 		STL_entry->strings = 0;
17527c478bd9Sstevel@tonic-gate 		STL_entry->size    = 0;
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate 		if (!STabList)
17557c478bd9Sstevel@tonic-gate 			STabList = STL_entry;
17567c478bd9Sstevel@tonic-gate 		else {
17577c478bd9Sstevel@tonic-gate 			STL_next = STabList;
17587c478bd9Sstevel@tonic-gate 			while (STL_next->next != (void *)0)
17597c478bd9Sstevel@tonic-gate 				STL_next = STL_next->next;
17607c478bd9Sstevel@tonic-gate 			STL_next->next = STL_entry;
17617c478bd9Sstevel@tonic-gate 		}
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate 		STL_entry->size    = p_ar->ar_size;
17647c478bd9Sstevel@tonic-gate 		STL_entry->strings = malloc(p_ar->ar_size);
17657c478bd9Sstevel@tonic-gate 		here = elf_getbase(elf_file);
17667c478bd9Sstevel@tonic-gate 		if ((lseek(fd, here, 0)) != here) {
17677c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
17687c478bd9Sstevel@tonic-gate 			    "%s: %s: could not lseek\n", prog_name, filename);
17697c478bd9Sstevel@tonic-gate 		}
17707c478bd9Sstevel@tonic-gate 
17717c478bd9Sstevel@tonic-gate 		if ((read(fd, STL_entry->strings, p_ar->ar_size)) == -1) {
17727c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
17737c478bd9Sstevel@tonic-gate 			    "%s: %s: could not read\n", prog_name, filename);
17747c478bd9Sstevel@tonic-gate 		}
17757c478bd9Sstevel@tonic-gate 	}
17767c478bd9Sstevel@tonic-gate 	return (STabList);
17777c478bd9Sstevel@tonic-gate }
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate /*
17807c478bd9Sstevel@tonic-gate  * Print the archive header for each member of an archive.
17817c478bd9Sstevel@tonic-gate  * Also call ar_sym_read to print the symbols in the
17827c478bd9Sstevel@tonic-gate  * archive symbol table if g_flag.  Input is a file descriptor,
17837c478bd9Sstevel@tonic-gate  * an ELF file descriptor, and the filename.  Putting the call
17847c478bd9Sstevel@tonic-gate  * to dump the archive symbol table in this function is more
17857c478bd9Sstevel@tonic-gate  * efficient since it is necessary to examine the archive member
17867c478bd9Sstevel@tonic-gate  * name in the archive header to determine which member is the
17877c478bd9Sstevel@tonic-gate  * symbol table.
17887c478bd9Sstevel@tonic-gate  */
17897c478bd9Sstevel@tonic-gate static void
17907c478bd9Sstevel@tonic-gate dump_ar_hdr(int fd, Elf *elf_file, char *filename)
17917c478bd9Sstevel@tonic-gate {
17927c478bd9Sstevel@tonic-gate 	extern int v_flag, g_flag, a_flag, p_flag;
17937c478bd9Sstevel@tonic-gate 	Elf_Arhdr  *p_ar;
17947c478bd9Sstevel@tonic-gate 	Elf *arf;
17957c478bd9Sstevel@tonic-gate 	Elf_Cmd cmd;
17967c478bd9Sstevel@tonic-gate 	int title = 0;
17977c478bd9Sstevel@tonic-gate 	int err = 0;
17987c478bd9Sstevel@tonic-gate 
17997c478bd9Sstevel@tonic-gate 	char buf[DATESIZE];
18007c478bd9Sstevel@tonic-gate 
18017c478bd9Sstevel@tonic-gate 	cmd = ELF_C_READ;
18027c478bd9Sstevel@tonic-gate 	while ((arf = elf_begin(fd, cmd, elf_file)) != 0) {
18037c478bd9Sstevel@tonic-gate 		p_ar = elf_getarhdr(arf);
18047c478bd9Sstevel@tonic-gate 		if (p_ar == NULL) {
18057c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1806de777a60Sab196087 			    "%s: %s: %s\n", prog_name, filename,
1807de777a60Sab196087 			    elf_errmsg(-1));
18087c478bd9Sstevel@tonic-gate 			continue;
18097c478bd9Sstevel@tonic-gate 		}
1810*ba7866cdSAli Bahrami 		if ((strcmp(p_ar->ar_name, "/") == 0) ||
1811*ba7866cdSAli Bahrami 		    (strcmp(p_ar->ar_name, "/SYM64/") == 0)) {
18127c478bd9Sstevel@tonic-gate 			if (g_flag)
18137c478bd9Sstevel@tonic-gate 				ar_sym_read(elf_file, filename);
18147c478bd9Sstevel@tonic-gate 		} else if (strcmp(p_ar->ar_name, "//") == 0) {
18157c478bd9Sstevel@tonic-gate 			StringTableList = load_arstring_table(
1816de777a60Sab196087 			    StringTableList, fd, arf, p_ar, filename);
18177c478bd9Sstevel@tonic-gate 			cmd = elf_next(arf);
18187c478bd9Sstevel@tonic-gate 			(void) elf_end(arf);
18197c478bd9Sstevel@tonic-gate 			continue;
18207c478bd9Sstevel@tonic-gate 		} else {
18217c478bd9Sstevel@tonic-gate 			if (a_flag) {
18227c478bd9Sstevel@tonic-gate 				(void) printf("%s[%s]:\n", filename,
18237c478bd9Sstevel@tonic-gate 				    p_ar->ar_name);
18247c478bd9Sstevel@tonic-gate 				if (!p_flag && title == 0) {
18257c478bd9Sstevel@tonic-gate 					if (!v_flag)
18267c478bd9Sstevel@tonic-gate 						(void) printf(
18277c478bd9Sstevel@tonic-gate "\n\n\t\t\t***ARCHIVE HEADER***"
18287c478bd9Sstevel@tonic-gate "\n	Date          Uid     Gid    Mode      Size	 Member Name\n\n");
18297c478bd9Sstevel@tonic-gate 					else
18307c478bd9Sstevel@tonic-gate 						(void) printf(
18317c478bd9Sstevel@tonic-gate "\n\n\t\t\t***ARCHIVE HEADER***"
18327c478bd9Sstevel@tonic-gate "\n	Date                   Uid    Gid   Mode     Size     Member Name\n\n");
18337c478bd9Sstevel@tonic-gate 					title = 1;
18347c478bd9Sstevel@tonic-gate 				}
18357c478bd9Sstevel@tonic-gate 				if (!v_flag) {
18367c478bd9Sstevel@tonic-gate 					(void) printf(
18377c478bd9Sstevel@tonic-gate "\t0x%.8lx  %6d  %6d  0%.6ho  0x%.8lx  %-s\n\n",
1838de777a60Sab196087 					    p_ar->ar_date, (int)p_ar->ar_uid,
18397c478bd9Sstevel@tonic-gate 					    (int)p_ar->ar_gid,
18407c478bd9Sstevel@tonic-gate 					    (int)p_ar->ar_mode,
1841de777a60Sab196087 					    p_ar->ar_size, p_ar->ar_name);
18427c478bd9Sstevel@tonic-gate 				} else {
18437c478bd9Sstevel@tonic-gate 					if ((strftime(buf, DATESIZE,
18447c478bd9Sstevel@tonic-gate 					    "%b %d %H:%M:%S %Y",
18457c478bd9Sstevel@tonic-gate 					    localtime(
18467c478bd9Sstevel@tonic-gate 					    &(p_ar->ar_date)))) == 0) {
18477c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
18487c478bd9Sstevel@tonic-gate "%s: %s: don't have enough space to store the date\n", prog_name, filename);
18497c478bd9Sstevel@tonic-gate 						exit(1);
18507c478bd9Sstevel@tonic-gate 					}
18517c478bd9Sstevel@tonic-gate 					(void) printf(
18527c478bd9Sstevel@tonic-gate "\t%s %6d %6d 0%.6ho 0x%.8lx %-s\n\n",
1853de777a60Sab196087 					    buf, (int)p_ar->ar_uid,
18547c478bd9Sstevel@tonic-gate 					    (int)p_ar->ar_gid,
18557c478bd9Sstevel@tonic-gate 					    (int)p_ar->ar_mode,
1856de777a60Sab196087 					    p_ar->ar_size, p_ar->ar_name);
18577c478bd9Sstevel@tonic-gate 				}
18587c478bd9Sstevel@tonic-gate 			}
18597c478bd9Sstevel@tonic-gate 		}
18607c478bd9Sstevel@tonic-gate 		cmd = elf_next(arf);
18617c478bd9Sstevel@tonic-gate 		(void) elf_end(arf);
18627c478bd9Sstevel@tonic-gate 	} /* end while */
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate 	err = elf_errno();
18657c478bd9Sstevel@tonic-gate 	if (err != 0) {
18667c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
18677c478bd9Sstevel@tonic-gate 		    "%s: %s: %s\n", prog_name, filename, elf_errmsg(err));
18687c478bd9Sstevel@tonic-gate 	}
18697c478bd9Sstevel@tonic-gate }
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate /*
18727c478bd9Sstevel@tonic-gate  * Process member files of an archive.  This function provides
18737c478bd9Sstevel@tonic-gate  * a loop through an archive equivalent the processing of
18747c478bd9Sstevel@tonic-gate  * each_file for individual object files.
18757c478bd9Sstevel@tonic-gate  */
18767c478bd9Sstevel@tonic-gate static void
18777c478bd9Sstevel@tonic-gate dump_ar_files(int fd, Elf *elf_file, char *filename)
18787c478bd9Sstevel@tonic-gate {
18797c478bd9Sstevel@tonic-gate 	Elf_Arhdr  *p_ar;
18807c478bd9Sstevel@tonic-gate 	Elf *arf;
18817c478bd9Sstevel@tonic-gate 	Elf_Cmd cmd;
18827c478bd9Sstevel@tonic-gate 	Elf_Kind file_type;
18837c478bd9Sstevel@tonic-gate 	GElf_Ehdr elf_head;
18847c478bd9Sstevel@tonic-gate 	char *fullname;
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 	cmd = ELF_C_READ;
18877c478bd9Sstevel@tonic-gate 	while ((arf = elf_begin(fd, cmd, elf_file)) != 0) {
18885aefb655Srie 		size_t	len;
18895aefb655Srie 
18907c478bd9Sstevel@tonic-gate 		p_ar = elf_getarhdr(arf);
18917c478bd9Sstevel@tonic-gate 		if (p_ar == NULL) {
1892de777a60Sab196087 			(void) fprintf(stderr, "%s: %s: %s\n",
18937c478bd9Sstevel@tonic-gate 			    prog_name, filename, elf_errmsg(-1));
18947c478bd9Sstevel@tonic-gate 			return;
18957c478bd9Sstevel@tonic-gate 		}
1896*ba7866cdSAli Bahrami 		if (p_ar->ar_name[0] == '/') {
18977c478bd9Sstevel@tonic-gate 			cmd = elf_next(arf);
18987c478bd9Sstevel@tonic-gate 			(void) elf_end(arf);
18997c478bd9Sstevel@tonic-gate 			continue;
19007c478bd9Sstevel@tonic-gate 		}
19017c478bd9Sstevel@tonic-gate 
19025aefb655Srie 		len = strlen(filename) + strlen(p_ar->ar_name) + 3;
19035aefb655Srie 		if ((fullname = malloc(len)) == NULL)
19045aefb655Srie 			return;
19055aefb655Srie 		(void) snprintf(fullname, len, "%s[%s]", filename,
19065aefb655Srie 		    p_ar->ar_name);
19077c478bd9Sstevel@tonic-gate 		(void) printf("\n%s:\n", fullname);
19087c478bd9Sstevel@tonic-gate 		file_type = elf_kind(arf);
19097c478bd9Sstevel@tonic-gate 		if (file_type == ELF_K_ELF) {
19107c478bd9Sstevel@tonic-gate 			if (dump_elf_header(arf, fullname, &elf_head) == NULL)
19117c478bd9Sstevel@tonic-gate 				return;
19127c478bd9Sstevel@tonic-gate 			if (o_flag)
19137c478bd9Sstevel@tonic-gate 				dump_exec_header(arf,
19147c478bd9Sstevel@tonic-gate 				    (unsigned)elf_head.e_phnum, fullname);
19157c478bd9Sstevel@tonic-gate 			if (x_flag)
19167c478bd9Sstevel@tonic-gate 				dump_section_table(arf, &elf_head, fullname);
19177c478bd9Sstevel@tonic-gate 		} else {
1918de777a60Sab196087 			(void) fprintf(stderr, "%s: %s: invalid file type\n",
19197c478bd9Sstevel@tonic-gate 			    prog_name, fullname);
19207c478bd9Sstevel@tonic-gate 			cmd = elf_next(arf);
19217c478bd9Sstevel@tonic-gate 			(void) elf_end(arf);
19227c478bd9Sstevel@tonic-gate 			continue;
19237c478bd9Sstevel@tonic-gate 		}
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate 		cmd = elf_next(arf);
19267c478bd9Sstevel@tonic-gate 		(void) elf_end(arf);
19277c478bd9Sstevel@tonic-gate 	} /* end while */
19287c478bd9Sstevel@tonic-gate }
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate /*
19317c478bd9Sstevel@tonic-gate  * Takes a filename as input.  Test first for a valid version
19327c478bd9Sstevel@tonic-gate  * of libelf.a and exit on error.  Process each valid file
19337c478bd9Sstevel@tonic-gate  * or archive given as input on the command line.  Check
19347c478bd9Sstevel@tonic-gate  * for file type.  If it is an archive, process the archive-
19357c478bd9Sstevel@tonic-gate  * specific options first, then files within the archive.
19367c478bd9Sstevel@tonic-gate  * If it is an ELF object file, process it; otherwise
19377c478bd9Sstevel@tonic-gate  * warn that it is an invalid file type.
19387c478bd9Sstevel@tonic-gate  * All options except the archive-specific and program
19397c478bd9Sstevel@tonic-gate  * execution header are processed in the function, dump_section_table.
19407c478bd9Sstevel@tonic-gate  */
19417c478bd9Sstevel@tonic-gate static void
19427c478bd9Sstevel@tonic-gate each_file(char *filename)
19437c478bd9Sstevel@tonic-gate {
19447c478bd9Sstevel@tonic-gate 	Elf *elf_file;
19457c478bd9Sstevel@tonic-gate 	GElf_Ehdr elf_head;
19467c478bd9Sstevel@tonic-gate 	int fd;
19477c478bd9Sstevel@tonic-gate 	Elf_Kind   file_type;
19487c478bd9Sstevel@tonic-gate 
19497c478bd9Sstevel@tonic-gate 	struct stat buf;
19507c478bd9Sstevel@tonic-gate 
19517c478bd9Sstevel@tonic-gate 	Elf_Cmd cmd;
19527c478bd9Sstevel@tonic-gate 	errno = 0;
19537c478bd9Sstevel@tonic-gate 
19547c478bd9Sstevel@tonic-gate 	if (stat(filename, &buf) == -1) {
19555aefb655Srie 		int	err = errno;
19565aefb655Srie 		(void) fprintf(stderr, "%s: %s: %s", prog_name, filename,
19575aefb655Srie 		    strerror(err));
19587c478bd9Sstevel@tonic-gate 		return;
19597c478bd9Sstevel@tonic-gate 	}
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 	if ((fd = open((filename), O_RDONLY)) == -1) {
19625aefb655Srie 		(void) fprintf(stderr, "%s: %s: cannot read\n", prog_name,
19635aefb655Srie 		    filename);
19647c478bd9Sstevel@tonic-gate 		return;
19657c478bd9Sstevel@tonic-gate 	}
19667c478bd9Sstevel@tonic-gate 	cmd = ELF_C_READ;
19677c478bd9Sstevel@tonic-gate 	if ((elf_file = elf_begin(fd, cmd, (Elf *)0)) == NULL) {
19685aefb655Srie 		(void) fprintf(stderr, "%s: %s: %s\n", prog_name, filename,
19695aefb655Srie 		    elf_errmsg(-1));
19707c478bd9Sstevel@tonic-gate 		return;
19717c478bd9Sstevel@tonic-gate 	}
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate 	file_type = elf_kind(elf_file);
19747c478bd9Sstevel@tonic-gate 	if (file_type == ELF_K_AR) {
19757c478bd9Sstevel@tonic-gate 		if (a_flag || g_flag) {
19767c478bd9Sstevel@tonic-gate 			dump_ar_hdr(fd, elf_file, filename);
19777c478bd9Sstevel@tonic-gate 			elf_file = elf_begin(fd, cmd, (Elf *)0);
19787c478bd9Sstevel@tonic-gate 		}
19797c478bd9Sstevel@tonic-gate 		if (z_flag)
19807c478bd9Sstevel@tonic-gate 			dump_ar_files(fd, elf_file, filename);
19817c478bd9Sstevel@tonic-gate 	} else {
19827c478bd9Sstevel@tonic-gate 		if (file_type == ELF_K_ELF) {
19837c478bd9Sstevel@tonic-gate 			(void) printf("\n%s:\n", filename);
19845aefb655Srie 			if (dump_elf_header(elf_file, filename, &elf_head)) {
19857c478bd9Sstevel@tonic-gate 				if (o_flag)
19867c478bd9Sstevel@tonic-gate 					dump_exec_header(elf_file,
19875aefb655Srie 					    (unsigned)elf_head.e_phnum,
19885aefb655Srie 					    filename);
19897c478bd9Sstevel@tonic-gate 				if (x_flag)
19907c478bd9Sstevel@tonic-gate 					dump_section_table(elf_file,
19917c478bd9Sstevel@tonic-gate 					    &elf_head, filename);
19925aefb655Srie 			}
19937c478bd9Sstevel@tonic-gate 		} else {
19947c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s: invalid file type\n",
19957c478bd9Sstevel@tonic-gate 			    prog_name, filename);
19967c478bd9Sstevel@tonic-gate 		}
19977c478bd9Sstevel@tonic-gate 	}
19987c478bd9Sstevel@tonic-gate 	(void) elf_end(elf_file);
19997c478bd9Sstevel@tonic-gate 	(void) close(fd);
20007c478bd9Sstevel@tonic-gate }
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate /*
20037c478bd9Sstevel@tonic-gate  * Sets up flags for command line options given and then
20047c478bd9Sstevel@tonic-gate  * calls each_file() to process each file.
20057c478bd9Sstevel@tonic-gate  */
20067c478bd9Sstevel@tonic-gate int
20077c478bd9Sstevel@tonic-gate main(int argc, char *argv[], char *envp[])
20087c478bd9Sstevel@tonic-gate {
20097c478bd9Sstevel@tonic-gate 	char *optstr = OPTSTR; /* option string used by getopt() */
20107c478bd9Sstevel@tonic-gate 	int optchar;
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate 	/*
20137c478bd9Sstevel@tonic-gate 	 * Check for a binary that better fits this architecture.
20147c478bd9Sstevel@tonic-gate 	 */
20157010c12aSrie 	(void) conv_check_native(argv, envp);
20167c478bd9Sstevel@tonic-gate 
20177c478bd9Sstevel@tonic-gate 	prog_name = argv[0];
20187c478bd9Sstevel@tonic-gate 
20197c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
20207c478bd9Sstevel@tonic-gate 	while ((optchar = getopt(argc, argv, optstr)) != -1) {
20217c478bd9Sstevel@tonic-gate 		switch (optchar) {
20227c478bd9Sstevel@tonic-gate 		case 'a':
20237c478bd9Sstevel@tonic-gate 			a_flag = 1;
20247c478bd9Sstevel@tonic-gate 			x_flag = 1;
20257c478bd9Sstevel@tonic-gate 			break;
20267c478bd9Sstevel@tonic-gate 		case 'g':
20277c478bd9Sstevel@tonic-gate 			g_flag = 1;
20287c478bd9Sstevel@tonic-gate 			x_flag = 1;
20297c478bd9Sstevel@tonic-gate 			break;
20307c478bd9Sstevel@tonic-gate 		case 'v':
20317c478bd9Sstevel@tonic-gate 			v_flag = 1;
20327c478bd9Sstevel@tonic-gate 			break;
20337c478bd9Sstevel@tonic-gate 		case 'p':
20347c478bd9Sstevel@tonic-gate 			p_flag = 1;
20357c478bd9Sstevel@tonic-gate 			break;
20367c478bd9Sstevel@tonic-gate 		case 'f':
20377c478bd9Sstevel@tonic-gate 			f_flag = 1;
20387c478bd9Sstevel@tonic-gate 			z_flag = 1;
20397c478bd9Sstevel@tonic-gate 			break;
20407c478bd9Sstevel@tonic-gate 		case 'o':
20417c478bd9Sstevel@tonic-gate 			o_flag = 1;
20427c478bd9Sstevel@tonic-gate 			z_flag = 1;
20437c478bd9Sstevel@tonic-gate 			break;
20447c478bd9Sstevel@tonic-gate 		case 'h':
20457c478bd9Sstevel@tonic-gate 			h_flag = 1;
20467c478bd9Sstevel@tonic-gate 			x_flag = 1;
20477c478bd9Sstevel@tonic-gate 			z_flag = 1;
20487c478bd9Sstevel@tonic-gate 			break;
20497c478bd9Sstevel@tonic-gate 		case 's':
20507c478bd9Sstevel@tonic-gate 			s_flag = 1;
20517c478bd9Sstevel@tonic-gate 			x_flag = 1;
20527c478bd9Sstevel@tonic-gate 			z_flag = 1;
20537c478bd9Sstevel@tonic-gate 			break;
20547c478bd9Sstevel@tonic-gate 		case 'd':
20557c478bd9Sstevel@tonic-gate 			d_flag = 1;
20567c478bd9Sstevel@tonic-gate 			x_flag = 1;
20577c478bd9Sstevel@tonic-gate 			z_flag = 1;
20587c478bd9Sstevel@tonic-gate 			set_range(optarg, &d_low, &d_hi);
20597c478bd9Sstevel@tonic-gate 			break;
20607c478bd9Sstevel@tonic-gate 		case 'n':
20617c478bd9Sstevel@tonic-gate 			n_flag++;
20627c478bd9Sstevel@tonic-gate 			x_flag = 1;
20637c478bd9Sstevel@tonic-gate 			z_flag = 1;
20647c478bd9Sstevel@tonic-gate 			name = optarg;
20657c478bd9Sstevel@tonic-gate 			break;
20667c478bd9Sstevel@tonic-gate 		case 'r':
20677c478bd9Sstevel@tonic-gate 			r_flag = 1;
20687c478bd9Sstevel@tonic-gate 			x_flag = 1;
20697c478bd9Sstevel@tonic-gate 			z_flag = 1;
20707c478bd9Sstevel@tonic-gate 			break;
20717c478bd9Sstevel@tonic-gate 		case 't':
20727c478bd9Sstevel@tonic-gate 			t_flag = 1;
20737c478bd9Sstevel@tonic-gate 			x_flag = 1;
20747c478bd9Sstevel@tonic-gate 			z_flag = 1;
20757c478bd9Sstevel@tonic-gate 			break;
20767c478bd9Sstevel@tonic-gate 		case 'C':
20777c478bd9Sstevel@tonic-gate 			C_flag = 1;
20787c478bd9Sstevel@tonic-gate 			t_flag = 1;
20797c478bd9Sstevel@tonic-gate 			x_flag = 1;
20807c478bd9Sstevel@tonic-gate 			z_flag = 1;
20817c478bd9Sstevel@tonic-gate 			break;
20827c478bd9Sstevel@tonic-gate 		case 'T':
20837c478bd9Sstevel@tonic-gate 			T_flag = 1;
20847c478bd9Sstevel@tonic-gate 			x_flag = 1;
20857c478bd9Sstevel@tonic-gate 			z_flag = 1;
20867c478bd9Sstevel@tonic-gate 			set_range(optarg, &T_low, &T_hi);
20877c478bd9Sstevel@tonic-gate 			break;
20887c478bd9Sstevel@tonic-gate 		case 'c':
20897c478bd9Sstevel@tonic-gate 			c_flag = 1;
20907c478bd9Sstevel@tonic-gate 			x_flag = 1;
20917c478bd9Sstevel@tonic-gate 			z_flag = 1;
20927c478bd9Sstevel@tonic-gate 			break;
20937c478bd9Sstevel@tonic-gate 		case 'L':
20947c478bd9Sstevel@tonic-gate 			L_flag = 1;
20957c478bd9Sstevel@tonic-gate 			x_flag = 1;
20967c478bd9Sstevel@tonic-gate 			z_flag = 1;
20977c478bd9Sstevel@tonic-gate 			break;
20987c478bd9Sstevel@tonic-gate 		case 'V':
20997c478bd9Sstevel@tonic-gate 			V_flag = 1;
21007c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "dump: %s %s\n",
21017c478bd9Sstevel@tonic-gate 			    (const char *)SGU_PKG,
21027c478bd9Sstevel@tonic-gate 			    (const char *)SGU_REL);
21037c478bd9Sstevel@tonic-gate 			break;
21047c478bd9Sstevel@tonic-gate 		case '?':
21057c478bd9Sstevel@tonic-gate 			errflag += 1;
21067c478bd9Sstevel@tonic-gate 			break;
21077c478bd9Sstevel@tonic-gate 		default:
21087c478bd9Sstevel@tonic-gate 			break;
21097c478bd9Sstevel@tonic-gate 		}
21107c478bd9Sstevel@tonic-gate 	}
21117c478bd9Sstevel@tonic-gate 
21127c478bd9Sstevel@tonic-gate 	if (errflag || (optind >= argc) || (!z_flag && !x_flag)) {
21137c478bd9Sstevel@tonic-gate 		if (!(V_flag && (argc == 2))) {
21147c478bd9Sstevel@tonic-gate 			usage();
21157c478bd9Sstevel@tonic-gate 			exit(269);
21167c478bd9Sstevel@tonic-gate 		}
21177c478bd9Sstevel@tonic-gate 	}
21187c478bd9Sstevel@tonic-gate 
21195aefb655Srie 	if (elf_version(EV_CURRENT) == EV_NONE) {
21205aefb655Srie 		(void) fprintf(stderr, "%s: libelf is out of date\n",
21215aefb655Srie 		    prog_name);
21225aefb655Srie 		exit(101);
21235aefb655Srie 	}
21245aefb655Srie 
21257c478bd9Sstevel@tonic-gate 	while (optind < argc) {
21267c478bd9Sstevel@tonic-gate 		each_file(argv[optind]);
21277c478bd9Sstevel@tonic-gate 		optind++;
21287c478bd9Sstevel@tonic-gate 	}
21297c478bd9Sstevel@tonic-gate 	return (0);
21307c478bd9Sstevel@tonic-gate }
2131