I'm following this book with regards to developing a little kernel: https://littleosbook.github.io
I've gotten to the point where the binary has to be linked and I want to be sure that I understand the linker script before proceeding:
ENTRY(loader) /* the name of the entry label */
SECTIONS {
. = 0x00100000; /* the code should be loaded at 1 MB */
.text ALIGN (0x1000) : /* align at 4 KB */
{
*(.text) /* all text sections from all files */
}
.rodata ALIGN (0x1000) : /* align at 4 KB */
{
*(.rodata*) /* all read-only data sections from all files */
}
.data ALIGN (0x1000) : /* align at 4 KB */
{
*(.data) /* all data sections from all files */
}
.bss ALIGN (0x1000) : /* align at 4 KB */
{
*(COMMON) /* all COMMON sections from all files */
*(.bss) /* all bss sections from all files */
}
}
First of all, why the specific ALIGN
ment of the sections (4KB)?
(I think it's very unlikely to be related with optimizations or I could me mistaken and this may be for performance reasons, since otherwise I think there's no restriction on the boot process - confirmed by removing the ALIGN
function calls in the script)
Then considering the executable to be linked:
global loader ; the entry symbol for ELF
MAGIC_NUMBER equ 0x1BADB002 ; define the magic number constant
FLAGS equ 0x0 ; multiboot flags
CHECKSUM equ -MAGIC_NUMBER ; calculate the checksum
; (magic number + checksum + flags should equal 0)
section .text: ; start of the text (code) section
align 4 ; the code must be 4 byte aligned
dd MAGIC_NUMBER ; write the magic number to the machine code,
dd FLAGS ; the flags,
dd CHECKSUM ; and the checksum
loader: ; the loader label (defined as entry point in linker script)
mov eax, 0xCAFEBABE ; place the number 0xCAFEBABE in the register eax
.loop:
jmp .loop ; loop forever
Am I right when saying that the .rodata
, and .bss
sections are not yet needed? (since the kernel still starts fine)
Is there anything else to understand form this little linker script that I cannot get out of the the ld
manual (which I read a bit to understand the dot, the SECTIONS
and the entry point)?
It seems that I can re arrange the sections' order in the script and the kernel still works, why is that?