1/*
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright © 2019 Keith Packard
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above
14 *    copyright notice, this list of conditions and the following
15 *    disclaimer in the documentation and/or other materials provided
16 *    with the distribution.
17 *
18 * 3. Neither the name of the copyright holder nor the names of its
19 *    contributors may be used to endorse or promote products derived
20 *    from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33 * OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36/* ----------------------------------------------------------------------------
37  Stack seal size definition
38 *----------------------------------------------------------------------------*/
39#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
40#define __STACKSEAL_SIZE   ( 8 )
41#else
42#define __STACKSEAL_SIZE   ( 0 )
43#endif
44
45/* ----------------------------------------------------------------------------
46  Memory definition
47 *----------------------------------------------------------------------------*/
48MEMORY
49{
50  ROM0  (rx!w)  : ORIGIN = __ROM0_BASE, LENGTH = __ROM0_SIZE
51#if __ROM1_SIZE > 0
52  ROM1  (rx!w)  : ORIGIN = __ROM1_BASE, LENGTH = __ROM1_SIZE
53#endif
54#if __ROM2_SIZE > 0
55  ROM2  (rx!w)  : ORIGIN = __ROM2_BASE, LENGTH = __ROM2_SIZE
56#endif
57#if __ROM3_SIZE > 0
58  ROM3  (rx!w)  : ORIGIN = __ROM3_BASE, LENGTH = __ROM3_SIZE
59#endif
60
61  RAM0  (w!rx) : ORIGIN = __RAM0_BASE, LENGTH = __RAM0_SIZE
62#if __RAM1_SIZE > 0
63  RAM1  (w!rx) : ORIGIN = __RAM1_BASE, LENGTH = __RAM1_SIZE
64#endif
65#if __RAM2_SIZE > 0
66  RAM2  (w!rx) : ORIGIN = __RAM2_BASE, LENGTH = __RAM2_SIZE
67#endif
68#if __RAM3_SIZE > 0
69  RAM3  (w!rx) : ORIGIN = __RAM3_BASE, LENGTH = __RAM3_SIZE
70#endif
71}
72
73ENTRY(Reset_Handler)
74
75PHDRS
76{
77	text PT_LOAD;
78	ram PT_LOAD;
79	ram_init PT_LOAD;
80	tls PT_TLS;
81}
82
83SECTIONS
84{
85	.init : {
86		KEEP (*(.vectors))
87		KEEP (*(.text.init.enter))
88		KEEP (*(.data.init.enter))
89		KEEP (*(SORT_BY_NAME(.init) SORT_BY_NAME(.init.*)))
90	} >ROM0 AT>ROM0 :text
91
92	.text : {
93
94		/* code */
95		*(.text.unlikely .text.unlikely.*)
96		*(.text.startup .text.startup.*)
97		*(.text .text.* .opd .opd.*)
98		*(.gnu.linkonce.t.*)
99		KEEP (*(.fini .fini.*))
100		__text_end = .;
101
102		PROVIDE (__etext = __text_end);
103		PROVIDE (_etext = __text_end);
104		PROVIDE (etext = __text_end);
105
106		/* read-only data */
107		*(.rdata)
108		*(.rodata .rodata.*)
109		*(.gnu.linkonce.r.*)
110
111		*(.srodata.cst16)
112		*(.srodata.cst8)
113		*(.srodata.cst4)
114		*(.srodata.cst2)
115		*(.srodata .srodata.*)
116		*(.data.rel.ro .data.rel.ro.*)
117		*(.got .got.*)
118
119		/* Need to pre-align so that the symbols come after padding */
120		. = ALIGN(8);
121
122		/* lists of constructors and destructors */
123		PROVIDE_HIDDEN ( __preinit_array_start = . );
124		KEEP (*(.preinit_array))
125		PROVIDE_HIDDEN ( __preinit_array_end = . );
126
127		PROVIDE_HIDDEN ( __init_array_start = . );
128		KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*)))
129		KEEP (*(.init_array .ctors))
130		PROVIDE_HIDDEN ( __init_array_end = . );
131
132		PROVIDE_HIDDEN ( __fini_array_start = . );
133		KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*)))
134		KEEP (*(.fini_array .dtors))
135		PROVIDE_HIDDEN ( __fini_array_end = . );
136
137	} >ROM0 AT>ROM0 :text
138
139	.toc : {
140		*(.toc .toc.*)
141	} >ROM0 AT>ROM0 :text
142
143	/* additional sections when compiling with C++ exception support */
144
145	.except_ordered : {
146		*(.gcc_except_table *.gcc_except_table.*)
147		KEEP (*(.eh_frame .eh_frame.*))
148		*(.ARM.extab* .gnu.linkonce.armextab.*)
149	} >ROM0 AT>ROM0 :text
150
151	.except_unordered : {
152		. = ALIGN(8);
153
154		PROVIDE(__exidx_start = .);
155		*(.ARM.exidx*)
156		PROVIDE(__exidx_end = .);
157	} >ROM0 AT>ROM0 :text
158
159
160	/*
161	 * Data values which are preserved across reset
162	 */
163	.preserve (NOLOAD) : {
164		PROVIDE(__preserve_start__ = .);
165		KEEP(*(SORT_BY_NAME(.preserve.*)))
166		KEEP(*(.preserve))
167		PROVIDE(__preserve_end__ = .);
168	} >RAM0 AT>RAM0 :ram
169
170	.data :  {
171		*(.data .data.*)
172		*(.gnu.linkonce.d.*)
173
174		/* Need to pre-align so that the symbols come after padding */
175		. = ALIGN(8);
176
177		PROVIDE( __global_pointer$ = . + 0x800 );
178		*(.sdata .sdata.* .sdata2.*)
179		*(.gnu.linkonce.s.*)
180	} >RAM0 AT>ROM0 :ram_init
181	PROVIDE(__data_start = ADDR(.data));
182	PROVIDE(__data_source = LOADADDR(.data));
183
184	/* Thread local initialized data. This gets
185	 * space allocated as it is expected to be placed
186	 * in ram to be used as a template for TLS data blocks
187	 * allocated at runtime. We're slightly abusing that
188	 * by placing the data in flash where it will be copied
189	 * into the allocate ram addresses by the existing
190	 * data initialization code in crt0
191	 */
192	.tdata :  {
193		*(.tdata .tdata.* .gnu.linkonce.td.*)
194		PROVIDE(__data_end = .);
195		PROVIDE(__tdata_end = .);
196	} >RAM0 AT>ROM0 :tls :ram_init
197	PROVIDE( __tls_base = ADDR(.tdata));
198	PROVIDE( __tdata_start = ADDR(.tdata));
199	PROVIDE( __tdata_source = LOADADDR(.tdata) );
200	PROVIDE( __tdata_source_end = LOADADDR(.tdata) + SIZEOF(.tdata) );
201	PROVIDE( __data_source_end = __tdata_source_end );
202	PROVIDE( __tdata_size = SIZEOF(.tdata) );
203	PROVIDE( __tls_align = MAX(ALIGNOF(.tdata),ALIGNOF(.tbss)) );
204
205	PROVIDE( __edata = __data_end );
206	PROVIDE( _edata = __data_end );
207	PROVIDE( edata = __data_end );
208	PROVIDE( __data_size = __data_end - __data_start );
209	PROVIDE( __data_source_size = __data_source_end - __data_source );
210
211	.tbss (NOLOAD) : {
212		*(.tbss .tbss.* .gnu.linkonce.tb.*)
213		*(.tcommon)
214		PROVIDE( __tls_end = . );
215		PROVIDE( __tbss_end = . );
216	} >RAM0 AT>RAM0 :tls :ram
217	PROVIDE( __bss_start = ADDR(.tbss));
218	PROVIDE( __tbss_start = ADDR(.tbss));
219	PROVIDE( __tbss_offset = ADDR(.tbss) - ADDR(.tdata) );
220	PROVIDE( __tbss_size = SIZEOF(.tbss) );
221	PROVIDE( __tls_size = __tls_end - __tls_base );
222	PROVIDE( __tls_align = MAX(ALIGNOF(.tdata), ALIGNOF(.tbss)) );
223	PROVIDE( __arm32_tls_tcb_offset = MAX(8, __tls_align) );
224	PROVIDE( __arm64_tls_tcb_offset = MAX(16, __tls_align) );
225
226	/*
227	 * The linker special cases .tbss segments which are
228	 * identified as segments which are not loaded and are
229	 * thread_local.
230	 *
231	 * For these segments, the linker does not advance 'dot'
232	 * across them.  We actually need memory allocated for tbss,
233	 * so we create a special segment here just to make room
234	 */
235	/*
236	.tbss_space (NOLOAD) : {
237		. = ADDR(.tbss);
238		. = . + SIZEOF(.tbss);
239	} >RAM0 AT>RAM0 :ram
240	*/
241
242	.bss (NOLOAD) : {
243		*(.sbss*)
244		*(.gnu.linkonce.sb.*)
245		*(.bss .bss.*)
246		*(.gnu.linkonce.b.*)
247		*(COMMON)
248
249		/* Align the heap */
250		. = ALIGN(8);
251		__bss_end = .;
252	} >RAM0 AT>RAM0 :ram
253	PROVIDE( __non_tls_bss_start = ADDR(.bss) );
254	PROVIDE( __end = __bss_end );
255	PROVIDE( _end = __bss_end );
256	PROVIDE( end = __bss_end );
257	PROVIDE( __bss_size = __bss_end - __bss_start );
258
259	/* Make the rest of memory available for heap storage */
260	PROVIDE (__heap_start = __end);
261#ifdef __HEAP_SIZE
262	PROVIDE (__heap_end = __heap_start + __HEAP_SIZE);
263	PROVIDE (__heap_size = __HEAP_SIZE);
264#else
265	PROVIDE (__heap_end = __stack - __STACK_SIZE);
266	PROVIDE (__heap_size = __heap_end - __heap_start);
267#endif
268	.heap (NOLOAD) : {
269		. += __heap_size;
270	} >RAM0 :ram
271
272	/* Define a stack region to make sure it fits in memory */
273	PROVIDE(__stack = ORIGIN(RAM0) + LENGTH(RAM0) - __STACKSEAL_SIZE);
274	PROVIDE(__stack_limit = __stack - __STACK_SIZE);
275	.stack (__stack_limit) (NOLOAD) : {
276		. += __STACK_SIZE;
277	} >RAM0 :ram
278
279#if __STACKSEAL_SIZE > 0
280	PROVIDE(__stack_seal = __stack)
281  	.stackseal (__stack) (NOLOAD) :
282  	{
283    	. += __STACKSEAL_SIZE;
284  	} >RAM0 :ram
285#endif
286
287	/* Throw away C++ exception handling information */
288
289	/*
290
291	/DISCARD/ : {
292		*(.note .note.*)
293		*(.eh_frame .eh_frame.*)
294		*(.ARM.extab* .gnu.linkonce.armextab.*)
295		*(.ARM.exidx*)
296	}
297
298	*/
299
300	/* Stabs debugging sections.  */
301	.stab          0 : { *(.stab) }
302	.stabstr       0 : { *(.stabstr) }
303	.stab.excl     0 : { *(.stab.excl) }
304	.stab.exclstr  0 : { *(.stab.exclstr) }
305	.stab.index    0 : { *(.stab.index) }
306	.stab.indexstr 0 : { *(.stab.indexstr) }
307	.comment       0 : { *(.comment) }
308	.gnu.build.attributes : { *(.gnu.build.attributes .gnu.build.attributes.*) }
309	/* DWARF debug sections.
310	   Symbols in the DWARF debugging sections are relative to the beginning
311	   of the section so we begin them at 0.  */
312	/* DWARF 1.  */
313	.debug          0 : { *(.debug) }
314	.line           0 : { *(.line) }
315	/* GNU DWARF 1 extensions.  */
316	.debug_srcinfo  0 : { *(.debug_srcinfo) }
317	.debug_sfnames  0 : { *(.debug_sfnames) }
318	/* DWARF 1.1 and DWARF 2.  */
319	.debug_aranges  0 : { *(.debug_aranges) }
320	.debug_pubnames 0 : { *(.debug_pubnames) }
321	/* DWARF 2.  */
322	.debug_info     0 : { *(.debug_info .gnu.linkonce.wi.*) }
323	.debug_abbrev   0 : { *(.debug_abbrev) }
324	.debug_line     0 : { *(.debug_line .debug_line.* .debug_line_end) }
325	.debug_frame    0 : { *(.debug_frame) }
326	.debug_str      0 : { *(.debug_str) }
327	.debug_loc      0 : { *(.debug_loc) }
328	.debug_macinfo  0 : { *(.debug_macinfo) }
329	/* SGI/MIPS DWARF 2 extensions.  */
330	.debug_weaknames 0 : { *(.debug_weaknames) }
331	.debug_funcnames 0 : { *(.debug_funcnames) }
332	.debug_typenames 0 : { *(.debug_typenames) }
333	.debug_varnames  0 : { *(.debug_varnames) }
334	/* DWARF 3.  */
335	.debug_pubtypes 0 : { *(.debug_pubtypes) }
336	.debug_ranges   0 : { *(.debug_ranges) }
337	/* DWARF 5.  */
338	.debug_addr     0 : { *(.debug_addr) }
339	.debug_line_str 0 : { *(.debug_line_str) }
340	.debug_loclists 0 : { *(.debug_loclists) }
341	.debug_macro    0 : { *(.debug_macro) }
342	.debug_names    0 : { *(.debug_names) }
343	.debug_rnglists 0 : { *(.debug_rnglists) }
344	.debug_str_offsets 0 : { *(.debug_str_offsets) }
345	.debug_sup      0 : { *(.debug_sup) }
346	.gnu.attributes 0 : { KEEP (*(.gnu.attributes)) }
347}
348/*
349 * Check that sections that are copied from flash to RAM have matching
350 * padding, so that a single memcpy() of __data_size copies the correct bytes.
351 */
352ASSERT( __data_size == __data_source_size,
353	"ERROR: .data/.tdata flash size does not match RAM size");
354