[llvm] [llvm-pdbutil] Create DBI section headers in yaml2pdb (PR #166566)

via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 5 06:13:12 PST 2025


https://github.com/Nerixyz created https://github.com/llvm/llvm-project/pull/166566

The section headers present in the DBI stream got lost when using `pdb2yaml` and `yaml2pdb`.

They are a list of COFF section headers. The `llvm::object::coff_section` didn't have a YAML mapping, so I added one in llvm-pdbutil. The mapping for COFF sections in ObjectYAML includes the section data itself, so we can't use it here.

Creation of the section map and headers in yaml2pdb is done like in LLD: https://github.com/llvm/llvm-project/blob/438a18c1e105ca04e624239644195e48b28b5099/lld/COFF/PDB.cpp#L1695-L1703

>From b59a701136afa850aa3a3ed485ec102cda9e13ee Mon Sep 17 00:00:00 2001
From: Nerixyz <nerixdev at outlook.de>
Date: Wed, 5 Nov 2025 15:03:50 +0100
Subject: [PATCH] [llvm-pdbutil] Create DBI section headers in yaml2pdb

---
 .../llvm-pdbutil/dbi-section-headers.test     | 131 ++++++++++++++++++
 llvm/tools/llvm-pdbutil/PdbYaml.cpp           |  45 ++++++
 llvm/tools/llvm-pdbutil/PdbYaml.h             |  21 +++
 llvm/tools/llvm-pdbutil/YAMLOutputStyle.cpp   |  14 ++
 llvm/tools/llvm-pdbutil/llvm-pdbutil.cpp      |  13 ++
 5 files changed, 224 insertions(+)
 create mode 100644 llvm/test/tools/llvm-pdbutil/dbi-section-headers.test

diff --git a/llvm/test/tools/llvm-pdbutil/dbi-section-headers.test b/llvm/test/tools/llvm-pdbutil/dbi-section-headers.test
new file mode 100644
index 0000000000000..bced13cc25199
--- /dev/null
+++ b/llvm/test/tools/llvm-pdbutil/dbi-section-headers.test
@@ -0,0 +1,131 @@
+# RUN: llvm-pdbutil yaml2pdb %s --pdb=%t.pdb
+# RUN: llvm-pdbutil dump --section-headers --section-map %t.pdb | FileCheck --check-prefix=CHECK-YAML2PDB %s
+
+# RUN: llvm-pdbutil pdb2yaml --dbi-stream %t.pdb > %t.yaml
+# RUN: FileCheck --input-file=%t.yaml --check-prefix=CHECK-PDB2YAML %s
+
+# CHECK-YAML2PDB:                      Section Headers
+# CHECK-YAML2PDB:  SECTION HEADER #1
+# CHECK-YAML2PDB:     .text name
+# CHECK-YAML2PDB:        47 virtual size
+# CHECK-YAML2PDB:      1000 virtual address
+# CHECK-YAML2PDB:       200 size of raw data
+# CHECK-YAML2PDB:       400 file pointer to raw data
+# CHECK-YAML2PDB:         0 file pointer to relocation table
+# CHECK-YAML2PDB:         0 file pointer to line numbers
+# CHECK-YAML2PDB:         0 number of relocations
+# CHECK-YAML2PDB:         0 number of line numbers
+# CHECK-YAML2PDB:  60000020 flags
+# CHECK-YAML2PDB:           IMAGE_SCN_CNT_CODE
+# CHECK-YAML2PDB:           IMAGE_SCN_MEM_EXECUTE
+# CHECK-YAML2PDB:           IMAGE_SCN_MEM_READ
+
+# CHECK-YAML2PDB:  SECTION HEADER #2
+# CHECK-YAML2PDB:    .rdata name
+# CHECK-YAML2PDB:        64 virtual size
+# CHECK-YAML2PDB:      2000 virtual address
+# CHECK-YAML2PDB:       200 size of raw data
+# CHECK-YAML2PDB:       600 file pointer to raw data
+# CHECK-YAML2PDB:         0 file pointer to relocation table
+# CHECK-YAML2PDB:         0 file pointer to line numbers
+# CHECK-YAML2PDB:         0 number of relocations
+# CHECK-YAML2PDB:         0 number of line numbers
+# CHECK-YAML2PDB:  40000040 flags
+# CHECK-YAML2PDB:           IMAGE_SCN_CNT_INITIALIZED_DATA
+# CHECK-YAML2PDB:           IMAGE_SCN_MEM_READ
+
+# CHECK-YAML2PDB:  SECTION HEADER #3
+# CHECK-YAML2PDB:    .pdata name
+# CHECK-YAML2PDB:        18 virtual size
+# CHECK-YAML2PDB:      3000 virtual address
+# CHECK-YAML2PDB:       200 size of raw data
+# CHECK-YAML2PDB:       800 file pointer to raw data
+# CHECK-YAML2PDB:         0 file pointer to relocation table
+# CHECK-YAML2PDB:         0 file pointer to line numbers
+# CHECK-YAML2PDB:         0 number of relocations
+# CHECK-YAML2PDB:         0 number of line numbers
+# CHECK-YAML2PDB:  40000040 flags
+# CHECK-YAML2PDB:           IMAGE_SCN_CNT_INITIALIZED_DATA
+# CHECK-YAML2PDB:           IMAGE_SCN_MEM_READ
+
+# CHECK-YAML2PDB:                        Section Map
+# CHECK-YAML2PDB:  Section 0000 | ovl = 0, group = 0, frame = 1, name = 65535
+# CHECK-YAML2PDB:                 class = 65535, offset = 0, size = 71
+# CHECK-YAML2PDB:                 flags = read | execute | 32 bit addr | selector
+# CHECK-YAML2PDB:  Section 0001 | ovl = 0, group = 0, frame = 2, name = 65535
+# CHECK-YAML2PDB:                 class = 65535, offset = 0, size = 100
+# CHECK-YAML2PDB:                 flags = read | 32 bit addr | selector
+# CHECK-YAML2PDB:  Section 0002 | ovl = 0, group = 0, frame = 3, name = 65535
+# CHECK-YAML2PDB:                 class = 65535, offset = 0, size = 24
+# CHECK-YAML2PDB:                 flags = read | 32 bit addr | selector
+# CHECK-YAML2PDB:  Section 0003 | ovl = 0, group = 0, frame = 4, name = 65535
+# CHECK-YAML2PDB:                 class = 65535, offset = 0, size = 4294967295
+# CHECK-YAML2PDB:                 flags = 32 bit addr | absolute addr
+
+# CHECK-PDB2YAML:DbiStream:
+# CHECK-PDB2YAML:  SectionHeaders:
+# CHECK-PDB2YAML:    - Name:            .text
+# CHECK-PDB2YAML:      VirtualSize:     71
+# CHECK-PDB2YAML:      VirtualAddress:  4096
+# CHECK-PDB2YAML:      SizeOfRawData:   512
+# CHECK-PDB2YAML:      PointerToRawData: 1024
+# CHECK-PDB2YAML:      PointerToRelocations: 0
+# CHECK-PDB2YAML:      PointerToLinenumbers: 0
+# CHECK-PDB2YAML:      NumberOfRelocations: 0
+# CHECK-PDB2YAML:      NumberOfLinenumbers: 0
+# CHECK-PDB2YAML:      Characteristics: 1610612768
+# CHECK-PDB2YAML:    - Name:            .rdata
+# CHECK-PDB2YAML:      VirtualSize:     100
+# CHECK-PDB2YAML:      VirtualAddress:  8192
+# CHECK-PDB2YAML:      SizeOfRawData:   512
+# CHECK-PDB2YAML:      PointerToRawData: 1536
+# CHECK-PDB2YAML:      PointerToRelocations: 0
+# CHECK-PDB2YAML:      PointerToLinenumbers: 0
+# CHECK-PDB2YAML:      NumberOfRelocations: 0
+# CHECK-PDB2YAML:      NumberOfLinenumbers: 0
+# CHECK-PDB2YAML:      Characteristics: 1073741888
+# CHECK-PDB2YAML:    - Name:            .pdata
+# CHECK-PDB2YAML:      VirtualSize:     24
+# CHECK-PDB2YAML:      VirtualAddress:  12288
+# CHECK-PDB2YAML:      SizeOfRawData:   512
+# CHECK-PDB2YAML:      PointerToRawData: 2048
+# CHECK-PDB2YAML:      PointerToRelocations: 0
+# CHECK-PDB2YAML:      PointerToLinenumbers: 0
+# CHECK-PDB2YAML:      NumberOfRelocations: 0
+# CHECK-PDB2YAML:      NumberOfLinenumbers: 0
+# CHECK-PDB2YAML:      Characteristics: 1073741888
+
+---
+DbiStream:
+  SectionHeaders:
+    - Name:            .text
+      VirtualSize:     71
+      VirtualAddress:  4096
+      SizeOfRawData:   512
+      PointerToRawData: 1024
+      PointerToRelocations: 0
+      PointerToLinenumbers: 0
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      Characteristics: 1610612768
+    - Name:            .rdata
+      VirtualSize:     100
+      VirtualAddress:  8192
+      SizeOfRawData:   512
+      PointerToRawData: 1536
+      PointerToRelocations: 0
+      PointerToLinenumbers: 0
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      Characteristics: 1073741888
+    - Name:            .pdata
+      VirtualSize:     24
+      VirtualAddress:  12288
+      SizeOfRawData:   512
+      PointerToRawData: 2048
+      PointerToRelocations: 0
+      PointerToLinenumbers: 0
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      Characteristics: 1073741888
+...
diff --git a/llvm/tools/llvm-pdbutil/PdbYaml.cpp b/llvm/tools/llvm-pdbutil/PdbYaml.cpp
index fac1d89321610..030d8cead24ff 100644
--- a/llvm/tools/llvm-pdbutil/PdbYaml.cpp
+++ b/llvm/tools/llvm-pdbutil/PdbYaml.cpp
@@ -24,6 +24,7 @@ using namespace llvm::yaml;
 
 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::NamedStreamMapping)
 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::PdbDbiModuleInfo)
+LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::CoffSectionHeader)
 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::StreamBlockList)
 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::pdb::PdbRaw_FeatureSig)
 
@@ -135,6 +136,49 @@ void MappingTraits<msf::SuperBlock>::mapping(IO &IO, msf::SuperBlock &SB) {
   IO.mapOptional("BlockMapAddr", SB.BlockMapAddr, u32(0U));
 }
 
+CoffSectionHeader::CoffSectionHeader() = default;
+
+CoffSectionHeader::CoffSectionHeader(const object::coff_section &Section)
+    : Name(Section.Name), VirtualSize(Section.VirtualSize),
+      VirtualAddress(Section.VirtualAddress),
+      SizeOfRawData(Section.SizeOfRawData),
+      PointerToRawData(Section.PointerToRawData),
+      PointerToRelocations(Section.PointerToRelocations),
+      PointerToLinenumbers(Section.PointerToLinenumbers),
+      NumberOfRelocations(Section.NumberOfRelocations),
+      NumberOfLinenumbers(Section.NumberOfLinenumbers),
+      Characteristics(Section.Characteristics) {}
+
+object::coff_section CoffSectionHeader::toCoffSection() const {
+  object::coff_section Sec;
+  std::memset(Sec.Name, 0, COFF::NameSize);
+  std::memcpy(Sec.Name, Name.data(),
+              std::min(static_cast<size_t>(COFF::NameSize), Name.size()));
+  Sec.VirtualSize = VirtualSize;
+  Sec.VirtualAddress = VirtualAddress;
+  Sec.SizeOfRawData = SizeOfRawData;
+  Sec.PointerToRawData = PointerToRawData;
+  Sec.PointerToRelocations = PointerToRelocations;
+  Sec.PointerToLinenumbers = PointerToLinenumbers;
+  Sec.NumberOfRelocations = NumberOfRelocations;
+  Sec.NumberOfLinenumbers = NumberOfLinenumbers;
+  Sec.Characteristics = Characteristics;
+  return Sec;
+}
+
+void MappingTraits<CoffSectionHeader>::mapping(IO &IO, CoffSectionHeader &Obj) {
+  IO.mapRequired("Name", Obj.Name);
+  IO.mapOptional("VirtualSize", Obj.VirtualSize);
+  IO.mapOptional("VirtualAddress", Obj.VirtualAddress);
+  IO.mapOptional("SizeOfRawData", Obj.SizeOfRawData);
+  IO.mapOptional("PointerToRawData", Obj.PointerToRawData);
+  IO.mapOptional("PointerToRelocations", Obj.PointerToRelocations);
+  IO.mapOptional("PointerToLinenumbers", Obj.PointerToLinenumbers);
+  IO.mapOptional("NumberOfRelocations", Obj.NumberOfRelocations);
+  IO.mapOptional("NumberOfLinenumbers", Obj.NumberOfLinenumbers);
+  IO.mapOptional("Characteristics", Obj.Characteristics);
+}
+
 void MappingTraits<StreamBlockList>::mapping(IO &IO, StreamBlockList &SB) {
   IO.mapRequired("Stream", SB.Blocks);
 }
