1/* Based on GCC ARM embedded samples.
2   Defines the following symbols for use by code:
3    __exidx_start
4    __exidx_end
5    __etext
6    __data_start__
7    __preinit_array_start
8    __preinit_array_end
9    __init_array_start
10    __init_array_end
11    __fini_array_start
12    __fini_array_end
13    __data_end__
14    __bss_start__
15    __bss_end__
16    __end__
17    end
18    __HeapLimit
19    __StackLimit
20    __StackTop
21    __stack (== StackTop)
22*/
23
24MEMORY
25{
26    FLASH(rx) : ORIGIN = 0x10000000, LENGTH = (2 * 1024 * 1024)
27    RAM(rwx) : ORIGIN =  0x20000000, LENGTH = 520k
28    SCRATCH_X(rwx) : ORIGIN = 0x20080000, LENGTH = 4k
29    SCRATCH_Y(rwx) : ORIGIN = 0x20081000, LENGTH = 4k
30}
31
32ENTRY(_entry_point)
33
34SECTIONS
35{
36    .flash_begin : {
37        __flash_binary_start = .;
38    } > FLASH
39
40    .text : {
41        _stext = .;
42        __logical_binary_start = .;
43        KEEP (*(.vectors))
44        KEEP (*(.binary_info_header))
45        __binary_info_header_end = .;
46        KEEP (*(.embedded_block))
47        __embedded_block_end = .;
48        KEEP (*(.reset))
49        /* TODO revisit this now memset/memcpy/float in ROM */
50        /* bit of a hack right now to exclude all floating point and time critical (e.g. memset, memcpy) code from
51         * FLASH ... we will include any thing excluded here in .data below by default */
52        *(.init)
53        *libgcc.a:cmse_nonsecure_call.o
54        *(EXCLUDE_FILE(*libgcc.a: *libc.a:*lib_a-mem*.o *libm.a:) .text*)
55        *(.fini)
56
57
58        . = ALIGN(4);
59
60        /* Pull all c'tors into .text */
61        *crtbegin.o(.ctors)
62        *crtbegin?.o(.ctors)
63        *(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors)
64        *(SORT(.ctors.*))
65        *(.ctors)
66        /* Followed by destructors */
67        *crtbegin.o(.dtors)
68        *crtbegin?.o(.dtors)
69        *(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors)
70        *(SORT(.dtors.*))
71        *(.dtors)
72
73        . = ALIGN(4);
74        /* preinit data */
75        PROVIDE_HIDDEN (__preinit_array_start = .);
76        KEEP(*(SORT(.preinit_array.*)))
77        KEEP(*(.preinit_array))
78        PROVIDE_HIDDEN (__preinit_array_end = .);
79
80        . = ALIGN(4);
81        /* init data */
82        PROVIDE_HIDDEN (__init_array_start = .);
83        KEEP(*(SORT(.init_array.*)))
84        KEEP(*(.init_array))
85        PROVIDE_HIDDEN (__init_array_end = .);
86
87        . = ALIGN(4);
88        /* finit data */
89        PROVIDE_HIDDEN (__fini_array_start = .);
90        *(SORT(.fini_array.*))
91        *(.fini_array)
92        PROVIDE_HIDDEN (__fini_array_end = .);
93
94
95        /* section information for utest */
96        . = ALIGN(4);
97        __rt_utest_tc_tab_start = .;
98        KEEP(*(UtestTcTab))
99        __rt_utest_tc_tab_end = .;
100
101        /* section information for finsh shell */
102        . = ALIGN(4);
103        __fsymtab_start = .;
104        KEEP(*(FSymTab))
105        __fsymtab_end = .;
106
107        . = ALIGN(4);
108        __vsymtab_start = .;
109        KEEP(*(VSymTab))
110        __vsymtab_end = .;
111
112        /* section information for initial. */
113        . = ALIGN(4);
114        __rt_init_start = .;
115        KEEP(*(SORT(.rti_fn*)))
116        __rt_init_end = .;
117        *(.eh_frame*)
118        . = ALIGN(4);
119        _etext = .;
120    } > FLASH
121
122    .boot2 : {
123        __boot2_start__ = .;
124        *(.boot2)
125        __boot2_end__ = .;
126    } > FLASH
127
128    ASSERT(__boot2_end__ - __boot2_start__ <= 256,
129        "ERROR: Pico second stage bootloader must be no more than 256 bytes in size")
130
131    .rodata : {
132        *(EXCLUDE_FILE(*libgcc.a: *libc.a:*lib_a-mem*.o *libm.a:) .rodata*)
133        *(.srodata*)
134        . = ALIGN(4);
135        *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.flashdata*)))
136        . = ALIGN(4);
137    } > FLASH
138
139    .ARM.extab :
140    {
141        *(.ARM.extab* .gnu.linkonce.armextab.*)
142    } > FLASH
143
144    __exidx_start = .;
145    .ARM.exidx :
146    {
147        *(.ARM.exidx* .gnu.linkonce.armexidx.*)
148    } > FLASH
149    __exidx_end = .;
150
151    /* Machine inspectable binary information */
152    . = ALIGN(4);
153    __binary_info_start = .;
154    .binary_info :
155    {
156        KEEP(*(.binary_info.keep.*))
157        *(.binary_info.*)
158    } > FLASH
159    __binary_info_end = .;
160    . = ALIGN(4);
161
162   .ram_vector_table (NOLOAD): {
163        *(.ram_vector_table)
164    } > RAM
165
166    .uninitialized_data (NOLOAD): {
167        . = ALIGN(4);
168        *(.uninitialized_data*)
169    } > RAM
170
171    .data : {
172        __data_start__ = .;
173        *(vtable)
174
175        *(.time_critical*)
176
177        /* remaining .text and .rodata; i.e. stuff we exclude above because we want it in RAM */
178        *(.text*)
179        . = ALIGN(4);
180        *(.rodata*)
181        . = ALIGN(4);
182
183        *(.data*)
184        *(.sdata*)
185
186        . = ALIGN(4);
187        *(.after_data.*)
188        . = ALIGN(4);
189        /* preinit data */
190        PROVIDE_HIDDEN (__mutex_array_start = .);
191        KEEP(*(SORT(.mutex_array.*)))
192        KEEP(*(.mutex_array))
193        PROVIDE_HIDDEN (__mutex_array_end = .);
194
195        *(.jcr)
196        . = ALIGN(4);
197    } > RAM AT> FLASH
198    /* __etext is (for backwards compatibility) the name of the .data init source pointer (...) */
199
200
201    .tdata : {
202        . = ALIGN(4);
203		*(.tdata .tdata.* .gnu.linkonce.td.*)
204        /* All data end */
205        __tdata_end = .;
206    } > RAM AT> FLASH
207    PROVIDE(__data_end__ = .);
208
209    /* __etext is (for backwards compatibility) the name of the .data init source pointer (...) */
210    __etext = LOADADDR(.data);
211
212    .tbss (NOLOAD) : {
213        . = ALIGN(4);
214        __bss_start__ = .;
215        __tls_base = .;
216        *(.tbss .tbss.* .gnu.linkonce.tb.*)
217        *(.tcommon)
218
219        __tls_end = .;
220
221    } > RAM
222
223
224    .bss (NOLOAD) : {
225        __tbss_end = .;
226        . = ALIGN(4);
227
228
229        *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.bss*)))
230        *(COMMON)
231        PROVIDE(__global_pointer$ = . + 2K);
232        *(.sbss*)
233        . = ALIGN(4);
234        __bss_end__ = .;
235    } > RAM
236
237    .heap (NOLOAD):
238    {
239        _sstack = .;
240        __end__ = .;
241        end = __end__;
242        KEEP(*(.heap*))
243        __HeapLimit = .;
244        _estack = .;
245    } > RAM
246    /* historically on GCC sbrk was growing past __HeapLimit to __StackLimit, however
247       to be more compatible, we now set __HeapLimit explicitly to where the end of the heap is */
248
249
250
251    /* Start and end symbols must be word-aligned */
252    .scratch_x : {
253        __scratch_x_start__ = .;
254        *(.scratch_x.*)
255        . = ALIGN(4);
256        __scratch_x_end__ = .;
257    } > SCRATCH_X AT > FLASH
258    __scratch_x_source__ = LOADADDR(.scratch_x);
259
260    .scratch_y : {
261        __scratch_y_start__ = .;
262        *(.scratch_y.*)
263        . = ALIGN(4);
264        __scratch_y_end__ = .;
265    } > SCRATCH_Y AT > FLASH
266    __scratch_y_source__ = LOADADDR(.scratch_y);
267
268    /* .stack*_dummy section doesn't contains any symbols. It is only
269     * used for linker to calculate size of stack sections, and assign
270     * values to stack symbols later
271     *
272     * stack1 section may be empty/missing if platform_launch_core1 is not used */
273
274    /* by default we put core 0 stack at the end of scratch Y, so that if core 1
275     * stack is not used then all of SCRATCH_X is free.
276     */
277    .stack1_dummy (NOLOAD):
278    {
279        *(.stack1*)
280    } > SCRATCH_X
281    .stack_dummy (NOLOAD):
282    {
283        KEEP(*(.stack*))
284    } > SCRATCH_Y
285
286    .flash_end : {
287        KEEP(*(.embedded_end_block*))
288        PROVIDE(__flash_binary_end = .);
289    } > FLASH =0xaa
290
291    /* stack limit is poorly named, but historically is maximum heap ptr */
292    __StackLimit = ORIGIN(RAM) + LENGTH(RAM);
293    __StackOneTop = ORIGIN(SCRATCH_X) + LENGTH(SCRATCH_X);
294    __StackTop = ORIGIN(SCRATCH_Y) + LENGTH(SCRATCH_Y);
295    __StackOneBottom = __StackOneTop - SIZEOF(.stack1_dummy);
296    __StackBottom = __StackTop - SIZEOF(.stack_dummy);
297    PROVIDE(__stack = __StackTop);
298
299    /* Check if data + heap + stack exceeds RAM limit */
300    ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed")
301
302    ASSERT( __binary_info_header_end - __logical_binary_start <= 1024, "Binary info must be in first 1024 bytes of the binary")
303    ASSERT( __embedded_block_end - __logical_binary_start <= 4096, "Embedded block must be in first 4096 bytes of the binary")
304
305    /* todo assert on extra code */
306
307}
308
309