Page 1 of 1

General question about linker script, heap and stack

Posted: Fri Mar 08, 2019 9:39 pm
by ExXec
Hi,
in my linker script heap is defined as follows:

Code: Select all

.heap : {
        __heap_start__ = .;
        end = __heap_start__;
        _heap_start = __heap_start__;
        _end = end;
        __end = end;
        . = . + HEAPSIZE;
        KEEP(*(.heap))
        __heap_end__ = .;
        __HeapLimit = __heap_end__;
        _heap_end = __heap_end__;
    } > REGION_HEAP
And in my startup file there is this line:

Code: Select all

#define  __HEAP_SIZE   0x00002000
#if __HEAP_SIZE > 0
static uint8_t heap[__HEAP_SIZE]   __attribute__ ((aligned(8), used, section(".heap")));
#endif
These two files are not from the same firmware package, I pulled the startup file from CMSIS and the linker file from my MCU verndor.

Am I correct, that the bottom part is redundant in this case and effectively doubles my heap?

What would be the best approach?

Re: General question about linker script, heap and stack

Posted: Fri Mar 08, 2019 10:43 pm
by dhylands
I think what will happen is that the linker script will initially reserve HEAPSIZE in the .heap section. It will then collect all of the .heap sections from the object files (this is the KEEP(*(.heap)) line). If your startup file is the only one that has a .heap section, then that will get added into the .heap section.

So you'll wind up with a final .heap section of HEAPSIZE (from the linker script) plus __HEAPSIZE from the startup file.

You can confirm all of this by looking at the map file.