vmlinux.lds.S 13 KB

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