使用 GNU 汇编的主引导记录:平面二进制输出中的额外字节
我尝试编译以下简单的 MBR:
.code16
.globl _start
.text
_start:
end:
jmp end
; Don't bother with 0xAA55 yet
我运行以下命令:
> as --32 -o boot.o boot.s
> ld -m elf_i386 boot.o --oformat=binary -o mbr -Ttext 0x7c00
但是,我得到了一个超过 129MB 的二进制文件,这对我来说很奇怪。因此, 我想知道构建过程中发生了什么?非常感谢。
在 boot.o 上运行 objdump 给我:
> objdump -s boot.o
boot.o: format de fichier elf32-i386
Contenu de la section .text :
0000 ebfe ..
Contenu de la section .note.gnu.property :
0000 04000000 18000000 05000000 474e5500 ............GNU.
0010 020001c0 04000000 00000000 010001c0 ................
0020 04000000 01000000
在调用 ld 之前手动删除 .note.gnu.property 部分似乎可以解决问题。但是,我不知道为什么默认情况下会出现此部分...运行以下构建命令似乎也可以解决问题:
> as --32 -o boot.o boot.s -mx86-used-note=no
> ld -m elf_i386 boot.o --oformat=binary -o mbr -Ttext 0x7c00
I am try to compile the simple following MBR:
.code16
.globl _start
.text
_start:
end:
jmp end
; Don't bother with 0xAA55 yet
I run the following commands:
> as --32 -o boot.o boot.s
> ld -m elf_i386 boot.o --oformat=binary -o mbr -Ttext 0x7c00
However, I get a binary file of more than 129MB which is strange to me. Thus,
I wanted to know what is going on in that build process ? Thank you very much.
Running objdump over boot.o give me:
> objdump -s boot.o
boot.o: format de fichier elf32-i386
Contenu de la section .text :
0000 ebfe ..
Contenu de la section .note.gnu.property :
0000 04000000 18000000 05000000 474e5500 ............GNU.
0010 020001c0 04000000 00000000 010001c0 ................
0020 04000000 01000000
Manually removing the section .note.gnu.property before calling ld seems to solve the problem. However, I don't know why this section appears by default... Running the following build commands seems to solve the problem too:
> as --32 -o boot.o boot.s -mx86-used-note=no
> ld -m elf_i386 boot.o --oformat=binary -o mbr -Ttext 0x7c00
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
-mx86-used-note=no
标志与 as 一起使用将删除注释部分。检查此处 https://sourceware.org/binutils/docs/as/i386_002dOptions.html< /a>
using
-mx86-used-note=no
flag with as will remove note section.Check here https://sourceware.org/binutils/docs/as/i386_002dOptions.html
ld
将所有部分链接到平面二进制输出中,除非您告诉它不要这样做(例如使用链接器脚本)。额外的字节来自
as
添加的.note.gnu.property
部分,它可以指示诸如 x86 ISA 版本之类的内容(例如 AVX2+FMA+BMI2、Haswell 功能级别) ,是 x86-64_v3。)您不希望在平面二进制文件中出现这种情况,尤其是在远离您告诉它放置.text
部分的默认高地址处-Ttext
;这将导致一个巨大的文件,其中有零填充间隙,因为它是平面二进制文件。使用
as -mx86-used-note=no
会首先从.o
中省略该部分,只留下您在 asm 源代码中定义的部分。来自GAS 手册的 i386 选项ld
links all your sections into the flat binary output unless you tell it not to (with a linker script for example).The extra bytes are from the
.note.gnu.property
section whichas
adds, which can indicate stuff like x86 ISA version (e.g. AVX2+FMA+BMI2, Haswell feature level, is x86-64_v3.) You don't want that in your flat binary, especially not at the default high address far from where you tell it to put your.text
section with-Ttext
; that would result in a huge file with zeros padding the gap since it's a flat binary.Using
as -mx86-used-note=no
will omit that section from the.o
in the first place, leaving only the sections you define in your asm source. From the GAS manual's i386 options