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.
COMMON
?.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?
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).
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.
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.
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.