xref: /titanic_41/usr/src/cmd/sgs/dump/common/dump.c (revision 74e20cfe817b82802b16fac8690dadcda76f54f5)
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 2006 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_PLTPAD:
1147 			case DT_MOVETAB:
1148 			case DT_SYMINFO:
1149 			case DT_RELACOUNT:
1150 			case DT_RELCOUNT:
1151 			case DT_VERSYM:
1152 			case DT_VERDEF:
1153 			case DT_VERDEFNUM:
1154 			case DT_VERNEED:
1155 				(void) printf(pdyn_Fmtptr,
1156 					EC_ADDR(p_dyn.d_un.d_ptr));
1157 				break;
1158 
1159 			/*
1160 			 * Items with a string value
1161 			 */
1162 			case DT_NEEDED:
1163 			case DT_SONAME:
1164 			case DT_RPATH:
1165 			case DT_RUNPATH:
1166 			case DT_SUNW_AUXILIARY:
1167 			case DT_SUNW_FILTER:
1168 			case DT_CONFIG:
1169 			case DT_DEPAUDIT:
1170 			case DT_AUDIT:
1171 			case DT_AUXILIARY:
1172 			case DT_USED:
1173 			case DT_FILTER:
1174 				if (v_flag) {	/* Look up the string */
1175 					str = (char *)elf_strptr(elf_file, link,
1176 						p_dyn.d_un.d_ptr);
1177 					if (!(str && *str))
1178 						str = (char *)UNKNOWN;
1179 					(void) printf("%s", str);
1180 				} else {	/* Show the address */
1181 					(void) printf(pdyn_Fmtptr,
1182 						EC_ADDR(p_dyn.d_un.d_ptr));
1183 				}
1184 				break;
1185 
1186 			/*
1187 			 * Items with a literal value
1188 			 */
1189 			case DT_PLTRELSZ:
1190 			case DT_RELASZ:
1191 			case DT_RELAENT:
1192 			case DT_STRSZ:
1193 			case DT_SYMENT:
1194 			case DT_RELSZ:
1195 			case DT_RELENT:
1196 			case DT_PLTREL:
1197 			case DT_BIND_NOW:
1198 			case DT_CHECKSUM:
1199 			case DT_PLTPADSZ:
1200 			case DT_MOVEENT:
1201 			case DT_MOVESZ:
1202 			case DT_SYMINSZ:
1203 			case DT_SYMINENT:
1204 			case DT_VERNEEDNUM:
1205 			case DT_SPARC_REGISTER:
1206 			case DT_SUNW_SYMSZ:
1207 				(void) printf(pdyn_Fmtptr,
1208 					EC_XWORD(p_dyn.d_un.d_val));
1209 				break;
1210 
1211 			/*
1212 			 * Items that are bitmasks
1213 			 */
1214 			case DT_FLAGS:
1215 			case DT_FEATURE_1:
1216 			case DT_POSFLAG_1:
1217 			case DT_FLAGS_1:
1218 				str = NULL;
1219 				if (v_flag) {
1220 					switch (p_dyn.d_tag) {
1221 					case DT_FLAGS:
1222 						str = conv_dyn_flag(
1223 							p_dyn.d_un.d_val,
1224 							DUMP_CONVFMT);
1225 					break;
1226 					case DT_FEATURE_1:
1227 						str = conv_dyn_feature1(
1228 							p_dyn.d_un.d_val,
1229 							DUMP_CONVFMT);
1230 						break;
1231 					case DT_POSFLAG_1:
1232 						str = conv_dyn_posflag1(
1233 							p_dyn.d_un.d_val,
1234 							DUMP_CONVFMT);
1235 						break;
1236 					case DT_FLAGS_1:
1237 						str = conv_dyn_flag1(
1238 							p_dyn.d_un.d_val);
1239 						break;
1240 					}
1241 				}
1242 				if (str) {	/* Show as string */
1243 					(void) printf("%s", str);
1244 				} else {	/* Numeric form */
1245 					(void) printf(pdyn_Fmtptr,
1246 						EC_ADDR(p_dyn.d_un.d_ptr));
1247 				}
1248 				break;
1249 
1250 			/*
1251 			 * Depreciated items with a literal value
1252 			 */
1253 			case DT_DEPRECATED_SPARC_REGISTER:
1254 				(void) printf(pdyn_Fmtptr
1255 					"  (deprecated value)",
1256 					EC_XWORD(p_dyn.d_un.d_val));
1257 				break;
1258 
1259 			/* Ignored items */
1260 			case DT_SYMBOLIC:
1261 				(void) printf("(ignored)");
1262 				break;
1263 			}
1264 			(void) printf("\n");
1265 			(void) gelf_getdyn(dyn_data, ii++, &p_dyn);
1266 		}
1267 	}
1268 
1269 	/*
1270 	 * Check for existence of static shared library information.
1271 	 */
1272 	while (header_num < p_ehdr.e_phnum) {
1273 		(void) gelf_getphdr(elf_file, header_num, &p_phdr);
1274 		if (p_phdr.p_type == PT_SHLIB) {
1275 			while (--lib_scns > 0) {
1276 				if (strcmp(l_scns->scn_name, ".lib") == 0) {
1277 					print_static(l_scns, filename);
1278 				}
1279 				l_scns++;
1280 			}
1281 		}
1282 		header_num++;
1283 	}
1284 #undef	pdyn_Fmtptr
1285 }
1286 
1287 /*
1288  * Print the ELF header.  Input is an ELF file descriptor
1289  * and the filename.  If f_flag is set, the ELF header is
1290  * printed to stdout, otherwise the function returns after
1291  * setting the pointer to the ELF header.  Any values which
1292  * are not known are printed in decimal.  Fields must be updated
1293  * as new values are added.
1294  */
1295 static GElf_Ehdr *
1296 dump_elf_header(Elf *elf_file, char *filename, GElf_Ehdr * elf_head_p)
1297 {
1298 	int class;
1299 	int field;
1300 
1301 	if (gelf_getehdr(elf_file, elf_head_p) == NULL) {
1302 		(void) fprintf(stderr, "%s: %s: %s\n", prog_name, filename,
1303 		    elf_errmsg(-1));
1304 		return (NULL);
1305 	}
1306 
1307 	class = (int)elf_head_p->e_ident[4];
1308 
1309 	if (class == ELFCLASS64)
1310 		field = 21;
1311 	else
1312 		field = 13;
1313 
1314 	if (!f_flag)
1315 		return (elf_head_p);
1316 
1317 	if (!p_flag) {
1318 		(void) printf("\n                    **** ELF HEADER ****\n");
1319 		(void) printf("%-*s%-11s%-*sMachine     Version\n",
1320 		    field, "Class", "Data", field, "Type");
1321 		(void) printf("%-*s%-11s%-*sFlags       Ehsize\n",
1322 		    field, "Entry", "Phoff", field, "Shoff");
1323 		(void) printf("%-*s%-11s%-*sShnum       Shstrndx\n\n",
1324 		    field, "Phentsize", "Phnum", field, "Shentsz");
1325 	}
1326 
1327 	if (!v_flag) {
1328 		(void) printf("%-*d%-11d%-*d%-12d%d\n",
1329 			field, elf_head_p->e_ident[4],
1330 			elf_head_p->e_ident[5],
1331 			field, (int)elf_head_p->e_type,
1332 			(int)elf_head_p->e_machine,
1333 			elf_head_p->e_version);
1334 	} else {
1335 		(void) printf("%-*s", field,
1336 			conv_ehdr_class(class, DUMP_CONVFMT));
1337 		(void) printf("%-11s",
1338 			conv_ehdr_data(elf_head_p->e_ident[5], DUMP_CONVFMT));
1339 		(void) printf("%-*s", field,
1340 			conv_ehdr_type(elf_head_p->e_type, DUMP_CONVFMT));
1341 		(void) printf("%-12s",
1342 			conv_ehdr_mach(elf_head_p->e_machine, DUMP_CONVFMT));
1343 		(void) printf("%s\n",
1344 			conv_ehdr_vers(elf_head_p->e_version, DUMP_CONVFMT));
1345 	}
1346 	(void) printf("%-#*llx%-#11llx%-#*llx%-#12x%#x\n",
1347 		field, EC_ADDR(elf_head_p->e_entry),
1348 		EC_OFF(elf_head_p->e_phoff),
1349 		field, EC_OFF(elf_head_p->e_shoff),
1350 		EC_WORD(elf_head_p->e_flags),
1351 		EC_WORD(elf_head_p->e_ehsize));
1352 	if (!v_flag || (elf_head_p->e_shstrndx != SHN_XINDEX)) {
1353 		(void) printf("%-#*x%-11u%-#*x%-12u%u\n",
1354 			field, EC_WORD(elf_head_p->e_phentsize),
1355 			EC_WORD(elf_head_p->e_phnum),
1356 			field, EC_WORD(elf_head_p->e_shentsize),
1357 			EC_WORD(elf_head_p->e_shnum),
1358 			EC_WORD(elf_head_p->e_shstrndx));
1359 	} else {
1360 		(void) printf("%-#*x%-11u%-#*x%-12uXINDEX\n",
1361 			field, EC_WORD(elf_head_p->e_phentsize),
1362 			EC_WORD(elf_head_p->e_phnum),
1363 			field, EC_WORD(elf_head_p->e_shentsize),
1364 			EC_WORD(elf_head_p->e_shnum));
1365 	}
1366 	if ((elf_head_p->e_shnum == 0) && (elf_head_p->e_shoff > 0)) {
1367 		Elf_Scn		*scn;
1368 		GElf_Shdr	shdr0;
1369 		int		field;
1370 
1371 		if (gelf_getclass(elf_file) == ELFCLASS64)
1372 			field = 21;
1373 		else
1374 			field = 13;
1375 		if (!p_flag) {
1376 			(void) printf("\n	   **** SECTION HEADER[0] "
1377 			    "{Elf Extensions} ****\n");
1378 			(void) printf(
1379 			    "[No]\tType\tFlags\t%-*s %-*s%-*s%sName\n",
1380 			    field, "Addr", field, "Offset", field,
1381 			    "Size(shnum)",
1382 			    /* compatibility:  tab for elf32 */
1383 			    (field == 13) ? "\t" : "  ");
1384 			(void) printf("\tLn(strndx) Info\t%-*s Entsize\n",
1385 			    field, "Adralgn");
1386 		}
1387 		if ((scn = elf_getscn(elf_file, 0)) == NULL) {
1388 			(void) fprintf(stderr,
1389 				"%s: %s: elf_getscn failed: %s\n",
1390 				prog_name, filename, elf_errmsg(-1));
1391 			return (NULL);
1392 		}
1393 		if (gelf_getshdr(scn, &shdr0) == 0) {
1394 			(void) fprintf(stderr,
1395 				"%s: %s: gelf_getshdr: %s\n",
1396 				prog_name, filename, elf_errmsg(-1));
1397 			return (NULL);
1398 		}
1399 		(void) printf("[0]\t%u\t%llu\t", EC_WORD(shdr0.sh_type),
1400 			EC_XWORD(shdr0.sh_flags));
1401 
1402 		(void) printf("%-#*llx %-#*llx%-*llu%s%-*u\n",
1403 			field, EC_ADDR(shdr0.sh_addr),
1404 			field, EC_OFF(shdr0.sh_offset),
1405 			field, EC_XWORD(shdr0.sh_size),
1406 			/* compatibility:  tab for elf32 */
1407 			((field == 13) ? "\t" : "  "),
1408 			field, EC_WORD(shdr0.sh_name));
1409 
1410 		(void) printf("\t%u\t%u\t%-#*llx %-#*llx\n",
1411 			EC_WORD(shdr0.sh_link),
1412 			EC_WORD(shdr0.sh_info),
1413 			field, EC_XWORD(shdr0.sh_addralign),
1414 			field, EC_XWORD(shdr0.sh_entsize));
1415 	}
1416 	(void) printf("\n");
1417 
1418 	return (elf_head_p);
1419 }
1420 
1421 /*
1422  * Print section contents.  Input is an ELF file descriptor,
1423  * the ELF header, the SCNTAB structure,
1424  * the number of symbols, and the filename.
1425  * The number of sections,
1426  * and the offset into the SCNTAB structure will be
1427  * set in dump_section if d_flag or n_flag are set.
1428  * If v_flag is set, sections which can be interpreted will
1429  * be interpreted, otherwise raw data will be output in hexidecimal.
1430  */
1431 static void
1432 print_section(Elf *elf_file,
1433 	GElf_Ehdr *p_ehdr, SCNTAB *p, int num_scns, char *filename)
1434 {
1435 	unsigned char    *p_sec;
1436 	int	i;
1437 	size_t	size;
1438 
1439 	for (i = 0; i < num_scns; i++, p++) {
1440 		GElf_Shdr shdr;
1441 
1442 		size = 0;
1443 		if (s_flag && !v_flag)
1444 			p_sec = (unsigned char *)get_rawscn(p->p_sd, &size);
1445 		else
1446 			p_sec = (unsigned char *)get_scndata(p->p_sd, &size);
1447 
1448 		if ((gelf_getshdr(p->p_sd, &shdr) != NULL) &&
1449 		    (shdr.sh_type == SHT_NOBITS)) {
1450 			continue;
1451 		}
1452 		if (s_flag && !v_flag) {
1453 			(void) printf("\n%s:\n", p->scn_name);
1454 			print_rawdata(p_sec, size);
1455 			continue;
1456 		}
1457 		if (shdr.sh_type == SHT_SYMTAB) {
1458 			dump_symbol_table(elf_file, p, filename);
1459 			continue;
1460 		}
1461 		if (shdr.sh_type == SHT_DYNSYM) {
1462 			dump_symbol_table(elf_file, p, filename);
1463 			continue;
1464 		}
1465 		if (shdr.sh_type == SHT_STRTAB) {
1466 			dump_string_table(p, 1);
1467 			continue;
1468 		}
1469 		if (shdr.sh_type == SHT_RELA) {
1470 			dump_reloc_table(elf_file, p_ehdr, p, 1, filename);
1471 			continue;
1472 		}
1473 		if (shdr.sh_type == SHT_REL) {
1474 			dump_reloc_table(elf_file, p_ehdr, p, 1, filename);
1475 			continue;
1476 		}
1477 		if (shdr.sh_type == SHT_DYNAMIC) {
1478 			dump_dynamic(elf_file, p, 1, filename);
1479 			continue;
1480 		}
1481 
1482 		(void) printf("\n%s:\n", p->scn_name);
1483 		print_rawdata(p_sec, size);
1484 	}
1485 	(void) printf("\n");
1486 }
1487 
1488 /*
1489  * Print section contents. This function does not print the contents
1490  * of the sections but sets up the parameters and then calls
1491  * print_section to print the contents.  Calling another function to print
1492  * the contents allows both -d and -n to work correctly
1493  * simultaneously. Input is an ELF file descriptor, the ELF header,
1494  * the SCNTAB structure, the number of sections, and the filename.
1495  * Set the range of sections if d_flag, and set section name if
1496  * n_flag.
1497  */
1498 static void
1499 dump_section(Elf *elf_file,
1500 	GElf_Ehdr *p_ehdr, SCNTAB *s, int num_scns, char *filename)
1501 {
1502 	SCNTAB *n_range, *d_range; /* for use with -n and -d modifiers */
1503 	int i;
1504 	int found_it = 0;  /* for use with -n section_name */
1505 
1506 	if (n_flag) {
1507 		n_range = s;
1508 
1509 		for (i = 0; i < num_scns; i++, n_range++) {
1510 			if ((strcmp(name, n_range->scn_name)) != 0)
1511 				continue;
1512 			else {
1513 				found_it = 1;
1514 				print_section(elf_file, p_ehdr,
1515 					n_range, 1, filename);
1516 			}
1517 		}
1518 
1519 		if (!found_it) {
1520 			(void) fprintf(stderr, "%s: %s: %s not found\n",
1521 				prog_name, filename, name);
1522 		}
1523 	} /* end n_flag */
1524 
1525 	if (d_flag) {
1526 		d_range = s;
1527 		d_num = check_range(d_low, d_hi, num_scns, filename);
1528 		if (d_num < 0)
1529 			return;
1530 		d_range += d_low - 1;
1531 
1532 		print_section(elf_file, p_ehdr, d_range, d_num, filename);
1533 	}	/* end d_flag */
1534 
1535 	if (!n_flag && !d_flag)
1536 		print_section(elf_file, p_ehdr, s, num_scns, filename);
1537 }
1538 
1539 /*
1540  * Print the section header table. This function does not print the contents
1541  * of the section headers but sets up the parameters and then calls
1542  * print_shdr to print the contents.  Calling another function to print
1543  * the contents allows both -d and -n to work correctly
1544  * simultaneously.  Input is the SCNTAB structure,
1545  * the number of sections from the ELF header, and the filename.
1546  * Set the range of section headers to print if d_flag, and set
1547  * name of section header to print if n_flag.
1548  */
1549 static void
1550 dump_shdr(Elf *elf_file, SCNTAB *s, int num_scns, char *filename)
1551 {
1552 
1553 	SCNTAB *n_range, *d_range;	/* for use with -n and -d modifiers */
1554 	int field;
1555 	int i;
1556 	int found_it = 0;  /* for use with -n section_name */
1557 
1558 	if (gelf_getclass(elf_file) == ELFCLASS64)
1559 		field = 21;
1560 	else
1561 		field = 13;
1562 
1563 	if (!p_flag) {
1564 		(void) printf("\n	   **** SECTION HEADER TABLE ****\n");
1565 		(void) printf("[No]\tType\tFlags\t%-*s %-*s %-*s%sName\n",
1566 		    field, "Addr", field, "Offset", field, "Size",
1567 		    /* compatibility:  tab for elf32 */
1568 		    (field == 13) ? "\t" : "  ");
1569 		(void) printf("\tLink\tInfo\t%-*s Entsize\n\n",
1570 		    field, "Adralgn");
1571 	}
1572 
1573 	if (n_flag) {
1574 		n_range = s;
1575 
1576 		for (i = 1; i <= num_scns; i++, n_range++) {
1577 			if ((strcmp(name, n_range->scn_name)) != 0)
1578 				continue;
1579 			else {
1580 				found_it = 1;
1581 				print_shdr(elf_file, n_range, 1, i);
1582 			}
1583 		}
1584 
1585 		if (!found_it) {
1586 			(void) fprintf(stderr, "%s: %s: %s not found\n",
1587 				prog_name, filename, name);
1588 		}
1589 	} /* end n_flag */
1590 
1591 	if (d_flag) {
1592 		d_range = s;
1593 		d_num = check_range(d_low, d_hi, num_scns, filename);
1594 		if (d_num < 0)
1595 			return;
1596 		d_range += d_low - 1;
1597 
1598 		print_shdr(elf_file, d_range, d_num, d_low);
1599 	}	/* end d_flag */
1600 
1601 	if (!n_flag && !d_flag)
1602 		print_shdr(elf_file, s, num_scns, 1);
1603 }
1604 
1605 /*
1606  * Process all of the command line options (except
1607  * for -a, -g, -f, and -o).  All of the options processed
1608  * by this function require the presence of the section
1609  * header table and will not be processed if it is not present.
1610  * Set up a buffer containing section name, section header,
1611  * and section descriptor for each section in the file.  This
1612  * structure is used to avoid duplicate calls to libelf functions.
1613  * Structure members for the symbol table, the debugging information,
1614  * and the line number information are global.  All of the
1615  * rest are local.
1616  */
1617 static void
1618 dump_section_table(Elf *elf_file, GElf_Ehdr *elf_head_p, char *filename)
1619 {
1620 
1621 	static SCNTAB	*buffer, *p_scns;
1622 	Elf_Scn		*scn = 0;
1623 	char		*s_name = NULL;
1624 	int		found = 0;
1625 	unsigned int	num_scns;
1626 	size_t		shstrndx;
1627 	size_t		shnum;
1628 
1629 
1630 	if (elf_getshnum(elf_file, &shnum) == 0) {
1631 		(void) fprintf(stderr,
1632 			"%s: %s: elf_getshnum failed: %s\n",
1633 			prog_name, filename, elf_errmsg(-1));
1634 		return;
1635 	}
1636 	if (elf_getshstrndx(elf_file, &shstrndx) == 0) {
1637 		(void) fprintf(stderr,
1638 			"%s: %s: elf_getshstrndx failed: %s\n",
1639 			prog_name, filename, elf_errmsg(-1));
1640 		return;
1641 	}
1642 
1643 	if ((buffer = calloc(shnum, sizeof (SCNTAB))) == NULL) {
1644 		(void) fprintf(stderr, "%s: %s: cannot calloc space\n",
1645 			prog_name, filename);
1646 		return;
1647 	}
1648 	/* LINTED */
1649 	num_scns = (int)shnum - 1;
1650 
1651 	p_symtab = (SCNTAB *)0;
1652 	p_dynsym = (SCNTAB *)0;
1653 	p_scns = buffer;
1654 	p_head_scns = buffer;
1655 
1656 	while ((scn = elf_nextscn(elf_file, scn)) != 0) {
1657 		if ((gelf_getshdr(scn, &buffer->p_shdr)) == 0) {
1658 			(void) fprintf(stderr,
1659 			"%s: %s: %s\n", prog_name, filename, elf_errmsg(-1));
1660 			return;
1661 		}
1662 		s_name = (char *)elf_strptr(elf_file,
1663 			shstrndx, buffer->p_shdr.sh_name);
1664 		buffer->scn_name = s_name ? s_name : (char *)UNKNOWN;
1665 		buffer->p_sd   =  scn;
1666 
1667 		if (buffer->p_shdr.sh_type == SHT_SYMTAB) {
1668 			found += 1;
1669 			p_symtab = buffer;
1670 		}
1671 		if (buffer->p_shdr.sh_type == SHT_DYNSYM)
1672 			p_dynsym = buffer;
1673 		buffer++;
1674 	}
1675 
1676 	/*
1677 	 * These functions depend upon the presence of the section header table
1678 	 * and will not be invoked in its absence
1679 	 */
1680 	if (h_flag) {
1681 		dump_shdr(elf_file, p_scns, num_scns, filename);
1682 	}
1683 	if (p_symtab && (t_flag || T_flag)) {
1684 		dump_symbol_table(elf_file, p_symtab, filename);
1685 	}
1686 	if (c_flag) {
1687 		dump_string_table(p_scns, num_scns);
1688 	}
1689 	if (r_flag) {
1690 		dump_reloc_table(elf_file, elf_head_p,
1691 			p_scns, num_scns, filename);
1692 	}
1693 	if (L_flag) {
1694 		dump_dynamic(elf_file, p_scns, num_scns, filename);
1695 	}
1696 	if (s_flag) {
1697 		dump_section(elf_file, elf_head_p, p_scns,
1698 			num_scns, filename);
1699 	}
1700 }
1701 
1702 /*
1703  * Load the archive string table(s) (for extended-length strings)
1704  * into an in-core table/list
1705  */
1706 static struct stab_list_s *
1707 load_arstring_table(struct stab_list_s *STabList,
1708 	int fd, Elf *elf_file, Elf_Arhdr *p_ar, char *filename)
1709 {
1710 	off_t here;
1711 	struct stab_list_s *STL_entry, *STL_next;
1712 
1713 	if (p_ar) {
1714 		STL_entry = malloc(sizeof (struct stab_list_s));
1715 		STL_entry->next    = 0;
1716 		STL_entry->strings = 0;
1717 		STL_entry->size    = 0;
1718 
1719 		if (!STabList)
1720 			STabList = STL_entry;
1721 		else {
1722 			STL_next = STabList;
1723 			while (STL_next->next != (void *)0)
1724 				STL_next = STL_next->next;
1725 			STL_next->next = STL_entry;
1726 		}
1727 
1728 		STL_entry->size    = p_ar->ar_size;
1729 		STL_entry->strings = malloc(p_ar->ar_size);
1730 		here = elf_getbase(elf_file);
1731 		if ((lseek(fd, here, 0)) != here) {
1732 			(void) fprintf(stderr,
1733 			"%s: %s: could not lseek\n", prog_name, filename);
1734 		}
1735 
1736 		if ((read(fd, STL_entry->strings, p_ar->ar_size)) == -1) {
1737 			(void) fprintf(stderr,
1738 			"%s: %s: could not read\n", prog_name, filename);
1739 		}
1740 	}
1741 	return (STabList);
1742 }
1743 
1744 /*
1745  * Print the archive header for each member of an archive.
1746  * Also call ar_sym_read to print the symbols in the
1747  * archive symbol table if g_flag.  Input is a file descriptor,
1748  * an ELF file descriptor, and the filename.  Putting the call
1749  * to dump the archive symbol table in this function is more
1750  * efficient since it is necessary to examine the archive member
1751  * name in the archive header to determine which member is the
1752  * symbol table.
1753  */
1754 static void
1755 dump_ar_hdr(int fd, Elf *elf_file, char *filename)
1756 {
1757 	extern int v_flag, g_flag, a_flag, p_flag;
1758 	Elf_Arhdr  *p_ar;
1759 	Elf *arf;
1760 	Elf_Cmd cmd;
1761 	int title = 0;
1762 	int err = 0;
1763 
1764 	char buf[DATESIZE];
1765 
1766 	cmd = ELF_C_READ;
1767 	while ((arf = elf_begin(fd, cmd, elf_file)) != 0) {
1768 		p_ar = elf_getarhdr(arf);
1769 		if (p_ar == NULL) {
1770 			(void) fprintf(stderr,
1771 			"%s: %s: %s\n", prog_name, filename, elf_errmsg(-1));
1772 			continue;
1773 		}
1774 		if (strcmp(p_ar->ar_name, "/") == 0) {
1775 			if (g_flag)
1776 				ar_sym_read(elf_file, filename);
1777 		} else if (strcmp(p_ar->ar_name, "//") == 0) {
1778 			StringTableList = load_arstring_table(
1779 				StringTableList, fd, arf, p_ar,
1780 				filename);
1781 			cmd = elf_next(arf);
1782 			(void) elf_end(arf);
1783 			continue;
1784 		} else {
1785 			if (a_flag) {
1786 				(void) printf("%s[%s]:\n", filename,
1787 					p_ar->ar_name);
1788 				if (!p_flag && title == 0) {
1789 					if (!v_flag)
1790 						(void) printf(
1791 "\n\n\t\t\t***ARCHIVE HEADER***"
1792 "\n	Date          Uid     Gid    Mode      Size	 Member Name\n\n");
1793 					else
1794 						(void) printf(
1795 "\n\n\t\t\t***ARCHIVE HEADER***"
1796 "\n	Date                   Uid    Gid   Mode     Size     Member Name\n\n");
1797 					title = 1;
1798 				}
1799 				if (!v_flag) {
1800 					(void) printf(
1801 "\t0x%.8lx  %6d  %6d  0%.6ho  0x%.8lx  %-s\n\n",
1802 						p_ar->ar_date,
1803 						(int)p_ar->ar_uid,
1804 						(int)p_ar->ar_gid,
1805 						(int)p_ar->ar_mode,
1806 						p_ar->ar_size,
1807 						p_ar->ar_name);
1808 				} else {
1809 					if ((strftime(buf, DATESIZE,
1810 					    "%b %d %H:%M:%S %Y",
1811 					    localtime(
1812 					    &(p_ar->ar_date)))) == 0) {
1813 						(void) fprintf(stderr,
1814 "%s: %s: don't have enough space to store the date\n", prog_name, filename);
1815 						exit(1);
1816 					}
1817 					(void) printf(
1818 					"\t%s %6d %6d 0%.6ho 0x%.8lx %-s\n\n",
1819 						buf,
1820 						(int)p_ar->ar_uid,
1821 						(int)p_ar->ar_gid,
1822 						(int)p_ar->ar_mode,
1823 						p_ar->ar_size,
1824 						p_ar->ar_name);
1825 				}
1826 			}
1827 		}
1828 		cmd = elf_next(arf);
1829 		(void) elf_end(arf);
1830 	} /* end while */
1831 
1832 	err = elf_errno();
1833 	if (err != 0) {
1834 		(void) fprintf(stderr,
1835 		"%s: %s: %s\n", prog_name, filename, elf_errmsg(err));
1836 	}
1837 }
1838 
1839 /*
1840  * Process member files of an archive.  This function provides
1841  * a loop through an archive equivalent the processing of
1842  * each_file for individual object files.
1843  */
1844 static void
1845 dump_ar_files(int fd, Elf *elf_file, char *filename)
1846 {
1847 	Elf_Arhdr  *p_ar;
1848 	Elf *arf;
1849 	Elf_Cmd cmd;
1850 	Elf_Kind file_type;
1851 	GElf_Ehdr elf_head;
1852 	char *fullname;
1853 
1854 	cmd = ELF_C_READ;
1855 	while ((arf = elf_begin(fd, cmd, elf_file)) != 0) {
1856 		size_t	len;
1857 
1858 		p_ar = elf_getarhdr(arf);
1859 		if (p_ar == NULL) {
1860 			(void) fprintf(stderr,
1861 				"%s: %s: %s\n",
1862 				prog_name, filename, elf_errmsg(-1));
1863 			return;
1864 		}
1865 		if ((strcmp(p_ar->ar_name, "/") == 0) ||
1866 			(strcmp(p_ar->ar_name, "//") == 0)) {
1867 			cmd = elf_next(arf);
1868 			(void) elf_end(arf);
1869 			continue;
1870 		}
1871 
1872 		len = strlen(filename) + strlen(p_ar->ar_name) + 3;
1873 		if ((fullname = malloc(len)) == NULL)
1874 			return;
1875 		(void) snprintf(fullname, len, "%s[%s]", filename,
1876 		    p_ar->ar_name);
1877 		(void) printf("\n%s:\n", fullname);
1878 		file_type = elf_kind(arf);
1879 		if (file_type == ELF_K_ELF) {
1880 			if (dump_elf_header(arf, fullname, &elf_head) == NULL)
1881 				return;
1882 			if (o_flag)
1883 				dump_exec_header(arf,
1884 					(unsigned)elf_head.e_phnum, fullname);
1885 			if (x_flag)
1886 				dump_section_table(arf, &elf_head, fullname);
1887 		} else {
1888 			(void) fprintf(stderr,
1889 				"%s: %s: invalid file type\n",
1890 				prog_name, fullname);
1891 			cmd = elf_next(arf);
1892 			(void) elf_end(arf);
1893 			continue;
1894 		}
1895 
1896 		cmd = elf_next(arf);
1897 		(void) elf_end(arf);
1898 	} /* end while */
1899 }
1900 
1901 /*
1902  * Takes a filename as input.  Test first for a valid version
1903  * of libelf.a and exit on error.  Process each valid file
1904  * or archive given as input on the command line.  Check
1905  * for file type.  If it is an archive, process the archive-
1906  * specific options first, then files within the archive.
1907  * If it is an ELF object file, process it; otherwise
1908  * warn that it is an invalid file type.
1909  * All options except the archive-specific and program
1910  * execution header are processed in the function, dump_section_table.
1911  */
1912 static void
1913 each_file(char *filename)
1914 {
1915 	Elf *elf_file;
1916 	GElf_Ehdr elf_head;
1917 	int fd;
1918 	Elf_Kind   file_type;
1919 
1920 	struct stat buf;
1921 
1922 	Elf_Cmd cmd;
1923 	errno = 0;
1924 
1925 	if (stat(filename, &buf) == -1) {
1926 		int	err = errno;
1927 		(void) fprintf(stderr, "%s: %s: %s", prog_name, filename,
1928 		    strerror(err));
1929 		return;
1930 	}
1931 
1932 	if ((fd = open((filename), O_RDONLY)) == -1) {
1933 		(void) fprintf(stderr, "%s: %s: cannot read\n", prog_name,
1934 		    filename);
1935 		return;
1936 	}
1937 	cmd = ELF_C_READ;
1938 	if ((elf_file = elf_begin(fd, cmd, (Elf *)0)) == NULL) {
1939 		(void) fprintf(stderr, "%s: %s: %s\n", prog_name, filename,
1940 		    elf_errmsg(-1));
1941 		return;
1942 	}
1943 
1944 	file_type = elf_kind(elf_file);
1945 	if (file_type == ELF_K_AR) {
1946 		if (a_flag || g_flag) {
1947 			dump_ar_hdr(fd, elf_file, filename);
1948 			elf_file = elf_begin(fd, cmd, (Elf *)0);
1949 		}
1950 		if (z_flag)
1951 			dump_ar_files(fd, elf_file, filename);
1952 	} else {
1953 		if (file_type == ELF_K_ELF) {
1954 			(void) printf("\n%s:\n", filename);
1955 			if (dump_elf_header(elf_file, filename, &elf_head)) {
1956 				if (o_flag)
1957 					dump_exec_header(elf_file,
1958 					    (unsigned)elf_head.e_phnum,
1959 					    filename);
1960 				if (x_flag)
1961 					dump_section_table(elf_file,
1962 					    &elf_head, filename);
1963 			}
1964 		} else {
1965 			(void) fprintf(stderr, "%s: %s: invalid file type\n",
1966 			    prog_name, filename);
1967 		}
1968 	}
1969 	(void) elf_end(elf_file);
1970 	(void) close(fd);
1971 }
1972 
1973 /*
1974  * Sets up flags for command line options given and then
1975  * calls each_file() to process each file.
1976  */
1977 int
1978 main(int argc, char *argv[], char *envp[])
1979 {
1980 	char *optstr = OPTSTR; /* option string used by getopt() */
1981 	int optchar;
1982 
1983 	/*
1984 	 * Check for a binary that better fits this architecture.
1985 	 */
1986 	(void) conv_check_native(argv, envp);
1987 
1988 	prog_name = argv[0];
1989 
1990 	(void) setlocale(LC_ALL, "");
1991 	while ((optchar = getopt(argc, argv, optstr)) != -1) {
1992 		switch (optchar) {
1993 		case 'a':
1994 			a_flag = 1;
1995 			x_flag = 1;
1996 			break;
1997 		case 'g':
1998 			g_flag = 1;
1999 			x_flag = 1;
2000 			break;
2001 		case 'v':
2002 			v_flag = 1;
2003 			break;
2004 		case 'p':
2005 			p_flag = 1;
2006 			break;
2007 		case 'f':
2008 			f_flag = 1;
2009 			z_flag = 1;
2010 			break;
2011 		case 'o':
2012 			o_flag = 1;
2013 			z_flag = 1;
2014 			break;
2015 		case 'h':
2016 			h_flag = 1;
2017 			x_flag = 1;
2018 			z_flag = 1;
2019 			break;
2020 		case 's':
2021 			s_flag = 1;
2022 			x_flag = 1;
2023 			z_flag = 1;
2024 			break;
2025 		case 'd':
2026 			d_flag = 1;
2027 			x_flag = 1;
2028 			z_flag = 1;
2029 			set_range(optarg, &d_low, &d_hi);
2030 			break;
2031 		case 'n':
2032 			n_flag++;
2033 			x_flag = 1;
2034 			z_flag = 1;
2035 			name = optarg;
2036 			break;
2037 		case 'r':
2038 			r_flag = 1;
2039 			x_flag = 1;
2040 			z_flag = 1;
2041 			break;
2042 		case 't':
2043 			t_flag = 1;
2044 			x_flag = 1;
2045 			z_flag = 1;
2046 			break;
2047 		case 'C':
2048 			C_flag = 1;
2049 			t_flag = 1;
2050 			x_flag = 1;
2051 			z_flag = 1;
2052 			break;
2053 		case 'T':
2054 			T_flag = 1;
2055 			x_flag = 1;
2056 			z_flag = 1;
2057 			set_range(optarg, &T_low, &T_hi);
2058 			break;
2059 		case 'c':
2060 			c_flag = 1;
2061 			x_flag = 1;
2062 			z_flag = 1;
2063 			break;
2064 		case 'L':
2065 			L_flag = 1;
2066 			x_flag = 1;
2067 			z_flag = 1;
2068 			break;
2069 		case 'V':
2070 			V_flag = 1;
2071 			(void) fprintf(stderr, "dump: %s %s\n",
2072 			    (const char *)SGU_PKG,
2073 			    (const char *)SGU_REL);
2074 			break;
2075 		case '?':
2076 			errflag += 1;
2077 			break;
2078 		default:
2079 			break;
2080 		}
2081 	}
2082 
2083 	if (errflag || (optind >= argc) || (!z_flag && !x_flag)) {
2084 		if (!(V_flag && (argc == 2))) {
2085 			usage();
2086 			exit(269);
2087 		}
2088 	}
2089 
2090 	if (elf_version(EV_CURRENT) == EV_NONE) {
2091 		(void) fprintf(stderr, "%s: libelf is out of date\n",
2092 		    prog_name);
2093 		exit(101);
2094 	}
2095 
2096 	while (optind < argc) {
2097 		each_file(argv[optind]);
2098 		optind++;
2099 	}
2100 	return (0);
2101 }
2102