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