4 ms·
Nice but very incomplete article. Some observations below... You will need to build a cross compiler toolchain (binutils and GCC). I.e. i686-pc-elf-gcc for 32
by exDM69 10y ago
Nice but very incomplete article. Some observations below...
You will need to build a cross compiler toolchain (binutils and GCC). I.e. i686-pc-elf-gcc for 32 bit or x86_64-pc-elf-gcc for 64 bit. The compiler that comes with your OS is built for a different target and may have downstream patches, esp. if you're on Ubuntu. Things may seem to work initially but you will encounter strange bugs sooner or later.
Build a bootable and debuggable ELF file, not a flat binary. All it takes is a slightly different linker script. You won't get very far without a debugger on bare metal projects.
You need to zero fill .bss section in the boot code before jumping to C code.
I recommend using gas instead of nasm. You might like Intel syntax but you'll need inline (at&t) assembly in C code anyway. Nicer to have just one syntax. It's just an assembler syntax, you will get used to it in a matter of hours
Use a makefile. Fancy build systems (e.g. cmake) aren't nice with kernel images.
See below for examples. The first one isn't my project but I contributed the linker script, Makefile and README for making bootable ELFs. The latter is my x86_64 toy kernel project from years ago.
https://github.com/Overv/MineAssemble https://github.com/Overv/MineAssemble
https://github.com/rikusalminen/danjeros https://github.com/rikusalminen/danjeros
- grahamedgecombe 10y ago> You need to zero fill .bss section in the boot code before jumping to C code. GRUB does this for you. See this thread from a few years ago: https://news.ycombinator.com/item?id=7590790 https://news.ycombinator.com/item?id=7590790
- exDM69 10y agoHah, it was a response to my comment back then too. Either that applies only to multiboot v2 or there was a bug in qemu loader since I had a non-zero value in .bss.
- adrianN 10y agoWhat problems do you have with cmake and kernel images?
- exDM69 10y agoUsing a linker script was more painful than it should have been. Pre-processed assembly files (.S) didn't work (they should run through C pre processor). Writing toolchain file for cross compiling wasn't easy. CMake works great for user space apps, and cross compiling works very well for them. But bare metal projects, kernel modules, etc just are not great.
- SixSigma 10y ago> You will need to build a cross compiler toolchain (binutils and GCC). I.e. i686-pc-elf-gcc for 32 bit or x86_64-pc-elf-gcc for 64 bit. Then a good idea would be to bootstrap it with a build environment on the emulator