unicorn/samples/sample_mips.c

149 lines
4.4 KiB
C
Raw Normal View History

2015-08-21 10:04:50 +03:00
/* Unicorn Emulator Engine */
/* By Nguyen Anh Quynh, 2015 */
/* Sample code to demonstrate how to emulate Mips code (big endian) */
#include <unicorn/unicorn.h>
#include <string.h>
2015-08-21 10:04:50 +03:00
// code to be emulated
#define MIPS_CODE_EB "\x34\x21\x34\x56" // ori $at, $at, 0x3456;
#define MIPS_CODE_EL "\x56\x34\x21\x34" // ori $at, $at, 0x3456;
// memory address where emulation starts
#define ADDRESS 0x10000
static void hook_block(uc_engine *uc, uint64_t address, uint32_t size, void *user_data)
2015-08-21 10:04:50 +03:00
{
printf(">>> Tracing basic block at 0x%"PRIx64 ", block size = 0x%x\n", address, size);
}
static void hook_code(uc_engine *uc, uint64_t address, uint32_t size, void *user_data)
2015-08-21 10:04:50 +03:00
{
printf(">>> Tracing instruction at 0x%"PRIx64 ", instruction size = 0x%x\n", address, size);
}
static void test_mips_eb(void)
{
uc_engine *uc;
2015-08-21 10:04:50 +03:00
uc_err err;
uc_hook trace1, trace2;
2015-08-21 10:04:50 +03:00
int r1 = 0x6789; // R1 register
printf("Emulate MIPS code (big-endian)\n");
// Initialize emulator in MIPS mode
err = uc_open(UC_ARCH_MIPS, UC_MODE_MIPS32 + UC_MODE_BIG_ENDIAN, &uc);
2015-08-21 10:04:50 +03:00
if (err) {
printf("Failed on uc_open() with error returned: %u (%s)\n",
err, uc_strerror(err));
return;
}
// map 2MB memory for this emulation
uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL);
2015-08-21 10:04:50 +03:00
// write machine code to be emulated to memory
uc_mem_write(uc, ADDRESS, MIPS_CODE_EB, sizeof(MIPS_CODE_EB) - 1);
2015-08-21 10:04:50 +03:00
// initialize machine registers
uc_reg_write(uc, UC_MIPS_REG_1, &r1);
2015-08-21 10:04:50 +03:00
// tracing all basic blocks with customized callback
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, 1, 0);
2015-08-21 10:04:50 +03:00
// tracing one instruction at ADDRESS with customized callback
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, ADDRESS, ADDRESS);
2015-08-21 10:04:50 +03:00
// emulate machine code in infinite time (last param = 0), or when
// finishing all the code.
err = uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(MIPS_CODE_EB) - 1, 0, 0);
2015-08-21 10:04:50 +03:00
if (err) {
printf("Failed on uc_emu_start() with error returned: %u (%s)\n", err, uc_strerror(err));
}
// now print out some registers
printf(">>> Emulation done. Below is the CPU context\n");
uc_reg_read(uc, UC_MIPS_REG_1, &r1);
2015-08-21 10:04:50 +03:00
printf(">>> R1 = 0x%x\n", r1);
uc_close(uc);
2015-08-21 10:04:50 +03:00
}
static void test_mips_el(void)
{
uc_engine *uc;
2015-08-21 10:04:50 +03:00
uc_err err;
uc_hook trace1, trace2;
2015-08-21 10:04:50 +03:00
int r1 = 0x6789; // R1 register
printf("===========================\n");
printf("Emulate MIPS code (little-endian)\n");
// Initialize emulator in MIPS mode
2016-01-23 04:08:49 +03:00
err = uc_open(UC_ARCH_MIPS, UC_MODE_MIPS32 + UC_MODE_LITTLE_ENDIAN, &uc);
2015-08-21 10:04:50 +03:00
if (err) {
printf("Failed on uc_open() with error returned: %u (%s)\n",
err, uc_strerror(err));
return;
}
// map 2MB memory for this emulation
uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL);
2015-08-21 10:04:50 +03:00
// write machine code to be emulated to memory
uc_mem_write(uc, ADDRESS, MIPS_CODE_EL, sizeof(MIPS_CODE_EL) - 1);
2015-08-21 10:04:50 +03:00
// initialize machine registers
uc_reg_write(uc, UC_MIPS_REG_1, &r1);
2015-08-21 10:04:50 +03:00
// tracing all basic blocks with customized callback
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, 1, 0);
2015-08-21 10:04:50 +03:00
// tracing one instruction at ADDRESS with customized callback
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, ADDRESS, ADDRESS);
2015-08-21 10:04:50 +03:00
// emulate machine code in infinite time (last param = 0), or when
// finishing all the code.
err = uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(MIPS_CODE_EL) - 1, 0, 0);
2015-08-21 10:04:50 +03:00
if (err) {
printf("Failed on uc_emu_start() with error returned: %u (%s)\n", err, uc_strerror(err));
}
// now print out some registers
printf(">>> Emulation done. Below is the CPU context\n");
uc_reg_read(uc, UC_MIPS_REG_1, &r1);
2015-08-21 10:04:50 +03:00
printf(">>> R1 = 0x%x\n", r1);
uc_close(uc);
2015-08-21 10:04:50 +03:00
}
int main(int argc, char **argv, char **envp)
{
// dynamically load shared library
#ifdef DYNLOAD
2015-12-09 19:53:48 +03:00
if (!uc_dyn_load(NULL, 0)) {
printf("Error dynamically loading shared library.\n");
printf("Please check that unicorn.dll/unicorn.so is available as well as\n");
printf("any other dependent dll/so files.\n");
printf("The easiest way is to place them in the same directory as this app.\n");
return 1;
}
#endif
test_mips_eb();
2015-08-21 10:04:50 +03:00
test_mips_el();
// dynamically free shared library
#ifdef DYNLOAD
uc_dyn_free();
#endif
2015-08-21 10:04:50 +03:00
return 0;
}