简体   繁体   中英

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

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

using -mx86-used-note=no flag with as will remove note section. Check here https://sourceware.org/binutils/docs/as/i386_002dOptions.html

-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. The default can be controlled by the --enable-x86-used-note configure option.

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 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 ; 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

-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. The default can be controlled by the --enable-x86-used-note configure option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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