[llvm] [llvm-pdbutil] Fix register enum field dumping/parsing (PR #82299)

via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 19 19:08:06 PST 2024


https://github.com/nikitalita updated https://github.com/llvm/llvm-project/pull/82299

>From da9c2eb1018fa9051ff60d9fb484303fa549657b Mon Sep 17 00:00:00 2001
From: nikitalita <69168929+nikitalita at users.noreply.github.com>
Date: Mon, 19 Feb 2024 18:10:36 -0800
Subject: [PATCH 1/2] [llvm-pdbutil] Fix dumping register enums

---
 llvm/tools/llvm-pdbutil/YAMLOutputStyle.cpp | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/llvm/tools/llvm-pdbutil/YAMLOutputStyle.cpp b/llvm/tools/llvm-pdbutil/YAMLOutputStyle.cpp
index 80b76657facc7c..ecb4c2175e49a3 100644
--- a/llvm/tools/llvm-pdbutil/YAMLOutputStyle.cpp
+++ b/llvm/tools/llvm-pdbutil/YAMLOutputStyle.cpp
@@ -11,6 +11,7 @@
 #include "PdbYaml.h"
 #include "llvm-pdbutil.h"
 
+#include "llvm/BinaryFormat/COFF.h"
 #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
 #include "llvm/DebugInfo/CodeView/DebugSubsection.h"
 #include "llvm/DebugInfo/CodeView/DebugUnknownSubsection.h"
@@ -73,7 +74,15 @@ Error YAMLOutputStyle::dump() {
   if (auto EC = dumpPublics())
     return EC;
 
+  // Fake Coff header for dumping register enumerations.
+  COFF::header Header;
+  auto MachineType =
+      Obj.DbiStream ? Obj.DbiStream->MachineType : PDB_Machine::Unknown;
+  Header.Machine = static_cast<uint16_t>(MachineType);
+  Out.setContext(&Header);
   flush();
+  Out.setContext(nullptr);
+
   return Error::success();
 }
 

>From e4888a92402f53000a3a5e79d3792c034fc2f343 Mon Sep 17 00:00:00 2001
From: nikitalita <69168929+nikitalita at users.noreply.github.com>
Date: Thu, 2 Nov 2023 18:41:16 -0700
Subject: [PATCH 2/2] [llvm-pdbutil] Fix yaml2pdb failing to parse yaml with
 register enums

---
 llvm/tools/llvm-pdbutil/PdbYaml.cpp | 7 +++++++
 llvm/tools/llvm-pdbutil/PdbYaml.h   | 2 ++
 2 files changed, 9 insertions(+)

diff --git a/llvm/tools/llvm-pdbutil/PdbYaml.cpp b/llvm/tools/llvm-pdbutil/PdbYaml.cpp
index a26241967b5add..fac1d89321610b 100644
--- a/llvm/tools/llvm-pdbutil/PdbYaml.cpp
+++ b/llvm/tools/llvm-pdbutil/PdbYaml.cpp
@@ -155,6 +155,13 @@ void MappingTraits<PdbDbiStream>::mapping(IO &IO, PdbDbiStream &Obj) {
   IO.mapOptional("PdbDllRbld", Obj.PdbDllRbld, uint16_t(0U));
   IO.mapOptional("Flags", Obj.Flags, uint16_t(1U));
   IO.mapOptional("MachineType", Obj.MachineType, PDB_Machine::x86);
+  // This is a workaround for IO not having document context with the
+  // machine type. The machine type is needed to properly parse Register enums
+  // in the PDB.
+  if (!IO.getContext()) {
+    Obj.FakeHeader.Machine = static_cast<uint16_t>(Obj.MachineType);
+    IO.setContext(&Obj.FakeHeader);
+  }
   IO.mapOptional("Modules", Obj.ModInfos);
 }
 
diff --git a/llvm/tools/llvm-pdbutil/PdbYaml.h b/llvm/tools/llvm-pdbutil/PdbYaml.h
index 4382e91e209737..b0c0f6a00c499b 100644
--- a/llvm/tools/llvm-pdbutil/PdbYaml.h
+++ b/llvm/tools/llvm-pdbutil/PdbYaml.h
@@ -11,6 +11,7 @@
 
 #include "OutputStyle.h"
 
+#include "llvm/BinaryFormat/COFF.h"
 #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
 #include "llvm/DebugInfo/CodeView/TypeRecord.h"
 #include "llvm/DebugInfo/MSF/MSFCommon.h"
@@ -80,6 +81,7 @@ struct PdbDbiStream {
   PDB_Machine MachineType = PDB_Machine::x86;
 
   std::vector<PdbDbiModuleInfo> ModInfos;
+  COFF::header FakeHeader;
 };
 
 struct PdbTpiStream {



More information about the llvm-commits mailing list