-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfd-disas.c
366 lines (315 loc) · 8.87 KB
/
bfd-disas.c
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* Interface to the BFD disassembler */
/* IMPORTANT LICENSING INFORMATION:
*
* linking this code against 'libbfd'/ 'libopcodes'
* may be subject to the GPL conditions.
* (This file itself is distributed under the 'SLAC'
* license)
*
*/
/* SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03
*
* Authorship
* ----------
* This software (CEXP - C-expression interpreter and runtime
* object loader/linker) was created by
*
* Till Straumann <[email protected]>, 2002-2008,
* Stanford Linear Accelerator Center, Stanford University.
*
* Acknowledgement of sponsorship
* ------------------------------
* This software was produced by
* the Stanford Linear Accelerator Center, Stanford University,
* under Contract DE-AC03-76SFO0515 with the Department of Energy.
*
* Government disclaimer of liability
* ----------------------------------
* Neither the United States nor the United States Department of Energy,
* nor any of their employees, makes any warranty, express or implied, or
* assumes any legal liability or responsibility for the accuracy,
* completeness, or usefulness of any data, apparatus, product, or process
* disclosed, or represents that its use would not infringe privately owned
* rights.
*
* Stanford disclaimer of liability
* --------------------------------
* Stanford University makes no representations or warranties, express or
* implied, nor assumes any liability for the use of this software.
*
* Stanford disclaimer of copyright
* --------------------------------
* Stanford University, owner of the copyright, hereby disclaims its
* copyright and all other rights in this software. Hence, anyone may
* freely use it for any purpose without restriction.
*
* Maintenance of notices
* ----------------------
* In the interest of clarity regarding the origin and status of this
* SLAC software, this and all the preceding Stanford University notices
* are to remain affixed to any copy or derivative of this software made
* or distributed by the recipient and are to be affixed to any copy of
* software made or distributed by the recipient that contains a copy or
* derivative of this software.
*
* SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
#define BUFMAX 500
#include "cexp.h"
#include "context.h"
#include "cexpsymsP.h"
#include "cexpmodP.h"
#ifdef USE_PMBFD
#include "pmbfd.h"
#endif
#include "dis-asm.h"
static disassembler_ftype bfdDisassembler = 0;
enum bfd_endian bfdEndian = BFD_ENDIAN_UNKNOWN;
enum bfd_flavour bfdFlavour = bfd_target_unknown_flavour;
enum bfd_architecture bfdArch = bfd_arch_unknown;
unsigned long bfdMach = 0;
unsigned int bfdOctetsPerByte = 1;
typedef struct DAStreamRec_ {
char buf[BUFMAX]; /* buffer to assemble the line */
int p; /* 'cursor' */
} DAStreamRec, *DAStream;
static int
daPrintf(DAStream s, char *fmt, ...)
{
va_list ap;
int written;
va_start(ap, fmt);
#ifdef HAVE_VSNPRINTF
written=vsnprintf(s->buf+s->p, BUFMAX - s->p, fmt, ap);
#else
written=vsprintf(s->buf+s->p, fmt, ap);
#endif
assert(written >= 0 && (s->p+=written) < BUFMAX);
va_end(ap);
return written;
}
static void
printSymAddr(bfd_vma addr, CexpModule mod, CexpSym sym, disassemble_info *di)
{
if (!sym) {
di->fprintf_func(di->stream,"?NULL?");
return;
} else {
long diff=addr - (bfd_vma)sym->value.ptv;
char diffbuf[30];
if (diff)
sprintf(diffbuf," + 0x%x",(unsigned)diff);
else
diffbuf[0]=0;
di->fprintf_func(di->stream,
"<%s:%s%s>",
cexpModuleName(mod),
sym->name,
diffbuf);
}
}
static void
printAddr(bfd_vma addr, disassemble_info *di)
{
CexpSym sym;
CexpModule mod;
sym = cexpSymLkAddr((void*)addr, 0, 0, &mod);
printSymAddr(addr,mod,sym,di);
}
static int
symbolAtAddr(bfd_vma addr, disassemble_info *di)
{
CexpSym s;
return (s=cexpSymLkAddr((void*)addr,0,0,0)) &&
(void*)s->value.ptv == (void*)addr;
}
#if 0 /* unused - why did I create that ?? */
static int
readMem(bfd_vma vma, bfd_byte *buf, unsigned int length, disassemble_info *di)
{
/* memory is already holding the data we want to disassemble */
return 0;
}
#endif
void
cexpDisassemblerInit(disassemble_info *di, PTR stream)
{
DAStreamRec dummy;
dummy.p = 0;
/* newer versions don't export the BFD_VERSION macro anymore :0 */
#ifdef BFD_VERSION
INIT_DISASSEMBLE_INFO((*di),(PTR)&dummy, (fprintf_ftype)daPrintf);
#else
init_disassemble_info (di, &dummy, (fprintf_ftype) daPrintf);
#endif
/* don't need the buffer_length; just set to a value high enough */
di->buffer_length = 100;
di->display_endian = di->endian = bfdEndian;
di->buffer = (bfd_byte *)cexpDisassemblerInit;
di->symbol_at_address_func = symbolAtAddr;
di->print_address_func = printAddr;
di->flavour = bfdFlavour;
di->arch = bfdArch;
di->mach = bfdMach;
di->octets_per_byte = bfdOctetsPerByte;
#ifndef BFD_VERSION
/* Allow the target to customize the info structure. */
disassemble_init_for_target (di);
#endif
/* disassemble one line to set the bytes_per_line field */
if (bfdDisassembler) {
bfdDisassembler((bfd_vma)di->buffer, di);
}
/* reset stream */
di->stream = stream;
di->fprintf_func = (fprintf_ftype)fprintf;
}
void
cexpDisassemblerInstall(bfd *abfd)
{
if (bfdDisassembler)
return; /* has been installed already */
#ifdef USEPMBFD
/* Special hack; the disassembler asks BFD for
* target properties. If pmbfd receives a NULL
* BFD handle then it returns the host machine's
* data which is what we want here...
*/
abfd = 0;
#endif
if (!(bfdDisassembler = disassembler(abfd))) {
bfd_perror("Warning: no disassembler found");
return;
}
if (bfd_big_endian(abfd))
bfdEndian = BFD_ENDIAN_BIG;
else if (bfd_little_endian(abfd))
bfdEndian = BFD_ENDIAN_LITTLE;
else {
fprintf(stderr,
"UNKNOWN BFD ENDIANNESS; unable to install disassembler\n");
bfdDisassembler=0;
}
bfdFlavour = bfd_get_flavour(abfd);
bfdArch = bfd_get_arch(abfd);
bfdMach = bfd_get_mach(abfd);
bfdOctetsPerByte = bfd_octets_per_byte(abfd);
}
static CexpSym
getNextSym(int *pindex, CexpModule *pmod, void *addr)
{
if (*pindex < 0 || *pindex >= (*pmod)->symtbl->nentries-1) {
/* reached the end of the module's symbol table;
* search module list again
*/
*pindex = cexpSymLkAddrIdx(addr, 0, 0, pmod);
if (*pindex < 0)
return 0;
} else {
(*pindex)++;
}
return (*pmod)->symtbl->aindex[*pindex];
}
int
cexpDisassemble(void *addr, int n, disassemble_info *di)
{
FILE *f;
fprintf_ftype orig_fprintf;
DAStreamRec b;
CexpSym currSym,nextSym;
CexpModule currMod,nextMod;
int found;
if (!bfdDisassembler) {
fprintf(stderr,"No disassembler support\n");
return -1;
}
if (!di) {
CexpContext currentContext = 0;
cexpContextGetCurrent(¤tContext);
assert(currentContext);
di = ¤tContext->dinfo;
}
if (addr)
di->buffer=addr;
/* redirect the stream */
orig_fprintf = di->fprintf_func;
f = di->stream;
di->stream = (PTR) &b;
b.p = 0;
di->fprintf_func = (fprintf_ftype)daPrintf;
if (n<1)
n=10;
found=-1;
currSym=getNextSym(&found,&currMod,di->buffer);
nextSym=0;
while (n-- > 0) {
int decoded,i,j,k,clip,spaces,bpc,bpl;
if (currSym) {
printSymAddr((bfd_vma)di->buffer,currMod, currSym, di);
b.p=0;
orig_fprintf(f,"\n%s:\n\n",b.buf);
currSym=0;
}
di->buffer_vma = (bfd_vma)di->buffer;
decoded = bfdDisassembler((bfd_vma)di->buffer, di);
bpc = di->bytes_per_chunk;
if (0==bpc) {
/* many targets don't set/use this */
bpc=1;
}
bpl = di->bytes_per_line;
if (0==bpl) {
/* some targets don't set this; take a wild guess
* (same as objdump)
*/
bpl = 4;
}
/* print the data in 'bytes_per_chunk' chunks
* which are endian-converted. Limit one line's
* output to 'bytes_per_line'.
*/
clip = decoded > bpl ? bpl : decoded;
for (i=0; i < decoded; i+=clip) {
/* print address */
orig_fprintf(f,"%p: ",di->buffer + i);
for (k=0; k < clip && k+i < decoded; k+=bpc) {
for (j=0; j<bpc; j++) {
if (BFD_ENDIAN_BIG == di->display_endian)
orig_fprintf(f,"%02x",di->buffer[i + k + j]);
else
orig_fprintf(f,"%02x",di->buffer[i + k + bpc - 1 - j]);
}
orig_fprintf(f," ");
}
if (i==0) {
spaces = (bpl - clip);
spaces = 2 * spaces + spaces/bpc;
orig_fprintf(f," %*s%s\n",spaces,"",b.buf);
} else {
orig_fprintf(f,"\n");
}
}
di->buffer+=decoded;
if (!nextSym) {
nextMod=currMod;
nextSym=getNextSym(&found,&nextMod,di->buffer);
}
if (nextSym) {
if (di->buffer >= (bfd_byte *)nextSym->value.ptv) {
currSym=nextSym; currMod=nextMod;
nextSym=0;
}
}
b.p=0;
}
/* restore the stream */
di->stream = f;
di->fprintf_func = orig_fprintf;
return 0;
}