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