Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Linker scripts do not define _end correctly #60794

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 2 additions & 0 deletions cmake/linker_script/arm/linker.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ if(NOT CONFIG_USERSPACE)
zephyr_linker_section_configure(SECTION .noinit INPUT ".kernel_noinit.*")
endif()

include(${COMMON_ZEPHYR_LINKER_DIR}/ram-end.cmake)

zephyr_linker_symbol(OBJECT REGION_RAM SYMBOL __kernel_ram_start EXPR "(@__bss_start@)")
zephyr_linker_symbol(OBJECT REGION_RAM SYMBOL __kernel_ram_end EXPR "(${RAM_ADDR} + ${RAM_SIZE})")
zephyr_linker_symbol(OBJECT REGION_RAM SYMBOL __kernel_ram_size EXPR "(@__kernel_ram_end@ - @__bss_start@)")
Expand Down
7 changes: 7 additions & 0 deletions cmake/linker_script/common/ram-end.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
zephyr_linker_section(NAME .last_ram_section VMA RAM LMA RAM_REGION TYPE BSS)
zephyr_linker_section_configure(
SECTION .last_ram_section
INPUT ""
SYMBOLS _end z_mapped_end
KEEP
)
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
Loading