So far we have only used the data section of our programs to define data and to read that data at runtime. However, we can also write to the data section! Try running the following code through the debugger:
.section .data
var:
.byte 5
.section .text
.globl _start
_start:
movq $10, var
movq $60, %rax
movq $0, %rdi
syscall
Using the techniques we learned in a previous post, you will see that the line
movq $10, var
changes the value of the byte of memory named var
from 5 to 10.
If you want to define read only data in your binary, you can do so with the .rodata
section. This works just like the data section. However, if you try to write to the memory here, you will get a seg-fault.