Skip to content

Commit d99c840

Browse files
authored
[llvm-pdbutil] Create DBI section headers in yaml2pdb (#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
1 parent 9fa15ef commit d99c840

File tree

6 files changed

+238
-0
lines changed

6 files changed

+238
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# RUN: llvm-pdbutil yaml2pdb %s --pdb=%t.pdb
2+
# RUN: llvm-pdbutil dump --section-headers --section-map %t.pdb | FileCheck --check-prefix=CHECK-YAML2PDB %s
3+
4+
# RUN: llvm-pdbutil pdb2yaml --section-headers %t.pdb > %t.yaml
5+
# RUN: FileCheck --input-file=%t.yaml --check-prefix=CHECK-PDB2YAML %s
6+
7+
# CHECK-YAML2PDB: Section Headers
8+
# CHECK-YAML2PDB: SECTION HEADER #1
9+
# CHECK-YAML2PDB: .text name
10+
# CHECK-YAML2PDB: 47 virtual size
11+
# CHECK-YAML2PDB: 1000 virtual address
12+
# CHECK-YAML2PDB: 200 size of raw data
13+
# CHECK-YAML2PDB: 400 file pointer to raw data
14+
# CHECK-YAML2PDB: 0 file pointer to relocation table
15+
# CHECK-YAML2PDB: 0 file pointer to line numbers
16+
# CHECK-YAML2PDB: 0 number of relocations
17+
# CHECK-YAML2PDB: 0 number of line numbers
18+
# CHECK-YAML2PDB: 60000020 flags
19+
# CHECK-YAML2PDB: IMAGE_SCN_CNT_CODE
20+
# CHECK-YAML2PDB: IMAGE_SCN_MEM_EXECUTE
21+
# CHECK-YAML2PDB: IMAGE_SCN_MEM_READ
22+
23+
# CHECK-YAML2PDB: SECTION HEADER #2
24+
# CHECK-YAML2PDB: .rdata name
25+
# CHECK-YAML2PDB: 64 virtual size
26+
# CHECK-YAML2PDB: 2000 virtual address
27+
# CHECK-YAML2PDB: 200 size of raw data
28+
# CHECK-YAML2PDB: 600 file pointer to raw data
29+
# CHECK-YAML2PDB: 0 file pointer to relocation table
30+
# CHECK-YAML2PDB: 0 file pointer to line numbers
31+
# CHECK-YAML2PDB: 0 number of relocations
32+
# CHECK-YAML2PDB: 0 number of line numbers
33+
# CHECK-YAML2PDB: 40000040 flags
34+
# CHECK-YAML2PDB: IMAGE_SCN_CNT_INITIALIZED_DATA
35+
# CHECK-YAML2PDB: IMAGE_SCN_MEM_READ
36+
37+
# CHECK-YAML2PDB: SECTION HEADER #3
38+
# CHECK-YAML2PDB: .pdata name
39+
# CHECK-YAML2PDB: 18 virtual size
40+
# CHECK-YAML2PDB: 3000 virtual address
41+
# CHECK-YAML2PDB: 200 size of raw data
42+
# CHECK-YAML2PDB: 800 file pointer to raw data
43+
# CHECK-YAML2PDB: 0 file pointer to relocation table
44+
# CHECK-YAML2PDB: 0 file pointer to line numbers
45+
# CHECK-YAML2PDB: 0 number of relocations
46+
# CHECK-YAML2PDB: 0 number of line numbers
47+
# CHECK-YAML2PDB: 40000040 flags
48+
# CHECK-YAML2PDB: IMAGE_SCN_CNT_INITIALIZED_DATA
49+
# CHECK-YAML2PDB: IMAGE_SCN_MEM_READ
50+
51+
# CHECK-YAML2PDB: Section Map
52+
# CHECK-YAML2PDB: Section 0000 | ovl = 0, group = 0, frame = 1, name = 65535
53+
# CHECK-YAML2PDB: class = 65535, offset = 0, size = 71
54+
# CHECK-YAML2PDB: flags = read | execute | 32 bit addr | selector
55+
# CHECK-YAML2PDB: Section 0001 | ovl = 0, group = 0, frame = 2, name = 65535
56+
# CHECK-YAML2PDB: class = 65535, offset = 0, size = 100
57+
# CHECK-YAML2PDB: flags = read | 32 bit addr | selector
58+
# CHECK-YAML2PDB: Section 0002 | ovl = 0, group = 0, frame = 3, name = 65535
59+
# CHECK-YAML2PDB: class = 65535, offset = 0, size = 24
60+
# CHECK-YAML2PDB: flags = read | 32 bit addr | selector
61+
# CHECK-YAML2PDB: Section 0003 | ovl = 0, group = 0, frame = 4, name = 65535
62+
# CHECK-YAML2PDB: class = 65535, offset = 0, size = 4294967295
63+
# CHECK-YAML2PDB: flags = 32 bit addr | absolute addr
64+
65+
# CHECK-PDB2YAML:DbiStream:
66+
# CHECK-PDB2YAML: SectionHeaders:
67+
# CHECK-PDB2YAML: - Name: .text
68+
# CHECK-PDB2YAML: VirtualSize: 71
69+
# CHECK-PDB2YAML: VirtualAddress: 4096
70+
# CHECK-PDB2YAML: SizeOfRawData: 512
71+
# CHECK-PDB2YAML: PointerToRawData: 1024
72+
# CHECK-PDB2YAML: PointerToRelocations: 0
73+
# CHECK-PDB2YAML: PointerToLinenumbers: 0
74+
# CHECK-PDB2YAML: NumberOfRelocations: 0
75+
# CHECK-PDB2YAML: NumberOfLinenumbers: 0
76+
# CHECK-PDB2YAML: Characteristics: 1610612768
77+
# CHECK-PDB2YAML: - Name: .rdata
78+
# CHECK-PDB2YAML: VirtualSize: 100
79+
# CHECK-PDB2YAML: VirtualAddress: 8192
80+
# CHECK-PDB2YAML: SizeOfRawData: 512
81+
# CHECK-PDB2YAML: PointerToRawData: 1536
82+
# CHECK-PDB2YAML: PointerToRelocations: 0
83+
# CHECK-PDB2YAML: PointerToLinenumbers: 0
84+
# CHECK-PDB2YAML: NumberOfRelocations: 0
85+
# CHECK-PDB2YAML: NumberOfLinenumbers: 0
86+
# CHECK-PDB2YAML: Characteristics: 1073741888
87+
# CHECK-PDB2YAML: - Name: .pdata
88+
# CHECK-PDB2YAML: VirtualSize: 24
89+
# CHECK-PDB2YAML: VirtualAddress: 12288
90+
# CHECK-PDB2YAML: SizeOfRawData: 512
91+
# CHECK-PDB2YAML: PointerToRawData: 2048
92+
# CHECK-PDB2YAML: PointerToRelocations: 0
93+
# CHECK-PDB2YAML: PointerToLinenumbers: 0
94+
# CHECK-PDB2YAML: NumberOfRelocations: 0
95+
# CHECK-PDB2YAML: NumberOfLinenumbers: 0
96+
# CHECK-PDB2YAML: Characteristics: 1073741888
97+
98+
---
99+
DbiStream:
100+
SectionHeaders:
101+
- Name: .text
102+
VirtualSize: 71
103+
VirtualAddress: 4096
104+
SizeOfRawData: 512
105+
PointerToRawData: 1024
106+
PointerToRelocations: 0
107+
PointerToLinenumbers: 0
108+
NumberOfRelocations: 0
109+
NumberOfLinenumbers: 0
110+
Characteristics: 1610612768
111+
- Name: .rdata
112+
VirtualSize: 100
113+
VirtualAddress: 8192
114+
SizeOfRawData: 512
115+
PointerToRawData: 1536
116+
PointerToRelocations: 0
117+
PointerToLinenumbers: 0
118+
NumberOfRelocations: 0
119+
NumberOfLinenumbers: 0
120+
Characteristics: 1073741888
121+
- Name: .pdata
122+
VirtualSize: 24
123+
VirtualAddress: 12288
124+
SizeOfRawData: 512
125+
PointerToRawData: 2048
126+
PointerToRelocations: 0
127+
PointerToLinenumbers: 0
128+
NumberOfRelocations: 0
129+
NumberOfLinenumbers: 0
130+
Characteristics: 1073741888
131+
...

llvm/tools/llvm-pdbutil/PdbYaml.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ using namespace llvm::pdb;
2222
using namespace llvm::pdb::yaml;
2323
using namespace llvm::yaml;
2424

25+
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::CoffSectionHeader)
2526
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::NamedStreamMapping)
2627
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::PdbDbiModuleInfo)
2728
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::StreamBlockList)
@@ -135,6 +136,49 @@ void MappingTraits<msf::SuperBlock>::mapping(IO &IO, msf::SuperBlock &SB) {
135136
IO.mapOptional("BlockMapAddr", SB.BlockMapAddr, u32(0U));
136137
}
137138

