objcopy为什么从二进制文件中移除我的节?

huangapple go评论38阅读模式
英文:

Why does objcopy remove my section from binary?

问题

我正在尝试链接我的针对ARM的小型裸机教育项目。我有一个简单的汇编源代码和链接器脚本。有一个专门的独立部分用于异常向量和启动代码:

.section STARTUP_SECTION, "x"
_reset:
    b reset_handler     @ 复位
    b .                 @ 未定义指令
    b .                 @ SWI
    b .                 @ 预取中断中止
    b .                 @ 数据中断中止
    b .                 @ 保留
    b .                 @ IRQ
    b .                 @ FIQ

reset_handler:
    @ 这里有一些代码
    b .

@ 然后是 .text 和 .data 部分

以及简单的链接器脚本:

ENTRY(_reset)
SECTIONS
{
    . = 0;
    .startup . :
    {
        startup.o (STARTUP_SECTION)
        reset_section_end = .;
    }

    .text. : {*(.text)}
    .data . : {*(.data)}
    .bss  . : {*(.bss COMMON)}

}

我在链接器生成的地图文件中看到了所有我的部分,.text 部分位于比 .startup 高的地址,正如预期的那样。但当我使用以下命令将其转换为二进制文件时:

arm-none-eabi-objcopy -O binary startup.elf startup.bin

我发现它从 .text 部分开始,并且我的启动部分丢失了。当我使用 objdump 反汇编它时,仍然可以看到 elf 文件中的所有部分,但 objcopy 删除了 .startup。该部分没有标记为 NOLOAD 或类似的内容。对于这种部分,是否默认为 NOLOAD 类型,如果是的话,为什么?以及如何将其标记为 LOAD,因为根据链接器手册,没有这样的部分类型。

这里发生了什么?

英文:

I'm trying to link my tiny bare-metal educational project for ARM. I have one simple assembly source and linker script. There is a special separate section for exception vectors and startup code:

.section STARTUP_SECTION, "x"
_reset:
    b reset_handler     @ Reset
    b .                 @ Undefined instruction
    b .                 @ SWI
    b .                 @ Prefetch Abort
    b .                 @ Data Abort
    b .                 @ reserved
    b .                 @ IRQ
    b .                 @ FIQ

reset_handler:
    @ some code here
    b .

@ then .text and .data section

And simple linker script:

ENTRY(_reset)
SECTIONS
{
    . = 0;
    .startup . :
    {
        startup.o (STARTUP_SECTION)
        reset_section_end = .;
    }

    .text. : {*(.text)}
    .data . : {*(.data)}
    .bss  . : {*(.bss COMMON)}

}

I see all my sections in the map file produced by linker, and .text section lies at higher address than .startup as expected. But when I convert it to binary with:

arm-none-eabi-objcopy -O binary startup.elf startup.bin

I see that it starts from .text contents, and my startup section is missing. I'm still able to see all sections in the elf file when I disassemble it with objdump, but objcopy removes .startup. The section is not marked as NOLOAD or something like this. Is NOLOAD type a default for such section and if so, why? And how to mark it as LOAD since there is no such section type according to linker manual.

What is going on here?

答案1

得分: 0

似乎,链接器将用“x”标记的非标准代码部分默认视为不可分配。通过使用“a”标志可以解决这个问题。因此,该部分声明最终如下所示:

.section STARTUP_SECTION, "xa"
英文:

Seems that a non-standard code section marked with "x" flag is considered by the linker as not "allocatable" by default. "a" flag solves the issue. So, the section declaration finally looks like this:

.section STARTUP_SECTION, "xa"

huangapple
  • 本文由 发表于 2023年2月19日 23:59:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501427.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定