xref: /titanic_51/usr/src/cmd/sgs/dump/common/dump.c (revision 27c48ed935c6a5f6015c6534b98e3090b1ddfdb6)
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 (c) 1988 AT&T
24  *	  All Rights Reserved
25  *
26  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /* Get definitions for the relocation types supported. */
33 #define	ELF_TARGET_ALL
34 
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <locale.h>
38 #include <unistd.h>
39 #include <libelf.h>
40 #include <link.h>
41 #include <sys/elf.h>
42 #include <sys/machelf.h>
43 #include <fcntl.h>
44 #include <sys/stat.h>
45 #include <errno.h>
46 #include <string.h>
47 #include "sgs.h"
48 #include "conv.h"
49 #include "dump.h"
50 
51 
52 #define	OPTSTR	"agcd:fhn:oprstvCLT:V?"		/* option string for getopt() */
53 
54 /*
55  * DUMP_CONVFMT defines the libconv formatting options we want to use:
56  *	- Unknown items to be printed as integers using decimal formatting
57  *	- The "Dump Style" versions of strings.
58  */
59 #define	DUMP_CONVFMT (CONV_FMT_DECIMAL|CONV_FMT_ALTDUMP)
60 
61 const char *UNKNOWN = "<unknown>";
62 
63 static SCNTAB *p_symtab, *p_head_scns, *p_dynsym;
64 
65 static int
66 	x_flag = 0,	/* option requires section header table */
67 	z_flag = 0,	/* process files within an archive */
68 	rn_flag = 0;	/* dump named relocation information */
69 
70 static int
71 	/* flags: ?_flag corresponds to ? option */
72 	a_flag = 0,	/* dump archive header of each member of archive */
73 	g_flag = 0,	/* dump archive symbol table */
74 	c_flag = 0,	/* dump the string table */
75 	d_flag = 0,	/* dump range of sections */
76 	f_flag = 0,	/* dump each file header */
77 	h_flag = 0,	/* dump section headers */
78 	n_flag = 0,	/* dump named section */
79 	o_flag = 0,	/* dump each program execution header */
80 	r_flag = 0,	/* dump relocation information */
81 	s_flag = 0,	/* dump section contents */
82 	t_flag = 0,	/* dump symbol table entries */
83 	C_flag = 0,	/* dump decoded C++ symbol names */
84 	L_flag = 0,	/* dump dynamic linking information */
85 	T_flag = 0,	/* dump symbol table range */
86 	V_flag = 0;	/* dump version information */
87 
88 int	p_flag = 0,	/* suppress printing of headings */
89 	v_flag = 0;	/* print information in verbose form */
90 
91 static int
92 	d_low = 0,	/* range for use with -d */
93 	d_hi = 0,
94 	d_num = 0;
95 
96 static int
97 	T_low = 0,	/* range for use with -T */
98 	T_hi = 0,
99 	T_num = 0;
100 
101 static char *name = NULL; /* for use with -n option */
102 char *prog_name;
103 static int errflag = 0;
104 
105 static struct stab_list_s {
106 	struct stab_list_s *next;
107 	char *strings;
108 	size_t size;
109 } *StringTableList = (void *)0;
110 
111 extern void ar_sym_read();
112 extern void dump_exec_header();
113 
114 
115 /*
116  * Get the section descriptor and set the size of the
117  * data returned.  Data is byte-order converted.
118  */
119 void *
120 get_scndata(Elf_Scn *fd_scn, size_t *size)
121 {
122 	Elf_Data *p_data;
123 
124 	p_data = 0;
125 	if ((p_data = elf_getdata(fd_scn, p_data)) == 0 ||
126 	    p_data->d_size == 0) {
127 		return (NULL);
128 	}
129 	*size = p_data->d_size;
130 	return (p_data->d_buf);
131 }
132 
133 /*
134  * Get the section descriptor and set the size of the
135  * data returned.  Data is raw (i.e., not byte-order converted).
136  */
137 static void *
138 get_rawscn(Elf_Scn *fd_scn, size_t *size)
139 {
140 	Elf_Data *p_data;
141 
142 	p_data = 0;
143 	if ((p_data = elf_rawdata(fd_scn, p_data)) == 0 ||
144 	    p_data->d_size == 0) {
145 		return (NULL);
146 	}
147 
148 	*size = p_data->d_size;
149 	return (p_data->d_buf);
150 }
151 
152 /*
153  * Print out a usage message in short form when program is invoked
154  * with insufficient or no arguments, and in long form when given
155  * either a ? or an invalid option.
156  */
157 static void
158 usage()
159 {
160 	(void) fprintf(stderr,
161 	"Usage: %s [-%s] file(s) ...\n", prog_name, OPTSTR);
162 	if (errflag) {
163 		(void) fprintf(stderr,
164 		"\t\t[-a dump archive header of each member of archive]\n\
165 		[-g dump archive global symbol table]\n\
166 		[-c dump the string table]\n\
167 		[-d dump range of sections]\n\
168 		[-f dump each file header]\n\
169 		[-h dump section headers]\n\
170 		[-n dump named section]\n\
171 		[-o dump each program execution header]\n\
172 		[-p suppress printing of headings]\n\
173 		[-r dump relocation information]\n\
174 		[-s dump section contents]\n\
175 		[-t dump symbol table entries]\n\
176 		[-v print information in verbose form]\n\
177 		[-C dump decoded C++ symbol names]\n\
178 		[-L dump the .dynamic structure]\n\
179 		[-T dump symbol table range]\n\
180 		[-V dump version information]\n");
181 	}
182 }
183 
184 /*
185  * Set a range.  Input is a character string, a lower
186  * bound and an upper bound.  This function converts
187  * a character string into its correct integer values,
188  * setting the first value as the lower bound, and
189  * the second value as the upper bound.  If more values
190  * are given they are ignored with a warning.
191  */
192 static void
193 set_range(char *s, int  *low, int  *high)
194 {
195 	char *w;
196 	char *lasts;
197 
198 	while ((w = strtok_r(s, ",", &lasts)) != NULL) {
199 		if (!(*low))
200 			/* LINTED */
201 			*low = (int)atol(w);
202 		else
203 			if (!(*high))
204 				/* LINTED */
205 				*high = (int)atol(w);
206 			else {
207 				(void) fprintf(stderr,
208 					"%s: too many arguments - %s ignored\n",
209 					prog_name, w);
210 				return;
211 			}
212 		s = NULL;
213 	} /* end while */
214 }
215 
216 
217 /*
218  * Print static shared library information.
219  */
220 static void
221 print_static(SCNTAB *l_scns, char *filename)
222 {
223 	size_t section_size;
224 	unsigned char *strtab;
225 	unsigned char *path, buf[1024];
226 	unsigned long *temp;
227 	unsigned long total, topath;
228 
229 	(void) printf("\n  **** STATIC SHARED LIBRARY INFORMATION ****\n");
230 	(void) printf("\n%s:\n", filename);
231 	(void) printf("\t");
232 	section_size  = 0;
233 	if ((strtab = (unsigned char *)
234 	    get_scndata(l_scns->p_sd, &section_size)) == NULL) {
235 		return;
236 	}
237 
238 	while (section_size != 0) {
239 		/* LINTED */
240 		temp = (unsigned long *)strtab;
241 		total = temp[0];
242 		topath = temp[1];
243 		path = strtab + (topath*sizeof (long));
244 		(void) strncpy((char *)buf, (char *)path,
245 			(total - topath)*sizeof (long));
246 		(void) fprintf(stdout, "%s\n", buf);
247 		strtab += total*sizeof (long);
248 		section_size -= (total*sizeof (long));
249 	}
250 }
251 
252 /*
253  * Print raw data in hexidecimal.  Input is the section data to
254  * be printed out and the size of the data.  Output is relative
255  * to a table lookup in dumpmap.h.
256  */
257 static void
258 print_rawdata(unsigned char *p_sec, size_t size)
259 {
260 	size_t   j;
261 	size_t   count;
262 
263 	count = 1;
264 
265 	(void) printf("\t");
266 	for (j = size/sizeof (short); j != 0; --j, ++count) {
267 		(void) printf("%.2x %.2x ", p_sec[0], p_sec[1]);
268 		p_sec += 2;
269 		if (count == 12) {
270 			(void) printf("\n\t");
271 			count = 0;
272 		}
273 	}
274 
275 	/*
276 	 * take care of last byte if odd byte section
277 	 */
278 	if ((size & 0x1L) == 1L)
279 		(void) printf("%.2x", *p_sec);
280 	(void) printf("\n");
281 }
282 
283 
284 
285 /*
286  * Print relocation data of type SHT_RELA
287  * If d_flag, print data corresponding only to
288  * the section or range of sections specified.
289  * If n_flag, print data corresponding only to
290  * the named section.
291  */
292 static void
293 print_rela(Elf *elf_file, SCNTAB *p_scns, Elf_Data *rdata, Elf_Data *sym_data,
294 	GElf_Ehdr * p_ehdr, size_t reloc_size, size_t sym_size, char *filename,
295 	SCNTAB *reloc_symtab)
296 {
297 	GElf_Rela rela;
298 	GElf_Sym sym;
299 	size_t no_entries;
300 	size_t rel_entsize;
301 	size_t no_syms;
302 	int type, symid;
303 	static int n_title = 0;
304 	int ndx = 0;
305 	char *sym_name;
306 	int adj = 0;
307 
308 	if (gelf_getclass(elf_file) == ELFCLASS64)
309 		adj = 8;
310 
311 	rel_entsize = p_scns->p_shdr.sh_entsize;
312 	if ((rel_entsize == 0) ||
313 	    (rel_entsize > p_scns->p_shdr.sh_size)) {
314 		rel_entsize = gelf_fsize(elf_file, ELF_T_RELA, 1,
315 		    EV_CURRENT);
316 	}
317 	no_entries = reloc_size / rel_entsize;
318 
319 	no_syms = sym_size / gelf_fsize(elf_file, ELF_T_SYM, 1, EV_CURRENT);
320 	while (no_entries--) {
321 		(void) gelf_getrela(rdata, ndx, &rela);
322 		/* LINTED */
323 		type = (int)GELF_R_TYPE(rela.r_info);
324 		/* LINTED */
325 		symid = (int)GELF_R_SYM(rela.r_info);
326 		/* LINTED */
327 		if ((symid > (no_syms - 1)) || (symid < 0)) {
328 			(void) fprintf(stderr, "%s: %s: invalid symbol table "
329 			    "offset - %d - in %s\n", prog_name, filename,
330 			    symid, p_scns->scn_name);
331 			ndx++;
332 			continue;
333 		}
334 		(void) gelf_getsym(sym_data, symid, &sym);
335 		sym_name = (char *)elf_strptr(elf_file,
336 			reloc_symtab->p_shdr.sh_link, sym.st_name);
337 		if (sym_name == NULL)
338 			sym_name = (char *)UNKNOWN;
339 		if (r_flag && rn_flag) {
340 			if (strcmp(name, p_scns->scn_name) != 0) {
341 				ndx++;
342 				continue;
343 			}
344 			if (!n_title) {
345 				(void) printf("\n%s:\n", p_scns->scn_name);
346 				(void) printf("%-*s%-*s%-*s%s\n\n",
347 				    12 + adj, "Offset", 22, "Symndx",
348 				    16, "Type", "Addend");
349 				n_title = 1;
350 			}
351 		}
352 		if (d_flag) {
353 			if (!d_hi)
354 				d_hi = d_low;
355 			if ((symid < d_low) || (symid > d_hi)) {
356 				ndx++;
357 				continue;
358 			}
359 		}
360 
361 		(void) printf("%-#*llx", 12 + adj, EC_XWORD(rela.r_offset));
362 		if (!v_flag) {
363 			(void) printf("%-22d%-18d", symid, type);
364 		} else {
365 			if (strlen(sym_name)) {
366 				size_t len = strlen(sym_name) + 1;
367 				char tmpstr[10];
368 				if (len > 22) {
369 					(void) sprintf(tmpstr, "%%-%ds",
370 						/* LINTED */
371 						(int)len);
372 					/*LINTED: E_SEC_PRINTF_VAR_FMT*/
373 					(void) printf(tmpstr, sym_name);
374 				} else
375 					(void) printf("%-22s", sym_name);
376 			} else {
377 				(void) printf("%-22d", symid);
378 			}
379 			(void) printf("%-20s",
380 				conv_reloc_type(p_ehdr->e_machine,
381 				type, DUMP_CONVFMT));
382 		}
383 		(void) printf("%lld\n", EC_SXWORD(rela.r_addend));
384 		ndx++;
385 	}
386 }
387 
388 /*
389  * Print relocation data of type SHT_REL.
390  * If d_flag, print data corresponding only to
391  * the section or range of sections specified.
392  * If n_flag, print data corresponding only to
393  * the named section.
394  */
395 static void
396 print_rel(Elf *elf_file, SCNTAB *p_scns, Elf_Data *rdata, Elf_Data *sym_data,
397 	GElf_Ehdr *p_ehdr, size_t reloc_size, size_t sym_size, char *filename,
398 	SCNTAB *reloc_symtab)
399 {
400 	GElf_Rel rel;
401 	GElf_Sym sym;
402 	size_t no_entries;
403 	size_t rel_entsize;
404 	int type, symid;
405 	size_t no_syms;
406 	static int n_title = 0;
407 	int ndx = 0;
408 	char *sym_name;
409 	int adj = 0;
410 
411 	if (gelf_getclass(elf_file) == ELFCLASS64)
412 		adj = 8;
413 
414 	rel_entsize = p_scns->p_shdr.sh_entsize;
415 	if ((rel_entsize == 0) ||
416 	    (rel_entsize > p_scns->p_shdr.sh_size)) {
417 		rel_entsize = gelf_fsize(elf_file, ELF_T_REL, 1,
418 		    EV_CURRENT);
419 	}
420 	no_entries = reloc_size / rel_entsize;
421 
422 	no_syms = sym_size / gelf_fsize(elf_file, ELF_T_SYM, 1, EV_CURRENT);
423 	while (no_entries--) {
424 		(void) gelf_getrel(rdata, ndx, &rel);
425 		/* LINTED */
426 		type = (int)GELF_R_TYPE(rel.r_info);
427 		/* LINTED */
428 		symid = (int)GELF_R_SYM(rel.r_info);
429 		/* LINTED */
430 		if ((symid > (no_syms - 1)) || (symid < 0)) {
431 			(void) fprintf(stderr, "%s: %s: invalid symbol table "
432 			    "offset - %d - in %s\n", prog_name, filename,
433 			    symid, p_scns->scn_name);
434 			ndx++;
435 			continue;
436 		}
437 		(void) gelf_getsym(sym_data, symid, &sym);
438 		sym_name = (char *)elf_strptr(elf_file,
439 			reloc_symtab->p_shdr.sh_link, sym.st_name);
440 		if (sym_name == NULL)
441 			sym_name = (char *)UNKNOWN;
442 		if (r_flag && rn_flag) {
443 			if (strcmp(name, p_scns->scn_name) != 0) {
444 				ndx++;
445 				continue;
446 			}
447 			if (!n_title) {
448 				(void) printf("\n%s:\n", p_scns->scn_name);
449 				(void) printf("%-*s%-*s%s\n\n",
450 				    12 + adj, "Offset", 20, "Symndx", "Type");
451 				n_title = 1;
452 			}
453 		}
454 		if (d_flag) {
455 			if (!d_hi)
456 				d_hi = d_low;
457 			if ((symid < d_low) || (symid > d_hi)) {
458 				ndx++;
459 				continue;
460 			}
461 		}
462 
463 		(void) printf("%-#*llx", 12 + adj, EC_ADDR(rel.r_offset));
464 		if (!v_flag) {
465 			(void) printf("%-20d%-18d", symid, type);
466 		} else {
467 			if (strlen(sym_name))
468 				(void) printf("%-20s", sym_name);
469 			else {
470 				(void) printf("%-20d", sym.st_name);
471 			}
472 			(void) printf("%-20s",
473 				conv_reloc_type(p_ehdr->e_machine,
474 				type, DUMP_CONVFMT));
475 		}
476 		(void) printf("\n");
477 		ndx++;
478 	}
479 }
480 
481 /* demangle C++ names */
482 static char *
483 demangled_name(char *s)
484 {
485 	static char	*buf = NULL;
486 	char		*dn;
487 	size_t		len;
488 
489 	dn = sgs_demangle(s);
490 
491 	/*
492 	 * If not demangled, just return the symbol name
493 	 */
494 	if (strcmp(s, dn) == 0)
495 		return (s);
496 
497 	/*
498 	 * Demangled. Format it
499 	 */
500 	if (buf != NULL)
501 		free(buf);
502 
503 	len = strlen(dn) + strlen(s) + 4;
504 	if ((buf = malloc(len)) == NULL)
505 		return (s);
506 
507 	(void) snprintf(buf, len, "%s\t[%s]", dn, s);
508 	return (buf);
509 }
510 
511 /*
512  * Print the symbol table.  Input is an ELF file descriptor, a
513  * pointer to the symbol table SCNTAB structure,
514  * the number of symbols, a range of symbols to print,
515  * an index which is the number of the
516  * section in the file, and the filename.  The number of sections,
517  * the range, and the index are set in
518  * dump_symbol_table, depending on whether -n or -T were set.
519  */
520 static void
521 print_symtab(Elf *elf_file, SCNTAB *p_symtab, Elf_Data *sym_data,
522 	long range, int index)
523 {
524 	GElf_Sym sym;
525 	int adj = 0;		/* field adjustment for elf64 */
526 	Elf32_Word	*symshndx = 0;
527 	unsigned int	nosymshndx = 0;
528 
529 	if (gelf_getclass(elf_file) == ELFCLASS64)
530 		adj = 8;
531 
532 	while (range > 0) {
533 		char		*sym_name = (char *)0;
534 		int		type, bind;
535 		int		specsec;
536 		unsigned int	shndx;
537 
538 		(void) gelf_getsym(sym_data, index, &sym);
539 		type = (int)GELF_ST_TYPE(sym.st_info);
540 		bind = (int)GELF_ST_BIND(sym.st_info);
541 
542 		if ((sym.st_shndx == SHN_XINDEX) &&
543 		    (symshndx == 0) && (nosymshndx == 0)) {
544 			Elf_Scn		*_scn;
545 			GElf_Shdr	_shdr;
546 			size_t		symscnndx;
547 
548 			symscnndx = elf_ndxscn(p_symtab->p_sd);
549 			_scn = 0;
550 			while ((_scn = elf_nextscn(elf_file, _scn)) != 0) {
551 				if (gelf_getshdr(_scn, &_shdr) == 0)
552 					break;
553 				if ((_shdr.sh_type == SHT_SYMTAB_SHNDX) &&
554 				    /* LINTED */
555 				    (_shdr.sh_link == (GElf_Word)symscnndx)) {
556 					Elf_Data	*_data;
557 
558 					if ((_data = elf_getdata(_scn, 0)) == 0)
559 						continue;
560 
561 					symshndx = (Elf32_Word *)_data->d_buf;
562 					nosymshndx = 0;
563 					break;
564 				}
565 			}
566 			nosymshndx = 1;
567 		}
568 
569 		if ((symshndx) && (sym.st_shndx == SHN_XINDEX)) {
570 			shndx = symshndx[index];
571 			specsec = 0;
572 		} else {
573 			shndx = sym.st_shndx;
574 			if ((sym.st_shndx == SHN_UNDEF) ||
575 			    (sym.st_shndx >= SHN_LORESERVE))
576 				specsec = 1;
577 			else
578 				specsec = 0;
579 		}
580 
581 
582 		(void) printf("[%d]\t ", index++);
583 
584 		if (v_flag && (type == STT_SPARC_REGISTER)) {
585 			/*
586 			 *  The strings "REG_G1" through "REG_G7" are intended
587 			 *  to be consistent with output from elfdump(1).
588 			 */
589 			(void) printf("%-*s", 12 + adj,
590 			    conv_sym_SPARC_value(sym.st_value, DUMP_CONVFMT));
591 		} else {
592 			(void) printf("0x%-*llx", 10 + adj,
593 			    EC_ADDR(sym.st_value));
594 		}
595 
596 		(void) printf("%-*lld", 9 + adj, EC_XWORD(sym.st_size));
597 
598 		if (!v_flag) {
599 			(void) printf("%d\t\t%d\t%d\t%#x\t",
600 			    type, bind, (int)sym.st_other, (int)shndx);
601 		} else {
602 			GElf_Ehdr p_ehdr;
603 			(void) gelf_getehdr(elf_file, &p_ehdr);
604 			(void) printf("%s\t",
605 				conv_sym_info_type(p_ehdr.e_machine, type,
606 				DUMP_CONVFMT));
607 			(void) printf("%s",
608 				conv_sym_info_bind(bind, DUMP_CONVFMT));
609 			(void) printf("\t  %d\t", EC_WORD(sym.st_other));
610 
611 			if (specsec) {
612 				(void) printf("%s", conv_sym_shndx(shndx));
613 			} else
614 				(void) printf("%d", EC_WORD(shndx));
615 			(void) printf("\t");
616 		}
617 
618 		/* support machines where NULL-deref causes core dump */
619 		if (sym.st_name == 0)
620 			sym_name = (char *)UNKNOWN;
621 		else
622 			if (C_flag)
623 				sym_name = demangled_name(
624 					(char *)elf_strptr(elf_file,
625 					p_symtab->p_shdr.sh_link,
626 					sym.st_name));
627 		else
628 			sym_name = (char *)elf_strptr(elf_file,
629 				p_symtab->p_shdr.sh_link,
630 				sym.st_name);
631 		if (sym_name == NULL)
632 			sym_name = (char *)UNKNOWN;
633 		(void) printf("%s\n", sym_name);
634 
635 		range--;
636 	}	/* end while */
637 }
638 
639 /*
640  * Print the section header table.  Input is the SCNTAB structure,
641  * the number of sections, an index which is the number of the
642  * section in the file, and the filename.  The values of the SCNTAB
643  * structure, the number of sections, and the index are set in
644  * dump_shdr depending on whether the -n or -d modifiers were set.
645  */
646 static void
647 print_shdr(Elf *elf_file, SCNTAB *s, int num_scns, int index)
648 {
649 	SCNTAB *p;
650 	int num;
651 	int field;
652 	GElf_Ehdr p_ehdr;
653 
654 	if (gelf_getclass(elf_file) == ELFCLASS64)
655 		field = 21;
656 	else
657 		field = 13;
658 
659 	p = s;
660 	(void) gelf_getehdr(elf_file, &p_ehdr);
661 
662 	for (num = 0; num < num_scns; num++, p++) {
663 		(void) printf("[%d]\t", index++);
664 		if (!v_flag) {
665 			(void) printf("%u\t%llu\t",
666 			EC_WORD(p->p_shdr.sh_type),
667 			EC_XWORD(p->p_shdr.sh_flags));
668 		} else {
669 			/*LINTED: E_SEC_PRINTF_VAR_FMT*/
670 			(void) printf(conv_sec_type(p_ehdr.e_machine,
671 				p->p_shdr.sh_type, DUMP_CONVFMT));
672 			(void) printf("    ");
673 
674 			if (p->p_shdr.sh_flags & SHF_WRITE)
675 				(void) printf("W");
676 			else
677 				(void) printf("-");
678 			if (p->p_shdr.sh_flags & SHF_ALLOC)
679 				(void) printf("A");
680 			else
681 				(void) printf("-");
682 			if (p->p_shdr.sh_flags & SHF_EXECINSTR)
683 				(void) printf("I");
684 			else
685 				(void) printf("-");
686 
687 			if (p->p_shdr.sh_flags & SHF_ORDERED)
688 				(void) printf("O");
689 			if (p->p_shdr.sh_flags & SHF_EXCLUDE)
690 				(void) printf("E");
691 
692 			(void) printf("\t");
693 
694 		}
695 		(void) printf("%-#*llx%-#*llx%-#*llx%s%s\n",
696 			field, EC_ADDR(p->p_shdr.sh_addr),
697 			field, EC_OFF(p->p_shdr.sh_offset),
698 			field, EC_XWORD(p->p_shdr.sh_size),
699 			/* compatibility:  tab for elf32 */
700 			(field == 13) ? "\t" : " ", p->scn_name);
701 
702 		(void) printf("\t%u\t%u\t%-#*llx%-#*llx\n\n",
703 			EC_WORD(p->p_shdr.sh_link),
704 			EC_WORD(p->p_shdr.sh_info),
705 			field, EC_XWORD(p->p_shdr.sh_addralign),
706 			field, EC_XWORD(p->p_shdr.sh_entsize));
707 	}
708 }
709 
710 /*
711  * Check that a range of numbers is valid.  Input is
712  * a lower bound, an upper bound, a boundary condition,
713  * and the filename.  Negative numbers and numbers greater
714  * than the bound are invalid.  low must be smaller than hi.
715  * The returned integer is the number of items in the
716  * range if it is valid and -1 otherwise.
717  */
718 static int
719 check_range(int low, int hi, size_t bound, char *filename)
720 {
721 	if (((size_t)low > bound) || (low <= 0)) {
722 		(void) fprintf(stderr,
723 			"%s: %s: number out of range, %d\n",
724 			prog_name, filename, low);
725 		return (-1);
726 	}
727 	if (((size_t)hi > bound) || (hi < 0)) {
728 		(void) fprintf(stderr,
729 			"%s: %s: number out of range, %d\n",
730 			prog_name, filename, hi);
731 			return (-1);
732 	}
733 
734 	if (hi && (low > hi)) {
735 		(void) fprintf(stderr,
736 			"%s: %s: invalid range, %d,%d\n",
737 			prog_name, filename, low, hi);
738 		return (-1);
739 	}
740 	if (hi)
741 		return (hi - low + 1);
742 	else
743 		return (1);
744 }
745 
746 /*
747  * Print relocation information.  Since this information is
748  * machine dependent, new sections must be added for each machine
749  * that is supported.  Input is an ELF file descriptor, the ELF header,
750  * the SCNTAB structure, the number of sections, and a filename.
751  * Set up necessary information to print relocation information
752  * and call the appropriate print function depending on the
753  * type of relocation information.  If the symbol table is
754  * absent, no relocation data is processed.  Input is an
755  * ELF file descriptor, the ELF header, the SCNTAB structure,
756  * and the filename.  Set range of d_flag and name if n_flag.
757  */
758 static void
759 dump_reloc_table(Elf *elf_file, GElf_Ehdr *p_ehdr,
760 	SCNTAB *p_scns, int num_scns, char *filename)
761 {
762 	Elf_Data *rel_data;
763 	Elf_Data *sym_data;
764 	size_t    sym_size;
765 	size_t    reloc_size;
766 	SCNTAB *reloc_symtab;
767 	SCNTAB *head_scns;
768 	int r_title = 0;
769 	int adj = 0;
770 	size_t shnum;
771 
772 	if (gelf_getclass(elf_file) == ELFCLASS64)
773 		adj = 8;
774 
775 	if ((!p_flag) && (!r_title)) {
776 		(void) printf("\n    **** RELOCATION INFORMATION ****\n");
777 		r_title = 1;
778 	}
779 
780 	while (num_scns-- > 0) {
781 		if ((p_scns->p_shdr.sh_type != SHT_RELA) &&
782 		    (p_scns->p_shdr.sh_type != SHT_REL)) {
783 			p_scns++;
784 			continue;
785 		}
786 
787 	head_scns = p_head_scns;
788 
789 	if (elf_getshnum(elf_file, &shnum) == 0) {
790 		(void) fprintf(stderr,
791 			"%s: %s: elf_getshnum failed: %s\n",
792 			prog_name, filename, elf_errmsg(-1));
793 		return;
794 	}
795 
796 	if ((p_scns->p_shdr.sh_link == 0) ||
797 	    /* LINTED */
798 	    (p_scns->p_shdr.sh_link >= (GElf_Word)shnum)) {
799 		(void) fprintf(stderr, "%s: %s: invalid sh_link field: "
800 			"section #: %d sh_link: %d\n",
801 			/* LINTED */
802 			prog_name, filename, (int)elf_ndxscn(p_scns->p_sd),
803 			(int)p_scns->p_shdr.sh_link);
804 		return;
805 	}
806 	head_scns += (p_scns->p_shdr.sh_link -1);
807 
808 	if (head_scns->p_shdr.sh_type == SHT_SYMTAB) {
809 		reloc_symtab = p_symtab;
810 	} else if (head_scns->p_shdr.sh_type  == SHT_DYNSYM) {
811 		reloc_symtab = p_dynsym;
812 	} else {
813 		(void) fprintf(stderr,
814 "%s: %s: could not get symbol table\n", prog_name, filename);
815 		return;
816 	}
817 
818 	sym_data = NULL;
819 	sym_size = 0;
820 	reloc_size = 0;
821 
822 	if ((sym_data = elf_getdata(reloc_symtab->p_sd, NULL)) == NULL) {
823 		(void) fprintf(stderr,
824 		"%s: %s: no symbol table data\n", prog_name, filename);
825 		return;
826 	}
827 	sym_size = sym_data->d_size;
828 
829 	if (p_scns == NULL) {
830 		(void) fprintf(stderr,
831 		"%s: %s: no section table data\n", prog_name, filename);
832 		return;
833 	}
834 
835 	if (p_scns->p_shdr.sh_type == SHT_RELA) {
836 		if (!n_flag && r_flag)
837 			(void) printf("\n%s:\n", p_scns->scn_name);
838 		if (!p_flag && (!n_flag && r_flag))
839 			(void) printf("%-*s%-*s%-*s%s\n\n",
840 			    12 + adj, "Offset", 22, "Symndx",
841 			    18, "Type", "Addend");
842 		if ((rel_data = elf_getdata(p_scns->p_sd, NULL)) == NULL) {
843 			(void) fprintf(stderr,
844 "%s: %s: no relocation information\n", prog_name, filename);
845 			return;
846 		}
847 		reloc_size = rel_data->d_size;
848 
849 		if (n_flag) {
850 			rn_flag = 1;
851 			print_rela(elf_file, p_scns, rel_data, sym_data, p_ehdr,
852 				reloc_size, sym_size, filename, reloc_symtab);
853 		}
854 		if (d_flag) {
855 			rn_flag = 0;
856 			print_rela(elf_file, p_scns, rel_data, sym_data, p_ehdr,
857 				reloc_size, sym_size, filename, reloc_symtab);
858 		}
859 		if (!n_flag && !d_flag)
860 			print_rela(elf_file, p_scns, rel_data, sym_data, p_ehdr,
861 				reloc_size, sym_size, filename, reloc_symtab);
862 	} else {
863 		if (p_scns->p_shdr.sh_type == SHT_REL) {
864 			if (!n_flag && r_flag)
865 				(void) printf("\n%s:\n", p_scns->scn_name);
866 			if (!p_flag && (!n_flag && r_flag)) {
867 				(void) printf("%-*s%-*s%s\n\n",
868 				    12 + adj, "Offset", 20, "Symndx", "Type");
869 			}
870 			if ((rel_data = elf_getdata(p_scns->p_sd, NULL))
871 			    == NULL) {
872 				(void) fprintf(stderr,
873 "%s: %s: no relocation information\n", prog_name, filename);
874 				return;
875 			}
876 			reloc_size = rel_data->d_size;
877 			if (n_flag) {
878 				rn_flag = 1;
879 				print_rel(elf_file, p_scns, rel_data, sym_data,
880 					p_ehdr, reloc_size, sym_size,
881 					filename, reloc_symtab);
882 			}
883 			if (d_flag) {
884 				rn_flag = 0;
885 				print_rel(elf_file, p_scns, rel_data, sym_data,
886 					p_ehdr, reloc_size, sym_size,
887 					filename, reloc_symtab);
888 			}
889 			if (!n_flag && !d_flag)
890 				print_rel(elf_file, p_scns, rel_data, sym_data,
891 					p_ehdr, reloc_size, sym_size,
892 					filename, reloc_symtab);
893 		}
894 	}
895 	p_scns++;
896 	}
897 }
898 
899 /*
900  * Print out the string tables.  Input is an opened ELF file,
901  * the SCNTAB structure, the number of sections, and the filename.
902  * Since there can be more than one string table, all sections are
903  * examined and any with the correct type are printed out.
904  */
905 static void
906 dump_string_table(SCNTAB *s, int num_scns)
907 {
908 	size_t section_size;
909 	unsigned char *strtab;
910 	int beg_of_string;
911 	int counter = 0;
912 	int str_off;
913 	int i;
914 
915 	if (!p_flag) {
916 		(void) printf("\n     **** STRING TABLE INFORMATION ****\n");
917 	}
918 
919 	for (i = 0; i < num_scns; i++, s++) {
920 		if (s->p_shdr.sh_type != SHT_STRTAB)
921 			continue;
922 
923 		str_off = 0;
924 
925 		if (!p_flag) {
926 			(void) printf("\n%s:\n", s->scn_name);
927 			(void) printf("   <offset>  \tName\n");
928 		}
929 		section_size = 0;
930 		if ((strtab = (unsigned char *)
931 		    get_scndata(s->p_sd, &section_size)) == NULL) {
932 			continue;
933 		}
934 
935 		if (section_size != 0) {
936 			(void) printf("   <%d>  \t", str_off);
937 			beg_of_string = 0;
938 			while (section_size--) {
939 				unsigned char c = *strtab++;
940 
941 				if (beg_of_string) {
942 					(void) printf("   <%d>  \t", str_off);
943 					counter++;
944 					beg_of_string = 0;
945 				}
946 				str_off++;
947 				switch (c) {
948 				case '\0':
949 					(void) printf("\n");
950 					beg_of_string = 1;
951 					break;
952 				default:
953 					(void) putchar(c);
954 				}
955 			}
956 		}
957 	}
958 	(void) printf("\n");
959 }
960 
961 /*
962  * Print the symbol table.  This function does not print the contents
963  * of the symbol table but sets up the parameters and then calls
964  * print_symtab to print the symbols.  Calling another function to print
965  * the symbols allows both -T and -n to work correctly
966  * simultaneously.  Input is an opened ELF file, a pointer to the
967  * symbol table SCNTAB structure, and the filename.
968  * Set the range of symbols to print if T_flag, and set
969  * name of symbol to print if n_flag.
970  */
971 static void
972 dump_symbol_table(Elf *elf_file, SCNTAB *p_symtab, char *filename)
973 {
974 	Elf_Data  *sym_data;
975 	GElf_Sym  T_range, n_range;	/* for use with -T and -n */
976 	size_t count = 0;
977 	size_t sym_size;
978 	int index = 1;
979 	int found_it = 0;
980 	int i;
981 	int adj = 0;			/*  field adjustment for elf64 */
982 
983 	if (gelf_getclass(elf_file) == ELFCLASS64)
984 		adj = 8;
985 
986 	if (p_symtab == NULL) {
987 		(void) fprintf(stderr,
988 		"%s: %s: could not get symbol table\n", prog_name, filename);
989 		return;
990 	}
991 
992 	/* get symbol table data */
993 	sym_data = NULL;
994 	sym_size = 0;
995 	if ((sym_data =
996 	    elf_getdata(p_symtab->p_sd, NULL)) == NULL) {
997 		(void) printf("\n%s:\n", p_symtab->scn_name);
998 		(void) printf("No symbol table data\n");
999 		return;
1000 	}
1001 	sym_size = sym_data->d_size;
1002 
1003 	count = sym_size / p_symtab->p_shdr.sh_entsize;
1004 
1005 	if (n_flag && t_flag && !T_flag) {
1006 		/* LINTED */
1007 		for (i = 1; i < count; i++) {
1008 			(void) gelf_getsym(sym_data, i, &n_range);
1009 			if (strcmp(name, (char *)
1010 			    elf_strptr(elf_file,
1011 			    p_symtab->p_shdr.sh_link,
1012 			    n_range.st_name)) != 0) {
1013 				continue;
1014 			} else {
1015 				found_it = 1;
1016 				if (!p_flag) {
1017 					(void) printf(
1018 "\n              ***** SYMBOL TABLE INFORMATION *****\n");
1019 					(void) printf(
1020 "[Index]  %-*s%-*sType\tBind\tOther\tShndx\tName",
1021 			    12 + adj, "Value", 9 + adj, "Size");
1022 				}
1023 				(void) printf("\n%s:\n", p_symtab->scn_name);
1024 				print_symtab(elf_file, p_symtab, sym_data,
1025 				    1, i);
1026 			}
1027 		}   /* end for */
1028 		if (!found_it) {
1029 			(void) fprintf(stderr, "%s: %s: %s not found\n",
1030 			prog_name, filename, name);
1031 		}
1032 	} else if (T_flag) {
1033 		T_num = check_range(T_low, T_hi, count, filename);
1034 		if (T_num < 0)
1035 			return;
1036 
1037 		(void) gelf_getsym(sym_data, T_low-1, &T_range);
1038 		index = T_low;
1039 
1040 		if (!p_flag) {
1041 			(void) printf(
1042 "\n              ***** SYMBOL TABLE INFORMATION *****\n");
1043 			(void) printf(
1044 "[Index]  %-*s%-*sType\tBind\tOther\tShndx\tName",
1045 			    12 + adj, "Value", 9 + adj, "Size");
1046 		}
1047 		(void) printf("\n%s:\n", p_symtab->scn_name);
1048 		print_symtab(elf_file, p_symtab, sym_data, T_num, index);
1049 	} else {
1050 		if (!p_flag) {
1051 			(void) printf(
1052 "\n              ***** SYMBOL TABLE INFORMATION *****\n");
1053 			(void) printf(
1054 "[Index]  %-*s%-*sType\tBind\tOther\tShndx\tName",
1055 			    12 + adj, "Value", 9 + adj, "Size");
1056 		}
1057 		(void) printf("\n%s:\n", p_symtab->scn_name);
1058 		print_symtab(elf_file, p_symtab, sym_data, count-1, 1);
1059 	}
1060 }
1061 
1062 
1063 /*
1064  * Print dynamic linking information.  Input is an ELF
1065  * file descriptor, the SCNTAB structure, the number of
1066  * sections, and the filename.
1067  */
1068 static void
1069 dump_dynamic(Elf *elf_file, SCNTAB *p_scns, int num_scns, char *filename)
1070 {
1071 #define	pdyn_Fmtptr	"%#llx"
1072 
1073 	Elf_Data	*dyn_data;
1074 	GElf_Dyn	p_dyn;
1075 	GElf_Phdr	p_phdr;
1076 	GElf_Ehdr	p_ehdr;
1077 	int		index = 1;
1078 	int		lib_scns = num_scns;
1079 	SCNTAB		*l_scns = p_scns;
1080 	int		header_num = 0;
1081 	const char	*str;
1082 
1083 	(void) gelf_getehdr(elf_file, &p_ehdr);
1084 
1085 	if (!p_flag)
1086 		(void) printf("\n  **** DYNAMIC SECTION INFORMATION ****\n");
1087 
1088 	for (; num_scns > 0; num_scns--, p_scns++) {
1089 		GElf_Word	link;
1090 		int		ii;
1091 
1092 
1093 		if (p_scns->p_shdr.sh_type != SHT_DYNAMIC)
1094 			continue;
1095 
1096 		if (!p_flag) {
1097 			(void) printf("%s:\n", p_scns->scn_name);
1098 			(void) printf("[INDEX]\tTag         Value\n");
1099 		}
1100 
1101 		if ((dyn_data = elf_getdata(p_scns->p_sd, NULL)) == 0) {
1102 			(void) fprintf(stderr, "%s: %s: no data in "
1103 			    "%s section\n", prog_name, filename,
1104 			    p_scns->scn_name);
1105 			return;
1106 		}
1107 
1108 		link = p_scns->p_shdr.sh_link;
1109 		ii = 0;
1110 
1111 		(void) gelf_getdyn(dyn_data, ii++, &p_dyn);
1112 		while (p_dyn.d_tag != DT_NULL) {
1113 			(void) printf("[%d]\t%-15.15s ", index++,
1114 				conv_dyn_tag(p_dyn.d_tag, p_ehdr.e_machine,
1115 				DUMP_CONVFMT));
1116 
1117 			/*
1118 			 * It would be nice to use a table driven loop
1119 			 * here, but the address space is too sparse
1120 			 * and irregular. A switch is simple and robust.
1121 			 */
1122 			switch (p_dyn.d_tag) {
1123 			/*
1124 			 * Items with an address value
1125 			 */
1126 			case DT_PLTGOT:
1127 			case DT_HASH:
1128 			case DT_STRTAB:
1129 			case DT_RELA:
1130 			case DT_SYMTAB:
1131 			case DT_INIT:
1132 			case DT_FINI:
1133 			case DT_REL:
1134 			case DT_DEBUG:
1135 			case DT_TEXTREL:
1136 			case DT_JMPREL:
1137 			case DT_INIT_ARRAY:
1138 			case DT_FINI_ARRAY:
1139 			case DT_INIT_ARRAYSZ:
1140 			case DT_FINI_ARRAYSZ:
1141 			case DT_PREINIT_ARRAY:
1142 			case DT_PREINIT_ARRAYSZ:
1143 			case DT_SUNW_RTLDINF:
1144 			case DT_SUNW_CAP:
1145 			case DT_SUNW_SYMTAB:
1146 			case DT_SUNW_SYMSORT:
1147 			case DT_SUNW_TLSSORT:
1148 			case DT_PLTPAD:
1149 			case DT_MOVETAB:
1150 			case DT_SYMINFO:
1151 			case DT_RELACOUNT:
1152 			case DT_RELCOUNT:
1153 			case DT_VERSYM:
1154 			case DT_VERDEF:
1155 			case DT_VERDEFNUM:
1156 			case DT_VERNEED:
1157 				(void) printf(pdyn_Fmtptr,
1158 					EC_ADDR(p_dyn.d_un.d_ptr));
1159 				break;
1160 
1161 			/*
1162 			 * Items with a string value
1163 			 */
1164 			case DT_NEEDED:
1165 			case DT_SONAME:
1166 			case DT_RPATH:
1167 			case DT_RUNPATH:
1168 			case DT_SUNW_AUXILIARY:
1169 			case DT_SUNW_FILTER:
1170 			case DT_CONFIG:
1171 			case DT_DEPAUDIT:
1172 			case DT_AUDIT:
1173 			case DT_AUXILIARY:
1174 			case DT_USED:
1175 			case DT_FILTER:
1176 				if (v_flag) {	/* Look up the string */
1177 					str = (char *)elf_strptr(elf_file, link,
1178 						p_dyn.d_un.d_ptr);
1179 					if (!(str && *str))
1180 						str = (char *)UNKNOWN;
1181 					(void) printf("%s", str);
1182 				} else {	/* Show the address */
1183 					(void) printf(pdyn_Fmtptr,
1184 						EC_ADDR(p_dyn.d_un.d_ptr));
1185 				}
1186 				break;
1187 
1188 			/*
1189 			 * Items with a literal value
1190 			 */
1191 			case DT_PLTRELSZ:
1192 			case DT_RELASZ:
1193 			case DT_RELAENT:
1194 			case DT_STRSZ:
1195 			case DT_SYMENT:
1196 			case DT_RELSZ:
1197 			case DT_RELENT:
1198 			case DT_PLTREL:
1199 			case DT_BIND_NOW:
1200 			case DT_CHECKSUM:
1201 			case DT_PLTPADSZ:
1202 			case DT_MOVEENT:
1203 			case DT_MOVESZ:
1204 			case DT_SYMINSZ:
1205 			case DT_SYMINENT:
1206 			case DT_VERNEEDNUM:
1207 			case DT_SPARC_REGISTER:
1208 			case DT_SUNW_SYMSZ:
1209 			case DT_SUNW_SORTENT:
1210 			case DT_SUNW_SYMSORTSZ:
1211 			case DT_SUNW_TLSSORTSZ:
1212 			case DT_SUNW_STRPAD:
1213 				(void) printf(pdyn_Fmtptr,
1214 					EC_XWORD(p_dyn.d_un.d_val));
1215 				break;
1216 
1217 			/*
1218 			 * Items that are bitmasks
1219 			 */
1220 			case DT_FLAGS:
1221 			case DT_FEATURE_1:
1222 			case DT_POSFLAG_1:
1223 			case DT_FLAGS_1:
1224 				str = NULL;
1225 				if (v_flag) {
1226 					switch (p_dyn.d_tag) {
1227 					case DT_FLAGS:
1228 						str = conv_dyn_flag(
1229 							p_dyn.d_un.d_val,
1230 							DUMP_CONVFMT);
1231 					break;
1232 					case DT_FEATURE_1:
1233 						str = conv_dyn_feature1(
1234 							p_dyn.d_un.d_val,
1235 							DUMP_CONVFMT);
1236 						break;
1237 					case DT_POSFLAG_1:
1238 						str = conv_dyn_posflag1(
1239 							p_dyn.d_un.d_val,
1240 							DUMP_CONVFMT);
1241 						break;
1242 					case DT_FLAGS_1:
1243 						str = conv_dyn_flag1(
1244 							p_dyn.d_un.d_val);
1245 						break;
1246 					}
1247 				}
1248 				if (str) {	/* Show as string */
1249 					(void) printf("%s", str);
1250 				} else {	/* Numeric form */
1251 					(void) printf(pdyn_Fmtptr,
1252 						EC_ADDR(p_dyn.d_un.d_ptr));
1253 				}
1254 				break;
1255 
1256 			/*
1257 			 * Depreciated items with a literal value
1258 			 */
1259 			case DT_DEPRECATED_SPARC_REGISTER:
1260 				(void) printf(pdyn_Fmtptr
1261 					"  (deprecated value)",
1262 					EC_XWORD(p_dyn.d_un.d_val));
1263 				break;
1264 
1265 			/* Ignored items */
1266 			case DT_SYMBOLIC:
1267 				(void) printf("(ignored)");
1268 				break;
1269 			}
1270 			(void) printf("\n");
1271 			(void) gelf_getdyn(dyn_data, ii++, &p_dyn);
1272 		}
1273 	}
1274 
1275 	/*
1276 	 * Check for existence of static shared library information.
1277 	 */
1278 	while (header_num < p_ehdr.e_phnum) {
1279 		(void) gelf_getphdr(elf_file, header_num, &p_phdr);
1280 		if (p_phdr.p_type == PT_SHLIB) {
1281 			while (--lib_scns > 0) {
1282 				if (strcmp(l_scns->scn_name, ".lib") == 0) {
1283 					print_static(l_scns, filename);
1284 				}
1285 				l_scns++;
1286 			}
1287 		}
1288 		header_num++;
1289 	}
1290 #undef	pdyn_Fmtptr
1291 }
1292 
1293 /*
1294  * Print the ELF header.  Input is an ELF file descriptor
1295  * and the filename.  If f_flag is set, the ELF header is
1296  * printed to stdout, otherwise the function returns after
1297  * setting the pointer to the ELF header.  Any values which
1298  * are not known are printed in decimal.  Fields must be updated
1299  * as new values are added.
1300  */
1301 static GElf_Ehdr *
1302 dump_elf_header(Elf *elf_file, char *filename, GElf_Ehdr * elf_head_p)
1303 {
1304 	int class;
1305 	int field;
1306 
1307 	if (gelf_getehdr(elf_file, elf_head_p) == NULL) {
1308 		(void) fprintf(stderr, "%s: %s: %s\n", prog_name, filename,
1309 		    elf_errmsg(-1));
1310 		return (NULL);
1311 	}
1312 
1313 	class = (int)elf_head_p->e_ident[4];
1314 
1315 	if (class == ELFCLASS64)
1316 		field = 21;
1317 	else
1318 		field = 13;
1319 
1320 	if (!f_flag)
1321 		return (elf_head_p);
1322 
1323 	if (!p_flag) {
1324 		(void) printf("\n                    **** ELF HEADER ****\n");
1325 		(void) printf("%-*s%-11s%-*sMachine     Version\n",
1326 		    field, "Class", "Data", field, "Type");
1327 		(void) printf("%-*s%-11s%-*sFlags       Ehsize\n",
1328 		    field, "Entry", "Phoff", field, "Shoff");
1329 		(void) printf("%-*s%-11s%-*sShnum       Shstrndx\n\n",
1330 		    field, "Phentsize", "Phnum", field, "Shentsz");
1331 	}
1332 
1333 	if (!v_flag) {
1334 		(void) printf("%-*d%-11d%-*d%-12d%d\n",
1335 			field, elf_head_p->e_ident[4],
1336 			elf_head_p->e_ident[5],
1337 			field, (int)elf_head_p->e_type,
1338 			(int)elf_head_p->e_machine,
1339 			elf_head_p->e_version);
1340 	} else {
1341 		(void) printf("%-*s", field,
1342 			conv_ehdr_class(class, DUMP_CONVFMT));
1343 		(void) printf("%-11s",
1344 			conv_ehdr_data(elf_head_p->e_ident[5], DUMP_CONVFMT));
1345 		(void) printf("%-*s", field,
1346 			conv_ehdr_type(elf_head_p->e_type, DUMP_CONVFMT));
1347 		(void) printf("%-12s",
1348 			conv_ehdr_mach(elf_head_p->e_machine, DUMP_CONVFMT));
1349 		(void) printf("%s\n",
1350 			conv_ehdr_vers(elf_head_p->e_version, DUMP_CONVFMT));
1351 	}
1352 	(void) printf("%-#*llx%-#11llx%-#*llx%-#12x%#x\n",
1353 		field, EC_ADDR(elf_head_p->e_entry),
1354 		EC_OFF(elf_head_p->e_phoff),
1355 		field, EC_OFF(elf_head_p->e_shoff),
1356 		EC_WORD(elf_head_p->e_flags),
1357 		EC_WORD(elf_head_p->e_ehsize));
1358 	if (!v_flag || (elf_head_p->e_shstrndx != SHN_XINDEX)) {
1359 		(void) printf("%-#*x%-11u%-#*x%-12u%u\n",
1360 			field, EC_WORD(elf_head_p->e_phentsize),
1361 			EC_WORD(elf_head_p->e_phnum),
1362 			field, EC_WORD(elf_head_p->e_shentsize),
1363 			EC_WORD(elf_head_p->e_shnum),
1364 			EC_WORD(elf_head_p->e_shstrndx));
1365 	} else {
1366 		(void) printf("%-#*x%-11u%-#*x%-12uXINDEX\n",
1367 			field, EC_WORD(elf_head_p->e_phentsize),
1368 			EC_WORD(elf_head_p->e_phnum),
1369 			field, EC_WORD(elf_head_p->e_shentsize),
1370 			EC_WORD(elf_head_p->e_shnum));
1371 	}
1372 	if ((elf_head_p->e_shnum == 0) && (elf_head_p->e_shoff > 0)) {
1373 		Elf_Scn		*scn;
1374 		GElf_Shdr	shdr0;
1375 		int		field;
1376 
1377 		if (gelf_getclass(elf_file) == ELFCLASS64)
1378 			field = 21;
1379 		else
1380 			field = 13;
1381 		if (!p_flag) {
1382 			(void) printf("\n	   **** SECTION HEADER[0] "
1383 			    "{Elf Extensions} ****\n");
1384 			(void) printf(
1385 			    "[No]\tType\tFlags\t%-*s %-*s%-*s%sName\n",
1386 			    field, "Addr", field, "Offset", field,
1387 			    "Size(shnum)",
1388 			    /* compatibility:  tab for elf32 */
1389 			    (field == 13) ? "\t" : "  ");
1390 			(void) printf("\tLn(strndx) Info\t%-*s Entsize\n",
1391 			    field, "Adralgn");
1392 		}
1393 		if ((scn = elf_getscn(elf_file, 0)) == NULL) {
1394 			(void) fprintf(stderr,
1395 				"%s: %s: elf_getscn failed: %s\n",
1396 				prog_name, filename, elf_errmsg(-1));
1397 			return (NULL);
1398 		}
1399 		if (gelf_getshdr(scn, &shdr0) == 0) {
1400 			(void) fprintf(stderr,
1401 				"%s: %s: gelf_getshdr: %s\n",
1402 				prog_name, filename, elf_errmsg(-1));
1403 			return (NULL);
1404 		}
1405 		(void) printf("[0]\t%u\t%llu\t", EC_WORD(shdr0.sh_type),
1406 			EC_XWORD(shdr0.sh_flags));
1407 
1408 		(void) printf("%-#*llx %-#*llx%-*llu%s%-*u\n",
1409 			field, EC_ADDR(shdr0.sh_addr),
1410 			field, EC_OFF(shdr0.sh_offset),
1411 			field, EC_XWORD(shdr0.sh_size),
1412 			/* compatibility:  tab for elf32 */
1413 			((field == 13) ? "\t" : "  "),
1414 			field, EC_WORD(shdr0.sh_name));
1415 
1416 		(void) printf("\t%u\t%u\t%-#*llx %-#*llx\n",
1417 			EC_WORD(shdr0.sh_link),
1418 			EC_WORD(shdr0.sh_info),
1419 			field, EC_XWORD(shdr0.sh_addralign),
1420 			field, EC_XWORD(shdr0.sh_entsize));
1421 	}
1422 	(void) printf("\n");
1423 
1424 	return (elf_head_p);
1425 }
1426 
1427 /*
1428  * Print section contents.  Input is an ELF file descriptor,
1429  * the ELF header, the SCNTAB structure,
1430  * the number of symbols, and the filename.
1431  * The number of sections,
1432  * and the offset into the SCNTAB structure will be
1433  * set in dump_section if d_flag or n_flag are set.
1434  * If v_flag is set, sections which can be interpreted will
1435  * be interpreted, otherwise raw data will be output in hexidecimal.
1436  */
1437 static void
1438 print_section(Elf *elf_file,
1439 	GElf_Ehdr *p_ehdr, SCNTAB *p, int num_scns, char *filename)
1440 {
1441 	unsigned char    *p_sec;
1442 	int	i;
1443 	size_t	size;
1444 
1445 	for (i = 0; i < num_scns; i++, p++) {
1446 		GElf_Shdr shdr;
1447 
1448 		size = 0;
1449 		if (s_flag && !v_flag)
1450 			p_sec = (unsigned char *)get_rawscn(p->p_sd, &size);
1451 		else
1452 			p_sec = (unsigned char *)get_scndata(p->p_sd, &size);
1453 
1454 		if ((gelf_getshdr(p->p_sd, &shdr) != NULL) &&
1455 		    (shdr.sh_type == SHT_NOBITS)) {
1456 			continue;
1457 		}
1458 		if (s_flag && !v_flag) {
1459 			(void) printf("\n%s:\n", p->scn_name);
1460 			print_rawdata(p_sec, size);
1461 			continue;
1462 		}
1463 		if (shdr.sh_type == SHT_SYMTAB) {
1464 			dump_symbol_table(elf_file, p, filename);
1465 			continue;
1466 		}
1467 		if (shdr.sh_type == SHT_DYNSYM) {
1468 			dump_symbol_table(elf_file, p, filename);
1469 			continue;
1470 		}
1471 		if (shdr.sh_type == SHT_STRTAB) {
1472 			dump_string_table(p, 1);
1473 			continue;
1474 		}
1475 		if (shdr.sh_type == SHT_RELA) {
1476 			dump_reloc_table(elf_file, p_ehdr, p, 1, filename);
1477 			continue;
1478 		}
1479 		if (shdr.sh_type == SHT_REL) {
1480 			dump_reloc_table(elf_file, p_ehdr, p, 1, filename);
1481 			continue;
1482 		}
1483 		if (shdr.sh_type == SHT_DYNAMIC) {
1484 			dump_dynamic(elf_file, p, 1, filename);
1485 			continue;
1486 		}
1487 
1488 		(void) printf("\n%s:\n", p->scn_name);
1489 		print_rawdata(p_sec, size);
1490 	}
1491 	(void) printf("\n");
1492 }
1493 
1494 /*
1495  * Print section contents. This function does not print the contents
1496  * of the sections but sets up the parameters and then calls
1497  * print_section to print the contents.  Calling another function to print
1498  * the contents allows both -d and -n to work correctly
1499  * simultaneously. Input is an ELF file descriptor, the ELF header,
1500  * the SCNTAB structure, the number of sections, and the filename.
1501  * Set the range of sections if d_flag, and set section name if
1502  * n_flag.
1503  */
1504 static void
1505 dump_section(Elf *elf_file,
1506 	GElf_Ehdr *p_ehdr, SCNTAB *s, int num_scns, char *filename)
1507 {
1508 	SCNTAB *n_range, *d_range; /* for use with -n and -d modifiers */
1509 	int i;
1510 	int found_it = 0;  /* for use with -n section_name */
1511 
1512 	if (n_flag) {
1513 		n_range = s;
1514 
1515 		for (i = 0; i < num_scns; i++, n_range++) {
1516 			if ((strcmp(name, n_range->scn_name)) != 0)
1517 				continue;
1518 			else {
1519 				found_it = 1;
1520 				print_section(elf_file, p_ehdr,
1521 					n_range, 1, filename);
1522 			}
1523 		}
1524 
1525 		if (!found_it) {
1526 			(void) fprintf(stderr, "%s: %s: %s not found\n",
1527 				prog_name, filename, name);
1528 		}
1529 	} /* end n_flag */
1530 
1531 	if (d_flag) {
1532 		d_range = s;
1533 		d_num = check_range(d_low, d_hi, num_scns, filename);
1534 		if (d_num < 0)
1535 			return;
1536 		d_range += d_low - 1;
1537 
1538 		print_section(elf_file, p_ehdr, d_range, d_num, filename);
1539 	}	/* end d_flag */
1540 
1541 	if (!n_flag && !d_flag)
1542 		print_section(elf_file, p_ehdr, s, num_scns, filename);
1543 }
1544 
1545 /*
1546  * Print the section header table. This function does not print the contents
1547  * of the section headers but sets up the parameters and then calls
1548  * print_shdr to print the contents.  Calling another function to print
1549  * the contents allows both -d and -n to work correctly
1550  * simultaneously.  Input is the SCNTAB structure,
1551  * the number of sections from the ELF header, and the filename.
1552  * Set the range of section headers to print if d_flag, and set
1553  * name of section header to print if n_flag.
1554  */
1555 static void
1556 dump_shdr(Elf *elf_file, SCNTAB *s, int num_scns, char *filename)
1557 {
1558 
1559 	SCNTAB *n_range, *d_range;	/* for use with -n and -d modifiers */
1560 	int field;
1561 	int i;
1562 	int found_it = 0;  /* for use with -n section_name */
1563 
1564 	if (gelf_getclass(elf_file) == ELFCLASS64)
1565 		field = 21;
1566 	else
1567 		field = 13;
1568 
1569 	if (!p_flag) {
1570 		(void) printf("\n	   **** SECTION HEADER TABLE ****\n");
1571 		(void) printf("[No]\tType\tFlags\t%-*s %-*s %-*s%sName\n",
1572 		    field, "Addr", field, "Offset", field, "Size",
1573 		    /* compatibility:  tab for elf32 */
1574 		    (field == 13) ? "\t" : "  ");
1575 		(void) printf("\tLink\tInfo\t%-*s Entsize\n\n",
1576 		    field, "Adralgn");
1577 	}
1578 
1579 	if (n_flag) {
1580 		n_range = s;
1581 
1582 		for (i = 1; i <= num_scns; i++, n_range++) {
1583 			if ((strcmp(name, n_range->scn_name)) != 0)
1584 				continue;
1585 			else {
1586 				found_it = 1;
1587 				print_shdr(elf_file, n_range, 1, i);
1588 			}
1589 		}
1590 
1591 		if (!found_it) {
1592 			(void) fprintf(stderr, "%s: %s: %s not found\n",
1593 				prog_name, filename, name);
1594 		}
1595 	} /* end n_flag */
1596 
1597 	if (d_flag) {
1598 		d_range = s;
1599 		d_num = check_range(d_low, d_hi, num_scns, filename);
1600 		if (d_num < 0)
1601 			return;
1602 		d_range += d_low - 1;
1603 
1604 		print_shdr(elf_file, d_range, d_num, d_low);
1605 	}	/* end d_flag */
1606 
1607 	if (!n_flag && !d_flag)
1608 		print_shdr(elf_file, s, num_scns, 1);
1609 }
1610 
1611 /*
1612  * Process all of the command line options (except
1613  * for -a, -g, -f, and -o).  All of the options processed
1614  * by this function require the presence of the section
1615  * header table and will not be processed if it is not present.
1616  * Set up a buffer containing section name, section header,
1617  * and section descriptor for each section in the file.  This
1618  * structure is used to avoid duplicate calls to libelf functions.
1619  * Structure members for the symbol table, the debugging information,
1620  * and the line number information are global.  All of the
1621  * rest are local.
1622  */
1623 static void
1624 dump_section_table(Elf *elf_file, GElf_Ehdr *elf_head_p, char *filename)
1625 {
1626 
1627 	static SCNTAB	*buffer, *p_scns;
1628 	Elf_Scn		*scn = 0;
1629 	char		*s_name = NULL;
1630 	int		found = 0;
1631 	unsigned int	num_scns;
1632 	size_t		shstrndx;
1633 	size_t		shnum;
1634 
1635 
1636 	if (elf_getshnum(elf_file, &shnum) == 0) {
1637 		(void) fprintf(stderr,
1638 			"%s: %s: elf_getshnum failed: %s\n",
1639 			prog_name, filename, elf_errmsg(-1));
1640 		return;
1641 	}
1642 	if (elf_getshstrndx(elf_file, &shstrndx) == 0) {
1643 		(void) fprintf(stderr,
1644 			"%s: %s: elf_getshstrndx failed: %s\n",
1645 			prog_name, filename, elf_errmsg(-1));
1646 		return;
1647 	}
1648 
1649 	if ((buffer = calloc(shnum, sizeof (SCNTAB))) == NULL) {
1650 		(void) fprintf(stderr, "%s: %s: cannot calloc space\n",
1651 			prog_name, filename);
1652 		return;
1653 	}
1654 	/* LINTED */
1655 	num_scns = (int)shnum - 1;
1656 
1657 	p_symtab = (SCNTAB *)0;
1658 	p_dynsym = (SCNTAB *)0;
1659 	p_scns = buffer;
1660 	p_head_scns = buffer;
1661 
1662 	while ((scn = elf_nextscn(elf_file, scn)) != 0) {
1663 		if ((gelf_getshdr(scn, &buffer->p_shdr)) == 0) {
1664 			(void) fprintf(stderr,
1665 			"%s: %s: %s\n", prog_name, filename, elf_errmsg(-1));
1666 			return;
1667 		}
1668 		s_name = (char *)elf_strptr(elf_file,
1669 			shstrndx, buffer->p_shdr.sh_name);
1670 		buffer->scn_name = s_name ? s_name : (char *)UNKNOWN;
1671 		buffer->p_sd   =  scn;
1672 
1673 		if (buffer->p_shdr.sh_type == SHT_SYMTAB) {
1674 			found += 1;
1675 			p_symtab = buffer;
1676 		}
1677 		if (buffer->p_shdr.sh_type == SHT_DYNSYM)
1678 			p_dynsym = buffer;
1679 		buffer++;
1680 	}
1681 
1682 	/*
1683 	 * These functions depend upon the presence of the section header table
1684 	 * and will not be invoked in its absence
1685 	 */
1686 	if (h_flag) {
1687 		dump_shdr(elf_file, p_scns, num_scns, filename);
1688 	}
1689 	if (p_symtab && (t_flag || T_flag)) {
1690 		dump_symbol_table(elf_file, p_symtab, filename);
1691 	}
1692 	if (c_flag) {
1693 		dump_string_table(p_scns, num_scns);
1694 	}
1695 	if (r_flag) {
1696 		dump_reloc_table(elf_file, elf_head_p,
1697 			p_scns, num_scns, filename);
1698 	}
1699 	if (L_flag) {
1700 		dump_dynamic(elf_file, p_scns, num_scns, filename);
1701 	}
1702 	if (s_flag) {
1703 		dump_section(elf_file, elf_head_p, p_scns,
1704 			num_scns, filename);
1705 	}
1706 }
1707 
1708 /*
1709  * Load the archive string table(s) (for extended-length strings)
1710  * into an in-core table/list
1711  */
1712 static struct stab_list_s *
1713 load_arstring_table(struct stab_list_s *STabList,
1714 	int fd, Elf *elf_file, Elf_Arhdr *p_ar, char *filename)
1715 {
1716 	off_t here;
1717 	struct stab_list_s *STL_entry, *STL_next;
1718 
1719 	if (p_ar) {
1720 		STL_entry = malloc(sizeof (struct stab_list_s));
1721 		STL_entry->next    = 0;
1722 		STL_entry->strings = 0;
1723 		STL_entry->size    = 0;
1724 
1725 		if (!STabList)
1726 			STabList = STL_entry;
1727 		else {
1728 			STL_next = STabList;
1729 			while (STL_next->next != (void *)0)
1730 				STL_next = STL_next->next;
1731 			STL_next->next = STL_entry;
1732 		}
1733 
1734 		STL_entry->size    = p_ar->ar_size;
1735 		STL_entry->strings = malloc(p_ar->ar_size);
1736 		here = elf_getbase(elf_file);
1737 		if ((lseek(fd, here, 0)) != here) {
1738 			(void) fprintf(stderr,
1739 			"%s: %s: could not lseek\n", prog_name, filename);
1740 		}
1741 
1742 		if ((read(fd, STL_entry->strings, p_ar->ar_size)) == -1) {
1743 			(void) fprintf(stderr,
1744 			"%s: %s: could not read\n", prog_name, filename);
1745 		}
1746 	}
1747 	return (STabList);
1748 }
1749 
1750 /*
1751  * Print the archive header for each member of an archive.
1752  * Also call ar_sym_read to print the symbols in the
1753  * archive symbol table if g_flag.  Input is a file descriptor,
1754  * an ELF file descriptor, and the filename.  Putting the call
1755  * to dump the archive symbol table in this function is more
1756  * efficient since it is necessary to examine the archive member
1757  * name in the archive header to determine which member is the
1758  * symbol table.
1759  */
1760 static void
1761 dump_ar_hdr(int fd, Elf *elf_file, char *filename)
1762 {
1763 	extern int v_flag, g_flag, a_flag, p_flag;
1764 	Elf_Arhdr  *p_ar;
1765 	Elf *arf;
1766 	Elf_Cmd cmd;
1767 	int title = 0;
1768 	int err = 0;
1769 
1770 	char buf[DATESIZE];
1771 
1772 	cmd = ELF_C_READ;
1773 	while ((arf = elf_begin(fd, cmd, elf_file)) != 0) {
1774 		p_ar = elf_getarhdr(arf);
1775 		if (p_ar == NULL) {
1776 			(void) fprintf(stderr,
1777 			"%s: %s: %s\n", prog_name, filename, elf_errmsg(-1));
1778 			continue;
1779 		}
1780 		if (strcmp(p_ar->ar_name, "/") == 0) {
1781 			if (g_flag)
1782 				ar_sym_read(elf_file, filename);
1783 		} else if (strcmp(p_ar->ar_name, "//") == 0) {
1784 			StringTableList = load_arstring_table(
1785 				StringTableList, fd, arf, p_ar,
1786 				filename);
1787 			cmd = elf_next(arf);
1788 			(void) elf_end(arf);
1789 			continue;
1790 		} else {
1791 			if (a_flag) {
1792 				(void) printf("%s[%s]:\n", filename,
1793 					p_ar->ar_name);
1794 				if (!p_flag && title == 0) {
1795 					if (!v_flag)
1796 						(void) printf(
1797 "\n\n\t\t\t***ARCHIVE HEADER***"
1798 "\n	Date          Uid     Gid    Mode      Size	 Member Name\n\n");
1799 					else
1800 						(void) printf(
1801 "\n\n\t\t\t***ARCHIVE HEADER***"
1802 "\n	Date                   Uid    Gid   Mode     Size     Member Name\n\n");
1803 					title = 1;
1804 				}
1805 				if (!v_flag) {
1806 					(void) printf(
1807 "\t0x%.8lx  %6d  %6d  0%.6ho  0x%.8lx  %-s\n\n",
1808 						p_ar->ar_date,
1809 						(int)p_ar->ar_uid,
1810 						(int)p_ar->ar_gid,
1811 						(int)p_ar->ar_mode,
1812 						p_ar->ar_size,
1813 						p_ar->ar_name);
1814 				} else {
1815 					if ((strftime(buf, DATESIZE,
1816 					    "%b %d %H:%M:%S %Y",
1817 					    localtime(
1818 					    &(p_ar->ar_date)))) == 0) {
1819 						(void) fprintf(stderr,
1820 "%s: %s: don't have enough space to store the date\n", prog_name, filename);
1821 						exit(1);
1822 					}
1823 					(void) printf(
1824 					"\t%s %6d %6d 0%.6ho 0x%.8lx %-s\n\n",
1825 						buf,
1826 						(int)p_ar->ar_uid,
1827 						(int)p_ar->ar_gid,
1828 						(int)p_ar->ar_mode,
1829 						p_ar->ar_size,
1830 						p_ar->ar_name);
1831 				}
1832 			}
1833 		}
1834 		cmd = elf_next(arf);
1835 		(void) elf_end(arf);
1836 	} /* end while */
1837 
1838 	err = elf_errno();
1839 	if (err != 0) {
1840 		(void) fprintf(stderr,
1841 		"%s: %s: %s\n", prog_name, filename, elf_errmsg(err));
1842 	}
1843 }
1844 
1845 /*
1846  * Process member files of an archive.  This function provides
1847  * a loop through an archive equivalent the processing of
1848  * each_file for individual object files.
1849  */
1850 static void
1851 dump_ar_files(int fd, Elf *elf_file, char *filename)
1852 {
1853 	Elf_Arhdr  *p_ar;
1854 	Elf *arf;
1855 	Elf_Cmd cmd;
1856 	Elf_Kind file_type;
1857 	GElf_Ehdr elf_head;
1858 	char *fullname;
1859 
1860 	cmd = ELF_C_READ;
1861 	while ((arf = elf_begin(fd, cmd, elf_file)) != 0) {
1862 		size_t	len;
1863 
1864 		p_ar = elf_getarhdr(arf);
1865 		if (p_ar == NULL) {
1866 			(void) fprintf(stderr,
1867 				"%s: %s: %s\n",
1868 				prog_name, filename, elf_errmsg(-1));
1869 			return;
1870 		}
1871 		if ((strcmp(p_ar->ar_name, "/") == 0) ||
1872 			(strcmp(p_ar->ar_name, "//") == 0)) {
1873 			cmd = elf_next(arf);
1874 			(void) elf_end(arf);
1875 			continue;
1876 		}
1877 
1878 		len = strlen(filename) + strlen(p_ar->ar_name) + 3;
1879 		if ((fullname = malloc(len)) == NULL)
1880 			return;
1881 		(void) snprintf(fullname, len, "%s[%s]", filename,
1882 		    p_ar->ar_name);
1883 		(void) printf("\n%s:\n", fullname);
1884 		file_type = elf_kind(arf);
1885 		if (file_type == ELF_K_ELF) {
1886 			if (dump_elf_header(arf, fullname, &elf_head) == NULL)
1887 				return;
1888 			if (o_flag)
1889 				dump_exec_header(arf,
1890 					(unsigned)elf_head.e_phnum, fullname);
1891 			if (x_flag)
1892 				dump_section_table(arf, &elf_head, fullname);
1893 		} else {
1894 			(void) fprintf(stderr,
1895 				"%s: %s: invalid file type\n",
1896 				prog_name, fullname);
1897 			cmd = elf_next(arf);
1898 			(void) elf_end(arf);
1899 			continue;
1900 		}
1901 
1902 		cmd = elf_next(arf);
1903 		(void) elf_end(arf);
1904 	} /* end while */
1905 }
1906 
1907 /*
1908  * Takes a filename as input.  Test first for a valid version
1909  * of libelf.a and exit on error.  Process each valid file
1910  * or archive given as input on the command line.  Check
1911  * for file type.  If it is an archive, process the archive-
1912  * specific options first, then files within the archive.
1913  * If it is an ELF object file, process it; otherwise
1914  * warn that it is an invalid file type.
1915  * All options except the archive-specific and program
1916  * execution header are processed in the function, dump_section_table.
1917  */
1918 static void
1919 each_file(char *filename)
1920 {
1921 	Elf *elf_file;
1922 	GElf_Ehdr elf_head;
1923 	int fd;
1924 	Elf_Kind   file_type;
1925 
1926 	struct stat buf;
1927 
1928 	Elf_Cmd cmd;
1929 	errno = 0;
1930 
1931 	if (stat(filename, &buf) == -1) {
1932 		int	err = errno;
1933 		(void) fprintf(stderr, "%s: %s: %s", prog_name, filename,
1934 		    strerror(err));
1935 		return;
1936 	}
1937 
1938 	if ((fd = open((filename), O_RDONLY)) == -1) {
1939 		(void) fprintf(stderr, "%s: %s: cannot read\n", prog_name,
1940 		    filename);
1941 		return;
1942 	}
1943 	cmd = ELF_C_READ;
1944 	if ((elf_file = elf_begin(fd, cmd, (Elf *)0)) == NULL) {
1945 		(void) fprintf(stderr, "%s: %s: %s\n", prog_name, filename,
1946 		    elf_errmsg(-1));
1947 		return;
1948 	}
1949 
1950 	file_type = elf_kind(elf_file);
1951 	if (file_type == ELF_K_AR) {
1952 		if (a_flag || g_flag) {
1953 			dump_ar_hdr(fd, elf_file, filename);
1954 			elf_file = elf_begin(fd, cmd, (Elf *)0);
1955 		}
1956 		if (z_flag)
1957 			dump_ar_files(fd, elf_file, filename);
1958 	} else {
1959 		if (file_type == ELF_K_ELF) {
1960 			(void) printf("\n%s:\n", filename);
1961 			if (dump_elf_header(elf_file, filename, &elf_head)) {
1962 				if (o_flag)
1963 					dump_exec_header(elf_file,
1964 					    (unsigned)elf_head.e_phnum,
1965 					    filename);
1966 				if (x_flag)
1967 					dump_section_table(elf_file,
1968 					    &elf_head, filename);
1969 			}
1970 		} else {
1971 			(void) fprintf(stderr, "%s: %s: invalid file type\n",
1972 			    prog_name, filename);
1973 		}
1974 	}
1975 	(void) elf_end(elf_file);
1976 	(void) close(fd);
1977 }
1978 
1979 /*
1980  * Sets up flags for command line options given and then
1981  * calls each_file() to process each file.
1982  */
1983 int
1984 main(int argc, char *argv[], char *envp[])
1985 {
1986 	char *optstr = OPTSTR; /* option string used by getopt() */
1987 	int optchar;
1988 
1989 	/*
1990 	 * Check for a binary that better fits this architecture.
1991 	 */
1992 	(void) conv_check_native(argv, envp);
1993 
1994 	prog_name = argv[0];
1995 
1996 	(void) setlocale(LC_ALL, "");
1997 	while ((optchar = getopt(argc, argv, optstr)) != -1) {
1998 		switch (optchar) {
1999 		case 'a':
2000 			a_flag = 1;
2001 			x_flag = 1;
2002 			break;
2003 		case 'g':
2004 			g_flag = 1;
2005 			x_flag = 1;
2006 			break;
2007 		case 'v':
2008 			v_flag = 1;
2009 			break;
2010 		case 'p':
2011 			p_flag = 1;
2012 			break;
2013 		case 'f':
2014 			f_flag = 1;
2015 			z_flag = 1;
2016 			break;
2017 		case 'o':
2018 			o_flag = 1;
2019 			z_flag = 1;
2020 			break;
2021 		case 'h':
2022 			h_flag = 1;
2023 			x_flag = 1;
2024 			z_flag = 1;
2025 			break;
2026 		case 's':
2027 			s_flag = 1;
2028 			x_flag = 1;
2029 			z_flag = 1;
2030 			break;
2031 		case 'd':
2032 			d_flag = 1;
2033 			x_flag = 1;
2034 			z_flag = 1;
2035 			set_range(optarg, &d_low, &d_hi);
2036 			break;
2037 		case 'n':
2038 			n_flag++;
2039 			x_flag = 1;
2040 			z_flag = 1;
2041 			name = optarg;
2042 			break;
2043 		case 'r':
2044 			r_flag = 1;
2045 			x_flag = 1;
2046 			z_flag = 1;
2047 			break;
2048 		case 't':
2049 			t_flag = 1;
2050 			x_flag = 1;
2051 			z_flag = 1;
2052 			break;
2053 		case 'C':
2054 			C_flag = 1;
2055 			t_flag = 1;
2056 			x_flag = 1;
2057 			z_flag = 1;
2058 			break;
2059 		case 'T':
2060 			T_flag = 1;
2061 			x_flag = 1;
2062 			z_flag = 1;
2063 			set_range(optarg, &T_low, &T_hi);
2064 			break;
2065 		case 'c':
2066 			c_flag = 1;
2067 			x_flag = 1;
2068 			z_flag = 1;
2069 			break;
2070 		case 'L':
2071 			L_flag = 1;
2072 			x_flag = 1;
2073 			z_flag = 1;
2074 			break;
2075 		case 'V':
2076 			V_flag = 1;
2077 			(void) fprintf(stderr, "dump: %s %s\n",
2078 			    (const char *)SGU_PKG,
2079 			    (const char *)SGU_REL);
2080 			break;
2081 		case '?':
2082 			errflag += 1;
2083 			break;
2084 		default:
2085 			break;
2086 		}
2087 	}
2088 
2089 	if (errflag || (optind >= argc) || (!z_flag && !x_flag)) {
2090 		if (!(V_flag && (argc == 2))) {
2091 			usage();
2092 			exit(269);
2093 		}
2094 	}
2095 
2096 	if (elf_version(EV_CURRENT) == EV_NONE) {
2097 		(void) fprintf(stderr, "%s: libelf is out of date\n",
2098 		    prog_name);
2099 		exit(101);
2100 	}
2101 
2102 	while (optind < argc) {
2103 		each_file(argv[optind]);
2104 		optind++;
2105 	}
2106 	return (0);
2107 }
2108