@@ -163,6 +207,7 @@ void MappingTraits<PdbDbiStream>::mapping(IO &IO, PdbDbiStream &Obj) {
     IO.setContext(&Obj.FakeHeader);
   }
   IO.mapOptional("Modules", Obj.ModInfos);
+  IO.mapOptional("SectionHeaders", Obj.SectionHeaders);
 }
 
 void MappingTraits<PdbTpiStream>::mapping(IO &IO,
diff --git a/llvm/tools/llvm-pdbutil/PdbYaml.h b/llvm/tools/llvm-pdbutil/PdbYaml.h
index d5111a9e8d3a3..b539428190998 100644
--- a/llvm/tools/llvm-pdbutil/PdbYaml.h
+++ b/llvm/tools/llvm-pdbutil/PdbYaml.h
@@ -18,6 +18,7 @@
 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
 #include "llvm/DebugInfo/PDB/Native/RawConstants.h"
 #include "llvm/DebugInfo/PDB/PDBTypes.h"
+#include "llvm/Object/COFF.h"
 #include "llvm/ObjectYAML/CodeViewYAMLDebugSections.h"
 #include "llvm/ObjectYAML/CodeViewYAMLSymbols.h"
 #include "llvm/ObjectYAML/CodeViewYAMLTypes.h"
@@ -40,6 +41,24 @@ struct MSFHeaders {
   uint64_t FileSize = 0;
 };
 
+struct CoffSectionHeader {
+  CoffSectionHeader();
+  CoffSectionHeader(const object::coff_section &Section);
+
+  object::coff_section toCoffSection() const;
+
+  StringRef Name;
+  uint32_t VirtualSize = 0;
+  uint32_t VirtualAddress = 0;
+  uint32_t SizeOfRawData = 0;
+  uint32_t PointerToRawData = 0;
+  uint32_t PointerToRelocations = 0;
+  uint32_t PointerToLinenumbers = 0;
+  uint16_t NumberOfRelocations = 0;
+  uint16_t NumberOfLinenumbers = 0;
+  uint32_t Characteristics = 0;
+};
+
 struct StreamBlockList {
   std::vector<uint32_t> Blocks;
 };
@@ -82,6 +101,7 @@ struct PdbDbiStream {
 
   std::vector<PdbDbiModuleInfo> ModInfos;
   COFF::header FakeHeader;
+  std::vector<CoffSectionHeader> SectionHeaders;
 };
 
 struct PdbTpiStream {
@@ -115,6 +135,7 @@ struct PdbObject {
 
 LLVM_YAML_DECLARE_MAPPING_TRAITS_PRIVATE(pdb::yaml::PdbObject)
 LLVM_YAML_DECLARE_MAPPING_TRAITS_PRIVATE(pdb::yaml::MSFHeaders)
+LLVM_YAML_DECLARE_MAPPING_TRAITS_PRIVATE(pdb::yaml::CoffSectionHeader)
 LLVM_YAML_DECLARE_MAPPING_TRAITS_PRIVATE(msf::SuperBlock)
 LLVM_YAML_DECLARE_MAPPING_TRAITS_PRIVATE(pdb::yaml::StreamBlockList)
 LLVM_YAML_DECLARE_MAPPING_TRAITS_PRIVATE(pdb::yaml::PdbInfoStream)
diff --git a/llvm/tools/llvm-pdbutil/YAMLOutputStyle.cpp b/llvm/tools/llvm-pdbutil/YAMLOutputStyle.cpp
index ecb4c2175e49a..24d182134bd6c 100644
--- a/llvm/tools/llvm-pdbutil/YAMLOutputStyle.cpp
+++ b/llvm/tools/llvm-pdbutil/YAMLOutputStyle.cpp
@@ -285,6 +285,20 @@ Error YAMLOutputStyle::dumpDbiStream() {
       }
     }
   }
+  for (const auto &Section : DS.getSectionHeaders()) {
+    yaml::CoffSectionHeader Hdr;
+    Hdr.Name = Section.Name;
+    Hdr.VirtualSize = Section.VirtualSize;
+    Hdr.VirtualAddress = Section.VirtualAddress;
+    Hdr.SizeOfRawData = Section.SizeOfRawData;
+    Hdr.PointerToRawData = Section.PointerToRawData;
+    Hdr.PointerToRelocations = Section.PointerToRelocations;
+    Hdr.PointerToLinenumbers = Section.PointerToLinenumbers;
+    Hdr.NumberOfRelocations = Section.NumberOfRelocations;
+    Hdr.NumberOfLinenumbers = Section.NumberOfLinenumbers;
+    Hdr.Characteristics = Section.Characteristics;
+    Obj.DbiStream->SectionHeaders.emplace_back(Hdr);
+  }
   return Error::success();
 }
 
