Skip to content

Commit f6e73ae

Browse files
keith-packardjgl-meta
authored andcommitted
include/zephyr: Fix linker scripts to define _end after all static RAM 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)
1 parent bd741f2 commit f6e73ae

File tree

17 files changed

+92
-80
lines changed

17 files changed

+92
-80
lines changed

boards/x86/qemu_x86/qemu_x86_tiny.ld

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -788,14 +788,9 @@ SECTIONS
788788
__kernel_ram_end = KERNEL_BASE_ADDR + KERNEL_RAM_SIZE;
789789
__kernel_ram_size = __kernel_ram_end - __kernel_ram_start;
790790

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

794-
z_mapped_end = .;
795793
z_mapped_size = z_mapped_end - z_mapped_start;
796-
_end = .; /* end of image */
797-
798-
GROUP_END(RAMABLE_REGION)
799794

800795
#ifndef LINKER_ZEPHYR_FINAL
801796
/* static interrupts */
@@ -823,6 +818,12 @@ SECTIONS
823818
*/
824819
#include <snippets-sections.ld>
825820

821+
#define LAST_RAM_ALIGN MMU_PAGE_ALIGN
822+
823+
#include <zephyr/linker/ram-end.ld>
824+
825+
GROUP_END(RAMABLE_REGION)
826+
826827
#include <zephyr/linker/debug-sections.ld>
827828

828829
/DISCARD/ : { *(.note.GNU-stack) }

