xref: /linux/arch/arm64/kernel/vmlinux.lds.S (revision 156010ed9c2ac1e9df6c11b1f688cf8a6e0152e6)
1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * ld script to make ARM Linux kernel
4 * taken from the i386 version by Russell King
5 * Written by Martin Mares <mj@atrey.karlin.mff.cuni.cz>
6 */
7
8#include <asm/hyp_image.h>
9#ifdef CONFIG_KVM
10#define HYPERVISOR_EXTABLE					\
11	. = ALIGN(SZ_8);					\
12	__start___kvm_ex_table = .;				\
13	*(__kvm_ex_table)					\
14	__stop___kvm_ex_table = .;
15
16#define HYPERVISOR_DATA_SECTIONS				\
17	HYP_SECTION_NAME(.rodata) : {				\
18		. = ALIGN(PAGE_SIZE);				\
19		__hyp_rodata_start = .;				\
20		*(HYP_SECTION_NAME(.data..ro_after_init))	\
21		*(HYP_SECTION_NAME(.rodata))			\
22		. = ALIGN(PAGE_SIZE);				\
23		__hyp_rodata_end = .;				\
24	}
25
26#define HYPERVISOR_PERCPU_SECTION				\
27	. = ALIGN(PAGE_SIZE);					\
28	HYP_SECTION_NAME(.data..percpu) : {			\
29		*(HYP_SECTION_NAME(.data..percpu))		\
30	}
31
32#define HYPERVISOR_RELOC_SECTION				\
33	.hyp.reloc : ALIGN(4) {					\
34		__hyp_reloc_begin = .;				\
35		*(.hyp.reloc)					\
36		__hyp_reloc_end = .;				\
37	}
38
39#define BSS_FIRST_SECTIONS					\
40	__hyp_bss_start = .;					\
41	*(HYP_SECTION_NAME(.bss))				\
42	. = ALIGN(PAGE_SIZE);					\
43	__hyp_bss_end = .;
44
45/*
46 * We require that __hyp_bss_start and __bss_start are aligned, and enforce it
47 * with an assertion. But the BSS_SECTION macro places an empty .sbss section
48 * between them, which can in some cases cause the linker to misalign them. To
49 * work around the issue, force a page alignment for __bss_start.
50 */
51#define SBSS_ALIGN			PAGE_SIZE
52#else /* CONFIG_KVM */
53#define HYPERVISOR_EXTABLE
54#define HYPERVISOR_DATA_SECTIONS
55#define HYPERVISOR_PERCPU_SECTION
56#define HYPERVISOR_RELOC_SECTION
57#define SBSS_ALIGN			0
58#endif
59
60#define RO_EXCEPTION_TABLE_ALIGN	4
61#define RUNTIME_DISCARD_EXIT
62
63#include <asm-generic/vmlinux.lds.h>
64#include <asm/cache.h>
65#include <asm/kernel-pgtable.h>
66#include <asm/kexec.h>
67#include <asm/memory.h>
68#include <asm/page.h>
69
70#include "image.h"
71
72OUTPUT_ARCH(aarch64)
73ENTRY(_text)
74
75jiffies = jiffies_64;
76
77#define HYPERVISOR_TEXT					\
78	. = ALIGN(PAGE_SIZE);				\
79	__hyp_idmap_text_start = .;			\
80	*(.hyp.idmap.text)				\
81	__hyp_idmap_text_end = .;			\
82	__hyp_text_start = .;				\
83	*(.hyp.text)					\
84	HYPERVISOR_EXTABLE				\
85	. = ALIGN(PAGE_SIZE);				\
86	__hyp_text_end = .;
87
88#define IDMAP_TEXT					\
89	. = ALIGN(SZ_4K);				\
90	__idmap_text_start = .;				\
91	*(.idmap.text)					\
92	__idmap_text_end = .;
93
94#ifdef CONFIG_HIBERNATION
95#define HIBERNATE_TEXT					\
96	ALIGN_FUNCTION();				\
97	__hibernate_exit_text_start = .;		\
98	*(.hibernate_exit.text)				\
99	__hibernate_exit_text_end = .;
100#else
101#define HIBERNATE_TEXT
102#endif
103
104#ifdef CONFIG_KEXEC_CORE
105#define KEXEC_TEXT					\
106	ALIGN_FUNCTION();				\
107	__relocate_new_kernel_start = .;		\
108	*(.kexec_relocate.text)				\
109	__relocate_new_kernel_end = .;
110#else
111#define KEXEC_TEXT
112#endif
113
114#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
115#define TRAMP_TEXT					\
116	. = ALIGN(PAGE_SIZE);				\
117	__entry_tramp_text_start = .;			\
118	*(.entry.tramp.text)				\
119	. = ALIGN(PAGE_SIZE);				\
120	__entry_tramp_text_end = .;			\
121	*(.entry.tramp.rodata)
122#else
123#define TRAMP_TEXT
124#endif
125
126#ifdef CONFIG_UNWIND_TABLES
127#define UNWIND_DATA_SECTIONS				\
128	.eh_frame : {					\
129		__eh_frame_start = .;			\
130		*(.eh_frame)				\
131		__eh_frame_end = .;			\
132	}
133#else
134#define UNWIND_DATA_SECTIONS
135#endif
136
137/*
138 * The size of the PE/COFF section that covers the kernel image, which
139 * runs from _stext to _edata, must be a round multiple of the PE/COFF
140 * FileAlignment, which we set to its minimum value of 0x200. '_stext'
141 * itself is 4 KB aligned, so padding out _edata to a 0x200 aligned
142 * boundary should be sufficient.
143 */
144PECOFF_FILE_ALIGNMENT = 0x200;
145
146#ifdef CONFIG_EFI
147#define PECOFF_EDATA_PADDING	\
148	.pecoff_edata_padding : { BYTE(0); . = ALIGN(PECOFF_FILE_ALIGNMENT); }
149#else
150#define PECOFF_EDATA_PADDING
151#endif
152
153SECTIONS
154{
155	/*
156	 * XXX: The linker does not define how output sections are
157	 * assigned to input sections when there are multiple statements
158	 * matching the same input section name.  There is no documented
159	 * order of matching.
160	 */
161	DISCARDS
162	/DISCARD/ : {
163		*(.interp .dynamic)
164		*(.dynsym .dynstr .hash .gnu.hash)
165	}
166
167	. = KIMAGE_VADDR;
168
169	.head.text : {
170		_text = .;
171		HEAD_TEXT
172	}
173	.text : ALIGN(SEGMENT_ALIGN) {	/* Real text segment		*/
174		_stext = .;		/* Text and read-only data	*/
175			IRQENTRY_TEXT
176			SOFTIRQENTRY_TEXT
177			ENTRY_TEXT
178			TEXT_TEXT
179			SCHED_TEXT
180			CPUIDLE_TEXT
181			LOCK_TEXT
182			KPROBES_TEXT
183			HYPERVISOR_TEXT
184			*(.gnu.warning)
185		. = ALIGN(16);
186		*(.got)			/* Global offset table		*/
187	}
188
189	/*
190	 * Make sure that the .got.plt is either completely empty or it
191	 * contains only the lazy dispatch entries.
192	 */
193	.got.plt : { *(.got.plt) }
194	ASSERT(SIZEOF(.got.plt) == 0 || SIZEOF(.got.plt) == 0x18,
195	       "Unexpected GOT/PLT entries detected!")
196
197	. = ALIGN(SEGMENT_ALIGN);
198	_etext = .;			/* End of text section */
199
200	/* everything from this point to __init_begin will be marked RO NX */
201	RO_DATA(PAGE_SIZE)
202
203	HYPERVISOR_DATA_SECTIONS
204
205	/* code sections that are never executed via the kernel mapping */
206	.rodata.text : {
207		TRAMP_TEXT
208		HIBERNATE_TEXT
209		KEXEC_TEXT
210		IDMAP_TEXT
211		. = ALIGN(PAGE_SIZE);
212	}
213
214	idmap_pg_dir = .;
215	. += PAGE_SIZE;
216
217#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
218	tramp_pg_dir = .;
219	. += PAGE_SIZE;
220#endif
221
222	reserved_pg_dir = .;
223	. += PAGE_SIZE;
224
225	swapper_pg_dir = .;
226	. += PAGE_SIZE;
227
228	. = ALIGN(SEGMENT_ALIGN);
229	__init_begin = .;
230	__inittext_begin = .;
231
232	INIT_TEXT_SECTION(8)
233
234	__exittext_begin = .;
235	.exit.text : {
236		EXIT_TEXT
237	}
238	__exittext_end = .;
239
240	. = ALIGN(4);
241	.altinstructions : {
242		__alt_instructions = .;
243		*(.altinstructions)
244		__alt_instructions_end = .;
245	}
246
247	UNWIND_DATA_SECTIONS
248
249	. = ALIGN(SEGMENT_ALIGN);
250	__inittext_end = .;
251	__initdata_begin = .;
252
253	init_idmap_pg_dir = .;
254	. += INIT_IDMAP_DIR_SIZE;
255	init_idmap_pg_end = .;
256
257	.init.data : {
258		INIT_DATA
259		INIT_SETUP(16)
260		INIT_CALLS
261		CON_INITCALL
262		INIT_RAM_FS
263		*(.init.altinstructions .init.bss)	/* from the EFI stub */
264	}
265	.exit.data : {
266		EXIT_DATA
267	}
268
269	PERCPU_SECTION(L1_CACHE_BYTES)
270	HYPERVISOR_PERCPU_SECTION
271
272	HYPERVISOR_RELOC_SECTION
273
274	.rela.dyn : ALIGN(8) {
275		__rela_start = .;
276		*(.rela .rela*)
277		__rela_end = .;
278	}
279
280	.relr.dyn : ALIGN(8) {
281		__relr_start = .;
282		*(.relr.dyn)
283		__relr_end = .;
284	}
285
286	. = ALIGN(SEGMENT_ALIGN);
287	__initdata_end = .;
288	__init_end = .;
289
290	_data = .;
291	_sdata = .;
292	RW_DATA(L1_CACHE_BYTES, PAGE_SIZE, THREAD_ALIGN)
293
294	/*
295	 * Data written with the MMU off but read with the MMU on requires
296	 * cache lines to be invalidated, discarding up to a Cache Writeback
297	 * Granule (CWG) of data from the cache. Keep the section that
298	 * requires this type of maintenance to be in its own Cache Writeback
299	 * Granule (CWG) area so the cache maintenance operations don't
300	 * interfere with adjacent data.
301	 */
302	.mmuoff.data.write : ALIGN(SZ_2K) {
303		__mmuoff_data_start = .;
304		*(.mmuoff.data.write)
305	}
306	. = ALIGN(SZ_2K);
307	.mmuoff.data.read : {
308		*(.mmuoff.data.read)
309		__mmuoff_data_end = .;
310	}
311
312	PECOFF_EDATA_PADDING
313	__pecoff_data_rawsize = ABSOLUTE(. - __initdata_begin);
314	_edata = .;
315
316	BSS_SECTION(SBSS_ALIGN, 0, 0)
317
318	. = ALIGN(PAGE_SIZE);
319	init_pg_dir = .;
320	. += INIT_DIR_SIZE;
321	init_pg_end = .;
322
323	. = ALIGN(SEGMENT_ALIGN);
324	__pecoff_data_size = ABSOLUTE(. - __initdata_begin);
325	_end = .;
326
327	STABS_DEBUG
328	DWARF_DEBUG
329	ELF_DETAILS
330
331	HEAD_SYMBOLS
332
333	/*
334	 * Sections that should stay zero sized, which is safer to
335	 * explicitly check instead of blindly discarding.
336	 */
337	.plt : {
338		*(.plt) *(.plt.*) *(.iplt) *(.igot .igot.plt)
339	}
340	ASSERT(SIZEOF(.plt) == 0, "Unexpected run-time procedure linkages detected!")
341
342	.data.rel.ro : { *(.data.rel.ro) }
343	ASSERT(SIZEOF(.data.rel.ro) == 0, "Unexpected RELRO detected!")
344}
345
346#include "image-vars.h"
347
348/*
349 * The HYP init code and ID map text can't be longer than a page each. The
350 * former is page-aligned, but the latter may not be with 16K or 64K pages, so
351 * it should also not cross a page boundary.
352 */
353ASSERT(__hyp_idmap_text_end - __hyp_idmap_text_start <= PAGE_SIZE,
354	"HYP init code too big")
355ASSERT(__idmap_text_end - (__idmap_text_start & ~(SZ_4K - 1)) <= SZ_4K,
356	"ID map text too big or misaligned")
357#ifdef CONFIG_HIBERNATION
358ASSERT(__hibernate_exit_text_end - __hibernate_exit_text_start <= SZ_4K,
359       "Hibernate exit text is bigger than 4 KiB")
360ASSERT(__hibernate_exit_text_start == swsusp_arch_suspend_exit,
361       "Hibernate exit text does not start with swsusp_arch_suspend_exit")
362#endif
363#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
364ASSERT((__entry_tramp_text_end - __entry_tramp_text_start) <= 3*PAGE_SIZE,
365	"Entry trampoline text too big")
366#endif
367#ifdef CONFIG_KVM
368ASSERT(__hyp_bss_start == __bss_start, "HYP and Host BSS are misaligned")
369#endif
370/*
371 * If padding is applied before .head.text, virt<->phys conversions will fail.
372 */
373ASSERT(_text == KIMAGE_VADDR, "HEAD is misaligned")
374
375ASSERT(swapper_pg_dir - reserved_pg_dir == RESERVED_SWAPPER_OFFSET,
376       "RESERVED_SWAPPER_OFFSET is wrong!")
377
378#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
379ASSERT(swapper_pg_dir - tramp_pg_dir == TRAMP_SWAPPER_OFFSET,
380       "TRAMP_SWAPPER_OFFSET is wrong!")
381#endif
382
383#ifdef CONFIG_KEXEC_CORE
384/* kexec relocation code should fit into one KEXEC_CONTROL_PAGE_SIZE */
385ASSERT(__relocate_new_kernel_end - __relocate_new_kernel_start <= SZ_4K,
386       "kexec relocation code is bigger than 4 KiB")
387ASSERT(KEXEC_CONTROL_PAGE_SIZE >= SZ_4K, "KEXEC_CONTROL_PAGE_SIZE is broken")
388ASSERT(__relocate_new_kernel_start == arm64_relocate_new_kernel,
389       "kexec control page does not start with arm64_relocate_new_kernel")
390#endif
391