1af732203SDimitry Andric //===-- ObjectFilePDB.cpp -------------------------------------------------===//
2af732203SDimitry Andric //
3af732203SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4af732203SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5af732203SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6af732203SDimitry Andric //
7af732203SDimitry Andric //===----------------------------------------------------------------------===//
8af732203SDimitry Andric 
9af732203SDimitry Andric #include "ObjectFilePDB.h"
10af732203SDimitry Andric #include "lldb/Core/Module.h"
11af732203SDimitry Andric #include "lldb/Core/ModuleSpec.h"
12af732203SDimitry Andric #include "lldb/Core/PluginManager.h"
13af732203SDimitry Andric #include "lldb/Core/Section.h"
14af732203SDimitry Andric #include "lldb/Utility/StreamString.h"
15af732203SDimitry Andric #include "llvm/BinaryFormat/Magic.h"
16af732203SDimitry Andric #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
17af732203SDimitry Andric #include "llvm/DebugInfo/PDB/Native/InfoStream.h"
18af732203SDimitry Andric #include "llvm/DebugInfo/PDB/Native/NativeSession.h"
19af732203SDimitry Andric #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
20af732203SDimitry Andric #include "llvm/DebugInfo/PDB/PDB.h"
21af732203SDimitry Andric #include "llvm/Support/BinaryByteStream.h"
22af732203SDimitry Andric 
23af732203SDimitry Andric using namespace lldb;
24af732203SDimitry Andric using namespace lldb_private;
25af732203SDimitry Andric using namespace llvm::pdb;
26af732203SDimitry Andric using namespace llvm::codeview;
27af732203SDimitry Andric 
LLDB_PLUGIN_DEFINE(ObjectFilePDB)28af732203SDimitry Andric LLDB_PLUGIN_DEFINE(ObjectFilePDB)
29af732203SDimitry Andric 
30af732203SDimitry Andric static UUID GetPDBUUID(InfoStream &IS) {
31af732203SDimitry Andric   UUID::CvRecordPdb70 debug_info;
32af732203SDimitry Andric   memcpy(&debug_info.Uuid, IS.getGuid().Guid, sizeof(debug_info.Uuid));
33af732203SDimitry Andric   debug_info.Age = IS.getAge();
34af732203SDimitry Andric   return UUID::fromCvRecord(debug_info);
35af732203SDimitry Andric }
36af732203SDimitry Andric 
37af732203SDimitry Andric char ObjectFilePDB::ID;
38af732203SDimitry Andric 
Initialize()39af732203SDimitry Andric void ObjectFilePDB::Initialize() {
40af732203SDimitry Andric   PluginManager::RegisterPlugin(GetPluginNameStatic(),
41af732203SDimitry Andric                                 GetPluginDescriptionStatic(), CreateInstance,
42af732203SDimitry Andric                                 CreateMemoryInstance, GetModuleSpecifications);
43af732203SDimitry Andric }
44af732203SDimitry Andric 
Terminate()45af732203SDimitry Andric void ObjectFilePDB::Terminate() {
46af732203SDimitry Andric   PluginManager::UnregisterPlugin(CreateInstance);
47af732203SDimitry Andric }
48af732203SDimitry Andric 
GetPluginNameStatic()49af732203SDimitry Andric ConstString ObjectFilePDB::GetPluginNameStatic() {
50af732203SDimitry Andric   static ConstString g_name("pdb");
51af732203SDimitry Andric   return g_name;
52af732203SDimitry Andric }
53af732203SDimitry Andric 
GetArchitecture()54af732203SDimitry Andric ArchSpec ObjectFilePDB::GetArchitecture() {
55af732203SDimitry Andric   auto dbi_stream = m_file_up->getPDBDbiStream();
56af732203SDimitry Andric   if (!dbi_stream) {
57af732203SDimitry Andric     llvm::consumeError(dbi_stream.takeError());
58af732203SDimitry Andric     return ArchSpec();
59af732203SDimitry Andric   }
60af732203SDimitry Andric 
61af732203SDimitry Andric   PDB_Machine machine = dbi_stream->getMachineType();
62af732203SDimitry Andric   switch (machine) {
63af732203SDimitry Andric   default:
64af732203SDimitry Andric     break;
65af732203SDimitry Andric   case PDB_Machine::Amd64:
66af732203SDimitry Andric   case PDB_Machine::x86:
67af732203SDimitry Andric   case PDB_Machine::PowerPC:
68af732203SDimitry Andric   case PDB_Machine::PowerPCFP:
69af732203SDimitry Andric   case PDB_Machine::Arm:
70af732203SDimitry Andric   case PDB_Machine::ArmNT:
71af732203SDimitry Andric   case PDB_Machine::Thumb:
72af732203SDimitry Andric   case PDB_Machine::Arm64:
73af732203SDimitry Andric     ArchSpec arch;
74af732203SDimitry Andric     arch.SetArchitecture(eArchTypeCOFF, static_cast<int>(machine),
75af732203SDimitry Andric                          LLDB_INVALID_CPUTYPE);
76af732203SDimitry Andric     return arch;
77af732203SDimitry Andric   }
78af732203SDimitry Andric   return ArchSpec();
79af732203SDimitry Andric }
80af732203SDimitry Andric 
initPDBFile()81af732203SDimitry Andric bool ObjectFilePDB::initPDBFile() {
82af732203SDimitry Andric   m_file_up = loadPDBFile(m_file.GetPath(), m_allocator);
83af732203SDimitry Andric   if (!m_file_up)
84af732203SDimitry Andric     return false;
85af732203SDimitry Andric   auto info_stream = m_file_up->getPDBInfoStream();
86af732203SDimitry Andric   if (!info_stream) {
87af732203SDimitry Andric     llvm::consumeError(info_stream.takeError());
88af732203SDimitry Andric     return false;
89af732203SDimitry Andric   }
90af732203SDimitry Andric   m_uuid = GetPDBUUID(*info_stream);
91af732203SDimitry Andric   return true;
92af732203SDimitry Andric }
93af732203SDimitry Andric 
94af732203SDimitry Andric ObjectFile *
CreateInstance(const ModuleSP & module_sp,DataBufferSP & data_sp,offset_t data_offset,const FileSpec * file,offset_t file_offset,offset_t length)95af732203SDimitry Andric ObjectFilePDB::CreateInstance(const ModuleSP &module_sp, DataBufferSP &data_sp,
96af732203SDimitry Andric                               offset_t data_offset, const FileSpec *file,
97af732203SDimitry Andric                               offset_t file_offset, offset_t length) {
98af732203SDimitry Andric   auto objfile_up = std::make_unique<ObjectFilePDB>(
99af732203SDimitry Andric       module_sp, data_sp, data_offset, file, file_offset, length);
100af732203SDimitry Andric   if (!objfile_up->initPDBFile())
101af732203SDimitry Andric     return nullptr;
102af732203SDimitry Andric   return objfile_up.release();
103af732203SDimitry Andric }
104af732203SDimitry Andric 
CreateMemoryInstance(const ModuleSP & module_sp,DataBufferSP & data_sp,const ProcessSP & process_sp,addr_t header_addr)105af732203SDimitry Andric ObjectFile *ObjectFilePDB::CreateMemoryInstance(const ModuleSP &module_sp,
106af732203SDimitry Andric                                                 DataBufferSP &data_sp,
107af732203SDimitry Andric                                                 const ProcessSP &process_sp,
108af732203SDimitry Andric                                                 addr_t header_addr) {
109af732203SDimitry Andric   return nullptr;
110af732203SDimitry Andric }
111af732203SDimitry Andric 
GetModuleSpecifications(const FileSpec & file,DataBufferSP & data_sp,offset_t data_offset,offset_t file_offset,offset_t length,ModuleSpecList & specs)112af732203SDimitry Andric size_t ObjectFilePDB::GetModuleSpecifications(
113af732203SDimitry Andric     const FileSpec &file, DataBufferSP &data_sp, offset_t data_offset,
114af732203SDimitry Andric     offset_t file_offset, offset_t length, ModuleSpecList &specs) {
115af732203SDimitry Andric   const size_t initial_count = specs.GetSize();
116af732203SDimitry Andric   ModuleSpec module_spec(file);
117af732203SDimitry Andric   llvm::BumpPtrAllocator allocator;
118af732203SDimitry Andric   std::unique_ptr<PDBFile> pdb_file = loadPDBFile(file.GetPath(), allocator);
119af732203SDimitry Andric   if (!pdb_file)
120af732203SDimitry Andric     return initial_count;
121af732203SDimitry Andric 
122af732203SDimitry Andric   auto info_stream = pdb_file->getPDBInfoStream();
123af732203SDimitry Andric   if (!info_stream) {
124af732203SDimitry Andric     llvm::consumeError(info_stream.takeError());
125af732203SDimitry Andric     return initial_count;
126af732203SDimitry Andric   }
127af732203SDimitry Andric   auto dbi_stream = pdb_file->getPDBDbiStream();
128af732203SDimitry Andric   if (!dbi_stream) {
129af732203SDimitry Andric     llvm::consumeError(dbi_stream.takeError());
130af732203SDimitry Andric     return initial_count;
131af732203SDimitry Andric   }
132af732203SDimitry Andric 
133af732203SDimitry Andric   lldb_private::UUID &uuid = module_spec.GetUUID();
134af732203SDimitry Andric   uuid = GetPDBUUID(*info_stream);
135af732203SDimitry Andric 
136af732203SDimitry Andric   ArchSpec &module_arch = module_spec.GetArchitecture();
137af732203SDimitry Andric   switch (dbi_stream->getMachineType()) {
138af732203SDimitry Andric   case PDB_Machine::Amd64:
139af732203SDimitry Andric     module_arch.SetTriple("x86_64-pc-windows");
140af732203SDimitry Andric     specs.Append(module_spec);
141af732203SDimitry Andric     break;
142af732203SDimitry Andric   case PDB_Machine::x86:
143af732203SDimitry Andric     module_arch.SetTriple("i386-pc-windows");
144af732203SDimitry Andric     specs.Append(module_spec);
145af732203SDimitry Andric     module_arch.SetTriple("i686-pc-windows");
146af732203SDimitry Andric     specs.Append(module_spec);
147af732203SDimitry Andric     break;
148af732203SDimitry Andric   case PDB_Machine::ArmNT:
149af732203SDimitry Andric     module_arch.SetTriple("armv7-pc-windows");
150af732203SDimitry Andric     specs.Append(module_spec);
151af732203SDimitry Andric     break;
152af732203SDimitry Andric   case PDB_Machine::Arm64:
153af732203SDimitry Andric     module_arch.SetTriple("aarch64-pc-windows");
154af732203SDimitry Andric     specs.Append(module_spec);
155af732203SDimitry Andric     break;
156af732203SDimitry Andric   default:
157af732203SDimitry Andric     break;
158af732203SDimitry Andric   }
159af732203SDimitry Andric 
160af732203SDimitry Andric   return specs.GetSize() - initial_count;
161af732203SDimitry Andric }
162af732203SDimitry Andric 
ObjectFilePDB(const ModuleSP & module_sp,DataBufferSP & data_sp,offset_t data_offset,const FileSpec * file,offset_t offset,offset_t length)163af732203SDimitry Andric ObjectFilePDB::ObjectFilePDB(const ModuleSP &module_sp, DataBufferSP &data_sp,
164af732203SDimitry Andric                              offset_t data_offset, const FileSpec *file,
165af732203SDimitry Andric                              offset_t offset, offset_t length)
166af732203SDimitry Andric     : ObjectFile(module_sp, file, offset, length, data_sp, data_offset) {}
167af732203SDimitry Andric 
168af732203SDimitry Andric std::unique_ptr<PDBFile>
loadPDBFile(std::string PdbPath,llvm::BumpPtrAllocator & Allocator)169af732203SDimitry Andric ObjectFilePDB::loadPDBFile(std::string PdbPath,
170af732203SDimitry Andric                            llvm::BumpPtrAllocator &Allocator) {
171af732203SDimitry Andric   llvm::file_magic magic;
172af732203SDimitry Andric   auto ec = llvm::identify_magic(PdbPath, magic);
173af732203SDimitry Andric   if (ec || magic != llvm::file_magic::pdb)
174af732203SDimitry Andric     return nullptr;
175af732203SDimitry Andric   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> ErrorOrBuffer =
176*5f7ddb14SDimitry Andric       llvm::MemoryBuffer::getFile(PdbPath, /*IsText=*/false,
177af732203SDimitry Andric                                   /*RequiresNullTerminator=*/false);
178af732203SDimitry Andric   if (!ErrorOrBuffer)
179af732203SDimitry Andric     return nullptr;
180af732203SDimitry Andric   std::unique_ptr<llvm::MemoryBuffer> Buffer = std::move(*ErrorOrBuffer);
181af732203SDimitry Andric 
182af732203SDimitry Andric   llvm::StringRef Path = Buffer->getBufferIdentifier();
183af732203SDimitry Andric   auto Stream = std::make_unique<llvm::MemoryBufferByteStream>(
184af732203SDimitry Andric       std::move(Buffer), llvm::support::little);
185af732203SDimitry Andric 
186af732203SDimitry Andric   auto File = std::make_unique<PDBFile>(Path, std::move(Stream), Allocator);
187af732203SDimitry Andric   if (auto EC = File->parseFileHeaders()) {
188af732203SDimitry Andric     llvm::consumeError(std::move(EC));
189af732203SDimitry Andric     return nullptr;
190af732203SDimitry Andric   }
191af732203SDimitry Andric   if (auto EC = File->parseStreamData()) {
192af732203SDimitry Andric     llvm::consumeError(std::move(EC));
193af732203SDimitry Andric     return nullptr;
194af732203SDimitry Andric   }
195af732203SDimitry Andric 
196af732203SDimitry Andric   return File;
197af732203SDimitry Andric }
198