139+
CoffSectionHeader::CoffSectionHeader() = default;
140+
141+
CoffSectionHeader::CoffSectionHeader(const object::coff_section &Section)
142+
: Name(Section.Name), VirtualSize(Section.VirtualSize),
143+
VirtualAddress(Section.VirtualAddress),
144+
SizeOfRawData(Section.SizeOfRawData),
145+
PointerToRawData(Section.PointerToRawData),
146+
PointerToRelocations(Section.PointerToRelocations),
147+
PointerToLinenumbers(Section.PointerToLinenumbers),
148+
NumberOfRelocations(Section.NumberOfRelocations),
149+
NumberOfLinenumbers(Section.NumberOfLinenumbers),
150+
Characteristics(Section.Characteristics) {}
151+
152+
object::coff_section CoffSectionHeader::toCoffSection() const {
153+
object::coff_section Sec;
154+
std::memset(Sec.Name, 0, COFF::NameSize);
155+
std::memcpy(Sec.Name, Name.data(),
156+
std::min(static_cast<size_t>(COFF::NameSize), Name.size()));
157+
Sec.VirtualSize = VirtualSize;
158+
Sec.VirtualAddress = VirtualAddress;
159+
Sec.SizeOfRawData = SizeOfRawData;
160+
Sec.PointerToRawData = PointerToRawData;
161+
Sec.PointerToRelocations = PointerToRelocations;
162+
Sec.PointerToLinenumbers = PointerToLinenumbers;
163+
Sec.NumberOfRelocations = NumberOfRelocations;
164+
Sec.NumberOfLinenumbers = NumberOfLinenumbers;
165+
Sec.Characteristics = Characteristics;
166+
return Sec;
167+
}
168+
169+
void MappingTraits<CoffSectionHeader>::mapping(IO &IO, CoffSectionHeader &Obj) {
170+
IO.mapRequired("Name", Obj.Name);
171+
IO.mapOptional("VirtualSize", Obj.VirtualSize);
172+
IO.mapOptional("VirtualAddress", Obj.VirtualAddress);
173+
IO.mapOptional("SizeOfRawData", Obj.SizeOfRawData);
174+
IO.mapOptional("PointerToRawData", Obj.PointerToRawData);
175+
IO.mapOptional("PointerToRelocations", Obj.PointerToRelocations);
176+
IO.mapOptional("PointerToLinenumbers", Obj.PointerToLinenumbers);
177+
IO.mapOptional("NumberOfRelocations", Obj.NumberOfRelocations);
178+
IO.mapOptional("NumberOfLinenumbers", Obj.NumberOfLinenumbers);
179+
IO.mapOptional("Characteristics", Obj.Characteristics);
180+
}
181+
138182
void MappingTraits<StreamBlockList>::mapping(IO &IO, StreamBlockList &SB) {
139183
IO.mapRequired("Stream", SB.Blocks);
140184
}
@@ -163,6 +207,7 @@ void MappingTraits<PdbDbiStream>::mapping(IO &IO, PdbDbiStream &Obj) {
163207
IO.setContext(&Obj.FakeHeader);
164208
}
165209
IO.mapOptional("Modules", Obj.ModInfos);
210+
IO.mapOptional("SectionHeaders", Obj.SectionHeaders);
166211
}
167212

