https://segmentfault.com/q/1010000004829859/a-1020000004850311
Q:
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都是在這裡嗎?
A:
.data和.bss是在__main裡進行初始化的。
我是搜尋的 “c library startup code”
- 對于ARM Compiler,__main主要執行以下函數
stm32中.bss和.data段是在哪裡初始化的
其中__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:
__main always calls this function during startup before calling __rt_entry.
- 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.
詳細内容見:ARM Compiler C Library Startup and Initialization
-
對于gcc
彙編檔案startup_stm32f10x_hd.s裡面
已經對.data和.bss進行了初始化Reset_Handler
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
轉載于:https://www.cnblogs.com/aardvark/p/5808936.html