Skip to content

Commit

Permalink
boot/startup: Linker script template unification
Browse files Browse the repository at this point in the history
Now all cortex linker script files have region for
interrupt stack.
It can be provided by memory_regions.ld.h if
stack is to be placed in separate memory region.
If MCU/BSP specific region is defined (no definition
of STACk_REGION)
STACK_RAM region is created at the end of RAM.
This simplifies check for heap.

mtb section is removed for now from all linker script
templates as it is not present in any MCU that has
generated linker script.

Few templates had duplicating section .bssnz that is now removed.

bss sections now have (NOLOAD) attribute

Signed-off-by: Jerzy Kasenberg <jerzy.kasenberg@codecoup.pl>
  • Loading branch information
kasjer committed Aug 22, 2024
1 parent ac2f9fa commit b8a263f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 82 deletions.
42 changes: 20 additions & 22 deletions boot/startup/mynewt_cortex_m0.ld
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <syscfg/syscfg.h>
#include <sysflash/sysflash.h>
#include <mcu_config.ld.h>
#include <bsp_config.ld.h>
#include <target_config.ld.h>
#include <mynewt_config.ld.h>
Expand All @@ -36,7 +37,18 @@ MEMORY
#ifdef FLASH_AREA_IMAGE_0_OFFSET
SLOT0 (rx!w) : ORIGIN = FLASH_AREA_IMAGE_0_OFFSET, LENGTH = FLASH_AREA_IMAGE_0_SIZE
#endif
/*
* If STACK_REGION is defined it means that MCU stack is place in other region then RAM
* int that case RAM region size is exactly RAM_SIZE
* If STACK_REGION is NOT defined STACK_RAM region is place at the end of RAM
* and RAM region size is shortened.
*/
#ifdef STACK_REGION
RAM (rwx) : ORIGIN = RAM_START, LENGTH = RAM_SIZE
#else
RAM (rwx) : ORIGIN = RAM_START, LENGTH = (RAM_SIZE - STACK_SIZE)
STACK_RAM (rw) : ORIGIN = RAM_START + RAM_SIZE - STACK_SIZE, LENGTH = STACK_SIZE
#endif
#include <memory_regions.ld.h>
}

Expand Down Expand Up @@ -247,7 +259,7 @@ SECTIONS
__data_image__ = LOADADDR(.data);
_sidata = LOADADDR(.data);

.bssnz :
.bssnz (NOLOAD) :
{
. = ALIGN(4);
__bssnz_start__ = .;
Expand All @@ -269,14 +281,8 @@ SECTIONS
__ecorebss = .;
} > COREBSS_RAM
#endif
.bssnz (NOLOAD):
{
. = ALIGN(4);
*(.bss.core.nz)
. = ALIGN(4);
} > RAM

.bss :
.bss (NOLOAD) :
{
. = ALIGN(4);
_sbss = .;
Expand All @@ -291,27 +297,19 @@ SECTIONS
/* Heap starts after BSS */
. = ALIGN(8);
__HeapBase = .;
/* Top of head is the bottom of the stack */
__HeapLimit = ORIGIN(RAM) + LENGTH(RAM);

/* Dummy section to calculate whether we need to move stack out of MTB
* buffer or not. */
.mtb (NOLOAD) :
{
KEEP(*(.mtb));
}
/* Check if data + heap + stack exceeds RAM limit */
ASSERT(__HeapBase <= __HeapLimit, "No space for MCU heap")

_ram_start = ORIGIN(RAM);

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

/* Top of head is the bottom of the stack */
__HeapLimit = __StackLimit;

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

42 changes: 20 additions & 22 deletions boot/startup/mynewt_cortex_m3.ld
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <syscfg/syscfg.h>
#include <sysflash/sysflash.h>
#include <mcu_config.ld.h>
#include <bsp_config.ld.h>
#include <target_config.ld.h>
#include <mynewt_config.ld.h>
Expand All @@ -36,7 +37,18 @@ MEMORY
#ifdef FLASH_AREA_IMAGE_0_OFFSET
SLOT0 (rx!w) : ORIGIN = FLASH_AREA_IMAGE_0_OFFSET, LENGTH = FLASH_AREA_IMAGE_0_SIZE
#endif
/*
* If STACK_REGION is defined it means that MCU stack is place in other region then RAM
* int that case RAM region size is exactly RAM_SIZE
* If STACK_REGION is NOT defined STACK_RAM region is place at the end of RAM
* and RAM region size is shortened.
*/
#ifdef STACK_REGION
RAM (rwx) : ORIGIN = RAM_START, LENGTH = RAM_SIZE
#else
RAM (rwx) : ORIGIN = RAM_START, LENGTH = (RAM_SIZE - STACK_SIZE)
STACK_RAM (rw) : ORIGIN = RAM_START + RAM_SIZE - STACK_SIZE, LENGTH = STACK_SIZE
#endif
#include <memory_regions.ld.h>
}

Expand Down Expand Up @@ -247,7 +259,7 @@ SECTIONS
__data_image__ = LOADADDR(.data);
_sidata = LOADADDR(.data);

.bssnz :
.bssnz (NOLOAD) :
{
. = ALIGN(4);
__bssnz_start__ = .;
Expand All @@ -269,14 +281,8 @@ SECTIONS
__ecorebss = .;
} > COREBSS_RAM
#endif
.bssnz (NOLOAD):
{
. = ALIGN(4);
*(.bss.core.nz)
. = ALIGN(4);
} > RAM

