2017-04-24 18:25:30 +03:00
|
|
|
/* Unicorn Emulator Engine */
|
2021-10-03 17:14:44 +03:00
|
|
|
/* By simigo79, 2020 */
|
2017-04-24 18:25:30 +03:00
|
|
|
|
2021-10-03 17:14:44 +03:00
|
|
|
/* Sample code to demonstrate how to emulate PPC code */
|
2017-04-24 18:25:30 +03:00
|
|
|
|
|
|
|
#include <unicorn/unicorn.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
// code to be emulated
|
2021-10-03 17:14:44 +03:00
|
|
|
#define PPC_CODE "\x7F\x46\x1A\x14" // add r26, r6, r3
|
2017-04-24 18:25:30 +03:00
|
|
|
|
|
|
|
// memory address where emulation starts
|
|
|
|
#define ADDRESS 0x10000
|
|
|
|
|
2021-10-29 13:44:49 +03:00
|
|
|
static void hook_block(uc_engine *uc, uint64_t address, uint32_t size,
|
|
|
|
void *user_data)
|
2017-04-24 18:25:30 +03:00
|
|
|
{
|
2021-10-29 13:44:49 +03:00
|
|
|
printf(">>> Tracing basic block at 0x%" PRIx64 ", block size = 0x%x\n",
|
|
|
|
address, size);
|
2017-04-24 18:25:30 +03:00
|
|
|
}
|
|
|
|
|
2021-10-29 13:44:49 +03:00
|
|
|
static void hook_code(uc_engine *uc, uint64_t address, uint32_t size,
|
|
|
|
void *user_data)
|
2017-04-24 18:25:30 +03:00
|
|
|
{
|
2021-10-29 13:44:49 +03:00
|
|
|
printf(">>> Tracing instruction at 0x%" PRIx64
|
|
|
|
", instruction size = 0x%x\n",
|
|
|
|
address, size);
|
2017-04-24 18:25:30 +03:00
|
|
|
}
|
|
|
|
|
2021-10-03 17:14:44 +03:00
|
|
|
static void test_ppc(void)
|
2017-04-24 18:25:30 +03:00
|
|
|
{
|
|
|
|
uc_engine *uc;
|
|
|
|
uc_err err;
|
|
|
|
uc_hook trace1, trace2;
|
|
|
|
|
2021-10-29 13:44:49 +03:00
|
|
|
int r3 = 0x1234; // R3 register
|
|
|
|
int r6 = 0x6789; // R6 register
|
|
|
|
int r26 = 0x8877; // R26 register (result)
|
2017-04-24 18:25:30 +03:00
|
|
|
|
2021-10-03 17:14:44 +03:00
|
|
|
printf("Emulate PPC code\n");
|
2017-04-24 18:25:30 +03:00
|
|
|
|
2021-10-03 17:14:44 +03:00
|
|
|
// Initialize emulator in PPC mode
|
2021-10-29 13:44:49 +03:00
|
|
|
err = uc_open(UC_ARCH_PPC, UC_MODE_PPC32 | UC_MODE_BIG_ENDIAN, &uc);
|
2017-04-24 18:25:30 +03:00
|
|
|
if (err) {
|
2021-10-29 13:44:49 +03:00
|
|
|
printf("Failed on uc_open() with error returned: %u (%s)\n", err,
|
|
|
|
uc_strerror(err));
|
2017-04-24 18:25:30 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// map 2MB memory for this emulation
|
|
|
|
uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL);
|
|
|
|
|
|
|
|
// write machine code to be emulated to memory
|
2021-10-03 17:14:44 +03:00
|
|
|
uc_mem_write(uc, ADDRESS, PPC_CODE, sizeof(PPC_CODE) - 1);
|
2017-04-24 18:25:30 +03:00
|
|
|
|
|
|
|
// initialize machine registers
|
2021-10-03 17:14:44 +03:00
|
|
|
uc_reg_write(uc, UC_PPC_REG_3, &r3);
|
|
|
|
uc_reg_write(uc, UC_PPC_REG_6, &r6);
|
|
|
|
uc_reg_write(uc, UC_PPC_REG_26, &r26);
|
2017-04-24 18:25:30 +03:00
|
|
|
|
|
|
|
// tracing all basic blocks with customized callback
|
|
|
|
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, 1, 0);
|
|
|
|
|
|
|
|
// tracing one instruction at ADDRESS with customized callback
|
|
|
|
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, ADDRESS, ADDRESS);
|
|
|
|
|
|
|
|
// emulate machine code in infinite time (last param = 0), or when
|
|
|
|
// finishing all the code.
|
2021-10-29 13:44:49 +03:00
|
|
|
err = uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(PPC_CODE) - 1, 0, 0);
|
2017-04-24 18:25:30 +03:00
|
|
|
if (err) {
|
|
|
|
printf("Failed on uc_emu_start() with error returned: %u\n", err);
|
2021-10-03 17:14:44 +03:00
|
|
|
return;
|
2017-04-24 18:25:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// now print out some registers
|
|
|
|
printf(">>> Emulation done. Below is the CPU context\n");
|
|
|
|
|
2021-10-03 17:14:44 +03:00
|
|
|
uc_reg_read(uc, UC_PPC_REG_26, &r26);
|
|
|
|
printf(">>> r26 = 0x%x\n", r26);
|
2017-04-24 18:25:30 +03:00
|
|
|
|
2021-10-03 17:14:44 +03:00
|
|
|
// close engine when done
|
2017-04-24 18:25:30 +03:00
|
|
|
uc_close(uc);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv, char **envp)
|
|
|
|
{
|
2021-10-03 17:14:44 +03:00
|
|
|
test_ppc();
|
|
|
|
|
2017-04-24 18:25:30 +03:00
|
|
|
return 0;
|
|
|
|
}
|