我有一台带有 1 MB 内部闪存的 f7。由于国际芯片短缺,我必须应对新的挑战:
我现在有一个 f7,只有 64kB 内部闪存和 16MB 外部 qspi 闪存。
我需要引导加载程序来更新外部 qspi 闪存中的应用程序。引导加载程序不适合内部闪存。但在内存映射模式下我无法写入 qspi 内存。因此,我初始化外部 qspi 闪存,将其全部复制到 RAM(我确实有足够的 RAM)并从那里运行引导加载程序。或者我是这么想的。
这是我的链接器脚本,仅让启动脚本从外部闪存运行。但是链接器错误重定位被截断以适合: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 限制的指导,或者告诉我我做错了什么。
有更简单的方法可以做到这一点,您只需要更改链接器脚本,无需更改启动代码
/*
******************************************************************************
**
** @file : LinkerScript.ld
**
** @author : Auto-generated by STM32CubeIDE
**
** Abstract : Linker script for NUCLEO-F413ZH Board embedding STM32F413ZHTx Device from stm32f4 series
** 1536Kbytes FLASH
** 320Kbytes RAM
**
** Set heap size, stack size and stack location according
** to application requirements.
**
** Set memory bank area and size if external memory is used
**
** Target : STMicroelectronics STM32
**
** Distribution: The file is distributed as is, without any warranty
** of any kind.
**
******************************************************************************
** @attention
**
** Copyright (c) 2022 STMicroelectronics.
** All rights reserved.
**
** This software is licensed under terms that can be found in the LICENSE file
** in the root directory of this software component.
** If no LICENSE file comes with this software, it is provided AS-IS.
**
******************************************************************************
*/
/* Entry Point */
ENTRY(Reset_Handler)
/* Highest address of the user mode stack */
_estack = ORIGIN(RAM) + LENGTH(RAM); /* end of "RAM" Ram type memory */
_Min_Heap_Size = 0x200 ; /* required amount of heap */
_Min_Stack_Size = 0x400 ; /* required amount of stack */
/* Memories definition */
MEMORY
{
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 320K
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 1536K
}
/* Sections */
SECTIONS
{
/* The startup code into "FLASH" Rom type memory */
.isr_vector :
{
. = ALIGN(4);
KEEP(*(.isr_vector)) /* Startup code */
. = ALIGN(4);
} >RAM AT> FLASH
/* The program code and other data goes into FLASH */
.text.Reset_Handler :
{
. = ALIGN(4);
*(.text.Reset_Handler)
. = ALIGN(4);
} >FLASH
/* The program code and other data into "FLASH" Rom type memory */
.text :
{
. = ALIGN(4);
KEEP (*(.init))
KEEP (*(.fini))
. = ALIGN(4);
_etext = .; /* define a global symbols at end of code */
} >FLASH
/* Constant data into "FLASH" Rom type memory */
.rodata :
{
. = ALIGN(4);
. = ALIGN(4);
} >FLASH
.ARM.extab : {
. = ALIGN(4);
*(.ARM.extab* .gnu.linkonce.armextab.*)
. = ALIGN(4);
} >FLASH
.ARM : {
. = ALIGN(4);
__exidx_start = .;
*(.ARM.exidx*)
__exidx_end = .;
. = ALIGN(4);
} >FLASH
.preinit_array :
{
. = ALIGN(4);
PROVIDE_HIDDEN (__preinit_array_start = .);
KEEP (*(.preinit_array*))
PROVIDE_HIDDEN (__preinit_array_end = .);
. = ALIGN(4);
} >FLASH
.init_array :
{
. = ALIGN(4);
PROVIDE_HIDDEN (__init_array_start = .);
KEEP (*(SORT(.init_array.*)))
KEEP (*(.init_array*))
PROVIDE_HIDDEN (__init_array_end = .);
. = ALIGN(4);
} >FLASH
.fini_array :
{
. = ALIGN(4);
PROVIDE_HIDDEN (__fini_array_start = .);
KEEP (*(SORT(.fini_array.*)))
KEEP (*(.fini_array*))
PROVIDE_HIDDEN (__fini_array_end = .);
. = ALIGN(4);
} >FLASH
/* Used by the startup to initialize data */
_sidata = LOADADDR(.data);
/* Initialized data sections into "RAM" Ram type memory */
.data :
{
. = ALIGN(4);
_sdata = .; /* create a global symbol at data start */
*(.data) /* .data sections */
*(.data*) /* .data* sections */
*(.RamFunc) /* .RamFunc sections */
*(.RamFunc*) /* .RamFunc* sections */
*(.text) /* .text sections (code) */
*(.text*) /* .text* sections (code) */
*(.glue_7) /* glue arm to thumb code */
*(.glue_7t) /* glue thumb to arm code */
*(.eh_frame)
*(.rodata) /* .rodata sections (constants, strings, etc.) */
*(.rodata*) /* .rodata* sections (constants, strings, etc.) */
. = ALIGN(4);
_edata = .; /* define a global symbol at data end */
} >RAM AT> FLASH
/* Uninitialized data section into "RAM" Ram type memory */
. = ALIGN(4);
.bss :
{
/* This is used by the startup in order to initialize the .bss section */
_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" Ram type memory left */
._user_heap_stack :
{
. = ALIGN(8);
PROVIDE ( end = . );
PROVIDE ( _end = . );
. = . + _Min_Heap_Size;
. = . + _Min_Stack_Size;
. = ALIGN(8);
} >RAM
/* Remove information from the compiler libraries */
/DISCARD/ :
{
libc.a ( * )
libm.a ( * )
libgcc.a ( * )
}
.ARM.attributes 0 : { *(.ARM.attributes) }
}
经过两天的摆弄,我终于可以运行了。 我多次更改了链接描述文件,现在错误不会再出现了。
我确实在这里提供了我的代码,但请注意它没有经过优化或没有错误。
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
我希望这对您有所帮助,或者至少为您指明了正确的方向。