What are these extra bytes in my binary?

I write a small operating system in C. I wrote a bootloader, and now I'm trying to get a simple C file ("kernel") to compile with gcc :

 int main(void) { return 0; } 

I will compile the file with the following command:

 gcc kernel.c -o kernel.o -nostdlib -nostartfiles 

I use the linker to create the final image using this command:

 ld kernel.o -o kernel.bin -T linker.ld --oformat=binary 

The contents of the linker.ld file are as follows:

  SECTIONS
 {
     .  = 0x7e00;

     .text ALIGN (0x00):
     {
         * (. text)
     }
 }

(The bootloader downloads the image at 0x7e00 .)

This works very well - ld creates a 128-byte file containing the following instructions in the first 11 bytes:

  00000000 55 push ebp
 00000001 48 dec eax
 00000002 89 E5 mov ebp, esp
 00000004 B8 00 00 00 00 mov eax, 0x00000000
 00000009 5D pop ebp
 0000000A C3 ret

However, I cannot understand why the remaining 117 bytes are needed. Dismantling them seems to create a bunch of garbage that makes no sense. The existence of additional bytes is interesting to me that I am doing something wrong.

Should I be bothered?

hexdump of the file

+7
source share
2 answers

These are additional sections that have not been partitioned or discarded. You want your linker.ld file linker.ld look like this:

 SECTIONS { . = 0x7e00; .text ALIGN (0x00) : { *(.text) } /DISCARD/ : { *(.comment) *(.eh_frame_hdr) *(.eh_frame) } } 

I know which sections to discard from the output of objdump -t kernel.o .

+9
source

Simple, you use gcc, and it always sets its initialization code before passing control to your main one. I don’t know which of these code launches, but they are. Since you can also see the GNU comment in its binary format, you cannot print specific sectors using the name objdump -s -j 'section.

0
source

All Articles