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”

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

其中__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
 

stm32中.bss和.data段是在哪里初始化的的更多相关文章

  1. Linux中的段管理,bss段,data段,

    Linux 的段管理, BSS段(bss segment)通常是指用来存放程序中未初始化的全局变量的一块内存区域.BSS是英文Block Started by Symbol的简称.BSS段属于静态内存 ...

  2. [转帖]浅谈程序中的text段、data段和bss段

    作者:百问科技链接:https://zhuanlan.zhihu.com/p/28659560来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 一般情况,一个程序本质上都 ...

  3. bss段和data段的区别

    一般情况下,一个程序本质上都是由 bss段.data段.text段三个组成的——本概念是当前的计算机程序设计中是很重要的一个基本概念.而且在嵌入式系统的设计中也非常重要,牵涉到嵌入式系统运行时的内存大 ...

  4. Linux段管理,BSS段,data段,.rodata段,text段

    近期在解决一个编译问题时,一直在考虑一个问题,那就是Linux下可执行程序执行时内存是什么状态,是依照什么方式分配内存并执行的.查看了一下资料.就此总结一下,众所周知.linux下内存管理是通过虚存管 ...

  5. (深入理解计算机系统) bss段,data段、text段、堆(heap)和栈(stack)

    bss段: bss段(bss segment)通常是指用来存放程序中未初始化的全局变量的一块内存区域. bss是英文Block Started by Symbol的简称. bss段属于静态内存分配. ...

  6. 【转】(深入理解计算机系统) bss段,data段、text段、堆(heap)和栈(stack)

    bss段: bss段(bss segment)通常是指用来存放程序中未初始化的全局变量的一块内存区域. bss是英文Block Started by Symbol的简称. bss段属于静态内存分配. ...

  7. 代码中函数、变量、常量 / bss段、data段、text段 /sct文件、.map文件的关系[实例分析arm代码(mdk)]

    函数代码://demo.c #include<stdio.h> #include<stdlib.h> , global2 = , global3 = ; void functi ...

  8. BSS段 data段 text段 堆heap 和 栈stack

    BSS段:BSS段(bss segment)通常是指用来存放程序中未初始化的全局变量的一块内存区域.BSS是英文Block Started by Symbol的简称.BSS段属于静态内存分配.   数 ...

  9. 汇编中bss,data,text,rodata,heap,stack,意义

    bss段: BSS段(bsssegment)通常是指用来存放程序中未初始化的全局变量的一块内存区域.BSS是英文BlockStarted by Symbol的简称.BSS段属于静态内存分配. data ...

随机推荐

  1. 【HTML】Intermediate1:Span&Div

    1.HTML is all bout applying meaning to content. The span & div tags apply no meaning at all=mean ...

  2. BestCoder Round #81 (div.2) B Matrix

    B题...水题,记录当前行是由原矩阵哪行变来的. #include<cstdio> #include<cstring> #include<cstdlib> #inc ...

  3. Java 并发之共享对象

    上一篇文章说的是,避免多个线程在同一时间访问对象中的同一数据,这篇文章来详细说说共享和发布对象. 在没有同步的情况下,我们无法预料编译器.处理器安排操作执行的顺序,经常会发生以为“一定会”发生的动作实 ...

  4. HW3.21

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  5. 编译android版libmpg

    环境:ubutnu 12.04,android SDK 1. 下载libmpg的一个android工程,得到一个Android-libmpg-master.zip.https://github.com ...

  6. 程序员需要掌握哪些IT技能

    据Foote Partners的最新调查:如今,你掌握的 IT 技能的多少决定了你薪资的多少,但你不一定非要比开源架构师或Certified Secure Software Life Cycle Pr ...

  7. 教程-Win7极速优化20项

    1. 加快Windows 7系统启动速度     启动-“msconfig”命令-系统配置-“引导”选项(英文系统是Boot)-点击“高级选项”--勾选“处理器数”和“最大内存”.   2. 加快Wi ...

  8. setTimeout中0毫秒延时

    先来看段代码,思考一下执行的结果. alert(1); setTimeout(function(){alert(2);}, 0); alert(3); 估计很多人认为执行结果为1,2,3,原因就是认为 ...

  9. python学习之元组

    #coding:utf-8# __author__ = 'Administrator'#元组:不可变序列 #空元组mm=()print mm#只有一个值的元组mm=(1,)print mmx=1,2, ...

  10. flex dispatchEvent 实例

    flashbuilder sdk:3.6:jdk:1.7,:tomcat:7:myeclipse:10.0 Flex dispatchEvent实例下载:点击打开链接 Flex dispatchEve ...