This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Is it possible to use a linker script to preserve unreferenced static C++ class instances defined in a static library?


Greetings,

  I am trying to convert a C++ shared library to be a static library.
Unfortunately one of the issues I've hit is that this library has some
code that uses a C++ static initialization trick whereby there are
static instances of a class that are not referenced beyond where they
are defined.  Subsequently the linker decides to eliminate these
instances causing the resulting binary to fail to function correctly.

  I've spent a few days trying to puzzle out if I can use the KEEP
linker script directive to instruct the linker to retain these
unreferenced static C++ class instances.  I have read about the
-Wl,--whole-archive option I could pass to the linker, but I do not
want to retain everything within the archive (in fact the reason for
moving to static linking is to allow the linker to drop from the
resulting binary lots of unused functions/objects reducing the run
time image size).

  Currently I'm using this linker script in an attempt to get the
linker to keep all items in the .data segment:

SECTIONS
{
    .data :
    {
        KEEP(*(.data))
    }
}

  But it seems to have no impact.  I'm not sure if what I am trying to
do isn't possible with KEEP or if I'm simply not generating a valid
linker script.  I would appreciate any advice on this topic.  I'd also
like to point out that I'm willing to rename these static instances
using a pattern that the KEEP statement could match (again if that's
possible).

  I'm using the 2.19.1 version of GNU ld if that matters/helps.

-bob


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]