xref: /titanic_41/usr/src/cmd/sgs/elfdump/common/elfdump.c (revision 55553f719b521a0bb4deab6efc944cd30c1a56aa)
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 2008 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	<sys/elf_386.h>
32 #include	<sys/elf_amd64.h>
33 #include	<sys/elf_SPARC.h>
34 #include	<_libelf.h>
35 #include	<dwarf.h>
36 #include	<stdio.h>
37 #include	<unistd.h>
38 #include	<errno.h>
39 #include	<strings.h>
40 #include	<debug.h>
41 #include	<conv.h>
42 #include	<msg.h>
43 #include	<_elfdump.h>
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_CTL_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(MATCH_F_ALL, secname, seccnt, shdr->sh_type))
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(MATCH_F_ALL, _cache->c_name, cnt, shdr->sh_type))
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_SHOW_VERSIONS) == 0) {
1275 		/*
1276 		 * If GNU versioning applies to this object, and there
1277 		 * is a Verneed section, then examine it to determine
1278 		 * the maximum Versym version index for this file.
1279 		 */
1280 		if ((versym->gnu) && (verneed_cache != NULL))
1281 			update_gnu_max_verndx(
1282 			    (Verneed *)verneed_cache->c_data->d_buf,
1283 			    verneed_cache->c_shdr->sh_info, versym);
1284 		return;
1285 	}
1286 
1287 	/*
1288 	 * Now that all the information is available, display the
1289 	 * Verdef and Verneed section contents.
1290 	 */
1291 	if (verdef_cache != NULL) {
1292 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1293 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_VERDEF),
1294 		    verdef_cache->c_name);
1295 		version_def((Verdef *)verdef_cache->c_data->d_buf,
1296 		    verdef_cache->c_shdr->sh_info, verdef_cache,
1297 		    &cache[verdef_cache->c_shdr->sh_link], file);
1298 	}
1299 	if (verneed_cache != NULL) {
1300 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1301 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_VERNEED),
1302 		    verneed_cache->c_name);
1303 		/*
1304 		 * If GNU versioning applies to this object, version_need()
1305 		 * will update versym->max_verndx, and it is not
1306 		 * necessary to call update_gnu_max_verndx().
1307 		 */
1308 		version_need((Verneed *)verneed_cache->c_data->d_buf,
1309 		    verneed_cache->c_shdr->sh_info, verneed_cache,
1310 		    &cache[verneed_cache->c_shdr->sh_link], file, versym);
1311 	}
1312 }
1313 
1314 /*
1315  * Initialize a symbol table state structure
1316  *
1317  * entry:
1318  *	state - State structure to be initialized
1319  *	cache - Cache of all section headers
1320  *	shnum - # of sections in cache
1321  *	secndx - Index of symbol table section
1322  *	ehdr - ELF header for file
1323  *	versym - Information about versym section
1324  *	file - Name of file
1325  *	flags - Command line option flags
1326  */
1327 static int
1328 init_symtbl_state(SYMTBL_STATE *state, Cache *cache, Word shnum, Word secndx,
1329     Ehdr *ehdr, VERSYM_STATE *versym, const char *file, uint_t flags)
1330 {
1331 	Shdr *shdr;
1332 
1333 	state->file = file;
1334 	state->ehdr = ehdr;
1335 	state->cache = cache;
1336 	state->shnum = shnum;
1337 	state->seccache = &cache[secndx];
1338 	state->secndx = secndx;
1339 	state->secname = state->seccache->c_name;
1340 	state->flags = flags;
1341 	state->shxndx.checked = 0;
1342 	state->shxndx.data = NULL;
1343 	state->shxndx.n = 0;
1344 
1345 	shdr = state->seccache->c_shdr;
1346 
1347 	/*
1348 	 * Check the symbol data and per-item size.
1349 	 */
1350 	if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
1351 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
1352 		    file, state->secname);
1353 		return (0);
1354 	}
1355 	if (state->seccache->c_data == NULL)
1356 		return (0);
1357 
1358 	/* LINTED */
1359 	state->symn = (Word)(shdr->sh_size / shdr->sh_entsize);
1360 	state->sym = (Sym *)state->seccache->c_data->d_buf;
1361 
1362 	/*
1363 	 * Check associated string table section.
1364 	 */
1365 	if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
1366 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
1367 		    file, state->secname, EC_WORD(shdr->sh_link));
1368 		return (0);
1369 	}
1370 
1371 	/*
1372 	 * Determine if there is a associated Versym section
1373 	 * with this Symbol Table.
1374 	 */
1375 	if (versym->cache &&
1376 	    (versym->cache->c_shdr->sh_link == state->secndx))
1377 		state->versym = versym;
1378 	else
1379 		state->versym = NULL;
1380 
1381 
1382 	return (1);
1383 }
1384 
1385 /*
1386  * Determine the extended section index used for symbol tables entries.
1387  */
1388 static void
1389 symbols_getxindex(SYMTBL_STATE * state)
1390 {
1391 	uint_t	symn;
1392 	Word	symcnt;
1393 
1394 	state->shxndx.checked = 1;   /* Note that we've been called */
1395 	for (symcnt = 1; symcnt < state->shnum; symcnt++) {
1396 		Cache	*_cache = &state->cache[symcnt];
1397 		Shdr	*shdr = _cache->c_shdr;
1398 
1399 		if ((shdr->sh_type != SHT_SYMTAB_SHNDX) ||
1400 		    (shdr->sh_link != state->secndx))
1401 			continue;
1402 
1403 		if ((shdr->sh_entsize) &&
1404 		    /* LINTED */
1405 		    ((symn = (uint_t)(shdr->sh_size / shdr->sh_entsize)) == 0))
1406 			continue;
1407 
1408 		if (_cache->c_data == NULL)
1409 			continue;
1410 
1411 		state->shxndx.data = _cache->c_data->d_buf;
1412 		state->shxndx.n = symn;
1413 		return;
1414 	}
1415 }
1416 
1417 /*
1418  * Produce a line of output for the given symbol
1419  *
1420  * entry:
1421  *	state - Symbol table state
1422  *	symndx - Index of symbol within the table
1423  *	info - Value of st_info (indicates local/global range)
1424  *	symndx_disp - Index to display. This may not be the same
1425  *		as symndx if the display is relative to the logical
1426  *		combination of the SUNW_ldynsym/dynsym tables.
1427  *	sym - Symbol to display
1428  */
1429 static void
1430 output_symbol(SYMTBL_STATE *state, Word symndx, Word info, Word disp_symndx,
1431     Sym *sym)
1432 {
1433 	/*
1434 	 * Symbol types for which we check that the specified
1435 	 * address/size land inside the target section.
1436 	 */
1437 	static const int addr_symtype[STT_NUM] = {
1438 		0,			/* STT_NOTYPE */
1439 		1,			/* STT_OBJECT */
1440 		1,			/* STT_FUNC */
1441 		0,			/* STT_SECTION */
1442 		0,			/* STT_FILE */
1443 		1,			/* STT_COMMON */
1444 		0,			/* STT_TLS */
1445 	};
1446 #if STT_NUM != (STT_TLS + 1)
1447 #error "STT_NUM has grown. Update addr_symtype[]"
1448 #endif
1449 
1450 	char		index[MAXNDXSIZE];
1451 	const char	*symname, *sec;
1452 	Versym		verndx;
1453 	int		gnuver;
1454 	uchar_t		type;
1455 	Shdr		*tshdr;
1456 	Word		shndx;
1457 	Conv_inv_buf_t	inv_buf;
1458 
1459 	/* Ensure symbol index is in range */
1460 	if (symndx >= state->symn) {
1461 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSORTNDX),
1462 		    state->file, state->secname, EC_WORD(symndx));
1463 		return;
1464 	}
1465 
1466 	/*
1467 	 * If we are using extended symbol indexes, find the
1468 	 * corresponding SHN_SYMTAB_SHNDX table.
1469 	 */
1470 	if ((sym->st_shndx == SHN_XINDEX) && (state->shxndx.checked == 0))
1471 		symbols_getxindex(state);
1472 
1473 	/* LINTED */
1474 	symname = string(state->seccache, symndx,
1475 	    &state->cache[state->seccache->c_shdr->sh_link], state->file,
1476 	    sym->st_name);
1477 
1478 	tshdr = 0;
1479 	sec = NULL;
1480 
1481 	if (state->ehdr->e_type == ET_CORE) {
1482 		sec = (char *)MSG_INTL(MSG_STR_UNKNOWN);
1483 	} else if (state->flags & FLG_CTL_FAKESHDR) {
1484 		/*
1485 		 * If we are using fake section headers derived from
1486 		 * the program headers, then the section indexes
1487 		 * in the symbols do not correspond to these headers.
1488 		 * The section names are not available, so all we can
1489 		 * do is to display them in numeric form.
1490 		 */
1491 		sec = conv_sym_shndx(sym->st_shndx, &inv_buf);
1492 	} else if ((sym->st_shndx < SHN_LORESERVE) &&
1493 	    (sym->st_shndx < state->shnum)) {
1494 		shndx = sym->st_shndx;
1495 		tshdr = state->cache[shndx].c_shdr;
1496 		sec = state->cache[shndx].c_name;
1497 	} else if (sym->st_shndx == SHN_XINDEX) {
1498 		if (state->shxndx.data) {
1499 			Word	_shxndx;
1500 
1501 			if (symndx > state->shxndx.n) {
1502 				(void) fprintf(stderr,
1503 				    MSG_INTL(MSG_ERR_BADSYMXINDEX1),
1504 				    state->file, state->secname,
1505 				    EC_WORD(symndx));
1506 			} else if ((_shxndx =
1507 			    state->shxndx.data[symndx]) > state->shnum) {
1508 				(void) fprintf(stderr,
1509 				    MSG_INTL(MSG_ERR_BADSYMXINDEX2),
1510 				    state->file, state->secname,
1511 				    EC_WORD(symndx), EC_WORD(_shxndx));
1512 			} else {
1513 				shndx = _shxndx;
1514 				tshdr = state->cache[shndx].c_shdr;
1515 				sec = state->cache[shndx].c_name;
1516 			}
1517 		} else {
1518 			(void) fprintf(stderr,
1519 			    MSG_INTL(MSG_ERR_BADSYMXINDEX3),
1520 			    state->file, state->secname, EC_WORD(symndx));
1521 		}
1522 	} else if ((sym->st_shndx < SHN_LORESERVE) &&
1523 	    (sym->st_shndx >= state->shnum)) {
1524 		(void) fprintf(stderr,
1525 		    MSG_INTL(MSG_ERR_BADSYM5), state->file,
1526 		    state->secname, EC_WORD(symndx),
1527 		    demangle(symname, state->flags), sym->st_shndx);
1528 	}
1529 
1530 	/*
1531 	 * If versioning is available display the
1532 	 * version index. If not, then use 0.
1533 	 */
1534 	if (state->versym) {
1535 		Versym test_verndx;
1536 
1537 		verndx = test_verndx = state->versym->data[symndx];
1538 		gnuver = state->versym->gnu;
1539 
1540 		/*
1541 		 * Check to see if this is a defined symbol with a
1542 		 * version index that is outside the valid range for
1543 		 * the file. The interpretation of this depends on
1544 		 * the style of versioning used by the object.
1545 		 *
1546 		 * Versions >= VER_NDX_LORESERVE have special meanings,
1547 		 * and are exempt from this checking.
1548 		 *
1549 		 * GNU style version indexes use the top bit of the
1550 		 * 16-bit index value (0x8000) as the "hidden bit".
1551 		 * We must mask off this bit in order to compare
1552 		 * the version against the maximum value.
1553 		 */
1554 		if (gnuver)
1555 			test_verndx &= ~0x8000;
1556 
1557 		if ((test_verndx > state->versym->max_verndx) &&
1558 		    (verndx < VER_NDX_LORESERVE))
1559 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADVER),
1560 			    state->file, state->secname, EC_WORD(symndx),
1561 			    EC_HALF(test_verndx), state->versym->max_verndx);
1562 	} else {
1563 		verndx = 0;
1564 		gnuver = 0;
1565 	}
1566 
1567 	/*
1568 	 * Error checking for TLS.
1569 	 */
1570 	type = ELF_ST_TYPE(sym->st_info);
1571 	if (type == STT_TLS) {
1572 		if (tshdr &&
1573 		    (sym->st_shndx != SHN_UNDEF) &&
1574 		    ((tshdr->sh_flags & SHF_TLS) == 0)) {
1575 			(void) fprintf(stderr,
1576 			    MSG_INTL(MSG_ERR_BADSYM3), state->file,
1577 			    state->secname, EC_WORD(symndx),
1578 			    demangle(symname, state->flags));
1579 		}
1580 	} else if ((type != STT_SECTION) && sym->st_size &&
1581 	    tshdr && (tshdr->sh_flags & SHF_TLS)) {
1582 		(void) fprintf(stderr,
1583 		    MSG_INTL(MSG_ERR_BADSYM4), state->file,
1584 		    state->secname, EC_WORD(symndx),
1585 		    demangle(symname, state->flags));
1586 	}
1587 
1588 	/*
1589 	 * If a symbol with non-zero size has a type that
1590 	 * specifies an address, then make sure the location
1591 	 * it references is actually contained within the
1592 	 * section.  UNDEF symbols don't count in this case,
1593 	 * so we ignore them.
1594 	 *
1595 	 * The meaning of the st_value field in a symbol
1596 	 * depends on the type of object. For a relocatable
1597 	 * object, it is the offset within the section.
1598 	 * For sharable objects, it is the offset relative to
1599 	 * the base of the object, and for other types, it is
1600 	 * the virtual address. To get an offset within the
1601 	 * section for non-ET_REL files, we subtract the
1602 	 * base address of the section.
1603 	 */
1604 	if (addr_symtype[type] && (sym->st_size > 0) &&
1605 	    (sym->st_shndx != SHN_UNDEF) && ((sym->st_shndx < SHN_LORESERVE) ||
1606 	    (sym->st_shndx == SHN_XINDEX)) && (tshdr != NULL)) {
1607 		Word v = sym->st_value;
1608 			if (state->ehdr->e_type != ET_REL)
1609 				v -= tshdr->sh_addr;
1610 		if (((v + sym->st_size) > tshdr->sh_size)) {
1611 			(void) fprintf(stderr,
1612 			    MSG_INTL(MSG_ERR_BADSYM6), state->file,
1613 			    state->secname, EC_WORD(symndx),
1614 			    demangle(symname, state->flags),
1615 			    EC_WORD(shndx), EC_XWORD(tshdr->sh_size),
1616 			    EC_XWORD(sym->st_value), EC_XWORD(sym->st_size));
1617 		}
1618 	}
1619 
1620 	/*
1621 	 * A typical symbol table uses the sh_info field to indicate one greater
1622 	 * than the symbol table index of the last local symbol, STB_LOCAL.
1623 	 * Therefore, symbol indexes less than sh_info should have local
1624 	 * binding.  Symbol indexes greater than, or equal to sh_info, should
1625 	 * have global binding.  Note, we exclude UNDEF/NOTY symbols with zero
1626 	 * value and size, as these symbols may be the result of an mcs(1)
1627 	 * section deletion.
1628 	 */
1629 	if (info) {
1630 		uchar_t	bind = ELF_ST_BIND(sym->st_info);
1631 
1632 		if ((symndx < info) && (bind != STB_LOCAL)) {
1633 			(void) fprintf(stderr,
1634 			    MSG_INTL(MSG_ERR_BADSYM7), state->file,
1635 			    state->secname, EC_WORD(symndx),
1636 			    demangle(symname, state->flags), EC_XWORD(info));
1637 
1638 		} else if ((symndx >= info) && (bind == STB_LOCAL) &&
1639 		    ((sym->st_shndx != SHN_UNDEF) ||
1640 		    (ELF_ST_TYPE(sym->st_info) != STT_NOTYPE) ||
1641 		    (sym->st_size != 0) || (sym->st_value != 0))) {
1642 			(void) fprintf(stderr,
1643 			    MSG_INTL(MSG_ERR_BADSYM8), state->file,
1644 			    state->secname, EC_WORD(symndx),
1645 			    demangle(symname, state->flags), EC_XWORD(info));
1646 		}
1647 	}
1648 
1649 	(void) snprintf(index, MAXNDXSIZE,
1650 	    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(disp_symndx));
1651 	Elf_syms_table_entry(0, ELF_DBG_ELFDUMP, index,
1652 	    state->ehdr->e_machine, sym, verndx, gnuver, sec, symname);
1653 }
1654 
1655 /*
1656  * Search for and process any symbol tables.
1657  */
1658 void
1659 symbols(Cache *cache, Word shnum, Ehdr *ehdr, VERSYM_STATE *versym,
1660     const char *file, uint_t flags)
1661 {
1662 	SYMTBL_STATE state;
1663 	Cache *_cache;
1664 	Word secndx;
1665 
1666 	for (secndx = 1; secndx < shnum; secndx++) {
1667 		Word		symcnt;
1668 		Shdr		*shdr;
1669 
1670 		_cache = &cache[secndx];
1671 		shdr = _cache->c_shdr;
1672 
1673 		if ((shdr->sh_type != SHT_SYMTAB) &&
1674 		    (shdr->sh_type != SHT_DYNSYM) &&
1675 		    (shdr->sh_type != SHT_SUNW_LDYNSYM))
1676 			continue;
1677 		if (!match(MATCH_F_ALL, _cache->c_name, secndx, shdr->sh_type))
1678 			continue;
1679 
1680 		if (!init_symtbl_state(&state, cache, shnum, secndx, ehdr,
1681 		    versym, file, flags))
1682 			continue;
1683 		/*
1684 		 * Loop through the symbol tables entries.
1685 		 */
1686 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1687 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMTAB), state.secname);
1688 		Elf_syms_table_title(0, ELF_DBG_ELFDUMP);
1689 
1690 		for (symcnt = 0; symcnt < state.symn; symcnt++)
1691 			output_symbol(&state, symcnt, shdr->sh_info, symcnt,
1692 			    state.sym + symcnt);
1693 	}
1694 }
1695 
1696 /*
1697  * Search for and process any SHT_SUNW_symsort or SHT_SUNW_tlssort sections.
1698  * These sections are always associated with the .SUNW_ldynsym./.dynsym pair.
1699  */
1700 static void
1701 sunw_sort(Cache *cache, Word shnum, Ehdr *ehdr, VERSYM_STATE *versym,
1702     const char *file, uint_t flags)
1703 {
1704 	SYMTBL_STATE	ldynsym_state,	dynsym_state;
1705 	Cache		*sortcache,	*symcache;
1706 	Shdr		*sortshdr,	*symshdr;
1707 	Word		sortsecndx,	symsecndx;
1708 	Word		ldynsym_cnt;
1709 	Word		*ndx;
1710 	Word		ndxn;
1711 	int		output_cnt = 0;
1712 	Conv_inv_buf_t	inv_buf;
1713 
1714 	for (sortsecndx = 1; sortsecndx < shnum; sortsecndx++) {
1715 
1716 		sortcache = &cache[sortsecndx];
1717 		sortshdr = sortcache->c_shdr;
1718 
1719 		if ((sortshdr->sh_type != SHT_SUNW_symsort) &&
1720 		    (sortshdr->sh_type != SHT_SUNW_tlssort))
1721 			continue;
1722 		if (!match(MATCH_F_ALL, sortcache->c_name, sortsecndx,
1723 		    sortshdr->sh_type))
1724 			continue;
1725 
1726 		/*
1727 		 * If the section references a SUNW_ldynsym, then we
1728 		 * expect to see the associated .dynsym immediately
1729 		 * following. If it references a .dynsym, there is no
1730 		 * SUNW_ldynsym. If it is any other type, then we don't
1731 		 * know what to do with it.
1732 		 */
1733 		if ((sortshdr->sh_link == 0) || (sortshdr->sh_link >= shnum)) {
1734 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
1735 			    file, sortcache->c_name,
1736 			    EC_WORD(sortshdr->sh_link));
1737 			continue;
1738 		}
1739 		symcache = &cache[sortshdr->sh_link];
1740 		symshdr = symcache->c_shdr;
1741 		symsecndx = sortshdr->sh_link;
1742 		ldynsym_cnt = 0;
1743 		switch (symshdr->sh_type) {
1744 		case SHT_SUNW_LDYNSYM:
1745 			if (!init_symtbl_state(&ldynsym_state, cache, shnum,
1746 			    symsecndx, ehdr, versym, file, flags))
1747 				continue;
1748 			ldynsym_cnt = ldynsym_state.symn;
1749 			/*
1750 			 * We know that the dynsym follows immediately
1751 			 * after the SUNW_ldynsym, and so, should be at
1752 			 * (sortshdr->sh_link + 1). However, elfdump is a
1753 			 * diagnostic tool, so we do the full paranoid
1754 			 * search instead.
1755 			 */
1756 			for (symsecndx = 1; symsecndx < shnum; symsecndx++) {
1757 				symcache = &cache[symsecndx];
1758 				symshdr = symcache->c_shdr;
1759 				if (symshdr->sh_type == SHT_DYNSYM)
1760 					break;
1761 			}
1762 			if (symsecndx >= shnum) {	/* Dynsym not found! */
1763 				(void) fprintf(stderr,
1764 				    MSG_INTL(MSG_ERR_NODYNSYM),
1765 				    file, sortcache->c_name);
1766 				continue;
1767 			}
1768 			/* Fallthrough to process associated dynsym */
1769 			/*FALLTHROUGH*/
1770 		case SHT_DYNSYM:
1771 			if (!init_symtbl_state(&dynsym_state, cache, shnum,
1772 			    symsecndx, ehdr, versym, file, flags))
1773 				continue;
1774 			break;
1775 		default:
1776 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADNDXSEC),
1777 			    file, sortcache->c_name, conv_sec_type(
1778 			    ehdr->e_machine, symshdr->sh_type, 0, &inv_buf));
1779 			continue;
1780 		}
1781 
1782 		/*
1783 		 * Output header
1784 		 */
1785 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1786 		if (ldynsym_cnt > 0) {
1787 			dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMSORT2),
1788 			    sortcache->c_name, ldynsym_state.secname,
1789 			    dynsym_state.secname);
1790 			/*
1791 			 * The data for .SUNW_ldynsym and dynsym sections
1792 			 * is supposed to be adjacent with SUNW_ldynsym coming
1793 			 * first. Check, and issue a warning if it isn't so.
1794 			 */
1795 			if (((ldynsym_state.sym + ldynsym_state.symn)
1796 			    != dynsym_state.sym) &&
1797 			    ((flags & FLG_CTL_FAKESHDR) == 0))
1798 				(void) fprintf(stderr,
1799 				    MSG_INTL(MSG_ERR_LDYNNOTADJ), file,
1800 				    ldynsym_state.secname,
1801 				    dynsym_state.secname);
1802 		} else {
1803 			dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMSORT1),
1804 			    sortcache->c_name, dynsym_state.secname);
1805 		}
1806 		Elf_syms_table_title(0, ELF_DBG_ELFDUMP);
1807 
1808 		/* If not first one, insert a line of whitespace */
1809 		if (output_cnt++ > 0)
1810 			dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1811 
1812 		/*
1813 		 * SUNW_dynsymsort and SUNW_dyntlssort are arrays of
1814 		 * symbol indices. Iterate over the array entries,
1815 		 * dispaying the referenced symbols.
1816 		 */
1817 		ndxn = sortshdr->sh_size / sortshdr->sh_entsize;
1818 		ndx = (Word *)sortcache->c_data->d_buf;
1819 		for (; ndxn-- > 0; ndx++) {
1820 			if (*ndx >= ldynsym_cnt) {
1821 				Word sec_ndx = *ndx - ldynsym_cnt;
1822 
1823 				output_symbol(&dynsym_state, sec_ndx, 0,
1824 				    *ndx, dynsym_state.sym + sec_ndx);
1825 			} else {
1826 				output_symbol(&ldynsym_state, *ndx, 0,
1827 				    *ndx, ldynsym_state.sym + *ndx);
1828 			}
1829 		}
1830 	}
1831 }
1832 
1833 /*
1834  * Search for and process any relocation sections.
1835  */
1836 static void
1837 reloc(Cache *cache, Word shnum, Ehdr *ehdr, const char *file,
1838     uint_t flags)
1839 {
1840 	Word	cnt;
1841 
1842 	for (cnt = 1; cnt < shnum; cnt++) {
1843 		Word		type, symnum;
1844 		Xword		relndx, relnum, relsize;
1845 		void		*rels;
1846 		Sym		*syms;
1847 		Cache		*symsec, *strsec;
1848 		Cache		*_cache = &cache[cnt];
1849 		Shdr		*shdr = _cache->c_shdr;
1850 		char		*relname = _cache->c_name;
1851 		Conv_inv_buf_t	inv_buf;
1852 
1853 		if (((type = shdr->sh_type) != SHT_RELA) &&
1854 		    (type != SHT_REL))
1855 			continue;
1856 		if (!match(MATCH_F_ALL, relname, cnt, type))
1857 			continue;
1858 
1859 		/*
1860 		 * Decide entry size.
1861 		 */
1862 		if (((relsize = shdr->sh_entsize) == 0) ||
1863 		    (relsize > shdr->sh_size)) {
1864 			if (type == SHT_RELA)
1865 				relsize = sizeof (Rela);
1866 			else
1867 				relsize = sizeof (Rel);
1868 		}
1869 
1870 		/*
1871 		 * Determine the number of relocations available.
1872 		 */
1873 		if (shdr->sh_size == 0) {
1874 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
1875 			    file, relname);
1876 			continue;
1877 		}
1878 		if (_cache->c_data == NULL)
1879 			continue;
1880 
1881 		rels = _cache->c_data->d_buf;
1882 		relnum = shdr->sh_size / relsize;
1883 
1884 		/*
1885 		 * Get the data buffer for the associated symbol table and
1886 		 * string table.
1887 		 */
1888 		if (stringtbl(cache, 1, cnt, shnum, file,
1889 		    &symnum, &symsec, &strsec) == 0)
1890 			continue;
1891 
1892 		syms = symsec->c_data->d_buf;
1893 
1894 		/*
1895 		 * Loop through the relocation entries.
1896 		 */
1897 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1898 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_RELOC), _cache->c_name);
1899 		Elf_reloc_title(0, ELF_DBG_ELFDUMP, type);
1900 
1901 		for (relndx = 0; relndx < relnum; relndx++,
1902 		    rels = (void *)((char *)rels + relsize)) {
1903 			Half		mach = ehdr->e_machine;
1904 			char		section[BUFSIZ];
1905 			const char	*symname;
1906 			Word		symndx, reltype;
1907 			Rela		*rela;
1908 			Rel		*rel;
1909 
1910 			/*
1911 			 * Unravel the relocation and determine the symbol with
1912 			 * which this relocation is associated.
1913 			 */
1914 			if (type == SHT_RELA) {
1915 				rela = (Rela *)rels;
1916 				symndx = ELF_R_SYM(rela->r_info);
1917 				reltype = ELF_R_TYPE(rela->r_info, mach);
1918 			} else {
1919 				rel = (Rel *)rels;
1920 				symndx = ELF_R_SYM(rel->r_info);
1921 				reltype = ELF_R_TYPE(rel->r_info, mach);
1922 			}
1923 
1924 			symname = relsymname(cache, _cache, strsec, symndx,
1925 			    symnum, relndx, syms, section, BUFSIZ, file,
1926 			    flags);
1927 
1928 			/*
1929 			 * A zero symbol index is only valid for a few
1930 			 * relocations.
1931 			 */
1932 			if (symndx == 0) {
1933 				int	badrel = 0;
1934 
1935 				if ((mach == EM_SPARC) ||
1936 				    (mach == EM_SPARC32PLUS) ||
1937 				    (mach == EM_SPARCV9)) {
1938 					if ((reltype != R_SPARC_NONE) &&
1939 					    (reltype != R_SPARC_REGISTER) &&
1940 					    (reltype != R_SPARC_RELATIVE))
1941 						badrel++;
1942 				} else if (mach == EM_386) {
1943 					if ((reltype != R_386_NONE) &&
1944 					    (reltype != R_386_RELATIVE))
1945 						badrel++;
1946 				} else if (mach == EM_AMD64) {
1947 					if ((reltype != R_AMD64_NONE) &&
1948 					    (reltype != R_AMD64_RELATIVE))
1949 						badrel++;
1950 				}
1951 
1952 				if (badrel) {
1953 					(void) fprintf(stderr,
1954 					    MSG_INTL(MSG_ERR_BADREL1), file,
1955 					    conv_reloc_type(mach, reltype,
1956 					    0, &inv_buf));
1957 				}
1958 			}
1959 
1960 			Elf_reloc_entry_1(0, ELF_DBG_ELFDUMP,
1961 			    MSG_ORIG(MSG_STR_EMPTY), ehdr->e_machine, type,
1962 			    rels, relname, symname, 0);
1963 		}
1964 	}
1965 }
1966 
1967 
1968 /*
1969  * This value controls which test dyn_test() performs.
1970  */
1971 typedef enum { DYN_TEST_ADDR, DYN_TEST_SIZE, DYN_TEST_ENTSIZE } dyn_test_t;
1972 
1973 /*
1974  * Used by dynamic() to compare the value of a dynamic element against
1975  * the starting address of the section it references.
1976  *
1977  * entry:
1978  *	test_type - Specify which dyn item is being tested.
1979  *	sh_type - SHT_* type value for required section.
1980  *	sec_cache - Cache entry for section, or NULL if the object lacks
1981  *		a section of this type.
1982  *	dyn - Dyn entry to be tested
1983  *	dynsec_cnt - # of dynamic section being examined. The first
1984  *		dynamic section is 1, the next is 2, and so on...
1985  *	ehdr - ELF header for file
1986  *	file - Name of file
1987  */
1988 static void
1989 dyn_test(dyn_test_t test_type, Word sh_type, Cache *sec_cache, Dyn *dyn,
1990     Word dynsec_cnt, Ehdr *ehdr, const char *file)
1991 {
1992 	Conv_inv_buf_t	buf1, buf2;
1993 
1994 	/*
1995 	 * These tests are based around the implicit assumption that
1996 	 * there is only one dynamic section in an object, and also only
1997 	 * one of the sections it references. We have therefore gathered
1998 	 * all of the necessary information to test this in a single pass
1999 	 * over the section headers, which is very efficient. We are not
2000 	 * aware of any case where more than one dynamic section would
2001 	 * be meaningful in an ELF object, so this is a reasonable solution.
2002 	 *
2003 	 * To test multiple dynamic sections correctly would be more
2004 	 * expensive in code and time. We would have to build a data structure
2005 	 * containing all the dynamic elements. Then, we would use the address
2006 	 * to locate the section it references and ensure the section is of
2007 	 * the right type and that the address in the dynamic element is
2008 	 * to the start of the section. Then, we could check the size and
2009 	 * entsize values against those same sections. This is O(n^2), and
2010 	 * also complicated.
2011 	 *
2012 	 * In the highly unlikely case that there is more than one dynamic
2013 	 * section, we only test the first one, and simply allow the values
2014 	 * of the subsequent one to be displayed unchallenged.
2015 	 */
2016 	if (dynsec_cnt != 1)
2017 		return;
2018 
2019 	/*
2020 	 * A DT_ item that references a section address should always find
2021 	 * the section in the file.
2022 	 */
2023 	if (sec_cache == NULL) {
2024 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_DYNNOBCKSEC), file,
2025 		    conv_sec_type(ehdr->e_machine, sh_type, 0, &buf1),
2026 		    conv_dyn_tag(dyn->d_tag, ehdr->e_machine, 0, &buf2));
2027 		return;
2028 	}
2029 
2030 
2031 	switch (test_type) {
2032 	case DYN_TEST_ADDR:
2033 		/* The section address should match the DT_ item value */
2034 		if (dyn->d_un.d_val != sec_cache->c_shdr->sh_addr)
2035 			(void) fprintf(stderr,
2036 			    MSG_INTL(MSG_ERR_DYNBADADDR), file,
2037 			    conv_dyn_tag(dyn->d_tag, ehdr->e_machine, 0, &buf1),
2038 			    EC_ADDR(dyn->d_un.d_val), sec_cache->c_ndx,
2039 			    sec_cache->c_name,
2040 			    EC_ADDR(sec_cache->c_shdr->sh_addr));
2041 		break;
2042 
2043 	case DYN_TEST_SIZE:
2044 		/* The section size should match the DT_ item value */
2045 		if (dyn->d_un.d_val != sec_cache->c_shdr->sh_size)
2046 			(void) fprintf(stderr,
2047 			    MSG_INTL(MSG_ERR_DYNBADSIZE), file,
2048 			    conv_dyn_tag(dyn->d_tag, ehdr->e_machine, 0, &buf1),
2049 			    EC_XWORD(dyn->d_un.d_val),
2050 			    sec_cache->c_ndx, sec_cache->c_name,
2051 			    EC_XWORD(sec_cache->c_shdr->sh_size));
2052 		break;
2053 
2054 	case DYN_TEST_ENTSIZE:
2055 		/* The sh_entsize value should match the DT_ item value */
2056 		if (dyn->d_un.d_val != sec_cache->c_shdr->sh_entsize)
2057 			(void) fprintf(stderr,
2058 			    MSG_INTL(MSG_ERR_DYNBADENTSIZE), file,
2059 			    conv_dyn_tag(dyn->d_tag, ehdr->e_machine, 0, &buf1),
2060 			    EC_XWORD(dyn->d_un.d_val),
2061 			    sec_cache->c_ndx, sec_cache->c_name,
2062 			    EC_XWORD(sec_cache->c_shdr->sh_entsize));
2063 		break;
2064 	}
2065 }
2066 
2067 
2068 /*
2069  * Search for and process a .dynamic section.
2070  */
2071 static void
2072 dynamic(Cache *cache, Word shnum, Ehdr *ehdr, const char *file)
2073 {
2074 	struct {
2075 		Cache	*dynstr;
2076 		Cache	*dynsym;
2077 		Cache	*hash;
2078 		Cache	*fini;
2079 		Cache	*fini_array;
2080 		Cache	*init;
2081 		Cache	*init_array;
2082 		Cache	*preinit_array;
2083 		Cache	*rel;
2084 		Cache	*rela;
2085 		Cache	*sunw_cap;
2086 		Cache	*sunw_ldynsym;
2087 		Cache	*sunw_move;
2088 		Cache	*sunw_syminfo;
2089 		Cache	*sunw_symsort;
2090 		Cache	*sunw_tlssort;
2091 		Cache	*sunw_verdef;
2092 		Cache	*sunw_verneed;
2093 		Cache	*sunw_versym;
2094 	} sec;
2095 	Word	dynsec_ndx;
2096 	Word	dynsec_num;
2097 	int	dynsec_cnt;
2098 	Word	cnt;
2099 
2100 	/*
2101 	 * Make a pass over all the sections, gathering section information
2102 	 * we'll need below.
2103 	 */
2104 	dynsec_num = 0;
2105 	bzero(&sec, sizeof (sec));
2106 	for (cnt = 1; cnt < shnum; cnt++) {
2107 		Cache	*_cache = &cache[cnt];
2108 
2109 		switch (_cache->c_shdr->sh_type) {
2110 		case SHT_DYNAMIC:
2111 			if (dynsec_num == 0) {
2112 				dynsec_ndx = cnt;
2113 
2114 				/* Does it have a valid string table? */
2115 				(void) stringtbl(cache, 0, cnt, shnum, file,
2116 				    0, 0, &sec.dynstr);
2117 			}
2118 			dynsec_num++;
2119 			break;
2120 
2121 
2122 		case SHT_PROGBITS:
2123 			/*
2124 			 * We want to detect the .init and .fini sections,
2125 			 * if present. These are SHT_PROGBITS, so all we
2126 			 * have to go on is the section name. Normally comparing
2127 			 * names is a bad idea, but there are some special
2128 			 * names (i.e. .init/.fini/.interp) that are very
2129 			 * difficult to use in any other context, and for
2130 			 * these symbols, we do the heuristic match.
2131 			 */
2132 			if (strcmp(_cache->c_name,
2133 			    MSG_ORIG(MSG_ELF_INIT)) == 0) {
2134 				if (sec.init == NULL)
2135 					sec.init = _cache;
2136 			} else if (strcmp(_cache->c_name,
2137 			    MSG_ORIG(MSG_ELF_FINI)) == 0) {
2138 				if (sec.fini == NULL)
2139 					sec.fini = _cache;
2140 			}
2141 			break;
2142 
2143 		case SHT_REL:
2144 			/*
2145 			 * We want the SHT_REL section with the lowest
2146 			 * offset. The linker gathers them together,
2147 			 * and puts the address of the first one
2148 			 * into the DT_REL dynamic element.
2149 			 */
2150 			if ((sec.rel == NULL) ||
2151 			    (_cache->c_shdr->sh_offset <
2152 			    sec.rel->c_shdr->sh_offset))
2153 				sec.rel = _cache;
2154 			break;
2155 
2156 		case SHT_RELA:
2157 			/* RELA is handled just like RELA above */
2158 			if ((sec.rela == NULL) ||
2159 			    (_cache->c_shdr->sh_offset <
2160 			    sec.rela->c_shdr->sh_offset))
2161 				sec.rela = _cache;
2162 			break;
2163 
2164 		/*
2165 		 * The GRAB macro is used for the simple case in which
2166 		 * we simply grab the first section of the desired type.
2167 		 */
2168 #define	GRAB(_sec_type, _sec_field) \
2169 		case _sec_type: \
2170 			if (sec._sec_field == NULL) \
2171 				sec._sec_field = _cache; \
2172 				break
2173 		GRAB(SHT_DYNSYM,	dynsym);
2174 		GRAB(SHT_FINI_ARRAY,	fini_array);
2175 		GRAB(SHT_HASH,		hash);
2176 		GRAB(SHT_INIT_ARRAY,	init_array);
2177 		GRAB(SHT_SUNW_move,	sunw_move);
2178 		GRAB(SHT_PREINIT_ARRAY,	preinit_array);
2179 		GRAB(SHT_SUNW_cap,	sunw_cap);
2180 		GRAB(SHT_SUNW_LDYNSYM,	sunw_ldynsym);
2181 		GRAB(SHT_SUNW_syminfo,	sunw_syminfo);
2182 		GRAB(SHT_SUNW_symsort,	sunw_symsort);
2183 		GRAB(SHT_SUNW_tlssort,	sunw_tlssort);
2184 		GRAB(SHT_SUNW_verdef,	sunw_verdef);
2185 		GRAB(SHT_SUNW_verneed,	sunw_verneed);
2186 		GRAB(SHT_SUNW_versym,	sunw_versym);
2187 #undef GRAB
2188 		}
2189 	}
2190 
2191 	/*
2192 	 * If no dynamic section, return immediately. If more than one
2193 	 * dynamic section, then something odd is going on and an error
2194 	 * is in order, but then continue on and display them all.
2195 	 */
2196 	if (dynsec_num == 0)
2197 		return;
2198 	if (dynsec_num > 1)
2199 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_MULTDYN),
2200 		    file, EC_WORD(dynsec_num));
2201 
2202 
2203 	dynsec_cnt = 0;
2204 	for (cnt = dynsec_ndx; (cnt < shnum) && (dynsec_cnt < dynsec_num);
2205 	    cnt++) {
2206 		Dyn	*dyn;
2207 		ulong_t	numdyn;
2208 		int	ndx, end_ndx;
2209 		Cache	*_cache = &cache[cnt], *strsec;
2210 		Shdr	*shdr = _cache->c_shdr;
2211 		int	dumped = 0;
2212 
2213 		if (shdr->sh_type != SHT_DYNAMIC)
2214 			continue;
2215 		dynsec_cnt++;
2216 
2217 		/*
2218 		 * Verify the associated string table section.
2219 		 */
2220 		if (stringtbl(cache, 0, cnt, shnum, file, 0, 0, &strsec) == 0)
2221 			continue;
2222 
2223 		if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
2224 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
2225 			    file, _cache->c_name);
2226 			continue;
2227 		}
2228 		if (_cache->c_data == NULL)
2229 			continue;
2230 
2231 		numdyn = shdr->sh_size / shdr->sh_entsize;
2232 		dyn = (Dyn *)_cache->c_data->d_buf;
2233 
2234 		/*
2235 		 * We expect the REL/RELA entries to reference the reloc
2236 		 * section with the lowest address. However, this is
2237 		 * not true for dumped objects. Detect if this object has
2238 		 * been dumped so that we can skip the reloc address test
2239 		 * in that case.
2240 		 */
2241 		for (ndx = 0; ndx < numdyn; dyn++, ndx++) {
2242 			if (dyn->d_tag == DT_FLAGS_1) {
2243 				dumped = (dyn->d_un.d_val & DF_1_CONFALT) != 0;
2244 				break;
2245 			}
2246 		}
2247 		dyn = (Dyn *)_cache->c_data->d_buf;
2248 
2249 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2250 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_DYNAMIC), _cache->c_name);
2251 
2252 		Elf_dyn_title(0);
2253 
2254 		for (ndx = 0; ndx < numdyn; dyn++, ndx++) {
2255 			union {
2256 				Conv_inv_buf_t		inv;
2257 				Conv_dyn_flag_buf_t	flag;
2258 				Conv_dyn_flag1_buf_t	flag1;
2259 				Conv_dyn_posflag1_buf_t	posflag1;
2260 				Conv_dyn_feature1_buf_t	feature1;
2261 			} c_buf;
2262 			const char	*name = NULL;
2263 
2264 			/*
2265 			 * Print the information numerically, and if possible
2266 			 * as a string. If a string is available, name is
2267 			 * set to reference it.
2268 			 *
2269 			 * Also, take this opportunity to sanity check
2270 			 * the values of DT elements. In the code above,
2271 			 * we gathered information on sections that are
2272 			 * referenced by the dynamic section. Here, we
2273 			 * compare the attributes of those sections to
2274 			 * the DT_ items that reference them and report
2275 			 * on inconsistencies.
2276 			 *
2277 			 * Things not currently tested that could be improved
2278 			 * in later revisions include:
2279 			 *	- We don't check PLT or GOT related items
2280 			 *	- We don't handle computing the lengths of
2281 			 *		relocation arrays. To handle this
2282 			 *		requires examining data that spans
2283 			 *		across sections, in a contiguous span
2284 			 *		within a single segment.
2285 			 *	- DT_VERDEFNUM and DT_VERNEEDNUM can't be
2286 			 *		verified without parsing the sections.
2287 			 *	- We don't handle DT_SUNW_SYMSZ, which would
2288 			 *		be the sum of the lengths of .dynsym and
2289 			 *		.SUNW_ldynsym
2290 			 *	- DT_SUNW_STRPAD can't be verified other than
2291 			 *		to check that it's not larger than
2292 			 *		the string table.
2293 			 *	- Some items come in "all or none" clusters
2294 			 *		that give an address, element size,
2295 			 *		and data length in bytes. We don't
2296 			 *		verify that there are no missing items
2297 			 *		in such groups.
2298 			 */
2299 			switch (dyn->d_tag) {
2300 			case DT_NULL:
2301 				/*
2302 				 * Special case: DT_NULLs can come in groups
2303 				 * that we prefer to reduce to a single line.
2304 				 */
2305 				end_ndx = ndx;
2306 				while ((end_ndx < (numdyn - 1)) &&
2307 				    ((dyn + 1)->d_tag == DT_NULL)) {
2308 					dyn++;
2309 					end_ndx++;
2310 				}
2311 				Elf_dyn_null_entry(0, dyn, ndx, end_ndx);
2312 				ndx = end_ndx;
2313 				continue;
2314 
2315 			/*
2316 			 * String items all reference the dynstr. The string()
2317 			 * function does the necessary sanity checking.
2318 			 */
2319 			case DT_NEEDED:
2320 			case DT_SONAME:
2321 			case DT_FILTER:
2322 			case DT_AUXILIARY:
2323 			case DT_CONFIG:
2324 			case DT_RPATH:
2325 			case DT_RUNPATH:
2326 			case DT_USED:
2327 			case DT_DEPAUDIT:
2328 			case DT_AUDIT:
2329 			case DT_SUNW_AUXILIARY:
2330 			case DT_SUNW_FILTER:
2331 				name = string(_cache, ndx, strsec,
2332 				    file, dyn->d_un.d_ptr);
2333 				break;
2334 
2335 			case DT_FLAGS:
2336 				name = conv_dyn_flag(dyn->d_un.d_val,
2337 				    0, &c_buf.flag);
2338 				break;
2339 			case DT_FLAGS_1:
2340 				name = conv_dyn_flag1(dyn->d_un.d_val, 0,
2341 				    &c_buf.flag1);
2342 				break;
2343 			case DT_POSFLAG_1:
2344 				name = conv_dyn_posflag1(dyn->d_un.d_val, 0,
2345 				    &c_buf.posflag1);
2346 				break;
2347 			case DT_FEATURE_1:
2348 				name = conv_dyn_feature1(dyn->d_un.d_val, 0,
2349 				    &c_buf.feature1);
2350 				break;
2351 			case DT_DEPRECATED_SPARC_REGISTER:
2352 				name = MSG_INTL(MSG_STR_DEPRECATED);
2353 				break;
2354 
2355 			case DT_SUNW_LDMACH:
2356 				name = conv_ehdr_mach((Half)dyn->d_un.d_val, 0,
2357 				    &c_buf.inv);
2358 				break;
2359 
2360 			/*
2361 			 * Cases below this point are strictly sanity checking,
2362 			 * and do not generate a name string. The TEST_ macros
2363 			 * are used to hide the boilerplate arguments neeeded
2364 			 * by dyn_test().
2365 			 */
2366 #define	TEST_ADDR(_sh_type, _sec_field) \
2367 				dyn_test(DYN_TEST_ADDR, _sh_type, \
2368 				    sec._sec_field, dyn, dynsec_cnt, ehdr, file)
2369 #define	TEST_SIZE(_sh_type, _sec_field) \
2370 				dyn_test(DYN_TEST_SIZE, _sh_type, \
2371 				    sec._sec_field, dyn, dynsec_cnt, ehdr, file)
2372 #define	TEST_ENTSIZE(_sh_type, _sec_field) \
2373 				dyn_test(DYN_TEST_ENTSIZE, _sh_type, \
2374 				    sec._sec_field, dyn, dynsec_cnt, ehdr, file)
2375 
2376 			case DT_FINI:
2377 				TEST_ADDR(SHT_PROGBITS, fini);
2378 				break;
2379 
2380 			case DT_FINI_ARRAY:
2381 				TEST_ADDR(SHT_FINI_ARRAY, fini_array);
2382 				break;
2383 
2384 			case DT_FINI_ARRAYSZ:
2385 				TEST_SIZE(SHT_FINI_ARRAY, fini_array);
2386 				break;
2387 
2388 			case DT_HASH:
2389 				TEST_ADDR(SHT_HASH, hash);
2390 				break;
2391 
2392 			case DT_INIT:
2393 				TEST_ADDR(SHT_PROGBITS, init);
2394 				break;
2395 
2396 			case DT_INIT_ARRAY:
2397 				TEST_ADDR(SHT_INIT_ARRAY, init_array);
2398 				break;
2399 
2400 			case DT_INIT_ARRAYSZ:
2401 				TEST_SIZE(SHT_INIT_ARRAY, init_array);
2402 				break;
2403 
2404 			case DT_MOVEENT:
2405 				TEST_ENTSIZE(SHT_SUNW_move, sunw_move);
2406 				break;
2407 
2408 			case DT_MOVESZ:
2409 				TEST_SIZE(SHT_SUNW_move, sunw_move);
2410 				break;
2411 
2412 			case DT_MOVETAB:
2413 				TEST_ADDR(SHT_SUNW_move, sunw_move);
2414 				break;
2415 
2416 			case DT_PREINIT_ARRAY:
2417 				TEST_ADDR(SHT_PREINIT_ARRAY, preinit_array);
2418 				break;
2419 
2420 			case DT_PREINIT_ARRAYSZ:
2421 				TEST_SIZE(SHT_PREINIT_ARRAY, preinit_array);
2422 				break;
2423 
2424 			case DT_REL:
2425 				if (!dumped)
2426 					TEST_ADDR(SHT_REL, rel);
2427 				break;
2428 
2429 			case DT_RELENT:
2430 				TEST_ENTSIZE(SHT_REL, rel);
2431 				break;
2432 
2433 			case DT_RELA:
2434 				if (!dumped)
2435 					TEST_ADDR(SHT_RELA, rela);
2436 				break;
2437 
2438 			case DT_RELAENT:
2439 				TEST_ENTSIZE(SHT_RELA, rela);
2440 				break;
2441 
2442 			case DT_STRTAB:
2443 				TEST_ADDR(SHT_STRTAB, dynstr);
2444 				break;
2445 
2446 			case DT_STRSZ:
2447 				TEST_SIZE(SHT_STRTAB, dynstr);
2448 				break;
2449 
2450 			case DT_SUNW_CAP:
2451 				TEST_ADDR(SHT_SUNW_cap, sunw_cap);
2452 				break;
2453 
2454 			case DT_SUNW_SYMTAB:
2455 				TEST_ADDR(SHT_SUNW_LDYNSYM, sunw_ldynsym);
2456 				break;
2457 
2458 			case DT_SYMENT:
2459 				TEST_ENTSIZE(SHT_DYNSYM, dynsym);
2460 				break;
2461 
2462 			case DT_SYMINENT:
2463 				TEST_ENTSIZE(SHT_SUNW_syminfo, sunw_syminfo);
2464 				break;
2465 
2466 			case DT_SYMINFO:
2467 				TEST_ADDR(SHT_SUNW_syminfo, sunw_syminfo);
2468 				break;
2469 
2470 			case DT_SYMINSZ:
2471 				TEST_SIZE(SHT_SUNW_syminfo, sunw_syminfo);
2472 				break;
2473 
2474 			case DT_SYMTAB:
2475 				TEST_ADDR(SHT_DYNSYM, dynsym);
2476 				break;
2477 
2478 			case DT_SUNW_SORTENT:
2479 				/*
2480 				 * This entry is related to both the symsort and
2481 				 * tlssort sections.
2482 				 */
2483 				{
2484 					int test_tls =
2485 					    (sec.sunw_tlssort != NULL);
2486 					int test_sym =
2487 					    (sec.sunw_symsort != NULL) ||
2488 					    !test_tls;
2489 					if (test_sym)
2490 						TEST_ENTSIZE(SHT_SUNW_symsort,
2491 						    sunw_symsort);
2492 					if (test_tls)
2493 						TEST_ENTSIZE(SHT_SUNW_tlssort,
2494 						    sunw_tlssort);
2495 				}
2496 				break;
2497 
2498 
2499 			case DT_SUNW_SYMSORT:
2500 				TEST_ADDR(SHT_SUNW_symsort, sunw_symsort);
2501 				break;
2502 
2503 			case DT_SUNW_SYMSORTSZ:
2504 				TEST_SIZE(SHT_SUNW_symsort, sunw_symsort);
2505 				break;
2506 
2507 			case DT_SUNW_TLSSORT:
2508 				TEST_ADDR(SHT_SUNW_tlssort, sunw_tlssort);
2509 				break;
2510 
2511 			case DT_SUNW_TLSSORTSZ:
2512 				TEST_SIZE(SHT_SUNW_tlssort, sunw_tlssort);
2513 				break;
2514 
2515 			case DT_VERDEF:
2516 				TEST_ADDR(SHT_SUNW_verdef, sunw_verdef);
2517 				break;
2518 
2519 			case DT_VERNEED:
2520 				TEST_ADDR(SHT_SUNW_verneed, sunw_verneed);
2521 				break;
2522 
2523 			case DT_VERSYM:
2524 				TEST_ADDR(SHT_SUNW_versym, sunw_versym);
2525 				break;
2526 #undef TEST_ADDR
2527 #undef TEST_SIZE
2528 #undef TEST_ENTSIZE
2529 			}
2530 
2531 			if (name == NULL)
2532 				name = MSG_ORIG(MSG_STR_EMPTY);
2533 			Elf_dyn_entry(0, dyn, ndx, name, ehdr->e_machine);
2534 		}
2535 	}
2536 }
2537 
2538 /*
2539  * Search for and process a MOVE section.
2540  */
2541 static void
2542 move(Cache *cache, Word shnum, const char *file, uint_t flags)
2543 {
2544 	Word		cnt;
2545 	const char	*fmt = 0;
2546 
2547 	for (cnt = 1; cnt < shnum; cnt++) {
2548 		Word	movenum, symnum, ndx;
2549 		Sym	*syms;
2550 		Cache	*_cache = &cache[cnt];
2551 		Shdr	*shdr = _cache->c_shdr;
2552 		Cache	*symsec, *strsec;
2553 		Move	*move;
2554 
2555 		if (shdr->sh_type != SHT_SUNW_move)
2556 			continue;
2557 		if (!match(MATCH_F_ALL, _cache->c_name, cnt, shdr->sh_type))
2558 			continue;
2559 
2560 		/*
2561 		 * Determine the move data and number.
2562 		 */
2563 		if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
2564 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
2565 			    file, _cache->c_name);
2566 			continue;
2567 		}
2568 		if (_cache->c_data == NULL)
2569 			continue;
2570 
2571 		move = (Move *)_cache->c_data->d_buf;
2572 		movenum = shdr->sh_size / shdr->sh_entsize;
2573 
2574 		/*
2575 		 * Get the data buffer for the associated symbol table and
2576 		 * string table.
2577 		 */
2578 		if (stringtbl(cache, 1, cnt, shnum, file,
2579 		    &symnum, &symsec, &strsec) == 0)
2580 			return;
2581 
2582 		syms = (Sym *)symsec->c_data->d_buf;
2583 
2584 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2585 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_MOVE), _cache->c_name);
2586 		dbg_print(0, MSG_INTL(MSG_MOVE_TITLE));
2587 
2588 		if (fmt == 0)
2589 			fmt = MSG_INTL(MSG_MOVE_ENTRY);
2590 
2591 		for (ndx = 0; ndx < movenum; move++, ndx++) {
2592 			const char	*symname;
2593 			char		index[MAXNDXSIZE], section[BUFSIZ];
2594 			Word		symndx, shndx;
2595 			Sym		*sym;
2596 
2597 			/*
2598 			 * Check for null entries
2599 			 */
2600 			if ((move->m_info == 0) && (move->m_value == 0) &&
2601 			    (move->m_poffset == 0) && (move->m_repeat == 0) &&
2602 			    (move->m_stride == 0)) {
2603 				dbg_print(0, fmt, MSG_ORIG(MSG_STR_EMPTY),
2604 				    EC_XWORD(move->m_poffset), 0, 0, 0,
2605 				    EC_LWORD(0), MSG_ORIG(MSG_STR_EMPTY));
2606 				continue;
2607 			}
2608 			if (((symndx = ELF_M_SYM(move->m_info)) == 0) ||
2609 			    (symndx >= symnum)) {
2610 				(void) fprintf(stderr,
2611 				    MSG_INTL(MSG_ERR_BADMINFO), file,
2612 				    _cache->c_name, EC_XWORD(move->m_info));
2613 
2614 				(void) snprintf(index, MAXNDXSIZE,
2615 				    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(symndx));
2616 				dbg_print(0, fmt, index,
2617 				    EC_XWORD(move->m_poffset),
2618 				    ELF_M_SIZE(move->m_info), move->m_repeat,
2619 				    move->m_stride, move->m_value,
2620 				    MSG_INTL(MSG_STR_UNKNOWN));
2621 				continue;
2622 			}
2623 
2624 			symname = relsymname(cache, _cache, strsec,
2625 			    symndx, symnum, ndx, syms, section, BUFSIZ, file,
2626 			    flags);
2627 			sym = (Sym *)(syms + symndx);
2628 
2629 			/*
2630 			 * Additional sanity check.
2631 			 */
2632 			shndx = sym->st_shndx;
2633 			if (!((shndx == SHN_COMMON) ||
2634 			    (((shndx >= 1) && (shndx <= shnum)) &&
2635 			    (cache[shndx].c_shdr)->sh_type == SHT_NOBITS))) {
2636 				(void) fprintf(stderr,
2637 				    MSG_INTL(MSG_ERR_BADSYM2), file,
2638 				    _cache->c_name, EC_WORD(symndx),
2639 				    demangle(symname, flags));
2640 			}
2641 
2642 			(void) snprintf(index, MAXNDXSIZE,
2643 			    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(symndx));
2644 			dbg_print(0, fmt, index, EC_XWORD(move->m_poffset),
2645 			    ELF_M_SIZE(move->m_info), move->m_repeat,
2646 			    move->m_stride, move->m_value,
2647 			    demangle(symname, flags));
2648 		}
2649 	}
2650 }
2651 
2652 /*
2653  * Traverse a note section analyzing each note information block.
2654  * The data buffers size is used to validate references before they are made,
2655  * and is decremented as each element is processed.
2656  */
2657 void
2658 note_entry(Cache *cache, Word *data, size_t size, const char *file)
2659 {
2660 	size_t	bsize = size;
2661 
2662 	/*
2663 	 * Print out a single `note' information block.
2664 	 */
2665 	while (size > 0) {
2666 		size_t	namesz, descsz, type, pad, noteoff;
2667 
2668 		noteoff = bsize - size;
2669 		/*
2670 		 * Make sure we can at least reference the 3 initial entries
2671 		 * (4-byte words) of the note information block.
2672 		 */
2673 		if (size >= (sizeof (Word) * 3))
2674 			size -= (sizeof (Word) * 3);
2675 		else {
2676 			(void) fprintf(stderr, MSG_INTL(MSG_NOTE_BADDATASZ),
2677 			    file, cache->c_name, EC_WORD(noteoff));
2678 			return;
2679 		}
2680 
2681 		/*
2682 		 * Make sure any specified name string can be referenced.
2683 		 */
2684 		if ((namesz = *data++) != 0) {
2685 			if (size >= namesz)
2686 				size -= namesz;
2687 			else {
2688 				(void) fprintf(stderr,
2689 				    MSG_INTL(MSG_NOTE_BADNMSZ), file,
2690 				    cache->c_name, EC_WORD(noteoff),
2691 				    EC_WORD(namesz));
2692 				return;
2693 			}
2694 		}
2695 
2696 		/*
2697 		 * Make sure any specified descriptor can be referenced.
2698 		 */
2699 		if ((descsz = *data++) != 0) {
2700 			/*
2701 			 * If namesz isn't a 4-byte multiple, account for any
2702 			 * padding that must exist before the descriptor.
2703 			 */
2704 			if ((pad = (namesz & (sizeof (Word) - 1))) != 0) {
2705 				pad = sizeof (Word) - pad;
2706 				size -= pad;
2707 			}
2708 			if (size >= descsz)
2709 				size -= descsz;
2710 			else {
2711 				(void) fprintf(stderr,
2712 				    MSG_INTL(MSG_NOTE_BADDESZ), file,
2713 				    cache->c_name, EC_WORD(noteoff),
2714 				    EC_WORD(namesz));
2715 				return;
2716 			}
2717 		}
2718 
2719 		type = *data++;
2720 
2721 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2722 		dbg_print(0, MSG_ORIG(MSG_NOTE_TYPE), EC_WORD(type));
2723 
2724 		dbg_print(0, MSG_ORIG(MSG_NOTE_NAMESZ), EC_WORD(namesz));
2725 		if (namesz) {
2726 			char	*name = (char *)data;
2727 
2728 			/*
2729 			 * Since the name string may have 'null' bytes
2730 			 * in it (ia32 .string) - we just write the
2731 			 * whole stream in a single fwrite.
2732 			 */
2733 			(void) fwrite(name, namesz, 1, stdout);
2734 			name = name + ((namesz + (sizeof (Word) - 1)) &
2735 			    ~(sizeof (Word) - 1));
2736 			/* LINTED */
2737 			data = (Word *)name;
2738 			dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2739 		}
2740 
2741 		/*
2742 		 * If multiple information blocks exist within a .note section
2743 		 * account for any padding that must exist before the next
2744 		 * information block.
2745 		 */
2746 		if ((pad = (descsz & (sizeof (Word) - 1))) != 0) {
2747 			pad = sizeof (Word) - pad;
2748 			if (size > pad)
2749 				size -= pad;
2750 		}
2751 
2752 		dbg_print(0, MSG_ORIG(MSG_NOTE_DESCSZ), EC_WORD(descsz));
2753 		if (descsz) {
2754 			int		ndx, byte, word;
2755 			char		string[58], *str = string;
2756 			uchar_t		*desc = (uchar_t *)data;
2757 
2758 			/*
2759 			 * Dump descriptor bytes.
2760 			 */
2761 			for (ndx = byte = word = 0; descsz; descsz--, desc++) {
2762 				int	tok = *desc;
2763 
2764 				(void) snprintf(str, 58, MSG_ORIG(MSG_NOTE_TOK),
2765 				    tok);
2766 				str += 3;
2767 
2768 				if (++byte == 4) {
2769 					*str++ = ' ', *str++ = ' ';
2770 					word++;
2771 					byte = 0;
2772 				}
2773 				if (word == 4) {
2774 					*str = '\0';
2775 					dbg_print(0, MSG_ORIG(MSG_NOTE_DESC),
2776 					    ndx, string);
2777 					word = 0;
2778 					ndx += 16;
2779 					str = string;
2780 				}
2781 			}
2782 			if (byte || word) {
2783 				*str = '\0';
2784 				dbg_print(0, MSG_ORIG(MSG_NOTE_DESC),
2785 				    ndx, string);
2786 			}
2787 
2788 			desc += pad;
2789 			/* LINTED */
2790 			data = (Word *)desc;
2791 		}
2792 	}
2793 }
2794 
2795 /*
2796  * Search for and process a .note section.
2797  */
2798 static void
2799 note(Cache *cache, Word shnum, const char *file)
2800 {
2801 	Word	cnt;
2802 
2803 	/*
2804 	 * Otherwise look for any .note sections.
2805 	 */
2806 	for (cnt = 1; cnt < shnum; cnt++) {
2807 		Cache	*_cache = &cache[cnt];
2808 		Shdr	*shdr = _cache->c_shdr;
2809 
2810 		if (shdr->sh_type != SHT_NOTE)
2811 			continue;
2812 		if (!match(MATCH_F_ALL, _cache->c_name, cnt, shdr->sh_type))
2813 			continue;
2814 
2815 		/*
2816 		 * As these sections are often hand rolled, make sure they're
2817 		 * properly aligned before proceeding, and issue an error
2818 		 * as necessary.
2819 		 *
2820 		 * Note that we will continue on to display the note even
2821 		 * if it has bad alignment. We can do this safely, because
2822 		 * libelf knows the alignment required for SHT_NOTE, and
2823 		 * takes steps to deliver a properly aligned buffer to us
2824 		 * even if the actual file is misaligned.
2825 		 */
2826 		if (shdr->sh_offset & (sizeof (Word) - 1))
2827 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADALIGN),
2828 			    file, _cache->c_name);
2829 
2830 		if (_cache->c_data == NULL)
2831 			continue;
2832 
2833 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2834 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_NOTE), _cache->c_name);
2835 		note_entry(_cache, (Word *)_cache->c_data->d_buf,
2836 		/* LINTED */
2837 		    (Word)_cache->c_data->d_size, file);
2838 	}
2839 }
2840 
2841 /*
2842  * Determine an individual hash entry.  This may be the initial hash entry,
2843  * or an associated chain entry.
2844  */
2845 static void
2846 hash_entry(Cache *refsec, Cache *strsec, const char *hsecname, Word hashndx,
2847     Word symndx, Word symn, Sym *syms, const char *file, ulong_t bkts,
2848     uint_t flags, int chain)
2849 {
2850 	Sym		*sym;
2851 	const char	*symname, *str;
2852 	char		_bucket[MAXNDXSIZE], _symndx[MAXNDXSIZE];
2853 	ulong_t		nbkt, nhash;
2854 
2855 	if (symndx > symn) {
2856 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_HSBADSYMNDX), file,
2857 		    EC_WORD(symndx), EC_WORD(hashndx));
2858 		symname = MSG_INTL(MSG_STR_UNKNOWN);
2859 	} else {
2860 		sym = (Sym *)(syms + symndx);
2861 		symname = string(refsec, symndx, strsec, file, sym->st_name);
2862 	}
2863 
2864 	if (chain == 0) {
2865 		(void) snprintf(_bucket, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INTEGER),
2866 		    hashndx);
2867 		str = (const char *)_bucket;
2868 	} else
2869 		str = MSG_ORIG(MSG_STR_EMPTY);
2870 
2871 	(void) snprintf(_symndx, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INDEX2),
2872 	    EC_WORD(symndx));
2873 	dbg_print(0, MSG_ORIG(MSG_FMT_HASH_INFO), str, _symndx,
2874 	    demangle(symname, flags));
2875 
2876 	/*
2877 	 * Determine if this string is in the correct bucket.
2878 	 */
2879 	nhash = elf_hash(symname);
2880 	nbkt = nhash % bkts;
2881 
2882 	if (nbkt != hashndx) {
2883 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADHASH), file,
2884 		    hsecname, symname, EC_WORD(hashndx), nbkt);
2885 	}
2886 }
2887 
2888 #define	MAXCOUNT	500
2889 
2890 static void
2891 hash(Cache *cache, Word shnum, const char *file, uint_t flags)
2892 {
2893 	static int	count[MAXCOUNT];
2894 	Word		cnt;
2895 	ulong_t		ndx, bkts;
2896 	char		number[MAXNDXSIZE];
2897 
2898 	for (cnt = 1; cnt < shnum; cnt++) {
2899 		uint_t		*hash, *chain;
2900 		Cache		*_cache = &cache[cnt];
2901 		Shdr		*sshdr, *hshdr = _cache->c_shdr;
2902 		char		*ssecname, *hsecname = _cache->c_name;
2903 		Sym		*syms;
2904 		Word		symn;
2905 
2906 		if (hshdr->sh_type != SHT_HASH)
2907 			continue;
2908 
2909 		/*
2910 		 * Determine the hash table data and size.
2911 		 */
2912 		if ((hshdr->sh_entsize == 0) || (hshdr->sh_size == 0)) {
2913 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
2914 			    file, hsecname);
2915 			continue;
2916 		}
2917 		if (_cache->c_data == NULL)
2918 			continue;
2919 
2920 		hash = (uint_t *)_cache->c_data->d_buf;
2921 		bkts = *hash;
2922 		chain = hash + 2 + bkts;
2923 		hash += 2;
2924 
2925 		/*
2926 		 * Get the data buffer for the associated symbol table.
2927 		 */
2928 		if ((hshdr->sh_link == 0) || (hshdr->sh_link >= shnum)) {
2929 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
2930 			    file, hsecname, EC_WORD(hshdr->sh_link));
2931 			continue;
2932 		}
2933 
2934 		_cache = &cache[hshdr->sh_link];
2935 		ssecname = _cache->c_name;
2936 
2937 		if (_cache->c_data == NULL)
2938 			continue;
2939 
2940 		if ((syms = (Sym *)_cache->c_data->d_buf) == NULL) {
2941 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
2942 			    file, ssecname);
2943 			continue;
2944 		}
2945 
2946 		sshdr = _cache->c_shdr;
2947 		/* LINTED */
2948 		symn = (Word)(sshdr->sh_size / sshdr->sh_entsize);
2949 
2950 		/*
2951 		 * Get the associated string table section.
2952 		 */
2953 		if ((sshdr->sh_link == 0) || (sshdr->sh_link >= shnum)) {
2954 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
2955 			    file, ssecname, EC_WORD(sshdr->sh_link));
2956 			continue;
2957 		}
2958 
2959 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2960 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_HASH), hsecname);
2961 		dbg_print(0, MSG_INTL(MSG_ELF_HASH_INFO));
2962 
2963 		/*
2964 		 * Loop through the hash buckets, printing the appropriate
2965 		 * symbols.
2966 		 */
2967 		for (ndx = 0; ndx < bkts; ndx++, hash++) {
2968 			Word	_ndx, _cnt;
2969 
2970 			if (*hash == 0) {
2971 				count[0]++;
2972 				continue;
2973 			}
2974 
2975 			hash_entry(_cache, &cache[sshdr->sh_link], hsecname,
2976 			    ndx, *hash, symn, syms, file, bkts, flags, 0);
2977 
2978 			/*
2979 			 * Determine if any other symbols are chained to this
2980 			 * bucket.
2981 			 */
2982 			_ndx = chain[*hash];
2983 			_cnt = 1;
2984 			while (_ndx) {
2985 				hash_entry(_cache, &cache[sshdr->sh_link],
2986 				    hsecname, ndx, _ndx, symn, syms, file,
2987 				    bkts, flags, 1);
2988 				_ndx = chain[_ndx];
2989 				_cnt++;
2990 			}
2991 
2992 			if (_cnt >= MAXCOUNT) {
2993 				(void) fprintf(stderr,
2994 				    MSG_INTL(MSG_HASH_OVERFLW), file,
2995 				    _cache->c_name, EC_WORD(ndx),
2996 				    EC_WORD(_cnt));
2997 			} else
2998 				count[_cnt]++;
2999 		}
3000 		break;
3001 	}
3002 
3003 	/*
3004 	 * Print out the count information.
3005 	 */
3006 	bkts = cnt = 0;
3007 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
3008 
3009 	for (ndx = 0; ndx < MAXCOUNT; ndx++) {
3010 		Word	_cnt;
3011 
3012 		if ((_cnt = count[ndx]) == 0)
3013 			continue;
3014 
3015 		(void) snprintf(number, MAXNDXSIZE,
3016 		    MSG_ORIG(MSG_FMT_INTEGER), _cnt);
3017 		dbg_print(0, MSG_INTL(MSG_ELF_HASH_BKTS1), number,
3018 		    EC_WORD(ndx));
3019 		bkts += _cnt;
3020 		cnt += (Word)(ndx * _cnt);
3021 	}
3022 	if (cnt) {
3023 		(void) snprintf(number, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INTEGER),
3024 		    bkts);
3025 		dbg_print(0, MSG_INTL(MSG_ELF_HASH_BKTS2), number,
3026 		    EC_WORD(cnt));
3027 	}
3028 }
3029 
3030 static void
3031 group(Cache *cache, Word shnum, const char *file, uint_t flags)
3032 {
3033 	Word	scnt;
3034 
3035 	for (scnt = 1; scnt < shnum; scnt++) {
3036 		Cache	*_cache = &cache[scnt];
3037 		Shdr	*shdr = _cache->c_shdr;
3038 		Word	*grpdata, gcnt, grpcnt, symnum, unknown;
3039 		Cache	*symsec, *strsec;
3040 		Sym	*syms, *sym;
3041 		char	flgstrbuf[MSG_GRP_COMDAT_SIZE + 10];
3042 
3043 		if (shdr->sh_type != SHT_GROUP)
3044 			continue;
3045 		if (!match(MATCH_F_ALL, _cache->c_name, scnt, shdr->sh_type))
3046 			continue;
3047 		if ((_cache->c_data == NULL) ||
3048 		    ((grpdata = (Word *)_cache->c_data->d_buf) == NULL))
3049 			continue;
3050 		grpcnt = shdr->sh_size / sizeof (Word);
3051 
3052 		/*
3053 		 * Get the data buffer for the associated symbol table and
3054 		 * string table.
3055 		 */
3056 		if (stringtbl(cache, 1, scnt, shnum, file,
3057 		    &symnum, &symsec, &strsec) == 0)
3058 			return;
3059 
3060 		syms = symsec->c_data->d_buf;
3061 
3062 		dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
3063 		dbg_print(0, MSG_INTL(MSG_ELF_SCN_GRP), _cache->c_name);
3064 		dbg_print(0, MSG_INTL(MSG_GRP_TITLE));
3065 
3066 		/*
3067 		 * The first element of the group defines the group.  The
3068 		 * associated symbol is defined by the sh_link field.
3069 		 */
3070 		if ((shdr->sh_info == SHN_UNDEF) || (shdr->sh_info > symnum)) {
3071 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHINFO),
3072 			    file, _cache->c_name, EC_WORD(shdr->sh_info));
3073 			return;
3074 		}
3075 
3076 		(void) strcpy(flgstrbuf, MSG_ORIG(MSG_STR_OSQBRKT));
3077 		if (grpdata[0] & GRP_COMDAT) {
3078 			(void) strcat(flgstrbuf, MSG_ORIG(MSG_GRP_COMDAT));
3079 		}
3080 		if ((unknown = (grpdata[0] & ~GRP_COMDAT)) != 0) {
3081 			size_t	len = strlen(flgstrbuf);
3082 
3083 			(void) snprintf(&flgstrbuf[len],
3084 			    (MSG_GRP_COMDAT_SIZE + 10 - len),
3085 			    MSG_ORIG(MSG_GRP_UNKNOWN), unknown);
3086 		}
3087 		(void) strcat(flgstrbuf, MSG_ORIG(MSG_STR_CSQBRKT));
3088 		sym = (Sym *)(syms + shdr->sh_info);
3089 
3090 		dbg_print(0, MSG_INTL(MSG_GRP_SIGNATURE), flgstrbuf,
3091 		    demangle(string(_cache, 0, strsec, file, sym->st_name),
3092 		    flags));
3093 
3094 		for (gcnt = 1; gcnt < grpcnt; gcnt++) {
3095 			char		index[MAXNDXSIZE];
3096 			const char	*name;
3097 
3098 			(void) snprintf(index, MAXNDXSIZE,
3099 			    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(gcnt));
3100 
3101 			if (grpdata[gcnt] >= shnum)
3102 				name = MSG_INTL(MSG_GRP_INVALSCN);
3103 			else
3104 				name = cache[grpdata[gcnt]].c_name;
3105 
3106 			(void) printf(MSG_ORIG(MSG_GRP_ENTRY), index, name,
3107 			    EC_XWORD(grpdata[gcnt]));
3108 		}
3109 	}
3110 }
3111 
3112 static void
3113 got(Cache *cache, Word shnum, Ehdr *ehdr, const char *file, uint_t flags)
3114 {
3115 	Cache		*gotcache = NULL, *symtab = NULL;
3116 	Addr		gotbgn, gotend;
3117 	Shdr		*gotshdr;
3118 	Word		cnt, gotents, gotndx;
3119 	size_t		gentsize;
3120 	Got_info	*gottable;
3121 	char		*gotdata;
3122 	Sym		*gotsym;
3123 	Xword		gotsymaddr;
3124 	uint_t		sys_encoding;
3125 
3126 	/*
3127 	 * First, find the got.
3128 	 */
3129 	for (cnt = 1; cnt < shnum; cnt++) {
3130 		if (strncmp(cache[cnt].c_name, MSG_ORIG(MSG_ELF_GOT),
3131 		    MSG_ELF_GOT_SIZE) == 0) {
3132 			gotcache = &cache[cnt];
3133 			break;
3134 		}
3135 	}
3136 	if (gotcache == NULL)
3137 		return;
3138 
3139 	/*
3140 	 * A got section within a relocatable object is suspicious.
3141 	 */
3142 	if (ehdr->e_type == ET_REL) {
3143 		(void) fprintf(stderr, MSG_INTL(MSG_GOT_UNEXPECTED), file,
3144 		    gotcache->c_name);
3145 	}
3146 
3147 	gotshdr = gotcache->c_shdr;
3148 	if (gotshdr->sh_size == 0) {
3149 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
3150 		    file, gotcache->c_name);
3151 		return;
3152 	}
3153 
3154 	gotbgn = gotshdr->sh_addr;
3155 	gotend = gotbgn + gotshdr->sh_size;
3156 
3157 	/*
3158 	 * Some architectures don't properly set the sh_entsize for the GOT
3159 	 * table.  If it's not set, default to a size of a pointer.
3160 	 */
3161 	if ((gentsize = gotshdr->sh_entsize) == 0)
3162 		gentsize = sizeof (Xword);
3163 
3164 	if (gotcache->c_data == NULL)
3165 		return;
3166 
3167 	/* LINTED */
3168 	gotents = (Word)(gotshdr->sh_size / gentsize);
3169 	gotdata = gotcache->c_data->d_buf;
3170 
3171 	if ((gottable = calloc(gotents, sizeof (Got_info))) == 0) {
3172 		int err = errno;
3173 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC), file,
3174 		    strerror(err));
3175 		return;
3176 	}
3177 
3178 	/*
3179 	 * Now we scan through all the sections looking for any relocations
3180 	 * that may be against the GOT.  Since these may not be isolated to a
3181 	 * .rel[a].got section we check them all.
3182 	 * While scanning sections save the symbol table entry (a symtab
3183 	 * overriding a dynsym) so that we can lookup _GLOBAL_OFFSET_TABLE_.
3184 	 */
3185 	for (cnt = 1; cnt < shnum; cnt++) {
3186 		Word		type, symnum;
3187 		Xword		relndx, relnum, relsize;
3188 		void		*rels;
3189 		Sym		*syms;
3190 		Cache		*symsec, *strsec;
3191 		Cache		*_cache = &cache[cnt];
3192 		Shdr		*shdr;
3193 
3194 		shdr = _cache->c_shdr;
3195 		type = shdr->sh_type;
3196 
3197 		if ((symtab == 0) && (type == SHT_DYNSYM)) {
3198 			symtab = _cache;
3199 			continue;
3200 		}
3201 		if (type == SHT_SYMTAB) {
3202 			symtab = _cache;
3203 			continue;
3204 		}
3205 		if ((type != SHT_RELA) && (type != SHT_REL))
3206 			continue;
3207 
3208 		/*
3209 		 * Decide entry size.
3210 		 */
3211 		if (((relsize = shdr->sh_entsize) == 0) ||
3212 		    (relsize > shdr->sh_size)) {
3213 			if (type == SHT_RELA)
3214 				relsize = sizeof (Rela);
3215 			else
3216 				relsize = sizeof (Rel);
3217 		}
3218 
3219 		/*
3220 		 * Determine the number of relocations available.
3221 		 */
3222 		if (shdr->sh_size == 0) {
3223 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
3224 			    file, _cache->c_name);
3225 			continue;
3226 		}
3227 		if (_cache->c_data == NULL)
3228 			continue;
3229 
3230 		rels = _cache->c_data->d_buf;
3231 		relnum = shdr->sh_size / relsize;
3232 
3233 		/*
3234 		 * Get the data buffer for the associated symbol table and
3235 		 * string table.
3236 		 */
3237 		if (stringtbl(cache, 1, cnt, shnum, file,
3238 		    &symnum, &symsec, &strsec) == 0)
3239 			continue;
3240 
3241 		syms = symsec->c_data->d_buf;
3242 
3243 		/*
3244 		 * Loop through the relocation entries.
3245 		 */
3246 		for (relndx = 0; relndx < relnum; relndx++,
3247 		    rels = (void *)((char *)rels + relsize)) {
3248 			char		section[BUFSIZ];
3249 			Addr		offset;
3250 			Got_info	*gip;
3251 			Word		symndx, reltype;
3252 			Rela		*rela;
3253 			Rel		*rel;
3254 
3255 			/*
3256 			 * Unravel the relocation.
3257 			 */
3258 			if (type == SHT_RELA) {
3259 				rela = (Rela *)rels;
3260 				symndx = ELF_R_SYM(rela->r_info);
3261 				reltype = ELF_R_TYPE(rela->r_info,
3262 				    ehdr->e_machine);
3263 				offset = rela->r_offset;
3264 			} else {
3265 				rel = (Rel *)rels;
3266 				symndx = ELF_R_SYM(rel->r_info);
3267 				reltype = ELF_R_TYPE(rel->r_info,
3268 				    ehdr->e_machine);
3269 				offset = rel->r_offset;
3270 			}
3271 
3272 			/*
3273 			 * Only pay attention to relocations against the GOT.
3274 			 */
3275 			if ((offset < gotbgn) || (offset >= gotend))
3276 				continue;
3277 
3278 			/* LINTED */
3279 			gotndx = (Word)((offset - gotbgn) /
3280 			    gotshdr->sh_entsize);
3281 			gip = &gottable[gotndx];
3282 
3283 			if (gip->g_reltype != 0) {
3284 				(void) fprintf(stderr,
3285 				    MSG_INTL(MSG_GOT_MULTIPLE), file,
3286 				    EC_WORD(gotndx), EC_ADDR(offset));
3287 				continue;
3288 			}
3289 
3290 			if (symndx)
3291 				gip->g_symname = relsymname(cache, _cache,
3292 				    strsec, symndx, symnum, relndx, syms,
3293 				    section, BUFSIZ, file, flags);
3294 			gip->g_reltype = reltype;
3295 			gip->g_rel = rels;
3296 		}
3297 	}
3298 
3299 	if (symlookup(MSG_ORIG(MSG_GOT_SYM), cache, shnum, &gotsym, symtab,
3300 	    file))
3301 		gotsymaddr = gotsym->st_value;
3302 	else
3303 		gotsymaddr = gotbgn;
3304 
3305 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
3306 	dbg_print(0, MSG_INTL(MSG_ELF_SCN_GOT), gotcache->c_name);
3307 	Elf_got_title(0);
3308 
3309 	sys_encoding = _elf_sys_encoding();
3310 	for (gotndx = 0; gotndx < gotents; gotndx++) {
3311 		Got_info	*gip;
3312 		Sword		gindex;
3313 		Addr		gaddr;
3314 		Xword		gotentry;
3315 
3316 		gip = &gottable[gotndx];
3317 
3318 		gaddr = gotbgn + (gotndx * gentsize);
3319 		gindex = (Sword)(gaddr - gotsymaddr) / (Sword)gentsize;
3320 
3321 		if (gentsize == sizeof (Word))
3322 			/* LINTED */
3323 			gotentry = (Xword)(*((Word *)(gotdata) + gotndx));
3324 		else
3325 			/* LINTED */
3326 			gotentry = *((Xword *)(gotdata) + gotndx);
3327 
3328 		Elf_got_entry(0, gindex, gaddr, gotentry, ehdr->e_machine,
3329 		    ehdr->e_ident[EI_DATA], sys_encoding,
3330 		    gip->g_reltype, gip->g_rel, gip->g_symname);
3331 	}
3332 	free(gottable);
3333 }
3334 
3335 void
3336 checksum(Elf *elf)
3337 {
3338 	dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
3339 	dbg_print(0, MSG_INTL(MSG_STR_CHECKSUM), elf_checksum(elf));
3340 }
3341 
3342 /*
3343  * This variable is used by regular() to communicate the address of
3344  * the section header cache to sort_shdr_ndx_arr(). Unfortunately,
3345  * the qsort() interface does not include a userdata argument by which
3346  * such arbitrary data can be passed, so we are stuck using global data.
3347  */
3348 static Cache *sort_shdr_ndx_arr_cache;
3349 
3350 
3351 /*
3352  * Used with qsort() to sort the section indices so that they can be
3353  * used to access the section headers in order of increasing data offset.
3354  *
3355  * entry:
3356  *	sort_shdr_ndx_arr_cache - Contains address of
3357  *		section header cache.
3358  *	v1, v2 - Point at elements of sort_shdr_bits array to be compared.
3359  *
3360  * exit:
3361  *	Returns -1 (less than), 0 (equal) or 1 (greater than).
3362  */
3363 static int
3364 sort_shdr_ndx_arr(const void *v1, const void *v2)
3365 {
3366 	Cache	*cache1 = sort_shdr_ndx_arr_cache + *((size_t *)v1);
3367 	Cache	*cache2 = sort_shdr_ndx_arr_cache + *((size_t *)v2);
3368 
3369 	if (cache1->c_shdr->sh_offset < cache2->c_shdr->sh_offset)
3370 		return (-1);
3371 
3372 	if (cache1->c_shdr->sh_offset > cache2->c_shdr->sh_offset)
3373 		return (1);
3374 
3375 	return (0);
3376 }
3377 
3378 
3379 static int
3380 shdr_cache(const char *file, Elf *elf, Ehdr *ehdr, size_t shstrndx,
3381     size_t shnum, Cache **cache_ret)
3382 {
3383 	Elf_Scn		*scn;
3384 	Elf_Data	*data;
3385 	size_t		ndx;
3386 	Shdr		*nameshdr;
3387 	char		*names = 0;
3388 	Cache		*cache, *_cache;
3389 	size_t		*shdr_ndx_arr, shdr_ndx_arr_cnt;
3390 
3391 
3392 	/*
3393 	 * Obtain the .shstrtab data buffer to provide the required section
3394 	 * name strings.
3395 	 */
3396 	if (shstrndx == SHN_UNDEF) {
3397 		/*
3398 		 * It is rare, but legal, for an object to lack a
3399 		 * header string table section.
3400 		 */
3401 		names = NULL;
3402 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_NOSHSTRSEC), file);
3403 	} else if ((scn = elf_getscn(elf, shstrndx)) == NULL) {
3404 		failure(file, MSG_ORIG(MSG_ELF_GETSCN));
3405 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SHDR),
3406 		    EC_XWORD(shstrndx));
3407 
3408 	} else if ((data = elf_getdata(scn, NULL)) == NULL) {
3409 		failure(file, MSG_ORIG(MSG_ELF_GETDATA));
3410 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_DATA),
3411 		    EC_XWORD(shstrndx));
3412 
3413 	} else if ((nameshdr = elf_getshdr(scn)) == NULL) {
3414 		failure(file, MSG_ORIG(MSG_ELF_GETSHDR));
3415 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN),
3416 		    EC_WORD(elf_ndxscn(scn)));
3417 
3418 	} else if ((names = data->d_buf) == 0)
3419 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_SHSTRNULL), file);
3420 
3421 	/*
3422 	 * Allocate a cache to maintain a descriptor for each section.
3423 	 */
3424 	if ((*cache_ret = cache = malloc(shnum * sizeof (Cache))) == NULL) {
3425 		int err = errno;
3426 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
3427 		    file, strerror(err));
3428 		return (0);
3429 	}
3430 
3431 	*cache = cache_init;
3432 	_cache = cache;
3433 	_cache++;
3434 
3435 	/*
3436 	 * Allocate an array that will hold the section index for
3437 	 * each section that has data in the ELF file:
3438 	 *
3439 	 *	- Is not a NOBITS section
3440 	 *	- Data has non-zero length
3441 	 *
3442 	 * Note that shnum is an upper bound on the size required. It
3443 	 * is likely that we won't use a few of these array elements.
3444 	 * Allocating a modest amount of extra memory in this case means
3445 	 * that we can avoid an extra loop to count the number of needed
3446 	 * items, and can fill this array immediately in the first loop
3447 	 * below.
3448 	 */
3449 	if ((shdr_ndx_arr = malloc(shnum * sizeof (*shdr_ndx_arr))) == NULL) {
3450 		int err = errno;
3451 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
3452 		    file, strerror(err));
3453 		return (0);
3454 	}
3455 	shdr_ndx_arr_cnt = 0;
3456 
3457 	/*
3458 	 * Traverse the sections of the file.  This gathering of data is
3459 	 * carried out in two passes.  First, the section headers are captured
3460 	 * and the section header names are evaluated.  A verification pass is
3461 	 * then carried out over the section information.  Files have been
3462 	 * known to exhibit overlapping (and hence erroneous) section header
3463 	 * information.
3464 	 *
3465 	 * Finally, the data for each section is obtained.  This processing is
3466 	 * carried out after section verification because should any section
3467 	 * header overlap occur, and a file needs translating (ie. xlate'ing
3468 	 * information from a non-native architecture file), then the process
3469 	 * of translation can corrupt the section header information.  Of
3470 	 * course, if there is any section overlap, the data related to the
3471 	 * sections is going to be compromised.  However, it is the translation
3472 	 * of this data that has caused problems with elfdump()'s ability to
3473 	 * extract the data.
3474 	 */
3475 	for (ndx = 1, scn = NULL; scn = elf_nextscn(elf, scn);
3476 	    ndx++, _cache++) {
3477 		char	scnndxnm[100];
3478 
3479 		_cache->c_ndx = ndx;
3480 		_cache->c_scn = scn;
3481 
3482 		if ((_cache->c_shdr = elf_getshdr(scn)) == NULL) {
3483 			failure(file, MSG_ORIG(MSG_ELF_GETSHDR));
3484 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN),
3485 			    EC_WORD(elf_ndxscn(scn)));
3486 		}
3487 
3488 		/*
3489 		 * If this section has data in the file, include it in
3490 		 * the array of sections to check for address overlap.
3491 		 */
3492 		if ((_cache->c_shdr->sh_size != 0) &&
3493 		    (_cache->c_shdr->sh_type != SHT_NOBITS))
3494 			shdr_ndx_arr[shdr_ndx_arr_cnt++] = ndx;
3495 
3496 		/*
3497 		 * If a shstrtab exists, assign the section name.
3498 		 */
3499 		if (names && _cache->c_shdr) {
3500 			if (_cache->c_shdr->sh_name &&
3501 			    /* LINTED */
3502 			    (nameshdr->sh_size > _cache->c_shdr->sh_name)) {
3503 				_cache->c_name =
3504 				    names + _cache->c_shdr->sh_name;
3505 				continue;
3506 			}
3507 
3508 			/*
3509 			 * Generate an error if the section name index is zero
3510 			 * or exceeds the shstrtab data.  Fall through to
3511 			 * fabricate a section name.
3512 			 */
3513 			if ((_cache->c_shdr->sh_name == 0) ||
3514 			    /* LINTED */
3515 			    (nameshdr->sh_size <= _cache->c_shdr->sh_name)) {
3516 				(void) fprintf(stderr,
3517 				    MSG_INTL(MSG_ERR_BADSHNAME), file,
3518 				    EC_WORD(ndx),
3519 				    EC_XWORD(_cache->c_shdr->sh_name));
3520 			}
3521 		}
3522 
3523 		/*
3524 		 * If there exists no shstrtab data, or a section header has no
3525 		 * name (an invalid index of 0), then compose a name for the
3526 		 * section.
3527 		 */
3528 		(void) snprintf(scnndxnm, sizeof (scnndxnm),
3529 		    MSG_INTL(MSG_FMT_SCNNDX), ndx);
3530 
3531 		if ((_cache->c_name = malloc(strlen(scnndxnm) + 1)) == NULL) {
3532 			int err = errno;
3533 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
3534 			    file, strerror(err));
3535 			return (0);
3536 		}
3537 		(void) strcpy(_cache->c_name, scnndxnm);
3538 	}
3539 
3540 	/*
3541 	 * Having collected all the sections, validate their address range.
3542 	 * Cases have existed where the section information has been invalid.
3543 	 * This can lead to all sorts of other, hard to diagnose errors, as
3544 	 * each section is processed individually (ie. with elf_getdata()).
3545 	 * Here, we carry out some address comparisons to catch a family of
3546 	 * overlapping memory issues we have observed (likely, there are others
3547 	 * that we have yet to discover).
3548 	 *
3549 	 * Note, should any memory overlap occur, obtaining any additional
3550 	 * data from the file is questionable.  However, it might still be
3551 	 * possible to inspect the ELF header, Programs headers, or individual
3552 	 * sections, so rather than bailing on an error condition, continue
3553 	 * processing to see if any data can be salvaged.
3554 	 */
3555 	if (shdr_ndx_arr_cnt > 1) {
3556 		sort_shdr_ndx_arr_cache = cache;
3557 		qsort(shdr_ndx_arr, shdr_ndx_arr_cnt,
3558 		    sizeof (*shdr_ndx_arr), sort_shdr_ndx_arr);
3559 	}
3560 	for (ndx = 0; ndx < shdr_ndx_arr_cnt; ndx++) {
3561 		Cache	*_cache = cache + shdr_ndx_arr[ndx];
3562 		Shdr	*shdr = _cache->c_shdr;
3563 		Off	bgn1, bgn = shdr->sh_offset;
3564 		Off	end1, end = shdr->sh_offset + shdr->sh_size;
3565 		size_t	ndx1;
3566 
3567 		/*
3568 		 * Check the section against all following ones, reporting
3569 		 * any overlaps. Since we've sorted the sections by offset,
3570 		 * we can stop after the first comparison that fails. There
3571 		 * are no overlaps in a properly formed ELF file, in which
3572 		 * case this algorithm runs in O(n) time. This will degenerate
3573 		 * to O(n^2) for a completely broken file. Such a file is
3574 		 * (1) highly unlikely, and (2) unusable, so it is reasonable
3575 		 * for the analysis to take longer.
3576 		 */
3577 		for (ndx1 = ndx + 1; ndx1 < shdr_ndx_arr_cnt; ndx1++) {
3578 			Cache	*_cache1 = cache + shdr_ndx_arr[ndx1];
3579 			Shdr	*shdr1 = _cache1->c_shdr;
3580 
3581 			bgn1 = shdr1->sh_offset;
3582 			end1 = shdr1->sh_offset + shdr1->sh_size;
3583 
3584 			if (((bgn1 <= bgn) && (end1 > bgn)) ||
3585 			    ((bgn1 < end) && (end1 >= end))) {
3586 				(void) fprintf(stderr,
3587 				    MSG_INTL(MSG_ERR_SECMEMOVER), file,
3588 				    EC_WORD(elf_ndxscn(_cache->c_scn)),
3589 				    _cache->c_name, EC_OFF(bgn), EC_OFF(end),
3590 				    EC_WORD(elf_ndxscn(_cache1->c_scn)),
3591 				    _cache1->c_name, EC_OFF(bgn1),
3592 				    EC_OFF(end1));
3593 			} else {	/* No overlap, so can stop */
3594 				break;
3595 			}
3596 		}
3597 
3598 		/*
3599 		 * In addition to checking for sections overlapping
3600 		 * each other (done above), we should also make sure
3601 		 * the section doesn't overlap the section header array.
3602 		 */
3603 		bgn1 = ehdr->e_shoff;
3604 		end1 = ehdr->e_shoff + (ehdr->e_shentsize * ehdr->e_shnum);
3605 
3606 		if (((bgn1 <= bgn) && (end1 > bgn)) ||
3607 		    ((bgn1 < end) && (end1 >= end))) {
3608 			(void) fprintf(stderr,
3609 			    MSG_INTL(MSG_ERR_SHDRMEMOVER), file, EC_OFF(bgn1),
3610 			    EC_OFF(end1),
3611 			    EC_WORD(elf_ndxscn(_cache->c_scn)),
3612 			    _cache->c_name, EC_OFF(bgn), EC_OFF(end));
3613 		}
3614 	}
3615 
3616 	/*
3617 	 * Obtain the data for each section.
3618 	 */
3619 	for (ndx = 1; ndx < shnum; ndx++) {
3620 		Cache	*_cache = &cache[ndx];
3621 		Elf_Scn	*scn = _cache->c_scn;
3622 
3623 		if ((_cache->c_data = elf_getdata(scn, NULL)) == NULL) {
3624 			failure(file, MSG_ORIG(MSG_ELF_GETDATA));
3625 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCNDATA),
3626 			    EC_WORD(elf_ndxscn(scn)));
3627 		}
3628 	}
3629 
3630 	return (1);
3631 }
3632 
3633 
3634 
3635 int
3636 regular(const char *file, int fd, Elf *elf, uint_t flags,
3637     const char *wname, int wfd)
3638 {
3639 	Elf_Scn		*scn;
3640 	Ehdr		*ehdr;
3641 	size_t		ndx, shstrndx, shnum, phnum;
3642 	Shdr		*shdr;
3643 	Cache		*cache;
3644 	VERSYM_STATE	versym;
3645 	int		ret = 0;
3646 	int		addr_align;
3647 
3648 	if ((ehdr = elf_getehdr(elf)) == NULL) {
3649 		failure(file, MSG_ORIG(MSG_ELF_GETEHDR));
3650 		return (ret);
3651 	}
3652 
3653 	if (elf_getshnum(elf, &shnum) == 0) {
3654 		failure(file, MSG_ORIG(MSG_ELF_GETSHNUM));
3655 		return (ret);
3656 	}
3657 
3658 	if (elf_getshstrndx(elf, &shstrndx) == 0) {
3659 		failure(file, MSG_ORIG(MSG_ELF_GETSHSTRNDX));
3660 		return (ret);
3661 	}
3662 
3663 	if (elf_getphnum(elf, &phnum) == 0) {
3664 		failure(file, MSG_ORIG(MSG_ELF_GETPHNUM));
3665 		return (ret);
3666 	}
3667 	/*
3668 	 * If the user requested section headers derived from the
3669 	 * program headers (-P option) and this file doesn't have
3670 	 * any program headers (i.e. ET_REL), then we can't do it.
3671 	 */
3672 	if ((phnum == 0) && (flags & FLG_CTL_FAKESHDR)) {
3673 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_PNEEDSPH), file);
3674 		return (ret);
3675 	}
3676 
3677 
3678 	if ((scn = elf_getscn(elf, 0)) != NULL) {
3679 		if ((shdr = elf_getshdr(scn)) == NULL) {
3680 			failure(file, MSG_ORIG(MSG_ELF_GETSHDR));
3681 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN), 0);
3682 			return (ret);
3683 		}
3684 	} else
3685 		shdr = 0;
3686 
3687 	/*
3688 	 * Print the elf header.
3689 	 */
3690 	if (flags & FLG_SHOW_EHDR)
3691 		Elf_ehdr(0, ehdr, shdr);
3692 
3693 	/*
3694 	 * If the section headers or program headers have inadequate
3695 	 * alignment for the class of object, print a warning. libelf
3696 	 * can handle such files, but programs that use them can crash
3697 	 * when they dereference unaligned items.
3698 	 *
3699 	 * Note that the AMD64 ABI, although it is a 64-bit architecture,
3700 	 * allows access to data types smaller than 128-bits to be on
3701 	 * word alignment.
3702 	 */
3703 	if (ehdr->e_machine == EM_AMD64)
3704 		addr_align = sizeof (Word);
3705 	else
3706 		addr_align = sizeof (Addr);
3707 
3708 	if (ehdr->e_phoff & (addr_align - 1))
3709 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADPHDRALIGN), file);
3710 	if (ehdr->e_shoff & (addr_align - 1))
3711 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHDRALIGN), file);
3712 
3713 	/*
3714 	 * Print the program headers.
3715 	 */
3716 	if ((flags & FLG_SHOW_PHDR) && (phnum != 0)) {
3717 		Phdr	*phdr;
3718 
3719 		if ((phdr = elf_getphdr(elf)) == NULL) {
3720 			failure(file, MSG_ORIG(MSG_ELF_GETPHDR));
3721 			return (ret);
3722 		}
3723 
3724 		for (ndx = 0; ndx < phnum; phdr++, ndx++) {
3725 			if (!match(MATCH_F_PHDR| MATCH_F_NDX | MATCH_F_TYPE,
3726 			    NULL, ndx, phdr->p_type))
3727 				continue;
3728 
3729 			dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
3730 			dbg_print(0, MSG_INTL(MSG_ELF_PHDR), EC_WORD(ndx));
3731 			Elf_phdr(0, ehdr->e_machine, phdr);
3732 		}
3733 	}
3734 
3735 	/*
3736 	 * If we have flag bits set that explicitly require a show or calc
3737 	 * operation, but none of them require the section headers, then
3738 	 * we are done and can return now.
3739 	 */
3740 	if (((flags & (FLG_MASK_SHOW | FLG_MASK_CALC)) != 0) &&
3741 	    ((flags & (FLG_MASK_SHOW_SHDR | FLG_MASK_CALC_SHDR)) == 0))
3742 		return (ret);
3743 
3744 	/*
3745 	 * If there are no section headers, then resort to synthesizing
3746 	 * section headers from the program headers. This is normally
3747 	 * only done by explicit request, but in this case there's no
3748 	 * reason not to go ahead, since the alternative is simply to quit.
3749 	 */
3750 	if ((shnum <= 1) && ((flags & FLG_CTL_FAKESHDR) == 0)) {
3751 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_NOSHDR), file);
3752 		flags |= FLG_CTL_FAKESHDR;
3753 	}
3754 
3755 	/*
3756 	 * Generate a cache of section headers and related information
3757 	 * for use by the rest of elfdump. If requested (or the file
3758 	 * contains no section headers), we generate a fake set of
3759 	 * headers from the information accessible from the program headers.
3760 	 * Otherwise, we use the real section headers contained in the file.
3761 	 */
3762 
3763 	if (flags & FLG_CTL_FAKESHDR) {
3764 		if (fake_shdr_cache(file, fd, elf, ehdr, &cache, &shnum) == 0)
3765 			return (ret);
3766 	} else {
3767 		if (shdr_cache(file, elf, ehdr, shstrndx, shnum, &cache) == 0)
3768 			return (ret);
3769 	}
3770 
3771 	/*
3772 	 * Everything from this point on requires section headers.
3773 	 * If we have no section headers, there is no reason to continue.
3774 	 */
3775 	if (shnum <= 1)
3776 		goto done;
3777 
3778 	/*
3779 	 * If -w was specified, find and write out the section(s) data.
3780 	 */
3781 	if (wfd) {
3782 		for (ndx = 1; ndx < shnum; ndx++) {
3783 			Cache	*_cache = &cache[ndx];
3784 
3785 			if (match(MATCH_F_STRICT | MATCH_F_ALL, _cache->c_name,
3786 			    ndx, _cache->c_shdr->sh_type) &&
3787 			    _cache->c_data && _cache->c_data->d_buf) {
3788 				if (write(wfd, _cache->c_data->d_buf,
3789 				    _cache->c_data->d_size) !=
3790 				    _cache->c_data->d_size) {
3791 					int err = errno;
3792 					(void) fprintf(stderr,
3793 					    MSG_INTL(MSG_ERR_WRITE), wname,
3794 					    strerror(err));
3795 					/*
3796 					 * Return an exit status of 1, because
3797 					 * the failure is not related to the
3798 					 * ELF file, but by system resources.
3799 					 */
3800 					ret = 1;
3801 					goto done;
3802 				}
3803 			}
3804 		}
3805 	}
3806 
3807 	/*
3808 	 * If we have no flag bits set that explicitly require a show or calc
3809 	 * operation, but match options (-I, -N, -T) were used, then run
3810 	 * through the section headers and see if we can't deduce show flags
3811 	 * from the match options given.
3812 	 *
3813 	 * We don't do this if -w was specified, because (-I, -N, -T) used
3814 	 * with -w in lieu of some other option is supposed to be quiet.
3815 	 */
3816 	if ((wfd == 0) && (flags & FLG_CTL_MATCH) &&
3817 	    ((flags & (FLG_MASK_SHOW | FLG_MASK_CALC)) == 0)) {
3818 		for (ndx = 1; ndx < shnum; ndx++) {
3819 			Cache	*_cache = &cache[ndx];
3820 
3821 			if (!match(MATCH_F_STRICT | MATCH_F_ALL, _cache->c_name,
3822 			    ndx, _cache->c_shdr->sh_type))
3823 				continue;
3824 
3825 			switch (_cache->c_shdr->sh_type) {
3826 			case SHT_PROGBITS:
3827 				/*
3828 				 * Heuristic time: It is usually bad form
3829 				 * to assume that specific section names
3830 				 * have a given meaning. However, the
3831 				 * ELF ABI does specify a few such names. Try
3832 				 * to match them:
3833 				 */
3834 				if (strcmp(_cache->c_name,
3835 				    MSG_ORIG(MSG_ELF_INTERP)) == 0)
3836 					flags |= FLG_SHOW_INTERP;
3837 				else if (strcmp(_cache->c_name,
3838 				    MSG_ORIG(MSG_ELF_GOT)) == 0)
3839 					flags |= FLG_SHOW_GOT;
3840 				break;
3841 
3842 			case SHT_SYMTAB:
3843 			case SHT_DYNSYM:
3844 			case SHT_SUNW_LDYNSYM:
3845 			case SHT_SUNW_versym:
3846 			case SHT_SYMTAB_SHNDX:
3847 				flags |= FLG_SHOW_SYMBOLS;
3848 				break;
3849 
3850 			case SHT_RELA:
3851 			case SHT_REL:
3852 				flags |= FLG_SHOW_RELOC;
3853 				break;
3854 
3855 			case SHT_HASH:
3856 				flags |= FLG_SHOW_HASH;
3857 				break;
3858 
3859 			case SHT_DYNAMIC:
3860 				flags |= FLG_SHOW_DYNAMIC;
3861 				break;
3862 
3863 			case SHT_NOTE:
3864 				flags |= FLG_SHOW_NOTE;
3865 				break;
3866 
3867 			case SHT_GROUP:
3868 				flags |= FLG_SHOW_GROUP;
3869 				break;
3870 
3871 			case SHT_SUNW_symsort:
3872 			case SHT_SUNW_tlssort:
3873 				flags |= FLG_SHOW_SORT;
3874 				break;
3875 
3876 			case SHT_SUNW_cap:
3877 				flags |= FLG_SHOW_CAP;
3878 				break;
3879 
3880 			case SHT_SUNW_move:
3881 				flags |= FLG_SHOW_MOVE;
3882 				break;
3883 
3884 			case SHT_SUNW_syminfo:
3885 				flags |= FLG_SHOW_SYMINFO;
3886 				break;
3887 
3888 			case SHT_SUNW_verdef:
3889 			case SHT_SUNW_verneed:
3890 				flags |= FLG_SHOW_VERSIONS;
3891 				break;
3892 
3893 			case SHT_AMD64_UNWIND:
3894 				flags |= FLG_SHOW_UNWIND;
3895 				break;
3896 			}
3897 		}
3898 	}
3899 
3900 
3901 	if (flags & FLG_SHOW_SHDR)
3902 		sections(file, cache, shnum, ehdr);
3903 
3904 	if (flags & FLG_SHOW_INTERP)
3905 		interp(file, cache, shnum, phnum, elf);
3906 
3907 	versions(cache, shnum, file, flags, &versym);
3908 
3909 	if (flags & FLG_SHOW_SYMBOLS)
3910 		symbols(cache, shnum, ehdr, &versym, file, flags);
3911 
3912 	if (flags & FLG_SHOW_SORT)
3913 		sunw_sort(cache, shnum, ehdr, &versym, file, flags);
3914 
3915 	if (flags & FLG_SHOW_HASH)
3916 		hash(cache, shnum, file, flags);
3917 
3918 	if (flags & FLG_SHOW_GOT)
3919 		got(cache, shnum, ehdr, file, flags);
3920 
3921 	if (flags & FLG_SHOW_GROUP)
3922 		group(cache, shnum, file, flags);
3923 
3924 	if (flags & FLG_SHOW_SYMINFO)
3925 		syminfo(cache, shnum, file);
3926 
3927 	if (flags & FLG_SHOW_RELOC)
3928 		reloc(cache, shnum, ehdr, file, flags);
3929 
3930 	if (flags & FLG_SHOW_DYNAMIC)
3931 		dynamic(cache, shnum, ehdr, file);
3932 
3933 	if (flags & FLG_SHOW_NOTE)
3934 		note(cache, shnum, file);
3935 
3936 	if (flags & FLG_SHOW_MOVE)
3937 		move(cache, shnum, file, flags);
3938 
3939 	if (flags & FLG_CALC_CHECKSUM)
3940 		checksum(elf);
3941 
3942 	if (flags & FLG_SHOW_CAP)
3943 		cap(file, cache, shnum, phnum, ehdr, elf);
3944 
3945 	if (flags & FLG_SHOW_UNWIND)
3946 		unwind(cache, shnum, phnum, ehdr, file, elf);
3947 
3948 
3949 	/* Release the memory used to cache section headers */
3950 done:
3951 	if (flags & FLG_CTL_FAKESHDR)
3952 		fake_shdr_cache_free(cache, shnum);
3953 	else
3954 		free(cache);
3955 
3956 	return (ret);
3957 }
3958