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#define RO_EXCEPTION_TABLE_ALIGN	8
9#define RUNTIME_DISCARD_EXIT
10
11#include <asm-generic/vmlinux.lds.h>
12#include <asm/cache.h>
13#include <asm/hyp_image.h>
14#include <asm/kernel-pgtable.h>
15#include <asm/memory.h>
16#include <asm/page.h>
17
18#include "image.h"
19
20OUTPUT_ARCH(aarch64)
21ENTRY(_text)
22
23jiffies = jiffies_64;
24
25
26#ifdef CONFIG_KVM
27#define HYPERVISOR_EXTABLE					\
28	. = ALIGN(SZ_8);					\
29	__start___kvm_ex_table = .;				\
30	*(__kvm_ex_table)					\
31	__stop___kvm_ex_table = .;
32
33#define HYPERVISOR_PERCPU_SECTION				\
34	. = ALIGN(PAGE_SIZE);					\
35	HYP_SECTION_NAME(.data..percpu) : {			\
36		*(HYP_SECTION_NAME(.data..percpu))		\
37	}
38#else /* CONFIG_KVM */
39#define HYPERVISOR_EXTABLE
40#define HYPERVISOR_PERCPU_SECTION
41#endif
42
43#define HYPERVISOR_TEXT					\
44	/*						\
45	 * Align to 4 KB so that			\
46	 * a) the HYP vector table is at its minimum	\
47	 *    alignment of 2048 bytes			\
48	 * b) the HYP init code will not cross a page	\
49	 *    boundary if its size does not exceed	\
50	 *    4 KB (see related ASSERT() below)		\
51	 */						\
52	. = ALIGN(SZ_4K);				\
53	__hyp_idmap_text_start = .;			\
54	*(.hyp.idmap.text)				\
55	__hyp_idmap_text_end = .;			\
56	__hyp_text_start = .;				\
57	*(.hyp.text)					\
58	HYPERVISOR_EXTABLE				\
59	__hyp_text_end = .;
60
61#define IDMAP_TEXT					\
62	. = ALIGN(SZ_4K);				\
63	__idmap_text_start = .;				\
64	*(.idmap.text)					\
65	__idmap_text_end = .;
66
67#ifdef CONFIG_HIBERNATION
68#define HIBERNATE_TEXT					\
69	. = ALIGN(SZ_4K);				\
70	__hibernate_exit_text_start = .;		\
71	*(.hibernate_exit.text)				\
72	__hibernate_exit_text_end = .;
73#else
74#define HIBERNATE_TEXT
75#endif
76
77#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
78#define TRAMP_TEXT					\
79	. = ALIGN(PAGE_SIZE);				\
80	__entry_tramp_text_start = .;			\
81	*(.entry.tramp.text)				\
82	. = ALIGN(PAGE_SIZE);				\
83	__entry_tramp_text_end = .;
84#else
85#define TRAMP_TEXT
86#endif
87
88/*
89 * The size of the PE/COFF section that covers the kernel image, which
90 * runs from _stext to _edata, must be a round multiple of the PE/COFF
91 * FileAlignment, which we set to its minimum value of 0x200. '_stext'
92 * itself is 4 KB aligned, so padding out _edata to a 0x200 aligned
93 * boundary should be sufficient.
94 */
95PECOFF_FILE_ALIGNMENT = 0x200;
96
97#ifdef CONFIG_EFI
98#define PECOFF_EDATA_PADDING	\
99	.pecoff_edata_padding : { BYTE(0); . = ALIGN(PECOFF_FILE_ALIGNMENT); }
100#else
101#define PECOFF_EDATA_PADDING
102#endif
103
104SECTIONS
105{
106	/*
107	 * XXX: The linker does not define how output sections are
108	 * assigned to input sections when there are multiple statements
109	 * matching the same input section name.  There is no documented
110	 * order of matching.
111	 */
112	DISCARDS
113	/DISCARD/ : {
114		*(.interp .dynamic)
115		*(.dynsym .dynstr .hash .gnu.hash)
116	}
117
118	. = KIMAGE_VADDR;
119
120	.head.text : {
121		_text = .;
122		HEAD_TEXT
123	}
124	.text : {			/* Real text segment		*/
125		_stext = .;		/* Text and read-only data	*/
126			IRQENTRY_TEXT
127			SOFTIRQENTRY_TEXT
128			ENTRY_TEXT
129			TEXT_TEXT
130			SCHED_TEXT
131			CPUIDLE_TEXT
132			LOCK_TEXT
133			KPROBES_TEXT
134			HYPERVISOR_TEXT
135			IDMAP_TEXT
136			HIBERNATE_TEXT
137			TRAMP_TEXT
138			*(.fixup)
139			*(.gnu.warning)
140		. = ALIGN(16);
141		*(.got)			/* Global offset table		*/
142	}
143
144	/*
145	 * Make sure that the .got.plt is either completely empty or it
146	 * contains only the lazy dispatch entries.
147	 */
148	.got.plt : { *(.got.plt) }
149	ASSERT(SIZEOF(.got.plt) == 0 || SIZEOF(.got.plt) == 0x18,
150	       "Unexpected GOT/PLT entries detected!")
151
152	. = ALIGN(SEGMENT_ALIGN);
153	_etext = .;			/* End of text section */
154
155	/* everything from this point to __init_begin will be marked RO NX */
156	RO_DATA(PAGE_SIZE)
157
158	idmap_pg_dir = .;
159	. += IDMAP_DIR_SIZE;
160	idmap_pg_end = .;
161
162#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
163	tramp_pg_dir = .;
164	. += PAGE_SIZE;
165#endif
166
167#ifdef CONFIG_ARM64_SW_TTBR0_PAN
168	reserved_ttbr0 = .;
169	. += RESERVED_TTBR0_SIZE;
170#endif
171	swapper_pg_dir = .;
172	. += PAGE_SIZE;
173	swapper_pg_end = .;
174
175	. = ALIGN(SEGMENT_ALIGN);
176	__init_begin = .;
177	__inittext_begin = .;
178
179	INIT_TEXT_SECTION(8)
180
181	__exittext_begin = .;
182	.exit.text : {
183		EXIT_TEXT
184	}
185	__exittext_end = .;
186
187	. = ALIGN(4);
188	.altinstructions : {
189		__alt_instructions = .;
190		*(.altinstructions)
191		__alt_instructions_end = .;
192	}
193
194	. = ALIGN(SEGMENT_ALIGN);
195	__inittext_end = .;
196	__initdata_begin = .;
197
198	.init.data : {
199		INIT_DATA
200		INIT_SETUP(16)
201		INIT_CALLS
202		CON_INITCALL
203		INIT_RAM_FS
204		*(.init.rodata.* .init.bss)	/* from the EFI stub */
205	}
206	.exit.data : {
207		EXIT_DATA
208	}
209
210	PERCPU_SECTION(L1_CACHE_BYTES)
211	HYPERVISOR_PERCPU_SECTION
212
213	.rela.dyn : ALIGN(8) {
214		*(.rela .rela*)
215	}
216
217	__rela_offset	= ABSOLUTE(ADDR(.rela.dyn) - KIMAGE_VADDR);
218	__rela_size	= SIZEOF(.rela.dyn);
219
220#ifdef CONFIG_RELR
221	.relr.dyn : ALIGN(8) {
222		*(.relr.dyn)
223	}
224
225	__relr_offset	= ABSOLUTE(ADDR(.relr.dyn) - KIMAGE_VADDR);
226	__relr_size	= SIZEOF(.relr.dyn);
227#endif
228
229	. = ALIGN(SEGMENT_ALIGN);
230	__initdata_end = .;
231	__init_end = .;
232
233	_data = .;
234	_sdata = .;
235	RW_DATA(L1_CACHE_BYTES, PAGE_SIZE, THREAD_ALIGN)
236
237	/*
238	 * Data written with the MMU off but read with the MMU on requires
239	 * cache lines to be invalidated, discarding up to a Cache Writeback
240	 * Granule (CWG) of data from the cache. Keep the section that
241	 * requires this type of maintenance to be in its own Cache Writeback
242	 * Granule (CWG) area so the cache maintenance operations don't
243	 * interfere with adjacent data.
244	 */
245	.mmuoff.data.write : ALIGN(SZ_2K) {
246		__mmuoff_data_start = .;
247		*(.mmuoff.data.write)
248	}
249	. = ALIGN(SZ_2K);
250	.mmuoff.data.read : {
251		*(.mmuoff.data.read)
252		__mmuoff_data_end = .;
253	}
254
255	PECOFF_EDATA_PADDING
256	__pecoff_data_rawsize = ABSOLUTE(. - __initdata_begin);
257	_edata = .;
258
259	BSS_SECTION(0, 0, 0)
260
261	. = ALIGN(PAGE_SIZE);
262	init_pg_dir = .;
263	. += INIT_DIR_SIZE;
264	init_pg_end = .;
265
266	. = ALIGN(SEGMENT_ALIGN);
267	__pecoff_data_size = ABSOLUTE(. - __initdata_begin);
268	_end = .;
269
270	STABS_DEBUG
271	DWARF_DEBUG
272	ELF_DETAILS
273
274	HEAD_SYMBOLS
275
276	/*
277	 * Sections that should stay zero sized, which is safer to
278	 * explicitly check instead of blindly discarding.
279	 */
280	.plt : {
281		*(.plt) *(.plt.*) *(.iplt) *(.igot .igot.plt)
282	}
283	ASSERT(SIZEOF(.plt) == 0, "Unexpected run-time procedure linkages detected!")
284
285	.data.rel.ro : { *(.data.rel.ro) }
286	ASSERT(SIZEOF(.data.rel.ro) == 0, "Unexpected RELRO detected!")
287}
288
289#include "image-vars.h"
290
291/*
292 * The HYP init code and ID map text can't be longer than a page each,
293 * and should not cross a page boundary.
294 */
295ASSERT(__hyp_idmap_text_end - (__hyp_idmap_text_start & ~(SZ_4K - 1)) <= SZ_4K,
296	"HYP init code too big or misaligned")
297ASSERT(__idmap_text_end - (__idmap_text_start & ~(SZ_4K - 1)) <= SZ_4K,
298	"ID map text too big or misaligned")
299#ifdef CONFIG_HIBERNATION
300ASSERT(__hibernate_exit_text_end - (__hibernate_exit_text_start & ~(SZ_4K - 1))
301	<= SZ_4K, "Hibernate exit text too big or misaligned")
302#endif
303#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
304ASSERT((__entry_tramp_text_end - __entry_tramp_text_start) == PAGE_SIZE,
305	"Entry trampoline text too big")
306#endif
307/*
308 * If padding is applied before .head.text, virt<->phys conversions will fail.
309 */
310ASSERT(_text == KIMAGE_VADDR, "HEAD is misaligned")
311