Skip to content

Commit

Permalink
include/zephyr: Fix linker scripts to define _end after all static RA…
Browse files Browse the repository at this point in the history
…M data

The Zephyr linker scripts have inconsistent ordering of various chunks of
data which lands in RAM at runtime. This leads to the value of _end not
being consistently defined as the maximum address of static variables used
in the application.

Create a helper linker fragment, zephyr/linker/ram-end.ld, which can be
included after the last possible definition of RAM data, that consistently
sets _image_ram_end, _end and z_mapped_end.

Signed-off-by: Keith Packard <[email protected]>
(cherry picked from commit 710422e)
  • Loading branch information
keith-packard authored and github-actions[bot] committed Jun 28, 2023
1 parent 356c8cb commit d329222
Show file tree
Hide file tree
Showing 17 changed files with 92 additions and 80 deletions.
11 changes: 6 additions & 5 deletions boards/x86/qemu_x86/qemu_x86_tiny.ld
Original file line number Diff line number Diff line change
Expand Up @@ -788,14 +788,9 @@ SECTIONS
__kernel_ram_end = KERNEL_BASE_ADDR + KERNEL_RAM_SIZE;
__kernel_ram_size = __kernel_ram_end - __kernel_ram_start;

_image_ram_end = .;
_image_ram_all = (KERNEL_BASE_ADDR + KERNEL_RAM_SIZE) - _image_ram_start;

z_mapped_end = .;
z_mapped_size = z_mapped_end - z_mapped_start;
_end = .; /* end of image */

GROUP_END(RAMABLE_REGION)

#ifndef LINKER_ZEPHYR_FINAL
/* static interrupts */
Expand Down Expand Up @@ -823,6 +818,12 @@ SECTIONS
*/
#include <snippets-sections.ld>

#define LAST_RAM_ALIGN MMU_PAGE_ALIGN

#include <zephyr/linker/ram-end.ld>

GROUP_END(RAMABLE_REGION)

#include <zephyr/linker/debug-sections.ld>

