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 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Dump an elf file.
30  */
31 #include	<machdep.h>
32 #include	<sys/elf_386.h>
33 #include	<sys/elf_amd64.h>
34 #include	<sys/elf_SPARC.h>
35 #include	<dwarf.h>
36 #include	<unistd.h>
37 #include	<errno.h>
38 #include	<strings.h>
39 #include	<debug.h>
40 #include	<conv.h>
41 #include	<msg.h>
42 #include	<_elfdump.h>
43 
44 
45 
46 /*
47  * VERSYM_STATE is used to maintain information about the VERSYM section
48  * in the object being analyzed. It is filled in by versions(), and used
49  * by init_symtbl_state() when displaying symbol information.
50  *
51  * max_verndx contains the largest version index that can appear
52  * in a Versym entry. This can never be less than 1: In the case where
53  * there is no verdef/verneed sections, the [0] index is reserved
54  * for local symbols, and the [1] index for globals. If Solaris versioning
55  * rules are in effect and there is a verdef section, then the number
56  * of defined versions provides this number. If GNU versioning is in effect,
57  * then:
58  *	- If there is no verneed section, it is the same as for
59  *		Solaris versioning.
60  *	- If there is a verneed section, the vna_other field of the
61  *		Vernaux structs contain versions, and max_verndx is the
62  *		largest such index.
63  *
64  * The value of the gnu field is based on the presence of
65  * a DT_VERSYM entry in the dynamic section: GNU ld produces these, and
66  * Solaris ld does not.
67  */
68 typedef struct {
69 	Cache	*cache;		/* Pointer to cache entry for VERSYM */
70 	Versym	*data;		/* Pointer to versym array */
71 	int	gnu;		/* True if object uses GNU versioning rules */
72 	int	max_verndx;	/* largest versym index value */
73 } VERSYM_STATE;
74 
75 /*
76  * SYMTBL_STATE is used to maintain information about a single symbol
77  * table section, for use by the routines that display symbol information.
78  */
79 typedef struct {
80 	const char	*file;		/* Name of file */
81 	Ehdr		*ehdr;		/* ELF header for file */
82 	Cache		*cache;		/* Cache of all section headers */
83 	Word		shnum;		/* # of sections in cache */
84 	Cache		*seccache;	/* Cache of symbol table section hdr */
85 	Word		secndx;		/* Index of symbol table section hdr */
86 	const char	*secname;	/* Name of section */
87 	uint_t		flags;		/* Command line option flags */
88 	struct {			/* Extended section index data */
89 		int	checked;	/* TRUE if already checked for shxndx */
90 		Word	*data;		/* NULL, or extended section index */
91 					/*	used for symbol table entries */
92 		uint_t	n;		/* # items in shxndx.data */
93 	} shxndx;
94 	VERSYM_STATE	*versym;	/* NULL, or associated VERSYM section */
95 	Sym 		*sym;		/* Array of symbols */
96 	Word		symn;		/* # of symbols */
97 } SYMTBL_STATE;
98 
99 
100 
101 /*
102  * Focal point for verifying symbol names.
103  */
104 static const char *
105 string(Cache *refsec, Word ndx, Cache *strsec, const char *file, Word name)
106 {
107 	/*
108 	 * If an error in this routine is due to a property of the string
109 	 * section, as opposed to a bad offset into the section (a property of
110 	 * the referencing section), then we will detect the same error on
111 	 * every call involving those sections. We use these static variables
112 	 * to retain the information needed to only issue each such error once.
113 	 */
114 	static Cache	*last_refsec;	/* Last referencing section seen */
115 	static int	strsec_err;	/* True if error issued */
116 
117 	const char	*strs;
118 	Word		strn;
119 
120 	if (strsec->c_data == NULL)
121 		return (NULL);
122 
123 	strs = (char *)strsec->c_data->d_buf;
124 	strn = strsec->c_data->d_size;
125 
126 	/*
127 	 * We only print a diagnostic regarding a bad string table once per
128 	 * input section being processed. If the refsec has changed, reset
129 	 * our retained error state.
130 	 */
131 	if (last_refsec != refsec) {
132 		last_refsec = refsec;
133 		strsec_err = 0;
134 	}
135 
136 	/* Verify that strsec really is a string table */
137 	if (strsec->c_shdr->sh_type != SHT_STRTAB) {
138 		if (!strsec_err) {
139 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_NOTSTRTAB),
140 			    file, strsec->c_ndx, refsec->c_ndx);
141 			strsec_err = 1;
142 		}
143 		return (MSG_INTL(MSG_STR_UNKNOWN));
144 	}
145 
146 	/*
147 	 * Is the string table offset within range of the available strings?
148 	 */
149 	if (name >= strn) {
150 		/*
151 		 * Do we have a empty string table?
152 		 */
153 		if (strs == 0) {
154 			if (!strsec_err) {
155 				(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
156 				    file, strsec->c_name);
157 				strsec_err = 1;
158 			}
159 		} else {
160 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSTOFF),
161 			    file, refsec->c_name, EC_WORD(ndx), strsec->c_name,
162 			    EC_WORD(name), EC_WORD(strn - 1));
163 		}
164 
165 		/*
166 		 * Return the empty string so that the calling function can
167 		 * continue it's output diagnostics.
168 		 */
169 		return (MSG_INTL(MSG_STR_UNKNOWN));
170 	}
171 	return (strs + name);
172 }
173 
174 /*
175  * Relocations can reference section symbols and standard symbols.  If the
176  * former, establish the section name.
177  */
178 static const char *
179 relsymname(Cache *cache, Cache *csec, Cache *strsec, Word symndx, Word symnum,
180     Word relndx, Sym *syms, char *secstr, size_t secsz, const char *file,
181     uint_t flags)
182 {
183 	Sym	*sym;
184 
185 	if (symndx >= symnum) {
186 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_RELBADSYMNDX),
187 		    file, EC_WORD(symndx), EC_WORD(relndx));
188 		return (MSG_INTL(MSG_STR_UNKNOWN));
189 	}
190 
191 	sym = (Sym *)(syms + symndx);
192 
193 	/*
194 	 * If the symbol represents a section offset construct an appropriate
195 	 * string.
196 	 */
197 	if ((ELF_ST_TYPE(sym->st_info) == STT_SECTION) && (sym->st_name == 0)) {
198 		if (flags & FLG_LONGNAME)
199 			(void) snprintf(secstr, secsz,
200 			    MSG_INTL(MSG_STR_L_SECTION),
201 			    cache[sym->st_shndx].c_name);
202 		else
203 			(void) snprintf(secstr, secsz,
204 			    MSG_INTL(MSG_STR_SECTION),
205 			    cache[sym->st_shndx].c_name);
206 		return ((const char *)secstr);
207 	}
208 
209 	return (string(csec, symndx, strsec, file, sym->st_name));
210 }
211 
212 /*
213  * Focal point for establishing a string table section.  Data such as the
214  * dynamic information simply points to a string table.  Data such as
215  * relocations, reference a symbol table, which in turn is associated with a
216  * string table.
217  */
218 static int
219 stringtbl(Cache *cache, int symtab, Word ndx, Word shnum, const char *file,
220     Word *symnum, Cache **symsec, Cache **strsec)
221 {
222 	Shdr	*shdr = cache[ndx].c_shdr;
223 
224 	if (symtab) {
225 		/*
226 		 * Validate the symbol table section.
227 		 */
228 		if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
229 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
230 			    file, cache[ndx].c_name, EC_WORD(shdr->sh_link));
231 			return (0);
232 		}
233 		if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
234 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
235 			    file, cache[ndx].c_name);
236 			return (0);
237 		}
238 
239 		/*
240 		 * Obtain, and verify the symbol table data.
241 		 */
242 		if ((cache[ndx].c_data == NULL) ||
243 		    (cache[ndx].c_data->d_buf == NULL)) {
244 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
245 			    file, cache[ndx].c_name);
246 			return (0);
247 		}
248 
249 		/*
250 		 * Establish the string table index.
251 		 */
252 		ndx = shdr->sh_link;
253 		shdr = cache[ndx].c_shdr;
254 
255 		/*
256 		 * Return symbol table information.
257 		 */
258 		if (symnum)
259 			*symnum = (shdr->sh_size / shdr->sh_entsize);
260 		if (symsec)
261 			*symsec = &cache[ndx];
262 	}
263 
264 	/*
265 	 * Validate the associated string table section.
266 	 */
267 	if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
268 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
269 		    file, cache[ndx].c_name, EC_WORD(shdr->sh_link));
270 		return (0);
271 	}
272 
273 	if (strsec)
274 		*strsec = &cache[shdr->sh_link];
275 
276 	return (1);
277 }
278 
279 /*
280  * Lookup a symbol and set Sym accordingly.
281  */
282 static int
283 symlookup(const char *name, Cache *cache, Word shnum, Sym **sym,
284     Cache *symtab, const char *file)
285 {
286 	Shdr	*shdr;
287 	Word	symn, cnt;
288 	Sym	*syms;
289 
290 	if (symtab == 0)
291 		return (0);
292 
293 	shdr = symtab->c_shdr;
294 
295 	/*
296 	 * Determine the symbol data and number.
297 	 */
298 	if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
299 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
300 		    file, symtab->c_name);
301 		return (0);
302 	}
303 	if (symtab->c_data == NULL)
304 		return (0);
305 
306 	/* LINTED */
307 	symn = (Word)(shdr->sh_size / shdr->sh_entsize);
308 	syms = (Sym *)symtab->c_data->d_buf;
309 
310 	/*
311 	 * Get the associated string table section.
312 	 */
313 	if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
314 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
315 		    file, symtab->c_name, EC_WORD(shdr->sh_link));
316 		return (0);
317 	}
318 
319 	/*
320 	 * Loop through the symbol table to find a match.
321 	 */
322 	for (cnt = 0; cnt < symn; syms++, cnt++) {
323 		const char	*symname;
324 
325 		symname = string(symtab, cnt, &cache[shdr->sh_link], file,
326 		    syms->st_name);
327 
328 		if (symname && (strcmp(name, symname) == 0)) {
329 			*sym = syms;
330 			return (1);
331 		}
332 	}
333 	return (0);
334 }
335 
336 /*
337  * Print section headers.
338  */
339 static void
340 sections(const char *file, Cache *cache, Word shnum, Ehdr *ehdr)
341 {
342 	size_t	seccnt;
343 
344 	for (seccnt = 1; seccnt < shnum; seccnt++) {
345 		Cache		*_cache = &cache[seccnt];
346 		Shdr		*shdr = _cache->c_shdr;
347 		const char	*secname = _cache->c_name;
348 
349 		/*
350 		 * Although numerous section header entries can be zero, it's
351 		 * usually a sign of trouble if the type is zero.
352 		 */
353 		if (shdr->sh_type == 0) {
354 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHTYPE),
355 			    file, secname, EC_WORD(shdr->sh_type));
356 		}
357 
358 		if (!match(0, secname, seccnt))
359 			continue;
360 
361 		/*
362 		 * Identify any sections that are suspicious.  A .got section
363 		 * shouldn't exist in a relocatable object.
364 		 */
365 		if (ehdr->e_type == ET_REL) {
366 			if (strncmp(secname, MSG_ORIG(MSG_ELF_GOT),
367 			    MSG_ELF_GOT_SIZE) == 0) {
368 				(void) fprintf(stderr,
369 				    MSG_INTL(MSG_GOT_UNEXPECTED), file,
370 				    secname);
371 			}
372 		}
373 
374 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
375 		dbg_print(0, MSG_INTL(MSG_ELF_SHDR), EC_WORD(seccnt), secname);
376 		Elf_shdr(0, ehdr->e_machine, shdr);
377 	}
378 }
379 
380 /*
381  * A couple of instances of unwind data are printed as tables of 8 data items
382  * expressed as 0x?? integers.
383  */
384 #define	UNWINDTBLSZ	10 + (8 * 5) + 1
385 
386 static void
387 unwindtbl(uint64_t *ndx, uint_t len, uchar_t *data, uint64_t doff,
388     const char *msg, const char *pre, size_t plen)
389 {
390 	char	buffer[UNWINDTBLSZ];
391 	uint_t	boff = plen, cnt = 0;
392 
393 	dbg_print(0, msg);
394 	(void) strncpy(buffer, pre, UNWINDTBLSZ);
395 
396 	while (*ndx < (len + 4)) {
397 		if (cnt == 8) {
398 			dbg_print(0, buffer);
399 			boff = plen;
400 			cnt = 0;
401 		}
402 		(void) snprintf(&buffer[boff], UNWINDTBLSZ - boff,
403 		    MSG_ORIG(MSG_UNW_TBLENTRY), data[doff + (*ndx)++]);
404 		boff += 5;
405 		cnt++;
406 	}
407 	if (cnt)
408 		dbg_print(0, buffer);
409 }
410 
411 /*
412  * Obtain a specified Phdr entry.
413  */
414 static Phdr *
415 getphdr(Word phnum, Word type, const char *file, Elf *elf)
416 {
417 	Word	cnt;
418 	Phdr	*phdr;
419 
420 	if ((phdr = elf_getphdr(elf)) == NULL) {
421 		failure(file, MSG_ORIG(MSG_ELF_GETPHDR));
422 		return (0);
423 	}
424 
425 	for (cnt = 0; cnt < phnum; phdr++, cnt++) {
426 		if (phdr->p_type == type)
427 			return (phdr);
428 	}
429 	return (0);
430 }
431 
432 static void
433 unwind(Cache *cache, Word shnum, Word phnum, Ehdr *ehdr, const char *file,
434     Elf *elf)
435 {
436 	Conv_dwarf_ehe_buf_t	dwarf_ehe_buf;
437 	Word	cnt;
438 	Phdr	*uphdr = 0;
439 
440 	/*
441 	 * For the moment - UNWIND is only relevant for a AMD64 object.
442 	 */
443 	if (ehdr->e_machine != EM_AMD64)
444 		return;
445 
446 	if (phnum)
447 		uphdr = getphdr(phnum, PT_SUNW_UNWIND, file, elf);
448 
449 	for (cnt = 1; cnt < shnum; cnt++) {
450 		Cache		*_cache = &cache[cnt];
451 		Shdr		*shdr = _cache->c_shdr;
452 		uchar_t		*data;
453 		size_t		datasize;
454 		uint64_t	off, ndx, frame_ptr, fde_cnt, tabndx;
455 		uint_t		vers, frame_ptr_enc, fde_cnt_enc, table_enc;
456 
457 		/*
458 		 * AMD64 - this is a strmcp() just to find the gcc produced
459 		 * sections.  Soon gcc should be setting the section type - and
460 		 * we'll not need this strcmp().
461 		 */
462 		if ((shdr->sh_type != SHT_AMD64_UNWIND) &&
463 		    (strncmp(_cache->c_name, MSG_ORIG(MSG_SCN_FRM),
464 		    MSG_SCN_FRM_SIZE) != 0) &&
465 		    (strncmp(_cache->c_name, MSG_ORIG(MSG_SCN_FRMHDR),
466 		    MSG_SCN_FRMHDR_SIZE) != 0))
467 			continue;
468 
469 		if (!match(0, _cache->c_name, cnt))
470 			continue;
471 
472 		if (_cache->c_data == NULL)
473 			continue;
474 
475 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
476 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_UNWIND), _cache->c_name);
477 
478 		data = (uchar_t *)(_cache->c_data->d_buf);
479 		datasize = _cache->c_data->d_size;
480 		off = 0;
481 
482 		/*
483 		 * Is this a .eh_frame_hdr
484 		 */
485 		if ((uphdr && (shdr->sh_addr == uphdr->p_vaddr)) ||
486 		    (strncmp(_cache->c_name, MSG_ORIG(MSG_SCN_FRMHDR),
487 		    MSG_SCN_FRMHDR_SIZE) == 0)) {
488 			dbg_print(0, MSG_ORIG(MSG_UNW_FRMHDR));
489 			ndx = 0;
490 
491 			vers = data[ndx++];
492 			frame_ptr_enc = data[ndx++];
493 			fde_cnt_enc = data[ndx++];
494 			table_enc = data[ndx++];
495 
496 			dbg_print(0, MSG_ORIG(MSG_UNW_FRMVERS), vers);
497 
498 			frame_ptr = dwarf_ehe_extract(data, &ndx, frame_ptr_enc,
499 			    ehdr->e_ident, shdr->sh_addr + ndx);
500 
501 			dbg_print(0, MSG_ORIG(MSG_UNW_FRPTRENC),
502 			    conv_dwarf_ehe(frame_ptr_enc, &dwarf_ehe_buf),
503 			    EC_XWORD(frame_ptr));
504 
505 			fde_cnt = dwarf_ehe_extract(data, &ndx, fde_cnt_enc,
506 			    ehdr->e_ident, shdr->sh_addr + ndx);
507 
508 			dbg_print(0, MSG_ORIG(MSG_UNW_FDCNENC),
509 			    conv_dwarf_ehe(fde_cnt_enc, &dwarf_ehe_buf),
510 			    EC_XWORD(fde_cnt));
511 			dbg_print(0, MSG_ORIG(MSG_UNW_TABENC),
512 			    conv_dwarf_ehe(table_enc, &dwarf_ehe_buf));
513 			dbg_print(0, MSG_ORIG(MSG_UNW_BINSRTAB1));
514 			dbg_print(0, MSG_ORIG(MSG_UNW_BINSRTAB2));
515 
516 			for (tabndx = 0; tabndx < fde_cnt; tabndx++) {
517 				dbg_print(0, MSG_ORIG(MSG_UNW_BINSRTABENT),
518 				    EC_XWORD(dwarf_ehe_extract(data, &ndx,
519 				    table_enc, ehdr->e_ident, shdr->sh_addr)),
520 				    EC_XWORD(dwarf_ehe_extract(data, &ndx,
521 				    table_enc, ehdr->e_ident, shdr->sh_addr)));
522 			}
523 			continue;
524 		}
525 
526 		/*
527 		 * Walk the Eh_frame's
528 		 */
529 		while (off < datasize) {
530 			uint_t		cieid, cielength, cieversion;
531 			uint_t		cieretaddr;
532 			int		cieRflag, cieLflag, ciePflag, cieZflag;
533 			uint_t		cieaugndx, length, id;
534 			uint64_t	ciecalign, ciedalign;
535 			char		*cieaugstr;
536 
537 			ndx = 0;
538 			/*
539 			 * extract length in lsb format
540 			 */
541 			length = LSB32EXTRACT(data + off + ndx);
542 			ndx += 4;
543 
544 			/*
545 			 * extract CIE id in lsb format
546 			 */
547 			id = LSB32EXTRACT(data + off + ndx);
548 			ndx += 4;
549 
550 			/*
551 			 * A CIE record has a id of '0', otherwise this is a
552 			 * FDE entry and the 'id' is the CIE pointer.
553 			 */
554 			if (id == 0) {
555 				uint64_t    persVal;
556 
557 				cielength = length;
558 				cieid = id;
559 				cieLflag = ciePflag = cieRflag = cieZflag = 0;
560 
561 				dbg_print(0, MSG_ORIG(MSG_UNW_CIE),
562 				    EC_XWORD(shdr->sh_addr + off));
563 				dbg_print(0, MSG_ORIG(MSG_UNW_CIELNGTH),
564 				    cielength, cieid);
565 
566 				cieversion = data[off + ndx];
567 				ndx += 1;
568 				cieaugstr = (char *)(&data[off + ndx]);
569 				ndx += strlen(cieaugstr) + 1;
570 
571 				dbg_print(0, MSG_ORIG(MSG_UNW_CIEVERS),
572 				    cieversion, cieaugstr);
573 
574 				ciecalign = uleb_extract(&data[off], &ndx);
575 				ciedalign = sleb_extract(&data[off], &ndx);
576 				cieretaddr = data[off + ndx];
577 				ndx += 1;
578 
579 				dbg_print(0, MSG_ORIG(MSG_UNW_CIECALGN),
580 				    EC_XWORD(ciecalign), EC_XWORD(ciedalign),
581 				    cieretaddr);
582 
583 				if (cieaugstr[0])
584 					dbg_print(0,
585 					    MSG_ORIG(MSG_UNW_CIEAXVAL));
586 
587 				for (cieaugndx = 0; cieaugstr[cieaugndx];
588 				    cieaugndx++) {
589 					uint_t	val;
590 
591 					switch (cieaugstr[cieaugndx]) {
592 					case 'z':
593 						val = uleb_extract(&data[off],
594 						    &ndx);
595 						dbg_print(0,
596 						    MSG_ORIG(MSG_UNW_CIEAXSIZ),
597 						    val);
598 						cieZflag = 1;
599 						break;
600 					case 'P':
601 						ciePflag = data[off + ndx];
602 						ndx += 1;
603 
604 						persVal = dwarf_ehe_extract(
605 						    &data[off], &ndx, ciePflag,
606 						    ehdr->e_ident,
607 						    shdr->sh_addr + off + ndx);
608 						dbg_print(0,
609 						    MSG_ORIG(MSG_UNW_CIEAXPERS),
610 						    ciePflag,
611 						    conv_dwarf_ehe(ciePflag,
612 						    &dwarf_ehe_buf),
613 						    EC_XWORD(persVal));
614 						break;
615 					case 'R':
616 						val = data[off + ndx];
617 						ndx += 1;
618 						dbg_print(0,
619 						    MSG_ORIG(MSG_UNW_CIEAXCENC),
620 						    val, conv_dwarf_ehe(val,
621 						    &dwarf_ehe_buf));
622 						cieRflag = val;
623 						break;
624 					case 'L':
625 						val = data[off + ndx];
626 						ndx += 1;
627 						dbg_print(0,
628 						    MSG_ORIG(MSG_UNW_CIEAXLSDA),
629 						    val, conv_dwarf_ehe(val,
630 						    &dwarf_ehe_buf));
631 						cieLflag = val;
632 						break;
633 					default:
634 						dbg_print(0,
635 						    MSG_ORIG(MSG_UNW_CIEAXUNEC),
636 						    cieaugstr[cieaugndx]);
637 						break;
638 					}
639 				}
640 				if ((cielength + 4) > ndx)
641 					unwindtbl(&ndx, cielength, data, off,
642 					    MSG_ORIG(MSG_UNW_CIECFI),
643 					    MSG_ORIG(MSG_UNW_CIEPRE),
644 					    MSG_UNW_CIEPRE_SIZE);
645 				off += cielength + 4;
646 
647 			} else {
648 				uint_t	    fdelength = length;
649 				int	    fdecieptr = id;
650 				uint64_t    fdeinitloc, fdeaddrrange;
651 
652 				dbg_print(0, MSG_ORIG(MSG_UNW_FDE),
653 				    EC_XWORD(shdr->sh_addr + off));
654 				dbg_print(0, MSG_ORIG(MSG_UNW_FDELNGTH),
655 				    fdelength, fdecieptr);
656 
657 				fdeinitloc = dwarf_ehe_extract(&data[off],
658 				    &ndx, cieRflag, ehdr->e_ident,
659 				    shdr->sh_addr + off + ndx);
660 				fdeaddrrange = dwarf_ehe_extract(&data[off],
661 				    &ndx, (cieRflag & ~DW_EH_PE_pcrel),
662 				    ehdr->e_ident,
663 				    shdr->sh_addr + off + ndx);
664 
665 				dbg_print(0, MSG_ORIG(MSG_UNW_FDEINITLOC),
666 				    EC_XWORD(fdeinitloc),
667 				    EC_XWORD(fdeaddrrange));
668 
669 				if (cieaugstr[0])
670 					dbg_print(0,
671 					    MSG_ORIG(MSG_UNW_FDEAXVAL));
672 				if (cieZflag) {
673 					uint64_t    val;
674 					val = uleb_extract(&data[off], &ndx);
675 					dbg_print(0,
676 					    MSG_ORIG(MSG_UNW_FDEAXSIZE),
677 					    EC_XWORD(val));
678 					if (val & cieLflag) {
679 						fdeinitloc = dwarf_ehe_extract(
680 						    &data[off], &ndx, cieLflag,
681 						    ehdr->e_ident,
682 						    shdr->sh_addr + off + ndx);
683 						dbg_print(0,
684 						    MSG_ORIG(MSG_UNW_FDEAXLSDA),
685 						    EC_XWORD(val));
686 					}
687 				}
688 				if ((fdelength + 4) > ndx)
689 					unwindtbl(&ndx, fdelength, data, off,
690 					    MSG_ORIG(MSG_UNW_FDECFI),
691 					    MSG_ORIG(MSG_UNW_FDEPRE),
692 					    MSG_UNW_FDEPRE_SIZE);
693 				off += fdelength + 4;
694 			}
695 		}
696 	}
697 }
698 
699 /*
700  * Print the hardware/software capabilities.  For executables and shared objects
701  * this should be accompanied with a program header.
702  */
703 static void
704 cap(const char *file, Cache *cache, Word shnum, Word phnum, Ehdr *ehdr,
705     Elf *elf)
706 {
707 	Word		cnt;
708 	Shdr		*cshdr = 0;
709 	Cache		*ccache;
710 	Off		cphdr_off = 0;
711 	Xword		cphdr_sz;
712 
713 	/*
714 	 * Determine if a hardware/software capabilities header exists.
715 	 */
716 	if (phnum) {
717 		Phdr	*phdr;
718 
719 		if ((phdr = elf_getphdr(elf)) == NULL) {
720 			failure(file, MSG_ORIG(MSG_ELF_GETPHDR));
721 			return;
722 		}
723 
724 		for (cnt = 0; cnt < phnum; phdr++, cnt++) {
725 			if (phdr->p_type == PT_SUNWCAP) {
726 				cphdr_off = phdr->p_offset;
727 				cphdr_sz = phdr->p_filesz;
728 				break;
729 			}
730 		}
731 	}
732 
733 	/*
734 	 * Determine if a hardware/software capabilities section exists.
735 	 */
736 	for (cnt = 1; cnt < shnum; cnt++) {
737 		Cache	*_cache = &cache[cnt];
738 		Shdr	*shdr = _cache->c_shdr;
739 
740 		if (shdr->sh_type != SHT_SUNW_cap)
741 			continue;
742 
743 		if (cphdr_off && ((cphdr_off < shdr->sh_offset) ||
744 		    (cphdr_off + cphdr_sz) > (shdr->sh_offset + shdr->sh_size)))
745 			continue;
746 
747 		if (_cache->c_data == NULL)
748 			continue;
749 
750 		ccache = _cache;
751 		cshdr = shdr;
752 		break;
753 	}
754 
755 	if ((cshdr == 0) && (cphdr_off == 0))
756 		return;
757 
758 	/*
759 	 * Print the hardware/software capabilities section.
760 	 */
761 	if (cshdr) {
762 		Word	ndx, capn;
763 		Cap	*cap = (Cap *)ccache->c_data->d_buf;
764 
765 		if ((cshdr->sh_entsize == 0) || (cshdr->sh_size == 0)) {
766 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
767 			    file, ccache->c_name);
768 			return;
769 		}
770 
771 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
772 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_CAP), ccache->c_name);
773 
774 		Elf_cap_title(0);
775 
776 		capn = (Word)(cshdr->sh_size / cshdr->sh_entsize);
777 
778 		for (ndx = 0; ndx < capn; cap++, ndx++) {
779 			if (cap->c_tag != CA_SUNW_NULL)
780 				Elf_cap_entry(0, cap, ndx, ehdr->e_machine);
781 		}
782 	} else
783 		(void) fprintf(stderr, MSG_INTL(MSG_WARN_INVCAP1), file);
784 
785 	/*
786 	 * If this object is an executable or shared object, then the
787 	 * hardware/software capabilities section should have an accompanying
788 	 * program header.
789 	 */
790 	if (cshdr && ((ehdr->e_type == ET_EXEC) || (ehdr->e_type == ET_DYN))) {
791 		if (cphdr_off == 0)
792 			(void) fprintf(stderr, MSG_INTL(MSG_WARN_INVCAP2),
793 			    file, ccache->c_name);
794 		else if ((cphdr_off != cshdr->sh_offset) ||
795 		    (cphdr_sz != cshdr->sh_size))
796 			(void) fprintf(stderr, MSG_INTL(MSG_WARN_INVCAP3),
797 			    file, ccache->c_name);
798 	}
799 }
800 
801 /*
802  * Print the interpretor.
803  */
804 static void
805 interp(const char *file, Cache *cache, Word shnum, Word phnum, Elf *elf)
806 {
807 	Word	cnt;
808 	Shdr	*ishdr = 0;
809 	Cache	*icache;
810 	Off	iphdr_off = 0;
811 	Xword	iphdr_fsz;
812 
813 	/*
814 	 * Determine if an interp header exists.
815 	 */
816 	if (phnum) {
817 		Phdr	*phdr;
818 
819 		if ((phdr = getphdr(phnum, PT_INTERP, file, elf)) != 0) {
820 			iphdr_off = phdr->p_offset;
821 			iphdr_fsz = phdr->p_filesz;
822 		}
823 	}
824 
825 	if (iphdr_off == 0)
826 		return;
827 
828 	/*
829 	 * Determine if an interp section exists.
830 	 */
831 	for (cnt = 1; cnt < shnum; cnt++) {
832 		Cache	*_cache = &cache[cnt];
833 		Shdr	*shdr = _cache->c_shdr;
834 
835 		/*
836 		 * Scan sections to find a section which contains the PT_INTERP
837 		 * string.  The target section can't be in a NOBITS section.
838 		 */
839 		if ((shdr->sh_type == SHT_NOBITS) ||
840 		    (iphdr_off < shdr->sh_offset) ||
841 		    (iphdr_off + iphdr_fsz) > (shdr->sh_offset + shdr->sh_size))
842 			continue;
843 
844 		icache = _cache;
845 		ishdr = shdr;
846 		break;
847 	}
848 
849 	/*
850 	 * Print the interpreter string based on the offset defined in the
851 	 * program header, as this is the offset used by the kernel.
852 	 */
853 	if (ishdr && icache->c_data) {
854 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
855 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_INTERP), icache->c_name);
856 		dbg_print(0, MSG_ORIG(MSG_FMT_INDENT),
857 		    (char *)icache->c_data->d_buf +
858 		    (iphdr_off - ishdr->sh_offset));
859 	} else
860 		(void) fprintf(stderr, MSG_INTL(MSG_WARN_INVINTERP1), file);
861 
862 	/*
863 	 * If there are any inconsistences between the program header and
864 	 * section information, flag them.
865 	 */
866 	if (ishdr && ((iphdr_off != ishdr->sh_offset) ||
867 	    (iphdr_fsz != ishdr->sh_size))) {
868 		(void) fprintf(stderr, MSG_INTL(MSG_WARN_INVINTERP2), file,
869 		    icache->c_name);
870 	}
871 }
872 
873 /*
874  * Print the syminfo section.
875  */
876 static void
877 syminfo(Cache *cache, Word shnum, const char *file)
878 {
879 	Shdr		*infoshdr;
880 	Syminfo		*info;
881 	Sym		*syms;
882 	Dyn		*dyns;
883 	Word		infonum, cnt, ndx, symnum;
884 	Cache		*infocache = 0, *symsec, *strsec;
885 
886 	for (cnt = 1; cnt < shnum; cnt++) {
887 		if (cache[cnt].c_shdr->sh_type == SHT_SUNW_syminfo) {
888 			infocache = &cache[cnt];
889 			break;
890 		}
891 	}
892 	if (infocache == 0)
893 		return;
894 
895 	infoshdr = infocache->c_shdr;
896 	if ((infoshdr->sh_entsize == 0) || (infoshdr->sh_size == 0)) {
897 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
898 		    file, infocache->c_name);
899 		return;
900 	}
901 	if (infocache->c_data == NULL)
902 		return;
903 
904 	infonum = (Word)(infoshdr->sh_size / infoshdr->sh_entsize);
905 	info = (Syminfo *)infocache->c_data->d_buf;
906 
907 	/*
908 	 * Get the data buffer of the associated dynamic section.
909 	 */
910 	if ((infoshdr->sh_info == 0) || (infoshdr->sh_info >= shnum)) {
911 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHINFO),
912 		    file, infocache->c_name, EC_WORD(infoshdr->sh_info));
913 		return;
914 	}
915 	if (cache[infoshdr->sh_info].c_data == NULL)
916 		return;
917 
918 	dyns = cache[infoshdr->sh_info].c_data->d_buf;
919 	if (dyns == 0) {
920 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
921 		    file, cache[infoshdr->sh_info].c_name);
922 		return;
923 	}
924 
925 	/*
926 	 * Get the data buffer for the associated symbol table and string table.
927 	 */
928 	if (stringtbl(cache, 1, cnt, shnum, file,
929 	    &symnum, &symsec, &strsec) == 0)
930 		return;
931 
932 	syms = symsec->c_data->d_buf;
933 
934 	/*
935 	 * Loop through the syminfo entries.
936 	 */
937 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
938 	dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMINFO), infocache->c_name);
939 	Elf_syminfo_title(0);
940 
941 	for (ndx = 1, info++; ndx < infonum; ndx++, info++) {
942 		Sym 		*sym;
943 		const char	*needed = 0, *name;
944 
945 		if ((info->si_flags == 0) && (info->si_boundto == 0))
946 			continue;
947 
948 		sym = &syms[ndx];
949 		name = string(infocache, ndx, strsec, file, sym->st_name);
950 
951 		if (info->si_boundto < SYMINFO_BT_LOWRESERVE) {
952 			Dyn	*dyn = &dyns[info->si_boundto];
953 
954 			needed = string(infocache, info->si_boundto,
955 			    strsec, file, dyn->d_un.d_val);
956 		}
957 		Elf_syminfo_entry(0, ndx, info, name, needed);
958 	}
959 }
960 
961 /*
962  * Print version definition section entries.
963  */
964 static void
965 version_def(Verdef *vdf, Word vdf_num, Cache *vcache, Cache *scache,
966     const char *file)
967 {
968 	Word	cnt;
969 	char	index[MAXNDXSIZE];
970 
971 	Elf_ver_def_title(0);
972 
973 	for (cnt = 1; cnt <= vdf_num; cnt++,
974 	    vdf = (Verdef *)((uintptr_t)vdf + vdf->vd_next)) {
975 		const char	*name, *dep;
976 		Half		vcnt = vdf->vd_cnt - 1;
977 		Half		ndx = vdf->vd_ndx;
978 		Verdaux *vdap = (Verdaux *)((uintptr_t)vdf + vdf->vd_aux);
979 
980 		/*
981 		 * Obtain the name and first dependency (if any).
982 		 */
983 		name = string(vcache, cnt, scache, file, vdap->vda_name);
984 		vdap = (Verdaux *)((uintptr_t)vdap + vdap->vda_next);
985 		if (vcnt)
986 			dep = string(vcache, cnt, scache, file, vdap->vda_name);
987 		else
988 			dep = MSG_ORIG(MSG_STR_EMPTY);
989 
990 		(void) snprintf(index, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INDEX),
991 		    EC_XWORD(ndx));
992 		Elf_ver_line_1(0, index, name, dep,
993 		    conv_ver_flags(vdf->vd_flags));
994 
995 		/*
996 		 * Print any additional dependencies.
997 		 */
998 		if (vcnt) {
999 			vdap = (Verdaux *)((uintptr_t)vdap + vdap->vda_next);
1000 			for (vcnt--; vcnt; vcnt--,
1001 			    vdap = (Verdaux *)((uintptr_t)vdap +
1002 			    vdap->vda_next)) {
1003 				dep = string(vcache, cnt, scache, file,
1004 				    vdap->vda_name);
1005 				Elf_ver_line_2(0, MSG_ORIG(MSG_STR_EMPTY), dep);
1006 			}
1007 		}
1008 	}
1009 }
1010 
1011 /*
1012  * Print version needed section entries.
1013  *
1014  * entry:
1015  *	vnd - Address of verneed data
1016  *	vnd_num - # of Verneed entries
1017  *	vcache - Cache of verneed section being processed
1018  *	scache - Cache of associated string table section
1019  *	file - Name of object being processed.
1020  *	versym - Information about versym section
1021  *
1022  * exit:
1023  *	The versions have been printed. If GNU style versioning
1024  *	is in effect, versym->max_verndx has been updated to
1025  *	contain the largest version index seen.
1026  */
1027 static void
1028 version_need(Verneed *vnd, Word vnd_num, Cache *vcache, Cache *scache,
1029     const char *file, VERSYM_STATE *versym)
1030 {
1031 	Word		cnt;
1032 	char		index[MAXNDXSIZE];
1033 	const char	*index_str;
1034 
1035 	Elf_ver_need_title(0, versym->gnu);
1036 
1037 	/*
1038 	 * The versym section in an object that follows Solaris versioning
1039 	 * rules contains indexes into the verdef section. Symbols defined
1040 	 * in other objects (UNDEF) are given a version of 0, indicating that
1041 	 * they are not defined by this file, and the Verneed entries do not
1042 	 * have associated version indexes. For these reasons, we do not
1043 	 * display a version index for Solaris Verneed sections.
1044 	 *
1045 	 * The GNU versioning rules are different: Symbols defined in other
1046 	 * objects receive a version index in the range above those defined
1047 	 * by the Verdef section, and the vna_other field of the Vernaux
1048 	 * structs inside the Verneed section contain the version index for
1049 	 * that item. We therefore  display the index when showing the
1050 	 * contents of a GNU Verneed section. You should not expect these
1051 	 * indexes to appear in sorted order --- it seems that the GNU ld
1052 	 * assigns the versions as symbols are encountered during linking,
1053 	 * and then the results are assembled into the Verneed section
1054 	 * afterwards.
1055 	 */
1056 	if (versym->gnu) {
1057 		index_str = index;
1058 	} else {
1059 		/* For Solaris versioning, display a NULL string */
1060 		index_str = MSG_ORIG(MSG_STR_EMPTY);
1061 	}
1062 
1063 	for (cnt = 1; cnt <= vnd_num; cnt++,
1064 	    vnd = (Verneed *)((uintptr_t)vnd + vnd->vn_next)) {
1065 		const char	*name, *dep;
1066 		Half		vcnt = vnd->vn_cnt;
1067 		Vernaux *vnap = (Vernaux *)((uintptr_t)vnd + vnd->vn_aux);
1068 
1069 		/*
1070 		 * Obtain the name of the needed file and the version name
1071 		 * within it that we're dependent on.  Note that the count
1072 		 * should be at least one, otherwise this is a pretty bogus
1073 		 * entry.
1074 		 */
1075 		name = string(vcache, cnt, scache, file, vnd->vn_file);
1076 		if (vcnt)
1077 			dep = string(vcache, cnt, scache, file, vnap->vna_name);
1078 		else
1079 			dep = MSG_INTL(MSG_STR_NULL);
1080 
1081 		if (versym->gnu) {
1082 			/* Format the version index value */
1083 			(void) snprintf(index, MAXNDXSIZE,
1084 			    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(vnap->vna_other));
1085 			if (vnap->vna_other > versym->max_verndx)
1086 				versym->max_verndx = vnap->vna_other;
1087 		}
1088 		Elf_ver_line_1(0, index_str, name, dep,
1089 		    conv_ver_flags(vnap->vna_flags));
1090 
1091 		/*
1092 		 * Print any additional version dependencies.
1093 		 */
1094 		if (vcnt) {
1095 			vnap = (Vernaux *)((uintptr_t)vnap + vnap->vna_next);
1096 			for (vcnt--; vcnt; vcnt--,
1097 			    vnap = (Vernaux *)((uintptr_t)vnap +
1098 			    vnap->vna_next)) {
1099 				dep = string(vcache, cnt, scache, file,
1100 				    vnap->vna_name);
1101 				if (versym->gnu) {
1102 					/* Format the next index value */
1103 					(void) snprintf(index, MAXNDXSIZE,
1104 					    MSG_ORIG(MSG_FMT_INDEX),
1105 					    EC_XWORD(vnap->vna_other));
1106 					Elf_ver_line_1(0, index_str,
1107 					    MSG_ORIG(MSG_STR_EMPTY), dep,
1108 					    conv_ver_flags(vnap->vna_flags));
1109 					if (vnap->vna_other >
1110 					    versym->max_verndx)
1111 						versym->max_verndx =
1112 						    vnap->vna_other;
1113 				} else {
1114 					Elf_ver_line_3(0,
1115 					    MSG_ORIG(MSG_STR_EMPTY), dep,
1116 					    conv_ver_flags(vnap->vna_flags));
1117 				}
1118 			}
1119 		}
1120 	}
1121 }
1122 
1123 /*
1124  * Compute the max_verndx value for a GNU style object with
1125  * a Verneed section. This is only needed if version_need() is not
1126  * called.
1127  *
1128  * entry:
1129  *	vnd - Address of verneed data
1130  *	vnd_num - # of Verneed entries
1131  *	versym - Information about versym section
1132  *
1133  * exit:
1134  *	versym->max_verndx has been updated to contain the largest
1135  *	version index seen.
1136  */
1137 static void
1138 update_gnu_max_verndx(Verneed *vnd, Word vnd_num, VERSYM_STATE *versym)
1139 {
1140 	Word		cnt;
1141 
1142 	for (cnt = 1; cnt <= vnd_num; cnt++,
1143 	    vnd = (Verneed *)((uintptr_t)vnd + vnd->vn_next)) {
1144 		Half	vcnt = vnd->vn_cnt;
1145 		Vernaux	*vnap = (Vernaux *)((uintptr_t)vnd + vnd->vn_aux);
1146 
1147 		if (vnap->vna_other > versym->max_verndx)
1148 			versym->max_verndx = vnap->vna_other;
1149 
1150 		/*
1151 		 * Check any additional version dependencies.
1152 		 */
1153 		if (vcnt) {
1154 			vnap = (Vernaux *)((uintptr_t)vnap + vnap->vna_next);
1155 			for (vcnt--; vcnt; vcnt--,
1156 			    vnap = (Vernaux *)((uintptr_t)vnap +
1157 			    vnap->vna_next)) {
1158 				if (vnap->vna_other > versym->max_verndx)
1159 					versym->max_verndx = vnap->vna_other;
1160 			}
1161 		}
1162 	}
1163 }
1164 
1165 /*
1166  * Display version section information if the flags require it.
1167  * Return version information needed by other output.
1168  *
1169  * entry:
1170  *	cache - Cache of all section headers
1171  *	shnum - # of sections in cache
1172  *	file - Name of file
1173  *	flags - Command line option flags
1174  *	versym - VERSYM_STATE block to be filled in.
1175  */
1176 static void
1177 versions(Cache *cache, Word shnum, const char *file, uint_t flags,
1178     VERSYM_STATE *versym)
1179 {
1180 	GElf_Word	cnt;
1181 	Cache		*verdef_cache = NULL, *verneed_cache = NULL;
1182 
1183 
1184 	/* Gather information about the version sections */
1185 	bzero(versym, sizeof (*versym));
1186 	versym->max_verndx = 1;
1187 	for (cnt = 1; cnt < shnum; cnt++) {
1188 		Cache		*_cache = &cache[cnt];
1189 		Shdr		*shdr = _cache->c_shdr;
1190 		Dyn		*dyn;
1191 		ulong_t		numdyn;
1192 
1193 		switch (shdr->sh_type) {
1194 		case SHT_DYNAMIC:
1195 			/*
1196 			 * The GNU ld puts a DT_VERSYM entry in the dynamic
1197 			 * section so that the runtime linker can use it to
1198 			 * implement their versioning rules. They allow multiple
1199 			 * incompatible functions with the same name to exist
1200 			 * in different versions. The Solaris ld does not
1201 			 * support this mechanism, and as such, does not
1202 			 * produce DT_VERSYM. We use this fact to determine
1203 			 * which ld produced this object, and how to interpret
1204 			 * the version values.
1205 			 */
1206 			if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0) ||
1207 			    (_cache->c_data == NULL))
1208 				continue;
1209 			numdyn = shdr->sh_size / shdr->sh_entsize;
1210 			dyn = (Dyn *)_cache->c_data->d_buf;
1211 			for (; numdyn-- > 0; dyn++)
1212 				if (dyn->d_tag == DT_VERSYM) {
1213 					versym->gnu = 1;
1214 					break;
1215 				}
1216 			break;
1217 
1218 		case SHT_SUNW_versym:
1219 			/* Record data address for later symbol processing */
1220 			if (_cache->c_data != NULL) {
1221 				versym->cache = _cache;
1222 				versym->data = _cache->c_data->d_buf;
1223 				continue;
1224 			}
1225 			break;
1226 
1227 		case SHT_SUNW_verdef:
1228 		case SHT_SUNW_verneed:
1229 			/*
1230 			 * Ensure the data is non-NULL and the number
1231 			 * of items is non-zero. Otherwise, we don't
1232 			 * understand the section, and will not use it.
1233 			 */
1234 			if ((_cache->c_data == NULL) ||
1235 			    (_cache->c_data->d_buf == NULL)) {
1236 				(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
1237 				    file, _cache->c_name);
1238 				continue;
1239 			}
1240 			if (shdr->sh_info == 0) {
1241 				(void) fprintf(stderr,
1242 				    MSG_INTL(MSG_ERR_BADSHINFO),
1243 				    file, _cache->c_name,
1244 				    EC_WORD(shdr->sh_info));
1245 				continue;
1246 			}
1247 
1248 			/* Make sure the string table index is in range */
1249 			if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
1250 				(void) fprintf(stderr,
1251 				    MSG_INTL(MSG_ERR_BADSHLINK), file,
1252 				    _cache->c_name, EC_WORD(shdr->sh_link));
1253 				continue;
1254 			}
1255 
1256 			/*
1257 			 * The section is usable. Save the cache entry.
1258 			 */
1259 			if (shdr->sh_type == SHT_SUNW_verdef) {
1260 				verdef_cache = _cache;
1261 				/*
1262 				 * Under Solaris rules, if there is a verdef
1263 				 * section, the max versym index is number
1264 				 * of version definitions it supplies.
1265 				 */
1266 				versym->max_verndx = shdr->sh_info;
1267 			} else {
1268 				verneed_cache = _cache;
1269 			}
1270 			break;
1271 		}
1272 	}
1273 
1274 	if ((flags & FLG_VERSIONS) == 0) {
1275 		/*
1276 		 * If GNU versioning applies to this object, and there
1277 		 * is a Verneed section, then examine it to determine
1278 		 * the maximum Versym version index for this file.
1279 		 */
1280 		if ((versym->gnu) && (verneed_cache != NULL))
1281 			update_gnu_max_verndx(
1282 			    (Verneed *)verneed_cache->c_data->d_buf,
1283 			    verneed_cache->c_shdr->sh_info, versym);
1284 		return;
1285 	}
1286 
1287 	/*
1288 	 * Now that all the information is available, display the
1289 	 * Verdef and Verneed section contents.
1290 	 */
1291 	if (verdef_cache != NULL) {
1292 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1293 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_VERDEF),
1294 		    verdef_cache->c_name);
1295 		version_def((Verdef *)verdef_cache->c_data->d_buf,
1296 		    verdef_cache->c_shdr->sh_info, verdef_cache,
1297 		    &cache[verdef_cache->c_shdr->sh_link], file);
1298 	}
1299 	if (verneed_cache != NULL) {
1300 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1301 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_VERNEED),
1302 		    verneed_cache->c_name);
1303 		/*
1304 		 * If GNU versioning applies to this object, version_need()
1305 		 * will update versym->max_verndx, and it is not
1306 		 * necessary to call update_gnu_max_verndx().
1307 		 */
1308 		version_need((Verneed *)verneed_cache->c_data->d_buf,
1309 		    verneed_cache->c_shdr->sh_info, verneed_cache,
1310 		    &cache[verneed_cache->c_shdr->sh_link], file, versym);
1311 	}
1312 }
1313 
1314 /*
1315  * Initialize a symbol table state structure
1316  *
1317  * entry:
1318  *	state - State structure to be initialized
1319  *	cache - Cache of all section headers
1320  *	shnum - # of sections in cache
1321  *	secndx - Index of symbol table section
1322  *	ehdr - ELF header for file
1323  *	versym - Information about versym section
1324  *	file - Name of file
1325  *	flags - Command line option flags
1326  */
1327 static int
1328 init_symtbl_state(SYMTBL_STATE *state, Cache *cache, Word shnum, Word secndx,
1329     Ehdr *ehdr, VERSYM_STATE *versym, const char *file, uint_t flags)
1330 {
1331 	Shdr *shdr;
1332 
1333 	state->file = file;
1334 	state->ehdr = ehdr;
1335 	state->cache = cache;
1336 	state->shnum = shnum;
1337 	state->seccache = &cache[secndx];
1338 	state->secndx = secndx;
1339 	state->secname = state->seccache->c_name;
1340 	state->flags = flags;
1341 	state->shxndx.checked = 0;
1342 	state->shxndx.data = NULL;
1343 	state->shxndx.n = 0;
1344 
1345 	shdr = state->seccache->c_shdr;
1346 
1347 	/*
1348 	 * Check the symbol data and per-item size.
1349 	 */
1350 	if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
1351 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
1352 		    file, state->secname);
1353 		return (0);
1354 	}
1355 	if (state->seccache->c_data == NULL)
1356 		return (0);
1357 
1358 	/* LINTED */
1359 	state->symn = (Word)(shdr->sh_size / shdr->sh_entsize);
1360 	state->sym = (Sym *)state->seccache->c_data->d_buf;
1361 
1362 	/*
1363 	 * Check associated string table section.
1364 	 */
1365 	if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
1366 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
1367 		    file, state->secname, EC_WORD(shdr->sh_link));
1368 		return (0);
1369 	}
1370 
1371 	/*
1372 	 * Determine if there is a associated Versym section
1373 	 * with this Symbol Table.
1374 	 */
1375 	if (versym->cache &&
1376 	    (versym->cache->c_shdr->sh_link == state->secndx))
1377 		state->versym = versym;
1378 	else
1379 		state->versym = NULL;
1380 
1381 
1382 	return (1);
1383 }
1384 
1385 /*
1386  * Determine the extended section index used for symbol tables entries.
1387  */
1388 static void
1389 symbols_getxindex(SYMTBL_STATE * state)
1390 {
1391 	uint_t	symn;
1392 	Word	symcnt;
1393 
1394 	state->shxndx.checked = 1;   /* Note that we've been called */
1395 	for (symcnt = 1; symcnt < state->shnum; symcnt++) {
1396 		Cache	*_cache = &state->cache[symcnt];
1397 		Shdr	*shdr = _cache->c_shdr;
1398 
1399 		if ((shdr->sh_type != SHT_SYMTAB_SHNDX) ||
1400 		    (shdr->sh_link != state->secndx))
1401 			continue;
1402 
1403 		if ((shdr->sh_entsize) &&
1404 		    /* LINTED */
1405 		    ((symn = (uint_t)(shdr->sh_size / shdr->sh_entsize)) == 0))
1406 			continue;
1407 
1408 		if (_cache->c_data == NULL)
1409 			continue;
1410 
1411 		state->shxndx.data = _cache->c_data->d_buf;
1412 		state->shxndx.n = symn;
1413 		return;
1414 	}
1415 }
1416 
1417 /*
1418  * Produce a line of output for the given symbol
1419  *
1420  * entry:
1421  *	state - Symbol table state
1422  *	symndx - Index of symbol within the table
1423  *	info - Value of st_info (indicates local/global range)
1424  *	symndx_disp - Index to display. This may not be the same
1425  *		as symndx if the display is relative to the logical
1426  *		combination of the SUNW_ldynsym/dynsym tables.
1427  *	sym - Symbol to display
1428  */
1429 static void
1430 output_symbol(SYMTBL_STATE *state, Word symndx, Word info, Word disp_symndx,
1431     Sym *sym)
1432 {
1433 	/*
1434 	 * Symbol types for which we check that the specified
1435 	 * address/size land inside the target section.
1436 	 */
1437 	static const int addr_symtype[STT_NUM] = {
1438 		0,			/* STT_NOTYPE */
1439 		1,			/* STT_OBJECT */
1440 		1,			/* STT_FUNC */
1441 		0,			/* STT_SECTION */
1442 		0,			/* STT_FILE */
1443 		1,			/* STT_COMMON */
1444 		0,			/* STT_TLS */
1445 	};
1446 #if STT_NUM != (STT_TLS + 1)
1447 #error "STT_NUM has grown. Update addr_symtype[]"
1448 #endif
1449 
1450 	char		index[MAXNDXSIZE];
1451 	const char	*symname, *sec;
1452 	Versym		verndx;
1453 	int		gnuver;
1454 	uchar_t		type;
1455 	Shdr		*tshdr;
1456 	Word		shndx;
1457 	Conv_inv_buf_t	inv_buf;
1458 
1459 	/* Ensure symbol index is in range */
1460 	if (symndx >= state->symn) {
1461 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSORTNDX),
1462 		    state->file, state->secname, EC_WORD(symndx));
1463 		return;
1464 	}
1465 
1466 	/*
1467 	 * If we are using extended symbol indexes, find the
1468 	 * corresponding SHN_SYMTAB_SHNDX table.
1469 	 */
1470 	if ((sym->st_shndx == SHN_XINDEX) && (state->shxndx.checked == 0))
1471 		symbols_getxindex(state);
1472 
1473 	/* LINTED */
1474 	symname = string(state->seccache, symndx,
1475 	    &state->cache[state->seccache->c_shdr->sh_link], state->file,
1476 	    sym->st_name);
1477 
1478 	tshdr = 0;
1479 	sec = NULL;
1480 
1481 	if (state->ehdr->e_type == ET_CORE) {
1482 		sec = (char *)MSG_INTL(MSG_STR_UNKNOWN);
1483 	} else if (state->flags & FLG_FAKESHDR) {
1484 		/*
1485 		 * If we are using fake section headers derived from
1486 		 * the program headers, then the section indexes
1487 		 * in the symbols do not correspond to these headers.
1488 		 * The section names are not available, so all we can
1489 		 * do is to display them in numeric form.
1490 		 */
1491 		sec = conv_sym_shndx(sym->st_shndx, &inv_buf);
1492 	} else if ((sym->st_shndx < SHN_LORESERVE) &&
1493 	    (sym->st_shndx < state->shnum)) {
1494 		shndx = sym->st_shndx;
1495 		tshdr = state->cache[shndx].c_shdr;
1496 		sec = state->cache[shndx].c_name;
1497 	} else if (sym->st_shndx == SHN_XINDEX) {
1498 		if (state->shxndx.data) {
1499 			Word	_shxndx;
1500 
1501 			if (symndx > state->shxndx.n) {
1502 				(void) fprintf(stderr,
1503 				    MSG_INTL(MSG_ERR_BADSYMXINDEX1),
1504 				    state->file, state->secname,
1505 				    EC_WORD(symndx));
1506 			} else if ((_shxndx =
1507 			    state->shxndx.data[symndx]) > state->shnum) {
1508 				(void) fprintf(stderr,
1509 				    MSG_INTL(MSG_ERR_BADSYMXINDEX2),
1510 				    state->file, state->secname,
1511 				    EC_WORD(symndx), EC_WORD(_shxndx));
1512 			} else {
1513 				shndx = _shxndx;
1514 				tshdr = state->cache[shndx].c_shdr;
1515 				sec = state->cache[shndx].c_name;
1516 			}
1517 		} else {
1518 			(void) fprintf(stderr,
1519 			    MSG_INTL(MSG_ERR_BADSYMXINDEX3),
1520 			    state->file, state->secname, EC_WORD(symndx));
1521 		}
1522 	} else if ((sym->st_shndx < SHN_LORESERVE) &&
1523 	    (sym->st_shndx >= state->shnum)) {
1524 		(void) fprintf(stderr,
1525 		    MSG_INTL(MSG_ERR_BADSYM5), state->file,
1526 		    state->secname, demangle(symname, state->flags),
1527 		    sym->st_shndx);
1528 	}
1529 
1530 	/*
1531 	 * If versioning is available display the
1532 	 * version index. If not, then use 0.
1533 	 */
1534 	if (state->versym) {
1535 		Versym test_verndx;
1536 
1537 		verndx = test_verndx = state->versym->data[symndx];
1538 		gnuver = state->versym->gnu;
1539 
1540 		/*
1541 		 * Check to see if this is a defined symbol with a
1542 		 * version index that is outside the valid range for
1543 		 * the file. The interpretation of this depends on
1544 		 * the style of versioning used by the object.
1545 		 *
1546 		 * Versions >= VER_NDX_LORESERVE have special meanings,
1547 		 * and are exempt from this checking.
1548 		 *
1549 		 * GNU style version indexes use the top bit of the
1550 		 * 16-bit index value (0x8000) as the "hidden bit".
1551 		 * We must mask off this bit in order to compare
1552 		 * the version against the maximum value.
1553 		 */
1554 		if (gnuver)
1555 			test_verndx &= ~0x8000;
1556 
1557 		if ((test_verndx > state->versym->max_verndx) &&
1558 		    (verndx < VER_NDX_LORESERVE))
1559 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADVER),
1560 			    state->file, state->secname, EC_WORD(symndx),
1561 			    EC_HALF(test_verndx), state->versym->max_verndx);
1562 	} else {
1563 		verndx = 0;
1564 		gnuver = 0;
1565 	}
1566 
1567 	/*
1568 	 * Error checking for TLS.
1569 	 */
1570 	type = ELF_ST_TYPE(sym->st_info);
1571 	if (type == STT_TLS) {
1572 		if (tshdr &&
1573 		    (sym->st_shndx != SHN_UNDEF) &&
1574 		    ((tshdr->sh_flags & SHF_TLS) == 0)) {
1575 			(void) fprintf(stderr,
1576 			    MSG_INTL(MSG_ERR_BADSYM3), state->file,
1577 			    state->secname, demangle(symname, state->flags));
1578 		}
1579 	} else if ((type != STT_SECTION) && sym->st_size &&
1580 	    tshdr && (tshdr->sh_flags & SHF_TLS)) {
1581 		(void) fprintf(stderr,
1582 		    MSG_INTL(MSG_ERR_BADSYM4), state->file,
1583 		    state->secname, demangle(symname, state->flags));
1584 	}
1585 
1586 	/*
1587 	 * If a symbol with non-zero size has a type that
1588 	 * specifies an address, then make sure the location
1589 	 * it references is actually contained within the
1590 	 * section.  UNDEF symbols don't count in this case,
1591 	 * so we ignore them.
1592 	 *
1593 	 * The meaning of the st_value field in a symbol
1594 	 * depends on the type of object. For a relocatable
1595 	 * object, it is the offset within the section.
1596 	 * For sharable objects, it is the offset relative to
1597 	 * the base of the object, and for other types, it is
1598 	 * the virtual address. To get an offset within the
1599 	 * section for non-ET_REL files, we subtract the
1600 	 * base address of the section.
1601 	 */
1602 	if (addr_symtype[type] && (sym->st_size > 0) &&
1603 	    (sym->st_shndx != SHN_UNDEF) && ((sym->st_shndx < SHN_LORESERVE) ||
1604 	    (sym->st_shndx == SHN_XINDEX)) && (tshdr != NULL)) {
1605 		Word v = sym->st_value;
1606 			if (state->ehdr->e_type != ET_REL)
1607 				v -= tshdr->sh_addr;
1608 		if (((v + sym->st_size) > tshdr->sh_size)) {
1609 			(void) fprintf(stderr,
1610 			    MSG_INTL(MSG_ERR_BADSYM6), state->file,
1611 			    state->secname, demangle(symname, state->flags),
1612 			    EC_WORD(shndx), EC_XWORD(tshdr->sh_size),
1613 			    EC_XWORD(sym->st_value), EC_XWORD(sym->st_size));
1614 		}
1615 	}
1616 
1617 	/*
1618 	 * A typical symbol table uses the sh_info field to indicate one greater
1619 	 * than the symbol table index of the last local symbol, STB_LOCAL.
1620 	 * Therefore, symbol indexes less than sh_info should have local
1621 	 * binding.  Symbol indexes greater than, or equal to sh_info, should
1622 	 * have global binding.  Note, we exclude UNDEF/NOTY symbols with zero
1623 	 * value and size, as these symbols may be the result of an mcs(1)
1624 	 * section deletion.
1625 	 */
1626 	if (info) {
1627 		uchar_t	bind = ELF_ST_BIND(sym->st_info);
1628 
1629 		if ((symndx < info) && (bind != STB_LOCAL)) {
1630 			(void) fprintf(stderr,
1631 			    MSG_INTL(MSG_ERR_BADSYM7), state->file,
1632 			    state->secname, demangle(symname, state->flags),
1633 			    EC_XWORD(info));
1634 
1635 		} else if ((symndx >= info) && (bind == STB_LOCAL) &&
1636 		    ((sym->st_shndx != SHN_UNDEF) ||
1637 		    (ELF_ST_TYPE(sym->st_info) != STT_NOTYPE) ||
1638 		    (sym->st_size != 0) || (sym->st_value != 0))) {
1639 			(void) fprintf(stderr,
1640 			    MSG_INTL(MSG_ERR_BADSYM8), state->file,
1641 			    state->secname, demangle(symname, state->flags),
1642 			    EC_XWORD(info));
1643 		}
1644 	}
1645 
1646 	(void) snprintf(index, MAXNDXSIZE,
1647 	    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(disp_symndx));
1648 	Elf_syms_table_entry(0, ELF_DBG_ELFDUMP, index,
1649 	    state->ehdr->e_machine, sym, verndx, gnuver, sec, symname);
1650 }
1651 
1652 /*
1653  * Search for and process any symbol tables.
1654  */
1655 void
1656 symbols(Cache *cache, Word shnum, Ehdr *ehdr, VERSYM_STATE *versym,
1657     const char *file, uint_t flags)
1658 {
1659 	SYMTBL_STATE state;
1660 	Cache *_cache;
1661 	Word secndx;
1662 
1663 	for (secndx = 1; secndx < shnum; secndx++) {
1664 		Word		symcnt;
1665 		Shdr		*shdr;
1666 
1667 		_cache = &cache[secndx];
1668 		shdr = _cache->c_shdr;
1669 
1670 		if ((shdr->sh_type != SHT_SYMTAB) &&
1671 		    (shdr->sh_type != SHT_DYNSYM) &&
1672 		    (shdr->sh_type != SHT_SUNW_LDYNSYM))
1673 			continue;
1674 		if (!match(0, _cache->c_name, secndx))
1675 			continue;
1676 
1677 		if (!init_symtbl_state(&state, cache, shnum, secndx, ehdr,
1678 		    versym, file, flags))
1679 			continue;
1680 		/*
1681 		 * Loop through the symbol tables entries.
1682 		 */
1683 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1684 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMTAB), state.secname);
1685 		Elf_syms_table_title(0, ELF_DBG_ELFDUMP);
1686 
1687 		for (symcnt = 0; symcnt < state.symn; symcnt++)
1688 			output_symbol(&state, symcnt, shdr->sh_info, symcnt,
1689 			    state.sym + symcnt);
1690 	}
1691 }
1692 
1693 /*
1694  * Search for and process any SHT_SUNW_symsort or SHT_SUNW_tlssort sections.
1695  * These sections are always associated with the .SUNW_ldynsym./.dynsym pair.
1696  */
1697 static void
1698 sunw_sort(Cache *cache, Word shnum, Ehdr *ehdr, VERSYM_STATE *versym,
1699     const char *file, uint_t flags)
1700 {
1701 	SYMTBL_STATE	ldynsym_state,	dynsym_state;
1702 	Cache		*sortcache,	*symcache;
1703 	Shdr		*sortshdr,	*symshdr;
1704 	Word		sortsecndx,	symsecndx;
1705 	Word		ldynsym_cnt;
1706 	Word		*ndx;
1707 	Word		ndxn;
1708 	int		output_cnt = 0;
1709 	Conv_inv_buf_t	inv_buf;
1710 
1711 	for (sortsecndx = 1; sortsecndx < shnum; sortsecndx++) {
1712 
1713 		sortcache = &cache[sortsecndx];
1714 		sortshdr = sortcache->c_shdr;
1715 
1716 		if ((sortshdr->sh_type != SHT_SUNW_symsort) &&
1717 		    (sortshdr->sh_type != SHT_SUNW_tlssort))
1718 			continue;
1719 		if (!match(0, sortcache->c_name, sortsecndx))
1720 			continue;
1721 
1722 		/*
1723 		 * If the section references a SUNW_ldynsym, then we
1724 		 * expect to see the associated .dynsym immediately
1725 		 * following. If it references a .dynsym, there is no
1726 		 * SUNW_ldynsym. If it is any other type, then we don't
1727 		 * know what to do with it.
1728 		 */
1729 		if ((sortshdr->sh_link == 0) || (sortshdr->sh_link >= shnum)) {
1730 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
1731 			    file, sortcache->c_name,
1732 			    EC_WORD(sortshdr->sh_link));
1733 			continue;
1734 		}
1735 		symcache = &cache[sortshdr->sh_link];
1736 		symshdr = symcache->c_shdr;
1737 		symsecndx = sortshdr->sh_link;
1738 		ldynsym_cnt = 0;
1739 		switch (symshdr->sh_type) {
1740 		case SHT_SUNW_LDYNSYM:
1741 			if (!init_symtbl_state(&ldynsym_state, cache, shnum,
1742 			    symsecndx, ehdr, versym, file, flags))
1743 				continue;
1744 			ldynsym_cnt = ldynsym_state.symn;
1745 			/*
1746 			 * We know that the dynsym follows immediately
1747 			 * after the SUNW_ldynsym, and so, should be at
1748 			 * (sortshdr->sh_link + 1). However, elfdump is a
1749 			 * diagnostic tool, so we do the full paranoid
1750 			 * search instead.
1751 			 */
1752 			for (symsecndx = 1; symsecndx < shnum; symsecndx++) {
1753 				symcache = &cache[symsecndx];
1754 				symshdr = symcache->c_shdr;
1755 				if (symshdr->sh_type == SHT_DYNSYM)
1756 					break;
1757 			}
1758 			if (symsecndx >= shnum) {	/* Dynsym not found! */
1759 				(void) fprintf(stderr,
1760 				    MSG_INTL(MSG_ERR_NODYNSYM),
1761 				    file, sortcache->c_name);
1762 				continue;
1763 			}
1764 			/* Fallthrough to process associated dynsym */
1765 			/*FALLTHROUGH*/
1766 		case SHT_DYNSYM:
1767 			if (!init_symtbl_state(&dynsym_state, cache, shnum,
1768 			    symsecndx, ehdr, versym, file, flags))
1769 				continue;
1770 			break;
1771 		default:
1772 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADNDXSEC),
1773 			    file, sortcache->c_name, conv_sec_type(
1774 			    ehdr->e_machine, symshdr->sh_type, 0, &inv_buf));
1775 			continue;
1776 		}
1777 
1778 		/*
1779 		 * Output header
1780 		 */
1781 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1782 		if (ldynsym_cnt > 0) {
1783 			dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMSORT2),
1784 			    sortcache->c_name, ldynsym_state.secname,
1785 			    dynsym_state.secname);
1786 			/*
1787 			 * The data for .SUNW_ldynsym and dynsym sections
1788 			 * is supposed to be adjacent with SUNW_ldynsym coming
1789 			 * first. Check, and issue a warning if it isn't so.
1790 			 */
1791 			if (((ldynsym_state.sym + ldynsym_state.symn)
1792 			    != dynsym_state.sym) &&
1793 			    ((flags & FLG_FAKESHDR) == 0))
1794 				(void) fprintf(stderr,
1795 				    MSG_INTL(MSG_ERR_LDYNNOTADJ), file,
1796 				    ldynsym_state.secname,
1797 				    dynsym_state.secname);
1798 		} else {
1799 			dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMSORT1),
1800 			    sortcache->c_name, dynsym_state.secname);
1801 		}
1802 		Elf_syms_table_title(0, ELF_DBG_ELFDUMP);
1803 
1804 		/* If not first one, insert a line of whitespace */
1805 		if (output_cnt++ > 0)
1806 			dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1807 
1808 		/*
1809 		 * SUNW_dynsymsort and SUNW_dyntlssort are arrays of
1810 		 * symbol indices. Iterate over the array entries,
1811 		 * dispaying the referenced symbols.
1812 		 */
1813 		ndxn = sortshdr->sh_size / sortshdr->sh_entsize;
1814 		ndx = (Word *)sortcache->c_data->d_buf;
1815 		for (; ndxn-- > 0; ndx++) {
1816 			if (*ndx >= ldynsym_cnt) {
1817 				Word sec_ndx = *ndx - ldynsym_cnt;
1818 
1819 				output_symbol(&dynsym_state, sec_ndx, 0,
1820 				    *ndx, dynsym_state.sym + sec_ndx);
1821 			} else {
1822 				output_symbol(&ldynsym_state, *ndx, 0,
1823 				    *ndx, ldynsym_state.sym + *ndx);
1824 			}
1825 		}
1826 	}
1827 }
1828 
1829 /*
1830  * Search for and process any relocation sections.
1831  */
1832 static void
1833 reloc(Cache *cache, Word shnum, Ehdr *ehdr, const char *file,
1834     uint_t flags)
1835 {
1836 	Word	cnt;
1837 
1838 	for (cnt = 1; cnt < shnum; cnt++) {
1839 		Word		type, symnum;
1840 		Xword		relndx, relnum, relsize;
1841 		void		*rels;
1842 		Sym		*syms;
1843 		Cache		*symsec, *strsec;
1844 		Cache		*_cache = &cache[cnt];
1845 		Shdr		*shdr = _cache->c_shdr;
1846 		char		*relname = _cache->c_name;
1847 		Conv_inv_buf_t	inv_buf;
1848 
1849 		if (((type = shdr->sh_type) != SHT_RELA) &&
1850 		    (type != SHT_REL))
1851 			continue;
1852 		if (!match(0, relname, cnt))
1853 			continue;
1854 
1855 		/*
1856 		 * Decide entry size.
1857 		 */
1858 		if (((relsize = shdr->sh_entsize) == 0) ||
1859 		    (relsize > shdr->sh_size)) {
1860 			if (type == SHT_RELA)
1861 				relsize = sizeof (Rela);
1862 			else
1863 				relsize = sizeof (Rel);
1864 		}
1865 
1866 		/*
1867 		 * Determine the number of relocations available.
1868 		 */
1869 		if (shdr->sh_size == 0) {
1870 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
1871 			    file, relname);
1872 			continue;
1873 		}
1874 		if (_cache->c_data == NULL)
1875 			continue;
1876 
1877 		rels = _cache->c_data->d_buf;
1878 		relnum = shdr->sh_size / relsize;
1879 
1880 		/*
1881 		 * Get the data buffer for the associated symbol table and
1882 		 * string table.
1883 		 */
1884 		if (stringtbl(cache, 1, cnt, shnum, file,
1885 		    &symnum, &symsec, &strsec) == 0)
1886 			continue;
1887 
1888 		syms = symsec->c_data->d_buf;
1889 
1890 		/*
1891 		 * Loop through the relocation entries.
1892 		 */
1893 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1894 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_RELOC), _cache->c_name);
1895 		Elf_reloc_title(0, ELF_DBG_ELFDUMP, type);
1896 
1897 		for (relndx = 0; relndx < relnum; relndx++,
1898 		    rels = (void *)((char *)rels + relsize)) {
1899 			char		section[BUFSIZ];
1900 			const char	*symname;
1901 			Word		symndx, reltype;
1902 			Rela		*rela;
1903 			Rel		*rel;
1904 
1905 			/*
1906 			 * Unravel the relocation and determine the symbol with
1907 			 * which this relocation is associated.
1908 			 */
1909 			if (type == SHT_RELA) {
1910 				rela = (Rela *)rels;
1911 				symndx = ELF_R_SYM(rela->r_info);
1912 				reltype = ELF_R_TYPE(rela->r_info);
1913 			} else {
1914 				rel = (Rel *)rels;
1915 				symndx = ELF_R_SYM(rel->r_info);
1916 				reltype = ELF_R_TYPE(rel->r_info);
1917 			}
1918 
1919 			symname = relsymname(cache, _cache, strsec, symndx,
1920 			    symnum, relndx, syms, section, BUFSIZ, file,
1921 			    flags);
1922 
1923 			/*
1924 			 * A zero symbol index is only valid for a few
1925 			 * relocations.
1926 			 */
1927 			if (symndx == 0) {
1928 				Half	mach = ehdr->e_machine;
1929 				int	badrel = 0;
1930 
1931 				if ((mach == EM_SPARC) ||
1932 				    (mach == EM_SPARC32PLUS) ||
1933 				    (mach == EM_SPARCV9)) {
1934 					if ((reltype != R_SPARC_NONE) &&
1935 					    (reltype != R_SPARC_REGISTER) &&
1936 					    (reltype != R_SPARC_RELATIVE))
1937 						badrel++;
1938 				} else if (mach == EM_386) {
1939 					if ((reltype != R_386_NONE) &&
1940 					    (reltype != R_386_RELATIVE))
1941 						badrel++;
1942 				} else if (mach == EM_AMD64) {
1943 					if ((reltype != R_AMD64_NONE) &&
1944 					    (reltype != R_AMD64_RELATIVE))
1945 						badrel++;
1946 				}
1947 
1948 				if (badrel) {
1949 					(void) fprintf(stderr,
1950 					    MSG_INTL(MSG_ERR_BADREL1), file,
1951 					    conv_reloc_type(mach, reltype,
1952 					    0, &inv_buf));
1953 				}
1954 			}
1955 
1956 			Elf_reloc_entry_1(0, ELF_DBG_ELFDUMP,
1957 			    MSG_ORIG(MSG_STR_EMPTY), ehdr->e_machine, type,
1958 			    rels, relname, symname, 0);
1959 		}
1960 	}
1961 }
1962 
1963 /*
1964  * Search for and process a .dynamic section.
1965  */
1966 static void
1967 dynamic(Cache *cache, Word shnum, Ehdr *ehdr, const char *file)
1968 {
1969 	Word	cnt;
1970 
1971 	for (cnt = 1; cnt < shnum; cnt++) {
1972 		Dyn	*dyn;
1973 		ulong_t	numdyn;
1974 		int	ndx, end_ndx;
1975 		Cache	*_cache = &cache[cnt], *strsec;
1976 		Shdr	*shdr = _cache->c_shdr;
1977 
1978 		if (shdr->sh_type != SHT_DYNAMIC)
1979 			continue;
1980 
1981 		/*
1982 		 * Verify the associated string table section.
1983 		 */
1984 		if (stringtbl(cache, 0, cnt, shnum, file, 0, 0, &strsec) == 0)
1985 			continue;
1986 
1987 		if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
1988 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
1989 			    file, _cache->c_name);
1990 			continue;
1991 		}
1992 		if (_cache->c_data == NULL)
1993 			continue;
1994 
1995 		numdyn = shdr->sh_size / shdr->sh_entsize;
1996 		dyn = (Dyn *)_cache->c_data->d_buf;
1997 
1998 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1999 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_DYNAMIC), _cache->c_name);
2000 
2001 		Elf_dyn_title(0);
2002 
2003 		for (ndx = 0; ndx < numdyn; dyn++, ndx++) {
2004 			union {
2005 				Conv_dyn_flag_buf_t	flag;
2006 				Conv_dyn_flag1_buf_t	flag1;
2007 				Conv_dyn_posflag1_buf_t	posflag1;
2008 				Conv_dyn_feature1_buf_t	feature1;
2009 			} c_buf;
2010 			const char	*name;
2011 
2012 			/*
2013 			 * Print the information numerically, and if possible
2014 			 * as a string.
2015 			 */
2016 			switch (dyn->d_tag) {
2017 			case DT_NULL:
2018 				/*
2019 				 * Special case: DT_NULLs can come in groups
2020 				 * that we prefer to reduce to a single line.
2021 				 */
2022 				end_ndx = ndx;
2023 				while ((end_ndx < (numdyn - 1)) &&
2024 				    ((dyn + 1)->d_tag == DT_NULL)) {
2025 					dyn++;
2026 					end_ndx++;
2027 				}
2028 				Elf_dyn_null_entry(0, dyn, ndx, end_ndx);
2029 				ndx = end_ndx;
2030 				continue;
2031 
2032 			/*
2033 			 * Print the information numerically, and if possible
2034 			 * as a string.
2035 			 */
2036 			case DT_NEEDED:
2037 			case DT_SONAME:
2038 			case DT_FILTER:
2039 			case DT_AUXILIARY:
2040 			case DT_CONFIG:
2041 			case DT_RPATH:
2042 			case DT_RUNPATH:
2043 			case DT_USED:
2044 			case DT_DEPAUDIT:
2045 			case DT_AUDIT:
2046 			case DT_SUNW_AUXILIARY:
2047 			case DT_SUNW_FILTER:
2048 				name = string(_cache, ndx, strsec,
2049 				    file, dyn->d_un.d_ptr);
2050 				break;
2051 
2052 			case DT_FLAGS:
2053 				name = conv_dyn_flag(dyn->d_un.d_val,
2054 				    0, &c_buf.flag);
2055 				break;
2056 			case DT_FLAGS_1:
2057 				name = conv_dyn_flag1(dyn->d_un.d_val, 0,
2058 				    &c_buf.flag1);
2059 				break;
2060 			case DT_POSFLAG_1:
2061 				name = conv_dyn_posflag1(dyn->d_un.d_val, 0,
2062 				    &c_buf.posflag1);
2063 				break;
2064 			case DT_FEATURE_1:
2065 				name = conv_dyn_feature1(dyn->d_un.d_val, 0,
2066 				    &c_buf.feature1);
2067 				break;
2068 			case DT_DEPRECATED_SPARC_REGISTER:
2069 				name = MSG_INTL(MSG_STR_DEPRECATED);
2070 				break;
2071 			default:
2072 				name = MSG_ORIG(MSG_STR_EMPTY);
2073 				break;
2074 			}
2075 
2076 			Elf_dyn_entry(0, dyn, ndx, name, ehdr->e_machine);
2077 		}
2078 	}
2079 }
2080 
2081 /*
2082  * Search for and process a MOVE section.
2083  */
2084 static void
2085 move(Cache *cache, Word shnum, const char *file, uint_t flags)
2086 {
2087 	Word		cnt;
2088 	const char	*fmt = 0;
2089 
2090 	for (cnt = 1; cnt < shnum; cnt++) {
2091 		Word	movenum, symnum, ndx;
2092 		Sym	*syms;
2093 		Cache	*_cache = &cache[cnt];
2094 		Shdr	*shdr = _cache->c_shdr;
2095 		Cache	*symsec, *strsec;
2096 		Move	*move;
2097 
2098 		if (shdr->sh_type != SHT_SUNW_move)
2099 			continue;
2100 		if (!match(0, _cache->c_name, cnt))
2101 			continue;
2102 
2103 		/*
2104 		 * Determine the move data and number.
2105 		 */
2106 		if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
2107 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
2108 			    file, _cache->c_name);
2109 			continue;
2110 		}
2111 		if (_cache->c_data == NULL)
2112 			continue;
2113 
2114 		move = (Move *)_cache->c_data->d_buf;
2115 		movenum = shdr->sh_size / shdr->sh_entsize;
2116 
2117 		/*
2118 		 * Get the data buffer for the associated symbol table and
2119 		 * string table.
2120 		 */
2121 		if (stringtbl(cache, 1, cnt, shnum, file,
2122 		    &symnum, &symsec, &strsec) == 0)
2123 			return;
2124 
2125 		syms = (Sym *)symsec->c_data->d_buf;
2126 
2127 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2128 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_MOVE), _cache->c_name);
2129 		dbg_print(0, MSG_INTL(MSG_MOVE_TITLE));
2130 
2131 		if (fmt == 0)
2132 			fmt = MSG_INTL(MSG_MOVE_ENTRY);
2133 
2134 		for (ndx = 0; ndx < movenum; move++, ndx++) {
2135 			const char	*symname;
2136 			char		index[MAXNDXSIZE], section[BUFSIZ];
2137 			Word		symndx, shndx;
2138 			Sym		*sym;
2139 
2140 			/*
2141 			 * Check for null entries
2142 			 */
2143 			if ((move->m_info == 0) && (move->m_value == 0) &&
2144 			    (move->m_poffset == 0) && (move->m_repeat == 0) &&
2145 			    (move->m_stride == 0)) {
2146 				dbg_print(0, fmt, MSG_ORIG(MSG_STR_EMPTY),
2147 				    EC_XWORD(move->m_poffset), 0, 0, 0,
2148 				    EC_LWORD(0), MSG_ORIG(MSG_STR_EMPTY));
2149 				continue;
2150 			}
2151 			if (((symndx = ELF_M_SYM(move->m_info)) == 0) ||
2152 			    (symndx >= symnum)) {
2153 				(void) fprintf(stderr,
2154 				    MSG_INTL(MSG_ERR_BADMINFO), file,
2155 				    _cache->c_name, EC_XWORD(move->m_info));
2156 
2157 				(void) snprintf(index, MAXNDXSIZE,
2158 				    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(symndx));
2159 				dbg_print(0, fmt, index,
2160 				    EC_XWORD(move->m_poffset),
2161 				    ELF_M_SIZE(move->m_info), move->m_repeat,
2162 				    move->m_stride, move->m_value,
2163 				    MSG_INTL(MSG_STR_UNKNOWN));
2164 				continue;
2165 			}
2166 
2167 			symname = relsymname(cache, _cache, strsec,
2168 			    symndx, symnum, ndx, syms, section, BUFSIZ, file,
2169 			    flags);
2170 			sym = (Sym *)(syms + symndx);
2171 
2172 			/*
2173 			 * Additional sanity check.
2174 			 */
2175 			shndx = sym->st_shndx;
2176 			if (!((shndx == SHN_COMMON) ||
2177 			    (((shndx >= 1) && (shndx <= shnum)) &&
2178 			    (cache[shndx].c_shdr)->sh_type == SHT_NOBITS))) {
2179 				(void) fprintf(stderr,
2180 				    MSG_INTL(MSG_ERR_BADSYM2), file,
2181 				    _cache->c_name, demangle(symname, flags));
2182 			}
2183 
2184 			(void) snprintf(index, MAXNDXSIZE,
2185 			    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(symndx));
2186 			dbg_print(0, fmt, index, EC_XWORD(move->m_poffset),
2187 			    ELF_M_SIZE(move->m_info), move->m_repeat,
2188 			    move->m_stride, move->m_value,
2189 			    demangle(symname, flags));
2190 		}
2191 	}
2192 }
2193 
2194 /*
2195  * Traverse a note section analyzing each note information block.
2196  * The data buffers size is used to validate references before they are made,
2197  * and is decremented as each element is processed.
2198  */
2199 void
2200 note_entry(Cache *cache, Word *data, size_t size, const char *file)
2201 {
2202 	size_t	bsize = size;
2203 
2204 	/*
2205 	 * Print out a single `note' information block.
2206 	 */
2207 	while (size > 0) {
2208 		size_t	namesz, descsz, type, pad, noteoff;
2209 
2210 		noteoff = bsize - size;
2211 		/*
2212 		 * Make sure we can at least reference the 3 initial entries
2213 		 * (4-byte words) of the note information block.
2214 		 */
2215 		if (size >= (sizeof (Word) * 3))
2216 			size -= (sizeof (Word) * 3);
2217 		else {
2218 			(void) fprintf(stderr, MSG_INTL(MSG_NOTE_BADDATASZ),
2219 			    file, cache->c_name, EC_WORD(noteoff));
2220 			return;
2221 		}
2222 
2223 		/*
2224 		 * Make sure any specified name string can be referenced.
2225 		 */
2226 		if ((namesz = *data++) != 0) {
2227 			if (size >= namesz)
2228 				size -= namesz;
2229 			else {
2230 				(void) fprintf(stderr,
2231 				    MSG_INTL(MSG_NOTE_BADNMSZ), file,
2232 				    cache->c_name, EC_WORD(noteoff),
2233 				    EC_WORD(namesz));
2234 				return;
2235 			}
2236 		}
2237 
2238 		/*
2239 		 * Make sure any specified descriptor can be referenced.
2240 		 */
2241 		if ((descsz = *data++) != 0) {
2242 			/*
2243 			 * If namesz isn't a 4-byte multiple, account for any
2244 			 * padding that must exist before the descriptor.
2245 			 */
2246 			if ((pad = (namesz & (sizeof (Word) - 1))) != 0) {
2247 				pad = sizeof (Word) - pad;
2248 				size -= pad;
2249 			}
2250 			if (size >= descsz)
2251 				size -= descsz;
2252 			else {
2253 				(void) fprintf(stderr,
2254 				    MSG_INTL(MSG_NOTE_BADDESZ), file,
2255 				    cache->c_name, EC_WORD(noteoff),
2256 				    EC_WORD(namesz));
2257 				return;
2258 			}
2259 		}
2260 
2261 		type = *data++;
2262 
2263 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2264 		dbg_print(0, MSG_ORIG(MSG_NOTE_TYPE), EC_WORD(type));
2265 
2266 		dbg_print(0, MSG_ORIG(MSG_NOTE_NAMESZ), EC_WORD(namesz));
2267 		if (namesz) {
2268 			char	*name = (char *)data;
2269 
2270 			/*
2271 			 * Since the name string may have 'null' bytes
2272 			 * in it (ia32 .string) - we just write the
2273 			 * whole stream in a single fwrite.
2274 			 */
2275 			(void) fwrite(name, namesz, 1, stdout);
2276 			name = name + ((namesz + (sizeof (Word) - 1)) &
2277 			    ~(sizeof (Word) - 1));
2278 			/* LINTED */
2279 			data = (Word *)name;
2280 			dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2281 		}
2282 
2283 		/*
2284 		 * If multiple information blocks exist within a .note section
2285 		 * account for any padding that must exist before the next
2286 		 * information block.
2287 		 */
2288 		if ((pad = (descsz & (sizeof (Word) - 1))) != 0) {
2289 			pad = sizeof (Word) - pad;
2290 			if (size > pad)
2291 				size -= pad;
2292 		}
2293 
2294 		dbg_print(0, MSG_ORIG(MSG_NOTE_DESCSZ), EC_WORD(descsz));
2295 		if (descsz) {
2296 			int		ndx, byte, word;
2297 			char		string[58], *str = string;
2298 			uchar_t		*desc = (uchar_t *)data;
2299 
2300 			/*
2301 			 * Dump descriptor bytes.
2302 			 */
2303 			for (ndx = byte = word = 0; descsz; descsz--, desc++) {
2304 				int	tok = *desc;
2305 
2306 				(void) snprintf(str, 58, MSG_ORIG(MSG_NOTE_TOK),
2307 				    tok);
2308 				str += 3;
2309 
2310 				if (++byte == 4) {
2311 					*str++ = ' ', *str++ = ' ';
2312 					word++;
2313 					byte = 0;
2314 				}
2315 				if (word == 4) {
2316 					*str = '\0';
2317 					dbg_print(0, MSG_ORIG(MSG_NOTE_DESC),
2318 					    ndx, string);
2319 					word = 0;
2320 					ndx += 16;
2321 					str = string;
2322 				}
2323 			}
2324 			if (byte || word) {
2325 				*str = '\0';
2326 				dbg_print(0, MSG_ORIG(MSG_NOTE_DESC),
2327 				    ndx, string);
2328 			}
2329 
2330 			desc += pad;
2331 			/* LINTED */
2332 			data = (Word *)desc;
2333 		}
2334 	}
2335 }
2336 
2337 /*
2338  * Search for and process a .note section.
2339  */
2340 static void
2341 note(Cache *cache, Word shnum, const char *file)
2342 {
2343 	Word	cnt;
2344 
2345 	/*
2346 	 * Otherwise look for any .note sections.
2347 	 */
2348 	for (cnt = 1; cnt < shnum; cnt++) {
2349 		Cache	*_cache = &cache[cnt];
2350 		Shdr	*shdr = _cache->c_shdr;
2351 
2352 		if (shdr->sh_type != SHT_NOTE)
2353 			continue;
2354 		if (!match(0, _cache->c_name, cnt))
2355 			continue;
2356 
2357 		/*
2358 		 * As these sections are often hand rolled, make sure they're
2359 		 * properly aligned before proceeding.
2360 		 */
2361 		if (shdr->sh_offset & (sizeof (Word) - 1)) {
2362 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADALIGN),
2363 			    file, _cache->c_name);
2364 			continue;
2365 		}
2366 		if (_cache->c_data == NULL)
2367 			continue;
2368 
2369 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2370 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_NOTE), _cache->c_name);
2371 		note_entry(_cache, (Word *)_cache->c_data->d_buf,
2372 		/* LINTED */
2373 		    (Word)_cache->c_data->d_size, file);
2374 	}
2375 }
2376 
2377 /*
2378  * Determine an individual hash entry.  This may be the initial hash entry,
2379  * or an associated chain entry.
2380  */
2381 static void
2382 hash_entry(Cache *refsec, Cache *strsec, const char *hsecname, Word hashndx,
2383     Word symndx, Word symn, Sym *syms, const char *file, ulong_t bkts,
2384     uint_t flags, int chain)
2385 {
2386 	Sym		*sym;
2387 	const char	*symname, *str;
2388 	char		_bucket[MAXNDXSIZE], _symndx[MAXNDXSIZE];
2389 	ulong_t		nbkt, nhash;
2390 
2391 	if (symndx > symn) {
2392 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_HSBADSYMNDX), file,
2393 		    EC_WORD(symndx), EC_WORD(hashndx));
2394 		symname = MSG_INTL(MSG_STR_UNKNOWN);
2395 	} else {
2396 		sym = (Sym *)(syms + symndx);
2397 		symname = string(refsec, symndx, strsec, file, sym->st_name);
2398 	}
2399 
2400 	if (chain == 0) {
2401 		(void) snprintf(_bucket, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INTEGER),
2402 		    hashndx);
2403 		str = (const char *)_bucket;
2404 	} else
2405 		str = MSG_ORIG(MSG_STR_EMPTY);
2406 
2407 	(void) snprintf(_symndx, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INDEX2),
2408 	    EC_WORD(symndx));
2409 	dbg_print(0, MSG_ORIG(MSG_FMT_HASH_INFO), str, _symndx,
2410 	    demangle(symname, flags));
2411 
2412 	/*
2413 	 * Determine if this string is in the correct bucket.
2414 	 */
2415 	nhash = elf_hash(symname);
2416 	nbkt = nhash % bkts;
2417 
2418 	if (nbkt != hashndx) {
2419 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADHASH), file,
2420 		    hsecname, symname, EC_WORD(hashndx), nbkt);
2421 	}
2422 }
2423 
2424 #define	MAXCOUNT	500
2425 
2426 static void
2427 hash(Cache *cache, Word shnum, const char *file, uint_t flags)
2428 {
2429 	static int	count[MAXCOUNT];
2430 	Word		cnt;
2431 	ulong_t		ndx, bkts;
2432 	char		number[MAXNDXSIZE];
2433 
2434 	for (cnt = 1; cnt < shnum; cnt++) {
2435 		uint_t		*hash, *chain;
2436 		Cache		*_cache = &cache[cnt];
2437 		Shdr		*sshdr, *hshdr = _cache->c_shdr;
2438 		char		*ssecname, *hsecname = _cache->c_name;
2439 		Sym		*syms;
2440 		Word		symn;
2441 
2442 		if (hshdr->sh_type != SHT_HASH)
2443 			continue;
2444 
2445 		/*
2446 		 * Determine the hash table data and size.
2447 		 */
2448 		if ((hshdr->sh_entsize == 0) || (hshdr->sh_size == 0)) {
2449 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
2450 			    file, hsecname);
2451 			continue;
2452 		}
2453 		if (_cache->c_data == NULL)
2454 			continue;
2455 
2456 		hash = (uint_t *)_cache->c_data->d_buf;
2457 		bkts = *hash;
2458 		chain = hash + 2 + bkts;
2459 		hash += 2;
2460 
2461 		/*
2462 		 * Get the data buffer for the associated symbol table.
2463 		 */
2464 		if ((hshdr->sh_link == 0) || (hshdr->sh_link >= shnum)) {
2465 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
2466 			    file, hsecname, EC_WORD(hshdr->sh_link));
2467 			continue;
2468 		}
2469 
2470 		_cache = &cache[hshdr->sh_link];
2471 		ssecname = _cache->c_name;
2472 
2473 		if (_cache->c_data == NULL)
2474 			continue;
2475 
2476 		if ((syms = (Sym *)_cache->c_data->d_buf) == NULL) {
2477 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
2478 			    file, ssecname);
2479 			continue;
2480 		}
2481 
2482 		sshdr = _cache->c_shdr;
2483 		/* LINTED */
2484 		symn = (Word)(sshdr->sh_size / sshdr->sh_entsize);
2485 
2486 		/*
2487 		 * Get the associated string table section.
2488 		 */
2489 		if ((sshdr->sh_link == 0) || (sshdr->sh_link >= shnum)) {
2490 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
2491 			    file, ssecname, EC_WORD(sshdr->sh_link));
2492 			continue;
2493 		}
2494 
2495 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2496 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_HASH), hsecname);
2497 		dbg_print(0, MSG_INTL(MSG_ELF_HASH_INFO));
2498 
2499 		/*
2500 		 * Loop through the hash buckets, printing the appropriate
2501 		 * symbols.
2502 		 */
2503 		for (ndx = 0; ndx < bkts; ndx++, hash++) {
2504 			Word	_ndx, _cnt;
2505 
2506 			if (*hash == 0) {
2507 				count[0]++;
2508 				continue;
2509 			}
2510 
2511 			hash_entry(_cache, &cache[sshdr->sh_link], hsecname,
2512 			    ndx, *hash, symn, syms, file, bkts, flags, 0);
2513 
2514 			/*
2515 			 * Determine if any other symbols are chained to this
2516 			 * bucket.
2517 			 */
2518 			_ndx = chain[*hash];
2519 			_cnt = 1;
2520 			while (_ndx) {
2521 				hash_entry(_cache, &cache[sshdr->sh_link],
2522 				    hsecname, ndx, _ndx, symn, syms, file,
2523 				    bkts, flags, 1);
2524 				_ndx = chain[_ndx];
2525 				_cnt++;
2526 			}
2527 
2528 			if (_cnt >= MAXCOUNT) {
2529 				(void) fprintf(stderr,
2530 				    MSG_INTL(MSG_HASH_OVERFLW), file,
2531 				    _cache->c_name, EC_WORD(ndx),
2532 				    EC_WORD(_cnt));
2533 			} else
2534 				count[_cnt]++;
2535 		}
2536 		break;
2537 	}
2538 
2539 	/*
2540 	 * Print out the count information.
2541 	 */
2542 	bkts = cnt = 0;
2543 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2544 
2545 	for (ndx = 0; ndx < MAXCOUNT; ndx++) {
2546 		Word	_cnt;
2547 
2548 		if ((_cnt = count[ndx]) == 0)
2549 			continue;
2550 
2551 		(void) snprintf(number, MAXNDXSIZE,
2552 		    MSG_ORIG(MSG_FMT_INTEGER), _cnt);
2553 		dbg_print(0, MSG_INTL(MSG_ELF_HASH_BKTS1), number,
2554 		    EC_WORD(ndx));
2555 		bkts += _cnt;
2556 		cnt += (Word)(ndx * _cnt);
2557 	}
2558 	if (cnt) {
2559 		(void) snprintf(number, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INTEGER),
2560 		    bkts);
2561 		dbg_print(0, MSG_INTL(MSG_ELF_HASH_BKTS2), number,
2562 		    EC_WORD(cnt));
2563 	}
2564 }
2565 
2566 static void
2567 group(Cache *cache, Word shnum, const char *file, uint_t flags)
2568 {
2569 	Word	scnt;
2570 
2571 	for (scnt = 1; scnt < shnum; scnt++) {
2572 		Cache	*_cache = &cache[scnt];
2573 		Shdr	*shdr = _cache->c_shdr;
2574 		Word	*grpdata, gcnt, grpcnt, symnum, unknown;
2575 		Cache	*symsec, *strsec;
2576 		Sym	*syms, *sym;
2577 		char	flgstrbuf[MSG_GRP_COMDAT_SIZE + 10];
2578 
2579 		if (shdr->sh_type != SHT_GROUP)
2580 			continue;
2581 		if (!match(0, _cache->c_name, scnt))
2582 			continue;
2583 		if ((_cache->c_data == NULL) ||
2584 		    ((grpdata = (Word *)_cache->c_data->d_buf) == NULL))
2585 			continue;
2586 		grpcnt = shdr->sh_size / sizeof (Word);
2587 
2588 		/*
2589 		 * Get the data buffer for the associated symbol table and
2590 		 * string table.
2591 		 */
2592 		if (stringtbl(cache, 1, scnt, shnum, file,
2593 		    &symnum, &symsec, &strsec) == 0)
2594 			return;
2595 
2596 		syms = symsec->c_data->d_buf;
2597 
2598 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2599 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_GRP), _cache->c_name);
2600 		dbg_print(0, MSG_INTL(MSG_GRP_TITLE));
2601 
2602 		/*
2603 		 * The first element of the group defines the group.  The
2604 		 * associated symbol is defined by the sh_link field.
2605 		 */
2606 		if ((shdr->sh_info == SHN_UNDEF) || (shdr->sh_info > symnum)) {
2607 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHINFO),
2608 			    file, _cache->c_name, EC_WORD(shdr->sh_info));
2609 			return;
2610 		}
2611 
2612 		(void) strcpy(flgstrbuf, MSG_ORIG(MSG_STR_OSQBRKT));
2613 		if (grpdata[0] & GRP_COMDAT) {
2614 			(void) strcat(flgstrbuf, MSG_ORIG(MSG_GRP_COMDAT));
2615 		}
2616 		if ((unknown = (grpdata[0] & ~GRP_COMDAT)) != 0) {
2617 			size_t	len = strlen(flgstrbuf);
2618 
2619 			(void) snprintf(&flgstrbuf[len],
2620 			    (MSG_GRP_COMDAT_SIZE + 10 - len),
2621 			    MSG_ORIG(MSG_GRP_UNKNOWN), unknown);
2622 		}
2623 		(void) strcat(flgstrbuf, MSG_ORIG(MSG_STR_CSQBRKT));
2624 		sym = (Sym *)(syms + shdr->sh_info);
2625 
2626 		dbg_print(0, MSG_INTL(MSG_GRP_SIGNATURE), flgstrbuf,
2627 		    demangle(string(_cache, 0, strsec, file, sym->st_name),
2628 		    flags));
2629 
2630 		for (gcnt = 1; gcnt < grpcnt; gcnt++) {
2631 			char		index[MAXNDXSIZE];
2632 			const char	*name;
2633 
2634 			(void) snprintf(index, MAXNDXSIZE,
2635 			    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(gcnt));
2636 
2637 			if (grpdata[gcnt] >= shnum)
2638 				name = MSG_INTL(MSG_GRP_INVALSCN);
2639 			else
2640 				name = cache[grpdata[gcnt]].c_name;
2641 
2642 			(void) printf(MSG_ORIG(MSG_GRP_ENTRY), index, name,
2643 			    EC_XWORD(grpdata[gcnt]));
2644 		}
2645 	}
2646 }
2647 
2648 static void
2649 got(Cache *cache, Word shnum, Ehdr *ehdr, const char *file, uint_t flags)
2650 {
2651 	Cache		*gotcache = 0, *symtab = 0, *_cache;
2652 	Addr		gotbgn, gotend;
2653 	Shdr		*gotshdr;
2654 	Word		cnt, gotents, gotndx;
2655 	size_t		gentsize;
2656 	Got_info	*gottable;
2657 	char		*gotdata;
2658 	Sym		*gotsym;
2659 	Xword		gotsymaddr;
2660 
2661 	/*
2662 	 * First, find the got.
2663 	 */
2664 	for (cnt = 1; cnt < shnum; cnt++) {
2665 		_cache = &cache[cnt];
2666 		if (strncmp(_cache->c_name, MSG_ORIG(MSG_ELF_GOT),
2667 		    MSG_ELF_GOT_SIZE) == 0) {
2668 			gotcache = _cache;
2669 			break;
2670 		}
2671 	}
2672 	if (gotcache == 0)
2673 		return;
2674 
2675 	/*
2676 	 * A got section within a relocatable object is suspicious.
2677 	 */
2678 	if (ehdr->e_type == ET_REL) {
2679 		(void) fprintf(stderr, MSG_INTL(MSG_GOT_UNEXPECTED), file,
2680 		    _cache->c_name);
2681 	}
2682 
2683 	gotshdr = gotcache->c_shdr;
2684 	if (gotshdr->sh_size == 0) {
2685 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
2686 		    file, gotcache->c_name);
2687 		return;
2688 	}
2689 
2690 	gotbgn = gotshdr->sh_addr;
2691 	gotend = gotbgn + gotshdr->sh_size;
2692 
2693 	/*
2694 	 * Some architectures don't properly set the sh_entsize for the GOT
2695 	 * table.  If it's not set, default to a size of a pointer.
2696 	 */
2697 	if ((gentsize = gotshdr->sh_entsize) == 0)
2698 		gentsize = sizeof (Xword);
2699 
2700 	if (gotcache->c_data == NULL)
2701 		return;
2702 
2703 	/* LINTED */
2704 	gotents = (Word)(gotshdr->sh_size / gentsize);
2705 	gotdata = gotcache->c_data->d_buf;
2706 
2707 	if ((gottable = calloc(gotents, sizeof (Got_info))) == 0) {
2708 		int err = errno;
2709 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC), file,
2710 		    strerror(err));
2711 		return;
2712 	}
2713 
2714 	/*
2715 	 * Now we scan through all the sections looking for any relocations
2716 	 * that may be against the GOT.  Since these may not be isolated to a
2717 	 * .rel[a].got section we check them all.
2718 	 * While scanning sections save the symbol table entry (a symtab
2719 	 * overriding a dynsym) so that we can lookup _GLOBAL_OFFSET_TABLE_.
2720 	 */
2721 	for (cnt = 1; cnt < shnum; cnt++) {
2722 		Word		type, symnum;
2723 		Xword		relndx, relnum, relsize;
2724 		void		*rels;
2725 		Sym		*syms;
2726 		Cache		*symsec, *strsec;
2727 		Cache		*_cache = &cache[cnt];
2728 		Shdr		*shdr;
2729 
2730 		shdr = _cache->c_shdr;
2731 		type = shdr->sh_type;
2732 
2733 		if ((symtab == 0) && (type == SHT_DYNSYM)) {
2734 			symtab = _cache;
2735 			continue;
2736 		}
2737 		if (type == SHT_SYMTAB) {
2738 			symtab = _cache;
2739 			continue;
2740 		}
2741 		if ((type != SHT_RELA) && (type != SHT_REL))
2742 			continue;
2743 
2744 		/*
2745 		 * Decide entry size.
2746 		 */
2747 		if (((relsize = shdr->sh_entsize) == 0) ||
2748 		    (relsize > shdr->sh_size)) {
2749 			if (type == SHT_RELA)
2750 				relsize = sizeof (Rela);
2751 			else
2752 				relsize = sizeof (Rel);
2753 		}
2754 
2755 		/*
2756 		 * Determine the number of relocations available.
2757 		 */
2758 		if (shdr->sh_size == 0) {
2759 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
2760 			    file, _cache->c_name);
2761 			continue;
2762 		}
2763 		if (_cache->c_data == NULL)
2764 			continue;
2765 
2766 		rels = _cache->c_data->d_buf;
2767 		relnum = shdr->sh_size / relsize;
2768 
2769 		/*
2770 		 * Get the data buffer for the associated symbol table and
2771 		 * string table.
2772 		 */
2773 		if (stringtbl(cache, 1, cnt, shnum, file,
2774 		    &symnum, &symsec, &strsec) == 0)
2775 			continue;
2776 
2777 		syms = symsec->c_data->d_buf;
2778 
2779 		/*
2780 		 * Loop through the relocation entries.
2781 		 */
2782 		for (relndx = 0; relndx < relnum; relndx++,
2783 		    rels = (void *)((char *)rels + relsize)) {
2784 			char		section[BUFSIZ];
2785 			Addr		offset;
2786 			Got_info	*gip;
2787 			Word		symndx, reltype;
2788 			Rela		*rela;
2789 			Rel		*rel;
2790 
2791 			/*
2792 			 * Unravel the relocation.
2793 			 */
2794 			if (type == SHT_RELA) {
2795 				rela = (Rela *)rels;
2796 				symndx = ELF_R_SYM(rela->r_info);
2797 				reltype = ELF_R_TYPE(rela->r_info);
2798 				offset = rela->r_offset;
2799 			} else {
2800 				rel = (Rel *)rels;
2801 				symndx = ELF_R_SYM(rel->r_info);
2802 				reltype = ELF_R_TYPE(rel->r_info);
2803 				offset = rel->r_offset;
2804 			}
2805 
2806 			/*
2807 			 * Only pay attention to relocations against the GOT.
2808 			 */
2809 			if ((offset < gotbgn) || (offset >= gotend))
2810 				continue;
2811 
2812 			/* LINTED */
2813 			gotndx = (Word)((offset - gotbgn) /
2814 			    gotshdr->sh_entsize);
2815 			gip = &gottable[gotndx];
2816 
2817 			if (gip->g_reltype != 0) {
2818 				(void) fprintf(stderr,
2819 				    MSG_INTL(MSG_GOT_MULTIPLE), file,
2820 				    EC_WORD(gotndx), EC_ADDR(offset));
2821 				continue;
2822 			}
2823 
2824 			if (symndx)
2825 				gip->g_symname = relsymname(cache, _cache,
2826 				    strsec, symndx, symnum, relndx, syms,
2827 				    section, BUFSIZ, file, flags);
2828 			gip->g_reltype = reltype;
2829 			gip->g_rel = rels;
2830 		}
2831 	}
2832 
2833 	if (symlookup(MSG_ORIG(MSG_GOT_SYM), cache, shnum, &gotsym, symtab,
2834 	    file))
2835 		gotsymaddr = gotsym->st_value;
2836 	else
2837 		gotsymaddr = gotbgn;
2838 
2839 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2840 	dbg_print(0, MSG_INTL(MSG_ELF_SCN_GOT), gotcache->c_name);
2841 	Elf_got_title(0);
2842 
2843 	for (gotndx = 0; gotndx < gotents; gotndx++) {
2844 		Got_info	*gip;
2845 		Sword		gindex;
2846 		Addr		gaddr;
2847 		Xword		gotentry;
2848 
2849 		gip = &gottable[gotndx];
2850 
2851 		gaddr = gotbgn + (gotndx * gentsize);
2852 		gindex = (Sword)(gaddr - gotsymaddr) / (Sword)gentsize;
2853 
2854 		if (gentsize == sizeof (Word))
2855 			/* LINTED */
2856 			gotentry = (Xword)(*((Word *)(gotdata) + gotndx));
2857 		else
2858 			/* LINTED */
2859 			gotentry = *((Xword *)(gotdata) + gotndx);
2860 
2861 		Elf_got_entry(0, gindex, gaddr, gotentry, ehdr->e_machine,
2862 		    gip->g_reltype, gip->g_rel, gip->g_symname);
2863 	}
2864 	free(gottable);
2865 }
2866 
2867 void
2868 checksum(Elf *elf)
2869 {
2870 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2871 	dbg_print(0, MSG_INTL(MSG_STR_CHECKSUM), elf_checksum(elf));
2872 }
2873 
2874 /*
2875  * This variable is used by regular() to communicate the address of
2876  * the section header cache to sort_shdr_ndx_arr(). Unfortunately,
2877  * the qsort() interface does not include a userdata argument by which
2878  * such arbitrary data can be passed, so we are stuck using global data.
2879  */
2880 static Cache *sort_shdr_ndx_arr_cache;
2881 
2882 
2883 /*
2884  * Used with qsort() to sort the section indices so that they can be
2885  * used to access the section headers in order of increasing data offset.
2886  *
2887  * entry:
2888  *	sort_shdr_ndx_arr_cache - Contains address of
2889  *		section header cache.
2890  *	v1, v2 - Point at elements of sort_shdr_bits array to be compared.
2891  *
2892  * exit:
2893  *	Returns -1 (less than), 0 (equal) or 1 (greater than).
2894  */
2895 static int
2896 sort_shdr_ndx_arr(const void *v1, const void *v2)
2897 {
2898 	Cache	*cache1 = sort_shdr_ndx_arr_cache + *((size_t *)v1);
2899 	Cache	*cache2 = sort_shdr_ndx_arr_cache + *((size_t *)v2);
2900 
2901 	if (cache1->c_shdr->sh_offset < cache2->c_shdr->sh_offset)
2902 		return (-1);
2903 
2904 	if (cache1->c_shdr->sh_offset > cache2->c_shdr->sh_offset)
2905 		return (1);
2906 
2907 	return (0);
2908 }
2909 
2910 
2911 static int
2912 shdr_cache(const char *file, Elf *elf, Ehdr *ehdr, size_t shstrndx,
2913     size_t shnum, Cache **cache_ret)
2914 {
2915 	Elf_Scn		*scn;
2916 	Elf_Data	*data;
2917 	size_t		ndx;
2918 	Shdr		*nameshdr;
2919 	char		*names = 0;
2920 	Cache		*cache, *_cache;
2921 	size_t		*shdr_ndx_arr, shdr_ndx_arr_cnt;
2922 
2923 
2924 	/*
2925 	 * Obtain the .shstrtab data buffer to provide the required section
2926 	 * name strings.
2927 	 */
2928 	if (shstrndx == SHN_UNDEF) {
2929 		/*
2930 		 * It is rare, but legal, for an object to lack a
2931 		 * header string table section.
2932 		 */
2933 		names = NULL;
2934 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_NOSHSTRSEC), file);
2935 	} else if ((scn = elf_getscn(elf, shstrndx)) == NULL) {
2936 		failure(file, MSG_ORIG(MSG_ELF_GETSCN));
2937 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SHDR),
2938 		    EC_XWORD(shstrndx));
2939 
2940 	} else if ((data = elf_getdata(scn, NULL)) == NULL) {
2941 		failure(file, MSG_ORIG(MSG_ELF_GETDATA));
2942 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_DATA),
2943 		    EC_XWORD(shstrndx));
2944 
2945 	} else if ((nameshdr = elf_getshdr(scn)) == NULL) {
2946 		failure(file, MSG_ORIG(MSG_ELF_GETSHDR));
2947 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN),
2948 		    EC_WORD(elf_ndxscn(scn)));
2949 
2950 	} else if ((names = data->d_buf) == 0)
2951 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_SHSTRNULL), file);
2952 
2953 	/*
2954 	 * Allocate a cache to maintain a descriptor for each section.
2955 	 */
2956 	if ((*cache_ret = cache = malloc(shnum * sizeof (Cache))) == NULL) {
2957 		int err = errno;
2958 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
2959 		    file, strerror(err));
2960 		return (0);
2961 	}
2962 
2963 	*cache = cache_init;
2964 	_cache = cache;
2965 	_cache++;
2966 
2967 	/*
2968 	 * Allocate an array that will hold the section index for
2969 	 * each section that has data in the ELF file:
2970 	 *
2971 	 *	- Is not a NOBITS section
2972 	 *	- Data has non-zero length
2973 	 *
2974 	 * Note that shnum is an upper bound on the size required. It
2975 	 * is likely that we won't use a few of these array elements.
2976 	 * Allocating a modest amount of extra memory in this case means
2977 	 * that we can avoid an extra loop to count the number of needed
2978 	 * items, and can fill this array immediately in the first loop
2979 	 * below.
2980 	 */
2981 	if ((shdr_ndx_arr = malloc(shnum * sizeof (*shdr_ndx_arr))) == NULL) {
2982 		int err = errno;
2983 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
2984 		    file, strerror(err));
2985 		return (0);
2986 	}
2987 	shdr_ndx_arr_cnt = 0;
2988 
2989 	/*
2990 	 * Traverse the sections of the file.  This gathering of data is
2991 	 * carried out in two passes.  First, the section headers are captured
2992 	 * and the section header names are evaluated.  A verification pass is
2993 	 * then carried out over the section information.  Files have been
2994 	 * known to exhibit overlapping (and hence erroneous) section header
2995 	 * information.
2996 	 *
2997 	 * Finally, the data for each section is obtained.  This processing is
2998 	 * carried out after section verification because should any section
2999 	 * header overlap occur, and a file needs translating (ie. xlate'ing
3000 	 * information from a non-native architecture file), then the process
3001 	 * of translation can corrupt the section header information.  Of
3002 	 * course, if there is any section overlap, the data related to the
3003 	 * sections is going to be compromised.  However, it is the translation
3004 	 * of this data that has caused problems with elfdump()'s ability to
3005 	 * extract the data.
3006 	 */
3007 	for (ndx = 1, scn = NULL; scn = elf_nextscn(elf, scn);
3008 	    ndx++, _cache++) {
3009 		char	scnndxnm[100];
3010 
3011 		_cache->c_ndx = ndx;
3012 		_cache->c_scn = scn;
3013 
3014 		if ((_cache->c_shdr = elf_getshdr(scn)) == NULL) {
3015 			failure(file, MSG_ORIG(MSG_ELF_GETSHDR));
3016 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN),
3017 			    EC_WORD(elf_ndxscn(scn)));
3018 		}
3019 
3020 		/*
3021 		 * If this section has data in the file, include it in
3022 		 * the array of sections to check for address overlap.
3023 		 */
3024 		if ((_cache->c_shdr->sh_size != 0) &&
3025 		    (_cache->c_shdr->sh_type != SHT_NOBITS))
3026 			shdr_ndx_arr[shdr_ndx_arr_cnt++] = ndx;
3027 
3028 		/*
3029 		 * If a shstrtab exists, assign the section name.
3030 		 */
3031 		if (names && _cache->c_shdr) {
3032 			if (_cache->c_shdr->sh_name &&
3033 			    /* LINTED */
3034 			    (nameshdr->sh_size > _cache->c_shdr->sh_name)) {
3035 				_cache->c_name =
3036 				    names + _cache->c_shdr->sh_name;
3037 				continue;
3038 			}
3039 
3040 			/*
3041 			 * Generate an error if the section name index is zero
3042 			 * or exceeds the shstrtab data.  Fall through to
3043 			 * fabricate a section name.
3044 			 */
3045 			if ((_cache->c_shdr->sh_name == 0) ||
3046 			    /* LINTED */
3047 			    (nameshdr->sh_size <= _cache->c_shdr->sh_name)) {
3048 				(void) fprintf(stderr,
3049 				    MSG_INTL(MSG_ERR_BADSHNAME), file,
3050 				    EC_WORD(ndx),
3051 				    EC_XWORD(_cache->c_shdr->sh_name));
3052 			}
3053 		}
3054 
3055 		/*
3056 		 * If there exists no shstrtab data, or a section header has no
3057 		 * name (an invalid index of 0), then compose a name for the
3058 		 * section.
3059 		 */
3060 		(void) snprintf(scnndxnm, sizeof (scnndxnm),
3061 		    MSG_INTL(MSG_FMT_SCNNDX), ndx);
3062 
3063 		if ((_cache->c_name = malloc(strlen(scnndxnm) + 1)) == NULL) {
3064 			int err = errno;
3065 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
3066 			    file, strerror(err));
3067 			return (0);
3068 		}
3069 		(void) strcpy(_cache->c_name, scnndxnm);
3070 	}
3071 
3072 	/*
3073 	 * Having collected all the sections, validate their address range.
3074 	 * Cases have existed where the section information has been invalid.
3075 	 * This can lead to all sorts of other, hard to diagnose errors, as
3076 	 * each section is processed individually (ie. with elf_getdata()).
3077 	 * Here, we carry out some address comparisons to catch a family of
3078 	 * overlapping memory issues we have observed (likely, there are others
3079 	 * that we have yet to discover).
3080 	 *
3081 	 * Note, should any memory overlap occur, obtaining any additional
3082 	 * data from the file is questionable.  However, it might still be
3083 	 * possible to inspect the ELF header, Programs headers, or individual
3084 	 * sections, so rather than bailing on an error condition, continue
3085 	 * processing to see if any data can be salvaged.
3086 	 */
3087 	if (shdr_ndx_arr_cnt > 1) {
3088 		sort_shdr_ndx_arr_cache = cache;
3089 		qsort(shdr_ndx_arr, shdr_ndx_arr_cnt,
3090 		    sizeof (*shdr_ndx_arr), sort_shdr_ndx_arr);
3091 	}
3092 	for (ndx = 0; ndx < shdr_ndx_arr_cnt; ndx++) {
3093 		Cache	*_cache = cache + shdr_ndx_arr[ndx];
3094 		Shdr	*shdr = _cache->c_shdr;
3095 		Off	bgn1, bgn = shdr->sh_offset;
3096 		Off	end1, end = shdr->sh_offset + shdr->sh_size;
3097 		size_t	ndx1;
3098 
3099 		/*
3100 		 * Check the section against all following ones, reporting
3101 		 * any overlaps. Since we've sorted the sections by offset,
3102 		 * we can stop after the first comparison that fails. There
3103 		 * are no overlaps in a properly formed ELF file, in which
3104 		 * case this algorithm runs in O(n) time. This will degenerate
3105 		 * to O(n^2) for a completely broken file. Such a file is
3106 		 * (1) highly unlikely, and (2) unusable, so it is reasonable
3107 		 * for the analysis to take longer.
3108 		 */
3109 		for (ndx1 = ndx + 1; ndx1 < shdr_ndx_arr_cnt; ndx1++) {
3110 			Cache	*_cache1 = cache + shdr_ndx_arr[ndx1];
3111 			Shdr	*shdr1 = _cache1->c_shdr;
3112 
3113 			bgn1 = shdr1->sh_offset;
3114 			end1 = shdr1->sh_offset + shdr1->sh_size;
3115 
3116 			if (((bgn1 <= bgn) && (end1 > bgn)) ||
3117 			    ((bgn1 < end) && (end1 >= end))) {
3118 				(void) fprintf(stderr,
3119 				    MSG_INTL(MSG_ERR_SECMEMOVER), file,
3120 				    EC_WORD(elf_ndxscn(_cache->c_scn)),
3121 				    _cache->c_name, EC_OFF(bgn), EC_OFF(end),
3122 				    EC_WORD(elf_ndxscn(_cache1->c_scn)),
3123 				    _cache1->c_name, EC_OFF(bgn1),
3124 				    EC_OFF(end1));
3125 			} else {	/* No overlap, so can stop */
3126 				break;
3127 			}
3128 		}
3129 
3130 		/*
3131 		 * In addition to checking for sections overlapping
3132 		 * each other (done above), we should also make sure
3133 		 * the section doesn't overlap the section header array.
3134 		 */
3135 		bgn1 = ehdr->e_shoff;
3136 		end1 = ehdr->e_shoff + (ehdr->e_shentsize * ehdr->e_shnum);
3137 
3138 		if (((bgn1 <= bgn) && (end1 > bgn)) ||
3139 		    ((bgn1 < end) && (end1 >= end))) {
3140 			(void) fprintf(stderr,
3141 			    MSG_INTL(MSG_ERR_SHDRMEMOVER), file, EC_OFF(bgn1),
3142 			    EC_OFF(end1),
3143 			    EC_WORD(elf_ndxscn(_cache->c_scn)),
3144 			    _cache->c_name, EC_OFF(bgn), EC_OFF(end));
3145 		}
3146 	}
3147 
3148 	/*
3149 	 * Obtain the data for each section.
3150 	 */
3151 	for (ndx = 1; ndx < shnum; ndx++) {
3152 		Cache	*_cache = &cache[ndx];
3153 		Elf_Scn	*scn = _cache->c_scn;
3154 
3155 		if ((_cache->c_data = elf_getdata(scn, NULL)) == NULL) {
3156 			failure(file, MSG_ORIG(MSG_ELF_GETDATA));
3157 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCNDATA),
3158 			    EC_WORD(elf_ndxscn(scn)));
3159 		}
3160 	}
3161 
3162 	return (1);
3163 }
3164 
3165 
3166 
3167 void
3168 regular(const char *file, int fd, Elf *elf, uint_t flags, int wfd)
3169 {
3170 	Elf_Scn		*scn;
3171 	Ehdr		*ehdr;
3172 	size_t		ndx, shstrndx, shnum, phnum;
3173 	Shdr		*shdr;
3174 	Cache		*cache;
3175 	VERSYM_STATE	versym;
3176 
3177 	if ((ehdr = elf_getehdr(elf)) == NULL) {
3178 		failure(file, MSG_ORIG(MSG_ELF_GETEHDR));
3179 		return;
3180 	}
3181 
3182 	if (elf_getshnum(elf, &shnum) == 0) {
3183 		failure(file, MSG_ORIG(MSG_ELF_GETSHNUM));
3184 		return;
3185 	}
3186 
3187 	if (elf_getshstrndx(elf, &shstrndx) == 0) {
3188 		failure(file, MSG_ORIG(MSG_ELF_GETSHSTRNDX));
3189 		return;
3190 	}
3191 
3192 	if (elf_getphnum(elf, &phnum) == 0) {
3193 		failure(file, MSG_ORIG(MSG_ELF_GETPHNUM));
3194 		return;
3195 	}
3196 	/*
3197 	 * If the user requested section headers derived from the
3198 	 * program headers (-P option) and this file doesn't have
3199 	 * any program headers (i.e. ET_REL), then we can't do it.
3200 	 */
3201 	if ((phnum == 0) && (flags & FLG_FAKESHDR)) {
3202 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_PNEEDSPH), file);
3203 		return;
3204 	}
3205 
3206 
3207 	if ((scn = elf_getscn(elf, 0)) != NULL) {
3208 		if ((shdr = elf_getshdr(scn)) == NULL) {
3209 			failure(file, MSG_ORIG(MSG_ELF_GETSHDR));
3210 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN), 0);
3211 			return;
3212 		}
3213 	} else
3214 		shdr = 0;
3215 
3216 	/*
3217 	 * Print the elf header.
3218 	 */
3219 	if (flags & FLG_EHDR)
3220 		Elf_ehdr(0, ehdr, shdr);
3221 
3222 	/*
3223 	 * If the section headers or program headers have inadequate
3224 	 * alignment for the class of object, print a warning. libelf
3225 	 * can handle such files, but programs that use them can crash
3226 	 * when they dereference unaligned items.
3227 	 */
3228 	if (ehdr->e_phoff & (sizeof (Addr) - 1))
3229 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADPHDRALIGN), file);
3230 	if (ehdr->e_shoff & (sizeof (Addr) - 1))
3231 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHDRALIGN), file);
3232 
3233 	/*
3234 	 * Print the program headers.
3235 	 */
3236 	if ((flags & FLG_PHDR) && (phnum != 0)) {
3237 		Conv_inv_buf_t	inv_buf;
3238 		Phdr		*phdr;
3239 
3240 		if ((phdr = elf_getphdr(elf)) == NULL) {
3241 			failure(file, MSG_ORIG(MSG_ELF_GETPHDR));
3242 			return;
3243 		}
3244 
3245 		for (ndx = 0; ndx < phnum; phdr++, ndx++) {
3246 			if (!match(0, conv_phdr_type(ehdr->e_machine,
3247 			    phdr->p_type, CONV_FMT_ALT_FILE, &inv_buf), ndx))
3248 				continue;
3249 
3250 			dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
3251 			dbg_print(0, MSG_INTL(MSG_ELF_PHDR), EC_WORD(ndx));
3252 			Elf_phdr(0, ehdr->e_machine, phdr);
3253 		}
3254 	}
3255 
3256 	/*
3257 	 * Decide how to proceed if there are no sections, if there's just
3258 	 * one section (the first section can act as an extension of the
3259 	 * ELF header), or if only program header information was requested.
3260 	 */
3261 	if ((shnum <= 1) || (flags && (flags & ~(FLG_EHDR | FLG_PHDR)) == 0)) {
3262 		/* If a core file, display the note and return */
3263 		if ((ehdr->e_type == ET_CORE) && (flags & FLG_NOTE)) {
3264 			note(0, shnum, file);
3265 			return;
3266 		}
3267 
3268 		/* If only program header info was requested, we're done */
3269 		if (flags && (flags & ~(FLG_EHDR | FLG_PHDR)) == 0)
3270 			return;
3271 
3272 		/*
3273 		 * Section headers are missing. Resort to synthesizing
3274 		 * section headers from the program headers.
3275 		 */
3276 		if ((flags & FLG_FAKESHDR) == 0) {
3277 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_NOSHDR), file);
3278 			flags |= FLG_FAKESHDR;
3279 		}
3280 	}
3281 
3282 	/*
3283 	 * Generate a cache of section headers and related information
3284 	 * for use by the rest of elfdump. If requested (or the file
3285 	 * contains no section headers), we generate a fake set of
3286 	 * headers from the information accessible from the program headers.
3287 	 * Otherwise, we use the real section headers contained in the file.
3288 	 */
3289 
3290 	if (flags & FLG_FAKESHDR) {
3291 		if (fake_shdr_cache(file, fd, elf, ehdr, &cache, &shnum) == 0)
3292 			return;
3293 	} else {
3294 		if (shdr_cache(file, elf, ehdr, shstrndx, shnum, &cache) == 0)
3295 			return;
3296 	}
3297 
3298 	/*
3299 	 * If -w was specified, find and write out the section(s) data.
3300 	 */
3301 	if (wfd) {
3302 		for (ndx = 1; ndx < shnum; ndx++) {
3303 			Cache	*_cache = &cache[ndx];
3304 
3305 			if (match(1, _cache->c_name, ndx) && _cache->c_data) {
3306 				(void) write(wfd, _cache->c_data->d_buf,
3307 				    _cache->c_data->d_size);
3308 			}
3309 		}
3310 	}
3311 
3312 	if (flags & FLG_SHDR)
3313 		sections(file, cache, shnum, ehdr);
3314 
3315 	if (flags & FLG_INTERP)
3316 		interp(file, cache, shnum, phnum, elf);
3317 
3318 	versions(cache, shnum, file, flags, &versym);
3319 
3320 	if (flags & FLG_SYMBOLS)
3321 		symbols(cache, shnum, ehdr, &versym, file, flags);
3322 
3323 	if (flags & FLG_SORT)
3324 		sunw_sort(cache, shnum, ehdr, &versym, file, flags);
3325 
3326 	if (flags & FLG_HASH)
3327 		hash(cache, shnum, file, flags);
3328 
3329 	if (flags & FLG_GOT)
3330 		got(cache, shnum, ehdr, file, flags);
3331 
3332 	if (flags & FLG_GROUP)
3333 		group(cache, shnum, file, flags);
3334 
3335 	if (flags & FLG_SYMINFO)
3336 		syminfo(cache, shnum, file);
3337 
3338 	if (flags & FLG_RELOC)
3339 		reloc(cache, shnum, ehdr, file, flags);
3340 
3341 	if (flags & FLG_DYNAMIC)
3342 		dynamic(cache, shnum, ehdr, file);
3343 
3344 	if (flags & FLG_NOTE)
3345 		note(cache, shnum, file);
3346 
3347 	if (flags & FLG_MOVE)
3348 		move(cache, shnum, file, flags);
3349 
3350 	if (flags & FLG_CHECKSUM)
3351 		checksum(elf);
3352 
3353 	if (flags & FLG_CAP)
3354 		cap(file, cache, shnum, phnum, ehdr, elf);
3355 
3356 	if (flags & FLG_UNWIND)
3357 		unwind(cache, shnum, phnum, ehdr, file, elf);
3358 
3359 
3360 	/* Release the memory used to cache section headers */
3361 	if (flags & FLG_FAKESHDR)
3362 		fake_shdr_cache_free(cache, shnum);
3363 	else
3364 		free(cache);
3365 }
3366