xref: /openbmc/linux/arch/arm64/kernel/vmlinux.lds.S (revision bf6e667f)
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/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			*(.gnu.warning)
165		. = ALIGN(16);
166		*(.got)			/* Global offset table		*/
167	}
168
169	/*
170	 * Make sure that the .got.plt is either completely empty or it
171	 * contains only the lazy dispatch entries.
172	 */
173	.got.plt : { *(.got.plt) }
174	ASSERT(SIZEOF(.got.plt) == 0 || SIZEOF(.got.plt) == 0x18,
175	       "Unexpected GOT/PLT entries detected!")
176
177	. = ALIGN(SEGMENT_ALIGN);
178	_etext = .;			/* End of text section */
179
180	/* everything from this point to __init_begin will be marked RO NX */
181	RO_DATA(PAGE_SIZE)
182
183	HYPERVISOR_DATA_SECTIONS
184
185	idmap_pg_dir = .;
186	. += IDMAP_DIR_SIZE;
187	idmap_pg_end = .;
188
189#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
190	tramp_pg_dir = .;
191	. += PAGE_SIZE;
192#endif
193
194	reserved_pg_dir = .;
195	. += PAGE_SIZE;
196
197	swapper_pg_dir = .;
198	. += PAGE_SIZE;
199
200	. = ALIGN(SEGMENT_ALIGN);
201	__init_begin = .;
202	__inittext_begin = .;
203
204	INIT_TEXT_SECTION(8)
205
206	__exittext_begin = .;
207	.exit.text : {
208		EXIT_TEXT
209	}
210	__exittext_end = .;
211
212	. = ALIGN(4);
213	.altinstructions : {
214		__alt_instructions = .;
215		*(.altinstructions)
216		__alt_instructions_end = .;
217	}
218
219	. = ALIGN(SEGMENT_ALIGN);
220	__inittext_end = .;
221	__initdata_begin = .;
222
223	.init.data : {
224		INIT_DATA
225		INIT_SETUP(16)
226		INIT_CALLS
227		CON_INITCALL
228		INIT_RAM_FS
229		*(.init.altinstructions .init.bss)	/* from the EFI stub */
230	}
231	.exit.data : {
232		EXIT_DATA
233	}
234
235	PERCPU_SECTION(L1_CACHE_BYTES)
236	HYPERVISOR_PERCPU_SECTION
237
238	HYPERVISOR_RELOC_SECTION
239
240	.rela.dyn : ALIGN(8) {
241		*(.rela .rela*)
242	}
243
244	__rela_offset	= ABSOLUTE(ADDR(.rela.dyn) - KIMAGE_VADDR);
245	__rela_size	= SIZEOF(.rela.dyn);
246
247#ifdef CONFIG_RELR
248	.relr.dyn : ALIGN(8) {
249		*(.relr.dyn)
250	}
251
252	__relr_offset	= ABSOLUTE(ADDR(.relr.dyn) - KIMAGE_VADDR);
253	__relr_size	= SIZEOF(.relr.dyn);
254#endif
255
256	. = ALIGN(SEGMENT_ALIGN);
257	__initdata_end = .;
258	__init_end = .;
259
260	_data = .;
261	_sdata = .;
262	RW_DATA(L1_CACHE_BYTES, PAGE_SIZE, THREAD_ALIGN)
263
264	/*
265	 * Data written with the MMU off but read with the MMU on requires
266	 * cache lines to be invalidated, discarding up to a Cache Writeback
267	 * Granule (CWG) of data from the cache. Keep the section that
268	 * requires this type of maintenance to be in its own Cache Writeback
269	 * Granule (CWG) area so the cache maintenance operations don't
270	 * interfere with adjacent data.
271	 */
272	.mmuoff.data.write : ALIGN(SZ_2K) {
273		__mmuoff_data_start = .;
274		*(.mmuoff.data.write)
275	}
276	. = ALIGN(SZ_2K);
277	.mmuoff.data.read : {
278		*(.mmuoff.data.read)
279		__mmuoff_data_end = .;
280	}
281
282	PECOFF_EDATA_PADDING
283	__pecoff_data_rawsize = ABSOLUTE(. - __initdata_begin);
284	_edata = .;
285
286	BSS_SECTION(SBSS_ALIGN, 0, 0)
287
288	. = ALIGN(PAGE_SIZE);
289	init_pg_dir = .;
290	. += INIT_DIR_SIZE;
291	init_pg_end = .;
292
293	. = ALIGN(SEGMENT_ALIGN);
294	__pecoff_data_size = ABSOLUTE(. - __initdata_begin);
295	_end = .;
296
297	STABS_DEBUG
298	DWARF_DEBUG
299	ELF_DETAILS
300
301	HEAD_SYMBOLS
302
303	/*
304	 * Sections that should stay zero sized, which is safer to
305	 * explicitly check instead of blindly discarding.
306	 */
307	.plt : {
308		*(.plt) *(.plt.*) *(.iplt) *(.igot .igot.plt)
309	}
310	ASSERT(SIZEOF(.plt) == 0, "Unexpected run-time procedure linkages detected!")
311
312	.data.rel.ro : { *(.data.rel.ro) }
313	ASSERT(SIZEOF(.data.rel.ro) == 0, "Unexpected RELRO detected!")
314}
315
316#include "image-vars.h"
317
318/*
319 * The HYP init code and ID map text can't be longer than a page each. The
320 * former is page-aligned, but the latter may not be with 16K or 64K pages, so
321 * it should also not cross a page boundary.
322 */
323ASSERT(__hyp_idmap_text_end - __hyp_idmap_text_start <= PAGE_SIZE,
324	"HYP init code too big")
325ASSERT(__idmap_text_end - (__idmap_text_start & ~(SZ_4K - 1)) <= SZ_4K,
326	"ID map text too big or misaligned")
327#ifdef CONFIG_HIBERNATION
328ASSERT(__hibernate_exit_text_end - (__hibernate_exit_text_start & ~(SZ_4K - 1))
329	<= SZ_4K, "Hibernate exit text too big or misaligned")
330#endif
331#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
332ASSERT((__entry_tramp_text_end - __entry_tramp_text_start) == PAGE_SIZE,
333	"Entry trampoline text too big")
334#endif
335#ifdef CONFIG_KVM
336ASSERT(__hyp_bss_start == __bss_start, "HYP and Host BSS are misaligned")
337#endif
338/*
339 * If padding is applied before .head.text, virt<->phys conversions will fail.
340 */
341ASSERT(_text == KIMAGE_VADDR, "HEAD is misaligned")
342
343ASSERT(swapper_pg_dir - reserved_pg_dir == RESERVED_SWAPPER_OFFSET,
344       "RESERVED_SWAPPER_OFFSET is wrong!")
345
346#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
347ASSERT(swapper_pg_dir - tramp_pg_dir == TRAMP_SWAPPER_OFFSET,
348       "TRAMP_SWAPPER_OFFSET is wrong!")
349#endif
350