0

This is my source code for linux x86_64:

it's very very simple but i got this error (Segmentation fault (core dumped))

i also change the 'main' with only 'ret' and i got this error again !!!! i don't think it's about 'main' code ...

FORMAT ELF64 EXECUTABLE

SEGMENT READABLE EXECUTABLE

ENTRY main
main:
        mov     rax, 3     ; SYS_CLOSE
        mov     rdi, 0
        syscall    

i'm using './fasm.x64 prog.asm' command to compile this file and my OS => 'Fedora 31 x86_64'

ELHASKSERVERS
  • 195
  • 1
  • 10
  • The problem is that there is nothing your `main` function can return to. You need to explicitly exit the program by invoking the `exit` system call. – fuz Nov 12 '19 at 09:43

1 Answers1

1

your code above tries to close file handle zero, which is dubious at best, and then it proceeds to execute code past the end of the program, so it crashes.

So, what you observe is exactly in line with what your "source code for linux x86_64" does.

(If you are trying to accomplish something other than a crash, then please ask a different question, explaining what you are trying to achieve.)

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • `stdin` (fd 0) is usually open on process startup. You can run `./my_program <&-` in bash to close stdin before execcing a program but normally it's connected to a terminal or /dev/null or something. If you wanted to test the `close` system call, an already-open file descriptor (not handle; this is POSIX not Windows) is a good choice; you can see with `strace` that it succeeds. – Peter Cordes Nov 12 '19 at 14:14