How to compile a simple multiboot2 bare-metal exec

2019-09-05 16:23发布

I want to start to write an os kernel, and then, i found a document introducing multboot2 spec.

There are three example code files, named boot.S, kernel.c and multiboot2.h, belonging to the multiboot2 branch of grub project.

Firstly, i tried to compile and link the code with some option (using i686-elf-gcc) such as -nostartfiles, -nodefaultlibs and -ffreestanding, but qemu showed me the format was error.

And then i found a file named Makefile.am, and the compiler option in this file is -fno-builtin -nostdinc -O -g -Wall, and the linker option in this file is -nostdlib -Wl,-N -Wl,-Ttext -Wl,80100000 -Wl,--build-id=none.

So i built the kernel with options above, and qemu started to booting the kernel(qemu-system-i386 -kernel kernel). But the kernel traped into infinite loop immediately.

So i cloned the branch of the project, and used gnulib to generate some files and started make but got some errors.

I don't show the error message here, because reading the document, i see what i need to do is just transforming boot.S to binary file and pasting it in the front of the compiled and non-linked kernel file(kernel.o), isn't it? How to complete it?

1条回答
ら.Afraid
2楼-- · 2019-09-05 16:57

This is trivial. Here's a how-to get a multiboot2 bare-metal executable set up using grub.

https://os.phil-opp.com/multiboot-kernel/

Try running the bare executable with -kernel and it will fail.

Try running the ISO containing grub and it will work.

查看更多
登录 后发表回答