Search code examples
assemblymemorysegmentation-faultarmqemu

ARM assembly program emulated in qemu throws segfault when trying to write to a valid memory address


I have a small ARM assembly program I'm trying to emulate:

.equ _STACK_SIZE,  64

.align 4
_stack_start:
    .fill _STACK_SIZE, 1, 0
_stack_end:

.text
.global _start
_start:
    ldr     r0, =0x101
    ldr     r1, =0x102
    ldr     r0, =_stack_start
    str     r1, [r0]
    push    {r0,r1}

This program is throwing a seg. fault when it reaches the str line. As far as I understand, that instruction is writing to a valid memory area, which has been allocated with .fill. In addition, the disasembly shows the following:


empty:     file format elf32-littlearm


Disassembly of section .text:

00008000 <_stack_start>:
    ...

00008040 <_start>:
    8040:   e59f0034    ldr r0, [pc, #52]   ; 807c <_start+0x3c>
    8044:   e59f1034    ldr r1, [pc, #52]   ; 8080 <_start+0x40>
    8048:   e59f0034    ldr r0, [pc, #52]   ; 8084 <_start+0x44>
    804c:   e5801000    str r1, [r0]

[...]

so _stack_start is actually there. Why is that memory region not valid?


Solution

  • As explained by @fuz, I needed to add .data before _stack_start:

    .data
    .align 4
    _stack_start:
    [...]