LibOpenCM3(三) .ld文件(连接器脚本)和startup代码说明
目录
说明
要了解 libopencm3 的代码结构, 就需要先了解它编译产生的固件结构和启动顺序, 这部分和 CMSIS 不一样
ld, Linker script, 连接器脚本文件
在 2020-11-29 的改动 drop all part specific ld files之前, 在 lib/stm32/f0 - f7 目录下, 可以看到各个型号的ld文件, 在这个提交中删除了所有具体型号的ld文件, 改为编译中使用脚本生成. 在用户的代码目录下可以看到生成的ld文件. 以下说明ld文件的功能和内容.
ld 文件的功能和结构
以ld为扩展名的文件是针对具体MCU的连接器脚本(Linker script), 连接器脚本用于在link阶段, 告诉linker(连接器)关于生成固件时需要的存储布局和根据不同内存区域的启动方式对其进行初始化. 连接器脚最关键的一点是指定了 flash 和 RAM 的起始位置和大小. 在连接阶段通过 -Tscriptname.ld
参数传递给连接器.
连接器脚本会包含以下的内容
- Memory layout 内存布局
- Entry point definition 程序入口定义
- Section definitions 各存储区的定义(定义Flash、RAM中代码和数据的存放位置)
ld 文件的示例代码说明
以下以 libopencm3 的 STM32F103x8 ld 文件为例
/* Linker script for STM32F103x8, 64k flash, 20k RAM. */
/* Define memory regions. 定义片上存储在地址空间的起始位置和大小 */
MEMORY
{
rom (rx) : ORIGIN = 0x08000000, LENGTH = 64K
ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K
}
/* Enforce emmition of the vector table. 声明中断向量表 */
EXTERN (vector_table)
/* Define the entry point of the output file. 定义程序入口, 程序将从 reset_handler 开始执行 */
ENTRY(reset_handler)
/* Define sections. */
SECTIONS
{
/** .text对应程序的可执行代码 */
.text : {
*(.vectors) /* Vector table 中断向量表 */
*(.text*) /* Program code 程序代码 */
. = ALIGN(4); /* 4字节对齐 */
*(.rodata*) /* 只读数据, 程序中使用的常量数据 */
. = ALIGN(4); /* 4字节对齐, 对应一个32bit字 */
} >rom
/* C++ Static constructors/destructors, also used for __attribute__
* ((constructor)) and the likes
* .preinit_array, .init_array, .fini_array 指向构造函数和解构函数的指针数组
*/
.preinit_array : {
. = ALIGN(4);
__preinit_array_start = .;
/* KEEP() 这个函数用在SECTIONS内部, 用于连接阶段的垃圾回收(--gc-sections参数开启), 在创建依赖树时定义这部分为root节点,
* 标记为在使用, 这样连接器就会保留这部分内存, 哪怕里面没有变量被引用.
*
* The KEEP statement within a linker script will instruct the linker to keep the specified section,
* even if no symbols inside it are referenced. This statement is used within the SECTIONS section of the
* linker script. This becomes relevant when garbage collection is performed at link time, enabled by
* passing the --gc-sections switch to the linker. The KEEP statement instructs the linker to use the
* specified section as a root node when creating a dependency graph, looking for unused sections.
* Essentially forcing the section to be marked as used.
* This statement is commonly seen in linker scripts targeting the ARM architecture for placing the
* interrupt vector table at offset 0x00000000. Without this directive the table, which might not be
* referenced explicitly in code, would be pruned out.
*/
KEEP (*(.preinit_array))
__preinit_array_end = .;
} >rom
.init_array : {
. = ALIGN(4);
__init_array_start = .;
KEEP (*(SORT(.init_array.*)))
KEEP (*(.init_array))
__init_array_end = .;
} >rom
.fini_array : {
. = ALIGN(4);
__fini_array_start = .;
KEEP (*(.fini_array))
KEEP (*(SORT(.fini_array.*)))
__fini_array_end = .;
} >rom
/*
* Another section used by C++ stuff, appears when using newlib with
* 64bit (long long) printf support
*
* C++使用的部分, 当使用带64位printf支持的newlib时需要
*/
.ARM.extab : {
*(.ARM.extab*)
} >rom
.ARM.exidx : {
__exidx_start = .;
*(.ARM.exidx*)
__exidx_end = .;
} >rom
. = ALIGN(4);
_etext = .;
/**
* .data对应已初始化的全局变量, 编译后位于可执行文件中, 由启动代码加载到数据区中
* 在单片机中这部分数据会存于flash中, 由启动代码把这部分内容拷贝到RAM
*/
.data : {
_data = .;
*(.data*) /* Read-write initialized data */
. = ALIGN(4);
_edata = .;
} >ram AT >rom
_data_loadaddr = LOADADDR(.data);
/* .bss段是没有初始值的全局变量, 由启动代码把这部分内容全初始化为0 */
.bss : {
*(.bss*) /* Read-write zero initialized data */
*(COMMON)
. = ALIGN(4);
_ebss = .;
} >ram
/*
* The .eh_frame section appears to be used for C++ exception handling.
* You may need to fix this if you're using C++.
*/
/DISCARD/ : { *(.eh_frame) }
. = ALIGN(4);
end = .;
}
PROVIDE(_stack = ORIGIN(ram) + LENGTH(ram));
ld 相关参考资料
- GCC linker script and STM32(a tutorial)
- Linker Scripts
- Understanding the linkerscript for an ARM Cortex-M microcontroller
- Understand the GNU linker script of cortex M4
- Understand the GNU assembler startup file of cortex M4
启动代码
libopencm3 和 CMSIS 不一样, 没有使用汇编代码的startup文件, 而是用 vector.c 生成startup文件, 这个文件位于 lib/cm3/ 目录下.
启动文件 vector.c
#include <libopencm3/cm3/scb.h>
// 在头文件中声明中断项列表的类型 vector_table_t
#include <libopencm3/cm3/vector.h>
// 根据不同的芯片, 引入 pre_main() 方法, STM32F1 没有对应的方法
/* load optional platform dependent initialization routines */
#include "../dispatch/vector_chipset.c"
// 弱定义, 会被实际值覆盖
/* load the weak symbols for IRQ_HANDLERS */
#include "../dispatch/vector_nvic.c"
/* Less common symbols exported by the linker script(s): */
typedef void (*funcp_t) (void);
extern funcp_t __preinit_array_start, __preinit_array_end;
extern funcp_t __init_array_start, __init_array_end;
extern funcp_t __fini_array_start, __fini_array_end;
// 主函数声明
int main(void);
// while循环函数, 空阻塞函数
void blocking_handler(void);
// 空函数
void null_handler(void);
// 定义中断向量表, 定义各个中断对应的处理函数
__attribute__ ((section(".vectors")))
vector_table_t vector_table = {
.initial_sp_value = &_stack,
.reset = reset_handler,
.nmi = nmi_handler, // Non maskable interrupt 不可屏蔽中断
.hard_fault = hard_fault_handler, // All classes of fault.
/* Those are defined only on CM3 or CM4 */
#if defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7EM__)
.memory_manage_fault = mem_manage_handler, // Memory management.
.bus_fault = bus_fault_handler, // Pre-fetch fault, memory access fault.
.usage_fault = usage_fault_handler, // Undefined instruction or illegal state.
.debug_monitor = debug_monitor_handler,
#endif
.sv_call = sv_call_handler, // System service call via SWI instruction, (Software Interrupt, SWI)软件中断指令用于产生软中断, 实现从用户模式变换到管理模式
.pend_sv = pend_sv_handler, // Pendable request for system service. 可挂起的中断
.systick = sys_tick_handler, // System tick timer
.irq = {
IRQ_HANDLERS // 中断向量的定义在芯片的irq.json中, 定义在 irq2nvic_h 脚本生成lib目录下对应型号下的 vector_nvic.c
}
};
// reset_handler 是连接器脚本中定义的程序执行入口, 下面的代码是具体的实现
void __attribute__ ((weak)) reset_handler(void)
{
volatile unsigned *src, *dest;
funcp_t *fp;
for (src = &_data_loadaddr, dest = &_data;
dest < &_edata;
src++, dest++) {
*dest = *src;
}
while (dest < &_ebss) {
*dest++ = 0;
}
/* Ensure 8-byte alignment of stack pointer on interrupts */
/* Enabled by default on most Cortex-M parts, but not M3 r1 */
SCB_CCR |= SCB_CCR_STKALIGN;
/* might be provided by platform specific vector.c */
pre_main();
/* Constructors. */
for (fp = &__preinit_array_start; fp < &__preinit_array_end; fp++) {
(*fp)();
}
for (fp = &__init_array_start; fp < &__init_array_end; fp++) {
(*fp)();
}
/* Call the application's entry point. */
(void)main();
/* Destructors. */
for (fp = &__fini_array_start; fp < &__fini_array_end; fp++) {
(*fp)();
}
}
void blocking_handler(void)
{
while (1);
}
void null_handler(void)
{
/* Do nothing. */
}
#pragma weak nmi_handler = null_handler
#pragma weak hard_fault_handler = blocking_handler
#pragma weak sv_call_handler = null_handler
#pragma weak pend_sv_handler = null_handler
#pragma weak sys_tick_handler = null_handler
/* Those are defined only on CM3 or CM4 */
#if defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7EM__)
#pragma weak mem_manage_handler = blocking_handler
#pragma weak bus_fault_handler = blocking_handler
#pragma weak usage_fault_handler = blocking_handler
#pragma weak debug_monitor_handler = null_handler
#endif
LibOpenCM3(三) .ld文件(连接器脚本)和startup代码说明的更多相关文章
- 一步一步开发Game服务器(三)加载脚本和服务器热更新(二)完整版
上一篇文章我介绍了如果动态加载dll文件来更新程序 一步一步开发Game服务器(三)加载脚本和服务器热更新 可是在使用过程中,也许有很多会发现,动态加载dll其实不方便,应为需要预先编译代码为dll文 ...
- RCP: MANIFEST.MF, plugin.xml, build.properties三种文件的区别
在Eclipse插件开发中, MANIFEST.MF, plugin.xml, build.properties是三种最常见的文件,由于它们共享同一个编辑器(Plug-in Manifest Edit ...
- Tomcat剖析(三):连接器(2)
Tomcat剖析(三):连接器(2) 1. Tomcat剖析(一):一个简单的Web服务器 2. Tomcat剖析(二):一个简单的Servlet服务器 3. Tomcat剖析(三):连接器(1) 4 ...
- Linux中/etc/fstab /etc/mtab /proc/mounts这三个文件的分析与比较 分区表位置
本文主要讲解Linux中/etc/fstab /etc/mtab /proc/mounts这三个文件的作用以及不同之处. 转自http://haohaozhang.blog.51cto.com/917 ...
- u-boot(三)启动文件
目录 u-boot(三)启动文件 汇编 C:_start_armboot 代码摘要 C:main_loop 内核启动 菜单处理(自定义实现) 命令处理 title: u-boot(三)启动文件 tag ...
- Oracle启动中,spfile.ora、init<SID>.ora、spfile<SID>.ora 这三个文件正确的先后顺序是什么?
Oracle启动中,spfile.ora.init<SID>.ora.spfile<SID>.ora 这三个文件正确的先后顺序是什么? 解答:启动数据库,使用startup命令 ...
- Unity3D开发入门教程(三)——添加启动脚本
五邑隐侠,本名关健昌,12年游戏生涯. 本教程以 Unity 3D + VS Code + C# + tolua 为例. 一.启动脚本 第一篇 "搭建开发环境",在 "配 ...
- Java基础知识笔记(三:文件与数据流)
一.输入流与输出流 输入流将数据从文件.标准输入或其他外部输入设备中加载到内存.输出流的作用则刚好相反,即将在内存中的数据保存到文件中,或传输给输出设备.输入流在Java语言中对应于抽象类java.i ...
- 用于svn添加当前目录下所有未追踪的文件,和删除所有手动删除的文件的脚本
由于要经常用到类似与 git 中的 git add --all 这种操作,但是发现svn中并不支持类似的操作. 虽然可以使用 wildcard 进行匹配,但是 wildcard是在shell中进行匹配 ...
随机推荐
- 随机数Random 和枚举enum
(一):生成随机数 要指定的话 需要在方法中指定最大的值 nextint(10)意思是 下一个的数是10需要导包:import java.util.Random; (二)枚举 enum 的语法和 ...
- 文件上传之结合phpinfo与本地文件包含利用
背景 某站点存在本地文件包含及phpinfo,可以利用其执行脚本. 原理 原理: 利用php post上传文件产生临时文件,phpinfo()读临时文件的路径和名字,本地包含漏洞生成1句话后门 1.p ...
- [学习分享] 在Windows操作系统下如何安装RMySQL包
最近在做股票的高频交易数据分析,需要用到数据库,而我只对MySQL比较熟悉,于是就安装了MySQL.当我安装好了MySQL后,正兴冲冲地准备安装RMySQL包时,问题来了:RMySQL包不支持wind ...
- Cesium入门8 - Configuring the Scene - 配置视窗
Cesium入门8 - Configuring the Scene - 配置视窗 Cesium中文网:http://cesiumcn.org/ | 国内快速访问:http://cesium.coini ...
- 桥接模式(Bridge模式)
桥接模式的定义与特点 桥接(Bridge)模式的定义如下:将抽象与实现分离,使它们可以独立变化.它是用组合关系代替继承关系来实现,从而降低了抽象和实现这两个可变维度的耦合度.通过上面的讲解,我们能很好 ...
- java多态转型II
1 package face_09; 2 3 /* 4 * 毕老师和毕姥爷的故事. 5 */ 6 class 毕姥爷 { 7 void 讲课() { 8 System.out.println(&quo ...
- 微信小程序入门教程之四:API 使用
今天是这个系列教程的最后一篇. 上一篇教程介绍了,小程序页面如何使用 JavaScript 脚本.有了脚本以后,就可以调用微信提供的各种能力(即微信 API),从而做出千变万化的页面.本篇就介绍怎么使 ...
- 简单socket服务器编程
package socket; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; ...
- 学习Java第3天
今天所做的工作: 1.循环结构 2.字符串 3.数组 4.面向对象概述 明天工作安排: 1.类和对象 2.包装类 所遇到的问题及解决方法: 1.循环foreach语句 2.字符串初始化与c++的差异 ...
- pod 详解
静态pod是由kubelet进行管理的仅存在于特定的node上的pod. pod容器共享volume同一个pod中的多个容器能够共享pod级别的存储卷volume pod的配置管理 应用配置管理方案 ...