GNU LD脚本命令语言(三)
2023-09-11 14:21:44 时间
一、目的
在《GNU LD脚本命令语言(一)》、《GNU LD脚本命令语言(二)》这两篇中我们详细介绍了GNU LD的语法知识点,本篇我们将从具体工程实例出发帮助大家更好的理解。
二、实战
链接脚本内容
使用的RT-Studio开发环境,其实编译器为arm-none-eabi-gcc,链接阶段使用的就是arm-none-eabi-ld,就是GNU LD
/*
* linker script for STM32H750XBHx with GNU ld
*/
/* Program Entry, set to mark it as "used" and avoid gc */
MEMORY
{
ROM (rx) : ORIGIN =0x08000000,LENGTH =128k
RAM (rw) : ORIGIN =0x24000000,LENGTH =512k
}
ENTRY(Reset_Handler)
_system_stack_size = 0x200;
SECTIONS
{
.text :
{
. = ALIGN(4);
_stext = .;
KEEP(*(.isr_vector)) /* Startup code */
. = ALIGN(4);
*(.text) /* remaining code */
*(.text.*) /* remaining code */
*(.rodata) /* read-only data (constants) */
*(.rodata*)
*(.glue_7)
*(.glue_7t)
*(.gnu.linkonce.t*)
/* section information for finsh shell */
. = ALIGN(4);
__fsymtab_start = .;
KEEP(*(FSymTab))
__fsymtab_end = .;
. = ALIGN(4);
__vsymtab_start = .;
KEEP(*(VSymTab))
__vsymtab_end = .;
/* section information for utest */
. = ALIGN(4);
__rt_utest_tc_tab_start = .;
KEEP(*(UtestTcTab))
__rt_utest_tc_tab_end = .;
/* section information for at server */
. = ALIGN(4);
__rtatcmdtab_start = .;
KEEP(*(RtAtCmdTab))
__rtatcmdtab_end = .;
. = ALIGN(4);
/* section information for initial. */
. = ALIGN(4);
__rt_init_start = .;
KEEP(*(SORT(.rti_fn*)))
__rt_init_end = .;
. = ALIGN(4);
PROVIDE(__ctors_start__ = .);
KEEP (*(SORT(.init_array.*)))
KEEP (*(.init_array))
PROVIDE(__ctors_end__ = .);
. = ALIGN(4);
_etext = .;
} > ROM = 0
/* .ARM.exidx is sorted, so has to go in its own output section. */
__exidx_start = .;
.ARM.exidx :
{
*(.ARM.exidx* .gnu.linkonce.armexidx.*)
/* This is used by the startup in order to initialize the .data secion */
_sidata = .;
} > ROM
__exidx_end = .;
/* .data section which is used for initialized data */
.data : AT (_sidata)
{
. = ALIGN(4);
/* This is used by the startup in order to initialize the .data secion */
_sdata = . ;
*(.data)
*(.data.*)
*(.gnu.linkonce.d*)
PROVIDE(__dtors_start__ = .);
KEEP(*(SORT(.dtors.*)))
KEEP(*(.dtors))
PROVIDE(__dtors_end__ = .);
. = ALIGN(4);
/* This is used by the startup in order to initialize the .data secion */
_edata = . ;
} >RAM
.stack :
{
. = ALIGN(4);
_sstack = .;
. = . + _system_stack_size;
. = ALIGN(4);
_estack = .;
} >RAM
__bss_start = .;
.bss :
{
. = ALIGN(4);
/* This is used by the startup in order to initialize the .bss secion */
_sbss = .;
*(.bss)
*(.bss.*)
*(COMMON)
. = ALIGN(4);
/* This is used by the startup in order to initialize the .bss secion */
_ebss = . ;
*(.bss.init)
} > RAM
__bss_end = .;
_end = .;
/* Stabs debugging sections. */
.stab 0 : { *(.stab) }
.stabstr 0 : { *(.stabstr) }
.stab.excl 0 : { *(.stab.excl) }
.stab.exclstr 0 : { *(.stab.exclstr) }
.stab.index 0 : { *(.stab.index) }
.stab.indexstr 0 : { *(.stab.indexstr) }
.comment 0 : { *(.comment) }
/* DWARF debug sections.
* Symbols in the DWARF debugging sections are relative to the beginning
* of the section so we begin them at 0. */
/* DWARF 1 */
.debug 0 : { *(.debug) }
.line 0 : { *(.line) }
/* GNU DWARF 1 extensions */
.debug_srcinfo 0 : { *(.debug_srcinfo) }
.debug_sfnames 0 : { *(.debug_sfnames) }
/* DWARF 1.1 and DWARF 2 */
.debug_aranges 0 : { *(.debug_aranges) }
.debug_pubnames 0 : { *(.debug_pubnames) }
/* DWARF 2 */
.debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }
.debug_abbrev 0 : { *(.debug_abbrev) }
.debug_line 0 : { *(.debug_line) }
.debug_frame 0 : { *(.debug_frame) }
.debug_str 0 : { *(.debug_str) }
.debug_loc 0 : { *(.debug_loc) }
.debug_macinfo 0 : { *(.debug_macinfo) }
/* SGI/MIPS DWARF 2 extensions */
.debug_weaknames 0 : { *(.debug_weaknames) }
.debug_funcnames 0 : { *(.debug_funcnames) }
.debug_typenames 0 : { *(.debug_typenames) }
.debug_varnames 0 : { *(.debug_varnames) }
}
MEMORY
{
ROM (rx) : ORIGIN =0x08000000,LENGTH =128k
RAM (rw) : ORIGIN =0x24000000,LENGTH =512k
}
上面的链接脚本片段定义了两个域,分别为ROM和RAM,具体信息为
![](https://img-blog.csdnimg.cn/img_convert/caa2bb6758e7406f1a7eac251ea0065e.png)
![](https://img-blog.csdnimg.cn/img_convert/8095e8b6af9eb97c11c107725cc9deba.png)
.text :
{
. = ALIGN(4);
_stext = .;
KEEP(*(.isr_vector)) /* Startup code */
. = ALIGN(4);
*(.text) /* remaining code */
*(.text.*) /* remaining code */
*(.rodata) /* read-only data (constants) */
*(.rodata*)
*(.glue_7)
*(.glue_7t)
*(.gnu.linkonce.t*)
/* section information for finsh shell */
. = ALIGN(4);
__fsymtab_start = .;
KEEP(*(FSymTab))
__fsymtab_end = .;
. = ALIGN(4);
__vsymtab_start = .;
KEEP(*(VSymTab))
__vsymtab_end = .;
/* section information for utest */
. = ALIGN(4);
__rt_utest_tc_tab_start = .;
KEEP(*(UtestTcTab))
__rt_utest_tc_tab_end = .;
/* section information for at server */
. = ALIGN(4);
__rtatcmdtab_start = .;
KEEP(*(RtAtCmdTab))
__rtatcmdtab_end = .;
. = ALIGN(4);
/* section information for initial. */
. = ALIGN(4);
__rt_init_start = .;
KEEP(*(SORT(.rti_fn*)))
__rt_init_end = .;
. = ALIGN(4);
PROVIDE(__ctors_start__ = .);
KEEP (*(SORT(.init_array.*)))
KEEP (*(.init_array))
PROVIDE(__ctors_end__ = .);
. = ALIGN(4);
_etext = .;
} > ROM = 0
上面的链接脚本片段定义了.text输出段,以及输出段的组成,如果没有“> ROM”这样的描述,.text输出段的首地址应该为0,但是现在因为有了这样的描述,所以现在的首地址就是0x08000000
我们打开对应工程的MAP文件
![](https://img-blog.csdnimg.cn/img_convert/7d92316c0310bb53bb552b724f531940.png)
①输出段的名字为.text,注意起始地址为0x08000000
②所有文件中的.isr_vector段,当前只有在startup_stm32h750xx.o目标文件中(源文件为startup_stm32h750xx.s)有这样的段(③)
![](https://img-blog.csdnimg.cn/img_convert/6f240c646a69508d974df135669fa427.png)
④所有的.text段
⑥对齐操作
因为代码都是存放在Flash中,所以加载地址和执行地址是一样的,我们再来看一下.data的情况
.data : AT (_sidata)
{
. = ALIGN(4);
/* This is used by the startup in order to initialize the .data secion */
_sdata = . ;
*(.data)
*(.data.*)
*(.gnu.linkonce.d*)
PROVIDE(__dtors_start__ = .);
KEEP(*(SORT(.dtors.*)))
KEEP(*(.dtors))
PROVIDE(__dtors_end__ = .);
. = ALIGN(4);
/* This is used by the startup in order to initialize the .data secion */
_edata = . ;
} >RAM
![](https://img-blog.csdnimg.cn/img_convert/23e67ba3e52267a6249fa98964091377.png)
我们知道初始化的全局变量不仅占用Flash而且运行时是在RAM执行的。
①.data是输出段名称
②输出段的加载地址
③输出段的执行地址
④所有输入段中的.data和.data.*都放入输出段.data中
对应在脚本中的加载地址和执行地址的语法
![](https://img-blog.csdnimg.cn/img_convert/32f899549fe6adb5f2fa08e8ac9efe5a.png)
①加载地址语法
②执行地址语法
通过上面的讲解,相信大家已经对GNU LD脚本已经有了一定的理解,点赞+收藏,不迷路。
相关文章
- 【C语言入门教程】2.8 C 语言的预处理命令
- Go语言基础之文件操作
- R语言基础画图/绘图/作图
- R语言数据重塑
- R语言如何将字符串转变为命令执行
- R语言与显著性检验学习笔记
- OpenGL ES着色器语言之着色概览(官方文档)
- 语言设计中的鸭子类型风格
- oracle sql语言模糊查询--通配符like的使用教程
- Python语言学习:利用python语言实现调用内部命令(python调用Shell脚本)—命令提示符cmd的几种方法
- 句子中的有效单词数-c语言描述
- c语言_双向链表代码
- 【GPT大模型】大型语言模型:基础知识及其企业应用
- Clickhouse建表语法、视图语法、数据表DDL(数据定义语言)、数据DML(数据操作语言)
- Go语言精进自学系列 | 汇总
- Go语言自学系列 | golang标准库ioutil包
- 在c# 7.3中不可用,请使用8.0或更高的语言版本
- 程序设计语言是软件的重要方面
- 设计模式(Python语言描述)全集
- 设计模式(Python语言)----代理模式
- android 在app内切换系统语言
- 挑战一晚上从零入门lua语言,直接对标Python快速上手