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) 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 X86_ALIGN_RODATA_BEGIN . = ALIGN(HPAGE_SIZE); 59 60#define X86_ALIGN_RODATA_END \ 61 . = ALIGN(HPAGE_SIZE); \ 62 __end_rodata_hpage_align = .; \ 63 __end_rodata_aligned = .; 64 65#define ALIGN_ENTRY_TEXT_BEGIN . = ALIGN(PMD_SIZE); 66#define ALIGN_ENTRY_TEXT_END . = ALIGN(PMD_SIZE); 67 68/* 69 * This section contains data which will be mapped as decrypted. Memory 70 * encryption operates on a page basis. Make this section PMD-aligned 71 * to avoid splitting the pages while mapping the section early. 72 * 73 * Note: We use a separate section so that only this section gets 74 * decrypted to avoid exposing more than we wish. 75 */ 76#define BSS_DECRYPTED \ 77 . = ALIGN(PMD_SIZE); \ 78 __start_bss_decrypted = .; \ 79 *(.bss..decrypted); \ 80 . = ALIGN(PAGE_SIZE); \ 81 __start_bss_decrypted_unused = .; \ 82 . = ALIGN(PMD_SIZE); \ 83 __end_bss_decrypted = .; \ 84 85#else 86 87#define X86_ALIGN_RODATA_BEGIN 88#define X86_ALIGN_RODATA_END \ 89 . = ALIGN(PAGE_SIZE); \ 90 __end_rodata_aligned = .; 91 92#define ALIGN_ENTRY_TEXT_BEGIN 93#define ALIGN_ENTRY_TEXT_END 94#define BSS_DECRYPTED 95 96#endif 97 98PHDRS { 99 text PT_LOAD FLAGS(5); /* R_E */ 100 data PT_LOAD FLAGS(6); /* RW_ */ 101#ifdef CONFIG_X86_64 102#ifdef CONFIG_SMP 103 percpu PT_LOAD FLAGS(6); /* RW_ */ 104#endif 105 init PT_LOAD FLAGS(7); /* RWE */ 106#endif 107 note PT_NOTE FLAGS(0); /* ___ */ 108} 109 110SECTIONS 111{ 112#ifdef CONFIG_X86_32 113 . = LOAD_OFFSET + LOAD_PHYSICAL_ADDR; 114 phys_startup_32 = ABSOLUTE(startup_32 - LOAD_OFFSET); 115#else 116 . = __START_KERNEL; 117 phys_startup_64 = ABSOLUTE(startup_64 - LOAD_OFFSET); 118#endif 119 120 /* Text and read-only data */ 121 .text : AT(ADDR(.text) - LOAD_OFFSET) { 122 _text = .; 123 _stext = .; 124 /* bootstrapping code */ 125 HEAD_TEXT 126 TEXT_TEXT 127 SCHED_TEXT 128 CPUIDLE_TEXT 129 LOCK_TEXT 130 KPROBES_TEXT 131 ALIGN_ENTRY_TEXT_BEGIN 132 ENTRY_TEXT 133 IRQENTRY_TEXT 134 ALIGN_ENTRY_TEXT_END 135 SOFTIRQENTRY_TEXT 136 *(.fixup) 137 *(.gnu.warning) 138 139#ifdef CONFIG_RETPOLINE 140 __indirect_thunk_start = .; 141 *(.text.__x86.indirect_thunk) 142 __indirect_thunk_end = .; 143#endif 144 145#ifdef CONFIG_CFI_CLANG 146 . = ALIGN(PAGE_SIZE); 147 __cfi_jt_start = .; 148 *(.text..L.cfi.jumptable .text..L.cfi.jumptable.*) 149 __cfi_jt_end = .; 150#endif 151 } :text = 0x9090 152 153 NOTES :text :note 154 155 EXCEPTION_TABLE(16) :text = 0x9090 156 157 /* End of text section, which should occupy whole number of pages */ 158 _etext = .; 159 . = ALIGN(PAGE_SIZE); 160 161 X86_ALIGN_RODATA_BEGIN 162 RO_DATA(PAGE_SIZE) 163 X86_ALIGN_RODATA_END 164 165 /* Data */ 166 .data : AT(ADDR(.data) - LOAD_OFFSET) { 167 /* Start of data section */ 168 _sdata = .; 169 170 /* init_task */ 171 INIT_TASK_DATA(THREAD_SIZE) 172 173#ifdef CONFIG_X86_32 174 /* 32 bit has nosave before _edata */ 175 NOSAVE_DATA 176#endif 177 178 PAGE_ALIGNED_DATA(PAGE_SIZE) 179 180 CACHELINE_ALIGNED_DATA(L1_CACHE_BYTES) 181 182 DATA_DATA 183 CONSTRUCTORS 184 185 /* rarely changed data like cpu maps */ 186 READ_MOSTLY_DATA(INTERNODE_CACHE_BYTES) 187 188 /* End of data section */ 189 _edata = .; 190 } :data 191 192 BUG_TABLE 193 194 ORC_UNWIND_TABLE 195 196 . = ALIGN(PAGE_SIZE); 197 __vvar_page = .; 198 199 .vvar : AT(ADDR(.vvar) - LOAD_OFFSET) { 200 /* work around gold bug 13023 */ 201 __vvar_beginning_hack = .; 202 203 /* Place all vvars at the offsets in asm/vvar.h. */ 204#define EMIT_VVAR(name, offset) \ 205 . = __vvar_beginning_hack + offset; \ 206 *(.vvar_ ## name) 207#define __VVAR_KERNEL_LDS 208#include <asm/vvar.h> 209#undef __VVAR_KERNEL_LDS 210#undef EMIT_VVAR 211 212 /* 213 * Pad the rest of the page with zeros. Otherwise the loader 214 * can leave garbage here. 215 */ 216 . = __vvar_beginning_hack + PAGE_SIZE; 217 } :data 218 219 . = ALIGN(__vvar_page + PAGE_SIZE, PAGE_SIZE); 220 221 /* Init code and data - will be freed after init */ 222 . = ALIGN(PAGE_SIZE); 223 .init.begin : AT(ADDR(.init.begin) - LOAD_OFFSET) { 224 __init_begin = .; /* paired with __init_end */ 225 } 226 227#if defined(CONFIG_X86_64) && defined(CONFIG_SMP) 228 /* 229 * percpu offsets are zero-based on SMP. PERCPU_VADDR() changes the 230 * output PHDR, so the next output section - .init.text - should 231 * start another segment - init. 232 */ 233 PERCPU_VADDR(INTERNODE_CACHE_BYTES, 0, :percpu) 234 ASSERT(SIZEOF(.data..percpu) < CONFIG_PHYSICAL_START, 235 "per-CPU data too large - increase CONFIG_PHYSICAL_START") 236#endif 237 238 INIT_TEXT_SECTION(PAGE_SIZE) 239#ifdef CONFIG_X86_64 240 :init 241#endif 242 243 /* 244 * Section for code used exclusively before alternatives are run. All 245 * references to such code must be patched out by alternatives, normally 246 * by using X86_FEATURE_ALWAYS CPU feature bit. 247 * 248 * See static_cpu_has() for an example. 249 */ 250 .altinstr_aux : AT(ADDR(.altinstr_aux) - LOAD_OFFSET) { 251 *(.altinstr_aux) 252 } 253 254 INIT_DATA_SECTION(16) 255 256 .x86_cpu_dev.init : AT(ADDR(.x86_cpu_dev.init) - LOAD_OFFSET) { 257 __x86_cpu_dev_start = .; 258 *(.x86_cpu_dev.init) 259 __x86_cpu_dev_end = .; 260 } 261 262#ifdef CONFIG_X86_INTEL_MID 263 .x86_intel_mid_dev.init : AT(ADDR(.x86_intel_mid_dev.init) - \ 264 LOAD_OFFSET) { 265 __x86_intel_mid_dev_start = .; 266 *(.x86_intel_mid_dev.init) 267 __x86_intel_mid_dev_end = .; 268 } 269#endif 270 271 /* 272 * start address and size of operations which during runtime 273 * can be patched with virtualization friendly instructions or 274 * baremetal native ones. Think page table operations. 275 * Details in paravirt_types.h 276 */ 277 . = ALIGN(8); 278 .parainstructions : AT(ADDR(.parainstructions) - LOAD_OFFSET) { 279 __parainstructions = .; 280 *(.parainstructions) 281 __parainstructions_end = .; 282 } 283 284 /* 285 * struct alt_inst entries. From the header (alternative.h): 286 * "Alternative instructions for different CPU types or capabilities" 287 * Think locking instructions on spinlocks. 288 */ 289 . = ALIGN(8); 290 .altinstructions : AT(ADDR(.altinstructions) - LOAD_OFFSET) { 291 __alt_instructions = .; 292 *(.altinstructions) 293 __alt_instructions_end = .; 294 } 295 296 /* 297 * And here are the replacement instructions. The linker sticks 298 * them as binary blobs. The .altinstructions has enough data to 299 * get the address and the length of them to patch the kernel safely. 300 */ 301 .altinstr_replacement : AT(ADDR(.altinstr_replacement) - LOAD_OFFSET) { 302 *(.altinstr_replacement) 303 } 304 305 /* 306 * struct iommu_table_entry entries are injected in this section. 307 * It is an array of IOMMUs which during run time gets sorted depending 308 * on its dependency order. After rootfs_initcall is complete 309 * this section can be safely removed. 310 */ 311 .iommu_table : AT(ADDR(.iommu_table) - LOAD_OFFSET) { 312 __iommu_table = .; 313 *(.iommu_table) 314 __iommu_table_end = .; 315 } 316 317 . = ALIGN(8); 318 .apicdrivers : AT(ADDR(.apicdrivers) - LOAD_OFFSET) { 319 __apicdrivers = .; 320 *(.apicdrivers); 321 __apicdrivers_end = .; 322 } 323 324 . = ALIGN(8); 325 /* 326 * .exit.text is discard at runtime, not link time, to deal with 327 * references from .altinstructions and .eh_frame 328 */ 329 .exit.text : AT(ADDR(.exit.text) - LOAD_OFFSET) { 330 EXIT_TEXT 331 } 332 333 .exit.data : AT(ADDR(.exit.data) - LOAD_OFFSET) { 334 EXIT_DATA 335 } 336 337#if !defined(CONFIG_X86_64) || !defined(CONFIG_SMP) 338 PERCPU_SECTION(INTERNODE_CACHE_BYTES) 339#endif 340 341 . = ALIGN(PAGE_SIZE); 342 343 /* freed after init ends here */ 344 .init.end : AT(ADDR(.init.end) - LOAD_OFFSET) { 345 __init_end = .; 346 } 347 348 /* 349 * smp_locks might be freed after init 350 * start/end must be page aligned 351 */ 352 . = ALIGN(PAGE_SIZE); 353 .smp_locks : AT(ADDR(.smp_locks) - LOAD_OFFSET) { 354 __smp_locks = .; 355 *(.smp_locks) 356 . = ALIGN(PAGE_SIZE); 357 __smp_locks_end = .; 358 } 359 360#ifdef CONFIG_X86_64 361 .data_nosave : AT(ADDR(.data_nosave) - LOAD_OFFSET) { 362 NOSAVE_DATA 363 } 364#endif 365 366 /* BSS */ 367 . = ALIGN(PAGE_SIZE); 368 .bss : AT(ADDR(.bss) - LOAD_OFFSET) { 369 __bss_start = .; 370 *(.bss..page_aligned) 371 *(BSS_MAIN) 372 BSS_DECRYPTED 373 . = ALIGN(PAGE_SIZE); 374 __bss_stop = .; 375 } 376 377 /* 378 * The memory occupied from _text to here, __end_of_kernel_reserve, is 379 * automatically reserved in setup_arch(). Anything after here must be 380 * explicitly reserved using memblock_reserve() or it will be discarded 381 * and treated as available memory. 382 */ 383 __end_of_kernel_reserve = .; 384 385 . = ALIGN(PAGE_SIZE); 386 .brk : AT(ADDR(.brk) - LOAD_OFFSET) { 387 __brk_base = .; 388 . += 64 * 1024; /* 64k alignment slop space */ 389 *(.brk_reservation) /* areas brk users have reserved */ 390 __brk_limit = .; 391 } 392 393 . = ALIGN(PAGE_SIZE); /* keep VO_INIT_SIZE page aligned */ 394 _end = .; 395 396#ifdef CONFIG_AMD_MEM_ENCRYPT 397 /* 398 * Early scratch/workarea section: Lives outside of the kernel proper 399 * (_text - _end). 400 * 401 * Resides after _end because even though the .brk section is after 402 * __end_of_kernel_reserve, the .brk section is later reserved as a 403 * part of the kernel. Since it is located after __end_of_kernel_reserve 404 * it will be discarded and become part of the available memory. As 405 * such, it can only be used by very early boot code and must not be 406 * needed afterwards. 407 * 408 * Currently used by SME for performing in-place encryption of the 409 * kernel during boot. Resides on a 2MB boundary to simplify the 410 * pagetable setup used for SME in-place encryption. 411 */ 412 . = ALIGN(HPAGE_SIZE); 413 .init.scratch : AT(ADDR(.init.scratch) - LOAD_OFFSET) { 414 __init_scratch_begin = .; 415 *(.init.scratch) 416 . = ALIGN(HPAGE_SIZE); 417 __init_scratch_end = .; 418 } 419#endif 420 421 STABS_DEBUG 422 DWARF_DEBUG 423 424 DISCARDS 425 /DISCARD/ : { 426 *(.eh_frame) 427 } 428} 429 430 431#ifdef CONFIG_X86_32 432/* 433 * The ASSERT() sink to . is intentional, for binutils 2.14 compatibility: 434 */ 435. = ASSERT((_end - LOAD_OFFSET <= KERNEL_IMAGE_SIZE), 436 "kernel image bigger than KERNEL_IMAGE_SIZE"); 437#else 438/* 439 * Per-cpu symbols which need to be offset from __per_cpu_load 440 * for the boot processor. 441 */ 442#define INIT_PER_CPU(x) init_per_cpu__##x = ABSOLUTE(x) + __per_cpu_load 443INIT_PER_CPU(gdt_page); 444INIT_PER_CPU(fixed_percpu_data); 445INIT_PER_CPU(irq_stack_backing_store); 446 447/* 448 * Build-time check on the image size: 449 */ 450. = ASSERT((_end - _text <= KERNEL_IMAGE_SIZE), 451 "kernel image bigger than KERNEL_IMAGE_SIZE"); 452 453#ifdef CONFIG_SMP 454. = ASSERT((fixed_percpu_data == 0), 455 "fixed_percpu_data is not at start of per-cpu area"); 456#endif 457 458#endif /* CONFIG_X86_32 */ 459 460#ifdef CONFIG_KEXEC_CORE 461#include <asm/kexec.h> 462 463. = ASSERT(kexec_control_code_size <= KEXEC_CONTROL_CODE_MAX_SIZE, 464 "kexec control code size is too big"); 465#endif 466 467#ifdef CONFIG_CFI_CLANG 468. = ASSERT((__cfi_jt_end - __cfi_jt_start > 0), 469 "CFI jump table is empty"); 470#endif 471