c - 我如何使用 crt0.o 在我的裸机 C 程序中设置 .bss 和 .data?

标签 c embedded crt bare-metal cortex-m

我成功编写了一个在我的 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/

相关文章:

c - Neon 内在功能是通过从所有元素中减去最小元素来防止溢出[无循环]

c++ - 为什么/MD 是编译器的选项?不是链接器的?

c - 变量类型信息如何以及在哪里存储?

c - 在 C 中,逻辑运算符是否在 || 的一侧结束是真的?

linux - sed 在不同的平台上返回不同的结果

C 结构格式/别名

c++ - MSVCR 和 CRT 初始化

c - 如何修复 Build TinyCCompiler(TCC) from Source 中的 Error of crt1.o,crti.o?

c - 最简单程序的段错误?

c - 使用 openmp 看不到任何加速