Global Object Constructor of not explicitly referenced object get discarded in final binary - LD

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

  •  07-07-2021
  •  | 
  •  

Question

I'm writing a linker script for LD to collect all .init_array sections in order to call the constructor of all global objects declared in my firmware.

Everything works fine unless I have a global object, not referenced, in a static library. In this case, despite the fact that I use the KEEP macro as follow, the object not explicitly referenced, it get discarded in the final binary. (no matter if I use -gc-sections or not).

If the very same not referenced object is placed outside the static library, thus being caught by the generic line of the linker script, it does get included.

.init_array :
{
   _init_array_start = .;
   KEEP (*core*:*(SORT(.init_array*)))
   KEEP (*core*:*(.init_array))
   KEEP (*(SORT(.init_array.*)))
   KEEP (*(.init_array))
   PROVIDE (_init_array_end = .);
} > flash

The library is called libcore.a. Other objects explicitly referenced in the libcore.a do get included.

Any help on how to fix this would be really appreciated.

Thanks in advance

Was it helpful?

Solution

Static libraries are actually an ar archive containing multiple .o files and an index. The index is used to determine which .o files to link based on what symbols were referenced by the main program. As such, if no symbols from a .o file are referenced, the linker won't even load the .o file in question, so the linker script never gets its hands on it.

Assuming you're using GNU LD, you could use ld's -r flag to combine multiple .o files into a single libcore.o file, and use that as the only .o file in the archive. The downside of this is that you'll potentially be pulling in unnecessary code that would otherwise be elided by skipping .o files. Alternately, you could also add dummy references from all other .o files in the archive to the .o file with your static constructor.

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