'Size of the file produced by linker is too large

I have those three files that don't have many bytes.

.rw-r--r-- 936 28 Jan 00:53 kernel.o
.rw-r--r-- 512 28 Jan 00:53 kernel_entry.o
.rw-r--r-- 872 28 Jan 00:53 screen.o

I assembled kernel_entry.o using NASM 2.15.05:

nasm src/kernel_entry.asm -f elf32 -o ../build/obj/kernel_entry.o

And i compiled kernel.o and screen.o using gcc 11.1.0:

gcc -ffreestanding -m32 -fno-pie -c -Iinclude -MMD -o ../build/obj/kernel.o src/kernel.c
gcc -ffreestanding -m32 -fno-pie -c -Iinclude -MMD -o ../build/obj/driver/screen.o src/driver/screen.c

My goal is to produce 32 bit executable code. Then i link those 3 files together using ld 2.36.31:

ld -m elf_i386 -Ttext 0x1000 --oformat binary ../build/obj/kernel_entry.o ../build/obj/kernel.o ../build/obj/driver/screen.o -o ../build/kernel.bin

However, I expected the output file to be the size of the three .o files but it is 135M which is way too much:

.rwxr-xr-x 135M 28 Jan 01:16 build/kernel.bin

Looking at the hexadecimal representation of the file, I see a little room for my instructions and the rest is just zeros.

Do you have any idea what's wrong? By removing the oformat parameter I get a decent size but the code does not execute as expected

Thank you.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source