15 #include "llvm/BinaryFormat/Magic.h"
16 #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
17 #include "llvm/DebugInfo/PDB/Native/InfoStream.h"
18 #include "llvm/DebugInfo/PDB/Native/NativeSession.h"
19 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
20 #include "llvm/DebugInfo/PDB/PDB.h"
21 #include "llvm/Support/BinaryByteStream.h"
32 memcpy(&debug_info.
Uuid, IS.getGuid().Guid,
sizeof(debug_info.
Uuid));
33 debug_info.
Age = IS.getAge();
34 return UUID::fromCvRecord(debug_info);
39 void ObjectFilePDB::Initialize() {
40 PluginManager::RegisterPlugin(GetPluginNameStatic(),
41 GetPluginDescriptionStatic(), CreateInstance,
42 CreateMemoryInstance, GetModuleSpecifications);
45 void ObjectFilePDB::Terminate() {
46 PluginManager::UnregisterPlugin(CreateInstance);
50 auto dbi_stream = m_file_up->getPDBDbiStream();
52 llvm::consumeError(dbi_stream.takeError());
56 PDB_Machine machine = dbi_stream->getMachineType();
60 case PDB_Machine::Amd64:
61 case PDB_Machine::x86:
62 case PDB_Machine::PowerPC:
63 case PDB_Machine::PowerPCFP:
64 case PDB_Machine::Arm:
65 case PDB_Machine::ArmNT:
66 case PDB_Machine::Thumb:
67 case PDB_Machine::Arm64:
76 bool ObjectFilePDB::initPDBFile() {
77 m_file_up = loadPDBFile(m_file.GetPath(), m_allocator);
80 auto info_stream = m_file_up->getPDBInfoStream();
82 llvm::consumeError(info_stream.takeError());
90 ObjectFilePDB::CreateInstance(
const ModuleSP &module_sp, DataBufferSP data_sp,
93 auto objfile_up = std::make_unique<ObjectFilePDB>(
94 module_sp, data_sp, data_offset, file, file_offset, length);
95 if (!objfile_up->initPDBFile())
97 return objfile_up.release();
100 ObjectFile *ObjectFilePDB::CreateMemoryInstance(
const ModuleSP &module_sp,
101 WritableDataBufferSP data_sp,
102 const ProcessSP &process_sp,
107 size_t ObjectFilePDB::GetModuleSpecifications(
110 const size_t initial_count = specs.
GetSize();
112 llvm::BumpPtrAllocator allocator;
113 std::unique_ptr<PDBFile> pdb_file = loadPDBFile(file.
GetPath(), allocator);
115 return initial_count;
117 auto info_stream = pdb_file->getPDBInfoStream();
119 llvm::consumeError(info_stream.takeError());
120 return initial_count;
122 auto dbi_stream = pdb_file->getPDBDbiStream();
124 llvm::consumeError(dbi_stream.takeError());
125 return initial_count;
132 switch (dbi_stream->getMachineType()) {
133 case PDB_Machine::Amd64:
134 module_arch.
SetTriple(
"x86_64-pc-windows");
135 specs.
Append(module_spec);
137 case PDB_Machine::x86:
138 module_arch.
SetTriple(
"i386-pc-windows");
139 specs.
Append(module_spec);
140 module_arch.
SetTriple(
"i686-pc-windows");
141 specs.
Append(module_spec);
143 case PDB_Machine::ArmNT:
144 module_arch.
SetTriple(
"armv7-pc-windows");
145 specs.
Append(module_spec);
147 case PDB_Machine::Arm64:
148 module_arch.
SetTriple(
"aarch64-pc-windows");
149 specs.
Append(module_spec);
155 return specs.
GetSize() - initial_count;
158 ObjectFilePDB::ObjectFilePDB(
const ModuleSP &module_sp, DataBufferSP &data_sp,
161 :
ObjectFile(module_sp, file, offset, length, data_sp, data_offset) {}
163 std::unique_ptr<PDBFile>
165 llvm::BumpPtrAllocator &Allocator) {
166 llvm::file_magic magic;
167 auto ec = llvm::identify_magic(PdbPath, magic);
168 if (ec || magic != llvm::file_magic::pdb)
170 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> ErrorOrBuffer =
171 llvm::MemoryBuffer::getFile(PdbPath,
false,
175 std::unique_ptr<llvm::MemoryBuffer> Buffer = std::move(*ErrorOrBuffer);
177 llvm::StringRef Path = Buffer->getBufferIdentifier();
178 auto Stream = std::make_unique<llvm::MemoryBufferByteStream>(
179 std::move(Buffer), llvm::support::little);
181 auto File = std::make_unique<PDBFile>(Path, std::move(
Stream), Allocator);
182 if (
auto EC =
File->parseFileHeaders()) {
183 llvm::consumeError(std::move(EC));
186 if (
auto EC =
File->parseStreamData()) {
187 llvm::consumeError(std::move(EC));