vmlinux.lds.S 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. #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. ENTRY_TEXT
  119. ALIGN_ENTRY_TEXT_END
  120. SOFTIRQENTRY_TEXT
  121. STATIC_CALL_TEXT
  122. *(.fixup)
  123. *(.gnu.warning)
  124. #ifdef CONFIG_RETPOLINE
  125. __indirect_thunk_start = .;
  126. *(.text.__x86.indirect_thunk)
  127. __indirect_thunk_end = .;
  128. #endif
  129. } :text =0xcccc
  130. /* End of text section, which should occupy whole number of pages */
  131. _etext = .;
  132. . = ALIGN(PAGE_SIZE);
  133. X86_ALIGN_RODATA_BEGIN
  134. RO_DATA(PAGE_SIZE)
  135. X86_ALIGN_RODATA_END
  136. /* Data */
  137. .data : AT(ADDR(.data) - LOAD_OFFSET) {
  138. /* Start of data section */
  139. _sdata = .;
  140. /* init_task */
  141. INIT_TASK_DATA(THREAD_SIZE)
  142. #ifdef CONFIG_X86_32
  143. /* 32 bit has nosave before _edata */
  144. NOSAVE_DATA
  145. #endif
  146. PAGE_ALIGNED_DATA(PAGE_SIZE)
  147. CACHELINE_ALIGNED_DATA(L1_CACHE_BYTES)
  148. DATA_DATA
  149. CONSTRUCTORS
  150. /* rarely changed data like cpu maps */
  151. READ_MOSTLY_DATA(INTERNODE_CACHE_BYTES)
  152. /* End of data section */
  153. _edata = .;
  154. } :data
  155. BUG_TABLE
  156. ORC_UNWIND_TABLE
  157. . = ALIGN(PAGE_SIZE);
  158. __vvar_page = .;
  159. .vvar : AT(ADDR(.vvar) - LOAD_OFFSET) {
  160. /* work around gold bug 13023 */
  161. __vvar_beginning_hack = .;
  162. /* Place all vvars at the offsets in asm/vvar.h. */
  163. #define EMIT_VVAR(name, offset) \
  164. . = __vvar_beginning_hack + offset; \
  165. *(.vvar_ ## name)
  166. #include <asm/vvar.h>
  167. #undef EMIT_VVAR
  168. /*
  169. * Pad the rest of the page with zeros. Otherwise the loader
  170. * can leave garbage here.
  171. */
  172. . = __vvar_beginning_hack + PAGE_SIZE;
  173. } :data
  174. . = ALIGN(__vvar_page + PAGE_SIZE, PAGE_SIZE);
  175. /* Init code and data - will be freed after init */
  176. . = ALIGN(PAGE_SIZE);
  177. .init.begin : AT(ADDR(.init.begin) - LOAD_OFFSET) {
  178. __init_begin = .; /* paired with __init_end */
  179. }
  180. #if defined(CONFIG_X86_64) && defined(CONFIG_SMP)
  181. /*
  182. * percpu offsets are zero-based on SMP. PERCPU_VADDR() changes the
  183. * output PHDR, so the next output section - .init.text - should
  184. * start another segment - init.
  185. */
  186. PERCPU_VADDR(INTERNODE_CACHE_BYTES, 0, :percpu)
  187. ASSERT(SIZEOF(.data..percpu) < CONFIG_PHYSICAL_START,
  188. "per-CPU data too large - increase CONFIG_PHYSICAL_START")
  189. #endif
  190. INIT_TEXT_SECTION(PAGE_SIZE)
  191. #ifdef CONFIG_X86_64
  192. :init
  193. #endif
  194. /*
  195. * Section for code used exclusively before alternatives are run. All
  196. * references to such code must be patched out by alternatives, normally
  197. * by using X86_FEATURE_ALWAYS CPU feature bit.
  198. *
  199. * See static_cpu_has() for an example.
  200. */
  201. .altinstr_aux : AT(ADDR(.altinstr_aux) - LOAD_OFFSET) {
  202. *(.altinstr_aux)
  203. }
  204. INIT_DATA_SECTION(16)
  205. .x86_cpu_dev.init : AT(ADDR(.x86_cpu_dev.init) - LOAD_OFFSET) {
  206. __x86_cpu_dev_start = .;
  207. *(.x86_cpu_dev.init)
  208. __x86_cpu_dev_end = .;
  209. }
  210. #ifdef CONFIG_X86_INTEL_MID
  211. .x86_intel_mid_dev.init : AT(ADDR(.x86_intel_mid_dev.init) - \
  212. LOAD_OFFSET) {
  213. __x86_intel_mid_dev_start = .;
  214. *(.x86_intel_mid_dev.init)
  215. __x86_intel_mid_dev_end = .;
  216. }
  217. #endif
  218. /*
  219. * start address and size of operations which during runtime
  220. * can be patched with virtualization friendly instructions or
  221. * baremetal native ones. Think page table operations.
  222. * Details in paravirt_types.h
  223. */
  224. . = ALIGN(8);
  225. .parainstructions : AT(ADDR(.parainstructions) - LOAD_OFFSET) {
  226. __parainstructions = .;
  227. *(.parainstructions)
  228. __parainstructions_end = .;
  229. }
  230. /*
  231. * struct alt_inst entries. From the header (alternative.h):
  232. * "Alternative instructions for different CPU types or capabilities"
  233. * Think locking instructions on spinlocks.
  234. */
  235. . = ALIGN(8);
  236. .altinstructions : AT(ADDR(.altinstructions) - LOAD_OFFSET) {
  237. __alt_instructions = .;
  238. *(.altinstructions)
  239. __alt_instructions_end = .;
  240. }
  241. /*
  242. * And here are the replacement instructions. The linker sticks
  243. * them as binary blobs. The .altinstructions has enough data to
  244. * get the address and the length of them to patch the kernel safely.
  245. */
  246. .altinstr_replacement : AT(ADDR(.altinstr_replacement) - LOAD_OFFSET) {
  247. *(.altinstr_replacement)
  248. }
  249. /*
  250. * struct iommu_table_entry entries are injected in this section.
  251. * It is an array of IOMMUs which during run time gets sorted depending
  252. * on its dependency order. After rootfs_initcall is complete
  253. * this section can be safely removed.
  254. */
  255. .iommu_table : AT(ADDR(.iommu_table) - LOAD_OFFSET) {
  256. __iommu_table = .;
  257. *(.iommu_table)
  258. __iommu_table_end = .;
  259. }
  260. . = ALIGN(8);
  261. .apicdrivers : AT(ADDR(.apicdrivers) - LOAD_OFFSET) {
  262. __apicdrivers = .;
  263. *(.apicdrivers);
  264. __apicdrivers_end = .;
  265. }
  266. . = ALIGN(8);
  267. /*
  268. * .exit.text is discarded at runtime, not link time, to deal with
  269. * references from .altinstructions
  270. */
  271. .exit.text : AT(ADDR(.exit.text) - LOAD_OFFSET) {
  272. EXIT_TEXT
  273. }
  274. .exit.data : AT(ADDR(.exit.data) - LOAD_OFFSET) {
  275. EXIT_DATA
  276. }
  277. #if !defined(CONFIG_X86_64) || !defined(CONFIG_SMP)
  278. PERCPU_SECTION(INTERNODE_CACHE_BYTES)
  279. #endif
  280. . = ALIGN(PAGE_SIZE);
  281. /* freed after init ends here */
  282. .init.end : AT(ADDR(.init.end) - LOAD_OFFSET) {
  283. __init_end = .;
  284. }
  285. /*
  286. * smp_locks might be freed after init
  287. * start/end must be page aligned
  288. */
  289. . = ALIGN(PAGE_SIZE);
  290. .smp_locks : AT(ADDR(.smp_locks) - LOAD_OFFSET) {
  291. __smp_locks = .;
  292. *(.smp_locks)
  293. . = ALIGN(PAGE_SIZE);
  294. __smp_locks_end = .;
  295. }
  296. #ifdef CONFIG_X86_64
  297. .data_nosave : AT(ADDR(.data_nosave) - LOAD_OFFSET) {
  298. NOSAVE_DATA
  299. }
  300. #endif
  301. /* BSS */
  302. . = ALIGN(PAGE_SIZE);
  303. .bss : AT(ADDR(.bss) - LOAD_OFFSET) {
  304. __bss_start = .;
  305. *(.bss..page_aligned)
  306. . = ALIGN(PAGE_SIZE);
  307. *(BSS_MAIN)
  308. BSS_DECRYPTED
  309. . = ALIGN(PAGE_SIZE);
  310. __bss_stop = .;
  311. }
  312. /*
  313. * The memory occupied from _text to here, __end_of_kernel_reserve, is
  314. * automatically reserved in setup_arch(). Anything after here must be
  315. * explicitly reserved using memblock_reserve() or it will be discarded
  316. * and treated as available memory.
  317. */
  318. __end_of_kernel_reserve = .;
  319. . = ALIGN(PAGE_SIZE);
  320. .brk : AT(ADDR(.brk) - LOAD_OFFSET) {
  321. __brk_base = .;
  322. . += 64 * 1024; /* 64k alignment slop space */
  323. *(.brk_reservation) /* areas brk users have reserved */
  324. __brk_limit = .;
  325. }
  326. . = ALIGN(PAGE_SIZE); /* keep VO_INIT_SIZE page aligned */
  327. _end = .;
  328. #ifdef CONFIG_AMD_MEM_ENCRYPT
  329. /*
  330. * Early scratch/workarea section: Lives outside of the kernel proper
  331. * (_text - _end).
  332. *
  333. * Resides after _end because even though the .brk section is after
  334. * __end_of_kernel_reserve, the .brk section is later reserved as a
  335. * part of the kernel. Since it is located after __end_of_kernel_reserve
  336. * it will be discarded and become part of the available memory. As
  337. * such, it can only be used by very early boot code and must not be
  338. * needed afterwards.
  339. *
  340. * Currently used by SME for performing in-place encryption of the
  341. * kernel during boot. Resides on a 2MB boundary to simplify the
  342. * pagetable setup used for SME in-place encryption.
  343. */
  344. . = ALIGN(HPAGE_SIZE);
  345. .init.scratch : AT(ADDR(.init.scratch) - LOAD_OFFSET) {
  346. __init_scratch_begin = .;
  347. *(.init.scratch)
  348. . = ALIGN(HPAGE_SIZE);
  349. __init_scratch_end = .;
  350. }
  351. #endif
  352. STABS_DEBUG
  353. DWARF_DEBUG
  354. ELF_DETAILS
  355. DISCARDS
  356. /*
  357. * Make sure that the .got.plt is either completely empty or it
  358. * contains only the lazy dispatch entries.
  359. */
  360. .got.plt (INFO) : { *(.got.plt) }
  361. ASSERT(SIZEOF(.got.plt) == 0 ||
  362. #ifdef CONFIG_X86_64
  363. SIZEOF(.got.plt) == 0x18,
  364. #else
  365. SIZEOF(.got.plt) == 0xc,
  366. #endif
  367. "Unexpected GOT/PLT entries detected!")
  368. /*
  369. * Sections that should stay zero sized, which is safer to
  370. * explicitly check instead of blindly discarding.
  371. */
  372. .got : {
  373. *(.got) *(.igot.*)
  374. }
  375. ASSERT(SIZEOF(.got) == 0, "Unexpected GOT entries detected!")
  376. .plt : {
  377. *(.plt) *(.plt.*) *(.iplt)
  378. }
  379. ASSERT(SIZEOF(.plt) == 0, "Unexpected run-time procedure linkages detected!")
  380. .rel.dyn : {
  381. *(.rel.*) *(.rel_*)
  382. }
  383. ASSERT(SIZEOF(.rel.dyn) == 0, "Unexpected run-time relocations (.rel) detected!")
  384. .rela.dyn : {
  385. *(.rela.*) *(.rela_*)
  386. }
  387. ASSERT(SIZEOF(.rela.dyn) == 0, "Unexpected run-time relocations (.rela) detected!")
  388. }
  389. #ifdef CONFIG_X86_32
  390. /*
  391. * The ASSERT() sink to . is intentional, for binutils 2.14 compatibility:
  392. */
  393. . = ASSERT((_end - LOAD_OFFSET <= KERNEL_IMAGE_SIZE),
  394. "kernel image bigger than KERNEL_IMAGE_SIZE");
  395. #else
  396. /*
  397. * Per-cpu symbols which need to be offset from __per_cpu_load
  398. * for the boot processor.
  399. */
  400. #define INIT_PER_CPU(x) init_per_cpu__##x = ABSOLUTE(x) + __per_cpu_load
  401. INIT_PER_CPU(gdt_page);
  402. INIT_PER_CPU(fixed_percpu_data);
  403. INIT_PER_CPU(irq_stack_backing_store);
  404. /*
  405. * Build-time check on the image size:
  406. */
  407. . = ASSERT((_end - _text <= KERNEL_IMAGE_SIZE),
  408. "kernel image bigger than KERNEL_IMAGE_SIZE");
  409. #ifdef CONFIG_SMP
  410. . = ASSERT((fixed_percpu_data == 0),
  411. "fixed_percpu_data is not at start of per-cpu area");
  412. #endif
  413. #endif /* CONFIG_X86_32 */
  414. #ifdef CONFIG_KEXEC_CORE
  415. #include <asm/kexec.h>
  416. . = ASSERT(kexec_control_code_size <= KEXEC_CONTROL_CODE_MAX_SIZE,
  417. "kexec control code size is too big");
  418. #endif
  419. #ifdef CONFIG_CFI_CLANG
  420. . = ASSERT((__cfi_jt_end - __cfi_jt_start > 0),
  421. "CFI jump table is empty");
  422. #endif