[llvm] 843e362 - MachObjectWriter: Simplify Asm.getContext().reportError

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sun May 25 13:06:19 PDT 2025


Author: Fangrui Song
Date: 2025-05-25T13:06:14-07:00
New Revision: 843e362318e884991e517a54446b4faeacdad789

URL: https://github.com/llvm/llvm-project/commit/843e362318e884991e517a54446b4faeacdad789
DIFF: https://github.com/llvm/llvm-project/commit/843e362318e884991e517a54446b4faeacdad789.diff

LOG: MachObjectWriter: Simplify Asm.getContext().reportError

Similar to b65760bc7fcdee8179bf1e57fce3786737528dd8 for ELF.

Added: 
    

Modified: 
    llvm/include/llvm/MC/MCMachObjectWriter.h
    llvm/lib/MC/MachObjectWriter.cpp
    llvm/lib/Target/AArch64/MCTargetDesc/AArch64MachObjectWriter.cpp
    llvm/lib/Target/ARM/MCTargetDesc/ARMMachObjectWriter.cpp
    llvm/lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/MC/MCMachObjectWriter.h b/llvm/include/llvm/MC/MCMachObjectWriter.h
index 1b6c182d086dd..5d8e69b32ba66 100644
--- a/llvm/include/llvm/MC/MCMachObjectWriter.h
+++ b/llvm/include/llvm/MC/MCMachObjectWriter.h
@@ -184,12 +184,8 @@ class MachObjectWriter final : public MCObjectWriter {
 
   const MCSymbol &findAliasedSymbol(const MCSymbol &Sym) const;
 
-  /// \name Lifetime management Methods
-  /// @{
-
   void reset() override;
-
-  /// @}
+  void setAssembler(MCAssembler *Asm) override;
 
   /// \name Utility Methods
   /// @{

diff  --git a/llvm/lib/MC/MachObjectWriter.cpp b/llvm/lib/MC/MachObjectWriter.cpp
index 44d7b75cf3fdb..9377a74b377ea 100644
--- a/llvm/lib/MC/MachObjectWriter.cpp
+++ b/llvm/lib/MC/MachObjectWriter.cpp
@@ -62,6 +62,11 @@ void MachObjectWriter::reset() {
   MCObjectWriter::reset();
 }
 
+void MachObjectWriter::setAssembler(MCAssembler *Asm) {
+  MCObjectWriter::setAssembler(Asm);
+  TargetObjectWriter->setAssembler(Asm);
+}
+
 bool MachObjectWriter::doesSymbolRequireExternRelocation(const MCSymbol &S) {
   // Undefined symbols are always extern.
   if (S.isUndefined())

diff  --git a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64MachObjectWriter.cpp b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64MachObjectWriter.cpp
index 04ca434c764c2..bce99b10f23b5 100644
--- a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64MachObjectWriter.cpp
+++ b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64MachObjectWriter.cpp
@@ -99,8 +99,7 @@ bool AArch64MachObjectWriter::getAArch64FixupKindMachOInfo(
     // This encompasses the relocation for the whole 21-bit value.
     switch (Spec) {
     default:
-      Asm.getContext().reportError(Fixup.getLoc(),
-                                   "ADR/ADRP relocations must be GOT relative");
+      reportError(Fixup.getLoc(), "ADR/ADRP relocations must be GOT relative");
       return false;
     case AArch64MCExpr::M_PAGE:
       RelocType = unsigned(MachO::ARM64_RELOC_PAGE21);
@@ -176,25 +175,24 @@ void AArch64MachObjectWriter::recordRelocation(
   // assembler local symbols. If we got here, that's not what we have,
   // so complain loudly.
   if (Kind == AArch64::fixup_aarch64_pcrel_branch19) {
-    Asm.getContext().reportError(
-        Fixup.getLoc(), "conditional branch requires assembler-local"
-                        " label. '" +
-                            Target.getAddSym()->getName() + "' is external.");
+    reportError(Fixup.getLoc(), "conditional branch requires assembler-local"
+                                " label. '" +
+                                    Target.getAddSym()->getName() +
+                                    "' is external.");
     return;
   }
 
   // 14-bit branch relocations should only target internal labels, and so
   // should never get here.
   if (Kind == AArch64::fixup_aarch64_pcrel_branch14) {
-    Asm.getContext().reportError(Fixup.getLoc(),
-                                 "Invalid relocation on conditional branch!");
+    reportError(Fixup.getLoc(), "Invalid relocation on conditional branch!");
     return;
   }
 
   if (!getAArch64FixupKindMachOInfo(
           Fixup, Type, AArch64MCExpr::Specifier(Target.getSpecifier()),
           Log2Size, Asm)) {
-    Asm.getContext().reportError(Fixup.getLoc(), "unknown AArch64 fixup kind!");
+    reportError(Fixup.getLoc(), "unknown AArch64 fixup kind!");
     return;
   }
 
@@ -206,8 +204,7 @@ void AArch64MachObjectWriter::recordRelocation(
     Type = MachO::ARM64_RELOC_UNSIGNED;
 
     if (IsPCRel) {
-      Asm.getContext().reportError(Fixup.getLoc(),
-                                   "PC relative absolute relocation!");
+      reportError(Fixup.getLoc(), "PC relative absolute relocation!");
       return;
 
       // FIXME: x86_64 sets the type to a branch reloc here. Should we do
@@ -234,16 +231,14 @@ void AArch64MachObjectWriter::recordRelocation(
       return;
     } else if (Target.getSpecifier() != AArch64MCExpr::None) {
       // Otherwise, neither symbol can be modified.
-      Asm.getContext().reportError(Fixup.getLoc(),
-                                   "unsupported relocation of modified symbol");
+      reportError(Fixup.getLoc(), "unsupported relocation of modified symbol");
       return;
     }
 
     // We don't support PCrel relocations of 
diff erences.
     if (IsPCRel) {
-      Asm.getContext().reportError(Fixup.getLoc(),
-                                   "unsupported pc-relative relocation of "
-                                   "
diff erence");
+      reportError(Fixup.getLoc(), "unsupported pc-relative relocation of "
+                                  "
diff erence");
       return;
     }
 
@@ -254,23 +249,20 @@ void AArch64MachObjectWriter::recordRelocation(
     // FIXME: We should probably just synthesize an external symbol and use
     // that.
     if (!A_Base) {
-      Asm.getContext().reportError(
-          Fixup.getLoc(),
-          "unsupported relocation of local symbol '" + A->getName() +
-              "'. Must have non-local symbol earlier in section.");
+      reportError(Fixup.getLoc(),
+                  "unsupported relocation of local symbol '" + A->getName() +
+                      "'. Must have non-local symbol earlier in section.");
       return;
     }
     if (!B_Base) {
-      Asm.getContext().reportError(
-          Fixup.getLoc(),
-          "unsupported relocation of local symbol '" + B->getName() +
-              "'. Must have non-local symbol earlier in section.");
+      reportError(Fixup.getLoc(),
+                  "unsupported relocation of local symbol '" + B->getName() +
+                      "'. Must have non-local symbol earlier in section.");
       return;
     }
 
     if (A_Base == B_Base && A_Base) {
-      Asm.getContext().reportError(
-          Fixup.getLoc(), "unsupported relocation with identical base");
+      reportError(Fixup.getLoc(), "unsupported relocation with identical base");
       return;
     }
 
@@ -303,10 +295,10 @@ void AArch64MachObjectWriter::recordRelocation(
       // Make sure that the symbol is actually in a section here. If it isn't,
       // emit an error and exit.
       if (!Symbol->isInSection()) {
-        Asm.getContext().reportError(
-            Fixup.getLoc(),
-            "unsupported relocation of local symbol '" + Symbol->getName() +
-                "'. Must have non-local symbol earlier in section.");
+        reportError(Fixup.getLoc(),
+                    "unsupported relocation of local symbol '" +
+                        Symbol->getName() +
+                        "'. Must have non-local symbol earlier in section.");
         return;
       }
       const MCSection &Sec = Symbol->getSection();
@@ -340,10 +332,10 @@ void AArch64MachObjectWriter::recordRelocation(
         Value += Asm.getSymbolOffset(*Symbol) - Asm.getSymbolOffset(*Base);
     } else if (Symbol->isInSection()) {
       if (!CanUseLocalRelocation) {
-        Asm.getContext().reportError(
-            Fixup.getLoc(),
-            "unsupported relocation of local symbol '" + Symbol->getName() +
-                "'. Must have non-local symbol earlier in section.");
+        reportError(Fixup.getLoc(),
+                    "unsupported relocation of local symbol '" +
+                        Symbol->getName() +
+                        "'. Must have non-local symbol earlier in section.");
         return;
       }
       // Adjust the relocation to be section-relative.
@@ -369,8 +361,7 @@ void AArch64MachObjectWriter::recordRelocation(
        Type == MachO::ARM64_RELOC_PAGEOFF12) &&
       Value) {
     if (!isInt<24>(Value)) {
-      Asm.getContext().reportError(Fixup.getLoc(),
-                                   "addend too big for relocation");
+      reportError(Fixup.getLoc(), "addend too big for relocation");
       return;
     }
 
@@ -398,21 +389,19 @@ void AArch64MachObjectWriter::recordRelocation(
     assert(Type == MachO::ARM64_RELOC_UNSIGNED);
 
     if (IsPCRel) {
-      Asm.getContext().reportError(Fixup.getLoc(),
-                                   "invalid PC relative auth relocation");
+      reportError(Fixup.getLoc(), "invalid PC relative auth relocation");
       return;
     }
 
     if (Log2Size != 3) {
-      Asm.getContext().reportError(
-          Fixup.getLoc(), "invalid auth relocation size, must be 8 bytes");
+      reportError(Fixup.getLoc(),
+                  "invalid auth relocation size, must be 8 bytes");
       return;
     }
 
     if (Target.getSubSym()) {
-      Asm.getContext().reportError(
-          Fixup.getLoc(),
-          "invalid auth relocation, can't reference two symbols");
+      reportError(Fixup.getLoc(),
+                  "invalid auth relocation, can't reference two symbols");
       return;
     }
 
@@ -420,8 +409,7 @@ void AArch64MachObjectWriter::recordRelocation(
     AArch64PACKey::ID Key = Expr->getKey();
 
     if (!isInt<32>(Value)) {
-      Asm.getContext().reportError(Fixup.getLoc(),
-                                   "addend too big for relocation");
+      reportError(Fixup.getLoc(), "addend too big for relocation");
       return;
     }
 

diff  --git a/llvm/lib/Target/ARM/MCTargetDesc/ARMMachObjectWriter.cpp b/llvm/lib/Target/ARM/MCTargetDesc/ARMMachObjectWriter.cpp
index 72502c5749d91..f6108ede42701 100644
--- a/llvm/lib/Target/ARM/MCTargetDesc/ARMMachObjectWriter.cpp
+++ b/llvm/lib/Target/ARM/MCTargetDesc/ARMMachObjectWriter.cpp
@@ -138,10 +138,9 @@ void ARMMachObjectWriter::recordARMScatteredHalfRelocation(
   uint32_t FixupOffset = Asm.getFragmentOffset(*Fragment) + Fixup.getOffset();
 
   if (FixupOffset & 0xff000000) {
-    Asm.getContext().reportError(Fixup.getLoc(),
-                                 "can not encode offset '0x" +
-                                     utohexstr(FixupOffset) +
-                                     "' in resulting scattered relocation.");
+    reportError(Fixup.getLoc(), "can not encode offset '0x" +
+                                    utohexstr(FixupOffset) +
+                                    "' in resulting scattered relocation.");
     return;
   }
 
@@ -152,9 +151,9 @@ void ARMMachObjectWriter::recordARMScatteredHalfRelocation(
   const MCSymbol *A = Target.getAddSym();
 
   if (!A->getFragment()) {
-    Asm.getContext().reportError(Fixup.getLoc(),
-                       "symbol '" + A->getName() +
-                       "' can not be undefined in a subtraction expression");
+    reportError(Fixup.getLoc(),
+                "symbol '" + A->getName() +
+                    "' can not be undefined in a subtraction expression");
     return;
   }
 
@@ -165,10 +164,9 @@ void ARMMachObjectWriter::recordARMScatteredHalfRelocation(
 
   if (const MCSymbol *SB = Target.getSubSym()) {
     if (!SB->getFragment()) {
-      Asm.getContext().reportError(
-          Fixup.getLoc(),
-          "symbol '" + SB->getName() +
-              "' can not be undefined in a subtraction expression");
+      reportError(Fixup.getLoc(),
+                  "symbol '" + SB->getName() +
+                      "' can not be undefined in a subtraction expression");
       return;
     }
 
@@ -247,10 +245,9 @@ void ARMMachObjectWriter::recordARMScatteredRelocation(
   uint32_t FixupOffset = Asm.getFragmentOffset(*Fragment) + Fixup.getOffset();
 
   if (FixupOffset & 0xff000000) {
-    Asm.getContext().reportError(Fixup.getLoc(),
-                                 "can not encode offset '0x" +
-                                     utohexstr(FixupOffset) +
-                                     "' in resulting scattered relocation.");
+    reportError(Fixup.getLoc(), "can not encode offset '0x" +
+                                    utohexstr(FixupOffset) +
+                                    "' in resulting scattered relocation.");
     return;
   }
 
@@ -260,9 +257,9 @@ void ARMMachObjectWriter::recordARMScatteredRelocation(
   const MCSymbol *A = Target.getAddSym();
 
   if (!A->getFragment()) {
-    Asm.getContext().reportError(Fixup.getLoc(),
-                       "symbol '" + A->getName() +
-                       "' can not be undefined in a subtraction expression");
+    reportError(Fixup.getLoc(),
+                "symbol '" + A->getName() +
+                    "' can not be undefined in a subtraction expression");
     return;
   }
 
@@ -275,10 +272,9 @@ void ARMMachObjectWriter::recordARMScatteredRelocation(
     assert(Type == MachO::ARM_RELOC_VANILLA && "invalid reloc for 2 symbols");
 
     if (!SB->getFragment()) {
-      Asm.getContext().reportError(
-          Fixup.getLoc(),
-          "symbol '" + SB->getName() +
-              "' can not be undefined in a subtraction expression");
+      reportError(Fixup.getLoc(),
+                  "symbol '" + SB->getName() +
+                      "' can not be undefined in a subtraction expression");
       return;
     }
 
@@ -370,7 +366,7 @@ void ARMMachObjectWriter::recordRelocation(MachObjectWriter *Writer,
     // relocation type for the fixup kind. This happens when it's a fixup that's
     // expected to always be resolvable at assembly time and not have any
     // relocations needed.
-    Asm.getContext().reportError(Fixup.getLoc(), "unsupported relocation type");
+    reportError(Fixup.getLoc(), "unsupported relocation type");
     return;
   }
 

diff  --git a/llvm/lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp b/llvm/lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp
index 896c1c3712266..b61dbfd968bd0 100644
--- a/llvm/lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp
+++ b/llvm/lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp
@@ -152,16 +152,15 @@ void X86MachObjectWriter::RecordX86_64Relocation(
 
     // Neither symbol can be modified.
     if (Target.getSpecifier()) {
-      Asm.getContext().reportError(Fixup.getLoc(),
-                                   "unsupported relocation of modified symbol");
+      reportError(Fixup.getLoc(), "unsupported relocation of modified symbol");
       return;
     }
 
     // We don't support PCrel relocations of 
diff erences. Darwin 'as' doesn't
     // implement most of these correctly.
     if (IsPCRel) {
-      Asm.getContext().reportError(
-          Fixup.getLoc(), "unsupported pc-relative relocation of 
diff erence");
+      reportError(Fixup.getLoc(),
+                  "unsupported pc-relative relocation of 
diff erence");
       return;
     }
 
@@ -176,8 +175,7 @@ void X86MachObjectWriter::RecordX86_64Relocation(
     // single SIGNED relocation); reject it for now.  Except the case where both
     // symbols don't have a base, equal but both NULL.
     if (A_Base == B_Base && A_Base) {
-      Asm.getContext().reportError(
-          Fixup.getLoc(), "unsupported relocation with identical base");
+      reportError(Fixup.getLoc(), "unsupported relocation with identical base");
       return;
     }
 
@@ -185,9 +183,10 @@ void X86MachObjectWriter::RecordX86_64Relocation(
     // non-relocatable expression.
     if (A->isUndefined() || B->isUndefined()) {
       StringRef Name = A->isUndefined() ? A->getName() : B->getName();
-      Asm.getContext().reportError(Fixup.getLoc(),
-        "unsupported relocation with subtraction expression, symbol '" +
-        Name + "' can not be undefined in a subtraction expression");
+      reportError(
+          Fixup.getLoc(),
+          "unsupported relocation with subtraction expression, symbol '" +
+              Name + "' can not be undefined in a subtraction expression");
       return;
     }
 
@@ -249,9 +248,9 @@ void X86MachObjectWriter::RecordX86_64Relocation(
       FixedValue = Writer->getSymbolAddress(*Symbol);
       return;
     } else {
-      Asm.getContext().reportError(
-          Fixup.getLoc(), "unsupported relocation of undefined symbol '" +
-                              Symbol->getName() + "'");
+      reportError(Fixup.getLoc(),
+                  "unsupported relocation of undefined symbol '" +
+                      Symbol->getName() + "'");
       return;
     }
 
@@ -269,8 +268,8 @@ void X86MachObjectWriter::RecordX86_64Relocation(
         } else if (Specifier == X86MCExpr::VK_TLVP) {
           Type = MachO::X86_64_RELOC_TLV;
         } else if (Specifier) {
-          Asm.getContext().reportError(
-              Fixup.getLoc(), "unsupported symbol modifier in relocation");
+          reportError(Fixup.getLoc(),
+                      "unsupported symbol modifier in relocation");
           return;
         } else {
           Type = MachO::X86_64_RELOC_SIGNED;
@@ -297,9 +296,8 @@ void X86MachObjectWriter::RecordX86_64Relocation(
         }
       } else {
         if (Specifier) {
-          Asm.getContext().reportError(
-              Fixup.getLoc(),
-              "unsupported symbol modifier in branch relocation");
+          reportError(Fixup.getLoc(),
+                      "unsupported symbol modifier in branch relocation");
           return;
         }
 
@@ -316,17 +314,17 @@ void X86MachObjectWriter::RecordX86_64Relocation(
         Type = MachO::X86_64_RELOC_GOT;
         IsPCRel = 1;
       } else if (Specifier == X86MCExpr::VK_TLVP) {
-        Asm.getContext().reportError(
-            Fixup.getLoc(), "TLVP symbol modifier should have been rip-rel");
+        reportError(Fixup.getLoc(),
+                    "TLVP symbol modifier should have been rip-rel");
         return;
       } else if (Specifier) {
-        Asm.getContext().reportError(
-            Fixup.getLoc(), "unsupported symbol modifier in relocation");
+        reportError(Fixup.getLoc(),
+                    "unsupported symbol modifier in relocation");
         return;
       } else {
         Type = MachO::X86_64_RELOC_UNSIGNED;
         if (Fixup.getTargetKind() == X86::reloc_signed_4byte) {
-          Asm.getContext().reportError(
+          reportError(
               Fixup.getLoc(),
               "32-bit absolute addressing is not supported in 64-bit mode");
           return;
@@ -362,10 +360,9 @@ bool X86MachObjectWriter::recordScatteredRelocation(MachObjectWriter *Writer,
   const MCSymbol *A = Target.getAddSym();
 
   if (!A->getFragment()) {
-    Asm.getContext().reportError(
-        Fixup.getLoc(),
-        "symbol '" + A->getName() +
-            "' can not be undefined in a subtraction expression");
+    reportError(Fixup.getLoc(),
+                "symbol '" + A->getName() +
+                    "' can not be undefined in a subtraction expression");
     return false;
   }
 
@@ -376,10 +373,9 @@ bool X86MachObjectWriter::recordScatteredRelocation(MachObjectWriter *Writer,
 
   if (const MCSymbol *SB = Target.getSubSym()) {
     if (!SB->getFragment()) {
-      Asm.getContext().reportError(
-          Fixup.getLoc(),
-          "symbol '" + SB->getName() +
-              "' can not be undefined in a subtraction expression");
+      reportError(Fixup.getLoc(),
+                  "symbol '" + SB->getName() +
+                      "' can not be undefined in a subtraction expression");
       return false;
     }
 
@@ -402,11 +398,11 @@ bool X86MachObjectWriter::recordScatteredRelocation(MachObjectWriter *Writer,
     if (FixupOffset > 0xffffff) {
       char Buffer[32];
       format("0x%x", FixupOffset).print(Buffer, sizeof(Buffer));
-      Asm.getContext().reportError(Fixup.getLoc(),
-                         Twine("Section too large, can't encode "
-                                "r_address (") + Buffer +
-                         ") into 24 bits of scattered "
-                         "relocation entry.");
+      reportError(Fixup.getLoc(), Twine("Section too large, can't encode "
+                                        "r_address (") +
+                                      Buffer +
+                                      ") into 24 bits of scattered "
+                                      "relocation entry.");
       return false;
     }
 


        


More information about the llvm-commits mailing list