STM32 .ld鏈接文件分析及一次bug解決過程
問題描述
原子板的代碼中含有一個關於使用外部SRAM的功能,由於本人的開發板的SRAM只有512K,因此稍微修改了一下代碼,同時使用GCC進行編譯,但是這里卻報錯了,源碼如下:
//內存池(4字節對齊)
__align(4) u8 mem1base[MEM1_MAX_SIZE];
__align(4) u8 mem2base[MEM2_MAX_SIZE] __attribute__((at(0x68000000))); //外部SRAM內存池
__align(4) u8 mem3base[MEM3_MAX_SIZE] __attribute__((at(0x10000000))); //內部CMM內存池
這里的__align(4)
指的是4字節對齊,這是個MDK的用法,換到GCC只能使用#pragma *pack*(4)
,這里還是個小問題,改后的代碼如下:
#pragma pack(4)
u8 mem1base[MEM1_MAX_SIZE];
u8 mem2base[MEM2_MAX_SIZE] __attribute__((at(0x68000000))); //外部SRAM內存池
u8 mem3base[MEM3_MAX_SIZE] __attribute__((at(0x10000000))); //內部CMM內存池
麻煩的地方來了,gcc中不支持at直接指定變量位置的寫法,因此報錯如下:
$ make
./Src/MALLOC/malloc.c:12:1: warning: 'at' attribute directive ignored [-Wattributes]
u8 mem2base[MEM2_MAX_SIZE] __attribute__((at(0x68000000))); //外部SRAM內存池
^
./Src/MALLOC/malloc.c:13:1: warning: 'at' attribute directive ignored [-Wattributes]
u8 mem3base[MEM3_MAX_SIZE] __attribute__((at(0x10000000))); //內部CMM內存池
這里的意思是at后面的指定地址方式不支持,因此忽略掉了。這個忽略就有大問題了,上面的mem2base和mem3base是分別要放在外部SRAM和內部CMM中的,這一忽略全部放到內部RAM上,空間就不夠了:
Output/obj/out.elf section `.bss' will not fit in region `RAM'
region `RAM' overflowed by 420320 bytes
解決辦法
arm-gcc同樣支持指定變量地址,只不過語法是下面這樣的:
__attribute__ ((section ("SECTIONNAME")))
換句話說,在link文件中划分一個新的段,將這個變量放到這個段內就可以解決了。與scatter文件不同,由cubemx生成的Makefile工程使用的是ld文件,下面看一下這個ld文件是什么樣子的:
ld文件解析
先貼上部分文件內容,之后再說明
/* Entry Point */
ENTRY(Reset_Handler)
/* Highest address of the user mode stack */
_estack = 0x20020000; /* end of RAM */
/* Generate a link error if heap and stack don't fit into RAM */
_Min_Heap_Size = 0x200; /* required amount of heap */
_Min_Stack_Size = 0x400; /* required amount of stack */
/* Specify the memory areas */
MEMORY
{
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 64K
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 1024K
MALLOC(rw) : ORIGIN = 0x68000000, LENGTH = 512K
}
/* 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 :
{
. = 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 */
} >FLASH
/* Constant data goes into FLASH */
.rodata :
{
. = ALIGN(4);
*(.rodata) /* .rodata sections (constants, strings, etc.) */
*(.rodata*) /* .rodata* sections (constants, strings, etc.) */
. = ALIGN(4);
} >FLASH
.ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH
.ARM : {
__exidx_start = .;
*(.ARM.exidx*)
__exidx_end = .;
} >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
_siccmram = LOADADDR(.ccmram);
/* CCM-RAM section
*
* IMPORTANT NOTE!
* If initialized variables will be placed in this section,
* the startup code needs to be modified to copy the init-values.
*/
.ccmram :
{
. = ALIGN(4);
_sccmram = .; /* create a global symbol at ccmram start */
*(.ccmram)
*(.ccmram*)
. = ALIGN(4);
_eccmram = .; /* create a global symbol at ccmram end */
} >CCMRAM 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
.malloc :
{
. = ALIGN(4);
__MALLOC_SYMBOLS = .; /* create a global symbol at ccmram start */
*(.malloc)
*(.malloc*)
. = ALIGN(4);
__EMALLOC_SYMBOLS = .; /* create a global symbol at ccmram end */
} >MALLOC AT> FLASH
/* Remove information from the standard libraries */
/DISCARD/ :
{
libc.a ( * )
libm.a ( * )
libgcc.a ( * )
}
.ARM.attributes 0 : { *(.ARM.attributes) }
}
第一部分
/* Entry Point */
ENTRY(Reset_Handler)
/* Highest address of the user mode stack */
_estack = 0x20020000; /* end of RAM */
/* Generate a link error if heap and stack don't fit into RAM */
_Min_Heap_Size = 0x200; /* required amount of heap */
_Min_Stack_Size = 0x400; /* required amount of stack */
指定入口地址,RAM的結束地址,可以看到F407的可以被AHB總線訪問的ram由於是128k,因此結束地址是0x20020000,后面指定了堆的大小和棧的大小分別為512B和1KB。
第二部分
MEMORY
{
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 64K
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 1024K
MALLOC(rw) : ORIGIN = 0x68000000, LENGTH = 512K
}
給出地址的划分區間,這里增加了一個由malloc使用的MALLOC段,放在外部SRAM上,地址0x68000000,大小比原子開發板小一半,只有512K
第三部分
/* Define output sections */
SECTIONS
{
/*中間跳過*/
.malloc :
{
. = ALIGN(4);
__MALLOC_SYMBOLS = .; /* create a global symbol at ccmram start */
*(.malloc)
*(.malloc*)
. = ALIGN(4);
__EMALLOC_SYMBOLS = .; /* create a global symbol at ccmram end */
} >MALLOC AT> FLASH
/*結尾跳過*/
}
這一部分實際上指定了程序的各個內容該如何放置在flash上或者ram上,有幾個用法:
. = ALIGN(4);
是指4字節對齊- .,小數點表示當前的地址位置,例如
__MALLOC_SYMBOLS = .;
的意思是`__MALLOC_SYMBOLS 的地址就是.malloc段的地址 - 一般的程序中包含常見的幾個段:text(存放程序),rodata(存放被初始化的數據),data(表示初始化不為0的變量),bss(表示初始化值為默認的全局變量)
- text,rodata放在flash中,而data中的初始化值作為rodata放在flash中,變量在ram中占有空間,bss占ram空間
- 段可以自定義,如上面寫的malloc段,由於編譯obj過程中不會生成用戶自定義的段,因此在源碼中需要指定需要特殊處理的段
- 結尾的
>MALLOC
指上面花括號內的內容都放在第二部分中定義的MALLOC空間中。如果沒有AT> FLASH
,那么編譯bin文件時地址是連續的
后續
將link文件修改好后,我們得到了一個新的段MALLOC,並且將CMM也利用了起來,在c代碼中修改如下:
#pragma pack(4)
u8 mem1base[MEM1_MAX_SIZE];
u8 mem2base[MEM2_MAX_SIZE] __attribute__((section(".malloc"))); //外部SRAM內存池
u8 mem3base[MEM3_MAX_SIZE] __attribute__((section(".ccmram"))); //內部CMM內存池
這樣,mem2base和mem3base將被特殊處理,這里ld文件還有一個需要注意的地方,那就是最后結束的地方>MALLOC
要加上AT> FLASH
,如果不加上的話,bin文件將連續生成,由於我們外部sram的地址在0x68000000,因此,gcc將會把這部分當做flash的地址一部分,也就是說將會生成一個超大的bin文件!!(約1.6G)所以這一句千萬不能少。