Where exactly is the stack located? (ARM)

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
asdfasdfasdf
Posts: 2
Joined: Sun Nov 29, 2020 8:49 am

Where exactly is the stack located? (ARM)

Post 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?
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

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

Post 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
Post Reply