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