1

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
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
manzerbredes
  • 310
  • 2
  • 13

2 Answers2

0

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.

hardfau18
  • 25
  • 6
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 01 '22 at 09:06
  • It's an `as` option, not an `ld` option. – Peter Cordes Mar 01 '22 at 10:31
  • Also, this seems to be written as an answer to your question, [--oformat binary generates random gnu header instead of the given assembly code](https://stackoverflow.com/q/71289511). It's not really answering this question, which already mentions the `-mx86-used-note=no` option, but is asking why it's needed and stuff like that. That's why I wrote my own answer. – Peter Cordes Mar 01 '22 at 13:37
0

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

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

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847