xref: /onnv-gate/usr/src/cmd/sgs/elfdump/common/elfdump.c (revision 7682:b04d06fd448f)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51618Srie  * Common Development and Distribution License (the "License").
61618Srie  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211324Srie 
220Sstevel@tonic-gate /*
236206Sab196087  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * Dump an elf file.
290Sstevel@tonic-gate  */
301618Srie #include	<sys/elf_386.h>
311618Srie #include	<sys/elf_amd64.h>
321618Srie #include	<sys/elf_SPARC.h>
336206Sab196087 #include	<_libelf.h>
341618Srie #include	<dwarf.h>
355549Srie #include	<stdio.h>
360Sstevel@tonic-gate #include	<unistd.h>
370Sstevel@tonic-gate #include	<errno.h>
380Sstevel@tonic-gate #include	<strings.h>
390Sstevel@tonic-gate #include	<debug.h>
400Sstevel@tonic-gate #include	<conv.h>
410Sstevel@tonic-gate #include	<msg.h>
421618Srie #include	<_elfdump.h>
430Sstevel@tonic-gate 
443875Sab196087 
453875Sab196087 /*
463875Sab196087  * VERSYM_STATE is used to maintain information about the VERSYM section
473875Sab196087  * in the object being analyzed. It is filled in by versions(), and used
483875Sab196087  * by init_symtbl_state() when displaying symbol information.
493875Sab196087  *
50*7682SAli.Bahrami@Sun.COM  * There are three forms of symbol versioning known to us:
51*7682SAli.Bahrami@Sun.COM  *
52*7682SAli.Bahrami@Sun.COM  * 1) The original form, introduced with Solaris 2.5, in which
53*7682SAli.Bahrami@Sun.COM  *	the Versym contains indexes to Verdef records, and the
54*7682SAli.Bahrami@Sun.COM  *	Versym values for UNDEF symbols resolved by other objects
55*7682SAli.Bahrami@Sun.COM  *	are all set to 0.
56*7682SAli.Bahrami@Sun.COM  * 2) The GNU form, which is backward compatible with the original
57*7682SAli.Bahrami@Sun.COM  *	Solaris form, but which adds several extensions:
58*7682SAli.Bahrami@Sun.COM  *	- The Versym also contains indexes to Verneed records, recording
59*7682SAli.Bahrami@Sun.COM  *		which object/version contributed the external symbol at
60*7682SAli.Bahrami@Sun.COM  *		link time. These indexes start with the next value following
61*7682SAli.Bahrami@Sun.COM  *		the final Verdef index. The index is written to the previously
62*7682SAli.Bahrami@Sun.COM  *		reserved vna_other field of the ELF Vernaux structure.
63*7682SAli.Bahrami@Sun.COM  *	- The top bit of the Versym value is no longer part of the index,
64*7682SAli.Bahrami@Sun.COM  *		but is used as a "hidden bit" to prevent binding to the symbol.
65*7682SAli.Bahrami@Sun.COM  *	- Multiple implementations of a given symbol, contained in varying
66*7682SAli.Bahrami@Sun.COM  *		versions are allowed, using special assembler pseudo ops,
67*7682SAli.Bahrami@Sun.COM  *		and encoded in the symbol name using '@' characters.
68*7682SAli.Bahrami@Sun.COM  * 3) Modified Solaris form, in which we adopt the first GNU extension
69*7682SAli.Bahrami@Sun.COM  *	(Versym indexes to Verneed records), but not the others.
70*7682SAli.Bahrami@Sun.COM  *
71*7682SAli.Bahrami@Sun.COM  * elfdump can handle any of these cases. The presence of a DT_VERSYM
72*7682SAli.Bahrami@Sun.COM  * dynamic element indicates a full GNU object. An object that lacks
73*7682SAli.Bahrami@Sun.COM  * a DT_VERSYM entry, but which has non-zero vna_other fields in the Vernaux
74*7682SAli.Bahrami@Sun.COM  * structures is a modified Solaris object. An object that has neither of
75*7682SAli.Bahrami@Sun.COM  * these uses the original form.
76*7682SAli.Bahrami@Sun.COM  *
774716Sab196087  * max_verndx contains the largest version index that can appear
784716Sab196087  * in a Versym entry. This can never be less than 1: In the case where
794716Sab196087  * there is no verdef/verneed sections, the [0] index is reserved
80*7682SAli.Bahrami@Sun.COM  * for local symbols, and the [1] index for globals. If the original
81*7682SAli.Bahrami@Sun.COM  * Solaris versioning rules are in effect and there is a verdef section,
82*7682SAli.Bahrami@Sun.COM  * then max_verndex is the number of defined versions. If one of the
83*7682SAli.Bahrami@Sun.COM  * other versioning forms is in effect, then:
84*7682SAli.Bahrami@Sun.COM  *	1) If there is no verneed section, it is the same as for
85*7682SAli.Bahrami@Sun.COM  *		original Solaris versioning.
86*7682SAli.Bahrami@Sun.COM  *	2) If there is a verneed section, the vna_other field of the
874716Sab196087  *		Vernaux structs contain versions, and max_verndx is the
884716Sab196087  *		largest such index.
894716Sab196087  *
90*7682SAli.Bahrami@Sun.COM  * If gnu_full is True, the object uses the full GNU form of versioning.
91*7682SAli.Bahrami@Sun.COM  * The value of the gnu_full field is based on the presence of
924716Sab196087  * a DT_VERSYM entry in the dynamic section: GNU ld produces these, and
934716Sab196087  * Solaris ld does not.
94*7682SAli.Bahrami@Sun.COM  *
95*7682SAli.Bahrami@Sun.COM  * The gnu_needed field is True if the Versym contains indexes to
96*7682SAli.Bahrami@Sun.COM  * Verneed records, as indicated by non-zero vna_other fields in the Verneed
97*7682SAli.Bahrami@Sun.COM  * section. If gnu_full is True, then gnu_needed will always be true.
98*7682SAli.Bahrami@Sun.COM  * However, gnu_needed can be true without gnu_full. This is the modified
99*7682SAli.Bahrami@Sun.COM  * Solaris form.
1003875Sab196087  */
1013875Sab196087 typedef struct {
1023875Sab196087 	Cache	*cache;		/* Pointer to cache entry for VERSYM */
1033875Sab196087 	Versym	*data;		/* Pointer to versym array */
104*7682SAli.Bahrami@Sun.COM 	int	gnu_full;	/* True if object uses GNU versioning rules */
105*7682SAli.Bahrami@Sun.COM 	int	gnu_needed;	/* True if object uses VERSYM indexes for */
106*7682SAli.Bahrami@Sun.COM 				/*	VERNEED (subset of gnu_full) */
1074716Sab196087 	int	max_verndx;	/* largest versym index value */
1083875Sab196087 } VERSYM_STATE;
1093875Sab196087 
1103875Sab196087 /*
1113875Sab196087  * SYMTBL_STATE is used to maintain information about a single symbol
1123875Sab196087  * table section, for use by the routines that display symbol information.
1133875Sab196087  */
1143875Sab196087 typedef struct {
1153875Sab196087 	const char	*file;		/* Name of file */
1163875Sab196087 	Ehdr		*ehdr;		/* ELF header for file */
1173875Sab196087 	Cache		*cache;		/* Cache of all section headers */
1183875Sab196087 	Word		shnum;		/* # of sections in cache */
1193875Sab196087 	Cache		*seccache;	/* Cache of symbol table section hdr */
1203875Sab196087 	Word		secndx;		/* Index of symbol table section hdr */
1213875Sab196087 	const char	*secname;	/* Name of section */
1223875Sab196087 	uint_t		flags;		/* Command line option flags */
1233875Sab196087 	struct {			/* Extended section index data */
1243875Sab196087 		int	checked;	/* TRUE if already checked for shxndx */
1253875Sab196087 		Word	*data;		/* NULL, or extended section index */
1263875Sab196087 					/*	used for symbol table entries */
1273875Sab196087 		uint_t	n;		/* # items in shxndx.data */
1283875Sab196087 	} shxndx;
1293875Sab196087 	VERSYM_STATE	*versym;	/* NULL, or associated VERSYM section */
1303875Sab196087 	Sym 		*sym;		/* Array of symbols */
1313875Sab196087 	Word		symn;		/* # of symbols */
1323875Sab196087 } SYMTBL_STATE;
1333875Sab196087 
1343875Sab196087 
1353875Sab196087 
1360Sstevel@tonic-gate /*
1370Sstevel@tonic-gate  * Focal point for verifying symbol names.
1380Sstevel@tonic-gate  */
1390Sstevel@tonic-gate static const char *
1401618Srie string(Cache *refsec, Word ndx, Cache *strsec, const char *file, Word name)
1410Sstevel@tonic-gate {
1424063Sab196087 	/*
1434063Sab196087 	 * If an error in this routine is due to a property of the string
1444063Sab196087 	 * section, as opposed to a bad offset into the section (a property of
1454063Sab196087 	 * the referencing section), then we will detect the same error on
1464063Sab196087 	 * every call involving those sections. We use these static variables
1474063Sab196087 	 * to retain the information needed to only issue each such error once.
1484063Sab196087 	 */
1494063Sab196087 	static Cache	*last_refsec;	/* Last referencing section seen */
1504063Sab196087 	static int	strsec_err;	/* True if error issued */
1514063Sab196087 
1523492Sab196087 	const char	*strs;
1533492Sab196087 	Word		strn;
1540Sstevel@tonic-gate 
1553466Srie 	if (strsec->c_data == NULL)
1563466Srie 		return (NULL);
1573466Srie 
1583492Sab196087 	strs = (char *)strsec->c_data->d_buf;
1593492Sab196087 	strn = strsec->c_data->d_size;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	/*
1624063Sab196087 	 * We only print a diagnostic regarding a bad string table once per
1634063Sab196087 	 * input section being processed. If the refsec has changed, reset
1644063Sab196087 	 * our retained error state.
1650Sstevel@tonic-gate 	 */
1664063Sab196087 	if (last_refsec != refsec) {
1674063Sab196087 		last_refsec = refsec;
1684063Sab196087 		strsec_err = 0;
1694063Sab196087 	}
1704063Sab196087 
1714063Sab196087 	/* Verify that strsec really is a string table */
1724063Sab196087 	if (strsec->c_shdr->sh_type != SHT_STRTAB) {
1734063Sab196087 		if (!strsec_err) {
1744063Sab196087 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_NOTSTRTAB),
1754063Sab196087 			    file, strsec->c_ndx, refsec->c_ndx);
1764063Sab196087 			strsec_err = 1;
1774063Sab196087 		}
1784063Sab196087 		return (MSG_INTL(MSG_STR_UNKNOWN));
1790Sstevel@tonic-gate 	}
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	/*
1820Sstevel@tonic-gate 	 * Is the string table offset within range of the available strings?
1830Sstevel@tonic-gate 	 */
1840Sstevel@tonic-gate 	if (name >= strn) {
1850Sstevel@tonic-gate 		/*
1860Sstevel@tonic-gate 		 * Do we have a empty string table?
1870Sstevel@tonic-gate 		 */
1880Sstevel@tonic-gate 		if (strs == 0) {
1894063Sab196087 			if (!strsec_err) {
1900Sstevel@tonic-gate 				(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
1910Sstevel@tonic-gate 				    file, strsec->c_name);
1924063Sab196087 				strsec_err = 1;
1930Sstevel@tonic-gate 			}
1940Sstevel@tonic-gate 		} else {
1950Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSTOFF),
1961618Srie 			    file, refsec->c_name, EC_WORD(ndx), strsec->c_name,
1971618Srie 			    EC_WORD(name), EC_WORD(strn - 1));
1980Sstevel@tonic-gate 		}
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 		/*
2010Sstevel@tonic-gate 		 * Return the empty string so that the calling function can
2020Sstevel@tonic-gate 		 * continue it's output diagnostics.
2030Sstevel@tonic-gate 		 */
2040Sstevel@tonic-gate 		return (MSG_INTL(MSG_STR_UNKNOWN));
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate 	return (strs + name);
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate /*
2101618Srie  * Relocations can reference section symbols and standard symbols.  If the
2111618Srie  * former, establish the section name.
2121618Srie  */
2131618Srie static const char *
2141618Srie relsymname(Cache *cache, Cache *csec, Cache *strsec, Word symndx, Word symnum,
2157463SRod.Evans@Sun.COM     Word relndx, Sym *syms, char *secstr, size_t secsz, const char *file)
2161618Srie {
2177463SRod.Evans@Sun.COM 	Sym		*sym;
2187463SRod.Evans@Sun.COM 	const char	*name;
2191618Srie 
2201618Srie 	if (symndx >= symnum) {
2211618Srie 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_RELBADSYMNDX),
2221618Srie 		    file, EC_WORD(symndx), EC_WORD(relndx));
2231618Srie 		return (MSG_INTL(MSG_STR_UNKNOWN));
2241618Srie 	}
2251618Srie 
2261618Srie 	sym = (Sym *)(syms + symndx);
2277463SRod.Evans@Sun.COM 	name = string(csec, symndx, strsec, file, sym->st_name);
2281618Srie 
2291618Srie 	/*
2301618Srie 	 * If the symbol represents a section offset construct an appropriate
2317463SRod.Evans@Sun.COM 	 * string.  Note, although section symbol table entries typically have
2327463SRod.Evans@Sun.COM 	 * a NULL name pointer, entries do exist that point into the string
2337463SRod.Evans@Sun.COM 	 * table to their own NULL strings.
2341618Srie 	 */
2357463SRod.Evans@Sun.COM 	if ((ELF_ST_TYPE(sym->st_info) == STT_SECTION) &&
2367463SRod.Evans@Sun.COM 	    ((sym->st_name == 0) || (*name == '\0'))) {
2377463SRod.Evans@Sun.COM 		(void) snprintf(secstr, secsz, MSG_INTL(MSG_STR_SECTION),
2387463SRod.Evans@Sun.COM 		    cache[sym->st_shndx].c_name);
2391618Srie 		return ((const char *)secstr);
2401618Srie 	}
2411618Srie 
2427463SRod.Evans@Sun.COM 	return (name);
2431618Srie }
2441618Srie 
2451618Srie /*
2461618Srie  * Focal point for establishing a string table section.  Data such as the
2471618Srie  * dynamic information simply points to a string table.  Data such as
2481618Srie  * relocations, reference a symbol table, which in turn is associated with a
2491618Srie  * string table.
2500Sstevel@tonic-gate  */
2510Sstevel@tonic-gate static int
2521618Srie stringtbl(Cache *cache, int symtab, Word ndx, Word shnum, const char *file,
2531618Srie     Word *symnum, Cache **symsec, Cache **strsec)
2541618Srie {
2551618Srie 	Shdr	*shdr = cache[ndx].c_shdr;
2561618Srie 
2571618Srie 	if (symtab) {
2581618Srie 		/*
2591618Srie 		 * Validate the symbol table section.
2601618Srie 		 */
2611618Srie 		if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
2621618Srie 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
2631618Srie 			    file, cache[ndx].c_name, EC_WORD(shdr->sh_link));
2641618Srie 			return (0);
2651618Srie 		}
2663466Srie 		if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
2673466Srie 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
2683466Srie 			    file, cache[ndx].c_name);
2693466Srie 			return (0);
2703466Srie 		}
2711618Srie 
2721618Srie 		/*
2731618Srie 		 * Obtain, and verify the symbol table data.
2741618Srie 		 */
2753466Srie 		if ((cache[ndx].c_data == NULL) ||
2763466Srie 		    (cache[ndx].c_data->d_buf == NULL)) {
2771618Srie 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
2781618Srie 			    file, cache[ndx].c_name);
2791618Srie 			return (0);
2801618Srie 		}
2811618Srie 
2821618Srie 		/*
2831618Srie 		 * Establish the string table index.
2841618Srie 		 */
2851618Srie 		ndx = shdr->sh_link;
2861618Srie 		shdr = cache[ndx].c_shdr;
2871618Srie 
2881618Srie 		/*
2891618Srie 		 * Return symbol table information.
2901618Srie 		 */
2911618Srie 		if (symnum)
2921618Srie 			*symnum = (shdr->sh_size / shdr->sh_entsize);
2931618Srie 		if (symsec)
2941618Srie 			*symsec = &cache[ndx];
2951618Srie 	}
2961618Srie 
2971618Srie 	/*
2981618Srie 	 * Validate the associated string table section.
2991618Srie 	 */
3001618Srie 	if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
3011618Srie 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
3021618Srie 		    file, cache[ndx].c_name, EC_WORD(shdr->sh_link));
3031618Srie 		return (0);
3041618Srie 	}
3051618Srie 
3061618Srie 	if (strsec)
3071618Srie 		*strsec = &cache[shdr->sh_link];
3081618Srie 
3091618Srie 	return (1);
3101618Srie }
3111618Srie 
3121618Srie /*
3131618Srie  * Lookup a symbol and set Sym accordingly.
3141618Srie  */
3151618Srie static int
3161618Srie symlookup(const char *name, Cache *cache, Word shnum, Sym **sym,
3170Sstevel@tonic-gate     Cache *symtab, const char *file)
3180Sstevel@tonic-gate {
3191618Srie 	Shdr	*shdr;
3201618Srie 	Word	symn, cnt;
3211618Srie 	Sym	*syms;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	if (symtab == 0)
3240Sstevel@tonic-gate 		return (0);
3250Sstevel@tonic-gate 
3261618Srie 	shdr = symtab->c_shdr;
3271618Srie 
3280Sstevel@tonic-gate 	/*
3290Sstevel@tonic-gate 	 * Determine the symbol data and number.
3300Sstevel@tonic-gate 	 */
3310Sstevel@tonic-gate 	if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
3320Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
3330Sstevel@tonic-gate 		    file, symtab->c_name);
3340Sstevel@tonic-gate 		return (0);
3350Sstevel@tonic-gate 	}
3363466Srie 	if (symtab->c_data == NULL)
3373466Srie 		return (0);
3383466Srie 
3390Sstevel@tonic-gate 	/* LINTED */
3401618Srie 	symn = (Word)(shdr->sh_size / shdr->sh_entsize);
3411618Srie 	syms = (Sym *)symtab->c_data->d_buf;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	/*
3440Sstevel@tonic-gate 	 * Get the associated string table section.
3450Sstevel@tonic-gate 	 */
3460Sstevel@tonic-gate 	if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
3470Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
3481618Srie 		    file, symtab->c_name, EC_WORD(shdr->sh_link));
3490Sstevel@tonic-gate 		return (0);
3500Sstevel@tonic-gate 	}
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	/*
3530Sstevel@tonic-gate 	 * Loop through the symbol table to find a match.
3540Sstevel@tonic-gate 	 */
3551618Srie 	for (cnt = 0; cnt < symn; syms++, cnt++) {
3561618Srie 		const char	*symname;
3570Sstevel@tonic-gate 
3581618Srie 		symname = string(symtab, cnt, &cache[shdr->sh_link], file,
3591618Srie 		    syms->st_name);
3600Sstevel@tonic-gate 
3611618Srie 		if (symname && (strcmp(name, symname) == 0)) {
3621618Srie 			*sym = syms;
3630Sstevel@tonic-gate 			return (1);
3640Sstevel@tonic-gate 		}
3650Sstevel@tonic-gate 	}
3660Sstevel@tonic-gate 	return (0);
3670Sstevel@tonic-gate }
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate /*
3700Sstevel@tonic-gate  * Print section headers.
3710Sstevel@tonic-gate  */
3720Sstevel@tonic-gate static void
3734168Sab196087 sections(const char *file, Cache *cache, Word shnum, Ehdr *ehdr)
3740Sstevel@tonic-gate {
3751618Srie 	size_t	seccnt;
3760Sstevel@tonic-gate 
3771618Srie 	for (seccnt = 1; seccnt < shnum; seccnt++) {
3781618Srie 		Cache		*_cache = &cache[seccnt];
3791618Srie 		Shdr		*shdr = _cache->c_shdr;
3801618Srie 		const char	*secname = _cache->c_name;
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 		/*
3830Sstevel@tonic-gate 		 * Although numerous section header entries can be zero, it's
3843862Srie 		 * usually a sign of trouble if the type is zero.
3850Sstevel@tonic-gate 		 */
3860Sstevel@tonic-gate 		if (shdr->sh_type == 0) {
3870Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHTYPE),
3881618Srie 			    file, secname, EC_WORD(shdr->sh_type));
3890Sstevel@tonic-gate 		}
3903862Srie 
3915411Sab196087 		if (!match(MATCH_F_ALL, secname, seccnt, shdr->sh_type))
3923862Srie 			continue;
3930Sstevel@tonic-gate 
3941324Srie 		/*
3951324Srie 		 * Identify any sections that are suspicious.  A .got section
3961324Srie 		 * shouldn't exist in a relocatable object.
3971324Srie 		 */
3981324Srie 		if (ehdr->e_type == ET_REL) {
3991618Srie 			if (strncmp(secname, MSG_ORIG(MSG_ELF_GOT),
4001324Srie 			    MSG_ELF_GOT_SIZE) == 0) {
4011324Srie 				(void) fprintf(stderr,
4021618Srie 				    MSG_INTL(MSG_GOT_UNEXPECTED), file,
4031618Srie 				    secname);
4041324Srie 			}
4051324Srie 		}
4061324Srie 
4071618Srie 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
4081618Srie 		dbg_print(0, MSG_INTL(MSG_ELF_SHDR), EC_WORD(seccnt), secname);
4091618Srie 		Elf_shdr(0, ehdr->e_machine, shdr);
4100Sstevel@tonic-gate 	}
4110Sstevel@tonic-gate }
4120Sstevel@tonic-gate 
4131618Srie /*
4141618Srie  * A couple of instances of unwind data are printed as tables of 8 data items
4151618Srie  * expressed as 0x?? integers.
4161618Srie  */
4171618Srie #define	UNWINDTBLSZ	10 + (8 * 5) + 1
4181618Srie 
4191618Srie static void
4201618Srie unwindtbl(uint64_t *ndx, uint_t len, uchar_t *data, uint64_t doff,
4211618Srie     const char *msg, const char *pre, size_t plen)
4221618Srie {
4231618Srie 	char	buffer[UNWINDTBLSZ];
4241618Srie 	uint_t	boff = plen, cnt = 0;
4251618Srie 
4261618Srie 	dbg_print(0, msg);
4271618Srie 	(void) strncpy(buffer, pre, UNWINDTBLSZ);
4281618Srie 
4291618Srie 	while (*ndx < (len + 4)) {
4301618Srie 		if (cnt == 8) {
4311618Srie 			dbg_print(0, buffer);
4321618Srie 			boff = plen;
4331618Srie 			cnt = 0;
4341618Srie 		}
4351618Srie 		(void) snprintf(&buffer[boff], UNWINDTBLSZ - boff,
4361618Srie 		    MSG_ORIG(MSG_UNW_TBLENTRY), data[doff + (*ndx)++]);
4371618Srie 		boff += 5;
4381618Srie 		cnt++;
4391618Srie 	}
4401618Srie 	if (cnt)
4411618Srie 		dbg_print(0, buffer);
4421618Srie }
4431618Srie 
4441618Srie /*
4451618Srie  * Obtain a specified Phdr entry.
4461618Srie  */
4471618Srie static Phdr *
4481618Srie getphdr(Word phnum, Word type, const char *file, Elf *elf)
4491618Srie {
4501618Srie 	Word	cnt;
4511618Srie 	Phdr	*phdr;
4521618Srie 
4531618Srie 	if ((phdr = elf_getphdr(elf)) == NULL) {
4541618Srie 		failure(file, MSG_ORIG(MSG_ELF_GETPHDR));
4551618Srie 		return (0);
4561618Srie 	}
4571618Srie 
4581618Srie 	for (cnt = 0; cnt < phnum; phdr++, cnt++) {
4591618Srie 		if (phdr->p_type == type)
4601618Srie 			return (phdr);
4611618Srie 	}
4621618Srie 	return (0);
4631618Srie }
4641618Srie 
4650Sstevel@tonic-gate static void
4664168Sab196087 unwind(Cache *cache, Word shnum, Word phnum, Ehdr *ehdr, const char *file,
4674168Sab196087     Elf *elf)
4680Sstevel@tonic-gate {
4694734Sab196087 	Conv_dwarf_ehe_buf_t	dwarf_ehe_buf;
4701618Srie 	Word	cnt;
4711618Srie 	Phdr	*uphdr = 0;
4721618Srie 
4730Sstevel@tonic-gate 	/*
4741618Srie 	 * For the moment - UNWIND is only relevant for a AMD64 object.
4750Sstevel@tonic-gate 	 */
4760Sstevel@tonic-gate 	if (ehdr->e_machine != EM_AMD64)
4771618Srie 		return;
4780Sstevel@tonic-gate 
4791618Srie 	if (phnum)
4801618Srie 		uphdr = getphdr(phnum, PT_SUNW_UNWIND, file, elf);
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
4831618Srie 		Cache		*_cache = &cache[cnt];
4841618Srie 		Shdr		*shdr = _cache->c_shdr;
4851618Srie 		uchar_t		*data;
4860Sstevel@tonic-gate 		size_t		datasize;
4871618Srie 		uint64_t	off, ndx, frame_ptr, fde_cnt, tabndx;
4881618Srie 		uint_t		vers, frame_ptr_enc, fde_cnt_enc, table_enc;
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 		/*
4911618Srie 		 * AMD64 - this is a strmcp() just to find the gcc produced
4921618Srie 		 * sections.  Soon gcc should be setting the section type - and
4931618Srie 		 * we'll not need this strcmp().
4940Sstevel@tonic-gate 		 */
4950Sstevel@tonic-gate 		if ((shdr->sh_type != SHT_AMD64_UNWIND) &&
4960Sstevel@tonic-gate 		    (strncmp(_cache->c_name, MSG_ORIG(MSG_SCN_FRM),
4970Sstevel@tonic-gate 		    MSG_SCN_FRM_SIZE) != 0) &&
4980Sstevel@tonic-gate 		    (strncmp(_cache->c_name, MSG_ORIG(MSG_SCN_FRMHDR),
4990Sstevel@tonic-gate 		    MSG_SCN_FRMHDR_SIZE) != 0))
5000Sstevel@tonic-gate 			continue;
5014168Sab196087 
5025411Sab196087 		if (!match(MATCH_F_ALL, _cache->c_name, cnt, shdr->sh_type))
5030Sstevel@tonic-gate 			continue;
5040Sstevel@tonic-gate 
5053466Srie 		if (_cache->c_data == NULL)
5063466Srie 			continue;
5073466Srie 
5081618Srie 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
5091618Srie 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_UNWIND), _cache->c_name);
5100Sstevel@tonic-gate 
5111618Srie 		data = (uchar_t *)(_cache->c_data->d_buf);
5120Sstevel@tonic-gate 		datasize = _cache->c_data->d_size;
5130Sstevel@tonic-gate 		off = 0;
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 		/*
5160Sstevel@tonic-gate 		 * Is this a .eh_frame_hdr
5170Sstevel@tonic-gate 		 */
5181618Srie 		if ((uphdr && (shdr->sh_addr == uphdr->p_vaddr)) ||
5190Sstevel@tonic-gate 		    (strncmp(_cache->c_name, MSG_ORIG(MSG_SCN_FRMHDR),
5201618Srie 		    MSG_SCN_FRMHDR_SIZE) == 0)) {
5211618Srie 			dbg_print(0, MSG_ORIG(MSG_UNW_FRMHDR));
5221618Srie 			ndx = 0;
5230Sstevel@tonic-gate 
5241618Srie 			vers = data[ndx++];
5251618Srie 			frame_ptr_enc = data[ndx++];
5261618Srie 			fde_cnt_enc = data[ndx++];
5271618Srie 			table_enc = data[ndx++];
5280Sstevel@tonic-gate 
5291618Srie 			dbg_print(0, MSG_ORIG(MSG_UNW_FRMVERS), vers);
5300Sstevel@tonic-gate 
5311618Srie 			frame_ptr = dwarf_ehe_extract(data, &ndx, frame_ptr_enc,
5321618Srie 			    ehdr->e_ident, shdr->sh_addr + ndx);
5330Sstevel@tonic-gate 
5341618Srie 			dbg_print(0, MSG_ORIG(MSG_UNW_FRPTRENC),
5354734Sab196087 			    conv_dwarf_ehe(frame_ptr_enc, &dwarf_ehe_buf),
5364734Sab196087 			    EC_XWORD(frame_ptr));
5371618Srie 
5381618Srie 			fde_cnt = dwarf_ehe_extract(data, &ndx, fde_cnt_enc,
5391618Srie 			    ehdr->e_ident, shdr->sh_addr + ndx);
5400Sstevel@tonic-gate 
5411618Srie 			dbg_print(0, MSG_ORIG(MSG_UNW_FDCNENC),
5424734Sab196087 			    conv_dwarf_ehe(fde_cnt_enc, &dwarf_ehe_buf),
5434734Sab196087 			    EC_XWORD(fde_cnt));
5441618Srie 			dbg_print(0, MSG_ORIG(MSG_UNW_TABENC),
5454734Sab196087 			    conv_dwarf_ehe(table_enc, &dwarf_ehe_buf));
5461618Srie 			dbg_print(0, MSG_ORIG(MSG_UNW_BINSRTAB1));
5471618Srie 			dbg_print(0, MSG_ORIG(MSG_UNW_BINSRTAB2));
5480Sstevel@tonic-gate 
5491618Srie 			for (tabndx = 0; tabndx < fde_cnt; tabndx++) {
5501618Srie 				dbg_print(0, MSG_ORIG(MSG_UNW_BINSRTABENT),
5511618Srie 				    EC_XWORD(dwarf_ehe_extract(data, &ndx,
5521618Srie 				    table_enc, ehdr->e_ident, shdr->sh_addr)),
5531618Srie 				    EC_XWORD(dwarf_ehe_extract(data, &ndx,
5541618Srie 				    table_enc, ehdr->e_ident, shdr->sh_addr)));
5551618Srie 			}
5561618Srie 			continue;
5570Sstevel@tonic-gate 		}
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 		/*
5600Sstevel@tonic-gate 		 * Walk the Eh_frame's
5610Sstevel@tonic-gate 		 */
5620Sstevel@tonic-gate 		while (off < datasize) {
5634433Sab196087 			uint_t		cieid, cielength, cieversion;
5644433Sab196087 			uint_t		cieretaddr;
5651618Srie 			int		cieRflag, cieLflag, ciePflag, cieZflag;
5661618Srie 			uint_t		cieaugndx, length, id;
5670Sstevel@tonic-gate 			uint64_t	ciecalign, ciedalign;
5680Sstevel@tonic-gate 			char		*cieaugstr;
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 			ndx = 0;
5710Sstevel@tonic-gate 			/*
5726388Srie 			 * Extract length in lsb format.  A zero length
5736388Srie 			 * indicates that this CIE is a terminator and that
5746388Srie 			 * processing for this unwind information should end.
5756388Srie 			 * However, skip this entry and keep processing, just
5766388Srie 			 * in case there is any other information remaining in
5776388Srie 			 * this section.  Note, ld(1) will terminate the
5786388Srie 			 * processing of the .eh_frame contents for this file
5796388Srie 			 * after a zero length CIE, thus any information that
5806388Srie 			 * does follow is ignored by ld(1), and is therefore
5816388Srie 			 * questionable.
5820Sstevel@tonic-gate 			 */
5836388Srie 			if ((length = LSB32EXTRACT(data + off + ndx)) == 0) {
5846388Srie 				dbg_print(0, MSG_ORIG(MSG_UNW_ZEROTERM));
5856388Srie 				off += 4;
5866388Srie 				continue;
5876388Srie 			}
5880Sstevel@tonic-gate 			ndx += 4;
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 			/*
5910Sstevel@tonic-gate 			 * extract CIE id in lsb format
5920Sstevel@tonic-gate 			 */
5930Sstevel@tonic-gate 			id = LSB32EXTRACT(data + off + ndx);
5940Sstevel@tonic-gate 			ndx += 4;
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 			/*
5971618Srie 			 * A CIE record has a id of '0', otherwise this is a
5981618Srie 			 * FDE entry and the 'id' is the CIE pointer.
5990Sstevel@tonic-gate 			 */
6000Sstevel@tonic-gate 			if (id == 0) {
6010Sstevel@tonic-gate 				uint64_t    persVal;
6021618Srie 
6030Sstevel@tonic-gate 				cielength = length;
6040Sstevel@tonic-gate 				cieid = id;
6051618Srie 				cieLflag = ciePflag = cieRflag = cieZflag = 0;
6060Sstevel@tonic-gate 
6071618Srie 				dbg_print(0, MSG_ORIG(MSG_UNW_CIE),
6081618Srie 				    EC_XWORD(shdr->sh_addr + off));
6091618Srie 				dbg_print(0, MSG_ORIG(MSG_UNW_CIELNGTH),
6101618Srie 				    cielength, cieid);
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 				cieversion = data[off + ndx];
6130Sstevel@tonic-gate 				ndx += 1;
6140Sstevel@tonic-gate 				cieaugstr = (char *)(&data[off + ndx]);
6150Sstevel@tonic-gate 				ndx += strlen(cieaugstr) + 1;
6161618Srie 
6171618Srie 				dbg_print(0, MSG_ORIG(MSG_UNW_CIEVERS),
6181618Srie 				    cieversion, cieaugstr);
6191618Srie 
6200Sstevel@tonic-gate 				ciecalign = uleb_extract(&data[off], &ndx);
6210Sstevel@tonic-gate 				ciedalign = sleb_extract(&data[off], &ndx);
6220Sstevel@tonic-gate 				cieretaddr = data[off + ndx];
6230Sstevel@tonic-gate 				ndx += 1;
6241618Srie 
6251618Srie 				dbg_print(0, MSG_ORIG(MSG_UNW_CIECALGN),
6261618Srie 				    EC_XWORD(ciecalign), EC_XWORD(ciedalign),
6271618Srie 				    cieretaddr);
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 				if (cieaugstr[0])
6304433Sab196087 					dbg_print(0,
6314433Sab196087 					    MSG_ORIG(MSG_UNW_CIEAXVAL));
6321618Srie 
6330Sstevel@tonic-gate 				for (cieaugndx = 0; cieaugstr[cieaugndx];
6340Sstevel@tonic-gate 				    cieaugndx++) {
6350Sstevel@tonic-gate 					uint_t	val;
6361618Srie 
6370Sstevel@tonic-gate 					switch (cieaugstr[cieaugndx]) {
6380Sstevel@tonic-gate 					case 'z':
6394433Sab196087 						val = uleb_extract(&data[off],
6404433Sab196087 						    &ndx);
6414433Sab196087 						dbg_print(0,
6424433Sab196087 						    MSG_ORIG(MSG_UNW_CIEAXSIZ),
6434433Sab196087 						    val);
6444433Sab196087 						cieZflag = 1;
6454433Sab196087 						break;
6460Sstevel@tonic-gate 					case 'P':
6474433Sab196087 						ciePflag = data[off + ndx];
6484433Sab196087 						ndx += 1;
6494433Sab196087 
6504433Sab196087 						persVal = dwarf_ehe_extract(
6514433Sab196087 						    &data[off], &ndx, ciePflag,
6524433Sab196087 						    ehdr->e_ident,
6534433Sab196087 						    shdr->sh_addr + off + ndx);
6544433Sab196087 						dbg_print(0,
6554433Sab196087 						    MSG_ORIG(MSG_UNW_CIEAXPERS),
6564433Sab196087 						    ciePflag,
6574734Sab196087 						    conv_dwarf_ehe(ciePflag,
6584734Sab196087 						    &dwarf_ehe_buf),
6594433Sab196087 						    EC_XWORD(persVal));
6604433Sab196087 						break;
6610Sstevel@tonic-gate 					case 'R':
6624433Sab196087 						val = data[off + ndx];
6634433Sab196087 						ndx += 1;
6644433Sab196087 						dbg_print(0,
6654433Sab196087 						    MSG_ORIG(MSG_UNW_CIEAXCENC),
6664734Sab196087 						    val, conv_dwarf_ehe(val,
6674734Sab196087 						    &dwarf_ehe_buf));
6684433Sab196087 						cieRflag = val;
6694433Sab196087 						break;
6700Sstevel@tonic-gate 					case 'L':
6714433Sab196087 						val = data[off + ndx];
6724433Sab196087 						ndx += 1;
6734433Sab196087 						dbg_print(0,
6744433Sab196087 						    MSG_ORIG(MSG_UNW_CIEAXLSDA),
6754734Sab196087 						    val, conv_dwarf_ehe(val,
6764734Sab196087 						    &dwarf_ehe_buf));
6774433Sab196087 						cieLflag = val;
6784433Sab196087 						break;
6790Sstevel@tonic-gate 					default:
6804433Sab196087 						dbg_print(0,
6814433Sab196087 						    MSG_ORIG(MSG_UNW_CIEAXUNEC),
6824433Sab196087 						    cieaugstr[cieaugndx]);
6834433Sab196087 						break;
6840Sstevel@tonic-gate 					}
6850Sstevel@tonic-gate 				}
6861618Srie 				if ((cielength + 4) > ndx)
6871618Srie 					unwindtbl(&ndx, cielength, data, off,
6881618Srie 					    MSG_ORIG(MSG_UNW_CIECFI),
6891618Srie 					    MSG_ORIG(MSG_UNW_CIEPRE),
6901618Srie 					    MSG_UNW_CIEPRE_SIZE);
6910Sstevel@tonic-gate 				off += cielength + 4;
6921618Srie 
6930Sstevel@tonic-gate 			} else {
6940Sstevel@tonic-gate 				uint_t	    fdelength = length;
6950Sstevel@tonic-gate 				int	    fdecieptr = id;
6960Sstevel@tonic-gate 				uint64_t    fdeinitloc, fdeaddrrange;
6970Sstevel@tonic-gate 
6981618Srie 				dbg_print(0, MSG_ORIG(MSG_UNW_FDE),
6991618Srie 				    EC_XWORD(shdr->sh_addr + off));
7001618Srie 				dbg_print(0, MSG_ORIG(MSG_UNW_FDELNGTH),
7010Sstevel@tonic-gate 				    fdelength, fdecieptr);
7021618Srie 
7030Sstevel@tonic-gate 				fdeinitloc = dwarf_ehe_extract(&data[off],
7040Sstevel@tonic-gate 				    &ndx, cieRflag, ehdr->e_ident,
7050Sstevel@tonic-gate 				    shdr->sh_addr + off + ndx);
7060Sstevel@tonic-gate 				fdeaddrrange = dwarf_ehe_extract(&data[off],
7070Sstevel@tonic-gate 				    &ndx, (cieRflag & ~DW_EH_PE_pcrel),
7080Sstevel@tonic-gate 				    ehdr->e_ident,
7090Sstevel@tonic-gate 				    shdr->sh_addr + off + ndx);
7101618Srie 
7111618Srie 				dbg_print(0, MSG_ORIG(MSG_UNW_FDEINITLOC),
7121618Srie 				    EC_XWORD(fdeinitloc),
7131618Srie 				    EC_XWORD(fdeaddrrange));
7141618Srie 
7150Sstevel@tonic-gate 				if (cieaugstr[0])
7161618Srie 					dbg_print(0,
7174433Sab196087 					    MSG_ORIG(MSG_UNW_FDEAXVAL));
7180Sstevel@tonic-gate 				if (cieZflag) {
7190Sstevel@tonic-gate 					uint64_t    val;
7200Sstevel@tonic-gate 					val = uleb_extract(&data[off], &ndx);
7211618Srie 					dbg_print(0,
7224433Sab196087 					    MSG_ORIG(MSG_UNW_FDEAXSIZE),
7231618Srie 					    EC_XWORD(val));
7240Sstevel@tonic-gate 					if (val & cieLflag) {
7254433Sab196087 						fdeinitloc = dwarf_ehe_extract(
7264433Sab196087 						    &data[off], &ndx, cieLflag,
7274433Sab196087 						    ehdr->e_ident,
7284433Sab196087 						    shdr->sh_addr + off + ndx);
7294433Sab196087 						dbg_print(0,
7304433Sab196087 						    MSG_ORIG(MSG_UNW_FDEAXLSDA),
7314433Sab196087 						    EC_XWORD(val));
7320Sstevel@tonic-gate 					}
7330Sstevel@tonic-gate 				}
7341618Srie 				if ((fdelength + 4) > ndx)
7351618Srie 					unwindtbl(&ndx, fdelength, data, off,
7361618Srie 					    MSG_ORIG(MSG_UNW_FDECFI),
7371618Srie 					    MSG_ORIG(MSG_UNW_FDEPRE),
7381618Srie 					    MSG_UNW_FDEPRE_SIZE);
7390Sstevel@tonic-gate 				off += fdelength + 4;
7400Sstevel@tonic-gate 			}
7410Sstevel@tonic-gate 		}
7420Sstevel@tonic-gate 	}
7430Sstevel@tonic-gate }
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate /*
7460Sstevel@tonic-gate  * Print the hardware/software capabilities.  For executables and shared objects
7470Sstevel@tonic-gate  * this should be accompanied with a program header.
7480Sstevel@tonic-gate  */
7490Sstevel@tonic-gate static void
7501618Srie cap(const char *file, Cache *cache, Word shnum, Word phnum, Ehdr *ehdr,
7511618Srie     Elf *elf)
7520Sstevel@tonic-gate {
7531618Srie 	Word		cnt;
7543466Srie 	Shdr		*cshdr = 0;
7553466Srie 	Cache		*ccache;
7561618Srie 	Off		cphdr_off = 0;
7571618Srie 	Xword		cphdr_sz;
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	/*
7600Sstevel@tonic-gate 	 * Determine if a hardware/software capabilities header exists.
7610Sstevel@tonic-gate 	 */
7621618Srie 	if (phnum) {
7631618Srie 		Phdr	*phdr;
7640Sstevel@tonic-gate 
7651618Srie 		if ((phdr = elf_getphdr(elf)) == NULL) {
7660Sstevel@tonic-gate 			failure(file, MSG_ORIG(MSG_ELF_GETPHDR));
7670Sstevel@tonic-gate 			return;
7680Sstevel@tonic-gate 		}
7690Sstevel@tonic-gate 
7701618Srie 		for (cnt = 0; cnt < phnum; phdr++, cnt++) {
7711618Srie 			if (phdr->p_type == PT_SUNWCAP) {
7721618Srie 				cphdr_off = phdr->p_offset;
7731618Srie 				cphdr_sz = phdr->p_filesz;
7741618Srie 				break;
7751618Srie 			}
7760Sstevel@tonic-gate 		}
7770Sstevel@tonic-gate 	}
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	/*
7800Sstevel@tonic-gate 	 * Determine if a hardware/software capabilities section exists.
7810Sstevel@tonic-gate 	 */
7820Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
7831618Srie 		Cache	*_cache = &cache[cnt];
7841618Srie 		Shdr	*shdr = _cache->c_shdr;
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 		if (shdr->sh_type != SHT_SUNW_cap)
7870Sstevel@tonic-gate 			continue;
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 		if (cphdr_off && ((cphdr_off < shdr->sh_offset) ||
7900Sstevel@tonic-gate 		    (cphdr_off + cphdr_sz) > (shdr->sh_offset + shdr->sh_size)))
7910Sstevel@tonic-gate 			continue;
7920Sstevel@tonic-gate 
7933466Srie 		if (_cache->c_data == NULL)
7943466Srie 			continue;
7953466Srie 
7960Sstevel@tonic-gate 		ccache = _cache;
7970Sstevel@tonic-gate 		cshdr = shdr;
7980Sstevel@tonic-gate 		break;
7990Sstevel@tonic-gate 	}
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	if ((cshdr == 0) && (cphdr_off == 0))
8020Sstevel@tonic-gate 		return;
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 	/*
8050Sstevel@tonic-gate 	 * Print the hardware/software capabilities section.
8060Sstevel@tonic-gate 	 */
8070Sstevel@tonic-gate 	if (cshdr) {
8081618Srie 		Word	ndx, capn;
8093492Sab196087 		Cap	*cap = (Cap *)ccache->c_data->d_buf;
8100Sstevel@tonic-gate 
8114665Sab196087 		if ((cshdr->sh_entsize == 0) || (cshdr->sh_size == 0)) {
8124665Sab196087 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
8134665Sab196087 			    file, ccache->c_name);
8144665Sab196087 			return;
8154665Sab196087 		}
8164665Sab196087 
8171618Srie 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
8181618Srie 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_CAP), ccache->c_name);
8190Sstevel@tonic-gate 
8201618Srie 		Elf_cap_title(0);
8211618Srie 
8221618Srie 		capn = (Word)(cshdr->sh_size / cshdr->sh_entsize);
8230Sstevel@tonic-gate 
8241618Srie 		for (ndx = 0; ndx < capn; cap++, ndx++) {
8251618Srie 			if (cap->c_tag != CA_SUNW_NULL)
8261618Srie 				Elf_cap_entry(0, cap, ndx, ehdr->e_machine);
8270Sstevel@tonic-gate 		}
8280Sstevel@tonic-gate 	} else
8290Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_WARN_INVCAP1), file);
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	/*
8320Sstevel@tonic-gate 	 * If this object is an executable or shared object, then the
8330Sstevel@tonic-gate 	 * hardware/software capabilities section should have an accompanying
8340Sstevel@tonic-gate 	 * program header.
8350Sstevel@tonic-gate 	 */
8360Sstevel@tonic-gate 	if (cshdr && ((ehdr->e_type == ET_EXEC) || (ehdr->e_type == ET_DYN))) {
8370Sstevel@tonic-gate 		if (cphdr_off == 0)
8380Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_WARN_INVCAP2),
8390Sstevel@tonic-gate 			    file, ccache->c_name);
8400Sstevel@tonic-gate 		else if ((cphdr_off != cshdr->sh_offset) ||
8410Sstevel@tonic-gate 		    (cphdr_sz != cshdr->sh_size))
8420Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_WARN_INVCAP3),
8430Sstevel@tonic-gate 			    file, ccache->c_name);
8440Sstevel@tonic-gate 	}
8450Sstevel@tonic-gate }
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate /*
8480Sstevel@tonic-gate  * Print the interpretor.
8490Sstevel@tonic-gate  */
8500Sstevel@tonic-gate static void
8511618Srie interp(const char *file, Cache *cache, Word shnum, Word phnum, Elf *elf)
8520Sstevel@tonic-gate {
8531618Srie 	Word	cnt;
8541618Srie 	Shdr	*ishdr = 0;
8551618Srie 	Cache	*icache;
8561618Srie 	Off	iphdr_off = 0;
8571618Srie 	Xword	iphdr_fsz;
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	/*
8600Sstevel@tonic-gate 	 * Determine if an interp header exists.
8610Sstevel@tonic-gate 	 */
8621618Srie 	if (phnum) {
8631618Srie 		Phdr	*phdr;
8640Sstevel@tonic-gate 
8651618Srie 		if ((phdr = getphdr(phnum, PT_INTERP, file, elf)) != 0) {
8661618Srie 			iphdr_off = phdr->p_offset;
8671618Srie 			iphdr_fsz = phdr->p_filesz;
8680Sstevel@tonic-gate 		}
8690Sstevel@tonic-gate 	}
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 	if (iphdr_off == 0)
8720Sstevel@tonic-gate 		return;
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	/*
8750Sstevel@tonic-gate 	 * Determine if an interp section exists.
8760Sstevel@tonic-gate 	 */
8770Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
8781618Srie 		Cache	*_cache = &cache[cnt];
8791618Srie 		Shdr	*shdr = _cache->c_shdr;
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 		/*
8820Sstevel@tonic-gate 		 * Scan sections to find a section which contains the PT_INTERP
8830Sstevel@tonic-gate 		 * string.  The target section can't be in a NOBITS section.
8840Sstevel@tonic-gate 		 */
8850Sstevel@tonic-gate 		if ((shdr->sh_type == SHT_NOBITS) ||
8860Sstevel@tonic-gate 		    (iphdr_off < shdr->sh_offset) ||
8871618Srie 		    (iphdr_off + iphdr_fsz) > (shdr->sh_offset + shdr->sh_size))
8880Sstevel@tonic-gate 			continue;
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 		icache = _cache;
8910Sstevel@tonic-gate 		ishdr = shdr;
8920Sstevel@tonic-gate 		break;
8930Sstevel@tonic-gate 	}
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 	/*
8960Sstevel@tonic-gate 	 * Print the interpreter string based on the offset defined in the
8970Sstevel@tonic-gate 	 * program header, as this is the offset used by the kernel.
8980Sstevel@tonic-gate 	 */
8993466Srie 	if (ishdr && icache->c_data) {
9001618Srie 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
9011618Srie 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_INTERP), icache->c_name);
9021618Srie 		dbg_print(0, MSG_ORIG(MSG_FMT_INDENT),
9030Sstevel@tonic-gate 		    (char *)icache->c_data->d_buf +
9040Sstevel@tonic-gate 		    (iphdr_off - ishdr->sh_offset));
9050Sstevel@tonic-gate 	} else
9060Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_WARN_INVINTERP1), file);
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 	/*
9090Sstevel@tonic-gate 	 * If there are any inconsistences between the program header and
9100Sstevel@tonic-gate 	 * section information, flag them.
9110Sstevel@tonic-gate 	 */
9120Sstevel@tonic-gate 	if (ishdr && ((iphdr_off != ishdr->sh_offset) ||
9131618Srie 	    (iphdr_fsz != ishdr->sh_size))) {
9140Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_WARN_INVINTERP2), file,
9150Sstevel@tonic-gate 		    icache->c_name);
9160Sstevel@tonic-gate 	}
9170Sstevel@tonic-gate }
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate /*
9200Sstevel@tonic-gate  * Print the syminfo section.
9210Sstevel@tonic-gate  */
9220Sstevel@tonic-gate static void
9231618Srie syminfo(Cache *cache, Word shnum, const char *file)
9240Sstevel@tonic-gate {
9251618Srie 	Shdr		*infoshdr;
9261618Srie 	Syminfo		*info;
9271618Srie 	Sym		*syms;
9281618Srie 	Dyn		*dyns;
9291618Srie 	Word		infonum, cnt, ndx, symnum;
9301618Srie 	Cache		*infocache = 0, *symsec, *strsec;
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
9331618Srie 		if (cache[cnt].c_shdr->sh_type == SHT_SUNW_syminfo) {
9341618Srie 			infocache = &cache[cnt];
9350Sstevel@tonic-gate 			break;
9360Sstevel@tonic-gate 		}
9370Sstevel@tonic-gate 	}
9381618Srie 	if (infocache == 0)
9390Sstevel@tonic-gate 		return;
9400Sstevel@tonic-gate 
9411618Srie 	infoshdr = infocache->c_shdr;
9421618Srie 	if ((infoshdr->sh_entsize == 0) || (infoshdr->sh_size == 0)) {
9430Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
9441618Srie 		    file, infocache->c_name);
9450Sstevel@tonic-gate 		return;
9460Sstevel@tonic-gate 	}
9473466Srie 	if (infocache->c_data == NULL)
9483466Srie 		return;
9493466Srie 
9501618Srie 	infonum = (Word)(infoshdr->sh_size / infoshdr->sh_entsize);
9511618Srie 	info = (Syminfo *)infocache->c_data->d_buf;
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 	/*
9540Sstevel@tonic-gate 	 * Get the data buffer of the associated dynamic section.
9550Sstevel@tonic-gate 	 */
9561618Srie 	if ((infoshdr->sh_info == 0) || (infoshdr->sh_info >= shnum)) {
9570Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHINFO),
9581618Srie 		    file, infocache->c_name, EC_WORD(infoshdr->sh_info));
9590Sstevel@tonic-gate 		return;
9600Sstevel@tonic-gate 	}
9613466Srie 	if (cache[infoshdr->sh_info].c_data == NULL)
9623466Srie 		return;
9633466Srie 
9641618Srie 	dyns = cache[infoshdr->sh_info].c_data->d_buf;
9651618Srie 	if (dyns == 0) {
9660Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
9671618Srie 		    file, cache[infoshdr->sh_info].c_name);
9680Sstevel@tonic-gate 		return;
9690Sstevel@tonic-gate 	}
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 	/*
9721618Srie 	 * Get the data buffer for the associated symbol table and string table.
9730Sstevel@tonic-gate 	 */
9741618Srie 	if (stringtbl(cache, 1, cnt, shnum, file,
9751618Srie 	    &symnum, &symsec, &strsec) == 0)
9760Sstevel@tonic-gate 		return;
9770Sstevel@tonic-gate 
9781618Srie 	syms = symsec->c_data->d_buf;
9790Sstevel@tonic-gate 
9801618Srie 	/*
9811618Srie 	 * Loop through the syminfo entries.
9821618Srie 	 */
9831618Srie 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
9841618Srie 	dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMINFO), infocache->c_name);
9851618Srie 	Elf_syminfo_title(0);
9860Sstevel@tonic-gate 
9871618Srie 	for (ndx = 1, info++; ndx < infonum; ndx++, info++) {
9881618Srie 		Sym 		*sym;
9891618Srie 		const char	*needed = 0, *name;
9901618Srie 
9911618Srie 		if ((info->si_flags == 0) && (info->si_boundto == 0))
9920Sstevel@tonic-gate 			continue;
9930Sstevel@tonic-gate 
9941618Srie 		sym = &syms[ndx];
9951618Srie 		name = string(infocache, ndx, strsec, file, sym->st_name);
9960Sstevel@tonic-gate 
9971618Srie 		if (info->si_boundto < SYMINFO_BT_LOWRESERVE) {
9981618Srie 			Dyn	*dyn = &dyns[info->si_boundto];
9991618Srie 
10001618Srie 			needed = string(infocache, info->si_boundto,
10011618Srie 			    strsec, file, dyn->d_un.d_val);
10020Sstevel@tonic-gate 		}
10031618Srie 		Elf_syminfo_entry(0, ndx, info, name, needed);
10040Sstevel@tonic-gate 	}
10050Sstevel@tonic-gate }
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate /*
10080Sstevel@tonic-gate  * Print version definition section entries.
10090Sstevel@tonic-gate  */
10100Sstevel@tonic-gate static void
10114716Sab196087 version_def(Verdef *vdf, Word vdf_num, Cache *vcache, Cache *scache,
10120Sstevel@tonic-gate     const char *file)
10130Sstevel@tonic-gate {
10141618Srie 	Word	cnt;
10151618Srie 	char	index[MAXNDXSIZE];
10160Sstevel@tonic-gate 
10171618Srie 	Elf_ver_def_title(0);
10180Sstevel@tonic-gate 
10194716Sab196087 	for (cnt = 1; cnt <= vdf_num; cnt++,
10201618Srie 	    vdf = (Verdef *)((uintptr_t)vdf + vdf->vd_next)) {
1021*7682SAli.Bahrami@Sun.COM 		Conv_ver_flags_buf_t	ver_flags_buf;
1022*7682SAli.Bahrami@Sun.COM 		const char		*name, *dep;
1023*7682SAli.Bahrami@Sun.COM 		Half			vcnt = vdf->vd_cnt - 1;
1024*7682SAli.Bahrami@Sun.COM 		Half			ndx = vdf->vd_ndx;
1025*7682SAli.Bahrami@Sun.COM 		Verdaux	*vdap = (Verdaux *)((uintptr_t)vdf + vdf->vd_aux);
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 		/*
10280Sstevel@tonic-gate 		 * Obtain the name and first dependency (if any).
10290Sstevel@tonic-gate 		 */
10300Sstevel@tonic-gate 		name = string(vcache, cnt, scache, file, vdap->vda_name);
10311618Srie 		vdap = (Verdaux *)((uintptr_t)vdap + vdap->vda_next);
10320Sstevel@tonic-gate 		if (vcnt)
10330Sstevel@tonic-gate 			dep = string(vcache, cnt, scache, file, vdap->vda_name);
10340Sstevel@tonic-gate 		else
10350Sstevel@tonic-gate 			dep = MSG_ORIG(MSG_STR_EMPTY);
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 		(void) snprintf(index, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INDEX),
10380Sstevel@tonic-gate 		    EC_XWORD(ndx));
10391618Srie 		Elf_ver_line_1(0, index, name, dep,
1040*7682SAli.Bahrami@Sun.COM 		    conv_ver_flags(vdf->vd_flags, 0, &ver_flags_buf));
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 		/*
10430Sstevel@tonic-gate 		 * Print any additional dependencies.
10440Sstevel@tonic-gate 		 */
10450Sstevel@tonic-gate 		if (vcnt) {
10461618Srie 			vdap = (Verdaux *)((uintptr_t)vdap + vdap->vda_next);
10470Sstevel@tonic-gate 			for (vcnt--; vcnt; vcnt--,
10481618Srie 			    vdap = (Verdaux *)((uintptr_t)vdap +
10490Sstevel@tonic-gate 			    vdap->vda_next)) {
10500Sstevel@tonic-gate 				dep = string(vcache, cnt, scache, file,
10510Sstevel@tonic-gate 				    vdap->vda_name);
10521618Srie 				Elf_ver_line_2(0, MSG_ORIG(MSG_STR_EMPTY), dep);
10530Sstevel@tonic-gate 			}
10540Sstevel@tonic-gate 		}
10550Sstevel@tonic-gate 	}
10560Sstevel@tonic-gate }
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate /*
10594716Sab196087  * Print version needed section entries.
10604716Sab196087  *
10614716Sab196087  * entry:
10624716Sab196087  *	vnd - Address of verneed data
10634716Sab196087  *	vnd_num - # of Verneed entries
10644716Sab196087  *	vcache - Cache of verneed section being processed
10654716Sab196087  *	scache - Cache of associated string table section
10664716Sab196087  *	file - Name of object being processed.
10674716Sab196087  *	versym - Information about versym section
10684716Sab196087  *
10694716Sab196087  * exit:
10704716Sab196087  *	The versions have been printed. If GNU style versioning
10714716Sab196087  *	is in effect, versym->max_verndx has been updated to
10724716Sab196087  *	contain the largest version index seen.
1073*7682SAli.Bahrami@Sun.COM  *
1074*7682SAli.Bahrami@Sun.COM  * note:
1075*7682SAli.Bahrami@Sun.COM  * 	The versym section of an object that follows the original
1076*7682SAli.Bahrami@Sun.COM  *	Solaris versioning rules only contains indexes into the verdef
1077*7682SAli.Bahrami@Sun.COM  *	section. Symbols defined in other objects (UNDEF) are given
1078*7682SAli.Bahrami@Sun.COM  *	a version of 0, indicating that they are not defined by
1079*7682SAli.Bahrami@Sun.COM  *	this file, and the Verneed entries do not have associated version
1080*7682SAli.Bahrami@Sun.COM  *	indexes. For these reasons, we do not display a version index
1081*7682SAli.Bahrami@Sun.COM  *	for original-style Verneed sections.
1082*7682SAli.Bahrami@Sun.COM  *
1083*7682SAli.Bahrami@Sun.COM  *	The GNU versioning extensions alter this: Symbols defined in other
1084*7682SAli.Bahrami@Sun.COM  *	objects receive a version index in the range above those defined
1085*7682SAli.Bahrami@Sun.COM  *	by the Verdef section, and the vna_other field of the Vernaux
1086*7682SAli.Bahrami@Sun.COM  *	structs inside the Verneed section contain the version index for
1087*7682SAli.Bahrami@Sun.COM  *	that item. We therefore  display the index when showing the
1088*7682SAli.Bahrami@Sun.COM  *	contents of a GNU style Verneed section. You should not
1089*7682SAli.Bahrami@Sun.COM  *	necessarily expect these indexes to appear in sorted
1090*7682SAli.Bahrami@Sun.COM  *	order --- it seems that the GNU ld assigns the versions as
1091*7682SAli.Bahrami@Sun.COM  *	symbols are encountered during linking, and then the results
1092*7682SAli.Bahrami@Sun.COM  *	are assembled into the Verneed section afterwards.
10930Sstevel@tonic-gate  */
10940Sstevel@tonic-gate static void
10954716Sab196087 version_need(Verneed *vnd, Word vnd_num, Cache *vcache, Cache *scache,
10964716Sab196087     const char *file, VERSYM_STATE *versym)
10970Sstevel@tonic-gate {
10984716Sab196087 	Word		cnt;
10994716Sab196087 	char		index[MAXNDXSIZE];
11004716Sab196087 	const char	*index_str;
11014716Sab196087 
1102*7682SAli.Bahrami@Sun.COM 	Elf_ver_need_title(0, versym->gnu_needed);
11034716Sab196087 
11044716Sab196087 	for (cnt = 1; cnt <= vnd_num; cnt++,
11051618Srie 	    vnd = (Verneed *)((uintptr_t)vnd + vnd->vn_next)) {
1106*7682SAli.Bahrami@Sun.COM 		Conv_ver_flags_buf_t	ver_flags_buf;
1107*7682SAli.Bahrami@Sun.COM 		const char		*name, *dep;
1108*7682SAli.Bahrami@Sun.COM 		Half			vcnt = vnd->vn_cnt;
11094433Sab196087 		Vernaux *vnap = (Vernaux *)((uintptr_t)vnd + vnd->vn_aux);
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 		/*
11120Sstevel@tonic-gate 		 * Obtain the name of the needed file and the version name
11130Sstevel@tonic-gate 		 * within it that we're dependent on.  Note that the count
11140Sstevel@tonic-gate 		 * should be at least one, otherwise this is a pretty bogus
11150Sstevel@tonic-gate 		 * entry.
11160Sstevel@tonic-gate 		 */
11170Sstevel@tonic-gate 		name = string(vcache, cnt, scache, file, vnd->vn_file);
11180Sstevel@tonic-gate 		if (vcnt)
11190Sstevel@tonic-gate 			dep = string(vcache, cnt, scache, file, vnap->vna_name);
11200Sstevel@tonic-gate 		else
11210Sstevel@tonic-gate 			dep = MSG_INTL(MSG_STR_NULL);
11220Sstevel@tonic-gate 
1123*7682SAli.Bahrami@Sun.COM 		if (vnap->vna_other == 0) {	/* Traditional form */
1124*7682SAli.Bahrami@Sun.COM 			index_str = MSG_ORIG(MSG_STR_EMPTY);
1125*7682SAli.Bahrami@Sun.COM 		} else {			/* GNU form */
1126*7682SAli.Bahrami@Sun.COM 			index_str = index;
11274716Sab196087 			/* Format the version index value */
11284716Sab196087 			(void) snprintf(index, MAXNDXSIZE,
11294716Sab196087 			    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(vnap->vna_other));
11304716Sab196087 			if (vnap->vna_other > versym->max_verndx)
11314716Sab196087 				versym->max_verndx = vnap->vna_other;
11324716Sab196087 		}
11334716Sab196087 		Elf_ver_line_1(0, index_str, name, dep,
1134*7682SAli.Bahrami@Sun.COM 		    conv_ver_flags(vnap->vna_flags, 0, &ver_flags_buf));
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 		/*
11370Sstevel@tonic-gate 		 * Print any additional version dependencies.
11380Sstevel@tonic-gate 		 */
11390Sstevel@tonic-gate 		if (vcnt) {
11401618Srie 			vnap = (Vernaux *)((uintptr_t)vnap + vnap->vna_next);
11410Sstevel@tonic-gate 			for (vcnt--; vcnt; vcnt--,
11421618Srie 			    vnap = (Vernaux *)((uintptr_t)vnap +
11430Sstevel@tonic-gate 			    vnap->vna_next)) {
11440Sstevel@tonic-gate 				dep = string(vcache, cnt, scache, file,
11450Sstevel@tonic-gate 				    vnap->vna_name);
1146*7682SAli.Bahrami@Sun.COM 				if (vnap->vna_other > 0) {
11474716Sab196087 					/* Format the next index value */
11484716Sab196087 					(void) snprintf(index, MAXNDXSIZE,
11494716Sab196087 					    MSG_ORIG(MSG_FMT_INDEX),
11504716Sab196087 					    EC_XWORD(vnap->vna_other));
1151*7682SAli.Bahrami@Sun.COM 					Elf_ver_line_1(0, index,
11524716Sab196087 					    MSG_ORIG(MSG_STR_EMPTY), dep,
1153*7682SAli.Bahrami@Sun.COM 					    conv_ver_flags(vnap->vna_flags,
1154*7682SAli.Bahrami@Sun.COM 					    0, &ver_flags_buf));
11554716Sab196087 					if (vnap->vna_other >
11564716Sab196087 					    versym->max_verndx)
11574716Sab196087 						versym->max_verndx =
11584716Sab196087 						    vnap->vna_other;
11594716Sab196087 				} else {
11604716Sab196087 					Elf_ver_line_3(0,
11614716Sab196087 					    MSG_ORIG(MSG_STR_EMPTY), dep,
1162*7682SAli.Bahrami@Sun.COM 					    conv_ver_flags(vnap->vna_flags,
1163*7682SAli.Bahrami@Sun.COM 					    0, &ver_flags_buf));
11644716Sab196087 				}
11654716Sab196087 			}
11664716Sab196087 		}
11674716Sab196087 	}
11684716Sab196087 }
11694716Sab196087 
11704716Sab196087 /*
1171*7682SAli.Bahrami@Sun.COM  * Examine the Verneed section for information related to GNU
1172*7682SAli.Bahrami@Sun.COM  * style Versym indexing:
1173*7682SAli.Bahrami@Sun.COM  *	- A non-zero vna_other field indicates that Versym indexes can
1174*7682SAli.Bahrami@Sun.COM  *		reference Verneed records.
1175*7682SAli.Bahrami@Sun.COM  *	- If the object uses GNU style Versym indexing, the
1176*7682SAli.Bahrami@Sun.COM  *	  maximum index value is needed to detect bad Versym entries.
11774716Sab196087  *
11784716Sab196087  * entry:
11794716Sab196087  *	vnd - Address of verneed data
11804716Sab196087  *	vnd_num - # of Verneed entries
11814716Sab196087  *	versym - Information about versym section
11824716Sab196087  *
11834716Sab196087  * exit:
1184*7682SAli.Bahrami@Sun.COM  *	If a non-zero vna_other field is seen, versym->gnu_needed is set.
1185*7682SAli.Bahrami@Sun.COM  *
11864716Sab196087  *	versym->max_verndx has been updated to contain the largest
11874716Sab196087  *	version index seen.
11884716Sab196087  */
11894716Sab196087 static void
1190*7682SAli.Bahrami@Sun.COM update_gnu_verndx(Verneed *vnd, Word vnd_num, VERSYM_STATE *versym)
11914716Sab196087 {
11924716Sab196087 	Word		cnt;
11934716Sab196087 
11944716Sab196087 	for (cnt = 1; cnt <= vnd_num; cnt++,
11954716Sab196087 	    vnd = (Verneed *)((uintptr_t)vnd + vnd->vn_next)) {
11964716Sab196087 		Half	vcnt = vnd->vn_cnt;
11974716Sab196087 		Vernaux	*vnap = (Vernaux *)((uintptr_t)vnd + vnd->vn_aux);
11984716Sab196087 
1199*7682SAli.Bahrami@Sun.COM 		/*
1200*7682SAli.Bahrami@Sun.COM 		 * A non-zero value of vna_other indicates that this
1201*7682SAli.Bahrami@Sun.COM 		 * object references VERNEED items from the VERSYM
1202*7682SAli.Bahrami@Sun.COM 		 * array.
1203*7682SAli.Bahrami@Sun.COM 		 */
1204*7682SAli.Bahrami@Sun.COM 		if (vnap->vna_other != 0) {
1205*7682SAli.Bahrami@Sun.COM 			versym->gnu_needed = 1;
1206*7682SAli.Bahrami@Sun.COM 			if (vnap->vna_other > versym->max_verndx)
1207*7682SAli.Bahrami@Sun.COM 				versym->max_verndx = vnap->vna_other;
1208*7682SAli.Bahrami@Sun.COM 		}
12094716Sab196087 
12104716Sab196087 		/*
12114716Sab196087 		 * Check any additional version dependencies.
12124716Sab196087 		 */
12134716Sab196087 		if (vcnt) {
12144716Sab196087 			vnap = (Vernaux *)((uintptr_t)vnap + vnap->vna_next);
12154716Sab196087 			for (vcnt--; vcnt; vcnt--,
12164716Sab196087 			    vnap = (Vernaux *)((uintptr_t)vnap +
12174716Sab196087 			    vnap->vna_next)) {
1218*7682SAli.Bahrami@Sun.COM 				if (vnap->vna_other == 0)
1219*7682SAli.Bahrami@Sun.COM 					continue;
1220*7682SAli.Bahrami@Sun.COM 
1221*7682SAli.Bahrami@Sun.COM 				versym->gnu_needed = 1;
12224716Sab196087 				if (vnap->vna_other > versym->max_verndx)
12234716Sab196087 					versym->max_verndx = vnap->vna_other;
12240Sstevel@tonic-gate 			}
12250Sstevel@tonic-gate 		}
12260Sstevel@tonic-gate 	}
12270Sstevel@tonic-gate }
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate /*
12303875Sab196087  * Display version section information if the flags require it.
12313875Sab196087  * Return version information needed by other output.
12323875Sab196087  *
12333875Sab196087  * entry:
12343875Sab196087  *	cache - Cache of all section headers
12353875Sab196087  *	shnum - # of sections in cache
12363875Sab196087  *	file - Name of file
12373875Sab196087  *	flags - Command line option flags
12383875Sab196087  *	versym - VERSYM_STATE block to be filled in.
12390Sstevel@tonic-gate  */
12403875Sab196087 static void
12413875Sab196087 versions(Cache *cache, Word shnum, const char *file, uint_t flags,
12423875Sab196087     VERSYM_STATE *versym)
12430Sstevel@tonic-gate {
12440Sstevel@tonic-gate 	GElf_Word	cnt;
12454716Sab196087 	Cache		*verdef_cache = NULL, *verneed_cache = NULL;
12464716Sab196087 
12474716Sab196087 
12484716Sab196087 	/* Gather information about the version sections */
12493875Sab196087 	bzero(versym, sizeof (*versym));
12504716Sab196087 	versym->max_verndx = 1;
12510Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
12521618Srie 		Cache		*_cache = &cache[cnt];
12531618Srie 		Shdr		*shdr = _cache->c_shdr;
12544716Sab196087 		Dyn		*dyn;
12554716Sab196087 		ulong_t		numdyn;
12564716Sab196087 
12574716Sab196087 		switch (shdr->sh_type) {
12584716Sab196087 		case SHT_DYNAMIC:
12594716Sab196087 			/*
12604716Sab196087 			 * The GNU ld puts a DT_VERSYM entry in the dynamic
12614716Sab196087 			 * section so that the runtime linker can use it to
12624716Sab196087 			 * implement their versioning rules. They allow multiple
12634716Sab196087 			 * incompatible functions with the same name to exist
12644716Sab196087 			 * in different versions. The Solaris ld does not
12654716Sab196087 			 * support this mechanism, and as such, does not
12664716Sab196087 			 * produce DT_VERSYM. We use this fact to determine
12674716Sab196087 			 * which ld produced this object, and how to interpret
12684716Sab196087 			 * the version values.
12694716Sab196087 			 */
12704716Sab196087 			if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0) ||
12714716Sab196087 			    (_cache->c_data == NULL))
12724716Sab196087 				continue;
12734716Sab196087 			numdyn = shdr->sh_size / shdr->sh_entsize;
12744716Sab196087 			dyn = (Dyn *)_cache->c_data->d_buf;
12754716Sab196087 			for (; numdyn-- > 0; dyn++)
12764716Sab196087 				if (dyn->d_tag == DT_VERSYM) {
1277*7682SAli.Bahrami@Sun.COM 					versym->gnu_full =
1278*7682SAli.Bahrami@Sun.COM 					    versym->gnu_needed = 1;
12794716Sab196087 					break;
12804716Sab196087 				}
12814716Sab196087 			break;
12824716Sab196087 
12834716Sab196087 		case SHT_SUNW_versym:
12844716Sab196087 			/* Record data address for later symbol processing */
12854716Sab196087 			if (_cache->c_data != NULL) {
12864716Sab196087 				versym->cache = _cache;
12874716Sab196087 				versym->data = _cache->c_data->d_buf;
12884716Sab196087 				continue;
12894716Sab196087 			}
12904716Sab196087 			break;
12914716Sab196087 
12924716Sab196087 		case SHT_SUNW_verdef:
12934716Sab196087 		case SHT_SUNW_verneed:
12944716Sab196087 			/*
12954716Sab196087 			 * Ensure the data is non-NULL and the number
12964716Sab196087 			 * of items is non-zero. Otherwise, we don't
12974716Sab196087 			 * understand the section, and will not use it.
12984716Sab196087 			 */
12994716Sab196087 			if ((_cache->c_data == NULL) ||
13004716Sab196087 			    (_cache->c_data->d_buf == NULL)) {
13014716Sab196087 				(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
13024716Sab196087 				    file, _cache->c_name);
13034716Sab196087 				continue;
13044716Sab196087 			}
13054716Sab196087 			if (shdr->sh_info == 0) {
13064716Sab196087 				(void) fprintf(stderr,
13074716Sab196087 				    MSG_INTL(MSG_ERR_BADSHINFO),
13084716Sab196087 				    file, _cache->c_name,
13094716Sab196087 				    EC_WORD(shdr->sh_info));
13104716Sab196087 				continue;
13114716Sab196087 			}
13124716Sab196087 
13134716Sab196087 			/* Make sure the string table index is in range */
13144716Sab196087 			if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
13154716Sab196087 				(void) fprintf(stderr,
13164716Sab196087 				    MSG_INTL(MSG_ERR_BADSHLINK), file,
13174716Sab196087 				    _cache->c_name, EC_WORD(shdr->sh_link));
13184716Sab196087 				continue;
13194716Sab196087 			}
13204716Sab196087 
13214716Sab196087 			/*
13224716Sab196087 			 * The section is usable. Save the cache entry.
13234716Sab196087 			 */
13244716Sab196087 			if (shdr->sh_type == SHT_SUNW_verdef) {
13254716Sab196087 				verdef_cache = _cache;
13264716Sab196087 				/*
13274716Sab196087 				 * Under Solaris rules, if there is a verdef
13284716Sab196087 				 * section, the max versym index is number
13294716Sab196087 				 * of version definitions it supplies.
13304716Sab196087 				 */
13314716Sab196087 				versym->max_verndx = shdr->sh_info;
13324716Sab196087 			} else {
13334716Sab196087 				verneed_cache = _cache;
13344716Sab196087 			}
13354716Sab196087 			break;
13364716Sab196087 		}
13374716Sab196087 	}
13384716Sab196087 
1339*7682SAli.Bahrami@Sun.COM 	/*
1340*7682SAli.Bahrami@Sun.COM 	 * If there is a Verneed section, examine it for information
1341*7682SAli.Bahrami@Sun.COM 	 * related to GNU style versioning.
1342*7682SAli.Bahrami@Sun.COM 	 */
1343*7682SAli.Bahrami@Sun.COM 	if (verneed_cache != NULL)
1344*7682SAli.Bahrami@Sun.COM 		update_gnu_verndx((Verneed *)verneed_cache->c_data->d_buf,
1345*7682SAli.Bahrami@Sun.COM 		    verneed_cache->c_shdr->sh_info, versym);
13464716Sab196087 
13474716Sab196087 	/*
13484716Sab196087 	 * Now that all the information is available, display the
1349*7682SAli.Bahrami@Sun.COM 	 * Verdef and Verneed section contents, if requested.
13504716Sab196087 	 */
1351*7682SAli.Bahrami@Sun.COM 	if ((flags & FLG_SHOW_VERSIONS) == 0)
1352*7682SAli.Bahrami@Sun.COM 		return;
13534716Sab196087 	if (verdef_cache != NULL) {
13544716Sab196087 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
13554716Sab196087 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_VERDEF),
13564716Sab196087 		    verdef_cache->c_name);
13574716Sab196087 		version_def((Verdef *)verdef_cache->c_data->d_buf,
13584716Sab196087 		    verdef_cache->c_shdr->sh_info, verdef_cache,
13594716Sab196087 		    &cache[verdef_cache->c_shdr->sh_link], file);
13604716Sab196087 	}
13614716Sab196087 	if (verneed_cache != NULL) {
13624716Sab196087 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
13634716Sab196087 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_VERNEED),
13644716Sab196087 		    verneed_cache->c_name);
13650Sstevel@tonic-gate 		/*
13664716Sab196087 		 * If GNU versioning applies to this object, version_need()
13674716Sab196087 		 * will update versym->max_verndx, and it is not
1368*7682SAli.Bahrami@Sun.COM 		 * necessary to call update_gnu_verndx().
13690Sstevel@tonic-gate 		 */
13704716Sab196087 		version_need((Verneed *)verneed_cache->c_data->d_buf,
13714716Sab196087 		    verneed_cache->c_shdr->sh_info, verneed_cache,
13724716Sab196087 		    &cache[verneed_cache->c_shdr->sh_link], file, versym);
13730Sstevel@tonic-gate 	}
13740Sstevel@tonic-gate }
13750Sstevel@tonic-gate 
13760Sstevel@tonic-gate /*
13773492Sab196087  * Initialize a symbol table state structure
13783492Sab196087  *
13793492Sab196087  * entry:
13803492Sab196087  *	state - State structure to be initialized
13813492Sab196087  *	cache - Cache of all section headers
13823492Sab196087  *	shnum - # of sections in cache
13833492Sab196087  *	secndx - Index of symbol table section
13843492Sab196087  *	ehdr - ELF header for file
13853875Sab196087  *	versym - Information about versym section
13863492Sab196087  *	file - Name of file
13873492Sab196087  *	flags - Command line option flags
13881618Srie  */
13891618Srie static int
13903492Sab196087 init_symtbl_state(SYMTBL_STATE *state, Cache *cache, Word shnum, Word secndx,
13913875Sab196087     Ehdr *ehdr, VERSYM_STATE *versym, const char *file, uint_t flags)
13923492Sab196087 {
13933492Sab196087 	Shdr *shdr;
13943492Sab196087 
13953492Sab196087 	state->file = file;
13963492Sab196087 	state->ehdr = ehdr;
13973492Sab196087 	state->cache = cache;
13983492Sab196087 	state->shnum = shnum;
13993492Sab196087 	state->seccache = &cache[secndx];
14003492Sab196087 	state->secndx = secndx;
14013492Sab196087 	state->secname = state->seccache->c_name;
14023492Sab196087 	state->flags = flags;
14033492Sab196087 	state->shxndx.checked = 0;
14043492Sab196087 	state->shxndx.data = NULL;
14053492Sab196087 	state->shxndx.n = 0;
14063492Sab196087 
14073492Sab196087 	shdr = state->seccache->c_shdr;
14083492Sab196087 
14093492Sab196087 	/*
14103492Sab196087 	 * Check the symbol data and per-item size.
14113492Sab196087 	 */
14123492Sab196087 	if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
14133492Sab196087 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
14143492Sab196087 		    file, state->secname);
14153492Sab196087 		return (0);
14163492Sab196087 	}
14173492Sab196087 	if (state->seccache->c_data == NULL)
14183492Sab196087 		return (0);
14193492Sab196087 
14203492Sab196087 	/* LINTED */
14213492Sab196087 	state->symn = (Word)(shdr->sh_size / shdr->sh_entsize);
14223492Sab196087 	state->sym = (Sym *)state->seccache->c_data->d_buf;
14233492Sab196087 
14243492Sab196087 	/*
14253492Sab196087 	 * Check associated string table section.
14263492Sab196087 	 */
14273492Sab196087 	if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
14283492Sab196087 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
14293492Sab196087 		    file, state->secname, EC_WORD(shdr->sh_link));
14303492Sab196087 		return (0);
14313492Sab196087 	}
14323492Sab196087 
14333492Sab196087 	/*
14343492Sab196087 	 * Determine if there is a associated Versym section
14353492Sab196087 	 * with this Symbol Table.
14363492Sab196087 	 */
14373875Sab196087 	if (versym->cache &&
14383875Sab196087 	    (versym->cache->c_shdr->sh_link == state->secndx))
14393875Sab196087 		state->versym = versym;
14403492Sab196087 	else
14413492Sab196087 		state->versym = NULL;
14423492Sab196087 
14433492Sab196087 
14443492Sab196087 	return (1);
14453492Sab196087 }
14463492Sab196087 
14473492Sab196087 /*
14483492Sab196087  * Determine the extended section index used for symbol tables entries.
14493492Sab196087  */
14503492Sab196087 static void
14517463SRod.Evans@Sun.COM symbols_getxindex(SYMTBL_STATE *state)
14521618Srie {
14531618Srie 	uint_t	symn;
14541618Srie 	Word	symcnt;
14551618Srie 
14563492Sab196087 	state->shxndx.checked = 1;   /* Note that we've been called */
14573492Sab196087 	for (symcnt = 1; symcnt < state->shnum; symcnt++) {
14583492Sab196087 		Cache	*_cache = &state->cache[symcnt];
14591618Srie 		Shdr	*shdr = _cache->c_shdr;
14601618Srie 
14611618Srie 		if ((shdr->sh_type != SHT_SYMTAB_SHNDX) ||
14623492Sab196087 		    (shdr->sh_link != state->secndx))
14631618Srie 			continue;
14641618Srie 
14651618Srie 		if ((shdr->sh_entsize) &&
14661618Srie 		    /* LINTED */
14671618Srie 		    ((symn = (uint_t)(shdr->sh_size / shdr->sh_entsize)) == 0))
14681618Srie 			continue;
14691618Srie 
14703466Srie 		if (_cache->c_data == NULL)
14713466Srie 			continue;
14723466Srie 
14733492Sab196087 		state->shxndx.data = _cache->c_data->d_buf;
14743492Sab196087 		state->shxndx.n = symn;
14753492Sab196087 		return;
14761618Srie 	}
14771618Srie }
14781618Srie 
14791618Srie /*
14803492Sab196087  * Produce a line of output for the given symbol
14813492Sab196087  *
14823492Sab196087  * entry:
14833875Sab196087  *	state - Symbol table state
14843492Sab196087  *	symndx - Index of symbol within the table
14854832Srie  *	info - Value of st_info (indicates local/global range)
14863492Sab196087  *	symndx_disp - Index to display. This may not be the same
14873492Sab196087  *		as symndx if the display is relative to the logical
14883492Sab196087  *		combination of the SUNW_ldynsym/dynsym tables.
14893492Sab196087  *	sym - Symbol to display
14900Sstevel@tonic-gate  */
14913492Sab196087 static void
14924832Srie output_symbol(SYMTBL_STATE *state, Word symndx, Word info, Word disp_symndx,
14934832Srie     Sym *sym)
14940Sstevel@tonic-gate {
14953118Sab196087 	/*
14963118Sab196087 	 * Symbol types for which we check that the specified
14973118Sab196087 	 * address/size land inside the target section.
14983118Sab196087 	 */
14993492Sab196087 	static const int addr_symtype[STT_NUM] = {
15003118Sab196087 		0,			/* STT_NOTYPE */
15013118Sab196087 		1,			/* STT_OBJECT */
15023118Sab196087 		1,			/* STT_FUNC */
15033118Sab196087 		0,			/* STT_SECTION */
15043118Sab196087 		0,			/* STT_FILE */
15053118Sab196087 		1,			/* STT_COMMON */
15063118Sab196087 		0,			/* STT_TLS */
15073118Sab196087 	};
15083118Sab196087 #if STT_NUM != (STT_TLS + 1)
15093492Sab196087 #error "STT_NUM has grown. Update addr_symtype[]"
15103118Sab196087 #endif
15113118Sab196087 
15124665Sab196087 	char		index[MAXNDXSIZE];
15134665Sab196087 	const char	*symname, *sec;
15143875Sab196087 	Versym		verndx;
15154716Sab196087 	int		gnuver;
15163492Sab196087 	uchar_t		type;
15173492Sab196087 	Shdr		*tshdr;
15183492Sab196087 	Word		shndx;
15194734Sab196087 	Conv_inv_buf_t	inv_buf;
15203492Sab196087 
15213492Sab196087 	/* Ensure symbol index is in range */
15223492Sab196087 	if (symndx >= state->symn) {
15233492Sab196087 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSORTNDX),
15243492Sab196087 		    state->file, state->secname, EC_WORD(symndx));
15253492Sab196087 		return;
15263492Sab196087 	}
15273492Sab196087 
15283492Sab196087 	/*
15293492Sab196087 	 * If we are using extended symbol indexes, find the
15303492Sab196087 	 * corresponding SHN_SYMTAB_SHNDX table.
15313492Sab196087 	 */
15323492Sab196087 	if ((sym->st_shndx == SHN_XINDEX) && (state->shxndx.checked == 0))
15333492Sab196087 		symbols_getxindex(state);
15343492Sab196087 
15353492Sab196087 	/* LINTED */
15363492Sab196087 	symname = string(state->seccache, symndx,
15373492Sab196087 	    &state->cache[state->seccache->c_shdr->sh_link], state->file,
15383492Sab196087 	    sym->st_name);
15393492Sab196087 
15403492Sab196087 	tshdr = 0;
15413492Sab196087 	sec = NULL;
15423492Sab196087 
15434665Sab196087 	if (state->ehdr->e_type == ET_CORE) {
15443492Sab196087 		sec = (char *)MSG_INTL(MSG_STR_UNKNOWN);
15455411Sab196087 	} else if (state->flags & FLG_CTL_FAKESHDR) {
15464665Sab196087 		/*
15474665Sab196087 		 * If we are using fake section headers derived from
15484665Sab196087 		 * the program headers, then the section indexes
15494665Sab196087 		 * in the symbols do not correspond to these headers.
15504665Sab196087 		 * The section names are not available, so all we can
15514665Sab196087 		 * do is to display them in numeric form.
15524665Sab196087 		 */
15534734Sab196087 		sec = conv_sym_shndx(sym->st_shndx, &inv_buf);
15544665Sab196087 	} else if ((sym->st_shndx < SHN_LORESERVE) &&
15553492Sab196087 	    (sym->st_shndx < state->shnum)) {
15563492Sab196087 		shndx = sym->st_shndx;
15573492Sab196087 		tshdr = state->cache[shndx].c_shdr;
15583492Sab196087 		sec = state->cache[shndx].c_name;
15593492Sab196087 	} else if (sym->st_shndx == SHN_XINDEX) {
15603492Sab196087 		if (state->shxndx.data) {
15613492Sab196087 			Word	_shxndx;
15623492Sab196087 
15633492Sab196087 			if (symndx > state->shxndx.n) {
15644433Sab196087 				(void) fprintf(stderr,
15654433Sab196087 				    MSG_INTL(MSG_ERR_BADSYMXINDEX1),
15664433Sab196087 				    state->file, state->secname,
15674433Sab196087 				    EC_WORD(symndx));
15683492Sab196087 			} else if ((_shxndx =
15693492Sab196087 			    state->shxndx.data[symndx]) > state->shnum) {
15704433Sab196087 				(void) fprintf(stderr,
15714433Sab196087 				    MSG_INTL(MSG_ERR_BADSYMXINDEX2),
15724433Sab196087 				    state->file, state->secname,
15734433Sab196087 				    EC_WORD(symndx), EC_WORD(_shxndx));
15743492Sab196087 			} else {
15754433Sab196087 				shndx = _shxndx;
15764433Sab196087 				tshdr = state->cache[shndx].c_shdr;
15774433Sab196087 				sec = state->cache[shndx].c_name;
15783492Sab196087 			}
15793492Sab196087 		} else {
15803492Sab196087 			(void) fprintf(stderr,
15813492Sab196087 			    MSG_INTL(MSG_ERR_BADSYMXINDEX3),
15823492Sab196087 			    state->file, state->secname, EC_WORD(symndx));
15833492Sab196087 		}
15843492Sab196087 	} else if ((sym->st_shndx < SHN_LORESERVE) &&
15853492Sab196087 	    (sym->st_shndx >= state->shnum)) {
15863492Sab196087 		(void) fprintf(stderr,
15873492Sab196087 		    MSG_INTL(MSG_ERR_BADSYM5), state->file,
15886206Sab196087 		    state->secname, EC_WORD(symndx),
15896206Sab196087 		    demangle(symname, state->flags), sym->st_shndx);
15903492Sab196087 	}
15910Sstevel@tonic-gate 
15923492Sab196087 	/*
15933492Sab196087 	 * If versioning is available display the
15943875Sab196087 	 * version index. If not, then use 0.
15953492Sab196087 	 */
15963875Sab196087 	if (state->versym) {
15974716Sab196087 		Versym test_verndx;
15984716Sab196087 
15994716Sab196087 		verndx = test_verndx = state->versym->data[symndx];
1600*7682SAli.Bahrami@Sun.COM 		gnuver = state->versym->gnu_full;
16013875Sab196087 
16023875Sab196087 		/*
16033875Sab196087 		 * Check to see if this is a defined symbol with a
16043875Sab196087 		 * version index that is outside the valid range for
16054716Sab196087 		 * the file. The interpretation of this depends on
16064716Sab196087 		 * the style of versioning used by the object.
16073875Sab196087 		 *
16084716Sab196087 		 * Versions >= VER_NDX_LORESERVE have special meanings,
16094716Sab196087 		 * and are exempt from this checking.
16104716Sab196087 		 *
16114716Sab196087 		 * GNU style version indexes use the top bit of the
16124716Sab196087 		 * 16-bit index value (0x8000) as the "hidden bit".
16134716Sab196087 		 * We must mask off this bit in order to compare
16144716Sab196087 		 * the version against the maximum value.
16153875Sab196087 		 */
16164716Sab196087 		if (gnuver)
16174716Sab196087 			test_verndx &= ~0x8000;
16184716Sab196087 
16194716Sab196087 		if ((test_verndx > state->versym->max_verndx) &&
16204716Sab196087 		    (verndx < VER_NDX_LORESERVE))
16214716Sab196087 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADVER),
16224716Sab196087 			    state->file, state->secname, EC_WORD(symndx),
16234716Sab196087 			    EC_HALF(test_verndx), state->versym->max_verndx);
16243875Sab196087 	} else {
16253492Sab196087 		verndx = 0;
16264716Sab196087 		gnuver = 0;
16273875Sab196087 	}
16283492Sab196087 
16293492Sab196087 	/*
16303492Sab196087 	 * Error checking for TLS.
16313492Sab196087 	 */
16323492Sab196087 	type = ELF_ST_TYPE(sym->st_info);
16333492Sab196087 	if (type == STT_TLS) {
16343492Sab196087 		if (tshdr &&
16353492Sab196087 		    (sym->st_shndx != SHN_UNDEF) &&
16363492Sab196087 		    ((tshdr->sh_flags & SHF_TLS) == 0)) {
16373492Sab196087 			(void) fprintf(stderr,
16383492Sab196087 			    MSG_INTL(MSG_ERR_BADSYM3), state->file,
16396206Sab196087 			    state->secname, EC_WORD(symndx),
16406206Sab196087 			    demangle(symname, state->flags));
16413492Sab196087 		}
16423492Sab196087 	} else if ((type != STT_SECTION) && sym->st_size &&
16433492Sab196087 	    tshdr && (tshdr->sh_flags & SHF_TLS)) {
16443492Sab196087 		(void) fprintf(stderr,
16453492Sab196087 		    MSG_INTL(MSG_ERR_BADSYM4), state->file,
16466206Sab196087 		    state->secname, EC_WORD(symndx),
16476206Sab196087 		    demangle(symname, state->flags));
16483492Sab196087 	}
16493492Sab196087 
16503492Sab196087 	/*
16513492Sab196087 	 * If a symbol with non-zero size has a type that
16523492Sab196087 	 * specifies an address, then make sure the location
16533492Sab196087 	 * it references is actually contained within the
16543492Sab196087 	 * section.  UNDEF symbols don't count in this case,
16553492Sab196087 	 * so we ignore them.
16563492Sab196087 	 *
16573492Sab196087 	 * The meaning of the st_value field in a symbol
16583492Sab196087 	 * depends on the type of object. For a relocatable
16593492Sab196087 	 * object, it is the offset within the section.
16603492Sab196087 	 * For sharable objects, it is the offset relative to
16613492Sab196087 	 * the base of the object, and for other types, it is
16623492Sab196087 	 * the virtual address. To get an offset within the
16633492Sab196087 	 * section for non-ET_REL files, we subtract the
16643492Sab196087 	 * base address of the section.
16653492Sab196087 	 */
16663492Sab196087 	if (addr_symtype[type] && (sym->st_size > 0) &&
16673492Sab196087 	    (sym->st_shndx != SHN_UNDEF) && ((sym->st_shndx < SHN_LORESERVE) ||
16683492Sab196087 	    (sym->st_shndx == SHN_XINDEX)) && (tshdr != NULL)) {
16693492Sab196087 		Word v = sym->st_value;
16703492Sab196087 			if (state->ehdr->e_type != ET_REL)
16714832Srie 				v -= tshdr->sh_addr;
16723492Sab196087 		if (((v + sym->st_size) > tshdr->sh_size)) {
16733492Sab196087 			(void) fprintf(stderr,
16743492Sab196087 			    MSG_INTL(MSG_ERR_BADSYM6), state->file,
16756206Sab196087 			    state->secname, EC_WORD(symndx),
16766206Sab196087 			    demangle(symname, state->flags),
16773492Sab196087 			    EC_WORD(shndx), EC_XWORD(tshdr->sh_size),
16783492Sab196087 			    EC_XWORD(sym->st_value), EC_XWORD(sym->st_size));
16793492Sab196087 		}
16803492Sab196087 	}
16813492Sab196087 
16824832Srie 	/*
16834832Srie 	 * A typical symbol table uses the sh_info field to indicate one greater
16844832Srie 	 * than the symbol table index of the last local symbol, STB_LOCAL.
16854832Srie 	 * Therefore, symbol indexes less than sh_info should have local
16864832Srie 	 * binding.  Symbol indexes greater than, or equal to sh_info, should
16874832Srie 	 * have global binding.  Note, we exclude UNDEF/NOTY symbols with zero
16884832Srie 	 * value and size, as these symbols may be the result of an mcs(1)
16894832Srie 	 * section deletion.
16904832Srie 	 */
16914832Srie 	if (info) {
16924832Srie 		uchar_t	bind = ELF_ST_BIND(sym->st_info);
16934832Srie 
16944832Srie 		if ((symndx < info) && (bind != STB_LOCAL)) {
16954832Srie 			(void) fprintf(stderr,
16964832Srie 			    MSG_INTL(MSG_ERR_BADSYM7), state->file,
16976206Sab196087 			    state->secname, EC_WORD(symndx),
16986206Sab196087 			    demangle(symname, state->flags), EC_XWORD(info));
16994832Srie 
17004832Srie 		} else if ((symndx >= info) && (bind == STB_LOCAL) &&
17014832Srie 		    ((sym->st_shndx != SHN_UNDEF) ||
17024832Srie 		    (ELF_ST_TYPE(sym->st_info) != STT_NOTYPE) ||
17034832Srie 		    (sym->st_size != 0) || (sym->st_value != 0))) {
17044832Srie 			(void) fprintf(stderr,
17054832Srie 			    MSG_INTL(MSG_ERR_BADSYM8), state->file,
17066206Sab196087 			    state->secname, EC_WORD(symndx),
17076206Sab196087 			    demangle(symname, state->flags), EC_XWORD(info));
17084832Srie 		}
17094832Srie 	}
17104832Srie 
17113492Sab196087 	(void) snprintf(index, MAXNDXSIZE,
17123492Sab196087 	    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(disp_symndx));
17133492Sab196087 	Elf_syms_table_entry(0, ELF_DBG_ELFDUMP, index,
17144716Sab196087 	    state->ehdr->e_machine, sym, verndx, gnuver, sec, symname);
17153492Sab196087 }
17163492Sab196087 
17173492Sab196087 /*
17183492Sab196087  * Search for and process any symbol tables.
17193492Sab196087  */
17203492Sab196087 void
17214168Sab196087 symbols(Cache *cache, Word shnum, Ehdr *ehdr, VERSYM_STATE *versym,
17224168Sab196087     const char *file, uint_t flags)
17233492Sab196087 {
17243492Sab196087 	SYMTBL_STATE state;
17253492Sab196087 	Cache *_cache;
17263492Sab196087 	Word secndx;
17273492Sab196087 
17283492Sab196087 	for (secndx = 1; secndx < shnum; secndx++) {
17293492Sab196087 		Word		symcnt;
17303492Sab196087 		Shdr		*shdr;
17313492Sab196087 
17323492Sab196087 		_cache = &cache[secndx];
17333492Sab196087 		shdr = _cache->c_shdr;
17340Sstevel@tonic-gate 
17350Sstevel@tonic-gate 		if ((shdr->sh_type != SHT_SYMTAB) &&
17362766Sab196087 		    (shdr->sh_type != SHT_DYNSYM) &&
17372766Sab196087 		    (shdr->sh_type != SHT_SUNW_LDYNSYM))
17380Sstevel@tonic-gate 			continue;
17395411Sab196087 		if (!match(MATCH_F_ALL, _cache->c_name, secndx, shdr->sh_type))
17403466Srie 			continue;
17413466Srie 
17423492Sab196087 		if (!init_symtbl_state(&state, cache, shnum, secndx, ehdr,
17433875Sab196087 		    versym, file, flags))
17440Sstevel@tonic-gate 			continue;
17450Sstevel@tonic-gate 		/*
17460Sstevel@tonic-gate 		 * Loop through the symbol tables entries.
17470Sstevel@tonic-gate 		 */
17481618Srie 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
17493492Sab196087 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMTAB), state.secname);
17501618Srie 		Elf_syms_table_title(0, ELF_DBG_ELFDUMP);
17510Sstevel@tonic-gate 
17523492Sab196087 		for (symcnt = 0; symcnt < state.symn; symcnt++)
17534832Srie 			output_symbol(&state, symcnt, shdr->sh_info, symcnt,
17543492Sab196087 			    state.sym + symcnt);
17553492Sab196087 	}
17563492Sab196087 }
17570Sstevel@tonic-gate 
17583492Sab196087 /*
17593492Sab196087  * Search for and process any SHT_SUNW_symsort or SHT_SUNW_tlssort sections.
17603492Sab196087  * These sections are always associated with the .SUNW_ldynsym./.dynsym pair.
17613492Sab196087  */
17623492Sab196087 static void
17634168Sab196087 sunw_sort(Cache *cache, Word shnum, Ehdr *ehdr, VERSYM_STATE *versym,
17644168Sab196087     const char *file, uint_t flags)
17653492Sab196087 {
17663492Sab196087 	SYMTBL_STATE	ldynsym_state,	dynsym_state;
17673492Sab196087 	Cache		*sortcache,	*symcache;
17683492Sab196087 	Shdr		*sortshdr,	*symshdr;
17693492Sab196087 	Word		sortsecndx,	symsecndx;
17703492Sab196087 	Word		ldynsym_cnt;
17713492Sab196087 	Word		*ndx;
17723492Sab196087 	Word		ndxn;
17733492Sab196087 	int		output_cnt = 0;
17744734Sab196087 	Conv_inv_buf_t	inv_buf;
17750Sstevel@tonic-gate 
17763492Sab196087 	for (sortsecndx = 1; sortsecndx < shnum; sortsecndx++) {
17770Sstevel@tonic-gate 
17783492Sab196087 		sortcache = &cache[sortsecndx];
17793492Sab196087 		sortshdr = sortcache->c_shdr;
17800Sstevel@tonic-gate 
17813492Sab196087 		if ((sortshdr->sh_type != SHT_SUNW_symsort) &&
17823492Sab196087 		    (sortshdr->sh_type != SHT_SUNW_tlssort))
17833492Sab196087 			continue;
17845411Sab196087 		if (!match(MATCH_F_ALL, sortcache->c_name, sortsecndx,
17855411Sab196087 		    sortshdr->sh_type))
17863492Sab196087 			continue;
17870Sstevel@tonic-gate 
17883492Sab196087 		/*
17893492Sab196087 		 * If the section references a SUNW_ldynsym, then we
17903492Sab196087 		 * expect to see the associated .dynsym immediately
17913492Sab196087 		 * following. If it references a .dynsym, there is no
17923492Sab196087 		 * SUNW_ldynsym. If it is any other type, then we don't
17933492Sab196087 		 * know what to do with it.
17943492Sab196087 		 */
17953492Sab196087 		if ((sortshdr->sh_link == 0) || (sortshdr->sh_link >= shnum)) {
17963492Sab196087 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
17973492Sab196087 			    file, sortcache->c_name,
17983492Sab196087 			    EC_WORD(sortshdr->sh_link));
17993492Sab196087 			continue;
18003492Sab196087 		}
18013492Sab196087 		symcache = &cache[sortshdr->sh_link];
18023492Sab196087 		symshdr = symcache->c_shdr;
18033492Sab196087 		symsecndx = sortshdr->sh_link;
18043492Sab196087 		ldynsym_cnt = 0;
18053492Sab196087 		switch (symshdr->sh_type) {
18063492Sab196087 		case SHT_SUNW_LDYNSYM:
18073492Sab196087 			if (!init_symtbl_state(&ldynsym_state, cache, shnum,
18083875Sab196087 			    symsecndx, ehdr, versym, file, flags))
18093492Sab196087 				continue;
18103492Sab196087 			ldynsym_cnt = ldynsym_state.symn;
18110Sstevel@tonic-gate 			/*
18123492Sab196087 			 * We know that the dynsym follows immediately
18133492Sab196087 			 * after the SUNW_ldynsym, and so, should be at
18143492Sab196087 			 * (sortshdr->sh_link + 1). However, elfdump is a
18153492Sab196087 			 * diagnostic tool, so we do the full paranoid
18163492Sab196087 			 * search instead.
18170Sstevel@tonic-gate 			 */
18183492Sab196087 			for (symsecndx = 1; symsecndx < shnum; symsecndx++) {
18193492Sab196087 				symcache = &cache[symsecndx];
18203492Sab196087 				symshdr = symcache->c_shdr;
18213492Sab196087 				if (symshdr->sh_type == SHT_DYNSYM)
18223492Sab196087 					break;
18233492Sab196087 			}
18243492Sab196087 			if (symsecndx >= shnum) {	/* Dynsym not found! */
18250Sstevel@tonic-gate 				(void) fprintf(stderr,
18263492Sab196087 				    MSG_INTL(MSG_ERR_NODYNSYM),
18273492Sab196087 				    file, sortcache->c_name);
18283492Sab196087 				continue;
18290Sstevel@tonic-gate 			}
18303492Sab196087 			/* Fallthrough to process associated dynsym */
18317463SRod.Evans@Sun.COM 			/* FALLTHROUGH */
18323492Sab196087 		case SHT_DYNSYM:
18333492Sab196087 			if (!init_symtbl_state(&dynsym_state, cache, shnum,
18343875Sab196087 			    symsecndx, ehdr, versym, file, flags))
18353492Sab196087 				continue;
18363492Sab196087 			break;
18373492Sab196087 		default:
18383492Sab196087 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADNDXSEC),
18393492Sab196087 			    file, sortcache->c_name, conv_sec_type(
18404734Sab196087 			    ehdr->e_machine, symshdr->sh_type, 0, &inv_buf));
18413492Sab196087 			continue;
18423492Sab196087 		}
18430Sstevel@tonic-gate 
18443492Sab196087 		/*
18453492Sab196087 		 * Output header
18463492Sab196087 		 */
18473492Sab196087 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
18483492Sab196087 		if (ldynsym_cnt > 0) {
18493492Sab196087 			dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMSORT2),
18503492Sab196087 			    sortcache->c_name, ldynsym_state.secname,
18513492Sab196087 			    dynsym_state.secname);
18520Sstevel@tonic-gate 			/*
18533492Sab196087 			 * The data for .SUNW_ldynsym and dynsym sections
18543492Sab196087 			 * is supposed to be adjacent with SUNW_ldynsym coming
18553492Sab196087 			 * first. Check, and issue a warning if it isn't so.
18560Sstevel@tonic-gate 			 */
18574665Sab196087 			if (((ldynsym_state.sym + ldynsym_state.symn)
18584665Sab196087 			    != dynsym_state.sym) &&
18595411Sab196087 			    ((flags & FLG_CTL_FAKESHDR) == 0))
18603492Sab196087 				(void) fprintf(stderr,
18613492Sab196087 				    MSG_INTL(MSG_ERR_LDYNNOTADJ), file,
18623492Sab196087 				    ldynsym_state.secname,
18633492Sab196087 				    dynsym_state.secname);
18643492Sab196087 		} else {
18653492Sab196087 			dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMSORT1),
18663492Sab196087 			    sortcache->c_name, dynsym_state.secname);
18673492Sab196087 		}
18683492Sab196087 		Elf_syms_table_title(0, ELF_DBG_ELFDUMP);
18693492Sab196087 
18703492Sab196087 		/* If not first one, insert a line of whitespace */
18713492Sab196087 		if (output_cnt++ > 0)
18723492Sab196087 			dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
18733118Sab196087 
18743492Sab196087 		/*
18753492Sab196087 		 * SUNW_dynsymsort and SUNW_dyntlssort are arrays of
18763492Sab196087 		 * symbol indices. Iterate over the array entries,
18773492Sab196087 		 * dispaying the referenced symbols.
18783492Sab196087 		 */
18793492Sab196087 		ndxn = sortshdr->sh_size / sortshdr->sh_entsize;
18803492Sab196087 		ndx = (Word *)sortcache->c_data->d_buf;
18813492Sab196087 		for (; ndxn-- > 0; ndx++) {
18823492Sab196087 			if (*ndx >= ldynsym_cnt) {
18833492Sab196087 				Word sec_ndx = *ndx - ldynsym_cnt;
18843492Sab196087 
18854832Srie 				output_symbol(&dynsym_state, sec_ndx, 0,
18863492Sab196087 				    *ndx, dynsym_state.sym + sec_ndx);
18873492Sab196087 			} else {
18884832Srie 				output_symbol(&ldynsym_state, *ndx, 0,
18893492Sab196087 				    *ndx, ldynsym_state.sym + *ndx);
18900Sstevel@tonic-gate 			}
18910Sstevel@tonic-gate 		}
18920Sstevel@tonic-gate 	}
18930Sstevel@tonic-gate }
18940Sstevel@tonic-gate 
18950Sstevel@tonic-gate /*
18960Sstevel@tonic-gate  * Search for and process any relocation sections.
18970Sstevel@tonic-gate  */
18980Sstevel@tonic-gate static void
18997463SRod.Evans@Sun.COM reloc(Cache *cache, Word shnum, Ehdr *ehdr, const char *file)
19000Sstevel@tonic-gate {
19011618Srie 	Word	cnt;
19020Sstevel@tonic-gate 
19030Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
19041618Srie 		Word		type, symnum;
19051618Srie 		Xword		relndx, relnum, relsize;
19061618Srie 		void		*rels;
19071618Srie 		Sym		*syms;
19081618Srie 		Cache		*symsec, *strsec;
19090Sstevel@tonic-gate 		Cache		*_cache = &cache[cnt];
19101618Srie 		Shdr		*shdr = _cache->c_shdr;
19111618Srie 		char		*relname = _cache->c_name;
19124734Sab196087 		Conv_inv_buf_t	inv_buf;
19130Sstevel@tonic-gate 
19140Sstevel@tonic-gate 		if (((type = shdr->sh_type) != SHT_RELA) &&
19150Sstevel@tonic-gate 		    (type != SHT_REL))
19160Sstevel@tonic-gate 			continue;
19175411Sab196087 		if (!match(MATCH_F_ALL, relname, cnt, type))
19180Sstevel@tonic-gate 			continue;
19190Sstevel@tonic-gate 
19200Sstevel@tonic-gate 		/*
19211618Srie 		 * Decide entry size.
19220Sstevel@tonic-gate 		 */
19231618Srie 		if (((relsize = shdr->sh_entsize) == 0) ||
19241618Srie 		    (relsize > shdr->sh_size)) {
19250Sstevel@tonic-gate 			if (type == SHT_RELA)
19261618Srie 				relsize = sizeof (Rela);
19270Sstevel@tonic-gate 			else
19281618Srie 				relsize = sizeof (Rel);
19290Sstevel@tonic-gate 		}
19300Sstevel@tonic-gate 
19310Sstevel@tonic-gate 		/*
19320Sstevel@tonic-gate 		 * Determine the number of relocations available.
19330Sstevel@tonic-gate 		 */
19340Sstevel@tonic-gate 		if (shdr->sh_size == 0) {
19350Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
19361618Srie 			    file, relname);
19370Sstevel@tonic-gate 			continue;
19380Sstevel@tonic-gate 		}
19393466Srie 		if (_cache->c_data == NULL)
19403466Srie 			continue;
19413466Srie 
19421618Srie 		rels = _cache->c_data->d_buf;
19431618Srie 		relnum = shdr->sh_size / relsize;
19440Sstevel@tonic-gate 
19450Sstevel@tonic-gate 		/*
19461618Srie 		 * Get the data buffer for the associated symbol table and
19471618Srie 		 * string table.
19480Sstevel@tonic-gate 		 */
19491618Srie 		if (stringtbl(cache, 1, cnt, shnum, file,
19501618Srie 		    &symnum, &symsec, &strsec) == 0)
19510Sstevel@tonic-gate 			continue;
19521618Srie 
19531618Srie 		syms = symsec->c_data->d_buf;
19540Sstevel@tonic-gate 
19550Sstevel@tonic-gate 		/*
19560Sstevel@tonic-gate 		 * Loop through the relocation entries.
19570Sstevel@tonic-gate 		 */
19581618Srie 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
19591618Srie 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_RELOC), _cache->c_name);
19601618Srie 		Elf_reloc_title(0, ELF_DBG_ELFDUMP, type);
19610Sstevel@tonic-gate 
19621618Srie 		for (relndx = 0; relndx < relnum; relndx++,
19631618Srie 		    rels = (void *)((char *)rels + relsize)) {
19646206Sab196087 			Half		mach = ehdr->e_machine;
19650Sstevel@tonic-gate 			char		section[BUFSIZ];
19661618Srie 			const char	*symname;
19671618Srie 			Word		symndx, reltype;
19681618Srie 			Rela		*rela;
19691618Srie 			Rel		*rel;
19700Sstevel@tonic-gate 
19710Sstevel@tonic-gate 			/*
19721618Srie 			 * Unravel the relocation and determine the symbol with
19731618Srie 			 * which this relocation is associated.
19740Sstevel@tonic-gate 			 */
19750Sstevel@tonic-gate 			if (type == SHT_RELA) {
19761618Srie 				rela = (Rela *)rels;
19771618Srie 				symndx = ELF_R_SYM(rela->r_info);
19786206Sab196087 				reltype = ELF_R_TYPE(rela->r_info, mach);
19790Sstevel@tonic-gate 			} else {
19801618Srie 				rel = (Rel *)rels;
19811618Srie 				symndx = ELF_R_SYM(rel->r_info);
19826206Sab196087 				reltype = ELF_R_TYPE(rel->r_info, mach);
19830Sstevel@tonic-gate 			}
19841618Srie 
19851618Srie 			symname = relsymname(cache, _cache, strsec, symndx,
19867463SRod.Evans@Sun.COM 			    symnum, relndx, syms, section, BUFSIZ, file);
19871618Srie 
19881618Srie 			/*
19891618Srie 			 * A zero symbol index is only valid for a few
19901618Srie 			 * relocations.
19911618Srie 			 */
19921618Srie 			if (symndx == 0) {
19931618Srie 				int	badrel = 0;
19940Sstevel@tonic-gate 
19951618Srie 				if ((mach == EM_SPARC) ||
19961618Srie 				    (mach == EM_SPARC32PLUS) ||
19971618Srie 				    (mach == EM_SPARCV9)) {
19981618Srie 					if ((reltype != R_SPARC_NONE) &&
19991618Srie 					    (reltype != R_SPARC_REGISTER) &&
20001618Srie 					    (reltype != R_SPARC_RELATIVE))
20011618Srie 						badrel++;
20021618Srie 				} else if (mach == EM_386) {
20031618Srie 					if ((reltype != R_386_NONE) &&
20041618Srie 					    (reltype != R_386_RELATIVE))
20051618Srie 						badrel++;
20061618Srie 				} else if (mach == EM_AMD64) {
20071618Srie 					if ((reltype != R_AMD64_NONE) &&
20081618Srie 					    (reltype != R_AMD64_RELATIVE))
20091618Srie 						badrel++;
20101618Srie 				}
20111618Srie 
20121618Srie 				if (badrel) {
20131618Srie 					(void) fprintf(stderr,
20141618Srie 					    MSG_INTL(MSG_ERR_BADREL1), file,
20154734Sab196087 					    conv_reloc_type(mach, reltype,
20164734Sab196087 					    0, &inv_buf));
20170Sstevel@tonic-gate 				}
20180Sstevel@tonic-gate 			}
20190Sstevel@tonic-gate 
20201618Srie 			Elf_reloc_entry_1(0, ELF_DBG_ELFDUMP,
20211618Srie 			    MSG_ORIG(MSG_STR_EMPTY), ehdr->e_machine, type,
20221618Srie 			    rels, relname, symname, 0);
20230Sstevel@tonic-gate 		}
20240Sstevel@tonic-gate 	}
20250Sstevel@tonic-gate }
20260Sstevel@tonic-gate 
20275230Sab196087 
20285230Sab196087 /*
20295230Sab196087  * This value controls which test dyn_test() performs.
20305230Sab196087  */
20315230Sab196087 typedef enum { DYN_TEST_ADDR, DYN_TEST_SIZE, DYN_TEST_ENTSIZE } dyn_test_t;
20325230Sab196087 
20335230Sab196087 /*
20345230Sab196087  * Used by dynamic() to compare the value of a dynamic element against
20355230Sab196087  * the starting address of the section it references.
20365230Sab196087  *
20375230Sab196087  * entry:
20385230Sab196087  *	test_type - Specify which dyn item is being tested.
20395230Sab196087  *	sh_type - SHT_* type value for required section.
20405230Sab196087  *	sec_cache - Cache entry for section, or NULL if the object lacks
20415230Sab196087  *		a section of this type.
20425230Sab196087  *	dyn - Dyn entry to be tested
20435230Sab196087  *	dynsec_cnt - # of dynamic section being examined. The first
20445230Sab196087  *		dynamic section is 1, the next is 2, and so on...
20455230Sab196087  *	ehdr - ELF header for file
20465230Sab196087  *	file - Name of file
20475230Sab196087  */
20485230Sab196087 static void
20495230Sab196087 dyn_test(dyn_test_t test_type, Word sh_type, Cache *sec_cache, Dyn *dyn,
20505230Sab196087     Word dynsec_cnt, Ehdr *ehdr, const char *file)
20515230Sab196087 {
20525230Sab196087 	Conv_inv_buf_t	buf1, buf2;
20535230Sab196087 
20545230Sab196087 	/*
20555230Sab196087 	 * These tests are based around the implicit assumption that
20565230Sab196087 	 * there is only one dynamic section in an object, and also only
20575230Sab196087 	 * one of the sections it references. We have therefore gathered
20585230Sab196087 	 * all of the necessary information to test this in a single pass
20595230Sab196087 	 * over the section headers, which is very efficient. We are not
20605230Sab196087 	 * aware of any case where more than one dynamic section would
20615230Sab196087 	 * be meaningful in an ELF object, so this is a reasonable solution.
20625230Sab196087 	 *
20635230Sab196087 	 * To test multiple dynamic sections correctly would be more
20645230Sab196087 	 * expensive in code and time. We would have to build a data structure
20655230Sab196087 	 * containing all the dynamic elements. Then, we would use the address
20665230Sab196087 	 * to locate the section it references and ensure the section is of
20675230Sab196087 	 * the right type and that the address in the dynamic element is
20685230Sab196087 	 * to the start of the section. Then, we could check the size and
20695230Sab196087 	 * entsize values against those same sections. This is O(n^2), and
20705230Sab196087 	 * also complicated.
20715230Sab196087 	 *
20725230Sab196087 	 * In the highly unlikely case that there is more than one dynamic
20735230Sab196087 	 * section, we only test the first one, and simply allow the values
20745230Sab196087 	 * of the subsequent one to be displayed unchallenged.
20755230Sab196087 	 */
20765230Sab196087 	if (dynsec_cnt != 1)
20775230Sab196087 		return;
20785230Sab196087 
20795230Sab196087 	/*
20805230Sab196087 	 * A DT_ item that references a section address should always find
20815230Sab196087 	 * the section in the file.
20825230Sab196087 	 */
20835230Sab196087 	if (sec_cache == NULL) {
20846299Sab196087 		const char *name;
20856299Sab196087 
20866299Sab196087 		/*
20876299Sab196087 		 * Supply section names instead of section types for
20886299Sab196087 		 * things that reference progbits so that the error
20896299Sab196087 		 * message will make more sense.
20906299Sab196087 		 */
20916299Sab196087 		switch (dyn->d_tag) {
20926299Sab196087 		case DT_INIT:
20936299Sab196087 			name = MSG_ORIG(MSG_ELF_INIT);
20946299Sab196087 			break;
20956299Sab196087 		case DT_FINI:
20966299Sab196087 			name = MSG_ORIG(MSG_ELF_FINI);
20976299Sab196087 			break;
20986299Sab196087 		default:
20996299Sab196087 			name = conv_sec_type(ehdr->e_machine, sh_type,
21006299Sab196087 			    0, &buf1);
21016299Sab196087 			break;
21026299Sab196087 		}
21035230Sab196087 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_DYNNOBCKSEC), file,
21046299Sab196087 		    name, conv_dyn_tag(dyn->d_tag, ehdr->e_machine, 0, &buf2));
21055230Sab196087 		return;
21065230Sab196087 	}
21075230Sab196087 
21085230Sab196087 
21095230Sab196087 	switch (test_type) {
21105230Sab196087 	case DYN_TEST_ADDR:
21115230Sab196087 		/* The section address should match the DT_ item value */
21125230Sab196087 		if (dyn->d_un.d_val != sec_cache->c_shdr->sh_addr)
21135230Sab196087 			(void) fprintf(stderr,
21145230Sab196087 			    MSG_INTL(MSG_ERR_DYNBADADDR), file,
21155230Sab196087 			    conv_dyn_tag(dyn->d_tag, ehdr->e_machine, 0, &buf1),
21165230Sab196087 			    EC_ADDR(dyn->d_un.d_val), sec_cache->c_ndx,
21175230Sab196087 			    sec_cache->c_name,
21185230Sab196087 			    EC_ADDR(sec_cache->c_shdr->sh_addr));
21195230Sab196087 		break;
21205230Sab196087 
21215230Sab196087 	case DYN_TEST_SIZE:
21225230Sab196087 		/* The section size should match the DT_ item value */
21235230Sab196087 		if (dyn->d_un.d_val != sec_cache->c_shdr->sh_size)
21245230Sab196087 			(void) fprintf(stderr,
21255230Sab196087 			    MSG_INTL(MSG_ERR_DYNBADSIZE), file,
21265230Sab196087 			    conv_dyn_tag(dyn->d_tag, ehdr->e_machine, 0, &buf1),
21275230Sab196087 			    EC_XWORD(dyn->d_un.d_val),
21285230Sab196087 			    sec_cache->c_ndx, sec_cache->c_name,
21295230Sab196087 			    EC_XWORD(sec_cache->c_shdr->sh_size));
21305230Sab196087 		break;
21315230Sab196087 
21325230Sab196087 	case DYN_TEST_ENTSIZE:
21335230Sab196087 		/* The sh_entsize value should match the DT_ item value */
21345230Sab196087 		if (dyn->d_un.d_val != sec_cache->c_shdr->sh_entsize)
21355230Sab196087 			(void) fprintf(stderr,
21365230Sab196087 			    MSG_INTL(MSG_ERR_DYNBADENTSIZE), file,
21375230Sab196087 			    conv_dyn_tag(dyn->d_tag, ehdr->e_machine, 0, &buf1),
21385230Sab196087 			    EC_XWORD(dyn->d_un.d_val),
21395230Sab196087 			    sec_cache->c_ndx, sec_cache->c_name,
21405230Sab196087 			    EC_XWORD(sec_cache->c_shdr->sh_entsize));
21415230Sab196087 		break;
21425230Sab196087 	}
21435230Sab196087 }
21445230Sab196087 
21455230Sab196087 
21460Sstevel@tonic-gate /*
21476299Sab196087  * There are some DT_ entries that have corresponding symbols
21486299Sab196087  * (e.g. DT_INIT and _init). It is expected that these items will
21496299Sab196087  * both have the same value if both are present. This routine
21506299Sab196087  * examines the well known symbol tables for such symbols and
21516299Sab196087  * issues warnings for any that don't match.
21526299Sab196087  *
21536299Sab196087  * entry:
21546299Sab196087  *	dyn - Dyn entry to be tested
21556299Sab196087  *	symname - Name of symbol that corresponds to dyn
21566299Sab196087  *	symtab_cache, dynsym_cache, ldynsym_cache - Symbol tables to check
21576299Sab196087  *	cache - Cache of all section headers
21586299Sab196087  *	shnum - # of sections in cache
21596299Sab196087  *	ehdr - ELF header for file
21606299Sab196087  *	file - Name of file
21616299Sab196087  */
21626299Sab196087 static void
21636299Sab196087 dyn_symtest(Dyn *dyn, const char *symname, Cache *symtab_cache,
21646299Sab196087     Cache *dynsym_cache, Cache *ldynsym_cache, Cache *cache,
21656299Sab196087     Word shnum, Ehdr *ehdr, const char *file)
21666299Sab196087 {
21676299Sab196087 	Conv_inv_buf_t	buf;
21686299Sab196087 	int		i;
21696299Sab196087 	Sym		*sym;
21706299Sab196087 	Cache		*_cache;
21716299Sab196087 
21726299Sab196087 	for (i = 0; i < 3; i++) {
21736299Sab196087 		switch (i) {
21746299Sab196087 		case 0:
21756299Sab196087 			_cache = symtab_cache;
21766299Sab196087 			break;
21776299Sab196087 		case 1:
21786299Sab196087 			_cache = dynsym_cache;
21796299Sab196087 			break;
21806299Sab196087 		case 2:
21816299Sab196087 			_cache = ldynsym_cache;
21826299Sab196087 			break;
21836299Sab196087 		}
21846299Sab196087 
21856299Sab196087 		if ((_cache != NULL) &&
21866299Sab196087 		    symlookup(symname, cache, shnum, &sym, _cache, file) &&
21876299Sab196087 		    (sym->st_value != dyn->d_un.d_val))
21886299Sab196087 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_DYNSYMVAL),
21896299Sab196087 			    file, _cache->c_name,
21906299Sab196087 			    conv_dyn_tag(dyn->d_tag, ehdr->e_machine, 0, &buf),
21916299Sab196087 			    symname, EC_ADDR(sym->st_value));
21926299Sab196087 	}
21936299Sab196087 }
21946299Sab196087 
21956299Sab196087 
21966299Sab196087 /*
21970Sstevel@tonic-gate  * Search for and process a .dynamic section.
21980Sstevel@tonic-gate  */
21990Sstevel@tonic-gate static void
22001618Srie dynamic(Cache *cache, Word shnum, Ehdr *ehdr, const char *file)
22010Sstevel@tonic-gate {
22025230Sab196087 	struct {
22036299Sab196087 		Cache	*symtab;
22045230Sab196087 		Cache	*dynstr;
22055230Sab196087 		Cache	*dynsym;
22065230Sab196087 		Cache	*hash;
22075230Sab196087 		Cache	*fini;
22085230Sab196087 		Cache	*fini_array;
22095230Sab196087 		Cache	*init;
22105230Sab196087 		Cache	*init_array;
22115230Sab196087 		Cache	*preinit_array;
22125230Sab196087 		Cache	*rel;
22135230Sab196087 		Cache	*rela;
22145230Sab196087 		Cache	*sunw_cap;
22155230Sab196087 		Cache	*sunw_ldynsym;
22165230Sab196087 		Cache	*sunw_move;
22175230Sab196087 		Cache	*sunw_syminfo;
22185230Sab196087 		Cache	*sunw_symsort;
22195230Sab196087 		Cache	*sunw_tlssort;
22205230Sab196087 		Cache	*sunw_verdef;
22215230Sab196087 		Cache	*sunw_verneed;
22225230Sab196087 		Cache	*sunw_versym;
22235230Sab196087 	} sec;
22245230Sab196087 	Word	dynsec_ndx;
22255230Sab196087 	Word	dynsec_num;
22265230Sab196087 	int	dynsec_cnt;
22271618Srie 	Word	cnt;
22280Sstevel@tonic-gate 
22295230Sab196087 	/*
22305230Sab196087 	 * Make a pass over all the sections, gathering section information
22315230Sab196087 	 * we'll need below.
22325230Sab196087 	 */
22335230Sab196087 	dynsec_num = 0;
22345230Sab196087 	bzero(&sec, sizeof (sec));
22350Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
22365230Sab196087 		Cache	*_cache = &cache[cnt];
22375230Sab196087 
22385230Sab196087 		switch (_cache->c_shdr->sh_type) {
22395230Sab196087 		case SHT_DYNAMIC:
22405230Sab196087 			if (dynsec_num == 0) {
22415230Sab196087 				dynsec_ndx = cnt;
22425230Sab196087 
22435230Sab196087 				/* Does it have a valid string table? */
22445230Sab196087 				(void) stringtbl(cache, 0, cnt, shnum, file,
22455230Sab196087 				    0, 0, &sec.dynstr);
22465230Sab196087 			}
22475230Sab196087 			dynsec_num++;
22485230Sab196087 			break;
22495230Sab196087 
22505230Sab196087 
22515230Sab196087 		case SHT_PROGBITS:
22525230Sab196087 			/*
22535230Sab196087 			 * We want to detect the .init and .fini sections,
22545230Sab196087 			 * if present. These are SHT_PROGBITS, so all we
22555230Sab196087 			 * have to go on is the section name. Normally comparing
22565230Sab196087 			 * names is a bad idea, but there are some special
22575230Sab196087 			 * names (i.e. .init/.fini/.interp) that are very
22585230Sab196087 			 * difficult to use in any other context, and for
22595230Sab196087 			 * these symbols, we do the heuristic match.
22605230Sab196087 			 */
22615230Sab196087 			if (strcmp(_cache->c_name,
22625230Sab196087 			    MSG_ORIG(MSG_ELF_INIT)) == 0) {
22635230Sab196087 				if (sec.init == NULL)
22645230Sab196087 					sec.init = _cache;
22655230Sab196087 			} else if (strcmp(_cache->c_name,
22665230Sab196087 			    MSG_ORIG(MSG_ELF_FINI)) == 0) {
22675230Sab196087 				if (sec.fini == NULL)
22685230Sab196087 					sec.fini = _cache;
22695230Sab196087 			}
22705230Sab196087 			break;
22715230Sab196087 
22725230Sab196087 		case SHT_REL:
22735230Sab196087 			/*
22745230Sab196087 			 * We want the SHT_REL section with the lowest
22755230Sab196087 			 * offset. The linker gathers them together,
22765230Sab196087 			 * and puts the address of the first one
22775230Sab196087 			 * into the DT_REL dynamic element.
22785230Sab196087 			 */
22795230Sab196087 			if ((sec.rel == NULL) ||
22805230Sab196087 			    (_cache->c_shdr->sh_offset <
22815230Sab196087 			    sec.rel->c_shdr->sh_offset))
22825230Sab196087 				sec.rel = _cache;
22835230Sab196087 			break;
22845230Sab196087 
22855230Sab196087 		case SHT_RELA:
22865230Sab196087 			/* RELA is handled just like RELA above */
22875230Sab196087 			if ((sec.rela == NULL) ||
22885230Sab196087 			    (_cache->c_shdr->sh_offset <
22895230Sab196087 			    sec.rela->c_shdr->sh_offset))
22905230Sab196087 				sec.rela = _cache;
22915230Sab196087 			break;
22925230Sab196087 
22935230Sab196087 		/*
22945230Sab196087 		 * The GRAB macro is used for the simple case in which
22955230Sab196087 		 * we simply grab the first section of the desired type.
22965230Sab196087 		 */
22975230Sab196087 #define	GRAB(_sec_type, _sec_field) \
22985230Sab196087 		case _sec_type: \
22995230Sab196087 			if (sec._sec_field == NULL) \
23005230Sab196087 				sec._sec_field = _cache; \
23015230Sab196087 				break
23026299Sab196087 		GRAB(SHT_SYMTAB,	symtab);
23035230Sab196087 		GRAB(SHT_DYNSYM,	dynsym);
23045230Sab196087 		GRAB(SHT_FINI_ARRAY,	fini_array);
23055230Sab196087 		GRAB(SHT_HASH,		hash);
23065230Sab196087 		GRAB(SHT_INIT_ARRAY,	init_array);
23075230Sab196087 		GRAB(SHT_SUNW_move,	sunw_move);
23085230Sab196087 		GRAB(SHT_PREINIT_ARRAY,	preinit_array);
23095230Sab196087 		GRAB(SHT_SUNW_cap,	sunw_cap);
23105230Sab196087 		GRAB(SHT_SUNW_LDYNSYM,	sunw_ldynsym);
23115230Sab196087 		GRAB(SHT_SUNW_syminfo,	sunw_syminfo);
23125230Sab196087 		GRAB(SHT_SUNW_symsort,	sunw_symsort);
23135230Sab196087 		GRAB(SHT_SUNW_tlssort,	sunw_tlssort);
23145230Sab196087 		GRAB(SHT_SUNW_verdef,	sunw_verdef);
23155230Sab196087 		GRAB(SHT_SUNW_verneed,	sunw_verneed);
23165230Sab196087 		GRAB(SHT_SUNW_versym,	sunw_versym);
23175230Sab196087 #undef GRAB
23185230Sab196087 		}
23195230Sab196087 	}
23205230Sab196087 
23215230Sab196087 	/*
23225230Sab196087 	 * If no dynamic section, return immediately. If more than one
23235230Sab196087 	 * dynamic section, then something odd is going on and an error
23245230Sab196087 	 * is in order, but then continue on and display them all.
23255230Sab196087 	 */
23265230Sab196087 	if (dynsec_num == 0)
23275230Sab196087 		return;
23285230Sab196087 	if (dynsec_num > 1)
23295230Sab196087 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_MULTDYN),
23305230Sab196087 		    file, EC_WORD(dynsec_num));
23315230Sab196087 
23325230Sab196087 
23335230Sab196087 	dynsec_cnt = 0;
23345230Sab196087 	for (cnt = dynsec_ndx; (cnt < shnum) && (dynsec_cnt < dynsec_num);
23355230Sab196087 	    cnt++) {
23361618Srie 		Dyn	*dyn;
23371618Srie 		ulong_t	numdyn;
23383850Sab196087 		int	ndx, end_ndx;
23391618Srie 		Cache	*_cache = &cache[cnt], *strsec;
23401618Srie 		Shdr	*shdr = _cache->c_shdr;
23415230Sab196087 		int	dumped = 0;
23420Sstevel@tonic-gate 
23430Sstevel@tonic-gate 		if (shdr->sh_type != SHT_DYNAMIC)
23440Sstevel@tonic-gate 			continue;
23455230Sab196087 		dynsec_cnt++;
23460Sstevel@tonic-gate 
23470Sstevel@tonic-gate 		/*
23481618Srie 		 * Verify the associated string table section.
23490Sstevel@tonic-gate 		 */
23501618Srie 		if (stringtbl(cache, 0, cnt, shnum, file, 0, 0, &strsec) == 0)
23510Sstevel@tonic-gate 			continue;
23521618Srie 
23533466Srie 		if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
23543466Srie 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
23553466Srie 			    file, _cache->c_name);
23563466Srie 			continue;
23573466Srie 		}
23583466Srie 		if (_cache->c_data == NULL)
23593466Srie 			continue;
23603466Srie 
23610Sstevel@tonic-gate 		numdyn = shdr->sh_size / shdr->sh_entsize;
23621618Srie 		dyn = (Dyn *)_cache->c_data->d_buf;
23630Sstevel@tonic-gate 
23645230Sab196087 		/*
23655230Sab196087 		 * We expect the REL/RELA entries to reference the reloc
23665230Sab196087 		 * section with the lowest address. However, this is
23675230Sab196087 		 * not true for dumped objects. Detect if this object has
23685230Sab196087 		 * been dumped so that we can skip the reloc address test
23695230Sab196087 		 * in that case.
23705230Sab196087 		 */
23715230Sab196087 		for (ndx = 0; ndx < numdyn; dyn++, ndx++) {
23725230Sab196087 			if (dyn->d_tag == DT_FLAGS_1) {
23735230Sab196087 				dumped = (dyn->d_un.d_val & DF_1_CONFALT) != 0;
23745230Sab196087 				break;
23755230Sab196087 			}
23765230Sab196087 		}
23775230Sab196087 		dyn = (Dyn *)_cache->c_data->d_buf;
23785230Sab196087 
23791618Srie 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
23801618Srie 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_DYNAMIC), _cache->c_name);
23810Sstevel@tonic-gate 
23821618Srie 		Elf_dyn_title(0);
23830Sstevel@tonic-gate 
23841618Srie 		for (ndx = 0; ndx < numdyn; dyn++, ndx++) {
23854734Sab196087 			union {
23866206Sab196087 				Conv_inv_buf_t		inv;
23874734Sab196087 				Conv_dyn_flag_buf_t	flag;
23884734Sab196087 				Conv_dyn_flag1_buf_t	flag1;
23894734Sab196087 				Conv_dyn_posflag1_buf_t	posflag1;
23904734Sab196087 				Conv_dyn_feature1_buf_t	feature1;
23914734Sab196087 			} c_buf;
23925230Sab196087 			const char	*name = NULL;
23930Sstevel@tonic-gate 
23940Sstevel@tonic-gate 			/*
23950Sstevel@tonic-gate 			 * Print the information numerically, and if possible
23965230Sab196087 			 * as a string. If a string is available, name is
23975230Sab196087 			 * set to reference it.
23985230Sab196087 			 *
23995230Sab196087 			 * Also, take this opportunity to sanity check
24005230Sab196087 			 * the values of DT elements. In the code above,
24015230Sab196087 			 * we gathered information on sections that are
24025230Sab196087 			 * referenced by the dynamic section. Here, we
24035230Sab196087 			 * compare the attributes of those sections to
24045230Sab196087 			 * the DT_ items that reference them and report
24055230Sab196087 			 * on inconsistencies.
24065230Sab196087 			 *
24075230Sab196087 			 * Things not currently tested that could be improved
24085230Sab196087 			 * in later revisions include:
24095230Sab196087 			 *	- We don't check PLT or GOT related items
24105230Sab196087 			 *	- We don't handle computing the lengths of
24115230Sab196087 			 *		relocation arrays. To handle this
24125230Sab196087 			 *		requires examining data that spans
24135230Sab196087 			 *		across sections, in a contiguous span
24145230Sab196087 			 *		within a single segment.
24155230Sab196087 			 *	- DT_VERDEFNUM and DT_VERNEEDNUM can't be
24165230Sab196087 			 *		verified without parsing the sections.
24175230Sab196087 			 *	- We don't handle DT_SUNW_SYMSZ, which would
24185230Sab196087 			 *		be the sum of the lengths of .dynsym and
24195230Sab196087 			 *		.SUNW_ldynsym
24205230Sab196087 			 *	- DT_SUNW_STRPAD can't be verified other than
24215230Sab196087 			 *		to check that it's not larger than
24225230Sab196087 			 *		the string table.
24235230Sab196087 			 *	- Some items come in "all or none" clusters
24245230Sab196087 			 *		that give an address, element size,
24255230Sab196087 			 *		and data length in bytes. We don't
24265230Sab196087 			 *		verify that there are no missing items
24275230Sab196087 			 *		in such groups.
24280Sstevel@tonic-gate 			 */
24293850Sab196087 			switch (dyn->d_tag) {
24303850Sab196087 			case DT_NULL:
24313850Sab196087 				/*
24323850Sab196087 				 * Special case: DT_NULLs can come in groups
24333850Sab196087 				 * that we prefer to reduce to a single line.
24343850Sab196087 				 */
24353850Sab196087 				end_ndx = ndx;
24363850Sab196087 				while ((end_ndx < (numdyn - 1)) &&
24374433Sab196087 				    ((dyn + 1)->d_tag == DT_NULL)) {
24383850Sab196087 					dyn++;
24393850Sab196087 					end_ndx++;
24403850Sab196087 				}
24413850Sab196087 				Elf_dyn_null_entry(0, dyn, ndx, end_ndx);
24423850Sab196087 				ndx = end_ndx;
24433850Sab196087 				continue;
24443850Sab196087 
24453850Sab196087 			/*
24465230Sab196087 			 * String items all reference the dynstr. The string()
24475230Sab196087 			 * function does the necessary sanity checking.
24483850Sab196087 			 */
24493850Sab196087 			case DT_NEEDED:
24503850Sab196087 			case DT_SONAME:
24513850Sab196087 			case DT_FILTER:
24523850Sab196087 			case DT_AUXILIARY:
24533850Sab196087 			case DT_CONFIG:
24543850Sab196087 			case DT_RPATH:
24553850Sab196087 			case DT_RUNPATH:
24563850Sab196087 			case DT_USED:
24573850Sab196087 			case DT_DEPAUDIT:
24583850Sab196087 			case DT_AUDIT:
24593850Sab196087 			case DT_SUNW_AUXILIARY:
24603850Sab196087 			case DT_SUNW_FILTER:
24611618Srie 				name = string(_cache, ndx, strsec,
24621618Srie 				    file, dyn->d_un.d_ptr);
24633850Sab196087 				break;
24643850Sab196087 
24653850Sab196087 			case DT_FLAGS:
24664734Sab196087 				name = conv_dyn_flag(dyn->d_un.d_val,
24674734Sab196087 				    0, &c_buf.flag);
24683850Sab196087 				break;
24693850Sab196087 			case DT_FLAGS_1:
24705088Sab196087 				name = conv_dyn_flag1(dyn->d_un.d_val, 0,
24714734Sab196087 				    &c_buf.flag1);
24723850Sab196087 				break;
24733850Sab196087 			case DT_POSFLAG_1:
24744734Sab196087 				name = conv_dyn_posflag1(dyn->d_un.d_val, 0,
24754734Sab196087 				    &c_buf.posflag1);
24763850Sab196087 				break;
24773850Sab196087 			case DT_FEATURE_1:
24784734Sab196087 				name = conv_dyn_feature1(dyn->d_un.d_val, 0,
24794734Sab196087 				    &c_buf.feature1);
24803850Sab196087 				break;
24813850Sab196087 			case DT_DEPRECATED_SPARC_REGISTER:
24821618Srie 				name = MSG_INTL(MSG_STR_DEPRECATED);
24833850Sab196087 				break;
24845230Sab196087 
24856206Sab196087 			case DT_SUNW_LDMACH:
24866206Sab196087 				name = conv_ehdr_mach((Half)dyn->d_un.d_val, 0,
24876206Sab196087 				    &c_buf.inv);
24886206Sab196087 				break;
24896206Sab196087 
24905230Sab196087 			/*
24915230Sab196087 			 * Cases below this point are strictly sanity checking,
24925230Sab196087 			 * and do not generate a name string. The TEST_ macros
24935230Sab196087 			 * are used to hide the boilerplate arguments neeeded
24945230Sab196087 			 * by dyn_test().
24955230Sab196087 			 */
24965230Sab196087 #define	TEST_ADDR(_sh_type, _sec_field) \
24975230Sab196087 				dyn_test(DYN_TEST_ADDR, _sh_type, \
24985230Sab196087 				    sec._sec_field, dyn, dynsec_cnt, ehdr, file)
24995230Sab196087 #define	TEST_SIZE(_sh_type, _sec_field) \
25005230Sab196087 				dyn_test(DYN_TEST_SIZE, _sh_type, \
25015230Sab196087 				    sec._sec_field, dyn, dynsec_cnt, ehdr, file)
25025230Sab196087 #define	TEST_ENTSIZE(_sh_type, _sec_field) \
25035230Sab196087 				dyn_test(DYN_TEST_ENTSIZE, _sh_type, \
25045230Sab196087 				    sec._sec_field, dyn, dynsec_cnt, ehdr, file)
25055230Sab196087 
25065230Sab196087 			case DT_FINI:
25076299Sab196087 				dyn_symtest(dyn, MSG_ORIG(MSG_SYM_FINI),
25086299Sab196087 				    sec.symtab, sec.dynsym, sec.sunw_ldynsym,
25096299Sab196087 				    cache, shnum, ehdr, file);
25105230Sab196087 				TEST_ADDR(SHT_PROGBITS, fini);
25115230Sab196087 				break;
25125230Sab196087 
25135230Sab196087 			case DT_FINI_ARRAY:
25145230Sab196087 				TEST_ADDR(SHT_FINI_ARRAY, fini_array);
25155230Sab196087 				break;
25165230Sab196087 
25175230Sab196087 			case DT_FINI_ARRAYSZ:
25185230Sab196087 				TEST_SIZE(SHT_FINI_ARRAY, fini_array);
25195230Sab196087 				break;
25205230Sab196087 
25215230Sab196087 			case DT_HASH:
25225230Sab196087 				TEST_ADDR(SHT_HASH, hash);
25235230Sab196087 				break;
25245230Sab196087 
25255230Sab196087 			case DT_INIT:
25266299Sab196087 				dyn_symtest(dyn, MSG_ORIG(MSG_SYM_INIT),
25276299Sab196087 				    sec.symtab, sec.dynsym, sec.sunw_ldynsym,
25286299Sab196087 				    cache, shnum, ehdr, file);
25295230Sab196087 				TEST_ADDR(SHT_PROGBITS, init);
25305230Sab196087 				break;
25315230Sab196087 
25325230Sab196087 			case DT_INIT_ARRAY:
25335230Sab196087 				TEST_ADDR(SHT_INIT_ARRAY, init_array);
25345230Sab196087 				break;
25355230Sab196087 
25365230Sab196087 			case DT_INIT_ARRAYSZ:
25375230Sab196087 				TEST_SIZE(SHT_INIT_ARRAY, init_array);
25385230Sab196087 				break;
25395230Sab196087 
25405230Sab196087 			case DT_MOVEENT:
25415230Sab196087 				TEST_ENTSIZE(SHT_SUNW_move, sunw_move);
25425230Sab196087 				break;
25435230Sab196087 
25445230Sab196087 			case DT_MOVESZ:
25455230Sab196087 				TEST_SIZE(SHT_SUNW_move, sunw_move);
25465230Sab196087 				break;
25475230Sab196087 
25485230Sab196087 			case DT_MOVETAB:
25495230Sab196087 				TEST_ADDR(SHT_SUNW_move, sunw_move);
25505230Sab196087 				break;
25515230Sab196087 
25525230Sab196087 			case DT_PREINIT_ARRAY:
25535230Sab196087 				TEST_ADDR(SHT_PREINIT_ARRAY, preinit_array);
25545230Sab196087 				break;
25555230Sab196087 
25565230Sab196087 			case DT_PREINIT_ARRAYSZ:
25575230Sab196087 				TEST_SIZE(SHT_PREINIT_ARRAY, preinit_array);
25585230Sab196087 				break;
25595230Sab196087 
25605230Sab196087 			case DT_REL:
25615230Sab196087 				if (!dumped)
25625230Sab196087 					TEST_ADDR(SHT_REL, rel);
25635230Sab196087 				break;
25645230Sab196087 
25655230Sab196087 			case DT_RELENT:
25665230Sab196087 				TEST_ENTSIZE(SHT_REL, rel);
25675230Sab196087 				break;
25685230Sab196087 
25695230Sab196087 			case DT_RELA:
25705230Sab196087 				if (!dumped)
25715230Sab196087 					TEST_ADDR(SHT_RELA, rela);
25725230Sab196087 				break;
25735230Sab196087 
25745230Sab196087 			case DT_RELAENT:
25755230Sab196087 				TEST_ENTSIZE(SHT_RELA, rela);
25765230Sab196087 				break;
25775230Sab196087 
25785230Sab196087 			case DT_STRTAB:
25795230Sab196087 				TEST_ADDR(SHT_STRTAB, dynstr);
25803850Sab196087 				break;
25815230Sab196087 
25825230Sab196087 			case DT_STRSZ:
25835230Sab196087 				TEST_SIZE(SHT_STRTAB, dynstr);
25845230Sab196087 				break;
25855230Sab196087 
25865230Sab196087 			case DT_SUNW_CAP:
25875230Sab196087 				TEST_ADDR(SHT_SUNW_cap, sunw_cap);
25885230Sab196087 				break;
25895230Sab196087 
25905230Sab196087 			case DT_SUNW_SYMTAB:
25915230Sab196087 				TEST_ADDR(SHT_SUNW_LDYNSYM, sunw_ldynsym);
25925230Sab196087 				break;
25935230Sab196087 
25945230Sab196087 			case DT_SYMENT:
25955230Sab196087 				TEST_ENTSIZE(SHT_DYNSYM, dynsym);
25965230Sab196087 				break;
25975230Sab196087 
25985230Sab196087 			case DT_SYMINENT:
25995230Sab196087 				TEST_ENTSIZE(SHT_SUNW_syminfo, sunw_syminfo);
26005230Sab196087 				break;
26015230Sab196087 
26025230Sab196087 			case DT_SYMINFO:
26035230Sab196087 				TEST_ADDR(SHT_SUNW_syminfo, sunw_syminfo);
26045230Sab196087 				break;
26055230Sab196087 
26065230Sab196087 			case DT_SYMINSZ:
26075230Sab196087 				TEST_SIZE(SHT_SUNW_syminfo, sunw_syminfo);
26085230Sab196087 				break;
26095230Sab196087 
26105230Sab196087 			case DT_SYMTAB:
26115230Sab196087 				TEST_ADDR(SHT_DYNSYM, dynsym);
26125230Sab196087 				break;
26135230Sab196087 
26145230Sab196087 			case DT_SUNW_SORTENT:
26155230Sab196087 				/*
26165230Sab196087 				 * This entry is related to both the symsort and
26175230Sab196087 				 * tlssort sections.
26185230Sab196087 				 */
26195230Sab196087 				{
26205230Sab196087 					int test_tls =
26215230Sab196087 					    (sec.sunw_tlssort != NULL);
26225230Sab196087 					int test_sym =
26235230Sab196087 					    (sec.sunw_symsort != NULL) ||
26245230Sab196087 					    !test_tls;
26255230Sab196087 					if (test_sym)
26265230Sab196087 						TEST_ENTSIZE(SHT_SUNW_symsort,
26275230Sab196087 						    sunw_symsort);
26285230Sab196087 					if (test_tls)
26295230Sab196087 						TEST_ENTSIZE(SHT_SUNW_tlssort,
26305230Sab196087 						    sunw_tlssort);
26315230Sab196087 				}
26325230Sab196087 				break;
26335230Sab196087 
26345230Sab196087 
26355230Sab196087 			case DT_SUNW_SYMSORT:
26365230Sab196087 				TEST_ADDR(SHT_SUNW_symsort, sunw_symsort);
26375230Sab196087 				break;
26385230Sab196087 
26395230Sab196087 			case DT_SUNW_SYMSORTSZ:
26405230Sab196087 				TEST_SIZE(SHT_SUNW_symsort, sunw_symsort);
26415230Sab196087 				break;
26425230Sab196087 
26435230Sab196087 			case DT_SUNW_TLSSORT:
26445230Sab196087 				TEST_ADDR(SHT_SUNW_tlssort, sunw_tlssort);
26455230Sab196087 				break;
26465230Sab196087 
26475230Sab196087 			case DT_SUNW_TLSSORTSZ:
26485230Sab196087 				TEST_SIZE(SHT_SUNW_tlssort, sunw_tlssort);
26495230Sab196087 				break;
26505230Sab196087 
26515230Sab196087 			case DT_VERDEF:
26525230Sab196087 				TEST_ADDR(SHT_SUNW_verdef, sunw_verdef);
26535230Sab196087 				break;
26545230Sab196087 
26555230Sab196087 			case DT_VERNEED:
26565230Sab196087 				TEST_ADDR(SHT_SUNW_verneed, sunw_verneed);
26575230Sab196087 				break;
26585230Sab196087 
26595230Sab196087 			case DT_VERSYM:
26605230Sab196087 				TEST_ADDR(SHT_SUNW_versym, sunw_versym);
26615230Sab196087 				break;
26625230Sab196087 #undef TEST_ADDR
26635230Sab196087 #undef TEST_SIZE
26645230Sab196087 #undef TEST_ENTSIZE
26653850Sab196087 			}
26660Sstevel@tonic-gate 
26675230Sab196087 			if (name == NULL)
26685230Sab196087 				name = MSG_ORIG(MSG_STR_EMPTY);
26691618Srie 			Elf_dyn_entry(0, dyn, ndx, name, ehdr->e_machine);
26700Sstevel@tonic-gate 		}
26710Sstevel@tonic-gate 	}
26720Sstevel@tonic-gate }
26730Sstevel@tonic-gate 
26740Sstevel@tonic-gate /*
26750Sstevel@tonic-gate  * Search for and process a MOVE section.
26760Sstevel@tonic-gate  */
26770Sstevel@tonic-gate static void
26784168Sab196087 move(Cache *cache, Word shnum, const char *file, uint_t flags)
26790Sstevel@tonic-gate {
26801618Srie 	Word		cnt;
26811618Srie 	const char	*fmt = 0;
26820Sstevel@tonic-gate 
26830Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
26841618Srie 		Word	movenum, symnum, ndx;
26851618Srie 		Sym	*syms;
26861618Srie 		Cache	*_cache = &cache[cnt];
26871618Srie 		Shdr	*shdr = _cache->c_shdr;
26881618Srie 		Cache	*symsec, *strsec;
26891618Srie 		Move	*move;
26900Sstevel@tonic-gate 
26910Sstevel@tonic-gate 		if (shdr->sh_type != SHT_SUNW_move)
26920Sstevel@tonic-gate 			continue;
26935411Sab196087 		if (!match(MATCH_F_ALL, _cache->c_name, cnt, shdr->sh_type))
26940Sstevel@tonic-gate 			continue;
26950Sstevel@tonic-gate 
26960Sstevel@tonic-gate 		/*
26970Sstevel@tonic-gate 		 * Determine the move data and number.
26980Sstevel@tonic-gate 		 */
26990Sstevel@tonic-gate 		if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
27000Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
27010Sstevel@tonic-gate 			    file, _cache->c_name);
27020Sstevel@tonic-gate 			continue;
27030Sstevel@tonic-gate 		}
27043466Srie 		if (_cache->c_data == NULL)
27053466Srie 			continue;
27063466Srie 
27071618Srie 		move = (Move *)_cache->c_data->d_buf;
27081618Srie 		movenum = shdr->sh_size / shdr->sh_entsize;
27090Sstevel@tonic-gate 
27100Sstevel@tonic-gate 		/*
27111618Srie 		 * Get the data buffer for the associated symbol table and
27121618Srie 		 * string table.
27130Sstevel@tonic-gate 		 */
27141618Srie 		if (stringtbl(cache, 1, cnt, shnum, file,
27151618Srie 		    &symnum, &symsec, &strsec) == 0)
27161618Srie 			return;
27171618Srie 
27181618Srie 		syms = (Sym *)symsec->c_data->d_buf;
27190Sstevel@tonic-gate 
27201618Srie 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
27211618Srie 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_MOVE), _cache->c_name);
27221618Srie 		dbg_print(0, MSG_INTL(MSG_MOVE_TITLE));
27230Sstevel@tonic-gate 
27241618Srie 		if (fmt == 0)
27251618Srie 			fmt = MSG_INTL(MSG_MOVE_ENTRY);
27260Sstevel@tonic-gate 
27271618Srie 		for (ndx = 0; ndx < movenum; move++, ndx++) {
27281618Srie 			const char	*symname;
27291618Srie 			char		index[MAXNDXSIZE], section[BUFSIZ];
27301618Srie 			Word		symndx, shndx;
27311618Srie 			Sym		*sym;
27320Sstevel@tonic-gate 
27330Sstevel@tonic-gate 			/*
27340Sstevel@tonic-gate 			 * Check for null entries
27350Sstevel@tonic-gate 			 */
27361618Srie 			if ((move->m_info == 0) && (move->m_value == 0) &&
27371618Srie 			    (move->m_poffset == 0) && (move->m_repeat == 0) &&
27381618Srie 			    (move->m_stride == 0)) {
27391618Srie 				dbg_print(0, fmt, MSG_ORIG(MSG_STR_EMPTY),
27401618Srie 				    EC_XWORD(move->m_poffset), 0, 0, 0,
27411618Srie 				    EC_LWORD(0), MSG_ORIG(MSG_STR_EMPTY));
27420Sstevel@tonic-gate 				continue;
27430Sstevel@tonic-gate 			}
27441618Srie 			if (((symndx = ELF_M_SYM(move->m_info)) == 0) ||
27451618Srie 			    (symndx >= symnum)) {
27460Sstevel@tonic-gate 				(void) fprintf(stderr,
27470Sstevel@tonic-gate 				    MSG_INTL(MSG_ERR_BADMINFO), file,
27481618Srie 				    _cache->c_name, EC_XWORD(move->m_info));
27491618Srie 
27501618Srie 				(void) snprintf(index, MAXNDXSIZE,
27511618Srie 				    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(symndx));
27521618Srie 				dbg_print(0, fmt, index,
27531618Srie 				    EC_XWORD(move->m_poffset),
27541618Srie 				    ELF_M_SIZE(move->m_info), move->m_repeat,
27551618Srie 				    move->m_stride, move->m_value,
27560Sstevel@tonic-gate 				    MSG_INTL(MSG_STR_UNKNOWN));
27570Sstevel@tonic-gate 				continue;
27580Sstevel@tonic-gate 			}
27590Sstevel@tonic-gate 
27601618Srie 			symname = relsymname(cache, _cache, strsec,
27617463SRod.Evans@Sun.COM 			    symndx, symnum, ndx, syms, section, BUFSIZ, file);
27621618Srie 			sym = (Sym *)(syms + symndx);
27630Sstevel@tonic-gate 
27640Sstevel@tonic-gate 			/*
27650Sstevel@tonic-gate 			 * Additional sanity check.
27660Sstevel@tonic-gate 			 */
27671618Srie 			shndx = sym->st_shndx;
27680Sstevel@tonic-gate 			if (!((shndx == SHN_COMMON) ||
27690Sstevel@tonic-gate 			    (((shndx >= 1) && (shndx <= shnum)) &&
27701618Srie 			    (cache[shndx].c_shdr)->sh_type == SHT_NOBITS))) {
27710Sstevel@tonic-gate 				(void) fprintf(stderr,
27721618Srie 				    MSG_INTL(MSG_ERR_BADSYM2), file,
27736206Sab196087 				    _cache->c_name, EC_WORD(symndx),
27746206Sab196087 				    demangle(symname, flags));
27750Sstevel@tonic-gate 			}
27760Sstevel@tonic-gate 
27771618Srie 			(void) snprintf(index, MAXNDXSIZE,
27781618Srie 			    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(symndx));
27791618Srie 			dbg_print(0, fmt, index, EC_XWORD(move->m_poffset),
27801618Srie 			    ELF_M_SIZE(move->m_info), move->m_repeat,
27811618Srie 			    move->m_stride, move->m_value,
27821618Srie 			    demangle(symname, flags));
27830Sstevel@tonic-gate 		}
27840Sstevel@tonic-gate 	}
27850Sstevel@tonic-gate }
27860Sstevel@tonic-gate 
27870Sstevel@tonic-gate /*
27886635Sab196087  * Callback function for use with conv_str_to_c_literal() below.
27896635Sab196087  */
27906635Sab196087 /*ARGSUSED2*/
27916635Sab196087 static void
27926635Sab196087 c_literal_cb(const void *ptr, size_t size, void *uvalue)
27936635Sab196087 {
27946635Sab196087 	(void) fwrite(ptr, size, 1, stdout);
27956635Sab196087 }
27966635Sab196087 
27976635Sab196087 /*
27980Sstevel@tonic-gate  * Traverse a note section analyzing each note information block.
27990Sstevel@tonic-gate  * The data buffers size is used to validate references before they are made,
28000Sstevel@tonic-gate  * and is decremented as each element is processed.
28010Sstevel@tonic-gate  */
28020Sstevel@tonic-gate void
28036635Sab196087 note_entry(Cache *cache, Word *data, size_t size, Ehdr *ehdr, const char *file)
28040Sstevel@tonic-gate {
28056635Sab196087 	size_t		bsize = size;
28066635Sab196087 	int		cnt = 0;
28076635Sab196087 	int		is_corenote;
28086635Sab196087 	int		do_swap;
28096635Sab196087 	Conv_inv_buf_t	inv_buf;
28106635Sab196087 
28116635Sab196087 	do_swap =  _elf_sys_encoding() != ehdr->e_ident[EI_DATA];
28121618Srie 
28130Sstevel@tonic-gate 	/*
28140Sstevel@tonic-gate 	 * Print out a single `note' information block.
28150Sstevel@tonic-gate 	 */
28160Sstevel@tonic-gate 	while (size > 0) {
28171618Srie 		size_t	namesz, descsz, type, pad, noteoff;
28180Sstevel@tonic-gate 
28190Sstevel@tonic-gate 		noteoff = bsize - size;
28200Sstevel@tonic-gate 		/*
28210Sstevel@tonic-gate 		 * Make sure we can at least reference the 3 initial entries
28220Sstevel@tonic-gate 		 * (4-byte words) of the note information block.
28230Sstevel@tonic-gate 		 */
28241618Srie 		if (size >= (sizeof (Word) * 3))
28251618Srie 			size -= (sizeof (Word) * 3);
28260Sstevel@tonic-gate 		else {
28271618Srie 			(void) fprintf(stderr, MSG_INTL(MSG_NOTE_BADDATASZ),
28281618Srie 			    file, cache->c_name, EC_WORD(noteoff));
28290Sstevel@tonic-gate 			return;
28300Sstevel@tonic-gate 		}
28310Sstevel@tonic-gate 
28320Sstevel@tonic-gate 		/*
28330Sstevel@tonic-gate 		 * Make sure any specified name string can be referenced.
28340Sstevel@tonic-gate 		 */
28350Sstevel@tonic-gate 		if ((namesz = *data++) != 0) {
28360Sstevel@tonic-gate 			if (size >= namesz)
28370Sstevel@tonic-gate 				size -= namesz;
28380Sstevel@tonic-gate 			else {
28390Sstevel@tonic-gate 				(void) fprintf(stderr,
28401618Srie 				    MSG_INTL(MSG_NOTE_BADNMSZ), file,
28411618Srie 				    cache->c_name, EC_WORD(noteoff),
28421618Srie 				    EC_WORD(namesz));
28430Sstevel@tonic-gate 				return;
28440Sstevel@tonic-gate 			}
28450Sstevel@tonic-gate 		}
28461618Srie 
28470Sstevel@tonic-gate 		/*
28480Sstevel@tonic-gate 		 * Make sure any specified descriptor can be referenced.
28490Sstevel@tonic-gate 		 */
28500Sstevel@tonic-gate 		if ((descsz = *data++) != 0) {
28510Sstevel@tonic-gate 			/*
28520Sstevel@tonic-gate 			 * If namesz isn't a 4-byte multiple, account for any
28530Sstevel@tonic-gate 			 * padding that must exist before the descriptor.
28540Sstevel@tonic-gate 			 */
28551618Srie 			if ((pad = (namesz & (sizeof (Word) - 1))) != 0) {
28561618Srie 				pad = sizeof (Word) - pad;
28570Sstevel@tonic-gate 				size -= pad;
28580Sstevel@tonic-gate 			}
28590Sstevel@tonic-gate 			if (size >= descsz)
28600Sstevel@tonic-gate 				size -= descsz;
28610Sstevel@tonic-gate 			else {
28620Sstevel@tonic-gate 				(void) fprintf(stderr,
28631618Srie 				    MSG_INTL(MSG_NOTE_BADDESZ), file,
28641618Srie 				    cache->c_name, EC_WORD(noteoff),
28651618Srie 				    EC_WORD(namesz));
28660Sstevel@tonic-gate 				return;
28670Sstevel@tonic-gate 			}
28680Sstevel@tonic-gate 		}
28690Sstevel@tonic-gate 
28700Sstevel@tonic-gate 		type = *data++;
28710Sstevel@tonic-gate 
28726635Sab196087 		/*
28736635Sab196087 		 * Is this a Solaris core note? Such notes all have
28746635Sab196087 		 * the name "CORE".
28756635Sab196087 		 */
28766635Sab196087 		is_corenote = (ehdr->e_type == ET_CORE) &&
28776635Sab196087 		    (namesz == (MSG_STR_CORE_SIZE + 1)) &&
28786635Sab196087 		    (strncmp(MSG_ORIG(MSG_STR_CORE), (char *)data,
28796635Sab196087 		    MSG_STR_CORE_SIZE + 1) == 0);
28806635Sab196087 
28811618Srie 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
28826635Sab196087 		dbg_print(0, MSG_INTL(MSG_FMT_NOTEENTNDX), EC_WORD(cnt));
28836635Sab196087 		cnt++;
28841618Srie 		dbg_print(0, MSG_ORIG(MSG_NOTE_NAMESZ), EC_WORD(namesz));
28856635Sab196087 		dbg_print(0, MSG_ORIG(MSG_NOTE_DESCSZ), EC_WORD(descsz));
28866635Sab196087 
28876635Sab196087 		if (is_corenote)
28886635Sab196087 			dbg_print(0, MSG_ORIG(MSG_NOTE_TYPE_STR),
28896635Sab196087 			    conv_cnote_type(type, 0, &inv_buf));
28906635Sab196087 		else
28916635Sab196087 			dbg_print(0, MSG_ORIG(MSG_NOTE_TYPE), EC_WORD(type));
28920Sstevel@tonic-gate 		if (namesz) {
28930Sstevel@tonic-gate 			char	*name = (char *)data;
28940Sstevel@tonic-gate 
28956635Sab196087 
28966635Sab196087 			dbg_print(0, MSG_ORIG(MSG_NOTE_NAME));
28970Sstevel@tonic-gate 			/*
28986635Sab196087 			 * The name string can contain embedded 'null'
28996635Sab196087 			 * bytes and/or unprintable characters. Also,
29006635Sab196087 			 * the final NULL is documented in the ELF ABI
29016635Sab196087 			 * as being included in the namesz. So, display
29026635Sab196087 			 * the name using C literal string notation, and
29036635Sab196087 			 * include the terminating NULL in the output.
29046635Sab196087 			 * We don't show surrounding double quotes, as
29056635Sab196087 			 * that implies the termination that we are showing
29066635Sab196087 			 * explicitly.
29070Sstevel@tonic-gate 			 */
29086635Sab196087 			(void) fwrite(MSG_ORIG(MSG_STR_8SP),
29096635Sab196087 			    MSG_STR_8SP_SIZE, 1, stdout);
29106635Sab196087 			conv_str_to_c_literal(name, namesz, c_literal_cb, NULL);
29110Sstevel@tonic-gate 			name = name + ((namesz + (sizeof (Word) - 1)) &
29120Sstevel@tonic-gate 			    ~(sizeof (Word) - 1));
29130Sstevel@tonic-gate 			/* LINTED */
29140Sstevel@tonic-gate 			data = (Word *)name;
29151618Srie 			dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
29160Sstevel@tonic-gate 		}
29170Sstevel@tonic-gate 
29180Sstevel@tonic-gate 		/*
29190Sstevel@tonic-gate 		 * If multiple information blocks exist within a .note section
29200Sstevel@tonic-gate 		 * account for any padding that must exist before the next
29210Sstevel@tonic-gate 		 * information block.
29220Sstevel@tonic-gate 		 */
29231618Srie 		if ((pad = (descsz & (sizeof (Word) - 1))) != 0) {
29241618Srie 			pad = sizeof (Word) - pad;
29250Sstevel@tonic-gate 			if (size > pad)
29260Sstevel@tonic-gate 				size -= pad;
29270Sstevel@tonic-gate 		}
29280Sstevel@tonic-gate 
29290Sstevel@tonic-gate 		if (descsz) {
29306635Sab196087 			int		hexdump = 1;
29316635Sab196087 			const char	*desc = (const char *)data;
29320Sstevel@tonic-gate 
29330Sstevel@tonic-gate 			/*
29346635Sab196087 			 * If this is a core note, let the corenote()
29356635Sab196087 			 * function handle it.
29360Sstevel@tonic-gate 			 */
29376635Sab196087 			if (is_corenote) {
29386635Sab196087 				/* We only issue the bad arch error once */
29396635Sab196087 				static int	badnote_done = 0;
29406635Sab196087 				corenote_ret_t	corenote_ret;
29416635Sab196087 
29426635Sab196087 				corenote_ret = corenote(ehdr->e_machine,
29436635Sab196087 				    do_swap, type, desc, descsz);
29446635Sab196087 				switch (corenote_ret) {
29456635Sab196087 				case CORENOTE_R_OK:
29466635Sab196087 					hexdump = 0;
29476635Sab196087 					break;
29486635Sab196087 				case CORENOTE_R_BADDATA:
29496635Sab196087 					(void) fprintf(stderr,
29506635Sab196087 					    MSG_INTL(MSG_NOTE_BADCOREDATA),
29516635Sab196087 					    file);
29526635Sab196087 					break;
29536635Sab196087 				case CORENOTE_R_BADARCH:
29546635Sab196087 					if (badnote_done)
29556635Sab196087 						break;
29566635Sab196087 					(void) fprintf(stderr,
29576635Sab196087 					    MSG_INTL(MSG_NOTE_BADCOREARCH),
29586635Sab196087 					    file,
29596635Sab196087 					    conv_ehdr_mach(ehdr->e_machine,
29606635Sab196087 					    0, &inv_buf));
29616635Sab196087 					break;
29620Sstevel@tonic-gate 				}
29630Sstevel@tonic-gate 			}
29646635Sab196087 
29656635Sab196087 			/*
29666635Sab196087 			 * The default thing when we don't understand
29676635Sab196087 			 * the note data is to display it as hex bytes.
29686635Sab196087 			 */
29696635Sab196087 			if (hexdump) {
29706635Sab196087 				dbg_print(0, MSG_ORIG(MSG_NOTE_DESC));
29716635Sab196087 				dump_hex_bytes(desc, descsz, 8, 4, 4);
29720Sstevel@tonic-gate 			}
29736635Sab196087 			desc += descsz + pad;
29746635Sab196087 
29750Sstevel@tonic-gate 			/* LINTED */
29760Sstevel@tonic-gate 			data = (Word *)desc;
29770Sstevel@tonic-gate 		}
29780Sstevel@tonic-gate 	}
29790Sstevel@tonic-gate }
29800Sstevel@tonic-gate 
29810Sstevel@tonic-gate /*
29826635Sab196087  * Search for and process .note sections.
29836635Sab196087  *
29846635Sab196087  * Returns the number of note sections seen.
29850Sstevel@tonic-gate  */
29866635Sab196087 static Word
29876635Sab196087 note(Cache *cache, Word shnum, Ehdr *ehdr, const char *file)
29880Sstevel@tonic-gate {
29896635Sab196087 	Word	cnt, note_cnt = 0;
29900Sstevel@tonic-gate 
29910Sstevel@tonic-gate 	/*
29920Sstevel@tonic-gate 	 * Otherwise look for any .note sections.
29930Sstevel@tonic-gate 	 */
29940Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
29951618Srie 		Cache	*_cache = &cache[cnt];
29961618Srie 		Shdr	*shdr = _cache->c_shdr;
29970Sstevel@tonic-gate 
29980Sstevel@tonic-gate 		if (shdr->sh_type != SHT_NOTE)
29990Sstevel@tonic-gate 			continue;
30006635Sab196087 		note_cnt++;
30015411Sab196087 		if (!match(MATCH_F_ALL, _cache->c_name, cnt, shdr->sh_type))
30020Sstevel@tonic-gate 			continue;
30030Sstevel@tonic-gate 
30040Sstevel@tonic-gate 		/*
30050Sstevel@tonic-gate 		 * As these sections are often hand rolled, make sure they're
30065230Sab196087 		 * properly aligned before proceeding, and issue an error
30075230Sab196087 		 * as necessary.
30085230Sab196087 		 *
30095230Sab196087 		 * Note that we will continue on to display the note even
30105230Sab196087 		 * if it has bad alignment. We can do this safely, because
30115230Sab196087 		 * libelf knows the alignment required for SHT_NOTE, and
30125230Sab196087 		 * takes steps to deliver a properly aligned buffer to us
30135230Sab196087 		 * even if the actual file is misaligned.
30140Sstevel@tonic-gate 		 */
30155230Sab196087 		if (shdr->sh_offset & (sizeof (Word) - 1))
30160Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADALIGN),
30170Sstevel@tonic-gate 			    file, _cache->c_name);
30185230Sab196087 
30193466Srie 		if (_cache->c_data == NULL)
30203466Srie 			continue;
30210Sstevel@tonic-gate 
30221618Srie 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
30231618Srie 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_NOTE), _cache->c_name);
30240Sstevel@tonic-gate 		note_entry(_cache, (Word *)_cache->c_data->d_buf,
30250Sstevel@tonic-gate 		/* LINTED */
30266635Sab196087 		    (Word)_cache->c_data->d_size, ehdr, file);
30270Sstevel@tonic-gate 	}
30286635Sab196087 
30296635Sab196087 	return (note_cnt);
30300Sstevel@tonic-gate }
30310Sstevel@tonic-gate 
30321618Srie /*
30331618Srie  * Determine an individual hash entry.  This may be the initial hash entry,
30341618Srie  * or an associated chain entry.
30351618Srie  */
30361618Srie static void
30371618Srie hash_entry(Cache *refsec, Cache *strsec, const char *hsecname, Word hashndx,
30381618Srie     Word symndx, Word symn, Sym *syms, const char *file, ulong_t bkts,
30391618Srie     uint_t flags, int chain)
30401618Srie {
30411618Srie 	Sym		*sym;
30421618Srie 	const char	*symname, *str;
30431618Srie 	char		_bucket[MAXNDXSIZE], _symndx[MAXNDXSIZE];
30441618Srie 	ulong_t		nbkt, nhash;
30451618Srie 
30461618Srie 	if (symndx > symn) {
30471618Srie 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_HSBADSYMNDX), file,
30481618Srie 		    EC_WORD(symndx), EC_WORD(hashndx));
30491618Srie 		symname = MSG_INTL(MSG_STR_UNKNOWN);
30501618Srie 	} else {
30511618Srie 		sym = (Sym *)(syms + symndx);
30521618Srie 		symname = string(refsec, symndx, strsec, file, sym->st_name);
30531618Srie 	}
30541618Srie 
30551618Srie 	if (chain == 0) {
30561618Srie 		(void) snprintf(_bucket, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INTEGER),
30571618Srie 		    hashndx);
30581618Srie 		str = (const char *)_bucket;
30591618Srie 	} else
30601618Srie 		str = MSG_ORIG(MSG_STR_EMPTY);
30611618Srie 
30621618Srie 	(void) snprintf(_symndx, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INDEX2),
30631618Srie 	    EC_WORD(symndx));
30641618Srie 	dbg_print(0, MSG_ORIG(MSG_FMT_HASH_INFO), str, _symndx,
30651618Srie 	    demangle(symname, flags));
30661618Srie 
30671618Srie 	/*
30681618Srie 	 * Determine if this string is in the correct bucket.
30691618Srie 	 */
30701618Srie 	nhash = elf_hash(symname);
30711618Srie 	nbkt = nhash % bkts;
30721618Srie 
30731618Srie 	if (nbkt != hashndx) {
30741618Srie 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADHASH), file,
30751618Srie 		    hsecname, symname, EC_WORD(hashndx), nbkt);
30761618Srie 	}
30771618Srie }
30780Sstevel@tonic-gate 
30790Sstevel@tonic-gate #define	MAXCOUNT	500
30800Sstevel@tonic-gate 
30810Sstevel@tonic-gate static void
30824168Sab196087 hash(Cache *cache, Word shnum, const char *file, uint_t flags)
30830Sstevel@tonic-gate {
30840Sstevel@tonic-gate 	static int	count[MAXCOUNT];
30851618Srie 	Word		cnt;
30860Sstevel@tonic-gate 	ulong_t		ndx, bkts;
30870Sstevel@tonic-gate 	char		number[MAXNDXSIZE];
30880Sstevel@tonic-gate 
30890Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
30900Sstevel@tonic-gate 		uint_t		*hash, *chain;
30910Sstevel@tonic-gate 		Cache		*_cache = &cache[cnt];
30921618Srie 		Shdr		*sshdr, *hshdr = _cache->c_shdr;
30931618Srie 		char		*ssecname, *hsecname = _cache->c_name;
30941618Srie 		Sym		*syms;
30951618Srie 		Word		symn;
30960Sstevel@tonic-gate 
30971618Srie 		if (hshdr->sh_type != SHT_HASH)
30980Sstevel@tonic-gate 			continue;
30990Sstevel@tonic-gate 
31000Sstevel@tonic-gate 		/*
31010Sstevel@tonic-gate 		 * Determine the hash table data and size.
31020Sstevel@tonic-gate 		 */
31031618Srie 		if ((hshdr->sh_entsize == 0) || (hshdr->sh_size == 0)) {
31040Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
31051618Srie 			    file, hsecname);
31060Sstevel@tonic-gate 			continue;
31070Sstevel@tonic-gate 		}
31083466Srie 		if (_cache->c_data == NULL)
31093466Srie 			continue;
31103466Srie 
31110Sstevel@tonic-gate 		hash = (uint_t *)_cache->c_data->d_buf;
31120Sstevel@tonic-gate 		bkts = *hash;
31130Sstevel@tonic-gate 		chain = hash + 2 + bkts;
31140Sstevel@tonic-gate 		hash += 2;
31150Sstevel@tonic-gate 
31160Sstevel@tonic-gate 		/*
31170Sstevel@tonic-gate 		 * Get the data buffer for the associated symbol table.
31180Sstevel@tonic-gate 		 */
31191618Srie 		if ((hshdr->sh_link == 0) || (hshdr->sh_link >= shnum)) {
31200Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
31211618Srie 			    file, hsecname, EC_WORD(hshdr->sh_link));
31220Sstevel@tonic-gate 			continue;
31230Sstevel@tonic-gate 		}
31241618Srie 
31251618Srie 		_cache = &cache[hshdr->sh_link];
31261618Srie 		ssecname = _cache->c_name;
31271618Srie 
31283466Srie 		if (_cache->c_data == NULL)
31293466Srie 			continue;
31303466Srie 
31313466Srie 		if ((syms = (Sym *)_cache->c_data->d_buf) == NULL) {
31320Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
31331618Srie 			    file, ssecname);
31340Sstevel@tonic-gate 			continue;
31350Sstevel@tonic-gate 		}
31360Sstevel@tonic-gate 
31371618Srie 		sshdr = _cache->c_shdr;
31381618Srie 		/* LINTED */
31391618Srie 		symn = (Word)(sshdr->sh_size / sshdr->sh_entsize);
31401618Srie 
31410Sstevel@tonic-gate 		/*
31420Sstevel@tonic-gate 		 * Get the associated string table section.
31430Sstevel@tonic-gate 		 */
31441618Srie 		if ((sshdr->sh_link == 0) || (sshdr->sh_link >= shnum)) {
31450Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
31461618Srie 			    file, ssecname, EC_WORD(sshdr->sh_link));
31470Sstevel@tonic-gate 			continue;
31480Sstevel@tonic-gate 		}
31490Sstevel@tonic-gate 
31501618Srie 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
31511618Srie 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_HASH), hsecname);
31521618Srie 		dbg_print(0, MSG_INTL(MSG_ELF_HASH_INFO));
31530Sstevel@tonic-gate 
31540Sstevel@tonic-gate 		/*
31550Sstevel@tonic-gate 		 * Loop through the hash buckets, printing the appropriate
31560Sstevel@tonic-gate 		 * symbols.
31570Sstevel@tonic-gate 		 */
31580Sstevel@tonic-gate 		for (ndx = 0; ndx < bkts; ndx++, hash++) {
31591618Srie 			Word	_ndx, _cnt;
31600Sstevel@tonic-gate 
31610Sstevel@tonic-gate 			if (*hash == 0) {
31620Sstevel@tonic-gate 				count[0]++;
31630Sstevel@tonic-gate 				continue;
31640Sstevel@tonic-gate 			}
31650Sstevel@tonic-gate 
31661618Srie 			hash_entry(_cache, &cache[sshdr->sh_link], hsecname,
31671618Srie 			    ndx, *hash, symn, syms, file, bkts, flags, 0);
31680Sstevel@tonic-gate 
31690Sstevel@tonic-gate 			/*
31700Sstevel@tonic-gate 			 * Determine if any other symbols are chained to this
31710Sstevel@tonic-gate 			 * bucket.
31720Sstevel@tonic-gate 			 */
31730Sstevel@tonic-gate 			_ndx = chain[*hash];
31740Sstevel@tonic-gate 			_cnt = 1;
31750Sstevel@tonic-gate 			while (_ndx) {
31761618Srie 				hash_entry(_cache, &cache[sshdr->sh_link],
31771618Srie 				    hsecname, ndx, _ndx, symn, syms, file,
31781618Srie 				    bkts, flags, 1);
31790Sstevel@tonic-gate 				_ndx = chain[_ndx];
31800Sstevel@tonic-gate 				_cnt++;
31810Sstevel@tonic-gate 			}
31820Sstevel@tonic-gate 
31830Sstevel@tonic-gate 			if (_cnt >= MAXCOUNT) {
31840Sstevel@tonic-gate 				(void) fprintf(stderr,
31851324Srie 				    MSG_INTL(MSG_HASH_OVERFLW), file,
31861618Srie 				    _cache->c_name, EC_WORD(ndx),
31871618Srie 				    EC_WORD(_cnt));
31880Sstevel@tonic-gate 			} else
31890Sstevel@tonic-gate 				count[_cnt]++;
31900Sstevel@tonic-gate 		}
31910Sstevel@tonic-gate 		break;
31920Sstevel@tonic-gate 	}
31930Sstevel@tonic-gate 
31940Sstevel@tonic-gate 	/*
31950Sstevel@tonic-gate 	 * Print out the count information.
31960Sstevel@tonic-gate 	 */
31970Sstevel@tonic-gate 	bkts = cnt = 0;
31981618Srie 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
31991618Srie 
32000Sstevel@tonic-gate 	for (ndx = 0; ndx < MAXCOUNT; ndx++) {
32011618Srie 		Word	_cnt;
32020Sstevel@tonic-gate 
32030Sstevel@tonic-gate 		if ((_cnt = count[ndx]) == 0)
32040Sstevel@tonic-gate 			continue;
32050Sstevel@tonic-gate 
32061618Srie 		(void) snprintf(number, MAXNDXSIZE,
32071618Srie 		    MSG_ORIG(MSG_FMT_INTEGER), _cnt);
32081618Srie 		dbg_print(0, MSG_INTL(MSG_ELF_HASH_BKTS1), number,
32091618Srie 		    EC_WORD(ndx));
32100Sstevel@tonic-gate 		bkts += _cnt;
32111618Srie 		cnt += (Word)(ndx * _cnt);
32120Sstevel@tonic-gate 	}
32130Sstevel@tonic-gate 	if (cnt) {
32140Sstevel@tonic-gate 		(void) snprintf(number, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INTEGER),
32151618Srie 		    bkts);
32161618Srie 		dbg_print(0, MSG_INTL(MSG_ELF_HASH_BKTS2), number,
32171618Srie 		    EC_WORD(cnt));
32180Sstevel@tonic-gate 	}
32190Sstevel@tonic-gate }
32200Sstevel@tonic-gate 
32210Sstevel@tonic-gate static void
32224168Sab196087 group(Cache *cache, Word shnum, const char *file, uint_t flags)
32230Sstevel@tonic-gate {
32241618Srie 	Word	scnt;
32250Sstevel@tonic-gate 
32261618Srie 	for (scnt = 1; scnt < shnum; scnt++) {
32271618Srie 		Cache	*_cache = &cache[scnt];
32281618Srie 		Shdr	*shdr = _cache->c_shdr;
32291618Srie 		Word	*grpdata, gcnt, grpcnt, symnum, unknown;
32301618Srie 		Cache	*symsec, *strsec;
32311618Srie 		Sym	*syms, *sym;
32321618Srie 		char	flgstrbuf[MSG_GRP_COMDAT_SIZE + 10];
32330Sstevel@tonic-gate 
32340Sstevel@tonic-gate 		if (shdr->sh_type != SHT_GROUP)
32350Sstevel@tonic-gate 			continue;
32365411Sab196087 		if (!match(MATCH_F_ALL, _cache->c_name, scnt, shdr->sh_type))
32370Sstevel@tonic-gate 			continue;
32383466Srie 		if ((_cache->c_data == NULL) ||
32393466Srie 		    ((grpdata = (Word *)_cache->c_data->d_buf) == NULL))
32400Sstevel@tonic-gate 			continue;
32411618Srie 		grpcnt = shdr->sh_size / sizeof (Word);
32421618Srie 
32431618Srie 		/*
32441618Srie 		 * Get the data buffer for the associated symbol table and
32451618Srie 		 * string table.
32461618Srie 		 */
32471618Srie 		if (stringtbl(cache, 1, scnt, shnum, file,
32481618Srie 		    &symnum, &symsec, &strsec) == 0)
32491618Srie 			return;
32501618Srie 
32511618Srie 		syms = symsec->c_data->d_buf;
32521618Srie 
32531618Srie 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
32541618Srie 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_GRP), _cache->c_name);
32551618Srie 		dbg_print(0, MSG_INTL(MSG_GRP_TITLE));
32561618Srie 
32571618Srie 		/*
32581618Srie 		 * The first element of the group defines the group.  The
32591618Srie 		 * associated symbol is defined by the sh_link field.
32601618Srie 		 */
32611618Srie 		if ((shdr->sh_info == SHN_UNDEF) || (shdr->sh_info > symnum)) {
32621618Srie 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHINFO),
32631618Srie 			    file, _cache->c_name, EC_WORD(shdr->sh_info));
32641618Srie 			return;
32650Sstevel@tonic-gate 		}
32660Sstevel@tonic-gate 
32671618Srie 		(void) strcpy(flgstrbuf, MSG_ORIG(MSG_STR_OSQBRKT));
32681618Srie 		if (grpdata[0] & GRP_COMDAT) {
32691618Srie 			(void) strcat(flgstrbuf, MSG_ORIG(MSG_GRP_COMDAT));
32700Sstevel@tonic-gate 		}
32711618Srie 		if ((unknown = (grpdata[0] & ~GRP_COMDAT)) != 0) {
32721618Srie 			size_t	len = strlen(flgstrbuf);
32731618Srie 
32741618Srie 			(void) snprintf(&flgstrbuf[len],
32751618Srie 			    (MSG_GRP_COMDAT_SIZE + 10 - len),
32761618Srie 			    MSG_ORIG(MSG_GRP_UNKNOWN), unknown);
32770Sstevel@tonic-gate 		}
32781618Srie 		(void) strcat(flgstrbuf, MSG_ORIG(MSG_STR_CSQBRKT));
32791618Srie 		sym = (Sym *)(syms + shdr->sh_info);
32800Sstevel@tonic-gate 
32811618Srie 		dbg_print(0, MSG_INTL(MSG_GRP_SIGNATURE), flgstrbuf,
32821618Srie 		    demangle(string(_cache, 0, strsec, file, sym->st_name),
32831618Srie 		    flags));
32841618Srie 
32851618Srie 		for (gcnt = 1; gcnt < grpcnt; gcnt++) {
32860Sstevel@tonic-gate 			char		index[MAXNDXSIZE];
32871618Srie 			const char	*name;
32880Sstevel@tonic-gate 
32890Sstevel@tonic-gate 			(void) snprintf(index, MAXNDXSIZE,
32901618Srie 			    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(gcnt));
32911618Srie 
32921618Srie 			if (grpdata[gcnt] >= shnum)
32931618Srie 				name = MSG_INTL(MSG_GRP_INVALSCN);
32941618Srie 			else
32951618Srie 				name = cache[grpdata[gcnt]].c_name;
32961618Srie 
32971618Srie 			(void) printf(MSG_ORIG(MSG_GRP_ENTRY), index, name,
32984433Sab196087 			    EC_XWORD(grpdata[gcnt]));
32990Sstevel@tonic-gate 		}
33000Sstevel@tonic-gate 	}
33010Sstevel@tonic-gate }
33020Sstevel@tonic-gate 
33030Sstevel@tonic-gate static void
33047463SRod.Evans@Sun.COM got(Cache *cache, Word shnum, Ehdr *ehdr, const char *file)
33050Sstevel@tonic-gate {
33065230Sab196087 	Cache		*gotcache = NULL, *symtab = NULL;
33071618Srie 	Addr		gotbgn, gotend;
33081618Srie 	Shdr		*gotshdr;
33091618Srie 	Word		cnt, gotents, gotndx;
33100Sstevel@tonic-gate 	size_t		gentsize;
33110Sstevel@tonic-gate 	Got_info	*gottable;
33120Sstevel@tonic-gate 	char		*gotdata;
33131618Srie 	Sym		*gotsym;
33141618Srie 	Xword		gotsymaddr;
33156206Sab196087 	uint_t		sys_encoding;
33160Sstevel@tonic-gate 
33170Sstevel@tonic-gate 	/*
33181324Srie 	 * First, find the got.
33190Sstevel@tonic-gate 	 */
33200Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
33215230Sab196087 		if (strncmp(cache[cnt].c_name, MSG_ORIG(MSG_ELF_GOT),
33221324Srie 		    MSG_ELF_GOT_SIZE) == 0) {
33235230Sab196087 			gotcache = &cache[cnt];
33240Sstevel@tonic-gate 			break;
33250Sstevel@tonic-gate 		}
33260Sstevel@tonic-gate 	}
33275230Sab196087 	if (gotcache == NULL)
33280Sstevel@tonic-gate 		return;
33291324Srie 
33301324Srie 	/*
33311324Srie 	 * A got section within a relocatable object is suspicious.
33321324Srie 	 */
33331324Srie 	if (ehdr->e_type == ET_REL) {
33341324Srie 		(void) fprintf(stderr, MSG_INTL(MSG_GOT_UNEXPECTED), file,
33355230Sab196087 		    gotcache->c_name);
33361324Srie 	}
33371324Srie 
33381618Srie 	gotshdr = gotcache->c_shdr;
33390Sstevel@tonic-gate 	if (gotshdr->sh_size == 0) {
33400Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
33410Sstevel@tonic-gate 		    file, gotcache->c_name);
33420Sstevel@tonic-gate 		return;
33430Sstevel@tonic-gate 	}
33441618Srie 
33451618Srie 	gotbgn = gotshdr->sh_addr;
33460Sstevel@tonic-gate 	gotend = gotbgn + gotshdr->sh_size;
33470Sstevel@tonic-gate 
33480Sstevel@tonic-gate 	/*
33491618Srie 	 * Some architectures don't properly set the sh_entsize for the GOT
33501618Srie 	 * table.  If it's not set, default to a size of a pointer.
33510Sstevel@tonic-gate 	 */
33521618Srie 	if ((gentsize = gotshdr->sh_entsize) == 0)
33531618Srie 		gentsize = sizeof (Xword);
33541618Srie 
33553466Srie 	if (gotcache->c_data == NULL)
33563466Srie 		return;
33573466Srie 
33580Sstevel@tonic-gate 	/* LINTED */
33591618Srie 	gotents = (Word)(gotshdr->sh_size / gentsize);
33600Sstevel@tonic-gate 	gotdata = gotcache->c_data->d_buf;
33610Sstevel@tonic-gate 
33620Sstevel@tonic-gate 	if ((gottable = calloc(gotents, sizeof (Got_info))) == 0) {
33630Sstevel@tonic-gate 		int err = errno;
33641618Srie 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC), file,
33651618Srie 		    strerror(err));
33660Sstevel@tonic-gate 		return;
33670Sstevel@tonic-gate 	}
33680Sstevel@tonic-gate 
33690Sstevel@tonic-gate 	/*
33700Sstevel@tonic-gate 	 * Now we scan through all the sections looking for any relocations
33710Sstevel@tonic-gate 	 * that may be against the GOT.  Since these may not be isolated to a
33720Sstevel@tonic-gate 	 * .rel[a].got section we check them all.
33730Sstevel@tonic-gate 	 * While scanning sections save the symbol table entry (a symtab
33740Sstevel@tonic-gate 	 * overriding a dynsym) so that we can lookup _GLOBAL_OFFSET_TABLE_.
33750Sstevel@tonic-gate 	 */
33760Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
33771618Srie 		Word		type, symnum;
33781618Srie 		Xword		relndx, relnum, relsize;
33791618Srie 		void		*rels;
33801618Srie 		Sym		*syms;
33811618Srie 		Cache		*symsec, *strsec;
33821618Srie 		Cache		*_cache = &cache[cnt];
33831618Srie 		Shdr		*shdr;
33840Sstevel@tonic-gate 
33851618Srie 		shdr = _cache->c_shdr;
33861618Srie 		type = shdr->sh_type;
33870Sstevel@tonic-gate 
33881618Srie 		if ((symtab == 0) && (type == SHT_DYNSYM)) {
33890Sstevel@tonic-gate 			symtab = _cache;
33900Sstevel@tonic-gate 			continue;
33910Sstevel@tonic-gate 		}
33921618Srie 		if (type == SHT_SYMTAB) {
33930Sstevel@tonic-gate 			symtab = _cache;
33940Sstevel@tonic-gate 			continue;
33950Sstevel@tonic-gate 		}
33961618Srie 		if ((type != SHT_RELA) && (type != SHT_REL))
33970Sstevel@tonic-gate 			continue;
33980Sstevel@tonic-gate 
33990Sstevel@tonic-gate 		/*
34001618Srie 		 * Decide entry size.
34010Sstevel@tonic-gate 		 */
34021618Srie 		if (((relsize = shdr->sh_entsize) == 0) ||
34031618Srie 		    (relsize > shdr->sh_size)) {
34041618Srie 			if (type == SHT_RELA)
34051618Srie 				relsize = sizeof (Rela);
34061618Srie 			else
34071618Srie 				relsize = sizeof (Rel);
34080Sstevel@tonic-gate 		}
34090Sstevel@tonic-gate 
34100Sstevel@tonic-gate 		/*
34111618Srie 		 * Determine the number of relocations available.
34120Sstevel@tonic-gate 		 */
34131618Srie 		if (shdr->sh_size == 0) {
34141618Srie 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
34151618Srie 			    file, _cache->c_name);
34160Sstevel@tonic-gate 			continue;
34170Sstevel@tonic-gate 		}
34183466Srie 		if (_cache->c_data == NULL)
34193466Srie 			continue;
34203466Srie 
34211618Srie 		rels = _cache->c_data->d_buf;
34221618Srie 		relnum = shdr->sh_size / relsize;
34230Sstevel@tonic-gate 
34241618Srie 		/*
34251618Srie 		 * Get the data buffer for the associated symbol table and
34261618Srie 		 * string table.
34271618Srie 		 */
34281618Srie 		if (stringtbl(cache, 1, cnt, shnum, file,
34291618Srie 		    &symnum, &symsec, &strsec) == 0)
34301618Srie 			continue;
34311618Srie 
34321618Srie 		syms = symsec->c_data->d_buf;
34331618Srie 
34341618Srie 		/*
34351618Srie 		 * Loop through the relocation entries.
34361618Srie 		 */
34371618Srie 		for (relndx = 0; relndx < relnum; relndx++,
34381618Srie 		    rels = (void *)((char *)rels + relsize)) {
34391618Srie 			char		section[BUFSIZ];
34401618Srie 			Addr		offset;
34410Sstevel@tonic-gate 			Got_info	*gip;
34421618Srie 			Word		symndx, reltype;
34431618Srie 			Rela		*rela;
34441618Srie 			Rel		*rel;
34450Sstevel@tonic-gate 
34461618Srie 			/*
34471618Srie 			 * Unravel the relocation.
34481618Srie 			 */
34491618Srie 			if (type == SHT_RELA) {
34501618Srie 				rela = (Rela *)rels;
34511618Srie 				symndx = ELF_R_SYM(rela->r_info);
34526206Sab196087 				reltype = ELF_R_TYPE(rela->r_info,
34536206Sab196087 				    ehdr->e_machine);
34541618Srie 				offset = rela->r_offset;
34550Sstevel@tonic-gate 			} else {
34561618Srie 				rel = (Rel *)rels;
34571618Srie 				symndx = ELF_R_SYM(rel->r_info);
34586206Sab196087 				reltype = ELF_R_TYPE(rel->r_info,
34596206Sab196087 				    ehdr->e_machine);
34601618Srie 				offset = rel->r_offset;
34610Sstevel@tonic-gate 			}
34620Sstevel@tonic-gate 
34630Sstevel@tonic-gate 			/*
34640Sstevel@tonic-gate 			 * Only pay attention to relocations against the GOT.
34650Sstevel@tonic-gate 			 */
34664146Sab196087 			if ((offset < gotbgn) || (offset >= gotend))
34670Sstevel@tonic-gate 				continue;
34680Sstevel@tonic-gate 
34690Sstevel@tonic-gate 			/* LINTED */
34701618Srie 			gotndx = (Word)((offset - gotbgn) /
34710Sstevel@tonic-gate 			    gotshdr->sh_entsize);
34720Sstevel@tonic-gate 			gip = &gottable[gotndx];
34731618Srie 
34741618Srie 			if (gip->g_reltype != 0) {
34750Sstevel@tonic-gate 				(void) fprintf(stderr,
34760Sstevel@tonic-gate 				    MSG_INTL(MSG_GOT_MULTIPLE), file,
34771618Srie 				    EC_WORD(gotndx), EC_ADDR(offset));
34780Sstevel@tonic-gate 				continue;
34790Sstevel@tonic-gate 			}
34800Sstevel@tonic-gate 
34811618Srie 			if (symndx)
34821618Srie 				gip->g_symname = relsymname(cache, _cache,
34831618Srie 				    strsec, symndx, symnum, relndx, syms,
34847463SRod.Evans@Sun.COM 				    section, BUFSIZ, file);
34851618Srie 			gip->g_reltype = reltype;
34861618Srie 			gip->g_rel = rels;
34870Sstevel@tonic-gate 		}
34880Sstevel@tonic-gate 	}
34890Sstevel@tonic-gate 
34906299Sab196087 	if (symlookup(MSG_ORIG(MSG_SYM_GOT), cache, shnum, &gotsym, symtab,
34911618Srie 	    file))
34921618Srie 		gotsymaddr = gotsym->st_value;
34930Sstevel@tonic-gate 	else
34941618Srie 		gotsymaddr = gotbgn;
34950Sstevel@tonic-gate 
34961618Srie 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
34971618Srie 	dbg_print(0, MSG_INTL(MSG_ELF_SCN_GOT), gotcache->c_name);
34981618Srie 	Elf_got_title(0);
34990Sstevel@tonic-gate 
35006206Sab196087 	sys_encoding = _elf_sys_encoding();
35010Sstevel@tonic-gate 	for (gotndx = 0; gotndx < gotents; gotndx++) {
35020Sstevel@tonic-gate 		Got_info	*gip;
35030Sstevel@tonic-gate 		Sword		gindex;
35041618Srie 		Addr		gaddr;
35051618Srie 		Xword		gotentry;
35060Sstevel@tonic-gate 
35070Sstevel@tonic-gate 		gip = &gottable[gotndx];
35080Sstevel@tonic-gate 
35090Sstevel@tonic-gate 		gaddr = gotbgn + (gotndx * gentsize);
35101618Srie 		gindex = (Sword)(gaddr - gotsymaddr) / (Sword)gentsize;
35110Sstevel@tonic-gate 
35121618Srie 		if (gentsize == sizeof (Word))
35130Sstevel@tonic-gate 			/* LINTED */
35141618Srie 			gotentry = (Xword)(*((Word *)(gotdata) + gotndx));
35150Sstevel@tonic-gate 		else
35160Sstevel@tonic-gate 			/* LINTED */
35171618Srie 			gotentry = *((Xword *)(gotdata) + gotndx);
35180Sstevel@tonic-gate 
35191618Srie 		Elf_got_entry(0, gindex, gaddr, gotentry, ehdr->e_machine,
35206206Sab196087 		    ehdr->e_ident[EI_DATA], sys_encoding,
35211618Srie 		    gip->g_reltype, gip->g_rel, gip->g_symname);
35220Sstevel@tonic-gate 	}
35230Sstevel@tonic-gate 	free(gottable);
35240Sstevel@tonic-gate }
35250Sstevel@tonic-gate 
35260Sstevel@tonic-gate void
35270Sstevel@tonic-gate checksum(Elf *elf)
35280Sstevel@tonic-gate {
35291618Srie 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
35301618Srie 	dbg_print(0, MSG_INTL(MSG_STR_CHECKSUM), elf_checksum(elf));
35310Sstevel@tonic-gate }
35320Sstevel@tonic-gate 
35334242Sab196087 /*
35344242Sab196087  * This variable is used by regular() to communicate the address of
35354242Sab196087  * the section header cache to sort_shdr_ndx_arr(). Unfortunately,
35364242Sab196087  * the qsort() interface does not include a userdata argument by which
35374242Sab196087  * such arbitrary data can be passed, so we are stuck using global data.
35384242Sab196087  */
35394242Sab196087 static Cache *sort_shdr_ndx_arr_cache;
35404242Sab196087 
35414242Sab196087 
35424242Sab196087 /*
35434242Sab196087  * Used with qsort() to sort the section indices so that they can be
35444242Sab196087  * used to access the section headers in order of increasing data offset.
35454242Sab196087  *
35464242Sab196087  * entry:
35474242Sab196087  *	sort_shdr_ndx_arr_cache - Contains address of
35484242Sab196087  *		section header cache.
35494242Sab196087  *	v1, v2 - Point at elements of sort_shdr_bits array to be compared.
35504242Sab196087  *
35514242Sab196087  * exit:
35524242Sab196087  *	Returns -1 (less than), 0 (equal) or 1 (greater than).
35534242Sab196087  */
35544242Sab196087 static int
35554242Sab196087 sort_shdr_ndx_arr(const void *v1, const void *v2)
35564242Sab196087 {
35574242Sab196087 	Cache	*cache1 = sort_shdr_ndx_arr_cache + *((size_t *)v1);
35584242Sab196087 	Cache	*cache2 = sort_shdr_ndx_arr_cache + *((size_t *)v2);
35594242Sab196087 
35604242Sab196087 	if (cache1->c_shdr->sh_offset < cache2->c_shdr->sh_offset)
35614242Sab196087 		return (-1);
35624242Sab196087 
35634242Sab196087 	if (cache1->c_shdr->sh_offset > cache2->c_shdr->sh_offset)
35644242Sab196087 		return (1);
35654242Sab196087 
35664242Sab196087 	return (0);
35674242Sab196087 }
35684242Sab196087 
35694242Sab196087 
35704665Sab196087 static int
35714665Sab196087 shdr_cache(const char *file, Elf *elf, Ehdr *ehdr, size_t shstrndx,
35727463SRod.Evans@Sun.COM     size_t shnum, Cache **cache_ret, Word flags)
35730Sstevel@tonic-gate {
35740Sstevel@tonic-gate 	Elf_Scn		*scn;
35750Sstevel@tonic-gate 	Elf_Data	*data;
35764665Sab196087 	size_t		ndx;
35774665Sab196087 	Shdr		*nameshdr;
35780Sstevel@tonic-gate 	char		*names = 0;
35790Sstevel@tonic-gate 	Cache		*cache, *_cache;
35804242Sab196087 	size_t		*shdr_ndx_arr, shdr_ndx_arr_cnt;
35810Sstevel@tonic-gate 
35820Sstevel@tonic-gate 
35830Sstevel@tonic-gate 	/*
35840Sstevel@tonic-gate 	 * Obtain the .shstrtab data buffer to provide the required section
35850Sstevel@tonic-gate 	 * name strings.
35860Sstevel@tonic-gate 	 */
35874156Sab196087 	if (shstrndx == SHN_UNDEF) {
35884156Sab196087 		/*
35894156Sab196087 		 * It is rare, but legal, for an object to lack a
35904156Sab196087 		 * header string table section.
35914156Sab196087 		 */
35924156Sab196087 		names = NULL;
35934156Sab196087 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_NOSHSTRSEC), file);
35944156Sab196087 	} else if ((scn = elf_getscn(elf, shstrndx)) == NULL) {
35950Sstevel@tonic-gate 		failure(file, MSG_ORIG(MSG_ELF_GETSCN));
35960Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SHDR),
35970Sstevel@tonic-gate 		    EC_XWORD(shstrndx));
35981618Srie 
35990Sstevel@tonic-gate 	} else if ((data = elf_getdata(scn, NULL)) == NULL) {
36000Sstevel@tonic-gate 		failure(file, MSG_ORIG(MSG_ELF_GETDATA));
36010Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_DATA),
36020Sstevel@tonic-gate 		    EC_XWORD(shstrndx));
36031618Srie 
36041618Srie 	} else if ((nameshdr = elf_getshdr(scn)) == NULL) {
36050Sstevel@tonic-gate 		failure(file, MSG_ORIG(MSG_ELF_GETSHDR));
36060Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN),
36073862Srie 		    EC_WORD(elf_ndxscn(scn)));
36081618Srie 
36091618Srie 	} else if ((names = data->d_buf) == 0)
36100Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_SHSTRNULL), file);
36110Sstevel@tonic-gate 
36120Sstevel@tonic-gate 	/*
36133862Srie 	 * Allocate a cache to maintain a descriptor for each section.
36140Sstevel@tonic-gate 	 */
36154665Sab196087 	if ((*cache_ret = cache = malloc(shnum * sizeof (Cache))) == NULL) {
36160Sstevel@tonic-gate 		int err = errno;
36170Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
36180Sstevel@tonic-gate 		    file, strerror(err));
36194665Sab196087 		return (0);
36200Sstevel@tonic-gate 	}
36210Sstevel@tonic-gate 
36221618Srie 	*cache = cache_init;
36230Sstevel@tonic-gate 	_cache = cache;
36240Sstevel@tonic-gate 	_cache++;
36250Sstevel@tonic-gate 
36263862Srie 	/*
36274242Sab196087 	 * Allocate an array that will hold the section index for
36284242Sab196087 	 * each section that has data in the ELF file:
36294242Sab196087 	 *
36304242Sab196087 	 *	- Is not a NOBITS section
36314242Sab196087 	 *	- Data has non-zero length
36324242Sab196087 	 *
36334242Sab196087 	 * Note that shnum is an upper bound on the size required. It
36344242Sab196087 	 * is likely that we won't use a few of these array elements.
36354242Sab196087 	 * Allocating a modest amount of extra memory in this case means
36364242Sab196087 	 * that we can avoid an extra loop to count the number of needed
36374242Sab196087 	 * items, and can fill this array immediately in the first loop
36384242Sab196087 	 * below.
36394242Sab196087 	 */
36404242Sab196087 	if ((shdr_ndx_arr = malloc(shnum * sizeof (*shdr_ndx_arr))) == NULL) {
36414242Sab196087 		int err = errno;
36424242Sab196087 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
36434242Sab196087 		    file, strerror(err));
36444665Sab196087 		return (0);
36454242Sab196087 	}
36464242Sab196087 	shdr_ndx_arr_cnt = 0;
36474242Sab196087 
36484242Sab196087 	/*
36493862Srie 	 * Traverse the sections of the file.  This gathering of data is
36503862Srie 	 * carried out in two passes.  First, the section headers are captured
36513862Srie 	 * and the section header names are evaluated.  A verification pass is
36523862Srie 	 * then carried out over the section information.  Files have been
36533862Srie 	 * known to exhibit overlapping (and hence erroneous) section header
36543862Srie 	 * information.
36553862Srie 	 *
36563862Srie 	 * Finally, the data for each section is obtained.  This processing is
36573862Srie 	 * carried out after section verification because should any section
36583862Srie 	 * header overlap occur, and a file needs translating (ie. xlate'ing
36593862Srie 	 * information from a non-native architecture file), then the process
36603862Srie 	 * of translation can corrupt the section header information.  Of
36613862Srie 	 * course, if there is any section overlap, the data related to the
36623862Srie 	 * sections is going to be compromised.  However, it is the translation
36633862Srie 	 * of this data that has caused problems with elfdump()'s ability to
36643862Srie 	 * extract the data.
36653862Srie 	 */
36664242Sab196087 	for (ndx = 1, scn = NULL; scn = elf_nextscn(elf, scn);
36674242Sab196087 	    ndx++, _cache++) {
36683862Srie 		char	scnndxnm[100];
36693862Srie 
36704242Sab196087 		_cache->c_ndx = ndx;
36713862Srie 		_cache->c_scn = scn;
36723862Srie 
36731618Srie 		if ((_cache->c_shdr = elf_getshdr(scn)) == NULL) {
36740Sstevel@tonic-gate 			failure(file, MSG_ORIG(MSG_ELF_GETSHDR));
36750Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN),
36763862Srie 			    EC_WORD(elf_ndxscn(scn)));
36770Sstevel@tonic-gate 		}
36780Sstevel@tonic-gate 
36793862Srie 		/*
36804242Sab196087 		 * If this section has data in the file, include it in
36814242Sab196087 		 * the array of sections to check for address overlap.
36824242Sab196087 		 */
36834242Sab196087 		if ((_cache->c_shdr->sh_size != 0) &&
36844242Sab196087 		    (_cache->c_shdr->sh_type != SHT_NOBITS))
36854242Sab196087 			shdr_ndx_arr[shdr_ndx_arr_cnt++] = ndx;
36864242Sab196087 
36874242Sab196087 		/*
36883862Srie 		 * If a shstrtab exists, assign the section name.
36893862Srie 		 */
36903862Srie 		if (names && _cache->c_shdr) {
36913862Srie 			if (_cache->c_shdr->sh_name &&
36923862Srie 			    /* LINTED */
36933862Srie 			    (nameshdr->sh_size > _cache->c_shdr->sh_name)) {
36947463SRod.Evans@Sun.COM 				const char	*symname;
36957463SRod.Evans@Sun.COM 				char		*secname;
36967463SRod.Evans@Sun.COM 
36977463SRod.Evans@Sun.COM 				secname = names + _cache->c_shdr->sh_name;
36987463SRod.Evans@Sun.COM 
36997463SRod.Evans@Sun.COM 				/*
37007463SRod.Evans@Sun.COM 				 * A SUN naming convention employs a "%" within
37017463SRod.Evans@Sun.COM 				 * a section name to indicate a section/symbol
37027463SRod.Evans@Sun.COM 				 * name.  This originated from the compilers
37037463SRod.Evans@Sun.COM 				 * -xF option, that places functions into their
37047463SRod.Evans@Sun.COM 				 * own sections.  This convention (which has no
37057463SRod.Evans@Sun.COM 				 * formal standard) has also been followed for
37067463SRod.Evans@Sun.COM 				 * COMDAT sections.  To demangle the symbol
37077463SRod.Evans@Sun.COM 				 * name, the name must be separated from the
37087463SRod.Evans@Sun.COM 				 * section name.
37097463SRod.Evans@Sun.COM 				 */
37107463SRod.Evans@Sun.COM 				if (((flags & FLG_CTL_DEMANGLE) == 0) ||
37117463SRod.Evans@Sun.COM 				    ((symname = strchr(secname, '%')) == NULL))
37127463SRod.Evans@Sun.COM 					_cache->c_name = secname;
37137463SRod.Evans@Sun.COM 				else {
37147463SRod.Evans@Sun.COM 					size_t	secsz = ++symname - secname;
37157463SRod.Evans@Sun.COM 					size_t	strsz;
37167463SRod.Evans@Sun.COM 
37177463SRod.Evans@Sun.COM 					symname = demangle(symname, flags);
37187463SRod.Evans@Sun.COM 					strsz = secsz + strlen(symname) + 1;
37197463SRod.Evans@Sun.COM 
37207463SRod.Evans@Sun.COM 					if ((_cache->c_name =
37217463SRod.Evans@Sun.COM 					    malloc(strsz)) == NULL) {
37227463SRod.Evans@Sun.COM 						int err = errno;
37237463SRod.Evans@Sun.COM 						(void) fprintf(stderr,
37247463SRod.Evans@Sun.COM 						    MSG_INTL(MSG_ERR_MALLOC),
37257463SRod.Evans@Sun.COM 						    file, strerror(err));
37267463SRod.Evans@Sun.COM 						return (0);
37277463SRod.Evans@Sun.COM 					}
37287463SRod.Evans@Sun.COM 					(void) snprintf(_cache->c_name, strsz,
37297463SRod.Evans@Sun.COM 					    MSG_ORIG(MSG_FMT_SECSYM),
37307463SRod.Evans@Sun.COM 					    EC_WORD(secsz), secname, symname);
37317463SRod.Evans@Sun.COM 				}
37323862Srie 				continue;
37333862Srie 			}
37340Sstevel@tonic-gate 
37350Sstevel@tonic-gate 			/*
37363862Srie 			 * Generate an error if the section name index is zero
37373862Srie 			 * or exceeds the shstrtab data.  Fall through to
37383862Srie 			 * fabricate a section name.
37390Sstevel@tonic-gate 			 */
37403862Srie 			if ((_cache->c_shdr->sh_name == 0) ||
37410Sstevel@tonic-gate 			    /* LINTED */
37421618Srie 			    (nameshdr->sh_size <= _cache->c_shdr->sh_name)) {
37430Sstevel@tonic-gate 				(void) fprintf(stderr,
37440Sstevel@tonic-gate 				    MSG_INTL(MSG_ERR_BADSHNAME), file,
37454242Sab196087 				    EC_WORD(ndx),
37461618Srie 				    EC_XWORD(_cache->c_shdr->sh_name));
37470Sstevel@tonic-gate 			}
37483862Srie 		}
37493862Srie 
37503862Srie 		/*
37513862Srie 		 * If there exists no shstrtab data, or a section header has no
37523862Srie 		 * name (an invalid index of 0), then compose a name for the
37533862Srie 		 * section.
37543862Srie 		 */
37553862Srie 		(void) snprintf(scnndxnm, sizeof (scnndxnm),
37564242Sab196087 		    MSG_INTL(MSG_FMT_SCNNDX), ndx);
37574242Sab196087 
37584242Sab196087 		if ((_cache->c_name = malloc(strlen(scnndxnm) + 1)) == NULL) {
37593862Srie 			int err = errno;
37603862Srie 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
37613862Srie 			    file, strerror(err));
37624665Sab196087 			return (0);
37633862Srie 		}
37643862Srie 		(void) strcpy(_cache->c_name, scnndxnm);
37653862Srie 	}
37663862Srie 
37673862Srie 	/*
37683862Srie 	 * Having collected all the sections, validate their address range.
37693862Srie 	 * Cases have existed where the section information has been invalid.
37703862Srie 	 * This can lead to all sorts of other, hard to diagnose errors, as
37713862Srie 	 * each section is processed individually (ie. with elf_getdata()).
37723862Srie 	 * Here, we carry out some address comparisons to catch a family of
37733862Srie 	 * overlapping memory issues we have observed (likely, there are others
37743862Srie 	 * that we have yet to discover).
37753862Srie 	 *
37763862Srie 	 * Note, should any memory overlap occur, obtaining any additional
37773862Srie 	 * data from the file is questionable.  However, it might still be
37783862Srie 	 * possible to inspect the ELF header, Programs headers, or individual
37793862Srie 	 * sections, so rather than bailing on an error condition, continue
37803862Srie 	 * processing to see if any data can be salvaged.
37813862Srie 	 */
37824242Sab196087 	if (shdr_ndx_arr_cnt > 1) {
37834242Sab196087 		sort_shdr_ndx_arr_cache = cache;
37844242Sab196087 		qsort(shdr_ndx_arr, shdr_ndx_arr_cnt,
37854242Sab196087 		    sizeof (*shdr_ndx_arr), sort_shdr_ndx_arr);
37864242Sab196087 	}
37874242Sab196087 	for (ndx = 0; ndx < shdr_ndx_arr_cnt; ndx++) {
37884242Sab196087 		Cache	*_cache = cache + shdr_ndx_arr[ndx];
37893862Srie 		Shdr	*shdr = _cache->c_shdr;
37903862Srie 		Off	bgn1, bgn = shdr->sh_offset;
37913862Srie 		Off	end1, end = shdr->sh_offset + shdr->sh_size;
37924242Sab196087 		size_t	ndx1;
37934242Sab196087 
37944242Sab196087 		/*
37954242Sab196087 		 * Check the section against all following ones, reporting
37964242Sab196087 		 * any overlaps. Since we've sorted the sections by offset,
37974242Sab196087 		 * we can stop after the first comparison that fails. There
37984242Sab196087 		 * are no overlaps in a properly formed ELF file, in which
37994242Sab196087 		 * case this algorithm runs in O(n) time. This will degenerate
38004242Sab196087 		 * to O(n^2) for a completely broken file. Such a file is
38014242Sab196087 		 * (1) highly unlikely, and (2) unusable, so it is reasonable
38024242Sab196087 		 * for the analysis to take longer.
38034242Sab196087 		 */
38044242Sab196087 		for (ndx1 = ndx + 1; ndx1 < shdr_ndx_arr_cnt; ndx1++) {
38054242Sab196087 			Cache	*_cache1 = cache + shdr_ndx_arr[ndx1];
38063862Srie 			Shdr	*shdr1 = _cache1->c_shdr;
38073862Srie 
38083862Srie 			bgn1 = shdr1->sh_offset;
38093862Srie 			end1 = shdr1->sh_offset + shdr1->sh_size;
38103862Srie 
38113862Srie 			if (((bgn1 <= bgn) && (end1 > bgn)) ||
38123862Srie 			    ((bgn1 < end) && (end1 >= end))) {
38133862Srie 				(void) fprintf(stderr,
38143862Srie 				    MSG_INTL(MSG_ERR_SECMEMOVER), file,
38154242Sab196087 				    EC_WORD(elf_ndxscn(_cache->c_scn)),
38164242Sab196087 				    _cache->c_name, EC_OFF(bgn), EC_OFF(end),
38173862Srie 				    EC_WORD(elf_ndxscn(_cache1->c_scn)),
38184242Sab196087 				    _cache1->c_name, EC_OFF(bgn1),
38194242Sab196087 				    EC_OFF(end1));
38204242Sab196087 			} else {	/* No overlap, so can stop */
38214242Sab196087 				break;
38220Sstevel@tonic-gate 			}
38230Sstevel@tonic-gate 		}
38240Sstevel@tonic-gate 
38253862Srie 		/*
38264242Sab196087 		 * In addition to checking for sections overlapping
38274242Sab196087 		 * each other (done above), we should also make sure
38284242Sab196087 		 * the section doesn't overlap the section header array.
38293862Srie 		 */
38303862Srie 		bgn1 = ehdr->e_shoff;
38313862Srie 		end1 = ehdr->e_shoff + (ehdr->e_shentsize * ehdr->e_shnum);
38323862Srie 
38333862Srie 		if (((bgn1 <= bgn) && (end1 > bgn)) ||
38343862Srie 		    ((bgn1 < end) && (end1 >= end))) {
38353862Srie 			(void) fprintf(stderr,
38363862Srie 			    MSG_INTL(MSG_ERR_SHDRMEMOVER), file, EC_OFF(bgn1),
38373862Srie 			    EC_OFF(end1),
38383862Srie 			    EC_WORD(elf_ndxscn(_cache->c_scn)),
38393862Srie 			    _cache->c_name, EC_OFF(bgn), EC_OFF(end));
38403862Srie 		}
38413862Srie 	}
38423862Srie 
38433862Srie 	/*
38444242Sab196087 	 * Obtain the data for each section.
38453862Srie 	 */
38464242Sab196087 	for (ndx = 1; ndx < shnum; ndx++) {
38474242Sab196087 		Cache	*_cache = &cache[ndx];
38483862Srie 		Elf_Scn	*scn = _cache->c_scn;
38493862Srie 
38500Sstevel@tonic-gate 		if ((_cache->c_data = elf_getdata(scn, NULL)) == NULL) {
38510Sstevel@tonic-gate 			failure(file, MSG_ORIG(MSG_ELF_GETDATA));
38520Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCNDATA),
38533862Srie 			    EC_WORD(elf_ndxscn(scn)));
38540Sstevel@tonic-gate 		}
38554665Sab196087 	}
38564665Sab196087 
38574665Sab196087 	return (1);
38584665Sab196087 }
38594665Sab196087 
38604665Sab196087 
38614665Sab196087 
38625411Sab196087 int
38635411Sab196087 regular(const char *file, int fd, Elf *elf, uint_t flags,
38645411Sab196087     const char *wname, int wfd)
38654665Sab196087 {
38664665Sab196087 	Elf_Scn		*scn;
38674665Sab196087 	Ehdr		*ehdr;
38684665Sab196087 	size_t		ndx, shstrndx, shnum, phnum;
38694665Sab196087 	Shdr		*shdr;
38704665Sab196087 	Cache		*cache;
38714665Sab196087 	VERSYM_STATE	versym;
38725411Sab196087 	int		ret = 0;
38735411Sab196087 	int		addr_align;
38744665Sab196087 
38754665Sab196087 	if ((ehdr = elf_getehdr(elf)) == NULL) {
38764665Sab196087 		failure(file, MSG_ORIG(MSG_ELF_GETEHDR));
38775411Sab196087 		return (ret);
38784665Sab196087 	}
38794665Sab196087 
38804665Sab196087 	if (elf_getshnum(elf, &shnum) == 0) {
38814665Sab196087 		failure(file, MSG_ORIG(MSG_ELF_GETSHNUM));
38825411Sab196087 		return (ret);
38834665Sab196087 	}
38844665Sab196087 
38854665Sab196087 	if (elf_getshstrndx(elf, &shstrndx) == 0) {
38864665Sab196087 		failure(file, MSG_ORIG(MSG_ELF_GETSHSTRNDX));
38875411Sab196087 		return (ret);
38884665Sab196087 	}
38894665Sab196087 
38904665Sab196087 	if (elf_getphnum(elf, &phnum) == 0) {
38914665Sab196087 		failure(file, MSG_ORIG(MSG_ELF_GETPHNUM));
38925411Sab196087 		return (ret);
38934665Sab196087 	}
38944665Sab196087 	/*
38954665Sab196087 	 * If the user requested section headers derived from the
38964665Sab196087 	 * program headers (-P option) and this file doesn't have
38974665Sab196087 	 * any program headers (i.e. ET_REL), then we can't do it.
38984665Sab196087 	 */
38995411Sab196087 	if ((phnum == 0) && (flags & FLG_CTL_FAKESHDR)) {
39004665Sab196087 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_PNEEDSPH), file);
39015411Sab196087 		return (ret);
39024665Sab196087 	}
39034665Sab196087 
39044665Sab196087 
39054665Sab196087 	if ((scn = elf_getscn(elf, 0)) != NULL) {
39064665Sab196087 		if ((shdr = elf_getshdr(scn)) == NULL) {
39074665Sab196087 			failure(file, MSG_ORIG(MSG_ELF_GETSHDR));
39084665Sab196087 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN), 0);
39095411Sab196087 			return (ret);
39104665Sab196087 		}
39114665Sab196087 	} else
39124665Sab196087 		shdr = 0;
39134665Sab196087 
39144665Sab196087 	/*
39154665Sab196087 	 * Print the elf header.
39164665Sab196087 	 */
39175411Sab196087 	if (flags & FLG_SHOW_EHDR)
39184665Sab196087 		Elf_ehdr(0, ehdr, shdr);
39194665Sab196087 
39204665Sab196087 	/*
39214665Sab196087 	 * If the section headers or program headers have inadequate
39224665Sab196087 	 * alignment for the class of object, print a warning. libelf
39234665Sab196087 	 * can handle such files, but programs that use them can crash
39244665Sab196087 	 * when they dereference unaligned items.
39255411Sab196087 	 *
39265411Sab196087 	 * Note that the AMD64 ABI, although it is a 64-bit architecture,
39275411Sab196087 	 * allows access to data types smaller than 128-bits to be on
39285411Sab196087 	 * word alignment.
39294665Sab196087 	 */
39305411Sab196087 	if (ehdr->e_machine == EM_AMD64)
39315411Sab196087 		addr_align = sizeof (Word);
39325411Sab196087 	else
39335411Sab196087 		addr_align = sizeof (Addr);
39345411Sab196087 
39355411Sab196087 	if (ehdr->e_phoff & (addr_align - 1))
39364665Sab196087 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADPHDRALIGN), file);
39375411Sab196087 	if (ehdr->e_shoff & (addr_align - 1))
39384665Sab196087 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHDRALIGN), file);
39394665Sab196087 
39404665Sab196087 	/*
39414665Sab196087 	 * Print the program headers.
39424665Sab196087 	 */
39435411Sab196087 	if ((flags & FLG_SHOW_PHDR) && (phnum != 0)) {
39445411Sab196087 		Phdr	*phdr;
39454665Sab196087 
39464665Sab196087 		if ((phdr = elf_getphdr(elf)) == NULL) {
39474665Sab196087 			failure(file, MSG_ORIG(MSG_ELF_GETPHDR));
39485411Sab196087 			return (ret);
39494665Sab196087 		}
39504665Sab196087 
39514665Sab196087 		for (ndx = 0; ndx < phnum; phdr++, ndx++) {
39525411Sab196087 			if (!match(MATCH_F_PHDR| MATCH_F_NDX | MATCH_F_TYPE,
39535411Sab196087 			    NULL, ndx, phdr->p_type))
39544665Sab196087 				continue;
39554665Sab196087 
39564665Sab196087 			dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
39574665Sab196087 			dbg_print(0, MSG_INTL(MSG_ELF_PHDR), EC_WORD(ndx));
39584665Sab196087 			Elf_phdr(0, ehdr->e_machine, phdr);
39594665Sab196087 		}
39604665Sab196087 	}
39614665Sab196087 
39624665Sab196087 	/*
39635411Sab196087 	 * If we have flag bits set that explicitly require a show or calc
39645411Sab196087 	 * operation, but none of them require the section headers, then
39655411Sab196087 	 * we are done and can return now.
39664665Sab196087 	 */
39675411Sab196087 	if (((flags & (FLG_MASK_SHOW | FLG_MASK_CALC)) != 0) &&
39685411Sab196087 	    ((flags & (FLG_MASK_SHOW_SHDR | FLG_MASK_CALC_SHDR)) == 0))
39695411Sab196087 		return (ret);
39705411Sab196087 
39715411Sab196087 	/*
39725411Sab196087 	 * If there are no section headers, then resort to synthesizing
39735411Sab196087 	 * section headers from the program headers. This is normally
39745411Sab196087 	 * only done by explicit request, but in this case there's no
39755411Sab196087 	 * reason not to go ahead, since the alternative is simply to quit.
39765411Sab196087 	 */
39775411Sab196087 	if ((shnum <= 1) && ((flags & FLG_CTL_FAKESHDR) == 0)) {
39785411Sab196087 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_NOSHDR), file);
39795411Sab196087 		flags |= FLG_CTL_FAKESHDR;
39804665Sab196087 	}
39814665Sab196087 
39824665Sab196087 	/*
39834665Sab196087 	 * Generate a cache of section headers and related information
39844665Sab196087 	 * for use by the rest of elfdump. If requested (or the file
39854665Sab196087 	 * contains no section headers), we generate a fake set of
39864665Sab196087 	 * headers from the information accessible from the program headers.
39874665Sab196087 	 * Otherwise, we use the real section headers contained in the file.
39884665Sab196087 	 */
39894665Sab196087 
39905411Sab196087 	if (flags & FLG_CTL_FAKESHDR) {
39914665Sab196087 		if (fake_shdr_cache(file, fd, elf, ehdr, &cache, &shnum) == 0)
39925411Sab196087 			return (ret);
39934665Sab196087 	} else {
39947463SRod.Evans@Sun.COM 		if (shdr_cache(file, elf, ehdr, shstrndx, shnum,
39957463SRod.Evans@Sun.COM 		    &cache, flags) == 0)
39965411Sab196087 			return (ret);
39974665Sab196087 	}
39984665Sab196087 
39994665Sab196087 	/*
40005411Sab196087 	 * Everything from this point on requires section headers.
40015411Sab196087 	 * If we have no section headers, there is no reason to continue.
40025411Sab196087 	 */
40035411Sab196087 	if (shnum <= 1)
40045411Sab196087 		goto done;
40055411Sab196087 
40065411Sab196087 	/*
40074665Sab196087 	 * If -w was specified, find and write out the section(s) data.
40084665Sab196087 	 */
40094665Sab196087 	if (wfd) {
40104665Sab196087 		for (ndx = 1; ndx < shnum; ndx++) {
40114665Sab196087 			Cache	*_cache = &cache[ndx];
40124665Sab196087 
40135411Sab196087 			if (match(MATCH_F_STRICT | MATCH_F_ALL, _cache->c_name,
40145411Sab196087 			    ndx, _cache->c_shdr->sh_type) &&
40155411Sab196087 			    _cache->c_data && _cache->c_data->d_buf) {
40165411Sab196087 				if (write(wfd, _cache->c_data->d_buf,
40175411Sab196087 				    _cache->c_data->d_size) !=
40185411Sab196087 				    _cache->c_data->d_size) {
40195411Sab196087 					int err = errno;
40205411Sab196087 					(void) fprintf(stderr,
40215411Sab196087 					    MSG_INTL(MSG_ERR_WRITE), wname,
40225411Sab196087 					    strerror(err));
40235411Sab196087 					/*
40245411Sab196087 					 * Return an exit status of 1, because
40255411Sab196087 					 * the failure is not related to the
40265411Sab196087 					 * ELF file, but by system resources.
40275411Sab196087 					 */
40285411Sab196087 					ret = 1;
40295411Sab196087 					goto done;
40305411Sab196087 				}
40314665Sab196087 			}
40320Sstevel@tonic-gate 		}
40330Sstevel@tonic-gate 	}
40340Sstevel@tonic-gate 
40355411Sab196087 	/*
40365411Sab196087 	 * If we have no flag bits set that explicitly require a show or calc
40375411Sab196087 	 * operation, but match options (-I, -N, -T) were used, then run
40385411Sab196087 	 * through the section headers and see if we can't deduce show flags
40395411Sab196087 	 * from the match options given.
40405411Sab196087 	 *
40415411Sab196087 	 * We don't do this if -w was specified, because (-I, -N, -T) used
40425411Sab196087 	 * with -w in lieu of some other option is supposed to be quiet.
40435411Sab196087 	 */
40445411Sab196087 	if ((wfd == 0) && (flags & FLG_CTL_MATCH) &&
40455411Sab196087 	    ((flags & (FLG_MASK_SHOW | FLG_MASK_CALC)) == 0)) {
40465411Sab196087 		for (ndx = 1; ndx < shnum; ndx++) {
40475411Sab196087 			Cache	*_cache = &cache[ndx];
40485411Sab196087 
40495411Sab196087 			if (!match(MATCH_F_STRICT | MATCH_F_ALL, _cache->c_name,
40505411Sab196087 			    ndx, _cache->c_shdr->sh_type))
40515411Sab196087 				continue;
40525411Sab196087 
40535411Sab196087 			switch (_cache->c_shdr->sh_type) {
40545411Sab196087 			case SHT_PROGBITS:
40555411Sab196087 				/*
40565411Sab196087 				 * Heuristic time: It is usually bad form
40575411Sab196087 				 * to assume that specific section names
40585411Sab196087 				 * have a given meaning. However, the
40595411Sab196087 				 * ELF ABI does specify a few such names. Try
40605411Sab196087 				 * to match them:
40615411Sab196087 				 */
40625411Sab196087 				if (strcmp(_cache->c_name,
40635411Sab196087 				    MSG_ORIG(MSG_ELF_INTERP)) == 0)
40645411Sab196087 					flags |= FLG_SHOW_INTERP;
40655411Sab196087 				else if (strcmp(_cache->c_name,
40665411Sab196087 				    MSG_ORIG(MSG_ELF_GOT)) == 0)
40675411Sab196087 					flags |= FLG_SHOW_GOT;
40685411Sab196087 				break;
40695411Sab196087 
40705411Sab196087 			case SHT_SYMTAB:
40715411Sab196087 			case SHT_DYNSYM:
40725411Sab196087 			case SHT_SUNW_LDYNSYM:
40735411Sab196087 			case SHT_SUNW_versym:
40745411Sab196087 			case SHT_SYMTAB_SHNDX:
40755411Sab196087 				flags |= FLG_SHOW_SYMBOLS;
40765411Sab196087 				break;
40775411Sab196087 
40785411Sab196087 			case SHT_RELA:
40795411Sab196087 			case SHT_REL:
40805411Sab196087 				flags |= FLG_SHOW_RELOC;
40815411Sab196087 				break;
40825411Sab196087 
40835411Sab196087 			case SHT_HASH:
40845411Sab196087 				flags |= FLG_SHOW_HASH;
40855411Sab196087 				break;
40865411Sab196087 
40875411Sab196087 			case SHT_DYNAMIC:
40885411Sab196087 				flags |= FLG_SHOW_DYNAMIC;
40895411Sab196087 				break;
40905411Sab196087 
40915411Sab196087 			case SHT_NOTE:
40925411Sab196087 				flags |= FLG_SHOW_NOTE;
40935411Sab196087 				break;
40945411Sab196087 
40955411Sab196087 			case SHT_GROUP:
40965411Sab196087 				flags |= FLG_SHOW_GROUP;
40975411Sab196087 				break;
40985411Sab196087 
40995411Sab196087 			case SHT_SUNW_symsort:
41005411Sab196087 			case SHT_SUNW_tlssort:
41015411Sab196087 				flags |= FLG_SHOW_SORT;
41025411Sab196087 				break;
41035411Sab196087 
41045411Sab196087 			case SHT_SUNW_cap:
41055411Sab196087 				flags |= FLG_SHOW_CAP;
41065411Sab196087 				break;
41075411Sab196087 
41085411Sab196087 			case SHT_SUNW_move:
41095411Sab196087 				flags |= FLG_SHOW_MOVE;
41105411Sab196087 				break;
41115411Sab196087 
41125411Sab196087 			case SHT_SUNW_syminfo:
41135411Sab196087 				flags |= FLG_SHOW_SYMINFO;
41145411Sab196087 				break;
41155411Sab196087 
41165411Sab196087 			case SHT_SUNW_verdef:
41175411Sab196087 			case SHT_SUNW_verneed:
41185411Sab196087 				flags |= FLG_SHOW_VERSIONS;
41195411Sab196087 				break;
41205411Sab196087 
41215411Sab196087 			case SHT_AMD64_UNWIND:
41225411Sab196087 				flags |= FLG_SHOW_UNWIND;
41235411Sab196087 				break;
41245411Sab196087 			}
41255411Sab196087 		}
41265411Sab196087 	}
41275411Sab196087 
41285411Sab196087 
41295411Sab196087 	if (flags & FLG_SHOW_SHDR)
41304168Sab196087 		sections(file, cache, shnum, ehdr);
41310Sstevel@tonic-gate 
41325411Sab196087 	if (flags & FLG_SHOW_INTERP)
41331618Srie 		interp(file, cache, shnum, phnum, elf);
41340Sstevel@tonic-gate 
41353875Sab196087 	versions(cache, shnum, file, flags, &versym);
41360Sstevel@tonic-gate 
41375411Sab196087 	if (flags & FLG_SHOW_SYMBOLS)
41384168Sab196087 		symbols(cache, shnum, ehdr, &versym, file, flags);
41390Sstevel@tonic-gate 
41405411Sab196087 	if (flags & FLG_SHOW_SORT)
41414168Sab196087 		sunw_sort(cache, shnum, ehdr, &versym, file, flags);
41423492Sab196087 
41435411Sab196087 	if (flags & FLG_SHOW_HASH)
41444168Sab196087 		hash(cache, shnum, file, flags);
41450Sstevel@tonic-gate 
41465411Sab196087 	if (flags & FLG_SHOW_GOT)
41477463SRod.Evans@Sun.COM 		got(cache, shnum, ehdr, file);
41480Sstevel@tonic-gate 
41495411Sab196087 	if (flags & FLG_SHOW_GROUP)
41504168Sab196087 		group(cache, shnum, file, flags);
41510Sstevel@tonic-gate 
41525411Sab196087 	if (flags & FLG_SHOW_SYMINFO)
41530Sstevel@tonic-gate 		syminfo(cache, shnum, file);
41540Sstevel@tonic-gate 
41555411Sab196087 	if (flags & FLG_SHOW_RELOC)
41567463SRod.Evans@Sun.COM 		reloc(cache, shnum, ehdr, file);
41570Sstevel@tonic-gate 
41585411Sab196087 	if (flags & FLG_SHOW_DYNAMIC)
41591618Srie 		dynamic(cache, shnum, ehdr, file);
41600Sstevel@tonic-gate 
41616635Sab196087 	if (flags & FLG_SHOW_NOTE) {
41626635Sab196087 		Word	note_cnt;
41636635Sab196087 		size_t	note_shnum;
41646635Sab196087 		Cache	*note_cache;
41656635Sab196087 
41666635Sab196087 		note_cnt = note(cache, shnum, ehdr, file);
41676635Sab196087 
41686635Sab196087 		/*
41696635Sab196087 		 * Solaris core files have section headers, but these
41706635Sab196087 		 * headers do not include SHT_NOTE sections that reference
41716635Sab196087 		 * the core note sections. This means that note() won't
41726635Sab196087 		 * find the core notes. Fake section headers (-P option)
41736635Sab196087 		 * recover these sections, but it is inconvenient to require
41746635Sab196087 		 * users to specify -P in this situation. If the following
41756635Sab196087 		 * are all true:
41766635Sab196087 		 *
41776635Sab196087 		 *	- No note sections were found
41786635Sab196087 		 *	- This is a core file
41796635Sab196087 		 *	- We are not already using fake section headers
41806635Sab196087 		 *
41816635Sab196087 		 * then we will automatically generate fake section headers
41826635Sab196087 		 * and then process them in a second call to note().
41836635Sab196087 		 */
41846635Sab196087 		if ((note_cnt == 0) && (ehdr->e_type == ET_CORE) &&
41856635Sab196087 		    !(flags & FLG_CTL_FAKESHDR) &&
41866635Sab196087 		    (fake_shdr_cache(file, fd, elf, ehdr,
41876635Sab196087 		    &note_cache, &note_shnum) != 0)) {
41886635Sab196087 			(void) note(note_cache, note_shnum, ehdr, file);
41896635Sab196087 			fake_shdr_cache_free(note_cache, note_shnum);
41906635Sab196087 		}
41916635Sab196087 	}
41920Sstevel@tonic-gate 
41935411Sab196087 	if (flags & FLG_SHOW_MOVE)
41944168Sab196087 		move(cache, shnum, file, flags);
41950Sstevel@tonic-gate 
41965411Sab196087 	if (flags & FLG_CALC_CHECKSUM)
41970Sstevel@tonic-gate 		checksum(elf);
41980Sstevel@tonic-gate 
41995411Sab196087 	if (flags & FLG_SHOW_CAP)
42001618Srie 		cap(file, cache, shnum, phnum, ehdr, elf);
42010Sstevel@tonic-gate 
42025411Sab196087 	if (flags & FLG_SHOW_UNWIND)
42034168Sab196087 		unwind(cache, shnum, phnum, ehdr, file, elf);
42040Sstevel@tonic-gate 
42054665Sab196087 
42064665Sab196087 	/* Release the memory used to cache section headers */
42075411Sab196087 done:
42085411Sab196087 	if (flags & FLG_CTL_FAKESHDR)
42094665Sab196087 		fake_shdr_cache_free(cache, shnum);
42104665Sab196087 	else
42114665Sab196087 		free(cache);
42125411Sab196087 
42135411Sab196087 	return (ret);
42140Sstevel@tonic-gate }
4215