forked from lmcad-unicamp/oi-dbt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
machine.cpp
349 lines (276 loc) · 9.05 KB
/
machine.cpp
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
#include <elfio/elfio.hpp>
#include <machine.hpp>
#include <cstring>
using namespace dbt;
//#define DEBUG
#ifdef DEBUG
#define CORRECT_ASSERT() assert(Addr>=DataMemOffset && "Error on correcting address. Data memory offset Value < 0!")
#else
#define CORRECT_ASSERT()
#endif //DEBUG
//#define MAX_ARGUMENT_SIZE 1024 * 1024 /* 1mb */
union HalfUn {
char asC_[2];
uint16_t asH_;
};
void copystr(char* Target, const char* Source, uint32_t Size) {
for (uint32_t i = 0; i < Size; ++i)
Target[i] = Source[i];
}
void Machine::setCodeMemory(uint32_t StartAddress, uint32_t Size, const char* CodeBuffer) {
CodeMemOffset = StartAddress;
CodeMemory = uptr<Word[]>(new Word[Size]);
CodeMemLimit = Size + CodeMemOffset;
for (uint32_t i = 0; i < Size; i++) {
Word Bytes = {CodeBuffer[i], CodeBuffer[i+1], CodeBuffer[i+2], CodeBuffer[i+3]};
CodeMemory[i] = Bytes;
}
}
void Machine::allocDataMemory(uint32_t Offset, uint32_t TotalSize) {
DataMemTotalSize = TotalSize;
DataMemOffset = Offset;
DataMemLimit = Offset + TotalSize;
DataMemory = std::unique_ptr<char[]>(new char[TotalSize]);
}
void Machine::addDataMemory(uint32_t StartAddress, uint32_t Size, const char* DataBuffer) {
uint32_t Offset = StartAddress - DataMemOffset;
DataMemLimit += Size; //Ops, allocated memory stills the same, no more allocation is done and DataMemLimit is updated!
copystr(DataMemory.get() + Offset, DataBuffer, Size);
}
int Machine::setCommandLineArguments(std::string parameters) {
unsigned int sp = getRegister(29), totalSize=0, offset;
std::istringstream iss(parameters);
std::vector<std::string> argv(std::istream_iterator<std::string>{iss}, std::istream_iterator<std::string>());
argv.insert(argv.cbegin(), BinPath);
for (auto argument : argv)
totalSize += argument.length()+1;
offset = DataMemTotalSize-totalSize-1;
setMemValueAt(sp, (uint32_t) argv.size());
for(auto argument : argv) {
sp += 4; //Subtract stack pointer
unsigned argSize = argument.length()+1; //Argument size
copystr(DataMemory.get() + offset, argument.c_str(), argSize); //Put argument in sp+4+size(arg[0..])=offset
setMemValueAt(sp, (uint32_t) offset+DataMemOffset); //Put offset in sp
offset += argSize; //Increment offset by argument Size
}
setMemValueAt(sp+4, 0);
copystr(DataMemory.get() + offset, "\0", 1);
return 0;
}
uint32_t Machine::getPC() {
return PC;
}
uint32_t Machine::getLastPC() {
return LastPC;
}
void Machine::incPC() {
PC += 4;
}
void Machine::setPC(uint32_t NewPC) {
LastPC = PC;
PC = NewPC;
#ifdef PRINTREG
std::cerr << "LastPC= " << LastPC << "; NewPC= " << PC << "; ";
#endif
}
dbt::Word Machine::getInstAt(uint32_t Addr) {
return CodeMemory[Addr - CodeMemOffset];
}
dbt::Word Machine::getInstAtPC() {
return getInstAt(PC);
}
dbt::Word Machine::getNextInst() {
++PC;
return getInstAtPC();
}
void Machine::setMemByteAt(uint32_t Addr, uint8_t Value) {
uint32_t CorrectAddr = Addr - DataMemOffset;
CORRECT_ASSERT();
#ifdef PRINTREG
std::cerr << "MEM[" << std::hex << CorrectAddr << "]=" << (uint32_t) Value << "; (uint8_t); ";
#endif
DataMemory[CorrectAddr] = Value;
}
uint8_t Machine::getMemByteAt(uint32_t Addr) {
uint32_t CorrectAddr = Addr - DataMemOffset;
CORRECT_ASSERT();
return DataMemory[CorrectAddr];
}
uint16_t Machine::getMemHalfAt(uint32_t Addr) {
uint32_t CorrectAddr = Addr - DataMemOffset;
CORRECT_ASSERT();
HalfUn Half = {DataMemory[CorrectAddr], DataMemory[CorrectAddr+1]};
return Half.asH_;
}
dbt::Word Machine::getMemValueAt(uint32_t Addr) {
uint32_t CorrectAddr = Addr - DataMemOffset;
Word Bytes;
CORRECT_ASSERT();
Bytes.asI_ = *((uint32_t*)(DataMemory.get() + CorrectAddr));
return Bytes;
}
void Machine::setMemValueAt(uint32_t Addr, uint32_t Value) {
uint32_t CorrectAddr = Addr - DataMemOffset;
CORRECT_ASSERT();
#ifdef PRINTREG
std::cerr << "MEM[" << std::hex << CorrectAddr << "]=" << Value << "; (uint32_t); ";
#endif
*((uint32_t*)(DataMemory.get() + CorrectAddr)) = Value;
}
uint32_t Machine::getNumInst() {
return (CodeMemLimit - CodeMemOffset)/4;
}
uint32_t Machine::getCodeStartAddrs() {
return CodeMemOffset;
}
uint32_t Machine::getCodeEndAddrs() {
return CodeMemLimit;
}
uint32_t Machine::getDataMemOffset() {
return DataMemOffset;
}
int32_t Machine::getRegister(uint16_t R) {
#ifdef PRINTREG
std::cerr << "GET R[" << std::dec << R << "]: " << std::hex << Register[R] << "; ";
#endif
return Register[R];
}
float Machine::getFloatRegister(uint16_t R) {
return ((float*) Register)[R + 66];
}
double Machine::getDoubleRegister(uint16_t R) {
return ((double*)Register)[R + 65];
}
void Machine::setRegister(uint16_t R, int32_t V) {
#ifdef PRINTREG
std::cerr << "R[" << std::dec << R << "] = " << std::hex << V << "; ";
#endif
Register[R] = V;
}
void Machine::setFloatRegister(uint16_t R, float V) {
((float*)Register)[R + 66] = V;
}
void Machine::setDoubleRegister(uint16_t R, double V) {
((double*)Register)[R + 65] = V;
}
int32_t* Machine::getRegisterPtr() {
return Register;
}
char* Machine::getByteMemoryPtr() {
return DataMemory.get();
}
uint32_t* Machine::getMemoryPtr() {
return (uint32_t*) DataMemory.get();
}
bool Machine::isOnNativeExecution() {
return OnNativeExecution;
}
uint32_t Machine::getRegionBeingExecuted() {
return RegionBeingExecuted;
}
void Machine::setOnNativeExecution(uint32_t EntryRegionAddrs) {
OnNativeExecution = true;
RegionBeingExecuted = EntryRegionAddrs;
}
void Machine::setOffNativeExecution() {
OnNativeExecution = false;
}
uint32_t Machine::findMethod(uint32_t Addr) {
for (auto Method : Symbolls)
if (Method.first < Addr && Method.second.second > Addr)
return Method.first;
return 0;
}
bool Machine::isMethodEntry(uint32_t Addr) {
return Symbolls.count(Addr) != 0;
}
uint32_t Machine::getMethodEnd(uint32_t Addr) {
return Symbolls[Addr].second;
}
std::string Machine::getMethodName(uint32_t Addr) {
return Symbolls[Addr].first;
}
std::vector<uint32_t> Machine::getVectorOfMethodEntries() {
std::vector<uint32_t> R;
for (auto KV : Symbolls)
R.push_back(KV.first);
return R;
}
using namespace ELFIO;
void Machine::reset() {
loadELF(BinPath);
}
int Machine::loadELF(const std::string ElfPath) {
BinPath = ElfPath;
elfio reader;
if (!reader.load(ElfPath))
return 0;
Elf_Half sec_num = reader.sections.size();
uint32_t TotalDataSize = 0;
uint32_t AddressOffset = 0;
bool Started = false;
bool First = false;
for (int i = 0; i < sec_num; ++i) {
section* psec = reader.sections[i];
if (Started && (psec->get_flags() & 0x2) != 0) {
TotalDataSize += psec->get_size();
if (!First) {
AddressOffset = psec->get_address();
First = true;
}
}
if (psec->get_name() == ".text")
Started = true;
}
allocDataMemory(AddressOffset, (TotalDataSize + stackSize + heapSize) + (4 - (TotalDataSize + stackSize + heapSize) % 4));
std::unordered_map<uint32_t, std::string> SymbolNames;
std::set<uint32_t> SymbolStartAddresses;
Started = false;
for (int i = 0; i < sec_num; ++i) {
section* psec = reader.sections[i];
if (Started && (psec->get_flags() & 0x2) != 0 && psec->get_data() != nullptr) {
addDataMemory(psec->get_address(), psec->get_size(), psec->get_data());
}
if (psec->get_name() == ".text") {
setCodeMemory(psec->get_address(), psec->get_size(), psec->get_data());
SymbolStartAddresses.insert(psec->get_address() + psec->get_size());
Started = true;
}
if (psec->get_name() == ".symtab") {
const symbol_section_accessor symbols(reader, psec);
std::string name = "";
Elf64_Addr value = 0;
Elf_Xword size;
unsigned char bind;
unsigned char type = 0;
Elf_Half section_index;
unsigned char other;
for ( unsigned int j = 0; j < symbols.get_symbols_num(); ++j ) {
symbols.get_symbol( j, name, value, size, bind, type, section_index, other );
if (type == 0 && name != "" && value != 0 && value < CodeMemLimit) {
SymbolStartAddresses.insert(value);
SymbolNames[value] = name;
}
}
}
}
for (auto I = SymbolStartAddresses.begin(); I != SymbolStartAddresses.end(); ++I)
Symbolls[*I] = {SymbolNames[*I], *SymbolStartAddresses.upper_bound(*I)};
for (int i = 0; i < 258; i++)
Register[i] = 0;
uint32_t StackAddr = DataMemLimit-stackSize/4;
setRegister(29, StackAddr + (4 - StackAddr%4)); //StackPointer
setRegister(30, StackAddr + (4 - StackAddr%4)); //StackPointer
setPC(reader.get_entry());
return 1;
}
//#ifdef DEBUG
void Machine::dumpRegisters(void) {
std::cerr << std::endl << "PC: " << std::hex << PC << "; \n";
std::cerr << "(int32_t [258]) = {" << std::endl;
for (int i = 0; i<258; ++i) {
std::cerr << " [" << std::dec << i << "] = " << "0x" << std::setw(8) << std::setfill('0') << std::hex << Register[i] << std::endl;
}
std::cerr << "}" << std::endl;
}
//#endif