samples: code style for sample_x86_32_gdt_and_seg_regs.c
This commit is contained in:
@ -103,7 +103,8 @@ do { \
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void hook_mem(uc_engine *uc, uc_mem_type type, uint64_t address, int size, int64_t value, void *user_data) {
|
||||
static void hook_mem(uc_engine *uc, uc_mem_type type, uint64_t address, int size, int64_t value, void *user_data)
|
||||
{
|
||||
switch(type) {
|
||||
case UC_MEM_WRITE:
|
||||
printf("mem write at 0x%"PRIx64 ", size = %u, value = 0x%"PRIx64 "\n", address, size, value);
|
||||
@ -112,12 +113,14 @@ void hook_mem(uc_engine *uc, uc_mem_type type, uint64_t address, int size, int64
|
||||
}
|
||||
}
|
||||
|
||||
void hook_code(uc_engine *uc, uint64_t address, uint32_t size, void *user_data) {
|
||||
static void hook_code(uc_engine *uc, uint64_t address, uint32_t size, void *user_data)
|
||||
{
|
||||
printf("Executing at 0x%"PRIx64 ", ilen = 0x%x\n", address, size);
|
||||
}
|
||||
|
||||
//VERY basic descriptor init function, sets many fields to user space sane defaults
|
||||
void init_descriptor(struct SegmentDescriptor *desc, uint32_t base, uint32_t limit, uint8_t is_code) {
|
||||
static void init_descriptor(struct SegmentDescriptor *desc, uint32_t base, uint32_t limit, uint8_t is_code)
|
||||
{
|
||||
desc->desc = 0; //clear the descriptor
|
||||
desc->base0 = base & 0xffff;
|
||||
desc->base1 = (base >> 16) & 0xff;
|
||||
@ -138,7 +141,9 @@ void init_descriptor(struct SegmentDescriptor *desc, uint32_t base, uint32_t lim
|
||||
desc->system = 1; //code or data
|
||||
}
|
||||
|
||||
void hex_dump(unsigned char *ptr, unsigned int len) {
|
||||
/*
|
||||
static void hex_dump(unsigned char *ptr, unsigned int len)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < len; i++) {
|
||||
if (i != 0 && (i & 0xf) == 0) {
|
||||
@ -148,23 +153,25 @@ void hex_dump(unsigned char *ptr, unsigned int len) {
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
*/
|
||||
|
||||
static void gdt_demo() {
|
||||
static void gdt_demo()
|
||||
{
|
||||
uc_engine *uc;
|
||||
uc_hook hook1, hook2;
|
||||
uc_err err;
|
||||
uint8_t buf[128];
|
||||
uc_x86_mmr gdtr;
|
||||
|
||||
/*
|
||||
bits 32
|
||||
/*
|
||||
bits 32
|
||||
|
||||
push dword 0x01234567
|
||||
push dword 0x89abcdef
|
||||
push dword 0x01234567
|
||||
push dword 0x89abcdef
|
||||
|
||||
mov dword [fs:0], 0x01234567
|
||||
mov dword [fs:4], 0x89abcdef
|
||||
*/
|
||||
mov dword [fs:0], 0x01234567
|
||||
mov dword [fs:4], 0x89abcdef
|
||||
*/
|
||||
|
||||
const uint8_t code[] = "\x68\x67\x45\x23\x01\x68\xef\xcd\xab\x89\x64\xc7\x05\x00\x00\x00\x00\x67\x45\x23\x01\x64\xc7\x05\x04\x00\x00\x00\xef\xcd\xab\x89";
|
||||
const uint64_t code_address = 0x1000000;
|
||||
@ -190,10 +197,10 @@ mov dword [fs:4], 0x89abcdef
|
||||
init_descriptor(&gdt[17], 0, 0xfffff000, 0); //ring 0 data
|
||||
gdt[17].dpl = 0; //set descriptor privilege level
|
||||
|
||||
/*
|
||||
/*
|
||||
fprintf(stderr, "GDT: \n");
|
||||
hex_dump((unsigned char*)gdt, 31 * sizeof(struct SegmentDescriptor));
|
||||
*/
|
||||
*/
|
||||
|
||||
// Initialize emulator in X86-32bit mode
|
||||
err = uc_open(UC_ARCH_X86, UC_MODE_32, &uc);
|
||||
@ -273,12 +280,12 @@ mov dword [fs:4], 0x89abcdef
|
||||
assert(memcmp(buf, "\x67\x45\x23\x01\xef\xcd\xab\x89", 8) == 0);
|
||||
|
||||
uc_close(uc);
|
||||
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
gdt_demo();
|
||||
|
||||
fprintf(stderr, "success\n");
|
||||
|
Reference in New Issue
Block a user