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(SZ_4K);				\
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(SZ_4K);				\
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#else
122#define TRAMP_TEXT
123#endif
124
125/*
126 * The size of the PE/COFF section that covers the kernel image, which
127 * runs from _stext to _edata, must be a round multiple of the PE/COFF
128 * FileAlignment, which we set to its minimum value of 0x200. '_stext'
129 * itself is 4 KB aligned, so padding out _edata to a 0x200 aligned
130 * boundary should be sufficient.
131 */
132PECOFF_FILE_ALIGNMENT = 0x200;
133
134#ifdef CONFIG_EFI
135#define PECOFF_EDATA_PADDING	\
136	.pecoff_edata_padding : { BYTE(0); . = ALIGN(PECOFF_FILE_ALIGNMENT); }
137#else
138#define PECOFF_EDATA_PADDING
139#endif
140
141SECTIONS
142{
143	/*
144	 * XXX: The linker does not define how output sections are
145	 * assigned to input sections when there are multiple statements
146	 * matching the same input section name.  There is no documented
147	 * order of matching.
148	 */
149	DISCARDS
150	/DISCARD/ : {
151		*(.interp .dynamic)
152		*(.dynsym .dynstr .hash .gnu.hash)
153	}
154
155	. = KIMAGE_VADDR;
156
157	.head.text : {
158		_text = .;
159		HEAD_TEXT
160	}
161	.text : ALIGN(SEGMENT_ALIGN) {	/* Real text segment		*/
162		_stext = .;		/* Text and read-only data	*/
163			IRQENTRY_TEXT
164			SOFTIRQENTRY_TEXT
165			ENTRY_TEXT
166			TEXT_TEXT
167			SCHED_TEXT
168			CPUIDLE_TEXT
169			LOCK_TEXT
170			KPROBES_TEXT
171			HYPERVISOR_TEXT
172			IDMAP_TEXT
173			HIBERNATE_TEXT
174			KEXEC_TEXT
175			TRAMP_TEXT
176			*(.gnu.warning)
177		. = ALIGN(16);
178		*(.got)			/* Global offset table		*/
179	}
180
181	/*
182	 * Make sure that the .got.plt is either completely empty or it
183	 * contains only the lazy dispatch entries.
184	 */
185	.got.plt : { *(.got.plt) }
186	ASSERT(SIZEOF(.got.plt) == 0 || SIZEOF(.got.plt) == 0x18,
187	       "Unexpected GOT/PLT entries detected!")
188
189	. = ALIGN(SEGMENT_ALIGN);
190	_etext = .;			/* End of text section */
191
192	/* everything from this point to __init_begin will be marked RO NX */
193	RO_DATA(PAGE_SIZE)
194
195	HYPERVISOR_DATA_SECTIONS
196
197	idmap_pg_dir = .;
198	. += IDMAP_DIR_SIZE;
199	idmap_pg_end = .;
200
201#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
202	tramp_pg_dir = .;
203	. += PAGE_SIZE;
204#endif
205
206	reserved_pg_dir = .;
207	. += PAGE_SIZE;
208
209	swapper_pg_dir = .;
210	. += PAGE_SIZE;
211
212	. = ALIGN(SEGMENT_ALIGN);
213	__init_begin = .;
214	__inittext_begin = .;
215
216	INIT_TEXT_SECTION(8)
217
218	__exittext_begin = .;
219	.exit.text : {
220		EXIT_TEXT
221	}
222	__exittext_end = .;
223
224	. = ALIGN(4);
225	.altinstructions : {
226		__alt_instructions = .;
227		*(.altinstructions)
228		__alt_instructions_end = .;
229	}
230
231	. = ALIGN(SEGMENT_ALIGN);
232	__inittext_end = .;
233	__initdata_begin = .;
234
235	.init.data : {
236		INIT_DATA
237		INIT_SETUP(16)
238		INIT_CALLS
239		CON_INITCALL
240		INIT_RAM_FS
241		*(.init.altinstructions .init.bss)	/* from the EFI stub */
242	}
243	.exit.data : {
244		EXIT_DATA
245	}
246
247	PERCPU_SECTION(L1_CACHE_BYTES)
248	HYPERVISOR_PERCPU_SECTION
249
250	HYPERVISOR_RELOC_SECTION
251
252	.rela.dyn : ALIGN(8) {
253		*(.rela .rela*)
254	}
255
256	__rela_offset	= ABSOLUTE(ADDR(.rela.dyn) - KIMAGE_VADDR);
257	__rela_size	= SIZEOF(.rela.dyn);
258
259#ifdef CONFIG_RELR
260	.relr.dyn : ALIGN(8) {
261		*(.relr.dyn)
262	}
263
264	__relr_offset	= ABSOLUTE(ADDR(.relr.dyn) - KIMAGE_VADDR);
265	__relr_size	= SIZEOF(.relr.dyn);
266#endif
267
268	. = ALIGN(SEGMENT_ALIGN);
269	__initdata_end = .;
270	__init_end = .;
271
272	_data = .;
273	_sdata = .;
274	RW_DATA(L1_CACHE_BYTES, PAGE_SIZE, THREAD_ALIGN)
275
276	/*
277	 * Data written with the MMU off but read with the MMU on requires
278	 * cache lines to be invalidated, discarding up to a Cache Writeback
279	 * Granule (CWG) of data from the cache. Keep the section that
280	 * requires this type of maintenance to be in its own Cache Writeback
281	 * Granule (CWG) area so the cache maintenance operations don't
282	 * interfere with adjacent data.
283	 */
284	.mmuoff.data.write : ALIGN(SZ_2K) {
285		__mmuoff_data_start = .;
286		*(.mmuoff.data.write)
287	}
288	. = ALIGN(SZ_2K);
289	.mmuoff.data.read : {
290		*(.mmuoff.data.read)
291		__mmuoff_data_end = .;
292	}
293
294	PECOFF_EDATA_PADDING
295	__pecoff_data_rawsize = ABSOLUTE(. - __initdata_begin);
296	_edata = .;
297
298	BSS_SECTION(SBSS_ALIGN, 0, 0)
299
300	. = ALIGN(PAGE_SIZE);
301	init_pg_dir = .;
302	. += INIT_DIR_SIZE;
303	init_pg_end = .;
304
305	. = ALIGN(SEGMENT_ALIGN);
306	__pecoff_data_size = ABSOLUTE(. - __initdata_begin);
307	_end = .;
308
309	STABS_DEBUG
310	DWARF_DEBUG
311	ELF_DETAILS
312
313	HEAD_SYMBOLS
314
315	/*
316	 * Sections that should stay zero sized, which is safer to
317	 * explicitly check instead of blindly discarding.
318	 */
319	.plt : {
320		*(.plt) *(.plt.*) *(.iplt) *(.igot .igot.plt)
321	}
322	ASSERT(SIZEOF(.plt) == 0, "Unexpected run-time procedure linkages detected!")
323
324	.data.rel.ro : { *(.data.rel.ro) }
325	ASSERT(SIZEOF(.data.rel.ro) == 0, "Unexpected RELRO detected!")
326}
327
328#include "image-vars.h"
329
330/*
331 * The HYP init code and ID map text can't be longer than a page each. The
332 * former is page-aligned, but the latter may not be with 16K or 64K pages, so
333 * it should also not cross a page boundary.
334 */
335ASSERT(__hyp_idmap_text_end - __hyp_idmap_text_start <= PAGE_SIZE,
336	"HYP init code too big")
337ASSERT(__idmap_text_end - (__idmap_text_start & ~(SZ_4K - 1)) <= SZ_4K,
338	"ID map text too big or misaligned")
339#ifdef CONFIG_HIBERNATION
340ASSERT(__hibernate_exit_text_end - (__hibernate_exit_text_start & ~(SZ_4K - 1))
341	<= SZ_4K, "Hibernate exit text too big or misaligned")
342#endif
343#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
344ASSERT((__entry_tramp_text_end - __entry_tramp_text_start) == PAGE_SIZE,
345	"Entry trampoline text too big")
346#endif
347#ifdef CONFIG_KVM
348ASSERT(__hyp_bss_start == __bss_start, "HYP and Host BSS are misaligned")
349#endif
350/*
351 * If padding is applied before .head.text, virt<->phys conversions will fail.
352 */
353ASSERT(_text == KIMAGE_VADDR, "HEAD is misaligned")
354
355ASSERT(swapper_pg_dir - reserved_pg_dir == RESERVED_SWAPPER_OFFSET,
356       "RESERVED_SWAPPER_OFFSET is wrong!")
357
358#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
359ASSERT(swapper_pg_dir - tramp_pg_dir == TRAMP_SWAPPER_OFFSET,
360       "TRAMP_SWAPPER_OFFSET is wrong!")
361#endif
362
363#ifdef CONFIG_KEXEC_CORE
364/* kexec relocation code should fit into one KEXEC_CONTROL_PAGE_SIZE */
365ASSERT(__relocate_new_kernel_end - (__relocate_new_kernel_start & ~(SZ_4K - 1))
366	<= SZ_4K, "kexec relocation code is too big or misaligned")
367ASSERT(KEXEC_CONTROL_PAGE_SIZE >= SZ_4K, "KEXEC_CONTROL_PAGE_SIZE is broken")
368#endif
369