diff options
Diffstat (limited to 'executor/x86_64/asm.c')
-rw-r--r-- | executor/x86_64/asm.c | 34 |
1 files changed, 16 insertions, 18 deletions
diff --git a/executor/x86_64/asm.c b/executor/x86_64/asm.c index c2b00ef..e29130e 100644 --- a/executor/x86_64/asm.c +++ b/executor/x86_64/asm.c @@ -218,24 +218,22 @@ int asm_execute(Asm *self, u32 offset) { /*asm_print_code_hex(self);*/ /* TODO: Verify if this is valid on all platforms. According to ISO C standard it isn't? */ - *(void**)(&func) = self->code + offset; + *(void**)(&func) = (u8*)self->code + offset; func(); return 0; } /* TODO: See how this can be optimized */ int asm_ensure_capacity(Asm *self, usize size) { - usize current_offset; - current_offset = (u8*)self->code_it - (u8*)self->code; + usize current_offset = (u8*)self->code_it - (u8*)self->code; if(current_offset + size > self->allocated_size) { - void *new_mem; - usize new_size; - new_size = self->allocated_size + am_pagesize(); - new_mem = mmap(NULL, new_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); - if(self->code == MAP_FAILED) + usize new_size = self->allocated_size + am_pagesize(); + void *new_mem = mmap(NULL, new_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if(new_mem == MAP_FAILED) return -errno; am_memcpy(new_mem, self->code, self->allocated_size); + munmap(self->code, self->allocated_size); self->code = new_mem; self->allocated_size = new_size; self->code_it = (u8*)self->code + current_offset; @@ -435,7 +433,7 @@ void asm_callr(Asm *self, Reg64 reg) { /* Note: This is sometimes called with @relative 0 (will print call -5), in which case it's most likely a dummy call until the relative position - is later changed with @asm_override_call_rel32. TODO: Update the ins_end debug print to take that into account somehow + is later changed with @asm_overwrite_call_rel32. TODO: Update the ins_end debug print to take that into account somehow */ void asm_call_rel32(Asm *self, i32 relative) { ins_start(self); @@ -446,8 +444,8 @@ void asm_call_rel32(Asm *self, i32 relative) { ins_end(self, "call 0x%x", relative); } -void asm_override_call_rel32(Asm *self, u32 asm_index, i32 new_relative) { - assert(*(u8*)(self->code + asm_index) == 0xE8); +void asm_overwrite_call_rel32(Asm *self, u32 asm_index, i32 new_relative) { + assert(*((u8*)self->code + asm_index) == 0xE8); new_relative -= 5; /* In x86, the relative position starts from the next instruction */ am_memcpy((u8*)self->code + asm_index + 1, &new_relative, sizeof(new_relative)); } @@ -480,7 +478,7 @@ void asm_sete_r(Asm *self, Reg64 dst) { /* Note: This is sometimes called with @relative INT32_MAX-(2 or 6) (will print jz 0x7ffffff9), in which case it's most likely a dummy - jump until the relative position is later changed with @asm_override_jcc_rel32. + jump until the relative position is later changed with @asm_overwrite_jcc_rel32. TODO: Update the ins_end debug print to take that into account somehow */ void asm_jz(Asm *self, i32 relative) { @@ -503,17 +501,17 @@ void asm_jz(Asm *self, i32 relative) { ins_end(self, "jz 0x%x", relative); } -void asm_override_jcc_rel32(Asm *self, u32 asm_index, i32 new_relative) { +void asm_overwrite_jcc_rel32(Asm *self, u32 asm_index, i32 new_relative) { /* +2 because rel32 variant of the jump instruction opcode is 2 bytes */ - assert(*(u8*)(self->code + asm_index) == 0x0F); - assert(*(u8*)(self->code + asm_index + 1) == 0x84); + assert(*((u8*)self->code + asm_index) == 0x0F); + assert(*((u8*)self->code + asm_index + 1) == 0x84); new_relative -= 6; /* In x86, the relative position starts from the next instruction */ am_memcpy((u8*)self->code + asm_index + 2, &new_relative, sizeof(new_relative)); } /* Note: This is sometimes called with @relative INT32_MAX-(2 or 5) (will print jmp 0x7ffffffa), in which case it's most likely a dummy - jump until the relative position is later changed with @asm_override_jmp_rel32. + jump until the relative position is later changed with @asm_overwrite_jmp_rel32. TODO: Update the ins_end debug print to take that into account somehow */ void asm_jmp(Asm *self, i32 relative) { @@ -535,9 +533,9 @@ void asm_jmp(Asm *self, i32 relative) { ins_end(self, "jmp 0x%x", relative); } -void asm_override_jmp_rel32(Asm *self, u32 asm_index, i32 new_relative) { +void asm_overwrite_jmp_rel32(Asm *self, u32 asm_index, i32 new_relative) { /* +1 to skip instruction opcode */ - assert(*(u8*)(self->code + asm_index) == 0xE9); + assert(*((u8*)self->code + asm_index) == 0xE9); new_relative -= 5; /* In x86, the relative position starts from the next instruction */ am_memcpy((u8*)self->code + asm_index + 1, &new_relative, sizeof(new_relative)); } |