General question about linker script, heap and stack

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
ExXec
Posts: 83
Joined: Sat Oct 20, 2018 4:02 pm

General question about linker script, heap and stack

Post by ExXec » Fri Mar 08, 2019 9:39 pm

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?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: General question about linker script, heap and stack

Post by dhylands » Fri Mar 08, 2019 10:43 pm

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.

Post Reply