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
36ENTRY(_start)
37
38/*
39 * These values should be provided by the application. We'll include
40 * some phony values here to make things link for testing
41 */
42
43MEMORY
44{
45	flash (rx!w) : ORIGIN = DEFINED(__flash) ? __flash : @DEFAULT_FLASH_ADDR@, LENGTH = DEFINED(__flash_size) ? __flash_size : @DEFAULT_FLASH_SIZE@
46	ram (w!rx)   : ORIGIN = DEFINED(__ram  ) ? __ram   : @DEFAULT_RAM_ADDR@, LENGTH = DEFINED(__ram_size  ) ? __ram_size   : @DEFAULT_RAM_SIZE@
47}
48
49PHDRS
50{
51	text PT_LOAD;
52	ram PT_LOAD;
53	ram_init PT_LOAD;
54	tls PT_TLS;
55}
56
57SECTIONS
58{
59	PROVIDE(__stack = ORIGIN(ram) + LENGTH(ram));
60
61	.init : {
62		KEEP (*(.text.init.enter))
63		KEEP (*(.data.init.enter))
64		KEEP (*(SORT_BY_NAME(.init) SORT_BY_NAME(.init.*)))
65	} >flash AT>flash :text
66
67	.text : {
68
69		/* code */
70		*(.text.unlikely .text.unlikely.*)
71		*(.text.startup .text.startup.*)
72		*(.text .text.* .opd .opd.*)
73		*(.gnu.linkonce.t.*)
74		KEEP (*(.fini .fini.*))
75		__text_end = .;
76
77		PROVIDE (__etext = __text_end);
78		PROVIDE (_etext = __text_end);
79		PROVIDE (etext = __text_end);
80
81		/* read-only data */
82		*(.rdata)
83		*(.rodata .rodata.*)
84		*(.gnu.linkonce.r.*)
85
86		*(.srodata.cst16)
87		*(.srodata.cst8)
88		*(.srodata.cst4)
89		*(.srodata.cst2)
90		*(.srodata .srodata.*)
91		*(.data.rel.ro .data.rel.ro.*)
92		*(.got .got.*)
93
94		/* Need to pre-align so that the symbols come after padding */
95		. = ALIGN(8);
96
97		/* lists of constructors and destructors */
98		PROVIDE_HIDDEN ( __preinit_array_start = . );
99		KEEP (*(.preinit_array))
100		PROVIDE_HIDDEN ( __preinit_array_end = . );
101
102		PROVIDE_HIDDEN ( __init_array_start = . );
103		KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*)))
104		KEEP (*(.init_array .ctors))
105		PROVIDE_HIDDEN ( __init_array_end = . );
106
107		PROVIDE_HIDDEN ( __fini_array_start = . );
108		KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*)))
109		KEEP (*(.fini_array .dtors))
110		PROVIDE_HIDDEN ( __fini_array_end = . );
111
112	} >flash AT>flash :text
113
114	.toc : {
115		*(.toc .toc.*)
116	} >flash AT>flash :text
117
118	/* additional sections when compiling with C++ exception support */
119	@CPP_START@
120	.except_ordered : {
121		*(.gcc_except_table *.gcc_except_table.*)
122		KEEP (*(.eh_frame .eh_frame.*))
123		*(.ARM.extab* .gnu.linkonce.armextab.*)
124	} >flash AT>flash :text
125
126	.except_unordered : {
127		. = ALIGN(8);
128
129		PROVIDE(__exidx_start = .);
130		*(.ARM.exidx*)
131		PROVIDE(__exidx_end = .);
132	} >flash AT>flash :text
133	@CPP_END@
134
135	/*
136	 * Data values which are preserved across reset
137	 */
138	.preserve (NOLOAD) : {
139		PROVIDE(__preserve_start__ = .);
140		KEEP(*(SORT_BY_NAME(.preserve.*)))
141		KEEP(*(.preserve))
142		PROVIDE(__preserve_end__ = .);
143	} >ram AT>ram :ram
144
145	.data : @LDSCRIPT_ALIGN_WITH_INPUT@ {
146		*(.data .data.*)
147		*(.gnu.linkonce.d.*)
148
149		/* Need to pre-align so that the symbols come after padding */
150		. = ALIGN(8);
151
152		PROVIDE( __global_pointer$ = . + 0x800 );
153		*(.sdata .sdata.* .sdata2.*)
154		*(.gnu.linkonce.s.*)
155	} >ram AT>flash :ram_init
156	PROVIDE(__data_start = ADDR(.data));
157	PROVIDE(__data_source = LOADADDR(.data));
158
159	/* Thread local initialized data. This gets
160	 * space allocated as it is expected to be placed
161	 * in ram to be used as a template for TLS data blocks
162	 * allocated at runtime. We're slightly abusing that
163	 * by placing the data in flash where it will be copied
164	 * into the allocate ram addresses by the existing
165	 * data initialization code in crt0
166	 */
167	.tdata : @LDSCRIPT_ALIGN_WITH_INPUT@ {
168		*(.tdata .tdata.* .gnu.linkonce.td.*)
169		PROVIDE(__data_end = .);
170		PROVIDE(__tdata_end = .);
171	} >ram AT>flash :tls :ram_init
172	PROVIDE( __tls_base = ADDR(.tdata));
173	PROVIDE( __tdata_start = ADDR(.tdata));
174	PROVIDE( __tdata_source = LOADADDR(.tdata) );
175	PROVIDE( __tdata_source_end = LOADADDR(.tdata) + SIZEOF(.tdata) );
176	PROVIDE( __data_source_end = __tdata_source_end );
177	PROVIDE( __tdata_size = SIZEOF(.tdata) );
178	PROVIDE( __tls_align = MAX(ALIGNOF(.tdata),ALIGNOF(.tbss)) );
179
180	PROVIDE( __edata = __data_end );
181	PROVIDE( _edata = __data_end );
182	PROVIDE( edata = __data_end );
183	PROVIDE( __data_size = __data_end - __data_start );
184	PROVIDE( __data_source_size = __data_source_end - __data_source );
185
186	.tbss (NOLOAD) : {
187		*(.tbss .tbss.* .gnu.linkonce.tb.*)
188		*(.tcommon)
189		PROVIDE( __tls_end = . );
190		PROVIDE( __tbss_end = . );
191	} >ram AT>ram :tls :ram
192	PROVIDE( __bss_start = ADDR(.tbss));
193	PROVIDE( __tbss_start = ADDR(.tbss));
194	PROVIDE( __tbss_offset = ADDR(.tbss) - ADDR(.tdata) );
195	PROVIDE( __tbss_size = SIZEOF(.tbss) );
196	PROVIDE( __tls_size = __tls_end - __tls_base );
197	PROVIDE( __tls_align = MAX(ALIGNOF(.tdata), ALIGNOF(.tbss)) );
198	PROVIDE( __arm32_tls_tcb_offset = MAX(8, __tls_align) );
199	PROVIDE( __arm64_tls_tcb_offset = MAX(16, __tls_align) );
200
201	/*
202	 * The linker special cases .tbss segments which are
203	 * identified as segments which are not loaded and are
204	 * thread_local.
205	 *
206	 * For these segments, the linker does not advance 'dot'
207	 * across them.  We actually need memory allocated for tbss,
208	 * so we create a special segment here just to make room
209	 */
210	.tbss_space (NOLOAD) : {
211		. = ADDR(.tbss);
212		. = . + SIZEOF(.tbss);
213	} >ram AT>ram :ram
214
215	.bss (NOLOAD) : {
216		*(.sbss*)
217		*(.gnu.linkonce.sb.*)
218		*(.bss .bss.*)
219		*(.gnu.linkonce.b.*)
220		*(COMMON)
221
222		/* Align the heap */
223		. = ALIGN(8);
224		__bss_end = .;
225	} >ram AT>ram :ram
226	PROVIDE( __non_tls_bss_start = ADDR(.bss) );
227	PROVIDE( __end = __bss_end );
228	PROVIDE( _end = __bss_end );
229	PROVIDE( end = __bss_end );
230	PROVIDE( __bss_size = __bss_end - __bss_start );
231
232	/* Make the rest of memory available for heap storage */
233	PROVIDE (__heap_start = __end);
234	PROVIDE (__heap_end = __stack - (DEFINED(__stack_size) ? __stack_size : @DEFAULT_STACK_SIZE@));
235	PROVIDE (__heap_size = __heap_end - __heap_start);
236
237	/* Define a stack region to make sure it fits in memory */
238	.stack (NOLOAD) : {
239		. += (DEFINED(__stack_size) ? __stack_size : @DEFAULT_STACK_SIZE@);
240	} >ram :ram
241
242	/* Throw away C++ exception handling information */
243
244	@C_START@
245
246	/DISCARD/ : {
247		*(.note .note.*)
248		*(.eh_frame .eh_frame.*)
249		*(.ARM.extab* .gnu.linkonce.armextab.*)
250		*(.ARM.exidx*)
251	}
252
253	@C_END@
254
255	/* Stabs debugging sections.  */
256	.stab          0 : { *(.stab) }
257	.stabstr       0 : { *(.stabstr) }
258	.stab.excl     0 : { *(.stab.excl) }
259	.stab.exclstr  0 : { *(.stab.exclstr) }
260	.stab.index    0 : { *(.stab.index) }
261	.stab.indexstr 0 : { *(.stab.indexstr) }
262	.comment       0 : { *(.comment) }
263	.gnu.build.attributes : { *(.gnu.build.attributes .gnu.build.attributes.*) }
264	/* DWARF debug sections.
265	   Symbols in the DWARF debugging sections are relative to the beginning
266	   of the section so we begin them at 0.  */
267	/* DWARF 1.  */
268	.debug          0 : { *(.debug) }
269	.line           0 : { *(.line) }
270	/* GNU DWARF 1 extensions.  */
271	.debug_srcinfo  0 : { *(.debug_srcinfo) }
272	.debug_sfnames  0 : { *(.debug_sfnames) }
273	/* DWARF 1.1 and DWARF 2.  */
274	.debug_aranges  0 : { *(.debug_aranges) }
275	.debug_pubnames 0 : { *(.debug_pubnames) }
276	/* DWARF 2.  */
277	.debug_info     0 : { *(.debug_info .gnu.linkonce.wi.*) }
278	.debug_abbrev   0 : { *(.debug_abbrev) }
279	.debug_line     0 : { *(.debug_line .debug_line.* .debug_line_end) }
280	.debug_frame    0 : { *(.debug_frame) }
281	.debug_str      0 : { *(.debug_str) }
282	.debug_loc      0 : { *(.debug_loc) }
283	.debug_macinfo  0 : { *(.debug_macinfo) }
284	/* SGI/MIPS DWARF 2 extensions.  */
285	.debug_weaknames 0 : { *(.debug_weaknames) }
286	.debug_funcnames 0 : { *(.debug_funcnames) }
287	.debug_typenames 0 : { *(.debug_typenames) }
288	.debug_varnames  0 : { *(.debug_varnames) }
289	/* DWARF 3.  */
290	.debug_pubtypes 0 : { *(.debug_pubtypes) }
291	.debug_ranges   0 : { *(.debug_ranges) }
292	/* DWARF 5.  */
293	.debug_addr     0 : { *(.debug_addr) }
294	.debug_line_str 0 : { *(.debug_line_str) }
295	.debug_loclists 0 : { *(.debug_loclists) }
296	.debug_macro    0 : { *(.debug_macro) }
297	.debug_names    0 : { *(.debug_names) }
298	.debug_rnglists 0 : { *(.debug_rnglists) }
299	.debug_str_offsets 0 : { *(.debug_str_offsets) }
300	.debug_sup      0 : { *(.debug_sup) }
301	.gnu.attributes 0 : { KEEP (*(.gnu.attributes)) }
302}
303/*
304 * Check that sections that are copied from flash to RAM have matching
305 * padding, so that a single memcpy() of __data_size copies the correct bytes.
306 */
307ASSERT( __data_size == __data_source_size,
308	"ERROR: .data/.tdata flash size does not match RAM size");
309