flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > Linux > simple sdl game segfaults on 64bit linux, why?

Author
Thread Post new topic Reply to topic
ginere



Joined: 15 Oct 2017
Posts: 3
simple sdl game segfaults on 64bit linux, why?

Code:

format ELF64
public main
extrn SDL_Init

section '.text' executable
main:
    mov edi,$30
    call SDL_Init
    ret

section '.data' writeable





Code:

fasm main.asm
gcc main.o `sdl2-config --libs --cflags` -o main




Starting ./main gives me "Segmentation fault". Why?
Post 23 Oct 2017, 14:36
View user's profile Send private message Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 868
I don't think you can use 'ret' on Linux to exit from the entry point main, never seen it that way. Call exit instead:

Code:
mov eax60
syscall

Also you might have to align the stack to 16 bytes before calling SDL_Init.
Post 23 Oct 2017, 15:08
View user's profile Send private message Reply with quote
ginere



Joined: 15 Oct 2017
Posts: 3

Furs wrote:
Also you might have to align the stack to 16 bytes before calling SDL_Init.



Thank you. That did the trick. Stack was aligned to $08 so I did sub rsp, $08 before SDL_Init, and after SDL_Init I put add rsp, $08 to get old rsp back.
Post 23 Oct 2017, 16:39
View user's profile Send private message Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 868
You should use a dummy push instead (e.g. push rax), it's much smaller (1 byte vs 4) and it's entry point anyway. Wink

I know that Linux is probably going to always give this (mis)alignment on entry point just to be compatible, but I'm just saying that libc (the library that hijacks the entry point of all C apps that use it on Linux) realigns the stack on entry point (as if it's unreliable). Paranoia? Razz But I'd err on the side of caution and just use a "and rsp, -16".
Post 23 Oct 2017, 20:17
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >

Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2005 phpBB Group.

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2016, Tomasz Grysztar.