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进行了初始化

  1. Reset_Handler:
  2. /* Copy the data segment initializers from flash to SRAM */
  3. movs r1, #0
  4. b LoopCopyDataInit
  5. CopyDataInit:
  6. ldr r3, =_sidata
  7. ldr r3, [r3, r1]
  8. str r3, [r0, r1]
  9. adds r1, r1, #4
  10. LoopCopyDataInit:
  11. ldr r0, =_sdata
  12. ldr r3, =_edata
  13. adds r2, r0, r1
  14. cmp r2, r3
  15. bcc CopyDataInit
  16. ldr r2, =_sbss
  17. b LoopFillZerobss
  18. /* Zero fill the bss segment. */
  19. FillZerobss:
  20. movs r3, #0
  21. str r3, [r2], #4
  22. LoopFillZerobss:
  23. ldr r3, = _ebss
  24. cmp r2, r3
  25. bcc FillZerobss
  26. /* Call the clock system intitialization function.*/
  27. bl SystemInit
  28. /* Call the application's entry point.*/
  29. bl main
  30. bx lr
  1.  

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. 增加eclipse启动的Tomcat内存的方法 tomcat内存增加

    增加eclipse启动的Tomcat内存的方法 Tomcat一般默认情况下最大最优内存设置为2G 这种情况下,修改Tomcat\bin\catalina.bat,添加如下内容 set JAVA_OPT ...

  2. 使用Hadoop打造私有云盘之API操作

    项目介绍:使用hadoop实现云盘的增删读获取列表功能,hadoop不支持数据修改,特性是一次写入多次读取.主流的网盘也不支持该功能.今天我们用hdfs的FileSystem实现这些操作. 1.上传功 ...

  3. 学习Windows Azure 视频集合大全

    Windows Azure 入门系列课程视频Windows Azure 入门系列课程(1):Windows Azure 概述http://www.aboutyun.com/thread-5777-1- ...

  4. mysql均衡负载

    一.利用mysql 复制分流查询操作: 利用mysql的主从复制可以有效的分流更新操作和查询操作,具体的实现是一个主服务器,承担更新操作,多台从服务器,承担查询操作,主从之间通过复制实现数据的同步.多 ...

  5. PC-飞起来!我的Windows XP——五步快速优化Windows XP

    虽然Microsoft的 Vista已经发售了快一年,但国内大部分系统用户仍使用着目前堪称完美的Windows XP.与以往的Windows操作系统一样,新安装的Windows XP可能还不在最佳状态 ...

  6. 有关gcc的扩展__attribute__((unused))

    ================================ Author: taoyuetao Email: tao_yuetao@yahoo.com.cn Blog: taoyuetao.cu ...

  7. 从CR线下活动学到的:如何组织一个小的线下活动

    作者:朱克锋 邮箱:zhukefeng@iboxpay.com 转载请注明出处:http://blog.csdn.net/linux_zkf 周末在腾讯组织了GR,活动达到了预期的收获,从这次活动我主 ...

  8. 换种眼光看Spring之bean是怎么诞生的(一)

    Java的世界里处处存在了对象,有时候换一种眼光往往会给自己带来与之前大不一样的理解. 一个对象的出现离不开字节码,拿classforname来讲,classforname("...&quo ...

  9. 获取datagrid中编辑列combobox的value值与text值

    var ed = $('#dg').datagrid('getEditor', {index:editIndex,field:'productid'}); var productname = $(ed ...

  10. TransactionScope 对该事务的状态无效 和一些注意事项

    使用TransactionScope 的时候要操作同一种数据库操作方式,不能一个方法用ado.net ,另外一个方法用EF,那样会报 "该事务管理器已经禁止了它对远程/网络事务的支持&quo ...