Search code examples
clinkerldsections

Why do section names start with a dot? Questions on naming sections


Until recently I thought that section names generally have to start with a dot .. However, When studying the sample linker file of my bare-metal-C-IDE, I noticed that there seems to be one exception: The section COMMON.

.text :
{
    KEEP(*(.isr_vector))
    *(.text*)

    KEEP(*(.init))
    KEEP(*(.fini))

    /* .ctors */
    *crtbegin.o(.ctors)
    *crtbegin?.o(.ctors)
    *(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors)
    *(SORT(.ctors.*))
    *(.ctors)

    /* .dtors */
    *crtbegin.o(.dtors)
    *crtbegin?.o(.dtors)
    *(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors)
    *(SORT(.dtors.*))
    *(.dtors)

    *(.rodata*)

    KEEP(*(.eh_frame*))
} > ROM

.bss (NOLOAD):
{
    *(.bss*)
    *(COMMON)
} > RAM

This led me to the conclusion that beginning the section names with . seems to merely be a convention rather than a requirement.

So, my questions are:

  • What is the reason for starting section names with a dot? I suppose there there was a (perhaps historical?) reason for this convention to be established.
  • Why was an exception made for COMMON?
  • Why do usually output sections have the same name as input sections? It was rather confusing for me that there is actually a non-.bss sections included in my .bss output section. Same goes with the .text section. There are plenty of non-.text sections included in my .text output section. Why isn't it common just to give them their own output section? Wouldn't that be much more logical?

Are there any real reasons behind this, or is it just the way it is?


Solution

  • What is the reason for starting section names with a dot?

    As noted by @cremno in the comments, the reason why a dot is used as a prefix for the linked sections is because the ELF (Executable and Linkable Format) specification (archive) reserves them for the system. In Book I, p. 16 (p. 30):

    Section names with a dot (.) prefix are reserved for the system, although applications may use these sections if their existing meanings are satisfactory. Applications may use names without the prefix to avoid conflicts with system sections. The object file format lets one define sections not in the list above. An object file may have more than one section with the same name.

    The specification also defines the names and meanings for the standard sections — .text, .rodata and .data among others — in Book I, pp. 15-16 (pp. 29-30).

    Why was an exception made for COMMON?

    Because the developer wanted to and because the specification permits them to make that exception. In this case, it is presumably the developers of the Fortran compiler who made that choice. As quoted above, the application may use the standard sections if they fit the needs of the application. Apparently these developers felt that a non-standard section name was needed.

    Why do output sections usually have the same name as input sections? Why isn't each input section given a separate output section?

    You answer the first question: "It was rather confusing ...". It is, indeed! That confusion would be exactly why the output section name is generally used as a prefix when defining a new section. Doing so makes the final result more predictable and allows the developer ignore the existence of the linker script since most linker scripts define wildcards for these sections.

    The reason why an input section might be placed into a differently-named output section is because a given (boot)loader or application do not know about the existence of said section (input or output). Perhaps you have non-standard sections, such as the .ctor and .dtor sections in the example, and the target system only loads/interacts with the .text, .rodata, .bss and .data sections. Or maybe you are writing a code generator and some parts of your .text segment containing compiled code fragments need to be placed in the .rodata segment for read access.

    Are there any real reasons behind this, or is it just the way it is?

    Answer is yes. Some people have agreed to do things in arbitrary ways and we follow their agreement(s) because they have written a bunch of useful software that we need to get our work done.