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