Linker script and stack size ? #69
-
|
Hello, I am currently adapting the software examples you give to my own needs, and while I was looking how everything was made, I noticed two things:
Is all this done on purpose and is it not so important, or something else should be done to correctly map the stack ? Maybe something like giving the remaining space to the heap ? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Hi @VoltXd, The purpose of setting a value different than 0 to STACK_SIZE is to "force" the linker to leave at least this exact amount of free space for stack growth. This is not required, but sometimes you want to make sure there is a decent amount of space free in the stack. For example, if your program has too many nested function calls. If the linker fails to fit a region of STACK_SIZE bytes in memory, you'll get an error message. Example: suppose you have MEMORY_SIZE = 4 MB and STACK_SIZE = 2MB. If you write a program whose .text + .data sections total, say, 3MB, the linker will print an error message saying there is no space left for the stack. As you noted, the .stack region does not "start" at the highest address, nor "ends" at it. This is not an issue because what really counts is the symbol __stack_top, which is set by the linker script to the highest address (equals to MEMORY_SIZE-1). RVX boot code (bootstrap.S) will load the address of this symbol in the stack pointer register before calling the main function of your program. The reason why the .stack region exists is just to make sure there will be enough space for stack growth. If the linker was able to fit this region after the heap, then the runtime requirements of your program were met. The symbols with the start and end of this region are not actually used for addressing the stack: __stack_top is used instead. Likewise, sometimes you want to make sure there will be a certain amount of free space for dynamic memory allocation. In this case you can set HEAP_SIZE to a value other than zero. I hope I could answer you questions. Thanks for using RVX. |
Beta Was this translation helpful? Give feedback.
Hi @VoltXd,
The purpose of setting a value different than 0 to STACK_SIZE is to "force" the linker to leave at least this exact amount of free space for stack growth. This is not required, but sometimes you want to make sure there is a decent amount of space free in the stack. For example, if your program has too many nested function calls. If the linker fails to fit a region of STACK_SIZE bytes in memory, you'll get an error message.
Example: suppose you have MEMORY_SIZE = 4 MB and STACK_SIZE = 2MB. If you write a program whose .text + .data sections total, say, 3MB, the linker will print an error message saying there is no space left for the stack.
As you noted, the .stack region does n…