1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright 2007 Jason King. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* The sparc disassembler is mostly straightforward, each instruction is
* represented by an inst_t structure. The inst_t definitions are organized
* into tables. The tables are correspond to the opcode maps documented in the
* various sparc architecture manuals. Each table defines the bit range of the
* instruction whose value act as an index into the array of instructions. A
* table can also refer to another table if needed. Each table also contains
* a function pointer of type format_fcn that knows how to output the
* instructions in the table, as well as handle any synthetic instructions
*
* Unfortunately, the changes from sparcv8 -> sparcv9 not only include new
* instructions, they sometimes renamed or just reused the same instruction to
* do different operations (i.e. the sparcv8 coprocessor instructions). To
* accommodate this, each table can define an overlay table. The overlay table
* is a list of (table index, architecture, new instruction definition) values.
*
*
* Traversal starts with the first table,
* get index value from the instruction
* if an relevant overlay entry exists for this index,
* grab the overlay definition
* else
* grab the definition from the array (corresponding to the index value)
*
* If the entry is an instruction,
* call print function of instruction.
* If the entry is a pointer to another table
* traverse the table
* If not valid,
* return an error
*
*
* To keep dis happy, for sparc, instead of actually returning an error, if
* the instruction cannot be disassembled, we instead merely place the value
* of the instruction into the output buffer.
*
* Adding new instructions:
*
* With the above information, it hopefully makes it clear how to add support
* for decoding new instructions. Presumably, with new instructions will come
* a new dissassembly mode (I.e. DIS_SPARC_V8, DIS_SPARC_V9, etc.).
*
* If the dissassembled format does not correspond to one of the existing
* formats, a new formatter will have to be written. The 'flags' value of
* inst_t is intended to instruct the corresponding formatter about how to
* output the instruction.
*
* If the corresponding entry in the correct table is currently unoccupied,
* simply replace the INVALID entry with the correct definition. The INST and
* TABLE macros are suggested to be used for this. If there is already an
* instruction defined, then the entry must be placed in an overlay table. If
* no overlay table exists for the instruction table, one will need to be
* created.
*/
#include <libdisasm.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/byteorder.h>
#include <string.h>
#include "libdisasm_impl.h"
#include "dis_sparc.h"
static const inst_t *dis_get_overlay(dis_handle_t *, const table_t *,
uint32_t);
static uint32_t dis_get_bits(uint32_t, int, int);
#if !defined(DIS_STANDALONE)
static void do_binary(uint32_t);
#endif /* DIS_STANDALONE */
dis_handle_t *
dis_handle_create(int flags, void *data, dis_lookup_f lookup_func,
dis_read_f read_func)
{
#if !defined(DIS_STANDALONE)
char *opt = NULL;
char *opt2, *save, *end;
#endif
dis_handle_t *dhp;
if ((flags & (DIS_SPARC_V8|DIS_SPARC_V9|DIS_SPARC_V9_SGI)) == 0) {
(void) dis_seterrno(E_DIS_INVALFLAG);
return (NULL);
}
if ((dhp = dis_zalloc(sizeof (struct dis_handle))) == NULL) {
(void) dis_seterrno(E_DIS_NOMEM);
return (NULL);
}
dhp->dh_lookup = lookup_func;
dhp->dh_read = read_func;
dhp->dh_flags = flags;
dhp->dh_data = data;
dhp->dh_debug = DIS_DEBUG_COMPAT;
#if !defined(DIS_STANDALONE)
opt = getenv("_LIBDISASM_DEBUG");
if (opt == NULL)
return (dhp);
opt2 = strdup(opt);
if (opt2 == NULL) {
dis_handle_destroy(dhp);
(void) dis_seterrno(E_DIS_NOMEM);
return (NULL);
}
save = opt2;
while (opt2 != NULL) {
end = strchr(opt2, ',');
if (end != 0)
*end++ = '\0';
if (strcasecmp("synth-all", opt2) == 0)
dhp->dh_debug |= DIS_DEBUG_SYN_ALL;
if (strcasecmp("compat", opt2) == 0)
dhp->dh_debug |= DIS_DEBUG_COMPAT;
if (strcasecmp("synth-none", opt2) == 0)
dhp->dh_debug &= ~(DIS_DEBUG_SYN_ALL|DIS_DEBUG_COMPAT);
if (strcasecmp("binary", opt2) == 0)
dhp->dh_debug |= DIS_DEBUG_PRTBIN;
if (strcasecmp("format", opt2) == 0)
dhp->dh_debug |= DIS_DEBUG_PRTFMT;
if (strcasecmp("all", opt2) == 0)
dhp->dh_debug = DIS_DEBUG_ALL;
if (strcasecmp("none", opt2) == 0)
dhp->dh_debug = DIS_DEBUG_NONE;
opt2 = end;
}
free(save);
#endif /* DIS_STANDALONE */
return (dhp);
}
void
dis_handle_destroy(dis_handle_t *dhp)
{
dis_free(dhp, sizeof (dis_handle_t));
}
void
dis_set_data(dis_handle_t *dhp, void *data)
{
dhp->dh_data = data;
}
void
dis_flags_set(dis_handle_t *dhp, int f)
{
dhp->dh_flags |= f;
}
void
dis_flags_clear(dis_handle_t *dhp, int f)
{
dhp->dh_flags &= ~f;
}
/* ARGSUSED */
int
dis_max_instrlen(dis_handle_t *dhp)
{
return (4);
}
/*
* The dis_i386.c comment for this says it returns the previous instruction,
* however, I'm fairly sure it's actually returning the _address_ of the
* nth previous instruction.
*/
/* ARGSUSED */
uint64_t
dis_previnstr(dis_handle_t *dhp, uint64_t pc, int n)
{
if (n <= 0)
return (pc);
if (pc < n)
return (pc);
return (pc - n*4);
}
int
dis_disassemble(dis_handle_t *dhp, uint64_t addr, char *buf, size_t buflen)
{
const table_t *tp = &initial_table;
const inst_t *inp = NULL;
uint32_t instr;
uint32_t idx = 0;
if (dhp->dh_read(dhp->dh_data, addr, &instr, sizeof (instr)) !=
sizeof (instr))
return (-1);
dhp->dh_buf = buf;
dhp->dh_buflen = buflen;
dhp->dh_addr = addr;
buf[0] = '\0';
/* this allows sparc code to be tested on x86 */
instr = BE_32(instr);
#if !defined(DIS_STANDALONE)
if ((dhp->dh_debug & DIS_DEBUG_PRTBIN) != 0)
do_binary(instr);
#endif /* DIS_STANDALONE */
/* CONSTCOND */
while (1) {
idx = dis_get_bits(instr, tp->tbl_field, tp->tbl_len);
inp = &tp->tbl_inp[idx];
inp = dis_get_overlay(dhp, tp, idx);
if ((inp->in_type == INST_NONE) ||
((inp->in_arch & dhp->dh_flags) == 0))
goto error;
if (inp->in_type == INST_TBL) {
tp = inp->in_data.in_tbl;
continue;
}
break;
}
if (tp->tbl_fmt(dhp, instr, inp, idx) == 0)
return (0);
error:
(void) snprintf(buf, buflen,
((dhp->dh_flags & DIS_OCTAL) != 0) ? "0%011lo" : "0x%08lx",
instr);
return (0);
}
static uint32_t
dis_get_bits(uint32_t instr, int offset, int length)
{
uint32_t mask, val;
int i;
for (i = 0, mask = 0; i < length; ++i)
mask |= (1UL << i);
mask = mask << (offset - length + 1);
val = instr & mask;
val = val >> (offset - length + 1);
return (val);
}
static const inst_t *
dis_get_overlay(dis_handle_t *dhp, const table_t *tp, uint32_t idx)
{
const inst_t *ip = &tp->tbl_inp[idx];
int i;
if (tp->tbl_ovp == NULL)
return (ip);
for (i = 0; tp->tbl_ovp[i].ov_idx != -1; ++i) {
if (tp->tbl_ovp[i].ov_idx != idx)
continue;
if ((tp->tbl_ovp[i].ov_inst.in_arch & dhp->dh_flags) == 0)
continue;
ip = &tp->tbl_ovp[i].ov_inst;
break;
}
return (ip);
}
#if !defined(DIS_STANDALONE)
static void
do_binary(uint32_t instr)
{
(void) fprintf(stderr, "DISASM: ");
prt_binary(instr, 32);
(void) fprintf(stderr, "\n");
}
#endif /* DIS_STANDALONE */
|