Page 1 of 1

Where exactly is the stack located? (ARM)

Posted: Mon Dec 07, 2020 11:23 am
by asdfasdfasdf
Looking at the boot.S file in the Raspberry Pi Bare Bones tutorial, it seems like the stack pointer register is set to the start of kernel_main(), but what happens to it from there? In the linker.ld file, __end is set to where the .elf ends, but does this include memory for the stack? i.e. when the program is running, are stack variables being stored somewhere between 0x0 and __end, or somewhere else in memory?

Re: Where exactly is the stack located? (ARM)

Posted: Mon Dec 07, 2020 1:39 pm
by bzt
asdfasdfasdf wrote:Looking at the boot.S file in the Raspberry Pi Bare Bones tutorial, it seems like the stack pointer register is set to the start of kernel_main()
Nope. The firmware loads the kernel raw binary at 0x8000 (AArch32) or 0x80000 (AArch64). That's going to be the same address as _start (and not kernel_main), and the stack is set to that too, because it's growing downwards.
asdfasdfasdf wrote:In the linker.ld file, __end is set to where the .elf ends, but does this include memory for the stack?
No. That _end label does not end where the elf ends (there's also a bss section), and it does not contain the stack either.
asdfasdfasdf wrote:i.e. when the program is running, are stack variables being stored somewhere between 0x0 and __end, or somewhere else in memory?
Here's a memory map:

Code: Select all

+------------+ 0xFFF..F top of memory
|    ...     |
+------------+ _end, _bss_end
| bss        |
+------------+ _bss_start, _data_end                             \
| data       |                                                   |
+------------+ _rodata_end, _data_start                          |
| rodata     |                                                   | kernel.img
+------------+ _text_end, _rodata_start                          |
| text       |                                                   |
+------------+ _text_start, _start, LOADER_ADDR, 0x8000/0x80000  /
| stack      |
|    ...     |
+------------+ 0
In short, kernel.img is loaded at LOADER_ADDR to _data_end, the bss section is AFTER that (ending at _end), and the stack is BEFORE that.

Cheers,
bzt