xref: /titanic_51/usr/src/cmd/sgs/elfdump/common/elfdump.c (revision 821da340f9590922ba8761a64fadda18843a0e88)
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  *	symndx_disp - Index to display. This may not be the same
1424  *		as symndx if the display is relative to the logical
1425  *		combination of the SUNW_ldynsym/dynsym tables.
1426  *	sym - Symbol to display
1427  */
1428 static void
1429 output_symbol(SYMTBL_STATE *state, Word symndx, Word disp_symndx, Sym *sym)
1430 {
1431 	/*
1432 	 * Symbol types for which we check that the specified
1433 	 * address/size land inside the target section.
1434 	 */
1435 	static const int addr_symtype[STT_NUM] = {
1436 		0,			/* STT_NOTYPE */
1437 		1,			/* STT_OBJECT */
1438 		1,			/* STT_FUNC */
1439 		0,			/* STT_SECTION */
1440 		0,			/* STT_FILE */
1441 		1,			/* STT_COMMON */
1442 		0,			/* STT_TLS */
1443 	};
1444 #if STT_NUM != (STT_TLS + 1)
1445 #error "STT_NUM has grown. Update addr_symtype[]"
1446 #endif
1447 
1448 	char		index[MAXNDXSIZE];
1449 	const char	*symname, *sec;
1450 	Versym		verndx;
1451 	int		gnuver;
1452 	uchar_t		type;
1453 	Shdr		*tshdr;
1454 	Word		shndx;
1455 	Conv_inv_buf_t	inv_buf;
1456 
1457 	/* Ensure symbol index is in range */
1458 	if (symndx >= state->symn) {
1459 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSORTNDX),
1460 		    state->file, state->secname, EC_WORD(symndx));
1461 		return;
1462 	}
1463 
1464 	/*
1465 	 * If we are using extended symbol indexes, find the
1466 	 * corresponding SHN_SYMTAB_SHNDX table.
1467 	 */
1468 	if ((sym->st_shndx == SHN_XINDEX) && (state->shxndx.checked == 0))
1469 		symbols_getxindex(state);
1470 
1471 	/* LINTED */
1472 	symname = string(state->seccache, symndx,
1473 	    &state->cache[state->seccache->c_shdr->sh_link], state->file,
1474 	    sym->st_name);
1475 
1476 	tshdr = 0;
1477 	sec = NULL;
1478 
1479 	if (state->ehdr->e_type == ET_CORE) {
1480 		sec = (char *)MSG_INTL(MSG_STR_UNKNOWN);
1481 	} else if (state->flags & FLG_FAKESHDR) {
1482 		/*
1483 		 * If we are using fake section headers derived from
1484 		 * the program headers, then the section indexes
1485 		 * in the symbols do not correspond to these headers.
1486 		 * The section names are not available, so all we can
1487 		 * do is to display them in numeric form.
1488 		 */
1489 		sec = conv_sym_shndx(sym->st_shndx, &inv_buf);
1490 	} else if ((sym->st_shndx < SHN_LORESERVE) &&
1491 	    (sym->st_shndx < state->shnum)) {
1492 		shndx = sym->st_shndx;
1493 		tshdr = state->cache[shndx].c_shdr;
1494 		sec = state->cache[shndx].c_name;
1495 	} else if (sym->st_shndx == SHN_XINDEX) {
1496 		if (state->shxndx.data) {
1497 			Word	_shxndx;
1498 
1499 			if (symndx > state->shxndx.n) {
1500 				(void) fprintf(stderr,
1501 				    MSG_INTL(MSG_ERR_BADSYMXINDEX1),
1502 				    state->file, state->secname,
1503 				    EC_WORD(symndx));
1504 			} else if ((_shxndx =
1505 			    state->shxndx.data[symndx]) > state->shnum) {
1506 				(void) fprintf(stderr,
1507 				    MSG_INTL(MSG_ERR_BADSYMXINDEX2),
1508 				    state->file, state->secname,
1509 				    EC_WORD(symndx), EC_WORD(_shxndx));
1510 			} else {
1511 				shndx = _shxndx;
1512 				tshdr = state->cache[shndx].c_shdr;
1513 				sec = state->cache[shndx].c_name;
1514 			}
1515 		} else {
1516 			(void) fprintf(stderr,
1517 			    MSG_INTL(MSG_ERR_BADSYMXINDEX3),
1518 			    state->file, state->secname, EC_WORD(symndx));
1519 		}
1520 	} else if ((sym->st_shndx < SHN_LORESERVE) &&
1521 	    (sym->st_shndx >= state->shnum)) {
1522 		(void) fprintf(stderr,
1523 		    MSG_INTL(MSG_ERR_BADSYM5), state->file,
1524 		    state->secname, demangle(symname, state->flags),
1525 		    sym->st_shndx);
1526 	}
1527 
1528 	/*
1529 	 * If versioning is available display the
1530 	 * version index. If not, then use 0.
1531 	 */
1532 	if (state->versym) {
1533 		Versym test_verndx;
1534 
1535 		verndx = test_verndx = state->versym->data[symndx];
1536 		gnuver = state->versym->gnu;
1537 
1538 		/*
1539 		 * Check to see if this is a defined symbol with a
1540 		 * version index that is outside the valid range for
1541 		 * the file. The interpretation of this depends on
1542 		 * the style of versioning used by the object.
1543 		 *
1544 		 * Versions >= VER_NDX_LORESERVE have special meanings,
1545 		 * and are exempt from this checking.
1546 		 *
1547 		 * GNU style version indexes use the top bit of the
1548 		 * 16-bit index value (0x8000) as the "hidden bit".
1549 		 * We must mask off this bit in order to compare
1550 		 * the version against the maximum value.
1551 		 */
1552 		if (gnuver)
1553 			test_verndx &= ~0x8000;
1554 
1555 		if ((test_verndx > state->versym->max_verndx) &&
1556 		    (verndx < VER_NDX_LORESERVE))
1557 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADVER),
1558 			    state->file, state->secname, EC_WORD(symndx),
1559 			    EC_HALF(test_verndx), state->versym->max_verndx);
1560 	} else {
1561 		verndx = 0;
1562 		gnuver = 0;
1563 	}
1564 
1565 	/*
1566 	 * Error checking for TLS.
1567 	 */
1568 	type = ELF_ST_TYPE(sym->st_info);
1569 	if (type == STT_TLS) {
1570 		if (tshdr &&
1571 		    (sym->st_shndx != SHN_UNDEF) &&
1572 		    ((tshdr->sh_flags & SHF_TLS) == 0)) {
1573 			(void) fprintf(stderr,
1574 			    MSG_INTL(MSG_ERR_BADSYM3), state->file,
1575 			    state->secname, demangle(symname, state->flags));
1576 		}
1577 	} else if ((type != STT_SECTION) && sym->st_size &&
1578 	    tshdr && (tshdr->sh_flags & SHF_TLS)) {
1579 		(void) fprintf(stderr,
1580 		    MSG_INTL(MSG_ERR_BADSYM4), state->file,
1581 		    state->secname, demangle(symname, state->flags));
1582 	}
1583 
1584 	/*
1585 	 * If a symbol with non-zero size has a type that
1586 	 * specifies an address, then make sure the location
1587 	 * it references is actually contained within the
1588 	 * section.  UNDEF symbols don't count in this case,
1589 	 * so we ignore them.
1590 	 *
1591 	 * The meaning of the st_value field in a symbol
1592 	 * depends on the type of object. For a relocatable
1593 	 * object, it is the offset within the section.
1594 	 * For sharable objects, it is the offset relative to
1595 	 * the base of the object, and for other types, it is
1596 	 * the virtual address. To get an offset within the
1597 	 * section for non-ET_REL files, we subtract the
1598 	 * base address of the section.
1599 	 */
1600 	if (addr_symtype[type] && (sym->st_size > 0) &&
1601 	    (sym->st_shndx != SHN_UNDEF) && ((sym->st_shndx < SHN_LORESERVE) ||
1602 	    (sym->st_shndx == SHN_XINDEX)) && (tshdr != NULL)) {
1603 		Word v = sym->st_value;
1604 			if (state->ehdr->e_type != ET_REL)
1605 			v -= tshdr->sh_addr;
1606 		if (((v + sym->st_size) > tshdr->sh_size)) {
1607 			(void) fprintf(stderr,
1608 			    MSG_INTL(MSG_ERR_BADSYM6), state->file,
1609 			    state->secname, demangle(symname, state->flags),
1610 			    EC_WORD(shndx), EC_XWORD(tshdr->sh_size),
1611 			    EC_XWORD(sym->st_value), EC_XWORD(sym->st_size));
1612 		}
1613 	}
1614 
1615 	(void) snprintf(index, MAXNDXSIZE,
1616 	    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(disp_symndx));
1617 	Elf_syms_table_entry(0, ELF_DBG_ELFDUMP, index,
1618 	    state->ehdr->e_machine, sym, verndx, gnuver, sec, symname);
1619 }
1620 
1621 /*
1622  * Search for and process any symbol tables.
1623  */
1624 void
1625 symbols(Cache *cache, Word shnum, Ehdr *ehdr, VERSYM_STATE *versym,
1626     const char *file, uint_t flags)
1627 {
1628 	SYMTBL_STATE state;
1629 	Cache *_cache;
1630 	Word secndx;
1631 
1632 	for (secndx = 1; secndx < shnum; secndx++) {
1633 		Word		symcnt;
1634 		Shdr		*shdr;
1635 
1636 		_cache = &cache[secndx];
1637 		shdr = _cache->c_shdr;
1638 
1639 		if ((shdr->sh_type != SHT_SYMTAB) &&
1640 		    (shdr->sh_type != SHT_DYNSYM) &&
1641 		    (shdr->sh_type != SHT_SUNW_LDYNSYM))
1642 			continue;
1643 		if (!match(0, _cache->c_name, secndx))
1644 			continue;
1645 
1646 		if (!init_symtbl_state(&state, cache, shnum, secndx, ehdr,
1647 		    versym, file, flags))
1648 			continue;
1649 		/*
1650 		 * Loop through the symbol tables entries.
1651 		 */
1652 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1653 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMTAB), state.secname);
1654 		Elf_syms_table_title(0, ELF_DBG_ELFDUMP);
1655 
1656 		for (symcnt = 0; symcnt < state.symn; symcnt++)
1657 			output_symbol(&state, symcnt, symcnt,
1658 			    state.sym + symcnt);
1659 	}
1660 }
1661 
1662 /*
1663  * Search for and process any SHT_SUNW_symsort or SHT_SUNW_tlssort sections.
1664  * These sections are always associated with the .SUNW_ldynsym./.dynsym pair.
1665  */
1666 static void
1667 sunw_sort(Cache *cache, Word shnum, Ehdr *ehdr, VERSYM_STATE *versym,
1668     const char *file, uint_t flags)
1669 {
1670 	SYMTBL_STATE	ldynsym_state,	dynsym_state;
1671 	Cache		*sortcache,	*symcache;
1672 	Shdr		*sortshdr,	*symshdr;
1673 	Word		sortsecndx,	symsecndx;
1674 	Word		ldynsym_cnt;
1675 	Word		*ndx;
1676 	Word		ndxn;
1677 	int		output_cnt = 0;
1678 	Conv_inv_buf_t	inv_buf;
1679 
1680 	for (sortsecndx = 1; sortsecndx < shnum; sortsecndx++) {
1681 
1682 		sortcache = &cache[sortsecndx];
1683 		sortshdr = sortcache->c_shdr;
1684 
1685 		if ((sortshdr->sh_type != SHT_SUNW_symsort) &&
1686 		    (sortshdr->sh_type != SHT_SUNW_tlssort))
1687 			continue;
1688 		if (!match(0, sortcache->c_name, sortsecndx))
1689 			continue;
1690 
1691 		/*
1692 		 * If the section references a SUNW_ldynsym, then we
1693 		 * expect to see the associated .dynsym immediately
1694 		 * following. If it references a .dynsym, there is no
1695 		 * SUNW_ldynsym. If it is any other type, then we don't
1696 		 * know what to do with it.
1697 		 */
1698 		if ((sortshdr->sh_link == 0) || (sortshdr->sh_link >= shnum)) {
1699 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
1700 			    file, sortcache->c_name,
1701 			    EC_WORD(sortshdr->sh_link));
1702 			continue;
1703 		}
1704 		symcache = &cache[sortshdr->sh_link];
1705 		symshdr = symcache->c_shdr;
1706 		symsecndx = sortshdr->sh_link;
1707 		ldynsym_cnt = 0;
1708 		switch (symshdr->sh_type) {
1709 		case SHT_SUNW_LDYNSYM:
1710 			if (!init_symtbl_state(&ldynsym_state, cache, shnum,
1711 			    symsecndx, ehdr, versym, file, flags))
1712 				continue;
1713 			ldynsym_cnt = ldynsym_state.symn;
1714 			/*
1715 			 * We know that the dynsym follows immediately
1716 			 * after the SUNW_ldynsym, and so, should be at
1717 			 * (sortshdr->sh_link + 1). However, elfdump is a
1718 			 * diagnostic tool, so we do the full paranoid
1719 			 * search instead.
1720 			 */
1721 			for (symsecndx = 1; symsecndx < shnum; symsecndx++) {
1722 				symcache = &cache[symsecndx];
1723 				symshdr = symcache->c_shdr;
1724 				if (symshdr->sh_type == SHT_DYNSYM)
1725 					break;
1726 			}
1727 			if (symsecndx >= shnum) {	/* Dynsym not found! */
1728 				(void) fprintf(stderr,
1729 				    MSG_INTL(MSG_ERR_NODYNSYM),
1730 				    file, sortcache->c_name);
1731 				continue;
1732 			}
1733 			/* Fallthrough to process associated dynsym */
1734 			/*FALLTHROUGH*/
1735 		case SHT_DYNSYM:
1736 			if (!init_symtbl_state(&dynsym_state, cache, shnum,
1737 			    symsecndx, ehdr, versym, file, flags))
1738 				continue;
1739 			break;
1740 		default:
1741 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADNDXSEC),
1742 			    file, sortcache->c_name, conv_sec_type(
1743 			    ehdr->e_machine, symshdr->sh_type, 0, &inv_buf));
1744 			continue;
1745 		}
1746 
1747 		/*
1748 		 * Output header
1749 		 */
1750 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1751 		if (ldynsym_cnt > 0) {
1752 			dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMSORT2),
1753 			    sortcache->c_name, ldynsym_state.secname,
1754 			    dynsym_state.secname);
1755 			/*
1756 			 * The data for .SUNW_ldynsym and dynsym sections
1757 			 * is supposed to be adjacent with SUNW_ldynsym coming
1758 			 * first. Check, and issue a warning if it isn't so.
1759 			 */
1760 			if (((ldynsym_state.sym + ldynsym_state.symn)
1761 			    != dynsym_state.sym) &&
1762 			    ((flags & FLG_FAKESHDR) == 0))
1763 				(void) fprintf(stderr,
1764 				    MSG_INTL(MSG_ERR_LDYNNOTADJ), file,
1765 				    ldynsym_state.secname,
1766 				    dynsym_state.secname);
1767 		} else {
1768 			dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMSORT1),
1769 			    sortcache->c_name, dynsym_state.secname);
1770 		}
1771 		Elf_syms_table_title(0, ELF_DBG_ELFDUMP);
1772 
1773 		/* If not first one, insert a line of whitespace */
1774 		if (output_cnt++ > 0)
1775 			dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1776 
1777 		/*
1778 		 * SUNW_dynsymsort and SUNW_dyntlssort are arrays of
1779 		 * symbol indices. Iterate over the array entries,
1780 		 * dispaying the referenced symbols.
1781 		 */
1782 		ndxn = sortshdr->sh_size / sortshdr->sh_entsize;
1783 		ndx = (Word *)sortcache->c_data->d_buf;
1784 		for (; ndxn-- > 0; ndx++) {
1785 			if (*ndx >= ldynsym_cnt) {
1786 				Word sec_ndx = *ndx - ldynsym_cnt;
1787 
1788 				output_symbol(&dynsym_state, sec_ndx,
1789 				    *ndx, dynsym_state.sym + sec_ndx);
1790 			} else {
1791 				output_symbol(&ldynsym_state, *ndx,
1792 				    *ndx, ldynsym_state.sym + *ndx);
1793 			}
1794 		}
1795 	}
1796 }
1797 
1798 /*
1799  * Search for and process any relocation sections.
1800  */
1801 static void
1802 reloc(Cache *cache, Word shnum, Ehdr *ehdr, const char *file,
1803     uint_t flags)
1804 {
1805 	Word	cnt;
1806 
1807 	for (cnt = 1; cnt < shnum; cnt++) {
1808 		Word		type, symnum;
1809 		Xword		relndx, relnum, relsize;
1810 		void		*rels;
1811 		Sym		*syms;
1812 		Cache		*symsec, *strsec;
1813 		Cache		*_cache = &cache[cnt];
1814 		Shdr		*shdr = _cache->c_shdr;
1815 		char		*relname = _cache->c_name;
1816 		Conv_inv_buf_t	inv_buf;
1817 
1818 		if (((type = shdr->sh_type) != SHT_RELA) &&
1819 		    (type != SHT_REL))
1820 			continue;
1821 		if (!match(0, relname, cnt))
1822 			continue;
1823 
1824 		/*
1825 		 * Decide entry size.
1826 		 */
1827 		if (((relsize = shdr->sh_entsize) == 0) ||
1828 		    (relsize > shdr->sh_size)) {
1829 			if (type == SHT_RELA)
1830 				relsize = sizeof (Rela);
1831 			else
1832 				relsize = sizeof (Rel);
1833 		}
1834 
1835 		/*
1836 		 * Determine the number of relocations available.
1837 		 */
1838 		if (shdr->sh_size == 0) {
1839 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
1840 			    file, relname);
1841 			continue;
1842 		}
1843 		if (_cache->c_data == NULL)
1844 			continue;
1845 
1846 		rels = _cache->c_data->d_buf;
1847 		relnum = shdr->sh_size / relsize;
1848 
1849 		/*
1850 		 * Get the data buffer for the associated symbol table and
1851 		 * string table.
1852 		 */
1853 		if (stringtbl(cache, 1, cnt, shnum, file,
1854 		    &symnum, &symsec, &strsec) == 0)
1855 			continue;
1856 
1857 		syms = symsec->c_data->d_buf;
1858 
1859 		/*
1860 		 * Loop through the relocation entries.
1861 		 */
1862 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1863 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_RELOC), _cache->c_name);
1864 		Elf_reloc_title(0, ELF_DBG_ELFDUMP, type);
1865 
1866 		for (relndx = 0; relndx < relnum; relndx++,
1867 		    rels = (void *)((char *)rels + relsize)) {
1868 			char		section[BUFSIZ];
1869 			const char	*symname;
1870 			Word		symndx, reltype;
1871 			Rela		*rela;
1872 			Rel		*rel;
1873 
1874 			/*
1875 			 * Unravel the relocation and determine the symbol with
1876 			 * which this relocation is associated.
1877 			 */
1878 			if (type == SHT_RELA) {
1879 				rela = (Rela *)rels;
1880 				symndx = ELF_R_SYM(rela->r_info);
1881 				reltype = ELF_R_TYPE(rela->r_info);
1882 			} else {
1883 				rel = (Rel *)rels;
1884 				symndx = ELF_R_SYM(rel->r_info);
1885 				reltype = ELF_R_TYPE(rel->r_info);
1886 			}
1887 
1888 			symname = relsymname(cache, _cache, strsec, symndx,
1889 			    symnum, relndx, syms, section, BUFSIZ, file,
1890 			    flags);
1891 
1892 			/*
1893 			 * A zero symbol index is only valid for a few
1894 			 * relocations.
1895 			 */
1896 			if (symndx == 0) {
1897 				Half	mach = ehdr->e_machine;
1898 				int	badrel = 0;
1899 
1900 				if ((mach == EM_SPARC) ||
1901 				    (mach == EM_SPARC32PLUS) ||
1902 				    (mach == EM_SPARCV9)) {
1903 					if ((reltype != R_SPARC_NONE) &&
1904 					    (reltype != R_SPARC_REGISTER) &&
1905 					    (reltype != R_SPARC_RELATIVE))
1906 						badrel++;
1907 				} else if (mach == EM_386) {
1908 					if ((reltype != R_386_NONE) &&
1909 					    (reltype != R_386_RELATIVE))
1910 						badrel++;
1911 				} else if (mach == EM_AMD64) {
1912 					if ((reltype != R_AMD64_NONE) &&
1913 					    (reltype != R_AMD64_RELATIVE))
1914 						badrel++;
1915 				}
1916 
1917 				if (badrel) {
1918 					(void) fprintf(stderr,
1919 					    MSG_INTL(MSG_ERR_BADREL1), file,
1920 					    conv_reloc_type(mach, reltype,
1921 					    0, &inv_buf));
1922 				}
1923 			}
1924 
1925 			Elf_reloc_entry_1(0, ELF_DBG_ELFDUMP,
1926 			    MSG_ORIG(MSG_STR_EMPTY), ehdr->e_machine, type,
1927 			    rels, relname, symname, 0);
1928 		}
1929 	}
1930 }
1931 
1932 /*
1933  * Search for and process a .dynamic section.
1934  */
1935 static void
1936 dynamic(Cache *cache, Word shnum, Ehdr *ehdr, const char *file)
1937 {
1938 	Word	cnt;
1939 
1940 	for (cnt = 1; cnt < shnum; cnt++) {
1941 		Dyn	*dyn;
1942 		ulong_t	numdyn;
1943 		int	ndx, end_ndx;
1944 		Cache	*_cache = &cache[cnt], *strsec;
1945 		Shdr	*shdr = _cache->c_shdr;
1946 
1947 		if (shdr->sh_type != SHT_DYNAMIC)
1948 			continue;
1949 
1950 		/*
1951 		 * Verify the associated string table section.
1952 		 */
1953 		if (stringtbl(cache, 0, cnt, shnum, file, 0, 0, &strsec) == 0)
1954 			continue;
1955 
1956 		if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
1957 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
1958 			    file, _cache->c_name);
1959 			continue;
1960 		}
1961 		if (_cache->c_data == NULL)
1962 			continue;
1963 
1964 		numdyn = shdr->sh_size / shdr->sh_entsize;
1965 		dyn = (Dyn *)_cache->c_data->d_buf;
1966 
1967 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1968 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_DYNAMIC), _cache->c_name);
1969 
1970 		Elf_dyn_title(0);
1971 
1972 		for (ndx = 0; ndx < numdyn; dyn++, ndx++) {
1973 			union {
1974 				Conv_dyn_flag_buf_t	flag;
1975 				Conv_dyn_flag1_buf_t	flag1;
1976 				Conv_dyn_posflag1_buf_t	posflag1;
1977 				Conv_dyn_feature1_buf_t	feature1;
1978 			} c_buf;
1979 			const char	*name;
1980 
1981 			/*
1982 			 * Print the information numerically, and if possible
1983 			 * as a string.
1984 			 */
1985 			switch (dyn->d_tag) {
1986 			case DT_NULL:
1987 				/*
1988 				 * Special case: DT_NULLs can come in groups
1989 				 * that we prefer to reduce to a single line.
1990 				 */
1991 				end_ndx = ndx;
1992 				while ((end_ndx < (numdyn - 1)) &&
1993 				    ((dyn + 1)->d_tag == DT_NULL)) {
1994 					dyn++;
1995 					end_ndx++;
1996 				}
1997 				Elf_dyn_null_entry(0, dyn, ndx, end_ndx);
1998 				ndx = end_ndx;
1999 				continue;
2000 
2001 			/*
2002 			 * Print the information numerically, and if possible
2003 			 * as a string.
2004 			 */
2005 			case DT_NEEDED:
2006 			case DT_SONAME:
2007 			case DT_FILTER:
2008 			case DT_AUXILIARY:
2009 			case DT_CONFIG:
2010 			case DT_RPATH:
2011 			case DT_RUNPATH:
2012 			case DT_USED:
2013 			case DT_DEPAUDIT:
2014 			case DT_AUDIT:
2015 			case DT_SUNW_AUXILIARY:
2016 			case DT_SUNW_FILTER:
2017 				name = string(_cache, ndx, strsec,
2018 				    file, dyn->d_un.d_ptr);
2019 				break;
2020 
2021 			case DT_FLAGS:
2022 				name = conv_dyn_flag(dyn->d_un.d_val,
2023 				    0, &c_buf.flag);
2024 				break;
2025 			case DT_FLAGS_1:
2026 				name = conv_dyn_flag1(dyn->d_un.d_val,
2027 				    &c_buf.flag1);
2028 				break;
2029 			case DT_POSFLAG_1:
2030 				name = conv_dyn_posflag1(dyn->d_un.d_val, 0,
2031 				    &c_buf.posflag1);
2032 				break;
2033 			case DT_FEATURE_1:
2034 				name = conv_dyn_feature1(dyn->d_un.d_val, 0,
2035 				    &c_buf.feature1);
2036 				break;
2037 			case DT_DEPRECATED_SPARC_REGISTER:
2038 				name = MSG_INTL(MSG_STR_DEPRECATED);
2039 				break;
2040 			default:
2041 				name = MSG_ORIG(MSG_STR_EMPTY);
2042 				break;
2043 			}
2044 
2045 			Elf_dyn_entry(0, dyn, ndx, name, ehdr->e_machine);
2046 		}
2047 	}
2048 }
2049 
2050 /*
2051  * Search for and process a MOVE section.
2052  */
2053 static void
2054 move(Cache *cache, Word shnum, const char *file, uint_t flags)
2055 {
2056 	Word		cnt;
2057 	const char	*fmt = 0;
2058 
2059 	for (cnt = 1; cnt < shnum; cnt++) {
2060 		Word	movenum, symnum, ndx;
2061 		Sym	*syms;
2062 		Cache	*_cache = &cache[cnt];
2063 		Shdr	*shdr = _cache->c_shdr;
2064 		Cache	*symsec, *strsec;
2065 		Move	*move;
2066 
2067 		if (shdr->sh_type != SHT_SUNW_move)
2068 			continue;
2069 		if (!match(0, _cache->c_name, cnt))
2070 			continue;
2071 
2072 		/*
2073 		 * Determine the move data and number.
2074 		 */
2075 		if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
2076 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
2077 			    file, _cache->c_name);
2078 			continue;
2079 		}
2080 		if (_cache->c_data == NULL)
2081 			continue;
2082 
2083 		move = (Move *)_cache->c_data->d_buf;
2084 		movenum = shdr->sh_size / shdr->sh_entsize;
2085 
2086 		/*
2087 		 * Get the data buffer for the associated symbol table and
2088 		 * string table.
2089 		 */
2090 		if (stringtbl(cache, 1, cnt, shnum, file,
2091 		    &symnum, &symsec, &strsec) == 0)
2092 			return;
2093 
2094 		syms = (Sym *)symsec->c_data->d_buf;
2095 
2096 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2097 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_MOVE), _cache->c_name);
2098 		dbg_print(0, MSG_INTL(MSG_MOVE_TITLE));
2099 
2100 		if (fmt == 0)
2101 			fmt = MSG_INTL(MSG_MOVE_ENTRY);
2102 
2103 		for (ndx = 0; ndx < movenum; move++, ndx++) {
2104 			const char	*symname;
2105 			char		index[MAXNDXSIZE], section[BUFSIZ];
2106 			Word		symndx, shndx;
2107 			Sym		*sym;
2108 
2109 			/*
2110 			 * Check for null entries
2111 			 */
2112 			if ((move->m_info == 0) && (move->m_value == 0) &&
2113 			    (move->m_poffset == 0) && (move->m_repeat == 0) &&
2114 			    (move->m_stride == 0)) {
2115 				dbg_print(0, fmt, MSG_ORIG(MSG_STR_EMPTY),
2116 				    EC_XWORD(move->m_poffset), 0, 0, 0,
2117 				    EC_LWORD(0), MSG_ORIG(MSG_STR_EMPTY));
2118 				continue;
2119 			}
2120 			if (((symndx = ELF_M_SYM(move->m_info)) == 0) ||
2121 			    (symndx >= symnum)) {
2122 				(void) fprintf(stderr,
2123 				    MSG_INTL(MSG_ERR_BADMINFO), file,
2124 				    _cache->c_name, EC_XWORD(move->m_info));
2125 
2126 				(void) snprintf(index, MAXNDXSIZE,
2127 				    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(symndx));
2128 				dbg_print(0, fmt, index,
2129 				    EC_XWORD(move->m_poffset),
2130 				    ELF_M_SIZE(move->m_info), move->m_repeat,
2131 				    move->m_stride, move->m_value,
2132 				    MSG_INTL(MSG_STR_UNKNOWN));
2133 				continue;
2134 			}
2135 
2136 			symname = relsymname(cache, _cache, strsec,
2137 			    symndx, symnum, ndx, syms, section, BUFSIZ, file,
2138 			    flags);
2139 			sym = (Sym *)(syms + symndx);
2140 
2141 			/*
2142 			 * Additional sanity check.
2143 			 */
2144 			shndx = sym->st_shndx;
2145 			if (!((shndx == SHN_COMMON) ||
2146 			    (((shndx >= 1) && (shndx <= shnum)) &&
2147 			    (cache[shndx].c_shdr)->sh_type == SHT_NOBITS))) {
2148 				(void) fprintf(stderr,
2149 				    MSG_INTL(MSG_ERR_BADSYM2), file,
2150 				    _cache->c_name, demangle(symname, flags));
2151 			}
2152 
2153 			(void) snprintf(index, MAXNDXSIZE,
2154 			    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(symndx));
2155 			dbg_print(0, fmt, index, EC_XWORD(move->m_poffset),
2156 			    ELF_M_SIZE(move->m_info), move->m_repeat,
2157 			    move->m_stride, move->m_value,
2158 			    demangle(symname, flags));
2159 		}
2160 	}
2161 }
2162 
2163 /*
2164  * Traverse a note section analyzing each note information block.
2165  * The data buffers size is used to validate references before they are made,
2166  * and is decremented as each element is processed.
2167  */
2168 void
2169 note_entry(Cache *cache, Word *data, size_t size, const char *file)
2170 {
2171 	size_t	bsize = size;
2172 
2173 	/*
2174 	 * Print out a single `note' information block.
2175 	 */
2176 	while (size > 0) {
2177 		size_t	namesz, descsz, type, pad, noteoff;
2178 
2179 		noteoff = bsize - size;
2180 		/*
2181 		 * Make sure we can at least reference the 3 initial entries
2182 		 * (4-byte words) of the note information block.
2183 		 */
2184 		if (size >= (sizeof (Word) * 3))
2185 			size -= (sizeof (Word) * 3);
2186 		else {
2187 			(void) fprintf(stderr, MSG_INTL(MSG_NOTE_BADDATASZ),
2188 			    file, cache->c_name, EC_WORD(noteoff));
2189 			return;
2190 		}
2191 
2192 		/*
2193 		 * Make sure any specified name string can be referenced.
2194 		 */
2195 		if ((namesz = *data++) != 0) {
2196 			if (size >= namesz)
2197 				size -= namesz;
2198 			else {
2199 				(void) fprintf(stderr,
2200 				    MSG_INTL(MSG_NOTE_BADNMSZ), file,
2201 				    cache->c_name, EC_WORD(noteoff),
2202 				    EC_WORD(namesz));
2203 				return;
2204 			}
2205 		}
2206 
2207 		/*
2208 		 * Make sure any specified descriptor can be referenced.
2209 		 */
2210 		if ((descsz = *data++) != 0) {
2211 			/*
2212 			 * If namesz isn't a 4-byte multiple, account for any
2213 			 * padding that must exist before the descriptor.
2214 			 */
2215 			if ((pad = (namesz & (sizeof (Word) - 1))) != 0) {
2216 				pad = sizeof (Word) - pad;
2217 				size -= pad;
2218 			}
2219 			if (size >= descsz)
2220 				size -= descsz;
2221 			else {
2222 				(void) fprintf(stderr,
2223 				    MSG_INTL(MSG_NOTE_BADDESZ), file,
2224 				    cache->c_name, EC_WORD(noteoff),
2225 				    EC_WORD(namesz));
2226 				return;
2227 			}
2228 		}
2229 
2230 		type = *data++;
2231 
2232 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2233 		dbg_print(0, MSG_ORIG(MSG_NOTE_TYPE), EC_WORD(type));
2234 
2235 		dbg_print(0, MSG_ORIG(MSG_NOTE_NAMESZ), EC_WORD(namesz));
2236 		if (namesz) {
2237 			char	*name = (char *)data;
2238 
2239 			/*
2240 			 * Since the name string may have 'null' bytes
2241 			 * in it (ia32 .string) - we just write the
2242 			 * whole stream in a single fwrite.
2243 			 */
2244 			(void) fwrite(name, namesz, 1, stdout);
2245 			name = name + ((namesz + (sizeof (Word) - 1)) &
2246 			    ~(sizeof (Word) - 1));
2247 			/* LINTED */
2248 			data = (Word *)name;
2249 			dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2250 		}
2251 
2252 		/*
2253 		 * If multiple information blocks exist within a .note section
2254 		 * account for any padding that must exist before the next
2255 		 * information block.
2256 		 */
2257 		if ((pad = (descsz & (sizeof (Word) - 1))) != 0) {
2258 			pad = sizeof (Word) - pad;
2259 			if (size > pad)
2260 				size -= pad;
2261 		}
2262 
2263 		dbg_print(0, MSG_ORIG(MSG_NOTE_DESCSZ), EC_WORD(descsz));
2264 		if (descsz) {
2265 			int		ndx, byte, word;
2266 			char		string[58], *str = string;
2267 			uchar_t		*desc = (uchar_t *)data;
2268 
2269 			/*
2270 			 * Dump descriptor bytes.
2271 			 */
2272 			for (ndx = byte = word = 0; descsz; descsz--, desc++) {
2273 				int	tok = *desc;
2274 
2275 				(void) snprintf(str, 58, MSG_ORIG(MSG_NOTE_TOK),
2276 				    tok);
2277 				str += 3;
2278 
2279 				if (++byte == 4) {
2280 					*str++ = ' ', *str++ = ' ';
2281 					word++;
2282 					byte = 0;
2283 				}
2284 				if (word == 4) {
2285 					*str = '\0';
2286 					dbg_print(0, MSG_ORIG(MSG_NOTE_DESC),
2287 					    ndx, string);
2288 					word = 0;
2289 					ndx += 16;
2290 					str = string;
2291 				}
2292 			}
2293 			if (byte || word) {
2294 				*str = '\0';
2295 				dbg_print(0, MSG_ORIG(MSG_NOTE_DESC),
2296 				    ndx, string);
2297 			}
2298 
2299 			desc += pad;
2300 			/* LINTED */
2301 			data = (Word *)desc;
2302 		}
2303 	}
2304 }
2305 
2306 /*
2307  * Search for and process a .note section.
2308  */
2309 static void
2310 note(Cache *cache, Word shnum, const char *file)
2311 {
2312 	Word	cnt;
2313 
2314 	/*
2315 	 * Otherwise look for any .note sections.
2316 	 */
2317 	for (cnt = 1; cnt < shnum; cnt++) {
2318 		Cache	*_cache = &cache[cnt];
2319 		Shdr	*shdr = _cache->c_shdr;
2320 
2321 		if (shdr->sh_type != SHT_NOTE)
2322 			continue;
2323 		if (!match(0, _cache->c_name, cnt))
2324 			continue;
2325 
2326 		/*
2327 		 * As these sections are often hand rolled, make sure they're
2328 		 * properly aligned before proceeding.
2329 		 */
2330 		if (shdr->sh_offset & (sizeof (Word) - 1)) {
2331 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADALIGN),
2332 			    file, _cache->c_name);
2333 			continue;
2334 		}
2335 		if (_cache->c_data == NULL)
2336 			continue;
2337 
2338 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2339 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_NOTE), _cache->c_name);
2340 		note_entry(_cache, (Word *)_cache->c_data->d_buf,
2341 		/* LINTED */
2342 		    (Word)_cache->c_data->d_size, file);
2343 	}
2344 }
2345 
2346 /*
2347  * Determine an individual hash entry.  This may be the initial hash entry,
2348  * or an associated chain entry.
2349  */
2350 static void
2351 hash_entry(Cache *refsec, Cache *strsec, const char *hsecname, Word hashndx,
2352     Word symndx, Word symn, Sym *syms, const char *file, ulong_t bkts,
2353     uint_t flags, int chain)
2354 {
2355 	Sym		*sym;
2356 	const char	*symname, *str;
2357 	char		_bucket[MAXNDXSIZE], _symndx[MAXNDXSIZE];
2358 	ulong_t		nbkt, nhash;
2359 
2360 	if (symndx > symn) {
2361 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_HSBADSYMNDX), file,
2362 		    EC_WORD(symndx), EC_WORD(hashndx));
2363 		symname = MSG_INTL(MSG_STR_UNKNOWN);
2364 	} else {
2365 		sym = (Sym *)(syms + symndx);
2366 		symname = string(refsec, symndx, strsec, file, sym->st_name);
2367 	}
2368 
2369 	if (chain == 0) {
2370 		(void) snprintf(_bucket, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INTEGER),
2371 		    hashndx);
2372 		str = (const char *)_bucket;
2373 	} else
2374 		str = MSG_ORIG(MSG_STR_EMPTY);
2375 
2376 	(void) snprintf(_symndx, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INDEX2),
2377 	    EC_WORD(symndx));
2378 	dbg_print(0, MSG_ORIG(MSG_FMT_HASH_INFO), str, _symndx,
2379 	    demangle(symname, flags));
2380 
2381 	/*
2382 	 * Determine if this string is in the correct bucket.
2383 	 */
2384 	nhash = elf_hash(symname);
2385 	nbkt = nhash % bkts;
2386 
2387 	if (nbkt != hashndx) {
2388 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADHASH), file,
2389 		    hsecname, symname, EC_WORD(hashndx), nbkt);
2390 	}
2391 }
2392 
2393 #define	MAXCOUNT	500
2394 
2395 static void
2396 hash(Cache *cache, Word shnum, const char *file, uint_t flags)
2397 {
2398 	static int	count[MAXCOUNT];
2399 	Word		cnt;
2400 	ulong_t		ndx, bkts;
2401 	char		number[MAXNDXSIZE];
2402 
2403 	for (cnt = 1; cnt < shnum; cnt++) {
2404 		uint_t		*hash, *chain;
2405 		Cache		*_cache = &cache[cnt];
2406 		Shdr		*sshdr, *hshdr = _cache->c_shdr;
2407 		char		*ssecname, *hsecname = _cache->c_name;
2408 		Sym		*syms;
2409 		Word		symn;
2410 
2411 		if (hshdr->sh_type != SHT_HASH)
2412 			continue;
2413 
2414 		/*
2415 		 * Determine the hash table data and size.
2416 		 */
2417 		if ((hshdr->sh_entsize == 0) || (hshdr->sh_size == 0)) {
2418 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
2419 			    file, hsecname);
2420 			continue;
2421 		}
2422 		if (_cache->c_data == NULL)
2423 			continue;
2424 
2425 		hash = (uint_t *)_cache->c_data->d_buf;
2426 		bkts = *hash;
2427 		chain = hash + 2 + bkts;
2428 		hash += 2;
2429 
2430 		/*
2431 		 * Get the data buffer for the associated symbol table.
2432 		 */
2433 		if ((hshdr->sh_link == 0) || (hshdr->sh_link >= shnum)) {
2434 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
2435 			    file, hsecname, EC_WORD(hshdr->sh_link));
2436 			continue;
2437 		}
2438 
2439 		_cache = &cache[hshdr->sh_link];
2440 		ssecname = _cache->c_name;
2441 
2442 		if (_cache->c_data == NULL)
2443 			continue;
2444 
2445 		if ((syms = (Sym *)_cache->c_data->d_buf) == NULL) {
2446 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
2447 			    file, ssecname);
2448 			continue;
2449 		}
2450 
2451 		sshdr = _cache->c_shdr;
2452 		/* LINTED */
2453 		symn = (Word)(sshdr->sh_size / sshdr->sh_entsize);
2454 
2455 		/*
2456 		 * Get the associated string table section.
2457 		 */
2458 		if ((sshdr->sh_link == 0) || (sshdr->sh_link >= shnum)) {
2459 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
2460 			    file, ssecname, EC_WORD(sshdr->sh_link));
2461 			continue;
2462 		}
2463 
2464 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2465 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_HASH), hsecname);
2466 		dbg_print(0, MSG_INTL(MSG_ELF_HASH_INFO));
2467 
2468 		/*
2469 		 * Loop through the hash buckets, printing the appropriate
2470 		 * symbols.
2471 		 */
2472 		for (ndx = 0; ndx < bkts; ndx++, hash++) {
2473 			Word	_ndx, _cnt;
2474 
2475 			if (*hash == 0) {
2476 				count[0]++;
2477 				continue;
2478 			}
2479 
2480 			hash_entry(_cache, &cache[sshdr->sh_link], hsecname,
2481 			    ndx, *hash, symn, syms, file, bkts, flags, 0);
2482 
2483 			/*
2484 			 * Determine if any other symbols are chained to this
2485 			 * bucket.
2486 			 */
2487 			_ndx = chain[*hash];
2488 			_cnt = 1;
2489 			while (_ndx) {
2490 				hash_entry(_cache, &cache[sshdr->sh_link],
2491 				    hsecname, ndx, _ndx, symn, syms, file,
2492 				    bkts, flags, 1);
2493 				_ndx = chain[_ndx];
2494 				_cnt++;
2495 			}
2496 
2497 			if (_cnt >= MAXCOUNT) {
2498 				(void) fprintf(stderr,
2499 				    MSG_INTL(MSG_HASH_OVERFLW), file,
2500 				    _cache->c_name, EC_WORD(ndx),
2501 				    EC_WORD(_cnt));
2502 			} else
2503 				count[_cnt]++;
2504 		}
2505 		break;
2506 	}
2507 
2508 	/*
2509 	 * Print out the count information.
2510 	 */
2511 	bkts = cnt = 0;
2512 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2513 
2514 	for (ndx = 0; ndx < MAXCOUNT; ndx++) {
2515 		Word	_cnt;
2516 
2517 		if ((_cnt = count[ndx]) == 0)
2518 			continue;
2519 
2520 		(void) snprintf(number, MAXNDXSIZE,
2521 		    MSG_ORIG(MSG_FMT_INTEGER), _cnt);
2522 		dbg_print(0, MSG_INTL(MSG_ELF_HASH_BKTS1), number,
2523 		    EC_WORD(ndx));
2524 		bkts += _cnt;
2525 		cnt += (Word)(ndx * _cnt);
2526 	}
2527 	if (cnt) {
2528 		(void) snprintf(number, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INTEGER),
2529 		    bkts);
2530 		dbg_print(0, MSG_INTL(MSG_ELF_HASH_BKTS2), number,
2531 		    EC_WORD(cnt));
2532 	}
2533 }
2534 
2535 static void
2536 group(Cache *cache, Word shnum, const char *file, uint_t flags)
2537 {
2538 	Word	scnt;
2539 
2540 	for (scnt = 1; scnt < shnum; scnt++) {
2541 		Cache	*_cache = &cache[scnt];
2542 		Shdr	*shdr = _cache->c_shdr;
2543 		Word	*grpdata, gcnt, grpcnt, symnum, unknown;
2544 		Cache	*symsec, *strsec;
2545 		Sym	*syms, *sym;
2546 		char	flgstrbuf[MSG_GRP_COMDAT_SIZE + 10];
2547 
2548 		if (shdr->sh_type != SHT_GROUP)
2549 			continue;
2550 		if (!match(0, _cache->c_name, scnt))
2551 			continue;
2552 		if ((_cache->c_data == NULL) ||
2553 		    ((grpdata = (Word *)_cache->c_data->d_buf) == NULL))
2554 			continue;
2555 		grpcnt = shdr->sh_size / sizeof (Word);
2556 
2557 		/*
2558 		 * Get the data buffer for the associated symbol table and
2559 		 * string table.
2560 		 */
2561 		if (stringtbl(cache, 1, scnt, shnum, file,
2562 		    &symnum, &symsec, &strsec) == 0)
2563 			return;
2564 
2565 		syms = symsec->c_data->d_buf;
2566 
2567 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2568 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_GRP), _cache->c_name);
2569 		dbg_print(0, MSG_INTL(MSG_GRP_TITLE));
2570 
2571 		/*
2572 		 * The first element of the group defines the group.  The
2573 		 * associated symbol is defined by the sh_link field.
2574 		 */
2575 		if ((shdr->sh_info == SHN_UNDEF) || (shdr->sh_info > symnum)) {
2576 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHINFO),
2577 			    file, _cache->c_name, EC_WORD(shdr->sh_info));
2578 			return;
2579 		}
2580 
2581 		(void) strcpy(flgstrbuf, MSG_ORIG(MSG_STR_OSQBRKT));
2582 		if (grpdata[0] & GRP_COMDAT) {
2583 			(void) strcat(flgstrbuf, MSG_ORIG(MSG_GRP_COMDAT));
2584 		}
2585 		if ((unknown = (grpdata[0] & ~GRP_COMDAT)) != 0) {
2586 			size_t	len = strlen(flgstrbuf);
2587 
2588 			(void) snprintf(&flgstrbuf[len],
2589 			    (MSG_GRP_COMDAT_SIZE + 10 - len),
2590 			    MSG_ORIG(MSG_GRP_UNKNOWN), unknown);
2591 		}
2592 		(void) strcat(flgstrbuf, MSG_ORIG(MSG_STR_CSQBRKT));
2593 		sym = (Sym *)(syms + shdr->sh_info);
2594 
2595 		dbg_print(0, MSG_INTL(MSG_GRP_SIGNATURE), flgstrbuf,
2596 		    demangle(string(_cache, 0, strsec, file, sym->st_name),
2597 		    flags));
2598 
2599 		for (gcnt = 1; gcnt < grpcnt; gcnt++) {
2600 			char		index[MAXNDXSIZE];
2601 			const char	*name;
2602 
2603 			(void) snprintf(index, MAXNDXSIZE,
2604 			    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(gcnt));
2605 
2606 			if (grpdata[gcnt] >= shnum)
2607 				name = MSG_INTL(MSG_GRP_INVALSCN);
2608 			else
2609 				name = cache[grpdata[gcnt]].c_name;
2610 
2611 			(void) printf(MSG_ORIG(MSG_GRP_ENTRY), index, name,
2612 			    EC_XWORD(grpdata[gcnt]));
2613 		}
2614 	}
2615 }
2616 
2617 static void
2618 got(Cache *cache, Word shnum, Ehdr *ehdr, const char *file, uint_t flags)
2619 {
2620 	Cache		*gotcache = 0, *symtab = 0, *_cache;
2621 	Addr		gotbgn, gotend;
2622 	Shdr		*gotshdr;
2623 	Word		cnt, gotents, gotndx;
2624 	size_t		gentsize;
2625 	Got_info	*gottable;
2626 	char		*gotdata;
2627 	Sym		*gotsym;
2628 	Xword		gotsymaddr;
2629 
2630 	/*
2631 	 * First, find the got.
2632 	 */
2633 	for (cnt = 1; cnt < shnum; cnt++) {
2634 		_cache = &cache[cnt];
2635 		if (strncmp(_cache->c_name, MSG_ORIG(MSG_ELF_GOT),
2636 		    MSG_ELF_GOT_SIZE) == 0) {
2637 			gotcache = _cache;
2638 			break;
2639 		}
2640 	}
2641 	if (gotcache == 0)
2642 		return;
2643 
2644 	/*
2645 	 * A got section within a relocatable object is suspicious.
2646 	 */
2647 	if (ehdr->e_type == ET_REL) {
2648 		(void) fprintf(stderr, MSG_INTL(MSG_GOT_UNEXPECTED), file,
2649 		    _cache->c_name);
2650 	}
2651 
2652 	gotshdr = gotcache->c_shdr;
2653 	if (gotshdr->sh_size == 0) {
2654 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
2655 		    file, gotcache->c_name);
2656 		return;
2657 	}
2658 
2659 	gotbgn = gotshdr->sh_addr;
2660 	gotend = gotbgn + gotshdr->sh_size;
2661 
2662 	/*
2663 	 * Some architectures don't properly set the sh_entsize for the GOT
2664 	 * table.  If it's not set, default to a size of a pointer.
2665 	 */
2666 	if ((gentsize = gotshdr->sh_entsize) == 0)
2667 		gentsize = sizeof (Xword);
2668 
2669 	if (gotcache->c_data == NULL)
2670 		return;
2671 
2672 	/* LINTED */
2673 	gotents = (Word)(gotshdr->sh_size / gentsize);
2674 	gotdata = gotcache->c_data->d_buf;
2675 
2676 	if ((gottable = calloc(gotents, sizeof (Got_info))) == 0) {
2677 		int err = errno;
2678 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC), file,
2679 		    strerror(err));
2680 		return;
2681 	}
2682 
2683 	/*
2684 	 * Now we scan through all the sections looking for any relocations
2685 	 * that may be against the GOT.  Since these may not be isolated to a
2686 	 * .rel[a].got section we check them all.
2687 	 * While scanning sections save the symbol table entry (a symtab
2688 	 * overriding a dynsym) so that we can lookup _GLOBAL_OFFSET_TABLE_.
2689 	 */
2690 	for (cnt = 1; cnt < shnum; cnt++) {
2691 		Word		type, symnum;
2692 		Xword		relndx, relnum, relsize;
2693 		void		*rels;
2694 		Sym		*syms;
2695 		Cache		*symsec, *strsec;
2696 		Cache		*_cache = &cache[cnt];
2697 		Shdr		*shdr;
2698 
2699 		shdr = _cache->c_shdr;
2700 		type = shdr->sh_type;
2701 
2702 		if ((symtab == 0) && (type == SHT_DYNSYM)) {
2703 			symtab = _cache;
2704 			continue;
2705 		}
2706 		if (type == SHT_SYMTAB) {
2707 			symtab = _cache;
2708 			continue;
2709 		}
2710 		if ((type != SHT_RELA) && (type != SHT_REL))
2711 			continue;
2712 
2713 		/*
2714 		 * Decide entry size.
2715 		 */
2716 		if (((relsize = shdr->sh_entsize) == 0) ||
2717 		    (relsize > shdr->sh_size)) {
2718 			if (type == SHT_RELA)
2719 				relsize = sizeof (Rela);
2720 			else
2721 				relsize = sizeof (Rel);
2722 		}
2723 
2724 		/*
2725 		 * Determine the number of relocations available.
2726 		 */
2727 		if (shdr->sh_size == 0) {
2728 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
2729 			    file, _cache->c_name);
2730 			continue;
2731 		}
2732 		if (_cache->c_data == NULL)
2733 			continue;
2734 
2735 		rels = _cache->c_data->d_buf;
2736 		relnum = shdr->sh_size / relsize;
2737 
2738 		/*
2739 		 * Get the data buffer for the associated symbol table and
2740 		 * string table.
2741 		 */
2742 		if (stringtbl(cache, 1, cnt, shnum, file,
2743 		    &symnum, &symsec, &strsec) == 0)
2744 			continue;
2745 
2746 		syms = symsec->c_data->d_buf;
2747 
2748 		/*
2749 		 * Loop through the relocation entries.
2750 		 */
2751 		for (relndx = 0; relndx < relnum; relndx++,
2752 		    rels = (void *)((char *)rels + relsize)) {
2753 			char		section[BUFSIZ];
2754 			Addr		offset;
2755 			Got_info	*gip;
2756 			Word		symndx, reltype;
2757 			Rela		*rela;
2758 			Rel		*rel;
2759 
2760 			/*
2761 			 * Unravel the relocation.
2762 			 */
2763 			if (type == SHT_RELA) {
2764 				rela = (Rela *)rels;
2765 				symndx = ELF_R_SYM(rela->r_info);
2766 				reltype = ELF_R_TYPE(rela->r_info);
2767 				offset = rela->r_offset;
2768 			} else {
2769 				rel = (Rel *)rels;
2770 				symndx = ELF_R_SYM(rel->r_info);
2771 				reltype = ELF_R_TYPE(rel->r_info);
2772 				offset = rel->r_offset;
2773 			}
2774 
2775 			/*
2776 			 * Only pay attention to relocations against the GOT.
2777 			 */
2778 			if ((offset < gotbgn) || (offset >= gotend))
2779 				continue;
2780 
2781 			/* LINTED */
2782 			gotndx = (Word)((offset - gotbgn) /
2783 			    gotshdr->sh_entsize);
2784 			gip = &gottable[gotndx];
2785 
2786 			if (gip->g_reltype != 0) {
2787 				(void) fprintf(stderr,
2788 				    MSG_INTL(MSG_GOT_MULTIPLE), file,
2789 				    EC_WORD(gotndx), EC_ADDR(offset));
2790 				continue;
2791 			}
2792 
2793 			if (symndx)
2794 				gip->g_symname = relsymname(cache, _cache,
2795 				    strsec, symndx, symnum, relndx, syms,
2796 				    section, BUFSIZ, file, flags);
2797 			gip->g_reltype = reltype;
2798 			gip->g_rel = rels;
2799 		}
2800 	}
2801 
2802 	if (symlookup(MSG_ORIG(MSG_GOT_SYM), cache, shnum, &gotsym, symtab,
2803 	    file))
2804 		gotsymaddr = gotsym->st_value;
2805 	else
2806 		gotsymaddr = gotbgn;
2807 
2808 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2809 	dbg_print(0, MSG_INTL(MSG_ELF_SCN_GOT), gotcache->c_name);
2810 	Elf_got_title(0);
2811 
2812 	for (gotndx = 0; gotndx < gotents; gotndx++) {
2813 		Got_info	*gip;
2814 		Sword		gindex;
2815 		Addr		gaddr;
2816 		Xword		gotentry;
2817 
2818 		gip = &gottable[gotndx];
2819 
2820 		gaddr = gotbgn + (gotndx * gentsize);
2821 		gindex = (Sword)(gaddr - gotsymaddr) / (Sword)gentsize;
2822 
2823 		if (gentsize == sizeof (Word))
2824 			/* LINTED */
2825 			gotentry = (Xword)(*((Word *)(gotdata) + gotndx));
2826 		else
2827 			/* LINTED */
2828 			gotentry = *((Xword *)(gotdata) + gotndx);
2829 
2830 		Elf_got_entry(0, gindex, gaddr, gotentry, ehdr->e_machine,
2831 		    gip->g_reltype, gip->g_rel, gip->g_symname);
2832 	}
2833 	free(gottable);
2834 }
2835 
2836 void
2837 checksum(Elf *elf)
2838 {
2839 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2840 	dbg_print(0, MSG_INTL(MSG_STR_CHECKSUM), elf_checksum(elf));
2841 }
2842 
2843 /*
2844  * This variable is used by regular() to communicate the address of
2845  * the section header cache to sort_shdr_ndx_arr(). Unfortunately,
2846  * the qsort() interface does not include a userdata argument by which
2847  * such arbitrary data can be passed, so we are stuck using global data.
2848  */
2849 static Cache *sort_shdr_ndx_arr_cache;
2850 
2851 
2852 /*
2853  * Used with qsort() to sort the section indices so that they can be
2854  * used to access the section headers in order of increasing data offset.
2855  *
2856  * entry:
2857  *	sort_shdr_ndx_arr_cache - Contains address of
2858  *		section header cache.
2859  *	v1, v2 - Point at elements of sort_shdr_bits array to be compared.
2860  *
2861  * exit:
2862  *	Returns -1 (less than), 0 (equal) or 1 (greater than).
2863  */
2864 static int
2865 sort_shdr_ndx_arr(const void *v1, const void *v2)
2866 {
2867 	Cache	*cache1 = sort_shdr_ndx_arr_cache + *((size_t *)v1);
2868 	Cache	*cache2 = sort_shdr_ndx_arr_cache + *((size_t *)v2);
2869 
2870 	if (cache1->c_shdr->sh_offset < cache2->c_shdr->sh_offset)
2871 		return (-1);
2872 
2873 	if (cache1->c_shdr->sh_offset > cache2->c_shdr->sh_offset)
2874 		return (1);
2875 
2876 	return (0);
2877 }
2878 
2879 
2880 static int
2881 shdr_cache(const char *file, Elf *elf, Ehdr *ehdr, size_t shstrndx,
2882     size_t shnum, Cache **cache_ret)
2883 {
2884 	Elf_Scn		*scn;
2885 	Elf_Data	*data;
2886 	size_t		ndx;
2887 	Shdr		*nameshdr;
2888 	char		*names = 0;
2889 	Cache		*cache, *_cache;
2890 	size_t		*shdr_ndx_arr, shdr_ndx_arr_cnt;
2891 
2892 
2893 	/*
2894 	 * Obtain the .shstrtab data buffer to provide the required section
2895 	 * name strings.
2896 	 */
2897 	if (shstrndx == SHN_UNDEF) {
2898 		/*
2899 		 * It is rare, but legal, for an object to lack a
2900 		 * header string table section.
2901 		 */
2902 		names = NULL;
2903 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_NOSHSTRSEC), file);
2904 	} else if ((scn = elf_getscn(elf, shstrndx)) == NULL) {
2905 		failure(file, MSG_ORIG(MSG_ELF_GETSCN));
2906 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SHDR),
2907 		    EC_XWORD(shstrndx));
2908 
2909 	} else if ((data = elf_getdata(scn, NULL)) == NULL) {
2910 		failure(file, MSG_ORIG(MSG_ELF_GETDATA));
2911 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_DATA),
2912 		    EC_XWORD(shstrndx));
2913 
2914 	} else if ((nameshdr = elf_getshdr(scn)) == NULL) {
2915 		failure(file, MSG_ORIG(MSG_ELF_GETSHDR));
2916 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN),
2917 		    EC_WORD(elf_ndxscn(scn)));
2918 
2919 	} else if ((names = data->d_buf) == 0)
2920 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_SHSTRNULL), file);
2921 
2922 	/*
2923 	 * Allocate a cache to maintain a descriptor for each section.
2924 	 */
2925 	if ((*cache_ret = cache = malloc(shnum * sizeof (Cache))) == NULL) {
2926 		int err = errno;
2927 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
2928 		    file, strerror(err));
2929 		return (0);
2930 	}
2931 
2932 	*cache = cache_init;
2933 	_cache = cache;
2934 	_cache++;
2935 
2936 	/*
2937 	 * Allocate an array that will hold the section index for
2938 	 * each section that has data in the ELF file:
2939 	 *
2940 	 *	- Is not a NOBITS section
2941 	 *	- Data has non-zero length
2942 	 *
2943 	 * Note that shnum is an upper bound on the size required. It
2944 	 * is likely that we won't use a few of these array elements.
2945 	 * Allocating a modest amount of extra memory in this case means
2946 	 * that we can avoid an extra loop to count the number of needed
2947 	 * items, and can fill this array immediately in the first loop
2948 	 * below.
2949 	 */
2950 	if ((shdr_ndx_arr = malloc(shnum * sizeof (*shdr_ndx_arr))) == NULL) {
2951 		int err = errno;
2952 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
2953 		    file, strerror(err));
2954 		return (0);
2955 	}
2956 	shdr_ndx_arr_cnt = 0;
2957 
2958 	/*
2959 	 * Traverse the sections of the file.  This gathering of data is
2960 	 * carried out in two passes.  First, the section headers are captured
2961 	 * and the section header names are evaluated.  A verification pass is
2962 	 * then carried out over the section information.  Files have been
2963 	 * known to exhibit overlapping (and hence erroneous) section header
2964 	 * information.
2965 	 *
2966 	 * Finally, the data for each section is obtained.  This processing is
2967 	 * carried out after section verification because should any section
2968 	 * header overlap occur, and a file needs translating (ie. xlate'ing
2969 	 * information from a non-native architecture file), then the process
2970 	 * of translation can corrupt the section header information.  Of
2971 	 * course, if there is any section overlap, the data related to the
2972 	 * sections is going to be compromised.  However, it is the translation
2973 	 * of this data that has caused problems with elfdump()'s ability to
2974 	 * extract the data.
2975 	 */
2976 	for (ndx = 1, scn = NULL; scn = elf_nextscn(elf, scn);
2977 	    ndx++, _cache++) {
2978 		char	scnndxnm[100];
2979 
2980 		_cache->c_ndx = ndx;
2981 		_cache->c_scn = scn;
2982 
2983 		if ((_cache->c_shdr = elf_getshdr(scn)) == NULL) {
2984 			failure(file, MSG_ORIG(MSG_ELF_GETSHDR));
2985 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN),
2986 			    EC_WORD(elf_ndxscn(scn)));
2987 		}
2988 
2989 		/*
2990 		 * If this section has data in the file, include it in
2991 		 * the array of sections to check for address overlap.
2992 		 */
2993 		if ((_cache->c_shdr->sh_size != 0) &&
2994 		    (_cache->c_shdr->sh_type != SHT_NOBITS))
2995 			shdr_ndx_arr[shdr_ndx_arr_cnt++] = ndx;
2996 
2997 		/*
2998 		 * If a shstrtab exists, assign the section name.
2999 		 */
3000 		if (names && _cache->c_shdr) {
3001 			if (_cache->c_shdr->sh_name &&
3002 			    /* LINTED */
3003 			    (nameshdr->sh_size > _cache->c_shdr->sh_name)) {
3004 				_cache->c_name =
3005 				    names + _cache->c_shdr->sh_name;
3006 				continue;
3007 			}
3008 
3009 			/*
3010 			 * Generate an error if the section name index is zero
3011 			 * or exceeds the shstrtab data.  Fall through to
3012 			 * fabricate a section name.
3013 			 */
3014 			if ((_cache->c_shdr->sh_name == 0) ||
3015 			    /* LINTED */
3016 			    (nameshdr->sh_size <= _cache->c_shdr->sh_name)) {
3017 				(void) fprintf(stderr,
3018 				    MSG_INTL(MSG_ERR_BADSHNAME), file,
3019 				    EC_WORD(ndx),
3020 				    EC_XWORD(_cache->c_shdr->sh_name));
3021 			}
3022 		}
3023 
3024 		/*
3025 		 * If there exists no shstrtab data, or a section header has no
3026 		 * name (an invalid index of 0), then compose a name for the
3027 		 * section.
3028 		 */
3029 		(void) snprintf(scnndxnm, sizeof (scnndxnm),
3030 		    MSG_INTL(MSG_FMT_SCNNDX), ndx);
3031 
3032 		if ((_cache->c_name = malloc(strlen(scnndxnm) + 1)) == NULL) {
3033 			int err = errno;
3034 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
3035 			    file, strerror(err));
3036 			return (0);
3037 		}
3038 		(void) strcpy(_cache->c_name, scnndxnm);
3039 	}
3040 
3041 	/*
3042 	 * Having collected all the sections, validate their address range.
3043 	 * Cases have existed where the section information has been invalid.
3044 	 * This can lead to all sorts of other, hard to diagnose errors, as
3045 	 * each section is processed individually (ie. with elf_getdata()).
3046 	 * Here, we carry out some address comparisons to catch a family of
3047 	 * overlapping memory issues we have observed (likely, there are others
3048 	 * that we have yet to discover).
3049 	 *
3050 	 * Note, should any memory overlap occur, obtaining any additional
3051 	 * data from the file is questionable.  However, it might still be
3052 	 * possible to inspect the ELF header, Programs headers, or individual
3053 	 * sections, so rather than bailing on an error condition, continue
3054 	 * processing to see if any data can be salvaged.
3055 	 */
3056 	if (shdr_ndx_arr_cnt > 1) {
3057 		sort_shdr_ndx_arr_cache = cache;
3058 		qsort(shdr_ndx_arr, shdr_ndx_arr_cnt,
3059 		    sizeof (*shdr_ndx_arr), sort_shdr_ndx_arr);
3060 	}
3061 	for (ndx = 0; ndx < shdr_ndx_arr_cnt; ndx++) {
3062 		Cache	*_cache = cache + shdr_ndx_arr[ndx];
3063 		Shdr	*shdr = _cache->c_shdr;
3064 		Off	bgn1, bgn = shdr->sh_offset;
3065 		Off	end1, end = shdr->sh_offset + shdr->sh_size;
3066 		size_t	ndx1;
3067 
3068 		/*
3069 		 * Check the section against all following ones, reporting
3070 		 * any overlaps. Since we've sorted the sections by offset,
3071 		 * we can stop after the first comparison that fails. There
3072 		 * are no overlaps in a properly formed ELF file, in which
3073 		 * case this algorithm runs in O(n) time. This will degenerate
3074 		 * to O(n^2) for a completely broken file. Such a file is
3075 		 * (1) highly unlikely, and (2) unusable, so it is reasonable
3076 		 * for the analysis to take longer.
3077 		 */
3078 		for (ndx1 = ndx + 1; ndx1 < shdr_ndx_arr_cnt; ndx1++) {
3079 			Cache	*_cache1 = cache + shdr_ndx_arr[ndx1];
3080 			Shdr	*shdr1 = _cache1->c_shdr;
3081 
3082 			bgn1 = shdr1->sh_offset;
3083 			end1 = shdr1->sh_offset + shdr1->sh_size;
3084 
3085 			if (((bgn1 <= bgn) && (end1 > bgn)) ||
3086 			    ((bgn1 < end) && (end1 >= end))) {
3087 				(void) fprintf(stderr,
3088 				    MSG_INTL(MSG_ERR_SECMEMOVER), file,
3089 				    EC_WORD(elf_ndxscn(_cache->c_scn)),
3090 				    _cache->c_name, EC_OFF(bgn), EC_OFF(end),
3091 				    EC_WORD(elf_ndxscn(_cache1->c_scn)),
3092 				    _cache1->c_name, EC_OFF(bgn1),
3093 				    EC_OFF(end1));
3094 			} else {	/* No overlap, so can stop */
3095 				break;
3096 			}
3097 		}
3098 
3099 		/*
3100 		 * In addition to checking for sections overlapping
3101 		 * each other (done above), we should also make sure
3102 		 * the section doesn't overlap the section header array.
3103 		 */
3104 		bgn1 = ehdr->e_shoff;
3105 		end1 = ehdr->e_shoff + (ehdr->e_shentsize * ehdr->e_shnum);
3106 
3107 		if (((bgn1 <= bgn) && (end1 > bgn)) ||
3108 		    ((bgn1 < end) && (end1 >= end))) {
3109 			(void) fprintf(stderr,
3110 			    MSG_INTL(MSG_ERR_SHDRMEMOVER), file, EC_OFF(bgn1),
3111 			    EC_OFF(end1),
3112 			    EC_WORD(elf_ndxscn(_cache->c_scn)),
3113 			    _cache->c_name, EC_OFF(bgn), EC_OFF(end));
3114 		}
3115 	}
3116 
3117 	/*
3118 	 * Obtain the data for each section.
3119 	 */
3120 	for (ndx = 1; ndx < shnum; ndx++) {
3121 		Cache	*_cache = &cache[ndx];
3122 		Elf_Scn	*scn = _cache->c_scn;
3123 
3124 		if ((_cache->c_data = elf_getdata(scn, NULL)) == NULL) {
3125 			failure(file, MSG_ORIG(MSG_ELF_GETDATA));
3126 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCNDATA),
3127 			    EC_WORD(elf_ndxscn(scn)));
3128 		}
3129 	}
3130 
3131 	return (1);
3132 }
3133 
3134 
3135 
3136 void
3137 regular(const char *file, int fd, Elf *elf, uint_t flags, int wfd)
3138 {
3139 	Elf_Scn		*scn;
3140 	Ehdr		*ehdr;
3141 	size_t		ndx, shstrndx, shnum, phnum;
3142 	Shdr		*shdr;
3143 	Cache		*cache;
3144 	VERSYM_STATE	versym;
3145 
3146 	if ((ehdr = elf_getehdr(elf)) == NULL) {
3147 		failure(file, MSG_ORIG(MSG_ELF_GETEHDR));
3148 		return;
3149 	}
3150 
3151 	if (elf_getshnum(elf, &shnum) == 0) {
3152 		failure(file, MSG_ORIG(MSG_ELF_GETSHNUM));
3153 		return;
3154 	}
3155 
3156 	if (elf_getshstrndx(elf, &shstrndx) == 0) {
3157 		failure(file, MSG_ORIG(MSG_ELF_GETSHSTRNDX));
3158 		return;
3159 	}
3160 
3161 	if (elf_getphnum(elf, &phnum) == 0) {
3162 		failure(file, MSG_ORIG(MSG_ELF_GETPHNUM));
3163 		return;
3164 	}
3165 	/*
3166 	 * If the user requested section headers derived from the
3167 	 * program headers (-P option) and this file doesn't have
3168 	 * any program headers (i.e. ET_REL), then we can't do it.
3169 	 */
3170 	if ((phnum == 0) && (flags & FLG_FAKESHDR)) {
3171 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_PNEEDSPH), file);
3172 		return;
3173 	}
3174 
3175 
3176 	if ((scn = elf_getscn(elf, 0)) != NULL) {
3177 		if ((shdr = elf_getshdr(scn)) == NULL) {
3178 			failure(file, MSG_ORIG(MSG_ELF_GETSHDR));
3179 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN), 0);
3180 			return;
3181 		}
3182 	} else
3183 		shdr = 0;
3184 
3185 	/*
3186 	 * Print the elf header.
3187 	 */
3188 	if (flags & FLG_EHDR)
3189 		Elf_ehdr(0, ehdr, shdr);
3190 
3191 	/*
3192 	 * If the section headers or program headers have inadequate
3193 	 * alignment for the class of object, print a warning. libelf
3194 	 * can handle such files, but programs that use them can crash
3195 	 * when they dereference unaligned items.
3196 	 */
3197 	if (ehdr->e_phoff & (sizeof (Addr) - 1))
3198 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADPHDRALIGN), file);
3199 	if (ehdr->e_shoff & (sizeof (Addr) - 1))
3200 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHDRALIGN), file);
3201 
3202 	/*
3203 	 * Print the program headers.
3204 	 */
3205 	if ((flags & FLG_PHDR) && (phnum != 0)) {
3206 		Conv_inv_buf_t	inv_buf;
3207 		Phdr		*phdr;
3208 
3209 		if ((phdr = elf_getphdr(elf)) == NULL) {
3210 			failure(file, MSG_ORIG(MSG_ELF_GETPHDR));
3211 			return;
3212 		}
3213 
3214 		for (ndx = 0; ndx < phnum; phdr++, ndx++) {
3215 			if (!match(0, conv_phdr_type(ehdr->e_machine,
3216 			    phdr->p_type, CONV_FMT_ALTFILE, &inv_buf), ndx))
3217 				continue;
3218 
3219 			dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
3220 			dbg_print(0, MSG_INTL(MSG_ELF_PHDR), EC_WORD(ndx));
3221 			Elf_phdr(0, ehdr->e_machine, phdr);
3222 		}
3223 	}
3224 
3225 	/*
3226 	 * Decide how to proceed if there are no sections, if there's just
3227 	 * one section (the first section can act as an extension of the
3228 	 * ELF header), or if only program header information was requested.
3229 	 */
3230 	if ((shnum <= 1) || (flags && (flags & ~(FLG_EHDR | FLG_PHDR)) == 0)) {
3231 		/* If a core file, display the note and return */
3232 		if ((ehdr->e_type == ET_CORE) && (flags & FLG_NOTE)) {
3233 			note(0, shnum, file);
3234 			return;
3235 		}
3236 
3237 		/* If only program header info was requested, we're done */
3238 		if (flags && (flags & ~(FLG_EHDR | FLG_PHDR)) == 0)
3239 			return;
3240 
3241 		/*
3242 		 * Section headers are missing. Resort to synthesizing
3243 		 * section headers from the program headers.
3244 		 */
3245 		if ((flags & FLG_FAKESHDR) == 0) {
3246 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_NOSHDR), file);
3247 			flags |= FLG_FAKESHDR;
3248 		}
3249 	}
3250 
3251 	/*
3252 	 * Generate a cache of section headers and related information
3253 	 * for use by the rest of elfdump. If requested (or the file
3254 	 * contains no section headers), we generate a fake set of
3255 	 * headers from the information accessible from the program headers.
3256 	 * Otherwise, we use the real section headers contained in the file.
3257 	 */
3258 
3259 	if (flags & FLG_FAKESHDR) {
3260 		if (fake_shdr_cache(file, fd, elf, ehdr, &cache, &shnum) == 0)
3261 			return;
3262 	} else {
3263 		if (shdr_cache(file, elf, ehdr, shstrndx, shnum, &cache) == 0)
3264 			return;
3265 	}
3266 
3267 	/*
3268 	 * If -w was specified, find and write out the section(s) data.
3269 	 */
3270 	if (wfd) {
3271 		for (ndx = 1; ndx < shnum; ndx++) {
3272 			Cache	*_cache = &cache[ndx];
3273 
3274 			if (match(1, _cache->c_name, ndx) && _cache->c_data) {
3275 				(void) write(wfd, _cache->c_data->d_buf,
3276 				    _cache->c_data->d_size);
3277 			}
3278 		}
3279 	}
3280 
3281 	if (flags & FLG_SHDR)
3282 		sections(file, cache, shnum, ehdr);
3283 
3284 	if (flags & FLG_INTERP)
3285 		interp(file, cache, shnum, phnum, elf);
3286 
3287 	versions(cache, shnum, file, flags, &versym);
3288 
3289 	if (flags & FLG_SYMBOLS)
3290 		symbols(cache, shnum, ehdr, &versym, file, flags);
3291 
3292 	if (flags & FLG_SORT)
3293 		sunw_sort(cache, shnum, ehdr, &versym, file, flags);
3294 
3295 	if (flags & FLG_HASH)
3296 		hash(cache, shnum, file, flags);
3297 
3298 	if (flags & FLG_GOT)
3299 		got(cache, shnum, ehdr, file, flags);
3300 
3301 	if (flags & FLG_GROUP)
3302 		group(cache, shnum, file, flags);
3303 
3304 	if (flags & FLG_SYMINFO)
3305 		syminfo(cache, shnum, file);
3306 
3307 	if (flags & FLG_RELOC)
3308 		reloc(cache, shnum, ehdr, file, flags);
3309 
3310 	if (flags & FLG_DYNAMIC)
3311 		dynamic(cache, shnum, ehdr, file);
3312 
3313 	if (flags & FLG_NOTE)
3314 		note(cache, shnum, file);
3315 
3316 	if (flags & FLG_MOVE)
3317 		move(cache, shnum, file, flags);
3318 
3319 	if (flags & FLG_CHECKSUM)
3320 		checksum(elf);
3321 
3322 	if (flags & FLG_CAP)
3323 		cap(file, cache, shnum, phnum, ehdr, elf);
3324 
3325 	if (flags & FLG_UNWIND)
3326 		unwind(cache, shnum, phnum, ehdr, file, elf);
3327 
3328 
3329 	/* Release the memory used to cache section headers */
3330 	if (flags & FLG_FAKESHDR)
3331 		fake_shdr_cache_free(cache, shnum);
3332 	else
3333 		free(cache);
3334 }
3335