0

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 ALIGNment 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?

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Paul
  • 20,883
  • 7
  • 57
  • 74
  • In some cases I think if you have writable data segment and code in the same 4kb section you can end up with cache invalidations (performance issue). If you start linking with C or another high level language you'll find data placed in `.rodata` (usually _const_ data). `.bss` is for the uninitialized data or data initialized to 0. Usually boot strap code will want to explicitly write zero data to the `.bss` section before code execution that relies on it is executed. In _C_ this is usually done in the C-Runtime code that executes before calling the `main` function. – Michael Petch Nov 07 '15 at 14:23
  • If you start out just writing assembler code then many of these optional sections don't apply unless you create them yourself (and it isn't a requirement to have them). – Michael Petch Nov 07 '15 at 14:26
  • I won't do it only in assembly, I go to C as much as I can. – Paul Nov 07 '15 at 22:12
  • If you are using _C_ then you will almost certainly have to deal with _.bss_ . But that is actually being taken care of if you build an ELF binary and use multiboot. Grub will intiialize your `.bss` to all zeroes for you. Your flags are set to multiboot 0x0 which indicates ELF. What you should do is create a bootstrap stack (when grub loads you you can't rely on there being a valid stack for you to use). You can get an idea of how to do that here: http://wiki.osdev.org/Bare_Bones_with_NASM . You'll also want to compile your _C_ code (if using gcc) with `-ffreestanding -nostdlib -lgcc` – Michael Petch Nov 07 '15 at 22:57
  • And lastly if you are going to use C/C++ please heed the advice on the sites about using a cross compiler to generate the kernel. In particular you want an `i686-elf-gcc` cross compiler. The OS development sites should link to instructions on building one. If you use `gcc` that comes in your host environment you may find little gotchyas in the generated code. This is because the compiler and _C_ runtime will target your host environment not a bare bones environment. Trust me you don't want to have to come back to SO to help us figure out bugs caused by using the wrong _gcc_ – Michael Petch Nov 07 '15 at 22:57
  • @MichaelPetch Could you please recommend a link for building a cross compiler? – Paul Nov 09 '15 at 17:48
  • OSDev has instructions on doing it http://wiki.osdev.org/GCC_Cross-Compiler . What OS are you doing development on? (Ubuntu, Windows, OSX etc?) – Michael Petch Nov 09 '15 at 17:58
  • If on Debian install the required dev libraries with the line `apt-get install libgmp3-dev libmpfr-dev libisl-dev libcloog-isl-dev libmpc-dev texinfo` . Use [2.24 of Binutils](ftp://ftp.gnu.org/gnu/binutils/binutils-2.24.tar.gz) and [GCC 5.2.0](ftp://ftp.gnu.org/gnu/gcc/gcc-5.2.0/gcc-5.2.0.tar.gz) sources. Extract both into a new directory and then follow the instructions per the http://wiki.osdev.org/GCC_Cross-Compiler_Cross-Compiler link starting at `The Build`. In the instructions don't do any of the lines that start with `mv` (the apt-get ine has already installed them above) – Michael Petch Nov 10 '15 at 16:49
  • GCC 5.2.0 is the latest as of this writing. Binutils is slightly older (they are up to 2.25) and I suggest using 2.24. The reason for this is that GCC 5.2.0 will not compile with the current Binutils 2.25 (I learned the hard way). – Michael Petch Nov 10 '15 at 16:50
  • Wow, thanks for the handy information! – Paul Nov 10 '15 at 20:18
  • No problem. I see I got the link in my last post wrong. It should have been http://wiki.osdev.org/GCC_Cross-Compiler . I got it right in the earlier comment (you probably realized that) – Michael Petch Nov 10 '15 at 20:21
  • `section text:` is a bug with the colon on the end, it should be `section text` – Michael Petch Mar 04 '19 at 17:25

0 Answers0