I need to add a small heap to use standard library functions on a TM4C ARM microcontroller (_sbrk
requires the end
symbol).
This is my linker script (came with a microcontroller demo):
/* Entry Point */
ENTRY(Reset_Handler)
HEAP_SIZE = 1024;
MEMORY
{
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x00100000
SRAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00040000
}
SECTIONS
{
.text :
{
_text = .;
KEEP(*(.isr_vector))
*(.text*)
*(.rodata*)
_etext = .;
} > FLASH
.data : AT(ADDR(.text) + SIZEOF(.text))
{
_data = .;
_ldata = LOADADDR (.data);
*(vtable)
*(.data*)
_edata = .;
} > SRAM
.bss :
{
_bss = .;
*(.bss*)
*(COMMON)
_ebss = .;
} > SRAM
.heap : AT(ADDR(.bss) + SIZEOF(.bss))
{
. = ALIGN(8);
__end__ = .;
PROVIDE(end = .);
__HeapBase = .;
. += HEAP_SIZE;
__HeapLimit = .;
} > SRAM
}
I only added .heap after .bss analogically to .data/.text but I get link error:
ld: section .init loaded at [000126b4,000126bf] overlaps section .data loaded at [000126b4,00012f8f]
collect2: error: ld returned 1 exit status
It also happens when I remove AT(ADDR(.bss) + SIZEOF(.bss))
. When I remove .heap and calls to libc functions everything compiles and links, the output binary runs correctly.
How should I adjust the script to correctly place heap after bss?
It turned out that my heap and bss were correct, but when linking standard library functions new sections called .init and .fini are added - they collided with .data. This is my corrected linker script:
/* Entry Point */
ENTRY(Reset_Handler)
HEAP_SIZE = 1024;
MEMORY
{
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x00100000
SRAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00040000
}
SECTIONS
{
.text :
{
_text = .;
KEEP(*(.isr_vector))
*(.text*)
*(.rodata*)
KEEP (*(.init))
KEEP (*(.fini))
_etext = .;
} > FLASH
.ARM.extab :
{
*(.ARM.extab* .gnu.linkonce.armextab.*)
} > FLASH
.ARM :
{
__exidx_start = .;
*(.ARM.exidx*)
__exidx_end = .;
} > FLASH
__end_code = .;
.data : AT(__end_code)
{
_data = .;
_ldata = LOADADDR (.data);
*(vtable)
*(.data*)
_edata = .;
} > SRAM
.bss :
{
_bss = .;
*(.bss*)
*(COMMON)
_ebss = .;
} > SRAM
.heap : AT(_ebss)
{
. = ALIGN(8);
__end__ = .;
PROVIDE(end = .);
__HeapBase = .;
. += HEAP_SIZE;
__HeapLimit = .;
} > SRAM
}
I added lines KEEP (*(.init))
, KEEP (*(.fini))
and .ARM
sections (from another MCU linker script). Now everything links and runs fine.
If you're using the TivaWare library, I'd suggest using the linker script that's generated when you create the appropriate project in Code Composer. You can just copy it over to your project. For example, the name would be tm4C123gh6pm.lds for the processor on the tm4c123glx launchpad eval board. I'll paste it here:
/******************************************************************************
*
* Default Linker script for the Texas Instruments TM4C123GH6PM
*
* This is derived from revision 15071 of the TivaWare Library.
*
*****************************************************************************/
MEMORY
{
FLASH (RX) : ORIGIN = 0x00000000, LENGTH = 0x00040000
SRAM (WX) : ORIGIN = 0x20000000, LENGTH = 0x00008000
}
REGION_ALIAS("REGION_TEXT", FLASH);
REGION_ALIAS("REGION_BSS", SRAM);
REGION_ALIAS("REGION_DATA", SRAM);
REGION_ALIAS("REGION_STACK", SRAM);
REGION_ALIAS("REGION_HEAP", SRAM);
REGION_ALIAS("REGION_ARM_EXIDX", FLASH);
REGION_ALIAS("REGION_ARM_EXTAB", FLASH);
SECTIONS {
PROVIDE (_intvecs_base_address = 0x0);
.intvecs (_intvecs_base_address) : AT (_intvecs_base_address) {
KEEP (*(.intvecs))
} > REGION_TEXT
PROVIDE (_vtable_base_address = 0x20000000);
.vtable (_vtable_base_address) : AT (_vtable_base_address) {
KEEP (*(.vtable))
} > REGION_DATA
.text : {
CREATE_OBJECT_SYMBOLS
*(.text)
*(.text.*)
. = ALIGN(0x4);
KEEP (*(.ctors))
. = ALIGN(0x4);
KEEP (*(.dtors))
. = ALIGN(0x4);
__init_array_start = .;
KEEP (*(.init_array*))
__init_array_end = .;
*(.init)
*(.fini*)
} > REGION_TEXT
PROVIDE (__etext = .);
PROVIDE (_etext = .);
PROVIDE (etext = .);
.rodata : {
*(.rodata)
*(.rodata*)
} > REGION_TEXT
.data : ALIGN (4) {
__data_load__ = LOADADDR (.data);
__data_start__ = .;
*(.data)
*(.data*)
. = ALIGN (4);
__data_end__ = .;
} > REGION_DATA AT> REGION_TEXT
.ARM.exidx : {
__exidx_start = .;
*(.ARM.exidx* .gnu.linkonce.armexidx.*)
__exidx_end = .;
} > REGION_ARM_EXIDX
.ARM.extab : {
*(.ARM.extab* .gnu.linkonce.armextab.*)
} > REGION_ARM_EXTAB
.bss : {
__bss_start__ = .;
*(.shbss)
*(.bss)
*(.bss.*)
*(COMMON)
. = ALIGN (4);
__bss_end__ = .;
} > REGION_BSS
.heap : {
__heap_start__ = .;
end = __heap_start__;
_end = end;
__end = end;
KEEP(*(.heap))
__heap_end__ = .;
__HeapLimit = __heap_end__;
} > REGION_HEAP
.stack : ALIGN(0x8) {
_stack = .;
__stack = .;
KEEP(*(.stack))
} > REGION_STACK
}
Then just make sure your startup code initializes the correct sections (you'll also get a file called e.g. tm4c123gh6pm_startup_ccs_gcc.c which will have startup code, either use as-is or as a guide.
You'll notice that they do a good job with variables needed for initializing various sections. Avoids the issue user2162550 brought up.
btw - I noticed that when compiling with -O2 linker errors were a bit obfuscated - if your code fits turn that off until you get everything where you want it.
User contributions licensed under CC BY-SA 3.0