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