include/zephyr/arch/arc/v2/linker.ld

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,6 @@ SECTIONS {
246246

247247
MPU_MIN_SIZE_ALIGN
248248
/* Define linker symbols */
249-
_image_ram_end = .;
250-
_end = .; /* end of image */
251249

252250
__kernel_ram_end = .;
253251
__kernel_ram_size = __kernel_ram_end - __kernel_ram_start;
@@ -278,13 +276,15 @@ SECTIONS {
278276
} GROUP_DATA_LINK_IN(YCCM, RAMABLE_REGION)
279277
#endif
280278

281-
GROUP_END(RAMABLE_REGION)
282-
283279
/* Located in generated directory. This file is populated by the
284280
* zephyr_linker_sources() Cmake function.
285281
*/
286282
#include <snippets-sections.ld>
287283

284+
#include <zephyr/linker/ram-end.ld>
285+
286+
GROUP_END(RAMABLE_REGION)
287+
288288
#include <zephyr/linker/debug-sections.ld>
289289

290290
SECTION_PROLOGUE(.ARC.attributes, 0,) {

include/zephyr/arch/arm/aarch32/cortex_a_r/scripts/linker.ld

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -332,16 +332,9 @@ SECTIONS
332332

333333
/* Define linker symbols */
334334

335-
. = ALIGN(_region_min_align);
336-
_image_ram_end = .;
337-
_end = .; /* end of image */
338-
z_mapped_end = .;
339-
340335
__kernel_ram_end = RAM_ADDR + RAM_SIZE;
341336
__kernel_ram_size = __kernel_ram_end - __kernel_ram_start;
342337

343-
GROUP_END(RAMABLE_REGION)
344-
345338
#if DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_ocm), okay)
346339
GROUP_START(OCM)
347340

@@ -373,6 +366,12 @@ GROUP_END(OCM)
373366
*/
374367
#include <snippets-sections.ld>
375368

369+
#define LAST_RAM_ALIGN . = ALIGN(_region_min_align);
370+
371+
#include <zephyr/linker/ram-end.ld>
372+
373+
GROUP_END(RAMABLE_REGION)
374+
376375
#include <zephyr/linker/debug-sections.ld>
377376

378377
SECTION_PROLOGUE(.ARM.attributes, 0,)

include/zephyr/arch/arm/aarch32/cortex_m/scripts/linker.ld

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -361,14 +361,9 @@ SECTIONS
361361

362362
/* Define linker symbols */
363363

364-
_image_ram_end = .;
365-
_end = .; /* end of image */
366-
367364
__kernel_ram_end = RAM_ADDR + RAM_SIZE;
368365
__kernel_ram_size = __kernel_ram_end - __kernel_ram_start;
369366

370-
GROUP_END(RAMABLE_REGION)
371-
372367
#if DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_itcm), okay)
373368
GROUP_START(ITCM)
374369

@@ -426,6 +421,10 @@ GROUP_END(DTCM)
426421
*/
427422
#include <snippets-sections.ld>
428423

424+
#include <zephyr/linker/ram-end.ld>
425+
426+
GROUP_END(RAMABLE_REGION)
427+
429428
#include <zephyr/linker/debug-sections.ld>
430429

431430
/DISCARD/ : { *(.note.GNU-stack) }

include/zephyr/arch/arm64/scripts/linker.ld

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -295,21 +295,21 @@ SECTIONS
295295

296296
/* Define linker symbols */
297297

298-
MMU_ALIGN;
299-
_image_ram_end = .;
300-
_end = .; /* end of image */
301-
z_mapped_end = .;
302-
303298
__kernel_ram_end = RAM_ADDR + RAM_SIZE;
304299
__kernel_ram_size = __kernel_ram_end - __kernel_ram_start;
305300

306-
GROUP_END(RAMABLE_REGION)
307301

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

307+
#define LAST_RAM_ALIGN MMU_ALIGN;
308+
309+
#include <zephyr/linker/ram-end.ld>
310+
311+
GROUP_END(RAMABLE_REGION)
312+
313313
#include <zephyr/linker/debug-sections.ld>
314314

315315
SECTION_PROLOGUE(.ARM.attributes, 0,)

include/zephyr/arch/mips/linker.ld

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,15 @@ SECTIONS
181181

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

184-
_image_ram_end = .;
185-
_end = .; /* end of image */
186-
187184
/* Located in generated directory. This file is populated by the
188185
* zephyr_linker_sources() Cmake function.
189186
*/
190187
#include <snippets-sections.ld>
191188

189+
#include <zephyr/linker/ram-end.ld>
190+
191+
GROUP_END(RAMABLE_REGION)
192+
192193
#include <zephyr/linker/debug-sections.ld>
193194

194195
.mdebug.abi32 : {

include/zephyr/arch/nios2/linker.ld

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,17 +259,15 @@ SECTIONS
259259

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

262-
/* Define linker symbols */
263-
_image_ram_end = .;
264-
_end = .; /* end of image */
265-
266-
GROUP_END(RAMABLE_REGION)
267-
268262
/* Located in generated directory. This file is populated by the
269263
* zephyr_linker_sources() Cmake function.
270264
*/
271265
#include <snippets-sections.ld>
272266

267+
#include <zephyr/linker/ram-end.ld>
268+
269+
GROUP_END(RAMABLE_REGION)
270+
273271
#include <zephyr/linker/debug-sections.ld>
274272

275273
}

include/zephyr/arch/riscv/common/linker.ld

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,6 @@ SECTIONS
305305

306306
__data_region_end = .;
307307

308-
MPU_MIN_SIZE_ALIGN
309-
310-
_image_ram_end = .;
311-
_end = .; /* end of image */
312-
313308
__kernel_ram_end = .;
314309
__kernel_ram_size = __kernel_ram_end - __kernel_ram_start;
315310

@@ -370,7 +365,11 @@ GROUP_END(DTCM)
370365
*/
371366
#include <snippets-sections.ld>
372367

373-
GROUP_END(RAMABLE_REGION)
368+
#define LAST_RAM_ALIGN MPU_MIN_SIZE_ALIGN
369+
370+
#include <zephyr/linker/ram-end.ld>
371+
372+
GROUP_END(RAMABLE_REGION)
374373

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

include/zephyr/arch/sparc/linker.ld

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,15 @@ SECTIONS
154154

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

157-
_image_ram_end = .;
158-
_end = .; /* end of image */
159-
160157
/* Located in generated directory. This file is populated by the
161158
* zephyr_linker_sources() Cmake function.
162159
*/
163160
#include <snippets-sections.ld>
164161

162+
#include <zephyr/linker/ram-end.ld>
163+
164+
GROUP_END(RAMABLE_REGION)
165+
165166
#include <zephyr/linker/debug-sections.ld>
166167

167168
/DISCARD/ : { *(.note.GNU-stack) }

include/zephyr/arch/x86/ia32/linker.ld

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -503,14 +503,10 @@ SECTIONS
503503
__kernel_ram_end = KERNEL_BASE_ADDR + KERNEL_RAM_SIZE;
504504
__kernel_ram_size = __kernel_ram_end - __kernel_ram_start;
505505

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

509508
z_mapped_end = .;
510509
z_mapped_size = z_mapped_end - z_mapped_start;
511-
_end = .; /* end of image */
512-
513-
GROUP_END(RAMABLE_REGION)
514510

515511
#ifndef LINKER_ZEPHYR_FINAL
516512
/* static interrupts */
@@ -538,6 +534,10 @@ SECTIONS
538534
*/
539535
#include <snippets-sections.ld>
540536

537+
#include <zephyr/linker/ram-end.ld>
538+
539+
GROUP_END(RAMABLE_REGION)
540+
541541
#include <zephyr/linker/debug-sections.ld>
542542

543543
/DISCARD/ : { *(.note.GNU-stack) }

0 commit comments

Comments
 (0)