GNU链接脚本学习-最简链接脚本

本文详细介绍了构建STM32微控制器最小运行系统的链接脚本关键元素,包括指定入口点、定义存储介质、必要的段分配以及导出end和_end变量。链接脚本在确保程序能成功生成.bin文件的同时,也涉及到堆栈设置、内存布局和中断向量表等,对于理解STM32程序的加载和执行过程至关重要。
摘要由CSDN通过智能技术生成

最简连接脚本必备的元素

注: 这里的最简是指能成功生成.bin文件的最简,不代表一定能使MCU跑起来,要跑起来还得指定中断向量表以及堆和栈

  1. 指定入口
    ENTRY(ramcode_start), 其中ramcode_start 可以是汇编程序标号,也可以是c程序函数,但光指定入口连接器并不会把入口链接在最开头,还得在.c或.s中为ramcode_start划分一个段并在链接文件中将该段放在最前面,这样连接成的可执行文件开头的地址就是ramcode_start。

  2. 定于存储介质
    /* Specify the memory areas */
    MEMORY
    {
    FLASHCODE(rx) : ORIGIN = 0x8040000, LENGTH = 128K
    FREERAM (xrw) : ORIGIN = 0x20019000, LENGTH = 50K
    }

  3. 必要的几个段,
    必须包括.text段、.rodata段、.data段、.bss段

  4. 导出end, _end变量
    连接器是根据这两个变量值来结束链接的,内存布局的边界也是通过此变量确定的,如果链接文件中没有用PROVIDE(end = .)导出end变量,那么链接器就不知道可执行文件的结束符在哪里,链接就会报如下错误:
    在这里插入图片描述
    所以,必须在最后一个段中 PROVIDE ( end = . ); PROVIDE ( _end = . );

/*
*****************************************************************************
**
**  File        : pipe485part.ld
**
**  Abstract    : Linker script for STM32F415VG Device with
**                1024KByte FLASH, 128KByte RAM, for run a part of code in SRAM and others in code flash.
**
**                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
**
**  Environment : Atollic TrueSTUDIO(R)
**
**  Distribution: The file is distributed ?as is,? without any warranty
**                of any kind.
**
**  (c)Copyright xxg .
**  You may use this file as-is or modify it according to the needs of your
**  project. This file may only be built (assembled or compiled and linked)
**  using the Atollic TrueSTUDIO(R) product. The use of this file together
**  with other tools than Atollic TrueSTUDIO(R) is not permitted.
**
*****************************************************************************
*/

/* Entry Point */
ENTRY(ramcode_start)


/* Highest address of the user mode stack */
_estack = 0x2001FFFF;    /* end of RAM */

/* Generate a link error if heap and stack don't fit into RAM */
_Min_Heap_Size = 0x700;      /* required amount of heap  */
_Min_Stack_Size = 0x80; /* required amount of stack */



/* Specify the memory areas */
MEMORY
{
	FLASHCODE(rx) : ORIGIN = 0x8040000, LENGTH = 128K
	FREERAM (xrw) : ORIGIN = 0x20019000, LENGTH = 50K
}

/* Define output sections */
SECTIONS
{

  .FLASHCODE :
  {
	*(.FLASHCODE)
	
  } >FLASHCODE

  .text :
  {
    . = ALIGN(4);
	*(.text)           /* .text sections (code) */
    *(.text*)          /* .text* sections (code) */
    . = ALIGN(4);
  } >FREERAM
  
  .rodata :
  {
    . = ALIGN(4);
    *(.rodata)         /* .rodata sections (constants, strings, etc.) */
    *(.rodata*)        /* .rodata* sections (constants, strings, etc.) */
    . = ALIGN(4);
  } >FREERAM

 .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 */
  } >FREERAM

  . = 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;

    PROVIDE ( end = . );
    PROVIDE ( _end = . );

  } >FREERAM


/*
  ._user_heap_stack :
  {
    . = ALIGN(4);
    PROVIDE ( end = . );
    PROVIDE ( _end = . );
    . = . + _Min_Heap_Size;
    . = . + _Min_Stack_Size;
    . = ALIGN(4);
  } >FREERAM
*/

}



  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xxgui1992

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值