Linking files

Programming, for all ages and all languages.
Post Reply
Frans
Posts: 4
Joined: Mon Nov 05, 2007 2:15 pm

Linking files

Post by Frans »

Hello,

Yesterday, I restarted working on an OS. I thought it would be easier if I did'nt write a bootloader, and let grub do that work for me. And maybe start later with my own bootloader.

I found Bare bones and C PlusPlus bare bones

They were really helpful, and it's now working, but I don't understand how the linker script works:

Code: Select all

ENTRY (_loader)

SECTIONS{
    . = 0x00100000;

    .text :{
        *(.text)
    }

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

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

    .bss : {
        _sbss = .;
        *(COMMON)
        *(.bss)
        _ebss = .;
    }

    .data ALIGN (0x1000) : {
        start_ctors = .;
        *(.ctor*)
        end_ctors = .;
        start_dtors = .;
        *(.dtor*)
        end_dtors = .;
        *(.data)
    }
}
The first line is the only one I understand (It tells the linker what function starts the program). Can someone explain the other lines?
If you live each day as if it was your last, someday you'll most certainly be right.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

Every program can be sub-divided in to a number of sections. These are normally something like:
  • .text - contains your executable code
    .data - contains initialised variables etc..
    .rodata - contains constant (read only) data
    .bss - contains zeroed data
    .ctor - contains c++ global / static class constructor pointer list
    .dtor - same for destructors
What you are saying is that, in memory, the binary starts at 0x100000 (1MB). Immediately following this, is your code (.text). You then have .rodata, aligned on a 4kb memory boundary (0x1000 bytes). This is followed by your other sections which are also 4kb aligned.

Wherever you see x = .; , x is a variable which is accessible from your code (by declaring an extern variable). In this case, start_ctors and end_ctors allows your code to find pointers to the global / static class constructors and call them when necessary (probably after heap initialisation).

This is pretty basic and shows my limited understanding of linker scripts, but HTH.
Adam
Post Reply