I am in the process of converting one of my 32 bit MASM programs which uses the Win32 API, to Linux using libcURL, cstdlib, and GTK+3 and of course NASM (at about 1500 lines of 14,000 converted so have a ways to go)
Red Zone - We can only use this if our function is a leaf function (does not call another function), what happens to the red zone if we use it in a non leaf function? I am guessing since functions don't need an epilogue to save rsp, our red zone gets over written.
Alignment - at the start of every function we need to be 16 byte aligned; fine, call pushes 8 bits so we just need to sub rsp, 8 at the beginning of the function and add rsp, 8 at the end. Of course if we push one register, we don't need to sub rsp.
Here is the thing, why does printf (or any other vararg function) segfault if we don't align the stack and zero rax if we don't use xmm regs?
This will segfault:
SomeFunction:
mov rsi, somestring
mov rdi, fmtstr
call printf
ret
This won't:
SomeFunction:
mov rsi, somestring
mov rdi, fmtstr
mov rax, 0
call printf
ret
This won't segfault either:
SomeFunctionAligned:
sub rsp, 8
mov rsi, somestring
mov rdi, fmtstr
call printf
add rsp, 8
ret
nor will this:
SomeFunctionAligned:
sub rsp, 8
mov rsi, somestring
mov rdi, fmtstr
mov rax, 0
call printf
add rsp, 8
ret