xref: /titanic_50/usr/src/cmd/sgs/dump/common/fcns.c (revision 90eb94cfb94cf76036f814623665c51517beebfb)
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 /*	Copyright (c) 1988 AT&T	*/
22 /*	  All Rights Reserved  	*/
23 
24 
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 #include	<stdio.h>
33 #include	<stdlib.h>
34 #include	<unistd.h>
35 #include	<string.h>
36 #include	<libelf.h>
37 #include	<limits.h>
38 #include	"dump.h"
39 
40 extern int	p_flag;
41 extern char	*prog_name;
42 
43 
44 /*
45  * Print the symbols in the archive symbol table.
46  */
47 void
48 ar_sym_read(Elf *elf, char *filename)
49 {
50 	Elf_Arsym *	arsym;
51 	size_t		cnt, ptr;
52 
53 	if ((arsym = elf_getarsym(elf, &ptr)) == NULL) {
54 		(void) fprintf(stderr, "%s: %s: no archive symbol table\n",
55 			prog_name, filename);
56 		return;
57 	}
58 
59 	(void) printf("%s:\n", filename);
60 
61 	if (!p_flag) {
62 		(void) printf("     **** ARCHIVE SYMBOL TABLE ****\n");
63 		(void) printf("%-8s %s\n\n", "Offset", "Name");
64 	}
65 	for (cnt = 0; cnt < ptr; cnt++, arsym++) {
66 		if (arsym->as_off) {
67 			/* LINTED */
68 			(void) printf("%-8.8x %s\n", (int)arsym->as_off,
69 			    (arsym->as_name ? arsym->as_name : ""));
70 		}
71 	}
72 }
73 
74 /*
75  * Print the program execution header.  Input is an opened ELF object file, the
76  * number of structure instances in the header as recorded in the ELF header,
77  * and the filename.
78  */
79 void
80 dump_exec_header(Elf *elf_file, unsigned nseg, char *filename)
81 {
82 	GElf_Ehdr ehdr;
83 	GElf_Phdr p_phdr;
84 	int counter;
85 	int field;
86 	extern int v_flag, p_flag;
87 	extern char *prog_name;
88 
89 	if (gelf_getclass(elf_file) == ELFCLASS64)
90 		field = 16;
91 	else
92 		field = 12;
93 
94 	if (!p_flag) {
95 		(void) printf(" ***** PROGRAM EXECUTION HEADER *****\n");
96 		(void) printf("%-*s%-*s%-*s%s\n",
97 		    field, "Type", field, "Offset",
98 		    field, "Vaddr", "Paddr");
99 		(void) printf("%-*s%-*s%-*s%s\n\n",
100 		    field, "Filesz", field, "Memsz",
101 		    field, "Flags", "Align");
102 	}
103 
104 	if ((gelf_getehdr(elf_file, &ehdr) == 0) || (ehdr.e_phnum == 0)) {
105 		return;
106 	}
107 
108 	for (counter = 0; counter < nseg; counter++) {
109 
110 		if (gelf_getphdr(elf_file, counter, &p_phdr) == 0) {
111 			(void) fprintf(stderr,
112 			"%s: %s: premature EOF on program exec header\n",
113 				prog_name, filename);
114 			return;
115 		}
116 
117 		if (!v_flag) {
118 			(void) printf(
119 	"%-*d%-#*llx%-#*llx%-#*llx\n%-#*llx%-#*llx%-*u%-#*llx\n\n",
120 				field, EC_WORD(p_phdr.p_type),
121 				field, EC_OFF(p_phdr.p_offset),
122 				field, EC_ADDR(p_phdr.p_vaddr),
123 				field, EC_ADDR(p_phdr.p_paddr),
124 				field, EC_XWORD(p_phdr.p_filesz),
125 				field, EC_XWORD(p_phdr.p_memsz),
126 				field, EC_WORD(p_phdr.p_flags),
127 				field, EC_XWORD(p_phdr.p_align));
128 		} else {
129 			switch (p_phdr.p_type) {
130 			case PT_NULL:
131 				(void) printf("%-*s", field, "NULL");
132 				break;
133 			case PT_LOAD:
134 				(void) printf("%-*s", field, "LOAD");
135 				break;
136 			case PT_DYNAMIC:
137 				(void) printf("%-*s", field, "DYN");
138 				break;
139 			case PT_INTERP:
140 				(void) printf("%-*s", field, "INTERP");
141 				break;
142 			case PT_NOTE:
143 				(void) printf("%-*s", field, "NOTE");
144 				break;
145 			case PT_SHLIB:
146 				(void) printf("%-*s", field, "SHLIB");
147 				break;
148 			case PT_PHDR:
149 				(void) printf("%-*s", field, "PHDR");
150 				break;
151 			case PT_TLS:
152 				(void) printf("%-*s", field, "TLS");
153 				break;
154 			case PT_SUNWBSS:
155 				(void) printf("%-*s", field, "SUNWBSS");
156 				break;
157 			case PT_SUNWSTACK:
158 				(void) printf("%-*s", field, "SUNWSTACK");
159 				break;
160 			default:
161 				(void) printf("%-*d", field,
162 					(int)p_phdr.p_type);
163 				break;
164 			}
165 			(void) printf(
166 				"%-#*llx%-#*llx%-#*llx\n%-#*llx%-#*llx",
167 				field, EC_OFF(p_phdr.p_offset),
168 				field, EC_ADDR(p_phdr.p_vaddr),
169 				field, EC_ADDR(p_phdr.p_paddr),
170 				field, EC_XWORD(p_phdr.p_filesz),
171 				field, EC_XWORD(p_phdr.p_memsz));
172 
173 			switch (p_phdr.p_flags) {
174 			case 0: (void) printf("%-*s", field, "---"); break;
175 			case PF_X:
176 				(void) printf("%-*s", field, "--x");
177 				break;
178 			case PF_W:
179 				(void) printf("%-*s", field, "-w-");
180 				break;
181 			case PF_W+PF_X:
182 				(void) printf("%-*s", field, "-wx");
183 				break;
184 			case PF_R:
185 				(void) printf("%-*s", field, "r--");
186 				break;
187 			case PF_R+PF_X:
188 				(void) printf("%-*s", field, "r-x");
189 				break;
190 			case PF_R+PF_W:
191 				(void) printf("%-*s", field, "rw-");
192 				break;
193 			case PF_R+PF_W+PF_X:
194 				(void) printf("%-*s", field, "rwx");
195 				break;
196 			default:
197 				(void) printf("%-*d", field, p_phdr.p_flags);
198 				break;
199 			}
200 			(void) printf(
201 				"%-#*llx\n\n", field, EC_XWORD(p_phdr.p_align));
202 		}
203 	}
204 }
205