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