pritamzope / OS

Writing & Making Operating System and Kernel parts so simple like Hello World Programs, Starting from writing Bootloaders, Hello World Kernel, GDT, IDT, Terminal, Keyboard/Mouse, Memory Manager, HDD ATA R/W, VGA/VESA Graphics
http://createyourownos.blogspot.com
663 stars 103 forks source link

Error on making `MyOs.bin` #2

Closed rahatzamancse closed 3 years ago

rahatzamancse commented 5 years ago

According to the run.sh file in kernel_1 folder, when running this command: gcc -T linker.ld -o MyOS.bin -ffreestanding -O2 -nostdlib kernel.o boot.o -lgcc I get the following error: /usr/bin/ld: boot.o: relocation R_X86_64_32 against '.multiboot' can not be used when making a PIE object; recompile with -fPIC /usr/bin/ld: final link failed: nonrepresentable section on output collect2: error: ld returned 1 exit status

ghost commented 5 years ago

Me too. Please fix @pritamzope

pritamzope commented 5 years ago

It's because the kernel is of 32 bit(i386) not 64 bit. The size of multiboot header is different on different architectures. so compile and links the kernel as 32 bit(i386) options. also we are using gcc for linking, use ld for linking. Here's the modified script contents.(Tested on Ubuntu_x86_64, WSL with gcc-5, 6, 7, 8):

assemble boot.s file

as --32 boot.s -o boot.o

compile kernel.c file

gcc -m32 -c kernel.c -o kernel.o -std=gnu99 -ffreestanding -O2 -Wall -Wextra

linking the kernel with kernel.o and boot.o files

ld -m elf_i386 -T linker.ld kernel.o boot.o -o MyOS.bin -nostdlib

check MyOS.bin file is x86 multiboot file or not

grub-file --is-x86-multiboot MyOS.bin

building the iso file

mkdir -p isodir/boot/grub cp MyOS.bin isodir/boot/MyOS.bin cp grub.cfg isodir/boot/grub/grub.cfg grub-mkrescue -o MyOS.iso isodir

run it in qemu

qemu-system-x86_64 -cdrom MyOS.iso