0

I'm trying to write a compiler but I am not very good at it. It compiles / assembles without any errors but it gives me segfault at runtime.

Here is how I assemble my assembly source code (on ubuntu linux) :

nasm -f elf64 assembly.asm ld -s -o assembly assembly.o --entry entrypoint ./assembly ; echo $? 

Here is my assembly source code:

global entrypoint entrypoint: call main main: mov eax, 0 ret 

Here is what it gives to me :

Segmentation fault (core dumped) ./assembly 
0

1 Answer 1

3

After the ret returns after call main, control ends up at the instruction after the call, which happens to be the beginning of main again. After the mov instruction runs a second time, next is ret again, but now there's no corresponding call, causing the instruction pointer to be pointing to whatever bytes just happened to be next on the stack. This is almost certainly not going to be executable memory, so a segfault occurs when it tries to execute there.

Sign up to request clarification or add additional context in comments.

1 Comment

In the x86-64 System V ABI, rsp on process entry (_start:) points at argc (a small integer, not a valid return address). Unlike Windows, there's no return address anywhere that you can use, you need to make an _exit or exit_group system call.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.