1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-19 02:52:53 +02:00

[ORC] Attempt to auto-claim responsibility for weak defs in ObjectLinkingLayer.

Compilers may insert new definitions during compilation, E.g. EH personality
function pointers, or named constant pool entries. This commit causes
ObjectLinkingLayer to attempt to claim responsibility for all weak definitions
in objects as they're linked. This is always safe (first claimant for each
symbol is granted responsibility, subsequent claims are rejected without error)
and prevents compiler-injected symbols from being dead-stripped (which they
will be if they remain unclaimed by anyone).

This change was motivated by errors seen by an out-of-tree client while testing
eh-frame support in JITLink ELF/x86-64: IR containing exceptions didn't define
DW.ref.__gxx_personality_v0 (since it's added by CodeGen), and this caused
DW.ref.__gxx_personality_v0 to be dead-stripped leading to linker failures.

No test case yet: We won't have a way to test in-tree until we enable JITLink
for lli on Linux.
This commit is contained in:
Lang Hames 2021-01-26 23:56:01 +11:00
parent 8bc0e661cd
commit 4a5753f9a7

View File

@ -214,8 +214,9 @@ public:
Error modifyPassConfig(const Triple &TT, PassConfiguration &Config) override { Error modifyPassConfig(const Triple &TT, PassConfiguration &Config) override {
// Add passes to mark duplicate defs as should-discard, and to walk the // Add passes to mark duplicate defs as should-discard, and to walk the
// link graph to build the symbol dependence graph. // link graph to build the symbol dependence graph.
Config.PrePrunePasses.push_back( Config.PrePrunePasses.push_back([this](LinkGraph &G) {
[this](LinkGraph &G) { return externalizeWeakAndCommonSymbols(G); }); return claimOrExternalizeWeakAndCommonSymbols(G);
});
Layer.modifyPassConfig(*MR, TT, Config); Layer.modifyPassConfig(*MR, TT, Config);
@ -233,19 +234,38 @@ private:
using LocalSymbolNamedDependenciesMap = using LocalSymbolNamedDependenciesMap =
DenseMap<const Symbol *, LocalSymbolNamedDependencies>; DenseMap<const Symbol *, LocalSymbolNamedDependencies>;
Error externalizeWeakAndCommonSymbols(LinkGraph &G) { Error claimOrExternalizeWeakAndCommonSymbols(LinkGraph &G) {
auto &ES = Layer.getExecutionSession(); auto &ES = Layer.getExecutionSession();
for (auto *Sym : G.defined_symbols())
if (Sym->hasName() && Sym->getLinkage() == Linkage::Weak) {
if (!MR->getSymbols().count(ES.intern(Sym->getName())))
G.makeExternal(*Sym);
}
for (auto *Sym : G.absolute_symbols()) SymbolFlagsMap NewSymbolsToClaim;
std::vector<std::pair<SymbolStringPtr, Symbol *>> NameToSym;
auto ProcessSymbol = [&](Symbol *Sym) {
if (Sym->hasName() && Sym->getLinkage() == Linkage::Weak) { if (Sym->hasName() && Sym->getLinkage() == Linkage::Weak) {
if (!MR->getSymbols().count(ES.intern(Sym->getName()))) auto Name = ES.intern(Sym->getName());
G.makeExternal(*Sym); if (!MR->getSymbols().count(ES.intern(Sym->getName()))) {
JITSymbolFlags SF = JITSymbolFlags::Weak;
if (Sym->getScope() == Scope::Default)
SF |= JITSymbolFlags::Exported;
NewSymbolsToClaim[Name] = SF;
NameToSym.push_back(std::make_pair(std::move(Name), Sym));
}
} }
};
for (auto *Sym : G.defined_symbols())
ProcessSymbol(Sym);
for (auto *Sym : G.absolute_symbols())
ProcessSymbol(Sym);
// Attempt to claim all weak defs that we're not already responsible for.
// This cannot fail -- any clashes will just result in rejection of our
// claim, at which point we'll externalize that symbol.
cantFail(MR->defineMaterializing(std::move(NewSymbolsToClaim)));
for (auto &KV : NameToSym)
if (!MR->getSymbols().count(KV.first))
G.makeExternal(*KV.second);
return Error::success(); return Error::success();
} }