I am not sure this is the right list for this question, but i will ask it anyways.
some of my functions which are not used in the final executable are optimized away by the clang++ linker. Is there any way to prevent clang++ to strip away these functions ? Maybe by some attribute prefixed at the the function signature ? I did some google search, but with no luck.
Thank you,
Trent
some of my functions which are not used in the final
executable are optimized away by the clang++ linker.
Is there any way to prevent clang++ to strip away
these functions ?
The obvious question first: why is this a problem? If they are not used, why do you need them?
An easy way to keep them around is to create a reference to them, so that they are “used”
in some way. Calls to them, or pointers to them stored in globals, should do the trick.
I want to pass them to libraries later loaded into the application.
If the global is not used, would the linker be able to optimize
the global out and in turn optimize the function out again ?
Potentially, but the fix is fairly simple. Create a global that holds pointers to the functions. Have the
code that wants to pass pointers to those functions read the pointers from that global.
However, this begs the question of why the part of your code that currently tries to pass the function
pointers isn’t being recognised as referring to the functions in question. How does that code work?
> I want to pass them to libraries later loaded into the application.
> If the global is not used, would the linker be able to optimize
> the global out and in turn optimize the function out again ?
Potentially, but the fix is fairly simple. Create a global that holds
pointers to the functions. Have the
code that wants to pass pointers to those functions read the pointers from
that global.
However, this begs the question of why the part of your code that
currently tries to pass the function
pointers isn't being recognised as referring to the functions in question.
How does that code work?
Ok this is the whole story. These *unused* functions are defined in LLVM
lli. and they are referenced in the bitcode lli is interpreting/JITting.
the bitcode expects lli to able to resolve these functions at JIT time.
basically, the bitcode is somewhat like libraries loaded into the lli
application. However, the linker optimized these functions out because it
sees no invocation of them in the final executable.
Ok this is the whole story. …
Oh, right. If you’d explained that, I’d have stayed out of the whole issue: I have no
experience of working with bitcode.
Perhaps try some combination of:
__attribute__((used))
to prevent the symbol from being removed and perhaps
-rdynamic
when linking to ensure the symbols are exported.
Good luck, hope this helps!
~Will