xref: /titanic_51/usr/src/cmd/sgs/elfdump/common/elfdump.c (revision 9bc2928da4128bbbe0feaaa43090efe2ea01abe2)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55aefb655Srie  * Common Development and Distribution License (the "License").
65aefb655Srie  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21141040e8Srie 
227c478bd9Sstevel@tonic-gate /*
2331dd2c84SAli Bahrami  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
28*9bc2928dSBryan Cantrill  * Copyright (c) 2015, Joyent, Inc. All rights reserved.
29*9bc2928dSBryan Cantrill  */
30*9bc2928dSBryan Cantrill 
31*9bc2928dSBryan Cantrill /*
327c478bd9Sstevel@tonic-gate  * Dump an elf file.
337c478bd9Sstevel@tonic-gate  */
34e23c41c9SAli Bahrami #include	<stddef.h>
357c478bd9Sstevel@tonic-gate #include	<sys/elf_386.h>
367c478bd9Sstevel@tonic-gate #include	<sys/elf_amd64.h>
375aefb655Srie #include	<sys/elf_SPARC.h>
38ba2be530Sab196087 #include	<_libelf.h>
395aefb655Srie #include	<dwarf.h>
40a194faf8Srie #include	<stdio.h>
415aefb655Srie #include	<unistd.h>
425aefb655Srie #include	<errno.h>
435aefb655Srie #include	<strings.h>
447c478bd9Sstevel@tonic-gate #include	<debug.h>
457c478bd9Sstevel@tonic-gate #include	<conv.h>
467c478bd9Sstevel@tonic-gate #include	<msg.h>
475aefb655Srie #include	<_elfdump.h>
487c478bd9Sstevel@tonic-gate 
493b41b08bSab196087 
503b41b08bSab196087 /*
513b41b08bSab196087  * VERSYM_STATE is used to maintain information about the VERSYM section
523b41b08bSab196087  * in the object being analyzed. It is filled in by versions(), and used
533b41b08bSab196087  * by init_symtbl_state() when displaying symbol information.
543b41b08bSab196087  *
55090a8d9eSAli Bahrami  * There are three forms of symbol versioning known to us:
56090a8d9eSAli Bahrami  *
57090a8d9eSAli Bahrami  * 1) The original form, introduced with Solaris 2.5, in which
58090a8d9eSAli Bahrami  *	the Versym contains indexes to Verdef records, and the
59090a8d9eSAli Bahrami  *	Versym values for UNDEF symbols resolved by other objects
60090a8d9eSAli Bahrami  *	are all set to 0.
61090a8d9eSAli Bahrami  * 2) The GNU form, which is backward compatible with the original
62090a8d9eSAli Bahrami  *	Solaris form, but which adds several extensions:
63090a8d9eSAli Bahrami  *	- The Versym also contains indexes to Verneed records, recording
64090a8d9eSAli Bahrami  *		which object/version contributed the external symbol at
65090a8d9eSAli Bahrami  *		link time. These indexes start with the next value following
66090a8d9eSAli Bahrami  *		the final Verdef index. The index is written to the previously
67090a8d9eSAli Bahrami  *		reserved vna_other field of the ELF Vernaux structure.
68090a8d9eSAli Bahrami  *	- The top bit of the Versym value is no longer part of the index,
69090a8d9eSAli Bahrami  *		but is used as a "hidden bit" to prevent binding to the symbol.
70090a8d9eSAli Bahrami  *	- Multiple implementations of a given symbol, contained in varying
71090a8d9eSAli Bahrami  *		versions are allowed, using special assembler pseudo ops,
72090a8d9eSAli Bahrami  *		and encoded in the symbol name using '@' characters.
73090a8d9eSAli Bahrami  * 3) Modified Solaris form, in which we adopt the first GNU extension
74090a8d9eSAli Bahrami  *	(Versym indexes to Verneed records), but not the others.
75090a8d9eSAli Bahrami  *
76090a8d9eSAli Bahrami  * elfdump can handle any of these cases. The presence of a DT_VERSYM
77090a8d9eSAli Bahrami  * dynamic element indicates a full GNU object. An object that lacks
78090a8d9eSAli Bahrami  * a DT_VERSYM entry, but which has non-zero vna_other fields in the Vernaux
79090a8d9eSAli Bahrami  * structures is a modified Solaris object. An object that has neither of
80090a8d9eSAli Bahrami  * these uses the original form.
81090a8d9eSAli Bahrami  *
82d840867fSab196087  * max_verndx contains the largest version index that can appear
83d840867fSab196087  * in a Versym entry. This can never be less than 1: In the case where
84d840867fSab196087  * there is no verdef/verneed sections, the [0] index is reserved
85090a8d9eSAli Bahrami  * for local symbols, and the [1] index for globals. If the original
86090a8d9eSAli Bahrami  * Solaris versioning rules are in effect and there is a verdef section,
87090a8d9eSAli Bahrami  * then max_verndex is the number of defined versions. If one of the
88090a8d9eSAli Bahrami  * other versioning forms is in effect, then:
89090a8d9eSAli Bahrami  *	1) If there is no verneed section, it is the same as for
90090a8d9eSAli Bahrami  *		original Solaris versioning.
91090a8d9eSAli Bahrami  *	2) If there is a verneed section, the vna_other field of the
92d840867fSab196087  *		Vernaux structs contain versions, and max_verndx is the
93d840867fSab196087  *		largest such index.
94d840867fSab196087  *
95090a8d9eSAli Bahrami  * If gnu_full is True, the object uses the full GNU form of versioning.
96090a8d9eSAli Bahrami  * The value of the gnu_full field is based on the presence of
97d840867fSab196087  * a DT_VERSYM entry in the dynamic section: GNU ld produces these, and
98d840867fSab196087  * Solaris ld does not.
99090a8d9eSAli Bahrami  *
100090a8d9eSAli Bahrami  * The gnu_needed field is True if the Versym contains indexes to
101090a8d9eSAli Bahrami  * Verneed records, as indicated by non-zero vna_other fields in the Verneed
102090a8d9eSAli Bahrami  * section. If gnu_full is True, then gnu_needed will always be true.
103090a8d9eSAli Bahrami  * However, gnu_needed can be true without gnu_full. This is the modified
104090a8d9eSAli Bahrami  * Solaris form.
1053b41b08bSab196087  */
1063b41b08bSab196087 typedef struct {
1073b41b08bSab196087 	Cache	*cache;		/* Pointer to cache entry for VERSYM */
1083b41b08bSab196087 	Versym	*data;		/* Pointer to versym array */
109090a8d9eSAli Bahrami 	int	gnu_full;	/* True if object uses GNU versioning rules */
110090a8d9eSAli Bahrami 	int	gnu_needed;	/* True if object uses VERSYM indexes for */
111090a8d9eSAli Bahrami 				/*	VERNEED (subset of gnu_full) */
112d840867fSab196087 	int	max_verndx;	/* largest versym index value */
1133b41b08bSab196087 } VERSYM_STATE;
1143b41b08bSab196087 
1153b41b08bSab196087 /*
1163b41b08bSab196087  * SYMTBL_STATE is used to maintain information about a single symbol
1173b41b08bSab196087  * table section, for use by the routines that display symbol information.
1183b41b08bSab196087  */
1193b41b08bSab196087 typedef struct {
1203b41b08bSab196087 	const char	*file;		/* Name of file */
1213b41b08bSab196087 	Ehdr		*ehdr;		/* ELF header for file */
1223b41b08bSab196087 	Cache		*cache;		/* Cache of all section headers */
1234f680cc6SAli Bahrami 	uchar_t		osabi;		/* OSABI to use */
1243b41b08bSab196087 	Word		shnum;		/* # of sections in cache */
1253b41b08bSab196087 	Cache		*seccache;	/* Cache of symbol table section hdr */
1263b41b08bSab196087 	Word		secndx;		/* Index of symbol table section hdr */
1273b41b08bSab196087 	const char	*secname;	/* Name of section */
1283b41b08bSab196087 	uint_t		flags;		/* Command line option flags */
1293b41b08bSab196087 	struct {			/* Extended section index data */
1303b41b08bSab196087 		int	checked;	/* TRUE if already checked for shxndx */
1313b41b08bSab196087 		Word	*data;		/* NULL, or extended section index */
1323b41b08bSab196087 					/*	used for symbol table entries */
1333b41b08bSab196087 		uint_t	n;		/* # items in shxndx.data */
1343b41b08bSab196087 	} shxndx;
1353b41b08bSab196087 	VERSYM_STATE	*versym;	/* NULL, or associated VERSYM section */
1363b41b08bSab196087 	Sym 		*sym;		/* Array of symbols */
1373b41b08bSab196087 	Word		symn;		/* # of symbols */
1383b41b08bSab196087 } SYMTBL_STATE;
1393b41b08bSab196087 
140e23c41c9SAli Bahrami /*
141e23c41c9SAli Bahrami  * A variable of this type is used to track information related to
142e23c41c9SAli Bahrami  * .eh_frame and .eh_frame_hdr sections across calls to unwind_eh_frame().
143e23c41c9SAli Bahrami  */
144e23c41c9SAli Bahrami typedef struct {
145e23c41c9SAli Bahrami 	Word		frame_cnt;	/* # .eh_frame sections seen */
146e23c41c9SAli Bahrami 	Word		frame_ndx;	/* Section index of 1st .eh_frame */
147e23c41c9SAli Bahrami 	Word		hdr_cnt;	/* # .eh_frame_hdr sections seen */
148e23c41c9SAli Bahrami 	Word		hdr_ndx;	/* Section index of 1st .eh_frame_hdr */
149e23c41c9SAli Bahrami 	uint64_t	frame_ptr;	/* Value of FramePtr field from first */
150e23c41c9SAli Bahrami 					/*	.eh_frame_hdr section */
151e23c41c9SAli Bahrami 	uint64_t	frame_base;	/* Data addr of 1st .eh_frame  */
152e23c41c9SAli Bahrami } gnu_eh_state_t;
1533b41b08bSab196087 
154e23c41c9SAli Bahrami /*
155e23c41c9SAli Bahrami  * C++ .exception_ranges entries make use of the signed ptrdiff_t
156e23c41c9SAli Bahrami  * type to record self-relative pointer values. We need a type
157e23c41c9SAli Bahrami  * for this that is matched to the ELFCLASS being processed.
158e23c41c9SAli Bahrami  */
159e23c41c9SAli Bahrami #if	defined(_ELF64)
160e23c41c9SAli Bahrami 	typedef int64_t PTRDIFF_T;
161e23c41c9SAli Bahrami #else
162e23c41c9SAli Bahrami 	typedef int32_t PTRDIFF_T;
163e23c41c9SAli Bahrami #endif
164e23c41c9SAli Bahrami 
165e23c41c9SAli Bahrami /*
166e23c41c9SAli Bahrami  * The Sun C++ ABI uses this struct to define each .exception_ranges
167e23c41c9SAli Bahrami  * entry. From the ABI:
168e23c41c9SAli Bahrami  *
169e23c41c9SAli Bahrami  * The field ret_addr is a self relative pointer to the start of the address
170e23c41c9SAli Bahrami  * range. The name was chosen because in the current implementation the range
171e23c41c9SAli Bahrami  * typically starts at the return address for a call site.
172e23c41c9SAli Bahrami  *
173e23c41c9SAli Bahrami  * The field length is the difference, in bytes, between the pc of the last
174e23c41c9SAli Bahrami  * instruction covered by the exception range and the first. When only a
175e23c41c9SAli Bahrami  * single call site is represented without optimization, this will equal zero.
176e23c41c9SAli Bahrami  *
177e23c41c9SAli Bahrami  * The field handler_addr is a relative pointer which stores the difference
178e23c41c9SAli Bahrami  * between the start of the exception range and the address of all code to
179e23c41c9SAli Bahrami  * catch exceptions and perform the cleanup for stack unwinding.
180e23c41c9SAli Bahrami  *
181e23c41c9SAli Bahrami  * The field type_block is a relative pointer which stores the difference
182e23c41c9SAli Bahrami  * between the start of the exception range and the address of an array used
183e23c41c9SAli Bahrami  * for storing a list of the types of exceptions which can be caught within
184e23c41c9SAli Bahrami  * the exception range.
185e23c41c9SAli Bahrami  */
186e23c41c9SAli Bahrami typedef struct {
187e23c41c9SAli Bahrami 	PTRDIFF_T	ret_addr;
188e23c41c9SAli Bahrami 	Xword		length;
189e23c41c9SAli Bahrami 	PTRDIFF_T	handler_addr;
190e23c41c9SAli Bahrami 	PTRDIFF_T	type_block;
191e23c41c9SAli Bahrami 	Xword		reserved;
192e23c41c9SAli Bahrami } exception_range_entry;
1933b41b08bSab196087 
1947c478bd9Sstevel@tonic-gate /*
1957c478bd9Sstevel@tonic-gate  * Focal point for verifying symbol names.
1967c478bd9Sstevel@tonic-gate  */
1977c478bd9Sstevel@tonic-gate static const char *
1985aefb655Srie string(Cache *refsec, Word ndx, Cache *strsec, const char *file, Word name)
1997c478bd9Sstevel@tonic-gate {
20031fdd7caSab196087 	/*
20131fdd7caSab196087 	 * If an error in this routine is due to a property of the string
20231fdd7caSab196087 	 * section, as opposed to a bad offset into the section (a property of
20331fdd7caSab196087 	 * the referencing section), then we will detect the same error on
20431fdd7caSab196087 	 * every call involving those sections. We use these static variables
20531fdd7caSab196087 	 * to retain the information needed to only issue each such error once.
20631fdd7caSab196087 	 */
20731fdd7caSab196087 	static Cache	*last_refsec;	/* Last referencing section seen */
20831fdd7caSab196087 	static int	strsec_err;	/* True if error issued */
20931fdd7caSab196087 
210d579eb63Sab196087 	const char	*strs;
211d579eb63Sab196087 	Word		strn;
2127c478bd9Sstevel@tonic-gate 
2139a411307Srie 	if (strsec->c_data == NULL)
2149a411307Srie 		return (NULL);
2159a411307Srie 
216d579eb63Sab196087 	strs = (char *)strsec->c_data->d_buf;
217d579eb63Sab196087 	strn = strsec->c_data->d_size;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	/*
22031fdd7caSab196087 	 * We only print a diagnostic regarding a bad string table once per
22131fdd7caSab196087 	 * input section being processed. If the refsec has changed, reset
22231fdd7caSab196087 	 * our retained error state.
2237c478bd9Sstevel@tonic-gate 	 */
22431fdd7caSab196087 	if (last_refsec != refsec) {
22531fdd7caSab196087 		last_refsec = refsec;
22631fdd7caSab196087 		strsec_err = 0;
22731fdd7caSab196087 	}
22831fdd7caSab196087 
22931fdd7caSab196087 	/* Verify that strsec really is a string table */
23031fdd7caSab196087 	if (strsec->c_shdr->sh_type != SHT_STRTAB) {
23131fdd7caSab196087 		if (!strsec_err) {
23231fdd7caSab196087 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_NOTSTRTAB),
23331fdd7caSab196087 			    file, strsec->c_ndx, refsec->c_ndx);
23431fdd7caSab196087 			strsec_err = 1;
23531fdd7caSab196087 		}
23631fdd7caSab196087 		return (MSG_INTL(MSG_STR_UNKNOWN));
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	/*
2407c478bd9Sstevel@tonic-gate 	 * Is the string table offset within range of the available strings?
2417c478bd9Sstevel@tonic-gate 	 */
2427c478bd9Sstevel@tonic-gate 	if (name >= strn) {
2437c478bd9Sstevel@tonic-gate 		/*
2447c478bd9Sstevel@tonic-gate 		 * Do we have a empty string table?
2457c478bd9Sstevel@tonic-gate 		 */
2467e16fca0SAli Bahrami 		if (strs == NULL) {
24731fdd7caSab196087 			if (!strsec_err) {
2487c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
2497c478bd9Sstevel@tonic-gate 				    file, strsec->c_name);
25031fdd7caSab196087 				strsec_err = 1;
2517c478bd9Sstevel@tonic-gate 			}
2527c478bd9Sstevel@tonic-gate 		} else {
2537c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSTOFF),
2545aefb655Srie 			    file, refsec->c_name, EC_WORD(ndx), strsec->c_name,
2555aefb655Srie 			    EC_WORD(name), EC_WORD(strn - 1));
2567c478bd9Sstevel@tonic-gate 		}
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 		/*
2597c478bd9Sstevel@tonic-gate 		 * Return the empty string so that the calling function can
2607c478bd9Sstevel@tonic-gate 		 * continue it's output diagnostics.
2617c478bd9Sstevel@tonic-gate 		 */
2627c478bd9Sstevel@tonic-gate 		return (MSG_INTL(MSG_STR_UNKNOWN));
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate 	return (strs + name);
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate /*
2685aefb655Srie  * Relocations can reference section symbols and standard symbols.  If the
2695aefb655Srie  * former, establish the section name.
2705aefb655Srie  */
2715aefb655Srie static const char *
2725aefb655Srie relsymname(Cache *cache, Cache *csec, Cache *strsec, Word symndx, Word symnum,
2730e233487SRod Evans     Word relndx, Sym *syms, char *secstr, size_t secsz, const char *file)
2745aefb655Srie {
2755aefb655Srie 	Sym		*sym;
2760e233487SRod Evans 	const char	*name;
2775aefb655Srie 
2785aefb655Srie 	if (symndx >= symnum) {
2795aefb655Srie 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_RELBADSYMNDX),
2805aefb655Srie 		    file, EC_WORD(symndx), EC_WORD(relndx));
2815aefb655Srie 		return (MSG_INTL(MSG_STR_UNKNOWN));
2825aefb655Srie 	}
2835aefb655Srie 
2845aefb655Srie 	sym = (Sym *)(syms + symndx);
2850e233487SRod Evans 	name = string(csec, symndx, strsec, file, sym->st_name);
2865aefb655Srie 
2875aefb655Srie 	/*
2885aefb655Srie 	 * If the symbol represents a section offset construct an appropriate
2890e233487SRod Evans 	 * string.  Note, although section symbol table entries typically have
2900e233487SRod Evans 	 * a NULL name pointer, entries do exist that point into the string
2910e233487SRod Evans 	 * table to their own NULL strings.
2925aefb655Srie 	 */
2930e233487SRod Evans 	if ((ELF_ST_TYPE(sym->st_info) == STT_SECTION) &&
2940e233487SRod Evans 	    ((sym->st_name == 0) || (*name == '\0'))) {
2950e233487SRod Evans 		(void) snprintf(secstr, secsz, MSG_INTL(MSG_STR_SECTION),
2965aefb655Srie 		    cache[sym->st_shndx].c_name);
2975aefb655Srie 		return ((const char *)secstr);
2985aefb655Srie 	}
2995aefb655Srie 
3000e233487SRod Evans 	return (name);
3015aefb655Srie }
3025aefb655Srie 
3035aefb655Srie /*
3045aefb655Srie  * Focal point for establishing a string table section.  Data such as the
3055aefb655Srie  * dynamic information simply points to a string table.  Data such as
3065aefb655Srie  * relocations, reference a symbol table, which in turn is associated with a
3075aefb655Srie  * string table.
3087c478bd9Sstevel@tonic-gate  */
3097c478bd9Sstevel@tonic-gate static int
3105aefb655Srie stringtbl(Cache *cache, int symtab, Word ndx, Word shnum, const char *file,
3115aefb655Srie     Word *symnum, Cache **symsec, Cache **strsec)
3125aefb655Srie {
3135aefb655Srie 	Shdr	*shdr = cache[ndx].c_shdr;
3145aefb655Srie 
3155aefb655Srie 	if (symtab) {
3165aefb655Srie 		/*
3175aefb655Srie 		 * Validate the symbol table section.
3185aefb655Srie 		 */
3195aefb655Srie 		if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
3205aefb655Srie 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
3215aefb655Srie 			    file, cache[ndx].c_name, EC_WORD(shdr->sh_link));
3225aefb655Srie 			return (0);
3235aefb655Srie 		}
3249a411307Srie 		if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
3259a411307Srie 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
3269a411307Srie 			    file, cache[ndx].c_name);
3279a411307Srie 			return (0);
3289a411307Srie 		}
3295aefb655Srie 
3305aefb655Srie 		/*
3315aefb655Srie 		 * Obtain, and verify the symbol table data.
3325aefb655Srie 		 */
3339a411307Srie 		if ((cache[ndx].c_data == NULL) ||
3349a411307Srie 		    (cache[ndx].c_data->d_buf == NULL)) {
3355aefb655Srie 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
3365aefb655Srie 			    file, cache[ndx].c_name);
3375aefb655Srie 			return (0);
3385aefb655Srie 		}
3395aefb655Srie 
3405aefb655Srie 		/*
3415aefb655Srie 		 * Establish the string table index.
3425aefb655Srie 		 */
3435aefb655Srie 		ndx = shdr->sh_link;
3445aefb655Srie 		shdr = cache[ndx].c_shdr;
3455aefb655Srie 
3465aefb655Srie 		/*
3475aefb655Srie 		 * Return symbol table information.
3485aefb655Srie 		 */
3495aefb655Srie 		if (symnum)
3505aefb655Srie 			*symnum = (shdr->sh_size / shdr->sh_entsize);
3515aefb655Srie 		if (symsec)
3525aefb655Srie 			*symsec = &cache[ndx];
3535aefb655Srie 	}
3545aefb655Srie 
3555aefb655Srie 	/*
3565aefb655Srie 	 * Validate the associated string table section.
3575aefb655Srie 	 */
3585aefb655Srie 	if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
3595aefb655Srie 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
3605aefb655Srie 		    file, cache[ndx].c_name, EC_WORD(shdr->sh_link));
3615aefb655Srie 		return (0);
3625aefb655Srie 	}
3635aefb655Srie 
3645aefb655Srie 	if (strsec)
3655aefb655Srie 		*strsec = &cache[shdr->sh_link];
3665aefb655Srie 
3675aefb655Srie 	return (1);
3685aefb655Srie }
3695aefb655Srie 
3705aefb655Srie /*
3715aefb655Srie  * Lookup a symbol and set Sym accordingly.
37231dd2c84SAli Bahrami  *
37331dd2c84SAli Bahrami  * entry:
37431dd2c84SAli Bahrami  *	name - Name of symbol to lookup
37531dd2c84SAli Bahrami  *	cache - Cache of all section headers
37631dd2c84SAli Bahrami  *	shnum - # of sections in cache
37731dd2c84SAli Bahrami  *	sym - Address of pointer to receive symbol
37831dd2c84SAli Bahrami  *	target - NULL, or section to which the symbol must be associated.
37931dd2c84SAli Bahrami  *	symtab - Symbol table to search for symbol
38031dd2c84SAli Bahrami  *	file - Name of file
38131dd2c84SAli Bahrami  *
38231dd2c84SAli Bahrami  * exit:
38331dd2c84SAli Bahrami  *	If the symbol is found, *sym is set to reference it, and True is
38431dd2c84SAli Bahrami  *	returned. If target is non-NULL, the symbol must reference the given
38531dd2c84SAli Bahrami  *	section --- otherwise the section is not checked.
38631dd2c84SAli Bahrami  *
38731dd2c84SAli Bahrami  *	If no symbol is found, False is returned.
3885aefb655Srie  */
3895aefb655Srie static int
3905aefb655Srie symlookup(const char *name, Cache *cache, Word shnum, Sym **sym,
39131dd2c84SAli Bahrami     Cache *target, Cache *symtab, const char *file)
3927c478bd9Sstevel@tonic-gate {
3935aefb655Srie 	Shdr	*shdr;
3945aefb655Srie 	Word	symn, cnt;
3955aefb655Srie 	Sym	*syms;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	if (symtab == 0)
3987c478bd9Sstevel@tonic-gate 		return (0);
3997c478bd9Sstevel@tonic-gate 
4005aefb655Srie 	shdr = symtab->c_shdr;
4015aefb655Srie 
4027c478bd9Sstevel@tonic-gate 	/*
4037c478bd9Sstevel@tonic-gate 	 * Determine the symbol data and number.
4047c478bd9Sstevel@tonic-gate 	 */
4057c478bd9Sstevel@tonic-gate 	if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
4067c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
4077c478bd9Sstevel@tonic-gate 		    file, symtab->c_name);
4087c478bd9Sstevel@tonic-gate 		return (0);
4097c478bd9Sstevel@tonic-gate 	}
4109a411307Srie 	if (symtab->c_data == NULL)
4119a411307Srie 		return (0);
4129a411307Srie 
4137c478bd9Sstevel@tonic-gate 	/* LINTED */
4145aefb655Srie 	symn = (Word)(shdr->sh_size / shdr->sh_entsize);
4155aefb655Srie 	syms = (Sym *)symtab->c_data->d_buf;
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	/*
4187c478bd9Sstevel@tonic-gate 	 * Get the associated string table section.
4197c478bd9Sstevel@tonic-gate 	 */
4207c478bd9Sstevel@tonic-gate 	if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
4217c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
4225aefb655Srie 		    file, symtab->c_name, EC_WORD(shdr->sh_link));
4237c478bd9Sstevel@tonic-gate 		return (0);
4247c478bd9Sstevel@tonic-gate 	}
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	/*
4277c478bd9Sstevel@tonic-gate 	 * Loop through the symbol table to find a match.
4287c478bd9Sstevel@tonic-gate 	 */
42931dd2c84SAli Bahrami 	*sym = NULL;
4305aefb655Srie 	for (cnt = 0; cnt < symn; syms++, cnt++) {
4315aefb655Srie 		const char	*symname;
4327c478bd9Sstevel@tonic-gate 
4335aefb655Srie 		symname = string(symtab, cnt, &cache[shdr->sh_link], file,
4345aefb655Srie 		    syms->st_name);
4357c478bd9Sstevel@tonic-gate 
43631dd2c84SAli Bahrami 		if (symname && (strcmp(name, symname) == 0) &&
43731dd2c84SAli Bahrami 		    ((target == NULL) || (target->c_ndx == syms->st_shndx))) {
43831dd2c84SAli Bahrami 			/*
43931dd2c84SAli Bahrami 			 * It is possible, though rare, for a local and
44031dd2c84SAli Bahrami 			 * global symbol of the same name to exist, each
44131dd2c84SAli Bahrami 			 * contributed by a different input object. If the
44231dd2c84SAli Bahrami 			 * symbol just found is local, remember it, but
44331dd2c84SAli Bahrami 			 * continue looking.
44431dd2c84SAli Bahrami 			 */
4455aefb655Srie 			*sym = syms;
44631dd2c84SAli Bahrami 			if (ELF_ST_BIND(syms->st_info) != STB_LOCAL)
44731dd2c84SAli Bahrami 				break;
4487c478bd9Sstevel@tonic-gate 		}
4497c478bd9Sstevel@tonic-gate 	}
45031dd2c84SAli Bahrami 
45131dd2c84SAli Bahrami 	return (*sym != NULL);
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate /*
4557c478bd9Sstevel@tonic-gate  * Print section headers.
4567c478bd9Sstevel@tonic-gate  */
4577c478bd9Sstevel@tonic-gate static void
4584f680cc6SAli Bahrami sections(const char *file, Cache *cache, Word shnum, Ehdr *ehdr, uchar_t osabi)
4597c478bd9Sstevel@tonic-gate {
4605aefb655Srie 	size_t	seccnt;
4617c478bd9Sstevel@tonic-gate 
4625aefb655Srie 	for (seccnt = 1; seccnt < shnum; seccnt++) {
4635aefb655Srie 		Cache		*_cache = &cache[seccnt];
4645aefb655Srie 		Shdr		*shdr = _cache->c_shdr;
4655aefb655Srie 		const char	*secname = _cache->c_name;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 		/*
4687c478bd9Sstevel@tonic-gate 		 * Although numerous section header entries can be zero, it's
469f5a18a30Srie 		 * usually a sign of trouble if the type is zero.
4707c478bd9Sstevel@tonic-gate 		 */
4717c478bd9Sstevel@tonic-gate 		if (shdr->sh_type == 0) {
4727c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHTYPE),
4735aefb655Srie 			    file, secname, EC_WORD(shdr->sh_type));
4747c478bd9Sstevel@tonic-gate 		}
4757c478bd9Sstevel@tonic-gate 
476981a172dSab196087 		if (!match(MATCH_F_ALL, secname, seccnt, shdr->sh_type))
477f5a18a30Srie 			continue;
4787c478bd9Sstevel@tonic-gate 
479141040e8Srie 		/*
480141040e8Srie 		 * Identify any sections that are suspicious.  A .got section
481141040e8Srie 		 * shouldn't exist in a relocatable object.
482141040e8Srie 		 */
483141040e8Srie 		if (ehdr->e_type == ET_REL) {
4845aefb655Srie 			if (strncmp(secname, MSG_ORIG(MSG_ELF_GOT),
485141040e8Srie 			    MSG_ELF_GOT_SIZE) == 0) {
486141040e8Srie 				(void) fprintf(stderr,
4875aefb655Srie 				    MSG_INTL(MSG_GOT_UNEXPECTED), file,
4885aefb655Srie 				    secname);
489141040e8Srie 			}
490141040e8Srie 		}
491141040e8Srie 
4925aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
4935aefb655Srie 		dbg_print(0, MSG_INTL(MSG_ELF_SHDR), EC_WORD(seccnt), secname);
4944f680cc6SAli Bahrami 		Elf_shdr(0, osabi, ehdr->e_machine, shdr);
4957c478bd9Sstevel@tonic-gate 	}
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate 
4985aefb655Srie /*
4995aefb655Srie  * Obtain a specified Phdr entry.
5005aefb655Srie  */
5015aefb655Srie static Phdr *
5027e16fca0SAli Bahrami getphdr(Word phnum, Word *type_arr, Word type_cnt, const char *file, Elf *elf)
5035aefb655Srie {
5047e16fca0SAli Bahrami 	Word	cnt, tcnt;
5055aefb655Srie 	Phdr	*phdr;
5065aefb655Srie 
5075aefb655Srie 	if ((phdr = elf_getphdr(elf)) == NULL) {
5085aefb655Srie 		failure(file, MSG_ORIG(MSG_ELF_GETPHDR));
5097e16fca0SAli Bahrami 		return (NULL);
5105aefb655Srie 	}
5115aefb655Srie 
5125aefb655Srie 	for (cnt = 0; cnt < phnum; phdr++, cnt++) {
5137e16fca0SAli Bahrami 		for (tcnt = 0; tcnt < type_cnt; tcnt++) {
5147e16fca0SAli Bahrami 			if (phdr->p_type == type_arr[tcnt])
5155aefb655Srie 				return (phdr);
5165aefb655Srie 		}
5177e16fca0SAli Bahrami 	}
5187e16fca0SAli Bahrami 	return (NULL);
5195aefb655Srie }
5205aefb655Srie 
521e23c41c9SAli Bahrami /*
522e23c41c9SAli Bahrami  * Display the contents of GNU/amd64 .eh_frame and .eh_frame_hdr
523e23c41c9SAli Bahrami  * sections.
524e23c41c9SAli Bahrami  *
525e23c41c9SAli Bahrami  * entry:
526e23c41c9SAli Bahrami  *	cache - Cache of all section headers
527e23c41c9SAli Bahrami  *	shndx - Index of .eh_frame or .eh_frame_hdr section to be displayed
528965630c1SRichard Lowe  *	shnum - Total number of sections which exist
529e23c41c9SAli Bahrami  *	uphdr - NULL, or unwind program header associated with
530e23c41c9SAli Bahrami  *		the .eh_frame_hdr section.
531e23c41c9SAli Bahrami  *	ehdr - ELF header for file
532e23c41c9SAli Bahrami  *	eh_state - Data used across calls to this routine. The
533e23c41c9SAli Bahrami  *		caller should zero it before the first call, and
534e23c41c9SAli Bahrami  *		pass it on every call.
535e23c41c9SAli Bahrami  *	osabi - OSABI to use in displaying information
536e23c41c9SAli Bahrami  *	file - Name of file
537e23c41c9SAli Bahrami  *	flags - Command line option flags
538e23c41c9SAli Bahrami  */
5397c478bd9Sstevel@tonic-gate static void
540965630c1SRichard Lowe unwind_eh_frame(Cache *cache, Word shndx, Word shnum, Phdr *uphdr, Ehdr *ehdr,
541e23c41c9SAli Bahrami     gnu_eh_state_t *eh_state, uchar_t osabi, const char *file, uint_t flags)
5427c478bd9Sstevel@tonic-gate {
5437e16fca0SAli Bahrami #if	defined(_ELF64)
5447e16fca0SAli Bahrami #define	MSG_UNW_BINSRTAB2	MSG_UNW_BINSRTAB2_64
5457e16fca0SAli Bahrami #define	MSG_UNW_BINSRTABENT	MSG_UNW_BINSRTABENT_64
5467e16fca0SAli Bahrami #else
5477e16fca0SAli Bahrami #define	MSG_UNW_BINSRTAB2	MSG_UNW_BINSRTAB2_32
5487e16fca0SAli Bahrami #define	MSG_UNW_BINSRTABENT	MSG_UNW_BINSRTABENT_32
5497e16fca0SAli Bahrami #endif
5507e16fca0SAli Bahrami 
551e23c41c9SAli Bahrami 	Cache			*_cache = &cache[shndx];
5525aefb655Srie 	Shdr			*shdr = _cache->c_shdr;
553e23c41c9SAli Bahrami 	uchar_t			*data = (uchar_t *)(_cache->c_data->d_buf);
554e23c41c9SAli Bahrami 	size_t			datasize = _cache->c_data->d_size;
555e23c41c9SAli Bahrami 	Conv_dwarf_ehe_buf_t	dwarf_ehe_buf;
5567e16fca0SAli Bahrami 	uint64_t		ndx, frame_ptr, fde_cnt, tabndx;
5575aefb655Srie 	uint_t			vers, frame_ptr_enc, fde_cnt_enc, table_enc;
55837915d86SRichard Lowe 	uint64_t		initloc, initloc0 = 0;
559965630c1SRichard Lowe 	uint64_t		gotaddr = 0;
560965630c1SRichard Lowe 	int			cnt;
561e23c41c9SAli Bahrami 
562965630c1SRichard Lowe 	for (cnt = 1; cnt < shnum; cnt++) {
563965630c1SRichard Lowe 		if (strncmp(cache[cnt].c_name, MSG_ORIG(MSG_ELF_GOT),
564965630c1SRichard Lowe 		    MSG_ELF_GOT_SIZE) == 0) {
565965630c1SRichard Lowe 			gotaddr = cache[cnt].c_shdr->sh_addr;
566965630c1SRichard Lowe 			break;
567965630c1SRichard Lowe 		}
568965630c1SRichard Lowe 	}
5697c478bd9Sstevel@tonic-gate 
57037915d86SRichard Lowe 	if ((data == NULL) || (datasize == 0)) {
57137915d86SRichard Lowe 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
57237915d86SRichard Lowe 		    file, _cache ->c_name);
57337915d86SRichard Lowe 		return;
57437915d86SRichard Lowe 	}
57537915d86SRichard Lowe 
5767c478bd9Sstevel@tonic-gate 	/*
577e23c41c9SAli Bahrami 	 * Is this a .eh_frame_hdr?
5787c478bd9Sstevel@tonic-gate 	 */
5795aefb655Srie 	if ((uphdr && (shdr->sh_addr == uphdr->p_vaddr)) ||
5807c478bd9Sstevel@tonic-gate 	    (strncmp(_cache->c_name, MSG_ORIG(MSG_SCN_FRMHDR),
5817c478bd9Sstevel@tonic-gate 	    MSG_SCN_FRMHDR_SIZE) == 0)) {
5827e16fca0SAli Bahrami 		/*
5837e16fca0SAli Bahrami 		 * There can only be a single .eh_frame_hdr.
5847e16fca0SAli Bahrami 		 * Flag duplicates.
5857e16fca0SAli Bahrami 		 */
586e23c41c9SAli Bahrami 		if (++eh_state->hdr_cnt > 1)
587e23c41c9SAli Bahrami 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_MULTEHFRMHDR),
588e23c41c9SAli Bahrami 			    file, EC_WORD(shndx), _cache->c_name);
5897e16fca0SAli Bahrami 
5905aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_UNW_FRMHDR));
5917c478bd9Sstevel@tonic-gate 		ndx = 0;
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 		vers = data[ndx++];
5947c478bd9Sstevel@tonic-gate 		frame_ptr_enc = data[ndx++];
5957c478bd9Sstevel@tonic-gate 		fde_cnt_enc = data[ndx++];
5967c478bd9Sstevel@tonic-gate 		table_enc = data[ndx++];
5977c478bd9Sstevel@tonic-gate 
5985aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_UNW_FRMVERS), vers);
5997c478bd9Sstevel@tonic-gate 
60037915d86SRichard Lowe 		switch (dwarf_ehe_extract(data, datasize, &ndx,
60137915d86SRichard Lowe 		    &frame_ptr, frame_ptr_enc, ehdr->e_ident, B_TRUE,
60237915d86SRichard Lowe 		    shdr->sh_addr, ndx, gotaddr)) {
60337915d86SRichard Lowe 		case DW_OVERFLOW:
60437915d86SRichard Lowe 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_DWOVRFLW),
60537915d86SRichard Lowe 			    file, _cache->c_name);
60637915d86SRichard Lowe 			return;
60737915d86SRichard Lowe 		case DW_BAD_ENCODING:
60837915d86SRichard Lowe 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_DWBADENC),
60937915d86SRichard Lowe 			    file, _cache->c_name, frame_ptr_enc);
61037915d86SRichard Lowe 			return;
61137915d86SRichard Lowe 		case DW_SUCCESS:
61237915d86SRichard Lowe 			break;
61337915d86SRichard Lowe 		}
614e23c41c9SAli Bahrami 		if (eh_state->hdr_cnt == 1) {
615e23c41c9SAli Bahrami 			eh_state->hdr_ndx = shndx;
616e23c41c9SAli Bahrami 			eh_state->frame_ptr = frame_ptr;
6177e16fca0SAli Bahrami 		}
6187c478bd9Sstevel@tonic-gate 
6195aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_UNW_FRPTRENC),
620de777a60Sab196087 		    conv_dwarf_ehe(frame_ptr_enc, &dwarf_ehe_buf),
621de777a60Sab196087 		    EC_XWORD(frame_ptr));
6227c478bd9Sstevel@tonic-gate 
62337915d86SRichard Lowe 		switch (dwarf_ehe_extract(data, datasize, &ndx, &fde_cnt,
62437915d86SRichard Lowe 		    fde_cnt_enc, ehdr->e_ident, B_TRUE, shdr->sh_addr, ndx,
62537915d86SRichard Lowe 		    gotaddr)) {
62637915d86SRichard Lowe 		case DW_OVERFLOW:
62737915d86SRichard Lowe 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_DWOVRFLW),
62837915d86SRichard Lowe 			    file, _cache->c_name);
62937915d86SRichard Lowe 			return;
63037915d86SRichard Lowe 		case DW_BAD_ENCODING:
63137915d86SRichard Lowe 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_DWBADENC),
63237915d86SRichard Lowe 			    file, _cache->c_name, fde_cnt_enc);
63337915d86SRichard Lowe 			return;
63437915d86SRichard Lowe 		case DW_SUCCESS:
63537915d86SRichard Lowe 			break;
63637915d86SRichard Lowe 		}
6375aefb655Srie 
6385aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_UNW_FDCNENC),
639de777a60Sab196087 		    conv_dwarf_ehe(fde_cnt_enc, &dwarf_ehe_buf),
640de777a60Sab196087 		    EC_XWORD(fde_cnt));
6415aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_UNW_TABENC),
642de777a60Sab196087 		    conv_dwarf_ehe(table_enc, &dwarf_ehe_buf));
6435aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_UNW_BINSRTAB1));
6445aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_UNW_BINSRTAB2));
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 		for (tabndx = 0; tabndx < fde_cnt; tabndx++) {
64737915d86SRichard Lowe 			uint64_t table;
64837915d86SRichard Lowe 
64937915d86SRichard Lowe 			switch (dwarf_ehe_extract(data, datasize, &ndx,
65037915d86SRichard Lowe 			    &initloc, table_enc, ehdr->e_ident, B_TRUE,
65137915d86SRichard Lowe 			    shdr->sh_addr, ndx, gotaddr)) {
65237915d86SRichard Lowe 			case DW_OVERFLOW:
65337915d86SRichard Lowe 				(void) fprintf(stderr,
65437915d86SRichard Lowe 				    MSG_INTL(MSG_ERR_DWOVRFLW), file,
65537915d86SRichard Lowe 				    _cache->c_name);
65637915d86SRichard Lowe 				return;
65737915d86SRichard Lowe 			case DW_BAD_ENCODING:
65837915d86SRichard Lowe 				(void) fprintf(stderr,
65937915d86SRichard Lowe 				    MSG_INTL(MSG_ERR_DWBADENC), file,
66037915d86SRichard Lowe 				    _cache->c_name, table_enc);
66137915d86SRichard Lowe 				return;
66237915d86SRichard Lowe 			case DW_SUCCESS:
66337915d86SRichard Lowe 				break;
66437915d86SRichard Lowe 			}
665e23c41c9SAli Bahrami 			if ((tabndx != 0) && (initloc0 > initloc))
6661dd9d86fSAli Bahrami 				(void) fprintf(stderr,
6671dd9d86fSAli Bahrami 				    MSG_INTL(MSG_ERR_BADSORT), file,
6681dd9d86fSAli Bahrami 				    _cache->c_name, EC_WORD(tabndx));
66937915d86SRichard Lowe 			switch (dwarf_ehe_extract(data, datasize, &ndx, &table,
67037915d86SRichard Lowe 			    table_enc, ehdr->e_ident, B_TRUE, shdr->sh_addr,
67137915d86SRichard Lowe 			    ndx, gotaddr)) {
67237915d86SRichard Lowe 			case DW_OVERFLOW:
67337915d86SRichard Lowe 				(void) fprintf(stderr,
67437915d86SRichard Lowe 				    MSG_INTL(MSG_ERR_DWOVRFLW), file,
67537915d86SRichard Lowe 				    _cache->c_name);
67637915d86SRichard Lowe 				return;
67737915d86SRichard Lowe 			case DW_BAD_ENCODING:
67837915d86SRichard Lowe 				(void) fprintf(stderr,
67937915d86SRichard Lowe 				    MSG_INTL(MSG_ERR_DWBADENC), file,
68037915d86SRichard Lowe 				    _cache->c_name, table_enc);
68137915d86SRichard Lowe 				return;
68237915d86SRichard Lowe 			case DW_SUCCESS:
68337915d86SRichard Lowe 				break;
68437915d86SRichard Lowe 			}
68537915d86SRichard Lowe 
6865aefb655Srie 			dbg_print(0, MSG_ORIG(MSG_UNW_BINSRTABENT),
687e23c41c9SAli Bahrami 			    EC_XWORD(initloc),
68837915d86SRichard Lowe 			    EC_XWORD(table));
689e23c41c9SAli Bahrami 			initloc0 = initloc;
6907c478bd9Sstevel@tonic-gate 		}
6917e16fca0SAli Bahrami 	} else {		/* Display the .eh_frame section */
692e23c41c9SAli Bahrami 		eh_state->frame_cnt++;
693e23c41c9SAli Bahrami 		if (eh_state->frame_cnt == 1) {
694e23c41c9SAli Bahrami 			eh_state->frame_ndx = shndx;
695e23c41c9SAli Bahrami 			eh_state->frame_base = shdr->sh_addr;
696e23c41c9SAli Bahrami 		} else if ((eh_state->frame_cnt >  1) &&
6977e16fca0SAli Bahrami 		    (ehdr->e_type != ET_REL)) {
6987e16fca0SAli Bahrami 			Conv_inv_buf_t	inv_buf;
6997e16fca0SAli Bahrami 
700e23c41c9SAli Bahrami 			(void) fprintf(stderr, MSG_INTL(MSG_WARN_MULTEHFRM),
701e23c41c9SAli Bahrami 			    file, EC_WORD(shndx), _cache->c_name,
702e23c41c9SAli Bahrami 			    conv_ehdr_type(osabi, ehdr->e_type, 0, &inv_buf));
7037e16fca0SAli Bahrami 		}
70437915d86SRichard Lowe 		dump_eh_frame(file, _cache->c_name, data, datasize,
70537915d86SRichard Lowe 		    shdr->sh_addr, ehdr->e_machine, ehdr->e_ident, gotaddr);
7067c478bd9Sstevel@tonic-gate 	}
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	/*
709e23c41c9SAli Bahrami 	 * If we've seen the .eh_frame_hdr and the first .eh_frame section,
710e23c41c9SAli Bahrami 	 * compare the header frame_ptr to the address of the actual frame
711e23c41c9SAli Bahrami 	 * section to ensure the link-editor got this right.  Note, this
712e23c41c9SAli Bahrami 	 * diagnostic is only produced when unwind information is explicitly
713e23c41c9SAli Bahrami 	 * asked for, as shared objects built with an older ld(1) may reveal
714e23c41c9SAli Bahrami 	 * this inconsistency.  Although an inconsistency, it doesn't seem to
715e23c41c9SAli Bahrami 	 * have any adverse effect on existing tools.
7167c478bd9Sstevel@tonic-gate 	 */
71757ef7aa9SRod Evans 	if (((flags & FLG_MASK_SHOW) != FLG_MASK_SHOW) &&
718e23c41c9SAli Bahrami 	    (eh_state->hdr_cnt > 0) && (eh_state->frame_cnt > 0) &&
719e23c41c9SAli Bahrami 	    (eh_state->frame_ptr != eh_state->frame_base))
7207e16fca0SAli Bahrami 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADEHFRMPTR),
721e23c41c9SAli Bahrami 		    file, EC_WORD(eh_state->hdr_ndx),
722e23c41c9SAli Bahrami 		    cache[eh_state->hdr_ndx].c_name,
723e23c41c9SAli Bahrami 		    EC_XWORD(eh_state->frame_ptr),
724e23c41c9SAli Bahrami 		    EC_WORD(eh_state->frame_ndx),
725e23c41c9SAli Bahrami 		    cache[eh_state->frame_ndx].c_name,
726e23c41c9SAli Bahrami 		    EC_XWORD(eh_state->frame_base));
7277e16fca0SAli Bahrami #undef MSG_UNW_BINSRTAB2
7287e16fca0SAli Bahrami #undef MSG_UNW_BINSRTABENT
7297c478bd9Sstevel@tonic-gate }
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate /*
732e23c41c9SAli Bahrami  * Convert a self relative pointer into an address. A self relative
733e23c41c9SAli Bahrami  * pointer adds the address where the pointer resides to the offset
734e23c41c9SAli Bahrami  * contained in the pointer. The benefit is that the value of the
735e23c41c9SAli Bahrami  * pointer does not require relocation.
736e23c41c9SAli Bahrami  *
737e23c41c9SAli Bahrami  * entry:
738e23c41c9SAli Bahrami  *	base_addr - Address of the pointer.
739e23c41c9SAli Bahrami  *	delta - Offset relative to base_addr giving desired address
740e23c41c9SAli Bahrami  *
741e23c41c9SAli Bahrami  * exit:
742e23c41c9SAli Bahrami  *	The computed address is returned.
743e23c41c9SAli Bahrami  *
744e23c41c9SAli Bahrami  * note:
745e23c41c9SAli Bahrami  *	base_addr is an unsigned value, while ret_addr is signed. This routine
746e23c41c9SAli Bahrami  *	used explicit testing and casting to explicitly control type
747e23c41c9SAli Bahrami  *	conversion, and ensure that we handle the maximum possible range.
748e23c41c9SAli Bahrami  */
749e23c41c9SAli Bahrami static Addr
750e23c41c9SAli Bahrami srelptr(Addr base_addr, PTRDIFF_T delta)
751e23c41c9SAli Bahrami {
752e23c41c9SAli Bahrami 	if (delta < 0)
753e23c41c9SAli Bahrami 		return (base_addr - (Addr) (-delta));
754e23c41c9SAli Bahrami 
755e23c41c9SAli Bahrami 	return (base_addr + (Addr) delta);
756e23c41c9SAli Bahrami }
757e23c41c9SAli Bahrami 
758e23c41c9SAli Bahrami /*
759e23c41c9SAli Bahrami  * Byte swap a PTRDIFF_T value.
760e23c41c9SAli Bahrami  */
761e23c41c9SAli Bahrami static PTRDIFF_T
762e23c41c9SAli Bahrami swap_ptrdiff(PTRDIFF_T value)
763e23c41c9SAli Bahrami {
764e23c41c9SAli Bahrami 	PTRDIFF_T r;
765e23c41c9SAli Bahrami 	uchar_t	*dst = (uchar_t *)&r;
766e23c41c9SAli Bahrami 	uchar_t	*src = (uchar_t *)&value;
767e23c41c9SAli Bahrami 
768e23c41c9SAli Bahrami 	UL_ASSIGN_BSWAP_XWORD(dst, src);
769e23c41c9SAli Bahrami 	return (r);
770e23c41c9SAli Bahrami }
771e23c41c9SAli Bahrami 
772e23c41c9SAli Bahrami /*
773e23c41c9SAli Bahrami  * Display exception_range_entry items from the .exception_ranges section
774e23c41c9SAli Bahrami  * of a Sun C++ object.
775e23c41c9SAli Bahrami  */
776e23c41c9SAli Bahrami static void
777e23c41c9SAli Bahrami unwind_exception_ranges(Cache *_cache, const char *file, int do_swap)
778e23c41c9SAli Bahrami {
779e23c41c9SAli Bahrami 	/*
780e23c41c9SAli Bahrami 	 * Translate a PTRDIFF_T self-relative address field of
781e23c41c9SAli Bahrami 	 * an exception_range_entry struct into an address.
782e23c41c9SAli Bahrami 	 *
783e23c41c9SAli Bahrami 	 * entry:
784e23c41c9SAli Bahrami 	 *	exc_addr - Address of base of exception_range_entry struct
785e23c41c9SAli Bahrami 	 *	cur_ent - Pointer to data in the struct to be translated
786e23c41c9SAli Bahrami 	 *
787e23c41c9SAli Bahrami 	 *	_f - Field of struct to be translated
788e23c41c9SAli Bahrami 	 */
789e23c41c9SAli Bahrami #define	SRELPTR(_f) \
790e23c41c9SAli Bahrami 	srelptr(exc_addr + offsetof(exception_range_entry, _f), cur_ent->_f)
791e23c41c9SAli Bahrami 
792e23c41c9SAli Bahrami #if	defined(_ELF64)
793e23c41c9SAli Bahrami #define	MSG_EXR_TITLE	MSG_EXR_TITLE_64
794e23c41c9SAli Bahrami #define	MSG_EXR_ENTRY	MSG_EXR_ENTRY_64
795e23c41c9SAli Bahrami #else
796e23c41c9SAli Bahrami #define	MSG_EXR_TITLE	MSG_EXR_TITLE_32
797e23c41c9SAli Bahrami #define	MSG_EXR_ENTRY	MSG_EXR_ENTRY_32
798e23c41c9SAli Bahrami #endif
799e23c41c9SAli Bahrami 
800e23c41c9SAli Bahrami 	exception_range_entry	scratch, *ent, *cur_ent = &scratch;
801e23c41c9SAli Bahrami 	char			index[MAXNDXSIZE];
802e23c41c9SAli Bahrami 	Word			i, nelts;
80337915d86SRichard Lowe 	Addr			addr, addr0 = 0, offset = 0;
804e23c41c9SAli Bahrami 	Addr			exc_addr = _cache->c_shdr->sh_addr;
805e23c41c9SAli Bahrami 
806e23c41c9SAli Bahrami 	dbg_print(0, MSG_INTL(MSG_EXR_TITLE));
807e23c41c9SAli Bahrami 	ent = (exception_range_entry *)(_cache->c_data->d_buf);
808e23c41c9SAli Bahrami 	nelts = _cache->c_data->d_size / sizeof (exception_range_entry);
809e23c41c9SAli Bahrami 
810e23c41c9SAli Bahrami 	for (i = 0; i < nelts; i++, ent++) {
811e23c41c9SAli Bahrami 		if (do_swap) {
812e23c41c9SAli Bahrami 			/*
813e23c41c9SAli Bahrami 			 * Copy byte swapped values into the scratch buffer.
814e23c41c9SAli Bahrami 			 * The reserved field is not used, so we skip it.
815e23c41c9SAli Bahrami 			 */
816e23c41c9SAli Bahrami 			scratch.ret_addr = swap_ptrdiff(ent->ret_addr);
817e23c41c9SAli Bahrami 			scratch.length = BSWAP_XWORD(ent->length);
818e23c41c9SAli Bahrami 			scratch.handler_addr = swap_ptrdiff(ent->handler_addr);
819e23c41c9SAli Bahrami 			scratch.type_block = swap_ptrdiff(ent->type_block);
820e23c41c9SAli Bahrami 		} else {
821e23c41c9SAli Bahrami 			cur_ent = ent;
822e23c41c9SAli Bahrami 		}
823e23c41c9SAli Bahrami 
824e23c41c9SAli Bahrami 		/*
825e23c41c9SAli Bahrami 		 * The table is required to be sorted by the address
826e23c41c9SAli Bahrami 		 * derived from ret_addr, to allow binary searching. Ensure
827e23c41c9SAli Bahrami 		 * that addresses grow monotonically.
828e23c41c9SAli Bahrami 		 */
829e23c41c9SAli Bahrami 		addr = SRELPTR(ret_addr);
830e23c41c9SAli Bahrami 		if ((i != 0) && (addr0 > addr))
8311dd9d86fSAli Bahrami 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSORT),
832e23c41c9SAli Bahrami 			    file, _cache->c_name, EC_WORD(i));
833e23c41c9SAli Bahrami 
834e23c41c9SAli Bahrami 		(void) snprintf(index, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INDEX),
835e23c41c9SAli Bahrami 		    EC_XWORD(i));
836e23c41c9SAli Bahrami 		dbg_print(0, MSG_INTL(MSG_EXR_ENTRY), index, EC_ADDR(offset),
837e23c41c9SAli Bahrami 		    EC_ADDR(addr), EC_ADDR(cur_ent->length),
838e23c41c9SAli Bahrami 		    EC_ADDR(SRELPTR(handler_addr)),
839e23c41c9SAli Bahrami 		    EC_ADDR(SRELPTR(type_block)));
840e23c41c9SAli Bahrami 
841e23c41c9SAli Bahrami 		addr0 = addr;
842e23c41c9SAli Bahrami 		exc_addr += sizeof (exception_range_entry);
843e23c41c9SAli Bahrami 		offset += sizeof (exception_range_entry);
844e23c41c9SAli Bahrami 	}
845e23c41c9SAli Bahrami 
846e23c41c9SAli Bahrami #undef SRELPTR
847e23c41c9SAli Bahrami #undef MSG_EXR_TITLE
848e23c41c9SAli Bahrami #undef MSG_EXR_ENTRY
849e23c41c9SAli Bahrami }
850e23c41c9SAli Bahrami 
851e23c41c9SAli Bahrami /*
852e23c41c9SAli Bahrami  * Display information from unwind/exception sections:
853e23c41c9SAli Bahrami  *
854e23c41c9SAli Bahrami  * -	GNU/amd64 .eh_frame and .eh_frame_hdr
855e23c41c9SAli Bahrami  * -	Sun C++ .exception_ranges
856e23c41c9SAli Bahrami  *
857e23c41c9SAli Bahrami  */
858e23c41c9SAli Bahrami static void
859e23c41c9SAli Bahrami unwind(Cache *cache, Word shnum, Word phnum, Ehdr *ehdr, uchar_t osabi,
860e23c41c9SAli Bahrami     const char *file, Elf *elf, uint_t flags)
861e23c41c9SAli Bahrami {
862e23c41c9SAli Bahrami 	static Word phdr_types[] = { PT_SUNW_UNWIND, PT_SUNW_EH_FRAME };
863e23c41c9SAli Bahrami 
864e23c41c9SAli Bahrami 	Word			cnt;
865e23c41c9SAli Bahrami 	Phdr			*uphdr = NULL;
866e23c41c9SAli Bahrami 	gnu_eh_state_t		eh_state;
867e23c41c9SAli Bahrami 
868e23c41c9SAli Bahrami 	/*
869e23c41c9SAli Bahrami 	 * Historical background: .eh_frame and .eh_frame_hdr sections
870e23c41c9SAli Bahrami 	 * come from the GNU compilers (particularly C++), and are used
871e23c41c9SAli Bahrami 	 * under all architectures. Their format is based on DWARF. When
872e23c41c9SAli Bahrami 	 * the amd64 ABI was defined, these sections were adopted wholesale
873e23c41c9SAli Bahrami 	 * from the existing practice.
874e23c41c9SAli Bahrami 	 *
875e23c41c9SAli Bahrami 	 * When amd64 support was added to Solaris, support for these
876e23c41c9SAli Bahrami 	 * sections was added, using the SHT_AMD64_UNWIND section type
877e23c41c9SAli Bahrami 	 * to identify them. At first, we ignored them in objects for
878e23c41c9SAli Bahrami 	 * non-amd64 targets, but later broadened our support to include
879e23c41c9SAli Bahrami 	 * other architectures in order to better support gcc-generated
880e23c41c9SAli Bahrami 	 * objects.
881e23c41c9SAli Bahrami 	 *
882e23c41c9SAli Bahrami 	 * .exception_ranges implement the same basic concepts, but
883e23c41c9SAli Bahrami 	 * were invented at Sun for the Sun C++ compiler.
884e23c41c9SAli Bahrami 	 *
885e23c41c9SAli Bahrami 	 * We match these sections by name, rather than section type,
886e23c41c9SAli Bahrami 	 * because they can come in as either SHT_AMD64_UNWIND, or as
887e23c41c9SAli Bahrami 	 * SHT_PROGBITS, and because the type isn't enough to determine
88808278a5eSRod Evans 	 * how they should be interpreted.
889e23c41c9SAli Bahrami 	 */
890e23c41c9SAli Bahrami 	/* Find the program header for .eh_frame_hdr if present */
891e23c41c9SAli Bahrami 	if (phnum)
892e23c41c9SAli Bahrami 		uphdr = getphdr(phnum, phdr_types,
893e23c41c9SAli Bahrami 		    sizeof (phdr_types) / sizeof (*phdr_types), file, elf);
894e23c41c9SAli Bahrami 
895e23c41c9SAli Bahrami 	/*
896e23c41c9SAli Bahrami 	 * eh_state is used to retain data used by unwind_eh_frame()
89708278a5eSRod Evans 	 * across calls.
898e23c41c9SAli Bahrami 	 */
899e23c41c9SAli Bahrami 	bzero(&eh_state, sizeof (eh_state));
900e23c41c9SAli Bahrami 
901e23c41c9SAli Bahrami 	for (cnt = 1; cnt < shnum; cnt++) {
902e23c41c9SAli Bahrami 		Cache		*_cache = &cache[cnt];
903e23c41c9SAli Bahrami 		Shdr		*shdr = _cache->c_shdr;
904e23c41c9SAli Bahrami 		int		is_exrange;
905e23c41c9SAli Bahrami 
906e23c41c9SAli Bahrami 		/*
907e23c41c9SAli Bahrami 		 * Skip sections of the wrong type. On amd64, they
908e23c41c9SAli Bahrami 		 * can be SHT_AMD64_UNWIND. On all platforms, they
909e23c41c9SAli Bahrami 		 * can be SHT_PROGBITS (including amd64, if using
910e23c41c9SAli Bahrami 		 * the GNU compilers).
911e23c41c9SAli Bahrami 		 *
912e23c41c9SAli Bahrami 		 * Skip anything other than these two types. The name
913e23c41c9SAli Bahrami 		 * test below will thin out the SHT_PROGBITS that don't apply.
914e23c41c9SAli Bahrami 		 */
915e23c41c9SAli Bahrami 		if ((shdr->sh_type != SHT_PROGBITS) &&
916e23c41c9SAli Bahrami 		    (shdr->sh_type != SHT_AMD64_UNWIND))
917e23c41c9SAli Bahrami 			continue;
918e23c41c9SAli Bahrami 
919e23c41c9SAli Bahrami 		/*
920e23c41c9SAli Bahrami 		 * Only sections with certain well known names are of interest.
921e23c41c9SAli Bahrami 		 * These are:
922e23c41c9SAli Bahrami 		 *
923e23c41c9SAli Bahrami 		 *	.eh_frame - amd64/GNU-compiler unwind sections
924e23c41c9SAli Bahrami 		 *	.eh_frame_hdr - Sorted table referencing .eh_frame
925e23c41c9SAli Bahrami 		 *	.exception_ranges - Sun C++ unwind sections
926e23c41c9SAli Bahrami 		 *
927e23c41c9SAli Bahrami 		 * We do a prefix comparison, allowing for naming conventions
928e23c41c9SAli Bahrami 		 * like .eh_frame.foo, hence the use of strncmp() rather than
929e23c41c9SAli Bahrami 		 * strcmp(). This means that we only really need to test for
930e23c41c9SAli Bahrami 		 * .eh_frame, as it's a prefix of .eh_frame_hdr.
931e23c41c9SAli Bahrami 		 */
932e23c41c9SAli Bahrami 		is_exrange =  strncmp(_cache->c_name,
933e23c41c9SAli Bahrami 		    MSG_ORIG(MSG_SCN_EXRANGE), MSG_SCN_EXRANGE_SIZE) == 0;
934e23c41c9SAli Bahrami 		if ((strncmp(_cache->c_name, MSG_ORIG(MSG_SCN_FRM),
935e23c41c9SAli Bahrami 		    MSG_SCN_FRM_SIZE) != 0) && !is_exrange)
936e23c41c9SAli Bahrami 			continue;
937e23c41c9SAli Bahrami 
938e23c41c9SAli Bahrami 		if (!match(MATCH_F_ALL, _cache->c_name, cnt, shdr->sh_type))
939e23c41c9SAli Bahrami 			continue;
940e23c41c9SAli Bahrami 
941e23c41c9SAli Bahrami 		if (_cache->c_data == NULL)
942e23c41c9SAli Bahrami 			continue;
943e23c41c9SAli Bahrami 
944e23c41c9SAli Bahrami 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
945e23c41c9SAli Bahrami 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_UNWIND), _cache->c_name);
946e23c41c9SAli Bahrami 
947e23c41c9SAli Bahrami 		if (is_exrange)
948e23c41c9SAli Bahrami 			unwind_exception_ranges(_cache, file,
949e23c41c9SAli Bahrami 			    _elf_sys_encoding() != ehdr->e_ident[EI_DATA]);
950e23c41c9SAli Bahrami 		else
951965630c1SRichard Lowe 			unwind_eh_frame(cache, cnt, shnum, uphdr, ehdr,
952965630c1SRichard Lowe 			    &eh_state, osabi, file, flags);
953e23c41c9SAli Bahrami 	}
954e23c41c9SAli Bahrami }
955e23c41c9SAli Bahrami 
956e23c41c9SAli Bahrami /*
95708278a5eSRod Evans  * Initialize a symbol table state structure
95808278a5eSRod Evans  *
95908278a5eSRod Evans  * entry:
96008278a5eSRod Evans  *	state - State structure to be initialized
96108278a5eSRod Evans  *	cache - Cache of all section headers
96208278a5eSRod Evans  *	shnum - # of sections in cache
96308278a5eSRod Evans  *	secndx - Index of symbol table section
96408278a5eSRod Evans  *	ehdr - ELF header for file
96508278a5eSRod Evans  *	versym - Information about versym section
96608278a5eSRod Evans  *	file - Name of file
96708278a5eSRod Evans  *	flags - Command line option flags
96808278a5eSRod Evans  */
96908278a5eSRod Evans static int
97008278a5eSRod Evans init_symtbl_state(SYMTBL_STATE *state, Cache *cache, Word shnum, Word secndx,
97108278a5eSRod Evans     Ehdr *ehdr, uchar_t osabi, VERSYM_STATE *versym, const char *file,
97208278a5eSRod Evans     uint_t flags)
97308278a5eSRod Evans {
97408278a5eSRod Evans 	Shdr *shdr;
97508278a5eSRod Evans 
97608278a5eSRod Evans 	state->file = file;
97708278a5eSRod Evans 	state->ehdr = ehdr;
97808278a5eSRod Evans 	state->cache = cache;
97908278a5eSRod Evans 	state->osabi = osabi;
98008278a5eSRod Evans 	state->shnum = shnum;
98108278a5eSRod Evans 	state->seccache = &cache[secndx];
98208278a5eSRod Evans 	state->secndx = secndx;
98308278a5eSRod Evans 	state->secname = state->seccache->c_name;
98408278a5eSRod Evans 	state->flags = flags;
98508278a5eSRod Evans 	state->shxndx.checked = 0;
98608278a5eSRod Evans 	state->shxndx.data = NULL;
98708278a5eSRod Evans 	state->shxndx.n = 0;
98808278a5eSRod Evans 
98908278a5eSRod Evans 	shdr = state->seccache->c_shdr;
99008278a5eSRod Evans 
99108278a5eSRod Evans 	/*
99208278a5eSRod Evans 	 * Check the symbol data and per-item size.
99308278a5eSRod Evans 	 */
99408278a5eSRod Evans 	if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
99508278a5eSRod Evans 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
99608278a5eSRod Evans 		    file, state->secname);
99708278a5eSRod Evans 		return (0);
99808278a5eSRod Evans 	}
99908278a5eSRod Evans 	if (state->seccache->c_data == NULL)
100008278a5eSRod Evans 		return (0);
100108278a5eSRod Evans 
100208278a5eSRod Evans 	/* LINTED */
100308278a5eSRod Evans 	state->symn = (Word)(shdr->sh_size / shdr->sh_entsize);
100408278a5eSRod Evans 	state->sym = (Sym *)state->seccache->c_data->d_buf;
100508278a5eSRod Evans 
100608278a5eSRod Evans 	/*
100708278a5eSRod Evans 	 * Check associated string table section.
100808278a5eSRod Evans 	 */
100908278a5eSRod Evans 	if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
101008278a5eSRod Evans 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
101108278a5eSRod Evans 		    file, state->secname, EC_WORD(shdr->sh_link));
101208278a5eSRod Evans 		return (0);
101308278a5eSRod Evans 	}
101408278a5eSRod Evans 
101508278a5eSRod Evans 	/*
101608278a5eSRod Evans 	 * Determine if there is a associated Versym section
101708278a5eSRod Evans 	 * with this Symbol Table.
101808278a5eSRod Evans 	 */
101908278a5eSRod Evans 	if (versym && versym->cache &&
102008278a5eSRod Evans 	    (versym->cache->c_shdr->sh_link == state->secndx))
102108278a5eSRod Evans 		state->versym = versym;
102208278a5eSRod Evans 	else
102308278a5eSRod Evans 		state->versym = NULL;
102408278a5eSRod Evans 
102508278a5eSRod Evans 
102608278a5eSRod Evans 	return (1);
102708278a5eSRod Evans }
102808278a5eSRod Evans 
102908278a5eSRod Evans /*
103008278a5eSRod Evans  * Determine the extended section index used for symbol tables entries.
103108278a5eSRod Evans  */
103208278a5eSRod Evans static void
103308278a5eSRod Evans symbols_getxindex(SYMTBL_STATE *state)
103408278a5eSRod Evans {
103508278a5eSRod Evans 	uint_t	symn;
103608278a5eSRod Evans 	Word	symcnt;
103708278a5eSRod Evans 
103808278a5eSRod Evans 	state->shxndx.checked = 1;   /* Note that we've been called */
103908278a5eSRod Evans 	for (symcnt = 1; symcnt < state->shnum; symcnt++) {
104008278a5eSRod Evans 		Cache	*_cache = &state->cache[symcnt];
104108278a5eSRod Evans 		Shdr	*shdr = _cache->c_shdr;
104208278a5eSRod Evans 
104308278a5eSRod Evans 		if ((shdr->sh_type != SHT_SYMTAB_SHNDX) ||
104408278a5eSRod Evans 		    (shdr->sh_link != state->secndx))
104508278a5eSRod Evans 			continue;
104608278a5eSRod Evans 
104708278a5eSRod Evans 		if ((shdr->sh_entsize) &&
104808278a5eSRod Evans 		    /* LINTED */
104908278a5eSRod Evans 		    ((symn = (uint_t)(shdr->sh_size / shdr->sh_entsize)) == 0))
105008278a5eSRod Evans 			continue;
105108278a5eSRod Evans 
105208278a5eSRod Evans 		if (_cache->c_data == NULL)
105308278a5eSRod Evans 			continue;
105408278a5eSRod Evans 
105508278a5eSRod Evans 		state->shxndx.data = _cache->c_data->d_buf;
105608278a5eSRod Evans 		state->shxndx.n = symn;
105708278a5eSRod Evans 		return;
105808278a5eSRod Evans 	}
105908278a5eSRod Evans }
106008278a5eSRod Evans 
106108278a5eSRod Evans /*
106208278a5eSRod Evans  * Produce a line of output for the given symbol
106308278a5eSRod Evans  *
106408278a5eSRod Evans  * entry:
106508278a5eSRod Evans  *	state - Symbol table state
106608278a5eSRod Evans  *	symndx - Index of symbol within the table
106708278a5eSRod Evans  *	info - Value of st_info (indicates local/global range)
106808278a5eSRod Evans  *	symndx_disp - Index to display. This may not be the same
106908278a5eSRod Evans  *		as symndx if the display is relative to the logical
107008278a5eSRod Evans  *		combination of the SUNW_ldynsym/dynsym tables.
107108278a5eSRod Evans  *	sym - Symbol to display
107208278a5eSRod Evans  */
107308278a5eSRod Evans static void
107408278a5eSRod Evans output_symbol(SYMTBL_STATE *state, Word symndx, Word info, Word disp_symndx,
107508278a5eSRod Evans     Sym *sym)
107608278a5eSRod Evans {
107708278a5eSRod Evans 	/*
107808278a5eSRod Evans 	 * Symbol types for which we check that the specified
107908278a5eSRod Evans 	 * address/size land inside the target section.
108008278a5eSRod Evans 	 */
108108278a5eSRod Evans 	static const int addr_symtype[] = {
108208278a5eSRod Evans 		0,			/* STT_NOTYPE */
108308278a5eSRod Evans 		1,			/* STT_OBJECT */
108408278a5eSRod Evans 		1,			/* STT_FUNC */
108508278a5eSRod Evans 		0,			/* STT_SECTION */
108608278a5eSRod Evans 		0,			/* STT_FILE */
108708278a5eSRod Evans 		1,			/* STT_COMMON */
108808278a5eSRod Evans 		0,			/* STT_TLS */
108908278a5eSRod Evans 		0,			/* 7 */
109008278a5eSRod Evans 		0,			/* 8 */
109108278a5eSRod Evans 		0,			/* 9 */
109208278a5eSRod Evans 		0,			/* 10 */
109308278a5eSRod Evans 		0,			/* 11 */
109408278a5eSRod Evans 		0,			/* 12 */
109508278a5eSRod Evans 		0,			/* STT_SPARC_REGISTER */
109608278a5eSRod Evans 		0,			/* 14 */
109708278a5eSRod Evans 		0,			/* 15 */
109808278a5eSRod Evans 	};
109908278a5eSRod Evans #if STT_NUM != (STT_TLS + 1)
110008278a5eSRod Evans #error "STT_NUM has grown. Update addr_symtype[]"
110108278a5eSRod Evans #endif
110208278a5eSRod Evans 
110308278a5eSRod Evans 	char		index[MAXNDXSIZE];
110408278a5eSRod Evans 	const char	*symname, *sec;
110508278a5eSRod Evans 	Versym		verndx;
110608278a5eSRod Evans 	int		gnuver;
110708278a5eSRod Evans 	uchar_t		type;
110808278a5eSRod Evans 	Shdr		*tshdr;
110908278a5eSRod Evans 	Word		shndx;
111008278a5eSRod Evans 	Conv_inv_buf_t	inv_buf;
111108278a5eSRod Evans 
111208278a5eSRod Evans 	/* Ensure symbol index is in range */
111308278a5eSRod Evans 	if (symndx >= state->symn) {
111408278a5eSRod Evans 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSYMNDX),
111508278a5eSRod Evans 		    state->file, state->secname, EC_WORD(symndx));
111608278a5eSRod Evans 		return;
111708278a5eSRod Evans 	}
111808278a5eSRod Evans 
111908278a5eSRod Evans 	/*
112008278a5eSRod Evans 	 * If we are using extended symbol indexes, find the
112108278a5eSRod Evans 	 * corresponding SHN_SYMTAB_SHNDX table.
112208278a5eSRod Evans 	 */
112308278a5eSRod Evans 	if ((sym->st_shndx == SHN_XINDEX) && (state->shxndx.checked == 0))
112408278a5eSRod Evans 		symbols_getxindex(state);
112508278a5eSRod Evans 
112608278a5eSRod Evans 	/* LINTED */
112708278a5eSRod Evans 	symname = string(state->seccache, symndx,
112808278a5eSRod Evans 	    &state->cache[state->seccache->c_shdr->sh_link], state->file,
112908278a5eSRod Evans 	    sym->st_name);
113008278a5eSRod Evans 
113108278a5eSRod Evans 	tshdr = NULL;
113208278a5eSRod Evans 	sec = NULL;
113308278a5eSRod Evans 
113408278a5eSRod Evans 	if (state->ehdr->e_type == ET_CORE) {
113508278a5eSRod Evans 		sec = (char *)MSG_INTL(MSG_STR_UNKNOWN);
113608278a5eSRod Evans 	} else if (state->flags & FLG_CTL_FAKESHDR) {
113708278a5eSRod Evans 		/*
113808278a5eSRod Evans 		 * If we are using fake section headers derived from
113908278a5eSRod Evans 		 * the program headers, then the section indexes
114008278a5eSRod Evans 		 * in the symbols do not correspond to these headers.
114108278a5eSRod Evans 		 * The section names are not available, so all we can
114208278a5eSRod Evans 		 * do is to display them in numeric form.
114308278a5eSRod Evans 		 */
114408278a5eSRod Evans 		sec = conv_sym_shndx(state->osabi, state->ehdr->e_machine,
114508278a5eSRod Evans 		    sym->st_shndx, CONV_FMT_DECIMAL, &inv_buf);
114608278a5eSRod Evans 	} else if ((sym->st_shndx < SHN_LORESERVE) &&
114708278a5eSRod Evans 	    (sym->st_shndx < state->shnum)) {
114808278a5eSRod Evans 		shndx = sym->st_shndx;
114908278a5eSRod Evans 		tshdr = state->cache[shndx].c_shdr;
115008278a5eSRod Evans 		sec = state->cache[shndx].c_name;
115108278a5eSRod Evans 	} else if (sym->st_shndx == SHN_XINDEX) {
115208278a5eSRod Evans 		if (state->shxndx.data) {
115308278a5eSRod Evans 			Word	_shxndx;
115408278a5eSRod Evans 
115508278a5eSRod Evans 			if (symndx > state->shxndx.n) {
115608278a5eSRod Evans 				(void) fprintf(stderr,
115708278a5eSRod Evans 				    MSG_INTL(MSG_ERR_BADSYMXINDEX1),
115808278a5eSRod Evans 				    state->file, state->secname,
115908278a5eSRod Evans 				    EC_WORD(symndx));
116008278a5eSRod Evans 			} else if ((_shxndx =
116108278a5eSRod Evans 			    state->shxndx.data[symndx]) > state->shnum) {
116208278a5eSRod Evans 				(void) fprintf(stderr,
116308278a5eSRod Evans 				    MSG_INTL(MSG_ERR_BADSYMXINDEX2),
116408278a5eSRod Evans 				    state->file, state->secname,
116508278a5eSRod Evans 				    EC_WORD(symndx), EC_WORD(_shxndx));
116608278a5eSRod Evans 			} else {
116708278a5eSRod Evans 				shndx = _shxndx;
116808278a5eSRod Evans 				tshdr = state->cache[shndx].c_shdr;
116908278a5eSRod Evans 				sec = state->cache[shndx].c_name;
117008278a5eSRod Evans 			}
117108278a5eSRod Evans 		} else {
117208278a5eSRod Evans 			(void) fprintf(stderr,
117308278a5eSRod Evans 			    MSG_INTL(MSG_ERR_BADSYMXINDEX3),
117408278a5eSRod Evans 			    state->file, state->secname, EC_WORD(symndx));
117508278a5eSRod Evans 		}
117608278a5eSRod Evans 	} else if ((sym->st_shndx < SHN_LORESERVE) &&
117708278a5eSRod Evans 	    (sym->st_shndx >= state->shnum)) {
117808278a5eSRod Evans 		(void) fprintf(stderr,
117908278a5eSRod Evans 		    MSG_INTL(MSG_ERR_BADSYM5), state->file,
118008278a5eSRod Evans 		    state->secname, EC_WORD(symndx),
118108278a5eSRod Evans 		    demangle(symname, state->flags), sym->st_shndx);
118208278a5eSRod Evans 	}
118308278a5eSRod Evans 
118408278a5eSRod Evans 	/*
118508278a5eSRod Evans 	 * If versioning is available display the
118608278a5eSRod Evans 	 * version index. If not, then use 0.
118708278a5eSRod Evans 	 */
118808278a5eSRod Evans 	if (state->versym) {
118908278a5eSRod Evans 		Versym test_verndx;
119008278a5eSRod Evans 
119108278a5eSRod Evans 		verndx = test_verndx = state->versym->data[symndx];
119208278a5eSRod Evans 		gnuver = state->versym->gnu_full;
119308278a5eSRod Evans 
119408278a5eSRod Evans 		/*
119508278a5eSRod Evans 		 * Check to see if this is a defined symbol with a
119608278a5eSRod Evans 		 * version index that is outside the valid range for
119708278a5eSRod Evans 		 * the file. The interpretation of this depends on
119808278a5eSRod Evans 		 * the style of versioning used by the object.
119908278a5eSRod Evans 		 *
120008278a5eSRod Evans 		 * Versions >= VER_NDX_LORESERVE have special meanings,
120108278a5eSRod Evans 		 * and are exempt from this checking.
120208278a5eSRod Evans 		 *
120308278a5eSRod Evans 		 * GNU style version indexes use the top bit of the
120408278a5eSRod Evans 		 * 16-bit index value (0x8000) as the "hidden bit".
120508278a5eSRod Evans 		 * We must mask off this bit in order to compare
120608278a5eSRod Evans 		 * the version against the maximum value.
120708278a5eSRod Evans 		 */
120808278a5eSRod Evans 		if (gnuver)
120908278a5eSRod Evans 			test_verndx &= ~0x8000;
121008278a5eSRod Evans 
121108278a5eSRod Evans 		if ((test_verndx > state->versym->max_verndx) &&
121208278a5eSRod Evans 		    (verndx < VER_NDX_LORESERVE))
121308278a5eSRod Evans 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADVER),
121408278a5eSRod Evans 			    state->file, state->secname, EC_WORD(symndx),
121508278a5eSRod Evans 			    EC_HALF(test_verndx), state->versym->max_verndx);
121608278a5eSRod Evans 	} else {
121708278a5eSRod Evans 		verndx = 0;
121808278a5eSRod Evans 		gnuver = 0;
121908278a5eSRod Evans 	}
122008278a5eSRod Evans 
122108278a5eSRod Evans 	/*
122208278a5eSRod Evans 	 * Error checking for TLS.
122308278a5eSRod Evans 	 */
122408278a5eSRod Evans 	type = ELF_ST_TYPE(sym->st_info);
122508278a5eSRod Evans 	if (type == STT_TLS) {
122608278a5eSRod Evans 		if (tshdr &&
122708278a5eSRod Evans 		    (sym->st_shndx != SHN_UNDEF) &&
122808278a5eSRod Evans 		    ((tshdr->sh_flags & SHF_TLS) == 0)) {
122908278a5eSRod Evans 			(void) fprintf(stderr,
123008278a5eSRod Evans 			    MSG_INTL(MSG_ERR_BADSYM3), state->file,
123108278a5eSRod Evans 			    state->secname, EC_WORD(symndx),
123208278a5eSRod Evans 			    demangle(symname, state->flags));
123308278a5eSRod Evans 		}
123408278a5eSRod Evans 	} else if ((type != STT_SECTION) && sym->st_size &&
123508278a5eSRod Evans 	    tshdr && (tshdr->sh_flags & SHF_TLS)) {
123608278a5eSRod Evans 		(void) fprintf(stderr,
123708278a5eSRod Evans 		    MSG_INTL(MSG_ERR_BADSYM4), state->file,
123808278a5eSRod Evans 		    state->secname, EC_WORD(symndx),
123908278a5eSRod Evans 		    demangle(symname, state->flags));
124008278a5eSRod Evans 	}
124108278a5eSRod Evans 
124208278a5eSRod Evans 	/*
124308278a5eSRod Evans 	 * If a symbol with non-zero size has a type that
124408278a5eSRod Evans 	 * specifies an address, then make sure the location
124508278a5eSRod Evans 	 * it references is actually contained within the
124608278a5eSRod Evans 	 * section.  UNDEF symbols don't count in this case,
124708278a5eSRod Evans 	 * so we ignore them.
124808278a5eSRod Evans 	 *
124908278a5eSRod Evans 	 * The meaning of the st_value field in a symbol
125008278a5eSRod Evans 	 * depends on the type of object. For a relocatable
125108278a5eSRod Evans 	 * object, it is the offset within the section.
125208278a5eSRod Evans 	 * For sharable objects, it is the offset relative to
125308278a5eSRod Evans 	 * the base of the object, and for other types, it is
125408278a5eSRod Evans 	 * the virtual address. To get an offset within the
125508278a5eSRod Evans 	 * section for non-ET_REL files, we subtract the
125608278a5eSRod Evans 	 * base address of the section.
125708278a5eSRod Evans 	 */
125808278a5eSRod Evans 	if (addr_symtype[type] && (sym->st_size > 0) &&
125908278a5eSRod Evans 	    (sym->st_shndx != SHN_UNDEF) && ((sym->st_shndx < SHN_LORESERVE) ||
126008278a5eSRod Evans 	    (sym->st_shndx == SHN_XINDEX)) && (tshdr != NULL)) {
126108278a5eSRod Evans 		Word v = sym->st_value;
126208278a5eSRod Evans 			if (state->ehdr->e_type != ET_REL)
126308278a5eSRod Evans 				v -= tshdr->sh_addr;
126408278a5eSRod Evans 		if (((v + sym->st_size) > tshdr->sh_size)) {
126508278a5eSRod Evans 			(void) fprintf(stderr,
126608278a5eSRod Evans 			    MSG_INTL(MSG_ERR_BADSYM6), state->file,
126708278a5eSRod Evans 			    state->secname, EC_WORD(symndx),
126808278a5eSRod Evans 			    demangle(symname, state->flags),
126908278a5eSRod Evans 			    EC_WORD(shndx), EC_XWORD(tshdr->sh_size),
127008278a5eSRod Evans 			    EC_XWORD(sym->st_value), EC_XWORD(sym->st_size));
127108278a5eSRod Evans 		}
127208278a5eSRod Evans 	}
127308278a5eSRod Evans 
127408278a5eSRod Evans 	/*
127508278a5eSRod Evans 	 * A typical symbol table uses the sh_info field to indicate one greater
127608278a5eSRod Evans 	 * than the symbol table index of the last local symbol, STB_LOCAL.
127708278a5eSRod Evans 	 * Therefore, symbol indexes less than sh_info should have local
127808278a5eSRod Evans 	 * binding.  Symbol indexes greater than, or equal to sh_info, should
127908278a5eSRod Evans 	 * have global binding.  Note, we exclude UNDEF/NOTY symbols with zero
128008278a5eSRod Evans 	 * value and size, as these symbols may be the result of an mcs(1)
128108278a5eSRod Evans 	 * section deletion.
128208278a5eSRod Evans 	 */
128308278a5eSRod Evans 	if (info) {
128408278a5eSRod Evans 		uchar_t	bind = ELF_ST_BIND(sym->st_info);
128508278a5eSRod Evans 
128608278a5eSRod Evans 		if ((symndx < info) && (bind != STB_LOCAL)) {
128708278a5eSRod Evans 			(void) fprintf(stderr,
128808278a5eSRod Evans 			    MSG_INTL(MSG_ERR_BADSYM7), state->file,
128908278a5eSRod Evans 			    state->secname, EC_WORD(symndx),
129008278a5eSRod Evans 			    demangle(symname, state->flags), EC_XWORD(info));
129108278a5eSRod Evans 
129208278a5eSRod Evans 		} else if ((symndx >= info) && (bind == STB_LOCAL) &&
129308278a5eSRod Evans 		    ((sym->st_shndx != SHN_UNDEF) ||
129408278a5eSRod Evans 		    (ELF_ST_TYPE(sym->st_info) != STT_NOTYPE) ||
129508278a5eSRod Evans 		    (sym->st_size != 0) || (sym->st_value != 0))) {
129608278a5eSRod Evans 			(void) fprintf(stderr,
129708278a5eSRod Evans 			    MSG_INTL(MSG_ERR_BADSYM8), state->file,
129808278a5eSRod Evans 			    state->secname, EC_WORD(symndx),
129908278a5eSRod Evans 			    demangle(symname, state->flags), EC_XWORD(info));
130008278a5eSRod Evans 		}
130108278a5eSRod Evans 	}
130208278a5eSRod Evans 
130308278a5eSRod Evans 	(void) snprintf(index, MAXNDXSIZE,
130408278a5eSRod Evans 	    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(disp_symndx));
130508278a5eSRod Evans 	Elf_syms_table_entry(0, ELF_DBG_ELFDUMP, index, state->osabi,
130608278a5eSRod Evans 	    state->ehdr->e_machine, sym, verndx, gnuver, sec, symname);
130708278a5eSRod Evans }
130808278a5eSRod Evans 
130908278a5eSRod Evans /*
131008278a5eSRod Evans  * Process a SHT_SUNW_cap capabilities section.
131108278a5eSRod Evans  */
131208278a5eSRod Evans static int
131308278a5eSRod Evans cap_section(const char *file, Cache *cache, Word shnum, Cache *ccache,
131408278a5eSRod Evans     uchar_t osabi, Ehdr *ehdr, uint_t flags)
131508278a5eSRod Evans {
131608278a5eSRod Evans 	SYMTBL_STATE	state;
131708278a5eSRod Evans 	Word		cnum, capnum, nulls, symcaps;
131808278a5eSRod Evans 	int		descapndx, objcap, title;
131908278a5eSRod Evans 	Cap		*cap = (Cap *)ccache->c_data->d_buf;
132008278a5eSRod Evans 	Shdr		*cishdr, *cshdr = ccache->c_shdr;
132108278a5eSRod Evans 	Cache		*cicache, *strcache;
132208278a5eSRod Evans 	Capinfo		*capinfo = NULL;
132308278a5eSRod Evans 	Word		capinfonum;
132408278a5eSRod Evans 	const char	*strs = NULL;
132508278a5eSRod Evans 	size_t		strs_size;
132608278a5eSRod Evans 
132708278a5eSRod Evans 	if ((cshdr->sh_entsize == 0) || (cshdr->sh_size == 0)) {
132808278a5eSRod Evans 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
132908278a5eSRod Evans 		    file, ccache->c_name);
133008278a5eSRod Evans 		return (0);
133108278a5eSRod Evans 	}
133208278a5eSRod Evans 
133308278a5eSRod Evans 	/*
133408278a5eSRod Evans 	 * If this capabilities section is associated with symbols, then the
133508278a5eSRod Evans 	 * sh_link field points to the associated capabilities information
133608278a5eSRod Evans 	 * section.  The sh_link field of the capabilities information section
133708278a5eSRod Evans 	 * points to the associated symbol table.
133808278a5eSRod Evans 	 */
133908278a5eSRod Evans 	if (cshdr->sh_link) {
134008278a5eSRod Evans 		Cache	*scache;
134108278a5eSRod Evans 		Shdr	*sshdr;
134208278a5eSRod Evans 
134308278a5eSRod Evans 		/*
134408278a5eSRod Evans 		 * Validate that the sh_link field points to a capabilities
134508278a5eSRod Evans 		 * information section.
134608278a5eSRod Evans 		 */
134708278a5eSRod Evans 		if (cshdr->sh_link >= shnum) {
134808278a5eSRod Evans 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
134908278a5eSRod Evans 			    file, ccache->c_name, EC_WORD(cshdr->sh_link));
135008278a5eSRod Evans 			return (0);
135108278a5eSRod Evans 		}
135208278a5eSRod Evans 
135308278a5eSRod Evans 		cicache = &cache[cshdr->sh_link];
135408278a5eSRod Evans 		cishdr = cicache->c_shdr;
135508278a5eSRod Evans 
135608278a5eSRod Evans 		if (cishdr->sh_type != SHT_SUNW_capinfo) {
135708278a5eSRod Evans 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_INVCAP),
135808278a5eSRod Evans 			    file, ccache->c_name, EC_WORD(cshdr->sh_link));
135908278a5eSRod Evans 			return (0);
136008278a5eSRod Evans 		}
136108278a5eSRod Evans 
136208278a5eSRod Evans 		capinfo = cicache->c_data->d_buf;
136308278a5eSRod Evans 		capinfonum = (Word)(cishdr->sh_size / cishdr->sh_entsize);
136408278a5eSRod Evans 
136508278a5eSRod Evans 		/*
136608278a5eSRod Evans 		 * Validate that the sh_link field of the capabilities
136708278a5eSRod Evans 		 * information section points to a valid symbol table.
136808278a5eSRod Evans 		 */
136908278a5eSRod Evans 		if ((cishdr->sh_link == 0) || (cishdr->sh_link >= shnum)) {
137008278a5eSRod Evans 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
137108278a5eSRod Evans 			    file, cicache->c_name, EC_WORD(cishdr->sh_link));
137208278a5eSRod Evans 			return (0);
137308278a5eSRod Evans 		}
137408278a5eSRod Evans 		scache = &cache[cishdr->sh_link];
137508278a5eSRod Evans 		sshdr = scache->c_shdr;
137608278a5eSRod Evans 
137708278a5eSRod Evans 		if ((sshdr->sh_type != SHT_SYMTAB) &&
137808278a5eSRod Evans 		    (sshdr->sh_type != SHT_DYNSYM)) {
137908278a5eSRod Evans 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_INVCAPINFO1),
138008278a5eSRod Evans 			    file, cicache->c_name, EC_WORD(cishdr->sh_link));
138108278a5eSRod Evans 			return (0);
138208278a5eSRod Evans 		}
138308278a5eSRod Evans 
138408278a5eSRod Evans 		if (!init_symtbl_state(&state, cache, shnum,
138508278a5eSRod Evans 		    cishdr->sh_link, ehdr, osabi, NULL, file, flags))
138608278a5eSRod Evans 			return (0);
138708278a5eSRod Evans 	}
138808278a5eSRod Evans 
138908278a5eSRod Evans 	/*
139008278a5eSRod Evans 	 * If this capabilities section contains capability string entries,
139108278a5eSRod Evans 	 * then determine the associated string table.  Capabilities entries
139208278a5eSRod Evans 	 * that define names require that the capability section indicate
139308278a5eSRod Evans 	 * which string table to use via sh_info.
139408278a5eSRod Evans 	 */
139508278a5eSRod Evans 	if (cshdr->sh_info) {
139608278a5eSRod Evans 		Shdr	*strshdr;
139708278a5eSRod Evans 
139808278a5eSRod Evans 		/*
139908278a5eSRod Evans 		 * Validate that the sh_info field points to a string table.
140008278a5eSRod Evans 		 */
140108278a5eSRod Evans 		if (cshdr->sh_info >= shnum) {
140208278a5eSRod Evans 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
140308278a5eSRod Evans 			    file, ccache->c_name, EC_WORD(cshdr->sh_info));
140408278a5eSRod Evans 			return (0);
140508278a5eSRod Evans 		}
140608278a5eSRod Evans 
140708278a5eSRod Evans 		strcache = &cache[cshdr->sh_info];
140808278a5eSRod Evans 		strshdr = strcache->c_shdr;
140908278a5eSRod Evans 
141008278a5eSRod Evans 		if (strshdr->sh_type != SHT_STRTAB) {
141108278a5eSRod Evans 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_INVCAP),
141208278a5eSRod Evans 			    file, ccache->c_name, EC_WORD(cshdr->sh_info));
141308278a5eSRod Evans 			return (0);
141408278a5eSRod Evans 		}
141508278a5eSRod Evans 		strs = (const char *)strcache->c_data->d_buf;
141608278a5eSRod Evans 		strs_size = strcache->c_data->d_size;
141708278a5eSRod Evans 	}
141808278a5eSRod Evans 
141908278a5eSRod Evans 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
142008278a5eSRod Evans 	dbg_print(0, MSG_INTL(MSG_ELF_SCN_CAP), ccache->c_name);
142108278a5eSRod Evans 
142208278a5eSRod Evans 	capnum = (Word)(cshdr->sh_size / cshdr->sh_entsize);
142308278a5eSRod Evans 
142408278a5eSRod Evans 	nulls = symcaps = 0;
142508278a5eSRod Evans 	objcap = title = 1;
142608278a5eSRod Evans 	descapndx = -1;
142708278a5eSRod Evans 
142808278a5eSRod Evans 	/*
142908278a5eSRod Evans 	 * Traverse the capabilities section printing each capability group.
143008278a5eSRod Evans 	 * The first capabilities group defines any object capabilities.  Any
143108278a5eSRod Evans 	 * following groups define symbol capabilities.  In the case where no
143208278a5eSRod Evans 	 * object capabilities exist, but symbol capabilities do, a single
143308278a5eSRod Evans 	 * CA_SUNW_NULL terminator for the object capabilities exists.
143408278a5eSRod Evans 	 */
143508278a5eSRod Evans 	for (cnum = 0; cnum < capnum; cap++, cnum++) {
143608278a5eSRod Evans 		if (cap->c_tag == CA_SUNW_NULL) {
143708278a5eSRod Evans 			/*
143808278a5eSRod Evans 			 * A CA_SUNW_NULL tag terminates a capabilities group.
143908278a5eSRod Evans 			 * If the first capabilities tag is CA_SUNW_NULL, then
144008278a5eSRod Evans 			 * no object capabilities exist.
144108278a5eSRod Evans 			 */
144208278a5eSRod Evans 			if ((nulls++ == 0) && (cnum == 0))
144308278a5eSRod Evans 				objcap = 0;
144408278a5eSRod Evans 			title = 1;
144508278a5eSRod Evans 		} else {
144608278a5eSRod Evans 			if (title) {
144708278a5eSRod Evans 				if (nulls == 0) {
144808278a5eSRod Evans 					/*
144908278a5eSRod Evans 					 * If this capabilities group represents
145008278a5eSRod Evans 					 * the object capabilities (i.e., no
145108278a5eSRod Evans 					 * CA_SUNW_NULL tag has been processed
145208278a5eSRod Evans 					 * yet), then display an object
145308278a5eSRod Evans 					 * capabilities title.
145408278a5eSRod Evans 					 */
145508278a5eSRod Evans 					dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
145608278a5eSRod Evans 					dbg_print(0,
145708278a5eSRod Evans 					    MSG_INTL(MSG_OBJ_CAP_TITLE));
145808278a5eSRod Evans 				} else {
145908278a5eSRod Evans 					/*
146008278a5eSRod Evans 					 * If this is a symbols capabilities
146108278a5eSRod Evans 					 * group (i.e., a CA_SUNW_NULL tag has
146208278a5eSRod Evans 					 * already be found that terminates
146308278a5eSRod Evans 					 * the object capabilities group), then
146408278a5eSRod Evans 					 * display a symbol capabilities title,
146508278a5eSRod Evans 					 * and retain this capabilities index
146608278a5eSRod Evans 					 * for later processing.
146708278a5eSRod Evans 					 */
146808278a5eSRod Evans 					dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
146908278a5eSRod Evans 					dbg_print(0,
147008278a5eSRod Evans 					    MSG_INTL(MSG_SYM_CAP_TITLE));
147108278a5eSRod Evans 					descapndx = cnum;
147208278a5eSRod Evans 				}
147308278a5eSRod Evans 				Elf_cap_title(0);
147408278a5eSRod Evans 				title = 0;
147508278a5eSRod Evans 			}
147608278a5eSRod Evans 
147708278a5eSRod Evans 			/*
147808278a5eSRod Evans 			 * Print the capabilities data.
147908278a5eSRod Evans 			 *
148008278a5eSRod Evans 			 * Note that CA_SUNW_PLAT, CA_SUNW_MACH and CA_SUNW_ID
148108278a5eSRod Evans 			 * entries require a string table, which should have
148208278a5eSRod Evans 			 * already been established.
148308278a5eSRod Evans 			 */
148408278a5eSRod Evans 			if ((strs == NULL) && ((cap->c_tag == CA_SUNW_PLAT) ||
148508278a5eSRod Evans 			    (cap->c_tag == CA_SUNW_MACH) ||
148608278a5eSRod Evans 			    (cap->c_tag == CA_SUNW_ID))) {
148708278a5eSRod Evans 				(void) fprintf(stderr,
148808278a5eSRod Evans 				    MSG_INTL(MSG_WARN_INVCAP4), file,
148908278a5eSRod Evans 				    EC_WORD(elf_ndxscn(ccache->c_scn)),
149008278a5eSRod Evans 				    ccache->c_name, EC_WORD(cshdr->sh_info));
149108278a5eSRod Evans 			}
149208278a5eSRod Evans 			Elf_cap_entry(0, cap, cnum, strs, strs_size,
149308278a5eSRod Evans 			    ehdr->e_machine);
149408278a5eSRod Evans 		}
149508278a5eSRod Evans 
149608278a5eSRod Evans 		/*
149708278a5eSRod Evans 		 * If this CA_SUNW_NULL tag terminates a symbol capabilities
149808278a5eSRod Evans 		 * group, determine the associated symbols.
149908278a5eSRod Evans 		 */
150008278a5eSRod Evans 		if ((cap->c_tag == CA_SUNW_NULL) && (nulls > 1) &&
150108278a5eSRod Evans 		    (descapndx != -1)) {
150208278a5eSRod Evans 			Capinfo	*cip;
150308278a5eSRod Evans 			Word	inum;
150408278a5eSRod Evans 
150508278a5eSRod Evans 			symcaps++;
150608278a5eSRod Evans 
150708278a5eSRod Evans 			/*
150808278a5eSRod Evans 			 * Make sure we've discovered a SHT_SUNW_capinfo table.
150908278a5eSRod Evans 			 */
151008278a5eSRod Evans 			if ((cip = capinfo) == NULL) {
151108278a5eSRod Evans 				(void) fprintf(stderr,
151208278a5eSRod Evans 				    MSG_INTL(MSG_ERR_INVCAP), file,
151308278a5eSRod Evans 				    ccache->c_name, EC_WORD(cshdr->sh_link));
151408278a5eSRod Evans 				return (0);
151508278a5eSRod Evans 			}
151608278a5eSRod Evans 
151708278a5eSRod Evans 			/*
151808278a5eSRod Evans 			 * Determine what symbols reference this capabilities
151908278a5eSRod Evans 			 * group.
152008278a5eSRod Evans 			 */
152108278a5eSRod Evans 			dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
152208278a5eSRod Evans 			dbg_print(0, MSG_INTL(MSG_CAPINFO_ENTRIES));
152308278a5eSRod Evans 			Elf_syms_table_title(0, ELF_DBG_ELFDUMP);
152408278a5eSRod Evans 
152508278a5eSRod Evans 			for (inum = 1, cip++; inum < capinfonum;
152608278a5eSRod Evans 			    inum++, cip++) {
152708278a5eSRod Evans 				Word	gndx = (Word)ELF_C_GROUP(*cip);
152808278a5eSRod Evans 
152908278a5eSRod Evans 				if (gndx && (gndx == descapndx)) {
153008278a5eSRod Evans 					output_symbol(&state, inum, 0,
153108278a5eSRod Evans 					    inum, state.sym + inum);
153208278a5eSRod Evans 				}
153308278a5eSRod Evans 			}
153408278a5eSRod Evans 			descapndx = -1;
153508278a5eSRod Evans 			continue;
153608278a5eSRod Evans 		}
153708278a5eSRod Evans 
153808278a5eSRod Evans 		/*
153908278a5eSRod Evans 		 * An SF1_SUNW_ADDR32 software capability tag in a 32-bit
154008278a5eSRod Evans 		 * object is suspicious as it has no effect.
154108278a5eSRod Evans 		 */
154208278a5eSRod Evans 		if ((cap->c_tag == CA_SUNW_SF_1) &&
154308278a5eSRod Evans 		    (ehdr->e_ident[EI_CLASS] == ELFCLASS32) &&
154408278a5eSRod Evans 		    (cap->c_un.c_val & SF1_SUNW_ADDR32)) {
154508278a5eSRod Evans 			(void) fprintf(stderr, MSG_INTL(MSG_WARN_INADDR32SF1),
154608278a5eSRod Evans 			    file, ccache->c_name);
154708278a5eSRod Evans 		}
154808278a5eSRod Evans 	}
154908278a5eSRod Evans 
155008278a5eSRod Evans 	/*
155108278a5eSRod Evans 	 * If this is a dynamic object, with symbol capabilities, then a
155208278a5eSRod Evans 	 * .SUNW_capchain section should exist.  This section contains a chain
155308278a5eSRod Evans 	 * of symbol indexes for each capabilities family.  This is the list
155408278a5eSRod Evans 	 * that is searched by ld.so.1 to determine the best capabilities
155508278a5eSRod Evans 	 * candidate.
155608278a5eSRod Evans 	 *
155708278a5eSRod Evans 	 * Note, more than one capabilities lead symbol can point to the same
155808278a5eSRod Evans 	 * family chain.  For example, a weak/global pair of symbols can both
155908278a5eSRod Evans 	 * represent the same family of capabilities symbols.  Therefore, to
156008278a5eSRod Evans 	 * display all possible families we traverse the capabilities
156108278a5eSRod Evans 	 * information section looking for CAPINFO_SUNW_GLOB lead symbols.
156208278a5eSRod Evans 	 * From these we determine the associated capabilities chain to inspect.
156308278a5eSRod Evans 	 */
156408278a5eSRod Evans 	if (symcaps &&
156508278a5eSRod Evans 	    ((ehdr->e_type == ET_EXEC) || (ehdr->e_type == ET_DYN))) {
156608278a5eSRod Evans 		Capinfo		*cip;
156708278a5eSRod Evans 		Capchain	*chain;
156808278a5eSRod Evans 		Cache   	*chcache;
156908278a5eSRod Evans 		Shdr		*chshdr;
157008278a5eSRod Evans 		Word		chainnum, inum;
157108278a5eSRod Evans 
157208278a5eSRod Evans 		/*
157308278a5eSRod Evans 		 * Validate that the sh_info field of the capabilities
157408278a5eSRod Evans 		 * information section points to a capabilities chain section.
157508278a5eSRod Evans 		 */
157608278a5eSRod Evans 		if (cishdr->sh_info >= shnum) {
157708278a5eSRod Evans 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
157808278a5eSRod Evans 			    file, cicache->c_name, EC_WORD(cishdr->sh_info));
157908278a5eSRod Evans 			return (0);
158008278a5eSRod Evans 		}
158108278a5eSRod Evans 
158208278a5eSRod Evans 		chcache = &cache[cishdr->sh_info];
158308278a5eSRod Evans 		chshdr = chcache->c_shdr;
158408278a5eSRod Evans 
158508278a5eSRod Evans 		if (chshdr->sh_type != SHT_SUNW_capchain) {
158608278a5eSRod Evans 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_INVCAPINFO2),
158708278a5eSRod Evans 			    file, cicache->c_name, EC_WORD(cishdr->sh_info));
158808278a5eSRod Evans 			return (0);
158908278a5eSRod Evans 		}
159008278a5eSRod Evans 
159108278a5eSRod Evans 		chainnum = (Word)(chshdr->sh_size / chshdr->sh_entsize);
159208278a5eSRod Evans 		chain = (Capchain *)chcache->c_data->d_buf;
159308278a5eSRod Evans 
159408278a5eSRod Evans 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
159508278a5eSRod Evans 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_CAPCHAIN), chcache->c_name);
159608278a5eSRod Evans 
159708278a5eSRod Evans 		/*
159808278a5eSRod Evans 		 * Traverse the capabilities information section looking for
159908278a5eSRod Evans 		 * CAPINFO_SUNW_GLOB lead capabilities symbols.
160008278a5eSRod Evans 		 */
160108278a5eSRod Evans 		cip = capinfo;
160208278a5eSRod Evans 		for (inum = 1, cip++; inum < capinfonum; inum++, cip++) {
160308278a5eSRod Evans 			const char	*name;
160408278a5eSRod Evans 			Sym		*sym;
160508278a5eSRod Evans 			Word		sndx, cndx;
160608278a5eSRod Evans 			Word		gndx = (Word)ELF_C_GROUP(*cip);
160708278a5eSRod Evans 
160808278a5eSRod Evans 			if ((gndx == 0) || (gndx != CAPINFO_SUNW_GLOB))
160908278a5eSRod Evans 				continue;
161008278a5eSRod Evans 
161108278a5eSRod Evans 			/*
161208278a5eSRod Evans 			 * Determine the symbol that is associated with this
161308278a5eSRod Evans 			 * capability information entry, and use this to
161408278a5eSRod Evans 			 * identify this capability family.
161508278a5eSRod Evans 			 */
161608278a5eSRod Evans 			sym = (Sym *)(state.sym + inum);
161708278a5eSRod Evans 			name = string(cicache, inum, strcache, file,
161808278a5eSRod Evans 			    sym->st_name);
161908278a5eSRod Evans 
162008278a5eSRod Evans 			dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
162108278a5eSRod Evans 			dbg_print(0, MSG_INTL(MSG_CAPCHAIN_TITLE), name);
162208278a5eSRod Evans 			dbg_print(0, MSG_INTL(MSG_CAPCHAIN_ENTRY));
162308278a5eSRod Evans 
162408278a5eSRod Evans 			cndx = (Word)ELF_C_SYM(*cip);
162508278a5eSRod Evans 
162608278a5eSRod Evans 			/*
162708278a5eSRod Evans 			 * Traverse this families chain and identify each
162808278a5eSRod Evans 			 * family member.
162908278a5eSRod Evans 			 */
163008278a5eSRod Evans 			for (;;) {
163108278a5eSRod Evans 				char	_chain[MAXNDXSIZE], _symndx[MAXNDXSIZE];
163208278a5eSRod Evans 
163308278a5eSRod Evans 				if (cndx >= chainnum) {
163408278a5eSRod Evans 					(void) fprintf(stderr,
163508278a5eSRod Evans 					    MSG_INTL(MSG_ERR_INVCAPINFO3), file,
163608278a5eSRod Evans 					    cicache->c_name, EC_WORD(inum),
163708278a5eSRod Evans 					    EC_WORD(cndx));
163808278a5eSRod Evans 					break;
163908278a5eSRod Evans 				}
164008278a5eSRod Evans 				if ((sndx = chain[cndx]) == 0)
164108278a5eSRod Evans 					break;
164208278a5eSRod Evans 
164308278a5eSRod Evans 				/*
164408278a5eSRod Evans 				 * Determine this entries symbol reference.
164508278a5eSRod Evans 				 */
164608278a5eSRod Evans 				if (sndx > state.symn) {
164708278a5eSRod Evans 					(void) fprintf(stderr,
164808278a5eSRod Evans 					    MSG_INTL(MSG_ERR_CHBADSYMNDX), file,
164908278a5eSRod Evans 					    EC_WORD(sndx), chcache->c_name,
165008278a5eSRod Evans 					    EC_WORD(cndx));
165108278a5eSRod Evans 					name = MSG_INTL(MSG_STR_UNKNOWN);
165208278a5eSRod Evans 				} else {
165308278a5eSRod Evans 					sym = (Sym *)(state.sym + sndx);
165408278a5eSRod Evans 					name = string(chcache, sndx,
165508278a5eSRod Evans 					    strcache, file, sym->st_name);
165608278a5eSRod Evans 				}
165708278a5eSRod Evans 
165808278a5eSRod Evans 				/*
165908278a5eSRod Evans 				 * Display the family member.
166008278a5eSRod Evans 				 */
166108278a5eSRod Evans 				(void) snprintf(_chain, MAXNDXSIZE,
166208278a5eSRod Evans 				    MSG_ORIG(MSG_FMT_INTEGER), cndx);
166308278a5eSRod Evans 				(void) snprintf(_symndx, MAXNDXSIZE,
166408278a5eSRod Evans 				    MSG_ORIG(MSG_FMT_INDEX2), EC_WORD(sndx));
166508278a5eSRod Evans 				dbg_print(0, MSG_ORIG(MSG_FMT_CHAIN_INFO),
166608278a5eSRod Evans 				    _chain, _symndx, demangle(name, flags));
166708278a5eSRod Evans 
166808278a5eSRod Evans 				cndx++;
166908278a5eSRod Evans 			}
167008278a5eSRod Evans 		}
167108278a5eSRod Evans 	}
167208278a5eSRod Evans 	return (objcap);
167308278a5eSRod Evans }
167408278a5eSRod Evans 
167508278a5eSRod Evans /*
167608278a5eSRod Evans  * Print the capabilities.
167708278a5eSRod Evans  *
167808278a5eSRod Evans  * A .SUNW_cap section can contain one or more, CA_SUNW_NULL terminated,
167908278a5eSRod Evans  * capabilities groups.  The first group defines the object capabilities.
168008278a5eSRod Evans  * This group defines the minimum capability requirements of the entire
168108278a5eSRod Evans  * object file.  If this is a dynamic object, this group should be associated
168208278a5eSRod Evans  * with a PT_SUNWCAP program header.
168308278a5eSRod Evans  *
168408278a5eSRod Evans  * Additional capabilities groups define the association of individual symbols
168508278a5eSRod Evans  * to specific capabilities.
16867c478bd9Sstevel@tonic-gate  */
16877c478bd9Sstevel@tonic-gate static void
16885aefb655Srie cap(const char *file, Cache *cache, Word shnum, Word phnum, Ehdr *ehdr,
168908278a5eSRod Evans     uchar_t osabi, Elf *elf, uint_t flags)
16907c478bd9Sstevel@tonic-gate {
16915aefb655Srie 	Word		cnt;
1692bebb829dSRod Evans 	Shdr		*cshdr = NULL;
16937c478bd9Sstevel@tonic-gate 	Cache		*ccache;
16945aefb655Srie 	Off		cphdr_off = 0;
16955aefb655Srie 	Xword		cphdr_sz;
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 	/*
169808278a5eSRod Evans 	 * Determine if a global capabilities header exists.
16997c478bd9Sstevel@tonic-gate 	 */
17005aefb655Srie 	if (phnum) {
17015aefb655Srie 		Phdr	*phdr;
17027c478bd9Sstevel@tonic-gate 
17035aefb655Srie 		if ((phdr = elf_getphdr(elf)) == NULL) {
17047c478bd9Sstevel@tonic-gate 			failure(file, MSG_ORIG(MSG_ELF_GETPHDR));
17057c478bd9Sstevel@tonic-gate 			return;
17067c478bd9Sstevel@tonic-gate 		}
17077c478bd9Sstevel@tonic-gate 
17085aefb655Srie 		for (cnt = 0; cnt < phnum; phdr++, cnt++) {
17095aefb655Srie 			if (phdr->p_type == PT_SUNWCAP) {
17105aefb655Srie 				cphdr_off = phdr->p_offset;
17115aefb655Srie 				cphdr_sz = phdr->p_filesz;
17127c478bd9Sstevel@tonic-gate 				break;
17137c478bd9Sstevel@tonic-gate 			}
17147c478bd9Sstevel@tonic-gate 		}
17155aefb655Srie 	}
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate 	/*
171808278a5eSRod Evans 	 * Determine if a capabilities section exists.
17197c478bd9Sstevel@tonic-gate 	 */
17207c478bd9Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
17215aefb655Srie 		Cache	*_cache = &cache[cnt];
17225aefb655Srie 		Shdr	*shdr = _cache->c_shdr;
17237c478bd9Sstevel@tonic-gate 
172408278a5eSRod Evans 		/*
172508278a5eSRod Evans 		 * Process any capabilities information.
172608278a5eSRod Evans 		 */
172708278a5eSRod Evans 		if (shdr->sh_type == SHT_SUNW_cap) {
172808278a5eSRod Evans 			if (cap_section(file, cache, shnum, _cache, osabi,
172908278a5eSRod Evans 			    ehdr, flags)) {
173008278a5eSRod Evans 				/*
173108278a5eSRod Evans 				 * If this section defined an object capability
173208278a5eSRod Evans 				 * group, retain the section information for
173308278a5eSRod Evans 				 * program header validation.
173408278a5eSRod Evans 				 */
17357c478bd9Sstevel@tonic-gate 				ccache = _cache;
17367c478bd9Sstevel@tonic-gate 				cshdr = shdr;
173708278a5eSRod Evans 			}
173808278a5eSRod Evans 			continue;
173908278a5eSRod Evans 		}
17407c478bd9Sstevel@tonic-gate 	}
17417c478bd9Sstevel@tonic-gate 
1742bebb829dSRod Evans 	if ((cshdr == NULL) && (cphdr_off == 0))
17437c478bd9Sstevel@tonic-gate 		return;
17447c478bd9Sstevel@tonic-gate 
174508278a5eSRod Evans 	if (cphdr_off && (cshdr == NULL))
17467c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_WARN_INVCAP1), file);
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 	/*
174908278a5eSRod Evans 	 * If this object is an executable or shared object, and it provided
175008278a5eSRod Evans 	 * an object capabilities group, then the group should have an
175108278a5eSRod Evans 	 * accompanying PT_SUNWCAP program header.
17527c478bd9Sstevel@tonic-gate 	 */
17537c478bd9Sstevel@tonic-gate 	if (cshdr && ((ehdr->e_type == ET_EXEC) || (ehdr->e_type == ET_DYN))) {
175408278a5eSRod Evans 		if (cphdr_off == 0) {
17557c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_WARN_INVCAP2),
175608278a5eSRod Evans 			    file, EC_WORD(elf_ndxscn(ccache->c_scn)),
175708278a5eSRod Evans 			    ccache->c_name);
175808278a5eSRod Evans 		} else if ((cphdr_off != cshdr->sh_offset) ||
175908278a5eSRod Evans 		    (cphdr_sz != cshdr->sh_size)) {
17607c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_WARN_INVCAP3),
176108278a5eSRod Evans 			    file, EC_WORD(elf_ndxscn(ccache->c_scn)),
176208278a5eSRod Evans 			    ccache->c_name);
176308278a5eSRod Evans 		}
17647c478bd9Sstevel@tonic-gate 	}
17657c478bd9Sstevel@tonic-gate }
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate /*
17687c478bd9Sstevel@tonic-gate  * Print the interpretor.
17697c478bd9Sstevel@tonic-gate  */
17707c478bd9Sstevel@tonic-gate static void
17715aefb655Srie interp(const char *file, Cache *cache, Word shnum, Word phnum, Elf *elf)
17727c478bd9Sstevel@tonic-gate {
17737e16fca0SAli Bahrami 	static Word phdr_types[] = { PT_INTERP };
17747e16fca0SAli Bahrami 
17757e16fca0SAli Bahrami 
17765aefb655Srie 	Word	cnt;
17777e16fca0SAli Bahrami 	Shdr	*ishdr = NULL;
17787c478bd9Sstevel@tonic-gate 	Cache	*icache;
17795aefb655Srie 	Off	iphdr_off = 0;
17805aefb655Srie 	Xword	iphdr_fsz;
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate 	/*
17837c478bd9Sstevel@tonic-gate 	 * Determine if an interp header exists.
17847c478bd9Sstevel@tonic-gate 	 */
17855aefb655Srie 	if (phnum) {
17865aefb655Srie 		Phdr	*phdr;
17877c478bd9Sstevel@tonic-gate 
17887e16fca0SAli Bahrami 		phdr = getphdr(phnum, phdr_types,
17897e16fca0SAli Bahrami 		    sizeof (phdr_types) / sizeof (*phdr_types), file, elf);
17907e16fca0SAli Bahrami 		if (phdr != NULL) {
17915aefb655Srie 			iphdr_off = phdr->p_offset;
17925aefb655Srie 			iphdr_fsz = phdr->p_filesz;
17937c478bd9Sstevel@tonic-gate 		}
17947c478bd9Sstevel@tonic-gate 	}
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate 	if (iphdr_off == 0)
17977c478bd9Sstevel@tonic-gate 		return;
17987c478bd9Sstevel@tonic-gate 
17997c478bd9Sstevel@tonic-gate 	/*
18007c478bd9Sstevel@tonic-gate 	 * Determine if an interp section exists.
18017c478bd9Sstevel@tonic-gate 	 */
18027c478bd9Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
18035aefb655Srie 		Cache	*_cache = &cache[cnt];
18045aefb655Srie 		Shdr	*shdr = _cache->c_shdr;
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate 		/*
18077c478bd9Sstevel@tonic-gate 		 * Scan sections to find a section which contains the PT_INTERP
18087c478bd9Sstevel@tonic-gate 		 * string.  The target section can't be in a NOBITS section.
18097c478bd9Sstevel@tonic-gate 		 */
18107c478bd9Sstevel@tonic-gate 		if ((shdr->sh_type == SHT_NOBITS) ||
18117c478bd9Sstevel@tonic-gate 		    (iphdr_off < shdr->sh_offset) ||
18125aefb655Srie 		    (iphdr_off + iphdr_fsz) > (shdr->sh_offset + shdr->sh_size))
18137c478bd9Sstevel@tonic-gate 			continue;
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate 		icache = _cache;
18167c478bd9Sstevel@tonic-gate 		ishdr = shdr;
18177c478bd9Sstevel@tonic-gate 		break;
18187c478bd9Sstevel@tonic-gate 	}
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate 	/*
18217c478bd9Sstevel@tonic-gate 	 * Print the interpreter string based on the offset defined in the
18227c478bd9Sstevel@tonic-gate 	 * program header, as this is the offset used by the kernel.
18237c478bd9Sstevel@tonic-gate 	 */
18249a411307Srie 	if (ishdr && icache->c_data) {
18255aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
18265aefb655Srie 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_INTERP), icache->c_name);
18275aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_FMT_INDENT),
18287c478bd9Sstevel@tonic-gate 		    (char *)icache->c_data->d_buf +
18297c478bd9Sstevel@tonic-gate 		    (iphdr_off - ishdr->sh_offset));
18307c478bd9Sstevel@tonic-gate 	} else
18317c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_WARN_INVINTERP1), file);
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate 	/*
18347c478bd9Sstevel@tonic-gate 	 * If there are any inconsistences between the program header and
18357c478bd9Sstevel@tonic-gate 	 * section information, flag them.
18367c478bd9Sstevel@tonic-gate 	 */
18377c478bd9Sstevel@tonic-gate 	if (ishdr && ((iphdr_off != ishdr->sh_offset) ||
18385aefb655Srie 	    (iphdr_fsz != ishdr->sh_size))) {
18397c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_WARN_INVINTERP2), file,
18407c478bd9Sstevel@tonic-gate 		    icache->c_name);
18417c478bd9Sstevel@tonic-gate 	}
18427c478bd9Sstevel@tonic-gate }
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate /*
18457c478bd9Sstevel@tonic-gate  * Print the syminfo section.
18467c478bd9Sstevel@tonic-gate  */
18477c478bd9Sstevel@tonic-gate static void
1848d444b03eSAli Bahrami syminfo(Cache *cache, Word shnum, Ehdr *ehdr, uchar_t osabi, const char *file)
18497c478bd9Sstevel@tonic-gate {
18505aefb655Srie 	Shdr		*infoshdr;
18515aefb655Srie 	Syminfo		*info;
18525aefb655Srie 	Sym		*syms;
18535aefb655Srie 	Dyn		*dyns;
1854d444b03eSAli Bahrami 	Word		infonum, cnt, ndx, symnum, dynnum;
1855d444b03eSAli Bahrami 	Cache		*infocache = NULL, *dyncache = NULL, *symsec, *strsec;
1856d444b03eSAli Bahrami 	Boolean		*dynerr;
18577c478bd9Sstevel@tonic-gate 
18587c478bd9Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
18595aefb655Srie 		if (cache[cnt].c_shdr->sh_type == SHT_SUNW_syminfo) {
18605aefb655Srie 			infocache = &cache[cnt];
18617c478bd9Sstevel@tonic-gate 			break;
18627c478bd9Sstevel@tonic-gate 		}
18637c478bd9Sstevel@tonic-gate 	}
18647e16fca0SAli Bahrami 	if (infocache == NULL)
18657c478bd9Sstevel@tonic-gate 		return;
18667c478bd9Sstevel@tonic-gate 
18675aefb655Srie 	infoshdr = infocache->c_shdr;
18685aefb655Srie 	if ((infoshdr->sh_entsize == 0) || (infoshdr->sh_size == 0)) {
18697c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
18705aefb655Srie 		    file, infocache->c_name);
18717c478bd9Sstevel@tonic-gate 		return;
18727c478bd9Sstevel@tonic-gate 	}
18739a411307Srie 	if (infocache->c_data == NULL)
18749a411307Srie 		return;
18759a411307Srie 
18765aefb655Srie 	infonum = (Word)(infoshdr->sh_size / infoshdr->sh_entsize);
18775aefb655Srie 	info = (Syminfo *)infocache->c_data->d_buf;
18787c478bd9Sstevel@tonic-gate 
18797c478bd9Sstevel@tonic-gate 	/*
1880d444b03eSAli Bahrami 	 * If there is no associated dynamic section, determine if one
1881d444b03eSAli Bahrami 	 * is needed, and if so issue a warning. If there is an
1882d444b03eSAli Bahrami 	 * associated dynamic section, validate it and get the data buffer
1883d444b03eSAli Bahrami 	 * for it.
18847c478bd9Sstevel@tonic-gate 	 */
1885d444b03eSAli Bahrami 	dyns = NULL;
1886d444b03eSAli Bahrami 	dynnum = 0;
1887d444b03eSAli Bahrami 	if (infoshdr->sh_info == 0) {
1888d444b03eSAli Bahrami 		Syminfo	*_info = info + 1;
1889d444b03eSAli Bahrami 
1890d444b03eSAli Bahrami 		for (ndx = 1; ndx < infonum; ndx++, _info++) {
1891d444b03eSAli Bahrami 			if ((_info->si_flags == 0) && (_info->si_boundto == 0))
1892d444b03eSAli Bahrami 				continue;
1893d444b03eSAli Bahrami 
1894d444b03eSAli Bahrami 			if (_info->si_boundto < SYMINFO_BT_LOWRESERVE)
1895d444b03eSAli Bahrami 				(void) fprintf(stderr,
1896d444b03eSAli Bahrami 				    MSG_INTL(MSG_ERR_BADSHINFO), file,
1897d444b03eSAli Bahrami 				    infocache->c_name,
1898d444b03eSAli Bahrami 				    EC_WORD(infoshdr->sh_info));
1899d444b03eSAli Bahrami 		}
1900d444b03eSAli Bahrami 	} else if ((infoshdr->sh_info >= shnum) ||
1901d444b03eSAli Bahrami 	    (cache[infoshdr->sh_info].c_shdr->sh_type != SHT_DYNAMIC)) {
19027c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHINFO),
19035aefb655Srie 		    file, infocache->c_name, EC_WORD(infoshdr->sh_info));
1904d444b03eSAli Bahrami 	} else {
1905d444b03eSAli Bahrami 		dyncache = &cache[infoshdr->sh_info];
1906d444b03eSAli Bahrami 		if ((dyncache->c_data == NULL) ||
1907d444b03eSAli Bahrami 		    ((dyns = dyncache->c_data->d_buf) == NULL)) {
1908d444b03eSAli Bahrami 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
1909d444b03eSAli Bahrami 			    file, dyncache->c_name);
1910d444b03eSAli Bahrami 		}
1911d444b03eSAli Bahrami 		if (dyns != NULL) {
1912d444b03eSAli Bahrami 			dynnum = dyncache->c_shdr->sh_size /
1913d444b03eSAli Bahrami 			    dyncache->c_shdr->sh_entsize;
1914d444b03eSAli Bahrami 
1915d444b03eSAli Bahrami 			/*
1916d444b03eSAli Bahrami 			 * We validate the type of dynamic elements referenced
1917d444b03eSAli Bahrami 			 * from the syminfo. This array is used report any
1918d444b03eSAli Bahrami 			 * bad dynamic entries.
1919d444b03eSAli Bahrami 			 */
1920d444b03eSAli Bahrami 			if ((dynerr = calloc(dynnum, sizeof (*dynerr))) ==
1921d444b03eSAli Bahrami 			    NULL) {
1922d444b03eSAli Bahrami 				int err = errno;
1923d444b03eSAli Bahrami 				(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
1924d444b03eSAli Bahrami 				    file, strerror(err));
19257c478bd9Sstevel@tonic-gate 				return;
19267c478bd9Sstevel@tonic-gate 			}
1927d444b03eSAli Bahrami 		}
19287c478bd9Sstevel@tonic-gate 	}
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate 	/*
19315aefb655Srie 	 * Get the data buffer for the associated symbol table and string table.
19327c478bd9Sstevel@tonic-gate 	 */
19335aefb655Srie 	if (stringtbl(cache, 1, cnt, shnum, file,
19345aefb655Srie 	    &symnum, &symsec, &strsec) == 0)
19357c478bd9Sstevel@tonic-gate 		return;
19367c478bd9Sstevel@tonic-gate 
19375aefb655Srie 	syms = symsec->c_data->d_buf;
19385aefb655Srie 
19397c478bd9Sstevel@tonic-gate 	/*
19405aefb655Srie 	 * Loop through the syminfo entries.
19417c478bd9Sstevel@tonic-gate 	 */
19425aefb655Srie 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
19435aefb655Srie 	dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMINFO), infocache->c_name);
19445aefb655Srie 	Elf_syminfo_title(0);
19457c478bd9Sstevel@tonic-gate 
19465aefb655Srie 	for (ndx = 1, info++; ndx < infonum; ndx++, info++) {
19475aefb655Srie 		Sym 		*sym;
1948d444b03eSAli Bahrami 		const char	*needed, *name;
1949d444b03eSAli Bahrami 		Word		expect_dt;
1950d444b03eSAli Bahrami 		Word		boundto = info->si_boundto;
19517c478bd9Sstevel@tonic-gate 
1952d444b03eSAli Bahrami 		if ((info->si_flags == 0) && (boundto == 0))
19537c478bd9Sstevel@tonic-gate 			continue;
19547c478bd9Sstevel@tonic-gate 
19555aefb655Srie 		sym = &syms[ndx];
19565aefb655Srie 		name = string(infocache, ndx, strsec, file, sym->st_name);
19577c478bd9Sstevel@tonic-gate 
1958d444b03eSAli Bahrami 		/* Is si_boundto set to one of the reserved values? */
1959d444b03eSAli Bahrami 		if (boundto >= SYMINFO_BT_LOWRESERVE) {
1960d444b03eSAli Bahrami 			Elf_syminfo_entry(0, ndx, info, name, NULL);
1961d444b03eSAli Bahrami 			continue;
1962d444b03eSAli Bahrami 		}
19637c478bd9Sstevel@tonic-gate 
1964d444b03eSAli Bahrami 		/*
1965d444b03eSAli Bahrami 		 * si_boundto is referencing a dynamic section. If we don't
1966d444b03eSAli Bahrami 		 * have one, an error was already issued above, so it suffices
1967d444b03eSAli Bahrami 		 * to display an empty string. If we are out of bounds, then
1968d444b03eSAli Bahrami 		 * report that and then display an empty string.
1969d444b03eSAli Bahrami 		 */
1970d444b03eSAli Bahrami 		if ((dyns == NULL) || (boundto >= dynnum)) {
1971d444b03eSAli Bahrami 			if (dyns != NULL)
1972d444b03eSAli Bahrami 				(void) fprintf(stderr,
1973d444b03eSAli Bahrami 				    MSG_INTL(MSG_ERR_BADSIDYNNDX), file,
1974d444b03eSAli Bahrami 				    infocache->c_ndx, infocache->c_name,
1975d444b03eSAli Bahrami 				    EC_WORD(ndx), EC_WORD(dynnum - 1),
1976d444b03eSAli Bahrami 				    EC_WORD(boundto));
1977d444b03eSAli Bahrami 			Elf_syminfo_entry(0, ndx, info, name,
1978d444b03eSAli Bahrami 			    MSG_ORIG(MSG_STR_EMPTY));
1979d444b03eSAli Bahrami 			continue;
1980d444b03eSAli Bahrami 		}
1981d444b03eSAli Bahrami 
1982d444b03eSAli Bahrami 		/*
1983d444b03eSAli Bahrami 		 * The si_boundto reference expects a specific dynamic element
1984d444b03eSAli Bahrami 		 * type at the given index. The dynamic element is always a
1985d444b03eSAli Bahrami 		 * string that gives an object name. The specific type depends
1986d444b03eSAli Bahrami 		 * on the si_flags present. Ensure that we've got the right
1987d444b03eSAli Bahrami 		 * type.
1988d444b03eSAli Bahrami 		 */
1989d444b03eSAli Bahrami 		if (info->si_flags & SYMINFO_FLG_FILTER)
1990d444b03eSAli Bahrami 			expect_dt = DT_SUNW_FILTER;
1991d444b03eSAli Bahrami 		else if (info->si_flags & SYMINFO_FLG_AUXILIARY)
1992d444b03eSAli Bahrami 			expect_dt = DT_SUNW_AUXILIARY;
1993d444b03eSAli Bahrami 		else if (info->si_flags & (SYMINFO_FLG_DIRECT |
1994d444b03eSAli Bahrami 		    SYMINFO_FLG_LAZYLOAD | SYMINFO_FLG_DIRECTBIND))
1995d444b03eSAli Bahrami 			expect_dt = DT_NEEDED;
1996d444b03eSAli Bahrami 		else
1997d444b03eSAli Bahrami 			expect_dt = DT_NULL;   /* means we ignore the type */
1998d444b03eSAli Bahrami 
1999d444b03eSAli Bahrami 		if ((dyns[boundto].d_tag != expect_dt) &&
2000d444b03eSAli Bahrami 		    (expect_dt != DT_NULL)) {
2001d444b03eSAli Bahrami 			Conv_inv_buf_t	buf1, buf2;
2002d444b03eSAli Bahrami 
2003d444b03eSAli Bahrami 			/* Only complain about each dynamic element once */
2004d444b03eSAli Bahrami 			if (!dynerr[boundto]) {
2005d444b03eSAli Bahrami 				(void) fprintf(stderr,
2006d444b03eSAli Bahrami 				    MSG_INTL(MSG_ERR_BADSIDYNTAG),
2007d444b03eSAli Bahrami 				    file, infocache->c_ndx, infocache->c_name,
2008d444b03eSAli Bahrami 				    EC_WORD(ndx), dyncache->c_ndx,
2009d444b03eSAli Bahrami 				    dyncache->c_name, EC_WORD(boundto),
2010d444b03eSAli Bahrami 				    conv_dyn_tag(expect_dt, osabi,
2011d444b03eSAli Bahrami 				    ehdr->e_machine, CONV_FMT_ALT_CF, &buf1),
2012d444b03eSAli Bahrami 				    conv_dyn_tag(dyns[boundto].d_tag, osabi,
2013d444b03eSAli Bahrami 				    ehdr->e_machine, CONV_FMT_ALT_CF, &buf2));
2014d444b03eSAli Bahrami 				dynerr[boundto] = TRUE;
2015d444b03eSAli Bahrami 			}
2016d444b03eSAli Bahrami 		}
2017d444b03eSAli Bahrami 
2018d444b03eSAli Bahrami 		/*
2019d444b03eSAli Bahrami 		 * Whether or not the DT item we're pointing at is
2020d444b03eSAli Bahrami 		 * of the right type, if it's a type we recognize as
2021d444b03eSAli Bahrami 		 * providing a string, go ahead and show it. Otherwise
2022d444b03eSAli Bahrami 		 * an empty string.
2023d444b03eSAli Bahrami 		 */
2024d444b03eSAli Bahrami 		switch (dyns[boundto].d_tag) {
2025d444b03eSAli Bahrami 		case DT_NEEDED:
2026d444b03eSAli Bahrami 		case DT_SONAME:
2027d444b03eSAli Bahrami 		case DT_RPATH:
2028d444b03eSAli Bahrami 		case DT_RUNPATH:
2029d444b03eSAli Bahrami 		case DT_CONFIG:
2030d444b03eSAli Bahrami 		case DT_DEPAUDIT:
2031d444b03eSAli Bahrami 		case DT_USED:
2032d444b03eSAli Bahrami 		case DT_AUDIT:
2033d444b03eSAli Bahrami 		case DT_SUNW_AUXILIARY:
2034d444b03eSAli Bahrami 		case DT_SUNW_FILTER:
2035d444b03eSAli Bahrami 		case DT_FILTER:
2036d444b03eSAli Bahrami 		case DT_AUXILIARY:
2037d444b03eSAli Bahrami 			needed = string(infocache, boundto,
2038d444b03eSAli Bahrami 			    strsec, file, dyns[boundto].d_un.d_val);
2039d444b03eSAli Bahrami 			break;
2040d444b03eSAli Bahrami 		default:
2041d444b03eSAli Bahrami 			needed = MSG_ORIG(MSG_STR_EMPTY);
20427c478bd9Sstevel@tonic-gate 		}
20435aefb655Srie 		Elf_syminfo_entry(0, ndx, info, name, needed);
20447c478bd9Sstevel@tonic-gate 	}
2045d444b03eSAli Bahrami 	if (dyns != NULL)
2046d444b03eSAli Bahrami 		free(dynerr);
20477c478bd9Sstevel@tonic-gate }
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate /*
20507c478bd9Sstevel@tonic-gate  * Print version definition section entries.
20517c478bd9Sstevel@tonic-gate  */
20527c478bd9Sstevel@tonic-gate static void
2053d840867fSab196087 version_def(Verdef *vdf, Word vdf_num, Cache *vcache, Cache *scache,
20547c478bd9Sstevel@tonic-gate     const char *file)
20557c478bd9Sstevel@tonic-gate {
20565aefb655Srie 	Word	cnt;
20577c478bd9Sstevel@tonic-gate 	char	index[MAXNDXSIZE];
20587c478bd9Sstevel@tonic-gate 
20595aefb655Srie 	Elf_ver_def_title(0);
20607c478bd9Sstevel@tonic-gate 
2061d840867fSab196087 	for (cnt = 1; cnt <= vdf_num; cnt++,
20625aefb655Srie 	    vdf = (Verdef *)((uintptr_t)vdf + vdf->vd_next)) {
2063090a8d9eSAli Bahrami 		Conv_ver_flags_buf_t	ver_flags_buf;
20647c478bd9Sstevel@tonic-gate 		const char		*name, *dep;
20655aefb655Srie 		Half			vcnt = vdf->vd_cnt - 1;
20665aefb655Srie 		Half			ndx = vdf->vd_ndx;
206733eb6ee1Sab196087 		Verdaux	*vdap = (Verdaux *)((uintptr_t)vdf + vdf->vd_aux);
20687c478bd9Sstevel@tonic-gate 
20697c478bd9Sstevel@tonic-gate 		/*
20707c478bd9Sstevel@tonic-gate 		 * Obtain the name and first dependency (if any).
20717c478bd9Sstevel@tonic-gate 		 */
20727c478bd9Sstevel@tonic-gate 		name = string(vcache, cnt, scache, file, vdap->vda_name);
20735aefb655Srie 		vdap = (Verdaux *)((uintptr_t)vdap + vdap->vda_next);
20747c478bd9Sstevel@tonic-gate 		if (vcnt)
20757c478bd9Sstevel@tonic-gate 			dep = string(vcache, cnt, scache, file, vdap->vda_name);
20767c478bd9Sstevel@tonic-gate 		else
20777c478bd9Sstevel@tonic-gate 			dep = MSG_ORIG(MSG_STR_EMPTY);
20787c478bd9Sstevel@tonic-gate 
20797c478bd9Sstevel@tonic-gate 		(void) snprintf(index, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INDEX),
20807c478bd9Sstevel@tonic-gate 		    EC_XWORD(ndx));
20815aefb655Srie 		Elf_ver_line_1(0, index, name, dep,
2082090a8d9eSAli Bahrami 		    conv_ver_flags(vdf->vd_flags, 0, &ver_flags_buf));
20837c478bd9Sstevel@tonic-gate 
20847c478bd9Sstevel@tonic-gate 		/*
20857c478bd9Sstevel@tonic-gate 		 * Print any additional dependencies.
20867c478bd9Sstevel@tonic-gate 		 */
20877c478bd9Sstevel@tonic-gate 		if (vcnt) {
20885aefb655Srie 			vdap = (Verdaux *)((uintptr_t)vdap + vdap->vda_next);
20897c478bd9Sstevel@tonic-gate 			for (vcnt--; vcnt; vcnt--,
20905aefb655Srie 			    vdap = (Verdaux *)((uintptr_t)vdap +
20917c478bd9Sstevel@tonic-gate 			    vdap->vda_next)) {
20927c478bd9Sstevel@tonic-gate 				dep = string(vcache, cnt, scache, file,
20937c478bd9Sstevel@tonic-gate 				    vdap->vda_name);
20945aefb655Srie 				Elf_ver_line_2(0, MSG_ORIG(MSG_STR_EMPTY), dep);
20957c478bd9Sstevel@tonic-gate 			}
20967c478bd9Sstevel@tonic-gate 		}
20977c478bd9Sstevel@tonic-gate 	}
20987c478bd9Sstevel@tonic-gate }
20997c478bd9Sstevel@tonic-gate 
21007c478bd9Sstevel@tonic-gate /*
2101d840867fSab196087  * Print version needed section entries.
2102d840867fSab196087  *
2103d840867fSab196087  * entry:
2104d840867fSab196087  *	vnd - Address of verneed data
2105d840867fSab196087  *	vnd_num - # of Verneed entries
2106d840867fSab196087  *	vcache - Cache of verneed section being processed
2107d840867fSab196087  *	scache - Cache of associated string table section
2108d840867fSab196087  *	file - Name of object being processed.
2109d840867fSab196087  *	versym - Information about versym section
2110d840867fSab196087  *
2111d840867fSab196087  * exit:
2112d840867fSab196087  *	The versions have been printed. If GNU style versioning
2113d840867fSab196087  *	is in effect, versym->max_verndx has been updated to
2114d840867fSab196087  *	contain the largest version index seen.
2115090a8d9eSAli Bahrami  *
2116090a8d9eSAli Bahrami  * note:
2117090a8d9eSAli Bahrami  * 	The versym section of an object that follows the original
2118090a8d9eSAli Bahrami  *	Solaris versioning rules only contains indexes into the verdef
2119090a8d9eSAli Bahrami  *	section. Symbols defined in other objects (UNDEF) are given
2120090a8d9eSAli Bahrami  *	a version of 0, indicating that they are not defined by
2121090a8d9eSAli Bahrami  *	this file, and the Verneed entries do not have associated version
2122090a8d9eSAli Bahrami  *	indexes. For these reasons, we do not display a version index
2123090a8d9eSAli Bahrami  *	for original-style Verneed sections.
2124090a8d9eSAli Bahrami  *
2125090a8d9eSAli Bahrami  *	The GNU versioning extensions alter this: Symbols defined in other
2126090a8d9eSAli Bahrami  *	objects receive a version index in the range above those defined
2127090a8d9eSAli Bahrami  *	by the Verdef section, and the vna_other field of the Vernaux
2128090a8d9eSAli Bahrami  *	structs inside the Verneed section contain the version index for
2129090a8d9eSAli Bahrami  *	that item. We therefore  display the index when showing the
2130090a8d9eSAli Bahrami  *	contents of a GNU style Verneed section. You should not
2131090a8d9eSAli Bahrami  *	necessarily expect these indexes to appear in sorted
2132090a8d9eSAli Bahrami  *	order --- it seems that the GNU ld assigns the versions as
2133090a8d9eSAli Bahrami  *	symbols are encountered during linking, and then the results
2134090a8d9eSAli Bahrami  *	are assembled into the Verneed section afterwards.
21357c478bd9Sstevel@tonic-gate  */
21367c478bd9Sstevel@tonic-gate static void
2137d840867fSab196087 version_need(Verneed *vnd, Word vnd_num, Cache *vcache, Cache *scache,
2138d840867fSab196087     const char *file, VERSYM_STATE *versym)
21397c478bd9Sstevel@tonic-gate {
21405aefb655Srie 	Word		cnt;
2141d840867fSab196087 	char		index[MAXNDXSIZE];
2142d840867fSab196087 	const char	*index_str;
21437c478bd9Sstevel@tonic-gate 
2144090a8d9eSAli Bahrami 	Elf_ver_need_title(0, versym->gnu_needed);
2145d840867fSab196087 
2146d840867fSab196087 	for (cnt = 1; cnt <= vnd_num; cnt++,
21475aefb655Srie 	    vnd = (Verneed *)((uintptr_t)vnd + vnd->vn_next)) {
2148090a8d9eSAli Bahrami 		Conv_ver_flags_buf_t	ver_flags_buf;
21497c478bd9Sstevel@tonic-gate 		const char		*name, *dep;
21505aefb655Srie 		Half			vcnt = vnd->vn_cnt;
215133eb6ee1Sab196087 		Vernaux *vnap = (Vernaux *)((uintptr_t)vnd + vnd->vn_aux);
21527c478bd9Sstevel@tonic-gate 
21537c478bd9Sstevel@tonic-gate 		/*
21547c478bd9Sstevel@tonic-gate 		 * Obtain the name of the needed file and the version name
21557c478bd9Sstevel@tonic-gate 		 * within it that we're dependent on.  Note that the count
21567c478bd9Sstevel@tonic-gate 		 * should be at least one, otherwise this is a pretty bogus
21577c478bd9Sstevel@tonic-gate 		 * entry.
21587c478bd9Sstevel@tonic-gate 		 */
21597c478bd9Sstevel@tonic-gate 		name = string(vcache, cnt, scache, file, vnd->vn_file);
21607c478bd9Sstevel@tonic-gate 		if (vcnt)
21617c478bd9Sstevel@tonic-gate 			dep = string(vcache, cnt, scache, file, vnap->vna_name);
21627c478bd9Sstevel@tonic-gate 		else
21637c478bd9Sstevel@tonic-gate 			dep = MSG_INTL(MSG_STR_NULL);
21647c478bd9Sstevel@tonic-gate 
2165090a8d9eSAli Bahrami 		if (vnap->vna_other == 0) {	/* Traditional form */
2166090a8d9eSAli Bahrami 			index_str = MSG_ORIG(MSG_STR_EMPTY);
2167090a8d9eSAli Bahrami 		} else {			/* GNU form */
2168090a8d9eSAli Bahrami 			index_str = index;
2169d840867fSab196087 			/* Format the version index value */
2170d840867fSab196087 			(void) snprintf(index, MAXNDXSIZE,
2171d840867fSab196087 			    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(vnap->vna_other));
2172d840867fSab196087 			if (vnap->vna_other > versym->max_verndx)
2173d840867fSab196087 				versym->max_verndx = vnap->vna_other;
2174d840867fSab196087 		}
2175d840867fSab196087 		Elf_ver_line_1(0, index_str, name, dep,
2176090a8d9eSAli Bahrami 		    conv_ver_flags(vnap->vna_flags, 0, &ver_flags_buf));
21777c478bd9Sstevel@tonic-gate 
21787c478bd9Sstevel@tonic-gate 		/*
21797c478bd9Sstevel@tonic-gate 		 * Print any additional version dependencies.
21807c478bd9Sstevel@tonic-gate 		 */
21817c478bd9Sstevel@tonic-gate 		if (vcnt) {
21825aefb655Srie 			vnap = (Vernaux *)((uintptr_t)vnap + vnap->vna_next);
21837c478bd9Sstevel@tonic-gate 			for (vcnt--; vcnt; vcnt--,
21845aefb655Srie 			    vnap = (Vernaux *)((uintptr_t)vnap +
21857c478bd9Sstevel@tonic-gate 			    vnap->vna_next)) {
21867c478bd9Sstevel@tonic-gate 				dep = string(vcache, cnt, scache, file,
21877c478bd9Sstevel@tonic-gate 				    vnap->vna_name);
2188090a8d9eSAli Bahrami 				if (vnap->vna_other > 0) {
2189d840867fSab196087 					/* Format the next index value */
2190d840867fSab196087 					(void) snprintf(index, MAXNDXSIZE,
2191d840867fSab196087 					    MSG_ORIG(MSG_FMT_INDEX),
2192d840867fSab196087 					    EC_XWORD(vnap->vna_other));
2193090a8d9eSAli Bahrami 					Elf_ver_line_1(0, index,
2194d840867fSab196087 					    MSG_ORIG(MSG_STR_EMPTY), dep,
2195090a8d9eSAli Bahrami 					    conv_ver_flags(vnap->vna_flags,
2196090a8d9eSAli Bahrami 					    0, &ver_flags_buf));
2197d840867fSab196087 					if (vnap->vna_other >
2198d840867fSab196087 					    versym->max_verndx)
2199d840867fSab196087 						versym->max_verndx =
2200d840867fSab196087 						    vnap->vna_other;
2201d840867fSab196087 				} else {
2202d840867fSab196087 					Elf_ver_line_3(0,
2203d840867fSab196087 					    MSG_ORIG(MSG_STR_EMPTY), dep,
2204090a8d9eSAli Bahrami 					    conv_ver_flags(vnap->vna_flags,
2205090a8d9eSAli Bahrami 					    0, &ver_flags_buf));
2206d840867fSab196087 				}
2207d840867fSab196087 			}
2208d840867fSab196087 		}
2209d840867fSab196087 	}
2210d840867fSab196087 }
2211d840867fSab196087 
2212d840867fSab196087 /*
2213090a8d9eSAli Bahrami  * Examine the Verneed section for information related to GNU
2214090a8d9eSAli Bahrami  * style Versym indexing:
2215090a8d9eSAli Bahrami  *	- A non-zero vna_other field indicates that Versym indexes can
2216090a8d9eSAli Bahrami  *		reference Verneed records.
2217090a8d9eSAli Bahrami  *	- If the object uses GNU style Versym indexing, the
2218090a8d9eSAli Bahrami  *	  maximum index value is needed to detect bad Versym entries.
2219d840867fSab196087  *
2220d840867fSab196087  * entry:
2221d840867fSab196087  *	vnd - Address of verneed data
2222d840867fSab196087  *	vnd_num - # of Verneed entries
2223d840867fSab196087  *	versym - Information about versym section
2224d840867fSab196087  *
2225d840867fSab196087  * exit:
2226090a8d9eSAli Bahrami  *	If a non-zero vna_other field is seen, versym->gnu_needed is set.
2227090a8d9eSAli Bahrami  *
2228d840867fSab196087  *	versym->max_verndx has been updated to contain the largest
2229d840867fSab196087  *	version index seen.
2230d840867fSab196087  */
2231d840867fSab196087 static void
2232090a8d9eSAli Bahrami update_gnu_verndx(Verneed *vnd, Word vnd_num, VERSYM_STATE *versym)
2233d840867fSab196087 {
2234d840867fSab196087 	Word		cnt;
2235d840867fSab196087 
2236d840867fSab196087 	for (cnt = 1; cnt <= vnd_num; cnt++,
2237d840867fSab196087 	    vnd = (Verneed *)((uintptr_t)vnd + vnd->vn_next)) {
2238d840867fSab196087 		Half	vcnt = vnd->vn_cnt;
2239d840867fSab196087 		Vernaux	*vnap = (Vernaux *)((uintptr_t)vnd + vnd->vn_aux);
2240d840867fSab196087 
2241090a8d9eSAli Bahrami 		/*
2242090a8d9eSAli Bahrami 		 * A non-zero value of vna_other indicates that this
2243090a8d9eSAli Bahrami 		 * object references VERNEED items from the VERSYM
2244090a8d9eSAli Bahrami 		 * array.
2245090a8d9eSAli Bahrami 		 */
2246090a8d9eSAli Bahrami 		if (vnap->vna_other != 0) {
2247090a8d9eSAli Bahrami 			versym->gnu_needed = 1;
2248d840867fSab196087 			if (vnap->vna_other > versym->max_verndx)
2249d840867fSab196087 				versym->max_verndx = vnap->vna_other;
2250090a8d9eSAli Bahrami 		}
2251d840867fSab196087 
2252d840867fSab196087 		/*
2253d840867fSab196087 		 * Check any additional version dependencies.
2254d840867fSab196087 		 */
2255d840867fSab196087 		if (vcnt) {
2256d840867fSab196087 			vnap = (Vernaux *)((uintptr_t)vnap + vnap->vna_next);
2257d840867fSab196087 			for (vcnt--; vcnt; vcnt--,
2258d840867fSab196087 			    vnap = (Vernaux *)((uintptr_t)vnap +
2259d840867fSab196087 			    vnap->vna_next)) {
2260090a8d9eSAli Bahrami 				if (vnap->vna_other == 0)
2261090a8d9eSAli Bahrami 					continue;
2262090a8d9eSAli Bahrami 
2263090a8d9eSAli Bahrami 				versym->gnu_needed = 1;
2264d840867fSab196087 				if (vnap->vna_other > versym->max_verndx)
2265d840867fSab196087 					versym->max_verndx = vnap->vna_other;
22667c478bd9Sstevel@tonic-gate 			}
22677c478bd9Sstevel@tonic-gate 		}
22687c478bd9Sstevel@tonic-gate 	}
22697c478bd9Sstevel@tonic-gate }
22707c478bd9Sstevel@tonic-gate 
22717c478bd9Sstevel@tonic-gate /*
22723b41b08bSab196087  * Display version section information if the flags require it.
22733b41b08bSab196087  * Return version information needed by other output.
22743b41b08bSab196087  *
22753b41b08bSab196087  * entry:
22763b41b08bSab196087  *	cache - Cache of all section headers
22773b41b08bSab196087  *	shnum - # of sections in cache
22783b41b08bSab196087  *	file - Name of file
22793b41b08bSab196087  *	flags - Command line option flags
22803b41b08bSab196087  *	versym - VERSYM_STATE block to be filled in.
22817c478bd9Sstevel@tonic-gate  */
22823b41b08bSab196087 static void
22833b41b08bSab196087 versions(Cache *cache, Word shnum, const char *file, uint_t flags,
22843b41b08bSab196087     VERSYM_STATE *versym)
22857c478bd9Sstevel@tonic-gate {
22867c478bd9Sstevel@tonic-gate 	GElf_Word	cnt;
2287d840867fSab196087 	Cache		*verdef_cache = NULL, *verneed_cache = NULL;
22883b41b08bSab196087 
2289d840867fSab196087 
2290d840867fSab196087 	/* Gather information about the version sections */
2291d840867fSab196087 	versym->max_verndx = 1;
22927c478bd9Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
22937c478bd9Sstevel@tonic-gate 		Cache		*_cache = &cache[cnt];
22945aefb655Srie 		Shdr		*shdr = _cache->c_shdr;
2295d840867fSab196087 		Dyn		*dyn;
2296d840867fSab196087 		ulong_t		numdyn;
22977c478bd9Sstevel@tonic-gate 
2298d840867fSab196087 		switch (shdr->sh_type) {
2299d840867fSab196087 		case SHT_DYNAMIC:
23007c478bd9Sstevel@tonic-gate 			/*
2301d840867fSab196087 			 * The GNU ld puts a DT_VERSYM entry in the dynamic
2302d840867fSab196087 			 * section so that the runtime linker can use it to
2303d840867fSab196087 			 * implement their versioning rules. They allow multiple
2304d840867fSab196087 			 * incompatible functions with the same name to exist
2305d840867fSab196087 			 * in different versions. The Solaris ld does not
2306d840867fSab196087 			 * support this mechanism, and as such, does not
2307d840867fSab196087 			 * produce DT_VERSYM. We use this fact to determine
2308d840867fSab196087 			 * which ld produced this object, and how to interpret
2309d840867fSab196087 			 * the version values.
23107c478bd9Sstevel@tonic-gate 			 */
2311d840867fSab196087 			if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0) ||
2312d840867fSab196087 			    (_cache->c_data == NULL))
2313d840867fSab196087 				continue;
2314d840867fSab196087 			numdyn = shdr->sh_size / shdr->sh_entsize;
2315d840867fSab196087 			dyn = (Dyn *)_cache->c_data->d_buf;
2316d840867fSab196087 			for (; numdyn-- > 0; dyn++)
2317d840867fSab196087 				if (dyn->d_tag == DT_VERSYM) {
2318090a8d9eSAli Bahrami 					versym->gnu_full =
2319090a8d9eSAli Bahrami 					    versym->gnu_needed = 1;
2320d840867fSab196087 					break;
2321d840867fSab196087 				}
2322d840867fSab196087 			break;
23233b41b08bSab196087 
2324d840867fSab196087 		case SHT_SUNW_versym:
2325d840867fSab196087 			/* Record data address for later symbol processing */
2326d840867fSab196087 			if (_cache->c_data != NULL) {
23273b41b08bSab196087 				versym->cache = _cache;
23283b41b08bSab196087 				versym->data = _cache->c_data->d_buf;
23297c478bd9Sstevel@tonic-gate 				continue;
23307c478bd9Sstevel@tonic-gate 			}
2331d840867fSab196087 			break;
23327c478bd9Sstevel@tonic-gate 
2333d840867fSab196087 		case SHT_SUNW_verdef:
2334d840867fSab196087 		case SHT_SUNW_verneed:
23353b41b08bSab196087 			/*
2336d840867fSab196087 			 * Ensure the data is non-NULL and the number
2337d840867fSab196087 			 * of items is non-zero. Otherwise, we don't
2338d840867fSab196087 			 * understand the section, and will not use it.
23397c478bd9Sstevel@tonic-gate 			 */
23409a411307Srie 			if ((_cache->c_data == NULL) ||
2341d840867fSab196087 			    (_cache->c_data->d_buf == NULL)) {
23427c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
2343d840867fSab196087 				    file, _cache->c_name);
23447c478bd9Sstevel@tonic-gate 				continue;
23457c478bd9Sstevel@tonic-gate 			}
2346d840867fSab196087 			if (shdr->sh_info == 0) {
2347d840867fSab196087 				(void) fprintf(stderr,
2348d840867fSab196087 				    MSG_INTL(MSG_ERR_BADSHINFO),
2349d840867fSab196087 				    file, _cache->c_name,
2350d840867fSab196087 				    EC_WORD(shdr->sh_info));
2351d840867fSab196087 				continue;
2352d840867fSab196087 			}
2353d840867fSab196087 
2354d840867fSab196087 			/* Make sure the string table index is in range */
2355d840867fSab196087 			if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
2356d840867fSab196087 				(void) fprintf(stderr,
2357d840867fSab196087 				    MSG_INTL(MSG_ERR_BADSHLINK), file,
2358d840867fSab196087 				    _cache->c_name, EC_WORD(shdr->sh_link));
23597c478bd9Sstevel@tonic-gate 				continue;
23607c478bd9Sstevel@tonic-gate 			}
23617c478bd9Sstevel@tonic-gate 
23627c478bd9Sstevel@tonic-gate 			/*
2363d840867fSab196087 			 * The section is usable. Save the cache entry.
23647c478bd9Sstevel@tonic-gate 			 */
2365d840867fSab196087 			if (shdr->sh_type == SHT_SUNW_verdef) {
2366d840867fSab196087 				verdef_cache = _cache;
2367d840867fSab196087 				/*
2368d840867fSab196087 				 * Under Solaris rules, if there is a verdef
2369d840867fSab196087 				 * section, the max versym index is number
2370d840867fSab196087 				 * of version definitions it supplies.
2371d840867fSab196087 				 */
2372d840867fSab196087 				versym->max_verndx = shdr->sh_info;
2373d840867fSab196087 			} else {
2374d840867fSab196087 				verneed_cache = _cache;
2375d840867fSab196087 			}
2376d840867fSab196087 			break;
2377d840867fSab196087 		}
23787c478bd9Sstevel@tonic-gate 	}
23797c478bd9Sstevel@tonic-gate 
2380d840867fSab196087 	/*
2381090a8d9eSAli Bahrami 	 * If there is a Verneed section, examine it for information
2382090a8d9eSAli Bahrami 	 * related to GNU style versioning.
2383d840867fSab196087 	 */
2384090a8d9eSAli Bahrami 	if (verneed_cache != NULL)
2385090a8d9eSAli Bahrami 		update_gnu_verndx((Verneed *)verneed_cache->c_data->d_buf,
2386d840867fSab196087 		    verneed_cache->c_shdr->sh_info, versym);
2387d840867fSab196087 
2388d840867fSab196087 	/*
2389d840867fSab196087 	 * Now that all the information is available, display the
2390090a8d9eSAli Bahrami 	 * Verdef and Verneed section contents, if requested.
2391d840867fSab196087 	 */
2392090a8d9eSAli Bahrami 	if ((flags & FLG_SHOW_VERSIONS) == 0)
2393090a8d9eSAli Bahrami 		return;
2394d840867fSab196087 	if (verdef_cache != NULL) {
2395d840867fSab196087 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2396d840867fSab196087 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_VERDEF),
2397d840867fSab196087 		    verdef_cache->c_name);
2398d840867fSab196087 		version_def((Verdef *)verdef_cache->c_data->d_buf,
2399d840867fSab196087 		    verdef_cache->c_shdr->sh_info, verdef_cache,
2400d840867fSab196087 		    &cache[verdef_cache->c_shdr->sh_link], file);
2401d840867fSab196087 	}
2402d840867fSab196087 	if (verneed_cache != NULL) {
2403d840867fSab196087 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2404d840867fSab196087 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_VERNEED),
2405d840867fSab196087 		    verneed_cache->c_name);
2406d840867fSab196087 		/*
2407d840867fSab196087 		 * If GNU versioning applies to this object, version_need()
2408d840867fSab196087 		 * will update versym->max_verndx, and it is not
2409090a8d9eSAli Bahrami 		 * necessary to call update_gnu_verndx().
2410d840867fSab196087 		 */
2411d840867fSab196087 		version_need((Verneed *)verneed_cache->c_data->d_buf,
2412d840867fSab196087 		    verneed_cache->c_shdr->sh_info, verneed_cache,
2413d840867fSab196087 		    &cache[verneed_cache->c_shdr->sh_link], file, versym);
24147c478bd9Sstevel@tonic-gate 	}
24157c478bd9Sstevel@tonic-gate }
24167c478bd9Sstevel@tonic-gate 
24177c478bd9Sstevel@tonic-gate /*
2418d579eb63Sab196087  * Search for and process any symbol tables.
2419d579eb63Sab196087  */
2420d579eb63Sab196087 void
24214f680cc6SAli Bahrami symbols(Cache *cache, Word shnum, Ehdr *ehdr, uchar_t osabi,
24224f680cc6SAli Bahrami     VERSYM_STATE *versym, const char *file, uint_t flags)
2423d579eb63Sab196087 {
2424d579eb63Sab196087 	SYMTBL_STATE state;
2425d579eb63Sab196087 	Cache *_cache;
2426d579eb63Sab196087 	Word secndx;
2427d579eb63Sab196087 
2428d579eb63Sab196087 	for (secndx = 1; secndx < shnum; secndx++) {
2429d579eb63Sab196087 		Word		symcnt;
2430d579eb63Sab196087 		Shdr		*shdr;
2431d579eb63Sab196087 
2432d579eb63Sab196087 		_cache = &cache[secndx];
2433d579eb63Sab196087 		shdr = _cache->c_shdr;
2434d579eb63Sab196087 
2435d579eb63Sab196087 		if ((shdr->sh_type != SHT_SYMTAB) &&
2436d579eb63Sab196087 		    (shdr->sh_type != SHT_DYNSYM) &&
24374f680cc6SAli Bahrami 		    ((shdr->sh_type != SHT_SUNW_LDYNSYM) ||
24384f680cc6SAli Bahrami 		    (osabi != ELFOSABI_SOLARIS)))
2439d579eb63Sab196087 			continue;
2440981a172dSab196087 		if (!match(MATCH_F_ALL, _cache->c_name, secndx, shdr->sh_type))
2441d579eb63Sab196087 			continue;
2442d579eb63Sab196087 
2443d579eb63Sab196087 		if (!init_symtbl_state(&state, cache, shnum, secndx, ehdr,
24444f680cc6SAli Bahrami 		    osabi, versym, file, flags))
2445d579eb63Sab196087 			continue;
2446d579eb63Sab196087 		/*
2447d579eb63Sab196087 		 * Loop through the symbol tables entries.
2448d579eb63Sab196087 		 */
2449d579eb63Sab196087 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2450d579eb63Sab196087 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMTAB), state.secname);
2451d579eb63Sab196087 		Elf_syms_table_title(0, ELF_DBG_ELFDUMP);
2452d579eb63Sab196087 
2453d579eb63Sab196087 		for (symcnt = 0; symcnt < state.symn; symcnt++)
2454b02637afSrie 			output_symbol(&state, symcnt, shdr->sh_info, symcnt,
2455d579eb63Sab196087 			    state.sym + symcnt);
2456d579eb63Sab196087 	}
2457d579eb63Sab196087 }
2458d579eb63Sab196087 
2459d579eb63Sab196087 /*
2460d579eb63Sab196087  * Search for and process any SHT_SUNW_symsort or SHT_SUNW_tlssort sections.
2461d579eb63Sab196087  * These sections are always associated with the .SUNW_ldynsym./.dynsym pair.
2462d579eb63Sab196087  */
2463d579eb63Sab196087 static void
24644f680cc6SAli Bahrami sunw_sort(Cache *cache, Word shnum, Ehdr *ehdr, uchar_t osabi,
24654f680cc6SAli Bahrami     VERSYM_STATE *versym, const char *file, uint_t flags)
2466d579eb63Sab196087 {
2467d579eb63Sab196087 	SYMTBL_STATE	ldynsym_state,	dynsym_state;
2468d579eb63Sab196087 	Cache		*sortcache,	*symcache;
2469d579eb63Sab196087 	Shdr		*sortshdr,	*symshdr;
2470d579eb63Sab196087 	Word		sortsecndx,	symsecndx;
2471d579eb63Sab196087 	Word		ldynsym_cnt;
2472d579eb63Sab196087 	Word		*ndx;
2473d579eb63Sab196087 	Word		ndxn;
2474d579eb63Sab196087 	int		output_cnt = 0;
2475de777a60Sab196087 	Conv_inv_buf_t	inv_buf;
2476d579eb63Sab196087 
2477d579eb63Sab196087 	for (sortsecndx = 1; sortsecndx < shnum; sortsecndx++) {
2478d579eb63Sab196087 
2479d579eb63Sab196087 		sortcache = &cache[sortsecndx];
2480d579eb63Sab196087 		sortshdr = sortcache->c_shdr;
2481d579eb63Sab196087 
2482d579eb63Sab196087 		if ((sortshdr->sh_type != SHT_SUNW_symsort) &&
2483d579eb63Sab196087 		    (sortshdr->sh_type != SHT_SUNW_tlssort))
2484d579eb63Sab196087 			continue;
2485981a172dSab196087 		if (!match(MATCH_F_ALL, sortcache->c_name, sortsecndx,
2486981a172dSab196087 		    sortshdr->sh_type))
2487d579eb63Sab196087 			continue;
2488d579eb63Sab196087 
2489d579eb63Sab196087 		/*
2490d579eb63Sab196087 		 * If the section references a SUNW_ldynsym, then we
2491d579eb63Sab196087 		 * expect to see the associated .dynsym immediately
2492d579eb63Sab196087 		 * following. If it references a .dynsym, there is no
2493d579eb63Sab196087 		 * SUNW_ldynsym. If it is any other type, then we don't
2494d579eb63Sab196087 		 * know what to do with it.
2495d579eb63Sab196087 		 */
2496d579eb63Sab196087 		if ((sortshdr->sh_link == 0) || (sortshdr->sh_link >= shnum)) {
2497d579eb63Sab196087 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
2498d579eb63Sab196087 			    file, sortcache->c_name,
2499d579eb63Sab196087 			    EC_WORD(sortshdr->sh_link));
2500d579eb63Sab196087 			continue;
2501d579eb63Sab196087 		}
2502d579eb63Sab196087 		symcache = &cache[sortshdr->sh_link];
2503d579eb63Sab196087 		symshdr = symcache->c_shdr;
2504d579eb63Sab196087 		symsecndx = sortshdr->sh_link;
2505d579eb63Sab196087 		ldynsym_cnt = 0;
2506d579eb63Sab196087 		switch (symshdr->sh_type) {
2507d579eb63Sab196087 		case SHT_SUNW_LDYNSYM:
2508d579eb63Sab196087 			if (!init_symtbl_state(&ldynsym_state, cache, shnum,
25094f680cc6SAli Bahrami 			    symsecndx, ehdr, osabi, versym, file, flags))
2510d579eb63Sab196087 				continue;
2511d579eb63Sab196087 			ldynsym_cnt = ldynsym_state.symn;
2512d579eb63Sab196087 			/*
2513d579eb63Sab196087 			 * We know that the dynsym follows immediately
2514d579eb63Sab196087 			 * after the SUNW_ldynsym, and so, should be at
2515d579eb63Sab196087 			 * (sortshdr->sh_link + 1). However, elfdump is a
2516d579eb63Sab196087 			 * diagnostic tool, so we do the full paranoid
2517d579eb63Sab196087 			 * search instead.
2518d579eb63Sab196087 			 */
2519d579eb63Sab196087 			for (symsecndx = 1; symsecndx < shnum; symsecndx++) {
2520d579eb63Sab196087 				symcache = &cache[symsecndx];
2521d579eb63Sab196087 				symshdr = symcache->c_shdr;
2522d579eb63Sab196087 				if (symshdr->sh_type == SHT_DYNSYM)
2523d579eb63Sab196087 					break;
2524d579eb63Sab196087 			}
2525d579eb63Sab196087 			if (symsecndx >= shnum) {	/* Dynsym not found! */
2526d579eb63Sab196087 				(void) fprintf(stderr,
2527d579eb63Sab196087 				    MSG_INTL(MSG_ERR_NODYNSYM),
2528d579eb63Sab196087 				    file, sortcache->c_name);
2529d579eb63Sab196087 				continue;
2530d579eb63Sab196087 			}
2531d579eb63Sab196087 			/* Fallthrough to process associated dynsym */
2532d579eb63Sab196087 			/* FALLTHROUGH */
2533d579eb63Sab196087 		case SHT_DYNSYM:
2534d579eb63Sab196087 			if (!init_symtbl_state(&dynsym_state, cache, shnum,
25354f680cc6SAli Bahrami 			    symsecndx, ehdr, osabi, versym, file, flags))
2536d579eb63Sab196087 				continue;
2537d579eb63Sab196087 			break;
2538d579eb63Sab196087 		default:
2539d579eb63Sab196087 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADNDXSEC),
25404f680cc6SAli Bahrami 			    file, sortcache->c_name,
25414f680cc6SAli Bahrami 			    conv_sec_type(osabi, ehdr->e_machine,
25424f680cc6SAli Bahrami 			    symshdr->sh_type, 0, &inv_buf));
2543d579eb63Sab196087 			continue;
2544d579eb63Sab196087 		}
2545d579eb63Sab196087 
2546d579eb63Sab196087 		/*
2547d579eb63Sab196087 		 * Output header
2548d579eb63Sab196087 		 */
2549d579eb63Sab196087 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2550d579eb63Sab196087 		if (ldynsym_cnt > 0) {
2551d579eb63Sab196087 			dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMSORT2),
2552d579eb63Sab196087 			    sortcache->c_name, ldynsym_state.secname,
2553d579eb63Sab196087 			    dynsym_state.secname);
2554d579eb63Sab196087 			/*
2555d579eb63Sab196087 			 * The data for .SUNW_ldynsym and dynsym sections
2556d579eb63Sab196087 			 * is supposed to be adjacent with SUNW_ldynsym coming
2557d579eb63Sab196087 			 * first. Check, and issue a warning if it isn't so.
2558d579eb63Sab196087 			 */
255939773e46Sab196087 			if (((ldynsym_state.sym + ldynsym_state.symn)
256039773e46Sab196087 			    != dynsym_state.sym) &&
2561981a172dSab196087 			    ((flags & FLG_CTL_FAKESHDR) == 0))
2562d579eb63Sab196087 				(void) fprintf(stderr,
2563d579eb63Sab196087 				    MSG_INTL(MSG_ERR_LDYNNOTADJ), file,
2564d579eb63Sab196087 				    ldynsym_state.secname,
2565d579eb63Sab196087 				    dynsym_state.secname);
2566d579eb63Sab196087 		} else {
2567d579eb63Sab196087 			dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMSORT1),
2568d579eb63Sab196087 			    sortcache->c_name, dynsym_state.secname);
2569d579eb63Sab196087 		}
2570d579eb63Sab196087 		Elf_syms_table_title(0, ELF_DBG_ELFDUMP);
2571d579eb63Sab196087 
2572d579eb63Sab196087 		/* If not first one, insert a line of white space */
2573d579eb63Sab196087 		if (output_cnt++ > 0)
2574d579eb63Sab196087 			dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2575d579eb63Sab196087 
2576d579eb63Sab196087 		/*
2577d579eb63Sab196087 		 * SUNW_dynsymsort and SUNW_dyntlssort are arrays of
2578d579eb63Sab196087 		 * symbol indices. Iterate over the array entries,
2579d579eb63Sab196087 		 * dispaying the referenced symbols.
2580d579eb63Sab196087 		 */
2581d579eb63Sab196087 		ndxn = sortshdr->sh_size / sortshdr->sh_entsize;
2582d579eb63Sab196087 		ndx = (Word *)sortcache->c_data->d_buf;
2583d579eb63Sab196087 		for (; ndxn-- > 0; ndx++) {
2584d579eb63Sab196087 			if (*ndx >= ldynsym_cnt) {
2585d579eb63Sab196087 				Word sec_ndx = *ndx - ldynsym_cnt;
2586d579eb63Sab196087 
2587b02637afSrie 				output_symbol(&dynsym_state, sec_ndx, 0,
2588d579eb63Sab196087 				    *ndx, dynsym_state.sym + sec_ndx);
2589d579eb63Sab196087 			} else {
2590b02637afSrie 				output_symbol(&ldynsym_state, *ndx, 0,
2591d579eb63Sab196087 				    *ndx, ldynsym_state.sym + *ndx);
2592d579eb63Sab196087 			}
25937c478bd9Sstevel@tonic-gate 		}
25947c478bd9Sstevel@tonic-gate 	}
25957c478bd9Sstevel@tonic-gate }
25967c478bd9Sstevel@tonic-gate 
25977c478bd9Sstevel@tonic-gate /*
25987c478bd9Sstevel@tonic-gate  * Search for and process any relocation sections.
25997c478bd9Sstevel@tonic-gate  */
26007c478bd9Sstevel@tonic-gate static void
26010e233487SRod Evans reloc(Cache *cache, Word shnum, Ehdr *ehdr, const char *file)
26027c478bd9Sstevel@tonic-gate {
26035aefb655Srie 	Word	cnt;
26047c478bd9Sstevel@tonic-gate 
26057c478bd9Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
26065aefb655Srie 		Word		type, symnum;
26075aefb655Srie 		Xword		relndx, relnum, relsize;
26085aefb655Srie 		void		*rels;
26095aefb655Srie 		Sym		*syms;
26105aefb655Srie 		Cache		*symsec, *strsec;
26117c478bd9Sstevel@tonic-gate 		Cache		*_cache = &cache[cnt];
26125aefb655Srie 		Shdr		*shdr = _cache->c_shdr;
26135aefb655Srie 		char		*relname = _cache->c_name;
2614de777a60Sab196087 		Conv_inv_buf_t	inv_buf;
26157c478bd9Sstevel@tonic-gate 
26167c478bd9Sstevel@tonic-gate 		if (((type = shdr->sh_type) != SHT_RELA) &&
26177c478bd9Sstevel@tonic-gate 		    (type != SHT_REL))
26187c478bd9Sstevel@tonic-gate 			continue;
2619981a172dSab196087 		if (!match(MATCH_F_ALL, relname, cnt, type))
26207c478bd9Sstevel@tonic-gate 			continue;
26217c478bd9Sstevel@tonic-gate 
26227c478bd9Sstevel@tonic-gate 		/*
26235aefb655Srie 		 * Decide entry size.
26247c478bd9Sstevel@tonic-gate 		 */
26255aefb655Srie 		if (((relsize = shdr->sh_entsize) == 0) ||
26265aefb655Srie 		    (relsize > shdr->sh_size)) {
26277c478bd9Sstevel@tonic-gate 			if (type == SHT_RELA)
26285aefb655Srie 				relsize = sizeof (Rela);
26297c478bd9Sstevel@tonic-gate 			else
26305aefb655Srie 				relsize = sizeof (Rel);
26317c478bd9Sstevel@tonic-gate 		}
26327c478bd9Sstevel@tonic-gate 
26337c478bd9Sstevel@tonic-gate 		/*
26347c478bd9Sstevel@tonic-gate 		 * Determine the number of relocations available.
26357c478bd9Sstevel@tonic-gate 		 */
26367c478bd9Sstevel@tonic-gate 		if (shdr->sh_size == 0) {
26377c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
26385aefb655Srie 			    file, relname);
26397c478bd9Sstevel@tonic-gate 			continue;
26407c478bd9Sstevel@tonic-gate 		}
26419a411307Srie 		if (_cache->c_data == NULL)
26429a411307Srie 			continue;
26439a411307Srie 
26445aefb655Srie 		rels = _cache->c_data->d_buf;
26455aefb655Srie 		relnum = shdr->sh_size / relsize;
26467c478bd9Sstevel@tonic-gate 
26477c478bd9Sstevel@tonic-gate 		/*
26485aefb655Srie 		 * Get the data buffer for the associated symbol table and
26495aefb655Srie 		 * string table.
26507c478bd9Sstevel@tonic-gate 		 */
26515aefb655Srie 		if (stringtbl(cache, 1, cnt, shnum, file,
26525aefb655Srie 		    &symnum, &symsec, &strsec) == 0)
26537c478bd9Sstevel@tonic-gate 			continue;
26547c478bd9Sstevel@tonic-gate 
26555aefb655Srie 		syms = symsec->c_data->d_buf;
26567c478bd9Sstevel@tonic-gate 
26577c478bd9Sstevel@tonic-gate 		/*
26587c478bd9Sstevel@tonic-gate 		 * Loop through the relocation entries.
26597c478bd9Sstevel@tonic-gate 		 */
26605aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
26615aefb655Srie 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_RELOC), _cache->c_name);
26625aefb655Srie 		Elf_reloc_title(0, ELF_DBG_ELFDUMP, type);
26637c478bd9Sstevel@tonic-gate 
26645aefb655Srie 		for (relndx = 0; relndx < relnum; relndx++,
26655aefb655Srie 		    rels = (void *)((char *)rels + relsize)) {
2666ba2be530Sab196087 			Half		mach = ehdr->e_machine;
26677c478bd9Sstevel@tonic-gate 			char		section[BUFSIZ];
26685aefb655Srie 			const char	*symname;
26695aefb655Srie 			Word		symndx, reltype;
26705aefb655Srie 			Rela		*rela;
26715aefb655Srie 			Rel		*rel;
26727c478bd9Sstevel@tonic-gate 
26737c478bd9Sstevel@tonic-gate 			/*
26745aefb655Srie 			 * Unravel the relocation and determine the symbol with
26755aefb655Srie 			 * which this relocation is associated.
26767c478bd9Sstevel@tonic-gate 			 */
26777c478bd9Sstevel@tonic-gate 			if (type == SHT_RELA) {
26785aefb655Srie 				rela = (Rela *)rels;
26795aefb655Srie 				symndx = ELF_R_SYM(rela->r_info);
2680ba2be530Sab196087 				reltype = ELF_R_TYPE(rela->r_info, mach);
26817c478bd9Sstevel@tonic-gate 			} else {
26825aefb655Srie 				rel = (Rel *)rels;
26835aefb655Srie 				symndx = ELF_R_SYM(rel->r_info);
2684ba2be530Sab196087 				reltype = ELF_R_TYPE(rel->r_info, mach);
26857c478bd9Sstevel@tonic-gate 			}
26867c478bd9Sstevel@tonic-gate 
26875aefb655Srie 			symname = relsymname(cache, _cache, strsec, symndx,
26880e233487SRod Evans 			    symnum, relndx, syms, section, BUFSIZ, file);
26895aefb655Srie 
26905aefb655Srie 			/*
26915aefb655Srie 			 * A zero symbol index is only valid for a few
26925aefb655Srie 			 * relocations.
26935aefb655Srie 			 */
26945aefb655Srie 			if (symndx == 0) {
26955aefb655Srie 				int	badrel = 0;
26965aefb655Srie 
26975aefb655Srie 				if ((mach == EM_SPARC) ||
26985aefb655Srie 				    (mach == EM_SPARC32PLUS) ||
26995aefb655Srie 				    (mach == EM_SPARCV9)) {
27005aefb655Srie 					if ((reltype != R_SPARC_NONE) &&
27015aefb655Srie 					    (reltype != R_SPARC_REGISTER) &&
27025aefb655Srie 					    (reltype != R_SPARC_RELATIVE))
27035aefb655Srie 						badrel++;
27045aefb655Srie 				} else if (mach == EM_386) {
27055aefb655Srie 					if ((reltype != R_386_NONE) &&
27065aefb655Srie 					    (reltype != R_386_RELATIVE))
27075aefb655Srie 						badrel++;
27085aefb655Srie 				} else if (mach == EM_AMD64) {
27095aefb655Srie 					if ((reltype != R_AMD64_NONE) &&
27105aefb655Srie 					    (reltype != R_AMD64_RELATIVE))
27115aefb655Srie 						badrel++;
27125aefb655Srie 				}
27135aefb655Srie 
27145aefb655Srie 				if (badrel) {
27157c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
27167c478bd9Sstevel@tonic-gate 					    MSG_INTL(MSG_ERR_BADREL1), file,
2717de777a60Sab196087 					    conv_reloc_type(mach, reltype,
2718de777a60Sab196087 					    0, &inv_buf));
27195aefb655Srie 				}
27207c478bd9Sstevel@tonic-gate 			}
27217c478bd9Sstevel@tonic-gate 
27225aefb655Srie 			Elf_reloc_entry_1(0, ELF_DBG_ELFDUMP,
27235aefb655Srie 			    MSG_ORIG(MSG_STR_EMPTY), ehdr->e_machine, type,
27245aefb655Srie 			    rels, relname, symname, 0);
27257c478bd9Sstevel@tonic-gate 		}
27267c478bd9Sstevel@tonic-gate 	}
27277c478bd9Sstevel@tonic-gate }
27287c478bd9Sstevel@tonic-gate 
2729f959f76aSab196087 
2730f959f76aSab196087 /*
2731f959f76aSab196087  * This value controls which test dyn_test() performs.
2732f959f76aSab196087  */
2733f959f76aSab196087 typedef enum { DYN_TEST_ADDR, DYN_TEST_SIZE, DYN_TEST_ENTSIZE } dyn_test_t;
2734f959f76aSab196087 
2735f959f76aSab196087 /*
2736f959f76aSab196087  * Used by dynamic() to compare the value of a dynamic element against
2737f959f76aSab196087  * the starting address of the section it references.
2738f959f76aSab196087  *
2739f959f76aSab196087  * entry:
2740f959f76aSab196087  *	test_type - Specify which dyn item is being tested.
2741f959f76aSab196087  *	sh_type - SHT_* type value for required section.
2742f959f76aSab196087  *	sec_cache - Cache entry for section, or NULL if the object lacks
2743f959f76aSab196087  *		a section of this type.
2744f959f76aSab196087  *	dyn - Dyn entry to be tested
2745f959f76aSab196087  *	dynsec_cnt - # of dynamic section being examined. The first
2746f959f76aSab196087  *		dynamic section is 1, the next is 2, and so on...
2747f959f76aSab196087  *	ehdr - ELF header for file
2748f959f76aSab196087  *	file - Name of file
2749f959f76aSab196087  */
2750f959f76aSab196087 static void
2751f959f76aSab196087 dyn_test(dyn_test_t test_type, Word sh_type, Cache *sec_cache, Dyn *dyn,
27524f680cc6SAli Bahrami     Word dynsec_cnt, Ehdr *ehdr, uchar_t osabi, const char *file)
2753f959f76aSab196087 {
2754f959f76aSab196087 	Conv_inv_buf_t	buf1, buf2;
2755f959f76aSab196087 
2756f959f76aSab196087 	/*
2757f959f76aSab196087 	 * These tests are based around the implicit assumption that
2758f959f76aSab196087 	 * there is only one dynamic section in an object, and also only
2759f959f76aSab196087 	 * one of the sections it references. We have therefore gathered
2760f959f76aSab196087 	 * all of the necessary information to test this in a single pass
2761f959f76aSab196087 	 * over the section headers, which is very efficient. We are not
2762f959f76aSab196087 	 * aware of any case where more than one dynamic section would
2763f959f76aSab196087 	 * be meaningful in an ELF object, so this is a reasonable solution.
2764f959f76aSab196087 	 *
2765f959f76aSab196087 	 * To test multiple dynamic sections correctly would be more
2766f959f76aSab196087 	 * expensive in code and time. We would have to build a data structure
2767f959f76aSab196087 	 * containing all the dynamic elements. Then, we would use the address
2768f959f76aSab196087 	 * to locate the section it references and ensure the section is of
2769f959f76aSab196087 	 * the right type and that the address in the dynamic element is
2770f959f76aSab196087 	 * to the start of the section. Then, we could check the size and
2771f959f76aSab196087 	 * entsize values against those same sections. This is O(n^2), and
2772f959f76aSab196087 	 * also complicated.
2773f959f76aSab196087 	 *
2774f959f76aSab196087 	 * In the highly unlikely case that there is more than one dynamic
2775f959f76aSab196087 	 * section, we only test the first one, and simply allow the values
2776f959f76aSab196087 	 * of the subsequent one to be displayed unchallenged.
2777f959f76aSab196087 	 */
2778f959f76aSab196087 	if (dynsec_cnt != 1)
2779f959f76aSab196087 		return;
2780f959f76aSab196087 
2781f959f76aSab196087 	/*
2782f959f76aSab196087 	 * A DT_ item that references a section address should always find
2783f959f76aSab196087 	 * the section in the file.
2784f959f76aSab196087 	 */
2785f959f76aSab196087 	if (sec_cache == NULL) {
27861d9df23bSab196087 		const char *name;
27871d9df23bSab196087 
27881d9df23bSab196087 		/*
27891d9df23bSab196087 		 * Supply section names instead of section types for
27901d9df23bSab196087 		 * things that reference progbits so that the error
27911d9df23bSab196087 		 * message will make more sense.
27921d9df23bSab196087 		 */
27931d9df23bSab196087 		switch (dyn->d_tag) {
27941d9df23bSab196087 		case DT_INIT:
27951d9df23bSab196087 			name = MSG_ORIG(MSG_ELF_INIT);
27961d9df23bSab196087 			break;
27971d9df23bSab196087 		case DT_FINI:
27981d9df23bSab196087 			name = MSG_ORIG(MSG_ELF_FINI);
27991d9df23bSab196087 			break;
28001d9df23bSab196087 		default:
28014f680cc6SAli Bahrami 			name = conv_sec_type(osabi, ehdr->e_machine,
28024f680cc6SAli Bahrami 			    sh_type, 0, &buf1);
28031d9df23bSab196087 			break;
28041d9df23bSab196087 		}
2805f959f76aSab196087 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_DYNNOBCKSEC), file,
28064f680cc6SAli Bahrami 		    name, conv_dyn_tag(dyn->d_tag, osabi, ehdr->e_machine,
28073228339cSAli Bahrami 		    CONV_FMT_ALT_CF, &buf2));
2808f959f76aSab196087 		return;
2809f959f76aSab196087 	}
2810f959f76aSab196087 
2811f959f76aSab196087 
2812f959f76aSab196087 	switch (test_type) {
2813f959f76aSab196087 	case DYN_TEST_ADDR:
2814f959f76aSab196087 		/* The section address should match the DT_ item value */
2815f959f76aSab196087 		if (dyn->d_un.d_val != sec_cache->c_shdr->sh_addr)
2816f959f76aSab196087 			(void) fprintf(stderr,
2817f959f76aSab196087 			    MSG_INTL(MSG_ERR_DYNBADADDR), file,
28184f680cc6SAli Bahrami 			    conv_dyn_tag(dyn->d_tag, osabi, ehdr->e_machine,
28193228339cSAli Bahrami 			    CONV_FMT_ALT_CF, &buf1), EC_ADDR(dyn->d_un.d_val),
28204f680cc6SAli Bahrami 			    sec_cache->c_ndx, sec_cache->c_name,
2821f959f76aSab196087 			    EC_ADDR(sec_cache->c_shdr->sh_addr));
2822f959f76aSab196087 		break;
2823f959f76aSab196087 
2824f959f76aSab196087 	case DYN_TEST_SIZE:
2825f959f76aSab196087 		/* The section size should match the DT_ item value */
2826f959f76aSab196087 		if (dyn->d_un.d_val != sec_cache->c_shdr->sh_size)
2827f959f76aSab196087 			(void) fprintf(stderr,
2828f959f76aSab196087 			    MSG_INTL(MSG_ERR_DYNBADSIZE), file,
28294f680cc6SAli Bahrami 			    conv_dyn_tag(dyn->d_tag, osabi, ehdr->e_machine,
28303228339cSAli Bahrami 			    CONV_FMT_ALT_CF, &buf1), EC_XWORD(dyn->d_un.d_val),
2831f959f76aSab196087 			    sec_cache->c_ndx, sec_cache->c_name,
2832f959f76aSab196087 			    EC_XWORD(sec_cache->c_shdr->sh_size));
2833f959f76aSab196087 		break;
2834f959f76aSab196087 
2835f959f76aSab196087 	case DYN_TEST_ENTSIZE:
2836f959f76aSab196087 		/* The sh_entsize value should match the DT_ item value */
2837f959f76aSab196087 		if (dyn->d_un.d_val != sec_cache->c_shdr->sh_entsize)
2838f959f76aSab196087 			(void) fprintf(stderr,
2839f959f76aSab196087 			    MSG_INTL(MSG_ERR_DYNBADENTSIZE), file,
28404f680cc6SAli Bahrami 			    conv_dyn_tag(dyn->d_tag, osabi, ehdr->e_machine,
28413228339cSAli Bahrami 			    CONV_FMT_ALT_CF, &buf1), EC_XWORD(dyn->d_un.d_val),
2842f959f76aSab196087 			    sec_cache->c_ndx, sec_cache->c_name,
2843f959f76aSab196087 			    EC_XWORD(sec_cache->c_shdr->sh_entsize));
2844f959f76aSab196087 		break;
2845f959f76aSab196087 	}
2846f959f76aSab196087 }
2847f959f76aSab196087 
28487c478bd9Sstevel@tonic-gate /*
28491d9df23bSab196087  * There are some DT_ entries that have corresponding symbols
28501d9df23bSab196087  * (e.g. DT_INIT and _init). It is expected that these items will
28511d9df23bSab196087  * both have the same value if both are present. This routine
28521d9df23bSab196087  * examines the well known symbol tables for such symbols and
28531d9df23bSab196087  * issues warnings for any that don't match.
28541d9df23bSab196087  *
28551d9df23bSab196087  * entry:
28561d9df23bSab196087  *	dyn - Dyn entry to be tested
28571d9df23bSab196087  *	symname - Name of symbol that corresponds to dyn
28581d9df23bSab196087  *	symtab_cache, dynsym_cache, ldynsym_cache - Symbol tables to check
285931dd2c84SAli Bahrami  *	target_cache - Section the symname section is expected to be
286031dd2c84SAli Bahrami  *		associated with.
28611d9df23bSab196087  *	cache - Cache of all section headers
28621d9df23bSab196087  *	shnum - # of sections in cache
28631d9df23bSab196087  *	ehdr - ELF header for file
286431dd2c84SAli Bahrami  *	osabi - OSABI to apply when interpreting object
28651d9df23bSab196087  *	file - Name of file
28661d9df23bSab196087  */
28671d9df23bSab196087 static void
28681d9df23bSab196087 dyn_symtest(Dyn *dyn, const char *symname, Cache *symtab_cache,
286931dd2c84SAli Bahrami     Cache *dynsym_cache, Cache *ldynsym_cache, Cache *target_cache,
287031dd2c84SAli Bahrami     Cache *cache, Word shnum, Ehdr *ehdr, uchar_t osabi, const char *file)
28711d9df23bSab196087 {
28721d9df23bSab196087 	Conv_inv_buf_t	buf;
28731d9df23bSab196087 	int		i;
28741d9df23bSab196087 	Sym		*sym;
28751d9df23bSab196087 	Cache		*_cache;
28761d9df23bSab196087 
28771d9df23bSab196087 	for (i = 0; i < 3; i++) {
28781d9df23bSab196087 		switch (i) {
28791d9df23bSab196087 		case 0:
28801d9df23bSab196087 			_cache = symtab_cache;
28811d9df23bSab196087 			break;
28821d9df23bSab196087 		case 1:
28831d9df23bSab196087 			_cache = dynsym_cache;
28841d9df23bSab196087 			break;
28851d9df23bSab196087 		case 2:
28861d9df23bSab196087 			_cache = ldynsym_cache;
28871d9df23bSab196087 			break;
28881d9df23bSab196087 		}
28891d9df23bSab196087 
28901d9df23bSab196087 		if ((_cache != NULL) &&
289131dd2c84SAli Bahrami 		    symlookup(symname, cache, shnum, &sym, target_cache,
289231dd2c84SAli Bahrami 		    _cache, file) && (sym->st_value != dyn->d_un.d_val))
28931d9df23bSab196087 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_DYNSYMVAL),
28944f680cc6SAli Bahrami 			    file, _cache->c_name, conv_dyn_tag(dyn->d_tag,
28953228339cSAli Bahrami 			    osabi, ehdr->e_machine, CONV_FMT_ALT_CF, &buf),
28961d9df23bSab196087 			    symname, EC_ADDR(sym->st_value));
28971d9df23bSab196087 	}
28981d9df23bSab196087 }
28991d9df23bSab196087 
29001d9df23bSab196087 /*
29017c478bd9Sstevel@tonic-gate  * Search for and process a .dynamic section.
29027c478bd9Sstevel@tonic-gate  */
29037c478bd9Sstevel@tonic-gate static void
29044f680cc6SAli Bahrami dynamic(Cache *cache, Word shnum, Ehdr *ehdr, uchar_t osabi, const char *file)
29057c478bd9Sstevel@tonic-gate {
2906f959f76aSab196087 	struct {
29071d9df23bSab196087 		Cache	*symtab;
2908f959f76aSab196087 		Cache	*dynstr;
2909f959f76aSab196087 		Cache	*dynsym;
2910f959f76aSab196087 		Cache	*hash;
2911f959f76aSab196087 		Cache	*fini;
2912f959f76aSab196087 		Cache	*fini_array;
2913f959f76aSab196087 		Cache	*init;
2914f959f76aSab196087 		Cache	*init_array;
2915f959f76aSab196087 		Cache	*preinit_array;
2916f959f76aSab196087 		Cache	*rel;
2917f959f76aSab196087 		Cache	*rela;
2918f959f76aSab196087 		Cache	*sunw_cap;
291908278a5eSRod Evans 		Cache	*sunw_capinfo;
292008278a5eSRod Evans 		Cache	*sunw_capchain;
2921f959f76aSab196087 		Cache	*sunw_ldynsym;
2922f959f76aSab196087 		Cache	*sunw_move;
2923f959f76aSab196087 		Cache	*sunw_syminfo;
2924f959f76aSab196087 		Cache	*sunw_symsort;
2925f959f76aSab196087 		Cache	*sunw_tlssort;
2926f959f76aSab196087 		Cache	*sunw_verdef;
2927f959f76aSab196087 		Cache	*sunw_verneed;
2928f959f76aSab196087 		Cache	*sunw_versym;
2929f959f76aSab196087 	} sec;
2930f959f76aSab196087 	Word	dynsec_ndx;
2931f959f76aSab196087 	Word	dynsec_num;
2932f959f76aSab196087 	int	dynsec_cnt;
29335aefb655Srie 	Word	cnt;
29344f680cc6SAli Bahrami 	int	osabi_solaris = osabi == ELFOSABI_SOLARIS;
29357c478bd9Sstevel@tonic-gate 
2936f959f76aSab196087 	/*
2937f959f76aSab196087 	 * Make a pass over all the sections, gathering section information
2938f959f76aSab196087 	 * we'll need below.
2939f959f76aSab196087 	 */
2940f959f76aSab196087 	dynsec_num = 0;
2941f959f76aSab196087 	bzero(&sec, sizeof (sec));
29427c478bd9Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
2943f959f76aSab196087 		Cache	*_cache = &cache[cnt];
2944f959f76aSab196087 
2945f959f76aSab196087 		switch (_cache->c_shdr->sh_type) {
2946f959f76aSab196087 		case SHT_DYNAMIC:
2947f959f76aSab196087 			if (dynsec_num == 0) {
2948f959f76aSab196087 				dynsec_ndx = cnt;
2949f959f76aSab196087 
2950f959f76aSab196087 				/* Does it have a valid string table? */
2951f959f76aSab196087 				(void) stringtbl(cache, 0, cnt, shnum, file,
2952f959f76aSab196087 				    0, 0, &sec.dynstr);
2953f959f76aSab196087 			}
2954f959f76aSab196087 			dynsec_num++;
2955f959f76aSab196087 			break;
2956f959f76aSab196087 
2957f959f76aSab196087 
2958f959f76aSab196087 		case SHT_PROGBITS:
2959f959f76aSab196087 			/*
2960f959f76aSab196087 			 * We want to detect the .init and .fini sections,
2961f959f76aSab196087 			 * if present. These are SHT_PROGBITS, so all we
2962f959f76aSab196087 			 * have to go on is the section name. Normally comparing
2963f959f76aSab196087 			 * names is a bad idea, but there are some special
2964f959f76aSab196087 			 * names (i.e. .init/.fini/.interp) that are very
2965f959f76aSab196087 			 * difficult to use in any other context, and for
2966f959f76aSab196087 			 * these symbols, we do the heuristic match.
2967f959f76aSab196087 			 */
2968f959f76aSab196087 			if (strcmp(_cache->c_name,
2969f959f76aSab196087 			    MSG_ORIG(MSG_ELF_INIT)) == 0) {
2970f959f76aSab196087 				if (sec.init == NULL)
2971f959f76aSab196087 					sec.init = _cache;
2972f959f76aSab196087 			} else if (strcmp(_cache->c_name,
2973f959f76aSab196087 			    MSG_ORIG(MSG_ELF_FINI)) == 0) {
2974f959f76aSab196087 				if (sec.fini == NULL)
2975f959f76aSab196087 					sec.fini = _cache;
2976f959f76aSab196087 			}
2977f959f76aSab196087 			break;
2978f959f76aSab196087 
2979f959f76aSab196087 		case SHT_REL:
2980f959f76aSab196087 			/*
2981f959f76aSab196087 			 * We want the SHT_REL section with the lowest
2982f959f76aSab196087 			 * offset. The linker gathers them together,
2983f959f76aSab196087 			 * and puts the address of the first one
2984f959f76aSab196087 			 * into the DT_REL dynamic element.
2985f959f76aSab196087 			 */
2986f959f76aSab196087 			if ((sec.rel == NULL) ||
2987f959f76aSab196087 			    (_cache->c_shdr->sh_offset <
2988f959f76aSab196087 			    sec.rel->c_shdr->sh_offset))
2989f959f76aSab196087 				sec.rel = _cache;
2990f959f76aSab196087 			break;
2991f959f76aSab196087 
2992f959f76aSab196087 		case SHT_RELA:
2993f959f76aSab196087 			/* RELA is handled just like RELA above */
2994f959f76aSab196087 			if ((sec.rela == NULL) ||
2995f959f76aSab196087 			    (_cache->c_shdr->sh_offset <
2996f959f76aSab196087 			    sec.rela->c_shdr->sh_offset))
2997f959f76aSab196087 				sec.rela = _cache;
2998f959f76aSab196087 			break;
2999f959f76aSab196087 
3000f959f76aSab196087 		/*
3001f959f76aSab196087 		 * The GRAB macro is used for the simple case in which
3002f959f76aSab196087 		 * we simply grab the first section of the desired type.
3003f959f76aSab196087 		 */
3004f959f76aSab196087 #define	GRAB(_sec_type, _sec_field) \
3005f959f76aSab196087 		case _sec_type: \
3006f959f76aSab196087 			if (sec._sec_field == NULL) \
3007f959f76aSab196087 				sec._sec_field = _cache; \
3008f959f76aSab196087 				break
30091d9df23bSab196087 		GRAB(SHT_SYMTAB,	symtab);
3010f959f76aSab196087 		GRAB(SHT_DYNSYM,	dynsym);
3011f959f76aSab196087 		GRAB(SHT_FINI_ARRAY,	fini_array);
3012f959f76aSab196087 		GRAB(SHT_HASH,		hash);
3013f959f76aSab196087 		GRAB(SHT_INIT_ARRAY,	init_array);
3014f959f76aSab196087 		GRAB(SHT_SUNW_move,	sunw_move);
3015f959f76aSab196087 		GRAB(SHT_PREINIT_ARRAY,	preinit_array);
3016f959f76aSab196087 		GRAB(SHT_SUNW_cap,	sunw_cap);
301708278a5eSRod Evans 		GRAB(SHT_SUNW_capinfo,	sunw_capinfo);
301808278a5eSRod Evans 		GRAB(SHT_SUNW_capchain,	sunw_capchain);
3019f959f76aSab196087 		GRAB(SHT_SUNW_LDYNSYM,	sunw_ldynsym);
3020f959f76aSab196087 		GRAB(SHT_SUNW_syminfo,	sunw_syminfo);
3021f959f76aSab196087 		GRAB(SHT_SUNW_symsort,	sunw_symsort);
3022f959f76aSab196087 		GRAB(SHT_SUNW_tlssort,	sunw_tlssort);
3023f959f76aSab196087 		GRAB(SHT_SUNW_verdef,	sunw_verdef);
3024f959f76aSab196087 		GRAB(SHT_SUNW_verneed,	sunw_verneed);
3025f959f76aSab196087 		GRAB(SHT_SUNW_versym,	sunw_versym);
3026f959f76aSab196087 #undef GRAB
3027f959f76aSab196087 		}
3028f959f76aSab196087 	}
3029f959f76aSab196087 
3030f959f76aSab196087 	/*
3031f959f76aSab196087 	 * If no dynamic section, return immediately. If more than one
3032f959f76aSab196087 	 * dynamic section, then something odd is going on and an error
3033f959f76aSab196087 	 * is in order, but then continue on and display them all.
3034f959f76aSab196087 	 */
3035f959f76aSab196087 	if (dynsec_num == 0)
3036f959f76aSab196087 		return;
3037f959f76aSab196087 	if (dynsec_num > 1)
3038f959f76aSab196087 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_MULTDYN),
3039f959f76aSab196087 		    file, EC_WORD(dynsec_num));
3040f959f76aSab196087 
3041f959f76aSab196087 
3042f959f76aSab196087 	dynsec_cnt = 0;
3043f959f76aSab196087 	for (cnt = dynsec_ndx; (cnt < shnum) && (dynsec_cnt < dynsec_num);
3044f959f76aSab196087 	    cnt++) {
30455aefb655Srie 		Dyn	*dyn;
30467c478bd9Sstevel@tonic-gate 		ulong_t	numdyn;
30473244bcaaSab196087 		int	ndx, end_ndx;
30485aefb655Srie 		Cache	*_cache = &cache[cnt], *strsec;
30495aefb655Srie 		Shdr	*shdr = _cache->c_shdr;
3050f959f76aSab196087 		int	dumped = 0;
30517c478bd9Sstevel@tonic-gate 
30527c478bd9Sstevel@tonic-gate 		if (shdr->sh_type != SHT_DYNAMIC)
30537c478bd9Sstevel@tonic-gate 			continue;
3054f959f76aSab196087 		dynsec_cnt++;
30557c478bd9Sstevel@tonic-gate 
30567c478bd9Sstevel@tonic-gate 		/*
30575aefb655Srie 		 * Verify the associated string table section.
30587c478bd9Sstevel@tonic-gate 		 */
30595aefb655Srie 		if (stringtbl(cache, 0, cnt, shnum, file, 0, 0, &strsec) == 0)
30607c478bd9Sstevel@tonic-gate 			continue;
30615aefb655Srie 
30629a411307Srie 		if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
30639a411307Srie 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
30649a411307Srie 			    file, _cache->c_name);
30659a411307Srie 			continue;
30669a411307Srie 		}
30679a411307Srie 		if (_cache->c_data == NULL)
30689a411307Srie 			continue;
30699a411307Srie 
30707c478bd9Sstevel@tonic-gate 		numdyn = shdr->sh_size / shdr->sh_entsize;
30715aefb655Srie 		dyn = (Dyn *)_cache->c_data->d_buf;
30727c478bd9Sstevel@tonic-gate 
3073f959f76aSab196087 		/*
3074f959f76aSab196087 		 * We expect the REL/RELA entries to reference the reloc
3075f959f76aSab196087 		 * section with the lowest address. However, this is
3076f959f76aSab196087 		 * not true for dumped objects. Detect if this object has
3077f959f76aSab196087 		 * been dumped so that we can skip the reloc address test
3078f959f76aSab196087 		 * in that case.
3079f959f76aSab196087 		 */
3080f959f76aSab196087 		for (ndx = 0; ndx < numdyn; dyn++, ndx++) {
3081f959f76aSab196087 			if (dyn->d_tag == DT_FLAGS_1) {
3082f959f76aSab196087 				dumped = (dyn->d_un.d_val & DF_1_CONFALT) != 0;
3083f959f76aSab196087 				break;
3084f959f76aSab196087 			}
3085f959f76aSab196087 		}
3086f959f76aSab196087 		dyn = (Dyn *)_cache->c_data->d_buf;
3087f959f76aSab196087 
30885aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
30895aefb655Srie 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_DYNAMIC), _cache->c_name);
30907c478bd9Sstevel@tonic-gate 
30915aefb655Srie 		Elf_dyn_title(0);
30927c478bd9Sstevel@tonic-gate 
30935aefb655Srie 		for (ndx = 0; ndx < numdyn; dyn++, ndx++) {
3094de777a60Sab196087 			union {
3095ba2be530Sab196087 				Conv_inv_buf_t		inv;
3096de777a60Sab196087 				Conv_dyn_flag_buf_t	flag;
3097de777a60Sab196087 				Conv_dyn_flag1_buf_t	flag1;
3098de777a60Sab196087 				Conv_dyn_posflag1_buf_t	posflag1;
3099de777a60Sab196087 				Conv_dyn_feature1_buf_t	feature1;
3100de777a60Sab196087 			} c_buf;
3101f959f76aSab196087 			const char	*name = NULL;
31027c478bd9Sstevel@tonic-gate 
31037c478bd9Sstevel@tonic-gate 			/*
31047c478bd9Sstevel@tonic-gate 			 * Print the information numerically, and if possible
3105f959f76aSab196087 			 * as a string. If a string is available, name is
3106f959f76aSab196087 			 * set to reference it.
3107f959f76aSab196087 			 *
3108f959f76aSab196087 			 * Also, take this opportunity to sanity check
3109f959f76aSab196087 			 * the values of DT elements. In the code above,
3110f959f76aSab196087 			 * we gathered information on sections that are
3111f959f76aSab196087 			 * referenced by the dynamic section. Here, we
3112f959f76aSab196087 			 * compare the attributes of those sections to
3113f959f76aSab196087 			 * the DT_ items that reference them and report
3114f959f76aSab196087 			 * on inconsistencies.
3115f959f76aSab196087 			 *
3116f959f76aSab196087 			 * Things not currently tested that could be improved
3117f959f76aSab196087 			 * in later revisions include:
3118f959f76aSab196087 			 *	- We don't check PLT or GOT related items
3119f959f76aSab196087 			 *	- We don't handle computing the lengths of
3120f959f76aSab196087 			 *		relocation arrays. To handle this
3121f959f76aSab196087 			 *		requires examining data that spans
3122f959f76aSab196087 			 *		across sections, in a contiguous span
3123f959f76aSab196087 			 *		within a single segment.
3124f959f76aSab196087 			 *	- DT_VERDEFNUM and DT_VERNEEDNUM can't be
3125f959f76aSab196087 			 *		verified without parsing the sections.
3126f959f76aSab196087 			 *	- We don't handle DT_SUNW_SYMSZ, which would
3127f959f76aSab196087 			 *		be the sum of the lengths of .dynsym and
3128f959f76aSab196087 			 *		.SUNW_ldynsym
3129f959f76aSab196087 			 *	- DT_SUNW_STRPAD can't be verified other than
3130f959f76aSab196087 			 *		to check that it's not larger than
3131f959f76aSab196087 			 *		the string table.
3132f959f76aSab196087 			 *	- Some items come in "all or none" clusters
3133f959f76aSab196087 			 *		that give an address, element size,
3134f959f76aSab196087 			 *		and data length in bytes. We don't
3135f959f76aSab196087 			 *		verify that there are no missing items
3136f959f76aSab196087 			 *		in such groups.
31377c478bd9Sstevel@tonic-gate 			 */
31383244bcaaSab196087 			switch (dyn->d_tag) {
31393244bcaaSab196087 			case DT_NULL:
31403244bcaaSab196087 				/*
31413244bcaaSab196087 				 * Special case: DT_NULLs can come in groups
31423244bcaaSab196087 				 * that we prefer to reduce to a single line.
31433244bcaaSab196087 				 */
31443244bcaaSab196087 				end_ndx = ndx;
31453244bcaaSab196087 				while ((end_ndx < (numdyn - 1)) &&
31463244bcaaSab196087 				    ((dyn + 1)->d_tag == DT_NULL)) {
31473244bcaaSab196087 					dyn++;
31483244bcaaSab196087 					end_ndx++;
31493244bcaaSab196087 				}
31503244bcaaSab196087 				Elf_dyn_null_entry(0, dyn, ndx, end_ndx);
31513244bcaaSab196087 				ndx = end_ndx;
31523244bcaaSab196087 				continue;
31533244bcaaSab196087 
31543244bcaaSab196087 			/*
3155f959f76aSab196087 			 * String items all reference the dynstr. The string()
3156f959f76aSab196087 			 * function does the necessary sanity checking.
31573244bcaaSab196087 			 */
31583244bcaaSab196087 			case DT_NEEDED:
31593244bcaaSab196087 			case DT_SONAME:
31603244bcaaSab196087 			case DT_FILTER:
31613244bcaaSab196087 			case DT_AUXILIARY:
31623244bcaaSab196087 			case DT_CONFIG:
31633244bcaaSab196087 			case DT_RPATH:
31643244bcaaSab196087 			case DT_RUNPATH:
31653244bcaaSab196087 			case DT_USED:
31663244bcaaSab196087 			case DT_DEPAUDIT:
31673244bcaaSab196087 			case DT_AUDIT:
31684f680cc6SAli Bahrami 				name = string(_cache, ndx, strsec,
31694f680cc6SAli Bahrami 				    file, dyn->d_un.d_ptr);
31704f680cc6SAli Bahrami 				break;
31714f680cc6SAli Bahrami 
31723244bcaaSab196087 			case DT_SUNW_AUXILIARY:
31733244bcaaSab196087 			case DT_SUNW_FILTER:
31744f680cc6SAli Bahrami 				if (osabi_solaris)
31755aefb655Srie 					name = string(_cache, ndx, strsec,
31765aefb655Srie 					    file, dyn->d_un.d_ptr);
31773244bcaaSab196087 				break;
31783244bcaaSab196087 
31793244bcaaSab196087 			case DT_FLAGS:
3180de777a60Sab196087 				name = conv_dyn_flag(dyn->d_un.d_val,
3181de777a60Sab196087 				    0, &c_buf.flag);
31823244bcaaSab196087 				break;
31833244bcaaSab196087 			case DT_FLAGS_1:
3184d29b2c44Sab196087 				name = conv_dyn_flag1(dyn->d_un.d_val, 0,
3185de777a60Sab196087 				    &c_buf.flag1);
31863244bcaaSab196087 				break;
31873244bcaaSab196087 			case DT_POSFLAG_1:
3188de777a60Sab196087 				name = conv_dyn_posflag1(dyn->d_un.d_val, 0,
3189de777a60Sab196087 				    &c_buf.posflag1);
31903244bcaaSab196087 				break;
31913244bcaaSab196087 			case DT_FEATURE_1:
3192de777a60Sab196087 				name = conv_dyn_feature1(dyn->d_un.d_val, 0,
3193de777a60Sab196087 				    &c_buf.feature1);
31943244bcaaSab196087 				break;
31953244bcaaSab196087 			case DT_DEPRECATED_SPARC_REGISTER:
31967c478bd9Sstevel@tonic-gate 				name = MSG_INTL(MSG_STR_DEPRECATED);
31973244bcaaSab196087 				break;
3198f959f76aSab196087 
3199ba2be530Sab196087 			case DT_SUNW_LDMACH:
32004f680cc6SAli Bahrami 				if (!osabi_solaris)
32014f680cc6SAli Bahrami 					break;
32024f680cc6SAli Bahrami 				name = conv_ehdr_mach((Half)dyn->d_un.d_val,
32034f680cc6SAli Bahrami 				    0, &c_buf.inv);
3204ba2be530Sab196087 				break;
3205ba2be530Sab196087 
3206f959f76aSab196087 			/*
3207f959f76aSab196087 			 * Cases below this point are strictly sanity checking,
3208f959f76aSab196087 			 * and do not generate a name string. The TEST_ macros
3209f959f76aSab196087 			 * are used to hide the boiler plate arguments neeeded
3210f959f76aSab196087 			 * by dyn_test().
3211f959f76aSab196087 			 */
3212f959f76aSab196087 #define	TEST_ADDR(_sh_type, _sec_field) \
3213f959f76aSab196087 				dyn_test(DYN_TEST_ADDR, _sh_type, \
32144f680cc6SAli Bahrami 				    sec._sec_field, dyn, dynsec_cnt, ehdr, \
32154f680cc6SAli Bahrami 				    osabi, file)
3216f959f76aSab196087 #define	TEST_SIZE(_sh_type, _sec_field) \
3217f959f76aSab196087 				dyn_test(DYN_TEST_SIZE, _sh_type, \
32184f680cc6SAli Bahrami 				    sec._sec_field, dyn, dynsec_cnt, ehdr, \
32194f680cc6SAli Bahrami 				    osabi, file)
3220f959f76aSab196087 #define	TEST_ENTSIZE(_sh_type, _sec_field) \
3221f959f76aSab196087 				dyn_test(DYN_TEST_ENTSIZE, _sh_type, \
32224f680cc6SAli Bahrami 				    sec._sec_field, dyn, dynsec_cnt, ehdr, \
32234f680cc6SAli Bahrami 				    osabi, file)
3224f959f76aSab196087 
3225f959f76aSab196087 			case DT_FINI:
32261d9df23bSab196087 				dyn_symtest(dyn, MSG_ORIG(MSG_SYM_FINI),
32271d9df23bSab196087 				    sec.symtab, sec.dynsym, sec.sunw_ldynsym,
322831dd2c84SAli Bahrami 				    sec.fini, cache, shnum, ehdr, osabi, file);
3229f959f76aSab196087 				TEST_ADDR(SHT_PROGBITS, fini);
32303244bcaaSab196087 				break;
3231f959f76aSab196087 
3232f959f76aSab196087 			case DT_FINI_ARRAY:
3233f959f76aSab196087 				TEST_ADDR(SHT_FINI_ARRAY, fini_array);
3234f959f76aSab196087 				break;
3235f959f76aSab196087 
3236f959f76aSab196087 			case DT_FINI_ARRAYSZ:
3237f959f76aSab196087 				TEST_SIZE(SHT_FINI_ARRAY, fini_array);
3238f959f76aSab196087 				break;
3239f959f76aSab196087 
3240f959f76aSab196087 			case DT_HASH:
3241f959f76aSab196087 				TEST_ADDR(SHT_HASH, hash);
3242f959f76aSab196087 				break;
3243f959f76aSab196087 
3244f959f76aSab196087 			case DT_INIT:
32451d9df23bSab196087 				dyn_symtest(dyn, MSG_ORIG(MSG_SYM_INIT),
32461d9df23bSab196087 				    sec.symtab, sec.dynsym, sec.sunw_ldynsym,
324731dd2c84SAli Bahrami 				    sec.init, cache, shnum, ehdr, osabi, file);
3248f959f76aSab196087 				TEST_ADDR(SHT_PROGBITS, init);
3249f959f76aSab196087 				break;
3250f959f76aSab196087 
3251f959f76aSab196087 			case DT_INIT_ARRAY:
3252f959f76aSab196087 				TEST_ADDR(SHT_INIT_ARRAY, init_array);
3253f959f76aSab196087 				break;
3254f959f76aSab196087 
3255f959f76aSab196087 			case DT_INIT_ARRAYSZ:
3256f959f76aSab196087 				TEST_SIZE(SHT_INIT_ARRAY, init_array);
3257f959f76aSab196087 				break;
3258f959f76aSab196087 
3259f959f76aSab196087 			case DT_MOVEENT:
3260f959f76aSab196087 				TEST_ENTSIZE(SHT_SUNW_move, sunw_move);
3261f959f76aSab196087 				break;
3262f959f76aSab196087 
3263f959f76aSab196087 			case DT_MOVESZ:
3264f959f76aSab196087 				TEST_SIZE(SHT_SUNW_move, sunw_move);
3265f959f76aSab196087 				break;
3266f959f76aSab196087 
3267f959f76aSab196087 			case DT_MOVETAB:
3268f959f76aSab196087 				TEST_ADDR(SHT_SUNW_move, sunw_move);
3269f959f76aSab196087 				break;
3270f959f76aSab196087 
3271f959f76aSab196087 			case DT_PREINIT_ARRAY:
3272f959f76aSab196087 				TEST_ADDR(SHT_PREINIT_ARRAY, preinit_array);
3273f959f76aSab196087 				break;
3274f959f76aSab196087 
3275f959f76aSab196087 			case DT_PREINIT_ARRAYSZ:
3276f959f76aSab196087 				TEST_SIZE(SHT_PREINIT_ARRAY, preinit_array);
3277f959f76aSab196087 				break;
3278f959f76aSab196087 
3279f959f76aSab196087 			case DT_REL:
3280f959f76aSab196087 				if (!dumped)
3281f959f76aSab196087 					TEST_ADDR(SHT_REL, rel);
3282f959f76aSab196087 				break;
3283f959f76aSab196087 
3284f959f76aSab196087 			case DT_RELENT:
3285f959f76aSab196087 				TEST_ENTSIZE(SHT_REL, rel);
3286f959f76aSab196087 				break;
3287f959f76aSab196087 
3288f959f76aSab196087 			case DT_RELA:
3289f959f76aSab196087 				if (!dumped)
3290f959f76aSab196087 					TEST_ADDR(SHT_RELA, rela);
3291f959f76aSab196087 				break;
3292f959f76aSab196087 
3293f959f76aSab196087 			case DT_RELAENT:
3294f959f76aSab196087 				TEST_ENTSIZE(SHT_RELA, rela);
3295f959f76aSab196087 				break;
3296f959f76aSab196087 
3297f959f76aSab196087 			case DT_STRTAB:
3298f959f76aSab196087 				TEST_ADDR(SHT_STRTAB, dynstr);
3299f959f76aSab196087 				break;
3300f959f76aSab196087 
3301f959f76aSab196087 			case DT_STRSZ:
3302f959f76aSab196087 				TEST_SIZE(SHT_STRTAB, dynstr);
3303f959f76aSab196087 				break;
3304f959f76aSab196087 
3305f959f76aSab196087 			case DT_SUNW_CAP:
33063228339cSAli Bahrami 				if (osabi_solaris)
3307f959f76aSab196087 					TEST_ADDR(SHT_SUNW_cap, sunw_cap);
3308f959f76aSab196087 				break;
3309f959f76aSab196087 
331008278a5eSRod Evans 			case DT_SUNW_CAPINFO:
33113228339cSAli Bahrami 				if (osabi_solaris)
33123228339cSAli Bahrami 					TEST_ADDR(SHT_SUNW_capinfo,
33133228339cSAli Bahrami 					    sunw_capinfo);
331408278a5eSRod Evans 				break;
331508278a5eSRod Evans 
331608278a5eSRod Evans 			case DT_SUNW_CAPCHAIN:
33173228339cSAli Bahrami 				if (osabi_solaris)
33183228339cSAli Bahrami 					TEST_ADDR(SHT_SUNW_capchain,
33193228339cSAli Bahrami 					    sunw_capchain);
332008278a5eSRod Evans 				break;
332108278a5eSRod Evans 
3322f959f76aSab196087 			case DT_SUNW_SYMTAB:
3323f959f76aSab196087 				TEST_ADDR(SHT_SUNW_LDYNSYM, sunw_ldynsym);
3324f959f76aSab196087 				break;
3325f959f76aSab196087 
3326f959f76aSab196087 			case DT_SYMENT:
3327f959f76aSab196087 				TEST_ENTSIZE(SHT_DYNSYM, dynsym);
3328f959f76aSab196087 				break;
3329f959f76aSab196087 
3330f959f76aSab196087 			case DT_SYMINENT:
3331f959f76aSab196087 				TEST_ENTSIZE(SHT_SUNW_syminfo, sunw_syminfo);
3332f959f76aSab196087 				break;
3333f959f76aSab196087 
3334f959f76aSab196087 			case DT_SYMINFO:
3335f959f76aSab196087 				TEST_ADDR(SHT_SUNW_syminfo, sunw_syminfo);
3336f959f76aSab196087 				break;
3337f959f76aSab196087 
3338f959f76aSab196087 			case DT_SYMINSZ:
3339f959f76aSab196087 				TEST_SIZE(SHT_SUNW_syminfo, sunw_syminfo);
3340f959f76aSab196087 				break;
3341f959f76aSab196087 
3342f959f76aSab196087 			case DT_SYMTAB:
3343f959f76aSab196087 				TEST_ADDR(SHT_DYNSYM, dynsym);
3344f959f76aSab196087 				break;
3345f959f76aSab196087 
3346f959f76aSab196087 			case DT_SUNW_SORTENT:
3347f959f76aSab196087 				/*
3348f959f76aSab196087 				 * This entry is related to both the symsort and
3349f959f76aSab196087 				 * tlssort sections.
3350f959f76aSab196087 				 */
33514f680cc6SAli Bahrami 				if (osabi_solaris) {
3352f959f76aSab196087 					int test_tls =
3353f959f76aSab196087 					    (sec.sunw_tlssort != NULL);
3354f959f76aSab196087 					int test_sym =
3355f959f76aSab196087 					    (sec.sunw_symsort != NULL) ||
3356f959f76aSab196087 					    !test_tls;
3357f959f76aSab196087 					if (test_sym)
3358f959f76aSab196087 						TEST_ENTSIZE(SHT_SUNW_symsort,
3359f959f76aSab196087 						    sunw_symsort);
3360f959f76aSab196087 					if (test_tls)
3361f959f76aSab196087 						TEST_ENTSIZE(SHT_SUNW_tlssort,
3362f959f76aSab196087 						    sunw_tlssort);
3363f959f76aSab196087 				}
3364f959f76aSab196087 				break;
3365f959f76aSab196087 
3366f959f76aSab196087 
3367f959f76aSab196087 			case DT_SUNW_SYMSORT:
33684f680cc6SAli Bahrami 				if (osabi_solaris)
33694f680cc6SAli Bahrami 					TEST_ADDR(SHT_SUNW_symsort,
33704f680cc6SAli Bahrami 					    sunw_symsort);
3371f959f76aSab196087 				break;
3372f959f76aSab196087 
3373f959f76aSab196087 			case DT_SUNW_SYMSORTSZ:
33744f680cc6SAli Bahrami 				if (osabi_solaris)
33754f680cc6SAli Bahrami 					TEST_SIZE(SHT_SUNW_symsort,
33764f680cc6SAli Bahrami 					    sunw_symsort);
3377f959f76aSab196087 				break;
3378f959f76aSab196087 
3379f959f76aSab196087 			case DT_SUNW_TLSSORT:
33804f680cc6SAli Bahrami 				if (osabi_solaris)
33814f680cc6SAli Bahrami 					TEST_ADDR(SHT_SUNW_tlssort,
33824f680cc6SAli Bahrami 					    sunw_tlssort);
3383f959f76aSab196087 				break;
3384f959f76aSab196087 
3385f959f76aSab196087 			case DT_SUNW_TLSSORTSZ:
33864f680cc6SAli Bahrami 				if (osabi_solaris)
33874f680cc6SAli Bahrami 					TEST_SIZE(SHT_SUNW_tlssort,
33884f680cc6SAli Bahrami 					    sunw_tlssort);
3389f959f76aSab196087 				break;
3390f959f76aSab196087 
3391f959f76aSab196087 			case DT_VERDEF:
3392f959f76aSab196087 				TEST_ADDR(SHT_SUNW_verdef, sunw_verdef);
3393f959f76aSab196087 				break;
3394f959f76aSab196087 
3395f959f76aSab196087 			case DT_VERNEED:
3396f959f76aSab196087 				TEST_ADDR(SHT_SUNW_verneed, sunw_verneed);
3397f959f76aSab196087 				break;
3398f959f76aSab196087 
3399f959f76aSab196087 			case DT_VERSYM:
3400f959f76aSab196087 				TEST_ADDR(SHT_SUNW_versym, sunw_versym);
3401f959f76aSab196087 				break;
3402f959f76aSab196087 #undef TEST_ADDR
3403f959f76aSab196087 #undef TEST_SIZE
3404f959f76aSab196087 #undef TEST_ENTSIZE
34053244bcaaSab196087 			}
34067c478bd9Sstevel@tonic-gate 
3407f959f76aSab196087 			if (name == NULL)
3408f959f76aSab196087 				name = MSG_ORIG(MSG_STR_EMPTY);
34094f680cc6SAli Bahrami 			Elf_dyn_entry(0, dyn, ndx, name,
34104f680cc6SAli Bahrami 			    osabi, ehdr->e_machine);
34117c478bd9Sstevel@tonic-gate 		}
34127c478bd9Sstevel@tonic-gate 	}
34137c478bd9Sstevel@tonic-gate }
34147c478bd9Sstevel@tonic-gate 
34157c478bd9Sstevel@tonic-gate /*
34167c478bd9Sstevel@tonic-gate  * Search for and process a MOVE section.
34177c478bd9Sstevel@tonic-gate  */
34187c478bd9Sstevel@tonic-gate static void
3419c809c407Sab196087 move(Cache *cache, Word shnum, const char *file, uint_t flags)
34207c478bd9Sstevel@tonic-gate {
34215aefb655Srie 	Word		cnt;
34227e16fca0SAli Bahrami 	const char	*fmt = NULL;
34237c478bd9Sstevel@tonic-gate 
34247c478bd9Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
34255aefb655Srie 		Word	movenum, symnum, ndx;
34265aefb655Srie 		Sym	*syms;
34277c478bd9Sstevel@tonic-gate 		Cache	*_cache = &cache[cnt];
34285aefb655Srie 		Shdr	*shdr = _cache->c_shdr;
34295aefb655Srie 		Cache	*symsec, *strsec;
34305aefb655Srie 		Move	*move;
34317c478bd9Sstevel@tonic-gate 
34327c478bd9Sstevel@tonic-gate 		if (shdr->sh_type != SHT_SUNW_move)
34337c478bd9Sstevel@tonic-gate 			continue;
3434981a172dSab196087 		if (!match(MATCH_F_ALL, _cache->c_name, cnt, shdr->sh_type))
34357c478bd9Sstevel@tonic-gate 			continue;
34367c478bd9Sstevel@tonic-gate 
34377c478bd9Sstevel@tonic-gate 		/*
34387c478bd9Sstevel@tonic-gate 		 * Determine the move data and number.
34397c478bd9Sstevel@tonic-gate 		 */
34407c478bd9Sstevel@tonic-gate 		if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
34417c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
34427c478bd9Sstevel@tonic-gate 			    file, _cache->c_name);
34437c478bd9Sstevel@tonic-gate 			continue;
34447c478bd9Sstevel@tonic-gate 		}
34459a411307Srie 		if (_cache->c_data == NULL)
34469a411307Srie 			continue;
34479a411307Srie 
34485aefb655Srie 		move = (Move *)_cache->c_data->d_buf;
34495aefb655Srie 		movenum = shdr->sh_size / shdr->sh_entsize;
34507c478bd9Sstevel@tonic-gate 
34517c478bd9Sstevel@tonic-gate 		/*
34525aefb655Srie 		 * Get the data buffer for the associated symbol table and
34535aefb655Srie 		 * string table.
34547c478bd9Sstevel@tonic-gate 		 */
34555aefb655Srie 		if (stringtbl(cache, 1, cnt, shnum, file,
34565aefb655Srie 		    &symnum, &symsec, &strsec) == 0)
34575aefb655Srie 			return;
34587c478bd9Sstevel@tonic-gate 
34595aefb655Srie 		syms = (Sym *)symsec->c_data->d_buf;
34607c478bd9Sstevel@tonic-gate 
34615aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
34625aefb655Srie 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_MOVE), _cache->c_name);
34635aefb655Srie 		dbg_print(0, MSG_INTL(MSG_MOVE_TITLE));
34647c478bd9Sstevel@tonic-gate 
34657e16fca0SAli Bahrami 		if (fmt == NULL)
34665aefb655Srie 			fmt = MSG_INTL(MSG_MOVE_ENTRY);
34677c478bd9Sstevel@tonic-gate 
34685aefb655Srie 		for (ndx = 0; ndx < movenum; move++, ndx++) {
34695aefb655Srie 			const char	*symname;
34705aefb655Srie 			char		index[MAXNDXSIZE], section[BUFSIZ];
34715aefb655Srie 			Word		symndx, shndx;
34725aefb655Srie 			Sym		*sym;
34737c478bd9Sstevel@tonic-gate 
34747c478bd9Sstevel@tonic-gate 			/*
34757c478bd9Sstevel@tonic-gate 			 * Check for null entries
34767c478bd9Sstevel@tonic-gate 			 */
34775aefb655Srie 			if ((move->m_info == 0) && (move->m_value == 0) &&
34785aefb655Srie 			    (move->m_poffset == 0) && (move->m_repeat == 0) &&
34795aefb655Srie 			    (move->m_stride == 0)) {
34805aefb655Srie 				dbg_print(0, fmt, MSG_ORIG(MSG_STR_EMPTY),
34815aefb655Srie 				    EC_XWORD(move->m_poffset), 0, 0, 0,
34825aefb655Srie 				    EC_LWORD(0), MSG_ORIG(MSG_STR_EMPTY));
34837c478bd9Sstevel@tonic-gate 				continue;
34847c478bd9Sstevel@tonic-gate 			}
34855aefb655Srie 			if (((symndx = ELF_M_SYM(move->m_info)) == 0) ||
34865aefb655Srie 			    (symndx >= symnum)) {
34877c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
34887c478bd9Sstevel@tonic-gate 				    MSG_INTL(MSG_ERR_BADMINFO), file,
34895aefb655Srie 				    _cache->c_name, EC_XWORD(move->m_info));
34905aefb655Srie 
34915aefb655Srie 				(void) snprintf(index, MAXNDXSIZE,
34925aefb655Srie 				    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(symndx));
34935aefb655Srie 				dbg_print(0, fmt, index,
34945aefb655Srie 				    EC_XWORD(move->m_poffset),
34955aefb655Srie 				    ELF_M_SIZE(move->m_info), move->m_repeat,
34965aefb655Srie 				    move->m_stride, move->m_value,
34977c478bd9Sstevel@tonic-gate 				    MSG_INTL(MSG_STR_UNKNOWN));
34987c478bd9Sstevel@tonic-gate 				continue;
34997c478bd9Sstevel@tonic-gate 			}
35007c478bd9Sstevel@tonic-gate 
35015aefb655Srie 			symname = relsymname(cache, _cache, strsec,
35020e233487SRod Evans 			    symndx, symnum, ndx, syms, section, BUFSIZ, file);
35035aefb655Srie 			sym = (Sym *)(syms + symndx);
35047c478bd9Sstevel@tonic-gate 
35057c478bd9Sstevel@tonic-gate 			/*
35067c478bd9Sstevel@tonic-gate 			 * Additional sanity check.
35077c478bd9Sstevel@tonic-gate 			 */
35085aefb655Srie 			shndx = sym->st_shndx;
35097c478bd9Sstevel@tonic-gate 			if (!((shndx == SHN_COMMON) ||
35107c478bd9Sstevel@tonic-gate 			    (((shndx >= 1) && (shndx <= shnum)) &&
35115aefb655Srie 			    (cache[shndx].c_shdr)->sh_type == SHT_NOBITS))) {
35127c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
35137c478bd9Sstevel@tonic-gate 				    MSG_INTL(MSG_ERR_BADSYM2), file,
3514ba2be530Sab196087 				    _cache->c_name, EC_WORD(symndx),
3515ba2be530Sab196087 				    demangle(symname, flags));
35167c478bd9Sstevel@tonic-gate 			}
35177c478bd9Sstevel@tonic-gate 
35185aefb655Srie 			(void) snprintf(index, MAXNDXSIZE,
35195aefb655Srie 			    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(symndx));
35205aefb655Srie 			dbg_print(0, fmt, index, EC_XWORD(move->m_poffset),
35215aefb655Srie 			    ELF_M_SIZE(move->m_info), move->m_repeat,
35225aefb655Srie 			    move->m_stride, move->m_value,
35235aefb655Srie 			    demangle(symname, flags));
35247c478bd9Sstevel@tonic-gate 		}
35257c478bd9Sstevel@tonic-gate 	}
35267c478bd9Sstevel@tonic-gate }
35277c478bd9Sstevel@tonic-gate 
35287c478bd9Sstevel@tonic-gate /*
35294f680cc6SAli Bahrami  * parse_note_t is used to track the state used by parse_note_entry()
35304f680cc6SAli Bahrami  * between calls, and also to return the results of each call.
35314f680cc6SAli Bahrami  */
35324f680cc6SAli Bahrami typedef struct {
35334f680cc6SAli Bahrami 	/* pns_ fields track progress through the data */
35344f680cc6SAli Bahrami 	const char	*pns_file;	/* File name */
35354f680cc6SAli Bahrami 	Cache		*pns_cache;	/* Note section cache entry */
35364f680cc6SAli Bahrami 	size_t		pns_size;	/* # unprocessed data bytes */
35374f680cc6SAli Bahrami 	Word		*pns_data;	/* # to next unused data byte */
35384f680cc6SAli Bahrami 
35394f680cc6SAli Bahrami 	/* pn_ fields return the results for a single call */
35404f680cc6SAli Bahrami 	Word		pn_namesz;	/* Value of note namesz field */
35414f680cc6SAli Bahrami 	Word		pn_descsz;	/* Value of note descsz field */
35424f680cc6SAli Bahrami 	Word		pn_type;	/* Value of note type field */
35434f680cc6SAli Bahrami 	const char	*pn_name;	/* if (namesz > 0) ptr to name bytes */
35444f680cc6SAli Bahrami 	const char	*pn_desc;	/* if (descsx > 0) ptr to data bytes */
35454f680cc6SAli Bahrami } parse_note_t;
35464f680cc6SAli Bahrami 
35474f680cc6SAli Bahrami /*
35484f680cc6SAli Bahrami  * Extract the various sub-parts of a note entry, and advance the
35494f680cc6SAli Bahrami  * data pointer past it.
35504f680cc6SAli Bahrami  *
35514f680cc6SAli Bahrami  * entry:
35524f680cc6SAli Bahrami  *	The state pns_ fields contain current values for the Note section
35534f680cc6SAli Bahrami  *
35544f680cc6SAli Bahrami  * exit:
35554f680cc6SAli Bahrami  *	On success, True (1) is returned, the state pns_ fields have been
35564f680cc6SAli Bahrami  *	advanced to point at the start of the next entry, and the information
35574f680cc6SAli Bahrami  *	for the recovered note entry is found in the state pn_ fields.
35584f680cc6SAli Bahrami  *
35594f680cc6SAli Bahrami  *	On failure, False (0) is returned. The values contained in state
35604f680cc6SAli Bahrami  *	are undefined.
35614f680cc6SAli Bahrami  */
35624f680cc6SAli Bahrami static int
35634f680cc6SAli Bahrami parse_note_entry(parse_note_t *state)
35644f680cc6SAli Bahrami {
35654f680cc6SAli Bahrami 	size_t	pad, noteoff;
35664f680cc6SAli Bahrami 
35674f680cc6SAli Bahrami 	noteoff = (Word)state->pns_cache->c_data->d_size - state->pns_size;
35684f680cc6SAli Bahrami 	/*
35694f680cc6SAli Bahrami 	 * Make sure we can at least reference the 3 initial entries
35704f680cc6SAli Bahrami 	 * (4-byte words) of the note information block.
35714f680cc6SAli Bahrami 	 */
35724f680cc6SAli Bahrami 	if (state->pns_size >= (sizeof (Word) * 3)) {
35734f680cc6SAli Bahrami 		state->pns_size -= (sizeof (Word) * 3);
35744f680cc6SAli Bahrami 	} else {
35754f680cc6SAli Bahrami 		(void) fprintf(stderr, MSG_INTL(MSG_NOTE_BADDATASZ),
35764f680cc6SAli Bahrami 		    state->pns_file, state->pns_cache->c_name,
35774f680cc6SAli Bahrami 		    EC_WORD(noteoff));
35784f680cc6SAli Bahrami 		return (0);
35794f680cc6SAli Bahrami 	}
35804f680cc6SAli Bahrami 
35814f680cc6SAli Bahrami 	/*
35824f680cc6SAli Bahrami 	 * Make sure any specified name string can be referenced.
35834f680cc6SAli Bahrami 	 */
35844f680cc6SAli Bahrami 	if ((state->pn_namesz = *state->pns_data++) != 0) {
35854f680cc6SAli Bahrami 		if (state->pns_size >= state->pn_namesz) {
35864f680cc6SAli Bahrami 			state->pns_size -= state->pn_namesz;
35874f680cc6SAli Bahrami 		} else {
35884f680cc6SAli Bahrami 			(void) fprintf(stderr, MSG_INTL(MSG_NOTE_BADNMSZ),
35894f680cc6SAli Bahrami 			    state->pns_file, state->pns_cache->c_name,
35904f680cc6SAli Bahrami 			    EC_WORD(noteoff), EC_WORD(state->pn_namesz));
35914f680cc6SAli Bahrami 			return (0);
35924f680cc6SAli Bahrami 		}
35934f680cc6SAli Bahrami 	}
35944f680cc6SAli Bahrami 
35954f680cc6SAli Bahrami 	/*
35964f680cc6SAli Bahrami 	 * Make sure any specified descriptor can be referenced.
35974f680cc6SAli Bahrami 	 */
35984f680cc6SAli Bahrami 	if ((state->pn_descsz = *state->pns_data++) != 0) {
35994f680cc6SAli Bahrami 		/*
36004f680cc6SAli Bahrami 		 * If namesz isn't a 4-byte multiple, account for any
36014f680cc6SAli Bahrami 		 * padding that must exist before the descriptor.
36024f680cc6SAli Bahrami 		 */
36034f680cc6SAli Bahrami 		if ((pad = (state->pn_namesz & (sizeof (Word) - 1))) != 0) {
36044f680cc6SAli Bahrami 			pad = sizeof (Word) - pad;
36054f680cc6SAli Bahrami 			state->pns_size -= pad;
36064f680cc6SAli Bahrami 		}
36074f680cc6SAli Bahrami 		if (state->pns_size >= state->pn_descsz) {
36084f680cc6SAli Bahrami 			state->pns_size -= state->pn_descsz;
36094f680cc6SAli Bahrami 		} else {
36104f680cc6SAli Bahrami 			(void) fprintf(stderr, MSG_INTL(MSG_NOTE_BADDESZ),
36114f680cc6SAli Bahrami 			    state->pns_file, state->pns_cache->c_name,
36124f680cc6SAli Bahrami 			    EC_WORD(noteoff), EC_WORD(state->pn_namesz));
36134f680cc6SAli Bahrami 			return (0);
36144f680cc6SAli Bahrami 		}
36154f680cc6SAli Bahrami 	}
36164f680cc6SAli Bahrami 
36174f680cc6SAli Bahrami 	state->pn_type = *state->pns_data++;
36184f680cc6SAli Bahrami 
36194f680cc6SAli Bahrami 	/* Name */
36204f680cc6SAli Bahrami 	if (state->pn_namesz) {
36214f680cc6SAli Bahrami 		state->pn_name = (char *)state->pns_data;
36224f680cc6SAli Bahrami 		pad = (state->pn_namesz +
36234f680cc6SAli Bahrami 		    (sizeof (Word) - 1)) & ~(sizeof (Word) - 1);
36244f680cc6SAli Bahrami 		/* LINTED */
36254f680cc6SAli Bahrami 		state->pns_data = (Word *)(state->pn_name + pad);
36264f680cc6SAli Bahrami 	}
36274f680cc6SAli Bahrami 
36284f680cc6SAli Bahrami 	/*
36294f680cc6SAli Bahrami 	 * If multiple information blocks exist within a .note section
36304f680cc6SAli Bahrami 	 * account for any padding that must exist before the next
36314f680cc6SAli Bahrami 	 * information block.
36324f680cc6SAli Bahrami 	 */
36334f680cc6SAli Bahrami 	if ((pad = (state->pn_descsz & (sizeof (Word) - 1))) != 0) {
36344f680cc6SAli Bahrami 		pad = sizeof (Word) - pad;
36354f680cc6SAli Bahrami 		if (state->pns_size > pad)
36364f680cc6SAli Bahrami 			state->pns_size -= pad;
36374f680cc6SAli Bahrami 	}
36384f680cc6SAli Bahrami 
36394f680cc6SAli Bahrami 	/* Data */
36404f680cc6SAli Bahrami 	if (state->pn_descsz) {
36414f680cc6SAli Bahrami 		state->pn_desc = (const char *)state->pns_data;
36424f680cc6SAli Bahrami 		/* LINTED */
36434f680cc6SAli Bahrami 		state->pns_data = (Word *)(state->pn_desc +
36444f680cc6SAli Bahrami 		    state->pn_descsz + pad);
36454f680cc6SAli Bahrami 	}
36464f680cc6SAli Bahrami 
36474f680cc6SAli Bahrami 	return (1);
36484f680cc6SAli Bahrami }
36494f680cc6SAli Bahrami 
36504f680cc6SAli Bahrami /*
3651c6c9aed4Sab196087  * Callback function for use with conv_str_to_c_literal() below.
3652c6c9aed4Sab196087  */
3653c6c9aed4Sab196087 /*ARGSUSED2*/
3654c6c9aed4Sab196087 static void
3655c6c9aed4Sab196087 c_literal_cb(const void *ptr, size_t size, void *uvalue)
3656c6c9aed4Sab196087 {
3657c6c9aed4Sab196087 	(void) fwrite(ptr, size, 1, stdout);
3658c6c9aed4Sab196087 }
3659c6c9aed4Sab196087 
3660c6c9aed4Sab196087 /*
36617c478bd9Sstevel@tonic-gate  * Traverse a note section analyzing each note information block.
36627c478bd9Sstevel@tonic-gate  * The data buffers size is used to validate references before they are made,
36637c478bd9Sstevel@tonic-gate  * and is decremented as each element is processed.
36647c478bd9Sstevel@tonic-gate  */
36657c478bd9Sstevel@tonic-gate void
3666c6c9aed4Sab196087 note_entry(Cache *cache, Word *data, size_t size, Ehdr *ehdr, const char *file)
36677c478bd9Sstevel@tonic-gate {
3668c6c9aed4Sab196087 	int		cnt = 0;
3669c6c9aed4Sab196087 	int		is_corenote;
3670c6c9aed4Sab196087 	int		do_swap;
3671c6c9aed4Sab196087 	Conv_inv_buf_t	inv_buf;
36724f680cc6SAli Bahrami 	parse_note_t	pnstate;
3673c6c9aed4Sab196087 
36744f680cc6SAli Bahrami 	pnstate.pns_file = file;
36754f680cc6SAli Bahrami 	pnstate.pns_cache = cache;
36764f680cc6SAli Bahrami 	pnstate.pns_size = size;
36774f680cc6SAli Bahrami 	pnstate.pns_data = data;
3678c6c9aed4Sab196087 	do_swap = _elf_sys_encoding() != ehdr->e_ident[EI_DATA];
36795aefb655Srie 
36807c478bd9Sstevel@tonic-gate 	/*
36817c478bd9Sstevel@tonic-gate 	 * Print out a single `note' information block.
36827c478bd9Sstevel@tonic-gate 	 */
36834f680cc6SAli Bahrami 	while (pnstate.pns_size > 0) {
36847c478bd9Sstevel@tonic-gate 
36854f680cc6SAli Bahrami 		if (parse_note_entry(&pnstate) == 0)
36867c478bd9Sstevel@tonic-gate 			return;
36877c478bd9Sstevel@tonic-gate 
3688c6c9aed4Sab196087 		/*
3689c6c9aed4Sab196087 		 * Is this a Solaris core note? Such notes all have
3690c6c9aed4Sab196087 		 * the name "CORE".
3691c6c9aed4Sab196087 		 */
3692c6c9aed4Sab196087 		is_corenote = (ehdr->e_type == ET_CORE) &&
36934f680cc6SAli Bahrami 		    (pnstate.pn_namesz == (MSG_STR_CORE_SIZE + 1)) &&
36944f680cc6SAli Bahrami 		    (strncmp(MSG_ORIG(MSG_STR_CORE), pnstate.pn_name,
3695c6c9aed4Sab196087 		    MSG_STR_CORE_SIZE + 1) == 0);
36967c478bd9Sstevel@tonic-gate 
3697c6c9aed4Sab196087 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
3698c6c9aed4Sab196087 		dbg_print(0, MSG_INTL(MSG_FMT_NOTEENTNDX), EC_WORD(cnt));
3699c6c9aed4Sab196087 		cnt++;
37004f680cc6SAli Bahrami 		dbg_print(0, MSG_ORIG(MSG_NOTE_NAMESZ),
37014f680cc6SAli Bahrami 		    EC_WORD(pnstate.pn_namesz));
37024f680cc6SAli Bahrami 		dbg_print(0, MSG_ORIG(MSG_NOTE_DESCSZ),
37034f680cc6SAli Bahrami 		    EC_WORD(pnstate.pn_descsz));
3704c6c9aed4Sab196087 
3705c6c9aed4Sab196087 		if (is_corenote)
3706c6c9aed4Sab196087 			dbg_print(0, MSG_ORIG(MSG_NOTE_TYPE_STR),
37074f680cc6SAli Bahrami 			    conv_cnote_type(pnstate.pn_type, 0, &inv_buf));
3708c6c9aed4Sab196087 		else
37094f680cc6SAli Bahrami 			dbg_print(0, MSG_ORIG(MSG_NOTE_TYPE),
37104f680cc6SAli Bahrami 			    EC_WORD(pnstate.pn_type));
37114f680cc6SAli Bahrami 		if (pnstate.pn_namesz) {
3712c6c9aed4Sab196087 			dbg_print(0, MSG_ORIG(MSG_NOTE_NAME));
37137c478bd9Sstevel@tonic-gate 			/*
3714c6c9aed4Sab196087 			 * The name string can contain embedded 'null'
3715c6c9aed4Sab196087 			 * bytes and/or unprintable characters. Also,
3716c6c9aed4Sab196087 			 * the final NULL is documented in the ELF ABI
3717c6c9aed4Sab196087 			 * as being included in the namesz. So, display
3718c6c9aed4Sab196087 			 * the name using C literal string notation, and
3719c6c9aed4Sab196087 			 * include the terminating NULL in the output.
3720c6c9aed4Sab196087 			 * We don't show surrounding double quotes, as
3721c6c9aed4Sab196087 			 * that implies the termination that we are showing
3722c6c9aed4Sab196087 			 * explicitly.
37237c478bd9Sstevel@tonic-gate 			 */
3724c6c9aed4Sab196087 			(void) fwrite(MSG_ORIG(MSG_STR_8SP),
3725c6c9aed4Sab196087 			    MSG_STR_8SP_SIZE, 1, stdout);
37264f680cc6SAli Bahrami 			conv_str_to_c_literal(pnstate.pn_name,
37274f680cc6SAli Bahrami 			    pnstate.pn_namesz, c_literal_cb, NULL);
37285aefb655Srie 			dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
37297c478bd9Sstevel@tonic-gate 		}
37307c478bd9Sstevel@tonic-gate 
37314f680cc6SAli Bahrami 		if (pnstate.pn_descsz) {
3732c6c9aed4Sab196087 			int		hexdump = 1;
37337c478bd9Sstevel@tonic-gate 
37347c478bd9Sstevel@tonic-gate 			/*
3735c6c9aed4Sab196087 			 * If this is a core note, let the corenote()
3736c6c9aed4Sab196087 			 * function handle it.
37377c478bd9Sstevel@tonic-gate 			 */
3738c6c9aed4Sab196087 			if (is_corenote) {
3739c6c9aed4Sab196087 				/* We only issue the bad arch error once */
3740c6c9aed4Sab196087 				static int	badnote_done = 0;
3741c6c9aed4Sab196087 				corenote_ret_t	corenote_ret;
37427c478bd9Sstevel@tonic-gate 
3743c6c9aed4Sab196087 				corenote_ret = corenote(ehdr->e_machine,
37444f680cc6SAli Bahrami 				    do_swap, pnstate.pn_type, pnstate.pn_desc,
37454f680cc6SAli Bahrami 				    pnstate.pn_descsz);
3746c6c9aed4Sab196087 				switch (corenote_ret) {
374737915d86SRichard Lowe 				case CORENOTE_R_OK_DUMP:
374837915d86SRichard Lowe 					hexdump = 1;
374937915d86SRichard Lowe 					break;
3750c6c9aed4Sab196087 				case CORENOTE_R_OK:
3751c6c9aed4Sab196087 					hexdump = 0;
3752c6c9aed4Sab196087 					break;
3753c6c9aed4Sab196087 				case CORENOTE_R_BADDATA:
3754c6c9aed4Sab196087 					(void) fprintf(stderr,
3755c6c9aed4Sab196087 					    MSG_INTL(MSG_NOTE_BADCOREDATA),
3756c6c9aed4Sab196087 					    file);
3757c6c9aed4Sab196087 					break;
3758c6c9aed4Sab196087 				case CORENOTE_R_BADARCH:
3759c6c9aed4Sab196087 					if (badnote_done)
3760c6c9aed4Sab196087 						break;
3761c6c9aed4Sab196087 					(void) fprintf(stderr,
3762c6c9aed4Sab196087 					    MSG_INTL(MSG_NOTE_BADCOREARCH),
3763c6c9aed4Sab196087 					    file,
3764c6c9aed4Sab196087 					    conv_ehdr_mach(ehdr->e_machine,
3765c6c9aed4Sab196087 					    0, &inv_buf));
3766c6c9aed4Sab196087 					break;
376737915d86SRichard Lowe 				case CORENOTE_R_BADTYPE:
376837915d86SRichard Lowe 					(void) fprintf(stderr,
376937915d86SRichard Lowe 					    MSG_INTL(MSG_NOTE_BADCORETYPE),
377037915d86SRichard Lowe 					    file,
377137915d86SRichard Lowe 					    EC_WORD(pnstate.pn_type));
377237915d86SRichard Lowe 					break;
377337915d86SRichard Lowe 
37747c478bd9Sstevel@tonic-gate 				}
37757c478bd9Sstevel@tonic-gate 			}
37767c478bd9Sstevel@tonic-gate 
3777c6c9aed4Sab196087 			/*
3778c6c9aed4Sab196087 			 * The default thing when we don't understand
3779c6c9aed4Sab196087 			 * the note data is to display it as hex bytes.
3780c6c9aed4Sab196087 			 */
3781c6c9aed4Sab196087 			if (hexdump) {
3782c6c9aed4Sab196087 				dbg_print(0, MSG_ORIG(MSG_NOTE_DESC));
37834f680cc6SAli Bahrami 				dump_hex_bytes(pnstate.pn_desc,
37844f680cc6SAli Bahrami 				    pnstate.pn_descsz, 8, 4, 4);
3785c6c9aed4Sab196087 			}
37867c478bd9Sstevel@tonic-gate 		}
37877c478bd9Sstevel@tonic-gate 	}
37887c478bd9Sstevel@tonic-gate }
37897c478bd9Sstevel@tonic-gate 
37907c478bd9Sstevel@tonic-gate /*
3791c6c9aed4Sab196087  * Search for and process .note sections.
3792c6c9aed4Sab196087  *
3793c6c9aed4Sab196087  * Returns the number of note sections seen.
37947c478bd9Sstevel@tonic-gate  */
3795c6c9aed4Sab196087 static Word
3796c6c9aed4Sab196087 note(Cache *cache, Word shnum, Ehdr *ehdr, const char *file)
37977c478bd9Sstevel@tonic-gate {
3798c6c9aed4Sab196087 	Word	cnt, note_cnt = 0;
37997c478bd9Sstevel@tonic-gate 
38007c478bd9Sstevel@tonic-gate 	/*
38017c478bd9Sstevel@tonic-gate 	 * Otherwise look for any .note sections.
38027c478bd9Sstevel@tonic-gate 	 */
38037c478bd9Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
38047c478bd9Sstevel@tonic-gate 		Cache	*_cache = &cache[cnt];
38055aefb655Srie 		Shdr	*shdr = _cache->c_shdr;
38067c478bd9Sstevel@tonic-gate 
38077c478bd9Sstevel@tonic-gate 		if (shdr->sh_type != SHT_NOTE)
38087c478bd9Sstevel@tonic-gate 			continue;
3809c6c9aed4Sab196087 		note_cnt++;
3810981a172dSab196087 		if (!match(MATCH_F_ALL, _cache->c_name, cnt, shdr->sh_type))
38117c478bd9Sstevel@tonic-gate 			continue;
38127c478bd9Sstevel@tonic-gate 
38137c478bd9Sstevel@tonic-gate 		/*
38147c478bd9Sstevel@tonic-gate 		 * As these sections are often hand rolled, make sure they're
3815f959f76aSab196087 		 * properly aligned before proceeding, and issue an error
3816f959f76aSab196087 		 * as necessary.
3817f959f76aSab196087 		 *
3818f959f76aSab196087 		 * Note that we will continue on to display the note even
3819f959f76aSab196087 		 * if it has bad alignment. We can do this safely, because
3820f959f76aSab196087 		 * libelf knows the alignment required for SHT_NOTE, and
3821f959f76aSab196087 		 * takes steps to deliver a properly aligned buffer to us
3822f959f76aSab196087 		 * even if the actual file is misaligned.
38237c478bd9Sstevel@tonic-gate 		 */
3824f959f76aSab196087 		if (shdr->sh_offset & (sizeof (Word) - 1))
38257c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADALIGN),
38267c478bd9Sstevel@tonic-gate 			    file, _cache->c_name);
3827f959f76aSab196087 
38289a411307Srie 		if (_cache->c_data == NULL)
38299a411307Srie 			continue;
38307c478bd9Sstevel@tonic-gate 
38315aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
38325aefb655Srie 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_NOTE), _cache->c_name);
38337c478bd9Sstevel@tonic-gate 		note_entry(_cache, (Word *)_cache->c_data->d_buf,
38347c478bd9Sstevel@tonic-gate 		/* LINTED */
3835c6c9aed4Sab196087 		    (Word)_cache->c_data->d_size, ehdr, file);
38367c478bd9Sstevel@tonic-gate 	}
3837c6c9aed4Sab196087 
3838c6c9aed4Sab196087 	return (note_cnt);
38397c478bd9Sstevel@tonic-gate }
38407c478bd9Sstevel@tonic-gate 
38415aefb655Srie /*
38424f680cc6SAli Bahrami  * The Linux Standard Base defines a special note named .note.ABI-tag
38434f680cc6SAli Bahrami  * that is used to maintain Linux ABI information. Presence of this section
38444f680cc6SAli Bahrami  * is a strong indication that the object should be considered to be
38454f680cc6SAli Bahrami  * ELFOSABI_LINUX.
38464f680cc6SAli Bahrami  *
38474f680cc6SAli Bahrami  * This function returns True (1) if such a note is seen, and False (0)
38484f680cc6SAli Bahrami  * otherwise.
38494f680cc6SAli Bahrami  */
38504f680cc6SAli Bahrami static int
38514f680cc6SAli Bahrami has_linux_abi_note(Cache *cache, Word shnum, const char *file)
38524f680cc6SAli Bahrami {
38534f680cc6SAli Bahrami 	Word	cnt;
38544f680cc6SAli Bahrami 
38554f680cc6SAli Bahrami 	for (cnt = 1; cnt < shnum; cnt++) {
38564f680cc6SAli Bahrami 		parse_note_t	pnstate;
38574f680cc6SAli Bahrami 		Cache		*_cache = &cache[cnt];
38584f680cc6SAli Bahrami 		Shdr		*shdr = _cache->c_shdr;
38594f680cc6SAli Bahrami 
38604f680cc6SAli Bahrami 		/*
38614f680cc6SAli Bahrami 		 * Section must be SHT_NOTE, must have the name
38624f680cc6SAli Bahrami 		 * .note.ABI-tag, and must have data.
38634f680cc6SAli Bahrami 		 */
38644f680cc6SAli Bahrami 		if ((shdr->sh_type != SHT_NOTE) ||
38654f680cc6SAli Bahrami 		    (strcmp(MSG_ORIG(MSG_STR_NOTEABITAG),
38664f680cc6SAli Bahrami 		    _cache->c_name) != 0) || (_cache->c_data == NULL))
38674f680cc6SAli Bahrami 			continue;
38684f680cc6SAli Bahrami 
38694f680cc6SAli Bahrami 		pnstate.pns_file = file;
38704f680cc6SAli Bahrami 		pnstate.pns_cache = _cache;
38714f680cc6SAli Bahrami 		pnstate.pns_size = _cache->c_data->d_size;
38724f680cc6SAli Bahrami 		pnstate.pns_data = (Word *)_cache->c_data->d_buf;
38734f680cc6SAli Bahrami 
38744f680cc6SAli Bahrami 		while (pnstate.pns_size > 0) {
38754f680cc6SAli Bahrami 			Word *w;
38764f680cc6SAli Bahrami 
38774f680cc6SAli Bahrami 			if (parse_note_entry(&pnstate) == 0)
38784f680cc6SAli Bahrami 				break;
38794f680cc6SAli Bahrami 
38804f680cc6SAli Bahrami 			/*
38814f680cc6SAli Bahrami 			 * The type must be 1, and the name must be "GNU".
38824f680cc6SAli Bahrami 			 * The descsz must be at least 16 bytes.
38834f680cc6SAli Bahrami 			 */
38844f680cc6SAli Bahrami 			if ((pnstate.pn_type != 1) ||
38854f680cc6SAli Bahrami 			    (pnstate.pn_namesz != (MSG_STR_GNU_SIZE + 1)) ||
38864f680cc6SAli Bahrami 			    (strncmp(MSG_ORIG(MSG_STR_GNU), pnstate.pn_name,
38874f680cc6SAli Bahrami 			    MSG_STR_CORE_SIZE + 1) != 0) ||
38884f680cc6SAli Bahrami 			    (pnstate.pn_descsz < 16))
38894f680cc6SAli Bahrami 				continue;
38904f680cc6SAli Bahrami 
38914f680cc6SAli Bahrami 			/*
38924f680cc6SAli Bahrami 			 * desc contains 4 32-bit fields. Field 0 must be 0,
38934f680cc6SAli Bahrami 			 * indicating Linux. The second, third, and fourth
38944f680cc6SAli Bahrami 			 * fields represent the earliest Linux kernel
38954f680cc6SAli Bahrami 			 * version compatible with this object.
38964f680cc6SAli Bahrami 			 */
38974f680cc6SAli Bahrami 			/*LINTED*/
38984f680cc6SAli Bahrami 			w = (Word *) pnstate.pn_desc;
38994f680cc6SAli Bahrami 			if (*w == 0)
39004f680cc6SAli Bahrami 				return (1);
39014f680cc6SAli Bahrami 		}
39024f680cc6SAli Bahrami 	}
39034f680cc6SAli Bahrami 
39044f680cc6SAli Bahrami 	return (0);
39054f680cc6SAli Bahrami }
39064f680cc6SAli Bahrami 
39074f680cc6SAli Bahrami /*
39085aefb655Srie  * Determine an individual hash entry.  This may be the initial hash entry,
39095aefb655Srie  * or an associated chain entry.
39105aefb655Srie  */
39115aefb655Srie static void
39125aefb655Srie hash_entry(Cache *refsec, Cache *strsec, const char *hsecname, Word hashndx,
39135aefb655Srie     Word symndx, Word symn, Sym *syms, const char *file, ulong_t bkts,
39145aefb655Srie     uint_t flags, int chain)
39155aefb655Srie {
39165aefb655Srie 	Sym		*sym;
39175aefb655Srie 	const char	*symname, *str;
39185aefb655Srie 	char		_bucket[MAXNDXSIZE], _symndx[MAXNDXSIZE];
39195aefb655Srie 	ulong_t		nbkt, nhash;
39205aefb655Srie 
39215aefb655Srie 	if (symndx > symn) {
39225aefb655Srie 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_HSBADSYMNDX), file,
39235aefb655Srie 		    EC_WORD(symndx), EC_WORD(hashndx));
39245aefb655Srie 		symname = MSG_INTL(MSG_STR_UNKNOWN);
39255aefb655Srie 	} else {
39265aefb655Srie 		sym = (Sym *)(syms + symndx);
39275aefb655Srie 		symname = string(refsec, symndx, strsec, file, sym->st_name);
39285aefb655Srie 	}
39295aefb655Srie 
39305aefb655Srie 	if (chain == 0) {
39315aefb655Srie 		(void) snprintf(_bucket, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INTEGER),
39325aefb655Srie 		    hashndx);
39335aefb655Srie 		str = (const char *)_bucket;
39345aefb655Srie 	} else
39355aefb655Srie 		str = MSG_ORIG(MSG_STR_EMPTY);
39365aefb655Srie 
39375aefb655Srie 	(void) snprintf(_symndx, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INDEX2),
39385aefb655Srie 	    EC_WORD(symndx));
39395aefb655Srie 	dbg_print(0, MSG_ORIG(MSG_FMT_HASH_INFO), str, _symndx,
39405aefb655Srie 	    demangle(symname, flags));
39415aefb655Srie 
39425aefb655Srie 	/*
39435aefb655Srie 	 * Determine if this string is in the correct bucket.
39445aefb655Srie 	 */
39455aefb655Srie 	nhash = elf_hash(symname);
39465aefb655Srie 	nbkt = nhash % bkts;
39475aefb655Srie 
39485aefb655Srie 	if (nbkt != hashndx) {
39495aefb655Srie 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADHASH), file,
39505aefb655Srie 		    hsecname, symname, EC_WORD(hashndx), nbkt);
39515aefb655Srie 	}
39525aefb655Srie }
39537c478bd9Sstevel@tonic-gate 
39547c478bd9Sstevel@tonic-gate #define	MAXCOUNT	500
39557c478bd9Sstevel@tonic-gate 
39567c478bd9Sstevel@tonic-gate static void
3957c809c407Sab196087 hash(Cache *cache, Word shnum, const char *file, uint_t flags)
39587c478bd9Sstevel@tonic-gate {
39597c478bd9Sstevel@tonic-gate 	static int	count[MAXCOUNT];
39605aefb655Srie 	Word		cnt;
39617c478bd9Sstevel@tonic-gate 	ulong_t		ndx, bkts;
39627c478bd9Sstevel@tonic-gate 	char		number[MAXNDXSIZE];
39637c478bd9Sstevel@tonic-gate 
39647c478bd9Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
39657c478bd9Sstevel@tonic-gate 		uint_t		*hash, *chain;
39667c478bd9Sstevel@tonic-gate 		Cache		*_cache = &cache[cnt];
39675aefb655Srie 		Shdr		*sshdr, *hshdr = _cache->c_shdr;
39685aefb655Srie 		char		*ssecname, *hsecname = _cache->c_name;
39695aefb655Srie 		Sym		*syms;
39705aefb655Srie 		Word		symn;
39717c478bd9Sstevel@tonic-gate 
39725aefb655Srie 		if (hshdr->sh_type != SHT_HASH)
39737c478bd9Sstevel@tonic-gate 			continue;
39747c478bd9Sstevel@tonic-gate 
39757c478bd9Sstevel@tonic-gate 		/*
39767c478bd9Sstevel@tonic-gate 		 * Determine the hash table data and size.
39777c478bd9Sstevel@tonic-gate 		 */
39785aefb655Srie 		if ((hshdr->sh_entsize == 0) || (hshdr->sh_size == 0)) {
39797c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
39805aefb655Srie 			    file, hsecname);
39817c478bd9Sstevel@tonic-gate 			continue;
39827c478bd9Sstevel@tonic-gate 		}
39839a411307Srie 		if (_cache->c_data == NULL)
39849a411307Srie 			continue;
39859a411307Srie 
39867c478bd9Sstevel@tonic-gate 		hash = (uint_t *)_cache->c_data->d_buf;
39877c478bd9Sstevel@tonic-gate 		bkts = *hash;
39887c478bd9Sstevel@tonic-gate 		chain = hash + 2 + bkts;
39897c478bd9Sstevel@tonic-gate 		hash += 2;
39907c478bd9Sstevel@tonic-gate 
39917c478bd9Sstevel@tonic-gate 		/*
39927c478bd9Sstevel@tonic-gate 		 * Get the data buffer for the associated symbol table.
39937c478bd9Sstevel@tonic-gate 		 */
39945aefb655Srie 		if ((hshdr->sh_link == 0) || (hshdr->sh_link >= shnum)) {
39957c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
39965aefb655Srie 			    file, hsecname, EC_WORD(hshdr->sh_link));
39977c478bd9Sstevel@tonic-gate 			continue;
39987c478bd9Sstevel@tonic-gate 		}
39997c478bd9Sstevel@tonic-gate 
40005aefb655Srie 		_cache = &cache[hshdr->sh_link];
40015aefb655Srie 		ssecname = _cache->c_name;
40025aefb655Srie 
40039a411307Srie 		if (_cache->c_data == NULL)
40049a411307Srie 			continue;
40059a411307Srie 
40069a411307Srie 		if ((syms = (Sym *)_cache->c_data->d_buf) == NULL) {
40075aefb655Srie 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
40085aefb655Srie 			    file, ssecname);
40095aefb655Srie 			continue;
40105aefb655Srie 		}
40115aefb655Srie 
40125aefb655Srie 		sshdr = _cache->c_shdr;
40135aefb655Srie 		/* LINTED */
40145aefb655Srie 		symn = (Word)(sshdr->sh_size / sshdr->sh_entsize);
40155aefb655Srie 
40167c478bd9Sstevel@tonic-gate 		/*
40177c478bd9Sstevel@tonic-gate 		 * Get the associated string table section.
40187c478bd9Sstevel@tonic-gate 		 */
40195aefb655Srie 		if ((sshdr->sh_link == 0) || (sshdr->sh_link >= shnum)) {
40207c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
40215aefb655Srie 			    file, ssecname, EC_WORD(sshdr->sh_link));
40227c478bd9Sstevel@tonic-gate 			continue;
40237c478bd9Sstevel@tonic-gate 		}
40247c478bd9Sstevel@tonic-gate 
40255aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
40265aefb655Srie 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_HASH), hsecname);
40275aefb655Srie 		dbg_print(0, MSG_INTL(MSG_ELF_HASH_INFO));
40287c478bd9Sstevel@tonic-gate 
40297c478bd9Sstevel@tonic-gate 		/*
40307c478bd9Sstevel@tonic-gate 		 * Loop through the hash buckets, printing the appropriate
40317c478bd9Sstevel@tonic-gate 		 * symbols.
40327c478bd9Sstevel@tonic-gate 		 */
40337c478bd9Sstevel@tonic-gate 		for (ndx = 0; ndx < bkts; ndx++, hash++) {
40345aefb655Srie 			Word	_ndx, _cnt;
40357c478bd9Sstevel@tonic-gate 
40367c478bd9Sstevel@tonic-gate 			if (*hash == 0) {
40377c478bd9Sstevel@tonic-gate 				count[0]++;
40387c478bd9Sstevel@tonic-gate 				continue;
40397c478bd9Sstevel@tonic-gate 			}
40407c478bd9Sstevel@tonic-gate 
40415aefb655Srie 			hash_entry(_cache, &cache[sshdr->sh_link], hsecname,
40425aefb655Srie 			    ndx, *hash, symn, syms, file, bkts, flags, 0);
40437c478bd9Sstevel@tonic-gate 
40447c478bd9Sstevel@tonic-gate 			/*
40457c478bd9Sstevel@tonic-gate 			 * Determine if any other symbols are chained to this
40467c478bd9Sstevel@tonic-gate 			 * bucket.
40477c478bd9Sstevel@tonic-gate 			 */
40487c478bd9Sstevel@tonic-gate 			_ndx = chain[*hash];
40497c478bd9Sstevel@tonic-gate 			_cnt = 1;
40507c478bd9Sstevel@tonic-gate 			while (_ndx) {
40515aefb655Srie 				hash_entry(_cache, &cache[sshdr->sh_link],
40525aefb655Srie 				    hsecname, ndx, _ndx, symn, syms, file,
40535aefb655Srie 				    bkts, flags, 1);
40547c478bd9Sstevel@tonic-gate 				_ndx = chain[_ndx];
40557c478bd9Sstevel@tonic-gate 				_cnt++;
40567c478bd9Sstevel@tonic-gate 			}
40577c478bd9Sstevel@tonic-gate 
40587c478bd9Sstevel@tonic-gate 			if (_cnt >= MAXCOUNT) {
40597c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
4060141040e8Srie 				    MSG_INTL(MSG_HASH_OVERFLW), file,
40615aefb655Srie 				    _cache->c_name, EC_WORD(ndx),
40625aefb655Srie 				    EC_WORD(_cnt));
40637c478bd9Sstevel@tonic-gate 			} else
40647c478bd9Sstevel@tonic-gate 				count[_cnt]++;
40657c478bd9Sstevel@tonic-gate 		}
40667c478bd9Sstevel@tonic-gate 		break;
40677c478bd9Sstevel@tonic-gate 	}
40687c478bd9Sstevel@tonic-gate 
40697c478bd9Sstevel@tonic-gate 	/*
40707c478bd9Sstevel@tonic-gate 	 * Print out the count information.
40717c478bd9Sstevel@tonic-gate 	 */
40727c478bd9Sstevel@tonic-gate 	bkts = cnt = 0;
40735aefb655Srie 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
40745aefb655Srie 
40757c478bd9Sstevel@tonic-gate 	for (ndx = 0; ndx < MAXCOUNT; ndx++) {
40765aefb655Srie 		Word	_cnt;
40777c478bd9Sstevel@tonic-gate 
40787c478bd9Sstevel@tonic-gate 		if ((_cnt = count[ndx]) == 0)
40797c478bd9Sstevel@tonic-gate 			continue;
40807c478bd9Sstevel@tonic-gate 
40815aefb655Srie 		(void) snprintf(number, MAXNDXSIZE,
40825aefb655Srie 		    MSG_ORIG(MSG_FMT_INTEGER), _cnt);
40835aefb655Srie 		dbg_print(0, MSG_INTL(MSG_ELF_HASH_BKTS1), number,
40845aefb655Srie 		    EC_WORD(ndx));
40857c478bd9Sstevel@tonic-gate 		bkts += _cnt;
40865aefb655Srie 		cnt += (Word)(ndx * _cnt);
40877c478bd9Sstevel@tonic-gate 	}
40887c478bd9Sstevel@tonic-gate 	if (cnt) {
40897c478bd9Sstevel@tonic-gate 		(void) snprintf(number, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INTEGER),
40905aefb655Srie 		    bkts);
40915aefb655Srie 		dbg_print(0, MSG_INTL(MSG_ELF_HASH_BKTS2), number,
40925aefb655Srie 		    EC_WORD(cnt));
40937c478bd9Sstevel@tonic-gate 	}
40947c478bd9Sstevel@tonic-gate }
40957c478bd9Sstevel@tonic-gate 
40967c478bd9Sstevel@tonic-gate static void
4097c809c407Sab196087 group(Cache *cache, Word shnum, const char *file, uint_t flags)
40987c478bd9Sstevel@tonic-gate {
40995aefb655Srie 	Word	scnt;
41007c478bd9Sstevel@tonic-gate 
41015aefb655Srie 	for (scnt = 1; scnt < shnum; scnt++) {
41025aefb655Srie 		Cache		*_cache = &cache[scnt];
41035aefb655Srie 		Shdr		*shdr = _cache->c_shdr;
41045aefb655Srie 		Word		*grpdata, gcnt, grpcnt, symnum, unknown;
41055aefb655Srie 		Cache		*symsec, *strsec;
41065aefb655Srie 		Sym		*syms, *sym;
41077c478bd9Sstevel@tonic-gate 		char		flgstrbuf[MSG_GRP_COMDAT_SIZE + 10];
4108e64d0ff9SAli Bahrami 		const char	*grpnam;
41097c478bd9Sstevel@tonic-gate 
41107c478bd9Sstevel@tonic-gate 		if (shdr->sh_type != SHT_GROUP)
41117c478bd9Sstevel@tonic-gate 			continue;
4112981a172dSab196087 		if (!match(MATCH_F_ALL, _cache->c_name, scnt, shdr->sh_type))
41137c478bd9Sstevel@tonic-gate 			continue;
41149a411307Srie 		if ((_cache->c_data == NULL) ||
41159a411307Srie 		    ((grpdata = (Word *)_cache->c_data->d_buf) == NULL))
41167c478bd9Sstevel@tonic-gate 			continue;
41175aefb655Srie 		grpcnt = shdr->sh_size / sizeof (Word);
41185aefb655Srie 
41195aefb655Srie 		/*
41205aefb655Srie 		 * Get the data buffer for the associated symbol table and
41215aefb655Srie 		 * string table.
41225aefb655Srie 		 */
41235aefb655Srie 		if (stringtbl(cache, 1, scnt, shnum, file,
41245aefb655Srie 		    &symnum, &symsec, &strsec) == 0)
41255aefb655Srie 			return;
41265aefb655Srie 
41275aefb655Srie 		syms = symsec->c_data->d_buf;
41285aefb655Srie 
41295aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
41305aefb655Srie 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_GRP), _cache->c_name);
41315aefb655Srie 		dbg_print(0, MSG_INTL(MSG_GRP_TITLE));
41325aefb655Srie 
41335aefb655Srie 		/*
41345aefb655Srie 		 * The first element of the group defines the group.  The
41355aefb655Srie 		 * associated symbol is defined by the sh_link field.
41365aefb655Srie 		 */
41375aefb655Srie 		if ((shdr->sh_info == SHN_UNDEF) || (shdr->sh_info > symnum)) {
41385aefb655Srie 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHINFO),
41395aefb655Srie 			    file, _cache->c_name, EC_WORD(shdr->sh_info));
41405aefb655Srie 			return;
41417c478bd9Sstevel@tonic-gate 		}
41427c478bd9Sstevel@tonic-gate 
41435aefb655Srie 		(void) strcpy(flgstrbuf, MSG_ORIG(MSG_STR_OSQBRKT));
41447c478bd9Sstevel@tonic-gate 		if (grpdata[0] & GRP_COMDAT) {
41455aefb655Srie 			(void) strcat(flgstrbuf, MSG_ORIG(MSG_GRP_COMDAT));
41467c478bd9Sstevel@tonic-gate 		}
41475aefb655Srie 		if ((unknown = (grpdata[0] & ~GRP_COMDAT)) != 0) {
41485aefb655Srie 			size_t	len = strlen(flgstrbuf);
41497c478bd9Sstevel@tonic-gate 
41505aefb655Srie 			(void) snprintf(&flgstrbuf[len],
41515aefb655Srie 			    (MSG_GRP_COMDAT_SIZE + 10 - len),
41525aefb655Srie 			    MSG_ORIG(MSG_GRP_UNKNOWN), unknown);
41537c478bd9Sstevel@tonic-gate 		}
41545aefb655Srie 		(void) strcat(flgstrbuf, MSG_ORIG(MSG_STR_CSQBRKT));
41555aefb655Srie 		sym = (Sym *)(syms + shdr->sh_info);
41565aefb655Srie 
4157e64d0ff9SAli Bahrami 		/*
4158e64d0ff9SAli Bahrami 		 * The GNU assembler can use section symbols as the signature
4159e64d0ff9SAli Bahrami 		 * symbol as described by this comment in the gold linker
4160e64d0ff9SAli Bahrami 		 * (found via google):
4161e64d0ff9SAli Bahrami 		 *
4162e64d0ff9SAli Bahrami 		 *	It seems that some versions of gas will create a
4163e64d0ff9SAli Bahrami 		 *	section group associated with a section symbol, and
4164e64d0ff9SAli Bahrami 		 *	then fail to give a name to the section symbol.  In
4165e64d0ff9SAli Bahrami 		 *	such a case, use the name of the section.
4166e64d0ff9SAli Bahrami 		 *
4167e64d0ff9SAli Bahrami 		 * In order to support such objects, we do the same.
4168e64d0ff9SAli Bahrami 		 */
4169e64d0ff9SAli Bahrami 		grpnam = string(_cache, 0, strsec, file, sym->st_name);
4170e64d0ff9SAli Bahrami 		if (((sym->st_name == 0) || (*grpnam == '\0')) &&
4171e64d0ff9SAli Bahrami 		    (ELF_ST_TYPE(sym->st_info) == STT_SECTION))
4172e64d0ff9SAli Bahrami 			grpnam = cache[sym->st_shndx].c_name;
4173e64d0ff9SAli Bahrami 
41745aefb655Srie 		dbg_print(0, MSG_INTL(MSG_GRP_SIGNATURE), flgstrbuf,
4175e64d0ff9SAli Bahrami 		    demangle(grpnam, flags));
41765aefb655Srie 
41775aefb655Srie 		for (gcnt = 1; gcnt < grpcnt; gcnt++) {
41787c478bd9Sstevel@tonic-gate 			char		index[MAXNDXSIZE];
41795aefb655Srie 			const char	*name;
41807c478bd9Sstevel@tonic-gate 
41817c478bd9Sstevel@tonic-gate 			(void) snprintf(index, MAXNDXSIZE,
41825aefb655Srie 			    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(gcnt));
41837c478bd9Sstevel@tonic-gate 
41845aefb655Srie 			if (grpdata[gcnt] >= shnum)
41855aefb655Srie 				name = MSG_INTL(MSG_GRP_INVALSCN);
41865aefb655Srie 			else
41875aefb655Srie 				name = cache[grpdata[gcnt]].c_name;
41885aefb655Srie 
41895aefb655Srie 			(void) printf(MSG_ORIG(MSG_GRP_ENTRY), index, name,
41905aefb655Srie 			    EC_XWORD(grpdata[gcnt]));
41915aefb655Srie 		}
41925aefb655Srie 	}
41935aefb655Srie }
41947c478bd9Sstevel@tonic-gate 
41957c478bd9Sstevel@tonic-gate static void
41960e233487SRod Evans got(Cache *cache, Word shnum, Ehdr *ehdr, const char *file)
41977c478bd9Sstevel@tonic-gate {
4198f959f76aSab196087 	Cache		*gotcache = NULL, *symtab = NULL;
41995aefb655Srie 	Addr		gotbgn, gotend;
42005aefb655Srie 	Shdr		*gotshdr;
42015aefb655Srie 	Word		cnt, gotents, gotndx;
42027c478bd9Sstevel@tonic-gate 	size_t		gentsize;
42037c478bd9Sstevel@tonic-gate 	Got_info	*gottable;
42047c478bd9Sstevel@tonic-gate 	char		*gotdata;
42055aefb655Srie 	Sym		*gotsym;
42065aefb655Srie 	Xword		gotsymaddr;
4207ba2be530Sab196087 	uint_t		sys_encoding;
42087c478bd9Sstevel@tonic-gate 
42097c478bd9Sstevel@tonic-gate 	/*
4210141040e8Srie 	 * First, find the got.
42117c478bd9Sstevel@tonic-gate 	 */
42127c478bd9Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
4213f959f76aSab196087 		if (strncmp(cache[cnt].c_name, MSG_ORIG(MSG_ELF_GOT),
4214141040e8Srie 		    MSG_ELF_GOT_SIZE) == 0) {
4215f959f76aSab196087 			gotcache = &cache[cnt];
42167c478bd9Sstevel@tonic-gate 			break;
42177c478bd9Sstevel@tonic-gate 		}
42187c478bd9Sstevel@tonic-gate 	}
4219f959f76aSab196087 	if (gotcache == NULL)
42207c478bd9Sstevel@tonic-gate 		return;
4221141040e8Srie 
4222141040e8Srie 	/*
4223141040e8Srie 	 * A got section within a relocatable object is suspicious.
4224141040e8Srie 	 */
4225141040e8Srie 	if (ehdr->e_type == ET_REL) {
4226141040e8Srie 		(void) fprintf(stderr, MSG_INTL(MSG_GOT_UNEXPECTED), file,
4227f959f76aSab196087 		    gotcache->c_name);
4228141040e8Srie 	}
4229141040e8Srie 
42305aefb655Srie 	gotshdr = gotcache->c_shdr;
42317c478bd9Sstevel@tonic-gate 	if (gotshdr->sh_size == 0) {
42327c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
42337c478bd9Sstevel@tonic-gate 		    file, gotcache->c_name);
42347c478bd9Sstevel@tonic-gate 		return;
42357c478bd9Sstevel@tonic-gate 	}
42365aefb655Srie 
42375aefb655Srie 	gotbgn = gotshdr->sh_addr;
42387c478bd9Sstevel@tonic-gate 	gotend = gotbgn + gotshdr->sh_size;
42397c478bd9Sstevel@tonic-gate 
42407c478bd9Sstevel@tonic-gate 	/*
42415aefb655Srie 	 * Some architectures don't properly set the sh_entsize for the GOT
42425aefb655Srie 	 * table.  If it's not set, default to a size of a pointer.
42437c478bd9Sstevel@tonic-gate 	 */
42445aefb655Srie 	if ((gentsize = gotshdr->sh_entsize) == 0)
42455aefb655Srie 		gentsize = sizeof (Xword);
42465aefb655Srie 
42479a411307Srie 	if (gotcache->c_data == NULL)
42489a411307Srie 		return;
42499a411307Srie 
42507c478bd9Sstevel@tonic-gate 	/* LINTED */
42515aefb655Srie 	gotents = (Word)(gotshdr->sh_size / gentsize);
42527c478bd9Sstevel@tonic-gate 	gotdata = gotcache->c_data->d_buf;
42537c478bd9Sstevel@tonic-gate 
42547c478bd9Sstevel@tonic-gate 	if ((gottable = calloc(gotents, sizeof (Got_info))) == 0) {
42557c478bd9Sstevel@tonic-gate 		int err = errno;
42565aefb655Srie 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC), file,
42575aefb655Srie 		    strerror(err));
42587c478bd9Sstevel@tonic-gate 		return;
42597c478bd9Sstevel@tonic-gate 	}
42607c478bd9Sstevel@tonic-gate 
42617c478bd9Sstevel@tonic-gate 	/*
42627c478bd9Sstevel@tonic-gate 	 * Now we scan through all the sections looking for any relocations
42637c478bd9Sstevel@tonic-gate 	 * that may be against the GOT.  Since these may not be isolated to a
42647c478bd9Sstevel@tonic-gate 	 * .rel[a].got section we check them all.
42657c478bd9Sstevel@tonic-gate 	 * While scanning sections save the symbol table entry (a symtab
42667c478bd9Sstevel@tonic-gate 	 * overriding a dynsym) so that we can lookup _GLOBAL_OFFSET_TABLE_.
42677c478bd9Sstevel@tonic-gate 	 */
42687c478bd9Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
42695aefb655Srie 		Word		type, symnum;
42705aefb655Srie 		Xword		relndx, relnum, relsize;
42715aefb655Srie 		void		*rels;
42725aefb655Srie 		Sym		*syms;
42735aefb655Srie 		Cache		*symsec, *strsec;
42745aefb655Srie 		Cache		*_cache = &cache[cnt];
42755aefb655Srie 		Shdr		*shdr;
42767c478bd9Sstevel@tonic-gate 
42775aefb655Srie 		shdr = _cache->c_shdr;
42785aefb655Srie 		type = shdr->sh_type;
42797c478bd9Sstevel@tonic-gate 
42805aefb655Srie 		if ((symtab == 0) && (type == SHT_DYNSYM)) {
42817c478bd9Sstevel@tonic-gate 			symtab = _cache;
42827c478bd9Sstevel@tonic-gate 			continue;
42837c478bd9Sstevel@tonic-gate 		}
42845aefb655Srie 		if (type == SHT_SYMTAB) {
42857c478bd9Sstevel@tonic-gate 			symtab = _cache;
42867c478bd9Sstevel@tonic-gate 			continue;
42877c478bd9Sstevel@tonic-gate 		}
42885aefb655Srie 		if ((type != SHT_RELA) && (type != SHT_REL))
42897c478bd9Sstevel@tonic-gate 			continue;
42907c478bd9Sstevel@tonic-gate 
42917c478bd9Sstevel@tonic-gate 		/*
42925aefb655Srie 		 * Decide entry size.
42937c478bd9Sstevel@tonic-gate 		 */
42945aefb655Srie 		if (((relsize = shdr->sh_entsize) == 0) ||
42955aefb655Srie 		    (relsize > shdr->sh_size)) {
42965aefb655Srie 			if (type == SHT_RELA)
42975aefb655Srie 				relsize = sizeof (Rela);
42985aefb655Srie 			else
42995aefb655Srie 				relsize = sizeof (Rel);
43005aefb655Srie 		}
43015aefb655Srie 
43025aefb655Srie 		/*
43035aefb655Srie 		 * Determine the number of relocations available.
43045aefb655Srie 		 */
43055aefb655Srie 		if (shdr->sh_size == 0) {
43067c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
43077c478bd9Sstevel@tonic-gate 			    file, _cache->c_name);
43087c478bd9Sstevel@tonic-gate 			continue;
43097c478bd9Sstevel@tonic-gate 		}
43109a411307Srie 		if (_cache->c_data == NULL)
43119a411307Srie 			continue;
43129a411307Srie 
43135aefb655Srie 		rels = _cache->c_data->d_buf;
43145aefb655Srie 		relnum = shdr->sh_size / relsize;
43157c478bd9Sstevel@tonic-gate 
43167c478bd9Sstevel@tonic-gate 		/*
43175aefb655Srie 		 * Get the data buffer for the associated symbol table and
43185aefb655Srie 		 * string table.
43197c478bd9Sstevel@tonic-gate 		 */
43205aefb655Srie 		if (stringtbl(cache, 1, cnt, shnum, file,
43215aefb655Srie 		    &symnum, &symsec, &strsec) == 0)
43227c478bd9Sstevel@tonic-gate 			continue;
43237c478bd9Sstevel@tonic-gate 
43245aefb655Srie 		syms = symsec->c_data->d_buf;
43255aefb655Srie 
43267c478bd9Sstevel@tonic-gate 		/*
43275aefb655Srie 		 * Loop through the relocation entries.
43287c478bd9Sstevel@tonic-gate 		 */
43295aefb655Srie 		for (relndx = 0; relndx < relnum; relndx++,
43305aefb655Srie 		    rels = (void *)((char *)rels + relsize)) {
43315aefb655Srie 			char		section[BUFSIZ];
43325aefb655Srie 			Addr		offset;
43337c478bd9Sstevel@tonic-gate 			Got_info	*gip;
43345aefb655Srie 			Word		symndx, reltype;
43355aefb655Srie 			Rela		*rela;
43365aefb655Srie 			Rel		*rel;
43377c478bd9Sstevel@tonic-gate 
43385aefb655Srie 			/*
43395aefb655Srie 			 * Unravel the relocation.
43405aefb655Srie 			 */
43415aefb655Srie 			if (type == SHT_RELA) {
43425aefb655Srie 				rela = (Rela *)rels;
43435aefb655Srie 				symndx = ELF_R_SYM(rela->r_info);
4344ba2be530Sab196087 				reltype = ELF_R_TYPE(rela->r_info,
4345ba2be530Sab196087 				    ehdr->e_machine);
43465aefb655Srie 				offset = rela->r_offset;
43477c478bd9Sstevel@tonic-gate 			} else {
43485aefb655Srie 				rel = (Rel *)rels;
43495aefb655Srie 				symndx = ELF_R_SYM(rel->r_info);
4350ba2be530Sab196087 				reltype = ELF_R_TYPE(rel->r_info,
4351ba2be530Sab196087 				    ehdr->e_machine);
43525aefb655Srie 				offset = rel->r_offset;
43537c478bd9Sstevel@tonic-gate 			}
43547c478bd9Sstevel@tonic-gate 
43557c478bd9Sstevel@tonic-gate 			/*
43567c478bd9Sstevel@tonic-gate 			 * Only pay attention to relocations against the GOT.
43577c478bd9Sstevel@tonic-gate 			 */
4358c22c4bcbSab196087 			if ((offset < gotbgn) || (offset >= gotend))
43597c478bd9Sstevel@tonic-gate 				continue;
43607c478bd9Sstevel@tonic-gate 
43617c478bd9Sstevel@tonic-gate 			/* LINTED */
43625aefb655Srie 			gotndx = (Word)((offset - gotbgn) /
43637c478bd9Sstevel@tonic-gate 			    gotshdr->sh_entsize);
43647c478bd9Sstevel@tonic-gate 			gip = &gottable[gotndx];
43655aefb655Srie 
43665aefb655Srie 			if (gip->g_reltype != 0) {
43677c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
43687c478bd9Sstevel@tonic-gate 				    MSG_INTL(MSG_GOT_MULTIPLE), file,
43695aefb655Srie 				    EC_WORD(gotndx), EC_ADDR(offset));
43707c478bd9Sstevel@tonic-gate 				continue;
43717c478bd9Sstevel@tonic-gate 			}
43727c478bd9Sstevel@tonic-gate 
43735aefb655Srie 			if (symndx)
43745aefb655Srie 				gip->g_symname = relsymname(cache, _cache,
43755aefb655Srie 				    strsec, symndx, symnum, relndx, syms,
43760e233487SRod Evans 				    section, BUFSIZ, file);
43775aefb655Srie 			gip->g_reltype = reltype;
43785aefb655Srie 			gip->g_rel = rels;
43797c478bd9Sstevel@tonic-gate 		}
43807c478bd9Sstevel@tonic-gate 	}
43817c478bd9Sstevel@tonic-gate 
438231dd2c84SAli Bahrami 	if (symlookup(MSG_ORIG(MSG_SYM_GOT), cache, shnum, &gotsym, NULL,
438331dd2c84SAli Bahrami 	    symtab, file))
43845aefb655Srie 		gotsymaddr = gotsym->st_value;
43857c478bd9Sstevel@tonic-gate 	else
43865aefb655Srie 		gotsymaddr = gotbgn;
43877c478bd9Sstevel@tonic-gate 
43885aefb655Srie 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
43895aefb655Srie 	dbg_print(0, MSG_INTL(MSG_ELF_SCN_GOT), gotcache->c_name);
43905aefb655Srie 	Elf_got_title(0);
43917c478bd9Sstevel@tonic-gate 
4392ba2be530Sab196087 	sys_encoding = _elf_sys_encoding();
43937c478bd9Sstevel@tonic-gate 	for (gotndx = 0; gotndx < gotents; gotndx++) {
43947c478bd9Sstevel@tonic-gate 		Got_info	*gip;
43957c478bd9Sstevel@tonic-gate 		Sword		gindex;
43965aefb655Srie 		Addr		gaddr;
43975aefb655Srie 		Xword		gotentry;
43987c478bd9Sstevel@tonic-gate 
43997c478bd9Sstevel@tonic-gate 		gip = &gottable[gotndx];
44007c478bd9Sstevel@tonic-gate 
44017c478bd9Sstevel@tonic-gate 		gaddr = gotbgn + (gotndx * gentsize);
44025aefb655Srie 		gindex = (Sword)(gaddr - gotsymaddr) / (Sword)gentsize;
44037c478bd9Sstevel@tonic-gate 
44045aefb655Srie 		if (gentsize == sizeof (Word))
44057c478bd9Sstevel@tonic-gate 			/* LINTED */
44065aefb655Srie 			gotentry = (Xword)(*((Word *)(gotdata) + gotndx));
44077c478bd9Sstevel@tonic-gate 		else
44087c478bd9Sstevel@tonic-gate 			/* LINTED */
44095aefb655Srie 			gotentry = *((Xword *)(gotdata) + gotndx);
44107c478bd9Sstevel@tonic-gate 
44115aefb655Srie 		Elf_got_entry(0, gindex, gaddr, gotentry, ehdr->e_machine,
4412ba2be530Sab196087 		    ehdr->e_ident[EI_DATA], sys_encoding,
44135aefb655Srie 		    gip->g_reltype, gip->g_rel, gip->g_symname);
44147c478bd9Sstevel@tonic-gate 	}
44157c478bd9Sstevel@tonic-gate 	free(gottable);
44167c478bd9Sstevel@tonic-gate }
44177c478bd9Sstevel@tonic-gate 
44187c478bd9Sstevel@tonic-gate void
44197c478bd9Sstevel@tonic-gate checksum(Elf *elf)
44207c478bd9Sstevel@tonic-gate {
44215aefb655Srie 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
44225aefb655Srie 	dbg_print(0, MSG_INTL(MSG_STR_CHECKSUM), elf_checksum(elf));
44237c478bd9Sstevel@tonic-gate }
44247c478bd9Sstevel@tonic-gate 
44254fa4b40cSab196087 /*
44264fa4b40cSab196087  * This variable is used by regular() to communicate the address of
44274fa4b40cSab196087  * the section header cache to sort_shdr_ndx_arr(). Unfortunately,
44284fa4b40cSab196087  * the qsort() interface does not include a userdata argument by which
44294fa4b40cSab196087  * such arbitrary data can be passed, so we are stuck using global data.
44304fa4b40cSab196087  */
44314fa4b40cSab196087 static Cache *sort_shdr_ndx_arr_cache;
44324fa4b40cSab196087 
44334fa4b40cSab196087 
44344fa4b40cSab196087 /*
44354fa4b40cSab196087  * Used with qsort() to sort the section indices so that they can be
44364fa4b40cSab196087  * used to access the section headers in order of increasing data offset.
44374fa4b40cSab196087  *
44384fa4b40cSab196087  * entry:
44394fa4b40cSab196087  *	sort_shdr_ndx_arr_cache - Contains address of
44404fa4b40cSab196087  *		section header cache.
44414fa4b40cSab196087  *	v1, v2 - Point at elements of sort_shdr_bits array to be compared.
44424fa4b40cSab196087  *
44434fa4b40cSab196087  * exit:
44444fa4b40cSab196087  *	Returns -1 (less than), 0 (equal) or 1 (greater than).
44454fa4b40cSab196087  */
44464fa4b40cSab196087 static int
44474fa4b40cSab196087 sort_shdr_ndx_arr(const void *v1, const void *v2)
44484fa4b40cSab196087 {
44494fa4b40cSab196087 	Cache	*cache1 = sort_shdr_ndx_arr_cache + *((size_t *)v1);
44504fa4b40cSab196087 	Cache	*cache2 = sort_shdr_ndx_arr_cache + *((size_t *)v2);
44514fa4b40cSab196087 
44524fa4b40cSab196087 	if (cache1->c_shdr->sh_offset < cache2->c_shdr->sh_offset)
44534fa4b40cSab196087 		return (-1);
44544fa4b40cSab196087 
44554fa4b40cSab196087 	if (cache1->c_shdr->sh_offset > cache2->c_shdr->sh_offset)
44564fa4b40cSab196087 		return (1);
44574fa4b40cSab196087 
44584fa4b40cSab196087 	return (0);
44594fa4b40cSab196087 }
44604fa4b40cSab196087 
44614fa4b40cSab196087 
446239773e46Sab196087 static int
446339773e46Sab196087 shdr_cache(const char *file, Elf *elf, Ehdr *ehdr, size_t shstrndx,
44640e233487SRod Evans     size_t shnum, Cache **cache_ret, Word flags)
44657c478bd9Sstevel@tonic-gate {
44667c478bd9Sstevel@tonic-gate 	Elf_Scn		*scn;
44677c478bd9Sstevel@tonic-gate 	Elf_Data	*data;
446839773e46Sab196087 	size_t		ndx;
446939773e46Sab196087 	Shdr		*nameshdr;
44707e16fca0SAli Bahrami 	char		*names = NULL;
44717c478bd9Sstevel@tonic-gate 	Cache		*cache, *_cache;
44724fa4b40cSab196087 	size_t		*shdr_ndx_arr, shdr_ndx_arr_cnt;
44737c478bd9Sstevel@tonic-gate 
44747c478bd9Sstevel@tonic-gate 
44757c478bd9Sstevel@tonic-gate 	/*
44767c478bd9Sstevel@tonic-gate 	 * Obtain the .shstrtab data buffer to provide the required section
44777c478bd9Sstevel@tonic-gate 	 * name strings.
44787c478bd9Sstevel@tonic-gate 	 */
44792eec250aSab196087 	if (shstrndx == SHN_UNDEF) {
44802eec250aSab196087 		/*
44812eec250aSab196087 		 * It is rare, but legal, for an object to lack a
44822eec250aSab196087 		 * header string table section.
44832eec250aSab196087 		 */
44842eec250aSab196087 		names = NULL;
44852eec250aSab196087 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_NOSHSTRSEC), file);
44862eec250aSab196087 	} else if ((scn = elf_getscn(elf, shstrndx)) == NULL) {
44877c478bd9Sstevel@tonic-gate 		failure(file, MSG_ORIG(MSG_ELF_GETSCN));
44887c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SHDR),
44897c478bd9Sstevel@tonic-gate 		    EC_XWORD(shstrndx));
44905aefb655Srie 
44917c478bd9Sstevel@tonic-gate 	} else if ((data = elf_getdata(scn, NULL)) == NULL) {
44927c478bd9Sstevel@tonic-gate 		failure(file, MSG_ORIG(MSG_ELF_GETDATA));
44937c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_DATA),
44947c478bd9Sstevel@tonic-gate 		    EC_XWORD(shstrndx));
44955aefb655Srie 
44965aefb655Srie 	} else if ((nameshdr = elf_getshdr(scn)) == NULL) {
44977c478bd9Sstevel@tonic-gate 		failure(file, MSG_ORIG(MSG_ELF_GETSHDR));
44987c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN),
4499f5a18a30Srie 		    EC_WORD(elf_ndxscn(scn)));
45005aefb655Srie 
45017e16fca0SAli Bahrami 	} else if ((names = data->d_buf) == NULL)
45027c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_SHSTRNULL), file);
45037c478bd9Sstevel@tonic-gate 
45047c478bd9Sstevel@tonic-gate 	/*
4505f5a18a30Srie 	 * Allocate a cache to maintain a descriptor for each section.
45067c478bd9Sstevel@tonic-gate 	 */
450739773e46Sab196087 	if ((*cache_ret = cache = malloc(shnum * sizeof (Cache))) == NULL) {
45087c478bd9Sstevel@tonic-gate 		int err = errno;
45097c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
45107c478bd9Sstevel@tonic-gate 		    file, strerror(err));
451139773e46Sab196087 		return (0);
45127c478bd9Sstevel@tonic-gate 	}
45137c478bd9Sstevel@tonic-gate 
45145aefb655Srie 	*cache = cache_init;
45157c478bd9Sstevel@tonic-gate 	_cache = cache;
45167c478bd9Sstevel@tonic-gate 	_cache++;
45177c478bd9Sstevel@tonic-gate 
4518f5a18a30Srie 	/*
45194fa4b40cSab196087 	 * Allocate an array that will hold the section index for
45204fa4b40cSab196087 	 * each section that has data in the ELF file:
45214fa4b40cSab196087 	 *
45224fa4b40cSab196087 	 *	- Is not a NOBITS section
45234fa4b40cSab196087 	 *	- Data has non-zero length
45244fa4b40cSab196087 	 *
45254fa4b40cSab196087 	 * Note that shnum is an upper bound on the size required. It
45264fa4b40cSab196087 	 * is likely that we won't use a few of these array elements.
45274fa4b40cSab196087 	 * Allocating a modest amount of extra memory in this case means
45284fa4b40cSab196087 	 * that we can avoid an extra loop to count the number of needed
45294fa4b40cSab196087 	 * items, and can fill this array immediately in the first loop
45304fa4b40cSab196087 	 * below.
45314fa4b40cSab196087 	 */
45324fa4b40cSab196087 	if ((shdr_ndx_arr = malloc(shnum * sizeof (*shdr_ndx_arr))) == NULL) {
45334fa4b40cSab196087 		int err = errno;
45344fa4b40cSab196087 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
45354fa4b40cSab196087 		    file, strerror(err));
453639773e46Sab196087 		return (0);
45374fa4b40cSab196087 	}
45384fa4b40cSab196087 	shdr_ndx_arr_cnt = 0;
45394fa4b40cSab196087 
45404fa4b40cSab196087 	/*
4541f5a18a30Srie 	 * Traverse the sections of the file.  This gathering of data is
4542f5a18a30Srie 	 * carried out in two passes.  First, the section headers are captured
4543f5a18a30Srie 	 * and the section header names are evaluated.  A verification pass is
4544f5a18a30Srie 	 * then carried out over the section information.  Files have been
4545f5a18a30Srie 	 * known to exhibit overlapping (and hence erroneous) section header
4546f5a18a30Srie 	 * information.
4547f5a18a30Srie 	 *
4548f5a18a30Srie 	 * Finally, the data for each section is obtained.  This processing is
4549f5a18a30Srie 	 * carried out after section verification because should any section
4550f5a18a30Srie 	 * header overlap occur, and a file needs translating (ie. xlate'ing
4551f5a18a30Srie 	 * information from a non-native architecture file), then the process
4552f5a18a30Srie 	 * of translation can corrupt the section header information.  Of
4553f5a18a30Srie 	 * course, if there is any section overlap, the data related to the
4554f5a18a30Srie 	 * sections is going to be compromised.  However, it is the translation
4555f5a18a30Srie 	 * of this data that has caused problems with elfdump()'s ability to
4556f5a18a30Srie 	 * extract the data.
4557f5a18a30Srie 	 */
45584fa4b40cSab196087 	for (ndx = 1, scn = NULL; scn = elf_nextscn(elf, scn);
45594fa4b40cSab196087 	    ndx++, _cache++) {
4560f5a18a30Srie 		char	scnndxnm[100];
4561f5a18a30Srie 
45624fa4b40cSab196087 		_cache->c_ndx = ndx;
4563f5a18a30Srie 		_cache->c_scn = scn;
4564f5a18a30Srie 
45655aefb655Srie 		if ((_cache->c_shdr = elf_getshdr(scn)) == NULL) {
45667c478bd9Sstevel@tonic-gate 			failure(file, MSG_ORIG(MSG_ELF_GETSHDR));
45677c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN),
4568f5a18a30Srie 			    EC_WORD(elf_ndxscn(scn)));
45697c478bd9Sstevel@tonic-gate 		}
45707c478bd9Sstevel@tonic-gate 
4571f5a18a30Srie 		/*
45724fa4b40cSab196087 		 * If this section has data in the file, include it in
45734fa4b40cSab196087 		 * the array of sections to check for address overlap.
45744fa4b40cSab196087 		 */
45754fa4b40cSab196087 		if ((_cache->c_shdr->sh_size != 0) &&
45764fa4b40cSab196087 		    (_cache->c_shdr->sh_type != SHT_NOBITS))
45774fa4b40cSab196087 			shdr_ndx_arr[shdr_ndx_arr_cnt++] = ndx;
45784fa4b40cSab196087 
45794fa4b40cSab196087 		/*
4580f5a18a30Srie 		 * If a shstrtab exists, assign the section name.
4581f5a18a30Srie 		 */
4582f5a18a30Srie 		if (names && _cache->c_shdr) {
4583f5a18a30Srie 			if (_cache->c_shdr->sh_name &&
45847c478bd9Sstevel@tonic-gate 			    /* LINTED */
4585f5a18a30Srie 			    (nameshdr->sh_size > _cache->c_shdr->sh_name)) {
45860e233487SRod Evans 				const char	*symname;
45870e233487SRod Evans 				char		*secname;
45880e233487SRod Evans 
45890e233487SRod Evans 				secname = names + _cache->c_shdr->sh_name;
45900e233487SRod Evans 
45910e233487SRod Evans 				/*
45920e233487SRod Evans 				 * A SUN naming convention employs a "%" within
45930e233487SRod Evans 				 * a section name to indicate a section/symbol
45940e233487SRod Evans 				 * name.  This originated from the compilers
45950e233487SRod Evans 				 * -xF option, that places functions into their
45960e233487SRod Evans 				 * own sections.  This convention (which has no
45970e233487SRod Evans 				 * formal standard) has also been followed for
45980e233487SRod Evans 				 * COMDAT sections.  To demangle the symbol
45990e233487SRod Evans 				 * name, the name must be separated from the
46000e233487SRod Evans 				 * section name.
46010e233487SRod Evans 				 */
46020e233487SRod Evans 				if (((flags & FLG_CTL_DEMANGLE) == 0) ||
46030e233487SRod Evans 				    ((symname = strchr(secname, '%')) == NULL))
46040e233487SRod Evans 					_cache->c_name = secname;
46050e233487SRod Evans 				else {
46060e233487SRod Evans 					size_t	secsz = ++symname - secname;
46070e233487SRod Evans 					size_t	strsz;
46080e233487SRod Evans 
46090e233487SRod Evans 					symname = demangle(symname, flags);
46100e233487SRod Evans 					strsz = secsz + strlen(symname) + 1;
46110e233487SRod Evans 
46120e233487SRod Evans 					if ((_cache->c_name =
46130e233487SRod Evans 					    malloc(strsz)) == NULL) {
46140e233487SRod Evans 						int err = errno;
46150e233487SRod Evans 						(void) fprintf(stderr,
46160e233487SRod Evans 						    MSG_INTL(MSG_ERR_MALLOC),
46170e233487SRod Evans 						    file, strerror(err));
46180e233487SRod Evans 						return (0);
46190e233487SRod Evans 					}
46200e233487SRod Evans 					(void) snprintf(_cache->c_name, strsz,
46210e233487SRod Evans 					    MSG_ORIG(MSG_FMT_SECSYM),
46220e233487SRod Evans 					    EC_WORD(secsz), secname, symname);
46230e233487SRod Evans 				}
46247e16fca0SAli Bahrami 
4625f5a18a30Srie 				continue;
4626f5a18a30Srie 			}
46277c478bd9Sstevel@tonic-gate 
46287c478bd9Sstevel@tonic-gate 			/*
4629f5a18a30Srie 			 * Generate an error if the section name index is zero
4630f5a18a30Srie 			 * or exceeds the shstrtab data.  Fall through to
4631f5a18a30Srie 			 * fabricate a section name.
46327c478bd9Sstevel@tonic-gate 			 */
4633f5a18a30Srie 			if ((_cache->c_shdr->sh_name == 0) ||
46347c478bd9Sstevel@tonic-gate 			    /* LINTED */
46355aefb655Srie 			    (nameshdr->sh_size <= _cache->c_shdr->sh_name)) {
46367c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
46377c478bd9Sstevel@tonic-gate 				    MSG_INTL(MSG_ERR_BADSHNAME), file,
46384fa4b40cSab196087 				    EC_WORD(ndx),
46395aefb655Srie 				    EC_XWORD(_cache->c_shdr->sh_name));
46407c478bd9Sstevel@tonic-gate 			}
4641f5a18a30Srie 		}
46427c478bd9Sstevel@tonic-gate 
4643f5a18a30Srie 		/*
4644f5a18a30Srie 		 * If there exists no shstrtab data, or a section header has no
4645f5a18a30Srie 		 * name (an invalid index of 0), then compose a name for the
4646f5a18a30Srie 		 * section.
4647f5a18a30Srie 		 */
4648f5a18a30Srie 		(void) snprintf(scnndxnm, sizeof (scnndxnm),
46494fa4b40cSab196087 		    MSG_INTL(MSG_FMT_SCNNDX), ndx);
4650f5a18a30Srie 
46514fa4b40cSab196087 		if ((_cache->c_name = malloc(strlen(scnndxnm) + 1)) == NULL) {
46527c478bd9Sstevel@tonic-gate 			int err = errno;
46537c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
46547c478bd9Sstevel@tonic-gate 			    file, strerror(err));
465539773e46Sab196087 			return (0);
46567c478bd9Sstevel@tonic-gate 		}
46577c478bd9Sstevel@tonic-gate 		(void) strcpy(_cache->c_name, scnndxnm);
46587c478bd9Sstevel@tonic-gate 	}
46597c478bd9Sstevel@tonic-gate 
4660f5a18a30Srie 	/*
4661f5a18a30Srie 	 * Having collected all the sections, validate their address range.
4662f5a18a30Srie 	 * Cases have existed where the section information has been invalid.
4663f5a18a30Srie 	 * This can lead to all sorts of other, hard to diagnose errors, as
4664f5a18a30Srie 	 * each section is processed individually (ie. with elf_getdata()).
4665f5a18a30Srie 	 * Here, we carry out some address comparisons to catch a family of
4666f5a18a30Srie 	 * overlapping memory issues we have observed (likely, there are others
4667f5a18a30Srie 	 * that we have yet to discover).
4668f5a18a30Srie 	 *
4669f5a18a30Srie 	 * Note, should any memory overlap occur, obtaining any additional
4670f5a18a30Srie 	 * data from the file is questionable.  However, it might still be
4671f5a18a30Srie 	 * possible to inspect the ELF header, Programs headers, or individual
4672f5a18a30Srie 	 * sections, so rather than bailing on an error condition, continue
4673f5a18a30Srie 	 * processing to see if any data can be salvaged.
4674f5a18a30Srie 	 */
46754fa4b40cSab196087 	if (shdr_ndx_arr_cnt > 1) {
46764fa4b40cSab196087 		sort_shdr_ndx_arr_cache = cache;
46774fa4b40cSab196087 		qsort(shdr_ndx_arr, shdr_ndx_arr_cnt,
46784fa4b40cSab196087 		    sizeof (*shdr_ndx_arr), sort_shdr_ndx_arr);
46794fa4b40cSab196087 	}
46804fa4b40cSab196087 	for (ndx = 0; ndx < shdr_ndx_arr_cnt; ndx++) {
46814fa4b40cSab196087 		Cache	*_cache = cache + shdr_ndx_arr[ndx];
4682f5a18a30Srie 		Shdr	*shdr = _cache->c_shdr;
4683f5a18a30Srie 		Off	bgn1, bgn = shdr->sh_offset;
4684f5a18a30Srie 		Off	end1, end = shdr->sh_offset + shdr->sh_size;
46854fa4b40cSab196087 		size_t	ndx1;
4686f5a18a30Srie 
46874fa4b40cSab196087 		/*
46884fa4b40cSab196087 		 * Check the section against all following ones, reporting
46894fa4b40cSab196087 		 * any overlaps. Since we've sorted the sections by offset,
46904fa4b40cSab196087 		 * we can stop after the first comparison that fails. There
46914fa4b40cSab196087 		 * are no overlaps in a properly formed ELF file, in which
46924fa4b40cSab196087 		 * case this algorithm runs in O(n) time. This will degenerate
46934fa4b40cSab196087 		 * to O(n^2) for a completely broken file. Such a file is
46944fa4b40cSab196087 		 * (1) highly unlikely, and (2) unusable, so it is reasonable
46954fa4b40cSab196087 		 * for the analysis to take longer.
46964fa4b40cSab196087 		 */
46974fa4b40cSab196087 		for (ndx1 = ndx + 1; ndx1 < shdr_ndx_arr_cnt; ndx1++) {
46984fa4b40cSab196087 			Cache	*_cache1 = cache + shdr_ndx_arr[ndx1];
4699f5a18a30Srie 			Shdr	*shdr1 = _cache1->c_shdr;
4700f5a18a30Srie 
4701f5a18a30Srie 			bgn1 = shdr1->sh_offset;
4702f5a18a30Srie 			end1 = shdr1->sh_offset + shdr1->sh_size;
4703f5a18a30Srie 
4704f5a18a30Srie 			if (((bgn1 <= bgn) && (end1 > bgn)) ||
4705f5a18a30Srie 			    ((bgn1 < end) && (end1 >= end))) {
4706f5a18a30Srie 				(void) fprintf(stderr,
4707f5a18a30Srie 				    MSG_INTL(MSG_ERR_SECMEMOVER), file,
4708f5a18a30Srie 				    EC_WORD(elf_ndxscn(_cache->c_scn)),
47094fa4b40cSab196087 				    _cache->c_name, EC_OFF(bgn), EC_OFF(end),
47104fa4b40cSab196087 				    EC_WORD(elf_ndxscn(_cache1->c_scn)),
47114fa4b40cSab196087 				    _cache1->c_name, EC_OFF(bgn1),
47124fa4b40cSab196087 				    EC_OFF(end1));
47134fa4b40cSab196087 			} else {	/* No overlap, so can stop */
47144fa4b40cSab196087 				break;
4715f5a18a30Srie 			}
4716f5a18a30Srie 		}
4717f5a18a30Srie 
4718f5a18a30Srie 		/*
47194fa4b40cSab196087 		 * In addition to checking for sections overlapping
47204fa4b40cSab196087 		 * each other (done above), we should also make sure
47214fa4b40cSab196087 		 * the section doesn't overlap the section header array.
4722f5a18a30Srie 		 */
4723f5a18a30Srie 		bgn1 = ehdr->e_shoff;
4724f5a18a30Srie 		end1 = ehdr->e_shoff + (ehdr->e_shentsize * ehdr->e_shnum);
4725f5a18a30Srie 
4726f5a18a30Srie 		if (((bgn1 <= bgn) && (end1 > bgn)) ||
4727f5a18a30Srie 		    ((bgn1 < end) && (end1 >= end))) {
4728f5a18a30Srie 			(void) fprintf(stderr,
4729f5a18a30Srie 			    MSG_INTL(MSG_ERR_SHDRMEMOVER), file, EC_OFF(bgn1),
4730f5a18a30Srie 			    EC_OFF(end1),
4731f5a18a30Srie 			    EC_WORD(elf_ndxscn(_cache->c_scn)),
4732f5a18a30Srie 			    _cache->c_name, EC_OFF(bgn), EC_OFF(end));
4733f5a18a30Srie 		}
4734f5a18a30Srie 	}
4735f5a18a30Srie 
4736f5a18a30Srie 	/*
47374fa4b40cSab196087 	 * Obtain the data for each section.
4738f5a18a30Srie 	 */
47394fa4b40cSab196087 	for (ndx = 1; ndx < shnum; ndx++) {
47404fa4b40cSab196087 		Cache	*_cache = &cache[ndx];
4741f5a18a30Srie 		Elf_Scn	*scn = _cache->c_scn;
4742f5a18a30Srie 
47437c478bd9Sstevel@tonic-gate 		if ((_cache->c_data = elf_getdata(scn, NULL)) == NULL) {
47447c478bd9Sstevel@tonic-gate 			failure(file, MSG_ORIG(MSG_ELF_GETDATA));
47457c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCNDATA),
4746f5a18a30Srie 			    EC_WORD(elf_ndxscn(scn)));
47477c478bd9Sstevel@tonic-gate 		}
47481da7e599SAli Bahrami 
47491da7e599SAli Bahrami 		/*
47501da7e599SAli Bahrami 		 * If a string table, verify that it has NULL first and
47511da7e599SAli Bahrami 		 * final bytes.
47521da7e599SAli Bahrami 		 */
47531da7e599SAli Bahrami 		if ((_cache->c_shdr->sh_type == SHT_STRTAB) &&
4754*9bc2928dSBryan Cantrill 		    (_cache->c_data != NULL) &&
47551da7e599SAli Bahrami 		    (_cache->c_data->d_buf != NULL) &&
47561da7e599SAli Bahrami 		    (_cache->c_data->d_size > 0)) {
47571da7e599SAli Bahrami 			const char *s = _cache->c_data->d_buf;
47581da7e599SAli Bahrami 
47591da7e599SAli Bahrami 			if ((*s != '\0') ||
47601da7e599SAli Bahrami 			    (*(s + _cache->c_data->d_size - 1) != '\0'))
47611da7e599SAli Bahrami 				(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALSTR),
47621da7e599SAli Bahrami 				    file, _cache->c_name);
47631da7e599SAli Bahrami 		}
476439773e46Sab196087 	}
476539773e46Sab196087 
476639773e46Sab196087 	return (1);
476739773e46Sab196087 }
476839773e46Sab196087 
476939773e46Sab196087 
477039773e46Sab196087 
47714f680cc6SAli Bahrami /*
47724f680cc6SAli Bahrami  * Generate a cache of section headers and related information
47734f680cc6SAli Bahrami  * for use by the rest of elfdump. If requested (or the file
47744f680cc6SAli Bahrami  * contains no section headers), we generate a fake set of
47754f680cc6SAli Bahrami  * headers from the information accessible from the program headers.
47764f680cc6SAli Bahrami  * Otherwise, we use the real section headers contained in the file.
47774f680cc6SAli Bahrami  */
47784f680cc6SAli Bahrami static int
47794f680cc6SAli Bahrami create_cache(const char *file, int fd, Elf *elf, Ehdr *ehdr, Cache **cache,
47804f680cc6SAli Bahrami     size_t shstrndx, size_t *shnum, uint_t *flags)
47814f680cc6SAli Bahrami {
47824f680cc6SAli Bahrami 	/*
47834f680cc6SAli Bahrami 	 * If there are no section headers, then resort to synthesizing
47844f680cc6SAli Bahrami 	 * section headers from the program headers. This is normally
47854f680cc6SAli Bahrami 	 * only done by explicit request, but in this case there's no
47864f680cc6SAli Bahrami 	 * reason not to go ahead, since the alternative is simply to quit.
47874f680cc6SAli Bahrami 	 */
47884f680cc6SAli Bahrami 	if ((*shnum <= 1) && ((*flags & FLG_CTL_FAKESHDR) == 0)) {
47894f680cc6SAli Bahrami 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_NOSHDR), file);
47904f680cc6SAli Bahrami 		*flags |= FLG_CTL_FAKESHDR;
47914f680cc6SAli Bahrami 	}
47924f680cc6SAli Bahrami 
47934f680cc6SAli Bahrami 	if (*flags & FLG_CTL_FAKESHDR) {
47944f680cc6SAli Bahrami 		if (fake_shdr_cache(file, fd, elf, ehdr, cache, shnum) == 0)
47954f680cc6SAli Bahrami 			return (0);
47964f680cc6SAli Bahrami 	} else {
47974f680cc6SAli Bahrami 		if (shdr_cache(file, elf, ehdr, shstrndx, *shnum,
47984f680cc6SAli Bahrami 		    cache, *flags) == 0)
47994f680cc6SAli Bahrami 			return (0);
48004f680cc6SAli Bahrami 	}
48014f680cc6SAli Bahrami 
48024f680cc6SAli Bahrami 	return (1);
48034f680cc6SAli Bahrami }
48044f680cc6SAli Bahrami 
4805981a172dSab196087 int
4806981a172dSab196087 regular(const char *file, int fd, Elf *elf, uint_t flags,
48074f680cc6SAli Bahrami     const char *wname, int wfd, uchar_t osabi)
480839773e46Sab196087 {
48094f680cc6SAli Bahrami 	enum { CACHE_NEEDED, CACHE_OK, CACHE_FAIL} cache_state = CACHE_NEEDED;
481039773e46Sab196087 	Elf_Scn		*scn;
481139773e46Sab196087 	Ehdr		*ehdr;
481239773e46Sab196087 	size_t		ndx, shstrndx, shnum, phnum;
481339773e46Sab196087 	Shdr		*shdr;
481439773e46Sab196087 	Cache		*cache;
48158a8ef765SRichard Lowe 	VERSYM_STATE	versym = { 0 };
4816981a172dSab196087 	int		ret = 0;
4817981a172dSab196087 	int		addr_align;
481839773e46Sab196087 
481939773e46Sab196087 	if ((ehdr = elf_getehdr(elf)) == NULL) {
482039773e46Sab196087 		failure(file, MSG_ORIG(MSG_ELF_GETEHDR));
4821981a172dSab196087 		return (ret);
482239773e46Sab196087 	}
482339773e46Sab196087 
482462b628a6SAli Bahrami 	if (elf_getshdrnum(elf, &shnum) == -1) {
482562b628a6SAli Bahrami 		failure(file, MSG_ORIG(MSG_ELF_GETSHDRNUM));
4826981a172dSab196087 		return (ret);
482739773e46Sab196087 	}
482839773e46Sab196087 
482962b628a6SAli Bahrami 	if (elf_getshdrstrndx(elf, &shstrndx) == -1) {
483062b628a6SAli Bahrami 		failure(file, MSG_ORIG(MSG_ELF_GETSHDRSTRNDX));
4831981a172dSab196087 		return (ret);
483239773e46Sab196087 	}
483339773e46Sab196087 
483462b628a6SAli Bahrami 	if (elf_getphdrnum(elf, &phnum) == -1) {
483562b628a6SAli Bahrami 		failure(file, MSG_ORIG(MSG_ELF_GETPHDRNUM));
4836981a172dSab196087 		return (ret);
483739773e46Sab196087 	}
483839773e46Sab196087 	/*
483939773e46Sab196087 	 * If the user requested section headers derived from the
484039773e46Sab196087 	 * program headers (-P option) and this file doesn't have
484139773e46Sab196087 	 * any program headers (i.e. ET_REL), then we can't do it.
484239773e46Sab196087 	 */
4843981a172dSab196087 	if ((phnum == 0) && (flags & FLG_CTL_FAKESHDR)) {
484439773e46Sab196087 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_PNEEDSPH), file);
4845981a172dSab196087 		return (ret);
484639773e46Sab196087 	}
484739773e46Sab196087 
484839773e46Sab196087 
484939773e46Sab196087 	if ((scn = elf_getscn(elf, 0)) != NULL) {
485039773e46Sab196087 		if ((shdr = elf_getshdr(scn)) == NULL) {
485139773e46Sab196087 			failure(file, MSG_ORIG(MSG_ELF_GETSHDR));
485239773e46Sab196087 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN), 0);
4853981a172dSab196087 			return (ret);
485439773e46Sab196087 		}
485539773e46Sab196087 	} else
48567e16fca0SAli Bahrami 		shdr = NULL;
48577c478bd9Sstevel@tonic-gate 
48587c478bd9Sstevel@tonic-gate 	/*
485939773e46Sab196087 	 * Print the elf header.
48607c478bd9Sstevel@tonic-gate 	 */
4861981a172dSab196087 	if (flags & FLG_SHOW_EHDR)
486239773e46Sab196087 		Elf_ehdr(0, ehdr, shdr);
486339773e46Sab196087 
486439773e46Sab196087 	/*
486539773e46Sab196087 	 * If the section headers or program headers have inadequate
486639773e46Sab196087 	 * alignment for the class of object, print a warning. libelf
486739773e46Sab196087 	 * can handle such files, but programs that use them can crash
486839773e46Sab196087 	 * when they dereference unaligned items.
4869981a172dSab196087 	 *
4870981a172dSab196087 	 * Note that the AMD64 ABI, although it is a 64-bit architecture,
4871981a172dSab196087 	 * allows access to data types smaller than 128-bits to be on
4872981a172dSab196087 	 * word alignment.
487339773e46Sab196087 	 */
4874981a172dSab196087 	if (ehdr->e_machine == EM_AMD64)
4875981a172dSab196087 		addr_align = sizeof (Word);
4876981a172dSab196087 	else
4877981a172dSab196087 		addr_align = sizeof (Addr);
4878981a172dSab196087 
4879981a172dSab196087 	if (ehdr->e_phoff & (addr_align - 1))
488039773e46Sab196087 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADPHDRALIGN), file);
4881981a172dSab196087 	if (ehdr->e_shoff & (addr_align - 1))
488239773e46Sab196087 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHDRALIGN), file);
488339773e46Sab196087 
48844f680cc6SAli Bahrami 
48854f680cc6SAli Bahrami 	/*
48864f680cc6SAli Bahrami 	 * Determine the Operating System ABI (osabi) we will use to
48874f680cc6SAli Bahrami 	 * interpret the object.
48884f680cc6SAli Bahrami 	 */
48894f680cc6SAli Bahrami 	if (flags & FLG_CTL_OSABI) {
48904f680cc6SAli Bahrami 		/*
48914f680cc6SAli Bahrami 		 * If the user explicitly specifies '-O none', we need
48924f680cc6SAli Bahrami 		 * to display a completely generic view of the file.
48934f680cc6SAli Bahrami 		 * However, libconv is written to assume that ELFOSABI_NONE
48944f680cc6SAli Bahrami 		 * is equivalent to ELFOSABI_SOLARIS. To get the desired
48954f680cc6SAli Bahrami 		 * effect, we use an osabi that libconv has no knowledge of.
48964f680cc6SAli Bahrami 		 */
48974f680cc6SAli Bahrami 		if (osabi == ELFOSABI_NONE)
48984f680cc6SAli Bahrami 			osabi = ELFOSABI_UNKNOWN4;
48994f680cc6SAli Bahrami 	} else {
49004f680cc6SAli Bahrami 		/* Determine osabi from file */
49014f680cc6SAli Bahrami 		osabi = ehdr->e_ident[EI_OSABI];
49024f680cc6SAli Bahrami 		if (osabi == ELFOSABI_NONE) {
49034f680cc6SAli Bahrami 			/*
49044f680cc6SAli Bahrami 			 * Chicken/Egg scenario:
49054f680cc6SAli Bahrami 			 *
49064f680cc6SAli Bahrami 			 * Ideally, we wait to create the section header cache
49074f680cc6SAli Bahrami 			 * until after the program headers are printed. If we
49084f680cc6SAli Bahrami 			 * only output program headers, we can skip building
49094f680cc6SAli Bahrami 			 * the cache entirely.
49104f680cc6SAli Bahrami 			 *
49114f680cc6SAli Bahrami 			 * Proper interpretation of program headers requires
49124f680cc6SAli Bahrami 			 * the osabi, which is supposed to be in the ELF header.
49134f680cc6SAli Bahrami 			 * However, many systems (Solaris and Linux included)
49144f680cc6SAli Bahrami 			 * have a history of setting the osabi to the generic
49154f680cc6SAli Bahrami 			 * SysV ABI (ELFOSABI_NONE). We assume ELFOSABI_SOLARIS
49164f680cc6SAli Bahrami 			 * in such cases, but would like to check the object
49174f680cc6SAli Bahrami 			 * to see if it has a Linux .note.ABI-tag section,
49184f680cc6SAli Bahrami 			 * which implies ELFOSABI_LINUX. This requires a
49194f680cc6SAli Bahrami 			 * section header cache.
49204f680cc6SAli Bahrami 			 *
49214f680cc6SAli Bahrami 			 * To break the cycle, we create section headers now
49224f680cc6SAli Bahrami 			 * if osabi is ELFOSABI_NONE, and later otherwise.
49234f680cc6SAli Bahrami 			 * If it succeeds, we use them, if not, we defer
49244f680cc6SAli Bahrami 			 * exiting until after the program headers are out.
49254f680cc6SAli Bahrami 			 */
49264f680cc6SAli Bahrami 			if (create_cache(file, fd, elf, ehdr, &cache,
49274f680cc6SAli Bahrami 			    shstrndx, &shnum, &flags) == 0) {
49284f680cc6SAli Bahrami 				cache_state = CACHE_FAIL;
49294f680cc6SAli Bahrami 			} else {
49304f680cc6SAli Bahrami 				cache_state = CACHE_OK;
49314f680cc6SAli Bahrami 				if (has_linux_abi_note(cache, shnum, file)) {
49324f680cc6SAli Bahrami 					Conv_inv_buf_t	ibuf1, ibuf2;
49334f680cc6SAli Bahrami 
49344f680cc6SAli Bahrami 					(void) fprintf(stderr,
49354f680cc6SAli Bahrami 					    MSG_INTL(MSG_INFO_LINUXOSABI), file,
49364f680cc6SAli Bahrami 					    conv_ehdr_osabi(osabi, 0, &ibuf1),
49374f680cc6SAli Bahrami 					    conv_ehdr_osabi(ELFOSABI_LINUX,
49384f680cc6SAli Bahrami 					    0, &ibuf2));
49394f680cc6SAli Bahrami 					osabi = ELFOSABI_LINUX;
49404f680cc6SAli Bahrami 				}
49414f680cc6SAli Bahrami 			}
49424f680cc6SAli Bahrami 		}
49434f680cc6SAli Bahrami 		/*
49444f680cc6SAli Bahrami 		 * We treat ELFOSABI_NONE identically to ELFOSABI_SOLARIS.
49454f680cc6SAli Bahrami 		 * Mapping NONE to SOLARIS simplifies the required test.
49464f680cc6SAli Bahrami 		 */
49474f680cc6SAli Bahrami 		if (osabi == ELFOSABI_NONE)
49484f680cc6SAli Bahrami 			osabi = ELFOSABI_SOLARIS;
49494f680cc6SAli Bahrami 	}
49504f680cc6SAli Bahrami 
495139773e46Sab196087 	/*
495239773e46Sab196087 	 * Print the program headers.
495339773e46Sab196087 	 */
4954981a172dSab196087 	if ((flags & FLG_SHOW_PHDR) && (phnum != 0)) {
495539773e46Sab196087 		Phdr	*phdr;
495639773e46Sab196087 
495739773e46Sab196087 		if ((phdr = elf_getphdr(elf)) == NULL) {
495839773e46Sab196087 			failure(file, MSG_ORIG(MSG_ELF_GETPHDR));
4959981a172dSab196087 			return (ret);
496039773e46Sab196087 		}
496139773e46Sab196087 
496239773e46Sab196087 		for (ndx = 0; ndx < phnum; phdr++, ndx++) {
4963981a172dSab196087 			if (!match(MATCH_F_PHDR| MATCH_F_NDX | MATCH_F_TYPE,
4964981a172dSab196087 			    NULL, ndx, phdr->p_type))
496539773e46Sab196087 				continue;
496639773e46Sab196087 
496739773e46Sab196087 			dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
496839773e46Sab196087 			dbg_print(0, MSG_INTL(MSG_ELF_PHDR), EC_WORD(ndx));
49694f680cc6SAli Bahrami 			Elf_phdr(0, osabi, ehdr->e_machine, phdr);
497039773e46Sab196087 		}
497139773e46Sab196087 	}
497239773e46Sab196087 
497339773e46Sab196087 	/*
4974981a172dSab196087 	 * If we have flag bits set that explicitly require a show or calc
4975981a172dSab196087 	 * operation, but none of them require the section headers, then
4976981a172dSab196087 	 * we are done and can return now.
497739773e46Sab196087 	 */
4978981a172dSab196087 	if (((flags & (FLG_MASK_SHOW | FLG_MASK_CALC)) != 0) &&
4979981a172dSab196087 	    ((flags & (FLG_MASK_SHOW_SHDR | FLG_MASK_CALC_SHDR)) == 0))
4980981a172dSab196087 		return (ret);
498139773e46Sab196087 
498239773e46Sab196087 	/*
4983981a172dSab196087 	 * Everything from this point on requires section headers.
4984981a172dSab196087 	 * If we have no section headers, there is no reason to continue.
49854f680cc6SAli Bahrami 	 *
49864f680cc6SAli Bahrami 	 * If we tried above to create the section header cache and failed,
49874f680cc6SAli Bahrami 	 * it is time to exit. Otherwise, create it if needed.
4988981a172dSab196087 	 */
49894f680cc6SAli Bahrami 	switch (cache_state) {
49904f680cc6SAli Bahrami 	case CACHE_NEEDED:
49914f680cc6SAli Bahrami 		if (create_cache(file, fd, elf, ehdr, &cache, shstrndx,
49924f680cc6SAli Bahrami 		    &shnum, &flags) == 0)
49934f680cc6SAli Bahrami 			return (ret);
49944f680cc6SAli Bahrami 		break;
499537915d86SRichard Lowe 	case CACHE_OK:
499637915d86SRichard Lowe 		break;
49974f680cc6SAli Bahrami 	case CACHE_FAIL:
49984f680cc6SAli Bahrami 		return (ret);
49994f680cc6SAli Bahrami 	}
5000981a172dSab196087 	if (shnum <= 1)
5001981a172dSab196087 		goto done;
5002981a172dSab196087 
5003981a172dSab196087 	/*
500439773e46Sab196087 	 * If -w was specified, find and write out the section(s) data.
500539773e46Sab196087 	 */
500639773e46Sab196087 	if (wfd) {
500739773e46Sab196087 		for (ndx = 1; ndx < shnum; ndx++) {
500839773e46Sab196087 			Cache	*_cache = &cache[ndx];
500939773e46Sab196087 
5010981a172dSab196087 			if (match(MATCH_F_STRICT | MATCH_F_ALL, _cache->c_name,
5011981a172dSab196087 			    ndx, _cache->c_shdr->sh_type) &&
5012981a172dSab196087 			    _cache->c_data && _cache->c_data->d_buf) {
5013981a172dSab196087 				if (write(wfd, _cache->c_data->d_buf,
5014981a172dSab196087 				    _cache->c_data->d_size) !=
5015981a172dSab196087 				    _cache->c_data->d_size) {
5016981a172dSab196087 					int err = errno;
5017981a172dSab196087 					(void) fprintf(stderr,
5018981a172dSab196087 					    MSG_INTL(MSG_ERR_WRITE), wname,
5019981a172dSab196087 					    strerror(err));
5020981a172dSab196087 					/*
5021981a172dSab196087 					 * Return an exit status of 1, because
5022981a172dSab196087 					 * the failure is not related to the
5023981a172dSab196087 					 * ELF file, but by system resources.
5024981a172dSab196087 					 */
5025981a172dSab196087 					ret = 1;
5026981a172dSab196087 					goto done;
5027981a172dSab196087 				}
50287c478bd9Sstevel@tonic-gate 			}
50297c478bd9Sstevel@tonic-gate 		}
503039773e46Sab196087 	}
50317c478bd9Sstevel@tonic-gate 
5032981a172dSab196087 	/*
5033981a172dSab196087 	 * If we have no flag bits set that explicitly require a show or calc
5034981a172dSab196087 	 * operation, but match options (-I, -N, -T) were used, then run
5035981a172dSab196087 	 * through the section headers and see if we can't deduce show flags
5036981a172dSab196087 	 * from the match options given.
5037981a172dSab196087 	 *
5038981a172dSab196087 	 * We don't do this if -w was specified, because (-I, -N, -T) used
5039981a172dSab196087 	 * with -w in lieu of some other option is supposed to be quiet.
5040981a172dSab196087 	 */
5041981a172dSab196087 	if ((wfd == 0) && (flags & FLG_CTL_MATCH) &&
5042981a172dSab196087 	    ((flags & (FLG_MASK_SHOW | FLG_MASK_CALC)) == 0)) {
5043981a172dSab196087 		for (ndx = 1; ndx < shnum; ndx++) {
5044981a172dSab196087 			Cache	*_cache = &cache[ndx];
5045981a172dSab196087 
5046981a172dSab196087 			if (!match(MATCH_F_STRICT | MATCH_F_ALL, _cache->c_name,
5047981a172dSab196087 			    ndx, _cache->c_shdr->sh_type))
5048981a172dSab196087 				continue;
5049981a172dSab196087 
5050981a172dSab196087 			switch (_cache->c_shdr->sh_type) {
5051981a172dSab196087 			case SHT_PROGBITS:
5052981a172dSab196087 				/*
5053981a172dSab196087 				 * Heuristic time: It is usually bad form
50547e16fca0SAli Bahrami 				 * to assume the meaning/format of a PROGBITS
50557e16fca0SAli Bahrami 				 * section based on its name. However, there
5056e23c41c9SAli Bahrami 				 * are ABI mandated exceptions. Check for
5057e23c41c9SAli Bahrami 				 * these special names.
5058981a172dSab196087 				 */
5059e23c41c9SAli Bahrami 
5060e23c41c9SAli Bahrami 				/* The ELF ABI specifies .interp and .got */
5061981a172dSab196087 				if (strcmp(_cache->c_name,
5062e23c41c9SAli Bahrami 				    MSG_ORIG(MSG_ELF_INTERP)) == 0) {
5063981a172dSab196087 					flags |= FLG_SHOW_INTERP;
5064e23c41c9SAli Bahrami 					break;
5065e23c41c9SAli Bahrami 				}
5066e23c41c9SAli Bahrami 				if (strcmp(_cache->c_name,
5067e23c41c9SAli Bahrami 				    MSG_ORIG(MSG_ELF_GOT)) == 0) {
5068981a172dSab196087 					flags |= FLG_SHOW_GOT;
5069e23c41c9SAli Bahrami 					break;
5070e23c41c9SAli Bahrami 				}
5071e23c41c9SAli Bahrami 				/*
5072e23c41c9SAli Bahrami 				 * The GNU compilers, and amd64 ABI, define
5073e23c41c9SAli Bahrami 				 * .eh_frame and .eh_frame_hdr. The Sun
5074e23c41c9SAli Bahrami 				 * C++ ABI defines .exception_ranges.
5075e23c41c9SAli Bahrami 				 */
5076e23c41c9SAli Bahrami 				if ((strncmp(_cache->c_name,
50777e16fca0SAli Bahrami 				    MSG_ORIG(MSG_SCN_FRM),
5078e23c41c9SAli Bahrami 				    MSG_SCN_FRM_SIZE) == 0) ||
5079e23c41c9SAli Bahrami 				    (strncmp(_cache->c_name,
5080e23c41c9SAli Bahrami 				    MSG_ORIG(MSG_SCN_EXRANGE),
5081e23c41c9SAli Bahrami 				    MSG_SCN_EXRANGE_SIZE) == 0)) {
50827e16fca0SAli Bahrami 					flags |= FLG_SHOW_UNWIND;
5083981a172dSab196087 					break;
5084e23c41c9SAli Bahrami 				}
5085e23c41c9SAli Bahrami 				break;
5086981a172dSab196087 
5087981a172dSab196087 			case SHT_SYMTAB:
5088981a172dSab196087 			case SHT_DYNSYM:
5089981a172dSab196087 			case SHT_SUNW_LDYNSYM:
5090981a172dSab196087 			case SHT_SUNW_versym:
5091981a172dSab196087 			case SHT_SYMTAB_SHNDX:
5092981a172dSab196087 				flags |= FLG_SHOW_SYMBOLS;
5093981a172dSab196087 				break;
5094981a172dSab196087 
5095981a172dSab196087 			case SHT_RELA:
5096981a172dSab196087 			case SHT_REL:
5097981a172dSab196087 				flags |= FLG_SHOW_RELOC;
5098981a172dSab196087 				break;
5099981a172dSab196087 
5100981a172dSab196087 			case SHT_HASH:
5101981a172dSab196087 				flags |= FLG_SHOW_HASH;
5102981a172dSab196087 				break;
5103981a172dSab196087 
5104981a172dSab196087 			case SHT_DYNAMIC:
5105981a172dSab196087 				flags |= FLG_SHOW_DYNAMIC;
5106981a172dSab196087 				break;
5107981a172dSab196087 
5108981a172dSab196087 			case SHT_NOTE:
5109981a172dSab196087 				flags |= FLG_SHOW_NOTE;
5110981a172dSab196087 				break;
5111981a172dSab196087 
5112981a172dSab196087 			case SHT_GROUP:
5113981a172dSab196087 				flags |= FLG_SHOW_GROUP;
5114981a172dSab196087 				break;
5115981a172dSab196087 
5116981a172dSab196087 			case SHT_SUNW_symsort:
5117981a172dSab196087 			case SHT_SUNW_tlssort:
5118981a172dSab196087 				flags |= FLG_SHOW_SORT;
5119981a172dSab196087 				break;
5120981a172dSab196087 
5121981a172dSab196087 			case SHT_SUNW_cap:
5122981a172dSab196087 				flags |= FLG_SHOW_CAP;
5123981a172dSab196087 				break;
5124981a172dSab196087 
5125981a172dSab196087 			case SHT_SUNW_move:
5126981a172dSab196087 				flags |= FLG_SHOW_MOVE;
5127981a172dSab196087 				break;
5128981a172dSab196087 
5129981a172dSab196087 			case SHT_SUNW_syminfo:
5130981a172dSab196087 				flags |= FLG_SHOW_SYMINFO;
5131981a172dSab196087 				break;
5132981a172dSab196087 
5133981a172dSab196087 			case SHT_SUNW_verdef:
5134981a172dSab196087 			case SHT_SUNW_verneed:
5135981a172dSab196087 				flags |= FLG_SHOW_VERSIONS;
5136981a172dSab196087 				break;
5137981a172dSab196087 
5138981a172dSab196087 			case SHT_AMD64_UNWIND:
5139981a172dSab196087 				flags |= FLG_SHOW_UNWIND;
5140981a172dSab196087 				break;
5141981a172dSab196087 			}
5142981a172dSab196087 		}
5143981a172dSab196087 	}
5144981a172dSab196087 
5145981a172dSab196087 
5146981a172dSab196087 	if (flags & FLG_SHOW_SHDR)
51474f680cc6SAli Bahrami 		sections(file, cache, shnum, ehdr, osabi);
51487c478bd9Sstevel@tonic-gate 
5149981a172dSab196087 	if (flags & FLG_SHOW_INTERP)
51505aefb655Srie 		interp(file, cache, shnum, phnum, elf);
51517c478bd9Sstevel@tonic-gate 
51524f680cc6SAli Bahrami 	if ((osabi == ELFOSABI_SOLARIS) || (osabi == ELFOSABI_LINUX))
51533b41b08bSab196087 		versions(cache, shnum, file, flags, &versym);
51547c478bd9Sstevel@tonic-gate 
5155981a172dSab196087 	if (flags & FLG_SHOW_SYMBOLS)
51564f680cc6SAli Bahrami 		symbols(cache, shnum, ehdr, osabi, &versym, file, flags);
51577c478bd9Sstevel@tonic-gate 
51584f680cc6SAli Bahrami 	if ((flags & FLG_SHOW_SORT) && (osabi == ELFOSABI_SOLARIS))
51594f680cc6SAli Bahrami 		sunw_sort(cache, shnum, ehdr, osabi, &versym, file, flags);
5160d579eb63Sab196087 
5161981a172dSab196087 	if (flags & FLG_SHOW_HASH)
5162c809c407Sab196087 		hash(cache, shnum, file, flags);
51637c478bd9Sstevel@tonic-gate 
5164981a172dSab196087 	if (flags & FLG_SHOW_GOT)
51650e233487SRod Evans 		got(cache, shnum, ehdr, file);
51667c478bd9Sstevel@tonic-gate 
5167981a172dSab196087 	if (flags & FLG_SHOW_GROUP)
5168c809c407Sab196087 		group(cache, shnum, file, flags);
51697c478bd9Sstevel@tonic-gate 
5170981a172dSab196087 	if (flags & FLG_SHOW_SYMINFO)
5171d444b03eSAli Bahrami 		syminfo(cache, shnum, ehdr, osabi, file);
51727c478bd9Sstevel@tonic-gate 
5173981a172dSab196087 	if (flags & FLG_SHOW_RELOC)
51740e233487SRod Evans 		reloc(cache, shnum, ehdr, file);
51757c478bd9Sstevel@tonic-gate 
5176981a172dSab196087 	if (flags & FLG_SHOW_DYNAMIC)
51774f680cc6SAli Bahrami 		dynamic(cache, shnum, ehdr, osabi, file);
51787c478bd9Sstevel@tonic-gate 
5179c6c9aed4Sab196087 	if (flags & FLG_SHOW_NOTE) {
5180c6c9aed4Sab196087 		Word	note_cnt;
5181c6c9aed4Sab196087 		size_t	note_shnum;
5182c6c9aed4Sab196087 		Cache	*note_cache;
5183c6c9aed4Sab196087 
5184c6c9aed4Sab196087 		note_cnt = note(cache, shnum, ehdr, file);
5185c6c9aed4Sab196087 
5186c6c9aed4Sab196087 		/*
5187c6c9aed4Sab196087 		 * Solaris core files have section headers, but these
5188c6c9aed4Sab196087 		 * headers do not include SHT_NOTE sections that reference
5189c6c9aed4Sab196087 		 * the core note sections. This means that note() won't
5190c6c9aed4Sab196087 		 * find the core notes. Fake section headers (-P option)
5191c6c9aed4Sab196087 		 * recover these sections, but it is inconvenient to require
5192c6c9aed4Sab196087 		 * users to specify -P in this situation. If the following
5193c6c9aed4Sab196087 		 * are all true:
5194c6c9aed4Sab196087 		 *
5195c6c9aed4Sab196087 		 *	- No note sections were found
5196c6c9aed4Sab196087 		 *	- This is a core file
5197c6c9aed4Sab196087 		 *	- We are not already using fake section headers
5198c6c9aed4Sab196087 		 *
5199c6c9aed4Sab196087 		 * then we will automatically generate fake section headers
5200c6c9aed4Sab196087 		 * and then process them in a second call to note().
5201c6c9aed4Sab196087 		 */
5202c6c9aed4Sab196087 		if ((note_cnt == 0) && (ehdr->e_type == ET_CORE) &&
5203c6c9aed4Sab196087 		    !(flags & FLG_CTL_FAKESHDR) &&
5204c6c9aed4Sab196087 		    (fake_shdr_cache(file, fd, elf, ehdr,
5205c6c9aed4Sab196087 		    &note_cache, &note_shnum) != 0)) {
5206c6c9aed4Sab196087 			(void) note(note_cache, note_shnum, ehdr, file);
5207c6c9aed4Sab196087 			fake_shdr_cache_free(note_cache, note_shnum);
5208c6c9aed4Sab196087 		}
5209c6c9aed4Sab196087 	}
52107c478bd9Sstevel@tonic-gate 
52114f680cc6SAli Bahrami 	if ((flags & FLG_SHOW_MOVE) && (osabi == ELFOSABI_SOLARIS))
5212c809c407Sab196087 		move(cache, shnum, file, flags);
52137c478bd9Sstevel@tonic-gate 
5214981a172dSab196087 	if (flags & FLG_CALC_CHECKSUM)
52157c478bd9Sstevel@tonic-gate 		checksum(elf);
52167c478bd9Sstevel@tonic-gate 
52174f680cc6SAli Bahrami 	if ((flags & FLG_SHOW_CAP) && (osabi == ELFOSABI_SOLARIS))
521808278a5eSRod Evans 		cap(file, cache, shnum, phnum, ehdr, osabi, elf, flags);
52197c478bd9Sstevel@tonic-gate 
52204f680cc6SAli Bahrami 	if ((flags & FLG_SHOW_UNWIND) &&
52214f680cc6SAli Bahrami 	    ((osabi == ELFOSABI_SOLARIS) || (osabi == ELFOSABI_LINUX)))
52224f680cc6SAli Bahrami 		unwind(cache, shnum, phnum, ehdr, osabi, file, elf, flags);
52237c478bd9Sstevel@tonic-gate 
522439773e46Sab196087 
522539773e46Sab196087 	/* Release the memory used to cache section headers */
5226981a172dSab196087 done:
5227981a172dSab196087 	if (flags & FLG_CTL_FAKESHDR)
522839773e46Sab196087 		fake_shdr_cache_free(cache, shnum);
522939773e46Sab196087 	else
52307c478bd9Sstevel@tonic-gate 		free(cache);
5231981a172dSab196087 
5232981a172dSab196087 	return (ret);
52337c478bd9Sstevel@tonic-gate }
5234