168213
void MappingTraits<PdbTpiStream>::mapping(IO &IO,

llvm/tools/llvm-pdbutil/PdbYaml.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
1919
#include "llvm/DebugInfo/PDB/Native/RawConstants.h"
2020
#include "llvm/DebugInfo/PDB/PDBTypes.h"
21+
#include "llvm/Object/COFF.h"
2122
#include "llvm/ObjectYAML/CodeViewYAMLDebugSections.h"
2223
#include "llvm/ObjectYAML/CodeViewYAMLSymbols.h"
2324
#include "llvm/ObjectYAML/CodeViewYAMLTypes.h"
@@ -40,6 +41,24 @@ struct MSFHeaders {
4041
uint64_t FileSize = 0;
4142
};
4243

44+
struct CoffSectionHeader {
45+
CoffSectionHeader();
46+
CoffSectionHeader(const object::coff_section &Section);
47+
48+
object::coff_section toCoffSection() const;
49+
50+
StringRef Name;
51+
uint32_t VirtualSize = 0;
52+
uint32_t VirtualAddress = 0;
53+
uint32_t SizeOfRawData = 0;
54+
uint32_t PointerToRawData = 0;
55+
uint32_t PointerToRelocations = 0;
56+
uint32_t PointerToLinenumbers = 0;
57+
uint16_t NumberOfRelocations = 0;
58+
uint16_t NumberOfLinenumbers = 0;
59+
uint32_t Characteristics = 0;
60+
};
61+
4362
struct StreamBlockList {
4463
std::vector<uint32_t> Blocks;
4564
};
@@ -82,6 +101,7 @@ struct PdbDbiStream {
82101

83102
std::vector<PdbDbiModuleInfo> ModInfos;
84103
COFF::header FakeHeader;
104+
std::vector<CoffSectionHeader> SectionHeaders;
85105
};
86106

87107
struct PdbTpiStream {
@@ -113,6 +133,7 @@ struct PdbObject {
113133
}
114134
}
115135

136+
LLVM_YAML_DECLARE_MAPPING_TRAITS_PRIVATE(pdb::yaml::CoffSectionHeader)
116137
LLVM_YAML_DECLARE_MAPPING_TRAITS_PRIVATE(pdb::yaml::PdbObject)
117138
LLVM_YAML_DECLARE_MAPPING_TRAITS_PRIVATE(pdb::yaml::MSFHeaders)
118139
LLVM_YAML_DECLARE_MAPPING_TRAITS_PRIVATE(msf::SuperBlock)

llvm/tools/llvm-pdbutil/YAMLOutputStyle.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,24 @@ Error YAMLOutputStyle::dumpDbiStream() {
285285
}
286286
}
287287
}
288+
289+
if (opts::pdb2yaml::DumpSectionHeaders) {
290+
for (const auto &Section : DS.getSectionHeaders()) {
291+
yaml::CoffSectionHeader Hdr;
292+
Hdr.Name = Section.Name;
293+
Hdr.VirtualSize = Section.VirtualSize;
294+
Hdr.VirtualAddress = Section.VirtualAddress;
295+
Hdr.SizeOfRawData = Section.SizeOfRawData;
296+
Hdr.PointerToRawData = Section.PointerToRawData;
297+
Hdr.PointerToRelocations = Section.PointerToRelocations;
298+
Hdr.PointerToLinenumbers = Section.PointerToLinenumbers;
299+
Hdr.NumberOfRelocations = Section.NumberOfRelocations;
300+
Hdr.NumberOfLinenumbers = Section.NumberOfLinenumbers;
301+
Hdr.Characteristics = Section.Characteristics;
302+
Obj.DbiStream->SectionHeaders.emplace_back(Hdr);
303+
}
304+
}
305+
288306
return Error::success();
289307
}
290308

