stm32中.bss和.data段是在哪里初始化的

STM32的启动文件startup_stm32f10x_hd.s中的描述是

This module performs:

  • Set the initial SP

  • Set the initial PC == Reset_Handler

  • Set the vector table entries with the exceptions ISR address

  • Configure the clock system and also configure the external SRAM mounted on STM3210E-EVAL board to be used as data memory (optional, to be enabled by user)

  • Branches to __main in the C library (which eventually calls main()).

我没有看到初始化.data和.bss段的描述
stm32应该是从中断向量Reset_Handler开始执行的

; Reset handler
Reset_Handler   PROC
                EXPORT  Reset_Handler             [WEAK]
                IMPORT  __main
                IMPORT  SystemInit
                LDR     R0, =SystemInit
                BLX     R0               
                LDR     R0, =__main
                BX      R0
                ENDP

SystemInit里面没有初始化代码,然后就到__main了,难道是在__main里进行的初始化?
因为上面说Branches to __main in the C library (which eventually calls main()),如果不在这里的话就进入C语言的main()函数了,我看到很多地方都说这个初始化在C语言运行之前。
如果是在__main里那具体是在哪里呢?
不同的编译器像gcc和Keil MDK都是在这里吗?

阅读 4k
评论 2016-04-01 提问
    1 个回答

    .data和.bss是在__main里进行初始化的。

    我是搜索的 “c library startup code”

    1. 对于ARM Compiler,__main主要执行以下函数
      image001.png

    其中__scatterload会对.data和.bss进行初始化

    Application code and data can be in a root region or a non-root region. Root regions
    have the same load-time and execution-time addresses. Non-root regions
    have different load-time and execution-time addresses. The root region
    contains a region table output by the ARM linker. The region table
    contains the addresses of the non-root code and data regions that
    require initialization. The region table also contains a function
    pointer that indicates what initialization is needed for the region,
    for example a copying, zeroing, or decompressing function.

    __scatterload goes through the region table and initializes the various execution-time regions. The function:

    • Initializes the Zero Initialized (ZI) regions to zero

    • Copies or decompresses the non-root code and data region from their load-time locations to the execute-time regions.

    __main always calls this function during startup before calling __rt_entry.

    详细内容见:ARM Compiler C Library Startup and Initialization

    1. 对于gcc
      汇编文件startup_stm32f10x_hd.s里面Reset_Handler已经对.data和.bss进行了初始化。

    Reset_Handler:  
    
    /* Copy the data segment initializers from flash to SRAM */  
      movs  r1, #0
      b  LoopCopyDataInit
    
    CopyDataInit:
      ldr  r3, =_sidata
      ldr  r3, [r3, r1]
      str  r3, [r0, r1]
      adds  r1, r1, #4
        
    LoopCopyDataInit:
      ldr  r0, =_sdata
      ldr  r3, =_edata
      adds  r2, r0, r1
      cmp  r2, r3
      bcc  CopyDataInit
      ldr  r2, =_sbss
      b  LoopFillZerobss
    /* Zero fill the bss segment. */  
    FillZerobss:
      movs  r3, #0
      str  r3, [r2], #4
        
    LoopFillZerobss:
      ldr  r3, = _ebss
      cmp  r2, r3
      bcc  FillZerobss
    /* Call the clock system intitialization function.*/
      bl  SystemInit   
    /* Call the application's entry point.*/
      bl  main
      bx  lr 
    评论 赞赏