• 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文件的功能和内容.

    连接器的工作机制

    MCU执行程序的过程中, 执行指令的顺序只有两种: 顺序执行和跳转.

    在生成汇编代码时会把各个函数和变量分块, 并在前面写上对应的标号, 例如 START, 最后由编译器将START程序处的地址装入写有 START标号跳转指令的地方. 这就是一个连接的过程.

    对应C代码被编译为二进制执行文件的过程:

    1. C语言转换为汇编代码 file.c -> file.asm
    2. 汇编编译 产生 .o 文件 file.o 带有未解析地址的中间文件
    3. 连接器将.o 文件排列填入到对应的存储地址中, 并记录其函数和变量对应的地址, 使得其他函数能够跳转到该函数的入口地址.

    最终的排列顺序和地址, 可以通过.map文件查看.

    ld 文件的功能和结构

    以ld为扩展名的文件是针对具体MCU的连接器脚本(Linker script), 是连接器使用的规则文件, 最关键的一点是指定了 flash 和 RAM 的起始位置和大小.

    连接器脚本用于在link阶段, 告诉linker(连接器)关于生成固件时的存储布局, 指示连接器将不同的机器码保存到固件的对应位置. 在连接阶段通过 -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 相关参考资料


    启动文件

    关于stm32的启动文件

    启动文件就是 MCU 在执行 main 函数前将系统初始化并把 PC(程序计数器, 当前执行代码位置的指针)设置到 main 函数的文件.

    Cortex-M3 内核中, 程序的执行会从 flash 首地址的第一位开始, 先取出 MSP (栈顶地址指针寄存器)的数值, 然后取出复位中断函数的地址并跳转过去.

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

    启动文件 vector.c

    以下是对启动文件 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
    
  • 相关阅读:
    JavaScript引用类型
    Java08_方法
    网络爬虫(一)
    openCV(四)——鼠标绘制
    openCV(三)——视频
    openCV(二)——基础绘制函数
    openCV(一) 读取保存图像
    JAVA07-数组
    JAVA06-while循环,do while循环
    JAVA05-switch多重选择
  • 原文地址:https://www.cnblogs.com/milton/p/15921959.html
Copyright © 2020-2023  润新知