1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright (c) 2000-2001 by Sun Microsystems, Inc.
28*0Sstevel@tonic-gate  * All rights reserved.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include	<stdio.h>
34*0Sstevel@tonic-gate #include	<stdlib.h>
35*0Sstevel@tonic-gate #include	<unistd.h>
36*0Sstevel@tonic-gate #include	<string.h>
37*0Sstevel@tonic-gate #include	<libelf.h>
38*0Sstevel@tonic-gate #include	<limits.h>
39*0Sstevel@tonic-gate #include	<sys/elf_M32.h>
40*0Sstevel@tonic-gate #include	<sys/elf_386.h>
41*0Sstevel@tonic-gate #include	<sys/elf_SPARC.h>
42*0Sstevel@tonic-gate #include	"dump.h"
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate extern int	p_flag;
45*0Sstevel@tonic-gate extern char *	prog_name;
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate /*
49*0Sstevel@tonic-gate  * Print the symbols in the archive symbol table.
50*0Sstevel@tonic-gate  */
51*0Sstevel@tonic-gate void
52*0Sstevel@tonic-gate ar_sym_read(Elf * elf, char * filename)
53*0Sstevel@tonic-gate {
54*0Sstevel@tonic-gate 	Elf_Arsym *	arsym;
55*0Sstevel@tonic-gate 	size_t		cnt, ptr;
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate 	if ((arsym = elf_getarsym(elf, &ptr)) == NULL) {
58*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: no archive symbol table\n",
59*0Sstevel@tonic-gate 			prog_name, filename);
60*0Sstevel@tonic-gate 		return;
61*0Sstevel@tonic-gate 	}
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate 	(void) printf("%s:\n", filename);
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 	if (!p_flag) {
66*0Sstevel@tonic-gate 		(void) printf("     **** ARCHIVE SYMBOL TABLE ****\n");
67*0Sstevel@tonic-gate 		(void) printf("%-8s %s\n\n", "Offset", "Name");
68*0Sstevel@tonic-gate 	}
69*0Sstevel@tonic-gate 	for (cnt = 0; cnt < ptr; cnt++, arsym++) {
70*0Sstevel@tonic-gate 		if (arsym->as_off) {
71*0Sstevel@tonic-gate 			/* LINTED */
72*0Sstevel@tonic-gate 			(void) printf("%-8.8x %s\n", (int)arsym->as_off,
73*0Sstevel@tonic-gate 			    (arsym->as_name ? arsym->as_name : ""));
74*0Sstevel@tonic-gate 		}
75*0Sstevel@tonic-gate 	}
76*0Sstevel@tonic-gate }
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate /*
79*0Sstevel@tonic-gate  * Print the program execution header.  Input is an opened ELF object file, the
80*0Sstevel@tonic-gate  * number of structure instances in the header as recorded in the ELF header,
81*0Sstevel@tonic-gate  * and the filename.
82*0Sstevel@tonic-gate  */
83*0Sstevel@tonic-gate void
84*0Sstevel@tonic-gate dump_exec_header(Elf *elf_file, unsigned nseg, char *filename)
85*0Sstevel@tonic-gate {
86*0Sstevel@tonic-gate 	GElf_Ehdr ehdr;
87*0Sstevel@tonic-gate 	GElf_Phdr p_phdr;
88*0Sstevel@tonic-gate 	int counter;
89*0Sstevel@tonic-gate 	int field;
90*0Sstevel@tonic-gate 	extern int v_flag, p_flag;
91*0Sstevel@tonic-gate 	extern char *prog_name;
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	if (gelf_getclass(elf_file) == ELFCLASS64)
94*0Sstevel@tonic-gate 		field = 16;
95*0Sstevel@tonic-gate 	else
96*0Sstevel@tonic-gate 		field = 12;
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 	if (!p_flag) {
99*0Sstevel@tonic-gate 		(void) printf(" ***** PROGRAM EXECUTION HEADER *****\n");
100*0Sstevel@tonic-gate 		(void) printf("%-*s%-*s%-*s%s\n",
101*0Sstevel@tonic-gate 		    field, "Type", field, "Offset",
102*0Sstevel@tonic-gate 		    field, "Vaddr", "Paddr");
103*0Sstevel@tonic-gate 		(void) printf("%-*s%-*s%-*s%s\n\n",
104*0Sstevel@tonic-gate 		    field, "Filesz", field, "Memsz",
105*0Sstevel@tonic-gate 		    field, "Flags", "Align");
106*0Sstevel@tonic-gate 	}
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 	if ((gelf_getehdr(elf_file, &ehdr) == 0) || (ehdr.e_phnum == 0)) {
109*0Sstevel@tonic-gate 		return;
110*0Sstevel@tonic-gate 	}
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 	for (counter = 0; counter < nseg; counter++) {
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 		if (gelf_getphdr(elf_file, counter, &p_phdr) == 0) {
115*0Sstevel@tonic-gate 			(void) fprintf(stderr,
116*0Sstevel@tonic-gate 			"%s: %s: premature EOF on program exec header\n",
117*0Sstevel@tonic-gate 				prog_name, filename);
118*0Sstevel@tonic-gate 			return;
119*0Sstevel@tonic-gate 		}
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 		if (!v_flag) {
122*0Sstevel@tonic-gate 			(void) printf(
123*0Sstevel@tonic-gate 	"%-*d%-#*llx%-#*llx%-#*llx\n%-#*llx%-#*llx%-*u%-#*llx\n\n",
124*0Sstevel@tonic-gate 				field, EC_WORD(p_phdr.p_type),
125*0Sstevel@tonic-gate 				field, EC_OFF(p_phdr.p_offset),
126*0Sstevel@tonic-gate 				field, EC_ADDR(p_phdr.p_vaddr),
127*0Sstevel@tonic-gate 				field, EC_ADDR(p_phdr.p_paddr),
128*0Sstevel@tonic-gate 				field, EC_XWORD(p_phdr.p_filesz),
129*0Sstevel@tonic-gate 				field, EC_XWORD(p_phdr.p_memsz),
130*0Sstevel@tonic-gate 				field, EC_WORD(p_phdr.p_flags),
131*0Sstevel@tonic-gate 				field, EC_XWORD(p_phdr.p_align));
132*0Sstevel@tonic-gate 		} else {
133*0Sstevel@tonic-gate 			switch (p_phdr.p_type) {
134*0Sstevel@tonic-gate 			case PT_NULL:
135*0Sstevel@tonic-gate 				(void) printf("%-*s", field, "NULL");
136*0Sstevel@tonic-gate 				break;
137*0Sstevel@tonic-gate 			case PT_LOAD:
138*0Sstevel@tonic-gate 				(void) printf("%-*s", field, "LOAD");
139*0Sstevel@tonic-gate 				break;
140*0Sstevel@tonic-gate 			case PT_DYNAMIC:
141*0Sstevel@tonic-gate 				(void) printf("%-*s", field, "DYN");
142*0Sstevel@tonic-gate 				break;
143*0Sstevel@tonic-gate 			case PT_INTERP:
144*0Sstevel@tonic-gate 				(void) printf("%-*s", field, "INTERP");
145*0Sstevel@tonic-gate 				break;
146*0Sstevel@tonic-gate 			case PT_NOTE:
147*0Sstevel@tonic-gate 				(void) printf("%-*s", field, "NOTE");
148*0Sstevel@tonic-gate 				break;
149*0Sstevel@tonic-gate 			case PT_SHLIB:
150*0Sstevel@tonic-gate 				(void) printf("%-*s", field, "SHLIB");
151*0Sstevel@tonic-gate 				break;
152*0Sstevel@tonic-gate 			case PT_PHDR:
153*0Sstevel@tonic-gate 				(void) printf("%-*s", field, "PHDR");
154*0Sstevel@tonic-gate 				break;
155*0Sstevel@tonic-gate 			case PT_TLS:
156*0Sstevel@tonic-gate 				(void) printf("%-*s", field, "TLS");
157*0Sstevel@tonic-gate 				break;
158*0Sstevel@tonic-gate 			case PT_SUNWBSS:
159*0Sstevel@tonic-gate 				(void) printf("%-*s", field, "SUNWBSS");
160*0Sstevel@tonic-gate 				break;
161*0Sstevel@tonic-gate 			case PT_SUNWSTACK:
162*0Sstevel@tonic-gate 				(void) printf("%-*s", field, "SUNWSTACK");
163*0Sstevel@tonic-gate 				break;
164*0Sstevel@tonic-gate 			default:
165*0Sstevel@tonic-gate 				(void) printf("%-*d", field,
166*0Sstevel@tonic-gate 					(int)p_phdr.p_type);
167*0Sstevel@tonic-gate 				break;
168*0Sstevel@tonic-gate 			}
169*0Sstevel@tonic-gate 			(void) printf(
170*0Sstevel@tonic-gate 				"%-#*llx%-#*llx%-#*llx\n%-#*llx%-#*llx",
171*0Sstevel@tonic-gate 				field, EC_OFF(p_phdr.p_offset),
172*0Sstevel@tonic-gate 				field, EC_ADDR(p_phdr.p_vaddr),
173*0Sstevel@tonic-gate 				field, EC_ADDR(p_phdr.p_paddr),
174*0Sstevel@tonic-gate 				field, EC_XWORD(p_phdr.p_filesz),
175*0Sstevel@tonic-gate 				field, EC_XWORD(p_phdr.p_memsz));
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 			switch (p_phdr.p_flags) {
178*0Sstevel@tonic-gate 			case 0: (void) printf("%-*s", field, "---"); break;
179*0Sstevel@tonic-gate 			case PF_X:
180*0Sstevel@tonic-gate 				(void) printf("%-*s", field, "--x");
181*0Sstevel@tonic-gate 				break;
182*0Sstevel@tonic-gate 			case PF_W:
183*0Sstevel@tonic-gate 				(void) printf("%-*s", field, "-w-");
184*0Sstevel@tonic-gate 				break;
185*0Sstevel@tonic-gate 			case PF_W+PF_X:
186*0Sstevel@tonic-gate 				(void) printf("%-*s", field, "-wx");
187*0Sstevel@tonic-gate 				break;
188*0Sstevel@tonic-gate 			case PF_R:
189*0Sstevel@tonic-gate 				(void) printf("%-*s", field, "r--");
190*0Sstevel@tonic-gate 				break;
191*0Sstevel@tonic-gate 			case PF_R+PF_X:
192*0Sstevel@tonic-gate 				(void) printf("%-*s", field, "r-x");
193*0Sstevel@tonic-gate 				break;
194*0Sstevel@tonic-gate 			case PF_R+PF_W:
195*0Sstevel@tonic-gate 				(void) printf("%-*s", field, "rw-");
196*0Sstevel@tonic-gate 				break;
197*0Sstevel@tonic-gate 			case PF_R+PF_W+PF_X:
198*0Sstevel@tonic-gate 				(void) printf("%-*s", field, "rwx");
199*0Sstevel@tonic-gate 				break;
200*0Sstevel@tonic-gate 			default:
201*0Sstevel@tonic-gate 				(void) printf("%-*d", field, p_phdr.p_flags);
202*0Sstevel@tonic-gate 				break;
203*0Sstevel@tonic-gate 			}
204*0Sstevel@tonic-gate 			(void) printf(
205*0Sstevel@tonic-gate 				"%-#*llx\n\n", field, EC_XWORD(p_phdr.p_align));
206*0Sstevel@tonic-gate 		}
207*0Sstevel@tonic-gate 	}
208*0Sstevel@tonic-gate }
209