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()).


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

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

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


    我是搜索的 “c library startup code”

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


    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

    /* Copy the data segment initializers from flash to SRAM */  
      movs  r1, #0
      b  LoopCopyDataInit
      ldr  r3, =_sidata
      ldr  r3, [r3, r1]
      str  r3, [r0, r1]
      adds  r1, r1, #4
      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. */  
      movs  r3, #0
      str  r3, [r2], #4
      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 
    评论 赞赏