简体   繁体   English

使用 GNU 程序集的主引导记录:平面二进制文件中的额外字节 output

[英]Master Boot Record using GNU Assembly: extra bytes in flat binary output

I am try to compile the simple following MBR:我正在尝试编译以下简单的 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.但是,我得到一个超过 129MB 的二进制文件,这对我来说很奇怪。 Thus, I wanted to know what is going on in that build process?因此,我想知道构建过程中发生了什么? Thank you very much.非常感谢你。

Running objdump over boot.o give me:在 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 

Manually removing the section .note.gnu.property before calling ld seems to solve the problem.在调用 ld 之前手动删除部分.note.gnu.property似乎可以解决问题。 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

using -mx86-used-note=no flag with as will remove note section.-mx86-used-note=no标志与 as 一起使用将删除注释部分。 Check here https://sourceware.org/binutils/docs/as/i386_002dOptions.html在这里查看https://sourceware.org/binutils/docs/as/i386_002dOptions.html

-mx86-used-note=no -mx86-used-note=no

-mx86-used-note=yes -mx86-used-note=yes

These options control whether the assembler should generate GNU_PROPERTY_X86_ISA_1_USED and GNU_PROPERTY_X86_FEATURE_2_USED GNU property notes.这些选项控制汇编程序是否应生成 GNU_PROPERTY_X86_ISA_1_USED 和 GNU_PROPERTY_X86_FEATURE_2_USED GNU 属性注释。 The default can be controlled by the --enable-x86-used-note configure option.默认值可以由 --enable-x86-used-note 配置选项控制。

ld links all your sections into the flat binary output unless you tell it not to (with a linker script for example). ld将您的所有部分链接到平面二进制文件 output 中,除非您告诉它不要这样做(例如使用 linker 脚本)。

The extra bytes are from the .note.gnu.property section which as adds, which can indicate stuff like x86 ISA version (eg 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 ;额外的字节来自.note.gnu.property部分, as添加,它可以指示诸如 x86 ISA 版本之类的东西(例如 AVX2+FMA+BMI2,Haswell 功能级别,是 x86-64_v3。)你不希望在您的平面二进制文件,尤其是在远离您告诉它使用-Ttext放置.text部分的位置的默认高地址处; 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.使用as -mx86-used-note=no将首先从.o中省略该部分,只留下您在 asm 源代码中定义的部分。 From the GAS manual's i386 options来自GAS 手册的 i386 选项

-mx86-used-note=no
-mx86-used-note=yes

These options control whether the assembler should generate GNU_PROPERTY_X86_ISA_1_USED and GNU_PROPERTY_X86_FEATURE_2_USED GNU property notes.这些选项控制汇编程序是否应生成 GNU_PROPERTY_X86_ISA_1_USED 和 GNU_PROPERTY_X86_FEATURE_2_USED GNU 属性注释。 The default can be controlled by the --enable-x86-used-note configure option.默认值可以由 --enable-x86-used-note 配置选项控制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM