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