-
Notifications
You must be signed in to change notification settings - Fork 3
/
Disassembler.C
147 lines (124 loc) · 3.56 KB
/
Disassembler.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
#include "Common.h"
#include "Disassembler.h"
#include <malloc.h>
#include <dis-asm.h>
#include <cstdarg>
#include <string.h>
#include <assert.h>
static void DisasPrintAddress(bfd_vma, struct disassemble_info*)
{
// do nothing
}
void Disassembler::Disassemble (bfd* abfd, asection* section,
char* sectionContents,
const unsigned int startAddress, const unsigned int endAddress)
{
disassembler_ftype disasFunc = disassembler(abfd);
DEBUG("disasFunc: %p", disasFunc);
DEBUG("section name: %s; size: %lld; vma: 0x%llx; lma: 0x%llx; output_offset: 0x%lld; filepos: %llx",
section->name, (long long int)section->size, (long long int)section->vma,
(long long int)section->lma, (long long int)section->output_offset,
(long long int)section->filepos);
struct disassemble_info disasInfo;
char printBuffer[100];
init_disassemble_info(&disasInfo, &printBuffer, (fprintf_ftype)DisasPrintf);
disasInfo.print_address_func = DisasPrintAddress;
disasInfo.arch = bfd_get_arch(abfd);
disasInfo.mach = bfd_get_mach(abfd);
disasInfo.buffer_vma = section->vma;
disasInfo.buffer_length = section->size;
disasInfo.section = section;
disasInfo.buffer = (bfd_byte*) sectionContents;
disassemble_init_for_target(&disasInfo);
DEBUG("disassembling addr range: 0x%x - 0x%x", startAddress, endAddress);
for (unsigned int pc = startAddress; pc < endAddress; /* empty */)
{
printBuffer[0] = '\0';
const int count = disasFunc(pc, &disasInfo);
DEBUG(" pc: %x; count: %d; disas: '%s'", pc, count, printBuffer);
if (count <= 0)
{
break;
}
vector<char*> args;
const InsType insType = DecodeInsString(printBuffer, args);
DEBUG(" pc: 0x%08x; raw: '%s'; instype: %d; %d args:",
pc, printBuffer, insType, args.size());
for (unsigned int j = 0; j < args.size(); ++j)
{
DEBUG(" args[%d]: '%s'", j, args[j]);
}
if (!this->HandleInstruction(pc, count, insType, args))
{
break;
}
pc += count;
}
}
Disassembler::InsType Disassembler::DecodeInsString (char* s, vector<char*>& args)
{
char* pos = strchr(s, ' ');
if (pos == NULL)
{
return INS_UNKNOWN;
}
*pos = '\0';
pos++;
while (*pos == ' ')
{
pos++;
}
if (*pos != '\0')
{
while (1)
{
args.push_back(pos);
pos = strchr(pos, ',');
if (pos == NULL)
{
break;
}
*pos = '\0';
pos++;
}
}
InsType typ = INS_UNKNOWN;
if (strcmp(s, "mov") == 0 && args.size() == 2)
{
typ = INS_MOV;
}
else if (strcmp(s, "push") == 0 && args.size() == 1)
{
typ = INS_PUSH;
}
else if (strcmp(s, "pop") == 0 && args.size() == 1)
{
typ = INS_POP;
}
return typ;
}
void Disassembler::DisasPrintf (void* userData, const char* format, ...)
{
char* outBuffer = (char*)userData;
if (strchr(format, '%') == NULL)
{
strcat(outBuffer, format);
}
else if (strcmp(format, "%s"))
{
va_list args;
va_start(args, format);
const char* stringArg = va_arg(args, char*);
strcat(outBuffer, stringArg);
va_end(args);
}
else
{
va_list args;
va_start(args, format);
char buffer[100];
vsnprintf(buffer, sizeof(buffer), format, args);
strcat(outBuffer, buffer);
va_end(args);
}
}