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