1/*
2 * Copyright 2018 The Hafnium Authors.
3 *
4 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
7 */
8
9/*
10 * Code will start running at this symbol which is places at the start of the
11 * image.
12 */
13ENTRY(entry)
14
15/*
16 * The following would be useful to check that .init code is not called back
17 * into once it has completed but it isn't supported by ld.lld.
18 *
19 * NOCROSSREFS_TO(.init .text)
20 */
21
22SECTIONS
23{
24	/*
25	 * Set the image origin to a platform specific address. The images are
26	 * relocatable but some platforms, e.g. QEMU, load to the same address
27	 * and it makes debugging easier if the addresses match the symbols.
28	 */
29	. = ORIGIN_ADDRESS;
30
31	/*
32	 * Collect together the code. This is page aligned so it can be mapped
33	 * as executable-only.
34	 */
35	text_begin = .;
36	.init : {
37		*(.init.entry)
38		*(.init.*)
39	}
40	.text : {
41		*(.text.*)
42	}
43	text_size = ABSOLUTE(. - text_begin);
44	. = ALIGN(4096);
45	text_end = .;
46
47	/*
48	 * Collect together read-only data including relocations at the end
49	 * which are applied by the entry code. This is page aligned so it can
50	 * be mapped as read-only and non-executable.
51	 */
52	. = ALIGN(4096);
53	rodata_begin = .;
54	.rodata : {
55		*(.rodata.*)
56	}
57	/*
58	 * .rela contains Elf64_Rela entries which contain 8-byte fields so
59	 * should be 8-byte aligned.
60	 */
61	. = ALIGN(8);
62	rela_begin = .;
63	.rela : {
64		*(.rela.*)
65	}
66	rela_end = .;
67	/*
68	 * The linker doesn't allow .dynsym and .dynstr to be discarded, see
69	 * /DISCARD/ below, so make sure they don't get in the way.
70	 */
71	.dynsym : {
72		*(.dynsym.*)
73	}
74	.dynstr : {
75		*(.dynstr.*)
76	}
77	/*
78	 * The hftest framework adds test descriptors in the .hftest section
79	 * which is examined at runtime to discover the available tests. The
80	 * input sections are named after the test they include so sorting here
81	 * means they are stored sorted by the name of the test suite and then
82	 * by test case names. To ensure tests aren't accidentally included in
83	 * images that are not meant to have them, the assertion checks for a
84	 * marker to signal tests are allowed.
85	 */
86	. = ALIGN(8);
87	hftest_begin = .;
88	.hftest : {
89		KEEP(*(SORT(.hftest.*)))
90	}
91	hftest_end = .;
92	ASSERT((SIZEOF(.hftest) == (DEFINED(hftest_enable) ? SIZEOF(.hftest) : 0)),
93	       "Error: Image includes .hftest section but not HFTEST_ENABLE().")
94	rodata_size = ABSOLUTE(. - rodata_begin);
95	. = ALIGN(4096);
96	rodata_end = .;
97
98	/*
99	 * A platform may choose to link blobs such as the FDT or the initrd
100	 * into the image rather than having them loaded separately. These are
101	 * placed at the end of the image and will not be mapped automatically
102	 * on boot so they can be treated as if they were loaded as separate
103	 * blobs. They are page aligned so they can be mapped individually.
104	 *
105	 * TODO: remove this when the loader can reliably deliver both the
106	 * binary and a separate blob for the initrd.
107	 */
108	. = ALIGN(4096);
109	initrd_begin = .;
110	.initrd : {
111		KEEP(*(.plat.initrd))
112	}
113	initrd_end = .;
114	. = ALIGN(4096);
115	fdt_begin = .;
116	.fdt : {
117		KEEP(*(.plat.fdt))
118	}
119	fdt_end = .;
120
121	/*
122	 * Collect together the read-write data including .bss at the end which
123	 * will be zero'd by the entry code. This is page aligned so it can be
124	 * mapped as non-executable.
125	 */
126	. = ALIGN(4096);
127	data_begin = .;
128	.data : {
129		*(.data*)
130	}
131	/*
132	 * Global offset table used for relocations. This is where relocation
133	 * fix-ups are applied.
134	 */
135	.got : {
136		*(.got.*)
137	}
138	/*
139	 * The linker doesn't allow .dynamic to be discarded, see /DISCARD/
140	 * below, so make sure it doesn't get in the way.
141	 */
142	.dynamic : {
143		*(.dynamic.*)
144	}
145	/* Everything beyond this point will not be included in the binary. */
146	bin_end = .;
147	/* The entry point code assumes that .bss is 16-byte aligned. */
148	. = ALIGN(16);
149	bss_begin = .;
150	.bss : {
151		*(.bss*)
152		*(COMMON)
153	}
154	. = ALIGN(16);
155	bss_end = .;
156	data_size = ABSOLUTE(. - data_begin);
157	. = ALIGN(4096);
158	data_end = .;
159
160	.stacks (NOLOAD) : {
161		stacks_begin = .;
162		*(.stacks)
163		stacks_end = .;
164	}
165
166	/*
167	 * Remove unused sections from the image.
168	 */
169	/DISCARD/ : {
170		/* The image loads itself so doesn't need these sections. */
171		/* ld.lld doesn't allow these to be discarded.
172		*(.dynsym)
173		*(.dynstr)
174		*(.dynamic)
175		*/
176		*(.gnu.hash)
177		*(.hash)
178		*(.interp)
179	}
180
181	/*
182	 * Make note of some useful values.
183	 */
184
185	/* Note the first page not used in the image. */
186	. = ALIGN(4096);
187	image_end = .;
188
189	/*
190	 * Calculate sizes of the binary file and image loaded into memory as
191	 * well as the text, read-only and read-write data sections.
192	 */
193	bin_size = ABSOLUTE(bin_end - ORIGIN_ADDRESS);
194	image_size = ABSOLUTE(image_end - ORIGIN_ADDRESS);
195}
196