/DISCARD/ : { *(.note.GNU-stack) }
Expand Down
8 changes: 4 additions & 4 deletions include/zephyr/arch/arc/v2/linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,6 @@ SECTIONS {

MPU_MIN_SIZE_ALIGN
/* Define linker symbols */
_image_ram_end = .;
_end = .; /* end of image */

__kernel_ram_end = .;
__kernel_ram_size = __kernel_ram_end - __kernel_ram_start;
Expand Down Expand Up @@ -278,13 +276,15 @@ SECTIONS {
} GROUP_DATA_LINK_IN(YCCM, RAMABLE_REGION)
#endif

GROUP_END(RAMABLE_REGION)

/* Located in generated directory. This file is populated by the
* zephyr_linker_sources() Cmake function.
*/
#include <snippets-sections.ld>

#include <zephyr/linker/ram-end.ld>

GROUP_END(RAMABLE_REGION)

#include <zephyr/linker/debug-sections.ld>

SECTION_PROLOGUE(.ARC.attributes, 0,) {
Expand Down
13 changes: 6 additions & 7 deletions include/zephyr/arch/arm/aarch32/cortex_a_r/scripts/linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -332,16 +332,9 @@ SECTIONS

/* Define linker symbols */

. = ALIGN(_region_min_align);
_image_ram_end = .;
_end = .; /* end of image */
z_mapped_end = .;

__kernel_ram_end = RAM_ADDR + RAM_SIZE;
__kernel_ram_size = __kernel_ram_end - __kernel_ram_start;

GROUP_END(RAMABLE_REGION)

#if DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_ocm), okay)
GROUP_START(OCM)

Expand Down Expand Up @@ -373,6 +366,12 @@ GROUP_END(OCM)
*/
#include <snippets-sections.ld>

#define LAST_RAM_ALIGN . = ALIGN(_region_min_align);

#include <zephyr/linker/ram-end.ld>

GROUP_END(RAMABLE_REGION)

#include <zephyr/linker/debug-sections.ld>

SECTION_PROLOGUE(.ARM.attributes, 0,)
Expand Down
9 changes: 4 additions & 5 deletions include/zephyr/arch/arm/aarch32/cortex_m/scripts/linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -361,14 +361,9 @@ SECTIONS

/* Define linker symbols */

_image_ram_end = .;
_end = .; /* end of image */

__kernel_ram_end = RAM_ADDR + RAM_SIZE;
__kernel_ram_size = __kernel_ram_end - __kernel_ram_start;

GROUP_END(RAMABLE_REGION)

#if DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_itcm), okay)
GROUP_START(ITCM)

Expand Down Expand Up @@ -426,6 +421,10 @@ GROUP_END(DTCM)
*/
#include <snippets-sections.ld>

#include <zephyr/linker/ram-end.ld>

GROUP_END(RAMABLE_REGION)

#include <zephyr/linker/debug-sections.ld>

/DISCARD/ : { *(.note.GNU-stack) }
Expand Down
12 changes: 6 additions & 6 deletions include/zephyr/arch/arm64/scripts/linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -295,21 +295,21 @@ SECTIONS

/* Define linker symbols */

MMU_ALIGN;
_image_ram_end = .;
_end = .; /* end of image */
z_mapped_end = .;

__kernel_ram_end = RAM_ADDR + RAM_SIZE;
__kernel_ram_size = __kernel_ram_end - __kernel_ram_start;

GROUP_END(RAMABLE_REGION)

/* Located in generated directory. This file is populated by the
* zephyr_linker_sources() Cmake function.
*/
#include <snippets-sections.ld>

#define LAST_RAM_ALIGN MMU_ALIGN;

#include <zephyr/linker/ram-end.ld>

GROUP_END(RAMABLE_REGION)

#include <zephyr/linker/debug-sections.ld>

SECTION_PROLOGUE(.ARM.attributes, 0,)
Expand Down
7 changes: 4 additions & 3 deletions include/zephyr/arch/mips/linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,15 @@ SECTIONS

#include <zephyr/linker/cplusplus-ram.ld>

_image_ram_end = .;
_end = .; /* end of image */

/* Located in generated directory. This file is populated by the
* zephyr_linker_sources() Cmake function.
*/
#include <snippets-sections.ld>

#include <zephyr/linker/ram-end.ld>

GROUP_END(RAMABLE_REGION)

#include <zephyr/linker/debug-sections.ld>

.mdebug.abi32 : {
Expand Down
10 changes: 4 additions & 6 deletions include/zephyr/arch/nios2/linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -259,17 +259,15 @@ SECTIONS

#include <zephyr/linker/common-noinit.ld>

/* Define linker symbols */
_image_ram_end = .;
_end = .; /* end of image */

GROUP_END(RAMABLE_REGION)

/* Located in generated directory. This file is populated by the
* zephyr_linker_sources() Cmake function.
*/
#include <snippets-sections.ld>

#include <zephyr/linker/ram-end.ld>

GROUP_END(RAMABLE_REGION)

#include <zephyr/linker/debug-sections.ld>

}
11 changes: 5 additions & 6 deletions include/zephyr/arch/riscv/common/linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,6 @@ SECTIONS

__data_region_end = .;

MPU_MIN_SIZE_ALIGN

_image_ram_end = .;
_end = .; /* end of image */

__kernel_ram_end = .;
__kernel_ram_size = __kernel_ram_end - __kernel_ram_start;

Expand Down Expand Up @@ -370,7 +365,11 @@ GROUP_END(DTCM)
*/
#include <snippets-sections.ld>

GROUP_END(RAMABLE_REGION)
#define LAST_RAM_ALIGN MPU_MIN_SIZE_ALIGN

#include <zephyr/linker/ram-end.ld>

GROUP_END(RAMABLE_REGION)

#include <zephyr/linker/debug-sections.ld>

Expand Down
7 changes: 4 additions & 3 deletions include/zephyr/arch/sparc/linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,15 @@ SECTIONS

#include <zephyr/linker/cplusplus-ram.ld>

_image_ram_end = .;
_end = .; /* end of image */

/* Located in generated directory. This file is populated by the
* zephyr_linker_sources() Cmake function.
*/
#include <snippets-sections.ld>

#include <zephyr/linker/ram-end.ld>

GROUP_END(RAMABLE_REGION)

#include <zephyr/linker/debug-sections.ld>

/DISCARD/ : { *(.note.GNU-stack) }
Expand Down
8 changes: 4 additions & 4 deletions include/zephyr/arch/x86/ia32/linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -503,14 +503,10 @@ SECTIONS
__kernel_ram_end = KERNEL_BASE_ADDR + KERNEL_RAM_SIZE;
__kernel_ram_size = __kernel_ram_end - __kernel_ram_start;

_image_ram_end = .;
_image_ram_all = (KERNEL_BASE_ADDR + KERNEL_RAM_SIZE) - _image_ram_start;

z_mapped_end = .;
z_mapped_size = z_mapped_end - z_mapped_start;
_end = .; /* end of image */

GROUP_END(RAMABLE_REGION)

#ifndef LINKER_ZEPHYR_FINAL
/* static interrupts */
Expand Down Expand Up @@ -538,6 +534,10 @@ SECTIONS
*/
#include <snippets-sections.ld>

#include <zephyr/linker/ram-end.ld>

GROUP_END(RAMABLE_REGION)

#include <zephyr/linker/debug-sections.ld>

/DISCARD/ : { *(.note.GNU-stack) }
Expand Down
10 changes: 6 additions & 4 deletions include/zephyr/arch/x86/intel64/linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,12 @@ SECTIONS

/* Must be last in RAM */
#include <zephyr/linker/kobject-data.ld>
MMU_PAGE_ALIGN
_image_ram_end = .;
z_mapped_end = .;
_end = .;

#define LAST_RAM_ALIGN MMU_PAGE_ALIGN

#include <zephyr/linker/ram-end.ld>

GROUP_END(RAMABLE_REGION)

/* All unused memory also owned by the kernel for heaps */
__kernel_ram_end = KERNEL_BASE_ADDR + KERNEL_RAM_SIZE;
Expand Down
14 changes: 14 additions & 0 deletions include/zephyr/linker/ram-end.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Added after the very last allocation that might land in RAM to define the various
* end-of-used-memory symbols
*/

SECTION_PROLOGUE(.last_ram_section,,)
{
#ifdef LAST_RAM_ALIGN
LAST_RAM_ALIGN
#endif
_image_ram_end = .;
_end = .; /* end of image */
z_mapped_end = .;
} GROUP_NOLOAD_LINK_IN(RAMABLE_REGION, RAMABLE_REGION)
19 changes: 9 additions & 10 deletions soc/arm64/nxp_imx/mimx9/linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -312,21 +312,20 @@ SECTIONS

/* Define linker symbols */

MMU_ALIGN;
_image_ram_end = .;
_end = .; /* end of image */
z_mapped_end = .;

__kernel_ram_end = RAM_ADDR + RAM_SIZE;
__kernel_ram_size = __kernel_ram_end - __kernel_ram_start;

GROUP_END(RAMABLE_REGION)

/* Located in generated directory. This file is populated by the
* zephyr_linker_sources() Cmake function.
*/
#include <snippets-sections.ld>

#define LAST_RAM_ALIGN MMU_ALIGN;

#include <zephyr/linker/ram-end.ld>

GROUP_END(RAMABLE_REGION)

__kernel_ram_end = RAM_ADDR + RAM_SIZE;
__kernel_ram_size = __kernel_ram_end - __kernel_ram_start;

#include <zephyr/linker/debug-sections.ld>

SECTION_PROLOGUE(.ARM.attributes, 0,)
Expand Down
9 changes: 4 additions & 5 deletions soc/riscv/openisa_rv32m1/linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -233,16 +233,15 @@ SECTIONS
*/
#include <snippets-ram-sections.ld>

_image_ram_end = .;
_end = .; /* end of image */

GROUP_END(RAM)

/* Located in generated directory. This file is populated by the
* zephyr_linker_sources() Cmake function.
*/
#include <snippets-sections.ld>

#include <zephyr/linker/ram-end.ld>

GROUP_END(RAMABLE_REGION)

#ifdef CONFIG_GEN_ISR_TABLES
/* Bogus section, post-processed during the build to initialize interrupts. */
#include <zephyr/linker/intlist.ld>
Expand Down
11 changes: 5 additions & 6 deletions soc/riscv/riscv-ite/it8xxx2/linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,6 @@ SECTIONS

__data_region_end = .;

MPU_MIN_SIZE_ALIGN

_image_ram_end = .;
_end = .; /* end of image */

__kernel_ram_end = .;
__kernel_ram_size = __kernel_ram_end - __kernel_ram_start;

Expand All @@ -369,7 +364,11 @@ SECTIONS
*/
#include <snippets-sections.ld>

GROUP_END(RAMABLE_REGION)
#define LAST_RAM_ALIGN MPU_MIN_SIZE_ALIGN

#include <zephyr/linker/ram-end.ld>

GROUP_END(RAMABLE_REGION)

#include <zephyr/linker/debug-sections.ld>

Expand Down
11 changes: 5 additions & 6 deletions soc/riscv/riscv-privileged/andes_v5/ae350/linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,6 @@ SECTIONS

__data_region_end = .;

MPU_MIN_SIZE_ALIGN

_image_ram_end = .;
_end = .; /* end of image */

__kernel_ram_end = .;
__kernel_ram_size = __kernel_ram_end - __kernel_ram_start;

Expand Down Expand Up @@ -339,7 +334,11 @@ GROUP_END(DTCM)
*/
#include <snippets-sections.ld>

GROUP_END(RAMABLE_REGION)
#define LAST_RAM_ALIGN MPU_MIN_SIZE_ALIGN

#include <zephyr/linker/ram-end.ld>

GROUP_END(RAMABLE_REGION)

#include <zephyr/linker/debug-sections.ld>

Expand Down
2 changes: 2 additions & 0 deletions soc/xtensa/intel_adsp/ace/ace-link.ld
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@ SECTIONS {
_unused_ram_start_marker = .;
*(.unused_ram_start_marker)
*(.unused_ram_start_marker.*)
_end = .;
z_mapped_end = .;
} >ram

. = L2_SRAM_BASE + L2_SRAM_SIZE;
Expand Down

0 comments on commit d329222

Please sign in to comment.