我成功编写了一个在我的 STM32F4 上运行的裸机 C 程序。没什么特别的,只是通常的 led-blinky-program。在这个项目中,我自己编写了清除 .bss
部分并初始化 .data
部分的初始化例程。
这并不复杂。在链接描述文件中,我只是指示链接器创建一些符号来标记 .data
和 .bss
部分的开始和结束。
.data 0x20001000 :
ALIGN(4)
{
__etext = LOADADDR(.data);
__data_start__ = ADDR(.data) ;
*(.data*) ;
__data_end__ = ADDR(.data) + SIZEOF(.data) ;
} >RAM AT>ROM
.bss :
ALIGN(4)
{
__bss_start__ = ADDR(.bss) ;
*(.bss*) ;
__bss_end__ = ADDR(.bss) + SIZEOF(.bss) ;
} >RAM
然后我在我的代码中使用了这些符号:
extern unsigned int __etext;
extern unsigned int __data_start__;
extern unsigned int __data_end__;
extern unsigned int __bss_start__;
extern unsigned int __bss_end__;
void Reset_Handler()
{
unsigned int * src;
unsigned int * dest;
src = &__etext;
dest = &__data_start__;
/* copy .data */
while (dest < &__data_end__)
*(dest++) = *(src++);
/* Zero bss. */
for (dest = &__bss_start__; dest < &__bss_end__; dest++)
*dest = 0;
}
现在我想使用 crt0
来设置 .bss
和 .data
。 (我听说设置东西是 crt0
的主要目的。)
我该怎么做? 在链接描述文件中定义符号和在代码中使用符号的基本原理是否相同?
长话短说
如何使用 crt0
设置我的 .bss
和 .data
部分?
最佳答案
通常,在链接器命令文件/脚本中...
发布的脚本有一些问题。
建议类似于以下内容。 (使用实际原点和长度参数) (请注意,.text、.data、.bss 并不是唯一创建的部分。 还有更多,应该适当列出它们)
你应该看看http://www.math.utah.edu/docs/info/ld_3.html#SEC18 有关链接器命令文件的详细信息和示例
/* this is a very simple memory layout */
/* usually there are separate memory items */
/* for each memory mapped peripheral */
/* external RAM, etc etc etc */
MEMORY {
rom : ORIGIN = 0, LENGTH = 256K
ram : ORIGIN = 0x40000000, LENGTH = 4M
}
SECTIONS {
rom :0 (NOLOAD) BLOCK(4) {
}
ram : {
.text : BLOCK(4) {
.textStart = .;
*(.text)
.textEnd = .;
}
.bss : BLOCK(4) {
.bssStart = .;
*(.bss)
.bssEnd = .;
}
.data : BLOCK(4) {
.dataStart = .;
*(.data)
.dataEnd = .;
}
}
}
关于c - 我如何使用 crt0.o 在我的裸机 C 程序中设置 .bss 和 .data?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29547690/