Search code examples
linkerldelf

Why should "data = .;" be repeated three times in a linker script?


I saw this link script in http://www.jamesmolloy.co.uk/tutorial_html/1.-Environment%20setup.html

SECTIONS
{

    .text 0x100000 :
    {
        code = .; _code = .; __code = .;    // What is this line for?
        *(.text)
        . = ALIGN(4096);
    }

    .data :
    {
        data = .; _data = .; __data = .;
        *(.data)
        *(.rodata)
        . = ALIGN(4096);
    }

    .bss :
    {
        bss = .; _bss = .; __bss = .;
        *(.bss)
        . = ALIGN(4096);
    }

    end = .; _end = .; __end = .;
}

You can see that, code, _code, __code and the fallowing ones all appearing in a same style. What are they for? Why should they be written in such a way?


Solution

  • The syntax <symbol> = . simply defines a symbol at the current address.

    You can use this symbol like this:

    extern int __code;
    
    int foo()  
    {
        cout << "Address of __code" << &__code << endl;
    }
    

    _code and __code typically holds the start address of the text section. This is used from the startup code of your system you compile for.

    Definig symbols without a leading underscore are not so common I believe. This can maybe result in conflicts with normal definitions from your code. But this is only a convention. Technically you can define what you want and need. The rules are the same as all other symbols in your project: Never define symbols twice :-)