xref: /illumos-gate/usr/src/cmd/sgs/libconv/common/dwarf.c (revision 7e16fca05dfbcfd32c2ebc9e4d1abdac1cd8657c)
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 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include	<strings.h>
28 #include	<dwarf.h>
29 #include	"_conv.h"
30 #include	<dwarf_msg.h>
31 
32 /*
33  * This code is primarily of interest to elfdump. Separating it from dwarf_ehe
34  * allows other tools to use dwarf_ehe without also pulling this in.
35  */
36 
37 /* Instantiate a local copy of conv_map2str() from _conv.h */
38 DEFINE_conv_map2str
39 
40 
41 /*
42  * Translate DW_CFA_ codes, used to identify Call Frame Instructions.
43  */
44 const char *
45 conv_dwarf_cfa(uchar_t op, Conv_fmt_flags_t fmt_flags, Conv_inv_buf_t *inv_buf)
46 {
47 	static const Msg	cfa[] = {
48 		MSG_DW_CFA_NOP,			MSG_DW_CFA_SET_LOC,
49 		MSG_DW_CFA_ADVANCE_LOC_1,	MSG_DW_CFA_ADVANCE_LOC_2,
50 		MSG_DW_CFA_ADVANCE_LOC_4,	MSG_DW_CFA_OFFSET_EXTENDED,
51 		MSG_DW_CFA_RESTORE_EXTENDED,	MSG_DW_CFA_UNDEFINED,
52 		MSG_DW_CFA_SAME_VALUE,		MSG_DW_CFA_REGISTER,
53 		MSG_DW_CFA_REMEMBER_STATE,	MSG_DW_CFA_RESTORE_STATE,
54 		MSG_DW_CFA_DEF_CFA,		MSG_DW_CFA_DEF_CFA_REGISTER,
55 		MSG_DW_CFA_DEF_CFA_OFFSET,	MSG_DW_CFA_DEF_CFA_EXPRESSION,
56 		MSG_DW_CFA_EXPRESSION,		MSG_DW_CFA_OFFSET_EXTENDED_SF,
57 		MSG_DW_CFA_DEF_CFA_SF,		MSG_DW_CFA_DEF_CFA_OFFSET_SF,
58 		MSG_DW_CFA_VAL_OFFSET,		MSG_DW_CFA_VAL_OFFSET_SF,
59 		MSG_DW_CFA_VAL_EXPRESSION
60 	};
61 	static const Msg	gnu[] = {
62 		MSG_DW_CFA_GNU_WINDOW_SAVE,	MSG_DW_CFA_GNU_ARGS_SIZE,
63 		MSG_DW_CFA_GNU_NEGATIVE_OFF_X
64 	};
65 
66 	/*
67 	 * DWARF CFA opcodes are bytes. The top 2 bits are a primary
68 	 * opcode, and if zero, the lower 6 bits specify a sub-opcode
69 	 */
70 	switch (op >> 6) {
71 	case 0x1:
72 		return (MSG_ORIG(MSG_DW_CFA_ADVANCE_LOC));
73 	case 0x2:
74 		return (MSG_ORIG(MSG_DW_CFA_OFFSET));
75 	case 0x3:
76 		return (MSG_ORIG(MSG_DW_CFA_RESTORE));
77 	}
78 
79 	if (op == 0x1d)
80 		return (MSG_ORIG(MSG_DW_CFA_MIPS_ADV_LOC8));
81 
82 	if ((op >= 0x2d) && (op <= 0x2f))
83 		return (conv_map2str(inv_buf, (op - 0x2d),
84 		    fmt_flags, ARRAY_NELTS(gnu), gnu));
85 
86 	return (conv_map2str(inv_buf, op, fmt_flags, ARRAY_NELTS(cfa), cfa));
87 }
88 
89 /*
90  * Translate DWARF register numbers to hardware specific names
91  *
92  * If good_name is non-NULL, conv_dwarf_regname() will set the variable to
93  * True(1) if the returned string is considered to be a good name to
94  * display, and False(0) otherwise. To be considered "good":
95  *
96  *    -	The name must be a well known mnemonic for a register
97  *	from the machine type in question.
98  *
99  *    -	The name must be different than the DWARF name for
100  *	the same register.
101  *
102  * The returned string is usable, regardless of the value returned in
103  * *good_name.
104  */
105 const char *
106 conv_dwarf_regname(Half mach, Word regno, Conv_fmt_flags_t fmt_flags,
107     int *good_name, Conv_inv_buf_t *inv_buf)
108 {
109 	static const Msg	reg_amd64[67] = {
110 		MSG_REG_RAX,		MSG_REG_RDX,
111 		MSG_REG_RCX,		MSG_REG_RBX,
112 		MSG_REG_RSI,		MSG_REG_RDI,
113 		MSG_REG_RBP,		MSG_REG_RSP,
114 		MSG_REG_R8,		MSG_REG_R9,
115 		MSG_REG_R10,		MSG_REG_R11,
116 		MSG_REG_R12,		MSG_REG_R13,
117 		MSG_REG_R14,		MSG_REG_R15,
118 		MSG_REG_RA,		MSG_REG_PERXMM0,
119 		MSG_REG_PERXMM1,	MSG_REG_PERXMM2,
120 		MSG_REG_PERXMM3,	MSG_REG_PERXMM4,
121 		MSG_REG_PERXMM5,	MSG_REG_PERXMM6,
122 		MSG_REG_PERXMM7,	MSG_REG_PERXMM8,
123 		MSG_REG_PERXMM9,	MSG_REG_PERXMM10,
124 		MSG_REG_PERXMM11,	MSG_REG_PERXMM12,
125 		MSG_REG_PERXMM13,	MSG_REG_PERXMM14,
126 		MSG_REG_PERXMM15,	MSG_REG_PERST0,
127 		MSG_REG_PERST1,		MSG_REG_PERST2,
128 		MSG_REG_PERST3,		MSG_REG_PERST4,
129 		MSG_REG_PERST5,		MSG_REG_PERST6,
130 		MSG_REG_PERST7,		MSG_REG_PERMM0,
131 		MSG_REG_PERMM1,		MSG_REG_PERMM2,
132 		MSG_REG_PERMM3,		MSG_REG_PERMM4,
133 		MSG_REG_PERMM5,		MSG_REG_PERMM6,
134 		MSG_REG_PERMM7,		MSG_REG_PERRFLAGS,
135 		MSG_REG_PERES,		MSG_REG_PERCS,
136 		MSG_REG_PERSS,		MSG_REG_PERDS,
137 		MSG_REG_PERFS,		MSG_REG_PERGS,
138 		MSG_REG_RESERVED,	MSG_REG_RESERVED,
139 		MSG_REG_PERFSDOTBASE,	MSG_REG_PERGSDOTBASE,
140 		MSG_REG_RESERVED,	MSG_REG_RESERVED,
141 		MSG_REG_PERTR,		MSG_REG_PERLDTR,
142 		MSG_REG_PERMXCSR,	MSG_REG_PERFCW,
143 		MSG_REG_PERFSW
144 	};
145 
146 	static const Msg	reg_i386[8] = {
147 		MSG_REG_EAX,		MSG_REG_ECX,
148 		MSG_REG_EDX,		MSG_REG_EBX,
149 		MSG_REG_UESP,		MSG_REG_EBP,
150 		MSG_REG_ESI,		MSG_REG_EDI
151 	};
152 
153 	static const Msg	reg_sparc[64] = {
154 		MSG_REG_G0,		MSG_REG_G1,
155 		MSG_REG_G2,		MSG_REG_G3,
156 		MSG_REG_G4,		MSG_REG_G5,
157 		MSG_REG_G6,		MSG_REG_G7,
158 		MSG_REG_O0,		MSG_REG_O1,
159 		MSG_REG_O2,		MSG_REG_O3,
160 		MSG_REG_O4,		MSG_REG_O5,
161 		MSG_REG_O6,		MSG_REG_O7,
162 		MSG_REG_L0,		MSG_REG_L1,
163 		MSG_REG_L2,		MSG_REG_L3,
164 		MSG_REG_L4,		MSG_REG_L5,
165 		MSG_REG_L6,		MSG_REG_L7,
166 		MSG_REG_I0,		MSG_REG_I1,
167 		MSG_REG_I2,		MSG_REG_I3,
168 		MSG_REG_I4,		MSG_REG_I5,
169 		MSG_REG_I6,		MSG_REG_I7,
170 		MSG_REG_F0,		MSG_REG_F1,
171 		MSG_REG_F2,		MSG_REG_F3,
172 		MSG_REG_F4,		MSG_REG_F5,
173 		MSG_REG_F6,		MSG_REG_F7,
174 		MSG_REG_F8,		MSG_REG_F9,
175 		MSG_REG_F10,		MSG_REG_F11,
176 		MSG_REG_F12,		MSG_REG_F13,
177 		MSG_REG_F14,		MSG_REG_F15,
178 		MSG_REG_F16,		MSG_REG_F17,
179 		MSG_REG_F18,		MSG_REG_F19,
180 		MSG_REG_F20,		MSG_REG_F21,
181 		MSG_REG_F22,		MSG_REG_F23,
182 		MSG_REG_F24,		MSG_REG_F25,
183 		MSG_REG_F26,		MSG_REG_F27,
184 		MSG_REG_F28,		MSG_REG_F29,
185 		MSG_REG_F30,		MSG_REG_F31
186 	};
187 
188 	switch (mach) {
189 	case EM_AMD64:
190 		/*
191 		 * amd64 has several in-bounds names we'd rather not
192 		 * use. R8-R15 have the same name as their DWARF counterparts.
193 		 * 56-57, and 60-61 are reserved, and don't have a good name.
194 		 */
195 		if (good_name)
196 			*good_name = ((regno < 8) || (regno > 15)) &&
197 			    (regno != 56) && (regno != 57) &&
198 			    (regno != 60) && (regno != 61) &&
199 			    (regno < ARRAY_NELTS(reg_amd64));
200 		return (conv_map2str(inv_buf, regno,
201 		    fmt_flags, ARRAY_NELTS(reg_amd64), reg_amd64));
202 
203 	case EM_386:
204 	case EM_486:
205 		if (good_name)
206 			*good_name = (regno < ARRAY_NELTS(reg_i386));
207 		return (conv_map2str(inv_buf, regno,
208 		    fmt_flags, ARRAY_NELTS(reg_i386), reg_i386));
209 
210 	case EM_SPARC:
211 	case EM_SPARC32PLUS:
212 	case EM_SPARCV9:
213 		if (good_name)
214 			*good_name = (regno < ARRAY_NELTS(reg_sparc));
215 		return (conv_map2str(inv_buf, regno,
216 		    fmt_flags, ARRAY_NELTS(reg_sparc), reg_sparc));
217 	}
218 
219 	if (good_name)
220 		*good_name = 0;
221 	return (conv_invalid_val(inv_buf, regno, 0));
222 }
223