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    INCLUDE "pico_flash_region.ld"
27    RAM(rwx) : ORIGIN =  0x20000000, LENGTH = 512k
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    /* The bootrom will enter the image at the point indicated in your
41       IMAGE_DEF, which is usually the reset handler of your vector table.
42
43       The debugger will use the ELF entry point, which is the _entry_point
44       symbol, and in our case is *different from the bootrom's entry point.*
45       This is used to go back through the bootrom on debugger launches only,
46       to perform the same initial flash setup that would be performed on a
47       cold boot.
48    */
49
50    .text : {
51        __logical_binary_start = .;
52        KEEP (*(.vectors))
53        KEEP (*(.binary_info_header))
54        __binary_info_header_end = .;
55        KEEP (*(.embedded_block))
56        __embedded_block_end = .;
57        KEEP (*(.reset))
58        /* TODO revisit this now memset/memcpy/float in ROM */
59        /* bit of a hack right now to exclude all floating point and time critical (e.g. memset, memcpy) code from
60         * FLASH ... we will include any thing excluded here in .data below by default */
61        *(.init)
62        *libgcc.a:cmse_nonsecure_call.o
63        *(EXCLUDE_FILE(*libgcc.a: *libc.a:*lib_a-mem*.o *libm.a:) .text*)
64        *(.fini)
65        /* Pull all c'tors into .text */
66        *crtbegin.o(.ctors)
67        *crtbegin?.o(.ctors)
68        *(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors)
69        *(SORT(.ctors.*))
70        *(.ctors)
71        /* Followed by destructors */
72        *crtbegin.o(.dtors)
73        *crtbegin?.o(.dtors)
74        *(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors)
75        *(SORT(.dtors.*))
76        *(.dtors)
77
78        . = ALIGN(4);
79        /* preinit data */
80        PROVIDE_HIDDEN (__preinit_array_start = .);
81        KEEP(*(SORT(.preinit_array.*)))
82        KEEP(*(.preinit_array))
83        PROVIDE_HIDDEN (__preinit_array_end = .);
84
85        . = ALIGN(4);
86        /* init data */
87        PROVIDE_HIDDEN (__init_array_start = .);
88        KEEP(*(SORT(.init_array.*)))
89        KEEP(*(.init_array))
90        PROVIDE_HIDDEN (__init_array_end = .);
91
92        . = ALIGN(4);
93        /* finit data */
94        PROVIDE_HIDDEN (__fini_array_start = .);
95        *(SORT(.fini_array.*))
96        *(.fini_array)
97        PROVIDE_HIDDEN (__fini_array_end = .);
98
99        *(.eh_frame*)
100        . = ALIGN(4);
101    } > FLASH
102
103    /* Note the boot2 section is optional, and should be discarded if there is
104       no reference to it *inside* the binary, as it is not called by the
105       bootrom. (The bootrom performs a simple best-effort XIP setup and
106       leaves it to the binary to do anything more sophisticated.) However
107       there is still a size limit of 256 bytes, to ensure the boot2 can be
108       stored in boot RAM.
109
110       Really this is a "XIP setup function" -- the name boot2 is historic and
111       refers to its dual-purpose on RP2040, where it also handled vectoring
112       from the bootrom into the user image.
113    */
114
115    .boot2 : {
116        __boot2_start__ = .;
117        *(.boot2)
118        __boot2_end__ = .;
119    } > FLASH
120
121    ASSERT(__boot2_end__ - __boot2_start__ <= 256,
122        "ERROR: Pico second stage bootloader must be no more than 256 bytes in size")
123
124    .rodata : {
125        *(EXCLUDE_FILE(*libgcc.a: *libc.a:*lib_a-mem*.o *libm.a:) .rodata*)
126        *(.srodata*)
127        . = ALIGN(4);
128        *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.flashdata*)))
129        . = ALIGN(4);
130    } > FLASH
131
132    .ARM.extab :
133    {
134        *(.ARM.extab* .gnu.linkonce.armextab.*)
135    } > FLASH
136
137    __exidx_start = .;
138    .ARM.exidx :
139    {
140        *(.ARM.exidx* .gnu.linkonce.armexidx.*)
141    } > FLASH
142    __exidx_end = .;
143
144    /* Machine inspectable binary information */
145    . = ALIGN(4);
146    __binary_info_start = .;
147    .binary_info :
148    {
149        KEEP(*(.binary_info.keep.*))
150        *(.binary_info.*)
151    } > FLASH
152    __binary_info_end = .;
153    . = ALIGN(4);
154
155    .ram_vector_table (NOLOAD): {
156        *(.ram_vector_table)
157    } > RAM
158
159    .uninitialized_data (NOLOAD): {
160        . = ALIGN(4);
161        *(.uninitialized_data*)
162    } > RAM
163
164    .data : {
165        __data_start__ = .;
166        *(vtable)
167
168        *(.time_critical*)
169
170        /* remaining .text and .rodata; i.e. stuff we exclude above because we want it in RAM */
171        *(.text*)
172        . = ALIGN(4);
173        *(.rodata*)
174        . = ALIGN(4);
175
176        *(.data*)
177        *(.sdata*)
178
179        . = ALIGN(4);
180        *(.after_data.*)
181        . = ALIGN(4);
182        /* preinit data */
183        PROVIDE_HIDDEN (__mutex_array_start = .);
184        KEEP(*(SORT(.mutex_array.*)))
185        KEEP(*(.mutex_array))
186        PROVIDE_HIDDEN (__mutex_array_end = .);
187
188        *(.jcr)
189        . = ALIGN(4);
190    } > RAM AT> FLASH
191
192    .tdata : {
193        . = ALIGN(4);
194		*(.tdata .tdata.* .gnu.linkonce.td.*)
195        /* All data end */
196        __tdata_end = .;
197    } > RAM AT> FLASH
198    PROVIDE(__data_end__ = .);
199
200    /* __etext is (for backwards compatibility) the name of the .data init source pointer (...) */
201    __etext = LOADADDR(.data);
202
203    .tbss (NOLOAD) : {
204        . = ALIGN(4);
205        __bss_start__ = .;
206        __tls_base = .;
207        *(.tbss .tbss.* .gnu.linkonce.tb.*)
208        *(.tcommon)
209
210        __tls_end = .;
211    } > RAM
212
213    .bss (NOLOAD) : {
214        . = ALIGN(4);
215        __tbss_end = .;
216
217        *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.bss*)))
218        *(COMMON)
219        PROVIDE(__global_pointer$ = . + 2K);
220        *(.sbss*)
221        . = ALIGN(4);
222        __bss_end__ = .;
223    } > RAM
224
225    .heap (NOLOAD):
226    {
227        __end__ = .;
228        end = __end__;
229        KEEP(*(.heap*))
230    } > RAM
231    /* historically on GCC sbrk was growing past __HeapLimit to __StackLimit, however
232       to be more compatible, we now set __HeapLimit explicitly to where the end of the heap is */
233    __HeapLimit = ORIGIN(RAM) + LENGTH(RAM);
234
235    /* Start and end symbols must be word-aligned */
236    .scratch_x : {
237        __scratch_x_start__ = .;
238        *(.scratch_x.*)
239        . = ALIGN(4);
240        __scratch_x_end__ = .;
241    } > SCRATCH_X AT > FLASH
242    __scratch_x_source__ = LOADADDR(.scratch_x);
243
244    .scratch_y : {
245        __scratch_y_start__ = .;
246        *(.scratch_y.*)
247        . = ALIGN(4);
248        __scratch_y_end__ = .;
249    } > SCRATCH_Y AT > FLASH
250    __scratch_y_source__ = LOADADDR(.scratch_y);
251
252    /* .stack*_dummy section doesn't contains any symbols. It is only
253     * used for linker to calculate size of stack sections, and assign
254     * values to stack symbols later
255     *
256     * stack1 section may be empty/missing if platform_launch_core1 is not used */
257
258    /* by default we put core 0 stack at the end of scratch Y, so that if core 1
259     * stack is not used then all of SCRATCH_X is free.
260     */
261    .stack1_dummy (NOLOAD):
262    {
263        *(.stack1*)
264    } > SCRATCH_X
265    .stack_dummy (NOLOAD):
266    {
267        KEEP(*(.stack*))
268    } > SCRATCH_Y
269
270    .flash_end : {
271        KEEP(*(.embedded_end_block*))
272        PROVIDE(__flash_binary_end = .);
273    } > FLASH =0xaa
274
275    /* stack limit is poorly named, but historically is maximum heap ptr */
276    __StackLimit = ORIGIN(RAM) + LENGTH(RAM);
277    __StackOneTop = ORIGIN(SCRATCH_X) + LENGTH(SCRATCH_X);
278    __StackTop = ORIGIN(SCRATCH_Y) + LENGTH(SCRATCH_Y);
279    __StackOneBottom = __StackOneTop - SIZEOF(.stack1_dummy);
280    __StackBottom = __StackTop - SIZEOF(.stack_dummy);
281    PROVIDE(__stack = __StackTop);
282
283    /* picolibc and LLVM */
284    PROVIDE (__heap_start = __end__);
285    PROVIDE (__heap_end = __HeapLimit);
286    PROVIDE( __tls_align = MAX(ALIGNOF(.tdata), ALIGNOF(.tbss)) );
287    PROVIDE( __tls_size_align = (__tls_size + __tls_align - 1) & ~(__tls_align - 1));
288    PROVIDE( __arm32_tls_tcb_offset = MAX(8, __tls_align) );
289
290    /* llvm-libc */
291    PROVIDE (_end = __end__);
292    PROVIDE (__llvm_libc_heap_limit = __HeapLimit);
293
294    /* Check if data + heap + stack exceeds RAM limit */
295    ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed")
296
297    ASSERT( __binary_info_header_end - __logical_binary_start <= 1024, "Binary info must be in first 1024 bytes of the binary")
298    ASSERT( __embedded_block_end - __logical_binary_start <= 4096, "Embedded block must be in first 4096 bytes of the binary")
299
300    /* todo assert on extra code */
301}
302
303