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