xref: /titanic_50/usr/src/cmd/sgs/elfdump/common/elfdump.c (revision 44743693dce3212f5edba623e0cb0327bd4337a3)
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 /*
1965  * This value controls which test dyn_test() performs.
1966  */
1967 typedef enum { DYN_TEST_ADDR, DYN_TEST_SIZE, DYN_TEST_ENTSIZE } dyn_test_t;
1968 
1969 /*
1970  * Used by dynamic() to compare the value of a dynamic element against
1971  * the starting address of the section it references.
1972  *
1973  * entry:
1974  *	test_type - Specify which dyn item is being tested.
1975  *	sh_type - SHT_* type value for required section.
1976  *	sec_cache - Cache entry for section, or NULL if the object lacks
1977  *		a section of this type.
1978  *	dyn - Dyn entry to be tested
1979  *	dynsec_cnt - # of dynamic section being examined. The first
1980  *		dynamic section is 1, the next is 2, and so on...
1981  *	ehdr - ELF header for file
1982  *	file - Name of file
1983  */
1984 static void
1985 dyn_test(dyn_test_t test_type, Word sh_type, Cache *sec_cache, Dyn *dyn,
1986     Word dynsec_cnt, Ehdr *ehdr, const char *file)
1987 {
1988 	Conv_inv_buf_t	buf1, buf2;
1989 
1990 	/*
1991 	 * These tests are based around the implicit assumption that
1992 	 * there is only one dynamic section in an object, and also only
1993 	 * one of the sections it references. We have therefore gathered
1994 	 * all of the necessary information to test this in a single pass
1995 	 * over the section headers, which is very efficient. We are not
1996 	 * aware of any case where more than one dynamic section would
1997 	 * be meaningful in an ELF object, so this is a reasonable solution.
1998 	 *
1999 	 * To test multiple dynamic sections correctly would be more
2000 	 * expensive in code and time. We would have to build a data structure
2001 	 * containing all the dynamic elements. Then, we would use the address
2002 	 * to locate the section it references and ensure the section is of
2003 	 * the right type and that the address in the dynamic element is
2004 	 * to the start of the section. Then, we could check the size and
2005 	 * entsize values against those same sections. This is O(n^2), and
2006 	 * also complicated.
2007 	 *
2008 	 * In the highly unlikely case that there is more than one dynamic
2009 	 * section, we only test the first one, and simply allow the values
2010 	 * of the subsequent one to be displayed unchallenged.
2011 	 */
2012 	if (dynsec_cnt != 1)
2013 		return;
2014 
2015 	/*
2016 	 * A DT_ item that references a section address should always find
2017 	 * the section in the file.
2018 	 */
2019 	if (sec_cache == NULL) {
2020 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_DYNNOBCKSEC), file,
2021 		    conv_sec_type(ehdr->e_machine, sh_type, 0, &buf1),
2022 		    conv_dyn_tag(dyn->d_tag, ehdr->e_machine, 0, &buf2));
2023 		return;
2024 	}
2025 
2026 
2027 	switch (test_type) {
2028 	case DYN_TEST_ADDR:
2029 		/* The section address should match the DT_ item value */
2030 		if (dyn->d_un.d_val != sec_cache->c_shdr->sh_addr)
2031 			(void) fprintf(stderr,
2032 			    MSG_INTL(MSG_ERR_DYNBADADDR), file,
2033 			    conv_dyn_tag(dyn->d_tag, ehdr->e_machine, 0, &buf1),
2034 			    EC_ADDR(dyn->d_un.d_val), sec_cache->c_ndx,
2035 			    sec_cache->c_name,
2036 			    EC_ADDR(sec_cache->c_shdr->sh_addr));
2037 		break;
2038 
2039 	case DYN_TEST_SIZE:
2040 		/* The section size should match the DT_ item value */
2041 		if (dyn->d_un.d_val != sec_cache->c_shdr->sh_size)
2042 			(void) fprintf(stderr,
2043 			    MSG_INTL(MSG_ERR_DYNBADSIZE), file,
2044 			    conv_dyn_tag(dyn->d_tag, ehdr->e_machine, 0, &buf1),
2045 			    EC_XWORD(dyn->d_un.d_val),
2046 			    sec_cache->c_ndx, sec_cache->c_name,
2047 			    EC_XWORD(sec_cache->c_shdr->sh_size));
2048 		break;
2049 
2050 	case DYN_TEST_ENTSIZE:
2051 		/* The sh_entsize value should match the DT_ item value */
2052 		if (dyn->d_un.d_val != sec_cache->c_shdr->sh_entsize)
2053 			(void) fprintf(stderr,
2054 			    MSG_INTL(MSG_ERR_DYNBADENTSIZE), file,
2055 			    conv_dyn_tag(dyn->d_tag, ehdr->e_machine, 0, &buf1),
2056 			    EC_XWORD(dyn->d_un.d_val),
2057 			    sec_cache->c_ndx, sec_cache->c_name,
2058 			    EC_XWORD(sec_cache->c_shdr->sh_entsize));
2059 		break;
2060 	}
2061 }
2062 
2063 
2064 /*
2065  * Search for and process a .dynamic section.
2066  */
2067 static void
2068 dynamic(Cache *cache, Word shnum, Ehdr *ehdr, const char *file)
2069 {
2070 	struct {
2071 		Cache	*dynstr;
2072 		Cache	*dynsym;
2073 		Cache	*hash;
2074 		Cache	*fini;
2075 		Cache	*fini_array;
2076 		Cache	*init;
2077 		Cache	*init_array;
2078 		Cache	*preinit_array;
2079 		Cache	*rel;
2080 		Cache	*rela;
2081 		Cache	*sunw_cap;
2082 		Cache	*sunw_ldynsym;
2083 		Cache	*sunw_move;
2084 		Cache	*sunw_syminfo;
2085 		Cache	*sunw_symsort;
2086 		Cache	*sunw_tlssort;
2087 		Cache	*sunw_verdef;
2088 		Cache	*sunw_verneed;
2089 		Cache	*sunw_versym;
2090 	} sec;
2091 	Word	dynsec_ndx;
2092 	Word	dynsec_num;
2093 	int	dynsec_cnt;
2094 	Word	cnt;
2095 
2096 	/*
2097 	 * Make a pass over all the sections, gathering section information
2098 	 * we'll need below.
2099 	 */
2100 	dynsec_num = 0;
2101 	bzero(&sec, sizeof (sec));
2102 	for (cnt = 1; cnt < shnum; cnt++) {
2103 		Cache	*_cache = &cache[cnt];
2104 
2105 		switch (_cache->c_shdr->sh_type) {
2106 		case SHT_DYNAMIC:
2107 			if (dynsec_num == 0) {
2108 				dynsec_ndx = cnt;
2109 
2110 				/* Does it have a valid string table? */
2111 				(void) stringtbl(cache, 0, cnt, shnum, file,
2112 				    0, 0, &sec.dynstr);
2113 			}
2114 			dynsec_num++;
2115 			break;
2116 
2117 
2118 		case SHT_PROGBITS:
2119 			/*
2120 			 * We want to detect the .init and .fini sections,
2121 			 * if present. These are SHT_PROGBITS, so all we
2122 			 * have to go on is the section name. Normally comparing
2123 			 * names is a bad idea, but there are some special
2124 			 * names (i.e. .init/.fini/.interp) that are very
2125 			 * difficult to use in any other context, and for
2126 			 * these symbols, we do the heuristic match.
2127 			 */
2128 			if (strcmp(_cache->c_name,
2129 			    MSG_ORIG(MSG_ELF_INIT)) == 0) {
2130 				if (sec.init == NULL)
2131 					sec.init = _cache;
2132 			} else if (strcmp(_cache->c_name,
2133 			    MSG_ORIG(MSG_ELF_FINI)) == 0) {
2134 				if (sec.fini == NULL)
2135 					sec.fini = _cache;
2136 			}
2137 			break;
2138 
2139 		case SHT_REL:
2140 			/*
2141 			 * We want the SHT_REL section with the lowest
2142 			 * offset. The linker gathers them together,
2143 			 * and puts the address of the first one
2144 			 * into the DT_REL dynamic element.
2145 			 */
2146 			if ((sec.rel == NULL) ||
2147 			    (_cache->c_shdr->sh_offset <
2148 			    sec.rel->c_shdr->sh_offset))
2149 				sec.rel = _cache;
2150 			break;
2151 
2152 		case SHT_RELA:
2153 			/* RELA is handled just like RELA above */
2154 			if ((sec.rela == NULL) ||
2155 			    (_cache->c_shdr->sh_offset <
2156 			    sec.rela->c_shdr->sh_offset))
2157 				sec.rela = _cache;
2158 			break;
2159 
2160 		/*
2161 		 * The GRAB macro is used for the simple case in which
2162 		 * we simply grab the first section of the desired type.
2163 		 */
2164 #define	GRAB(_sec_type, _sec_field) \
2165 		case _sec_type: \
2166 			if (sec._sec_field == NULL) \
2167 				sec._sec_field = _cache; \
2168 				break
2169 		GRAB(SHT_DYNSYM,	dynsym);
2170 		GRAB(SHT_FINI_ARRAY,	fini_array);
2171 		GRAB(SHT_HASH,		hash);
2172 		GRAB(SHT_INIT_ARRAY,	init_array);
2173 		GRAB(SHT_SUNW_move,	sunw_move);
2174 		GRAB(SHT_PREINIT_ARRAY,	preinit_array);
2175 		GRAB(SHT_SUNW_cap,	sunw_cap);
2176 		GRAB(SHT_SUNW_LDYNSYM,	sunw_ldynsym);
2177 		GRAB(SHT_SUNW_syminfo,	sunw_syminfo);
2178 		GRAB(SHT_SUNW_symsort,	sunw_symsort);
2179 		GRAB(SHT_SUNW_tlssort,	sunw_tlssort);
2180 		GRAB(SHT_SUNW_verdef,	sunw_verdef);
2181 		GRAB(SHT_SUNW_verneed,	sunw_verneed);
2182 		GRAB(SHT_SUNW_versym,	sunw_versym);
2183 #undef GRAB
2184 		}
2185 	}
2186 
2187 	/*
2188 	 * If no dynamic section, return immediately. If more than one
2189 	 * dynamic section, then something odd is going on and an error
2190 	 * is in order, but then continue on and display them all.
2191 	 */
2192 	if (dynsec_num == 0)
2193 		return;
2194 	if (dynsec_num > 1)
2195 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_MULTDYN),
2196 		    file, EC_WORD(dynsec_num));
2197 
2198 
2199 	dynsec_cnt = 0;
2200 	for (cnt = dynsec_ndx; (cnt < shnum) && (dynsec_cnt < dynsec_num);
2201 	    cnt++) {
2202 		Dyn	*dyn;
2203 		ulong_t	numdyn;
2204 		int	ndx, end_ndx;
2205 		Cache	*_cache = &cache[cnt], *strsec;
2206 		Shdr	*shdr = _cache->c_shdr;
2207 		int	dumped = 0;
2208 
2209 		if (shdr->sh_type != SHT_DYNAMIC)
2210 			continue;
2211 		dynsec_cnt++;
2212 
2213 		/*
2214 		 * Verify the associated string table section.
2215 		 */
2216 		if (stringtbl(cache, 0, cnt, shnum, file, 0, 0, &strsec) == 0)
2217 			continue;
2218 
2219 		if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
2220 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
2221 			    file, _cache->c_name);
2222 			continue;
2223 		}
2224 		if (_cache->c_data == NULL)
2225 			continue;
2226 
2227 		numdyn = shdr->sh_size / shdr->sh_entsize;
2228 		dyn = (Dyn *)_cache->c_data->d_buf;
2229 
2230 		/*
2231 		 * We expect the REL/RELA entries to reference the reloc
2232 		 * section with the lowest address. However, this is
2233 		 * not true for dumped objects. Detect if this object has
2234 		 * been dumped so that we can skip the reloc address test
2235 		 * in that case.
2236 		 */
2237 		for (ndx = 0; ndx < numdyn; dyn++, ndx++) {
2238 			if (dyn->d_tag == DT_FLAGS_1) {
2239 				dumped = (dyn->d_un.d_val & DF_1_CONFALT) != 0;
2240 				break;
2241 			}
2242 		}
2243 		dyn = (Dyn *)_cache->c_data->d_buf;
2244 
2245 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2246 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_DYNAMIC), _cache->c_name);
2247 
2248 		Elf_dyn_title(0);
2249 
2250 		for (ndx = 0; ndx < numdyn; dyn++, ndx++) {
2251 			union {
2252 				Conv_dyn_flag_buf_t	flag;
2253 				Conv_dyn_flag1_buf_t	flag1;
2254 				Conv_dyn_posflag1_buf_t	posflag1;
2255 				Conv_dyn_feature1_buf_t	feature1;
2256 			} c_buf;
2257 			const char	*name = NULL;
2258 
2259 			/*
2260 			 * Print the information numerically, and if possible
2261 			 * as a string. If a string is available, name is
2262 			 * set to reference it.
2263 			 *
2264 			 * Also, take this opportunity to sanity check
2265 			 * the values of DT elements. In the code above,
2266 			 * we gathered information on sections that are
2267 			 * referenced by the dynamic section. Here, we
2268 			 * compare the attributes of those sections to
2269 			 * the DT_ items that reference them and report
2270 			 * on inconsistencies.
2271 			 *
2272 			 * Things not currently tested that could be improved
2273 			 * in later revisions include:
2274 			 *	- We don't check PLT or GOT related items
2275 			 *	- We don't handle computing the lengths of
2276 			 *		relocation arrays. To handle this
2277 			 *		requires examining data that spans
2278 			 *		across sections, in a contiguous span
2279 			 *		within a single segment.
2280 			 *	- DT_VERDEFNUM and DT_VERNEEDNUM can't be
2281 			 *		verified without parsing the sections.
2282 			 *	- We don't handle DT_SUNW_SYMSZ, which would
2283 			 *		be the sum of the lengths of .dynsym and
2284 			 *		.SUNW_ldynsym
2285 			 *	- DT_SUNW_STRPAD can't be verified other than
2286 			 *		to check that it's not larger than
2287 			 *		the string table.
2288 			 *	- Some items come in "all or none" clusters
2289 			 *		that give an address, element size,
2290 			 *		and data length in bytes. We don't
2291 			 *		verify that there are no missing items
2292 			 *		in such groups.
2293 			 */
2294 			switch (dyn->d_tag) {
2295 			case DT_NULL:
2296 				/*
2297 				 * Special case: DT_NULLs can come in groups
2298 				 * that we prefer to reduce to a single line.
2299 				 */
2300 				end_ndx = ndx;
2301 				while ((end_ndx < (numdyn - 1)) &&
2302 				    ((dyn + 1)->d_tag == DT_NULL)) {
2303 					dyn++;
2304 					end_ndx++;
2305 				}
2306 				Elf_dyn_null_entry(0, dyn, ndx, end_ndx);
2307 				ndx = end_ndx;
2308 				continue;
2309 
2310 			/*
2311 			 * String items all reference the dynstr. The string()
2312 			 * function does the necessary sanity checking.
2313 			 */
2314 			case DT_NEEDED:
2315 			case DT_SONAME:
2316 			case DT_FILTER:
2317 			case DT_AUXILIARY:
2318 			case DT_CONFIG:
2319 			case DT_RPATH:
2320 			case DT_RUNPATH:
2321 			case DT_USED:
2322 			case DT_DEPAUDIT:
2323 			case DT_AUDIT:
2324 			case DT_SUNW_AUXILIARY:
2325 			case DT_SUNW_FILTER:
2326 				name = string(_cache, ndx, strsec,
2327 				    file, dyn->d_un.d_ptr);
2328 				break;
2329 
2330 			case DT_FLAGS:
2331 				name = conv_dyn_flag(dyn->d_un.d_val,
2332 				    0, &c_buf.flag);
2333 				break;
2334 			case DT_FLAGS_1:
2335 				name = conv_dyn_flag1(dyn->d_un.d_val, 0,
2336 				    &c_buf.flag1);
2337 				break;
2338 			case DT_POSFLAG_1:
2339 				name = conv_dyn_posflag1(dyn->d_un.d_val, 0,
2340 				    &c_buf.posflag1);
2341 				break;
2342 			case DT_FEATURE_1:
2343 				name = conv_dyn_feature1(dyn->d_un.d_val, 0,
2344 				    &c_buf.feature1);
2345 				break;
2346 			case DT_DEPRECATED_SPARC_REGISTER:
2347 				name = MSG_INTL(MSG_STR_DEPRECATED);
2348 				break;
2349 
2350 			/*
2351 			 * Cases below this point are strictly sanity checking,
2352 			 * and do not generate a name string. The TEST_ macros
2353 			 * are used to hide the boilerplate arguments neeeded
2354 			 * by dyn_test().
2355 			 */
2356 #define	TEST_ADDR(_sh_type, _sec_field) \
2357 				dyn_test(DYN_TEST_ADDR, _sh_type, \
2358 				    sec._sec_field, dyn, dynsec_cnt, ehdr, file)
2359 #define	TEST_SIZE(_sh_type, _sec_field) \
2360 				dyn_test(DYN_TEST_SIZE, _sh_type, \
2361 				    sec._sec_field, dyn, dynsec_cnt, ehdr, file)
2362 #define	TEST_ENTSIZE(_sh_type, _sec_field) \
2363 				dyn_test(DYN_TEST_ENTSIZE, _sh_type, \
2364 				    sec._sec_field, dyn, dynsec_cnt, ehdr, file)
2365 
2366 			case DT_FINI:
2367 				TEST_ADDR(SHT_PROGBITS, fini);
2368 				break;
2369 
2370 			case DT_FINI_ARRAY:
2371 				TEST_ADDR(SHT_FINI_ARRAY, fini_array);
2372 				break;
2373 
2374 			case DT_FINI_ARRAYSZ:
2375 				TEST_SIZE(SHT_FINI_ARRAY, fini_array);
2376 				break;
2377 
2378 			case DT_HASH:
2379 				TEST_ADDR(SHT_HASH, hash);
2380 				break;
2381 
2382 			case DT_INIT:
2383 				TEST_ADDR(SHT_PROGBITS, init);
2384 				break;
2385 
2386 			case DT_INIT_ARRAY:
2387 				TEST_ADDR(SHT_INIT_ARRAY, init_array);
2388 				break;
2389 
2390 			case DT_INIT_ARRAYSZ:
2391 				TEST_SIZE(SHT_INIT_ARRAY, init_array);
2392 				break;
2393 
2394 			case DT_MOVEENT:
2395 				TEST_ENTSIZE(SHT_SUNW_move, sunw_move);
2396 				break;
2397 
2398 			case DT_MOVESZ:
2399 				TEST_SIZE(SHT_SUNW_move, sunw_move);
2400 				break;
2401 
2402 			case DT_MOVETAB:
2403 				TEST_ADDR(SHT_SUNW_move, sunw_move);
2404 				break;
2405 
2406 			case DT_PREINIT_ARRAY:
2407 				TEST_ADDR(SHT_PREINIT_ARRAY, preinit_array);
2408 				break;
2409 
2410 			case DT_PREINIT_ARRAYSZ:
2411 				TEST_SIZE(SHT_PREINIT_ARRAY, preinit_array);
2412 				break;
2413 
2414 			case DT_REL:
2415 				if (!dumped)
2416 					TEST_ADDR(SHT_REL, rel);
2417 				break;
2418 
2419 			case DT_RELENT:
2420 				TEST_ENTSIZE(SHT_REL, rel);
2421 				break;
2422 
2423 			case DT_RELA:
2424 				if (!dumped)
2425 					TEST_ADDR(SHT_RELA, rela);
2426 				break;
2427 
2428 			case DT_RELAENT:
2429 				TEST_ENTSIZE(SHT_RELA, rela);
2430 				break;
2431 
2432 			case DT_STRTAB:
2433 				TEST_ADDR(SHT_STRTAB, dynstr);
2434 				break;
2435 
2436 			case DT_STRSZ:
2437 				TEST_SIZE(SHT_STRTAB, dynstr);
2438 				break;
2439 
2440 			case DT_SUNW_CAP:
2441 				TEST_ADDR(SHT_SUNW_cap, sunw_cap);
2442 				break;
2443 
2444 			case DT_SUNW_SYMTAB:
2445 				TEST_ADDR(SHT_SUNW_LDYNSYM, sunw_ldynsym);
2446 				break;
2447 
2448 			case DT_SYMENT:
2449 				TEST_ENTSIZE(SHT_DYNSYM, dynsym);
2450 				break;
2451 
2452 			case DT_SYMINENT:
2453 				TEST_ENTSIZE(SHT_SUNW_syminfo, sunw_syminfo);
2454 				break;
2455 
2456 			case DT_SYMINFO:
2457 				TEST_ADDR(SHT_SUNW_syminfo, sunw_syminfo);
2458 				break;
2459 
2460 			case DT_SYMINSZ:
2461 				TEST_SIZE(SHT_SUNW_syminfo, sunw_syminfo);
2462 				break;
2463 
2464 			case DT_SYMTAB:
2465 				TEST_ADDR(SHT_DYNSYM, dynsym);
2466 				break;
2467 
2468 			case DT_SUNW_SORTENT:
2469 				/*
2470 				 * This entry is related to both the symsort and
2471 				 * tlssort sections.
2472 				 */
2473 				{
2474 					int test_tls =
2475 					    (sec.sunw_tlssort != NULL);
2476 					int test_sym =
2477 					    (sec.sunw_symsort != NULL) ||
2478 					    !test_tls;
2479 					if (test_sym)
2480 						TEST_ENTSIZE(SHT_SUNW_symsort,
2481 						    sunw_symsort);
2482 					if (test_tls)
2483 						TEST_ENTSIZE(SHT_SUNW_tlssort,
2484 						    sunw_tlssort);
2485 				}
2486 				break;
2487 
2488 
2489 			case DT_SUNW_SYMSORT:
2490 				TEST_ADDR(SHT_SUNW_symsort, sunw_symsort);
2491 				break;
2492 
2493 			case DT_SUNW_SYMSORTSZ:
2494 				TEST_SIZE(SHT_SUNW_symsort, sunw_symsort);
2495 				break;
2496 
2497 			case DT_SUNW_TLSSORT:
2498 				TEST_ADDR(SHT_SUNW_tlssort, sunw_tlssort);
2499 				break;
2500 
2501 			case DT_SUNW_TLSSORTSZ:
2502 				TEST_SIZE(SHT_SUNW_tlssort, sunw_tlssort);
2503 				break;
2504 
2505 			case DT_VERDEF:
2506 				TEST_ADDR(SHT_SUNW_verdef, sunw_verdef);
2507 				break;
2508 
2509 			case DT_VERNEED:
2510 				TEST_ADDR(SHT_SUNW_verneed, sunw_verneed);
2511 				break;
2512 
2513 			case DT_VERSYM:
2514 				TEST_ADDR(SHT_SUNW_versym, sunw_versym);
2515 				break;
2516 #undef TEST_ADDR
2517 #undef TEST_SIZE
2518 #undef TEST_ENTSIZE
2519 			}
2520 
2521 			if (name == NULL)
2522 				name = MSG_ORIG(MSG_STR_EMPTY);
2523 			Elf_dyn_entry(0, dyn, ndx, name, ehdr->e_machine);
2524 		}
2525 	}
2526 }
2527 
2528 /*
2529  * Search for and process a MOVE section.
2530  */
2531 static void
2532 move(Cache *cache, Word shnum, const char *file, uint_t flags)
2533 {
2534 	Word		cnt;
2535 	const char	*fmt = 0;
2536 
2537 	for (cnt = 1; cnt < shnum; cnt++) {
2538 		Word	movenum, symnum, ndx;
2539 		Sym	*syms;
2540 		Cache	*_cache = &cache[cnt];
2541 		Shdr	*shdr = _cache->c_shdr;
2542 		Cache	*symsec, *strsec;
2543 		Move	*move;
2544 
2545 		if (shdr->sh_type != SHT_SUNW_move)
2546 			continue;
2547 		if (!match(0, _cache->c_name, cnt))
2548 			continue;
2549 
2550 		/*
2551 		 * Determine the move data and number.
2552 		 */
2553 		if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
2554 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
2555 			    file, _cache->c_name);
2556 			continue;
2557 		}
2558 		if (_cache->c_data == NULL)
2559 			continue;
2560 
2561 		move = (Move *)_cache->c_data->d_buf;
2562 		movenum = shdr->sh_size / shdr->sh_entsize;
2563 
2564 		/*
2565 		 * Get the data buffer for the associated symbol table and
2566 		 * string table.
2567 		 */
2568 		if (stringtbl(cache, 1, cnt, shnum, file,
2569 		    &symnum, &symsec, &strsec) == 0)
2570 			return;
2571 
2572 		syms = (Sym *)symsec->c_data->d_buf;
2573 
2574 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2575 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_MOVE), _cache->c_name);
2576 		dbg_print(0, MSG_INTL(MSG_MOVE_TITLE));
2577 
2578 		if (fmt == 0)
2579 			fmt = MSG_INTL(MSG_MOVE_ENTRY);
2580 
2581 		for (ndx = 0; ndx < movenum; move++, ndx++) {
2582 			const char	*symname;
2583 			char		index[MAXNDXSIZE], section[BUFSIZ];
2584 			Word		symndx, shndx;
2585 			Sym		*sym;
2586 
2587 			/*
2588 			 * Check for null entries
2589 			 */
2590 			if ((move->m_info == 0) && (move->m_value == 0) &&
2591 			    (move->m_poffset == 0) && (move->m_repeat == 0) &&
2592 			    (move->m_stride == 0)) {
2593 				dbg_print(0, fmt, MSG_ORIG(MSG_STR_EMPTY),
2594 				    EC_XWORD(move->m_poffset), 0, 0, 0,
2595 				    EC_LWORD(0), MSG_ORIG(MSG_STR_EMPTY));
2596 				continue;
2597 			}
2598 			if (((symndx = ELF_M_SYM(move->m_info)) == 0) ||
2599 			    (symndx >= symnum)) {
2600 				(void) fprintf(stderr,
2601 				    MSG_INTL(MSG_ERR_BADMINFO), file,
2602 				    _cache->c_name, EC_XWORD(move->m_info));
2603 
2604 				(void) snprintf(index, MAXNDXSIZE,
2605 				    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(symndx));
2606 				dbg_print(0, fmt, index,
2607 				    EC_XWORD(move->m_poffset),
2608 				    ELF_M_SIZE(move->m_info), move->m_repeat,
2609 				    move->m_stride, move->m_value,
2610 				    MSG_INTL(MSG_STR_UNKNOWN));
2611 				continue;
2612 			}
2613 
2614 			symname = relsymname(cache, _cache, strsec,
2615 			    symndx, symnum, ndx, syms, section, BUFSIZ, file,
2616 			    flags);
2617 			sym = (Sym *)(syms + symndx);
2618 
2619 			/*
2620 			 * Additional sanity check.
2621 			 */
2622 			shndx = sym->st_shndx;
2623 			if (!((shndx == SHN_COMMON) ||
2624 			    (((shndx >= 1) && (shndx <= shnum)) &&
2625 			    (cache[shndx].c_shdr)->sh_type == SHT_NOBITS))) {
2626 				(void) fprintf(stderr,
2627 				    MSG_INTL(MSG_ERR_BADSYM2), file,
2628 				    _cache->c_name, demangle(symname, flags));
2629 			}
2630 
2631 			(void) snprintf(index, MAXNDXSIZE,
2632 			    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(symndx));
2633 			dbg_print(0, fmt, index, EC_XWORD(move->m_poffset),
2634 			    ELF_M_SIZE(move->m_info), move->m_repeat,
2635 			    move->m_stride, move->m_value,
2636 			    demangle(symname, flags));
2637 		}
2638 	}
2639 }
2640 
2641 /*
2642  * Traverse a note section analyzing each note information block.
2643  * The data buffers size is used to validate references before they are made,
2644  * and is decremented as each element is processed.
2645  */
2646 void
2647 note_entry(Cache *cache, Word *data, size_t size, const char *file)
2648 {
2649 	size_t	bsize = size;
2650 
2651 	/*
2652 	 * Print out a single `note' information block.
2653 	 */
2654 	while (size > 0) {
2655 		size_t	namesz, descsz, type, pad, noteoff;
2656 
2657 		noteoff = bsize - size;
2658 		/*
2659 		 * Make sure we can at least reference the 3 initial entries
2660 		 * (4-byte words) of the note information block.
2661 		 */
2662 		if (size >= (sizeof (Word) * 3))
2663 			size -= (sizeof (Word) * 3);
2664 		else {
2665 			(void) fprintf(stderr, MSG_INTL(MSG_NOTE_BADDATASZ),
2666 			    file, cache->c_name, EC_WORD(noteoff));
2667 			return;
2668 		}
2669 
2670 		/*
2671 		 * Make sure any specified name string can be referenced.
2672 		 */
2673 		if ((namesz = *data++) != 0) {
2674 			if (size >= namesz)
2675 				size -= namesz;
2676 			else {
2677 				(void) fprintf(stderr,
2678 				    MSG_INTL(MSG_NOTE_BADNMSZ), file,
2679 				    cache->c_name, EC_WORD(noteoff),
2680 				    EC_WORD(namesz));
2681 				return;
2682 			}
2683 		}
2684 
2685 		/*
2686 		 * Make sure any specified descriptor can be referenced.
2687 		 */
2688 		if ((descsz = *data++) != 0) {
2689 			/*
2690 			 * If namesz isn't a 4-byte multiple, account for any
2691 			 * padding that must exist before the descriptor.
2692 			 */
2693 			if ((pad = (namesz & (sizeof (Word) - 1))) != 0) {
2694 				pad = sizeof (Word) - pad;
2695 				size -= pad;
2696 			}
2697 			if (size >= descsz)
2698 				size -= descsz;
2699 			else {
2700 				(void) fprintf(stderr,
2701 				    MSG_INTL(MSG_NOTE_BADDESZ), file,
2702 				    cache->c_name, EC_WORD(noteoff),
2703 				    EC_WORD(namesz));
2704 				return;
2705 			}
2706 		}
2707 
2708 		type = *data++;
2709 
2710 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2711 		dbg_print(0, MSG_ORIG(MSG_NOTE_TYPE), EC_WORD(type));
2712 
2713 		dbg_print(0, MSG_ORIG(MSG_NOTE_NAMESZ), EC_WORD(namesz));
2714 		if (namesz) {
2715 			char	*name = (char *)data;
2716 
2717 			/*
2718 			 * Since the name string may have 'null' bytes
2719 			 * in it (ia32 .string) - we just write the
2720 			 * whole stream in a single fwrite.
2721 			 */
2722 			(void) fwrite(name, namesz, 1, stdout);
2723 			name = name + ((namesz + (sizeof (Word) - 1)) &
2724 			    ~(sizeof (Word) - 1));
2725 			/* LINTED */
2726 			data = (Word *)name;
2727 			dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2728 		}
2729 
2730 		/*
2731 		 * If multiple information blocks exist within a .note section
2732 		 * account for any padding that must exist before the next
2733 		 * information block.
2734 		 */
2735 		if ((pad = (descsz & (sizeof (Word) - 1))) != 0) {
2736 			pad = sizeof (Word) - pad;
2737 			if (size > pad)
2738 				size -= pad;
2739 		}
2740 
2741 		dbg_print(0, MSG_ORIG(MSG_NOTE_DESCSZ), EC_WORD(descsz));
2742 		if (descsz) {
2743 			int		ndx, byte, word;
2744 			char		string[58], *str = string;
2745 			uchar_t		*desc = (uchar_t *)data;
2746 
2747 			/*
2748 			 * Dump descriptor bytes.
2749 			 */
2750 			for (ndx = byte = word = 0; descsz; descsz--, desc++) {
2751 				int	tok = *desc;
2752 
2753 				(void) snprintf(str, 58, MSG_ORIG(MSG_NOTE_TOK),
2754 				    tok);
2755 				str += 3;
2756 
2757 				if (++byte == 4) {
2758 					*str++ = ' ', *str++ = ' ';
2759 					word++;
2760 					byte = 0;
2761 				}
2762 				if (word == 4) {
2763 					*str = '\0';
2764 					dbg_print(0, MSG_ORIG(MSG_NOTE_DESC),
2765 					    ndx, string);
2766 					word = 0;
2767 					ndx += 16;
2768 					str = string;
2769 				}
2770 			}
2771 			if (byte || word) {
2772 				*str = '\0';
2773 				dbg_print(0, MSG_ORIG(MSG_NOTE_DESC),
2774 				    ndx, string);
2775 			}
2776 
2777 			desc += pad;
2778 			/* LINTED */
2779 			data = (Word *)desc;
2780 		}
2781 	}
2782 }
2783 
2784 /*
2785  * Search for and process a .note section.
2786  */
2787 static void
2788 note(Cache *cache, Word shnum, const char *file)
2789 {
2790 	Word	cnt;
2791 
2792 	/*
2793 	 * Otherwise look for any .note sections.
2794 	 */
2795 	for (cnt = 1; cnt < shnum; cnt++) {
2796 		Cache	*_cache = &cache[cnt];
2797 		Shdr	*shdr = _cache->c_shdr;
2798 
2799 		if (shdr->sh_type != SHT_NOTE)
2800 			continue;
2801 		if (!match(0, _cache->c_name, cnt))
2802 			continue;
2803 
2804 		/*
2805 		 * As these sections are often hand rolled, make sure they're
2806 		 * properly aligned before proceeding, and issue an error
2807 		 * as necessary.
2808 		 *
2809 		 * Note that we will continue on to display the note even
2810 		 * if it has bad alignment. We can do this safely, because
2811 		 * libelf knows the alignment required for SHT_NOTE, and
2812 		 * takes steps to deliver a properly aligned buffer to us
2813 		 * even if the actual file is misaligned.
2814 		 */
2815 		if (shdr->sh_offset & (sizeof (Word) - 1))
2816 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADALIGN),
2817 			    file, _cache->c_name);
2818 
2819 		if (_cache->c_data == NULL)
2820 			continue;
2821 
2822 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2823 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_NOTE), _cache->c_name);
2824 		note_entry(_cache, (Word *)_cache->c_data->d_buf,
2825 		/* LINTED */
2826 		    (Word)_cache->c_data->d_size, file);
2827 	}
2828 }
2829 
2830 /*
2831  * Determine an individual hash entry.  This may be the initial hash entry,
2832  * or an associated chain entry.
2833  */
2834 static void
2835 hash_entry(Cache *refsec, Cache *strsec, const char *hsecname, Word hashndx,
2836     Word symndx, Word symn, Sym *syms, const char *file, ulong_t bkts,
2837     uint_t flags, int chain)
2838 {
2839 	Sym		*sym;
2840 	const char	*symname, *str;
2841 	char		_bucket[MAXNDXSIZE], _symndx[MAXNDXSIZE];
2842 	ulong_t		nbkt, nhash;
2843 
2844 	if (symndx > symn) {
2845 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_HSBADSYMNDX), file,
2846 		    EC_WORD(symndx), EC_WORD(hashndx));
2847 		symname = MSG_INTL(MSG_STR_UNKNOWN);
2848 	} else {
2849 		sym = (Sym *)(syms + symndx);
2850 		symname = string(refsec, symndx, strsec, file, sym->st_name);
2851 	}
2852 
2853 	if (chain == 0) {
2854 		(void) snprintf(_bucket, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INTEGER),
2855 		    hashndx);
2856 		str = (const char *)_bucket;
2857 	} else
2858 		str = MSG_ORIG(MSG_STR_EMPTY);
2859 
2860 	(void) snprintf(_symndx, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INDEX2),
2861 	    EC_WORD(symndx));
2862 	dbg_print(0, MSG_ORIG(MSG_FMT_HASH_INFO), str, _symndx,
2863 	    demangle(symname, flags));
2864 
2865 	/*
2866 	 * Determine if this string is in the correct bucket.
2867 	 */
2868 	nhash = elf_hash(symname);
2869 	nbkt = nhash % bkts;
2870 
2871 	if (nbkt != hashndx) {
2872 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADHASH), file,
2873 		    hsecname, symname, EC_WORD(hashndx), nbkt);
2874 	}
2875 }
2876 
2877 #define	MAXCOUNT	500
2878 
2879 static void
2880 hash(Cache *cache, Word shnum, const char *file, uint_t flags)
2881 {
2882 	static int	count[MAXCOUNT];
2883 	Word		cnt;
2884 	ulong_t		ndx, bkts;
2885 	char		number[MAXNDXSIZE];
2886 
2887 	for (cnt = 1; cnt < shnum; cnt++) {
2888 		uint_t		*hash, *chain;
2889 		Cache		*_cache = &cache[cnt];
2890 		Shdr		*sshdr, *hshdr = _cache->c_shdr;
2891 		char		*ssecname, *hsecname = _cache->c_name;
2892 		Sym		*syms;
2893 		Word		symn;
2894 
2895 		if (hshdr->sh_type != SHT_HASH)
2896 			continue;
2897 
2898 		/*
2899 		 * Determine the hash table data and size.
2900 		 */
2901 		if ((hshdr->sh_entsize == 0) || (hshdr->sh_size == 0)) {
2902 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
2903 			    file, hsecname);
2904 			continue;
2905 		}
2906 		if (_cache->c_data == NULL)
2907 			continue;
2908 
2909 		hash = (uint_t *)_cache->c_data->d_buf;
2910 		bkts = *hash;
2911 		chain = hash + 2 + bkts;
2912 		hash += 2;
2913 
2914 		/*
2915 		 * Get the data buffer for the associated symbol table.
2916 		 */
2917 		if ((hshdr->sh_link == 0) || (hshdr->sh_link >= shnum)) {
2918 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
2919 			    file, hsecname, EC_WORD(hshdr->sh_link));
2920 			continue;
2921 		}
2922 
2923 		_cache = &cache[hshdr->sh_link];
2924 		ssecname = _cache->c_name;
2925 
2926 		if (_cache->c_data == NULL)
2927 			continue;
2928 
2929 		if ((syms = (Sym *)_cache->c_data->d_buf) == NULL) {
2930 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
2931 			    file, ssecname);
2932 			continue;
2933 		}
2934 
2935 		sshdr = _cache->c_shdr;
2936 		/* LINTED */
2937 		symn = (Word)(sshdr->sh_size / sshdr->sh_entsize);
2938 
2939 		/*
2940 		 * Get the associated string table section.
2941 		 */
2942 		if ((sshdr->sh_link == 0) || (sshdr->sh_link >= shnum)) {
2943 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
2944 			    file, ssecname, EC_WORD(sshdr->sh_link));
2945 			continue;
2946 		}
2947 
2948 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2949 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_HASH), hsecname);
2950 		dbg_print(0, MSG_INTL(MSG_ELF_HASH_INFO));
2951 
2952 		/*
2953 		 * Loop through the hash buckets, printing the appropriate
2954 		 * symbols.
2955 		 */
2956 		for (ndx = 0; ndx < bkts; ndx++, hash++) {
2957 			Word	_ndx, _cnt;
2958 
2959 			if (*hash == 0) {
2960 				count[0]++;
2961 				continue;
2962 			}
2963 
2964 			hash_entry(_cache, &cache[sshdr->sh_link], hsecname,
2965 			    ndx, *hash, symn, syms, file, bkts, flags, 0);
2966 
2967 			/*
2968 			 * Determine if any other symbols are chained to this
2969 			 * bucket.
2970 			 */
2971 			_ndx = chain[*hash];
2972 			_cnt = 1;
2973 			while (_ndx) {
2974 				hash_entry(_cache, &cache[sshdr->sh_link],
2975 				    hsecname, ndx, _ndx, symn, syms, file,
2976 				    bkts, flags, 1);
2977 				_ndx = chain[_ndx];
2978 				_cnt++;
2979 			}
2980 
2981 			if (_cnt >= MAXCOUNT) {
2982 				(void) fprintf(stderr,
2983 				    MSG_INTL(MSG_HASH_OVERFLW), file,
2984 				    _cache->c_name, EC_WORD(ndx),
2985 				    EC_WORD(_cnt));
2986 			} else
2987 				count[_cnt]++;
2988 		}
2989 		break;
2990 	}
2991 
2992 	/*
2993 	 * Print out the count information.
2994 	 */
2995 	bkts = cnt = 0;
2996 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2997 
2998 	for (ndx = 0; ndx < MAXCOUNT; ndx++) {
2999 		Word	_cnt;
3000 
3001 		if ((_cnt = count[ndx]) == 0)
3002 			continue;
3003 
3004 		(void) snprintf(number, MAXNDXSIZE,
3005 		    MSG_ORIG(MSG_FMT_INTEGER), _cnt);
3006 		dbg_print(0, MSG_INTL(MSG_ELF_HASH_BKTS1), number,
3007 		    EC_WORD(ndx));
3008 		bkts += _cnt;
3009 		cnt += (Word)(ndx * _cnt);
3010 	}
3011 	if (cnt) {
3012 		(void) snprintf(number, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INTEGER),
3013 		    bkts);
3014 		dbg_print(0, MSG_INTL(MSG_ELF_HASH_BKTS2), number,
3015 		    EC_WORD(cnt));
3016 	}
3017 }
3018 
3019 static void
3020 group(Cache *cache, Word shnum, const char *file, uint_t flags)
3021 {
3022 	Word	scnt;
3023 
3024 	for (scnt = 1; scnt < shnum; scnt++) {
3025 		Cache	*_cache = &cache[scnt];
3026 		Shdr	*shdr = _cache->c_shdr;
3027 		Word	*grpdata, gcnt, grpcnt, symnum, unknown;
3028 		Cache	*symsec, *strsec;
3029 		Sym	*syms, *sym;
3030 		char	flgstrbuf[MSG_GRP_COMDAT_SIZE + 10];
3031 
3032 		if (shdr->sh_type != SHT_GROUP)
3033 			continue;
3034 		if (!match(0, _cache->c_name, scnt))
3035 			continue;
3036 		if ((_cache->c_data == NULL) ||
3037 		    ((grpdata = (Word *)_cache->c_data->d_buf) == NULL))
3038 			continue;
3039 		grpcnt = shdr->sh_size / sizeof (Word);
3040 
3041 		/*
3042 		 * Get the data buffer for the associated symbol table and
3043 		 * string table.
3044 		 */
3045 		if (stringtbl(cache, 1, scnt, shnum, file,
3046 		    &symnum, &symsec, &strsec) == 0)
3047 			return;
3048 
3049 		syms = symsec->c_data->d_buf;
3050 
3051 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
3052 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_GRP), _cache->c_name);
3053 		dbg_print(0, MSG_INTL(MSG_GRP_TITLE));
3054 
3055 		/*
3056 		 * The first element of the group defines the group.  The
3057 		 * associated symbol is defined by the sh_link field.
3058 		 */
3059 		if ((shdr->sh_info == SHN_UNDEF) || (shdr->sh_info > symnum)) {
3060 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHINFO),
3061 			    file, _cache->c_name, EC_WORD(shdr->sh_info));
3062 			return;
3063 		}
3064 
3065 		(void) strcpy(flgstrbuf, MSG_ORIG(MSG_STR_OSQBRKT));
3066 		if (grpdata[0] & GRP_COMDAT) {
3067 			(void) strcat(flgstrbuf, MSG_ORIG(MSG_GRP_COMDAT));
3068 		}
3069 		if ((unknown = (grpdata[0] & ~GRP_COMDAT)) != 0) {
3070 			size_t	len = strlen(flgstrbuf);
3071 
3072 			(void) snprintf(&flgstrbuf[len],
3073 			    (MSG_GRP_COMDAT_SIZE + 10 - len),
3074 			    MSG_ORIG(MSG_GRP_UNKNOWN), unknown);
3075 		}
3076 		(void) strcat(flgstrbuf, MSG_ORIG(MSG_STR_CSQBRKT));
3077 		sym = (Sym *)(syms + shdr->sh_info);
3078 
3079 		dbg_print(0, MSG_INTL(MSG_GRP_SIGNATURE), flgstrbuf,
3080 		    demangle(string(_cache, 0, strsec, file, sym->st_name),
3081 		    flags));
3082 
3083 		for (gcnt = 1; gcnt < grpcnt; gcnt++) {
3084 			char		index[MAXNDXSIZE];
3085 			const char	*name;
3086 
3087 			(void) snprintf(index, MAXNDXSIZE,
3088 			    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(gcnt));
3089 
3090 			if (grpdata[gcnt] >= shnum)
3091 				name = MSG_INTL(MSG_GRP_INVALSCN);
3092 			else
3093 				name = cache[grpdata[gcnt]].c_name;
3094 
3095 			(void) printf(MSG_ORIG(MSG_GRP_ENTRY), index, name,
3096 			    EC_XWORD(grpdata[gcnt]));
3097 		}
3098 	}
3099 }
3100 
3101 static void
3102 got(Cache *cache, Word shnum, Ehdr *ehdr, const char *file, uint_t flags)
3103 {
3104 	Cache		*gotcache = NULL, *symtab = NULL;
3105 	Addr		gotbgn, gotend;
3106 	Shdr		*gotshdr;
3107 	Word		cnt, gotents, gotndx;
3108 	size_t		gentsize;
3109 	Got_info	*gottable;
3110 	char		*gotdata;
3111 	Sym		*gotsym;
3112 	Xword		gotsymaddr;
3113 
3114 	/*
3115 	 * First, find the got.
3116 	 */
3117 	for (cnt = 1; cnt < shnum; cnt++) {
3118 		if (strncmp(cache[cnt].c_name, MSG_ORIG(MSG_ELF_GOT),
3119 		    MSG_ELF_GOT_SIZE) == 0) {
3120 			gotcache = &cache[cnt];
3121 			break;
3122 		}
3123 	}
3124 	if (gotcache == NULL)
3125 		return;
3126 
3127 	/*
3128 	 * A got section within a relocatable object is suspicious.
3129 	 */
3130 	if (ehdr->e_type == ET_REL) {
3131 		(void) fprintf(stderr, MSG_INTL(MSG_GOT_UNEXPECTED), file,
3132 		    gotcache->c_name);
3133 	}
3134 
3135 	gotshdr = gotcache->c_shdr;
3136 	if (gotshdr->sh_size == 0) {
3137 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
3138 		    file, gotcache->c_name);
3139 		return;
3140 	}
3141 
3142 	gotbgn = gotshdr->sh_addr;
3143 	gotend = gotbgn + gotshdr->sh_size;
3144 
3145 	/*
3146 	 * Some architectures don't properly set the sh_entsize for the GOT
3147 	 * table.  If it's not set, default to a size of a pointer.
3148 	 */
3149 	if ((gentsize = gotshdr->sh_entsize) == 0)
3150 		gentsize = sizeof (Xword);
3151 
3152 	if (gotcache->c_data == NULL)
3153 		return;
3154 
3155 	/* LINTED */
3156 	gotents = (Word)(gotshdr->sh_size / gentsize);
3157 	gotdata = gotcache->c_data->d_buf;
3158 
3159 	if ((gottable = calloc(gotents, sizeof (Got_info))) == 0) {
3160 		int err = errno;
3161 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC), file,
3162 		    strerror(err));
3163 		return;
3164 	}
3165 
3166 	/*
3167 	 * Now we scan through all the sections looking for any relocations
3168 	 * that may be against the GOT.  Since these may not be isolated to a
3169 	 * .rel[a].got section we check them all.
3170 	 * While scanning sections save the symbol table entry (a symtab
3171 	 * overriding a dynsym) so that we can lookup _GLOBAL_OFFSET_TABLE_.
3172 	 */
3173 	for (cnt = 1; cnt < shnum; cnt++) {
3174 		Word		type, symnum;
3175 		Xword		relndx, relnum, relsize;
3176 		void		*rels;
3177 		Sym		*syms;
3178 		Cache		*symsec, *strsec;
3179 		Cache		*_cache = &cache[cnt];
3180 		Shdr		*shdr;
3181 
3182 		shdr = _cache->c_shdr;
3183 		type = shdr->sh_type;
3184 
3185 		if ((symtab == 0) && (type == SHT_DYNSYM)) {
3186 			symtab = _cache;
3187 			continue;
3188 		}
3189 		if (type == SHT_SYMTAB) {
3190 			symtab = _cache;
3191 			continue;
3192 		}
3193 		if ((type != SHT_RELA) && (type != SHT_REL))
3194 			continue;
3195 
3196 		/*
3197 		 * Decide entry size.
3198 		 */
3199 		if (((relsize = shdr->sh_entsize) == 0) ||
3200 		    (relsize > shdr->sh_size)) {
3201 			if (type == SHT_RELA)
3202 				relsize = sizeof (Rela);
3203 			else
3204 				relsize = sizeof (Rel);
3205 		}
3206 
3207 		/*
3208 		 * Determine the number of relocations available.
3209 		 */
3210 		if (shdr->sh_size == 0) {
3211 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
3212 			    file, _cache->c_name);
3213 			continue;
3214 		}
3215 		if (_cache->c_data == NULL)
3216 			continue;
3217 
3218 		rels = _cache->c_data->d_buf;
3219 		relnum = shdr->sh_size / relsize;
3220 
3221 		/*
3222 		 * Get the data buffer for the associated symbol table and
3223 		 * string table.
3224 		 */
3225 		if (stringtbl(cache, 1, cnt, shnum, file,
3226 		    &symnum, &symsec, &strsec) == 0)
3227 			continue;
3228 
3229 		syms = symsec->c_data->d_buf;
3230 
3231 		/*
3232 		 * Loop through the relocation entries.
3233 		 */
3234 		for (relndx = 0; relndx < relnum; relndx++,
3235 		    rels = (void *)((char *)rels + relsize)) {
3236 			char		section[BUFSIZ];
3237 			Addr		offset;
3238 			Got_info	*gip;
3239 			Word		symndx, reltype;
3240 			Rela		*rela;
3241 			Rel		*rel;
3242 
3243 			/*
3244 			 * Unravel the relocation.
3245 			 */
3246 			if (type == SHT_RELA) {
3247 				rela = (Rela *)rels;
3248 				symndx = ELF_R_SYM(rela->r_info);
3249 				reltype = ELF_R_TYPE(rela->r_info);
3250 				offset = rela->r_offset;
3251 			} else {
3252 				rel = (Rel *)rels;
3253 				symndx = ELF_R_SYM(rel->r_info);
3254 				reltype = ELF_R_TYPE(rel->r_info);
3255 				offset = rel->r_offset;
3256 			}
3257 
3258 			/*
3259 			 * Only pay attention to relocations against the GOT.
3260 			 */
3261 			if ((offset < gotbgn) || (offset >= gotend))
3262 				continue;
3263 
3264 			/* LINTED */
3265 			gotndx = (Word)((offset - gotbgn) /
3266 			    gotshdr->sh_entsize);
3267 			gip = &gottable[gotndx];
3268 
3269 			if (gip->g_reltype != 0) {
3270 				(void) fprintf(stderr,
3271 				    MSG_INTL(MSG_GOT_MULTIPLE), file,
3272 				    EC_WORD(gotndx), EC_ADDR(offset));
3273 				continue;
3274 			}
3275 
3276 			if (symndx)
3277 				gip->g_symname = relsymname(cache, _cache,
3278 				    strsec, symndx, symnum, relndx, syms,
3279 				    section, BUFSIZ, file, flags);
3280 			gip->g_reltype = reltype;
3281 			gip->g_rel = rels;
3282 		}
3283 	}
3284 
3285 	if (symlookup(MSG_ORIG(MSG_GOT_SYM), cache, shnum, &gotsym, symtab,
3286 	    file))
3287 		gotsymaddr = gotsym->st_value;
3288 	else
3289 		gotsymaddr = gotbgn;
3290 
3291 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
3292 	dbg_print(0, MSG_INTL(MSG_ELF_SCN_GOT), gotcache->c_name);
3293 	Elf_got_title(0);
3294 
3295 	for (gotndx = 0; gotndx < gotents; gotndx++) {
3296 		Got_info	*gip;
3297 		Sword		gindex;
3298 		Addr		gaddr;
3299 		Xword		gotentry;
3300 
3301 		gip = &gottable[gotndx];
3302 
3303 		gaddr = gotbgn + (gotndx * gentsize);
3304 		gindex = (Sword)(gaddr - gotsymaddr) / (Sword)gentsize;
3305 
3306 		if (gentsize == sizeof (Word))
3307 			/* LINTED */
3308 			gotentry = (Xword)(*((Word *)(gotdata) + gotndx));
3309 		else
3310 			/* LINTED */
3311 			gotentry = *((Xword *)(gotdata) + gotndx);
3312 
3313 		Elf_got_entry(0, gindex, gaddr, gotentry, ehdr->e_machine,
3314 		    gip->g_reltype, gip->g_rel, gip->g_symname);
3315 	}
3316 	free(gottable);
3317 }
3318 
3319 void
3320 checksum(Elf *elf)
3321 {
3322 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
3323 	dbg_print(0, MSG_INTL(MSG_STR_CHECKSUM), elf_checksum(elf));
3324 }
3325 
3326 /*
3327  * This variable is used by regular() to communicate the address of
3328  * the section header cache to sort_shdr_ndx_arr(). Unfortunately,
3329  * the qsort() interface does not include a userdata argument by which
3330  * such arbitrary data can be passed, so we are stuck using global data.
3331  */
3332 static Cache *sort_shdr_ndx_arr_cache;
3333 
3334 
3335 /*
3336  * Used with qsort() to sort the section indices so that they can be
3337  * used to access the section headers in order of increasing data offset.
3338  *
3339  * entry:
3340  *	sort_shdr_ndx_arr_cache - Contains address of
3341  *		section header cache.
3342  *	v1, v2 - Point at elements of sort_shdr_bits array to be compared.
3343  *
3344  * exit:
3345  *	Returns -1 (less than), 0 (equal) or 1 (greater than).
3346  */
3347 static int
3348 sort_shdr_ndx_arr(const void *v1, const void *v2)
3349 {
3350 	Cache	*cache1 = sort_shdr_ndx_arr_cache + *((size_t *)v1);
3351 	Cache	*cache2 = sort_shdr_ndx_arr_cache + *((size_t *)v2);
3352 
3353 	if (cache1->c_shdr->sh_offset < cache2->c_shdr->sh_offset)
3354 		return (-1);
3355 
3356 	if (cache1->c_shdr->sh_offset > cache2->c_shdr->sh_offset)
3357 		return (1);
3358 
3359 	return (0);
3360 }
3361 
3362 
3363 static int
3364 shdr_cache(const char *file, Elf *elf, Ehdr *ehdr, size_t shstrndx,
3365     size_t shnum, Cache **cache_ret)
3366 {
3367 	Elf_Scn		*scn;
3368 	Elf_Data	*data;
3369 	size_t		ndx;
3370 	Shdr		*nameshdr;
3371 	char		*names = 0;
3372 	Cache		*cache, *_cache;
3373 	size_t		*shdr_ndx_arr, shdr_ndx_arr_cnt;
3374 
3375 
3376 	/*
3377 	 * Obtain the .shstrtab data buffer to provide the required section
3378 	 * name strings.
3379 	 */
3380 	if (shstrndx == SHN_UNDEF) {
3381 		/*
3382 		 * It is rare, but legal, for an object to lack a
3383 		 * header string table section.
3384 		 */
3385 		names = NULL;
3386 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_NOSHSTRSEC), file);
3387 	} else if ((scn = elf_getscn(elf, shstrndx)) == NULL) {
3388 		failure(file, MSG_ORIG(MSG_ELF_GETSCN));
3389 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SHDR),
3390 		    EC_XWORD(shstrndx));
3391 
3392 	} else if ((data = elf_getdata(scn, NULL)) == NULL) {
3393 		failure(file, MSG_ORIG(MSG_ELF_GETDATA));
3394 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_DATA),
3395 		    EC_XWORD(shstrndx));
3396 
3397 	} else if ((nameshdr = elf_getshdr(scn)) == NULL) {
3398 		failure(file, MSG_ORIG(MSG_ELF_GETSHDR));
3399 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN),
3400 		    EC_WORD(elf_ndxscn(scn)));
3401 
3402 	} else if ((names = data->d_buf) == 0)
3403 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_SHSTRNULL), file);
3404 
3405 	/*
3406 	 * Allocate a cache to maintain a descriptor for each section.
3407 	 */
3408 	if ((*cache_ret = cache = malloc(shnum * sizeof (Cache))) == NULL) {
3409 		int err = errno;
3410 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
3411 		    file, strerror(err));
3412 		return (0);
3413 	}
3414 
3415 	*cache = cache_init;
3416 	_cache = cache;
3417 	_cache++;
3418 
3419 	/*
3420 	 * Allocate an array that will hold the section index for
3421 	 * each section that has data in the ELF file:
3422 	 *
3423 	 *	- Is not a NOBITS section
3424 	 *	- Data has non-zero length
3425 	 *
3426 	 * Note that shnum is an upper bound on the size required. It
3427 	 * is likely that we won't use a few of these array elements.
3428 	 * Allocating a modest amount of extra memory in this case means
3429 	 * that we can avoid an extra loop to count the number of needed
3430 	 * items, and can fill this array immediately in the first loop
3431 	 * below.
3432 	 */
3433 	if ((shdr_ndx_arr = malloc(shnum * sizeof (*shdr_ndx_arr))) == NULL) {
3434 		int err = errno;
3435 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
3436 		    file, strerror(err));
3437 		return (0);
3438 	}
3439 	shdr_ndx_arr_cnt = 0;
3440 
3441 	/*
3442 	 * Traverse the sections of the file.  This gathering of data is
3443 	 * carried out in two passes.  First, the section headers are captured
3444 	 * and the section header names are evaluated.  A verification pass is
3445 	 * then carried out over the section information.  Files have been
3446 	 * known to exhibit overlapping (and hence erroneous) section header
3447 	 * information.
3448 	 *
3449 	 * Finally, the data for each section is obtained.  This processing is
3450 	 * carried out after section verification because should any section
3451 	 * header overlap occur, and a file needs translating (ie. xlate'ing
3452 	 * information from a non-native architecture file), then the process
3453 	 * of translation can corrupt the section header information.  Of
3454 	 * course, if there is any section overlap, the data related to the
3455 	 * sections is going to be compromised.  However, it is the translation
3456 	 * of this data that has caused problems with elfdump()'s ability to
3457 	 * extract the data.
3458 	 */
3459 	for (ndx = 1, scn = NULL; scn = elf_nextscn(elf, scn);
3460 	    ndx++, _cache++) {
3461 		char	scnndxnm[100];
3462 
3463 		_cache->c_ndx = ndx;
3464 		_cache->c_scn = scn;
3465 
3466 		if ((_cache->c_shdr = elf_getshdr(scn)) == NULL) {
3467 			failure(file, MSG_ORIG(MSG_ELF_GETSHDR));
3468 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN),
3469 			    EC_WORD(elf_ndxscn(scn)));
3470 		}
3471 
3472 		/*
3473 		 * If this section has data in the file, include it in
3474 		 * the array of sections to check for address overlap.
3475 		 */
3476 		if ((_cache->c_shdr->sh_size != 0) &&
3477 		    (_cache->c_shdr->sh_type != SHT_NOBITS))
3478 			shdr_ndx_arr[shdr_ndx_arr_cnt++] = ndx;
3479 
3480 		/*
3481 		 * If a shstrtab exists, assign the section name.
3482 		 */
3483 		if (names && _cache->c_shdr) {
3484 			if (_cache->c_shdr->sh_name &&
3485 			    /* LINTED */
3486 			    (nameshdr->sh_size > _cache->c_shdr->sh_name)) {
3487 				_cache->c_name =
3488 				    names + _cache->c_shdr->sh_name;
3489 				continue;
3490 			}
3491 
3492 			/*
3493 			 * Generate an error if the section name index is zero
3494 			 * or exceeds the shstrtab data.  Fall through to
3495 			 * fabricate a section name.
3496 			 */
3497 			if ((_cache->c_shdr->sh_name == 0) ||
3498 			    /* LINTED */
3499 			    (nameshdr->sh_size <= _cache->c_shdr->sh_name)) {
3500 				(void) fprintf(stderr,
3501 				    MSG_INTL(MSG_ERR_BADSHNAME), file,
3502 				    EC_WORD(ndx),
3503 				    EC_XWORD(_cache->c_shdr->sh_name));
3504 			}
3505 		}
3506 
3507 		/*
3508 		 * If there exists no shstrtab data, or a section header has no
3509 		 * name (an invalid index of 0), then compose a name for the
3510 		 * section.
3511 		 */
3512 		(void) snprintf(scnndxnm, sizeof (scnndxnm),
3513 		    MSG_INTL(MSG_FMT_SCNNDX), ndx);
3514 
3515 		if ((_cache->c_name = malloc(strlen(scnndxnm) + 1)) == NULL) {
3516 			int err = errno;
3517 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
3518 			    file, strerror(err));
3519 			return (0);
3520 		}
3521 		(void) strcpy(_cache->c_name, scnndxnm);
3522 	}
3523 
3524 	/*
3525 	 * Having collected all the sections, validate their address range.
3526 	 * Cases have existed where the section information has been invalid.
3527 	 * This can lead to all sorts of other, hard to diagnose errors, as
3528 	 * each section is processed individually (ie. with elf_getdata()).
3529 	 * Here, we carry out some address comparisons to catch a family of
3530 	 * overlapping memory issues we have observed (likely, there are others
3531 	 * that we have yet to discover).
3532 	 *
3533 	 * Note, should any memory overlap occur, obtaining any additional
3534 	 * data from the file is questionable.  However, it might still be
3535 	 * possible to inspect the ELF header, Programs headers, or individual
3536 	 * sections, so rather than bailing on an error condition, continue
3537 	 * processing to see if any data can be salvaged.
3538 	 */
3539 	if (shdr_ndx_arr_cnt > 1) {
3540 		sort_shdr_ndx_arr_cache = cache;
3541 		qsort(shdr_ndx_arr, shdr_ndx_arr_cnt,
3542 		    sizeof (*shdr_ndx_arr), sort_shdr_ndx_arr);
3543 	}
3544 	for (ndx = 0; ndx < shdr_ndx_arr_cnt; ndx++) {
3545 		Cache	*_cache = cache + shdr_ndx_arr[ndx];
3546 		Shdr	*shdr = _cache->c_shdr;
3547 		Off	bgn1, bgn = shdr->sh_offset;
3548 		Off	end1, end = shdr->sh_offset + shdr->sh_size;
3549 		size_t	ndx1;
3550 
3551 		/*
3552 		 * Check the section against all following ones, reporting
3553 		 * any overlaps. Since we've sorted the sections by offset,
3554 		 * we can stop after the first comparison that fails. There
3555 		 * are no overlaps in a properly formed ELF file, in which
3556 		 * case this algorithm runs in O(n) time. This will degenerate
3557 		 * to O(n^2) for a completely broken file. Such a file is
3558 		 * (1) highly unlikely, and (2) unusable, so it is reasonable
3559 		 * for the analysis to take longer.
3560 		 */
3561 		for (ndx1 = ndx + 1; ndx1 < shdr_ndx_arr_cnt; ndx1++) {
3562 			Cache	*_cache1 = cache + shdr_ndx_arr[ndx1];
3563 			Shdr	*shdr1 = _cache1->c_shdr;
3564 
3565 			bgn1 = shdr1->sh_offset;
3566 			end1 = shdr1->sh_offset + shdr1->sh_size;
3567 
3568 			if (((bgn1 <= bgn) && (end1 > bgn)) ||
3569 			    ((bgn1 < end) && (end1 >= end))) {
3570 				(void) fprintf(stderr,
3571 				    MSG_INTL(MSG_ERR_SECMEMOVER), file,
3572 				    EC_WORD(elf_ndxscn(_cache->c_scn)),
3573 				    _cache->c_name, EC_OFF(bgn), EC_OFF(end),
3574 				    EC_WORD(elf_ndxscn(_cache1->c_scn)),
3575 				    _cache1->c_name, EC_OFF(bgn1),
3576 				    EC_OFF(end1));
3577 			} else {	/* No overlap, so can stop */
3578 				break;
3579 			}
3580 		}
3581 
3582 		/*
3583 		 * In addition to checking for sections overlapping
3584 		 * each other (done above), we should also make sure
3585 		 * the section doesn't overlap the section header array.
3586 		 */
3587 		bgn1 = ehdr->e_shoff;
3588 		end1 = ehdr->e_shoff + (ehdr->e_shentsize * ehdr->e_shnum);
3589 
3590 		if (((bgn1 <= bgn) && (end1 > bgn)) ||
3591 		    ((bgn1 < end) && (end1 >= end))) {
3592 			(void) fprintf(stderr,
3593 			    MSG_INTL(MSG_ERR_SHDRMEMOVER), file, EC_OFF(bgn1),
3594 			    EC_OFF(end1),
3595 			    EC_WORD(elf_ndxscn(_cache->c_scn)),
3596 			    _cache->c_name, EC_OFF(bgn), EC_OFF(end));
3597 		}
3598 	}
3599 
3600 	/*
3601 	 * Obtain the data for each section.
3602 	 */
3603 	for (ndx = 1; ndx < shnum; ndx++) {
3604 		Cache	*_cache = &cache[ndx];
3605 		Elf_Scn	*scn = _cache->c_scn;
3606 
3607 		if ((_cache->c_data = elf_getdata(scn, NULL)) == NULL) {
3608 			failure(file, MSG_ORIG(MSG_ELF_GETDATA));
3609 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCNDATA),
3610 			    EC_WORD(elf_ndxscn(scn)));
3611 		}
3612 	}
3613 
3614 	return (1);
3615 }
3616 
3617 
3618 
3619 void
3620 regular(const char *file, int fd, Elf *elf, uint_t flags, int wfd)
3621 {
3622 	Elf_Scn		*scn;
3623 	Ehdr		*ehdr;
3624 	size_t		ndx, shstrndx, shnum, phnum;
3625 	Shdr		*shdr;
3626 	Cache		*cache;
3627 	VERSYM_STATE	versym;
3628 
3629 	if ((ehdr = elf_getehdr(elf)) == NULL) {
3630 		failure(file, MSG_ORIG(MSG_ELF_GETEHDR));
3631 		return;
3632 	}
3633 
3634 	if (elf_getshnum(elf, &shnum) == 0) {
3635 		failure(file, MSG_ORIG(MSG_ELF_GETSHNUM));
3636 		return;
3637 	}
3638 
3639 	if (elf_getshstrndx(elf, &shstrndx) == 0) {
3640 		failure(file, MSG_ORIG(MSG_ELF_GETSHSTRNDX));
3641 		return;
3642 	}
3643 
3644 	if (elf_getphnum(elf, &phnum) == 0) {
3645 		failure(file, MSG_ORIG(MSG_ELF_GETPHNUM));
3646 		return;
3647 	}
3648 	/*
3649 	 * If the user requested section headers derived from the
3650 	 * program headers (-P option) and this file doesn't have
3651 	 * any program headers (i.e. ET_REL), then we can't do it.
3652 	 */
3653 	if ((phnum == 0) && (flags & FLG_FAKESHDR)) {
3654 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_PNEEDSPH), file);
3655 		return;
3656 	}
3657 
3658 
3659 	if ((scn = elf_getscn(elf, 0)) != NULL) {
3660 		if ((shdr = elf_getshdr(scn)) == NULL) {
3661 			failure(file, MSG_ORIG(MSG_ELF_GETSHDR));
3662 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN), 0);
3663 			return;
3664 		}
3665 	} else
3666 		shdr = 0;
3667 
3668 	/*
3669 	 * Print the elf header.
3670 	 */
3671 	if (flags & FLG_EHDR)
3672 		Elf_ehdr(0, ehdr, shdr);
3673 
3674 	/*
3675 	 * If the section headers or program headers have inadequate
3676 	 * alignment for the class of object, print a warning. libelf
3677 	 * can handle such files, but programs that use them can crash
3678 	 * when they dereference unaligned items.
3679 	 */
3680 	if (ehdr->e_phoff & (sizeof (Addr) - 1))
3681 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADPHDRALIGN), file);
3682 	if (ehdr->e_shoff & (sizeof (Addr) - 1))
3683 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHDRALIGN), file);
3684 
3685 	/*
3686 	 * Print the program headers.
3687 	 */
3688 	if ((flags & FLG_PHDR) && (phnum != 0)) {
3689 		Conv_inv_buf_t	inv_buf;
3690 		Phdr		*phdr;
3691 
3692 		if ((phdr = elf_getphdr(elf)) == NULL) {
3693 			failure(file, MSG_ORIG(MSG_ELF_GETPHDR));
3694 			return;
3695 		}
3696 
3697 		for (ndx = 0; ndx < phnum; phdr++, ndx++) {
3698 			if (!match(0, conv_phdr_type(ehdr->e_machine,
3699 			    phdr->p_type, CONV_FMT_ALT_FILE, &inv_buf), ndx))
3700 				continue;
3701 
3702 			dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
3703 			dbg_print(0, MSG_INTL(MSG_ELF_PHDR), EC_WORD(ndx));
3704 			Elf_phdr(0, ehdr->e_machine, phdr);
3705 		}
3706 	}
3707 
3708 	/*
3709 	 * Decide how to proceed if there are no sections, if there's just
3710 	 * one section (the first section can act as an extension of the
3711 	 * ELF header), or if only program header information was requested.
3712 	 */
3713 	if ((shnum <= 1) || (flags && (flags & ~(FLG_EHDR | FLG_PHDR)) == 0)) {
3714 		/* If a core file, display the note and return */
3715 		if ((ehdr->e_type == ET_CORE) && (flags & FLG_NOTE)) {
3716 			note(0, shnum, file);
3717 			return;
3718 		}
3719 
3720 		/* If only program header info was requested, we're done */
3721 		if (flags && (flags & ~(FLG_EHDR | FLG_PHDR)) == 0)
3722 			return;
3723 
3724 		/*
3725 		 * Section headers are missing. Resort to synthesizing
3726 		 * section headers from the program headers.
3727 		 */
3728 		if ((flags & FLG_FAKESHDR) == 0) {
3729 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_NOSHDR), file);
3730 			flags |= FLG_FAKESHDR;
3731 		}
3732 	}
3733 
3734 	/*
3735 	 * Generate a cache of section headers and related information
3736 	 * for use by the rest of elfdump. If requested (or the file
3737 	 * contains no section headers), we generate a fake set of
3738 	 * headers from the information accessible from the program headers.
3739 	 * Otherwise, we use the real section headers contained in the file.
3740 	 */
3741 
3742 	if (flags & FLG_FAKESHDR) {
3743 		if (fake_shdr_cache(file, fd, elf, ehdr, &cache, &shnum) == 0)
3744 			return;
3745 	} else {
3746 		if (shdr_cache(file, elf, ehdr, shstrndx, shnum, &cache) == 0)
3747 			return;
3748 	}
3749 
3750 	/*
3751 	 * If -w was specified, find and write out the section(s) data.
3752 	 */
3753 	if (wfd) {
3754 		for (ndx = 1; ndx < shnum; ndx++) {
3755 			Cache	*_cache = &cache[ndx];
3756 
3757 			if (match(1, _cache->c_name, ndx) && _cache->c_data) {
3758 				(void) write(wfd, _cache->c_data->d_buf,
3759 				    _cache->c_data->d_size);
3760 			}
3761 		}
3762 	}
3763 
3764 	if (flags & FLG_SHDR)
3765 		sections(file, cache, shnum, ehdr);
3766 
3767 	if (flags & FLG_INTERP)
3768 		interp(file, cache, shnum, phnum, elf);
3769 
3770 	versions(cache, shnum, file, flags, &versym);
3771 
3772 	if (flags & FLG_SYMBOLS)
3773 		symbols(cache, shnum, ehdr, &versym, file, flags);
3774 
3775 	if (flags & FLG_SORT)
3776 		sunw_sort(cache, shnum, ehdr, &versym, file, flags);
3777 
3778 	if (flags & FLG_HASH)
3779 		hash(cache, shnum, file, flags);
3780 
3781 	if (flags & FLG_GOT)
3782 		got(cache, shnum, ehdr, file, flags);
3783 
3784 	if (flags & FLG_GROUP)
3785 		group(cache, shnum, file, flags);
3786 
3787 	if (flags & FLG_SYMINFO)
3788 		syminfo(cache, shnum, file);
3789 
3790 	if (flags & FLG_RELOC)
3791 		reloc(cache, shnum, ehdr, file, flags);
3792 
3793 	if (flags & FLG_DYNAMIC)
3794 		dynamic(cache, shnum, ehdr, file);
3795 
3796 	if (flags & FLG_NOTE)
3797 		note(cache, shnum, file);
3798 
3799 	if (flags & FLG_MOVE)
3800 		move(cache, shnum, file, flags);
3801 
3802 	if (flags & FLG_CHECKSUM)
3803 		checksum(elf);
3804 
3805 	if (flags & FLG_CAP)
3806 		cap(file, cache, shnum, phnum, ehdr, elf);
3807 
3808 	if (flags & FLG_UNWIND)
3809 		unwind(cache, shnum, phnum, ehdr, file, elf);
3810 
3811 
3812 	/* Release the memory used to cache section headers */
3813 	if (flags & FLG_FAKESHDR)
3814 		fake_shdr_cache_free(cache, shnum);
3815 	else
3816 		free(cache);
3817 }
3818