[BOLT] Fix crash when trying to resolve external symbols for runtime libs

Summary:
As pointed out by Vladislav in issue 217, if our RTDyld-based
linker fails to locate a symbol, it will crash with segfault. Fix that.

(cherry picked from FBD32481543)
This commit is contained in:
Rafael Auler 2021-11-16 16:47:02 -08:00 committed by Maksim Panchenko
parent e4ccdfe84f
commit a23726bb33
1 changed files with 11 additions and 17 deletions

View File

@ -2906,10 +2906,8 @@ namespace {
class BOLTSymbolResolver : public JITSymbolResolver {
BinaryContext &BC;
RuntimeDyld &RTDyld;
public:
BOLTSymbolResolver(BinaryContext &BC, RuntimeDyld &RTDyld)
: BC(BC), RTDyld(RTDyld) {}
BOLTSymbolResolver(BinaryContext &BC) : BC(BC) {}
// We are responsible for all symbols
Expected<LookupSet> getResponsibilitySet(const LookupSet &Symbols) override {
@ -2928,20 +2926,16 @@ public:
for (const StringRef &Symbol : Symbols) {
std::string SymName = Symbol.str();
LLVM_DEBUG(dbgs() << "BOLT: looking for " << SymName << "\n");
JITEvaluatedSymbol Result = RTDyld.getSymbol(SymName);
if (Result.getAddress() == 0) {
// Resolve to a PLT entry if possible
if (BinaryData *I = BC.getBinaryDataByName(SymName + "@PLT")) {
AllResults[Symbol] =
JITEvaluatedSymbol(I->getAddress(), JITSymbolFlags());
continue;
}
OnResolved(make_error<StringError>(
"Symbol not found required by runtime: " + Symbol,
inconvertibleErrorCode()));
return;
// Resolve to a PLT entry if possible
if (BinaryData *I = BC.getBinaryDataByName(SymName + "@PLT")) {
AllResults[Symbol] =
JITEvaluatedSymbol(I->getAddress(), JITSymbolFlags());
continue;
}
AllResults[Symbol] = Result;
OnResolved(make_error<StringError>(
"Symbol not found required by runtime: " + Symbol,
inconvertibleErrorCode()));
return;
}
OnResolved(std::move(AllResults));
return;
@ -3018,7 +3012,7 @@ void RewriteInstance::emitAndLink() {
object::ObjectFile::createObjectFile(ObjectMemBuffer->getMemBufferRef()),
"error creating in-memory object");
BOLTSymbolResolver Resolver = BOLTSymbolResolver(*BC, *RTDyld);
BOLTSymbolResolver Resolver = BOLTSymbolResolver(*BC);
MCAsmLayout FinalLayout(
static_cast<MCObjectStreamer *>(Streamer.get())->getAssembler());