diff --git a/llvm/tools/llvm-pdbutil/llvm-pdbutil.cpp b/llvm/tools/llvm-pdbutil/llvm-pdbutil.cpp
index a5b8e8b7941d7..20a9bc839f343 100644
--- a/llvm/tools/llvm-pdbutil/llvm-pdbutil.cpp
+++ b/llvm/tools/llvm-pdbutil/llvm-pdbutil.cpp
@@ -865,6 +865,19 @@ static void yamlToPdb(StringRef Path) {
     }
   }
 
+  std::vector<object::coff_section> Sections;
+  if (!Dbi.SectionHeaders.empty()) {
+    for (const auto &Hdr : Dbi.SectionHeaders) {
+      Sections.emplace_back(Hdr.toCoffSection());
+    }
+
+    DbiBuilder.createSectionMap(Sections);
+    ExitOnErr(DbiBuilder.addDbgStream(
+        pdb::DbgHeaderType::SectionHdr,
+        ArrayRef<uint8_t>{(const uint8_t *)Sections.data(),
+                          Sections.size() * sizeof(object::coff_section)}));
+  }
+
   auto &TpiBuilder = Builder.getTpiBuilder();
   const auto &Tpi = YamlObj.TpiStream.value_or(DefaultTpiStream);
   TpiBuilder.setVersionHeader(Tpi.Version);



More information about the llvm-commits mailing list