Please explain __StackLimit & __HeapLimit gcc.ld

Asked by franchan

This dummy section at the end of all other sections helps me to verify there's enough remaining space in RAM to avoid stack would run into heap by setting appropriate values in MIN_HEAP_SIZE and MIN_STACK_SIZE
    ._user_heap_stack : {
        . = ALIGN(4);
        . = . + _MIN_HEAP_SIZE;
        . = . + _MIN_STACK_SIZE;
        . = ALIGN(4);
    } >RAM

gcc.ld seems to use a different mechanism that I don't understand (note I stripped some lines of what I believe is not relevant for my question/this discussion).

How do __HeapLimit and __StackLimit get their (max-) value so that the ASSERT might go off and warn during compile & link time?

I mean, the compiler nor linker will ever put things inside *(.heap*) or *(.stack*), right? So that the size of these sections at link time is always zero. Thus __StackLimit remains __StackTop, the very end of RAM. If none of the section before .heap fill up RAM, this assert would not trigger? What am I missing?

    .heap (COPY): {
        *(.heap*)
        __HeapLimit = .;
    } > RAM

    .stack_dummy (COPY): {
        *(.stack*)
    } > RAM

 /* Set stack top to end of RAM, and stack limit move down by size of stack_dummy section */
 __StackTop = ORIGIN(RAM) + LENGTH(RAM);
 __StackLimit = __StackTop - SIZEOF(.stack_dummy);
 PROVIDE(__stack = __StackTop);

 /* Check if data + heap + stack exceeds RAM limit */
 ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed with stack")

Question information

Language:
English Edit question
Status:
Solved
For:
GNU Arm Embedded Toolchain Edit question
Assignee:
No assignee Edit question
Solved by:
franchan
Solved:
Last query:
Last reply:
Revision history for this message
Launchpad Janitor (janitor) said :
#1

This question was expired because it remained in the 'Open' state without activity for the last 15 days.

Revision history for this message
Thomas Preud'homme (thomas-preudhomme) said :
#2

Hi,

I don't think you can know the size of the stack or the heap as both of these are dynamic values. The size of heap depends of the size of stack and vice versa since both grow towards each other. The limit is reached whenever they encounter each other. If you know how much stack you are going to use you could deduce the size of the heap by doing stack top - heap start (which is the __HeapLimit, however confusing that is) - needed stack size for your program.

Hope this helps.

Best regards.

Revision history for this message
Thomas Preud'homme (thomas-preudhomme) said :
#3

Last comment explained a possible solution.

Revision history for this message
franchan (francis-meyvis) said :
#4

Hello Mr Preud'homme,

With your explanation I can then concluded that the code in gcc.ld (./src/samples/ldscripts/gcc.ld)
is wrong or at least confusing (for a beginner) giving false confidence.

ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed with stack")
will never trigger unless __HeapLimit is set to span the whole RAM.

I therefor think it would be better to define _MIN_HEAP_SIZE and _MIN_STACK_SIZE in the ld script.
If porting to a platform with less RAM, at least such mechanism might trigger the assert.

Thanks for taking the time to answer ...
Naively I thought there was some secret working "behind the scenes" that made this ASSERT trigger.

Thank you,
francis