[llvm] 2849b12 - MCObjectwriter: Add getContext and simplify code

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sat May 24 09:26:35 PDT 2025


Author: Fangrui Song
Date: 2025-05-24T09:26:30-07:00
New Revision: 2849b1282e76062f2237f37714ce770e8e60b9c1

URL: https://github.com/llvm/llvm-project/commit/2849b1282e76062f2237f37714ce770e8e60b9c1
DIFF: https://github.com/llvm/llvm-project/commit/2849b1282e76062f2237f37714ce770e8e60b9c1.diff

LOG: MCObjectwriter: Add getContext and simplify code

Added: 
    

Modified: 
    llvm/include/llvm/MC/MCObjectWriter.h
    llvm/lib/MC/ELFObjectWriter.cpp
    llvm/lib/MC/MCDXContainerWriter.cpp
    llvm/lib/MC/MCObjectWriter.cpp
    llvm/lib/MC/MachObjectWriter.cpp
    llvm/lib/MC/WasmObjectWriter.cpp
    llvm/lib/MC/WinCOFFObjectWriter.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/MC/MCObjectWriter.h b/llvm/include/llvm/MC/MCObjectWriter.h
index b821ab214926c..ef79262700163 100644
--- a/llvm/include/llvm/MC/MCObjectWriter.h
+++ b/llvm/include/llvm/MC/MCObjectWriter.h
@@ -57,6 +57,8 @@ class MCObjectWriter {
 
   void setAssembler(MCAssembler *A) { Asm = A; }
 
+  MCContext &getContext() const;
+
   /// lifetime management
   virtual void reset();
 

diff  --git a/llvm/lib/MC/ELFObjectWriter.cpp b/llvm/lib/MC/ELFObjectWriter.cpp
index acc1bfbd1ae67..986bc6e098392 100644
--- a/llvm/lib/MC/ELFObjectWriter.cpp
+++ b/llvm/lib/MC/ELFObjectWriter.cpp
@@ -110,6 +110,7 @@ class SymbolTableWriter {
 };
 
 struct ELFWriter {
+  MCAssembler &Asm;
   ELFObjectWriter &OWriter;
   support::endian::Writer W;
 
@@ -160,12 +161,15 @@ struct ELFWriter {
                              Align Alignment);
 
 public:
-  ELFWriter(ELFObjectWriter &OWriter, raw_pwrite_stream &OS,
+  ELFWriter(MCAssembler &Asm, ELFObjectWriter &OWriter, raw_pwrite_stream &OS,
             bool IsLittleEndian, DwoMode Mode)
-      : OWriter(OWriter), W(OS, IsLittleEndian ? llvm::endianness::little
-                                               : llvm::endianness::big),
+      : Asm(Asm), OWriter(OWriter),
+        W(OS,
+          IsLittleEndian ? llvm::endianness::little : llvm::endianness::big),
         Mode(Mode) {}
 
+  MCContext &getContext() const { return Asm.getContext(); }
+
   void writeWord(uint64_t Word) {
     if (is64Bit())
       W.write<uint64_t>(Word);
@@ -205,7 +209,7 @@ struct ELFWriter {
                                uint32_t Link, uint32_t Info,
                                MaybeAlign Alignment, uint64_t EntrySize);
 
-  void writeRelocations(const MCAssembler &Asm, const MCSectionELF &Sec);
+  void writeRelocations(const MCSectionELF &Sec);
 
   uint64_t writeObject(MCAssembler &Asm);
   void writeSectionHeader(uint32_t GroupSymbolIndex, uint64_t Offset,
@@ -818,10 +822,9 @@ static void encodeCrel(ArrayRef<ELFRelocationEntry> Relocs, raw_ostream &OS) {
   });
 }
 
-void ELFWriter::writeRelocations(const MCAssembler &Asm,
-                                       const MCSectionELF &Sec) {
+void ELFWriter::writeRelocations(const MCSectionELF &Sec) {
   std::vector<ELFRelocationEntry> &Relocs = OWriter.Relocations[&Sec];
-  const MCTargetOptions *TO = Asm.getContext().getTargetOptions();
+  const MCTargetOptions *TO = getContext().getTargetOptions();
   const bool Rela = OWriter.usesRela(TO, Sec);
 
   // Sort the relocation entries. MIPS needs this.
@@ -1017,7 +1020,7 @@ void ELFWriter::writeSectionHeaders(const MCAssembler &Asm) {
 uint64_t ELFWriter::writeObject(MCAssembler &Asm) {
   uint64_t StartOffset = W.OS.tell();
 
-  MCContext &Ctx = Asm.getContext();
+  MCContext &Ctx = getContext();
   MCSectionELF *StrtabSection =
       Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0);
   StringTableIndex = addToSectionTable(StrtabSection);
@@ -1111,8 +1114,7 @@ uint64_t ELFWriter::writeObject(MCAssembler &Asm) {
       // Remember the offset into the file for this section.
       const uint64_t SecStart = align(RelSection->getAlign());
 
-      writeRelocations(Asm,
-                       cast<MCSectionELF>(*RelSection->getLinkedToSection()));
+      writeRelocations(cast<MCSectionELF>(*RelSection->getLinkedToSection()));
 
       uint64_t SecEnd = W.OS.tell();
       RelSection->setOffsets(SecStart, SecEnd);
@@ -1326,7 +1328,7 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
                                        uint64_t &FixedValue) {
   MCAsmBackend &Backend = Asm.getBackend();
   const MCSectionELF &FixupSection = cast<MCSectionELF>(*Fragment->getParent());
-  MCContext &Ctx = Asm.getContext();
+  MCContext &Ctx = getContext();
 
   const auto *SymA = cast_or_null<MCSymbolELF>(Target.getAddSym());
   bool ViaWeakRef = false;
@@ -1429,11 +1431,11 @@ bool ELFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
 
 uint64_t ELFObjectWriter::writeObject(MCAssembler &Asm) {
   uint64_t Size =
-      ELFWriter(*this, OS, IsLittleEndian,
+      ELFWriter(Asm, *this, OS, IsLittleEndian,
                 DwoOS ? ELFWriter::NonDwoOnly : ELFWriter::AllSections)
           .writeObject(Asm);
   if (DwoOS)
-    Size += ELFWriter(*this, *DwoOS, IsLittleEndian, ELFWriter::DwoOnly)
+    Size += ELFWriter(Asm, *this, *DwoOS, IsLittleEndian, ELFWriter::DwoOnly)
                 .writeObject(Asm);
   return Size;
 }

diff  --git a/llvm/lib/MC/MCDXContainerWriter.cpp b/llvm/lib/MC/MCDXContainerWriter.cpp
index ae86c435a041b..2e6379b5feb0d 100644
--- a/llvm/lib/MC/MCDXContainerWriter.cpp
+++ b/llvm/lib/MC/MCDXContainerWriter.cpp
@@ -86,7 +86,7 @@ uint64_t DXContainerObjectWriter::writeObject(MCAssembler &Asm) {
       dxbc::ProgramHeader Header;
       memset(reinterpret_cast<void *>(&Header), 0, sizeof(dxbc::ProgramHeader));
 
-      const Triple &TT = Asm.getContext().getTargetTriple();
+      const Triple &TT = getContext().getTargetTriple();
       VersionTuple Version = TT.getOSVersion();
       uint8_t MajorVersion = static_cast<uint8_t>(Version.getMajor());
       uint8_t MinorVersion =

diff  --git a/llvm/lib/MC/MCObjectWriter.cpp b/llvm/lib/MC/MCObjectWriter.cpp
index 5f1f2472c417c..47bb6b1c01a0a 100644
--- a/llvm/lib/MC/MCObjectWriter.cpp
+++ b/llvm/lib/MC/MCObjectWriter.cpp
@@ -19,6 +19,8 @@ using namespace llvm;
 
 MCObjectWriter::~MCObjectWriter() = default;
 
+MCContext &MCObjectWriter::getContext() const { return Asm->getContext(); }
+
 void MCObjectWriter::reset() {
   FileNames.clear();
   AddrsigSyms.clear();

diff  --git a/llvm/lib/MC/MachObjectWriter.cpp b/llvm/lib/MC/MachObjectWriter.cpp
index 0de16b515c4bd..86eb92cec6f2c 100644
--- a/llvm/lib/MC/MachObjectWriter.cpp
+++ b/llvm/lib/MC/MachObjectWriter.cpp
@@ -516,8 +516,8 @@ void MachObjectWriter::recordRelocation(MCAssembler &Asm,
                                         const MCFixup &Fixup, MCValue Target,
                                         uint64_t &FixedValue) {
   if (!isFixupTargetValid(Target)) {
-    Asm.getContext().reportError(Fixup.getLoc(),
-                                 "unsupported relocation expression");
+    getContext().reportError(Fixup.getLoc(),
+                             "unsupported relocation expression");
     return;
   }
 
@@ -778,7 +778,7 @@ static MachO::LoadCommandType getLCFromMCVM(MCVersionMinType Type) {
 
 void MachObjectWriter::populateAddrSigSection(MCAssembler &Asm) {
   MCSection *AddrSigSection =
-      Asm.getContext().getObjectFileInfo()->getAddrSigSection();
+      getContext().getObjectFileInfo()->getAddrSigSection();
   unsigned Log2Size = is64Bit() ? 3 : 2;
   for (const MCSymbol *S : getAddrsigSyms()) {
     if (!S->isRegistered())
@@ -801,7 +801,7 @@ uint64_t MachObjectWriter::writeObject(MCAssembler &Asm) {
                      UndefinedSymbolData);
 
   if (!CGProfile.empty()) {
-    MCSection *CGProfileSection = Asm.getContext().getMachOSection(
+    MCSection *CGProfileSection = getContext().getMachOSection(
         "__LLVM", "__cg_profile", 0, SectionKind::getMetadata());
     auto &Frag = cast<MCDataFragment>(*CGProfileSection->begin());
     Frag.getContents().clear();
@@ -920,12 +920,12 @@ uint64_t MachObjectWriter::writeObject(MCAssembler &Asm) {
       Flags |= MachO::S_ATTR_SOME_INSTRUCTIONS;
     if (!cast<MCSectionMachO>(Sec).isVirtualSection() &&
         !isUInt<32>(SectionStart)) {
-      Asm.getContext().reportError(
+      getContext().reportError(
           SMLoc(), "cannot encode offset of section; object file too large");
       return NumBytesWritten();
     }
     if (NumRelocs && !isUInt<32>(RelocTableEnd)) {
-      Asm.getContext().reportError(
+      getContext().reportError(
           SMLoc(),
           "cannot encode offset of relocations; object file too large");
       return NumBytesWritten();

diff  --git a/llvm/lib/MC/WasmObjectWriter.cpp b/llvm/lib/MC/WasmObjectWriter.cpp
index 6f51a70175775..f6175414ef6f9 100644
--- a/llvm/lib/MC/WasmObjectWriter.cpp
+++ b/llvm/lib/MC/WasmObjectWriter.cpp
@@ -487,7 +487,7 @@ void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
   const auto &FixupSection = cast<MCSectionWasm>(*Fragment->getParent());
   uint64_t C = Target.getConstant();
   uint64_t FixupOffset = Asm.getFragmentOffset(*Fragment) + Fixup.getOffset();
-  MCContext &Ctx = Asm.getContext();
+  MCContext &Ctx = getContext();
   bool IsLocRel = false;
 
   if (const auto *RefB = Target.getSubSym()) {
@@ -1929,7 +1929,7 @@ uint64_t WasmObjectWriter::writeOneObject(MCAssembler &Asm,
     writeGlobalSection(Globals);
     writeExportSection(Exports);
     const MCSymbol *IndirectFunctionTable =
-        Asm.getContext().lookupSymbol("__indirect_function_table");
+        getContext().lookupSymbol("__indirect_function_table");
     writeElemSection(cast_or_null<const MCSymbolWasm>(IndirectFunctionTable),
                      TableElems);
     writeDataCountSection();

diff  --git a/llvm/lib/MC/WinCOFFObjectWriter.cpp b/llvm/lib/MC/WinCOFFObjectWriter.cpp
index 599c1d81ec6ba..b3a8cf0d936f2 100644
--- a/llvm/lib/MC/WinCOFFObjectWriter.cpp
+++ b/llvm/lib/MC/WinCOFFObjectWriter.cpp
@@ -166,6 +166,7 @@ class llvm::WinCOFFWriter {
   int getSectionNumber(const MCSection &Section) const;
 
 private:
+  MCContext &getContext() const { return OWriter.getContext(); }
   COFFSymbol *createSymbol(StringRef Name);
   COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol *Symbol);
   COFFSection *createSection(StringRef Name);
@@ -841,15 +842,14 @@ void WinCOFFWriter::recordRelocation(MCAssembler &Asm,
 
   const MCSymbol &A = *Target.getAddSym();
   if (!A.isRegistered()) {
-    Asm.getContext().reportError(Fixup.getLoc(), Twine("symbol '") +
-                                                     A.getName() +
-                                                     "' can not be undefined");
+    getContext().reportError(Fixup.getLoc(), Twine("symbol '") + A.getName() +
+                                                 "' can not be undefined");
     return;
   }
   if (A.isTemporary() && A.isUndefined()) {
-    Asm.getContext().reportError(Fixup.getLoc(), Twine("assembler label '") +
-                                                     A.getName() +
-                                                     "' can not be undefined");
+    getContext().reportError(Fixup.getLoc(), Twine("assembler label '") +
+                                                 A.getName() +
+                                                 "' can not be undefined");
     return;
   }
 
@@ -862,7 +862,7 @@ void WinCOFFWriter::recordRelocation(MCAssembler &Asm,
   COFFSection *Sec = SectionMap[MCSec];
   if (const MCSymbol *B = Target.getSubSym()) {
     if (!B->getFragment()) {
-      Asm.getContext().reportError(
+      getContext().reportError(
           Fixup.getLoc(),
           Twine("symbol '") + B->getName() +
               "' can not be undefined in a subtraction expression");
@@ -920,7 +920,7 @@ void WinCOFFWriter::recordRelocation(MCAssembler &Asm,
 
   Reloc.Data.VirtualAddress += Fixup.getOffset();
   Reloc.Data.Type = OWriter.TargetObjectWriter->getRelocType(
-      Asm.getContext(), Target, Fixup, Target.getSubSym(), Asm.getBackend());
+      getContext(), Target, Fixup, Target.getSubSym(), Asm.getBackend());
 
   // The *_REL32 relocations are relative to the end of the relocation,
   // not to the start.
@@ -1053,7 +1053,7 @@ uint64_t WinCOFFWriter::writeObject(MCAssembler &Asm) {
     // It's an error to try to associate with an undefined symbol or a symbol
     // without a section.
     if (!AssocMCSym->isInSection()) {
-      Asm.getContext().reportError(
+      getContext().reportError(
           SMLoc(), Twine("cannot make section ") + MCSec.getName() +
                        Twine(" associative with sectionless symbol ") +
                        AssocMCSym->getName());
@@ -1073,8 +1073,8 @@ uint64_t WinCOFFWriter::writeObject(MCAssembler &Asm) {
 
   // Create the contents of the .llvm_addrsig section.
   if (Mode != DwoOnly && OWriter.getEmitAddrsigSection()) {
-    auto *Sec = Asm.getContext().getCOFFSection(
-        ".llvm_addrsig", COFF::IMAGE_SCN_LNK_REMOVE);
+    auto *Sec = getContext().getCOFFSection(".llvm_addrsig",
+                                            COFF::IMAGE_SCN_LNK_REMOVE);
     auto *Frag = cast<MCDataFragment>(Sec->curFragList()->Head);
     raw_svector_ostream OS(Frag->getContents());
     for (const MCSymbol *S : OWriter.AddrsigSyms) {
@@ -1095,8 +1095,8 @@ uint64_t WinCOFFWriter::writeObject(MCAssembler &Asm) {
 
   // Create the contents of the .llvm.call-graph-profile section.
   if (Mode != DwoOnly && !OWriter.getCGProfile().empty()) {
-    auto *Sec = Asm.getContext().getCOFFSection(
-        ".llvm.call-graph-profile", COFF::IMAGE_SCN_LNK_REMOVE);
+    auto *Sec = getContext().getCOFFSection(".llvm.call-graph-profile",
+                                            COFF::IMAGE_SCN_LNK_REMOVE);
     auto *Frag = cast<MCDataFragment>(Sec->curFragList()->Head);
     raw_svector_ostream OS(Frag->getContents());
     for (const auto &CGPE : OWriter.getCGProfile()) {
@@ -1205,7 +1205,7 @@ void WinCOFFObjectWriter::recordRelocation(MCAssembler &Asm,
 uint64_t WinCOFFObjectWriter::writeObject(MCAssembler &Asm) {
   // If the assember had an error, then layout will not have completed, so we
   // cannot write an object file.
-  if (Asm.getContext().hadError())
+  if (getContext().hadError())
     return 0;
 
   uint64_t TotalSize = ObjWriter->writeObject(Asm);


        


More information about the llvm-commits mailing list