1/*
2 * Copyright (c) 2013-2014 Wind River Systems, Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7/**
8 * @file
9 * @brief Linker command/script file
10 *
11 * Linker script for the Cortex-M platforms.
12 */
13
14#include <zephyr/linker/sections.h>
15#include <zephyr/devicetree.h>
16
17#include <zephyr/linker/devicetree_regions.h>
18#include <zephyr/linker/linker-defs.h>
19#include <zephyr/linker/linker-tool.h>
20
21/* physical address of RAM */
22#ifdef CONFIG_XIP
23#define ROMABLE_REGION FLASH
24#else
25#define ROMABLE_REGION RAM
26#endif
27#define RAMABLE_REGION RAM
28
29/* Region of the irq vectors and boot-vector SP/PC */
30#if defined(CONFIG_ROMSTART_RELOCATION_ROM)
31#define ROMSTART_ADDR CONFIG_ROMSTART_REGION_ADDRESS
32#define ROMSTART_SIZE (CONFIG_ROMSTART_REGION_SIZE * 1K)
33#else
34#define ROMSTART_REGION ROMABLE_REGION
35#endif
36
37#if !defined(CONFIG_XIP) && (CONFIG_FLASH_SIZE == 0)
38#define ROM_ADDR RAM_ADDR
39#else
40#define ROM_ADDR (CONFIG_FLASH_BASE_ADDRESS + CONFIG_FLASH_LOAD_OFFSET)
41#endif
42
43#if defined(CONFIG_ROM_END_OFFSET)
44#define ROM_END_OFFSET CONFIG_ROM_END_OFFSET
45#else
46#define ROM_END_OFFSET 0
47#endif
48
49#if CONFIG_FLASH_LOAD_SIZE > 0
50#define ROM_SIZE (CONFIG_FLASH_LOAD_SIZE - ROM_END_OFFSET)
51#else
52#define ROM_SIZE (CONFIG_FLASH_SIZE * 1024 - CONFIG_FLASH_LOAD_OFFSET - ROM_END_OFFSET)
53#endif
54
55#define RAM_SIZE (CONFIG_SRAM_SIZE * 1K)
56#define RAM_ADDR CONFIG_SRAM_BASE_ADDRESS
57
58#if defined(CONFIG_CUSTOM_SECTION_ALIGN)
59_region_min_align = CONFIG_CUSTOM_SECTION_MIN_ALIGN_SIZE;
60#else
61/* Set alignment to CONFIG_ARM_MPU_REGION_MIN_ALIGN_AND_SIZE
62 * to make linker section alignment comply with MPU granularity.
63 */
64#if defined(CONFIG_ARM_MPU_REGION_MIN_ALIGN_AND_SIZE)
65_region_min_align = CONFIG_ARM_MPU_REGION_MIN_ALIGN_AND_SIZE;
66#else
67/* If building without MPU support, use default 4-byte alignment. */
68_region_min_align = 4;
69#endif
70#endif
71
72#if !defined(CONFIG_CUSTOM_SECTION_ALIGN) && defined(CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT)
73#define MPU_ALIGN(region_size) \
74    . = ALIGN(_region_min_align); \
75    . = ALIGN( 1 << LOG2CEIL(region_size))
76#else
77#define MPU_ALIGN(region_size) \
78    . = ALIGN(_region_min_align)
79#endif
80
81#include <zephyr/linker/linker-devnull.h>
82
83MEMORY
84    {
85#if defined(CONFIG_ROMSTART_RELOCATION_ROM)
86    ROMSTART_REGION (rx) : ORIGIN = ROMSTART_ADDR, LENGTH = ROMSTART_SIZE
87#endif
88    FLASH (rx) : ORIGIN = ROM_ADDR, LENGTH = ROM_SIZE
89    RAM   (wx) : ORIGIN = RAM_ADDR, LENGTH = RAM_SIZE
90#if defined(CONFIG_LINKER_DEVNULL_MEMORY)
91    DEVNULL_ROM (rx) : ORIGIN = DEVNULL_ADDR, LENGTH = DEVNULL_SIZE
92#endif
93    LINKER_DT_REGIONS()
94    /* Used by and documented in include/linker/intlist.ld */
95    IDT_LIST (wx) : ORIGIN = 0xFFFF7FFF, LENGTH = 32K
96    }
97
98ENTRY(CONFIG_KERNEL_ENTRY)
99
100SECTIONS
101    {
102
103#include <zephyr/linker/rel-sections.ld>
104#include <zephyr/linker/llext-sections.ld>
105
106    /*
107     * .plt and .iplt are here according to 'arm-zephyr-elf-ld --verbose',
108     * before text section.
109     */
110    /DISCARD/ :
111	{
112	*(.plt)
113	}
114
115    /DISCARD/ :
116	{
117	*(.iplt)
118	}
119
120    GROUP_START(ROMABLE_REGION)
121
122	__rom_region_start = ROM_ADDR;
123
124    SECTION_PROLOGUE(rom_start,,)
125	{
126
127/* Located in generated directory. This file is populated by calling
128 * zephyr_linker_sources(ROM_START ...). This typically contains the vector
129 * table and debug information.
130 */
131#include <snippets-rom-start.ld>
132
133	} GROUP_LINK_IN(ROMSTART_REGION)
134
135#ifdef CONFIG_CODE_DATA_RELOCATION
136
137#include <linker_relocate.ld>
138
139#endif /* CONFIG_CODE_DATA_RELOCATION */
140
141#if DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_itcm), okay)
142GROUP_START(ITCM)
143
144	SECTION_PROLOGUE(_ITCM_SECTION_NAME,,SUBALIGN(4))
145	{
146		__itcm_start = .;
147		*(.itcm)
148		*(".itcm.*")
149
150/* Located in generated directory. This file is populated by the
151 * zephyr_linker_sources() Cmake function. */
152#include <snippets-itcm-section.ld>
153
154		__itcm_end = .;
155	} GROUP_LINK_IN(ITCM AT> ROMABLE_REGION)
156
157	__itcm_size = __itcm_end - __itcm_start;
158	__itcm_load_start = LOADADDR(_ITCM_SECTION_NAME);
159
160GROUP_END(ITCM)
161#endif
162
163    SECTION_PROLOGUE(_TEXT_SECTION_NAME,,)
164	{
165	__text_region_start = .;
166
167#include <zephyr/linker/kobject-text.ld>
168
169	*(.text)
170	*(".text.*")
171	*(".TEXT.*")
172	*(.gnu.linkonce.t.*)
173
174	/*
175	 * These are here according to 'arm-zephyr-elf-ld --verbose',
176	 * after .gnu.linkonce.t.*
177	 */
178	*(.glue_7t) *(.glue_7) *(.vfp11_veneer) *(.v4_bx)
179	. = ALIGN(4);
180
181	} GROUP_LINK_IN(ROMABLE_REGION)
182
183	__text_region_end = .;
184
185#if defined (CONFIG_CPP) || defined(CONFIG_RUST)
186	SECTION_PROLOGUE(.ARM.extab,,)
187	{
188	/*
189	 * .ARM.extab section containing exception unwinding information.
190	 */
191	*(.ARM.extab* .gnu.linkonce.armextab.*)
192	} GROUP_LINK_IN(ROMABLE_REGION)
193#endif
194
195	SECTION_PROLOGUE(.ARM.exidx,,)
196	{
197	/*
198	 * This section, related to stack and exception unwinding, is placed
199	 * explicitly to prevent it from being shared between multiple regions.
200	 * It  must be defined for gcc to support 64-bit math and avoid
201	 * section overlap.
202	 */
203	__exidx_start = .;
204#if defined (__GCC_LINKER_CMD__) || defined (__LLD_LINKER_CMD__)
205	*(.ARM.exidx* gnu.linkonce.armexidx.*)
206#endif
207	__exidx_end = .;
208	} GROUP_LINK_IN(ROMABLE_REGION)
209
210	__rodata_region_start = .;
211
212#include <zephyr/linker/common-rom.ld>
213/* Located in generated directory. This file is populated by calling
214 * zephyr_linker_sources(ROM_SECTIONS ...). Useful for grouping iterable RO structs.
215 */
216#include <snippets-rom-sections.ld>
217#include <zephyr/linker/thread-local-storage.ld>
218
219    SECTION_PROLOGUE(_RODATA_SECTION_NAME,,)
220	{
221	*(.rodata)
222	*(".rodata.*")
223	*(.gnu.linkonce.r.*)
224
225/* Located in generated directory. This file is populated by the
226 * zephyr_linker_sources() Cmake function.
227 */
228#include <snippets-rodata.ld>
229
230#include <zephyr/linker/kobject-rom.ld>
231
232	/*
233	 * For XIP images, in order to avoid the situation when __data_rom_start
234	 * is 32-bit aligned, but the actual data is placed right after rodata
235	 * section, which may not end exactly at 32-bit border, pad rodata
236	 * section, so __data_rom_start points at data and it is 32-bit aligned.
237	 *
238	 * On non-XIP images this may enlarge image size up to 3 bytes. This
239	 * generally is not an issue, since modern ROM and FLASH memory is
240	 * usually 4k aligned.
241	 */
242	. = ALIGN(4);
243	} GROUP_LINK_IN(ROMABLE_REGION)
244
245#include <zephyr/linker/cplusplus-rom.ld>
246
247#if defined(CONFIG_BUILD_ALIGN_LMA) || !defined(CONFIG_XIP)
248    /*
249     * Include a padding section here to make sure that the LMA address
250     * of the sections in the RAMABLE_REGION are aligned with those
251     * section's VMA alignment requirements.
252     */
253    SECTION_PROLOGUE(padding_section,,)
254	{
255	__rodata_region_end = .;
256	MPU_ALIGN(__rodata_region_end - ADDR(rom_start));
257	} GROUP_LINK_IN(ROMABLE_REGION)
258#else
259	__rodata_region_end = .;
260	MPU_ALIGN(__rodata_region_end - ADDR(rom_start));
261#endif
262	__rom_region_end = __rom_region_start + . - ADDR(rom_start);
263	__rom_region_size = __rom_region_end - __rom_region_start;
264
265    GROUP_END(ROMABLE_REGION)
266
267    /*
268     * These are here according to 'arm-zephyr-elf-ld --verbose',
269     * before data section.
270     */
271    /DISCARD/ : {
272	*(.got.plt)
273	*(.igot.plt)
274	*(.got)
275	*(.igot)
276	}
277
278    GROUP_START(RAMABLE_REGION)
279
280#if ROM_ADDR != RAM_ADDR
281	. = RAM_ADDR;
282#endif
283
284	/* Align the start of image RAM with the
285	 * minimum granularity required by MPU.
286	 */
287	. = ALIGN(_region_min_align);
288	_image_ram_start = .;
289
290/* Located in generated directory. This file is populated by the
291 * zephyr_linker_sources() Cmake function.
292 */
293#include <snippets-ram-sections.ld>
294
295#if defined(CONFIG_USERSPACE)
296#define APP_SHARED_ALIGN . = ALIGN(_region_min_align);
297#define SMEM_PARTITION_ALIGN MPU_ALIGN
298
299#include <app_smem.ld>
300
301	_app_smem_size = _app_smem_end - _app_smem_start;
302	_app_smem_rom_start = LOADADDR(_APP_SMEM_SECTION_NAME);
303#endif  /* CONFIG_USERSPACE */
304
305    GROUP_START(DATA_REGION)
306
307    SECTION_DATA_PROLOGUE(_DATA_SECTION_NAME,,)
308	{
309	__data_region_start = .;
310	__data_start = .;
311	*(.data)
312	*(".data.*")
313	*(".kernel.*")
314
315/* Located in generated directory. This file is populated by the
316 * zephyr_linker_sources() Cmake function.
317 */
318#include <snippets-rwdata.ld>
319
320#ifdef CONFIG_CODE_DATA_RELOCATION
321#include <linker_sram_data_relocate.ld>
322#endif
323	__data_end = .;
324
325	} GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
326    __data_size = __data_end - __data_start;
327    __data_load_start = LOADADDR(_DATA_SECTION_NAME);
328
329    __data_region_load_start = LOADADDR(_DATA_SECTION_NAME);
330
331#include <zephyr/linker/common-ram.ld>
332#include <zephyr/linker/kobject-data.ld>
333
334#include <zephyr/linker/cplusplus-ram.ld>
335
336/* Located in generated directory. This file is populated by the
337 * zephyr_linker_sources() Cmake function.
338 */
339#include <snippets-data-sections.ld>
340
341    __data_region_end = .;
342
343#if DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_dtcm), okay)
344GROUP_START(DTCM)
345
346	SECTION_PROLOGUE(_DTCM_BSS_SECTION_NAME, (NOLOAD),SUBALIGN(4))
347	{
348		__dtcm_start = .;
349		__dtcm_bss_start = .;
350		*(.dtcm_bss)
351		*(".dtcm_bss.*")
352		__dtcm_bss_end = .;
353	} GROUP_LINK_IN(DTCM)
354
355	SECTION_PROLOGUE(_DTCM_NOINIT_SECTION_NAME, (NOLOAD),SUBALIGN(4))
356	{
357		__dtcm_noinit_start = .;
358		*(.dtcm_noinit)
359		*(".dtcm_noinit.*")
360		__dtcm_noinit_end = .;
361	} GROUP_LINK_IN(DTCM)
362
363	SECTION_PROLOGUE(_DTCM_DATA_SECTION_NAME,,SUBALIGN(4))
364	{
365		__dtcm_data_start = .;
366		*(.dtcm_data)
367		*(".dtcm_data.*")
368
369/* Located in generated directory. This file is populated by the
370 * zephyr_linker_sources() Cmake function. */
371#include <snippets-dtcm-section.ld>
372
373		__dtcm_data_end = .;
374	} GROUP_LINK_IN(DTCM AT> ROMABLE_REGION)
375
376	__dtcm_end = .;
377
378	__dtcm_data_load_start = LOADADDR(_DTCM_DATA_SECTION_NAME);
379
380GROUP_END(DTCM)
381#endif
382
383/* Located in generated directory. This file is populated by the
384 * zephyr_linker_sources() Cmake function.
385 */
386#include <snippets-sections.ld>
387
388#include <zephyr/linker/debug-sections.ld>
389
390    /DISCARD/ : { *(.note.GNU-stack) }
391
392    SECTION_PROLOGUE(.ARM.attributes, 0,)
393	{
394	KEEP(*(.ARM.attributes))
395	KEEP(*(.gnu.attributes))
396	}
397
398/* Output section descriptions are needed for these sections to suppress
399 * warnings when "--orphan-handling=warn" is set for lld.
400 */
401#if defined(CONFIG_LLVM_USE_LLD)
402    SECTION_PROLOGUE(.symtab, 0,) { *(.symtab) }
403    SECTION_PROLOGUE(.strtab, 0,) { *(.strtab) }
404    SECTION_PROLOGUE(.shstrtab, 0,) { *(.shstrtab) }
405#endif
406
407    /* Sections generated from 'zephyr,memory-region' nodes */
408    LINKER_DT_SECTIONS()
409
410/* Must be last in romable region */
411SECTION_PROLOGUE(.last_section,,)
412{
413  /* .last_section contains a fixed word to ensure location counter and actual
414   * rom region data usage match when CONFIG_LINKER_LAST_SECTION_ID=y. */
415  KEEP(*(.last_section))
416} GROUP_LINK_IN(ROMABLE_REGION)
417
418/* To provide the image size as a const expression,
419 * calculate this value here. */
420_flash_used = LOADADDR(.last_section) + SIZEOF(.last_section) - __rom_region_start;
421
422    SECTION_DATA_PROLOGUE(_BSS_SECTION_NAME,(NOLOAD),)
423	{
424        /*
425         * For performance, BSS section is assumed to be 4 byte aligned and
426         * a multiple of 4 bytes
427         */
428        . = ALIGN(4);
429	__bss_start = .;
430	__kernel_ram_start = .;
431
432	*(.bss)
433	*(".bss.*")
434	*(COMMON)
435	*(".kernel_bss.*")
436
437#ifdef CONFIG_CODE_DATA_RELOCATION
438#include <linker_sram_bss_relocate.ld>
439#endif
440
441        /*
442         * As memory is cleared in words only, it is simpler to ensure the BSS
443         * section ends on a 4 byte boundary. This wastes a maximum of 3 bytes.
444		 */
445	__bss_end = ALIGN(4);
446	} GROUP_DATA_LINK_IN(RAMABLE_REGION, RAMABLE_REGION)
447
448#include <zephyr/linker/common-noinit.ld>
449
450    /* Define linker symbols */
451
452    __kernel_ram_end = RAM_ADDR + RAM_SIZE;
453    __kernel_ram_size = __kernel_ram_end - __kernel_ram_start;
454
455#include <zephyr/linker/ram-end.ld>
456
457    GROUP_END(RAMABLE_REGION)
458
459    }
460