Home > OS >  Segmentation fault in x86_64 assembly
Segmentation fault in x86_64 assembly

Time:09-22

Why do I get a segmentation fault?

I'm using nasm -f elf64 t.asm -o t.o ld t.o -o t to compile on linux.

I've done all I can think of.

section .data:
  variable_int db 1
  variable_string db "yaaaa", 10
section .text:
  global _start
_start:
  mov rax, 1
  mov rdi, 1
  mov rsi, variable_string
  mov rdx, 14
  syscall
  mov rax, 60
  mov rdi, 0
  syscall

CodePudding user response:

section .data:
section .text:

Omit the colons. A section directive is not a label, and the colon is parsed as part of the section name. This causes your data to be put in a section called .data: whereas the linker is expecting a section called .data without the colon. This may cause the section to be given the wrong permissions (e.g. a .text: section which is not executable).

Also:

mov rdx, 14

This parameter is the length of the data to be written, and your string is not 14 bytes long; it is only 6. This may result in extra garbage being written (which may or may not be visible characters) or potentially a failure of the system call if this runs into unmapped memory.

  • Related