xref: /openbmc/linux/arch/x86/kernel/vmlinux.lds.S (revision 9dae47aba0a055f761176d9297371d5bb24289ec)
1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * ld script for the x86 kernel
4 *
5 * Historic 32-bit version written by Martin Mares <mj@atrey.karlin.mff.cuni.cz>
6 *
7 * Modernisation, unification and other changes and fixes:
8 *   Copyright (C) 2007-2009  Sam Ravnborg <sam@ravnborg.org>
9 *
10 *
11 * Don't define absolute symbols until and unless you know that symbol
12 * value is should remain constant even if kernel image is relocated
13 * at run time. Absolute symbols are not relocated. If symbol value should
14 * change if kernel is relocated, make the symbol section relative and
15 * put it inside the section definition.
16 */
17
18#ifdef CONFIG_X86_32
19#define LOAD_OFFSET __PAGE_OFFSET
20#else
21#define LOAD_OFFSET __START_KERNEL_map
22#endif
23
24#include <asm-generic/vmlinux.lds.h>
25#include <asm/asm-offsets.h>
26#include <asm/thread_info.h>
27#include <asm/page_types.h>
28#include <asm/orc_lookup.h>
29#include <asm/cache.h>
30#include <asm/boot.h>
31
32#undef i386     /* in case the preprocessor is a 32bit one */
33
34OUTPUT_FORMAT(CONFIG_OUTPUT_FORMAT, CONFIG_OUTPUT_FORMAT, CONFIG_OUTPUT_FORMAT)
35
36#ifdef CONFIG_X86_32
37OUTPUT_ARCH(i386)
38ENTRY(phys_startup_32)
39jiffies = jiffies_64;
40#else
41OUTPUT_ARCH(i386:x86-64)
42ENTRY(phys_startup_64)
43jiffies_64 = jiffies;
44#endif
45
46#if defined(CONFIG_X86_64)
47/*
48 * On 64-bit, align RODATA to 2MB so we retain large page mappings for
49 * boundaries spanning kernel text, rodata and data sections.
50 *
51 * However, kernel identity mappings will have different RWX permissions
52 * to the pages mapping to text and to the pages padding (which are freed) the
53 * text section. Hence kernel identity mappings will be broken to smaller
54 * pages. For 64-bit, kernel text and kernel identity mappings are different,
55 * so we can enable protection checks as well as retain 2MB large page
56 * mappings for kernel text.
57 */
58#define X64_ALIGN_RODATA_BEGIN	. = ALIGN(HPAGE_SIZE);
59
60#define X64_ALIGN_RODATA_END					\
61		. = ALIGN(HPAGE_SIZE);				\
62		__end_rodata_hpage_align = .;
63
64#else
65
66#define X64_ALIGN_RODATA_BEGIN
67#define X64_ALIGN_RODATA_END
68
69#endif
70
71PHDRS {
72	text PT_LOAD FLAGS(5);          /* R_E */
73	data PT_LOAD FLAGS(6);          /* RW_ */
74#ifdef CONFIG_X86_64
75#ifdef CONFIG_SMP
76	percpu PT_LOAD FLAGS(6);        /* RW_ */
77#endif
78	init PT_LOAD FLAGS(7);          /* RWE */
79#endif
80	note PT_NOTE FLAGS(0);          /* ___ */
81}
82
83SECTIONS
84{
85#ifdef CONFIG_X86_32
86	. = LOAD_OFFSET + LOAD_PHYSICAL_ADDR;
87	phys_startup_32 = ABSOLUTE(startup_32 - LOAD_OFFSET);
88#else
89	. = __START_KERNEL;
90	phys_startup_64 = ABSOLUTE(startup_64 - LOAD_OFFSET);
91#endif
92
93	/* Text and read-only data */
94	.text :  AT(ADDR(.text) - LOAD_OFFSET) {
95		_text = .;
96		_stext = .;
97		/* bootstrapping code */
98		HEAD_TEXT
99		. = ALIGN(8);
100		TEXT_TEXT
101		SCHED_TEXT
102		CPUIDLE_TEXT
103		LOCK_TEXT
104		KPROBES_TEXT
105		ENTRY_TEXT
106		IRQENTRY_TEXT
107		SOFTIRQENTRY_TEXT
108		*(.fixup)
109		*(.gnu.warning)
110
111#ifdef CONFIG_X86_64
112		. = ALIGN(PAGE_SIZE);
113		_entry_trampoline = .;
114		*(.entry_trampoline)
115		. = ALIGN(PAGE_SIZE);
116		ASSERT(. - _entry_trampoline == PAGE_SIZE, "entry trampoline is too big");
117#endif
118
119		/* End of text section */
120		_etext = .;
121	} :text = 0x9090
122
123	NOTES :text :note
124
125	EXCEPTION_TABLE(16) :text = 0x9090
126
127	/* .text should occupy whole number of pages */
128	. = ALIGN(PAGE_SIZE);
129	X64_ALIGN_RODATA_BEGIN
130	RO_DATA(PAGE_SIZE)
131	X64_ALIGN_RODATA_END
132
133	/* Data */
134	.data : AT(ADDR(.data) - LOAD_OFFSET) {
135		/* Start of data section */
136		_sdata = .;
137
138		/* init_task */
139		INIT_TASK_DATA(THREAD_SIZE)
140
141#ifdef CONFIG_X86_32
142		/* 32 bit has nosave before _edata */
143		NOSAVE_DATA
144#endif
145
146		PAGE_ALIGNED_DATA(PAGE_SIZE)
147
148		CACHELINE_ALIGNED_DATA(L1_CACHE_BYTES)
149
150		DATA_DATA
151		CONSTRUCTORS
152
153		/* rarely changed data like cpu maps */
154		READ_MOSTLY_DATA(INTERNODE_CACHE_BYTES)
155
156		/* End of data section */
157		_edata = .;
158	} :data
159
160	BUG_TABLE
161
162	ORC_UNWIND_TABLE
163
164	. = ALIGN(PAGE_SIZE);
165	__vvar_page = .;
166
167	.vvar : AT(ADDR(.vvar) - LOAD_OFFSET) {
168		/* work around gold bug 13023 */
169		__vvar_beginning_hack = .;
170
171		/* Place all vvars at the offsets in asm/vvar.h. */
172#define EMIT_VVAR(name, offset) 			\
173		. = __vvar_beginning_hack + offset;	\
174		*(.vvar_ ## name)
175#define __VVAR_KERNEL_LDS
176#include <asm/vvar.h>
177#undef __VVAR_KERNEL_LDS
178#undef EMIT_VVAR
179
180		/*
181		 * Pad the rest of the page with zeros.  Otherwise the loader
182		 * can leave garbage here.
183		 */
184		. = __vvar_beginning_hack + PAGE_SIZE;
185	} :data
186
187       . = ALIGN(__vvar_page + PAGE_SIZE, PAGE_SIZE);
188
189	/* Init code and data - will be freed after init */
190	. = ALIGN(PAGE_SIZE);
191	.init.begin : AT(ADDR(.init.begin) - LOAD_OFFSET) {
192		__init_begin = .; /* paired with __init_end */
193	}
194
195#if defined(CONFIG_X86_64) && defined(CONFIG_SMP)
196	/*
197	 * percpu offsets are zero-based on SMP.  PERCPU_VADDR() changes the
198	 * output PHDR, so the next output section - .init.text - should
199	 * start another segment - init.
200	 */
201	PERCPU_VADDR(INTERNODE_CACHE_BYTES, 0, :percpu)
202	ASSERT(SIZEOF(.data..percpu) < CONFIG_PHYSICAL_START,
203	       "per-CPU data too large - increase CONFIG_PHYSICAL_START")
204#endif
205
206	INIT_TEXT_SECTION(PAGE_SIZE)
207#ifdef CONFIG_X86_64
208	:init
209#endif
210
211	/*
212	 * Section for code used exclusively before alternatives are run. All
213	 * references to such code must be patched out by alternatives, normally
214	 * by using X86_FEATURE_ALWAYS CPU feature bit.
215	 *
216	 * See static_cpu_has() for an example.
217	 */
218	.altinstr_aux : AT(ADDR(.altinstr_aux) - LOAD_OFFSET) {
219		*(.altinstr_aux)
220	}
221
222	INIT_DATA_SECTION(16)
223
224	.x86_cpu_dev.init : AT(ADDR(.x86_cpu_dev.init) - LOAD_OFFSET) {
225		__x86_cpu_dev_start = .;
226		*(.x86_cpu_dev.init)
227		__x86_cpu_dev_end = .;
228	}
229
230#ifdef CONFIG_X86_INTEL_MID
231	.x86_intel_mid_dev.init : AT(ADDR(.x86_intel_mid_dev.init) - \
232								LOAD_OFFSET) {
233		__x86_intel_mid_dev_start = .;
234		*(.x86_intel_mid_dev.init)
235		__x86_intel_mid_dev_end = .;
236	}
237#endif
238
239	/*
240	 * start address and size of operations which during runtime
241	 * can be patched with virtualization friendly instructions or
242	 * baremetal native ones. Think page table operations.
243	 * Details in paravirt_types.h
244	 */
245	. = ALIGN(8);
246	.parainstructions : AT(ADDR(.parainstructions) - LOAD_OFFSET) {
247		__parainstructions = .;
248		*(.parainstructions)
249		__parainstructions_end = .;
250	}
251
252	/*
253	 * struct alt_inst entries. From the header (alternative.h):
254	 * "Alternative instructions for different CPU types or capabilities"
255	 * Think locking instructions on spinlocks.
256	 */
257	. = ALIGN(8);
258	.altinstructions : AT(ADDR(.altinstructions) - LOAD_OFFSET) {
259		__alt_instructions = .;
260		*(.altinstructions)
261		__alt_instructions_end = .;
262	}
263
264	/*
265	 * And here are the replacement instructions. The linker sticks
266	 * them as binary blobs. The .altinstructions has enough data to
267	 * get the address and the length of them to patch the kernel safely.
268	 */
269	.altinstr_replacement : AT(ADDR(.altinstr_replacement) - LOAD_OFFSET) {
270		*(.altinstr_replacement)
271	}
272
273	/*
274	 * struct iommu_table_entry entries are injected in this section.
275	 * It is an array of IOMMUs which during run time gets sorted depending
276	 * on its dependency order. After rootfs_initcall is complete
277	 * this section can be safely removed.
278	 */
279	.iommu_table : AT(ADDR(.iommu_table) - LOAD_OFFSET) {
280		__iommu_table = .;
281		*(.iommu_table)
282		__iommu_table_end = .;
283	}
284
285	. = ALIGN(8);
286	.apicdrivers : AT(ADDR(.apicdrivers) - LOAD_OFFSET) {
287		__apicdrivers = .;
288		*(.apicdrivers);
289		__apicdrivers_end = .;
290	}
291
292	. = ALIGN(8);
293	/*
294	 * .exit.text is discard at runtime, not link time, to deal with
295	 *  references from .altinstructions and .eh_frame
296	 */
297	.exit.text : AT(ADDR(.exit.text) - LOAD_OFFSET) {
298		EXIT_TEXT
299	}
300
301	.exit.data : AT(ADDR(.exit.data) - LOAD_OFFSET) {
302		EXIT_DATA
303	}
304
305#if !defined(CONFIG_X86_64) || !defined(CONFIG_SMP)
306	PERCPU_SECTION(INTERNODE_CACHE_BYTES)
307#endif
308
309	. = ALIGN(PAGE_SIZE);
310
311	/* freed after init ends here */
312	.init.end : AT(ADDR(.init.end) - LOAD_OFFSET) {
313		__init_end = .;
314	}
315
316	/*
317	 * smp_locks might be freed after init
318	 * start/end must be page aligned
319	 */
320	. = ALIGN(PAGE_SIZE);
321	.smp_locks : AT(ADDR(.smp_locks) - LOAD_OFFSET) {
322		__smp_locks = .;
323		*(.smp_locks)
324		. = ALIGN(PAGE_SIZE);
325		__smp_locks_end = .;
326	}
327
328#ifdef CONFIG_X86_64
329	.data_nosave : AT(ADDR(.data_nosave) - LOAD_OFFSET) {
330		NOSAVE_DATA
331	}
332#endif
333
334	/* BSS */
335	. = ALIGN(PAGE_SIZE);
336	.bss : AT(ADDR(.bss) - LOAD_OFFSET) {
337		__bss_start = .;
338		*(.bss..page_aligned)
339		*(.bss)
340		. = ALIGN(PAGE_SIZE);
341		__bss_stop = .;
342	}
343
344	. = ALIGN(PAGE_SIZE);
345	.brk : AT(ADDR(.brk) - LOAD_OFFSET) {
346		__brk_base = .;
347		. += 64 * 1024;		/* 64k alignment slop space */
348		*(.brk_reservation)	/* areas brk users have reserved */
349		__brk_limit = .;
350	}
351
352	. = ALIGN(PAGE_SIZE);		/* keep VO_INIT_SIZE page aligned */
353	_end = .;
354
355        STABS_DEBUG
356        DWARF_DEBUG
357
358	/* Sections to be discarded */
359	DISCARDS
360	/DISCARD/ : {
361		*(.eh_frame)
362	}
363}
364
365
366#ifdef CONFIG_X86_32
367/*
368 * The ASSERT() sink to . is intentional, for binutils 2.14 compatibility:
369 */
370. = ASSERT((_end - LOAD_OFFSET <= KERNEL_IMAGE_SIZE),
371	   "kernel image bigger than KERNEL_IMAGE_SIZE");
372#else
373/*
374 * Per-cpu symbols which need to be offset from __per_cpu_load
375 * for the boot processor.
376 */
377#define INIT_PER_CPU(x) init_per_cpu__##x = x + __per_cpu_load
378INIT_PER_CPU(gdt_page);
379INIT_PER_CPU(irq_stack_union);
380
381/*
382 * Build-time check on the image size:
383 */
384. = ASSERT((_end - _text <= KERNEL_IMAGE_SIZE),
385	   "kernel image bigger than KERNEL_IMAGE_SIZE");
386
387#ifdef CONFIG_SMP
388. = ASSERT((irq_stack_union == 0),
389           "irq_stack_union is not at start of per-cpu area");
390#endif
391
392#endif /* CONFIG_X86_32 */
393
394#ifdef CONFIG_KEXEC_CORE
395#include <asm/kexec.h>
396
397. = ASSERT(kexec_control_code_size <= KEXEC_CONTROL_CODE_MAX_SIZE,
398           "kexec control code size is too big");
399#endif
400
401