连接脚本 ld 文件:
/* Memories definition */
MEMORY
{
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 16K
ROM (rx) : ORIGIN = 0x08000000, LENGTH = 128K
}
/* Sections */
SECTIONS
{
/* The startup code into ROM memory */
.isr_vector :
{
. = ALIGN(4);
KEEP(*(.isr_vector)) /* Startup code */
. = ALIGN(4);
} >ROM
/* The program code and other data into ROM memory */
.myvars :
{
. = ALIGN(4);
KEEP(*(.myvars*))
_smyvars = .;
*(.myvars.ROM_VERSION)
*(.myvars.CRC)
*(.myvars.LENGTH)
*(.myvars.BUILD_ID)
*(.myvars*);
} > ROM
.text :
{
. = ALIGN(4);
_stext = .;
*(.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 */
} >ROM
/* Constant data into ROM memory*/
.rodata :
{
. = ALIGN(4);
*(.rodata) /* .rodata sections (constants, strings, etc.) */
*(.rodata*) /* .rodata* sections (constants, strings, etc.) */
. = ALIGN(4);
} >ROM
自定义的section段即:
.myvars :
{
. = ALIGN(4);
KEEP(*(.myvars*))
_smyvars = .;
*(.myvars.ROM_VERSION)
*(.myvars.CRC)
*(.myvars.LENGTH)
*(.myvars.BUILD_ID)
*(.myvars*);
} > ROM
在C代码中使用:
#define MYVARS_START _smyvars
extern const int MYVARS_START;
#pragma optimize=none
__attribute__((section(".myvars.ROM_VERSION"))) const char ROM_VERSION[16]= FW_VERSION_BOARD;
__attribute__((section(".myvars.CRC"))) const uint32_t CRC;
__attribute__((section(".myvars.LENGTH"))) const uint32_t LENGTH = (uint32_t) &TotalFlashUsed;
__attribute__((section(".myvars.BUILD_ID"))) const uint32_t BUILD_ID=0xAAAAAAAA;
void rom_info(void)
{
myvars_start_addr = (uint32_t)&MYVARS_START;
logi("myvars offset:%#x\r\n",myvars_start_addr );
logi("version number =%s \r\n",ROM_VERSION);
logi("TotalFlashUsed =%d \r\n",LENGTH);
}
_smyvars 为自定义段的起始地址, 该地址位于 中断向量表的后面,芯片信号选定后这个起始地址一般就是固定。