Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"?

like image 391
ysap Avatar asked Nov 18 '25 21:11

ysap


1 Answers

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...

like image 183
Maxpm Avatar answered Nov 21 '25 12:11

Maxpm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!