xref: /titanic_50/usr/src/cmd/sgs/dump/common/fcns.c (revision 12240b1daf1d29749412cd4091e50e29cda86615)
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 (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
27  */
28 
29 #include	<stdio.h>
30 #include	<stdlib.h>
31 #include	<unistd.h>
32 #include	<string.h>
33 #include	<_libelf.h>
34 #include	<limits.h>
35 #include	"conv.h"
36 #include	"dump.h"
37 
38 extern int	p_flag;
39 extern char	*prog_name;
40 
41 
42 /*
43  * Print the symbols in the archive symbol table.
44  */
45 void
46 ar_sym_read(Elf *elf, char *filename)
47 {
48 	Elf_Arsym *	arsym;
49 	size_t		cnt, ptr, is64;
50 	const char	*fmt;
51 
52 	if ((arsym = elf_getarsym(elf, &ptr)) == NULL) {
53 		(void) fprintf(stderr, "%s: %s: no archive symbol table\n",
54 		    prog_name, filename);
55 		return;
56 	}
57 
58 	is64 = (_elf_getarsymwordsize(elf) == 8);
59 	(void) printf("%s:\n", filename);
60 
61 	if (!p_flag) {
62 		(void) printf("     **** ARCHIVE SYMBOL TABLE ****\n");
63 		if (is64) {
64 			(void) printf("%-8s         %s\n\n", "Offset", "Name");
65 			fmt = "%-16.16llx %s\n";
66 		} else {
67 			(void) printf("%-8s %s\n\n", "Offset", "Name");
68 			fmt = "%-8.8llx %s\n";
69 		}
70 	}
71 	for (cnt = 0; cnt < ptr; cnt++, arsym++) {
72 		if (arsym->as_off)
73 			(void) printf(fmt, EC_XWORD(arsym->as_off),
74 			    (arsym->as_name ? arsym->as_name : ""));
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 			Conv_inv_buf_t	inv_buf;
134 
135 			(void) printf("%-*s", field,
136 			    conv_phdr_type(ehdr.e_ident[EI_OSABI],
137 			    ehdr.e_machine, p_phdr.p_type, DUMP_CONVFMT,
138 			    &inv_buf));
139 			(void) printf(
140 			    "%-#*llx%-#*llx%-#*llx\n%-#*llx%-#*llx",
141 			    field, EC_OFF(p_phdr.p_offset),
142 			    field, EC_ADDR(p_phdr.p_vaddr),
143 			    field, EC_ADDR(p_phdr.p_paddr),
144 			    field, EC_XWORD(p_phdr.p_filesz),
145 			    field, EC_XWORD(p_phdr.p_memsz));
146 
147 			switch (p_phdr.p_flags) {
148 			case 0: (void) printf("%-*s", field, "---"); break;
149 			case PF_X:
150 				(void) printf("%-*s", field, "--x");
151 				break;
152 			case PF_W:
153 				(void) printf("%-*s", field, "-w-");
154 				break;
155 			case PF_W+PF_X:
156 				(void) printf("%-*s", field, "-wx");
157 				break;
158 			case PF_R:
159 				(void) printf("%-*s", field, "r--");
160 				break;
161 			case PF_R+PF_X:
162 				(void) printf("%-*s", field, "r-x");
163 				break;
164 			case PF_R+PF_W:
165 				(void) printf("%-*s", field, "rw-");
166 				break;
167 			case PF_R+PF_W+PF_X:
168 				(void) printf("%-*s", field, "rwx");
169 				break;
170 			default:
171 				(void) printf("%-*d", field, p_phdr.p_flags);
172 				break;
173 			}
174 			(void) printf(
175 			    "%-#*llx\n\n", field, EC_XWORD(p_phdr.p_align));
176 		}
177 	}
178 }
179