#include "bolt/Core/BinaryFunction.h"
#include "bolt/Rewrite/MetadataRewriter.h"
#include "bolt/Rewrite/MetadataRewriters.h"
#include "llvm/Support/Errc.h"
using namespace llvm;
using namespace bolt;
namespace {
class RSeqRewriter final : public MetadataRewriter {
public:
RSeqRewriter(StringRef Name, BinaryContext &BC)
: MetadataRewriter(Name, BC) {}
Error preCFGInitializer() override {
for (const BinarySection &Section : BC.allocatableSections()) {
if (Section.getName() != "__rseq_cs")
continue;
auto handleRelocation = [&](const Relocation &Rel, bool IsDynamic) {
BinaryFunction *BF = nullptr;
if (Rel.Symbol)
BF = BC.getFunctionForSymbol(Rel.Symbol);
else if (Relocation::isRelative(Rel.Type))
BF = BC.getBinaryFunctionContainingAddress(Rel.Addend);
if (!BF) {
BC.errs() << "BOLT-WARNING: no function found matching "
<< (IsDynamic ? "dynamic " : "")
<< "relocation in __rseq_cs\n";
} else if (!BF->isIgnored()) {
BC.outs() << "BOLT-INFO: restartable sequence reference detected in "
<< *BF << ". Function will not be optimized\n";
BF->setIgnored();
}
};
for (const Relocation &Rel : Section.dynamicRelocations())
handleRelocation(Rel, true);
for (const Relocation &Rel : Section.relocations())
handleRelocation(Rel, false);
}
return Error::success();
}
};
}
std::unique_ptr<MetadataRewriter>
llvm::bolt::createRSeqRewriter(BinaryContext &BC) {
return std::make_unique<RSeqRewriter>("rseq-cs-rewriter", BC);
}