xref: /illumos-gate/usr/src/cmd/sgs/dump/common/fcns.c (revision bb9b6b3f59b8820022416cea99b49c50fef6e391)
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 2009 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 #include	<stdio.h>
31 #include	<stdlib.h>
32 #include	<unistd.h>
33 #include	<string.h>
34 #include	<libelf.h>
35 #include	<limits.h>
36 #include	"conv.h"
37 #include	"dump.h"
38 
39 extern int	p_flag;
40 extern char	*prog_name;
41 
42 
43 /*
44  * Print the symbols in the archive symbol table.
45  */
46 void
47 ar_sym_read(Elf *elf, char *filename)
48 {
49 	Elf_Arsym *	arsym;
50 	size_t		cnt, ptr;
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 	(void) printf("%s:\n", filename);
59 
60 	if (!p_flag) {
61 		(void) printf("     **** ARCHIVE SYMBOL TABLE ****\n");
62 		(void) printf("%-8s %s\n\n", "Offset", "Name");
63 	}
64 	for (cnt = 0; cnt < ptr; cnt++, arsym++) {
65 		if (arsym->as_off) {
66 			/* LINTED */
67 			(void) printf("%-8.8x %s\n", (int)arsym->as_off,
68 			    (arsym->as_name ? arsym->as_name : ""));
69 		}
70 	}
71 }
72 
73 /*
74  * Print the program execution header.  Input is an opened ELF object file, the
75  * number of structure instances in the header as recorded in the ELF header,
76  * and the filename.
77  */
78 void
79 dump_exec_header(Elf *elf_file, unsigned nseg, char *filename)
80 {
81 	GElf_Ehdr ehdr;
82 	GElf_Phdr p_phdr;
83 	int counter;
84 	int field;
85 	extern int v_flag, p_flag;
86 	extern char *prog_name;
87 
88 	if (gelf_getclass(elf_file) == ELFCLASS64)
89 		field = 16;
90 	else
91 		field = 12;
92 
93 	if (!p_flag) {
94 		(void) printf(" ***** PROGRAM EXECUTION HEADER *****\n");
95 		(void) printf("%-*s%-*s%-*s%s\n",
96 		    field, "Type", field, "Offset",
97 		    field, "Vaddr", "Paddr");
98 		(void) printf("%-*s%-*s%-*s%s\n\n",
99 		    field, "Filesz", field, "Memsz",
100 		    field, "Flags", "Align");
101 	}
102 
103 	if ((gelf_getehdr(elf_file, &ehdr) == 0) || (ehdr.e_phnum == 0)) {
104 		return;
105 	}
106 
107 	for (counter = 0; counter < nseg; counter++) {
108 
109 		if (gelf_getphdr(elf_file, counter, &p_phdr) == 0) {
110 			(void) fprintf(stderr,
111 			    "%s: %s: premature EOF on program exec header\n",
112 			    prog_name, filename);
113 			return;
114 		}
115 
116 		if (!v_flag) {
117 			(void) printf(
118 	"%-*d%-#*llx%-#*llx%-#*llx\n%-#*llx%-#*llx%-*u%-#*llx\n\n",
119 			    field, EC_WORD(p_phdr.p_type),
120 			    field, EC_OFF(p_phdr.p_offset),
121 			    field, EC_ADDR(p_phdr.p_vaddr),
122 			    field, EC_ADDR(p_phdr.p_paddr),
123 			    field, EC_XWORD(p_phdr.p_filesz),
124 			    field, EC_XWORD(p_phdr.p_memsz),
125 			    field, EC_WORD(p_phdr.p_flags),
126 			    field, EC_XWORD(p_phdr.p_align));
127 		} else {
128 			Conv_inv_buf_t	inv_buf;
129 
130 			(void) printf("%-*s", field,
131 			    conv_phdr_type(ehdr.e_ident[EI_OSABI],
132 			    ehdr.e_machine, p_phdr.p_type, DUMP_CONVFMT,
133 			    &inv_buf));
134 			(void) printf(
135 			    "%-#*llx%-#*llx%-#*llx\n%-#*llx%-#*llx",
136 			    field, EC_OFF(p_phdr.p_offset),
137 			    field, EC_ADDR(p_phdr.p_vaddr),
138 			    field, EC_ADDR(p_phdr.p_paddr),
139 			    field, EC_XWORD(p_phdr.p_filesz),
140 			    field, EC_XWORD(p_phdr.p_memsz));
141 
142 			switch (p_phdr.p_flags) {
143 			case 0: (void) printf("%-*s", field, "---"); break;
144 			case PF_X:
145 				(void) printf("%-*s", field, "--x");
146 				break;
147 			case PF_W:
148 				(void) printf("%-*s", field, "-w-");
149 				break;
150 			case PF_W+PF_X:
151 				(void) printf("%-*s", field, "-wx");
152 				break;
153 			case PF_R:
154 				(void) printf("%-*s", field, "r--");
155 				break;
156 			case PF_R+PF_X:
157 				(void) printf("%-*s", field, "r-x");
158 				break;
159 			case PF_R+PF_W:
160 				(void) printf("%-*s", field, "rw-");
161 				break;
162 			case PF_R+PF_W+PF_X:
163 				(void) printf("%-*s", field, "rwx");
164 				break;
165 			default:
166 				(void) printf("%-*d", field, p_phdr.p_flags);
167 				break;
168 			}
169 			(void) printf(
170 			    "%-#*llx\n\n", field, EC_XWORD(p_phdr.p_align));
171 		}
172 	}
173 }
174