I'm trying to write a 32 bits kernel in gcc and i'm cross-compiling it with x86_64-elf-gcc by using the -m32
flag.
It builds and runs ok, but the binary file is 129 MB!!!. I'm very sure the actual code is not that big, and the result binary is full of zeroes, using hexdump, everything in between of 0x2080
and 0x8047000
are zeroes, random hex code and GNU labels.
This is my Makefile:
C_SOURCES = $(wildcard kernel/*.c cpu/*.c lain/*.c)
HEADERS = $(wildcard kernel/*.h cpu/*.h lain/*.h)
OBJ_FILES = ${C_SOURCES:.c=.o cpu/interrupt.o}
all: run
kernel.bin: boot/kernel-entry.o ${OBJ_FILES}
x86_64-elf-ld -m elf_i386 -o $@ -Ttext 0x1000 $^ --oformat binary
os-image.bin: boot/mbr.bin kernel.bin
cat $^ > $@
run: os-image.bin
qemu-system-i386 -fda $<
%.o: %.c ${HEADERS}
x86_64-elf-gcc -g -m32 -ffreestanding -fno-builtin -nostdlib -c $< -o $@
%.o: %.asm
nasm $< -f elf -o $@
%.bin: %.asm
nasm $< -f bin -o $@
clean:
$(RM) */*.bin */*.o */*.dis