diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h --- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h +++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h @@ -12,6 +12,23 @@ #include "RISCVTargetStreamer.h" #include "llvm/MC/MCELFStreamer.h" +using namespace llvm; + +class RISCVELFStreamer : public MCELFStreamer { + static std::pair getRelocPairForSize(unsigned Size); + static bool requiresFixups(MCContext &C, const MCExpr *Value, + const MCExpr *&LHS, const MCExpr *&RHS); + void reset() override; + +public: + RISCVELFStreamer(MCContext &C, std::unique_ptr MAB, + std::unique_ptr MOW, + std::unique_ptr MCE) + : MCELFStreamer(C, std::move(MAB), std::move(MOW), std::move(MCE)) {} + + void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override; +}; + namespace llvm { class RISCVTargetELFStreamer : public RISCVTargetStreamer { @@ -95,7 +112,7 @@ void reset() override; public: - MCELFStreamer &getStreamer(); + RISCVELFStreamer &getStreamer(); RISCVTargetELFStreamer(MCStreamer &S, const MCSubtargetInfo &STI); void emitDirectiveOptionPush() override; diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp --- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp +++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp @@ -39,8 +39,8 @@ MAB.getTargetOptions().getABIName())); } -MCELFStreamer &RISCVTargetELFStreamer::getStreamer() { - return static_cast(Streamer); +RISCVELFStreamer &RISCVTargetELFStreamer::getStreamer() { + return static_cast(Streamer); } void RISCVTargetELFStreamer::emitDirectiveOptionPush() {} @@ -187,97 +187,85 @@ Contents.clear(); } -namespace { -class RISCVELFStreamer : public MCELFStreamer { - static std::pair getRelocPairForSize(unsigned Size) { - switch (Size) { - default: - llvm_unreachable("unsupported fixup size"); - case 1: - return std::make_pair(RISCV::fixup_riscv_add_8, RISCV::fixup_riscv_sub_8); - case 2: - return std::make_pair(RISCV::fixup_riscv_add_16, - RISCV::fixup_riscv_sub_16); - case 4: - return std::make_pair(RISCV::fixup_riscv_add_32, - RISCV::fixup_riscv_sub_32); - case 8: - return std::make_pair(RISCV::fixup_riscv_add_64, - RISCV::fixup_riscv_sub_64); - } +std::pair +RISCVELFStreamer::getRelocPairForSize(unsigned Size) { + switch (Size) { + default: + llvm_unreachable("unsupported fixup size"); + case 1: + return std::make_pair(RISCV::fixup_riscv_add_8, RISCV::fixup_riscv_sub_8); + case 2: + return std::make_pair(RISCV::fixup_riscv_add_16, RISCV::fixup_riscv_sub_16); + case 4: + return std::make_pair(RISCV::fixup_riscv_add_32, RISCV::fixup_riscv_sub_32); + case 8: + return std::make_pair(RISCV::fixup_riscv_add_64, RISCV::fixup_riscv_sub_64); } +} - static bool requiresFixups(MCContext &C, const MCExpr *Value, - const MCExpr *&LHS, const MCExpr *&RHS) { - const auto *MBE = dyn_cast(Value); - if (MBE == nullptr) - return false; +bool RISCVELFStreamer::requiresFixups(MCContext &C, const MCExpr *Value, + const MCExpr *&LHS, const MCExpr *&RHS) { + const auto *MBE = dyn_cast(Value); + if (MBE == nullptr) + return false; - MCValue E; - if (!Value->evaluateAsRelocatable(E, nullptr, nullptr)) - return false; - if (E.getSymA() == nullptr || E.getSymB() == nullptr) - return false; + MCValue E; + if (!Value->evaluateAsRelocatable(E, nullptr, nullptr)) + return false; + if (E.getSymA() == nullptr || E.getSymB() == nullptr) + return false; - const auto &A = E.getSymA()->getSymbol(); - const auto &B = E.getSymB()->getSymbol(); + const auto &A = E.getSymA()->getSymbol(); + const auto &B = E.getSymB()->getSymbol(); - LHS = - MCBinaryExpr::create(MCBinaryExpr::Add, MCSymbolRefExpr::create(&A, C), + LHS = MCBinaryExpr::create(MCBinaryExpr::Add, MCSymbolRefExpr::create(&A, C), MCConstantExpr::create(E.getConstant(), C), C); - RHS = E.getSymB(); - - // If either symbol is in a text section, we need to delay the relocation - // evaluation as relaxation may alter the size of the symbol. - // - // Unfortunately, we cannot identify if the symbol was built with relaxation - // as we do not track the state per symbol or section. However, BFD will - // always emit the relocation and so we follow suit which avoids the need to - // track that information. - if (A.isInSection() && A.getSection().getKind().isText()) - return true; - if (B.isInSection() && B.getSection().getKind().isText()) - return true; - - // Support cross-section symbolic differences ... - return A.isInSection() && B.isInSection() && - A.getSection().getName() != B.getSection().getName(); - } - - void reset() override { - static_cast(getTargetStreamer())->reset(); - MCELFStreamer::reset(); - } + RHS = E.getSymB(); + + // If either symbol is in a text section, we need to delay the relocation + // evaluation as relaxation may alter the size of the symbol. + // + // Unfortunately, we cannot identify if the symbol was built with relaxation + // as we do not track the state per symbol or section. However, BFD will + // always emit the relocation and so we follow suit which avoids the need to + // track that information. + if (A.isInSection() && A.getSection().getKind().isText()) + return true; + if (B.isInSection() && B.getSection().getKind().isText()) + return true; + + // Support cross-section symbolic differences ... + return A.isInSection() && B.isInSection() && + A.getSection().getName() != B.getSection().getName(); +} -public: - RISCVELFStreamer(MCContext &C, std::unique_ptr MAB, - std::unique_ptr MOW, - std::unique_ptr MCE) - : MCELFStreamer(C, std::move(MAB), std::move(MOW), std::move(MCE)) {} +void RISCVELFStreamer::reset() { + static_cast(getTargetStreamer())->reset(); + MCELFStreamer::reset(); +} - void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override { - const MCExpr *A, *B; - if (!requiresFixups(getContext(), Value, A, B)) - return MCELFStreamer::emitValueImpl(Value, Size, Loc); +void RISCVELFStreamer::emitValueImpl(const MCExpr *Value, unsigned Size, + SMLoc Loc) { + const MCExpr *A, *B; + if (!requiresFixups(getContext(), Value, A, B)) + return MCELFStreamer::emitValueImpl(Value, Size, Loc); - MCStreamer::emitValueImpl(Value, Size, Loc); + MCStreamer::emitValueImpl(Value, Size, Loc); - MCDataFragment *DF = getOrCreateDataFragment(); - flushPendingLabels(DF, DF->getContents().size()); - MCDwarfLineEntry::make(this, getCurrentSectionOnly()); + MCDataFragment *DF = getOrCreateDataFragment(); + flushPendingLabels(DF, DF->getContents().size()); + MCDwarfLineEntry::make(this, getCurrentSectionOnly()); - unsigned Add, Sub; - std::tie(Add, Sub) = getRelocPairForSize(Size); + unsigned Add, Sub; + std::tie(Add, Sub) = getRelocPairForSize(Size); - DF->getFixups().push_back(MCFixup::create( - DF->getContents().size(), A, static_cast(Add), Loc)); - DF->getFixups().push_back(MCFixup::create( - DF->getContents().size(), B, static_cast(Sub), Loc)); + DF->getFixups().push_back(MCFixup::create( + DF->getContents().size(), A, static_cast(Add), Loc)); + DF->getFixups().push_back(MCFixup::create( + DF->getContents().size(), B, static_cast(Sub), Loc)); - DF->getContents().resize(DF->getContents().size() + Size, 0); - } -}; -} // namespace + DF->getContents().resize(DF->getContents().size() + Size, 0); +} namespace llvm { MCELFStreamer *createRISCVELFStreamer(MCContext &C,