-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
33 lines (30 loc) · 986 Bytes
/
main.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
#include <stdio.h>
#include <string>
#include <fstream>
#include <malloc.h>
#include "arm.hpp"
#include "elf.h"
int main() {
ARM arm;
std::ifstream file("tests/a.out", std::ios::binary | std::ios::ate);
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
char* payload = new char[size]{0};
file.read(payload, size);
Elf32_Ehdr *Eheader = (Elf32_Ehdr*)payload;
Elf32_Phdr *Pheader = (Elf32_Phdr*)(payload + Eheader->e_phoff);
for (uint32_t i = 0; i < Eheader->e_phnum; ++i) {
if (Pheader[i].p_type == PT_LOAD) {
arm.getMpu()->createRegion(Pheader[i].p_paddr, Pheader[i].p_memsz);
void* data = arm.getMpu()->getRegion(Pheader[i].p_paddr)->memory;
memcpy(data, (void*)(payload + Pheader[i].p_offset), Pheader[i].p_filesz);
}
}
arm.getMpu()->createRegion(0xC0000000, 0x10000); // stack region
arm.setRegister(13, 0xC0000000 + 0x10000); // stack pointer
arm.setPc(Eheader->e_entry);
arm.execute();
delete[] payload;
file.close();
return 0;
}