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 = S_CODE_START, LENGTH = S_CODE_SIZE + 0x000000
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 = S_DATA_START, LENGTH = S_DATA_SIZE + 0x000000
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		*(.gnu.linkonce.r.*)
107
108
109		*(.srodata.cst16)
110		*(.srodata.cst8)
111		*(.srodata.cst4)
112		*(.srodata.cst2)
113		*(.srodata .srodata.*)
114		*(.data.rel.ro .data.rel.ro.*)
115		*(.got .got.*)
116
117		/* Need to pre-align so that the symbols come after padding */
118		. = ALIGN(8);
119
120		/* lists of constructors and destructors */
121		PROVIDE_HIDDEN ( __preinit_array_start = . );
122		KEEP (*(.preinit_array))
123		PROVIDE_HIDDEN ( __preinit_array_end = . );
124
125		PROVIDE_HIDDEN ( __init_array_start = . );
126		KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*)))
127		KEEP (*(.init_array .ctors))
128		PROVIDE_HIDDEN ( __init_array_end = . );
129
130		PROVIDE_HIDDEN ( __fini_array_start = . );
131		KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*)))
132		KEEP (*(.fini_array .dtors))
133		PROVIDE_HIDDEN ( __fini_array_end = . );
134
135	} >ROM0 AT>ROM0 :text
136
137#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
138	.veneers :
139	{
140		. = ALIGN(32);
141		KEEP(*(.gnu.sgstubs))
142	} > ROM0 AT>ROM0 :text
143#endif
144
145	.toc : {
146		*(.toc .toc.*)
147	} >ROM0 AT>ROM0 :text
148
149	/* additional sections when compiling with C++ exception support */
150
151	.except_ordered : {
152		*(.gcc_except_table *.gcc_except_table.*)
153		KEEP (*(.eh_frame .eh_frame.*))
154		*(.ARM.extab* .gnu.linkonce.armextab.*)
155	} >ROM0 AT>ROM0 :text
156
157	.except_unordered : {
158		. = ALIGN(8);
159
160		PROVIDE(__exidx_start = .);
161		*(.ARM.exidx*)
162		PROVIDE(__exidx_end = .);
163	} >ROM0 AT>ROM0 :text
164
165
166	/*
167	 * Data values which are preserved across reset
168	 */
169	.preserve (NOLOAD) : {
170		PROVIDE(__preserve_start__ = .);
171		KEEP(*(SORT_BY_NAME(.preserve.*)))
172		KEEP(*(.preserve))
173		PROVIDE(__preserve_end__ = .);
174	} >RAM0 AT>RAM0 :ram
175
176	.data :  {
177		*(.data .data.*)
178		*(.gnu.linkonce.d.*)
179
180		/* read-only data */
181		*(.rdata)
182		*(.rodata .rodata.*)
183
184		/* Need to pre-align so that the symbols come after padding */
185		. = ALIGN(8);
186
187		PROVIDE( __global_pointer$ = . + 0x800 );
188		*(.sdata .sdata.* .sdata2.*)
189		*(.gnu.linkonce.s.*)
190	} >RAM0 AT>ROM0 :ram_init
191	PROVIDE(__data_start = ADDR(.data));
192	PROVIDE(__data_source = LOADADDR(.data));
193
194	/* Thread local initialized data. This gets
195	 * space allocated as it is expected to be placed
196	 * in ram to be used as a template for TLS data blocks
197	 * allocated at runtime. We're slightly abusing that
198	 * by placing the data in flash where it will be copied
199	 * into the allocate ram addresses by the existing
200	 * data initialization code in crt0
201	 */
202	.tdata :  {
203		*(.tdata .tdata.* .gnu.linkonce.td.*)
204		PROVIDE(__data_end = .);
205		PROVIDE(__tdata_end = .);
206	} >RAM0 AT>ROM0 :tls :ram_init
207	PROVIDE( __tls_base = ADDR(.tdata));
208	PROVIDE( __tdata_start = ADDR(.tdata));
209	PROVIDE( __tdata_source = LOADADDR(.tdata) );
210	PROVIDE( __tdata_source_end = LOADADDR(.tdata) + SIZEOF(.tdata) );
211	PROVIDE( __data_source_end = __tdata_source_end );
212	PROVIDE( __tdata_size = SIZEOF(.tdata) );
213	PROVIDE( __tls_align = MAX(ALIGNOF(.tdata),ALIGNOF(.tbss)) );
214
215	PROVIDE( __edata = __data_end );
216	PROVIDE( _edata = __data_end );
217	PROVIDE( edata = __data_end );
218	PROVIDE( __data_size = __data_end - __data_start );
219	PROVIDE( __data_source_size = __data_source_end - __data_source );
220
221	.tbss (NOLOAD) : {
222		*(.tbss .tbss.* .gnu.linkonce.tb.*)
223		*(.tcommon)
224		PROVIDE( __tls_end = . );
225		PROVIDE( __tbss_end = . );
226	} >RAM0 AT>RAM0 :tls :ram
227	PROVIDE( __bss_start = ADDR(.tbss));
228	PROVIDE( __tbss_start = ADDR(.tbss));
229	PROVIDE( __tbss_offset = ADDR(.tbss) - ADDR(.tdata) );
230	PROVIDE( __tbss_size = SIZEOF(.tbss) );
231	PROVIDE( __tls_size = __tls_end - __tls_base );
232	PROVIDE( __tls_align = MAX(ALIGNOF(.tdata), ALIGNOF(.tbss)) );
233	PROVIDE( __arm32_tls_tcb_offset = MAX(8, __tls_align) );
234	PROVIDE( __arm64_tls_tcb_offset = MAX(16, __tls_align) );
235
236	/*
237	 * The linker special cases .tbss segments which are
238	 * identified as segments which are not loaded and are
239	 * thread_local.
240	 *
241	 * For these segments, the linker does not advance 'dot'
242	 * across them.  We actually need memory allocated for tbss,
243	 * so we create a special segment here just to make room
244	 */
245	/*
246	.tbss_space (NOLOAD) : {
247		. = ADDR(.tbss);
248		. = . + SIZEOF(.tbss);
249	} >RAM0 AT>RAM0 :ram
250	*/
251
252	.bss (NOLOAD) : {
253		*(.sbss*)
254		*(.gnu.linkonce.sb.*)
255		*(.bss .bss.*)
256		*(.gnu.linkonce.b.*)
257		*(COMMON)
258
259		/* Align the heap */
260		. = ALIGN(8);
261		__bss_end = .;
262	} >RAM0 AT>RAM0 :ram
263	PROVIDE( __non_tls_bss_start = ADDR(.bss) );
264	PROVIDE( __end = __bss_end );
265	PROVIDE( _end = __bss_end );
266	PROVIDE( end = __bss_end );
267	PROVIDE( __bss_size = __bss_end - __bss_start );
268
269	/* Make the rest of memory available for heap storage */
270	PROVIDE (__heap_start = __end);
271#ifdef HEAP_SIZE
272	PROVIDE (__heap_end = __heap_start + HEAP_SIZE);
273	PROVIDE (__heap_size = HEAP_SIZE);
274#else
275	PROVIDE (__heap_end = __stack - STACK_SIZE);
276	PROVIDE (__heap_size = __heap_end - __heap_start);
277#endif
278	.heap (NOLOAD) : {
279		. += __heap_size;
280	} >RAM0 :ram
281
282	/* Define a stack region to make sure it fits in memory */
283	PROVIDE(__stack = ORIGIN(RAM0) + LENGTH(RAM0) - __STACKSEAL_SIZE);
284	PROVIDE(__stack_limit = __stack - STACK_SIZE);
285	.stack (__stack_limit) (NOLOAD) : {
286		. += STACK_SIZE;
287	} >RAM0 :ram
288
289#if __STACKSEAL_SIZE > 0
290	PROVIDE(__stack_seal = __stack);
291  	.stackseal (__stack) (NOLOAD) :
292  	{
293    	. += __STACKSEAL_SIZE;
294  	} >RAM0 :ram
295#endif
296
297	/* Throw away C++ exception handling information */
298
299	/*
300
301	/DISCARD/ : {
302		*(.note .note.*)
303		*(.eh_frame .eh_frame.*)
304		*(.ARM.extab* .gnu.linkonce.armextab.*)
305		*(.ARM.exidx*)
306	}
307
308	*/
309
310	/* Stabs debugging sections.  */
311	.stab          0 : { *(.stab) }
312	.stabstr       0 : { *(.stabstr) }
313	.stab.excl     0 : { *(.stab.excl) }
314	.stab.exclstr  0 : { *(.stab.exclstr) }
315	.stab.index    0 : { *(.stab.index) }
316	.stab.indexstr 0 : { *(.stab.indexstr) }
317	.comment       0 : { *(.comment) }
318	.gnu.build.attributes : { *(.gnu.build.attributes .gnu.build.attributes.*) }
319	/* DWARF debug sections.
320	   Symbols in the DWARF debugging sections are relative to the beginning
321	   of the section so we begin them at 0.  */
322	/* DWARF 1.  */
323	.debug          0 : { *(.debug) }
324	.line           0 : { *(.line) }
325	/* GNU DWARF 1 extensions.  */
326	.debug_srcinfo  0 : { *(.debug_srcinfo) }
327	.debug_sfnames  0 : { *(.debug_sfnames) }
328	/* DWARF 1.1 and DWARF 2.  */
329	.debug_aranges  0 : { *(.debug_aranges) }
330	.debug_pubnames 0 : { *(.debug_pubnames) }
331	/* DWARF 2.  */
332	.debug_info     0 : { *(.debug_info .gnu.linkonce.wi.*) }
333	.debug_abbrev   0 : { *(.debug_abbrev) }
334	.debug_line     0 : { *(.debug_line .debug_line.* .debug_line_end) }
335	.debug_frame    0 : { *(.debug_frame) }
336	.debug_str      0 : { *(.debug_str) }
337	.debug_loc      0 : { *(.debug_loc) }
338	.debug_macinfo  0 : { *(.debug_macinfo) }
339	/* SGI/MIPS DWARF 2 extensions.  */
340	.debug_weaknames 0 : { *(.debug_weaknames) }
341	.debug_funcnames 0 : { *(.debug_funcnames) }
342	.debug_typenames 0 : { *(.debug_typenames) }
343	.debug_varnames  0 : { *(.debug_varnames) }
344	/* DWARF 3.  */
345	.debug_pubtypes 0 : { *(.debug_pubtypes) }
346	.debug_ranges   0 : { *(.debug_ranges) }
347	/* DWARF 5.  */
348	.debug_addr     0 : { *(.debug_addr) }
349	.debug_line_str 0 : { *(.debug_line_str) }
350	.debug_loclists 0 : { *(.debug_loclists) }
351	.debug_macro    0 : { *(.debug_macro) }
352	.debug_names    0 : { *(.debug_names) }
353	.debug_rnglists 0 : { *(.debug_rnglists) }
354	.debug_str_offsets 0 : { *(.debug_str_offsets) }
355	.debug_sup      0 : { *(.debug_sup) }
356	.gnu.attributes 0 : { KEEP (*(.gnu.attributes)) }
357}
358/*
359 * Check that sections that are copied from flash to RAM have matching
360 * padding, so that a single memcpy() of __data_size copies the correct bytes.
361 */
362ASSERT( __data_size == __data_source_size,
363	"ERROR: .data/.tdata flash size does not match RAM size");
364