ELFObjectWriter: Simplify
diff --git a/llvm/lib/MC/ELFObjectWriter.cpp b/llvm/lib/MC/ELFObjectWriter.cpp index 5c39dae..55a6bab 100644 --- a/llvm/lib/MC/ELFObjectWriter.cpp +++ b/llvm/lib/MC/ELFObjectWriter.cpp
@@ -1309,15 +1309,13 @@ bool ELFObjectWriter::checkRelocation(MCContext &Ctx, SMLoc Loc, const MCSectionELF *From, const MCSectionELF *To) { - if (DwoOS) { - if (isDwoSection(*From)) { - Ctx.reportError(Loc, "A dwo section may not contain relocations"); - return false; - } - if (To && isDwoSection(*To)) { - Ctx.reportError(Loc, "A relocation may not refer to a dwo section"); - return false; - } + if (isDwoSection(*From)) { + Ctx.reportError(Loc, "A dwo section may not contain relocations"); + return false; + } + if (To && isDwoSection(*To)) { + Ctx.reportError(Loc, "A relocation may not refer to a dwo section"); + return false; } return true; } @@ -1327,19 +1325,36 @@ const MCFixup &Fixup, MCValue Target, uint64_t &FixedValue) { MCAsmBackend &Backend = Asm.getBackend(); + const MCSectionELF &FixupSection = cast<MCSectionELF>(*Fragment->getParent()); + MCContext &Ctx = Asm.getContext(); + + const auto *SymA = cast_or_null<MCSymbolELF>(Target.getAddSym()); + bool ViaWeakRef = false; + if (SymA && SymA->isVariable()) { + const MCExpr *Expr = SymA->getVariableValue(); + if (const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr)) { + if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF) { + SymA = cast<MCSymbolELF>(&Inner->getSymbol()); + ViaWeakRef = true; + } + } + } + + const MCSectionELF *SecA = (SymA && SymA->isInSection()) + ? cast<MCSectionELF>(&SymA->getSection()) + : nullptr; + if (DwoOS && !checkRelocation(Ctx, Fixup.getLoc(), &FixupSection, SecA)) + return; + bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags & MCFixupKindInfo::FKF_IsPCRel; - const MCSectionELF &FixupSection = cast<MCSectionELF>(*Fragment->getParent()); - uint64_t C = Target.getConstant(); uint64_t FixupOffset = Asm.getFragmentOffset(*Fragment) + Fixup.getOffset(); - MCContext &Ctx = Asm.getContext(); - const MCTargetOptions *TO = Ctx.getTargetOptions(); - + uint64_t Addend = Target.getConstant(); if (auto *RefB = Target.getSubSym()) { // When there is no relocation specifier, a linker relaxation target may // emit ADD/SUB relocations for A-B+C. - if (Target.getAddSym() && Backend.handleAddSubRelocations( - Asm, *Fragment, Fixup, Target, FixedValue)) + if (SymA && Backend.handleAddSubRelocations(Asm, *Fragment, Fixup, Target, + FixedValue)) return; const auto &SymB = cast<MCSymbolELF>(*RefB); @@ -1360,29 +1375,9 @@ assert(!IsPCRel && "should have been folded"); IsPCRel = true; - C += FixupOffset - Asm.getSymbolOffset(SymB); + Addend += FixupOffset - Asm.getSymbolOffset(SymB); } - // We either rejected the fixup or folded B into C at this point. - const auto *SymA = cast_or_null<MCSymbolELF>(Target.getAddSym()); - - bool ViaWeakRef = false; - if (SymA && SymA->isVariable()) { - const MCExpr *Expr = SymA->getVariableValue(); - if (const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr)) { - if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF) { - SymA = cast<MCSymbolELF>(&Inner->getSymbol()); - ViaWeakRef = true; - } - } - } - - const MCSectionELF *SecA = (SymA && SymA->isInSection()) - ? cast<MCSectionELF>(&SymA->getSection()) - : nullptr; - if (!checkRelocation(Ctx, Fixup.getLoc(), &FixupSection, SecA)) - return; - auto EMachine = TargetObjectWriter->getEMachine(); unsigned Type; if (mc::isRelocRelocation(Fixup.getKind())) @@ -1393,14 +1388,16 @@ bool UseSectionSym = SymA && SymA->getBinding() == ELF::STB_LOCAL && !SymA->isUndefined(); if (UseSectionSym) { - UseSectionSym = useSectionSymbol(Asm, Target, SymA, C, Type); + UseSectionSym = useSectionSymbol(Asm, Target, SymA, Addend, Type); // Disable STT_SECTION adjustment for .reloc directives. UseSectionSym &= !mc::isRelocRelocation(Fixup.getKind()); + + if (UseSectionSym) + Addend += Asm.getSymbolOffset(*SymA); } - uint64_t Addend = UseSectionSym ? C + Asm.getSymbolOffset(*SymA) : C; - FixedValue = usesRela(TO, FixupSection) ? 0 : Addend; + FixedValue = usesRela(Ctx.getTargetOptions(), FixupSection) ? 0 : Addend; if (UseSectionSym) { SymA = cast<MCSymbolELF>(SecA->getBeginSymbol()); SymA->setUsedInReloc();