xref: /titanic_50/usr/src/cmd/sgs/elfdump/common/elfdump.c (revision d47ced1f1801c1c1ca309ff7c5997bf0c8cb4092)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Copyright (c) 2015, Joyent, Inc. All rights reserved.
29  */
30 
31 /*
32  * Dump an elf file.
33  */
34 #include	<stddef.h>
35 #include	<sys/elf_386.h>
36 #include	<sys/elf_amd64.h>
37 #include	<sys/elf_SPARC.h>
38 #include	<_libelf.h>
39 #include	<dwarf.h>
40 #include	<stdio.h>
41 #include	<unistd.h>
42 #include	<errno.h>
43 #include	<strings.h>
44 #include	<debug.h>
45 #include	<conv.h>
46 #include	<msg.h>
47 #include	<_elfdump.h>
48 
49 
50 /*
51  * VERSYM_STATE is used to maintain information about the VERSYM section
52  * in the object being analyzed. It is filled in by versions(), and used
53  * by init_symtbl_state() when displaying symbol information.
54  *
55  * There are three forms of symbol versioning known to us:
56  *
57  * 1) The original form, introduced with Solaris 2.5, in which
58  *	the Versym contains indexes to Verdef records, and the
59  *	Versym values for UNDEF symbols resolved by other objects
60  *	are all set to 0.
61  * 2) The GNU form, which is backward compatible with the original
62  *	Solaris form, but which adds several extensions:
63  *	- The Versym also contains indexes to Verneed records, recording
64  *		which object/version contributed the external symbol at
65  *		link time. These indexes start with the next value following
66  *		the final Verdef index. The index is written to the previously
67  *		reserved vna_other field of the ELF Vernaux structure.
68  *	- The top bit of the Versym value is no longer part of the index,
69  *		but is used as a "hidden bit" to prevent binding to the symbol.
70  *	- Multiple implementations of a given symbol, contained in varying
71  *		versions are allowed, using special assembler pseudo ops,
72  *		and encoded in the symbol name using '@' characters.
73  * 3) Modified Solaris form, in which we adopt the first GNU extension
74  *	(Versym indexes to Verneed records), but not the others.
75  *
76  * elfdump can handle any of these cases. The presence of a DT_VERSYM
77  * dynamic element indicates a full GNU object. An object that lacks
78  * a DT_VERSYM entry, but which has non-zero vna_other fields in the Vernaux
79  * structures is a modified Solaris object. An object that has neither of
80  * these uses the original form.
81  *
82  * max_verndx contains the largest version index that can appear
83  * in a Versym entry. This can never be less than 1: In the case where
84  * there is no verdef/verneed sections, the [0] index is reserved
85  * for local symbols, and the [1] index for globals. If the original
86  * Solaris versioning rules are in effect and there is a verdef section,
87  * then max_verndex is the number of defined versions. If one of the
88  * other versioning forms is in effect, then:
89  *	1) If there is no verneed section, it is the same as for
90  *		original Solaris versioning.
91  *	2) If there is a verneed section, the vna_other field of the
92  *		Vernaux structs contain versions, and max_verndx is the
93  *		largest such index.
94  *
95  * If gnu_full is True, the object uses the full GNU form of versioning.
96  * The value of the gnu_full field is based on the presence of
97  * a DT_VERSYM entry in the dynamic section: GNU ld produces these, and
98  * Solaris ld does not.
99  *
100  * The gnu_needed field is True if the Versym contains indexes to
101  * Verneed records, as indicated by non-zero vna_other fields in the Verneed
102  * section. If gnu_full is True, then gnu_needed will always be true.
103  * However, gnu_needed can be true without gnu_full. This is the modified
104  * Solaris form.
105  */
106 typedef struct {
107 	Cache	*cache;		/* Pointer to cache entry for VERSYM */
108 	Versym	*data;		/* Pointer to versym array */
109 	int	gnu_full;	/* True if object uses GNU versioning rules */
110 	int	gnu_needed;	/* True if object uses VERSYM indexes for */
111 				/*	VERNEED (subset of gnu_full) */
112 	int	max_verndx;	/* largest versym index value */
113 } VERSYM_STATE;
114 
115 /*
116  * SYMTBL_STATE is used to maintain information about a single symbol
117  * table section, for use by the routines that display symbol information.
118  */
119 typedef struct {
120 	const char	*file;		/* Name of file */
121 	Ehdr		*ehdr;		/* ELF header for file */
122 	Cache		*cache;		/* Cache of all section headers */
123 	uchar_t		osabi;		/* OSABI to use */
124 	Word		shnum;		/* # of sections in cache */
125 	Cache		*seccache;	/* Cache of symbol table section hdr */
126 	Word		secndx;		/* Index of symbol table section hdr */
127 	const char	*secname;	/* Name of section */
128 	uint_t		flags;		/* Command line option flags */
129 	struct {			/* Extended section index data */
130 		int	checked;	/* TRUE if already checked for shxndx */
131 		Word	*data;		/* NULL, or extended section index */
132 					/*	used for symbol table entries */
133 		uint_t	n;		/* # items in shxndx.data */
134 	} shxndx;
135 	VERSYM_STATE	*versym;	/* NULL, or associated VERSYM section */
136 	Sym 		*sym;		/* Array of symbols */
137 	Word		symn;		/* # of symbols */
138 } SYMTBL_STATE;
139 
140 /*
141  * A variable of this type is used to track information related to
142  * .eh_frame and .eh_frame_hdr sections across calls to unwind_eh_frame().
143  */
144 typedef struct {
145 	Word		frame_cnt;	/* # .eh_frame sections seen */
146 	Word		frame_ndx;	/* Section index of 1st .eh_frame */
147 	Word		hdr_cnt;	/* # .eh_frame_hdr sections seen */
148 	Word		hdr_ndx;	/* Section index of 1st .eh_frame_hdr */
149 	uint64_t	frame_ptr;	/* Value of FramePtr field from first */
150 					/*	.eh_frame_hdr section */
151 	uint64_t	frame_base;	/* Data addr of 1st .eh_frame  */
152 } gnu_eh_state_t;
153 
154 /*
155  * C++ .exception_ranges entries make use of the signed ptrdiff_t
156  * type to record self-relative pointer values. We need a type
157  * for this that is matched to the ELFCLASS being processed.
158  */
159 #if	defined(_ELF64)
160 	typedef int64_t PTRDIFF_T;
161 #else
162 	typedef int32_t PTRDIFF_T;
163 #endif
164 
165 /*
166  * The Sun C++ ABI uses this struct to define each .exception_ranges
167  * entry. From the ABI:
168  *
169  * The field ret_addr is a self relative pointer to the start of the address
170  * range. The name was chosen because in the current implementation the range
171  * typically starts at the return address for a call site.
172  *
173  * The field length is the difference, in bytes, between the pc of the last
174  * instruction covered by the exception range and the first. When only a
175  * single call site is represented without optimization, this will equal zero.
176  *
177  * The field handler_addr is a relative pointer which stores the difference
178  * between the start of the exception range and the address of all code to
179  * catch exceptions and perform the cleanup for stack unwinding.
180  *
181  * The field type_block is a relative pointer which stores the difference
182  * between the start of the exception range and the address of an array used
183  * for storing a list of the types of exceptions which can be caught within
184  * the exception range.
185  */
186 typedef struct {
187 	PTRDIFF_T	ret_addr;
188 	Xword		length;
189 	PTRDIFF_T	handler_addr;
190 	PTRDIFF_T	type_block;
191 	Xword		reserved;
192 } exception_range_entry;
193 
194 /*
195  * Focal point for verifying symbol names.
196  */
197 static const char *
198 string(Cache *refsec, Word ndx, Cache *strsec, const char *file, Word name)
199 {
200 	/*
201 	 * If an error in this routine is due to a property of the string
202 	 * section, as opposed to a bad offset into the section (a property of
203 	 * the referencing section), then we will detect the same error on
204 	 * every call involving those sections. We use these static variables
205 	 * to retain the information needed to only issue each such error once.
206 	 */
207 	static Cache	*last_refsec;	/* Last referencing section seen */
208 	static int	strsec_err;	/* True if error issued */
209 
210 	const char	*strs;
211 	Word		strn;
212 
213 	if (strsec->c_data == NULL)
214 		return (NULL);
215 
216 	strs = (char *)strsec->c_data->d_buf;
217 	strn = strsec->c_data->d_size;
218 
219 	/*
220 	 * We only print a diagnostic regarding a bad string table once per
221 	 * input section being processed. If the refsec has changed, reset
222 	 * our retained error state.
223 	 */
224 	if (last_refsec != refsec) {
225 		last_refsec = refsec;
226 		strsec_err = 0;
227 	}
228 
229 	/* Verify that strsec really is a string table */
230 	if (strsec->c_shdr->sh_type != SHT_STRTAB) {
231 		if (!strsec_err) {
232 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_NOTSTRTAB),
233 			    file, strsec->c_ndx, refsec->c_ndx);
234 			strsec_err = 1;
235 		}
236 		return (MSG_INTL(MSG_STR_UNKNOWN));
237 	}
238 
239 	/*
240 	 * Is the string table offset within range of the available strings?
241 	 */
242 	if (name >= strn) {
243 		/*
244 		 * Do we have a empty string table?
245 		 */
246 		if (strs == NULL) {
247 			if (!strsec_err) {
248 				(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
249 				    file, strsec->c_name);
250 				strsec_err = 1;
251 			}
252 		} else {
253 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSTOFF),
254 			    file, refsec->c_name, EC_WORD(ndx), strsec->c_name,
255 			    EC_WORD(name), EC_WORD(strn - 1));
256 		}
257 
258 		/*
259 		 * Return the empty string so that the calling function can
260 		 * continue it's output diagnostics.
261 		 */
262 		return (MSG_INTL(MSG_STR_UNKNOWN));
263 	}
264 	return (strs + name);
265 }
266 
267 /*
268  * Relocations can reference section symbols and standard symbols.  If the
269  * former, establish the section name.
270  */
271 static const char *
272 relsymname(Cache *cache, Cache *csec, Cache *strsec, Word symndx, Word symnum,
273     Word relndx, Sym *syms, char *secstr, size_t secsz, const char *file)
274 {
275 	Sym		*sym;
276 	const char	*name;
277 
278 	if (symndx >= symnum) {
279 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_RELBADSYMNDX),
280 		    file, EC_WORD(symndx), EC_WORD(relndx));
281 		return (MSG_INTL(MSG_STR_UNKNOWN));
282 	}
283 
284 	sym = (Sym *)(syms + symndx);
285 	name = string(csec, symndx, strsec, file, sym->st_name);
286 
287 	/*
288 	 * If the symbol represents a section offset construct an appropriate
289 	 * string.  Note, although section symbol table entries typically have
290 	 * a NULL name pointer, entries do exist that point into the string
291 	 * table to their own NULL strings.
292 	 */
293 	if ((ELF_ST_TYPE(sym->st_info) == STT_SECTION) &&
294 	    ((sym->st_name == 0) || (*name == '\0'))) {
295 		(void) snprintf(secstr, secsz, MSG_INTL(MSG_STR_SECTION),
296 		    cache[sym->st_shndx].c_name);
297 		return ((const char *)secstr);
298 	}
299 
300 	return (name);
301 }
302 
303 /*
304  * Focal point for establishing a string table section.  Data such as the
305  * dynamic information simply points to a string table.  Data such as
306  * relocations, reference a symbol table, which in turn is associated with a
307  * string table.
308  */
309 static int
310 stringtbl(Cache *cache, int symtab, Word ndx, Word shnum, const char *file,
311     Word *symnum, Cache **symsec, Cache **strsec)
312 {
313 	Shdr	*shdr = cache[ndx].c_shdr;
314 
315 	if (symtab) {
316 		/*
317 		 * Validate the symbol table section.
318 		 */
319 		if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
320 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
321 			    file, cache[ndx].c_name, EC_WORD(shdr->sh_link));
322 			return (0);
323 		}
324 		if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
325 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
326 			    file, cache[ndx].c_name);
327 			return (0);
328 		}
329 
330 		/*
331 		 * Obtain, and verify the symbol table data.
332 		 */
333 		if ((cache[ndx].c_data == NULL) ||
334 		    (cache[ndx].c_data->d_buf == NULL)) {
335 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
336 			    file, cache[ndx].c_name);
337 			return (0);
338 		}
339 
340 		/*
341 		 * Establish the string table index.
342 		 */
343 		ndx = shdr->sh_link;
344 		shdr = cache[ndx].c_shdr;
345 
346 		/*
347 		 * Return symbol table information.
348 		 */
349 		if (symnum)
350 			*symnum = (shdr->sh_size / shdr->sh_entsize);
351 		if (symsec)
352 			*symsec = &cache[ndx];
353 	}
354 
355 	/*
356 	 * Validate the associated string table section.
357 	 */
358 	if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
359 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
360 		    file, cache[ndx].c_name, EC_WORD(shdr->sh_link));
361 		return (0);
362 	}
363 
364 	if (strsec)
365 		*strsec = &cache[shdr->sh_link];
366 
367 	return (1);
368 }
369 
370 /*
371  * Lookup a symbol and set Sym accordingly.
372  *
373  * entry:
374  *	name - Name of symbol to lookup
375  *	cache - Cache of all section headers
376  *	shnum - # of sections in cache
377  *	sym - Address of pointer to receive symbol
378  *	target - NULL, or section to which the symbol must be associated.
379  *	symtab - Symbol table to search for symbol
380  *	file - Name of file
381  *
382  * exit:
383  *	If the symbol is found, *sym is set to reference it, and True is
384  *	returned. If target is non-NULL, the symbol must reference the given
385  *	section --- otherwise the section is not checked.
386  *
387  *	If no symbol is found, False is returned.
388  */
389 static int
390 symlookup(const char *name, Cache *cache, Word shnum, Sym **sym,
391     Cache *target, Cache *symtab, const char *file)
392 {
393 	Shdr	*shdr;
394 	Word	symn, cnt;
395 	Sym	*syms;
396 
397 	if (symtab == 0)
398 		return (0);
399 
400 	shdr = symtab->c_shdr;
401 
402 	/*
403 	 * Determine the symbol data and number.
404 	 */
405 	if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
406 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
407 		    file, symtab->c_name);
408 		return (0);
409 	}
410 	if (symtab->c_data == NULL)
411 		return (0);
412 
413 	/* LINTED */
414 	symn = (Word)(shdr->sh_size / shdr->sh_entsize);
415 	syms = (Sym *)symtab->c_data->d_buf;
416 
417 	/*
418 	 * Get the associated string table section.
419 	 */
420 	if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
421 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
422 		    file, symtab->c_name, EC_WORD(shdr->sh_link));
423 		return (0);
424 	}
425 
426 	/*
427 	 * Loop through the symbol table to find a match.
428 	 */
429 	*sym = NULL;
430 	for (cnt = 0; cnt < symn; syms++, cnt++) {
431 		const char	*symname;
432 
433 		symname = string(symtab, cnt, &cache[shdr->sh_link], file,
434 		    syms->st_name);
435 
436 		if (symname && (strcmp(name, symname) == 0) &&
437 		    ((target == NULL) || (target->c_ndx == syms->st_shndx))) {
438 			/*
439 			 * It is possible, though rare, for a local and
440 			 * global symbol of the same name to exist, each
441 			 * contributed by a different input object. If the
442 			 * symbol just found is local, remember it, but
443 			 * continue looking.
444 			 */
445 			*sym = syms;
446 			if (ELF_ST_BIND(syms->st_info) != STB_LOCAL)
447 				break;
448 		}
449 	}
450 
451 	return (*sym != NULL);
452 }
453 
454 /*
455  * Print section headers.
456  */
457 static void
458 sections(const char *file, Cache *cache, Word shnum, Ehdr *ehdr, uchar_t osabi)
459 {
460 	size_t	seccnt;
461 
462 	for (seccnt = 1; seccnt < shnum; seccnt++) {
463 		Cache		*_cache = &cache[seccnt];
464 		Shdr		*shdr = _cache->c_shdr;
465 		const char	*secname = _cache->c_name;
466 
467 		/*
468 		 * Although numerous section header entries can be zero, it's
469 		 * usually a sign of trouble if the type is zero.
470 		 */
471 		if (shdr->sh_type == 0) {
472 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHTYPE),
473 			    file, secname, EC_WORD(shdr->sh_type));
474 		}
475 
476 		if (!match(MATCH_F_ALL, secname, seccnt, shdr->sh_type))
477 			continue;
478 
479 		/*
480 		 * Identify any sections that are suspicious.  A .got section
481 		 * shouldn't exist in a relocatable object.
482 		 */
483 		if (ehdr->e_type == ET_REL) {
484 			if (strncmp(secname, MSG_ORIG(MSG_ELF_GOT),
485 			    MSG_ELF_GOT_SIZE) == 0) {
486 				(void) fprintf(stderr,
487 				    MSG_INTL(MSG_GOT_UNEXPECTED), file,
488 				    secname);
489 			}
490 		}
491 
492 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
493 		dbg_print(0, MSG_INTL(MSG_ELF_SHDR), EC_WORD(seccnt), secname);
494 		Elf_shdr(0, osabi, ehdr->e_machine, shdr);
495 	}
496 }
497 
498 /*
499  * Obtain a specified Phdr entry.
500  */
501 static Phdr *
502 getphdr(Word phnum, Word *type_arr, Word type_cnt, const char *file, Elf *elf)
503 {
504 	Word	cnt, tcnt;
505 	Phdr	*phdr;
506 
507 	if ((phdr = elf_getphdr(elf)) == NULL) {
508 		failure(file, MSG_ORIG(MSG_ELF_GETPHDR));
509 		return (NULL);
510 	}
511 
512 	for (cnt = 0; cnt < phnum; phdr++, cnt++) {
513 		for (tcnt = 0; tcnt < type_cnt; tcnt++) {
514 			if (phdr->p_type == type_arr[tcnt])
515 				return (phdr);
516 		}
517 	}
518 	return (NULL);
519 }
520 
521 /*
522  * Display the contents of GNU/amd64 .eh_frame and .eh_frame_hdr
523  * sections.
524  *
525  * entry:
526  *	cache - Cache of all section headers
527  *	shndx - Index of .eh_frame or .eh_frame_hdr section to be displayed
528  *	shnum - Total number of sections which exist
529  *	uphdr - NULL, or unwind program header associated with
530  *		the .eh_frame_hdr section.
531  *	ehdr - ELF header for file
532  *	eh_state - Data used across calls to this routine. The
533  *		caller should zero it before the first call, and
534  *		pass it on every call.
535  *	osabi - OSABI to use in displaying information
536  *	file - Name of file
537  *	flags - Command line option flags
538  */
539 static void
540 unwind_eh_frame(Cache *cache, Word shndx, Word shnum, Phdr *uphdr, Ehdr *ehdr,
541     gnu_eh_state_t *eh_state, uchar_t osabi, const char *file, uint_t flags)
542 {
543 #if	defined(_ELF64)
544 #define	MSG_UNW_BINSRTAB2	MSG_UNW_BINSRTAB2_64
545 #define	MSG_UNW_BINSRTABENT	MSG_UNW_BINSRTABENT_64
546 #else
547 #define	MSG_UNW_BINSRTAB2	MSG_UNW_BINSRTAB2_32
548 #define	MSG_UNW_BINSRTABENT	MSG_UNW_BINSRTABENT_32
549 #endif
550 
551 	Cache			*_cache = &cache[shndx];
552 	Shdr			*shdr = _cache->c_shdr;
553 	uchar_t			*data = (uchar_t *)(_cache->c_data->d_buf);
554 	size_t			datasize = _cache->c_data->d_size;
555 	Conv_dwarf_ehe_buf_t	dwarf_ehe_buf;
556 	uint64_t		ndx, frame_ptr, fde_cnt, tabndx;
557 	uint_t			vers, frame_ptr_enc, fde_cnt_enc, table_enc;
558 	uint64_t		initloc, initloc0 = 0;
559 	uint64_t		gotaddr = 0;
560 	int			cnt;
561 
562 	for (cnt = 1; cnt < shnum; cnt++) {
563 		if (strncmp(cache[cnt].c_name, MSG_ORIG(MSG_ELF_GOT),
564 		    MSG_ELF_GOT_SIZE) == 0) {
565 			gotaddr = cache[cnt].c_shdr->sh_addr;
566 			break;
567 		}
568 	}
569 
570 	if ((data == NULL) || (datasize == 0)) {
571 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
572 		    file, _cache ->c_name);
573 		return;
574 	}
575 
576 	/*
577 	 * Is this a .eh_frame_hdr?
578 	 */
579 	if ((uphdr && (shdr->sh_addr == uphdr->p_vaddr)) ||
580 	    (strncmp(_cache->c_name, MSG_ORIG(MSG_SCN_FRMHDR),
581 	    MSG_SCN_FRMHDR_SIZE) == 0)) {
582 		/*
583 		 * There can only be a single .eh_frame_hdr.
584 		 * Flag duplicates.
585 		 */
586 		if (++eh_state->hdr_cnt > 1)
587 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_MULTEHFRMHDR),
588 			    file, EC_WORD(shndx), _cache->c_name);
589 
590 		dbg_print(0, MSG_ORIG(MSG_UNW_FRMHDR));
591 		ndx = 0;
592 
593 		vers = data[ndx++];
594 		frame_ptr_enc = data[ndx++];
595 		fde_cnt_enc = data[ndx++];
596 		table_enc = data[ndx++];
597 
598 		dbg_print(0, MSG_ORIG(MSG_UNW_FRMVERS), vers);
599 
600 		switch (dwarf_ehe_extract(data, datasize, &ndx,
601 		    &frame_ptr, frame_ptr_enc, ehdr->e_ident, B_TRUE,
602 		    shdr->sh_addr, ndx, gotaddr)) {
603 		case DW_OVERFLOW:
604 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_DWOVRFLW),
605 			    file, _cache->c_name);
606 			return;
607 		case DW_BAD_ENCODING:
608 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_DWBADENC),
609 			    file, _cache->c_name, frame_ptr_enc);
610 			return;
611 		case DW_SUCCESS:
612 			break;
613 		}
614 		if (eh_state->hdr_cnt == 1) {
615 			eh_state->hdr_ndx = shndx;
616 			eh_state->frame_ptr = frame_ptr;
617 		}
618 
619 		dbg_print(0, MSG_ORIG(MSG_UNW_FRPTRENC),
620 		    conv_dwarf_ehe(frame_ptr_enc, &dwarf_ehe_buf),
621 		    EC_XWORD(frame_ptr));
622 
623 		switch (dwarf_ehe_extract(data, datasize, &ndx, &fde_cnt,
624 		    fde_cnt_enc, ehdr->e_ident, B_TRUE, shdr->sh_addr, ndx,
625 		    gotaddr)) {
626 		case DW_OVERFLOW:
627 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_DWOVRFLW),
628 			    file, _cache->c_name);
629 			return;
630 		case DW_BAD_ENCODING:
631 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_DWBADENC),
632 			    file, _cache->c_name, fde_cnt_enc);
633 			return;
634 		case DW_SUCCESS:
635 			break;
636 		}
637 
638 		dbg_print(0, MSG_ORIG(MSG_UNW_FDCNENC),
639 		    conv_dwarf_ehe(fde_cnt_enc, &dwarf_ehe_buf),
640 		    EC_XWORD(fde_cnt));
641 		dbg_print(0, MSG_ORIG(MSG_UNW_TABENC),
642 		    conv_dwarf_ehe(table_enc, &dwarf_ehe_buf));
643 		dbg_print(0, MSG_ORIG(MSG_UNW_BINSRTAB1));
644 		dbg_print(0, MSG_ORIG(MSG_UNW_BINSRTAB2));
645 
646 		for (tabndx = 0; tabndx < fde_cnt; tabndx++) {
647 			uint64_t table;
648 
649 			switch (dwarf_ehe_extract(data, datasize, &ndx,
650 			    &initloc, table_enc, ehdr->e_ident, B_TRUE,
651 			    shdr->sh_addr, ndx, gotaddr)) {
652 			case DW_OVERFLOW:
653 				(void) fprintf(stderr,
654 				    MSG_INTL(MSG_ERR_DWOVRFLW), file,
655 				    _cache->c_name);
656 				return;
657 			case DW_BAD_ENCODING:
658 				(void) fprintf(stderr,
659 				    MSG_INTL(MSG_ERR_DWBADENC), file,
660 				    _cache->c_name, table_enc);
661 				return;
662 			case DW_SUCCESS:
663 				break;
664 			}
665 			if ((tabndx != 0) && (initloc0 > initloc))
666 				(void) fprintf(stderr,
667 				    MSG_INTL(MSG_ERR_BADSORT), file,
668 				    _cache->c_name, EC_WORD(tabndx));
669 			switch (dwarf_ehe_extract(data, datasize, &ndx, &table,
670 			    table_enc, ehdr->e_ident, B_TRUE, shdr->sh_addr,
671 			    ndx, gotaddr)) {
672 			case DW_OVERFLOW:
673 				(void) fprintf(stderr,
674 				    MSG_INTL(MSG_ERR_DWOVRFLW), file,
675 				    _cache->c_name);
676 				return;
677 			case DW_BAD_ENCODING:
678 				(void) fprintf(stderr,
679 				    MSG_INTL(MSG_ERR_DWBADENC), file,
680 				    _cache->c_name, table_enc);
681 				return;
682 			case DW_SUCCESS:
683 				break;
684 			}
685 
686 			dbg_print(0, MSG_ORIG(MSG_UNW_BINSRTABENT),
687 			    EC_XWORD(initloc),
688 			    EC_XWORD(table));
689 			initloc0 = initloc;
690 		}
691 	} else {		/* Display the .eh_frame section */
692 		eh_state->frame_cnt++;
693 		if (eh_state->frame_cnt == 1) {
694 			eh_state->frame_ndx = shndx;
695 			eh_state->frame_base = shdr->sh_addr;
696 		} else if ((eh_state->frame_cnt >  1) &&
697 		    (ehdr->e_type != ET_REL)) {
698 			Conv_inv_buf_t	inv_buf;
699 
700 			(void) fprintf(stderr, MSG_INTL(MSG_WARN_MULTEHFRM),
701 			    file, EC_WORD(shndx), _cache->c_name,
702 			    conv_ehdr_type(osabi, ehdr->e_type, 0, &inv_buf));
703 		}
704 		dump_eh_frame(file, _cache->c_name, data, datasize,
705 		    shdr->sh_addr, ehdr->e_machine, ehdr->e_ident, gotaddr);
706 	}
707 
708 	/*
709 	 * If we've seen the .eh_frame_hdr and the first .eh_frame section,
710 	 * compare the header frame_ptr to the address of the actual frame
711 	 * section to ensure the link-editor got this right.  Note, this
712 	 * diagnostic is only produced when unwind information is explicitly
713 	 * asked for, as shared objects built with an older ld(1) may reveal
714 	 * this inconsistency.  Although an inconsistency, it doesn't seem to
715 	 * have any adverse effect on existing tools.
716 	 */
717 	if (((flags & FLG_MASK_SHOW) != FLG_MASK_SHOW) &&
718 	    (eh_state->hdr_cnt > 0) && (eh_state->frame_cnt > 0) &&
719 	    (eh_state->frame_ptr != eh_state->frame_base))
720 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADEHFRMPTR),
721 		    file, EC_WORD(eh_state->hdr_ndx),
722 		    cache[eh_state->hdr_ndx].c_name,
723 		    EC_XWORD(eh_state->frame_ptr),
724 		    EC_WORD(eh_state->frame_ndx),
725 		    cache[eh_state->frame_ndx].c_name,
726 		    EC_XWORD(eh_state->frame_base));
727 #undef MSG_UNW_BINSRTAB2
728 #undef MSG_UNW_BINSRTABENT
729 }
730 
731 /*
732  * Convert a self relative pointer into an address. A self relative
733  * pointer adds the address where the pointer resides to the offset
734  * contained in the pointer. The benefit is that the value of the
735  * pointer does not require relocation.
736  *
737  * entry:
738  *	base_addr - Address of the pointer.
739  *	delta - Offset relative to base_addr giving desired address
740  *
741  * exit:
742  *	The computed address is returned.
743  *
744  * note:
745  *	base_addr is an unsigned value, while ret_addr is signed. This routine
746  *	used explicit testing and casting to explicitly control type
747  *	conversion, and ensure that we handle the maximum possible range.
748  */
749 static Addr
750 srelptr(Addr base_addr, PTRDIFF_T delta)
751 {
752 	if (delta < 0)
753 		return (base_addr - (Addr) (-delta));
754 
755 	return (base_addr + (Addr) delta);
756 }
757 
758 /*
759  * Byte swap a PTRDIFF_T value.
760  */
761 static PTRDIFF_T
762 swap_ptrdiff(PTRDIFF_T value)
763 {
764 	PTRDIFF_T r;
765 	uchar_t	*dst = (uchar_t *)&r;
766 	uchar_t	*src = (uchar_t *)&value;
767 
768 	UL_ASSIGN_BSWAP_XWORD(dst, src);
769 	return (r);
770 }
771 
772 /*
773  * Display exception_range_entry items from the .exception_ranges section
774  * of a Sun C++ object.
775  */
776 static void
777 unwind_exception_ranges(Cache *_cache, const char *file, int do_swap)
778 {
779 	/*
780 	 * Translate a PTRDIFF_T self-relative address field of
781 	 * an exception_range_entry struct into an address.
782 	 *
783 	 * entry:
784 	 *	exc_addr - Address of base of exception_range_entry struct
785 	 *	cur_ent - Pointer to data in the struct to be translated
786 	 *
787 	 *	_f - Field of struct to be translated
788 	 */
789 #define	SRELPTR(_f) \
790 	srelptr(exc_addr + offsetof(exception_range_entry, _f), cur_ent->_f)
791 
792 #if	defined(_ELF64)
793 #define	MSG_EXR_TITLE	MSG_EXR_TITLE_64
794 #define	MSG_EXR_ENTRY	MSG_EXR_ENTRY_64
795 #else
796 #define	MSG_EXR_TITLE	MSG_EXR_TITLE_32
797 #define	MSG_EXR_ENTRY	MSG_EXR_ENTRY_32
798 #endif
799 
800 	exception_range_entry	scratch, *ent, *cur_ent = &scratch;
801 	char			index[MAXNDXSIZE];
802 	Word			i, nelts;
803 	Addr			addr, addr0 = 0, offset = 0;
804 	Addr			exc_addr = _cache->c_shdr->sh_addr;
805 
806 	dbg_print(0, MSG_INTL(MSG_EXR_TITLE));
807 	ent = (exception_range_entry *)(_cache->c_data->d_buf);
808 	nelts = _cache->c_data->d_size / sizeof (exception_range_entry);
809 
810 	for (i = 0; i < nelts; i++, ent++) {
811 		if (do_swap) {
812 			/*
813 			 * Copy byte swapped values into the scratch buffer.
814 			 * The reserved field is not used, so we skip it.
815 			 */
816 			scratch.ret_addr = swap_ptrdiff(ent->ret_addr);
817 			scratch.length = BSWAP_XWORD(ent->length);
818 			scratch.handler_addr = swap_ptrdiff(ent->handler_addr);
819 			scratch.type_block = swap_ptrdiff(ent->type_block);
820 		} else {
821 			cur_ent = ent;
822 		}
823 
824 		/*
825 		 * The table is required to be sorted by the address
826 		 * derived from ret_addr, to allow binary searching. Ensure
827 		 * that addresses grow monotonically.
828 		 */
829 		addr = SRELPTR(ret_addr);
830 		if ((i != 0) && (addr0 > addr))
831 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSORT),
832 			    file, _cache->c_name, EC_WORD(i));
833 
834 		(void) snprintf(index, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INDEX),
835 		    EC_XWORD(i));
836 		dbg_print(0, MSG_INTL(MSG_EXR_ENTRY), index, EC_ADDR(offset),
837 		    EC_ADDR(addr), EC_ADDR(cur_ent->length),
838 		    EC_ADDR(SRELPTR(handler_addr)),
839 		    EC_ADDR(SRELPTR(type_block)));
840 
841 		addr0 = addr;
842 		exc_addr += sizeof (exception_range_entry);
843 		offset += sizeof (exception_range_entry);
844 	}
845 
846 #undef SRELPTR
847 #undef MSG_EXR_TITLE
848 #undef MSG_EXR_ENTRY
849 }
850 
851 /*
852  * Display information from unwind/exception sections:
853  *
854  * -	GNU/amd64 .eh_frame and .eh_frame_hdr
855  * -	Sun C++ .exception_ranges
856  *
857  */
858 static void
859 unwind(Cache *cache, Word shnum, Word phnum, Ehdr *ehdr, uchar_t osabi,
860     const char *file, Elf *elf, uint_t flags)
861 {
862 	static Word phdr_types[] = { PT_SUNW_UNWIND, PT_SUNW_EH_FRAME };
863 
864 	Word			cnt;
865 	Phdr			*uphdr = NULL;
866 	gnu_eh_state_t		eh_state;
867 
868 	/*
869 	 * Historical background: .eh_frame and .eh_frame_hdr sections
870 	 * come from the GNU compilers (particularly C++), and are used
871 	 * under all architectures. Their format is based on DWARF. When
872 	 * the amd64 ABI was defined, these sections were adopted wholesale
873 	 * from the existing practice.
874 	 *
875 	 * When amd64 support was added to Solaris, support for these
876 	 * sections was added, using the SHT_AMD64_UNWIND section type
877 	 * to identify them. At first, we ignored them in objects for
878 	 * non-amd64 targets, but later broadened our support to include
879 	 * other architectures in order to better support gcc-generated
880 	 * objects.
881 	 *
882 	 * .exception_ranges implement the same basic concepts, but
883 	 * were invented at Sun for the Sun C++ compiler.
884 	 *
885 	 * We match these sections by name, rather than section type,
886 	 * because they can come in as either SHT_AMD64_UNWIND, or as
887 	 * SHT_PROGBITS, and because the type isn't enough to determine
888 	 * how they should be interpreted.
889 	 */
890 	/* Find the program header for .eh_frame_hdr if present */
891 	if (phnum)
892 		uphdr = getphdr(phnum, phdr_types,
893 		    sizeof (phdr_types) / sizeof (*phdr_types), file, elf);
894 
895 	/*
896 	 * eh_state is used to retain data used by unwind_eh_frame()
897 	 * across calls.
898 	 */
899 	bzero(&eh_state, sizeof (eh_state));
900 
901 	for (cnt = 1; cnt < shnum; cnt++) {
902 		Cache		*_cache = &cache[cnt];
903 		Shdr		*shdr = _cache->c_shdr;
904 		int		is_exrange;
905 
906 		/*
907 		 * Skip sections of the wrong type. On amd64, they
908 		 * can be SHT_AMD64_UNWIND. On all platforms, they
909 		 * can be SHT_PROGBITS (including amd64, if using
910 		 * the GNU compilers).
911 		 *
912 		 * Skip anything other than these two types. The name
913 		 * test below will thin out the SHT_PROGBITS that don't apply.
914 		 */
915 		if ((shdr->sh_type != SHT_PROGBITS) &&
916 		    (shdr->sh_type != SHT_AMD64_UNWIND))
917 			continue;
918 
919 		/*
920 		 * Only sections with certain well known names are of interest.
921 		 * These are:
922 		 *
923 		 *	.eh_frame - amd64/GNU-compiler unwind sections
924 		 *	.eh_frame_hdr - Sorted table referencing .eh_frame
925 		 *	.exception_ranges - Sun C++ unwind sections
926 		 *
927 		 * We do a prefix comparison, allowing for naming conventions
928 		 * like .eh_frame.foo, hence the use of strncmp() rather than
929 		 * strcmp(). This means that we only really need to test for
930 		 * .eh_frame, as it's a prefix of .eh_frame_hdr.
931 		 */
932 		is_exrange =  strncmp(_cache->c_name,
933 		    MSG_ORIG(MSG_SCN_EXRANGE), MSG_SCN_EXRANGE_SIZE) == 0;
934 		if ((strncmp(_cache->c_name, MSG_ORIG(MSG_SCN_FRM),
935 		    MSG_SCN_FRM_SIZE) != 0) && !is_exrange)
936 			continue;
937 
938 		if (!match(MATCH_F_ALL, _cache->c_name, cnt, shdr->sh_type))
939 			continue;
940 
941 		if (_cache->c_data == NULL)
942 			continue;
943 
944 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
945 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_UNWIND), _cache->c_name);
946 
947 		if (is_exrange)
948 			unwind_exception_ranges(_cache, file,
949 			    _elf_sys_encoding() != ehdr->e_ident[EI_DATA]);
950 		else
951 			unwind_eh_frame(cache, cnt, shnum, uphdr, ehdr,
952 			    &eh_state, osabi, file, flags);
953 	}
954 }
955 
956 /*
957  * Initialize a symbol table state structure
958  *
959  * entry:
960  *	state - State structure to be initialized
961  *	cache - Cache of all section headers
962  *	shnum - # of sections in cache
963  *	secndx - Index of symbol table section
964  *	ehdr - ELF header for file
965  *	versym - Information about versym section
966  *	file - Name of file
967  *	flags - Command line option flags
968  */
969 static int
970 init_symtbl_state(SYMTBL_STATE *state, Cache *cache, Word shnum, Word secndx,
971     Ehdr *ehdr, uchar_t osabi, VERSYM_STATE *versym, const char *file,
972     uint_t flags)
973 {
974 	Shdr *shdr;
975 
976 	state->file = file;
977 	state->ehdr = ehdr;
978 	state->cache = cache;
979 	state->osabi = osabi;
980 	state->shnum = shnum;
981 	state->seccache = &cache[secndx];
982 	state->secndx = secndx;
983 	state->secname = state->seccache->c_name;
984 	state->flags = flags;
985 	state->shxndx.checked = 0;
986 	state->shxndx.data = NULL;
987 	state->shxndx.n = 0;
988 
989 	shdr = state->seccache->c_shdr;
990 
991 	/*
992 	 * Check the symbol data and per-item size.
993 	 */
994 	if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
995 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
996 		    file, state->secname);
997 		return (0);
998 	}
999 	if (state->seccache->c_data == NULL)
1000 		return (0);
1001 
1002 	/* LINTED */
1003 	state->symn = (Word)(shdr->sh_size / shdr->sh_entsize);
1004 	state->sym = (Sym *)state->seccache->c_data->d_buf;
1005 
1006 	/*
1007 	 * Check associated string table section.
1008 	 */
1009 	if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
1010 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
1011 		    file, state->secname, EC_WORD(shdr->sh_link));
1012 		return (0);
1013 	}
1014 
1015 	/*
1016 	 * Determine if there is a associated Versym section
1017 	 * with this Symbol Table.
1018 	 */
1019 	if (versym && versym->cache &&
1020 	    (versym->cache->c_shdr->sh_link == state->secndx))
1021 		state->versym = versym;
1022 	else
1023 		state->versym = NULL;
1024 
1025 
1026 	return (1);
1027 }
1028 
1029 /*
1030  * Determine the extended section index used for symbol tables entries.
1031  */
1032 static void
1033 symbols_getxindex(SYMTBL_STATE *state)
1034 {
1035 	uint_t	symn;
1036 	Word	symcnt;
1037 
1038 	state->shxndx.checked = 1;   /* Note that we've been called */
1039 	for (symcnt = 1; symcnt < state->shnum; symcnt++) {
1040 		Cache	*_cache = &state->cache[symcnt];
1041 		Shdr	*shdr = _cache->c_shdr;
1042 
1043 		if ((shdr->sh_type != SHT_SYMTAB_SHNDX) ||
1044 		    (shdr->sh_link != state->secndx))
1045 			continue;
1046 
1047 		if ((shdr->sh_entsize) &&
1048 		    /* LINTED */
1049 		    ((symn = (uint_t)(shdr->sh_size / shdr->sh_entsize)) == 0))
1050 			continue;
1051 
1052 		if (_cache->c_data == NULL)
1053 			continue;
1054 
1055 		state->shxndx.data = _cache->c_data->d_buf;
1056 		state->shxndx.n = symn;
1057 		return;
1058 	}
1059 }
1060 
1061 /*
1062  * Produce a line of output for the given symbol
1063  *
1064  * entry:
1065  *	state - Symbol table state
1066  *	symndx - Index of symbol within the table
1067  *	info - Value of st_info (indicates local/global range)
1068  *	symndx_disp - Index to display. This may not be the same
1069  *		as symndx if the display is relative to the logical
1070  *		combination of the SUNW_ldynsym/dynsym tables.
1071  *	sym - Symbol to display
1072  */
1073 static void
1074 output_symbol(SYMTBL_STATE *state, Word symndx, Word info, Word disp_symndx,
1075     Sym *sym)
1076 {
1077 	/*
1078 	 * Symbol types for which we check that the specified
1079 	 * address/size land inside the target section.
1080 	 */
1081 	static const int addr_symtype[] = {
1082 		0,			/* STT_NOTYPE */
1083 		1,			/* STT_OBJECT */
1084 		1,			/* STT_FUNC */
1085 		0,			/* STT_SECTION */
1086 		0,			/* STT_FILE */
1087 		1,			/* STT_COMMON */
1088 		0,			/* STT_TLS */
1089 		0,			/* 7 */
1090 		0,			/* 8 */
1091 		0,			/* 9 */
1092 		0,			/* 10 */
1093 		0,			/* 11 */
1094 		0,			/* 12 */
1095 		0,			/* STT_SPARC_REGISTER */
1096 		0,			/* 14 */
1097 		0,			/* 15 */
1098 	};
1099 #if STT_NUM != (STT_TLS + 1)
1100 #error "STT_NUM has grown. Update addr_symtype[]"
1101 #endif
1102 
1103 	char		index[MAXNDXSIZE];
1104 	const char	*symname, *sec;
1105 	Versym		verndx;
1106 	int		gnuver;
1107 	uchar_t		type;
1108 	Shdr		*tshdr;
1109 	Word		shndx;
1110 	Conv_inv_buf_t	inv_buf;
1111 
1112 	/* Ensure symbol index is in range */
1113 	if (symndx >= state->symn) {
1114 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSYMNDX),
1115 		    state->file, state->secname, EC_WORD(symndx));
1116 		return;
1117 	}
1118 
1119 	/*
1120 	 * If we are using extended symbol indexes, find the
1121 	 * corresponding SHN_SYMTAB_SHNDX table.
1122 	 */
1123 	if ((sym->st_shndx == SHN_XINDEX) && (state->shxndx.checked == 0))
1124 		symbols_getxindex(state);
1125 
1126 	/* LINTED */
1127 	symname = string(state->seccache, symndx,
1128 	    &state->cache[state->seccache->c_shdr->sh_link], state->file,
1129 	    sym->st_name);
1130 
1131 	tshdr = NULL;
1132 	sec = NULL;
1133 
1134 	if (state->ehdr->e_type == ET_CORE) {
1135 		sec = (char *)MSG_INTL(MSG_STR_UNKNOWN);
1136 	} else if (state->flags & FLG_CTL_FAKESHDR) {
1137 		/*
1138 		 * If we are using fake section headers derived from
1139 		 * the program headers, then the section indexes
1140 		 * in the symbols do not correspond to these headers.
1141 		 * The section names are not available, so all we can
1142 		 * do is to display them in numeric form.
1143 		 */
1144 		sec = conv_sym_shndx(state->osabi, state->ehdr->e_machine,
1145 		    sym->st_shndx, CONV_FMT_DECIMAL, &inv_buf);
1146 	} else if ((sym->st_shndx < SHN_LORESERVE) &&
1147 	    (sym->st_shndx < state->shnum)) {
1148 		shndx = sym->st_shndx;
1149 		tshdr = state->cache[shndx].c_shdr;
1150 		sec = state->cache[shndx].c_name;
1151 	} else if (sym->st_shndx == SHN_XINDEX) {
1152 		if (state->shxndx.data) {
1153 			Word	_shxndx;
1154 
1155 			if (symndx > state->shxndx.n) {
1156 				(void) fprintf(stderr,
1157 				    MSG_INTL(MSG_ERR_BADSYMXINDEX1),
1158 				    state->file, state->secname,
1159 				    EC_WORD(symndx));
1160 			} else if ((_shxndx =
1161 			    state->shxndx.data[symndx]) > state->shnum) {
1162 				(void) fprintf(stderr,
1163 				    MSG_INTL(MSG_ERR_BADSYMXINDEX2),
1164 				    state->file, state->secname,
1165 				    EC_WORD(symndx), EC_WORD(_shxndx));
1166 			} else {
1167 				shndx = _shxndx;
1168 				tshdr = state->cache[shndx].c_shdr;
1169 				sec = state->cache[shndx].c_name;
1170 			}
1171 		} else {
1172 			(void) fprintf(stderr,
1173 			    MSG_INTL(MSG_ERR_BADSYMXINDEX3),
1174 			    state->file, state->secname, EC_WORD(symndx));
1175 		}
1176 	} else if ((sym->st_shndx < SHN_LORESERVE) &&
1177 	    (sym->st_shndx >= state->shnum)) {
1178 		(void) fprintf(stderr,
1179 		    MSG_INTL(MSG_ERR_BADSYM5), state->file,
1180 		    state->secname, EC_WORD(symndx),
1181 		    demangle(symname, state->flags), sym->st_shndx);
1182 	}
1183 
1184 	/*
1185 	 * If versioning is available display the
1186 	 * version index. If not, then use 0.
1187 	 */
1188 	if (state->versym) {
1189 		Versym test_verndx;
1190 
1191 		verndx = test_verndx = state->versym->data[symndx];
1192 		gnuver = state->versym->gnu_full;
1193 
1194 		/*
1195 		 * Check to see if this is a defined symbol with a
1196 		 * version index that is outside the valid range for
1197 		 * the file. The interpretation of this depends on
1198 		 * the style of versioning used by the object.
1199 		 *
1200 		 * Versions >= VER_NDX_LORESERVE have special meanings,
1201 		 * and are exempt from this checking.
1202 		 *
1203 		 * GNU style version indexes use the top bit of the
1204 		 * 16-bit index value (0x8000) as the "hidden bit".
1205 		 * We must mask off this bit in order to compare
1206 		 * the version against the maximum value.
1207 		 */
1208 		if (gnuver)
1209 			test_verndx &= ~0x8000;
1210 
1211 		if ((test_verndx > state->versym->max_verndx) &&
1212 		    (verndx < VER_NDX_LORESERVE))
1213 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADVER),
1214 			    state->file, state->secname, EC_WORD(symndx),
1215 			    EC_HALF(test_verndx), state->versym->max_verndx);
1216 	} else {
1217 		verndx = 0;
1218 		gnuver = 0;
1219 	}
1220 
1221 	/*
1222 	 * Error checking for TLS.
1223 	 */
1224 	type = ELF_ST_TYPE(sym->st_info);
1225 	if (type == STT_TLS) {
1226 		if (tshdr &&
1227 		    (sym->st_shndx != SHN_UNDEF) &&
1228 		    ((tshdr->sh_flags & SHF_TLS) == 0)) {
1229 			(void) fprintf(stderr,
1230 			    MSG_INTL(MSG_ERR_BADSYM3), state->file,
1231 			    state->secname, EC_WORD(symndx),
1232 			    demangle(symname, state->flags));
1233 		}
1234 	} else if ((type != STT_SECTION) && sym->st_size &&
1235 	    tshdr && (tshdr->sh_flags & SHF_TLS)) {
1236 		(void) fprintf(stderr,
1237 		    MSG_INTL(MSG_ERR_BADSYM4), state->file,
1238 		    state->secname, EC_WORD(symndx),
1239 		    demangle(symname, state->flags));
1240 	}
1241 
1242 	/*
1243 	 * If a symbol with non-zero size has a type that
1244 	 * specifies an address, then make sure the location
1245 	 * it references is actually contained within the
1246 	 * section.  UNDEF symbols don't count in this case,
1247 	 * so we ignore them.
1248 	 *
1249 	 * The meaning of the st_value field in a symbol
1250 	 * depends on the type of object. For a relocatable
1251 	 * object, it is the offset within the section.
1252 	 * For sharable objects, it is the offset relative to
1253 	 * the base of the object, and for other types, it is
1254 	 * the virtual address. To get an offset within the
1255 	 * section for non-ET_REL files, we subtract the
1256 	 * base address of the section.
1257 	 */
1258 	if (addr_symtype[type] && (sym->st_size > 0) &&
1259 	    (sym->st_shndx != SHN_UNDEF) && ((sym->st_shndx < SHN_LORESERVE) ||
1260 	    (sym->st_shndx == SHN_XINDEX)) && (tshdr != NULL)) {
1261 		Word v = sym->st_value;
1262 			if (state->ehdr->e_type != ET_REL)
1263 				v -= tshdr->sh_addr;
1264 		if (((v + sym->st_size) > tshdr->sh_size)) {
1265 			(void) fprintf(stderr,
1266 			    MSG_INTL(MSG_ERR_BADSYM6), state->file,
1267 			    state->secname, EC_WORD(symndx),
1268 			    demangle(symname, state->flags),
1269 			    EC_WORD(shndx), EC_XWORD(tshdr->sh_size),
1270 			    EC_XWORD(sym->st_value), EC_XWORD(sym->st_size));
1271 		}
1272 	}
1273 
1274 	/*
1275 	 * A typical symbol table uses the sh_info field to indicate one greater
1276 	 * than the symbol table index of the last local symbol, STB_LOCAL.
1277 	 * Therefore, symbol indexes less than sh_info should have local
1278 	 * binding.  Symbol indexes greater than, or equal to sh_info, should
1279 	 * have global binding.  Note, we exclude UNDEF/NOTY symbols with zero
1280 	 * value and size, as these symbols may be the result of an mcs(1)
1281 	 * section deletion.
1282 	 */
1283 	if (info) {
1284 		uchar_t	bind = ELF_ST_BIND(sym->st_info);
1285 
1286 		if ((symndx < info) && (bind != STB_LOCAL)) {
1287 			(void) fprintf(stderr,
1288 			    MSG_INTL(MSG_ERR_BADSYM7), state->file,
1289 			    state->secname, EC_WORD(symndx),
1290 			    demangle(symname, state->flags), EC_XWORD(info));
1291 
1292 		} else if ((symndx >= info) && (bind == STB_LOCAL) &&
1293 		    ((sym->st_shndx != SHN_UNDEF) ||
1294 		    (ELF_ST_TYPE(sym->st_info) != STT_NOTYPE) ||
1295 		    (sym->st_size != 0) || (sym->st_value != 0))) {
1296 			(void) fprintf(stderr,
1297 			    MSG_INTL(MSG_ERR_BADSYM8), state->file,
1298 			    state->secname, EC_WORD(symndx),
1299 			    demangle(symname, state->flags), EC_XWORD(info));
1300 		}
1301 	}
1302 
1303 	(void) snprintf(index, MAXNDXSIZE,
1304 	    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(disp_symndx));
1305 	Elf_syms_table_entry(0, ELF_DBG_ELFDUMP, index, state->osabi,
1306 	    state->ehdr->e_machine, sym, verndx, gnuver, sec, symname);
1307 }
1308 
1309 /*
1310  * Process a SHT_SUNW_cap capabilities section.
1311  */
1312 static int
1313 cap_section(const char *file, Cache *cache, Word shnum, Cache *ccache,
1314     uchar_t osabi, Ehdr *ehdr, uint_t flags)
1315 {
1316 	SYMTBL_STATE	state;
1317 	Word		cnum, capnum, nulls, symcaps;
1318 	int		descapndx, objcap, title;
1319 	Cap		*cap = (Cap *)ccache->c_data->d_buf;
1320 	Shdr		*cishdr, *cshdr = ccache->c_shdr;
1321 	Cache		*cicache, *strcache;
1322 	Capinfo		*capinfo = NULL;
1323 	Word		capinfonum;
1324 	const char	*strs = NULL;
1325 	size_t		strs_size;
1326 
1327 	if ((cshdr->sh_entsize == 0) || (cshdr->sh_size == 0)) {
1328 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
1329 		    file, ccache->c_name);
1330 		return (0);
1331 	}
1332 
1333 	/*
1334 	 * If this capabilities section is associated with symbols, then the
1335 	 * sh_link field points to the associated capabilities information
1336 	 * section.  The sh_link field of the capabilities information section
1337 	 * points to the associated symbol table.
1338 	 */
1339 	if (cshdr->sh_link) {
1340 		Cache	*scache;
1341 		Shdr	*sshdr;
1342 
1343 		/*
1344 		 * Validate that the sh_link field points to a capabilities
1345 		 * information section.
1346 		 */
1347 		if (cshdr->sh_link >= shnum) {
1348 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
1349 			    file, ccache->c_name, EC_WORD(cshdr->sh_link));
1350 			return (0);
1351 		}
1352 
1353 		cicache = &cache[cshdr->sh_link];
1354 		cishdr = cicache->c_shdr;
1355 
1356 		if (cishdr->sh_type != SHT_SUNW_capinfo) {
1357 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_INVCAP),
1358 			    file, ccache->c_name, EC_WORD(cshdr->sh_link));
1359 			return (0);
1360 		}
1361 
1362 		capinfo = cicache->c_data->d_buf;
1363 		capinfonum = (Word)(cishdr->sh_size / cishdr->sh_entsize);
1364 
1365 		/*
1366 		 * Validate that the sh_link field of the capabilities
1367 		 * information section points to a valid symbol table.
1368 		 */
1369 		if ((cishdr->sh_link == 0) || (cishdr->sh_link >= shnum)) {
1370 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
1371 			    file, cicache->c_name, EC_WORD(cishdr->sh_link));
1372 			return (0);
1373 		}
1374 		scache = &cache[cishdr->sh_link];
1375 		sshdr = scache->c_shdr;
1376 
1377 		if ((sshdr->sh_type != SHT_SYMTAB) &&
1378 		    (sshdr->sh_type != SHT_DYNSYM)) {
1379 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_INVCAPINFO1),
1380 			    file, cicache->c_name, EC_WORD(cishdr->sh_link));
1381 			return (0);
1382 		}
1383 
1384 		if (!init_symtbl_state(&state, cache, shnum,
1385 		    cishdr->sh_link, ehdr, osabi, NULL, file, flags))
1386 			return (0);
1387 	}
1388 
1389 	/*
1390 	 * If this capabilities section contains capability string entries,
1391 	 * then determine the associated string table.  Capabilities entries
1392 	 * that define names require that the capability section indicate
1393 	 * which string table to use via sh_info.
1394 	 */
1395 	if (cshdr->sh_info) {
1396 		Shdr	*strshdr;
1397 
1398 		/*
1399 		 * Validate that the sh_info field points to a string table.
1400 		 */
1401 		if (cshdr->sh_info >= shnum) {
1402 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
1403 			    file, ccache->c_name, EC_WORD(cshdr->sh_info));
1404 			return (0);
1405 		}
1406 
1407 		strcache = &cache[cshdr->sh_info];
1408 		strshdr = strcache->c_shdr;
1409 
1410 		if (strshdr->sh_type != SHT_STRTAB) {
1411 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_INVCAP),
1412 			    file, ccache->c_name, EC_WORD(cshdr->sh_info));
1413 			return (0);
1414 		}
1415 		strs = (const char *)strcache->c_data->d_buf;
1416 		strs_size = strcache->c_data->d_size;
1417 	}
1418 
1419 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1420 	dbg_print(0, MSG_INTL(MSG_ELF_SCN_CAP), ccache->c_name);
1421 
1422 	capnum = (Word)(cshdr->sh_size / cshdr->sh_entsize);
1423 
1424 	nulls = symcaps = 0;
1425 	objcap = title = 1;
1426 	descapndx = -1;
1427 
1428 	/*
1429 	 * Traverse the capabilities section printing each capability group.
1430 	 * The first capabilities group defines any object capabilities.  Any
1431 	 * following groups define symbol capabilities.  In the case where no
1432 	 * object capabilities exist, but symbol capabilities do, a single
1433 	 * CA_SUNW_NULL terminator for the object capabilities exists.
1434 	 */
1435 	for (cnum = 0; cnum < capnum; cap++, cnum++) {
1436 		if (cap->c_tag == CA_SUNW_NULL) {
1437 			/*
1438 			 * A CA_SUNW_NULL tag terminates a capabilities group.
1439 			 * If the first capabilities tag is CA_SUNW_NULL, then
1440 			 * no object capabilities exist.
1441 			 */
1442 			if ((nulls++ == 0) && (cnum == 0))
1443 				objcap = 0;
1444 			title = 1;
1445 		} else {
1446 			if (title) {
1447 				if (nulls == 0) {
1448 					/*
1449 					 * If this capabilities group represents
1450 					 * the object capabilities (i.e., no
1451 					 * CA_SUNW_NULL tag has been processed
1452 					 * yet), then display an object
1453 					 * capabilities title.
1454 					 */
1455 					dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1456 					dbg_print(0,
1457 					    MSG_INTL(MSG_OBJ_CAP_TITLE));
1458 				} else {
1459 					/*
1460 					 * If this is a symbols capabilities
1461 					 * group (i.e., a CA_SUNW_NULL tag has
1462 					 * already be found that terminates
1463 					 * the object capabilities group), then
1464 					 * display a symbol capabilities title,
1465 					 * and retain this capabilities index
1466 					 * for later processing.
1467 					 */
1468 					dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1469 					dbg_print(0,
1470 					    MSG_INTL(MSG_SYM_CAP_TITLE));
1471 					descapndx = cnum;
1472 				}
1473 				Elf_cap_title(0);
1474 				title = 0;
1475 			}
1476 
1477 			/*
1478 			 * Print the capabilities data.
1479 			 *
1480 			 * Note that CA_SUNW_PLAT, CA_SUNW_MACH and CA_SUNW_ID
1481 			 * entries require a string table, which should have
1482 			 * already been established.
1483 			 */
1484 			if ((strs == NULL) && ((cap->c_tag == CA_SUNW_PLAT) ||
1485 			    (cap->c_tag == CA_SUNW_MACH) ||
1486 			    (cap->c_tag == CA_SUNW_ID))) {
1487 				(void) fprintf(stderr,
1488 				    MSG_INTL(MSG_WARN_INVCAP4), file,
1489 				    EC_WORD(elf_ndxscn(ccache->c_scn)),
1490 				    ccache->c_name, EC_WORD(cshdr->sh_info));
1491 			}
1492 			Elf_cap_entry(0, cap, cnum, strs, strs_size,
1493 			    ehdr->e_machine);
1494 		}
1495 
1496 		/*
1497 		 * If this CA_SUNW_NULL tag terminates a symbol capabilities
1498 		 * group, determine the associated symbols.
1499 		 */
1500 		if ((cap->c_tag == CA_SUNW_NULL) && (nulls > 1) &&
1501 		    (descapndx != -1)) {
1502 			Capinfo	*cip;
1503 			Word	inum;
1504 
1505 			symcaps++;
1506 
1507 			/*
1508 			 * Make sure we've discovered a SHT_SUNW_capinfo table.
1509 			 */
1510 			if ((cip = capinfo) == NULL) {
1511 				(void) fprintf(stderr,
1512 				    MSG_INTL(MSG_ERR_INVCAP), file,
1513 				    ccache->c_name, EC_WORD(cshdr->sh_link));
1514 				return (0);
1515 			}
1516 
1517 			/*
1518 			 * Determine what symbols reference this capabilities
1519 			 * group.
1520 			 */
1521 			dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1522 			dbg_print(0, MSG_INTL(MSG_CAPINFO_ENTRIES));
1523 			Elf_syms_table_title(0, ELF_DBG_ELFDUMP);
1524 
1525 			for (inum = 1, cip++; inum < capinfonum;
1526 			    inum++, cip++) {
1527 				Word	gndx = (Word)ELF_C_GROUP(*cip);
1528 
1529 				if (gndx && (gndx == descapndx)) {
1530 					output_symbol(&state, inum, 0,
1531 					    inum, state.sym + inum);
1532 				}
1533 			}
1534 			descapndx = -1;
1535 			continue;
1536 		}
1537 
1538 		/*
1539 		 * An SF1_SUNW_ADDR32 software capability tag in a 32-bit
1540 		 * object is suspicious as it has no effect.
1541 		 */
1542 		if ((cap->c_tag == CA_SUNW_SF_1) &&
1543 		    (ehdr->e_ident[EI_CLASS] == ELFCLASS32) &&
1544 		    (cap->c_un.c_val & SF1_SUNW_ADDR32)) {
1545 			(void) fprintf(stderr, MSG_INTL(MSG_WARN_INADDR32SF1),
1546 			    file, ccache->c_name);
1547 		}
1548 	}
1549 
1550 	/*
1551 	 * If this is a dynamic object, with symbol capabilities, then a
1552 	 * .SUNW_capchain section should exist.  This section contains a chain
1553 	 * of symbol indexes for each capabilities family.  This is the list
1554 	 * that is searched by ld.so.1 to determine the best capabilities
1555 	 * candidate.
1556 	 *
1557 	 * Note, more than one capabilities lead symbol can point to the same
1558 	 * family chain.  For example, a weak/global pair of symbols can both
1559 	 * represent the same family of capabilities symbols.  Therefore, to
1560 	 * display all possible families we traverse the capabilities
1561 	 * information section looking for CAPINFO_SUNW_GLOB lead symbols.
1562 	 * From these we determine the associated capabilities chain to inspect.
1563 	 */
1564 	if (symcaps &&
1565 	    ((ehdr->e_type == ET_EXEC) || (ehdr->e_type == ET_DYN))) {
1566 		Capinfo		*cip;
1567 		Capchain	*chain;
1568 		Cache   	*chcache;
1569 		Shdr		*chshdr;
1570 		Word		chainnum, inum;
1571 
1572 		/*
1573 		 * Validate that the sh_info field of the capabilities
1574 		 * information section points to a capabilities chain section.
1575 		 */
1576 		if (cishdr->sh_info >= shnum) {
1577 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
1578 			    file, cicache->c_name, EC_WORD(cishdr->sh_info));
1579 			return (0);
1580 		}
1581 
1582 		chcache = &cache[cishdr->sh_info];
1583 		chshdr = chcache->c_shdr;
1584 
1585 		if (chshdr->sh_type != SHT_SUNW_capchain) {
1586 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_INVCAPINFO2),
1587 			    file, cicache->c_name, EC_WORD(cishdr->sh_info));
1588 			return (0);
1589 		}
1590 
1591 		chainnum = (Word)(chshdr->sh_size / chshdr->sh_entsize);
1592 		chain = (Capchain *)chcache->c_data->d_buf;
1593 
1594 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1595 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_CAPCHAIN), chcache->c_name);
1596 
1597 		/*
1598 		 * Traverse the capabilities information section looking for
1599 		 * CAPINFO_SUNW_GLOB lead capabilities symbols.
1600 		 */
1601 		cip = capinfo;
1602 		for (inum = 1, cip++; inum < capinfonum; inum++, cip++) {
1603 			const char	*name;
1604 			Sym		*sym;
1605 			Word		sndx, cndx;
1606 			Word		gndx = (Word)ELF_C_GROUP(*cip);
1607 
1608 			if ((gndx == 0) || (gndx != CAPINFO_SUNW_GLOB))
1609 				continue;
1610 
1611 			/*
1612 			 * Determine the symbol that is associated with this
1613 			 * capability information entry, and use this to
1614 			 * identify this capability family.
1615 			 */
1616 			sym = (Sym *)(state.sym + inum);
1617 			name = string(cicache, inum, strcache, file,
1618 			    sym->st_name);
1619 
1620 			dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1621 			dbg_print(0, MSG_INTL(MSG_CAPCHAIN_TITLE), name);
1622 			dbg_print(0, MSG_INTL(MSG_CAPCHAIN_ENTRY));
1623 
1624 			cndx = (Word)ELF_C_SYM(*cip);
1625 
1626 			/*
1627 			 * Traverse this families chain and identify each
1628 			 * family member.
1629 			 */
1630 			for (;;) {
1631 				char	_chain[MAXNDXSIZE], _symndx[MAXNDXSIZE];
1632 
1633 				if (cndx >= chainnum) {
1634 					(void) fprintf(stderr,
1635 					    MSG_INTL(MSG_ERR_INVCAPINFO3), file,
1636 					    cicache->c_name, EC_WORD(inum),
1637 					    EC_WORD(cndx));
1638 					break;
1639 				}
1640 				if ((sndx = chain[cndx]) == 0)
1641 					break;
1642 
1643 				/*
1644 				 * Determine this entries symbol reference.
1645 				 */
1646 				if (sndx > state.symn) {
1647 					(void) fprintf(stderr,
1648 					    MSG_INTL(MSG_ERR_CHBADSYMNDX), file,
1649 					    EC_WORD(sndx), chcache->c_name,
1650 					    EC_WORD(cndx));
1651 					name = MSG_INTL(MSG_STR_UNKNOWN);
1652 				} else {
1653 					sym = (Sym *)(state.sym + sndx);
1654 					name = string(chcache, sndx,
1655 					    strcache, file, sym->st_name);
1656 				}
1657 
1658 				/*
1659 				 * Display the family member.
1660 				 */
1661 				(void) snprintf(_chain, MAXNDXSIZE,
1662 				    MSG_ORIG(MSG_FMT_INTEGER), cndx);
1663 				(void) snprintf(_symndx, MAXNDXSIZE,
1664 				    MSG_ORIG(MSG_FMT_INDEX2), EC_WORD(sndx));
1665 				dbg_print(0, MSG_ORIG(MSG_FMT_CHAIN_INFO),
1666 				    _chain, _symndx, demangle(name, flags));
1667 
1668 				cndx++;
1669 			}
1670 		}
1671 	}
1672 	return (objcap);
1673 }
1674 
1675 /*
1676  * Print the capabilities.
1677  *
1678  * A .SUNW_cap section can contain one or more, CA_SUNW_NULL terminated,
1679  * capabilities groups.  The first group defines the object capabilities.
1680  * This group defines the minimum capability requirements of the entire
1681  * object file.  If this is a dynamic object, this group should be associated
1682  * with a PT_SUNWCAP program header.
1683  *
1684  * Additional capabilities groups define the association of individual symbols
1685  * to specific capabilities.
1686  */
1687 static void
1688 cap(const char *file, Cache *cache, Word shnum, Word phnum, Ehdr *ehdr,
1689     uchar_t osabi, Elf *elf, uint_t flags)
1690 {
1691 	Word		cnt;
1692 	Shdr		*cshdr = NULL;
1693 	Cache		*ccache;
1694 	Off		cphdr_off = 0;
1695 	Xword		cphdr_sz;
1696 
1697 	/*
1698 	 * Determine if a global capabilities header exists.
1699 	 */
1700 	if (phnum) {
1701 		Phdr	*phdr;
1702 
1703 		if ((phdr = elf_getphdr(elf)) == NULL) {
1704 			failure(file, MSG_ORIG(MSG_ELF_GETPHDR));
1705 			return;
1706 		}
1707 
1708 		for (cnt = 0; cnt < phnum; phdr++, cnt++) {
1709 			if (phdr->p_type == PT_SUNWCAP) {
1710 				cphdr_off = phdr->p_offset;
1711 				cphdr_sz = phdr->p_filesz;
1712 				break;
1713 			}
1714 		}
1715 	}
1716 
1717 	/*
1718 	 * Determine if a capabilities section exists.
1719 	 */
1720 	for (cnt = 1; cnt < shnum; cnt++) {
1721 		Cache	*_cache = &cache[cnt];
1722 		Shdr	*shdr = _cache->c_shdr;
1723 
1724 		/*
1725 		 * Process any capabilities information.
1726 		 */
1727 		if (shdr->sh_type == SHT_SUNW_cap) {
1728 			if (cap_section(file, cache, shnum, _cache, osabi,
1729 			    ehdr, flags)) {
1730 				/*
1731 				 * If this section defined an object capability
1732 				 * group, retain the section information for
1733 				 * program header validation.
1734 				 */
1735 				ccache = _cache;
1736 				cshdr = shdr;
1737 			}
1738 			continue;
1739 		}
1740 	}
1741 
1742 	if ((cshdr == NULL) && (cphdr_off == 0))
1743 		return;
1744 
1745 	if (cphdr_off && (cshdr == NULL))
1746 		(void) fprintf(stderr, MSG_INTL(MSG_WARN_INVCAP1), file);
1747 
1748 	/*
1749 	 * If this object is an executable or shared object, and it provided
1750 	 * an object capabilities group, then the group should have an
1751 	 * accompanying PT_SUNWCAP program header.
1752 	 */
1753 	if (cshdr && ((ehdr->e_type == ET_EXEC) || (ehdr->e_type == ET_DYN))) {
1754 		if (cphdr_off == 0) {
1755 			(void) fprintf(stderr, MSG_INTL(MSG_WARN_INVCAP2),
1756 			    file, EC_WORD(elf_ndxscn(ccache->c_scn)),
1757 			    ccache->c_name);
1758 		} else if ((cphdr_off != cshdr->sh_offset) ||
1759 		    (cphdr_sz != cshdr->sh_size)) {
1760 			(void) fprintf(stderr, MSG_INTL(MSG_WARN_INVCAP3),
1761 			    file, EC_WORD(elf_ndxscn(ccache->c_scn)),
1762 			    ccache->c_name);
1763 		}
1764 	}
1765 }
1766 
1767 /*
1768  * Print the interpretor.
1769  */
1770 static void
1771 interp(const char *file, Cache *cache, Word shnum, Word phnum, Elf *elf)
1772 {
1773 	static Word phdr_types[] = { PT_INTERP };
1774 
1775 
1776 	Word	cnt;
1777 	Shdr	*ishdr = NULL;
1778 	Cache	*icache;
1779 	Off	iphdr_off = 0;
1780 	Xword	iphdr_fsz;
1781 
1782 	/*
1783 	 * Determine if an interp header exists.
1784 	 */
1785 	if (phnum) {
1786 		Phdr	*phdr;
1787 
1788 		phdr = getphdr(phnum, phdr_types,
1789 		    sizeof (phdr_types) / sizeof (*phdr_types), file, elf);
1790 		if (phdr != NULL) {
1791 			iphdr_off = phdr->p_offset;
1792 			iphdr_fsz = phdr->p_filesz;
1793 		}
1794 	}
1795 
1796 	if (iphdr_off == 0)
1797 		return;
1798 
1799 	/*
1800 	 * Determine if an interp section exists.
1801 	 */
1802 	for (cnt = 1; cnt < shnum; cnt++) {
1803 		Cache	*_cache = &cache[cnt];
1804 		Shdr	*shdr = _cache->c_shdr;
1805 
1806 		/*
1807 		 * Scan sections to find a section which contains the PT_INTERP
1808 		 * string.  The target section can't be in a NOBITS section.
1809 		 */
1810 		if ((shdr->sh_type == SHT_NOBITS) ||
1811 		    (iphdr_off < shdr->sh_offset) ||
1812 		    (iphdr_off + iphdr_fsz) > (shdr->sh_offset + shdr->sh_size))
1813 			continue;
1814 
1815 		icache = _cache;
1816 		ishdr = shdr;
1817 		break;
1818 	}
1819 
1820 	/*
1821 	 * Print the interpreter string based on the offset defined in the
1822 	 * program header, as this is the offset used by the kernel.
1823 	 */
1824 	if (ishdr && icache->c_data) {
1825 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1826 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_INTERP), icache->c_name);
1827 		dbg_print(0, MSG_ORIG(MSG_FMT_INDENT),
1828 		    (char *)icache->c_data->d_buf +
1829 		    (iphdr_off - ishdr->sh_offset));
1830 	} else
1831 		(void) fprintf(stderr, MSG_INTL(MSG_WARN_INVINTERP1), file);
1832 
1833 	/*
1834 	 * If there are any inconsistences between the program header and
1835 	 * section information, flag them.
1836 	 */
1837 	if (ishdr && ((iphdr_off != ishdr->sh_offset) ||
1838 	    (iphdr_fsz != ishdr->sh_size))) {
1839 		(void) fprintf(stderr, MSG_INTL(MSG_WARN_INVINTERP2), file,
1840 		    icache->c_name);
1841 	}
1842 }
1843 
1844 /*
1845  * Print the syminfo section.
1846  */
1847 static void
1848 syminfo(Cache *cache, Word shnum, Ehdr *ehdr, uchar_t osabi, const char *file)
1849 {
1850 	Shdr		*infoshdr;
1851 	Syminfo		*info;
1852 	Sym		*syms;
1853 	Dyn		*dyns;
1854 	Word		infonum, cnt, ndx, symnum, dynnum;
1855 	Cache		*infocache = NULL, *dyncache = NULL, *symsec, *strsec;
1856 	Boolean		*dynerr;
1857 
1858 	for (cnt = 1; cnt < shnum; cnt++) {
1859 		if (cache[cnt].c_shdr->sh_type == SHT_SUNW_syminfo) {
1860 			infocache = &cache[cnt];
1861 			break;
1862 		}
1863 	}
1864 	if (infocache == NULL)
1865 		return;
1866 
1867 	infoshdr = infocache->c_shdr;
1868 	if ((infoshdr->sh_entsize == 0) || (infoshdr->sh_size == 0)) {
1869 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
1870 		    file, infocache->c_name);
1871 		return;
1872 	}
1873 	if (infocache->c_data == NULL)
1874 		return;
1875 
1876 	infonum = (Word)(infoshdr->sh_size / infoshdr->sh_entsize);
1877 	info = (Syminfo *)infocache->c_data->d_buf;
1878 
1879 	/*
1880 	 * If there is no associated dynamic section, determine if one
1881 	 * is needed, and if so issue a warning. If there is an
1882 	 * associated dynamic section, validate it and get the data buffer
1883 	 * for it.
1884 	 */
1885 	dyns = NULL;
1886 	dynnum = 0;
1887 	if (infoshdr->sh_info == 0) {
1888 		Syminfo	*_info = info + 1;
1889 
1890 		for (ndx = 1; ndx < infonum; ndx++, _info++) {
1891 			if ((_info->si_flags == 0) && (_info->si_boundto == 0))
1892 				continue;
1893 
1894 			if (_info->si_boundto < SYMINFO_BT_LOWRESERVE)
1895 				(void) fprintf(stderr,
1896 				    MSG_INTL(MSG_ERR_BADSHINFO), file,
1897 				    infocache->c_name,
1898 				    EC_WORD(infoshdr->sh_info));
1899 		}
1900 	} else if ((infoshdr->sh_info >= shnum) ||
1901 	    (cache[infoshdr->sh_info].c_shdr->sh_type != SHT_DYNAMIC)) {
1902 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHINFO),
1903 		    file, infocache->c_name, EC_WORD(infoshdr->sh_info));
1904 	} else {
1905 		dyncache = &cache[infoshdr->sh_info];
1906 		if ((dyncache->c_data == NULL) ||
1907 		    ((dyns = dyncache->c_data->d_buf) == NULL)) {
1908 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
1909 			    file, dyncache->c_name);
1910 		}
1911 		if (dyns != NULL) {
1912 			dynnum = dyncache->c_shdr->sh_size /
1913 			    dyncache->c_shdr->sh_entsize;
1914 
1915 			/*
1916 			 * We validate the type of dynamic elements referenced
1917 			 * from the syminfo. This array is used report any
1918 			 * bad dynamic entries.
1919 			 */
1920 			if ((dynerr = calloc(dynnum, sizeof (*dynerr))) ==
1921 			    NULL) {
1922 				int err = errno;
1923 				(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
1924 				    file, strerror(err));
1925 				return;
1926 			}
1927 		}
1928 	}
1929 
1930 	/*
1931 	 * Get the data buffer for the associated symbol table and string table.
1932 	 */
1933 	if (stringtbl(cache, 1, cnt, shnum, file,
1934 	    &symnum, &symsec, &strsec) == 0)
1935 		return;
1936 
1937 	syms = symsec->c_data->d_buf;
1938 
1939 	/*
1940 	 * Loop through the syminfo entries.
1941 	 */
1942 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1943 	dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMINFO), infocache->c_name);
1944 	Elf_syminfo_title(0);
1945 
1946 	for (ndx = 1, info++; ndx < infonum; ndx++, info++) {
1947 		Sym 		*sym;
1948 		const char	*needed, *name;
1949 		Word		expect_dt;
1950 		Word		boundto = info->si_boundto;
1951 
1952 		if ((info->si_flags == 0) && (boundto == 0))
1953 			continue;
1954 
1955 		sym = &syms[ndx];
1956 		name = string(infocache, ndx, strsec, file, sym->st_name);
1957 
1958 		/* Is si_boundto set to one of the reserved values? */
1959 		if (boundto >= SYMINFO_BT_LOWRESERVE) {
1960 			Elf_syminfo_entry(0, ndx, info, name, NULL);
1961 			continue;
1962 		}
1963 
1964 		/*
1965 		 * si_boundto is referencing a dynamic section. If we don't
1966 		 * have one, an error was already issued above, so it suffices
1967 		 * to display an empty string. If we are out of bounds, then
1968 		 * report that and then display an empty string.
1969 		 */
1970 		if ((dyns == NULL) || (boundto >= dynnum)) {
1971 			if (dyns != NULL)
1972 				(void) fprintf(stderr,
1973 				    MSG_INTL(MSG_ERR_BADSIDYNNDX), file,
1974 				    infocache->c_ndx, infocache->c_name,
1975 				    EC_WORD(ndx), EC_WORD(dynnum - 1),
1976 				    EC_WORD(boundto));
1977 			Elf_syminfo_entry(0, ndx, info, name,
1978 			    MSG_ORIG(MSG_STR_EMPTY));
1979 			continue;
1980 		}
1981 
1982 		/*
1983 		 * The si_boundto reference expects a specific dynamic element
1984 		 * type at the given index. The dynamic element is always a
1985 		 * string that gives an object name. The specific type depends
1986 		 * on the si_flags present. Ensure that we've got the right
1987 		 * type.
1988 		 */
1989 		if (info->si_flags & SYMINFO_FLG_FILTER)
1990 			expect_dt = DT_SUNW_FILTER;
1991 		else if (info->si_flags & SYMINFO_FLG_AUXILIARY)
1992 			expect_dt = DT_SUNW_AUXILIARY;
1993 		else if (info->si_flags & (SYMINFO_FLG_DIRECT |
1994 		    SYMINFO_FLG_LAZYLOAD | SYMINFO_FLG_DIRECTBIND))
1995 			expect_dt = DT_NEEDED;
1996 		else
1997 			expect_dt = DT_NULL;   /* means we ignore the type */
1998 
1999 		if ((dyns[boundto].d_tag != expect_dt) &&
2000 		    (expect_dt != DT_NULL)) {
2001 			Conv_inv_buf_t	buf1, buf2;
2002 
2003 			/* Only complain about each dynamic element once */
2004 			if (!dynerr[boundto]) {
2005 				(void) fprintf(stderr,
2006 				    MSG_INTL(MSG_ERR_BADSIDYNTAG),
2007 				    file, infocache->c_ndx, infocache->c_name,
2008 				    EC_WORD(ndx), dyncache->c_ndx,
2009 				    dyncache->c_name, EC_WORD(boundto),
2010 				    conv_dyn_tag(expect_dt, osabi,
2011 				    ehdr->e_machine, CONV_FMT_ALT_CF, &buf1),
2012 				    conv_dyn_tag(dyns[boundto].d_tag, osabi,
2013 				    ehdr->e_machine, CONV_FMT_ALT_CF, &buf2));
2014 				dynerr[boundto] = TRUE;
2015 			}
2016 		}
2017 
2018 		/*
2019 		 * Whether or not the DT item we're pointing at is
2020 		 * of the right type, if it's a type we recognize as
2021 		 * providing a string, go ahead and show it. Otherwise
2022 		 * an empty string.
2023 		 */
2024 		switch (dyns[boundto].d_tag) {
2025 		case DT_NEEDED:
2026 		case DT_SONAME:
2027 		case DT_RPATH:
2028 		case DT_RUNPATH:
2029 		case DT_CONFIG:
2030 		case DT_DEPAUDIT:
2031 		case DT_USED:
2032 		case DT_AUDIT:
2033 		case DT_SUNW_AUXILIARY:
2034 		case DT_SUNW_FILTER:
2035 		case DT_FILTER:
2036 		case DT_AUXILIARY:
2037 			needed = string(infocache, boundto,
2038 			    strsec, file, dyns[boundto].d_un.d_val);
2039 			break;
2040 		default:
2041 			needed = MSG_ORIG(MSG_STR_EMPTY);
2042 		}
2043 		Elf_syminfo_entry(0, ndx, info, name, needed);
2044 	}
2045 	if (dyns != NULL)
2046 		free(dynerr);
2047 }
2048 
2049 /*
2050  * Print version definition section entries.
2051  */
2052 static void
2053 version_def(Verdef *vdf, Word vdf_num, Cache *vcache, Cache *scache,
2054     const char *file)
2055 {
2056 	Word	cnt;
2057 	char	index[MAXNDXSIZE];
2058 
2059 	Elf_ver_def_title(0);
2060 
2061 	for (cnt = 1; cnt <= vdf_num; cnt++,
2062 	    vdf = (Verdef *)((uintptr_t)vdf + vdf->vd_next)) {
2063 		Conv_ver_flags_buf_t	ver_flags_buf;
2064 		const char		*name, *dep;
2065 		Half			vcnt = vdf->vd_cnt - 1;
2066 		Half			ndx = vdf->vd_ndx;
2067 		Verdaux	*vdap = (Verdaux *)((uintptr_t)vdf + vdf->vd_aux);
2068 
2069 		/*
2070 		 * Obtain the name and first dependency (if any).
2071 		 */
2072 		name = string(vcache, cnt, scache, file, vdap->vda_name);
2073 		vdap = (Verdaux *)((uintptr_t)vdap + vdap->vda_next);
2074 		if (vcnt)
2075 			dep = string(vcache, cnt, scache, file, vdap->vda_name);
2076 		else
2077 			dep = MSG_ORIG(MSG_STR_EMPTY);
2078 
2079 		(void) snprintf(index, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INDEX),
2080 		    EC_XWORD(ndx));
2081 		Elf_ver_line_1(0, index, name, dep,
2082 		    conv_ver_flags(vdf->vd_flags, 0, &ver_flags_buf));
2083 
2084 		/*
2085 		 * Print any additional dependencies.
2086 		 */
2087 		if (vcnt) {
2088 			vdap = (Verdaux *)((uintptr_t)vdap + vdap->vda_next);
2089 			for (vcnt--; vcnt; vcnt--,
2090 			    vdap = (Verdaux *)((uintptr_t)vdap +
2091 			    vdap->vda_next)) {
2092 				dep = string(vcache, cnt, scache, file,
2093 				    vdap->vda_name);
2094 				Elf_ver_line_2(0, MSG_ORIG(MSG_STR_EMPTY), dep);
2095 			}
2096 		}
2097 	}
2098 }
2099 
2100 /*
2101  * Print version needed section entries.
2102  *
2103  * entry:
2104  *	vnd - Address of verneed data
2105  *	vnd_num - # of Verneed entries
2106  *	vcache - Cache of verneed section being processed
2107  *	scache - Cache of associated string table section
2108  *	file - Name of object being processed.
2109  *	versym - Information about versym section
2110  *
2111  * exit:
2112  *	The versions have been printed. If GNU style versioning
2113  *	is in effect, versym->max_verndx has been updated to
2114  *	contain the largest version index seen.
2115  *
2116  * note:
2117  * 	The versym section of an object that follows the original
2118  *	Solaris versioning rules only contains indexes into the verdef
2119  *	section. Symbols defined in other objects (UNDEF) are given
2120  *	a version of 0, indicating that they are not defined by
2121  *	this file, and the Verneed entries do not have associated version
2122  *	indexes. For these reasons, we do not display a version index
2123  *	for original-style Verneed sections.
2124  *
2125  *	The GNU versioning extensions alter this: Symbols defined in other
2126  *	objects receive a version index in the range above those defined
2127  *	by the Verdef section, and the vna_other field of the Vernaux
2128  *	structs inside the Verneed section contain the version index for
2129  *	that item. We therefore  display the index when showing the
2130  *	contents of a GNU style Verneed section. You should not
2131  *	necessarily expect these indexes to appear in sorted
2132  *	order --- it seems that the GNU ld assigns the versions as
2133  *	symbols are encountered during linking, and then the results
2134  *	are assembled into the Verneed section afterwards.
2135  */
2136 static void
2137 version_need(Verneed *vnd, Word vnd_num, Cache *vcache, Cache *scache,
2138     const char *file, VERSYM_STATE *versym)
2139 {
2140 	Word		cnt;
2141 	char		index[MAXNDXSIZE];
2142 	const char	*index_str;
2143 
2144 	Elf_ver_need_title(0, versym->gnu_needed);
2145 
2146 	for (cnt = 1; cnt <= vnd_num; cnt++,
2147 	    vnd = (Verneed *)((uintptr_t)vnd + vnd->vn_next)) {
2148 		Conv_ver_flags_buf_t	ver_flags_buf;
2149 		const char		*name, *dep;
2150 		Half			vcnt = vnd->vn_cnt;
2151 		Vernaux *vnap = (Vernaux *)((uintptr_t)vnd + vnd->vn_aux);
2152 
2153 		/*
2154 		 * Obtain the name of the needed file and the version name
2155 		 * within it that we're dependent on.  Note that the count
2156 		 * should be at least one, otherwise this is a pretty bogus
2157 		 * entry.
2158 		 */
2159 		name = string(vcache, cnt, scache, file, vnd->vn_file);
2160 		if (vcnt)
2161 			dep = string(vcache, cnt, scache, file, vnap->vna_name);
2162 		else
2163 			dep = MSG_INTL(MSG_STR_NULL);
2164 
2165 		if (vnap->vna_other == 0) {	/* Traditional form */
2166 			index_str = MSG_ORIG(MSG_STR_EMPTY);
2167 		} else {			/* GNU form */
2168 			index_str = index;
2169 			/* Format the version index value */
2170 			(void) snprintf(index, MAXNDXSIZE,
2171 			    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(vnap->vna_other));
2172 			if (vnap->vna_other > versym->max_verndx)
2173 				versym->max_verndx = vnap->vna_other;
2174 		}
2175 		Elf_ver_line_1(0, index_str, name, dep,
2176 		    conv_ver_flags(vnap->vna_flags, 0, &ver_flags_buf));
2177 
2178 		/*
2179 		 * Print any additional version dependencies.
2180 		 */
2181 		if (vcnt) {
2182 			vnap = (Vernaux *)((uintptr_t)vnap + vnap->vna_next);
2183 			for (vcnt--; vcnt; vcnt--,
2184 			    vnap = (Vernaux *)((uintptr_t)vnap +
2185 			    vnap->vna_next)) {
2186 				dep = string(vcache, cnt, scache, file,
2187 				    vnap->vna_name);
2188 				if (vnap->vna_other > 0) {
2189 					/* Format the next index value */
2190 					(void) snprintf(index, MAXNDXSIZE,
2191 					    MSG_ORIG(MSG_FMT_INDEX),
2192 					    EC_XWORD(vnap->vna_other));
2193 					Elf_ver_line_1(0, index,
2194 					    MSG_ORIG(MSG_STR_EMPTY), dep,
2195 					    conv_ver_flags(vnap->vna_flags,
2196 					    0, &ver_flags_buf));
2197 					if (vnap->vna_other >
2198 					    versym->max_verndx)
2199 						versym->max_verndx =
2200 						    vnap->vna_other;
2201 				} else {
2202 					Elf_ver_line_3(0,
2203 					    MSG_ORIG(MSG_STR_EMPTY), dep,
2204 					    conv_ver_flags(vnap->vna_flags,
2205 					    0, &ver_flags_buf));
2206 				}
2207 			}
2208 		}
2209 	}
2210 }
2211 
2212 /*
2213  * Examine the Verneed section for information related to GNU
2214  * style Versym indexing:
2215  *	- A non-zero vna_other field indicates that Versym indexes can
2216  *		reference Verneed records.
2217  *	- If the object uses GNU style Versym indexing, the
2218  *	  maximum index value is needed to detect bad Versym entries.
2219  *
2220  * entry:
2221  *	vnd - Address of verneed data
2222  *	vnd_num - # of Verneed entries
2223  *	versym - Information about versym section
2224  *
2225  * exit:
2226  *	If a non-zero vna_other field is seen, versym->gnu_needed is set.
2227  *
2228  *	versym->max_verndx has been updated to contain the largest
2229  *	version index seen.
2230  */
2231 static void
2232 update_gnu_verndx(Verneed *vnd, Word vnd_num, VERSYM_STATE *versym)
2233 {
2234 	Word		cnt;
2235 
2236 	for (cnt = 1; cnt <= vnd_num; cnt++,
2237 	    vnd = (Verneed *)((uintptr_t)vnd + vnd->vn_next)) {
2238 		Half	vcnt = vnd->vn_cnt;
2239 		Vernaux	*vnap = (Vernaux *)((uintptr_t)vnd + vnd->vn_aux);
2240 
2241 		/*
2242 		 * A non-zero value of vna_other indicates that this
2243 		 * object references VERNEED items from the VERSYM
2244 		 * array.
2245 		 */
2246 		if (vnap->vna_other != 0) {
2247 			versym->gnu_needed = 1;
2248 			if (vnap->vna_other > versym->max_verndx)
2249 				versym->max_verndx = vnap->vna_other;
2250 		}
2251 
2252 		/*
2253 		 * Check any additional version dependencies.
2254 		 */
2255 		if (vcnt) {
2256 			vnap = (Vernaux *)((uintptr_t)vnap + vnap->vna_next);
2257 			for (vcnt--; vcnt; vcnt--,
2258 			    vnap = (Vernaux *)((uintptr_t)vnap +
2259 			    vnap->vna_next)) {
2260 				if (vnap->vna_other == 0)
2261 					continue;
2262 
2263 				versym->gnu_needed = 1;
2264 				if (vnap->vna_other > versym->max_verndx)
2265 					versym->max_verndx = vnap->vna_other;
2266 			}
2267 		}
2268 	}
2269 }
2270 
2271 /*
2272  * Display version section information if the flags require it.
2273  * Return version information needed by other output.
2274  *
2275  * entry:
2276  *	cache - Cache of all section headers
2277  *	shnum - # of sections in cache
2278  *	file - Name of file
2279  *	flags - Command line option flags
2280  *	versym - VERSYM_STATE block to be filled in.
2281  */
2282 static void
2283 versions(Cache *cache, Word shnum, const char *file, uint_t flags,
2284     VERSYM_STATE *versym)
2285 {
2286 	GElf_Word	cnt;
2287 	Cache		*verdef_cache = NULL, *verneed_cache = NULL;
2288 
2289 
2290 	/* Gather information about the version sections */
2291 	versym->max_verndx = 1;
2292 	for (cnt = 1; cnt < shnum; cnt++) {
2293 		Cache		*_cache = &cache[cnt];
2294 		Shdr		*shdr = _cache->c_shdr;
2295 		Dyn		*dyn;
2296 		ulong_t		numdyn;
2297 
2298 		switch (shdr->sh_type) {
2299 		case SHT_DYNAMIC:
2300 			/*
2301 			 * The GNU ld puts a DT_VERSYM entry in the dynamic
2302 			 * section so that the runtime linker can use it to
2303 			 * implement their versioning rules. They allow multiple
2304 			 * incompatible functions with the same name to exist
2305 			 * in different versions. The Solaris ld does not
2306 			 * support this mechanism, and as such, does not
2307 			 * produce DT_VERSYM. We use this fact to determine
2308 			 * which ld produced this object, and how to interpret
2309 			 * the version values.
2310 			 */
2311 			if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0) ||
2312 			    (_cache->c_data == NULL))
2313 				continue;
2314 			numdyn = shdr->sh_size / shdr->sh_entsize;
2315 			dyn = (Dyn *)_cache->c_data->d_buf;
2316 			for (; numdyn-- > 0; dyn++)
2317 				if (dyn->d_tag == DT_VERSYM) {
2318 					versym->gnu_full =
2319 					    versym->gnu_needed = 1;
2320 					break;
2321 				}
2322 			break;
2323 
2324 		case SHT_SUNW_versym:
2325 			/* Record data address for later symbol processing */
2326 			if (_cache->c_data != NULL) {
2327 				versym->cache = _cache;
2328 				versym->data = _cache->c_data->d_buf;
2329 				continue;
2330 			}
2331 			break;
2332 
2333 		case SHT_SUNW_verdef:
2334 		case SHT_SUNW_verneed:
2335 			/*
2336 			 * Ensure the data is non-NULL and the number
2337 			 * of items is non-zero. Otherwise, we don't
2338 			 * understand the section, and will not use it.
2339 			 */
2340 			if ((_cache->c_data == NULL) ||
2341 			    (_cache->c_data->d_buf == NULL)) {
2342 				(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
2343 				    file, _cache->c_name);
2344 				continue;
2345 			}
2346 			if (shdr->sh_info == 0) {
2347 				(void) fprintf(stderr,
2348 				    MSG_INTL(MSG_ERR_BADSHINFO),
2349 				    file, _cache->c_name,
2350 				    EC_WORD(shdr->sh_info));
2351 				continue;
2352 			}
2353 
2354 			/* Make sure the string table index is in range */
2355 			if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
2356 				(void) fprintf(stderr,
2357 				    MSG_INTL(MSG_ERR_BADSHLINK), file,
2358 				    _cache->c_name, EC_WORD(shdr->sh_link));
2359 				continue;
2360 			}
2361 
2362 			/*
2363 			 * The section is usable. Save the cache entry.
2364 			 */
2365 			if (shdr->sh_type == SHT_SUNW_verdef) {
2366 				verdef_cache = _cache;
2367 				/*
2368 				 * Under Solaris rules, if there is a verdef
2369 				 * section, the max versym index is number
2370 				 * of version definitions it supplies.
2371 				 */
2372 				versym->max_verndx = shdr->sh_info;
2373 			} else {
2374 				verneed_cache = _cache;
2375 			}
2376 			break;
2377 		}
2378 	}
2379 
2380 	/*
2381 	 * If there is a Verneed section, examine it for information
2382 	 * related to GNU style versioning.
2383 	 */
2384 	if (verneed_cache != NULL)
2385 		update_gnu_verndx((Verneed *)verneed_cache->c_data->d_buf,
2386 		    verneed_cache->c_shdr->sh_info, versym);
2387 
2388 	/*
2389 	 * Now that all the information is available, display the
2390 	 * Verdef and Verneed section contents, if requested.
2391 	 */
2392 	if ((flags & FLG_SHOW_VERSIONS) == 0)
2393 		return;
2394 	if (verdef_cache != NULL) {
2395 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2396 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_VERDEF),
2397 		    verdef_cache->c_name);
2398 		version_def((Verdef *)verdef_cache->c_data->d_buf,
2399 		    verdef_cache->c_shdr->sh_info, verdef_cache,
2400 		    &cache[verdef_cache->c_shdr->sh_link], file);
2401 	}
2402 	if (verneed_cache != NULL) {
2403 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2404 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_VERNEED),
2405 		    verneed_cache->c_name);
2406 		/*
2407 		 * If GNU versioning applies to this object, version_need()
2408 		 * will update versym->max_verndx, and it is not
2409 		 * necessary to call update_gnu_verndx().
2410 		 */
2411 		version_need((Verneed *)verneed_cache->c_data->d_buf,
2412 		    verneed_cache->c_shdr->sh_info, verneed_cache,
2413 		    &cache[verneed_cache->c_shdr->sh_link], file, versym);
2414 	}
2415 }
2416 
2417 /*
2418  * Search for and process any symbol tables.
2419  */
2420 void
2421 symbols(Cache *cache, Word shnum, Ehdr *ehdr, uchar_t osabi,
2422     VERSYM_STATE *versym, const char *file, uint_t flags)
2423 {
2424 	SYMTBL_STATE state;
2425 	Cache *_cache;
2426 	Word secndx;
2427 
2428 	for (secndx = 1; secndx < shnum; secndx++) {
2429 		Word		symcnt;
2430 		Shdr		*shdr;
2431 
2432 		_cache = &cache[secndx];
2433 		shdr = _cache->c_shdr;
2434 
2435 		if ((shdr->sh_type != SHT_SYMTAB) &&
2436 		    (shdr->sh_type != SHT_DYNSYM) &&
2437 		    ((shdr->sh_type != SHT_SUNW_LDYNSYM) ||
2438 		    (osabi != ELFOSABI_SOLARIS)))
2439 			continue;
2440 		if (!match(MATCH_F_ALL, _cache->c_name, secndx, shdr->sh_type))
2441 			continue;
2442 
2443 		if (!init_symtbl_state(&state, cache, shnum, secndx, ehdr,
2444 		    osabi, versym, file, flags))
2445 			continue;
2446 		/*
2447 		 * Loop through the symbol tables entries.
2448 		 */
2449 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2450 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMTAB), state.secname);
2451 		Elf_syms_table_title(0, ELF_DBG_ELFDUMP);
2452 
2453 		for (symcnt = 0; symcnt < state.symn; symcnt++)
2454 			output_symbol(&state, symcnt, shdr->sh_info, symcnt,
2455 			    state.sym + symcnt);
2456 	}
2457 }
2458 
2459 /*
2460  * Search for and process any SHT_SUNW_symsort or SHT_SUNW_tlssort sections.
2461  * These sections are always associated with the .SUNW_ldynsym./.dynsym pair.
2462  */
2463 static void
2464 sunw_sort(Cache *cache, Word shnum, Ehdr *ehdr, uchar_t osabi,
2465     VERSYM_STATE *versym, const char *file, uint_t flags)
2466 {
2467 	SYMTBL_STATE	ldynsym_state,	dynsym_state;
2468 	Cache		*sortcache,	*symcache;
2469 	Shdr		*sortshdr,	*symshdr;
2470 	Word		sortsecndx,	symsecndx;
2471 	Word		ldynsym_cnt;
2472 	Word		*ndx;
2473 	Word		ndxn;
2474 	int		output_cnt = 0;
2475 	Conv_inv_buf_t	inv_buf;
2476 
2477 	for (sortsecndx = 1; sortsecndx < shnum; sortsecndx++) {
2478 
2479 		sortcache = &cache[sortsecndx];
2480 		sortshdr = sortcache->c_shdr;
2481 
2482 		if ((sortshdr->sh_type != SHT_SUNW_symsort) &&
2483 		    (sortshdr->sh_type != SHT_SUNW_tlssort))
2484 			continue;
2485 		if (!match(MATCH_F_ALL, sortcache->c_name, sortsecndx,
2486 		    sortshdr->sh_type))
2487 			continue;
2488 
2489 		/*
2490 		 * If the section references a SUNW_ldynsym, then we
2491 		 * expect to see the associated .dynsym immediately
2492 		 * following. If it references a .dynsym, there is no
2493 		 * SUNW_ldynsym. If it is any other type, then we don't
2494 		 * know what to do with it.
2495 		 */
2496 		if ((sortshdr->sh_link == 0) || (sortshdr->sh_link >= shnum)) {
2497 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
2498 			    file, sortcache->c_name,
2499 			    EC_WORD(sortshdr->sh_link));
2500 			continue;
2501 		}
2502 		symcache = &cache[sortshdr->sh_link];
2503 		symshdr = symcache->c_shdr;
2504 		symsecndx = sortshdr->sh_link;
2505 		ldynsym_cnt = 0;
2506 		switch (symshdr->sh_type) {
2507 		case SHT_SUNW_LDYNSYM:
2508 			if (!init_symtbl_state(&ldynsym_state, cache, shnum,
2509 			    symsecndx, ehdr, osabi, versym, file, flags))
2510 				continue;
2511 			ldynsym_cnt = ldynsym_state.symn;
2512 			/*
2513 			 * We know that the dynsym follows immediately
2514 			 * after the SUNW_ldynsym, and so, should be at
2515 			 * (sortshdr->sh_link + 1). However, elfdump is a
2516 			 * diagnostic tool, so we do the full paranoid
2517 			 * search instead.
2518 			 */
2519 			for (symsecndx = 1; symsecndx < shnum; symsecndx++) {
2520 				symcache = &cache[symsecndx];
2521 				symshdr = symcache->c_shdr;
2522 				if (symshdr->sh_type == SHT_DYNSYM)
2523 					break;
2524 			}
2525 			if (symsecndx >= shnum) {	/* Dynsym not found! */
2526 				(void) fprintf(stderr,
2527 				    MSG_INTL(MSG_ERR_NODYNSYM),
2528 				    file, sortcache->c_name);
2529 				continue;
2530 			}
2531 			/* Fallthrough to process associated dynsym */
2532 			/* FALLTHROUGH */
2533 		case SHT_DYNSYM:
2534 			if (!init_symtbl_state(&dynsym_state, cache, shnum,
2535 			    symsecndx, ehdr, osabi, versym, file, flags))
2536 				continue;
2537 			break;
2538 		default:
2539 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADNDXSEC),
2540 			    file, sortcache->c_name,
2541 			    conv_sec_type(osabi, ehdr->e_machine,
2542 			    symshdr->sh_type, 0, &inv_buf));
2543 			continue;
2544 		}
2545 
2546 		/*
2547 		 * Output header
2548 		 */
2549 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2550 		if (ldynsym_cnt > 0) {
2551 			dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMSORT2),
2552 			    sortcache->c_name, ldynsym_state.secname,
2553 			    dynsym_state.secname);
2554 			/*
2555 			 * The data for .SUNW_ldynsym and dynsym sections
2556 			 * is supposed to be adjacent with SUNW_ldynsym coming
2557 			 * first. Check, and issue a warning if it isn't so.
2558 			 */
2559 			if (((ldynsym_state.sym + ldynsym_state.symn)
2560 			    != dynsym_state.sym) &&
2561 			    ((flags & FLG_CTL_FAKESHDR) == 0))
2562 				(void) fprintf(stderr,
2563 				    MSG_INTL(MSG_ERR_LDYNNOTADJ), file,
2564 				    ldynsym_state.secname,
2565 				    dynsym_state.secname);
2566 		} else {
2567 			dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMSORT1),
2568 			    sortcache->c_name, dynsym_state.secname);
2569 		}
2570 		Elf_syms_table_title(0, ELF_DBG_ELFDUMP);
2571 
2572 		/* If not first one, insert a line of white space */
2573 		if (output_cnt++ > 0)
2574 			dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2575 
2576 		/*
2577 		 * SUNW_dynsymsort and SUNW_dyntlssort are arrays of
2578 		 * symbol indices. Iterate over the array entries,
2579 		 * dispaying the referenced symbols.
2580 		 */
2581 		ndxn = sortshdr->sh_size / sortshdr->sh_entsize;
2582 		ndx = (Word *)sortcache->c_data->d_buf;
2583 		for (; ndxn-- > 0; ndx++) {
2584 			if (*ndx >= ldynsym_cnt) {
2585 				Word sec_ndx = *ndx - ldynsym_cnt;
2586 
2587 				output_symbol(&dynsym_state, sec_ndx, 0,
2588 				    *ndx, dynsym_state.sym + sec_ndx);
2589 			} else {
2590 				output_symbol(&ldynsym_state, *ndx, 0,
2591 				    *ndx, ldynsym_state.sym + *ndx);
2592 			}
2593 		}
2594 	}
2595 }
2596 
2597 /*
2598  * Search for and process any relocation sections.
2599  */
2600 static void
2601 reloc(Cache *cache, Word shnum, Ehdr *ehdr, const char *file)
2602 {
2603 	Word	cnt;
2604 
2605 	for (cnt = 1; cnt < shnum; cnt++) {
2606 		Word		type, symnum;
2607 		Xword		relndx, relnum, relsize;
2608 		void		*rels;
2609 		Sym		*syms;
2610 		Cache		*symsec, *strsec;
2611 		Cache		*_cache = &cache[cnt];
2612 		Shdr		*shdr = _cache->c_shdr;
2613 		char		*relname = _cache->c_name;
2614 		Conv_inv_buf_t	inv_buf;
2615 
2616 		if (((type = shdr->sh_type) != SHT_RELA) &&
2617 		    (type != SHT_REL))
2618 			continue;
2619 		if (!match(MATCH_F_ALL, relname, cnt, type))
2620 			continue;
2621 
2622 		/*
2623 		 * Decide entry size.
2624 		 */
2625 		if (((relsize = shdr->sh_entsize) == 0) ||
2626 		    (relsize > shdr->sh_size)) {
2627 			if (type == SHT_RELA)
2628 				relsize = sizeof (Rela);
2629 			else
2630 				relsize = sizeof (Rel);
2631 		}
2632 
2633 		/*
2634 		 * Determine the number of relocations available.
2635 		 */
2636 		if (shdr->sh_size == 0) {
2637 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
2638 			    file, relname);
2639 			continue;
2640 		}
2641 		if (_cache->c_data == NULL)
2642 			continue;
2643 
2644 		rels = _cache->c_data->d_buf;
2645 		relnum = shdr->sh_size / relsize;
2646 
2647 		/*
2648 		 * Get the data buffer for the associated symbol table and
2649 		 * string table.
2650 		 */
2651 		if (stringtbl(cache, 1, cnt, shnum, file,
2652 		    &symnum, &symsec, &strsec) == 0)
2653 			continue;
2654 
2655 		syms = symsec->c_data->d_buf;
2656 
2657 		/*
2658 		 * Loop through the relocation entries.
2659 		 */
2660 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2661 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_RELOC), _cache->c_name);
2662 		Elf_reloc_title(0, ELF_DBG_ELFDUMP, type);
2663 
2664 		for (relndx = 0; relndx < relnum; relndx++,
2665 		    rels = (void *)((char *)rels + relsize)) {
2666 			Half		mach = ehdr->e_machine;
2667 			char		section[BUFSIZ];
2668 			const char	*symname;
2669 			Word		symndx, reltype;
2670 			Rela		*rela;
2671 			Rel		*rel;
2672 
2673 			/*
2674 			 * Unravel the relocation and determine the symbol with
2675 			 * which this relocation is associated.
2676 			 */
2677 			if (type == SHT_RELA) {
2678 				rela = (Rela *)rels;
2679 				symndx = ELF_R_SYM(rela->r_info);
2680 				reltype = ELF_R_TYPE(rela->r_info, mach);
2681 			} else {
2682 				rel = (Rel *)rels;
2683 				symndx = ELF_R_SYM(rel->r_info);
2684 				reltype = ELF_R_TYPE(rel->r_info, mach);
2685 			}
2686 
2687 			symname = relsymname(cache, _cache, strsec, symndx,
2688 			    symnum, relndx, syms, section, BUFSIZ, file);
2689 
2690 			/*
2691 			 * A zero symbol index is only valid for a few
2692 			 * relocations.
2693 			 */
2694 			if (symndx == 0) {
2695 				int	badrel = 0;
2696 
2697 				if ((mach == EM_SPARC) ||
2698 				    (mach == EM_SPARC32PLUS) ||
2699 				    (mach == EM_SPARCV9)) {
2700 					if ((reltype != R_SPARC_NONE) &&
2701 					    (reltype != R_SPARC_REGISTER) &&
2702 					    (reltype != R_SPARC_RELATIVE))
2703 						badrel++;
2704 				} else if (mach == EM_386) {
2705 					if ((reltype != R_386_NONE) &&
2706 					    (reltype != R_386_RELATIVE))
2707 						badrel++;
2708 				} else if (mach == EM_AMD64) {
2709 					if ((reltype != R_AMD64_NONE) &&
2710 					    (reltype != R_AMD64_RELATIVE))
2711 						badrel++;
2712 				}
2713 
2714 				if (badrel) {
2715 					(void) fprintf(stderr,
2716 					    MSG_INTL(MSG_ERR_BADREL1), file,
2717 					    conv_reloc_type(mach, reltype,
2718 					    0, &inv_buf));
2719 				}
2720 			}
2721 
2722 			Elf_reloc_entry_1(0, ELF_DBG_ELFDUMP,
2723 			    MSG_ORIG(MSG_STR_EMPTY), ehdr->e_machine, type,
2724 			    rels, relname, symname, 0);
2725 		}
2726 	}
2727 }
2728 
2729 
2730 /*
2731  * This value controls which test dyn_test() performs.
2732  */
2733 typedef enum { DYN_TEST_ADDR, DYN_TEST_SIZE, DYN_TEST_ENTSIZE } dyn_test_t;
2734 
2735 /*
2736  * Used by dynamic() to compare the value of a dynamic element against
2737  * the starting address of the section it references.
2738  *
2739  * entry:
2740  *	test_type - Specify which dyn item is being tested.
2741  *	sh_type - SHT_* type value for required section.
2742  *	sec_cache - Cache entry for section, or NULL if the object lacks
2743  *		a section of this type.
2744  *	dyn - Dyn entry to be tested
2745  *	dynsec_cnt - # of dynamic section being examined. The first
2746  *		dynamic section is 1, the next is 2, and so on...
2747  *	ehdr - ELF header for file
2748  *	file - Name of file
2749  */
2750 static void
2751 dyn_test(dyn_test_t test_type, Word sh_type, Cache *sec_cache, Dyn *dyn,
2752     Word dynsec_cnt, Ehdr *ehdr, uchar_t osabi, const char *file)
2753 {
2754 	Conv_inv_buf_t	buf1, buf2;
2755 
2756 	/*
2757 	 * These tests are based around the implicit assumption that
2758 	 * there is only one dynamic section in an object, and also only
2759 	 * one of the sections it references. We have therefore gathered
2760 	 * all of the necessary information to test this in a single pass
2761 	 * over the section headers, which is very efficient. We are not
2762 	 * aware of any case where more than one dynamic section would
2763 	 * be meaningful in an ELF object, so this is a reasonable solution.
2764 	 *
2765 	 * To test multiple dynamic sections correctly would be more
2766 	 * expensive in code and time. We would have to build a data structure
2767 	 * containing all the dynamic elements. Then, we would use the address
2768 	 * to locate the section it references and ensure the section is of
2769 	 * the right type and that the address in the dynamic element is
2770 	 * to the start of the section. Then, we could check the size and
2771 	 * entsize values against those same sections. This is O(n^2), and
2772 	 * also complicated.
2773 	 *
2774 	 * In the highly unlikely case that there is more than one dynamic
2775 	 * section, we only test the first one, and simply allow the values
2776 	 * of the subsequent one to be displayed unchallenged.
2777 	 */
2778 	if (dynsec_cnt != 1)
2779 		return;
2780 
2781 	/*
2782 	 * A DT_ item that references a section address should always find
2783 	 * the section in the file.
2784 	 */
2785 	if (sec_cache == NULL) {
2786 		const char *name;
2787 
2788 		/*
2789 		 * Supply section names instead of section types for
2790 		 * things that reference progbits so that the error
2791 		 * message will make more sense.
2792 		 */
2793 		switch (dyn->d_tag) {
2794 		case DT_INIT:
2795 			name = MSG_ORIG(MSG_ELF_INIT);
2796 			break;
2797 		case DT_FINI:
2798 			name = MSG_ORIG(MSG_ELF_FINI);
2799 			break;
2800 		default:
2801 			name = conv_sec_type(osabi, ehdr->e_machine,
2802 			    sh_type, 0, &buf1);
2803 			break;
2804 		}
2805 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_DYNNOBCKSEC), file,
2806 		    name, conv_dyn_tag(dyn->d_tag, osabi, ehdr->e_machine,
2807 		    CONV_FMT_ALT_CF, &buf2));
2808 		return;
2809 	}
2810 
2811 
2812 	switch (test_type) {
2813 	case DYN_TEST_ADDR:
2814 		/* The section address should match the DT_ item value */
2815 		if (dyn->d_un.d_val != sec_cache->c_shdr->sh_addr)
2816 			(void) fprintf(stderr,
2817 			    MSG_INTL(MSG_ERR_DYNBADADDR), file,
2818 			    conv_dyn_tag(dyn->d_tag, osabi, ehdr->e_machine,
2819 			    CONV_FMT_ALT_CF, &buf1), EC_ADDR(dyn->d_un.d_val),
2820 			    sec_cache->c_ndx, sec_cache->c_name,
2821 			    EC_ADDR(sec_cache->c_shdr->sh_addr));
2822 		break;
2823 
2824 	case DYN_TEST_SIZE:
2825 		/* The section size should match the DT_ item value */
2826 		if (dyn->d_un.d_val != sec_cache->c_shdr->sh_size)
2827 			(void) fprintf(stderr,
2828 			    MSG_INTL(MSG_ERR_DYNBADSIZE), file,
2829 			    conv_dyn_tag(dyn->d_tag, osabi, ehdr->e_machine,
2830 			    CONV_FMT_ALT_CF, &buf1), EC_XWORD(dyn->d_un.d_val),
2831 			    sec_cache->c_ndx, sec_cache->c_name,
2832 			    EC_XWORD(sec_cache->c_shdr->sh_size));
2833 		break;
2834 
2835 	case DYN_TEST_ENTSIZE:
2836 		/* The sh_entsize value should match the DT_ item value */
2837 		if (dyn->d_un.d_val != sec_cache->c_shdr->sh_entsize)
2838 			(void) fprintf(stderr,
2839 			    MSG_INTL(MSG_ERR_DYNBADENTSIZE), file,
2840 			    conv_dyn_tag(dyn->d_tag, osabi, ehdr->e_machine,
2841 			    CONV_FMT_ALT_CF, &buf1), EC_XWORD(dyn->d_un.d_val),
2842 			    sec_cache->c_ndx, sec_cache->c_name,
2843 			    EC_XWORD(sec_cache->c_shdr->sh_entsize));
2844 		break;
2845 	}
2846 }
2847 
2848 /*
2849  * There are some DT_ entries that have corresponding symbols
2850  * (e.g. DT_INIT and _init). It is expected that these items will
2851  * both have the same value if both are present. This routine
2852  * examines the well known symbol tables for such symbols and
2853  * issues warnings for any that don't match.
2854  *
2855  * entry:
2856  *	dyn - Dyn entry to be tested
2857  *	symname - Name of symbol that corresponds to dyn
2858  *	symtab_cache, dynsym_cache, ldynsym_cache - Symbol tables to check
2859  *	target_cache - Section the symname section is expected to be
2860  *		associated with.
2861  *	cache - Cache of all section headers
2862  *	shnum - # of sections in cache
2863  *	ehdr - ELF header for file
2864  *	osabi - OSABI to apply when interpreting object
2865  *	file - Name of file
2866  */
2867 static void
2868 dyn_symtest(Dyn *dyn, const char *symname, Cache *symtab_cache,
2869     Cache *dynsym_cache, Cache *ldynsym_cache, Cache *target_cache,
2870     Cache *cache, Word shnum, Ehdr *ehdr, uchar_t osabi, const char *file)
2871 {
2872 	Conv_inv_buf_t	buf;
2873 	int		i;
2874 	Sym		*sym;
2875 	Cache		*_cache;
2876 
2877 	for (i = 0; i < 3; i++) {
2878 		switch (i) {
2879 		case 0:
2880 			_cache = symtab_cache;
2881 			break;
2882 		case 1:
2883 			_cache = dynsym_cache;
2884 			break;
2885 		case 2:
2886 			_cache = ldynsym_cache;
2887 			break;
2888 		}
2889 
2890 		if ((_cache != NULL) &&
2891 		    symlookup(symname, cache, shnum, &sym, target_cache,
2892 		    _cache, file) && (sym->st_value != dyn->d_un.d_val))
2893 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_DYNSYMVAL),
2894 			    file, _cache->c_name, conv_dyn_tag(dyn->d_tag,
2895 			    osabi, ehdr->e_machine, CONV_FMT_ALT_CF, &buf),
2896 			    symname, EC_ADDR(sym->st_value));
2897 	}
2898 }
2899 
2900 /*
2901  * Search for and process a .dynamic section.
2902  */
2903 static void
2904 dynamic(Cache *cache, Word shnum, Ehdr *ehdr, uchar_t osabi, const char *file)
2905 {
2906 	struct {
2907 		Cache	*symtab;
2908 		Cache	*dynstr;
2909 		Cache	*dynsym;
2910 		Cache	*hash;
2911 		Cache	*fini;
2912 		Cache	*fini_array;
2913 		Cache	*init;
2914 		Cache	*init_array;
2915 		Cache	*preinit_array;
2916 		Cache	*rel;
2917 		Cache	*rela;
2918 		Cache	*sunw_cap;
2919 		Cache	*sunw_capinfo;
2920 		Cache	*sunw_capchain;
2921 		Cache	*sunw_ldynsym;
2922 		Cache	*sunw_move;
2923 		Cache	*sunw_syminfo;
2924 		Cache	*sunw_symsort;
2925 		Cache	*sunw_tlssort;
2926 		Cache	*sunw_verdef;
2927 		Cache	*sunw_verneed;
2928 		Cache	*sunw_versym;
2929 	} sec;
2930 	Word	dynsec_ndx;
2931 	Word	dynsec_num;
2932 	int	dynsec_cnt;
2933 	Word	cnt;
2934 	int	osabi_solaris = osabi == ELFOSABI_SOLARIS;
2935 
2936 	/*
2937 	 * Make a pass over all the sections, gathering section information
2938 	 * we'll need below.
2939 	 */
2940 	dynsec_num = 0;
2941 	bzero(&sec, sizeof (sec));
2942 	for (cnt = 1; cnt < shnum; cnt++) {
2943 		Cache	*_cache = &cache[cnt];
2944 
2945 		switch (_cache->c_shdr->sh_type) {
2946 		case SHT_DYNAMIC:
2947 			if (dynsec_num == 0) {
2948 				dynsec_ndx = cnt;
2949 
2950 				/* Does it have a valid string table? */
2951 				(void) stringtbl(cache, 0, cnt, shnum, file,
2952 				    0, 0, &sec.dynstr);
2953 			}
2954 			dynsec_num++;
2955 			break;
2956 
2957 
2958 		case SHT_PROGBITS:
2959 			/*
2960 			 * We want to detect the .init and .fini sections,
2961 			 * if present. These are SHT_PROGBITS, so all we
2962 			 * have to go on is the section name. Normally comparing
2963 			 * names is a bad idea, but there are some special
2964 			 * names (i.e. .init/.fini/.interp) that are very
2965 			 * difficult to use in any other context, and for
2966 			 * these symbols, we do the heuristic match.
2967 			 */
2968 			if (strcmp(_cache->c_name,
2969 			    MSG_ORIG(MSG_ELF_INIT)) == 0) {
2970 				if (sec.init == NULL)
2971 					sec.init = _cache;
2972 			} else if (strcmp(_cache->c_name,
2973 			    MSG_ORIG(MSG_ELF_FINI)) == 0) {
2974 				if (sec.fini == NULL)
2975 					sec.fini = _cache;
2976 			}
2977 			break;
2978 
2979 		case SHT_REL:
2980 			/*
2981 			 * We want the SHT_REL section with the lowest
2982 			 * offset. The linker gathers them together,
2983 			 * and puts the address of the first one
2984 			 * into the DT_REL dynamic element.
2985 			 */
2986 			if ((sec.rel == NULL) ||
2987 			    (_cache->c_shdr->sh_offset <
2988 			    sec.rel->c_shdr->sh_offset))
2989 				sec.rel = _cache;
2990 			break;
2991 
2992 		case SHT_RELA:
2993 			/* RELA is handled just like RELA above */
2994 			if ((sec.rela == NULL) ||
2995 			    (_cache->c_shdr->sh_offset <
2996 			    sec.rela->c_shdr->sh_offset))
2997 				sec.rela = _cache;
2998 			break;
2999 
3000 		/*
3001 		 * The GRAB macro is used for the simple case in which
3002 		 * we simply grab the first section of the desired type.
3003 		 */
3004 #define	GRAB(_sec_type, _sec_field) \
3005 		case _sec_type: \
3006 			if (sec._sec_field == NULL) \
3007 				sec._sec_field = _cache; \
3008 				break
3009 		GRAB(SHT_SYMTAB,	symtab);
3010 		GRAB(SHT_DYNSYM,	dynsym);
3011 		GRAB(SHT_FINI_ARRAY,	fini_array);
3012 		GRAB(SHT_HASH,		hash);
3013 		GRAB(SHT_INIT_ARRAY,	init_array);
3014 		GRAB(SHT_SUNW_move,	sunw_move);
3015 		GRAB(SHT_PREINIT_ARRAY,	preinit_array);
3016 		GRAB(SHT_SUNW_cap,	sunw_cap);
3017 		GRAB(SHT_SUNW_capinfo,	sunw_capinfo);
3018 		GRAB(SHT_SUNW_capchain,	sunw_capchain);
3019 		GRAB(SHT_SUNW_LDYNSYM,	sunw_ldynsym);
3020 		GRAB(SHT_SUNW_syminfo,	sunw_syminfo);
3021 		GRAB(SHT_SUNW_symsort,	sunw_symsort);
3022 		GRAB(SHT_SUNW_tlssort,	sunw_tlssort);
3023 		GRAB(SHT_SUNW_verdef,	sunw_verdef);
3024 		GRAB(SHT_SUNW_verneed,	sunw_verneed);
3025 		GRAB(SHT_SUNW_versym,	sunw_versym);
3026 #undef GRAB
3027 		}
3028 	}
3029 
3030 	/*
3031 	 * If no dynamic section, return immediately. If more than one
3032 	 * dynamic section, then something odd is going on and an error
3033 	 * is in order, but then continue on and display them all.
3034 	 */
3035 	if (dynsec_num == 0)
3036 		return;
3037 	if (dynsec_num > 1)
3038 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_MULTDYN),
3039 		    file, EC_WORD(dynsec_num));
3040 
3041 
3042 	dynsec_cnt = 0;
3043 	for (cnt = dynsec_ndx; (cnt < shnum) && (dynsec_cnt < dynsec_num);
3044 	    cnt++) {
3045 		Dyn	*dyn;
3046 		ulong_t	numdyn;
3047 		int	ndx, end_ndx;
3048 		Cache	*_cache = &cache[cnt], *strsec;
3049 		Shdr	*shdr = _cache->c_shdr;
3050 		int	dumped = 0;
3051 
3052 		if (shdr->sh_type != SHT_DYNAMIC)
3053 			continue;
3054 		dynsec_cnt++;
3055 
3056 		/*
3057 		 * Verify the associated string table section.
3058 		 */
3059 		if (stringtbl(cache, 0, cnt, shnum, file, 0, 0, &strsec) == 0)
3060 			continue;
3061 
3062 		if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
3063 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
3064 			    file, _cache->c_name);
3065 			continue;
3066 		}
3067 		if (_cache->c_data == NULL)
3068 			continue;
3069 
3070 		numdyn = shdr->sh_size / shdr->sh_entsize;
3071 		dyn = (Dyn *)_cache->c_data->d_buf;
3072 
3073 		/*
3074 		 * We expect the REL/RELA entries to reference the reloc
3075 		 * section with the lowest address. However, this is
3076 		 * not true for dumped objects. Detect if this object has
3077 		 * been dumped so that we can skip the reloc address test
3078 		 * in that case.
3079 		 */
3080 		for (ndx = 0; ndx < numdyn; dyn++, ndx++) {
3081 			if (dyn->d_tag == DT_FLAGS_1) {
3082 				dumped = (dyn->d_un.d_val & DF_1_CONFALT) != 0;
3083 				break;
3084 			}
3085 		}
3086 		dyn = (Dyn *)_cache->c_data->d_buf;
3087 
3088 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
3089 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_DYNAMIC), _cache->c_name);
3090 
3091 		Elf_dyn_title(0);
3092 
3093 		for (ndx = 0; ndx < numdyn; dyn++, ndx++) {
3094 			union {
3095 				Conv_inv_buf_t		inv;
3096 				Conv_dyn_flag_buf_t	flag;
3097 				Conv_dyn_flag1_buf_t	flag1;
3098 				Conv_dyn_posflag1_buf_t	posflag1;
3099 				Conv_dyn_feature1_buf_t	feature1;
3100 			} c_buf;
3101 			const char	*name = NULL;
3102 
3103 			/*
3104 			 * Print the information numerically, and if possible
3105 			 * as a string. If a string is available, name is
3106 			 * set to reference it.
3107 			 *
3108 			 * Also, take this opportunity to sanity check
3109 			 * the values of DT elements. In the code above,
3110 			 * we gathered information on sections that are
3111 			 * referenced by the dynamic section. Here, we
3112 			 * compare the attributes of those sections to
3113 			 * the DT_ items that reference them and report
3114 			 * on inconsistencies.
3115 			 *
3116 			 * Things not currently tested that could be improved
3117 			 * in later revisions include:
3118 			 *	- We don't check PLT or GOT related items
3119 			 *	- We don't handle computing the lengths of
3120 			 *		relocation arrays. To handle this
3121 			 *		requires examining data that spans
3122 			 *		across sections, in a contiguous span
3123 			 *		within a single segment.
3124 			 *	- DT_VERDEFNUM and DT_VERNEEDNUM can't be
3125 			 *		verified without parsing the sections.
3126 			 *	- We don't handle DT_SUNW_SYMSZ, which would
3127 			 *		be the sum of the lengths of .dynsym and
3128 			 *		.SUNW_ldynsym
3129 			 *	- DT_SUNW_STRPAD can't be verified other than
3130 			 *		to check that it's not larger than
3131 			 *		the string table.
3132 			 *	- Some items come in "all or none" clusters
3133 			 *		that give an address, element size,
3134 			 *		and data length in bytes. We don't
3135 			 *		verify that there are no missing items
3136 			 *		in such groups.
3137 			 */
3138 			switch (dyn->d_tag) {
3139 			case DT_NULL:
3140 				/*
3141 				 * Special case: DT_NULLs can come in groups
3142 				 * that we prefer to reduce to a single line.
3143 				 */
3144 				end_ndx = ndx;
3145 				while ((end_ndx < (numdyn - 1)) &&
3146 				    ((dyn + 1)->d_tag == DT_NULL)) {
3147 					dyn++;
3148 					end_ndx++;
3149 				}
3150 				Elf_dyn_null_entry(0, dyn, ndx, end_ndx);
3151 				ndx = end_ndx;
3152 				continue;
3153 
3154 			/*
3155 			 * String items all reference the dynstr. The string()
3156 			 * function does the necessary sanity checking.
3157 			 */
3158 			case DT_NEEDED:
3159 			case DT_SONAME:
3160 			case DT_FILTER:
3161 			case DT_AUXILIARY:
3162 			case DT_CONFIG:
3163 			case DT_RPATH:
3164 			case DT_RUNPATH:
3165 			case DT_USED:
3166 			case DT_DEPAUDIT:
3167 			case DT_AUDIT:
3168 				name = string(_cache, ndx, strsec,
3169 				    file, dyn->d_un.d_ptr);
3170 				break;
3171 
3172 			case DT_SUNW_AUXILIARY:
3173 			case DT_SUNW_FILTER:
3174 				if (osabi_solaris)
3175 					name = string(_cache, ndx, strsec,
3176 					    file, dyn->d_un.d_ptr);
3177 				break;
3178 
3179 			case DT_FLAGS:
3180 				name = conv_dyn_flag(dyn->d_un.d_val,
3181 				    0, &c_buf.flag);
3182 				break;
3183 			case DT_FLAGS_1:
3184 				name = conv_dyn_flag1(dyn->d_un.d_val, 0,
3185 				    &c_buf.flag1);
3186 				break;
3187 			case DT_POSFLAG_1:
3188 				name = conv_dyn_posflag1(dyn->d_un.d_val, 0,
3189 				    &c_buf.posflag1);
3190 				break;
3191 			case DT_FEATURE_1:
3192 				name = conv_dyn_feature1(dyn->d_un.d_val, 0,
3193 				    &c_buf.feature1);
3194 				break;
3195 			case DT_DEPRECATED_SPARC_REGISTER:
3196 				name = MSG_INTL(MSG_STR_DEPRECATED);
3197 				break;
3198 
3199 			case DT_SUNW_LDMACH:
3200 				if (!osabi_solaris)
3201 					break;
3202 				name = conv_ehdr_mach((Half)dyn->d_un.d_val,
3203 				    0, &c_buf.inv);
3204 				break;
3205 
3206 			/*
3207 			 * Cases below this point are strictly sanity checking,
3208 			 * and do not generate a name string. The TEST_ macros
3209 			 * are used to hide the boiler plate arguments neeeded
3210 			 * by dyn_test().
3211 			 */
3212 #define	TEST_ADDR(_sh_type, _sec_field) \
3213 				dyn_test(DYN_TEST_ADDR, _sh_type, \
3214 				    sec._sec_field, dyn, dynsec_cnt, ehdr, \
3215 				    osabi, file)
3216 #define	TEST_SIZE(_sh_type, _sec_field) \
3217 				dyn_test(DYN_TEST_SIZE, _sh_type, \
3218 				    sec._sec_field, dyn, dynsec_cnt, ehdr, \
3219 				    osabi, file)
3220 #define	TEST_ENTSIZE(_sh_type, _sec_field) \
3221 				dyn_test(DYN_TEST_ENTSIZE, _sh_type, \
3222 				    sec._sec_field, dyn, dynsec_cnt, ehdr, \
3223 				    osabi, file)
3224 
3225 			case DT_FINI:
3226 				dyn_symtest(dyn, MSG_ORIG(MSG_SYM_FINI),
3227 				    sec.symtab, sec.dynsym, sec.sunw_ldynsym,
3228 				    sec.fini, cache, shnum, ehdr, osabi, file);
3229 				TEST_ADDR(SHT_PROGBITS, fini);
3230 				break;
3231 
3232 			case DT_FINI_ARRAY:
3233 				TEST_ADDR(SHT_FINI_ARRAY, fini_array);
3234 				break;
3235 
3236 			case DT_FINI_ARRAYSZ:
3237 				TEST_SIZE(SHT_FINI_ARRAY, fini_array);
3238 				break;
3239 
3240 			case DT_HASH:
3241 				TEST_ADDR(SHT_HASH, hash);
3242 				break;
3243 
3244 			case DT_INIT:
3245 				dyn_symtest(dyn, MSG_ORIG(MSG_SYM_INIT),
3246 				    sec.symtab, sec.dynsym, sec.sunw_ldynsym,
3247 				    sec.init, cache, shnum, ehdr, osabi, file);
3248 				TEST_ADDR(SHT_PROGBITS, init);
3249 				break;
3250 
3251 			case DT_INIT_ARRAY:
3252 				TEST_ADDR(SHT_INIT_ARRAY, init_array);
3253 				break;
3254 
3255 			case DT_INIT_ARRAYSZ:
3256 				TEST_SIZE(SHT_INIT_ARRAY, init_array);
3257 				break;
3258 
3259 			case DT_MOVEENT:
3260 				TEST_ENTSIZE(SHT_SUNW_move, sunw_move);
3261 				break;
3262 
3263 			case DT_MOVESZ:
3264 				TEST_SIZE(SHT_SUNW_move, sunw_move);
3265 				break;
3266 
3267 			case DT_MOVETAB:
3268 				TEST_ADDR(SHT_SUNW_move, sunw_move);
3269 				break;
3270 
3271 			case DT_PREINIT_ARRAY:
3272 				TEST_ADDR(SHT_PREINIT_ARRAY, preinit_array);
3273 				break;
3274 
3275 			case DT_PREINIT_ARRAYSZ:
3276 				TEST_SIZE(SHT_PREINIT_ARRAY, preinit_array);
3277 				break;
3278 
3279 			case DT_REL:
3280 				if (!dumped)
3281 					TEST_ADDR(SHT_REL, rel);
3282 				break;
3283 
3284 			case DT_RELENT:
3285 				TEST_ENTSIZE(SHT_REL, rel);
3286 				break;
3287 
3288 			case DT_RELA:
3289 				if (!dumped)
3290 					TEST_ADDR(SHT_RELA, rela);
3291 				break;
3292 
3293 			case DT_RELAENT:
3294 				TEST_ENTSIZE(SHT_RELA, rela);
3295 				break;
3296 
3297 			case DT_STRTAB:
3298 				TEST_ADDR(SHT_STRTAB, dynstr);
3299 				break;
3300 
3301 			case DT_STRSZ:
3302 				TEST_SIZE(SHT_STRTAB, dynstr);
3303 				break;
3304 
3305 			case DT_SUNW_CAP:
3306 				if (osabi_solaris)
3307 					TEST_ADDR(SHT_SUNW_cap, sunw_cap);
3308 				break;
3309 
3310 			case DT_SUNW_CAPINFO:
3311 				if (osabi_solaris)
3312 					TEST_ADDR(SHT_SUNW_capinfo,
3313 					    sunw_capinfo);
3314 				break;
3315 
3316 			case DT_SUNW_CAPCHAIN:
3317 				if (osabi_solaris)
3318 					TEST_ADDR(SHT_SUNW_capchain,
3319 					    sunw_capchain);
3320 				break;
3321 
3322 			case DT_SUNW_SYMTAB:
3323 				TEST_ADDR(SHT_SUNW_LDYNSYM, sunw_ldynsym);
3324 				break;
3325 
3326 			case DT_SYMENT:
3327 				TEST_ENTSIZE(SHT_DYNSYM, dynsym);
3328 				break;
3329 
3330 			case DT_SYMINENT:
3331 				TEST_ENTSIZE(SHT_SUNW_syminfo, sunw_syminfo);
3332 				break;
3333 
3334 			case DT_SYMINFO:
3335 				TEST_ADDR(SHT_SUNW_syminfo, sunw_syminfo);
3336 				break;
3337 
3338 			case DT_SYMINSZ:
3339 				TEST_SIZE(SHT_SUNW_syminfo, sunw_syminfo);
3340 				break;
3341 
3342 			case DT_SYMTAB:
3343 				TEST_ADDR(SHT_DYNSYM, dynsym);
3344 				break;
3345 
3346 			case DT_SUNW_SORTENT:
3347 				/*
3348 				 * This entry is related to both the symsort and
3349 				 * tlssort sections.
3350 				 */
3351 				if (osabi_solaris) {
3352 					int test_tls =
3353 					    (sec.sunw_tlssort != NULL);
3354 					int test_sym =
3355 					    (sec.sunw_symsort != NULL) ||
3356 					    !test_tls;
3357 					if (test_sym)
3358 						TEST_ENTSIZE(SHT_SUNW_symsort,
3359 						    sunw_symsort);
3360 					if (test_tls)
3361 						TEST_ENTSIZE(SHT_SUNW_tlssort,
3362 						    sunw_tlssort);
3363 				}
3364 				break;
3365 
3366 
3367 			case DT_SUNW_SYMSORT:
3368 				if (osabi_solaris)
3369 					TEST_ADDR(SHT_SUNW_symsort,
3370 					    sunw_symsort);
3371 				break;
3372 
3373 			case DT_SUNW_SYMSORTSZ:
3374 				if (osabi_solaris)
3375 					TEST_SIZE(SHT_SUNW_symsort,
3376 					    sunw_symsort);
3377 				break;
3378 
3379 			case DT_SUNW_TLSSORT:
3380 				if (osabi_solaris)
3381 					TEST_ADDR(SHT_SUNW_tlssort,
3382 					    sunw_tlssort);
3383 				break;
3384 
3385 			case DT_SUNW_TLSSORTSZ:
3386 				if (osabi_solaris)
3387 					TEST_SIZE(SHT_SUNW_tlssort,
3388 					    sunw_tlssort);
3389 				break;
3390 
3391 			case DT_VERDEF:
3392 				TEST_ADDR(SHT_SUNW_verdef, sunw_verdef);
3393 				break;
3394 
3395 			case DT_VERNEED:
3396 				TEST_ADDR(SHT_SUNW_verneed, sunw_verneed);
3397 				break;
3398 
3399 			case DT_VERSYM:
3400 				TEST_ADDR(SHT_SUNW_versym, sunw_versym);
3401 				break;
3402 #undef TEST_ADDR
3403 #undef TEST_SIZE
3404 #undef TEST_ENTSIZE
3405 			}
3406 
3407 			if (name == NULL)
3408 				name = MSG_ORIG(MSG_STR_EMPTY);
3409 			Elf_dyn_entry(0, dyn, ndx, name,
3410 			    osabi, ehdr->e_machine);
3411 		}
3412 	}
3413 }
3414 
3415 /*
3416  * Search for and process a MOVE section.
3417  */
3418 static void
3419 move(Cache *cache, Word shnum, const char *file, uint_t flags)
3420 {
3421 	Word		cnt;
3422 	const char	*fmt = NULL;
3423 
3424 	for (cnt = 1; cnt < shnum; cnt++) {
3425 		Word	movenum, symnum, ndx;
3426 		Sym	*syms;
3427 		Cache	*_cache = &cache[cnt];
3428 		Shdr	*shdr = _cache->c_shdr;
3429 		Cache	*symsec, *strsec;
3430 		Move	*move;
3431 
3432 		if (shdr->sh_type != SHT_SUNW_move)
3433 			continue;
3434 		if (!match(MATCH_F_ALL, _cache->c_name, cnt, shdr->sh_type))
3435 			continue;
3436 
3437 		/*
3438 		 * Determine the move data and number.
3439 		 */
3440 		if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
3441 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
3442 			    file, _cache->c_name);
3443 			continue;
3444 		}
3445 		if (_cache->c_data == NULL)
3446 			continue;
3447 
3448 		move = (Move *)_cache->c_data->d_buf;
3449 		movenum = shdr->sh_size / shdr->sh_entsize;
3450 
3451 		/*
3452 		 * Get the data buffer for the associated symbol table and
3453 		 * string table.
3454 		 */
3455 		if (stringtbl(cache, 1, cnt, shnum, file,
3456 		    &symnum, &symsec, &strsec) == 0)
3457 			return;
3458 
3459 		syms = (Sym *)symsec->c_data->d_buf;
3460 
3461 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
3462 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_MOVE), _cache->c_name);
3463 		dbg_print(0, MSG_INTL(MSG_MOVE_TITLE));
3464 
3465 		if (fmt == NULL)
3466 			fmt = MSG_INTL(MSG_MOVE_ENTRY);
3467 
3468 		for (ndx = 0; ndx < movenum; move++, ndx++) {
3469 			const char	*symname;
3470 			char		index[MAXNDXSIZE], section[BUFSIZ];
3471 			Word		symndx, shndx;
3472 			Sym		*sym;
3473 
3474 			/*
3475 			 * Check for null entries
3476 			 */
3477 			if ((move->m_info == 0) && (move->m_value == 0) &&
3478 			    (move->m_poffset == 0) && (move->m_repeat == 0) &&
3479 			    (move->m_stride == 0)) {
3480 				dbg_print(0, fmt, MSG_ORIG(MSG_STR_EMPTY),
3481 				    EC_XWORD(move->m_poffset), 0, 0, 0,
3482 				    EC_LWORD(0), MSG_ORIG(MSG_STR_EMPTY));
3483 				continue;
3484 			}
3485 			if (((symndx = ELF_M_SYM(move->m_info)) == 0) ||
3486 			    (symndx >= symnum)) {
3487 				(void) fprintf(stderr,
3488 				    MSG_INTL(MSG_ERR_BADMINFO), file,
3489 				    _cache->c_name, EC_XWORD(move->m_info));
3490 
3491 				(void) snprintf(index, MAXNDXSIZE,
3492 				    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(symndx));
3493 				dbg_print(0, fmt, index,
3494 				    EC_XWORD(move->m_poffset),
3495 				    ELF_M_SIZE(move->m_info), move->m_repeat,
3496 				    move->m_stride, move->m_value,
3497 				    MSG_INTL(MSG_STR_UNKNOWN));
3498 				continue;
3499 			}
3500 
3501 			symname = relsymname(cache, _cache, strsec,
3502 			    symndx, symnum, ndx, syms, section, BUFSIZ, file);
3503 			sym = (Sym *)(syms + symndx);
3504 
3505 			/*
3506 			 * Additional sanity check.
3507 			 */
3508 			shndx = sym->st_shndx;
3509 			if (!((shndx == SHN_COMMON) ||
3510 			    (((shndx >= 1) && (shndx <= shnum)) &&
3511 			    (cache[shndx].c_shdr)->sh_type == SHT_NOBITS))) {
3512 				(void) fprintf(stderr,
3513 				    MSG_INTL(MSG_ERR_BADSYM2), file,
3514 				    _cache->c_name, EC_WORD(symndx),
3515 				    demangle(symname, flags));
3516 			}
3517 
3518 			(void) snprintf(index, MAXNDXSIZE,
3519 			    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(symndx));
3520 			dbg_print(0, fmt, index, EC_XWORD(move->m_poffset),
3521 			    ELF_M_SIZE(move->m_info), move->m_repeat,
3522 			    move->m_stride, move->m_value,
3523 			    demangle(symname, flags));
3524 		}
3525 	}
3526 }
3527 
3528 /*
3529  * parse_note_t is used to track the state used by parse_note_entry()
3530  * between calls, and also to return the results of each call.
3531  */
3532 typedef struct {
3533 	/* pns_ fields track progress through the data */
3534 	const char	*pns_file;	/* File name */
3535 	Cache		*pns_cache;	/* Note section cache entry */
3536 	size_t		pns_size;	/* # unprocessed data bytes */
3537 	Word		*pns_data;	/* # to next unused data byte */
3538 
3539 	/* pn_ fields return the results for a single call */
3540 	Word		pn_namesz;	/* Value of note namesz field */
3541 	Word		pn_descsz;	/* Value of note descsz field */
3542 	Word		pn_type;	/* Value of note type field */
3543 	const char	*pn_name;	/* if (namesz > 0) ptr to name bytes */
3544 	const char	*pn_desc;	/* if (descsx > 0) ptr to data bytes */
3545 } parse_note_t;
3546 
3547 /*
3548  * Extract the various sub-parts of a note entry, and advance the
3549  * data pointer past it.
3550  *
3551  * entry:
3552  *	The state pns_ fields contain current values for the Note section
3553  *
3554  * exit:
3555  *	On success, True (1) is returned, the state pns_ fields have been
3556  *	advanced to point at the start of the next entry, and the information
3557  *	for the recovered note entry is found in the state pn_ fields.
3558  *
3559  *	On failure, False (0) is returned. The values contained in state
3560  *	are undefined.
3561  */
3562 static int
3563 parse_note_entry(parse_note_t *state)
3564 {
3565 	size_t	pad, noteoff;
3566 
3567 	noteoff = (Word)state->pns_cache->c_data->d_size - state->pns_size;
3568 	/*
3569 	 * Make sure we can at least reference the 3 initial entries
3570 	 * (4-byte words) of the note information block.
3571 	 */
3572 	if (state->pns_size >= (sizeof (Word) * 3)) {
3573 		state->pns_size -= (sizeof (Word) * 3);
3574 	} else {
3575 		(void) fprintf(stderr, MSG_INTL(MSG_NOTE_BADDATASZ),
3576 		    state->pns_file, state->pns_cache->c_name,
3577 		    EC_WORD(noteoff));
3578 		return (0);
3579 	}
3580 
3581 	/*
3582 	 * Make sure any specified name string can be referenced.
3583 	 */
3584 	if ((state->pn_namesz = *state->pns_data++) != 0) {
3585 		if (state->pns_size >= state->pn_namesz) {
3586 			state->pns_size -= state->pn_namesz;
3587 		} else {
3588 			(void) fprintf(stderr, MSG_INTL(MSG_NOTE_BADNMSZ),
3589 			    state->pns_file, state->pns_cache->c_name,
3590 			    EC_WORD(noteoff), EC_WORD(state->pn_namesz));
3591 			return (0);
3592 		}
3593 	}
3594 
3595 	/*
3596 	 * Make sure any specified descriptor can be referenced.
3597 	 */
3598 	if ((state->pn_descsz = *state->pns_data++) != 0) {
3599 		/*
3600 		 * If namesz isn't a 4-byte multiple, account for any
3601 		 * padding that must exist before the descriptor.
3602 		 */
3603 		if ((pad = (state->pn_namesz & (sizeof (Word) - 1))) != 0) {
3604 			pad = sizeof (Word) - pad;
3605 			state->pns_size -= pad;
3606 		}
3607 		if (state->pns_size >= state->pn_descsz) {
3608 			state->pns_size -= state->pn_descsz;
3609 		} else {
3610 			(void) fprintf(stderr, MSG_INTL(MSG_NOTE_BADDESZ),
3611 			    state->pns_file, state->pns_cache->c_name,
3612 			    EC_WORD(noteoff), EC_WORD(state->pn_namesz));
3613 			return (0);
3614 		}
3615 	}
3616 
3617 	state->pn_type = *state->pns_data++;
3618 
3619 	/* Name */
3620 	if (state->pn_namesz) {
3621 		state->pn_name = (char *)state->pns_data;
3622 		pad = (state->pn_namesz +
3623 		    (sizeof (Word) - 1)) & ~(sizeof (Word) - 1);
3624 		/* LINTED */
3625 		state->pns_data = (Word *)(state->pn_name + pad);
3626 	}
3627 
3628 	/*
3629 	 * If multiple information blocks exist within a .note section
3630 	 * account for any padding that must exist before the next
3631 	 * information block.
3632 	 */
3633 	if ((pad = (state->pn_descsz & (sizeof (Word) - 1))) != 0) {
3634 		pad = sizeof (Word) - pad;
3635 		if (state->pns_size > pad)
3636 			state->pns_size -= pad;
3637 	}
3638 
3639 	/* Data */
3640 	if (state->pn_descsz) {
3641 		state->pn_desc = (const char *)state->pns_data;
3642 		/* LINTED */
3643 		state->pns_data = (Word *)(state->pn_desc +
3644 		    state->pn_descsz + pad);
3645 	}
3646 
3647 	return (1);
3648 }
3649 
3650 /*
3651  * Callback function for use with conv_str_to_c_literal() below.
3652  */
3653 /*ARGSUSED2*/
3654 static void
3655 c_literal_cb(const void *ptr, size_t size, void *uvalue)
3656 {
3657 	(void) fwrite(ptr, size, 1, stdout);
3658 }
3659 
3660 /*
3661  * Traverse a note section analyzing each note information block.
3662  * The data buffers size is used to validate references before they are made,
3663  * and is decremented as each element is processed.
3664  */
3665 void
3666 note_entry(Cache *cache, Word *data, size_t size, Ehdr *ehdr, const char *file)
3667 {
3668 	int		cnt = 0;
3669 	int		is_corenote;
3670 	int		do_swap;
3671 	Conv_inv_buf_t	inv_buf;
3672 	parse_note_t	pnstate;
3673 
3674 	pnstate.pns_file = file;
3675 	pnstate.pns_cache = cache;
3676 	pnstate.pns_size = size;
3677 	pnstate.pns_data = data;
3678 	do_swap = _elf_sys_encoding() != ehdr->e_ident[EI_DATA];
3679 
3680 	/*
3681 	 * Print out a single `note' information block.
3682 	 */
3683 	while (pnstate.pns_size > 0) {
3684 
3685 		if (parse_note_entry(&pnstate) == 0)
3686 			return;
3687 
3688 		/*
3689 		 * Is this a Solaris core note? Such notes all have
3690 		 * the name "CORE".
3691 		 */
3692 		is_corenote = (ehdr->e_type == ET_CORE) &&
3693 		    (pnstate.pn_namesz == (MSG_STR_CORE_SIZE + 1)) &&
3694 		    (strncmp(MSG_ORIG(MSG_STR_CORE), pnstate.pn_name,
3695 		    MSG_STR_CORE_SIZE + 1) == 0);
3696 
3697 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
3698 		dbg_print(0, MSG_INTL(MSG_FMT_NOTEENTNDX), EC_WORD(cnt));
3699 		cnt++;
3700 		dbg_print(0, MSG_ORIG(MSG_NOTE_NAMESZ),
3701 		    EC_WORD(pnstate.pn_namesz));
3702 		dbg_print(0, MSG_ORIG(MSG_NOTE_DESCSZ),
3703 		    EC_WORD(pnstate.pn_descsz));
3704 
3705 		if (is_corenote)
3706 			dbg_print(0, MSG_ORIG(MSG_NOTE_TYPE_STR),
3707 			    conv_cnote_type(pnstate.pn_type, 0, &inv_buf));
3708 		else
3709 			dbg_print(0, MSG_ORIG(MSG_NOTE_TYPE),
3710 			    EC_WORD(pnstate.pn_type));
3711 		if (pnstate.pn_namesz) {
3712 			dbg_print(0, MSG_ORIG(MSG_NOTE_NAME));
3713 			/*
3714 			 * The name string can contain embedded 'null'
3715 			 * bytes and/or unprintable characters. Also,
3716 			 * the final NULL is documented in the ELF ABI
3717 			 * as being included in the namesz. So, display
3718 			 * the name using C literal string notation, and
3719 			 * include the terminating NULL in the output.
3720 			 * We don't show surrounding double quotes, as
3721 			 * that implies the termination that we are showing
3722 			 * explicitly.
3723 			 */
3724 			(void) fwrite(MSG_ORIG(MSG_STR_8SP),
3725 			    MSG_STR_8SP_SIZE, 1, stdout);
3726 			conv_str_to_c_literal(pnstate.pn_name,
3727 			    pnstate.pn_namesz, c_literal_cb, NULL);
3728 			dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
3729 		}
3730 
3731 		if (pnstate.pn_descsz) {
3732 			int		hexdump = 1;
3733 
3734 			/*
3735 			 * If this is a core note, let the corenote()
3736 			 * function handle it.
3737 			 */
3738 			if (is_corenote) {
3739 				/* We only issue the bad arch error once */
3740 				static int	badnote_done = 0;
3741 				corenote_ret_t	corenote_ret;
3742 
3743 				corenote_ret = corenote(ehdr->e_machine,
3744 				    do_swap, pnstate.pn_type, pnstate.pn_desc,
3745 				    pnstate.pn_descsz);
3746 				switch (corenote_ret) {
3747 				case CORENOTE_R_OK_DUMP:
3748 					hexdump = 1;
3749 					break;
3750 				case CORENOTE_R_OK:
3751 					hexdump = 0;
3752 					break;
3753 				case CORENOTE_R_BADDATA:
3754 					(void) fprintf(stderr,
3755 					    MSG_INTL(MSG_NOTE_BADCOREDATA),
3756 					    file);
3757 					break;
3758 				case CORENOTE_R_BADARCH:
3759 					if (badnote_done)
3760 						break;
3761 					(void) fprintf(stderr,
3762 					    MSG_INTL(MSG_NOTE_BADCOREARCH),
3763 					    file,
3764 					    conv_ehdr_mach(ehdr->e_machine,
3765 					    0, &inv_buf));
3766 					break;
3767 				case CORENOTE_R_BADTYPE:
3768 					(void) fprintf(stderr,
3769 					    MSG_INTL(MSG_NOTE_BADCORETYPE),
3770 					    file,
3771 					    EC_WORD(pnstate.pn_type));
3772 					break;
3773 
3774 				}
3775 			}
3776 
3777 			/*
3778 			 * The default thing when we don't understand
3779 			 * the note data is to display it as hex bytes.
3780 			 */
3781 			if (hexdump) {
3782 				dbg_print(0, MSG_ORIG(MSG_NOTE_DESC));
3783 				dump_hex_bytes(pnstate.pn_desc,
3784 				    pnstate.pn_descsz, 8, 4, 4);
3785 			}
3786 		}
3787 	}
3788 }
3789 
3790 /*
3791  * Search for and process .note sections.
3792  *
3793  * Returns the number of note sections seen.
3794  */
3795 static Word
3796 note(Cache *cache, Word shnum, Ehdr *ehdr, const char *file)
3797 {
3798 	Word	cnt, note_cnt = 0;
3799 
3800 	/*
3801 	 * Otherwise look for any .note sections.
3802 	 */
3803 	for (cnt = 1; cnt < shnum; cnt++) {
3804 		Cache	*_cache = &cache[cnt];
3805 		Shdr	*shdr = _cache->c_shdr;
3806 
3807 		if (shdr->sh_type != SHT_NOTE)
3808 			continue;
3809 		note_cnt++;
3810 		if (!match(MATCH_F_ALL, _cache->c_name, cnt, shdr->sh_type))
3811 			continue;
3812 
3813 		/*
3814 		 * As these sections are often hand rolled, make sure they're
3815 		 * properly aligned before proceeding, and issue an error
3816 		 * as necessary.
3817 		 *
3818 		 * Note that we will continue on to display the note even
3819 		 * if it has bad alignment. We can do this safely, because
3820 		 * libelf knows the alignment required for SHT_NOTE, and
3821 		 * takes steps to deliver a properly aligned buffer to us
3822 		 * even if the actual file is misaligned.
3823 		 */
3824 		if (shdr->sh_offset & (sizeof (Word) - 1))
3825 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADALIGN),
3826 			    file, _cache->c_name);
3827 
3828 		if (_cache->c_data == NULL)
3829 			continue;
3830 
3831 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
3832 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_NOTE), _cache->c_name);
3833 		note_entry(_cache, (Word *)_cache->c_data->d_buf,
3834 		/* LINTED */
3835 		    (Word)_cache->c_data->d_size, ehdr, file);
3836 	}
3837 
3838 	return (note_cnt);
3839 }
3840 
3841 /*
3842  * The Linux Standard Base defines a special note named .note.ABI-tag
3843  * that is used to maintain Linux ABI information. Presence of this section
3844  * is a strong indication that the object should be considered to be
3845  * ELFOSABI_LINUX.
3846  *
3847  * This function returns True (1) if such a note is seen, and False (0)
3848  * otherwise.
3849  */
3850 static int
3851 has_linux_abi_note(Cache *cache, Word shnum, const char *file)
3852 {
3853 	Word	cnt;
3854 
3855 	for (cnt = 1; cnt < shnum; cnt++) {
3856 		parse_note_t	pnstate;
3857 		Cache		*_cache = &cache[cnt];
3858 		Shdr		*shdr = _cache->c_shdr;
3859 
3860 		/*
3861 		 * Section must be SHT_NOTE, must have the name
3862 		 * .note.ABI-tag, and must have data.
3863 		 */
3864 		if ((shdr->sh_type != SHT_NOTE) ||
3865 		    (strcmp(MSG_ORIG(MSG_STR_NOTEABITAG),
3866 		    _cache->c_name) != 0) || (_cache->c_data == NULL))
3867 			continue;
3868 
3869 		pnstate.pns_file = file;
3870 		pnstate.pns_cache = _cache;
3871 		pnstate.pns_size = _cache->c_data->d_size;
3872 		pnstate.pns_data = (Word *)_cache->c_data->d_buf;
3873 
3874 		while (pnstate.pns_size > 0) {
3875 			Word *w;
3876 
3877 			if (parse_note_entry(&pnstate) == 0)
3878 				break;
3879 
3880 			/*
3881 			 * The type must be 1, and the name must be "GNU".
3882 			 * The descsz must be at least 16 bytes.
3883 			 */
3884 			if ((pnstate.pn_type != 1) ||
3885 			    (pnstate.pn_namesz != (MSG_STR_GNU_SIZE + 1)) ||
3886 			    (strncmp(MSG_ORIG(MSG_STR_GNU), pnstate.pn_name,
3887 			    MSG_STR_CORE_SIZE + 1) != 0) ||
3888 			    (pnstate.pn_descsz < 16))
3889 				continue;
3890 
3891 			/*
3892 			 * desc contains 4 32-bit fields. Field 0 must be 0,
3893 			 * indicating Linux. The second, third, and fourth
3894 			 * fields represent the earliest Linux kernel
3895 			 * version compatible with this object.
3896 			 */
3897 			/*LINTED*/
3898 			w = (Word *) pnstate.pn_desc;
3899 			if (*w == 0)
3900 				return (1);
3901 		}
3902 	}
3903 
3904 	return (0);
3905 }
3906 
3907 /*
3908  * Determine an individual hash entry.  This may be the initial hash entry,
3909  * or an associated chain entry.
3910  */
3911 static void
3912 hash_entry(Cache *refsec, Cache *strsec, const char *hsecname, Word hashndx,
3913     Word symndx, Word symn, Sym *syms, const char *file, ulong_t bkts,
3914     uint_t flags, int chain)
3915 {
3916 	Sym		*sym;
3917 	const char	*symname, *str;
3918 	char		_bucket[MAXNDXSIZE], _symndx[MAXNDXSIZE];
3919 	ulong_t		nbkt, nhash;
3920 
3921 	if (symndx > symn) {
3922 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_HSBADSYMNDX), file,
3923 		    EC_WORD(symndx), EC_WORD(hashndx));
3924 		symname = MSG_INTL(MSG_STR_UNKNOWN);
3925 	} else {
3926 		sym = (Sym *)(syms + symndx);
3927 		symname = string(refsec, symndx, strsec, file, sym->st_name);
3928 	}
3929 
3930 	if (chain == 0) {
3931 		(void) snprintf(_bucket, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INTEGER),
3932 		    hashndx);
3933 		str = (const char *)_bucket;
3934 	} else
3935 		str = MSG_ORIG(MSG_STR_EMPTY);
3936 
3937 	(void) snprintf(_symndx, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INDEX2),
3938 	    EC_WORD(symndx));
3939 	dbg_print(0, MSG_ORIG(MSG_FMT_HASH_INFO), str, _symndx,
3940 	    demangle(symname, flags));
3941 
3942 	/*
3943 	 * Determine if this string is in the correct bucket.
3944 	 */
3945 	nhash = elf_hash(symname);
3946 	nbkt = nhash % bkts;
3947 
3948 	if (nbkt != hashndx) {
3949 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADHASH), file,
3950 		    hsecname, symname, EC_WORD(hashndx), nbkt);
3951 	}
3952 }
3953 
3954 #define	MAXCOUNT	500
3955 
3956 static void
3957 hash(Cache *cache, Word shnum, const char *file, uint_t flags)
3958 {
3959 	static int	count[MAXCOUNT];
3960 	Word		cnt;
3961 	ulong_t		ndx, bkts;
3962 	char		number[MAXNDXSIZE];
3963 
3964 	for (cnt = 1; cnt < shnum; cnt++) {
3965 		uint_t		*hash, *chain;
3966 		Cache		*_cache = &cache[cnt];
3967 		Shdr		*sshdr, *hshdr = _cache->c_shdr;
3968 		char		*ssecname, *hsecname = _cache->c_name;
3969 		Sym		*syms;
3970 		Word		symn;
3971 
3972 		if (hshdr->sh_type != SHT_HASH)
3973 			continue;
3974 
3975 		/*
3976 		 * Determine the hash table data and size.
3977 		 */
3978 		if ((hshdr->sh_entsize == 0) || (hshdr->sh_size == 0)) {
3979 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
3980 			    file, hsecname);
3981 			continue;
3982 		}
3983 		if (_cache->c_data == NULL)
3984 			continue;
3985 
3986 		hash = (uint_t *)_cache->c_data->d_buf;
3987 		bkts = *hash;
3988 		chain = hash + 2 + bkts;
3989 		hash += 2;
3990 
3991 		/*
3992 		 * Get the data buffer for the associated symbol table.
3993 		 */
3994 		if ((hshdr->sh_link == 0) || (hshdr->sh_link >= shnum)) {
3995 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
3996 			    file, hsecname, EC_WORD(hshdr->sh_link));
3997 			continue;
3998 		}
3999 
4000 		_cache = &cache[hshdr->sh_link];
4001 		ssecname = _cache->c_name;
4002 
4003 		if (_cache->c_data == NULL)
4004 			continue;
4005 
4006 		if ((syms = (Sym *)_cache->c_data->d_buf) == NULL) {
4007 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
4008 			    file, ssecname);
4009 			continue;
4010 		}
4011 
4012 		sshdr = _cache->c_shdr;
4013 		/* LINTED */
4014 		symn = (Word)(sshdr->sh_size / sshdr->sh_entsize);
4015 
4016 		/*
4017 		 * Get the associated string table section.
4018 		 */
4019 		if ((sshdr->sh_link == 0) || (sshdr->sh_link >= shnum)) {
4020 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
4021 			    file, ssecname, EC_WORD(sshdr->sh_link));
4022 			continue;
4023 		}
4024 
4025 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
4026 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_HASH), hsecname);
4027 		dbg_print(0, MSG_INTL(MSG_ELF_HASH_INFO));
4028 
4029 		/*
4030 		 * Loop through the hash buckets, printing the appropriate
4031 		 * symbols.
4032 		 */
4033 		for (ndx = 0; ndx < bkts; ndx++, hash++) {
4034 			Word	_ndx, _cnt;
4035 
4036 			if (*hash == 0) {
4037 				count[0]++;
4038 				continue;
4039 			}
4040 
4041 			hash_entry(_cache, &cache[sshdr->sh_link], hsecname,
4042 			    ndx, *hash, symn, syms, file, bkts, flags, 0);
4043 
4044 			/*
4045 			 * Determine if any other symbols are chained to this
4046 			 * bucket.
4047 			 */
4048 			_ndx = chain[*hash];
4049 			_cnt = 1;
4050 			while (_ndx) {
4051 				hash_entry(_cache, &cache[sshdr->sh_link],
4052 				    hsecname, ndx, _ndx, symn, syms, file,
4053 				    bkts, flags, 1);
4054 				_ndx = chain[_ndx];
4055 				_cnt++;
4056 			}
4057 
4058 			if (_cnt >= MAXCOUNT) {
4059 				(void) fprintf(stderr,
4060 				    MSG_INTL(MSG_HASH_OVERFLW), file,
4061 				    _cache->c_name, EC_WORD(ndx),
4062 				    EC_WORD(_cnt));
4063 			} else
4064 				count[_cnt]++;
4065 		}
4066 		break;
4067 	}
4068 
4069 	/*
4070 	 * Print out the count information.
4071 	 */
4072 	bkts = cnt = 0;
4073 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
4074 
4075 	for (ndx = 0; ndx < MAXCOUNT; ndx++) {
4076 		Word	_cnt;
4077 
4078 		if ((_cnt = count[ndx]) == 0)
4079 			continue;
4080 
4081 		(void) snprintf(number, MAXNDXSIZE,
4082 		    MSG_ORIG(MSG_FMT_INTEGER), _cnt);
4083 		dbg_print(0, MSG_INTL(MSG_ELF_HASH_BKTS1), number,
4084 		    EC_WORD(ndx));
4085 		bkts += _cnt;
4086 		cnt += (Word)(ndx * _cnt);
4087 	}
4088 	if (cnt) {
4089 		(void) snprintf(number, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INTEGER),
4090 		    bkts);
4091 		dbg_print(0, MSG_INTL(MSG_ELF_HASH_BKTS2), number,
4092 		    EC_WORD(cnt));
4093 	}
4094 }
4095 
4096 static void
4097 group(Cache *cache, Word shnum, const char *file, uint_t flags)
4098 {
4099 	Word	scnt;
4100 
4101 	for (scnt = 1; scnt < shnum; scnt++) {
4102 		Cache		*_cache = &cache[scnt];
4103 		Shdr		*shdr = _cache->c_shdr;
4104 		Word		*grpdata, gcnt, grpcnt, symnum, unknown;
4105 		Cache		*symsec, *strsec;
4106 		Sym		*syms, *sym;
4107 		char		flgstrbuf[MSG_GRP_COMDAT_SIZE + 10];
4108 		const char	*grpnam;
4109 
4110 		if (shdr->sh_type != SHT_GROUP)
4111 			continue;
4112 		if (!match(MATCH_F_ALL, _cache->c_name, scnt, shdr->sh_type))
4113 			continue;
4114 		if ((_cache->c_data == NULL) ||
4115 		    ((grpdata = (Word *)_cache->c_data->d_buf) == NULL))
4116 			continue;
4117 		grpcnt = shdr->sh_size / sizeof (Word);
4118 
4119 		/*
4120 		 * Get the data buffer for the associated symbol table and
4121 		 * string table.
4122 		 */
4123 		if (stringtbl(cache, 1, scnt, shnum, file,
4124 		    &symnum, &symsec, &strsec) == 0)
4125 			return;
4126 
4127 		syms = symsec->c_data->d_buf;
4128 
4129 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
4130 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_GRP), _cache->c_name);
4131 		dbg_print(0, MSG_INTL(MSG_GRP_TITLE));
4132 
4133 		/*
4134 		 * The first element of the group defines the group.  The
4135 		 * associated symbol is defined by the sh_link field.
4136 		 */
4137 		if ((shdr->sh_info == SHN_UNDEF) || (shdr->sh_info > symnum)) {
4138 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHINFO),
4139 			    file, _cache->c_name, EC_WORD(shdr->sh_info));
4140 			return;
4141 		}
4142 
4143 		(void) strcpy(flgstrbuf, MSG_ORIG(MSG_STR_OSQBRKT));
4144 		if (grpdata[0] & GRP_COMDAT) {
4145 			(void) strcat(flgstrbuf, MSG_ORIG(MSG_GRP_COMDAT));
4146 		}
4147 		if ((unknown = (grpdata[0] & ~GRP_COMDAT)) != 0) {
4148 			size_t	len = strlen(flgstrbuf);
4149 
4150 			(void) snprintf(&flgstrbuf[len],
4151 			    (MSG_GRP_COMDAT_SIZE + 10 - len),
4152 			    MSG_ORIG(MSG_GRP_UNKNOWN), unknown);
4153 		}
4154 		(void) strcat(flgstrbuf, MSG_ORIG(MSG_STR_CSQBRKT));
4155 		sym = (Sym *)(syms + shdr->sh_info);
4156 
4157 		/*
4158 		 * The GNU assembler can use section symbols as the signature
4159 		 * symbol as described by this comment in the gold linker
4160 		 * (found via google):
4161 		 *
4162 		 *	It seems that some versions of gas will create a
4163 		 *	section group associated with a section symbol, and
4164 		 *	then fail to give a name to the section symbol.  In
4165 		 *	such a case, use the name of the section.
4166 		 *
4167 		 * In order to support such objects, we do the same.
4168 		 */
4169 		grpnam = string(_cache, 0, strsec, file, sym->st_name);
4170 		if (((sym->st_name == 0) || (*grpnam == '\0')) &&
4171 		    (ELF_ST_TYPE(sym->st_info) == STT_SECTION))
4172 			grpnam = cache[sym->st_shndx].c_name;
4173 
4174 		dbg_print(0, MSG_INTL(MSG_GRP_SIGNATURE), flgstrbuf,
4175 		    demangle(grpnam, flags));
4176 
4177 		for (gcnt = 1; gcnt < grpcnt; gcnt++) {
4178 			char		index[MAXNDXSIZE];
4179 			const char	*name;
4180 
4181 			(void) snprintf(index, MAXNDXSIZE,
4182 			    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(gcnt));
4183 
4184 			if (grpdata[gcnt] >= shnum)
4185 				name = MSG_INTL(MSG_GRP_INVALSCN);
4186 			else
4187 				name = cache[grpdata[gcnt]].c_name;
4188 
4189 			(void) printf(MSG_ORIG(MSG_GRP_ENTRY), index, name,
4190 			    EC_XWORD(grpdata[gcnt]));
4191 		}
4192 	}
4193 }
4194 
4195 static void
4196 got(Cache *cache, Word shnum, Ehdr *ehdr, const char *file)
4197 {
4198 	Cache		*gotcache = NULL, *symtab = NULL;
4199 	Addr		gotbgn, gotend;
4200 	Shdr		*gotshdr;
4201 	Word		cnt, gotents, gotndx;
4202 	size_t		gentsize;
4203 	Got_info	*gottable;
4204 	char		*gotdata;
4205 	Sym		*gotsym;
4206 	Xword		gotsymaddr;
4207 	uint_t		sys_encoding;
4208 
4209 	/*
4210 	 * First, find the got.
4211 	 */
4212 	for (cnt = 1; cnt < shnum; cnt++) {
4213 		if (strncmp(cache[cnt].c_name, MSG_ORIG(MSG_ELF_GOT),
4214 		    MSG_ELF_GOT_SIZE) == 0) {
4215 			gotcache = &cache[cnt];
4216 			break;
4217 		}
4218 	}
4219 	if (gotcache == NULL)
4220 		return;
4221 
4222 	/*
4223 	 * A got section within a relocatable object is suspicious.
4224 	 */
4225 	if (ehdr->e_type == ET_REL) {
4226 		(void) fprintf(stderr, MSG_INTL(MSG_GOT_UNEXPECTED), file,
4227 		    gotcache->c_name);
4228 	}
4229 
4230 	gotshdr = gotcache->c_shdr;
4231 	if (gotshdr->sh_size == 0) {
4232 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
4233 		    file, gotcache->c_name);
4234 		return;
4235 	}
4236 
4237 	gotbgn = gotshdr->sh_addr;
4238 	gotend = gotbgn + gotshdr->sh_size;
4239 
4240 	/*
4241 	 * Some architectures don't properly set the sh_entsize for the GOT
4242 	 * table.  If it's not set, default to a size of a pointer.
4243 	 */
4244 	if ((gentsize = gotshdr->sh_entsize) == 0)
4245 		gentsize = sizeof (Xword);
4246 
4247 	if (gotcache->c_data == NULL)
4248 		return;
4249 
4250 	/* LINTED */
4251 	gotents = (Word)(gotshdr->sh_size / gentsize);
4252 	gotdata = gotcache->c_data->d_buf;
4253 
4254 	if ((gottable = calloc(gotents, sizeof (Got_info))) == 0) {
4255 		int err = errno;
4256 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC), file,
4257 		    strerror(err));
4258 		return;
4259 	}
4260 
4261 	/*
4262 	 * Now we scan through all the sections looking for any relocations
4263 	 * that may be against the GOT.  Since these may not be isolated to a
4264 	 * .rel[a].got section we check them all.
4265 	 * While scanning sections save the symbol table entry (a symtab
4266 	 * overriding a dynsym) so that we can lookup _GLOBAL_OFFSET_TABLE_.
4267 	 */
4268 	for (cnt = 1; cnt < shnum; cnt++) {
4269 		Word		type, symnum;
4270 		Xword		relndx, relnum, relsize;
4271 		void		*rels;
4272 		Sym		*syms;
4273 		Cache		*symsec, *strsec;
4274 		Cache		*_cache = &cache[cnt];
4275 		Shdr		*shdr;
4276 
4277 		shdr = _cache->c_shdr;
4278 		type = shdr->sh_type;
4279 
4280 		if ((symtab == 0) && (type == SHT_DYNSYM)) {
4281 			symtab = _cache;
4282 			continue;
4283 		}
4284 		if (type == SHT_SYMTAB) {
4285 			symtab = _cache;
4286 			continue;
4287 		}
4288 		if ((type != SHT_RELA) && (type != SHT_REL))
4289 			continue;
4290 
4291 		/*
4292 		 * Decide entry size.
4293 		 */
4294 		if (((relsize = shdr->sh_entsize) == 0) ||
4295 		    (relsize > shdr->sh_size)) {
4296 			if (type == SHT_RELA)
4297 				relsize = sizeof (Rela);
4298 			else
4299 				relsize = sizeof (Rel);
4300 		}
4301 
4302 		/*
4303 		 * Determine the number of relocations available.
4304 		 */
4305 		if (shdr->sh_size == 0) {
4306 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
4307 			    file, _cache->c_name);
4308 			continue;
4309 		}
4310 		if (_cache->c_data == NULL)
4311 			continue;
4312 
4313 		rels = _cache->c_data->d_buf;
4314 		relnum = shdr->sh_size / relsize;
4315 
4316 		/*
4317 		 * Get the data buffer for the associated symbol table and
4318 		 * string table.
4319 		 */
4320 		if (stringtbl(cache, 1, cnt, shnum, file,
4321 		    &symnum, &symsec, &strsec) == 0)
4322 			continue;
4323 
4324 		syms = symsec->c_data->d_buf;
4325 
4326 		/*
4327 		 * Loop through the relocation entries.
4328 		 */
4329 		for (relndx = 0; relndx < relnum; relndx++,
4330 		    rels = (void *)((char *)rels + relsize)) {
4331 			char		section[BUFSIZ];
4332 			Addr		offset;
4333 			Got_info	*gip;
4334 			Word		symndx, reltype;
4335 			Rela		*rela;
4336 			Rel		*rel;
4337 
4338 			/*
4339 			 * Unravel the relocation.
4340 			 */
4341 			if (type == SHT_RELA) {
4342 				rela = (Rela *)rels;
4343 				symndx = ELF_R_SYM(rela->r_info);
4344 				reltype = ELF_R_TYPE(rela->r_info,
4345 				    ehdr->e_machine);
4346 				offset = rela->r_offset;
4347 			} else {
4348 				rel = (Rel *)rels;
4349 				symndx = ELF_R_SYM(rel->r_info);
4350 				reltype = ELF_R_TYPE(rel->r_info,
4351 				    ehdr->e_machine);
4352 				offset = rel->r_offset;
4353 			}
4354 
4355 			/*
4356 			 * Only pay attention to relocations against the GOT.
4357 			 */
4358 			if ((offset < gotbgn) || (offset >= gotend))
4359 				continue;
4360 
4361 			/* LINTED */
4362 			gotndx = (Word)((offset - gotbgn) /
4363 			    gotshdr->sh_entsize);
4364 			gip = &gottable[gotndx];
4365 
4366 			if (gip->g_reltype != 0) {
4367 				(void) fprintf(stderr,
4368 				    MSG_INTL(MSG_GOT_MULTIPLE), file,
4369 				    EC_WORD(gotndx), EC_ADDR(offset));
4370 				continue;
4371 			}
4372 
4373 			if (symndx)
4374 				gip->g_symname = relsymname(cache, _cache,
4375 				    strsec, symndx, symnum, relndx, syms,
4376 				    section, BUFSIZ, file);
4377 			gip->g_reltype = reltype;
4378 			gip->g_rel = rels;
4379 		}
4380 	}
4381 
4382 	if (symlookup(MSG_ORIG(MSG_SYM_GOT), cache, shnum, &gotsym, NULL,
4383 	    symtab, file))
4384 		gotsymaddr = gotsym->st_value;
4385 	else
4386 		gotsymaddr = gotbgn;
4387 
4388 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
4389 	dbg_print(0, MSG_INTL(MSG_ELF_SCN_GOT), gotcache->c_name);
4390 	Elf_got_title(0);
4391 
4392 	sys_encoding = _elf_sys_encoding();
4393 	for (gotndx = 0; gotndx < gotents; gotndx++) {
4394 		Got_info	*gip;
4395 		Sword		gindex;
4396 		Addr		gaddr;
4397 		Xword		gotentry;
4398 
4399 		gip = &gottable[gotndx];
4400 
4401 		gaddr = gotbgn + (gotndx * gentsize);
4402 		gindex = (Sword)(gaddr - gotsymaddr) / (Sword)gentsize;
4403 
4404 		if (gentsize == sizeof (Word))
4405 			/* LINTED */
4406 			gotentry = (Xword)(*((Word *)(gotdata) + gotndx));
4407 		else
4408 			/* LINTED */
4409 			gotentry = *((Xword *)(gotdata) + gotndx);
4410 
4411 		Elf_got_entry(0, gindex, gaddr, gotentry, ehdr->e_machine,
4412 		    ehdr->e_ident[EI_DATA], sys_encoding,
4413 		    gip->g_reltype, gip->g_rel, gip->g_symname);
4414 	}
4415 	free(gottable);
4416 }
4417 
4418 void
4419 checksum(Elf *elf)
4420 {
4421 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
4422 	dbg_print(0, MSG_INTL(MSG_STR_CHECKSUM), elf_checksum(elf));
4423 }
4424 
4425 /*
4426  * This variable is used by regular() to communicate the address of
4427  * the section header cache to sort_shdr_ndx_arr(). Unfortunately,
4428  * the qsort() interface does not include a userdata argument by which
4429  * such arbitrary data can be passed, so we are stuck using global data.
4430  */
4431 static Cache *sort_shdr_ndx_arr_cache;
4432 
4433 
4434 /*
4435  * Used with qsort() to sort the section indices so that they can be
4436  * used to access the section headers in order of increasing data offset.
4437  *
4438  * entry:
4439  *	sort_shdr_ndx_arr_cache - Contains address of
4440  *		section header cache.
4441  *	v1, v2 - Point at elements of sort_shdr_bits array to be compared.
4442  *
4443  * exit:
4444  *	Returns -1 (less than), 0 (equal) or 1 (greater than).
4445  */
4446 static int
4447 sort_shdr_ndx_arr(const void *v1, const void *v2)
4448 {
4449 	Cache	*cache1 = sort_shdr_ndx_arr_cache + *((size_t *)v1);
4450 	Cache	*cache2 = sort_shdr_ndx_arr_cache + *((size_t *)v2);
4451 
4452 	if (cache1->c_shdr->sh_offset < cache2->c_shdr->sh_offset)
4453 		return (-1);
4454 
4455 	if (cache1->c_shdr->sh_offset > cache2->c_shdr->sh_offset)
4456 		return (1);
4457 
4458 	return (0);
4459 }
4460 
4461 
4462 static int
4463 shdr_cache(const char *file, Elf *elf, Ehdr *ehdr, size_t shstrndx,
4464     size_t shnum, Cache **cache_ret, Word flags)
4465 {
4466 	Elf_Scn		*scn;
4467 	Elf_Data	*data;
4468 	size_t		ndx;
4469 	Shdr		*nameshdr;
4470 	char		*names = NULL;
4471 	Cache		*cache, *_cache;
4472 	size_t		*shdr_ndx_arr, shdr_ndx_arr_cnt;
4473 
4474 
4475 	/*
4476 	 * Obtain the .shstrtab data buffer to provide the required section
4477 	 * name strings.
4478 	 */
4479 	if (shstrndx == SHN_UNDEF) {
4480 		/*
4481 		 * It is rare, but legal, for an object to lack a
4482 		 * header string table section.
4483 		 */
4484 		names = NULL;
4485 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_NOSHSTRSEC), file);
4486 	} else if ((scn = elf_getscn(elf, shstrndx)) == NULL) {
4487 		failure(file, MSG_ORIG(MSG_ELF_GETSCN));
4488 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SHDR),
4489 		    EC_XWORD(shstrndx));
4490 
4491 	} else if ((data = elf_getdata(scn, NULL)) == NULL) {
4492 		failure(file, MSG_ORIG(MSG_ELF_GETDATA));
4493 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_DATA),
4494 		    EC_XWORD(shstrndx));
4495 
4496 	} else if ((nameshdr = elf_getshdr(scn)) == NULL) {
4497 		failure(file, MSG_ORIG(MSG_ELF_GETSHDR));
4498 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN),
4499 		    EC_WORD(elf_ndxscn(scn)));
4500 
4501 	} else if ((names = data->d_buf) == NULL)
4502 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_SHSTRNULL), file);
4503 
4504 	/*
4505 	 * Allocate a cache to maintain a descriptor for each section.
4506 	 */
4507 	if ((*cache_ret = cache = malloc(shnum * sizeof (Cache))) == NULL) {
4508 		int err = errno;
4509 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
4510 		    file, strerror(err));
4511 		return (0);
4512 	}
4513 
4514 	*cache = cache_init;
4515 	_cache = cache;
4516 	_cache++;
4517 
4518 	/*
4519 	 * Allocate an array that will hold the section index for
4520 	 * each section that has data in the ELF file:
4521 	 *
4522 	 *	- Is not a NOBITS section
4523 	 *	- Data has non-zero length
4524 	 *
4525 	 * Note that shnum is an upper bound on the size required. It
4526 	 * is likely that we won't use a few of these array elements.
4527 	 * Allocating a modest amount of extra memory in this case means
4528 	 * that we can avoid an extra loop to count the number of needed
4529 	 * items, and can fill this array immediately in the first loop
4530 	 * below.
4531 	 */
4532 	if ((shdr_ndx_arr = malloc(shnum * sizeof (*shdr_ndx_arr))) == NULL) {
4533 		int err = errno;
4534 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
4535 		    file, strerror(err));
4536 		return (0);
4537 	}
4538 	shdr_ndx_arr_cnt = 0;
4539 
4540 	/*
4541 	 * Traverse the sections of the file.  This gathering of data is
4542 	 * carried out in two passes.  First, the section headers are captured
4543 	 * and the section header names are evaluated.  A verification pass is
4544 	 * then carried out over the section information.  Files have been
4545 	 * known to exhibit overlapping (and hence erroneous) section header
4546 	 * information.
4547 	 *
4548 	 * Finally, the data for each section is obtained.  This processing is
4549 	 * carried out after section verification because should any section
4550 	 * header overlap occur, and a file needs translating (ie. xlate'ing
4551 	 * information from a non-native architecture file), then the process
4552 	 * of translation can corrupt the section header information.  Of
4553 	 * course, if there is any section overlap, the data related to the
4554 	 * sections is going to be compromised.  However, it is the translation
4555 	 * of this data that has caused problems with elfdump()'s ability to
4556 	 * extract the data.
4557 	 */
4558 	for (ndx = 1, scn = NULL; scn = elf_nextscn(elf, scn);
4559 	    ndx++, _cache++) {
4560 		char	scnndxnm[100];
4561 
4562 		_cache->c_ndx = ndx;
4563 		_cache->c_scn = scn;
4564 
4565 		if ((_cache->c_shdr = elf_getshdr(scn)) == NULL) {
4566 			failure(file, MSG_ORIG(MSG_ELF_GETSHDR));
4567 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN),
4568 			    EC_WORD(elf_ndxscn(scn)));
4569 		}
4570 
4571 		/*
4572 		 * If this section has data in the file, include it in
4573 		 * the array of sections to check for address overlap.
4574 		 */
4575 		if ((_cache->c_shdr->sh_size != 0) &&
4576 		    (_cache->c_shdr->sh_type != SHT_NOBITS))
4577 			shdr_ndx_arr[shdr_ndx_arr_cnt++] = ndx;
4578 
4579 		/*
4580 		 * If a shstrtab exists, assign the section name.
4581 		 */
4582 		if (names && _cache->c_shdr) {
4583 			if (_cache->c_shdr->sh_name &&
4584 			    /* LINTED */
4585 			    (nameshdr->sh_size > _cache->c_shdr->sh_name)) {
4586 				const char	*symname;
4587 				char		*secname;
4588 
4589 				secname = names + _cache->c_shdr->sh_name;
4590 
4591 				/*
4592 				 * A SUN naming convention employs a "%" within
4593 				 * a section name to indicate a section/symbol
4594 				 * name.  This originated from the compilers
4595 				 * -xF option, that places functions into their
4596 				 * own sections.  This convention (which has no
4597 				 * formal standard) has also been followed for
4598 				 * COMDAT sections.  To demangle the symbol
4599 				 * name, the name must be separated from the
4600 				 * section name.
4601 				 */
4602 				if (((flags & FLG_CTL_DEMANGLE) == 0) ||
4603 				    ((symname = strchr(secname, '%')) == NULL))
4604 					_cache->c_name = secname;
4605 				else {
4606 					size_t	secsz = ++symname - secname;
4607 					size_t	strsz;
4608 
4609 					symname = demangle(symname, flags);
4610 					strsz = secsz + strlen(symname) + 1;
4611 
4612 					if ((_cache->c_name =
4613 					    malloc(strsz)) == NULL) {
4614 						int err = errno;
4615 						(void) fprintf(stderr,
4616 						    MSG_INTL(MSG_ERR_MALLOC),
4617 						    file, strerror(err));
4618 						return (0);
4619 					}
4620 					(void) snprintf(_cache->c_name, strsz,
4621 					    MSG_ORIG(MSG_FMT_SECSYM),
4622 					    EC_WORD(secsz), secname, symname);
4623 				}
4624 
4625 				continue;
4626 			}
4627 
4628 			/*
4629 			 * Generate an error if the section name index is zero
4630 			 * or exceeds the shstrtab data.  Fall through to
4631 			 * fabricate a section name.
4632 			 */
4633 			if ((_cache->c_shdr->sh_name == 0) ||
4634 			    /* LINTED */
4635 			    (nameshdr->sh_size <= _cache->c_shdr->sh_name)) {
4636 				(void) fprintf(stderr,
4637 				    MSG_INTL(MSG_ERR_BADSHNAME), file,
4638 				    EC_WORD(ndx),
4639 				    EC_XWORD(_cache->c_shdr->sh_name));
4640 			}
4641 		}
4642 
4643 		/*
4644 		 * If there exists no shstrtab data, or a section header has no
4645 		 * name (an invalid index of 0), then compose a name for the
4646 		 * section.
4647 		 */
4648 		(void) snprintf(scnndxnm, sizeof (scnndxnm),
4649 		    MSG_INTL(MSG_FMT_SCNNDX), ndx);
4650 
4651 		if ((_cache->c_name = malloc(strlen(scnndxnm) + 1)) == NULL) {
4652 			int err = errno;
4653 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
4654 			    file, strerror(err));
4655 			return (0);
4656 		}
4657 		(void) strcpy(_cache->c_name, scnndxnm);
4658 	}
4659 
4660 	/*
4661 	 * Having collected all the sections, validate their address range.
4662 	 * Cases have existed where the section information has been invalid.
4663 	 * This can lead to all sorts of other, hard to diagnose errors, as
4664 	 * each section is processed individually (ie. with elf_getdata()).
4665 	 * Here, we carry out some address comparisons to catch a family of
4666 	 * overlapping memory issues we have observed (likely, there are others
4667 	 * that we have yet to discover).
4668 	 *
4669 	 * Note, should any memory overlap occur, obtaining any additional
4670 	 * data from the file is questionable.  However, it might still be
4671 	 * possible to inspect the ELF header, Programs headers, or individual
4672 	 * sections, so rather than bailing on an error condition, continue
4673 	 * processing to see if any data can be salvaged.
4674 	 */
4675 	if (shdr_ndx_arr_cnt > 1) {
4676 		sort_shdr_ndx_arr_cache = cache;
4677 		qsort(shdr_ndx_arr, shdr_ndx_arr_cnt,
4678 		    sizeof (*shdr_ndx_arr), sort_shdr_ndx_arr);
4679 	}
4680 	for (ndx = 0; ndx < shdr_ndx_arr_cnt; ndx++) {
4681 		Cache	*_cache = cache + shdr_ndx_arr[ndx];
4682 		Shdr	*shdr = _cache->c_shdr;
4683 		Off	bgn1, bgn = shdr->sh_offset;
4684 		Off	end1, end = shdr->sh_offset + shdr->sh_size;
4685 		size_t	ndx1;
4686 
4687 		/*
4688 		 * Check the section against all following ones, reporting
4689 		 * any overlaps. Since we've sorted the sections by offset,
4690 		 * we can stop after the first comparison that fails. There
4691 		 * are no overlaps in a properly formed ELF file, in which
4692 		 * case this algorithm runs in O(n) time. This will degenerate
4693 		 * to O(n^2) for a completely broken file. Such a file is
4694 		 * (1) highly unlikely, and (2) unusable, so it is reasonable
4695 		 * for the analysis to take longer.
4696 		 */
4697 		for (ndx1 = ndx + 1; ndx1 < shdr_ndx_arr_cnt; ndx1++) {
4698 			Cache	*_cache1 = cache + shdr_ndx_arr[ndx1];
4699 			Shdr	*shdr1 = _cache1->c_shdr;
4700 
4701 			bgn1 = shdr1->sh_offset;
4702 			end1 = shdr1->sh_offset + shdr1->sh_size;
4703 
4704 			if (((bgn1 <= bgn) && (end1 > bgn)) ||
4705 			    ((bgn1 < end) && (end1 >= end))) {
4706 				(void) fprintf(stderr,
4707 				    MSG_INTL(MSG_ERR_SECMEMOVER), file,
4708 				    EC_WORD(elf_ndxscn(_cache->c_scn)),
4709 				    _cache->c_name, EC_OFF(bgn), EC_OFF(end),
4710 				    EC_WORD(elf_ndxscn(_cache1->c_scn)),
4711 				    _cache1->c_name, EC_OFF(bgn1),
4712 				    EC_OFF(end1));
4713 			} else {	/* No overlap, so can stop */
4714 				break;
4715 			}
4716 		}
4717 
4718 		/*
4719 		 * In addition to checking for sections overlapping
4720 		 * each other (done above), we should also make sure
4721 		 * the section doesn't overlap the section header array.
4722 		 */
4723 		bgn1 = ehdr->e_shoff;
4724 		end1 = ehdr->e_shoff + (ehdr->e_shentsize * ehdr->e_shnum);
4725 
4726 		if (((bgn1 <= bgn) && (end1 > bgn)) ||
4727 		    ((bgn1 < end) && (end1 >= end))) {
4728 			(void) fprintf(stderr,
4729 			    MSG_INTL(MSG_ERR_SHDRMEMOVER), file, EC_OFF(bgn1),
4730 			    EC_OFF(end1),
4731 			    EC_WORD(elf_ndxscn(_cache->c_scn)),
4732 			    _cache->c_name, EC_OFF(bgn), EC_OFF(end));
4733 		}
4734 	}
4735 
4736 	/*
4737 	 * Obtain the data for each section.
4738 	 */
4739 	for (ndx = 1; ndx < shnum; ndx++) {
4740 		Cache	*_cache = &cache[ndx];
4741 		Elf_Scn	*scn = _cache->c_scn;
4742 
4743 		if ((_cache->c_data = elf_getdata(scn, NULL)) == NULL) {
4744 			failure(file, MSG_ORIG(MSG_ELF_GETDATA));
4745 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCNDATA),
4746 			    EC_WORD(elf_ndxscn(scn)));
4747 		}
4748 
4749 		/*
4750 		 * If a string table, verify that it has NULL first and
4751 		 * final bytes.
4752 		 */
4753 		if ((_cache->c_shdr->sh_type == SHT_STRTAB) &&
4754 		    (_cache->c_data != NULL) &&
4755 		    (_cache->c_data->d_buf != NULL) &&
4756 		    (_cache->c_data->d_size > 0)) {
4757 			const char *s = _cache->c_data->d_buf;
4758 
4759 			if ((*s != '\0') ||
4760 			    (*(s + _cache->c_data->d_size - 1) != '\0'))
4761 				(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALSTR),
4762 				    file, _cache->c_name);
4763 		}
4764 	}
4765 
4766 	return (1);
4767 }
4768 
4769 
4770 
4771 /*
4772  * Generate a cache of section headers and related information
4773  * for use by the rest of elfdump. If requested (or the file
4774  * contains no section headers), we generate a fake set of
4775  * headers from the information accessible from the program headers.
4776  * Otherwise, we use the real section headers contained in the file.
4777  */
4778 static int
4779 create_cache(const char *file, int fd, Elf *elf, Ehdr *ehdr, Cache **cache,
4780     size_t shstrndx, size_t *shnum, uint_t *flags)
4781 {
4782 	/*
4783 	 * If there are no section headers, then resort to synthesizing
4784 	 * section headers from the program headers. This is normally
4785 	 * only done by explicit request, but in this case there's no
4786 	 * reason not to go ahead, since the alternative is simply to quit.
4787 	 */
4788 	if ((*shnum <= 1) && ((*flags & FLG_CTL_FAKESHDR) == 0)) {
4789 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_NOSHDR), file);
4790 		*flags |= FLG_CTL_FAKESHDR;
4791 	}
4792 
4793 	if (*flags & FLG_CTL_FAKESHDR) {
4794 		if (fake_shdr_cache(file, fd, elf, ehdr, cache, shnum) == 0)
4795 			return (0);
4796 	} else {
4797 		if (shdr_cache(file, elf, ehdr, shstrndx, *shnum,
4798 		    cache, *flags) == 0)
4799 			return (0);
4800 	}
4801 
4802 	return (1);
4803 }
4804 
4805 int
4806 regular(const char *file, int fd, Elf *elf, uint_t flags,
4807     const char *wname, int wfd, uchar_t osabi)
4808 {
4809 	enum { CACHE_NEEDED, CACHE_OK, CACHE_FAIL} cache_state = CACHE_NEEDED;
4810 	Elf_Scn		*scn;
4811 	Ehdr		*ehdr;
4812 	size_t		ndx, shstrndx, shnum, phnum;
4813 	Shdr		*shdr;
4814 	Cache		*cache;
4815 	VERSYM_STATE	versym = { 0 };
4816 	int		ret = 0;
4817 	int		addr_align;
4818 
4819 	if ((ehdr = elf_getehdr(elf)) == NULL) {
4820 		failure(file, MSG_ORIG(MSG_ELF_GETEHDR));
4821 		return (ret);
4822 	}
4823 
4824 	if (elf_getshdrnum(elf, &shnum) == -1) {
4825 		failure(file, MSG_ORIG(MSG_ELF_GETSHDRNUM));
4826 		return (ret);
4827 	}
4828 
4829 	if (elf_getshdrstrndx(elf, &shstrndx) == -1) {
4830 		failure(file, MSG_ORIG(MSG_ELF_GETSHDRSTRNDX));
4831 		return (ret);
4832 	}
4833 
4834 	if (elf_getphdrnum(elf, &phnum) == -1) {
4835 		failure(file, MSG_ORIG(MSG_ELF_GETPHDRNUM));
4836 		return (ret);
4837 	}
4838 	/*
4839 	 * If the user requested section headers derived from the
4840 	 * program headers (-P option) and this file doesn't have
4841 	 * any program headers (i.e. ET_REL), then we can't do it.
4842 	 */
4843 	if ((phnum == 0) && (flags & FLG_CTL_FAKESHDR)) {
4844 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_PNEEDSPH), file);
4845 		return (ret);
4846 	}
4847 
4848 
4849 	if ((scn = elf_getscn(elf, 0)) != NULL) {
4850 		if ((shdr = elf_getshdr(scn)) == NULL) {
4851 			failure(file, MSG_ORIG(MSG_ELF_GETSHDR));
4852 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN), 0);
4853 			return (ret);
4854 		}
4855 	} else
4856 		shdr = NULL;
4857 
4858 	/*
4859 	 * Print the elf header.
4860 	 */
4861 	if (flags & FLG_SHOW_EHDR)
4862 		Elf_ehdr(0, ehdr, shdr);
4863 
4864 	/*
4865 	 * If the section headers or program headers have inadequate
4866 	 * alignment for the class of object, print a warning. libelf
4867 	 * can handle such files, but programs that use them can crash
4868 	 * when they dereference unaligned items.
4869 	 *
4870 	 * Note that the AMD64 ABI, although it is a 64-bit architecture,
4871 	 * allows access to data types smaller than 128-bits to be on
4872 	 * word alignment.
4873 	 */
4874 	if (ehdr->e_machine == EM_AMD64)
4875 		addr_align = sizeof (Word);
4876 	else
4877 		addr_align = sizeof (Addr);
4878 
4879 	if (ehdr->e_phoff & (addr_align - 1))
4880 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADPHDRALIGN), file);
4881 	if (ehdr->e_shoff & (addr_align - 1))
4882 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHDRALIGN), file);
4883 
4884 
4885 	/*
4886 	 * Determine the Operating System ABI (osabi) we will use to
4887 	 * interpret the object.
4888 	 */
4889 	if (flags & FLG_CTL_OSABI) {
4890 		/*
4891 		 * If the user explicitly specifies '-O none', we need
4892 		 * to display a completely generic view of the file.
4893 		 * However, libconv is written to assume that ELFOSABI_NONE
4894 		 * is equivalent to ELFOSABI_SOLARIS. To get the desired
4895 		 * effect, we use an osabi that libconv has no knowledge of.
4896 		 */
4897 		if (osabi == ELFOSABI_NONE)
4898 			osabi = ELFOSABI_UNKNOWN4;
4899 	} else {
4900 		/* Determine osabi from file */
4901 		osabi = ehdr->e_ident[EI_OSABI];
4902 		if (osabi == ELFOSABI_NONE) {
4903 			/*
4904 			 * Chicken/Egg scenario:
4905 			 *
4906 			 * Ideally, we wait to create the section header cache
4907 			 * until after the program headers are printed. If we
4908 			 * only output program headers, we can skip building
4909 			 * the cache entirely.
4910 			 *
4911 			 * Proper interpretation of program headers requires
4912 			 * the osabi, which is supposed to be in the ELF header.
4913 			 * However, many systems (Solaris and Linux included)
4914 			 * have a history of setting the osabi to the generic
4915 			 * SysV ABI (ELFOSABI_NONE). We assume ELFOSABI_SOLARIS
4916 			 * in such cases, but would like to check the object
4917 			 * to see if it has a Linux .note.ABI-tag section,
4918 			 * which implies ELFOSABI_LINUX. This requires a
4919 			 * section header cache.
4920 			 *
4921 			 * To break the cycle, we create section headers now
4922 			 * if osabi is ELFOSABI_NONE, and later otherwise.
4923 			 * If it succeeds, we use them, if not, we defer
4924 			 * exiting until after the program headers are out.
4925 			 */
4926 			if (create_cache(file, fd, elf, ehdr, &cache,
4927 			    shstrndx, &shnum, &flags) == 0) {
4928 				cache_state = CACHE_FAIL;
4929 			} else {
4930 				cache_state = CACHE_OK;
4931 				if (has_linux_abi_note(cache, shnum, file)) {
4932 					Conv_inv_buf_t	ibuf1, ibuf2;
4933 
4934 					(void) fprintf(stderr,
4935 					    MSG_INTL(MSG_INFO_LINUXOSABI), file,
4936 					    conv_ehdr_osabi(osabi, 0, &ibuf1),
4937 					    conv_ehdr_osabi(ELFOSABI_LINUX,
4938 					    0, &ibuf2));
4939 					osabi = ELFOSABI_LINUX;
4940 				}
4941 			}
4942 		}
4943 		/*
4944 		 * We treat ELFOSABI_NONE identically to ELFOSABI_SOLARIS.
4945 		 * Mapping NONE to SOLARIS simplifies the required test.
4946 		 */
4947 		if (osabi == ELFOSABI_NONE)
4948 			osabi = ELFOSABI_SOLARIS;
4949 	}
4950 
4951 	/*
4952 	 * Print the program headers.
4953 	 */
4954 	if ((flags & FLG_SHOW_PHDR) && (phnum != 0)) {
4955 		Phdr	*phdr;
4956 
4957 		if ((phdr = elf_getphdr(elf)) == NULL) {
4958 			failure(file, MSG_ORIG(MSG_ELF_GETPHDR));
4959 			return (ret);
4960 		}
4961 
4962 		for (ndx = 0; ndx < phnum; phdr++, ndx++) {
4963 			if (!match(MATCH_F_PHDR| MATCH_F_NDX | MATCH_F_TYPE,
4964 			    NULL, ndx, phdr->p_type))
4965 				continue;
4966 
4967 			dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
4968 			dbg_print(0, MSG_INTL(MSG_ELF_PHDR), EC_WORD(ndx));
4969 			Elf_phdr(0, osabi, ehdr->e_machine, phdr);
4970 		}
4971 	}
4972 
4973 	/*
4974 	 * If we have flag bits set that explicitly require a show or calc
4975 	 * operation, but none of them require the section headers, then
4976 	 * we are done and can return now.
4977 	 */
4978 	if (((flags & (FLG_MASK_SHOW | FLG_MASK_CALC)) != 0) &&
4979 	    ((flags & (FLG_MASK_SHOW_SHDR | FLG_MASK_CALC_SHDR)) == 0))
4980 		return (ret);
4981 
4982 	/*
4983 	 * Everything from this point on requires section headers.
4984 	 * If we have no section headers, there is no reason to continue.
4985 	 *
4986 	 * If we tried above to create the section header cache and failed,
4987 	 * it is time to exit. Otherwise, create it if needed.
4988 	 */
4989 	switch (cache_state) {
4990 	case CACHE_NEEDED:
4991 		if (create_cache(file, fd, elf, ehdr, &cache, shstrndx,
4992 		    &shnum, &flags) == 0)
4993 			return (ret);
4994 		break;
4995 	case CACHE_OK:
4996 		break;
4997 	case CACHE_FAIL:
4998 		return (ret);
4999 	}
5000 	if (shnum <= 1)
5001 		goto done;
5002 
5003 	/*
5004 	 * If -w was specified, find and write out the section(s) data.
5005 	 */
5006 	if (wfd) {
5007 		for (ndx = 1; ndx < shnum; ndx++) {
5008 			Cache	*_cache = &cache[ndx];
5009 
5010 			if (match(MATCH_F_STRICT | MATCH_F_ALL, _cache->c_name,
5011 			    ndx, _cache->c_shdr->sh_type) &&
5012 			    _cache->c_data && _cache->c_data->d_buf) {
5013 				if (write(wfd, _cache->c_data->d_buf,
5014 				    _cache->c_data->d_size) !=
5015 				    _cache->c_data->d_size) {
5016 					int err = errno;
5017 					(void) fprintf(stderr,
5018 					    MSG_INTL(MSG_ERR_WRITE), wname,
5019 					    strerror(err));
5020 					/*
5021 					 * Return an exit status of 1, because
5022 					 * the failure is not related to the
5023 					 * ELF file, but by system resources.
5024 					 */
5025 					ret = 1;
5026 					goto done;
5027 				}
5028 			}
5029 		}
5030 	}
5031 
5032 	/*
5033 	 * If we have no flag bits set that explicitly require a show or calc
5034 	 * operation, but match options (-I, -N, -T) were used, then run
5035 	 * through the section headers and see if we can't deduce show flags
5036 	 * from the match options given.
5037 	 *
5038 	 * We don't do this if -w was specified, because (-I, -N, -T) used
5039 	 * with -w in lieu of some other option is supposed to be quiet.
5040 	 */
5041 	if ((wfd == 0) && (flags & FLG_CTL_MATCH) &&
5042 	    ((flags & (FLG_MASK_SHOW | FLG_MASK_CALC)) == 0)) {
5043 		for (ndx = 1; ndx < shnum; ndx++) {
5044 			Cache	*_cache = &cache[ndx];
5045 
5046 			if (!match(MATCH_F_STRICT | MATCH_F_ALL, _cache->c_name,
5047 			    ndx, _cache->c_shdr->sh_type))
5048 				continue;
5049 
5050 			switch (_cache->c_shdr->sh_type) {
5051 			case SHT_PROGBITS:
5052 				/*
5053 				 * Heuristic time: It is usually bad form
5054 				 * to assume the meaning/format of a PROGBITS
5055 				 * section based on its name. However, there
5056 				 * are ABI mandated exceptions. Check for
5057 				 * these special names.
5058 				 */
5059 
5060 				/* The ELF ABI specifies .interp and .got */
5061 				if (strcmp(_cache->c_name,
5062 				    MSG_ORIG(MSG_ELF_INTERP)) == 0) {
5063 					flags |= FLG_SHOW_INTERP;
5064 					break;
5065 				}
5066 				if (strcmp(_cache->c_name,
5067 				    MSG_ORIG(MSG_ELF_GOT)) == 0) {
5068 					flags |= FLG_SHOW_GOT;
5069 					break;
5070 				}
5071 				/*
5072 				 * The GNU compilers, and amd64 ABI, define
5073 				 * .eh_frame and .eh_frame_hdr. The Sun
5074 				 * C++ ABI defines .exception_ranges.
5075 				 */
5076 				if ((strncmp(_cache->c_name,
5077 				    MSG_ORIG(MSG_SCN_FRM),
5078 				    MSG_SCN_FRM_SIZE) == 0) ||
5079 				    (strncmp(_cache->c_name,
5080 				    MSG_ORIG(MSG_SCN_EXRANGE),
5081 				    MSG_SCN_EXRANGE_SIZE) == 0)) {
5082 					flags |= FLG_SHOW_UNWIND;
5083 					break;
5084 				}
5085 				break;
5086 
5087 			case SHT_SYMTAB:
5088 			case SHT_DYNSYM:
5089 			case SHT_SUNW_LDYNSYM:
5090 			case SHT_SUNW_versym:
5091 			case SHT_SYMTAB_SHNDX:
5092 				flags |= FLG_SHOW_SYMBOLS;
5093 				break;
5094 
5095 			case SHT_RELA:
5096 			case SHT_REL:
5097 				flags |= FLG_SHOW_RELOC;
5098 				break;
5099 
5100 			case SHT_HASH:
5101 				flags |= FLG_SHOW_HASH;
5102 				break;
5103 
5104 			case SHT_DYNAMIC:
5105 				flags |= FLG_SHOW_DYNAMIC;
5106 				break;
5107 
5108 			case SHT_NOTE:
5109 				flags |= FLG_SHOW_NOTE;
5110 				break;
5111 
5112 			case SHT_GROUP:
5113 				flags |= FLG_SHOW_GROUP;
5114 				break;
5115 
5116 			case SHT_SUNW_symsort:
5117 			case SHT_SUNW_tlssort:
5118 				flags |= FLG_SHOW_SORT;
5119 				break;
5120 
5121 			case SHT_SUNW_cap:
5122 				flags |= FLG_SHOW_CAP;
5123 				break;
5124 
5125 			case SHT_SUNW_move:
5126 				flags |= FLG_SHOW_MOVE;
5127 				break;
5128 
5129 			case SHT_SUNW_syminfo:
5130 				flags |= FLG_SHOW_SYMINFO;
5131 				break;
5132 
5133 			case SHT_SUNW_verdef:
5134 			case SHT_SUNW_verneed:
5135 				flags |= FLG_SHOW_VERSIONS;
5136 				break;
5137 
5138 			case SHT_AMD64_UNWIND:
5139 				flags |= FLG_SHOW_UNWIND;
5140 				break;
5141 			}
5142 		}
5143 	}
5144 
5145 
5146 	if (flags & FLG_SHOW_SHDR)
5147 		sections(file, cache, shnum, ehdr, osabi);
5148 
5149 	if (flags & FLG_SHOW_INTERP)
5150 		interp(file, cache, shnum, phnum, elf);
5151 
5152 	if ((osabi == ELFOSABI_SOLARIS) || (osabi == ELFOSABI_LINUX))
5153 		versions(cache, shnum, file, flags, &versym);
5154 
5155 	if (flags & FLG_SHOW_SYMBOLS)
5156 		symbols(cache, shnum, ehdr, osabi, &versym, file, flags);
5157 
5158 	if ((flags & FLG_SHOW_SORT) && (osabi == ELFOSABI_SOLARIS))
5159 		sunw_sort(cache, shnum, ehdr, osabi, &versym, file, flags);
5160 
5161 	if (flags & FLG_SHOW_HASH)
5162 		hash(cache, shnum, file, flags);
5163 
5164 	if (flags & FLG_SHOW_GOT)
5165 		got(cache, shnum, ehdr, file);
5166 
5167 	if (flags & FLG_SHOW_GROUP)
5168 		group(cache, shnum, file, flags);
5169 
5170 	if (flags & FLG_SHOW_SYMINFO)
5171 		syminfo(cache, shnum, ehdr, osabi, file);
5172 
5173 	if (flags & FLG_SHOW_RELOC)
5174 		reloc(cache, shnum, ehdr, file);
5175 
5176 	if (flags & FLG_SHOW_DYNAMIC)
5177 		dynamic(cache, shnum, ehdr, osabi, file);
5178 
5179 	if (flags & FLG_SHOW_NOTE) {
5180 		Word	note_cnt;
5181 		size_t	note_shnum;
5182 		Cache	*note_cache;
5183 
5184 		note_cnt = note(cache, shnum, ehdr, file);
5185 
5186 		/*
5187 		 * Solaris core files have section headers, but these
5188 		 * headers do not include SHT_NOTE sections that reference
5189 		 * the core note sections. This means that note() won't
5190 		 * find the core notes. Fake section headers (-P option)
5191 		 * recover these sections, but it is inconvenient to require
5192 		 * users to specify -P in this situation. If the following
5193 		 * are all true:
5194 		 *
5195 		 *	- No note sections were found
5196 		 *	- This is a core file
5197 		 *	- We are not already using fake section headers
5198 		 *
5199 		 * then we will automatically generate fake section headers
5200 		 * and then process them in a second call to note().
5201 		 */
5202 		if ((note_cnt == 0) && (ehdr->e_type == ET_CORE) &&
5203 		    !(flags & FLG_CTL_FAKESHDR) &&
5204 		    (fake_shdr_cache(file, fd, elf, ehdr,
5205 		    &note_cache, &note_shnum) != 0)) {
5206 			(void) note(note_cache, note_shnum, ehdr, file);
5207 			fake_shdr_cache_free(note_cache, note_shnum);
5208 		}
5209 	}
5210 
5211 	if ((flags & FLG_SHOW_MOVE) && (osabi == ELFOSABI_SOLARIS))
5212 		move(cache, shnum, file, flags);
5213 
5214 	if (flags & FLG_CALC_CHECKSUM)
5215 		checksum(elf);
5216 
5217 	if ((flags & FLG_SHOW_CAP) && (osabi == ELFOSABI_SOLARIS))
5218 		cap(file, cache, shnum, phnum, ehdr, osabi, elf, flags);
5219 
5220 	if ((flags & FLG_SHOW_UNWIND) &&
5221 	    ((osabi == ELFOSABI_SOLARIS) || (osabi == ELFOSABI_LINUX)))
5222 		unwind(cache, shnum, phnum, ehdr, osabi, file, elf, flags);
5223 
5224 
5225 	/* Release the memory used to cache section headers */
5226 done:
5227 	if (flags & FLG_CTL_FAKESHDR)
5228 		fake_shdr_cache_free(cache, shnum);
5229 	else
5230 		free(cache);
5231 
5232 	return (ret);
5233 }
5234