链接描述文件未按预期跳过字节 [英] Linker Script Does Not Skip Bytes As Expected

查看:126
本文介绍了链接描述文件未按预期跳过字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有这个汇编文件,我将其与GNU一起汇编并使用链接程序脚本与GNU ld链接.

链接描述文件(boot.ld):

INPUT(boot.o)
OUTPUT(boot.out)
ENTRY(boot_start)

SECTIONS {
        . = 0x7c00;
        .text : { *(.text) }
        .data : { *(.data) }
        . = 0x7dfe;
        .boot_end : { *(.boot_end) }
}

如您所见,我尝试通过执行. = 0x7cdfe来使文件完全符合Bootloader所需的512字节. .boot_end包含启动签名,因此填充了剩余的两个字节.

我按如下方式创建引导加载程序:

m4 boot.S | as -o boot.o
ld -T boot.ld
objcopy -O binary boot.out boot.img

boot.out包含已经具有绝对地址的部分,并且一切似乎都很好. .boot_end位于0x7dfe,我希望孔用零填充,但不,boot.img总共为55个字节.对我来说,奇怪的是该文件甚至不包含启动签名.只是.text.data而没有.boot_end或跳过的字节.

如何移动ld跳过那些字节?我的启动签名去了哪里?

解决方案

您很可能缺少节标记.关键是,如果您以后链接到非标准(非.text.data等)ELF的ELF时,如果没有将段标志赋予as,则该段将不会在转储中分配(即,被objcopy默默忽略,请注意,生成coff文件时,这种行为会有所不同)

尝试更换(大概是)裸露的衣服

.section boot_end 

使用

的汇编程序输入文件中的

伪指令

.section boot_end,"a" 

(或更多标志,例如根据您的需要使其可执行),然后重试.

"a"标志使该段可分配 ,即告诉ldobjcopy您希望在二进制文件中使用它.

So, I have this assembly file, which I assemble with GNU as and link with GNU ld using a linker script.

Linker script (boot.ld):

INPUT(boot.o)
OUTPUT(boot.out)
ENTRY(boot_start)

SECTIONS {
        . = 0x7c00;
        .text : { *(.text) }
        .data : { *(.data) }
        . = 0x7dfe;
        .boot_end : { *(.boot_end) }
}

As you see I try to make the file exactly 512 bytes as needed for a bootloader by doing . = 0x7cdfe. .boot_end contains the boot signature and thus fills up the remaining two bytes.

I create the bootloader as follows:

m4 boot.S | as -o boot.o
ld -T boot.ld
objcopy -O binary boot.out boot.img

boot.out contains the sections already with absolute addresses and everything seems fine. .boot_end is at 0x7dfe and I expect the holes to be filled with zeros, but no, boot.img is a total of 55 bytes. The, for me, weird thing is that the file does not even contain the boot signature. It's just .text and .data without either .boot_end or the skipped bytes.

How do I move ld to skip those bytes? And where is my boot signature gone?

解决方案

You are most probably missing a section flag. Point is, if no section flags are given to as when you are later linking to ELF for a non-standard (not .text or .data or the like) section name, the section will not be allocated in the dump (i.e, silently be ignored by objcopy, note that this behaves differently when producing coff files)

Try to replace your (presumably) naked

.section boot_end 

directive in your assembler input file with

.section boot_end,"a" 

(or more flags, for example to make it executable, depending on what you need) and try again.

The "a" flag makes the section allocatable, i.e. tells both ld and objcopy you want it in your binary.

这篇关于链接描述文件未按预期跳过字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