vmlinux.lds.S 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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 <[email protected]>
  6. *
  7. * Modernisation, unification and other changes and fixes:
  8. * Copyright (C) 2007-2009 Sam Ravnborg <[email protected]>
  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. #ifdef CONFIG_X86_32
  18. #define LOAD_OFFSET __PAGE_OFFSET
  19. #else
  20. #define LOAD_OFFSET __START_KERNEL_map
  21. #endif
  22. #define RUNTIME_DISCARD_EXIT
  23. #define EMITS_PT_NOTE
  24. #define RO_EXCEPTION_TABLE_ALIGN 16
  25. #include <asm-generic/vmlinux.lds.h>
  26. #include <asm/asm-offsets.h>
  27. #include <asm/thread_info.h>
  28. #include <asm/page_types.h>
  29. #include <asm/orc_lookup.h>
  30. #include <asm/cache.h>
  31. #include <asm/boot.h>
  32. #undef i386 /* in case the preprocessor is a 32bit one */
  33. OUTPUT_FORMAT(CONFIG_OUTPUT_FORMAT)
  34. #ifdef CONFIG_X86_32
  35. OUTPUT_ARCH(i386)
  36. ENTRY(phys_startup_32)
  37. #else
  38. OUTPUT_ARCH(i386:x86-64)
  39. ENTRY(phys_startup_64)
  40. #endif
  41. jiffies = jiffies_64;
  42. #if defined(CONFIG_X86_64)
  43. /*
  44. * On 64-bit, align RODATA to 2MB so we retain large page mappings for
  45. * boundaries spanning kernel text, rodata and data sections.
  46. *
  47. * However, kernel identity mappings will have different RWX permissions
  48. * to the pages mapping to text and to the pages padding (which are freed) the
  49. * text section. Hence kernel identity mappings will be broken to smaller
  50. * pages. For 64-bit, kernel text and kernel identity mappings are different,
  51. * so we can enable protection checks as well as retain 2MB large page
  52. * mappings for kernel text.
  53. */
  54. #define X86_ALIGN_RODATA_BEGIN . = ALIGN(HPAGE_SIZE);
  55. #define X86_ALIGN_RODATA_END \
  56. . = ALIGN(HPAGE_SIZE); \
  57. __end_rodata_hpage_align = .; \
  58. __end_rodata_aligned = .;
  59. #define ALIGN_ENTRY_TEXT_BEGIN . = ALIGN(PMD_SIZE);
  60. #define ALIGN_ENTRY_TEXT_END . = ALIGN(PMD_SIZE);
  61. /*
  62. * This section contains data which will be mapped as decrypted. Memory
  63. * encryption operates on a page basis. Make this section PMD-aligned
  64. * to avoid splitting the pages while mapping the section early.
  65. *
  66. * Note: We use a separate section so that only this section gets
  67. * decrypted to avoid exposing more than we wish.
  68. */
  69. #define BSS_DECRYPTED \
  70. . = ALIGN(PMD_SIZE); \
  71. __start_bss_decrypted = .; \
  72. *(.bss..decrypted); \
  73. . = ALIGN(PAGE_SIZE); \
  74. __start_bss_decrypted_unused = .; \
  75. . = ALIGN(PMD_SIZE); \
  76. __end_bss_decrypted = .; \
  77. #else
  78. #define X86_ALIGN_RODATA_BEGIN
  79. #define X86_ALIGN_RODATA_END \
  80. . = ALIGN(PAGE_SIZE); \
  81. __end_rodata_aligned = .;
  82. #define ALIGN_ENTRY_TEXT_BEGIN
  83. #define ALIGN_ENTRY_TEXT_END
  84. #define BSS_DECRYPTED
  85. #endif
  86. PHDRS {
  87. text PT_LOAD FLAGS(5); /* R_E */
  88. data PT_LOAD FLAGS(6); /* RW_ */
  89. #ifdef CONFIG_X86_64
  90. #ifdef CONFIG_SMP
  91. percpu PT_LOAD FLAGS(6); /* RW_ */
  92. #endif
  93. init PT_LOAD FLAGS(7); /* RWE */
  94. #endif
  95. note PT_NOTE FLAGS(0); /* ___ */
  96. }
  97. SECTIONS
  98. {
  99. #ifdef CONFIG_X86_32
  100. . = LOAD_OFFSET + LOAD_PHYSICAL_ADDR;
  101. phys_startup_32 = ABSOLUTE(startup_32 - LOAD_OFFSET);
  102. #else
  103. . = __START_KERNEL;
  104. phys_startup_64 = ABSOLUTE(startup_64 - LOAD_OFFSET);
  105. #endif
  106. /* Text and read-only data */
  107. .text : AT(ADDR(.text) - LOAD_OFFSET) {
  108. _text = .;
  109. _stext = .;
  110. /* bootstrapping code */
  111. HEAD_TEXT
  112. TEXT_TEXT
  113. SCHED_TEXT
  114. CPUIDLE_TEXT
  115. LOCK_TEXT
  116. KPROBES_TEXT
  117. ALIGN_ENTRY_TEXT_BEGIN
  118. #ifdef CONFIG_CPU_SRSO
  119. *(.text..__x86.rethunk_untrain)
  120. #endif
  121. ENTRY_TEXT
  122. #ifdef CONFIG_CPU_SRSO
  123. /*
  124. * See the comment above srso_alias_untrain_ret()'s
  125. * definition.
  126. */
  127. . = srso_alias_untrain_ret | (1 << 2) | (1 << 8) | (1 << 14) | (1 << 20);
  128. *(.text..__x86.rethunk_safe)
  129. #endif
  130. ALIGN_ENTRY_TEXT_END
  131. SOFTIRQENTRY_TEXT
  132. STATIC_CALL_TEXT
  133. *(.gnu.warning)
  134. #ifdef CONFIG_RETPOLINE
  135. __indirect_thunk_start = .;
  136. *(.text..__x86.indirect_thunk)
  137. *(.text..__x86.return_thunk)
  138. __indirect_thunk_end = .;
  139. #endif
  140. } :text =0xcccc
  141. /* End of text section, which should occupy whole number of pages */
  142. _etext = .;
  143. . = ALIGN(PAGE_SIZE);
  144. X86_ALIGN_RODATA_BEGIN
  145. RO_DATA(PAGE_SIZE)
  146. X86_ALIGN_RODATA_END
  147. /* Data */
  148. .data : AT(ADDR(.data) - LOAD_OFFSET) {
  149. /* Start of data section */
  150. _sdata = .;
  151. /* init_task */
  152. INIT_TASK_DATA(THREAD_SIZE)
  153. #ifdef CONFIG_X86_32
  154. /* 32 bit has nosave before _edata */
  155. NOSAVE_DATA
  156. #endif
  157. PAGE_ALIGNED_DATA(PAGE_SIZE)
  158. CACHELINE_ALIGNED_DATA(L1_CACHE_BYTES)
  159. DATA_DATA
  160. CONSTRUCTORS
  161. /* rarely changed data like cpu maps */
  162. READ_MOSTLY_DATA(INTERNODE_CACHE_BYTES)
  163. /* End of data section */
  164. _edata = .;
  165. } :data
  166. BUG_TABLE
  167. ORC_UNWIND_TABLE
  168. . = ALIGN(PAGE_SIZE);
  169. __vvar_page = .;
  170. .vvar : AT(ADDR(.vvar) - LOAD_OFFSET) {
  171. /* work around gold bug 13023 */
  172. __vvar_beginning_hack = .;
  173. /* Place all vvars at the offsets in asm/vvar.h. */
  174. #define EMIT_VVAR(name, offset) \
  175. . = __vvar_beginning_hack + offset; \
  176. *(.vvar_ ## name)
  177. #include <asm/vvar.h>
  178. #undef EMIT_VVAR
  179. /*
  180. * Pad the rest of the page with zeros. Otherwise the loader
  181. * can leave garbage here.
  182. */
  183. . = __vvar_beginning_hack + PAGE_SIZE;
  184. } :data
  185. . = ALIGN(__vvar_page + PAGE_SIZE, PAGE_SIZE);
  186. /* Init code and data - will be freed after init */
  187. . = ALIGN(PAGE_SIZE);
  188. .init.begin : AT(ADDR(.init.begin) - LOAD_OFFSET) {
  189. __init_begin = .; /* paired with __init_end */
  190. }
  191. #if defined(CONFIG_X86_64) && defined(CONFIG_SMP)
  192. /*
  193. * percpu offsets are zero-based on SMP. PERCPU_VADDR() changes the
  194. * output PHDR, so the next output section - .init.text - should
  195. * start another segment - init.
  196. */
  197. PERCPU_VADDR(INTERNODE_CACHE_BYTES, 0, :percpu)
  198. ASSERT(SIZEOF(.data..percpu) < CONFIG_PHYSICAL_START,
  199. "per-CPU data too large - increase CONFIG_PHYSICAL_START")
  200. #endif
  201. INIT_TEXT_SECTION(PAGE_SIZE)
  202. #ifdef CONFIG_X86_64
  203. :init
  204. #endif
  205. /*
  206. * Section for code used exclusively before alternatives are run. All
  207. * references to such code must be patched out by alternatives, normally
  208. * by using X86_FEATURE_ALWAYS CPU feature bit.
  209. *
  210. * See static_cpu_has() for an example.
  211. */
  212. .altinstr_aux : AT(ADDR(.altinstr_aux) - LOAD_OFFSET) {
  213. *(.altinstr_aux)
  214. }
  215. INIT_DATA_SECTION(16)
  216. .x86_cpu_dev.init : AT(ADDR(.x86_cpu_dev.init) - LOAD_OFFSET) {
  217. __x86_cpu_dev_start = .;
  218. *(.x86_cpu_dev.init)
  219. __x86_cpu_dev_end = .;
  220. }
  221. #ifdef CONFIG_X86_INTEL_MID
  222. .x86_intel_mid_dev.init : AT(ADDR(.x86_intel_mid_dev.init) - \
  223. LOAD_OFFSET) {
  224. __x86_intel_mid_dev_start = .;
  225. *(.x86_intel_mid_dev.init)
  226. __x86_intel_mid_dev_end = .;
  227. }
  228. #endif
  229. /*
  230. * start address and size of operations which during runtime
  231. * can be patched with virtualization friendly instructions or
  232. * baremetal native ones. Think page table operations.
  233. * Details in paravirt_types.h
  234. */
  235. . = ALIGN(8);
  236. .parainstructions : AT(ADDR(.parainstructions) - LOAD_OFFSET) {
  237. __parainstructions = .;
  238. *(.parainstructions)
  239. __parainstructions_end = .;
  240. }
  241. #ifdef CONFIG_RETPOLINE
  242. /*
  243. * List of instructions that call/jmp/jcc to retpoline thunks
  244. * __x86_indirect_thunk_*(). These instructions can be patched along
  245. * with alternatives, after which the section can be freed.
  246. */
  247. . = ALIGN(8);
  248. .retpoline_sites : AT(ADDR(.retpoline_sites) - LOAD_OFFSET) {
  249. __retpoline_sites = .;
  250. *(.retpoline_sites)
  251. __retpoline_sites_end = .;
  252. }
  253. . = ALIGN(8);
  254. .return_sites : AT(ADDR(.return_sites) - LOAD_OFFSET) {
  255. __return_sites = .;
  256. *(.return_sites)
  257. __return_sites_end = .;
  258. }
  259. #endif
  260. #ifdef CONFIG_X86_KERNEL_IBT
  261. . = ALIGN(8);
  262. .ibt_endbr_seal : AT(ADDR(.ibt_endbr_seal) - LOAD_OFFSET) {
  263. __ibt_endbr_seal = .;
  264. *(.ibt_endbr_seal)
  265. __ibt_endbr_seal_end = .;
  266. }
  267. #endif
  268. /*
  269. * struct alt_inst entries. From the header (alternative.h):
  270. * "Alternative instructions for different CPU types or capabilities"
  271. * Think locking instructions on spinlocks.
  272. */
  273. . = ALIGN(8);
  274. .altinstructions : AT(ADDR(.altinstructions) - LOAD_OFFSET) {
  275. __alt_instructions = .;
  276. *(.altinstructions)
  277. __alt_instructions_end = .;
  278. }
  279. /*
  280. * And here are the replacement instructions. The linker sticks
  281. * them as binary blobs. The .altinstructions has enough data to
  282. * get the address and the length of them to patch the kernel safely.
  283. */
  284. .altinstr_replacement : AT(ADDR(.altinstr_replacement) - LOAD_OFFSET) {
  285. *(.altinstr_replacement)
  286. }
  287. . = ALIGN(8);
  288. .apicdrivers : AT(ADDR(.apicdrivers) - LOAD_OFFSET) {
  289. __apicdrivers = .;
  290. *(.apicdrivers);
  291. __apicdrivers_end = .;
  292. }
  293. . = ALIGN(8);
  294. /*
  295. * .exit.text is discarded at runtime, not link time, to deal with
  296. * references from .altinstructions
  297. */
  298. .exit.text : AT(ADDR(.exit.text) - LOAD_OFFSET) {
  299. EXIT_TEXT
  300. }
  301. .exit.data : AT(ADDR(.exit.data) - LOAD_OFFSET) {
  302. EXIT_DATA
  303. }
  304. #if !defined(CONFIG_X86_64) || !defined(CONFIG_SMP)
  305. PERCPU_SECTION(INTERNODE_CACHE_BYTES)
  306. #endif
  307. . = ALIGN(PAGE_SIZE);
  308. /* freed after init ends here */
  309. .init.end : AT(ADDR(.init.end) - LOAD_OFFSET) {
  310. __init_end = .;
  311. }
  312. /*
  313. * smp_locks might be freed after init
  314. * start/end must be page aligned
  315. */
  316. . = ALIGN(PAGE_SIZE);
  317. .smp_locks : AT(ADDR(.smp_locks) - LOAD_OFFSET) {
  318. __smp_locks = .;
  319. *(.smp_locks)
  320. . = ALIGN(PAGE_SIZE);
  321. __smp_locks_end = .;
  322. }
  323. #ifdef CONFIG_X86_64
  324. .data_nosave : AT(ADDR(.data_nosave) - LOAD_OFFSET) {
  325. NOSAVE_DATA
  326. }
  327. #endif
  328. /* BSS */
  329. . = ALIGN(PAGE_SIZE);
  330. .bss : AT(ADDR(.bss) - LOAD_OFFSET) {
  331. __bss_start = .;
  332. *(.bss..page_aligned)
  333. . = ALIGN(PAGE_SIZE);
  334. *(BSS_MAIN)
  335. BSS_DECRYPTED
  336. . = ALIGN(PAGE_SIZE);
  337. __bss_stop = .;
  338. }
  339. /*
  340. * The memory occupied from _text to here, __end_of_kernel_reserve, is
  341. * automatically reserved in setup_arch(). Anything after here must be
  342. * explicitly reserved using memblock_reserve() or it will be discarded
  343. * and treated as available memory.
  344. */
  345. __end_of_kernel_reserve = .;
  346. . = ALIGN(PAGE_SIZE);
  347. .brk : AT(ADDR(.brk) - LOAD_OFFSET) {
  348. __brk_base = .;
  349. . += 64 * 1024; /* 64k alignment slop space */
  350. *(.bss..brk) /* areas brk users have reserved */
  351. __brk_limit = .;
  352. }
  353. . = ALIGN(PAGE_SIZE); /* keep VO_INIT_SIZE page aligned */
  354. _end = .;
  355. #ifdef CONFIG_AMD_MEM_ENCRYPT
  356. /*
  357. * Early scratch/workarea section: Lives outside of the kernel proper
  358. * (_text - _end).
  359. *
  360. * Resides after _end because even though the .brk section is after
  361. * __end_of_kernel_reserve, the .brk section is later reserved as a
  362. * part of the kernel. Since it is located after __end_of_kernel_reserve
  363. * it will be discarded and become part of the available memory. As
  364. * such, it can only be used by very early boot code and must not be
  365. * needed afterwards.
  366. *
  367. * Currently used by SME for performing in-place encryption of the
  368. * kernel during boot. Resides on a 2MB boundary to simplify the
  369. * pagetable setup used for SME in-place encryption.
  370. */
  371. . = ALIGN(HPAGE_SIZE);
  372. .init.scratch : AT(ADDR(.init.scratch) - LOAD_OFFSET) {
  373. __init_scratch_begin = .;
  374. *(.init.scratch)
  375. . = ALIGN(HPAGE_SIZE);
  376. __init_scratch_end = .;
  377. }
  378. #endif
  379. STABS_DEBUG
  380. DWARF_DEBUG
  381. ELF_DETAILS
  382. DISCARDS
  383. /*
  384. * Make sure that the .got.plt is either completely empty or it
  385. * contains only the lazy dispatch entries.
  386. */
  387. .got.plt (INFO) : { *(.got.plt) }
  388. ASSERT(SIZEOF(.got.plt) == 0 ||
  389. #ifdef CONFIG_X86_64
  390. SIZEOF(.got.plt) == 0x18,
  391. #else
  392. SIZEOF(.got.plt) == 0xc,
  393. #endif
  394. "Unexpected GOT/PLT entries detected!")
  395. /*
  396. * Sections that should stay zero sized, which is safer to
  397. * explicitly check instead of blindly discarding.
  398. */
  399. .got : {
  400. *(.got) *(.igot.*)
  401. }
  402. ASSERT(SIZEOF(.got) == 0, "Unexpected GOT entries detected!")
  403. .plt : {
  404. *(.plt) *(.plt.*) *(.iplt)
  405. }
  406. ASSERT(SIZEOF(.plt) == 0, "Unexpected run-time procedure linkages detected!")
  407. .rel.dyn : {
  408. *(.rel.*) *(.rel_*)
  409. }
  410. ASSERT(SIZEOF(.rel.dyn) == 0, "Unexpected run-time relocations (.rel) detected!")
  411. .rela.dyn : {
  412. *(.rela.*) *(.rela_*)
  413. }
  414. ASSERT(SIZEOF(.rela.dyn) == 0, "Unexpected run-time relocations (.rela) detected!")
  415. }
  416. /*
  417. * The ASSERT() sink to . is intentional, for binutils 2.14 compatibility:
  418. */
  419. . = ASSERT((_end - LOAD_OFFSET <= KERNEL_IMAGE_SIZE),
  420. "kernel image bigger than KERNEL_IMAGE_SIZE");
  421. #ifdef CONFIG_X86_64
  422. /*
  423. * Per-cpu symbols which need to be offset from __per_cpu_load
  424. * for the boot processor.
  425. */
  426. #define INIT_PER_CPU(x) init_per_cpu__##x = ABSOLUTE(x) + __per_cpu_load
  427. INIT_PER_CPU(gdt_page);
  428. INIT_PER_CPU(fixed_percpu_data);
  429. INIT_PER_CPU(irq_stack_backing_store);
  430. #ifdef CONFIG_SMP
  431. . = ASSERT((fixed_percpu_data == 0),
  432. "fixed_percpu_data is not at start of per-cpu area");
  433. #endif
  434. #ifdef CONFIG_RETHUNK
  435. . = ASSERT((retbleed_return_thunk & 0x3f) == 0, "retbleed_return_thunk not cacheline-aligned");
  436. . = ASSERT((srso_safe_ret & 0x3f) == 0, "srso_safe_ret not cacheline-aligned");
  437. #endif
  438. #ifdef CONFIG_CPU_SRSO
  439. /*
  440. * GNU ld cannot do XOR until 2.41.
  441. * https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=f6f78318fca803c4907fb8d7f6ded8295f1947b1
  442. *
  443. * LLVM lld cannot do XOR until lld-17.
  444. * https://github.com/llvm/llvm-project/commit/fae96104d4378166cbe5c875ef8ed808a356f3fb
  445. *
  446. * Instead do: (A | B) - (A & B) in order to compute the XOR
  447. * of the two function addresses:
  448. */
  449. . = ASSERT(((ABSOLUTE(srso_alias_untrain_ret) | srso_alias_safe_ret) -
  450. (ABSOLUTE(srso_alias_untrain_ret) & srso_alias_safe_ret)) == ((1 << 2) | (1 << 8) | (1 << 14) | (1 << 20)),
  451. "SRSO function pair won't alias");
  452. #endif
  453. #endif /* CONFIG_X86_64 */
  454. #ifdef CONFIG_KEXEC_CORE
  455. #include <asm/kexec.h>
  456. . = ASSERT(kexec_control_code_size <= KEXEC_CONTROL_CODE_MAX_SIZE,
  457. "kexec control code size is too big");
  458. #endif