llvm/tools/llvm-pdbutil/llvm-pdbutil.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,10 @@ cl::list<ModuleSubsection> DumpModuleSubsections(
716716
cl::opt<bool> DumpModuleSyms("module-syms", cl::desc("dump module symbols"),
717717
cl::cat(FileOptions),
718718
cl::sub(PdbToYamlSubcommand));
719+
cl::opt<bool> DumpSectionHeaders("section-headers",
720+
cl::desc("Dump section headers."),
721+
cl::cat(FileOptions),
722+
cl::sub(PdbToYamlSubcommand));
719723

720724
cl::list<std::string> InputFilename(cl::Positional,
721725
cl::desc("<input PDB file>"), cl::Required,
@@ -865,6 +869,20 @@ static void yamlToPdb(StringRef Path) {
865869
}
866870
}
867871

872+
std::vector<object::coff_section> Sections;
873+
if (!Dbi.SectionHeaders.empty()) {
874+
for (const auto &Hdr : Dbi.SectionHeaders)
875+
Sections.emplace_back(Hdr.toCoffSection());
876+
877+
DbiBuilder.createSectionMap(Sections);
878+
ExitOnErr(DbiBuilder.addDbgStream(
879+
pdb::DbgHeaderType::SectionHdr,
880+
// FIXME: Downcasting to an ArrayRef<uint8_t> should use a helper
881+
// function in LLVM
882+
ArrayRef<uint8_t>{(const uint8_t *)Sections.data(),
883+
Sections.size() * sizeof(object::coff_section)}));
884+
}
885+
868886
auto &TpiBuilder = Builder.getTpiBuilder();
869887
const auto &Tpi = YamlObj.TpiStream.value_or(DefaultTpiStream);
870888
TpiBuilder.setVersionHeader(Tpi.Version);
@@ -1541,6 +1559,7 @@ int main(int Argc, const char **Argv) {
15411559
opts::pdb2yaml::DumpModules = true;
15421560
opts::pdb2yaml::DumpModuleFiles = true;
15431561
opts::pdb2yaml::DumpModuleSyms = true;
1562+
opts::pdb2yaml::DumpSectionHeaders = true;
15441563
opts::pdb2yaml::DumpModuleSubsections.push_back(
15451564
opts::ModuleSubsection::All);
15461565
}
@@ -1551,6 +1570,9 @@ int main(int Argc, const char **Argv) {
15511570

15521571
if (opts::pdb2yaml::DumpModules)
15531572
opts::pdb2yaml::DbiStream = true;
1573+
1574+
if (opts::pdb2yaml::DumpSectionHeaders)
1575+
opts::pdb2yaml::DbiStream = true;
15541576
}
15551577

15561578
llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded);

llvm/tools/llvm-pdbutil/llvm-pdbutil.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ extern llvm::cl::opt<bool> DumpModules;
201201
extern llvm::cl::opt<bool> DumpModuleFiles;
202202
extern llvm::cl::list<ModuleSubsection> DumpModuleSubsections;
203203
extern llvm::cl::opt<bool> DumpModuleSyms;
204+
extern llvm::cl::opt<bool> DumpSectionHeaders;
204205
} // namespace pdb2yaml
205206

206207
namespace explain {

0 commit comments

Comments
 (0)