Skip to content

Commit 3e8580a

Browse files
committed
ports/rp2: Per-board linker scripts.
Add per-board linker scripts to avoid silently overflowing flash. Excludes region allocated to user filesystem from FLASH section. Signed-off-by: Phil Howard <phil@pimoroni.com>
1 parent d75892c commit 3e8580a

File tree

5 files changed

+1021
-1
lines changed

5 files changed

+1021
-1
lines changed

ports/rp2/CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,10 @@ target_link_libraries(${MICROPY_TARGET}
356356
${PICO_SDK_COMPONENTS}
357357
)
358358

359+
target_link_options(${MICROPY_TARGET} PRIVATE
360+
-Wl,--print-memory-usage
361+
)
362+
359363
if (MICROPY_HW_ENABLE_DOUBLE_TAP)
360364
# Enable double tap reset into bootrom.
361365
target_link_libraries(${MICROPY_TARGET}
@@ -367,7 +371,11 @@ endif()
367371
# a linker script modification) until we explicitly add macro calls around the function
368372
# defs to move them into RAM.
369373
if (PICO_ON_DEVICE AND NOT PICO_NO_FLASH AND NOT PICO_COPY_TO_RAM)
370-
pico_set_linker_script(${MICROPY_TARGET} ${CMAKE_CURRENT_LIST_DIR}/memmap_mp.ld)
374+
if(EXISTS ${MICROPY_BOARD_DIR}/memmap_mp.ld)
375+
pico_set_linker_script(${MICROPY_TARGET} ${MICROPY_BOARD_DIR}/memmap_mp.ld)
376+
else()
377+
pico_set_linker_script(${MICROPY_TARGET} ${CMAKE_CURRENT_LIST_DIR}/memmap_mp.ld)
378+
endif()
371379
endif()
372380

373381
pico_add_extra_outputs(${MICROPY_TARGET})

ports/rp2/boards/PICO/memmap_mp.ld

Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
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+
24+
MEMORY
25+
{
26+
FLASH(rx) : ORIGIN = 0x10000000, LENGTH = 640k
27+
RAM(rwx) : ORIGIN = 0x20000000, LENGTH = 256k
28+
SCRATCH_X(rwx) : ORIGIN = 0x20040000, LENGTH = 4k
29+
SCRATCH_Y(rwx) : ORIGIN = 0x20041000, LENGTH = 4k
30+
}
31+
32+
ENTRY(_entry_point)
33+
34+
SECTIONS
35+
{
36+
/* Second stage bootloader is prepended to the image. It must be 256 bytes big
37+
and checksummed. It is usually built by the boot_stage2 target
38+
in the Raspberry Pi Pico SDK
39+
*/
40+
41+
.flash_begin : {
42+
__flash_binary_start = .;
43+
} > FLASH
44+
45+
.boot2 : {
46+
__boot2_start__ = .;
47+
KEEP (*(.boot2))
48+
__boot2_end__ = .;
49+
} > FLASH
50+
51+
ASSERT(__boot2_end__ - __boot2_start__ == 256,
52+
"ERROR: Pico second stage bootloader must be 256 bytes in size")
53+
54+
/* The second stage will always enter the image at the start of .text.
55+
The debugger will use the ELF entry point, which is the _entry_point
56+
symbol if present, otherwise defaults to start of .text.
57+
This can be used to transfer control back to the bootrom on debugger
58+
launches only, to perform proper flash setup.
59+
*/
60+
61+
.text : {
62+
__logical_binary_start = .;
63+
KEEP (*(.vectors))
64+
KEEP (*(.binary_info_header))
65+
__binary_info_header_end = .;
66+
KEEP (*(.reset))
67+
/* TODO revisit this now memset/memcpy/float in ROM */
68+
/* bit of a hack right now to exclude all floating point and time critical (e.g. memset, memcpy) code from
69+
* FLASH ... we will include any thing excluded here in .data below by default */
70+
*(.init)
71+
/* Change for MicroPython... excluse gc.c, parse.c, vm.c from flash */
72+
*(EXCLUDE_FILE(*libgcc.a: *libc.a: *lib_a-mem*.o *libm.a: *gc.c.obj *vm.c.obj *parse.c.obj) .text*)
73+
*(.fini)
74+
/* Pull all c'tors into .text */
75+
*crtbegin.o(.ctors)
76+
*crtbegin?.o(.ctors)
77+
*(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors)
78+
*(SORT(.ctors.*))
79+
*(.ctors)
80+
/* Followed by destructors */
81+
*crtbegin.o(.dtors)
82+
*crtbegin?.o(.dtors)
83+
*(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors)
84+
*(SORT(.dtors.*))
85+
*(.dtors)
86+
87+
*(.eh_frame*)
88+
. = ALIGN(4);
89+
} > FLASH
90+
91+
.rodata : {
92+
*(EXCLUDE_FILE(*libgcc.a: *libc.a:*lib_a-mem*.o *libm.a:) .rodata*)
93+
. = ALIGN(4);
94+
*(SORT_BY_ALIGNMENT(SORT_BY_NAME(.flashdata*)))
95+
. = ALIGN(4);
96+
} > FLASH
97+
98+
.ARM.extab :
99+
{
100+
*(.ARM.extab* .gnu.linkonce.armextab.*)
101+
} > FLASH
102+
103+
__exidx_start = .;
104+
.ARM.exidx :
105+
{
106+
*(.ARM.exidx* .gnu.linkonce.armexidx.*)
107+
} > FLASH
108+
__exidx_end = .;
109+
110+
/* Machine inspectable binary information */
111+
. = ALIGN(4);
112+
__binary_info_start = .;
113+
.binary_info :
114+
{
115+
KEEP(*(.binary_info.keep.*))
116+
*(.binary_info.*)
117+
} > FLASH
118+
__binary_info_end = .;
119+
. = ALIGN(4);
120+
121+
/* End of .text-like segments */
122+
__etext = .;
123+
124+
.ram_vector_table (COPY): {
125+
*(.ram_vector_table)
126+
} > RAM
127+
128+
.data : {
129+
__data_start__ = .;
130+
*(vtable)
131+
132+
*(.time_critical*)
133+
134+
/* remaining .text and .rodata; i.e. stuff we exclude above because we want it in RAM */
135+
*(.text*)
136+
. = ALIGN(4);
137+
*(.rodata*)
138+
. = ALIGN(4);
139+
140+
*(.data*)
141+
142+
. = ALIGN(4);
143+
*(.after_data.*)
144+
. = ALIGN(4);
145+
/* preinit data */
146+
PROVIDE_HIDDEN (__mutex_array_start = .);
147+
KEEP(*(SORT(.mutex_array.*)))
148+
KEEP(*(.mutex_array))
149+
PROVIDE_HIDDEN (__mutex_array_end = .);
150+
151+
. = ALIGN(4);
152+
/* preinit data */
153+
PROVIDE_HIDDEN (__preinit_array_start = .);
154+
KEEP(*(SORT(.preinit_array.*)))
155+
KEEP(*(.preinit_array))
156+
PROVIDE_HIDDEN (__preinit_array_end = .);
157+
158+
. = ALIGN(4);
159+
/* init data */
160+
PROVIDE_HIDDEN (__init_array_start = .);
161+
KEEP(*(SORT(.init_array.*)))
162+
KEEP(*(.init_array))
163+
PROVIDE_HIDDEN (__init_array_end = .);
164+
165+
. = ALIGN(4);
166+
/* finit data */
167+
PROVIDE_HIDDEN (__fini_array_start = .);
168+
*(SORT(.fini_array.*))
169+
*(.fini_array)
170+
PROVIDE_HIDDEN (__fini_array_end = .);
171+
172+
*(.jcr)
173+
. = ALIGN(4);
174+
/* All data end */
175+
__data_end__ = .;
176+
} > RAM AT> FLASH
177+
178+
.uninitialized_data (COPY): {
179+
. = ALIGN(4);
180+
*(.uninitialized_data*)
181+
} > RAM
182+
183+
/* Start and end symbols must be word-aligned */
184+
.scratch_x : {
185+
__scratch_x_start__ = .;
186+
*(.scratch_x.*)
187+
. = ALIGN(4);
188+
__scratch_x_end__ = .;
189+
} > SCRATCH_X AT > FLASH
190+
__scratch_x_source__ = LOADADDR(.scratch_x);
191+
192+
.scratch_y : {
193+
__scratch_y_start__ = .;
194+
*(.scratch_y.*)
195+
. = ALIGN(4);
196+
__scratch_y_end__ = .;
197+
} > SCRATCH_Y AT > FLASH
198+
__scratch_y_source__ = LOADADDR(.scratch_y);
199+
200+
.bss : {
201+
. = ALIGN(4);
202+
__bss_start__ = .;
203+
*(SORT_BY_ALIGNMENT(SORT_BY_NAME(.bss*)))
204+
*(COMMON)
205+
. = ALIGN(4);
206+
__bss_end__ = .;
207+
} > RAM
208+
209+
.heap (COPY):
210+
{
211+
__end__ = .;
212+
end = __end__;
213+
*(.heap*)
214+
__HeapLimit = .;
215+
} > RAM
216+
217+
/* .stack*_dummy section doesn't contains any symbols. It is only
218+
* used for linker to calculate size of stack sections, and assign
219+
* values to stack symbols later
220+
*
221+
* stack1 section may be empty/missing if platform_launch_core1 is not used */
222+
223+
/* by default we put core 0 stack at the end of scratch Y, so that if core 1
224+
* stack is not used then all of SCRATCH_X is free.
225+
*/
226+
.stack1_dummy (COPY):
227+
{
228+
*(.stack1*)
229+
} > SCRATCH_X
230+
.stack_dummy (COPY):
231+
{
232+
*(.stack*)
233+
} > SCRATCH_Y
234+
235+
.flash_end : {
236+
__flash_binary_end = .;
237+
} > FLASH
238+
239+
/* stack limit is poorly named, but historically is maximum heap ptr */
240+
__StackLimit = ORIGIN(RAM) + LENGTH(RAM);
241+
__StackOneTop = ORIGIN(SCRATCH_X) + LENGTH(SCRATCH_X);
242+
__StackTop = ORIGIN(SCRATCH_Y) + LENGTH(SCRATCH_Y);
243+
__StackOneBottom = __StackOneTop - SIZEOF(.stack1_dummy);
244+
__StackBottom = __StackTop - SIZEOF(.stack_dummy);
245+
PROVIDE(__stack = __StackTop);
246+
247+
/* Check if data + heap + stack exceeds RAM limit */
248+
ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed")
249+
250+
ASSERT( __binary_info_header_end - __logical_binary_start <= 256, "Binary info must be in first 256 bytes of the binary")
251+
/* todo assert on extra code */
252+
}
253+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy