C++ Bare Bones: Difference between revisions

[unchecked revision][unchecked revision]
Content deleted Content added
mNo edit summary
mNo edit summary
Line 7:
==Preface==
 
This tutorial assumes you have a compiler / assembler / linker toolchain capable of handling [[ELF]] files. On a Windows machine, you are ''strongly'' encouraged to set up a [[GCC Cross-Compiler]], as it removes all the various toolchain-specific issues you might have ("PE operation on a non-PE file", "unsupported file format", and a number of others). While technically*nix a Linux machinemachines already hashave anELF ELF-capable toolchaintoolchains, you are ''still'' encouraged to build a cross-compiler, as it is the first step to do, and it keeps you from relying on things you shouldn't (header and library files, for example).
 
To make starting an OS easy, we will be using a lot of existing parts,: GRUB will be the bootloader, and the kernel will be in ELF format. GRUB (a Multiboot compliant boot loader) puts the system in to the correct state for your kernel to start executing. This includes enabling the A20 line (to give you access to all available memory addresses) and puttingswitching the system in to 32-bit Protected Mode, giving you access to a theoretical 4GiB of memory. We will not use a flat binary but a kernel in ELF format, so that we have a lot of control tocan tell GRUB where to load which part in memory.
 
==Overview==
Line 43:
|}
 
Because there is no environment executing your kernel (you can't expect the bootloader to do this), you have to execute your own constructors (and possibly destructors). Both are described below. <!-- I think the following sentence distracts from the objective at hand. There is however discussion about whether executing the global destructors in your kernel makes any sense when you're shutting down (seeing as you're shutting the computer or OS down). It would probably be best to call these destructors only if you have specific object destructors that need to clean something up before the kernel is shut down (and you can't or don't want to explicitly call these). Note--> alsoNote that you should call the destructors in '''reverse''' order! Thisof construction to make sure objects depending on the existence of other objects do not gettry destroyedto inaccess thealready wrongdestroyed orderobjects.
 
The vague <!-- ?? Explain! --> linking sections should be split across multiple sections: you should put them in text, rodata, data and bss. You will need a linker script only later on, but to understand the other source, have a look at it now (tip: you can use 'i586-elf-ld --verbose' to see LD's standard linker script for comparison):
 
<pre>
Line 99:
The script might require modification to suit your kernel's needs. Note that forgetting to add the vague linking sections might result in GRUB randomly not being able to load your kernel anymore (e.g. after modifying the most trivial code). It might also explain sudden rises in executable size by 50 kB and other issues.
 
Note that the .ctor and .dtor sections need to be properly aligned. The linker script shown here does that by placing them at the beginning of the .rodata section, which is aligned at a page boundary.
 
==loader.s==