1 /*
2  * Helper macros to support writing architecture specific
3  * linker scripts.
4  *
5  * A minimal linker scripts has following content:
6  * [This is a sample, architectures may have special requiriements]
7  *
8  * OUTPUT_FORMAT(...)
9  * OUTPUT_ARCH(...)
10  * ENTRY(...)
11  * SECTIONS
12  * {
13  *	. = START;
14  *	__init_begin = .;
15  *	HEAD_TEXT_SECTION
16  *	INIT_TEXT_SECTION(PAGE_SIZE)
17  *	INIT_DATA_SECTION(...)
18  *	PERCPU_SECTION(CACHELINE_SIZE)
19  *	__init_end = .;
20  *
21  *	_stext = .;
22  *	TEXT_SECTION = 0
23  *	_etext = .;
24  *
25  *      _sdata = .;
26  *	RO_DATA(PAGE_SIZE)
27  *	RW_DATA(...)
28  *	_edata = .;
29  *
30  *	EXCEPTION_TABLE(...)
31  *
32  *	BSS_SECTION(0, 0, 0)
33  *	_end = .;
34  *
35  *	STABS_DEBUG
36  *	DWARF_DEBUG
37  *	ELF_DETAILS
38  *
39  *	DISCARDS		// must be the last
40  * }
41  *
42  * [__init_begin, __init_end] is the init section that may be freed after init
43  * 	// __init_begin and __init_end should be page aligned, so that we can
44  *	// free the whole .init memory
45  * [_stext, _etext] is the text section
46  * [_sdata, _edata] is the data section
47  *
48  * Some of the included output section have their own set of constants.
49  * Examples are: [__initramfs_start, __initramfs_end] for initramfs and
50  *               [__nosave_begin, __nosave_end] for the nosave data
51  */
52 
53 #ifndef LOAD_OFFSET
54 #define LOAD_OFFSET 0
55 #endif
56 
57 /*
58  * Only some architectures want to have the .notes segment visible in
59  * a separate PT_NOTE ELF Program Header. When this happens, it needs
60  * to be visible in both the kernel text's PT_LOAD and the PT_NOTE
61  * Program Headers. In this case, though, the PT_LOAD needs to be made
62  * the default again so that all the following sections don't also end
63  * up in the PT_NOTE Program Header.
64  */
65 #ifdef EMITS_PT_NOTE
66 #define NOTES_HEADERS		:text :note
67 #define NOTES_HEADERS_RESTORE	__restore_ph : { *(.__restore_ph) } :text
68 #else
69 #define NOTES_HEADERS
70 #define NOTES_HEADERS_RESTORE
71 #endif
72 
73 /*
74  * Some architectures have non-executable read-only exception tables.
75  * They can be added to the RO_DATA segment by specifying their desired
76  * alignment.
77  */
78 #ifdef RO_EXCEPTION_TABLE_ALIGN
79 #define RO_EXCEPTION_TABLE	EXCEPTION_TABLE(RO_EXCEPTION_TABLE_ALIGN)
80 #else
81 #define RO_EXCEPTION_TABLE
82 #endif
83 
84 /* Align . to a 8 byte boundary equals to maximum function alignment. */
85 #define ALIGN_FUNCTION()  . = ALIGN(8)
86 
87 /*
88  * LD_DEAD_CODE_DATA_ELIMINATION option enables -fdata-sections, which
89  * generates .data.identifier sections, which need to be pulled in with
90  * .data. We don't want to pull in .data..other sections, which Linux
91  * has defined. Same for text and bss.
92  *
93  * RODATA_MAIN is not used because existing code already defines .rodata.x
94  * sections to be brought in with rodata.
95  */
96 #ifdef CONFIG_LD_DEAD_CODE_DATA_ELIMINATION
97 #define TEXT_MAIN .text .text.[0-9a-zA-Z_]*
98 #define DATA_MAIN .data .data.[0-9a-zA-Z_]* .data..LPBX*
99 #define SDATA_MAIN .sdata .sdata.[0-9a-zA-Z_]*
100 #define RODATA_MAIN .rodata .rodata.[0-9a-zA-Z_]*
101 #define BSS_MAIN .bss .bss.[0-9a-zA-Z_]*
102 #define SBSS_MAIN .sbss .sbss.[0-9a-zA-Z_]*
103 #else
104 #define TEXT_MAIN .text
105 #define DATA_MAIN .data
106 #define SDATA_MAIN .sdata
107 #define RODATA_MAIN .rodata
108 #define BSS_MAIN .bss
109 #define SBSS_MAIN .sbss
110 #endif
111 
112 /*
113  * GCC 4.5 and later have a 32 bytes section alignment for structures.
114  * Except GCC 4.9, that feels the need to align on 64 bytes.
115  */
116 #if __GNUC__ == 4 && __GNUC_MINOR__ == 9
117 #define STRUCT_ALIGNMENT 64
118 #else
119 #define STRUCT_ALIGNMENT 32
120 #endif
121 #define STRUCT_ALIGN() . = ALIGN(STRUCT_ALIGNMENT)
122 
123 /*
124  * The order of the sched class addresses are important, as they are
125  * used to determine the order of the priority of each sched class in
126  * relation to each other.
127  */
128 #define SCHED_DATA				\
129 	STRUCT_ALIGN();				\
130 	__begin_sched_classes = .;		\
131 	*(__idle_sched_class)			\
132 	*(__fair_sched_class)			\
133 	*(__rt_sched_class)			\
134 	*(__dl_sched_class)			\
135 	*(__stop_sched_class)			\
136 	__end_sched_classes = .;
137 
138 /* The actual configuration determine if the init/exit sections
139  * are handled as text/data or they can be discarded (which
140  * often happens at runtime)
141  */
142 #ifdef CONFIG_HOTPLUG_CPU
143 #define CPU_KEEP(sec)    *(.cpu##sec)
144 #define CPU_DISCARD(sec)
145 #else
146 #define CPU_KEEP(sec)
147 #define CPU_DISCARD(sec) *(.cpu##sec)
148 #endif
149 
150 #if defined(CONFIG_MEMORY_HOTPLUG)
151 #define MEM_KEEP(sec)    *(.mem##sec)
152 #define MEM_DISCARD(sec)
153 #else
154 #define MEM_KEEP(sec)
155 #define MEM_DISCARD(sec) *(.mem##sec)
156 #endif
157 
158 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
159 /*
160  * The ftrace call sites are logged to a section whose name depends on the
161  * compiler option used. A given kernel image will only use one, AKA
162  * FTRACE_CALLSITE_SECTION. We capture all of them here to avoid header
163  * dependencies for FTRACE_CALLSITE_SECTION's definition.
164  *
165  * Need to also make ftrace_stub_graph point to ftrace_stub
166  * so that the same stub location may have different protocols
167  * and not mess up with C verifiers.
168  */
169 #define MCOUNT_REC()	. = ALIGN(8);				\
170 			__start_mcount_loc = .;			\
171 			KEEP(*(__mcount_loc))			\
172 			KEEP(*(__patchable_function_entries))	\
173 			__stop_mcount_loc = .;			\
174 			ftrace_stub_graph = ftrace_stub;
175 #else
176 # ifdef CONFIG_FUNCTION_TRACER
177 #  define MCOUNT_REC()	ftrace_stub_graph = ftrace_stub;
178 # else
179 #  define MCOUNT_REC()
180 # endif
181 #endif
182 
183 #ifdef CONFIG_TRACE_BRANCH_PROFILING
184 #define LIKELY_PROFILE()	__start_annotated_branch_profile = .;	\
185 				KEEP(*(_ftrace_annotated_branch))	\
186 				__stop_annotated_branch_profile = .;
187 #else
188 #define LIKELY_PROFILE()
189 #endif
190 
191 #ifdef CONFIG_PROFILE_ALL_BRANCHES
192 #define BRANCH_PROFILE()	__start_branch_profile = .;		\
193 				KEEP(*(_ftrace_branch))			\
194 				__stop_branch_profile = .;
195 #else
196 #define BRANCH_PROFILE()
197 #endif
198 
199 #ifdef CONFIG_KPROBES
200 #define KPROBE_BLACKLIST()	. = ALIGN(8);				      \
201 				__start_kprobe_blacklist = .;		      \
202 				KEEP(*(_kprobe_blacklist))		      \
203 				__stop_kprobe_blacklist = .;
204 #else
205 #define KPROBE_BLACKLIST()
206 #endif
207 
208 #ifdef CONFIG_FUNCTION_ERROR_INJECTION
209 #define ERROR_INJECT_WHITELIST()	STRUCT_ALIGN();			      \
210 			__start_error_injection_whitelist = .;		      \
211 			KEEP(*(_error_injection_whitelist))		      \
212 			__stop_error_injection_whitelist = .;
213 #else
214 #define ERROR_INJECT_WHITELIST()
215 #endif
216 
217 #ifdef CONFIG_EVENT_TRACING
218 #define FTRACE_EVENTS()	. = ALIGN(8);					\
219 			__start_ftrace_events = .;			\
220 			KEEP(*(_ftrace_events))				\
221 			__stop_ftrace_events = .;			\
222 			__start_ftrace_eval_maps = .;			\
223 			KEEP(*(_ftrace_eval_map))			\
224 			__stop_ftrace_eval_maps = .;
225 #else
226 #define FTRACE_EVENTS()
227 #endif
228 
229 #ifdef CONFIG_TRACING
230 #define TRACE_PRINTKS()	 __start___trace_bprintk_fmt = .;      \
231 			 KEEP(*(__trace_printk_fmt)) /* Trace_printk fmt' pointer */ \
232 			 __stop___trace_bprintk_fmt = .;
233 #define TRACEPOINT_STR() __start___tracepoint_str = .;	\
234 			 KEEP(*(__tracepoint_str)) /* Trace_printk fmt' pointer */ \
235 			 __stop___tracepoint_str = .;
236 #else
237 #define TRACE_PRINTKS()
238 #define TRACEPOINT_STR()
239 #endif
240 
241 #ifdef CONFIG_FTRACE_SYSCALLS
242 #define TRACE_SYSCALLS() . = ALIGN(8);					\
243 			 __start_syscalls_metadata = .;			\
244 			 KEEP(*(__syscalls_metadata))			\
245 			 __stop_syscalls_metadata = .;
246 #else
247 #define TRACE_SYSCALLS()
248 #endif
249 
250 #ifdef CONFIG_BPF_EVENTS
251 #define BPF_RAW_TP() STRUCT_ALIGN();					\
252 			 __start__bpf_raw_tp = .;			\
253 			 KEEP(*(__bpf_raw_tp_map))			\
254 			 __stop__bpf_raw_tp = .;
255 #else
256 #define BPF_RAW_TP()
257 #endif
258 
259 #ifdef CONFIG_SERIAL_EARLYCON
260 #define EARLYCON_TABLE() . = ALIGN(8);				\
261 			 __earlycon_table = .;			\
262 			 KEEP(*(__earlycon_table))		\
263 			 __earlycon_table_end = .;
264 #else
265 #define EARLYCON_TABLE()
266 #endif
267 
268 #ifdef CONFIG_SECURITY
269 #define LSM_TABLE()	. = ALIGN(8);					\
270 			__start_lsm_info = .;				\
271 			KEEP(*(.lsm_info.init))				\
272 			__end_lsm_info = .;
273 #define EARLY_LSM_TABLE()	. = ALIGN(8);				\
274 			__start_early_lsm_info = .;			\
275 			KEEP(*(.early_lsm_info.init))			\
276 			__end_early_lsm_info = .;
277 #else
278 #define LSM_TABLE()
279 #define EARLY_LSM_TABLE()
280 #endif
281 
282 #define ___OF_TABLE(cfg, name)	_OF_TABLE_##cfg(name)
283 #define __OF_TABLE(cfg, name)	___OF_TABLE(cfg, name)
284 #define OF_TABLE(cfg, name)	__OF_TABLE(IS_ENABLED(cfg), name)
285 #define _OF_TABLE_0(name)
286 #define _OF_TABLE_1(name)						\
287 	. = ALIGN(8);							\
288 	__##name##_of_table = .;					\
289 	KEEP(*(__##name##_of_table))					\
290 	KEEP(*(__##name##_of_table_end))
291 
292 #define TIMER_OF_TABLES()	OF_TABLE(CONFIG_TIMER_OF, timer)
293 #define IRQCHIP_OF_MATCH_TABLE() OF_TABLE(CONFIG_IRQCHIP, irqchip)
294 #define CLK_OF_TABLES()		OF_TABLE(CONFIG_COMMON_CLK, clk)
295 #define RESERVEDMEM_OF_TABLES()	OF_TABLE(CONFIG_OF_RESERVED_MEM, reservedmem)
296 #define CPU_METHOD_OF_TABLES()	OF_TABLE(CONFIG_SMP, cpu_method)
297 #define CPUIDLE_METHOD_OF_TABLES() OF_TABLE(CONFIG_CPU_IDLE, cpuidle_method)
298 
299 #ifdef CONFIG_ACPI
300 #define ACPI_PROBE_TABLE(name)						\
301 	. = ALIGN(8);							\
302 	__##name##_acpi_probe_table = .;				\
303 	KEEP(*(__##name##_acpi_probe_table))				\
304 	__##name##_acpi_probe_table_end = .;
305 #else
306 #define ACPI_PROBE_TABLE(name)
307 #endif
308 
309 #ifdef CONFIG_THERMAL
310 #define THERMAL_TABLE(name)						\
311 	. = ALIGN(8);							\
312 	__##name##_thermal_table = .;					\
313 	KEEP(*(__##name##_thermal_table))				\
314 	__##name##_thermal_table_end = .;
315 #else
316 #define THERMAL_TABLE(name)
317 #endif
318 
319 #define KERNEL_DTB()							\
320 	STRUCT_ALIGN();							\
321 	__dtb_start = .;						\
322 	KEEP(*(.dtb.init.rodata))					\
323 	__dtb_end = .;
324 
325 /*
326  * .data section
327  */
328 #define DATA_DATA							\
329 	*(.xiptext)							\
330 	*(DATA_MAIN)							\
331 	*(.ref.data)							\
332 	*(.data..shared_aligned) /* percpu related */			\
333 	MEM_KEEP(init.data*)						\
334 	MEM_KEEP(exit.data*)						\
335 	*(.data.unlikely)						\
336 	__start_once = .;						\
337 	*(.data.once)							\
338 	__end_once = .;							\
339 	STRUCT_ALIGN();							\
340 	*(__tracepoints)						\
341 	/* implement dynamic printk debug */				\
342 	. = ALIGN(8);							\
343 	__start___dyndbg = .;						\
344 	KEEP(*(__dyndbg))						\
345 	__stop___dyndbg = .;						\
346 	LIKELY_PROFILE()		       				\
347 	BRANCH_PROFILE()						\
348 	TRACE_PRINTKS()							\
349 	BPF_RAW_TP()							\
350 	TRACEPOINT_STR()
351 
352 /*
353  * Data section helpers
354  */
355 #define NOSAVE_DATA							\
356 	. = ALIGN(PAGE_SIZE);						\
357 	__nosave_begin = .;						\
358 	*(.data..nosave)						\
359 	. = ALIGN(PAGE_SIZE);						\
360 	__nosave_end = .;
361 
362 #define PAGE_ALIGNED_DATA(page_align)					\
363 	. = ALIGN(page_align);						\
364 	*(.data..page_aligned)						\
365 	. = ALIGN(page_align);
366 
367 #define READ_MOSTLY_DATA(align)						\
368 	. = ALIGN(align);						\
369 	*(.data..read_mostly)						\
370 	. = ALIGN(align);
371 
372 #define CACHELINE_ALIGNED_DATA(align)					\
373 	. = ALIGN(align);						\
374 	*(.data..cacheline_aligned)
375 
376 #define INIT_TASK_DATA(align)						\
377 	. = ALIGN(align);						\
378 	__start_init_task = .;						\
379 	init_thread_union = .;						\
380 	init_stack = .;							\
381 	KEEP(*(.data..init_task))					\
382 	KEEP(*(.data..init_thread_info))				\
383 	. = __start_init_task + THREAD_SIZE;				\
384 	__end_init_task = .;
385 
386 #define JUMP_TABLE_DATA							\
387 	. = ALIGN(8);							\
388 	__start___jump_table = .;					\
389 	KEEP(*(__jump_table))						\
390 	__stop___jump_table = .;
391 
392 #define STATIC_CALL_DATA						\
393 	. = ALIGN(8);							\
394 	__start_static_call_sites = .;					\
395 	KEEP(*(.static_call_sites))					\
396 	__stop_static_call_sites = .;
397 
398 /*
399  * Allow architectures to handle ro_after_init data on their
400  * own by defining an empty RO_AFTER_INIT_DATA.
401  */
402 #ifndef RO_AFTER_INIT_DATA
403 #define RO_AFTER_INIT_DATA						\
404 	. = ALIGN(8);							\
405 	__start_ro_after_init = .;					\
406 	*(.data..ro_after_init)						\
407 	JUMP_TABLE_DATA							\
408 	STATIC_CALL_DATA						\
409 	__end_ro_after_init = .;
410 #endif
411 
412 /*
413  * Read only Data
414  */
415 #define RO_DATA(align)							\
416 	. = ALIGN((align));						\
417 	.rodata           : AT(ADDR(.rodata) - LOAD_OFFSET) {		\
418 		__start_rodata = .;					\
419 		*(.rodata) *(.rodata.*)					\
420 		SCHED_DATA						\
421 		RO_AFTER_INIT_DATA	/* Read only after init */	\
422 		. = ALIGN(8);						\
423 		__start___tracepoints_ptrs = .;				\
424 		KEEP(*(__tracepoints_ptrs)) /* Tracepoints: pointer array */ \
425 		__stop___tracepoints_ptrs = .;				\
426 		*(__tracepoints_strings)/* Tracepoints: strings */	\
427 	}								\
428 									\
429 	.rodata1          : AT(ADDR(.rodata1) - LOAD_OFFSET) {		\
430 		*(.rodata1)						\
431 	}								\
432 									\
433 	/* PCI quirks */						\
434 	.pci_fixup        : AT(ADDR(.pci_fixup) - LOAD_OFFSET) {	\
435 		__start_pci_fixups_early = .;				\
436 		KEEP(*(.pci_fixup_early))				\
437 		__end_pci_fixups_early = .;				\
438 		__start_pci_fixups_header = .;				\
439 		KEEP(*(.pci_fixup_header))				\
440 		__end_pci_fixups_header = .;				\
441 		__start_pci_fixups_final = .;				\
442 		KEEP(*(.pci_fixup_final))				\
443 		__end_pci_fixups_final = .;				\
444 		__start_pci_fixups_enable = .;				\
445 		KEEP(*(.pci_fixup_enable))				\
446 		__end_pci_fixups_enable = .;				\
447 		__start_pci_fixups_resume = .;				\
448 		KEEP(*(.pci_fixup_resume))				\
449 		__end_pci_fixups_resume = .;				\
450 		__start_pci_fixups_resume_early = .;			\
451 		KEEP(*(.pci_fixup_resume_early))			\
452 		__end_pci_fixups_resume_early = .;			\
453 		__start_pci_fixups_suspend = .;				\
454 		KEEP(*(.pci_fixup_suspend))				\
455 		__end_pci_fixups_suspend = .;				\
456 		__start_pci_fixups_suspend_late = .;			\
457 		KEEP(*(.pci_fixup_suspend_late))			\
458 		__end_pci_fixups_suspend_late = .;			\
459 	}								\
460 									\
461 	/* Built-in firmware blobs */					\
462 	.builtin_fw        : AT(ADDR(.builtin_fw) - LOAD_OFFSET) {	\
463 		__start_builtin_fw = .;					\
464 		KEEP(*(.builtin_fw))					\
465 		__end_builtin_fw = .;					\
466 	}								\
467 									\
468 	TRACEDATA							\
469 									\
470 	/* Kernel symbol table: Normal symbols */			\
471 	__ksymtab         : AT(ADDR(__ksymtab) - LOAD_OFFSET) {		\
472 		__start___ksymtab = .;					\
473 		KEEP(*(SORT(___ksymtab+*)))				\
474 		__stop___ksymtab = .;					\
475 	}								\
476 									\
477 	/* Kernel symbol table: GPL-only symbols */			\
478 	__ksymtab_gpl     : AT(ADDR(__ksymtab_gpl) - LOAD_OFFSET) {	\
479 		__start___ksymtab_gpl = .;				\
480 		KEEP(*(SORT(___ksymtab_gpl+*)))				\
481 		__stop___ksymtab_gpl = .;				\
482 	}								\
483 									\
484 	/* Kernel symbol table: Normal unused symbols */		\
485 	__ksymtab_unused  : AT(ADDR(__ksymtab_unused) - LOAD_OFFSET) {	\
486 		__start___ksymtab_unused = .;				\
487 		KEEP(*(SORT(___ksymtab_unused+*)))			\
488 		__stop___ksymtab_unused = .;				\
489 	}								\
490 									\
491 	/* Kernel symbol table: GPL-only unused symbols */		\
492 	__ksymtab_unused_gpl : AT(ADDR(__ksymtab_unused_gpl) - LOAD_OFFSET) { \
493 		__start___ksymtab_unused_gpl = .;			\
494 		KEEP(*(SORT(___ksymtab_unused_gpl+*)))			\
495 		__stop___ksymtab_unused_gpl = .;			\
496 	}								\
497 									\
498 	/* Kernel symbol table: GPL-future-only symbols */		\
499 	__ksymtab_gpl_future : AT(ADDR(__ksymtab_gpl_future) - LOAD_OFFSET) { \
500 		__start___ksymtab_gpl_future = .;			\
501 		KEEP(*(SORT(___ksymtab_gpl_future+*)))			\
502 		__stop___ksymtab_gpl_future = .;			\
503 	}								\
504 									\
505 	/* Kernel symbol table: Normal symbols */			\
506 	__kcrctab         : AT(ADDR(__kcrctab) - LOAD_OFFSET) {		\
507 		__start___kcrctab = .;					\
508 		KEEP(*(SORT(___kcrctab+*)))				\
509 		__stop___kcrctab = .;					\
510 	}								\
511 									\
512 	/* Kernel symbol table: GPL-only symbols */			\
513 	__kcrctab_gpl     : AT(ADDR(__kcrctab_gpl) - LOAD_OFFSET) {	\
514 		__start___kcrctab_gpl = .;				\
515 		KEEP(*(SORT(___kcrctab_gpl+*)))				\
516 		__stop___kcrctab_gpl = .;				\
517 	}								\
518 									\
519 	/* Kernel symbol table: Normal unused symbols */		\
520 	__kcrctab_unused  : AT(ADDR(__kcrctab_unused) - LOAD_OFFSET) {	\
521 		__start___kcrctab_unused = .;				\
522 		KEEP(*(SORT(___kcrctab_unused+*)))			\
523 		__stop___kcrctab_unused = .;				\
524 	}								\
525 									\
526 	/* Kernel symbol table: GPL-only unused symbols */		\
527 	__kcrctab_unused_gpl : AT(ADDR(__kcrctab_unused_gpl) - LOAD_OFFSET) { \
528 		__start___kcrctab_unused_gpl = .;			\
529 		KEEP(*(SORT(___kcrctab_unused_gpl+*)))			\
530 		__stop___kcrctab_unused_gpl = .;			\
531 	}								\
532 									\
533 	/* Kernel symbol table: GPL-future-only symbols */		\
534 	__kcrctab_gpl_future : AT(ADDR(__kcrctab_gpl_future) - LOAD_OFFSET) { \
535 		__start___kcrctab_gpl_future = .;			\
536 		KEEP(*(SORT(___kcrctab_gpl_future+*)))			\
537 		__stop___kcrctab_gpl_future = .;			\
538 	}								\
539 									\
540 	/* Kernel symbol table: strings */				\
541         __ksymtab_strings : AT(ADDR(__ksymtab_strings) - LOAD_OFFSET) {	\
542 		*(__ksymtab_strings)					\
543 	}								\
544 									\
545 	/* __*init sections */						\
546 	__init_rodata : AT(ADDR(__init_rodata) - LOAD_OFFSET) {		\
547 		*(.ref.rodata)						\
548 		MEM_KEEP(init.rodata)					\
549 		MEM_KEEP(exit.rodata)					\
550 	}								\
551 									\
552 	/* Built-in module parameters. */				\
553 	__param : AT(ADDR(__param) - LOAD_OFFSET) {			\
554 		__start___param = .;					\
555 		KEEP(*(__param))					\
556 		__stop___param = .;					\
557 	}								\
558 									\
559 	/* Built-in module versions. */					\
560 	__modver : AT(ADDR(__modver) - LOAD_OFFSET) {			\
561 		__start___modver = .;					\
562 		KEEP(*(__modver))					\
563 		__stop___modver = .;					\
564 	}								\
565 									\
566 	RO_EXCEPTION_TABLE						\
567 	NOTES								\
568 	BTF								\
569 									\
570 	. = ALIGN((align));						\
571 	__end_rodata = .;
572 
573 /*
574  * Non-instrumentable text section
575  */
576 #define NOINSTR_TEXT							\
577 		ALIGN_FUNCTION();					\
578 		__noinstr_text_start = .;				\
579 		*(.noinstr.text)					\
580 		__noinstr_text_end = .;
581 
582 /*
583  * .text section. Map to function alignment to avoid address changes
584  * during second ld run in second ld pass when generating System.map
585  *
586  * TEXT_MAIN here will match .text.fixup and .text.unlikely if dead
587  * code elimination is enabled, so these sections should be converted
588  * to use ".." first.
589  */
590 #define TEXT_TEXT							\
591 		ALIGN_FUNCTION();					\
592 		*(.text.hot .text.hot.*)				\
593 		*(TEXT_MAIN .text.fixup)				\
594 		*(.text.unlikely .text.unlikely.*)			\
595 		*(.text.unknown .text.unknown.*)			\
596 		NOINSTR_TEXT						\
597 		*(.text..refcount)					\
598 		*(.ref.text)						\
599 	MEM_KEEP(init.text*)						\
600 	MEM_KEEP(exit.text*)						\
601 
602 
603 /* sched.text is aling to function alignment to secure we have same
604  * address even at second ld pass when generating System.map */
605 #define SCHED_TEXT							\
606 		ALIGN_FUNCTION();					\
607 		__sched_text_start = .;					\
608 		*(.sched.text)						\
609 		__sched_text_end = .;
610 
611 /* spinlock.text is aling to function alignment to secure we have same
612  * address even at second ld pass when generating System.map */
613 #define LOCK_TEXT							\
614 		ALIGN_FUNCTION();					\
615 		__lock_text_start = .;					\
616 		*(.spinlock.text)					\
617 		__lock_text_end = .;
618 
619 #define CPUIDLE_TEXT							\
620 		ALIGN_FUNCTION();					\
621 		__cpuidle_text_start = .;				\
622 		*(.cpuidle.text)					\
623 		__cpuidle_text_end = .;
624 
625 #define KPROBES_TEXT							\
626 		ALIGN_FUNCTION();					\
627 		__kprobes_text_start = .;				\
628 		*(.kprobes.text)					\
629 		__kprobes_text_end = .;
630 
631 #define ENTRY_TEXT							\
632 		ALIGN_FUNCTION();					\
633 		__entry_text_start = .;					\
634 		*(.entry.text)						\
635 		__entry_text_end = .;
636 
637 #define IRQENTRY_TEXT							\
638 		ALIGN_FUNCTION();					\
639 		__irqentry_text_start = .;				\
640 		*(.irqentry.text)					\
641 		__irqentry_text_end = .;
642 
643 #define SOFTIRQENTRY_TEXT						\
644 		ALIGN_FUNCTION();					\
645 		__softirqentry_text_start = .;				\
646 		*(.softirqentry.text)					\
647 		__softirqentry_text_end = .;
648 
649 #define STATIC_CALL_TEXT						\
650 		ALIGN_FUNCTION();					\
651 		__static_call_text_start = .;				\
652 		*(.static_call.text)					\
653 		__static_call_text_end = .;
654 
655 /* Section used for early init (in .S files) */
656 #define HEAD_TEXT  KEEP(*(.head.text))
657 
658 #define HEAD_TEXT_SECTION							\
659 	.head.text : AT(ADDR(.head.text) - LOAD_OFFSET) {		\
660 		HEAD_TEXT						\
661 	}
662 
663 /*
664  * Exception table
665  */
666 #define EXCEPTION_TABLE(align)						\
667 	. = ALIGN(align);						\
668 	__ex_table : AT(ADDR(__ex_table) - LOAD_OFFSET) {		\
669 		__start___ex_table = .;					\
670 		KEEP(*(__ex_table))					\
671 		__stop___ex_table = .;					\
672 	}
673 
674 /*
675  * .BTF
676  */
677 #ifdef CONFIG_DEBUG_INFO_BTF
678 #define BTF								\
679 	.BTF : AT(ADDR(.BTF) - LOAD_OFFSET) {				\
680 		__start_BTF = .;					\
681 		KEEP(*(.BTF))						\
682 		__stop_BTF = .;						\
683 	}								\
684 	. = ALIGN(4);							\
685 	.BTF_ids : AT(ADDR(.BTF_ids) - LOAD_OFFSET) {			\
686 		*(.BTF_ids)						\
687 	}
688 #else
689 #define BTF
690 #endif
691 
692 /*
693  * Init task
694  */
695 #define INIT_TASK_DATA_SECTION(align)					\
696 	. = ALIGN(align);						\
697 	.data..init_task :  AT(ADDR(.data..init_task) - LOAD_OFFSET) {	\
698 		INIT_TASK_DATA(align)					\
699 	}
700 
701 #ifdef CONFIG_CONSTRUCTORS
702 #define KERNEL_CTORS()	. = ALIGN(8);			   \
703 			__ctors_start = .;		   \
704 			KEEP(*(.ctors))			   \
705 			KEEP(*(SORT(.init_array.*)))	   \
706 			KEEP(*(.init_array))		   \
707 			__ctors_end = .;
708 #else
709 #define KERNEL_CTORS()
710 #endif
711 
712 /* init and exit section handling */
713 #define INIT_DATA							\
714 	KEEP(*(SORT(___kentry+*)))					\
715 	*(.init.data init.data.*)					\
716 	MEM_DISCARD(init.data*)						\
717 	KERNEL_CTORS()							\
718 	MCOUNT_REC()							\
719 	*(.init.rodata .init.rodata.*)					\
720 	FTRACE_EVENTS()							\
721 	TRACE_SYSCALLS()						\
722 	KPROBE_BLACKLIST()						\
723 	ERROR_INJECT_WHITELIST()					\
724 	MEM_DISCARD(init.rodata)					\
725 	CLK_OF_TABLES()							\
726 	RESERVEDMEM_OF_TABLES()						\
727 	TIMER_OF_TABLES()						\
728 	CPU_METHOD_OF_TABLES()						\
729 	CPUIDLE_METHOD_OF_TABLES()					\
730 	KERNEL_DTB()							\
731 	IRQCHIP_OF_MATCH_TABLE()					\
732 	ACPI_PROBE_TABLE(irqchip)					\
733 	ACPI_PROBE_TABLE(timer)						\
734 	THERMAL_TABLE(governor)						\
735 	EARLYCON_TABLE()						\
736 	LSM_TABLE()							\
737 	EARLY_LSM_TABLE()						\
738 	KUNIT_TABLE()
739 
740 #define INIT_TEXT							\
741 	*(.init.text .init.text.*)					\
742 	*(.text.startup)						\
743 	MEM_DISCARD(init.text*)
744 
745 #define EXIT_DATA							\
746 	*(.exit.data .exit.data.*)					\
747 	*(.fini_array .fini_array.*)					\
748 	*(.dtors .dtors.*)						\
749 	MEM_DISCARD(exit.data*)						\
750 	MEM_DISCARD(exit.rodata*)
751 
752 #define EXIT_TEXT							\
753 	*(.exit.text)							\
754 	*(.text.exit)							\
755 	MEM_DISCARD(exit.text)
756 
757 #define EXIT_CALL							\
758 	*(.exitcall.exit)
759 
760 /*
761  * bss (Block Started by Symbol) - uninitialized data
762  * zeroed during startup
763  */
764 #define SBSS(sbss_align)						\
765 	. = ALIGN(sbss_align);						\
766 	.sbss : AT(ADDR(.sbss) - LOAD_OFFSET) {				\
767 		*(.dynsbss)						\
768 		*(SBSS_MAIN)						\
769 		*(.scommon)						\
770 	}
771 
772 /*
773  * Allow archectures to redefine BSS_FIRST_SECTIONS to add extra
774  * sections to the front of bss.
775  */
776 #ifndef BSS_FIRST_SECTIONS
777 #define BSS_FIRST_SECTIONS
778 #endif
779 
780 #define BSS(bss_align)							\
781 	. = ALIGN(bss_align);						\
782 	.bss : AT(ADDR(.bss) - LOAD_OFFSET) {				\
783 		BSS_FIRST_SECTIONS					\
784 		. = ALIGN(PAGE_SIZE);					\
785 		*(.bss..page_aligned)					\
786 		. = ALIGN(PAGE_SIZE);					\
787 		*(.dynbss)						\
788 		*(BSS_MAIN)						\
789 		*(COMMON)						\
790 	}
791 
792 /*
793  * DWARF debug sections.
794  * Symbols in the DWARF debugging sections are relative to
795  * the beginning of the section so we begin them at 0.
796  */
797 #define DWARF_DEBUG							\
798 		/* DWARF 1 */						\
799 		.debug          0 : { *(.debug) }			\
800 		.line           0 : { *(.line) }			\
801 		/* GNU DWARF 1 extensions */				\
802 		.debug_srcinfo  0 : { *(.debug_srcinfo) }		\
803 		.debug_sfnames  0 : { *(.debug_sfnames) }		\
804 		/* DWARF 1.1 and DWARF 2 */				\
805 		.debug_aranges  0 : { *(.debug_aranges) }		\
806 		.debug_pubnames 0 : { *(.debug_pubnames) }		\
807 		/* DWARF 2 */						\
808 		.debug_info     0 : { *(.debug_info			\
809 				.gnu.linkonce.wi.*) }			\
810 		.debug_abbrev   0 : { *(.debug_abbrev) }		\
811 		.debug_line     0 : { *(.debug_line) }			\
812 		.debug_frame    0 : { *(.debug_frame) }			\
813 		.debug_str      0 : { *(.debug_str) }			\
814 		.debug_loc      0 : { *(.debug_loc) }			\
815 		.debug_macinfo  0 : { *(.debug_macinfo) }		\
816 		.debug_pubtypes 0 : { *(.debug_pubtypes) }		\
817 		/* DWARF 3 */						\
818 		.debug_ranges	0 : { *(.debug_ranges) }		\
819 		/* SGI/MIPS DWARF 2 extensions */			\
820 		.debug_weaknames 0 : { *(.debug_weaknames) }		\
821 		.debug_funcnames 0 : { *(.debug_funcnames) }		\
822 		.debug_typenames 0 : { *(.debug_typenames) }		\
823 		.debug_varnames  0 : { *(.debug_varnames) }		\
824 		/* GNU DWARF 2 extensions */				\
825 		.debug_gnu_pubnames 0 : { *(.debug_gnu_pubnames) }	\
826 		.debug_gnu_pubtypes 0 : { *(.debug_gnu_pubtypes) }	\
827 		/* DWARF 4 */						\
828 		.debug_types	0 : { *(.debug_types) }			\
829 		/* DWARF 5 */						\
830 		.debug_macro	0 : { *(.debug_macro) }			\
831 		.debug_addr	0 : { *(.debug_addr) }
832 
833 /* Stabs debugging sections. */
834 #define STABS_DEBUG							\
835 		.stab 0 : { *(.stab) }					\
836 		.stabstr 0 : { *(.stabstr) }				\
837 		.stab.excl 0 : { *(.stab.excl) }			\
838 		.stab.exclstr 0 : { *(.stab.exclstr) }			\
839 		.stab.index 0 : { *(.stab.index) }			\
840 		.stab.indexstr 0 : { *(.stab.indexstr) }
841 
842 /* Required sections not related to debugging. */
843 #define ELF_DETAILS							\
844 		.comment 0 : { *(.comment) }				\
845 		.symtab 0 : { *(.symtab) }				\
846 		.strtab 0 : { *(.strtab) }				\
847 		.shstrtab 0 : { *(.shstrtab) }
848 
849 #ifdef CONFIG_GENERIC_BUG
850 #define BUG_TABLE							\
851 	. = ALIGN(8);							\
852 	__bug_table : AT(ADDR(__bug_table) - LOAD_OFFSET) {		\
853 		__start___bug_table = .;				\
854 		KEEP(*(__bug_table))					\
855 		__stop___bug_table = .;					\
856 	}
857 #else
858 #define BUG_TABLE
859 #endif
860 
861 #ifdef CONFIG_UNWINDER_ORC
862 #define ORC_UNWIND_TABLE						\
863 	. = ALIGN(4);							\
864 	.orc_unwind_ip : AT(ADDR(.orc_unwind_ip) - LOAD_OFFSET) {	\
865 		__start_orc_unwind_ip = .;				\
866 		KEEP(*(.orc_unwind_ip))					\
867 		__stop_orc_unwind_ip = .;				\
868 	}								\
869 	. = ALIGN(2);							\
870 	.orc_unwind : AT(ADDR(.orc_unwind) - LOAD_OFFSET) {		\
871 		__start_orc_unwind = .;					\
872 		KEEP(*(.orc_unwind))					\
873 		__stop_orc_unwind = .;					\
874 	}								\
875 	. = ALIGN(4);							\
876 	.orc_lookup : AT(ADDR(.orc_lookup) - LOAD_OFFSET) {		\
877 		orc_lookup = .;						\
878 		. += (((SIZEOF(.text) + LOOKUP_BLOCK_SIZE - 1) /	\
879 			LOOKUP_BLOCK_SIZE) + 1) * 4;			\
880 		orc_lookup_end = .;					\
881 	}
882 #else
883 #define ORC_UNWIND_TABLE
884 #endif
885 
886 #ifdef CONFIG_PM_TRACE
887 #define TRACEDATA							\
888 	. = ALIGN(4);							\
889 	.tracedata : AT(ADDR(.tracedata) - LOAD_OFFSET) {		\
890 		__tracedata_start = .;					\
891 		KEEP(*(.tracedata))					\
892 		__tracedata_end = .;					\
893 	}
894 #else
895 #define TRACEDATA
896 #endif
897 
898 #define NOTES								\
899 	.notes : AT(ADDR(.notes) - LOAD_OFFSET) {			\
900 		__start_notes = .;					\
901 		KEEP(*(.note.*))					\
902 		__stop_notes = .;					\
903 	} NOTES_HEADERS							\
904 	NOTES_HEADERS_RESTORE
905 
906 #define INIT_SETUP(initsetup_align)					\
907 		. = ALIGN(initsetup_align);				\
908 		__setup_start = .;					\
909 		KEEP(*(.init.setup))					\
910 		__setup_end = .;
911 
912 #define INIT_CALLS_LEVEL(level)						\
913 		__initcall##level##_start = .;				\
914 		KEEP(*(.initcall##level##.init))			\
915 		KEEP(*(.initcall##level##s.init))			\
916 
917 #define INIT_CALLS							\
918 		__initcall_start = .;					\
919 		KEEP(*(.initcallearly.init))				\
920 		INIT_CALLS_LEVEL(0)					\
921 		INIT_CALLS_LEVEL(1)					\
922 		INIT_CALLS_LEVEL(2)					\
923 		INIT_CALLS_LEVEL(3)					\
924 		INIT_CALLS_LEVEL(4)					\
925 		INIT_CALLS_LEVEL(5)					\
926 		INIT_CALLS_LEVEL(rootfs)				\
927 		INIT_CALLS_LEVEL(6)					\
928 		INIT_CALLS_LEVEL(7)					\
929 		__initcall_end = .;
930 
931 #define CON_INITCALL							\
932 		__con_initcall_start = .;				\
933 		KEEP(*(.con_initcall.init))				\
934 		__con_initcall_end = .;
935 
936 /* Alignment must be consistent with (kunit_suite *) in include/kunit/test.h */
937 #define KUNIT_TABLE()							\
938 		. = ALIGN(8);						\
939 		__kunit_suites_start = .;				\
940 		KEEP(*(.kunit_test_suites))				\
941 		__kunit_suites_end = .;
942 
943 #ifdef CONFIG_BLK_DEV_INITRD
944 #define INIT_RAM_FS							\
945 	. = ALIGN(4);							\
946 	__initramfs_start = .;						\
947 	KEEP(*(.init.ramfs))						\
948 	. = ALIGN(8);							\
949 	KEEP(*(.init.ramfs.info))
950 #else
951 #define INIT_RAM_FS
952 #endif
953 
954 /*
955  * Memory encryption operates on a page basis. Since we need to clear
956  * the memory encryption mask for this section, it needs to be aligned
957  * on a page boundary and be a page-size multiple in length.
958  *
959  * Note: We use a separate section so that only this section gets
960  * decrypted to avoid exposing more than we wish.
961  */
962 #ifdef CONFIG_AMD_MEM_ENCRYPT
963 #define PERCPU_DECRYPTED_SECTION					\
964 	. = ALIGN(PAGE_SIZE);						\
965 	*(.data..percpu..decrypted)					\
966 	. = ALIGN(PAGE_SIZE);
967 #else
968 #define PERCPU_DECRYPTED_SECTION
969 #endif
970 
971 
972 /*
973  * Default discarded sections.
974  *
975  * Some archs want to discard exit text/data at runtime rather than
976  * link time due to cross-section references such as alt instructions,
977  * bug table, eh_frame, etc.  DISCARDS must be the last of output
978  * section definitions so that such archs put those in earlier section
979  * definitions.
980  */
981 #ifdef RUNTIME_DISCARD_EXIT
982 #define EXIT_DISCARDS
983 #else
984 #define EXIT_DISCARDS							\
985 	EXIT_TEXT							\
986 	EXIT_DATA
987 #endif
988 
989 /*
990  * Clang's -fsanitize=kernel-address and -fsanitize=thread produce
991  * unwanted sections (.eh_frame and .init_array.*), but
992  * CONFIG_CONSTRUCTORS wants to keep any .init_array.* sections.
993  * https://bugs.llvm.org/show_bug.cgi?id=46478
994  */
995 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KCSAN)
996 # ifdef CONFIG_CONSTRUCTORS
997 #  define SANITIZER_DISCARDS						\
998 	*(.eh_frame)
999 # else
1000 #  define SANITIZER_DISCARDS						\
1001 	*(.init_array) *(.init_array.*)					\
1002 	*(.eh_frame)
1003 # endif
1004 #else
1005 # define SANITIZER_DISCARDS
1006 #endif
1007 
1008 #define COMMON_DISCARDS							\
1009 	SANITIZER_DISCARDS						\
1010 	*(.discard)							\
1011 	*(.discard.*)							\
1012 	*(.modinfo)							\
1013 	/* ld.bfd warns about .gnu.version* even when not emitted */	\
1014 	*(.gnu.version*)						\
1015 
1016 #define DISCARDS							\
1017 	/DISCARD/ : {							\
1018 	EXIT_DISCARDS							\
1019 	EXIT_CALL							\
1020 	COMMON_DISCARDS							\
1021 	}
1022 
1023 /**
1024  * PERCPU_INPUT - the percpu input sections
1025  * @cacheline: cacheline size
1026  *
1027  * The core percpu section names and core symbols which do not rely
1028  * directly upon load addresses.
1029  *
1030  * @cacheline is used to align subsections to avoid false cacheline
1031  * sharing between subsections for different purposes.
1032  */
1033 #define PERCPU_INPUT(cacheline)						\
1034 	__per_cpu_start = .;						\
1035 	*(.data..percpu..first)						\
1036 	. = ALIGN(PAGE_SIZE);						\
1037 	*(.data..percpu..page_aligned)					\
1038 	. = ALIGN(cacheline);						\
1039 	*(.data..percpu..read_mostly)					\
1040 	. = ALIGN(cacheline);						\
1041 	*(.data..percpu)						\
1042 	*(.data..percpu..shared_aligned)				\
1043 	PERCPU_DECRYPTED_SECTION					\
1044 	__per_cpu_end = .;
1045 
1046 /**
1047  * PERCPU_VADDR - define output section for percpu area
1048  * @cacheline: cacheline size
1049  * @vaddr: explicit base address (optional)
1050  * @phdr: destination PHDR (optional)
1051  *
1052  * Macro which expands to output section for percpu area.
1053  *
1054  * @cacheline is used to align subsections to avoid false cacheline
1055  * sharing between subsections for different purposes.
1056  *
1057  * If @vaddr is not blank, it specifies explicit base address and all
1058  * percpu symbols will be offset from the given address.  If blank,
1059  * @vaddr always equals @laddr + LOAD_OFFSET.
1060  *
1061  * @phdr defines the output PHDR to use if not blank.  Be warned that
1062  * output PHDR is sticky.  If @phdr is specified, the next output
1063  * section in the linker script will go there too.  @phdr should have
1064  * a leading colon.
1065  *
1066  * Note that this macros defines __per_cpu_load as an absolute symbol.
1067  * If there is no need to put the percpu section at a predetermined
1068  * address, use PERCPU_SECTION.
1069  */
1070 #define PERCPU_VADDR(cacheline, vaddr, phdr)				\
1071 	__per_cpu_load = .;						\
1072 	.data..percpu vaddr : AT(__per_cpu_load - LOAD_OFFSET) {	\
1073 		PERCPU_INPUT(cacheline)					\
1074 	} phdr								\
1075 	. = __per_cpu_load + SIZEOF(.data..percpu);
1076 
1077 /**
1078  * PERCPU_SECTION - define output section for percpu area, simple version
1079  * @cacheline: cacheline size
1080  *
1081  * Align to PAGE_SIZE and outputs output section for percpu area.  This
1082  * macro doesn't manipulate @vaddr or @phdr and __per_cpu_load and
1083  * __per_cpu_start will be identical.
1084  *
1085  * This macro is equivalent to ALIGN(PAGE_SIZE); PERCPU_VADDR(@cacheline,,)
1086  * except that __per_cpu_load is defined as a relative symbol against
1087  * .data..percpu which is required for relocatable x86_32 configuration.
1088  */
1089 #define PERCPU_SECTION(cacheline)					\
1090 	. = ALIGN(PAGE_SIZE);						\
1091 	.data..percpu	: AT(ADDR(.data..percpu) - LOAD_OFFSET) {	\
1092 		__per_cpu_load = .;					\
1093 		PERCPU_INPUT(cacheline)					\
1094 	}
1095 
1096 
1097 /*
1098  * Definition of the high level *_SECTION macros
1099  * They will fit only a subset of the architectures
1100  */
1101 
1102 
1103 /*
1104  * Writeable data.
1105  * All sections are combined in a single .data section.
1106  * The sections following CONSTRUCTORS are arranged so their
1107  * typical alignment matches.
1108  * A cacheline is typical/always less than a PAGE_SIZE so
1109  * the sections that has this restriction (or similar)
1110  * is located before the ones requiring PAGE_SIZE alignment.
1111  * NOSAVE_DATA starts and ends with a PAGE_SIZE alignment which
1112  * matches the requirement of PAGE_ALIGNED_DATA.
1113  *
1114  * use 0 as page_align if page_aligned data is not used */
1115 #define RW_DATA(cacheline, pagealigned, inittask)			\
1116 	. = ALIGN(PAGE_SIZE);						\
1117 	.data : AT(ADDR(.data) - LOAD_OFFSET) {				\
1118 		INIT_TASK_DATA(inittask)				\
1119 		NOSAVE_DATA						\
1120 		PAGE_ALIGNED_DATA(pagealigned)				\
1121 		CACHELINE_ALIGNED_DATA(cacheline)			\
1122 		READ_MOSTLY_DATA(cacheline)				\
1123 		DATA_DATA						\
1124 		CONSTRUCTORS						\
1125 	}								\
1126 	BUG_TABLE							\
1127 
1128 #define INIT_TEXT_SECTION(inittext_align)				\
1129 	. = ALIGN(inittext_align);					\
1130 	.init.text : AT(ADDR(.init.text) - LOAD_OFFSET) {		\
1131 		_sinittext = .;						\
1132 		INIT_TEXT						\
1133 		_einittext = .;						\
1134 	}
1135 
1136 #define INIT_DATA_SECTION(initsetup_align)				\
1137 	.init.data : AT(ADDR(.init.data) - LOAD_OFFSET) {		\
1138 		INIT_DATA						\
1139 		INIT_SETUP(initsetup_align)				\
1140 		INIT_CALLS						\
1141 		CON_INITCALL						\
1142 		INIT_RAM_FS						\
1143 	}
1144 
1145 #define BSS_SECTION(sbss_align, bss_align, stop_align)			\
1146 	. = ALIGN(sbss_align);						\
1147 	__bss_start = .;						\
1148 	SBSS(sbss_align)						\
1149 	BSS(bss_align)							\
1150 	. = ALIGN(stop_align);						\
1151 	__bss_stop = .;
1152