1/*
2 * Copyright (c) 2019-2021 Intel Corp.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6#include <zephyr/linker/linker-defs.h>
7#include <zephyr/linker/linker-tool.h>
8
9#define ROMABLE_REGION RAM
10#define RAMABLE_REGION RAM
11
12#define MMU_PAGE_ALIGN		. = ALIGN(CONFIG_MMU_PAGE_SIZE);
13
14/* Used to align areas with separate memory permission characteristics
15 * so that the page permissions can be set in the MMU. Without this,
16 * the kernel is just one blob with the same RWX permissions on all RAM
17 */
18#ifdef CONFIG_SRAM_REGION_PERMISSIONS
19	#define MMU_PAGE_ALIGN_PERM	MMU_PAGE_ALIGN
20#else
21	#define MMU_PAGE_ALIGN_PERM
22#endif
23
24ENTRY(CONFIG_KERNEL_ENTRY)
25
26SECTIONS
27{
28	/*
29	 * The "locore" must be in the 64K of RAM, so that 16-bit code (with
30	 * segment registers == 0x0000) and 32/64-bit code agree on addresses.
31	 * ... there is no 16-bit code yet, but there will be when we add SMP.
32	 */
33
34	SECTION_PROLOGUE(.locore,,)
35	{
36	_locore_start = .;
37	*(.locore)
38	*(.locore.*)
39	MMU_PAGE_ALIGN_PERM
40	_locore_end = .;
41
42	_lorodata_start = .;
43	*(.lorodata)
44	MMU_PAGE_ALIGN_PERM
45	_lodata_start = .;
46
47	*(.lodata)
48
49#ifdef CONFIG_X86_KPTI
50	/* Special page containing supervisor data that is still mapped in
51	 * user mode page tables. GDT, TSSes, trampoline stack, and
52	 * any LDT must go here as they always must live in a page that is
53	 * marked 'present'. Still not directly user accessible, but
54	 * no sensitive data should be here as Meltdown exploits may read it.
55	 *
56	 * On x86-64 the IDT is in rodata and doesn't need to be in the
57	 * trampoline page.
58	 */
59	MMU_PAGE_ALIGN_PERM
60	z_shared_kernel_page_start = .;
61#endif /* CONFIG_X86_KPTI */
62
63        *(.boot_arg)
64	*(.tss)
65	*(.gdt)
66
67#ifdef CONFIG_X86_KPTI
68	*(.trampolines)
69	MMU_PAGE_ALIGN_PERM
70	z_shared_kernel_page_end = .;
71
72	ASSERT(z_shared_kernel_page_end - z_shared_kernel_page_start == 4096,
73	       "shared kernel area is not one memory page");
74#endif /* CONFIG_X86_KPTI */
75
76	. = ALIGN(CONFIG_MMU_PAGE_SIZE);
77	_lodata_end = .;
78	} > LOCORE
79
80	_locore_size = _lorodata_start - _locore_start;
81	_lorodata_size = _lodata_start - _lorodata_start;
82	_lodata_size = _lodata_end - _lodata_start;
83
84	/*
85	 * The rest of the system is loaded in "normal" memory (typically
86	 * placed above 1MB to avoid the by memory hole at 0x90000-0xFFFFF).
87	 */
88
89	SECTION_PROLOGUE(_TEXT_SECTION_NAME,,)
90	{
91	. = ALIGN(16);
92	__rom_region_start = .;
93	__text_region_start = .;
94	z_mapped_start = .;
95	*(.text)
96	*(.text.*)
97
98	#include <zephyr/linker/kobject-text.ld>
99
100	MMU_PAGE_ALIGN_PERM
101	} GROUP_ROM_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
102
103	__text_region_end = .;
104	__text_region_size = __text_region_end - __text_region_start;
105	__rodata_region_start = .;
106
107	#include <zephyr/linker/common-rom.ld>
108	/* Located in generated directory. This file is populated by calling
109	 * zephyr_linker_sources(ROM_SECTIONS ...). Useful for grouping iterable RO structs.
110	 */
111	#include <snippets-rom-sections.ld>
112	#include <zephyr/linker/thread-local-storage.ld>
113
114	SECTION_PROLOGUE(_RODATA_SECTION_NAME,,)
115	{
116	. = ALIGN(16);
117	*(.rodata)
118	*(.rodata.*)
119
120	MMU_PAGE_ALIGN
121	#include <snippets-rodata.ld>
122
123#ifdef CONFIG_X86_MMU
124	. = ALIGN(8);
125	_mmu_region_list_start = .;
126	KEEP(*("._mmu_region.static.*"))
127	_mmu_region_list_end = .;
128#endif /* CONFIG_X86_MMU */
129
130	#include <zephyr/linker/kobject-rom.ld>
131	} GROUP_ROM_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
132
133#include <zephyr/linker/cplusplus-rom.ld>
134
135	MMU_PAGE_ALIGN_PERM
136	__rodata_region_end = .;
137	__rodata_region_size = __rodata_region_end - __rodata_region_start;
138	__rom_region_end = .;
139
140#ifdef CONFIG_USERSPACE
141	/* APP SHARED MEMORY REGION */
142#define SMEM_PARTITION_ALIGN(size) MMU_PAGE_ALIGN_PERM
143#define APP_SHARED_ALIGN  MMU_PAGE_ALIGN_PERM
144
145#include <app_smem.ld>
146
147	_image_ram_start = _app_smem_start;
148	_app_smem_size = _app_smem_end - _app_smem_start;
149	_app_smem_num_words = _app_smem_size >> 2;
150	_app_smem_rom_start = LOADADDR(_APP_SMEM_SECTION_NAME);
151	_app_smem_num_words = _app_smem_size >> 2;
152#endif /* CONFIG_USERSPACE */
153
154/* This should be put here before BSS section, otherwise the .bss.__gcov will
155 * be put in BSS section. That causes gcov not work properly */
156#include <snippets-ram-sections.ld>
157
158	SECTION_PROLOGUE(_BSS_SECTION_NAME, (NOLOAD),)
159	{
160	. = ALIGN(16);
161	MMU_PAGE_ALIGN_PERM
162#ifndef CONFIG_USERSPACE
163	_image_ram_start = .;
164#endif
165	__kernel_ram_start = .;
166	__bss_start = .;
167	*(.bss)
168	*(.bss.*)
169	*(COMMON)
170	. = ALIGN(4);	/* so __bss_num_dwords is exact */
171	__bss_end = .;
172	} GROUP_NOLOAD_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
173
174	__bss_num_dwords = (__bss_end - __bss_start) >> 2;
175
176#include <zephyr/linker/common-noinit.ld>
177
178#include <snippets-sections.ld>
179
180	SECTION_PROLOGUE(_DATA_SECTION_NAME,,)
181	{
182	. = ALIGN(16);
183	*(.data)
184	*(.data.*)
185	#include <snippets-rwdata.ld>
186	} GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
187
188#include <zephyr/linker/common-ram.ld>
189#include <zephyr/linker/cplusplus-ram.ld>
190#include <zephyr/arch/x86/pagetables.ld>
191
192/* Located in generated directory. This file is populated by the
193 * zephyr_linker_sources() Cmake function.
194 */
195#include <snippets-data-sections.ld>
196
197/* Must be last in RAM */
198#include <zephyr/linker/kobject-data.ld>
199
200#define LAST_RAM_ALIGN MMU_PAGE_ALIGN
201
202#include <zephyr/linker/ram-end.ld>
203
204    GROUP_END(RAMABLE_REGION)
205
206	/* All unused memory also owned by the kernel for heaps */
207	__kernel_ram_end = KERNEL_BASE_ADDR + KERNEL_RAM_SIZE;
208	__kernel_ram_size = __kernel_ram_end - __kernel_ram_start;
209
210	z_mapped_size = z_mapped_end - z_mapped_start;
211
212#include <zephyr/linker/debug-sections.ld>
213
214	/DISCARD/ :
215	{
216	*(.got)
217	*(.got.plt)
218	*(.igot)
219	*(.igot.plt)
220	*(.iplt)
221	*(.plt)
222	*(.note.GNU-stack)
223	*(.rel.*)
224	*(.rela.*)
225	}
226/*
227 * eh_frame section won't be removed even with "--gc-sections" by LLVM lld.
228 */
229#if !defined(CONFIG_CPP_EXCEPTIONS)
230	/DISCARD/ : { *(.eh_frame) }
231#endif
232
233/*
234 * The sections below are still treated as warnings
235 * with "--orphan-handling=warn" by LLVM lld.
236 */
237#if !defined(CONFIG_LLVM_USE_LD)
238	.symtab 0 : { *(.symtab) }
239	.strtab 0 : { *(.strtab) }
240	.shstrtab 0 : { *(.shstrtab) }
241#endif
242
243#ifdef CONFIG_LLEXT
244	#include <zephyr/linker/llext-sections.ld>
245#endif
246}
247