[llvm] 9d3ef8a - ELFObjectWriter: Simplify

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sun May 18 14:18:25 PDT 2025


Author: Fangrui Song
Date: 2025-05-18T14:18:20-07:00
New Revision: 9d3ef8a66fbcb614843ae6e864d0b0a950ceb9ae

URL: https://github.com/llvm/llvm-project/commit/9d3ef8a66fbcb614843ae6e864d0b0a950ceb9ae
DIFF: https://github.com/llvm/llvm-project/commit/9d3ef8a66fbcb614843ae6e864d0b0a950ceb9ae.diff

LOG: ELFObjectWriter: Simplify

Added: 
    

Modified: 
    llvm/lib/MC/ELFObjectWriter.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/MC/ELFObjectWriter.cpp b/llvm/lib/MC/ELFObjectWriter.cpp
index 5c39daef9c98d..55a6bab003b31 100644
--- a/llvm/lib/MC/ELFObjectWriter.cpp
+++ b/llvm/lib/MC/ELFObjectWriter.cpp
@@ -1309,15 +1309,13 @@ bool ELFObjectWriter::useSectionSymbol(const MCAssembler &Asm,
 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 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
                                        const MCFixup &Fixup, MCValue Target,
                                        uint64_t &FixedValue) {
   MCAsmBackend &Backend = Asm.getBackend();
-  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();
 
+  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;
+  uint64_t FixupOffset = Asm.getFragmentOffset(*Fragment) + Fixup.getOffset();
+  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 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
 
     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 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
   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();


        


More information about the llvm-commits mailing list