STM32 链接器脚本仅用于来自 FLASH 的启动脚本,其他一切来自 RAM
Posted
技术标签:
【中文标题】STM32 链接器脚本仅用于来自 FLASH 的启动脚本,其他一切来自 RAM【英文标题】:STM32 linker script for only startup script from FLASH, everything else from RAM 【发布时间】:2021-11-14 04:57:11 【问题描述】:我有一个带有 1 MB 内部闪存的 f7。由于国际芯片短缺,我必须应对新的挑战:
我现在有一个只有 64kB 内部闪存和 16MB 外部 qspi 闪存的 f7。
我需要引导加载程序来更新外部 qspi 闪存中的应用程序。引导加载程序不适合内部闪存。但是在内存映射模式下我无法写入 qspi 内存。所以我初始化外部 qspi 闪存,将其全部复制到 RAM 中(我确实有足够的 RAM)并从那里运行引导加载程序。或者我是这么想的。
这是我认为只让启动脚本从外部闪存运行的链接器脚本。但是链接器错误 relocation truncated truncated to fit: R_ARM_PREL31 against `.text' 出现了。
MEMORY
FLASH (xr) : ORIGIN = 0x90F00000, LENGTH = 1M
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 316K
/* Define output sections */
SECTIONS
/* The startup code goes first into FLASH */
.isr_vector :
. = ALIGN(4);
KEEP(*(.isr_vector)) /* Startup code */
. = ALIGN(4);
>FLASH
/* The program code and other data goes into FLASH */
.text.Reset_Handler :
. = ALIGN(4);
*(.text)
*(.text*)
. = ALIGN(4);
>FLASH
.text :
. = ALIGN(4);
*(.text) /* .text sections (code) */
*(.text*) /* .text* sections (code) */
*(.glue_7) /* glue arm to thumb code */
*(.glue_7t) /* glue thumb to arm code */
*(.eh_frame)
KEEP (*(.init))
KEEP (*(.fini))
. = ALIGN(4);
_etext = .; /* define a global symbols at end of code */
>RAM AT> FLASH
/* Constant data goes into FLASH */
.rodata :
. = ALIGN(4);
*(.rodata) /* .rodata sections (constants, strings, etc.) */
*(.rodata*) /* .rodata* sections (constants, strings, etc.) */
. = ALIGN(4);
>RAM AT> FLASH
.ARM.extab : *(.ARM.extab* .gnu.linkonce.armextab.*) >FLASH
.ARM :
__exidx_start = .;
*(.ARM.exidx*)
__exidx_end = .;
>RAM AT> FLASH
.preinit_array :
PROVIDE_HIDDEN (__preinit_array_start = .);
KEEP (*(.preinit_array*))
PROVIDE_HIDDEN (__preinit_array_end = .);
>FLASH
.init_array :
PROVIDE_HIDDEN (__init_array_start = .);
KEEP (*(SORT(.init_array.*)))
KEEP (*(.init_array*))
PROVIDE_HIDDEN (__init_array_end = .);
>FLASH
.fini_array :
PROVIDE_HIDDEN (__fini_array_start = .);
KEEP (*(SORT(.fini_array.*)))
KEEP (*(.fini_array*))
PROVIDE_HIDDEN (__fini_array_end = .);
>FLASH
/* used by the startup to initialize data */
_sidata = LOADADDR(.data);
/* Initialized data sections goes into RAM, load LMA copy after code */
.data :
. = ALIGN(4);
_sdata = .; /* create a global symbol at data start */
*(.data) /* .data sections */
*(.data*) /* .data* sections */
. = ALIGN(4);
_edata = .; /* define a global symbol at data end */
>RAM AT >FLASH
/* Uninitialized data section */
. = ALIGN(4);
.bss :
/* This is used by the startup in order to initialize the .bss secion */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
*(.bss*)
*(COMMON)
. = ALIGN(4);
_ebss = .; /* define a global symbol at bss end */
__bss_end__ = _ebss;
>RAM
/* User_heap_stack section, used to check that there is enough RAM left */
._user_heap_stack :
/* . = ALIGN(4); */
. = ALIGN(8);
PROVIDE ( end = . );
PROVIDE ( _end = . );
. = . + _Min_Heap_Size;
. = . + _Min_Stack_Size;
/* . = ALIGN(4); */
. = ALIGN(8);
>RAM
/* Remove information from the standard libraries */
/DISCARD/ :
libc.a ( * )
libm.a ( * )
libgcc.a ( * )
.ARM.attributes 0 : *(.ARM.attributes)
现在我想获得有关如何克服此 PREL31 限制的指导或告诉我我做错了什么。
【问题讨论】:
我认为这个错误可能来自 Cortex M7 上立即分支指令的 16MB 限制。我的猜测是编译器使用B
指令而不是BX
。您能否发布您的 C 代码,从外部闪存跳转到 RAM 或其他方式?
它是来自 STM32 Cube IDE 的标准启动脚本,应该进行复制。它在汇编程序中而不是 C 中。这是 pastebin。请关注第 74 行 ff。 pastebin.com/AJXqrkYY
【参考方案1】:
摆弄了 2 天后,我让它运行起来了。 我多次更改链接描述文件,现在错误不会再次出现。
我确实在这里提供了我的代码,但请注意它没有经过优化或没有错误。
TLDR;这是我的链接器脚本。
MEMORY
FLASH (xr) : ORIGIN = 0x90F00000, LENGTH = 1M
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 316K
/* Define output sections */
SECTIONS
/* used by the startup to initialize data */
_siisr_vector = LOADADDR(.isr_vector);
/* The startup code goes first into FLASH */
.isr_vector :
. = ALIGN(4);
_sisr_vector = .; /* create a global symbol at data start */
KEEP(*(.isr_vector)) /* Startup code */
. = ALIGN(4);
_eisr_vector = .; /* define a global symbol at data end */
>RAM AT> FLASH
/* The program code and other data goes into FLASH */
.text.Reset_Handler :
. = ALIGN(4);
*(.text.Reset_Handler)
. = ALIGN(4);
>FLASH
/* used by the startup to initialize data */
_sitext = LOADADDR(.text);
.text :
. = ALIGN(4);
_stext = .; /* create a global symbol at code start */
*(.text) /* .text sections (code) */
*(.text*) /* .text* sections (code) */
*(.glue_7) /* glue arm to thumb code */
*(.glue_7t) /* glue thumb to arm code */
*(.eh_frame)
KEEP (*(.init))
KEEP (*(.fini))
. = ALIGN(4);
_etext = .; /* define a global symbols at end of code */
>RAM AT> FLASH
/* used by the startup to initialize data */
_sirodata = LOADADDR(.rodata);
/* Constant data goes into FLASH */
.rodata :
. = ALIGN(4);
_srodata = .;
*(.rodata) /* .rodata sections (constants, strings, etc.) */
*(.rodata*) /* .rodata* sections (constants, strings, etc.) */
. = ALIGN(4);
_erodata = .;
>RAM AT> FLASH
/* used by the startup to initialize data */
_siextab = LOADADDR(.ARM.extab);
.ARM.extab :
_sextab = .;
*(.ARM.extab* .gnu.linkonce.armextab.*)
_eextab = .;
>RAM AT> FLASH
/* used by the startup to initialize data */
_siexidx = LOADADDR(.ARM);
.ARM :
__exidx_start = .;
*(.ARM.exidx*)
__exidx_end = .;
>RAM AT> FLASH
/* used by the startup to initialize data */
_sipreinit_array = LOADADDR(.preinit_array);
.preinit_array :
__preinit_array_start = .;
KEEP (*(.preinit_array*))
__preinit_array_end = .;
>RAM AT> FLASH
/* used by the startup to initialize data */
_siinit_array = LOADADDR(.init_array);
.init_array :
__init_array_start = .;
KEEP (*(SORT(.init_array.*)))
KEEP (*(.init_array*))
__init_array_end = .;
>RAM AT> FLASH
/* used by the startup to initialize data */
_sifini_array = LOADADDR(.fini_array);
.fini_array :
__fini_array_start = .;
KEEP (*(SORT(.fini_array.*)))
KEEP (*(.fini_array*))
__fini_array_end = .;
>RAM AT> FLASH
/* used by the startup to initialize data */
_sidata = LOADADDR(.data);
/* Initialized data sections goes into RAM, load LMA copy after code */
.data :
. = ALIGN(4);
_sdata = .; /* create a global symbol at data start */
*(.data) /* .data sections */
*(.data*) /* .data* sections */
. = ALIGN(4);
_edata = .; /* define a global symbol at data end */
>RAM AT> FLASH
/* Uninitialized data section */
. = ALIGN(4);
.bss :
/* This is used by the startup in order to initialize the .bss secion */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
*(.bss*)
*(COMMON)
. = ALIGN(4);
_ebss = .; /* define a global symbol at bss end */
__bss_end__ = _ebss;
>RAM
/* User_heap_stack section, used to check that there is enough RAM left */
._user_heap_stack :
. = ALIGN(8);
PROVIDE ( end = . );
PROVIDE ( _end = . );
. = . + _Min_Heap_Size;
. = . + _Min_Stack_Size;
. = ALIGN(8);
>RAM
/* Remove information from the standard libraries */
/DISCARD/ :
libc.a ( * )
libm.a ( * )
libgcc.a ( * )
.ARM.attributes 0 : *(.ARM.attributes)
感谢@Guillaume Petitjean 我发现我还应该编写启动脚本,将所有内容复制到 Ram。所以我也修改了启动汇编脚本。这里是:
.section .text.Reset_Handler
.weak Reset_Handler
.type Reset_Handler, %function
Reset_Handler:
ldr sp, =_estack /* set stack pointer */
/* Copy the data segment initializers from flash to SRAM */
movs r1, #0
b LoopCopyDataInit
CopyDataInit:
ldr r3, =_sidata
ldr r3, [r3, r1]
str r3, [r0, r1]
adds r1, r1, #4
LoopCopyDataInit:
ldr r0, =_sdata
ldr r3, =_edata
adds r2, r0, r1
cmp r2, r3
bcc CopyDataInit
movs r1, #0
b LoopCopyIsrInit
CopyIsrInit:
ldr r3, =_siisr_vector
ldr r3, [r3, r1]
str r3, [r0, r1]
adds r1, r1, #4
LoopCopyIsrInit:
ldr r0, =_sisr_vector
ldr r3, =_eisr_vector
adds r2, r0, r1
cmp r2, r3
bcc CopyIsrInit
movs r1, #0
b LoopCopyCodeInit
CopyCodeInit:
ldr r3, =_sitext
ldr r3, [r3, r1]
str r3, [r0, r1]
adds r1, r1, #4
LoopCopyCodeInit:
ldr r0, =_stext
ldr r3, =_etext
adds r2, r0, r1
cmp r2, r3
bcc CopyCodeInit
movs r1, #0
b LoopCopyRodataInit
CopyRodataInit:
ldr r3, =_sirodata
ldr r3, [r3, r1]
str r3, [r0, r1]
adds r1, r1, #4
LoopCopyRodataInit:
ldr r0, =_srodata
ldr r3, =_erodata
adds r2, r0, r1
cmp r2, r3
bcc CopyRodataInit
movs r1, #0
b LoopCopyArmExtabInit
CopyArmExtabInit:
ldr r3, =_siextab
ldr r3, [r3, r1]
str r3, [r0, r1]
adds r1, r1, #4
LoopCopyArmExtabInit:
ldr r0, =_sextab
ldr r3, =_eextab
adds r2, r0, r1
cmp r2, r3
bcc CopyArmExtabInit
movs r1, #0
b LoopCopyArmInit
CopyArmInit:
ldr r3, =_siexidx
ldr r3, [r3, r1]
str r3, [r0, r1]
adds r1, r1, #4
LoopCopyArmInit:
ldr r0, =__exidx_start
ldr r3, =__exidx_end
adds r2, r0, r1
cmp r2, r3
bcc CopyArmInit
movs r1, #0
b LoopCopyPreinitArrayInit
CopyPreinitArrayInit:
ldr r3, =_sipreinit_array
ldr r3, [r3, r1]
str r3, [r0, r1]
adds r1, r1, #4
LoopCopyPreinitArrayInit:
ldr r0, =__preinit_array_start
ldr r3, =__preinit_array_end
adds r2, r0, r1
cmp r2, r3
bcc CopyPreinitArrayInit
movs r1, #0
b LoopCopyInitArrayInit
CopyInitArrayInit:
ldr r3, =_siinit_array
ldr r3, [r3, r1]
str r3, [r0, r1]
adds r1, r1, #4
LoopCopyInitArrayInit:
ldr r0, =__init_array_start
ldr r3, =__init_array_end
adds r2, r0, r1
cmp r2, r3
bcc CopyInitArrayInit
movs r1, #0
b LoopCopyFiniArrayInit
CopyFiniArrayInit:
ldr r3, =_sifini_array
ldr r3, [r3, r1]
str r3, [r0, r1]
adds r1, r1, #4
LoopCopyFiniArrayInit:
ldr r0, =__fini_array_start
ldr r3, =__fini_array_end
adds r2, r0, r1
cmp r2, r3
bcc CopyFiniArrayInit
ldr r2, =_sbss
b LoopFillZerobss
/* Zero fill the bss segment. */
FillZerobss:
movs r3, #0
str r3, [r2], #4
LoopFillZerobss:
ldr r3, = _ebss
cmp r2, r3
bcc FillZerobss
/* Call the clock system initialization function.*/
bl SystemInit
/* Call static constructors */
bl __libc_init_array
/* Call the application's entry point.*/
bl main
我希望这可以帮助或至少为您指明正确的方向。
【讨论】:
以上是关于STM32 链接器脚本仅用于来自 FLASH 的启动脚本,其他一切来自 RAM的主要内容,如果未能解决你的问题,请参考以下文章
为啥 STM32CubeIDE 有 2 个生成的链接器脚本?
STM32CubeMX学习笔记(51)——读写内部Flash
STM32CubeMX学习笔记(51)——读写内部Flash