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. Windows Azure 的虚拟硬盘和文件的相关概念

    虚拟硬盘和文件 在 Windows Azure 外部,虚拟硬盘可使用 VHD 或 VHDX 格式.它们还可以是固定的.动态扩展或差异的.Windows Azure 支持 VHD 格式的固定磁盘.固定格 ...

  2. JS实现的一个验证码,可以在前端验证后在提交action

    js实现的一个验证码功能,可以在前端判断验证码输入是否正确 输入的邮箱格式是否正确 验证成功后才提交action到后台 <!DOCTYPE html PUBLIC "-//W3C//D ...

  3. 15个实用的Linux find命令示例

    妈咪,我找到了! -- 15个实用的Linux find命令示例 http://www.oschina.net/translate/15-practical-linux-find-command-ex ...

  4. visual studio 2010 "创建控件时出错"解决办法[转]

    之前我有在博问里面提问(http://space.cnblogs.com/q/16208/),但一直都没答案.我系统都重装了两次了,可还是出现这样的错误,我很郁闷啊.今天我终于找到原因了. 我写了一个 ...

  5. Tuple元组

    Tuple元组 Tuple 是 Storm 的主要数据结构,并且是 Storm 中使用的最基本单元.数据模型和元组. Tuple 描述 Tuple 就是一个值列表, Tuple 中的值可以是任何类型的 ...

  6. Yii Listview 更新及搜索

    更新: http://my.oschina.net/shixiaobao17145/blog/130992 http://www.yiiframework.com/forum/index.php/to ...

  7. nyoj 1870 愚人节礼物

    愚人节的礼物 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  8. iOS DES 加密转base64

      //用法 加密转base 64 NSString *str = [self base64StringFromText:@"qingjoin" withKey:@"ke ...

  9. JavaScript MVC框架PK:Angular、Backbone、CanJS与Ember

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  10. SQL Server2000企业管理器在Win7中新建表错误的解决方法

    Sql Server2000建表错误与解决方法: 在Windwos7中SQL Server 2000企业管理器在新建表时会提示错误,尝试各种方法均告无效,包括升级SP3和SP4,最终发现如下规律可以暂 ...