LibOpenCM3(三) .ld文件(连接器脚本)和startup代码说明
阅读原文时间:2022年02月23日阅读:1

目录

说明

要了解 libopencm3 的代码结构, 就需要先了解它编译产生的固件结构和启动顺序, 这部分和 CMSIS 不一样

ld, Linker script, 连接器脚本文件

在 2020-11-29 的改动 drop all part specific ld files之前, 在 lib/stm32/f0 - f7 目录下, 可以看到各个型号的ld文件, 在这个提交中删除了所有具体型号的ld文件, 改为编译中使用脚本生成. 在用户的代码目录下可以看到生成的ld文件. 以下说明ld文件的功能和内容.

以ld为扩展名的文件是针对具体MCU的连接器脚本(Linker script), 连接器脚本用于在link阶段, 告诉linker(连接器)关于生成固件时需要的存储布局和根据不同内存区域的启动方式对其进行初始化. 连接器脚最关键的一点是指定了 flash 和 RAM 的起始位置和大小. 在连接阶段通过 -Tscriptname.ld参数传递给连接器.

连接器脚本会包含以下的内容

  • Memory layout 内存布局
  • Entry point definition 程序入口定义
  • Section definitions 各存储区的定义(定义Flash、RAM中代码和数据的存放位置)

以下以 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));

启动代码

libopencm3 和 CMSIS 不一样, 没有使用汇编代码的startup文件, 而是用 vector.c 生成startup文件, 这个文件位于 lib/cm3/ 目录下.

#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

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器