.bss :
.bss (NOLOAD) :
{
. = ALIGN(4);
_sbss = .;
Expand All @@ -291,27 +297,19 @@ SECTIONS
/* Heap starts after BSS */
. = ALIGN(8);
__HeapBase = .;
/* Top of head is the bottom of the stack */
__HeapLimit = ORIGIN(RAM) + LENGTH(RAM);

/* Dummy section to calculate whether we need to move stack out of MTB
* buffer or not. */
.mtb (NOLOAD) :
{
KEEP(*(.mtb));
}
/* Check if data + heap + stack exceeds RAM limit */
ASSERT(__HeapBase <= __HeapLimit, "No space for MCU heap")

_ram_start = ORIGIN(RAM);

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

/* Top of head is the bottom of the stack */
__HeapLimit = __StackLimit;

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

42 changes: 20 additions & 22 deletions boot/startup/mynewt_cortex_m33.ld
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <syscfg/syscfg.h>
#include <sysflash/sysflash.h>
#include <mcu_config.ld.h>
#include <bsp_config.ld.h>
#include <target_config.ld.h>
#include <mynewt_config.ld.h>
Expand All @@ -36,7 +37,18 @@ MEMORY
#ifdef FLASH_AREA_IMAGE_0_OFFSET
SLOT0 (rx!w) : ORIGIN = FLASH_AREA_IMAGE_0_OFFSET, LENGTH = FLASH_AREA_IMAGE_0_SIZE
#endif
/*
* If STACK_REGION is defined it means that MCU stack is place in other region then RAM
* int that case RAM region size is exactly RAM_SIZE
* If STACK_REGION is NOT defined STACK_RAM region is place at the end of RAM
* and RAM region size is shortened.
*/
#ifdef STACK_REGION
RAM (rwx) : ORIGIN = RAM_START, LENGTH = RAM_SIZE
#else
RAM (rwx) : ORIGIN = RAM_START, LENGTH = (RAM_SIZE - STACK_SIZE)
STACK_RAM (rw) : ORIGIN = RAM_START + RAM_SIZE - STACK_SIZE, LENGTH = STACK_SIZE
#endif
#include <memory_regions.ld.h>
}

Expand Down Expand Up @@ -247,7 +259,7 @@ SECTIONS
__data_image__ = LOADADDR(.data);
_sidata = LOADADDR(.data);

.bssnz :
.bssnz (NOLOAD) :
{
. = ALIGN(4);
__bssnz_start__ = .;
Expand All @@ -269,14 +281,8 @@ SECTIONS
__ecorebss = .;
} > COREBSS_RAM
#endif
.bssnz (NOLOAD):
{
. = ALIGN(4);
*(.bss.core.nz)
. = ALIGN(4);
} > RAM

.bss :
.bss (NOLOAD) :
{
. = ALIGN(4);
_sbss = .;
Expand All @@ -291,27 +297,19 @@ SECTIONS
/* Heap starts after BSS */
. = ALIGN(8);
__HeapBase = .;
/* Top of head is the bottom of the stack */
__HeapLimit = ORIGIN(RAM) + LENGTH(RAM);

/* Dummy section to calculate whether we need to move stack out of MTB
* buffer or not. */
.mtb (NOLOAD) :
{
KEEP(*(.mtb));
}
/* Check if data + heap + stack exceeds RAM limit */
ASSERT(__HeapBase <= __HeapLimit, "No space for MCU heap")

_ram_start = ORIGIN(RAM);

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

/* Top of head is the bottom of the stack */
__HeapLimit = __StackLimit;

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

35 changes: 19 additions & 16 deletions boot/startup/mynewt_cortex_m7.ld
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,18 @@ MEMORY
#ifdef FLASH_AREA_IMAGE_0_OFFSET
SLOT0 (rx!w) : ORIGIN = FLASH_AREA_IMAGE_0_OFFSET, LENGTH = FLASH_AREA_IMAGE_0_SIZE
#endif
/*
* If STACK_REGION is defined it means that MCU stack is place in other region then RAM
* int that case RAM region size is exactly RAM_SIZE
* If STACK_REGION is NOT defined STACK_RAM region is place at the end of RAM
* and RAM region size is shortened.
*/
#ifdef STACK_REGION
RAM (rwx) : ORIGIN = RAM_START, LENGTH = RAM_SIZE
#else
RAM (rwx) : ORIGIN = RAM_START, LENGTH = (RAM_SIZE - STACK_SIZE)
STACK_RAM (rw) : ORIGIN = RAM_START + RAM_SIZE - STACK_SIZE, LENGTH = STACK_SIZE
#endif
#include <memory_regions.ld.h>
}

Expand Down Expand Up @@ -248,7 +259,7 @@ SECTIONS
__data_image__ = LOADADDR(.data);
_sidata = LOADADDR(.data);

.bssnz :
.bssnz (NOLOAD) :
{
. = ALIGN(4);
__bssnz_start__ = .;
Expand All @@ -271,7 +282,7 @@ SECTIONS
} > COREBSS_RAM
#endif

.bss :
.bss (NOLOAD) :
{
. = ALIGN(4);
_sbss = .;
Expand All @@ -286,27 +297,19 @@ SECTIONS
/* Heap starts after BSS */
. = ALIGN(8);
__HeapBase = .;
/* Top of head is the bottom of the stack */
__HeapLimit = ORIGIN(RAM) + LENGTH(RAM);

/* Dummy section to calculate whether we need to move stack out of MTB
* buffer or not. */
.mtb (NOLOAD) :
{
KEEP(*(.mtb));
}
/* Check if data + heap + stack exceeds RAM limit */
ASSERT(__HeapBase <= __HeapLimit, "No space for MCU heap")

_ram_start = ORIGIN(RAM);

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

/* Top of head is the bottom of the stack */
__HeapLimit = __StackLimit;

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

0 comments on commit b8a263f

Please sign in to comment.