What is the effect of a `section` command with an empty list of input sections in a GNU linker script?

StackOverflow https://stackoverflow.com/questions/17054882

Question

In an LD linker script I have the following fragment in the SECTIONS section:

. = (__BUFFER_LOCATION_); 
BUFFER . : { } > EXTERNAL_MEM

where __BUFFER_LOCATION_ is defined to some address and EXTERNAL_MEM is defined in the MEMORY section.

In the C program, I have a global buffer declared as:

char outbuf[4096] __attribute__((section("BUFFER")));

It can be seen that the linker script does not mention any input section named BUFFER, but the output section is named as such.

When compiling the program I see that the linker placed the buffer in the supposed address (BUFFER_LOCATION), although the input section was not defined in the LDF. When I remove the attribute from the source, the buffer is placed in a completely different address.

So, I assume that by default, an output-section-command of type "input section description" adds the output section's name to the input sections list implicitly, unless defined somewhere else. However, reading the manual, I could not find a description of such behaviour.

Did I miss something, or is it an "undocumented feature"?

Was it helpful?

Solution

Yes, an output section will automatically match input sections with the same name, unless a different output section mentions them explicitly.

This is documented under Orphan Sections (emphasis mine):

Orphan sections are sections present in the input files which are not explicitly placed into the output file by the linker script. The linker will still copy these sections into the output file by either finding, or creating a suitable output section in which to place the orphaned input section.

If the name of an orphaned input section exactly matches the name of an existing output section, then the orphaned input section will be placed at the end of that output section.

If there is no output section with a matching name then new output sections will be created...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top