.ld文件的作用
1.定义程序入口地址
2.定义Flash、RAM中代码和数据的存放位置
/* Entry Point */
/* 程序入口——程序将从Reset Handler开始执行,而该函数定义在stm32fxxx.s启动文件中。
ENTRY(Reset_Handler)
/* Highest address of the user mode stack */
/* end of stack 堆栈末尾 = RAM起始地址 + RAM空间大小 */
_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 */
/* 单片机内部存储空间 RAM FLASH起始位置和大小的声明 */
MEMORY
{
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 512K
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 2048K
}
/* Sections */
SECTIONS
{
/* The startup code into "FLASH" Rom type memory */
/* 中断向量表定义于 .s启动文件中,应位于Flash的最前端,也就是从0x8000000的位置开始 */
.isr_vector :
{
/* 字对齐 */
. = ALIGN(4);
/* 将中断向量的内容链接到该地址 */
KEEP(*(.isr_vector)) /* Startup code */
. = ALIGN(4);
} >FLASH
/* The program code and other data into "FLASH" Rom type memory */
/* .text对应程序的可执行代码 */
.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() 的作用是当启用连接器的--gc-sections垃圾回收选项时,这部分不能被回收
参考链接 https://blog.youkuaiyun.com/wangbinyantai/article/details/79001303 */
KEEP (*(.init))
KEEP (*(.fini))
. = ALIGN(4);
/* _etext是链接器的预定义变量,代表程序正文段结束的第一个地址 */
_etext = .; /* define a global symbols at end of code */
} >FLASH
/* Constant data into "FLASH" Rom type memory */
/* .rodata代表程序中使用的常量表格数据等 */
.rodata :
{
/* 前后字对齐 */
. = ALIGN(4);
*(.rodata) /* .rodata sections (constants, strings, etc.) */
*(.rodata*) /* .rodata* sections (constants, strings, etc.) */
. = ALIGN(4);
} >FLASH
/* ?如果有朋友清楚下面这段定义的含义请评论告诉我,多谢!*/
.ARM.extab : {
. = ALIGN(4);
*(.ARM.extab* .gnu.linkonce.armextab.*)
. = ALIGN(4);
} >FLASH
.ARM : {

本文详细解析.ld文件在STM32程序中的作用,包括定义程序入口、内存布局、代码和数据存放位置。深入探讨启动文件如何初始化堆栈、数据段和.bss段,以及中断向量表的配置。
最低0.47元/天 解锁文章
1359





