xref: /titanic_44/usr/src/cmd/sgs/elfdump/common/elfdump.c (revision 37915d862fc5cc87b5b702954d7d7f16a9f98e5c)
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 /*
287c478bd9Sstevel@tonic-gate  * Dump an elf file.
297c478bd9Sstevel@tonic-gate  */
30e23c41c9SAli Bahrami #include	<stddef.h>
317c478bd9Sstevel@tonic-gate #include	<sys/elf_386.h>
327c478bd9Sstevel@tonic-gate #include	<sys/elf_amd64.h>
335aefb655Srie #include	<sys/elf_SPARC.h>
34ba2be530Sab196087 #include	<_libelf.h>
355aefb655Srie #include	<dwarf.h>
36a194faf8Srie #include	<stdio.h>
375aefb655Srie #include	<unistd.h>
385aefb655Srie #include	<errno.h>
395aefb655Srie #include	<strings.h>
407c478bd9Sstevel@tonic-gate #include	<debug.h>
417c478bd9Sstevel@tonic-gate #include	<conv.h>
427c478bd9Sstevel@tonic-gate #include	<msg.h>
435aefb655Srie #include	<_elfdump.h>
447c478bd9Sstevel@tonic-gate 
453b41b08bSab196087 
463b41b08bSab196087 /*
473b41b08bSab196087  * VERSYM_STATE is used to maintain information about the VERSYM section
483b41b08bSab196087  * in the object being analyzed. It is filled in by versions(), and used
493b41b08bSab196087  * by init_symtbl_state() when displaying symbol information.
503b41b08bSab196087  *
51090a8d9eSAli Bahrami  * There are three forms of symbol versioning known to us:
52090a8d9eSAli Bahrami  *
53090a8d9eSAli Bahrami  * 1) The original form, introduced with Solaris 2.5, in which
54090a8d9eSAli Bahrami  *	the Versym contains indexes to Verdef records, and the
55090a8d9eSAli Bahrami  *	Versym values for UNDEF symbols resolved by other objects
56090a8d9eSAli Bahrami  *	are all set to 0.
57090a8d9eSAli Bahrami  * 2) The GNU form, which is backward compatible with the original
58090a8d9eSAli Bahrami  *	Solaris form, but which adds several extensions:
59090a8d9eSAli Bahrami  *	- The Versym also contains indexes to Verneed records, recording
60090a8d9eSAli Bahrami  *		which object/version contributed the external symbol at
61090a8d9eSAli Bahrami  *		link time. These indexes start with the next value following
62090a8d9eSAli Bahrami  *		the final Verdef index. The index is written to the previously
63090a8d9eSAli Bahrami  *		reserved vna_other field of the ELF Vernaux structure.
64090a8d9eSAli Bahrami  *	- The top bit of the Versym value is no longer part of the index,
65090a8d9eSAli Bahrami  *		but is used as a "hidden bit" to prevent binding to the symbol.
66090a8d9eSAli Bahrami  *	- Multiple implementations of a given symbol, contained in varying
67090a8d9eSAli Bahrami  *		versions are allowed, using special assembler pseudo ops,
68090a8d9eSAli Bahrami  *		and encoded in the symbol name using '@' characters.
69090a8d9eSAli Bahrami  * 3) Modified Solaris form, in which we adopt the first GNU extension
70090a8d9eSAli Bahrami  *	(Versym indexes to Verneed records), but not the others.
71090a8d9eSAli Bahrami  *
72090a8d9eSAli Bahrami  * elfdump can handle any of these cases. The presence of a DT_VERSYM
73090a8d9eSAli Bahrami  * dynamic element indicates a full GNU object. An object that lacks
74090a8d9eSAli Bahrami  * a DT_VERSYM entry, but which has non-zero vna_other fields in the Vernaux
75090a8d9eSAli Bahrami  * structures is a modified Solaris object. An object that has neither of
76090a8d9eSAli Bahrami  * these uses the original form.
77090a8d9eSAli Bahrami  *
78d840867fSab196087  * max_verndx contains the largest version index that can appear
79d840867fSab196087  * in a Versym entry. This can never be less than 1: In the case where
80d840867fSab196087  * there is no verdef/verneed sections, the [0] index is reserved
81090a8d9eSAli Bahrami  * for local symbols, and the [1] index for globals. If the original
82090a8d9eSAli Bahrami  * Solaris versioning rules are in effect and there is a verdef section,
83090a8d9eSAli Bahrami  * then max_verndex is the number of defined versions. If one of the
84090a8d9eSAli Bahrami  * other versioning forms is in effect, then:
85090a8d9eSAli Bahrami  *	1) If there is no verneed section, it is the same as for
86090a8d9eSAli Bahrami  *		original Solaris versioning.
87090a8d9eSAli Bahrami  *	2) If there is a verneed section, the vna_other field of the
88d840867fSab196087  *		Vernaux structs contain versions, and max_verndx is the
89d840867fSab196087  *		largest such index.
90d840867fSab196087  *
91090a8d9eSAli Bahrami  * If gnu_full is True, the object uses the full GNU form of versioning.
92090a8d9eSAli Bahrami  * The value of the gnu_full field is based on the presence of
93d840867fSab196087  * a DT_VERSYM entry in the dynamic section: GNU ld produces these, and
94d840867fSab196087  * Solaris ld does not.
95090a8d9eSAli Bahrami  *
96090a8d9eSAli Bahrami  * The gnu_needed field is True if the Versym contains indexes to
97090a8d9eSAli Bahrami  * Verneed records, as indicated by non-zero vna_other fields in the Verneed
98090a8d9eSAli Bahrami  * section. If gnu_full is True, then gnu_needed will always be true.
99090a8d9eSAli Bahrami  * However, gnu_needed can be true without gnu_full. This is the modified
100090a8d9eSAli Bahrami  * Solaris form.
1013b41b08bSab196087  */
1023b41b08bSab196087 typedef struct {
1033b41b08bSab196087 	Cache	*cache;		/* Pointer to cache entry for VERSYM */
1043b41b08bSab196087 	Versym	*data;		/* Pointer to versym array */
105090a8d9eSAli Bahrami 	int	gnu_full;	/* True if object uses GNU versioning rules */
106090a8d9eSAli Bahrami 	int	gnu_needed;	/* True if object uses VERSYM indexes for */
107090a8d9eSAli Bahrami 				/*	VERNEED (subset of gnu_full) */
108d840867fSab196087 	int	max_verndx;	/* largest versym index value */
1093b41b08bSab196087 } VERSYM_STATE;
1103b41b08bSab196087 
1113b41b08bSab196087 /*
1123b41b08bSab196087  * SYMTBL_STATE is used to maintain information about a single symbol
1133b41b08bSab196087  * table section, for use by the routines that display symbol information.
1143b41b08bSab196087  */
1153b41b08bSab196087 typedef struct {
1163b41b08bSab196087 	const char	*file;		/* Name of file */
1173b41b08bSab196087 	Ehdr		*ehdr;		/* ELF header for file */
1183b41b08bSab196087 	Cache		*cache;		/* Cache of all section headers */
1194f680cc6SAli Bahrami 	uchar_t		osabi;		/* OSABI to use */
1203b41b08bSab196087 	Word		shnum;		/* # of sections in cache */
1213b41b08bSab196087 	Cache		*seccache;	/* Cache of symbol table section hdr */
1223b41b08bSab196087 	Word		secndx;		/* Index of symbol table section hdr */
1233b41b08bSab196087 	const char	*secname;	/* Name of section */
1243b41b08bSab196087 	uint_t		flags;		/* Command line option flags */
1253b41b08bSab196087 	struct {			/* Extended section index data */
1263b41b08bSab196087 		int	checked;	/* TRUE if already checked for shxndx */
1273b41b08bSab196087 		Word	*data;		/* NULL, or extended section index */
1283b41b08bSab196087 					/*	used for symbol table entries */
1293b41b08bSab196087 		uint_t	n;		/* # items in shxndx.data */
1303b41b08bSab196087 	} shxndx;
1313b41b08bSab196087 	VERSYM_STATE	*versym;	/* NULL, or associated VERSYM section */
1323b41b08bSab196087 	Sym 		*sym;		/* Array of symbols */
1333b41b08bSab196087 	Word		symn;		/* # of symbols */
1343b41b08bSab196087 } SYMTBL_STATE;
1353b41b08bSab196087 
136e23c41c9SAli Bahrami /*
137e23c41c9SAli Bahrami  * A variable of this type is used to track information related to
138e23c41c9SAli Bahrami  * .eh_frame and .eh_frame_hdr sections across calls to unwind_eh_frame().
139e23c41c9SAli Bahrami  */
140e23c41c9SAli Bahrami typedef struct {
141e23c41c9SAli Bahrami 	Word		frame_cnt;	/* # .eh_frame sections seen */
142e23c41c9SAli Bahrami 	Word		frame_ndx;	/* Section index of 1st .eh_frame */
143e23c41c9SAli Bahrami 	Word		hdr_cnt;	/* # .eh_frame_hdr sections seen */
144e23c41c9SAli Bahrami 	Word		hdr_ndx;	/* Section index of 1st .eh_frame_hdr */
145e23c41c9SAli Bahrami 	uint64_t	frame_ptr;	/* Value of FramePtr field from first */
146e23c41c9SAli Bahrami 					/*	.eh_frame_hdr section */
147e23c41c9SAli Bahrami 	uint64_t	frame_base;	/* Data addr of 1st .eh_frame  */
148e23c41c9SAli Bahrami } gnu_eh_state_t;
1493b41b08bSab196087 
150e23c41c9SAli Bahrami /*
151e23c41c9SAli Bahrami  * C++ .exception_ranges entries make use of the signed ptrdiff_t
152e23c41c9SAli Bahrami  * type to record self-relative pointer values. We need a type
153e23c41c9SAli Bahrami  * for this that is matched to the ELFCLASS being processed.
154e23c41c9SAli Bahrami  */
155e23c41c9SAli Bahrami #if	defined(_ELF64)
156e23c41c9SAli Bahrami 	typedef int64_t PTRDIFF_T;
157e23c41c9SAli Bahrami #else
158e23c41c9SAli Bahrami 	typedef int32_t PTRDIFF_T;
159e23c41c9SAli Bahrami #endif
160e23c41c9SAli Bahrami 
161e23c41c9SAli Bahrami /*
162e23c41c9SAli Bahrami  * The Sun C++ ABI uses this struct to define each .exception_ranges
163e23c41c9SAli Bahrami  * entry. From the ABI:
164e23c41c9SAli Bahrami  *
165e23c41c9SAli Bahrami  * The field ret_addr is a self relative pointer to the start of the address
166e23c41c9SAli Bahrami  * range. The name was chosen because in the current implementation the range
167e23c41c9SAli Bahrami  * typically starts at the return address for a call site.
168e23c41c9SAli Bahrami  *
169e23c41c9SAli Bahrami  * The field length is the difference, in bytes, between the pc of the last
170e23c41c9SAli Bahrami  * instruction covered by the exception range and the first. When only a
171e23c41c9SAli Bahrami  * single call site is represented without optimization, this will equal zero.
172e23c41c9SAli Bahrami  *
173e23c41c9SAli Bahrami  * The field handler_addr is a relative pointer which stores the difference
174e23c41c9SAli Bahrami  * between the start of the exception range and the address of all code to
175e23c41c9SAli Bahrami  * catch exceptions and perform the cleanup for stack unwinding.
176e23c41c9SAli Bahrami  *
177e23c41c9SAli Bahrami  * The field type_block is a relative pointer which stores the difference
178e23c41c9SAli Bahrami  * between the start of the exception range and the address of an array used
179e23c41c9SAli Bahrami  * for storing a list of the types of exceptions which can be caught within
180e23c41c9SAli Bahrami  * the exception range.
181e23c41c9SAli Bahrami  */
182e23c41c9SAli Bahrami typedef struct {
183e23c41c9SAli Bahrami 	PTRDIFF_T	ret_addr;
184e23c41c9SAli Bahrami 	Xword		length;
185e23c41c9SAli Bahrami 	PTRDIFF_T	handler_addr;
186e23c41c9SAli Bahrami 	PTRDIFF_T	type_block;
187e23c41c9SAli Bahrami 	Xword		reserved;
188e23c41c9SAli Bahrami } exception_range_entry;
1893b41b08bSab196087 
1907c478bd9Sstevel@tonic-gate /*
1917c478bd9Sstevel@tonic-gate  * Focal point for verifying symbol names.
1927c478bd9Sstevel@tonic-gate  */
1937c478bd9Sstevel@tonic-gate static const char *
string(Cache * refsec,Word ndx,Cache * strsec,const char * file,Word name)1945aefb655Srie string(Cache *refsec, Word ndx, Cache *strsec, const char *file, Word name)
1957c478bd9Sstevel@tonic-gate {
19631fdd7caSab196087 	/*
19731fdd7caSab196087 	 * If an error in this routine is due to a property of the string
19831fdd7caSab196087 	 * section, as opposed to a bad offset into the section (a property of
19931fdd7caSab196087 	 * the referencing section), then we will detect the same error on
20031fdd7caSab196087 	 * every call involving those sections. We use these static variables
20131fdd7caSab196087 	 * to retain the information needed to only issue each such error once.
20231fdd7caSab196087 	 */
20331fdd7caSab196087 	static Cache	*last_refsec;	/* Last referencing section seen */
20431fdd7caSab196087 	static int	strsec_err;	/* True if error issued */
20531fdd7caSab196087 
206d579eb63Sab196087 	const char	*strs;
207d579eb63Sab196087 	Word		strn;
2087c478bd9Sstevel@tonic-gate 
2099a411307Srie 	if (strsec->c_data == NULL)
2109a411307Srie 		return (NULL);
2119a411307Srie 
212d579eb63Sab196087 	strs = (char *)strsec->c_data->d_buf;
213d579eb63Sab196087 	strn = strsec->c_data->d_size;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	/*
21631fdd7caSab196087 	 * We only print a diagnostic regarding a bad string table once per
21731fdd7caSab196087 	 * input section being processed. If the refsec has changed, reset
21831fdd7caSab196087 	 * our retained error state.
2197c478bd9Sstevel@tonic-gate 	 */
22031fdd7caSab196087 	if (last_refsec != refsec) {
22131fdd7caSab196087 		last_refsec = refsec;
22231fdd7caSab196087 		strsec_err = 0;
22331fdd7caSab196087 	}
22431fdd7caSab196087 
22531fdd7caSab196087 	/* Verify that strsec really is a string table */
22631fdd7caSab196087 	if (strsec->c_shdr->sh_type != SHT_STRTAB) {
22731fdd7caSab196087 		if (!strsec_err) {
22831fdd7caSab196087 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_NOTSTRTAB),
22931fdd7caSab196087 			    file, strsec->c_ndx, refsec->c_ndx);
23031fdd7caSab196087 			strsec_err = 1;
23131fdd7caSab196087 		}
23231fdd7caSab196087 		return (MSG_INTL(MSG_STR_UNKNOWN));
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	/*
2367c478bd9Sstevel@tonic-gate 	 * Is the string table offset within range of the available strings?
2377c478bd9Sstevel@tonic-gate 	 */
2387c478bd9Sstevel@tonic-gate 	if (name >= strn) {
2397c478bd9Sstevel@tonic-gate 		/*
2407c478bd9Sstevel@tonic-gate 		 * Do we have a empty string table?
2417c478bd9Sstevel@tonic-gate 		 */
2427e16fca0SAli Bahrami 		if (strs == NULL) {
24331fdd7caSab196087 			if (!strsec_err) {
2447c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
2457c478bd9Sstevel@tonic-gate 				    file, strsec->c_name);
24631fdd7caSab196087 				strsec_err = 1;
2477c478bd9Sstevel@tonic-gate 			}
2487c478bd9Sstevel@tonic-gate 		} else {
2497c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSTOFF),
2505aefb655Srie 			    file, refsec->c_name, EC_WORD(ndx), strsec->c_name,
2515aefb655Srie 			    EC_WORD(name), EC_WORD(strn - 1));
2527c478bd9Sstevel@tonic-gate 		}
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 		/*
2557c478bd9Sstevel@tonic-gate 		 * Return the empty string so that the calling function can
2567c478bd9Sstevel@tonic-gate 		 * continue it's output diagnostics.
2577c478bd9Sstevel@tonic-gate 		 */
2587c478bd9Sstevel@tonic-gate 		return (MSG_INTL(MSG_STR_UNKNOWN));
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 	return (strs + name);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate /*
2645aefb655Srie  * Relocations can reference section symbols and standard symbols.  If the
2655aefb655Srie  * former, establish the section name.
2665aefb655Srie  */
2675aefb655Srie static const char *
relsymname(Cache * cache,Cache * csec,Cache * strsec,Word symndx,Word symnum,Word relndx,Sym * syms,char * secstr,size_t secsz,const char * file)2685aefb655Srie relsymname(Cache *cache, Cache *csec, Cache *strsec, Word symndx, Word symnum,
2690e233487SRod Evans     Word relndx, Sym *syms, char *secstr, size_t secsz, const char *file)
2705aefb655Srie {
2715aefb655Srie 	Sym		*sym;
2720e233487SRod Evans 	const char	*name;
2735aefb655Srie 
2745aefb655Srie 	if (symndx >= symnum) {
2755aefb655Srie 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_RELBADSYMNDX),
2765aefb655Srie 		    file, EC_WORD(symndx), EC_WORD(relndx));
2775aefb655Srie 		return (MSG_INTL(MSG_STR_UNKNOWN));
2785aefb655Srie 	}
2795aefb655Srie 
2805aefb655Srie 	sym = (Sym *)(syms + symndx);
2810e233487SRod Evans 	name = string(csec, symndx, strsec, file, sym->st_name);
2825aefb655Srie 
2835aefb655Srie 	/*
2845aefb655Srie 	 * If the symbol represents a section offset construct an appropriate
2850e233487SRod Evans 	 * string.  Note, although section symbol table entries typically have
2860e233487SRod Evans 	 * a NULL name pointer, entries do exist that point into the string
2870e233487SRod Evans 	 * table to their own NULL strings.
2885aefb655Srie 	 */
2890e233487SRod Evans 	if ((ELF_ST_TYPE(sym->st_info) == STT_SECTION) &&
2900e233487SRod Evans 	    ((sym->st_name == 0) || (*name == '\0'))) {
2910e233487SRod Evans 		(void) snprintf(secstr, secsz, MSG_INTL(MSG_STR_SECTION),
2925aefb655Srie 		    cache[sym->st_shndx].c_name);
2935aefb655Srie 		return ((const char *)secstr);
2945aefb655Srie 	}
2955aefb655Srie 
2960e233487SRod Evans 	return (name);
2975aefb655Srie }
2985aefb655Srie 
2995aefb655Srie /*
3005aefb655Srie  * Focal point for establishing a string table section.  Data such as the
3015aefb655Srie  * dynamic information simply points to a string table.  Data such as
3025aefb655Srie  * relocations, reference a symbol table, which in turn is associated with a
3035aefb655Srie  * string table.
3047c478bd9Sstevel@tonic-gate  */
3057c478bd9Sstevel@tonic-gate static int
stringtbl(Cache * cache,int symtab,Word ndx,Word shnum,const char * file,Word * symnum,Cache ** symsec,Cache ** strsec)3065aefb655Srie stringtbl(Cache *cache, int symtab, Word ndx, Word shnum, const char *file,
3075aefb655Srie     Word *symnum, Cache **symsec, Cache **strsec)
3085aefb655Srie {
3095aefb655Srie 	Shdr	*shdr = cache[ndx].c_shdr;
3105aefb655Srie 
3115aefb655Srie 	if (symtab) {
3125aefb655Srie 		/*
3135aefb655Srie 		 * Validate the symbol table section.
3145aefb655Srie 		 */
3155aefb655Srie 		if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
3165aefb655Srie 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
3175aefb655Srie 			    file, cache[ndx].c_name, EC_WORD(shdr->sh_link));
3185aefb655Srie 			return (0);
3195aefb655Srie 		}
3209a411307Srie 		if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
3219a411307Srie 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
3229a411307Srie 			    file, cache[ndx].c_name);
3239a411307Srie 			return (0);
3249a411307Srie 		}
3255aefb655Srie 
3265aefb655Srie 		/*
3275aefb655Srie 		 * Obtain, and verify the symbol table data.
3285aefb655Srie 		 */
3299a411307Srie 		if ((cache[ndx].c_data == NULL) ||
3309a411307Srie 		    (cache[ndx].c_data->d_buf == NULL)) {
3315aefb655Srie 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
3325aefb655Srie 			    file, cache[ndx].c_name);
3335aefb655Srie 			return (0);
3345aefb655Srie 		}
3355aefb655Srie 
3365aefb655Srie 		/*
3375aefb655Srie 		 * Establish the string table index.
3385aefb655Srie 		 */
3395aefb655Srie 		ndx = shdr->sh_link;
3405aefb655Srie 		shdr = cache[ndx].c_shdr;
3415aefb655Srie 
3425aefb655Srie 		/*
3435aefb655Srie 		 * Return symbol table information.
3445aefb655Srie 		 */
3455aefb655Srie 		if (symnum)
3465aefb655Srie 			*symnum = (shdr->sh_size / shdr->sh_entsize);
3475aefb655Srie 		if (symsec)
3485aefb655Srie 			*symsec = &cache[ndx];
3495aefb655Srie 	}
3505aefb655Srie 
3515aefb655Srie 	/*
3525aefb655Srie 	 * Validate the associated string table section.
3535aefb655Srie 	 */
3545aefb655Srie 	if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
3555aefb655Srie 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
3565aefb655Srie 		    file, cache[ndx].c_name, EC_WORD(shdr->sh_link));
3575aefb655Srie 		return (0);
3585aefb655Srie 	}
3595aefb655Srie 
3605aefb655Srie 	if (strsec)
3615aefb655Srie 		*strsec = &cache[shdr->sh_link];
3625aefb655Srie 
3635aefb655Srie 	return (1);
3645aefb655Srie }
3655aefb655Srie 
3665aefb655Srie /*
3675aefb655Srie  * Lookup a symbol and set Sym accordingly.
36831dd2c84SAli Bahrami  *
36931dd2c84SAli Bahrami  * entry:
37031dd2c84SAli Bahrami  *	name - Name of symbol to lookup
37131dd2c84SAli Bahrami  *	cache - Cache of all section headers
37231dd2c84SAli Bahrami  *	shnum - # of sections in cache
37331dd2c84SAli Bahrami  *	sym - Address of pointer to receive symbol
37431dd2c84SAli Bahrami  *	target - NULL, or section to which the symbol must be associated.
37531dd2c84SAli Bahrami  *	symtab - Symbol table to search for symbol
37631dd2c84SAli Bahrami  *	file - Name of file
37731dd2c84SAli Bahrami  *
37831dd2c84SAli Bahrami  * exit:
37931dd2c84SAli Bahrami  *	If the symbol is found, *sym is set to reference it, and True is
38031dd2c84SAli Bahrami  *	returned. If target is non-NULL, the symbol must reference the given
38131dd2c84SAli Bahrami  *	section --- otherwise the section is not checked.
38231dd2c84SAli Bahrami  *
38331dd2c84SAli Bahrami  *	If no symbol is found, False is returned.
3845aefb655Srie  */
3855aefb655Srie static int
symlookup(const char * name,Cache * cache,Word shnum,Sym ** sym,Cache * target,Cache * symtab,const char * file)3865aefb655Srie symlookup(const char *name, Cache *cache, Word shnum, Sym **sym,
38731dd2c84SAli Bahrami     Cache *target, Cache *symtab, const char *file)
3887c478bd9Sstevel@tonic-gate {
3895aefb655Srie 	Shdr	*shdr;
3905aefb655Srie 	Word	symn, cnt;
3915aefb655Srie 	Sym	*syms;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	if (symtab == 0)
3947c478bd9Sstevel@tonic-gate 		return (0);
3957c478bd9Sstevel@tonic-gate 
3965aefb655Srie 	shdr = symtab->c_shdr;
3975aefb655Srie 
3987c478bd9Sstevel@tonic-gate 	/*
3997c478bd9Sstevel@tonic-gate 	 * Determine the symbol data and number.
4007c478bd9Sstevel@tonic-gate 	 */
4017c478bd9Sstevel@tonic-gate 	if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
4027c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
4037c478bd9Sstevel@tonic-gate 		    file, symtab->c_name);
4047c478bd9Sstevel@tonic-gate 		return (0);
4057c478bd9Sstevel@tonic-gate 	}
4069a411307Srie 	if (symtab->c_data == NULL)
4079a411307Srie 		return (0);
4089a411307Srie 
4097c478bd9Sstevel@tonic-gate 	/* LINTED */
4105aefb655Srie 	symn = (Word)(shdr->sh_size / shdr->sh_entsize);
4115aefb655Srie 	syms = (Sym *)symtab->c_data->d_buf;
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	/*
4147c478bd9Sstevel@tonic-gate 	 * Get the associated string table section.
4157c478bd9Sstevel@tonic-gate 	 */
4167c478bd9Sstevel@tonic-gate 	if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
4177c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
4185aefb655Srie 		    file, symtab->c_name, EC_WORD(shdr->sh_link));
4197c478bd9Sstevel@tonic-gate 		return (0);
4207c478bd9Sstevel@tonic-gate 	}
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	/*
4237c478bd9Sstevel@tonic-gate 	 * Loop through the symbol table to find a match.
4247c478bd9Sstevel@tonic-gate 	 */
42531dd2c84SAli Bahrami 	*sym = NULL;
4265aefb655Srie 	for (cnt = 0; cnt < symn; syms++, cnt++) {
4275aefb655Srie 		const char	*symname;
4287c478bd9Sstevel@tonic-gate 
4295aefb655Srie 		symname = string(symtab, cnt, &cache[shdr->sh_link], file,
4305aefb655Srie 		    syms->st_name);
4317c478bd9Sstevel@tonic-gate 
43231dd2c84SAli Bahrami 		if (symname && (strcmp(name, symname) == 0) &&
43331dd2c84SAli Bahrami 		    ((target == NULL) || (target->c_ndx == syms->st_shndx))) {
43431dd2c84SAli Bahrami 			/*
43531dd2c84SAli Bahrami 			 * It is possible, though rare, for a local and
43631dd2c84SAli Bahrami 			 * global symbol of the same name to exist, each
43731dd2c84SAli Bahrami 			 * contributed by a different input object. If the
43831dd2c84SAli Bahrami 			 * symbol just found is local, remember it, but
43931dd2c84SAli Bahrami 			 * continue looking.
44031dd2c84SAli Bahrami 			 */
4415aefb655Srie 			*sym = syms;
44231dd2c84SAli Bahrami 			if (ELF_ST_BIND(syms->st_info) != STB_LOCAL)
44331dd2c84SAli Bahrami 				break;
4447c478bd9Sstevel@tonic-gate 		}
4457c478bd9Sstevel@tonic-gate 	}
44631dd2c84SAli Bahrami 
44731dd2c84SAli Bahrami 	return (*sym != NULL);
4487c478bd9Sstevel@tonic-gate }
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate /*
4517c478bd9Sstevel@tonic-gate  * Print section headers.
4527c478bd9Sstevel@tonic-gate  */
4537c478bd9Sstevel@tonic-gate static void
sections(const char * file,Cache * cache,Word shnum,Ehdr * ehdr,uchar_t osabi)4544f680cc6SAli Bahrami sections(const char *file, Cache *cache, Word shnum, Ehdr *ehdr, uchar_t osabi)
4557c478bd9Sstevel@tonic-gate {
4565aefb655Srie 	size_t	seccnt;
4577c478bd9Sstevel@tonic-gate 
4585aefb655Srie 	for (seccnt = 1; seccnt < shnum; seccnt++) {
4595aefb655Srie 		Cache		*_cache = &cache[seccnt];
4605aefb655Srie 		Shdr		*shdr = _cache->c_shdr;
4615aefb655Srie 		const char	*secname = _cache->c_name;
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 		/*
4647c478bd9Sstevel@tonic-gate 		 * Although numerous section header entries can be zero, it's
465f5a18a30Srie 		 * usually a sign of trouble if the type is zero.
4667c478bd9Sstevel@tonic-gate 		 */
4677c478bd9Sstevel@tonic-gate 		if (shdr->sh_type == 0) {
4687c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHTYPE),
4695aefb655Srie 			    file, secname, EC_WORD(shdr->sh_type));
4707c478bd9Sstevel@tonic-gate 		}
4717c478bd9Sstevel@tonic-gate 
472981a172dSab196087 		if (!match(MATCH_F_ALL, secname, seccnt, shdr->sh_type))
473f5a18a30Srie 			continue;
4747c478bd9Sstevel@tonic-gate 
475141040e8Srie 		/*
476141040e8Srie 		 * Identify any sections that are suspicious.  A .got section
477141040e8Srie 		 * shouldn't exist in a relocatable object.
478141040e8Srie 		 */
479141040e8Srie 		if (ehdr->e_type == ET_REL) {
4805aefb655Srie 			if (strncmp(secname, MSG_ORIG(MSG_ELF_GOT),
481141040e8Srie 			    MSG_ELF_GOT_SIZE) == 0) {
482141040e8Srie 				(void) fprintf(stderr,
4835aefb655Srie 				    MSG_INTL(MSG_GOT_UNEXPECTED), file,
4845aefb655Srie 				    secname);
485141040e8Srie 			}
486141040e8Srie 		}
487141040e8Srie 
4885aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
4895aefb655Srie 		dbg_print(0, MSG_INTL(MSG_ELF_SHDR), EC_WORD(seccnt), secname);
4904f680cc6SAli Bahrami 		Elf_shdr(0, osabi, ehdr->e_machine, shdr);
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate }
4937c478bd9Sstevel@tonic-gate 
4945aefb655Srie /*
4955aefb655Srie  * Obtain a specified Phdr entry.
4965aefb655Srie  */
4975aefb655Srie static Phdr *
getphdr(Word phnum,Word * type_arr,Word type_cnt,const char * file,Elf * elf)4987e16fca0SAli Bahrami getphdr(Word phnum, Word *type_arr, Word type_cnt, const char *file, Elf *elf)
4995aefb655Srie {
5007e16fca0SAli Bahrami 	Word	cnt, tcnt;
5015aefb655Srie 	Phdr	*phdr;
5025aefb655Srie 
5035aefb655Srie 	if ((phdr = elf_getphdr(elf)) == NULL) {
5045aefb655Srie 		failure(file, MSG_ORIG(MSG_ELF_GETPHDR));
5057e16fca0SAli Bahrami 		return (NULL);
5065aefb655Srie 	}
5075aefb655Srie 
5085aefb655Srie 	for (cnt = 0; cnt < phnum; phdr++, cnt++) {
5097e16fca0SAli Bahrami 		for (tcnt = 0; tcnt < type_cnt; tcnt++) {
5107e16fca0SAli Bahrami 			if (phdr->p_type == type_arr[tcnt])
5115aefb655Srie 				return (phdr);
5125aefb655Srie 		}
5137e16fca0SAli Bahrami 	}
5147e16fca0SAli Bahrami 	return (NULL);
5155aefb655Srie }
5165aefb655Srie 
517e23c41c9SAli Bahrami /*
518e23c41c9SAli Bahrami  * Display the contents of GNU/amd64 .eh_frame and .eh_frame_hdr
519e23c41c9SAli Bahrami  * sections.
520e23c41c9SAli Bahrami  *
521e23c41c9SAli Bahrami  * entry:
522e23c41c9SAli Bahrami  *	cache - Cache of all section headers
523e23c41c9SAli Bahrami  *	shndx - Index of .eh_frame or .eh_frame_hdr section to be displayed
524965630c1SRichard Lowe  *	shnum - Total number of sections which exist
525e23c41c9SAli Bahrami  *	uphdr - NULL, or unwind program header associated with
526e23c41c9SAli Bahrami  *		the .eh_frame_hdr section.
527e23c41c9SAli Bahrami  *	ehdr - ELF header for file
528e23c41c9SAli Bahrami  *	eh_state - Data used across calls to this routine. The
529e23c41c9SAli Bahrami  *		caller should zero it before the first call, and
530e23c41c9SAli Bahrami  *		pass it on every call.
531e23c41c9SAli Bahrami  *	osabi - OSABI to use in displaying information
532e23c41c9SAli Bahrami  *	file - Name of file
533e23c41c9SAli Bahrami  *	flags - Command line option flags
534e23c41c9SAli Bahrami  */
5357c478bd9Sstevel@tonic-gate static void
unwind_eh_frame(Cache * cache,Word shndx,Word shnum,Phdr * uphdr,Ehdr * ehdr,gnu_eh_state_t * eh_state,uchar_t osabi,const char * file,uint_t flags)536965630c1SRichard Lowe unwind_eh_frame(Cache *cache, Word shndx, Word shnum, Phdr *uphdr, Ehdr *ehdr,
537e23c41c9SAli Bahrami     gnu_eh_state_t *eh_state, uchar_t osabi, const char *file, uint_t flags)
5387c478bd9Sstevel@tonic-gate {
5397e16fca0SAli Bahrami #if	defined(_ELF64)
5407e16fca0SAli Bahrami #define	MSG_UNW_BINSRTAB2	MSG_UNW_BINSRTAB2_64
5417e16fca0SAli Bahrami #define	MSG_UNW_BINSRTABENT	MSG_UNW_BINSRTABENT_64
5427e16fca0SAli Bahrami #else
5437e16fca0SAli Bahrami #define	MSG_UNW_BINSRTAB2	MSG_UNW_BINSRTAB2_32
5447e16fca0SAli Bahrami #define	MSG_UNW_BINSRTABENT	MSG_UNW_BINSRTABENT_32
5457e16fca0SAli Bahrami #endif
5467e16fca0SAli Bahrami 
547e23c41c9SAli Bahrami 	Cache			*_cache = &cache[shndx];
5485aefb655Srie 	Shdr			*shdr = _cache->c_shdr;
549e23c41c9SAli Bahrami 	uchar_t			*data = (uchar_t *)(_cache->c_data->d_buf);
550e23c41c9SAli Bahrami 	size_t			datasize = _cache->c_data->d_size;
551e23c41c9SAli Bahrami 	Conv_dwarf_ehe_buf_t	dwarf_ehe_buf;
5527e16fca0SAli Bahrami 	uint64_t		ndx, frame_ptr, fde_cnt, tabndx;
5535aefb655Srie 	uint_t			vers, frame_ptr_enc, fde_cnt_enc, table_enc;
554*37915d86SRichard Lowe 	uint64_t		initloc, initloc0 = 0;
555965630c1SRichard Lowe 	uint64_t		gotaddr = 0;
556965630c1SRichard Lowe 	int			cnt;
557e23c41c9SAli Bahrami 
558965630c1SRichard Lowe 	for (cnt = 1; cnt < shnum; cnt++) {
559965630c1SRichard Lowe 		if (strncmp(cache[cnt].c_name, MSG_ORIG(MSG_ELF_GOT),
560965630c1SRichard Lowe 		    MSG_ELF_GOT_SIZE) == 0) {
561965630c1SRichard Lowe 			gotaddr = cache[cnt].c_shdr->sh_addr;
562965630c1SRichard Lowe 			break;
563965630c1SRichard Lowe 		}
564965630c1SRichard Lowe 	}
5657c478bd9Sstevel@tonic-gate 
566*37915d86SRichard Lowe 	if ((data == NULL) || (datasize == 0)) {
567*37915d86SRichard Lowe 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
568*37915d86SRichard Lowe 		    file, _cache ->c_name);
569*37915d86SRichard Lowe 		return;
570*37915d86SRichard Lowe 	}
571*37915d86SRichard Lowe 
5727c478bd9Sstevel@tonic-gate 	/*
573e23c41c9SAli Bahrami 	 * Is this a .eh_frame_hdr?
5747c478bd9Sstevel@tonic-gate 	 */
5755aefb655Srie 	if ((uphdr && (shdr->sh_addr == uphdr->p_vaddr)) ||
5767c478bd9Sstevel@tonic-gate 	    (strncmp(_cache->c_name, MSG_ORIG(MSG_SCN_FRMHDR),
5777c478bd9Sstevel@tonic-gate 	    MSG_SCN_FRMHDR_SIZE) == 0)) {
5787e16fca0SAli Bahrami 		/*
5797e16fca0SAli Bahrami 		 * There can only be a single .eh_frame_hdr.
5807e16fca0SAli Bahrami 		 * Flag duplicates.
5817e16fca0SAli Bahrami 		 */
582e23c41c9SAli Bahrami 		if (++eh_state->hdr_cnt > 1)
583e23c41c9SAli Bahrami 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_MULTEHFRMHDR),
584e23c41c9SAli Bahrami 			    file, EC_WORD(shndx), _cache->c_name);
5857e16fca0SAli Bahrami 
5865aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_UNW_FRMHDR));
5877c478bd9Sstevel@tonic-gate 		ndx = 0;
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 		vers = data[ndx++];
5907c478bd9Sstevel@tonic-gate 		frame_ptr_enc = data[ndx++];
5917c478bd9Sstevel@tonic-gate 		fde_cnt_enc = data[ndx++];
5927c478bd9Sstevel@tonic-gate 		table_enc = data[ndx++];
5937c478bd9Sstevel@tonic-gate 
5945aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_UNW_FRMVERS), vers);
5957c478bd9Sstevel@tonic-gate 
596*37915d86SRichard Lowe 		switch (dwarf_ehe_extract(data, datasize, &ndx,
597*37915d86SRichard Lowe 		    &frame_ptr, frame_ptr_enc, ehdr->e_ident, B_TRUE,
598*37915d86SRichard Lowe 		    shdr->sh_addr, ndx, gotaddr)) {
599*37915d86SRichard Lowe 		case DW_OVERFLOW:
600*37915d86SRichard Lowe 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_DWOVRFLW),
601*37915d86SRichard Lowe 			    file, _cache->c_name);
602*37915d86SRichard Lowe 			return;
603*37915d86SRichard Lowe 		case DW_BAD_ENCODING:
604*37915d86SRichard Lowe 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_DWBADENC),
605*37915d86SRichard Lowe 			    file, _cache->c_name, frame_ptr_enc);
606*37915d86SRichard Lowe 			return;
607*37915d86SRichard Lowe 		case DW_SUCCESS:
608*37915d86SRichard Lowe 			break;
609*37915d86SRichard Lowe 		}
610e23c41c9SAli Bahrami 		if (eh_state->hdr_cnt == 1) {
611e23c41c9SAli Bahrami 			eh_state->hdr_ndx = shndx;
612e23c41c9SAli Bahrami 			eh_state->frame_ptr = frame_ptr;
6137e16fca0SAli Bahrami 		}
6147c478bd9Sstevel@tonic-gate 
6155aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_UNW_FRPTRENC),
616de777a60Sab196087 		    conv_dwarf_ehe(frame_ptr_enc, &dwarf_ehe_buf),
617de777a60Sab196087 		    EC_XWORD(frame_ptr));
6187c478bd9Sstevel@tonic-gate 
619*37915d86SRichard Lowe 		switch (dwarf_ehe_extract(data, datasize, &ndx, &fde_cnt,
620*37915d86SRichard Lowe 		    fde_cnt_enc, ehdr->e_ident, B_TRUE, shdr->sh_addr, ndx,
621*37915d86SRichard Lowe 		    gotaddr)) {
622*37915d86SRichard Lowe 		case DW_OVERFLOW:
623*37915d86SRichard Lowe 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_DWOVRFLW),
624*37915d86SRichard Lowe 			    file, _cache->c_name);
625*37915d86SRichard Lowe 			return;
626*37915d86SRichard Lowe 		case DW_BAD_ENCODING:
627*37915d86SRichard Lowe 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_DWBADENC),
628*37915d86SRichard Lowe 			    file, _cache->c_name, fde_cnt_enc);
629*37915d86SRichard Lowe 			return;
630*37915d86SRichard Lowe 		case DW_SUCCESS:
631*37915d86SRichard Lowe 			break;
632*37915d86SRichard Lowe 		}
6335aefb655Srie 
6345aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_UNW_FDCNENC),
635de777a60Sab196087 		    conv_dwarf_ehe(fde_cnt_enc, &dwarf_ehe_buf),
636de777a60Sab196087 		    EC_XWORD(fde_cnt));
6375aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_UNW_TABENC),
638de777a60Sab196087 		    conv_dwarf_ehe(table_enc, &dwarf_ehe_buf));
6395aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_UNW_BINSRTAB1));
6405aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_UNW_BINSRTAB2));
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 		for (tabndx = 0; tabndx < fde_cnt; tabndx++) {
643*37915d86SRichard Lowe 			uint64_t table;
644*37915d86SRichard Lowe 
645*37915d86SRichard Lowe 			switch (dwarf_ehe_extract(data, datasize, &ndx,
646*37915d86SRichard Lowe 			    &initloc, table_enc, ehdr->e_ident, B_TRUE,
647*37915d86SRichard Lowe 			    shdr->sh_addr, ndx, gotaddr)) {
648*37915d86SRichard Lowe 			case DW_OVERFLOW:
649*37915d86SRichard Lowe 				(void) fprintf(stderr,
650*37915d86SRichard Lowe 				    MSG_INTL(MSG_ERR_DWOVRFLW), file,
651*37915d86SRichard Lowe 				    _cache->c_name);
652*37915d86SRichard Lowe 				return;
653*37915d86SRichard Lowe 			case DW_BAD_ENCODING:
654*37915d86SRichard Lowe 				(void) fprintf(stderr,
655*37915d86SRichard Lowe 				    MSG_INTL(MSG_ERR_DWBADENC), file,
656*37915d86SRichard Lowe 				    _cache->c_name, table_enc);
657*37915d86SRichard Lowe 				return;
658*37915d86SRichard Lowe 			case DW_SUCCESS:
659*37915d86SRichard Lowe 				break;
660*37915d86SRichard Lowe 			}
661e23c41c9SAli Bahrami 			if ((tabndx != 0) && (initloc0 > initloc))
6621dd9d86fSAli Bahrami 				(void) fprintf(stderr,
6631dd9d86fSAli Bahrami 				    MSG_INTL(MSG_ERR_BADSORT), file,
6641dd9d86fSAli Bahrami 				    _cache->c_name, EC_WORD(tabndx));
665*37915d86SRichard Lowe 			switch (dwarf_ehe_extract(data, datasize, &ndx, &table,
666*37915d86SRichard Lowe 			    table_enc, ehdr->e_ident, B_TRUE, shdr->sh_addr,
667*37915d86SRichard Lowe 			    ndx, gotaddr)) {
668*37915d86SRichard Lowe 			case DW_OVERFLOW:
669*37915d86SRichard Lowe 				(void) fprintf(stderr,
670*37915d86SRichard Lowe 				    MSG_INTL(MSG_ERR_DWOVRFLW), file,
671*37915d86SRichard Lowe 				    _cache->c_name);
672*37915d86SRichard Lowe 				return;
673*37915d86SRichard Lowe 			case DW_BAD_ENCODING:
674*37915d86SRichard Lowe 				(void) fprintf(stderr,
675*37915d86SRichard Lowe 				    MSG_INTL(MSG_ERR_DWBADENC), file,
676*37915d86SRichard Lowe 				    _cache->c_name, table_enc);
677*37915d86SRichard Lowe 				return;
678*37915d86SRichard Lowe 			case DW_SUCCESS:
679*37915d86SRichard Lowe 				break;
680*37915d86SRichard Lowe 			}
681*37915d86SRichard Lowe 
6825aefb655Srie 			dbg_print(0, MSG_ORIG(MSG_UNW_BINSRTABENT),
683e23c41c9SAli Bahrami 			    EC_XWORD(initloc),
684*37915d86SRichard Lowe 			    EC_XWORD(table));
685e23c41c9SAli Bahrami 			initloc0 = initloc;
6867c478bd9Sstevel@tonic-gate 		}
6877e16fca0SAli Bahrami 	} else {		/* Display the .eh_frame section */
688e23c41c9SAli Bahrami 		eh_state->frame_cnt++;
689e23c41c9SAli Bahrami 		if (eh_state->frame_cnt == 1) {
690e23c41c9SAli Bahrami 			eh_state->frame_ndx = shndx;
691e23c41c9SAli Bahrami 			eh_state->frame_base = shdr->sh_addr;
692e23c41c9SAli Bahrami 		} else if ((eh_state->frame_cnt >  1) &&
6937e16fca0SAli Bahrami 		    (ehdr->e_type != ET_REL)) {
6947e16fca0SAli Bahrami 			Conv_inv_buf_t	inv_buf;
6957e16fca0SAli Bahrami 
696e23c41c9SAli Bahrami 			(void) fprintf(stderr, MSG_INTL(MSG_WARN_MULTEHFRM),
697e23c41c9SAli Bahrami 			    file, EC_WORD(shndx), _cache->c_name,
698e23c41c9SAli Bahrami 			    conv_ehdr_type(osabi, ehdr->e_type, 0, &inv_buf));
6997e16fca0SAli Bahrami 		}
700*37915d86SRichard Lowe 		dump_eh_frame(file, _cache->c_name, data, datasize,
701*37915d86SRichard Lowe 		    shdr->sh_addr, ehdr->e_machine, ehdr->e_ident, gotaddr);
7027c478bd9Sstevel@tonic-gate 	}
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	/*
705e23c41c9SAli Bahrami 	 * If we've seen the .eh_frame_hdr and the first .eh_frame section,
706e23c41c9SAli Bahrami 	 * compare the header frame_ptr to the address of the actual frame
707e23c41c9SAli Bahrami 	 * section to ensure the link-editor got this right.  Note, this
708e23c41c9SAli Bahrami 	 * diagnostic is only produced when unwind information is explicitly
709e23c41c9SAli Bahrami 	 * asked for, as shared objects built with an older ld(1) may reveal
710e23c41c9SAli Bahrami 	 * this inconsistency.  Although an inconsistency, it doesn't seem to
711e23c41c9SAli Bahrami 	 * have any adverse effect on existing tools.
7127c478bd9Sstevel@tonic-gate 	 */
71357ef7aa9SRod Evans 	if (((flags & FLG_MASK_SHOW) != FLG_MASK_SHOW) &&
714e23c41c9SAli Bahrami 	    (eh_state->hdr_cnt > 0) && (eh_state->frame_cnt > 0) &&
715e23c41c9SAli Bahrami 	    (eh_state->frame_ptr != eh_state->frame_base))
7167e16fca0SAli Bahrami 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADEHFRMPTR),
717e23c41c9SAli Bahrami 		    file, EC_WORD(eh_state->hdr_ndx),
718e23c41c9SAli Bahrami 		    cache[eh_state->hdr_ndx].c_name,
719e23c41c9SAli Bahrami 		    EC_XWORD(eh_state->frame_ptr),
720e23c41c9SAli Bahrami 		    EC_WORD(eh_state->frame_ndx),
721e23c41c9SAli Bahrami 		    cache[eh_state->frame_ndx].c_name,
722e23c41c9SAli Bahrami 		    EC_XWORD(eh_state->frame_base));
7237e16fca0SAli Bahrami #undef MSG_UNW_BINSRTAB2
7247e16fca0SAli Bahrami #undef MSG_UNW_BINSRTABENT
7257c478bd9Sstevel@tonic-gate }
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate /*
728e23c41c9SAli Bahrami  * Convert a self relative pointer into an address. A self relative
729e23c41c9SAli Bahrami  * pointer adds the address where the pointer resides to the offset
730e23c41c9SAli Bahrami  * contained in the pointer. The benefit is that the value of the
731e23c41c9SAli Bahrami  * pointer does not require relocation.
732e23c41c9SAli Bahrami  *
733e23c41c9SAli Bahrami  * entry:
734e23c41c9SAli Bahrami  *	base_addr - Address of the pointer.
735e23c41c9SAli Bahrami  *	delta - Offset relative to base_addr giving desired address
736e23c41c9SAli Bahrami  *
737e23c41c9SAli Bahrami  * exit:
738e23c41c9SAli Bahrami  *	The computed address is returned.
739e23c41c9SAli Bahrami  *
740e23c41c9SAli Bahrami  * note:
741e23c41c9SAli Bahrami  *	base_addr is an unsigned value, while ret_addr is signed. This routine
742e23c41c9SAli Bahrami  *	used explicit testing and casting to explicitly control type
743e23c41c9SAli Bahrami  *	conversion, and ensure that we handle the maximum possible range.
744e23c41c9SAli Bahrami  */
745e23c41c9SAli Bahrami static Addr
srelptr(Addr base_addr,PTRDIFF_T delta)746e23c41c9SAli Bahrami srelptr(Addr base_addr, PTRDIFF_T delta)
747e23c41c9SAli Bahrami {
748e23c41c9SAli Bahrami 	if (delta < 0)
749e23c41c9SAli Bahrami 		return (base_addr - (Addr) (-delta));
750e23c41c9SAli Bahrami 
751e23c41c9SAli Bahrami 	return (base_addr + (Addr) delta);
752e23c41c9SAli Bahrami }
753e23c41c9SAli Bahrami 
754e23c41c9SAli Bahrami /*
755e23c41c9SAli Bahrami  * Byte swap a PTRDIFF_T value.
756e23c41c9SAli Bahrami  */
757e23c41c9SAli Bahrami static PTRDIFF_T
swap_ptrdiff(PTRDIFF_T value)758e23c41c9SAli Bahrami swap_ptrdiff(PTRDIFF_T value)
759e23c41c9SAli Bahrami {
760e23c41c9SAli Bahrami 	PTRDIFF_T r;
761e23c41c9SAli Bahrami 	uchar_t	*dst = (uchar_t *)&r;
762e23c41c9SAli Bahrami 	uchar_t	*src = (uchar_t *)&value;
763e23c41c9SAli Bahrami 
764e23c41c9SAli Bahrami 	UL_ASSIGN_BSWAP_XWORD(dst, src);
765e23c41c9SAli Bahrami 	return (r);
766e23c41c9SAli Bahrami }
767e23c41c9SAli Bahrami 
768e23c41c9SAli Bahrami /*
769e23c41c9SAli Bahrami  * Display exception_range_entry items from the .exception_ranges section
770e23c41c9SAli Bahrami  * of a Sun C++ object.
771e23c41c9SAli Bahrami  */
772e23c41c9SAli Bahrami static void
unwind_exception_ranges(Cache * _cache,const char * file,int do_swap)773e23c41c9SAli Bahrami unwind_exception_ranges(Cache *_cache, const char *file, int do_swap)
774e23c41c9SAli Bahrami {
775e23c41c9SAli Bahrami 	/*
776e23c41c9SAli Bahrami 	 * Translate a PTRDIFF_T self-relative address field of
777e23c41c9SAli Bahrami 	 * an exception_range_entry struct into an address.
778e23c41c9SAli Bahrami 	 *
779e23c41c9SAli Bahrami 	 * entry:
780e23c41c9SAli Bahrami 	 *	exc_addr - Address of base of exception_range_entry struct
781e23c41c9SAli Bahrami 	 *	cur_ent - Pointer to data in the struct to be translated
782e23c41c9SAli Bahrami 	 *
783e23c41c9SAli Bahrami 	 *	_f - Field of struct to be translated
784e23c41c9SAli Bahrami 	 */
785e23c41c9SAli Bahrami #define	SRELPTR(_f) \
786e23c41c9SAli Bahrami 	srelptr(exc_addr + offsetof(exception_range_entry, _f), cur_ent->_f)
787e23c41c9SAli Bahrami 
788e23c41c9SAli Bahrami #if	defined(_ELF64)
789e23c41c9SAli Bahrami #define	MSG_EXR_TITLE	MSG_EXR_TITLE_64
790e23c41c9SAli Bahrami #define	MSG_EXR_ENTRY	MSG_EXR_ENTRY_64
791e23c41c9SAli Bahrami #else
792e23c41c9SAli Bahrami #define	MSG_EXR_TITLE	MSG_EXR_TITLE_32
793e23c41c9SAli Bahrami #define	MSG_EXR_ENTRY	MSG_EXR_ENTRY_32
794e23c41c9SAli Bahrami #endif
795e23c41c9SAli Bahrami 
796e23c41c9SAli Bahrami 	exception_range_entry	scratch, *ent, *cur_ent = &scratch;
797e23c41c9SAli Bahrami 	char			index[MAXNDXSIZE];
798e23c41c9SAli Bahrami 	Word			i, nelts;
799*37915d86SRichard Lowe 	Addr			addr, addr0 = 0, offset = 0;
800e23c41c9SAli Bahrami 	Addr			exc_addr = _cache->c_shdr->sh_addr;
801e23c41c9SAli Bahrami 
802e23c41c9SAli Bahrami 	dbg_print(0, MSG_INTL(MSG_EXR_TITLE));
803e23c41c9SAli Bahrami 	ent = (exception_range_entry *)(_cache->c_data->d_buf);
804e23c41c9SAli Bahrami 	nelts = _cache->c_data->d_size / sizeof (exception_range_entry);
805e23c41c9SAli Bahrami 
806e23c41c9SAli Bahrami 	for (i = 0; i < nelts; i++, ent++) {
807e23c41c9SAli Bahrami 		if (do_swap) {
808e23c41c9SAli Bahrami 			/*
809e23c41c9SAli Bahrami 			 * Copy byte swapped values into the scratch buffer.
810e23c41c9SAli Bahrami 			 * The reserved field is not used, so we skip it.
811e23c41c9SAli Bahrami 			 */
812e23c41c9SAli Bahrami 			scratch.ret_addr = swap_ptrdiff(ent->ret_addr);
813e23c41c9SAli Bahrami 			scratch.length = BSWAP_XWORD(ent->length);
814e23c41c9SAli Bahrami 			scratch.handler_addr = swap_ptrdiff(ent->handler_addr);
815e23c41c9SAli Bahrami 			scratch.type_block = swap_ptrdiff(ent->type_block);
816e23c41c9SAli Bahrami 		} else {
817e23c41c9SAli Bahrami 			cur_ent = ent;
818e23c41c9SAli Bahrami 		}
819e23c41c9SAli Bahrami 
820e23c41c9SAli Bahrami 		/*
821e23c41c9SAli Bahrami 		 * The table is required to be sorted by the address
822e23c41c9SAli Bahrami 		 * derived from ret_addr, to allow binary searching. Ensure
823e23c41c9SAli Bahrami 		 * that addresses grow monotonically.
824e23c41c9SAli Bahrami 		 */
825e23c41c9SAli Bahrami 		addr = SRELPTR(ret_addr);
826e23c41c9SAli Bahrami 		if ((i != 0) && (addr0 > addr))
8271dd9d86fSAli Bahrami 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSORT),
828e23c41c9SAli Bahrami 			    file, _cache->c_name, EC_WORD(i));
829e23c41c9SAli Bahrami 
830e23c41c9SAli Bahrami 		(void) snprintf(index, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INDEX),
831e23c41c9SAli Bahrami 		    EC_XWORD(i));
832e23c41c9SAli Bahrami 		dbg_print(0, MSG_INTL(MSG_EXR_ENTRY), index, EC_ADDR(offset),
833e23c41c9SAli Bahrami 		    EC_ADDR(addr), EC_ADDR(cur_ent->length),
834e23c41c9SAli Bahrami 		    EC_ADDR(SRELPTR(handler_addr)),
835e23c41c9SAli Bahrami 		    EC_ADDR(SRELPTR(type_block)));
836e23c41c9SAli Bahrami 
837e23c41c9SAli Bahrami 		addr0 = addr;
838e23c41c9SAli Bahrami 		exc_addr += sizeof (exception_range_entry);
839e23c41c9SAli Bahrami 		offset += sizeof (exception_range_entry);
840e23c41c9SAli Bahrami 	}
841e23c41c9SAli Bahrami 
842e23c41c9SAli Bahrami #undef SRELPTR
843e23c41c9SAli Bahrami #undef MSG_EXR_TITLE
844e23c41c9SAli Bahrami #undef MSG_EXR_ENTRY
845e23c41c9SAli Bahrami }
846e23c41c9SAli Bahrami 
847e23c41c9SAli Bahrami /*
848e23c41c9SAli Bahrami  * Display information from unwind/exception sections:
849e23c41c9SAli Bahrami  *
850e23c41c9SAli Bahrami  * -	GNU/amd64 .eh_frame and .eh_frame_hdr
851e23c41c9SAli Bahrami  * -	Sun C++ .exception_ranges
852e23c41c9SAli Bahrami  *
853e23c41c9SAli Bahrami  */
854e23c41c9SAli Bahrami static void
unwind(Cache * cache,Word shnum,Word phnum,Ehdr * ehdr,uchar_t osabi,const char * file,Elf * elf,uint_t flags)855e23c41c9SAli Bahrami unwind(Cache *cache, Word shnum, Word phnum, Ehdr *ehdr, uchar_t osabi,
856e23c41c9SAli Bahrami     const char *file, Elf *elf, uint_t flags)
857e23c41c9SAli Bahrami {
858e23c41c9SAli Bahrami 	static Word phdr_types[] = { PT_SUNW_UNWIND, PT_SUNW_EH_FRAME };
859e23c41c9SAli Bahrami 
860e23c41c9SAli Bahrami 	Word			cnt;
861e23c41c9SAli Bahrami 	Phdr			*uphdr = NULL;
862e23c41c9SAli Bahrami 	gnu_eh_state_t		eh_state;
863e23c41c9SAli Bahrami 
864e23c41c9SAli Bahrami 	/*
865e23c41c9SAli Bahrami 	 * Historical background: .eh_frame and .eh_frame_hdr sections
866e23c41c9SAli Bahrami 	 * come from the GNU compilers (particularly C++), and are used
867e23c41c9SAli Bahrami 	 * under all architectures. Their format is based on DWARF. When
868e23c41c9SAli Bahrami 	 * the amd64 ABI was defined, these sections were adopted wholesale
869e23c41c9SAli Bahrami 	 * from the existing practice.
870e23c41c9SAli Bahrami 	 *
871e23c41c9SAli Bahrami 	 * When amd64 support was added to Solaris, support for these
872e23c41c9SAli Bahrami 	 * sections was added, using the SHT_AMD64_UNWIND section type
873e23c41c9SAli Bahrami 	 * to identify them. At first, we ignored them in objects for
874e23c41c9SAli Bahrami 	 * non-amd64 targets, but later broadened our support to include
875e23c41c9SAli Bahrami 	 * other architectures in order to better support gcc-generated
876e23c41c9SAli Bahrami 	 * objects.
877e23c41c9SAli Bahrami 	 *
878e23c41c9SAli Bahrami 	 * .exception_ranges implement the same basic concepts, but
879e23c41c9SAli Bahrami 	 * were invented at Sun for the Sun C++ compiler.
880e23c41c9SAli Bahrami 	 *
881e23c41c9SAli Bahrami 	 * We match these sections by name, rather than section type,
882e23c41c9SAli Bahrami 	 * because they can come in as either SHT_AMD64_UNWIND, or as
883e23c41c9SAli Bahrami 	 * SHT_PROGBITS, and because the type isn't enough to determine
88408278a5eSRod Evans 	 * how they should be interpreted.
885e23c41c9SAli Bahrami 	 */
886e23c41c9SAli Bahrami 	/* Find the program header for .eh_frame_hdr if present */
887e23c41c9SAli Bahrami 	if (phnum)
888e23c41c9SAli Bahrami 		uphdr = getphdr(phnum, phdr_types,
889e23c41c9SAli Bahrami 		    sizeof (phdr_types) / sizeof (*phdr_types), file, elf);
890e23c41c9SAli Bahrami 
891e23c41c9SAli Bahrami 	/*
892e23c41c9SAli Bahrami 	 * eh_state is used to retain data used by unwind_eh_frame()
89308278a5eSRod Evans 	 * across calls.
894e23c41c9SAli Bahrami 	 */
895e23c41c9SAli Bahrami 	bzero(&eh_state, sizeof (eh_state));
896e23c41c9SAli Bahrami 
897e23c41c9SAli Bahrami 	for (cnt = 1; cnt < shnum; cnt++) {
898e23c41c9SAli Bahrami 		Cache		*_cache = &cache[cnt];
899e23c41c9SAli Bahrami 		Shdr		*shdr = _cache->c_shdr;
900e23c41c9SAli Bahrami 		int		is_exrange;
901e23c41c9SAli Bahrami 
902e23c41c9SAli Bahrami 		/*
903e23c41c9SAli Bahrami 		 * Skip sections of the wrong type. On amd64, they
904e23c41c9SAli Bahrami 		 * can be SHT_AMD64_UNWIND. On all platforms, they
905e23c41c9SAli Bahrami 		 * can be SHT_PROGBITS (including amd64, if using
906e23c41c9SAli Bahrami 		 * the GNU compilers).
907e23c41c9SAli Bahrami 		 *
908e23c41c9SAli Bahrami 		 * Skip anything other than these two types. The name
909e23c41c9SAli Bahrami 		 * test below will thin out the SHT_PROGBITS that don't apply.
910e23c41c9SAli Bahrami 		 */
911e23c41c9SAli Bahrami 		if ((shdr->sh_type != SHT_PROGBITS) &&
912e23c41c9SAli Bahrami 		    (shdr->sh_type != SHT_AMD64_UNWIND))
913e23c41c9SAli Bahrami 			continue;
914e23c41c9SAli Bahrami 
915e23c41c9SAli Bahrami 		/*
916e23c41c9SAli Bahrami 		 * Only sections with certain well known names are of interest.
917e23c41c9SAli Bahrami 		 * These are:
918e23c41c9SAli Bahrami 		 *
919e23c41c9SAli Bahrami 		 *	.eh_frame - amd64/GNU-compiler unwind sections
920e23c41c9SAli Bahrami 		 *	.eh_frame_hdr - Sorted table referencing .eh_frame
921e23c41c9SAli Bahrami 		 *	.exception_ranges - Sun C++ unwind sections
922e23c41c9SAli Bahrami 		 *
923e23c41c9SAli Bahrami 		 * We do a prefix comparison, allowing for naming conventions
924e23c41c9SAli Bahrami 		 * like .eh_frame.foo, hence the use of strncmp() rather than
925e23c41c9SAli Bahrami 		 * strcmp(). This means that we only really need to test for
926e23c41c9SAli Bahrami 		 * .eh_frame, as it's a prefix of .eh_frame_hdr.
927e23c41c9SAli Bahrami 		 */
928e23c41c9SAli Bahrami 		is_exrange =  strncmp(_cache->c_name,
929e23c41c9SAli Bahrami 		    MSG_ORIG(MSG_SCN_EXRANGE), MSG_SCN_EXRANGE_SIZE) == 0;
930e23c41c9SAli Bahrami 		if ((strncmp(_cache->c_name, MSG_ORIG(MSG_SCN_FRM),
931e23c41c9SAli Bahrami 		    MSG_SCN_FRM_SIZE) != 0) && !is_exrange)
932e23c41c9SAli Bahrami 			continue;
933e23c41c9SAli Bahrami 
934e23c41c9SAli Bahrami 		if (!match(MATCH_F_ALL, _cache->c_name, cnt, shdr->sh_type))
935e23c41c9SAli Bahrami 			continue;
936e23c41c9SAli Bahrami 
937e23c41c9SAli Bahrami 		if (_cache->c_data == NULL)
938e23c41c9SAli Bahrami 			continue;
939e23c41c9SAli Bahrami 
940e23c41c9SAli Bahrami 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
941e23c41c9SAli Bahrami 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_UNWIND), _cache->c_name);
942e23c41c9SAli Bahrami 
943e23c41c9SAli Bahrami 		if (is_exrange)
944e23c41c9SAli Bahrami 			unwind_exception_ranges(_cache, file,
945e23c41c9SAli Bahrami 			    _elf_sys_encoding() != ehdr->e_ident[EI_DATA]);
946e23c41c9SAli Bahrami 		else
947965630c1SRichard Lowe 			unwind_eh_frame(cache, cnt, shnum, uphdr, ehdr,
948965630c1SRichard Lowe 			    &eh_state, osabi, file, flags);
949e23c41c9SAli Bahrami 	}
950e23c41c9SAli Bahrami }
951e23c41c9SAli Bahrami 
952e23c41c9SAli Bahrami /*
95308278a5eSRod Evans  * Initialize a symbol table state structure
95408278a5eSRod Evans  *
95508278a5eSRod Evans  * entry:
95608278a5eSRod Evans  *	state - State structure to be initialized
95708278a5eSRod Evans  *	cache - Cache of all section headers
95808278a5eSRod Evans  *	shnum - # of sections in cache
95908278a5eSRod Evans  *	secndx - Index of symbol table section
96008278a5eSRod Evans  *	ehdr - ELF header for file
96108278a5eSRod Evans  *	versym - Information about versym section
96208278a5eSRod Evans  *	file - Name of file
96308278a5eSRod Evans  *	flags - Command line option flags
96408278a5eSRod Evans  */
96508278a5eSRod Evans static int
init_symtbl_state(SYMTBL_STATE * state,Cache * cache,Word shnum,Word secndx,Ehdr * ehdr,uchar_t osabi,VERSYM_STATE * versym,const char * file,uint_t flags)96608278a5eSRod Evans init_symtbl_state(SYMTBL_STATE *state, Cache *cache, Word shnum, Word secndx,
96708278a5eSRod Evans     Ehdr *ehdr, uchar_t osabi, VERSYM_STATE *versym, const char *file,
96808278a5eSRod Evans     uint_t flags)
96908278a5eSRod Evans {
97008278a5eSRod Evans 	Shdr *shdr;
97108278a5eSRod Evans 
97208278a5eSRod Evans 	state->file = file;
97308278a5eSRod Evans 	state->ehdr = ehdr;
97408278a5eSRod Evans 	state->cache = cache;
97508278a5eSRod Evans 	state->osabi = osabi;
97608278a5eSRod Evans 	state->shnum = shnum;
97708278a5eSRod Evans 	state->seccache = &cache[secndx];
97808278a5eSRod Evans 	state->secndx = secndx;
97908278a5eSRod Evans 	state->secname = state->seccache->c_name;
98008278a5eSRod Evans 	state->flags = flags;
98108278a5eSRod Evans 	state->shxndx.checked = 0;
98208278a5eSRod Evans 	state->shxndx.data = NULL;
98308278a5eSRod Evans 	state->shxndx.n = 0;
98408278a5eSRod Evans 
98508278a5eSRod Evans 	shdr = state->seccache->c_shdr;
98608278a5eSRod Evans 
98708278a5eSRod Evans 	/*
98808278a5eSRod Evans 	 * Check the symbol data and per-item size.
98908278a5eSRod Evans 	 */
99008278a5eSRod Evans 	if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
99108278a5eSRod Evans 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
99208278a5eSRod Evans 		    file, state->secname);
99308278a5eSRod Evans 		return (0);
99408278a5eSRod Evans 	}
99508278a5eSRod Evans 	if (state->seccache->c_data == NULL)
99608278a5eSRod Evans 		return (0);
99708278a5eSRod Evans 
99808278a5eSRod Evans 	/* LINTED */
99908278a5eSRod Evans 	state->symn = (Word)(shdr->sh_size / shdr->sh_entsize);
100008278a5eSRod Evans 	state->sym = (Sym *)state->seccache->c_data->d_buf;
100108278a5eSRod Evans 
100208278a5eSRod Evans 	/*
100308278a5eSRod Evans 	 * Check associated string table section.
100408278a5eSRod Evans 	 */
100508278a5eSRod Evans 	if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
100608278a5eSRod Evans 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
100708278a5eSRod Evans 		    file, state->secname, EC_WORD(shdr->sh_link));
100808278a5eSRod Evans 		return (0);
100908278a5eSRod Evans 	}
101008278a5eSRod Evans 
101108278a5eSRod Evans 	/*
101208278a5eSRod Evans 	 * Determine if there is a associated Versym section
101308278a5eSRod Evans 	 * with this Symbol Table.
101408278a5eSRod Evans 	 */
101508278a5eSRod Evans 	if (versym && versym->cache &&
101608278a5eSRod Evans 	    (versym->cache->c_shdr->sh_link == state->secndx))
101708278a5eSRod Evans 		state->versym = versym;
101808278a5eSRod Evans 	else
101908278a5eSRod Evans 		state->versym = NULL;
102008278a5eSRod Evans 
102108278a5eSRod Evans 
102208278a5eSRod Evans 	return (1);
102308278a5eSRod Evans }
102408278a5eSRod Evans 
102508278a5eSRod Evans /*
102608278a5eSRod Evans  * Determine the extended section index used for symbol tables entries.
102708278a5eSRod Evans  */
102808278a5eSRod Evans static void
symbols_getxindex(SYMTBL_STATE * state)102908278a5eSRod Evans symbols_getxindex(SYMTBL_STATE *state)
103008278a5eSRod Evans {
103108278a5eSRod Evans 	uint_t	symn;
103208278a5eSRod Evans 	Word	symcnt;
103308278a5eSRod Evans 
103408278a5eSRod Evans 	state->shxndx.checked = 1;   /* Note that we've been called */
103508278a5eSRod Evans 	for (symcnt = 1; symcnt < state->shnum; symcnt++) {
103608278a5eSRod Evans 		Cache	*_cache = &state->cache[symcnt];
103708278a5eSRod Evans 		Shdr	*shdr = _cache->c_shdr;
103808278a5eSRod Evans 
103908278a5eSRod Evans 		if ((shdr->sh_type != SHT_SYMTAB_SHNDX) ||
104008278a5eSRod Evans 		    (shdr->sh_link != state->secndx))
104108278a5eSRod Evans 			continue;
104208278a5eSRod Evans 
104308278a5eSRod Evans 		if ((shdr->sh_entsize) &&
104408278a5eSRod Evans 		    /* LINTED */
104508278a5eSRod Evans 		    ((symn = (uint_t)(shdr->sh_size / shdr->sh_entsize)) == 0))
104608278a5eSRod Evans 			continue;
104708278a5eSRod Evans 
104808278a5eSRod Evans 		if (_cache->c_data == NULL)
104908278a5eSRod Evans 			continue;
105008278a5eSRod Evans 
105108278a5eSRod Evans 		state->shxndx.data = _cache->c_data->d_buf;
105208278a5eSRod Evans 		state->shxndx.n = symn;
105308278a5eSRod Evans 		return;
105408278a5eSRod Evans 	}
105508278a5eSRod Evans }
105608278a5eSRod Evans 
105708278a5eSRod Evans /*
105808278a5eSRod Evans  * Produce a line of output for the given symbol
105908278a5eSRod Evans  *
106008278a5eSRod Evans  * entry:
106108278a5eSRod Evans  *	state - Symbol table state
106208278a5eSRod Evans  *	symndx - Index of symbol within the table
106308278a5eSRod Evans  *	info - Value of st_info (indicates local/global range)
106408278a5eSRod Evans  *	symndx_disp - Index to display. This may not be the same
106508278a5eSRod Evans  *		as symndx if the display is relative to the logical
106608278a5eSRod Evans  *		combination of the SUNW_ldynsym/dynsym tables.
106708278a5eSRod Evans  *	sym - Symbol to display
106808278a5eSRod Evans  */
106908278a5eSRod Evans static void
output_symbol(SYMTBL_STATE * state,Word symndx,Word info,Word disp_symndx,Sym * sym)107008278a5eSRod Evans output_symbol(SYMTBL_STATE *state, Word symndx, Word info, Word disp_symndx,
107108278a5eSRod Evans     Sym *sym)
107208278a5eSRod Evans {
107308278a5eSRod Evans 	/*
107408278a5eSRod Evans 	 * Symbol types for which we check that the specified
107508278a5eSRod Evans 	 * address/size land inside the target section.
107608278a5eSRod Evans 	 */
107708278a5eSRod Evans 	static const int addr_symtype[] = {
107808278a5eSRod Evans 		0,			/* STT_NOTYPE */
107908278a5eSRod Evans 		1,			/* STT_OBJECT */
108008278a5eSRod Evans 		1,			/* STT_FUNC */
108108278a5eSRod Evans 		0,			/* STT_SECTION */
108208278a5eSRod Evans 		0,			/* STT_FILE */
108308278a5eSRod Evans 		1,			/* STT_COMMON */
108408278a5eSRod Evans 		0,			/* STT_TLS */
108508278a5eSRod Evans 		0,			/* 7 */
108608278a5eSRod Evans 		0,			/* 8 */
108708278a5eSRod Evans 		0,			/* 9 */
108808278a5eSRod Evans 		0,			/* 10 */
108908278a5eSRod Evans 		0,			/* 11 */
109008278a5eSRod Evans 		0,			/* 12 */
109108278a5eSRod Evans 		0,			/* STT_SPARC_REGISTER */
109208278a5eSRod Evans 		0,			/* 14 */
109308278a5eSRod Evans 		0,			/* 15 */
109408278a5eSRod Evans 	};
109508278a5eSRod Evans #if STT_NUM != (STT_TLS + 1)
109608278a5eSRod Evans #error "STT_NUM has grown. Update addr_symtype[]"
109708278a5eSRod Evans #endif
109808278a5eSRod Evans 
109908278a5eSRod Evans 	char		index[MAXNDXSIZE];
110008278a5eSRod Evans 	const char	*symname, *sec;
110108278a5eSRod Evans 	Versym		verndx;
110208278a5eSRod Evans 	int		gnuver;
110308278a5eSRod Evans 	uchar_t		type;
110408278a5eSRod Evans 	Shdr		*tshdr;
110508278a5eSRod Evans 	Word		shndx;
110608278a5eSRod Evans 	Conv_inv_buf_t	inv_buf;
110708278a5eSRod Evans 
110808278a5eSRod Evans 	/* Ensure symbol index is in range */
110908278a5eSRod Evans 	if (symndx >= state->symn) {
111008278a5eSRod Evans 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSYMNDX),
111108278a5eSRod Evans 		    state->file, state->secname, EC_WORD(symndx));
111208278a5eSRod Evans 		return;
111308278a5eSRod Evans 	}
111408278a5eSRod Evans 
111508278a5eSRod Evans 	/*
111608278a5eSRod Evans 	 * If we are using extended symbol indexes, find the
111708278a5eSRod Evans 	 * corresponding SHN_SYMTAB_SHNDX table.
111808278a5eSRod Evans 	 */
111908278a5eSRod Evans 	if ((sym->st_shndx == SHN_XINDEX) && (state->shxndx.checked == 0))
112008278a5eSRod Evans 		symbols_getxindex(state);
112108278a5eSRod Evans 
112208278a5eSRod Evans 	/* LINTED */
112308278a5eSRod Evans 	symname = string(state->seccache, symndx,
112408278a5eSRod Evans 	    &state->cache[state->seccache->c_shdr->sh_link], state->file,
112508278a5eSRod Evans 	    sym->st_name);
112608278a5eSRod Evans 
112708278a5eSRod Evans 	tshdr = NULL;
112808278a5eSRod Evans 	sec = NULL;
112908278a5eSRod Evans 
113008278a5eSRod Evans 	if (state->ehdr->e_type == ET_CORE) {
113108278a5eSRod Evans 		sec = (char *)MSG_INTL(MSG_STR_UNKNOWN);
113208278a5eSRod Evans 	} else if (state->flags & FLG_CTL_FAKESHDR) {
113308278a5eSRod Evans 		/*
113408278a5eSRod Evans 		 * If we are using fake section headers derived from
113508278a5eSRod Evans 		 * the program headers, then the section indexes
113608278a5eSRod Evans 		 * in the symbols do not correspond to these headers.
113708278a5eSRod Evans 		 * The section names are not available, so all we can
113808278a5eSRod Evans 		 * do is to display them in numeric form.
113908278a5eSRod Evans 		 */
114008278a5eSRod Evans 		sec = conv_sym_shndx(state->osabi, state->ehdr->e_machine,
114108278a5eSRod Evans 		    sym->st_shndx, CONV_FMT_DECIMAL, &inv_buf);
114208278a5eSRod Evans 	} else if ((sym->st_shndx < SHN_LORESERVE) &&
114308278a5eSRod Evans 	    (sym->st_shndx < state->shnum)) {
114408278a5eSRod Evans 		shndx = sym->st_shndx;
114508278a5eSRod Evans 		tshdr = state->cache[shndx].c_shdr;
114608278a5eSRod Evans 		sec = state->cache[shndx].c_name;
114708278a5eSRod Evans 	} else if (sym->st_shndx == SHN_XINDEX) {
114808278a5eSRod Evans 		if (state->shxndx.data) {
114908278a5eSRod Evans 			Word	_shxndx;
115008278a5eSRod Evans 
115108278a5eSRod Evans 			if (symndx > state->shxndx.n) {
115208278a5eSRod Evans 				(void) fprintf(stderr,
115308278a5eSRod Evans 				    MSG_INTL(MSG_ERR_BADSYMXINDEX1),
115408278a5eSRod Evans 				    state->file, state->secname,
115508278a5eSRod Evans 				    EC_WORD(symndx));
115608278a5eSRod Evans 			} else if ((_shxndx =
115708278a5eSRod Evans 			    state->shxndx.data[symndx]) > state->shnum) {
115808278a5eSRod Evans 				(void) fprintf(stderr,
115908278a5eSRod Evans 				    MSG_INTL(MSG_ERR_BADSYMXINDEX2),
116008278a5eSRod Evans 				    state->file, state->secname,
116108278a5eSRod Evans 				    EC_WORD(symndx), EC_WORD(_shxndx));
116208278a5eSRod Evans 			} else {
116308278a5eSRod Evans 				shndx = _shxndx;
116408278a5eSRod Evans 				tshdr = state->cache[shndx].c_shdr;
116508278a5eSRod Evans 				sec = state->cache[shndx].c_name;
116608278a5eSRod Evans 			}
116708278a5eSRod Evans 		} else {
116808278a5eSRod Evans 			(void) fprintf(stderr,
116908278a5eSRod Evans 			    MSG_INTL(MSG_ERR_BADSYMXINDEX3),
117008278a5eSRod Evans 			    state->file, state->secname, EC_WORD(symndx));
117108278a5eSRod Evans 		}
117208278a5eSRod Evans 	} else if ((sym->st_shndx < SHN_LORESERVE) &&
117308278a5eSRod Evans 	    (sym->st_shndx >= state->shnum)) {
117408278a5eSRod Evans 		(void) fprintf(stderr,
117508278a5eSRod Evans 		    MSG_INTL(MSG_ERR_BADSYM5), state->file,
117608278a5eSRod Evans 		    state->secname, EC_WORD(symndx),
117708278a5eSRod Evans 		    demangle(symname, state->flags), sym->st_shndx);
117808278a5eSRod Evans 	}
117908278a5eSRod Evans 
118008278a5eSRod Evans 	/*
118108278a5eSRod Evans 	 * If versioning is available display the
118208278a5eSRod Evans 	 * version index. If not, then use 0.
118308278a5eSRod Evans 	 */
118408278a5eSRod Evans 	if (state->versym) {
118508278a5eSRod Evans 		Versym test_verndx;
118608278a5eSRod Evans 
118708278a5eSRod Evans 		verndx = test_verndx = state->versym->data[symndx];
118808278a5eSRod Evans 		gnuver = state->versym->gnu_full;
118908278a5eSRod Evans 
119008278a5eSRod Evans 		/*
119108278a5eSRod Evans 		 * Check to see if this is a defined symbol with a
119208278a5eSRod Evans 		 * version index that is outside the valid range for
119308278a5eSRod Evans 		 * the file. The interpretation of this depends on
119408278a5eSRod Evans 		 * the style of versioning used by the object.
119508278a5eSRod Evans 		 *
119608278a5eSRod Evans 		 * Versions >= VER_NDX_LORESERVE have special meanings,
119708278a5eSRod Evans 		 * and are exempt from this checking.
119808278a5eSRod Evans 		 *
119908278a5eSRod Evans 		 * GNU style version indexes use the top bit of the
120008278a5eSRod Evans 		 * 16-bit index value (0x8000) as the "hidden bit".
120108278a5eSRod Evans 		 * We must mask off this bit in order to compare
120208278a5eSRod Evans 		 * the version against the maximum value.
120308278a5eSRod Evans 		 */
120408278a5eSRod Evans 		if (gnuver)
120508278a5eSRod Evans 			test_verndx &= ~0x8000;
120608278a5eSRod Evans 
120708278a5eSRod Evans 		if ((test_verndx > state->versym->max_verndx) &&
120808278a5eSRod Evans 		    (verndx < VER_NDX_LORESERVE))
120908278a5eSRod Evans 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADVER),
121008278a5eSRod Evans 			    state->file, state->secname, EC_WORD(symndx),
121108278a5eSRod Evans 			    EC_HALF(test_verndx), state->versym->max_verndx);
121208278a5eSRod Evans 	} else {
121308278a5eSRod Evans 		verndx = 0;
121408278a5eSRod Evans 		gnuver = 0;
121508278a5eSRod Evans 	}
121608278a5eSRod Evans 
121708278a5eSRod Evans 	/*
121808278a5eSRod Evans 	 * Error checking for TLS.
121908278a5eSRod Evans 	 */
122008278a5eSRod Evans 	type = ELF_ST_TYPE(sym->st_info);
122108278a5eSRod Evans 	if (type == STT_TLS) {
122208278a5eSRod Evans 		if (tshdr &&
122308278a5eSRod Evans 		    (sym->st_shndx != SHN_UNDEF) &&
122408278a5eSRod Evans 		    ((tshdr->sh_flags & SHF_TLS) == 0)) {
122508278a5eSRod Evans 			(void) fprintf(stderr,
122608278a5eSRod Evans 			    MSG_INTL(MSG_ERR_BADSYM3), state->file,
122708278a5eSRod Evans 			    state->secname, EC_WORD(symndx),
122808278a5eSRod Evans 			    demangle(symname, state->flags));
122908278a5eSRod Evans 		}
123008278a5eSRod Evans 	} else if ((type != STT_SECTION) && sym->st_size &&
123108278a5eSRod Evans 	    tshdr && (tshdr->sh_flags & SHF_TLS)) {
123208278a5eSRod Evans 		(void) fprintf(stderr,
123308278a5eSRod Evans 		    MSG_INTL(MSG_ERR_BADSYM4), state->file,
123408278a5eSRod Evans 		    state->secname, EC_WORD(symndx),
123508278a5eSRod Evans 		    demangle(symname, state->flags));
123608278a5eSRod Evans 	}
123708278a5eSRod Evans 
123808278a5eSRod Evans 	/*
123908278a5eSRod Evans 	 * If a symbol with non-zero size has a type that
124008278a5eSRod Evans 	 * specifies an address, then make sure the location
124108278a5eSRod Evans 	 * it references is actually contained within the
124208278a5eSRod Evans 	 * section.  UNDEF symbols don't count in this case,
124308278a5eSRod Evans 	 * so we ignore them.
124408278a5eSRod Evans 	 *
124508278a5eSRod Evans 	 * The meaning of the st_value field in a symbol
124608278a5eSRod Evans 	 * depends on the type of object. For a relocatable
124708278a5eSRod Evans 	 * object, it is the offset within the section.
124808278a5eSRod Evans 	 * For sharable objects, it is the offset relative to
124908278a5eSRod Evans 	 * the base of the object, and for other types, it is
125008278a5eSRod Evans 	 * the virtual address. To get an offset within the
125108278a5eSRod Evans 	 * section for non-ET_REL files, we subtract the
125208278a5eSRod Evans 	 * base address of the section.
125308278a5eSRod Evans 	 */
125408278a5eSRod Evans 	if (addr_symtype[type] && (sym->st_size > 0) &&
125508278a5eSRod Evans 	    (sym->st_shndx != SHN_UNDEF) && ((sym->st_shndx < SHN_LORESERVE) ||
125608278a5eSRod Evans 	    (sym->st_shndx == SHN_XINDEX)) && (tshdr != NULL)) {
125708278a5eSRod Evans 		Word v = sym->st_value;
125808278a5eSRod Evans 			if (state->ehdr->e_type != ET_REL)
125908278a5eSRod Evans 				v -= tshdr->sh_addr;
126008278a5eSRod Evans 		if (((v + sym->st_size) > tshdr->sh_size)) {
126108278a5eSRod Evans 			(void) fprintf(stderr,
126208278a5eSRod Evans 			    MSG_INTL(MSG_ERR_BADSYM6), state->file,
126308278a5eSRod Evans 			    state->secname, EC_WORD(symndx),
126408278a5eSRod Evans 			    demangle(symname, state->flags),
126508278a5eSRod Evans 			    EC_WORD(shndx), EC_XWORD(tshdr->sh_size),
126608278a5eSRod Evans 			    EC_XWORD(sym->st_value), EC_XWORD(sym->st_size));
126708278a5eSRod Evans 		}
126808278a5eSRod Evans 	}
126908278a5eSRod Evans 
127008278a5eSRod Evans 	/*
127108278a5eSRod Evans 	 * A typical symbol table uses the sh_info field to indicate one greater
127208278a5eSRod Evans 	 * than the symbol table index of the last local symbol, STB_LOCAL.
127308278a5eSRod Evans 	 * Therefore, symbol indexes less than sh_info should have local
127408278a5eSRod Evans 	 * binding.  Symbol indexes greater than, or equal to sh_info, should
127508278a5eSRod Evans 	 * have global binding.  Note, we exclude UNDEF/NOTY symbols with zero
127608278a5eSRod Evans 	 * value and size, as these symbols may be the result of an mcs(1)
127708278a5eSRod Evans 	 * section deletion.
127808278a5eSRod Evans 	 */
127908278a5eSRod Evans 	if (info) {
128008278a5eSRod Evans 		uchar_t	bind = ELF_ST_BIND(sym->st_info);
128108278a5eSRod Evans 
128208278a5eSRod Evans 		if ((symndx < info) && (bind != STB_LOCAL)) {
128308278a5eSRod Evans 			(void) fprintf(stderr,
128408278a5eSRod Evans 			    MSG_INTL(MSG_ERR_BADSYM7), state->file,
128508278a5eSRod Evans 			    state->secname, EC_WORD(symndx),
128608278a5eSRod Evans 			    demangle(symname, state->flags), EC_XWORD(info));
128708278a5eSRod Evans 
128808278a5eSRod Evans 		} else if ((symndx >= info) && (bind == STB_LOCAL) &&
128908278a5eSRod Evans 		    ((sym->st_shndx != SHN_UNDEF) ||
129008278a5eSRod Evans 		    (ELF_ST_TYPE(sym->st_info) != STT_NOTYPE) ||
129108278a5eSRod Evans 		    (sym->st_size != 0) || (sym->st_value != 0))) {
129208278a5eSRod Evans 			(void) fprintf(stderr,
129308278a5eSRod Evans 			    MSG_INTL(MSG_ERR_BADSYM8), state->file,
129408278a5eSRod Evans 			    state->secname, EC_WORD(symndx),
129508278a5eSRod Evans 			    demangle(symname, state->flags), EC_XWORD(info));
129608278a5eSRod Evans 		}
129708278a5eSRod Evans 	}
129808278a5eSRod Evans 
129908278a5eSRod Evans 	(void) snprintf(index, MAXNDXSIZE,
130008278a5eSRod Evans 	    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(disp_symndx));
130108278a5eSRod Evans 	Elf_syms_table_entry(0, ELF_DBG_ELFDUMP, index, state->osabi,
130208278a5eSRod Evans 	    state->ehdr->e_machine, sym, verndx, gnuver, sec, symname);
130308278a5eSRod Evans }
130408278a5eSRod Evans 
130508278a5eSRod Evans /*
130608278a5eSRod Evans  * Process a SHT_SUNW_cap capabilities section.
130708278a5eSRod Evans  */
130808278a5eSRod Evans static int
cap_section(const char * file,Cache * cache,Word shnum,Cache * ccache,uchar_t osabi,Ehdr * ehdr,uint_t flags)130908278a5eSRod Evans cap_section(const char *file, Cache *cache, Word shnum, Cache *ccache,
131008278a5eSRod Evans     uchar_t osabi, Ehdr *ehdr, uint_t flags)
131108278a5eSRod Evans {
131208278a5eSRod Evans 	SYMTBL_STATE	state;
131308278a5eSRod Evans 	Word		cnum, capnum, nulls, symcaps;
131408278a5eSRod Evans 	int		descapndx, objcap, title;
131508278a5eSRod Evans 	Cap		*cap = (Cap *)ccache->c_data->d_buf;
131608278a5eSRod Evans 	Shdr		*cishdr, *cshdr = ccache->c_shdr;
131708278a5eSRod Evans 	Cache		*cicache, *strcache;
131808278a5eSRod Evans 	Capinfo		*capinfo = NULL;
131908278a5eSRod Evans 	Word		capinfonum;
132008278a5eSRod Evans 	const char	*strs = NULL;
132108278a5eSRod Evans 	size_t		strs_size;
132208278a5eSRod Evans 
132308278a5eSRod Evans 	if ((cshdr->sh_entsize == 0) || (cshdr->sh_size == 0)) {
132408278a5eSRod Evans 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
132508278a5eSRod Evans 		    file, ccache->c_name);
132608278a5eSRod Evans 		return (0);
132708278a5eSRod Evans 	}
132808278a5eSRod Evans 
132908278a5eSRod Evans 	/*
133008278a5eSRod Evans 	 * If this capabilities section is associated with symbols, then the
133108278a5eSRod Evans 	 * sh_link field points to the associated capabilities information
133208278a5eSRod Evans 	 * section.  The sh_link field of the capabilities information section
133308278a5eSRod Evans 	 * points to the associated symbol table.
133408278a5eSRod Evans 	 */
133508278a5eSRod Evans 	if (cshdr->sh_link) {
133608278a5eSRod Evans 		Cache	*scache;
133708278a5eSRod Evans 		Shdr	*sshdr;
133808278a5eSRod Evans 
133908278a5eSRod Evans 		/*
134008278a5eSRod Evans 		 * Validate that the sh_link field points to a capabilities
134108278a5eSRod Evans 		 * information section.
134208278a5eSRod Evans 		 */
134308278a5eSRod Evans 		if (cshdr->sh_link >= shnum) {
134408278a5eSRod Evans 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
134508278a5eSRod Evans 			    file, ccache->c_name, EC_WORD(cshdr->sh_link));
134608278a5eSRod Evans 			return (0);
134708278a5eSRod Evans 		}
134808278a5eSRod Evans 
134908278a5eSRod Evans 		cicache = &cache[cshdr->sh_link];
135008278a5eSRod Evans 		cishdr = cicache->c_shdr;
135108278a5eSRod Evans 
135208278a5eSRod Evans 		if (cishdr->sh_type != SHT_SUNW_capinfo) {
135308278a5eSRod Evans 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_INVCAP),
135408278a5eSRod Evans 			    file, ccache->c_name, EC_WORD(cshdr->sh_link));
135508278a5eSRod Evans 			return (0);
135608278a5eSRod Evans 		}
135708278a5eSRod Evans 
135808278a5eSRod Evans 		capinfo = cicache->c_data->d_buf;
135908278a5eSRod Evans 		capinfonum = (Word)(cishdr->sh_size / cishdr->sh_entsize);
136008278a5eSRod Evans 
136108278a5eSRod Evans 		/*
136208278a5eSRod Evans 		 * Validate that the sh_link field of the capabilities
136308278a5eSRod Evans 		 * information section points to a valid symbol table.
136408278a5eSRod Evans 		 */
136508278a5eSRod Evans 		if ((cishdr->sh_link == 0) || (cishdr->sh_link >= shnum)) {
136608278a5eSRod Evans 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
136708278a5eSRod Evans 			    file, cicache->c_name, EC_WORD(cishdr->sh_link));
136808278a5eSRod Evans 			return (0);
136908278a5eSRod Evans 		}
137008278a5eSRod Evans 		scache = &cache[cishdr->sh_link];
137108278a5eSRod Evans 		sshdr = scache->c_shdr;
137208278a5eSRod Evans 
137308278a5eSRod Evans 		if ((sshdr->sh_type != SHT_SYMTAB) &&
137408278a5eSRod Evans 		    (sshdr->sh_type != SHT_DYNSYM)) {
137508278a5eSRod Evans 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_INVCAPINFO1),
137608278a5eSRod Evans 			    file, cicache->c_name, EC_WORD(cishdr->sh_link));
137708278a5eSRod Evans 			return (0);
137808278a5eSRod Evans 		}
137908278a5eSRod Evans 
138008278a5eSRod Evans 		if (!init_symtbl_state(&state, cache, shnum,
138108278a5eSRod Evans 		    cishdr->sh_link, ehdr, osabi, NULL, file, flags))
138208278a5eSRod Evans 			return (0);
138308278a5eSRod Evans 	}
138408278a5eSRod Evans 
138508278a5eSRod Evans 	/*
138608278a5eSRod Evans 	 * If this capabilities section contains capability string entries,
138708278a5eSRod Evans 	 * then determine the associated string table.  Capabilities entries
138808278a5eSRod Evans 	 * that define names require that the capability section indicate
138908278a5eSRod Evans 	 * which string table to use via sh_info.
139008278a5eSRod Evans 	 */
139108278a5eSRod Evans 	if (cshdr->sh_info) {
139208278a5eSRod Evans 		Shdr	*strshdr;
139308278a5eSRod Evans 
139408278a5eSRod Evans 		/*
139508278a5eSRod Evans 		 * Validate that the sh_info field points to a string table.
139608278a5eSRod Evans 		 */
139708278a5eSRod Evans 		if (cshdr->sh_info >= shnum) {
139808278a5eSRod Evans 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
139908278a5eSRod Evans 			    file, ccache->c_name, EC_WORD(cshdr->sh_info));
140008278a5eSRod Evans 			return (0);
140108278a5eSRod Evans 		}
140208278a5eSRod Evans 
140308278a5eSRod Evans 		strcache = &cache[cshdr->sh_info];
140408278a5eSRod Evans 		strshdr = strcache->c_shdr;
140508278a5eSRod Evans 
140608278a5eSRod Evans 		if (strshdr->sh_type != SHT_STRTAB) {
140708278a5eSRod Evans 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_INVCAP),
140808278a5eSRod Evans 			    file, ccache->c_name, EC_WORD(cshdr->sh_info));
140908278a5eSRod Evans 			return (0);
141008278a5eSRod Evans 		}
141108278a5eSRod Evans 		strs = (const char *)strcache->c_data->d_buf;
141208278a5eSRod Evans 		strs_size = strcache->c_data->d_size;
141308278a5eSRod Evans 	}
141408278a5eSRod Evans 
141508278a5eSRod Evans 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
141608278a5eSRod Evans 	dbg_print(0, MSG_INTL(MSG_ELF_SCN_CAP), ccache->c_name);
141708278a5eSRod Evans 
141808278a5eSRod Evans 	capnum = (Word)(cshdr->sh_size / cshdr->sh_entsize);
141908278a5eSRod Evans 
142008278a5eSRod Evans 	nulls = symcaps = 0;
142108278a5eSRod Evans 	objcap = title = 1;
142208278a5eSRod Evans 	descapndx = -1;
142308278a5eSRod Evans 
142408278a5eSRod Evans 	/*
142508278a5eSRod Evans 	 * Traverse the capabilities section printing each capability group.
142608278a5eSRod Evans 	 * The first capabilities group defines any object capabilities.  Any
142708278a5eSRod Evans 	 * following groups define symbol capabilities.  In the case where no
142808278a5eSRod Evans 	 * object capabilities exist, but symbol capabilities do, a single
142908278a5eSRod Evans 	 * CA_SUNW_NULL terminator for the object capabilities exists.
143008278a5eSRod Evans 	 */
143108278a5eSRod Evans 	for (cnum = 0; cnum < capnum; cap++, cnum++) {
143208278a5eSRod Evans 		if (cap->c_tag == CA_SUNW_NULL) {
143308278a5eSRod Evans 			/*
143408278a5eSRod Evans 			 * A CA_SUNW_NULL tag terminates a capabilities group.
143508278a5eSRod Evans 			 * If the first capabilities tag is CA_SUNW_NULL, then
143608278a5eSRod Evans 			 * no object capabilities exist.
143708278a5eSRod Evans 			 */
143808278a5eSRod Evans 			if ((nulls++ == 0) && (cnum == 0))
143908278a5eSRod Evans 				objcap = 0;
144008278a5eSRod Evans 			title = 1;
144108278a5eSRod Evans 		} else {
144208278a5eSRod Evans 			if (title) {
144308278a5eSRod Evans 				if (nulls == 0) {
144408278a5eSRod Evans 					/*
144508278a5eSRod Evans 					 * If this capabilities group represents
144608278a5eSRod Evans 					 * the object capabilities (i.e., no
144708278a5eSRod Evans 					 * CA_SUNW_NULL tag has been processed
144808278a5eSRod Evans 					 * yet), then display an object
144908278a5eSRod Evans 					 * capabilities title.
145008278a5eSRod Evans 					 */
145108278a5eSRod Evans 					dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
145208278a5eSRod Evans 					dbg_print(0,
145308278a5eSRod Evans 					    MSG_INTL(MSG_OBJ_CAP_TITLE));
145408278a5eSRod Evans 				} else {
145508278a5eSRod Evans 					/*
145608278a5eSRod Evans 					 * If this is a symbols capabilities
145708278a5eSRod Evans 					 * group (i.e., a CA_SUNW_NULL tag has
145808278a5eSRod Evans 					 * already be found that terminates
145908278a5eSRod Evans 					 * the object capabilities group), then
146008278a5eSRod Evans 					 * display a symbol capabilities title,
146108278a5eSRod Evans 					 * and retain this capabilities index
146208278a5eSRod Evans 					 * for later processing.
146308278a5eSRod Evans 					 */
146408278a5eSRod Evans 					dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
146508278a5eSRod Evans 					dbg_print(0,
146608278a5eSRod Evans 					    MSG_INTL(MSG_SYM_CAP_TITLE));
146708278a5eSRod Evans 					descapndx = cnum;
146808278a5eSRod Evans 				}
146908278a5eSRod Evans 				Elf_cap_title(0);
147008278a5eSRod Evans 				title = 0;
147108278a5eSRod Evans 			}
147208278a5eSRod Evans 
147308278a5eSRod Evans 			/*
147408278a5eSRod Evans 			 * Print the capabilities data.
147508278a5eSRod Evans 			 *
147608278a5eSRod Evans 			 * Note that CA_SUNW_PLAT, CA_SUNW_MACH and CA_SUNW_ID
147708278a5eSRod Evans 			 * entries require a string table, which should have
147808278a5eSRod Evans 			 * already been established.
147908278a5eSRod Evans 			 */
148008278a5eSRod Evans 			if ((strs == NULL) && ((cap->c_tag == CA_SUNW_PLAT) ||
148108278a5eSRod Evans 			    (cap->c_tag == CA_SUNW_MACH) ||
148208278a5eSRod Evans 			    (cap->c_tag == CA_SUNW_ID))) {
148308278a5eSRod Evans 				(void) fprintf(stderr,
148408278a5eSRod Evans 				    MSG_INTL(MSG_WARN_INVCAP4), file,
148508278a5eSRod Evans 				    EC_WORD(elf_ndxscn(ccache->c_scn)),
148608278a5eSRod Evans 				    ccache->c_name, EC_WORD(cshdr->sh_info));
148708278a5eSRod Evans 			}
148808278a5eSRod Evans 			Elf_cap_entry(0, cap, cnum, strs, strs_size,
148908278a5eSRod Evans 			    ehdr->e_machine);
149008278a5eSRod Evans 		}
149108278a5eSRod Evans 
149208278a5eSRod Evans 		/*
149308278a5eSRod Evans 		 * If this CA_SUNW_NULL tag terminates a symbol capabilities
149408278a5eSRod Evans 		 * group, determine the associated symbols.
149508278a5eSRod Evans 		 */
149608278a5eSRod Evans 		if ((cap->c_tag == CA_SUNW_NULL) && (nulls > 1) &&
149708278a5eSRod Evans 		    (descapndx != -1)) {
149808278a5eSRod Evans 			Capinfo	*cip;
149908278a5eSRod Evans 			Word	inum;
150008278a5eSRod Evans 
150108278a5eSRod Evans 			symcaps++;
150208278a5eSRod Evans 
150308278a5eSRod Evans 			/*
150408278a5eSRod Evans 			 * Make sure we've discovered a SHT_SUNW_capinfo table.
150508278a5eSRod Evans 			 */
150608278a5eSRod Evans 			if ((cip = capinfo) == NULL) {
150708278a5eSRod Evans 				(void) fprintf(stderr,
150808278a5eSRod Evans 				    MSG_INTL(MSG_ERR_INVCAP), file,
150908278a5eSRod Evans 				    ccache->c_name, EC_WORD(cshdr->sh_link));
151008278a5eSRod Evans 				return (0);
151108278a5eSRod Evans 			}
151208278a5eSRod Evans 
151308278a5eSRod Evans 			/*
151408278a5eSRod Evans 			 * Determine what symbols reference this capabilities
151508278a5eSRod Evans 			 * group.
151608278a5eSRod Evans 			 */
151708278a5eSRod Evans 			dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
151808278a5eSRod Evans 			dbg_print(0, MSG_INTL(MSG_CAPINFO_ENTRIES));
151908278a5eSRod Evans 			Elf_syms_table_title(0, ELF_DBG_ELFDUMP);
152008278a5eSRod Evans 
152108278a5eSRod Evans 			for (inum = 1, cip++; inum < capinfonum;
152208278a5eSRod Evans 			    inum++, cip++) {
152308278a5eSRod Evans 				Word	gndx = (Word)ELF_C_GROUP(*cip);
152408278a5eSRod Evans 
152508278a5eSRod Evans 				if (gndx && (gndx == descapndx)) {
152608278a5eSRod Evans 					output_symbol(&state, inum, 0,
152708278a5eSRod Evans 					    inum, state.sym + inum);
152808278a5eSRod Evans 				}
152908278a5eSRod Evans 			}
153008278a5eSRod Evans 			descapndx = -1;
153108278a5eSRod Evans 			continue;
153208278a5eSRod Evans 		}
153308278a5eSRod Evans 
153408278a5eSRod Evans 		/*
153508278a5eSRod Evans 		 * An SF1_SUNW_ADDR32 software capability tag in a 32-bit
153608278a5eSRod Evans 		 * object is suspicious as it has no effect.
153708278a5eSRod Evans 		 */
153808278a5eSRod Evans 		if ((cap->c_tag == CA_SUNW_SF_1) &&
153908278a5eSRod Evans 		    (ehdr->e_ident[EI_CLASS] == ELFCLASS32) &&
154008278a5eSRod Evans 		    (cap->c_un.c_val & SF1_SUNW_ADDR32)) {
154108278a5eSRod Evans 			(void) fprintf(stderr, MSG_INTL(MSG_WARN_INADDR32SF1),
154208278a5eSRod Evans 			    file, ccache->c_name);
154308278a5eSRod Evans 		}
154408278a5eSRod Evans 	}
154508278a5eSRod Evans 
154608278a5eSRod Evans 	/*
154708278a5eSRod Evans 	 * If this is a dynamic object, with symbol capabilities, then a
154808278a5eSRod Evans 	 * .SUNW_capchain section should exist.  This section contains a chain
154908278a5eSRod Evans 	 * of symbol indexes for each capabilities family.  This is the list
155008278a5eSRod Evans 	 * that is searched by ld.so.1 to determine the best capabilities
155108278a5eSRod Evans 	 * candidate.
155208278a5eSRod Evans 	 *
155308278a5eSRod Evans 	 * Note, more than one capabilities lead symbol can point to the same
155408278a5eSRod Evans 	 * family chain.  For example, a weak/global pair of symbols can both
155508278a5eSRod Evans 	 * represent the same family of capabilities symbols.  Therefore, to
155608278a5eSRod Evans 	 * display all possible families we traverse the capabilities
155708278a5eSRod Evans 	 * information section looking for CAPINFO_SUNW_GLOB lead symbols.
155808278a5eSRod Evans 	 * From these we determine the associated capabilities chain to inspect.
155908278a5eSRod Evans 	 */
156008278a5eSRod Evans 	if (symcaps &&
156108278a5eSRod Evans 	    ((ehdr->e_type == ET_EXEC) || (ehdr->e_type == ET_DYN))) {
156208278a5eSRod Evans 		Capinfo		*cip;
156308278a5eSRod Evans 		Capchain	*chain;
156408278a5eSRod Evans 		Cache   	*chcache;
156508278a5eSRod Evans 		Shdr		*chshdr;
156608278a5eSRod Evans 		Word		chainnum, inum;
156708278a5eSRod Evans 
156808278a5eSRod Evans 		/*
156908278a5eSRod Evans 		 * Validate that the sh_info field of the capabilities
157008278a5eSRod Evans 		 * information section points to a capabilities chain section.
157108278a5eSRod Evans 		 */
157208278a5eSRod Evans 		if (cishdr->sh_info >= shnum) {
157308278a5eSRod Evans 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
157408278a5eSRod Evans 			    file, cicache->c_name, EC_WORD(cishdr->sh_info));
157508278a5eSRod Evans 			return (0);
157608278a5eSRod Evans 		}
157708278a5eSRod Evans 
157808278a5eSRod Evans 		chcache = &cache[cishdr->sh_info];
157908278a5eSRod Evans 		chshdr = chcache->c_shdr;
158008278a5eSRod Evans 
158108278a5eSRod Evans 		if (chshdr->sh_type != SHT_SUNW_capchain) {
158208278a5eSRod Evans 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_INVCAPINFO2),
158308278a5eSRod Evans 			    file, cicache->c_name, EC_WORD(cishdr->sh_info));
158408278a5eSRod Evans 			return (0);
158508278a5eSRod Evans 		}
158608278a5eSRod Evans 
158708278a5eSRod Evans 		chainnum = (Word)(chshdr->sh_size / chshdr->sh_entsize);
158808278a5eSRod Evans 		chain = (Capchain *)chcache->c_data->d_buf;
158908278a5eSRod Evans 
159008278a5eSRod Evans 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
159108278a5eSRod Evans 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_CAPCHAIN), chcache->c_name);
159208278a5eSRod Evans 
159308278a5eSRod Evans 		/*
159408278a5eSRod Evans 		 * Traverse the capabilities information section looking for
159508278a5eSRod Evans 		 * CAPINFO_SUNW_GLOB lead capabilities symbols.
159608278a5eSRod Evans 		 */
159708278a5eSRod Evans 		cip = capinfo;
159808278a5eSRod Evans 		for (inum = 1, cip++; inum < capinfonum; inum++, cip++) {
159908278a5eSRod Evans 			const char	*name;
160008278a5eSRod Evans 			Sym		*sym;
160108278a5eSRod Evans 			Word		sndx, cndx;
160208278a5eSRod Evans 			Word		gndx = (Word)ELF_C_GROUP(*cip);
160308278a5eSRod Evans 
160408278a5eSRod Evans 			if ((gndx == 0) || (gndx != CAPINFO_SUNW_GLOB))
160508278a5eSRod Evans 				continue;
160608278a5eSRod Evans 
160708278a5eSRod Evans 			/*
160808278a5eSRod Evans 			 * Determine the symbol that is associated with this
160908278a5eSRod Evans 			 * capability information entry, and use this to
161008278a5eSRod Evans 			 * identify this capability family.
161108278a5eSRod Evans 			 */
161208278a5eSRod Evans 			sym = (Sym *)(state.sym + inum);
161308278a5eSRod Evans 			name = string(cicache, inum, strcache, file,
161408278a5eSRod Evans 			    sym->st_name);
161508278a5eSRod Evans 
161608278a5eSRod Evans 			dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
161708278a5eSRod Evans 			dbg_print(0, MSG_INTL(MSG_CAPCHAIN_TITLE), name);
161808278a5eSRod Evans 			dbg_print(0, MSG_INTL(MSG_CAPCHAIN_ENTRY));
161908278a5eSRod Evans 
162008278a5eSRod Evans 			cndx = (Word)ELF_C_SYM(*cip);
162108278a5eSRod Evans 
162208278a5eSRod Evans 			/*
162308278a5eSRod Evans 			 * Traverse this families chain and identify each
162408278a5eSRod Evans 			 * family member.
162508278a5eSRod Evans 			 */
162608278a5eSRod Evans 			for (;;) {
162708278a5eSRod Evans 				char	_chain[MAXNDXSIZE], _symndx[MAXNDXSIZE];
162808278a5eSRod Evans 
162908278a5eSRod Evans 				if (cndx >= chainnum) {
163008278a5eSRod Evans 					(void) fprintf(stderr,
163108278a5eSRod Evans 					    MSG_INTL(MSG_ERR_INVCAPINFO3), file,
163208278a5eSRod Evans 					    cicache->c_name, EC_WORD(inum),
163308278a5eSRod Evans 					    EC_WORD(cndx));
163408278a5eSRod Evans 					break;
163508278a5eSRod Evans 				}
163608278a5eSRod Evans 				if ((sndx = chain[cndx]) == 0)
163708278a5eSRod Evans 					break;
163808278a5eSRod Evans 
163908278a5eSRod Evans 				/*
164008278a5eSRod Evans 				 * Determine this entries symbol reference.
164108278a5eSRod Evans 				 */
164208278a5eSRod Evans 				if (sndx > state.symn) {
164308278a5eSRod Evans 					(void) fprintf(stderr,
164408278a5eSRod Evans 					    MSG_INTL(MSG_ERR_CHBADSYMNDX), file,
164508278a5eSRod Evans 					    EC_WORD(sndx), chcache->c_name,
164608278a5eSRod Evans 					    EC_WORD(cndx));
164708278a5eSRod Evans 					name = MSG_INTL(MSG_STR_UNKNOWN);
164808278a5eSRod Evans 				} else {
164908278a5eSRod Evans 					sym = (Sym *)(state.sym + sndx);
165008278a5eSRod Evans 					name = string(chcache, sndx,
165108278a5eSRod Evans 					    strcache, file, sym->st_name);
165208278a5eSRod Evans 				}
165308278a5eSRod Evans 
165408278a5eSRod Evans 				/*
165508278a5eSRod Evans 				 * Display the family member.
165608278a5eSRod Evans 				 */
165708278a5eSRod Evans 				(void) snprintf(_chain, MAXNDXSIZE,
165808278a5eSRod Evans 				    MSG_ORIG(MSG_FMT_INTEGER), cndx);
165908278a5eSRod Evans 				(void) snprintf(_symndx, MAXNDXSIZE,
166008278a5eSRod Evans 				    MSG_ORIG(MSG_FMT_INDEX2), EC_WORD(sndx));
166108278a5eSRod Evans 				dbg_print(0, MSG_ORIG(MSG_FMT_CHAIN_INFO),
166208278a5eSRod Evans 				    _chain, _symndx, demangle(name, flags));
166308278a5eSRod Evans 
166408278a5eSRod Evans 				cndx++;
166508278a5eSRod Evans 			}
166608278a5eSRod Evans 		}
166708278a5eSRod Evans 	}
166808278a5eSRod Evans 	return (objcap);
166908278a5eSRod Evans }
167008278a5eSRod Evans 
167108278a5eSRod Evans /*
167208278a5eSRod Evans  * Print the capabilities.
167308278a5eSRod Evans  *
167408278a5eSRod Evans  * A .SUNW_cap section can contain one or more, CA_SUNW_NULL terminated,
167508278a5eSRod Evans  * capabilities groups.  The first group defines the object capabilities.
167608278a5eSRod Evans  * This group defines the minimum capability requirements of the entire
167708278a5eSRod Evans  * object file.  If this is a dynamic object, this group should be associated
167808278a5eSRod Evans  * with a PT_SUNWCAP program header.
167908278a5eSRod Evans  *
168008278a5eSRod Evans  * Additional capabilities groups define the association of individual symbols
168108278a5eSRod Evans  * to specific capabilities.
16827c478bd9Sstevel@tonic-gate  */
16837c478bd9Sstevel@tonic-gate static void
cap(const char * file,Cache * cache,Word shnum,Word phnum,Ehdr * ehdr,uchar_t osabi,Elf * elf,uint_t flags)16845aefb655Srie cap(const char *file, Cache *cache, Word shnum, Word phnum, Ehdr *ehdr,
168508278a5eSRod Evans     uchar_t osabi, Elf *elf, uint_t flags)
16867c478bd9Sstevel@tonic-gate {
16875aefb655Srie 	Word		cnt;
1688bebb829dSRod Evans 	Shdr		*cshdr = NULL;
16897c478bd9Sstevel@tonic-gate 	Cache		*ccache;
16905aefb655Srie 	Off		cphdr_off = 0;
16915aefb655Srie 	Xword		cphdr_sz;
16927c478bd9Sstevel@tonic-gate 
16937c478bd9Sstevel@tonic-gate 	/*
169408278a5eSRod Evans 	 * Determine if a global capabilities header exists.
16957c478bd9Sstevel@tonic-gate 	 */
16965aefb655Srie 	if (phnum) {
16975aefb655Srie 		Phdr	*phdr;
16987c478bd9Sstevel@tonic-gate 
16995aefb655Srie 		if ((phdr = elf_getphdr(elf)) == NULL) {
17007c478bd9Sstevel@tonic-gate 			failure(file, MSG_ORIG(MSG_ELF_GETPHDR));
17017c478bd9Sstevel@tonic-gate 			return;
17027c478bd9Sstevel@tonic-gate 		}
17037c478bd9Sstevel@tonic-gate 
17045aefb655Srie 		for (cnt = 0; cnt < phnum; phdr++, cnt++) {
17055aefb655Srie 			if (phdr->p_type == PT_SUNWCAP) {
17065aefb655Srie 				cphdr_off = phdr->p_offset;
17075aefb655Srie 				cphdr_sz = phdr->p_filesz;
17087c478bd9Sstevel@tonic-gate 				break;
17097c478bd9Sstevel@tonic-gate 			}
17107c478bd9Sstevel@tonic-gate 		}
17115aefb655Srie 	}
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate 	/*
171408278a5eSRod Evans 	 * Determine if a capabilities section exists.
17157c478bd9Sstevel@tonic-gate 	 */
17167c478bd9Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
17175aefb655Srie 		Cache	*_cache = &cache[cnt];
17185aefb655Srie 		Shdr	*shdr = _cache->c_shdr;
17197c478bd9Sstevel@tonic-gate 
172008278a5eSRod Evans 		/*
172108278a5eSRod Evans 		 * Process any capabilities information.
172208278a5eSRod Evans 		 */
172308278a5eSRod Evans 		if (shdr->sh_type == SHT_SUNW_cap) {
172408278a5eSRod Evans 			if (cap_section(file, cache, shnum, _cache, osabi,
172508278a5eSRod Evans 			    ehdr, flags)) {
172608278a5eSRod Evans 				/*
172708278a5eSRod Evans 				 * If this section defined an object capability
172808278a5eSRod Evans 				 * group, retain the section information for
172908278a5eSRod Evans 				 * program header validation.
173008278a5eSRod Evans 				 */
17317c478bd9Sstevel@tonic-gate 				ccache = _cache;
17327c478bd9Sstevel@tonic-gate 				cshdr = shdr;
173308278a5eSRod Evans 			}
173408278a5eSRod Evans 			continue;
173508278a5eSRod Evans 		}
17367c478bd9Sstevel@tonic-gate 	}
17377c478bd9Sstevel@tonic-gate 
1738bebb829dSRod Evans 	if ((cshdr == NULL) && (cphdr_off == 0))
17397c478bd9Sstevel@tonic-gate 		return;
17407c478bd9Sstevel@tonic-gate 
174108278a5eSRod Evans 	if (cphdr_off && (cshdr == NULL))
17427c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_WARN_INVCAP1), file);
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 	/*
174508278a5eSRod Evans 	 * If this object is an executable or shared object, and it provided
174608278a5eSRod Evans 	 * an object capabilities group, then the group should have an
174708278a5eSRod Evans 	 * accompanying PT_SUNWCAP program header.
17487c478bd9Sstevel@tonic-gate 	 */
17497c478bd9Sstevel@tonic-gate 	if (cshdr && ((ehdr->e_type == ET_EXEC) || (ehdr->e_type == ET_DYN))) {
175008278a5eSRod Evans 		if (cphdr_off == 0) {
17517c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_WARN_INVCAP2),
175208278a5eSRod Evans 			    file, EC_WORD(elf_ndxscn(ccache->c_scn)),
175308278a5eSRod Evans 			    ccache->c_name);
175408278a5eSRod Evans 		} else if ((cphdr_off != cshdr->sh_offset) ||
175508278a5eSRod Evans 		    (cphdr_sz != cshdr->sh_size)) {
17567c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_WARN_INVCAP3),
175708278a5eSRod Evans 			    file, EC_WORD(elf_ndxscn(ccache->c_scn)),
175808278a5eSRod Evans 			    ccache->c_name);
175908278a5eSRod Evans 		}
17607c478bd9Sstevel@tonic-gate 	}
17617c478bd9Sstevel@tonic-gate }
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate /*
17647c478bd9Sstevel@tonic-gate  * Print the interpretor.
17657c478bd9Sstevel@tonic-gate  */
17667c478bd9Sstevel@tonic-gate static void
interp(const char * file,Cache * cache,Word shnum,Word phnum,Elf * elf)17675aefb655Srie interp(const char *file, Cache *cache, Word shnum, Word phnum, Elf *elf)
17687c478bd9Sstevel@tonic-gate {
17697e16fca0SAli Bahrami 	static Word phdr_types[] = { PT_INTERP };
17707e16fca0SAli Bahrami 
17717e16fca0SAli Bahrami 
17725aefb655Srie 	Word	cnt;
17737e16fca0SAli Bahrami 	Shdr	*ishdr = NULL;
17747c478bd9Sstevel@tonic-gate 	Cache	*icache;
17755aefb655Srie 	Off	iphdr_off = 0;
17765aefb655Srie 	Xword	iphdr_fsz;
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 	/*
17797c478bd9Sstevel@tonic-gate 	 * Determine if an interp header exists.
17807c478bd9Sstevel@tonic-gate 	 */
17815aefb655Srie 	if (phnum) {
17825aefb655Srie 		Phdr	*phdr;
17837c478bd9Sstevel@tonic-gate 
17847e16fca0SAli Bahrami 		phdr = getphdr(phnum, phdr_types,
17857e16fca0SAli Bahrami 		    sizeof (phdr_types) / sizeof (*phdr_types), file, elf);
17867e16fca0SAli Bahrami 		if (phdr != NULL) {
17875aefb655Srie 			iphdr_off = phdr->p_offset;
17885aefb655Srie 			iphdr_fsz = phdr->p_filesz;
17897c478bd9Sstevel@tonic-gate 		}
17907c478bd9Sstevel@tonic-gate 	}
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate 	if (iphdr_off == 0)
17937c478bd9Sstevel@tonic-gate 		return;
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate 	/*
17967c478bd9Sstevel@tonic-gate 	 * Determine if an interp section exists.
17977c478bd9Sstevel@tonic-gate 	 */
17987c478bd9Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
17995aefb655Srie 		Cache	*_cache = &cache[cnt];
18005aefb655Srie 		Shdr	*shdr = _cache->c_shdr;
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate 		/*
18037c478bd9Sstevel@tonic-gate 		 * Scan sections to find a section which contains the PT_INTERP
18047c478bd9Sstevel@tonic-gate 		 * string.  The target section can't be in a NOBITS section.
18057c478bd9Sstevel@tonic-gate 		 */
18067c478bd9Sstevel@tonic-gate 		if ((shdr->sh_type == SHT_NOBITS) ||
18077c478bd9Sstevel@tonic-gate 		    (iphdr_off < shdr->sh_offset) ||
18085aefb655Srie 		    (iphdr_off + iphdr_fsz) > (shdr->sh_offset + shdr->sh_size))
18097c478bd9Sstevel@tonic-gate 			continue;
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate 		icache = _cache;
18127c478bd9Sstevel@tonic-gate 		ishdr = shdr;
18137c478bd9Sstevel@tonic-gate 		break;
18147c478bd9Sstevel@tonic-gate 	}
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate 	/*
18177c478bd9Sstevel@tonic-gate 	 * Print the interpreter string based on the offset defined in the
18187c478bd9Sstevel@tonic-gate 	 * program header, as this is the offset used by the kernel.
18197c478bd9Sstevel@tonic-gate 	 */
18209a411307Srie 	if (ishdr && icache->c_data) {
18215aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
18225aefb655Srie 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_INTERP), icache->c_name);
18235aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_FMT_INDENT),
18247c478bd9Sstevel@tonic-gate 		    (char *)icache->c_data->d_buf +
18257c478bd9Sstevel@tonic-gate 		    (iphdr_off - ishdr->sh_offset));
18267c478bd9Sstevel@tonic-gate 	} else
18277c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_WARN_INVINTERP1), file);
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate 	/*
18307c478bd9Sstevel@tonic-gate 	 * If there are any inconsistences between the program header and
18317c478bd9Sstevel@tonic-gate 	 * section information, flag them.
18327c478bd9Sstevel@tonic-gate 	 */
18337c478bd9Sstevel@tonic-gate 	if (ishdr && ((iphdr_off != ishdr->sh_offset) ||
18345aefb655Srie 	    (iphdr_fsz != ishdr->sh_size))) {
18357c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_WARN_INVINTERP2), file,
18367c478bd9Sstevel@tonic-gate 		    icache->c_name);
18377c478bd9Sstevel@tonic-gate 	}
18387c478bd9Sstevel@tonic-gate }
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate /*
18417c478bd9Sstevel@tonic-gate  * Print the syminfo section.
18427c478bd9Sstevel@tonic-gate  */
18437c478bd9Sstevel@tonic-gate static void
syminfo(Cache * cache,Word shnum,Ehdr * ehdr,uchar_t osabi,const char * file)1844d444b03eSAli Bahrami syminfo(Cache *cache, Word shnum, Ehdr *ehdr, uchar_t osabi, const char *file)
18457c478bd9Sstevel@tonic-gate {
18465aefb655Srie 	Shdr		*infoshdr;
18475aefb655Srie 	Syminfo		*info;
18485aefb655Srie 	Sym		*syms;
18495aefb655Srie 	Dyn		*dyns;
1850d444b03eSAli Bahrami 	Word		infonum, cnt, ndx, symnum, dynnum;
1851d444b03eSAli Bahrami 	Cache		*infocache = NULL, *dyncache = NULL, *symsec, *strsec;
1852d444b03eSAli Bahrami 	Boolean		*dynerr;
18537c478bd9Sstevel@tonic-gate 
18547c478bd9Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
18555aefb655Srie 		if (cache[cnt].c_shdr->sh_type == SHT_SUNW_syminfo) {
18565aefb655Srie 			infocache = &cache[cnt];
18577c478bd9Sstevel@tonic-gate 			break;
18587c478bd9Sstevel@tonic-gate 		}
18597c478bd9Sstevel@tonic-gate 	}
18607e16fca0SAli Bahrami 	if (infocache == NULL)
18617c478bd9Sstevel@tonic-gate 		return;
18627c478bd9Sstevel@tonic-gate 
18635aefb655Srie 	infoshdr = infocache->c_shdr;
18645aefb655Srie 	if ((infoshdr->sh_entsize == 0) || (infoshdr->sh_size == 0)) {
18657c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
18665aefb655Srie 		    file, infocache->c_name);
18677c478bd9Sstevel@tonic-gate 		return;
18687c478bd9Sstevel@tonic-gate 	}
18699a411307Srie 	if (infocache->c_data == NULL)
18709a411307Srie 		return;
18719a411307Srie 
18725aefb655Srie 	infonum = (Word)(infoshdr->sh_size / infoshdr->sh_entsize);
18735aefb655Srie 	info = (Syminfo *)infocache->c_data->d_buf;
18747c478bd9Sstevel@tonic-gate 
18757c478bd9Sstevel@tonic-gate 	/*
1876d444b03eSAli Bahrami 	 * If there is no associated dynamic section, determine if one
1877d444b03eSAli Bahrami 	 * is needed, and if so issue a warning. If there is an
1878d444b03eSAli Bahrami 	 * associated dynamic section, validate it and get the data buffer
1879d444b03eSAli Bahrami 	 * for it.
18807c478bd9Sstevel@tonic-gate 	 */
1881d444b03eSAli Bahrami 	dyns = NULL;
1882d444b03eSAli Bahrami 	dynnum = 0;
1883d444b03eSAli Bahrami 	if (infoshdr->sh_info == 0) {
1884d444b03eSAli Bahrami 		Syminfo	*_info = info + 1;
1885d444b03eSAli Bahrami 
1886d444b03eSAli Bahrami 		for (ndx = 1; ndx < infonum; ndx++, _info++) {
1887d444b03eSAli Bahrami 			if ((_info->si_flags == 0) && (_info->si_boundto == 0))
1888d444b03eSAli Bahrami 				continue;
1889d444b03eSAli Bahrami 
1890d444b03eSAli Bahrami 			if (_info->si_boundto < SYMINFO_BT_LOWRESERVE)
1891d444b03eSAli Bahrami 				(void) fprintf(stderr,
1892d444b03eSAli Bahrami 				    MSG_INTL(MSG_ERR_BADSHINFO), file,
1893d444b03eSAli Bahrami 				    infocache->c_name,
1894d444b03eSAli Bahrami 				    EC_WORD(infoshdr->sh_info));
1895d444b03eSAli Bahrami 		}
1896d444b03eSAli Bahrami 	} else if ((infoshdr->sh_info >= shnum) ||
1897d444b03eSAli Bahrami 	    (cache[infoshdr->sh_info].c_shdr->sh_type != SHT_DYNAMIC)) {
18987c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHINFO),
18995aefb655Srie 		    file, infocache->c_name, EC_WORD(infoshdr->sh_info));
1900d444b03eSAli Bahrami 	} else {
1901d444b03eSAli Bahrami 		dyncache = &cache[infoshdr->sh_info];
1902d444b03eSAli Bahrami 		if ((dyncache->c_data == NULL) ||
1903d444b03eSAli Bahrami 		    ((dyns = dyncache->c_data->d_buf) == NULL)) {
1904d444b03eSAli Bahrami 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
1905d444b03eSAli Bahrami 			    file, dyncache->c_name);
1906d444b03eSAli Bahrami 		}
1907d444b03eSAli Bahrami 		if (dyns != NULL) {
1908d444b03eSAli Bahrami 			dynnum = dyncache->c_shdr->sh_size /
1909d444b03eSAli Bahrami 			    dyncache->c_shdr->sh_entsize;
1910d444b03eSAli Bahrami 
1911d444b03eSAli Bahrami 			/*
1912d444b03eSAli Bahrami 			 * We validate the type of dynamic elements referenced
1913d444b03eSAli Bahrami 			 * from the syminfo. This array is used report any
1914d444b03eSAli Bahrami 			 * bad dynamic entries.
1915d444b03eSAli Bahrami 			 */
1916d444b03eSAli Bahrami 			if ((dynerr = calloc(dynnum, sizeof (*dynerr))) ==
1917d444b03eSAli Bahrami 			    NULL) {
1918d444b03eSAli Bahrami 				int err = errno;
1919d444b03eSAli Bahrami 				(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
1920d444b03eSAli Bahrami 				    file, strerror(err));
19217c478bd9Sstevel@tonic-gate 				return;
19227c478bd9Sstevel@tonic-gate 			}
1923d444b03eSAli Bahrami 		}
19247c478bd9Sstevel@tonic-gate 	}
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate 	/*
19275aefb655Srie 	 * Get the data buffer for the associated symbol table and string table.
19287c478bd9Sstevel@tonic-gate 	 */
19295aefb655Srie 	if (stringtbl(cache, 1, cnt, shnum, file,
19305aefb655Srie 	    &symnum, &symsec, &strsec) == 0)
19317c478bd9Sstevel@tonic-gate 		return;
19327c478bd9Sstevel@tonic-gate 
19335aefb655Srie 	syms = symsec->c_data->d_buf;
19345aefb655Srie 
19357c478bd9Sstevel@tonic-gate 	/*
19365aefb655Srie 	 * Loop through the syminfo entries.
19377c478bd9Sstevel@tonic-gate 	 */
19385aefb655Srie 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
19395aefb655Srie 	dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMINFO), infocache->c_name);
19405aefb655Srie 	Elf_syminfo_title(0);
19417c478bd9Sstevel@tonic-gate 
19425aefb655Srie 	for (ndx = 1, info++; ndx < infonum; ndx++, info++) {
19435aefb655Srie 		Sym 		*sym;
1944d444b03eSAli Bahrami 		const char	*needed, *name;
1945d444b03eSAli Bahrami 		Word		expect_dt;
1946d444b03eSAli Bahrami 		Word		boundto = info->si_boundto;
19477c478bd9Sstevel@tonic-gate 
1948d444b03eSAli Bahrami 		if ((info->si_flags == 0) && (boundto == 0))
19497c478bd9Sstevel@tonic-gate 			continue;
19507c478bd9Sstevel@tonic-gate 
19515aefb655Srie 		sym = &syms[ndx];
19525aefb655Srie 		name = string(infocache, ndx, strsec, file, sym->st_name);
19537c478bd9Sstevel@tonic-gate 
1954d444b03eSAli Bahrami 		/* Is si_boundto set to one of the reserved values? */
1955d444b03eSAli Bahrami 		if (boundto >= SYMINFO_BT_LOWRESERVE) {
1956d444b03eSAli Bahrami 			Elf_syminfo_entry(0, ndx, info, name, NULL);
1957d444b03eSAli Bahrami 			continue;
1958d444b03eSAli Bahrami 		}
19597c478bd9Sstevel@tonic-gate 
1960d444b03eSAli Bahrami 		/*
1961d444b03eSAli Bahrami 		 * si_boundto is referencing a dynamic section. If we don't
1962d444b03eSAli Bahrami 		 * have one, an error was already issued above, so it suffices
1963d444b03eSAli Bahrami 		 * to display an empty string. If we are out of bounds, then
1964d444b03eSAli Bahrami 		 * report that and then display an empty string.
1965d444b03eSAli Bahrami 		 */
1966d444b03eSAli Bahrami 		if ((dyns == NULL) || (boundto >= dynnum)) {
1967d444b03eSAli Bahrami 			if (dyns != NULL)
1968d444b03eSAli Bahrami 				(void) fprintf(stderr,
1969d444b03eSAli Bahrami 				    MSG_INTL(MSG_ERR_BADSIDYNNDX), file,
1970d444b03eSAli Bahrami 				    infocache->c_ndx, infocache->c_name,
1971d444b03eSAli Bahrami 				    EC_WORD(ndx), EC_WORD(dynnum - 1),
1972d444b03eSAli Bahrami 				    EC_WORD(boundto));
1973d444b03eSAli Bahrami 			Elf_syminfo_entry(0, ndx, info, name,
1974d444b03eSAli Bahrami 			    MSG_ORIG(MSG_STR_EMPTY));
1975d444b03eSAli Bahrami 			continue;
1976d444b03eSAli Bahrami 		}
1977d444b03eSAli Bahrami 
1978d444b03eSAli Bahrami 		/*
1979d444b03eSAli Bahrami 		 * The si_boundto reference expects a specific dynamic element
1980d444b03eSAli Bahrami 		 * type at the given index. The dynamic element is always a
1981d444b03eSAli Bahrami 		 * string that gives an object name. The specific type depends
1982d444b03eSAli Bahrami 		 * on the si_flags present. Ensure that we've got the right
1983d444b03eSAli Bahrami 		 * type.
1984d444b03eSAli Bahrami 		 */
1985d444b03eSAli Bahrami 		if (info->si_flags & SYMINFO_FLG_FILTER)
1986d444b03eSAli Bahrami 			expect_dt = DT_SUNW_FILTER;
1987d444b03eSAli Bahrami 		else if (info->si_flags & SYMINFO_FLG_AUXILIARY)
1988d444b03eSAli Bahrami 			expect_dt = DT_SUNW_AUXILIARY;
1989d444b03eSAli Bahrami 		else if (info->si_flags & (SYMINFO_FLG_DIRECT |
1990d444b03eSAli Bahrami 		    SYMINFO_FLG_LAZYLOAD | SYMINFO_FLG_DIRECTBIND))
1991d444b03eSAli Bahrami 			expect_dt = DT_NEEDED;
1992d444b03eSAli Bahrami 		else
1993d444b03eSAli Bahrami 			expect_dt = DT_NULL;   /* means we ignore the type */
1994d444b03eSAli Bahrami 
1995d444b03eSAli Bahrami 		if ((dyns[boundto].d_tag != expect_dt) &&
1996d444b03eSAli Bahrami 		    (expect_dt != DT_NULL)) {
1997d444b03eSAli Bahrami 			Conv_inv_buf_t	buf1, buf2;
1998d444b03eSAli Bahrami 
1999d444b03eSAli Bahrami 			/* Only complain about each dynamic element once */
2000d444b03eSAli Bahrami 			if (!dynerr[boundto]) {
2001d444b03eSAli Bahrami 				(void) fprintf(stderr,
2002d444b03eSAli Bahrami 				    MSG_INTL(MSG_ERR_BADSIDYNTAG),
2003d444b03eSAli Bahrami 				    file, infocache->c_ndx, infocache->c_name,
2004d444b03eSAli Bahrami 				    EC_WORD(ndx), dyncache->c_ndx,
2005d444b03eSAli Bahrami 				    dyncache->c_name, EC_WORD(boundto),
2006d444b03eSAli Bahrami 				    conv_dyn_tag(expect_dt, osabi,
2007d444b03eSAli Bahrami 				    ehdr->e_machine, CONV_FMT_ALT_CF, &buf1),
2008d444b03eSAli Bahrami 				    conv_dyn_tag(dyns[boundto].d_tag, osabi,
2009d444b03eSAli Bahrami 				    ehdr->e_machine, CONV_FMT_ALT_CF, &buf2));
2010d444b03eSAli Bahrami 				dynerr[boundto] = TRUE;
2011d444b03eSAli Bahrami 			}
2012d444b03eSAli Bahrami 		}
2013d444b03eSAli Bahrami 
2014d444b03eSAli Bahrami 		/*
2015d444b03eSAli Bahrami 		 * Whether or not the DT item we're pointing at is
2016d444b03eSAli Bahrami 		 * of the right type, if it's a type we recognize as
2017d444b03eSAli Bahrami 		 * providing a string, go ahead and show it. Otherwise
2018d444b03eSAli Bahrami 		 * an empty string.
2019d444b03eSAli Bahrami 		 */
2020d444b03eSAli Bahrami 		switch (dyns[boundto].d_tag) {
2021d444b03eSAli Bahrami 		case DT_NEEDED:
2022d444b03eSAli Bahrami 		case DT_SONAME:
2023d444b03eSAli Bahrami 		case DT_RPATH:
2024d444b03eSAli Bahrami 		case DT_RUNPATH:
2025d444b03eSAli Bahrami 		case DT_CONFIG:
2026d444b03eSAli Bahrami 		case DT_DEPAUDIT:
2027d444b03eSAli Bahrami 		case DT_USED:
2028d444b03eSAli Bahrami 		case DT_AUDIT:
2029d444b03eSAli Bahrami 		case DT_SUNW_AUXILIARY:
2030d444b03eSAli Bahrami 		case DT_SUNW_FILTER:
2031d444b03eSAli Bahrami 		case DT_FILTER:
2032d444b03eSAli Bahrami 		case DT_AUXILIARY:
2033d444b03eSAli Bahrami 			needed = string(infocache, boundto,
2034d444b03eSAli Bahrami 			    strsec, file, dyns[boundto].d_un.d_val);
2035d444b03eSAli Bahrami 			break;
2036d444b03eSAli Bahrami 		default:
2037d444b03eSAli Bahrami 			needed = MSG_ORIG(MSG_STR_EMPTY);
20387c478bd9Sstevel@tonic-gate 		}
20395aefb655Srie 		Elf_syminfo_entry(0, ndx, info, name, needed);
20407c478bd9Sstevel@tonic-gate 	}
2041d444b03eSAli Bahrami 	if (dyns != NULL)
2042d444b03eSAli Bahrami 		free(dynerr);
20437c478bd9Sstevel@tonic-gate }
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate /*
20467c478bd9Sstevel@tonic-gate  * Print version definition section entries.
20477c478bd9Sstevel@tonic-gate  */
20487c478bd9Sstevel@tonic-gate static void
version_def(Verdef * vdf,Word vdf_num,Cache * vcache,Cache * scache,const char * file)2049d840867fSab196087 version_def(Verdef *vdf, Word vdf_num, Cache *vcache, Cache *scache,
20507c478bd9Sstevel@tonic-gate     const char *file)
20517c478bd9Sstevel@tonic-gate {
20525aefb655Srie 	Word	cnt;
20537c478bd9Sstevel@tonic-gate 	char	index[MAXNDXSIZE];
20547c478bd9Sstevel@tonic-gate 
20555aefb655Srie 	Elf_ver_def_title(0);
20567c478bd9Sstevel@tonic-gate 
2057d840867fSab196087 	for (cnt = 1; cnt <= vdf_num; cnt++,
20585aefb655Srie 	    vdf = (Verdef *)((uintptr_t)vdf + vdf->vd_next)) {
2059090a8d9eSAli Bahrami 		Conv_ver_flags_buf_t	ver_flags_buf;
20607c478bd9Sstevel@tonic-gate 		const char		*name, *dep;
20615aefb655Srie 		Half			vcnt = vdf->vd_cnt - 1;
20625aefb655Srie 		Half			ndx = vdf->vd_ndx;
206333eb6ee1Sab196087 		Verdaux	*vdap = (Verdaux *)((uintptr_t)vdf + vdf->vd_aux);
20647c478bd9Sstevel@tonic-gate 
20657c478bd9Sstevel@tonic-gate 		/*
20667c478bd9Sstevel@tonic-gate 		 * Obtain the name and first dependency (if any).
20677c478bd9Sstevel@tonic-gate 		 */
20687c478bd9Sstevel@tonic-gate 		name = string(vcache, cnt, scache, file, vdap->vda_name);
20695aefb655Srie 		vdap = (Verdaux *)((uintptr_t)vdap + vdap->vda_next);
20707c478bd9Sstevel@tonic-gate 		if (vcnt)
20717c478bd9Sstevel@tonic-gate 			dep = string(vcache, cnt, scache, file, vdap->vda_name);
20727c478bd9Sstevel@tonic-gate 		else
20737c478bd9Sstevel@tonic-gate 			dep = MSG_ORIG(MSG_STR_EMPTY);
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate 		(void) snprintf(index, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INDEX),
20767c478bd9Sstevel@tonic-gate 		    EC_XWORD(ndx));
20775aefb655Srie 		Elf_ver_line_1(0, index, name, dep,
2078090a8d9eSAli Bahrami 		    conv_ver_flags(vdf->vd_flags, 0, &ver_flags_buf));
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate 		/*
20817c478bd9Sstevel@tonic-gate 		 * Print any additional dependencies.
20827c478bd9Sstevel@tonic-gate 		 */
20837c478bd9Sstevel@tonic-gate 		if (vcnt) {
20845aefb655Srie 			vdap = (Verdaux *)((uintptr_t)vdap + vdap->vda_next);
20857c478bd9Sstevel@tonic-gate 			for (vcnt--; vcnt; vcnt--,
20865aefb655Srie 			    vdap = (Verdaux *)((uintptr_t)vdap +
20877c478bd9Sstevel@tonic-gate 			    vdap->vda_next)) {
20887c478bd9Sstevel@tonic-gate 				dep = string(vcache, cnt, scache, file,
20897c478bd9Sstevel@tonic-gate 				    vdap->vda_name);
20905aefb655Srie 				Elf_ver_line_2(0, MSG_ORIG(MSG_STR_EMPTY), dep);
20917c478bd9Sstevel@tonic-gate 			}
20927c478bd9Sstevel@tonic-gate 		}
20937c478bd9Sstevel@tonic-gate 	}
20947c478bd9Sstevel@tonic-gate }
20957c478bd9Sstevel@tonic-gate 
20967c478bd9Sstevel@tonic-gate /*
2097d840867fSab196087  * Print version needed section entries.
2098d840867fSab196087  *
2099d840867fSab196087  * entry:
2100d840867fSab196087  *	vnd - Address of verneed data
2101d840867fSab196087  *	vnd_num - # of Verneed entries
2102d840867fSab196087  *	vcache - Cache of verneed section being processed
2103d840867fSab196087  *	scache - Cache of associated string table section
2104d840867fSab196087  *	file - Name of object being processed.
2105d840867fSab196087  *	versym - Information about versym section
2106d840867fSab196087  *
2107d840867fSab196087  * exit:
2108d840867fSab196087  *	The versions have been printed. If GNU style versioning
2109d840867fSab196087  *	is in effect, versym->max_verndx has been updated to
2110d840867fSab196087  *	contain the largest version index seen.
2111090a8d9eSAli Bahrami  *
2112090a8d9eSAli Bahrami  * note:
2113090a8d9eSAli Bahrami  * 	The versym section of an object that follows the original
2114090a8d9eSAli Bahrami  *	Solaris versioning rules only contains indexes into the verdef
2115090a8d9eSAli Bahrami  *	section. Symbols defined in other objects (UNDEF) are given
2116090a8d9eSAli Bahrami  *	a version of 0, indicating that they are not defined by
2117090a8d9eSAli Bahrami  *	this file, and the Verneed entries do not have associated version
2118090a8d9eSAli Bahrami  *	indexes. For these reasons, we do not display a version index
2119090a8d9eSAli Bahrami  *	for original-style Verneed sections.
2120090a8d9eSAli Bahrami  *
2121090a8d9eSAli Bahrami  *	The GNU versioning extensions alter this: Symbols defined in other
2122090a8d9eSAli Bahrami  *	objects receive a version index in the range above those defined
2123090a8d9eSAli Bahrami  *	by the Verdef section, and the vna_other field of the Vernaux
2124090a8d9eSAli Bahrami  *	structs inside the Verneed section contain the version index for
2125090a8d9eSAli Bahrami  *	that item. We therefore  display the index when showing the
2126090a8d9eSAli Bahrami  *	contents of a GNU style Verneed section. You should not
2127090a8d9eSAli Bahrami  *	necessarily expect these indexes to appear in sorted
2128090a8d9eSAli Bahrami  *	order --- it seems that the GNU ld assigns the versions as
2129090a8d9eSAli Bahrami  *	symbols are encountered during linking, and then the results
2130090a8d9eSAli Bahrami  *	are assembled into the Verneed section afterwards.
21317c478bd9Sstevel@tonic-gate  */
21327c478bd9Sstevel@tonic-gate static void
version_need(Verneed * vnd,Word vnd_num,Cache * vcache,Cache * scache,const char * file,VERSYM_STATE * versym)2133d840867fSab196087 version_need(Verneed *vnd, Word vnd_num, Cache *vcache, Cache *scache,
2134d840867fSab196087     const char *file, VERSYM_STATE *versym)
21357c478bd9Sstevel@tonic-gate {
21365aefb655Srie 	Word		cnt;
2137d840867fSab196087 	char		index[MAXNDXSIZE];
2138d840867fSab196087 	const char	*index_str;
21397c478bd9Sstevel@tonic-gate 
2140090a8d9eSAli Bahrami 	Elf_ver_need_title(0, versym->gnu_needed);
2141d840867fSab196087 
2142d840867fSab196087 	for (cnt = 1; cnt <= vnd_num; cnt++,
21435aefb655Srie 	    vnd = (Verneed *)((uintptr_t)vnd + vnd->vn_next)) {
2144090a8d9eSAli Bahrami 		Conv_ver_flags_buf_t	ver_flags_buf;
21457c478bd9Sstevel@tonic-gate 		const char		*name, *dep;
21465aefb655Srie 		Half			vcnt = vnd->vn_cnt;
214733eb6ee1Sab196087 		Vernaux *vnap = (Vernaux *)((uintptr_t)vnd + vnd->vn_aux);
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate 		/*
21507c478bd9Sstevel@tonic-gate 		 * Obtain the name of the needed file and the version name
21517c478bd9Sstevel@tonic-gate 		 * within it that we're dependent on.  Note that the count
21527c478bd9Sstevel@tonic-gate 		 * should be at least one, otherwise this is a pretty bogus
21537c478bd9Sstevel@tonic-gate 		 * entry.
21547c478bd9Sstevel@tonic-gate 		 */
21557c478bd9Sstevel@tonic-gate 		name = string(vcache, cnt, scache, file, vnd->vn_file);
21567c478bd9Sstevel@tonic-gate 		if (vcnt)
21577c478bd9Sstevel@tonic-gate 			dep = string(vcache, cnt, scache, file, vnap->vna_name);
21587c478bd9Sstevel@tonic-gate 		else
21597c478bd9Sstevel@tonic-gate 			dep = MSG_INTL(MSG_STR_NULL);
21607c478bd9Sstevel@tonic-gate 
2161090a8d9eSAli Bahrami 		if (vnap->vna_other == 0) {	/* Traditional form */
2162090a8d9eSAli Bahrami 			index_str = MSG_ORIG(MSG_STR_EMPTY);
2163090a8d9eSAli Bahrami 		} else {			/* GNU form */
2164090a8d9eSAli Bahrami 			index_str = index;
2165d840867fSab196087 			/* Format the version index value */
2166d840867fSab196087 			(void) snprintf(index, MAXNDXSIZE,
2167d840867fSab196087 			    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(vnap->vna_other));
2168d840867fSab196087 			if (vnap->vna_other > versym->max_verndx)
2169d840867fSab196087 				versym->max_verndx = vnap->vna_other;
2170d840867fSab196087 		}
2171d840867fSab196087 		Elf_ver_line_1(0, index_str, name, dep,
2172090a8d9eSAli Bahrami 		    conv_ver_flags(vnap->vna_flags, 0, &ver_flags_buf));
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate 		/*
21757c478bd9Sstevel@tonic-gate 		 * Print any additional version dependencies.
21767c478bd9Sstevel@tonic-gate 		 */
21777c478bd9Sstevel@tonic-gate 		if (vcnt) {
21785aefb655Srie 			vnap = (Vernaux *)((uintptr_t)vnap + vnap->vna_next);
21797c478bd9Sstevel@tonic-gate 			for (vcnt--; vcnt; vcnt--,
21805aefb655Srie 			    vnap = (Vernaux *)((uintptr_t)vnap +
21817c478bd9Sstevel@tonic-gate 			    vnap->vna_next)) {
21827c478bd9Sstevel@tonic-gate 				dep = string(vcache, cnt, scache, file,
21837c478bd9Sstevel@tonic-gate 				    vnap->vna_name);
2184090a8d9eSAli Bahrami 				if (vnap->vna_other > 0) {
2185d840867fSab196087 					/* Format the next index value */
2186d840867fSab196087 					(void) snprintf(index, MAXNDXSIZE,
2187d840867fSab196087 					    MSG_ORIG(MSG_FMT_INDEX),
2188d840867fSab196087 					    EC_XWORD(vnap->vna_other));
2189090a8d9eSAli Bahrami 					Elf_ver_line_1(0, index,
2190d840867fSab196087 					    MSG_ORIG(MSG_STR_EMPTY), dep,
2191090a8d9eSAli Bahrami 					    conv_ver_flags(vnap->vna_flags,
2192090a8d9eSAli Bahrami 					    0, &ver_flags_buf));
2193d840867fSab196087 					if (vnap->vna_other >
2194d840867fSab196087 					    versym->max_verndx)
2195d840867fSab196087 						versym->max_verndx =
2196d840867fSab196087 						    vnap->vna_other;
2197d840867fSab196087 				} else {
2198d840867fSab196087 					Elf_ver_line_3(0,
2199d840867fSab196087 					    MSG_ORIG(MSG_STR_EMPTY), dep,
2200090a8d9eSAli Bahrami 					    conv_ver_flags(vnap->vna_flags,
2201090a8d9eSAli Bahrami 					    0, &ver_flags_buf));
2202d840867fSab196087 				}
2203d840867fSab196087 			}
2204d840867fSab196087 		}
2205d840867fSab196087 	}
2206d840867fSab196087 }
2207d840867fSab196087 
2208d840867fSab196087 /*
2209090a8d9eSAli Bahrami  * Examine the Verneed section for information related to GNU
2210090a8d9eSAli Bahrami  * style Versym indexing:
2211090a8d9eSAli Bahrami  *	- A non-zero vna_other field indicates that Versym indexes can
2212090a8d9eSAli Bahrami  *		reference Verneed records.
2213090a8d9eSAli Bahrami  *	- If the object uses GNU style Versym indexing, the
2214090a8d9eSAli Bahrami  *	  maximum index value is needed to detect bad Versym entries.
2215d840867fSab196087  *
2216d840867fSab196087  * entry:
2217d840867fSab196087  *	vnd - Address of verneed data
2218d840867fSab196087  *	vnd_num - # of Verneed entries
2219d840867fSab196087  *	versym - Information about versym section
2220d840867fSab196087  *
2221d840867fSab196087  * exit:
2222090a8d9eSAli Bahrami  *	If a non-zero vna_other field is seen, versym->gnu_needed is set.
2223090a8d9eSAli Bahrami  *
2224d840867fSab196087  *	versym->max_verndx has been updated to contain the largest
2225d840867fSab196087  *	version index seen.
2226d840867fSab196087  */
2227d840867fSab196087 static void
update_gnu_verndx(Verneed * vnd,Word vnd_num,VERSYM_STATE * versym)2228090a8d9eSAli Bahrami update_gnu_verndx(Verneed *vnd, Word vnd_num, VERSYM_STATE *versym)
2229d840867fSab196087 {
2230d840867fSab196087 	Word		cnt;
2231d840867fSab196087 
2232d840867fSab196087 	for (cnt = 1; cnt <= vnd_num; cnt++,
2233d840867fSab196087 	    vnd = (Verneed *)((uintptr_t)vnd + vnd->vn_next)) {
2234d840867fSab196087 		Half	vcnt = vnd->vn_cnt;
2235d840867fSab196087 		Vernaux	*vnap = (Vernaux *)((uintptr_t)vnd + vnd->vn_aux);
2236d840867fSab196087 
2237090a8d9eSAli Bahrami 		/*
2238090a8d9eSAli Bahrami 		 * A non-zero value of vna_other indicates that this
2239090a8d9eSAli Bahrami 		 * object references VERNEED items from the VERSYM
2240090a8d9eSAli Bahrami 		 * array.
2241090a8d9eSAli Bahrami 		 */
2242090a8d9eSAli Bahrami 		if (vnap->vna_other != 0) {
2243090a8d9eSAli Bahrami 			versym->gnu_needed = 1;
2244d840867fSab196087 			if (vnap->vna_other > versym->max_verndx)
2245d840867fSab196087 				versym->max_verndx = vnap->vna_other;
2246090a8d9eSAli Bahrami 		}
2247d840867fSab196087 
2248d840867fSab196087 		/*
2249d840867fSab196087 		 * Check any additional version dependencies.
2250d840867fSab196087 		 */
2251d840867fSab196087 		if (vcnt) {
2252d840867fSab196087 			vnap = (Vernaux *)((uintptr_t)vnap + vnap->vna_next);
2253d840867fSab196087 			for (vcnt--; vcnt; vcnt--,
2254d840867fSab196087 			    vnap = (Vernaux *)((uintptr_t)vnap +
2255d840867fSab196087 			    vnap->vna_next)) {
2256090a8d9eSAli Bahrami 				if (vnap->vna_other == 0)
2257090a8d9eSAli Bahrami 					continue;
2258090a8d9eSAli Bahrami 
2259090a8d9eSAli Bahrami 				versym->gnu_needed = 1;
2260d840867fSab196087 				if (vnap->vna_other > versym->max_verndx)
2261d840867fSab196087 					versym->max_verndx = vnap->vna_other;
22627c478bd9Sstevel@tonic-gate 			}
22637c478bd9Sstevel@tonic-gate 		}
22647c478bd9Sstevel@tonic-gate 	}
22657c478bd9Sstevel@tonic-gate }
22667c478bd9Sstevel@tonic-gate 
22677c478bd9Sstevel@tonic-gate /*
22683b41b08bSab196087  * Display version section information if the flags require it.
22693b41b08bSab196087  * Return version information needed by other output.
22703b41b08bSab196087  *
22713b41b08bSab196087  * entry:
22723b41b08bSab196087  *	cache - Cache of all section headers
22733b41b08bSab196087  *	shnum - # of sections in cache
22743b41b08bSab196087  *	file - Name of file
22753b41b08bSab196087  *	flags - Command line option flags
22763b41b08bSab196087  *	versym - VERSYM_STATE block to be filled in.
22777c478bd9Sstevel@tonic-gate  */
22783b41b08bSab196087 static void
versions(Cache * cache,Word shnum,const char * file,uint_t flags,VERSYM_STATE * versym)22793b41b08bSab196087 versions(Cache *cache, Word shnum, const char *file, uint_t flags,
22803b41b08bSab196087     VERSYM_STATE *versym)
22817c478bd9Sstevel@tonic-gate {
22827c478bd9Sstevel@tonic-gate 	GElf_Word	cnt;
2283d840867fSab196087 	Cache		*verdef_cache = NULL, *verneed_cache = NULL;
22843b41b08bSab196087 
2285d840867fSab196087 
2286d840867fSab196087 	/* Gather information about the version sections */
2287d840867fSab196087 	versym->max_verndx = 1;
22887c478bd9Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
22897c478bd9Sstevel@tonic-gate 		Cache		*_cache = &cache[cnt];
22905aefb655Srie 		Shdr		*shdr = _cache->c_shdr;
2291d840867fSab196087 		Dyn		*dyn;
2292d840867fSab196087 		ulong_t		numdyn;
22937c478bd9Sstevel@tonic-gate 
2294d840867fSab196087 		switch (shdr->sh_type) {
2295d840867fSab196087 		case SHT_DYNAMIC:
22967c478bd9Sstevel@tonic-gate 			/*
2297d840867fSab196087 			 * The GNU ld puts a DT_VERSYM entry in the dynamic
2298d840867fSab196087 			 * section so that the runtime linker can use it to
2299d840867fSab196087 			 * implement their versioning rules. They allow multiple
2300d840867fSab196087 			 * incompatible functions with the same name to exist
2301d840867fSab196087 			 * in different versions. The Solaris ld does not
2302d840867fSab196087 			 * support this mechanism, and as such, does not
2303d840867fSab196087 			 * produce DT_VERSYM. We use this fact to determine
2304d840867fSab196087 			 * which ld produced this object, and how to interpret
2305d840867fSab196087 			 * the version values.
23067c478bd9Sstevel@tonic-gate 			 */
2307d840867fSab196087 			if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0) ||
2308d840867fSab196087 			    (_cache->c_data == NULL))
2309d840867fSab196087 				continue;
2310d840867fSab196087 			numdyn = shdr->sh_size / shdr->sh_entsize;
2311d840867fSab196087 			dyn = (Dyn *)_cache->c_data->d_buf;
2312d840867fSab196087 			for (; numdyn-- > 0; dyn++)
2313d840867fSab196087 				if (dyn->d_tag == DT_VERSYM) {
2314090a8d9eSAli Bahrami 					versym->gnu_full =
2315090a8d9eSAli Bahrami 					    versym->gnu_needed = 1;
2316d840867fSab196087 					break;
2317d840867fSab196087 				}
2318d840867fSab196087 			break;
23193b41b08bSab196087 
2320d840867fSab196087 		case SHT_SUNW_versym:
2321d840867fSab196087 			/* Record data address for later symbol processing */
2322d840867fSab196087 			if (_cache->c_data != NULL) {
23233b41b08bSab196087 				versym->cache = _cache;
23243b41b08bSab196087 				versym->data = _cache->c_data->d_buf;
23257c478bd9Sstevel@tonic-gate 				continue;
23267c478bd9Sstevel@tonic-gate 			}
2327d840867fSab196087 			break;
23287c478bd9Sstevel@tonic-gate 
2329d840867fSab196087 		case SHT_SUNW_verdef:
2330d840867fSab196087 		case SHT_SUNW_verneed:
23313b41b08bSab196087 			/*
2332d840867fSab196087 			 * Ensure the data is non-NULL and the number
2333d840867fSab196087 			 * of items is non-zero. Otherwise, we don't
2334d840867fSab196087 			 * understand the section, and will not use it.
23357c478bd9Sstevel@tonic-gate 			 */
23369a411307Srie 			if ((_cache->c_data == NULL) ||
2337d840867fSab196087 			    (_cache->c_data->d_buf == NULL)) {
23387c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
2339d840867fSab196087 				    file, _cache->c_name);
23407c478bd9Sstevel@tonic-gate 				continue;
23417c478bd9Sstevel@tonic-gate 			}
2342d840867fSab196087 			if (shdr->sh_info == 0) {
2343d840867fSab196087 				(void) fprintf(stderr,
2344d840867fSab196087 				    MSG_INTL(MSG_ERR_BADSHINFO),
2345d840867fSab196087 				    file, _cache->c_name,
2346d840867fSab196087 				    EC_WORD(shdr->sh_info));
2347d840867fSab196087 				continue;
2348d840867fSab196087 			}
2349d840867fSab196087 
2350d840867fSab196087 			/* Make sure the string table index is in range */
2351d840867fSab196087 			if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
2352d840867fSab196087 				(void) fprintf(stderr,
2353d840867fSab196087 				    MSG_INTL(MSG_ERR_BADSHLINK), file,
2354d840867fSab196087 				    _cache->c_name, EC_WORD(shdr->sh_link));
23557c478bd9Sstevel@tonic-gate 				continue;
23567c478bd9Sstevel@tonic-gate 			}
23577c478bd9Sstevel@tonic-gate 
23587c478bd9Sstevel@tonic-gate 			/*
2359d840867fSab196087 			 * The section is usable. Save the cache entry.
23607c478bd9Sstevel@tonic-gate 			 */
2361d840867fSab196087 			if (shdr->sh_type == SHT_SUNW_verdef) {
2362d840867fSab196087 				verdef_cache = _cache;
2363d840867fSab196087 				/*
2364d840867fSab196087 				 * Under Solaris rules, if there is a verdef
2365d840867fSab196087 				 * section, the max versym index is number
2366d840867fSab196087 				 * of version definitions it supplies.
2367d840867fSab196087 				 */
2368d840867fSab196087 				versym->max_verndx = shdr->sh_info;
2369d840867fSab196087 			} else {
2370d840867fSab196087 				verneed_cache = _cache;
2371d840867fSab196087 			}
2372d840867fSab196087 			break;
2373d840867fSab196087 		}
23747c478bd9Sstevel@tonic-gate 	}
23757c478bd9Sstevel@tonic-gate 
2376d840867fSab196087 	/*
2377090a8d9eSAli Bahrami 	 * If there is a Verneed section, examine it for information
2378090a8d9eSAli Bahrami 	 * related to GNU style versioning.
2379d840867fSab196087 	 */
2380090a8d9eSAli Bahrami 	if (verneed_cache != NULL)
2381090a8d9eSAli Bahrami 		update_gnu_verndx((Verneed *)verneed_cache->c_data->d_buf,
2382d840867fSab196087 		    verneed_cache->c_shdr->sh_info, versym);
2383d840867fSab196087 
2384d840867fSab196087 	/*
2385d840867fSab196087 	 * Now that all the information is available, display the
2386090a8d9eSAli Bahrami 	 * Verdef and Verneed section contents, if requested.
2387d840867fSab196087 	 */
2388090a8d9eSAli Bahrami 	if ((flags & FLG_SHOW_VERSIONS) == 0)
2389090a8d9eSAli Bahrami 		return;
2390d840867fSab196087 	if (verdef_cache != NULL) {
2391d840867fSab196087 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2392d840867fSab196087 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_VERDEF),
2393d840867fSab196087 		    verdef_cache->c_name);
2394d840867fSab196087 		version_def((Verdef *)verdef_cache->c_data->d_buf,
2395d840867fSab196087 		    verdef_cache->c_shdr->sh_info, verdef_cache,
2396d840867fSab196087 		    &cache[verdef_cache->c_shdr->sh_link], file);
2397d840867fSab196087 	}
2398d840867fSab196087 	if (verneed_cache != NULL) {
2399d840867fSab196087 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2400d840867fSab196087 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_VERNEED),
2401d840867fSab196087 		    verneed_cache->c_name);
2402d840867fSab196087 		/*
2403d840867fSab196087 		 * If GNU versioning applies to this object, version_need()
2404d840867fSab196087 		 * will update versym->max_verndx, and it is not
2405090a8d9eSAli Bahrami 		 * necessary to call update_gnu_verndx().
2406d840867fSab196087 		 */
2407d840867fSab196087 		version_need((Verneed *)verneed_cache->c_data->d_buf,
2408d840867fSab196087 		    verneed_cache->c_shdr->sh_info, verneed_cache,
2409d840867fSab196087 		    &cache[verneed_cache->c_shdr->sh_link], file, versym);
24107c478bd9Sstevel@tonic-gate 	}
24117c478bd9Sstevel@tonic-gate }
24127c478bd9Sstevel@tonic-gate 
24137c478bd9Sstevel@tonic-gate /*
2414d579eb63Sab196087  * Search for and process any symbol tables.
2415d579eb63Sab196087  */
2416d579eb63Sab196087 void
symbols(Cache * cache,Word shnum,Ehdr * ehdr,uchar_t osabi,VERSYM_STATE * versym,const char * file,uint_t flags)24174f680cc6SAli Bahrami symbols(Cache *cache, Word shnum, Ehdr *ehdr, uchar_t osabi,
24184f680cc6SAli Bahrami     VERSYM_STATE *versym, const char *file, uint_t flags)
2419d579eb63Sab196087 {
2420d579eb63Sab196087 	SYMTBL_STATE state;
2421d579eb63Sab196087 	Cache *_cache;
2422d579eb63Sab196087 	Word secndx;
2423d579eb63Sab196087 
2424d579eb63Sab196087 	for (secndx = 1; secndx < shnum; secndx++) {
2425d579eb63Sab196087 		Word		symcnt;
2426d579eb63Sab196087 		Shdr		*shdr;
2427d579eb63Sab196087 
2428d579eb63Sab196087 		_cache = &cache[secndx];
2429d579eb63Sab196087 		shdr = _cache->c_shdr;
2430d579eb63Sab196087 
2431d579eb63Sab196087 		if ((shdr->sh_type != SHT_SYMTAB) &&
2432d579eb63Sab196087 		    (shdr->sh_type != SHT_DYNSYM) &&
24334f680cc6SAli Bahrami 		    ((shdr->sh_type != SHT_SUNW_LDYNSYM) ||
24344f680cc6SAli Bahrami 		    (osabi != ELFOSABI_SOLARIS)))
2435d579eb63Sab196087 			continue;
2436981a172dSab196087 		if (!match(MATCH_F_ALL, _cache->c_name, secndx, shdr->sh_type))
2437d579eb63Sab196087 			continue;
2438d579eb63Sab196087 
2439d579eb63Sab196087 		if (!init_symtbl_state(&state, cache, shnum, secndx, ehdr,
24404f680cc6SAli Bahrami 		    osabi, versym, file, flags))
2441d579eb63Sab196087 			continue;
2442d579eb63Sab196087 		/*
2443d579eb63Sab196087 		 * Loop through the symbol tables entries.
2444d579eb63Sab196087 		 */
2445d579eb63Sab196087 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2446d579eb63Sab196087 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMTAB), state.secname);
2447d579eb63Sab196087 		Elf_syms_table_title(0, ELF_DBG_ELFDUMP);
2448d579eb63Sab196087 
2449d579eb63Sab196087 		for (symcnt = 0; symcnt < state.symn; symcnt++)
2450b02637afSrie 			output_symbol(&state, symcnt, shdr->sh_info, symcnt,
2451d579eb63Sab196087 			    state.sym + symcnt);
2452d579eb63Sab196087 	}
2453d579eb63Sab196087 }
2454d579eb63Sab196087 
2455d579eb63Sab196087 /*
2456d579eb63Sab196087  * Search for and process any SHT_SUNW_symsort or SHT_SUNW_tlssort sections.
2457d579eb63Sab196087  * These sections are always associated with the .SUNW_ldynsym./.dynsym pair.
2458d579eb63Sab196087  */
2459d579eb63Sab196087 static void
sunw_sort(Cache * cache,Word shnum,Ehdr * ehdr,uchar_t osabi,VERSYM_STATE * versym,const char * file,uint_t flags)24604f680cc6SAli Bahrami sunw_sort(Cache *cache, Word shnum, Ehdr *ehdr, uchar_t osabi,
24614f680cc6SAli Bahrami     VERSYM_STATE *versym, const char *file, uint_t flags)
2462d579eb63Sab196087 {
2463d579eb63Sab196087 	SYMTBL_STATE	ldynsym_state,	dynsym_state;
2464d579eb63Sab196087 	Cache		*sortcache,	*symcache;
2465d579eb63Sab196087 	Shdr		*sortshdr,	*symshdr;
2466d579eb63Sab196087 	Word		sortsecndx,	symsecndx;
2467d579eb63Sab196087 	Word		ldynsym_cnt;
2468d579eb63Sab196087 	Word		*ndx;
2469d579eb63Sab196087 	Word		ndxn;
2470d579eb63Sab196087 	int		output_cnt = 0;
2471de777a60Sab196087 	Conv_inv_buf_t	inv_buf;
2472d579eb63Sab196087 
2473d579eb63Sab196087 	for (sortsecndx = 1; sortsecndx < shnum; sortsecndx++) {
2474d579eb63Sab196087 
2475d579eb63Sab196087 		sortcache = &cache[sortsecndx];
2476d579eb63Sab196087 		sortshdr = sortcache->c_shdr;
2477d579eb63Sab196087 
2478d579eb63Sab196087 		if ((sortshdr->sh_type != SHT_SUNW_symsort) &&
2479d579eb63Sab196087 		    (sortshdr->sh_type != SHT_SUNW_tlssort))
2480d579eb63Sab196087 			continue;
2481981a172dSab196087 		if (!match(MATCH_F_ALL, sortcache->c_name, sortsecndx,
2482981a172dSab196087 		    sortshdr->sh_type))
2483d579eb63Sab196087 			continue;
2484d579eb63Sab196087 
2485d579eb63Sab196087 		/*
2486d579eb63Sab196087 		 * If the section references a SUNW_ldynsym, then we
2487d579eb63Sab196087 		 * expect to see the associated .dynsym immediately
2488d579eb63Sab196087 		 * following. If it references a .dynsym, there is no
2489d579eb63Sab196087 		 * SUNW_ldynsym. If it is any other type, then we don't
2490d579eb63Sab196087 		 * know what to do with it.
2491d579eb63Sab196087 		 */
2492d579eb63Sab196087 		if ((sortshdr->sh_link == 0) || (sortshdr->sh_link >= shnum)) {
2493d579eb63Sab196087 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
2494d579eb63Sab196087 			    file, sortcache->c_name,
2495d579eb63Sab196087 			    EC_WORD(sortshdr->sh_link));
2496d579eb63Sab196087 			continue;
2497d579eb63Sab196087 		}
2498d579eb63Sab196087 		symcache = &cache[sortshdr->sh_link];
2499d579eb63Sab196087 		symshdr = symcache->c_shdr;
2500d579eb63Sab196087 		symsecndx = sortshdr->sh_link;
2501d579eb63Sab196087 		ldynsym_cnt = 0;
2502d579eb63Sab196087 		switch (symshdr->sh_type) {
2503d579eb63Sab196087 		case SHT_SUNW_LDYNSYM:
2504d579eb63Sab196087 			if (!init_symtbl_state(&ldynsym_state, cache, shnum,
25054f680cc6SAli Bahrami 			    symsecndx, ehdr, osabi, versym, file, flags))
2506d579eb63Sab196087 				continue;
2507d579eb63Sab196087 			ldynsym_cnt = ldynsym_state.symn;
2508d579eb63Sab196087 			/*
2509d579eb63Sab196087 			 * We know that the dynsym follows immediately
2510d579eb63Sab196087 			 * after the SUNW_ldynsym, and so, should be at
2511d579eb63Sab196087 			 * (sortshdr->sh_link + 1). However, elfdump is a
2512d579eb63Sab196087 			 * diagnostic tool, so we do the full paranoid
2513d579eb63Sab196087 			 * search instead.
2514d579eb63Sab196087 			 */
2515d579eb63Sab196087 			for (symsecndx = 1; symsecndx < shnum; symsecndx++) {
2516d579eb63Sab196087 				symcache = &cache[symsecndx];
2517d579eb63Sab196087 				symshdr = symcache->c_shdr;
2518d579eb63Sab196087 				if (symshdr->sh_type == SHT_DYNSYM)
2519d579eb63Sab196087 					break;
2520d579eb63Sab196087 			}
2521d579eb63Sab196087 			if (symsecndx >= shnum) {	/* Dynsym not found! */
2522d579eb63Sab196087 				(void) fprintf(stderr,
2523d579eb63Sab196087 				    MSG_INTL(MSG_ERR_NODYNSYM),
2524d579eb63Sab196087 				    file, sortcache->c_name);
2525d579eb63Sab196087 				continue;
2526d579eb63Sab196087 			}
2527d579eb63Sab196087 			/* Fallthrough to process associated dynsym */
2528d579eb63Sab196087 			/* FALLTHROUGH */
2529d579eb63Sab196087 		case SHT_DYNSYM:
2530d579eb63Sab196087 			if (!init_symtbl_state(&dynsym_state, cache, shnum,
25314f680cc6SAli Bahrami 			    symsecndx, ehdr, osabi, versym, file, flags))
2532d579eb63Sab196087 				continue;
2533d579eb63Sab196087 			break;
2534d579eb63Sab196087 		default:
2535d579eb63Sab196087 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADNDXSEC),
25364f680cc6SAli Bahrami 			    file, sortcache->c_name,
25374f680cc6SAli Bahrami 			    conv_sec_type(osabi, ehdr->e_machine,
25384f680cc6SAli Bahrami 			    symshdr->sh_type, 0, &inv_buf));
2539d579eb63Sab196087 			continue;
2540d579eb63Sab196087 		}
2541d579eb63Sab196087 
2542d579eb63Sab196087 		/*
2543d579eb63Sab196087 		 * Output header
2544d579eb63Sab196087 		 */
2545d579eb63Sab196087 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2546d579eb63Sab196087 		if (ldynsym_cnt > 0) {
2547d579eb63Sab196087 			dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMSORT2),
2548d579eb63Sab196087 			    sortcache->c_name, ldynsym_state.secname,
2549d579eb63Sab196087 			    dynsym_state.secname);
2550d579eb63Sab196087 			/*
2551d579eb63Sab196087 			 * The data for .SUNW_ldynsym and dynsym sections
2552d579eb63Sab196087 			 * is supposed to be adjacent with SUNW_ldynsym coming
2553d579eb63Sab196087 			 * first. Check, and issue a warning if it isn't so.
2554d579eb63Sab196087 			 */
255539773e46Sab196087 			if (((ldynsym_state.sym + ldynsym_state.symn)
255639773e46Sab196087 			    != dynsym_state.sym) &&
2557981a172dSab196087 			    ((flags & FLG_CTL_FAKESHDR) == 0))
2558d579eb63Sab196087 				(void) fprintf(stderr,
2559d579eb63Sab196087 				    MSG_INTL(MSG_ERR_LDYNNOTADJ), file,
2560d579eb63Sab196087 				    ldynsym_state.secname,
2561d579eb63Sab196087 				    dynsym_state.secname);
2562d579eb63Sab196087 		} else {
2563d579eb63Sab196087 			dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMSORT1),
2564d579eb63Sab196087 			    sortcache->c_name, dynsym_state.secname);
2565d579eb63Sab196087 		}
2566d579eb63Sab196087 		Elf_syms_table_title(0, ELF_DBG_ELFDUMP);
2567d579eb63Sab196087 
2568d579eb63Sab196087 		/* If not first one, insert a line of white space */
2569d579eb63Sab196087 		if (output_cnt++ > 0)
2570d579eb63Sab196087 			dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2571d579eb63Sab196087 
2572d579eb63Sab196087 		/*
2573d579eb63Sab196087 		 * SUNW_dynsymsort and SUNW_dyntlssort are arrays of
2574d579eb63Sab196087 		 * symbol indices. Iterate over the array entries,
2575d579eb63Sab196087 		 * dispaying the referenced symbols.
2576d579eb63Sab196087 		 */
2577d579eb63Sab196087 		ndxn = sortshdr->sh_size / sortshdr->sh_entsize;
2578d579eb63Sab196087 		ndx = (Word *)sortcache->c_data->d_buf;
2579d579eb63Sab196087 		for (; ndxn-- > 0; ndx++) {
2580d579eb63Sab196087 			if (*ndx >= ldynsym_cnt) {
2581d579eb63Sab196087 				Word sec_ndx = *ndx - ldynsym_cnt;
2582d579eb63Sab196087 
2583b02637afSrie 				output_symbol(&dynsym_state, sec_ndx, 0,
2584d579eb63Sab196087 				    *ndx, dynsym_state.sym + sec_ndx);
2585d579eb63Sab196087 			} else {
2586b02637afSrie 				output_symbol(&ldynsym_state, *ndx, 0,
2587d579eb63Sab196087 				    *ndx, ldynsym_state.sym + *ndx);
2588d579eb63Sab196087 			}
25897c478bd9Sstevel@tonic-gate 		}
25907c478bd9Sstevel@tonic-gate 	}
25917c478bd9Sstevel@tonic-gate }
25927c478bd9Sstevel@tonic-gate 
25937c478bd9Sstevel@tonic-gate /*
25947c478bd9Sstevel@tonic-gate  * Search for and process any relocation sections.
25957c478bd9Sstevel@tonic-gate  */
25967c478bd9Sstevel@tonic-gate static void
reloc(Cache * cache,Word shnum,Ehdr * ehdr,const char * file)25970e233487SRod Evans reloc(Cache *cache, Word shnum, Ehdr *ehdr, const char *file)
25987c478bd9Sstevel@tonic-gate {
25995aefb655Srie 	Word	cnt;
26007c478bd9Sstevel@tonic-gate 
26017c478bd9Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
26025aefb655Srie 		Word		type, symnum;
26035aefb655Srie 		Xword		relndx, relnum, relsize;
26045aefb655Srie 		void		*rels;
26055aefb655Srie 		Sym		*syms;
26065aefb655Srie 		Cache		*symsec, *strsec;
26077c478bd9Sstevel@tonic-gate 		Cache		*_cache = &cache[cnt];
26085aefb655Srie 		Shdr		*shdr = _cache->c_shdr;
26095aefb655Srie 		char		*relname = _cache->c_name;
2610de777a60Sab196087 		Conv_inv_buf_t	inv_buf;
26117c478bd9Sstevel@tonic-gate 
26127c478bd9Sstevel@tonic-gate 		if (((type = shdr->sh_type) != SHT_RELA) &&
26137c478bd9Sstevel@tonic-gate 		    (type != SHT_REL))
26147c478bd9Sstevel@tonic-gate 			continue;
2615981a172dSab196087 		if (!match(MATCH_F_ALL, relname, cnt, type))
26167c478bd9Sstevel@tonic-gate 			continue;
26177c478bd9Sstevel@tonic-gate 
26187c478bd9Sstevel@tonic-gate 		/*
26195aefb655Srie 		 * Decide entry size.
26207c478bd9Sstevel@tonic-gate 		 */
26215aefb655Srie 		if (((relsize = shdr->sh_entsize) == 0) ||
26225aefb655Srie 		    (relsize > shdr->sh_size)) {
26237c478bd9Sstevel@tonic-gate 			if (type == SHT_RELA)
26245aefb655Srie 				relsize = sizeof (Rela);
26257c478bd9Sstevel@tonic-gate 			else
26265aefb655Srie 				relsize = sizeof (Rel);
26277c478bd9Sstevel@tonic-gate 		}
26287c478bd9Sstevel@tonic-gate 
26297c478bd9Sstevel@tonic-gate 		/*
26307c478bd9Sstevel@tonic-gate 		 * Determine the number of relocations available.
26317c478bd9Sstevel@tonic-gate 		 */
26327c478bd9Sstevel@tonic-gate 		if (shdr->sh_size == 0) {
26337c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
26345aefb655Srie 			    file, relname);
26357c478bd9Sstevel@tonic-gate 			continue;
26367c478bd9Sstevel@tonic-gate 		}
26379a411307Srie 		if (_cache->c_data == NULL)
26389a411307Srie 			continue;
26399a411307Srie 
26405aefb655Srie 		rels = _cache->c_data->d_buf;
26415aefb655Srie 		relnum = shdr->sh_size / relsize;
26427c478bd9Sstevel@tonic-gate 
26437c478bd9Sstevel@tonic-gate 		/*
26445aefb655Srie 		 * Get the data buffer for the associated symbol table and
26455aefb655Srie 		 * string table.
26467c478bd9Sstevel@tonic-gate 		 */
26475aefb655Srie 		if (stringtbl(cache, 1, cnt, shnum, file,
26485aefb655Srie 		    &symnum, &symsec, &strsec) == 0)
26497c478bd9Sstevel@tonic-gate 			continue;
26507c478bd9Sstevel@tonic-gate 
26515aefb655Srie 		syms = symsec->c_data->d_buf;
26527c478bd9Sstevel@tonic-gate 
26537c478bd9Sstevel@tonic-gate 		/*
26547c478bd9Sstevel@tonic-gate 		 * Loop through the relocation entries.
26557c478bd9Sstevel@tonic-gate 		 */
26565aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
26575aefb655Srie 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_RELOC), _cache->c_name);
26585aefb655Srie 		Elf_reloc_title(0, ELF_DBG_ELFDUMP, type);
26597c478bd9Sstevel@tonic-gate 
26605aefb655Srie 		for (relndx = 0; relndx < relnum; relndx++,
26615aefb655Srie 		    rels = (void *)((char *)rels + relsize)) {
2662ba2be530Sab196087 			Half		mach = ehdr->e_machine;
26637c478bd9Sstevel@tonic-gate 			char		section[BUFSIZ];
26645aefb655Srie 			const char	*symname;
26655aefb655Srie 			Word		symndx, reltype;
26665aefb655Srie 			Rela		*rela;
26675aefb655Srie 			Rel		*rel;
26687c478bd9Sstevel@tonic-gate 
26697c478bd9Sstevel@tonic-gate 			/*
26705aefb655Srie 			 * Unravel the relocation and determine the symbol with
26715aefb655Srie 			 * which this relocation is associated.
26727c478bd9Sstevel@tonic-gate 			 */
26737c478bd9Sstevel@tonic-gate 			if (type == SHT_RELA) {
26745aefb655Srie 				rela = (Rela *)rels;
26755aefb655Srie 				symndx = ELF_R_SYM(rela->r_info);
2676ba2be530Sab196087 				reltype = ELF_R_TYPE(rela->r_info, mach);
26777c478bd9Sstevel@tonic-gate 			} else {
26785aefb655Srie 				rel = (Rel *)rels;
26795aefb655Srie 				symndx = ELF_R_SYM(rel->r_info);
2680ba2be530Sab196087 				reltype = ELF_R_TYPE(rel->r_info, mach);
26817c478bd9Sstevel@tonic-gate 			}
26827c478bd9Sstevel@tonic-gate 
26835aefb655Srie 			symname = relsymname(cache, _cache, strsec, symndx,
26840e233487SRod Evans 			    symnum, relndx, syms, section, BUFSIZ, file);
26855aefb655Srie 
26865aefb655Srie 			/*
26875aefb655Srie 			 * A zero symbol index is only valid for a few
26885aefb655Srie 			 * relocations.
26895aefb655Srie 			 */
26905aefb655Srie 			if (symndx == 0) {
26915aefb655Srie 				int	badrel = 0;
26925aefb655Srie 
26935aefb655Srie 				if ((mach == EM_SPARC) ||
26945aefb655Srie 				    (mach == EM_SPARC32PLUS) ||
26955aefb655Srie 				    (mach == EM_SPARCV9)) {
26965aefb655Srie 					if ((reltype != R_SPARC_NONE) &&
26975aefb655Srie 					    (reltype != R_SPARC_REGISTER) &&
26985aefb655Srie 					    (reltype != R_SPARC_RELATIVE))
26995aefb655Srie 						badrel++;
27005aefb655Srie 				} else if (mach == EM_386) {
27015aefb655Srie 					if ((reltype != R_386_NONE) &&
27025aefb655Srie 					    (reltype != R_386_RELATIVE))
27035aefb655Srie 						badrel++;
27045aefb655Srie 				} else if (mach == EM_AMD64) {
27055aefb655Srie 					if ((reltype != R_AMD64_NONE) &&
27065aefb655Srie 					    (reltype != R_AMD64_RELATIVE))
27075aefb655Srie 						badrel++;
27085aefb655Srie 				}
27095aefb655Srie 
27105aefb655Srie 				if (badrel) {
27117c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
27127c478bd9Sstevel@tonic-gate 					    MSG_INTL(MSG_ERR_BADREL1), file,
2713de777a60Sab196087 					    conv_reloc_type(mach, reltype,
2714de777a60Sab196087 					    0, &inv_buf));
27155aefb655Srie 				}
27167c478bd9Sstevel@tonic-gate 			}
27177c478bd9Sstevel@tonic-gate 
27185aefb655Srie 			Elf_reloc_entry_1(0, ELF_DBG_ELFDUMP,
27195aefb655Srie 			    MSG_ORIG(MSG_STR_EMPTY), ehdr->e_machine, type,
27205aefb655Srie 			    rels, relname, symname, 0);
27217c478bd9Sstevel@tonic-gate 		}
27227c478bd9Sstevel@tonic-gate 	}
27237c478bd9Sstevel@tonic-gate }
27247c478bd9Sstevel@tonic-gate 
2725f959f76aSab196087 
2726f959f76aSab196087 /*
2727f959f76aSab196087  * This value controls which test dyn_test() performs.
2728f959f76aSab196087  */
2729f959f76aSab196087 typedef enum { DYN_TEST_ADDR, DYN_TEST_SIZE, DYN_TEST_ENTSIZE } dyn_test_t;
2730f959f76aSab196087 
2731f959f76aSab196087 /*
2732f959f76aSab196087  * Used by dynamic() to compare the value of a dynamic element against
2733f959f76aSab196087  * the starting address of the section it references.
2734f959f76aSab196087  *
2735f959f76aSab196087  * entry:
2736f959f76aSab196087  *	test_type - Specify which dyn item is being tested.
2737f959f76aSab196087  *	sh_type - SHT_* type value for required section.
2738f959f76aSab196087  *	sec_cache - Cache entry for section, or NULL if the object lacks
2739f959f76aSab196087  *		a section of this type.
2740f959f76aSab196087  *	dyn - Dyn entry to be tested
2741f959f76aSab196087  *	dynsec_cnt - # of dynamic section being examined. The first
2742f959f76aSab196087  *		dynamic section is 1, the next is 2, and so on...
2743f959f76aSab196087  *	ehdr - ELF header for file
2744f959f76aSab196087  *	file - Name of file
2745f959f76aSab196087  */
2746f959f76aSab196087 static void
dyn_test(dyn_test_t test_type,Word sh_type,Cache * sec_cache,Dyn * dyn,Word dynsec_cnt,Ehdr * ehdr,uchar_t osabi,const char * file)2747f959f76aSab196087 dyn_test(dyn_test_t test_type, Word sh_type, Cache *sec_cache, Dyn *dyn,
27484f680cc6SAli Bahrami     Word dynsec_cnt, Ehdr *ehdr, uchar_t osabi, const char *file)
2749f959f76aSab196087 {
2750f959f76aSab196087 	Conv_inv_buf_t	buf1, buf2;
2751f959f76aSab196087 
2752f959f76aSab196087 	/*
2753f959f76aSab196087 	 * These tests are based around the implicit assumption that
2754f959f76aSab196087 	 * there is only one dynamic section in an object, and also only
2755f959f76aSab196087 	 * one of the sections it references. We have therefore gathered
2756f959f76aSab196087 	 * all of the necessary information to test this in a single pass
2757f959f76aSab196087 	 * over the section headers, which is very efficient. We are not
2758f959f76aSab196087 	 * aware of any case where more than one dynamic section would
2759f959f76aSab196087 	 * be meaningful in an ELF object, so this is a reasonable solution.
2760f959f76aSab196087 	 *
2761f959f76aSab196087 	 * To test multiple dynamic sections correctly would be more
2762f959f76aSab196087 	 * expensive in code and time. We would have to build a data structure
2763f959f76aSab196087 	 * containing all the dynamic elements. Then, we would use the address
2764f959f76aSab196087 	 * to locate the section it references and ensure the section is of
2765f959f76aSab196087 	 * the right type and that the address in the dynamic element is
2766f959f76aSab196087 	 * to the start of the section. Then, we could check the size and
2767f959f76aSab196087 	 * entsize values against those same sections. This is O(n^2), and
2768f959f76aSab196087 	 * also complicated.
2769f959f76aSab196087 	 *
2770f959f76aSab196087 	 * In the highly unlikely case that there is more than one dynamic
2771f959f76aSab196087 	 * section, we only test the first one, and simply allow the values
2772f959f76aSab196087 	 * of the subsequent one to be displayed unchallenged.
2773f959f76aSab196087 	 */
2774f959f76aSab196087 	if (dynsec_cnt != 1)
2775f959f76aSab196087 		return;
2776f959f76aSab196087 
2777f959f76aSab196087 	/*
2778f959f76aSab196087 	 * A DT_ item that references a section address should always find
2779f959f76aSab196087 	 * the section in the file.
2780f959f76aSab196087 	 */
2781f959f76aSab196087 	if (sec_cache == NULL) {
27821d9df23bSab196087 		const char *name;
27831d9df23bSab196087 
27841d9df23bSab196087 		/*
27851d9df23bSab196087 		 * Supply section names instead of section types for
27861d9df23bSab196087 		 * things that reference progbits so that the error
27871d9df23bSab196087 		 * message will make more sense.
27881d9df23bSab196087 		 */
27891d9df23bSab196087 		switch (dyn->d_tag) {
27901d9df23bSab196087 		case DT_INIT:
27911d9df23bSab196087 			name = MSG_ORIG(MSG_ELF_INIT);
27921d9df23bSab196087 			break;
27931d9df23bSab196087 		case DT_FINI:
27941d9df23bSab196087 			name = MSG_ORIG(MSG_ELF_FINI);
27951d9df23bSab196087 			break;
27961d9df23bSab196087 		default:
27974f680cc6SAli Bahrami 			name = conv_sec_type(osabi, ehdr->e_machine,
27984f680cc6SAli Bahrami 			    sh_type, 0, &buf1);
27991d9df23bSab196087 			break;
28001d9df23bSab196087 		}
2801f959f76aSab196087 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_DYNNOBCKSEC), file,
28024f680cc6SAli Bahrami 		    name, conv_dyn_tag(dyn->d_tag, osabi, ehdr->e_machine,
28033228339cSAli Bahrami 		    CONV_FMT_ALT_CF, &buf2));
2804f959f76aSab196087 		return;
2805f959f76aSab196087 	}
2806f959f76aSab196087 
2807f959f76aSab196087 
2808f959f76aSab196087 	switch (test_type) {
2809f959f76aSab196087 	case DYN_TEST_ADDR:
2810f959f76aSab196087 		/* The section address should match the DT_ item value */
2811f959f76aSab196087 		if (dyn->d_un.d_val != sec_cache->c_shdr->sh_addr)
2812f959f76aSab196087 			(void) fprintf(stderr,
2813f959f76aSab196087 			    MSG_INTL(MSG_ERR_DYNBADADDR), file,
28144f680cc6SAli Bahrami 			    conv_dyn_tag(dyn->d_tag, osabi, ehdr->e_machine,
28153228339cSAli Bahrami 			    CONV_FMT_ALT_CF, &buf1), EC_ADDR(dyn->d_un.d_val),
28164f680cc6SAli Bahrami 			    sec_cache->c_ndx, sec_cache->c_name,
2817f959f76aSab196087 			    EC_ADDR(sec_cache->c_shdr->sh_addr));
2818f959f76aSab196087 		break;
2819f959f76aSab196087 
2820f959f76aSab196087 	case DYN_TEST_SIZE:
2821f959f76aSab196087 		/* The section size should match the DT_ item value */
2822f959f76aSab196087 		if (dyn->d_un.d_val != sec_cache->c_shdr->sh_size)
2823f959f76aSab196087 			(void) fprintf(stderr,
2824f959f76aSab196087 			    MSG_INTL(MSG_ERR_DYNBADSIZE), file,
28254f680cc6SAli Bahrami 			    conv_dyn_tag(dyn->d_tag, osabi, ehdr->e_machine,
28263228339cSAli Bahrami 			    CONV_FMT_ALT_CF, &buf1), EC_XWORD(dyn->d_un.d_val),
2827f959f76aSab196087 			    sec_cache->c_ndx, sec_cache->c_name,
2828f959f76aSab196087 			    EC_XWORD(sec_cache->c_shdr->sh_size));
2829f959f76aSab196087 		break;
2830f959f76aSab196087 
2831f959f76aSab196087 	case DYN_TEST_ENTSIZE:
2832f959f76aSab196087 		/* The sh_entsize value should match the DT_ item value */
2833f959f76aSab196087 		if (dyn->d_un.d_val != sec_cache->c_shdr->sh_entsize)
2834f959f76aSab196087 			(void) fprintf(stderr,
2835f959f76aSab196087 			    MSG_INTL(MSG_ERR_DYNBADENTSIZE), file,
28364f680cc6SAli Bahrami 			    conv_dyn_tag(dyn->d_tag, osabi, ehdr->e_machine,
28373228339cSAli Bahrami 			    CONV_FMT_ALT_CF, &buf1), EC_XWORD(dyn->d_un.d_val),
2838f959f76aSab196087 			    sec_cache->c_ndx, sec_cache->c_name,
2839f959f76aSab196087 			    EC_XWORD(sec_cache->c_shdr->sh_entsize));
2840f959f76aSab196087 		break;
2841f959f76aSab196087 	}
2842f959f76aSab196087 }
2843f959f76aSab196087 
28447c478bd9Sstevel@tonic-gate /*
28451d9df23bSab196087  * There are some DT_ entries that have corresponding symbols
28461d9df23bSab196087  * (e.g. DT_INIT and _init). It is expected that these items will
28471d9df23bSab196087  * both have the same value if both are present. This routine
28481d9df23bSab196087  * examines the well known symbol tables for such symbols and
28491d9df23bSab196087  * issues warnings for any that don't match.
28501d9df23bSab196087  *
28511d9df23bSab196087  * entry:
28521d9df23bSab196087  *	dyn - Dyn entry to be tested
28531d9df23bSab196087  *	symname - Name of symbol that corresponds to dyn
28541d9df23bSab196087  *	symtab_cache, dynsym_cache, ldynsym_cache - Symbol tables to check
285531dd2c84SAli Bahrami  *	target_cache - Section the symname section is expected to be
285631dd2c84SAli Bahrami  *		associated with.
28571d9df23bSab196087  *	cache - Cache of all section headers
28581d9df23bSab196087  *	shnum - # of sections in cache
28591d9df23bSab196087  *	ehdr - ELF header for file
286031dd2c84SAli Bahrami  *	osabi - OSABI to apply when interpreting object
28611d9df23bSab196087  *	file - Name of file
28621d9df23bSab196087  */
28631d9df23bSab196087 static void
dyn_symtest(Dyn * dyn,const char * symname,Cache * symtab_cache,Cache * dynsym_cache,Cache * ldynsym_cache,Cache * target_cache,Cache * cache,Word shnum,Ehdr * ehdr,uchar_t osabi,const char * file)28641d9df23bSab196087 dyn_symtest(Dyn *dyn, const char *symname, Cache *symtab_cache,
286531dd2c84SAli Bahrami     Cache *dynsym_cache, Cache *ldynsym_cache, Cache *target_cache,
286631dd2c84SAli Bahrami     Cache *cache, Word shnum, Ehdr *ehdr, uchar_t osabi, const char *file)
28671d9df23bSab196087 {
28681d9df23bSab196087 	Conv_inv_buf_t	buf;
28691d9df23bSab196087 	int		i;
28701d9df23bSab196087 	Sym		*sym;
28711d9df23bSab196087 	Cache		*_cache;
28721d9df23bSab196087 
28731d9df23bSab196087 	for (i = 0; i < 3; i++) {
28741d9df23bSab196087 		switch (i) {
28751d9df23bSab196087 		case 0:
28761d9df23bSab196087 			_cache = symtab_cache;
28771d9df23bSab196087 			break;
28781d9df23bSab196087 		case 1:
28791d9df23bSab196087 			_cache = dynsym_cache;
28801d9df23bSab196087 			break;
28811d9df23bSab196087 		case 2:
28821d9df23bSab196087 			_cache = ldynsym_cache;
28831d9df23bSab196087 			break;
28841d9df23bSab196087 		}
28851d9df23bSab196087 
28861d9df23bSab196087 		if ((_cache != NULL) &&
288731dd2c84SAli Bahrami 		    symlookup(symname, cache, shnum, &sym, target_cache,
288831dd2c84SAli Bahrami 		    _cache, file) && (sym->st_value != dyn->d_un.d_val))
28891d9df23bSab196087 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_DYNSYMVAL),
28904f680cc6SAli Bahrami 			    file, _cache->c_name, conv_dyn_tag(dyn->d_tag,
28913228339cSAli Bahrami 			    osabi, ehdr->e_machine, CONV_FMT_ALT_CF, &buf),
28921d9df23bSab196087 			    symname, EC_ADDR(sym->st_value));
28931d9df23bSab196087 	}
28941d9df23bSab196087 }
28951d9df23bSab196087 
28961d9df23bSab196087 /*
28977c478bd9Sstevel@tonic-gate  * Search for and process a .dynamic section.
28987c478bd9Sstevel@tonic-gate  */
28997c478bd9Sstevel@tonic-gate static void
dynamic(Cache * cache,Word shnum,Ehdr * ehdr,uchar_t osabi,const char * file)29004f680cc6SAli Bahrami dynamic(Cache *cache, Word shnum, Ehdr *ehdr, uchar_t osabi, const char *file)
29017c478bd9Sstevel@tonic-gate {
2902f959f76aSab196087 	struct {
29031d9df23bSab196087 		Cache	*symtab;
2904f959f76aSab196087 		Cache	*dynstr;
2905f959f76aSab196087 		Cache	*dynsym;
2906f959f76aSab196087 		Cache	*hash;
2907f959f76aSab196087 		Cache	*fini;
2908f959f76aSab196087 		Cache	*fini_array;
2909f959f76aSab196087 		Cache	*init;
2910f959f76aSab196087 		Cache	*init_array;
2911f959f76aSab196087 		Cache	*preinit_array;
2912f959f76aSab196087 		Cache	*rel;
2913f959f76aSab196087 		Cache	*rela;
2914f959f76aSab196087 		Cache	*sunw_cap;
291508278a5eSRod Evans 		Cache	*sunw_capinfo;
291608278a5eSRod Evans 		Cache	*sunw_capchain;
2917f959f76aSab196087 		Cache	*sunw_ldynsym;
2918f959f76aSab196087 		Cache	*sunw_move;
2919f959f76aSab196087 		Cache	*sunw_syminfo;
2920f959f76aSab196087 		Cache	*sunw_symsort;
2921f959f76aSab196087 		Cache	*sunw_tlssort;
2922f959f76aSab196087 		Cache	*sunw_verdef;
2923f959f76aSab196087 		Cache	*sunw_verneed;
2924f959f76aSab196087 		Cache	*sunw_versym;
2925f959f76aSab196087 	} sec;
2926f959f76aSab196087 	Word	dynsec_ndx;
2927f959f76aSab196087 	Word	dynsec_num;
2928f959f76aSab196087 	int	dynsec_cnt;
29295aefb655Srie 	Word	cnt;
29304f680cc6SAli Bahrami 	int	osabi_solaris = osabi == ELFOSABI_SOLARIS;
29317c478bd9Sstevel@tonic-gate 
2932f959f76aSab196087 	/*
2933f959f76aSab196087 	 * Make a pass over all the sections, gathering section information
2934f959f76aSab196087 	 * we'll need below.
2935f959f76aSab196087 	 */
2936f959f76aSab196087 	dynsec_num = 0;
2937f959f76aSab196087 	bzero(&sec, sizeof (sec));
29387c478bd9Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
2939f959f76aSab196087 		Cache	*_cache = &cache[cnt];
2940f959f76aSab196087 
2941f959f76aSab196087 		switch (_cache->c_shdr->sh_type) {
2942f959f76aSab196087 		case SHT_DYNAMIC:
2943f959f76aSab196087 			if (dynsec_num == 0) {
2944f959f76aSab196087 				dynsec_ndx = cnt;
2945f959f76aSab196087 
2946f959f76aSab196087 				/* Does it have a valid string table? */
2947f959f76aSab196087 				(void) stringtbl(cache, 0, cnt, shnum, file,
2948f959f76aSab196087 				    0, 0, &sec.dynstr);
2949f959f76aSab196087 			}
2950f959f76aSab196087 			dynsec_num++;
2951f959f76aSab196087 			break;
2952f959f76aSab196087 
2953f959f76aSab196087 
2954f959f76aSab196087 		case SHT_PROGBITS:
2955f959f76aSab196087 			/*
2956f959f76aSab196087 			 * We want to detect the .init and .fini sections,
2957f959f76aSab196087 			 * if present. These are SHT_PROGBITS, so all we
2958f959f76aSab196087 			 * have to go on is the section name. Normally comparing
2959f959f76aSab196087 			 * names is a bad idea, but there are some special
2960f959f76aSab196087 			 * names (i.e. .init/.fini/.interp) that are very
2961f959f76aSab196087 			 * difficult to use in any other context, and for
2962f959f76aSab196087 			 * these symbols, we do the heuristic match.
2963f959f76aSab196087 			 */
2964f959f76aSab196087 			if (strcmp(_cache->c_name,
2965f959f76aSab196087 			    MSG_ORIG(MSG_ELF_INIT)) == 0) {
2966f959f76aSab196087 				if (sec.init == NULL)
2967f959f76aSab196087 					sec.init = _cache;
2968f959f76aSab196087 			} else if (strcmp(_cache->c_name,
2969f959f76aSab196087 			    MSG_ORIG(MSG_ELF_FINI)) == 0) {
2970f959f76aSab196087 				if (sec.fini == NULL)
2971f959f76aSab196087 					sec.fini = _cache;
2972f959f76aSab196087 			}
2973f959f76aSab196087 			break;
2974f959f76aSab196087 
2975f959f76aSab196087 		case SHT_REL:
2976f959f76aSab196087 			/*
2977f959f76aSab196087 			 * We want the SHT_REL section with the lowest
2978f959f76aSab196087 			 * offset. The linker gathers them together,
2979f959f76aSab196087 			 * and puts the address of the first one
2980f959f76aSab196087 			 * into the DT_REL dynamic element.
2981f959f76aSab196087 			 */
2982f959f76aSab196087 			if ((sec.rel == NULL) ||
2983f959f76aSab196087 			    (_cache->c_shdr->sh_offset <
2984f959f76aSab196087 			    sec.rel->c_shdr->sh_offset))
2985f959f76aSab196087 				sec.rel = _cache;
2986f959f76aSab196087 			break;
2987f959f76aSab196087 
2988f959f76aSab196087 		case SHT_RELA:
2989f959f76aSab196087 			/* RELA is handled just like RELA above */
2990f959f76aSab196087 			if ((sec.rela == NULL) ||
2991f959f76aSab196087 			    (_cache->c_shdr->sh_offset <
2992f959f76aSab196087 			    sec.rela->c_shdr->sh_offset))
2993f959f76aSab196087 				sec.rela = _cache;
2994f959f76aSab196087 			break;
2995f959f76aSab196087 
2996f959f76aSab196087 		/*
2997f959f76aSab196087 		 * The GRAB macro is used for the simple case in which
2998f959f76aSab196087 		 * we simply grab the first section of the desired type.
2999f959f76aSab196087 		 */
3000f959f76aSab196087 #define	GRAB(_sec_type, _sec_field) \
3001f959f76aSab196087 		case _sec_type: \
3002f959f76aSab196087 			if (sec._sec_field == NULL) \
3003f959f76aSab196087 				sec._sec_field = _cache; \
3004f959f76aSab196087 				break
30051d9df23bSab196087 		GRAB(SHT_SYMTAB,	symtab);
3006f959f76aSab196087 		GRAB(SHT_DYNSYM,	dynsym);
3007f959f76aSab196087 		GRAB(SHT_FINI_ARRAY,	fini_array);
3008f959f76aSab196087 		GRAB(SHT_HASH,		hash);
3009f959f76aSab196087 		GRAB(SHT_INIT_ARRAY,	init_array);
3010f959f76aSab196087 		GRAB(SHT_SUNW_move,	sunw_move);
3011f959f76aSab196087 		GRAB(SHT_PREINIT_ARRAY,	preinit_array);
3012f959f76aSab196087 		GRAB(SHT_SUNW_cap,	sunw_cap);
301308278a5eSRod Evans 		GRAB(SHT_SUNW_capinfo,	sunw_capinfo);
301408278a5eSRod Evans 		GRAB(SHT_SUNW_capchain,	sunw_capchain);
3015f959f76aSab196087 		GRAB(SHT_SUNW_LDYNSYM,	sunw_ldynsym);
3016f959f76aSab196087 		GRAB(SHT_SUNW_syminfo,	sunw_syminfo);
3017f959f76aSab196087 		GRAB(SHT_SUNW_symsort,	sunw_symsort);
3018f959f76aSab196087 		GRAB(SHT_SUNW_tlssort,	sunw_tlssort);
3019f959f76aSab196087 		GRAB(SHT_SUNW_verdef,	sunw_verdef);
3020f959f76aSab196087 		GRAB(SHT_SUNW_verneed,	sunw_verneed);
3021f959f76aSab196087 		GRAB(SHT_SUNW_versym,	sunw_versym);
3022f959f76aSab196087 #undef GRAB
3023f959f76aSab196087 		}
3024f959f76aSab196087 	}
3025f959f76aSab196087 
3026f959f76aSab196087 	/*
3027f959f76aSab196087 	 * If no dynamic section, return immediately. If more than one
3028f959f76aSab196087 	 * dynamic section, then something odd is going on and an error
3029f959f76aSab196087 	 * is in order, but then continue on and display them all.
3030f959f76aSab196087 	 */
3031f959f76aSab196087 	if (dynsec_num == 0)
3032f959f76aSab196087 		return;
3033f959f76aSab196087 	if (dynsec_num > 1)
3034f959f76aSab196087 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_MULTDYN),
3035f959f76aSab196087 		    file, EC_WORD(dynsec_num));
3036f959f76aSab196087 
3037f959f76aSab196087 
3038f959f76aSab196087 	dynsec_cnt = 0;
3039f959f76aSab196087 	for (cnt = dynsec_ndx; (cnt < shnum) && (dynsec_cnt < dynsec_num);
3040f959f76aSab196087 	    cnt++) {
30415aefb655Srie 		Dyn	*dyn;
30427c478bd9Sstevel@tonic-gate 		ulong_t	numdyn;
30433244bcaaSab196087 		int	ndx, end_ndx;
30445aefb655Srie 		Cache	*_cache = &cache[cnt], *strsec;
30455aefb655Srie 		Shdr	*shdr = _cache->c_shdr;
3046f959f76aSab196087 		int	dumped = 0;
30477c478bd9Sstevel@tonic-gate 
30487c478bd9Sstevel@tonic-gate 		if (shdr->sh_type != SHT_DYNAMIC)
30497c478bd9Sstevel@tonic-gate 			continue;
3050f959f76aSab196087 		dynsec_cnt++;
30517c478bd9Sstevel@tonic-gate 
30527c478bd9Sstevel@tonic-gate 		/*
30535aefb655Srie 		 * Verify the associated string table section.
30547c478bd9Sstevel@tonic-gate 		 */
30555aefb655Srie 		if (stringtbl(cache, 0, cnt, shnum, file, 0, 0, &strsec) == 0)
30567c478bd9Sstevel@tonic-gate 			continue;
30575aefb655Srie 
30589a411307Srie 		if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
30599a411307Srie 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
30609a411307Srie 			    file, _cache->c_name);
30619a411307Srie 			continue;
30629a411307Srie 		}
30639a411307Srie 		if (_cache->c_data == NULL)
30649a411307Srie 			continue;
30659a411307Srie 
30667c478bd9Sstevel@tonic-gate 		numdyn = shdr->sh_size / shdr->sh_entsize;
30675aefb655Srie 		dyn = (Dyn *)_cache->c_data->d_buf;
30687c478bd9Sstevel@tonic-gate 
3069f959f76aSab196087 		/*
3070f959f76aSab196087 		 * We expect the REL/RELA entries to reference the reloc
3071f959f76aSab196087 		 * section with the lowest address. However, this is
3072f959f76aSab196087 		 * not true for dumped objects. Detect if this object has
3073f959f76aSab196087 		 * been dumped so that we can skip the reloc address test
3074f959f76aSab196087 		 * in that case.
3075f959f76aSab196087 		 */
3076f959f76aSab196087 		for (ndx = 0; ndx < numdyn; dyn++, ndx++) {
3077f959f76aSab196087 			if (dyn->d_tag == DT_FLAGS_1) {
3078f959f76aSab196087 				dumped = (dyn->d_un.d_val & DF_1_CONFALT) != 0;
3079f959f76aSab196087 				break;
3080f959f76aSab196087 			}
3081f959f76aSab196087 		}
3082f959f76aSab196087 		dyn = (Dyn *)_cache->c_data->d_buf;
3083f959f76aSab196087 
30845aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
30855aefb655Srie 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_DYNAMIC), _cache->c_name);
30867c478bd9Sstevel@tonic-gate 
30875aefb655Srie 		Elf_dyn_title(0);
30887c478bd9Sstevel@tonic-gate 
30895aefb655Srie 		for (ndx = 0; ndx < numdyn; dyn++, ndx++) {
3090de777a60Sab196087 			union {
3091ba2be530Sab196087 				Conv_inv_buf_t		inv;
3092de777a60Sab196087 				Conv_dyn_flag_buf_t	flag;
3093de777a60Sab196087 				Conv_dyn_flag1_buf_t	flag1;
3094de777a60Sab196087 				Conv_dyn_posflag1_buf_t	posflag1;
3095de777a60Sab196087 				Conv_dyn_feature1_buf_t	feature1;
3096de777a60Sab196087 			} c_buf;
3097f959f76aSab196087 			const char	*name = NULL;
30987c478bd9Sstevel@tonic-gate 
30997c478bd9Sstevel@tonic-gate 			/*
31007c478bd9Sstevel@tonic-gate 			 * Print the information numerically, and if possible
3101f959f76aSab196087 			 * as a string. If a string is available, name is
3102f959f76aSab196087 			 * set to reference it.
3103f959f76aSab196087 			 *
3104f959f76aSab196087 			 * Also, take this opportunity to sanity check
3105f959f76aSab196087 			 * the values of DT elements. In the code above,
3106f959f76aSab196087 			 * we gathered information on sections that are
3107f959f76aSab196087 			 * referenced by the dynamic section. Here, we
3108f959f76aSab196087 			 * compare the attributes of those sections to
3109f959f76aSab196087 			 * the DT_ items that reference them and report
3110f959f76aSab196087 			 * on inconsistencies.
3111f959f76aSab196087 			 *
3112f959f76aSab196087 			 * Things not currently tested that could be improved
3113f959f76aSab196087 			 * in later revisions include:
3114f959f76aSab196087 			 *	- We don't check PLT or GOT related items
3115f959f76aSab196087 			 *	- We don't handle computing the lengths of
3116f959f76aSab196087 			 *		relocation arrays. To handle this
3117f959f76aSab196087 			 *		requires examining data that spans
3118f959f76aSab196087 			 *		across sections, in a contiguous span
3119f959f76aSab196087 			 *		within a single segment.
3120f959f76aSab196087 			 *	- DT_VERDEFNUM and DT_VERNEEDNUM can't be
3121f959f76aSab196087 			 *		verified without parsing the sections.
3122f959f76aSab196087 			 *	- We don't handle DT_SUNW_SYMSZ, which would
3123f959f76aSab196087 			 *		be the sum of the lengths of .dynsym and
3124f959f76aSab196087 			 *		.SUNW_ldynsym
3125f959f76aSab196087 			 *	- DT_SUNW_STRPAD can't be verified other than
3126f959f76aSab196087 			 *		to check that it's not larger than
3127f959f76aSab196087 			 *		the string table.
3128f959f76aSab196087 			 *	- Some items come in "all or none" clusters
3129f959f76aSab196087 			 *		that give an address, element size,
3130f959f76aSab196087 			 *		and data length in bytes. We don't
3131f959f76aSab196087 			 *		verify that there are no missing items
3132f959f76aSab196087 			 *		in such groups.
31337c478bd9Sstevel@tonic-gate 			 */
31343244bcaaSab196087 			switch (dyn->d_tag) {
31353244bcaaSab196087 			case DT_NULL:
31363244bcaaSab196087 				/*
31373244bcaaSab196087 				 * Special case: DT_NULLs can come in groups
31383244bcaaSab196087 				 * that we prefer to reduce to a single line.
31393244bcaaSab196087 				 */
31403244bcaaSab196087 				end_ndx = ndx;
31413244bcaaSab196087 				while ((end_ndx < (numdyn - 1)) &&
31423244bcaaSab196087 				    ((dyn + 1)->d_tag == DT_NULL)) {
31433244bcaaSab196087 					dyn++;
31443244bcaaSab196087 					end_ndx++;
31453244bcaaSab196087 				}
31463244bcaaSab196087 				Elf_dyn_null_entry(0, dyn, ndx, end_ndx);
31473244bcaaSab196087 				ndx = end_ndx;
31483244bcaaSab196087 				continue;
31493244bcaaSab196087 
31503244bcaaSab196087 			/*
3151f959f76aSab196087 			 * String items all reference the dynstr. The string()
3152f959f76aSab196087 			 * function does the necessary sanity checking.
31533244bcaaSab196087 			 */
31543244bcaaSab196087 			case DT_NEEDED:
31553244bcaaSab196087 			case DT_SONAME:
31563244bcaaSab196087 			case DT_FILTER:
31573244bcaaSab196087 			case DT_AUXILIARY:
31583244bcaaSab196087 			case DT_CONFIG:
31593244bcaaSab196087 			case DT_RPATH:
31603244bcaaSab196087 			case DT_RUNPATH:
31613244bcaaSab196087 			case DT_USED:
31623244bcaaSab196087 			case DT_DEPAUDIT:
31633244bcaaSab196087 			case DT_AUDIT:
31644f680cc6SAli Bahrami 				name = string(_cache, ndx, strsec,
31654f680cc6SAli Bahrami 				    file, dyn->d_un.d_ptr);
31664f680cc6SAli Bahrami 				break;
31674f680cc6SAli Bahrami 
31683244bcaaSab196087 			case DT_SUNW_AUXILIARY:
31693244bcaaSab196087 			case DT_SUNW_FILTER:
31704f680cc6SAli Bahrami 				if (osabi_solaris)
31715aefb655Srie 					name = string(_cache, ndx, strsec,
31725aefb655Srie 					    file, dyn->d_un.d_ptr);
31733244bcaaSab196087 				break;
31743244bcaaSab196087 
31753244bcaaSab196087 			case DT_FLAGS:
3176de777a60Sab196087 				name = conv_dyn_flag(dyn->d_un.d_val,
3177de777a60Sab196087 				    0, &c_buf.flag);
31783244bcaaSab196087 				break;
31793244bcaaSab196087 			case DT_FLAGS_1:
3180d29b2c44Sab196087 				name = conv_dyn_flag1(dyn->d_un.d_val, 0,
3181de777a60Sab196087 				    &c_buf.flag1);
31823244bcaaSab196087 				break;
31833244bcaaSab196087 			case DT_POSFLAG_1:
3184de777a60Sab196087 				name = conv_dyn_posflag1(dyn->d_un.d_val, 0,
3185de777a60Sab196087 				    &c_buf.posflag1);
31863244bcaaSab196087 				break;
31873244bcaaSab196087 			case DT_FEATURE_1:
3188de777a60Sab196087 				name = conv_dyn_feature1(dyn->d_un.d_val, 0,
3189de777a60Sab196087 				    &c_buf.feature1);
31903244bcaaSab196087 				break;
31913244bcaaSab196087 			case DT_DEPRECATED_SPARC_REGISTER:
31927c478bd9Sstevel@tonic-gate 				name = MSG_INTL(MSG_STR_DEPRECATED);
31933244bcaaSab196087 				break;
3194f959f76aSab196087 
3195ba2be530Sab196087 			case DT_SUNW_LDMACH:
31964f680cc6SAli Bahrami 				if (!osabi_solaris)
31974f680cc6SAli Bahrami 					break;
31984f680cc6SAli Bahrami 				name = conv_ehdr_mach((Half)dyn->d_un.d_val,
31994f680cc6SAli Bahrami 				    0, &c_buf.inv);
3200ba2be530Sab196087 				break;
3201ba2be530Sab196087 
3202f959f76aSab196087 			/*
3203f959f76aSab196087 			 * Cases below this point are strictly sanity checking,
3204f959f76aSab196087 			 * and do not generate a name string. The TEST_ macros
3205f959f76aSab196087 			 * are used to hide the boiler plate arguments neeeded
3206f959f76aSab196087 			 * by dyn_test().
3207f959f76aSab196087 			 */
3208f959f76aSab196087 #define	TEST_ADDR(_sh_type, _sec_field) \
3209f959f76aSab196087 				dyn_test(DYN_TEST_ADDR, _sh_type, \
32104f680cc6SAli Bahrami 				    sec._sec_field, dyn, dynsec_cnt, ehdr, \
32114f680cc6SAli Bahrami 				    osabi, file)
3212f959f76aSab196087 #define	TEST_SIZE(_sh_type, _sec_field) \
3213f959f76aSab196087 				dyn_test(DYN_TEST_SIZE, _sh_type, \
32144f680cc6SAli Bahrami 				    sec._sec_field, dyn, dynsec_cnt, ehdr, \
32154f680cc6SAli Bahrami 				    osabi, file)
3216f959f76aSab196087 #define	TEST_ENTSIZE(_sh_type, _sec_field) \
3217f959f76aSab196087 				dyn_test(DYN_TEST_ENTSIZE, _sh_type, \
32184f680cc6SAli Bahrami 				    sec._sec_field, dyn, dynsec_cnt, ehdr, \
32194f680cc6SAli Bahrami 				    osabi, file)
3220f959f76aSab196087 
3221f959f76aSab196087 			case DT_FINI:
32221d9df23bSab196087 				dyn_symtest(dyn, MSG_ORIG(MSG_SYM_FINI),
32231d9df23bSab196087 				    sec.symtab, sec.dynsym, sec.sunw_ldynsym,
322431dd2c84SAli Bahrami 				    sec.fini, cache, shnum, ehdr, osabi, file);
3225f959f76aSab196087 				TEST_ADDR(SHT_PROGBITS, fini);
32263244bcaaSab196087 				break;
3227f959f76aSab196087 
3228f959f76aSab196087 			case DT_FINI_ARRAY:
3229f959f76aSab196087 				TEST_ADDR(SHT_FINI_ARRAY, fini_array);
3230f959f76aSab196087 				break;
3231f959f76aSab196087 
3232f959f76aSab196087 			case DT_FINI_ARRAYSZ:
3233f959f76aSab196087 				TEST_SIZE(SHT_FINI_ARRAY, fini_array);
3234f959f76aSab196087 				break;
3235f959f76aSab196087 
3236f959f76aSab196087 			case DT_HASH:
3237f959f76aSab196087 				TEST_ADDR(SHT_HASH, hash);
3238f959f76aSab196087 				break;
3239f959f76aSab196087 
3240f959f76aSab196087 			case DT_INIT:
32411d9df23bSab196087 				dyn_symtest(dyn, MSG_ORIG(MSG_SYM_INIT),
32421d9df23bSab196087 				    sec.symtab, sec.dynsym, sec.sunw_ldynsym,
324331dd2c84SAli Bahrami 				    sec.init, cache, shnum, ehdr, osabi, file);
3244f959f76aSab196087 				TEST_ADDR(SHT_PROGBITS, init);
3245f959f76aSab196087 				break;
3246f959f76aSab196087 
3247f959f76aSab196087 			case DT_INIT_ARRAY:
3248f959f76aSab196087 				TEST_ADDR(SHT_INIT_ARRAY, init_array);
3249f959f76aSab196087 				break;
3250f959f76aSab196087 
3251f959f76aSab196087 			case DT_INIT_ARRAYSZ:
3252f959f76aSab196087 				TEST_SIZE(SHT_INIT_ARRAY, init_array);
3253f959f76aSab196087 				break;
3254f959f76aSab196087 
3255f959f76aSab196087 			case DT_MOVEENT:
3256f959f76aSab196087 				TEST_ENTSIZE(SHT_SUNW_move, sunw_move);
3257f959f76aSab196087 				break;
3258f959f76aSab196087 
3259f959f76aSab196087 			case DT_MOVESZ:
3260f959f76aSab196087 				TEST_SIZE(SHT_SUNW_move, sunw_move);
3261f959f76aSab196087 				break;
3262f959f76aSab196087 
3263f959f76aSab196087 			case DT_MOVETAB:
3264f959f76aSab196087 				TEST_ADDR(SHT_SUNW_move, sunw_move);
3265f959f76aSab196087 				break;
3266f959f76aSab196087 
3267f959f76aSab196087 			case DT_PREINIT_ARRAY:
3268f959f76aSab196087 				TEST_ADDR(SHT_PREINIT_ARRAY, preinit_array);
3269f959f76aSab196087 				break;
3270f959f76aSab196087 
3271f959f76aSab196087 			case DT_PREINIT_ARRAYSZ:
3272f959f76aSab196087 				TEST_SIZE(SHT_PREINIT_ARRAY, preinit_array);
3273f959f76aSab196087 				break;
3274f959f76aSab196087 
3275f959f76aSab196087 			case DT_REL:
3276f959f76aSab196087 				if (!dumped)
3277f959f76aSab196087 					TEST_ADDR(SHT_REL, rel);
3278f959f76aSab196087 				break;
3279f959f76aSab196087 
3280f959f76aSab196087 			case DT_RELENT:
3281f959f76aSab196087 				TEST_ENTSIZE(SHT_REL, rel);
3282f959f76aSab196087 				break;
3283f959f76aSab196087 
3284f959f76aSab196087 			case DT_RELA:
3285f959f76aSab196087 				if (!dumped)
3286f959f76aSab196087 					TEST_ADDR(SHT_RELA, rela);
3287f959f76aSab196087 				break;
3288f959f76aSab196087 
3289f959f76aSab196087 			case DT_RELAENT:
3290f959f76aSab196087 				TEST_ENTSIZE(SHT_RELA, rela);
3291f959f76aSab196087 				break;
3292f959f76aSab196087 
3293f959f76aSab196087 			case DT_STRTAB:
3294f959f76aSab196087 				TEST_ADDR(SHT_STRTAB, dynstr);
3295f959f76aSab196087 				break;
3296f959f76aSab196087 
3297f959f76aSab196087 			case DT_STRSZ:
3298f959f76aSab196087 				TEST_SIZE(SHT_STRTAB, dynstr);
3299f959f76aSab196087 				break;
3300f959f76aSab196087 
3301f959f76aSab196087 			case DT_SUNW_CAP:
33023228339cSAli Bahrami 				if (osabi_solaris)
3303f959f76aSab196087 					TEST_ADDR(SHT_SUNW_cap, sunw_cap);
3304f959f76aSab196087 				break;
3305f959f76aSab196087 
330608278a5eSRod Evans 			case DT_SUNW_CAPINFO:
33073228339cSAli Bahrami 				if (osabi_solaris)
33083228339cSAli Bahrami 					TEST_ADDR(SHT_SUNW_capinfo,
33093228339cSAli Bahrami 					    sunw_capinfo);
331008278a5eSRod Evans 				break;
331108278a5eSRod Evans 
331208278a5eSRod Evans 			case DT_SUNW_CAPCHAIN:
33133228339cSAli Bahrami 				if (osabi_solaris)
33143228339cSAli Bahrami 					TEST_ADDR(SHT_SUNW_capchain,
33153228339cSAli Bahrami 					    sunw_capchain);
331608278a5eSRod Evans 				break;
331708278a5eSRod Evans 
3318f959f76aSab196087 			case DT_SUNW_SYMTAB:
3319f959f76aSab196087 				TEST_ADDR(SHT_SUNW_LDYNSYM, sunw_ldynsym);
3320f959f76aSab196087 				break;
3321f959f76aSab196087 
3322f959f76aSab196087 			case DT_SYMENT:
3323f959f76aSab196087 				TEST_ENTSIZE(SHT_DYNSYM, dynsym);
3324f959f76aSab196087 				break;
3325f959f76aSab196087 
3326f959f76aSab196087 			case DT_SYMINENT:
3327f959f76aSab196087 				TEST_ENTSIZE(SHT_SUNW_syminfo, sunw_syminfo);
3328f959f76aSab196087 				break;
3329f959f76aSab196087 
3330f959f76aSab196087 			case DT_SYMINFO:
3331f959f76aSab196087 				TEST_ADDR(SHT_SUNW_syminfo, sunw_syminfo);
3332f959f76aSab196087 				break;
3333f959f76aSab196087 
3334f959f76aSab196087 			case DT_SYMINSZ:
3335f959f76aSab196087 				TEST_SIZE(SHT_SUNW_syminfo, sunw_syminfo);
3336f959f76aSab196087 				break;
3337f959f76aSab196087 
3338f959f76aSab196087 			case DT_SYMTAB:
3339f959f76aSab196087 				TEST_ADDR(SHT_DYNSYM, dynsym);
3340f959f76aSab196087 				break;
3341f959f76aSab196087 
3342f959f76aSab196087 			case DT_SUNW_SORTENT:
3343f959f76aSab196087 				/*
3344f959f76aSab196087 				 * This entry is related to both the symsort and
3345f959f76aSab196087 				 * tlssort sections.
3346f959f76aSab196087 				 */
33474f680cc6SAli Bahrami 				if (osabi_solaris) {
3348f959f76aSab196087 					int test_tls =
3349f959f76aSab196087 					    (sec.sunw_tlssort != NULL);
3350f959f76aSab196087 					int test_sym =
3351f959f76aSab196087 					    (sec.sunw_symsort != NULL) ||
3352f959f76aSab196087 					    !test_tls;
3353f959f76aSab196087 					if (test_sym)
3354f959f76aSab196087 						TEST_ENTSIZE(SHT_SUNW_symsort,
3355f959f76aSab196087 						    sunw_symsort);
3356f959f76aSab196087 					if (test_tls)
3357f959f76aSab196087 						TEST_ENTSIZE(SHT_SUNW_tlssort,
3358f959f76aSab196087 						    sunw_tlssort);
3359f959f76aSab196087 				}
3360f959f76aSab196087 				break;
3361f959f76aSab196087 
3362f959f76aSab196087 
3363f959f76aSab196087 			case DT_SUNW_SYMSORT:
33644f680cc6SAli Bahrami 				if (osabi_solaris)
33654f680cc6SAli Bahrami 					TEST_ADDR(SHT_SUNW_symsort,
33664f680cc6SAli Bahrami 					    sunw_symsort);
3367f959f76aSab196087 				break;
3368f959f76aSab196087 
3369f959f76aSab196087 			case DT_SUNW_SYMSORTSZ:
33704f680cc6SAli Bahrami 				if (osabi_solaris)
33714f680cc6SAli Bahrami 					TEST_SIZE(SHT_SUNW_symsort,
33724f680cc6SAli Bahrami 					    sunw_symsort);
3373f959f76aSab196087 				break;
3374f959f76aSab196087 
3375f959f76aSab196087 			case DT_SUNW_TLSSORT:
33764f680cc6SAli Bahrami 				if (osabi_solaris)
33774f680cc6SAli Bahrami 					TEST_ADDR(SHT_SUNW_tlssort,
33784f680cc6SAli Bahrami 					    sunw_tlssort);
3379f959f76aSab196087 				break;
3380f959f76aSab196087 
3381f959f76aSab196087 			case DT_SUNW_TLSSORTSZ:
33824f680cc6SAli Bahrami 				if (osabi_solaris)
33834f680cc6SAli Bahrami 					TEST_SIZE(SHT_SUNW_tlssort,
33844f680cc6SAli Bahrami 					    sunw_tlssort);
3385f959f76aSab196087 				break;
3386f959f76aSab196087 
3387f959f76aSab196087 			case DT_VERDEF:
3388f959f76aSab196087 				TEST_ADDR(SHT_SUNW_verdef, sunw_verdef);
3389f959f76aSab196087 				break;
3390f959f76aSab196087 
3391f959f76aSab196087 			case DT_VERNEED:
3392f959f76aSab196087 				TEST_ADDR(SHT_SUNW_verneed, sunw_verneed);
3393f959f76aSab196087 				break;
3394f959f76aSab196087 
3395f959f76aSab196087 			case DT_VERSYM:
3396f959f76aSab196087 				TEST_ADDR(SHT_SUNW_versym, sunw_versym);
3397f959f76aSab196087 				break;
3398f959f76aSab196087 #undef TEST_ADDR
3399f959f76aSab196087 #undef TEST_SIZE
3400f959f76aSab196087 #undef TEST_ENTSIZE
34013244bcaaSab196087 			}
34027c478bd9Sstevel@tonic-gate 
3403f959f76aSab196087 			if (name == NULL)
3404f959f76aSab196087 				name = MSG_ORIG(MSG_STR_EMPTY);
34054f680cc6SAli Bahrami 			Elf_dyn_entry(0, dyn, ndx, name,
34064f680cc6SAli Bahrami 			    osabi, ehdr->e_machine);
34077c478bd9Sstevel@tonic-gate 		}
34087c478bd9Sstevel@tonic-gate 	}
34097c478bd9Sstevel@tonic-gate }
34107c478bd9Sstevel@tonic-gate 
34117c478bd9Sstevel@tonic-gate /*
34127c478bd9Sstevel@tonic-gate  * Search for and process a MOVE section.
34137c478bd9Sstevel@tonic-gate  */
34147c478bd9Sstevel@tonic-gate static void
move(Cache * cache,Word shnum,const char * file,uint_t flags)3415c809c407Sab196087 move(Cache *cache, Word shnum, const char *file, uint_t flags)
34167c478bd9Sstevel@tonic-gate {
34175aefb655Srie 	Word		cnt;
34187e16fca0SAli Bahrami 	const char	*fmt = NULL;
34197c478bd9Sstevel@tonic-gate 
34207c478bd9Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
34215aefb655Srie 		Word	movenum, symnum, ndx;
34225aefb655Srie 		Sym	*syms;
34237c478bd9Sstevel@tonic-gate 		Cache	*_cache = &cache[cnt];
34245aefb655Srie 		Shdr	*shdr = _cache->c_shdr;
34255aefb655Srie 		Cache	*symsec, *strsec;
34265aefb655Srie 		Move	*move;
34277c478bd9Sstevel@tonic-gate 
34287c478bd9Sstevel@tonic-gate 		if (shdr->sh_type != SHT_SUNW_move)
34297c478bd9Sstevel@tonic-gate 			continue;
3430981a172dSab196087 		if (!match(MATCH_F_ALL, _cache->c_name, cnt, shdr->sh_type))
34317c478bd9Sstevel@tonic-gate 			continue;
34327c478bd9Sstevel@tonic-gate 
34337c478bd9Sstevel@tonic-gate 		/*
34347c478bd9Sstevel@tonic-gate 		 * Determine the move data and number.
34357c478bd9Sstevel@tonic-gate 		 */
34367c478bd9Sstevel@tonic-gate 		if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
34377c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
34387c478bd9Sstevel@tonic-gate 			    file, _cache->c_name);
34397c478bd9Sstevel@tonic-gate 			continue;
34407c478bd9Sstevel@tonic-gate 		}
34419a411307Srie 		if (_cache->c_data == NULL)
34429a411307Srie 			continue;
34439a411307Srie 
34445aefb655Srie 		move = (Move *)_cache->c_data->d_buf;
34455aefb655Srie 		movenum = shdr->sh_size / shdr->sh_entsize;
34467c478bd9Sstevel@tonic-gate 
34477c478bd9Sstevel@tonic-gate 		/*
34485aefb655Srie 		 * Get the data buffer for the associated symbol table and
34495aefb655Srie 		 * string table.
34507c478bd9Sstevel@tonic-gate 		 */
34515aefb655Srie 		if (stringtbl(cache, 1, cnt, shnum, file,
34525aefb655Srie 		    &symnum, &symsec, &strsec) == 0)
34535aefb655Srie 			return;
34547c478bd9Sstevel@tonic-gate 
34555aefb655Srie 		syms = (Sym *)symsec->c_data->d_buf;
34567c478bd9Sstevel@tonic-gate 
34575aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
34585aefb655Srie 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_MOVE), _cache->c_name);
34595aefb655Srie 		dbg_print(0, MSG_INTL(MSG_MOVE_TITLE));
34607c478bd9Sstevel@tonic-gate 
34617e16fca0SAli Bahrami 		if (fmt == NULL)
34625aefb655Srie 			fmt = MSG_INTL(MSG_MOVE_ENTRY);
34637c478bd9Sstevel@tonic-gate 
34645aefb655Srie 		for (ndx = 0; ndx < movenum; move++, ndx++) {
34655aefb655Srie 			const char	*symname;
34665aefb655Srie 			char		index[MAXNDXSIZE], section[BUFSIZ];
34675aefb655Srie 			Word		symndx, shndx;
34685aefb655Srie 			Sym		*sym;
34697c478bd9Sstevel@tonic-gate 
34707c478bd9Sstevel@tonic-gate 			/*
34717c478bd9Sstevel@tonic-gate 			 * Check for null entries
34727c478bd9Sstevel@tonic-gate 			 */
34735aefb655Srie 			if ((move->m_info == 0) && (move->m_value == 0) &&
34745aefb655Srie 			    (move->m_poffset == 0) && (move->m_repeat == 0) &&
34755aefb655Srie 			    (move->m_stride == 0)) {
34765aefb655Srie 				dbg_print(0, fmt, MSG_ORIG(MSG_STR_EMPTY),
34775aefb655Srie 				    EC_XWORD(move->m_poffset), 0, 0, 0,
34785aefb655Srie 				    EC_LWORD(0), MSG_ORIG(MSG_STR_EMPTY));
34797c478bd9Sstevel@tonic-gate 				continue;
34807c478bd9Sstevel@tonic-gate 			}
34815aefb655Srie 			if (((symndx = ELF_M_SYM(move->m_info)) == 0) ||
34825aefb655Srie 			    (symndx >= symnum)) {
34837c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
34847c478bd9Sstevel@tonic-gate 				    MSG_INTL(MSG_ERR_BADMINFO), file,
34855aefb655Srie 				    _cache->c_name, EC_XWORD(move->m_info));
34865aefb655Srie 
34875aefb655Srie 				(void) snprintf(index, MAXNDXSIZE,
34885aefb655Srie 				    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(symndx));
34895aefb655Srie 				dbg_print(0, fmt, index,
34905aefb655Srie 				    EC_XWORD(move->m_poffset),
34915aefb655Srie 				    ELF_M_SIZE(move->m_info), move->m_repeat,
34925aefb655Srie 				    move->m_stride, move->m_value,
34937c478bd9Sstevel@tonic-gate 				    MSG_INTL(MSG_STR_UNKNOWN));
34947c478bd9Sstevel@tonic-gate 				continue;
34957c478bd9Sstevel@tonic-gate 			}
34967c478bd9Sstevel@tonic-gate 
34975aefb655Srie 			symname = relsymname(cache, _cache, strsec,
34980e233487SRod Evans 			    symndx, symnum, ndx, syms, section, BUFSIZ, file);
34995aefb655Srie 			sym = (Sym *)(syms + symndx);
35007c478bd9Sstevel@tonic-gate 
35017c478bd9Sstevel@tonic-gate 			/*
35027c478bd9Sstevel@tonic-gate 			 * Additional sanity check.
35037c478bd9Sstevel@tonic-gate 			 */
35045aefb655Srie 			shndx = sym->st_shndx;
35057c478bd9Sstevel@tonic-gate 			if (!((shndx == SHN_COMMON) ||
35067c478bd9Sstevel@tonic-gate 			    (((shndx >= 1) && (shndx <= shnum)) &&
35075aefb655Srie 			    (cache[shndx].c_shdr)->sh_type == SHT_NOBITS))) {
35087c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
35097c478bd9Sstevel@tonic-gate 				    MSG_INTL(MSG_ERR_BADSYM2), file,
3510ba2be530Sab196087 				    _cache->c_name, EC_WORD(symndx),
3511ba2be530Sab196087 				    demangle(symname, flags));
35127c478bd9Sstevel@tonic-gate 			}
35137c478bd9Sstevel@tonic-gate 
35145aefb655Srie 			(void) snprintf(index, MAXNDXSIZE,
35155aefb655Srie 			    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(symndx));
35165aefb655Srie 			dbg_print(0, fmt, index, EC_XWORD(move->m_poffset),
35175aefb655Srie 			    ELF_M_SIZE(move->m_info), move->m_repeat,
35185aefb655Srie 			    move->m_stride, move->m_value,
35195aefb655Srie 			    demangle(symname, flags));
35207c478bd9Sstevel@tonic-gate 		}
35217c478bd9Sstevel@tonic-gate 	}
35227c478bd9Sstevel@tonic-gate }
35237c478bd9Sstevel@tonic-gate 
35247c478bd9Sstevel@tonic-gate /*
35254f680cc6SAli Bahrami  * parse_note_t is used to track the state used by parse_note_entry()
35264f680cc6SAli Bahrami  * between calls, and also to return the results of each call.
35274f680cc6SAli Bahrami  */
35284f680cc6SAli Bahrami typedef struct {
35294f680cc6SAli Bahrami 	/* pns_ fields track progress through the data */
35304f680cc6SAli Bahrami 	const char	*pns_file;	/* File name */
35314f680cc6SAli Bahrami 	Cache		*pns_cache;	/* Note section cache entry */
35324f680cc6SAli Bahrami 	size_t		pns_size;	/* # unprocessed data bytes */
35334f680cc6SAli Bahrami 	Word		*pns_data;	/* # to next unused data byte */
35344f680cc6SAli Bahrami 
35354f680cc6SAli Bahrami 	/* pn_ fields return the results for a single call */
35364f680cc6SAli Bahrami 	Word		pn_namesz;	/* Value of note namesz field */
35374f680cc6SAli Bahrami 	Word		pn_descsz;	/* Value of note descsz field */
35384f680cc6SAli Bahrami 	Word		pn_type;	/* Value of note type field */
35394f680cc6SAli Bahrami 	const char	*pn_name;	/* if (namesz > 0) ptr to name bytes */
35404f680cc6SAli Bahrami 	const char	*pn_desc;	/* if (descsx > 0) ptr to data bytes */
35414f680cc6SAli Bahrami } parse_note_t;
35424f680cc6SAli Bahrami 
35434f680cc6SAli Bahrami /*
35444f680cc6SAli Bahrami  * Extract the various sub-parts of a note entry, and advance the
35454f680cc6SAli Bahrami  * data pointer past it.
35464f680cc6SAli Bahrami  *
35474f680cc6SAli Bahrami  * entry:
35484f680cc6SAli Bahrami  *	The state pns_ fields contain current values for the Note section
35494f680cc6SAli Bahrami  *
35504f680cc6SAli Bahrami  * exit:
35514f680cc6SAli Bahrami  *	On success, True (1) is returned, the state pns_ fields have been
35524f680cc6SAli Bahrami  *	advanced to point at the start of the next entry, and the information
35534f680cc6SAli Bahrami  *	for the recovered note entry is found in the state pn_ fields.
35544f680cc6SAli Bahrami  *
35554f680cc6SAli Bahrami  *	On failure, False (0) is returned. The values contained in state
35564f680cc6SAli Bahrami  *	are undefined.
35574f680cc6SAli Bahrami  */
35584f680cc6SAli Bahrami static int
parse_note_entry(parse_note_t * state)35594f680cc6SAli Bahrami parse_note_entry(parse_note_t *state)
35604f680cc6SAli Bahrami {
35614f680cc6SAli Bahrami 	size_t	pad, noteoff;
35624f680cc6SAli Bahrami 
35634f680cc6SAli Bahrami 	noteoff = (Word)state->pns_cache->c_data->d_size - state->pns_size;
35644f680cc6SAli Bahrami 	/*
35654f680cc6SAli Bahrami 	 * Make sure we can at least reference the 3 initial entries
35664f680cc6SAli Bahrami 	 * (4-byte words) of the note information block.
35674f680cc6SAli Bahrami 	 */
35684f680cc6SAli Bahrami 	if (state->pns_size >= (sizeof (Word) * 3)) {
35694f680cc6SAli Bahrami 		state->pns_size -= (sizeof (Word) * 3);
35704f680cc6SAli Bahrami 	} else {
35714f680cc6SAli Bahrami 		(void) fprintf(stderr, MSG_INTL(MSG_NOTE_BADDATASZ),
35724f680cc6SAli Bahrami 		    state->pns_file, state->pns_cache->c_name,
35734f680cc6SAli Bahrami 		    EC_WORD(noteoff));
35744f680cc6SAli Bahrami 		return (0);
35754f680cc6SAli Bahrami 	}
35764f680cc6SAli Bahrami 
35774f680cc6SAli Bahrami 	/*
35784f680cc6SAli Bahrami 	 * Make sure any specified name string can be referenced.
35794f680cc6SAli Bahrami 	 */
35804f680cc6SAli Bahrami 	if ((state->pn_namesz = *state->pns_data++) != 0) {
35814f680cc6SAli Bahrami 		if (state->pns_size >= state->pn_namesz) {
35824f680cc6SAli Bahrami 			state->pns_size -= state->pn_namesz;
35834f680cc6SAli Bahrami 		} else {
35844f680cc6SAli Bahrami 			(void) fprintf(stderr, MSG_INTL(MSG_NOTE_BADNMSZ),
35854f680cc6SAli Bahrami 			    state->pns_file, state->pns_cache->c_name,
35864f680cc6SAli Bahrami 			    EC_WORD(noteoff), EC_WORD(state->pn_namesz));
35874f680cc6SAli Bahrami 			return (0);
35884f680cc6SAli Bahrami 		}
35894f680cc6SAli Bahrami 	}
35904f680cc6SAli Bahrami 
35914f680cc6SAli Bahrami 	/*
35924f680cc6SAli Bahrami 	 * Make sure any specified descriptor can be referenced.
35934f680cc6SAli Bahrami 	 */
35944f680cc6SAli Bahrami 	if ((state->pn_descsz = *state->pns_data++) != 0) {
35954f680cc6SAli Bahrami 		/*
35964f680cc6SAli Bahrami 		 * If namesz isn't a 4-byte multiple, account for any
35974f680cc6SAli Bahrami 		 * padding that must exist before the descriptor.
35984f680cc6SAli Bahrami 		 */
35994f680cc6SAli Bahrami 		if ((pad = (state->pn_namesz & (sizeof (Word) - 1))) != 0) {
36004f680cc6SAli Bahrami 			pad = sizeof (Word) - pad;
36014f680cc6SAli Bahrami 			state->pns_size -= pad;
36024f680cc6SAli Bahrami 		}
36034f680cc6SAli Bahrami 		if (state->pns_size >= state->pn_descsz) {
36044f680cc6SAli Bahrami 			state->pns_size -= state->pn_descsz;
36054f680cc6SAli Bahrami 		} else {
36064f680cc6SAli Bahrami 			(void) fprintf(stderr, MSG_INTL(MSG_NOTE_BADDESZ),
36074f680cc6SAli Bahrami 			    state->pns_file, state->pns_cache->c_name,
36084f680cc6SAli Bahrami 			    EC_WORD(noteoff), EC_WORD(state->pn_namesz));
36094f680cc6SAli Bahrami 			return (0);
36104f680cc6SAli Bahrami 		}
36114f680cc6SAli Bahrami 	}
36124f680cc6SAli Bahrami 
36134f680cc6SAli Bahrami 	state->pn_type = *state->pns_data++;
36144f680cc6SAli Bahrami 
36154f680cc6SAli Bahrami 	/* Name */
36164f680cc6SAli Bahrami 	if (state->pn_namesz) {
36174f680cc6SAli Bahrami 		state->pn_name = (char *)state->pns_data;
36184f680cc6SAli Bahrami 		pad = (state->pn_namesz +
36194f680cc6SAli Bahrami 		    (sizeof (Word) - 1)) & ~(sizeof (Word) - 1);
36204f680cc6SAli Bahrami 		/* LINTED */
36214f680cc6SAli Bahrami 		state->pns_data = (Word *)(state->pn_name + pad);
36224f680cc6SAli Bahrami 	}
36234f680cc6SAli Bahrami 
36244f680cc6SAli Bahrami 	/*
36254f680cc6SAli Bahrami 	 * If multiple information blocks exist within a .note section
36264f680cc6SAli Bahrami 	 * account for any padding that must exist before the next
36274f680cc6SAli Bahrami 	 * information block.
36284f680cc6SAli Bahrami 	 */
36294f680cc6SAli Bahrami 	if ((pad = (state->pn_descsz & (sizeof (Word) - 1))) != 0) {
36304f680cc6SAli Bahrami 		pad = sizeof (Word) - pad;
36314f680cc6SAli Bahrami 		if (state->pns_size > pad)
36324f680cc6SAli Bahrami 			state->pns_size -= pad;
36334f680cc6SAli Bahrami 	}
36344f680cc6SAli Bahrami 
36354f680cc6SAli Bahrami 	/* Data */
36364f680cc6SAli Bahrami 	if (state->pn_descsz) {
36374f680cc6SAli Bahrami 		state->pn_desc = (const char *)state->pns_data;
36384f680cc6SAli Bahrami 		/* LINTED */
36394f680cc6SAli Bahrami 		state->pns_data = (Word *)(state->pn_desc +
36404f680cc6SAli Bahrami 		    state->pn_descsz + pad);
36414f680cc6SAli Bahrami 	}
36424f680cc6SAli Bahrami 
36434f680cc6SAli Bahrami 	return (1);
36444f680cc6SAli Bahrami }
36454f680cc6SAli Bahrami 
36464f680cc6SAli Bahrami /*
3647c6c9aed4Sab196087  * Callback function for use with conv_str_to_c_literal() below.
3648c6c9aed4Sab196087  */
3649c6c9aed4Sab196087 /*ARGSUSED2*/
3650c6c9aed4Sab196087 static void
c_literal_cb(const void * ptr,size_t size,void * uvalue)3651c6c9aed4Sab196087 c_literal_cb(const void *ptr, size_t size, void *uvalue)
3652c6c9aed4Sab196087 {
3653c6c9aed4Sab196087 	(void) fwrite(ptr, size, 1, stdout);
3654c6c9aed4Sab196087 }
3655c6c9aed4Sab196087 
3656c6c9aed4Sab196087 /*
36577c478bd9Sstevel@tonic-gate  * Traverse a note section analyzing each note information block.
36587c478bd9Sstevel@tonic-gate  * The data buffers size is used to validate references before they are made,
36597c478bd9Sstevel@tonic-gate  * and is decremented as each element is processed.
36607c478bd9Sstevel@tonic-gate  */
36617c478bd9Sstevel@tonic-gate void
note_entry(Cache * cache,Word * data,size_t size,Ehdr * ehdr,const char * file)3662c6c9aed4Sab196087 note_entry(Cache *cache, Word *data, size_t size, Ehdr *ehdr, const char *file)
36637c478bd9Sstevel@tonic-gate {
3664c6c9aed4Sab196087 	int		cnt = 0;
3665c6c9aed4Sab196087 	int		is_corenote;
3666c6c9aed4Sab196087 	int		do_swap;
3667c6c9aed4Sab196087 	Conv_inv_buf_t	inv_buf;
36684f680cc6SAli Bahrami 	parse_note_t	pnstate;
3669c6c9aed4Sab196087 
36704f680cc6SAli Bahrami 	pnstate.pns_file = file;
36714f680cc6SAli Bahrami 	pnstate.pns_cache = cache;
36724f680cc6SAli Bahrami 	pnstate.pns_size = size;
36734f680cc6SAli Bahrami 	pnstate.pns_data = data;
3674c6c9aed4Sab196087 	do_swap = _elf_sys_encoding() != ehdr->e_ident[EI_DATA];
36755aefb655Srie 
36767c478bd9Sstevel@tonic-gate 	/*
36777c478bd9Sstevel@tonic-gate 	 * Print out a single `note' information block.
36787c478bd9Sstevel@tonic-gate 	 */
36794f680cc6SAli Bahrami 	while (pnstate.pns_size > 0) {
36807c478bd9Sstevel@tonic-gate 
36814f680cc6SAli Bahrami 		if (parse_note_entry(&pnstate) == 0)
36827c478bd9Sstevel@tonic-gate 			return;
36837c478bd9Sstevel@tonic-gate 
3684c6c9aed4Sab196087 		/*
3685c6c9aed4Sab196087 		 * Is this a Solaris core note? Such notes all have
3686c6c9aed4Sab196087 		 * the name "CORE".
3687c6c9aed4Sab196087 		 */
3688c6c9aed4Sab196087 		is_corenote = (ehdr->e_type == ET_CORE) &&
36894f680cc6SAli Bahrami 		    (pnstate.pn_namesz == (MSG_STR_CORE_SIZE + 1)) &&
36904f680cc6SAli Bahrami 		    (strncmp(MSG_ORIG(MSG_STR_CORE), pnstate.pn_name,
3691c6c9aed4Sab196087 		    MSG_STR_CORE_SIZE + 1) == 0);
36927c478bd9Sstevel@tonic-gate 
3693c6c9aed4Sab196087 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
3694c6c9aed4Sab196087 		dbg_print(0, MSG_INTL(MSG_FMT_NOTEENTNDX), EC_WORD(cnt));
3695c6c9aed4Sab196087 		cnt++;
36964f680cc6SAli Bahrami 		dbg_print(0, MSG_ORIG(MSG_NOTE_NAMESZ),
36974f680cc6SAli Bahrami 		    EC_WORD(pnstate.pn_namesz));
36984f680cc6SAli Bahrami 		dbg_print(0, MSG_ORIG(MSG_NOTE_DESCSZ),
36994f680cc6SAli Bahrami 		    EC_WORD(pnstate.pn_descsz));
3700c6c9aed4Sab196087 
3701c6c9aed4Sab196087 		if (is_corenote)
3702c6c9aed4Sab196087 			dbg_print(0, MSG_ORIG(MSG_NOTE_TYPE_STR),
37034f680cc6SAli Bahrami 			    conv_cnote_type(pnstate.pn_type, 0, &inv_buf));
3704c6c9aed4Sab196087 		else
37054f680cc6SAli Bahrami 			dbg_print(0, MSG_ORIG(MSG_NOTE_TYPE),
37064f680cc6SAli Bahrami 			    EC_WORD(pnstate.pn_type));
37074f680cc6SAli Bahrami 		if (pnstate.pn_namesz) {
3708c6c9aed4Sab196087 			dbg_print(0, MSG_ORIG(MSG_NOTE_NAME));
37097c478bd9Sstevel@tonic-gate 			/*
3710c6c9aed4Sab196087 			 * The name string can contain embedded 'null'
3711c6c9aed4Sab196087 			 * bytes and/or unprintable characters. Also,
3712c6c9aed4Sab196087 			 * the final NULL is documented in the ELF ABI
3713c6c9aed4Sab196087 			 * as being included in the namesz. So, display
3714c6c9aed4Sab196087 			 * the name using C literal string notation, and
3715c6c9aed4Sab196087 			 * include the terminating NULL in the output.
3716c6c9aed4Sab196087 			 * We don't show surrounding double quotes, as
3717c6c9aed4Sab196087 			 * that implies the termination that we are showing
3718c6c9aed4Sab196087 			 * explicitly.
37197c478bd9Sstevel@tonic-gate 			 */
3720c6c9aed4Sab196087 			(void) fwrite(MSG_ORIG(MSG_STR_8SP),
3721c6c9aed4Sab196087 			    MSG_STR_8SP_SIZE, 1, stdout);
37224f680cc6SAli Bahrami 			conv_str_to_c_literal(pnstate.pn_name,
37234f680cc6SAli Bahrami 			    pnstate.pn_namesz, c_literal_cb, NULL);
37245aefb655Srie 			dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
37257c478bd9Sstevel@tonic-gate 		}
37267c478bd9Sstevel@tonic-gate 
37274f680cc6SAli Bahrami 		if (pnstate.pn_descsz) {
3728c6c9aed4Sab196087 			int		hexdump = 1;
37297c478bd9Sstevel@tonic-gate 
37307c478bd9Sstevel@tonic-gate 			/*
3731c6c9aed4Sab196087 			 * If this is a core note, let the corenote()
3732c6c9aed4Sab196087 			 * function handle it.
37337c478bd9Sstevel@tonic-gate 			 */
3734c6c9aed4Sab196087 			if (is_corenote) {
3735c6c9aed4Sab196087 				/* We only issue the bad arch error once */
3736c6c9aed4Sab196087 				static int	badnote_done = 0;
3737c6c9aed4Sab196087 				corenote_ret_t	corenote_ret;
37387c478bd9Sstevel@tonic-gate 
3739c6c9aed4Sab196087 				corenote_ret = corenote(ehdr->e_machine,
37404f680cc6SAli Bahrami 				    do_swap, pnstate.pn_type, pnstate.pn_desc,
37414f680cc6SAli Bahrami 				    pnstate.pn_descsz);
3742c6c9aed4Sab196087 				switch (corenote_ret) {
3743*37915d86SRichard Lowe 				case CORENOTE_R_OK_DUMP:
3744*37915d86SRichard Lowe 					hexdump = 1;
3745*37915d86SRichard Lowe 					break;
3746c6c9aed4Sab196087 				case CORENOTE_R_OK:
3747c6c9aed4Sab196087 					hexdump = 0;
3748c6c9aed4Sab196087 					break;
3749c6c9aed4Sab196087 				case CORENOTE_R_BADDATA:
3750c6c9aed4Sab196087 					(void) fprintf(stderr,
3751c6c9aed4Sab196087 					    MSG_INTL(MSG_NOTE_BADCOREDATA),
3752c6c9aed4Sab196087 					    file);
3753c6c9aed4Sab196087 					break;
3754c6c9aed4Sab196087 				case CORENOTE_R_BADARCH:
3755c6c9aed4Sab196087 					if (badnote_done)
3756c6c9aed4Sab196087 						break;
3757c6c9aed4Sab196087 					(void) fprintf(stderr,
3758c6c9aed4Sab196087 					    MSG_INTL(MSG_NOTE_BADCOREARCH),
3759c6c9aed4Sab196087 					    file,
3760c6c9aed4Sab196087 					    conv_ehdr_mach(ehdr->e_machine,
3761c6c9aed4Sab196087 					    0, &inv_buf));
3762c6c9aed4Sab196087 					break;
3763*37915d86SRichard Lowe 				case CORENOTE_R_BADTYPE:
3764*37915d86SRichard Lowe 					(void) fprintf(stderr,
3765*37915d86SRichard Lowe 					    MSG_INTL(MSG_NOTE_BADCORETYPE),
3766*37915d86SRichard Lowe 					    file,
3767*37915d86SRichard Lowe 					    EC_WORD(pnstate.pn_type));
3768*37915d86SRichard Lowe 					break;
3769*37915d86SRichard Lowe 
37707c478bd9Sstevel@tonic-gate 				}
37717c478bd9Sstevel@tonic-gate 			}
37727c478bd9Sstevel@tonic-gate 
3773c6c9aed4Sab196087 			/*
3774c6c9aed4Sab196087 			 * The default thing when we don't understand
3775c6c9aed4Sab196087 			 * the note data is to display it as hex bytes.
3776c6c9aed4Sab196087 			 */
3777c6c9aed4Sab196087 			if (hexdump) {
3778c6c9aed4Sab196087 				dbg_print(0, MSG_ORIG(MSG_NOTE_DESC));
37794f680cc6SAli Bahrami 				dump_hex_bytes(pnstate.pn_desc,
37804f680cc6SAli Bahrami 				    pnstate.pn_descsz, 8, 4, 4);
3781c6c9aed4Sab196087 			}
37827c478bd9Sstevel@tonic-gate 		}
37837c478bd9Sstevel@tonic-gate 	}
37847c478bd9Sstevel@tonic-gate }
37857c478bd9Sstevel@tonic-gate 
37867c478bd9Sstevel@tonic-gate /*
3787c6c9aed4Sab196087  * Search for and process .note sections.
3788c6c9aed4Sab196087  *
3789c6c9aed4Sab196087  * Returns the number of note sections seen.
37907c478bd9Sstevel@tonic-gate  */
3791c6c9aed4Sab196087 static Word
note(Cache * cache,Word shnum,Ehdr * ehdr,const char * file)3792c6c9aed4Sab196087 note(Cache *cache, Word shnum, Ehdr *ehdr, const char *file)
37937c478bd9Sstevel@tonic-gate {
3794c6c9aed4Sab196087 	Word	cnt, note_cnt = 0;
37957c478bd9Sstevel@tonic-gate 
37967c478bd9Sstevel@tonic-gate 	/*
37977c478bd9Sstevel@tonic-gate 	 * Otherwise look for any .note sections.
37987c478bd9Sstevel@tonic-gate 	 */
37997c478bd9Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
38007c478bd9Sstevel@tonic-gate 		Cache	*_cache = &cache[cnt];
38015aefb655Srie 		Shdr	*shdr = _cache->c_shdr;
38027c478bd9Sstevel@tonic-gate 
38037c478bd9Sstevel@tonic-gate 		if (shdr->sh_type != SHT_NOTE)
38047c478bd9Sstevel@tonic-gate 			continue;
3805c6c9aed4Sab196087 		note_cnt++;
3806981a172dSab196087 		if (!match(MATCH_F_ALL, _cache->c_name, cnt, shdr->sh_type))
38077c478bd9Sstevel@tonic-gate 			continue;
38087c478bd9Sstevel@tonic-gate 
38097c478bd9Sstevel@tonic-gate 		/*
38107c478bd9Sstevel@tonic-gate 		 * As these sections are often hand rolled, make sure they're
3811f959f76aSab196087 		 * properly aligned before proceeding, and issue an error
3812f959f76aSab196087 		 * as necessary.
3813f959f76aSab196087 		 *
3814f959f76aSab196087 		 * Note that we will continue on to display the note even
3815f959f76aSab196087 		 * if it has bad alignment. We can do this safely, because
3816f959f76aSab196087 		 * libelf knows the alignment required for SHT_NOTE, and
3817f959f76aSab196087 		 * takes steps to deliver a properly aligned buffer to us
3818f959f76aSab196087 		 * even if the actual file is misaligned.
38197c478bd9Sstevel@tonic-gate 		 */
3820f959f76aSab196087 		if (shdr->sh_offset & (sizeof (Word) - 1))
38217c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADALIGN),
38227c478bd9Sstevel@tonic-gate 			    file, _cache->c_name);
3823f959f76aSab196087 
38249a411307Srie 		if (_cache->c_data == NULL)
38259a411307Srie 			continue;
38267c478bd9Sstevel@tonic-gate 
38275aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
38285aefb655Srie 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_NOTE), _cache->c_name);
38297c478bd9Sstevel@tonic-gate 		note_entry(_cache, (Word *)_cache->c_data->d_buf,
38307c478bd9Sstevel@tonic-gate 		/* LINTED */
3831c6c9aed4Sab196087 		    (Word)_cache->c_data->d_size, ehdr, file);
38327c478bd9Sstevel@tonic-gate 	}
3833c6c9aed4Sab196087 
3834c6c9aed4Sab196087 	return (note_cnt);
38357c478bd9Sstevel@tonic-gate }
38367c478bd9Sstevel@tonic-gate 
38375aefb655Srie /*
38384f680cc6SAli Bahrami  * The Linux Standard Base defines a special note named .note.ABI-tag
38394f680cc6SAli Bahrami  * that is used to maintain Linux ABI information. Presence of this section
38404f680cc6SAli Bahrami  * is a strong indication that the object should be considered to be
38414f680cc6SAli Bahrami  * ELFOSABI_LINUX.
38424f680cc6SAli Bahrami  *
38434f680cc6SAli Bahrami  * This function returns True (1) if such a note is seen, and False (0)
38444f680cc6SAli Bahrami  * otherwise.
38454f680cc6SAli Bahrami  */
38464f680cc6SAli Bahrami static int
has_linux_abi_note(Cache * cache,Word shnum,const char * file)38474f680cc6SAli Bahrami has_linux_abi_note(Cache *cache, Word shnum, const char *file)
38484f680cc6SAli Bahrami {
38494f680cc6SAli Bahrami 	Word	cnt;
38504f680cc6SAli Bahrami 
38514f680cc6SAli Bahrami 	for (cnt = 1; cnt < shnum; cnt++) {
38524f680cc6SAli Bahrami 		parse_note_t	pnstate;
38534f680cc6SAli Bahrami 		Cache		*_cache = &cache[cnt];
38544f680cc6SAli Bahrami 		Shdr		*shdr = _cache->c_shdr;
38554f680cc6SAli Bahrami 
38564f680cc6SAli Bahrami 		/*
38574f680cc6SAli Bahrami 		 * Section must be SHT_NOTE, must have the name
38584f680cc6SAli Bahrami 		 * .note.ABI-tag, and must have data.
38594f680cc6SAli Bahrami 		 */
38604f680cc6SAli Bahrami 		if ((shdr->sh_type != SHT_NOTE) ||
38614f680cc6SAli Bahrami 		    (strcmp(MSG_ORIG(MSG_STR_NOTEABITAG),
38624f680cc6SAli Bahrami 		    _cache->c_name) != 0) || (_cache->c_data == NULL))
38634f680cc6SAli Bahrami 			continue;
38644f680cc6SAli Bahrami 
38654f680cc6SAli Bahrami 		pnstate.pns_file = file;
38664f680cc6SAli Bahrami 		pnstate.pns_cache = _cache;
38674f680cc6SAli Bahrami 		pnstate.pns_size = _cache->c_data->d_size;
38684f680cc6SAli Bahrami 		pnstate.pns_data = (Word *)_cache->c_data->d_buf;
38694f680cc6SAli Bahrami 
38704f680cc6SAli Bahrami 		while (pnstate.pns_size > 0) {
38714f680cc6SAli Bahrami 			Word *w;
38724f680cc6SAli Bahrami 
38734f680cc6SAli Bahrami 			if (parse_note_entry(&pnstate) == 0)
38744f680cc6SAli Bahrami 				break;
38754f680cc6SAli Bahrami 
38764f680cc6SAli Bahrami 			/*
38774f680cc6SAli Bahrami 			 * The type must be 1, and the name must be "GNU".
38784f680cc6SAli Bahrami 			 * The descsz must be at least 16 bytes.
38794f680cc6SAli Bahrami 			 */
38804f680cc6SAli Bahrami 			if ((pnstate.pn_type != 1) ||
38814f680cc6SAli Bahrami 			    (pnstate.pn_namesz != (MSG_STR_GNU_SIZE + 1)) ||
38824f680cc6SAli Bahrami 			    (strncmp(MSG_ORIG(MSG_STR_GNU), pnstate.pn_name,
38834f680cc6SAli Bahrami 			    MSG_STR_CORE_SIZE + 1) != 0) ||
38844f680cc6SAli Bahrami 			    (pnstate.pn_descsz < 16))
38854f680cc6SAli Bahrami 				continue;
38864f680cc6SAli Bahrami 
38874f680cc6SAli Bahrami 			/*
38884f680cc6SAli Bahrami 			 * desc contains 4 32-bit fields. Field 0 must be 0,
38894f680cc6SAli Bahrami 			 * indicating Linux. The second, third, and fourth
38904f680cc6SAli Bahrami 			 * fields represent the earliest Linux kernel
38914f680cc6SAli Bahrami 			 * version compatible with this object.
38924f680cc6SAli Bahrami 			 */
38934f680cc6SAli Bahrami 			/*LINTED*/
38944f680cc6SAli Bahrami 			w = (Word *) pnstate.pn_desc;
38954f680cc6SAli Bahrami 			if (*w == 0)
38964f680cc6SAli Bahrami 				return (1);
38974f680cc6SAli Bahrami 		}
38984f680cc6SAli Bahrami 	}
38994f680cc6SAli Bahrami 
39004f680cc6SAli Bahrami 	return (0);
39014f680cc6SAli Bahrami }
39024f680cc6SAli Bahrami 
39034f680cc6SAli Bahrami /*
39045aefb655Srie  * Determine an individual hash entry.  This may be the initial hash entry,
39055aefb655Srie  * or an associated chain entry.
39065aefb655Srie  */
39075aefb655Srie static void
hash_entry(Cache * refsec,Cache * strsec,const char * hsecname,Word hashndx,Word symndx,Word symn,Sym * syms,const char * file,ulong_t bkts,uint_t flags,int chain)39085aefb655Srie hash_entry(Cache *refsec, Cache *strsec, const char *hsecname, Word hashndx,
39095aefb655Srie     Word symndx, Word symn, Sym *syms, const char *file, ulong_t bkts,
39105aefb655Srie     uint_t flags, int chain)
39115aefb655Srie {
39125aefb655Srie 	Sym		*sym;
39135aefb655Srie 	const char	*symname, *str;
39145aefb655Srie 	char		_bucket[MAXNDXSIZE], _symndx[MAXNDXSIZE];
39155aefb655Srie 	ulong_t		nbkt, nhash;
39165aefb655Srie 
39175aefb655Srie 	if (symndx > symn) {
39185aefb655Srie 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_HSBADSYMNDX), file,
39195aefb655Srie 		    EC_WORD(symndx), EC_WORD(hashndx));
39205aefb655Srie 		symname = MSG_INTL(MSG_STR_UNKNOWN);
39215aefb655Srie 	} else {
39225aefb655Srie 		sym = (Sym *)(syms + symndx);
39235aefb655Srie 		symname = string(refsec, symndx, strsec, file, sym->st_name);
39245aefb655Srie 	}
39255aefb655Srie 
39265aefb655Srie 	if (chain == 0) {
39275aefb655Srie 		(void) snprintf(_bucket, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INTEGER),
39285aefb655Srie 		    hashndx);
39295aefb655Srie 		str = (const char *)_bucket;
39305aefb655Srie 	} else
39315aefb655Srie 		str = MSG_ORIG(MSG_STR_EMPTY);
39325aefb655Srie 
39335aefb655Srie 	(void) snprintf(_symndx, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INDEX2),
39345aefb655Srie 	    EC_WORD(symndx));
39355aefb655Srie 	dbg_print(0, MSG_ORIG(MSG_FMT_HASH_INFO), str, _symndx,
39365aefb655Srie 	    demangle(symname, flags));
39375aefb655Srie 
39385aefb655Srie 	/*
39395aefb655Srie 	 * Determine if this string is in the correct bucket.
39405aefb655Srie 	 */
39415aefb655Srie 	nhash = elf_hash(symname);
39425aefb655Srie 	nbkt = nhash % bkts;
39435aefb655Srie 
39445aefb655Srie 	if (nbkt != hashndx) {
39455aefb655Srie 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADHASH), file,
39465aefb655Srie 		    hsecname, symname, EC_WORD(hashndx), nbkt);
39475aefb655Srie 	}
39485aefb655Srie }
39497c478bd9Sstevel@tonic-gate 
39507c478bd9Sstevel@tonic-gate #define	MAXCOUNT	500
39517c478bd9Sstevel@tonic-gate 
39527c478bd9Sstevel@tonic-gate static void
hash(Cache * cache,Word shnum,const char * file,uint_t flags)3953c809c407Sab196087 hash(Cache *cache, Word shnum, const char *file, uint_t flags)
39547c478bd9Sstevel@tonic-gate {
39557c478bd9Sstevel@tonic-gate 	static int	count[MAXCOUNT];
39565aefb655Srie 	Word		cnt;
39577c478bd9Sstevel@tonic-gate 	ulong_t		ndx, bkts;
39587c478bd9Sstevel@tonic-gate 	char		number[MAXNDXSIZE];
39597c478bd9Sstevel@tonic-gate 
39607c478bd9Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
39617c478bd9Sstevel@tonic-gate 		uint_t		*hash, *chain;
39627c478bd9Sstevel@tonic-gate 		Cache		*_cache = &cache[cnt];
39635aefb655Srie 		Shdr		*sshdr, *hshdr = _cache->c_shdr;
39645aefb655Srie 		char		*ssecname, *hsecname = _cache->c_name;
39655aefb655Srie 		Sym		*syms;
39665aefb655Srie 		Word		symn;
39677c478bd9Sstevel@tonic-gate 
39685aefb655Srie 		if (hshdr->sh_type != SHT_HASH)
39697c478bd9Sstevel@tonic-gate 			continue;
39707c478bd9Sstevel@tonic-gate 
39717c478bd9Sstevel@tonic-gate 		/*
39727c478bd9Sstevel@tonic-gate 		 * Determine the hash table data and size.
39737c478bd9Sstevel@tonic-gate 		 */
39745aefb655Srie 		if ((hshdr->sh_entsize == 0) || (hshdr->sh_size == 0)) {
39757c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
39765aefb655Srie 			    file, hsecname);
39777c478bd9Sstevel@tonic-gate 			continue;
39787c478bd9Sstevel@tonic-gate 		}
39799a411307Srie 		if (_cache->c_data == NULL)
39809a411307Srie 			continue;
39819a411307Srie 
39827c478bd9Sstevel@tonic-gate 		hash = (uint_t *)_cache->c_data->d_buf;
39837c478bd9Sstevel@tonic-gate 		bkts = *hash;
39847c478bd9Sstevel@tonic-gate 		chain = hash + 2 + bkts;
39857c478bd9Sstevel@tonic-gate 		hash += 2;
39867c478bd9Sstevel@tonic-gate 
39877c478bd9Sstevel@tonic-gate 		/*
39887c478bd9Sstevel@tonic-gate 		 * Get the data buffer for the associated symbol table.
39897c478bd9Sstevel@tonic-gate 		 */
39905aefb655Srie 		if ((hshdr->sh_link == 0) || (hshdr->sh_link >= shnum)) {
39917c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
39925aefb655Srie 			    file, hsecname, EC_WORD(hshdr->sh_link));
39937c478bd9Sstevel@tonic-gate 			continue;
39947c478bd9Sstevel@tonic-gate 		}
39957c478bd9Sstevel@tonic-gate 
39965aefb655Srie 		_cache = &cache[hshdr->sh_link];
39975aefb655Srie 		ssecname = _cache->c_name;
39985aefb655Srie 
39999a411307Srie 		if (_cache->c_data == NULL)
40009a411307Srie 			continue;
40019a411307Srie 
40029a411307Srie 		if ((syms = (Sym *)_cache->c_data->d_buf) == NULL) {
40035aefb655Srie 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
40045aefb655Srie 			    file, ssecname);
40055aefb655Srie 			continue;
40065aefb655Srie 		}
40075aefb655Srie 
40085aefb655Srie 		sshdr = _cache->c_shdr;
40095aefb655Srie 		/* LINTED */
40105aefb655Srie 		symn = (Word)(sshdr->sh_size / sshdr->sh_entsize);
40115aefb655Srie 
40127c478bd9Sstevel@tonic-gate 		/*
40137c478bd9Sstevel@tonic-gate 		 * Get the associated string table section.
40147c478bd9Sstevel@tonic-gate 		 */
40155aefb655Srie 		if ((sshdr->sh_link == 0) || (sshdr->sh_link >= shnum)) {
40167c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
40175aefb655Srie 			    file, ssecname, EC_WORD(sshdr->sh_link));
40187c478bd9Sstevel@tonic-gate 			continue;
40197c478bd9Sstevel@tonic-gate 		}
40207c478bd9Sstevel@tonic-gate 
40215aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
40225aefb655Srie 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_HASH), hsecname);
40235aefb655Srie 		dbg_print(0, MSG_INTL(MSG_ELF_HASH_INFO));
40247c478bd9Sstevel@tonic-gate 
40257c478bd9Sstevel@tonic-gate 		/*
40267c478bd9Sstevel@tonic-gate 		 * Loop through the hash buckets, printing the appropriate
40277c478bd9Sstevel@tonic-gate 		 * symbols.
40287c478bd9Sstevel@tonic-gate 		 */
40297c478bd9Sstevel@tonic-gate 		for (ndx = 0; ndx < bkts; ndx++, hash++) {
40305aefb655Srie 			Word	_ndx, _cnt;
40317c478bd9Sstevel@tonic-gate 
40327c478bd9Sstevel@tonic-gate 			if (*hash == 0) {
40337c478bd9Sstevel@tonic-gate 				count[0]++;
40347c478bd9Sstevel@tonic-gate 				continue;
40357c478bd9Sstevel@tonic-gate 			}
40367c478bd9Sstevel@tonic-gate 
40375aefb655Srie 			hash_entry(_cache, &cache[sshdr->sh_link], hsecname,
40385aefb655Srie 			    ndx, *hash, symn, syms, file, bkts, flags, 0);
40397c478bd9Sstevel@tonic-gate 
40407c478bd9Sstevel@tonic-gate 			/*
40417c478bd9Sstevel@tonic-gate 			 * Determine if any other symbols are chained to this
40427c478bd9Sstevel@tonic-gate 			 * bucket.
40437c478bd9Sstevel@tonic-gate 			 */
40447c478bd9Sstevel@tonic-gate 			_ndx = chain[*hash];
40457c478bd9Sstevel@tonic-gate 			_cnt = 1;
40467c478bd9Sstevel@tonic-gate 			while (_ndx) {
40475aefb655Srie 				hash_entry(_cache, &cache[sshdr->sh_link],
40485aefb655Srie 				    hsecname, ndx, _ndx, symn, syms, file,
40495aefb655Srie 				    bkts, flags, 1);
40507c478bd9Sstevel@tonic-gate 				_ndx = chain[_ndx];
40517c478bd9Sstevel@tonic-gate 				_cnt++;
40527c478bd9Sstevel@tonic-gate 			}
40537c478bd9Sstevel@tonic-gate 
40547c478bd9Sstevel@tonic-gate 			if (_cnt >= MAXCOUNT) {
40557c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
4056141040e8Srie 				    MSG_INTL(MSG_HASH_OVERFLW), file,
40575aefb655Srie 				    _cache->c_name, EC_WORD(ndx),
40585aefb655Srie 				    EC_WORD(_cnt));
40597c478bd9Sstevel@tonic-gate 			} else
40607c478bd9Sstevel@tonic-gate 				count[_cnt]++;
40617c478bd9Sstevel@tonic-gate 		}
40627c478bd9Sstevel@tonic-gate 		break;
40637c478bd9Sstevel@tonic-gate 	}
40647c478bd9Sstevel@tonic-gate 
40657c478bd9Sstevel@tonic-gate 	/*
40667c478bd9Sstevel@tonic-gate 	 * Print out the count information.
40677c478bd9Sstevel@tonic-gate 	 */
40687c478bd9Sstevel@tonic-gate 	bkts = cnt = 0;
40695aefb655Srie 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
40705aefb655Srie 
40717c478bd9Sstevel@tonic-gate 	for (ndx = 0; ndx < MAXCOUNT; ndx++) {
40725aefb655Srie 		Word	_cnt;
40737c478bd9Sstevel@tonic-gate 
40747c478bd9Sstevel@tonic-gate 		if ((_cnt = count[ndx]) == 0)
40757c478bd9Sstevel@tonic-gate 			continue;
40767c478bd9Sstevel@tonic-gate 
40775aefb655Srie 		(void) snprintf(number, MAXNDXSIZE,
40785aefb655Srie 		    MSG_ORIG(MSG_FMT_INTEGER), _cnt);
40795aefb655Srie 		dbg_print(0, MSG_INTL(MSG_ELF_HASH_BKTS1), number,
40805aefb655Srie 		    EC_WORD(ndx));
40817c478bd9Sstevel@tonic-gate 		bkts += _cnt;
40825aefb655Srie 		cnt += (Word)(ndx * _cnt);
40837c478bd9Sstevel@tonic-gate 	}
40847c478bd9Sstevel@tonic-gate 	if (cnt) {
40857c478bd9Sstevel@tonic-gate 		(void) snprintf(number, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INTEGER),
40865aefb655Srie 		    bkts);
40875aefb655Srie 		dbg_print(0, MSG_INTL(MSG_ELF_HASH_BKTS2), number,
40885aefb655Srie 		    EC_WORD(cnt));
40897c478bd9Sstevel@tonic-gate 	}
40907c478bd9Sstevel@tonic-gate }
40917c478bd9Sstevel@tonic-gate 
40927c478bd9Sstevel@tonic-gate static void
group(Cache * cache,Word shnum,const char * file,uint_t flags)4093c809c407Sab196087 group(Cache *cache, Word shnum, const char *file, uint_t flags)
40947c478bd9Sstevel@tonic-gate {
40955aefb655Srie 	Word	scnt;
40967c478bd9Sstevel@tonic-gate 
40975aefb655Srie 	for (scnt = 1; scnt < shnum; scnt++) {
40985aefb655Srie 		Cache		*_cache = &cache[scnt];
40995aefb655Srie 		Shdr		*shdr = _cache->c_shdr;
41005aefb655Srie 		Word		*grpdata, gcnt, grpcnt, symnum, unknown;
41015aefb655Srie 		Cache		*symsec, *strsec;
41025aefb655Srie 		Sym		*syms, *sym;
41037c478bd9Sstevel@tonic-gate 		char		flgstrbuf[MSG_GRP_COMDAT_SIZE + 10];
4104e64d0ff9SAli Bahrami 		const char	*grpnam;
41057c478bd9Sstevel@tonic-gate 
41067c478bd9Sstevel@tonic-gate 		if (shdr->sh_type != SHT_GROUP)
41077c478bd9Sstevel@tonic-gate 			continue;
4108981a172dSab196087 		if (!match(MATCH_F_ALL, _cache->c_name, scnt, shdr->sh_type))
41097c478bd9Sstevel@tonic-gate 			continue;
41109a411307Srie 		if ((_cache->c_data == NULL) ||
41119a411307Srie 		    ((grpdata = (Word *)_cache->c_data->d_buf) == NULL))
41127c478bd9Sstevel@tonic-gate 			continue;
41135aefb655Srie 		grpcnt = shdr->sh_size / sizeof (Word);
41145aefb655Srie 
41155aefb655Srie 		/*
41165aefb655Srie 		 * Get the data buffer for the associated symbol table and
41175aefb655Srie 		 * string table.
41185aefb655Srie 		 */
41195aefb655Srie 		if (stringtbl(cache, 1, scnt, shnum, file,
41205aefb655Srie 		    &symnum, &symsec, &strsec) == 0)
41215aefb655Srie 			return;
41225aefb655Srie 
41235aefb655Srie 		syms = symsec->c_data->d_buf;
41245aefb655Srie 
41255aefb655Srie 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
41265aefb655Srie 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_GRP), _cache->c_name);
41275aefb655Srie 		dbg_print(0, MSG_INTL(MSG_GRP_TITLE));
41285aefb655Srie 
41295aefb655Srie 		/*
41305aefb655Srie 		 * The first element of the group defines the group.  The
41315aefb655Srie 		 * associated symbol is defined by the sh_link field.
41325aefb655Srie 		 */
41335aefb655Srie 		if ((shdr->sh_info == SHN_UNDEF) || (shdr->sh_info > symnum)) {
41345aefb655Srie 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHINFO),
41355aefb655Srie 			    file, _cache->c_name, EC_WORD(shdr->sh_info));
41365aefb655Srie 			return;
41377c478bd9Sstevel@tonic-gate 		}
41387c478bd9Sstevel@tonic-gate 
41395aefb655Srie 		(void) strcpy(flgstrbuf, MSG_ORIG(MSG_STR_OSQBRKT));
41407c478bd9Sstevel@tonic-gate 		if (grpdata[0] & GRP_COMDAT) {
41415aefb655Srie 			(void) strcat(flgstrbuf, MSG_ORIG(MSG_GRP_COMDAT));
41427c478bd9Sstevel@tonic-gate 		}
41435aefb655Srie 		if ((unknown = (grpdata[0] & ~GRP_COMDAT)) != 0) {
41445aefb655Srie 			size_t	len = strlen(flgstrbuf);
41457c478bd9Sstevel@tonic-gate 
41465aefb655Srie 			(void) snprintf(&flgstrbuf[len],
41475aefb655Srie 			    (MSG_GRP_COMDAT_SIZE + 10 - len),
41485aefb655Srie 			    MSG_ORIG(MSG_GRP_UNKNOWN), unknown);
41497c478bd9Sstevel@tonic-gate 		}
41505aefb655Srie 		(void) strcat(flgstrbuf, MSG_ORIG(MSG_STR_CSQBRKT));
41515aefb655Srie 		sym = (Sym *)(syms + shdr->sh_info);
41525aefb655Srie 
4153e64d0ff9SAli Bahrami 		/*
4154e64d0ff9SAli Bahrami 		 * The GNU assembler can use section symbols as the signature
4155e64d0ff9SAli Bahrami 		 * symbol as described by this comment in the gold linker
4156e64d0ff9SAli Bahrami 		 * (found via google):
4157e64d0ff9SAli Bahrami 		 *
4158e64d0ff9SAli Bahrami 		 *	It seems that some versions of gas will create a
4159e64d0ff9SAli Bahrami 		 *	section group associated with a section symbol, and
4160e64d0ff9SAli Bahrami 		 *	then fail to give a name to the section symbol.  In
4161e64d0ff9SAli Bahrami 		 *	such a case, use the name of the section.
4162e64d0ff9SAli Bahrami 		 *
4163e64d0ff9SAli Bahrami 		 * In order to support such objects, we do the same.
4164e64d0ff9SAli Bahrami 		 */
4165e64d0ff9SAli Bahrami 		grpnam = string(_cache, 0, strsec, file, sym->st_name);
4166e64d0ff9SAli Bahrami 		if (((sym->st_name == 0) || (*grpnam == '\0')) &&
4167e64d0ff9SAli Bahrami 		    (ELF_ST_TYPE(sym->st_info) == STT_SECTION))
4168e64d0ff9SAli Bahrami 			grpnam = cache[sym->st_shndx].c_name;
4169e64d0ff9SAli Bahrami 
41705aefb655Srie 		dbg_print(0, MSG_INTL(MSG_GRP_SIGNATURE), flgstrbuf,
4171e64d0ff9SAli Bahrami 		    demangle(grpnam, flags));
41725aefb655Srie 
41735aefb655Srie 		for (gcnt = 1; gcnt < grpcnt; gcnt++) {
41747c478bd9Sstevel@tonic-gate 			char		index[MAXNDXSIZE];
41755aefb655Srie 			const char	*name;
41767c478bd9Sstevel@tonic-gate 
41777c478bd9Sstevel@tonic-gate 			(void) snprintf(index, MAXNDXSIZE,
41785aefb655Srie 			    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(gcnt));
41797c478bd9Sstevel@tonic-gate 
41805aefb655Srie 			if (grpdata[gcnt] >= shnum)
41815aefb655Srie 				name = MSG_INTL(MSG_GRP_INVALSCN);
41825aefb655Srie 			else
41835aefb655Srie 				name = cache[grpdata[gcnt]].c_name;
41845aefb655Srie 
41855aefb655Srie 			(void) printf(MSG_ORIG(MSG_GRP_ENTRY), index, name,
41865aefb655Srie 			    EC_XWORD(grpdata[gcnt]));
41875aefb655Srie 		}
41885aefb655Srie 	}
41895aefb655Srie }
41907c478bd9Sstevel@tonic-gate 
41917c478bd9Sstevel@tonic-gate static void
got(Cache * cache,Word shnum,Ehdr * ehdr,const char * file)41920e233487SRod Evans got(Cache *cache, Word shnum, Ehdr *ehdr, const char *file)
41937c478bd9Sstevel@tonic-gate {
4194f959f76aSab196087 	Cache		*gotcache = NULL, *symtab = NULL;
41955aefb655Srie 	Addr		gotbgn, gotend;
41965aefb655Srie 	Shdr		*gotshdr;
41975aefb655Srie 	Word		cnt, gotents, gotndx;
41987c478bd9Sstevel@tonic-gate 	size_t		gentsize;
41997c478bd9Sstevel@tonic-gate 	Got_info	*gottable;
42007c478bd9Sstevel@tonic-gate 	char		*gotdata;
42015aefb655Srie 	Sym		*gotsym;
42025aefb655Srie 	Xword		gotsymaddr;
4203ba2be530Sab196087 	uint_t		sys_encoding;
42047c478bd9Sstevel@tonic-gate 
42057c478bd9Sstevel@tonic-gate 	/*
4206141040e8Srie 	 * First, find the got.
42077c478bd9Sstevel@tonic-gate 	 */
42087c478bd9Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
4209f959f76aSab196087 		if (strncmp(cache[cnt].c_name, MSG_ORIG(MSG_ELF_GOT),
4210141040e8Srie 		    MSG_ELF_GOT_SIZE) == 0) {
4211f959f76aSab196087 			gotcache = &cache[cnt];
42127c478bd9Sstevel@tonic-gate 			break;
42137c478bd9Sstevel@tonic-gate 		}
42147c478bd9Sstevel@tonic-gate 	}
4215f959f76aSab196087 	if (gotcache == NULL)
42167c478bd9Sstevel@tonic-gate 		return;
4217141040e8Srie 
4218141040e8Srie 	/*
4219141040e8Srie 	 * A got section within a relocatable object is suspicious.
4220141040e8Srie 	 */
4221141040e8Srie 	if (ehdr->e_type == ET_REL) {
4222141040e8Srie 		(void) fprintf(stderr, MSG_INTL(MSG_GOT_UNEXPECTED), file,
4223f959f76aSab196087 		    gotcache->c_name);
4224141040e8Srie 	}
4225141040e8Srie 
42265aefb655Srie 	gotshdr = gotcache->c_shdr;
42277c478bd9Sstevel@tonic-gate 	if (gotshdr->sh_size == 0) {
42287c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
42297c478bd9Sstevel@tonic-gate 		    file, gotcache->c_name);
42307c478bd9Sstevel@tonic-gate 		return;
42317c478bd9Sstevel@tonic-gate 	}
42325aefb655Srie 
42335aefb655Srie 	gotbgn = gotshdr->sh_addr;
42347c478bd9Sstevel@tonic-gate 	gotend = gotbgn + gotshdr->sh_size;
42357c478bd9Sstevel@tonic-gate 
42367c478bd9Sstevel@tonic-gate 	/*
42375aefb655Srie 	 * Some architectures don't properly set the sh_entsize for the GOT
42385aefb655Srie 	 * table.  If it's not set, default to a size of a pointer.
42397c478bd9Sstevel@tonic-gate 	 */
42405aefb655Srie 	if ((gentsize = gotshdr->sh_entsize) == 0)
42415aefb655Srie 		gentsize = sizeof (Xword);
42425aefb655Srie 
42439a411307Srie 	if (gotcache->c_data == NULL)
42449a411307Srie 		return;
42459a411307Srie 
42467c478bd9Sstevel@tonic-gate 	/* LINTED */
42475aefb655Srie 	gotents = (Word)(gotshdr->sh_size / gentsize);
42487c478bd9Sstevel@tonic-gate 	gotdata = gotcache->c_data->d_buf;
42497c478bd9Sstevel@tonic-gate 
42507c478bd9Sstevel@tonic-gate 	if ((gottable = calloc(gotents, sizeof (Got_info))) == 0) {
42517c478bd9Sstevel@tonic-gate 		int err = errno;
42525aefb655Srie 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC), file,
42535aefb655Srie 		    strerror(err));
42547c478bd9Sstevel@tonic-gate 		return;
42557c478bd9Sstevel@tonic-gate 	}
42567c478bd9Sstevel@tonic-gate 
42577c478bd9Sstevel@tonic-gate 	/*
42587c478bd9Sstevel@tonic-gate 	 * Now we scan through all the sections looking for any relocations
42597c478bd9Sstevel@tonic-gate 	 * that may be against the GOT.  Since these may not be isolated to a
42607c478bd9Sstevel@tonic-gate 	 * .rel[a].got section we check them all.
42617c478bd9Sstevel@tonic-gate 	 * While scanning sections save the symbol table entry (a symtab
42627c478bd9Sstevel@tonic-gate 	 * overriding a dynsym) so that we can lookup _GLOBAL_OFFSET_TABLE_.
42637c478bd9Sstevel@tonic-gate 	 */
42647c478bd9Sstevel@tonic-gate 	for (cnt = 1; cnt < shnum; cnt++) {
42655aefb655Srie 		Word		type, symnum;
42665aefb655Srie 		Xword		relndx, relnum, relsize;
42675aefb655Srie 		void		*rels;
42685aefb655Srie 		Sym		*syms;
42695aefb655Srie 		Cache		*symsec, *strsec;
42705aefb655Srie 		Cache		*_cache = &cache[cnt];
42715aefb655Srie 		Shdr		*shdr;
42727c478bd9Sstevel@tonic-gate 
42735aefb655Srie 		shdr = _cache->c_shdr;
42745aefb655Srie 		type = shdr->sh_type;
42757c478bd9Sstevel@tonic-gate 
42765aefb655Srie 		if ((symtab == 0) && (type == SHT_DYNSYM)) {
42777c478bd9Sstevel@tonic-gate 			symtab = _cache;
42787c478bd9Sstevel@tonic-gate 			continue;
42797c478bd9Sstevel@tonic-gate 		}
42805aefb655Srie 		if (type == SHT_SYMTAB) {
42817c478bd9Sstevel@tonic-gate 			symtab = _cache;
42827c478bd9Sstevel@tonic-gate 			continue;
42837c478bd9Sstevel@tonic-gate 		}
42845aefb655Srie 		if ((type != SHT_RELA) && (type != SHT_REL))
42857c478bd9Sstevel@tonic-gate 			continue;
42867c478bd9Sstevel@tonic-gate 
42877c478bd9Sstevel@tonic-gate 		/*
42885aefb655Srie 		 * Decide entry size.
42897c478bd9Sstevel@tonic-gate 		 */
42905aefb655Srie 		if (((relsize = shdr->sh_entsize) == 0) ||
42915aefb655Srie 		    (relsize > shdr->sh_size)) {
42925aefb655Srie 			if (type == SHT_RELA)
42935aefb655Srie 				relsize = sizeof (Rela);
42945aefb655Srie 			else
42955aefb655Srie 				relsize = sizeof (Rel);
42965aefb655Srie 		}
42975aefb655Srie 
42985aefb655Srie 		/*
42995aefb655Srie 		 * Determine the number of relocations available.
43005aefb655Srie 		 */
43015aefb655Srie 		if (shdr->sh_size == 0) {
43027c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
43037c478bd9Sstevel@tonic-gate 			    file, _cache->c_name);
43047c478bd9Sstevel@tonic-gate 			continue;
43057c478bd9Sstevel@tonic-gate 		}
43069a411307Srie 		if (_cache->c_data == NULL)
43079a411307Srie 			continue;
43089a411307Srie 
43095aefb655Srie 		rels = _cache->c_data->d_buf;
43105aefb655Srie 		relnum = shdr->sh_size / relsize;
43117c478bd9Sstevel@tonic-gate 
43127c478bd9Sstevel@tonic-gate 		/*
43135aefb655Srie 		 * Get the data buffer for the associated symbol table and
43145aefb655Srie 		 * string table.
43157c478bd9Sstevel@tonic-gate 		 */
43165aefb655Srie 		if (stringtbl(cache, 1, cnt, shnum, file,
43175aefb655Srie 		    &symnum, &symsec, &strsec) == 0)
43187c478bd9Sstevel@tonic-gate 			continue;
43197c478bd9Sstevel@tonic-gate 
43205aefb655Srie 		syms = symsec->c_data->d_buf;
43215aefb655Srie 
43227c478bd9Sstevel@tonic-gate 		/*
43235aefb655Srie 		 * Loop through the relocation entries.
43247c478bd9Sstevel@tonic-gate 		 */
43255aefb655Srie 		for (relndx = 0; relndx < relnum; relndx++,
43265aefb655Srie 		    rels = (void *)((char *)rels + relsize)) {
43275aefb655Srie 			char		section[BUFSIZ];
43285aefb655Srie 			Addr		offset;
43297c478bd9Sstevel@tonic-gate 			Got_info	*gip;
43305aefb655Srie 			Word		symndx, reltype;
43315aefb655Srie 			Rela		*rela;
43325aefb655Srie 			Rel		*rel;
43337c478bd9Sstevel@tonic-gate 
43345aefb655Srie 			/*
43355aefb655Srie 			 * Unravel the relocation.
43365aefb655Srie 			 */
43375aefb655Srie 			if (type == SHT_RELA) {
43385aefb655Srie 				rela = (Rela *)rels;
43395aefb655Srie 				symndx = ELF_R_SYM(rela->r_info);
4340ba2be530Sab196087 				reltype = ELF_R_TYPE(rela->r_info,
4341ba2be530Sab196087 				    ehdr->e_machine);
43425aefb655Srie 				offset = rela->r_offset;
43437c478bd9Sstevel@tonic-gate 			} else {
43445aefb655Srie 				rel = (Rel *)rels;
43455aefb655Srie 				symndx = ELF_R_SYM(rel->r_info);
4346ba2be530Sab196087 				reltype = ELF_R_TYPE(rel->r_info,
4347ba2be530Sab196087 				    ehdr->e_machine);
43485aefb655Srie 				offset = rel->r_offset;
43497c478bd9Sstevel@tonic-gate 			}
43507c478bd9Sstevel@tonic-gate 
43517c478bd9Sstevel@tonic-gate 			/*
43527c478bd9Sstevel@tonic-gate 			 * Only pay attention to relocations against the GOT.
43537c478bd9Sstevel@tonic-gate 			 */
4354c22c4bcbSab196087 			if ((offset < gotbgn) || (offset >= gotend))
43557c478bd9Sstevel@tonic-gate 				continue;
43567c478bd9Sstevel@tonic-gate 
43577c478bd9Sstevel@tonic-gate 			/* LINTED */
43585aefb655Srie 			gotndx = (Word)((offset - gotbgn) /
43597c478bd9Sstevel@tonic-gate 			    gotshdr->sh_entsize);
43607c478bd9Sstevel@tonic-gate 			gip = &gottable[gotndx];
43615aefb655Srie 
43625aefb655Srie 			if (gip->g_reltype != 0) {
43637c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
43647c478bd9Sstevel@tonic-gate 				    MSG_INTL(MSG_GOT_MULTIPLE), file,
43655aefb655Srie 				    EC_WORD(gotndx), EC_ADDR(offset));
43667c478bd9Sstevel@tonic-gate 				continue;
43677c478bd9Sstevel@tonic-gate 			}
43687c478bd9Sstevel@tonic-gate 
43695aefb655Srie 			if (symndx)
43705aefb655Srie 				gip->g_symname = relsymname(cache, _cache,
43715aefb655Srie 				    strsec, symndx, symnum, relndx, syms,
43720e233487SRod Evans 				    section, BUFSIZ, file);
43735aefb655Srie 			gip->g_reltype = reltype;
43745aefb655Srie 			gip->g_rel = rels;
43757c478bd9Sstevel@tonic-gate 		}
43767c478bd9Sstevel@tonic-gate 	}
43777c478bd9Sstevel@tonic-gate 
437831dd2c84SAli Bahrami 	if (symlookup(MSG_ORIG(MSG_SYM_GOT), cache, shnum, &gotsym, NULL,
437931dd2c84SAli Bahrami 	    symtab, file))
43805aefb655Srie 		gotsymaddr = gotsym->st_value;
43817c478bd9Sstevel@tonic-gate 	else
43825aefb655Srie 		gotsymaddr = gotbgn;
43837c478bd9Sstevel@tonic-gate 
43845aefb655Srie 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
43855aefb655Srie 	dbg_print(0, MSG_INTL(MSG_ELF_SCN_GOT), gotcache->c_name);
43865aefb655Srie 	Elf_got_title(0);
43877c478bd9Sstevel@tonic-gate 
4388ba2be530Sab196087 	sys_encoding = _elf_sys_encoding();
43897c478bd9Sstevel@tonic-gate 	for (gotndx = 0; gotndx < gotents; gotndx++) {
43907c478bd9Sstevel@tonic-gate 		Got_info	*gip;
43917c478bd9Sstevel@tonic-gate 		Sword		gindex;
43925aefb655Srie 		Addr		gaddr;
43935aefb655Srie 		Xword		gotentry;
43947c478bd9Sstevel@tonic-gate 
43957c478bd9Sstevel@tonic-gate 		gip = &gottable[gotndx];
43967c478bd9Sstevel@tonic-gate 
43977c478bd9Sstevel@tonic-gate 		gaddr = gotbgn + (gotndx * gentsize);
43985aefb655Srie 		gindex = (Sword)(gaddr - gotsymaddr) / (Sword)gentsize;
43997c478bd9Sstevel@tonic-gate 
44005aefb655Srie 		if (gentsize == sizeof (Word))
44017c478bd9Sstevel@tonic-gate 			/* LINTED */
44025aefb655Srie 			gotentry = (Xword)(*((Word *)(gotdata) + gotndx));
44037c478bd9Sstevel@tonic-gate 		else
44047c478bd9Sstevel@tonic-gate 			/* LINTED */
44055aefb655Srie 			gotentry = *((Xword *)(gotdata) + gotndx);
44067c478bd9Sstevel@tonic-gate 
44075aefb655Srie 		Elf_got_entry(0, gindex, gaddr, gotentry, ehdr->e_machine,
4408ba2be530Sab196087 		    ehdr->e_ident[EI_DATA], sys_encoding,
44095aefb655Srie 		    gip->g_reltype, gip->g_rel, gip->g_symname);
44107c478bd9Sstevel@tonic-gate 	}
44117c478bd9Sstevel@tonic-gate 	free(gottable);
44127c478bd9Sstevel@tonic-gate }
44137c478bd9Sstevel@tonic-gate 
44147c478bd9Sstevel@tonic-gate void
checksum(Elf * elf)44157c478bd9Sstevel@tonic-gate checksum(Elf *elf)
44167c478bd9Sstevel@tonic-gate {
44175aefb655Srie 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
44185aefb655Srie 	dbg_print(0, MSG_INTL(MSG_STR_CHECKSUM), elf_checksum(elf));
44197c478bd9Sstevel@tonic-gate }
44207c478bd9Sstevel@tonic-gate 
44214fa4b40cSab196087 /*
44224fa4b40cSab196087  * This variable is used by regular() to communicate the address of
44234fa4b40cSab196087  * the section header cache to sort_shdr_ndx_arr(). Unfortunately,
44244fa4b40cSab196087  * the qsort() interface does not include a userdata argument by which
44254fa4b40cSab196087  * such arbitrary data can be passed, so we are stuck using global data.
44264fa4b40cSab196087  */
44274fa4b40cSab196087 static Cache *sort_shdr_ndx_arr_cache;
44284fa4b40cSab196087 
44294fa4b40cSab196087 
44304fa4b40cSab196087 /*
44314fa4b40cSab196087  * Used with qsort() to sort the section indices so that they can be
44324fa4b40cSab196087  * used to access the section headers in order of increasing data offset.
44334fa4b40cSab196087  *
44344fa4b40cSab196087  * entry:
44354fa4b40cSab196087  *	sort_shdr_ndx_arr_cache - Contains address of
44364fa4b40cSab196087  *		section header cache.
44374fa4b40cSab196087  *	v1, v2 - Point at elements of sort_shdr_bits array to be compared.
44384fa4b40cSab196087  *
44394fa4b40cSab196087  * exit:
44404fa4b40cSab196087  *	Returns -1 (less than), 0 (equal) or 1 (greater than).
44414fa4b40cSab196087  */
44424fa4b40cSab196087 static int
sort_shdr_ndx_arr(const void * v1,const void * v2)44434fa4b40cSab196087 sort_shdr_ndx_arr(const void *v1, const void *v2)
44444fa4b40cSab196087 {
44454fa4b40cSab196087 	Cache	*cache1 = sort_shdr_ndx_arr_cache + *((size_t *)v1);
44464fa4b40cSab196087 	Cache	*cache2 = sort_shdr_ndx_arr_cache + *((size_t *)v2);
44474fa4b40cSab196087 
44484fa4b40cSab196087 	if (cache1->c_shdr->sh_offset < cache2->c_shdr->sh_offset)
44494fa4b40cSab196087 		return (-1);
44504fa4b40cSab196087 
44514fa4b40cSab196087 	if (cache1->c_shdr->sh_offset > cache2->c_shdr->sh_offset)
44524fa4b40cSab196087 		return (1);
44534fa4b40cSab196087 
44544fa4b40cSab196087 	return (0);
44554fa4b40cSab196087 }
44564fa4b40cSab196087 
44574fa4b40cSab196087 
445839773e46Sab196087 static int
shdr_cache(const char * file,Elf * elf,Ehdr * ehdr,size_t shstrndx,size_t shnum,Cache ** cache_ret,Word flags)445939773e46Sab196087 shdr_cache(const char *file, Elf *elf, Ehdr *ehdr, size_t shstrndx,
44600e233487SRod Evans     size_t shnum, Cache **cache_ret, Word flags)
44617c478bd9Sstevel@tonic-gate {
44627c478bd9Sstevel@tonic-gate 	Elf_Scn		*scn;
44637c478bd9Sstevel@tonic-gate 	Elf_Data	*data;
446439773e46Sab196087 	size_t		ndx;
446539773e46Sab196087 	Shdr		*nameshdr;
44667e16fca0SAli Bahrami 	char		*names = NULL;
44677c478bd9Sstevel@tonic-gate 	Cache		*cache, *_cache;
44684fa4b40cSab196087 	size_t		*shdr_ndx_arr, shdr_ndx_arr_cnt;
44697c478bd9Sstevel@tonic-gate 
44707c478bd9Sstevel@tonic-gate 
44717c478bd9Sstevel@tonic-gate 	/*
44727c478bd9Sstevel@tonic-gate 	 * Obtain the .shstrtab data buffer to provide the required section
44737c478bd9Sstevel@tonic-gate 	 * name strings.
44747c478bd9Sstevel@tonic-gate 	 */
44752eec250aSab196087 	if (shstrndx == SHN_UNDEF) {
44762eec250aSab196087 		/*
44772eec250aSab196087 		 * It is rare, but legal, for an object to lack a
44782eec250aSab196087 		 * header string table section.
44792eec250aSab196087 		 */
44802eec250aSab196087 		names = NULL;
44812eec250aSab196087 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_NOSHSTRSEC), file);
44822eec250aSab196087 	} else if ((scn = elf_getscn(elf, shstrndx)) == NULL) {
44837c478bd9Sstevel@tonic-gate 		failure(file, MSG_ORIG(MSG_ELF_GETSCN));
44847c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SHDR),
44857c478bd9Sstevel@tonic-gate 		    EC_XWORD(shstrndx));
44865aefb655Srie 
44877c478bd9Sstevel@tonic-gate 	} else if ((data = elf_getdata(scn, NULL)) == NULL) {
44887c478bd9Sstevel@tonic-gate 		failure(file, MSG_ORIG(MSG_ELF_GETDATA));
44897c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_DATA),
44907c478bd9Sstevel@tonic-gate 		    EC_XWORD(shstrndx));
44915aefb655Srie 
44925aefb655Srie 	} else if ((nameshdr = elf_getshdr(scn)) == NULL) {
44937c478bd9Sstevel@tonic-gate 		failure(file, MSG_ORIG(MSG_ELF_GETSHDR));
44947c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN),
4495f5a18a30Srie 		    EC_WORD(elf_ndxscn(scn)));
44965aefb655Srie 
44977e16fca0SAli Bahrami 	} else if ((names = data->d_buf) == NULL)
44987c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_SHSTRNULL), file);
44997c478bd9Sstevel@tonic-gate 
45007c478bd9Sstevel@tonic-gate 	/*
4501f5a18a30Srie 	 * Allocate a cache to maintain a descriptor for each section.
45027c478bd9Sstevel@tonic-gate 	 */
450339773e46Sab196087 	if ((*cache_ret = cache = malloc(shnum * sizeof (Cache))) == NULL) {
45047c478bd9Sstevel@tonic-gate 		int err = errno;
45057c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
45067c478bd9Sstevel@tonic-gate 		    file, strerror(err));
450739773e46Sab196087 		return (0);
45087c478bd9Sstevel@tonic-gate 	}
45097c478bd9Sstevel@tonic-gate 
45105aefb655Srie 	*cache = cache_init;
45117c478bd9Sstevel@tonic-gate 	_cache = cache;
45127c478bd9Sstevel@tonic-gate 	_cache++;
45137c478bd9Sstevel@tonic-gate 
4514f5a18a30Srie 	/*
45154fa4b40cSab196087 	 * Allocate an array that will hold the section index for
45164fa4b40cSab196087 	 * each section that has data in the ELF file:
45174fa4b40cSab196087 	 *
45184fa4b40cSab196087 	 *	- Is not a NOBITS section
45194fa4b40cSab196087 	 *	- Data has non-zero length
45204fa4b40cSab196087 	 *
45214fa4b40cSab196087 	 * Note that shnum is an upper bound on the size required. It
45224fa4b40cSab196087 	 * is likely that we won't use a few of these array elements.
45234fa4b40cSab196087 	 * Allocating a modest amount of extra memory in this case means
45244fa4b40cSab196087 	 * that we can avoid an extra loop to count the number of needed
45254fa4b40cSab196087 	 * items, and can fill this array immediately in the first loop
45264fa4b40cSab196087 	 * below.
45274fa4b40cSab196087 	 */
45284fa4b40cSab196087 	if ((shdr_ndx_arr = malloc(shnum * sizeof (*shdr_ndx_arr))) == NULL) {
45294fa4b40cSab196087 		int err = errno;
45304fa4b40cSab196087 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
45314fa4b40cSab196087 		    file, strerror(err));
453239773e46Sab196087 		return (0);
45334fa4b40cSab196087 	}
45344fa4b40cSab196087 	shdr_ndx_arr_cnt = 0;
45354fa4b40cSab196087 
45364fa4b40cSab196087 	/*
4537f5a18a30Srie 	 * Traverse the sections of the file.  This gathering of data is
4538f5a18a30Srie 	 * carried out in two passes.  First, the section headers are captured
4539f5a18a30Srie 	 * and the section header names are evaluated.  A verification pass is
4540f5a18a30Srie 	 * then carried out over the section information.  Files have been
4541f5a18a30Srie 	 * known to exhibit overlapping (and hence erroneous) section header
4542f5a18a30Srie 	 * information.
4543f5a18a30Srie 	 *
4544f5a18a30Srie 	 * Finally, the data for each section is obtained.  This processing is
4545f5a18a30Srie 	 * carried out after section verification because should any section
4546f5a18a30Srie 	 * header overlap occur, and a file needs translating (ie. xlate'ing
4547f5a18a30Srie 	 * information from a non-native architecture file), then the process
4548f5a18a30Srie 	 * of translation can corrupt the section header information.  Of
4549f5a18a30Srie 	 * course, if there is any section overlap, the data related to the
4550f5a18a30Srie 	 * sections is going to be compromised.  However, it is the translation
4551f5a18a30Srie 	 * of this data that has caused problems with elfdump()'s ability to
4552f5a18a30Srie 	 * extract the data.
4553f5a18a30Srie 	 */
45544fa4b40cSab196087 	for (ndx = 1, scn = NULL; scn = elf_nextscn(elf, scn);
45554fa4b40cSab196087 	    ndx++, _cache++) {
4556f5a18a30Srie 		char	scnndxnm[100];
4557f5a18a30Srie 
45584fa4b40cSab196087 		_cache->c_ndx = ndx;
4559f5a18a30Srie 		_cache->c_scn = scn;
4560f5a18a30Srie 
45615aefb655Srie 		if ((_cache->c_shdr = elf_getshdr(scn)) == NULL) {
45627c478bd9Sstevel@tonic-gate 			failure(file, MSG_ORIG(MSG_ELF_GETSHDR));
45637c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN),
4564f5a18a30Srie 			    EC_WORD(elf_ndxscn(scn)));
45657c478bd9Sstevel@tonic-gate 		}
45667c478bd9Sstevel@tonic-gate 
4567f5a18a30Srie 		/*
45684fa4b40cSab196087 		 * If this section has data in the file, include it in
45694fa4b40cSab196087 		 * the array of sections to check for address overlap.
45704fa4b40cSab196087 		 */
45714fa4b40cSab196087 		if ((_cache->c_shdr->sh_size != 0) &&
45724fa4b40cSab196087 		    (_cache->c_shdr->sh_type != SHT_NOBITS))
45734fa4b40cSab196087 			shdr_ndx_arr[shdr_ndx_arr_cnt++] = ndx;
45744fa4b40cSab196087 
45754fa4b40cSab196087 		/*
4576f5a18a30Srie 		 * If a shstrtab exists, assign the section name.
4577f5a18a30Srie 		 */
4578f5a18a30Srie 		if (names && _cache->c_shdr) {
4579f5a18a30Srie 			if (_cache->c_shdr->sh_name &&
45807c478bd9Sstevel@tonic-gate 			    /* LINTED */
4581f5a18a30Srie 			    (nameshdr->sh_size > _cache->c_shdr->sh_name)) {
45820e233487SRod Evans 				const char	*symname;
45830e233487SRod Evans 				char		*secname;
45840e233487SRod Evans 
45850e233487SRod Evans 				secname = names + _cache->c_shdr->sh_name;
45860e233487SRod Evans 
45870e233487SRod Evans 				/*
45880e233487SRod Evans 				 * A SUN naming convention employs a "%" within
45890e233487SRod Evans 				 * a section name to indicate a section/symbol
45900e233487SRod Evans 				 * name.  This originated from the compilers
45910e233487SRod Evans 				 * -xF option, that places functions into their
45920e233487SRod Evans 				 * own sections.  This convention (which has no
45930e233487SRod Evans 				 * formal standard) has also been followed for
45940e233487SRod Evans 				 * COMDAT sections.  To demangle the symbol
45950e233487SRod Evans 				 * name, the name must be separated from the
45960e233487SRod Evans 				 * section name.
45970e233487SRod Evans 				 */
45980e233487SRod Evans 				if (((flags & FLG_CTL_DEMANGLE) == 0) ||
45990e233487SRod Evans 				    ((symname = strchr(secname, '%')) == NULL))
46000e233487SRod Evans 					_cache->c_name = secname;
46010e233487SRod Evans 				else {
46020e233487SRod Evans 					size_t	secsz = ++symname - secname;
46030e233487SRod Evans 					size_t	strsz;
46040e233487SRod Evans 
46050e233487SRod Evans 					symname = demangle(symname, flags);
46060e233487SRod Evans 					strsz = secsz + strlen(symname) + 1;
46070e233487SRod Evans 
46080e233487SRod Evans 					if ((_cache->c_name =
46090e233487SRod Evans 					    malloc(strsz)) == NULL) {
46100e233487SRod Evans 						int err = errno;
46110e233487SRod Evans 						(void) fprintf(stderr,
46120e233487SRod Evans 						    MSG_INTL(MSG_ERR_MALLOC),
46130e233487SRod Evans 						    file, strerror(err));
46140e233487SRod Evans 						return (0);
46150e233487SRod Evans 					}
46160e233487SRod Evans 					(void) snprintf(_cache->c_name, strsz,
46170e233487SRod Evans 					    MSG_ORIG(MSG_FMT_SECSYM),
46180e233487SRod Evans 					    EC_WORD(secsz), secname, symname);
46190e233487SRod Evans 				}
46207e16fca0SAli Bahrami 
4621f5a18a30Srie 				continue;
4622f5a18a30Srie 			}
46237c478bd9Sstevel@tonic-gate 
46247c478bd9Sstevel@tonic-gate 			/*
4625f5a18a30Srie 			 * Generate an error if the section name index is zero
4626f5a18a30Srie 			 * or exceeds the shstrtab data.  Fall through to
4627f5a18a30Srie 			 * fabricate a section name.
46287c478bd9Sstevel@tonic-gate 			 */
4629f5a18a30Srie 			if ((_cache->c_shdr->sh_name == 0) ||
46307c478bd9Sstevel@tonic-gate 			    /* LINTED */
46315aefb655Srie 			    (nameshdr->sh_size <= _cache->c_shdr->sh_name)) {
46327c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
46337c478bd9Sstevel@tonic-gate 				    MSG_INTL(MSG_ERR_BADSHNAME), file,
46344fa4b40cSab196087 				    EC_WORD(ndx),
46355aefb655Srie 				    EC_XWORD(_cache->c_shdr->sh_name));
46367c478bd9Sstevel@tonic-gate 			}
4637f5a18a30Srie 		}
46387c478bd9Sstevel@tonic-gate 
4639f5a18a30Srie 		/*
4640f5a18a30Srie 		 * If there exists no shstrtab data, or a section header has no
4641f5a18a30Srie 		 * name (an invalid index of 0), then compose a name for the
4642f5a18a30Srie 		 * section.
4643f5a18a30Srie 		 */
4644f5a18a30Srie 		(void) snprintf(scnndxnm, sizeof (scnndxnm),
46454fa4b40cSab196087 		    MSG_INTL(MSG_FMT_SCNNDX), ndx);
4646f5a18a30Srie 
46474fa4b40cSab196087 		if ((_cache->c_name = malloc(strlen(scnndxnm) + 1)) == NULL) {
46487c478bd9Sstevel@tonic-gate 			int err = errno;
46497c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
46507c478bd9Sstevel@tonic-gate 			    file, strerror(err));
465139773e46Sab196087 			return (0);
46527c478bd9Sstevel@tonic-gate 		}
46537c478bd9Sstevel@tonic-gate 		(void) strcpy(_cache->c_name, scnndxnm);
46547c478bd9Sstevel@tonic-gate 	}
46557c478bd9Sstevel@tonic-gate 
4656f5a18a30Srie 	/*
4657f5a18a30Srie 	 * Having collected all the sections, validate their address range.
4658f5a18a30Srie 	 * Cases have existed where the section information has been invalid.
4659f5a18a30Srie 	 * This can lead to all sorts of other, hard to diagnose errors, as
4660f5a18a30Srie 	 * each section is processed individually (ie. with elf_getdata()).
4661f5a18a30Srie 	 * Here, we carry out some address comparisons to catch a family of
4662f5a18a30Srie 	 * overlapping memory issues we have observed (likely, there are others
4663f5a18a30Srie 	 * that we have yet to discover).
4664f5a18a30Srie 	 *
4665f5a18a30Srie 	 * Note, should any memory overlap occur, obtaining any additional
4666f5a18a30Srie 	 * data from the file is questionable.  However, it might still be
4667f5a18a30Srie 	 * possible to inspect the ELF header, Programs headers, or individual
4668f5a18a30Srie 	 * sections, so rather than bailing on an error condition, continue
4669f5a18a30Srie 	 * processing to see if any data can be salvaged.
4670f5a18a30Srie 	 */
46714fa4b40cSab196087 	if (shdr_ndx_arr_cnt > 1) {
46724fa4b40cSab196087 		sort_shdr_ndx_arr_cache = cache;
46734fa4b40cSab196087 		qsort(shdr_ndx_arr, shdr_ndx_arr_cnt,
46744fa4b40cSab196087 		    sizeof (*shdr_ndx_arr), sort_shdr_ndx_arr);
46754fa4b40cSab196087 	}
46764fa4b40cSab196087 	for (ndx = 0; ndx < shdr_ndx_arr_cnt; ndx++) {
46774fa4b40cSab196087 		Cache	*_cache = cache + shdr_ndx_arr[ndx];
4678f5a18a30Srie 		Shdr	*shdr = _cache->c_shdr;
4679f5a18a30Srie 		Off	bgn1, bgn = shdr->sh_offset;
4680f5a18a30Srie 		Off	end1, end = shdr->sh_offset + shdr->sh_size;
46814fa4b40cSab196087 		size_t	ndx1;
4682f5a18a30Srie 
46834fa4b40cSab196087 		/*
46844fa4b40cSab196087 		 * Check the section against all following ones, reporting
46854fa4b40cSab196087 		 * any overlaps. Since we've sorted the sections by offset,
46864fa4b40cSab196087 		 * we can stop after the first comparison that fails. There
46874fa4b40cSab196087 		 * are no overlaps in a properly formed ELF file, in which
46884fa4b40cSab196087 		 * case this algorithm runs in O(n) time. This will degenerate
46894fa4b40cSab196087 		 * to O(n^2) for a completely broken file. Such a file is
46904fa4b40cSab196087 		 * (1) highly unlikely, and (2) unusable, so it is reasonable
46914fa4b40cSab196087 		 * for the analysis to take longer.
46924fa4b40cSab196087 		 */
46934fa4b40cSab196087 		for (ndx1 = ndx + 1; ndx1 < shdr_ndx_arr_cnt; ndx1++) {
46944fa4b40cSab196087 			Cache	*_cache1 = cache + shdr_ndx_arr[ndx1];
4695f5a18a30Srie 			Shdr	*shdr1 = _cache1->c_shdr;
4696f5a18a30Srie 
4697f5a18a30Srie 			bgn1 = shdr1->sh_offset;
4698f5a18a30Srie 			end1 = shdr1->sh_offset + shdr1->sh_size;
4699f5a18a30Srie 
4700f5a18a30Srie 			if (((bgn1 <= bgn) && (end1 > bgn)) ||
4701f5a18a30Srie 			    ((bgn1 < end) && (end1 >= end))) {
4702f5a18a30Srie 				(void) fprintf(stderr,
4703f5a18a30Srie 				    MSG_INTL(MSG_ERR_SECMEMOVER), file,
4704f5a18a30Srie 				    EC_WORD(elf_ndxscn(_cache->c_scn)),
47054fa4b40cSab196087 				    _cache->c_name, EC_OFF(bgn), EC_OFF(end),
47064fa4b40cSab196087 				    EC_WORD(elf_ndxscn(_cache1->c_scn)),
47074fa4b40cSab196087 				    _cache1->c_name, EC_OFF(bgn1),
47084fa4b40cSab196087 				    EC_OFF(end1));
47094fa4b40cSab196087 			} else {	/* No overlap, so can stop */
47104fa4b40cSab196087 				break;
4711f5a18a30Srie 			}
4712f5a18a30Srie 		}
4713f5a18a30Srie 
4714f5a18a30Srie 		/*
47154fa4b40cSab196087 		 * In addition to checking for sections overlapping
47164fa4b40cSab196087 		 * each other (done above), we should also make sure
47174fa4b40cSab196087 		 * the section doesn't overlap the section header array.
4718f5a18a30Srie 		 */
4719f5a18a30Srie 		bgn1 = ehdr->e_shoff;
4720f5a18a30Srie 		end1 = ehdr->e_shoff + (ehdr->e_shentsize * ehdr->e_shnum);
4721f5a18a30Srie 
4722f5a18a30Srie 		if (((bgn1 <= bgn) && (end1 > bgn)) ||
4723f5a18a30Srie 		    ((bgn1 < end) && (end1 >= end))) {
4724f5a18a30Srie 			(void) fprintf(stderr,
4725f5a18a30Srie 			    MSG_INTL(MSG_ERR_SHDRMEMOVER), file, EC_OFF(bgn1),
4726f5a18a30Srie 			    EC_OFF(end1),
4727f5a18a30Srie 			    EC_WORD(elf_ndxscn(_cache->c_scn)),
4728f5a18a30Srie 			    _cache->c_name, EC_OFF(bgn), EC_OFF(end));
4729f5a18a30Srie 		}
4730f5a18a30Srie 	}
4731f5a18a30Srie 
4732f5a18a30Srie 	/*
47334fa4b40cSab196087 	 * Obtain the data for each section.
4734f5a18a30Srie 	 */
47354fa4b40cSab196087 	for (ndx = 1; ndx < shnum; ndx++) {
47364fa4b40cSab196087 		Cache	*_cache = &cache[ndx];
4737f5a18a30Srie 		Elf_Scn	*scn = _cache->c_scn;
4738f5a18a30Srie 
47397c478bd9Sstevel@tonic-gate 		if ((_cache->c_data = elf_getdata(scn, NULL)) == NULL) {
47407c478bd9Sstevel@tonic-gate 			failure(file, MSG_ORIG(MSG_ELF_GETDATA));
47417c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCNDATA),
4742f5a18a30Srie 			    EC_WORD(elf_ndxscn(scn)));
47437c478bd9Sstevel@tonic-gate 		}
47441da7e599SAli Bahrami 
47451da7e599SAli Bahrami 		/*
47461da7e599SAli Bahrami 		 * If a string table, verify that it has NULL first and
47471da7e599SAli Bahrami 		 * final bytes.
47481da7e599SAli Bahrami 		 */
47491da7e599SAli Bahrami 		if ((_cache->c_shdr->sh_type == SHT_STRTAB) &&
47501da7e599SAli Bahrami 		    (_cache->c_data->d_buf != NULL) &&
47511da7e599SAli Bahrami 		    (_cache->c_data->d_size > 0)) {
47521da7e599SAli Bahrami 			const char *s = _cache->c_data->d_buf;
47531da7e599SAli Bahrami 
47541da7e599SAli Bahrami 			if ((*s != '\0') ||
47551da7e599SAli Bahrami 			    (*(s + _cache->c_data->d_size - 1) != '\0'))
47561da7e599SAli Bahrami 				(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALSTR),
47571da7e599SAli Bahrami 				    file, _cache->c_name);
47581da7e599SAli Bahrami 		}
475939773e46Sab196087 	}
476039773e46Sab196087 
476139773e46Sab196087 	return (1);
476239773e46Sab196087 }
476339773e46Sab196087 
476439773e46Sab196087 
476539773e46Sab196087 
47664f680cc6SAli Bahrami /*
47674f680cc6SAli Bahrami  * Generate a cache of section headers and related information
47684f680cc6SAli Bahrami  * for use by the rest of elfdump. If requested (or the file
47694f680cc6SAli Bahrami  * contains no section headers), we generate a fake set of
47704f680cc6SAli Bahrami  * headers from the information accessible from the program headers.
47714f680cc6SAli Bahrami  * Otherwise, we use the real section headers contained in the file.
47724f680cc6SAli Bahrami  */
47734f680cc6SAli Bahrami static int
create_cache(const char * file,int fd,Elf * elf,Ehdr * ehdr,Cache ** cache,size_t shstrndx,size_t * shnum,uint_t * flags)47744f680cc6SAli Bahrami create_cache(const char *file, int fd, Elf *elf, Ehdr *ehdr, Cache **cache,
47754f680cc6SAli Bahrami     size_t shstrndx, size_t *shnum, uint_t *flags)
47764f680cc6SAli Bahrami {
47774f680cc6SAli Bahrami 	/*
47784f680cc6SAli Bahrami 	 * If there are no section headers, then resort to synthesizing
47794f680cc6SAli Bahrami 	 * section headers from the program headers. This is normally
47804f680cc6SAli Bahrami 	 * only done by explicit request, but in this case there's no
47814f680cc6SAli Bahrami 	 * reason not to go ahead, since the alternative is simply to quit.
47824f680cc6SAli Bahrami 	 */
47834f680cc6SAli Bahrami 	if ((*shnum <= 1) && ((*flags & FLG_CTL_FAKESHDR) == 0)) {
47844f680cc6SAli Bahrami 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_NOSHDR), file);
47854f680cc6SAli Bahrami 		*flags |= FLG_CTL_FAKESHDR;
47864f680cc6SAli Bahrami 	}
47874f680cc6SAli Bahrami 
47884f680cc6SAli Bahrami 	if (*flags & FLG_CTL_FAKESHDR) {
47894f680cc6SAli Bahrami 		if (fake_shdr_cache(file, fd, elf, ehdr, cache, shnum) == 0)
47904f680cc6SAli Bahrami 			return (0);
47914f680cc6SAli Bahrami 	} else {
47924f680cc6SAli Bahrami 		if (shdr_cache(file, elf, ehdr, shstrndx, *shnum,
47934f680cc6SAli Bahrami 		    cache, *flags) == 0)
47944f680cc6SAli Bahrami 			return (0);
47954f680cc6SAli Bahrami 	}
47964f680cc6SAli Bahrami 
47974f680cc6SAli Bahrami 	return (1);
47984f680cc6SAli Bahrami }
47994f680cc6SAli Bahrami 
4800981a172dSab196087 int
regular(const char * file,int fd,Elf * elf,uint_t flags,const char * wname,int wfd,uchar_t osabi)4801981a172dSab196087 regular(const char *file, int fd, Elf *elf, uint_t flags,
48024f680cc6SAli Bahrami     const char *wname, int wfd, uchar_t osabi)
480339773e46Sab196087 {
48044f680cc6SAli Bahrami 	enum { CACHE_NEEDED, CACHE_OK, CACHE_FAIL} cache_state = CACHE_NEEDED;
480539773e46Sab196087 	Elf_Scn		*scn;
480639773e46Sab196087 	Ehdr		*ehdr;
480739773e46Sab196087 	size_t		ndx, shstrndx, shnum, phnum;
480839773e46Sab196087 	Shdr		*shdr;
480939773e46Sab196087 	Cache		*cache;
48108a8ef765SRichard Lowe 	VERSYM_STATE	versym = { 0 };
4811981a172dSab196087 	int		ret = 0;
4812981a172dSab196087 	int		addr_align;
481339773e46Sab196087 
481439773e46Sab196087 	if ((ehdr = elf_getehdr(elf)) == NULL) {
481539773e46Sab196087 		failure(file, MSG_ORIG(MSG_ELF_GETEHDR));
4816981a172dSab196087 		return (ret);
481739773e46Sab196087 	}
481839773e46Sab196087 
481962b628a6SAli Bahrami 	if (elf_getshdrnum(elf, &shnum) == -1) {
482062b628a6SAli Bahrami 		failure(file, MSG_ORIG(MSG_ELF_GETSHDRNUM));
4821981a172dSab196087 		return (ret);
482239773e46Sab196087 	}
482339773e46Sab196087 
482462b628a6SAli Bahrami 	if (elf_getshdrstrndx(elf, &shstrndx) == -1) {
482562b628a6SAli Bahrami 		failure(file, MSG_ORIG(MSG_ELF_GETSHDRSTRNDX));
4826981a172dSab196087 		return (ret);
482739773e46Sab196087 	}
482839773e46Sab196087 
482962b628a6SAli Bahrami 	if (elf_getphdrnum(elf, &phnum) == -1) {
483062b628a6SAli Bahrami 		failure(file, MSG_ORIG(MSG_ELF_GETPHDRNUM));
4831981a172dSab196087 		return (ret);
483239773e46Sab196087 	}
483339773e46Sab196087 	/*
483439773e46Sab196087 	 * If the user requested section headers derived from the
483539773e46Sab196087 	 * program headers (-P option) and this file doesn't have
483639773e46Sab196087 	 * any program headers (i.e. ET_REL), then we can't do it.
483739773e46Sab196087 	 */
4838981a172dSab196087 	if ((phnum == 0) && (flags & FLG_CTL_FAKESHDR)) {
483939773e46Sab196087 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_PNEEDSPH), file);
4840981a172dSab196087 		return (ret);
484139773e46Sab196087 	}
484239773e46Sab196087 
484339773e46Sab196087 
484439773e46Sab196087 	if ((scn = elf_getscn(elf, 0)) != NULL) {
484539773e46Sab196087 		if ((shdr = elf_getshdr(scn)) == NULL) {
484639773e46Sab196087 			failure(file, MSG_ORIG(MSG_ELF_GETSHDR));
484739773e46Sab196087 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN), 0);
4848981a172dSab196087 			return (ret);
484939773e46Sab196087 		}
485039773e46Sab196087 	} else
48517e16fca0SAli Bahrami 		shdr = NULL;
48527c478bd9Sstevel@tonic-gate 
48537c478bd9Sstevel@tonic-gate 	/*
485439773e46Sab196087 	 * Print the elf header.
48557c478bd9Sstevel@tonic-gate 	 */
4856981a172dSab196087 	if (flags & FLG_SHOW_EHDR)
485739773e46Sab196087 		Elf_ehdr(0, ehdr, shdr);
485839773e46Sab196087 
485939773e46Sab196087 	/*
486039773e46Sab196087 	 * If the section headers or program headers have inadequate
486139773e46Sab196087 	 * alignment for the class of object, print a warning. libelf
486239773e46Sab196087 	 * can handle such files, but programs that use them can crash
486339773e46Sab196087 	 * when they dereference unaligned items.
4864981a172dSab196087 	 *
4865981a172dSab196087 	 * Note that the AMD64 ABI, although it is a 64-bit architecture,
4866981a172dSab196087 	 * allows access to data types smaller than 128-bits to be on
4867981a172dSab196087 	 * word alignment.
486839773e46Sab196087 	 */
4869981a172dSab196087 	if (ehdr->e_machine == EM_AMD64)
4870981a172dSab196087 		addr_align = sizeof (Word);
4871981a172dSab196087 	else
4872981a172dSab196087 		addr_align = sizeof (Addr);
4873981a172dSab196087 
4874981a172dSab196087 	if (ehdr->e_phoff & (addr_align - 1))
487539773e46Sab196087 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADPHDRALIGN), file);
4876981a172dSab196087 	if (ehdr->e_shoff & (addr_align - 1))
487739773e46Sab196087 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHDRALIGN), file);
487839773e46Sab196087 
48794f680cc6SAli Bahrami 
48804f680cc6SAli Bahrami 	/*
48814f680cc6SAli Bahrami 	 * Determine the Operating System ABI (osabi) we will use to
48824f680cc6SAli Bahrami 	 * interpret the object.
48834f680cc6SAli Bahrami 	 */
48844f680cc6SAli Bahrami 	if (flags & FLG_CTL_OSABI) {
48854f680cc6SAli Bahrami 		/*
48864f680cc6SAli Bahrami 		 * If the user explicitly specifies '-O none', we need
48874f680cc6SAli Bahrami 		 * to display a completely generic view of the file.
48884f680cc6SAli Bahrami 		 * However, libconv is written to assume that ELFOSABI_NONE
48894f680cc6SAli Bahrami 		 * is equivalent to ELFOSABI_SOLARIS. To get the desired
48904f680cc6SAli Bahrami 		 * effect, we use an osabi that libconv has no knowledge of.
48914f680cc6SAli Bahrami 		 */
48924f680cc6SAli Bahrami 		if (osabi == ELFOSABI_NONE)
48934f680cc6SAli Bahrami 			osabi = ELFOSABI_UNKNOWN4;
48944f680cc6SAli Bahrami 	} else {
48954f680cc6SAli Bahrami 		/* Determine osabi from file */
48964f680cc6SAli Bahrami 		osabi = ehdr->e_ident[EI_OSABI];
48974f680cc6SAli Bahrami 		if (osabi == ELFOSABI_NONE) {
48984f680cc6SAli Bahrami 			/*
48994f680cc6SAli Bahrami 			 * Chicken/Egg scenario:
49004f680cc6SAli Bahrami 			 *
49014f680cc6SAli Bahrami 			 * Ideally, we wait to create the section header cache
49024f680cc6SAli Bahrami 			 * until after the program headers are printed. If we
49034f680cc6SAli Bahrami 			 * only output program headers, we can skip building
49044f680cc6SAli Bahrami 			 * the cache entirely.
49054f680cc6SAli Bahrami 			 *
49064f680cc6SAli Bahrami 			 * Proper interpretation of program headers requires
49074f680cc6SAli Bahrami 			 * the osabi, which is supposed to be in the ELF header.
49084f680cc6SAli Bahrami 			 * However, many systems (Solaris and Linux included)
49094f680cc6SAli Bahrami 			 * have a history of setting the osabi to the generic
49104f680cc6SAli Bahrami 			 * SysV ABI (ELFOSABI_NONE). We assume ELFOSABI_SOLARIS
49114f680cc6SAli Bahrami 			 * in such cases, but would like to check the object
49124f680cc6SAli Bahrami 			 * to see if it has a Linux .note.ABI-tag section,
49134f680cc6SAli Bahrami 			 * which implies ELFOSABI_LINUX. This requires a
49144f680cc6SAli Bahrami 			 * section header cache.
49154f680cc6SAli Bahrami 			 *
49164f680cc6SAli Bahrami 			 * To break the cycle, we create section headers now
49174f680cc6SAli Bahrami 			 * if osabi is ELFOSABI_NONE, and later otherwise.
49184f680cc6SAli Bahrami 			 * If it succeeds, we use them, if not, we defer
49194f680cc6SAli Bahrami 			 * exiting until after the program headers are out.
49204f680cc6SAli Bahrami 			 */
49214f680cc6SAli Bahrami 			if (create_cache(file, fd, elf, ehdr, &cache,
49224f680cc6SAli Bahrami 			    shstrndx, &shnum, &flags) == 0) {
49234f680cc6SAli Bahrami 				cache_state = CACHE_FAIL;
49244f680cc6SAli Bahrami 			} else {
49254f680cc6SAli Bahrami 				cache_state = CACHE_OK;
49264f680cc6SAli Bahrami 				if (has_linux_abi_note(cache, shnum, file)) {
49274f680cc6SAli Bahrami 					Conv_inv_buf_t	ibuf1, ibuf2;
49284f680cc6SAli Bahrami 
49294f680cc6SAli Bahrami 					(void) fprintf(stderr,
49304f680cc6SAli Bahrami 					    MSG_INTL(MSG_INFO_LINUXOSABI), file,
49314f680cc6SAli Bahrami 					    conv_ehdr_osabi(osabi, 0, &ibuf1),
49324f680cc6SAli Bahrami 					    conv_ehdr_osabi(ELFOSABI_LINUX,
49334f680cc6SAli Bahrami 					    0, &ibuf2));
49344f680cc6SAli Bahrami 					osabi = ELFOSABI_LINUX;
49354f680cc6SAli Bahrami 				}
49364f680cc6SAli Bahrami 			}
49374f680cc6SAli Bahrami 		}
49384f680cc6SAli Bahrami 		/*
49394f680cc6SAli Bahrami 		 * We treat ELFOSABI_NONE identically to ELFOSABI_SOLARIS.
49404f680cc6SAli Bahrami 		 * Mapping NONE to SOLARIS simplifies the required test.
49414f680cc6SAli Bahrami 		 */
49424f680cc6SAli Bahrami 		if (osabi == ELFOSABI_NONE)
49434f680cc6SAli Bahrami 			osabi = ELFOSABI_SOLARIS;
49444f680cc6SAli Bahrami 	}
49454f680cc6SAli Bahrami 
494639773e46Sab196087 	/*
494739773e46Sab196087 	 * Print the program headers.
494839773e46Sab196087 	 */
4949981a172dSab196087 	if ((flags & FLG_SHOW_PHDR) && (phnum != 0)) {
495039773e46Sab196087 		Phdr	*phdr;
495139773e46Sab196087 
495239773e46Sab196087 		if ((phdr = elf_getphdr(elf)) == NULL) {
495339773e46Sab196087 			failure(file, MSG_ORIG(MSG_ELF_GETPHDR));
4954981a172dSab196087 			return (ret);
495539773e46Sab196087 		}
495639773e46Sab196087 
495739773e46Sab196087 		for (ndx = 0; ndx < phnum; phdr++, ndx++) {
4958981a172dSab196087 			if (!match(MATCH_F_PHDR| MATCH_F_NDX | MATCH_F_TYPE,
4959981a172dSab196087 			    NULL, ndx, phdr->p_type))
496039773e46Sab196087 				continue;
496139773e46Sab196087 
496239773e46Sab196087 			dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
496339773e46Sab196087 			dbg_print(0, MSG_INTL(MSG_ELF_PHDR), EC_WORD(ndx));
49644f680cc6SAli Bahrami 			Elf_phdr(0, osabi, ehdr->e_machine, phdr);
496539773e46Sab196087 		}
496639773e46Sab196087 	}
496739773e46Sab196087 
496839773e46Sab196087 	/*
4969981a172dSab196087 	 * If we have flag bits set that explicitly require a show or calc
4970981a172dSab196087 	 * operation, but none of them require the section headers, then
4971981a172dSab196087 	 * we are done and can return now.
497239773e46Sab196087 	 */
4973981a172dSab196087 	if (((flags & (FLG_MASK_SHOW | FLG_MASK_CALC)) != 0) &&
4974981a172dSab196087 	    ((flags & (FLG_MASK_SHOW_SHDR | FLG_MASK_CALC_SHDR)) == 0))
4975981a172dSab196087 		return (ret);
497639773e46Sab196087 
497739773e46Sab196087 	/*
4978981a172dSab196087 	 * Everything from this point on requires section headers.
4979981a172dSab196087 	 * If we have no section headers, there is no reason to continue.
49804f680cc6SAli Bahrami 	 *
49814f680cc6SAli Bahrami 	 * If we tried above to create the section header cache and failed,
49824f680cc6SAli Bahrami 	 * it is time to exit. Otherwise, create it if needed.
4983981a172dSab196087 	 */
49844f680cc6SAli Bahrami 	switch (cache_state) {
49854f680cc6SAli Bahrami 	case CACHE_NEEDED:
49864f680cc6SAli Bahrami 		if (create_cache(file, fd, elf, ehdr, &cache, shstrndx,
49874f680cc6SAli Bahrami 		    &shnum, &flags) == 0)
49884f680cc6SAli Bahrami 			return (ret);
49894f680cc6SAli Bahrami 		break;
4990*37915d86SRichard Lowe 	case CACHE_OK:
4991*37915d86SRichard Lowe 		break;
49924f680cc6SAli Bahrami 	case CACHE_FAIL:
49934f680cc6SAli Bahrami 		return (ret);
49944f680cc6SAli Bahrami 	}
4995981a172dSab196087 	if (shnum <= 1)
4996981a172dSab196087 		goto done;
4997981a172dSab196087 
4998981a172dSab196087 	/*
499939773e46Sab196087 	 * If -w was specified, find and write out the section(s) data.
500039773e46Sab196087 	 */
500139773e46Sab196087 	if (wfd) {
500239773e46Sab196087 		for (ndx = 1; ndx < shnum; ndx++) {
500339773e46Sab196087 			Cache	*_cache = &cache[ndx];
500439773e46Sab196087 
5005981a172dSab196087 			if (match(MATCH_F_STRICT | MATCH_F_ALL, _cache->c_name,
5006981a172dSab196087 			    ndx, _cache->c_shdr->sh_type) &&
5007981a172dSab196087 			    _cache->c_data && _cache->c_data->d_buf) {
5008981a172dSab196087 				if (write(wfd, _cache->c_data->d_buf,
5009981a172dSab196087 				    _cache->c_data->d_size) !=
5010981a172dSab196087 				    _cache->c_data->d_size) {
5011981a172dSab196087 					int err = errno;
5012981a172dSab196087 					(void) fprintf(stderr,
5013981a172dSab196087 					    MSG_INTL(MSG_ERR_WRITE), wname,
5014981a172dSab196087 					    strerror(err));
5015981a172dSab196087 					/*
5016981a172dSab196087 					 * Return an exit status of 1, because
5017981a172dSab196087 					 * the failure is not related to the
5018981a172dSab196087 					 * ELF file, but by system resources.
5019981a172dSab196087 					 */
5020981a172dSab196087 					ret = 1;
5021981a172dSab196087 					goto done;
5022981a172dSab196087 				}
50237c478bd9Sstevel@tonic-gate 			}
50247c478bd9Sstevel@tonic-gate 		}
502539773e46Sab196087 	}
50267c478bd9Sstevel@tonic-gate 
5027981a172dSab196087 	/*
5028981a172dSab196087 	 * If we have no flag bits set that explicitly require a show or calc
5029981a172dSab196087 	 * operation, but match options (-I, -N, -T) were used, then run
5030981a172dSab196087 	 * through the section headers and see if we can't deduce show flags
5031981a172dSab196087 	 * from the match options given.
5032981a172dSab196087 	 *
5033981a172dSab196087 	 * We don't do this if -w was specified, because (-I, -N, -T) used
5034981a172dSab196087 	 * with -w in lieu of some other option is supposed to be quiet.
5035981a172dSab196087 	 */
5036981a172dSab196087 	if ((wfd == 0) && (flags & FLG_CTL_MATCH) &&
5037981a172dSab196087 	    ((flags & (FLG_MASK_SHOW | FLG_MASK_CALC)) == 0)) {
5038981a172dSab196087 		for (ndx = 1; ndx < shnum; ndx++) {
5039981a172dSab196087 			Cache	*_cache = &cache[ndx];
5040981a172dSab196087 
5041981a172dSab196087 			if (!match(MATCH_F_STRICT | MATCH_F_ALL, _cache->c_name,
5042981a172dSab196087 			    ndx, _cache->c_shdr->sh_type))
5043981a172dSab196087 				continue;
5044981a172dSab196087 
5045981a172dSab196087 			switch (_cache->c_shdr->sh_type) {
5046981a172dSab196087 			case SHT_PROGBITS:
5047981a172dSab196087 				/*
5048981a172dSab196087 				 * Heuristic time: It is usually bad form
50497e16fca0SAli Bahrami 				 * to assume the meaning/format of a PROGBITS
50507e16fca0SAli Bahrami 				 * section based on its name. However, there
5051e23c41c9SAli Bahrami 				 * are ABI mandated exceptions. Check for
5052e23c41c9SAli Bahrami 				 * these special names.
5053981a172dSab196087 				 */
5054e23c41c9SAli Bahrami 
5055e23c41c9SAli Bahrami 				/* The ELF ABI specifies .interp and .got */
5056981a172dSab196087 				if (strcmp(_cache->c_name,
5057e23c41c9SAli Bahrami 				    MSG_ORIG(MSG_ELF_INTERP)) == 0) {
5058981a172dSab196087 					flags |= FLG_SHOW_INTERP;
5059e23c41c9SAli Bahrami 					break;
5060e23c41c9SAli Bahrami 				}
5061e23c41c9SAli Bahrami 				if (strcmp(_cache->c_name,
5062e23c41c9SAli Bahrami 				    MSG_ORIG(MSG_ELF_GOT)) == 0) {
5063981a172dSab196087 					flags |= FLG_SHOW_GOT;
5064e23c41c9SAli Bahrami 					break;
5065e23c41c9SAli Bahrami 				}
5066e23c41c9SAli Bahrami 				/*
5067e23c41c9SAli Bahrami 				 * The GNU compilers, and amd64 ABI, define
5068e23c41c9SAli Bahrami 				 * .eh_frame and .eh_frame_hdr. The Sun
5069e23c41c9SAli Bahrami 				 * C++ ABI defines .exception_ranges.
5070e23c41c9SAli Bahrami 				 */
5071e23c41c9SAli Bahrami 				if ((strncmp(_cache->c_name,
50727e16fca0SAli Bahrami 				    MSG_ORIG(MSG_SCN_FRM),
5073e23c41c9SAli Bahrami 				    MSG_SCN_FRM_SIZE) == 0) ||
5074e23c41c9SAli Bahrami 				    (strncmp(_cache->c_name,
5075e23c41c9SAli Bahrami 				    MSG_ORIG(MSG_SCN_EXRANGE),
5076e23c41c9SAli Bahrami 				    MSG_SCN_EXRANGE_SIZE) == 0)) {
50777e16fca0SAli Bahrami 					flags |= FLG_SHOW_UNWIND;
5078981a172dSab196087 					break;
5079e23c41c9SAli Bahrami 				}
5080e23c41c9SAli Bahrami 				break;
5081981a172dSab196087 
5082981a172dSab196087 			case SHT_SYMTAB:
5083981a172dSab196087 			case SHT_DYNSYM:
5084981a172dSab196087 			case SHT_SUNW_LDYNSYM:
5085981a172dSab196087 			case SHT_SUNW_versym:
5086981a172dSab196087 			case SHT_SYMTAB_SHNDX:
5087981a172dSab196087 				flags |= FLG_SHOW_SYMBOLS;
5088981a172dSab196087 				break;
5089981a172dSab196087 
5090981a172dSab196087 			case SHT_RELA:
5091981a172dSab196087 			case SHT_REL:
5092981a172dSab196087 				flags |= FLG_SHOW_RELOC;
5093981a172dSab196087 				break;
5094981a172dSab196087 
5095981a172dSab196087 			case SHT_HASH:
5096981a172dSab196087 				flags |= FLG_SHOW_HASH;
5097981a172dSab196087 				break;
5098981a172dSab196087 
5099981a172dSab196087 			case SHT_DYNAMIC:
5100981a172dSab196087 				flags |= FLG_SHOW_DYNAMIC;
5101981a172dSab196087 				break;
5102981a172dSab196087 
5103981a172dSab196087 			case SHT_NOTE:
5104981a172dSab196087 				flags |= FLG_SHOW_NOTE;
5105981a172dSab196087 				break;
5106981a172dSab196087 
5107981a172dSab196087 			case SHT_GROUP:
5108981a172dSab196087 				flags |= FLG_SHOW_GROUP;
5109981a172dSab196087 				break;
5110981a172dSab196087 
5111981a172dSab196087 			case SHT_SUNW_symsort:
5112981a172dSab196087 			case SHT_SUNW_tlssort:
5113981a172dSab196087 				flags |= FLG_SHOW_SORT;
5114981a172dSab196087 				break;
5115981a172dSab196087 
5116981a172dSab196087 			case SHT_SUNW_cap:
5117981a172dSab196087 				flags |= FLG_SHOW_CAP;
5118981a172dSab196087 				break;
5119981a172dSab196087 
5120981a172dSab196087 			case SHT_SUNW_move:
5121981a172dSab196087 				flags |= FLG_SHOW_MOVE;
5122981a172dSab196087 				break;
5123981a172dSab196087 
5124981a172dSab196087 			case SHT_SUNW_syminfo:
5125981a172dSab196087 				flags |= FLG_SHOW_SYMINFO;
5126981a172dSab196087 				break;
5127981a172dSab196087 
5128981a172dSab196087 			case SHT_SUNW_verdef:
5129981a172dSab196087 			case SHT_SUNW_verneed:
5130981a172dSab196087 				flags |= FLG_SHOW_VERSIONS;
5131981a172dSab196087 				break;
5132981a172dSab196087 
5133981a172dSab196087 			case SHT_AMD64_UNWIND:
5134981a172dSab196087 				flags |= FLG_SHOW_UNWIND;
5135981a172dSab196087 				break;
5136981a172dSab196087 			}
5137981a172dSab196087 		}
5138981a172dSab196087 	}
5139981a172dSab196087 
5140981a172dSab196087 
5141981a172dSab196087 	if (flags & FLG_SHOW_SHDR)
51424f680cc6SAli Bahrami 		sections(file, cache, shnum, ehdr, osabi);
51437c478bd9Sstevel@tonic-gate 
5144981a172dSab196087 	if (flags & FLG_SHOW_INTERP)
51455aefb655Srie 		interp(file, cache, shnum, phnum, elf);
51467c478bd9Sstevel@tonic-gate 
51474f680cc6SAli Bahrami 	if ((osabi == ELFOSABI_SOLARIS) || (osabi == ELFOSABI_LINUX))
51483b41b08bSab196087 		versions(cache, shnum, file, flags, &versym);
51497c478bd9Sstevel@tonic-gate 
5150981a172dSab196087 	if (flags & FLG_SHOW_SYMBOLS)
51514f680cc6SAli Bahrami 		symbols(cache, shnum, ehdr, osabi, &versym, file, flags);
51527c478bd9Sstevel@tonic-gate 
51534f680cc6SAli Bahrami 	if ((flags & FLG_SHOW_SORT) && (osabi == ELFOSABI_SOLARIS))
51544f680cc6SAli Bahrami 		sunw_sort(cache, shnum, ehdr, osabi, &versym, file, flags);
5155d579eb63Sab196087 
5156981a172dSab196087 	if (flags & FLG_SHOW_HASH)
5157c809c407Sab196087 		hash(cache, shnum, file, flags);
51587c478bd9Sstevel@tonic-gate 
5159981a172dSab196087 	if (flags & FLG_SHOW_GOT)
51600e233487SRod Evans 		got(cache, shnum, ehdr, file);
51617c478bd9Sstevel@tonic-gate 
5162981a172dSab196087 	if (flags & FLG_SHOW_GROUP)
5163c809c407Sab196087 		group(cache, shnum, file, flags);
51647c478bd9Sstevel@tonic-gate 
5165981a172dSab196087 	if (flags & FLG_SHOW_SYMINFO)
5166d444b03eSAli Bahrami 		syminfo(cache, shnum, ehdr, osabi, file);
51677c478bd9Sstevel@tonic-gate 
5168981a172dSab196087 	if (flags & FLG_SHOW_RELOC)
51690e233487SRod Evans 		reloc(cache, shnum, ehdr, file);
51707c478bd9Sstevel@tonic-gate 
5171981a172dSab196087 	if (flags & FLG_SHOW_DYNAMIC)
51724f680cc6SAli Bahrami 		dynamic(cache, shnum, ehdr, osabi, file);
51737c478bd9Sstevel@tonic-gate 
5174c6c9aed4Sab196087 	if (flags & FLG_SHOW_NOTE) {
5175c6c9aed4Sab196087 		Word	note_cnt;
5176c6c9aed4Sab196087 		size_t	note_shnum;
5177c6c9aed4Sab196087 		Cache	*note_cache;
5178c6c9aed4Sab196087 
5179c6c9aed4Sab196087 		note_cnt = note(cache, shnum, ehdr, file);
5180c6c9aed4Sab196087 
5181c6c9aed4Sab196087 		/*
5182c6c9aed4Sab196087 		 * Solaris core files have section headers, but these
5183c6c9aed4Sab196087 		 * headers do not include SHT_NOTE sections that reference
5184c6c9aed4Sab196087 		 * the core note sections. This means that note() won't
5185c6c9aed4Sab196087 		 * find the core notes. Fake section headers (-P option)
5186c6c9aed4Sab196087 		 * recover these sections, but it is inconvenient to require
5187c6c9aed4Sab196087 		 * users to specify -P in this situation. If the following
5188c6c9aed4Sab196087 		 * are all true:
5189c6c9aed4Sab196087 		 *
5190c6c9aed4Sab196087 		 *	- No note sections were found
5191c6c9aed4Sab196087 		 *	- This is a core file
5192c6c9aed4Sab196087 		 *	- We are not already using fake section headers
5193c6c9aed4Sab196087 		 *
5194c6c9aed4Sab196087 		 * then we will automatically generate fake section headers
5195c6c9aed4Sab196087 		 * and then process them in a second call to note().
5196c6c9aed4Sab196087 		 */
5197c6c9aed4Sab196087 		if ((note_cnt == 0) && (ehdr->e_type == ET_CORE) &&
5198c6c9aed4Sab196087 		    !(flags & FLG_CTL_FAKESHDR) &&
5199c6c9aed4Sab196087 		    (fake_shdr_cache(file, fd, elf, ehdr,
5200c6c9aed4Sab196087 		    &note_cache, &note_shnum) != 0)) {
5201c6c9aed4Sab196087 			(void) note(note_cache, note_shnum, ehdr, file);
5202c6c9aed4Sab196087 			fake_shdr_cache_free(note_cache, note_shnum);
5203c6c9aed4Sab196087 		}
5204c6c9aed4Sab196087 	}
52057c478bd9Sstevel@tonic-gate 
52064f680cc6SAli Bahrami 	if ((flags & FLG_SHOW_MOVE) && (osabi == ELFOSABI_SOLARIS))
5207c809c407Sab196087 		move(cache, shnum, file, flags);
52087c478bd9Sstevel@tonic-gate 
5209981a172dSab196087 	if (flags & FLG_CALC_CHECKSUM)
52107c478bd9Sstevel@tonic-gate 		checksum(elf);
52117c478bd9Sstevel@tonic-gate 
52124f680cc6SAli Bahrami 	if ((flags & FLG_SHOW_CAP) && (osabi == ELFOSABI_SOLARIS))
521308278a5eSRod Evans 		cap(file, cache, shnum, phnum, ehdr, osabi, elf, flags);
52147c478bd9Sstevel@tonic-gate 
52154f680cc6SAli Bahrami 	if ((flags & FLG_SHOW_UNWIND) &&
52164f680cc6SAli Bahrami 	    ((osabi == ELFOSABI_SOLARIS) || (osabi == ELFOSABI_LINUX)))
52174f680cc6SAli Bahrami 		unwind(cache, shnum, phnum, ehdr, osabi, file, elf, flags);
52187c478bd9Sstevel@tonic-gate 
521939773e46Sab196087 
522039773e46Sab196087 	/* Release the memory used to cache section headers */
5221981a172dSab196087 done:
5222981a172dSab196087 	if (flags & FLG_CTL_FAKESHDR)
522339773e46Sab196087 		fake_shdr_cache_free(cache, shnum);
522439773e46Sab196087 	else
52257c478bd9Sstevel@tonic-gate 		free(cache);
5226981a172dSab196087 
5227981a172dSab196087 	return (ret);
52287c478bd9Sstevel@tonic-gate }
5229