xref: /optee_os/core/arch/arm/kernel/kern.ld.S (revision 0014a941f0d7d8bd61fbe6dd9977b902529bf803)
1/*
2 * Copyright (c) 2014, Linaro Limited
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28/*
29 * Copyright (c) 2008-2010 Travis Geiselbrecht
30 *
31 * Permission is hereby granted, free of charge, to any person obtaining
32 * a copy of this software and associated documentation files
33 * (the "Software"), to deal in the Software without restriction,
34 * including without limitation the rights to use, copy, modify, merge,
35 * publish, distribute, sublicense, and/or sell copies of the Software,
36 * and to permit persons to whom the Software is furnished to do so,
37 * subject to the following conditions:
38 *
39 * The above copyright notice and this permission notice shall be
40 * included in all copies or substantial portions of the Software.
41 *
42 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
43 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
46 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
47 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
48 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
49 */
50
51#include <platform_config.h>
52
53#ifndef SMALL_PAGE_SIZE
54#define SMALL_PAGE_SIZE		4096
55#endif
56#ifndef ROUNDUP
57#define ROUNDUP(x, y)		((((x) + (y) - 1) / (y)) * (y))
58#define ROUNDDOWN(x, y)		(((x) / (y)) * (y))
59#endif
60
61OUTPUT_FORMAT(CFG_KERN_LINKER_FORMAT)
62OUTPUT_ARCH(CFG_KERN_LINKER_ARCH)
63
64ENTRY(_start)
65SECTIONS
66{
67	. = CFG_TEE_LOAD_ADDR;
68
69	__text_start = .;
70
71	/*
72	 * Memory between CFG_TEE_LOAD_ADDR and page aligned rounded down
73	 * value will be mapped with unpaged "text" section attributes:
74	 * likely to be read-only/executable.
75	 */
76	__flatmap_unpg_rx_start = ROUNDDOWN(__text_start, SMALL_PAGE_SIZE);
77
78	.text : {
79		KEEP(*(.text.boot.vectab1))
80		KEEP(*(.text.boot.vectab2))
81		KEEP(*(.text.boot))
82
83		. = ALIGN(8);
84		__initcall_start = .;
85		KEEP(*(.initcall1))
86		KEEP(*(.initcall2))
87		KEEP(*(.initcall3))
88		KEEP(*(.initcall4))
89		__initcall_end = .;
90
91#ifdef CFG_WITH_PAGER
92		*(.text)
93/* Include list of sections needed for paging */
94#include <text_unpaged.ld.S>
95#else
96		*(.text .text.*)
97#endif
98		*(.sram.text.glue_7* .gnu.linkonce.t.*)
99		. = ALIGN(8);
100	}
101	__text_end = .;
102
103#ifdef CFG_CORE_RODATA_NOEXEC
104	. = ALIGN(SMALL_PAGE_SIZE);
105#endif
106	__flatmap_unpg_rx_size = . - __flatmap_unpg_rx_start;
107	__flatmap_unpg_ro_start = .;
108
109	.rodata : ALIGN(8) {
110		__rodata_start = .;
111		*(.gnu.linkonce.r.*)
112#ifdef CFG_WITH_PAGER
113		*(.rodata .rodata.__unpaged)
114#include <rodata_unpaged.ld.S>
115#else
116#ifdef CFG_DT
117		__rodata_dtdrv_start = .;
118		KEEP(*(.rodata.dtdrv))
119		__rodata_dtdrv_end = .;
120#endif
121		*(.rodata .rodata.*)
122
123		/*
124		 * 8 to avoid unwanted padding between __start_ta_head_section
125		 * and the first structure in ta_head_section, in 64-bit
126		 * builds
127		 */
128		. = ALIGN(8);
129		__start_ta_head_section = . ;
130		KEEP(*(ta_head_section))
131		__stop_ta_head_section = . ;
132		. = ALIGN(8);
133		__start_phys_mem_map_section = . ;
134		KEEP(*(phys_mem_map_section))
135		__end_phys_mem_map_section = . ;
136		. = ALIGN(8);
137		__start_phys_sdp_mem_section = . ;
138		KEEP(*(phys_sdp_mem_section))
139		__end_phys_sdp_mem_section = . ;
140		. = ALIGN(8);
141		__start_phys_nsec_ddr_section = . ;
142		KEEP(*(phys_nsec_ddr_section))
143		__end_phys_nsec_ddr_section = . ;
144#endif
145		. = ALIGN(8);
146		__rodata_end = .;
147	}
148
149	.interp : { *(.interp) }
150	.hash : { *(.hash) }
151	.dynsym : { *(.dynsym) }
152	.dynstr : { *(.dynstr) }
153	.rel.text : { *(.rel.text) *(.rel.gnu.linkonce.t*) }
154	.rela.text : { *(.rela.text) *(.rela.gnu.linkonce.t*) }
155	.rel.data : { *(.rel.data) *(.rel.gnu.linkonce.d*) }
156	.rela.data : { *(.rela.data) *(.rela.gnu.linkonce.d*) }
157	.rel.rodata : { *(.rel.rodata) *(.rel.gnu.linkonce.r*) }
158	.rela.rodata : { *(.rela.rodata) *(.rela.gnu.linkonce.r*) }
159	.rel.got : { *(.rel.got) }
160	.rela.got : { *(.rela.got) }
161	.rel.ctors : { *(.rel.ctors) }
162	.rela.ctors : { *(.rela.ctors) }
163	.rel.dtors : { *(.rel.dtors) }
164	.rela.dtors : { *(.rela.dtors) }
165	.rel.init : { *(.rel.init) }
166	.rela.init : { *(.rela.init) }
167	.rel.fini : { *(.rel.fini) }
168	.rela.fini : { *(.rela.fini) }
169	.rel.bss : { *(.rel.bss) }
170	.rela.bss : { *(.rela.bss) }
171	.rel.plt : { *(.rel.plt) }
172	.rela.plt : { *(.rela.plt) }
173	.init : { *(.init) } =0x9090
174	.plt : { *(.plt) }
175
176	/* .ARM.exidx is sorted, so has to go in its own output section.  */
177	.ARM.exidx : {
178		__exidx_start = .;
179		*(.ARM.exidx* .gnu.linkonce.armexidx.*)
180		__exidx_end = .;
181	}
182
183	.ARM.extab : {
184		__extab_start = .;
185		*(.ARM.extab*)
186		__extab_end = .;
187	}
188
189	/* Start page aligned read-write memory */
190#ifdef CFG_CORE_RWDATA_NOEXEC
191	. = ALIGN(SMALL_PAGE_SIZE);
192#endif
193	__flatmap_unpg_ro_size = . - __flatmap_unpg_ro_start;
194	__flatmap_unpg_rw_start = .;
195
196	.data : ALIGN(8) {
197		/* writable data  */
198		__data_start_rom = .;
199		/* in one segment binaries, the rom data address is on top
200		   of the ram data address */
201		__data_start = .;
202		*(.data .data.* .gnu.linkonce.d.*)
203		. = ALIGN(8);
204	}
205
206	.ctors : ALIGN(8) {
207		__ctor_list = .;
208		KEEP(*(.ctors .ctors.* .init_array .init_array.*))
209		__ctor_end = .;
210	}
211	.dtors : ALIGN(8) {
212		__dtor_list = .;
213		KEEP(*(.dtors .dtors.* .fini_array .fini_array.*))
214		__dtor_end = .;
215	}
216	.got : { *(.got.plt) *(.got) }
217	.dynamic : { *(.dynamic) }
218
219	/* unintialized data */
220	.bss : {
221		__data_end = .;
222		__bss_start = .;
223		*(.bss .bss.*)
224		*(.gnu.linkonce.b.*)
225		*(COMMON)
226		. = ALIGN(8);
227		__bss_end = .;
228	}
229
230	.heap1 (NOLOAD) : {
231		/*
232		 * We're keeping track of the padding added before the
233		 * .nozi section so we can do something useful with
234		 * this otherwise wasted memory.
235		 */
236		__heap1_start = .;
237#ifndef CFG_WITH_PAGER
238		. += CFG_CORE_HEAP_SIZE;
239#endif
240		. = ALIGN(16 * 1024);
241		__heap1_end = .;
242	}
243
244	/*
245	 * Uninitialized data that shouldn't be zero initialized at
246	 * runtime.
247	 *
248	 * L1 mmu table requires 16 KiB alignment
249	 */
250	.nozi (NOLOAD) : {
251		__nozi_start = .;
252		ASSERT(!(__nozi_start & (16 * 1024 - 1)), "align nozi to 16kB");
253
254		KEEP(*(.nozi .nozi.*))
255		. = ALIGN(16);
256		__nozi_end = .;
257		__nozi_stack_start = .;
258		KEEP(*(.nozi_stack))
259		. = ALIGN(8);
260		__nozi_stack_end = .;
261	}
262
263#ifdef CFG_WITH_PAGER
264	.heap2 (NOLOAD) : {
265		__heap2_start = .;
266		/*
267		 * Reserve additional memory for heap, the total should be
268		 * at least CFG_CORE_HEAP_SIZE, but count what has already
269		 * been reserved in .heap1
270		 */
271		. += CFG_CORE_HEAP_SIZE - (__heap1_end - __heap1_start);
272		. = ALIGN(SMALL_PAGE_SIZE);
273		__heap2_end = .;
274	}
275
276	/* Start page aligned read-only memory */
277	__flatmap_unpg_rw_size = . - __flatmap_unpg_rw_start;
278
279	__init_start = .;
280	__flatmap_init_rx_start = .;
281
282	ASSERT(!(__flatmap_init_rx_start & (SMALL_PAGE_SIZE - 1)),
283		"read-write memory is not paged aligned")
284
285	.text_init : {
286/*
287 * Include list of sections needed for boot initialization, this list
288 * overlaps with unpaged.ld.S but since unpaged.ld.S is first all those
289 * sections will go into the unpaged area.
290 */
291#include <text_init.ld.S>
292		. = ALIGN(8);
293	}
294
295#ifdef CFG_CORE_RODATA_NOEXEC
296	. = ALIGN(SMALL_PAGE_SIZE);
297#endif
298	__flatmap_init_rx_size = . - __flatmap_init_rx_start;
299	__flatmap_init_ro_start = .;
300
301	.rodata_init : {
302#include <rodata_init.ld.S>
303		. = ALIGN(8);
304		__start_phys_mem_map_section = . ;
305		KEEP(*(phys_mem_map_section))
306		__end_phys_mem_map_section = . ;
307		. = ALIGN(8);
308		__start_phys_sdp_mem_section = . ;
309		KEEP(*(phys_sdp_mem_section))
310		__end_phys_sdp_mem_section = . ;
311		. = ALIGN(8);
312		__start_phys_nsec_ddr_section = . ;
313		KEEP(*(phys_nsec_ddr_section))
314		__end_phys_nsec_ddr_section = . ;
315		. = ALIGN(8);
316		__rodata_init_end = .;
317	}
318	__rodata_init_end = .;
319
320	__init_end = ROUNDUP(__rodata_init_end, SMALL_PAGE_SIZE);
321	__init_size = __init_end - __init_start;
322
323	/* vcore flat map stops here. No need to page align, rodata follows. */
324	__flatmap_init_ro_size = __init_end - __flatmap_init_ro_start;
325
326	.rodata_pageable : ALIGN(8) {
327#ifdef CFG_DT
328		__rodata_dtdrv_start = .;
329		KEEP(*(.rodata.dtdrv))
330		__rodata_dtdrv_end = .;
331#endif
332		*(.rodata*)
333		/*
334		 * 8 to avoid unwanted padding between __start_ta_head_section
335		 * and the first structure in ta_head_section, in 64-bit
336		 * builds
337		 */
338		. = ALIGN(8);
339		__start_ta_head_section = . ;
340		KEEP(*(ta_head_section))
341		__stop_ta_head_section = . ;
342	}
343
344#ifdef CFG_CORE_RODATA_NOEXEC
345	. = ALIGN(SMALL_PAGE_SIZE);
346#endif
347
348	.text_pageable : ALIGN(8) {
349		*(.text*)
350		. = ALIGN(SMALL_PAGE_SIZE);
351	}
352
353	__pageable_part_end = .;
354	__pageable_part_start = __init_end;
355	__pageable_start = __init_start;
356	__pageable_end = __pageable_part_end;
357
358	/*
359	 * Assign a safe spot to store the hashes of the pages before
360	 * heap is initialized.
361	 */
362	__tmp_hashes_start = __init_end;
363	__tmp_hashes_size = ((__pageable_end - __pageable_start) /
364				SMALL_PAGE_SIZE) * 32;
365	__tmp_hashes_end = __tmp_hashes_start + __tmp_hashes_size;
366
367	__init_mem_usage = __tmp_hashes_end - CFG_TEE_LOAD_ADDR;
368
369	ASSERT(CFG_TEE_LOAD_ADDR >= CFG_TEE_RAM_START,
370		"Load address before start of physical memory")
371	ASSERT(CFG_TEE_LOAD_ADDR < (CFG_TEE_RAM_START + CFG_TEE_RAM_PH_SIZE),
372		"Load address after end of physical memory")
373	ASSERT(__tmp_hashes_end < (CFG_TEE_RAM_START + CFG_TEE_RAM_PH_SIZE),
374		"OP-TEE can't fit init part into available physical memory")
375	ASSERT((CFG_TEE_RAM_START + CFG_TEE_RAM_PH_SIZE - __init_end) >
376		SMALL_PAGE_SIZE, "Too few free pages to initialize paging")
377
378
379#endif /*CFG_WITH_PAGER*/
380
381#ifdef CFG_CORE_SANITIZE_KADDRESS
382	. = CFG_TEE_RAM_START + (CFG_TEE_RAM_VA_SIZE * 8) / 9 - 8;
383	. = ALIGN(8);
384	.asan_shadow : {
385		__asan_shadow_start = .;
386		. += CFG_TEE_RAM_VA_SIZE / 9;
387		__asan_shadow_end = .;
388	}
389#endif /*CFG_CORE_SANITIZE_KADDRESS*/
390
391	__end = .;
392
393#ifndef CFG_WITH_PAGER
394	__init_size = __data_end - CFG_TEE_LOAD_ADDR;
395	__init_mem_usage = __end - CFG_TEE_LOAD_ADDR;
396#endif
397	. = CFG_TEE_RAM_START + CFG_TEE_RAM_VA_SIZE;
398
399	_end_of_ram = .;
400
401#ifndef CFG_WITH_PAGER
402	__flatmap_unpg_rw_size = _end_of_ram - __flatmap_unpg_rw_start;
403#endif
404
405	/DISCARD/ : {
406		/* Strip unnecessary stuff */
407		*(.comment .note .eh_frame)
408		/* Strip meta variables */
409		*(__keep_meta_vars*)
410	}
411
412}
413
414/* Unpaged read-only memories */
415PROVIDE(__vcore_unpg_rx_start = __flatmap_unpg_rx_start);
416PROVIDE(__vcore_unpg_ro_start = __flatmap_unpg_ro_start);
417#ifdef CFG_CORE_RODATA_NOEXEC
418PROVIDE(__vcore_unpg_rx_size = __flatmap_unpg_rx_size);
419PROVIDE(__vcore_unpg_ro_size = __flatmap_unpg_ro_size);
420#else
421PROVIDE(__vcore_unpg_rx_size = __flatmap_unpg_rx_size +
422				__flatmap_unpg_ro_size);
423PROVIDE(__vcore_unpg_ro_size = 0);
424#endif
425
426/* Unpaged read-write memory */
427PROVIDE(__vcore_unpg_rw_start = __flatmap_unpg_rw_start);
428PROVIDE(__vcore_unpg_rw_size = __flatmap_unpg_rw_size);
429
430#ifdef CFG_WITH_PAGER
431/*
432 * Core init mapping shall cover up to end of the physical RAM.
433 * This is required since the hash table is appended to the
434 * binary data after the firmware build sequence.
435 */
436#define __FLATMAP_PAGER_TRAILING_SPACE	\
437	(CFG_TEE_RAM_START + CFG_TEE_RAM_PH_SIZE - \
438		(__flatmap_init_ro_start + __flatmap_init_ro_size))
439
440/* Paged/init read-only memories */
441PROVIDE(__vcore_init_rx_start = __flatmap_init_rx_start);
442PROVIDE(__vcore_init_ro_start = __flatmap_init_ro_start);
443#ifdef CFG_CORE_RODATA_NOEXEC
444PROVIDE(__vcore_init_rx_size = __flatmap_init_rx_size);
445PROVIDE(__vcore_init_ro_size = __flatmap_init_ro_size +
446				__FLATMAP_PAGER_TRAILING_SPACE);
447#else
448PROVIDE(__vcore_init_rx_size = __flatmap_init_rx_size +
449				__flatmap_init_ro_size +
450				__FLATMAP_PAGER_TRAILING_SPACE);
451PROVIDE(__vcore_init_ro_size = 0);
452#endif /* CFG_CORE_RODATA_NOEXEC */
453#endif /* CFG_WITH_PAGER */
454