Skip to content

Conversation

@redstar
Copy link
Member

@redstar redstar commented Mar 13, 2025

The GOFFWriter has 2 purposes:

  • Simplify resource management
  • Enable writing of split DWARF files

It follows the design of the other writer classes. No added functionality at this point.

@redstar redstar requested review from MaskRay and uweigand March 13, 2025 20:59
@redstar redstar self-assigned this Mar 13, 2025
@llvmbot llvmbot added the llvm:mc Machine (object) code label Mar 13, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 13, 2025

@llvm/pr-subscribers-mc

Author: Kai Nacke (redstar)

Changes

The GOFFWriter has 2 purposes:

  • Simplify resource management
  • Enable writing of split DWARF files

It follows the design of the other writer classes. No added functionality at this point.


Full diff: https://github.com/llvm/llvm-project/pull/131216.diff

1 Files Affected:

  • (modified) llvm/lib/MC/GOFFObjectWriter.cpp (+41-23)
diff --git a/llvm/lib/MC/GOFFObjectWriter.cpp b/llvm/lib/MC/GOFFObjectWriter.cpp
index 85deebd89d1f6..4ee8e1487751f 100644
--- a/llvm/lib/MC/GOFFObjectWriter.cpp
+++ b/llvm/lib/MC/GOFFObjectWriter.cpp
@@ -223,34 +223,23 @@ void GOFFOstream::finalizeRecord() {
 }
 
 namespace {
-
-class GOFFObjectWriter : public MCObjectWriter {
-  // The target specific GOFF writer instance.
-  std::unique_ptr<MCGOFFObjectTargetWriter> TargetObjectWriter;
-
-  // The stream used to write the GOFF records.
+class GOFFWriter {
   GOFFOstream OS;
+  [[maybe_unused]] MCAssembler &Asm;
 
-public:
-  GOFFObjectWriter(std::unique_ptr<MCGOFFObjectTargetWriter> MOTW,
-                   raw_pwrite_stream &OS)
-      : TargetObjectWriter(std::move(MOTW)), OS(OS) {}
-
-  ~GOFFObjectWriter() override {}
-
-  // Write GOFF records.
   void writeHeader();
   void writeEnd();
 
-  // Implementation of the MCObjectWriter interface.
-  void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
-                        const MCFixup &Fixup, MCValue Target,
-                        uint64_t &FixedValue) override {}
-  uint64_t writeObject(MCAssembler &Asm) override;
+public:
+  GOFFWriter(raw_pwrite_stream &OS, MCAssembler &Asm);
+  uint64_t writeObject();
 };
-} // end anonymous namespace
+} // namespace
+
+GOFFWriter::GOFFWriter(raw_pwrite_stream &OS, MCAssembler &Asm)
+    : OS(OS), Asm(Asm) {}
 
