The following program crashes (Segmentation fault):
//something.c
int somefunc3();
void somefunc2();
void* globalptr;
void somefunc1(void* regs)
{
globalptr = regs;
somefunc2();
}
int foo()
{
return somefunc3();
}
int main(void)
{
show_all_registers();
foo();
show_all_registers();
}
//something1.asm
extern _somefunc1
global _somefunc2
global _somefunc3
section .text
%macro RESTORE_REGISTERS 0
pop rcx
pop rcx
pop rcx
pop rcx
pop rcx
pop rcx
pop rcx
pop rcx
pop rcx
pop rcx
pop rcx
pop rcx
pop rcx
pop rcx
pop rcx
pop rcx
%endmacro
%macro SAVE_REGISTERS 0
push rcx
push rcx
push rcx
push rcx
push rcx
push rcx
push rcx
push rcx
push rcx
push rcx
push rcx
push rcx
push rcx
push rcx
push rcx
push rcx
%endmacro
_somefunc3:
push rbp
mov rbp, rsp
SAVE_REGISTERS
mov rdi, rsp
sub rsp,8
call _somefunc1
add rsp,8
pop rbp
ret
_somefunc2:
push rbp
mov rbp, rsp
RESTORE_REGISTERS
pop rbp
ret
few notes:
Please don't try to understand what this program does because you won't find anything that makes sense. This is just a user mode app which I created in order to understand something.
show_all_registers is just a function that prints out to the screen all of the 64 bit registers.
Here's what happens before it crashes:
64 Bit registers:
RAX=10767ad00, RCX=1, RDX=10767ab70, RBX=0, RSP=7fff58585bd0, RBP=7fff58585bd0, RSI=20000000200, RDI=7
Segmentation fault: 11
Using GDB it seems that the crash occurs on somefunc2 (When restoring the registers)
I think that it has something to do with stack alignment or the epilogues & epilogues I wrote for the ASM functions. Still kinda newbie so it is most likely something silly.
thanks