Page 1 of 1

Linker script in bare bones tutorial

Posted: Fri Jan 04, 2013 1:12 pm
by gsingh2011
I have a few questions about the linker script in the bare bones tutorial. Here is the script:

Code: Select all

ENTRY (loader)

SECTIONS
{
    . = 0x00100000;

    .text ALIGN (0x1000) :
    {
        *(.text)
    }

    .rodata ALIGN (0x1000) :
    {
        *(.rodata*)
    }

    .data ALIGN (0x1000) :
    {
        *(.data)
    }

    .bss :
    {
        sbss = .;
        *(COMMON)
        *(.bss)
        ebss = .;
    }
}
My first question is in this section:

Code: Select all

    .bss :
    {
        sbss = .;
        *(COMMON)
        *(.bss)
        ebss = .;
    }
I assume sbss and ebss stand for start bss and end bss. Why do they need to be there?

My second question is that why we need a linker script in the first place? Isn't ELF a common format? Should ld be able to link it without us writing our own script?

Re: Linker script in bare bones tutorial

Posted: Fri Jan 04, 2013 1:21 pm
by Griwes
http://en.wikipedia.org/wiki/.bss

In general, .bss is used for global variables, but it is not included in executable. `sbss` and `ebss` are there so you can "create" it in memory (for example, move something that was right after executable on disk, zero it or whatever you want).

Personally, since I have recently hit a problem with it, I just put `*(.bss)` in `.data`, so I don't have to worry about it.

As for the "why linker script" question - there might be a chance that ld will get it just right without it, but since in OSDev what you need and what you seek is *control* over what's happening - and linker script gives you that.