-void GOFFObjectWriter::writeHeader() {
+void GOFFWriter::writeHeader() {
   OS.newRecord(GOFF::RT_HDR);
   OS.write_zeros(1);       // Reserved
   OS.writebe<uint32_t>(0); // Target Hardware Environment
@@ -264,7 +253,7 @@ void GOFFObjectWriter::writeHeader() {
   OS.write_zeros(6);       // Reserved
 }
 
-void GOFFObjectWriter::writeEnd() {
+void GOFFWriter::writeEnd() {
   uint8_t F = GOFF::END_EPR_None;
   uint8_t AMODE = 0;
   uint32_t ESDID = 0;
@@ -282,7 +271,7 @@ void GOFFObjectWriter::writeEnd() {
   OS.writebe<uint32_t>(ESDID); // ESDID (of entry point)
 }
 
-uint64_t GOFFObjectWriter::writeObject(MCAssembler &Asm) {
+uint64_t GOFFWriter::writeObject() {
   writeHeader();
   writeEnd();
 
@@ -295,6 +284,35 @@ uint64_t GOFFObjectWriter::writeObject(MCAssembler &Asm) {
   return OS.getWrittenSize();
 }
 
+namespace {
+
+class GOFFObjectWriter : public MCObjectWriter {
+  // The target specific GOFF writer instance.
+  std::unique_ptr<MCGOFFObjectTargetWriter> TargetObjectWriter;
+
+  // The stream used to write the GOFF records.
+  raw_pwrite_stream &OS;
+
+public:
+  GOFFObjectWriter(std::unique_ptr<MCGOFFObjectTargetWriter> MOTW,
+                   raw_pwrite_stream &OS)
+      : TargetObjectWriter(std::move(MOTW)), OS(OS) {}
+
+  ~GOFFObjectWriter() override {}
+
+  // Implementation of the MCObjectWriter interface.
+  void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
+                        const MCFixup &Fixup, MCValue Target,
+                        uint64_t &FixedValue) override {}
+  uint64_t writeObject(MCAssembler &Asm) override;
+};
+} // end anonymous namespace
+
+uint64_t GOFFObjectWriter::writeObject(MCAssembler &Asm) {
+  uint64_t Size = GOFFWriter(OS, Asm).writeObject();
+  return Size;
+}
+
 std::unique_ptr<MCObjectWriter>
 llvm::createGOFFObjectWriter(std::unique_ptr<MCGOFFObjectTargetWriter> MOTW,
                              raw_pwrite_stream &OS) {


namespace {

class GOFFObjectWriter : public MCObjectWriter {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you will change AsmPrinter or a targetstreamer to be aware of GOFF-specifics, it would be better to expose the class to a header.

In 70c52b6 , I exported ELF and enabled a lot of simplification to MCAssembler/MCELFStreamer.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a good point. I think we can also make use of a public available GOFFWriter. Only downside is that it pulls a couple of internal definitions into the header file, but that is the same as in the ELF implementation. Thanks for pointing that out.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, with the 2nd try it should now be correct....

@redstar redstar force-pushed the users/redstar/goffwriter-2 branch from 824ce7a to 7810f30 Compare March 24, 2025 21:37
@redstar
Copy link
Member Author

redstar commented Mar 31, 2025

Is the change ok?

@redstar redstar requested a review from Everybody0523 April 2, 2025 22:51
@redstar
Copy link
Member Author

redstar commented Apr 10, 2025

More comments on this PR?

@redstar
Copy link
Member Author

redstar commented Apr 24, 2025

Gentle ping. :-)

@redstar
Copy link
Member Author

redstar commented Jun 16, 2025

Gentle ping :-)

@uweigand
Copy link
Member

Hi @redstar, looks like this no longer merges cleanly and has test failures now.

@redstar
Copy link
Member Author

redstar commented Jun 23, 2025

I'll rebase all the PRs...

The GOFFWriter has 2 purposes:
- Simplify resource management
- Enable writing of split DWARF files

It follows the design of the other writer classes. No added functionality at this point.

This changes also makes the GOFFObjectWriter a public class.
@redstar redstar force-pushed the users/redstar/goffwriter-2 branch from 7810f30 to 7e6a2e2 Compare June 25, 2025 15:54
@redstar
Copy link
Member Author

redstar commented Jun 25, 2025

Hopefully this gives a clean build. Ther merge conflict & build failures were caused by the removal of the Asm parameter in recordRelocation() and writeObject().

Copy link
Member

@uweigand uweigand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This refactoring brings GOFF in line with other formats. LGTM.

@redstar redstar merged commit 35a0c18 into main Jun 26, 2025
7 checks passed
@redstar redstar deleted the users/redstar/goffwriter-2 branch June 26, 2025 12:24
anthonyhatran pushed a commit to anthonyhatran/llvm-project that referenced this pull request Jun 26, 2025
The GOFFWriter has 2 purposes:
- Simplify resource management
- Enable writing of split DWARF files

It follows the design of the other writer classes. No added
functionality at this point.

This changes also makes the GOFFObjectWriter a public class.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:mc Machine (object) code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants