Forcing a constructor to be linked
Dave Korn
dave.korn@artimi.com
Tue Jul 12 12:39:00 GMT 2005
----Original Message----
>From: Ian Lance Taylor
>Sent: 12 July 2005 03:56
> Darin Johnson writes:
>
>> I'm running across a linking problem when porting to embedded PowerPC
>> with
>> GNU tools. There's a file that was normally linked in because it has a
>> static constructor (and thus a .ctors section), but there are no other
>> external references to that file. Ie, the static constructor registers
>> a hardware test to a central list that will be run later, and the tests
>> that are included depend upon the libraries included in the build, not
>> upon modifying a source file to add or remove test functions.
>>
>> This works fine with GNU ld if the the object file is listed on the
>> command line. However if the file is inside of a library it will not be
>> linked in, even though there's a KEEP() around the .ctors. Is there an
>> ld option to cause files like this to be linked without having to
>> explicitly create references to them?
>
> Not really. The linker will only bring in files from an archive if
> they are referenced. Merely having a .ctors section is not a reference.
>
> You could use --whole-archive, but that is probably not what you are
> after.
>
> Ian
The approach we use is to generate a whole load of -u options as part of
the make process that we pass in to ld at final-link time to ensure that
everything from unreferenced .ctor/.dtor sections gets pulled in. You can
perhaps extract the necessary symbols by using nm on newly-generated .o
files, as part of the makefile rule, and keep them handy somewhere in a
similar way to autogenerated dependency files.
cheers,
DaveK
--
Can't think of a witty .sigline today....
More information about the Binutils
mailing list