xref: /illumos-gate/usr/src/cmd/sgs/dump/common/fcns.c (revision dd72704bd9e794056c558153663c739e2012d721)
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 		else
66 			(void) printf("%-8s %s\n\n", "Offset", "Name");
67 	}
68 
69 	if (is64)
70 		fmt = "%-16.16llx %s\n";
71 	else
72 		fmt = "%-8.8llx %s\n";
73 
74 	for (cnt = 0; cnt < ptr; cnt++, arsym++) {
75 		if (arsym->as_off)
76 			(void) printf(fmt, EC_XWORD(arsym->as_off),
77 			    (arsym->as_name ? arsym->as_name : ""));
78 	}
79 }
80 
81 /*
82  * Print the program execution header.  Input is an opened ELF object file, the
83  * number of structure instances in the header as recorded in the ELF header,
84  * and the filename.
85  */
86 void
87 dump_exec_header(Elf *elf_file, unsigned nseg, char *filename)
88 {
89 	GElf_Ehdr ehdr;
90 	GElf_Phdr p_phdr;
91 	int counter;
92 	int field;
93 	extern int v_flag, p_flag;
94 	extern char *prog_name;
95 
96 	if (gelf_getclass(elf_file) == ELFCLASS64)
97 		field = 16;
98 	else
99 		field = 12;
100 
101 	if (!p_flag) {
102 		(void) printf(" ***** PROGRAM EXECUTION HEADER *****\n");
103 		(void) printf("%-*s%-*s%-*s%s\n",
104 		    field, "Type", field, "Offset",
105 		    field, "Vaddr", "Paddr");
106 		(void) printf("%-*s%-*s%-*s%s\n\n",
107 		    field, "Filesz", field, "Memsz",
108 		    field, "Flags", "Align");
109 	}
110 
111 	if ((gelf_getehdr(elf_file, &ehdr) == 0) || (ehdr.e_phnum == 0)) {
112 		return;
113 	}
114 
115 	for (counter = 0; counter < nseg; counter++) {
116 
117 		if (gelf_getphdr(elf_file, counter, &p_phdr) == 0) {
118 			(void) fprintf(stderr,
119 			    "%s: %s: premature EOF on program exec header\n",
120 			    prog_name, filename);
121 			return;
122 		}
123 
124 		if (!v_flag) {
125 			(void) printf(
126 	"%-*d%-#*llx%-#*llx%-#*llx\n%-#*llx%-#*llx%-*u%-#*llx\n\n",
127 			    field, EC_WORD(p_phdr.p_type),
128 			    field, EC_OFF(p_phdr.p_offset),
129 			    field, EC_ADDR(p_phdr.p_vaddr),
130 			    field, EC_ADDR(p_phdr.p_paddr),
131 			    field, EC_XWORD(p_phdr.p_filesz),
132 			    field, EC_XWORD(p_phdr.p_memsz),
133 			    field, EC_WORD(p_phdr.p_flags),
134 			    field, EC_XWORD(p_phdr.p_align));
135 		} else {
136 			Conv_inv_buf_t	inv_buf;
137 
138 			(void) printf("%-*s", field,
139 			    conv_phdr_type(ehdr.e_ident[EI_OSABI],
140 			    ehdr.e_machine, p_phdr.p_type, DUMP_CONVFMT,
141 			    &inv_buf));
142 			(void) printf(
143 			    "%-#*llx%-#*llx%-#*llx\n%-#*llx%-#*llx",
144 			    field, EC_OFF(p_phdr.p_offset),
145 			    field, EC_ADDR(p_phdr.p_vaddr),
146 			    field, EC_ADDR(p_phdr.p_paddr),
147 			    field, EC_XWORD(p_phdr.p_filesz),
148 			    field, EC_XWORD(p_phdr.p_memsz));
149 
150 			switch (p_phdr.p_flags) {
151 			case 0: (void) printf("%-*s", field, "---"); break;
152 			case PF_X:
153 				(void) printf("%-*s", field, "--x");
154 				break;
155 			case PF_W:
156 				(void) printf("%-*s", field, "-w-");
157 				break;
158 			case PF_W+PF_X:
159 				(void) printf("%-*s", field, "-wx");
160 				break;
161 			case PF_R:
162 				(void) printf("%-*s", field, "r--");
163 				break;
164 			case PF_R+PF_X:
165 				(void) printf("%-*s", field, "r-x");
166 				break;
167 			case PF_R+PF_W:
168 				(void) printf("%-*s", field, "rw-");
169 				break;
170 			case PF_R+PF_W+PF_X:
171 				(void) printf("%-*s", field, "rwx");
172 				break;
173 			default:
174 				(void) printf("%-*d", field, p_phdr.p_flags);
175 				break;
176 			}
177 			(void) printf(
178 			    "%-#*llx\n\n", field, EC_XWORD(p_phdr.p_align));
179 		}
180 	}
181 }
182