1/* Excerpts written by Martin Mares <mj@atrey.karlin.mff.cuni.cz> */
2/* Modified for i386/x86-64 Xen by Keir Fraser */
3
4#include <xen/cache.h>
5#include <xen/lib.h>
6#include <xen/xen.lds.h>
7#include <asm/page.h>
8#undef ENTRY
9#undef ALIGN
10
11#ifdef EFI
12
13#define FORMAT "pei-x86-64"
14#undef __XEN_VIRT_START
15#undef DECL_SECTION
16#define __XEN_VIRT_START __image_base__
17#define DECL_SECTION(x) x :
18
19ENTRY(efi_start)
20
21#else /* !EFI */
22
23#define FORMAT "elf64-x86-64"
24
25ENTRY(start_pa)
26
27#endif /* EFI */
28
29#if defined(CONFIG_XEN_ALIGN_2M) || defined(EFI)
30# define SECTION_ALIGN MB(2)
31#else
32# define SECTION_ALIGN PAGE_SIZE
33#endif
34
35#ifndef HAVE_LD_SORT_BY_INIT_PRIORITY
36# define SORT_BY_INIT_PRIORITY SORT
37#endif
38
39OUTPUT_FORMAT(FORMAT, FORMAT, FORMAT)
40
41OUTPUT_ARCH(i386:x86-64)
42
43#ifndef EFI
44PHDRS
45{
46  text PT_LOAD ;
47  note PT_NOTE ;
48}
49#define PHDR(x) :x
50#else
51#define PHDR(x)
52#endif
53
54SECTIONS
55{
56#if !defined(EFI)
57  . = __XEN_VIRT_START;
58  __image_base__ = .;
59#else
60  . = __image_base__;
61#endif
62
63#if 0
64/*
65 * We don't really use this symbol anywhere, and the way it would get defined
66 * here would result in it having a negative (wrapped to huge positive)
67 * offset relative to the .text section. That, in turn, causes an assembler
68 * truncation warning when including all symbols in the symbol table for Live
69 * Patching code.
70 */
71  __2M_text_start = .;         /* Start of 2M superpages, mapped RX. */
72#endif
73
74  start_pa = ABSOLUTE(start - __XEN_VIRT_START);
75
76  . = __XEN_VIRT_START + XEN_IMG_OFFSET;
77  _start = .;
78  DECL_SECTION(.text) {
79        _stext = .;            /* Text and read-only data */
80       *(.text.header)
81
82       . = ALIGN(PAGE_SIZE);
83       _stextentry = .;
84       *(.text.entry)
85       . = ALIGN(PAGE_SIZE);
86       _etextentry = .;
87
88       *(.text.kexec)          /* Page aligned in the object file. */
89       kexec_reloc_end = .;
90
91       *(.text.cold)
92       *(.text.unlikely .text.*_unlikely .text.unlikely.*)
93
94       *(.text.page_aligned)
95       *(.text)
96#ifdef CONFIG_CC_SPLIT_SECTIONS
97       *(.text.*)
98#endif
99       *(.text.__x86_indirect_thunk_*)
100
101       *(.fixup)
102       *(.gnu.warning)
103       _etext = .;             /* End of text section */
104  } PHDR(text) = 0x9090
105
106  . = ALIGN(SECTION_ALIGN);
107  __2M_text_end = .;
108
109  __2M_rodata_start = .;       /* Start of 2M superpages, mapped RO. */
110  DECL_SECTION(.rodata) {
111       _srodata = .;
112
113       __ro_after_init_start = .;
114       *(.data.ro_after_init)
115
116       . = ALIGN(8);
117       /* Exception table */
118       __start___ex_table = .;
119       *(.ex_table)
120       __stop___ex_table = .;
121
122       /* Pre-exception table */
123       __start___pre_ex_table = .;
124       *(.ex_table.pre)
125       __stop___pre_ex_table = .;
126
127       . = ALIGN(PAGE_SIZE);
128       __ro_after_init_end = .;
129
130       BUGFRAMES
131
132       *(.rodata)
133       *(.rodata.*)
134       *(.data.rel.ro)
135       *(.data.rel.ro.*)
136
137#if defined(BUILD_ID) && defined(EFI) && !defined(BUILD_ID_EFI)
138/*
139 * No mechanism to put an PT_NOTE in the EFI file - so put
140 * it in .rodata section. (notes.o supplies us with .note.gnu.build-id).
141 */
142       . = ALIGN(4);
143       __note_gnu_build_id_start = .;
144       *(.note.gnu.build-id)
145       __note_gnu_build_id_end = .;
146#endif
147       VPCI_ARRAY
148  } PHDR(text)
149
150#if defined(CONFIG_PVH_GUEST) && !defined(EFI)
151  /*
152   * In principle this should be fine to live in .note (below), but let's keep
153   * it separate in case anyone decided to find these notes by section name.
154   */
155  DECL_SECTION(.note.Xen) {
156      *(.note.Xen)
157  } PHDR(note) PHDR(text)
158#endif
159
160#if defined(BUILD_ID)
161#if !defined(EFI)
162/*
163 * What a strange section name. The reason is that on ELF builds this section
164 * is extracted to notes.o (which then is ingested in the EFI file). But the
165 * compiler may want to inject other things in the .note which we don't care
166 * about - hence this unique name.
167 */
168  DECL_SECTION(.note.gnu.build-id) {
169       __note_gnu_build_id_start = .;
170       *(.note.gnu.build-id)
171       __note_gnu_build_id_end = .;
172  } PHDR(note) PHDR(text)
173#elif defined(BUILD_ID_EFI)
174  DECL_SECTION(.buildid) {
175       __note_gnu_build_id_start = .;
176       *(.buildid)
177       __note_gnu_build_id_end = .;
178  }
179#endif
180#endif
181
182#ifndef EFI
183  /* Retain these just for the purpose of possible analysis tools. */
184  DECL_SECTION(.note) {
185       *(.note.*)
186  } PHDR(note) PHDR(text)
187#endif
188
189  _erodata = .;
190
191  . = ALIGN(SECTION_ALIGN);
192  __2M_rodata_end = .;
193
194  __2M_init_start = .;         /* Start of 2M superpages, mapped RWX (boot only). */
195  . = ALIGN(PAGE_SIZE);             /* Init code and data */
196  __init_begin = .;
197#ifdef EFI /* EFI wants to merge all of .init.*  ELF doesn't. */
198  DECL_SECTION(.init) {
199#else
200  DECL_SECTION(.init.text) {
201#endif
202       _sinittext = .;
203       *(.init.text)
204       *(.text.startup)
205       _einittext = .;
206       /*
207        * Here are the replacement instructions. The linker sticks them
208        * as binary blobs. The .altinstructions has enough data to get
209        * the address and the length of them to patch the kernel safely.
210        */
211       *(.altinstr_replacement)
212
213#ifdef EFI /* EFI wants to merge all of .init.*  ELF doesn't. */
214       . = ALIGN(SMP_CACHE_BYTES);
215#else
216  } PHDR(text)
217  DECL_SECTION(.init.data) {
218#endif
219       *(.init.bss.stack_aligned)
220
221       . = ALIGN(POINTER_ALIGN);
222       __initdata_cf_clobber_start = .;
223       *(.init.data.cf_clobber)
224       *(.init.rodata.cf_clobber)
225       __initdata_cf_clobber_end = .;
226
227       *(.init.rodata)
228       *(.init.rodata.*)
229
230       . = ALIGN(POINTER_ALIGN);
231       __setup_start = .;
232       *(.init.setup)
233       __setup_end = .;
234
235       __initcall_start = .;
236       *(.initcallpresmp.init)
237       __presmp_initcall_end = .;
238       *(.initcall1.init)
239       __initcall_end = .;
240
241       *(.init.data)
242       *(.init.data.rel)
243       *(.init.data.rel.*)
244       . = ALIGN(4);
245       __trampoline_rel_start = .;
246       *(.trampoline_rel)
247       __trampoline_rel_stop = .;
248       __trampoline_seg_start = .;
249       *(.trampoline_seg)
250       __trampoline_seg_stop = .;
251       /*
252        * struct alt_inst entries. From the header (alternative.h):
253        * "Alternative instructions for different CPU types or capabilities"
254        * Think locking instructions on spinlocks.
255        */
256       . = ALIGN(8);
257        __alt_instructions = .;
258        *(.altinstructions)
259        __alt_instructions_end = .;
260
261       LOCK_PROFILE_DATA
262
263       . = ALIGN(8);
264       __ctors_start = .;
265       *(SORT_BY_INIT_PRIORITY(.init_array.*))
266       *(SORT_BY_INIT_PRIORITY(.ctors.*))
267       *(.init_array)
268       *(.ctors)
269       __ctors_end = .;
270  } PHDR(text)
271
272#ifndef EFI
273  /*
274   * With --orphan-sections=warn (or =error) we need to handle certain linker
275   * generated sections.  These are all expected to be empty; respective
276   * ASSERT()s can be found towards the end of this file.  Put them in the
277   * text program header, just to be on the safe side against e.g. a linker
278   * otherwise chosing to put them in a separate one.
279   */
280  DECL_SECTION(.got) {
281       *(.got)
282  } PHDR(text)
283  DECL_SECTION(.got.plt) {
284       *(.got.plt)
285  } PHDR(text)
286  DECL_SECTION(.igot.plt) {
287       *(.igot.plt)
288  } PHDR(text)
289  DECL_SECTION(.iplt) {
290       *(.iplt)
291  } PHDR(text)
292  DECL_SECTION(.plt) {
293       *(.plt)
294  } PHDR(text)
295  DECL_SECTION(.rela) {
296       *(.rela.*)
297  } PHDR(text)
298#endif
299
300  . = ALIGN(SECTION_ALIGN);
301  __init_end = .;
302  __2M_init_end = .;
303
304  __2M_rwdata_start = .;       /* Start of 2M superpages, mapped RW. */
305  . = ALIGN(SMP_CACHE_BYTES);
306  DECL_SECTION(.data.read_mostly) {
307       *(.data.read_mostly)
308       . = ALIGN(8);
309       __start_schedulers_array = .;
310       *(.data.schedulers)
311       __end_schedulers_array = .;
312
313       HYPFS_PARAM
314  } PHDR(text)
315
316  DECL_SECTION(.data) {
317       *(.data.page_aligned)
318       *(.data .data.*)
319  } PHDR(text)
320
321  DECL_SECTION(.bss) {
322       __bss_start = .;
323       *(.bss.page_aligned*)
324       . = ALIGN(PAGE_SIZE);
325       __per_cpu_start = .;
326       *(.bss.percpu.page_aligned)
327       *(.bss.percpu)
328       . = ALIGN(SMP_CACHE_BYTES);
329       *(.bss.percpu.read_mostly)
330       . = ALIGN(SMP_CACHE_BYTES);
331       __per_cpu_data_end = .;
332       *(.bss .bss.*)
333       . = ALIGN(POINTER_ALIGN);
334       __bss_end = .;
335  } PHDR(text)
336  _end = . ;
337
338  __2M_rwdata_end = ALIGN(SECTION_ALIGN);
339
340#ifdef EFI
341  .reloc ALIGN(4) : {
342    __base_relocs_start = .;
343    *(.reloc)
344    __base_relocs_end = .;
345  }
346#elif defined(XEN_BUILD_EFI)
347  /*
348   * Due to the way EFI support is currently implemented, these two symbols
349   * need to be defined.  Their precise values shouldn't matter (the consuming
350   * function doesn't get called), but to be on the safe side both values would
351   * better match.  Of course the need to be reachable by the relocations
352   * referencing them.
353   */
354  PROVIDE(__base_relocs_start = .);
355  PROVIDE(__base_relocs_end = .);
356#else
357  efi = .;
358#endif
359
360  DWARF2_DEBUG_SECTIONS
361
362#ifdef EFI
363  /* Trick the linker into setting the image size to no less than 16Mb. */
364  __image_end__ = .;
365  .pad ALIGN(__section_alignment__) : {
366    . = __image_end__ < __image_base__ + MB(16) ? ALIGN(MB(16)) : .;
367  }
368#endif
369
370#ifdef CONFIG_HYPERV_GUEST
371  hv_hcall_page = ABSOLUTE(HV_HCALL_PAGE - XEN_VIRT_START + __XEN_VIRT_START);
372#endif
373
374  DISCARD_SECTIONS
375
376#ifndef EFI
377  STABS_DEBUG_SECTIONS
378
379  ELF_DETAILS_SECTIONS
380#endif
381}
382
383ASSERT(__2M_rwdata_end <= XEN_VIRT_END - XEN_VIRT_START + __XEN_VIRT_START -
384                          FIXADDR_X_SIZE -
385                          NR_CPUS * PAGE_SIZE,
386       "Xen image overlaps stubs area")
387
388#ifdef CONFIG_KEXEC
389ASSERT(kexec_reloc_end - kexec_reloc <= PAGE_SIZE, "kexec_reloc is too large")
390#endif
391
392/* The Multiboot setup paths relies on this to simplify superpage PTE creation. */
393ASSERT(IS_ALIGNED(_start,            MB(2)), "_start misaligned")
394
395ASSERT(IS_ALIGNED(__2M_text_end,     SECTION_ALIGN), "__2M_text_end misaligned")
396ASSERT(IS_ALIGNED(__2M_rodata_start, SECTION_ALIGN), "__2M_rodata_start misaligned")
397ASSERT(IS_ALIGNED(__2M_rodata_end,   SECTION_ALIGN), "__2M_rodata_end misaligned")
398ASSERT(IS_ALIGNED(__2M_init_start,   SECTION_ALIGN), "__2M_init_start misaligned")
399ASSERT(IS_ALIGNED(__2M_init_end,     SECTION_ALIGN), "__2M_init_end misaligned")
400ASSERT(IS_ALIGNED(__2M_rwdata_start, SECTION_ALIGN), "__2M_rwdata_start misaligned")
401ASSERT(IS_ALIGNED(__2M_rwdata_end,   SECTION_ALIGN), "__2M_rwdata_end misaligned")
402
403ASSERT(IS_ALIGNED(cpu0_stack, STACK_SIZE), "cpu0_stack misaligned")
404
405ASSERT(IS_ALIGNED(__init_begin, PAGE_SIZE), "__init_begin misaligned")
406ASSERT(IS_ALIGNED(__init_end,   PAGE_SIZE), "__init_end misaligned")
407
408ASSERT(IS_ALIGNED(trampoline_start, 4), "trampoline_start misaligned")
409ASSERT(IS_ALIGNED(trampoline_end,   4), "trampoline_end misaligned")
410ASSERT(IS_ALIGNED(__bss_start,      8), "__bss_start misaligned")
411ASSERT(IS_ALIGNED(__bss_end,        8), "__bss_end misaligned")
412
413#ifndef EFI
414ASSERT(!SIZEOF(.got),      ".got non-empty")
415/*
416 * At least GNU ld 2.30 and earlier fail to discard the generic part of
417 * .got.plt when no actual entries were allocated. Permit this case alongside
418 * the section being empty.
419 */
420ASSERT(!SIZEOF(.got.plt) || SIZEOF(.got.plt) == 3 * 8,
421       "unexpected .got.plt size")
422ASSERT(!SIZEOF(.igot.plt), ".igot.plt non-empty")
423ASSERT(!SIZEOF(.iplt),     ".iplt non-empty")
424ASSERT(!SIZEOF(.plt),      ".plt non-empty")
425ASSERT(!SIZEOF(.rela),     "leftover relocations")
426#endif
427
428ASSERT((trampoline_end - trampoline_start) < TRAMPOLINE_SPACE - MBI_SPACE_MIN,
429    "not enough room for trampoline and mbi data")
430ASSERT((wakeup_stack - wakeup_stack_start) >= WAKEUP_STACK_MIN,
431    "wakeup stack too small")
432
433/* Plenty of boot code assumes that Xen isn't larger than 16M. */
434ASSERT(_end - _start <= MB(16), "Xen too large for early-boot assumptions")
435