将 .data 部分放在一个单独的段中程序头

问题描述

我遇到了一个复杂的问题,我需要访问 .data 初始化数据作为 Arm 裸机应用程序的单独段。

我能够使用 Output Section Region 实现它。

我原来的链接脚本是这样的:

SECTIONS
{
    /*
     * For Cortex-M devices,the beginning of the startup code is stored in
     * the .interrupt_vector section,which goes to FLASH
     */
    .text :
    {
        . = ALIGN(4);
        KEEP(*(.interrupt_vector))
        KEEP(*(.reset))

        *(.text .text.*)            /* all remaining code */
        *(.rodata .rodata.*)        /* read-only data (constants) */
        . = ALIGN(4);
    } >FLASH
   /*
     * This address is used by the startup code to
     * initialize the .data section.
     */
    . = ALIGN(4);
    __data_init__ = .;

    .data  : AT ( __data_init__ )
    {
        . = ALIGN(4);

        /* This is used by the startup code to initialize the .data section */
        __data_start__ = . ;
        *(.data_begin .data_begin.*)
        *(.data .data.*)
        *(.data_end .data_end.*)
        . = ALIGN(4);

        /* This is used by the startup code to initialize the .data section */
        __data_end__ = . ;
    } >RAM

    .bss (NOLOAD) :
    {
        . = ALIGN(4);
        __bss_start__ = .;
        *(.bss_begin .bss_begin.*)

        *(.bss .bss.*)
        *(COMMON)

        *(.bss_end .bss_end.*)
        . = ALIGN(4);
        __bss_end__ = .;
    } >RAM

而我的启动功能有:

    /* copy .data from FLASH to RAM */
    src = &__data_init__;
    dest = &__data_start__;
    end = &__data_end__;

    while(dest < end) {
      *dest++ = *src++;
    }

我将链接器脚本更改为:

SECTIONS
{
    /*
     * For Cortex-M devices,which goes to FLASH
     */
    .text :
    {
        . = ALIGN(4);
        KEEP(*(.interrupt_vector))
        KEEP(*(.reset))

        *(.text .text.*)            /* all remaining code */
        *(.rodata .rodata.*)        /* read-only data (constants) */
        . = ALIGN(4);
    } >FLASH
    .data :
    {
        . = ALIGN(4);
        __data_init__ = LOADADDR (.data);

        /* This is used by the startup code to initialize the .data section */
        __data_start__ = . ;
        *(.data_begin .data_begin.*)
        *(.data .data.*)
        *(.data_end .data_end.*)
        . = ALIGN(4);

        /* This is used by the startup code to initialize the .data section */
        __data_end__ = . ;
    } >RAM AT > FLASH

    .bss (NOLOAD) :
    {
        . = ALIGN(4);
        __bss_start__ = .;
        *(.bss_begin .bss_begin.*)

        *(.bss .bss.*)
        *(COMMON)

        *(.bss_end .bss_end.*)
        . = ALIGN(4);
        __bss_end__ = .;
    } >RAM AT > RAM

现在我有 .data 作为一个完全独立的 ELF 段:

arm-none-eabi-readelf.exe -l ../../../firmware/Cortex-M3/cm3-bootloader/Release/cm3-bootloader.elf

Elf file type is EXEC (Executable file)
Entry point 0x601b9
There are 3 program headers,starting at offset 52

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  LOAD           0x010000 0x00060000 0x00060000 0x067fc 0x067fc R E 0x10000
  LOAD           0x019b30 0x20019b30 0x000667fc 0x00064 0x00064 RW  0x10000
  LOAD           0x029b2c 0x20019b2c 0x20019b2c 0x00000 0x060e8 RW  0x10000

 Section to Segment mapping:
  Segment Sections...
   00     .text .ARM.exidx.reset
   01     .data
   02     .systemclock .bss ._stack

此外,__data_init__ 仍然准确地指向 .data LMA 所在的位置:

.data           0x20019b30       0x64 load address 0x000680f4
                0x20019b30                . = ALIGN (0x4)
                0x000680f4                __data_init__ = LOADADDR (.data)
                0x20019b30                __data_start__ = .

这不是一个问题,这只是为了记录我是如何做到的,以防对其他人有帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)