STM32F103C8Tx 链接脚本(Linker Script)详细分析
核心作用
-
定义存储器分布:明确指定芯片的
FLASH和RAM的物理地址范围(如FLASH: 0x8000000-0x8010000,RAM: 0x20000000-0x20005000)。 -
分配程序段(Sections):将代码、数据、堆栈等分配到正确的存储区域(如代码放
FLASH,变量放RAM)。 -
确保硬件兼容性:满足 STM32 的硬件要求(如中断向量表必须对齐到
FLASH起始地址)。
1. 基本配置
-
入口点:
ENTRY(Reset_Handler)
程序从启动文件中的Reset_Handler函数开始执行(初始化硬件和跳转到main)。 -
栈顶地址:
_estack = ORIGIN(RAM) + LENGTH(RAM)
栈顶位于 RAM 的末尾(0x20000000 + 20K = 0x20005000)。 -
堆/栈最小尺寸:
_Min_Heap_Size = 0x200; // 512 字节堆
_Min_Stack_Size = 0x400; // 1024 字节栈若堆栈空间不足,链接器将报错。
2. 存储器布局 (MEMORY)
| 存储器 | 类型 | 起始地址 | 长度 | 用途 |
|---|---|---|---|---|
FLASH |
rx |
0x8000000 |
64K |
代码、常量数据 |
RAM |
xrw |
0x20000000 |
20K |
变量、堆栈 |
3. 段 (SECTIONS) 分配
(1) .isr_vector(中断向量表)
-
位置:
FLASH起始位置(0x8000000)。 -
作用:存储中断服务函数的入口地址。
-
关键指令:
KEEP(*(.isr_vector))确保向量表不被优化删除。
(2) .text(程序代码)
-
内容:代码(
.text)、内联函数(.text*)、ARM/Thumb 切换(.glue_7,.glue_7t)。 -
符号:
_etext标记代码段结束地址(用于初始化数据复制)。
(3) .rodata(只读数据)
-
内容:常量字符串、全局常量(
const变量)。 -
对齐:
ALIGN(4)确保 4 字节对齐(STM32 内存访问优化)。
(4) ARM 特定段
-
.ARM.extab:异常处理元数据。 -
.ARM.exidx:异常索引表(用于栈展开),由__exidx_start/end标记范围。
(5) 初始化数组
-
.preinit_array:构造函数前调用的函数指针数组。 -
.init_array:全局对象构造函数指针数组(按优先级排序)。 -
.fini_array:析构函数指针数组。
(6) .data(已初始化数据)
-
存储策略:
-
加载地址 (LMA):
FLASH(_sidata = LOADADDR(.data))。 -
运行地址 (VMA):
RAM(_sdata到_edata)。
-
-
启动流程:上电后启动代码将数据从
FLASH复制到RAM。
(7) .bss(未初始化数据)
-
内容:全局/静态变量(初始值为 0 或未显式初始化)。
-
符号:
_sbss和_ebss标记段的起止地址。 -
启动流程:启动代码将此区域清零。
(8) .user_heap_stack(堆栈空间)
-
布局:
text
RAM 末端 = . (当前地址) | 堆空间 (_Min_Heap_Size) | | 栈空间 (_Min_Stack_Size) |
-
符号:
end和_end指向堆的起始地址。
(9) 丢弃的库
-
指令:
/DISCARD/ : { libc.a, libm.a, libgcc.a }
避免链接标准库的重复部分(由启动文件提供必要实现)。
4. 关键符号总结
| 符号 | 含义 | 用途 |
|---|---|---|
_estack |
栈顶地址 (RAM 末尾) | 设置主栈指针 (MSP) |
_etext |
代码段结束地址 | 定位 .data 的 LMA |
_sidata |
.data 在 FLASH 中的加载地址 |
启动代码复制源地址 |
_sdata/_edata |
.data 在 RAM 中的起止地址 |
启动代码复制目标地址 |
_sbss/_ebss |
.bss 的起止地址 |
启动代码清零区域 |
end / _end |
堆的起始地址 | sbrk() 函数管理堆内存 |
5. 内存映射示例
text
FLASH (64K) ├─ 0x8000000: .isr_vector ├─ ... .text ├─ ... .rodata └─ ... .data 的 LMA (初始化数据副本) RAM (20K) ├─ 0x20000000: .data (运行时) ├─ ... .bss ├─ ... Heap (512B) └─ 0x20005000: Stack (1024B, 向下增长)
6. 总结
这个链接脚本的本质是 为STM32F103C8Tx芯片定制内存世界的“地图”,它决定了:
-
代码、常量、变量住在哪个“房间”(地址)。
-
启动时如何“搬家”(初始化数据)。
-
堆栈的“地盘”有多大。
-
硬件如何安全访问这些数据。
如果没有它,链接器会使用默认布局,几乎必然导致STM32无法正常运行(尤其是中断向量表和RAM初始化)。
7. 具体代码
/*
******************************************************************************
**
** File : LinkerScript.ld
**
** Author : STM32CubeMX
**
** Abstract : Linker script for STM32F103C8Tx series
** 64Kbytes FLASH and 20Kbytes 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
**
** <h2><center>© COPYRIGHT(c) 2019 STMicroelectronics</center></h2>
**
** Redistribution and use in source and binary forms, with or without modification,
** are permitted provided that the following conditions are met:
** 1. Redistributions of source code must retain the above copyright notice,
** this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright notice,
** this list of conditions and the following disclaimer in the documentation
** and/or other materials provided with the distribution.
** 3. Neither the name of STMicroelectronics nor the names of its contributors
** may be used to endorse or promote products derived from this software
** without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
** DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
*****************************************************************************
*/
/* Entry Point */
ENTRY(Reset_Handler)
/* Highest address of the user mode stack */
_estack = ORIGIN(RAM) + LENGTH(RAM); /* 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 = 20K
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 64K
}
/* 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
/* 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) }
}
更多推荐



所有评论(0)