10dac6e2df2dc782a674b12bad8f112cf8cf4184
[platform/core/security/tef-optee_os.git] / core / arch / arm / kernel / kern.ld.S
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 OUTPUT_FORMAT(CFG_KERN_LINKER_FORMAT)
54 OUTPUT_ARCH(CFG_KERN_LINKER_ARCH)
55
56 ENTRY(_start)
57 SECTIONS
58 {
59         . = CFG_TEE_LOAD_ADDR;
60
61         /* text/read-only data */
62         .text : {
63                 __text_start = .;
64                 KEEP(*(.text.boot.vectab1))
65                 KEEP(*(.text.boot.vectab2))
66                 KEEP(*(.text.boot))
67
68                 . = ALIGN(8);
69                 __initcall_start = .;
70                 KEEP(*(.initcall1))
71                 KEEP(*(.initcall2))
72                 KEEP(*(.initcall3))
73                 KEEP(*(.initcall4))
74                 __initcall_end = .;
75
76 #ifdef CFG_WITH_PAGER
77                 *(.text)
78 /* Include list of sections needed for paging */
79 #include <text_unpaged.ld.S>
80 #else
81                 *(.text .text.*)
82 #endif
83                 *(.sram.text.glue_7* .gnu.linkonce.t.*)
84                 . = ALIGN(8);
85                 __text_end = .;
86         }
87
88         .rodata : ALIGN(8) {
89                 __rodata_start = .;
90                 *(.gnu.linkonce.r.*)
91 #ifdef CFG_WITH_PAGER
92                 *(.rodata .rodata.__unpaged)
93 #include <rodata_unpaged.ld.S>
94 #else
95                 *(.rodata .rodata.*)
96
97                 /*
98                  * 8 to avoid unwanted padding between __start_ta_head_section
99                  * and the first structure in ta_head_section, in 64-bit
100                  * builds
101                  */
102                 . = ALIGN(8);
103                 __start_ta_head_section = . ;
104                 KEEP(*(ta_head_section))
105                 __stop_ta_head_section = . ;
106                 . = ALIGN(8);
107                 __start_phys_mem_map_section = . ;
108                 KEEP(*(phys_mem_map_section))
109                 __end_phys_mem_map_section = . ;
110 #endif
111                 . = ALIGN(8);
112                 __rodata_end = .;
113         }
114
115         .interp : { *(.interp) }
116         .hash : { *(.hash) }
117         .dynsym : { *(.dynsym) }
118         .dynstr : { *(.dynstr) }
119         .rel.text : { *(.rel.text) *(.rel.gnu.linkonce.t*) }
120         .rela.text : { *(.rela.text) *(.rela.gnu.linkonce.t*) }
121         .rel.data : { *(.rel.data) *(.rel.gnu.linkonce.d*) }
122         .rela.data : { *(.rela.data) *(.rela.gnu.linkonce.d*) }
123         .rel.rodata : { *(.rel.rodata) *(.rel.gnu.linkonce.r*) }
124         .rela.rodata : { *(.rela.rodata) *(.rela.gnu.linkonce.r*) }
125         .rel.got : { *(.rel.got) }
126         .rela.got : { *(.rela.got) }
127         .rel.ctors : { *(.rel.ctors) }
128         .rela.ctors : { *(.rela.ctors) }
129         .rel.dtors : { *(.rel.dtors) }
130         .rela.dtors : { *(.rela.dtors) }
131         .rel.init : { *(.rel.init) }
132         .rela.init : { *(.rela.init) }
133         .rel.fini : { *(.rel.fini) }
134         .rela.fini : { *(.rela.fini) }
135         .rel.bss : { *(.rel.bss) }
136         .rela.bss : { *(.rela.bss) }
137         .rel.plt : { *(.rel.plt) }
138         .rela.plt : { *(.rela.plt) }
139         .init : { *(.init) } =0x9090
140         .plt : { *(.plt) }
141
142         /* .ARM.exidx is sorted, so has to go in its own output section.  */
143         .ARM.exidx : {
144                 __exidx_start = .;
145                 *(.ARM.exidx* .gnu.linkonce.armexidx.*)
146                 __exidx_end = .;
147         }
148
149         .ARM.extab : {
150                 __extab_start = .;
151                 *(.ARM.extab*)
152                 __extab_end = .;
153         }
154
155         .data : ALIGN(8) {
156                 /* writable data  */
157                 __data_start_rom = .;
158                 /* in one segment binaries, the rom data address is on top
159                    of the ram data address */
160                 __early_bss_start = .;
161                 *(.early_bss .early_bss.*)
162                 . = ALIGN(8);
163                 __early_bss_end = .;
164                 __data_start = .;
165                 *(.data .data.* .gnu.linkonce.d.*)
166                 . = ALIGN(8);
167         }
168
169         .ctors : ALIGN(8) {
170                 __ctor_list = .;
171                 KEEP(*(.ctors .ctors.* .init_array .init_array.*))
172                 __ctor_end = .;
173         }
174         .dtors : ALIGN(8) {
175                 __dtor_list = .;
176                 KEEP(*(.dtors .dtors.* .fini_array .fini_array.*))
177                 __dtor_end = .;
178         }
179         .got : { *(.got.plt) *(.got) }
180         .dynamic : { *(.dynamic) }
181
182         __data_end = .;
183         /* unintialized data */
184         .bss : ALIGN(8) {
185                 __bss_start = .;
186                 *(.bss .bss.*)
187                 *(.gnu.linkonce.b.*)
188                 *(COMMON)
189                 . = ALIGN(8);
190                 __bss_end = .;
191         }
192
193         .heap1 (NOLOAD) : {
194                 /*
195                  * We're keeping track of the padding added before the
196                  * .nozi section so we can do something useful with
197                  * this otherwise wasted memory.
198                  */
199                 __heap1_start = .;
200 #ifndef CFG_WITH_PAGER
201                 . += CFG_CORE_HEAP_SIZE;
202 #endif
203                 . = ALIGN(16 * 1024);
204                 __heap1_end = .;
205         }
206
207         /*
208          * Uninitialized data that shouldn't be zero initialized at
209          * runtime.
210          *
211          * L1 mmu table requires 16 KiB alignment
212          */
213         .nozi (NOLOAD) : ALIGN(16 * 1024) {
214                 __nozi_start = .;
215                 KEEP(*(.nozi .nozi.*))
216                 . = ALIGN(16);
217                 __nozi_end = .;
218                 __nozi_stack_start = .;
219                 KEEP(*(.nozi_stack))
220                 . = ALIGN(8);
221                 __nozi_stack_end = .;
222         }
223
224 #ifdef CFG_WITH_PAGER
225         .heap2 (NOLOAD) : {
226                 __heap2_start = .;
227                 /*
228                  * Reserve additional memory for heap, the total should be
229                  * at least CFG_CORE_HEAP_SIZE, but count what has already
230                  * been reserved in .heap1
231                  */
232                 . += CFG_CORE_HEAP_SIZE - (__heap1_end - __heap1_start);
233                 . = ALIGN(4 * 1024);
234                 __heap2_end = .;
235         }
236
237         .text_init : ALIGN(4 * 1024) {
238                 __text_init_start = .;
239 /*
240  * Include list of sections needed for boot initialization, this list
241  * overlaps with unpaged.ld.S but since unpaged.ld.S is first all those
242  * sections will go into the unpaged area.
243  */
244 #include <text_init.ld.S>
245                 . = ALIGN(8);
246                 __text_init_end = .;
247         }
248
249         .rodata_init : ALIGN(8) {
250                 __rodata_init_start = .;
251 #include <rodata_init.ld.S>
252                 . = ALIGN(8);
253                 __start_phys_mem_map_section = . ;
254                 KEEP(*(phys_mem_map_section))
255                 __end_phys_mem_map_section = . ;
256                 . = ALIGN(8);
257                 __rodata_init_end = .;
258         }
259         __init_start = __text_init_start;
260         __init_end = .;
261         __init_size = __init_end - __text_init_start;
262
263         .text_pageable : ALIGN(8) {
264                 __text_pageable_start = .;
265                 *(.text*)
266                 . = ALIGN(8);
267                 __text_pageable_end = .;
268         }
269
270         .rodata_pageable : ALIGN(8) {
271                 __rodata_pageable_start = .;
272                 *(.rodata*)
273                 /*
274                  * 8 to avoid unwanted padding between __start_ta_head_section
275                  * and the first structure in ta_head_section, in 64-bit
276                  * builds
277                  */
278                 . = ALIGN(8);
279                 __start_ta_head_section = . ;
280                 KEEP(*(ta_head_section))
281                 __stop_ta_head_section = . ;
282                 . = ALIGN(4 * 1024);
283                 __rodata_pageable_end = .;
284         }
285
286         __pageable_part_start = __rodata_init_end;
287         __pageable_part_end = __rodata_pageable_end;
288         __pageable_start = __text_init_start;
289         __pageable_end = __pageable_part_end;
290
291         /*
292          * Assign a safe spot to store the hashes of the pages before
293          * heap is initialized.
294          */
295         __tmp_hashes_start = __rodata_init_end;
296         __tmp_hashes_size = ((__pageable_end - __pageable_start) /
297                                 (4 * 1024)) * 32;
298         __tmp_hashes_end = __tmp_hashes_start + __tmp_hashes_size;
299
300         __init_mem_usage = __tmp_hashes_end - CFG_TEE_LOAD_ADDR;
301
302         ASSERT(CFG_TEE_LOAD_ADDR >= CFG_TEE_RAM_START,
303                 "Load address before start of physical memory")
304         ASSERT(CFG_TEE_LOAD_ADDR < (CFG_TEE_RAM_START + CFG_TEE_RAM_PH_SIZE),
305                 "Load address after end of physical memory")
306         ASSERT(__tmp_hashes_end < (CFG_TEE_RAM_START + CFG_TEE_RAM_PH_SIZE),
307                 "OP-TEE can't fit init part into available physical memory")
308         ASSERT((CFG_TEE_RAM_START + CFG_TEE_RAM_PH_SIZE - __init_end) >
309                 1 * 4096, "Too few free pages to initialize paging")
310
311
312 #endif /*CFG_WITH_PAGER*/
313
314 #ifdef CFG_CORE_SANITIZE_KADDRESS
315         . = CFG_TEE_RAM_START + (CFG_TEE_RAM_VA_SIZE * 8) / 9 - 8;
316         . = ALIGN(8);
317         .asan_shadow : {
318                 __asan_shadow_start = .;
319                 . += CFG_TEE_RAM_VA_SIZE / 9;
320                 __asan_shadow_end = .;
321         }
322 #endif /*CFG_CORE_SANITIZE_KADDRESS*/
323
324         __end = .;
325
326 #ifndef CFG_WITH_PAGER
327         __init_size = __data_end - CFG_TEE_LOAD_ADDR;
328         __init_mem_usage = __end - CFG_TEE_LOAD_ADDR;
329 #endif
330         . = CFG_TEE_RAM_START + CFG_TEE_RAM_VA_SIZE;
331         _end_of_ram = .;
332
333         /DISCARD/ : {
334                 /* Strip unnecessary stuff */
335                 *(.comment .note .eh_frame)
336                 /* Strip meta variables */
337                 *(__keep_meta_vars*)
338         }
339
340 }