linux - main and stack alignment -
linux - main and stack alignment -
i have function prints text , floating point number. here version not utilize main
extern printf extern _exit section .data hello: db 'hello world! %f',10,0 pi: dq 3.14159 section .text global _start _start: xor eax, eax lea rdi, [rel hello] movsd xmm0, [rel pi] mov eax, 1 phone call printf mov rax, 0 jmp _exit i assemble , link this
nasm -felf64 hello.asm ld hello.o -dynamic-linker /lib64/ld-linux-x86-64.so.2 -lc -melf_x86_64 this runs fine. however, want using main.
global main extern printf section .data hello: db 'hello world! %f',10,0 pi: dq 3.14159 section .text main: sub rsp, 8 xor eax, eax lea rdi, [rel hello] movsd xmm0, [rel pi] mov eax, 1 phone call printf mov rax, 0 add together rsp, 8 ret i assembly , link this
nasm -felf64 hello_main.asm gcc hello_main.o this runs fine well. however, had subtract 8 bytes stack pointer before calling printf , add together 8 bytes stack pointer after otherwise segmentation fault.
looking @ stack pointer see without using main it's 16-byte aligned main it's 8 byte aligned. fact 8 bytes has subtracted , added says it's 8-byte aligned , never 16-byte aligned (unless misunderstand something). why this? thought x86_64 code assume stack 16-byte aligned (at to the lowest degree standard library function calls think includes main).
according abi, stack pointer + 8 should kept 16 byte aligned upon entry functions. reason have subtract 8 call places 8 bytes of homecoming address on stack, thereby violating constraint. have create sure total stack pointer motion multiple of 16, including homecoming address. stack pointer needs moved multiple of 16 + 8 leave room homecoming address.
as _start, don't think can rely on working without manual alignment either. happens in case works due things on stack.
linux gcc assembly nasm x86-64
Comments
Post a Comment