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	#include <zephyr/linker/thread-local-storage.ld>
109
110	SECTION_PROLOGUE(_RODATA_SECTION_NAME,,)
111	{
112	. = ALIGN(16);
113	*(.rodata)
114	*(.rodata.*)
115
116	MMU_PAGE_ALIGN
117	#include <snippets-rodata.ld>
118
119#ifdef CONFIG_X86_MMU
120	. = ALIGN(8);
121	_mmu_region_list_start = .;
122	KEEP(*("._mmu_region.static.*"))
123	_mmu_region_list_end = .;
124#endif /* CONFIG_X86_MMU */
125
126	#include <zephyr/linker/kobject-rom.ld>
127	} GROUP_ROM_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
128
129#include <zephyr/linker/cplusplus-rom.ld>
130
131	MMU_PAGE_ALIGN_PERM
132	__rodata_region_end = .;
133	__rodata_region_size = __rodata_region_end - __rodata_region_start;
134	__rom_region_end = .;
135
136#ifdef CONFIG_USERSPACE
137	/* APP SHARED MEMORY REGION */
138#define SMEM_PARTITION_ALIGN(size) MMU_PAGE_ALIGN_PERM
139#define APP_SHARED_ALIGN  MMU_PAGE_ALIGN_PERM
140
141#include <app_smem.ld>
142
143	_image_ram_start = _app_smem_start;
144	_app_smem_size = _app_smem_end - _app_smem_start;
145	_app_smem_num_words = _app_smem_size >> 2;
146	_app_smem_rom_start = LOADADDR(_APP_SMEM_SECTION_NAME);
147	_app_smem_num_words = _app_smem_size >> 2;
148#endif /* CONFIG_USERSPACE */
149
150/* This should be put here before BSS section, otherwise the .bss.__gcov will
151 * be put in BSS section. That causes gcov not work properly */
152#include <snippets-ram-sections.ld>
153
154	SECTION_PROLOGUE(_BSS_SECTION_NAME, (NOLOAD),)
155	{
156	. = ALIGN(16);
157	MMU_PAGE_ALIGN_PERM
158#ifndef CONFIG_USERSPACE
159	_image_ram_start = .;
160#endif
161	__kernel_ram_start = .;
162	__bss_start = .;
163	*(.bss)
164	*(.bss.*)
165	*(COMMON)
166	. = ALIGN(4);	/* so __bss_num_dwords is exact */
167	__bss_end = .;
168	} GROUP_NOLOAD_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
169
170	__bss_num_dwords = (__bss_end - __bss_start) >> 2;
171
172#include <zephyr/linker/common-noinit.ld>
173
174#include <snippets-sections.ld>
175
176	SECTION_PROLOGUE(_DATA_SECTION_NAME,,)
177	{
178	. = ALIGN(16);
179	*(.data)
180	*(.data.*)
181	#include <snippets-rwdata.ld>
182	} GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
183
184#include <zephyr/linker/common-ram.ld>
185#include <zephyr/linker/cplusplus-ram.ld>
186#include <zephyr/arch/x86/pagetables.ld>
187
188/* Located in generated directory. This file is populated by the
189 * zephyr_linker_sources() Cmake function.
190 */
191#include <snippets-data-sections.ld>
192
193/* Must be last in RAM */
194#include <zephyr/linker/kobject-data.ld>
195
196#define LAST_RAM_ALIGN MMU_PAGE_ALIGN
197
198#include <zephyr/linker/ram-end.ld>
199
200    GROUP_END(RAMABLE_REGION)
201
202	/* All unused memory also owned by the kernel for heaps */
203	__kernel_ram_end = KERNEL_BASE_ADDR + KERNEL_RAM_SIZE;
204	__kernel_ram_size = __kernel_ram_end - __kernel_ram_start;
205
206	z_mapped_size = z_mapped_end - z_mapped_start;
207
208#include <zephyr/linker/debug-sections.ld>
209
210	/DISCARD/ :
211	{
212	*(.got)
213	*(.got.plt)
214	*(.igot)
215	*(.igot.plt)
216	*(.iplt)
217	*(.plt)
218	*(.note.GNU-stack)
219	*(.rel.*)
220	*(.rela.*)
221	}
222/*
223 * eh_frame section won't be removed even with "--gc-sections" by LLVM lld.
224 */
225#if !defined(CONFIG_CPP_EXCEPTIONS)
226	/DISCARD/ : { *(.eh_frame) }
227#endif
228
229/*
230 * The sections below are still treated as warnings
231 * with "--orphan-handling=warn" by LLVM lld.
232 */
233#if !defined(CONFIG_LLVM_USE_LD)
234	.symtab 0 : { *(.symtab) }
235	.strtab 0 : { *(.strtab) }
236	.shstrtab 0 : { *(.shstrtab) }
237#endif
238
239}
240